Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Linux driver for Disk-On-Chip 2000 and Millennium |
| 4 | * (c) 1999 Machine Vision Holdings, Inc. |
| 5 | * (c) 1999, 2000 David Woodhouse <dwmw2@infradead.org> |
| 6 | * |
Thomas Gleixner | e5580fb | 2005-11-07 11:15:40 +0000 | [diff] [blame] | 7 | * $Id: doc2000.c,v 1.67 2005/11/07 11:14:24 gleixner Exp $ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include <linux/kernel.h> |
| 11 | #include <linux/module.h> |
| 12 | #include <asm/errno.h> |
| 13 | #include <asm/io.h> |
| 14 | #include <asm/uaccess.h> |
| 15 | #include <linux/miscdevice.h> |
| 16 | #include <linux/pci.h> |
| 17 | #include <linux/delay.h> |
| 18 | #include <linux/slab.h> |
| 19 | #include <linux/sched.h> |
| 20 | #include <linux/init.h> |
| 21 | #include <linux/types.h> |
| 22 | #include <linux/bitops.h> |
Ingo Molnar | 040d79f | 2006-03-31 02:29:40 -0800 | [diff] [blame] | 23 | #include <linux/mutex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | |
| 25 | #include <linux/mtd/mtd.h> |
| 26 | #include <linux/mtd/nand.h> |
| 27 | #include <linux/mtd/doc2000.h> |
| 28 | |
| 29 | #define DOC_SUPPORT_2000 |
| 30 | #define DOC_SUPPORT_2000TSOP |
| 31 | #define DOC_SUPPORT_MILLENNIUM |
| 32 | |
| 33 | #ifdef DOC_SUPPORT_2000 |
| 34 | #define DoC_is_2000(doc) (doc->ChipID == DOC_ChipID_Doc2k) |
| 35 | #else |
| 36 | #define DoC_is_2000(doc) (0) |
| 37 | #endif |
| 38 | |
| 39 | #if defined(DOC_SUPPORT_2000TSOP) || defined(DOC_SUPPORT_MILLENNIUM) |
| 40 | #define DoC_is_Millennium(doc) (doc->ChipID == DOC_ChipID_DocMil) |
| 41 | #else |
| 42 | #define DoC_is_Millennium(doc) (0) |
| 43 | #endif |
| 44 | |
| 45 | /* #define ECC_DEBUG */ |
| 46 | |
| 47 | /* I have no idea why some DoC chips can not use memcpy_from|to_io(). |
| 48 | * This may be due to the different revisions of the ASIC controller built-in or |
| 49 | * simplily a QA/Bug issue. Who knows ?? If you have trouble, please uncomment |
| 50 | * this: |
| 51 | #undef USE_MEMCPY |
| 52 | */ |
| 53 | |
| 54 | static int doc_read(struct mtd_info *mtd, loff_t from, size_t len, |
| 55 | size_t *retlen, u_char *buf); |
| 56 | static int doc_write(struct mtd_info *mtd, loff_t to, size_t len, |
| 57 | size_t *retlen, const u_char *buf); |
| 58 | static int doc_read_ecc(struct mtd_info *mtd, loff_t from, size_t len, |
| 59 | size_t *retlen, u_char *buf, u_char *eccbuf, struct nand_oobinfo *oobsel); |
| 60 | static int doc_write_ecc(struct mtd_info *mtd, loff_t to, size_t len, |
| 61 | size_t *retlen, const u_char *buf, u_char *eccbuf, struct nand_oobinfo *oobsel); |
Thomas Gleixner | e5580fb | 2005-11-07 11:15:40 +0000 | [diff] [blame] | 62 | static int doc_writev_ecc(struct mtd_info *mtd, const struct kvec *vecs, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | unsigned long count, loff_t to, size_t *retlen, |
| 64 | u_char *eccbuf, struct nand_oobinfo *oobsel); |
| 65 | static int doc_read_oob(struct mtd_info *mtd, loff_t ofs, size_t len, |
| 66 | size_t *retlen, u_char *buf); |
| 67 | static int doc_write_oob(struct mtd_info *mtd, loff_t ofs, size_t len, |
| 68 | size_t *retlen, const u_char *buf); |
| 69 | static int doc_write_oob_nolock(struct mtd_info *mtd, loff_t ofs, size_t len, |
| 70 | size_t *retlen, const u_char *buf); |
| 71 | static int doc_erase (struct mtd_info *mtd, struct erase_info *instr); |
| 72 | |
| 73 | static struct mtd_info *doc2klist = NULL; |
| 74 | |
| 75 | /* Perform the required delay cycles by reading from the appropriate register */ |
| 76 | static void DoC_Delay(struct DiskOnChip *doc, unsigned short cycles) |
| 77 | { |
| 78 | volatile char dummy; |
| 79 | int i; |
Thomas Gleixner | e5580fb | 2005-11-07 11:15:40 +0000 | [diff] [blame] | 80 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | for (i = 0; i < cycles; i++) { |
| 82 | if (DoC_is_Millennium(doc)) |
| 83 | dummy = ReadDOC(doc->virtadr, NOP); |
| 84 | else |
| 85 | dummy = ReadDOC(doc->virtadr, DOCStatus); |
| 86 | } |
Thomas Gleixner | e5580fb | 2005-11-07 11:15:40 +0000 | [diff] [blame] | 87 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | /* DOC_WaitReady: Wait for RDY line to be asserted by the flash chip */ |
| 91 | static int _DoC_WaitReady(struct DiskOnChip *doc) |
| 92 | { |
| 93 | void __iomem *docptr = doc->virtadr; |
| 94 | unsigned long timeo = jiffies + (HZ * 10); |
| 95 | |
| 96 | DEBUG(MTD_DEBUG_LEVEL3, |
| 97 | "_DoC_WaitReady called for out-of-line wait\n"); |
| 98 | |
| 99 | /* Out-of-line routine to wait for chip response */ |
| 100 | while (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B)) { |
| 101 | /* issue 2 read from NOP register after reading from CDSNControl register |
| 102 | see Software Requirement 11.4 item 2. */ |
| 103 | DoC_Delay(doc, 2); |
| 104 | |
| 105 | if (time_after(jiffies, timeo)) { |
| 106 | DEBUG(MTD_DEBUG_LEVEL2, "_DoC_WaitReady timed out.\n"); |
| 107 | return -EIO; |
| 108 | } |
| 109 | udelay(1); |
| 110 | cond_resched(); |
| 111 | } |
| 112 | |
| 113 | return 0; |
| 114 | } |
| 115 | |
| 116 | static inline int DoC_WaitReady(struct DiskOnChip *doc) |
| 117 | { |
| 118 | void __iomem *docptr = doc->virtadr; |
| 119 | |
| 120 | /* This is inline, to optimise the common case, where it's ready instantly */ |
| 121 | int ret = 0; |
| 122 | |
| 123 | /* 4 read form NOP register should be issued in prior to the read from CDSNControl |
| 124 | see Software Requirement 11.4 item 2. */ |
| 125 | DoC_Delay(doc, 4); |
| 126 | |
| 127 | if (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B)) |
| 128 | /* Call the out-of-line routine to wait */ |
| 129 | ret = _DoC_WaitReady(doc); |
| 130 | |
| 131 | /* issue 2 read from NOP register after reading from CDSNControl register |
| 132 | see Software Requirement 11.4 item 2. */ |
| 133 | DoC_Delay(doc, 2); |
| 134 | |
| 135 | return ret; |
| 136 | } |
| 137 | |
| 138 | /* DoC_Command: Send a flash command to the flash chip through the CDSN Slow IO register to |
| 139 | bypass the internal pipeline. Each of 4 delay cycles (read from the NOP register) is |
| 140 | required after writing to CDSN Control register, see Software Requirement 11.4 item 3. */ |
| 141 | |
Arjan van de Ven | 858119e | 2006-01-14 13:20:43 -0800 | [diff] [blame] | 142 | static int DoC_Command(struct DiskOnChip *doc, unsigned char command, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | unsigned char xtraflags) |
| 144 | { |
| 145 | void __iomem *docptr = doc->virtadr; |
| 146 | |
| 147 | if (DoC_is_2000(doc)) |
| 148 | xtraflags |= CDSN_CTRL_FLASH_IO; |
| 149 | |
| 150 | /* Assert the CLE (Command Latch Enable) line to the flash chip */ |
| 151 | WriteDOC(xtraflags | CDSN_CTRL_CLE | CDSN_CTRL_CE, docptr, CDSNControl); |
| 152 | DoC_Delay(doc, 4); /* Software requirement 11.4.3 for Millennium */ |
| 153 | |
| 154 | if (DoC_is_Millennium(doc)) |
| 155 | WriteDOC(command, docptr, CDSNSlowIO); |
| 156 | |
| 157 | /* Send the command */ |
| 158 | WriteDOC_(command, docptr, doc->ioreg); |
| 159 | if (DoC_is_Millennium(doc)) |
| 160 | WriteDOC(command, docptr, WritePipeTerm); |
| 161 | |
| 162 | /* Lower the CLE line */ |
| 163 | WriteDOC(xtraflags | CDSN_CTRL_CE, docptr, CDSNControl); |
| 164 | DoC_Delay(doc, 4); /* Software requirement 11.4.3 for Millennium */ |
| 165 | |
| 166 | /* Wait for the chip to respond - Software requirement 11.4.1 (extended for any command) */ |
| 167 | return DoC_WaitReady(doc); |
| 168 | } |
| 169 | |
| 170 | /* DoC_Address: Set the current address for the flash chip through the CDSN Slow IO register to |
| 171 | bypass the internal pipeline. Each of 4 delay cycles (read from the NOP register) is |
| 172 | required after writing to CDSN Control register, see Software Requirement 11.4 item 3. */ |
| 173 | |
| 174 | static int DoC_Address(struct DiskOnChip *doc, int numbytes, unsigned long ofs, |
| 175 | unsigned char xtraflags1, unsigned char xtraflags2) |
| 176 | { |
| 177 | int i; |
| 178 | void __iomem *docptr = doc->virtadr; |
| 179 | |
| 180 | if (DoC_is_2000(doc)) |
| 181 | xtraflags1 |= CDSN_CTRL_FLASH_IO; |
| 182 | |
| 183 | /* Assert the ALE (Address Latch Enable) line to the flash chip */ |
| 184 | WriteDOC(xtraflags1 | CDSN_CTRL_ALE | CDSN_CTRL_CE, docptr, CDSNControl); |
| 185 | |
| 186 | DoC_Delay(doc, 4); /* Software requirement 11.4.3 for Millennium */ |
| 187 | |
| 188 | /* Send the address */ |
| 189 | /* Devices with 256-byte page are addressed as: |
| 190 | Column (bits 0-7), Page (bits 8-15, 16-23, 24-31) |
| 191 | * there is no device on the market with page256 |
| 192 | and more than 24 bits. |
| 193 | Devices with 512-byte page are addressed as: |
| 194 | Column (bits 0-7), Page (bits 9-16, 17-24, 25-31) |
| 195 | * 25-31 is sent only if the chip support it. |
| 196 | * bit 8 changes the read command to be sent |
| 197 | (NAND_CMD_READ0 or NAND_CMD_READ1). |
| 198 | */ |
| 199 | |
| 200 | if (numbytes == ADDR_COLUMN || numbytes == ADDR_COLUMN_PAGE) { |
| 201 | if (DoC_is_Millennium(doc)) |
| 202 | WriteDOC(ofs & 0xff, docptr, CDSNSlowIO); |
| 203 | WriteDOC_(ofs & 0xff, docptr, doc->ioreg); |
| 204 | } |
| 205 | |
| 206 | if (doc->page256) { |
| 207 | ofs = ofs >> 8; |
| 208 | } else { |
| 209 | ofs = ofs >> 9; |
| 210 | } |
| 211 | |
| 212 | if (numbytes == ADDR_PAGE || numbytes == ADDR_COLUMN_PAGE) { |
| 213 | for (i = 0; i < doc->pageadrlen; i++, ofs = ofs >> 8) { |
| 214 | if (DoC_is_Millennium(doc)) |
| 215 | WriteDOC(ofs & 0xff, docptr, CDSNSlowIO); |
| 216 | WriteDOC_(ofs & 0xff, docptr, doc->ioreg); |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | if (DoC_is_Millennium(doc)) |
| 221 | WriteDOC(ofs & 0xff, docptr, WritePipeTerm); |
| 222 | |
| 223 | DoC_Delay(doc, 2); /* Needed for some slow flash chips. mf. */ |
Thomas Gleixner | e5580fb | 2005-11-07 11:15:40 +0000 | [diff] [blame] | 224 | |
| 225 | /* FIXME: The SlowIO's for millennium could be replaced by |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | a single WritePipeTerm here. mf. */ |
| 227 | |
| 228 | /* Lower the ALE line */ |
| 229 | WriteDOC(xtraflags1 | xtraflags2 | CDSN_CTRL_CE, docptr, |
| 230 | CDSNControl); |
| 231 | |
| 232 | DoC_Delay(doc, 4); /* Software requirement 11.4.3 for Millennium */ |
| 233 | |
| 234 | /* Wait for the chip to respond - Software requirement 11.4.1 */ |
| 235 | return DoC_WaitReady(doc); |
| 236 | } |
| 237 | |
| 238 | /* Read a buffer from DoC, taking care of Millennium odditys */ |
| 239 | static void DoC_ReadBuf(struct DiskOnChip *doc, u_char * buf, int len) |
| 240 | { |
| 241 | volatile int dummy; |
| 242 | int modulus = 0xffff; |
| 243 | void __iomem *docptr = doc->virtadr; |
| 244 | int i; |
| 245 | |
| 246 | if (len <= 0) |
| 247 | return; |
| 248 | |
| 249 | if (DoC_is_Millennium(doc)) { |
| 250 | /* Read the data via the internal pipeline through CDSN IO register, |
| 251 | see Pipelined Read Operations 11.3 */ |
| 252 | dummy = ReadDOC(docptr, ReadPipeInit); |
| 253 | |
| 254 | /* Millennium should use the LastDataRead register - Pipeline Reads */ |
| 255 | len--; |
| 256 | |
| 257 | /* This is needed for correctly ECC calculation */ |
| 258 | modulus = 0xff; |
| 259 | } |
| 260 | |
| 261 | for (i = 0; i < len; i++) |
| 262 | buf[i] = ReadDOC_(docptr, doc->ioreg + (i & modulus)); |
| 263 | |
| 264 | if (DoC_is_Millennium(doc)) { |
| 265 | buf[i] = ReadDOC(docptr, LastDataRead); |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | /* Write a buffer to DoC, taking care of Millennium odditys */ |
| 270 | static void DoC_WriteBuf(struct DiskOnChip *doc, const u_char * buf, int len) |
| 271 | { |
| 272 | void __iomem *docptr = doc->virtadr; |
| 273 | int i; |
| 274 | |
| 275 | if (len <= 0) |
| 276 | return; |
| 277 | |
| 278 | for (i = 0; i < len; i++) |
| 279 | WriteDOC_(buf[i], docptr, doc->ioreg + i); |
| 280 | |
| 281 | if (DoC_is_Millennium(doc)) { |
| 282 | WriteDOC(0x00, docptr, WritePipeTerm); |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | |
| 287 | /* DoC_SelectChip: Select a given flash chip within the current floor */ |
| 288 | |
| 289 | static inline int DoC_SelectChip(struct DiskOnChip *doc, int chip) |
| 290 | { |
| 291 | void __iomem *docptr = doc->virtadr; |
| 292 | |
| 293 | /* Software requirement 11.4.4 before writing DeviceSelect */ |
| 294 | /* Deassert the CE line to eliminate glitches on the FCE# outputs */ |
| 295 | WriteDOC(CDSN_CTRL_WP, docptr, CDSNControl); |
| 296 | DoC_Delay(doc, 4); /* Software requirement 11.4.3 for Millennium */ |
| 297 | |
| 298 | /* Select the individual flash chip requested */ |
| 299 | WriteDOC(chip, docptr, CDSNDeviceSelect); |
| 300 | DoC_Delay(doc, 4); |
| 301 | |
| 302 | /* Reassert the CE line */ |
| 303 | WriteDOC(CDSN_CTRL_CE | CDSN_CTRL_FLASH_IO | CDSN_CTRL_WP, docptr, |
| 304 | CDSNControl); |
| 305 | DoC_Delay(doc, 4); /* Software requirement 11.4.3 for Millennium */ |
| 306 | |
| 307 | /* Wait for it to be ready */ |
| 308 | return DoC_WaitReady(doc); |
| 309 | } |
| 310 | |
| 311 | /* DoC_SelectFloor: Select a given floor (bank of flash chips) */ |
| 312 | |
| 313 | static inline int DoC_SelectFloor(struct DiskOnChip *doc, int floor) |
| 314 | { |
| 315 | void __iomem *docptr = doc->virtadr; |
| 316 | |
| 317 | /* Select the floor (bank) of chips required */ |
| 318 | WriteDOC(floor, docptr, FloorSelect); |
| 319 | |
| 320 | /* Wait for the chip to be ready */ |
| 321 | return DoC_WaitReady(doc); |
| 322 | } |
| 323 | |
| 324 | /* DoC_IdentChip: Identify a given NAND chip given {floor,chip} */ |
| 325 | |
| 326 | static int DoC_IdentChip(struct DiskOnChip *doc, int floor, int chip) |
| 327 | { |
| 328 | int mfr, id, i, j; |
| 329 | volatile char dummy; |
| 330 | |
| 331 | /* Page in the required floor/chip */ |
| 332 | DoC_SelectFloor(doc, floor); |
| 333 | DoC_SelectChip(doc, chip); |
| 334 | |
| 335 | /* Reset the chip */ |
| 336 | if (DoC_Command(doc, NAND_CMD_RESET, CDSN_CTRL_WP)) { |
| 337 | DEBUG(MTD_DEBUG_LEVEL2, |
| 338 | "DoC_Command (reset) for %d,%d returned true\n", |
| 339 | floor, chip); |
| 340 | return 0; |
| 341 | } |
| 342 | |
| 343 | |
| 344 | /* Read the NAND chip ID: 1. Send ReadID command */ |
| 345 | if (DoC_Command(doc, NAND_CMD_READID, CDSN_CTRL_WP)) { |
| 346 | DEBUG(MTD_DEBUG_LEVEL2, |
| 347 | "DoC_Command (ReadID) for %d,%d returned true\n", |
| 348 | floor, chip); |
| 349 | return 0; |
| 350 | } |
| 351 | |
| 352 | /* Read the NAND chip ID: 2. Send address byte zero */ |
| 353 | DoC_Address(doc, ADDR_COLUMN, 0, CDSN_CTRL_WP, 0); |
| 354 | |
| 355 | /* Read the manufacturer and device id codes from the device */ |
| 356 | |
| 357 | if (DoC_is_Millennium(doc)) { |
| 358 | DoC_Delay(doc, 2); |
| 359 | dummy = ReadDOC(doc->virtadr, ReadPipeInit); |
| 360 | mfr = ReadDOC(doc->virtadr, LastDataRead); |
| 361 | |
| 362 | DoC_Delay(doc, 2); |
| 363 | dummy = ReadDOC(doc->virtadr, ReadPipeInit); |
| 364 | id = ReadDOC(doc->virtadr, LastDataRead); |
| 365 | } else { |
| 366 | /* CDSN Slow IO register see Software Req 11.4 item 5. */ |
| 367 | dummy = ReadDOC(doc->virtadr, CDSNSlowIO); |
| 368 | DoC_Delay(doc, 2); |
| 369 | mfr = ReadDOC_(doc->virtadr, doc->ioreg); |
| 370 | |
| 371 | /* CDSN Slow IO register see Software Req 11.4 item 5. */ |
| 372 | dummy = ReadDOC(doc->virtadr, CDSNSlowIO); |
| 373 | DoC_Delay(doc, 2); |
| 374 | id = ReadDOC_(doc->virtadr, doc->ioreg); |
| 375 | } |
| 376 | |
| 377 | /* No response - return failure */ |
| 378 | if (mfr == 0xff || mfr == 0) |
| 379 | return 0; |
| 380 | |
Thomas Gleixner | e5580fb | 2005-11-07 11:15:40 +0000 | [diff] [blame] | 381 | /* Check it's the same as the first chip we identified. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | * M-Systems say that any given DiskOnChip device should only |
Thomas Gleixner | e5580fb | 2005-11-07 11:15:40 +0000 | [diff] [blame] | 383 | * contain _one_ type of flash part, although that's not a |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | * hardware restriction. */ |
| 385 | if (doc->mfr) { |
| 386 | if (doc->mfr == mfr && doc->id == id) |
| 387 | return 1; /* This is another the same the first */ |
| 388 | else |
| 389 | printk(KERN_WARNING |
| 390 | "Flash chip at floor %d, chip %d is different:\n", |
| 391 | floor, chip); |
| 392 | } |
| 393 | |
| 394 | /* Print and store the manufacturer and ID codes. */ |
| 395 | for (i = 0; nand_flash_ids[i].name != NULL; i++) { |
| 396 | if (id == nand_flash_ids[i].id) { |
| 397 | /* Try to identify manufacturer */ |
| 398 | for (j = 0; nand_manuf_ids[j].id != 0x0; j++) { |
| 399 | if (nand_manuf_ids[j].id == mfr) |
| 400 | break; |
Thomas Gleixner | e5580fb | 2005-11-07 11:15:40 +0000 | [diff] [blame] | 401 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | printk(KERN_INFO |
| 403 | "Flash chip found: Manufacturer ID: %2.2X, " |
| 404 | "Chip ID: %2.2X (%s:%s)\n", mfr, id, |
| 405 | nand_manuf_ids[j].name, nand_flash_ids[i].name); |
| 406 | if (!doc->mfr) { |
| 407 | doc->mfr = mfr; |
| 408 | doc->id = id; |
Thomas Gleixner | e5580fb | 2005-11-07 11:15:40 +0000 | [diff] [blame] | 409 | doc->chipshift = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | ffs((nand_flash_ids[i].chipsize << 20)) - 1; |
| 411 | doc->page256 = (nand_flash_ids[i].pagesize == 256) ? 1 : 0; |
| 412 | doc->pageadrlen = doc->chipshift > 25 ? 3 : 2; |
| 413 | doc->erasesize = |
| 414 | nand_flash_ids[i].erasesize; |
| 415 | return 1; |
| 416 | } |
| 417 | return 0; |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | |
| 422 | /* We haven't fully identified the chip. Print as much as we know. */ |
| 423 | printk(KERN_WARNING "Unknown flash chip found: %2.2X %2.2X\n", |
| 424 | id, mfr); |
| 425 | |
| 426 | printk(KERN_WARNING "Please report to dwmw2@infradead.org\n"); |
| 427 | return 0; |
| 428 | } |
| 429 | |
| 430 | /* DoC_ScanChips: Find all NAND chips present in a DiskOnChip, and identify them */ |
| 431 | |
| 432 | static void DoC_ScanChips(struct DiskOnChip *this, int maxchips) |
| 433 | { |
| 434 | int floor, chip; |
| 435 | int numchips[MAX_FLOORS]; |
| 436 | int ret = 1; |
| 437 | |
| 438 | this->numchips = 0; |
| 439 | this->mfr = 0; |
| 440 | this->id = 0; |
| 441 | |
| 442 | /* For each floor, find the number of valid chips it contains */ |
| 443 | for (floor = 0; floor < MAX_FLOORS; floor++) { |
| 444 | ret = 1; |
| 445 | numchips[floor] = 0; |
| 446 | for (chip = 0; chip < maxchips && ret != 0; chip++) { |
| 447 | |
| 448 | ret = DoC_IdentChip(this, floor, chip); |
| 449 | if (ret) { |
| 450 | numchips[floor]++; |
| 451 | this->numchips++; |
| 452 | } |
| 453 | } |
| 454 | } |
| 455 | |
| 456 | /* If there are none at all that we recognise, bail */ |
| 457 | if (!this->numchips) { |
| 458 | printk(KERN_NOTICE "No flash chips recognised.\n"); |
| 459 | return; |
| 460 | } |
| 461 | |
| 462 | /* Allocate an array to hold the information for each chip */ |
| 463 | this->chips = kmalloc(sizeof(struct Nand) * this->numchips, GFP_KERNEL); |
| 464 | if (!this->chips) { |
| 465 | printk(KERN_NOTICE "No memory for allocating chip info structures\n"); |
| 466 | return; |
| 467 | } |
| 468 | |
| 469 | ret = 0; |
| 470 | |
Thomas Gleixner | e5580fb | 2005-11-07 11:15:40 +0000 | [diff] [blame] | 471 | /* Fill out the chip array with {floor, chipno} for each |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | * detected chip in the device. */ |
| 473 | for (floor = 0; floor < MAX_FLOORS; floor++) { |
| 474 | for (chip = 0; chip < numchips[floor]; chip++) { |
| 475 | this->chips[ret].floor = floor; |
| 476 | this->chips[ret].chip = chip; |
| 477 | this->chips[ret].curadr = 0; |
| 478 | this->chips[ret].curmode = 0x50; |
| 479 | ret++; |
| 480 | } |
| 481 | } |
| 482 | |
| 483 | /* Calculate and print the total size of the device */ |
| 484 | this->totlen = this->numchips * (1 << this->chipshift); |
| 485 | |
| 486 | printk(KERN_INFO "%d flash chips found. Total DiskOnChip size: %ld MiB\n", |
| 487 | this->numchips, this->totlen >> 20); |
| 488 | } |
| 489 | |
| 490 | static int DoC2k_is_alias(struct DiskOnChip *doc1, struct DiskOnChip *doc2) |
| 491 | { |
| 492 | int tmp1, tmp2, retval; |
| 493 | if (doc1->physadr == doc2->physadr) |
| 494 | return 1; |
| 495 | |
| 496 | /* Use the alias resolution register which was set aside for this |
| 497 | * purpose. If it's value is the same on both chips, they might |
| 498 | * be the same chip, and we write to one and check for a change in |
| 499 | * the other. It's unclear if this register is usuable in the |
| 500 | * DoC 2000 (it's in the Millennium docs), but it seems to work. */ |
| 501 | tmp1 = ReadDOC(doc1->virtadr, AliasResolution); |
| 502 | tmp2 = ReadDOC(doc2->virtadr, AliasResolution); |
| 503 | if (tmp1 != tmp2) |
| 504 | return 0; |
| 505 | |
| 506 | WriteDOC((tmp1 + 1) % 0xff, doc1->virtadr, AliasResolution); |
| 507 | tmp2 = ReadDOC(doc2->virtadr, AliasResolution); |
| 508 | if (tmp2 == (tmp1 + 1) % 0xff) |
| 509 | retval = 1; |
| 510 | else |
| 511 | retval = 0; |
| 512 | |
| 513 | /* Restore register contents. May not be necessary, but do it just to |
| 514 | * be safe. */ |
| 515 | WriteDOC(tmp1, doc1->virtadr, AliasResolution); |
| 516 | |
| 517 | return retval; |
| 518 | } |
| 519 | |
| 520 | static const char im_name[] = "DoC2k_init"; |
| 521 | |
| 522 | /* This routine is made available to other mtd code via |
| 523 | * inter_module_register. It must only be accessed through |
| 524 | * inter_module_get which will bump the use count of this module. The |
| 525 | * addresses passed back in mtd are valid as long as the use count of |
| 526 | * this module is non-zero, i.e. between inter_module_get and |
| 527 | * inter_module_put. Keith Owens <kaos@ocs.com.au> 29 Oct 2000. |
| 528 | */ |
| 529 | static void DoC2k_init(struct mtd_info *mtd) |
| 530 | { |
| 531 | struct DiskOnChip *this = mtd->priv; |
| 532 | struct DiskOnChip *old = NULL; |
| 533 | int maxchips; |
| 534 | |
| 535 | /* We must avoid being called twice for the same device. */ |
| 536 | |
| 537 | if (doc2klist) |
| 538 | old = doc2klist->priv; |
| 539 | |
| 540 | while (old) { |
| 541 | if (DoC2k_is_alias(old, this)) { |
| 542 | printk(KERN_NOTICE |
| 543 | "Ignoring DiskOnChip 2000 at 0x%lX - already configured\n", |
| 544 | this->physadr); |
| 545 | iounmap(this->virtadr); |
| 546 | kfree(mtd); |
| 547 | return; |
| 548 | } |
| 549 | if (old->nextdoc) |
| 550 | old = old->nextdoc->priv; |
| 551 | else |
| 552 | old = NULL; |
| 553 | } |
| 554 | |
| 555 | |
| 556 | switch (this->ChipID) { |
| 557 | case DOC_ChipID_Doc2kTSOP: |
| 558 | mtd->name = "DiskOnChip 2000 TSOP"; |
| 559 | this->ioreg = DoC_Mil_CDSN_IO; |
| 560 | /* Pretend it's a Millennium */ |
| 561 | this->ChipID = DOC_ChipID_DocMil; |
| 562 | maxchips = MAX_CHIPS; |
| 563 | break; |
| 564 | case DOC_ChipID_Doc2k: |
| 565 | mtd->name = "DiskOnChip 2000"; |
| 566 | this->ioreg = DoC_2k_CDSN_IO; |
| 567 | maxchips = MAX_CHIPS; |
| 568 | break; |
| 569 | case DOC_ChipID_DocMil: |
| 570 | mtd->name = "DiskOnChip Millennium"; |
| 571 | this->ioreg = DoC_Mil_CDSN_IO; |
| 572 | maxchips = MAX_CHIPS_MIL; |
| 573 | break; |
| 574 | default: |
| 575 | printk("Unknown ChipID 0x%02x\n", this->ChipID); |
| 576 | kfree(mtd); |
| 577 | iounmap(this->virtadr); |
| 578 | return; |
| 579 | } |
| 580 | |
| 581 | printk(KERN_NOTICE "%s found at address 0x%lX\n", mtd->name, |
| 582 | this->physadr); |
| 583 | |
| 584 | mtd->type = MTD_NANDFLASH; |
| 585 | mtd->flags = MTD_CAP_NANDFLASH; |
| 586 | mtd->ecctype = MTD_ECC_RS_DiskOnChip; |
| 587 | mtd->size = 0; |
| 588 | mtd->erasesize = 0; |
| 589 | mtd->oobblock = 512; |
| 590 | mtd->oobsize = 16; |
| 591 | mtd->owner = THIS_MODULE; |
| 592 | mtd->erase = doc_erase; |
| 593 | mtd->point = NULL; |
| 594 | mtd->unpoint = NULL; |
| 595 | mtd->read = doc_read; |
| 596 | mtd->write = doc_write; |
| 597 | mtd->read_ecc = doc_read_ecc; |
| 598 | mtd->write_ecc = doc_write_ecc; |
| 599 | mtd->writev_ecc = doc_writev_ecc; |
| 600 | mtd->read_oob = doc_read_oob; |
| 601 | mtd->write_oob = doc_write_oob; |
| 602 | mtd->sync = NULL; |
| 603 | |
| 604 | this->totlen = 0; |
| 605 | this->numchips = 0; |
| 606 | |
| 607 | this->curfloor = -1; |
| 608 | this->curchip = -1; |
Ingo Molnar | 48b1926 | 2006-03-31 02:29:41 -0800 | [diff] [blame] | 609 | mutex_init(&this->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 | |
| 611 | /* Ident all the chips present. */ |
| 612 | DoC_ScanChips(this, maxchips); |
| 613 | |
| 614 | if (!this->totlen) { |
| 615 | kfree(mtd); |
| 616 | iounmap(this->virtadr); |
| 617 | } else { |
| 618 | this->nextdoc = doc2klist; |
| 619 | doc2klist = mtd; |
| 620 | mtd->size = this->totlen; |
| 621 | mtd->erasesize = this->erasesize; |
| 622 | add_mtd_device(mtd); |
| 623 | return; |
| 624 | } |
| 625 | } |
| 626 | |
| 627 | static int doc_read(struct mtd_info *mtd, loff_t from, size_t len, |
| 628 | size_t * retlen, u_char * buf) |
| 629 | { |
| 630 | /* Just a special case of doc_read_ecc */ |
| 631 | return doc_read_ecc(mtd, from, len, retlen, buf, NULL, NULL); |
| 632 | } |
| 633 | |
| 634 | static int doc_read_ecc(struct mtd_info *mtd, loff_t from, size_t len, |
| 635 | size_t * retlen, u_char * buf, u_char * eccbuf, struct nand_oobinfo *oobsel) |
| 636 | { |
| 637 | struct DiskOnChip *this = mtd->priv; |
| 638 | void __iomem *docptr = this->virtadr; |
| 639 | struct Nand *mychip; |
| 640 | unsigned char syndrome[6]; |
| 641 | volatile char dummy; |
| 642 | int i, len256 = 0, ret=0; |
| 643 | size_t left = len; |
| 644 | |
| 645 | /* Don't allow read past end of device */ |
| 646 | if (from >= this->totlen) |
| 647 | return -EINVAL; |
| 648 | |
Ingo Molnar | 48b1926 | 2006-03-31 02:29:41 -0800 | [diff] [blame] | 649 | mutex_lock(&this->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 650 | |
| 651 | *retlen = 0; |
| 652 | while (left) { |
| 653 | len = left; |
| 654 | |
| 655 | /* Don't allow a single read to cross a 512-byte block boundary */ |
| 656 | if (from + len > ((from | 0x1ff) + 1)) |
| 657 | len = ((from | 0x1ff) + 1) - from; |
| 658 | |
| 659 | /* The ECC will not be calculated correctly if less than 512 is read */ |
| 660 | if (len != 0x200 && eccbuf) |
| 661 | printk(KERN_WARNING |
| 662 | "ECC needs a full sector read (adr: %lx size %lx)\n", |
| 663 | (long) from, (long) len); |
| 664 | |
| 665 | /* printk("DoC_Read (adr: %lx size %lx)\n", (long) from, (long) len); */ |
| 666 | |
| 667 | |
| 668 | /* Find the chip which is to be used and select it */ |
| 669 | mychip = &this->chips[from >> (this->chipshift)]; |
| 670 | |
| 671 | if (this->curfloor != mychip->floor) { |
| 672 | DoC_SelectFloor(this, mychip->floor); |
| 673 | DoC_SelectChip(this, mychip->chip); |
| 674 | } else if (this->curchip != mychip->chip) { |
| 675 | DoC_SelectChip(this, mychip->chip); |
| 676 | } |
| 677 | |
| 678 | this->curfloor = mychip->floor; |
| 679 | this->curchip = mychip->chip; |
| 680 | |
| 681 | DoC_Command(this, |
| 682 | (!this->page256 |
| 683 | && (from & 0x100)) ? NAND_CMD_READ1 : NAND_CMD_READ0, |
| 684 | CDSN_CTRL_WP); |
| 685 | DoC_Address(this, ADDR_COLUMN_PAGE, from, CDSN_CTRL_WP, |
| 686 | CDSN_CTRL_ECC_IO); |
| 687 | |
| 688 | if (eccbuf) { |
| 689 | /* Prime the ECC engine */ |
| 690 | WriteDOC(DOC_ECC_RESET, docptr, ECCConf); |
| 691 | WriteDOC(DOC_ECC_EN, docptr, ECCConf); |
| 692 | } else { |
| 693 | /* disable the ECC engine */ |
| 694 | WriteDOC(DOC_ECC_RESET, docptr, ECCConf); |
| 695 | WriteDOC(DOC_ECC_DIS, docptr, ECCConf); |
| 696 | } |
| 697 | |
| 698 | /* treat crossing 256-byte sector for 2M x 8bits devices */ |
| 699 | if (this->page256 && from + len > (from | 0xff) + 1) { |
| 700 | len256 = (from | 0xff) + 1 - from; |
| 701 | DoC_ReadBuf(this, buf, len256); |
| 702 | |
| 703 | DoC_Command(this, NAND_CMD_READ0, CDSN_CTRL_WP); |
| 704 | DoC_Address(this, ADDR_COLUMN_PAGE, from + len256, |
| 705 | CDSN_CTRL_WP, CDSN_CTRL_ECC_IO); |
| 706 | } |
| 707 | |
| 708 | DoC_ReadBuf(this, &buf[len256], len - len256); |
| 709 | |
| 710 | /* Let the caller know we completed it */ |
| 711 | *retlen += len; |
| 712 | |
| 713 | if (eccbuf) { |
| 714 | /* Read the ECC data through the DiskOnChip ECC logic */ |
| 715 | /* Note: this will work even with 2M x 8bit devices as */ |
| 716 | /* they have 8 bytes of OOB per 256 page. mf. */ |
| 717 | DoC_ReadBuf(this, eccbuf, 6); |
| 718 | |
| 719 | /* Flush the pipeline */ |
| 720 | if (DoC_is_Millennium(this)) { |
| 721 | dummy = ReadDOC(docptr, ECCConf); |
| 722 | dummy = ReadDOC(docptr, ECCConf); |
| 723 | i = ReadDOC(docptr, ECCConf); |
| 724 | } else { |
| 725 | dummy = ReadDOC(docptr, 2k_ECCStatus); |
| 726 | dummy = ReadDOC(docptr, 2k_ECCStatus); |
| 727 | i = ReadDOC(docptr, 2k_ECCStatus); |
| 728 | } |
| 729 | |
| 730 | /* Check the ECC Status */ |
| 731 | if (i & 0x80) { |
| 732 | int nb_errors; |
| 733 | /* There was an ECC error */ |
| 734 | #ifdef ECC_DEBUG |
| 735 | printk(KERN_ERR "DiskOnChip ECC Error: Read at %lx\n", (long)from); |
| 736 | #endif |
| 737 | /* Read the ECC syndrom through the DiskOnChip ECC logic. |
| 738 | These syndrome will be all ZERO when there is no error */ |
| 739 | for (i = 0; i < 6; i++) { |
| 740 | syndrome[i] = |
| 741 | ReadDOC(docptr, ECCSyndrome0 + i); |
| 742 | } |
| 743 | nb_errors = doc_decode_ecc(buf, syndrome); |
| 744 | |
| 745 | #ifdef ECC_DEBUG |
| 746 | printk(KERN_ERR "Errors corrected: %x\n", nb_errors); |
| 747 | #endif |
| 748 | if (nb_errors < 0) { |
| 749 | /* We return error, but have actually done the read. Not that |
| 750 | this can be told to user-space, via sys_read(), but at least |
| 751 | MTD-aware stuff can know about it by checking *retlen */ |
| 752 | ret = -EIO; |
| 753 | } |
| 754 | } |
| 755 | |
| 756 | #ifdef PSYCHO_DEBUG |
| 757 | printk(KERN_DEBUG "ECC DATA at %lxB: %2.2X %2.2X %2.2X %2.2X %2.2X %2.2X\n", |
| 758 | (long)from, eccbuf[0], eccbuf[1], eccbuf[2], |
| 759 | eccbuf[3], eccbuf[4], eccbuf[5]); |
| 760 | #endif |
Thomas Gleixner | e5580fb | 2005-11-07 11:15:40 +0000 | [diff] [blame] | 761 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 762 | /* disable the ECC engine */ |
| 763 | WriteDOC(DOC_ECC_DIS, docptr , ECCConf); |
| 764 | } |
| 765 | |
Thomas Gleixner | e5580fb | 2005-11-07 11:15:40 +0000 | [diff] [blame] | 766 | /* according to 11.4.1, we need to wait for the busy line |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 767 | * drop if we read to the end of the page. */ |
| 768 | if(0 == ((from + len) & 0x1ff)) |
| 769 | { |
| 770 | DoC_WaitReady(this); |
| 771 | } |
| 772 | |
| 773 | from += len; |
| 774 | left -= len; |
| 775 | buf += len; |
| 776 | } |
| 777 | |
Ingo Molnar | 48b1926 | 2006-03-31 02:29:41 -0800 | [diff] [blame] | 778 | mutex_unlock(&this->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 779 | |
| 780 | return ret; |
| 781 | } |
| 782 | |
| 783 | static int doc_write(struct mtd_info *mtd, loff_t to, size_t len, |
| 784 | size_t * retlen, const u_char * buf) |
| 785 | { |
| 786 | char eccbuf[6]; |
| 787 | return doc_write_ecc(mtd, to, len, retlen, buf, eccbuf, NULL); |
| 788 | } |
| 789 | |
| 790 | static int doc_write_ecc(struct mtd_info *mtd, loff_t to, size_t len, |
| 791 | size_t * retlen, const u_char * buf, |
| 792 | u_char * eccbuf, struct nand_oobinfo *oobsel) |
| 793 | { |
| 794 | struct DiskOnChip *this = mtd->priv; |
| 795 | int di; /* Yes, DI is a hangover from when I was disassembling the binary driver */ |
| 796 | void __iomem *docptr = this->virtadr; |
| 797 | volatile char dummy; |
| 798 | int len256 = 0; |
| 799 | struct Nand *mychip; |
| 800 | size_t left = len; |
| 801 | int status; |
| 802 | |
| 803 | /* Don't allow write past end of device */ |
| 804 | if (to >= this->totlen) |
| 805 | return -EINVAL; |
| 806 | |
Ingo Molnar | 48b1926 | 2006-03-31 02:29:41 -0800 | [diff] [blame] | 807 | mutex_lock(&this->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 808 | |
| 809 | *retlen = 0; |
| 810 | while (left) { |
| 811 | len = left; |
| 812 | |
| 813 | /* Don't allow a single write to cross a 512-byte block boundary */ |
| 814 | if (to + len > ((to | 0x1ff) + 1)) |
| 815 | len = ((to | 0x1ff) + 1) - to; |
| 816 | |
| 817 | /* The ECC will not be calculated correctly if less than 512 is written */ |
| 818 | /* DBB- |
| 819 | if (len != 0x200 && eccbuf) |
| 820 | printk(KERN_WARNING |
| 821 | "ECC needs a full sector write (adr: %lx size %lx)\n", |
| 822 | (long) to, (long) len); |
| 823 | -DBB */ |
| 824 | |
| 825 | /* printk("DoC_Write (adr: %lx size %lx)\n", (long) to, (long) len); */ |
| 826 | |
| 827 | /* Find the chip which is to be used and select it */ |
| 828 | mychip = &this->chips[to >> (this->chipshift)]; |
| 829 | |
| 830 | if (this->curfloor != mychip->floor) { |
| 831 | DoC_SelectFloor(this, mychip->floor); |
| 832 | DoC_SelectChip(this, mychip->chip); |
| 833 | } else if (this->curchip != mychip->chip) { |
| 834 | DoC_SelectChip(this, mychip->chip); |
| 835 | } |
| 836 | |
| 837 | this->curfloor = mychip->floor; |
| 838 | this->curchip = mychip->chip; |
| 839 | |
| 840 | /* Set device to main plane of flash */ |
| 841 | DoC_Command(this, NAND_CMD_RESET, CDSN_CTRL_WP); |
| 842 | DoC_Command(this, |
| 843 | (!this->page256 |
| 844 | && (to & 0x100)) ? NAND_CMD_READ1 : NAND_CMD_READ0, |
| 845 | CDSN_CTRL_WP); |
| 846 | |
| 847 | DoC_Command(this, NAND_CMD_SEQIN, 0); |
| 848 | DoC_Address(this, ADDR_COLUMN_PAGE, to, 0, CDSN_CTRL_ECC_IO); |
| 849 | |
| 850 | if (eccbuf) { |
| 851 | /* Prime the ECC engine */ |
| 852 | WriteDOC(DOC_ECC_RESET, docptr, ECCConf); |
| 853 | WriteDOC(DOC_ECC_EN | DOC_ECC_RW, docptr, ECCConf); |
| 854 | } else { |
| 855 | /* disable the ECC engine */ |
| 856 | WriteDOC(DOC_ECC_RESET, docptr, ECCConf); |
| 857 | WriteDOC(DOC_ECC_DIS, docptr, ECCConf); |
| 858 | } |
| 859 | |
| 860 | /* treat crossing 256-byte sector for 2M x 8bits devices */ |
| 861 | if (this->page256 && to + len > (to | 0xff) + 1) { |
| 862 | len256 = (to | 0xff) + 1 - to; |
| 863 | DoC_WriteBuf(this, buf, len256); |
| 864 | |
| 865 | DoC_Command(this, NAND_CMD_PAGEPROG, 0); |
| 866 | |
| 867 | DoC_Command(this, NAND_CMD_STATUS, CDSN_CTRL_WP); |
| 868 | /* There's an implicit DoC_WaitReady() in DoC_Command */ |
| 869 | |
| 870 | dummy = ReadDOC(docptr, CDSNSlowIO); |
| 871 | DoC_Delay(this, 2); |
| 872 | |
| 873 | if (ReadDOC_(docptr, this->ioreg) & 1) { |
| 874 | printk(KERN_ERR "Error programming flash\n"); |
| 875 | /* Error in programming */ |
| 876 | *retlen = 0; |
Ingo Molnar | 48b1926 | 2006-03-31 02:29:41 -0800 | [diff] [blame] | 877 | mutex_unlock(&this->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 878 | return -EIO; |
| 879 | } |
| 880 | |
| 881 | DoC_Command(this, NAND_CMD_SEQIN, 0); |
| 882 | DoC_Address(this, ADDR_COLUMN_PAGE, to + len256, 0, |
| 883 | CDSN_CTRL_ECC_IO); |
| 884 | } |
| 885 | |
| 886 | DoC_WriteBuf(this, &buf[len256], len - len256); |
| 887 | |
| 888 | if (eccbuf) { |
| 889 | WriteDOC(CDSN_CTRL_ECC_IO | CDSN_CTRL_CE, docptr, |
| 890 | CDSNControl); |
| 891 | |
| 892 | if (DoC_is_Millennium(this)) { |
| 893 | WriteDOC(0, docptr, NOP); |
| 894 | WriteDOC(0, docptr, NOP); |
| 895 | WriteDOC(0, docptr, NOP); |
| 896 | } else { |
| 897 | WriteDOC_(0, docptr, this->ioreg); |
| 898 | WriteDOC_(0, docptr, this->ioreg); |
| 899 | WriteDOC_(0, docptr, this->ioreg); |
| 900 | } |
| 901 | |
| 902 | WriteDOC(CDSN_CTRL_ECC_IO | CDSN_CTRL_FLASH_IO | CDSN_CTRL_CE, docptr, |
| 903 | CDSNControl); |
| 904 | |
| 905 | /* Read the ECC data through the DiskOnChip ECC logic */ |
| 906 | for (di = 0; di < 6; di++) { |
| 907 | eccbuf[di] = ReadDOC(docptr, ECCSyndrome0 + di); |
| 908 | } |
| 909 | |
| 910 | /* Reset the ECC engine */ |
| 911 | WriteDOC(DOC_ECC_DIS, docptr, ECCConf); |
| 912 | |
| 913 | #ifdef PSYCHO_DEBUG |
| 914 | printk |
| 915 | ("OOB data at %lx is %2.2X %2.2X %2.2X %2.2X %2.2X %2.2X\n", |
| 916 | (long) to, eccbuf[0], eccbuf[1], eccbuf[2], eccbuf[3], |
| 917 | eccbuf[4], eccbuf[5]); |
| 918 | #endif |
| 919 | } |
| 920 | |
| 921 | DoC_Command(this, NAND_CMD_PAGEPROG, 0); |
| 922 | |
| 923 | DoC_Command(this, NAND_CMD_STATUS, CDSN_CTRL_WP); |
| 924 | /* There's an implicit DoC_WaitReady() in DoC_Command */ |
| 925 | |
| 926 | if (DoC_is_Millennium(this)) { |
| 927 | ReadDOC(docptr, ReadPipeInit); |
| 928 | status = ReadDOC(docptr, LastDataRead); |
| 929 | } else { |
| 930 | dummy = ReadDOC(docptr, CDSNSlowIO); |
| 931 | DoC_Delay(this, 2); |
| 932 | status = ReadDOC_(docptr, this->ioreg); |
| 933 | } |
| 934 | |
| 935 | if (status & 1) { |
| 936 | printk(KERN_ERR "Error programming flash\n"); |
| 937 | /* Error in programming */ |
| 938 | *retlen = 0; |
Ingo Molnar | 48b1926 | 2006-03-31 02:29:41 -0800 | [diff] [blame] | 939 | mutex_unlock(&this->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 940 | return -EIO; |
| 941 | } |
| 942 | |
| 943 | /* Let the caller know we completed it */ |
| 944 | *retlen += len; |
Thomas Gleixner | e5580fb | 2005-11-07 11:15:40 +0000 | [diff] [blame] | 945 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 946 | if (eccbuf) { |
| 947 | unsigned char x[8]; |
| 948 | size_t dummy; |
| 949 | int ret; |
| 950 | |
| 951 | /* Write the ECC data to flash */ |
| 952 | for (di=0; di<6; di++) |
| 953 | x[di] = eccbuf[di]; |
Thomas Gleixner | e5580fb | 2005-11-07 11:15:40 +0000 | [diff] [blame] | 954 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 955 | x[6]=0x55; |
| 956 | x[7]=0x55; |
Thomas Gleixner | e5580fb | 2005-11-07 11:15:40 +0000 | [diff] [blame] | 957 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 958 | ret = doc_write_oob_nolock(mtd, to, 8, &dummy, x); |
| 959 | if (ret) { |
Ingo Molnar | 48b1926 | 2006-03-31 02:29:41 -0800 | [diff] [blame] | 960 | mutex_unlock(&this->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 961 | return ret; |
| 962 | } |
| 963 | } |
| 964 | |
| 965 | to += len; |
| 966 | left -= len; |
| 967 | buf += len; |
| 968 | } |
| 969 | |
Ingo Molnar | 48b1926 | 2006-03-31 02:29:41 -0800 | [diff] [blame] | 970 | mutex_unlock(&this->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 971 | return 0; |
| 972 | } |
| 973 | |
Thomas Gleixner | e5580fb | 2005-11-07 11:15:40 +0000 | [diff] [blame] | 974 | static int doc_writev_ecc(struct mtd_info *mtd, const struct kvec *vecs, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 975 | unsigned long count, loff_t to, size_t *retlen, |
| 976 | u_char *eccbuf, struct nand_oobinfo *oobsel) |
| 977 | { |
| 978 | static char static_buf[512]; |
Ingo Molnar | 040d79f | 2006-03-31 02:29:40 -0800 | [diff] [blame] | 979 | static DEFINE_MUTEX(writev_buf_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 980 | |
| 981 | size_t totretlen = 0; |
| 982 | size_t thisvecofs = 0; |
| 983 | int ret= 0; |
| 984 | |
Ingo Molnar | 040d79f | 2006-03-31 02:29:40 -0800 | [diff] [blame] | 985 | mutex_lock(&writev_buf_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 986 | |
| 987 | while(count) { |
| 988 | size_t thislen, thisretlen; |
| 989 | unsigned char *buf; |
| 990 | |
| 991 | buf = vecs->iov_base + thisvecofs; |
| 992 | thislen = vecs->iov_len - thisvecofs; |
| 993 | |
| 994 | |
| 995 | if (thislen >= 512) { |
| 996 | thislen = thislen & ~(512-1); |
| 997 | thisvecofs += thislen; |
| 998 | } else { |
| 999 | /* Not enough to fill a page. Copy into buf */ |
| 1000 | memcpy(static_buf, buf, thislen); |
| 1001 | buf = &static_buf[thislen]; |
| 1002 | |
| 1003 | while(count && thislen < 512) { |
| 1004 | vecs++; |
| 1005 | count--; |
| 1006 | thisvecofs = min((512-thislen), vecs->iov_len); |
| 1007 | memcpy(buf, vecs->iov_base, thisvecofs); |
| 1008 | thislen += thisvecofs; |
| 1009 | buf += thisvecofs; |
| 1010 | } |
| 1011 | buf = static_buf; |
| 1012 | } |
| 1013 | if (count && thisvecofs == vecs->iov_len) { |
| 1014 | thisvecofs = 0; |
| 1015 | vecs++; |
| 1016 | count--; |
| 1017 | } |
| 1018 | ret = doc_write_ecc(mtd, to, thislen, &thisretlen, buf, eccbuf, oobsel); |
| 1019 | |
| 1020 | totretlen += thisretlen; |
| 1021 | |
| 1022 | if (ret || thisretlen != thislen) |
| 1023 | break; |
| 1024 | |
| 1025 | to += thislen; |
Thomas Gleixner | e5580fb | 2005-11-07 11:15:40 +0000 | [diff] [blame] | 1026 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1027 | |
Ingo Molnar | 040d79f | 2006-03-31 02:29:40 -0800 | [diff] [blame] | 1028 | mutex_unlock(&writev_buf_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1029 | *retlen = totretlen; |
| 1030 | return ret; |
| 1031 | } |
| 1032 | |
| 1033 | |
| 1034 | static int doc_read_oob(struct mtd_info *mtd, loff_t ofs, size_t len, |
| 1035 | size_t * retlen, u_char * buf) |
| 1036 | { |
| 1037 | struct DiskOnChip *this = mtd->priv; |
| 1038 | int len256 = 0, ret; |
| 1039 | struct Nand *mychip; |
| 1040 | |
Ingo Molnar | 48b1926 | 2006-03-31 02:29:41 -0800 | [diff] [blame] | 1041 | mutex_lock(&this->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1042 | |
| 1043 | mychip = &this->chips[ofs >> this->chipshift]; |
| 1044 | |
| 1045 | if (this->curfloor != mychip->floor) { |
| 1046 | DoC_SelectFloor(this, mychip->floor); |
| 1047 | DoC_SelectChip(this, mychip->chip); |
| 1048 | } else if (this->curchip != mychip->chip) { |
| 1049 | DoC_SelectChip(this, mychip->chip); |
| 1050 | } |
| 1051 | this->curfloor = mychip->floor; |
| 1052 | this->curchip = mychip->chip; |
| 1053 | |
| 1054 | /* update address for 2M x 8bit devices. OOB starts on the second */ |
| 1055 | /* page to maintain compatibility with doc_read_ecc. */ |
| 1056 | if (this->page256) { |
| 1057 | if (!(ofs & 0x8)) |
| 1058 | ofs += 0x100; |
| 1059 | else |
| 1060 | ofs -= 0x8; |
| 1061 | } |
| 1062 | |
| 1063 | DoC_Command(this, NAND_CMD_READOOB, CDSN_CTRL_WP); |
| 1064 | DoC_Address(this, ADDR_COLUMN_PAGE, ofs, CDSN_CTRL_WP, 0); |
| 1065 | |
| 1066 | /* treat crossing 8-byte OOB data for 2M x 8bit devices */ |
| 1067 | /* Note: datasheet says it should automaticaly wrap to the */ |
| 1068 | /* next OOB block, but it didn't work here. mf. */ |
| 1069 | if (this->page256 && ofs + len > (ofs | 0x7) + 1) { |
| 1070 | len256 = (ofs | 0x7) + 1 - ofs; |
| 1071 | DoC_ReadBuf(this, buf, len256); |
| 1072 | |
| 1073 | DoC_Command(this, NAND_CMD_READOOB, CDSN_CTRL_WP); |
| 1074 | DoC_Address(this, ADDR_COLUMN_PAGE, ofs & (~0x1ff), |
| 1075 | CDSN_CTRL_WP, 0); |
| 1076 | } |
| 1077 | |
| 1078 | DoC_ReadBuf(this, &buf[len256], len - len256); |
| 1079 | |
| 1080 | *retlen = len; |
| 1081 | /* Reading the full OOB data drops us off of the end of the page, |
| 1082 | * causing the flash device to go into busy mode, so we need |
| 1083 | * to wait until ready 11.4.1 and Toshiba TC58256FT docs */ |
Thomas Gleixner | e5580fb | 2005-11-07 11:15:40 +0000 | [diff] [blame] | 1084 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1085 | ret = DoC_WaitReady(this); |
| 1086 | |
Ingo Molnar | 48b1926 | 2006-03-31 02:29:41 -0800 | [diff] [blame] | 1087 | mutex_unlock(&this->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1088 | return ret; |
| 1089 | |
| 1090 | } |
| 1091 | |
| 1092 | static int doc_write_oob_nolock(struct mtd_info *mtd, loff_t ofs, size_t len, |
| 1093 | size_t * retlen, const u_char * buf) |
| 1094 | { |
| 1095 | struct DiskOnChip *this = mtd->priv; |
| 1096 | int len256 = 0; |
| 1097 | void __iomem *docptr = this->virtadr; |
| 1098 | struct Nand *mychip = &this->chips[ofs >> this->chipshift]; |
| 1099 | volatile int dummy; |
| 1100 | int status; |
| 1101 | |
| 1102 | // printk("doc_write_oob(%lx, %d): %2.2X %2.2X %2.2X %2.2X ... %2.2X %2.2X .. %2.2X %2.2X\n",(long)ofs, len, |
| 1103 | // buf[0], buf[1], buf[2], buf[3], buf[8], buf[9], buf[14],buf[15]); |
| 1104 | |
| 1105 | /* Find the chip which is to be used and select it */ |
| 1106 | if (this->curfloor != mychip->floor) { |
| 1107 | DoC_SelectFloor(this, mychip->floor); |
| 1108 | DoC_SelectChip(this, mychip->chip); |
| 1109 | } else if (this->curchip != mychip->chip) { |
| 1110 | DoC_SelectChip(this, mychip->chip); |
| 1111 | } |
| 1112 | this->curfloor = mychip->floor; |
| 1113 | this->curchip = mychip->chip; |
| 1114 | |
| 1115 | /* disable the ECC engine */ |
| 1116 | WriteDOC (DOC_ECC_RESET, docptr, ECCConf); |
| 1117 | WriteDOC (DOC_ECC_DIS, docptr, ECCConf); |
| 1118 | |
| 1119 | /* Reset the chip, see Software Requirement 11.4 item 1. */ |
| 1120 | DoC_Command(this, NAND_CMD_RESET, CDSN_CTRL_WP); |
| 1121 | |
| 1122 | /* issue the Read2 command to set the pointer to the Spare Data Area. */ |
| 1123 | DoC_Command(this, NAND_CMD_READOOB, CDSN_CTRL_WP); |
| 1124 | |
| 1125 | /* update address for 2M x 8bit devices. OOB starts on the second */ |
| 1126 | /* page to maintain compatibility with doc_read_ecc. */ |
| 1127 | if (this->page256) { |
| 1128 | if (!(ofs & 0x8)) |
| 1129 | ofs += 0x100; |
| 1130 | else |
| 1131 | ofs -= 0x8; |
| 1132 | } |
| 1133 | |
| 1134 | /* issue the Serial Data In command to initial the Page Program process */ |
| 1135 | DoC_Command(this, NAND_CMD_SEQIN, 0); |
| 1136 | DoC_Address(this, ADDR_COLUMN_PAGE, ofs, 0, 0); |
| 1137 | |
| 1138 | /* treat crossing 8-byte OOB data for 2M x 8bit devices */ |
| 1139 | /* Note: datasheet says it should automaticaly wrap to the */ |
| 1140 | /* next OOB block, but it didn't work here. mf. */ |
| 1141 | if (this->page256 && ofs + len > (ofs | 0x7) + 1) { |
| 1142 | len256 = (ofs | 0x7) + 1 - ofs; |
| 1143 | DoC_WriteBuf(this, buf, len256); |
| 1144 | |
| 1145 | DoC_Command(this, NAND_CMD_PAGEPROG, 0); |
| 1146 | DoC_Command(this, NAND_CMD_STATUS, 0); |
| 1147 | /* DoC_WaitReady() is implicit in DoC_Command */ |
| 1148 | |
| 1149 | if (DoC_is_Millennium(this)) { |
| 1150 | ReadDOC(docptr, ReadPipeInit); |
| 1151 | status = ReadDOC(docptr, LastDataRead); |
| 1152 | } else { |
| 1153 | dummy = ReadDOC(docptr, CDSNSlowIO); |
| 1154 | DoC_Delay(this, 2); |
| 1155 | status = ReadDOC_(docptr, this->ioreg); |
| 1156 | } |
| 1157 | |
| 1158 | if (status & 1) { |
| 1159 | printk(KERN_ERR "Error programming oob data\n"); |
| 1160 | /* There was an error */ |
| 1161 | *retlen = 0; |
| 1162 | return -EIO; |
| 1163 | } |
| 1164 | DoC_Command(this, NAND_CMD_SEQIN, 0); |
| 1165 | DoC_Address(this, ADDR_COLUMN_PAGE, ofs & (~0x1ff), 0, 0); |
| 1166 | } |
| 1167 | |
| 1168 | DoC_WriteBuf(this, &buf[len256], len - len256); |
| 1169 | |
| 1170 | DoC_Command(this, NAND_CMD_PAGEPROG, 0); |
| 1171 | DoC_Command(this, NAND_CMD_STATUS, 0); |
| 1172 | /* DoC_WaitReady() is implicit in DoC_Command */ |
| 1173 | |
| 1174 | if (DoC_is_Millennium(this)) { |
| 1175 | ReadDOC(docptr, ReadPipeInit); |
| 1176 | status = ReadDOC(docptr, LastDataRead); |
| 1177 | } else { |
| 1178 | dummy = ReadDOC(docptr, CDSNSlowIO); |
| 1179 | DoC_Delay(this, 2); |
| 1180 | status = ReadDOC_(docptr, this->ioreg); |
| 1181 | } |
| 1182 | |
| 1183 | if (status & 1) { |
| 1184 | printk(KERN_ERR "Error programming oob data\n"); |
| 1185 | /* There was an error */ |
| 1186 | *retlen = 0; |
| 1187 | return -EIO; |
| 1188 | } |
| 1189 | |
| 1190 | *retlen = len; |
| 1191 | return 0; |
| 1192 | |
| 1193 | } |
Thomas Gleixner | e5580fb | 2005-11-07 11:15:40 +0000 | [diff] [blame] | 1194 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1195 | static int doc_write_oob(struct mtd_info *mtd, loff_t ofs, size_t len, |
| 1196 | size_t * retlen, const u_char * buf) |
| 1197 | { |
| 1198 | struct DiskOnChip *this = mtd->priv; |
| 1199 | int ret; |
| 1200 | |
Ingo Molnar | 48b1926 | 2006-03-31 02:29:41 -0800 | [diff] [blame] | 1201 | mutex_lock(&this->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1202 | ret = doc_write_oob_nolock(mtd, ofs, len, retlen, buf); |
| 1203 | |
Ingo Molnar | 48b1926 | 2006-03-31 02:29:41 -0800 | [diff] [blame] | 1204 | mutex_unlock(&this->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1205 | return ret; |
| 1206 | } |
| 1207 | |
| 1208 | static int doc_erase(struct mtd_info *mtd, struct erase_info *instr) |
| 1209 | { |
| 1210 | struct DiskOnChip *this = mtd->priv; |
| 1211 | __u32 ofs = instr->addr; |
| 1212 | __u32 len = instr->len; |
| 1213 | volatile int dummy; |
| 1214 | void __iomem *docptr = this->virtadr; |
| 1215 | struct Nand *mychip; |
| 1216 | int status; |
| 1217 | |
Ingo Molnar | 48b1926 | 2006-03-31 02:29:41 -0800 | [diff] [blame] | 1218 | mutex_lock(&this->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1219 | |
| 1220 | if (ofs & (mtd->erasesize-1) || len & (mtd->erasesize-1)) { |
Ingo Molnar | 48b1926 | 2006-03-31 02:29:41 -0800 | [diff] [blame] | 1221 | mutex_unlock(&this->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1222 | return -EINVAL; |
| 1223 | } |
| 1224 | |
| 1225 | instr->state = MTD_ERASING; |
Thomas Gleixner | e5580fb | 2005-11-07 11:15:40 +0000 | [diff] [blame] | 1226 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1227 | /* FIXME: Do this in the background. Use timers or schedule_task() */ |
| 1228 | while(len) { |
| 1229 | mychip = &this->chips[ofs >> this->chipshift]; |
| 1230 | |
| 1231 | if (this->curfloor != mychip->floor) { |
| 1232 | DoC_SelectFloor(this, mychip->floor); |
| 1233 | DoC_SelectChip(this, mychip->chip); |
| 1234 | } else if (this->curchip != mychip->chip) { |
| 1235 | DoC_SelectChip(this, mychip->chip); |
| 1236 | } |
| 1237 | this->curfloor = mychip->floor; |
| 1238 | this->curchip = mychip->chip; |
| 1239 | |
| 1240 | DoC_Command(this, NAND_CMD_ERASE1, 0); |
| 1241 | DoC_Address(this, ADDR_PAGE, ofs, 0, 0); |
| 1242 | DoC_Command(this, NAND_CMD_ERASE2, 0); |
| 1243 | |
| 1244 | DoC_Command(this, NAND_CMD_STATUS, CDSN_CTRL_WP); |
| 1245 | |
| 1246 | if (DoC_is_Millennium(this)) { |
| 1247 | ReadDOC(docptr, ReadPipeInit); |
| 1248 | status = ReadDOC(docptr, LastDataRead); |
| 1249 | } else { |
| 1250 | dummy = ReadDOC(docptr, CDSNSlowIO); |
| 1251 | DoC_Delay(this, 2); |
| 1252 | status = ReadDOC_(docptr, this->ioreg); |
| 1253 | } |
| 1254 | |
| 1255 | if (status & 1) { |
| 1256 | printk(KERN_ERR "Error erasing at 0x%x\n", ofs); |
| 1257 | /* There was an error */ |
| 1258 | instr->state = MTD_ERASE_FAILED; |
| 1259 | goto callback; |
| 1260 | } |
| 1261 | ofs += mtd->erasesize; |
| 1262 | len -= mtd->erasesize; |
| 1263 | } |
| 1264 | instr->state = MTD_ERASE_DONE; |
| 1265 | |
| 1266 | callback: |
| 1267 | mtd_erase_callback(instr); |
| 1268 | |
Ingo Molnar | 48b1926 | 2006-03-31 02:29:41 -0800 | [diff] [blame] | 1269 | mutex_unlock(&this->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1270 | return 0; |
| 1271 | } |
| 1272 | |
| 1273 | |
| 1274 | /**************************************************************************** |
| 1275 | * |
| 1276 | * Module stuff |
| 1277 | * |
| 1278 | ****************************************************************************/ |
| 1279 | |
| 1280 | static int __init init_doc2000(void) |
| 1281 | { |
| 1282 | inter_module_register(im_name, THIS_MODULE, &DoC2k_init); |
| 1283 | return 0; |
| 1284 | } |
| 1285 | |
| 1286 | static void __exit cleanup_doc2000(void) |
| 1287 | { |
| 1288 | struct mtd_info *mtd; |
| 1289 | struct DiskOnChip *this; |
| 1290 | |
| 1291 | while ((mtd = doc2klist)) { |
| 1292 | this = mtd->priv; |
| 1293 | doc2klist = this->nextdoc; |
| 1294 | |
| 1295 | del_mtd_device(mtd); |
| 1296 | |
| 1297 | iounmap(this->virtadr); |
| 1298 | kfree(this->chips); |
| 1299 | kfree(mtd); |
| 1300 | } |
| 1301 | inter_module_unregister(im_name); |
| 1302 | } |
| 1303 | |
| 1304 | module_exit(cleanup_doc2000); |
| 1305 | module_init(init_doc2000); |
| 1306 | |
| 1307 | MODULE_LICENSE("GPL"); |
| 1308 | MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org> et al."); |
| 1309 | MODULE_DESCRIPTION("MTD driver for DiskOnChip 2000 and Millennium"); |
| 1310 | |