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