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