blob: e9dc4d1fd5d6412d18b3e20bfa0dc51c25bd24cd [file] [log] [blame]
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02001/*
Wolfgang Denk932394a2005-08-17 12:55:25 +02002 * drivers/mtd/nand/diskonchip.c
3 *
4 * (C) 2003 Red Hat, Inc.
5 * (C) 2004 Dan Brown <dan_brown@ieee.org>
6 * (C) 2004 Kalev Lember <kalev@smartlink.ee>
7 *
8 * Author: David Woodhouse <dwmw2@infradead.org>
9 * Additional Diskonchip 2000 and Millennium support by Dan Brown <dan_brown@ieee.org>
10 * Diskonchip Millennium Plus support by Kalev Lember <kalev@smartlink.ee>
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +020011 *
Wolfgang Denk932394a2005-08-17 12:55:25 +020012 * Error correction code lifted from the old docecc code
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +020013 * Author: Fabrice Bellard (fabrice.bellard@netgem.com)
Wolfgang Denk932394a2005-08-17 12:55:25 +020014 * Copyright (C) 2000 Netgem S.A.
15 * converted to the generic Reed-Solomon library by Thomas Gleixner <tglx@linutronix.de>
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +020016 *
Wolfgang Denk932394a2005-08-17 12:55:25 +020017 * Interface to generic NAND code for M-Systems DiskOnChip devices
Wolfgang Denk932394a2005-08-17 12:55:25 +020018 */
19
Bartlomiej Sieka038ccac2006-02-24 09:37:22 +010020#include <common.h>
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010021
Jean-Christophe PLAGNIOL-VILLARDcc4a0ce2008-08-13 01:40:43 +020022#if !defined(CONFIG_NAND_LEGACY)
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010023
Wolfgang Denk932394a2005-08-17 12:55:25 +020024#include <linux/kernel.h>
25#include <linux/init.h>
26#include <linux/sched.h>
27#include <linux/delay.h>
28#include <linux/rslib.h>
29#include <linux/moduleparam.h>
30#include <asm/io.h>
31
32#include <linux/mtd/mtd.h>
33#include <linux/mtd/nand.h>
34#include <linux/mtd/doc2000.h>
35#include <linux/mtd/compatmac.h>
36#include <linux/mtd/partitions.h>
37#include <linux/mtd/inftl.h>
38
39/* Where to look for the devices? */
William Juulcfa460a2007-10-31 13:53:06 +010040#ifndef CONFIG_MTD_NAND_DISKONCHIP_PROBE_ADDRESS
41#define CONFIG_MTD_NAND_DISKONCHIP_PROBE_ADDRESS 0
Wolfgang Denk932394a2005-08-17 12:55:25 +020042#endif
43
44static unsigned long __initdata doc_locations[] = {
45#if defined (__alpha__) || defined(__i386__) || defined(__x86_64__)
William Juulcfa460a2007-10-31 13:53:06 +010046#ifdef CONFIG_MTD_NAND_DISKONCHIP_PROBE_HIGH
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +020047 0xfffc8000, 0xfffca000, 0xfffcc000, 0xfffce000,
Wolfgang Denk932394a2005-08-17 12:55:25 +020048 0xfffd0000, 0xfffd2000, 0xfffd4000, 0xfffd6000,
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +020049 0xfffd8000, 0xfffda000, 0xfffdc000, 0xfffde000,
50 0xfffe0000, 0xfffe2000, 0xfffe4000, 0xfffe6000,
Wolfgang Denk932394a2005-08-17 12:55:25 +020051 0xfffe8000, 0xfffea000, 0xfffec000, 0xfffee000,
52#else /* CONFIG_MTD_DOCPROBE_HIGH */
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +020053 0xc8000, 0xca000, 0xcc000, 0xce000,
Wolfgang Denk932394a2005-08-17 12:55:25 +020054 0xd0000, 0xd2000, 0xd4000, 0xd6000,
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +020055 0xd8000, 0xda000, 0xdc000, 0xde000,
56 0xe0000, 0xe2000, 0xe4000, 0xe6000,
Wolfgang Denk932394a2005-08-17 12:55:25 +020057 0xe8000, 0xea000, 0xec000, 0xee000,
58#endif /* CONFIG_MTD_DOCPROBE_HIGH */
William Juulcfa460a2007-10-31 13:53:06 +010059#else
Wolfgang Denk932394a2005-08-17 12:55:25 +020060#warning Unknown architecture for DiskOnChip. No default probe locations defined
61#endif
62 0xffffffff };
63
64static struct mtd_info *doclist = NULL;
65
66struct doc_priv {
67 void __iomem *virtadr;
68 unsigned long physadr;
69 u_char ChipID;
70 u_char CDSNControl;
William Juulcfa460a2007-10-31 13:53:06 +010071 int chips_per_floor; /* The number of chips detected on each floor */
Wolfgang Denk932394a2005-08-17 12:55:25 +020072 int curfloor;
73 int curchip;
74 int mh0_page;
75 int mh1_page;
76 struct mtd_info *nextdoc;
77};
78
Wolfgang Denk932394a2005-08-17 12:55:25 +020079/* This is the syndrome computed by the HW ecc generator upon reading an empty
80 page, one with all 0xff for data and stored ecc code. */
81static u_char empty_read_syndrome[6] = { 0x26, 0xff, 0x6d, 0x47, 0x73, 0x7a };
William Juulcfa460a2007-10-31 13:53:06 +010082
Wolfgang Denk932394a2005-08-17 12:55:25 +020083/* This is the ecc value computed by the HW ecc generator upon writing an empty
84 page, one with all 0xff for data. */
85static u_char empty_write_ecc[6] = { 0x4b, 0x00, 0xe2, 0x0e, 0x93, 0xf7 };
86
87#define INFTL_BBT_RESERVED_BLOCKS 4
88
89#define DoC_is_MillenniumPlus(doc) ((doc)->ChipID == DOC_ChipID_DocMilPlus16 || (doc)->ChipID == DOC_ChipID_DocMilPlus32)
90#define DoC_is_Millennium(doc) ((doc)->ChipID == DOC_ChipID_DocMil)
91#define DoC_is_2000(doc) ((doc)->ChipID == DOC_ChipID_Doc2k)
92
William Juulcfa460a2007-10-31 13:53:06 +010093static void doc200x_hwcontrol(struct mtd_info *mtd, int cmd,
94 unsigned int bitmask);
Wolfgang Denk932394a2005-08-17 12:55:25 +020095static void doc200x_select_chip(struct mtd_info *mtd, int chip);
96
William Juulcfa460a2007-10-31 13:53:06 +010097static int debug = 0;
Wolfgang Denk932394a2005-08-17 12:55:25 +020098module_param(debug, int, 0);
99
William Juulcfa460a2007-10-31 13:53:06 +0100100static int try_dword = 1;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200101module_param(try_dword, int, 0);
102
William Juulcfa460a2007-10-31 13:53:06 +0100103static int no_ecc_failures = 0;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200104module_param(no_ecc_failures, int, 0);
105
William Juulcfa460a2007-10-31 13:53:06 +0100106static int no_autopart = 0;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200107module_param(no_autopart, int, 0);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200108
William Juulcfa460a2007-10-31 13:53:06 +0100109static int show_firmware_partition = 0;
110module_param(show_firmware_partition, int, 0);
111
112#ifdef CONFIG_MTD_NAND_DISKONCHIP_BBTWRITE
113static int inftl_bbt_write = 1;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200114#else
William Juulcfa460a2007-10-31 13:53:06 +0100115static int inftl_bbt_write = 0;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200116#endif
117module_param(inftl_bbt_write, int, 0);
118
William Juulcfa460a2007-10-31 13:53:06 +0100119static unsigned long doc_config_location = CONFIG_MTD_NAND_DISKONCHIP_PROBE_ADDRESS;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200120module_param(doc_config_location, ulong, 0);
121MODULE_PARM_DESC(doc_config_location, "Physical memory address at which to probe for DiskOnChip");
122
Wolfgang Denk932394a2005-08-17 12:55:25 +0200123/* Sector size for HW ECC */
124#define SECTOR_SIZE 512
125/* The sector bytes are packed into NB_DATA 10 bit words */
126#define NB_DATA (((SECTOR_SIZE + 1) * 8 + 6) / 10)
127/* Number of roots */
128#define NROOTS 4
129/* First consective root */
130#define FCR 510
131/* Number of symbols */
132#define NN 1023
133
134/* the Reed Solomon control structure */
135static struct rs_control *rs_decoder;
136
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200137/*
Wolfgang Denk932394a2005-08-17 12:55:25 +0200138 * The HW decoder in the DoC ASIC's provides us a error syndrome,
139 * which we must convert to a standard syndrom usable by the generic
140 * Reed-Solomon library code.
141 *
142 * Fabrice Bellard figured this out in the old docecc code. I added
143 * some comments, improved a minor bit and converted it to make use
144 * of the generic Reed-Solomon libary. tglx
145 */
William Juulcfa460a2007-10-31 13:53:06 +0100146static int doc_ecc_decode(struct rs_control *rs, uint8_t *data, uint8_t *ecc)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200147{
148 int i, j, nerr, errpos[8];
149 uint8_t parity;
150 uint16_t ds[4], s[5], tmp, errval[8], syn[4];
151
152 /* Convert the ecc bytes into words */
153 ds[0] = ((ecc[4] & 0xff) >> 0) | ((ecc[5] & 0x03) << 8);
154 ds[1] = ((ecc[5] & 0xfc) >> 2) | ((ecc[2] & 0x0f) << 6);
155 ds[2] = ((ecc[2] & 0xf0) >> 4) | ((ecc[3] & 0x3f) << 4);
156 ds[3] = ((ecc[3] & 0xc0) >> 6) | ((ecc[0] & 0xff) << 2);
157 parity = ecc[1];
158
159 /* Initialize the syndrom buffer */
160 for (i = 0; i < NROOTS; i++)
161 s[i] = ds[0];
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200162 /*
163 * Evaluate
Wolfgang Denk932394a2005-08-17 12:55:25 +0200164 * s[i] = ds[3]x^3 + ds[2]x^2 + ds[1]x^1 + ds[0]
165 * where x = alpha^(FCR + i)
166 */
William Juulcfa460a2007-10-31 13:53:06 +0100167 for (j = 1; j < NROOTS; j++) {
168 if (ds[j] == 0)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200169 continue;
170 tmp = rs->index_of[ds[j]];
William Juulcfa460a2007-10-31 13:53:06 +0100171 for (i = 0; i < NROOTS; i++)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200172 s[i] ^= rs->alpha_to[rs_modnn(rs, tmp + (FCR + i) * j)];
173 }
174
175 /* Calc s[i] = s[i] / alpha^(v + i) */
176 for (i = 0; i < NROOTS; i++) {
177 if (syn[i])
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200178 syn[i] = rs_modnn(rs, rs->index_of[s[i]] + (NN - FCR - i));
Wolfgang Denk932394a2005-08-17 12:55:25 +0200179 }
180 /* Call the decoder library */
181 nerr = decode_rs16(rs, NULL, NULL, 1019, syn, 0, errpos, 0, errval);
182
183 /* Incorrectable errors ? */
184 if (nerr < 0)
185 return nerr;
186
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200187 /*
Wolfgang Denk932394a2005-08-17 12:55:25 +0200188 * Correct the errors. The bitpositions are a bit of magic,
189 * but they are given by the design of the de/encoder circuit
190 * in the DoC ASIC's.
191 */
William Juulcfa460a2007-10-31 13:53:06 +0100192 for (i = 0; i < nerr; i++) {
Wolfgang Denk932394a2005-08-17 12:55:25 +0200193 int index, bitpos, pos = 1015 - errpos[i];
194 uint8_t val;
195 if (pos >= NB_DATA && pos < 1019)
196 continue;
197 if (pos < NB_DATA) {
198 /* extract bit position (MSB first) */
199 pos = 10 * (NB_DATA - 1 - pos) - 6;
200 /* now correct the following 10 bits. At most two bytes
201 can be modified since pos is even */
202 index = (pos >> 3) ^ 1;
203 bitpos = pos & 7;
William Juulcfa460a2007-10-31 13:53:06 +0100204 if ((index >= 0 && index < SECTOR_SIZE) || index == (SECTOR_SIZE + 1)) {
Wolfgang Denk932394a2005-08-17 12:55:25 +0200205 val = (uint8_t) (errval[i] >> (2 + bitpos));
206 parity ^= val;
207 if (index < SECTOR_SIZE)
208 data[index] ^= val;
209 }
210 index = ((pos >> 3) + 1) ^ 1;
211 bitpos = (bitpos + 10) & 7;
212 if (bitpos == 0)
213 bitpos = 8;
William Juulcfa460a2007-10-31 13:53:06 +0100214 if ((index >= 0 && index < SECTOR_SIZE) || index == (SECTOR_SIZE + 1)) {
215 val = (uint8_t) (errval[i] << (8 - bitpos));
Wolfgang Denk932394a2005-08-17 12:55:25 +0200216 parity ^= val;
217 if (index < SECTOR_SIZE)
218 data[index] ^= val;
219 }
220 }
221 }
222 /* If the parity is wrong, no rescue possible */
Scott Woodc45912d2008-10-24 16:20:43 -0500223 return parity ? -EBADMSG : nerr;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200224}
225
226static void DoC_Delay(struct doc_priv *doc, unsigned short cycles)
227{
228 volatile char dummy;
229 int i;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200230
Wolfgang Denk932394a2005-08-17 12:55:25 +0200231 for (i = 0; i < cycles; i++) {
232 if (DoC_is_Millennium(doc))
233 dummy = ReadDOC(doc->virtadr, NOP);
234 else if (DoC_is_MillenniumPlus(doc))
235 dummy = ReadDOC(doc->virtadr, Mplus_NOP);
236 else
237 dummy = ReadDOC(doc->virtadr, DOCStatus);
238 }
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200239
Wolfgang Denk932394a2005-08-17 12:55:25 +0200240}
241
242#define CDSN_CTRL_FR_B_MASK (CDSN_CTRL_FR_B0 | CDSN_CTRL_FR_B1)
243
244/* DOC_WaitReady: Wait for RDY line to be asserted by the flash chip */
245static int _DoC_WaitReady(struct doc_priv *doc)
246{
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200247 void __iomem *docptr = doc->virtadr;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200248 unsigned long timeo = jiffies + (HZ * 10);
249
William Juulcfa460a2007-10-31 13:53:06 +0100250 if (debug)
251 printk("_DoC_WaitReady...\n");
Wolfgang Denk932394a2005-08-17 12:55:25 +0200252 /* Out-of-line routine to wait for chip response */
253 if (DoC_is_MillenniumPlus(doc)) {
254 while ((ReadDOC(docptr, Mplus_FlashControl) & CDSN_CTRL_FR_B_MASK) != CDSN_CTRL_FR_B_MASK) {
255 if (time_after(jiffies, timeo)) {
256 printk("_DoC_WaitReady timed out.\n");
257 return -EIO;
258 }
259 udelay(1);
260 cond_resched();
261 }
262 } else {
263 while (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B)) {
264 if (time_after(jiffies, timeo)) {
265 printk("_DoC_WaitReady timed out.\n");
266 return -EIO;
267 }
268 udelay(1);
269 cond_resched();
270 }
271 }
272
273 return 0;
274}
275
276static inline int DoC_WaitReady(struct doc_priv *doc)
277{
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200278 void __iomem *docptr = doc->virtadr;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200279 int ret = 0;
280
281 if (DoC_is_MillenniumPlus(doc)) {
282 DoC_Delay(doc, 4);
283
284 if ((ReadDOC(docptr, Mplus_FlashControl) & CDSN_CTRL_FR_B_MASK) != CDSN_CTRL_FR_B_MASK)
285 /* Call the out-of-line routine to wait */
286 ret = _DoC_WaitReady(doc);
287 } else {
288 DoC_Delay(doc, 4);
289
290 if (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B))
291 /* Call the out-of-line routine to wait */
292 ret = _DoC_WaitReady(doc);
293 DoC_Delay(doc, 2);
294 }
295
William Juulcfa460a2007-10-31 13:53:06 +0100296 if (debug)
297 printk("DoC_WaitReady OK\n");
Wolfgang Denk932394a2005-08-17 12:55:25 +0200298 return ret;
299}
300
301static void doc2000_write_byte(struct mtd_info *mtd, u_char datum)
302{
303 struct nand_chip *this = mtd->priv;
304 struct doc_priv *doc = this->priv;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200305 void __iomem *docptr = doc->virtadr;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200306
William Juulcfa460a2007-10-31 13:53:06 +0100307 if (debug)
308 printk("write_byte %02x\n", datum);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200309 WriteDOC(datum, docptr, CDSNSlowIO);
310 WriteDOC(datum, docptr, 2k_CDSN_IO);
311}
312
313static u_char doc2000_read_byte(struct mtd_info *mtd)
314{
315 struct nand_chip *this = mtd->priv;
316 struct doc_priv *doc = this->priv;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200317 void __iomem *docptr = doc->virtadr;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200318 u_char ret;
319
320 ReadDOC(docptr, CDSNSlowIO);
321 DoC_Delay(doc, 2);
322 ret = ReadDOC(docptr, 2k_CDSN_IO);
William Juulcfa460a2007-10-31 13:53:06 +0100323 if (debug)
324 printk("read_byte returns %02x\n", ret);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200325 return ret;
326}
327
William Juulcfa460a2007-10-31 13:53:06 +0100328static void doc2000_writebuf(struct mtd_info *mtd, const u_char *buf, int len)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200329{
330 struct nand_chip *this = mtd->priv;
331 struct doc_priv *doc = this->priv;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200332 void __iomem *docptr = doc->virtadr;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200333 int i;
William Juulcfa460a2007-10-31 13:53:06 +0100334 if (debug)
335 printk("writebuf of %d bytes: ", len);
336 for (i = 0; i < len; i++) {
Wolfgang Denk932394a2005-08-17 12:55:25 +0200337 WriteDOC_(buf[i], docptr, DoC_2k_CDSN_IO + i);
338 if (debug && i < 16)
339 printk("%02x ", buf[i]);
340 }
William Juulcfa460a2007-10-31 13:53:06 +0100341 if (debug)
342 printk("\n");
Wolfgang Denk932394a2005-08-17 12:55:25 +0200343}
344
William Juulcfa460a2007-10-31 13:53:06 +0100345static void doc2000_readbuf(struct mtd_info *mtd, u_char *buf, int len)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200346{
347 struct nand_chip *this = mtd->priv;
348 struct doc_priv *doc = this->priv;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200349 void __iomem *docptr = doc->virtadr;
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200350 int i;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200351
William Juulcfa460a2007-10-31 13:53:06 +0100352 if (debug)
353 printk("readbuf of %d bytes: ", len);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200354
William Juulcfa460a2007-10-31 13:53:06 +0100355 for (i = 0; i < len; i++) {
Wolfgang Denk932394a2005-08-17 12:55:25 +0200356 buf[i] = ReadDOC(docptr, 2k_CDSN_IO + i);
357 }
358}
359
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200360static void doc2000_readbuf_dword(struct mtd_info *mtd,
Wolfgang Denk932394a2005-08-17 12:55:25 +0200361 u_char *buf, int len)
362{
363 struct nand_chip *this = mtd->priv;
364 struct doc_priv *doc = this->priv;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200365 void __iomem *docptr = doc->virtadr;
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200366 int i;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200367
William Juulcfa460a2007-10-31 13:53:06 +0100368 if (debug)
369 printk("readbuf_dword of %d bytes: ", len);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200370
William Juulcfa460a2007-10-31 13:53:06 +0100371 if (unlikely((((unsigned long)buf) | len) & 3)) {
372 for (i = 0; i < len; i++) {
373 *(uint8_t *) (&buf[i]) = ReadDOC(docptr, 2k_CDSN_IO + i);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200374 }
375 } else {
William Juulcfa460a2007-10-31 13:53:06 +0100376 for (i = 0; i < len; i += 4) {
377 *(uint32_t*) (&buf[i]) = readl(docptr + DoC_2k_CDSN_IO + i);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200378 }
379 }
380}
381
William Juulcfa460a2007-10-31 13:53:06 +0100382static int doc2000_verifybuf(struct mtd_info *mtd, const u_char *buf, int len)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200383{
384 struct nand_chip *this = mtd->priv;
385 struct doc_priv *doc = this->priv;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200386 void __iomem *docptr = doc->virtadr;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200387 int i;
388
William Juulcfa460a2007-10-31 13:53:06 +0100389 for (i = 0; i < len; i++)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200390 if (buf[i] != ReadDOC(docptr, 2k_CDSN_IO))
391 return -EFAULT;
392 return 0;
393}
394
395static uint16_t __init doc200x_ident_chip(struct mtd_info *mtd, int nr)
396{
397 struct nand_chip *this = mtd->priv;
398 struct doc_priv *doc = this->priv;
399 uint16_t ret;
400
401 doc200x_select_chip(mtd, nr);
William Juulcfa460a2007-10-31 13:53:06 +0100402 doc200x_hwcontrol(mtd, NAND_CMD_READID,
403 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
404 doc200x_hwcontrol(mtd, 0, NAND_CTRL_ALE | NAND_CTRL_CHANGE);
405 doc200x_hwcontrol(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
406
407 /* We cant' use dev_ready here, but at least we wait for the
408 * command to complete
409 */
410 udelay(50);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200411
412 ret = this->read_byte(mtd) << 8;
413 ret |= this->read_byte(mtd);
414
415 if (doc->ChipID == DOC_ChipID_Doc2k && try_dword && !nr) {
416 /* First chip probe. See if we get same results by 32-bit access */
417 union {
418 uint32_t dword;
419 uint8_t byte[4];
420 } ident;
421 void __iomem *docptr = doc->virtadr;
422
William Juulcfa460a2007-10-31 13:53:06 +0100423 doc200x_hwcontrol(mtd, NAND_CMD_READID,
424 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
425 doc200x_hwcontrol(mtd, 0, NAND_CTRL_ALE | NAND_CTRL_CHANGE);
426 doc200x_hwcontrol(mtd, NAND_CMD_NONE,
427 NAND_NCE | NAND_CTRL_CHANGE);
428
429 udelay(50);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200430
431 ident.dword = readl(docptr + DoC_2k_CDSN_IO);
432 if (((ident.byte[0] << 8) | ident.byte[1]) == ret) {
433 printk(KERN_INFO "DiskOnChip 2000 responds to DWORD access\n");
434 this->read_buf = &doc2000_readbuf_dword;
435 }
436 }
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200437
Wolfgang Denk932394a2005-08-17 12:55:25 +0200438 return ret;
439}
440
441static void __init doc2000_count_chips(struct mtd_info *mtd)
442{
443 struct nand_chip *this = mtd->priv;
444 struct doc_priv *doc = this->priv;
445 uint16_t mfrid;
446 int i;
447
448 /* Max 4 chips per floor on DiskOnChip 2000 */
449 doc->chips_per_floor = 4;
450
451 /* Find out what the first chip is */
452 mfrid = doc200x_ident_chip(mtd, 0);
453
454 /* Find how many chips in each floor. */
455 for (i = 1; i < 4; i++) {
456 if (doc200x_ident_chip(mtd, i) != mfrid)
457 break;
458 }
459 doc->chips_per_floor = i;
460 printk(KERN_DEBUG "Detected %d chips per floor.\n", i);
461}
462
William Juulcfa460a2007-10-31 13:53:06 +0100463static int doc200x_wait(struct mtd_info *mtd, struct nand_chip *this)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200464{
465 struct doc_priv *doc = this->priv;
466
467 int status;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200468
Wolfgang Denk932394a2005-08-17 12:55:25 +0200469 DoC_WaitReady(doc);
470 this->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
471 DoC_WaitReady(doc);
472 status = (int)this->read_byte(mtd);
473
474 return status;
475}
476
477static void doc2001_write_byte(struct mtd_info *mtd, u_char datum)
478{
479 struct nand_chip *this = mtd->priv;
480 struct doc_priv *doc = this->priv;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200481 void __iomem *docptr = doc->virtadr;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200482
483 WriteDOC(datum, docptr, CDSNSlowIO);
484 WriteDOC(datum, docptr, Mil_CDSN_IO);
485 WriteDOC(datum, docptr, WritePipeTerm);
486}
487
488static u_char doc2001_read_byte(struct mtd_info *mtd)
489{
490 struct nand_chip *this = mtd->priv;
491 struct doc_priv *doc = this->priv;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200492 void __iomem *docptr = doc->virtadr;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200493
William Juul5e1dae52007-11-09 13:32:30 +0100494 /*ReadDOC(docptr, CDSNSlowIO); */
Wolfgang Denk932394a2005-08-17 12:55:25 +0200495 /* 11.4.5 -- delay twice to allow extended length cycle */
496 DoC_Delay(doc, 2);
497 ReadDOC(docptr, ReadPipeInit);
William Juul5e1dae52007-11-09 13:32:30 +0100498 /*return ReadDOC(docptr, Mil_CDSN_IO); */
Wolfgang Denk932394a2005-08-17 12:55:25 +0200499 return ReadDOC(docptr, LastDataRead);
500}
501
William Juulcfa460a2007-10-31 13:53:06 +0100502static void doc2001_writebuf(struct mtd_info *mtd, const u_char *buf, int len)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200503{
504 struct nand_chip *this = mtd->priv;
505 struct doc_priv *doc = this->priv;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200506 void __iomem *docptr = doc->virtadr;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200507 int i;
508
William Juulcfa460a2007-10-31 13:53:06 +0100509 for (i = 0; i < len; i++)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200510 WriteDOC_(buf[i], docptr, DoC_Mil_CDSN_IO + i);
511 /* Terminate write pipeline */
512 WriteDOC(0x00, docptr, WritePipeTerm);
513}
514
William Juulcfa460a2007-10-31 13:53:06 +0100515static void doc2001_readbuf(struct mtd_info *mtd, u_char *buf, int len)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200516{
517 struct nand_chip *this = mtd->priv;
518 struct doc_priv *doc = this->priv;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200519 void __iomem *docptr = doc->virtadr;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200520 int i;
521
522 /* Start read pipeline */
523 ReadDOC(docptr, ReadPipeInit);
524
William Juulcfa460a2007-10-31 13:53:06 +0100525 for (i = 0; i < len - 1; i++)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200526 buf[i] = ReadDOC(docptr, Mil_CDSN_IO + (i & 0xff));
527
528 /* Terminate read pipeline */
529 buf[i] = ReadDOC(docptr, LastDataRead);
530}
531
William Juulcfa460a2007-10-31 13:53:06 +0100532static int doc2001_verifybuf(struct mtd_info *mtd, const u_char *buf, int len)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200533{
534 struct nand_chip *this = mtd->priv;
535 struct doc_priv *doc = this->priv;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200536 void __iomem *docptr = doc->virtadr;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200537 int i;
538
539 /* Start read pipeline */
540 ReadDOC(docptr, ReadPipeInit);
541
William Juulcfa460a2007-10-31 13:53:06 +0100542 for (i = 0; i < len - 1; i++)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200543 if (buf[i] != ReadDOC(docptr, Mil_CDSN_IO)) {
544 ReadDOC(docptr, LastDataRead);
545 return i;
546 }
547 if (buf[i] != ReadDOC(docptr, LastDataRead))
548 return i;
549 return 0;
550}
551
552static u_char doc2001plus_read_byte(struct mtd_info *mtd)
553{
554 struct nand_chip *this = mtd->priv;
555 struct doc_priv *doc = this->priv;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200556 void __iomem *docptr = doc->virtadr;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200557 u_char ret;
558
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200559 ReadDOC(docptr, Mplus_ReadPipeInit);
560 ReadDOC(docptr, Mplus_ReadPipeInit);
561 ret = ReadDOC(docptr, Mplus_LastDataRead);
William Juulcfa460a2007-10-31 13:53:06 +0100562 if (debug)
563 printk("read_byte returns %02x\n", ret);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200564 return ret;
565}
566
William Juulcfa460a2007-10-31 13:53:06 +0100567static void doc2001plus_writebuf(struct mtd_info *mtd, const u_char *buf, int len)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200568{
569 struct nand_chip *this = mtd->priv;
570 struct doc_priv *doc = this->priv;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200571 void __iomem *docptr = doc->virtadr;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200572 int i;
573
William Juulcfa460a2007-10-31 13:53:06 +0100574 if (debug)
575 printk("writebuf of %d bytes: ", len);
576 for (i = 0; i < len; i++) {
Wolfgang Denk932394a2005-08-17 12:55:25 +0200577 WriteDOC_(buf[i], docptr, DoC_Mil_CDSN_IO + i);
578 if (debug && i < 16)
579 printk("%02x ", buf[i]);
580 }
William Juulcfa460a2007-10-31 13:53:06 +0100581 if (debug)
582 printk("\n");
Wolfgang Denk932394a2005-08-17 12:55:25 +0200583}
584
William Juulcfa460a2007-10-31 13:53:06 +0100585static void doc2001plus_readbuf(struct mtd_info *mtd, u_char *buf, int len)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200586{
587 struct nand_chip *this = mtd->priv;
588 struct doc_priv *doc = this->priv;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200589 void __iomem *docptr = doc->virtadr;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200590 int i;
591
William Juulcfa460a2007-10-31 13:53:06 +0100592 if (debug)
593 printk("readbuf of %d bytes: ", len);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200594
595 /* Start read pipeline */
596 ReadDOC(docptr, Mplus_ReadPipeInit);
597 ReadDOC(docptr, Mplus_ReadPipeInit);
598
William Juulcfa460a2007-10-31 13:53:06 +0100599 for (i = 0; i < len - 2; i++) {
Wolfgang Denk932394a2005-08-17 12:55:25 +0200600 buf[i] = ReadDOC(docptr, Mil_CDSN_IO);
601 if (debug && i < 16)
602 printk("%02x ", buf[i]);
603 }
604
605 /* Terminate read pipeline */
William Juulcfa460a2007-10-31 13:53:06 +0100606 buf[len - 2] = ReadDOC(docptr, Mplus_LastDataRead);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200607 if (debug && i < 16)
William Juulcfa460a2007-10-31 13:53:06 +0100608 printk("%02x ", buf[len - 2]);
609 buf[len - 1] = ReadDOC(docptr, Mplus_LastDataRead);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200610 if (debug && i < 16)
William Juulcfa460a2007-10-31 13:53:06 +0100611 printk("%02x ", buf[len - 1]);
612 if (debug)
613 printk("\n");
Wolfgang Denk932394a2005-08-17 12:55:25 +0200614}
615
William Juulcfa460a2007-10-31 13:53:06 +0100616static int doc2001plus_verifybuf(struct mtd_info *mtd, const u_char *buf, int len)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200617{
618 struct nand_chip *this = mtd->priv;
619 struct doc_priv *doc = this->priv;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200620 void __iomem *docptr = doc->virtadr;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200621 int i;
622
William Juulcfa460a2007-10-31 13:53:06 +0100623 if (debug)
624 printk("verifybuf of %d bytes: ", len);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200625
626 /* Start read pipeline */
627 ReadDOC(docptr, Mplus_ReadPipeInit);
628 ReadDOC(docptr, Mplus_ReadPipeInit);
629
William Juulcfa460a2007-10-31 13:53:06 +0100630 for (i = 0; i < len - 2; i++)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200631 if (buf[i] != ReadDOC(docptr, Mil_CDSN_IO)) {
632 ReadDOC(docptr, Mplus_LastDataRead);
633 ReadDOC(docptr, Mplus_LastDataRead);
634 return i;
635 }
William Juulcfa460a2007-10-31 13:53:06 +0100636 if (buf[len - 2] != ReadDOC(docptr, Mplus_LastDataRead))
637 return len - 2;
638 if (buf[len - 1] != ReadDOC(docptr, Mplus_LastDataRead))
639 return len - 1;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200640 return 0;
641}
642
643static void doc2001plus_select_chip(struct mtd_info *mtd, int chip)
644{
645 struct nand_chip *this = mtd->priv;
646 struct doc_priv *doc = this->priv;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200647 void __iomem *docptr = doc->virtadr;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200648 int floor = 0;
649
William Juulcfa460a2007-10-31 13:53:06 +0100650 if (debug)
651 printk("select chip (%d)\n", chip);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200652
653 if (chip == -1) {
654 /* Disable flash internally */
655 WriteDOC(0, docptr, Mplus_FlashSelect);
656 return;
657 }
658
659 floor = chip / doc->chips_per_floor;
William Juulcfa460a2007-10-31 13:53:06 +0100660 chip -= (floor * doc->chips_per_floor);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200661
662 /* Assert ChipEnable and deassert WriteProtect */
663 WriteDOC((DOC_FLASH_CE), docptr, Mplus_FlashSelect);
664 this->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
665
666 doc->curchip = chip;
667 doc->curfloor = floor;
668}
669
670static void doc200x_select_chip(struct mtd_info *mtd, int chip)
671{
672 struct nand_chip *this = mtd->priv;
673 struct doc_priv *doc = this->priv;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200674 void __iomem *docptr = doc->virtadr;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200675 int floor = 0;
676
William Juulcfa460a2007-10-31 13:53:06 +0100677 if (debug)
678 printk("select chip (%d)\n", chip);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200679
680 if (chip == -1)
681 return;
682
683 floor = chip / doc->chips_per_floor;
William Juulcfa460a2007-10-31 13:53:06 +0100684 chip -= (floor * doc->chips_per_floor);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200685
686 /* 11.4.4 -- deassert CE before changing chip */
William Juulcfa460a2007-10-31 13:53:06 +0100687 doc200x_hwcontrol(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200688
689 WriteDOC(floor, docptr, FloorSelect);
690 WriteDOC(chip, docptr, CDSNDeviceSelect);
691
William Juulcfa460a2007-10-31 13:53:06 +0100692 doc200x_hwcontrol(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200693
694 doc->curchip = chip;
695 doc->curfloor = floor;
696}
697
William Juulcfa460a2007-10-31 13:53:06 +0100698#define CDSN_CTRL_MSK (CDSN_CTRL_CE | CDSN_CTRL_CLE | CDSN_CTRL_ALE)
699
700static void doc200x_hwcontrol(struct mtd_info *mtd, int cmd,
701 unsigned int ctrl)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200702{
703 struct nand_chip *this = mtd->priv;
704 struct doc_priv *doc = this->priv;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200705 void __iomem *docptr = doc->virtadr;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200706
William Juulcfa460a2007-10-31 13:53:06 +0100707 if (ctrl & NAND_CTRL_CHANGE) {
708 doc->CDSNControl &= ~CDSN_CTRL_MSK;
709 doc->CDSNControl |= ctrl & CDSN_CTRL_MSK;
710 if (debug)
711 printk("hwcontrol(%d): %02x\n", cmd, doc->CDSNControl);
712 WriteDOC(doc->CDSNControl, docptr, CDSNControl);
713 /* 11.4.3 -- 4 NOPs after CSDNControl write */
714 DoC_Delay(doc, 4);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200715 }
William Juulcfa460a2007-10-31 13:53:06 +0100716 if (cmd != NAND_CMD_NONE) {
717 if (DoC_is_2000(doc))
718 doc2000_write_byte(mtd, cmd);
719 else
720 doc2001_write_byte(mtd, cmd);
721 }
Wolfgang Denk932394a2005-08-17 12:55:25 +0200722}
723
William Juulcfa460a2007-10-31 13:53:06 +0100724static void doc2001plus_command(struct mtd_info *mtd, unsigned command, int column, int page_addr)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200725{
726 struct nand_chip *this = mtd->priv;
727 struct doc_priv *doc = this->priv;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200728 void __iomem *docptr = doc->virtadr;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200729
730 /*
731 * Must terminate write pipeline before sending any commands
732 * to the device.
733 */
734 if (command == NAND_CMD_PAGEPROG) {
735 WriteDOC(0x00, docptr, Mplus_WritePipeTerm);
736 WriteDOC(0x00, docptr, Mplus_WritePipeTerm);
737 }
738
739 /*
740 * Write out the command to the device.
741 */
742 if (command == NAND_CMD_SEQIN) {
743 int readcmd;
744
William Juulcfa460a2007-10-31 13:53:06 +0100745 if (column >= mtd->writesize) {
Wolfgang Denk932394a2005-08-17 12:55:25 +0200746 /* OOB area */
William Juulcfa460a2007-10-31 13:53:06 +0100747 column -= mtd->writesize;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200748 readcmd = NAND_CMD_READOOB;
749 } else if (column < 256) {
750 /* First 256 bytes --> READ0 */
751 readcmd = NAND_CMD_READ0;
752 } else {
753 column -= 256;
754 readcmd = NAND_CMD_READ1;
755 }
756 WriteDOC(readcmd, docptr, Mplus_FlashCmd);
757 }
758 WriteDOC(command, docptr, Mplus_FlashCmd);
759 WriteDOC(0, docptr, Mplus_WritePipeTerm);
760 WriteDOC(0, docptr, Mplus_WritePipeTerm);
761
762 if (column != -1 || page_addr != -1) {
763 /* Serially input address */
764 if (column != -1) {
765 /* Adjust columns for 16 bit buswidth */
766 if (this->options & NAND_BUSWIDTH_16)
767 column >>= 1;
768 WriteDOC(column, docptr, Mplus_FlashAddress);
769 }
770 if (page_addr != -1) {
William Juulcfa460a2007-10-31 13:53:06 +0100771 WriteDOC((unsigned char)(page_addr & 0xff), docptr, Mplus_FlashAddress);
772 WriteDOC((unsigned char)((page_addr >> 8) & 0xff), docptr, Mplus_FlashAddress);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200773 /* One more address cycle for higher density devices */
774 if (this->chipsize & 0x0c000000) {
William Juulcfa460a2007-10-31 13:53:06 +0100775 WriteDOC((unsigned char)((page_addr >> 16) & 0x0f), docptr, Mplus_FlashAddress);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200776 printk("high density\n");
777 }
778 }
779 WriteDOC(0, docptr, Mplus_WritePipeTerm);
780 WriteDOC(0, docptr, Mplus_WritePipeTerm);
781 /* deassert ALE */
William Juulcfa460a2007-10-31 13:53:06 +0100782 if (command == NAND_CMD_READ0 || command == NAND_CMD_READ1 ||
783 command == NAND_CMD_READOOB || command == NAND_CMD_READID)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200784 WriteDOC(0, docptr, Mplus_FlashControl);
785 }
786
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200787 /*
Wolfgang Denk932394a2005-08-17 12:55:25 +0200788 * program and erase have their own busy handlers
789 * status and sequential in needs no delay
William Juulcfa460a2007-10-31 13:53:06 +0100790 */
Wolfgang Denk932394a2005-08-17 12:55:25 +0200791 switch (command) {
792
793 case NAND_CMD_PAGEPROG:
794 case NAND_CMD_ERASE1:
795 case NAND_CMD_ERASE2:
796 case NAND_CMD_SEQIN:
797 case NAND_CMD_STATUS:
798 return;
799
800 case NAND_CMD_RESET:
801 if (this->dev_ready)
802 break;
803 udelay(this->chip_delay);
804 WriteDOC(NAND_CMD_STATUS, docptr, Mplus_FlashCmd);
805 WriteDOC(0, docptr, Mplus_WritePipeTerm);
806 WriteDOC(0, docptr, Mplus_WritePipeTerm);
William Juulcfa460a2007-10-31 13:53:06 +0100807 while (!(this->read_byte(mtd) & 0x40)) ;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200808 return;
809
William Juulcfa460a2007-10-31 13:53:06 +0100810 /* This applies to read commands */
Wolfgang Denk932394a2005-08-17 12:55:25 +0200811 default:
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200812 /*
Wolfgang Denk932394a2005-08-17 12:55:25 +0200813 * If we don't have access to the busy pin, we apply the given
814 * command delay
William Juulcfa460a2007-10-31 13:53:06 +0100815 */
Wolfgang Denk932394a2005-08-17 12:55:25 +0200816 if (!this->dev_ready) {
William Juulcfa460a2007-10-31 13:53:06 +0100817 udelay(this->chip_delay);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200818 return;
819 }
820 }
821
822 /* Apply this short delay always to ensure that we do wait tWB in
823 * any case on any machine. */
William Juulcfa460a2007-10-31 13:53:06 +0100824 ndelay(100);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200825 /* wait until command is processed */
William Juulcfa460a2007-10-31 13:53:06 +0100826 while (!this->dev_ready(mtd)) ;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200827}
828
829static int doc200x_dev_ready(struct mtd_info *mtd)
830{
831 struct nand_chip *this = mtd->priv;
832 struct doc_priv *doc = this->priv;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200833 void __iomem *docptr = doc->virtadr;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200834
835 if (DoC_is_MillenniumPlus(doc)) {
836 /* 11.4.2 -- must NOP four times before checking FR/B# */
837 DoC_Delay(doc, 4);
838 if ((ReadDOC(docptr, Mplus_FlashControl) & CDSN_CTRL_FR_B_MASK) != CDSN_CTRL_FR_B_MASK) {
William Juulcfa460a2007-10-31 13:53:06 +0100839 if (debug)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200840 printk("not ready\n");
841 return 0;
842 }
William Juulcfa460a2007-10-31 13:53:06 +0100843 if (debug)
844 printk("was ready\n");
Wolfgang Denk932394a2005-08-17 12:55:25 +0200845 return 1;
846 } else {
847 /* 11.4.2 -- must NOP four times before checking FR/B# */
848 DoC_Delay(doc, 4);
849 if (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B)) {
William Juulcfa460a2007-10-31 13:53:06 +0100850 if (debug)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200851 printk("not ready\n");
852 return 0;
853 }
854 /* 11.4.2 -- Must NOP twice if it's ready */
855 DoC_Delay(doc, 2);
William Juulcfa460a2007-10-31 13:53:06 +0100856 if (debug)
857 printk("was ready\n");
Wolfgang Denk932394a2005-08-17 12:55:25 +0200858 return 1;
859 }
860}
861
862static int doc200x_block_bad(struct mtd_info *mtd, loff_t ofs, int getchip)
863{
864 /* This is our last resort if we couldn't find or create a BBT. Just
865 pretend all blocks are good. */
866 return 0;
867}
868
869static void doc200x_enable_hwecc(struct mtd_info *mtd, int mode)
870{
871 struct nand_chip *this = mtd->priv;
872 struct doc_priv *doc = this->priv;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200873 void __iomem *docptr = doc->virtadr;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200874
875 /* Prime the ECC engine */
William Juulcfa460a2007-10-31 13:53:06 +0100876 switch (mode) {
Wolfgang Denk932394a2005-08-17 12:55:25 +0200877 case NAND_ECC_READ:
878 WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
879 WriteDOC(DOC_ECC_EN, docptr, ECCConf);
880 break;
881 case NAND_ECC_WRITE:
882 WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
883 WriteDOC(DOC_ECC_EN | DOC_ECC_RW, docptr, ECCConf);
884 break;
885 }
886}
887
888static void doc2001plus_enable_hwecc(struct mtd_info *mtd, int mode)
889{
890 struct nand_chip *this = mtd->priv;
891 struct doc_priv *doc = this->priv;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200892 void __iomem *docptr = doc->virtadr;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200893
894 /* Prime the ECC engine */
William Juulcfa460a2007-10-31 13:53:06 +0100895 switch (mode) {
Wolfgang Denk932394a2005-08-17 12:55:25 +0200896 case NAND_ECC_READ:
897 WriteDOC(DOC_ECC_RESET, docptr, Mplus_ECCConf);
898 WriteDOC(DOC_ECC_EN, docptr, Mplus_ECCConf);
899 break;
900 case NAND_ECC_WRITE:
901 WriteDOC(DOC_ECC_RESET, docptr, Mplus_ECCConf);
902 WriteDOC(DOC_ECC_EN | DOC_ECC_RW, docptr, Mplus_ECCConf);
903 break;
904 }
905}
906
907/* This code is only called on write */
William Juulcfa460a2007-10-31 13:53:06 +0100908static int doc200x_calculate_ecc(struct mtd_info *mtd, const u_char *dat, unsigned char *ecc_code)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200909{
910 struct nand_chip *this = mtd->priv;
911 struct doc_priv *doc = this->priv;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200912 void __iomem *docptr = doc->virtadr;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200913 int i;
914 int emptymatch = 1;
915
916 /* flush the pipeline */
917 if (DoC_is_2000(doc)) {
918 WriteDOC(doc->CDSNControl & ~CDSN_CTRL_FLASH_IO, docptr, CDSNControl);
919 WriteDOC(0, docptr, 2k_CDSN_IO);
920 WriteDOC(0, docptr, 2k_CDSN_IO);
921 WriteDOC(0, docptr, 2k_CDSN_IO);
922 WriteDOC(doc->CDSNControl, docptr, CDSNControl);
923 } else if (DoC_is_MillenniumPlus(doc)) {
924 WriteDOC(0, docptr, Mplus_NOP);
925 WriteDOC(0, docptr, Mplus_NOP);
926 WriteDOC(0, docptr, Mplus_NOP);
927 } else {
928 WriteDOC(0, docptr, NOP);
929 WriteDOC(0, docptr, NOP);
930 WriteDOC(0, docptr, NOP);
931 }
932
933 for (i = 0; i < 6; i++) {
934 if (DoC_is_MillenniumPlus(doc))
935 ecc_code[i] = ReadDOC_(docptr, DoC_Mplus_ECCSyndrome0 + i);
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200936 else
Wolfgang Denk932394a2005-08-17 12:55:25 +0200937 ecc_code[i] = ReadDOC_(docptr, DoC_ECCSyndrome0 + i);
938 if (ecc_code[i] != empty_write_ecc[i])
939 emptymatch = 0;
940 }
941 if (DoC_is_MillenniumPlus(doc))
942 WriteDOC(DOC_ECC_DIS, docptr, Mplus_ECCConf);
943 else
944 WriteDOC(DOC_ECC_DIS, docptr, ECCConf);
945#if 0
946 /* If emptymatch=1, we might have an all-0xff data buffer. Check. */
947 if (emptymatch) {
948 /* Note: this somewhat expensive test should not be triggered
949 often. It could be optimized away by examining the data in
950 the writebuf routine, and remembering the result. */
951 for (i = 0; i < 512; i++) {
William Juulcfa460a2007-10-31 13:53:06 +0100952 if (dat[i] == 0xff)
953 continue;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200954 emptymatch = 0;
955 break;
956 }
957 }
958 /* If emptymatch still =1, we do have an all-0xff data buffer.
959 Return all-0xff ecc value instead of the computed one, so
960 it'll look just like a freshly-erased page. */
William Juulcfa460a2007-10-31 13:53:06 +0100961 if (emptymatch)
962 memset(ecc_code, 0xff, 6);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200963#endif
964 return 0;
965}
966
William Juulcfa460a2007-10-31 13:53:06 +0100967static int doc200x_correct_data(struct mtd_info *mtd, u_char *dat,
968 u_char *read_ecc, u_char *isnull)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200969{
970 int i, ret = 0;
971 struct nand_chip *this = mtd->priv;
972 struct doc_priv *doc = this->priv;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200973 void __iomem *docptr = doc->virtadr;
William Juulcfa460a2007-10-31 13:53:06 +0100974 uint8_t calc_ecc[6];
Wolfgang Denk932394a2005-08-17 12:55:25 +0200975 volatile u_char dummy;
976 int emptymatch = 1;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200977
Wolfgang Denk932394a2005-08-17 12:55:25 +0200978 /* flush the pipeline */
979 if (DoC_is_2000(doc)) {
980 dummy = ReadDOC(docptr, 2k_ECCStatus);
981 dummy = ReadDOC(docptr, 2k_ECCStatus);
982 dummy = ReadDOC(docptr, 2k_ECCStatus);
983 } else if (DoC_is_MillenniumPlus(doc)) {
984 dummy = ReadDOC(docptr, Mplus_ECCConf);
985 dummy = ReadDOC(docptr, Mplus_ECCConf);
986 dummy = ReadDOC(docptr, Mplus_ECCConf);
987 } else {
988 dummy = ReadDOC(docptr, ECCConf);
989 dummy = ReadDOC(docptr, ECCConf);
990 dummy = ReadDOC(docptr, ECCConf);
991 }
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200992
Wolfgang Denk932394a2005-08-17 12:55:25 +0200993 /* Error occured ? */
994 if (dummy & 0x80) {
995 for (i = 0; i < 6; i++) {
996 if (DoC_is_MillenniumPlus(doc))
997 calc_ecc[i] = ReadDOC_(docptr, DoC_Mplus_ECCSyndrome0 + i);
998 else
999 calc_ecc[i] = ReadDOC_(docptr, DoC_ECCSyndrome0 + i);
1000 if (calc_ecc[i] != empty_read_syndrome[i])
1001 emptymatch = 0;
1002 }
1003 /* If emptymatch=1, the read syndrome is consistent with an
1004 all-0xff data and stored ecc block. Check the stored ecc. */
1005 if (emptymatch) {
1006 for (i = 0; i < 6; i++) {
William Juulcfa460a2007-10-31 13:53:06 +01001007 if (read_ecc[i] == 0xff)
1008 continue;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001009 emptymatch = 0;
1010 break;
1011 }
1012 }
1013 /* If emptymatch still =1, check the data block. */
1014 if (emptymatch) {
William Juulcfa460a2007-10-31 13:53:06 +01001015 /* Note: this somewhat expensive test should not be triggered
1016 often. It could be optimized away by examining the data in
1017 the readbuf routine, and remembering the result. */
Wolfgang Denk932394a2005-08-17 12:55:25 +02001018 for (i = 0; i < 512; i++) {
William Juulcfa460a2007-10-31 13:53:06 +01001019 if (dat[i] == 0xff)
1020 continue;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001021 emptymatch = 0;
1022 break;
1023 }
1024 }
1025 /* If emptymatch still =1, this is almost certainly a freshly-
1026 erased block, in which case the ECC will not come out right.
1027 We'll suppress the error and tell the caller everything's
1028 OK. Because it is. */
William Juulcfa460a2007-10-31 13:53:06 +01001029 if (!emptymatch)
1030 ret = doc_ecc_decode(rs_decoder, dat, calc_ecc);
Wolfgang Denk932394a2005-08-17 12:55:25 +02001031 if (ret > 0)
1032 printk(KERN_ERR "doc200x_correct_data corrected %d errors\n", ret);
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02001033 }
Wolfgang Denk932394a2005-08-17 12:55:25 +02001034 if (DoC_is_MillenniumPlus(doc))
1035 WriteDOC(DOC_ECC_DIS, docptr, Mplus_ECCConf);
1036 else
1037 WriteDOC(DOC_ECC_DIS, docptr, ECCConf);
Scott Woodc45912d2008-10-24 16:20:43 -05001038 if (no_ecc_failures && (ret == -EBADMSG)) {
Wolfgang Denk932394a2005-08-17 12:55:25 +02001039 printk(KERN_ERR "suppressing ECC failure\n");
1040 ret = 0;
1041 }
1042 return ret;
1043}
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02001044
William Juul5e1dae52007-11-09 13:32:30 +01001045/*u_char mydatabuf[528]; */
Wolfgang Denk932394a2005-08-17 12:55:25 +02001046
William Juulcfa460a2007-10-31 13:53:06 +01001047/* The strange out-of-order .oobfree list below is a (possibly unneeded)
1048 * attempt to retain compatibility. It used to read:
1049 * .oobfree = { {8, 8} }
1050 * Since that leaves two bytes unusable, it was changed. But the following
1051 * scheme might affect existing jffs2 installs by moving the cleanmarker:
1052 * .oobfree = { {6, 10} }
1053 * jffs2 seems to handle the above gracefully, but the current scheme seems
1054 * safer. The only problem with it is that any code that parses oobfree must
1055 * be able to handle out-of-order segments.
1056 */
1057static struct nand_ecclayout doc200x_oobinfo = {
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02001058 .eccbytes = 6,
1059 .eccpos = {0, 1, 2, 3, 4, 5},
William Juulcfa460a2007-10-31 13:53:06 +01001060 .oobfree = {{8, 8}, {6, 2}}
Wolfgang Denk932394a2005-08-17 12:55:25 +02001061};
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02001062
Wolfgang Denk932394a2005-08-17 12:55:25 +02001063/* Find the (I)NFTL Media Header, and optionally also the mirror media header.
1064 On sucessful return, buf will contain a copy of the media header for
1065 further processing. id is the string to scan for, and will presumably be
1066 either "ANAND" or "BNAND". If findmirror=1, also look for the mirror media
1067 header. The page #s of the found media headers are placed in mh0_page and
1068 mh1_page in the DOC private structure. */
William Juulcfa460a2007-10-31 13:53:06 +01001069static int __init find_media_headers(struct mtd_info *mtd, u_char *buf, const char *id, int findmirror)
Wolfgang Denk932394a2005-08-17 12:55:25 +02001070{
1071 struct nand_chip *this = mtd->priv;
1072 struct doc_priv *doc = this->priv;
William Juulcfa460a2007-10-31 13:53:06 +01001073 unsigned offs;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001074 int ret;
1075 size_t retlen;
1076
William Juulcfa460a2007-10-31 13:53:06 +01001077 for (offs = 0; offs < mtd->size; offs += mtd->erasesize) {
1078 ret = mtd->read(mtd, offs, mtd->writesize, &retlen, buf);
1079 if (retlen != mtd->writesize)
1080 continue;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001081 if (ret) {
William Juulcfa460a2007-10-31 13:53:06 +01001082 printk(KERN_WARNING "ECC error scanning DOC at 0x%x\n", offs);
Wolfgang Denk932394a2005-08-17 12:55:25 +02001083 }
William Juulcfa460a2007-10-31 13:53:06 +01001084 if (memcmp(buf, id, 6))
1085 continue;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001086 printk(KERN_INFO "Found DiskOnChip %s Media Header at 0x%x\n", id, offs);
1087 if (doc->mh0_page == -1) {
1088 doc->mh0_page = offs >> this->page_shift;
William Juulcfa460a2007-10-31 13:53:06 +01001089 if (!findmirror)
1090 return 1;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001091 continue;
1092 }
1093 doc->mh1_page = offs >> this->page_shift;
1094 return 2;
1095 }
1096 if (doc->mh0_page == -1) {
1097 printk(KERN_WARNING "DiskOnChip %s Media Header not found.\n", id);
1098 return 0;
1099 }
1100 /* Only one mediaheader was found. We want buf to contain a
1101 mediaheader on return, so we'll have to re-read the one we found. */
1102 offs = doc->mh0_page << this->page_shift;
William Juulcfa460a2007-10-31 13:53:06 +01001103 ret = mtd->read(mtd, offs, mtd->writesize, &retlen, buf);
1104 if (retlen != mtd->writesize) {
Wolfgang Denk932394a2005-08-17 12:55:25 +02001105 /* Insanity. Give up. */
1106 printk(KERN_ERR "Read DiskOnChip Media Header once, but can't reread it???\n");
1107 return 0;
1108 }
1109 return 1;
1110}
1111
William Juulcfa460a2007-10-31 13:53:06 +01001112static inline int __init nftl_partscan(struct mtd_info *mtd, struct mtd_partition *parts)
Wolfgang Denk932394a2005-08-17 12:55:25 +02001113{
1114 struct nand_chip *this = mtd->priv;
1115 struct doc_priv *doc = this->priv;
1116 int ret = 0;
1117 u_char *buf;
1118 struct NFTLMediaHeader *mh;
1119 const unsigned psize = 1 << this->page_shift;
William Juulcfa460a2007-10-31 13:53:06 +01001120 int numparts = 0;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001121 unsigned blocks, maxblocks;
1122 int offs, numheaders;
1123
William Juulcfa460a2007-10-31 13:53:06 +01001124 buf = kmalloc(mtd->writesize, GFP_KERNEL);
Wolfgang Denk932394a2005-08-17 12:55:25 +02001125 if (!buf) {
1126 printk(KERN_ERR "DiskOnChip mediaheader kmalloc failed!\n");
1127 return 0;
1128 }
William Juulcfa460a2007-10-31 13:53:06 +01001129 if (!(numheaders = find_media_headers(mtd, buf, "ANAND", 1)))
1130 goto out;
1131 mh = (struct NFTLMediaHeader *)buf;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001132
Scott Woodc45912d2008-10-24 16:20:43 -05001133 le16_to_cpus(&mh->NumEraseUnits);
1134 le16_to_cpus(&mh->FirstPhysicalEUN);
1135 le32_to_cpus(&mh->FormattedSize);
William Juulcfa460a2007-10-31 13:53:06 +01001136
Wolfgang Denk932394a2005-08-17 12:55:25 +02001137 printk(KERN_INFO " DataOrgID = %s\n"
1138 " NumEraseUnits = %d\n"
1139 " FirstPhysicalEUN = %d\n"
1140 " FormattedSize = %d\n"
1141 " UnitSizeFactor = %d\n",
1142 mh->DataOrgID, mh->NumEraseUnits,
1143 mh->FirstPhysicalEUN, mh->FormattedSize,
1144 mh->UnitSizeFactor);
Wolfgang Denk932394a2005-08-17 12:55:25 +02001145
1146 blocks = mtd->size >> this->phys_erase_shift;
1147 maxblocks = min(32768U, mtd->erasesize - psize);
1148
1149 if (mh->UnitSizeFactor == 0x00) {
1150 /* Auto-determine UnitSizeFactor. The constraints are:
1151 - There can be at most 32768 virtual blocks.
1152 - There can be at most (virtual block size - page size)
William Juulcfa460a2007-10-31 13:53:06 +01001153 virtual blocks (because MediaHeader+BBT must fit in 1).
1154 */
Wolfgang Denk932394a2005-08-17 12:55:25 +02001155 mh->UnitSizeFactor = 0xff;
1156 while (blocks > maxblocks) {
1157 blocks >>= 1;
1158 maxblocks = min(32768U, (maxblocks << 1) + psize);
1159 mh->UnitSizeFactor--;
1160 }
1161 printk(KERN_WARNING "UnitSizeFactor=0x00 detected. Correct value is assumed to be 0x%02x.\n", mh->UnitSizeFactor);
1162 }
1163
1164 /* NOTE: The lines below modify internal variables of the NAND and MTD
1165 layers; variables with have already been configured by nand_scan.
1166 Unfortunately, we didn't know before this point what these values
1167 should be. Thus, this code is somewhat dependant on the exact
1168 implementation of the NAND layer. */
1169 if (mh->UnitSizeFactor != 0xff) {
1170 this->bbt_erase_shift += (0xff - mh->UnitSizeFactor);
1171 mtd->erasesize <<= (0xff - mh->UnitSizeFactor);
1172 printk(KERN_INFO "Setting virtual erase size to %d\n", mtd->erasesize);
1173 blocks = mtd->size >> this->bbt_erase_shift;
1174 maxblocks = min(32768U, mtd->erasesize - psize);
1175 }
1176
1177 if (blocks > maxblocks) {
1178 printk(KERN_ERR "UnitSizeFactor of 0x%02x is inconsistent with device size. Aborting.\n", mh->UnitSizeFactor);
1179 goto out;
1180 }
1181
1182 /* Skip past the media headers. */
1183 offs = max(doc->mh0_page, doc->mh1_page);
1184 offs <<= this->page_shift;
1185 offs += mtd->erasesize;
1186
William Juulcfa460a2007-10-31 13:53:06 +01001187 if (show_firmware_partition == 1) {
1188 parts[0].name = " DiskOnChip Firmware / Media Header partition";
1189 parts[0].offset = 0;
1190 parts[0].size = offs;
1191 numparts = 1;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001192 }
William Juulcfa460a2007-10-31 13:53:06 +01001193
1194 parts[numparts].name = " DiskOnChip BDTL partition";
1195 parts[numparts].offset = offs;
1196 parts[numparts].size = (mh->NumEraseUnits - numheaders) << this->bbt_erase_shift;
1197
1198 offs += parts[numparts].size;
1199 numparts++;
1200
1201 if (offs < mtd->size) {
1202 parts[numparts].name = " DiskOnChip Remainder partition";
1203 parts[numparts].offset = offs;
1204 parts[numparts].size = mtd->size - offs;
1205 numparts++;
1206 }
1207
1208 ret = numparts;
1209 out:
Wolfgang Denk932394a2005-08-17 12:55:25 +02001210 kfree(buf);
1211 return ret;
1212}
1213
1214/* This is a stripped-down copy of the code in inftlmount.c */
William Juulcfa460a2007-10-31 13:53:06 +01001215static inline int __init inftl_partscan(struct mtd_info *mtd, struct mtd_partition *parts)
Wolfgang Denk932394a2005-08-17 12:55:25 +02001216{
1217 struct nand_chip *this = mtd->priv;
1218 struct doc_priv *doc = this->priv;
1219 int ret = 0;
1220 u_char *buf;
1221 struct INFTLMediaHeader *mh;
1222 struct INFTLPartition *ip;
1223 int numparts = 0;
1224 int blocks;
1225 int vshift, lastvunit = 0;
1226 int i;
1227 int end = mtd->size;
1228
1229 if (inftl_bbt_write)
1230 end -= (INFTL_BBT_RESERVED_BLOCKS << this->phys_erase_shift);
1231
William Juulcfa460a2007-10-31 13:53:06 +01001232 buf = kmalloc(mtd->writesize, GFP_KERNEL);
Wolfgang Denk932394a2005-08-17 12:55:25 +02001233 if (!buf) {
1234 printk(KERN_ERR "DiskOnChip mediaheader kmalloc failed!\n");
1235 return 0;
1236 }
1237
William Juulcfa460a2007-10-31 13:53:06 +01001238 if (!find_media_headers(mtd, buf, "BNAND", 0))
1239 goto out;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001240 doc->mh1_page = doc->mh0_page + (4096 >> this->page_shift);
William Juulcfa460a2007-10-31 13:53:06 +01001241 mh = (struct INFTLMediaHeader *)buf;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001242
Scott Woodc45912d2008-10-24 16:20:43 -05001243 le32_to_cpus(&mh->NoOfBootImageBlocks);
1244 le32_to_cpus(&mh->NoOfBinaryPartitions);
1245 le32_to_cpus(&mh->NoOfBDTLPartitions);
1246 le32_to_cpus(&mh->BlockMultiplierBits);
1247 le32_to_cpus(&mh->FormatFlags);
1248 le32_to_cpus(&mh->PercentUsed);
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02001249
Wolfgang Denk932394a2005-08-17 12:55:25 +02001250 printk(KERN_INFO " bootRecordID = %s\n"
1251 " NoOfBootImageBlocks = %d\n"
1252 " NoOfBinaryPartitions = %d\n"
1253 " NoOfBDTLPartitions = %d\n"
1254 " BlockMultiplerBits = %d\n"
1255 " FormatFlgs = %d\n"
1256 " OsakVersion = %d.%d.%d.%d\n"
1257 " PercentUsed = %d\n",
1258 mh->bootRecordID, mh->NoOfBootImageBlocks,
1259 mh->NoOfBinaryPartitions,
1260 mh->NoOfBDTLPartitions,
1261 mh->BlockMultiplierBits, mh->FormatFlags,
1262 ((unsigned char *) &mh->OsakVersion)[0] & 0xf,
1263 ((unsigned char *) &mh->OsakVersion)[1] & 0xf,
1264 ((unsigned char *) &mh->OsakVersion)[2] & 0xf,
1265 ((unsigned char *) &mh->OsakVersion)[3] & 0xf,
1266 mh->PercentUsed);
Wolfgang Denk932394a2005-08-17 12:55:25 +02001267
1268 vshift = this->phys_erase_shift + mh->BlockMultiplierBits;
1269
1270 blocks = mtd->size >> vshift;
1271 if (blocks > 32768) {
1272 printk(KERN_ERR "BlockMultiplierBits=%d is inconsistent with device size. Aborting.\n", mh->BlockMultiplierBits);
1273 goto out;
1274 }
1275
1276 blocks = doc->chips_per_floor << (this->chip_shift - this->phys_erase_shift);
1277 if (inftl_bbt_write && (blocks > mtd->erasesize)) {
1278 printk(KERN_ERR "Writeable BBTs spanning more than one erase block are not yet supported. FIX ME!\n");
1279 goto out;
1280 }
1281
1282 /* Scan the partitions */
1283 for (i = 0; (i < 4); i++) {
1284 ip = &(mh->Partitions[i]);
Scott Woodc45912d2008-10-24 16:20:43 -05001285 le32_to_cpus(&ip->virtualUnits);
1286 le32_to_cpus(&ip->firstUnit);
1287 le32_to_cpus(&ip->lastUnit);
1288 le32_to_cpus(&ip->flags);
1289 le32_to_cpus(&ip->spareUnits);
1290 le32_to_cpus(&ip->Reserved0);
Wolfgang Denk932394a2005-08-17 12:55:25 +02001291
Wolfgang Denk932394a2005-08-17 12:55:25 +02001292 printk(KERN_INFO " PARTITION[%d] ->\n"
1293 " virtualUnits = %d\n"
1294 " firstUnit = %d\n"
1295 " lastUnit = %d\n"
1296 " flags = 0x%x\n"
1297 " spareUnits = %d\n",
1298 i, ip->virtualUnits, ip->firstUnit,
1299 ip->lastUnit, ip->flags,
1300 ip->spareUnits);
Wolfgang Denk932394a2005-08-17 12:55:25 +02001301
William Juulcfa460a2007-10-31 13:53:06 +01001302 if ((show_firmware_partition == 1) &&
1303 (i == 0) && (ip->firstUnit > 0)) {
Wolfgang Denk932394a2005-08-17 12:55:25 +02001304 parts[0].name = " DiskOnChip IPL / Media Header partition";
1305 parts[0].offset = 0;
1306 parts[0].size = mtd->erasesize * ip->firstUnit;
1307 numparts = 1;
1308 }
Wolfgang Denk932394a2005-08-17 12:55:25 +02001309
1310 if (ip->flags & INFTL_BINARY)
1311 parts[numparts].name = " DiskOnChip BDK partition";
1312 else
1313 parts[numparts].name = " DiskOnChip BDTL partition";
1314 parts[numparts].offset = ip->firstUnit << vshift;
1315 parts[numparts].size = (1 + ip->lastUnit - ip->firstUnit) << vshift;
1316 numparts++;
William Juulcfa460a2007-10-31 13:53:06 +01001317 if (ip->lastUnit > lastvunit)
1318 lastvunit = ip->lastUnit;
1319 if (ip->flags & INFTL_LAST)
1320 break;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001321 }
1322 lastvunit++;
1323 if ((lastvunit << vshift) < end) {
1324 parts[numparts].name = " DiskOnChip Remainder partition";
1325 parts[numparts].offset = lastvunit << vshift;
1326 parts[numparts].size = end - parts[numparts].offset;
1327 numparts++;
1328 }
1329 ret = numparts;
William Juulcfa460a2007-10-31 13:53:06 +01001330 out:
Wolfgang Denk932394a2005-08-17 12:55:25 +02001331 kfree(buf);
1332 return ret;
1333}
1334
1335static int __init nftl_scan_bbt(struct mtd_info *mtd)
1336{
1337 int ret, numparts;
1338 struct nand_chip *this = mtd->priv;
1339 struct doc_priv *doc = this->priv;
1340 struct mtd_partition parts[2];
1341
William Juulcfa460a2007-10-31 13:53:06 +01001342 memset((char *)parts, 0, sizeof(parts));
Wolfgang Denk932394a2005-08-17 12:55:25 +02001343 /* On NFTL, we have to find the media headers before we can read the
1344 BBTs, since they're stored in the media header eraseblocks. */
1345 numparts = nftl_partscan(mtd, parts);
William Juulcfa460a2007-10-31 13:53:06 +01001346 if (!numparts)
1347 return -EIO;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001348 this->bbt_td->options = NAND_BBT_ABSPAGE | NAND_BBT_8BIT |
1349 NAND_BBT_SAVECONTENT | NAND_BBT_WRITE |
1350 NAND_BBT_VERSION;
1351 this->bbt_td->veroffs = 7;
1352 this->bbt_td->pages[0] = doc->mh0_page + 1;
1353 if (doc->mh1_page != -1) {
1354 this->bbt_md->options = NAND_BBT_ABSPAGE | NAND_BBT_8BIT |
1355 NAND_BBT_SAVECONTENT | NAND_BBT_WRITE |
1356 NAND_BBT_VERSION;
1357 this->bbt_md->veroffs = 7;
1358 this->bbt_md->pages[0] = doc->mh1_page + 1;
1359 } else {
1360 this->bbt_md = NULL;
1361 }
1362
1363 /* It's safe to set bd=NULL below because NAND_BBT_CREATE is not set.
1364 At least as nand_bbt.c is currently written. */
1365 if ((ret = nand_scan_bbt(mtd, NULL)))
1366 return ret;
1367 add_mtd_device(mtd);
1368#ifdef CONFIG_MTD_PARTITIONS
1369 if (!no_autopart)
1370 add_mtd_partitions(mtd, parts, numparts);
1371#endif
1372 return 0;
1373}
1374
1375static int __init inftl_scan_bbt(struct mtd_info *mtd)
1376{
1377 int ret, numparts;
1378 struct nand_chip *this = mtd->priv;
1379 struct doc_priv *doc = this->priv;
1380 struct mtd_partition parts[5];
1381
1382 if (this->numchips > doc->chips_per_floor) {
1383 printk(KERN_ERR "Multi-floor INFTL devices not yet supported.\n");
1384 return -EIO;
1385 }
1386
1387 if (DoC_is_MillenniumPlus(doc)) {
1388 this->bbt_td->options = NAND_BBT_2BIT | NAND_BBT_ABSPAGE;
1389 if (inftl_bbt_write)
1390 this->bbt_td->options |= NAND_BBT_WRITE;
1391 this->bbt_td->pages[0] = 2;
1392 this->bbt_md = NULL;
1393 } else {
William Juulcfa460a2007-10-31 13:53:06 +01001394 this->bbt_td->options = NAND_BBT_LASTBLOCK | NAND_BBT_8BIT | NAND_BBT_VERSION;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001395 if (inftl_bbt_write)
1396 this->bbt_td->options |= NAND_BBT_WRITE;
1397 this->bbt_td->offs = 8;
1398 this->bbt_td->len = 8;
1399 this->bbt_td->veroffs = 7;
1400 this->bbt_td->maxblocks = INFTL_BBT_RESERVED_BLOCKS;
1401 this->bbt_td->reserved_block_code = 0x01;
1402 this->bbt_td->pattern = "MSYS_BBT";
1403
William Juulcfa460a2007-10-31 13:53:06 +01001404 this->bbt_md->options = NAND_BBT_LASTBLOCK | NAND_BBT_8BIT | NAND_BBT_VERSION;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001405 if (inftl_bbt_write)
1406 this->bbt_md->options |= NAND_BBT_WRITE;
1407 this->bbt_md->offs = 8;
1408 this->bbt_md->len = 8;
1409 this->bbt_md->veroffs = 7;
1410 this->bbt_md->maxblocks = INFTL_BBT_RESERVED_BLOCKS;
1411 this->bbt_md->reserved_block_code = 0x01;
1412 this->bbt_md->pattern = "TBB_SYSM";
1413 }
1414
1415 /* It's safe to set bd=NULL below because NAND_BBT_CREATE is not set.
1416 At least as nand_bbt.c is currently written. */
1417 if ((ret = nand_scan_bbt(mtd, NULL)))
1418 return ret;
William Juulcfa460a2007-10-31 13:53:06 +01001419 memset((char *)parts, 0, sizeof(parts));
Wolfgang Denk932394a2005-08-17 12:55:25 +02001420 numparts = inftl_partscan(mtd, parts);
1421 /* At least for now, require the INFTL Media Header. We could probably
1422 do without it for non-INFTL use, since all it gives us is
1423 autopartitioning, but I want to give it more thought. */
William Juulcfa460a2007-10-31 13:53:06 +01001424 if (!numparts)
1425 return -EIO;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001426 add_mtd_device(mtd);
1427#ifdef CONFIG_MTD_PARTITIONS
1428 if (!no_autopart)
1429 add_mtd_partitions(mtd, parts, numparts);
1430#endif
1431 return 0;
1432}
1433
1434static inline int __init doc2000_init(struct mtd_info *mtd)
1435{
1436 struct nand_chip *this = mtd->priv;
1437 struct doc_priv *doc = this->priv;
1438
Wolfgang Denk932394a2005-08-17 12:55:25 +02001439 this->read_byte = doc2000_read_byte;
1440 this->write_buf = doc2000_writebuf;
1441 this->read_buf = doc2000_readbuf;
1442 this->verify_buf = doc2000_verifybuf;
1443 this->scan_bbt = nftl_scan_bbt;
1444
1445 doc->CDSNControl = CDSN_CTRL_FLASH_IO | CDSN_CTRL_ECC_IO;
1446 doc2000_count_chips(mtd);
1447 mtd->name = "DiskOnChip 2000 (NFTL Model)";
1448 return (4 * doc->chips_per_floor);
1449}
1450
1451static inline int __init doc2001_init(struct mtd_info *mtd)
1452{
1453 struct nand_chip *this = mtd->priv;
1454 struct doc_priv *doc = this->priv;
1455
Wolfgang Denk932394a2005-08-17 12:55:25 +02001456 this->read_byte = doc2001_read_byte;
1457 this->write_buf = doc2001_writebuf;
1458 this->read_buf = doc2001_readbuf;
1459 this->verify_buf = doc2001_verifybuf;
1460
1461 ReadDOC(doc->virtadr, ChipID);
1462 ReadDOC(doc->virtadr, ChipID);
1463 ReadDOC(doc->virtadr, ChipID);
1464 if (ReadDOC(doc->virtadr, ChipID) != DOC_ChipID_DocMil) {
1465 /* It's not a Millennium; it's one of the newer
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02001466 DiskOnChip 2000 units with a similar ASIC.
Wolfgang Denk932394a2005-08-17 12:55:25 +02001467 Treat it like a Millennium, except that it
1468 can have multiple chips. */
1469 doc2000_count_chips(mtd);
1470 mtd->name = "DiskOnChip 2000 (INFTL Model)";
1471 this->scan_bbt = inftl_scan_bbt;
1472 return (4 * doc->chips_per_floor);
1473 } else {
1474 /* Bog-standard Millennium */
1475 doc->chips_per_floor = 1;
1476 mtd->name = "DiskOnChip Millennium";
1477 this->scan_bbt = nftl_scan_bbt;
1478 return 1;
1479 }
1480}
1481
1482static inline int __init doc2001plus_init(struct mtd_info *mtd)
1483{
1484 struct nand_chip *this = mtd->priv;
1485 struct doc_priv *doc = this->priv;
1486
Wolfgang Denk932394a2005-08-17 12:55:25 +02001487 this->read_byte = doc2001plus_read_byte;
1488 this->write_buf = doc2001plus_writebuf;
1489 this->read_buf = doc2001plus_readbuf;
1490 this->verify_buf = doc2001plus_verifybuf;
1491 this->scan_bbt = inftl_scan_bbt;
William Juulcfa460a2007-10-31 13:53:06 +01001492 this->cmd_ctrl = NULL;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001493 this->select_chip = doc2001plus_select_chip;
1494 this->cmdfunc = doc2001plus_command;
William Juulcfa460a2007-10-31 13:53:06 +01001495 this->ecc.hwctl = doc2001plus_enable_hwecc;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001496
1497 doc->chips_per_floor = 1;
1498 mtd->name = "DiskOnChip Millennium Plus";
1499
1500 return 1;
1501}
1502
William Juulcfa460a2007-10-31 13:53:06 +01001503static int __init doc_probe(unsigned long physadr)
Wolfgang Denk932394a2005-08-17 12:55:25 +02001504{
1505 unsigned char ChipID;
1506 struct mtd_info *mtd;
1507 struct nand_chip *nand;
1508 struct doc_priv *doc;
1509 void __iomem *virtadr;
1510 unsigned char save_control;
1511 unsigned char tmp, tmpb, tmpc;
1512 int reg, len, numchips;
1513 int ret = 0;
1514
1515 virtadr = ioremap(physadr, DOC_IOREMAP_LEN);
1516 if (!virtadr) {
1517 printk(KERN_ERR "Diskonchip ioremap failed: 0x%x bytes at 0x%lx\n", DOC_IOREMAP_LEN, physadr);
1518 return -EIO;
1519 }
1520
1521 /* It's not possible to cleanly detect the DiskOnChip - the
1522 * bootup procedure will put the device into reset mode, and
1523 * it's not possible to talk to it without actually writing
1524 * to the DOCControl register. So we store the current contents
1525 * of the DOCControl register's location, in case we later decide
1526 * that it's not a DiskOnChip, and want to put it back how we
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02001527 * found it.
Wolfgang Denk932394a2005-08-17 12:55:25 +02001528 */
1529 save_control = ReadDOC(virtadr, DOCControl);
1530
1531 /* Reset the DiskOnChip ASIC */
William Juulcfa460a2007-10-31 13:53:06 +01001532 WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_RESET, virtadr, DOCControl);
1533 WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_RESET, virtadr, DOCControl);
Wolfgang Denk932394a2005-08-17 12:55:25 +02001534
1535 /* Enable the DiskOnChip ASIC */
William Juulcfa460a2007-10-31 13:53:06 +01001536 WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_NORMAL, virtadr, DOCControl);
1537 WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_NORMAL, virtadr, DOCControl);
Wolfgang Denk932394a2005-08-17 12:55:25 +02001538
1539 ChipID = ReadDOC(virtadr, ChipID);
1540
William Juulcfa460a2007-10-31 13:53:06 +01001541 switch (ChipID) {
Wolfgang Denk932394a2005-08-17 12:55:25 +02001542 case DOC_ChipID_Doc2k:
1543 reg = DoC_2k_ECCStatus;
1544 break;
1545 case DOC_ChipID_DocMil:
1546 reg = DoC_ECCConf;
1547 break;
1548 case DOC_ChipID_DocMilPlus16:
1549 case DOC_ChipID_DocMilPlus32:
1550 case 0:
1551 /* Possible Millennium Plus, need to do more checks */
1552 /* Possibly release from power down mode */
1553 for (tmp = 0; (tmp < 4); tmp++)
1554 ReadDOC(virtadr, Mplus_Power);
1555
1556 /* Reset the Millennium Plus ASIC */
William Juulcfa460a2007-10-31 13:53:06 +01001557 tmp = DOC_MODE_RESET | DOC_MODE_MDWREN | DOC_MODE_RST_LAT | DOC_MODE_BDECT;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001558 WriteDOC(tmp, virtadr, Mplus_DOCControl);
1559 WriteDOC(~tmp, virtadr, Mplus_CtrlConfirm);
1560
1561 mdelay(1);
1562 /* Enable the Millennium Plus ASIC */
William Juulcfa460a2007-10-31 13:53:06 +01001563 tmp = DOC_MODE_NORMAL | DOC_MODE_MDWREN | DOC_MODE_RST_LAT | DOC_MODE_BDECT;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001564 WriteDOC(tmp, virtadr, Mplus_DOCControl);
1565 WriteDOC(~tmp, virtadr, Mplus_CtrlConfirm);
1566 mdelay(1);
1567
1568 ChipID = ReadDOC(virtadr, ChipID);
1569
1570 switch (ChipID) {
1571 case DOC_ChipID_DocMilPlus16:
1572 reg = DoC_Mplus_Toggle;
1573 break;
1574 case DOC_ChipID_DocMilPlus32:
1575 printk(KERN_ERR "DiskOnChip Millennium Plus 32MB is not supported, ignoring.\n");
1576 default:
1577 ret = -ENODEV;
1578 goto notfound;
1579 }
1580 break;
1581
1582 default:
1583 ret = -ENODEV;
1584 goto notfound;
1585 }
1586 /* Check the TOGGLE bit in the ECC register */
William Juulcfa460a2007-10-31 13:53:06 +01001587 tmp = ReadDOC_(virtadr, reg) & DOC_TOGGLE_BIT;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001588 tmpb = ReadDOC_(virtadr, reg) & DOC_TOGGLE_BIT;
1589 tmpc = ReadDOC_(virtadr, reg) & DOC_TOGGLE_BIT;
1590 if ((tmp == tmpb) || (tmp != tmpc)) {
1591 printk(KERN_WARNING "Possible DiskOnChip at 0x%lx failed TOGGLE test, dropping.\n", physadr);
1592 ret = -ENODEV;
1593 goto notfound;
1594 }
1595
1596 for (mtd = doclist; mtd; mtd = doc->nextdoc) {
1597 unsigned char oldval;
1598 unsigned char newval;
1599 nand = mtd->priv;
1600 doc = nand->priv;
1601 /* Use the alias resolution register to determine if this is
1602 in fact the same DOC aliased to a new address. If writes
1603 to one chip's alias resolution register change the value on
1604 the other chip, they're the same chip. */
1605 if (ChipID == DOC_ChipID_DocMilPlus16) {
1606 oldval = ReadDOC(doc->virtadr, Mplus_AliasResolution);
1607 newval = ReadDOC(virtadr, Mplus_AliasResolution);
1608 } else {
1609 oldval = ReadDOC(doc->virtadr, AliasResolution);
1610 newval = ReadDOC(virtadr, AliasResolution);
1611 }
1612 if (oldval != newval)
1613 continue;
1614 if (ChipID == DOC_ChipID_DocMilPlus16) {
1615 WriteDOC(~newval, virtadr, Mplus_AliasResolution);
1616 oldval = ReadDOC(doc->virtadr, Mplus_AliasResolution);
William Juul5e1dae52007-11-09 13:32:30 +01001617 WriteDOC(newval, virtadr, Mplus_AliasResolution); /* restore it */
Wolfgang Denk932394a2005-08-17 12:55:25 +02001618 } else {
1619 WriteDOC(~newval, virtadr, AliasResolution);
1620 oldval = ReadDOC(doc->virtadr, AliasResolution);
William Juul5e1dae52007-11-09 13:32:30 +01001621 WriteDOC(newval, virtadr, AliasResolution); /* restore it */
Wolfgang Denk932394a2005-08-17 12:55:25 +02001622 }
1623 newval = ~newval;
1624 if (oldval == newval) {
1625 printk(KERN_DEBUG "Found alias of DOC at 0x%lx to 0x%lx\n", doc->physadr, physadr);
1626 goto notfound;
1627 }
1628 }
1629
1630 printk(KERN_NOTICE "DiskOnChip found at 0x%lx\n", physadr);
1631
1632 len = sizeof(struct mtd_info) +
William Juulcfa460a2007-10-31 13:53:06 +01001633 sizeof(struct nand_chip) + sizeof(struct doc_priv) + (2 * sizeof(struct nand_bbt_descr));
1634 mtd = kzalloc(len, GFP_KERNEL);
Wolfgang Denk932394a2005-08-17 12:55:25 +02001635 if (!mtd) {
1636 printk(KERN_ERR "DiskOnChip kmalloc (%d bytes) failed!\n", len);
1637 ret = -ENOMEM;
1638 goto fail;
1639 }
Wolfgang Denk932394a2005-08-17 12:55:25 +02001640
1641 nand = (struct nand_chip *) (mtd + 1);
1642 doc = (struct doc_priv *) (nand + 1);
1643 nand->bbt_td = (struct nand_bbt_descr *) (doc + 1);
1644 nand->bbt_md = nand->bbt_td + 1;
1645
1646 mtd->priv = nand;
1647 mtd->owner = THIS_MODULE;
1648
1649 nand->priv = doc;
1650 nand->select_chip = doc200x_select_chip;
William Juulcfa460a2007-10-31 13:53:06 +01001651 nand->cmd_ctrl = doc200x_hwcontrol;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001652 nand->dev_ready = doc200x_dev_ready;
1653 nand->waitfunc = doc200x_wait;
1654 nand->block_bad = doc200x_block_bad;
William Juulcfa460a2007-10-31 13:53:06 +01001655 nand->ecc.hwctl = doc200x_enable_hwecc;
1656 nand->ecc.calculate = doc200x_calculate_ecc;
1657 nand->ecc.correct = doc200x_correct_data;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001658
William Juulcfa460a2007-10-31 13:53:06 +01001659 nand->ecc.layout = &doc200x_oobinfo;
1660 nand->ecc.mode = NAND_ECC_HW_SYNDROME;
1661 nand->ecc.size = 512;
1662 nand->ecc.bytes = 6;
1663 nand->options = NAND_USE_FLASH_BBT;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001664
1665 doc->physadr = physadr;
1666 doc->virtadr = virtadr;
1667 doc->ChipID = ChipID;
1668 doc->curfloor = -1;
1669 doc->curchip = -1;
1670 doc->mh0_page = -1;
1671 doc->mh1_page = -1;
1672 doc->nextdoc = doclist;
1673
1674 if (ChipID == DOC_ChipID_Doc2k)
1675 numchips = doc2000_init(mtd);
1676 else if (ChipID == DOC_ChipID_DocMilPlus16)
1677 numchips = doc2001plus_init(mtd);
1678 else
1679 numchips = doc2001_init(mtd);
1680
1681 if ((ret = nand_scan(mtd, numchips))) {
1682 /* DBB note: i believe nand_release is necessary here, as
1683 buffers may have been allocated in nand_base. Check with
1684 Thomas. FIX ME! */
1685 /* nand_release will call del_mtd_device, but we haven't yet
1686 added it. This is handled without incident by
1687 del_mtd_device, as far as I can tell. */
1688 nand_release(mtd);
1689 kfree(mtd);
1690 goto fail;
1691 }
1692
1693 /* Success! */
1694 doclist = mtd;
1695 return 0;
1696
William Juulcfa460a2007-10-31 13:53:06 +01001697 notfound:
Wolfgang Denk932394a2005-08-17 12:55:25 +02001698 /* Put back the contents of the DOCControl register, in case it's not
1699 actually a DiskOnChip. */
1700 WriteDOC(save_control, virtadr, DOCControl);
William Juulcfa460a2007-10-31 13:53:06 +01001701 fail:
Wolfgang Denk932394a2005-08-17 12:55:25 +02001702 iounmap(virtadr);
1703 return ret;
1704}
1705
1706static void release_nanddoc(void)
1707{
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001708 struct mtd_info *mtd, *nextmtd;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001709 struct nand_chip *nand;
1710 struct doc_priv *doc;
1711
1712 for (mtd = doclist; mtd; mtd = nextmtd) {
1713 nand = mtd->priv;
1714 doc = nand->priv;
1715
1716 nextmtd = doc->nextdoc;
1717 nand_release(mtd);
1718 iounmap(doc->virtadr);
1719 kfree(mtd);
1720 }
1721}
1722
1723static int __init init_nanddoc(void)
1724{
1725 int i, ret = 0;
1726
1727 /* We could create the decoder on demand, if memory is a concern.
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02001728 * This way we have it handy, if an error happens
Wolfgang Denk932394a2005-08-17 12:55:25 +02001729 *
1730 * Symbolsize is 10 (bits)
1731 * Primitve polynomial is x^10+x^3+1
1732 * first consecutive root is 510
1733 * primitve element to generate roots = 1
1734 * generator polinomial degree = 4
1735 */
1736 rs_decoder = init_rs(10, 0x409, FCR, 1, NROOTS);
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001737 if (!rs_decoder) {
William Juulcfa460a2007-10-31 13:53:06 +01001738 printk(KERN_ERR "DiskOnChip: Could not create a RS decoder\n");
Wolfgang Denk932394a2005-08-17 12:55:25 +02001739 return -ENOMEM;
1740 }
1741
1742 if (doc_config_location) {
1743 printk(KERN_INFO "Using configured DiskOnChip probe address 0x%lx\n", doc_config_location);
1744 ret = doc_probe(doc_config_location);
1745 if (ret < 0)
1746 goto outerr;
1747 } else {
William Juulcfa460a2007-10-31 13:53:06 +01001748 for (i = 0; (doc_locations[i] != 0xffffffff); i++) {
Wolfgang Denk932394a2005-08-17 12:55:25 +02001749 doc_probe(doc_locations[i]);
1750 }
1751 }
1752 /* No banner message any more. Print a message if no DiskOnChip
1753 found, so the user knows we at least tried. */
1754 if (!doclist) {
1755 printk(KERN_INFO "No valid DiskOnChip devices found\n");
1756 ret = -ENODEV;
1757 goto outerr;
1758 }
1759 return 0;
William Juulcfa460a2007-10-31 13:53:06 +01001760 outerr:
Wolfgang Denk932394a2005-08-17 12:55:25 +02001761 free_rs(rs_decoder);
1762 return ret;
1763}
1764
1765static void __exit cleanup_nanddoc(void)
1766{
1767 /* Cleanup the nand/DoC resources */
1768 release_nanddoc();
1769
1770 /* Free the reed solomon resources */
1771 if (rs_decoder) {
1772 free_rs(rs_decoder);
1773 }
1774}
1775
1776module_init(init_nanddoc);
1777module_exit(cleanup_nanddoc);
1778
1779MODULE_LICENSE("GPL");
1780MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
1781MODULE_DESCRIPTION("M-Systems DiskOnChip 2000, Millennium and Millennium Plus device driver\n");
Marian Balakowicz6db39702006-04-08 19:08:06 +02001782#endif