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