1 /*
2  * drivers/mtd/nand/diskonchip.c
3  *
4  * (C) 2003 Red Hat, Inc.
5  * (C) 2004 Dan Brown <dan_brown@ieee.org>
6  * (C) 2004 Kalev Lember <kalev@smartlink.ee>
7  *
8  * Author: David Woodhouse <dwmw2@infradead.org>
9  * Additional Diskonchip 2000 and Millennium support by Dan Brown <dan_brown@ieee.org>
10  * Diskonchip Millennium Plus support by Kalev Lember <kalev@smartlink.ee>
11  *
12  * Error correction code lifted from the old docecc code
13  * Author: Fabrice Bellard (fabrice.bellard@netgem.com)
14  * Copyright (C) 2000 Netgem S.A.
15  * converted to the generic Reed-Solomon library by Thomas Gleixner <tglx@linutronix.de>
16  *
17  * Interface to generic NAND code for M-Systems DiskOnChip devices
18  */
19 
20 #include <linux/kernel.h>
21 #include <linux/init.h>
22 #include <linux/sched.h>
23 #include <linux/delay.h>
24 #include <linux/rslib.h>
25 #include <linux/moduleparam.h>
26 #include <linux/slab.h>
27 #include <linux/io.h>
28 
29 #include <linux/mtd/mtd.h>
30 #include <linux/mtd/nand.h>
31 #include <linux/mtd/doc2000.h>
32 #include <linux/mtd/partitions.h>
33 #include <linux/mtd/inftl.h>
34 #include <linux/module.h>
35 
36 /* Where to look for the devices? */
37 #ifndef CONFIG_MTD_NAND_DISKONCHIP_PROBE_ADDRESS
38 #define CONFIG_MTD_NAND_DISKONCHIP_PROBE_ADDRESS 0
39 #endif
40 
41 static unsigned long doc_locations[] __initdata = {
42 #if defined (__alpha__) || defined(__i386__) || defined(__x86_64__)
43 #ifdef CONFIG_MTD_NAND_DISKONCHIP_PROBE_HIGH
44 	0xfffc8000, 0xfffca000, 0xfffcc000, 0xfffce000,
45 	0xfffd0000, 0xfffd2000, 0xfffd4000, 0xfffd6000,
46 	0xfffd8000, 0xfffda000, 0xfffdc000, 0xfffde000,
47 	0xfffe0000, 0xfffe2000, 0xfffe4000, 0xfffe6000,
48 	0xfffe8000, 0xfffea000, 0xfffec000, 0xfffee000,
49 #else
50 	0xc8000, 0xca000, 0xcc000, 0xce000,
51 	0xd0000, 0xd2000, 0xd4000, 0xd6000,
52 	0xd8000, 0xda000, 0xdc000, 0xde000,
53 	0xe0000, 0xe2000, 0xe4000, 0xe6000,
54 	0xe8000, 0xea000, 0xec000, 0xee000,
55 #endif
56 #endif
57 	0xffffffff };
58 
59 static struct mtd_info *doclist = NULL;
60 
61 struct doc_priv {
62 	void __iomem *virtadr;
63 	unsigned long physadr;
64 	u_char ChipID;
65 	u_char CDSNControl;
66 	int chips_per_floor;	/* The number of chips detected on each floor */
67 	int curfloor;
68 	int curchip;
69 	int mh0_page;
70 	int mh1_page;
71 	struct mtd_info *nextdoc;
72 
73 	/* Handle the last stage of initialization (BBT scan, partitioning) */
74 	int (*late_init)(struct mtd_info *mtd);
75 };
76 
77 /* This is the syndrome computed by the HW ecc generator upon reading an empty
78    page, one with all 0xff for data and stored ecc code. */
79 static u_char empty_read_syndrome[6] = { 0x26, 0xff, 0x6d, 0x47, 0x73, 0x7a };
80 
81 /* This is the ecc value computed by the HW ecc generator upon writing an empty
82    page, one with all 0xff for data. */
83 static u_char empty_write_ecc[6] = { 0x4b, 0x00, 0xe2, 0x0e, 0x93, 0xf7 };
84 
85 #define INFTL_BBT_RESERVED_BLOCKS 4
86 
87 #define DoC_is_MillenniumPlus(doc) ((doc)->ChipID == DOC_ChipID_DocMilPlus16 || (doc)->ChipID == DOC_ChipID_DocMilPlus32)
88 #define DoC_is_Millennium(doc) ((doc)->ChipID == DOC_ChipID_DocMil)
89 #define DoC_is_2000(doc) ((doc)->ChipID == DOC_ChipID_Doc2k)
90 
91 static void doc200x_hwcontrol(struct mtd_info *mtd, int cmd,
92 			      unsigned int bitmask);
93 static void doc200x_select_chip(struct mtd_info *mtd, int chip);
94 
95 static int debug = 0;
96 module_param(debug, int, 0);
97 
98 static int try_dword = 1;
99 module_param(try_dword, int, 0);
100 
101 static int no_ecc_failures = 0;
102 module_param(no_ecc_failures, int, 0);
103 
104 static int no_autopart = 0;
105 module_param(no_autopart, int, 0);
106 
107 static int show_firmware_partition = 0;
108 module_param(show_firmware_partition, int, 0);
109 
110 #ifdef CONFIG_MTD_NAND_DISKONCHIP_BBTWRITE
111 static int inftl_bbt_write = 1;
112 #else
113 static int inftl_bbt_write = 0;
114 #endif
115 module_param(inftl_bbt_write, int, 0);
116 
117 static unsigned long doc_config_location = CONFIG_MTD_NAND_DISKONCHIP_PROBE_ADDRESS;
118 module_param(doc_config_location, ulong, 0);
119 MODULE_PARM_DESC(doc_config_location, "Physical memory address at which to probe for DiskOnChip");
120 
121 /* Sector size for HW ECC */
122 #define SECTOR_SIZE 512
123 /* The sector bytes are packed into NB_DATA 10 bit words */
124 #define NB_DATA (((SECTOR_SIZE + 1) * 8 + 6) / 10)
125 /* Number of roots */
126 #define NROOTS 4
127 /* First consective root */
128 #define FCR 510
129 /* Number of symbols */
130 #define NN 1023
131 
132 /* the Reed Solomon control structure */
133 static struct rs_control *rs_decoder;
134 
135 /*
136  * The HW decoder in the DoC ASIC's provides us a error syndrome,
137  * which we must convert to a standard syndrome usable by the generic
138  * Reed-Solomon library code.
139  *
140  * Fabrice Bellard figured this out in the old docecc code. I added
141  * some comments, improved a minor bit and converted it to make use
142  * of the generic Reed-Solomon library. tglx
143  */
doc_ecc_decode(struct rs_control * rs,uint8_t * data,uint8_t * ecc)144 static int doc_ecc_decode(struct rs_control *rs, uint8_t *data, uint8_t *ecc)
145 {
146 	int i, j, nerr, errpos[8];
147 	uint8_t parity;
148 	uint16_t ds[4], s[5], tmp, errval[8], syn[4];
149 
150 	memset(syn, 0, sizeof(syn));
151 	/* Convert the ecc bytes into words */
152 	ds[0] = ((ecc[4] & 0xff) >> 0) | ((ecc[5] & 0x03) << 8);
153 	ds[1] = ((ecc[5] & 0xfc) >> 2) | ((ecc[2] & 0x0f) << 6);
154 	ds[2] = ((ecc[2] & 0xf0) >> 4) | ((ecc[3] & 0x3f) << 4);
155 	ds[3] = ((ecc[3] & 0xc0) >> 6) | ((ecc[0] & 0xff) << 2);
156 	parity = ecc[1];
157 
158 	/* Initialize the syndrome buffer */
159 	for (i = 0; i < NROOTS; i++)
160 		s[i] = ds[0];
161 	/*
162 	 *  Evaluate
163 	 *  s[i] = ds[3]x^3 + ds[2]x^2 + ds[1]x^1 + ds[0]
164 	 *  where x = alpha^(FCR + i)
165 	 */
166 	for (j = 1; j < NROOTS; j++) {
167 		if (ds[j] == 0)
168 			continue;
169 		tmp = rs->index_of[ds[j]];
170 		for (i = 0; i < NROOTS; i++)
171 			s[i] ^= rs->alpha_to[rs_modnn(rs, tmp + (FCR + i) * j)];
172 	}
173 
174 	/* Calc syn[i] = s[i] / alpha^(v + i) */
175 	for (i = 0; i < NROOTS; i++) {
176 		if (s[i])
177 			syn[i] = rs_modnn(rs, rs->index_of[s[i]] + (NN - FCR - i));
178 	}
179 	/* Call the decoder library */
180 	nerr = decode_rs16(rs, NULL, NULL, 1019, syn, 0, errpos, 0, errval);
181 
182 	/* Incorrectable errors ? */
183 	if (nerr < 0)
184 		return nerr;
185 
186 	/*
187 	 * Correct the errors. The bitpositions are a bit of magic,
188 	 * but they are given by the design of the de/encoder circuit
189 	 * in the DoC ASIC's.
190 	 */
191 	for (i = 0; i < nerr; i++) {
192 		int index, bitpos, pos = 1015 - errpos[i];
193 		uint8_t val;
194 		if (pos >= NB_DATA && pos < 1019)
195 			continue;
196 		if (pos < NB_DATA) {
197 			/* extract bit position (MSB first) */
198 			pos = 10 * (NB_DATA - 1 - pos) - 6;
199 			/* now correct the following 10 bits. At most two bytes
200 			   can be modified since pos is even */
201 			index = (pos >> 3) ^ 1;
202 			bitpos = pos & 7;
203 			if ((index >= 0 && index < SECTOR_SIZE) || index == (SECTOR_SIZE + 1)) {
204 				val = (uint8_t) (errval[i] >> (2 + bitpos));
205 				parity ^= val;
206 				if (index < SECTOR_SIZE)
207 					data[index] ^= val;
208 			}
209 			index = ((pos >> 3) + 1) ^ 1;
210 			bitpos = (bitpos + 10) & 7;
211 			if (bitpos == 0)
212 				bitpos = 8;
213 			if ((index >= 0 && index < SECTOR_SIZE) || index == (SECTOR_SIZE + 1)) {
214 				val = (uint8_t) (errval[i] << (8 - bitpos));
215 				parity ^= val;
216 				if (index < SECTOR_SIZE)
217 					data[index] ^= val;
218 			}
219 		}
220 	}
221 	/* If the parity is wrong, no rescue possible */
222 	return parity ? -EBADMSG : nerr;
223 }
224 
DoC_Delay(struct doc_priv * doc,unsigned short cycles)225 static void DoC_Delay(struct doc_priv *doc, unsigned short cycles)
226 {
227 	volatile char dummy;
228 	int i;
229 
230 	for (i = 0; i < cycles; i++) {
231 		if (DoC_is_Millennium(doc))
232 			dummy = ReadDOC(doc->virtadr, NOP);
233 		else if (DoC_is_MillenniumPlus(doc))
234 			dummy = ReadDOC(doc->virtadr, Mplus_NOP);
235 		else
236 			dummy = ReadDOC(doc->virtadr, DOCStatus);
237 	}
238 
239 }
240 
241 #define CDSN_CTRL_FR_B_MASK	(CDSN_CTRL_FR_B0 | CDSN_CTRL_FR_B1)
242 
243 /* DOC_WaitReady: Wait for RDY line to be asserted by the flash chip */
_DoC_WaitReady(struct doc_priv * doc)244 static int _DoC_WaitReady(struct doc_priv *doc)
245 {
246 	void __iomem *docptr = doc->virtadr;
247 	unsigned long timeo = jiffies + (HZ * 10);
248 
249 	if (debug)
250 		printk("_DoC_WaitReady...\n");
251 	/* Out-of-line routine to wait for chip response */
252 	if (DoC_is_MillenniumPlus(doc)) {
253 		while ((ReadDOC(docptr, Mplus_FlashControl) & CDSN_CTRL_FR_B_MASK) != CDSN_CTRL_FR_B_MASK) {
254 			if (time_after(jiffies, timeo)) {
255 				printk("_DoC_WaitReady timed out.\n");
256 				return -EIO;
257 			}
258 			udelay(1);
259 			cond_resched();
260 		}
261 	} else {
262 		while (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B)) {
263 			if (time_after(jiffies, timeo)) {
264 				printk("_DoC_WaitReady timed out.\n");
265 				return -EIO;
266 			}
267 			udelay(1);
268 			cond_resched();
269 		}
270 	}
271 
272 	return 0;
273 }
274 
DoC_WaitReady(struct doc_priv * doc)275 static inline int DoC_WaitReady(struct doc_priv *doc)
276 {
277 	void __iomem *docptr = doc->virtadr;
278 	int ret = 0;
279 
280 	if (DoC_is_MillenniumPlus(doc)) {
281 		DoC_Delay(doc, 4);
282 
283 		if ((ReadDOC(docptr, Mplus_FlashControl) & CDSN_CTRL_FR_B_MASK) != CDSN_CTRL_FR_B_MASK)
284 			/* Call the out-of-line routine to wait */
285 			ret = _DoC_WaitReady(doc);
286 	} else {
287 		DoC_Delay(doc, 4);
288 
289 		if (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B))
290 			/* Call the out-of-line routine to wait */
291 			ret = _DoC_WaitReady(doc);
292 		DoC_Delay(doc, 2);
293 	}
294 
295 	if (debug)
296 		printk("DoC_WaitReady OK\n");
297 	return ret;
298 }
299 
doc2000_write_byte(struct mtd_info * mtd,u_char datum)300 static void doc2000_write_byte(struct mtd_info *mtd, u_char datum)
301 {
302 	struct nand_chip *this = mtd->priv;
303 	struct doc_priv *doc = this->priv;
304 	void __iomem *docptr = doc->virtadr;
305 
306 	if (debug)
307 		printk("write_byte %02x\n", datum);
308 	WriteDOC(datum, docptr, CDSNSlowIO);
309 	WriteDOC(datum, docptr, 2k_CDSN_IO);
310 }
311 
doc2000_read_byte(struct mtd_info * mtd)312 static u_char doc2000_read_byte(struct mtd_info *mtd)
313 {
314 	struct nand_chip *this = mtd->priv;
315 	struct doc_priv *doc = this->priv;
316 	void __iomem *docptr = doc->virtadr;
317 	u_char ret;
318 
319 	ReadDOC(docptr, CDSNSlowIO);
320 	DoC_Delay(doc, 2);
321 	ret = ReadDOC(docptr, 2k_CDSN_IO);
322 	if (debug)
323 		printk("read_byte returns %02x\n", ret);
324 	return ret;
325 }
326 
doc2000_writebuf(struct mtd_info * mtd,const u_char * buf,int len)327 static void doc2000_writebuf(struct mtd_info *mtd, const u_char *buf, int len)
328 {
329 	struct nand_chip *this = mtd->priv;
330 	struct doc_priv *doc = this->priv;
331 	void __iomem *docptr = doc->virtadr;
332 	int i;
333 	if (debug)
334 		printk("writebuf of %d bytes: ", len);
335 	for (i = 0; i < len; i++) {
336 		WriteDOC_(buf[i], docptr, DoC_2k_CDSN_IO + i);
337 		if (debug && i < 16)
338 			printk("%02x ", buf[i]);
339 	}
340 	if (debug)
341 		printk("\n");
342 }
343 
doc2000_readbuf(struct mtd_info * mtd,u_char * buf,int len)344 static void doc2000_readbuf(struct mtd_info *mtd, u_char *buf, int len)
345 {
346 	struct nand_chip *this = mtd->priv;
347 	struct doc_priv *doc = this->priv;
348 	void __iomem *docptr = doc->virtadr;
349 	int i;
350 
351 	if (debug)
352 		printk("readbuf of %d bytes: ", len);
353 
354 	for (i = 0; i < len; i++) {
355 		buf[i] = ReadDOC(docptr, 2k_CDSN_IO + i);
356 	}
357 }
358 
doc2000_readbuf_dword(struct mtd_info * mtd,u_char * buf,int len)359 static void doc2000_readbuf_dword(struct mtd_info *mtd, u_char *buf, int len)
360 {
361 	struct nand_chip *this = mtd->priv;
362 	struct doc_priv *doc = this->priv;
363 	void __iomem *docptr = doc->virtadr;
364 	int i;
365 
366 	if (debug)
367 		printk("readbuf_dword of %d bytes: ", len);
368 
369 	if (unlikely((((unsigned long)buf) | len) & 3)) {
370 		for (i = 0; i < len; i++) {
371 			*(uint8_t *) (&buf[i]) = ReadDOC(docptr, 2k_CDSN_IO + i);
372 		}
373 	} else {
374 		for (i = 0; i < len; i += 4) {
375 			*(uint32_t *) (&buf[i]) = readl(docptr + DoC_2k_CDSN_IO + i);
376 		}
377 	}
378 }
379 
doc200x_ident_chip(struct mtd_info * mtd,int nr)380 static uint16_t __init doc200x_ident_chip(struct mtd_info *mtd, int nr)
381 {
382 	struct nand_chip *this = mtd->priv;
383 	struct doc_priv *doc = this->priv;
384 	uint16_t ret;
385 
386 	doc200x_select_chip(mtd, nr);
387 	doc200x_hwcontrol(mtd, NAND_CMD_READID,
388 			  NAND_CTRL_CLE | NAND_CTRL_CHANGE);
389 	doc200x_hwcontrol(mtd, 0, NAND_CTRL_ALE | NAND_CTRL_CHANGE);
390 	doc200x_hwcontrol(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
391 
392 	/* We can't use dev_ready here, but at least we wait for the
393 	 * command to complete
394 	 */
395 	udelay(50);
396 
397 	ret = this->read_byte(mtd) << 8;
398 	ret |= this->read_byte(mtd);
399 
400 	if (doc->ChipID == DOC_ChipID_Doc2k && try_dword && !nr) {
401 		/* First chip probe. See if we get same results by 32-bit access */
402 		union {
403 			uint32_t dword;
404 			uint8_t byte[4];
405 		} ident;
406 		void __iomem *docptr = doc->virtadr;
407 
408 		doc200x_hwcontrol(mtd, NAND_CMD_READID,
409 				  NAND_CTRL_CLE | NAND_CTRL_CHANGE);
410 		doc200x_hwcontrol(mtd, 0, NAND_CTRL_ALE | NAND_CTRL_CHANGE);
411 		doc200x_hwcontrol(mtd, NAND_CMD_NONE,
412 				  NAND_NCE | NAND_CTRL_CHANGE);
413 
414 		udelay(50);
415 
416 		ident.dword = readl(docptr + DoC_2k_CDSN_IO);
417 		if (((ident.byte[0] << 8) | ident.byte[1]) == ret) {
418 			printk(KERN_INFO "DiskOnChip 2000 responds to DWORD access\n");
419 			this->read_buf = &doc2000_readbuf_dword;
420 		}
421 	}
422 
423 	return ret;
424 }
425 
doc2000_count_chips(struct mtd_info * mtd)426 static void __init doc2000_count_chips(struct mtd_info *mtd)
427 {
428 	struct nand_chip *this = mtd->priv;
429 	struct doc_priv *doc = this->priv;
430 	uint16_t mfrid;
431 	int i;
432 
433 	/* Max 4 chips per floor on DiskOnChip 2000 */
434 	doc->chips_per_floor = 4;
435 
436 	/* Find out what the first chip is */
437 	mfrid = doc200x_ident_chip(mtd, 0);
438 
439 	/* Find how many chips in each floor. */
440 	for (i = 1; i < 4; i++) {
441 		if (doc200x_ident_chip(mtd, i) != mfrid)
442 			break;
443 	}
444 	doc->chips_per_floor = i;
445 	printk(KERN_DEBUG "Detected %d chips per floor.\n", i);
446 }
447 
doc200x_wait(struct mtd_info * mtd,struct nand_chip * this)448 static int doc200x_wait(struct mtd_info *mtd, struct nand_chip *this)
449 {
450 	struct doc_priv *doc = this->priv;
451 
452 	int status;
453 
454 	DoC_WaitReady(doc);
455 	this->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
456 	DoC_WaitReady(doc);
457 	status = (int)this->read_byte(mtd);
458 
459 	return status;
460 }
461 
doc2001_write_byte(struct mtd_info * mtd,u_char datum)462 static void doc2001_write_byte(struct mtd_info *mtd, u_char datum)
463 {
464 	struct nand_chip *this = mtd->priv;
465 	struct doc_priv *doc = this->priv;
466 	void __iomem *docptr = doc->virtadr;
467 
468 	WriteDOC(datum, docptr, CDSNSlowIO);
469 	WriteDOC(datum, docptr, Mil_CDSN_IO);
470 	WriteDOC(datum, docptr, WritePipeTerm);
471 }
472 
doc2001_read_byte(struct mtd_info * mtd)473 static u_char doc2001_read_byte(struct mtd_info *mtd)
474 {
475 	struct nand_chip *this = mtd->priv;
476 	struct doc_priv *doc = this->priv;
477 	void __iomem *docptr = doc->virtadr;
478 
479 	//ReadDOC(docptr, CDSNSlowIO);
480 	/* 11.4.5 -- delay twice to allow extended length cycle */
481 	DoC_Delay(doc, 2);
482 	ReadDOC(docptr, ReadPipeInit);
483 	//return ReadDOC(docptr, Mil_CDSN_IO);
484 	return ReadDOC(docptr, LastDataRead);
485 }
486 
doc2001_writebuf(struct mtd_info * mtd,const u_char * buf,int len)487 static void doc2001_writebuf(struct mtd_info *mtd, const u_char *buf, int len)
488 {
489 	struct nand_chip *this = mtd->priv;
490 	struct doc_priv *doc = this->priv;
491 	void __iomem *docptr = doc->virtadr;
492 	int i;
493 
494 	for (i = 0; i < len; i++)
495 		WriteDOC_(buf[i], docptr, DoC_Mil_CDSN_IO + i);
496 	/* Terminate write pipeline */
497 	WriteDOC(0x00, docptr, WritePipeTerm);
498 }
499 
doc2001_readbuf(struct mtd_info * mtd,u_char * buf,int len)500 static void doc2001_readbuf(struct mtd_info *mtd, u_char *buf, int len)
501 {
502 	struct nand_chip *this = mtd->priv;
503 	struct doc_priv *doc = this->priv;
504 	void __iomem *docptr = doc->virtadr;
505 	int i;
506 
507 	/* Start read pipeline */
508 	ReadDOC(docptr, ReadPipeInit);
509 
510 	for (i = 0; i < len - 1; i++)
511 		buf[i] = ReadDOC(docptr, Mil_CDSN_IO + (i & 0xff));
512 
513 	/* Terminate read pipeline */
514 	buf[i] = ReadDOC(docptr, LastDataRead);
515 }
516 
doc2001plus_read_byte(struct mtd_info * mtd)517 static u_char doc2001plus_read_byte(struct mtd_info *mtd)
518 {
519 	struct nand_chip *this = mtd->priv;
520 	struct doc_priv *doc = this->priv;
521 	void __iomem *docptr = doc->virtadr;
522 	u_char ret;
523 
524 	ReadDOC(docptr, Mplus_ReadPipeInit);
525 	ReadDOC(docptr, Mplus_ReadPipeInit);
526 	ret = ReadDOC(docptr, Mplus_LastDataRead);
527 	if (debug)
528 		printk("read_byte returns %02x\n", ret);
529 	return ret;
530 }
531 
doc2001plus_writebuf(struct mtd_info * mtd,const u_char * buf,int len)532 static void doc2001plus_writebuf(struct mtd_info *mtd, const u_char *buf, int len)
533 {
534 	struct nand_chip *this = mtd->priv;
535 	struct doc_priv *doc = this->priv;
536 	void __iomem *docptr = doc->virtadr;
537 	int i;
538 
539 	if (debug)
540 		printk("writebuf of %d bytes: ", len);
541 	for (i = 0; i < len; i++) {
542 		WriteDOC_(buf[i], docptr, DoC_Mil_CDSN_IO + i);
543 		if (debug && i < 16)
544 			printk("%02x ", buf[i]);
545 	}
546 	if (debug)
547 		printk("\n");
548 }
549 
doc2001plus_readbuf(struct mtd_info * mtd,u_char * buf,int len)550 static void doc2001plus_readbuf(struct mtd_info *mtd, u_char *buf, int len)
551 {
552 	struct nand_chip *this = mtd->priv;
553 	struct doc_priv *doc = this->priv;
554 	void __iomem *docptr = doc->virtadr;
555 	int i;
556 
557 	if (debug)
558 		printk("readbuf of %d bytes: ", len);
559 
560 	/* Start read pipeline */
561 	ReadDOC(docptr, Mplus_ReadPipeInit);
562 	ReadDOC(docptr, Mplus_ReadPipeInit);
563 
564 	for (i = 0; i < len - 2; i++) {
565 		buf[i] = ReadDOC(docptr, Mil_CDSN_IO);
566 		if (debug && i < 16)
567 			printk("%02x ", buf[i]);
568 	}
569 
570 	/* Terminate read pipeline */
571 	buf[len - 2] = ReadDOC(docptr, Mplus_LastDataRead);
572 	if (debug && i < 16)
573 		printk("%02x ", buf[len - 2]);
574 	buf[len - 1] = ReadDOC(docptr, Mplus_LastDataRead);
575 	if (debug && i < 16)
576 		printk("%02x ", buf[len - 1]);
577 	if (debug)
578 		printk("\n");
579 }
580 
doc2001plus_select_chip(struct mtd_info * mtd,int chip)581 static void doc2001plus_select_chip(struct mtd_info *mtd, int chip)
582 {
583 	struct nand_chip *this = mtd->priv;
584 	struct doc_priv *doc = this->priv;
585 	void __iomem *docptr = doc->virtadr;
586 	int floor = 0;
587 
588 	if (debug)
589 		printk("select chip (%d)\n", chip);
590 
591 	if (chip == -1) {
592 		/* Disable flash internally */
593 		WriteDOC(0, docptr, Mplus_FlashSelect);
594 		return;
595 	}
596 
597 	floor = chip / doc->chips_per_floor;
598 	chip -= (floor * doc->chips_per_floor);
599 
600 	/* Assert ChipEnable and deassert WriteProtect */
601 	WriteDOC((DOC_FLASH_CE), docptr, Mplus_FlashSelect);
602 	this->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
603 
604 	doc->curchip = chip;
605 	doc->curfloor = floor;
606 }
607 
doc200x_select_chip(struct mtd_info * mtd,int chip)608 static void doc200x_select_chip(struct mtd_info *mtd, int chip)
609 {
610 	struct nand_chip *this = mtd->priv;
611 	struct doc_priv *doc = this->priv;
612 	void __iomem *docptr = doc->virtadr;
613 	int floor = 0;
614 
615 	if (debug)
616 		printk("select chip (%d)\n", chip);
617 
618 	if (chip == -1)
619 		return;
620 
621 	floor = chip / doc->chips_per_floor;
622 	chip -= (floor * doc->chips_per_floor);
623 
624 	/* 11.4.4 -- deassert CE before changing chip */
625 	doc200x_hwcontrol(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
626 
627 	WriteDOC(floor, docptr, FloorSelect);
628 	WriteDOC(chip, docptr, CDSNDeviceSelect);
629 
630 	doc200x_hwcontrol(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
631 
632 	doc->curchip = chip;
633 	doc->curfloor = floor;
634 }
635 
636 #define CDSN_CTRL_MSK (CDSN_CTRL_CE | CDSN_CTRL_CLE | CDSN_CTRL_ALE)
637 
doc200x_hwcontrol(struct mtd_info * mtd,int cmd,unsigned int ctrl)638 static void doc200x_hwcontrol(struct mtd_info *mtd, int cmd,
639 			      unsigned int ctrl)
640 {
641 	struct nand_chip *this = mtd->priv;
642 	struct doc_priv *doc = this->priv;
643 	void __iomem *docptr = doc->virtadr;
644 
645 	if (ctrl & NAND_CTRL_CHANGE) {
646 		doc->CDSNControl &= ~CDSN_CTRL_MSK;
647 		doc->CDSNControl |= ctrl & CDSN_CTRL_MSK;
648 		if (debug)
649 			printk("hwcontrol(%d): %02x\n", cmd, doc->CDSNControl);
650 		WriteDOC(doc->CDSNControl, docptr, CDSNControl);
651 		/* 11.4.3 -- 4 NOPs after CSDNControl write */
652 		DoC_Delay(doc, 4);
653 	}
654 	if (cmd != NAND_CMD_NONE) {
655 		if (DoC_is_2000(doc))
656 			doc2000_write_byte(mtd, cmd);
657 		else
658 			doc2001_write_byte(mtd, cmd);
659 	}
660 }
661 
doc2001plus_command(struct mtd_info * mtd,unsigned command,int column,int page_addr)662 static void doc2001plus_command(struct mtd_info *mtd, unsigned command, int column, int page_addr)
663 {
664 	struct nand_chip *this = mtd->priv;
665 	struct doc_priv *doc = this->priv;
666 	void __iomem *docptr = doc->virtadr;
667 
668 	/*
669 	 * Must terminate write pipeline before sending any commands
670 	 * to the device.
671 	 */
672 	if (command == NAND_CMD_PAGEPROG) {
673 		WriteDOC(0x00, docptr, Mplus_WritePipeTerm);
674 		WriteDOC(0x00, docptr, Mplus_WritePipeTerm);
675 	}
676 
677 	/*
678 	 * Write out the command to the device.
679 	 */
680 	if (command == NAND_CMD_SEQIN) {
681 		int readcmd;
682 
683 		if (column >= mtd->writesize) {
684 			/* OOB area */
685 			column -= mtd->writesize;
686 			readcmd = NAND_CMD_READOOB;
687 		} else if (column < 256) {
688 			/* First 256 bytes --> READ0 */
689 			readcmd = NAND_CMD_READ0;
690 		} else {
691 			column -= 256;
692 			readcmd = NAND_CMD_READ1;
693 		}
694 		WriteDOC(readcmd, docptr, Mplus_FlashCmd);
695 	}
696 	WriteDOC(command, docptr, Mplus_FlashCmd);
697 	WriteDOC(0, docptr, Mplus_WritePipeTerm);
698 	WriteDOC(0, docptr, Mplus_WritePipeTerm);
699 
700 	if (column != -1 || page_addr != -1) {
701 		/* Serially input address */
702 		if (column != -1) {
703 			/* Adjust columns for 16 bit buswidth */
704 			if (this->options & NAND_BUSWIDTH_16 &&
705 					!nand_opcode_8bits(command))
706 				column >>= 1;
707 			WriteDOC(column, docptr, Mplus_FlashAddress);
708 		}
709 		if (page_addr != -1) {
710 			WriteDOC((unsigned char)(page_addr & 0xff), docptr, Mplus_FlashAddress);
711 			WriteDOC((unsigned char)((page_addr >> 8) & 0xff), docptr, Mplus_FlashAddress);
712 			/* One more address cycle for higher density devices */
713 			if (this->chipsize & 0x0c000000) {
714 				WriteDOC((unsigned char)((page_addr >> 16) & 0x0f), docptr, Mplus_FlashAddress);
715 				printk("high density\n");
716 			}
717 		}
718 		WriteDOC(0, docptr, Mplus_WritePipeTerm);
719 		WriteDOC(0, docptr, Mplus_WritePipeTerm);
720 		/* deassert ALE */
721 		if (command == NAND_CMD_READ0 || command == NAND_CMD_READ1 ||
722 		    command == NAND_CMD_READOOB || command == NAND_CMD_READID)
723 			WriteDOC(0, docptr, Mplus_FlashControl);
724 	}
725 
726 	/*
727 	 * program and erase have their own busy handlers
728 	 * status and sequential in needs no delay
729 	 */
730 	switch (command) {
731 
732 	case NAND_CMD_PAGEPROG:
733 	case NAND_CMD_ERASE1:
734 	case NAND_CMD_ERASE2:
735 	case NAND_CMD_SEQIN:
736 	case NAND_CMD_STATUS:
737 		return;
738 
739 	case NAND_CMD_RESET:
740 		if (this->dev_ready)
741 			break;
742 		udelay(this->chip_delay);
743 		WriteDOC(NAND_CMD_STATUS, docptr, Mplus_FlashCmd);
744 		WriteDOC(0, docptr, Mplus_WritePipeTerm);
745 		WriteDOC(0, docptr, Mplus_WritePipeTerm);
746 		while (!(this->read_byte(mtd) & 0x40)) ;
747 		return;
748 
749 		/* This applies to read commands */
750 	default:
751 		/*
752 		 * If we don't have access to the busy pin, we apply the given
753 		 * command delay
754 		 */
755 		if (!this->dev_ready) {
756 			udelay(this->chip_delay);
757 			return;
758 		}
759 	}
760 
761 	/* Apply this short delay always to ensure that we do wait tWB in
762 	 * any case on any machine. */
763 	ndelay(100);
764 	/* wait until command is processed */
765 	while (!this->dev_ready(mtd)) ;
766 }
767 
doc200x_dev_ready(struct mtd_info * mtd)768 static int doc200x_dev_ready(struct mtd_info *mtd)
769 {
770 	struct nand_chip *this = mtd->priv;
771 	struct doc_priv *doc = this->priv;
772 	void __iomem *docptr = doc->virtadr;
773 
774 	if (DoC_is_MillenniumPlus(doc)) {
775 		/* 11.4.2 -- must NOP four times before checking FR/B# */
776 		DoC_Delay(doc, 4);
777 		if ((ReadDOC(docptr, Mplus_FlashControl) & CDSN_CTRL_FR_B_MASK) != CDSN_CTRL_FR_B_MASK) {
778 			if (debug)
779 				printk("not ready\n");
780 			return 0;
781 		}
782 		if (debug)
783 			printk("was ready\n");
784 		return 1;
785 	} else {
786 		/* 11.4.2 -- must NOP four times before checking FR/B# */
787 		DoC_Delay(doc, 4);
788 		if (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B)) {
789 			if (debug)
790 				printk("not ready\n");
791 			return 0;
792 		}
793 		/* 11.4.2 -- Must NOP twice if it's ready */
794 		DoC_Delay(doc, 2);
795 		if (debug)
796 			printk("was ready\n");
797 		return 1;
798 	}
799 }
800 
doc200x_block_bad(struct mtd_info * mtd,loff_t ofs,int getchip)801 static int doc200x_block_bad(struct mtd_info *mtd, loff_t ofs, int getchip)
802 {
803 	/* This is our last resort if we couldn't find or create a BBT.  Just
804 	   pretend all blocks are good. */
805 	return 0;
806 }
807 
doc200x_enable_hwecc(struct mtd_info * mtd,int mode)808 static void doc200x_enable_hwecc(struct mtd_info *mtd, int mode)
809 {
810 	struct nand_chip *this = mtd->priv;
811 	struct doc_priv *doc = this->priv;
812 	void __iomem *docptr = doc->virtadr;
813 
814 	/* Prime the ECC engine */
815 	switch (mode) {
816 	case NAND_ECC_READ:
817 		WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
818 		WriteDOC(DOC_ECC_EN, docptr, ECCConf);
819 		break;
820 	case NAND_ECC_WRITE:
821 		WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
822 		WriteDOC(DOC_ECC_EN | DOC_ECC_RW, docptr, ECCConf);
823 		break;
824 	}
825 }
826 
doc2001plus_enable_hwecc(struct mtd_info * mtd,int mode)827 static void doc2001plus_enable_hwecc(struct mtd_info *mtd, int mode)
828 {
829 	struct nand_chip *this = mtd->priv;
830 	struct doc_priv *doc = this->priv;
831 	void __iomem *docptr = doc->virtadr;
832 
833 	/* Prime the ECC engine */
834 	switch (mode) {
835 	case NAND_ECC_READ:
836 		WriteDOC(DOC_ECC_RESET, docptr, Mplus_ECCConf);
837 		WriteDOC(DOC_ECC_EN, docptr, Mplus_ECCConf);
838 		break;
839 	case NAND_ECC_WRITE:
840 		WriteDOC(DOC_ECC_RESET, docptr, Mplus_ECCConf);
841 		WriteDOC(DOC_ECC_EN | DOC_ECC_RW, docptr, Mplus_ECCConf);
842 		break;
843 	}
844 }
845 
846 /* This code is only called on write */
doc200x_calculate_ecc(struct mtd_info * mtd,const u_char * dat,unsigned char * ecc_code)847 static int doc200x_calculate_ecc(struct mtd_info *mtd, const u_char *dat, unsigned char *ecc_code)
848 {
849 	struct nand_chip *this = mtd->priv;
850 	struct doc_priv *doc = this->priv;
851 	void __iomem *docptr = doc->virtadr;
852 	int i;
853 	int emptymatch = 1;
854 
855 	/* flush the pipeline */
856 	if (DoC_is_2000(doc)) {
857 		WriteDOC(doc->CDSNControl & ~CDSN_CTRL_FLASH_IO, docptr, CDSNControl);
858 		WriteDOC(0, docptr, 2k_CDSN_IO);
859 		WriteDOC(0, docptr, 2k_CDSN_IO);
860 		WriteDOC(0, docptr, 2k_CDSN_IO);
861 		WriteDOC(doc->CDSNControl, docptr, CDSNControl);
862 	} else if (DoC_is_MillenniumPlus(doc)) {
863 		WriteDOC(0, docptr, Mplus_NOP);
864 		WriteDOC(0, docptr, Mplus_NOP);
865 		WriteDOC(0, docptr, Mplus_NOP);
866 	} else {
867 		WriteDOC(0, docptr, NOP);
868 		WriteDOC(0, docptr, NOP);
869 		WriteDOC(0, docptr, NOP);
870 	}
871 
872 	for (i = 0; i < 6; i++) {
873 		if (DoC_is_MillenniumPlus(doc))
874 			ecc_code[i] = ReadDOC_(docptr, DoC_Mplus_ECCSyndrome0 + i);
875 		else
876 			ecc_code[i] = ReadDOC_(docptr, DoC_ECCSyndrome0 + i);
877 		if (ecc_code[i] != empty_write_ecc[i])
878 			emptymatch = 0;
879 	}
880 	if (DoC_is_MillenniumPlus(doc))
881 		WriteDOC(DOC_ECC_DIS, docptr, Mplus_ECCConf);
882 	else
883 		WriteDOC(DOC_ECC_DIS, docptr, ECCConf);
884 #if 0
885 	/* If emptymatch=1, we might have an all-0xff data buffer.  Check. */
886 	if (emptymatch) {
887 		/* Note: this somewhat expensive test should not be triggered
888 		   often.  It could be optimized away by examining the data in
889 		   the writebuf routine, and remembering the result. */
890 		for (i = 0; i < 512; i++) {
891 			if (dat[i] == 0xff)
892 				continue;
893 			emptymatch = 0;
894 			break;
895 		}
896 	}
897 	/* If emptymatch still =1, we do have an all-0xff data buffer.
898 	   Return all-0xff ecc value instead of the computed one, so
899 	   it'll look just like a freshly-erased page. */
900 	if (emptymatch)
901 		memset(ecc_code, 0xff, 6);
902 #endif
903 	return 0;
904 }
905 
doc200x_correct_data(struct mtd_info * mtd,u_char * dat,u_char * read_ecc,u_char * isnull)906 static int doc200x_correct_data(struct mtd_info *mtd, u_char *dat,
907 				u_char *read_ecc, u_char *isnull)
908 {
909 	int i, ret = 0;
910 	struct nand_chip *this = mtd->priv;
911 	struct doc_priv *doc = this->priv;
912 	void __iomem *docptr = doc->virtadr;
913 	uint8_t calc_ecc[6];
914 	volatile u_char dummy;
915 	int emptymatch = 1;
916 
917 	/* flush the pipeline */
918 	if (DoC_is_2000(doc)) {
919 		dummy = ReadDOC(docptr, 2k_ECCStatus);
920 		dummy = ReadDOC(docptr, 2k_ECCStatus);
921 		dummy = ReadDOC(docptr, 2k_ECCStatus);
922 	} else if (DoC_is_MillenniumPlus(doc)) {
923 		dummy = ReadDOC(docptr, Mplus_ECCConf);
924 		dummy = ReadDOC(docptr, Mplus_ECCConf);
925 		dummy = ReadDOC(docptr, Mplus_ECCConf);
926 	} else {
927 		dummy = ReadDOC(docptr, ECCConf);
928 		dummy = ReadDOC(docptr, ECCConf);
929 		dummy = ReadDOC(docptr, ECCConf);
930 	}
931 
932 	/* Error occurred ? */
933 	if (dummy & 0x80) {
934 		for (i = 0; i < 6; i++) {
935 			if (DoC_is_MillenniumPlus(doc))
936 				calc_ecc[i] = ReadDOC_(docptr, DoC_Mplus_ECCSyndrome0 + i);
937 			else
938 				calc_ecc[i] = ReadDOC_(docptr, DoC_ECCSyndrome0 + i);
939 			if (calc_ecc[i] != empty_read_syndrome[i])
940 				emptymatch = 0;
941 		}
942 		/* If emptymatch=1, the read syndrome is consistent with an
943 		   all-0xff data and stored ecc block.  Check the stored ecc. */
944 		if (emptymatch) {
945 			for (i = 0; i < 6; i++) {
946 				if (read_ecc[i] == 0xff)
947 					continue;
948 				emptymatch = 0;
949 				break;
950 			}
951 		}
952 		/* If emptymatch still =1, check the data block. */
953 		if (emptymatch) {
954 			/* Note: this somewhat expensive test should not be triggered
955 			   often.  It could be optimized away by examining the data in
956 			   the readbuf routine, and remembering the result. */
957 			for (i = 0; i < 512; i++) {
958 				if (dat[i] == 0xff)
959 					continue;
960 				emptymatch = 0;
961 				break;
962 			}
963 		}
964 		/* If emptymatch still =1, this is almost certainly a freshly-
965 		   erased block, in which case the ECC will not come out right.
966 		   We'll suppress the error and tell the caller everything's
967 		   OK.  Because it is. */
968 		if (!emptymatch)
969 			ret = doc_ecc_decode(rs_decoder, dat, calc_ecc);
970 		if (ret > 0)
971 			printk(KERN_ERR "doc200x_correct_data corrected %d errors\n", ret);
972 	}
973 	if (DoC_is_MillenniumPlus(doc))
974 		WriteDOC(DOC_ECC_DIS, docptr, Mplus_ECCConf);
975 	else
976 		WriteDOC(DOC_ECC_DIS, docptr, ECCConf);
977 	if (no_ecc_failures && mtd_is_eccerr(ret)) {
978 		printk(KERN_ERR "suppressing ECC failure\n");
979 		ret = 0;
980 	}
981 	return ret;
982 }
983 
984 //u_char mydatabuf[528];
985 
986 /* The strange out-of-order .oobfree list below is a (possibly unneeded)
987  * attempt to retain compatibility.  It used to read:
988  * 	.oobfree = { {8, 8} }
989  * Since that leaves two bytes unusable, it was changed.  But the following
990  * scheme might affect existing jffs2 installs by moving the cleanmarker:
991  * 	.oobfree = { {6, 10} }
992  * jffs2 seems to handle the above gracefully, but the current scheme seems
993  * safer.  The only problem with it is that any code that parses oobfree must
994  * be able to handle out-of-order segments.
995  */
996 static struct nand_ecclayout doc200x_oobinfo = {
997 	.eccbytes = 6,
998 	.eccpos = {0, 1, 2, 3, 4, 5},
999 	.oobfree = {{8, 8}, {6, 2}}
1000 };
1001 
1002 /* Find the (I)NFTL Media Header, and optionally also the mirror media header.
1003    On successful return, buf will contain a copy of the media header for
1004    further processing.  id is the string to scan for, and will presumably be
1005    either "ANAND" or "BNAND".  If findmirror=1, also look for the mirror media
1006    header.  The page #s of the found media headers are placed in mh0_page and
1007    mh1_page in the DOC private structure. */
find_media_headers(struct mtd_info * mtd,u_char * buf,const char * id,int findmirror)1008 static int __init find_media_headers(struct mtd_info *mtd, u_char *buf, const char *id, int findmirror)
1009 {
1010 	struct nand_chip *this = mtd->priv;
1011 	struct doc_priv *doc = this->priv;
1012 	unsigned offs;
1013 	int ret;
1014 	size_t retlen;
1015 
1016 	for (offs = 0; offs < mtd->size; offs += mtd->erasesize) {
1017 		ret = mtd_read(mtd, offs, mtd->writesize, &retlen, buf);
1018 		if (retlen != mtd->writesize)
1019 			continue;
1020 		if (ret) {
1021 			printk(KERN_WARNING "ECC error scanning DOC at 0x%x\n", offs);
1022 		}
1023 		if (memcmp(buf, id, 6))
1024 			continue;
1025 		printk(KERN_INFO "Found DiskOnChip %s Media Header at 0x%x\n", id, offs);
1026 		if (doc->mh0_page == -1) {
1027 			doc->mh0_page = offs >> this->page_shift;
1028 			if (!findmirror)
1029 				return 1;
1030 			continue;
1031 		}
1032 		doc->mh1_page = offs >> this->page_shift;
1033 		return 2;
1034 	}
1035 	if (doc->mh0_page == -1) {
1036 		printk(KERN_WARNING "DiskOnChip %s Media Header not found.\n", id);
1037 		return 0;
1038 	}
1039 	/* Only one mediaheader was found.  We want buf to contain a
1040 	   mediaheader on return, so we'll have to re-read the one we found. */
1041 	offs = doc->mh0_page << this->page_shift;
1042 	ret = mtd_read(mtd, offs, mtd->writesize, &retlen, buf);
1043 	if (retlen != mtd->writesize) {
1044 		/* Insanity.  Give up. */
1045 		printk(KERN_ERR "Read DiskOnChip Media Header once, but can't reread it???\n");
1046 		return 0;
1047 	}
1048 	return 1;
1049 }
1050 
nftl_partscan(struct mtd_info * mtd,struct mtd_partition * parts)1051 static inline int __init nftl_partscan(struct mtd_info *mtd, struct mtd_partition *parts)
1052 {
1053 	struct nand_chip *this = mtd->priv;
1054 	struct doc_priv *doc = this->priv;
1055 	int ret = 0;
1056 	u_char *buf;
1057 	struct NFTLMediaHeader *mh;
1058 	const unsigned psize = 1 << this->page_shift;
1059 	int numparts = 0;
1060 	unsigned blocks, maxblocks;
1061 	int offs, numheaders;
1062 
1063 	buf = kmalloc(mtd->writesize, GFP_KERNEL);
1064 	if (!buf) {
1065 		return 0;
1066 	}
1067 	if (!(numheaders = find_media_headers(mtd, buf, "ANAND", 1)))
1068 		goto out;
1069 	mh = (struct NFTLMediaHeader *)buf;
1070 
1071 	le16_to_cpus(&mh->NumEraseUnits);
1072 	le16_to_cpus(&mh->FirstPhysicalEUN);
1073 	le32_to_cpus(&mh->FormattedSize);
1074 
1075 	printk(KERN_INFO "    DataOrgID        = %s\n"
1076 			 "    NumEraseUnits    = %d\n"
1077 			 "    FirstPhysicalEUN = %d\n"
1078 			 "    FormattedSize    = %d\n"
1079 			 "    UnitSizeFactor   = %d\n",
1080 		mh->DataOrgID, mh->NumEraseUnits,
1081 		mh->FirstPhysicalEUN, mh->FormattedSize,
1082 		mh->UnitSizeFactor);
1083 
1084 	blocks = mtd->size >> this->phys_erase_shift;
1085 	maxblocks = min(32768U, mtd->erasesize - psize);
1086 
1087 	if (mh->UnitSizeFactor == 0x00) {
1088 		/* Auto-determine UnitSizeFactor.  The constraints are:
1089 		   - There can be at most 32768 virtual blocks.
1090 		   - There can be at most (virtual block size - page size)
1091 		   virtual blocks (because MediaHeader+BBT must fit in 1).
1092 		 */
1093 		mh->UnitSizeFactor = 0xff;
1094 		while (blocks > maxblocks) {
1095 			blocks >>= 1;
1096 			maxblocks = min(32768U, (maxblocks << 1) + psize);
1097 			mh->UnitSizeFactor--;
1098 		}
1099 		printk(KERN_WARNING "UnitSizeFactor=0x00 detected.  Correct value is assumed to be 0x%02x.\n", mh->UnitSizeFactor);
1100 	}
1101 
1102 	/* NOTE: The lines below modify internal variables of the NAND and MTD
1103 	   layers; variables with have already been configured by nand_scan.
1104 	   Unfortunately, we didn't know before this point what these values
1105 	   should be.  Thus, this code is somewhat dependent on the exact
1106 	   implementation of the NAND layer.  */
1107 	if (mh->UnitSizeFactor != 0xff) {
1108 		this->bbt_erase_shift += (0xff - mh->UnitSizeFactor);
1109 		mtd->erasesize <<= (0xff - mh->UnitSizeFactor);
1110 		printk(KERN_INFO "Setting virtual erase size to %d\n", mtd->erasesize);
1111 		blocks = mtd->size >> this->bbt_erase_shift;
1112 		maxblocks = min(32768U, mtd->erasesize - psize);
1113 	}
1114 
1115 	if (blocks > maxblocks) {
1116 		printk(KERN_ERR "UnitSizeFactor of 0x%02x is inconsistent with device size.  Aborting.\n", mh->UnitSizeFactor);
1117 		goto out;
1118 	}
1119 
1120 	/* Skip past the media headers. */
1121 	offs = max(doc->mh0_page, doc->mh1_page);
1122 	offs <<= this->page_shift;
1123 	offs += mtd->erasesize;
1124 
1125 	if (show_firmware_partition == 1) {
1126 		parts[0].name = " DiskOnChip Firmware / Media Header partition";
1127 		parts[0].offset = 0;
1128 		parts[0].size = offs;
1129 		numparts = 1;
1130 	}
1131 
1132 	parts[numparts].name = " DiskOnChip BDTL partition";
1133 	parts[numparts].offset = offs;
1134 	parts[numparts].size = (mh->NumEraseUnits - numheaders) << this->bbt_erase_shift;
1135 
1136 	offs += parts[numparts].size;
1137 	numparts++;
1138 
1139 	if (offs < mtd->size) {
1140 		parts[numparts].name = " DiskOnChip Remainder partition";
1141 		parts[numparts].offset = offs;
1142 		parts[numparts].size = mtd->size - offs;
1143 		numparts++;
1144 	}
1145 
1146 	ret = numparts;
1147  out:
1148 	kfree(buf);
1149 	return ret;
1150 }
1151 
1152 /* This is a stripped-down copy of the code in inftlmount.c */
inftl_partscan(struct mtd_info * mtd,struct mtd_partition * parts)1153 static inline int __init inftl_partscan(struct mtd_info *mtd, struct mtd_partition *parts)
1154 {
1155 	struct nand_chip *this = mtd->priv;
1156 	struct doc_priv *doc = this->priv;
1157 	int ret = 0;
1158 	u_char *buf;
1159 	struct INFTLMediaHeader *mh;
1160 	struct INFTLPartition *ip;
1161 	int numparts = 0;
1162 	int blocks;
1163 	int vshift, lastvunit = 0;
1164 	int i;
1165 	int end = mtd->size;
1166 
1167 	if (inftl_bbt_write)
1168 		end -= (INFTL_BBT_RESERVED_BLOCKS << this->phys_erase_shift);
1169 
1170 	buf = kmalloc(mtd->writesize, GFP_KERNEL);
1171 	if (!buf) {
1172 		return 0;
1173 	}
1174 
1175 	if (!find_media_headers(mtd, buf, "BNAND", 0))
1176 		goto out;
1177 	doc->mh1_page = doc->mh0_page + (4096 >> this->page_shift);
1178 	mh = (struct INFTLMediaHeader *)buf;
1179 
1180 	le32_to_cpus(&mh->NoOfBootImageBlocks);
1181 	le32_to_cpus(&mh->NoOfBinaryPartitions);
1182 	le32_to_cpus(&mh->NoOfBDTLPartitions);
1183 	le32_to_cpus(&mh->BlockMultiplierBits);
1184 	le32_to_cpus(&mh->FormatFlags);
1185 	le32_to_cpus(&mh->PercentUsed);
1186 
1187 	printk(KERN_INFO "    bootRecordID          = %s\n"
1188 			 "    NoOfBootImageBlocks   = %d\n"
1189 			 "    NoOfBinaryPartitions  = %d\n"
1190 			 "    NoOfBDTLPartitions    = %d\n"
1191 			 "    BlockMultiplerBits    = %d\n"
1192 			 "    FormatFlgs            = %d\n"
1193 			 "    OsakVersion           = %d.%d.%d.%d\n"
1194 			 "    PercentUsed           = %d\n",
1195 		mh->bootRecordID, mh->NoOfBootImageBlocks,
1196 		mh->NoOfBinaryPartitions,
1197 		mh->NoOfBDTLPartitions,
1198 		mh->BlockMultiplierBits, mh->FormatFlags,
1199 		((unsigned char *) &mh->OsakVersion)[0] & 0xf,
1200 		((unsigned char *) &mh->OsakVersion)[1] & 0xf,
1201 		((unsigned char *) &mh->OsakVersion)[2] & 0xf,
1202 		((unsigned char *) &mh->OsakVersion)[3] & 0xf,
1203 		mh->PercentUsed);
1204 
1205 	vshift = this->phys_erase_shift + mh->BlockMultiplierBits;
1206 
1207 	blocks = mtd->size >> vshift;
1208 	if (blocks > 32768) {
1209 		printk(KERN_ERR "BlockMultiplierBits=%d is inconsistent with device size.  Aborting.\n", mh->BlockMultiplierBits);
1210 		goto out;
1211 	}
1212 
1213 	blocks = doc->chips_per_floor << (this->chip_shift - this->phys_erase_shift);
1214 	if (inftl_bbt_write && (blocks > mtd->erasesize)) {
1215 		printk(KERN_ERR "Writeable BBTs spanning more than one erase block are not yet supported.  FIX ME!\n");
1216 		goto out;
1217 	}
1218 
1219 	/* Scan the partitions */
1220 	for (i = 0; (i < 4); i++) {
1221 		ip = &(mh->Partitions[i]);
1222 		le32_to_cpus(&ip->virtualUnits);
1223 		le32_to_cpus(&ip->firstUnit);
1224 		le32_to_cpus(&ip->lastUnit);
1225 		le32_to_cpus(&ip->flags);
1226 		le32_to_cpus(&ip->spareUnits);
1227 		le32_to_cpus(&ip->Reserved0);
1228 
1229 		printk(KERN_INFO	"    PARTITION[%d] ->\n"
1230 			"        virtualUnits    = %d\n"
1231 			"        firstUnit       = %d\n"
1232 			"        lastUnit        = %d\n"
1233 			"        flags           = 0x%x\n"
1234 			"        spareUnits      = %d\n",
1235 			i, ip->virtualUnits, ip->firstUnit,
1236 			ip->lastUnit, ip->flags,
1237 			ip->spareUnits);
1238 
1239 		if ((show_firmware_partition == 1) &&
1240 		    (i == 0) && (ip->firstUnit > 0)) {
1241 			parts[0].name = " DiskOnChip IPL / Media Header partition";
1242 			parts[0].offset = 0;
1243 			parts[0].size = mtd->erasesize * ip->firstUnit;
1244 			numparts = 1;
1245 		}
1246 
1247 		if (ip->flags & INFTL_BINARY)
1248 			parts[numparts].name = " DiskOnChip BDK partition";
1249 		else
1250 			parts[numparts].name = " DiskOnChip BDTL partition";
1251 		parts[numparts].offset = ip->firstUnit << vshift;
1252 		parts[numparts].size = (1 + ip->lastUnit - ip->firstUnit) << vshift;
1253 		numparts++;
1254 		if (ip->lastUnit > lastvunit)
1255 			lastvunit = ip->lastUnit;
1256 		if (ip->flags & INFTL_LAST)
1257 			break;
1258 	}
1259 	lastvunit++;
1260 	if ((lastvunit << vshift) < end) {
1261 		parts[numparts].name = " DiskOnChip Remainder partition";
1262 		parts[numparts].offset = lastvunit << vshift;
1263 		parts[numparts].size = end - parts[numparts].offset;
1264 		numparts++;
1265 	}
1266 	ret = numparts;
1267  out:
1268 	kfree(buf);
1269 	return ret;
1270 }
1271 
nftl_scan_bbt(struct mtd_info * mtd)1272 static int __init nftl_scan_bbt(struct mtd_info *mtd)
1273 {
1274 	int ret, numparts;
1275 	struct nand_chip *this = mtd->priv;
1276 	struct doc_priv *doc = this->priv;
1277 	struct mtd_partition parts[2];
1278 
1279 	memset((char *)parts, 0, sizeof(parts));
1280 	/* On NFTL, we have to find the media headers before we can read the
1281 	   BBTs, since they're stored in the media header eraseblocks. */
1282 	numparts = nftl_partscan(mtd, parts);
1283 	if (!numparts)
1284 		return -EIO;
1285 	this->bbt_td->options = NAND_BBT_ABSPAGE | NAND_BBT_8BIT |
1286 				NAND_BBT_SAVECONTENT | NAND_BBT_WRITE |
1287 				NAND_BBT_VERSION;
1288 	this->bbt_td->veroffs = 7;
1289 	this->bbt_td->pages[0] = doc->mh0_page + 1;
1290 	if (doc->mh1_page != -1) {
1291 		this->bbt_md->options = NAND_BBT_ABSPAGE | NAND_BBT_8BIT |
1292 					NAND_BBT_SAVECONTENT | NAND_BBT_WRITE |
1293 					NAND_BBT_VERSION;
1294 		this->bbt_md->veroffs = 7;
1295 		this->bbt_md->pages[0] = doc->mh1_page + 1;
1296 	} else {
1297 		this->bbt_md = NULL;
1298 	}
1299 
1300 	ret = this->scan_bbt(mtd);
1301 	if (ret)
1302 		return ret;
1303 
1304 	return mtd_device_register(mtd, parts, no_autopart ? 0 : numparts);
1305 }
1306 
inftl_scan_bbt(struct mtd_info * mtd)1307 static int __init inftl_scan_bbt(struct mtd_info *mtd)
1308 {
1309 	int ret, numparts;
1310 	struct nand_chip *this = mtd->priv;
1311 	struct doc_priv *doc = this->priv;
1312 	struct mtd_partition parts[5];
1313 
1314 	if (this->numchips > doc->chips_per_floor) {
1315 		printk(KERN_ERR "Multi-floor INFTL devices not yet supported.\n");
1316 		return -EIO;
1317 	}
1318 
1319 	if (DoC_is_MillenniumPlus(doc)) {
1320 		this->bbt_td->options = NAND_BBT_2BIT | NAND_BBT_ABSPAGE;
1321 		if (inftl_bbt_write)
1322 			this->bbt_td->options |= NAND_BBT_WRITE;
1323 		this->bbt_td->pages[0] = 2;
1324 		this->bbt_md = NULL;
1325 	} else {
1326 		this->bbt_td->options = NAND_BBT_LASTBLOCK | NAND_BBT_8BIT | NAND_BBT_VERSION;
1327 		if (inftl_bbt_write)
1328 			this->bbt_td->options |= NAND_BBT_WRITE;
1329 		this->bbt_td->offs = 8;
1330 		this->bbt_td->len = 8;
1331 		this->bbt_td->veroffs = 7;
1332 		this->bbt_td->maxblocks = INFTL_BBT_RESERVED_BLOCKS;
1333 		this->bbt_td->reserved_block_code = 0x01;
1334 		this->bbt_td->pattern = "MSYS_BBT";
1335 
1336 		this->bbt_md->options = NAND_BBT_LASTBLOCK | NAND_BBT_8BIT | NAND_BBT_VERSION;
1337 		if (inftl_bbt_write)
1338 			this->bbt_md->options |= NAND_BBT_WRITE;
1339 		this->bbt_md->offs = 8;
1340 		this->bbt_md->len = 8;
1341 		this->bbt_md->veroffs = 7;
1342 		this->bbt_md->maxblocks = INFTL_BBT_RESERVED_BLOCKS;
1343 		this->bbt_md->reserved_block_code = 0x01;
1344 		this->bbt_md->pattern = "TBB_SYSM";
1345 	}
1346 
1347 	ret = this->scan_bbt(mtd);
1348 	if (ret)
1349 		return ret;
1350 
1351 	memset((char *)parts, 0, sizeof(parts));
1352 	numparts = inftl_partscan(mtd, parts);
1353 	/* At least for now, require the INFTL Media Header.  We could probably
1354 	   do without it for non-INFTL use, since all it gives us is
1355 	   autopartitioning, but I want to give it more thought. */
1356 	if (!numparts)
1357 		return -EIO;
1358 	return mtd_device_register(mtd, parts, no_autopart ? 0 : numparts);
1359 }
1360 
doc2000_init(struct mtd_info * mtd)1361 static inline int __init doc2000_init(struct mtd_info *mtd)
1362 {
1363 	struct nand_chip *this = mtd->priv;
1364 	struct doc_priv *doc = this->priv;
1365 
1366 	this->read_byte = doc2000_read_byte;
1367 	this->write_buf = doc2000_writebuf;
1368 	this->read_buf = doc2000_readbuf;
1369 	doc->late_init = nftl_scan_bbt;
1370 
1371 	doc->CDSNControl = CDSN_CTRL_FLASH_IO | CDSN_CTRL_ECC_IO;
1372 	doc2000_count_chips(mtd);
1373 	mtd->name = "DiskOnChip 2000 (NFTL Model)";
1374 	return (4 * doc->chips_per_floor);
1375 }
1376 
doc2001_init(struct mtd_info * mtd)1377 static inline int __init doc2001_init(struct mtd_info *mtd)
1378 {
1379 	struct nand_chip *this = mtd->priv;
1380 	struct doc_priv *doc = this->priv;
1381 
1382 	this->read_byte = doc2001_read_byte;
1383 	this->write_buf = doc2001_writebuf;
1384 	this->read_buf = doc2001_readbuf;
1385 
1386 	ReadDOC(doc->virtadr, ChipID);
1387 	ReadDOC(doc->virtadr, ChipID);
1388 	ReadDOC(doc->virtadr, ChipID);
1389 	if (ReadDOC(doc->virtadr, ChipID) != DOC_ChipID_DocMil) {
1390 		/* It's not a Millennium; it's one of the newer
1391 		   DiskOnChip 2000 units with a similar ASIC.
1392 		   Treat it like a Millennium, except that it
1393 		   can have multiple chips. */
1394 		doc2000_count_chips(mtd);
1395 		mtd->name = "DiskOnChip 2000 (INFTL Model)";
1396 		doc->late_init = inftl_scan_bbt;
1397 		return (4 * doc->chips_per_floor);
1398 	} else {
1399 		/* Bog-standard Millennium */
1400 		doc->chips_per_floor = 1;
1401 		mtd->name = "DiskOnChip Millennium";
1402 		doc->late_init = nftl_scan_bbt;
1403 		return 1;
1404 	}
1405 }
1406 
doc2001plus_init(struct mtd_info * mtd)1407 static inline int __init doc2001plus_init(struct mtd_info *mtd)
1408 {
1409 	struct nand_chip *this = mtd->priv;
1410 	struct doc_priv *doc = this->priv;
1411 
1412 	this->read_byte = doc2001plus_read_byte;
1413 	this->write_buf = doc2001plus_writebuf;
1414 	this->read_buf = doc2001plus_readbuf;
1415 	doc->late_init = inftl_scan_bbt;
1416 	this->cmd_ctrl = NULL;
1417 	this->select_chip = doc2001plus_select_chip;
1418 	this->cmdfunc = doc2001plus_command;
1419 	this->ecc.hwctl = doc2001plus_enable_hwecc;
1420 
1421 	doc->chips_per_floor = 1;
1422 	mtd->name = "DiskOnChip Millennium Plus";
1423 
1424 	return 1;
1425 }
1426 
doc_probe(unsigned long physadr)1427 static int __init doc_probe(unsigned long physadr)
1428 {
1429 	unsigned char ChipID;
1430 	struct mtd_info *mtd;
1431 	struct nand_chip *nand;
1432 	struct doc_priv *doc;
1433 	void __iomem *virtadr;
1434 	unsigned char save_control;
1435 	unsigned char tmp, tmpb, tmpc;
1436 	int reg, len, numchips;
1437 	int ret = 0;
1438 
1439 	if (!request_mem_region(physadr, DOC_IOREMAP_LEN, "DiskOnChip"))
1440 		return -EBUSY;
1441 	virtadr = ioremap(physadr, DOC_IOREMAP_LEN);
1442 	if (!virtadr) {
1443 		printk(KERN_ERR "Diskonchip ioremap failed: 0x%x bytes at 0x%lx\n", DOC_IOREMAP_LEN, physadr);
1444 		ret = -EIO;
1445 		goto error_ioremap;
1446 	}
1447 
1448 	/* It's not possible to cleanly detect the DiskOnChip - the
1449 	 * bootup procedure will put the device into reset mode, and
1450 	 * it's not possible to talk to it without actually writing
1451 	 * to the DOCControl register. So we store the current contents
1452 	 * of the DOCControl register's location, in case we later decide
1453 	 * that it's not a DiskOnChip, and want to put it back how we
1454 	 * found it.
1455 	 */
1456 	save_control = ReadDOC(virtadr, DOCControl);
1457 
1458 	/* Reset the DiskOnChip ASIC */
1459 	WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_RESET, virtadr, DOCControl);
1460 	WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_RESET, virtadr, DOCControl);
1461 
1462 	/* Enable the DiskOnChip ASIC */
1463 	WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_NORMAL, virtadr, DOCControl);
1464 	WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_NORMAL, virtadr, DOCControl);
1465 
1466 	ChipID = ReadDOC(virtadr, ChipID);
1467 
1468 	switch (ChipID) {
1469 	case DOC_ChipID_Doc2k:
1470 		reg = DoC_2k_ECCStatus;
1471 		break;
1472 	case DOC_ChipID_DocMil:
1473 		reg = DoC_ECCConf;
1474 		break;
1475 	case DOC_ChipID_DocMilPlus16:
1476 	case DOC_ChipID_DocMilPlus32:
1477 	case 0:
1478 		/* Possible Millennium Plus, need to do more checks */
1479 		/* Possibly release from power down mode */
1480 		for (tmp = 0; (tmp < 4); tmp++)
1481 			ReadDOC(virtadr, Mplus_Power);
1482 
1483 		/* Reset the Millennium Plus ASIC */
1484 		tmp = DOC_MODE_RESET | DOC_MODE_MDWREN | DOC_MODE_RST_LAT | DOC_MODE_BDECT;
1485 		WriteDOC(tmp, virtadr, Mplus_DOCControl);
1486 		WriteDOC(~tmp, virtadr, Mplus_CtrlConfirm);
1487 
1488 		mdelay(1);
1489 		/* Enable the Millennium Plus ASIC */
1490 		tmp = DOC_MODE_NORMAL | DOC_MODE_MDWREN | DOC_MODE_RST_LAT | DOC_MODE_BDECT;
1491 		WriteDOC(tmp, virtadr, Mplus_DOCControl);
1492 		WriteDOC(~tmp, virtadr, Mplus_CtrlConfirm);
1493 		mdelay(1);
1494 
1495 		ChipID = ReadDOC(virtadr, ChipID);
1496 
1497 		switch (ChipID) {
1498 		case DOC_ChipID_DocMilPlus16:
1499 			reg = DoC_Mplus_Toggle;
1500 			break;
1501 		case DOC_ChipID_DocMilPlus32:
1502 			printk(KERN_ERR "DiskOnChip Millennium Plus 32MB is not supported, ignoring.\n");
1503 		default:
1504 			ret = -ENODEV;
1505 			goto notfound;
1506 		}
1507 		break;
1508 
1509 	default:
1510 		ret = -ENODEV;
1511 		goto notfound;
1512 	}
1513 	/* Check the TOGGLE bit in the ECC register */
1514 	tmp = ReadDOC_(virtadr, reg) & DOC_TOGGLE_BIT;
1515 	tmpb = ReadDOC_(virtadr, reg) & DOC_TOGGLE_BIT;
1516 	tmpc = ReadDOC_(virtadr, reg) & DOC_TOGGLE_BIT;
1517 	if ((tmp == tmpb) || (tmp != tmpc)) {
1518 		printk(KERN_WARNING "Possible DiskOnChip at 0x%lx failed TOGGLE test, dropping.\n", physadr);
1519 		ret = -ENODEV;
1520 		goto notfound;
1521 	}
1522 
1523 	for (mtd = doclist; mtd; mtd = doc->nextdoc) {
1524 		unsigned char oldval;
1525 		unsigned char newval;
1526 		nand = mtd->priv;
1527 		doc = nand->priv;
1528 		/* Use the alias resolution register to determine if this is
1529 		   in fact the same DOC aliased to a new address.  If writes
1530 		   to one chip's alias resolution register change the value on
1531 		   the other chip, they're the same chip. */
1532 		if (ChipID == DOC_ChipID_DocMilPlus16) {
1533 			oldval = ReadDOC(doc->virtadr, Mplus_AliasResolution);
1534 			newval = ReadDOC(virtadr, Mplus_AliasResolution);
1535 		} else {
1536 			oldval = ReadDOC(doc->virtadr, AliasResolution);
1537 			newval = ReadDOC(virtadr, AliasResolution);
1538 		}
1539 		if (oldval != newval)
1540 			continue;
1541 		if (ChipID == DOC_ChipID_DocMilPlus16) {
1542 			WriteDOC(~newval, virtadr, Mplus_AliasResolution);
1543 			oldval = ReadDOC(doc->virtadr, Mplus_AliasResolution);
1544 			WriteDOC(newval, virtadr, Mplus_AliasResolution);	// restore it
1545 		} else {
1546 			WriteDOC(~newval, virtadr, AliasResolution);
1547 			oldval = ReadDOC(doc->virtadr, AliasResolution);
1548 			WriteDOC(newval, virtadr, AliasResolution);	// restore it
1549 		}
1550 		newval = ~newval;
1551 		if (oldval == newval) {
1552 			printk(KERN_DEBUG "Found alias of DOC at 0x%lx to 0x%lx\n", doc->physadr, physadr);
1553 			goto notfound;
1554 		}
1555 	}
1556 
1557 	printk(KERN_NOTICE "DiskOnChip found at 0x%lx\n", physadr);
1558 
1559 	len = sizeof(struct mtd_info) +
1560 	    sizeof(struct nand_chip) + sizeof(struct doc_priv) + (2 * sizeof(struct nand_bbt_descr));
1561 	mtd = kzalloc(len, GFP_KERNEL);
1562 	if (!mtd) {
1563 		ret = -ENOMEM;
1564 		goto fail;
1565 	}
1566 
1567 	nand			= (struct nand_chip *) (mtd + 1);
1568 	doc			= (struct doc_priv *) (nand + 1);
1569 	nand->bbt_td		= (struct nand_bbt_descr *) (doc + 1);
1570 	nand->bbt_md		= nand->bbt_td + 1;
1571 
1572 	mtd->priv		= nand;
1573 	mtd->owner		= THIS_MODULE;
1574 
1575 	nand->priv		= doc;
1576 	nand->select_chip	= doc200x_select_chip;
1577 	nand->cmd_ctrl		= doc200x_hwcontrol;
1578 	nand->dev_ready		= doc200x_dev_ready;
1579 	nand->waitfunc		= doc200x_wait;
1580 	nand->block_bad		= doc200x_block_bad;
1581 	nand->ecc.hwctl		= doc200x_enable_hwecc;
1582 	nand->ecc.calculate	= doc200x_calculate_ecc;
1583 	nand->ecc.correct	= doc200x_correct_data;
1584 
1585 	nand->ecc.layout	= &doc200x_oobinfo;
1586 	nand->ecc.mode		= NAND_ECC_HW_SYNDROME;
1587 	nand->ecc.size		= 512;
1588 	nand->ecc.bytes		= 6;
1589 	nand->ecc.strength	= 2;
1590 	nand->bbt_options	= NAND_BBT_USE_FLASH;
1591 	/* Skip the automatic BBT scan so we can run it manually */
1592 	nand->options		|= NAND_SKIP_BBTSCAN;
1593 
1594 	doc->physadr		= physadr;
1595 	doc->virtadr		= virtadr;
1596 	doc->ChipID		= ChipID;
1597 	doc->curfloor		= -1;
1598 	doc->curchip		= -1;
1599 	doc->mh0_page		= -1;
1600 	doc->mh1_page		= -1;
1601 	doc->nextdoc		= doclist;
1602 
1603 	if (ChipID == DOC_ChipID_Doc2k)
1604 		numchips = doc2000_init(mtd);
1605 	else if (ChipID == DOC_ChipID_DocMilPlus16)
1606 		numchips = doc2001plus_init(mtd);
1607 	else
1608 		numchips = doc2001_init(mtd);
1609 
1610 	if ((ret = nand_scan(mtd, numchips)) || (ret = doc->late_init(mtd))) {
1611 		/* DBB note: i believe nand_release is necessary here, as
1612 		   buffers may have been allocated in nand_base.  Check with
1613 		   Thomas. FIX ME! */
1614 		/* nand_release will call mtd_device_unregister, but we
1615 		   haven't yet added it.  This is handled without incident by
1616 		   mtd_device_unregister, as far as I can tell. */
1617 		nand_release(mtd);
1618 		kfree(mtd);
1619 		goto fail;
1620 	}
1621 
1622 	/* Success! */
1623 	doclist = mtd;
1624 	return 0;
1625 
1626  notfound:
1627 	/* Put back the contents of the DOCControl register, in case it's not
1628 	   actually a DiskOnChip.  */
1629 	WriteDOC(save_control, virtadr, DOCControl);
1630  fail:
1631 	iounmap(virtadr);
1632 
1633 error_ioremap:
1634 	release_mem_region(physadr, DOC_IOREMAP_LEN);
1635 
1636 	return ret;
1637 }
1638 
release_nanddoc(void)1639 static void release_nanddoc(void)
1640 {
1641 	struct mtd_info *mtd, *nextmtd;
1642 	struct nand_chip *nand;
1643 	struct doc_priv *doc;
1644 
1645 	for (mtd = doclist; mtd; mtd = nextmtd) {
1646 		nand = mtd->priv;
1647 		doc = nand->priv;
1648 
1649 		nextmtd = doc->nextdoc;
1650 		nand_release(mtd);
1651 		iounmap(doc->virtadr);
1652 		release_mem_region(doc->physadr, DOC_IOREMAP_LEN);
1653 		kfree(mtd);
1654 	}
1655 }
1656 
init_nanddoc(void)1657 static int __init init_nanddoc(void)
1658 {
1659 	int i, ret = 0;
1660 
1661 	/* We could create the decoder on demand, if memory is a concern.
1662 	 * This way we have it handy, if an error happens
1663 	 *
1664 	 * Symbolsize is 10 (bits)
1665 	 * Primitve polynomial is x^10+x^3+1
1666 	 * first consecutive root is 510
1667 	 * primitve element to generate roots = 1
1668 	 * generator polinomial degree = 4
1669 	 */
1670 	rs_decoder = init_rs(10, 0x409, FCR, 1, NROOTS);
1671 	if (!rs_decoder) {
1672 		printk(KERN_ERR "DiskOnChip: Could not create a RS decoder\n");
1673 		return -ENOMEM;
1674 	}
1675 
1676 	if (doc_config_location) {
1677 		printk(KERN_INFO "Using configured DiskOnChip probe address 0x%lx\n", doc_config_location);
1678 		ret = doc_probe(doc_config_location);
1679 		if (ret < 0)
1680 			goto outerr;
1681 	} else {
1682 		for (i = 0; (doc_locations[i] != 0xffffffff); i++) {
1683 			doc_probe(doc_locations[i]);
1684 		}
1685 	}
1686 	/* No banner message any more. Print a message if no DiskOnChip
1687 	   found, so the user knows we at least tried. */
1688 	if (!doclist) {
1689 		printk(KERN_INFO "No valid DiskOnChip devices found\n");
1690 		ret = -ENODEV;
1691 		goto outerr;
1692 	}
1693 	return 0;
1694  outerr:
1695 	free_rs(rs_decoder);
1696 	return ret;
1697 }
1698 
cleanup_nanddoc(void)1699 static void __exit cleanup_nanddoc(void)
1700 {
1701 	/* Cleanup the nand/DoC resources */
1702 	release_nanddoc();
1703 
1704 	/* Free the reed solomon resources */
1705 	if (rs_decoder) {
1706 		free_rs(rs_decoder);
1707 	}
1708 }
1709 
1710 module_init(init_nanddoc);
1711 module_exit(cleanup_nanddoc);
1712 
1713 MODULE_LICENSE("GPL");
1714 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
1715 MODULE_DESCRIPTION("M-Systems DiskOnChip 2000, Millennium and Millennium Plus device driver");
1716