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