Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * drivers/mtd/nand/diskonchip.c |
| 3 | * |
| 4 | * (C) 2003 Red Hat, Inc. |
| 5 | * (C) 2004 Dan Brown <dan_brown@ieee.org> |
| 6 | * (C) 2004 Kalev Lember <kalev@smartlink.ee> |
| 7 | * |
| 8 | * Author: David Woodhouse <dwmw2@infradead.org> |
| 9 | * Additional Diskonchip 2000 and Millennium support by Dan Brown <dan_brown@ieee.org> |
| 10 | * Diskonchip Millennium Plus support by Kalev Lember <kalev@smartlink.ee> |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 11 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | * Error correction code lifted from the old docecc code |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 13 | * Author: Fabrice Bellard (fabrice.bellard@netgem.com) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | * Copyright (C) 2000 Netgem S.A. |
| 15 | * converted to the generic Reed-Solomon library by Thomas Gleixner <tglx@linutronix.de> |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 16 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | * Interface to generic NAND code for M-Systems DiskOnChip devices |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | #include <linux/kernel.h> |
| 21 | #include <linux/init.h> |
| 22 | #include <linux/sched.h> |
| 23 | #include <linux/delay.h> |
| 24 | #include <linux/rslib.h> |
| 25 | #include <linux/moduleparam.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 26 | #include <linux/slab.h> |
Dan Williams | 2584cf8 | 2015-08-10 23:07:05 -0400 | [diff] [blame] | 27 | #include <linux/io.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | |
| 29 | #include <linux/mtd/mtd.h> |
| 30 | #include <linux/mtd/nand.h> |
| 31 | #include <linux/mtd/doc2000.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | #include <linux/mtd/partitions.h> |
| 33 | #include <linux/mtd/inftl.h> |
Paul Gortmaker | a0e5cc5 | 2011-07-03 15:17:31 -0400 | [diff] [blame] | 34 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | |
| 36 | /* Where to look for the devices? */ |
Thomas Gleixner | 651078b | 2005-01-31 20:36:46 +0000 | [diff] [blame] | 37 | #ifndef CONFIG_MTD_NAND_DISKONCHIP_PROBE_ADDRESS |
| 38 | #define CONFIG_MTD_NAND_DISKONCHIP_PROBE_ADDRESS 0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | #endif |
| 40 | |
Sachin Kamat | 14a95b8a | 2013-08-12 15:10:47 +0530 | [diff] [blame] | 41 | static unsigned long doc_locations[] __initdata = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | #if defined (__alpha__) || defined(__i386__) || defined(__x86_64__) |
Thomas Gleixner | 651078b | 2005-01-31 20:36:46 +0000 | [diff] [blame] | 43 | #ifdef CONFIG_MTD_NAND_DISKONCHIP_PROBE_HIGH |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 44 | 0xfffc8000, 0xfffca000, 0xfffcc000, 0xfffce000, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | 0xfffd0000, 0xfffd2000, 0xfffd4000, 0xfffd6000, |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 46 | 0xfffd8000, 0xfffda000, 0xfffdc000, 0xfffde000, |
| 47 | 0xfffe0000, 0xfffe2000, 0xfffe4000, 0xfffe6000, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | 0xfffe8000, 0xfffea000, 0xfffec000, 0xfffee000, |
Michael Opdenacker | 9d40349 | 2013-07-08 06:39:20 +0200 | [diff] [blame] | 49 | #else |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 50 | 0xc8000, 0xca000, 0xcc000, 0xce000, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | 0xd0000, 0xd2000, 0xd4000, 0xd6000, |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 52 | 0xd8000, 0xda000, 0xdc000, 0xde000, |
| 53 | 0xe0000, 0xe2000, 0xe4000, 0xe6000, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | 0xe8000, 0xea000, 0xec000, 0xee000, |
Michael Opdenacker | 9d40349 | 2013-07-08 06:39:20 +0200 | [diff] [blame] | 55 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | #endif |
| 57 | 0xffffffff }; |
| 58 | |
| 59 | static struct mtd_info *doclist = NULL; |
| 60 | |
| 61 | struct doc_priv { |
| 62 | void __iomem *virtadr; |
| 63 | unsigned long physadr; |
| 64 | u_char ChipID; |
| 65 | u_char CDSNControl; |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 66 | int chips_per_floor; /* The number of chips detected on each floor */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | int curfloor; |
| 68 | int curchip; |
| 69 | int mh0_page; |
| 70 | int mh1_page; |
| 71 | struct mtd_info *nextdoc; |
Brian Norris | d24fe0c | 2015-02-28 02:13:10 -0800 | [diff] [blame] | 72 | |
| 73 | /* Handle the last stage of initialization (BBT scan, partitioning) */ |
| 74 | int (*late_init)(struct mtd_info *mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | }; |
| 76 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | /* This is the ecc value computed by the HW ecc generator upon writing an empty |
| 78 | page, one with all 0xff for data. */ |
| 79 | static 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 | |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 87 | static void doc200x_hwcontrol(struct mtd_info *mtd, int cmd, |
| 88 | unsigned int bitmask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | static void doc200x_select_chip(struct mtd_info *mtd, int chip); |
| 90 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 91 | static int debug = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | module_param(debug, int, 0); |
| 93 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 94 | static int try_dword = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | module_param(try_dword, int, 0); |
| 96 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 97 | static int no_ecc_failures = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | module_param(no_ecc_failures, int, 0); |
| 99 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 100 | static int no_autopart = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | module_param(no_autopart, int, 0); |
Dan Brown | 1a78ff6 | 2005-03-29 21:57:48 +0100 | [diff] [blame] | 102 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 103 | static int show_firmware_partition = 0; |
Dan Brown | 1a78ff6 | 2005-03-29 21:57:48 +0100 | [diff] [blame] | 104 | module_param(show_firmware_partition, int, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | |
Robert P. J. Day | 89e2bf6 | 2007-03-07 18:33:25 -0500 | [diff] [blame] | 106 | #ifdef CONFIG_MTD_NAND_DISKONCHIP_BBTWRITE |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 107 | static int inftl_bbt_write = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | #else |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 109 | static int inftl_bbt_write = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | #endif |
| 111 | module_param(inftl_bbt_write, int, 0); |
| 112 | |
Thomas Gleixner | 651078b | 2005-01-31 20:36:46 +0000 | [diff] [blame] | 113 | static unsigned long doc_config_location = CONFIG_MTD_NAND_DISKONCHIP_PROBE_ADDRESS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | module_param(doc_config_location, ulong, 0); |
| 115 | MODULE_PARM_DESC(doc_config_location, "Physical memory address at which to probe for DiskOnChip"); |
| 116 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | /* 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 | |
| 128 | /* the Reed Solomon control structure */ |
| 129 | static struct rs_control *rs_decoder; |
| 130 | |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 131 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | * The HW decoder in the DoC ASIC's provides us a error syndrome, |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 133 | * which we must convert to a standard syndrome usable by the generic |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | * Reed-Solomon library code. |
| 135 | * |
| 136 | * Fabrice Bellard figured this out in the old docecc code. I added |
| 137 | * some comments, improved a minor bit and converted it to make use |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 138 | * of the generic Reed-Solomon library. tglx |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 140 | static int doc_ecc_decode(struct rs_control *rs, uint8_t *data, uint8_t *ecc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | { |
| 142 | int i, j, nerr, errpos[8]; |
| 143 | uint8_t parity; |
| 144 | uint16_t ds[4], s[5], tmp, errval[8], syn[4]; |
| 145 | |
Mark Ware | c9fb677 | 2010-05-27 11:45:39 +1000 | [diff] [blame] | 146 | memset(syn, 0, sizeof(syn)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | /* Convert the ecc bytes into words */ |
| 148 | ds[0] = ((ecc[4] & 0xff) >> 0) | ((ecc[5] & 0x03) << 8); |
| 149 | ds[1] = ((ecc[5] & 0xfc) >> 2) | ((ecc[2] & 0x0f) << 6); |
| 150 | ds[2] = ((ecc[2] & 0xf0) >> 4) | ((ecc[3] & 0x3f) << 4); |
| 151 | ds[3] = ((ecc[3] & 0xc0) >> 6) | ((ecc[0] & 0xff) << 2); |
| 152 | parity = ecc[1]; |
| 153 | |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 154 | /* Initialize the syndrome buffer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | for (i = 0; i < NROOTS; i++) |
| 156 | s[i] = ds[0]; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 157 | /* |
| 158 | * Evaluate |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | * s[i] = ds[3]x^3 + ds[2]x^2 + ds[1]x^1 + ds[0] |
| 160 | * where x = alpha^(FCR + i) |
| 161 | */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 162 | for (j = 1; j < NROOTS; j++) { |
| 163 | if (ds[j] == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | continue; |
| 165 | tmp = rs->index_of[ds[j]]; |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 166 | for (i = 0; i < NROOTS; i++) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | s[i] ^= rs->alpha_to[rs_modnn(rs, tmp + (FCR + i) * j)]; |
| 168 | } |
| 169 | |
Mark Ware | c9fb677 | 2010-05-27 11:45:39 +1000 | [diff] [blame] | 170 | /* Calc syn[i] = s[i] / alpha^(v + i) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | for (i = 0; i < NROOTS; i++) { |
Mark Ware | c9fb677 | 2010-05-27 11:45:39 +1000 | [diff] [blame] | 172 | if (s[i]) |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 173 | syn[i] = rs_modnn(rs, rs->index_of[s[i]] + (NN - FCR - i)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | } |
| 175 | /* Call the decoder library */ |
| 176 | nerr = decode_rs16(rs, NULL, NULL, 1019, syn, 0, errpos, 0, errval); |
| 177 | |
| 178 | /* Incorrectable errors ? */ |
| 179 | if (nerr < 0) |
| 180 | return nerr; |
| 181 | |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 182 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | * Correct the errors. The bitpositions are a bit of magic, |
| 184 | * but they are given by the design of the de/encoder circuit |
| 185 | * in the DoC ASIC's. |
| 186 | */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 187 | for (i = 0; i < nerr; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | int index, bitpos, pos = 1015 - errpos[i]; |
| 189 | uint8_t val; |
| 190 | if (pos >= NB_DATA && pos < 1019) |
| 191 | continue; |
| 192 | if (pos < NB_DATA) { |
| 193 | /* extract bit position (MSB first) */ |
| 194 | pos = 10 * (NB_DATA - 1 - pos) - 6; |
| 195 | /* now correct the following 10 bits. At most two bytes |
| 196 | can be modified since pos is even */ |
| 197 | index = (pos >> 3) ^ 1; |
| 198 | bitpos = pos & 7; |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 199 | if ((index >= 0 && index < SECTOR_SIZE) || index == (SECTOR_SIZE + 1)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | val = (uint8_t) (errval[i] >> (2 + bitpos)); |
| 201 | parity ^= val; |
| 202 | if (index < SECTOR_SIZE) |
| 203 | data[index] ^= val; |
| 204 | } |
| 205 | index = ((pos >> 3) + 1) ^ 1; |
| 206 | bitpos = (bitpos + 10) & 7; |
| 207 | if (bitpos == 0) |
| 208 | bitpos = 8; |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 209 | if ((index >= 0 && index < SECTOR_SIZE) || index == (SECTOR_SIZE + 1)) { |
| 210 | val = (uint8_t) (errval[i] << (8 - bitpos)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | parity ^= val; |
| 212 | if (index < SECTOR_SIZE) |
| 213 | data[index] ^= val; |
| 214 | } |
| 215 | } |
| 216 | } |
| 217 | /* If the parity is wrong, no rescue possible */ |
Jörn Engel | eb68450 | 2007-10-20 23:16:32 +0200 | [diff] [blame] | 218 | return parity ? -EBADMSG : nerr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | static void DoC_Delay(struct doc_priv *doc, unsigned short cycles) |
| 222 | { |
| 223 | volatile char dummy; |
| 224 | int i; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 225 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | for (i = 0; i < cycles; i++) { |
| 227 | if (DoC_is_Millennium(doc)) |
| 228 | dummy = ReadDOC(doc->virtadr, NOP); |
| 229 | else if (DoC_is_MillenniumPlus(doc)) |
| 230 | dummy = ReadDOC(doc->virtadr, Mplus_NOP); |
| 231 | else |
| 232 | dummy = ReadDOC(doc->virtadr, DOCStatus); |
| 233 | } |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 234 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | #define CDSN_CTRL_FR_B_MASK (CDSN_CTRL_FR_B0 | CDSN_CTRL_FR_B1) |
| 238 | |
| 239 | /* DOC_WaitReady: Wait for RDY line to be asserted by the flash chip */ |
| 240 | static int _DoC_WaitReady(struct doc_priv *doc) |
| 241 | { |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 242 | void __iomem *docptr = doc->virtadr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | unsigned long timeo = jiffies + (HZ * 10); |
| 244 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 245 | if (debug) |
| 246 | printk("_DoC_WaitReady...\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | /* Out-of-line routine to wait for chip response */ |
| 248 | if (DoC_is_MillenniumPlus(doc)) { |
| 249 | while ((ReadDOC(docptr, Mplus_FlashControl) & CDSN_CTRL_FR_B_MASK) != CDSN_CTRL_FR_B_MASK) { |
| 250 | if (time_after(jiffies, timeo)) { |
| 251 | printk("_DoC_WaitReady timed out.\n"); |
| 252 | return -EIO; |
| 253 | } |
| 254 | udelay(1); |
| 255 | cond_resched(); |
| 256 | } |
| 257 | } else { |
| 258 | while (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B)) { |
| 259 | if (time_after(jiffies, timeo)) { |
| 260 | printk("_DoC_WaitReady timed out.\n"); |
| 261 | return -EIO; |
| 262 | } |
| 263 | udelay(1); |
| 264 | cond_resched(); |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | return 0; |
| 269 | } |
| 270 | |
| 271 | static inline int DoC_WaitReady(struct doc_priv *doc) |
| 272 | { |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 273 | void __iomem *docptr = doc->virtadr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | int ret = 0; |
| 275 | |
| 276 | if (DoC_is_MillenniumPlus(doc)) { |
| 277 | DoC_Delay(doc, 4); |
| 278 | |
| 279 | if ((ReadDOC(docptr, Mplus_FlashControl) & CDSN_CTRL_FR_B_MASK) != CDSN_CTRL_FR_B_MASK) |
| 280 | /* Call the out-of-line routine to wait */ |
| 281 | ret = _DoC_WaitReady(doc); |
| 282 | } else { |
| 283 | DoC_Delay(doc, 4); |
| 284 | |
| 285 | if (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B)) |
| 286 | /* Call the out-of-line routine to wait */ |
| 287 | ret = _DoC_WaitReady(doc); |
| 288 | DoC_Delay(doc, 2); |
| 289 | } |
| 290 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 291 | if (debug) |
| 292 | printk("DoC_WaitReady OK\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | return ret; |
| 294 | } |
| 295 | |
| 296 | static void doc2000_write_byte(struct mtd_info *mtd, u_char datum) |
| 297 | { |
Boris BREZILLON | 4bd4ebc | 2015-12-01 12:03:04 +0100 | [diff] [blame] | 298 | struct nand_chip *this = mtd_to_nand(mtd); |
Boris BREZILLON | d699ed2 | 2015-12-10 09:00:41 +0100 | [diff] [blame] | 299 | struct doc_priv *doc = nand_get_controller_data(this); |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 300 | void __iomem *docptr = doc->virtadr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 302 | if (debug) |
| 303 | printk("write_byte %02x\n", datum); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | WriteDOC(datum, docptr, CDSNSlowIO); |
| 305 | WriteDOC(datum, docptr, 2k_CDSN_IO); |
| 306 | } |
| 307 | |
| 308 | static u_char doc2000_read_byte(struct mtd_info *mtd) |
| 309 | { |
Boris BREZILLON | 4bd4ebc | 2015-12-01 12:03:04 +0100 | [diff] [blame] | 310 | struct nand_chip *this = mtd_to_nand(mtd); |
Boris BREZILLON | d699ed2 | 2015-12-10 09:00:41 +0100 | [diff] [blame] | 311 | struct doc_priv *doc = nand_get_controller_data(this); |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 312 | void __iomem *docptr = doc->virtadr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | u_char ret; |
| 314 | |
| 315 | ReadDOC(docptr, CDSNSlowIO); |
| 316 | DoC_Delay(doc, 2); |
| 317 | ret = ReadDOC(docptr, 2k_CDSN_IO); |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 318 | if (debug) |
| 319 | printk("read_byte returns %02x\n", ret); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | return ret; |
| 321 | } |
| 322 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 323 | static void doc2000_writebuf(struct mtd_info *mtd, const u_char *buf, int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | { |
Boris BREZILLON | 4bd4ebc | 2015-12-01 12:03:04 +0100 | [diff] [blame] | 325 | struct nand_chip *this = mtd_to_nand(mtd); |
Boris BREZILLON | d699ed2 | 2015-12-10 09:00:41 +0100 | [diff] [blame] | 326 | struct doc_priv *doc = nand_get_controller_data(this); |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 327 | void __iomem *docptr = doc->virtadr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | int i; |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 329 | if (debug) |
| 330 | printk("writebuf of %d bytes: ", len); |
| 331 | for (i = 0; i < len; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | WriteDOC_(buf[i], docptr, DoC_2k_CDSN_IO + i); |
| 333 | if (debug && i < 16) |
| 334 | printk("%02x ", buf[i]); |
| 335 | } |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 336 | if (debug) |
| 337 | printk("\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | } |
| 339 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 340 | static void doc2000_readbuf(struct mtd_info *mtd, u_char *buf, int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | { |
Boris BREZILLON | 4bd4ebc | 2015-12-01 12:03:04 +0100 | [diff] [blame] | 342 | struct nand_chip *this = mtd_to_nand(mtd); |
Boris BREZILLON | d699ed2 | 2015-12-10 09:00:41 +0100 | [diff] [blame] | 343 | struct doc_priv *doc = nand_get_controller_data(this); |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 344 | void __iomem *docptr = doc->virtadr; |
| 345 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 347 | if (debug) |
| 348 | printk("readbuf of %d bytes: ", len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 350 | for (i = 0; i < len; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | buf[i] = ReadDOC(docptr, 2k_CDSN_IO + i); |
| 352 | } |
| 353 | } |
| 354 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 355 | static void doc2000_readbuf_dword(struct mtd_info *mtd, u_char *buf, int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | { |
Boris BREZILLON | 4bd4ebc | 2015-12-01 12:03:04 +0100 | [diff] [blame] | 357 | struct nand_chip *this = mtd_to_nand(mtd); |
Boris BREZILLON | d699ed2 | 2015-12-10 09:00:41 +0100 | [diff] [blame] | 358 | struct doc_priv *doc = nand_get_controller_data(this); |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 359 | void __iomem *docptr = doc->virtadr; |
| 360 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 362 | if (debug) |
| 363 | printk("readbuf_dword of %d bytes: ", len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 365 | if (unlikely((((unsigned long)buf) | len) & 3)) { |
| 366 | for (i = 0; i < len; i++) { |
| 367 | *(uint8_t *) (&buf[i]) = ReadDOC(docptr, 2k_CDSN_IO + i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | } |
| 369 | } else { |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 370 | for (i = 0; i < len; i += 4) { |
| 371 | *(uint32_t *) (&buf[i]) = readl(docptr + DoC_2k_CDSN_IO + i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | } |
| 373 | } |
| 374 | } |
| 375 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | static uint16_t __init doc200x_ident_chip(struct mtd_info *mtd, int nr) |
| 377 | { |
Boris BREZILLON | 4bd4ebc | 2015-12-01 12:03:04 +0100 | [diff] [blame] | 378 | struct nand_chip *this = mtd_to_nand(mtd); |
Boris BREZILLON | d699ed2 | 2015-12-10 09:00:41 +0100 | [diff] [blame] | 379 | struct doc_priv *doc = nand_get_controller_data(this); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | uint16_t ret; |
| 381 | |
| 382 | doc200x_select_chip(mtd, nr); |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 383 | doc200x_hwcontrol(mtd, NAND_CMD_READID, |
| 384 | NAND_CTRL_CLE | NAND_CTRL_CHANGE); |
| 385 | doc200x_hwcontrol(mtd, 0, NAND_CTRL_ALE | NAND_CTRL_CHANGE); |
| 386 | doc200x_hwcontrol(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE); |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 387 | |
Lucas De Marchi | e9c5499 | 2011-04-26 23:28:26 -0700 | [diff] [blame] | 388 | /* We can't use dev_ready here, but at least we wait for the |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 389 | * command to complete |
Thomas Gleixner | dfd6129 | 2005-02-22 21:48:25 +0000 | [diff] [blame] | 390 | */ |
| 391 | udelay(50); |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 392 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | ret = this->read_byte(mtd) << 8; |
| 394 | ret |= this->read_byte(mtd); |
| 395 | |
| 396 | if (doc->ChipID == DOC_ChipID_Doc2k && try_dword && !nr) { |
| 397 | /* First chip probe. See if we get same results by 32-bit access */ |
| 398 | union { |
| 399 | uint32_t dword; |
| 400 | uint8_t byte[4]; |
| 401 | } ident; |
| 402 | void __iomem *docptr = doc->virtadr; |
| 403 | |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 404 | doc200x_hwcontrol(mtd, NAND_CMD_READID, |
| 405 | NAND_CTRL_CLE | NAND_CTRL_CHANGE); |
| 406 | doc200x_hwcontrol(mtd, 0, NAND_CTRL_ALE | NAND_CTRL_CHANGE); |
| 407 | doc200x_hwcontrol(mtd, NAND_CMD_NONE, |
| 408 | NAND_NCE | NAND_CTRL_CHANGE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | |
Thomas Gleixner | dfd6129 | 2005-02-22 21:48:25 +0000 | [diff] [blame] | 410 | udelay(50); |
| 411 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | ident.dword = readl(docptr + DoC_2k_CDSN_IO); |
| 413 | if (((ident.byte[0] << 8) | ident.byte[1]) == ret) { |
| 414 | printk(KERN_INFO "DiskOnChip 2000 responds to DWORD access\n"); |
| 415 | this->read_buf = &doc2000_readbuf_dword; |
| 416 | } |
| 417 | } |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 418 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | return ret; |
| 420 | } |
| 421 | |
| 422 | static void __init doc2000_count_chips(struct mtd_info *mtd) |
| 423 | { |
Boris BREZILLON | 4bd4ebc | 2015-12-01 12:03:04 +0100 | [diff] [blame] | 424 | struct nand_chip *this = mtd_to_nand(mtd); |
Boris BREZILLON | d699ed2 | 2015-12-10 09:00:41 +0100 | [diff] [blame] | 425 | struct doc_priv *doc = nand_get_controller_data(this); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | uint16_t mfrid; |
| 427 | int i; |
| 428 | |
| 429 | /* Max 4 chips per floor on DiskOnChip 2000 */ |
| 430 | doc->chips_per_floor = 4; |
| 431 | |
| 432 | /* Find out what the first chip is */ |
| 433 | mfrid = doc200x_ident_chip(mtd, 0); |
| 434 | |
| 435 | /* Find how many chips in each floor. */ |
| 436 | for (i = 1; i < 4; i++) { |
| 437 | if (doc200x_ident_chip(mtd, i) != mfrid) |
| 438 | break; |
| 439 | } |
| 440 | doc->chips_per_floor = i; |
| 441 | printk(KERN_DEBUG "Detected %d chips per floor.\n", i); |
| 442 | } |
| 443 | |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 444 | static int doc200x_wait(struct mtd_info *mtd, struct nand_chip *this) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | { |
Boris BREZILLON | d699ed2 | 2015-12-10 09:00:41 +0100 | [diff] [blame] | 446 | struct doc_priv *doc = nand_get_controller_data(this); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | |
| 448 | int status; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 449 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | DoC_WaitReady(doc); |
| 451 | this->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1); |
| 452 | DoC_WaitReady(doc); |
| 453 | status = (int)this->read_byte(mtd); |
| 454 | |
| 455 | return status; |
| 456 | } |
| 457 | |
| 458 | static void doc2001_write_byte(struct mtd_info *mtd, u_char datum) |
| 459 | { |
Boris BREZILLON | 4bd4ebc | 2015-12-01 12:03:04 +0100 | [diff] [blame] | 460 | struct nand_chip *this = mtd_to_nand(mtd); |
Boris BREZILLON | d699ed2 | 2015-12-10 09:00:41 +0100 | [diff] [blame] | 461 | struct doc_priv *doc = nand_get_controller_data(this); |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 462 | void __iomem *docptr = doc->virtadr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | |
| 464 | WriteDOC(datum, docptr, CDSNSlowIO); |
| 465 | WriteDOC(datum, docptr, Mil_CDSN_IO); |
| 466 | WriteDOC(datum, docptr, WritePipeTerm); |
| 467 | } |
| 468 | |
| 469 | static u_char doc2001_read_byte(struct mtd_info *mtd) |
| 470 | { |
Boris BREZILLON | 4bd4ebc | 2015-12-01 12:03:04 +0100 | [diff] [blame] | 471 | struct nand_chip *this = mtd_to_nand(mtd); |
Boris BREZILLON | d699ed2 | 2015-12-10 09:00:41 +0100 | [diff] [blame] | 472 | struct doc_priv *doc = nand_get_controller_data(this); |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 473 | void __iomem *docptr = doc->virtadr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | |
| 475 | //ReadDOC(docptr, CDSNSlowIO); |
| 476 | /* 11.4.5 -- delay twice to allow extended length cycle */ |
| 477 | DoC_Delay(doc, 2); |
| 478 | ReadDOC(docptr, ReadPipeInit); |
| 479 | //return ReadDOC(docptr, Mil_CDSN_IO); |
| 480 | return ReadDOC(docptr, LastDataRead); |
| 481 | } |
| 482 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 483 | static void doc2001_writebuf(struct mtd_info *mtd, const u_char *buf, int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | { |
Boris BREZILLON | 4bd4ebc | 2015-12-01 12:03:04 +0100 | [diff] [blame] | 485 | struct nand_chip *this = mtd_to_nand(mtd); |
Boris BREZILLON | d699ed2 | 2015-12-10 09:00:41 +0100 | [diff] [blame] | 486 | struct doc_priv *doc = nand_get_controller_data(this); |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 487 | void __iomem *docptr = doc->virtadr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | int i; |
| 489 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 490 | for (i = 0; i < len; i++) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | WriteDOC_(buf[i], docptr, DoC_Mil_CDSN_IO + i); |
| 492 | /* Terminate write pipeline */ |
| 493 | WriteDOC(0x00, docptr, WritePipeTerm); |
| 494 | } |
| 495 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 496 | static void doc2001_readbuf(struct mtd_info *mtd, u_char *buf, int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 497 | { |
Boris BREZILLON | 4bd4ebc | 2015-12-01 12:03:04 +0100 | [diff] [blame] | 498 | struct nand_chip *this = mtd_to_nand(mtd); |
Boris BREZILLON | d699ed2 | 2015-12-10 09:00:41 +0100 | [diff] [blame] | 499 | struct doc_priv *doc = nand_get_controller_data(this); |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 500 | void __iomem *docptr = doc->virtadr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | int i; |
| 502 | |
| 503 | /* Start read pipeline */ |
| 504 | ReadDOC(docptr, ReadPipeInit); |
| 505 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 506 | for (i = 0; i < len - 1; i++) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 507 | buf[i] = ReadDOC(docptr, Mil_CDSN_IO + (i & 0xff)); |
| 508 | |
| 509 | /* Terminate read pipeline */ |
| 510 | buf[i] = ReadDOC(docptr, LastDataRead); |
| 511 | } |
| 512 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | static u_char doc2001plus_read_byte(struct mtd_info *mtd) |
| 514 | { |
Boris BREZILLON | 4bd4ebc | 2015-12-01 12:03:04 +0100 | [diff] [blame] | 515 | struct nand_chip *this = mtd_to_nand(mtd); |
Boris BREZILLON | d699ed2 | 2015-12-10 09:00:41 +0100 | [diff] [blame] | 516 | struct doc_priv *doc = nand_get_controller_data(this); |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 517 | void __iomem *docptr = doc->virtadr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 518 | u_char ret; |
| 519 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 520 | ReadDOC(docptr, Mplus_ReadPipeInit); |
| 521 | ReadDOC(docptr, Mplus_ReadPipeInit); |
| 522 | ret = ReadDOC(docptr, Mplus_LastDataRead); |
| 523 | if (debug) |
| 524 | printk("read_byte returns %02x\n", ret); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | return ret; |
| 526 | } |
| 527 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 528 | static void doc2001plus_writebuf(struct mtd_info *mtd, const u_char *buf, int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | { |
Boris BREZILLON | 4bd4ebc | 2015-12-01 12:03:04 +0100 | [diff] [blame] | 530 | struct nand_chip *this = mtd_to_nand(mtd); |
Boris BREZILLON | d699ed2 | 2015-12-10 09:00:41 +0100 | [diff] [blame] | 531 | struct doc_priv *doc = nand_get_controller_data(this); |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 532 | void __iomem *docptr = doc->virtadr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 533 | int i; |
| 534 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 535 | if (debug) |
| 536 | printk("writebuf of %d bytes: ", len); |
| 537 | for (i = 0; i < len; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 538 | WriteDOC_(buf[i], docptr, DoC_Mil_CDSN_IO + i); |
| 539 | if (debug && i < 16) |
| 540 | printk("%02x ", buf[i]); |
| 541 | } |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 542 | if (debug) |
| 543 | printk("\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 544 | } |
| 545 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 546 | static void doc2001plus_readbuf(struct mtd_info *mtd, u_char *buf, int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 547 | { |
Boris BREZILLON | 4bd4ebc | 2015-12-01 12:03:04 +0100 | [diff] [blame] | 548 | struct nand_chip *this = mtd_to_nand(mtd); |
Boris BREZILLON | d699ed2 | 2015-12-10 09:00:41 +0100 | [diff] [blame] | 549 | struct doc_priv *doc = nand_get_controller_data(this); |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 550 | void __iomem *docptr = doc->virtadr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | int i; |
| 552 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 553 | if (debug) |
| 554 | printk("readbuf of %d bytes: ", len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 555 | |
| 556 | /* Start read pipeline */ |
| 557 | ReadDOC(docptr, Mplus_ReadPipeInit); |
| 558 | ReadDOC(docptr, Mplus_ReadPipeInit); |
| 559 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 560 | for (i = 0; i < len - 2; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | buf[i] = ReadDOC(docptr, Mil_CDSN_IO); |
| 562 | if (debug && i < 16) |
| 563 | printk("%02x ", buf[i]); |
| 564 | } |
| 565 | |
| 566 | /* Terminate read pipeline */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 567 | buf[len - 2] = ReadDOC(docptr, Mplus_LastDataRead); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 568 | if (debug && i < 16) |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 569 | printk("%02x ", buf[len - 2]); |
| 570 | buf[len - 1] = ReadDOC(docptr, Mplus_LastDataRead); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 571 | if (debug && i < 16) |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 572 | printk("%02x ", buf[len - 1]); |
| 573 | if (debug) |
| 574 | printk("\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 575 | } |
| 576 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 577 | static void doc2001plus_select_chip(struct mtd_info *mtd, int chip) |
| 578 | { |
Boris BREZILLON | 4bd4ebc | 2015-12-01 12:03:04 +0100 | [diff] [blame] | 579 | struct nand_chip *this = mtd_to_nand(mtd); |
Boris BREZILLON | d699ed2 | 2015-12-10 09:00:41 +0100 | [diff] [blame] | 580 | struct doc_priv *doc = nand_get_controller_data(this); |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 581 | void __iomem *docptr = doc->virtadr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 582 | int floor = 0; |
| 583 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 584 | if (debug) |
| 585 | printk("select chip (%d)\n", chip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 586 | |
| 587 | if (chip == -1) { |
| 588 | /* Disable flash internally */ |
| 589 | WriteDOC(0, docptr, Mplus_FlashSelect); |
| 590 | return; |
| 591 | } |
| 592 | |
| 593 | floor = chip / doc->chips_per_floor; |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 594 | chip -= (floor * doc->chips_per_floor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 595 | |
| 596 | /* Assert ChipEnable and deassert WriteProtect */ |
| 597 | WriteDOC((DOC_FLASH_CE), docptr, Mplus_FlashSelect); |
| 598 | this->cmdfunc(mtd, NAND_CMD_RESET, -1, -1); |
| 599 | |
| 600 | doc->curchip = chip; |
| 601 | doc->curfloor = floor; |
| 602 | } |
| 603 | |
| 604 | static void doc200x_select_chip(struct mtd_info *mtd, int chip) |
| 605 | { |
Boris BREZILLON | 4bd4ebc | 2015-12-01 12:03:04 +0100 | [diff] [blame] | 606 | struct nand_chip *this = mtd_to_nand(mtd); |
Boris BREZILLON | d699ed2 | 2015-12-10 09:00:41 +0100 | [diff] [blame] | 607 | struct doc_priv *doc = nand_get_controller_data(this); |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 608 | void __iomem *docptr = doc->virtadr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 609 | int floor = 0; |
| 610 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 611 | if (debug) |
| 612 | printk("select chip (%d)\n", chip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 613 | |
| 614 | if (chip == -1) |
| 615 | return; |
| 616 | |
| 617 | floor = chip / doc->chips_per_floor; |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 618 | chip -= (floor * doc->chips_per_floor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 619 | |
| 620 | /* 11.4.4 -- deassert CE before changing chip */ |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 621 | doc200x_hwcontrol(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 622 | |
| 623 | WriteDOC(floor, docptr, FloorSelect); |
| 624 | WriteDOC(chip, docptr, CDSNDeviceSelect); |
| 625 | |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 626 | doc200x_hwcontrol(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 627 | |
| 628 | doc->curchip = chip; |
| 629 | doc->curfloor = floor; |
| 630 | } |
| 631 | |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 632 | #define CDSN_CTRL_MSK (CDSN_CTRL_CE | CDSN_CTRL_CLE | CDSN_CTRL_ALE) |
| 633 | |
| 634 | static void doc200x_hwcontrol(struct mtd_info *mtd, int cmd, |
| 635 | unsigned int ctrl) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 636 | { |
Boris BREZILLON | 4bd4ebc | 2015-12-01 12:03:04 +0100 | [diff] [blame] | 637 | struct nand_chip *this = mtd_to_nand(mtd); |
Boris BREZILLON | d699ed2 | 2015-12-10 09:00:41 +0100 | [diff] [blame] | 638 | struct doc_priv *doc = nand_get_controller_data(this); |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 639 | void __iomem *docptr = doc->virtadr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 640 | |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 641 | if (ctrl & NAND_CTRL_CHANGE) { |
| 642 | doc->CDSNControl &= ~CDSN_CTRL_MSK; |
| 643 | doc->CDSNControl |= ctrl & CDSN_CTRL_MSK; |
| 644 | if (debug) |
| 645 | printk("hwcontrol(%d): %02x\n", cmd, doc->CDSNControl); |
| 646 | WriteDOC(doc->CDSNControl, docptr, CDSNControl); |
| 647 | /* 11.4.3 -- 4 NOPs after CSDNControl write */ |
| 648 | DoC_Delay(doc, 4); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 649 | } |
Thomas Gleixner | cad74f2 | 2006-05-23 23:28:48 +0200 | [diff] [blame] | 650 | if (cmd != NAND_CMD_NONE) { |
| 651 | if (DoC_is_2000(doc)) |
| 652 | doc2000_write_byte(mtd, cmd); |
| 653 | else |
| 654 | doc2001_write_byte(mtd, cmd); |
| 655 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 656 | } |
| 657 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 658 | static void doc2001plus_command(struct mtd_info *mtd, unsigned command, int column, int page_addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 659 | { |
Boris BREZILLON | 4bd4ebc | 2015-12-01 12:03:04 +0100 | [diff] [blame] | 660 | struct nand_chip *this = mtd_to_nand(mtd); |
Boris BREZILLON | d699ed2 | 2015-12-10 09:00:41 +0100 | [diff] [blame] | 661 | struct doc_priv *doc = nand_get_controller_data(this); |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 662 | void __iomem *docptr = doc->virtadr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 663 | |
| 664 | /* |
| 665 | * Must terminate write pipeline before sending any commands |
| 666 | * to the device. |
| 667 | */ |
| 668 | if (command == NAND_CMD_PAGEPROG) { |
| 669 | WriteDOC(0x00, docptr, Mplus_WritePipeTerm); |
| 670 | WriteDOC(0x00, docptr, Mplus_WritePipeTerm); |
| 671 | } |
| 672 | |
| 673 | /* |
| 674 | * Write out the command to the device. |
| 675 | */ |
| 676 | if (command == NAND_CMD_SEQIN) { |
| 677 | int readcmd; |
| 678 | |
Joern Engel | 2831877 | 2006-05-22 23:18:05 +0200 | [diff] [blame] | 679 | if (column >= mtd->writesize) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 680 | /* OOB area */ |
Joern Engel | 2831877 | 2006-05-22 23:18:05 +0200 | [diff] [blame] | 681 | column -= mtd->writesize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 682 | readcmd = NAND_CMD_READOOB; |
| 683 | } else if (column < 256) { |
| 684 | /* First 256 bytes --> READ0 */ |
| 685 | readcmd = NAND_CMD_READ0; |
| 686 | } else { |
| 687 | column -= 256; |
| 688 | readcmd = NAND_CMD_READ1; |
| 689 | } |
| 690 | WriteDOC(readcmd, docptr, Mplus_FlashCmd); |
| 691 | } |
| 692 | WriteDOC(command, docptr, Mplus_FlashCmd); |
| 693 | WriteDOC(0, docptr, Mplus_WritePipeTerm); |
| 694 | WriteDOC(0, docptr, Mplus_WritePipeTerm); |
| 695 | |
| 696 | if (column != -1 || page_addr != -1) { |
| 697 | /* Serially input address */ |
| 698 | if (column != -1) { |
| 699 | /* Adjust columns for 16 bit buswidth */ |
Brian Norris | 3dad234 | 2014-01-29 14:08:12 -0800 | [diff] [blame] | 700 | if (this->options & NAND_BUSWIDTH_16 && |
| 701 | !nand_opcode_8bits(command)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 702 | column >>= 1; |
| 703 | WriteDOC(column, docptr, Mplus_FlashAddress); |
| 704 | } |
| 705 | if (page_addr != -1) { |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 706 | WriteDOC((unsigned char)(page_addr & 0xff), docptr, Mplus_FlashAddress); |
| 707 | WriteDOC((unsigned char)((page_addr >> 8) & 0xff), docptr, Mplus_FlashAddress); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 708 | /* One more address cycle for higher density devices */ |
| 709 | if (this->chipsize & 0x0c000000) { |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 710 | WriteDOC((unsigned char)((page_addr >> 16) & 0x0f), docptr, Mplus_FlashAddress); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 711 | printk("high density\n"); |
| 712 | } |
| 713 | } |
| 714 | WriteDOC(0, docptr, Mplus_WritePipeTerm); |
| 715 | WriteDOC(0, docptr, Mplus_WritePipeTerm); |
| 716 | /* deassert ALE */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 717 | if (command == NAND_CMD_READ0 || command == NAND_CMD_READ1 || |
| 718 | command == NAND_CMD_READOOB || command == NAND_CMD_READID) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 719 | WriteDOC(0, docptr, Mplus_FlashControl); |
| 720 | } |
| 721 | |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 722 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 723 | * program and erase have their own busy handlers |
| 724 | * status and sequential in needs no delay |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 725 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 726 | switch (command) { |
| 727 | |
| 728 | case NAND_CMD_PAGEPROG: |
| 729 | case NAND_CMD_ERASE1: |
| 730 | case NAND_CMD_ERASE2: |
| 731 | case NAND_CMD_SEQIN: |
| 732 | case NAND_CMD_STATUS: |
| 733 | return; |
| 734 | |
| 735 | case NAND_CMD_RESET: |
| 736 | if (this->dev_ready) |
| 737 | break; |
| 738 | udelay(this->chip_delay); |
| 739 | WriteDOC(NAND_CMD_STATUS, docptr, Mplus_FlashCmd); |
| 740 | WriteDOC(0, docptr, Mplus_WritePipeTerm); |
| 741 | WriteDOC(0, docptr, Mplus_WritePipeTerm); |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 742 | while (!(this->read_byte(mtd) & 0x40)) ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 743 | return; |
| 744 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 745 | /* This applies to read commands */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 746 | default: |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 747 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 748 | * If we don't have access to the busy pin, we apply the given |
| 749 | * command delay |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 750 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 751 | if (!this->dev_ready) { |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 752 | udelay(this->chip_delay); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 753 | return; |
| 754 | } |
| 755 | } |
| 756 | |
| 757 | /* Apply this short delay always to ensure that we do wait tWB in |
| 758 | * any case on any machine. */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 759 | ndelay(100); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 760 | /* wait until command is processed */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 761 | while (!this->dev_ready(mtd)) ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 762 | } |
| 763 | |
| 764 | static int doc200x_dev_ready(struct mtd_info *mtd) |
| 765 | { |
Boris BREZILLON | 4bd4ebc | 2015-12-01 12:03:04 +0100 | [diff] [blame] | 766 | struct nand_chip *this = mtd_to_nand(mtd); |
Boris BREZILLON | d699ed2 | 2015-12-10 09:00:41 +0100 | [diff] [blame] | 767 | struct doc_priv *doc = nand_get_controller_data(this); |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 768 | void __iomem *docptr = doc->virtadr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 769 | |
| 770 | if (DoC_is_MillenniumPlus(doc)) { |
| 771 | /* 11.4.2 -- must NOP four times before checking FR/B# */ |
| 772 | DoC_Delay(doc, 4); |
| 773 | if ((ReadDOC(docptr, Mplus_FlashControl) & CDSN_CTRL_FR_B_MASK) != CDSN_CTRL_FR_B_MASK) { |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 774 | if (debug) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 775 | printk("not ready\n"); |
| 776 | return 0; |
| 777 | } |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 778 | if (debug) |
| 779 | printk("was ready\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 780 | return 1; |
| 781 | } else { |
| 782 | /* 11.4.2 -- must NOP four times before checking FR/B# */ |
| 783 | DoC_Delay(doc, 4); |
| 784 | if (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B)) { |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 785 | if (debug) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 786 | printk("not ready\n"); |
| 787 | return 0; |
| 788 | } |
| 789 | /* 11.4.2 -- Must NOP twice if it's ready */ |
| 790 | DoC_Delay(doc, 2); |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 791 | if (debug) |
| 792 | printk("was ready\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 793 | return 1; |
| 794 | } |
| 795 | } |
| 796 | |
Archit Taneja | 9f3e042 | 2016-02-03 14:29:49 +0530 | [diff] [blame] | 797 | static int doc200x_block_bad(struct mtd_info *mtd, loff_t ofs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 798 | { |
| 799 | /* This is our last resort if we couldn't find or create a BBT. Just |
| 800 | pretend all blocks are good. */ |
| 801 | return 0; |
| 802 | } |
| 803 | |
| 804 | static void doc200x_enable_hwecc(struct mtd_info *mtd, int mode) |
| 805 | { |
Boris BREZILLON | 4bd4ebc | 2015-12-01 12:03:04 +0100 | [diff] [blame] | 806 | struct nand_chip *this = mtd_to_nand(mtd); |
Boris BREZILLON | d699ed2 | 2015-12-10 09:00:41 +0100 | [diff] [blame] | 807 | struct doc_priv *doc = nand_get_controller_data(this); |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 808 | void __iomem *docptr = doc->virtadr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 809 | |
| 810 | /* Prime the ECC engine */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 811 | switch (mode) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 812 | case NAND_ECC_READ: |
| 813 | WriteDOC(DOC_ECC_RESET, docptr, ECCConf); |
| 814 | WriteDOC(DOC_ECC_EN, docptr, ECCConf); |
| 815 | break; |
| 816 | case NAND_ECC_WRITE: |
| 817 | WriteDOC(DOC_ECC_RESET, docptr, ECCConf); |
| 818 | WriteDOC(DOC_ECC_EN | DOC_ECC_RW, docptr, ECCConf); |
| 819 | break; |
| 820 | } |
| 821 | } |
| 822 | |
| 823 | static void doc2001plus_enable_hwecc(struct mtd_info *mtd, int mode) |
| 824 | { |
Boris BREZILLON | 4bd4ebc | 2015-12-01 12:03:04 +0100 | [diff] [blame] | 825 | struct nand_chip *this = mtd_to_nand(mtd); |
Boris BREZILLON | d699ed2 | 2015-12-10 09:00:41 +0100 | [diff] [blame] | 826 | struct doc_priv *doc = nand_get_controller_data(this); |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 827 | void __iomem *docptr = doc->virtadr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 828 | |
| 829 | /* Prime the ECC engine */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 830 | switch (mode) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 831 | case NAND_ECC_READ: |
| 832 | WriteDOC(DOC_ECC_RESET, docptr, Mplus_ECCConf); |
| 833 | WriteDOC(DOC_ECC_EN, docptr, Mplus_ECCConf); |
| 834 | break; |
| 835 | case NAND_ECC_WRITE: |
| 836 | WriteDOC(DOC_ECC_RESET, docptr, Mplus_ECCConf); |
| 837 | WriteDOC(DOC_ECC_EN | DOC_ECC_RW, docptr, Mplus_ECCConf); |
| 838 | break; |
| 839 | } |
| 840 | } |
| 841 | |
| 842 | /* This code is only called on write */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 843 | static int doc200x_calculate_ecc(struct mtd_info *mtd, const u_char *dat, unsigned char *ecc_code) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 844 | { |
Boris BREZILLON | 4bd4ebc | 2015-12-01 12:03:04 +0100 | [diff] [blame] | 845 | struct nand_chip *this = mtd_to_nand(mtd); |
Boris BREZILLON | d699ed2 | 2015-12-10 09:00:41 +0100 | [diff] [blame] | 846 | struct doc_priv *doc = nand_get_controller_data(this); |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 847 | void __iomem *docptr = doc->virtadr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 848 | int i; |
| 849 | int emptymatch = 1; |
| 850 | |
| 851 | /* flush the pipeline */ |
| 852 | if (DoC_is_2000(doc)) { |
| 853 | WriteDOC(doc->CDSNControl & ~CDSN_CTRL_FLASH_IO, docptr, CDSNControl); |
| 854 | WriteDOC(0, docptr, 2k_CDSN_IO); |
| 855 | WriteDOC(0, docptr, 2k_CDSN_IO); |
| 856 | WriteDOC(0, docptr, 2k_CDSN_IO); |
| 857 | WriteDOC(doc->CDSNControl, docptr, CDSNControl); |
| 858 | } else if (DoC_is_MillenniumPlus(doc)) { |
| 859 | WriteDOC(0, docptr, Mplus_NOP); |
| 860 | WriteDOC(0, docptr, Mplus_NOP); |
| 861 | WriteDOC(0, docptr, Mplus_NOP); |
| 862 | } else { |
| 863 | WriteDOC(0, docptr, NOP); |
| 864 | WriteDOC(0, docptr, NOP); |
| 865 | WriteDOC(0, docptr, NOP); |
| 866 | } |
| 867 | |
| 868 | for (i = 0; i < 6; i++) { |
| 869 | if (DoC_is_MillenniumPlus(doc)) |
| 870 | ecc_code[i] = ReadDOC_(docptr, DoC_Mplus_ECCSyndrome0 + i); |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 871 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 872 | ecc_code[i] = ReadDOC_(docptr, DoC_ECCSyndrome0 + i); |
| 873 | if (ecc_code[i] != empty_write_ecc[i]) |
| 874 | emptymatch = 0; |
| 875 | } |
| 876 | if (DoC_is_MillenniumPlus(doc)) |
| 877 | WriteDOC(DOC_ECC_DIS, docptr, Mplus_ECCConf); |
| 878 | else |
| 879 | WriteDOC(DOC_ECC_DIS, docptr, ECCConf); |
| 880 | #if 0 |
| 881 | /* If emptymatch=1, we might have an all-0xff data buffer. Check. */ |
| 882 | if (emptymatch) { |
| 883 | /* Note: this somewhat expensive test should not be triggered |
| 884 | often. It could be optimized away by examining the data in |
| 885 | the writebuf routine, and remembering the result. */ |
| 886 | for (i = 0; i < 512; i++) { |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 887 | if (dat[i] == 0xff) |
| 888 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 889 | emptymatch = 0; |
| 890 | break; |
| 891 | } |
| 892 | } |
| 893 | /* If emptymatch still =1, we do have an all-0xff data buffer. |
| 894 | Return all-0xff ecc value instead of the computed one, so |
| 895 | it'll look just like a freshly-erased page. */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 896 | if (emptymatch) |
| 897 | memset(ecc_code, 0xff, 6); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 898 | #endif |
| 899 | return 0; |
| 900 | } |
| 901 | |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 902 | static int doc200x_correct_data(struct mtd_info *mtd, u_char *dat, |
| 903 | u_char *read_ecc, u_char *isnull) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 904 | { |
| 905 | int i, ret = 0; |
Boris BREZILLON | 4bd4ebc | 2015-12-01 12:03:04 +0100 | [diff] [blame] | 906 | struct nand_chip *this = mtd_to_nand(mtd); |
Boris BREZILLON | d699ed2 | 2015-12-10 09:00:41 +0100 | [diff] [blame] | 907 | struct doc_priv *doc = nand_get_controller_data(this); |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 908 | void __iomem *docptr = doc->virtadr; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 909 | uint8_t calc_ecc[6]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 910 | volatile u_char dummy; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 911 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 912 | /* flush the pipeline */ |
| 913 | if (DoC_is_2000(doc)) { |
| 914 | dummy = ReadDOC(docptr, 2k_ECCStatus); |
| 915 | dummy = ReadDOC(docptr, 2k_ECCStatus); |
| 916 | dummy = ReadDOC(docptr, 2k_ECCStatus); |
| 917 | } else if (DoC_is_MillenniumPlus(doc)) { |
| 918 | dummy = ReadDOC(docptr, Mplus_ECCConf); |
| 919 | dummy = ReadDOC(docptr, Mplus_ECCConf); |
| 920 | dummy = ReadDOC(docptr, Mplus_ECCConf); |
| 921 | } else { |
| 922 | dummy = ReadDOC(docptr, ECCConf); |
| 923 | dummy = ReadDOC(docptr, ECCConf); |
| 924 | dummy = ReadDOC(docptr, ECCConf); |
| 925 | } |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 926 | |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 927 | /* Error occurred ? */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 928 | if (dummy & 0x80) { |
| 929 | for (i = 0; i < 6; i++) { |
| 930 | if (DoC_is_MillenniumPlus(doc)) |
| 931 | calc_ecc[i] = ReadDOC_(docptr, DoC_Mplus_ECCSyndrome0 + i); |
| 932 | else |
| 933 | calc_ecc[i] = ReadDOC_(docptr, DoC_ECCSyndrome0 + i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 934 | } |
Boris BREZILLON | cc01e60 | 2015-12-30 20:39:52 +0100 | [diff] [blame] | 935 | |
| 936 | ret = doc_ecc_decode(rs_decoder, dat, calc_ecc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 937 | if (ret > 0) |
| 938 | printk(KERN_ERR "doc200x_correct_data corrected %d errors\n", ret); |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 939 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 940 | if (DoC_is_MillenniumPlus(doc)) |
| 941 | WriteDOC(DOC_ECC_DIS, docptr, Mplus_ECCConf); |
| 942 | else |
| 943 | WriteDOC(DOC_ECC_DIS, docptr, ECCConf); |
Brian Norris | d57f4054 | 2011-09-20 18:34:25 -0700 | [diff] [blame] | 944 | if (no_ecc_failures && mtd_is_eccerr(ret)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 945 | printk(KERN_ERR "suppressing ECC failure\n"); |
| 946 | ret = 0; |
| 947 | } |
| 948 | return ret; |
| 949 | } |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 950 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 951 | //u_char mydatabuf[528]; |
| 952 | |
Boris Brezillon | 68c1b75 | 2016-02-03 20:00:23 +0100 | [diff] [blame] | 953 | static int doc200x_ooblayout_ecc(struct mtd_info *mtd, int section, |
| 954 | struct mtd_oob_region *oobregion) |
| 955 | { |
| 956 | if (section) |
| 957 | return -ERANGE; |
| 958 | |
| 959 | oobregion->offset = 0; |
| 960 | oobregion->length = 6; |
| 961 | |
| 962 | return 0; |
| 963 | } |
| 964 | |
| 965 | static int doc200x_ooblayout_free(struct mtd_info *mtd, int section, |
| 966 | struct mtd_oob_region *oobregion) |
| 967 | { |
| 968 | if (section > 1) |
| 969 | return -ERANGE; |
| 970 | |
| 971 | /* |
| 972 | * The strange out-of-order free bytes definition is a (possibly |
| 973 | * unneeded) attempt to retain compatibility. It used to read: |
| 974 | * .oobfree = { {8, 8} } |
| 975 | * Since that leaves two bytes unusable, it was changed. But the |
| 976 | * following scheme might affect existing jffs2 installs by moving the |
| 977 | * cleanmarker: |
| 978 | * .oobfree = { {6, 10} } |
| 979 | * jffs2 seems to handle the above gracefully, but the current scheme |
| 980 | * seems safer. The only problem with it is that any code retrieving |
| 981 | * free bytes position must be able to handle out-of-order segments. |
| 982 | */ |
| 983 | if (!section) { |
| 984 | oobregion->offset = 8; |
| 985 | oobregion->length = 8; |
| 986 | } else { |
| 987 | oobregion->offset = 6; |
| 988 | oobregion->length = 2; |
| 989 | } |
| 990 | |
| 991 | return 0; |
| 992 | } |
| 993 | |
| 994 | static const struct mtd_ooblayout_ops doc200x_ooblayout_ops = { |
| 995 | .ecc = doc200x_ooblayout_ecc, |
| 996 | .free = doc200x_ooblayout_free, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 997 | }; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 998 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 999 | /* Find the (I)NFTL Media Header, and optionally also the mirror media header. |
André Goddard Rosa | af901ca | 2009-11-14 13:09:05 -0200 | [diff] [blame] | 1000 | On successful return, buf will contain a copy of the media header for |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1001 | further processing. id is the string to scan for, and will presumably be |
| 1002 | either "ANAND" or "BNAND". If findmirror=1, also look for the mirror media |
| 1003 | header. The page #s of the found media headers are placed in mh0_page and |
| 1004 | mh1_page in the DOC private structure. */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1005 | static int __init find_media_headers(struct mtd_info *mtd, u_char *buf, const char *id, int findmirror) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1006 | { |
Boris BREZILLON | 4bd4ebc | 2015-12-01 12:03:04 +0100 | [diff] [blame] | 1007 | struct nand_chip *this = mtd_to_nand(mtd); |
Boris BREZILLON | d699ed2 | 2015-12-10 09:00:41 +0100 | [diff] [blame] | 1008 | struct doc_priv *doc = nand_get_controller_data(this); |
Dan Brown | 1a78ff6 | 2005-03-29 21:57:48 +0100 | [diff] [blame] | 1009 | unsigned offs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1010 | int ret; |
| 1011 | size_t retlen; |
| 1012 | |
Dan Brown | 1a78ff6 | 2005-03-29 21:57:48 +0100 | [diff] [blame] | 1013 | for (offs = 0; offs < mtd->size; offs += mtd->erasesize) { |
Artem Bityutskiy | 329ad39 | 2011-12-23 17:30:16 +0200 | [diff] [blame] | 1014 | ret = mtd_read(mtd, offs, mtd->writesize, &retlen, buf); |
Joern Engel | 2831877 | 2006-05-22 23:18:05 +0200 | [diff] [blame] | 1015 | if (retlen != mtd->writesize) |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1016 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1017 | if (ret) { |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1018 | printk(KERN_WARNING "ECC error scanning DOC at 0x%x\n", offs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1019 | } |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1020 | if (memcmp(buf, id, 6)) |
| 1021 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1022 | printk(KERN_INFO "Found DiskOnChip %s Media Header at 0x%x\n", id, offs); |
| 1023 | if (doc->mh0_page == -1) { |
| 1024 | doc->mh0_page = offs >> this->page_shift; |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1025 | if (!findmirror) |
| 1026 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1027 | continue; |
| 1028 | } |
| 1029 | doc->mh1_page = offs >> this->page_shift; |
| 1030 | return 2; |
| 1031 | } |
| 1032 | if (doc->mh0_page == -1) { |
| 1033 | printk(KERN_WARNING "DiskOnChip %s Media Header not found.\n", id); |
| 1034 | return 0; |
| 1035 | } |
| 1036 | /* Only one mediaheader was found. We want buf to contain a |
| 1037 | mediaheader on return, so we'll have to re-read the one we found. */ |
| 1038 | offs = doc->mh0_page << this->page_shift; |
Artem Bityutskiy | 329ad39 | 2011-12-23 17:30:16 +0200 | [diff] [blame] | 1039 | ret = mtd_read(mtd, offs, mtd->writesize, &retlen, buf); |
Joern Engel | 2831877 | 2006-05-22 23:18:05 +0200 | [diff] [blame] | 1040 | if (retlen != mtd->writesize) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1041 | /* Insanity. Give up. */ |
| 1042 | printk(KERN_ERR "Read DiskOnChip Media Header once, but can't reread it???\n"); |
| 1043 | return 0; |
| 1044 | } |
| 1045 | return 1; |
| 1046 | } |
| 1047 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1048 | static inline int __init nftl_partscan(struct mtd_info *mtd, struct mtd_partition *parts) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1049 | { |
Boris BREZILLON | 4bd4ebc | 2015-12-01 12:03:04 +0100 | [diff] [blame] | 1050 | struct nand_chip *this = mtd_to_nand(mtd); |
Boris BREZILLON | d699ed2 | 2015-12-10 09:00:41 +0100 | [diff] [blame] | 1051 | struct doc_priv *doc = nand_get_controller_data(this); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1052 | int ret = 0; |
| 1053 | u_char *buf; |
| 1054 | struct NFTLMediaHeader *mh; |
| 1055 | const unsigned psize = 1 << this->page_shift; |
Dan Brown | 1a78ff6 | 2005-03-29 21:57:48 +0100 | [diff] [blame] | 1056 | int numparts = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1057 | unsigned blocks, maxblocks; |
| 1058 | int offs, numheaders; |
| 1059 | |
Joern Engel | 2831877 | 2006-05-22 23:18:05 +0200 | [diff] [blame] | 1060 | buf = kmalloc(mtd->writesize, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1061 | if (!buf) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1062 | return 0; |
| 1063 | } |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1064 | if (!(numheaders = find_media_headers(mtd, buf, "ANAND", 1))) |
| 1065 | goto out; |
| 1066 | mh = (struct NFTLMediaHeader *)buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1067 | |
Harvey Harrison | 9637244 | 2008-07-30 12:34:57 -0700 | [diff] [blame] | 1068 | le16_to_cpus(&mh->NumEraseUnits); |
| 1069 | le16_to_cpus(&mh->FirstPhysicalEUN); |
| 1070 | le32_to_cpus(&mh->FormattedSize); |
Thomas Gleixner | f29a4b8 | 2005-01-31 22:22:24 +0000 | [diff] [blame] | 1071 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1072 | printk(KERN_INFO " DataOrgID = %s\n" |
| 1073 | " NumEraseUnits = %d\n" |
| 1074 | " FirstPhysicalEUN = %d\n" |
| 1075 | " FormattedSize = %d\n" |
| 1076 | " UnitSizeFactor = %d\n", |
| 1077 | mh->DataOrgID, mh->NumEraseUnits, |
| 1078 | mh->FirstPhysicalEUN, mh->FormattedSize, |
| 1079 | mh->UnitSizeFactor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1080 | |
| 1081 | blocks = mtd->size >> this->phys_erase_shift; |
| 1082 | maxblocks = min(32768U, mtd->erasesize - psize); |
| 1083 | |
| 1084 | if (mh->UnitSizeFactor == 0x00) { |
| 1085 | /* Auto-determine UnitSizeFactor. The constraints are: |
| 1086 | - There can be at most 32768 virtual blocks. |
| 1087 | - There can be at most (virtual block size - page size) |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1088 | virtual blocks (because MediaHeader+BBT must fit in 1). |
| 1089 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1090 | mh->UnitSizeFactor = 0xff; |
| 1091 | while (blocks > maxblocks) { |
| 1092 | blocks >>= 1; |
| 1093 | maxblocks = min(32768U, (maxblocks << 1) + psize); |
| 1094 | mh->UnitSizeFactor--; |
| 1095 | } |
| 1096 | printk(KERN_WARNING "UnitSizeFactor=0x00 detected. Correct value is assumed to be 0x%02x.\n", mh->UnitSizeFactor); |
| 1097 | } |
| 1098 | |
| 1099 | /* NOTE: The lines below modify internal variables of the NAND and MTD |
| 1100 | layers; variables with have already been configured by nand_scan. |
| 1101 | Unfortunately, we didn't know before this point what these values |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 1102 | should be. Thus, this code is somewhat dependent on the exact |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1103 | implementation of the NAND layer. */ |
| 1104 | if (mh->UnitSizeFactor != 0xff) { |
| 1105 | this->bbt_erase_shift += (0xff - mh->UnitSizeFactor); |
| 1106 | mtd->erasesize <<= (0xff - mh->UnitSizeFactor); |
| 1107 | printk(KERN_INFO "Setting virtual erase size to %d\n", mtd->erasesize); |
| 1108 | blocks = mtd->size >> this->bbt_erase_shift; |
| 1109 | maxblocks = min(32768U, mtd->erasesize - psize); |
| 1110 | } |
| 1111 | |
| 1112 | if (blocks > maxblocks) { |
| 1113 | printk(KERN_ERR "UnitSizeFactor of 0x%02x is inconsistent with device size. Aborting.\n", mh->UnitSizeFactor); |
| 1114 | goto out; |
| 1115 | } |
| 1116 | |
| 1117 | /* Skip past the media headers. */ |
| 1118 | offs = max(doc->mh0_page, doc->mh1_page); |
| 1119 | offs <<= this->page_shift; |
| 1120 | offs += mtd->erasesize; |
| 1121 | |
Dan Brown | 1a78ff6 | 2005-03-29 21:57:48 +0100 | [diff] [blame] | 1122 | if (show_firmware_partition == 1) { |
| 1123 | parts[0].name = " DiskOnChip Firmware / Media Header partition"; |
| 1124 | parts[0].offset = 0; |
| 1125 | parts[0].size = offs; |
| 1126 | numparts = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1127 | } |
Dan Brown | 1a78ff6 | 2005-03-29 21:57:48 +0100 | [diff] [blame] | 1128 | |
| 1129 | parts[numparts].name = " DiskOnChip BDTL partition"; |
| 1130 | parts[numparts].offset = offs; |
| 1131 | parts[numparts].size = (mh->NumEraseUnits - numheaders) << this->bbt_erase_shift; |
| 1132 | |
| 1133 | offs += parts[numparts].size; |
| 1134 | numparts++; |
| 1135 | |
| 1136 | if (offs < mtd->size) { |
| 1137 | parts[numparts].name = " DiskOnChip Remainder partition"; |
| 1138 | parts[numparts].offset = offs; |
| 1139 | parts[numparts].size = mtd->size - offs; |
| 1140 | numparts++; |
| 1141 | } |
| 1142 | |
| 1143 | ret = numparts; |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1144 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1145 | kfree(buf); |
| 1146 | return ret; |
| 1147 | } |
| 1148 | |
| 1149 | /* This is a stripped-down copy of the code in inftlmount.c */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1150 | static inline int __init inftl_partscan(struct mtd_info *mtd, struct mtd_partition *parts) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1151 | { |
Boris BREZILLON | 4bd4ebc | 2015-12-01 12:03:04 +0100 | [diff] [blame] | 1152 | struct nand_chip *this = mtd_to_nand(mtd); |
Boris BREZILLON | d699ed2 | 2015-12-10 09:00:41 +0100 | [diff] [blame] | 1153 | struct doc_priv *doc = nand_get_controller_data(this); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1154 | int ret = 0; |
| 1155 | u_char *buf; |
| 1156 | struct INFTLMediaHeader *mh; |
| 1157 | struct INFTLPartition *ip; |
| 1158 | int numparts = 0; |
| 1159 | int blocks; |
| 1160 | int vshift, lastvunit = 0; |
| 1161 | int i; |
| 1162 | int end = mtd->size; |
| 1163 | |
| 1164 | if (inftl_bbt_write) |
| 1165 | end -= (INFTL_BBT_RESERVED_BLOCKS << this->phys_erase_shift); |
| 1166 | |
Joern Engel | 2831877 | 2006-05-22 23:18:05 +0200 | [diff] [blame] | 1167 | buf = kmalloc(mtd->writesize, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1168 | if (!buf) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1169 | return 0; |
| 1170 | } |
| 1171 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1172 | if (!find_media_headers(mtd, buf, "BNAND", 0)) |
| 1173 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1174 | doc->mh1_page = doc->mh0_page + (4096 >> this->page_shift); |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1175 | mh = (struct INFTLMediaHeader *)buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1176 | |
Harvey Harrison | 9637244 | 2008-07-30 12:34:57 -0700 | [diff] [blame] | 1177 | le32_to_cpus(&mh->NoOfBootImageBlocks); |
| 1178 | le32_to_cpus(&mh->NoOfBinaryPartitions); |
| 1179 | le32_to_cpus(&mh->NoOfBDTLPartitions); |
| 1180 | le32_to_cpus(&mh->BlockMultiplierBits); |
| 1181 | le32_to_cpus(&mh->FormatFlags); |
| 1182 | le32_to_cpus(&mh->PercentUsed); |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1183 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1184 | printk(KERN_INFO " bootRecordID = %s\n" |
| 1185 | " NoOfBootImageBlocks = %d\n" |
| 1186 | " NoOfBinaryPartitions = %d\n" |
| 1187 | " NoOfBDTLPartitions = %d\n" |
| 1188 | " BlockMultiplerBits = %d\n" |
| 1189 | " FormatFlgs = %d\n" |
| 1190 | " OsakVersion = %d.%d.%d.%d\n" |
| 1191 | " PercentUsed = %d\n", |
| 1192 | mh->bootRecordID, mh->NoOfBootImageBlocks, |
| 1193 | mh->NoOfBinaryPartitions, |
| 1194 | mh->NoOfBDTLPartitions, |
| 1195 | mh->BlockMultiplierBits, mh->FormatFlags, |
| 1196 | ((unsigned char *) &mh->OsakVersion)[0] & 0xf, |
| 1197 | ((unsigned char *) &mh->OsakVersion)[1] & 0xf, |
| 1198 | ((unsigned char *) &mh->OsakVersion)[2] & 0xf, |
| 1199 | ((unsigned char *) &mh->OsakVersion)[3] & 0xf, |
| 1200 | mh->PercentUsed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1201 | |
| 1202 | vshift = this->phys_erase_shift + mh->BlockMultiplierBits; |
| 1203 | |
| 1204 | blocks = mtd->size >> vshift; |
| 1205 | if (blocks > 32768) { |
| 1206 | printk(KERN_ERR "BlockMultiplierBits=%d is inconsistent with device size. Aborting.\n", mh->BlockMultiplierBits); |
| 1207 | goto out; |
| 1208 | } |
| 1209 | |
| 1210 | blocks = doc->chips_per_floor << (this->chip_shift - this->phys_erase_shift); |
| 1211 | if (inftl_bbt_write && (blocks > mtd->erasesize)) { |
| 1212 | printk(KERN_ERR "Writeable BBTs spanning more than one erase block are not yet supported. FIX ME!\n"); |
| 1213 | goto out; |
| 1214 | } |
| 1215 | |
| 1216 | /* Scan the partitions */ |
| 1217 | for (i = 0; (i < 4); i++) { |
| 1218 | ip = &(mh->Partitions[i]); |
Harvey Harrison | 9637244 | 2008-07-30 12:34:57 -0700 | [diff] [blame] | 1219 | le32_to_cpus(&ip->virtualUnits); |
| 1220 | le32_to_cpus(&ip->firstUnit); |
| 1221 | le32_to_cpus(&ip->lastUnit); |
| 1222 | le32_to_cpus(&ip->flags); |
| 1223 | le32_to_cpus(&ip->spareUnits); |
| 1224 | le32_to_cpus(&ip->Reserved0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1225 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1226 | printk(KERN_INFO " PARTITION[%d] ->\n" |
| 1227 | " virtualUnits = %d\n" |
| 1228 | " firstUnit = %d\n" |
| 1229 | " lastUnit = %d\n" |
| 1230 | " flags = 0x%x\n" |
| 1231 | " spareUnits = %d\n", |
| 1232 | i, ip->virtualUnits, ip->firstUnit, |
| 1233 | ip->lastUnit, ip->flags, |
| 1234 | ip->spareUnits); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1235 | |
Dan Brown | 1a78ff6 | 2005-03-29 21:57:48 +0100 | [diff] [blame] | 1236 | if ((show_firmware_partition == 1) && |
| 1237 | (i == 0) && (ip->firstUnit > 0)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1238 | parts[0].name = " DiskOnChip IPL / Media Header partition"; |
| 1239 | parts[0].offset = 0; |
| 1240 | parts[0].size = mtd->erasesize * ip->firstUnit; |
| 1241 | numparts = 1; |
| 1242 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1243 | |
| 1244 | if (ip->flags & INFTL_BINARY) |
| 1245 | parts[numparts].name = " DiskOnChip BDK partition"; |
| 1246 | else |
| 1247 | parts[numparts].name = " DiskOnChip BDTL partition"; |
| 1248 | parts[numparts].offset = ip->firstUnit << vshift; |
| 1249 | parts[numparts].size = (1 + ip->lastUnit - ip->firstUnit) << vshift; |
| 1250 | numparts++; |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1251 | if (ip->lastUnit > lastvunit) |
| 1252 | lastvunit = ip->lastUnit; |
| 1253 | if (ip->flags & INFTL_LAST) |
| 1254 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1255 | } |
| 1256 | lastvunit++; |
| 1257 | if ((lastvunit << vshift) < end) { |
| 1258 | parts[numparts].name = " DiskOnChip Remainder partition"; |
| 1259 | parts[numparts].offset = lastvunit << vshift; |
| 1260 | parts[numparts].size = end - parts[numparts].offset; |
| 1261 | numparts++; |
| 1262 | } |
| 1263 | ret = numparts; |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1264 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1265 | kfree(buf); |
| 1266 | return ret; |
| 1267 | } |
| 1268 | |
| 1269 | static int __init nftl_scan_bbt(struct mtd_info *mtd) |
| 1270 | { |
| 1271 | int ret, numparts; |
Boris BREZILLON | 4bd4ebc | 2015-12-01 12:03:04 +0100 | [diff] [blame] | 1272 | struct nand_chip *this = mtd_to_nand(mtd); |
Boris BREZILLON | d699ed2 | 2015-12-10 09:00:41 +0100 | [diff] [blame] | 1273 | struct doc_priv *doc = nand_get_controller_data(this); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1274 | struct mtd_partition parts[2]; |
| 1275 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1276 | memset((char *)parts, 0, sizeof(parts)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1277 | /* On NFTL, we have to find the media headers before we can read the |
| 1278 | BBTs, since they're stored in the media header eraseblocks. */ |
| 1279 | numparts = nftl_partscan(mtd, parts); |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1280 | if (!numparts) |
| 1281 | return -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1282 | this->bbt_td->options = NAND_BBT_ABSPAGE | NAND_BBT_8BIT | |
| 1283 | NAND_BBT_SAVECONTENT | NAND_BBT_WRITE | |
| 1284 | NAND_BBT_VERSION; |
| 1285 | this->bbt_td->veroffs = 7; |
| 1286 | this->bbt_td->pages[0] = doc->mh0_page + 1; |
| 1287 | if (doc->mh1_page != -1) { |
| 1288 | this->bbt_md->options = NAND_BBT_ABSPAGE | NAND_BBT_8BIT | |
| 1289 | NAND_BBT_SAVECONTENT | NAND_BBT_WRITE | |
| 1290 | NAND_BBT_VERSION; |
| 1291 | this->bbt_md->veroffs = 7; |
| 1292 | this->bbt_md->pages[0] = doc->mh1_page + 1; |
| 1293 | } else { |
| 1294 | this->bbt_md = NULL; |
| 1295 | } |
| 1296 | |
Brian Norris | d24fe0c | 2015-02-28 02:13:10 -0800 | [diff] [blame] | 1297 | ret = this->scan_bbt(mtd); |
| 1298 | if (ret) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1299 | return ret; |
Brian Norris | d24fe0c | 2015-02-28 02:13:10 -0800 | [diff] [blame] | 1300 | |
Brian Norris | 6a7c733 | 2015-06-01 16:17:17 -0700 | [diff] [blame] | 1301 | return mtd_device_register(mtd, parts, no_autopart ? 0 : numparts); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1302 | } |
| 1303 | |
| 1304 | static int __init inftl_scan_bbt(struct mtd_info *mtd) |
| 1305 | { |
| 1306 | int ret, numparts; |
Boris BREZILLON | 4bd4ebc | 2015-12-01 12:03:04 +0100 | [diff] [blame] | 1307 | struct nand_chip *this = mtd_to_nand(mtd); |
Boris BREZILLON | d699ed2 | 2015-12-10 09:00:41 +0100 | [diff] [blame] | 1308 | struct doc_priv *doc = nand_get_controller_data(this); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1309 | struct mtd_partition parts[5]; |
| 1310 | |
| 1311 | if (this->numchips > doc->chips_per_floor) { |
| 1312 | printk(KERN_ERR "Multi-floor INFTL devices not yet supported.\n"); |
| 1313 | return -EIO; |
| 1314 | } |
| 1315 | |
| 1316 | if (DoC_is_MillenniumPlus(doc)) { |
| 1317 | this->bbt_td->options = NAND_BBT_2BIT | NAND_BBT_ABSPAGE; |
| 1318 | if (inftl_bbt_write) |
| 1319 | this->bbt_td->options |= NAND_BBT_WRITE; |
| 1320 | this->bbt_td->pages[0] = 2; |
| 1321 | this->bbt_md = NULL; |
| 1322 | } else { |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1323 | this->bbt_td->options = NAND_BBT_LASTBLOCK | NAND_BBT_8BIT | NAND_BBT_VERSION; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1324 | if (inftl_bbt_write) |
| 1325 | this->bbt_td->options |= NAND_BBT_WRITE; |
| 1326 | this->bbt_td->offs = 8; |
| 1327 | this->bbt_td->len = 8; |
| 1328 | this->bbt_td->veroffs = 7; |
| 1329 | this->bbt_td->maxblocks = INFTL_BBT_RESERVED_BLOCKS; |
| 1330 | this->bbt_td->reserved_block_code = 0x01; |
| 1331 | this->bbt_td->pattern = "MSYS_BBT"; |
| 1332 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1333 | this->bbt_md->options = NAND_BBT_LASTBLOCK | NAND_BBT_8BIT | NAND_BBT_VERSION; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1334 | if (inftl_bbt_write) |
| 1335 | this->bbt_md->options |= NAND_BBT_WRITE; |
| 1336 | this->bbt_md->offs = 8; |
| 1337 | this->bbt_md->len = 8; |
| 1338 | this->bbt_md->veroffs = 7; |
| 1339 | this->bbt_md->maxblocks = INFTL_BBT_RESERVED_BLOCKS; |
| 1340 | this->bbt_md->reserved_block_code = 0x01; |
| 1341 | this->bbt_md->pattern = "TBB_SYSM"; |
| 1342 | } |
| 1343 | |
Brian Norris | d24fe0c | 2015-02-28 02:13:10 -0800 | [diff] [blame] | 1344 | ret = this->scan_bbt(mtd); |
| 1345 | if (ret) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1346 | return ret; |
Brian Norris | d24fe0c | 2015-02-28 02:13:10 -0800 | [diff] [blame] | 1347 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1348 | memset((char *)parts, 0, sizeof(parts)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1349 | numparts = inftl_partscan(mtd, parts); |
| 1350 | /* At least for now, require the INFTL Media Header. We could probably |
| 1351 | do without it for non-INFTL use, since all it gives us is |
| 1352 | autopartitioning, but I want to give it more thought. */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1353 | if (!numparts) |
| 1354 | return -EIO; |
Brian Norris | 6a7c733 | 2015-06-01 16:17:17 -0700 | [diff] [blame] | 1355 | return mtd_device_register(mtd, parts, no_autopart ? 0 : numparts); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1356 | } |
| 1357 | |
| 1358 | static inline int __init doc2000_init(struct mtd_info *mtd) |
| 1359 | { |
Boris BREZILLON | 4bd4ebc | 2015-12-01 12:03:04 +0100 | [diff] [blame] | 1360 | struct nand_chip *this = mtd_to_nand(mtd); |
Boris BREZILLON | d699ed2 | 2015-12-10 09:00:41 +0100 | [diff] [blame] | 1361 | struct doc_priv *doc = nand_get_controller_data(this); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1362 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1363 | this->read_byte = doc2000_read_byte; |
| 1364 | this->write_buf = doc2000_writebuf; |
| 1365 | this->read_buf = doc2000_readbuf; |
Brian Norris | d24fe0c | 2015-02-28 02:13:10 -0800 | [diff] [blame] | 1366 | doc->late_init = nftl_scan_bbt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1367 | |
| 1368 | doc->CDSNControl = CDSN_CTRL_FLASH_IO | CDSN_CTRL_ECC_IO; |
| 1369 | doc2000_count_chips(mtd); |
| 1370 | mtd->name = "DiskOnChip 2000 (NFTL Model)"; |
| 1371 | return (4 * doc->chips_per_floor); |
| 1372 | } |
| 1373 | |
| 1374 | static inline int __init doc2001_init(struct mtd_info *mtd) |
| 1375 | { |
Boris BREZILLON | 4bd4ebc | 2015-12-01 12:03:04 +0100 | [diff] [blame] | 1376 | struct nand_chip *this = mtd_to_nand(mtd); |
Boris BREZILLON | d699ed2 | 2015-12-10 09:00:41 +0100 | [diff] [blame] | 1377 | struct doc_priv *doc = nand_get_controller_data(this); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1378 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1379 | this->read_byte = doc2001_read_byte; |
| 1380 | this->write_buf = doc2001_writebuf; |
| 1381 | this->read_buf = doc2001_readbuf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1382 | |
| 1383 | ReadDOC(doc->virtadr, ChipID); |
| 1384 | ReadDOC(doc->virtadr, ChipID); |
| 1385 | ReadDOC(doc->virtadr, ChipID); |
| 1386 | if (ReadDOC(doc->virtadr, ChipID) != DOC_ChipID_DocMil) { |
| 1387 | /* It's not a Millennium; it's one of the newer |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1388 | DiskOnChip 2000 units with a similar ASIC. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1389 | Treat it like a Millennium, except that it |
| 1390 | can have multiple chips. */ |
| 1391 | doc2000_count_chips(mtd); |
| 1392 | mtd->name = "DiskOnChip 2000 (INFTL Model)"; |
Brian Norris | d24fe0c | 2015-02-28 02:13:10 -0800 | [diff] [blame] | 1393 | doc->late_init = inftl_scan_bbt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1394 | return (4 * doc->chips_per_floor); |
| 1395 | } else { |
| 1396 | /* Bog-standard Millennium */ |
| 1397 | doc->chips_per_floor = 1; |
| 1398 | mtd->name = "DiskOnChip Millennium"; |
Brian Norris | d24fe0c | 2015-02-28 02:13:10 -0800 | [diff] [blame] | 1399 | doc->late_init = nftl_scan_bbt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1400 | return 1; |
| 1401 | } |
| 1402 | } |
| 1403 | |
| 1404 | static inline int __init doc2001plus_init(struct mtd_info *mtd) |
| 1405 | { |
Boris BREZILLON | 4bd4ebc | 2015-12-01 12:03:04 +0100 | [diff] [blame] | 1406 | struct nand_chip *this = mtd_to_nand(mtd); |
Boris BREZILLON | d699ed2 | 2015-12-10 09:00:41 +0100 | [diff] [blame] | 1407 | struct doc_priv *doc = nand_get_controller_data(this); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1408 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1409 | this->read_byte = doc2001plus_read_byte; |
| 1410 | this->write_buf = doc2001plus_writebuf; |
| 1411 | this->read_buf = doc2001plus_readbuf; |
Brian Norris | d24fe0c | 2015-02-28 02:13:10 -0800 | [diff] [blame] | 1412 | doc->late_init = inftl_scan_bbt; |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 1413 | this->cmd_ctrl = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1414 | this->select_chip = doc2001plus_select_chip; |
| 1415 | this->cmdfunc = doc2001plus_command; |
Thomas Gleixner | 0cddd6c | 2006-05-23 15:59:58 +0200 | [diff] [blame] | 1416 | this->ecc.hwctl = doc2001plus_enable_hwecc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1417 | |
| 1418 | doc->chips_per_floor = 1; |
| 1419 | mtd->name = "DiskOnChip Millennium Plus"; |
| 1420 | |
| 1421 | return 1; |
| 1422 | } |
| 1423 | |
Arjan van de Ven | 858119e | 2006-01-14 13:20:43 -0800 | [diff] [blame] | 1424 | static int __init doc_probe(unsigned long physadr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1425 | { |
| 1426 | unsigned char ChipID; |
| 1427 | struct mtd_info *mtd; |
| 1428 | struct nand_chip *nand; |
| 1429 | struct doc_priv *doc; |
| 1430 | void __iomem *virtadr; |
| 1431 | unsigned char save_control; |
| 1432 | unsigned char tmp, tmpb, tmpc; |
| 1433 | int reg, len, numchips; |
| 1434 | int ret = 0; |
| 1435 | |
Sasha Levin | 86e4bbc | 2014-03-19 18:24:37 -0400 | [diff] [blame] | 1436 | if (!request_mem_region(physadr, DOC_IOREMAP_LEN, "DiskOnChip")) |
Alexander Shiyan | 4f0614a | 2013-11-13 15:59:25 +0400 | [diff] [blame] | 1437 | return -EBUSY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1438 | virtadr = ioremap(physadr, DOC_IOREMAP_LEN); |
| 1439 | if (!virtadr) { |
| 1440 | printk(KERN_ERR "Diskonchip ioremap failed: 0x%x bytes at 0x%lx\n", DOC_IOREMAP_LEN, physadr); |
Alexander Shiyan | 4f0614a | 2013-11-13 15:59:25 +0400 | [diff] [blame] | 1441 | ret = -EIO; |
| 1442 | goto error_ioremap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1443 | } |
| 1444 | |
| 1445 | /* It's not possible to cleanly detect the DiskOnChip - the |
| 1446 | * bootup procedure will put the device into reset mode, and |
| 1447 | * it's not possible to talk to it without actually writing |
| 1448 | * to the DOCControl register. So we store the current contents |
| 1449 | * of the DOCControl register's location, in case we later decide |
| 1450 | * that it's not a DiskOnChip, and want to put it back how we |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1451 | * found it. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1452 | */ |
| 1453 | save_control = ReadDOC(virtadr, DOCControl); |
| 1454 | |
| 1455 | /* Reset the DiskOnChip ASIC */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1456 | WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_RESET, virtadr, DOCControl); |
| 1457 | WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_RESET, virtadr, DOCControl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1458 | |
| 1459 | /* Enable the DiskOnChip ASIC */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1460 | WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_NORMAL, virtadr, DOCControl); |
| 1461 | WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_NORMAL, virtadr, DOCControl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1462 | |
| 1463 | ChipID = ReadDOC(virtadr, ChipID); |
| 1464 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1465 | switch (ChipID) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1466 | case DOC_ChipID_Doc2k: |
| 1467 | reg = DoC_2k_ECCStatus; |
| 1468 | break; |
| 1469 | case DOC_ChipID_DocMil: |
| 1470 | reg = DoC_ECCConf; |
| 1471 | break; |
| 1472 | case DOC_ChipID_DocMilPlus16: |
| 1473 | case DOC_ChipID_DocMilPlus32: |
| 1474 | case 0: |
| 1475 | /* Possible Millennium Plus, need to do more checks */ |
| 1476 | /* Possibly release from power down mode */ |
| 1477 | for (tmp = 0; (tmp < 4); tmp++) |
| 1478 | ReadDOC(virtadr, Mplus_Power); |
| 1479 | |
| 1480 | /* Reset the Millennium Plus ASIC */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1481 | tmp = DOC_MODE_RESET | DOC_MODE_MDWREN | DOC_MODE_RST_LAT | DOC_MODE_BDECT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1482 | WriteDOC(tmp, virtadr, Mplus_DOCControl); |
| 1483 | WriteDOC(~tmp, virtadr, Mplus_CtrlConfirm); |
| 1484 | |
| 1485 | mdelay(1); |
| 1486 | /* Enable the Millennium Plus ASIC */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1487 | tmp = DOC_MODE_NORMAL | DOC_MODE_MDWREN | DOC_MODE_RST_LAT | DOC_MODE_BDECT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1488 | WriteDOC(tmp, virtadr, Mplus_DOCControl); |
| 1489 | WriteDOC(~tmp, virtadr, Mplus_CtrlConfirm); |
| 1490 | mdelay(1); |
| 1491 | |
| 1492 | ChipID = ReadDOC(virtadr, ChipID); |
| 1493 | |
| 1494 | switch (ChipID) { |
| 1495 | case DOC_ChipID_DocMilPlus16: |
| 1496 | reg = DoC_Mplus_Toggle; |
| 1497 | break; |
| 1498 | case DOC_ChipID_DocMilPlus32: |
| 1499 | printk(KERN_ERR "DiskOnChip Millennium Plus 32MB is not supported, ignoring.\n"); |
| 1500 | default: |
| 1501 | ret = -ENODEV; |
| 1502 | goto notfound; |
| 1503 | } |
| 1504 | break; |
| 1505 | |
| 1506 | default: |
| 1507 | ret = -ENODEV; |
| 1508 | goto notfound; |
| 1509 | } |
| 1510 | /* Check the TOGGLE bit in the ECC register */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1511 | tmp = ReadDOC_(virtadr, reg) & DOC_TOGGLE_BIT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1512 | tmpb = ReadDOC_(virtadr, reg) & DOC_TOGGLE_BIT; |
| 1513 | tmpc = ReadDOC_(virtadr, reg) & DOC_TOGGLE_BIT; |
| 1514 | if ((tmp == tmpb) || (tmp != tmpc)) { |
| 1515 | printk(KERN_WARNING "Possible DiskOnChip at 0x%lx failed TOGGLE test, dropping.\n", physadr); |
| 1516 | ret = -ENODEV; |
| 1517 | goto notfound; |
| 1518 | } |
| 1519 | |
| 1520 | for (mtd = doclist; mtd; mtd = doc->nextdoc) { |
| 1521 | unsigned char oldval; |
| 1522 | unsigned char newval; |
Boris BREZILLON | 4bd4ebc | 2015-12-01 12:03:04 +0100 | [diff] [blame] | 1523 | nand = mtd_to_nand(mtd); |
Boris BREZILLON | d699ed2 | 2015-12-10 09:00:41 +0100 | [diff] [blame] | 1524 | doc = nand_get_controller_data(nand); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1525 | /* Use the alias resolution register to determine if this is |
| 1526 | in fact the same DOC aliased to a new address. If writes |
| 1527 | to one chip's alias resolution register change the value on |
| 1528 | the other chip, they're the same chip. */ |
| 1529 | if (ChipID == DOC_ChipID_DocMilPlus16) { |
| 1530 | oldval = ReadDOC(doc->virtadr, Mplus_AliasResolution); |
| 1531 | newval = ReadDOC(virtadr, Mplus_AliasResolution); |
| 1532 | } else { |
| 1533 | oldval = ReadDOC(doc->virtadr, AliasResolution); |
| 1534 | newval = ReadDOC(virtadr, AliasResolution); |
| 1535 | } |
| 1536 | if (oldval != newval) |
| 1537 | continue; |
| 1538 | if (ChipID == DOC_ChipID_DocMilPlus16) { |
| 1539 | WriteDOC(~newval, virtadr, Mplus_AliasResolution); |
| 1540 | oldval = ReadDOC(doc->virtadr, Mplus_AliasResolution); |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1541 | WriteDOC(newval, virtadr, Mplus_AliasResolution); // restore it |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1542 | } else { |
| 1543 | WriteDOC(~newval, virtadr, AliasResolution); |
| 1544 | oldval = ReadDOC(doc->virtadr, AliasResolution); |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1545 | WriteDOC(newval, virtadr, AliasResolution); // restore it |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1546 | } |
| 1547 | newval = ~newval; |
| 1548 | if (oldval == newval) { |
| 1549 | printk(KERN_DEBUG "Found alias of DOC at 0x%lx to 0x%lx\n", doc->physadr, physadr); |
| 1550 | goto notfound; |
| 1551 | } |
| 1552 | } |
| 1553 | |
| 1554 | printk(KERN_NOTICE "DiskOnChip found at 0x%lx\n", physadr); |
| 1555 | |
Boris BREZILLON | b0c423c | 2015-12-10 09:00:00 +0100 | [diff] [blame] | 1556 | len = sizeof(struct nand_chip) + sizeof(struct doc_priv) + |
| 1557 | (2 * sizeof(struct nand_bbt_descr)); |
| 1558 | nand = kzalloc(len, GFP_KERNEL); |
| 1559 | if (!nand) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1560 | ret = -ENOMEM; |
| 1561 | goto fail; |
| 1562 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1563 | |
Boris BREZILLON | b0c423c | 2015-12-10 09:00:00 +0100 | [diff] [blame] | 1564 | mtd = nand_to_mtd(nand); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1565 | doc = (struct doc_priv *) (nand + 1); |
| 1566 | nand->bbt_td = (struct nand_bbt_descr *) (doc + 1); |
| 1567 | nand->bbt_md = nand->bbt_td + 1; |
| 1568 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1569 | mtd->owner = THIS_MODULE; |
Boris Brezillon | 68c1b75 | 2016-02-03 20:00:23 +0100 | [diff] [blame] | 1570 | mtd_set_ooblayout(mtd, &doc200x_ooblayout_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1571 | |
Boris BREZILLON | d699ed2 | 2015-12-10 09:00:41 +0100 | [diff] [blame] | 1572 | nand_set_controller_data(nand, doc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1573 | nand->select_chip = doc200x_select_chip; |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 1574 | nand->cmd_ctrl = doc200x_hwcontrol; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1575 | nand->dev_ready = doc200x_dev_ready; |
| 1576 | nand->waitfunc = doc200x_wait; |
| 1577 | nand->block_bad = doc200x_block_bad; |
Thomas Gleixner | 6dfc6d2 | 2006-05-23 12:00:46 +0200 | [diff] [blame] | 1578 | nand->ecc.hwctl = doc200x_enable_hwecc; |
| 1579 | nand->ecc.calculate = doc200x_calculate_ecc; |
| 1580 | nand->ecc.correct = doc200x_correct_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1581 | |
Thomas Gleixner | 6dfc6d2 | 2006-05-23 12:00:46 +0200 | [diff] [blame] | 1582 | nand->ecc.mode = NAND_ECC_HW_SYNDROME; |
| 1583 | nand->ecc.size = 512; |
| 1584 | nand->ecc.bytes = 6; |
Mike Dunn | 6a918ba | 2012-03-11 14:21:11 -0700 | [diff] [blame] | 1585 | nand->ecc.strength = 2; |
Boris BREZILLON | cc01e60 | 2015-12-30 20:39:52 +0100 | [diff] [blame] | 1586 | nand->ecc.options = NAND_ECC_GENERIC_ERASED_CHECK; |
Brian Norris | bb9ebd4 | 2011-05-31 16:31:23 -0700 | [diff] [blame] | 1587 | nand->bbt_options = NAND_BBT_USE_FLASH; |
Brian Norris | d24fe0c | 2015-02-28 02:13:10 -0800 | [diff] [blame] | 1588 | /* Skip the automatic BBT scan so we can run it manually */ |
| 1589 | nand->options |= NAND_SKIP_BBTSCAN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1590 | |
| 1591 | doc->physadr = physadr; |
| 1592 | doc->virtadr = virtadr; |
| 1593 | doc->ChipID = ChipID; |
| 1594 | doc->curfloor = -1; |
| 1595 | doc->curchip = -1; |
| 1596 | doc->mh0_page = -1; |
| 1597 | doc->mh1_page = -1; |
| 1598 | doc->nextdoc = doclist; |
| 1599 | |
| 1600 | if (ChipID == DOC_ChipID_Doc2k) |
| 1601 | numchips = doc2000_init(mtd); |
| 1602 | else if (ChipID == DOC_ChipID_DocMilPlus16) |
| 1603 | numchips = doc2001plus_init(mtd); |
| 1604 | else |
| 1605 | numchips = doc2001_init(mtd); |
| 1606 | |
Brian Norris | d24fe0c | 2015-02-28 02:13:10 -0800 | [diff] [blame] | 1607 | if ((ret = nand_scan(mtd, numchips)) || (ret = doc->late_init(mtd))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1608 | /* DBB note: i believe nand_release is necessary here, as |
| 1609 | buffers may have been allocated in nand_base. Check with |
| 1610 | Thomas. FIX ME! */ |
Jamie Iles | 0f47e95 | 2011-05-23 10:23:19 +0100 | [diff] [blame] | 1611 | /* nand_release will call mtd_device_unregister, but we |
| 1612 | haven't yet added it. This is handled without incident by |
| 1613 | mtd_device_unregister, as far as I can tell. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1614 | nand_release(mtd); |
Boris BREZILLON | b0c423c | 2015-12-10 09:00:00 +0100 | [diff] [blame] | 1615 | kfree(nand); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1616 | goto fail; |
| 1617 | } |
| 1618 | |
| 1619 | /* Success! */ |
| 1620 | doclist = mtd; |
| 1621 | return 0; |
| 1622 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1623 | notfound: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1624 | /* Put back the contents of the DOCControl register, in case it's not |
| 1625 | actually a DiskOnChip. */ |
| 1626 | WriteDOC(save_control, virtadr, DOCControl); |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1627 | fail: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1628 | iounmap(virtadr); |
Alexander Shiyan | 4f0614a | 2013-11-13 15:59:25 +0400 | [diff] [blame] | 1629 | |
| 1630 | error_ioremap: |
| 1631 | release_mem_region(physadr, DOC_IOREMAP_LEN); |
| 1632 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1633 | return ret; |
| 1634 | } |
| 1635 | |
| 1636 | static void release_nanddoc(void) |
| 1637 | { |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1638 | struct mtd_info *mtd, *nextmtd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1639 | struct nand_chip *nand; |
| 1640 | struct doc_priv *doc; |
| 1641 | |
| 1642 | for (mtd = doclist; mtd; mtd = nextmtd) { |
Boris BREZILLON | 4bd4ebc | 2015-12-01 12:03:04 +0100 | [diff] [blame] | 1643 | nand = mtd_to_nand(mtd); |
Boris BREZILLON | d699ed2 | 2015-12-10 09:00:41 +0100 | [diff] [blame] | 1644 | doc = nand_get_controller_data(nand); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1645 | |
| 1646 | nextmtd = doc->nextdoc; |
| 1647 | nand_release(mtd); |
| 1648 | iounmap(doc->virtadr); |
Alexander Shiyan | 4f0614a | 2013-11-13 15:59:25 +0400 | [diff] [blame] | 1649 | release_mem_region(doc->physadr, DOC_IOREMAP_LEN); |
Boris BREZILLON | b0c423c | 2015-12-10 09:00:00 +0100 | [diff] [blame] | 1650 | kfree(nand); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1651 | } |
| 1652 | } |
| 1653 | |
| 1654 | static int __init init_nanddoc(void) |
| 1655 | { |
| 1656 | int i, ret = 0; |
| 1657 | |
| 1658 | /* We could create the decoder on demand, if memory is a concern. |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1659 | * This way we have it handy, if an error happens |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1660 | * |
| 1661 | * Symbolsize is 10 (bits) |
| 1662 | * Primitve polynomial is x^10+x^3+1 |
| 1663 | * first consecutive root is 510 |
| 1664 | * primitve element to generate roots = 1 |
| 1665 | * generator polinomial degree = 4 |
| 1666 | */ |
| 1667 | rs_decoder = init_rs(10, 0x409, FCR, 1, NROOTS); |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1668 | if (!rs_decoder) { |
| 1669 | printk(KERN_ERR "DiskOnChip: Could not create a RS decoder\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1670 | return -ENOMEM; |
| 1671 | } |
| 1672 | |
| 1673 | if (doc_config_location) { |
| 1674 | printk(KERN_INFO "Using configured DiskOnChip probe address 0x%lx\n", doc_config_location); |
| 1675 | ret = doc_probe(doc_config_location); |
| 1676 | if (ret < 0) |
| 1677 | goto outerr; |
| 1678 | } else { |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1679 | for (i = 0; (doc_locations[i] != 0xffffffff); i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1680 | doc_probe(doc_locations[i]); |
| 1681 | } |
| 1682 | } |
| 1683 | /* No banner message any more. Print a message if no DiskOnChip |
| 1684 | found, so the user knows we at least tried. */ |
| 1685 | if (!doclist) { |
| 1686 | printk(KERN_INFO "No valid DiskOnChip devices found\n"); |
| 1687 | ret = -ENODEV; |
| 1688 | goto outerr; |
| 1689 | } |
| 1690 | return 0; |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1691 | outerr: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1692 | free_rs(rs_decoder); |
| 1693 | return ret; |
| 1694 | } |
| 1695 | |
| 1696 | static void __exit cleanup_nanddoc(void) |
| 1697 | { |
| 1698 | /* Cleanup the nand/DoC resources */ |
| 1699 | release_nanddoc(); |
| 1700 | |
| 1701 | /* Free the reed solomon resources */ |
| 1702 | if (rs_decoder) { |
| 1703 | free_rs(rs_decoder); |
| 1704 | } |
| 1705 | } |
| 1706 | |
| 1707 | module_init(init_nanddoc); |
| 1708 | module_exit(cleanup_nanddoc); |
| 1709 | |
| 1710 | MODULE_LICENSE("GPL"); |
| 1711 | MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>"); |
Niels de Vos | 2a7af8c | 2009-01-30 11:08:35 +0100 | [diff] [blame] | 1712 | MODULE_DESCRIPTION("M-Systems DiskOnChip 2000, Millennium and Millennium Plus device driver"); |