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