blob: 43d1e08133ceb221f11208baead1f4d61bc005a8 [file] [log] [blame]
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * (C) 2003 Red Hat, Inc.
3 * (C) 2004 Dan Brown <dan_brown@ieee.org>
4 * (C) 2004 Kalev Lember <kalev@smartlink.ee>
5 *
6 * Author: David Woodhouse <dwmw2@infradead.org>
7 * Additional Diskonchip 2000 and Millennium support by Dan Brown <dan_brown@ieee.org>
8 * Diskonchip Millennium Plus support by Kalev Lember <kalev@smartlink.ee>
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00009 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 * Error correction code lifted from the old docecc code
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000011 * Author: Fabrice Bellard (fabrice.bellard@netgem.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 * Copyright (C) 2000 Netgem S.A.
13 * converted to the generic Reed-Solomon library by Thomas Gleixner <tglx@linutronix.de>
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000014 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 * Interface to generic NAND code for M-Systems DiskOnChip devices
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 */
17
18#include <linux/kernel.h>
19#include <linux/init.h>
20#include <linux/sched.h>
21#include <linux/delay.h>
22#include <linux/rslib.h>
23#include <linux/moduleparam.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/slab.h>
Dan Williams2584cf82015-08-10 23:07:05 -040025#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
27#include <linux/mtd/mtd.h>
Boris Brezillond4092d72017-08-04 17:29:10 +020028#include <linux/mtd/rawnand.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/mtd/doc2000.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/mtd/partitions.h>
31#include <linux/mtd/inftl.h>
Paul Gortmakera0e5cc52011-07-03 15:17:31 -040032#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
34/* Where to look for the devices? */
Thomas Gleixner651078b2005-01-31 20:36:46 +000035#ifndef CONFIG_MTD_NAND_DISKONCHIP_PROBE_ADDRESS
36#define CONFIG_MTD_NAND_DISKONCHIP_PROBE_ADDRESS 0
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#endif
38
Sachin Kamat14a95b8a2013-08-12 15:10:47 +053039static unsigned long doc_locations[] __initdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#if defined (__alpha__) || defined(__i386__) || defined(__x86_64__)
Thomas Gleixner651078b2005-01-31 20:36:46 +000041#ifdef CONFIG_MTD_NAND_DISKONCHIP_PROBE_HIGH
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000042 0xfffc8000, 0xfffca000, 0xfffcc000, 0xfffce000,
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 0xfffd0000, 0xfffd2000, 0xfffd4000, 0xfffd6000,
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000044 0xfffd8000, 0xfffda000, 0xfffdc000, 0xfffde000,
45 0xfffe0000, 0xfffe2000, 0xfffe4000, 0xfffe6000,
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 0xfffe8000, 0xfffea000, 0xfffec000, 0xfffee000,
Michael Opdenacker9d403492013-07-08 06:39:20 +020047#else
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000048 0xc8000, 0xca000, 0xcc000, 0xce000,
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 0xd0000, 0xd2000, 0xd4000, 0xd6000,
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000050 0xd8000, 0xda000, 0xdc000, 0xde000,
51 0xe0000, 0xe2000, 0xe4000, 0xe6000,
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 0xe8000, 0xea000, 0xec000, 0xee000,
Michael Opdenacker9d403492013-07-08 06:39:20 +020053#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#endif
55 0xffffffff };
56
57static struct mtd_info *doclist = NULL;
58
59struct doc_priv {
60 void __iomem *virtadr;
61 unsigned long physadr;
62 u_char ChipID;
63 u_char CDSNControl;
David Woodhousee0c7d762006-05-13 18:07:53 +010064 int chips_per_floor; /* The number of chips detected on each floor */
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 int curfloor;
66 int curchip;
67 int mh0_page;
68 int mh1_page;
Thomas Gleixner964dfce2018-04-22 18:23:54 +020069 struct rs_control *rs_decoder;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 struct mtd_info *nextdoc;
Brian Norrisd24fe0c2015-02-28 02:13:10 -080071
72 /* Handle the last stage of initialization (BBT scan, partitioning) */
73 int (*late_init)(struct mtd_info *mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074};
75
Linus Torvalds1da177e2005-04-16 15:20:36 -070076/* This is the ecc value computed by the HW ecc generator upon writing an empty
77 page, one with all 0xff for data. */
78static u_char empty_write_ecc[6] = { 0x4b, 0x00, 0xe2, 0x0e, 0x93, 0xf7 };
79
80#define INFTL_BBT_RESERVED_BLOCKS 4
81
82#define DoC_is_MillenniumPlus(doc) ((doc)->ChipID == DOC_ChipID_DocMilPlus16 || (doc)->ChipID == DOC_ChipID_DocMilPlus32)
83#define DoC_is_Millennium(doc) ((doc)->ChipID == DOC_ChipID_DocMil)
84#define DoC_is_2000(doc) ((doc)->ChipID == DOC_ChipID_Doc2k)
85
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +020086static void doc200x_hwcontrol(struct mtd_info *mtd, int cmd,
87 unsigned int bitmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088static void doc200x_select_chip(struct mtd_info *mtd, int chip);
89
David Woodhousee0c7d762006-05-13 18:07:53 +010090static int debug = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091module_param(debug, int, 0);
92
David Woodhousee0c7d762006-05-13 18:07:53 +010093static int try_dword = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094module_param(try_dword, int, 0);
95
David Woodhousee0c7d762006-05-13 18:07:53 +010096static int no_ecc_failures = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097module_param(no_ecc_failures, int, 0);
98
David Woodhousee0c7d762006-05-13 18:07:53 +010099static int no_autopart = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100module_param(no_autopart, int, 0);
Dan Brown1a78ff62005-03-29 21:57:48 +0100101
David Woodhousee0c7d762006-05-13 18:07:53 +0100102static int show_firmware_partition = 0;
Dan Brown1a78ff62005-03-29 21:57:48 +0100103module_param(show_firmware_partition, int, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
Robert P. J. Day89e2bf62007-03-07 18:33:25 -0500105#ifdef CONFIG_MTD_NAND_DISKONCHIP_BBTWRITE
David Woodhousee0c7d762006-05-13 18:07:53 +0100106static int inftl_bbt_write = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107#else
David Woodhousee0c7d762006-05-13 18:07:53 +0100108static int inftl_bbt_write = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109#endif
110module_param(inftl_bbt_write, int, 0);
111
Thomas Gleixner651078b2005-01-31 20:36:46 +0000112static unsigned long doc_config_location = CONFIG_MTD_NAND_DISKONCHIP_PROBE_ADDRESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113module_param(doc_config_location, ulong, 0);
114MODULE_PARM_DESC(doc_config_location, "Physical memory address at which to probe for DiskOnChip");
115
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116/* Sector size for HW ECC */
117#define SECTOR_SIZE 512
118/* The sector bytes are packed into NB_DATA 10 bit words */
119#define NB_DATA (((SECTOR_SIZE + 1) * 8 + 6) / 10)
120/* Number of roots */
121#define NROOTS 4
122/* First consective root */
123#define FCR 510
124/* Number of symbols */
125#define NN 1023
126
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000127/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 * The HW decoder in the DoC ASIC's provides us a error syndrome,
Brian Norris7854d3f2011-06-23 14:12:08 -0700129 * which we must convert to a standard syndrome usable by the generic
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 * Reed-Solomon library code.
131 *
132 * Fabrice Bellard figured this out in the old docecc code. I added
133 * some comments, improved a minor bit and converted it to make use
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300134 * of the generic Reed-Solomon library. tglx
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100136static int doc_ecc_decode(struct rs_control *rs, uint8_t *data, uint8_t *ecc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137{
138 int i, j, nerr, errpos[8];
139 uint8_t parity;
140 uint16_t ds[4], s[5], tmp, errval[8], syn[4];
Thomas Gleixner21633982018-04-22 18:23:53 +0200141 struct rs_codec *cd = rs->codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Mark Warec9fb6772010-05-27 11:45:39 +1000143 memset(syn, 0, sizeof(syn));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 /* Convert the ecc bytes into words */
145 ds[0] = ((ecc[4] & 0xff) >> 0) | ((ecc[5] & 0x03) << 8);
146 ds[1] = ((ecc[5] & 0xfc) >> 2) | ((ecc[2] & 0x0f) << 6);
147 ds[2] = ((ecc[2] & 0xf0) >> 4) | ((ecc[3] & 0x3f) << 4);
148 ds[3] = ((ecc[3] & 0xc0) >> 6) | ((ecc[0] & 0xff) << 2);
149 parity = ecc[1];
150
Brian Norris7854d3f2011-06-23 14:12:08 -0700151 /* Initialize the syndrome buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 for (i = 0; i < NROOTS; i++)
153 s[i] = ds[0];
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000154 /*
155 * Evaluate
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 * s[i] = ds[3]x^3 + ds[2]x^2 + ds[1]x^1 + ds[0]
157 * where x = alpha^(FCR + i)
158 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100159 for (j = 1; j < NROOTS; j++) {
160 if (ds[j] == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 continue;
Thomas Gleixner21633982018-04-22 18:23:53 +0200162 tmp = cd->index_of[ds[j]];
David Woodhousee0c7d762006-05-13 18:07:53 +0100163 for (i = 0; i < NROOTS; i++)
Thomas Gleixner21633982018-04-22 18:23:53 +0200164 s[i] ^= cd->alpha_to[rs_modnn(cd, tmp + (FCR + i) * j)];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 }
166
Mark Warec9fb6772010-05-27 11:45:39 +1000167 /* Calc syn[i] = s[i] / alpha^(v + i) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 for (i = 0; i < NROOTS; i++) {
Mark Warec9fb6772010-05-27 11:45:39 +1000169 if (s[i])
Thomas Gleixner21633982018-04-22 18:23:53 +0200170 syn[i] = rs_modnn(cd, cd->index_of[s[i]] + (NN - FCR - i));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 }
172 /* Call the decoder library */
173 nerr = decode_rs16(rs, NULL, NULL, 1019, syn, 0, errpos, 0, errval);
174
175 /* Incorrectable errors ? */
176 if (nerr < 0)
177 return nerr;
178
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000179 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 * Correct the errors. The bitpositions are a bit of magic,
181 * but they are given by the design of the de/encoder circuit
182 * in the DoC ASIC's.
183 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100184 for (i = 0; i < nerr; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 int index, bitpos, pos = 1015 - errpos[i];
186 uint8_t val;
187 if (pos >= NB_DATA && pos < 1019)
188 continue;
189 if (pos < NB_DATA) {
190 /* extract bit position (MSB first) */
191 pos = 10 * (NB_DATA - 1 - pos) - 6;
192 /* now correct the following 10 bits. At most two bytes
193 can be modified since pos is even */
194 index = (pos >> 3) ^ 1;
195 bitpos = pos & 7;
David Woodhousee0c7d762006-05-13 18:07:53 +0100196 if ((index >= 0 && index < SECTOR_SIZE) || index == (SECTOR_SIZE + 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 val = (uint8_t) (errval[i] >> (2 + bitpos));
198 parity ^= val;
199 if (index < SECTOR_SIZE)
200 data[index] ^= val;
201 }
202 index = ((pos >> 3) + 1) ^ 1;
203 bitpos = (bitpos + 10) & 7;
204 if (bitpos == 0)
205 bitpos = 8;
David Woodhousee0c7d762006-05-13 18:07:53 +0100206 if ((index >= 0 && index < SECTOR_SIZE) || index == (SECTOR_SIZE + 1)) {
207 val = (uint8_t) (errval[i] << (8 - bitpos));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 parity ^= val;
209 if (index < SECTOR_SIZE)
210 data[index] ^= val;
211 }
212 }
213 }
214 /* If the parity is wrong, no rescue possible */
Jörn Engeleb684502007-10-20 23:16:32 +0200215 return parity ? -EBADMSG : nerr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216}
217
218static void DoC_Delay(struct doc_priv *doc, unsigned short cycles)
219{
220 volatile char dummy;
221 int i;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000222
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 for (i = 0; i < cycles; i++) {
224 if (DoC_is_Millennium(doc))
225 dummy = ReadDOC(doc->virtadr, NOP);
226 else if (DoC_is_MillenniumPlus(doc))
227 dummy = ReadDOC(doc->virtadr, Mplus_NOP);
228 else
229 dummy = ReadDOC(doc->virtadr, DOCStatus);
230 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000231
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232}
233
234#define CDSN_CTRL_FR_B_MASK (CDSN_CTRL_FR_B0 | CDSN_CTRL_FR_B1)
235
236/* DOC_WaitReady: Wait for RDY line to be asserted by the flash chip */
237static int _DoC_WaitReady(struct doc_priv *doc)
238{
David Woodhousee0c7d762006-05-13 18:07:53 +0100239 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 unsigned long timeo = jiffies + (HZ * 10);
241
David Woodhousee0c7d762006-05-13 18:07:53 +0100242 if (debug)
243 printk("_DoC_WaitReady...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 /* Out-of-line routine to wait for chip response */
245 if (DoC_is_MillenniumPlus(doc)) {
246 while ((ReadDOC(docptr, Mplus_FlashControl) & CDSN_CTRL_FR_B_MASK) != CDSN_CTRL_FR_B_MASK) {
247 if (time_after(jiffies, timeo)) {
248 printk("_DoC_WaitReady timed out.\n");
249 return -EIO;
250 }
251 udelay(1);
252 cond_resched();
253 }
254 } else {
255 while (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B)) {
256 if (time_after(jiffies, timeo)) {
257 printk("_DoC_WaitReady timed out.\n");
258 return -EIO;
259 }
260 udelay(1);
261 cond_resched();
262 }
263 }
264
265 return 0;
266}
267
268static inline int DoC_WaitReady(struct doc_priv *doc)
269{
David Woodhousee0c7d762006-05-13 18:07:53 +0100270 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 int ret = 0;
272
273 if (DoC_is_MillenniumPlus(doc)) {
274 DoC_Delay(doc, 4);
275
276 if ((ReadDOC(docptr, Mplus_FlashControl) & CDSN_CTRL_FR_B_MASK) != CDSN_CTRL_FR_B_MASK)
277 /* Call the out-of-line routine to wait */
278 ret = _DoC_WaitReady(doc);
279 } else {
280 DoC_Delay(doc, 4);
281
282 if (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B))
283 /* Call the out-of-line routine to wait */
284 ret = _DoC_WaitReady(doc);
285 DoC_Delay(doc, 2);
286 }
287
David Woodhousee0c7d762006-05-13 18:07:53 +0100288 if (debug)
289 printk("DoC_WaitReady OK\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 return ret;
291}
292
293static void doc2000_write_byte(struct mtd_info *mtd, u_char datum)
294{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100295 struct nand_chip *this = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100296 struct doc_priv *doc = nand_get_controller_data(this);
David Woodhousee0c7d762006-05-13 18:07:53 +0100297 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
David Woodhousee0c7d762006-05-13 18:07:53 +0100299 if (debug)
300 printk("write_byte %02x\n", datum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 WriteDOC(datum, docptr, CDSNSlowIO);
302 WriteDOC(datum, docptr, 2k_CDSN_IO);
303}
304
305static u_char doc2000_read_byte(struct mtd_info *mtd)
306{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100307 struct nand_chip *this = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100308 struct doc_priv *doc = nand_get_controller_data(this);
David Woodhousee0c7d762006-05-13 18:07:53 +0100309 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 u_char ret;
311
312 ReadDOC(docptr, CDSNSlowIO);
313 DoC_Delay(doc, 2);
314 ret = ReadDOC(docptr, 2k_CDSN_IO);
David Woodhousee0c7d762006-05-13 18:07:53 +0100315 if (debug)
316 printk("read_byte returns %02x\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 return ret;
318}
319
David Woodhousee0c7d762006-05-13 18:07:53 +0100320static void doc2000_writebuf(struct mtd_info *mtd, const u_char *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100322 struct nand_chip *this = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100323 struct doc_priv *doc = nand_get_controller_data(this);
David Woodhousee0c7d762006-05-13 18:07:53 +0100324 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 int i;
David Woodhousee0c7d762006-05-13 18:07:53 +0100326 if (debug)
327 printk("writebuf of %d bytes: ", len);
328 for (i = 0; i < len; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 WriteDOC_(buf[i], docptr, DoC_2k_CDSN_IO + i);
330 if (debug && i < 16)
331 printk("%02x ", buf[i]);
332 }
David Woodhousee0c7d762006-05-13 18:07:53 +0100333 if (debug)
334 printk("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335}
336
David Woodhousee0c7d762006-05-13 18:07:53 +0100337static void doc2000_readbuf(struct mtd_info *mtd, u_char *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100339 struct nand_chip *this = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100340 struct doc_priv *doc = nand_get_controller_data(this);
David Woodhousee0c7d762006-05-13 18:07:53 +0100341 void __iomem *docptr = doc->virtadr;
342 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
David Woodhousee0c7d762006-05-13 18:07:53 +0100344 if (debug)
345 printk("readbuf of %d bytes: ", len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
David Woodhousee0c7d762006-05-13 18:07:53 +0100347 for (i = 0; i < len; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 buf[i] = ReadDOC(docptr, 2k_CDSN_IO + i);
349 }
350}
351
David Woodhousee0c7d762006-05-13 18:07:53 +0100352static void doc2000_readbuf_dword(struct mtd_info *mtd, u_char *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100354 struct nand_chip *this = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100355 struct doc_priv *doc = nand_get_controller_data(this);
David Woodhousee0c7d762006-05-13 18:07:53 +0100356 void __iomem *docptr = doc->virtadr;
357 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
David Woodhousee0c7d762006-05-13 18:07:53 +0100359 if (debug)
360 printk("readbuf_dword of %d bytes: ", len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
David Woodhousee0c7d762006-05-13 18:07:53 +0100362 if (unlikely((((unsigned long)buf) | len) & 3)) {
363 for (i = 0; i < len; i++) {
364 *(uint8_t *) (&buf[i]) = ReadDOC(docptr, 2k_CDSN_IO + i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 }
366 } else {
David Woodhousee0c7d762006-05-13 18:07:53 +0100367 for (i = 0; i < len; i += 4) {
368 *(uint32_t *) (&buf[i]) = readl(docptr + DoC_2k_CDSN_IO + i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 }
370 }
371}
372
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373static uint16_t __init doc200x_ident_chip(struct mtd_info *mtd, int nr)
374{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100375 struct nand_chip *this = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100376 struct doc_priv *doc = nand_get_controller_data(this);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 uint16_t ret;
378
379 doc200x_select_chip(mtd, nr);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200380 doc200x_hwcontrol(mtd, NAND_CMD_READID,
381 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
382 doc200x_hwcontrol(mtd, 0, NAND_CTRL_ALE | NAND_CTRL_CHANGE);
383 doc200x_hwcontrol(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000384
Lucas De Marchie9c54992011-04-26 23:28:26 -0700385 /* We can't use dev_ready here, but at least we wait for the
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000386 * command to complete
Thomas Gleixnerdfd61292005-02-22 21:48:25 +0000387 */
388 udelay(50);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000389
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 ret = this->read_byte(mtd) << 8;
391 ret |= this->read_byte(mtd);
392
393 if (doc->ChipID == DOC_ChipID_Doc2k && try_dword && !nr) {
394 /* First chip probe. See if we get same results by 32-bit access */
395 union {
396 uint32_t dword;
397 uint8_t byte[4];
398 } ident;
399 void __iomem *docptr = doc->virtadr;
400
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200401 doc200x_hwcontrol(mtd, NAND_CMD_READID,
402 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
403 doc200x_hwcontrol(mtd, 0, NAND_CTRL_ALE | NAND_CTRL_CHANGE);
404 doc200x_hwcontrol(mtd, NAND_CMD_NONE,
405 NAND_NCE | NAND_CTRL_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
Thomas Gleixnerdfd61292005-02-22 21:48:25 +0000407 udelay(50);
408
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 ident.dword = readl(docptr + DoC_2k_CDSN_IO);
410 if (((ident.byte[0] << 8) | ident.byte[1]) == ret) {
Shreeya Patel63fa37f2018-02-22 22:01:22 +0530411 pr_info("DiskOnChip 2000 responds to DWORD access\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 this->read_buf = &doc2000_readbuf_dword;
413 }
414 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000415
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 return ret;
417}
418
419static void __init doc2000_count_chips(struct mtd_info *mtd)
420{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100421 struct nand_chip *this = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100422 struct doc_priv *doc = nand_get_controller_data(this);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 uint16_t mfrid;
424 int i;
425
426 /* Max 4 chips per floor on DiskOnChip 2000 */
427 doc->chips_per_floor = 4;
428
429 /* Find out what the first chip is */
430 mfrid = doc200x_ident_chip(mtd, 0);
431
432 /* Find how many chips in each floor. */
433 for (i = 1; i < 4; i++) {
434 if (doc200x_ident_chip(mtd, i) != mfrid)
435 break;
436 }
437 doc->chips_per_floor = i;
Shreeya Patel63fa37f2018-02-22 22:01:22 +0530438 pr_debug("Detected %d chips per floor.\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439}
440
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200441static int doc200x_wait(struct mtd_info *mtd, struct nand_chip *this)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442{
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100443 struct doc_priv *doc = nand_get_controller_data(this);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
445 int status;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000446
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 DoC_WaitReady(doc);
Boris Brezillon97d90da2017-11-30 18:01:29 +0100448 nand_status_op(this, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 DoC_WaitReady(doc);
450 status = (int)this->read_byte(mtd);
451
452 return status;
453}
454
455static void doc2001_write_byte(struct mtd_info *mtd, u_char datum)
456{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100457 struct nand_chip *this = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100458 struct doc_priv *doc = nand_get_controller_data(this);
David Woodhousee0c7d762006-05-13 18:07:53 +0100459 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
461 WriteDOC(datum, docptr, CDSNSlowIO);
462 WriteDOC(datum, docptr, Mil_CDSN_IO);
463 WriteDOC(datum, docptr, WritePipeTerm);
464}
465
466static u_char doc2001_read_byte(struct mtd_info *mtd)
467{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100468 struct nand_chip *this = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100469 struct doc_priv *doc = nand_get_controller_data(this);
David Woodhousee0c7d762006-05-13 18:07:53 +0100470 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
472 //ReadDOC(docptr, CDSNSlowIO);
473 /* 11.4.5 -- delay twice to allow extended length cycle */
474 DoC_Delay(doc, 2);
475 ReadDOC(docptr, ReadPipeInit);
476 //return ReadDOC(docptr, Mil_CDSN_IO);
477 return ReadDOC(docptr, LastDataRead);
478}
479
David Woodhousee0c7d762006-05-13 18:07:53 +0100480static void doc2001_writebuf(struct mtd_info *mtd, const u_char *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100482 struct nand_chip *this = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100483 struct doc_priv *doc = nand_get_controller_data(this);
David Woodhousee0c7d762006-05-13 18:07:53 +0100484 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 int i;
486
David Woodhousee0c7d762006-05-13 18:07:53 +0100487 for (i = 0; i < len; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 WriteDOC_(buf[i], docptr, DoC_Mil_CDSN_IO + i);
489 /* Terminate write pipeline */
490 WriteDOC(0x00, docptr, WritePipeTerm);
491}
492
David Woodhousee0c7d762006-05-13 18:07:53 +0100493static void doc2001_readbuf(struct mtd_info *mtd, u_char *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100495 struct nand_chip *this = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100496 struct doc_priv *doc = nand_get_controller_data(this);
David Woodhousee0c7d762006-05-13 18:07:53 +0100497 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 int i;
499
500 /* Start read pipeline */
501 ReadDOC(docptr, ReadPipeInit);
502
David Woodhousee0c7d762006-05-13 18:07:53 +0100503 for (i = 0; i < len - 1; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 buf[i] = ReadDOC(docptr, Mil_CDSN_IO + (i & 0xff));
505
506 /* Terminate read pipeline */
507 buf[i] = ReadDOC(docptr, LastDataRead);
508}
509
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510static u_char doc2001plus_read_byte(struct mtd_info *mtd)
511{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100512 struct nand_chip *this = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100513 struct doc_priv *doc = nand_get_controller_data(this);
David Woodhousee0c7d762006-05-13 18:07:53 +0100514 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 u_char ret;
516
David Woodhousee0c7d762006-05-13 18:07:53 +0100517 ReadDOC(docptr, Mplus_ReadPipeInit);
518 ReadDOC(docptr, Mplus_ReadPipeInit);
519 ret = ReadDOC(docptr, Mplus_LastDataRead);
520 if (debug)
521 printk("read_byte returns %02x\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 return ret;
523}
524
David Woodhousee0c7d762006-05-13 18:07:53 +0100525static void doc2001plus_writebuf(struct mtd_info *mtd, const u_char *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100527 struct nand_chip *this = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100528 struct doc_priv *doc = nand_get_controller_data(this);
David Woodhousee0c7d762006-05-13 18:07:53 +0100529 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 int i;
531
David Woodhousee0c7d762006-05-13 18:07:53 +0100532 if (debug)
533 printk("writebuf of %d bytes: ", len);
534 for (i = 0; i < len; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 WriteDOC_(buf[i], docptr, DoC_Mil_CDSN_IO + i);
536 if (debug && i < 16)
537 printk("%02x ", buf[i]);
538 }
David Woodhousee0c7d762006-05-13 18:07:53 +0100539 if (debug)
540 printk("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541}
542
David Woodhousee0c7d762006-05-13 18:07:53 +0100543static void doc2001plus_readbuf(struct mtd_info *mtd, u_char *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100545 struct nand_chip *this = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100546 struct doc_priv *doc = nand_get_controller_data(this);
David Woodhousee0c7d762006-05-13 18:07:53 +0100547 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 int i;
549
David Woodhousee0c7d762006-05-13 18:07:53 +0100550 if (debug)
551 printk("readbuf of %d bytes: ", len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
553 /* Start read pipeline */
554 ReadDOC(docptr, Mplus_ReadPipeInit);
555 ReadDOC(docptr, Mplus_ReadPipeInit);
556
David Woodhousee0c7d762006-05-13 18:07:53 +0100557 for (i = 0; i < len - 2; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 buf[i] = ReadDOC(docptr, Mil_CDSN_IO);
559 if (debug && i < 16)
560 printk("%02x ", buf[i]);
561 }
562
563 /* Terminate read pipeline */
David Woodhousee0c7d762006-05-13 18:07:53 +0100564 buf[len - 2] = ReadDOC(docptr, Mplus_LastDataRead);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 if (debug && i < 16)
David Woodhousee0c7d762006-05-13 18:07:53 +0100566 printk("%02x ", buf[len - 2]);
567 buf[len - 1] = ReadDOC(docptr, Mplus_LastDataRead);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 if (debug && i < 16)
David Woodhousee0c7d762006-05-13 18:07:53 +0100569 printk("%02x ", buf[len - 1]);
570 if (debug)
571 printk("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572}
573
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574static void doc2001plus_select_chip(struct mtd_info *mtd, int chip)
575{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100576 struct nand_chip *this = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100577 struct doc_priv *doc = nand_get_controller_data(this);
David Woodhousee0c7d762006-05-13 18:07:53 +0100578 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 int floor = 0;
580
David Woodhousee0c7d762006-05-13 18:07:53 +0100581 if (debug)
582 printk("select chip (%d)\n", chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
584 if (chip == -1) {
585 /* Disable flash internally */
586 WriteDOC(0, docptr, Mplus_FlashSelect);
587 return;
588 }
589
590 floor = chip / doc->chips_per_floor;
David Woodhousee0c7d762006-05-13 18:07:53 +0100591 chip -= (floor * doc->chips_per_floor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
593 /* Assert ChipEnable and deassert WriteProtect */
594 WriteDOC((DOC_FLASH_CE), docptr, Mplus_FlashSelect);
Boris Brezillon97d90da2017-11-30 18:01:29 +0100595 nand_reset_op(this);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
597 doc->curchip = chip;
598 doc->curfloor = floor;
599}
600
601static void doc200x_select_chip(struct mtd_info *mtd, int chip)
602{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100603 struct nand_chip *this = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100604 struct doc_priv *doc = nand_get_controller_data(this);
David Woodhousee0c7d762006-05-13 18:07:53 +0100605 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 int floor = 0;
607
David Woodhousee0c7d762006-05-13 18:07:53 +0100608 if (debug)
609 printk("select chip (%d)\n", chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
611 if (chip == -1)
612 return;
613
614 floor = chip / doc->chips_per_floor;
David Woodhousee0c7d762006-05-13 18:07:53 +0100615 chip -= (floor * doc->chips_per_floor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
617 /* 11.4.4 -- deassert CE before changing chip */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200618 doc200x_hwcontrol(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
620 WriteDOC(floor, docptr, FloorSelect);
621 WriteDOC(chip, docptr, CDSNDeviceSelect);
622
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200623 doc200x_hwcontrol(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
625 doc->curchip = chip;
626 doc->curfloor = floor;
627}
628
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200629#define CDSN_CTRL_MSK (CDSN_CTRL_CE | CDSN_CTRL_CLE | CDSN_CTRL_ALE)
630
631static void doc200x_hwcontrol(struct mtd_info *mtd, int cmd,
632 unsigned int ctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100634 struct nand_chip *this = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100635 struct doc_priv *doc = nand_get_controller_data(this);
David Woodhousee0c7d762006-05-13 18:07:53 +0100636 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200638 if (ctrl & NAND_CTRL_CHANGE) {
639 doc->CDSNControl &= ~CDSN_CTRL_MSK;
640 doc->CDSNControl |= ctrl & CDSN_CTRL_MSK;
641 if (debug)
642 printk("hwcontrol(%d): %02x\n", cmd, doc->CDSNControl);
643 WriteDOC(doc->CDSNControl, docptr, CDSNControl);
644 /* 11.4.3 -- 4 NOPs after CSDNControl write */
645 DoC_Delay(doc, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 }
Thomas Gleixnercad74f22006-05-23 23:28:48 +0200647 if (cmd != NAND_CMD_NONE) {
648 if (DoC_is_2000(doc))
649 doc2000_write_byte(mtd, cmd);
650 else
651 doc2001_write_byte(mtd, cmd);
652 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653}
654
David Woodhousee0c7d762006-05-13 18:07:53 +0100655static void doc2001plus_command(struct mtd_info *mtd, unsigned command, int column, int page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100657 struct nand_chip *this = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100658 struct doc_priv *doc = nand_get_controller_data(this);
David Woodhousee0c7d762006-05-13 18:07:53 +0100659 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
661 /*
662 * Must terminate write pipeline before sending any commands
663 * to the device.
664 */
665 if (command == NAND_CMD_PAGEPROG) {
666 WriteDOC(0x00, docptr, Mplus_WritePipeTerm);
667 WriteDOC(0x00, docptr, Mplus_WritePipeTerm);
668 }
669
670 /*
671 * Write out the command to the device.
672 */
673 if (command == NAND_CMD_SEQIN) {
674 int readcmd;
675
Joern Engel28318772006-05-22 23:18:05 +0200676 if (column >= mtd->writesize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 /* OOB area */
Joern Engel28318772006-05-22 23:18:05 +0200678 column -= mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 readcmd = NAND_CMD_READOOB;
680 } else if (column < 256) {
681 /* First 256 bytes --> READ0 */
682 readcmd = NAND_CMD_READ0;
683 } else {
684 column -= 256;
685 readcmd = NAND_CMD_READ1;
686 }
687 WriteDOC(readcmd, docptr, Mplus_FlashCmd);
688 }
689 WriteDOC(command, docptr, Mplus_FlashCmd);
690 WriteDOC(0, docptr, Mplus_WritePipeTerm);
691 WriteDOC(0, docptr, Mplus_WritePipeTerm);
692
693 if (column != -1 || page_addr != -1) {
694 /* Serially input address */
695 if (column != -1) {
696 /* Adjust columns for 16 bit buswidth */
Brian Norris3dad2342014-01-29 14:08:12 -0800697 if (this->options & NAND_BUSWIDTH_16 &&
698 !nand_opcode_8bits(command))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 column >>= 1;
700 WriteDOC(column, docptr, Mplus_FlashAddress);
701 }
702 if (page_addr != -1) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100703 WriteDOC((unsigned char)(page_addr & 0xff), docptr, Mplus_FlashAddress);
704 WriteDOC((unsigned char)((page_addr >> 8) & 0xff), docptr, Mplus_FlashAddress);
Masahiro Yamada14157f82017-09-13 11:05:50 +0900705 if (this->options & NAND_ROW_ADDR_3) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100706 WriteDOC((unsigned char)((page_addr >> 16) & 0x0f), docptr, Mplus_FlashAddress);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 printk("high density\n");
708 }
709 }
710 WriteDOC(0, docptr, Mplus_WritePipeTerm);
711 WriteDOC(0, docptr, Mplus_WritePipeTerm);
712 /* deassert ALE */
David Woodhousee0c7d762006-05-13 18:07:53 +0100713 if (command == NAND_CMD_READ0 || command == NAND_CMD_READ1 ||
714 command == NAND_CMD_READOOB || command == NAND_CMD_READID)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 WriteDOC(0, docptr, Mplus_FlashControl);
716 }
717
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000718 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 * program and erase have their own busy handlers
720 * status and sequential in needs no delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100721 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 switch (command) {
723
724 case NAND_CMD_PAGEPROG:
725 case NAND_CMD_ERASE1:
726 case NAND_CMD_ERASE2:
727 case NAND_CMD_SEQIN:
728 case NAND_CMD_STATUS:
729 return;
730
731 case NAND_CMD_RESET:
732 if (this->dev_ready)
733 break;
734 udelay(this->chip_delay);
735 WriteDOC(NAND_CMD_STATUS, docptr, Mplus_FlashCmd);
736 WriteDOC(0, docptr, Mplus_WritePipeTerm);
737 WriteDOC(0, docptr, Mplus_WritePipeTerm);
David Woodhousee0c7d762006-05-13 18:07:53 +0100738 while (!(this->read_byte(mtd) & 0x40)) ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 return;
740
David Woodhousee0c7d762006-05-13 18:07:53 +0100741 /* This applies to read commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000743 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 * If we don't have access to the busy pin, we apply the given
745 * command delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100746 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 if (!this->dev_ready) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100748 udelay(this->chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 return;
750 }
751 }
752
753 /* Apply this short delay always to ensure that we do wait tWB in
754 * any case on any machine. */
David Woodhousee0c7d762006-05-13 18:07:53 +0100755 ndelay(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 /* wait until command is processed */
David Woodhousee0c7d762006-05-13 18:07:53 +0100757 while (!this->dev_ready(mtd)) ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758}
759
760static int doc200x_dev_ready(struct mtd_info *mtd)
761{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100762 struct nand_chip *this = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100763 struct doc_priv *doc = nand_get_controller_data(this);
David Woodhousee0c7d762006-05-13 18:07:53 +0100764 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
766 if (DoC_is_MillenniumPlus(doc)) {
767 /* 11.4.2 -- must NOP four times before checking FR/B# */
768 DoC_Delay(doc, 4);
769 if ((ReadDOC(docptr, Mplus_FlashControl) & CDSN_CTRL_FR_B_MASK) != CDSN_CTRL_FR_B_MASK) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100770 if (debug)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 printk("not ready\n");
772 return 0;
773 }
David Woodhousee0c7d762006-05-13 18:07:53 +0100774 if (debug)
775 printk("was ready\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 return 1;
777 } else {
778 /* 11.4.2 -- must NOP four times before checking FR/B# */
779 DoC_Delay(doc, 4);
780 if (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B)) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100781 if (debug)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 printk("not ready\n");
783 return 0;
784 }
785 /* 11.4.2 -- Must NOP twice if it's ready */
786 DoC_Delay(doc, 2);
David Woodhousee0c7d762006-05-13 18:07:53 +0100787 if (debug)
788 printk("was ready\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 return 1;
790 }
791}
792
Archit Taneja9f3e0422016-02-03 14:29:49 +0530793static int doc200x_block_bad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794{
795 /* This is our last resort if we couldn't find or create a BBT. Just
796 pretend all blocks are good. */
797 return 0;
798}
799
800static void doc200x_enable_hwecc(struct mtd_info *mtd, int mode)
801{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100802 struct nand_chip *this = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100803 struct doc_priv *doc = nand_get_controller_data(this);
David Woodhousee0c7d762006-05-13 18:07:53 +0100804 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
806 /* Prime the ECC engine */
David Woodhousee0c7d762006-05-13 18:07:53 +0100807 switch (mode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 case NAND_ECC_READ:
809 WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
810 WriteDOC(DOC_ECC_EN, docptr, ECCConf);
811 break;
812 case NAND_ECC_WRITE:
813 WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
814 WriteDOC(DOC_ECC_EN | DOC_ECC_RW, docptr, ECCConf);
815 break;
816 }
817}
818
819static void doc2001plus_enable_hwecc(struct mtd_info *mtd, int mode)
820{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100821 struct nand_chip *this = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100822 struct doc_priv *doc = nand_get_controller_data(this);
David Woodhousee0c7d762006-05-13 18:07:53 +0100823 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
825 /* Prime the ECC engine */
David Woodhousee0c7d762006-05-13 18:07:53 +0100826 switch (mode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 case NAND_ECC_READ:
828 WriteDOC(DOC_ECC_RESET, docptr, Mplus_ECCConf);
829 WriteDOC(DOC_ECC_EN, docptr, Mplus_ECCConf);
830 break;
831 case NAND_ECC_WRITE:
832 WriteDOC(DOC_ECC_RESET, docptr, Mplus_ECCConf);
833 WriteDOC(DOC_ECC_EN | DOC_ECC_RW, docptr, Mplus_ECCConf);
834 break;
835 }
836}
837
838/* This code is only called on write */
David Woodhousee0c7d762006-05-13 18:07:53 +0100839static int doc200x_calculate_ecc(struct mtd_info *mtd, const u_char *dat, unsigned char *ecc_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100841 struct nand_chip *this = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100842 struct doc_priv *doc = nand_get_controller_data(this);
David Woodhousee0c7d762006-05-13 18:07:53 +0100843 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 int i;
845 int emptymatch = 1;
846
847 /* flush the pipeline */
848 if (DoC_is_2000(doc)) {
849 WriteDOC(doc->CDSNControl & ~CDSN_CTRL_FLASH_IO, docptr, CDSNControl);
850 WriteDOC(0, docptr, 2k_CDSN_IO);
851 WriteDOC(0, docptr, 2k_CDSN_IO);
852 WriteDOC(0, docptr, 2k_CDSN_IO);
853 WriteDOC(doc->CDSNControl, docptr, CDSNControl);
854 } else if (DoC_is_MillenniumPlus(doc)) {
855 WriteDOC(0, docptr, Mplus_NOP);
856 WriteDOC(0, docptr, Mplus_NOP);
857 WriteDOC(0, docptr, Mplus_NOP);
858 } else {
859 WriteDOC(0, docptr, NOP);
860 WriteDOC(0, docptr, NOP);
861 WriteDOC(0, docptr, NOP);
862 }
863
864 for (i = 0; i < 6; i++) {
865 if (DoC_is_MillenniumPlus(doc))
866 ecc_code[i] = ReadDOC_(docptr, DoC_Mplus_ECCSyndrome0 + i);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000867 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 ecc_code[i] = ReadDOC_(docptr, DoC_ECCSyndrome0 + i);
869 if (ecc_code[i] != empty_write_ecc[i])
870 emptymatch = 0;
871 }
872 if (DoC_is_MillenniumPlus(doc))
873 WriteDOC(DOC_ECC_DIS, docptr, Mplus_ECCConf);
874 else
875 WriteDOC(DOC_ECC_DIS, docptr, ECCConf);
876#if 0
877 /* If emptymatch=1, we might have an all-0xff data buffer. Check. */
878 if (emptymatch) {
879 /* Note: this somewhat expensive test should not be triggered
880 often. It could be optimized away by examining the data in
881 the writebuf routine, and remembering the result. */
882 for (i = 0; i < 512; i++) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100883 if (dat[i] == 0xff)
884 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 emptymatch = 0;
886 break;
887 }
888 }
889 /* If emptymatch still =1, we do have an all-0xff data buffer.
890 Return all-0xff ecc value instead of the computed one, so
891 it'll look just like a freshly-erased page. */
David Woodhousee0c7d762006-05-13 18:07:53 +0100892 if (emptymatch)
893 memset(ecc_code, 0xff, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894#endif
895 return 0;
896}
897
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200898static int doc200x_correct_data(struct mtd_info *mtd, u_char *dat,
899 u_char *read_ecc, u_char *isnull)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900{
901 int i, ret = 0;
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100902 struct nand_chip *this = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100903 struct doc_priv *doc = nand_get_controller_data(this);
David Woodhousee0c7d762006-05-13 18:07:53 +0100904 void __iomem *docptr = doc->virtadr;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200905 uint8_t calc_ecc[6];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 volatile u_char dummy;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000907
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 /* flush the pipeline */
909 if (DoC_is_2000(doc)) {
910 dummy = ReadDOC(docptr, 2k_ECCStatus);
911 dummy = ReadDOC(docptr, 2k_ECCStatus);
912 dummy = ReadDOC(docptr, 2k_ECCStatus);
913 } else if (DoC_is_MillenniumPlus(doc)) {
914 dummy = ReadDOC(docptr, Mplus_ECCConf);
915 dummy = ReadDOC(docptr, Mplus_ECCConf);
916 dummy = ReadDOC(docptr, Mplus_ECCConf);
917 } else {
918 dummy = ReadDOC(docptr, ECCConf);
919 dummy = ReadDOC(docptr, ECCConf);
920 dummy = ReadDOC(docptr, ECCConf);
921 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000922
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300923 /* Error occurred ? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 if (dummy & 0x80) {
925 for (i = 0; i < 6; i++) {
926 if (DoC_is_MillenniumPlus(doc))
927 calc_ecc[i] = ReadDOC_(docptr, DoC_Mplus_ECCSyndrome0 + i);
928 else
929 calc_ecc[i] = ReadDOC_(docptr, DoC_ECCSyndrome0 + i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 }
Boris BREZILLONcc01e602015-12-30 20:39:52 +0100931
Thomas Gleixner964dfce2018-04-22 18:23:54 +0200932 ret = doc_ecc_decode(doc->rs_decoder, dat, calc_ecc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 if (ret > 0)
Shreeya Patel63fa37f2018-02-22 22:01:22 +0530934 pr_err("doc200x_correct_data corrected %d errors\n",
935 ret);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000936 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 if (DoC_is_MillenniumPlus(doc))
938 WriteDOC(DOC_ECC_DIS, docptr, Mplus_ECCConf);
939 else
940 WriteDOC(DOC_ECC_DIS, docptr, ECCConf);
Brian Norrisd57f40542011-09-20 18:34:25 -0700941 if (no_ecc_failures && mtd_is_eccerr(ret)) {
Shreeya Patel63fa37f2018-02-22 22:01:22 +0530942 pr_err("suppressing ECC failure\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 ret = 0;
944 }
945 return ret;
946}
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000947
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948//u_char mydatabuf[528];
949
Boris Brezillon68c1b752016-02-03 20:00:23 +0100950static int doc200x_ooblayout_ecc(struct mtd_info *mtd, int section,
951 struct mtd_oob_region *oobregion)
952{
953 if (section)
954 return -ERANGE;
955
956 oobregion->offset = 0;
957 oobregion->length = 6;
958
959 return 0;
960}
961
962static int doc200x_ooblayout_free(struct mtd_info *mtd, int section,
963 struct mtd_oob_region *oobregion)
964{
965 if (section > 1)
966 return -ERANGE;
967
968 /*
969 * The strange out-of-order free bytes definition is a (possibly
970 * unneeded) attempt to retain compatibility. It used to read:
971 * .oobfree = { {8, 8} }
972 * Since that leaves two bytes unusable, it was changed. But the
973 * following scheme might affect existing jffs2 installs by moving the
974 * cleanmarker:
975 * .oobfree = { {6, 10} }
976 * jffs2 seems to handle the above gracefully, but the current scheme
977 * seems safer. The only problem with it is that any code retrieving
978 * free bytes position must be able to handle out-of-order segments.
979 */
980 if (!section) {
981 oobregion->offset = 8;
982 oobregion->length = 8;
983 } else {
984 oobregion->offset = 6;
985 oobregion->length = 2;
986 }
987
988 return 0;
989}
990
991static const struct mtd_ooblayout_ops doc200x_ooblayout_ops = {
992 .ecc = doc200x_ooblayout_ecc,
993 .free = doc200x_ooblayout_free,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994};
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000995
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996/* Find the (I)NFTL Media Header, and optionally also the mirror media header.
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200997 On successful return, buf will contain a copy of the media header for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 further processing. id is the string to scan for, and will presumably be
999 either "ANAND" or "BNAND". If findmirror=1, also look for the mirror media
1000 header. The page #s of the found media headers are placed in mh0_page and
1001 mh1_page in the DOC private structure. */
David Woodhousee0c7d762006-05-13 18:07:53 +01001002static int __init find_media_headers(struct mtd_info *mtd, u_char *buf, const char *id, int findmirror)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +01001004 struct nand_chip *this = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001005 struct doc_priv *doc = nand_get_controller_data(this);
Dan Brown1a78ff62005-03-29 21:57:48 +01001006 unsigned offs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 int ret;
1008 size_t retlen;
1009
Dan Brown1a78ff62005-03-29 21:57:48 +01001010 for (offs = 0; offs < mtd->size; offs += mtd->erasesize) {
Artem Bityutskiy329ad392011-12-23 17:30:16 +02001011 ret = mtd_read(mtd, offs, mtd->writesize, &retlen, buf);
Joern Engel28318772006-05-22 23:18:05 +02001012 if (retlen != mtd->writesize)
David Woodhousee0c7d762006-05-13 18:07:53 +01001013 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 if (ret) {
Shreeya Patel63fa37f2018-02-22 22:01:22 +05301015 pr_warn("ECC error scanning DOC at 0x%x\n", offs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 }
David Woodhousee0c7d762006-05-13 18:07:53 +01001017 if (memcmp(buf, id, 6))
1018 continue;
Shreeya Patel63fa37f2018-02-22 22:01:22 +05301019 pr_info("Found DiskOnChip %s Media Header at 0x%x\n", id, offs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 if (doc->mh0_page == -1) {
1021 doc->mh0_page = offs >> this->page_shift;
David Woodhousee0c7d762006-05-13 18:07:53 +01001022 if (!findmirror)
1023 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 continue;
1025 }
1026 doc->mh1_page = offs >> this->page_shift;
1027 return 2;
1028 }
1029 if (doc->mh0_page == -1) {
Shreeya Patel63fa37f2018-02-22 22:01:22 +05301030 pr_warn("DiskOnChip %s Media Header not found.\n", id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 return 0;
1032 }
1033 /* Only one mediaheader was found. We want buf to contain a
1034 mediaheader on return, so we'll have to re-read the one we found. */
1035 offs = doc->mh0_page << this->page_shift;
Artem Bityutskiy329ad392011-12-23 17:30:16 +02001036 ret = mtd_read(mtd, offs, mtd->writesize, &retlen, buf);
Joern Engel28318772006-05-22 23:18:05 +02001037 if (retlen != mtd->writesize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 /* Insanity. Give up. */
Shreeya Patel63fa37f2018-02-22 22:01:22 +05301039 pr_err("Read DiskOnChip Media Header once, but can't reread it???\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 return 0;
1041 }
1042 return 1;
1043}
1044
David Woodhousee0c7d762006-05-13 18:07:53 +01001045static inline int __init nftl_partscan(struct mtd_info *mtd, struct mtd_partition *parts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +01001047 struct nand_chip *this = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001048 struct doc_priv *doc = nand_get_controller_data(this);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 int ret = 0;
1050 u_char *buf;
1051 struct NFTLMediaHeader *mh;
1052 const unsigned psize = 1 << this->page_shift;
Dan Brown1a78ff62005-03-29 21:57:48 +01001053 int numparts = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 unsigned blocks, maxblocks;
1055 int offs, numheaders;
1056
Joern Engel28318772006-05-22 23:18:05 +02001057 buf = kmalloc(mtd->writesize, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 if (!buf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 return 0;
1060 }
David Woodhousee0c7d762006-05-13 18:07:53 +01001061 if (!(numheaders = find_media_headers(mtd, buf, "ANAND", 1)))
1062 goto out;
1063 mh = (struct NFTLMediaHeader *)buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064
Harvey Harrison96372442008-07-30 12:34:57 -07001065 le16_to_cpus(&mh->NumEraseUnits);
1066 le16_to_cpus(&mh->FirstPhysicalEUN);
1067 le32_to_cpus(&mh->FormattedSize);
Thomas Gleixnerf29a4b82005-01-31 22:22:24 +00001068
Shreeya Patel63fa37f2018-02-22 22:01:22 +05301069 pr_info(" DataOrgID = %s\n"
1070 " NumEraseUnits = %d\n"
1071 " FirstPhysicalEUN = %d\n"
1072 " FormattedSize = %d\n"
1073 " UnitSizeFactor = %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 mh->DataOrgID, mh->NumEraseUnits,
1075 mh->FirstPhysicalEUN, mh->FormattedSize,
1076 mh->UnitSizeFactor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
1078 blocks = mtd->size >> this->phys_erase_shift;
1079 maxblocks = min(32768U, mtd->erasesize - psize);
1080
1081 if (mh->UnitSizeFactor == 0x00) {
1082 /* Auto-determine UnitSizeFactor. The constraints are:
1083 - There can be at most 32768 virtual blocks.
1084 - There can be at most (virtual block size - page size)
David Woodhousee0c7d762006-05-13 18:07:53 +01001085 virtual blocks (because MediaHeader+BBT must fit in 1).
1086 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 mh->UnitSizeFactor = 0xff;
1088 while (blocks > maxblocks) {
1089 blocks >>= 1;
1090 maxblocks = min(32768U, (maxblocks << 1) + psize);
1091 mh->UnitSizeFactor--;
1092 }
Shreeya Patel63fa37f2018-02-22 22:01:22 +05301093 pr_warn("UnitSizeFactor=0x00 detected. Correct value is assumed to be 0x%02x.\n", mh->UnitSizeFactor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 }
1095
1096 /* NOTE: The lines below modify internal variables of the NAND and MTD
1097 layers; variables with have already been configured by nand_scan.
1098 Unfortunately, we didn't know before this point what these values
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001099 should be. Thus, this code is somewhat dependent on the exact
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 implementation of the NAND layer. */
1101 if (mh->UnitSizeFactor != 0xff) {
1102 this->bbt_erase_shift += (0xff - mh->UnitSizeFactor);
1103 mtd->erasesize <<= (0xff - mh->UnitSizeFactor);
Shreeya Patel63fa37f2018-02-22 22:01:22 +05301104 pr_info("Setting virtual erase size to %d\n", mtd->erasesize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 blocks = mtd->size >> this->bbt_erase_shift;
1106 maxblocks = min(32768U, mtd->erasesize - psize);
1107 }
1108
1109 if (blocks > maxblocks) {
Shreeya Patel63fa37f2018-02-22 22:01:22 +05301110 pr_err("UnitSizeFactor of 0x%02x is inconsistent with device size. Aborting.\n", mh->UnitSizeFactor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 goto out;
1112 }
1113
1114 /* Skip past the media headers. */
1115 offs = max(doc->mh0_page, doc->mh1_page);
1116 offs <<= this->page_shift;
1117 offs += mtd->erasesize;
1118
Dan Brown1a78ff62005-03-29 21:57:48 +01001119 if (show_firmware_partition == 1) {
1120 parts[0].name = " DiskOnChip Firmware / Media Header partition";
1121 parts[0].offset = 0;
1122 parts[0].size = offs;
1123 numparts = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 }
Dan Brown1a78ff62005-03-29 21:57:48 +01001125
1126 parts[numparts].name = " DiskOnChip BDTL partition";
1127 parts[numparts].offset = offs;
1128 parts[numparts].size = (mh->NumEraseUnits - numheaders) << this->bbt_erase_shift;
1129
1130 offs += parts[numparts].size;
1131 numparts++;
1132
1133 if (offs < mtd->size) {
1134 parts[numparts].name = " DiskOnChip Remainder partition";
1135 parts[numparts].offset = offs;
1136 parts[numparts].size = mtd->size - offs;
1137 numparts++;
1138 }
1139
1140 ret = numparts;
David Woodhousee0c7d762006-05-13 18:07:53 +01001141 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 kfree(buf);
1143 return ret;
1144}
1145
1146/* This is a stripped-down copy of the code in inftlmount.c */
David Woodhousee0c7d762006-05-13 18:07:53 +01001147static inline int __init inftl_partscan(struct mtd_info *mtd, struct mtd_partition *parts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +01001149 struct nand_chip *this = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001150 struct doc_priv *doc = nand_get_controller_data(this);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 int ret = 0;
1152 u_char *buf;
1153 struct INFTLMediaHeader *mh;
1154 struct INFTLPartition *ip;
1155 int numparts = 0;
1156 int blocks;
1157 int vshift, lastvunit = 0;
1158 int i;
1159 int end = mtd->size;
1160
1161 if (inftl_bbt_write)
1162 end -= (INFTL_BBT_RESERVED_BLOCKS << this->phys_erase_shift);
1163
Joern Engel28318772006-05-22 23:18:05 +02001164 buf = kmalloc(mtd->writesize, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 if (!buf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 return 0;
1167 }
1168
David Woodhousee0c7d762006-05-13 18:07:53 +01001169 if (!find_media_headers(mtd, buf, "BNAND", 0))
1170 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 doc->mh1_page = doc->mh0_page + (4096 >> this->page_shift);
David Woodhousee0c7d762006-05-13 18:07:53 +01001172 mh = (struct INFTLMediaHeader *)buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173
Harvey Harrison96372442008-07-30 12:34:57 -07001174 le32_to_cpus(&mh->NoOfBootImageBlocks);
1175 le32_to_cpus(&mh->NoOfBinaryPartitions);
1176 le32_to_cpus(&mh->NoOfBDTLPartitions);
1177 le32_to_cpus(&mh->BlockMultiplierBits);
1178 le32_to_cpus(&mh->FormatFlags);
1179 le32_to_cpus(&mh->PercentUsed);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001180
Shreeya Patel63fa37f2018-02-22 22:01:22 +05301181 pr_info(" bootRecordID = %s\n"
1182 " NoOfBootImageBlocks = %d\n"
1183 " NoOfBinaryPartitions = %d\n"
1184 " NoOfBDTLPartitions = %d\n"
1185 " BlockMultiplerBits = %d\n"
1186 " FormatFlgs = %d\n"
1187 " OsakVersion = %d.%d.%d.%d\n"
1188 " PercentUsed = %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 mh->bootRecordID, mh->NoOfBootImageBlocks,
1190 mh->NoOfBinaryPartitions,
1191 mh->NoOfBDTLPartitions,
1192 mh->BlockMultiplierBits, mh->FormatFlags,
1193 ((unsigned char *) &mh->OsakVersion)[0] & 0xf,
1194 ((unsigned char *) &mh->OsakVersion)[1] & 0xf,
1195 ((unsigned char *) &mh->OsakVersion)[2] & 0xf,
1196 ((unsigned char *) &mh->OsakVersion)[3] & 0xf,
1197 mh->PercentUsed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
1199 vshift = this->phys_erase_shift + mh->BlockMultiplierBits;
1200
1201 blocks = mtd->size >> vshift;
1202 if (blocks > 32768) {
Shreeya Patel63fa37f2018-02-22 22:01:22 +05301203 pr_err("BlockMultiplierBits=%d is inconsistent with device size. Aborting.\n", mh->BlockMultiplierBits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 goto out;
1205 }
1206
1207 blocks = doc->chips_per_floor << (this->chip_shift - this->phys_erase_shift);
1208 if (inftl_bbt_write && (blocks > mtd->erasesize)) {
Shreeya Patel63fa37f2018-02-22 22:01:22 +05301209 pr_err("Writeable BBTs spanning more than one erase block are not yet supported. FIX ME!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 goto out;
1211 }
1212
1213 /* Scan the partitions */
1214 for (i = 0; (i < 4); i++) {
1215 ip = &(mh->Partitions[i]);
Harvey Harrison96372442008-07-30 12:34:57 -07001216 le32_to_cpus(&ip->virtualUnits);
1217 le32_to_cpus(&ip->firstUnit);
1218 le32_to_cpus(&ip->lastUnit);
1219 le32_to_cpus(&ip->flags);
1220 le32_to_cpus(&ip->spareUnits);
1221 le32_to_cpus(&ip->Reserved0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222
Shreeya Patel63fa37f2018-02-22 22:01:22 +05301223 pr_info(" PARTITION[%d] ->\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 " virtualUnits = %d\n"
1225 " firstUnit = %d\n"
1226 " lastUnit = %d\n"
1227 " flags = 0x%x\n"
1228 " spareUnits = %d\n",
1229 i, ip->virtualUnits, ip->firstUnit,
1230 ip->lastUnit, ip->flags,
1231 ip->spareUnits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232
Dan Brown1a78ff62005-03-29 21:57:48 +01001233 if ((show_firmware_partition == 1) &&
1234 (i == 0) && (ip->firstUnit > 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 parts[0].name = " DiskOnChip IPL / Media Header partition";
1236 parts[0].offset = 0;
1237 parts[0].size = mtd->erasesize * ip->firstUnit;
1238 numparts = 1;
1239 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240
1241 if (ip->flags & INFTL_BINARY)
1242 parts[numparts].name = " DiskOnChip BDK partition";
1243 else
1244 parts[numparts].name = " DiskOnChip BDTL partition";
1245 parts[numparts].offset = ip->firstUnit << vshift;
1246 parts[numparts].size = (1 + ip->lastUnit - ip->firstUnit) << vshift;
1247 numparts++;
David Woodhousee0c7d762006-05-13 18:07:53 +01001248 if (ip->lastUnit > lastvunit)
1249 lastvunit = ip->lastUnit;
1250 if (ip->flags & INFTL_LAST)
1251 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 }
1253 lastvunit++;
1254 if ((lastvunit << vshift) < end) {
1255 parts[numparts].name = " DiskOnChip Remainder partition";
1256 parts[numparts].offset = lastvunit << vshift;
1257 parts[numparts].size = end - parts[numparts].offset;
1258 numparts++;
1259 }
1260 ret = numparts;
David Woodhousee0c7d762006-05-13 18:07:53 +01001261 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 kfree(buf);
1263 return ret;
1264}
1265
1266static int __init nftl_scan_bbt(struct mtd_info *mtd)
1267{
1268 int ret, numparts;
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +01001269 struct nand_chip *this = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001270 struct doc_priv *doc = nand_get_controller_data(this);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 struct mtd_partition parts[2];
1272
David Woodhousee0c7d762006-05-13 18:07:53 +01001273 memset((char *)parts, 0, sizeof(parts));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 /* On NFTL, we have to find the media headers before we can read the
1275 BBTs, since they're stored in the media header eraseblocks. */
1276 numparts = nftl_partscan(mtd, parts);
David Woodhousee0c7d762006-05-13 18:07:53 +01001277 if (!numparts)
1278 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 this->bbt_td->options = NAND_BBT_ABSPAGE | NAND_BBT_8BIT |
1280 NAND_BBT_SAVECONTENT | NAND_BBT_WRITE |
1281 NAND_BBT_VERSION;
1282 this->bbt_td->veroffs = 7;
1283 this->bbt_td->pages[0] = doc->mh0_page + 1;
1284 if (doc->mh1_page != -1) {
1285 this->bbt_md->options = NAND_BBT_ABSPAGE | NAND_BBT_8BIT |
1286 NAND_BBT_SAVECONTENT | NAND_BBT_WRITE |
1287 NAND_BBT_VERSION;
1288 this->bbt_md->veroffs = 7;
1289 this->bbt_md->pages[0] = doc->mh1_page + 1;
1290 } else {
1291 this->bbt_md = NULL;
1292 }
1293
Boris Brezillone80eba72018-07-05 12:27:31 +02001294 ret = nand_create_bbt(this);
Brian Norrisd24fe0c2015-02-28 02:13:10 -08001295 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 return ret;
Brian Norrisd24fe0c2015-02-28 02:13:10 -08001297
Brian Norris6a7c7332015-06-01 16:17:17 -07001298 return mtd_device_register(mtd, parts, no_autopart ? 0 : numparts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299}
1300
1301static int __init inftl_scan_bbt(struct mtd_info *mtd)
1302{
1303 int ret, numparts;
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +01001304 struct nand_chip *this = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001305 struct doc_priv *doc = nand_get_controller_data(this);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 struct mtd_partition parts[5];
1307
1308 if (this->numchips > doc->chips_per_floor) {
Shreeya Patel63fa37f2018-02-22 22:01:22 +05301309 pr_err("Multi-floor INFTL devices not yet supported.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 return -EIO;
1311 }
1312
1313 if (DoC_is_MillenniumPlus(doc)) {
1314 this->bbt_td->options = NAND_BBT_2BIT | NAND_BBT_ABSPAGE;
1315 if (inftl_bbt_write)
1316 this->bbt_td->options |= NAND_BBT_WRITE;
1317 this->bbt_td->pages[0] = 2;
1318 this->bbt_md = NULL;
1319 } else {
David Woodhousee0c7d762006-05-13 18:07:53 +01001320 this->bbt_td->options = NAND_BBT_LASTBLOCK | NAND_BBT_8BIT | NAND_BBT_VERSION;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 if (inftl_bbt_write)
1322 this->bbt_td->options |= NAND_BBT_WRITE;
1323 this->bbt_td->offs = 8;
1324 this->bbt_td->len = 8;
1325 this->bbt_td->veroffs = 7;
1326 this->bbt_td->maxblocks = INFTL_BBT_RESERVED_BLOCKS;
1327 this->bbt_td->reserved_block_code = 0x01;
1328 this->bbt_td->pattern = "MSYS_BBT";
1329
David Woodhousee0c7d762006-05-13 18:07:53 +01001330 this->bbt_md->options = NAND_BBT_LASTBLOCK | NAND_BBT_8BIT | NAND_BBT_VERSION;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 if (inftl_bbt_write)
1332 this->bbt_md->options |= NAND_BBT_WRITE;
1333 this->bbt_md->offs = 8;
1334 this->bbt_md->len = 8;
1335 this->bbt_md->veroffs = 7;
1336 this->bbt_md->maxblocks = INFTL_BBT_RESERVED_BLOCKS;
1337 this->bbt_md->reserved_block_code = 0x01;
1338 this->bbt_md->pattern = "TBB_SYSM";
1339 }
1340
Boris Brezillone80eba72018-07-05 12:27:31 +02001341 ret = nand_create_bbt(this);
Brian Norrisd24fe0c2015-02-28 02:13:10 -08001342 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 return ret;
Brian Norrisd24fe0c2015-02-28 02:13:10 -08001344
David Woodhousee0c7d762006-05-13 18:07:53 +01001345 memset((char *)parts, 0, sizeof(parts));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 numparts = inftl_partscan(mtd, parts);
1347 /* At least for now, require the INFTL Media Header. We could probably
1348 do without it for non-INFTL use, since all it gives us is
1349 autopartitioning, but I want to give it more thought. */
David Woodhousee0c7d762006-05-13 18:07:53 +01001350 if (!numparts)
1351 return -EIO;
Brian Norris6a7c7332015-06-01 16:17:17 -07001352 return mtd_device_register(mtd, parts, no_autopart ? 0 : numparts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353}
1354
1355static inline int __init doc2000_init(struct mtd_info *mtd)
1356{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +01001357 struct nand_chip *this = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001358 struct doc_priv *doc = nand_get_controller_data(this);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 this->read_byte = doc2000_read_byte;
1361 this->write_buf = doc2000_writebuf;
1362 this->read_buf = doc2000_readbuf;
Brian Norrisd24fe0c2015-02-28 02:13:10 -08001363 doc->late_init = nftl_scan_bbt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
1365 doc->CDSNControl = CDSN_CTRL_FLASH_IO | CDSN_CTRL_ECC_IO;
1366 doc2000_count_chips(mtd);
1367 mtd->name = "DiskOnChip 2000 (NFTL Model)";
1368 return (4 * doc->chips_per_floor);
1369}
1370
1371static inline int __init doc2001_init(struct mtd_info *mtd)
1372{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +01001373 struct nand_chip *this = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001374 struct doc_priv *doc = nand_get_controller_data(this);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 this->read_byte = doc2001_read_byte;
1377 this->write_buf = doc2001_writebuf;
1378 this->read_buf = doc2001_readbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379
1380 ReadDOC(doc->virtadr, ChipID);
1381 ReadDOC(doc->virtadr, ChipID);
1382 ReadDOC(doc->virtadr, ChipID);
1383 if (ReadDOC(doc->virtadr, ChipID) != DOC_ChipID_DocMil) {
1384 /* It's not a Millennium; it's one of the newer
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001385 DiskOnChip 2000 units with a similar ASIC.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 Treat it like a Millennium, except that it
1387 can have multiple chips. */
1388 doc2000_count_chips(mtd);
1389 mtd->name = "DiskOnChip 2000 (INFTL Model)";
Brian Norrisd24fe0c2015-02-28 02:13:10 -08001390 doc->late_init = inftl_scan_bbt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 return (4 * doc->chips_per_floor);
1392 } else {
1393 /* Bog-standard Millennium */
1394 doc->chips_per_floor = 1;
1395 mtd->name = "DiskOnChip Millennium";
Brian Norrisd24fe0c2015-02-28 02:13:10 -08001396 doc->late_init = nftl_scan_bbt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 return 1;
1398 }
1399}
1400
1401static inline int __init doc2001plus_init(struct mtd_info *mtd)
1402{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +01001403 struct nand_chip *this = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001404 struct doc_priv *doc = nand_get_controller_data(this);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 this->read_byte = doc2001plus_read_byte;
1407 this->write_buf = doc2001plus_writebuf;
1408 this->read_buf = doc2001plus_readbuf;
Brian Norrisd24fe0c2015-02-28 02:13:10 -08001409 doc->late_init = inftl_scan_bbt;
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +02001410 this->cmd_ctrl = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 this->select_chip = doc2001plus_select_chip;
1412 this->cmdfunc = doc2001plus_command;
Thomas Gleixner0cddd6c2006-05-23 15:59:58 +02001413 this->ecc.hwctl = doc2001plus_enable_hwecc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414
1415 doc->chips_per_floor = 1;
1416 mtd->name = "DiskOnChip Millennium Plus";
1417
1418 return 1;
1419}
1420
Arjan van de Ven858119e2006-01-14 13:20:43 -08001421static int __init doc_probe(unsigned long physadr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422{
Thomas Gleixner964dfce2018-04-22 18:23:54 +02001423 struct nand_chip *nand = NULL;
1424 struct doc_priv *doc = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 unsigned char ChipID;
1426 struct mtd_info *mtd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 void __iomem *virtadr;
1428 unsigned char save_control;
1429 unsigned char tmp, tmpb, tmpc;
1430 int reg, len, numchips;
1431 int ret = 0;
1432
Sasha Levin86e4bbc2014-03-19 18:24:37 -04001433 if (!request_mem_region(physadr, DOC_IOREMAP_LEN, "DiskOnChip"))
Alexander Shiyan4f0614a2013-11-13 15:59:25 +04001434 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 virtadr = ioremap(physadr, DOC_IOREMAP_LEN);
1436 if (!virtadr) {
Shreeya Patel63fa37f2018-02-22 22:01:22 +05301437 pr_err("Diskonchip ioremap failed: 0x%x bytes at 0x%lx\n",
1438 DOC_IOREMAP_LEN, physadr);
Alexander Shiyan4f0614a2013-11-13 15:59:25 +04001439 ret = -EIO;
1440 goto error_ioremap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 }
1442
1443 /* It's not possible to cleanly detect the DiskOnChip - the
1444 * bootup procedure will put the device into reset mode, and
1445 * it's not possible to talk to it without actually writing
1446 * to the DOCControl register. So we store the current contents
1447 * of the DOCControl register's location, in case we later decide
1448 * that it's not a DiskOnChip, and want to put it back how we
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001449 * found it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 */
1451 save_control = ReadDOC(virtadr, DOCControl);
1452
1453 /* Reset the DiskOnChip ASIC */
David Woodhousee0c7d762006-05-13 18:07:53 +01001454 WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_RESET, virtadr, DOCControl);
1455 WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_RESET, virtadr, DOCControl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
1457 /* Enable the DiskOnChip ASIC */
David Woodhousee0c7d762006-05-13 18:07:53 +01001458 WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_NORMAL, virtadr, DOCControl);
1459 WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_NORMAL, virtadr, DOCControl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460
1461 ChipID = ReadDOC(virtadr, ChipID);
1462
David Woodhousee0c7d762006-05-13 18:07:53 +01001463 switch (ChipID) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 case DOC_ChipID_Doc2k:
1465 reg = DoC_2k_ECCStatus;
1466 break;
1467 case DOC_ChipID_DocMil:
1468 reg = DoC_ECCConf;
1469 break;
1470 case DOC_ChipID_DocMilPlus16:
1471 case DOC_ChipID_DocMilPlus32:
1472 case 0:
1473 /* Possible Millennium Plus, need to do more checks */
1474 /* Possibly release from power down mode */
1475 for (tmp = 0; (tmp < 4); tmp++)
1476 ReadDOC(virtadr, Mplus_Power);
1477
1478 /* Reset the Millennium Plus ASIC */
David Woodhousee0c7d762006-05-13 18:07:53 +01001479 tmp = DOC_MODE_RESET | DOC_MODE_MDWREN | DOC_MODE_RST_LAT | DOC_MODE_BDECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 WriteDOC(tmp, virtadr, Mplus_DOCControl);
1481 WriteDOC(~tmp, virtadr, Mplus_CtrlConfirm);
1482
Jia-Ju Bai7b4b1992018-04-11 10:57:57 +08001483 usleep_range(1000, 2000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 /* Enable the Millennium Plus ASIC */
David Woodhousee0c7d762006-05-13 18:07:53 +01001485 tmp = DOC_MODE_NORMAL | DOC_MODE_MDWREN | DOC_MODE_RST_LAT | DOC_MODE_BDECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 WriteDOC(tmp, virtadr, Mplus_DOCControl);
1487 WriteDOC(~tmp, virtadr, Mplus_CtrlConfirm);
Jia-Ju Bai7b4b1992018-04-11 10:57:57 +08001488 usleep_range(1000, 2000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489
1490 ChipID = ReadDOC(virtadr, ChipID);
1491
1492 switch (ChipID) {
1493 case DOC_ChipID_DocMilPlus16:
1494 reg = DoC_Mplus_Toggle;
1495 break;
1496 case DOC_ChipID_DocMilPlus32:
Shreeya Patel63fa37f2018-02-22 22:01:22 +05301497 pr_err("DiskOnChip Millennium Plus 32MB is not supported, ignoring.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 default:
1499 ret = -ENODEV;
1500 goto notfound;
1501 }
1502 break;
1503
1504 default:
1505 ret = -ENODEV;
1506 goto notfound;
1507 }
1508 /* Check the TOGGLE bit in the ECC register */
David Woodhousee0c7d762006-05-13 18:07:53 +01001509 tmp = ReadDOC_(virtadr, reg) & DOC_TOGGLE_BIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 tmpb = ReadDOC_(virtadr, reg) & DOC_TOGGLE_BIT;
1511 tmpc = ReadDOC_(virtadr, reg) & DOC_TOGGLE_BIT;
1512 if ((tmp == tmpb) || (tmp != tmpc)) {
Shreeya Patel63fa37f2018-02-22 22:01:22 +05301513 pr_warn("Possible DiskOnChip at 0x%lx failed TOGGLE test, dropping.\n", physadr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 ret = -ENODEV;
1515 goto notfound;
1516 }
1517
1518 for (mtd = doclist; mtd; mtd = doc->nextdoc) {
1519 unsigned char oldval;
1520 unsigned char newval;
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +01001521 nand = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001522 doc = nand_get_controller_data(nand);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 /* Use the alias resolution register to determine if this is
1524 in fact the same DOC aliased to a new address. If writes
1525 to one chip's alias resolution register change the value on
1526 the other chip, they're the same chip. */
1527 if (ChipID == DOC_ChipID_DocMilPlus16) {
1528 oldval = ReadDOC(doc->virtadr, Mplus_AliasResolution);
1529 newval = ReadDOC(virtadr, Mplus_AliasResolution);
1530 } else {
1531 oldval = ReadDOC(doc->virtadr, AliasResolution);
1532 newval = ReadDOC(virtadr, AliasResolution);
1533 }
1534 if (oldval != newval)
1535 continue;
1536 if (ChipID == DOC_ChipID_DocMilPlus16) {
1537 WriteDOC(~newval, virtadr, Mplus_AliasResolution);
1538 oldval = ReadDOC(doc->virtadr, Mplus_AliasResolution);
David Woodhousee0c7d762006-05-13 18:07:53 +01001539 WriteDOC(newval, virtadr, Mplus_AliasResolution); // restore it
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 } else {
1541 WriteDOC(~newval, virtadr, AliasResolution);
1542 oldval = ReadDOC(doc->virtadr, AliasResolution);
David Woodhousee0c7d762006-05-13 18:07:53 +01001543 WriteDOC(newval, virtadr, AliasResolution); // restore it
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 }
1545 newval = ~newval;
1546 if (oldval == newval) {
Shreeya Patel63fa37f2018-02-22 22:01:22 +05301547 pr_debug("Found alias of DOC at 0x%lx to 0x%lx\n",
1548 doc->physadr, physadr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 goto notfound;
1550 }
1551 }
1552
Shreeya Patel63fa37f2018-02-22 22:01:22 +05301553 pr_notice("DiskOnChip found at 0x%lx\n", physadr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554
Boris BREZILLONb0c423c2015-12-10 09:00:00 +01001555 len = sizeof(struct nand_chip) + sizeof(struct doc_priv) +
1556 (2 * sizeof(struct nand_bbt_descr));
1557 nand = kzalloc(len, GFP_KERNEL);
1558 if (!nand) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 ret = -ENOMEM;
1560 goto fail;
1561 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562
Thomas Gleixner964dfce2018-04-22 18:23:54 +02001563
1564 /*
1565 * Allocate a RS codec instance
1566 *
1567 * Symbolsize is 10 (bits)
1568 * Primitve polynomial is x^10+x^3+1
1569 * First consecutive root is 510
1570 * Primitve element to generate roots = 1
1571 * Generator polinomial degree = 4
1572 */
1573 doc = (struct doc_priv *) (nand + 1);
1574 doc->rs_decoder = init_rs(10, 0x409, FCR, 1, NROOTS);
1575 if (!doc->rs_decoder) {
1576 pr_err("DiskOnChip: Could not create a RS codec\n");
1577 ret = -ENOMEM;
1578 goto fail;
1579 }
1580
Boris BREZILLONb0c423c2015-12-10 09:00:00 +01001581 mtd = nand_to_mtd(nand);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 nand->bbt_td = (struct nand_bbt_descr *) (doc + 1);
1583 nand->bbt_md = nand->bbt_td + 1;
1584
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 mtd->owner = THIS_MODULE;
Boris Brezillon68c1b752016-02-03 20:00:23 +01001586 mtd_set_ooblayout(mtd, &doc200x_ooblayout_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001588 nand_set_controller_data(nand, doc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 nand->select_chip = doc200x_select_chip;
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +02001590 nand->cmd_ctrl = doc200x_hwcontrol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 nand->dev_ready = doc200x_dev_ready;
1592 nand->waitfunc = doc200x_wait;
1593 nand->block_bad = doc200x_block_bad;
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02001594 nand->ecc.hwctl = doc200x_enable_hwecc;
1595 nand->ecc.calculate = doc200x_calculate_ecc;
1596 nand->ecc.correct = doc200x_correct_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02001598 nand->ecc.mode = NAND_ECC_HW_SYNDROME;
1599 nand->ecc.size = 512;
1600 nand->ecc.bytes = 6;
Mike Dunn6a918ba2012-03-11 14:21:11 -07001601 nand->ecc.strength = 2;
Boris BREZILLONcc01e602015-12-30 20:39:52 +01001602 nand->ecc.options = NAND_ECC_GENERIC_ERASED_CHECK;
Brian Norrisbb9ebd42011-05-31 16:31:23 -07001603 nand->bbt_options = NAND_BBT_USE_FLASH;
Brian Norrisd24fe0c2015-02-28 02:13:10 -08001604 /* Skip the automatic BBT scan so we can run it manually */
1605 nand->options |= NAND_SKIP_BBTSCAN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606
1607 doc->physadr = physadr;
1608 doc->virtadr = virtadr;
1609 doc->ChipID = ChipID;
1610 doc->curfloor = -1;
1611 doc->curchip = -1;
1612 doc->mh0_page = -1;
1613 doc->mh1_page = -1;
1614 doc->nextdoc = doclist;
1615
1616 if (ChipID == DOC_ChipID_Doc2k)
1617 numchips = doc2000_init(mtd);
1618 else if (ChipID == DOC_ChipID_DocMilPlus16)
1619 numchips = doc2001plus_init(mtd);
1620 else
1621 numchips = doc2001_init(mtd);
1622
Boris Brezillon00ad3782018-09-06 14:05:14 +02001623 if ((ret = nand_scan(nand, numchips)) || (ret = doc->late_init(mtd))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 /* DBB note: i believe nand_release is necessary here, as
1625 buffers may have been allocated in nand_base. Check with
1626 Thomas. FIX ME! */
Jamie Iles0f47e952011-05-23 10:23:19 +01001627 /* nand_release will call mtd_device_unregister, but we
1628 haven't yet added it. This is handled without incident by
1629 mtd_device_unregister, as far as I can tell. */
Boris Brezillon59ac2762018-09-06 14:05:15 +02001630 nand_release(nand);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 goto fail;
1632 }
1633
1634 /* Success! */
1635 doclist = mtd;
1636 return 0;
1637
David Woodhousee0c7d762006-05-13 18:07:53 +01001638 notfound:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 /* Put back the contents of the DOCControl register, in case it's not
1640 actually a DiskOnChip. */
1641 WriteDOC(save_control, virtadr, DOCControl);
David Woodhousee0c7d762006-05-13 18:07:53 +01001642 fail:
Thomas Gleixner964dfce2018-04-22 18:23:54 +02001643 if (doc)
1644 free_rs(doc->rs_decoder);
1645 kfree(nand);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 iounmap(virtadr);
Alexander Shiyan4f0614a2013-11-13 15:59:25 +04001647
1648error_ioremap:
1649 release_mem_region(physadr, DOC_IOREMAP_LEN);
1650
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 return ret;
1652}
1653
1654static void release_nanddoc(void)
1655{
David Woodhousee0c7d762006-05-13 18:07:53 +01001656 struct mtd_info *mtd, *nextmtd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 struct nand_chip *nand;
1658 struct doc_priv *doc;
1659
1660 for (mtd = doclist; mtd; mtd = nextmtd) {
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +01001661 nand = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001662 doc = nand_get_controller_data(nand);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663
1664 nextmtd = doc->nextdoc;
Boris Brezillon59ac2762018-09-06 14:05:15 +02001665 nand_release(nand);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 iounmap(doc->virtadr);
Alexander Shiyan4f0614a2013-11-13 15:59:25 +04001667 release_mem_region(doc->physadr, DOC_IOREMAP_LEN);
Thomas Gleixner964dfce2018-04-22 18:23:54 +02001668 free_rs(doc->rs_decoder);
Boris BREZILLONb0c423c2015-12-10 09:00:00 +01001669 kfree(nand);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 }
1671}
1672
1673static int __init init_nanddoc(void)
1674{
1675 int i, ret = 0;
1676
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 if (doc_config_location) {
Shreeya Patel63fa37f2018-02-22 22:01:22 +05301678 pr_info("Using configured DiskOnChip probe address 0x%lx\n",
1679 doc_config_location);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 ret = doc_probe(doc_config_location);
1681 if (ret < 0)
Thomas Gleixner964dfce2018-04-22 18:23:54 +02001682 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 } else {
David Woodhousee0c7d762006-05-13 18:07:53 +01001684 for (i = 0; (doc_locations[i] != 0xffffffff); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 doc_probe(doc_locations[i]);
1686 }
1687 }
1688 /* No banner message any more. Print a message if no DiskOnChip
1689 found, so the user knows we at least tried. */
1690 if (!doclist) {
Shreeya Patel63fa37f2018-02-22 22:01:22 +05301691 pr_info("No valid DiskOnChip devices found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 ret = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 return ret;
1695}
1696
1697static void __exit cleanup_nanddoc(void)
1698{
1699 /* Cleanup the nand/DoC resources */
1700 release_nanddoc();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701}
1702
1703module_init(init_nanddoc);
1704module_exit(cleanup_nanddoc);
1705
1706MODULE_LICENSE("GPL");
1707MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
Niels de Vos2a7af8c2009-01-30 11:08:35 +01001708MODULE_DESCRIPTION("M-Systems DiskOnChip 2000, Millennium and Millennium Plus device driver");