blob: e5800146cf33a9c1c2fc452846c3931cfa79030d [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 */
19
20#include <linux/kernel.h>
21#include <linux/init.h>
22#include <linux/sched.h>
23#include <linux/delay.h>
24#include <linux/rslib.h>
25#include <linux/moduleparam.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <asm/io.h>
28
29#include <linux/mtd/mtd.h>
30#include <linux/mtd/nand.h>
31#include <linux/mtd/doc2000.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/mtd/partitions.h>
33#include <linux/mtd/inftl.h>
Paul Gortmakera0e5cc52011-07-03 15:17:31 -040034#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
36/* Where to look for the devices? */
Thomas Gleixner651078b2005-01-31 20:36:46 +000037#ifndef CONFIG_MTD_NAND_DISKONCHIP_PROBE_ADDRESS
38#define CONFIG_MTD_NAND_DISKONCHIP_PROBE_ADDRESS 0
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#endif
40
Sachin Kamat14a95b8a2013-08-12 15:10:47 +053041static unsigned long doc_locations[] __initdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#if defined (__alpha__) || defined(__i386__) || defined(__x86_64__)
Thomas Gleixner651078b2005-01-31 20:36:46 +000043#ifdef CONFIG_MTD_NAND_DISKONCHIP_PROBE_HIGH
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000044 0xfffc8000, 0xfffca000, 0xfffcc000, 0xfffce000,
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 0xfffd0000, 0xfffd2000, 0xfffd4000, 0xfffd6000,
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000046 0xfffd8000, 0xfffda000, 0xfffdc000, 0xfffde000,
47 0xfffe0000, 0xfffe2000, 0xfffe4000, 0xfffe6000,
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 0xfffe8000, 0xfffea000, 0xfffec000, 0xfffee000,
Michael Opdenacker9d403492013-07-08 06:39:20 +020049#else
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000050 0xc8000, 0xca000, 0xcc000, 0xce000,
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 0xd0000, 0xd2000, 0xd4000, 0xd6000,
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000052 0xd8000, 0xda000, 0xdc000, 0xde000,
53 0xe0000, 0xe2000, 0xe4000, 0xe6000,
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 0xe8000, 0xea000, 0xec000, 0xee000,
Michael Opdenacker9d403492013-07-08 06:39:20 +020055#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#endif
57 0xffffffff };
58
59static struct mtd_info *doclist = NULL;
60
61struct doc_priv {
62 void __iomem *virtadr;
63 unsigned long physadr;
64 u_char ChipID;
65 u_char CDSNControl;
David Woodhousee0c7d762006-05-13 18:07:53 +010066 int chips_per_floor; /* The number of chips detected on each floor */
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 int curfloor;
68 int curchip;
69 int mh0_page;
70 int mh1_page;
71 struct mtd_info *nextdoc;
Brian Norrisd24fe0c2015-02-28 02:13:10 -080072
73 /* Handle the last stage of initialization (BBT scan, partitioning) */
74 int (*late_init)(struct mtd_info *mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075};
76
Linus Torvalds1da177e2005-04-16 15:20:36 -070077/* This is the syndrome computed by the HW ecc generator upon reading an empty
78 page, one with all 0xff for data and stored ecc code. */
79static u_char empty_read_syndrome[6] = { 0x26, 0xff, 0x6d, 0x47, 0x73, 0x7a };
David Woodhousee0c7d762006-05-13 18:07:53 +010080
Linus Torvalds1da177e2005-04-16 15:20:36 -070081/* This is the ecc value computed by the HW ecc generator upon writing an empty
82 page, one with all 0xff for data. */
83static u_char empty_write_ecc[6] = { 0x4b, 0x00, 0xe2, 0x0e, 0x93, 0xf7 };
84
85#define INFTL_BBT_RESERVED_BLOCKS 4
86
87#define DoC_is_MillenniumPlus(doc) ((doc)->ChipID == DOC_ChipID_DocMilPlus16 || (doc)->ChipID == DOC_ChipID_DocMilPlus32)
88#define DoC_is_Millennium(doc) ((doc)->ChipID == DOC_ChipID_DocMil)
89#define DoC_is_2000(doc) ((doc)->ChipID == DOC_ChipID_Doc2k)
90
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +020091static void doc200x_hwcontrol(struct mtd_info *mtd, int cmd,
92 unsigned int bitmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093static void doc200x_select_chip(struct mtd_info *mtd, int chip);
94
David Woodhousee0c7d762006-05-13 18:07:53 +010095static int debug = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096module_param(debug, int, 0);
97
David Woodhousee0c7d762006-05-13 18:07:53 +010098static int try_dword = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099module_param(try_dword, int, 0);
100
David Woodhousee0c7d762006-05-13 18:07:53 +0100101static int no_ecc_failures = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102module_param(no_ecc_failures, int, 0);
103
David Woodhousee0c7d762006-05-13 18:07:53 +0100104static int no_autopart = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105module_param(no_autopart, int, 0);
Dan Brown1a78ff62005-03-29 21:57:48 +0100106
David Woodhousee0c7d762006-05-13 18:07:53 +0100107static int show_firmware_partition = 0;
Dan Brown1a78ff62005-03-29 21:57:48 +0100108module_param(show_firmware_partition, int, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
Robert P. J. Day89e2bf62007-03-07 18:33:25 -0500110#ifdef CONFIG_MTD_NAND_DISKONCHIP_BBTWRITE
David Woodhousee0c7d762006-05-13 18:07:53 +0100111static int inftl_bbt_write = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112#else
David Woodhousee0c7d762006-05-13 18:07:53 +0100113static int inftl_bbt_write = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114#endif
115module_param(inftl_bbt_write, int, 0);
116
Thomas Gleixner651078b2005-01-31 20:36:46 +0000117static unsigned long doc_config_location = CONFIG_MTD_NAND_DISKONCHIP_PROBE_ADDRESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118module_param(doc_config_location, ulong, 0);
119MODULE_PARM_DESC(doc_config_location, "Physical memory address at which to probe for DiskOnChip");
120
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121/* Sector size for HW ECC */
122#define SECTOR_SIZE 512
123/* The sector bytes are packed into NB_DATA 10 bit words */
124#define NB_DATA (((SECTOR_SIZE + 1) * 8 + 6) / 10)
125/* Number of roots */
126#define NROOTS 4
127/* First consective root */
128#define FCR 510
129/* Number of symbols */
130#define NN 1023
131
132/* the Reed Solomon control structure */
133static struct rs_control *rs_decoder;
134
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000135/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 * The HW decoder in the DoC ASIC's provides us a error syndrome,
Brian Norris7854d3f2011-06-23 14:12:08 -0700137 * which we must convert to a standard syndrome usable by the generic
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 * Reed-Solomon library code.
139 *
140 * Fabrice Bellard figured this out in the old docecc code. I added
141 * some comments, improved a minor bit and converted it to make use
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300142 * of the generic Reed-Solomon library. tglx
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100144static int doc_ecc_decode(struct rs_control *rs, uint8_t *data, uint8_t *ecc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145{
146 int i, j, nerr, errpos[8];
147 uint8_t parity;
148 uint16_t ds[4], s[5], tmp, errval[8], syn[4];
149
Mark Warec9fb6772010-05-27 11:45:39 +1000150 memset(syn, 0, sizeof(syn));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 /* Convert the ecc bytes into words */
152 ds[0] = ((ecc[4] & 0xff) >> 0) | ((ecc[5] & 0x03) << 8);
153 ds[1] = ((ecc[5] & 0xfc) >> 2) | ((ecc[2] & 0x0f) << 6);
154 ds[2] = ((ecc[2] & 0xf0) >> 4) | ((ecc[3] & 0x3f) << 4);
155 ds[3] = ((ecc[3] & 0xc0) >> 6) | ((ecc[0] & 0xff) << 2);
156 parity = ecc[1];
157
Brian Norris7854d3f2011-06-23 14:12:08 -0700158 /* Initialize the syndrome buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 for (i = 0; i < NROOTS; i++)
160 s[i] = ds[0];
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000161 /*
162 * Evaluate
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 * s[i] = ds[3]x^3 + ds[2]x^2 + ds[1]x^1 + ds[0]
164 * where x = alpha^(FCR + i)
165 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100166 for (j = 1; j < NROOTS; j++) {
167 if (ds[j] == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 continue;
169 tmp = rs->index_of[ds[j]];
David Woodhousee0c7d762006-05-13 18:07:53 +0100170 for (i = 0; i < NROOTS; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 s[i] ^= rs->alpha_to[rs_modnn(rs, tmp + (FCR + i) * j)];
172 }
173
Mark Warec9fb6772010-05-27 11:45:39 +1000174 /* Calc syn[i] = s[i] / alpha^(v + i) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 for (i = 0; i < NROOTS; i++) {
Mark Warec9fb6772010-05-27 11:45:39 +1000176 if (s[i])
David Woodhousee0c7d762006-05-13 18:07:53 +0100177 syn[i] = rs_modnn(rs, rs->index_of[s[i]] + (NN - FCR - i));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 }
179 /* Call the decoder library */
180 nerr = decode_rs16(rs, NULL, NULL, 1019, syn, 0, errpos, 0, errval);
181
182 /* Incorrectable errors ? */
183 if (nerr < 0)
184 return nerr;
185
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000186 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 * Correct the errors. The bitpositions are a bit of magic,
188 * but they are given by the design of the de/encoder circuit
189 * in the DoC ASIC's.
190 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100191 for (i = 0; i < nerr; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 int index, bitpos, pos = 1015 - errpos[i];
193 uint8_t val;
194 if (pos >= NB_DATA && pos < 1019)
195 continue;
196 if (pos < NB_DATA) {
197 /* extract bit position (MSB first) */
198 pos = 10 * (NB_DATA - 1 - pos) - 6;
199 /* now correct the following 10 bits. At most two bytes
200 can be modified since pos is even */
201 index = (pos >> 3) ^ 1;
202 bitpos = pos & 7;
David Woodhousee0c7d762006-05-13 18:07:53 +0100203 if ((index >= 0 && index < SECTOR_SIZE) || index == (SECTOR_SIZE + 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 val = (uint8_t) (errval[i] >> (2 + bitpos));
205 parity ^= val;
206 if (index < SECTOR_SIZE)
207 data[index] ^= val;
208 }
209 index = ((pos >> 3) + 1) ^ 1;
210 bitpos = (bitpos + 10) & 7;
211 if (bitpos == 0)
212 bitpos = 8;
David Woodhousee0c7d762006-05-13 18:07:53 +0100213 if ((index >= 0 && index < SECTOR_SIZE) || index == (SECTOR_SIZE + 1)) {
214 val = (uint8_t) (errval[i] << (8 - bitpos));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 parity ^= val;
216 if (index < SECTOR_SIZE)
217 data[index] ^= val;
218 }
219 }
220 }
221 /* If the parity is wrong, no rescue possible */
Jörn Engeleb684502007-10-20 23:16:32 +0200222 return parity ? -EBADMSG : nerr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223}
224
225static void DoC_Delay(struct doc_priv *doc, unsigned short cycles)
226{
227 volatile char dummy;
228 int i;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000229
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 for (i = 0; i < cycles; i++) {
231 if (DoC_is_Millennium(doc))
232 dummy = ReadDOC(doc->virtadr, NOP);
233 else if (DoC_is_MillenniumPlus(doc))
234 dummy = ReadDOC(doc->virtadr, Mplus_NOP);
235 else
236 dummy = ReadDOC(doc->virtadr, DOCStatus);
237 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000238
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239}
240
241#define CDSN_CTRL_FR_B_MASK (CDSN_CTRL_FR_B0 | CDSN_CTRL_FR_B1)
242
243/* DOC_WaitReady: Wait for RDY line to be asserted by the flash chip */
244static int _DoC_WaitReady(struct doc_priv *doc)
245{
David Woodhousee0c7d762006-05-13 18:07:53 +0100246 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 unsigned long timeo = jiffies + (HZ * 10);
248
David Woodhousee0c7d762006-05-13 18:07:53 +0100249 if (debug)
250 printk("_DoC_WaitReady...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 /* Out-of-line routine to wait for chip response */
252 if (DoC_is_MillenniumPlus(doc)) {
253 while ((ReadDOC(docptr, Mplus_FlashControl) & CDSN_CTRL_FR_B_MASK) != CDSN_CTRL_FR_B_MASK) {
254 if (time_after(jiffies, timeo)) {
255 printk("_DoC_WaitReady timed out.\n");
256 return -EIO;
257 }
258 udelay(1);
259 cond_resched();
260 }
261 } else {
262 while (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B)) {
263 if (time_after(jiffies, timeo)) {
264 printk("_DoC_WaitReady timed out.\n");
265 return -EIO;
266 }
267 udelay(1);
268 cond_resched();
269 }
270 }
271
272 return 0;
273}
274
275static inline int DoC_WaitReady(struct doc_priv *doc)
276{
David Woodhousee0c7d762006-05-13 18:07:53 +0100277 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 int ret = 0;
279
280 if (DoC_is_MillenniumPlus(doc)) {
281 DoC_Delay(doc, 4);
282
283 if ((ReadDOC(docptr, Mplus_FlashControl) & CDSN_CTRL_FR_B_MASK) != CDSN_CTRL_FR_B_MASK)
284 /* Call the out-of-line routine to wait */
285 ret = _DoC_WaitReady(doc);
286 } else {
287 DoC_Delay(doc, 4);
288
289 if (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B))
290 /* Call the out-of-line routine to wait */
291 ret = _DoC_WaitReady(doc);
292 DoC_Delay(doc, 2);
293 }
294
David Woodhousee0c7d762006-05-13 18:07:53 +0100295 if (debug)
296 printk("DoC_WaitReady OK\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 return ret;
298}
299
300static void doc2000_write_byte(struct mtd_info *mtd, u_char datum)
301{
302 struct nand_chip *this = mtd->priv;
303 struct doc_priv *doc = this->priv;
David Woodhousee0c7d762006-05-13 18:07:53 +0100304 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
David Woodhousee0c7d762006-05-13 18:07:53 +0100306 if (debug)
307 printk("write_byte %02x\n", datum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 WriteDOC(datum, docptr, CDSNSlowIO);
309 WriteDOC(datum, docptr, 2k_CDSN_IO);
310}
311
312static u_char doc2000_read_byte(struct mtd_info *mtd)
313{
314 struct nand_chip *this = mtd->priv;
315 struct doc_priv *doc = this->priv;
David Woodhousee0c7d762006-05-13 18:07:53 +0100316 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 u_char ret;
318
319 ReadDOC(docptr, CDSNSlowIO);
320 DoC_Delay(doc, 2);
321 ret = ReadDOC(docptr, 2k_CDSN_IO);
David Woodhousee0c7d762006-05-13 18:07:53 +0100322 if (debug)
323 printk("read_byte returns %02x\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 return ret;
325}
326
David Woodhousee0c7d762006-05-13 18:07:53 +0100327static void doc2000_writebuf(struct mtd_info *mtd, const u_char *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328{
329 struct nand_chip *this = mtd->priv;
330 struct doc_priv *doc = this->priv;
David Woodhousee0c7d762006-05-13 18:07:53 +0100331 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 int i;
David Woodhousee0c7d762006-05-13 18:07:53 +0100333 if (debug)
334 printk("writebuf of %d bytes: ", len);
335 for (i = 0; i < len; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 WriteDOC_(buf[i], docptr, DoC_2k_CDSN_IO + i);
337 if (debug && i < 16)
338 printk("%02x ", buf[i]);
339 }
David Woodhousee0c7d762006-05-13 18:07:53 +0100340 if (debug)
341 printk("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342}
343
David Woodhousee0c7d762006-05-13 18:07:53 +0100344static void doc2000_readbuf(struct mtd_info *mtd, u_char *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345{
346 struct nand_chip *this = mtd->priv;
347 struct doc_priv *doc = this->priv;
David Woodhousee0c7d762006-05-13 18:07:53 +0100348 void __iomem *docptr = doc->virtadr;
349 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
David Woodhousee0c7d762006-05-13 18:07:53 +0100351 if (debug)
352 printk("readbuf of %d bytes: ", len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
David Woodhousee0c7d762006-05-13 18:07:53 +0100354 for (i = 0; i < len; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 buf[i] = ReadDOC(docptr, 2k_CDSN_IO + i);
356 }
357}
358
David Woodhousee0c7d762006-05-13 18:07:53 +0100359static void doc2000_readbuf_dword(struct mtd_info *mtd, u_char *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360{
361 struct nand_chip *this = mtd->priv;
362 struct doc_priv *doc = this->priv;
David Woodhousee0c7d762006-05-13 18:07:53 +0100363 void __iomem *docptr = doc->virtadr;
364 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
David Woodhousee0c7d762006-05-13 18:07:53 +0100366 if (debug)
367 printk("readbuf_dword of %d bytes: ", len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
David Woodhousee0c7d762006-05-13 18:07:53 +0100369 if (unlikely((((unsigned long)buf) | len) & 3)) {
370 for (i = 0; i < len; i++) {
371 *(uint8_t *) (&buf[i]) = ReadDOC(docptr, 2k_CDSN_IO + i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 }
373 } else {
David Woodhousee0c7d762006-05-13 18:07:53 +0100374 for (i = 0; i < len; i += 4) {
375 *(uint32_t *) (&buf[i]) = readl(docptr + DoC_2k_CDSN_IO + i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 }
377 }
378}
379
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380static uint16_t __init doc200x_ident_chip(struct mtd_info *mtd, int nr)
381{
382 struct nand_chip *this = mtd->priv;
383 struct doc_priv *doc = this->priv;
384 uint16_t ret;
385
386 doc200x_select_chip(mtd, nr);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200387 doc200x_hwcontrol(mtd, NAND_CMD_READID,
388 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
389 doc200x_hwcontrol(mtd, 0, NAND_CTRL_ALE | NAND_CTRL_CHANGE);
390 doc200x_hwcontrol(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000391
Lucas De Marchie9c54992011-04-26 23:28:26 -0700392 /* We can't use dev_ready here, but at least we wait for the
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000393 * command to complete
Thomas Gleixnerdfd61292005-02-22 21:48:25 +0000394 */
395 udelay(50);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000396
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 ret = this->read_byte(mtd) << 8;
398 ret |= this->read_byte(mtd);
399
400 if (doc->ChipID == DOC_ChipID_Doc2k && try_dword && !nr) {
401 /* First chip probe. See if we get same results by 32-bit access */
402 union {
403 uint32_t dword;
404 uint8_t byte[4];
405 } ident;
406 void __iomem *docptr = doc->virtadr;
407
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200408 doc200x_hwcontrol(mtd, NAND_CMD_READID,
409 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
410 doc200x_hwcontrol(mtd, 0, NAND_CTRL_ALE | NAND_CTRL_CHANGE);
411 doc200x_hwcontrol(mtd, NAND_CMD_NONE,
412 NAND_NCE | NAND_CTRL_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
Thomas Gleixnerdfd61292005-02-22 21:48:25 +0000414 udelay(50);
415
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 ident.dword = readl(docptr + DoC_2k_CDSN_IO);
417 if (((ident.byte[0] << 8) | ident.byte[1]) == ret) {
418 printk(KERN_INFO "DiskOnChip 2000 responds to DWORD access\n");
419 this->read_buf = &doc2000_readbuf_dword;
420 }
421 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000422
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 return ret;
424}
425
426static void __init doc2000_count_chips(struct mtd_info *mtd)
427{
428 struct nand_chip *this = mtd->priv;
429 struct doc_priv *doc = this->priv;
430 uint16_t mfrid;
431 int i;
432
433 /* Max 4 chips per floor on DiskOnChip 2000 */
434 doc->chips_per_floor = 4;
435
436 /* Find out what the first chip is */
437 mfrid = doc200x_ident_chip(mtd, 0);
438
439 /* Find how many chips in each floor. */
440 for (i = 1; i < 4; i++) {
441 if (doc200x_ident_chip(mtd, i) != mfrid)
442 break;
443 }
444 doc->chips_per_floor = i;
445 printk(KERN_DEBUG "Detected %d chips per floor.\n", i);
446}
447
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200448static int doc200x_wait(struct mtd_info *mtd, struct nand_chip *this)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449{
450 struct doc_priv *doc = this->priv;
451
452 int status;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000453
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 DoC_WaitReady(doc);
455 this->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
456 DoC_WaitReady(doc);
457 status = (int)this->read_byte(mtd);
458
459 return status;
460}
461
462static void doc2001_write_byte(struct mtd_info *mtd, u_char datum)
463{
464 struct nand_chip *this = mtd->priv;
465 struct doc_priv *doc = this->priv;
David Woodhousee0c7d762006-05-13 18:07:53 +0100466 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
468 WriteDOC(datum, docptr, CDSNSlowIO);
469 WriteDOC(datum, docptr, Mil_CDSN_IO);
470 WriteDOC(datum, docptr, WritePipeTerm);
471}
472
473static u_char doc2001_read_byte(struct mtd_info *mtd)
474{
475 struct nand_chip *this = mtd->priv;
476 struct doc_priv *doc = this->priv;
David Woodhousee0c7d762006-05-13 18:07:53 +0100477 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
479 //ReadDOC(docptr, CDSNSlowIO);
480 /* 11.4.5 -- delay twice to allow extended length cycle */
481 DoC_Delay(doc, 2);
482 ReadDOC(docptr, ReadPipeInit);
483 //return ReadDOC(docptr, Mil_CDSN_IO);
484 return ReadDOC(docptr, LastDataRead);
485}
486
David Woodhousee0c7d762006-05-13 18:07:53 +0100487static void doc2001_writebuf(struct mtd_info *mtd, const u_char *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488{
489 struct nand_chip *this = mtd->priv;
490 struct doc_priv *doc = this->priv;
David Woodhousee0c7d762006-05-13 18:07:53 +0100491 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 int i;
493
David Woodhousee0c7d762006-05-13 18:07:53 +0100494 for (i = 0; i < len; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 WriteDOC_(buf[i], docptr, DoC_Mil_CDSN_IO + i);
496 /* Terminate write pipeline */
497 WriteDOC(0x00, docptr, WritePipeTerm);
498}
499
David Woodhousee0c7d762006-05-13 18:07:53 +0100500static void doc2001_readbuf(struct mtd_info *mtd, u_char *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501{
502 struct nand_chip *this = mtd->priv;
503 struct doc_priv *doc = this->priv;
David Woodhousee0c7d762006-05-13 18:07:53 +0100504 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 int i;
506
507 /* Start read pipeline */
508 ReadDOC(docptr, ReadPipeInit);
509
David Woodhousee0c7d762006-05-13 18:07:53 +0100510 for (i = 0; i < len - 1; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 buf[i] = ReadDOC(docptr, Mil_CDSN_IO + (i & 0xff));
512
513 /* Terminate read pipeline */
514 buf[i] = ReadDOC(docptr, LastDataRead);
515}
516
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517static u_char doc2001plus_read_byte(struct mtd_info *mtd)
518{
519 struct nand_chip *this = mtd->priv;
520 struct doc_priv *doc = this->priv;
David Woodhousee0c7d762006-05-13 18:07:53 +0100521 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 u_char ret;
523
David Woodhousee0c7d762006-05-13 18:07:53 +0100524 ReadDOC(docptr, Mplus_ReadPipeInit);
525 ReadDOC(docptr, Mplus_ReadPipeInit);
526 ret = ReadDOC(docptr, Mplus_LastDataRead);
527 if (debug)
528 printk("read_byte returns %02x\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 return ret;
530}
531
David Woodhousee0c7d762006-05-13 18:07:53 +0100532static void doc2001plus_writebuf(struct mtd_info *mtd, const u_char *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533{
534 struct nand_chip *this = mtd->priv;
535 struct doc_priv *doc = this->priv;
David Woodhousee0c7d762006-05-13 18:07:53 +0100536 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 int i;
538
David Woodhousee0c7d762006-05-13 18:07:53 +0100539 if (debug)
540 printk("writebuf of %d bytes: ", len);
541 for (i = 0; i < len; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 WriteDOC_(buf[i], docptr, DoC_Mil_CDSN_IO + i);
543 if (debug && i < 16)
544 printk("%02x ", buf[i]);
545 }
David Woodhousee0c7d762006-05-13 18:07:53 +0100546 if (debug)
547 printk("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548}
549
David Woodhousee0c7d762006-05-13 18:07:53 +0100550static void doc2001plus_readbuf(struct mtd_info *mtd, u_char *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551{
552 struct nand_chip *this = mtd->priv;
553 struct doc_priv *doc = this->priv;
David Woodhousee0c7d762006-05-13 18:07:53 +0100554 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 int i;
556
David Woodhousee0c7d762006-05-13 18:07:53 +0100557 if (debug)
558 printk("readbuf of %d bytes: ", len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
560 /* Start read pipeline */
561 ReadDOC(docptr, Mplus_ReadPipeInit);
562 ReadDOC(docptr, Mplus_ReadPipeInit);
563
David Woodhousee0c7d762006-05-13 18:07:53 +0100564 for (i = 0; i < len - 2; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 buf[i] = ReadDOC(docptr, Mil_CDSN_IO);
566 if (debug && i < 16)
567 printk("%02x ", buf[i]);
568 }
569
570 /* Terminate read pipeline */
David Woodhousee0c7d762006-05-13 18:07:53 +0100571 buf[len - 2] = ReadDOC(docptr, Mplus_LastDataRead);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 if (debug && i < 16)
David Woodhousee0c7d762006-05-13 18:07:53 +0100573 printk("%02x ", buf[len - 2]);
574 buf[len - 1] = ReadDOC(docptr, Mplus_LastDataRead);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 if (debug && i < 16)
David Woodhousee0c7d762006-05-13 18:07:53 +0100576 printk("%02x ", buf[len - 1]);
577 if (debug)
578 printk("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579}
580
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581static void doc2001plus_select_chip(struct mtd_info *mtd, int chip)
582{
583 struct nand_chip *this = mtd->priv;
584 struct doc_priv *doc = this->priv;
David Woodhousee0c7d762006-05-13 18:07:53 +0100585 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 int floor = 0;
587
David Woodhousee0c7d762006-05-13 18:07:53 +0100588 if (debug)
589 printk("select chip (%d)\n", chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
591 if (chip == -1) {
592 /* Disable flash internally */
593 WriteDOC(0, docptr, Mplus_FlashSelect);
594 return;
595 }
596
597 floor = chip / doc->chips_per_floor;
David Woodhousee0c7d762006-05-13 18:07:53 +0100598 chip -= (floor * doc->chips_per_floor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
600 /* Assert ChipEnable and deassert WriteProtect */
601 WriteDOC((DOC_FLASH_CE), docptr, Mplus_FlashSelect);
602 this->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
603
604 doc->curchip = chip;
605 doc->curfloor = floor;
606}
607
608static void doc200x_select_chip(struct mtd_info *mtd, int chip)
609{
610 struct nand_chip *this = mtd->priv;
611 struct doc_priv *doc = this->priv;
David Woodhousee0c7d762006-05-13 18:07:53 +0100612 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 int floor = 0;
614
David Woodhousee0c7d762006-05-13 18:07:53 +0100615 if (debug)
616 printk("select chip (%d)\n", chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
618 if (chip == -1)
619 return;
620
621 floor = chip / doc->chips_per_floor;
David Woodhousee0c7d762006-05-13 18:07:53 +0100622 chip -= (floor * doc->chips_per_floor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
624 /* 11.4.4 -- deassert CE before changing chip */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200625 doc200x_hwcontrol(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
627 WriteDOC(floor, docptr, FloorSelect);
628 WriteDOC(chip, docptr, CDSNDeviceSelect);
629
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200630 doc200x_hwcontrol(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
632 doc->curchip = chip;
633 doc->curfloor = floor;
634}
635
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200636#define CDSN_CTRL_MSK (CDSN_CTRL_CE | CDSN_CTRL_CLE | CDSN_CTRL_ALE)
637
638static void doc200x_hwcontrol(struct mtd_info *mtd, int cmd,
639 unsigned int ctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640{
641 struct nand_chip *this = mtd->priv;
642 struct doc_priv *doc = this->priv;
David Woodhousee0c7d762006-05-13 18:07:53 +0100643 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200645 if (ctrl & NAND_CTRL_CHANGE) {
646 doc->CDSNControl &= ~CDSN_CTRL_MSK;
647 doc->CDSNControl |= ctrl & CDSN_CTRL_MSK;
648 if (debug)
649 printk("hwcontrol(%d): %02x\n", cmd, doc->CDSNControl);
650 WriteDOC(doc->CDSNControl, docptr, CDSNControl);
651 /* 11.4.3 -- 4 NOPs after CSDNControl write */
652 DoC_Delay(doc, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 }
Thomas Gleixnercad74f22006-05-23 23:28:48 +0200654 if (cmd != NAND_CMD_NONE) {
655 if (DoC_is_2000(doc))
656 doc2000_write_byte(mtd, cmd);
657 else
658 doc2001_write_byte(mtd, cmd);
659 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660}
661
David Woodhousee0c7d762006-05-13 18:07:53 +0100662static void doc2001plus_command(struct mtd_info *mtd, unsigned command, int column, int page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663{
664 struct nand_chip *this = mtd->priv;
665 struct doc_priv *doc = this->priv;
David Woodhousee0c7d762006-05-13 18:07:53 +0100666 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
668 /*
669 * Must terminate write pipeline before sending any commands
670 * to the device.
671 */
672 if (command == NAND_CMD_PAGEPROG) {
673 WriteDOC(0x00, docptr, Mplus_WritePipeTerm);
674 WriteDOC(0x00, docptr, Mplus_WritePipeTerm);
675 }
676
677 /*
678 * Write out the command to the device.
679 */
680 if (command == NAND_CMD_SEQIN) {
681 int readcmd;
682
Joern Engel28318772006-05-22 23:18:05 +0200683 if (column >= mtd->writesize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 /* OOB area */
Joern Engel28318772006-05-22 23:18:05 +0200685 column -= mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 readcmd = NAND_CMD_READOOB;
687 } else if (column < 256) {
688 /* First 256 bytes --> READ0 */
689 readcmd = NAND_CMD_READ0;
690 } else {
691 column -= 256;
692 readcmd = NAND_CMD_READ1;
693 }
694 WriteDOC(readcmd, docptr, Mplus_FlashCmd);
695 }
696 WriteDOC(command, docptr, Mplus_FlashCmd);
697 WriteDOC(0, docptr, Mplus_WritePipeTerm);
698 WriteDOC(0, docptr, Mplus_WritePipeTerm);
699
700 if (column != -1 || page_addr != -1) {
701 /* Serially input address */
702 if (column != -1) {
703 /* Adjust columns for 16 bit buswidth */
Brian Norris3dad2342014-01-29 14:08:12 -0800704 if (this->options & NAND_BUSWIDTH_16 &&
705 !nand_opcode_8bits(command))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 column >>= 1;
707 WriteDOC(column, docptr, Mplus_FlashAddress);
708 }
709 if (page_addr != -1) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100710 WriteDOC((unsigned char)(page_addr & 0xff), docptr, Mplus_FlashAddress);
711 WriteDOC((unsigned char)((page_addr >> 8) & 0xff), docptr, Mplus_FlashAddress);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 /* One more address cycle for higher density devices */
713 if (this->chipsize & 0x0c000000) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100714 WriteDOC((unsigned char)((page_addr >> 16) & 0x0f), docptr, Mplus_FlashAddress);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 printk("high density\n");
716 }
717 }
718 WriteDOC(0, docptr, Mplus_WritePipeTerm);
719 WriteDOC(0, docptr, Mplus_WritePipeTerm);
720 /* deassert ALE */
David Woodhousee0c7d762006-05-13 18:07:53 +0100721 if (command == NAND_CMD_READ0 || command == NAND_CMD_READ1 ||
722 command == NAND_CMD_READOOB || command == NAND_CMD_READID)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 WriteDOC(0, docptr, Mplus_FlashControl);
724 }
725
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000726 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 * program and erase have their own busy handlers
728 * status and sequential in needs no delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100729 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 switch (command) {
731
732 case NAND_CMD_PAGEPROG:
733 case NAND_CMD_ERASE1:
734 case NAND_CMD_ERASE2:
735 case NAND_CMD_SEQIN:
736 case NAND_CMD_STATUS:
737 return;
738
739 case NAND_CMD_RESET:
740 if (this->dev_ready)
741 break;
742 udelay(this->chip_delay);
743 WriteDOC(NAND_CMD_STATUS, docptr, Mplus_FlashCmd);
744 WriteDOC(0, docptr, Mplus_WritePipeTerm);
745 WriteDOC(0, docptr, Mplus_WritePipeTerm);
David Woodhousee0c7d762006-05-13 18:07:53 +0100746 while (!(this->read_byte(mtd) & 0x40)) ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 return;
748
David Woodhousee0c7d762006-05-13 18:07:53 +0100749 /* This applies to read commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000751 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 * If we don't have access to the busy pin, we apply the given
753 * command delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100754 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 if (!this->dev_ready) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100756 udelay(this->chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 return;
758 }
759 }
760
761 /* Apply this short delay always to ensure that we do wait tWB in
762 * any case on any machine. */
David Woodhousee0c7d762006-05-13 18:07:53 +0100763 ndelay(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 /* wait until command is processed */
David Woodhousee0c7d762006-05-13 18:07:53 +0100765 while (!this->dev_ready(mtd)) ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766}
767
768static int doc200x_dev_ready(struct mtd_info *mtd)
769{
770 struct nand_chip *this = mtd->priv;
771 struct doc_priv *doc = this->priv;
David Woodhousee0c7d762006-05-13 18:07:53 +0100772 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
774 if (DoC_is_MillenniumPlus(doc)) {
775 /* 11.4.2 -- must NOP four times before checking FR/B# */
776 DoC_Delay(doc, 4);
777 if ((ReadDOC(docptr, Mplus_FlashControl) & CDSN_CTRL_FR_B_MASK) != CDSN_CTRL_FR_B_MASK) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100778 if (debug)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 printk("not ready\n");
780 return 0;
781 }
David Woodhousee0c7d762006-05-13 18:07:53 +0100782 if (debug)
783 printk("was ready\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 return 1;
785 } else {
786 /* 11.4.2 -- must NOP four times before checking FR/B# */
787 DoC_Delay(doc, 4);
788 if (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B)) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100789 if (debug)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 printk("not ready\n");
791 return 0;
792 }
793 /* 11.4.2 -- Must NOP twice if it's ready */
794 DoC_Delay(doc, 2);
David Woodhousee0c7d762006-05-13 18:07:53 +0100795 if (debug)
796 printk("was ready\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 return 1;
798 }
799}
800
801static int doc200x_block_bad(struct mtd_info *mtd, loff_t ofs, int getchip)
802{
803 /* This is our last resort if we couldn't find or create a BBT. Just
804 pretend all blocks are good. */
805 return 0;
806}
807
808static void doc200x_enable_hwecc(struct mtd_info *mtd, int mode)
809{
810 struct nand_chip *this = mtd->priv;
811 struct doc_priv *doc = this->priv;
David Woodhousee0c7d762006-05-13 18:07:53 +0100812 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
814 /* Prime the ECC engine */
David Woodhousee0c7d762006-05-13 18:07:53 +0100815 switch (mode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 case NAND_ECC_READ:
817 WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
818 WriteDOC(DOC_ECC_EN, docptr, ECCConf);
819 break;
820 case NAND_ECC_WRITE:
821 WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
822 WriteDOC(DOC_ECC_EN | DOC_ECC_RW, docptr, ECCConf);
823 break;
824 }
825}
826
827static void doc2001plus_enable_hwecc(struct mtd_info *mtd, int mode)
828{
829 struct nand_chip *this = mtd->priv;
830 struct doc_priv *doc = this->priv;
David Woodhousee0c7d762006-05-13 18:07:53 +0100831 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
833 /* Prime the ECC engine */
David Woodhousee0c7d762006-05-13 18:07:53 +0100834 switch (mode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 case NAND_ECC_READ:
836 WriteDOC(DOC_ECC_RESET, docptr, Mplus_ECCConf);
837 WriteDOC(DOC_ECC_EN, docptr, Mplus_ECCConf);
838 break;
839 case NAND_ECC_WRITE:
840 WriteDOC(DOC_ECC_RESET, docptr, Mplus_ECCConf);
841 WriteDOC(DOC_ECC_EN | DOC_ECC_RW, docptr, Mplus_ECCConf);
842 break;
843 }
844}
845
846/* This code is only called on write */
David Woodhousee0c7d762006-05-13 18:07:53 +0100847static int doc200x_calculate_ecc(struct mtd_info *mtd, const u_char *dat, unsigned char *ecc_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848{
849 struct nand_chip *this = mtd->priv;
850 struct doc_priv *doc = this->priv;
David Woodhousee0c7d762006-05-13 18:07:53 +0100851 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 int i;
853 int emptymatch = 1;
854
855 /* flush the pipeline */
856 if (DoC_is_2000(doc)) {
857 WriteDOC(doc->CDSNControl & ~CDSN_CTRL_FLASH_IO, docptr, CDSNControl);
858 WriteDOC(0, docptr, 2k_CDSN_IO);
859 WriteDOC(0, docptr, 2k_CDSN_IO);
860 WriteDOC(0, docptr, 2k_CDSN_IO);
861 WriteDOC(doc->CDSNControl, docptr, CDSNControl);
862 } else if (DoC_is_MillenniumPlus(doc)) {
863 WriteDOC(0, docptr, Mplus_NOP);
864 WriteDOC(0, docptr, Mplus_NOP);
865 WriteDOC(0, docptr, Mplus_NOP);
866 } else {
867 WriteDOC(0, docptr, NOP);
868 WriteDOC(0, docptr, NOP);
869 WriteDOC(0, docptr, NOP);
870 }
871
872 for (i = 0; i < 6; i++) {
873 if (DoC_is_MillenniumPlus(doc))
874 ecc_code[i] = ReadDOC_(docptr, DoC_Mplus_ECCSyndrome0 + i);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000875 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 ecc_code[i] = ReadDOC_(docptr, DoC_ECCSyndrome0 + i);
877 if (ecc_code[i] != empty_write_ecc[i])
878 emptymatch = 0;
879 }
880 if (DoC_is_MillenniumPlus(doc))
881 WriteDOC(DOC_ECC_DIS, docptr, Mplus_ECCConf);
882 else
883 WriteDOC(DOC_ECC_DIS, docptr, ECCConf);
884#if 0
885 /* If emptymatch=1, we might have an all-0xff data buffer. Check. */
886 if (emptymatch) {
887 /* Note: this somewhat expensive test should not be triggered
888 often. It could be optimized away by examining the data in
889 the writebuf routine, and remembering the result. */
890 for (i = 0; i < 512; i++) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100891 if (dat[i] == 0xff)
892 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 emptymatch = 0;
894 break;
895 }
896 }
897 /* If emptymatch still =1, we do have an all-0xff data buffer.
898 Return all-0xff ecc value instead of the computed one, so
899 it'll look just like a freshly-erased page. */
David Woodhousee0c7d762006-05-13 18:07:53 +0100900 if (emptymatch)
901 memset(ecc_code, 0xff, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902#endif
903 return 0;
904}
905
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200906static int doc200x_correct_data(struct mtd_info *mtd, u_char *dat,
907 u_char *read_ecc, u_char *isnull)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908{
909 int i, ret = 0;
910 struct nand_chip *this = mtd->priv;
911 struct doc_priv *doc = this->priv;
David Woodhousee0c7d762006-05-13 18:07:53 +0100912 void __iomem *docptr = doc->virtadr;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200913 uint8_t calc_ecc[6];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 volatile u_char dummy;
915 int emptymatch = 1;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000916
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 /* flush the pipeline */
918 if (DoC_is_2000(doc)) {
919 dummy = ReadDOC(docptr, 2k_ECCStatus);
920 dummy = ReadDOC(docptr, 2k_ECCStatus);
921 dummy = ReadDOC(docptr, 2k_ECCStatus);
922 } else if (DoC_is_MillenniumPlus(doc)) {
923 dummy = ReadDOC(docptr, Mplus_ECCConf);
924 dummy = ReadDOC(docptr, Mplus_ECCConf);
925 dummy = ReadDOC(docptr, Mplus_ECCConf);
926 } else {
927 dummy = ReadDOC(docptr, ECCConf);
928 dummy = ReadDOC(docptr, ECCConf);
929 dummy = ReadDOC(docptr, ECCConf);
930 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000931
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300932 /* Error occurred ? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 if (dummy & 0x80) {
934 for (i = 0; i < 6; i++) {
935 if (DoC_is_MillenniumPlus(doc))
936 calc_ecc[i] = ReadDOC_(docptr, DoC_Mplus_ECCSyndrome0 + i);
937 else
938 calc_ecc[i] = ReadDOC_(docptr, DoC_ECCSyndrome0 + i);
939 if (calc_ecc[i] != empty_read_syndrome[i])
940 emptymatch = 0;
941 }
942 /* If emptymatch=1, the read syndrome is consistent with an
943 all-0xff data and stored ecc block. Check the stored ecc. */
944 if (emptymatch) {
945 for (i = 0; i < 6; i++) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100946 if (read_ecc[i] == 0xff)
947 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 emptymatch = 0;
949 break;
950 }
951 }
952 /* If emptymatch still =1, check the data block. */
953 if (emptymatch) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100954 /* Note: this somewhat expensive test should not be triggered
955 often. It could be optimized away by examining the data in
956 the readbuf routine, and remembering the result. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 for (i = 0; i < 512; i++) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100958 if (dat[i] == 0xff)
959 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 emptymatch = 0;
961 break;
962 }
963 }
964 /* If emptymatch still =1, this is almost certainly a freshly-
965 erased block, in which case the ECC will not come out right.
966 We'll suppress the error and tell the caller everything's
967 OK. Because it is. */
David Woodhousee0c7d762006-05-13 18:07:53 +0100968 if (!emptymatch)
969 ret = doc_ecc_decode(rs_decoder, dat, calc_ecc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 if (ret > 0)
971 printk(KERN_ERR "doc200x_correct_data corrected %d errors\n", ret);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000972 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 if (DoC_is_MillenniumPlus(doc))
974 WriteDOC(DOC_ECC_DIS, docptr, Mplus_ECCConf);
975 else
976 WriteDOC(DOC_ECC_DIS, docptr, ECCConf);
Brian Norrisd57f40542011-09-20 18:34:25 -0700977 if (no_ecc_failures && mtd_is_eccerr(ret)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 printk(KERN_ERR "suppressing ECC failure\n");
979 ret = 0;
980 }
981 return ret;
982}
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000983
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984//u_char mydatabuf[528];
985
Dan Brownabc37e62005-04-07 15:22:58 +0100986/* The strange out-of-order .oobfree list below is a (possibly unneeded)
987 * attempt to retain compatibility. It used to read:
988 * .oobfree = { {8, 8} }
989 * Since that leaves two bytes unusable, it was changed. But the following
990 * scheme might affect existing jffs2 installs by moving the cleanmarker:
991 * .oobfree = { {6, 10} }
992 * jffs2 seems to handle the above gracefully, but the current scheme seems
993 * safer. The only problem with it is that any code that parses oobfree must
994 * be able to handle out-of-order segments.
995 */
Thomas Gleixner5bd34c02006-05-27 22:16:10 +0200996static struct nand_ecclayout doc200x_oobinfo = {
David Woodhousee0c7d762006-05-13 18:07:53 +0100997 .eccbytes = 6,
998 .eccpos = {0, 1, 2, 3, 4, 5},
999 .oobfree = {{8, 8}, {6, 2}}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000};
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001001
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002/* Find the (I)NFTL Media Header, and optionally also the mirror media header.
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001003 On successful return, buf will contain a copy of the media header for
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 further processing. id is the string to scan for, and will presumably be
1005 either "ANAND" or "BNAND". If findmirror=1, also look for the mirror media
1006 header. The page #s of the found media headers are placed in mh0_page and
1007 mh1_page in the DOC private structure. */
David Woodhousee0c7d762006-05-13 18:07:53 +01001008static int __init find_media_headers(struct mtd_info *mtd, u_char *buf, const char *id, int findmirror)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009{
1010 struct nand_chip *this = mtd->priv;
1011 struct doc_priv *doc = this->priv;
Dan Brown1a78ff62005-03-29 21:57:48 +01001012 unsigned offs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 int ret;
1014 size_t retlen;
1015
Dan Brown1a78ff62005-03-29 21:57:48 +01001016 for (offs = 0; offs < mtd->size; offs += mtd->erasesize) {
Artem Bityutskiy329ad392011-12-23 17:30:16 +02001017 ret = mtd_read(mtd, offs, mtd->writesize, &retlen, buf);
Joern Engel28318772006-05-22 23:18:05 +02001018 if (retlen != mtd->writesize)
David Woodhousee0c7d762006-05-13 18:07:53 +01001019 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 if (ret) {
David Woodhousee0c7d762006-05-13 18:07:53 +01001021 printk(KERN_WARNING "ECC error scanning DOC at 0x%x\n", offs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 }
David Woodhousee0c7d762006-05-13 18:07:53 +01001023 if (memcmp(buf, id, 6))
1024 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 printk(KERN_INFO "Found DiskOnChip %s Media Header at 0x%x\n", id, offs);
1026 if (doc->mh0_page == -1) {
1027 doc->mh0_page = offs >> this->page_shift;
David Woodhousee0c7d762006-05-13 18:07:53 +01001028 if (!findmirror)
1029 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 continue;
1031 }
1032 doc->mh1_page = offs >> this->page_shift;
1033 return 2;
1034 }
1035 if (doc->mh0_page == -1) {
1036 printk(KERN_WARNING "DiskOnChip %s Media Header not found.\n", id);
1037 return 0;
1038 }
1039 /* Only one mediaheader was found. We want buf to contain a
1040 mediaheader on return, so we'll have to re-read the one we found. */
1041 offs = doc->mh0_page << this->page_shift;
Artem Bityutskiy329ad392011-12-23 17:30:16 +02001042 ret = mtd_read(mtd, offs, mtd->writesize, &retlen, buf);
Joern Engel28318772006-05-22 23:18:05 +02001043 if (retlen != mtd->writesize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 /* Insanity. Give up. */
1045 printk(KERN_ERR "Read DiskOnChip Media Header once, but can't reread it???\n");
1046 return 0;
1047 }
1048 return 1;
1049}
1050
David Woodhousee0c7d762006-05-13 18:07:53 +01001051static inline int __init nftl_partscan(struct mtd_info *mtd, struct mtd_partition *parts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052{
1053 struct nand_chip *this = mtd->priv;
1054 struct doc_priv *doc = this->priv;
1055 int ret = 0;
1056 u_char *buf;
1057 struct NFTLMediaHeader *mh;
1058 const unsigned psize = 1 << this->page_shift;
Dan Brown1a78ff62005-03-29 21:57:48 +01001059 int numparts = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 unsigned blocks, maxblocks;
1061 int offs, numheaders;
1062
Joern Engel28318772006-05-22 23:18:05 +02001063 buf = kmalloc(mtd->writesize, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 if (!buf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 return 0;
1066 }
David Woodhousee0c7d762006-05-13 18:07:53 +01001067 if (!(numheaders = find_media_headers(mtd, buf, "ANAND", 1)))
1068 goto out;
1069 mh = (struct NFTLMediaHeader *)buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
Harvey Harrison96372442008-07-30 12:34:57 -07001071 le16_to_cpus(&mh->NumEraseUnits);
1072 le16_to_cpus(&mh->FirstPhysicalEUN);
1073 le32_to_cpus(&mh->FormattedSize);
Thomas Gleixnerf29a4b82005-01-31 22:22:24 +00001074
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 printk(KERN_INFO " DataOrgID = %s\n"
1076 " NumEraseUnits = %d\n"
1077 " FirstPhysicalEUN = %d\n"
1078 " FormattedSize = %d\n"
1079 " UnitSizeFactor = %d\n",
1080 mh->DataOrgID, mh->NumEraseUnits,
1081 mh->FirstPhysicalEUN, mh->FormattedSize,
1082 mh->UnitSizeFactor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083
1084 blocks = mtd->size >> this->phys_erase_shift;
1085 maxblocks = min(32768U, mtd->erasesize - psize);
1086
1087 if (mh->UnitSizeFactor == 0x00) {
1088 /* Auto-determine UnitSizeFactor. The constraints are:
1089 - There can be at most 32768 virtual blocks.
1090 - There can be at most (virtual block size - page size)
David Woodhousee0c7d762006-05-13 18:07:53 +01001091 virtual blocks (because MediaHeader+BBT must fit in 1).
1092 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 mh->UnitSizeFactor = 0xff;
1094 while (blocks > maxblocks) {
1095 blocks >>= 1;
1096 maxblocks = min(32768U, (maxblocks << 1) + psize);
1097 mh->UnitSizeFactor--;
1098 }
1099 printk(KERN_WARNING "UnitSizeFactor=0x00 detected. Correct value is assumed to be 0x%02x.\n", mh->UnitSizeFactor);
1100 }
1101
1102 /* NOTE: The lines below modify internal variables of the NAND and MTD
1103 layers; variables with have already been configured by nand_scan.
1104 Unfortunately, we didn't know before this point what these values
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001105 should be. Thus, this code is somewhat dependent on the exact
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 implementation of the NAND layer. */
1107 if (mh->UnitSizeFactor != 0xff) {
1108 this->bbt_erase_shift += (0xff - mh->UnitSizeFactor);
1109 mtd->erasesize <<= (0xff - mh->UnitSizeFactor);
1110 printk(KERN_INFO "Setting virtual erase size to %d\n", mtd->erasesize);
1111 blocks = mtd->size >> this->bbt_erase_shift;
1112 maxblocks = min(32768U, mtd->erasesize - psize);
1113 }
1114
1115 if (blocks > maxblocks) {
1116 printk(KERN_ERR "UnitSizeFactor of 0x%02x is inconsistent with device size. Aborting.\n", mh->UnitSizeFactor);
1117 goto out;
1118 }
1119
1120 /* Skip past the media headers. */
1121 offs = max(doc->mh0_page, doc->mh1_page);
1122 offs <<= this->page_shift;
1123 offs += mtd->erasesize;
1124
Dan Brown1a78ff62005-03-29 21:57:48 +01001125 if (show_firmware_partition == 1) {
1126 parts[0].name = " DiskOnChip Firmware / Media Header partition";
1127 parts[0].offset = 0;
1128 parts[0].size = offs;
1129 numparts = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 }
Dan Brown1a78ff62005-03-29 21:57:48 +01001131
1132 parts[numparts].name = " DiskOnChip BDTL partition";
1133 parts[numparts].offset = offs;
1134 parts[numparts].size = (mh->NumEraseUnits - numheaders) << this->bbt_erase_shift;
1135
1136 offs += parts[numparts].size;
1137 numparts++;
1138
1139 if (offs < mtd->size) {
1140 parts[numparts].name = " DiskOnChip Remainder partition";
1141 parts[numparts].offset = offs;
1142 parts[numparts].size = mtd->size - offs;
1143 numparts++;
1144 }
1145
1146 ret = numparts;
David Woodhousee0c7d762006-05-13 18:07:53 +01001147 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 kfree(buf);
1149 return ret;
1150}
1151
1152/* This is a stripped-down copy of the code in inftlmount.c */
David Woodhousee0c7d762006-05-13 18:07:53 +01001153static inline int __init inftl_partscan(struct mtd_info *mtd, struct mtd_partition *parts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154{
1155 struct nand_chip *this = mtd->priv;
1156 struct doc_priv *doc = this->priv;
1157 int ret = 0;
1158 u_char *buf;
1159 struct INFTLMediaHeader *mh;
1160 struct INFTLPartition *ip;
1161 int numparts = 0;
1162 int blocks;
1163 int vshift, lastvunit = 0;
1164 int i;
1165 int end = mtd->size;
1166
1167 if (inftl_bbt_write)
1168 end -= (INFTL_BBT_RESERVED_BLOCKS << this->phys_erase_shift);
1169
Joern Engel28318772006-05-22 23:18:05 +02001170 buf = kmalloc(mtd->writesize, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 if (!buf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 return 0;
1173 }
1174
David Woodhousee0c7d762006-05-13 18:07:53 +01001175 if (!find_media_headers(mtd, buf, "BNAND", 0))
1176 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 doc->mh1_page = doc->mh0_page + (4096 >> this->page_shift);
David Woodhousee0c7d762006-05-13 18:07:53 +01001178 mh = (struct INFTLMediaHeader *)buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179
Harvey Harrison96372442008-07-30 12:34:57 -07001180 le32_to_cpus(&mh->NoOfBootImageBlocks);
1181 le32_to_cpus(&mh->NoOfBinaryPartitions);
1182 le32_to_cpus(&mh->NoOfBDTLPartitions);
1183 le32_to_cpus(&mh->BlockMultiplierBits);
1184 le32_to_cpus(&mh->FormatFlags);
1185 le32_to_cpus(&mh->PercentUsed);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001186
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 printk(KERN_INFO " bootRecordID = %s\n"
1188 " NoOfBootImageBlocks = %d\n"
1189 " NoOfBinaryPartitions = %d\n"
1190 " NoOfBDTLPartitions = %d\n"
1191 " BlockMultiplerBits = %d\n"
1192 " FormatFlgs = %d\n"
1193 " OsakVersion = %d.%d.%d.%d\n"
1194 " PercentUsed = %d\n",
1195 mh->bootRecordID, mh->NoOfBootImageBlocks,
1196 mh->NoOfBinaryPartitions,
1197 mh->NoOfBDTLPartitions,
1198 mh->BlockMultiplierBits, mh->FormatFlags,
1199 ((unsigned char *) &mh->OsakVersion)[0] & 0xf,
1200 ((unsigned char *) &mh->OsakVersion)[1] & 0xf,
1201 ((unsigned char *) &mh->OsakVersion)[2] & 0xf,
1202 ((unsigned char *) &mh->OsakVersion)[3] & 0xf,
1203 mh->PercentUsed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204
1205 vshift = this->phys_erase_shift + mh->BlockMultiplierBits;
1206
1207 blocks = mtd->size >> vshift;
1208 if (blocks > 32768) {
1209 printk(KERN_ERR "BlockMultiplierBits=%d is inconsistent with device size. Aborting.\n", mh->BlockMultiplierBits);
1210 goto out;
1211 }
1212
1213 blocks = doc->chips_per_floor << (this->chip_shift - this->phys_erase_shift);
1214 if (inftl_bbt_write && (blocks > mtd->erasesize)) {
1215 printk(KERN_ERR "Writeable BBTs spanning more than one erase block are not yet supported. FIX ME!\n");
1216 goto out;
1217 }
1218
1219 /* Scan the partitions */
1220 for (i = 0; (i < 4); i++) {
1221 ip = &(mh->Partitions[i]);
Harvey Harrison96372442008-07-30 12:34:57 -07001222 le32_to_cpus(&ip->virtualUnits);
1223 le32_to_cpus(&ip->firstUnit);
1224 le32_to_cpus(&ip->lastUnit);
1225 le32_to_cpus(&ip->flags);
1226 le32_to_cpus(&ip->spareUnits);
1227 le32_to_cpus(&ip->Reserved0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 printk(KERN_INFO " PARTITION[%d] ->\n"
1230 " virtualUnits = %d\n"
1231 " firstUnit = %d\n"
1232 " lastUnit = %d\n"
1233 " flags = 0x%x\n"
1234 " spareUnits = %d\n",
1235 i, ip->virtualUnits, ip->firstUnit,
1236 ip->lastUnit, ip->flags,
1237 ip->spareUnits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238
Dan Brown1a78ff62005-03-29 21:57:48 +01001239 if ((show_firmware_partition == 1) &&
1240 (i == 0) && (ip->firstUnit > 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 parts[0].name = " DiskOnChip IPL / Media Header partition";
1242 parts[0].offset = 0;
1243 parts[0].size = mtd->erasesize * ip->firstUnit;
1244 numparts = 1;
1245 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246
1247 if (ip->flags & INFTL_BINARY)
1248 parts[numparts].name = " DiskOnChip BDK partition";
1249 else
1250 parts[numparts].name = " DiskOnChip BDTL partition";
1251 parts[numparts].offset = ip->firstUnit << vshift;
1252 parts[numparts].size = (1 + ip->lastUnit - ip->firstUnit) << vshift;
1253 numparts++;
David Woodhousee0c7d762006-05-13 18:07:53 +01001254 if (ip->lastUnit > lastvunit)
1255 lastvunit = ip->lastUnit;
1256 if (ip->flags & INFTL_LAST)
1257 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 }
1259 lastvunit++;
1260 if ((lastvunit << vshift) < end) {
1261 parts[numparts].name = " DiskOnChip Remainder partition";
1262 parts[numparts].offset = lastvunit << vshift;
1263 parts[numparts].size = end - parts[numparts].offset;
1264 numparts++;
1265 }
1266 ret = numparts;
David Woodhousee0c7d762006-05-13 18:07:53 +01001267 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 kfree(buf);
1269 return ret;
1270}
1271
1272static int __init nftl_scan_bbt(struct mtd_info *mtd)
1273{
1274 int ret, numparts;
1275 struct nand_chip *this = mtd->priv;
1276 struct doc_priv *doc = this->priv;
1277 struct mtd_partition parts[2];
1278
David Woodhousee0c7d762006-05-13 18:07:53 +01001279 memset((char *)parts, 0, sizeof(parts));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 /* On NFTL, we have to find the media headers before we can read the
1281 BBTs, since they're stored in the media header eraseblocks. */
1282 numparts = nftl_partscan(mtd, parts);
David Woodhousee0c7d762006-05-13 18:07:53 +01001283 if (!numparts)
1284 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 this->bbt_td->options = NAND_BBT_ABSPAGE | NAND_BBT_8BIT |
1286 NAND_BBT_SAVECONTENT | NAND_BBT_WRITE |
1287 NAND_BBT_VERSION;
1288 this->bbt_td->veroffs = 7;
1289 this->bbt_td->pages[0] = doc->mh0_page + 1;
1290 if (doc->mh1_page != -1) {
1291 this->bbt_md->options = NAND_BBT_ABSPAGE | NAND_BBT_8BIT |
1292 NAND_BBT_SAVECONTENT | NAND_BBT_WRITE |
1293 NAND_BBT_VERSION;
1294 this->bbt_md->veroffs = 7;
1295 this->bbt_md->pages[0] = doc->mh1_page + 1;
1296 } else {
1297 this->bbt_md = NULL;
1298 }
1299
Brian Norrisd24fe0c2015-02-28 02:13:10 -08001300 ret = this->scan_bbt(mtd);
1301 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 return ret;
Brian Norrisd24fe0c2015-02-28 02:13:10 -08001303
Jamie Iles0f47e952011-05-23 10:23:19 +01001304 mtd_device_register(mtd, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 if (!no_autopart)
Jamie Iles0f47e952011-05-23 10:23:19 +01001306 mtd_device_register(mtd, parts, numparts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 return 0;
1308}
1309
1310static int __init inftl_scan_bbt(struct mtd_info *mtd)
1311{
1312 int ret, numparts;
1313 struct nand_chip *this = mtd->priv;
1314 struct doc_priv *doc = this->priv;
1315 struct mtd_partition parts[5];
1316
1317 if (this->numchips > doc->chips_per_floor) {
1318 printk(KERN_ERR "Multi-floor INFTL devices not yet supported.\n");
1319 return -EIO;
1320 }
1321
1322 if (DoC_is_MillenniumPlus(doc)) {
1323 this->bbt_td->options = NAND_BBT_2BIT | NAND_BBT_ABSPAGE;
1324 if (inftl_bbt_write)
1325 this->bbt_td->options |= NAND_BBT_WRITE;
1326 this->bbt_td->pages[0] = 2;
1327 this->bbt_md = NULL;
1328 } else {
David Woodhousee0c7d762006-05-13 18:07:53 +01001329 this->bbt_td->options = NAND_BBT_LASTBLOCK | NAND_BBT_8BIT | NAND_BBT_VERSION;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 if (inftl_bbt_write)
1331 this->bbt_td->options |= NAND_BBT_WRITE;
1332 this->bbt_td->offs = 8;
1333 this->bbt_td->len = 8;
1334 this->bbt_td->veroffs = 7;
1335 this->bbt_td->maxblocks = INFTL_BBT_RESERVED_BLOCKS;
1336 this->bbt_td->reserved_block_code = 0x01;
1337 this->bbt_td->pattern = "MSYS_BBT";
1338
David Woodhousee0c7d762006-05-13 18:07:53 +01001339 this->bbt_md->options = NAND_BBT_LASTBLOCK | NAND_BBT_8BIT | NAND_BBT_VERSION;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 if (inftl_bbt_write)
1341 this->bbt_md->options |= NAND_BBT_WRITE;
1342 this->bbt_md->offs = 8;
1343 this->bbt_md->len = 8;
1344 this->bbt_md->veroffs = 7;
1345 this->bbt_md->maxblocks = INFTL_BBT_RESERVED_BLOCKS;
1346 this->bbt_md->reserved_block_code = 0x01;
1347 this->bbt_md->pattern = "TBB_SYSM";
1348 }
1349
Brian Norrisd24fe0c2015-02-28 02:13:10 -08001350 ret = this->scan_bbt(mtd);
1351 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 return ret;
Brian Norrisd24fe0c2015-02-28 02:13:10 -08001353
David Woodhousee0c7d762006-05-13 18:07:53 +01001354 memset((char *)parts, 0, sizeof(parts));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 numparts = inftl_partscan(mtd, parts);
1356 /* At least for now, require the INFTL Media Header. We could probably
1357 do without it for non-INFTL use, since all it gives us is
1358 autopartitioning, but I want to give it more thought. */
David Woodhousee0c7d762006-05-13 18:07:53 +01001359 if (!numparts)
1360 return -EIO;
Jamie Iles0f47e952011-05-23 10:23:19 +01001361 mtd_device_register(mtd, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 if (!no_autopart)
Jamie Iles0f47e952011-05-23 10:23:19 +01001363 mtd_device_register(mtd, parts, numparts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 return 0;
1365}
1366
1367static inline int __init doc2000_init(struct mtd_info *mtd)
1368{
1369 struct nand_chip *this = mtd->priv;
1370 struct doc_priv *doc = this->priv;
1371
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 this->read_byte = doc2000_read_byte;
1373 this->write_buf = doc2000_writebuf;
1374 this->read_buf = doc2000_readbuf;
Brian Norrisd24fe0c2015-02-28 02:13:10 -08001375 doc->late_init = nftl_scan_bbt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376
1377 doc->CDSNControl = CDSN_CTRL_FLASH_IO | CDSN_CTRL_ECC_IO;
1378 doc2000_count_chips(mtd);
1379 mtd->name = "DiskOnChip 2000 (NFTL Model)";
1380 return (4 * doc->chips_per_floor);
1381}
1382
1383static inline int __init doc2001_init(struct mtd_info *mtd)
1384{
1385 struct nand_chip *this = mtd->priv;
1386 struct doc_priv *doc = this->priv;
1387
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 this->read_byte = doc2001_read_byte;
1389 this->write_buf = doc2001_writebuf;
1390 this->read_buf = doc2001_readbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391
1392 ReadDOC(doc->virtadr, ChipID);
1393 ReadDOC(doc->virtadr, ChipID);
1394 ReadDOC(doc->virtadr, ChipID);
1395 if (ReadDOC(doc->virtadr, ChipID) != DOC_ChipID_DocMil) {
1396 /* It's not a Millennium; it's one of the newer
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001397 DiskOnChip 2000 units with a similar ASIC.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 Treat it like a Millennium, except that it
1399 can have multiple chips. */
1400 doc2000_count_chips(mtd);
1401 mtd->name = "DiskOnChip 2000 (INFTL Model)";
Brian Norrisd24fe0c2015-02-28 02:13:10 -08001402 doc->late_init = inftl_scan_bbt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 return (4 * doc->chips_per_floor);
1404 } else {
1405 /* Bog-standard Millennium */
1406 doc->chips_per_floor = 1;
1407 mtd->name = "DiskOnChip Millennium";
Brian Norrisd24fe0c2015-02-28 02:13:10 -08001408 doc->late_init = nftl_scan_bbt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 return 1;
1410 }
1411}
1412
1413static inline int __init doc2001plus_init(struct mtd_info *mtd)
1414{
1415 struct nand_chip *this = mtd->priv;
1416 struct doc_priv *doc = this->priv;
1417
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 this->read_byte = doc2001plus_read_byte;
1419 this->write_buf = doc2001plus_writebuf;
1420 this->read_buf = doc2001plus_readbuf;
Brian Norrisd24fe0c2015-02-28 02:13:10 -08001421 doc->late_init = inftl_scan_bbt;
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +02001422 this->cmd_ctrl = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 this->select_chip = doc2001plus_select_chip;
1424 this->cmdfunc = doc2001plus_command;
Thomas Gleixner0cddd6c2006-05-23 15:59:58 +02001425 this->ecc.hwctl = doc2001plus_enable_hwecc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426
1427 doc->chips_per_floor = 1;
1428 mtd->name = "DiskOnChip Millennium Plus";
1429
1430 return 1;
1431}
1432
Arjan van de Ven858119e2006-01-14 13:20:43 -08001433static int __init doc_probe(unsigned long physadr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434{
1435 unsigned char ChipID;
1436 struct mtd_info *mtd;
1437 struct nand_chip *nand;
1438 struct doc_priv *doc;
1439 void __iomem *virtadr;
1440 unsigned char save_control;
1441 unsigned char tmp, tmpb, tmpc;
1442 int reg, len, numchips;
1443 int ret = 0;
1444
Sasha Levin86e4bbc2014-03-19 18:24:37 -04001445 if (!request_mem_region(physadr, DOC_IOREMAP_LEN, "DiskOnChip"))
Alexander Shiyan4f0614a2013-11-13 15:59:25 +04001446 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 virtadr = ioremap(physadr, DOC_IOREMAP_LEN);
1448 if (!virtadr) {
1449 printk(KERN_ERR "Diskonchip ioremap failed: 0x%x bytes at 0x%lx\n", DOC_IOREMAP_LEN, physadr);
Alexander Shiyan4f0614a2013-11-13 15:59:25 +04001450 ret = -EIO;
1451 goto error_ioremap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 }
1453
1454 /* It's not possible to cleanly detect the DiskOnChip - the
1455 * bootup procedure will put the device into reset mode, and
1456 * it's not possible to talk to it without actually writing
1457 * to the DOCControl register. So we store the current contents
1458 * of the DOCControl register's location, in case we later decide
1459 * that it's not a DiskOnChip, and want to put it back how we
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001460 * found it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 */
1462 save_control = ReadDOC(virtadr, DOCControl);
1463
1464 /* Reset the DiskOnChip ASIC */
David Woodhousee0c7d762006-05-13 18:07:53 +01001465 WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_RESET, virtadr, DOCControl);
1466 WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_RESET, virtadr, DOCControl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467
1468 /* Enable the DiskOnChip ASIC */
David Woodhousee0c7d762006-05-13 18:07:53 +01001469 WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_NORMAL, virtadr, DOCControl);
1470 WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_NORMAL, virtadr, DOCControl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471
1472 ChipID = ReadDOC(virtadr, ChipID);
1473
David Woodhousee0c7d762006-05-13 18:07:53 +01001474 switch (ChipID) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 case DOC_ChipID_Doc2k:
1476 reg = DoC_2k_ECCStatus;
1477 break;
1478 case DOC_ChipID_DocMil:
1479 reg = DoC_ECCConf;
1480 break;
1481 case DOC_ChipID_DocMilPlus16:
1482 case DOC_ChipID_DocMilPlus32:
1483 case 0:
1484 /* Possible Millennium Plus, need to do more checks */
1485 /* Possibly release from power down mode */
1486 for (tmp = 0; (tmp < 4); tmp++)
1487 ReadDOC(virtadr, Mplus_Power);
1488
1489 /* Reset the Millennium Plus ASIC */
David Woodhousee0c7d762006-05-13 18:07:53 +01001490 tmp = DOC_MODE_RESET | DOC_MODE_MDWREN | DOC_MODE_RST_LAT | DOC_MODE_BDECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 WriteDOC(tmp, virtadr, Mplus_DOCControl);
1492 WriteDOC(~tmp, virtadr, Mplus_CtrlConfirm);
1493
1494 mdelay(1);
1495 /* Enable the Millennium Plus ASIC */
David Woodhousee0c7d762006-05-13 18:07:53 +01001496 tmp = DOC_MODE_NORMAL | DOC_MODE_MDWREN | DOC_MODE_RST_LAT | DOC_MODE_BDECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 WriteDOC(tmp, virtadr, Mplus_DOCControl);
1498 WriteDOC(~tmp, virtadr, Mplus_CtrlConfirm);
1499 mdelay(1);
1500
1501 ChipID = ReadDOC(virtadr, ChipID);
1502
1503 switch (ChipID) {
1504 case DOC_ChipID_DocMilPlus16:
1505 reg = DoC_Mplus_Toggle;
1506 break;
1507 case DOC_ChipID_DocMilPlus32:
1508 printk(KERN_ERR "DiskOnChip Millennium Plus 32MB is not supported, ignoring.\n");
1509 default:
1510 ret = -ENODEV;
1511 goto notfound;
1512 }
1513 break;
1514
1515 default:
1516 ret = -ENODEV;
1517 goto notfound;
1518 }
1519 /* Check the TOGGLE bit in the ECC register */
David Woodhousee0c7d762006-05-13 18:07:53 +01001520 tmp = ReadDOC_(virtadr, reg) & DOC_TOGGLE_BIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 tmpb = ReadDOC_(virtadr, reg) & DOC_TOGGLE_BIT;
1522 tmpc = ReadDOC_(virtadr, reg) & DOC_TOGGLE_BIT;
1523 if ((tmp == tmpb) || (tmp != tmpc)) {
1524 printk(KERN_WARNING "Possible DiskOnChip at 0x%lx failed TOGGLE test, dropping.\n", physadr);
1525 ret = -ENODEV;
1526 goto notfound;
1527 }
1528
1529 for (mtd = doclist; mtd; mtd = doc->nextdoc) {
1530 unsigned char oldval;
1531 unsigned char newval;
1532 nand = mtd->priv;
1533 doc = nand->priv;
1534 /* Use the alias resolution register to determine if this is
1535 in fact the same DOC aliased to a new address. If writes
1536 to one chip's alias resolution register change the value on
1537 the other chip, they're the same chip. */
1538 if (ChipID == DOC_ChipID_DocMilPlus16) {
1539 oldval = ReadDOC(doc->virtadr, Mplus_AliasResolution);
1540 newval = ReadDOC(virtadr, Mplus_AliasResolution);
1541 } else {
1542 oldval = ReadDOC(doc->virtadr, AliasResolution);
1543 newval = ReadDOC(virtadr, AliasResolution);
1544 }
1545 if (oldval != newval)
1546 continue;
1547 if (ChipID == DOC_ChipID_DocMilPlus16) {
1548 WriteDOC(~newval, virtadr, Mplus_AliasResolution);
1549 oldval = ReadDOC(doc->virtadr, Mplus_AliasResolution);
David Woodhousee0c7d762006-05-13 18:07:53 +01001550 WriteDOC(newval, virtadr, Mplus_AliasResolution); // restore it
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 } else {
1552 WriteDOC(~newval, virtadr, AliasResolution);
1553 oldval = ReadDOC(doc->virtadr, AliasResolution);
David Woodhousee0c7d762006-05-13 18:07:53 +01001554 WriteDOC(newval, virtadr, AliasResolution); // restore it
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 }
1556 newval = ~newval;
1557 if (oldval == newval) {
1558 printk(KERN_DEBUG "Found alias of DOC at 0x%lx to 0x%lx\n", doc->physadr, physadr);
1559 goto notfound;
1560 }
1561 }
1562
1563 printk(KERN_NOTICE "DiskOnChip found at 0x%lx\n", physadr);
1564
1565 len = sizeof(struct mtd_info) +
David Woodhousee0c7d762006-05-13 18:07:53 +01001566 sizeof(struct nand_chip) + sizeof(struct doc_priv) + (2 * sizeof(struct nand_bbt_descr));
Burman Yan95b93a02006-11-15 21:10:29 +02001567 mtd = kzalloc(len, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 if (!mtd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 ret = -ENOMEM;
1570 goto fail;
1571 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572
1573 nand = (struct nand_chip *) (mtd + 1);
1574 doc = (struct doc_priv *) (nand + 1);
1575 nand->bbt_td = (struct nand_bbt_descr *) (doc + 1);
1576 nand->bbt_md = nand->bbt_td + 1;
1577
1578 mtd->priv = nand;
1579 mtd->owner = THIS_MODULE;
1580
1581 nand->priv = doc;
1582 nand->select_chip = doc200x_select_chip;
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +02001583 nand->cmd_ctrl = doc200x_hwcontrol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 nand->dev_ready = doc200x_dev_ready;
1585 nand->waitfunc = doc200x_wait;
1586 nand->block_bad = doc200x_block_bad;
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02001587 nand->ecc.hwctl = doc200x_enable_hwecc;
1588 nand->ecc.calculate = doc200x_calculate_ecc;
1589 nand->ecc.correct = doc200x_correct_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02001591 nand->ecc.layout = &doc200x_oobinfo;
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02001592 nand->ecc.mode = NAND_ECC_HW_SYNDROME;
1593 nand->ecc.size = 512;
1594 nand->ecc.bytes = 6;
Mike Dunn6a918ba2012-03-11 14:21:11 -07001595 nand->ecc.strength = 2;
Brian Norrisbb9ebd4e2011-05-31 16:31:23 -07001596 nand->bbt_options = NAND_BBT_USE_FLASH;
Brian Norrisd24fe0c2015-02-28 02:13:10 -08001597 /* Skip the automatic BBT scan so we can run it manually */
1598 nand->options |= NAND_SKIP_BBTSCAN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599
1600 doc->physadr = physadr;
1601 doc->virtadr = virtadr;
1602 doc->ChipID = ChipID;
1603 doc->curfloor = -1;
1604 doc->curchip = -1;
1605 doc->mh0_page = -1;
1606 doc->mh1_page = -1;
1607 doc->nextdoc = doclist;
1608
1609 if (ChipID == DOC_ChipID_Doc2k)
1610 numchips = doc2000_init(mtd);
1611 else if (ChipID == DOC_ChipID_DocMilPlus16)
1612 numchips = doc2001plus_init(mtd);
1613 else
1614 numchips = doc2001_init(mtd);
1615
Brian Norrisd24fe0c2015-02-28 02:13:10 -08001616 if ((ret = nand_scan(mtd, numchips)) || (ret = doc->late_init(mtd))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 /* DBB note: i believe nand_release is necessary here, as
1618 buffers may have been allocated in nand_base. Check with
1619 Thomas. FIX ME! */
Jamie Iles0f47e952011-05-23 10:23:19 +01001620 /* nand_release will call mtd_device_unregister, but we
1621 haven't yet added it. This is handled without incident by
1622 mtd_device_unregister, as far as I can tell. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 nand_release(mtd);
1624 kfree(mtd);
1625 goto fail;
1626 }
1627
1628 /* Success! */
1629 doclist = mtd;
1630 return 0;
1631
David Woodhousee0c7d762006-05-13 18:07:53 +01001632 notfound:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 /* Put back the contents of the DOCControl register, in case it's not
1634 actually a DiskOnChip. */
1635 WriteDOC(save_control, virtadr, DOCControl);
David Woodhousee0c7d762006-05-13 18:07:53 +01001636 fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 iounmap(virtadr);
Alexander Shiyan4f0614a2013-11-13 15:59:25 +04001638
1639error_ioremap:
1640 release_mem_region(physadr, DOC_IOREMAP_LEN);
1641
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 return ret;
1643}
1644
1645static void release_nanddoc(void)
1646{
David Woodhousee0c7d762006-05-13 18:07:53 +01001647 struct mtd_info *mtd, *nextmtd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 struct nand_chip *nand;
1649 struct doc_priv *doc;
1650
1651 for (mtd = doclist; mtd; mtd = nextmtd) {
1652 nand = mtd->priv;
1653 doc = nand->priv;
1654
1655 nextmtd = doc->nextdoc;
1656 nand_release(mtd);
1657 iounmap(doc->virtadr);
Alexander Shiyan4f0614a2013-11-13 15:59:25 +04001658 release_mem_region(doc->physadr, DOC_IOREMAP_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 kfree(mtd);
1660 }
1661}
1662
1663static int __init init_nanddoc(void)
1664{
1665 int i, ret = 0;
1666
1667 /* We could create the decoder on demand, if memory is a concern.
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001668 * This way we have it handy, if an error happens
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 *
1670 * Symbolsize is 10 (bits)
1671 * Primitve polynomial is x^10+x^3+1
1672 * first consecutive root is 510
1673 * primitve element to generate roots = 1
1674 * generator polinomial degree = 4
1675 */
1676 rs_decoder = init_rs(10, 0x409, FCR, 1, NROOTS);
David Woodhousee0c7d762006-05-13 18:07:53 +01001677 if (!rs_decoder) {
1678 printk(KERN_ERR "DiskOnChip: Could not create a RS decoder\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 return -ENOMEM;
1680 }
1681
1682 if (doc_config_location) {
1683 printk(KERN_INFO "Using configured DiskOnChip probe address 0x%lx\n", doc_config_location);
1684 ret = doc_probe(doc_config_location);
1685 if (ret < 0)
1686 goto outerr;
1687 } else {
David Woodhousee0c7d762006-05-13 18:07:53 +01001688 for (i = 0; (doc_locations[i] != 0xffffffff); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 doc_probe(doc_locations[i]);
1690 }
1691 }
1692 /* No banner message any more. Print a message if no DiskOnChip
1693 found, so the user knows we at least tried. */
1694 if (!doclist) {
1695 printk(KERN_INFO "No valid DiskOnChip devices found\n");
1696 ret = -ENODEV;
1697 goto outerr;
1698 }
1699 return 0;
David Woodhousee0c7d762006-05-13 18:07:53 +01001700 outerr:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 free_rs(rs_decoder);
1702 return ret;
1703}
1704
1705static void __exit cleanup_nanddoc(void)
1706{
1707 /* Cleanup the nand/DoC resources */
1708 release_nanddoc();
1709
1710 /* Free the reed solomon resources */
1711 if (rs_decoder) {
1712 free_rs(rs_decoder);
1713 }
1714}
1715
1716module_init(init_nanddoc);
1717module_exit(cleanup_nanddoc);
1718
1719MODULE_LICENSE("GPL");
1720MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
Niels de Vos2a7af8c2009-01-30 11:08:35 +01001721MODULE_DESCRIPTION("M-Systems DiskOnChip 2000, Millennium and Millennium Plus device driver");