blob: e96259f22cca3405b6919c2d01f3b5eb11049552 [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,
Franck Bui-Huu192cca62007-06-11 15:08:55 +020059#elif defined(CONFIG_MOMENCO_OCELOT_G)
David Woodhousee0c7d762006-05-13 18:07:53 +010060 0xff000000,
61#else
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#warning Unknown architecture for DiskOnChip. No default probe locations defined
63#endif
64 0xffffffff };
65
66static struct mtd_info *doclist = NULL;
67
68struct doc_priv {
69 void __iomem *virtadr;
70 unsigned long physadr;
71 u_char ChipID;
72 u_char CDSNControl;
David Woodhousee0c7d762006-05-13 18:07:53 +010073 int chips_per_floor; /* The number of chips detected on each floor */
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 int curfloor;
75 int curchip;
76 int mh0_page;
77 int mh1_page;
78 struct mtd_info *nextdoc;
79};
80
Linus Torvalds1da177e2005-04-16 15:20:36 -070081/* This is the syndrome computed by the HW ecc generator upon reading an empty
82 page, one with all 0xff for data and stored ecc code. */
83static u_char empty_read_syndrome[6] = { 0x26, 0xff, 0x6d, 0x47, 0x73, 0x7a };
David Woodhousee0c7d762006-05-13 18:07:53 +010084
Linus Torvalds1da177e2005-04-16 15:20:36 -070085/* This is the ecc value computed by the HW ecc generator upon writing an empty
86 page, one with all 0xff for data. */
87static u_char empty_write_ecc[6] = { 0x4b, 0x00, 0xe2, 0x0e, 0x93, 0xf7 };
88
89#define INFTL_BBT_RESERVED_BLOCKS 4
90
91#define DoC_is_MillenniumPlus(doc) ((doc)->ChipID == DOC_ChipID_DocMilPlus16 || (doc)->ChipID == DOC_ChipID_DocMilPlus32)
92#define DoC_is_Millennium(doc) ((doc)->ChipID == DOC_ChipID_DocMil)
93#define DoC_is_2000(doc) ((doc)->ChipID == DOC_ChipID_Doc2k)
94
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +020095static void doc200x_hwcontrol(struct mtd_info *mtd, int cmd,
96 unsigned int bitmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097static void doc200x_select_chip(struct mtd_info *mtd, int chip);
98
David Woodhousee0c7d762006-05-13 18:07:53 +010099static int debug = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100module_param(debug, int, 0);
101
David Woodhousee0c7d762006-05-13 18:07:53 +0100102static int try_dword = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103module_param(try_dword, int, 0);
104
David Woodhousee0c7d762006-05-13 18:07:53 +0100105static int no_ecc_failures = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106module_param(no_ecc_failures, int, 0);
107
David Woodhousee0c7d762006-05-13 18:07:53 +0100108static int no_autopart = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109module_param(no_autopart, int, 0);
Dan Brown1a78ff62005-03-29 21:57:48 +0100110
David Woodhousee0c7d762006-05-13 18:07:53 +0100111static int show_firmware_partition = 0;
Dan Brown1a78ff62005-03-29 21:57:48 +0100112module_param(show_firmware_partition, int, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Robert P. J. Day89e2bf62007-03-07 18:33:25 -0500114#ifdef CONFIG_MTD_NAND_DISKONCHIP_BBTWRITE
David Woodhousee0c7d762006-05-13 18:07:53 +0100115static int inftl_bbt_write = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116#else
David Woodhousee0c7d762006-05-13 18:07:53 +0100117static int inftl_bbt_write = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118#endif
119module_param(inftl_bbt_write, int, 0);
120
Thomas Gleixner651078b2005-01-31 20:36:46 +0000121static unsigned long doc_config_location = CONFIG_MTD_NAND_DISKONCHIP_PROBE_ADDRESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122module_param(doc_config_location, ulong, 0);
123MODULE_PARM_DESC(doc_config_location, "Physical memory address at which to probe for DiskOnChip");
124
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125/* Sector size for HW ECC */
126#define SECTOR_SIZE 512
127/* The sector bytes are packed into NB_DATA 10 bit words */
128#define NB_DATA (((SECTOR_SIZE + 1) * 8 + 6) / 10)
129/* Number of roots */
130#define NROOTS 4
131/* First consective root */
132#define FCR 510
133/* Number of symbols */
134#define NN 1023
135
136/* the Reed Solomon control structure */
137static struct rs_control *rs_decoder;
138
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000139/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 * The HW decoder in the DoC ASIC's provides us a error syndrome,
141 * which we must convert to a standard syndrom usable by the generic
142 * Reed-Solomon library code.
143 *
144 * Fabrice Bellard figured this out in the old docecc code. I added
145 * some comments, improved a minor bit and converted it to make use
146 * of the generic Reed-Solomon libary. tglx
147 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100148static int doc_ecc_decode(struct rs_control *rs, uint8_t *data, uint8_t *ecc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149{
150 int i, j, nerr, errpos[8];
151 uint8_t parity;
152 uint16_t ds[4], s[5], tmp, errval[8], syn[4];
153
154 /* Convert the ecc bytes into words */
155 ds[0] = ((ecc[4] & 0xff) >> 0) | ((ecc[5] & 0x03) << 8);
156 ds[1] = ((ecc[5] & 0xfc) >> 2) | ((ecc[2] & 0x0f) << 6);
157 ds[2] = ((ecc[2] & 0xf0) >> 4) | ((ecc[3] & 0x3f) << 4);
158 ds[3] = ((ecc[3] & 0xc0) >> 6) | ((ecc[0] & 0xff) << 2);
159 parity = ecc[1];
160
161 /* Initialize the syndrom buffer */
162 for (i = 0; i < NROOTS; i++)
163 s[i] = ds[0];
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000164 /*
165 * Evaluate
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 * s[i] = ds[3]x^3 + ds[2]x^2 + ds[1]x^1 + ds[0]
167 * where x = alpha^(FCR + i)
168 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100169 for (j = 1; j < NROOTS; j++) {
170 if (ds[j] == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 continue;
172 tmp = rs->index_of[ds[j]];
David Woodhousee0c7d762006-05-13 18:07:53 +0100173 for (i = 0; i < NROOTS; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 s[i] ^= rs->alpha_to[rs_modnn(rs, tmp + (FCR + i) * j)];
175 }
176
177 /* Calc s[i] = s[i] / alpha^(v + i) */
178 for (i = 0; i < NROOTS; i++) {
179 if (syn[i])
David Woodhousee0c7d762006-05-13 18:07:53 +0100180 syn[i] = rs_modnn(rs, rs->index_of[s[i]] + (NN - FCR - i));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 }
182 /* Call the decoder library */
183 nerr = decode_rs16(rs, NULL, NULL, 1019, syn, 0, errpos, 0, errval);
184
185 /* Incorrectable errors ? */
186 if (nerr < 0)
187 return nerr;
188
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000189 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 * Correct the errors. The bitpositions are a bit of magic,
191 * but they are given by the design of the de/encoder circuit
192 * in the DoC ASIC's.
193 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100194 for (i = 0; i < nerr; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 int index, bitpos, pos = 1015 - errpos[i];
196 uint8_t val;
197 if (pos >= NB_DATA && pos < 1019)
198 continue;
199 if (pos < NB_DATA) {
200 /* extract bit position (MSB first) */
201 pos = 10 * (NB_DATA - 1 - pos) - 6;
202 /* now correct the following 10 bits. At most two bytes
203 can be modified since pos is even */
204 index = (pos >> 3) ^ 1;
205 bitpos = pos & 7;
David Woodhousee0c7d762006-05-13 18:07:53 +0100206 if ((index >= 0 && index < SECTOR_SIZE) || index == (SECTOR_SIZE + 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 val = (uint8_t) (errval[i] >> (2 + bitpos));
208 parity ^= val;
209 if (index < SECTOR_SIZE)
210 data[index] ^= val;
211 }
212 index = ((pos >> 3) + 1) ^ 1;
213 bitpos = (bitpos + 10) & 7;
214 if (bitpos == 0)
215 bitpos = 8;
David Woodhousee0c7d762006-05-13 18:07:53 +0100216 if ((index >= 0 && index < SECTOR_SIZE) || index == (SECTOR_SIZE + 1)) {
217 val = (uint8_t) (errval[i] << (8 - bitpos));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 parity ^= val;
219 if (index < SECTOR_SIZE)
220 data[index] ^= val;
221 }
222 }
223 }
224 /* If the parity is wrong, no rescue possible */
225 return parity ? -1 : nerr;
226}
227
228static void DoC_Delay(struct doc_priv *doc, unsigned short cycles)
229{
230 volatile char dummy;
231 int i;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000232
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 for (i = 0; i < cycles; i++) {
234 if (DoC_is_Millennium(doc))
235 dummy = ReadDOC(doc->virtadr, NOP);
236 else if (DoC_is_MillenniumPlus(doc))
237 dummy = ReadDOC(doc->virtadr, Mplus_NOP);
238 else
239 dummy = ReadDOC(doc->virtadr, DOCStatus);
240 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000241
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242}
243
244#define CDSN_CTRL_FR_B_MASK (CDSN_CTRL_FR_B0 | CDSN_CTRL_FR_B1)
245
246/* DOC_WaitReady: Wait for RDY line to be asserted by the flash chip */
247static int _DoC_WaitReady(struct doc_priv *doc)
248{
David Woodhousee0c7d762006-05-13 18:07:53 +0100249 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 unsigned long timeo = jiffies + (HZ * 10);
251
David Woodhousee0c7d762006-05-13 18:07:53 +0100252 if (debug)
253 printk("_DoC_WaitReady...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 /* Out-of-line routine to wait for chip response */
255 if (DoC_is_MillenniumPlus(doc)) {
256 while ((ReadDOC(docptr, Mplus_FlashControl) & CDSN_CTRL_FR_B_MASK) != CDSN_CTRL_FR_B_MASK) {
257 if (time_after(jiffies, timeo)) {
258 printk("_DoC_WaitReady timed out.\n");
259 return -EIO;
260 }
261 udelay(1);
262 cond_resched();
263 }
264 } else {
265 while (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B)) {
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 }
274
275 return 0;
276}
277
278static inline int DoC_WaitReady(struct doc_priv *doc)
279{
David Woodhousee0c7d762006-05-13 18:07:53 +0100280 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 int ret = 0;
282
283 if (DoC_is_MillenniumPlus(doc)) {
284 DoC_Delay(doc, 4);
285
286 if ((ReadDOC(docptr, Mplus_FlashControl) & CDSN_CTRL_FR_B_MASK) != CDSN_CTRL_FR_B_MASK)
287 /* Call the out-of-line routine to wait */
288 ret = _DoC_WaitReady(doc);
289 } else {
290 DoC_Delay(doc, 4);
291
292 if (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B))
293 /* Call the out-of-line routine to wait */
294 ret = _DoC_WaitReady(doc);
295 DoC_Delay(doc, 2);
296 }
297
David Woodhousee0c7d762006-05-13 18:07:53 +0100298 if (debug)
299 printk("DoC_WaitReady OK\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 return ret;
301}
302
303static void doc2000_write_byte(struct mtd_info *mtd, u_char datum)
304{
305 struct nand_chip *this = mtd->priv;
306 struct doc_priv *doc = this->priv;
David Woodhousee0c7d762006-05-13 18:07:53 +0100307 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
David Woodhousee0c7d762006-05-13 18:07:53 +0100309 if (debug)
310 printk("write_byte %02x\n", datum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 WriteDOC(datum, docptr, CDSNSlowIO);
312 WriteDOC(datum, docptr, 2k_CDSN_IO);
313}
314
315static u_char doc2000_read_byte(struct mtd_info *mtd)
316{
317 struct nand_chip *this = mtd->priv;
318 struct doc_priv *doc = this->priv;
David Woodhousee0c7d762006-05-13 18:07:53 +0100319 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 u_char ret;
321
322 ReadDOC(docptr, CDSNSlowIO);
323 DoC_Delay(doc, 2);
324 ret = ReadDOC(docptr, 2k_CDSN_IO);
David Woodhousee0c7d762006-05-13 18:07:53 +0100325 if (debug)
326 printk("read_byte returns %02x\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 return ret;
328}
329
David Woodhousee0c7d762006-05-13 18:07:53 +0100330static void doc2000_writebuf(struct mtd_info *mtd, const u_char *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331{
332 struct nand_chip *this = mtd->priv;
333 struct doc_priv *doc = this->priv;
David Woodhousee0c7d762006-05-13 18:07:53 +0100334 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 int i;
David Woodhousee0c7d762006-05-13 18:07:53 +0100336 if (debug)
337 printk("writebuf of %d bytes: ", len);
338 for (i = 0; i < len; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 WriteDOC_(buf[i], docptr, DoC_2k_CDSN_IO + i);
340 if (debug && i < 16)
341 printk("%02x ", buf[i]);
342 }
David Woodhousee0c7d762006-05-13 18:07:53 +0100343 if (debug)
344 printk("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345}
346
David Woodhousee0c7d762006-05-13 18:07:53 +0100347static void doc2000_readbuf(struct mtd_info *mtd, u_char *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348{
349 struct nand_chip *this = mtd->priv;
350 struct doc_priv *doc = this->priv;
David Woodhousee0c7d762006-05-13 18:07:53 +0100351 void __iomem *docptr = doc->virtadr;
352 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
David Woodhousee0c7d762006-05-13 18:07:53 +0100354 if (debug)
355 printk("readbuf of %d bytes: ", len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
David Woodhousee0c7d762006-05-13 18:07:53 +0100357 for (i = 0; i < len; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 buf[i] = ReadDOC(docptr, 2k_CDSN_IO + i);
359 }
360}
361
David Woodhousee0c7d762006-05-13 18:07:53 +0100362static void doc2000_readbuf_dword(struct mtd_info *mtd, u_char *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363{
364 struct nand_chip *this = mtd->priv;
365 struct doc_priv *doc = this->priv;
David Woodhousee0c7d762006-05-13 18:07:53 +0100366 void __iomem *docptr = doc->virtadr;
367 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
David Woodhousee0c7d762006-05-13 18:07:53 +0100369 if (debug)
370 printk("readbuf_dword of %d bytes: ", len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
David Woodhousee0c7d762006-05-13 18:07:53 +0100372 if (unlikely((((unsigned long)buf) | len) & 3)) {
373 for (i = 0; i < len; i++) {
374 *(uint8_t *) (&buf[i]) = ReadDOC(docptr, 2k_CDSN_IO + i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 }
376 } else {
David Woodhousee0c7d762006-05-13 18:07:53 +0100377 for (i = 0; i < len; i += 4) {
378 *(uint32_t *) (&buf[i]) = readl(docptr + DoC_2k_CDSN_IO + i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 }
380 }
381}
382
David Woodhousee0c7d762006-05-13 18:07:53 +0100383static int doc2000_verifybuf(struct mtd_info *mtd, const u_char *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384{
385 struct nand_chip *this = mtd->priv;
386 struct doc_priv *doc = this->priv;
David Woodhousee0c7d762006-05-13 18:07:53 +0100387 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 int i;
389
David Woodhousee0c7d762006-05-13 18:07:53 +0100390 for (i = 0; i < len; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 if (buf[i] != ReadDOC(docptr, 2k_CDSN_IO))
392 return -EFAULT;
393 return 0;
394}
395
396static uint16_t __init doc200x_ident_chip(struct mtd_info *mtd, int nr)
397{
398 struct nand_chip *this = mtd->priv;
399 struct doc_priv *doc = this->priv;
400 uint16_t ret;
401
402 doc200x_select_chip(mtd, nr);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200403 doc200x_hwcontrol(mtd, NAND_CMD_READID,
404 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
405 doc200x_hwcontrol(mtd, 0, NAND_CTRL_ALE | NAND_CTRL_CHANGE);
406 doc200x_hwcontrol(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000407
Thomas Gleixnerdfd61292005-02-22 21:48:25 +0000408 /* We cant' use dev_ready here, but at least we wait for the
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000409 * command to complete
Thomas Gleixnerdfd61292005-02-22 21:48:25 +0000410 */
411 udelay(50);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000412
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 ret = this->read_byte(mtd) << 8;
414 ret |= this->read_byte(mtd);
415
416 if (doc->ChipID == DOC_ChipID_Doc2k && try_dword && !nr) {
417 /* First chip probe. See if we get same results by 32-bit access */
418 union {
419 uint32_t dword;
420 uint8_t byte[4];
421 } ident;
422 void __iomem *docptr = doc->virtadr;
423
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200424 doc200x_hwcontrol(mtd, NAND_CMD_READID,
425 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
426 doc200x_hwcontrol(mtd, 0, NAND_CTRL_ALE | NAND_CTRL_CHANGE);
427 doc200x_hwcontrol(mtd, NAND_CMD_NONE,
428 NAND_NCE | NAND_CTRL_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
Thomas Gleixnerdfd61292005-02-22 21:48:25 +0000430 udelay(50);
431
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 ident.dword = readl(docptr + DoC_2k_CDSN_IO);
433 if (((ident.byte[0] << 8) | ident.byte[1]) == ret) {
434 printk(KERN_INFO "DiskOnChip 2000 responds to DWORD access\n");
435 this->read_buf = &doc2000_readbuf_dword;
436 }
437 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000438
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 return ret;
440}
441
442static void __init doc2000_count_chips(struct mtd_info *mtd)
443{
444 struct nand_chip *this = mtd->priv;
445 struct doc_priv *doc = this->priv;
446 uint16_t mfrid;
447 int i;
448
449 /* Max 4 chips per floor on DiskOnChip 2000 */
450 doc->chips_per_floor = 4;
451
452 /* Find out what the first chip is */
453 mfrid = doc200x_ident_chip(mtd, 0);
454
455 /* Find how many chips in each floor. */
456 for (i = 1; i < 4; i++) {
457 if (doc200x_ident_chip(mtd, i) != mfrid)
458 break;
459 }
460 doc->chips_per_floor = i;
461 printk(KERN_DEBUG "Detected %d chips per floor.\n", i);
462}
463
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200464static int doc200x_wait(struct mtd_info *mtd, struct nand_chip *this)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465{
466 struct doc_priv *doc = this->priv;
467
468 int status;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000469
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 DoC_WaitReady(doc);
471 this->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
472 DoC_WaitReady(doc);
473 status = (int)this->read_byte(mtd);
474
475 return status;
476}
477
478static void doc2001_write_byte(struct mtd_info *mtd, u_char datum)
479{
480 struct nand_chip *this = mtd->priv;
481 struct doc_priv *doc = this->priv;
David Woodhousee0c7d762006-05-13 18:07:53 +0100482 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
484 WriteDOC(datum, docptr, CDSNSlowIO);
485 WriteDOC(datum, docptr, Mil_CDSN_IO);
486 WriteDOC(datum, docptr, WritePipeTerm);
487}
488
489static u_char doc2001_read_byte(struct mtd_info *mtd)
490{
491 struct nand_chip *this = mtd->priv;
492 struct doc_priv *doc = this->priv;
David Woodhousee0c7d762006-05-13 18:07:53 +0100493 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
495 //ReadDOC(docptr, CDSNSlowIO);
496 /* 11.4.5 -- delay twice to allow extended length cycle */
497 DoC_Delay(doc, 2);
498 ReadDOC(docptr, ReadPipeInit);
499 //return ReadDOC(docptr, Mil_CDSN_IO);
500 return ReadDOC(docptr, LastDataRead);
501}
502
David Woodhousee0c7d762006-05-13 18:07:53 +0100503static void doc2001_writebuf(struct mtd_info *mtd, const u_char *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504{
505 struct nand_chip *this = mtd->priv;
506 struct doc_priv *doc = this->priv;
David Woodhousee0c7d762006-05-13 18:07:53 +0100507 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 int i;
509
David Woodhousee0c7d762006-05-13 18:07:53 +0100510 for (i = 0; i < len; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 WriteDOC_(buf[i], docptr, DoC_Mil_CDSN_IO + i);
512 /* Terminate write pipeline */
513 WriteDOC(0x00, docptr, WritePipeTerm);
514}
515
David Woodhousee0c7d762006-05-13 18:07:53 +0100516static void doc2001_readbuf(struct mtd_info *mtd, u_char *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517{
518 struct nand_chip *this = mtd->priv;
519 struct doc_priv *doc = this->priv;
David Woodhousee0c7d762006-05-13 18:07:53 +0100520 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 int i;
522
523 /* Start read pipeline */
524 ReadDOC(docptr, ReadPipeInit);
525
David Woodhousee0c7d762006-05-13 18:07:53 +0100526 for (i = 0; i < len - 1; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 buf[i] = ReadDOC(docptr, Mil_CDSN_IO + (i & 0xff));
528
529 /* Terminate read pipeline */
530 buf[i] = ReadDOC(docptr, LastDataRead);
531}
532
David Woodhousee0c7d762006-05-13 18:07:53 +0100533static int doc2001_verifybuf(struct mtd_info *mtd, const u_char *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534{
535 struct nand_chip *this = mtd->priv;
536 struct doc_priv *doc = this->priv;
David Woodhousee0c7d762006-05-13 18:07:53 +0100537 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 int i;
539
540 /* Start read pipeline */
541 ReadDOC(docptr, ReadPipeInit);
542
David Woodhousee0c7d762006-05-13 18:07:53 +0100543 for (i = 0; i < len - 1; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 if (buf[i] != ReadDOC(docptr, Mil_CDSN_IO)) {
545 ReadDOC(docptr, LastDataRead);
546 return i;
547 }
548 if (buf[i] != ReadDOC(docptr, LastDataRead))
549 return i;
550 return 0;
551}
552
553static u_char doc2001plus_read_byte(struct mtd_info *mtd)
554{
555 struct nand_chip *this = mtd->priv;
556 struct doc_priv *doc = this->priv;
David Woodhousee0c7d762006-05-13 18:07:53 +0100557 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 u_char ret;
559
David Woodhousee0c7d762006-05-13 18:07:53 +0100560 ReadDOC(docptr, Mplus_ReadPipeInit);
561 ReadDOC(docptr, Mplus_ReadPipeInit);
562 ret = ReadDOC(docptr, Mplus_LastDataRead);
563 if (debug)
564 printk("read_byte returns %02x\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 return ret;
566}
567
David Woodhousee0c7d762006-05-13 18:07:53 +0100568static void doc2001plus_writebuf(struct mtd_info *mtd, const u_char *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569{
570 struct nand_chip *this = mtd->priv;
571 struct doc_priv *doc = this->priv;
David Woodhousee0c7d762006-05-13 18:07:53 +0100572 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 int i;
574
David Woodhousee0c7d762006-05-13 18:07:53 +0100575 if (debug)
576 printk("writebuf of %d bytes: ", len);
577 for (i = 0; i < len; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 WriteDOC_(buf[i], docptr, DoC_Mil_CDSN_IO + i);
579 if (debug && i < 16)
580 printk("%02x ", buf[i]);
581 }
David Woodhousee0c7d762006-05-13 18:07:53 +0100582 if (debug)
583 printk("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584}
585
David Woodhousee0c7d762006-05-13 18:07:53 +0100586static void doc2001plus_readbuf(struct mtd_info *mtd, u_char *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587{
588 struct nand_chip *this = mtd->priv;
589 struct doc_priv *doc = this->priv;
David Woodhousee0c7d762006-05-13 18:07:53 +0100590 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 int i;
592
David Woodhousee0c7d762006-05-13 18:07:53 +0100593 if (debug)
594 printk("readbuf of %d bytes: ", len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
596 /* Start read pipeline */
597 ReadDOC(docptr, Mplus_ReadPipeInit);
598 ReadDOC(docptr, Mplus_ReadPipeInit);
599
David Woodhousee0c7d762006-05-13 18:07:53 +0100600 for (i = 0; i < len - 2; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 buf[i] = ReadDOC(docptr, Mil_CDSN_IO);
602 if (debug && i < 16)
603 printk("%02x ", buf[i]);
604 }
605
606 /* Terminate read pipeline */
David Woodhousee0c7d762006-05-13 18:07:53 +0100607 buf[len - 2] = ReadDOC(docptr, Mplus_LastDataRead);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 if (debug && i < 16)
David Woodhousee0c7d762006-05-13 18:07:53 +0100609 printk("%02x ", buf[len - 2]);
610 buf[len - 1] = ReadDOC(docptr, Mplus_LastDataRead);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 if (debug && i < 16)
David Woodhousee0c7d762006-05-13 18:07:53 +0100612 printk("%02x ", buf[len - 1]);
613 if (debug)
614 printk("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615}
616
David Woodhousee0c7d762006-05-13 18:07:53 +0100617static int doc2001plus_verifybuf(struct mtd_info *mtd, const u_char *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618{
619 struct nand_chip *this = mtd->priv;
620 struct doc_priv *doc = this->priv;
David Woodhousee0c7d762006-05-13 18:07:53 +0100621 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 int i;
623
David Woodhousee0c7d762006-05-13 18:07:53 +0100624 if (debug)
625 printk("verifybuf of %d bytes: ", len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
627 /* Start read pipeline */
628 ReadDOC(docptr, Mplus_ReadPipeInit);
629 ReadDOC(docptr, Mplus_ReadPipeInit);
630
David Woodhousee0c7d762006-05-13 18:07:53 +0100631 for (i = 0; i < len - 2; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 if (buf[i] != ReadDOC(docptr, Mil_CDSN_IO)) {
633 ReadDOC(docptr, Mplus_LastDataRead);
634 ReadDOC(docptr, Mplus_LastDataRead);
635 return i;
636 }
David Woodhousee0c7d762006-05-13 18:07:53 +0100637 if (buf[len - 2] != ReadDOC(docptr, Mplus_LastDataRead))
638 return len - 2;
639 if (buf[len - 1] != ReadDOC(docptr, Mplus_LastDataRead))
640 return len - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 return 0;
642}
643
644static void doc2001plus_select_chip(struct mtd_info *mtd, int chip)
645{
646 struct nand_chip *this = mtd->priv;
647 struct doc_priv *doc = this->priv;
David Woodhousee0c7d762006-05-13 18:07:53 +0100648 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 int floor = 0;
650
David Woodhousee0c7d762006-05-13 18:07:53 +0100651 if (debug)
652 printk("select chip (%d)\n", chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
654 if (chip == -1) {
655 /* Disable flash internally */
656 WriteDOC(0, docptr, Mplus_FlashSelect);
657 return;
658 }
659
660 floor = chip / doc->chips_per_floor;
David Woodhousee0c7d762006-05-13 18:07:53 +0100661 chip -= (floor * doc->chips_per_floor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
663 /* Assert ChipEnable and deassert WriteProtect */
664 WriteDOC((DOC_FLASH_CE), docptr, Mplus_FlashSelect);
665 this->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
666
667 doc->curchip = chip;
668 doc->curfloor = floor;
669}
670
671static void doc200x_select_chip(struct mtd_info *mtd, int chip)
672{
673 struct nand_chip *this = mtd->priv;
674 struct doc_priv *doc = this->priv;
David Woodhousee0c7d762006-05-13 18:07:53 +0100675 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 int floor = 0;
677
David Woodhousee0c7d762006-05-13 18:07:53 +0100678 if (debug)
679 printk("select chip (%d)\n", chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
681 if (chip == -1)
682 return;
683
684 floor = chip / doc->chips_per_floor;
David Woodhousee0c7d762006-05-13 18:07:53 +0100685 chip -= (floor * doc->chips_per_floor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
687 /* 11.4.4 -- deassert CE before changing chip */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200688 doc200x_hwcontrol(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
690 WriteDOC(floor, docptr, FloorSelect);
691 WriteDOC(chip, docptr, CDSNDeviceSelect);
692
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200693 doc200x_hwcontrol(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
695 doc->curchip = chip;
696 doc->curfloor = floor;
697}
698
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200699#define CDSN_CTRL_MSK (CDSN_CTRL_CE | CDSN_CTRL_CLE | CDSN_CTRL_ALE)
700
701static void doc200x_hwcontrol(struct mtd_info *mtd, int cmd,
702 unsigned int ctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703{
704 struct nand_chip *this = mtd->priv;
705 struct doc_priv *doc = this->priv;
David Woodhousee0c7d762006-05-13 18:07:53 +0100706 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200708 if (ctrl & NAND_CTRL_CHANGE) {
709 doc->CDSNControl &= ~CDSN_CTRL_MSK;
710 doc->CDSNControl |= ctrl & CDSN_CTRL_MSK;
711 if (debug)
712 printk("hwcontrol(%d): %02x\n", cmd, doc->CDSNControl);
713 WriteDOC(doc->CDSNControl, docptr, CDSNControl);
714 /* 11.4.3 -- 4 NOPs after CSDNControl write */
715 DoC_Delay(doc, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 }
Thomas Gleixnercad74f22006-05-23 23:28:48 +0200717 if (cmd != NAND_CMD_NONE) {
718 if (DoC_is_2000(doc))
719 doc2000_write_byte(mtd, cmd);
720 else
721 doc2001_write_byte(mtd, cmd);
722 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723}
724
David Woodhousee0c7d762006-05-13 18:07:53 +0100725static void doc2001plus_command(struct mtd_info *mtd, unsigned command, int column, int page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726{
727 struct nand_chip *this = mtd->priv;
728 struct doc_priv *doc = this->priv;
David Woodhousee0c7d762006-05-13 18:07:53 +0100729 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
731 /*
732 * Must terminate write pipeline before sending any commands
733 * to the device.
734 */
735 if (command == NAND_CMD_PAGEPROG) {
736 WriteDOC(0x00, docptr, Mplus_WritePipeTerm);
737 WriteDOC(0x00, docptr, Mplus_WritePipeTerm);
738 }
739
740 /*
741 * Write out the command to the device.
742 */
743 if (command == NAND_CMD_SEQIN) {
744 int readcmd;
745
Joern Engel28318772006-05-22 23:18:05 +0200746 if (column >= mtd->writesize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 /* OOB area */
Joern Engel28318772006-05-22 23:18:05 +0200748 column -= mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 readcmd = NAND_CMD_READOOB;
750 } else if (column < 256) {
751 /* First 256 bytes --> READ0 */
752 readcmd = NAND_CMD_READ0;
753 } else {
754 column -= 256;
755 readcmd = NAND_CMD_READ1;
756 }
757 WriteDOC(readcmd, docptr, Mplus_FlashCmd);
758 }
759 WriteDOC(command, docptr, Mplus_FlashCmd);
760 WriteDOC(0, docptr, Mplus_WritePipeTerm);
761 WriteDOC(0, docptr, Mplus_WritePipeTerm);
762
763 if (column != -1 || page_addr != -1) {
764 /* Serially input address */
765 if (column != -1) {
766 /* Adjust columns for 16 bit buswidth */
767 if (this->options & NAND_BUSWIDTH_16)
768 column >>= 1;
769 WriteDOC(column, docptr, Mplus_FlashAddress);
770 }
771 if (page_addr != -1) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100772 WriteDOC((unsigned char)(page_addr & 0xff), docptr, Mplus_FlashAddress);
773 WriteDOC((unsigned char)((page_addr >> 8) & 0xff), docptr, Mplus_FlashAddress);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 /* One more address cycle for higher density devices */
775 if (this->chipsize & 0x0c000000) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100776 WriteDOC((unsigned char)((page_addr >> 16) & 0x0f), docptr, Mplus_FlashAddress);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 printk("high density\n");
778 }
779 }
780 WriteDOC(0, docptr, Mplus_WritePipeTerm);
781 WriteDOC(0, docptr, Mplus_WritePipeTerm);
782 /* deassert ALE */
David Woodhousee0c7d762006-05-13 18:07:53 +0100783 if (command == NAND_CMD_READ0 || command == NAND_CMD_READ1 ||
784 command == NAND_CMD_READOOB || command == NAND_CMD_READID)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 WriteDOC(0, docptr, Mplus_FlashControl);
786 }
787
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000788 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 * program and erase have their own busy handlers
790 * status and sequential in needs no delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100791 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 switch (command) {
793
794 case NAND_CMD_PAGEPROG:
795 case NAND_CMD_ERASE1:
796 case NAND_CMD_ERASE2:
797 case NAND_CMD_SEQIN:
798 case NAND_CMD_STATUS:
799 return;
800
801 case NAND_CMD_RESET:
802 if (this->dev_ready)
803 break;
804 udelay(this->chip_delay);
805 WriteDOC(NAND_CMD_STATUS, docptr, Mplus_FlashCmd);
806 WriteDOC(0, docptr, Mplus_WritePipeTerm);
807 WriteDOC(0, docptr, Mplus_WritePipeTerm);
David Woodhousee0c7d762006-05-13 18:07:53 +0100808 while (!(this->read_byte(mtd) & 0x40)) ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 return;
810
David Woodhousee0c7d762006-05-13 18:07:53 +0100811 /* This applies to read commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000813 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 * If we don't have access to the busy pin, we apply the given
815 * command delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100816 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 if (!this->dev_ready) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100818 udelay(this->chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 return;
820 }
821 }
822
823 /* Apply this short delay always to ensure that we do wait tWB in
824 * any case on any machine. */
David Woodhousee0c7d762006-05-13 18:07:53 +0100825 ndelay(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 /* wait until command is processed */
David Woodhousee0c7d762006-05-13 18:07:53 +0100827 while (!this->dev_ready(mtd)) ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828}
829
830static int doc200x_dev_ready(struct mtd_info *mtd)
831{
832 struct nand_chip *this = mtd->priv;
833 struct doc_priv *doc = this->priv;
David Woodhousee0c7d762006-05-13 18:07:53 +0100834 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
836 if (DoC_is_MillenniumPlus(doc)) {
837 /* 11.4.2 -- must NOP four times before checking FR/B# */
838 DoC_Delay(doc, 4);
839 if ((ReadDOC(docptr, Mplus_FlashControl) & CDSN_CTRL_FR_B_MASK) != CDSN_CTRL_FR_B_MASK) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100840 if (debug)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 printk("not ready\n");
842 return 0;
843 }
David Woodhousee0c7d762006-05-13 18:07:53 +0100844 if (debug)
845 printk("was ready\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 return 1;
847 } else {
848 /* 11.4.2 -- must NOP four times before checking FR/B# */
849 DoC_Delay(doc, 4);
850 if (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B)) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100851 if (debug)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 printk("not ready\n");
853 return 0;
854 }
855 /* 11.4.2 -- Must NOP twice if it's ready */
856 DoC_Delay(doc, 2);
David Woodhousee0c7d762006-05-13 18:07:53 +0100857 if (debug)
858 printk("was ready\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 return 1;
860 }
861}
862
863static int doc200x_block_bad(struct mtd_info *mtd, loff_t ofs, int getchip)
864{
865 /* This is our last resort if we couldn't find or create a BBT. Just
866 pretend all blocks are good. */
867 return 0;
868}
869
870static void doc200x_enable_hwecc(struct mtd_info *mtd, int mode)
871{
872 struct nand_chip *this = mtd->priv;
873 struct doc_priv *doc = this->priv;
David Woodhousee0c7d762006-05-13 18:07:53 +0100874 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
876 /* Prime the ECC engine */
David Woodhousee0c7d762006-05-13 18:07:53 +0100877 switch (mode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 case NAND_ECC_READ:
879 WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
880 WriteDOC(DOC_ECC_EN, docptr, ECCConf);
881 break;
882 case NAND_ECC_WRITE:
883 WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
884 WriteDOC(DOC_ECC_EN | DOC_ECC_RW, docptr, ECCConf);
885 break;
886 }
887}
888
889static void doc2001plus_enable_hwecc(struct mtd_info *mtd, int mode)
890{
891 struct nand_chip *this = mtd->priv;
892 struct doc_priv *doc = this->priv;
David Woodhousee0c7d762006-05-13 18:07:53 +0100893 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
895 /* Prime the ECC engine */
David Woodhousee0c7d762006-05-13 18:07:53 +0100896 switch (mode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 case NAND_ECC_READ:
898 WriteDOC(DOC_ECC_RESET, docptr, Mplus_ECCConf);
899 WriteDOC(DOC_ECC_EN, docptr, Mplus_ECCConf);
900 break;
901 case NAND_ECC_WRITE:
902 WriteDOC(DOC_ECC_RESET, docptr, Mplus_ECCConf);
903 WriteDOC(DOC_ECC_EN | DOC_ECC_RW, docptr, Mplus_ECCConf);
904 break;
905 }
906}
907
908/* This code is only called on write */
David Woodhousee0c7d762006-05-13 18:07:53 +0100909static int doc200x_calculate_ecc(struct mtd_info *mtd, const u_char *dat, unsigned char *ecc_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910{
911 struct nand_chip *this = mtd->priv;
912 struct doc_priv *doc = this->priv;
David Woodhousee0c7d762006-05-13 18:07:53 +0100913 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 int i;
915 int emptymatch = 1;
916
917 /* flush the pipeline */
918 if (DoC_is_2000(doc)) {
919 WriteDOC(doc->CDSNControl & ~CDSN_CTRL_FLASH_IO, docptr, CDSNControl);
920 WriteDOC(0, docptr, 2k_CDSN_IO);
921 WriteDOC(0, docptr, 2k_CDSN_IO);
922 WriteDOC(0, docptr, 2k_CDSN_IO);
923 WriteDOC(doc->CDSNControl, docptr, CDSNControl);
924 } else if (DoC_is_MillenniumPlus(doc)) {
925 WriteDOC(0, docptr, Mplus_NOP);
926 WriteDOC(0, docptr, Mplus_NOP);
927 WriteDOC(0, docptr, Mplus_NOP);
928 } else {
929 WriteDOC(0, docptr, NOP);
930 WriteDOC(0, docptr, NOP);
931 WriteDOC(0, docptr, NOP);
932 }
933
934 for (i = 0; i < 6; i++) {
935 if (DoC_is_MillenniumPlus(doc))
936 ecc_code[i] = ReadDOC_(docptr, DoC_Mplus_ECCSyndrome0 + i);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000937 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 ecc_code[i] = ReadDOC_(docptr, DoC_ECCSyndrome0 + i);
939 if (ecc_code[i] != empty_write_ecc[i])
940 emptymatch = 0;
941 }
942 if (DoC_is_MillenniumPlus(doc))
943 WriteDOC(DOC_ECC_DIS, docptr, Mplus_ECCConf);
944 else
945 WriteDOC(DOC_ECC_DIS, docptr, ECCConf);
946#if 0
947 /* If emptymatch=1, we might have an all-0xff data buffer. Check. */
948 if (emptymatch) {
949 /* Note: this somewhat expensive test should not be triggered
950 often. It could be optimized away by examining the data in
951 the writebuf routine, and remembering the result. */
952 for (i = 0; i < 512; i++) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100953 if (dat[i] == 0xff)
954 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 emptymatch = 0;
956 break;
957 }
958 }
959 /* If emptymatch still =1, we do have an all-0xff data buffer.
960 Return all-0xff ecc value instead of the computed one, so
961 it'll look just like a freshly-erased page. */
David Woodhousee0c7d762006-05-13 18:07:53 +0100962 if (emptymatch)
963 memset(ecc_code, 0xff, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964#endif
965 return 0;
966}
967
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200968static int doc200x_correct_data(struct mtd_info *mtd, u_char *dat,
969 u_char *read_ecc, u_char *isnull)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970{
971 int i, ret = 0;
972 struct nand_chip *this = mtd->priv;
973 struct doc_priv *doc = this->priv;
David Woodhousee0c7d762006-05-13 18:07:53 +0100974 void __iomem *docptr = doc->virtadr;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200975 uint8_t calc_ecc[6];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 volatile u_char dummy;
977 int emptymatch = 1;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000978
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 /* flush the pipeline */
980 if (DoC_is_2000(doc)) {
981 dummy = ReadDOC(docptr, 2k_ECCStatus);
982 dummy = ReadDOC(docptr, 2k_ECCStatus);
983 dummy = ReadDOC(docptr, 2k_ECCStatus);
984 } else if (DoC_is_MillenniumPlus(doc)) {
985 dummy = ReadDOC(docptr, Mplus_ECCConf);
986 dummy = ReadDOC(docptr, Mplus_ECCConf);
987 dummy = ReadDOC(docptr, Mplus_ECCConf);
988 } else {
989 dummy = ReadDOC(docptr, ECCConf);
990 dummy = ReadDOC(docptr, ECCConf);
991 dummy = ReadDOC(docptr, ECCConf);
992 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000993
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 /* Error occured ? */
995 if (dummy & 0x80) {
996 for (i = 0; i < 6; i++) {
997 if (DoC_is_MillenniumPlus(doc))
998 calc_ecc[i] = ReadDOC_(docptr, DoC_Mplus_ECCSyndrome0 + i);
999 else
1000 calc_ecc[i] = ReadDOC_(docptr, DoC_ECCSyndrome0 + i);
1001 if (calc_ecc[i] != empty_read_syndrome[i])
1002 emptymatch = 0;
1003 }
1004 /* If emptymatch=1, the read syndrome is consistent with an
1005 all-0xff data and stored ecc block. Check the stored ecc. */
1006 if (emptymatch) {
1007 for (i = 0; i < 6; i++) {
David Woodhousee0c7d762006-05-13 18:07:53 +01001008 if (read_ecc[i] == 0xff)
1009 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 emptymatch = 0;
1011 break;
1012 }
1013 }
1014 /* If emptymatch still =1, check the data block. */
1015 if (emptymatch) {
David Woodhousee0c7d762006-05-13 18:07:53 +01001016 /* Note: this somewhat expensive test should not be triggered
1017 often. It could be optimized away by examining the data in
1018 the readbuf routine, and remembering the result. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 for (i = 0; i < 512; i++) {
David Woodhousee0c7d762006-05-13 18:07:53 +01001020 if (dat[i] == 0xff)
1021 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 emptymatch = 0;
1023 break;
1024 }
1025 }
1026 /* If emptymatch still =1, this is almost certainly a freshly-
1027 erased block, in which case the ECC will not come out right.
1028 We'll suppress the error and tell the caller everything's
1029 OK. Because it is. */
David Woodhousee0c7d762006-05-13 18:07:53 +01001030 if (!emptymatch)
1031 ret = doc_ecc_decode(rs_decoder, dat, calc_ecc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 if (ret > 0)
1033 printk(KERN_ERR "doc200x_correct_data corrected %d errors\n", ret);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001034 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 if (DoC_is_MillenniumPlus(doc))
1036 WriteDOC(DOC_ECC_DIS, docptr, Mplus_ECCConf);
1037 else
1038 WriteDOC(DOC_ECC_DIS, docptr, ECCConf);
1039 if (no_ecc_failures && (ret == -1)) {
1040 printk(KERN_ERR "suppressing ECC failure\n");
1041 ret = 0;
1042 }
1043 return ret;
1044}
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001045
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046//u_char mydatabuf[528];
1047
Dan Brownabc37e62005-04-07 15:22:58 +01001048/* The strange out-of-order .oobfree list below is a (possibly unneeded)
1049 * attempt to retain compatibility. It used to read:
1050 * .oobfree = { {8, 8} }
1051 * Since that leaves two bytes unusable, it was changed. But the following
1052 * scheme might affect existing jffs2 installs by moving the cleanmarker:
1053 * .oobfree = { {6, 10} }
1054 * jffs2 seems to handle the above gracefully, but the current scheme seems
1055 * safer. The only problem with it is that any code that parses oobfree must
1056 * be able to handle out-of-order segments.
1057 */
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02001058static struct nand_ecclayout doc200x_oobinfo = {
David Woodhousee0c7d762006-05-13 18:07:53 +01001059 .eccbytes = 6,
1060 .eccpos = {0, 1, 2, 3, 4, 5},
1061 .oobfree = {{8, 8}, {6, 2}}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062};
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001063
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064/* Find the (I)NFTL Media Header, and optionally also the mirror media header.
1065 On sucessful return, buf will contain a copy of the media header for
1066 further processing. id is the string to scan for, and will presumably be
1067 either "ANAND" or "BNAND". If findmirror=1, also look for the mirror media
1068 header. The page #s of the found media headers are placed in mh0_page and
1069 mh1_page in the DOC private structure. */
David Woodhousee0c7d762006-05-13 18:07:53 +01001070static int __init find_media_headers(struct mtd_info *mtd, u_char *buf, const char *id, int findmirror)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071{
1072 struct nand_chip *this = mtd->priv;
1073 struct doc_priv *doc = this->priv;
Dan Brown1a78ff62005-03-29 21:57:48 +01001074 unsigned offs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 int ret;
1076 size_t retlen;
1077
Dan Brown1a78ff62005-03-29 21:57:48 +01001078 for (offs = 0; offs < mtd->size; offs += mtd->erasesize) {
Joern Engel28318772006-05-22 23:18:05 +02001079 ret = mtd->read(mtd, offs, mtd->writesize, &retlen, buf);
1080 if (retlen != mtd->writesize)
David Woodhousee0c7d762006-05-13 18:07:53 +01001081 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 if (ret) {
David Woodhousee0c7d762006-05-13 18:07:53 +01001083 printk(KERN_WARNING "ECC error scanning DOC at 0x%x\n", offs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 }
David Woodhousee0c7d762006-05-13 18:07:53 +01001085 if (memcmp(buf, id, 6))
1086 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 printk(KERN_INFO "Found DiskOnChip %s Media Header at 0x%x\n", id, offs);
1088 if (doc->mh0_page == -1) {
1089 doc->mh0_page = offs >> this->page_shift;
David Woodhousee0c7d762006-05-13 18:07:53 +01001090 if (!findmirror)
1091 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 continue;
1093 }
1094 doc->mh1_page = offs >> this->page_shift;
1095 return 2;
1096 }
1097 if (doc->mh0_page == -1) {
1098 printk(KERN_WARNING "DiskOnChip %s Media Header not found.\n", id);
1099 return 0;
1100 }
1101 /* Only one mediaheader was found. We want buf to contain a
1102 mediaheader on return, so we'll have to re-read the one we found. */
1103 offs = doc->mh0_page << this->page_shift;
Joern Engel28318772006-05-22 23:18:05 +02001104 ret = mtd->read(mtd, offs, mtd->writesize, &retlen, buf);
1105 if (retlen != mtd->writesize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 /* Insanity. Give up. */
1107 printk(KERN_ERR "Read DiskOnChip Media Header once, but can't reread it???\n");
1108 return 0;
1109 }
1110 return 1;
1111}
1112
David Woodhousee0c7d762006-05-13 18:07:53 +01001113static inline int __init nftl_partscan(struct mtd_info *mtd, struct mtd_partition *parts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114{
1115 struct nand_chip *this = mtd->priv;
1116 struct doc_priv *doc = this->priv;
1117 int ret = 0;
1118 u_char *buf;
1119 struct NFTLMediaHeader *mh;
1120 const unsigned psize = 1 << this->page_shift;
Dan Brown1a78ff62005-03-29 21:57:48 +01001121 int numparts = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 unsigned blocks, maxblocks;
1123 int offs, numheaders;
1124
Joern Engel28318772006-05-22 23:18:05 +02001125 buf = kmalloc(mtd->writesize, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 if (!buf) {
1127 printk(KERN_ERR "DiskOnChip mediaheader kmalloc failed!\n");
1128 return 0;
1129 }
David Woodhousee0c7d762006-05-13 18:07:53 +01001130 if (!(numheaders = find_media_headers(mtd, buf, "ANAND", 1)))
1131 goto out;
1132 mh = (struct NFTLMediaHeader *)buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133
Thomas Gleixnerf29a4b82005-01-31 22:22:24 +00001134 mh->NumEraseUnits = le16_to_cpu(mh->NumEraseUnits);
1135 mh->FirstPhysicalEUN = le16_to_cpu(mh->FirstPhysicalEUN);
1136 mh->FormattedSize = le32_to_cpu(mh->FormattedSize);
1137
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 printk(KERN_INFO " DataOrgID = %s\n"
1139 " NumEraseUnits = %d\n"
1140 " FirstPhysicalEUN = %d\n"
1141 " FormattedSize = %d\n"
1142 " UnitSizeFactor = %d\n",
1143 mh->DataOrgID, mh->NumEraseUnits,
1144 mh->FirstPhysicalEUN, mh->FormattedSize,
1145 mh->UnitSizeFactor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146
1147 blocks = mtd->size >> this->phys_erase_shift;
1148 maxblocks = min(32768U, mtd->erasesize - psize);
1149
1150 if (mh->UnitSizeFactor == 0x00) {
1151 /* Auto-determine UnitSizeFactor. The constraints are:
1152 - There can be at most 32768 virtual blocks.
1153 - There can be at most (virtual block size - page size)
David Woodhousee0c7d762006-05-13 18:07:53 +01001154 virtual blocks (because MediaHeader+BBT must fit in 1).
1155 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 mh->UnitSizeFactor = 0xff;
1157 while (blocks > maxblocks) {
1158 blocks >>= 1;
1159 maxblocks = min(32768U, (maxblocks << 1) + psize);
1160 mh->UnitSizeFactor--;
1161 }
1162 printk(KERN_WARNING "UnitSizeFactor=0x00 detected. Correct value is assumed to be 0x%02x.\n", mh->UnitSizeFactor);
1163 }
1164
1165 /* NOTE: The lines below modify internal variables of the NAND and MTD
1166 layers; variables with have already been configured by nand_scan.
1167 Unfortunately, we didn't know before this point what these values
1168 should be. Thus, this code is somewhat dependant on the exact
1169 implementation of the NAND layer. */
1170 if (mh->UnitSizeFactor != 0xff) {
1171 this->bbt_erase_shift += (0xff - mh->UnitSizeFactor);
1172 mtd->erasesize <<= (0xff - mh->UnitSizeFactor);
1173 printk(KERN_INFO "Setting virtual erase size to %d\n", mtd->erasesize);
1174 blocks = mtd->size >> this->bbt_erase_shift;
1175 maxblocks = min(32768U, mtd->erasesize - psize);
1176 }
1177
1178 if (blocks > maxblocks) {
1179 printk(KERN_ERR "UnitSizeFactor of 0x%02x is inconsistent with device size. Aborting.\n", mh->UnitSizeFactor);
1180 goto out;
1181 }
1182
1183 /* Skip past the media headers. */
1184 offs = max(doc->mh0_page, doc->mh1_page);
1185 offs <<= this->page_shift;
1186 offs += mtd->erasesize;
1187
Dan Brown1a78ff62005-03-29 21:57:48 +01001188 if (show_firmware_partition == 1) {
1189 parts[0].name = " DiskOnChip Firmware / Media Header partition";
1190 parts[0].offset = 0;
1191 parts[0].size = offs;
1192 numparts = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 }
Dan Brown1a78ff62005-03-29 21:57:48 +01001194
1195 parts[numparts].name = " DiskOnChip BDTL partition";
1196 parts[numparts].offset = offs;
1197 parts[numparts].size = (mh->NumEraseUnits - numheaders) << this->bbt_erase_shift;
1198
1199 offs += parts[numparts].size;
1200 numparts++;
1201
1202 if (offs < mtd->size) {
1203 parts[numparts].name = " DiskOnChip Remainder partition";
1204 parts[numparts].offset = offs;
1205 parts[numparts].size = mtd->size - offs;
1206 numparts++;
1207 }
1208
1209 ret = numparts;
David Woodhousee0c7d762006-05-13 18:07:53 +01001210 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 kfree(buf);
1212 return ret;
1213}
1214
1215/* This is a stripped-down copy of the code in inftlmount.c */
David Woodhousee0c7d762006-05-13 18:07:53 +01001216static inline int __init inftl_partscan(struct mtd_info *mtd, struct mtd_partition *parts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217{
1218 struct nand_chip *this = mtd->priv;
1219 struct doc_priv *doc = this->priv;
1220 int ret = 0;
1221 u_char *buf;
1222 struct INFTLMediaHeader *mh;
1223 struct INFTLPartition *ip;
1224 int numparts = 0;
1225 int blocks;
1226 int vshift, lastvunit = 0;
1227 int i;
1228 int end = mtd->size;
1229
1230 if (inftl_bbt_write)
1231 end -= (INFTL_BBT_RESERVED_BLOCKS << this->phys_erase_shift);
1232
Joern Engel28318772006-05-22 23:18:05 +02001233 buf = kmalloc(mtd->writesize, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 if (!buf) {
1235 printk(KERN_ERR "DiskOnChip mediaheader kmalloc failed!\n");
1236 return 0;
1237 }
1238
David Woodhousee0c7d762006-05-13 18:07:53 +01001239 if (!find_media_headers(mtd, buf, "BNAND", 0))
1240 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 doc->mh1_page = doc->mh0_page + (4096 >> this->page_shift);
David Woodhousee0c7d762006-05-13 18:07:53 +01001242 mh = (struct INFTLMediaHeader *)buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243
1244 mh->NoOfBootImageBlocks = le32_to_cpu(mh->NoOfBootImageBlocks);
1245 mh->NoOfBinaryPartitions = le32_to_cpu(mh->NoOfBinaryPartitions);
1246 mh->NoOfBDTLPartitions = le32_to_cpu(mh->NoOfBDTLPartitions);
1247 mh->BlockMultiplierBits = le32_to_cpu(mh->BlockMultiplierBits);
1248 mh->FormatFlags = le32_to_cpu(mh->FormatFlags);
1249 mh->PercentUsed = le32_to_cpu(mh->PercentUsed);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001250
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 printk(KERN_INFO " bootRecordID = %s\n"
1252 " NoOfBootImageBlocks = %d\n"
1253 " NoOfBinaryPartitions = %d\n"
1254 " NoOfBDTLPartitions = %d\n"
1255 " BlockMultiplerBits = %d\n"
1256 " FormatFlgs = %d\n"
1257 " OsakVersion = %d.%d.%d.%d\n"
1258 " PercentUsed = %d\n",
1259 mh->bootRecordID, mh->NoOfBootImageBlocks,
1260 mh->NoOfBinaryPartitions,
1261 mh->NoOfBDTLPartitions,
1262 mh->BlockMultiplierBits, mh->FormatFlags,
1263 ((unsigned char *) &mh->OsakVersion)[0] & 0xf,
1264 ((unsigned char *) &mh->OsakVersion)[1] & 0xf,
1265 ((unsigned char *) &mh->OsakVersion)[2] & 0xf,
1266 ((unsigned char *) &mh->OsakVersion)[3] & 0xf,
1267 mh->PercentUsed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
1269 vshift = this->phys_erase_shift + mh->BlockMultiplierBits;
1270
1271 blocks = mtd->size >> vshift;
1272 if (blocks > 32768) {
1273 printk(KERN_ERR "BlockMultiplierBits=%d is inconsistent with device size. Aborting.\n", mh->BlockMultiplierBits);
1274 goto out;
1275 }
1276
1277 blocks = doc->chips_per_floor << (this->chip_shift - this->phys_erase_shift);
1278 if (inftl_bbt_write && (blocks > mtd->erasesize)) {
1279 printk(KERN_ERR "Writeable BBTs spanning more than one erase block are not yet supported. FIX ME!\n");
1280 goto out;
1281 }
1282
1283 /* Scan the partitions */
1284 for (i = 0; (i < 4); i++) {
1285 ip = &(mh->Partitions[i]);
1286 ip->virtualUnits = le32_to_cpu(ip->virtualUnits);
1287 ip->firstUnit = le32_to_cpu(ip->firstUnit);
1288 ip->lastUnit = le32_to_cpu(ip->lastUnit);
1289 ip->flags = le32_to_cpu(ip->flags);
1290 ip->spareUnits = le32_to_cpu(ip->spareUnits);
1291 ip->Reserved0 = le32_to_cpu(ip->Reserved0);
1292
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 printk(KERN_INFO " PARTITION[%d] ->\n"
1294 " virtualUnits = %d\n"
1295 " firstUnit = %d\n"
1296 " lastUnit = %d\n"
1297 " flags = 0x%x\n"
1298 " spareUnits = %d\n",
1299 i, ip->virtualUnits, ip->firstUnit,
1300 ip->lastUnit, ip->flags,
1301 ip->spareUnits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302
Dan Brown1a78ff62005-03-29 21:57:48 +01001303 if ((show_firmware_partition == 1) &&
1304 (i == 0) && (ip->firstUnit > 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 parts[0].name = " DiskOnChip IPL / Media Header partition";
1306 parts[0].offset = 0;
1307 parts[0].size = mtd->erasesize * ip->firstUnit;
1308 numparts = 1;
1309 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310
1311 if (ip->flags & INFTL_BINARY)
1312 parts[numparts].name = " DiskOnChip BDK partition";
1313 else
1314 parts[numparts].name = " DiskOnChip BDTL partition";
1315 parts[numparts].offset = ip->firstUnit << vshift;
1316 parts[numparts].size = (1 + ip->lastUnit - ip->firstUnit) << vshift;
1317 numparts++;
David Woodhousee0c7d762006-05-13 18:07:53 +01001318 if (ip->lastUnit > lastvunit)
1319 lastvunit = ip->lastUnit;
1320 if (ip->flags & INFTL_LAST)
1321 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 }
1323 lastvunit++;
1324 if ((lastvunit << vshift) < end) {
1325 parts[numparts].name = " DiskOnChip Remainder partition";
1326 parts[numparts].offset = lastvunit << vshift;
1327 parts[numparts].size = end - parts[numparts].offset;
1328 numparts++;
1329 }
1330 ret = numparts;
David Woodhousee0c7d762006-05-13 18:07:53 +01001331 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 kfree(buf);
1333 return ret;
1334}
1335
1336static int __init nftl_scan_bbt(struct mtd_info *mtd)
1337{
1338 int ret, numparts;
1339 struct nand_chip *this = mtd->priv;
1340 struct doc_priv *doc = this->priv;
1341 struct mtd_partition parts[2];
1342
David Woodhousee0c7d762006-05-13 18:07:53 +01001343 memset((char *)parts, 0, sizeof(parts));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 /* On NFTL, we have to find the media headers before we can read the
1345 BBTs, since they're stored in the media header eraseblocks. */
1346 numparts = nftl_partscan(mtd, parts);
David Woodhousee0c7d762006-05-13 18:07:53 +01001347 if (!numparts)
1348 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 this->bbt_td->options = NAND_BBT_ABSPAGE | NAND_BBT_8BIT |
1350 NAND_BBT_SAVECONTENT | NAND_BBT_WRITE |
1351 NAND_BBT_VERSION;
1352 this->bbt_td->veroffs = 7;
1353 this->bbt_td->pages[0] = doc->mh0_page + 1;
1354 if (doc->mh1_page != -1) {
1355 this->bbt_md->options = NAND_BBT_ABSPAGE | NAND_BBT_8BIT |
1356 NAND_BBT_SAVECONTENT | NAND_BBT_WRITE |
1357 NAND_BBT_VERSION;
1358 this->bbt_md->veroffs = 7;
1359 this->bbt_md->pages[0] = doc->mh1_page + 1;
1360 } else {
1361 this->bbt_md = NULL;
1362 }
1363
1364 /* It's safe to set bd=NULL below because NAND_BBT_CREATE is not set.
1365 At least as nand_bbt.c is currently written. */
1366 if ((ret = nand_scan_bbt(mtd, NULL)))
1367 return ret;
1368 add_mtd_device(mtd);
1369#ifdef CONFIG_MTD_PARTITIONS
1370 if (!no_autopart)
1371 add_mtd_partitions(mtd, parts, numparts);
1372#endif
1373 return 0;
1374}
1375
1376static int __init inftl_scan_bbt(struct mtd_info *mtd)
1377{
1378 int ret, numparts;
1379 struct nand_chip *this = mtd->priv;
1380 struct doc_priv *doc = this->priv;
1381 struct mtd_partition parts[5];
1382
1383 if (this->numchips > doc->chips_per_floor) {
1384 printk(KERN_ERR "Multi-floor INFTL devices not yet supported.\n");
1385 return -EIO;
1386 }
1387
1388 if (DoC_is_MillenniumPlus(doc)) {
1389 this->bbt_td->options = NAND_BBT_2BIT | NAND_BBT_ABSPAGE;
1390 if (inftl_bbt_write)
1391 this->bbt_td->options |= NAND_BBT_WRITE;
1392 this->bbt_td->pages[0] = 2;
1393 this->bbt_md = NULL;
1394 } else {
David Woodhousee0c7d762006-05-13 18:07:53 +01001395 this->bbt_td->options = NAND_BBT_LASTBLOCK | NAND_BBT_8BIT | NAND_BBT_VERSION;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 if (inftl_bbt_write)
1397 this->bbt_td->options |= NAND_BBT_WRITE;
1398 this->bbt_td->offs = 8;
1399 this->bbt_td->len = 8;
1400 this->bbt_td->veroffs = 7;
1401 this->bbt_td->maxblocks = INFTL_BBT_RESERVED_BLOCKS;
1402 this->bbt_td->reserved_block_code = 0x01;
1403 this->bbt_td->pattern = "MSYS_BBT";
1404
David Woodhousee0c7d762006-05-13 18:07:53 +01001405 this->bbt_md->options = NAND_BBT_LASTBLOCK | NAND_BBT_8BIT | NAND_BBT_VERSION;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 if (inftl_bbt_write)
1407 this->bbt_md->options |= NAND_BBT_WRITE;
1408 this->bbt_md->offs = 8;
1409 this->bbt_md->len = 8;
1410 this->bbt_md->veroffs = 7;
1411 this->bbt_md->maxblocks = INFTL_BBT_RESERVED_BLOCKS;
1412 this->bbt_md->reserved_block_code = 0x01;
1413 this->bbt_md->pattern = "TBB_SYSM";
1414 }
1415
1416 /* It's safe to set bd=NULL below because NAND_BBT_CREATE is not set.
1417 At least as nand_bbt.c is currently written. */
1418 if ((ret = nand_scan_bbt(mtd, NULL)))
1419 return ret;
David Woodhousee0c7d762006-05-13 18:07:53 +01001420 memset((char *)parts, 0, sizeof(parts));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 numparts = inftl_partscan(mtd, parts);
1422 /* At least for now, require the INFTL Media Header. We could probably
1423 do without it for non-INFTL use, since all it gives us is
1424 autopartitioning, but I want to give it more thought. */
David Woodhousee0c7d762006-05-13 18:07:53 +01001425 if (!numparts)
1426 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 add_mtd_device(mtd);
1428#ifdef CONFIG_MTD_PARTITIONS
1429 if (!no_autopart)
1430 add_mtd_partitions(mtd, parts, numparts);
1431#endif
1432 return 0;
1433}
1434
1435static inline int __init doc2000_init(struct mtd_info *mtd)
1436{
1437 struct nand_chip *this = mtd->priv;
1438 struct doc_priv *doc = this->priv;
1439
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 this->read_byte = doc2000_read_byte;
1441 this->write_buf = doc2000_writebuf;
1442 this->read_buf = doc2000_readbuf;
1443 this->verify_buf = doc2000_verifybuf;
1444 this->scan_bbt = nftl_scan_bbt;
1445
1446 doc->CDSNControl = CDSN_CTRL_FLASH_IO | CDSN_CTRL_ECC_IO;
1447 doc2000_count_chips(mtd);
1448 mtd->name = "DiskOnChip 2000 (NFTL Model)";
1449 return (4 * doc->chips_per_floor);
1450}
1451
1452static inline int __init doc2001_init(struct mtd_info *mtd)
1453{
1454 struct nand_chip *this = mtd->priv;
1455 struct doc_priv *doc = this->priv;
1456
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 this->read_byte = doc2001_read_byte;
1458 this->write_buf = doc2001_writebuf;
1459 this->read_buf = doc2001_readbuf;
1460 this->verify_buf = doc2001_verifybuf;
1461
1462 ReadDOC(doc->virtadr, ChipID);
1463 ReadDOC(doc->virtadr, ChipID);
1464 ReadDOC(doc->virtadr, ChipID);
1465 if (ReadDOC(doc->virtadr, ChipID) != DOC_ChipID_DocMil) {
1466 /* It's not a Millennium; it's one of the newer
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001467 DiskOnChip 2000 units with a similar ASIC.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 Treat it like a Millennium, except that it
1469 can have multiple chips. */
1470 doc2000_count_chips(mtd);
1471 mtd->name = "DiskOnChip 2000 (INFTL Model)";
1472 this->scan_bbt = inftl_scan_bbt;
1473 return (4 * doc->chips_per_floor);
1474 } else {
1475 /* Bog-standard Millennium */
1476 doc->chips_per_floor = 1;
1477 mtd->name = "DiskOnChip Millennium";
1478 this->scan_bbt = nftl_scan_bbt;
1479 return 1;
1480 }
1481}
1482
1483static inline int __init doc2001plus_init(struct mtd_info *mtd)
1484{
1485 struct nand_chip *this = mtd->priv;
1486 struct doc_priv *doc = this->priv;
1487
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 this->read_byte = doc2001plus_read_byte;
1489 this->write_buf = doc2001plus_writebuf;
1490 this->read_buf = doc2001plus_readbuf;
1491 this->verify_buf = doc2001plus_verifybuf;
1492 this->scan_bbt = inftl_scan_bbt;
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +02001493 this->cmd_ctrl = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 this->select_chip = doc2001plus_select_chip;
1495 this->cmdfunc = doc2001plus_command;
Thomas Gleixner0cddd6c2006-05-23 15:59:58 +02001496 this->ecc.hwctl = doc2001plus_enable_hwecc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497
1498 doc->chips_per_floor = 1;
1499 mtd->name = "DiskOnChip Millennium Plus";
1500
1501 return 1;
1502}
1503
Arjan van de Ven858119e2006-01-14 13:20:43 -08001504static int __init doc_probe(unsigned long physadr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505{
1506 unsigned char ChipID;
1507 struct mtd_info *mtd;
1508 struct nand_chip *nand;
1509 struct doc_priv *doc;
1510 void __iomem *virtadr;
1511 unsigned char save_control;
1512 unsigned char tmp, tmpb, tmpc;
1513 int reg, len, numchips;
1514 int ret = 0;
1515
1516 virtadr = ioremap(physadr, DOC_IOREMAP_LEN);
1517 if (!virtadr) {
1518 printk(KERN_ERR "Diskonchip ioremap failed: 0x%x bytes at 0x%lx\n", DOC_IOREMAP_LEN, physadr);
1519 return -EIO;
1520 }
1521
1522 /* It's not possible to cleanly detect the DiskOnChip - the
1523 * bootup procedure will put the device into reset mode, and
1524 * it's not possible to talk to it without actually writing
1525 * to the DOCControl register. So we store the current contents
1526 * of the DOCControl register's location, in case we later decide
1527 * that it's not a DiskOnChip, and want to put it back how we
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001528 * found it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 */
1530 save_control = ReadDOC(virtadr, DOCControl);
1531
1532 /* Reset the DiskOnChip ASIC */
David Woodhousee0c7d762006-05-13 18:07:53 +01001533 WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_RESET, virtadr, DOCControl);
1534 WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_RESET, virtadr, DOCControl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535
1536 /* Enable the DiskOnChip ASIC */
David Woodhousee0c7d762006-05-13 18:07:53 +01001537 WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_NORMAL, virtadr, DOCControl);
1538 WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_NORMAL, virtadr, DOCControl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539
1540 ChipID = ReadDOC(virtadr, ChipID);
1541
David Woodhousee0c7d762006-05-13 18:07:53 +01001542 switch (ChipID) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 case DOC_ChipID_Doc2k:
1544 reg = DoC_2k_ECCStatus;
1545 break;
1546 case DOC_ChipID_DocMil:
1547 reg = DoC_ECCConf;
1548 break;
1549 case DOC_ChipID_DocMilPlus16:
1550 case DOC_ChipID_DocMilPlus32:
1551 case 0:
1552 /* Possible Millennium Plus, need to do more checks */
1553 /* Possibly release from power down mode */
1554 for (tmp = 0; (tmp < 4); tmp++)
1555 ReadDOC(virtadr, Mplus_Power);
1556
1557 /* Reset the Millennium Plus ASIC */
David Woodhousee0c7d762006-05-13 18:07:53 +01001558 tmp = DOC_MODE_RESET | DOC_MODE_MDWREN | DOC_MODE_RST_LAT | DOC_MODE_BDECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 WriteDOC(tmp, virtadr, Mplus_DOCControl);
1560 WriteDOC(~tmp, virtadr, Mplus_CtrlConfirm);
1561
1562 mdelay(1);
1563 /* Enable the Millennium Plus ASIC */
David Woodhousee0c7d762006-05-13 18:07:53 +01001564 tmp = DOC_MODE_NORMAL | DOC_MODE_MDWREN | DOC_MODE_RST_LAT | DOC_MODE_BDECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 WriteDOC(tmp, virtadr, Mplus_DOCControl);
1566 WriteDOC(~tmp, virtadr, Mplus_CtrlConfirm);
1567 mdelay(1);
1568
1569 ChipID = ReadDOC(virtadr, ChipID);
1570
1571 switch (ChipID) {
1572 case DOC_ChipID_DocMilPlus16:
1573 reg = DoC_Mplus_Toggle;
1574 break;
1575 case DOC_ChipID_DocMilPlus32:
1576 printk(KERN_ERR "DiskOnChip Millennium Plus 32MB is not supported, ignoring.\n");
1577 default:
1578 ret = -ENODEV;
1579 goto notfound;
1580 }
1581 break;
1582
1583 default:
1584 ret = -ENODEV;
1585 goto notfound;
1586 }
1587 /* Check the TOGGLE bit in the ECC register */
David Woodhousee0c7d762006-05-13 18:07:53 +01001588 tmp = ReadDOC_(virtadr, reg) & DOC_TOGGLE_BIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 tmpb = ReadDOC_(virtadr, reg) & DOC_TOGGLE_BIT;
1590 tmpc = ReadDOC_(virtadr, reg) & DOC_TOGGLE_BIT;
1591 if ((tmp == tmpb) || (tmp != tmpc)) {
1592 printk(KERN_WARNING "Possible DiskOnChip at 0x%lx failed TOGGLE test, dropping.\n", physadr);
1593 ret = -ENODEV;
1594 goto notfound;
1595 }
1596
1597 for (mtd = doclist; mtd; mtd = doc->nextdoc) {
1598 unsigned char oldval;
1599 unsigned char newval;
1600 nand = mtd->priv;
1601 doc = nand->priv;
1602 /* Use the alias resolution register to determine if this is
1603 in fact the same DOC aliased to a new address. If writes
1604 to one chip's alias resolution register change the value on
1605 the other chip, they're the same chip. */
1606 if (ChipID == DOC_ChipID_DocMilPlus16) {
1607 oldval = ReadDOC(doc->virtadr, Mplus_AliasResolution);
1608 newval = ReadDOC(virtadr, Mplus_AliasResolution);
1609 } else {
1610 oldval = ReadDOC(doc->virtadr, AliasResolution);
1611 newval = ReadDOC(virtadr, AliasResolution);
1612 }
1613 if (oldval != newval)
1614 continue;
1615 if (ChipID == DOC_ChipID_DocMilPlus16) {
1616 WriteDOC(~newval, virtadr, Mplus_AliasResolution);
1617 oldval = ReadDOC(doc->virtadr, Mplus_AliasResolution);
David Woodhousee0c7d762006-05-13 18:07:53 +01001618 WriteDOC(newval, virtadr, Mplus_AliasResolution); // restore it
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 } else {
1620 WriteDOC(~newval, virtadr, AliasResolution);
1621 oldval = ReadDOC(doc->virtadr, AliasResolution);
David Woodhousee0c7d762006-05-13 18:07:53 +01001622 WriteDOC(newval, virtadr, AliasResolution); // restore it
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 }
1624 newval = ~newval;
1625 if (oldval == newval) {
1626 printk(KERN_DEBUG "Found alias of DOC at 0x%lx to 0x%lx\n", doc->physadr, physadr);
1627 goto notfound;
1628 }
1629 }
1630
1631 printk(KERN_NOTICE "DiskOnChip found at 0x%lx\n", physadr);
1632
1633 len = sizeof(struct mtd_info) +
David Woodhousee0c7d762006-05-13 18:07:53 +01001634 sizeof(struct nand_chip) + sizeof(struct doc_priv) + (2 * sizeof(struct nand_bbt_descr));
Burman Yan95b93a02006-11-15 21:10:29 +02001635 mtd = kzalloc(len, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 if (!mtd) {
1637 printk(KERN_ERR "DiskOnChip kmalloc (%d bytes) failed!\n", len);
1638 ret = -ENOMEM;
1639 goto fail;
1640 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641
1642 nand = (struct nand_chip *) (mtd + 1);
1643 doc = (struct doc_priv *) (nand + 1);
1644 nand->bbt_td = (struct nand_bbt_descr *) (doc + 1);
1645 nand->bbt_md = nand->bbt_td + 1;
1646
1647 mtd->priv = nand;
1648 mtd->owner = THIS_MODULE;
1649
1650 nand->priv = doc;
1651 nand->select_chip = doc200x_select_chip;
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +02001652 nand->cmd_ctrl = doc200x_hwcontrol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 nand->dev_ready = doc200x_dev_ready;
1654 nand->waitfunc = doc200x_wait;
1655 nand->block_bad = doc200x_block_bad;
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02001656 nand->ecc.hwctl = doc200x_enable_hwecc;
1657 nand->ecc.calculate = doc200x_calculate_ecc;
1658 nand->ecc.correct = doc200x_correct_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02001660 nand->ecc.layout = &doc200x_oobinfo;
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02001661 nand->ecc.mode = NAND_ECC_HW_SYNDROME;
1662 nand->ecc.size = 512;
1663 nand->ecc.bytes = 6;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001664 nand->options = NAND_USE_FLASH_BBT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665
1666 doc->physadr = physadr;
1667 doc->virtadr = virtadr;
1668 doc->ChipID = ChipID;
1669 doc->curfloor = -1;
1670 doc->curchip = -1;
1671 doc->mh0_page = -1;
1672 doc->mh1_page = -1;
1673 doc->nextdoc = doclist;
1674
1675 if (ChipID == DOC_ChipID_Doc2k)
1676 numchips = doc2000_init(mtd);
1677 else if (ChipID == DOC_ChipID_DocMilPlus16)
1678 numchips = doc2001plus_init(mtd);
1679 else
1680 numchips = doc2001_init(mtd);
1681
1682 if ((ret = nand_scan(mtd, numchips))) {
1683 /* DBB note: i believe nand_release is necessary here, as
1684 buffers may have been allocated in nand_base. Check with
1685 Thomas. FIX ME! */
1686 /* nand_release will call del_mtd_device, but we haven't yet
1687 added it. This is handled without incident by
1688 del_mtd_device, as far as I can tell. */
1689 nand_release(mtd);
1690 kfree(mtd);
1691 goto fail;
1692 }
1693
1694 /* Success! */
1695 doclist = mtd;
1696 return 0;
1697
David Woodhousee0c7d762006-05-13 18:07:53 +01001698 notfound:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 /* Put back the contents of the DOCControl register, in case it's not
1700 actually a DiskOnChip. */
1701 WriteDOC(save_control, virtadr, DOCControl);
David Woodhousee0c7d762006-05-13 18:07:53 +01001702 fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 iounmap(virtadr);
1704 return ret;
1705}
1706
1707static void release_nanddoc(void)
1708{
David Woodhousee0c7d762006-05-13 18:07:53 +01001709 struct mtd_info *mtd, *nextmtd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 struct nand_chip *nand;
1711 struct doc_priv *doc;
1712
1713 for (mtd = doclist; mtd; mtd = nextmtd) {
1714 nand = mtd->priv;
1715 doc = nand->priv;
1716
1717 nextmtd = doc->nextdoc;
1718 nand_release(mtd);
1719 iounmap(doc->virtadr);
1720 kfree(mtd);
1721 }
1722}
1723
1724static int __init init_nanddoc(void)
1725{
1726 int i, ret = 0;
1727
1728 /* We could create the decoder on demand, if memory is a concern.
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001729 * This way we have it handy, if an error happens
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 *
1731 * Symbolsize is 10 (bits)
1732 * Primitve polynomial is x^10+x^3+1
1733 * first consecutive root is 510
1734 * primitve element to generate roots = 1
1735 * generator polinomial degree = 4
1736 */
1737 rs_decoder = init_rs(10, 0x409, FCR, 1, NROOTS);
David Woodhousee0c7d762006-05-13 18:07:53 +01001738 if (!rs_decoder) {
1739 printk(KERN_ERR "DiskOnChip: Could not create a RS decoder\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 return -ENOMEM;
1741 }
1742
1743 if (doc_config_location) {
1744 printk(KERN_INFO "Using configured DiskOnChip probe address 0x%lx\n", doc_config_location);
1745 ret = doc_probe(doc_config_location);
1746 if (ret < 0)
1747 goto outerr;
1748 } else {
David Woodhousee0c7d762006-05-13 18:07:53 +01001749 for (i = 0; (doc_locations[i] != 0xffffffff); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 doc_probe(doc_locations[i]);
1751 }
1752 }
1753 /* No banner message any more. Print a message if no DiskOnChip
1754 found, so the user knows we at least tried. */
1755 if (!doclist) {
1756 printk(KERN_INFO "No valid DiskOnChip devices found\n");
1757 ret = -ENODEV;
1758 goto outerr;
1759 }
1760 return 0;
David Woodhousee0c7d762006-05-13 18:07:53 +01001761 outerr:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 free_rs(rs_decoder);
1763 return ret;
1764}
1765
1766static void __exit cleanup_nanddoc(void)
1767{
1768 /* Cleanup the nand/DoC resources */
1769 release_nanddoc();
1770
1771 /* Free the reed solomon resources */
1772 if (rs_decoder) {
1773 free_rs(rs_decoder);
1774 }
1775}
1776
1777module_init(init_nanddoc);
1778module_exit(cleanup_nanddoc);
1779
1780MODULE_LICENSE("GPL");
1781MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
1782MODULE_DESCRIPTION("M-Systems DiskOnChip 2000, Millennium and Millennium Plus device driver\n");