PageRenderTime 902ms CodeModel.GetById 13ms RepoModel.GetById 2ms app.codeStats 0ms

/drivers/mtd/devices/doc2000.c

https://bitbucket.org/slukk/jb-tsm-kernel-4.2
C | 1201 lines | 789 code | 235 blank | 177 comment | 155 complexity | ee2880301af6844367a35d2c1536162a MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, AGPL-1.0
  1. /*
  2. * Linux driver for Disk-On-Chip 2000 and Millennium
  3. * (c) 1999 Machine Vision Holdings, Inc.
  4. * (c) 1999, 2000 David Woodhouse <dwmw2@infradead.org>
  5. */
  6. #include <linux/kernel.h>
  7. #include <linux/module.h>
  8. #include <asm/errno.h>
  9. #include <asm/io.h>
  10. #include <asm/uaccess.h>
  11. #include <linux/delay.h>
  12. #include <linux/slab.h>
  13. #include <linux/sched.h>
  14. #include <linux/init.h>
  15. #include <linux/types.h>
  16. #include <linux/bitops.h>
  17. #include <linux/mutex.h>
  18. #include <linux/mtd/mtd.h>
  19. #include <linux/mtd/nand.h>
  20. #include <linux/mtd/doc2000.h>
  21. #define DOC_SUPPORT_2000
  22. #define DOC_SUPPORT_2000TSOP
  23. #define DOC_SUPPORT_MILLENNIUM
  24. #ifdef DOC_SUPPORT_2000
  25. #define DoC_is_2000(doc) (doc->ChipID == DOC_ChipID_Doc2k)
  26. #else
  27. #define DoC_is_2000(doc) (0)
  28. #endif
  29. #if defined(DOC_SUPPORT_2000TSOP) || defined(DOC_SUPPORT_MILLENNIUM)
  30. #define DoC_is_Millennium(doc) (doc->ChipID == DOC_ChipID_DocMil)
  31. #else
  32. #define DoC_is_Millennium(doc) (0)
  33. #endif
  34. /* #define ECC_DEBUG */
  35. /* I have no idea why some DoC chips can not use memcpy_from|to_io().
  36. * This may be due to the different revisions of the ASIC controller built-in or
  37. * simplily a QA/Bug issue. Who knows ?? If you have trouble, please uncomment
  38. * this:
  39. #undef USE_MEMCPY
  40. */
  41. static int doc_read(struct mtd_info *mtd, loff_t from, size_t len,
  42. size_t *retlen, u_char *buf);
  43. static int doc_write(struct mtd_info *mtd, loff_t to, size_t len,
  44. size_t *retlen, const u_char *buf);
  45. static int doc_read_oob(struct mtd_info *mtd, loff_t ofs,
  46. struct mtd_oob_ops *ops);
  47. static int doc_write_oob(struct mtd_info *mtd, loff_t ofs,
  48. struct mtd_oob_ops *ops);
  49. static int doc_write_oob_nolock(struct mtd_info *mtd, loff_t ofs, size_t len,
  50. size_t *retlen, const u_char *buf);
  51. static int doc_erase (struct mtd_info *mtd, struct erase_info *instr);
  52. static struct mtd_info *doc2klist = NULL;
  53. /* Perform the required delay cycles by reading from the appropriate register */
  54. static void DoC_Delay(struct DiskOnChip *doc, unsigned short cycles)
  55. {
  56. volatile char dummy;
  57. int i;
  58. for (i = 0; i < cycles; i++) {
  59. if (DoC_is_Millennium(doc))
  60. dummy = ReadDOC(doc->virtadr, NOP);
  61. else
  62. dummy = ReadDOC(doc->virtadr, DOCStatus);
  63. }
  64. }
  65. /* DOC_WaitReady: Wait for RDY line to be asserted by the flash chip */
  66. static int _DoC_WaitReady(struct DiskOnChip *doc)
  67. {
  68. void __iomem *docptr = doc->virtadr;
  69. unsigned long timeo = jiffies + (HZ * 10);
  70. DEBUG(MTD_DEBUG_LEVEL3,
  71. "_DoC_WaitReady called for out-of-line wait\n");
  72. /* Out-of-line routine to wait for chip response */
  73. while (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B)) {
  74. /* issue 2 read from NOP register after reading from CDSNControl register
  75. see Software Requirement 11.4 item 2. */
  76. DoC_Delay(doc, 2);
  77. if (time_after(jiffies, timeo)) {
  78. DEBUG(MTD_DEBUG_LEVEL2, "_DoC_WaitReady timed out.\n");
  79. return -EIO;
  80. }
  81. udelay(1);
  82. cond_resched();
  83. }
  84. return 0;
  85. }
  86. static inline int DoC_WaitReady(struct DiskOnChip *doc)
  87. {
  88. void __iomem *docptr = doc->virtadr;
  89. /* This is inline, to optimise the common case, where it's ready instantly */
  90. int ret = 0;
  91. /* 4 read form NOP register should be issued in prior to the read from CDSNControl
  92. see Software Requirement 11.4 item 2. */
  93. DoC_Delay(doc, 4);
  94. if (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B))
  95. /* Call the out-of-line routine to wait */
  96. ret = _DoC_WaitReady(doc);
  97. /* issue 2 read from NOP register after reading from CDSNControl register
  98. see Software Requirement 11.4 item 2. */
  99. DoC_Delay(doc, 2);
  100. return ret;
  101. }
  102. /* DoC_Command: Send a flash command to the flash chip through the CDSN Slow IO register to
  103. bypass the internal pipeline. Each of 4 delay cycles (read from the NOP register) is
  104. required after writing to CDSN Control register, see Software Requirement 11.4 item 3. */
  105. static int DoC_Command(struct DiskOnChip *doc, unsigned char command,
  106. unsigned char xtraflags)
  107. {
  108. void __iomem *docptr = doc->virtadr;
  109. if (DoC_is_2000(doc))
  110. xtraflags |= CDSN_CTRL_FLASH_IO;
  111. /* Assert the CLE (Command Latch Enable) line to the flash chip */
  112. WriteDOC(xtraflags | CDSN_CTRL_CLE | CDSN_CTRL_CE, docptr, CDSNControl);
  113. DoC_Delay(doc, 4); /* Software requirement 11.4.3 for Millennium */
  114. if (DoC_is_Millennium(doc))
  115. WriteDOC(command, docptr, CDSNSlowIO);
  116. /* Send the command */
  117. WriteDOC_(command, docptr, doc->ioreg);
  118. if (DoC_is_Millennium(doc))
  119. WriteDOC(command, docptr, WritePipeTerm);
  120. /* Lower the CLE line */
  121. WriteDOC(xtraflags | CDSN_CTRL_CE, docptr, CDSNControl);
  122. DoC_Delay(doc, 4); /* Software requirement 11.4.3 for Millennium */
  123. /* Wait for the chip to respond - Software requirement 11.4.1 (extended for any command) */
  124. return DoC_WaitReady(doc);
  125. }
  126. /* DoC_Address: Set the current address for the flash chip through the CDSN Slow IO register to
  127. bypass the internal pipeline. Each of 4 delay cycles (read from the NOP register) is
  128. required after writing to CDSN Control register, see Software Requirement 11.4 item 3. */
  129. static int DoC_Address(struct DiskOnChip *doc, int numbytes, unsigned long ofs,
  130. unsigned char xtraflags1, unsigned char xtraflags2)
  131. {
  132. int i;
  133. void __iomem *docptr = doc->virtadr;
  134. if (DoC_is_2000(doc))
  135. xtraflags1 |= CDSN_CTRL_FLASH_IO;
  136. /* Assert the ALE (Address Latch Enable) line to the flash chip */
  137. WriteDOC(xtraflags1 | CDSN_CTRL_ALE | CDSN_CTRL_CE, docptr, CDSNControl);
  138. DoC_Delay(doc, 4); /* Software requirement 11.4.3 for Millennium */
  139. /* Send the address */
  140. /* Devices with 256-byte page are addressed as:
  141. Column (bits 0-7), Page (bits 8-15, 16-23, 24-31)
  142. * there is no device on the market with page256
  143. and more than 24 bits.
  144. Devices with 512-byte page are addressed as:
  145. Column (bits 0-7), Page (bits 9-16, 17-24, 25-31)
  146. * 25-31 is sent only if the chip support it.
  147. * bit 8 changes the read command to be sent
  148. (NAND_CMD_READ0 or NAND_CMD_READ1).
  149. */
  150. if (numbytes == ADDR_COLUMN || numbytes == ADDR_COLUMN_PAGE) {
  151. if (DoC_is_Millennium(doc))
  152. WriteDOC(ofs & 0xff, docptr, CDSNSlowIO);
  153. WriteDOC_(ofs & 0xff, docptr, doc->ioreg);
  154. }
  155. if (doc->page256) {
  156. ofs = ofs >> 8;
  157. } else {
  158. ofs = ofs >> 9;
  159. }
  160. if (numbytes == ADDR_PAGE || numbytes == ADDR_COLUMN_PAGE) {
  161. for (i = 0; i < doc->pageadrlen; i++, ofs = ofs >> 8) {
  162. if (DoC_is_Millennium(doc))
  163. WriteDOC(ofs & 0xff, docptr, CDSNSlowIO);
  164. WriteDOC_(ofs & 0xff, docptr, doc->ioreg);
  165. }
  166. }
  167. if (DoC_is_Millennium(doc))
  168. WriteDOC(ofs & 0xff, docptr, WritePipeTerm);
  169. DoC_Delay(doc, 2); /* Needed for some slow flash chips. mf. */
  170. /* FIXME: The SlowIO's for millennium could be replaced by
  171. a single WritePipeTerm here. mf. */
  172. /* Lower the ALE line */
  173. WriteDOC(xtraflags1 | xtraflags2 | CDSN_CTRL_CE, docptr,
  174. CDSNControl);
  175. DoC_Delay(doc, 4); /* Software requirement 11.4.3 for Millennium */
  176. /* Wait for the chip to respond - Software requirement 11.4.1 */
  177. return DoC_WaitReady(doc);
  178. }
  179. /* Read a buffer from DoC, taking care of Millennium odditys */
  180. static void DoC_ReadBuf(struct DiskOnChip *doc, u_char * buf, int len)
  181. {
  182. volatile int dummy;
  183. int modulus = 0xffff;
  184. void __iomem *docptr = doc->virtadr;
  185. int i;
  186. if (len <= 0)
  187. return;
  188. if (DoC_is_Millennium(doc)) {
  189. /* Read the data via the internal pipeline through CDSN IO register,
  190. see Pipelined Read Operations 11.3 */
  191. dummy = ReadDOC(docptr, ReadPipeInit);
  192. /* Millennium should use the LastDataRead register - Pipeline Reads */
  193. len--;
  194. /* This is needed for correctly ECC calculation */
  195. modulus = 0xff;
  196. }
  197. for (i = 0; i < len; i++)
  198. buf[i] = ReadDOC_(docptr, doc->ioreg + (i & modulus));
  199. if (DoC_is_Millennium(doc)) {
  200. buf[i] = ReadDOC(docptr, LastDataRead);
  201. }
  202. }
  203. /* Write a buffer to DoC, taking care of Millennium odditys */
  204. static void DoC_WriteBuf(struct DiskOnChip *doc, const u_char * buf, int len)
  205. {
  206. void __iomem *docptr = doc->virtadr;
  207. int i;
  208. if (len <= 0)
  209. return;
  210. for (i = 0; i < len; i++)
  211. WriteDOC_(buf[i], docptr, doc->ioreg + i);
  212. if (DoC_is_Millennium(doc)) {
  213. WriteDOC(0x00, docptr, WritePipeTerm);
  214. }
  215. }
  216. /* DoC_SelectChip: Select a given flash chip within the current floor */
  217. static inline int DoC_SelectChip(struct DiskOnChip *doc, int chip)
  218. {
  219. void __iomem *docptr = doc->virtadr;
  220. /* Software requirement 11.4.4 before writing DeviceSelect */
  221. /* Deassert the CE line to eliminate glitches on the FCE# outputs */
  222. WriteDOC(CDSN_CTRL_WP, docptr, CDSNControl);
  223. DoC_Delay(doc, 4); /* Software requirement 11.4.3 for Millennium */
  224. /* Select the individual flash chip requested */
  225. WriteDOC(chip, docptr, CDSNDeviceSelect);
  226. DoC_Delay(doc, 4);
  227. /* Reassert the CE line */
  228. WriteDOC(CDSN_CTRL_CE | CDSN_CTRL_FLASH_IO | CDSN_CTRL_WP, docptr,
  229. CDSNControl);
  230. DoC_Delay(doc, 4); /* Software requirement 11.4.3 for Millennium */
  231. /* Wait for it to be ready */
  232. return DoC_WaitReady(doc);
  233. }
  234. /* DoC_SelectFloor: Select a given floor (bank of flash chips) */
  235. static inline int DoC_SelectFloor(struct DiskOnChip *doc, int floor)
  236. {
  237. void __iomem *docptr = doc->virtadr;
  238. /* Select the floor (bank) of chips required */
  239. WriteDOC(floor, docptr, FloorSelect);
  240. /* Wait for the chip to be ready */
  241. return DoC_WaitReady(doc);
  242. }
  243. /* DoC_IdentChip: Identify a given NAND chip given {floor,chip} */
  244. static int DoC_IdentChip(struct DiskOnChip *doc, int floor, int chip)
  245. {
  246. int mfr, id, i, j;
  247. volatile char dummy;
  248. /* Page in the required floor/chip */
  249. DoC_SelectFloor(doc, floor);
  250. DoC_SelectChip(doc, chip);
  251. /* Reset the chip */
  252. if (DoC_Command(doc, NAND_CMD_RESET, CDSN_CTRL_WP)) {
  253. DEBUG(MTD_DEBUG_LEVEL2,
  254. "DoC_Command (reset) for %d,%d returned true\n",
  255. floor, chip);
  256. return 0;
  257. }
  258. /* Read the NAND chip ID: 1. Send ReadID command */
  259. if (DoC_Command(doc, NAND_CMD_READID, CDSN_CTRL_WP)) {
  260. DEBUG(MTD_DEBUG_LEVEL2,
  261. "DoC_Command (ReadID) for %d,%d returned true\n",
  262. floor, chip);
  263. return 0;
  264. }
  265. /* Read the NAND chip ID: 2. Send address byte zero */
  266. DoC_Address(doc, ADDR_COLUMN, 0, CDSN_CTRL_WP, 0);
  267. /* Read the manufacturer and device id codes from the device */
  268. if (DoC_is_Millennium(doc)) {
  269. DoC_Delay(doc, 2);
  270. dummy = ReadDOC(doc->virtadr, ReadPipeInit);
  271. mfr = ReadDOC(doc->virtadr, LastDataRead);
  272. DoC_Delay(doc, 2);
  273. dummy = ReadDOC(doc->virtadr, ReadPipeInit);
  274. id = ReadDOC(doc->virtadr, LastDataRead);
  275. } else {
  276. /* CDSN Slow IO register see Software Req 11.4 item 5. */
  277. dummy = ReadDOC(doc->virtadr, CDSNSlowIO);
  278. DoC_Delay(doc, 2);
  279. mfr = ReadDOC_(doc->virtadr, doc->ioreg);
  280. /* CDSN Slow IO register see Software Req 11.4 item 5. */
  281. dummy = ReadDOC(doc->virtadr, CDSNSlowIO);
  282. DoC_Delay(doc, 2);
  283. id = ReadDOC_(doc->virtadr, doc->ioreg);
  284. }
  285. /* No response - return failure */
  286. if (mfr == 0xff || mfr == 0)
  287. return 0;
  288. /* Check it's the same as the first chip we identified.
  289. * M-Systems say that any given DiskOnChip device should only
  290. * contain _one_ type of flash part, although that's not a
  291. * hardware restriction. */
  292. if (doc->mfr) {
  293. if (doc->mfr == mfr && doc->id == id)
  294. return 1; /* This is the same as the first */
  295. else
  296. printk(KERN_WARNING
  297. "Flash chip at floor %d, chip %d is different:\n",
  298. floor, chip);
  299. }
  300. /* Print and store the manufacturer and ID codes. */
  301. for (i = 0; nand_flash_ids[i].name != NULL; i++) {
  302. if (id == nand_flash_ids[i].id) {
  303. /* Try to identify manufacturer */
  304. for (j = 0; nand_manuf_ids[j].id != 0x0; j++) {
  305. if (nand_manuf_ids[j].id == mfr)
  306. break;
  307. }
  308. printk(KERN_INFO
  309. "Flash chip found: Manufacturer ID: %2.2X, "
  310. "Chip ID: %2.2X (%s:%s)\n", mfr, id,
  311. nand_manuf_ids[j].name, nand_flash_ids[i].name);
  312. if (!doc->mfr) {
  313. doc->mfr = mfr;
  314. doc->id = id;
  315. doc->chipshift =
  316. ffs((nand_flash_ids[i].chipsize << 20)) - 1;
  317. doc->page256 = (nand_flash_ids[i].pagesize == 256) ? 1 : 0;
  318. doc->pageadrlen = doc->chipshift > 25 ? 3 : 2;
  319. doc->erasesize =
  320. nand_flash_ids[i].erasesize;
  321. return 1;
  322. }
  323. return 0;
  324. }
  325. }
  326. /* We haven't fully identified the chip. Print as much as we know. */
  327. printk(KERN_WARNING "Unknown flash chip found: %2.2X %2.2X\n",
  328. id, mfr);
  329. printk(KERN_WARNING "Please report to dwmw2@infradead.org\n");
  330. return 0;
  331. }
  332. /* DoC_ScanChips: Find all NAND chips present in a DiskOnChip, and identify them */
  333. static void DoC_ScanChips(struct DiskOnChip *this, int maxchips)
  334. {
  335. int floor, chip;
  336. int numchips[MAX_FLOORS];
  337. int ret = 1;
  338. this->numchips = 0;
  339. this->mfr = 0;
  340. this->id = 0;
  341. /* For each floor, find the number of valid chips it contains */
  342. for (floor = 0; floor < MAX_FLOORS; floor++) {
  343. ret = 1;
  344. numchips[floor] = 0;
  345. for (chip = 0; chip < maxchips && ret != 0; chip++) {
  346. ret = DoC_IdentChip(this, floor, chip);
  347. if (ret) {
  348. numchips[floor]++;
  349. this->numchips++;
  350. }
  351. }
  352. }
  353. /* If there are none at all that we recognise, bail */
  354. if (!this->numchips) {
  355. printk(KERN_NOTICE "No flash chips recognised.\n");
  356. return;
  357. }
  358. /* Allocate an array to hold the information for each chip */
  359. this->chips = kmalloc(sizeof(struct Nand) * this->numchips, GFP_KERNEL);
  360. if (!this->chips) {
  361. printk(KERN_NOTICE "No memory for allocating chip info structures\n");
  362. return;
  363. }
  364. ret = 0;
  365. /* Fill out the chip array with {floor, chipno} for each
  366. * detected chip in the device. */
  367. for (floor = 0; floor < MAX_FLOORS; floor++) {
  368. for (chip = 0; chip < numchips[floor]; chip++) {
  369. this->chips[ret].floor = floor;
  370. this->chips[ret].chip = chip;
  371. this->chips[ret].curadr = 0;
  372. this->chips[ret].curmode = 0x50;
  373. ret++;
  374. }
  375. }
  376. /* Calculate and print the total size of the device */
  377. this->totlen = this->numchips * (1 << this->chipshift);
  378. printk(KERN_INFO "%d flash chips found. Total DiskOnChip size: %ld MiB\n",
  379. this->numchips, this->totlen >> 20);
  380. }
  381. static int DoC2k_is_alias(struct DiskOnChip *doc1, struct DiskOnChip *doc2)
  382. {
  383. int tmp1, tmp2, retval;
  384. if (doc1->physadr == doc2->physadr)
  385. return 1;
  386. /* Use the alias resolution register which was set aside for this
  387. * purpose. If it's value is the same on both chips, they might
  388. * be the same chip, and we write to one and check for a change in
  389. * the other. It's unclear if this register is usuable in the
  390. * DoC 2000 (it's in the Millennium docs), but it seems to work. */
  391. tmp1 = ReadDOC(doc1->virtadr, AliasResolution);
  392. tmp2 = ReadDOC(doc2->virtadr, AliasResolution);
  393. if (tmp1 != tmp2)
  394. return 0;
  395. WriteDOC((tmp1 + 1) % 0xff, doc1->virtadr, AliasResolution);
  396. tmp2 = ReadDOC(doc2->virtadr, AliasResolution);
  397. if (tmp2 == (tmp1 + 1) % 0xff)
  398. retval = 1;
  399. else
  400. retval = 0;
  401. /* Restore register contents. May not be necessary, but do it just to
  402. * be safe. */
  403. WriteDOC(tmp1, doc1->virtadr, AliasResolution);
  404. return retval;
  405. }
  406. /* This routine is found from the docprobe code by symbol_get(),
  407. * which will bump the use count of this module. */
  408. void DoC2k_init(struct mtd_info *mtd)
  409. {
  410. struct DiskOnChip *this = mtd->priv;
  411. struct DiskOnChip *old = NULL;
  412. int maxchips;
  413. /* We must avoid being called twice for the same device. */
  414. if (doc2klist)
  415. old = doc2klist->priv;
  416. while (old) {
  417. if (DoC2k_is_alias(old, this)) {
  418. printk(KERN_NOTICE
  419. "Ignoring DiskOnChip 2000 at 0x%lX - already configured\n",
  420. this->physadr);
  421. iounmap(this->virtadr);
  422. kfree(mtd);
  423. return;
  424. }
  425. if (old->nextdoc)
  426. old = old->nextdoc->priv;
  427. else
  428. old = NULL;
  429. }
  430. switch (this->ChipID) {
  431. case DOC_ChipID_Doc2kTSOP:
  432. mtd->name = "DiskOnChip 2000 TSOP";
  433. this->ioreg = DoC_Mil_CDSN_IO;
  434. /* Pretend it's a Millennium */
  435. this->ChipID = DOC_ChipID_DocMil;
  436. maxchips = MAX_CHIPS;
  437. break;
  438. case DOC_ChipID_Doc2k:
  439. mtd->name = "DiskOnChip 2000";
  440. this->ioreg = DoC_2k_CDSN_IO;
  441. maxchips = MAX_CHIPS;
  442. break;
  443. case DOC_ChipID_DocMil:
  444. mtd->name = "DiskOnChip Millennium";
  445. this->ioreg = DoC_Mil_CDSN_IO;
  446. maxchips = MAX_CHIPS_MIL;
  447. break;
  448. default:
  449. printk("Unknown ChipID 0x%02x\n", this->ChipID);
  450. kfree(mtd);
  451. iounmap(this->virtadr);
  452. return;
  453. }
  454. printk(KERN_NOTICE "%s found at address 0x%lX\n", mtd->name,
  455. this->physadr);
  456. mtd->type = MTD_NANDFLASH;
  457. mtd->flags = MTD_CAP_NANDFLASH;
  458. mtd->size = 0;
  459. mtd->erasesize = 0;
  460. mtd->writesize = 512;
  461. mtd->oobsize = 16;
  462. mtd->owner = THIS_MODULE;
  463. mtd->erase = doc_erase;
  464. mtd->point = NULL;
  465. mtd->unpoint = NULL;
  466. mtd->read = doc_read;
  467. mtd->write = doc_write;
  468. mtd->read_oob = doc_read_oob;
  469. mtd->write_oob = doc_write_oob;
  470. mtd->sync = NULL;
  471. this->totlen = 0;
  472. this->numchips = 0;
  473. this->curfloor = -1;
  474. this->curchip = -1;
  475. mutex_init(&this->lock);
  476. /* Ident all the chips present. */
  477. DoC_ScanChips(this, maxchips);
  478. if (!this->totlen) {
  479. kfree(mtd);
  480. iounmap(this->virtadr);
  481. } else {
  482. this->nextdoc = doc2klist;
  483. doc2klist = mtd;
  484. mtd->size = this->totlen;
  485. mtd->erasesize = this->erasesize;
  486. mtd_device_register(mtd, NULL, 0);
  487. return;
  488. }
  489. }
  490. EXPORT_SYMBOL_GPL(DoC2k_init);
  491. static int doc_read(struct mtd_info *mtd, loff_t from, size_t len,
  492. size_t * retlen, u_char * buf)
  493. {
  494. struct DiskOnChip *this = mtd->priv;
  495. void __iomem *docptr = this->virtadr;
  496. struct Nand *mychip;
  497. unsigned char syndrome[6], eccbuf[6];
  498. volatile char dummy;
  499. int i, len256 = 0, ret=0;
  500. size_t left = len;
  501. /* Don't allow read past end of device */
  502. if (from >= this->totlen)
  503. return -EINVAL;
  504. mutex_lock(&this->lock);
  505. *retlen = 0;
  506. while (left) {
  507. len = left;
  508. /* Don't allow a single read to cross a 512-byte block boundary */
  509. if (from + len > ((from | 0x1ff) + 1))
  510. len = ((from | 0x1ff) + 1) - from;
  511. /* The ECC will not be calculated correctly if less than 512 is read */
  512. if (len != 0x200)
  513. printk(KERN_WARNING
  514. "ECC needs a full sector read (adr: %lx size %lx)\n",
  515. (long) from, (long) len);
  516. /* printk("DoC_Read (adr: %lx size %lx)\n", (long) from, (long) len); */
  517. /* Find the chip which is to be used and select it */
  518. mychip = &this->chips[from >> (this->chipshift)];
  519. if (this->curfloor != mychip->floor) {
  520. DoC_SelectFloor(this, mychip->floor);
  521. DoC_SelectChip(this, mychip->chip);
  522. } else if (this->curchip != mychip->chip) {
  523. DoC_SelectChip(this, mychip->chip);
  524. }
  525. this->curfloor = mychip->floor;
  526. this->curchip = mychip->chip;
  527. DoC_Command(this,
  528. (!this->page256
  529. && (from & 0x100)) ? NAND_CMD_READ1 : NAND_CMD_READ0,
  530. CDSN_CTRL_WP);
  531. DoC_Address(this, ADDR_COLUMN_PAGE, from, CDSN_CTRL_WP,
  532. CDSN_CTRL_ECC_IO);
  533. /* Prime the ECC engine */
  534. WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
  535. WriteDOC(DOC_ECC_EN, docptr, ECCConf);
  536. /* treat crossing 256-byte sector for 2M x 8bits devices */
  537. if (this->page256 && from + len > (from | 0xff) + 1) {
  538. len256 = (from | 0xff) + 1 - from;
  539. DoC_ReadBuf(this, buf, len256);
  540. DoC_Command(this, NAND_CMD_READ0, CDSN_CTRL_WP);
  541. DoC_Address(this, ADDR_COLUMN_PAGE, from + len256,
  542. CDSN_CTRL_WP, CDSN_CTRL_ECC_IO);
  543. }
  544. DoC_ReadBuf(this, &buf[len256], len - len256);
  545. /* Let the caller know we completed it */
  546. *retlen += len;
  547. /* Read the ECC data through the DiskOnChip ECC logic */
  548. /* Note: this will work even with 2M x 8bit devices as */
  549. /* they have 8 bytes of OOB per 256 page. mf. */
  550. DoC_ReadBuf(this, eccbuf, 6);
  551. /* Flush the pipeline */
  552. if (DoC_is_Millennium(this)) {
  553. dummy = ReadDOC(docptr, ECCConf);
  554. dummy = ReadDOC(docptr, ECCConf);
  555. i = ReadDOC(docptr, ECCConf);
  556. } else {
  557. dummy = ReadDOC(docptr, 2k_ECCStatus);
  558. dummy = ReadDOC(docptr, 2k_ECCStatus);
  559. i = ReadDOC(docptr, 2k_ECCStatus);
  560. }
  561. /* Check the ECC Status */
  562. if (i & 0x80) {
  563. int nb_errors;
  564. /* There was an ECC error */
  565. #ifdef ECC_DEBUG
  566. printk(KERN_ERR "DiskOnChip ECC Error: Read at %lx\n", (long)from);
  567. #endif
  568. /* Read the ECC syndrom through the DiskOnChip ECC
  569. logic. These syndrome will be all ZERO when there
  570. is no error */
  571. for (i = 0; i < 6; i++) {
  572. syndrome[i] =
  573. ReadDOC(docptr, ECCSyndrome0 + i);
  574. }
  575. nb_errors = doc_decode_ecc(buf, syndrome);
  576. #ifdef ECC_DEBUG
  577. printk(KERN_ERR "Errors corrected: %x\n", nb_errors);
  578. #endif
  579. if (nb_errors < 0) {
  580. /* We return error, but have actually done the
  581. read. Not that this can be told to
  582. user-space, via sys_read(), but at least
  583. MTD-aware stuff can know about it by
  584. checking *retlen */
  585. ret = -EIO;
  586. }
  587. }
  588. #ifdef PSYCHO_DEBUG
  589. printk(KERN_DEBUG "ECC DATA at %lxB: %2.2X %2.2X %2.2X %2.2X %2.2X %2.2X\n",
  590. (long)from, eccbuf[0], eccbuf[1], eccbuf[2],
  591. eccbuf[3], eccbuf[4], eccbuf[5]);
  592. #endif
  593. /* disable the ECC engine */
  594. WriteDOC(DOC_ECC_DIS, docptr , ECCConf);
  595. /* according to 11.4.1, we need to wait for the busy line
  596. * drop if we read to the end of the page. */
  597. if(0 == ((from + len) & 0x1ff))
  598. {
  599. DoC_WaitReady(this);
  600. }
  601. from += len;
  602. left -= len;
  603. buf += len;
  604. }
  605. mutex_unlock(&this->lock);
  606. return ret;
  607. }
  608. static int doc_write(struct mtd_info *mtd, loff_t to, size_t len,
  609. size_t * retlen, const u_char * buf)
  610. {
  611. struct DiskOnChip *this = mtd->priv;
  612. int di; /* Yes, DI is a hangover from when I was disassembling the binary driver */
  613. void __iomem *docptr = this->virtadr;
  614. unsigned char eccbuf[6];
  615. volatile char dummy;
  616. int len256 = 0;
  617. struct Nand *mychip;
  618. size_t left = len;
  619. int status;
  620. /* Don't allow write past end of device */
  621. if (to >= this->totlen)
  622. return -EINVAL;
  623. mutex_lock(&this->lock);
  624. *retlen = 0;
  625. while (left) {
  626. len = left;
  627. /* Don't allow a single write to cross a 512-byte block boundary */
  628. if (to + len > ((to | 0x1ff) + 1))
  629. len = ((to | 0x1ff) + 1) - to;
  630. /* The ECC will not be calculated correctly if less than 512 is written */
  631. /* DBB-
  632. if (len != 0x200 && eccbuf)
  633. printk(KERN_WARNING
  634. "ECC needs a full sector write (adr: %lx size %lx)\n",
  635. (long) to, (long) len);
  636. -DBB */
  637. /* printk("DoC_Write (adr: %lx size %lx)\n", (long) to, (long) len); */
  638. /* Find the chip which is to be used and select it */
  639. mychip = &this->chips[to >> (this->chipshift)];
  640. if (this->curfloor != mychip->floor) {
  641. DoC_SelectFloor(this, mychip->floor);
  642. DoC_SelectChip(this, mychip->chip);
  643. } else if (this->curchip != mychip->chip) {
  644. DoC_SelectChip(this, mychip->chip);
  645. }
  646. this->curfloor = mychip->floor;
  647. this->curchip = mychip->chip;
  648. /* Set device to main plane of flash */
  649. DoC_Command(this, NAND_CMD_RESET, CDSN_CTRL_WP);
  650. DoC_Command(this,
  651. (!this->page256
  652. && (to & 0x100)) ? NAND_CMD_READ1 : NAND_CMD_READ0,
  653. CDSN_CTRL_WP);
  654. DoC_Command(this, NAND_CMD_SEQIN, 0);
  655. DoC_Address(this, ADDR_COLUMN_PAGE, to, 0, CDSN_CTRL_ECC_IO);
  656. /* Prime the ECC engine */
  657. WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
  658. WriteDOC(DOC_ECC_EN | DOC_ECC_RW, docptr, ECCConf);
  659. /* treat crossing 256-byte sector for 2M x 8bits devices */
  660. if (this->page256 && to + len > (to | 0xff) + 1) {
  661. len256 = (to | 0xff) + 1 - to;
  662. DoC_WriteBuf(this, buf, len256);
  663. DoC_Command(this, NAND_CMD_PAGEPROG, 0);
  664. DoC_Command(this, NAND_CMD_STATUS, CDSN_CTRL_WP);
  665. /* There's an implicit DoC_WaitReady() in DoC_Command */
  666. dummy = ReadDOC(docptr, CDSNSlowIO);
  667. DoC_Delay(this, 2);
  668. if (ReadDOC_(docptr, this->ioreg) & 1) {
  669. printk(KERN_ERR "Error programming flash\n");
  670. /* Error in programming */
  671. *retlen = 0;
  672. mutex_unlock(&this->lock);
  673. return -EIO;
  674. }
  675. DoC_Command(this, NAND_CMD_SEQIN, 0);
  676. DoC_Address(this, ADDR_COLUMN_PAGE, to + len256, 0,
  677. CDSN_CTRL_ECC_IO);
  678. }
  679. DoC_WriteBuf(this, &buf[len256], len - len256);
  680. WriteDOC(CDSN_CTRL_ECC_IO | CDSN_CTRL_CE, docptr, CDSNControl);
  681. if (DoC_is_Millennium(this)) {
  682. WriteDOC(0, docptr, NOP);
  683. WriteDOC(0, docptr, NOP);
  684. WriteDOC(0, docptr, NOP);
  685. } else {
  686. WriteDOC_(0, docptr, this->ioreg);
  687. WriteDOC_(0, docptr, this->ioreg);
  688. WriteDOC_(0, docptr, this->ioreg);
  689. }
  690. WriteDOC(CDSN_CTRL_ECC_IO | CDSN_CTRL_FLASH_IO | CDSN_CTRL_CE, docptr,
  691. CDSNControl);
  692. /* Read the ECC data through the DiskOnChip ECC logic */
  693. for (di = 0; di < 6; di++) {
  694. eccbuf[di] = ReadDOC(docptr, ECCSyndrome0 + di);
  695. }
  696. /* Reset the ECC engine */
  697. WriteDOC(DOC_ECC_DIS, docptr, ECCConf);
  698. #ifdef PSYCHO_DEBUG
  699. printk
  700. ("OOB data at %lx is %2.2X %2.2X %2.2X %2.2X %2.2X %2.2X\n",
  701. (long) to, eccbuf[0], eccbuf[1], eccbuf[2], eccbuf[3],
  702. eccbuf[4], eccbuf[5]);
  703. #endif
  704. DoC_Command(this, NAND_CMD_PAGEPROG, 0);
  705. DoC_Command(this, NAND_CMD_STATUS, CDSN_CTRL_WP);
  706. /* There's an implicit DoC_WaitReady() in DoC_Command */
  707. if (DoC_is_Millennium(this)) {
  708. ReadDOC(docptr, ReadPipeInit);
  709. status = ReadDOC(docptr, LastDataRead);
  710. } else {
  711. dummy = ReadDOC(docptr, CDSNSlowIO);
  712. DoC_Delay(this, 2);
  713. status = ReadDOC_(docptr, this->ioreg);
  714. }
  715. if (status & 1) {
  716. printk(KERN_ERR "Error programming flash\n");
  717. /* Error in programming */
  718. *retlen = 0;
  719. mutex_unlock(&this->lock);
  720. return -EIO;
  721. }
  722. /* Let the caller know we completed it */
  723. *retlen += len;
  724. {
  725. unsigned char x[8];
  726. size_t dummy;
  727. int ret;
  728. /* Write the ECC data to flash */
  729. for (di=0; di<6; di++)
  730. x[di] = eccbuf[di];
  731. x[6]=0x55;
  732. x[7]=0x55;
  733. ret = doc_write_oob_nolock(mtd, to, 8, &dummy, x);
  734. if (ret) {
  735. mutex_unlock(&this->lock);
  736. return ret;
  737. }
  738. }
  739. to += len;
  740. left -= len;
  741. buf += len;
  742. }
  743. mutex_unlock(&this->lock);
  744. return 0;
  745. }
  746. static int doc_read_oob(struct mtd_info *mtd, loff_t ofs,
  747. struct mtd_oob_ops *ops)
  748. {
  749. struct DiskOnChip *this = mtd->priv;
  750. int len256 = 0, ret;
  751. struct Nand *mychip;
  752. uint8_t *buf = ops->oobbuf;
  753. size_t len = ops->len;
  754. BUG_ON(ops->mode != MTD_OOB_PLACE);
  755. ofs += ops->ooboffs;
  756. mutex_lock(&this->lock);
  757. mychip = &this->chips[ofs >> this->chipshift];
  758. if (this->curfloor != mychip->floor) {
  759. DoC_SelectFloor(this, mychip->floor);
  760. DoC_SelectChip(this, mychip->chip);
  761. } else if (this->curchip != mychip->chip) {
  762. DoC_SelectChip(this, mychip->chip);
  763. }
  764. this->curfloor = mychip->floor;
  765. this->curchip = mychip->chip;
  766. /* update address for 2M x 8bit devices. OOB starts on the second */
  767. /* page to maintain compatibility with doc_read_ecc. */
  768. if (this->page256) {
  769. if (!(ofs & 0x8))
  770. ofs += 0x100;
  771. else
  772. ofs -= 0x8;
  773. }
  774. DoC_Command(this, NAND_CMD_READOOB, CDSN_CTRL_WP);
  775. DoC_Address(this, ADDR_COLUMN_PAGE, ofs, CDSN_CTRL_WP, 0);
  776. /* treat crossing 8-byte OOB data for 2M x 8bit devices */
  777. /* Note: datasheet says it should automaticaly wrap to the */
  778. /* next OOB block, but it didn't work here. mf. */
  779. if (this->page256 && ofs + len > (ofs | 0x7) + 1) {
  780. len256 = (ofs | 0x7) + 1 - ofs;
  781. DoC_ReadBuf(this, buf, len256);
  782. DoC_Command(this, NAND_CMD_READOOB, CDSN_CTRL_WP);
  783. DoC_Address(this, ADDR_COLUMN_PAGE, ofs & (~0x1ff),
  784. CDSN_CTRL_WP, 0);
  785. }
  786. DoC_ReadBuf(this, &buf[len256], len - len256);
  787. ops->retlen = len;
  788. /* Reading the full OOB data drops us off of the end of the page,
  789. * causing the flash device to go into busy mode, so we need
  790. * to wait until ready 11.4.1 and Toshiba TC58256FT docs */
  791. ret = DoC_WaitReady(this);
  792. mutex_unlock(&this->lock);
  793. return ret;
  794. }
  795. static int doc_write_oob_nolock(struct mtd_info *mtd, loff_t ofs, size_t len,
  796. size_t * retlen, const u_char * buf)
  797. {
  798. struct DiskOnChip *this = mtd->priv;
  799. int len256 = 0;
  800. void __iomem *docptr = this->virtadr;
  801. struct Nand *mychip = &this->chips[ofs >> this->chipshift];
  802. volatile int dummy;
  803. int status;
  804. // 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,
  805. // buf[0], buf[1], buf[2], buf[3], buf[8], buf[9], buf[14],buf[15]);
  806. /* Find the chip which is to be used and select it */
  807. if (this->curfloor != mychip->floor) {
  808. DoC_SelectFloor(this, mychip->floor);
  809. DoC_SelectChip(this, mychip->chip);
  810. } else if (this->curchip != mychip->chip) {
  811. DoC_SelectChip(this, mychip->chip);
  812. }
  813. this->curfloor = mychip->floor;
  814. this->curchip = mychip->chip;
  815. /* disable the ECC engine */
  816. WriteDOC (DOC_ECC_RESET, docptr, ECCConf);
  817. WriteDOC (DOC_ECC_DIS, docptr, ECCConf);
  818. /* Reset the chip, see Software Requirement 11.4 item 1. */
  819. DoC_Command(this, NAND_CMD_RESET, CDSN_CTRL_WP);
  820. /* issue the Read2 command to set the pointer to the Spare Data Area. */
  821. DoC_Command(this, NAND_CMD_READOOB, CDSN_CTRL_WP);
  822. /* update address for 2M x 8bit devices. OOB starts on the second */
  823. /* page to maintain compatibility with doc_read_ecc. */
  824. if (this->page256) {
  825. if (!(ofs & 0x8))
  826. ofs += 0x100;
  827. else
  828. ofs -= 0x8;
  829. }
  830. /* issue the Serial Data In command to initial the Page Program process */
  831. DoC_Command(this, NAND_CMD_SEQIN, 0);
  832. DoC_Address(this, ADDR_COLUMN_PAGE, ofs, 0, 0);
  833. /* treat crossing 8-byte OOB data for 2M x 8bit devices */
  834. /* Note: datasheet says it should automaticaly wrap to the */
  835. /* next OOB block, but it didn't work here. mf. */
  836. if (this->page256 && ofs + len > (ofs | 0x7) + 1) {
  837. len256 = (ofs | 0x7) + 1 - ofs;
  838. DoC_WriteBuf(this, buf, len256);
  839. DoC_Command(this, NAND_CMD_PAGEPROG, 0);
  840. DoC_Command(this, NAND_CMD_STATUS, 0);
  841. /* DoC_WaitReady() is implicit in DoC_Command */
  842. if (DoC_is_Millennium(this)) {
  843. ReadDOC(docptr, ReadPipeInit);
  844. status = ReadDOC(docptr, LastDataRead);
  845. } else {
  846. dummy = ReadDOC(docptr, CDSNSlowIO);
  847. DoC_Delay(this, 2);
  848. status = ReadDOC_(docptr, this->ioreg);
  849. }
  850. if (status & 1) {
  851. printk(KERN_ERR "Error programming oob data\n");
  852. /* There was an error */
  853. *retlen = 0;
  854. return -EIO;
  855. }
  856. DoC_Command(this, NAND_CMD_SEQIN, 0);
  857. DoC_Address(this, ADDR_COLUMN_PAGE, ofs & (~0x1ff), 0, 0);
  858. }
  859. DoC_WriteBuf(this, &buf[len256], len - len256);
  860. DoC_Command(this, NAND_CMD_PAGEPROG, 0);
  861. DoC_Command(this, NAND_CMD_STATUS, 0);
  862. /* DoC_WaitReady() is implicit in DoC_Command */
  863. if (DoC_is_Millennium(this)) {
  864. ReadDOC(docptr, ReadPipeInit);
  865. status = ReadDOC(docptr, LastDataRead);
  866. } else {
  867. dummy = ReadDOC(docptr, CDSNSlowIO);
  868. DoC_Delay(this, 2);
  869. status = ReadDOC_(docptr, this->ioreg);
  870. }
  871. if (status & 1) {
  872. printk(KERN_ERR "Error programming oob data\n");
  873. /* There was an error */
  874. *retlen = 0;
  875. return -EIO;
  876. }
  877. *retlen = len;
  878. return 0;
  879. }
  880. static int doc_write_oob(struct mtd_info *mtd, loff_t ofs,
  881. struct mtd_oob_ops *ops)
  882. {
  883. struct DiskOnChip *this = mtd->priv;
  884. int ret;
  885. BUG_ON(ops->mode != MTD_OOB_PLACE);
  886. mutex_lock(&this->lock);
  887. ret = doc_write_oob_nolock(mtd, ofs + ops->ooboffs, ops->len,
  888. &ops->retlen, ops->oobbuf);
  889. mutex_unlock(&this->lock);
  890. return ret;
  891. }
  892. static int doc_erase(struct mtd_info *mtd, struct erase_info *instr)
  893. {
  894. struct DiskOnChip *this = mtd->priv;
  895. __u32 ofs = instr->addr;
  896. __u32 len = instr->len;
  897. volatile int dummy;
  898. void __iomem *docptr = this->virtadr;
  899. struct Nand *mychip;
  900. int status;
  901. mutex_lock(&this->lock);
  902. if (ofs & (mtd->erasesize-1) || len & (mtd->erasesize-1)) {
  903. mutex_unlock(&this->lock);
  904. return -EINVAL;
  905. }
  906. instr->state = MTD_ERASING;
  907. /* FIXME: Do this in the background. Use timers or schedule_task() */
  908. while(len) {
  909. mychip = &this->chips[ofs >> this->chipshift];
  910. if (this->curfloor != mychip->floor) {
  911. DoC_SelectFloor(this, mychip->floor);
  912. DoC_SelectChip(this, mychip->chip);
  913. } else if (this->curchip != mychip->chip) {
  914. DoC_SelectChip(this, mychip->chip);
  915. }
  916. this->curfloor = mychip->floor;
  917. this->curchip = mychip->chip;
  918. DoC_Command(this, NAND_CMD_ERASE1, 0);
  919. DoC_Address(this, ADDR_PAGE, ofs, 0, 0);
  920. DoC_Command(this, NAND_CMD_ERASE2, 0);
  921. DoC_Command(this, NAND_CMD_STATUS, CDSN_CTRL_WP);
  922. if (DoC_is_Millennium(this)) {
  923. ReadDOC(docptr, ReadPipeInit);
  924. status = ReadDOC(docptr, LastDataRead);
  925. } else {
  926. dummy = ReadDOC(docptr, CDSNSlowIO);
  927. DoC_Delay(this, 2);
  928. status = ReadDOC_(docptr, this->ioreg);
  929. }
  930. if (status & 1) {
  931. printk(KERN_ERR "Error erasing at 0x%x\n", ofs);
  932. /* There was an error */
  933. instr->state = MTD_ERASE_FAILED;
  934. goto callback;
  935. }
  936. ofs += mtd->erasesize;
  937. len -= mtd->erasesize;
  938. }
  939. instr->state = MTD_ERASE_DONE;
  940. callback:
  941. mtd_erase_callback(instr);
  942. mutex_unlock(&this->lock);
  943. return 0;
  944. }
  945. /****************************************************************************
  946. *
  947. * Module stuff
  948. *
  949. ****************************************************************************/
  950. static void __exit cleanup_doc2000(void)
  951. {
  952. struct mtd_info *mtd;
  953. struct DiskOnChip *this;
  954. while ((mtd = doc2klist)) {
  955. this = mtd->priv;
  956. doc2klist = this->nextdoc;
  957. mtd_device_unregister(mtd);
  958. iounmap(this->virtadr);
  959. kfree(this->chips);
  960. kfree(mtd);
  961. }
  962. }
  963. module_exit(cleanup_doc2000);
  964. MODULE_LICENSE("GPL");
  965. MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org> et al.");
  966. MODULE_DESCRIPTION("MTD driver for DiskOnChip 2000 and Millennium");