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