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