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