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