blob: f68a7bccecdc65878fac3ed3056ac499099992b7 [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;
72};
73
Linus Torvalds1da177e2005-04-16 15:20:36 -070074/* This is the syndrome computed by the HW ecc generator upon reading an empty
75 page, one with all 0xff for data and stored ecc code. */
76static u_char empty_read_syndrome[6] = { 0x26, 0xff, 0x6d, 0x47, 0x73, 0x7a };
David Woodhousee0c7d762006-05-13 18:07:53 +010077
Linus Torvalds1da177e2005-04-16 15:20:36 -070078/* This is the ecc value computed by the HW ecc generator upon writing an empty
79 page, one with all 0xff for data. */
80static u_char empty_write_ecc[6] = { 0x4b, 0x00, 0xe2, 0x0e, 0x93, 0xf7 };
81
82#define INFTL_BBT_RESERVED_BLOCKS 4
83
84#define DoC_is_MillenniumPlus(doc) ((doc)->ChipID == DOC_ChipID_DocMilPlus16 || (doc)->ChipID == DOC_ChipID_DocMilPlus32)
85#define DoC_is_Millennium(doc) ((doc)->ChipID == DOC_ChipID_DocMil)
86#define DoC_is_2000(doc) ((doc)->ChipID == DOC_ChipID_Doc2k)
87
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +020088static void doc200x_hwcontrol(struct mtd_info *mtd, int cmd,
89 unsigned int bitmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090static void doc200x_select_chip(struct mtd_info *mtd, int chip);
91
David Woodhousee0c7d762006-05-13 18:07:53 +010092static int debug = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093module_param(debug, int, 0);
94
David Woodhousee0c7d762006-05-13 18:07:53 +010095static int try_dword = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096module_param(try_dword, int, 0);
97
David Woodhousee0c7d762006-05-13 18:07:53 +010098static int no_ecc_failures = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099module_param(no_ecc_failures, int, 0);
100
David Woodhousee0c7d762006-05-13 18:07:53 +0100101static int no_autopart = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102module_param(no_autopart, int, 0);
Dan Brown1a78ff62005-03-29 21:57:48 +0100103
David Woodhousee0c7d762006-05-13 18:07:53 +0100104static int show_firmware_partition = 0;
Dan Brown1a78ff62005-03-29 21:57:48 +0100105module_param(show_firmware_partition, int, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
Robert P. J. Day89e2bf62007-03-07 18:33:25 -0500107#ifdef CONFIG_MTD_NAND_DISKONCHIP_BBTWRITE
David Woodhousee0c7d762006-05-13 18:07:53 +0100108static int inftl_bbt_write = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109#else
David Woodhousee0c7d762006-05-13 18:07:53 +0100110static int inftl_bbt_write = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111#endif
112module_param(inftl_bbt_write, int, 0);
113
Thomas Gleixner651078b2005-01-31 20:36:46 +0000114static unsigned long doc_config_location = CONFIG_MTD_NAND_DISKONCHIP_PROBE_ADDRESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115module_param(doc_config_location, ulong, 0);
116MODULE_PARM_DESC(doc_config_location, "Physical memory address at which to probe for DiskOnChip");
117
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118/* Sector size for HW ECC */
119#define SECTOR_SIZE 512
120/* The sector bytes are packed into NB_DATA 10 bit words */
121#define NB_DATA (((SECTOR_SIZE + 1) * 8 + 6) / 10)
122/* Number of roots */
123#define NROOTS 4
124/* First consective root */
125#define FCR 510
126/* Number of symbols */
127#define NN 1023
128
129/* the Reed Solomon control structure */
130static struct rs_control *rs_decoder;
131
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000132/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 * The HW decoder in the DoC ASIC's provides us a error syndrome,
Brian Norris7854d3f2011-06-23 14:12:08 -0700134 * which we must convert to a standard syndrome usable by the generic
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 * Reed-Solomon library code.
136 *
137 * Fabrice Bellard figured this out in the old docecc code. I added
138 * some comments, improved a minor bit and converted it to make use
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300139 * of the generic Reed-Solomon library. tglx
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100141static int doc_ecc_decode(struct rs_control *rs, uint8_t *data, uint8_t *ecc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142{
143 int i, j, nerr, errpos[8];
144 uint8_t parity;
145 uint16_t ds[4], s[5], tmp, errval[8], syn[4];
146
Mark Warec9fb6772010-05-27 11:45:39 +1000147 memset(syn, 0, sizeof(syn));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 /* Convert the ecc bytes into words */
149 ds[0] = ((ecc[4] & 0xff) >> 0) | ((ecc[5] & 0x03) << 8);
150 ds[1] = ((ecc[5] & 0xfc) >> 2) | ((ecc[2] & 0x0f) << 6);
151 ds[2] = ((ecc[2] & 0xf0) >> 4) | ((ecc[3] & 0x3f) << 4);
152 ds[3] = ((ecc[3] & 0xc0) >> 6) | ((ecc[0] & 0xff) << 2);
153 parity = ecc[1];
154
Brian Norris7854d3f2011-06-23 14:12:08 -0700155 /* Initialize the syndrome buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 for (i = 0; i < NROOTS; i++)
157 s[i] = ds[0];
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000158 /*
159 * Evaluate
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 * s[i] = ds[3]x^3 + ds[2]x^2 + ds[1]x^1 + ds[0]
161 * where x = alpha^(FCR + i)
162 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100163 for (j = 1; j < NROOTS; j++) {
164 if (ds[j] == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 continue;
166 tmp = rs->index_of[ds[j]];
David Woodhousee0c7d762006-05-13 18:07:53 +0100167 for (i = 0; i < NROOTS; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 s[i] ^= rs->alpha_to[rs_modnn(rs, tmp + (FCR + i) * j)];
169 }
170
Mark Warec9fb6772010-05-27 11:45:39 +1000171 /* Calc syn[i] = s[i] / alpha^(v + i) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 for (i = 0; i < NROOTS; i++) {
Mark Warec9fb6772010-05-27 11:45:39 +1000173 if (s[i])
David Woodhousee0c7d762006-05-13 18:07:53 +0100174 syn[i] = rs_modnn(rs, rs->index_of[s[i]] + (NN - FCR - i));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 }
176 /* Call the decoder library */
177 nerr = decode_rs16(rs, NULL, NULL, 1019, syn, 0, errpos, 0, errval);
178
179 /* Incorrectable errors ? */
180 if (nerr < 0)
181 return nerr;
182
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000183 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 * Correct the errors. The bitpositions are a bit of magic,
185 * but they are given by the design of the de/encoder circuit
186 * in the DoC ASIC's.
187 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100188 for (i = 0; i < nerr; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 int index, bitpos, pos = 1015 - errpos[i];
190 uint8_t val;
191 if (pos >= NB_DATA && pos < 1019)
192 continue;
193 if (pos < NB_DATA) {
194 /* extract bit position (MSB first) */
195 pos = 10 * (NB_DATA - 1 - pos) - 6;
196 /* now correct the following 10 bits. At most two bytes
197 can be modified since pos is even */
198 index = (pos >> 3) ^ 1;
199 bitpos = pos & 7;
David Woodhousee0c7d762006-05-13 18:07:53 +0100200 if ((index >= 0 && index < SECTOR_SIZE) || index == (SECTOR_SIZE + 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 val = (uint8_t) (errval[i] >> (2 + bitpos));
202 parity ^= val;
203 if (index < SECTOR_SIZE)
204 data[index] ^= val;
205 }
206 index = ((pos >> 3) + 1) ^ 1;
207 bitpos = (bitpos + 10) & 7;
208 if (bitpos == 0)
209 bitpos = 8;
David Woodhousee0c7d762006-05-13 18:07:53 +0100210 if ((index >= 0 && index < SECTOR_SIZE) || index == (SECTOR_SIZE + 1)) {
211 val = (uint8_t) (errval[i] << (8 - bitpos));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 parity ^= val;
213 if (index < SECTOR_SIZE)
214 data[index] ^= val;
215 }
216 }
217 }
218 /* If the parity is wrong, no rescue possible */
Jörn Engeleb684502007-10-20 23:16:32 +0200219 return parity ? -EBADMSG : nerr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220}
221
222static void DoC_Delay(struct doc_priv *doc, unsigned short cycles)
223{
224 volatile char dummy;
225 int i;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000226
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 for (i = 0; i < cycles; i++) {
228 if (DoC_is_Millennium(doc))
229 dummy = ReadDOC(doc->virtadr, NOP);
230 else if (DoC_is_MillenniumPlus(doc))
231 dummy = ReadDOC(doc->virtadr, Mplus_NOP);
232 else
233 dummy = ReadDOC(doc->virtadr, DOCStatus);
234 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000235
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236}
237
238#define CDSN_CTRL_FR_B_MASK (CDSN_CTRL_FR_B0 | CDSN_CTRL_FR_B1)
239
240/* DOC_WaitReady: Wait for RDY line to be asserted by the flash chip */
241static int _DoC_WaitReady(struct doc_priv *doc)
242{
David Woodhousee0c7d762006-05-13 18:07:53 +0100243 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 unsigned long timeo = jiffies + (HZ * 10);
245
David Woodhousee0c7d762006-05-13 18:07:53 +0100246 if (debug)
247 printk("_DoC_WaitReady...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 /* Out-of-line routine to wait for chip response */
249 if (DoC_is_MillenniumPlus(doc)) {
250 while ((ReadDOC(docptr, Mplus_FlashControl) & CDSN_CTRL_FR_B_MASK) != CDSN_CTRL_FR_B_MASK) {
251 if (time_after(jiffies, timeo)) {
252 printk("_DoC_WaitReady timed out.\n");
253 return -EIO;
254 }
255 udelay(1);
256 cond_resched();
257 }
258 } else {
259 while (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B)) {
260 if (time_after(jiffies, timeo)) {
261 printk("_DoC_WaitReady timed out.\n");
262 return -EIO;
263 }
264 udelay(1);
265 cond_resched();
266 }
267 }
268
269 return 0;
270}
271
272static inline int DoC_WaitReady(struct doc_priv *doc)
273{
David Woodhousee0c7d762006-05-13 18:07:53 +0100274 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 int ret = 0;
276
277 if (DoC_is_MillenniumPlus(doc)) {
278 DoC_Delay(doc, 4);
279
280 if ((ReadDOC(docptr, Mplus_FlashControl) & CDSN_CTRL_FR_B_MASK) != CDSN_CTRL_FR_B_MASK)
281 /* Call the out-of-line routine to wait */
282 ret = _DoC_WaitReady(doc);
283 } else {
284 DoC_Delay(doc, 4);
285
286 if (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B))
287 /* Call the out-of-line routine to wait */
288 ret = _DoC_WaitReady(doc);
289 DoC_Delay(doc, 2);
290 }
291
David Woodhousee0c7d762006-05-13 18:07:53 +0100292 if (debug)
293 printk("DoC_WaitReady OK\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 return ret;
295}
296
297static void doc2000_write_byte(struct mtd_info *mtd, u_char datum)
298{
299 struct nand_chip *this = mtd->priv;
300 struct doc_priv *doc = this->priv;
David Woodhousee0c7d762006-05-13 18:07:53 +0100301 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
David Woodhousee0c7d762006-05-13 18:07:53 +0100303 if (debug)
304 printk("write_byte %02x\n", datum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 WriteDOC(datum, docptr, CDSNSlowIO);
306 WriteDOC(datum, docptr, 2k_CDSN_IO);
307}
308
309static u_char doc2000_read_byte(struct mtd_info *mtd)
310{
311 struct nand_chip *this = mtd->priv;
312 struct doc_priv *doc = this->priv;
David Woodhousee0c7d762006-05-13 18:07:53 +0100313 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 u_char ret;
315
316 ReadDOC(docptr, CDSNSlowIO);
317 DoC_Delay(doc, 2);
318 ret = ReadDOC(docptr, 2k_CDSN_IO);
David Woodhousee0c7d762006-05-13 18:07:53 +0100319 if (debug)
320 printk("read_byte returns %02x\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 return ret;
322}
323
David Woodhousee0c7d762006-05-13 18:07:53 +0100324static void doc2000_writebuf(struct mtd_info *mtd, const u_char *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325{
326 struct nand_chip *this = mtd->priv;
327 struct doc_priv *doc = this->priv;
David Woodhousee0c7d762006-05-13 18:07:53 +0100328 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 int i;
David Woodhousee0c7d762006-05-13 18:07:53 +0100330 if (debug)
331 printk("writebuf of %d bytes: ", len);
332 for (i = 0; i < len; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 WriteDOC_(buf[i], docptr, DoC_2k_CDSN_IO + i);
334 if (debug && i < 16)
335 printk("%02x ", buf[i]);
336 }
David Woodhousee0c7d762006-05-13 18:07:53 +0100337 if (debug)
338 printk("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339}
340
David Woodhousee0c7d762006-05-13 18:07:53 +0100341static void doc2000_readbuf(struct mtd_info *mtd, u_char *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342{
343 struct nand_chip *this = mtd->priv;
344 struct doc_priv *doc = this->priv;
David Woodhousee0c7d762006-05-13 18:07:53 +0100345 void __iomem *docptr = doc->virtadr;
346 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
David Woodhousee0c7d762006-05-13 18:07:53 +0100348 if (debug)
349 printk("readbuf of %d bytes: ", len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
David Woodhousee0c7d762006-05-13 18:07:53 +0100351 for (i = 0; i < len; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 buf[i] = ReadDOC(docptr, 2k_CDSN_IO + i);
353 }
354}
355
David Woodhousee0c7d762006-05-13 18:07:53 +0100356static void doc2000_readbuf_dword(struct mtd_info *mtd, u_char *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357{
358 struct nand_chip *this = mtd->priv;
359 struct doc_priv *doc = this->priv;
David Woodhousee0c7d762006-05-13 18:07:53 +0100360 void __iomem *docptr = doc->virtadr;
361 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
David Woodhousee0c7d762006-05-13 18:07:53 +0100363 if (debug)
364 printk("readbuf_dword of %d bytes: ", len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
David Woodhousee0c7d762006-05-13 18:07:53 +0100366 if (unlikely((((unsigned long)buf) | len) & 3)) {
367 for (i = 0; i < len; i++) {
368 *(uint8_t *) (&buf[i]) = ReadDOC(docptr, 2k_CDSN_IO + i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 }
370 } else {
David Woodhousee0c7d762006-05-13 18:07:53 +0100371 for (i = 0; i < len; i += 4) {
372 *(uint32_t *) (&buf[i]) = readl(docptr + DoC_2k_CDSN_IO + i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 }
374 }
375}
376
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377static uint16_t __init doc200x_ident_chip(struct mtd_info *mtd, int nr)
378{
379 struct nand_chip *this = mtd->priv;
380 struct doc_priv *doc = this->priv;
381 uint16_t ret;
382
383 doc200x_select_chip(mtd, nr);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200384 doc200x_hwcontrol(mtd, NAND_CMD_READID,
385 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
386 doc200x_hwcontrol(mtd, 0, NAND_CTRL_ALE | NAND_CTRL_CHANGE);
387 doc200x_hwcontrol(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000388
Lucas De Marchie9c54992011-04-26 23:28:26 -0700389 /* We can't use dev_ready here, but at least we wait for the
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000390 * command to complete
Thomas Gleixnerdfd61292005-02-22 21:48:25 +0000391 */
392 udelay(50);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000393
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 ret = this->read_byte(mtd) << 8;
395 ret |= this->read_byte(mtd);
396
397 if (doc->ChipID == DOC_ChipID_Doc2k && try_dword && !nr) {
398 /* First chip probe. See if we get same results by 32-bit access */
399 union {
400 uint32_t dword;
401 uint8_t byte[4];
402 } ident;
403 void __iomem *docptr = doc->virtadr;
404
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200405 doc200x_hwcontrol(mtd, NAND_CMD_READID,
406 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
407 doc200x_hwcontrol(mtd, 0, NAND_CTRL_ALE | NAND_CTRL_CHANGE);
408 doc200x_hwcontrol(mtd, NAND_CMD_NONE,
409 NAND_NCE | NAND_CTRL_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
Thomas Gleixnerdfd61292005-02-22 21:48:25 +0000411 udelay(50);
412
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 ident.dword = readl(docptr + DoC_2k_CDSN_IO);
414 if (((ident.byte[0] << 8) | ident.byte[1]) == ret) {
415 printk(KERN_INFO "DiskOnChip 2000 responds to DWORD access\n");
416 this->read_buf = &doc2000_readbuf_dword;
417 }
418 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000419
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 return ret;
421}
422
423static void __init doc2000_count_chips(struct mtd_info *mtd)
424{
425 struct nand_chip *this = mtd->priv;
426 struct doc_priv *doc = this->priv;
427 uint16_t mfrid;
428 int i;
429
430 /* Max 4 chips per floor on DiskOnChip 2000 */
431 doc->chips_per_floor = 4;
432
433 /* Find out what the first chip is */
434 mfrid = doc200x_ident_chip(mtd, 0);
435
436 /* Find how many chips in each floor. */
437 for (i = 1; i < 4; i++) {
438 if (doc200x_ident_chip(mtd, i) != mfrid)
439 break;
440 }
441 doc->chips_per_floor = i;
442 printk(KERN_DEBUG "Detected %d chips per floor.\n", i);
443}
444
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200445static int doc200x_wait(struct mtd_info *mtd, struct nand_chip *this)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446{
447 struct doc_priv *doc = this->priv;
448
449 int status;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000450
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 DoC_WaitReady(doc);
452 this->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
453 DoC_WaitReady(doc);
454 status = (int)this->read_byte(mtd);
455
456 return status;
457}
458
459static void doc2001_write_byte(struct mtd_info *mtd, u_char datum)
460{
461 struct nand_chip *this = mtd->priv;
462 struct doc_priv *doc = this->priv;
David Woodhousee0c7d762006-05-13 18:07:53 +0100463 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
465 WriteDOC(datum, docptr, CDSNSlowIO);
466 WriteDOC(datum, docptr, Mil_CDSN_IO);
467 WriteDOC(datum, docptr, WritePipeTerm);
468}
469
470static u_char doc2001_read_byte(struct mtd_info *mtd)
471{
472 struct nand_chip *this = mtd->priv;
473 struct doc_priv *doc = this->priv;
David Woodhousee0c7d762006-05-13 18:07:53 +0100474 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
476 //ReadDOC(docptr, CDSNSlowIO);
477 /* 11.4.5 -- delay twice to allow extended length cycle */
478 DoC_Delay(doc, 2);
479 ReadDOC(docptr, ReadPipeInit);
480 //return ReadDOC(docptr, Mil_CDSN_IO);
481 return ReadDOC(docptr, LastDataRead);
482}
483
David Woodhousee0c7d762006-05-13 18:07:53 +0100484static void doc2001_writebuf(struct mtd_info *mtd, const u_char *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485{
486 struct nand_chip *this = mtd->priv;
487 struct doc_priv *doc = this->priv;
David Woodhousee0c7d762006-05-13 18:07:53 +0100488 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 int i;
490
David Woodhousee0c7d762006-05-13 18:07:53 +0100491 for (i = 0; i < len; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 WriteDOC_(buf[i], docptr, DoC_Mil_CDSN_IO + i);
493 /* Terminate write pipeline */
494 WriteDOC(0x00, docptr, WritePipeTerm);
495}
496
David Woodhousee0c7d762006-05-13 18:07:53 +0100497static void doc2001_readbuf(struct mtd_info *mtd, u_char *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498{
499 struct nand_chip *this = mtd->priv;
500 struct doc_priv *doc = this->priv;
David Woodhousee0c7d762006-05-13 18:07:53 +0100501 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 int i;
503
504 /* Start read pipeline */
505 ReadDOC(docptr, ReadPipeInit);
506
David Woodhousee0c7d762006-05-13 18:07:53 +0100507 for (i = 0; i < len - 1; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 buf[i] = ReadDOC(docptr, Mil_CDSN_IO + (i & 0xff));
509
510 /* Terminate read pipeline */
511 buf[i] = ReadDOC(docptr, LastDataRead);
512}
513
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514static u_char doc2001plus_read_byte(struct mtd_info *mtd)
515{
516 struct nand_chip *this = mtd->priv;
517 struct doc_priv *doc = this->priv;
David Woodhousee0c7d762006-05-13 18:07:53 +0100518 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 u_char ret;
520
David Woodhousee0c7d762006-05-13 18:07:53 +0100521 ReadDOC(docptr, Mplus_ReadPipeInit);
522 ReadDOC(docptr, Mplus_ReadPipeInit);
523 ret = ReadDOC(docptr, Mplus_LastDataRead);
524 if (debug)
525 printk("read_byte returns %02x\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 return ret;
527}
528
David Woodhousee0c7d762006-05-13 18:07:53 +0100529static void doc2001plus_writebuf(struct mtd_info *mtd, const u_char *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530{
531 struct nand_chip *this = mtd->priv;
532 struct doc_priv *doc = this->priv;
David Woodhousee0c7d762006-05-13 18:07:53 +0100533 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 int i;
535
David Woodhousee0c7d762006-05-13 18:07:53 +0100536 if (debug)
537 printk("writebuf of %d bytes: ", len);
538 for (i = 0; i < len; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 WriteDOC_(buf[i], docptr, DoC_Mil_CDSN_IO + i);
540 if (debug && i < 16)
541 printk("%02x ", buf[i]);
542 }
David Woodhousee0c7d762006-05-13 18:07:53 +0100543 if (debug)
544 printk("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545}
546
David Woodhousee0c7d762006-05-13 18:07:53 +0100547static void doc2001plus_readbuf(struct mtd_info *mtd, u_char *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548{
549 struct nand_chip *this = mtd->priv;
550 struct doc_priv *doc = this->priv;
David Woodhousee0c7d762006-05-13 18:07:53 +0100551 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 int i;
553
David Woodhousee0c7d762006-05-13 18:07:53 +0100554 if (debug)
555 printk("readbuf of %d bytes: ", len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
557 /* Start read pipeline */
558 ReadDOC(docptr, Mplus_ReadPipeInit);
559 ReadDOC(docptr, Mplus_ReadPipeInit);
560
David Woodhousee0c7d762006-05-13 18:07:53 +0100561 for (i = 0; i < len - 2; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 buf[i] = ReadDOC(docptr, Mil_CDSN_IO);
563 if (debug && i < 16)
564 printk("%02x ", buf[i]);
565 }
566
567 /* Terminate read pipeline */
David Woodhousee0c7d762006-05-13 18:07:53 +0100568 buf[len - 2] = ReadDOC(docptr, Mplus_LastDataRead);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 if (debug && i < 16)
David Woodhousee0c7d762006-05-13 18:07:53 +0100570 printk("%02x ", buf[len - 2]);
571 buf[len - 1] = 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 - 1]);
574 if (debug)
575 printk("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576}
577
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578static void doc2001plus_select_chip(struct mtd_info *mtd, int chip)
579{
580 struct nand_chip *this = mtd->priv;
581 struct doc_priv *doc = this->priv;
David Woodhousee0c7d762006-05-13 18:07:53 +0100582 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 int floor = 0;
584
David Woodhousee0c7d762006-05-13 18:07:53 +0100585 if (debug)
586 printk("select chip (%d)\n", chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
588 if (chip == -1) {
589 /* Disable flash internally */
590 WriteDOC(0, docptr, Mplus_FlashSelect);
591 return;
592 }
593
594 floor = chip / doc->chips_per_floor;
David Woodhousee0c7d762006-05-13 18:07:53 +0100595 chip -= (floor * doc->chips_per_floor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
597 /* Assert ChipEnable and deassert WriteProtect */
598 WriteDOC((DOC_FLASH_CE), docptr, Mplus_FlashSelect);
599 this->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
600
601 doc->curchip = chip;
602 doc->curfloor = floor;
603}
604
605static void doc200x_select_chip(struct mtd_info *mtd, int chip)
606{
607 struct nand_chip *this = mtd->priv;
608 struct doc_priv *doc = this->priv;
David Woodhousee0c7d762006-05-13 18:07:53 +0100609 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 int floor = 0;
611
David Woodhousee0c7d762006-05-13 18:07:53 +0100612 if (debug)
613 printk("select chip (%d)\n", chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
615 if (chip == -1)
616 return;
617
618 floor = chip / doc->chips_per_floor;
David Woodhousee0c7d762006-05-13 18:07:53 +0100619 chip -= (floor * doc->chips_per_floor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
621 /* 11.4.4 -- deassert CE before changing chip */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200622 doc200x_hwcontrol(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
624 WriteDOC(floor, docptr, FloorSelect);
625 WriteDOC(chip, docptr, CDSNDeviceSelect);
626
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200627 doc200x_hwcontrol(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
629 doc->curchip = chip;
630 doc->curfloor = floor;
631}
632
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200633#define CDSN_CTRL_MSK (CDSN_CTRL_CE | CDSN_CTRL_CLE | CDSN_CTRL_ALE)
634
635static void doc200x_hwcontrol(struct mtd_info *mtd, int cmd,
636 unsigned int ctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637{
638 struct nand_chip *this = mtd->priv;
639 struct doc_priv *doc = this->priv;
David Woodhousee0c7d762006-05-13 18:07:53 +0100640 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200642 if (ctrl & NAND_CTRL_CHANGE) {
643 doc->CDSNControl &= ~CDSN_CTRL_MSK;
644 doc->CDSNControl |= ctrl & CDSN_CTRL_MSK;
645 if (debug)
646 printk("hwcontrol(%d): %02x\n", cmd, doc->CDSNControl);
647 WriteDOC(doc->CDSNControl, docptr, CDSNControl);
648 /* 11.4.3 -- 4 NOPs after CSDNControl write */
649 DoC_Delay(doc, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 }
Thomas Gleixnercad74f22006-05-23 23:28:48 +0200651 if (cmd != NAND_CMD_NONE) {
652 if (DoC_is_2000(doc))
653 doc2000_write_byte(mtd, cmd);
654 else
655 doc2001_write_byte(mtd, cmd);
656 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657}
658
David Woodhousee0c7d762006-05-13 18:07:53 +0100659static void doc2001plus_command(struct mtd_info *mtd, unsigned command, int column, int page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660{
661 struct nand_chip *this = mtd->priv;
662 struct doc_priv *doc = this->priv;
David Woodhousee0c7d762006-05-13 18:07:53 +0100663 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
665 /*
666 * Must terminate write pipeline before sending any commands
667 * to the device.
668 */
669 if (command == NAND_CMD_PAGEPROG) {
670 WriteDOC(0x00, docptr, Mplus_WritePipeTerm);
671 WriteDOC(0x00, docptr, Mplus_WritePipeTerm);
672 }
673
674 /*
675 * Write out the command to the device.
676 */
677 if (command == NAND_CMD_SEQIN) {
678 int readcmd;
679
Joern Engel28318772006-05-22 23:18:05 +0200680 if (column >= mtd->writesize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 /* OOB area */
Joern Engel28318772006-05-22 23:18:05 +0200682 column -= mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 readcmd = NAND_CMD_READOOB;
684 } else if (column < 256) {
685 /* First 256 bytes --> READ0 */
686 readcmd = NAND_CMD_READ0;
687 } else {
688 column -= 256;
689 readcmd = NAND_CMD_READ1;
690 }
691 WriteDOC(readcmd, docptr, Mplus_FlashCmd);
692 }
693 WriteDOC(command, docptr, Mplus_FlashCmd);
694 WriteDOC(0, docptr, Mplus_WritePipeTerm);
695 WriteDOC(0, docptr, Mplus_WritePipeTerm);
696
697 if (column != -1 || page_addr != -1) {
698 /* Serially input address */
699 if (column != -1) {
700 /* Adjust columns for 16 bit buswidth */
Brian Norris3dad2342014-01-29 14:08:12 -0800701 if (this->options & NAND_BUSWIDTH_16 &&
702 !nand_opcode_8bits(command))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 column >>= 1;
704 WriteDOC(column, docptr, Mplus_FlashAddress);
705 }
706 if (page_addr != -1) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100707 WriteDOC((unsigned char)(page_addr & 0xff), docptr, Mplus_FlashAddress);
708 WriteDOC((unsigned char)((page_addr >> 8) & 0xff), docptr, Mplus_FlashAddress);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 /* One more address cycle for higher density devices */
710 if (this->chipsize & 0x0c000000) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100711 WriteDOC((unsigned char)((page_addr >> 16) & 0x0f), docptr, Mplus_FlashAddress);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 printk("high density\n");
713 }
714 }
715 WriteDOC(0, docptr, Mplus_WritePipeTerm);
716 WriteDOC(0, docptr, Mplus_WritePipeTerm);
717 /* deassert ALE */
David Woodhousee0c7d762006-05-13 18:07:53 +0100718 if (command == NAND_CMD_READ0 || command == NAND_CMD_READ1 ||
719 command == NAND_CMD_READOOB || command == NAND_CMD_READID)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 WriteDOC(0, docptr, Mplus_FlashControl);
721 }
722
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000723 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 * program and erase have their own busy handlers
725 * status and sequential in needs no delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100726 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 switch (command) {
728
729 case NAND_CMD_PAGEPROG:
730 case NAND_CMD_ERASE1:
731 case NAND_CMD_ERASE2:
732 case NAND_CMD_SEQIN:
733 case NAND_CMD_STATUS:
734 return;
735
736 case NAND_CMD_RESET:
737 if (this->dev_ready)
738 break;
739 udelay(this->chip_delay);
740 WriteDOC(NAND_CMD_STATUS, docptr, Mplus_FlashCmd);
741 WriteDOC(0, docptr, Mplus_WritePipeTerm);
742 WriteDOC(0, docptr, Mplus_WritePipeTerm);
David Woodhousee0c7d762006-05-13 18:07:53 +0100743 while (!(this->read_byte(mtd) & 0x40)) ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 return;
745
David Woodhousee0c7d762006-05-13 18:07:53 +0100746 /* This applies to read commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000748 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 * If we don't have access to the busy pin, we apply the given
750 * command delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100751 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 if (!this->dev_ready) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100753 udelay(this->chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 return;
755 }
756 }
757
758 /* Apply this short delay always to ensure that we do wait tWB in
759 * any case on any machine. */
David Woodhousee0c7d762006-05-13 18:07:53 +0100760 ndelay(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 /* wait until command is processed */
David Woodhousee0c7d762006-05-13 18:07:53 +0100762 while (!this->dev_ready(mtd)) ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763}
764
765static int doc200x_dev_ready(struct mtd_info *mtd)
766{
767 struct nand_chip *this = mtd->priv;
768 struct doc_priv *doc = this->priv;
David Woodhousee0c7d762006-05-13 18:07:53 +0100769 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
771 if (DoC_is_MillenniumPlus(doc)) {
772 /* 11.4.2 -- must NOP four times before checking FR/B# */
773 DoC_Delay(doc, 4);
774 if ((ReadDOC(docptr, Mplus_FlashControl) & CDSN_CTRL_FR_B_MASK) != CDSN_CTRL_FR_B_MASK) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100775 if (debug)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 printk("not ready\n");
777 return 0;
778 }
David Woodhousee0c7d762006-05-13 18:07:53 +0100779 if (debug)
780 printk("was ready\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 return 1;
782 } else {
783 /* 11.4.2 -- must NOP four times before checking FR/B# */
784 DoC_Delay(doc, 4);
785 if (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B)) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100786 if (debug)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 printk("not ready\n");
788 return 0;
789 }
790 /* 11.4.2 -- Must NOP twice if it's ready */
791 DoC_Delay(doc, 2);
David Woodhousee0c7d762006-05-13 18:07:53 +0100792 if (debug)
793 printk("was ready\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 return 1;
795 }
796}
797
798static int doc200x_block_bad(struct mtd_info *mtd, loff_t ofs, int getchip)
799{
800 /* This is our last resort if we couldn't find or create a BBT. Just
801 pretend all blocks are good. */
802 return 0;
803}
804
805static void doc200x_enable_hwecc(struct mtd_info *mtd, int mode)
806{
807 struct nand_chip *this = mtd->priv;
808 struct doc_priv *doc = this->priv;
David Woodhousee0c7d762006-05-13 18:07:53 +0100809 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
811 /* Prime the ECC engine */
David Woodhousee0c7d762006-05-13 18:07:53 +0100812 switch (mode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 case NAND_ECC_READ:
814 WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
815 WriteDOC(DOC_ECC_EN, docptr, ECCConf);
816 break;
817 case NAND_ECC_WRITE:
818 WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
819 WriteDOC(DOC_ECC_EN | DOC_ECC_RW, docptr, ECCConf);
820 break;
821 }
822}
823
824static void doc2001plus_enable_hwecc(struct mtd_info *mtd, int mode)
825{
826 struct nand_chip *this = mtd->priv;
827 struct doc_priv *doc = this->priv;
David Woodhousee0c7d762006-05-13 18:07:53 +0100828 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
830 /* Prime the ECC engine */
David Woodhousee0c7d762006-05-13 18:07:53 +0100831 switch (mode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 case NAND_ECC_READ:
833 WriteDOC(DOC_ECC_RESET, docptr, Mplus_ECCConf);
834 WriteDOC(DOC_ECC_EN, docptr, Mplus_ECCConf);
835 break;
836 case NAND_ECC_WRITE:
837 WriteDOC(DOC_ECC_RESET, docptr, Mplus_ECCConf);
838 WriteDOC(DOC_ECC_EN | DOC_ECC_RW, docptr, Mplus_ECCConf);
839 break;
840 }
841}
842
843/* This code is only called on write */
David Woodhousee0c7d762006-05-13 18:07:53 +0100844static int doc200x_calculate_ecc(struct mtd_info *mtd, const u_char *dat, unsigned char *ecc_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845{
846 struct nand_chip *this = mtd->priv;
847 struct doc_priv *doc = this->priv;
David Woodhousee0c7d762006-05-13 18:07:53 +0100848 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 int i;
850 int emptymatch = 1;
851
852 /* flush the pipeline */
853 if (DoC_is_2000(doc)) {
854 WriteDOC(doc->CDSNControl & ~CDSN_CTRL_FLASH_IO, docptr, CDSNControl);
855 WriteDOC(0, docptr, 2k_CDSN_IO);
856 WriteDOC(0, docptr, 2k_CDSN_IO);
857 WriteDOC(0, docptr, 2k_CDSN_IO);
858 WriteDOC(doc->CDSNControl, docptr, CDSNControl);
859 } else if (DoC_is_MillenniumPlus(doc)) {
860 WriteDOC(0, docptr, Mplus_NOP);
861 WriteDOC(0, docptr, Mplus_NOP);
862 WriteDOC(0, docptr, Mplus_NOP);
863 } else {
864 WriteDOC(0, docptr, NOP);
865 WriteDOC(0, docptr, NOP);
866 WriteDOC(0, docptr, NOP);
867 }
868
869 for (i = 0; i < 6; i++) {
870 if (DoC_is_MillenniumPlus(doc))
871 ecc_code[i] = ReadDOC_(docptr, DoC_Mplus_ECCSyndrome0 + i);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000872 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 ecc_code[i] = ReadDOC_(docptr, DoC_ECCSyndrome0 + i);
874 if (ecc_code[i] != empty_write_ecc[i])
875 emptymatch = 0;
876 }
877 if (DoC_is_MillenniumPlus(doc))
878 WriteDOC(DOC_ECC_DIS, docptr, Mplus_ECCConf);
879 else
880 WriteDOC(DOC_ECC_DIS, docptr, ECCConf);
881#if 0
882 /* If emptymatch=1, we might have an all-0xff data buffer. Check. */
883 if (emptymatch) {
884 /* Note: this somewhat expensive test should not be triggered
885 often. It could be optimized away by examining the data in
886 the writebuf routine, and remembering the result. */
887 for (i = 0; i < 512; i++) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100888 if (dat[i] == 0xff)
889 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 emptymatch = 0;
891 break;
892 }
893 }
894 /* If emptymatch still =1, we do have an all-0xff data buffer.
895 Return all-0xff ecc value instead of the computed one, so
896 it'll look just like a freshly-erased page. */
David Woodhousee0c7d762006-05-13 18:07:53 +0100897 if (emptymatch)
898 memset(ecc_code, 0xff, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899#endif
900 return 0;
901}
902
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200903static int doc200x_correct_data(struct mtd_info *mtd, u_char *dat,
904 u_char *read_ecc, u_char *isnull)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905{
906 int i, ret = 0;
907 struct nand_chip *this = mtd->priv;
908 struct doc_priv *doc = this->priv;
David Woodhousee0c7d762006-05-13 18:07:53 +0100909 void __iomem *docptr = doc->virtadr;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200910 uint8_t calc_ecc[6];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 volatile u_char dummy;
912 int emptymatch = 1;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000913
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 /* flush the pipeline */
915 if (DoC_is_2000(doc)) {
916 dummy = ReadDOC(docptr, 2k_ECCStatus);
917 dummy = ReadDOC(docptr, 2k_ECCStatus);
918 dummy = ReadDOC(docptr, 2k_ECCStatus);
919 } else if (DoC_is_MillenniumPlus(doc)) {
920 dummy = ReadDOC(docptr, Mplus_ECCConf);
921 dummy = ReadDOC(docptr, Mplus_ECCConf);
922 dummy = ReadDOC(docptr, Mplus_ECCConf);
923 } else {
924 dummy = ReadDOC(docptr, ECCConf);
925 dummy = ReadDOC(docptr, ECCConf);
926 dummy = ReadDOC(docptr, ECCConf);
927 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000928
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300929 /* Error occurred ? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 if (dummy & 0x80) {
931 for (i = 0; i < 6; i++) {
932 if (DoC_is_MillenniumPlus(doc))
933 calc_ecc[i] = ReadDOC_(docptr, DoC_Mplus_ECCSyndrome0 + i);
934 else
935 calc_ecc[i] = ReadDOC_(docptr, DoC_ECCSyndrome0 + i);
936 if (calc_ecc[i] != empty_read_syndrome[i])
937 emptymatch = 0;
938 }
939 /* If emptymatch=1, the read syndrome is consistent with an
940 all-0xff data and stored ecc block. Check the stored ecc. */
941 if (emptymatch) {
942 for (i = 0; i < 6; i++) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100943 if (read_ecc[i] == 0xff)
944 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 emptymatch = 0;
946 break;
947 }
948 }
949 /* If emptymatch still =1, check the data block. */
950 if (emptymatch) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100951 /* Note: this somewhat expensive test should not be triggered
952 often. It could be optimized away by examining the data in
953 the readbuf routine, and remembering the result. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 for (i = 0; i < 512; i++) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100955 if (dat[i] == 0xff)
956 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 emptymatch = 0;
958 break;
959 }
960 }
961 /* If emptymatch still =1, this is almost certainly a freshly-
962 erased block, in which case the ECC will not come out right.
963 We'll suppress the error and tell the caller everything's
964 OK. Because it is. */
David Woodhousee0c7d762006-05-13 18:07:53 +0100965 if (!emptymatch)
966 ret = doc_ecc_decode(rs_decoder, dat, calc_ecc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 if (ret > 0)
968 printk(KERN_ERR "doc200x_correct_data corrected %d errors\n", ret);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000969 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 if (DoC_is_MillenniumPlus(doc))
971 WriteDOC(DOC_ECC_DIS, docptr, Mplus_ECCConf);
972 else
973 WriteDOC(DOC_ECC_DIS, docptr, ECCConf);
Brian Norrisd57f40542011-09-20 18:34:25 -0700974 if (no_ecc_failures && mtd_is_eccerr(ret)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 printk(KERN_ERR "suppressing ECC failure\n");
976 ret = 0;
977 }
978 return ret;
979}
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000980
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981//u_char mydatabuf[528];
982
Dan Brownabc37e62005-04-07 15:22:58 +0100983/* The strange out-of-order .oobfree list below is a (possibly unneeded)
984 * attempt to retain compatibility. It used to read:
985 * .oobfree = { {8, 8} }
986 * Since that leaves two bytes unusable, it was changed. But the following
987 * scheme might affect existing jffs2 installs by moving the cleanmarker:
988 * .oobfree = { {6, 10} }
989 * jffs2 seems to handle the above gracefully, but the current scheme seems
990 * safer. The only problem with it is that any code that parses oobfree must
991 * be able to handle out-of-order segments.
992 */
Thomas Gleixner5bd34c02006-05-27 22:16:10 +0200993static struct nand_ecclayout doc200x_oobinfo = {
David Woodhousee0c7d762006-05-13 18:07:53 +0100994 .eccbytes = 6,
995 .eccpos = {0, 1, 2, 3, 4, 5},
996 .oobfree = {{8, 8}, {6, 2}}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997};
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000998
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999/* Find the (I)NFTL Media Header, and optionally also the mirror media header.
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001000 On successful return, buf will contain a copy of the media header for
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 further processing. id is the string to scan for, and will presumably be
1002 either "ANAND" or "BNAND". If findmirror=1, also look for the mirror media
1003 header. The page #s of the found media headers are placed in mh0_page and
1004 mh1_page in the DOC private structure. */
David Woodhousee0c7d762006-05-13 18:07:53 +01001005static int __init find_media_headers(struct mtd_info *mtd, u_char *buf, const char *id, int findmirror)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006{
1007 struct nand_chip *this = mtd->priv;
1008 struct doc_priv *doc = this->priv;
Dan Brown1a78ff62005-03-29 21:57:48 +01001009 unsigned offs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 int ret;
1011 size_t retlen;
1012
Dan Brown1a78ff62005-03-29 21:57:48 +01001013 for (offs = 0; offs < mtd->size; offs += mtd->erasesize) {
Artem Bityutskiy329ad392011-12-23 17:30:16 +02001014 ret = mtd_read(mtd, offs, mtd->writesize, &retlen, buf);
Joern Engel28318772006-05-22 23:18:05 +02001015 if (retlen != mtd->writesize)
David Woodhousee0c7d762006-05-13 18:07:53 +01001016 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 if (ret) {
David Woodhousee0c7d762006-05-13 18:07:53 +01001018 printk(KERN_WARNING "ECC error scanning DOC at 0x%x\n", offs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 }
David Woodhousee0c7d762006-05-13 18:07:53 +01001020 if (memcmp(buf, id, 6))
1021 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 printk(KERN_INFO "Found DiskOnChip %s Media Header at 0x%x\n", id, offs);
1023 if (doc->mh0_page == -1) {
1024 doc->mh0_page = offs >> this->page_shift;
David Woodhousee0c7d762006-05-13 18:07:53 +01001025 if (!findmirror)
1026 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 continue;
1028 }
1029 doc->mh1_page = offs >> this->page_shift;
1030 return 2;
1031 }
1032 if (doc->mh0_page == -1) {
1033 printk(KERN_WARNING "DiskOnChip %s Media Header not found.\n", id);
1034 return 0;
1035 }
1036 /* Only one mediaheader was found. We want buf to contain a
1037 mediaheader on return, so we'll have to re-read the one we found. */
1038 offs = doc->mh0_page << this->page_shift;
Artem Bityutskiy329ad392011-12-23 17:30:16 +02001039 ret = mtd_read(mtd, offs, mtd->writesize, &retlen, buf);
Joern Engel28318772006-05-22 23:18:05 +02001040 if (retlen != mtd->writesize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 /* Insanity. Give up. */
1042 printk(KERN_ERR "Read DiskOnChip Media Header once, but can't reread it???\n");
1043 return 0;
1044 }
1045 return 1;
1046}
1047
David Woodhousee0c7d762006-05-13 18:07:53 +01001048static inline int __init nftl_partscan(struct mtd_info *mtd, struct mtd_partition *parts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049{
1050 struct nand_chip *this = mtd->priv;
1051 struct doc_priv *doc = this->priv;
1052 int ret = 0;
1053 u_char *buf;
1054 struct NFTLMediaHeader *mh;
1055 const unsigned psize = 1 << this->page_shift;
Dan Brown1a78ff62005-03-29 21:57:48 +01001056 int numparts = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 unsigned blocks, maxblocks;
1058 int offs, numheaders;
1059
Joern Engel28318772006-05-22 23:18:05 +02001060 buf = kmalloc(mtd->writesize, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 if (!buf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 return 0;
1063 }
David Woodhousee0c7d762006-05-13 18:07:53 +01001064 if (!(numheaders = find_media_headers(mtd, buf, "ANAND", 1)))
1065 goto out;
1066 mh = (struct NFTLMediaHeader *)buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067
Harvey Harrison96372442008-07-30 12:34:57 -07001068 le16_to_cpus(&mh->NumEraseUnits);
1069 le16_to_cpus(&mh->FirstPhysicalEUN);
1070 le32_to_cpus(&mh->FormattedSize);
Thomas Gleixnerf29a4b82005-01-31 22:22:24 +00001071
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 printk(KERN_INFO " DataOrgID = %s\n"
1073 " NumEraseUnits = %d\n"
1074 " FirstPhysicalEUN = %d\n"
1075 " FormattedSize = %d\n"
1076 " UnitSizeFactor = %d\n",
1077 mh->DataOrgID, mh->NumEraseUnits,
1078 mh->FirstPhysicalEUN, mh->FormattedSize,
1079 mh->UnitSizeFactor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
1081 blocks = mtd->size >> this->phys_erase_shift;
1082 maxblocks = min(32768U, mtd->erasesize - psize);
1083
1084 if (mh->UnitSizeFactor == 0x00) {
1085 /* Auto-determine UnitSizeFactor. The constraints are:
1086 - There can be at most 32768 virtual blocks.
1087 - There can be at most (virtual block size - page size)
David Woodhousee0c7d762006-05-13 18:07:53 +01001088 virtual blocks (because MediaHeader+BBT must fit in 1).
1089 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 mh->UnitSizeFactor = 0xff;
1091 while (blocks > maxblocks) {
1092 blocks >>= 1;
1093 maxblocks = min(32768U, (maxblocks << 1) + psize);
1094 mh->UnitSizeFactor--;
1095 }
1096 printk(KERN_WARNING "UnitSizeFactor=0x00 detected. Correct value is assumed to be 0x%02x.\n", mh->UnitSizeFactor);
1097 }
1098
1099 /* NOTE: The lines below modify internal variables of the NAND and MTD
1100 layers; variables with have already been configured by nand_scan.
1101 Unfortunately, we didn't know before this point what these values
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001102 should be. Thus, this code is somewhat dependent on the exact
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 implementation of the NAND layer. */
1104 if (mh->UnitSizeFactor != 0xff) {
1105 this->bbt_erase_shift += (0xff - mh->UnitSizeFactor);
1106 mtd->erasesize <<= (0xff - mh->UnitSizeFactor);
1107 printk(KERN_INFO "Setting virtual erase size to %d\n", mtd->erasesize);
1108 blocks = mtd->size >> this->bbt_erase_shift;
1109 maxblocks = min(32768U, mtd->erasesize - psize);
1110 }
1111
1112 if (blocks > maxblocks) {
1113 printk(KERN_ERR "UnitSizeFactor of 0x%02x is inconsistent with device size. Aborting.\n", mh->UnitSizeFactor);
1114 goto out;
1115 }
1116
1117 /* Skip past the media headers. */
1118 offs = max(doc->mh0_page, doc->mh1_page);
1119 offs <<= this->page_shift;
1120 offs += mtd->erasesize;
1121
Dan Brown1a78ff62005-03-29 21:57:48 +01001122 if (show_firmware_partition == 1) {
1123 parts[0].name = " DiskOnChip Firmware / Media Header partition";
1124 parts[0].offset = 0;
1125 parts[0].size = offs;
1126 numparts = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 }
Dan Brown1a78ff62005-03-29 21:57:48 +01001128
1129 parts[numparts].name = " DiskOnChip BDTL partition";
1130 parts[numparts].offset = offs;
1131 parts[numparts].size = (mh->NumEraseUnits - numheaders) << this->bbt_erase_shift;
1132
1133 offs += parts[numparts].size;
1134 numparts++;
1135
1136 if (offs < mtd->size) {
1137 parts[numparts].name = " DiskOnChip Remainder partition";
1138 parts[numparts].offset = offs;
1139 parts[numparts].size = mtd->size - offs;
1140 numparts++;
1141 }
1142
1143 ret = numparts;
David Woodhousee0c7d762006-05-13 18:07:53 +01001144 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 kfree(buf);
1146 return ret;
1147}
1148
1149/* This is a stripped-down copy of the code in inftlmount.c */
David Woodhousee0c7d762006-05-13 18:07:53 +01001150static inline int __init inftl_partscan(struct mtd_info *mtd, struct mtd_partition *parts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151{
1152 struct nand_chip *this = mtd->priv;
1153 struct doc_priv *doc = this->priv;
1154 int ret = 0;
1155 u_char *buf;
1156 struct INFTLMediaHeader *mh;
1157 struct INFTLPartition *ip;
1158 int numparts = 0;
1159 int blocks;
1160 int vshift, lastvunit = 0;
1161 int i;
1162 int end = mtd->size;
1163
1164 if (inftl_bbt_write)
1165 end -= (INFTL_BBT_RESERVED_BLOCKS << this->phys_erase_shift);
1166
Joern Engel28318772006-05-22 23:18:05 +02001167 buf = kmalloc(mtd->writesize, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 if (!buf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 return 0;
1170 }
1171
David Woodhousee0c7d762006-05-13 18:07:53 +01001172 if (!find_media_headers(mtd, buf, "BNAND", 0))
1173 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 doc->mh1_page = doc->mh0_page + (4096 >> this->page_shift);
David Woodhousee0c7d762006-05-13 18:07:53 +01001175 mh = (struct INFTLMediaHeader *)buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176
Harvey Harrison96372442008-07-30 12:34:57 -07001177 le32_to_cpus(&mh->NoOfBootImageBlocks);
1178 le32_to_cpus(&mh->NoOfBinaryPartitions);
1179 le32_to_cpus(&mh->NoOfBDTLPartitions);
1180 le32_to_cpus(&mh->BlockMultiplierBits);
1181 le32_to_cpus(&mh->FormatFlags);
1182 le32_to_cpus(&mh->PercentUsed);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001183
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 printk(KERN_INFO " bootRecordID = %s\n"
1185 " NoOfBootImageBlocks = %d\n"
1186 " NoOfBinaryPartitions = %d\n"
1187 " NoOfBDTLPartitions = %d\n"
1188 " BlockMultiplerBits = %d\n"
1189 " FormatFlgs = %d\n"
1190 " OsakVersion = %d.%d.%d.%d\n"
1191 " PercentUsed = %d\n",
1192 mh->bootRecordID, mh->NoOfBootImageBlocks,
1193 mh->NoOfBinaryPartitions,
1194 mh->NoOfBDTLPartitions,
1195 mh->BlockMultiplierBits, mh->FormatFlags,
1196 ((unsigned char *) &mh->OsakVersion)[0] & 0xf,
1197 ((unsigned char *) &mh->OsakVersion)[1] & 0xf,
1198 ((unsigned char *) &mh->OsakVersion)[2] & 0xf,
1199 ((unsigned char *) &mh->OsakVersion)[3] & 0xf,
1200 mh->PercentUsed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201
1202 vshift = this->phys_erase_shift + mh->BlockMultiplierBits;
1203
1204 blocks = mtd->size >> vshift;
1205 if (blocks > 32768) {
1206 printk(KERN_ERR "BlockMultiplierBits=%d is inconsistent with device size. Aborting.\n", mh->BlockMultiplierBits);
1207 goto out;
1208 }
1209
1210 blocks = doc->chips_per_floor << (this->chip_shift - this->phys_erase_shift);
1211 if (inftl_bbt_write && (blocks > mtd->erasesize)) {
1212 printk(KERN_ERR "Writeable BBTs spanning more than one erase block are not yet supported. FIX ME!\n");
1213 goto out;
1214 }
1215
1216 /* Scan the partitions */
1217 for (i = 0; (i < 4); i++) {
1218 ip = &(mh->Partitions[i]);
Harvey Harrison96372442008-07-30 12:34:57 -07001219 le32_to_cpus(&ip->virtualUnits);
1220 le32_to_cpus(&ip->firstUnit);
1221 le32_to_cpus(&ip->lastUnit);
1222 le32_to_cpus(&ip->flags);
1223 le32_to_cpus(&ip->spareUnits);
1224 le32_to_cpus(&ip->Reserved0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 printk(KERN_INFO " PARTITION[%d] ->\n"
1227 " virtualUnits = %d\n"
1228 " firstUnit = %d\n"
1229 " lastUnit = %d\n"
1230 " flags = 0x%x\n"
1231 " spareUnits = %d\n",
1232 i, ip->virtualUnits, ip->firstUnit,
1233 ip->lastUnit, ip->flags,
1234 ip->spareUnits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235
Dan Brown1a78ff62005-03-29 21:57:48 +01001236 if ((show_firmware_partition == 1) &&
1237 (i == 0) && (ip->firstUnit > 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 parts[0].name = " DiskOnChip IPL / Media Header partition";
1239 parts[0].offset = 0;
1240 parts[0].size = mtd->erasesize * ip->firstUnit;
1241 numparts = 1;
1242 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243
1244 if (ip->flags & INFTL_BINARY)
1245 parts[numparts].name = " DiskOnChip BDK partition";
1246 else
1247 parts[numparts].name = " DiskOnChip BDTL partition";
1248 parts[numparts].offset = ip->firstUnit << vshift;
1249 parts[numparts].size = (1 + ip->lastUnit - ip->firstUnit) << vshift;
1250 numparts++;
David Woodhousee0c7d762006-05-13 18:07:53 +01001251 if (ip->lastUnit > lastvunit)
1252 lastvunit = ip->lastUnit;
1253 if (ip->flags & INFTL_LAST)
1254 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 }
1256 lastvunit++;
1257 if ((lastvunit << vshift) < end) {
1258 parts[numparts].name = " DiskOnChip Remainder partition";
1259 parts[numparts].offset = lastvunit << vshift;
1260 parts[numparts].size = end - parts[numparts].offset;
1261 numparts++;
1262 }
1263 ret = numparts;
David Woodhousee0c7d762006-05-13 18:07:53 +01001264 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 kfree(buf);
1266 return ret;
1267}
1268
1269static int __init nftl_scan_bbt(struct mtd_info *mtd)
1270{
1271 int ret, numparts;
1272 struct nand_chip *this = mtd->priv;
1273 struct doc_priv *doc = this->priv;
1274 struct mtd_partition parts[2];
1275
David Woodhousee0c7d762006-05-13 18:07:53 +01001276 memset((char *)parts, 0, sizeof(parts));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 /* On NFTL, we have to find the media headers before we can read the
1278 BBTs, since they're stored in the media header eraseblocks. */
1279 numparts = nftl_partscan(mtd, parts);
David Woodhousee0c7d762006-05-13 18:07:53 +01001280 if (!numparts)
1281 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 this->bbt_td->options = NAND_BBT_ABSPAGE | NAND_BBT_8BIT |
1283 NAND_BBT_SAVECONTENT | NAND_BBT_WRITE |
1284 NAND_BBT_VERSION;
1285 this->bbt_td->veroffs = 7;
1286 this->bbt_td->pages[0] = doc->mh0_page + 1;
1287 if (doc->mh1_page != -1) {
1288 this->bbt_md->options = NAND_BBT_ABSPAGE | NAND_BBT_8BIT |
1289 NAND_BBT_SAVECONTENT | NAND_BBT_WRITE |
1290 NAND_BBT_VERSION;
1291 this->bbt_md->veroffs = 7;
1292 this->bbt_md->pages[0] = doc->mh1_page + 1;
1293 } else {
1294 this->bbt_md = NULL;
1295 }
1296
1297 /* It's safe to set bd=NULL below because NAND_BBT_CREATE is not set.
1298 At least as nand_bbt.c is currently written. */
1299 if ((ret = nand_scan_bbt(mtd, NULL)))
1300 return ret;
Jamie Iles0f47e952011-05-23 10:23:19 +01001301 mtd_device_register(mtd, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 if (!no_autopart)
Jamie Iles0f47e952011-05-23 10:23:19 +01001303 mtd_device_register(mtd, parts, numparts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 return 0;
1305}
1306
1307static int __init inftl_scan_bbt(struct mtd_info *mtd)
1308{
1309 int ret, numparts;
1310 struct nand_chip *this = mtd->priv;
1311 struct doc_priv *doc = this->priv;
1312 struct mtd_partition parts[5];
1313
1314 if (this->numchips > doc->chips_per_floor) {
1315 printk(KERN_ERR "Multi-floor INFTL devices not yet supported.\n");
1316 return -EIO;
1317 }
1318
1319 if (DoC_is_MillenniumPlus(doc)) {
1320 this->bbt_td->options = NAND_BBT_2BIT | NAND_BBT_ABSPAGE;
1321 if (inftl_bbt_write)
1322 this->bbt_td->options |= NAND_BBT_WRITE;
1323 this->bbt_td->pages[0] = 2;
1324 this->bbt_md = NULL;
1325 } else {
David Woodhousee0c7d762006-05-13 18:07:53 +01001326 this->bbt_td->options = NAND_BBT_LASTBLOCK | NAND_BBT_8BIT | NAND_BBT_VERSION;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 if (inftl_bbt_write)
1328 this->bbt_td->options |= NAND_BBT_WRITE;
1329 this->bbt_td->offs = 8;
1330 this->bbt_td->len = 8;
1331 this->bbt_td->veroffs = 7;
1332 this->bbt_td->maxblocks = INFTL_BBT_RESERVED_BLOCKS;
1333 this->bbt_td->reserved_block_code = 0x01;
1334 this->bbt_td->pattern = "MSYS_BBT";
1335
David Woodhousee0c7d762006-05-13 18:07:53 +01001336 this->bbt_md->options = NAND_BBT_LASTBLOCK | NAND_BBT_8BIT | NAND_BBT_VERSION;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 if (inftl_bbt_write)
1338 this->bbt_md->options |= NAND_BBT_WRITE;
1339 this->bbt_md->offs = 8;
1340 this->bbt_md->len = 8;
1341 this->bbt_md->veroffs = 7;
1342 this->bbt_md->maxblocks = INFTL_BBT_RESERVED_BLOCKS;
1343 this->bbt_md->reserved_block_code = 0x01;
1344 this->bbt_md->pattern = "TBB_SYSM";
1345 }
1346
1347 /* It's safe to set bd=NULL below because NAND_BBT_CREATE is not set.
1348 At least as nand_bbt.c is currently written. */
1349 if ((ret = nand_scan_bbt(mtd, NULL)))
1350 return ret;
David Woodhousee0c7d762006-05-13 18:07:53 +01001351 memset((char *)parts, 0, sizeof(parts));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 numparts = inftl_partscan(mtd, parts);
1353 /* At least for now, require the INFTL Media Header. We could probably
1354 do without it for non-INFTL use, since all it gives us is
1355 autopartitioning, but I want to give it more thought. */
David Woodhousee0c7d762006-05-13 18:07:53 +01001356 if (!numparts)
1357 return -EIO;
Jamie Iles0f47e952011-05-23 10:23:19 +01001358 mtd_device_register(mtd, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 if (!no_autopart)
Jamie Iles0f47e952011-05-23 10:23:19 +01001360 mtd_device_register(mtd, parts, numparts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 return 0;
1362}
1363
1364static inline int __init doc2000_init(struct mtd_info *mtd)
1365{
1366 struct nand_chip *this = mtd->priv;
1367 struct doc_priv *doc = this->priv;
1368
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 this->read_byte = doc2000_read_byte;
1370 this->write_buf = doc2000_writebuf;
1371 this->read_buf = doc2000_readbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 this->scan_bbt = nftl_scan_bbt;
1373
1374 doc->CDSNControl = CDSN_CTRL_FLASH_IO | CDSN_CTRL_ECC_IO;
1375 doc2000_count_chips(mtd);
1376 mtd->name = "DiskOnChip 2000 (NFTL Model)";
1377 return (4 * doc->chips_per_floor);
1378}
1379
1380static inline int __init doc2001_init(struct mtd_info *mtd)
1381{
1382 struct nand_chip *this = mtd->priv;
1383 struct doc_priv *doc = this->priv;
1384
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 this->read_byte = doc2001_read_byte;
1386 this->write_buf = doc2001_writebuf;
1387 this->read_buf = doc2001_readbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388
1389 ReadDOC(doc->virtadr, ChipID);
1390 ReadDOC(doc->virtadr, ChipID);
1391 ReadDOC(doc->virtadr, ChipID);
1392 if (ReadDOC(doc->virtadr, ChipID) != DOC_ChipID_DocMil) {
1393 /* It's not a Millennium; it's one of the newer
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001394 DiskOnChip 2000 units with a similar ASIC.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 Treat it like a Millennium, except that it
1396 can have multiple chips. */
1397 doc2000_count_chips(mtd);
1398 mtd->name = "DiskOnChip 2000 (INFTL Model)";
1399 this->scan_bbt = inftl_scan_bbt;
1400 return (4 * doc->chips_per_floor);
1401 } else {
1402 /* Bog-standard Millennium */
1403 doc->chips_per_floor = 1;
1404 mtd->name = "DiskOnChip Millennium";
1405 this->scan_bbt = nftl_scan_bbt;
1406 return 1;
1407 }
1408}
1409
1410static inline int __init doc2001plus_init(struct mtd_info *mtd)
1411{
1412 struct nand_chip *this = mtd->priv;
1413 struct doc_priv *doc = this->priv;
1414
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 this->read_byte = doc2001plus_read_byte;
1416 this->write_buf = doc2001plus_writebuf;
1417 this->read_buf = doc2001plus_readbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 this->scan_bbt = inftl_scan_bbt;
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +02001419 this->cmd_ctrl = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 this->select_chip = doc2001plus_select_chip;
1421 this->cmdfunc = doc2001plus_command;
Thomas Gleixner0cddd6c2006-05-23 15:59:58 +02001422 this->ecc.hwctl = doc2001plus_enable_hwecc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423
1424 doc->chips_per_floor = 1;
1425 mtd->name = "DiskOnChip Millennium Plus";
1426
1427 return 1;
1428}
1429
Arjan van de Ven858119e2006-01-14 13:20:43 -08001430static int __init doc_probe(unsigned long physadr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431{
1432 unsigned char ChipID;
1433 struct mtd_info *mtd;
1434 struct nand_chip *nand;
1435 struct doc_priv *doc;
1436 void __iomem *virtadr;
1437 unsigned char save_control;
1438 unsigned char tmp, tmpb, tmpc;
1439 int reg, len, numchips;
1440 int ret = 0;
1441
Sasha Levin86e4bbc2014-03-19 18:24:37 -04001442 if (!request_mem_region(physadr, DOC_IOREMAP_LEN, "DiskOnChip"))
Alexander Shiyan4f0614a2013-11-13 15:59:25 +04001443 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 virtadr = ioremap(physadr, DOC_IOREMAP_LEN);
1445 if (!virtadr) {
1446 printk(KERN_ERR "Diskonchip ioremap failed: 0x%x bytes at 0x%lx\n", DOC_IOREMAP_LEN, physadr);
Alexander Shiyan4f0614a2013-11-13 15:59:25 +04001447 ret = -EIO;
1448 goto error_ioremap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 }
1450
1451 /* It's not possible to cleanly detect the DiskOnChip - the
1452 * bootup procedure will put the device into reset mode, and
1453 * it's not possible to talk to it without actually writing
1454 * to the DOCControl register. So we store the current contents
1455 * of the DOCControl register's location, in case we later decide
1456 * that it's not a DiskOnChip, and want to put it back how we
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001457 * found it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 */
1459 save_control = ReadDOC(virtadr, DOCControl);
1460
1461 /* Reset the DiskOnChip ASIC */
David Woodhousee0c7d762006-05-13 18:07:53 +01001462 WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_RESET, virtadr, DOCControl);
1463 WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_RESET, virtadr, DOCControl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464
1465 /* Enable the DiskOnChip ASIC */
David Woodhousee0c7d762006-05-13 18:07:53 +01001466 WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_NORMAL, virtadr, DOCControl);
1467 WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_NORMAL, virtadr, DOCControl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468
1469 ChipID = ReadDOC(virtadr, ChipID);
1470
David Woodhousee0c7d762006-05-13 18:07:53 +01001471 switch (ChipID) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 case DOC_ChipID_Doc2k:
1473 reg = DoC_2k_ECCStatus;
1474 break;
1475 case DOC_ChipID_DocMil:
1476 reg = DoC_ECCConf;
1477 break;
1478 case DOC_ChipID_DocMilPlus16:
1479 case DOC_ChipID_DocMilPlus32:
1480 case 0:
1481 /* Possible Millennium Plus, need to do more checks */
1482 /* Possibly release from power down mode */
1483 for (tmp = 0; (tmp < 4); tmp++)
1484 ReadDOC(virtadr, Mplus_Power);
1485
1486 /* Reset the Millennium Plus ASIC */
David Woodhousee0c7d762006-05-13 18:07:53 +01001487 tmp = DOC_MODE_RESET | DOC_MODE_MDWREN | DOC_MODE_RST_LAT | DOC_MODE_BDECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 WriteDOC(tmp, virtadr, Mplus_DOCControl);
1489 WriteDOC(~tmp, virtadr, Mplus_CtrlConfirm);
1490
1491 mdelay(1);
1492 /* Enable the Millennium Plus ASIC */
David Woodhousee0c7d762006-05-13 18:07:53 +01001493 tmp = DOC_MODE_NORMAL | DOC_MODE_MDWREN | DOC_MODE_RST_LAT | DOC_MODE_BDECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 WriteDOC(tmp, virtadr, Mplus_DOCControl);
1495 WriteDOC(~tmp, virtadr, Mplus_CtrlConfirm);
1496 mdelay(1);
1497
1498 ChipID = ReadDOC(virtadr, ChipID);
1499
1500 switch (ChipID) {
1501 case DOC_ChipID_DocMilPlus16:
1502 reg = DoC_Mplus_Toggle;
1503 break;
1504 case DOC_ChipID_DocMilPlus32:
1505 printk(KERN_ERR "DiskOnChip Millennium Plus 32MB is not supported, ignoring.\n");
1506 default:
1507 ret = -ENODEV;
1508 goto notfound;
1509 }
1510 break;
1511
1512 default:
1513 ret = -ENODEV;
1514 goto notfound;
1515 }
1516 /* Check the TOGGLE bit in the ECC register */
David Woodhousee0c7d762006-05-13 18:07:53 +01001517 tmp = ReadDOC_(virtadr, reg) & DOC_TOGGLE_BIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 tmpb = ReadDOC_(virtadr, reg) & DOC_TOGGLE_BIT;
1519 tmpc = ReadDOC_(virtadr, reg) & DOC_TOGGLE_BIT;
1520 if ((tmp == tmpb) || (tmp != tmpc)) {
1521 printk(KERN_WARNING "Possible DiskOnChip at 0x%lx failed TOGGLE test, dropping.\n", physadr);
1522 ret = -ENODEV;
1523 goto notfound;
1524 }
1525
1526 for (mtd = doclist; mtd; mtd = doc->nextdoc) {
1527 unsigned char oldval;
1528 unsigned char newval;
1529 nand = mtd->priv;
1530 doc = nand->priv;
1531 /* Use the alias resolution register to determine if this is
1532 in fact the same DOC aliased to a new address. If writes
1533 to one chip's alias resolution register change the value on
1534 the other chip, they're the same chip. */
1535 if (ChipID == DOC_ChipID_DocMilPlus16) {
1536 oldval = ReadDOC(doc->virtadr, Mplus_AliasResolution);
1537 newval = ReadDOC(virtadr, Mplus_AliasResolution);
1538 } else {
1539 oldval = ReadDOC(doc->virtadr, AliasResolution);
1540 newval = ReadDOC(virtadr, AliasResolution);
1541 }
1542 if (oldval != newval)
1543 continue;
1544 if (ChipID == DOC_ChipID_DocMilPlus16) {
1545 WriteDOC(~newval, virtadr, Mplus_AliasResolution);
1546 oldval = ReadDOC(doc->virtadr, Mplus_AliasResolution);
David Woodhousee0c7d762006-05-13 18:07:53 +01001547 WriteDOC(newval, virtadr, Mplus_AliasResolution); // restore it
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 } else {
1549 WriteDOC(~newval, virtadr, AliasResolution);
1550 oldval = ReadDOC(doc->virtadr, AliasResolution);
David Woodhousee0c7d762006-05-13 18:07:53 +01001551 WriteDOC(newval, virtadr, AliasResolution); // restore it
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 }
1553 newval = ~newval;
1554 if (oldval == newval) {
1555 printk(KERN_DEBUG "Found alias of DOC at 0x%lx to 0x%lx\n", doc->physadr, physadr);
1556 goto notfound;
1557 }
1558 }
1559
1560 printk(KERN_NOTICE "DiskOnChip found at 0x%lx\n", physadr);
1561
1562 len = sizeof(struct mtd_info) +
David Woodhousee0c7d762006-05-13 18:07:53 +01001563 sizeof(struct nand_chip) + sizeof(struct doc_priv) + (2 * sizeof(struct nand_bbt_descr));
Burman Yan95b93a02006-11-15 21:10:29 +02001564 mtd = kzalloc(len, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 if (!mtd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 ret = -ENOMEM;
1567 goto fail;
1568 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569
1570 nand = (struct nand_chip *) (mtd + 1);
1571 doc = (struct doc_priv *) (nand + 1);
1572 nand->bbt_td = (struct nand_bbt_descr *) (doc + 1);
1573 nand->bbt_md = nand->bbt_td + 1;
1574
1575 mtd->priv = nand;
1576 mtd->owner = THIS_MODULE;
1577
1578 nand->priv = doc;
1579 nand->select_chip = doc200x_select_chip;
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +02001580 nand->cmd_ctrl = doc200x_hwcontrol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 nand->dev_ready = doc200x_dev_ready;
1582 nand->waitfunc = doc200x_wait;
1583 nand->block_bad = doc200x_block_bad;
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02001584 nand->ecc.hwctl = doc200x_enable_hwecc;
1585 nand->ecc.calculate = doc200x_calculate_ecc;
1586 nand->ecc.correct = doc200x_correct_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02001588 nand->ecc.layout = &doc200x_oobinfo;
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02001589 nand->ecc.mode = NAND_ECC_HW_SYNDROME;
1590 nand->ecc.size = 512;
1591 nand->ecc.bytes = 6;
Mike Dunn6a918ba2012-03-11 14:21:11 -07001592 nand->ecc.strength = 2;
Brian Norrisbb9ebd42011-05-31 16:31:23 -07001593 nand->bbt_options = NAND_BBT_USE_FLASH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594
1595 doc->physadr = physadr;
1596 doc->virtadr = virtadr;
1597 doc->ChipID = ChipID;
1598 doc->curfloor = -1;
1599 doc->curchip = -1;
1600 doc->mh0_page = -1;
1601 doc->mh1_page = -1;
1602 doc->nextdoc = doclist;
1603
1604 if (ChipID == DOC_ChipID_Doc2k)
1605 numchips = doc2000_init(mtd);
1606 else if (ChipID == DOC_ChipID_DocMilPlus16)
1607 numchips = doc2001plus_init(mtd);
1608 else
1609 numchips = doc2001_init(mtd);
1610
1611 if ((ret = nand_scan(mtd, numchips))) {
1612 /* DBB note: i believe nand_release is necessary here, as
1613 buffers may have been allocated in nand_base. Check with
1614 Thomas. FIX ME! */
Jamie Iles0f47e952011-05-23 10:23:19 +01001615 /* nand_release will call mtd_device_unregister, but we
1616 haven't yet added it. This is handled without incident by
1617 mtd_device_unregister, as far as I can tell. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 nand_release(mtd);
1619 kfree(mtd);
1620 goto fail;
1621 }
1622
1623 /* Success! */
1624 doclist = mtd;
1625 return 0;
1626
David Woodhousee0c7d762006-05-13 18:07:53 +01001627 notfound:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 /* Put back the contents of the DOCControl register, in case it's not
1629 actually a DiskOnChip. */
1630 WriteDOC(save_control, virtadr, DOCControl);
David Woodhousee0c7d762006-05-13 18:07:53 +01001631 fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 iounmap(virtadr);
Alexander Shiyan4f0614a2013-11-13 15:59:25 +04001633
1634error_ioremap:
1635 release_mem_region(physadr, DOC_IOREMAP_LEN);
1636
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 return ret;
1638}
1639
1640static void release_nanddoc(void)
1641{
David Woodhousee0c7d762006-05-13 18:07:53 +01001642 struct mtd_info *mtd, *nextmtd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 struct nand_chip *nand;
1644 struct doc_priv *doc;
1645
1646 for (mtd = doclist; mtd; mtd = nextmtd) {
1647 nand = mtd->priv;
1648 doc = nand->priv;
1649
1650 nextmtd = doc->nextdoc;
1651 nand_release(mtd);
1652 iounmap(doc->virtadr);
Alexander Shiyan4f0614a2013-11-13 15:59:25 +04001653 release_mem_region(doc->physadr, DOC_IOREMAP_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 kfree(mtd);
1655 }
1656}
1657
1658static int __init init_nanddoc(void)
1659{
1660 int i, ret = 0;
1661
1662 /* We could create the decoder on demand, if memory is a concern.
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001663 * This way we have it handy, if an error happens
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 *
1665 * Symbolsize is 10 (bits)
1666 * Primitve polynomial is x^10+x^3+1
1667 * first consecutive root is 510
1668 * primitve element to generate roots = 1
1669 * generator polinomial degree = 4
1670 */
1671 rs_decoder = init_rs(10, 0x409, FCR, 1, NROOTS);
David Woodhousee0c7d762006-05-13 18:07:53 +01001672 if (!rs_decoder) {
1673 printk(KERN_ERR "DiskOnChip: Could not create a RS decoder\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 return -ENOMEM;
1675 }
1676
1677 if (doc_config_location) {
1678 printk(KERN_INFO "Using configured DiskOnChip probe address 0x%lx\n", doc_config_location);
1679 ret = doc_probe(doc_config_location);
1680 if (ret < 0)
1681 goto outerr;
1682 } else {
David Woodhousee0c7d762006-05-13 18:07:53 +01001683 for (i = 0; (doc_locations[i] != 0xffffffff); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 doc_probe(doc_locations[i]);
1685 }
1686 }
1687 /* No banner message any more. Print a message if no DiskOnChip
1688 found, so the user knows we at least tried. */
1689 if (!doclist) {
1690 printk(KERN_INFO "No valid DiskOnChip devices found\n");
1691 ret = -ENODEV;
1692 goto outerr;
1693 }
1694 return 0;
David Woodhousee0c7d762006-05-13 18:07:53 +01001695 outerr:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 free_rs(rs_decoder);
1697 return ret;
1698}
1699
1700static void __exit cleanup_nanddoc(void)
1701{
1702 /* Cleanup the nand/DoC resources */
1703 release_nanddoc();
1704
1705 /* Free the reed solomon resources */
1706 if (rs_decoder) {
1707 free_rs(rs_decoder);
1708 }
1709}
1710
1711module_init(init_nanddoc);
1712module_exit(cleanup_nanddoc);
1713
1714MODULE_LICENSE("GPL");
1715MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
Niels de Vos2a7af8c2009-01-30 11:08:35 +01001716MODULE_DESCRIPTION("M-Systems DiskOnChip 2000, Millennium and Millennium Plus device driver");