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