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