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