blob: c0e1a8ebe8206b52c0e65e2839b876616e3e22af [file] [log] [blame]
Thomas Gleixner09c434b2019-05-19 13:08:20 +01001// SPDX-License-Identifier: GPL-2.0-only
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * (C) 2003 Red Hat, Inc.
4 * (C) 2004 Dan Brown <dan_brown@ieee.org>
5 * (C) 2004 Kalev Lember <kalev@smartlink.ee>
6 *
7 * Author: David Woodhouse <dwmw2@infradead.org>
8 * Additional Diskonchip 2000 and Millennium support by Dan Brown <dan_brown@ieee.org>
9 * Diskonchip Millennium Plus support by Kalev Lember <kalev@smartlink.ee>
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000010 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 * Error correction code lifted from the old docecc code
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000012 * Author: Fabrice Bellard (fabrice.bellard@netgem.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 * Copyright (C) 2000 Netgem S.A.
14 * converted to the generic Reed-Solomon library by Thomas Gleixner <tglx@linutronix.de>
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000015 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 * Interface to generic NAND code for M-Systems DiskOnChip devices
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
18
19#include <linux/kernel.h>
20#include <linux/init.h>
21#include <linux/sched.h>
22#include <linux/delay.h>
23#include <linux/rslib.h>
24#include <linux/moduleparam.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/slab.h>
Dan Williams2584cf82015-08-10 23:07:05 -040026#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
28#include <linux/mtd/mtd.h>
Boris Brezillond4092d72017-08-04 17:29:10 +020029#include <linux/mtd/rawnand.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/mtd/doc2000.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/mtd/partitions.h>
32#include <linux/mtd/inftl.h>
Paul Gortmakera0e5cc52011-07-03 15:17:31 -040033#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
35/* Where to look for the devices? */
Thomas Gleixner651078b2005-01-31 20:36:46 +000036#ifndef CONFIG_MTD_NAND_DISKONCHIP_PROBE_ADDRESS
37#define CONFIG_MTD_NAND_DISKONCHIP_PROBE_ADDRESS 0
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#endif
39
Sachin Kamat14a95b8a2013-08-12 15:10:47 +053040static unsigned long doc_locations[] __initdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#if defined (__alpha__) || defined(__i386__) || defined(__x86_64__)
Thomas Gleixner651078b2005-01-31 20:36:46 +000042#ifdef CONFIG_MTD_NAND_DISKONCHIP_PROBE_HIGH
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000043 0xfffc8000, 0xfffca000, 0xfffcc000, 0xfffce000,
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 0xfffd0000, 0xfffd2000, 0xfffd4000, 0xfffd6000,
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000045 0xfffd8000, 0xfffda000, 0xfffdc000, 0xfffde000,
46 0xfffe0000, 0xfffe2000, 0xfffe4000, 0xfffe6000,
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 0xfffe8000, 0xfffea000, 0xfffec000, 0xfffee000,
Michael Opdenacker9d403492013-07-08 06:39:20 +020048#else
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000049 0xc8000, 0xca000, 0xcc000, 0xce000,
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 0xd0000, 0xd2000, 0xd4000, 0xd6000,
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000051 0xd8000, 0xda000, 0xdc000, 0xde000,
52 0xe0000, 0xe2000, 0xe4000, 0xe6000,
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 0xe8000, 0xea000, 0xec000, 0xee000,
Michael Opdenacker9d403492013-07-08 06:39:20 +020054#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#endif
56 0xffffffff };
57
58static struct mtd_info *doclist = NULL;
59
60struct doc_priv {
61 void __iomem *virtadr;
62 unsigned long physadr;
63 u_char ChipID;
64 u_char CDSNControl;
David Woodhousee0c7d762006-05-13 18:07:53 +010065 int chips_per_floor; /* The number of chips detected on each floor */
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 int curfloor;
67 int curchip;
68 int mh0_page;
69 int mh1_page;
Thomas Gleixner964dfce2018-04-22 18:23:54 +020070 struct rs_control *rs_decoder;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 struct mtd_info *nextdoc;
Brian Norrisd24fe0c2015-02-28 02:13:10 -080072
73 /* Handle the last stage of initialization (BBT scan, partitioning) */
74 int (*late_init)(struct mtd_info *mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075};
76
Linus Torvalds1da177e2005-04-16 15:20:36 -070077/* This is the ecc value computed by the HW ecc generator upon writing an empty
78 page, one with all 0xff for data. */
79static u_char empty_write_ecc[6] = { 0x4b, 0x00, 0xe2, 0x0e, 0x93, 0xf7 };
80
81#define INFTL_BBT_RESERVED_BLOCKS 4
82
83#define DoC_is_MillenniumPlus(doc) ((doc)->ChipID == DOC_ChipID_DocMilPlus16 || (doc)->ChipID == DOC_ChipID_DocMilPlus32)
84#define DoC_is_Millennium(doc) ((doc)->ChipID == DOC_ChipID_DocMil)
85#define DoC_is_2000(doc) ((doc)->ChipID == DOC_ChipID_Doc2k)
86
Boris Brezillon0f808c12018-09-06 14:05:26 +020087static void doc200x_hwcontrol(struct nand_chip *this, int cmd,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +020088 unsigned int bitmask);
Boris Brezillon758b56f2018-09-06 14:05:24 +020089static void doc200x_select_chip(struct nand_chip *this, int chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
David Woodhousee0c7d762006-05-13 18:07:53 +010091static int debug = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092module_param(debug, int, 0);
93
David Woodhousee0c7d762006-05-13 18:07:53 +010094static int try_dword = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095module_param(try_dword, int, 0);
96
David Woodhousee0c7d762006-05-13 18:07:53 +010097static int no_ecc_failures = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098module_param(no_ecc_failures, int, 0);
99
David Woodhousee0c7d762006-05-13 18:07:53 +0100100static int no_autopart = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101module_param(no_autopart, int, 0);
Dan Brown1a78ff62005-03-29 21:57:48 +0100102
David Woodhousee0c7d762006-05-13 18:07:53 +0100103static int show_firmware_partition = 0;
Dan Brown1a78ff62005-03-29 21:57:48 +0100104module_param(show_firmware_partition, int, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
Robert P. J. Day89e2bf62007-03-07 18:33:25 -0500106#ifdef CONFIG_MTD_NAND_DISKONCHIP_BBTWRITE
David Woodhousee0c7d762006-05-13 18:07:53 +0100107static int inftl_bbt_write = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108#else
David Woodhousee0c7d762006-05-13 18:07:53 +0100109static int inftl_bbt_write = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110#endif
111module_param(inftl_bbt_write, int, 0);
112
Thomas Gleixner651078b2005-01-31 20:36:46 +0000113static unsigned long doc_config_location = CONFIG_MTD_NAND_DISKONCHIP_PROBE_ADDRESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114module_param(doc_config_location, ulong, 0);
115MODULE_PARM_DESC(doc_config_location, "Physical memory address at which to probe for DiskOnChip");
116
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117/* Sector size for HW ECC */
118#define SECTOR_SIZE 512
119/* The sector bytes are packed into NB_DATA 10 bit words */
120#define NB_DATA (((SECTOR_SIZE + 1) * 8 + 6) / 10)
121/* Number of roots */
122#define NROOTS 4
123/* First consective root */
124#define FCR 510
125/* Number of symbols */
126#define NN 1023
127
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000128/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 * The HW decoder in the DoC ASIC's provides us a error syndrome,
Brian Norris7854d3f2011-06-23 14:12:08 -0700130 * which we must convert to a standard syndrome usable by the generic
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 * Reed-Solomon library code.
132 *
133 * Fabrice Bellard figured this out in the old docecc code. I added
134 * some comments, improved a minor bit and converted it to make use
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300135 * of the generic Reed-Solomon library. tglx
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100137static int doc_ecc_decode(struct rs_control *rs, uint8_t *data, uint8_t *ecc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138{
139 int i, j, nerr, errpos[8];
140 uint8_t parity;
141 uint16_t ds[4], s[5], tmp, errval[8], syn[4];
Thomas Gleixner21633982018-04-22 18:23:53 +0200142 struct rs_codec *cd = rs->codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
Mark Warec9fb6772010-05-27 11:45:39 +1000144 memset(syn, 0, sizeof(syn));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 /* Convert the ecc bytes into words */
146 ds[0] = ((ecc[4] & 0xff) >> 0) | ((ecc[5] & 0x03) << 8);
147 ds[1] = ((ecc[5] & 0xfc) >> 2) | ((ecc[2] & 0x0f) << 6);
148 ds[2] = ((ecc[2] & 0xf0) >> 4) | ((ecc[3] & 0x3f) << 4);
149 ds[3] = ((ecc[3] & 0xc0) >> 6) | ((ecc[0] & 0xff) << 2);
150 parity = ecc[1];
151
Brian Norris7854d3f2011-06-23 14:12:08 -0700152 /* Initialize the syndrome buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 for (i = 0; i < NROOTS; i++)
154 s[i] = ds[0];
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000155 /*
156 * Evaluate
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 * s[i] = ds[3]x^3 + ds[2]x^2 + ds[1]x^1 + ds[0]
158 * where x = alpha^(FCR + i)
159 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100160 for (j = 1; j < NROOTS; j++) {
161 if (ds[j] == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 continue;
Thomas Gleixner21633982018-04-22 18:23:53 +0200163 tmp = cd->index_of[ds[j]];
David Woodhousee0c7d762006-05-13 18:07:53 +0100164 for (i = 0; i < NROOTS; i++)
Thomas Gleixner21633982018-04-22 18:23:53 +0200165 s[i] ^= cd->alpha_to[rs_modnn(cd, tmp + (FCR + i) * j)];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 }
167
Mark Warec9fb6772010-05-27 11:45:39 +1000168 /* Calc syn[i] = s[i] / alpha^(v + i) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 for (i = 0; i < NROOTS; i++) {
Mark Warec9fb6772010-05-27 11:45:39 +1000170 if (s[i])
Thomas Gleixner21633982018-04-22 18:23:53 +0200171 syn[i] = rs_modnn(cd, cd->index_of[s[i]] + (NN - FCR - i));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 }
173 /* Call the decoder library */
174 nerr = decode_rs16(rs, NULL, NULL, 1019, syn, 0, errpos, 0, errval);
175
176 /* Incorrectable errors ? */
177 if (nerr < 0)
178 return nerr;
179
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000180 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 * Correct the errors. The bitpositions are a bit of magic,
182 * but they are given by the design of the de/encoder circuit
183 * in the DoC ASIC's.
184 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100185 for (i = 0; i < nerr; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 int index, bitpos, pos = 1015 - errpos[i];
187 uint8_t val;
188 if (pos >= NB_DATA && pos < 1019)
189 continue;
190 if (pos < NB_DATA) {
191 /* extract bit position (MSB first) */
192 pos = 10 * (NB_DATA - 1 - pos) - 6;
193 /* now correct the following 10 bits. At most two bytes
194 can be modified since pos is even */
195 index = (pos >> 3) ^ 1;
196 bitpos = pos & 7;
David Woodhousee0c7d762006-05-13 18:07:53 +0100197 if ((index >= 0 && index < SECTOR_SIZE) || index == (SECTOR_SIZE + 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 val = (uint8_t) (errval[i] >> (2 + bitpos));
199 parity ^= val;
200 if (index < SECTOR_SIZE)
201 data[index] ^= val;
202 }
203 index = ((pos >> 3) + 1) ^ 1;
204 bitpos = (bitpos + 10) & 7;
205 if (bitpos == 0)
206 bitpos = 8;
David Woodhousee0c7d762006-05-13 18:07:53 +0100207 if ((index >= 0 && index < SECTOR_SIZE) || index == (SECTOR_SIZE + 1)) {
208 val = (uint8_t) (errval[i] << (8 - bitpos));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 parity ^= val;
210 if (index < SECTOR_SIZE)
211 data[index] ^= val;
212 }
213 }
214 }
215 /* If the parity is wrong, no rescue possible */
Jörn Engeleb684502007-10-20 23:16:32 +0200216 return parity ? -EBADMSG : nerr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217}
218
219static void DoC_Delay(struct doc_priv *doc, unsigned short cycles)
220{
221 volatile char dummy;
222 int i;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000223
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 for (i = 0; i < cycles; i++) {
225 if (DoC_is_Millennium(doc))
226 dummy = ReadDOC(doc->virtadr, NOP);
227 else if (DoC_is_MillenniumPlus(doc))
228 dummy = ReadDOC(doc->virtadr, Mplus_NOP);
229 else
230 dummy = ReadDOC(doc->virtadr, DOCStatus);
231 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000232
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233}
234
235#define CDSN_CTRL_FR_B_MASK (CDSN_CTRL_FR_B0 | CDSN_CTRL_FR_B1)
236
237/* DOC_WaitReady: Wait for RDY line to be asserted by the flash chip */
238static int _DoC_WaitReady(struct doc_priv *doc)
239{
David Woodhousee0c7d762006-05-13 18:07:53 +0100240 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 unsigned long timeo = jiffies + (HZ * 10);
242
David Woodhousee0c7d762006-05-13 18:07:53 +0100243 if (debug)
244 printk("_DoC_WaitReady...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 /* Out-of-line routine to wait for chip response */
246 if (DoC_is_MillenniumPlus(doc)) {
247 while ((ReadDOC(docptr, Mplus_FlashControl) & CDSN_CTRL_FR_B_MASK) != CDSN_CTRL_FR_B_MASK) {
248 if (time_after(jiffies, timeo)) {
249 printk("_DoC_WaitReady timed out.\n");
250 return -EIO;
251 }
252 udelay(1);
253 cond_resched();
254 }
255 } else {
256 while (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B)) {
257 if (time_after(jiffies, timeo)) {
258 printk("_DoC_WaitReady timed out.\n");
259 return -EIO;
260 }
261 udelay(1);
262 cond_resched();
263 }
264 }
265
266 return 0;
267}
268
269static inline int DoC_WaitReady(struct doc_priv *doc)
270{
David Woodhousee0c7d762006-05-13 18:07:53 +0100271 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 int ret = 0;
273
274 if (DoC_is_MillenniumPlus(doc)) {
275 DoC_Delay(doc, 4);
276
277 if ((ReadDOC(docptr, Mplus_FlashControl) & CDSN_CTRL_FR_B_MASK) != CDSN_CTRL_FR_B_MASK)
278 /* Call the out-of-line routine to wait */
279 ret = _DoC_WaitReady(doc);
280 } else {
281 DoC_Delay(doc, 4);
282
283 if (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B))
284 /* Call the out-of-line routine to wait */
285 ret = _DoC_WaitReady(doc);
286 DoC_Delay(doc, 2);
287 }
288
David Woodhousee0c7d762006-05-13 18:07:53 +0100289 if (debug)
290 printk("DoC_WaitReady OK\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 return ret;
292}
293
Boris Brezillonc0739d82018-09-06 14:05:23 +0200294static void doc2000_write_byte(struct nand_chip *this, u_char datum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295{
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
Boris Brezillon7e534322018-09-06 14:05:22 +0200305static u_char doc2000_read_byte(struct nand_chip *this)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306{
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100307 struct doc_priv *doc = nand_get_controller_data(this);
David Woodhousee0c7d762006-05-13 18:07:53 +0100308 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 u_char ret;
310
311 ReadDOC(docptr, CDSNSlowIO);
312 DoC_Delay(doc, 2);
313 ret = ReadDOC(docptr, 2k_CDSN_IO);
David Woodhousee0c7d762006-05-13 18:07:53 +0100314 if (debug)
315 printk("read_byte returns %02x\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 return ret;
317}
318
Boris Brezillonc0739d82018-09-06 14:05:23 +0200319static void doc2000_writebuf(struct nand_chip *this, const u_char *buf,
320 int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321{
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100322 struct doc_priv *doc = nand_get_controller_data(this);
David Woodhousee0c7d762006-05-13 18:07:53 +0100323 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 int i;
David Woodhousee0c7d762006-05-13 18:07:53 +0100325 if (debug)
326 printk("writebuf of %d bytes: ", len);
327 for (i = 0; i < len; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 WriteDOC_(buf[i], docptr, DoC_2k_CDSN_IO + i);
329 if (debug && i < 16)
330 printk("%02x ", buf[i]);
331 }
David Woodhousee0c7d762006-05-13 18:07:53 +0100332 if (debug)
333 printk("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334}
335
Boris Brezillon7e534322018-09-06 14:05:22 +0200336static void doc2000_readbuf(struct nand_chip *this, u_char *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337{
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100338 struct doc_priv *doc = nand_get_controller_data(this);
David Woodhousee0c7d762006-05-13 18:07:53 +0100339 void __iomem *docptr = doc->virtadr;
340 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
David Woodhousee0c7d762006-05-13 18:07:53 +0100342 if (debug)
343 printk("readbuf of %d bytes: ", len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
Boris Brezillon7e534322018-09-06 14:05:22 +0200345 for (i = 0; i < len; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 buf[i] = ReadDOC(docptr, 2k_CDSN_IO + i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347}
348
Boris Brezillon7e534322018-09-06 14:05:22 +0200349static void doc2000_readbuf_dword(struct nand_chip *this, u_char *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350{
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100351 struct doc_priv *doc = nand_get_controller_data(this);
David Woodhousee0c7d762006-05-13 18:07:53 +0100352 void __iomem *docptr = doc->virtadr;
353 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
David Woodhousee0c7d762006-05-13 18:07:53 +0100355 if (debug)
356 printk("readbuf_dword of %d bytes: ", len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
David Woodhousee0c7d762006-05-13 18:07:53 +0100358 if (unlikely((((unsigned long)buf) | len) & 3)) {
359 for (i = 0; i < len; i++) {
360 *(uint8_t *) (&buf[i]) = ReadDOC(docptr, 2k_CDSN_IO + i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 }
362 } else {
David Woodhousee0c7d762006-05-13 18:07:53 +0100363 for (i = 0; i < len; i += 4) {
364 *(uint32_t *) (&buf[i]) = readl(docptr + DoC_2k_CDSN_IO + i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 }
366 }
367}
368
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369static uint16_t __init doc200x_ident_chip(struct mtd_info *mtd, int nr)
370{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100371 struct nand_chip *this = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100372 struct doc_priv *doc = nand_get_controller_data(this);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 uint16_t ret;
374
Boris Brezillon758b56f2018-09-06 14:05:24 +0200375 doc200x_select_chip(this, nr);
Boris Brezillon0f808c12018-09-06 14:05:26 +0200376 doc200x_hwcontrol(this, NAND_CMD_READID,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200377 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
Boris Brezillon0f808c12018-09-06 14:05:26 +0200378 doc200x_hwcontrol(this, 0, NAND_CTRL_ALE | NAND_CTRL_CHANGE);
379 doc200x_hwcontrol(this, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000380
Lucas De Marchie9c54992011-04-26 23:28:26 -0700381 /* We can't use dev_ready here, but at least we wait for the
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000382 * command to complete
Thomas Gleixnerdfd61292005-02-22 21:48:25 +0000383 */
384 udelay(50);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000385
Boris Brezillon716bbba2018-09-07 00:38:35 +0200386 ret = this->legacy.read_byte(this) << 8;
387 ret |= this->legacy.read_byte(this);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
389 if (doc->ChipID == DOC_ChipID_Doc2k && try_dword && !nr) {
390 /* First chip probe. See if we get same results by 32-bit access */
391 union {
392 uint32_t dword;
393 uint8_t byte[4];
394 } ident;
395 void __iomem *docptr = doc->virtadr;
396
Boris Brezillon0f808c12018-09-06 14:05:26 +0200397 doc200x_hwcontrol(this, NAND_CMD_READID,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200398 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
Boris Brezillon0f808c12018-09-06 14:05:26 +0200399 doc200x_hwcontrol(this, 0, NAND_CTRL_ALE | NAND_CTRL_CHANGE);
400 doc200x_hwcontrol(this, NAND_CMD_NONE,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200401 NAND_NCE | NAND_CTRL_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
Thomas Gleixnerdfd61292005-02-22 21:48:25 +0000403 udelay(50);
404
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 ident.dword = readl(docptr + DoC_2k_CDSN_IO);
406 if (((ident.byte[0] << 8) | ident.byte[1]) == ret) {
Shreeya Patel63fa37f2018-02-22 22:01:22 +0530407 pr_info("DiskOnChip 2000 responds to DWORD access\n");
Boris Brezillon716bbba2018-09-07 00:38:35 +0200408 this->legacy.read_buf = &doc2000_readbuf_dword;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 }
410 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000411
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 return ret;
413}
414
415static void __init doc2000_count_chips(struct mtd_info *mtd)
416{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100417 struct nand_chip *this = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100418 struct doc_priv *doc = nand_get_controller_data(this);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 uint16_t mfrid;
420 int i;
421
422 /* Max 4 chips per floor on DiskOnChip 2000 */
423 doc->chips_per_floor = 4;
424
425 /* Find out what the first chip is */
426 mfrid = doc200x_ident_chip(mtd, 0);
427
428 /* Find how many chips in each floor. */
429 for (i = 1; i < 4; i++) {
430 if (doc200x_ident_chip(mtd, i) != mfrid)
431 break;
432 }
433 doc->chips_per_floor = i;
Shreeya Patel63fa37f2018-02-22 22:01:22 +0530434 pr_debug("Detected %d chips per floor.\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435}
436
Boris Brezillonf1d46942018-09-06 14:05:29 +0200437static int doc200x_wait(struct nand_chip *this)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438{
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100439 struct doc_priv *doc = nand_get_controller_data(this);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
441 int status;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000442
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 DoC_WaitReady(doc);
Boris Brezillon97d90da2017-11-30 18:01:29 +0100444 nand_status_op(this, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 DoC_WaitReady(doc);
Boris Brezillon716bbba2018-09-07 00:38:35 +0200446 status = (int)this->legacy.read_byte(this);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
448 return status;
449}
450
Boris Brezillonc0739d82018-09-06 14:05:23 +0200451static void doc2001_write_byte(struct nand_chip *this, u_char datum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452{
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100453 struct doc_priv *doc = nand_get_controller_data(this);
David Woodhousee0c7d762006-05-13 18:07:53 +0100454 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
456 WriteDOC(datum, docptr, CDSNSlowIO);
457 WriteDOC(datum, docptr, Mil_CDSN_IO);
458 WriteDOC(datum, docptr, WritePipeTerm);
459}
460
Boris Brezillon7e534322018-09-06 14:05:22 +0200461static u_char doc2001_read_byte(struct nand_chip *this)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462{
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100463 struct doc_priv *doc = nand_get_controller_data(this);
David Woodhousee0c7d762006-05-13 18:07:53 +0100464 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
466 //ReadDOC(docptr, CDSNSlowIO);
467 /* 11.4.5 -- delay twice to allow extended length cycle */
468 DoC_Delay(doc, 2);
469 ReadDOC(docptr, ReadPipeInit);
470 //return ReadDOC(docptr, Mil_CDSN_IO);
471 return ReadDOC(docptr, LastDataRead);
472}
473
Boris Brezillonc0739d82018-09-06 14:05:23 +0200474static void doc2001_writebuf(struct nand_chip *this, const u_char *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475{
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100476 struct doc_priv *doc = nand_get_controller_data(this);
David Woodhousee0c7d762006-05-13 18:07:53 +0100477 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 int i;
479
David Woodhousee0c7d762006-05-13 18:07:53 +0100480 for (i = 0; i < len; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 WriteDOC_(buf[i], docptr, DoC_Mil_CDSN_IO + i);
482 /* Terminate write pipeline */
483 WriteDOC(0x00, docptr, WritePipeTerm);
484}
485
Boris Brezillon7e534322018-09-06 14:05:22 +0200486static void doc2001_readbuf(struct nand_chip *this, u_char *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487{
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100488 struct doc_priv *doc = nand_get_controller_data(this);
David Woodhousee0c7d762006-05-13 18:07:53 +0100489 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 int i;
491
492 /* Start read pipeline */
493 ReadDOC(docptr, ReadPipeInit);
494
David Woodhousee0c7d762006-05-13 18:07:53 +0100495 for (i = 0; i < len - 1; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 buf[i] = ReadDOC(docptr, Mil_CDSN_IO + (i & 0xff));
497
498 /* Terminate read pipeline */
499 buf[i] = ReadDOC(docptr, LastDataRead);
500}
501
Boris Brezillon7e534322018-09-06 14:05:22 +0200502static u_char doc2001plus_read_byte(struct nand_chip *this)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503{
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100504 struct doc_priv *doc = nand_get_controller_data(this);
David Woodhousee0c7d762006-05-13 18:07:53 +0100505 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 u_char ret;
507
David Woodhousee0c7d762006-05-13 18:07:53 +0100508 ReadDOC(docptr, Mplus_ReadPipeInit);
509 ReadDOC(docptr, Mplus_ReadPipeInit);
510 ret = ReadDOC(docptr, Mplus_LastDataRead);
511 if (debug)
512 printk("read_byte returns %02x\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 return ret;
514}
515
Boris Brezillonc0739d82018-09-06 14:05:23 +0200516static void doc2001plus_writebuf(struct nand_chip *this, const u_char *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517{
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100518 struct doc_priv *doc = nand_get_controller_data(this);
David Woodhousee0c7d762006-05-13 18:07:53 +0100519 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 int i;
521
David Woodhousee0c7d762006-05-13 18:07:53 +0100522 if (debug)
523 printk("writebuf of %d bytes: ", len);
524 for (i = 0; i < len; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 WriteDOC_(buf[i], docptr, DoC_Mil_CDSN_IO + i);
526 if (debug && i < 16)
527 printk("%02x ", buf[i]);
528 }
David Woodhousee0c7d762006-05-13 18:07:53 +0100529 if (debug)
530 printk("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531}
532
Boris Brezillon7e534322018-09-06 14:05:22 +0200533static void doc2001plus_readbuf(struct nand_chip *this, u_char *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534{
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100535 struct doc_priv *doc = nand_get_controller_data(this);
David Woodhousee0c7d762006-05-13 18:07:53 +0100536 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 int i;
538
David Woodhousee0c7d762006-05-13 18:07:53 +0100539 if (debug)
540 printk("readbuf of %d bytes: ", len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
542 /* Start read pipeline */
543 ReadDOC(docptr, Mplus_ReadPipeInit);
544 ReadDOC(docptr, Mplus_ReadPipeInit);
545
David Woodhousee0c7d762006-05-13 18:07:53 +0100546 for (i = 0; i < len - 2; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 buf[i] = ReadDOC(docptr, Mil_CDSN_IO);
548 if (debug && i < 16)
549 printk("%02x ", buf[i]);
550 }
551
552 /* Terminate read pipeline */
David Woodhousee0c7d762006-05-13 18:07:53 +0100553 buf[len - 2] = ReadDOC(docptr, Mplus_LastDataRead);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 if (debug && i < 16)
David Woodhousee0c7d762006-05-13 18:07:53 +0100555 printk("%02x ", buf[len - 2]);
556 buf[len - 1] = ReadDOC(docptr, Mplus_LastDataRead);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 if (debug && i < 16)
David Woodhousee0c7d762006-05-13 18:07:53 +0100558 printk("%02x ", buf[len - 1]);
559 if (debug)
560 printk("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561}
562
Boris Brezillon758b56f2018-09-06 14:05:24 +0200563static void doc2001plus_select_chip(struct nand_chip *this, int chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564{
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100565 struct doc_priv *doc = nand_get_controller_data(this);
David Woodhousee0c7d762006-05-13 18:07:53 +0100566 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 int floor = 0;
568
David Woodhousee0c7d762006-05-13 18:07:53 +0100569 if (debug)
570 printk("select chip (%d)\n", chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
572 if (chip == -1) {
573 /* Disable flash internally */
574 WriteDOC(0, docptr, Mplus_FlashSelect);
575 return;
576 }
577
578 floor = chip / doc->chips_per_floor;
David Woodhousee0c7d762006-05-13 18:07:53 +0100579 chip -= (floor * doc->chips_per_floor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
581 /* Assert ChipEnable and deassert WriteProtect */
582 WriteDOC((DOC_FLASH_CE), docptr, Mplus_FlashSelect);
Boris Brezillon97d90da2017-11-30 18:01:29 +0100583 nand_reset_op(this);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
585 doc->curchip = chip;
586 doc->curfloor = floor;
587}
588
Boris Brezillon758b56f2018-09-06 14:05:24 +0200589static void doc200x_select_chip(struct nand_chip *this, int chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590{
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100591 struct doc_priv *doc = nand_get_controller_data(this);
David Woodhousee0c7d762006-05-13 18:07:53 +0100592 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 int floor = 0;
594
David Woodhousee0c7d762006-05-13 18:07:53 +0100595 if (debug)
596 printk("select chip (%d)\n", chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
598 if (chip == -1)
599 return;
600
601 floor = chip / doc->chips_per_floor;
David Woodhousee0c7d762006-05-13 18:07:53 +0100602 chip -= (floor * doc->chips_per_floor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
604 /* 11.4.4 -- deassert CE before changing chip */
Boris Brezillon0f808c12018-09-06 14:05:26 +0200605 doc200x_hwcontrol(this, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
607 WriteDOC(floor, docptr, FloorSelect);
608 WriteDOC(chip, docptr, CDSNDeviceSelect);
609
Boris Brezillon0f808c12018-09-06 14:05:26 +0200610 doc200x_hwcontrol(this, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
612 doc->curchip = chip;
613 doc->curfloor = floor;
614}
615
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200616#define CDSN_CTRL_MSK (CDSN_CTRL_CE | CDSN_CTRL_CLE | CDSN_CTRL_ALE)
617
Boris Brezillon0f808c12018-09-06 14:05:26 +0200618static void doc200x_hwcontrol(struct nand_chip *this, int cmd,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200619 unsigned int ctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620{
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100621 struct doc_priv *doc = nand_get_controller_data(this);
David Woodhousee0c7d762006-05-13 18:07:53 +0100622 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200624 if (ctrl & NAND_CTRL_CHANGE) {
625 doc->CDSNControl &= ~CDSN_CTRL_MSK;
626 doc->CDSNControl |= ctrl & CDSN_CTRL_MSK;
627 if (debug)
628 printk("hwcontrol(%d): %02x\n", cmd, doc->CDSNControl);
629 WriteDOC(doc->CDSNControl, docptr, CDSNControl);
630 /* 11.4.3 -- 4 NOPs after CSDNControl write */
631 DoC_Delay(doc, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 }
Thomas Gleixnercad74f22006-05-23 23:28:48 +0200633 if (cmd != NAND_CMD_NONE) {
634 if (DoC_is_2000(doc))
Boris Brezillonc0739d82018-09-06 14:05:23 +0200635 doc2000_write_byte(this, cmd);
Thomas Gleixnercad74f22006-05-23 23:28:48 +0200636 else
Boris Brezillonc0739d82018-09-06 14:05:23 +0200637 doc2001_write_byte(this, cmd);
Thomas Gleixnercad74f22006-05-23 23:28:48 +0200638 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639}
640
Boris Brezillon5295cf22018-09-06 14:05:28 +0200641static void doc2001plus_command(struct nand_chip *this, unsigned command,
642 int column, int page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643{
Boris Brezillon5295cf22018-09-06 14:05:28 +0200644 struct mtd_info *mtd = nand_to_mtd(this);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100645 struct doc_priv *doc = nand_get_controller_data(this);
David Woodhousee0c7d762006-05-13 18:07:53 +0100646 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
648 /*
649 * Must terminate write pipeline before sending any commands
650 * to the device.
651 */
652 if (command == NAND_CMD_PAGEPROG) {
653 WriteDOC(0x00, docptr, Mplus_WritePipeTerm);
654 WriteDOC(0x00, docptr, Mplus_WritePipeTerm);
655 }
656
657 /*
658 * Write out the command to the device.
659 */
660 if (command == NAND_CMD_SEQIN) {
661 int readcmd;
662
Joern Engel28318772006-05-22 23:18:05 +0200663 if (column >= mtd->writesize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 /* OOB area */
Joern Engel28318772006-05-22 23:18:05 +0200665 column -= mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 readcmd = NAND_CMD_READOOB;
667 } else if (column < 256) {
668 /* First 256 bytes --> READ0 */
669 readcmd = NAND_CMD_READ0;
670 } else {
671 column -= 256;
672 readcmd = NAND_CMD_READ1;
673 }
674 WriteDOC(readcmd, docptr, Mplus_FlashCmd);
675 }
676 WriteDOC(command, docptr, Mplus_FlashCmd);
677 WriteDOC(0, docptr, Mplus_WritePipeTerm);
678 WriteDOC(0, docptr, Mplus_WritePipeTerm);
679
680 if (column != -1 || page_addr != -1) {
681 /* Serially input address */
682 if (column != -1) {
683 /* Adjust columns for 16 bit buswidth */
Brian Norris3dad2342014-01-29 14:08:12 -0800684 if (this->options & NAND_BUSWIDTH_16 &&
685 !nand_opcode_8bits(command))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 column >>= 1;
687 WriteDOC(column, docptr, Mplus_FlashAddress);
688 }
689 if (page_addr != -1) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100690 WriteDOC((unsigned char)(page_addr & 0xff), docptr, Mplus_FlashAddress);
691 WriteDOC((unsigned char)((page_addr >> 8) & 0xff), docptr, Mplus_FlashAddress);
Masahiro Yamada14157f82017-09-13 11:05:50 +0900692 if (this->options & NAND_ROW_ADDR_3) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100693 WriteDOC((unsigned char)((page_addr >> 16) & 0x0f), docptr, Mplus_FlashAddress);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 printk("high density\n");
695 }
696 }
697 WriteDOC(0, docptr, Mplus_WritePipeTerm);
698 WriteDOC(0, docptr, Mplus_WritePipeTerm);
699 /* deassert ALE */
David Woodhousee0c7d762006-05-13 18:07:53 +0100700 if (command == NAND_CMD_READ0 || command == NAND_CMD_READ1 ||
701 command == NAND_CMD_READOOB || command == NAND_CMD_READID)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 WriteDOC(0, docptr, Mplus_FlashControl);
703 }
704
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000705 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 * program and erase have their own busy handlers
707 * status and sequential in needs no delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100708 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 switch (command) {
710
711 case NAND_CMD_PAGEPROG:
712 case NAND_CMD_ERASE1:
713 case NAND_CMD_ERASE2:
714 case NAND_CMD_SEQIN:
715 case NAND_CMD_STATUS:
716 return;
717
718 case NAND_CMD_RESET:
Boris Brezillon8395b752018-09-07 00:38:37 +0200719 if (this->legacy.dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 break;
Boris Brezillon3cece3a2018-09-07 00:38:41 +0200721 udelay(this->legacy.chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 WriteDOC(NAND_CMD_STATUS, docptr, Mplus_FlashCmd);
723 WriteDOC(0, docptr, Mplus_WritePipeTerm);
724 WriteDOC(0, docptr, Mplus_WritePipeTerm);
Boris Brezillon716bbba2018-09-07 00:38:35 +0200725 while (!(this->legacy.read_byte(this) & 0x40)) ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 return;
727
David Woodhousee0c7d762006-05-13 18:07:53 +0100728 /* This applies to read commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000730 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 * If we don't have access to the busy pin, we apply the given
732 * command delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100733 */
Boris Brezillon8395b752018-09-07 00:38:37 +0200734 if (!this->legacy.dev_ready) {
Boris Brezillon3cece3a2018-09-07 00:38:41 +0200735 udelay(this->legacy.chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 return;
737 }
738 }
739
740 /* Apply this short delay always to ensure that we do wait tWB in
741 * any case on any machine. */
David Woodhousee0c7d762006-05-13 18:07:53 +0100742 ndelay(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 /* wait until command is processed */
Boris Brezillon8395b752018-09-07 00:38:37 +0200744 while (!this->legacy.dev_ready(this)) ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745}
746
Boris Brezillon50a487e2018-09-06 14:05:27 +0200747static int doc200x_dev_ready(struct nand_chip *this)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748{
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100749 struct doc_priv *doc = nand_get_controller_data(this);
David Woodhousee0c7d762006-05-13 18:07:53 +0100750 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
752 if (DoC_is_MillenniumPlus(doc)) {
753 /* 11.4.2 -- must NOP four times before checking FR/B# */
754 DoC_Delay(doc, 4);
755 if ((ReadDOC(docptr, Mplus_FlashControl) & CDSN_CTRL_FR_B_MASK) != CDSN_CTRL_FR_B_MASK) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100756 if (debug)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 printk("not ready\n");
758 return 0;
759 }
David Woodhousee0c7d762006-05-13 18:07:53 +0100760 if (debug)
761 printk("was ready\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 return 1;
763 } else {
764 /* 11.4.2 -- must NOP four times before checking FR/B# */
765 DoC_Delay(doc, 4);
766 if (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B)) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100767 if (debug)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 printk("not ready\n");
769 return 0;
770 }
771 /* 11.4.2 -- Must NOP twice if it's ready */
772 DoC_Delay(doc, 2);
David Woodhousee0c7d762006-05-13 18:07:53 +0100773 if (debug)
774 printk("was ready\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 return 1;
776 }
777}
778
Boris Brezillonc17556f2018-09-06 14:05:25 +0200779static int doc200x_block_bad(struct nand_chip *this, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780{
781 /* This is our last resort if we couldn't find or create a BBT. Just
782 pretend all blocks are good. */
783 return 0;
784}
785
Boris Brezillonec476362018-09-06 14:05:17 +0200786static void doc200x_enable_hwecc(struct nand_chip *this, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787{
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100788 struct doc_priv *doc = nand_get_controller_data(this);
David Woodhousee0c7d762006-05-13 18:07:53 +0100789 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
791 /* Prime the ECC engine */
David Woodhousee0c7d762006-05-13 18:07:53 +0100792 switch (mode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 case NAND_ECC_READ:
794 WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
795 WriteDOC(DOC_ECC_EN, docptr, ECCConf);
796 break;
797 case NAND_ECC_WRITE:
798 WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
799 WriteDOC(DOC_ECC_EN | DOC_ECC_RW, docptr, ECCConf);
800 break;
801 }
802}
803
Boris Brezillonec476362018-09-06 14:05:17 +0200804static void doc2001plus_enable_hwecc(struct nand_chip *this, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805{
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100806 struct doc_priv *doc = nand_get_controller_data(this);
David Woodhousee0c7d762006-05-13 18:07:53 +0100807 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
809 /* Prime the ECC engine */
David Woodhousee0c7d762006-05-13 18:07:53 +0100810 switch (mode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 case NAND_ECC_READ:
812 WriteDOC(DOC_ECC_RESET, docptr, Mplus_ECCConf);
813 WriteDOC(DOC_ECC_EN, docptr, Mplus_ECCConf);
814 break;
815 case NAND_ECC_WRITE:
816 WriteDOC(DOC_ECC_RESET, docptr, Mplus_ECCConf);
817 WriteDOC(DOC_ECC_EN | DOC_ECC_RW, docptr, Mplus_ECCConf);
818 break;
819 }
820}
821
822/* This code is only called on write */
Boris Brezillonaf37d2c2018-09-06 14:05:18 +0200823static int doc200x_calculate_ecc(struct nand_chip *this, const u_char *dat,
824 unsigned char *ecc_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825{
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100826 struct doc_priv *doc = nand_get_controller_data(this);
David Woodhousee0c7d762006-05-13 18:07:53 +0100827 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 int i;
829 int emptymatch = 1;
830
831 /* flush the pipeline */
832 if (DoC_is_2000(doc)) {
833 WriteDOC(doc->CDSNControl & ~CDSN_CTRL_FLASH_IO, docptr, CDSNControl);
834 WriteDOC(0, docptr, 2k_CDSN_IO);
835 WriteDOC(0, docptr, 2k_CDSN_IO);
836 WriteDOC(0, docptr, 2k_CDSN_IO);
837 WriteDOC(doc->CDSNControl, docptr, CDSNControl);
838 } else if (DoC_is_MillenniumPlus(doc)) {
839 WriteDOC(0, docptr, Mplus_NOP);
840 WriteDOC(0, docptr, Mplus_NOP);
841 WriteDOC(0, docptr, Mplus_NOP);
842 } else {
843 WriteDOC(0, docptr, NOP);
844 WriteDOC(0, docptr, NOP);
845 WriteDOC(0, docptr, NOP);
846 }
847
848 for (i = 0; i < 6; i++) {
849 if (DoC_is_MillenniumPlus(doc))
850 ecc_code[i] = ReadDOC_(docptr, DoC_Mplus_ECCSyndrome0 + i);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000851 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 ecc_code[i] = ReadDOC_(docptr, DoC_ECCSyndrome0 + i);
853 if (ecc_code[i] != empty_write_ecc[i])
854 emptymatch = 0;
855 }
856 if (DoC_is_MillenniumPlus(doc))
857 WriteDOC(DOC_ECC_DIS, docptr, Mplus_ECCConf);
858 else
859 WriteDOC(DOC_ECC_DIS, docptr, ECCConf);
860#if 0
861 /* If emptymatch=1, we might have an all-0xff data buffer. Check. */
862 if (emptymatch) {
863 /* Note: this somewhat expensive test should not be triggered
864 often. It could be optimized away by examining the data in
865 the writebuf routine, and remembering the result. */
866 for (i = 0; i < 512; i++) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100867 if (dat[i] == 0xff)
868 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 emptymatch = 0;
870 break;
871 }
872 }
873 /* If emptymatch still =1, we do have an all-0xff data buffer.
874 Return all-0xff ecc value instead of the computed one, so
875 it'll look just like a freshly-erased page. */
David Woodhousee0c7d762006-05-13 18:07:53 +0100876 if (emptymatch)
877 memset(ecc_code, 0xff, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878#endif
879 return 0;
880}
881
Boris Brezillon00da2ea2018-09-06 14:05:19 +0200882static int doc200x_correct_data(struct nand_chip *this, u_char *dat,
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200883 u_char *read_ecc, u_char *isnull)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884{
885 int i, ret = 0;
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100886 struct doc_priv *doc = nand_get_controller_data(this);
David Woodhousee0c7d762006-05-13 18:07:53 +0100887 void __iomem *docptr = doc->virtadr;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200888 uint8_t calc_ecc[6];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 volatile u_char dummy;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000890
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 /* flush the pipeline */
892 if (DoC_is_2000(doc)) {
893 dummy = ReadDOC(docptr, 2k_ECCStatus);
894 dummy = ReadDOC(docptr, 2k_ECCStatus);
895 dummy = ReadDOC(docptr, 2k_ECCStatus);
896 } else if (DoC_is_MillenniumPlus(doc)) {
897 dummy = ReadDOC(docptr, Mplus_ECCConf);
898 dummy = ReadDOC(docptr, Mplus_ECCConf);
899 dummy = ReadDOC(docptr, Mplus_ECCConf);
900 } else {
901 dummy = ReadDOC(docptr, ECCConf);
902 dummy = ReadDOC(docptr, ECCConf);
903 dummy = ReadDOC(docptr, ECCConf);
904 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000905
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300906 /* Error occurred ? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 if (dummy & 0x80) {
908 for (i = 0; i < 6; i++) {
909 if (DoC_is_MillenniumPlus(doc))
910 calc_ecc[i] = ReadDOC_(docptr, DoC_Mplus_ECCSyndrome0 + i);
911 else
912 calc_ecc[i] = ReadDOC_(docptr, DoC_ECCSyndrome0 + i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 }
Boris BREZILLONcc01e602015-12-30 20:39:52 +0100914
Thomas Gleixner964dfce2018-04-22 18:23:54 +0200915 ret = doc_ecc_decode(doc->rs_decoder, dat, calc_ecc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 if (ret > 0)
Shreeya Patel63fa37f2018-02-22 22:01:22 +0530917 pr_err("doc200x_correct_data corrected %d errors\n",
918 ret);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000919 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 if (DoC_is_MillenniumPlus(doc))
921 WriteDOC(DOC_ECC_DIS, docptr, Mplus_ECCConf);
922 else
923 WriteDOC(DOC_ECC_DIS, docptr, ECCConf);
Brian Norrisd57f40542011-09-20 18:34:25 -0700924 if (no_ecc_failures && mtd_is_eccerr(ret)) {
Shreeya Patel63fa37f2018-02-22 22:01:22 +0530925 pr_err("suppressing ECC failure\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 ret = 0;
927 }
928 return ret;
929}
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000930
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931//u_char mydatabuf[528];
932
Boris Brezillon68c1b752016-02-03 20:00:23 +0100933static int doc200x_ooblayout_ecc(struct mtd_info *mtd, int section,
934 struct mtd_oob_region *oobregion)
935{
936 if (section)
937 return -ERANGE;
938
939 oobregion->offset = 0;
940 oobregion->length = 6;
941
942 return 0;
943}
944
945static int doc200x_ooblayout_free(struct mtd_info *mtd, int section,
946 struct mtd_oob_region *oobregion)
947{
948 if (section > 1)
949 return -ERANGE;
950
951 /*
952 * The strange out-of-order free bytes definition is a (possibly
953 * unneeded) attempt to retain compatibility. It used to read:
954 * .oobfree = { {8, 8} }
955 * Since that leaves two bytes unusable, it was changed. But the
956 * following scheme might affect existing jffs2 installs by moving the
957 * cleanmarker:
958 * .oobfree = { {6, 10} }
959 * jffs2 seems to handle the above gracefully, but the current scheme
960 * seems safer. The only problem with it is that any code retrieving
961 * free bytes position must be able to handle out-of-order segments.
962 */
963 if (!section) {
964 oobregion->offset = 8;
965 oobregion->length = 8;
966 } else {
967 oobregion->offset = 6;
968 oobregion->length = 2;
969 }
970
971 return 0;
972}
973
974static const struct mtd_ooblayout_ops doc200x_ooblayout_ops = {
975 .ecc = doc200x_ooblayout_ecc,
976 .free = doc200x_ooblayout_free,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977};
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000978
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979/* Find the (I)NFTL Media Header, and optionally also the mirror media header.
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200980 On successful return, buf will contain a copy of the media header for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 further processing. id is the string to scan for, and will presumably be
982 either "ANAND" or "BNAND". If findmirror=1, also look for the mirror media
983 header. The page #s of the found media headers are placed in mh0_page and
984 mh1_page in the DOC private structure. */
David Woodhousee0c7d762006-05-13 18:07:53 +0100985static int __init find_media_headers(struct mtd_info *mtd, u_char *buf, const char *id, int findmirror)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100987 struct nand_chip *this = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100988 struct doc_priv *doc = nand_get_controller_data(this);
Dan Brown1a78ff62005-03-29 21:57:48 +0100989 unsigned offs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 int ret;
991 size_t retlen;
992
Dan Brown1a78ff62005-03-29 21:57:48 +0100993 for (offs = 0; offs < mtd->size; offs += mtd->erasesize) {
Artem Bityutskiy329ad392011-12-23 17:30:16 +0200994 ret = mtd_read(mtd, offs, mtd->writesize, &retlen, buf);
Joern Engel28318772006-05-22 23:18:05 +0200995 if (retlen != mtd->writesize)
David Woodhousee0c7d762006-05-13 18:07:53 +0100996 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 if (ret) {
Shreeya Patel63fa37f2018-02-22 22:01:22 +0530998 pr_warn("ECC error scanning DOC at 0x%x\n", offs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 }
David Woodhousee0c7d762006-05-13 18:07:53 +01001000 if (memcmp(buf, id, 6))
1001 continue;
Shreeya Patel63fa37f2018-02-22 22:01:22 +05301002 pr_info("Found DiskOnChip %s Media Header at 0x%x\n", id, offs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 if (doc->mh0_page == -1) {
1004 doc->mh0_page = offs >> this->page_shift;
David Woodhousee0c7d762006-05-13 18:07:53 +01001005 if (!findmirror)
1006 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 continue;
1008 }
1009 doc->mh1_page = offs >> this->page_shift;
1010 return 2;
1011 }
1012 if (doc->mh0_page == -1) {
Shreeya Patel63fa37f2018-02-22 22:01:22 +05301013 pr_warn("DiskOnChip %s Media Header not found.\n", id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 return 0;
1015 }
1016 /* Only one mediaheader was found. We want buf to contain a
1017 mediaheader on return, so we'll have to re-read the one we found. */
1018 offs = doc->mh0_page << this->page_shift;
Artem Bityutskiy329ad392011-12-23 17:30:16 +02001019 ret = mtd_read(mtd, offs, mtd->writesize, &retlen, buf);
Joern Engel28318772006-05-22 23:18:05 +02001020 if (retlen != mtd->writesize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 /* Insanity. Give up. */
Shreeya Patel63fa37f2018-02-22 22:01:22 +05301022 pr_err("Read DiskOnChip Media Header once, but can't reread it???\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 return 0;
1024 }
1025 return 1;
1026}
1027
David Woodhousee0c7d762006-05-13 18:07:53 +01001028static inline int __init nftl_partscan(struct mtd_info *mtd, struct mtd_partition *parts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +01001030 struct nand_chip *this = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001031 struct doc_priv *doc = nand_get_controller_data(this);
Boris Brezillon629a4422018-10-25 17:10:37 +02001032 struct nand_memory_organization *memorg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 int ret = 0;
1034 u_char *buf;
1035 struct NFTLMediaHeader *mh;
1036 const unsigned psize = 1 << this->page_shift;
Dan Brown1a78ff62005-03-29 21:57:48 +01001037 int numparts = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 unsigned blocks, maxblocks;
1039 int offs, numheaders;
1040
Boris Brezillon629a4422018-10-25 17:10:37 +02001041 memorg = nanddev_get_memorg(&this->base);
1042
Joern Engel28318772006-05-22 23:18:05 +02001043 buf = kmalloc(mtd->writesize, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 if (!buf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 return 0;
1046 }
David Woodhousee0c7d762006-05-13 18:07:53 +01001047 if (!(numheaders = find_media_headers(mtd, buf, "ANAND", 1)))
1048 goto out;
1049 mh = (struct NFTLMediaHeader *)buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050
Harvey Harrison96372442008-07-30 12:34:57 -07001051 le16_to_cpus(&mh->NumEraseUnits);
1052 le16_to_cpus(&mh->FirstPhysicalEUN);
1053 le32_to_cpus(&mh->FormattedSize);
Thomas Gleixnerf29a4b82005-01-31 22:22:24 +00001054
Shreeya Patel63fa37f2018-02-22 22:01:22 +05301055 pr_info(" DataOrgID = %s\n"
1056 " NumEraseUnits = %d\n"
1057 " FirstPhysicalEUN = %d\n"
1058 " FormattedSize = %d\n"
1059 " UnitSizeFactor = %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 mh->DataOrgID, mh->NumEraseUnits,
1061 mh->FirstPhysicalEUN, mh->FormattedSize,
1062 mh->UnitSizeFactor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063
1064 blocks = mtd->size >> this->phys_erase_shift;
1065 maxblocks = min(32768U, mtd->erasesize - psize);
1066
1067 if (mh->UnitSizeFactor == 0x00) {
1068 /* Auto-determine UnitSizeFactor. The constraints are:
1069 - There can be at most 32768 virtual blocks.
1070 - There can be at most (virtual block size - page size)
David Woodhousee0c7d762006-05-13 18:07:53 +01001071 virtual blocks (because MediaHeader+BBT must fit in 1).
1072 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 mh->UnitSizeFactor = 0xff;
1074 while (blocks > maxblocks) {
1075 blocks >>= 1;
1076 maxblocks = min(32768U, (maxblocks << 1) + psize);
1077 mh->UnitSizeFactor--;
1078 }
Shreeya Patel63fa37f2018-02-22 22:01:22 +05301079 pr_warn("UnitSizeFactor=0x00 detected. Correct value is assumed to be 0x%02x.\n", mh->UnitSizeFactor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 }
1081
1082 /* NOTE: The lines below modify internal variables of the NAND and MTD
1083 layers; variables with have already been configured by nand_scan.
1084 Unfortunately, we didn't know before this point what these values
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001085 should be. Thus, this code is somewhat dependent on the exact
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 implementation of the NAND layer. */
1087 if (mh->UnitSizeFactor != 0xff) {
1088 this->bbt_erase_shift += (0xff - mh->UnitSizeFactor);
Boris Brezillon629a4422018-10-25 17:10:37 +02001089 memorg->pages_per_eraseblock <<= (0xff - mh->UnitSizeFactor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 mtd->erasesize <<= (0xff - mh->UnitSizeFactor);
Shreeya Patel63fa37f2018-02-22 22:01:22 +05301091 pr_info("Setting virtual erase size to %d\n", mtd->erasesize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 blocks = mtd->size >> this->bbt_erase_shift;
1093 maxblocks = min(32768U, mtd->erasesize - psize);
1094 }
1095
1096 if (blocks > maxblocks) {
Shreeya Patel63fa37f2018-02-22 22:01:22 +05301097 pr_err("UnitSizeFactor of 0x%02x is inconsistent with device size. Aborting.\n", mh->UnitSizeFactor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 goto out;
1099 }
1100
1101 /* Skip past the media headers. */
1102 offs = max(doc->mh0_page, doc->mh1_page);
1103 offs <<= this->page_shift;
1104 offs += mtd->erasesize;
1105
Dan Brown1a78ff62005-03-29 21:57:48 +01001106 if (show_firmware_partition == 1) {
1107 parts[0].name = " DiskOnChip Firmware / Media Header partition";
1108 parts[0].offset = 0;
1109 parts[0].size = offs;
1110 numparts = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 }
Dan Brown1a78ff62005-03-29 21:57:48 +01001112
1113 parts[numparts].name = " DiskOnChip BDTL partition";
1114 parts[numparts].offset = offs;
1115 parts[numparts].size = (mh->NumEraseUnits - numheaders) << this->bbt_erase_shift;
1116
1117 offs += parts[numparts].size;
1118 numparts++;
1119
1120 if (offs < mtd->size) {
1121 parts[numparts].name = " DiskOnChip Remainder partition";
1122 parts[numparts].offset = offs;
1123 parts[numparts].size = mtd->size - offs;
1124 numparts++;
1125 }
1126
1127 ret = numparts;
David Woodhousee0c7d762006-05-13 18:07:53 +01001128 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 kfree(buf);
1130 return ret;
1131}
1132
1133/* This is a stripped-down copy of the code in inftlmount.c */
David Woodhousee0c7d762006-05-13 18:07:53 +01001134static inline int __init inftl_partscan(struct mtd_info *mtd, struct mtd_partition *parts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +01001136 struct nand_chip *this = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001137 struct doc_priv *doc = nand_get_controller_data(this);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 int ret = 0;
1139 u_char *buf;
1140 struct INFTLMediaHeader *mh;
1141 struct INFTLPartition *ip;
1142 int numparts = 0;
1143 int blocks;
1144 int vshift, lastvunit = 0;
1145 int i;
1146 int end = mtd->size;
1147
1148 if (inftl_bbt_write)
1149 end -= (INFTL_BBT_RESERVED_BLOCKS << this->phys_erase_shift);
1150
Joern Engel28318772006-05-22 23:18:05 +02001151 buf = kmalloc(mtd->writesize, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 if (!buf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 return 0;
1154 }
1155
David Woodhousee0c7d762006-05-13 18:07:53 +01001156 if (!find_media_headers(mtd, buf, "BNAND", 0))
1157 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 doc->mh1_page = doc->mh0_page + (4096 >> this->page_shift);
David Woodhousee0c7d762006-05-13 18:07:53 +01001159 mh = (struct INFTLMediaHeader *)buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
Harvey Harrison96372442008-07-30 12:34:57 -07001161 le32_to_cpus(&mh->NoOfBootImageBlocks);
1162 le32_to_cpus(&mh->NoOfBinaryPartitions);
1163 le32_to_cpus(&mh->NoOfBDTLPartitions);
1164 le32_to_cpus(&mh->BlockMultiplierBits);
1165 le32_to_cpus(&mh->FormatFlags);
1166 le32_to_cpus(&mh->PercentUsed);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001167
Shreeya Patel63fa37f2018-02-22 22:01:22 +05301168 pr_info(" bootRecordID = %s\n"
1169 " NoOfBootImageBlocks = %d\n"
1170 " NoOfBinaryPartitions = %d\n"
1171 " NoOfBDTLPartitions = %d\n"
1172 " BlockMultiplerBits = %d\n"
1173 " FormatFlgs = %d\n"
1174 " OsakVersion = %d.%d.%d.%d\n"
1175 " PercentUsed = %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 mh->bootRecordID, mh->NoOfBootImageBlocks,
1177 mh->NoOfBinaryPartitions,
1178 mh->NoOfBDTLPartitions,
1179 mh->BlockMultiplierBits, mh->FormatFlags,
1180 ((unsigned char *) &mh->OsakVersion)[0] & 0xf,
1181 ((unsigned char *) &mh->OsakVersion)[1] & 0xf,
1182 ((unsigned char *) &mh->OsakVersion)[2] & 0xf,
1183 ((unsigned char *) &mh->OsakVersion)[3] & 0xf,
1184 mh->PercentUsed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185
1186 vshift = this->phys_erase_shift + mh->BlockMultiplierBits;
1187
1188 blocks = mtd->size >> vshift;
1189 if (blocks > 32768) {
Shreeya Patel63fa37f2018-02-22 22:01:22 +05301190 pr_err("BlockMultiplierBits=%d is inconsistent with device size. Aborting.\n", mh->BlockMultiplierBits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 goto out;
1192 }
1193
1194 blocks = doc->chips_per_floor << (this->chip_shift - this->phys_erase_shift);
1195 if (inftl_bbt_write && (blocks > mtd->erasesize)) {
Shreeya Patel63fa37f2018-02-22 22:01:22 +05301196 pr_err("Writeable BBTs spanning more than one erase block are not yet supported. FIX ME!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 goto out;
1198 }
1199
1200 /* Scan the partitions */
1201 for (i = 0; (i < 4); i++) {
1202 ip = &(mh->Partitions[i]);
Harvey Harrison96372442008-07-30 12:34:57 -07001203 le32_to_cpus(&ip->virtualUnits);
1204 le32_to_cpus(&ip->firstUnit);
1205 le32_to_cpus(&ip->lastUnit);
1206 le32_to_cpus(&ip->flags);
1207 le32_to_cpus(&ip->spareUnits);
1208 le32_to_cpus(&ip->Reserved0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209
Shreeya Patel63fa37f2018-02-22 22:01:22 +05301210 pr_info(" PARTITION[%d] ->\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 " virtualUnits = %d\n"
1212 " firstUnit = %d\n"
1213 " lastUnit = %d\n"
1214 " flags = 0x%x\n"
1215 " spareUnits = %d\n",
1216 i, ip->virtualUnits, ip->firstUnit,
1217 ip->lastUnit, ip->flags,
1218 ip->spareUnits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219
Dan Brown1a78ff62005-03-29 21:57:48 +01001220 if ((show_firmware_partition == 1) &&
1221 (i == 0) && (ip->firstUnit > 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 parts[0].name = " DiskOnChip IPL / Media Header partition";
1223 parts[0].offset = 0;
1224 parts[0].size = mtd->erasesize * ip->firstUnit;
1225 numparts = 1;
1226 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227
1228 if (ip->flags & INFTL_BINARY)
1229 parts[numparts].name = " DiskOnChip BDK partition";
1230 else
1231 parts[numparts].name = " DiskOnChip BDTL partition";
1232 parts[numparts].offset = ip->firstUnit << vshift;
1233 parts[numparts].size = (1 + ip->lastUnit - ip->firstUnit) << vshift;
1234 numparts++;
David Woodhousee0c7d762006-05-13 18:07:53 +01001235 if (ip->lastUnit > lastvunit)
1236 lastvunit = ip->lastUnit;
1237 if (ip->flags & INFTL_LAST)
1238 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 }
1240 lastvunit++;
1241 if ((lastvunit << vshift) < end) {
1242 parts[numparts].name = " DiskOnChip Remainder partition";
1243 parts[numparts].offset = lastvunit << vshift;
1244 parts[numparts].size = end - parts[numparts].offset;
1245 numparts++;
1246 }
1247 ret = numparts;
David Woodhousee0c7d762006-05-13 18:07:53 +01001248 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 kfree(buf);
1250 return ret;
1251}
1252
1253static int __init nftl_scan_bbt(struct mtd_info *mtd)
1254{
1255 int ret, numparts;
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +01001256 struct nand_chip *this = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001257 struct doc_priv *doc = nand_get_controller_data(this);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 struct mtd_partition parts[2];
1259
David Woodhousee0c7d762006-05-13 18:07:53 +01001260 memset((char *)parts, 0, sizeof(parts));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 /* On NFTL, we have to find the media headers before we can read the
1262 BBTs, since they're stored in the media header eraseblocks. */
1263 numparts = nftl_partscan(mtd, parts);
David Woodhousee0c7d762006-05-13 18:07:53 +01001264 if (!numparts)
1265 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 this->bbt_td->options = NAND_BBT_ABSPAGE | NAND_BBT_8BIT |
1267 NAND_BBT_SAVECONTENT | NAND_BBT_WRITE |
1268 NAND_BBT_VERSION;
1269 this->bbt_td->veroffs = 7;
1270 this->bbt_td->pages[0] = doc->mh0_page + 1;
1271 if (doc->mh1_page != -1) {
1272 this->bbt_md->options = NAND_BBT_ABSPAGE | NAND_BBT_8BIT |
1273 NAND_BBT_SAVECONTENT | NAND_BBT_WRITE |
1274 NAND_BBT_VERSION;
1275 this->bbt_md->veroffs = 7;
1276 this->bbt_md->pages[0] = doc->mh1_page + 1;
1277 } else {
1278 this->bbt_md = NULL;
1279 }
1280
Boris Brezillone80eba72018-07-05 12:27:31 +02001281 ret = nand_create_bbt(this);
Brian Norrisd24fe0c2015-02-28 02:13:10 -08001282 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 return ret;
Brian Norrisd24fe0c2015-02-28 02:13:10 -08001284
Brian Norris6a7c7332015-06-01 16:17:17 -07001285 return mtd_device_register(mtd, parts, no_autopart ? 0 : numparts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286}
1287
1288static int __init inftl_scan_bbt(struct mtd_info *mtd)
1289{
1290 int ret, numparts;
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +01001291 struct nand_chip *this = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001292 struct doc_priv *doc = nand_get_controller_data(this);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 struct mtd_partition parts[5];
1294
Boris Brezillon32813e22018-10-29 11:58:29 +01001295 if (nanddev_ntargets(&this->base) > doc->chips_per_floor) {
Shreeya Patel63fa37f2018-02-22 22:01:22 +05301296 pr_err("Multi-floor INFTL devices not yet supported.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 return -EIO;
1298 }
1299
1300 if (DoC_is_MillenniumPlus(doc)) {
1301 this->bbt_td->options = NAND_BBT_2BIT | NAND_BBT_ABSPAGE;
1302 if (inftl_bbt_write)
1303 this->bbt_td->options |= NAND_BBT_WRITE;
1304 this->bbt_td->pages[0] = 2;
1305 this->bbt_md = NULL;
1306 } else {
David Woodhousee0c7d762006-05-13 18:07:53 +01001307 this->bbt_td->options = NAND_BBT_LASTBLOCK | NAND_BBT_8BIT | NAND_BBT_VERSION;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 if (inftl_bbt_write)
1309 this->bbt_td->options |= NAND_BBT_WRITE;
1310 this->bbt_td->offs = 8;
1311 this->bbt_td->len = 8;
1312 this->bbt_td->veroffs = 7;
1313 this->bbt_td->maxblocks = INFTL_BBT_RESERVED_BLOCKS;
1314 this->bbt_td->reserved_block_code = 0x01;
1315 this->bbt_td->pattern = "MSYS_BBT";
1316
David Woodhousee0c7d762006-05-13 18:07:53 +01001317 this->bbt_md->options = NAND_BBT_LASTBLOCK | NAND_BBT_8BIT | NAND_BBT_VERSION;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 if (inftl_bbt_write)
1319 this->bbt_md->options |= NAND_BBT_WRITE;
1320 this->bbt_md->offs = 8;
1321 this->bbt_md->len = 8;
1322 this->bbt_md->veroffs = 7;
1323 this->bbt_md->maxblocks = INFTL_BBT_RESERVED_BLOCKS;
1324 this->bbt_md->reserved_block_code = 0x01;
1325 this->bbt_md->pattern = "TBB_SYSM";
1326 }
1327
Boris Brezillone80eba72018-07-05 12:27:31 +02001328 ret = nand_create_bbt(this);
Brian Norrisd24fe0c2015-02-28 02:13:10 -08001329 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 return ret;
Brian Norrisd24fe0c2015-02-28 02:13:10 -08001331
David Woodhousee0c7d762006-05-13 18:07:53 +01001332 memset((char *)parts, 0, sizeof(parts));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 numparts = inftl_partscan(mtd, parts);
1334 /* At least for now, require the INFTL Media Header. We could probably
1335 do without it for non-INFTL use, since all it gives us is
1336 autopartitioning, but I want to give it more thought. */
David Woodhousee0c7d762006-05-13 18:07:53 +01001337 if (!numparts)
1338 return -EIO;
Brian Norris6a7c7332015-06-01 16:17:17 -07001339 return mtd_device_register(mtd, parts, no_autopart ? 0 : numparts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340}
1341
1342static inline int __init doc2000_init(struct mtd_info *mtd)
1343{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +01001344 struct nand_chip *this = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001345 struct doc_priv *doc = nand_get_controller_data(this);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346
Boris Brezillon716bbba2018-09-07 00:38:35 +02001347 this->legacy.read_byte = doc2000_read_byte;
1348 this->legacy.write_buf = doc2000_writebuf;
1349 this->legacy.read_buf = doc2000_readbuf;
Brian Norrisd24fe0c2015-02-28 02:13:10 -08001350 doc->late_init = nftl_scan_bbt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351
1352 doc->CDSNControl = CDSN_CTRL_FLASH_IO | CDSN_CTRL_ECC_IO;
1353 doc2000_count_chips(mtd);
1354 mtd->name = "DiskOnChip 2000 (NFTL Model)";
1355 return (4 * doc->chips_per_floor);
1356}
1357
1358static inline int __init doc2001_init(struct mtd_info *mtd)
1359{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +01001360 struct nand_chip *this = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001361 struct doc_priv *doc = nand_get_controller_data(this);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362
Boris Brezillon716bbba2018-09-07 00:38:35 +02001363 this->legacy.read_byte = doc2001_read_byte;
1364 this->legacy.write_buf = doc2001_writebuf;
1365 this->legacy.read_buf = doc2001_readbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366
1367 ReadDOC(doc->virtadr, ChipID);
1368 ReadDOC(doc->virtadr, ChipID);
1369 ReadDOC(doc->virtadr, ChipID);
1370 if (ReadDOC(doc->virtadr, ChipID) != DOC_ChipID_DocMil) {
1371 /* It's not a Millennium; it's one of the newer
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001372 DiskOnChip 2000 units with a similar ASIC.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 Treat it like a Millennium, except that it
1374 can have multiple chips. */
1375 doc2000_count_chips(mtd);
1376 mtd->name = "DiskOnChip 2000 (INFTL Model)";
Brian Norrisd24fe0c2015-02-28 02:13:10 -08001377 doc->late_init = inftl_scan_bbt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 return (4 * doc->chips_per_floor);
1379 } else {
1380 /* Bog-standard Millennium */
1381 doc->chips_per_floor = 1;
1382 mtd->name = "DiskOnChip Millennium";
Brian Norrisd24fe0c2015-02-28 02:13:10 -08001383 doc->late_init = nftl_scan_bbt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 return 1;
1385 }
1386}
1387
1388static inline int __init doc2001plus_init(struct mtd_info *mtd)
1389{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +01001390 struct nand_chip *this = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001391 struct doc_priv *doc = nand_get_controller_data(this);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392
Boris Brezillon716bbba2018-09-07 00:38:35 +02001393 this->legacy.read_byte = doc2001plus_read_byte;
1394 this->legacy.write_buf = doc2001plus_writebuf;
1395 this->legacy.read_buf = doc2001plus_readbuf;
Brian Norrisd24fe0c2015-02-28 02:13:10 -08001396 doc->late_init = inftl_scan_bbt;
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001397 this->legacy.cmd_ctrl = NULL;
Boris Brezillon7d6c37e2018-11-11 08:55:22 +01001398 this->legacy.select_chip = doc2001plus_select_chip;
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001399 this->legacy.cmdfunc = doc2001plus_command;
Thomas Gleixner0cddd6c2006-05-23 15:59:58 +02001400 this->ecc.hwctl = doc2001plus_enable_hwecc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401
1402 doc->chips_per_floor = 1;
1403 mtd->name = "DiskOnChip Millennium Plus";
1404
1405 return 1;
1406}
1407
Arjan van de Ven858119e2006-01-14 13:20:43 -08001408static int __init doc_probe(unsigned long physadr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409{
Thomas Gleixner964dfce2018-04-22 18:23:54 +02001410 struct nand_chip *nand = NULL;
1411 struct doc_priv *doc = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 unsigned char ChipID;
1413 struct mtd_info *mtd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 void __iomem *virtadr;
1415 unsigned char save_control;
1416 unsigned char tmp, tmpb, tmpc;
1417 int reg, len, numchips;
1418 int ret = 0;
1419
Sasha Levin86e4bbc2014-03-19 18:24:37 -04001420 if (!request_mem_region(physadr, DOC_IOREMAP_LEN, "DiskOnChip"))
Alexander Shiyan4f0614a2013-11-13 15:59:25 +04001421 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 virtadr = ioremap(physadr, DOC_IOREMAP_LEN);
1423 if (!virtadr) {
Shreeya Patel63fa37f2018-02-22 22:01:22 +05301424 pr_err("Diskonchip ioremap failed: 0x%x bytes at 0x%lx\n",
1425 DOC_IOREMAP_LEN, physadr);
Alexander Shiyan4f0614a2013-11-13 15:59:25 +04001426 ret = -EIO;
1427 goto error_ioremap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 }
1429
1430 /* It's not possible to cleanly detect the DiskOnChip - the
1431 * bootup procedure will put the device into reset mode, and
1432 * it's not possible to talk to it without actually writing
1433 * to the DOCControl register. So we store the current contents
1434 * of the DOCControl register's location, in case we later decide
1435 * that it's not a DiskOnChip, and want to put it back how we
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001436 * found it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 */
1438 save_control = ReadDOC(virtadr, DOCControl);
1439
1440 /* Reset the DiskOnChip ASIC */
David Woodhousee0c7d762006-05-13 18:07:53 +01001441 WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_RESET, virtadr, DOCControl);
1442 WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_RESET, virtadr, DOCControl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443
1444 /* Enable the DiskOnChip ASIC */
David Woodhousee0c7d762006-05-13 18:07:53 +01001445 WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_NORMAL, virtadr, DOCControl);
1446 WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_NORMAL, virtadr, DOCControl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447
1448 ChipID = ReadDOC(virtadr, ChipID);
1449
David Woodhousee0c7d762006-05-13 18:07:53 +01001450 switch (ChipID) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 case DOC_ChipID_Doc2k:
1452 reg = DoC_2k_ECCStatus;
1453 break;
1454 case DOC_ChipID_DocMil:
1455 reg = DoC_ECCConf;
1456 break;
1457 case DOC_ChipID_DocMilPlus16:
1458 case DOC_ChipID_DocMilPlus32:
1459 case 0:
1460 /* Possible Millennium Plus, need to do more checks */
1461 /* Possibly release from power down mode */
1462 for (tmp = 0; (tmp < 4); tmp++)
1463 ReadDOC(virtadr, Mplus_Power);
1464
1465 /* Reset the Millennium Plus ASIC */
David Woodhousee0c7d762006-05-13 18:07:53 +01001466 tmp = DOC_MODE_RESET | DOC_MODE_MDWREN | DOC_MODE_RST_LAT | DOC_MODE_BDECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 WriteDOC(tmp, virtadr, Mplus_DOCControl);
1468 WriteDOC(~tmp, virtadr, Mplus_CtrlConfirm);
1469
Jia-Ju Bai7b4b1992018-04-11 10:57:57 +08001470 usleep_range(1000, 2000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 /* Enable the Millennium Plus ASIC */
David Woodhousee0c7d762006-05-13 18:07:53 +01001472 tmp = DOC_MODE_NORMAL | DOC_MODE_MDWREN | DOC_MODE_RST_LAT | DOC_MODE_BDECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 WriteDOC(tmp, virtadr, Mplus_DOCControl);
1474 WriteDOC(~tmp, virtadr, Mplus_CtrlConfirm);
Jia-Ju Bai7b4b1992018-04-11 10:57:57 +08001475 usleep_range(1000, 2000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476
1477 ChipID = ReadDOC(virtadr, ChipID);
1478
1479 switch (ChipID) {
1480 case DOC_ChipID_DocMilPlus16:
1481 reg = DoC_Mplus_Toggle;
1482 break;
1483 case DOC_ChipID_DocMilPlus32:
Shreeya Patel63fa37f2018-02-22 22:01:22 +05301484 pr_err("DiskOnChip Millennium Plus 32MB is not supported, ignoring.\n");
Gustavo A. R. Silva64f1da12019-02-08 11:49:30 -06001485 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 default:
1487 ret = -ENODEV;
1488 goto notfound;
1489 }
1490 break;
1491
1492 default:
1493 ret = -ENODEV;
1494 goto notfound;
1495 }
1496 /* Check the TOGGLE bit in the ECC register */
David Woodhousee0c7d762006-05-13 18:07:53 +01001497 tmp = ReadDOC_(virtadr, reg) & DOC_TOGGLE_BIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 tmpb = ReadDOC_(virtadr, reg) & DOC_TOGGLE_BIT;
1499 tmpc = ReadDOC_(virtadr, reg) & DOC_TOGGLE_BIT;
1500 if ((tmp == tmpb) || (tmp != tmpc)) {
Shreeya Patel63fa37f2018-02-22 22:01:22 +05301501 pr_warn("Possible DiskOnChip at 0x%lx failed TOGGLE test, dropping.\n", physadr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 ret = -ENODEV;
1503 goto notfound;
1504 }
1505
1506 for (mtd = doclist; mtd; mtd = doc->nextdoc) {
1507 unsigned char oldval;
1508 unsigned char newval;
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +01001509 nand = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001510 doc = nand_get_controller_data(nand);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 /* Use the alias resolution register to determine if this is
1512 in fact the same DOC aliased to a new address. If writes
1513 to one chip's alias resolution register change the value on
1514 the other chip, they're the same chip. */
1515 if (ChipID == DOC_ChipID_DocMilPlus16) {
1516 oldval = ReadDOC(doc->virtadr, Mplus_AliasResolution);
1517 newval = ReadDOC(virtadr, Mplus_AliasResolution);
1518 } else {
1519 oldval = ReadDOC(doc->virtadr, AliasResolution);
1520 newval = ReadDOC(virtadr, AliasResolution);
1521 }
1522 if (oldval != newval)
1523 continue;
1524 if (ChipID == DOC_ChipID_DocMilPlus16) {
1525 WriteDOC(~newval, virtadr, Mplus_AliasResolution);
1526 oldval = ReadDOC(doc->virtadr, Mplus_AliasResolution);
David Woodhousee0c7d762006-05-13 18:07:53 +01001527 WriteDOC(newval, virtadr, Mplus_AliasResolution); // restore it
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 } else {
1529 WriteDOC(~newval, virtadr, AliasResolution);
1530 oldval = ReadDOC(doc->virtadr, AliasResolution);
David Woodhousee0c7d762006-05-13 18:07:53 +01001531 WriteDOC(newval, virtadr, AliasResolution); // restore it
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 }
1533 newval = ~newval;
1534 if (oldval == newval) {
Shreeya Patel63fa37f2018-02-22 22:01:22 +05301535 pr_debug("Found alias of DOC at 0x%lx to 0x%lx\n",
1536 doc->physadr, physadr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 goto notfound;
1538 }
1539 }
1540
Shreeya Patel63fa37f2018-02-22 22:01:22 +05301541 pr_notice("DiskOnChip found at 0x%lx\n", physadr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542
Boris BREZILLONb0c423c2015-12-10 09:00:00 +01001543 len = sizeof(struct nand_chip) + sizeof(struct doc_priv) +
1544 (2 * sizeof(struct nand_bbt_descr));
1545 nand = kzalloc(len, GFP_KERNEL);
1546 if (!nand) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 ret = -ENOMEM;
1548 goto fail;
1549 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550
Thomas Gleixner964dfce2018-04-22 18:23:54 +02001551
1552 /*
1553 * Allocate a RS codec instance
1554 *
1555 * Symbolsize is 10 (bits)
1556 * Primitve polynomial is x^10+x^3+1
1557 * First consecutive root is 510
1558 * Primitve element to generate roots = 1
1559 * Generator polinomial degree = 4
1560 */
1561 doc = (struct doc_priv *) (nand + 1);
1562 doc->rs_decoder = init_rs(10, 0x409, FCR, 1, NROOTS);
1563 if (!doc->rs_decoder) {
1564 pr_err("DiskOnChip: Could not create a RS codec\n");
1565 ret = -ENOMEM;
1566 goto fail;
1567 }
1568
Boris BREZILLONb0c423c2015-12-10 09:00:00 +01001569 mtd = nand_to_mtd(nand);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 nand->bbt_td = (struct nand_bbt_descr *) (doc + 1);
1571 nand->bbt_md = nand->bbt_td + 1;
1572
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 mtd->owner = THIS_MODULE;
Boris Brezillon68c1b752016-02-03 20:00:23 +01001574 mtd_set_ooblayout(mtd, &doc200x_ooblayout_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001576 nand_set_controller_data(nand, doc);
Boris Brezillon7d6c37e2018-11-11 08:55:22 +01001577 nand->legacy.select_chip = doc200x_select_chip;
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001578 nand->legacy.cmd_ctrl = doc200x_hwcontrol;
Boris Brezillon8395b752018-09-07 00:38:37 +02001579 nand->legacy.dev_ready = doc200x_dev_ready;
1580 nand->legacy.waitfunc = doc200x_wait;
Boris Brezilloncdc784c2018-09-07 00:38:38 +02001581 nand->legacy.block_bad = doc200x_block_bad;
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02001582 nand->ecc.hwctl = doc200x_enable_hwecc;
1583 nand->ecc.calculate = doc200x_calculate_ecc;
1584 nand->ecc.correct = doc200x_correct_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02001586 nand->ecc.mode = NAND_ECC_HW_SYNDROME;
1587 nand->ecc.size = 512;
1588 nand->ecc.bytes = 6;
Mike Dunn6a918ba2012-03-11 14:21:11 -07001589 nand->ecc.strength = 2;
Boris BREZILLONcc01e602015-12-30 20:39:52 +01001590 nand->ecc.options = NAND_ECC_GENERIC_ERASED_CHECK;
Brian Norrisbb9ebd42011-05-31 16:31:23 -07001591 nand->bbt_options = NAND_BBT_USE_FLASH;
Brian Norrisd24fe0c2015-02-28 02:13:10 -08001592 /* Skip the automatic BBT scan so we can run it manually */
1593 nand->options |= NAND_SKIP_BBTSCAN;
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
Boris Brezillon00ad3782018-09-06 14:05:14 +02001611 if ((ret = nand_scan(nand, numchips)) || (ret = doc->late_init(mtd))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 /* 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. */
Boris Brezillon59ac2762018-09-06 14:05:15 +02001618 nand_release(nand);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 goto fail;
1620 }
1621
1622 /* Success! */
1623 doclist = mtd;
1624 return 0;
1625
David Woodhousee0c7d762006-05-13 18:07:53 +01001626 notfound:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 /* Put back the contents of the DOCControl register, in case it's not
1628 actually a DiskOnChip. */
1629 WriteDOC(save_control, virtadr, DOCControl);
David Woodhousee0c7d762006-05-13 18:07:53 +01001630 fail:
Thomas Gleixner964dfce2018-04-22 18:23:54 +02001631 if (doc)
1632 free_rs(doc->rs_decoder);
1633 kfree(nand);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 iounmap(virtadr);
Alexander Shiyan4f0614a2013-11-13 15:59:25 +04001635
1636error_ioremap:
1637 release_mem_region(physadr, DOC_IOREMAP_LEN);
1638
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 return ret;
1640}
1641
1642static void release_nanddoc(void)
1643{
David Woodhousee0c7d762006-05-13 18:07:53 +01001644 struct mtd_info *mtd, *nextmtd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 struct nand_chip *nand;
1646 struct doc_priv *doc;
1647
1648 for (mtd = doclist; mtd; mtd = nextmtd) {
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +01001649 nand = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001650 doc = nand_get_controller_data(nand);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651
1652 nextmtd = doc->nextdoc;
Boris Brezillon59ac2762018-09-06 14:05:15 +02001653 nand_release(nand);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 iounmap(doc->virtadr);
Alexander Shiyan4f0614a2013-11-13 15:59:25 +04001655 release_mem_region(doc->physadr, DOC_IOREMAP_LEN);
Thomas Gleixner964dfce2018-04-22 18:23:54 +02001656 free_rs(doc->rs_decoder);
Boris BREZILLONb0c423c2015-12-10 09:00:00 +01001657 kfree(nand);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 }
1659}
1660
1661static int __init init_nanddoc(void)
1662{
1663 int i, ret = 0;
1664
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 if (doc_config_location) {
Shreeya Patel63fa37f2018-02-22 22:01:22 +05301666 pr_info("Using configured DiskOnChip probe address 0x%lx\n",
1667 doc_config_location);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 ret = doc_probe(doc_config_location);
1669 if (ret < 0)
Thomas Gleixner964dfce2018-04-22 18:23:54 +02001670 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 } else {
David Woodhousee0c7d762006-05-13 18:07:53 +01001672 for (i = 0; (doc_locations[i] != 0xffffffff); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 doc_probe(doc_locations[i]);
1674 }
1675 }
1676 /* No banner message any more. Print a message if no DiskOnChip
1677 found, so the user knows we at least tried. */
1678 if (!doclist) {
Shreeya Patel63fa37f2018-02-22 22:01:22 +05301679 pr_info("No valid DiskOnChip devices found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 ret = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 return ret;
1683}
1684
1685static void __exit cleanup_nanddoc(void)
1686{
1687 /* Cleanup the nand/DoC resources */
1688 release_nanddoc();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689}
1690
1691module_init(init_nanddoc);
1692module_exit(cleanup_nanddoc);
1693
1694MODULE_LICENSE("GPL");
1695MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
Niels de Vos2a7af8c2009-01-30 11:08:35 +01001696MODULE_DESCRIPTION("M-Systems DiskOnChip 2000, Millennium and Millennium Plus device driver");