blob: f77298f3af605729062d9917ac9b2fd4e06cd6a9 [file] [log] [blame]
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * 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>
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000011 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 * Error correction code lifted from the old docecc code
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000013 * Author: Fabrice Bellard (fabrice.bellard@netgem.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 * Copyright (C) 2000 Netgem S.A.
15 * converted to the generic Reed-Solomon library by Thomas Gleixner <tglx@linutronix.de>
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000016 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 * Interface to generic NAND code for M-Systems DiskOnChip devices
18 *
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000019 * $Id: diskonchip.c,v 1.55 2005/11/07 11:14:30 gleixner Exp $
Linus Torvalds1da177e2005-04-16 15:20:36 -070020 */
21
22#include <linux/kernel.h>
23#include <linux/init.h>
24#include <linux/sched.h>
25#include <linux/delay.h>
26#include <linux/rslib.h>
27#include <linux/moduleparam.h>
28#include <asm/io.h>
29
30#include <linux/mtd/mtd.h>
31#include <linux/mtd/nand.h>
32#include <linux/mtd/doc2000.h>
33#include <linux/mtd/compatmac.h>
34#include <linux/mtd/partitions.h>
35#include <linux/mtd/inftl.h>
36
37/* Where to look for the devices? */
Thomas Gleixner651078b2005-01-31 20:36:46 +000038#ifndef CONFIG_MTD_NAND_DISKONCHIP_PROBE_ADDRESS
39#define CONFIG_MTD_NAND_DISKONCHIP_PROBE_ADDRESS 0
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#endif
41
42static unsigned long __initdata doc_locations[] = {
43#if defined (__alpha__) || defined(__i386__) || defined(__x86_64__)
Thomas Gleixner651078b2005-01-31 20:36:46 +000044#ifdef CONFIG_MTD_NAND_DISKONCHIP_PROBE_HIGH
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000045 0xfffc8000, 0xfffca000, 0xfffcc000, 0xfffce000,
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 0xfffd0000, 0xfffd2000, 0xfffd4000, 0xfffd6000,
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000047 0xfffd8000, 0xfffda000, 0xfffdc000, 0xfffde000,
48 0xfffe0000, 0xfffe2000, 0xfffe4000, 0xfffe6000,
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 0xfffe8000, 0xfffea000, 0xfffec000, 0xfffee000,
50#else /* CONFIG_MTD_DOCPROBE_HIGH */
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000051 0xc8000, 0xca000, 0xcc000, 0xce000,
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 0xd0000, 0xd2000, 0xd4000, 0xd6000,
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000053 0xd8000, 0xda000, 0xdc000, 0xde000,
54 0xe0000, 0xe2000, 0xe4000, 0xe6000,
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 0xe8000, 0xea000, 0xec000, 0xee000,
56#endif /* CONFIG_MTD_DOCPROBE_HIGH */
57#elif defined(__PPC__)
58 0xe4000000,
59#elif defined(CONFIG_MOMENCO_OCELOT)
60 0x2f000000,
David Woodhousee0c7d762006-05-13 18:07:53 +010061 0xff000000,
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#elif defined(CONFIG_MOMENCO_OCELOT_G) || defined (CONFIG_MOMENCO_OCELOT_C)
David Woodhousee0c7d762006-05-13 18:07:53 +010063 0xff000000,
64#else
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#warning Unknown architecture for DiskOnChip. No default probe locations defined
66#endif
67 0xffffffff };
68
69static struct mtd_info *doclist = NULL;
70
71struct doc_priv {
72 void __iomem *virtadr;
73 unsigned long physadr;
74 u_char ChipID;
75 u_char CDSNControl;
David Woodhousee0c7d762006-05-13 18:07:53 +010076 int chips_per_floor; /* The number of chips detected on each floor */
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 int curfloor;
78 int curchip;
79 int mh0_page;
80 int mh1_page;
81 struct mtd_info *nextdoc;
82};
83
Linus Torvalds1da177e2005-04-16 15:20:36 -070084/* This is the syndrome computed by the HW ecc generator upon reading an empty
85 page, one with all 0xff for data and stored ecc code. */
86static u_char empty_read_syndrome[6] = { 0x26, 0xff, 0x6d, 0x47, 0x73, 0x7a };
David Woodhousee0c7d762006-05-13 18:07:53 +010087
Linus Torvalds1da177e2005-04-16 15:20:36 -070088/* This is the ecc value computed by the HW ecc generator upon writing an empty
89 page, one with all 0xff for data. */
90static u_char empty_write_ecc[6] = { 0x4b, 0x00, 0xe2, 0x0e, 0x93, 0xf7 };
91
92#define INFTL_BBT_RESERVED_BLOCKS 4
93
94#define DoC_is_MillenniumPlus(doc) ((doc)->ChipID == DOC_ChipID_DocMilPlus16 || (doc)->ChipID == DOC_ChipID_DocMilPlus32)
95#define DoC_is_Millennium(doc) ((doc)->ChipID == DOC_ChipID_DocMil)
96#define DoC_is_2000(doc) ((doc)->ChipID == DOC_ChipID_Doc2k)
97
98static void doc200x_hwcontrol(struct mtd_info *mtd, int cmd);
99static void doc200x_select_chip(struct mtd_info *mtd, int chip);
100
David Woodhousee0c7d762006-05-13 18:07:53 +0100101static int debug = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102module_param(debug, int, 0);
103
David Woodhousee0c7d762006-05-13 18:07:53 +0100104static int try_dword = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105module_param(try_dword, int, 0);
106
David Woodhousee0c7d762006-05-13 18:07:53 +0100107static int no_ecc_failures = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108module_param(no_ecc_failures, int, 0);
109
David Woodhousee0c7d762006-05-13 18:07:53 +0100110static int no_autopart = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111module_param(no_autopart, int, 0);
Dan Brown1a78ff62005-03-29 21:57:48 +0100112
David Woodhousee0c7d762006-05-13 18:07:53 +0100113static int show_firmware_partition = 0;
Dan Brown1a78ff62005-03-29 21:57:48 +0100114module_param(show_firmware_partition, int, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
116#ifdef MTD_NAND_DISKONCHIP_BBTWRITE
David Woodhousee0c7d762006-05-13 18:07:53 +0100117static int inftl_bbt_write = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118#else
David Woodhousee0c7d762006-05-13 18:07:53 +0100119static int inftl_bbt_write = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120#endif
121module_param(inftl_bbt_write, int, 0);
122
Thomas Gleixner651078b2005-01-31 20:36:46 +0000123static unsigned long doc_config_location = CONFIG_MTD_NAND_DISKONCHIP_PROBE_ADDRESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124module_param(doc_config_location, ulong, 0);
125MODULE_PARM_DESC(doc_config_location, "Physical memory address at which to probe for DiskOnChip");
126
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127/* Sector size for HW ECC */
128#define SECTOR_SIZE 512
129/* The sector bytes are packed into NB_DATA 10 bit words */
130#define NB_DATA (((SECTOR_SIZE + 1) * 8 + 6) / 10)
131/* Number of roots */
132#define NROOTS 4
133/* First consective root */
134#define FCR 510
135/* Number of symbols */
136#define NN 1023
137
138/* the Reed Solomon control structure */
139static struct rs_control *rs_decoder;
140
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000141/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 * The HW decoder in the DoC ASIC's provides us a error syndrome,
143 * which we must convert to a standard syndrom usable by the generic
144 * Reed-Solomon library code.
145 *
146 * Fabrice Bellard figured this out in the old docecc code. I added
147 * some comments, improved a minor bit and converted it to make use
148 * of the generic Reed-Solomon libary. tglx
149 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100150static int doc_ecc_decode(struct rs_control *rs, uint8_t *data, uint8_t *ecc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151{
152 int i, j, nerr, errpos[8];
153 uint8_t parity;
154 uint16_t ds[4], s[5], tmp, errval[8], syn[4];
155
156 /* Convert the ecc bytes into words */
157 ds[0] = ((ecc[4] & 0xff) >> 0) | ((ecc[5] & 0x03) << 8);
158 ds[1] = ((ecc[5] & 0xfc) >> 2) | ((ecc[2] & 0x0f) << 6);
159 ds[2] = ((ecc[2] & 0xf0) >> 4) | ((ecc[3] & 0x3f) << 4);
160 ds[3] = ((ecc[3] & 0xc0) >> 6) | ((ecc[0] & 0xff) << 2);
161 parity = ecc[1];
162
163 /* Initialize the syndrom buffer */
164 for (i = 0; i < NROOTS; i++)
165 s[i] = ds[0];
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000166 /*
167 * Evaluate
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 * s[i] = ds[3]x^3 + ds[2]x^2 + ds[1]x^1 + ds[0]
169 * where x = alpha^(FCR + i)
170 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100171 for (j = 1; j < NROOTS; j++) {
172 if (ds[j] == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 continue;
174 tmp = rs->index_of[ds[j]];
David Woodhousee0c7d762006-05-13 18:07:53 +0100175 for (i = 0; i < NROOTS; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 s[i] ^= rs->alpha_to[rs_modnn(rs, tmp + (FCR + i) * j)];
177 }
178
179 /* Calc s[i] = s[i] / alpha^(v + i) */
180 for (i = 0; i < NROOTS; i++) {
181 if (syn[i])
David Woodhousee0c7d762006-05-13 18:07:53 +0100182 syn[i] = rs_modnn(rs, rs->index_of[s[i]] + (NN - FCR - i));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 }
184 /* Call the decoder library */
185 nerr = decode_rs16(rs, NULL, NULL, 1019, syn, 0, errpos, 0, errval);
186
187 /* Incorrectable errors ? */
188 if (nerr < 0)
189 return nerr;
190
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000191 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 * Correct the errors. The bitpositions are a bit of magic,
193 * but they are given by the design of the de/encoder circuit
194 * in the DoC ASIC's.
195 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100196 for (i = 0; i < nerr; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 int index, bitpos, pos = 1015 - errpos[i];
198 uint8_t val;
199 if (pos >= NB_DATA && pos < 1019)
200 continue;
201 if (pos < NB_DATA) {
202 /* extract bit position (MSB first) */
203 pos = 10 * (NB_DATA - 1 - pos) - 6;
204 /* now correct the following 10 bits. At most two bytes
205 can be modified since pos is even */
206 index = (pos >> 3) ^ 1;
207 bitpos = pos & 7;
David Woodhousee0c7d762006-05-13 18:07:53 +0100208 if ((index >= 0 && index < SECTOR_SIZE) || index == (SECTOR_SIZE + 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 val = (uint8_t) (errval[i] >> (2 + bitpos));
210 parity ^= val;
211 if (index < SECTOR_SIZE)
212 data[index] ^= val;
213 }
214 index = ((pos >> 3) + 1) ^ 1;
215 bitpos = (bitpos + 10) & 7;
216 if (bitpos == 0)
217 bitpos = 8;
David Woodhousee0c7d762006-05-13 18:07:53 +0100218 if ((index >= 0 && index < SECTOR_SIZE) || index == (SECTOR_SIZE + 1)) {
219 val = (uint8_t) (errval[i] << (8 - bitpos));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 parity ^= val;
221 if (index < SECTOR_SIZE)
222 data[index] ^= val;
223 }
224 }
225 }
226 /* If the parity is wrong, no rescue possible */
227 return parity ? -1 : nerr;
228}
229
230static void DoC_Delay(struct doc_priv *doc, unsigned short cycles)
231{
232 volatile char dummy;
233 int i;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000234
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 for (i = 0; i < cycles; i++) {
236 if (DoC_is_Millennium(doc))
237 dummy = ReadDOC(doc->virtadr, NOP);
238 else if (DoC_is_MillenniumPlus(doc))
239 dummy = ReadDOC(doc->virtadr, Mplus_NOP);
240 else
241 dummy = ReadDOC(doc->virtadr, DOCStatus);
242 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000243
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244}
245
246#define CDSN_CTRL_FR_B_MASK (CDSN_CTRL_FR_B0 | CDSN_CTRL_FR_B1)
247
248/* DOC_WaitReady: Wait for RDY line to be asserted by the flash chip */
249static int _DoC_WaitReady(struct doc_priv *doc)
250{
David Woodhousee0c7d762006-05-13 18:07:53 +0100251 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 unsigned long timeo = jiffies + (HZ * 10);
253
David Woodhousee0c7d762006-05-13 18:07:53 +0100254 if (debug)
255 printk("_DoC_WaitReady...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 /* Out-of-line routine to wait for chip response */
257 if (DoC_is_MillenniumPlus(doc)) {
258 while ((ReadDOC(docptr, Mplus_FlashControl) & CDSN_CTRL_FR_B_MASK) != CDSN_CTRL_FR_B_MASK) {
259 if (time_after(jiffies, timeo)) {
260 printk("_DoC_WaitReady timed out.\n");
261 return -EIO;
262 }
263 udelay(1);
264 cond_resched();
265 }
266 } else {
267 while (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B)) {
268 if (time_after(jiffies, timeo)) {
269 printk("_DoC_WaitReady timed out.\n");
270 return -EIO;
271 }
272 udelay(1);
273 cond_resched();
274 }
275 }
276
277 return 0;
278}
279
280static inline int DoC_WaitReady(struct doc_priv *doc)
281{
David Woodhousee0c7d762006-05-13 18:07:53 +0100282 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 int ret = 0;
284
285 if (DoC_is_MillenniumPlus(doc)) {
286 DoC_Delay(doc, 4);
287
288 if ((ReadDOC(docptr, Mplus_FlashControl) & CDSN_CTRL_FR_B_MASK) != CDSN_CTRL_FR_B_MASK)
289 /* Call the out-of-line routine to wait */
290 ret = _DoC_WaitReady(doc);
291 } else {
292 DoC_Delay(doc, 4);
293
294 if (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B))
295 /* Call the out-of-line routine to wait */
296 ret = _DoC_WaitReady(doc);
297 DoC_Delay(doc, 2);
298 }
299
David Woodhousee0c7d762006-05-13 18:07:53 +0100300 if (debug)
301 printk("DoC_WaitReady OK\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 return ret;
303}
304
305static void doc2000_write_byte(struct mtd_info *mtd, u_char datum)
306{
307 struct nand_chip *this = mtd->priv;
308 struct doc_priv *doc = this->priv;
David Woodhousee0c7d762006-05-13 18:07:53 +0100309 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
David Woodhousee0c7d762006-05-13 18:07:53 +0100311 if (debug)
312 printk("write_byte %02x\n", datum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 WriteDOC(datum, docptr, CDSNSlowIO);
314 WriteDOC(datum, docptr, 2k_CDSN_IO);
315}
316
317static u_char doc2000_read_byte(struct mtd_info *mtd)
318{
319 struct nand_chip *this = mtd->priv;
320 struct doc_priv *doc = this->priv;
David Woodhousee0c7d762006-05-13 18:07:53 +0100321 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 u_char ret;
323
324 ReadDOC(docptr, CDSNSlowIO);
325 DoC_Delay(doc, 2);
326 ret = ReadDOC(docptr, 2k_CDSN_IO);
David Woodhousee0c7d762006-05-13 18:07:53 +0100327 if (debug)
328 printk("read_byte returns %02x\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 return ret;
330}
331
David Woodhousee0c7d762006-05-13 18:07:53 +0100332static void doc2000_writebuf(struct mtd_info *mtd, const u_char *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333{
334 struct nand_chip *this = mtd->priv;
335 struct doc_priv *doc = this->priv;
David Woodhousee0c7d762006-05-13 18:07:53 +0100336 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 int i;
David Woodhousee0c7d762006-05-13 18:07:53 +0100338 if (debug)
339 printk("writebuf of %d bytes: ", len);
340 for (i = 0; i < len; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 WriteDOC_(buf[i], docptr, DoC_2k_CDSN_IO + i);
342 if (debug && i < 16)
343 printk("%02x ", buf[i]);
344 }
David Woodhousee0c7d762006-05-13 18:07:53 +0100345 if (debug)
346 printk("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347}
348
David Woodhousee0c7d762006-05-13 18:07:53 +0100349static void doc2000_readbuf(struct mtd_info *mtd, u_char *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350{
351 struct nand_chip *this = mtd->priv;
352 struct doc_priv *doc = this->priv;
David Woodhousee0c7d762006-05-13 18:07:53 +0100353 void __iomem *docptr = doc->virtadr;
354 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
David Woodhousee0c7d762006-05-13 18:07:53 +0100356 if (debug)
357 printk("readbuf of %d bytes: ", len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
David Woodhousee0c7d762006-05-13 18:07:53 +0100359 for (i = 0; i < len; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 buf[i] = ReadDOC(docptr, 2k_CDSN_IO + i);
361 }
362}
363
David Woodhousee0c7d762006-05-13 18:07:53 +0100364static void doc2000_readbuf_dword(struct mtd_info *mtd, u_char *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365{
366 struct nand_chip *this = mtd->priv;
367 struct doc_priv *doc = this->priv;
David Woodhousee0c7d762006-05-13 18:07:53 +0100368 void __iomem *docptr = doc->virtadr;
369 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
David Woodhousee0c7d762006-05-13 18:07:53 +0100371 if (debug)
372 printk("readbuf_dword of %d bytes: ", len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
David Woodhousee0c7d762006-05-13 18:07:53 +0100374 if (unlikely((((unsigned long)buf) | len) & 3)) {
375 for (i = 0; i < len; i++) {
376 *(uint8_t *) (&buf[i]) = ReadDOC(docptr, 2k_CDSN_IO + i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 }
378 } else {
David Woodhousee0c7d762006-05-13 18:07:53 +0100379 for (i = 0; i < len; i += 4) {
380 *(uint32_t *) (&buf[i]) = readl(docptr + DoC_2k_CDSN_IO + i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 }
382 }
383}
384
David Woodhousee0c7d762006-05-13 18:07:53 +0100385static int doc2000_verifybuf(struct mtd_info *mtd, const u_char *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386{
387 struct nand_chip *this = mtd->priv;
388 struct doc_priv *doc = this->priv;
David Woodhousee0c7d762006-05-13 18:07:53 +0100389 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 int i;
391
David Woodhousee0c7d762006-05-13 18:07:53 +0100392 for (i = 0; i < len; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 if (buf[i] != ReadDOC(docptr, 2k_CDSN_IO))
394 return -EFAULT;
395 return 0;
396}
397
398static uint16_t __init doc200x_ident_chip(struct mtd_info *mtd, int nr)
399{
400 struct nand_chip *this = mtd->priv;
401 struct doc_priv *doc = this->priv;
402 uint16_t ret;
403
404 doc200x_select_chip(mtd, nr);
405 doc200x_hwcontrol(mtd, NAND_CTL_SETCLE);
406 this->write_byte(mtd, NAND_CMD_READID);
407 doc200x_hwcontrol(mtd, NAND_CTL_CLRCLE);
408 doc200x_hwcontrol(mtd, NAND_CTL_SETALE);
409 this->write_byte(mtd, 0);
410 doc200x_hwcontrol(mtd, NAND_CTL_CLRALE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000411
Thomas Gleixnerdfd61292005-02-22 21:48:25 +0000412 /* We cant' use dev_ready here, but at least we wait for the
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000413 * command to complete
Thomas Gleixnerdfd61292005-02-22 21:48:25 +0000414 */
415 udelay(50);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000416
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 ret = this->read_byte(mtd) << 8;
418 ret |= this->read_byte(mtd);
419
420 if (doc->ChipID == DOC_ChipID_Doc2k && try_dword && !nr) {
421 /* First chip probe. See if we get same results by 32-bit access */
422 union {
423 uint32_t dword;
424 uint8_t byte[4];
425 } ident;
426 void __iomem *docptr = doc->virtadr;
427
428 doc200x_hwcontrol(mtd, NAND_CTL_SETCLE);
429 doc2000_write_byte(mtd, NAND_CMD_READID);
430 doc200x_hwcontrol(mtd, NAND_CTL_CLRCLE);
431 doc200x_hwcontrol(mtd, NAND_CTL_SETALE);
432 doc2000_write_byte(mtd, 0);
433 doc200x_hwcontrol(mtd, NAND_CTL_CLRALE);
434
Thomas Gleixnerdfd61292005-02-22 21:48:25 +0000435 udelay(50);
436
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 ident.dword = readl(docptr + DoC_2k_CDSN_IO);
438 if (((ident.byte[0] << 8) | ident.byte[1]) == ret) {
439 printk(KERN_INFO "DiskOnChip 2000 responds to DWORD access\n");
440 this->read_buf = &doc2000_readbuf_dword;
441 }
442 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000443
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 return ret;
445}
446
447static void __init doc2000_count_chips(struct mtd_info *mtd)
448{
449 struct nand_chip *this = mtd->priv;
450 struct doc_priv *doc = this->priv;
451 uint16_t mfrid;
452 int i;
453
454 /* Max 4 chips per floor on DiskOnChip 2000 */
455 doc->chips_per_floor = 4;
456
457 /* Find out what the first chip is */
458 mfrid = doc200x_ident_chip(mtd, 0);
459
460 /* Find how many chips in each floor. */
461 for (i = 1; i < 4; i++) {
462 if (doc200x_ident_chip(mtd, i) != mfrid)
463 break;
464 }
465 doc->chips_per_floor = i;
466 printk(KERN_DEBUG "Detected %d chips per floor.\n", i);
467}
468
469static int doc200x_wait(struct mtd_info *mtd, struct nand_chip *this, int state)
470{
471 struct doc_priv *doc = this->priv;
472
473 int status;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000474
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 DoC_WaitReady(doc);
476 this->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
477 DoC_WaitReady(doc);
478 status = (int)this->read_byte(mtd);
479
480 return status;
481}
482
483static void doc2001_write_byte(struct mtd_info *mtd, u_char datum)
484{
485 struct nand_chip *this = mtd->priv;
486 struct doc_priv *doc = this->priv;
David Woodhousee0c7d762006-05-13 18:07:53 +0100487 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
489 WriteDOC(datum, docptr, CDSNSlowIO);
490 WriteDOC(datum, docptr, Mil_CDSN_IO);
491 WriteDOC(datum, docptr, WritePipeTerm);
492}
493
494static u_char doc2001_read_byte(struct mtd_info *mtd)
495{
496 struct nand_chip *this = mtd->priv;
497 struct doc_priv *doc = this->priv;
David Woodhousee0c7d762006-05-13 18:07:53 +0100498 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
500 //ReadDOC(docptr, CDSNSlowIO);
501 /* 11.4.5 -- delay twice to allow extended length cycle */
502 DoC_Delay(doc, 2);
503 ReadDOC(docptr, ReadPipeInit);
504 //return ReadDOC(docptr, Mil_CDSN_IO);
505 return ReadDOC(docptr, LastDataRead);
506}
507
David Woodhousee0c7d762006-05-13 18:07:53 +0100508static void doc2001_writebuf(struct mtd_info *mtd, const u_char *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509{
510 struct nand_chip *this = mtd->priv;
511 struct doc_priv *doc = this->priv;
David Woodhousee0c7d762006-05-13 18:07:53 +0100512 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 int i;
514
David Woodhousee0c7d762006-05-13 18:07:53 +0100515 for (i = 0; i < len; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 WriteDOC_(buf[i], docptr, DoC_Mil_CDSN_IO + i);
517 /* Terminate write pipeline */
518 WriteDOC(0x00, docptr, WritePipeTerm);
519}
520
David Woodhousee0c7d762006-05-13 18:07:53 +0100521static void doc2001_readbuf(struct mtd_info *mtd, u_char *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522{
523 struct nand_chip *this = mtd->priv;
524 struct doc_priv *doc = this->priv;
David Woodhousee0c7d762006-05-13 18:07:53 +0100525 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 int i;
527
528 /* Start read pipeline */
529 ReadDOC(docptr, ReadPipeInit);
530
David Woodhousee0c7d762006-05-13 18:07:53 +0100531 for (i = 0; i < len - 1; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 buf[i] = ReadDOC(docptr, Mil_CDSN_IO + (i & 0xff));
533
534 /* Terminate read pipeline */
535 buf[i] = ReadDOC(docptr, LastDataRead);
536}
537
David Woodhousee0c7d762006-05-13 18:07:53 +0100538static int doc2001_verifybuf(struct mtd_info *mtd, const u_char *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539{
540 struct nand_chip *this = mtd->priv;
541 struct doc_priv *doc = this->priv;
David Woodhousee0c7d762006-05-13 18:07:53 +0100542 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 int i;
544
545 /* Start read pipeline */
546 ReadDOC(docptr, ReadPipeInit);
547
David Woodhousee0c7d762006-05-13 18:07:53 +0100548 for (i = 0; i < len - 1; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 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;
David Woodhousee0c7d762006-05-13 18:07:53 +0100562 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 u_char ret;
564
David Woodhousee0c7d762006-05-13 18:07:53 +0100565 ReadDOC(docptr, Mplus_ReadPipeInit);
566 ReadDOC(docptr, Mplus_ReadPipeInit);
567 ret = ReadDOC(docptr, Mplus_LastDataRead);
568 if (debug)
569 printk("read_byte returns %02x\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 return ret;
571}
572
David Woodhousee0c7d762006-05-13 18:07:53 +0100573static void doc2001plus_writebuf(struct mtd_info *mtd, const u_char *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574{
575 struct nand_chip *this = mtd->priv;
576 struct doc_priv *doc = this->priv;
David Woodhousee0c7d762006-05-13 18:07:53 +0100577 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 int i;
579
David Woodhousee0c7d762006-05-13 18:07:53 +0100580 if (debug)
581 printk("writebuf of %d bytes: ", len);
582 for (i = 0; i < len; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 WriteDOC_(buf[i], docptr, DoC_Mil_CDSN_IO + i);
584 if (debug && i < 16)
585 printk("%02x ", buf[i]);
586 }
David Woodhousee0c7d762006-05-13 18:07:53 +0100587 if (debug)
588 printk("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589}
590
David Woodhousee0c7d762006-05-13 18:07:53 +0100591static void doc2001plus_readbuf(struct mtd_info *mtd, u_char *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592{
593 struct nand_chip *this = mtd->priv;
594 struct doc_priv *doc = this->priv;
David Woodhousee0c7d762006-05-13 18:07:53 +0100595 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 int i;
597
David Woodhousee0c7d762006-05-13 18:07:53 +0100598 if (debug)
599 printk("readbuf of %d bytes: ", len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
601 /* Start read pipeline */
602 ReadDOC(docptr, Mplus_ReadPipeInit);
603 ReadDOC(docptr, Mplus_ReadPipeInit);
604
David Woodhousee0c7d762006-05-13 18:07:53 +0100605 for (i = 0; i < len - 2; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 buf[i] = ReadDOC(docptr, Mil_CDSN_IO);
607 if (debug && i < 16)
608 printk("%02x ", buf[i]);
609 }
610
611 /* Terminate read pipeline */
David Woodhousee0c7d762006-05-13 18:07:53 +0100612 buf[len - 2] = ReadDOC(docptr, Mplus_LastDataRead);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 if (debug && i < 16)
David Woodhousee0c7d762006-05-13 18:07:53 +0100614 printk("%02x ", buf[len - 2]);
615 buf[len - 1] = ReadDOC(docptr, Mplus_LastDataRead);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 if (debug && i < 16)
David Woodhousee0c7d762006-05-13 18:07:53 +0100617 printk("%02x ", buf[len - 1]);
618 if (debug)
619 printk("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620}
621
David Woodhousee0c7d762006-05-13 18:07:53 +0100622static int doc2001plus_verifybuf(struct mtd_info *mtd, const u_char *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623{
624 struct nand_chip *this = mtd->priv;
625 struct doc_priv *doc = this->priv;
David Woodhousee0c7d762006-05-13 18:07:53 +0100626 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 int i;
628
David Woodhousee0c7d762006-05-13 18:07:53 +0100629 if (debug)
630 printk("verifybuf of %d bytes: ", len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
632 /* Start read pipeline */
633 ReadDOC(docptr, Mplus_ReadPipeInit);
634 ReadDOC(docptr, Mplus_ReadPipeInit);
635
David Woodhousee0c7d762006-05-13 18:07:53 +0100636 for (i = 0; i < len - 2; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 if (buf[i] != ReadDOC(docptr, Mil_CDSN_IO)) {
638 ReadDOC(docptr, Mplus_LastDataRead);
639 ReadDOC(docptr, Mplus_LastDataRead);
640 return i;
641 }
David Woodhousee0c7d762006-05-13 18:07:53 +0100642 if (buf[len - 2] != ReadDOC(docptr, Mplus_LastDataRead))
643 return len - 2;
644 if (buf[len - 1] != ReadDOC(docptr, Mplus_LastDataRead))
645 return len - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 return 0;
647}
648
649static void doc2001plus_select_chip(struct mtd_info *mtd, int chip)
650{
651 struct nand_chip *this = mtd->priv;
652 struct doc_priv *doc = this->priv;
David Woodhousee0c7d762006-05-13 18:07:53 +0100653 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 int floor = 0;
655
David Woodhousee0c7d762006-05-13 18:07:53 +0100656 if (debug)
657 printk("select chip (%d)\n", chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
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;
David Woodhousee0c7d762006-05-13 18:07:53 +0100666 chip -= (floor * doc->chips_per_floor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
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;
David Woodhousee0c7d762006-05-13 18:07:53 +0100680 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 int floor = 0;
682
David Woodhousee0c7d762006-05-13 18:07:53 +0100683 if (debug)
684 printk("select chip (%d)\n", chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
686 if (chip == -1)
687 return;
688
689 floor = chip / doc->chips_per_floor;
David Woodhousee0c7d762006-05-13 18:07:53 +0100690 chip -= (floor * doc->chips_per_floor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
692 /* 11.4.4 -- deassert CE before changing chip */
693 doc200x_hwcontrol(mtd, NAND_CTL_CLRNCE);
694
695 WriteDOC(floor, docptr, FloorSelect);
696 WriteDOC(chip, docptr, CDSNDeviceSelect);
697
698 doc200x_hwcontrol(mtd, NAND_CTL_SETNCE);
699
700 doc->curchip = chip;
701 doc->curfloor = floor;
702}
703
704static void doc200x_hwcontrol(struct mtd_info *mtd, int cmd)
705{
706 struct nand_chip *this = mtd->priv;
707 struct doc_priv *doc = this->priv;
David Woodhousee0c7d762006-05-13 18:07:53 +0100708 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
David Woodhousee0c7d762006-05-13 18:07:53 +0100710 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 case NAND_CTL_SETNCE:
712 doc->CDSNControl |= CDSN_CTRL_CE;
713 break;
714 case NAND_CTL_CLRNCE:
715 doc->CDSNControl &= ~CDSN_CTRL_CE;
716 break;
717 case NAND_CTL_SETCLE:
718 doc->CDSNControl |= CDSN_CTRL_CLE;
719 break;
720 case NAND_CTL_CLRCLE:
721 doc->CDSNControl &= ~CDSN_CTRL_CLE;
722 break;
723 case NAND_CTL_SETALE:
724 doc->CDSNControl |= CDSN_CTRL_ALE;
725 break;
726 case NAND_CTL_CLRALE:
727 doc->CDSNControl &= ~CDSN_CTRL_ALE;
728 break;
729 case NAND_CTL_SETWP:
730 doc->CDSNControl |= CDSN_CTRL_WP;
731 break;
732 case NAND_CTL_CLRWP:
733 doc->CDSNControl &= ~CDSN_CTRL_WP;
734 break;
735 }
David Woodhousee0c7d762006-05-13 18:07:53 +0100736 if (debug)
737 printk("hwcontrol(%d): %02x\n", cmd, doc->CDSNControl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 WriteDOC(doc->CDSNControl, docptr, CDSNControl);
739 /* 11.4.3 -- 4 NOPs after CSDNControl write */
740 DoC_Delay(doc, 4);
741}
742
David Woodhousee0c7d762006-05-13 18:07:53 +0100743static void doc2001plus_command(struct mtd_info *mtd, unsigned command, int column, int page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744{
745 struct nand_chip *this = mtd->priv;
746 struct doc_priv *doc = this->priv;
David Woodhousee0c7d762006-05-13 18:07:53 +0100747 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
749 /*
750 * Must terminate write pipeline before sending any commands
751 * to the device.
752 */
753 if (command == NAND_CMD_PAGEPROG) {
754 WriteDOC(0x00, docptr, Mplus_WritePipeTerm);
755 WriteDOC(0x00, docptr, Mplus_WritePipeTerm);
756 }
757
758 /*
759 * Write out the command to the device.
760 */
761 if (command == NAND_CMD_SEQIN) {
762 int readcmd;
763
Joern Engel28318772006-05-22 23:18:05 +0200764 if (column >= mtd->writesize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 /* OOB area */
Joern Engel28318772006-05-22 23:18:05 +0200766 column -= mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 readcmd = NAND_CMD_READOOB;
768 } else if (column < 256) {
769 /* First 256 bytes --> READ0 */
770 readcmd = NAND_CMD_READ0;
771 } else {
772 column -= 256;
773 readcmd = NAND_CMD_READ1;
774 }
775 WriteDOC(readcmd, docptr, Mplus_FlashCmd);
776 }
777 WriteDOC(command, docptr, Mplus_FlashCmd);
778 WriteDOC(0, docptr, Mplus_WritePipeTerm);
779 WriteDOC(0, docptr, Mplus_WritePipeTerm);
780
781 if (column != -1 || page_addr != -1) {
782 /* Serially input address */
783 if (column != -1) {
784 /* Adjust columns for 16 bit buswidth */
785 if (this->options & NAND_BUSWIDTH_16)
786 column >>= 1;
787 WriteDOC(column, docptr, Mplus_FlashAddress);
788 }
789 if (page_addr != -1) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100790 WriteDOC((unsigned char)(page_addr & 0xff), docptr, Mplus_FlashAddress);
791 WriteDOC((unsigned char)((page_addr >> 8) & 0xff), docptr, Mplus_FlashAddress);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 /* One more address cycle for higher density devices */
793 if (this->chipsize & 0x0c000000) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100794 WriteDOC((unsigned char)((page_addr >> 16) & 0x0f), docptr, Mplus_FlashAddress);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 printk("high density\n");
796 }
797 }
798 WriteDOC(0, docptr, Mplus_WritePipeTerm);
799 WriteDOC(0, docptr, Mplus_WritePipeTerm);
800 /* deassert ALE */
David Woodhousee0c7d762006-05-13 18:07:53 +0100801 if (command == NAND_CMD_READ0 || command == NAND_CMD_READ1 ||
802 command == NAND_CMD_READOOB || command == NAND_CMD_READID)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 WriteDOC(0, docptr, Mplus_FlashControl);
804 }
805
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000806 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 * program and erase have their own busy handlers
808 * status and sequential in needs no delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100809 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 switch (command) {
811
812 case NAND_CMD_PAGEPROG:
813 case NAND_CMD_ERASE1:
814 case NAND_CMD_ERASE2:
815 case NAND_CMD_SEQIN:
816 case NAND_CMD_STATUS:
817 return;
818
819 case NAND_CMD_RESET:
820 if (this->dev_ready)
821 break;
822 udelay(this->chip_delay);
823 WriteDOC(NAND_CMD_STATUS, docptr, Mplus_FlashCmd);
824 WriteDOC(0, docptr, Mplus_WritePipeTerm);
825 WriteDOC(0, docptr, Mplus_WritePipeTerm);
David Woodhousee0c7d762006-05-13 18:07:53 +0100826 while (!(this->read_byte(mtd) & 0x40)) ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 return;
828
David Woodhousee0c7d762006-05-13 18:07:53 +0100829 /* This applies to read commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000831 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 * If we don't have access to the busy pin, we apply the given
833 * command delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100834 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 if (!this->dev_ready) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100836 udelay(this->chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 return;
838 }
839 }
840
841 /* Apply this short delay always to ensure that we do wait tWB in
842 * any case on any machine. */
David Woodhousee0c7d762006-05-13 18:07:53 +0100843 ndelay(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 /* wait until command is processed */
David Woodhousee0c7d762006-05-13 18:07:53 +0100845 while (!this->dev_ready(mtd)) ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846}
847
848static int doc200x_dev_ready(struct mtd_info *mtd)
849{
850 struct nand_chip *this = mtd->priv;
851 struct doc_priv *doc = this->priv;
David Woodhousee0c7d762006-05-13 18:07:53 +0100852 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
854 if (DoC_is_MillenniumPlus(doc)) {
855 /* 11.4.2 -- must NOP four times before checking FR/B# */
856 DoC_Delay(doc, 4);
857 if ((ReadDOC(docptr, Mplus_FlashControl) & CDSN_CTRL_FR_B_MASK) != CDSN_CTRL_FR_B_MASK) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100858 if (debug)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 printk("not ready\n");
860 return 0;
861 }
David Woodhousee0c7d762006-05-13 18:07:53 +0100862 if (debug)
863 printk("was ready\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 return 1;
865 } else {
866 /* 11.4.2 -- must NOP four times before checking FR/B# */
867 DoC_Delay(doc, 4);
868 if (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B)) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100869 if (debug)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 printk("not ready\n");
871 return 0;
872 }
873 /* 11.4.2 -- Must NOP twice if it's ready */
874 DoC_Delay(doc, 2);
David Woodhousee0c7d762006-05-13 18:07:53 +0100875 if (debug)
876 printk("was ready\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 return 1;
878 }
879}
880
881static int doc200x_block_bad(struct mtd_info *mtd, loff_t ofs, int getchip)
882{
883 /* This is our last resort if we couldn't find or create a BBT. Just
884 pretend all blocks are good. */
885 return 0;
886}
887
888static void doc200x_enable_hwecc(struct mtd_info *mtd, int mode)
889{
890 struct nand_chip *this = mtd->priv;
891 struct doc_priv *doc = this->priv;
David Woodhousee0c7d762006-05-13 18:07:53 +0100892 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
894 /* Prime the ECC engine */
David Woodhousee0c7d762006-05-13 18:07:53 +0100895 switch (mode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 case NAND_ECC_READ:
897 WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
898 WriteDOC(DOC_ECC_EN, docptr, ECCConf);
899 break;
900 case NAND_ECC_WRITE:
901 WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
902 WriteDOC(DOC_ECC_EN | DOC_ECC_RW, docptr, ECCConf);
903 break;
904 }
905}
906
907static void doc2001plus_enable_hwecc(struct mtd_info *mtd, int mode)
908{
909 struct nand_chip *this = mtd->priv;
910 struct doc_priv *doc = this->priv;
David Woodhousee0c7d762006-05-13 18:07:53 +0100911 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
913 /* Prime the ECC engine */
David Woodhousee0c7d762006-05-13 18:07:53 +0100914 switch (mode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 case NAND_ECC_READ:
916 WriteDOC(DOC_ECC_RESET, docptr, Mplus_ECCConf);
917 WriteDOC(DOC_ECC_EN, docptr, Mplus_ECCConf);
918 break;
919 case NAND_ECC_WRITE:
920 WriteDOC(DOC_ECC_RESET, docptr, Mplus_ECCConf);
921 WriteDOC(DOC_ECC_EN | DOC_ECC_RW, docptr, Mplus_ECCConf);
922 break;
923 }
924}
925
926/* This code is only called on write */
David Woodhousee0c7d762006-05-13 18:07:53 +0100927static int doc200x_calculate_ecc(struct mtd_info *mtd, const u_char *dat, unsigned char *ecc_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928{
929 struct nand_chip *this = mtd->priv;
930 struct doc_priv *doc = this->priv;
David Woodhousee0c7d762006-05-13 18:07:53 +0100931 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 int i;
933 int emptymatch = 1;
934
935 /* flush the pipeline */
936 if (DoC_is_2000(doc)) {
937 WriteDOC(doc->CDSNControl & ~CDSN_CTRL_FLASH_IO, docptr, CDSNControl);
938 WriteDOC(0, docptr, 2k_CDSN_IO);
939 WriteDOC(0, docptr, 2k_CDSN_IO);
940 WriteDOC(0, docptr, 2k_CDSN_IO);
941 WriteDOC(doc->CDSNControl, docptr, CDSNControl);
942 } else if (DoC_is_MillenniumPlus(doc)) {
943 WriteDOC(0, docptr, Mplus_NOP);
944 WriteDOC(0, docptr, Mplus_NOP);
945 WriteDOC(0, docptr, Mplus_NOP);
946 } else {
947 WriteDOC(0, docptr, NOP);
948 WriteDOC(0, docptr, NOP);
949 WriteDOC(0, docptr, NOP);
950 }
951
952 for (i = 0; i < 6; i++) {
953 if (DoC_is_MillenniumPlus(doc))
954 ecc_code[i] = ReadDOC_(docptr, DoC_Mplus_ECCSyndrome0 + i);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000955 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 ecc_code[i] = ReadDOC_(docptr, DoC_ECCSyndrome0 + i);
957 if (ecc_code[i] != empty_write_ecc[i])
958 emptymatch = 0;
959 }
960 if (DoC_is_MillenniumPlus(doc))
961 WriteDOC(DOC_ECC_DIS, docptr, Mplus_ECCConf);
962 else
963 WriteDOC(DOC_ECC_DIS, docptr, ECCConf);
964#if 0
965 /* If emptymatch=1, we might have an all-0xff data buffer. Check. */
966 if (emptymatch) {
967 /* Note: this somewhat expensive test should not be triggered
968 often. It could be optimized away by examining the data in
969 the writebuf routine, and remembering the result. */
970 for (i = 0; i < 512; i++) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100971 if (dat[i] == 0xff)
972 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 emptymatch = 0;
974 break;
975 }
976 }
977 /* If emptymatch still =1, we do have an all-0xff data buffer.
978 Return all-0xff ecc value instead of the computed one, so
979 it'll look just like a freshly-erased page. */
David Woodhousee0c7d762006-05-13 18:07:53 +0100980 if (emptymatch)
981 memset(ecc_code, 0xff, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982#endif
983 return 0;
984}
985
986static int doc200x_correct_data(struct mtd_info *mtd, u_char *dat, u_char *read_ecc, u_char *calc_ecc)
987{
988 int i, ret = 0;
989 struct nand_chip *this = mtd->priv;
990 struct doc_priv *doc = this->priv;
David Woodhousee0c7d762006-05-13 18:07:53 +0100991 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 volatile u_char dummy;
993 int emptymatch = 1;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000994
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 /* flush the pipeline */
996 if (DoC_is_2000(doc)) {
997 dummy = ReadDOC(docptr, 2k_ECCStatus);
998 dummy = ReadDOC(docptr, 2k_ECCStatus);
999 dummy = ReadDOC(docptr, 2k_ECCStatus);
1000 } else if (DoC_is_MillenniumPlus(doc)) {
1001 dummy = ReadDOC(docptr, Mplus_ECCConf);
1002 dummy = ReadDOC(docptr, Mplus_ECCConf);
1003 dummy = ReadDOC(docptr, Mplus_ECCConf);
1004 } else {
1005 dummy = ReadDOC(docptr, ECCConf);
1006 dummy = ReadDOC(docptr, ECCConf);
1007 dummy = ReadDOC(docptr, ECCConf);
1008 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001009
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 /* Error occured ? */
1011 if (dummy & 0x80) {
1012 for (i = 0; i < 6; i++) {
1013 if (DoC_is_MillenniumPlus(doc))
1014 calc_ecc[i] = ReadDOC_(docptr, DoC_Mplus_ECCSyndrome0 + i);
1015 else
1016 calc_ecc[i] = ReadDOC_(docptr, DoC_ECCSyndrome0 + i);
1017 if (calc_ecc[i] != empty_read_syndrome[i])
1018 emptymatch = 0;
1019 }
1020 /* If emptymatch=1, the read syndrome is consistent with an
1021 all-0xff data and stored ecc block. Check the stored ecc. */
1022 if (emptymatch) {
1023 for (i = 0; i < 6; i++) {
David Woodhousee0c7d762006-05-13 18:07:53 +01001024 if (read_ecc[i] == 0xff)
1025 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 emptymatch = 0;
1027 break;
1028 }
1029 }
1030 /* If emptymatch still =1, check the data block. */
1031 if (emptymatch) {
David Woodhousee0c7d762006-05-13 18:07:53 +01001032 /* Note: this somewhat expensive test should not be triggered
1033 often. It could be optimized away by examining the data in
1034 the readbuf routine, and remembering the result. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 for (i = 0; i < 512; i++) {
David Woodhousee0c7d762006-05-13 18:07:53 +01001036 if (dat[i] == 0xff)
1037 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 emptymatch = 0;
1039 break;
1040 }
1041 }
1042 /* If emptymatch still =1, this is almost certainly a freshly-
1043 erased block, in which case the ECC will not come out right.
1044 We'll suppress the error and tell the caller everything's
1045 OK. Because it is. */
David Woodhousee0c7d762006-05-13 18:07:53 +01001046 if (!emptymatch)
1047 ret = doc_ecc_decode(rs_decoder, dat, calc_ecc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 if (ret > 0)
1049 printk(KERN_ERR "doc200x_correct_data corrected %d errors\n", ret);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001050 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 if (DoC_is_MillenniumPlus(doc))
1052 WriteDOC(DOC_ECC_DIS, docptr, Mplus_ECCConf);
1053 else
1054 WriteDOC(DOC_ECC_DIS, docptr, ECCConf);
1055 if (no_ecc_failures && (ret == -1)) {
1056 printk(KERN_ERR "suppressing ECC failure\n");
1057 ret = 0;
1058 }
1059 return ret;
1060}
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001061
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062//u_char mydatabuf[528];
1063
Dan Brownabc37e62005-04-07 15:22:58 +01001064/* The strange out-of-order .oobfree list below is a (possibly unneeded)
1065 * attempt to retain compatibility. It used to read:
1066 * .oobfree = { {8, 8} }
1067 * Since that leaves two bytes unusable, it was changed. But the following
1068 * scheme might affect existing jffs2 installs by moving the cleanmarker:
1069 * .oobfree = { {6, 10} }
1070 * jffs2 seems to handle the above gracefully, but the current scheme seems
1071 * safer. The only problem with it is that any code that parses oobfree must
1072 * be able to handle out-of-order segments.
1073 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074static struct nand_oobinfo doc200x_oobinfo = {
David Woodhousee0c7d762006-05-13 18:07:53 +01001075 .useecc = MTD_NANDECC_AUTOPLACE,
1076 .eccbytes = 6,
1077 .eccpos = {0, 1, 2, 3, 4, 5},
1078 .oobfree = {{8, 8}, {6, 2}}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079};
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001080
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081/* Find the (I)NFTL Media Header, and optionally also the mirror media header.
1082 On sucessful return, buf will contain a copy of the media header for
1083 further processing. id is the string to scan for, and will presumably be
1084 either "ANAND" or "BNAND". If findmirror=1, also look for the mirror media
1085 header. The page #s of the found media headers are placed in mh0_page and
1086 mh1_page in the DOC private structure. */
David Woodhousee0c7d762006-05-13 18:07:53 +01001087static int __init find_media_headers(struct mtd_info *mtd, u_char *buf, const char *id, int findmirror)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088{
1089 struct nand_chip *this = mtd->priv;
1090 struct doc_priv *doc = this->priv;
Dan Brown1a78ff62005-03-29 21:57:48 +01001091 unsigned offs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 int ret;
1093 size_t retlen;
1094
Dan Brown1a78ff62005-03-29 21:57:48 +01001095 for (offs = 0; offs < mtd->size; offs += mtd->erasesize) {
Joern Engel28318772006-05-22 23:18:05 +02001096 ret = mtd->read(mtd, offs, mtd->writesize, &retlen, buf);
1097 if (retlen != mtd->writesize)
David Woodhousee0c7d762006-05-13 18:07:53 +01001098 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 if (ret) {
David Woodhousee0c7d762006-05-13 18:07:53 +01001100 printk(KERN_WARNING "ECC error scanning DOC at 0x%x\n", offs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 }
David Woodhousee0c7d762006-05-13 18:07:53 +01001102 if (memcmp(buf, id, 6))
1103 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 printk(KERN_INFO "Found DiskOnChip %s Media Header at 0x%x\n", id, offs);
1105 if (doc->mh0_page == -1) {
1106 doc->mh0_page = offs >> this->page_shift;
David Woodhousee0c7d762006-05-13 18:07:53 +01001107 if (!findmirror)
1108 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 continue;
1110 }
1111 doc->mh1_page = offs >> this->page_shift;
1112 return 2;
1113 }
1114 if (doc->mh0_page == -1) {
1115 printk(KERN_WARNING "DiskOnChip %s Media Header not found.\n", id);
1116 return 0;
1117 }
1118 /* Only one mediaheader was found. We want buf to contain a
1119 mediaheader on return, so we'll have to re-read the one we found. */
1120 offs = doc->mh0_page << this->page_shift;
Joern Engel28318772006-05-22 23:18:05 +02001121 ret = mtd->read(mtd, offs, mtd->writesize, &retlen, buf);
1122 if (retlen != mtd->writesize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 /* Insanity. Give up. */
1124 printk(KERN_ERR "Read DiskOnChip Media Header once, but can't reread it???\n");
1125 return 0;
1126 }
1127 return 1;
1128}
1129
David Woodhousee0c7d762006-05-13 18:07:53 +01001130static inline int __init nftl_partscan(struct mtd_info *mtd, struct mtd_partition *parts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131{
1132 struct nand_chip *this = mtd->priv;
1133 struct doc_priv *doc = this->priv;
1134 int ret = 0;
1135 u_char *buf;
1136 struct NFTLMediaHeader *mh;
1137 const unsigned psize = 1 << this->page_shift;
Dan Brown1a78ff62005-03-29 21:57:48 +01001138 int numparts = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 unsigned blocks, maxblocks;
1140 int offs, numheaders;
1141
Joern Engel28318772006-05-22 23:18:05 +02001142 buf = kmalloc(mtd->writesize, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 if (!buf) {
1144 printk(KERN_ERR "DiskOnChip mediaheader kmalloc failed!\n");
1145 return 0;
1146 }
David Woodhousee0c7d762006-05-13 18:07:53 +01001147 if (!(numheaders = find_media_headers(mtd, buf, "ANAND", 1)))
1148 goto out;
1149 mh = (struct NFTLMediaHeader *)buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150
Thomas Gleixnerf29a4b82005-01-31 22:22:24 +00001151 mh->NumEraseUnits = le16_to_cpu(mh->NumEraseUnits);
1152 mh->FirstPhysicalEUN = le16_to_cpu(mh->FirstPhysicalEUN);
1153 mh->FormattedSize = le32_to_cpu(mh->FormattedSize);
1154
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 printk(KERN_INFO " DataOrgID = %s\n"
1156 " NumEraseUnits = %d\n"
1157 " FirstPhysicalEUN = %d\n"
1158 " FormattedSize = %d\n"
1159 " UnitSizeFactor = %d\n",
1160 mh->DataOrgID, mh->NumEraseUnits,
1161 mh->FirstPhysicalEUN, mh->FormattedSize,
1162 mh->UnitSizeFactor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
1164 blocks = mtd->size >> this->phys_erase_shift;
1165 maxblocks = min(32768U, mtd->erasesize - psize);
1166
1167 if (mh->UnitSizeFactor == 0x00) {
1168 /* Auto-determine UnitSizeFactor. The constraints are:
1169 - There can be at most 32768 virtual blocks.
1170 - There can be at most (virtual block size - page size)
David Woodhousee0c7d762006-05-13 18:07:53 +01001171 virtual blocks (because MediaHeader+BBT must fit in 1).
1172 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 mh->UnitSizeFactor = 0xff;
1174 while (blocks > maxblocks) {
1175 blocks >>= 1;
1176 maxblocks = min(32768U, (maxblocks << 1) + psize);
1177 mh->UnitSizeFactor--;
1178 }
1179 printk(KERN_WARNING "UnitSizeFactor=0x00 detected. Correct value is assumed to be 0x%02x.\n", mh->UnitSizeFactor);
1180 }
1181
1182 /* NOTE: The lines below modify internal variables of the NAND and MTD
1183 layers; variables with have already been configured by nand_scan.
1184 Unfortunately, we didn't know before this point what these values
1185 should be. Thus, this code is somewhat dependant on the exact
1186 implementation of the NAND layer. */
1187 if (mh->UnitSizeFactor != 0xff) {
1188 this->bbt_erase_shift += (0xff - mh->UnitSizeFactor);
1189 mtd->erasesize <<= (0xff - mh->UnitSizeFactor);
1190 printk(KERN_INFO "Setting virtual erase size to %d\n", mtd->erasesize);
1191 blocks = mtd->size >> this->bbt_erase_shift;
1192 maxblocks = min(32768U, mtd->erasesize - psize);
1193 }
1194
1195 if (blocks > maxblocks) {
1196 printk(KERN_ERR "UnitSizeFactor of 0x%02x is inconsistent with device size. Aborting.\n", mh->UnitSizeFactor);
1197 goto out;
1198 }
1199
1200 /* Skip past the media headers. */
1201 offs = max(doc->mh0_page, doc->mh1_page);
1202 offs <<= this->page_shift;
1203 offs += mtd->erasesize;
1204
Dan Brown1a78ff62005-03-29 21:57:48 +01001205 if (show_firmware_partition == 1) {
1206 parts[0].name = " DiskOnChip Firmware / Media Header partition";
1207 parts[0].offset = 0;
1208 parts[0].size = offs;
1209 numparts = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 }
Dan Brown1a78ff62005-03-29 21:57:48 +01001211
1212 parts[numparts].name = " DiskOnChip BDTL partition";
1213 parts[numparts].offset = offs;
1214 parts[numparts].size = (mh->NumEraseUnits - numheaders) << this->bbt_erase_shift;
1215
1216 offs += parts[numparts].size;
1217 numparts++;
1218
1219 if (offs < mtd->size) {
1220 parts[numparts].name = " DiskOnChip Remainder partition";
1221 parts[numparts].offset = offs;
1222 parts[numparts].size = mtd->size - offs;
1223 numparts++;
1224 }
1225
1226 ret = numparts;
David Woodhousee0c7d762006-05-13 18:07:53 +01001227 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 kfree(buf);
1229 return ret;
1230}
1231
1232/* This is a stripped-down copy of the code in inftlmount.c */
David Woodhousee0c7d762006-05-13 18:07:53 +01001233static inline int __init inftl_partscan(struct mtd_info *mtd, struct mtd_partition *parts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234{
1235 struct nand_chip *this = mtd->priv;
1236 struct doc_priv *doc = this->priv;
1237 int ret = 0;
1238 u_char *buf;
1239 struct INFTLMediaHeader *mh;
1240 struct INFTLPartition *ip;
1241 int numparts = 0;
1242 int blocks;
1243 int vshift, lastvunit = 0;
1244 int i;
1245 int end = mtd->size;
1246
1247 if (inftl_bbt_write)
1248 end -= (INFTL_BBT_RESERVED_BLOCKS << this->phys_erase_shift);
1249
Joern Engel28318772006-05-22 23:18:05 +02001250 buf = kmalloc(mtd->writesize, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 if (!buf) {
1252 printk(KERN_ERR "DiskOnChip mediaheader kmalloc failed!\n");
1253 return 0;
1254 }
1255
David Woodhousee0c7d762006-05-13 18:07:53 +01001256 if (!find_media_headers(mtd, buf, "BNAND", 0))
1257 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 doc->mh1_page = doc->mh0_page + (4096 >> this->page_shift);
David Woodhousee0c7d762006-05-13 18:07:53 +01001259 mh = (struct INFTLMediaHeader *)buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
1261 mh->NoOfBootImageBlocks = le32_to_cpu(mh->NoOfBootImageBlocks);
1262 mh->NoOfBinaryPartitions = le32_to_cpu(mh->NoOfBinaryPartitions);
1263 mh->NoOfBDTLPartitions = le32_to_cpu(mh->NoOfBDTLPartitions);
1264 mh->BlockMultiplierBits = le32_to_cpu(mh->BlockMultiplierBits);
1265 mh->FormatFlags = le32_to_cpu(mh->FormatFlags);
1266 mh->PercentUsed = le32_to_cpu(mh->PercentUsed);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001267
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 printk(KERN_INFO " bootRecordID = %s\n"
1269 " NoOfBootImageBlocks = %d\n"
1270 " NoOfBinaryPartitions = %d\n"
1271 " NoOfBDTLPartitions = %d\n"
1272 " BlockMultiplerBits = %d\n"
1273 " FormatFlgs = %d\n"
1274 " OsakVersion = %d.%d.%d.%d\n"
1275 " PercentUsed = %d\n",
1276 mh->bootRecordID, mh->NoOfBootImageBlocks,
1277 mh->NoOfBinaryPartitions,
1278 mh->NoOfBDTLPartitions,
1279 mh->BlockMultiplierBits, mh->FormatFlags,
1280 ((unsigned char *) &mh->OsakVersion)[0] & 0xf,
1281 ((unsigned char *) &mh->OsakVersion)[1] & 0xf,
1282 ((unsigned char *) &mh->OsakVersion)[2] & 0xf,
1283 ((unsigned char *) &mh->OsakVersion)[3] & 0xf,
1284 mh->PercentUsed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285
1286 vshift = this->phys_erase_shift + mh->BlockMultiplierBits;
1287
1288 blocks = mtd->size >> vshift;
1289 if (blocks > 32768) {
1290 printk(KERN_ERR "BlockMultiplierBits=%d is inconsistent with device size. Aborting.\n", mh->BlockMultiplierBits);
1291 goto out;
1292 }
1293
1294 blocks = doc->chips_per_floor << (this->chip_shift - this->phys_erase_shift);
1295 if (inftl_bbt_write && (blocks > mtd->erasesize)) {
1296 printk(KERN_ERR "Writeable BBTs spanning more than one erase block are not yet supported. FIX ME!\n");
1297 goto out;
1298 }
1299
1300 /* Scan the partitions */
1301 for (i = 0; (i < 4); i++) {
1302 ip = &(mh->Partitions[i]);
1303 ip->virtualUnits = le32_to_cpu(ip->virtualUnits);
1304 ip->firstUnit = le32_to_cpu(ip->firstUnit);
1305 ip->lastUnit = le32_to_cpu(ip->lastUnit);
1306 ip->flags = le32_to_cpu(ip->flags);
1307 ip->spareUnits = le32_to_cpu(ip->spareUnits);
1308 ip->Reserved0 = le32_to_cpu(ip->Reserved0);
1309
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 printk(KERN_INFO " PARTITION[%d] ->\n"
1311 " virtualUnits = %d\n"
1312 " firstUnit = %d\n"
1313 " lastUnit = %d\n"
1314 " flags = 0x%x\n"
1315 " spareUnits = %d\n",
1316 i, ip->virtualUnits, ip->firstUnit,
1317 ip->lastUnit, ip->flags,
1318 ip->spareUnits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319
Dan Brown1a78ff62005-03-29 21:57:48 +01001320 if ((show_firmware_partition == 1) &&
1321 (i == 0) && (ip->firstUnit > 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 parts[0].name = " DiskOnChip IPL / Media Header partition";
1323 parts[0].offset = 0;
1324 parts[0].size = mtd->erasesize * ip->firstUnit;
1325 numparts = 1;
1326 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327
1328 if (ip->flags & INFTL_BINARY)
1329 parts[numparts].name = " DiskOnChip BDK partition";
1330 else
1331 parts[numparts].name = " DiskOnChip BDTL partition";
1332 parts[numparts].offset = ip->firstUnit << vshift;
1333 parts[numparts].size = (1 + ip->lastUnit - ip->firstUnit) << vshift;
1334 numparts++;
David Woodhousee0c7d762006-05-13 18:07:53 +01001335 if (ip->lastUnit > lastvunit)
1336 lastvunit = ip->lastUnit;
1337 if (ip->flags & INFTL_LAST)
1338 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 }
1340 lastvunit++;
1341 if ((lastvunit << vshift) < end) {
1342 parts[numparts].name = " DiskOnChip Remainder partition";
1343 parts[numparts].offset = lastvunit << vshift;
1344 parts[numparts].size = end - parts[numparts].offset;
1345 numparts++;
1346 }
1347 ret = numparts;
David Woodhousee0c7d762006-05-13 18:07:53 +01001348 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 kfree(buf);
1350 return ret;
1351}
1352
1353static int __init nftl_scan_bbt(struct mtd_info *mtd)
1354{
1355 int ret, numparts;
1356 struct nand_chip *this = mtd->priv;
1357 struct doc_priv *doc = this->priv;
1358 struct mtd_partition parts[2];
1359
David Woodhousee0c7d762006-05-13 18:07:53 +01001360 memset((char *)parts, 0, sizeof(parts));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 /* On NFTL, we have to find the media headers before we can read the
1362 BBTs, since they're stored in the media header eraseblocks. */
1363 numparts = nftl_partscan(mtd, parts);
David Woodhousee0c7d762006-05-13 18:07:53 +01001364 if (!numparts)
1365 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 this->bbt_td->options = NAND_BBT_ABSPAGE | NAND_BBT_8BIT |
1367 NAND_BBT_SAVECONTENT | NAND_BBT_WRITE |
1368 NAND_BBT_VERSION;
1369 this->bbt_td->veroffs = 7;
1370 this->bbt_td->pages[0] = doc->mh0_page + 1;
1371 if (doc->mh1_page != -1) {
1372 this->bbt_md->options = NAND_BBT_ABSPAGE | NAND_BBT_8BIT |
1373 NAND_BBT_SAVECONTENT | NAND_BBT_WRITE |
1374 NAND_BBT_VERSION;
1375 this->bbt_md->veroffs = 7;
1376 this->bbt_md->pages[0] = doc->mh1_page + 1;
1377 } else {
1378 this->bbt_md = NULL;
1379 }
1380
1381 /* It's safe to set bd=NULL below because NAND_BBT_CREATE is not set.
1382 At least as nand_bbt.c is currently written. */
1383 if ((ret = nand_scan_bbt(mtd, NULL)))
1384 return ret;
1385 add_mtd_device(mtd);
1386#ifdef CONFIG_MTD_PARTITIONS
1387 if (!no_autopart)
1388 add_mtd_partitions(mtd, parts, numparts);
1389#endif
1390 return 0;
1391}
1392
1393static int __init inftl_scan_bbt(struct mtd_info *mtd)
1394{
1395 int ret, numparts;
1396 struct nand_chip *this = mtd->priv;
1397 struct doc_priv *doc = this->priv;
1398 struct mtd_partition parts[5];
1399
1400 if (this->numchips > doc->chips_per_floor) {
1401 printk(KERN_ERR "Multi-floor INFTL devices not yet supported.\n");
1402 return -EIO;
1403 }
1404
1405 if (DoC_is_MillenniumPlus(doc)) {
1406 this->bbt_td->options = NAND_BBT_2BIT | NAND_BBT_ABSPAGE;
1407 if (inftl_bbt_write)
1408 this->bbt_td->options |= NAND_BBT_WRITE;
1409 this->bbt_td->pages[0] = 2;
1410 this->bbt_md = NULL;
1411 } else {
David Woodhousee0c7d762006-05-13 18:07:53 +01001412 this->bbt_td->options = NAND_BBT_LASTBLOCK | NAND_BBT_8BIT | NAND_BBT_VERSION;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 if (inftl_bbt_write)
1414 this->bbt_td->options |= NAND_BBT_WRITE;
1415 this->bbt_td->offs = 8;
1416 this->bbt_td->len = 8;
1417 this->bbt_td->veroffs = 7;
1418 this->bbt_td->maxblocks = INFTL_BBT_RESERVED_BLOCKS;
1419 this->bbt_td->reserved_block_code = 0x01;
1420 this->bbt_td->pattern = "MSYS_BBT";
1421
David Woodhousee0c7d762006-05-13 18:07:53 +01001422 this->bbt_md->options = NAND_BBT_LASTBLOCK | NAND_BBT_8BIT | NAND_BBT_VERSION;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 if (inftl_bbt_write)
1424 this->bbt_md->options |= NAND_BBT_WRITE;
1425 this->bbt_md->offs = 8;
1426 this->bbt_md->len = 8;
1427 this->bbt_md->veroffs = 7;
1428 this->bbt_md->maxblocks = INFTL_BBT_RESERVED_BLOCKS;
1429 this->bbt_md->reserved_block_code = 0x01;
1430 this->bbt_md->pattern = "TBB_SYSM";
1431 }
1432
1433 /* It's safe to set bd=NULL below because NAND_BBT_CREATE is not set.
1434 At least as nand_bbt.c is currently written. */
1435 if ((ret = nand_scan_bbt(mtd, NULL)))
1436 return ret;
David Woodhousee0c7d762006-05-13 18:07:53 +01001437 memset((char *)parts, 0, sizeof(parts));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 numparts = inftl_partscan(mtd, parts);
1439 /* At least for now, require the INFTL Media Header. We could probably
1440 do without it for non-INFTL use, since all it gives us is
1441 autopartitioning, but I want to give it more thought. */
David Woodhousee0c7d762006-05-13 18:07:53 +01001442 if (!numparts)
1443 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 add_mtd_device(mtd);
1445#ifdef CONFIG_MTD_PARTITIONS
1446 if (!no_autopart)
1447 add_mtd_partitions(mtd, parts, numparts);
1448#endif
1449 return 0;
1450}
1451
1452static inline int __init doc2000_init(struct mtd_info *mtd)
1453{
1454 struct nand_chip *this = mtd->priv;
1455 struct doc_priv *doc = this->priv;
1456
1457 this->write_byte = doc2000_write_byte;
1458 this->read_byte = doc2000_read_byte;
1459 this->write_buf = doc2000_writebuf;
1460 this->read_buf = doc2000_readbuf;
1461 this->verify_buf = doc2000_verifybuf;
1462 this->scan_bbt = nftl_scan_bbt;
1463
1464 doc->CDSNControl = CDSN_CTRL_FLASH_IO | CDSN_CTRL_ECC_IO;
1465 doc2000_count_chips(mtd);
1466 mtd->name = "DiskOnChip 2000 (NFTL Model)";
1467 return (4 * doc->chips_per_floor);
1468}
1469
1470static inline int __init doc2001_init(struct mtd_info *mtd)
1471{
1472 struct nand_chip *this = mtd->priv;
1473 struct doc_priv *doc = this->priv;
1474
1475 this->write_byte = doc2001_write_byte;
1476 this->read_byte = doc2001_read_byte;
1477 this->write_buf = doc2001_writebuf;
1478 this->read_buf = doc2001_readbuf;
1479 this->verify_buf = doc2001_verifybuf;
1480
1481 ReadDOC(doc->virtadr, ChipID);
1482 ReadDOC(doc->virtadr, ChipID);
1483 ReadDOC(doc->virtadr, ChipID);
1484 if (ReadDOC(doc->virtadr, ChipID) != DOC_ChipID_DocMil) {
1485 /* It's not a Millennium; it's one of the newer
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001486 DiskOnChip 2000 units with a similar ASIC.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 Treat it like a Millennium, except that it
1488 can have multiple chips. */
1489 doc2000_count_chips(mtd);
1490 mtd->name = "DiskOnChip 2000 (INFTL Model)";
1491 this->scan_bbt = inftl_scan_bbt;
1492 return (4 * doc->chips_per_floor);
1493 } else {
1494 /* Bog-standard Millennium */
1495 doc->chips_per_floor = 1;
1496 mtd->name = "DiskOnChip Millennium";
1497 this->scan_bbt = nftl_scan_bbt;
1498 return 1;
1499 }
1500}
1501
1502static inline int __init doc2001plus_init(struct mtd_info *mtd)
1503{
1504 struct nand_chip *this = mtd->priv;
1505 struct doc_priv *doc = this->priv;
1506
1507 this->write_byte = NULL;
1508 this->read_byte = doc2001plus_read_byte;
1509 this->write_buf = doc2001plus_writebuf;
1510 this->read_buf = doc2001plus_readbuf;
1511 this->verify_buf = doc2001plus_verifybuf;
1512 this->scan_bbt = inftl_scan_bbt;
1513 this->hwcontrol = NULL;
1514 this->select_chip = doc2001plus_select_chip;
1515 this->cmdfunc = doc2001plus_command;
Thomas Gleixner0cddd6c2006-05-23 15:59:58 +02001516 this->ecc.hwctl = doc2001plus_enable_hwecc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517
1518 doc->chips_per_floor = 1;
1519 mtd->name = "DiskOnChip Millennium Plus";
1520
1521 return 1;
1522}
1523
Arjan van de Ven858119e2006-01-14 13:20:43 -08001524static int __init doc_probe(unsigned long physadr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525{
1526 unsigned char ChipID;
1527 struct mtd_info *mtd;
1528 struct nand_chip *nand;
1529 struct doc_priv *doc;
1530 void __iomem *virtadr;
1531 unsigned char save_control;
1532 unsigned char tmp, tmpb, tmpc;
1533 int reg, len, numchips;
1534 int ret = 0;
1535
1536 virtadr = ioremap(physadr, DOC_IOREMAP_LEN);
1537 if (!virtadr) {
1538 printk(KERN_ERR "Diskonchip ioremap failed: 0x%x bytes at 0x%lx\n", DOC_IOREMAP_LEN, physadr);
1539 return -EIO;
1540 }
1541
1542 /* It's not possible to cleanly detect the DiskOnChip - the
1543 * bootup procedure will put the device into reset mode, and
1544 * it's not possible to talk to it without actually writing
1545 * to the DOCControl register. So we store the current contents
1546 * of the DOCControl register's location, in case we later decide
1547 * that it's not a DiskOnChip, and want to put it back how we
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001548 * found it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 */
1550 save_control = ReadDOC(virtadr, DOCControl);
1551
1552 /* Reset the DiskOnChip ASIC */
David Woodhousee0c7d762006-05-13 18:07:53 +01001553 WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_RESET, virtadr, DOCControl);
1554 WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_RESET, virtadr, DOCControl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555
1556 /* Enable the DiskOnChip ASIC */
David Woodhousee0c7d762006-05-13 18:07:53 +01001557 WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_NORMAL, virtadr, DOCControl);
1558 WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_NORMAL, virtadr, DOCControl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559
1560 ChipID = ReadDOC(virtadr, ChipID);
1561
David Woodhousee0c7d762006-05-13 18:07:53 +01001562 switch (ChipID) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 case DOC_ChipID_Doc2k:
1564 reg = DoC_2k_ECCStatus;
1565 break;
1566 case DOC_ChipID_DocMil:
1567 reg = DoC_ECCConf;
1568 break;
1569 case DOC_ChipID_DocMilPlus16:
1570 case DOC_ChipID_DocMilPlus32:
1571 case 0:
1572 /* Possible Millennium Plus, need to do more checks */
1573 /* Possibly release from power down mode */
1574 for (tmp = 0; (tmp < 4); tmp++)
1575 ReadDOC(virtadr, Mplus_Power);
1576
1577 /* Reset the Millennium Plus ASIC */
David Woodhousee0c7d762006-05-13 18:07:53 +01001578 tmp = DOC_MODE_RESET | DOC_MODE_MDWREN | DOC_MODE_RST_LAT | DOC_MODE_BDECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 WriteDOC(tmp, virtadr, Mplus_DOCControl);
1580 WriteDOC(~tmp, virtadr, Mplus_CtrlConfirm);
1581
1582 mdelay(1);
1583 /* Enable the Millennium Plus ASIC */
David Woodhousee0c7d762006-05-13 18:07:53 +01001584 tmp = DOC_MODE_NORMAL | DOC_MODE_MDWREN | DOC_MODE_RST_LAT | DOC_MODE_BDECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 WriteDOC(tmp, virtadr, Mplus_DOCControl);
1586 WriteDOC(~tmp, virtadr, Mplus_CtrlConfirm);
1587 mdelay(1);
1588
1589 ChipID = ReadDOC(virtadr, ChipID);
1590
1591 switch (ChipID) {
1592 case DOC_ChipID_DocMilPlus16:
1593 reg = DoC_Mplus_Toggle;
1594 break;
1595 case DOC_ChipID_DocMilPlus32:
1596 printk(KERN_ERR "DiskOnChip Millennium Plus 32MB is not supported, ignoring.\n");
1597 default:
1598 ret = -ENODEV;
1599 goto notfound;
1600 }
1601 break;
1602
1603 default:
1604 ret = -ENODEV;
1605 goto notfound;
1606 }
1607 /* Check the TOGGLE bit in the ECC register */
David Woodhousee0c7d762006-05-13 18:07:53 +01001608 tmp = ReadDOC_(virtadr, reg) & DOC_TOGGLE_BIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 tmpb = ReadDOC_(virtadr, reg) & DOC_TOGGLE_BIT;
1610 tmpc = ReadDOC_(virtadr, reg) & DOC_TOGGLE_BIT;
1611 if ((tmp == tmpb) || (tmp != tmpc)) {
1612 printk(KERN_WARNING "Possible DiskOnChip at 0x%lx failed TOGGLE test, dropping.\n", physadr);
1613 ret = -ENODEV;
1614 goto notfound;
1615 }
1616
1617 for (mtd = doclist; mtd; mtd = doc->nextdoc) {
1618 unsigned char oldval;
1619 unsigned char newval;
1620 nand = mtd->priv;
1621 doc = nand->priv;
1622 /* Use the alias resolution register to determine if this is
1623 in fact the same DOC aliased to a new address. If writes
1624 to one chip's alias resolution register change the value on
1625 the other chip, they're the same chip. */
1626 if (ChipID == DOC_ChipID_DocMilPlus16) {
1627 oldval = ReadDOC(doc->virtadr, Mplus_AliasResolution);
1628 newval = ReadDOC(virtadr, Mplus_AliasResolution);
1629 } else {
1630 oldval = ReadDOC(doc->virtadr, AliasResolution);
1631 newval = ReadDOC(virtadr, AliasResolution);
1632 }
1633 if (oldval != newval)
1634 continue;
1635 if (ChipID == DOC_ChipID_DocMilPlus16) {
1636 WriteDOC(~newval, virtadr, Mplus_AliasResolution);
1637 oldval = ReadDOC(doc->virtadr, Mplus_AliasResolution);
David Woodhousee0c7d762006-05-13 18:07:53 +01001638 WriteDOC(newval, virtadr, Mplus_AliasResolution); // restore it
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 } else {
1640 WriteDOC(~newval, virtadr, AliasResolution);
1641 oldval = ReadDOC(doc->virtadr, AliasResolution);
David Woodhousee0c7d762006-05-13 18:07:53 +01001642 WriteDOC(newval, virtadr, AliasResolution); // restore it
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 }
1644 newval = ~newval;
1645 if (oldval == newval) {
1646 printk(KERN_DEBUG "Found alias of DOC at 0x%lx to 0x%lx\n", doc->physadr, physadr);
1647 goto notfound;
1648 }
1649 }
1650
1651 printk(KERN_NOTICE "DiskOnChip found at 0x%lx\n", physadr);
1652
1653 len = sizeof(struct mtd_info) +
David Woodhousee0c7d762006-05-13 18:07:53 +01001654 sizeof(struct nand_chip) + sizeof(struct doc_priv) + (2 * sizeof(struct nand_bbt_descr));
1655 mtd = kmalloc(len, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 if (!mtd) {
1657 printk(KERN_ERR "DiskOnChip kmalloc (%d bytes) failed!\n", len);
1658 ret = -ENOMEM;
1659 goto fail;
1660 }
1661 memset(mtd, 0, len);
1662
1663 nand = (struct nand_chip *) (mtd + 1);
1664 doc = (struct doc_priv *) (nand + 1);
1665 nand->bbt_td = (struct nand_bbt_descr *) (doc + 1);
1666 nand->bbt_md = nand->bbt_td + 1;
1667
1668 mtd->priv = nand;
1669 mtd->owner = THIS_MODULE;
1670
1671 nand->priv = doc;
1672 nand->select_chip = doc200x_select_chip;
1673 nand->hwcontrol = doc200x_hwcontrol;
1674 nand->dev_ready = doc200x_dev_ready;
1675 nand->waitfunc = doc200x_wait;
1676 nand->block_bad = doc200x_block_bad;
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02001677 nand->ecc.hwctl = doc200x_enable_hwecc;
1678 nand->ecc.calculate = doc200x_calculate_ecc;
1679 nand->ecc.correct = doc200x_correct_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680
1681 nand->autooob = &doc200x_oobinfo;
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02001682 nand->ecc.mode = NAND_ECC_HW_SYNDROME;
1683 nand->ecc.size = 512;
1684 nand->ecc.bytes = 6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 nand->options = NAND_USE_FLASH_BBT | NAND_HWECC_SYNDROME;
1686
1687 doc->physadr = physadr;
1688 doc->virtadr = virtadr;
1689 doc->ChipID = ChipID;
1690 doc->curfloor = -1;
1691 doc->curchip = -1;
1692 doc->mh0_page = -1;
1693 doc->mh1_page = -1;
1694 doc->nextdoc = doclist;
1695
1696 if (ChipID == DOC_ChipID_Doc2k)
1697 numchips = doc2000_init(mtd);
1698 else if (ChipID == DOC_ChipID_DocMilPlus16)
1699 numchips = doc2001plus_init(mtd);
1700 else
1701 numchips = doc2001_init(mtd);
1702
1703 if ((ret = nand_scan(mtd, numchips))) {
1704 /* DBB note: i believe nand_release is necessary here, as
1705 buffers may have been allocated in nand_base. Check with
1706 Thomas. FIX ME! */
1707 /* nand_release will call del_mtd_device, but we haven't yet
1708 added it. This is handled without incident by
1709 del_mtd_device, as far as I can tell. */
1710 nand_release(mtd);
1711 kfree(mtd);
1712 goto fail;
1713 }
1714
1715 /* Success! */
1716 doclist = mtd;
1717 return 0;
1718
David Woodhousee0c7d762006-05-13 18:07:53 +01001719 notfound:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 /* Put back the contents of the DOCControl register, in case it's not
1721 actually a DiskOnChip. */
1722 WriteDOC(save_control, virtadr, DOCControl);
David Woodhousee0c7d762006-05-13 18:07:53 +01001723 fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 iounmap(virtadr);
1725 return ret;
1726}
1727
1728static void release_nanddoc(void)
1729{
David Woodhousee0c7d762006-05-13 18:07:53 +01001730 struct mtd_info *mtd, *nextmtd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 struct nand_chip *nand;
1732 struct doc_priv *doc;
1733
1734 for (mtd = doclist; mtd; mtd = nextmtd) {
1735 nand = mtd->priv;
1736 doc = nand->priv;
1737
1738 nextmtd = doc->nextdoc;
1739 nand_release(mtd);
1740 iounmap(doc->virtadr);
1741 kfree(mtd);
1742 }
1743}
1744
1745static int __init init_nanddoc(void)
1746{
1747 int i, ret = 0;
1748
1749 /* We could create the decoder on demand, if memory is a concern.
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001750 * This way we have it handy, if an error happens
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 *
1752 * Symbolsize is 10 (bits)
1753 * Primitve polynomial is x^10+x^3+1
1754 * first consecutive root is 510
1755 * primitve element to generate roots = 1
1756 * generator polinomial degree = 4
1757 */
1758 rs_decoder = init_rs(10, 0x409, FCR, 1, NROOTS);
David Woodhousee0c7d762006-05-13 18:07:53 +01001759 if (!rs_decoder) {
1760 printk(KERN_ERR "DiskOnChip: Could not create a RS decoder\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 return -ENOMEM;
1762 }
1763
1764 if (doc_config_location) {
1765 printk(KERN_INFO "Using configured DiskOnChip probe address 0x%lx\n", doc_config_location);
1766 ret = doc_probe(doc_config_location);
1767 if (ret < 0)
1768 goto outerr;
1769 } else {
David Woodhousee0c7d762006-05-13 18:07:53 +01001770 for (i = 0; (doc_locations[i] != 0xffffffff); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 doc_probe(doc_locations[i]);
1772 }
1773 }
1774 /* No banner message any more. Print a message if no DiskOnChip
1775 found, so the user knows we at least tried. */
1776 if (!doclist) {
1777 printk(KERN_INFO "No valid DiskOnChip devices found\n");
1778 ret = -ENODEV;
1779 goto outerr;
1780 }
1781 return 0;
David Woodhousee0c7d762006-05-13 18:07:53 +01001782 outerr:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 free_rs(rs_decoder);
1784 return ret;
1785}
1786
1787static void __exit cleanup_nanddoc(void)
1788{
1789 /* Cleanup the nand/DoC resources */
1790 release_nanddoc();
1791
1792 /* Free the reed solomon resources */
1793 if (rs_decoder) {
1794 free_rs(rs_decoder);
1795 }
1796}
1797
1798module_init(init_nanddoc);
1799module_exit(cleanup_nanddoc);
1800
1801MODULE_LICENSE("GPL");
1802MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
1803MODULE_DESCRIPTION("M-Systems DiskOnChip 2000, Millennium and Millennium Plus device driver\n");