doc2001.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823
  1. /*
  2. * Linux driver for Disk-On-Chip 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/init.h>
  14. #include <linux/types.h>
  15. #include <linux/bitops.h>
  16. #include <linux/mtd/mtd.h>
  17. #include <linux/mtd/nand.h>
  18. #include <linux/mtd/doc2000.h>
  19. /* #define ECC_DEBUG */
  20. /* I have no idea why some DoC chips can not use memcop_form|to_io().
  21. * This may be due to the different revisions of the ASIC controller built-in or
  22. * simplily a QA/Bug issue. Who knows ?? If you have trouble, please uncomment
  23. * this:*/
  24. #undef USE_MEMCPY
  25. static int doc_read(struct mtd_info *mtd, loff_t from, size_t len,
  26. size_t *retlen, u_char *buf);
  27. static int doc_write(struct mtd_info *mtd, loff_t to, size_t len,
  28. size_t *retlen, const u_char *buf);
  29. static int doc_read_oob(struct mtd_info *mtd, loff_t ofs,
  30. struct mtd_oob_ops *ops);
  31. static int doc_write_oob(struct mtd_info *mtd, loff_t ofs,
  32. struct mtd_oob_ops *ops);
  33. static int doc_erase (struct mtd_info *mtd, struct erase_info *instr);
  34. static struct mtd_info *docmillist = NULL;
  35. /* Perform the required delay cycles by reading from the NOP register */
  36. static void DoC_Delay(void __iomem * docptr, unsigned short cycles)
  37. {
  38. volatile char dummy;
  39. int i;
  40. for (i = 0; i < cycles; i++)
  41. dummy = ReadDOC(docptr, NOP);
  42. }
  43. /* DOC_WaitReady: Wait for RDY line to be asserted by the flash chip */
  44. static int _DoC_WaitReady(void __iomem * docptr)
  45. {
  46. unsigned short c = 0xffff;
  47. pr_debug("_DoC_WaitReady called for out-of-line wait\n");
  48. /* Out-of-line routine to wait for chip response */
  49. while (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B) && --c)
  50. ;
  51. if (c == 0)
  52. pr_debug("_DoC_WaitReady timed out.\n");
  53. return (c == 0);
  54. }
  55. static inline int DoC_WaitReady(void __iomem * docptr)
  56. {
  57. /* This is inline, to optimise the common case, where it's ready instantly */
  58. int ret = 0;
  59. /* 4 read form NOP register should be issued in prior to the read from CDSNControl
  60. see Software Requirement 11.4 item 2. */
  61. DoC_Delay(docptr, 4);
  62. if (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B))
  63. /* Call the out-of-line routine to wait */
  64. ret = _DoC_WaitReady(docptr);
  65. /* issue 2 read from NOP register after reading from CDSNControl register
  66. see Software Requirement 11.4 item 2. */
  67. DoC_Delay(docptr, 2);
  68. return ret;
  69. }
  70. /* DoC_Command: Send a flash command to the flash chip through the CDSN IO register
  71. with the internal pipeline. Each of 4 delay cycles (read from the NOP register) is
  72. required after writing to CDSN Control register, see Software Requirement 11.4 item 3. */
  73. static void DoC_Command(void __iomem * docptr, unsigned char command,
  74. unsigned char xtraflags)
  75. {
  76. /* Assert the CLE (Command Latch Enable) line to the flash chip */
  77. WriteDOC(xtraflags | CDSN_CTRL_CLE | CDSN_CTRL_CE, docptr, CDSNControl);
  78. DoC_Delay(docptr, 4);
  79. /* Send the command */
  80. WriteDOC(command, docptr, Mil_CDSN_IO);
  81. WriteDOC(0x00, docptr, WritePipeTerm);
  82. /* Lower the CLE line */
  83. WriteDOC(xtraflags | CDSN_CTRL_CE, docptr, CDSNControl);
  84. DoC_Delay(docptr, 4);
  85. }
  86. /* DoC_Address: Set the current address for the flash chip through the CDSN IO register
  87. with the internal pipeline. Each of 4 delay cycles (read from the NOP register) is
  88. required after writing to CDSN Control register, see Software Requirement 11.4 item 3. */
  89. static inline void DoC_Address(void __iomem * docptr, int numbytes, unsigned long ofs,
  90. unsigned char xtraflags1, unsigned char xtraflags2)
  91. {
  92. /* Assert the ALE (Address Latch Enable) line to the flash chip */
  93. WriteDOC(xtraflags1 | CDSN_CTRL_ALE | CDSN_CTRL_CE, docptr, CDSNControl);
  94. DoC_Delay(docptr, 4);
  95. /* Send the address */
  96. switch (numbytes)
  97. {
  98. case 1:
  99. /* Send single byte, bits 0-7. */
  100. WriteDOC(ofs & 0xff, docptr, Mil_CDSN_IO);
  101. WriteDOC(0x00, docptr, WritePipeTerm);
  102. break;
  103. case 2:
  104. /* Send bits 9-16 followed by 17-23 */
  105. WriteDOC((ofs >> 9) & 0xff, docptr, Mil_CDSN_IO);
  106. WriteDOC((ofs >> 17) & 0xff, docptr, Mil_CDSN_IO);
  107. WriteDOC(0x00, docptr, WritePipeTerm);
  108. break;
  109. case 3:
  110. /* Send 0-7, 9-16, then 17-23 */
  111. WriteDOC(ofs & 0xff, docptr, Mil_CDSN_IO);
  112. WriteDOC((ofs >> 9) & 0xff, docptr, Mil_CDSN_IO);
  113. WriteDOC((ofs >> 17) & 0xff, docptr, Mil_CDSN_IO);
  114. WriteDOC(0x00, docptr, WritePipeTerm);
  115. break;
  116. default:
  117. return;
  118. }
  119. /* Lower the ALE line */
  120. WriteDOC(xtraflags1 | xtraflags2 | CDSN_CTRL_CE, docptr, CDSNControl);
  121. DoC_Delay(docptr, 4);
  122. }
  123. /* DoC_SelectChip: Select a given flash chip within the current floor */
  124. static int DoC_SelectChip(void __iomem * docptr, int chip)
  125. {
  126. /* Select the individual flash chip requested */
  127. WriteDOC(chip, docptr, CDSNDeviceSelect);
  128. DoC_Delay(docptr, 4);
  129. /* Wait for it to be ready */
  130. return DoC_WaitReady(docptr);
  131. }
  132. /* DoC_SelectFloor: Select a given floor (bank of flash chips) */
  133. static int DoC_SelectFloor(void __iomem * docptr, int floor)
  134. {
  135. /* Select the floor (bank) of chips required */
  136. WriteDOC(floor, docptr, FloorSelect);
  137. /* Wait for the chip to be ready */
  138. return DoC_WaitReady(docptr);
  139. }
  140. /* DoC_IdentChip: Identify a given NAND chip given {floor,chip} */
  141. static int DoC_IdentChip(struct DiskOnChip *doc, int floor, int chip)
  142. {
  143. int mfr, id, i, j;
  144. volatile char dummy;
  145. /* Page in the required floor/chip
  146. FIXME: is this supported by Millennium ?? */
  147. DoC_SelectFloor(doc->virtadr, floor);
  148. DoC_SelectChip(doc->virtadr, chip);
  149. /* Reset the chip, see Software Requirement 11.4 item 1. */
  150. DoC_Command(doc->virtadr, NAND_CMD_RESET, CDSN_CTRL_WP);
  151. DoC_WaitReady(doc->virtadr);
  152. /* Read the NAND chip ID: 1. Send ReadID command */
  153. DoC_Command(doc->virtadr, NAND_CMD_READID, CDSN_CTRL_WP);
  154. /* Read the NAND chip ID: 2. Send address byte zero */
  155. DoC_Address(doc->virtadr, 1, 0x00, CDSN_CTRL_WP, 0x00);
  156. /* Read the manufacturer and device id codes of the flash device through
  157. CDSN IO register see Software Requirement 11.4 item 5.*/
  158. dummy = ReadDOC(doc->virtadr, ReadPipeInit);
  159. DoC_Delay(doc->virtadr, 2);
  160. mfr = ReadDOC(doc->virtadr, Mil_CDSN_IO);
  161. DoC_Delay(doc->virtadr, 2);
  162. id = ReadDOC(doc->virtadr, Mil_CDSN_IO);
  163. dummy = ReadDOC(doc->virtadr, LastDataRead);
  164. /* No response - return failure */
  165. if (mfr == 0xff || mfr == 0)
  166. return 0;
  167. /* FIXME: to deal with multi-flash on multi-Millennium case more carefully */
  168. for (i = 0; nand_flash_ids[i].name != NULL; i++) {
  169. if ( id == nand_flash_ids[i].id) {
  170. /* Try to identify manufacturer */
  171. for (j = 0; nand_manuf_ids[j].id != 0x0; j++) {
  172. if (nand_manuf_ids[j].id == mfr)
  173. break;
  174. }
  175. printk(KERN_INFO "Flash chip found: Manufacturer ID: %2.2X, "
  176. "Chip ID: %2.2X (%s:%s)\n",
  177. mfr, id, nand_manuf_ids[j].name, nand_flash_ids[i].name);
  178. doc->mfr = mfr;
  179. doc->id = id;
  180. doc->chipshift = ffs((nand_flash_ids[i].chipsize << 20)) - 1;
  181. break;
  182. }
  183. }
  184. if (nand_flash_ids[i].name == NULL)
  185. return 0;
  186. else
  187. return 1;
  188. }
  189. /* DoC_ScanChips: Find all NAND chips present in a DiskOnChip, and identify them */
  190. static void DoC_ScanChips(struct DiskOnChip *this)
  191. {
  192. int floor, chip;
  193. int numchips[MAX_FLOORS_MIL];
  194. int ret;
  195. this->numchips = 0;
  196. this->mfr = 0;
  197. this->id = 0;
  198. /* For each floor, find the number of valid chips it contains */
  199. for (floor = 0,ret = 1; floor < MAX_FLOORS_MIL; floor++) {
  200. numchips[floor] = 0;
  201. for (chip = 0; chip < MAX_CHIPS_MIL && ret != 0; chip++) {
  202. ret = DoC_IdentChip(this, floor, chip);
  203. if (ret) {
  204. numchips[floor]++;
  205. this->numchips++;
  206. }
  207. }
  208. }
  209. /* If there are none at all that we recognise, bail */
  210. if (!this->numchips) {
  211. printk("No flash chips recognised.\n");
  212. return;
  213. }
  214. /* Allocate an array to hold the information for each chip */
  215. this->chips = kmalloc(sizeof(struct Nand) * this->numchips, GFP_KERNEL);
  216. if (!this->chips){
  217. printk("No memory for allocating chip info structures\n");
  218. return;
  219. }
  220. /* Fill out the chip array with {floor, chipno} for each
  221. * detected chip in the device. */
  222. for (floor = 0, ret = 0; floor < MAX_FLOORS_MIL; floor++) {
  223. for (chip = 0 ; chip < numchips[floor] ; chip++) {
  224. this->chips[ret].floor = floor;
  225. this->chips[ret].chip = chip;
  226. this->chips[ret].curadr = 0;
  227. this->chips[ret].curmode = 0x50;
  228. ret++;
  229. }
  230. }
  231. /* Calculate and print the total size of the device */
  232. this->totlen = this->numchips * (1 << this->chipshift);
  233. printk(KERN_INFO "%d flash chips found. Total DiskOnChip size: %ld MiB\n",
  234. this->numchips ,this->totlen >> 20);
  235. }
  236. static int DoCMil_is_alias(struct DiskOnChip *doc1, struct DiskOnChip *doc2)
  237. {
  238. int tmp1, tmp2, retval;
  239. if (doc1->physadr == doc2->physadr)
  240. return 1;
  241. /* Use the alias resolution register which was set aside for this
  242. * purpose. If it's value is the same on both chips, they might
  243. * be the same chip, and we write to one and check for a change in
  244. * the other. It's unclear if this register is usuable in the
  245. * DoC 2000 (it's in the Millenium docs), but it seems to work. */
  246. tmp1 = ReadDOC(doc1->virtadr, AliasResolution);
  247. tmp2 = ReadDOC(doc2->virtadr, AliasResolution);
  248. if (tmp1 != tmp2)
  249. return 0;
  250. WriteDOC((tmp1+1) % 0xff, doc1->virtadr, AliasResolution);
  251. tmp2 = ReadDOC(doc2->virtadr, AliasResolution);
  252. if (tmp2 == (tmp1+1) % 0xff)
  253. retval = 1;
  254. else
  255. retval = 0;
  256. /* Restore register contents. May not be necessary, but do it just to
  257. * be safe. */
  258. WriteDOC(tmp1, doc1->virtadr, AliasResolution);
  259. return retval;
  260. }
  261. /* This routine is found from the docprobe code by symbol_get(),
  262. * which will bump the use count of this module. */
  263. void DoCMil_init(struct mtd_info *mtd)
  264. {
  265. struct DiskOnChip *this = mtd->priv;
  266. struct DiskOnChip *old = NULL;
  267. /* We must avoid being called twice for the same device. */
  268. if (docmillist)
  269. old = docmillist->priv;
  270. while (old) {
  271. if (DoCMil_is_alias(this, old)) {
  272. printk(KERN_NOTICE "Ignoring DiskOnChip Millennium at "
  273. "0x%lX - already configured\n", this->physadr);
  274. iounmap(this->virtadr);
  275. kfree(mtd);
  276. return;
  277. }
  278. if (old->nextdoc)
  279. old = old->nextdoc->priv;
  280. else
  281. old = NULL;
  282. }
  283. mtd->name = "DiskOnChip Millennium";
  284. printk(KERN_NOTICE "DiskOnChip Millennium found at address 0x%lX\n",
  285. this->physadr);
  286. mtd->type = MTD_NANDFLASH;
  287. mtd->flags = MTD_CAP_NANDFLASH;
  288. /* FIXME: erase size is not always 8KiB */
  289. mtd->erasesize = 0x2000;
  290. mtd->writebufsize = mtd->writesize = 512;
  291. mtd->oobsize = 16;
  292. mtd->owner = THIS_MODULE;
  293. mtd->_erase = doc_erase;
  294. mtd->_read = doc_read;
  295. mtd->_write = doc_write;
  296. mtd->_read_oob = doc_read_oob;
  297. mtd->_write_oob = doc_write_oob;
  298. this->curfloor = -1;
  299. this->curchip = -1;
  300. /* Ident all the chips present. */
  301. DoC_ScanChips(this);
  302. if (!this->totlen) {
  303. kfree(mtd);
  304. iounmap(this->virtadr);
  305. } else {
  306. this->nextdoc = docmillist;
  307. docmillist = mtd;
  308. mtd->size = this->totlen;
  309. mtd_device_register(mtd, NULL, 0);
  310. return;
  311. }
  312. }
  313. EXPORT_SYMBOL_GPL(DoCMil_init);
  314. static int doc_read (struct mtd_info *mtd, loff_t from, size_t len,
  315. size_t *retlen, u_char *buf)
  316. {
  317. int i, ret;
  318. volatile char dummy;
  319. unsigned char syndrome[6], eccbuf[6];
  320. struct DiskOnChip *this = mtd->priv;
  321. void __iomem *docptr = this->virtadr;
  322. struct Nand *mychip = &this->chips[from >> (this->chipshift)];
  323. /* Don't allow a single read to cross a 512-byte block boundary */
  324. if (from + len > ((from | 0x1ff) + 1))
  325. len = ((from | 0x1ff) + 1) - from;
  326. /* Find the chip which is to be used and select it */
  327. if (this->curfloor != mychip->floor) {
  328. DoC_SelectFloor(docptr, mychip->floor);
  329. DoC_SelectChip(docptr, mychip->chip);
  330. } else if (this->curchip != mychip->chip) {
  331. DoC_SelectChip(docptr, mychip->chip);
  332. }
  333. this->curfloor = mychip->floor;
  334. this->curchip = mychip->chip;
  335. /* issue the Read0 or Read1 command depend on which half of the page
  336. we are accessing. Polling the Flash Ready bit after issue 3 bytes
  337. address in Sequence Read Mode, see Software Requirement 11.4 item 1.*/
  338. DoC_Command(docptr, (from >> 8) & 1, CDSN_CTRL_WP);
  339. DoC_Address(docptr, 3, from, CDSN_CTRL_WP, 0x00);
  340. DoC_WaitReady(docptr);
  341. /* init the ECC engine, see Reed-Solomon EDC/ECC 11.1 .*/
  342. WriteDOC (DOC_ECC_RESET, docptr, ECCConf);
  343. WriteDOC (DOC_ECC_EN, docptr, ECCConf);
  344. /* Read the data via the internal pipeline through CDSN IO register,
  345. see Pipelined Read Operations 11.3 */
  346. dummy = ReadDOC(docptr, ReadPipeInit);
  347. #ifndef USE_MEMCPY
  348. for (i = 0; i < len-1; i++) {
  349. /* N.B. you have to increase the source address in this way or the
  350. ECC logic will not work properly */
  351. buf[i] = ReadDOC(docptr, Mil_CDSN_IO + (i & 0xff));
  352. }
  353. #else
  354. memcpy_fromio(buf, docptr + DoC_Mil_CDSN_IO, len - 1);
  355. #endif
  356. buf[len - 1] = ReadDOC(docptr, LastDataRead);
  357. /* Let the caller know we completed it */
  358. *retlen = len;
  359. ret = 0;
  360. /* Read the ECC data from Spare Data Area,
  361. see Reed-Solomon EDC/ECC 11.1 */
  362. dummy = ReadDOC(docptr, ReadPipeInit);
  363. #ifndef USE_MEMCPY
  364. for (i = 0; i < 5; i++) {
  365. /* N.B. you have to increase the source address in this way or the
  366. ECC logic will not work properly */
  367. eccbuf[i] = ReadDOC(docptr, Mil_CDSN_IO + i);
  368. }
  369. #else
  370. memcpy_fromio(eccbuf, docptr + DoC_Mil_CDSN_IO, 5);
  371. #endif
  372. eccbuf[5] = ReadDOC(docptr, LastDataRead);
  373. /* Flush the pipeline */
  374. dummy = ReadDOC(docptr, ECCConf);
  375. dummy = ReadDOC(docptr, ECCConf);
  376. /* Check the ECC Status */
  377. if (ReadDOC(docptr, ECCConf) & 0x80) {
  378. int nb_errors;
  379. /* There was an ECC error */
  380. #ifdef ECC_DEBUG
  381. printk("DiskOnChip ECC Error: Read at %lx\n", (long)from);
  382. #endif
  383. /* Read the ECC syndrome through the DiskOnChip ECC logic.
  384. These syndrome will be all ZERO when there is no error */
  385. for (i = 0; i < 6; i++) {
  386. syndrome[i] = ReadDOC(docptr, ECCSyndrome0 + i);
  387. }
  388. nb_errors = doc_decode_ecc(buf, syndrome);
  389. #ifdef ECC_DEBUG
  390. printk("ECC Errors corrected: %x\n", nb_errors);
  391. #endif
  392. if (nb_errors < 0) {
  393. /* We return error, but have actually done the read. Not that
  394. this can be told to user-space, via sys_read(), but at least
  395. MTD-aware stuff can know about it by checking *retlen */
  396. ret = -EIO;
  397. }
  398. }
  399. #ifdef PSYCHO_DEBUG
  400. printk("ECC DATA at %lx: %2.2X %2.2X %2.2X %2.2X %2.2X %2.2X\n",
  401. (long)from, eccbuf[0], eccbuf[1], eccbuf[2], eccbuf[3],
  402. eccbuf[4], eccbuf[5]);
  403. #endif
  404. /* disable the ECC engine */
  405. WriteDOC(DOC_ECC_DIS, docptr , ECCConf);
  406. return ret;
  407. }
  408. static int doc_write (struct mtd_info *mtd, loff_t to, size_t len,
  409. size_t *retlen, const u_char *buf)
  410. {
  411. int i,ret = 0;
  412. char eccbuf[6];
  413. volatile char dummy;
  414. struct DiskOnChip *this = mtd->priv;
  415. void __iomem *docptr = this->virtadr;
  416. struct Nand *mychip = &this->chips[to >> (this->chipshift)];
  417. #if 0
  418. /* Don't allow a single write to cross a 512-byte block boundary */
  419. if (to + len > ( (to | 0x1ff) + 1))
  420. len = ((to | 0x1ff) + 1) - to;
  421. #else
  422. /* Don't allow writes which aren't exactly one block */
  423. if (to & 0x1ff || len != 0x200)
  424. return -EINVAL;
  425. #endif
  426. /* Find the chip which is to be used and select it */
  427. if (this->curfloor != mychip->floor) {
  428. DoC_SelectFloor(docptr, mychip->floor);
  429. DoC_SelectChip(docptr, mychip->chip);
  430. } else if (this->curchip != mychip->chip) {
  431. DoC_SelectChip(docptr, mychip->chip);
  432. }
  433. this->curfloor = mychip->floor;
  434. this->curchip = mychip->chip;
  435. /* Reset the chip, see Software Requirement 11.4 item 1. */
  436. DoC_Command(docptr, NAND_CMD_RESET, 0x00);
  437. DoC_WaitReady(docptr);
  438. /* Set device to main plane of flash */
  439. DoC_Command(docptr, NAND_CMD_READ0, 0x00);
  440. /* issue the Serial Data In command to initial the Page Program process */
  441. DoC_Command(docptr, NAND_CMD_SEQIN, 0x00);
  442. DoC_Address(docptr, 3, to, 0x00, 0x00);
  443. DoC_WaitReady(docptr);
  444. /* init the ECC engine, see Reed-Solomon EDC/ECC 11.1 .*/
  445. WriteDOC (DOC_ECC_RESET, docptr, ECCConf);
  446. WriteDOC (DOC_ECC_EN | DOC_ECC_RW, docptr, ECCConf);
  447. /* Write the data via the internal pipeline through CDSN IO register,
  448. see Pipelined Write Operations 11.2 */
  449. #ifndef USE_MEMCPY
  450. for (i = 0; i < len; i++) {
  451. /* N.B. you have to increase the source address in this way or the
  452. ECC logic will not work properly */
  453. WriteDOC(buf[i], docptr, Mil_CDSN_IO + i);
  454. }
  455. #else
  456. memcpy_toio(docptr + DoC_Mil_CDSN_IO, buf, len);
  457. #endif
  458. WriteDOC(0x00, docptr, WritePipeTerm);
  459. /* Write ECC data to flash, the ECC info is generated by the DiskOnChip ECC logic
  460. see Reed-Solomon EDC/ECC 11.1 */
  461. WriteDOC(0, docptr, NOP);
  462. WriteDOC(0, docptr, NOP);
  463. WriteDOC(0, docptr, NOP);
  464. /* Read the ECC data through the DiskOnChip ECC logic */
  465. for (i = 0; i < 6; i++) {
  466. eccbuf[i] = ReadDOC(docptr, ECCSyndrome0 + i);
  467. }
  468. /* ignore the ECC engine */
  469. WriteDOC(DOC_ECC_DIS, docptr , ECCConf);
  470. #ifndef USE_MEMCPY
  471. /* Write the ECC data to flash */
  472. for (i = 0; i < 6; i++) {
  473. /* N.B. you have to increase the source address in this way or the
  474. ECC logic will not work properly */
  475. WriteDOC(eccbuf[i], docptr, Mil_CDSN_IO + i);
  476. }
  477. #else
  478. memcpy_toio(docptr + DoC_Mil_CDSN_IO, eccbuf, 6);
  479. #endif
  480. /* write the block status BLOCK_USED (0x5555) at the end of ECC data
  481. FIXME: this is only a hack for programming the IPL area for LinuxBIOS
  482. and should be replace with proper codes in user space utilities */
  483. WriteDOC(0x55, docptr, Mil_CDSN_IO);
  484. WriteDOC(0x55, docptr, Mil_CDSN_IO + 1);
  485. WriteDOC(0x00, docptr, WritePipeTerm);
  486. #ifdef PSYCHO_DEBUG
  487. printk("OOB data at %lx is %2.2X %2.2X %2.2X %2.2X %2.2X %2.2X\n",
  488. (long) to, eccbuf[0], eccbuf[1], eccbuf[2], eccbuf[3],
  489. eccbuf[4], eccbuf[5]);
  490. #endif
  491. /* Commit the Page Program command and wait for ready
  492. see Software Requirement 11.4 item 1.*/
  493. DoC_Command(docptr, NAND_CMD_PAGEPROG, 0x00);
  494. DoC_WaitReady(docptr);
  495. /* Read the status of the flash device through CDSN IO register
  496. see Software Requirement 11.4 item 5.*/
  497. DoC_Command(docptr, NAND_CMD_STATUS, CDSN_CTRL_WP);
  498. dummy = ReadDOC(docptr, ReadPipeInit);
  499. DoC_Delay(docptr, 2);
  500. if (ReadDOC(docptr, Mil_CDSN_IO) & 1) {
  501. printk("Error programming flash\n");
  502. /* Error in programming
  503. FIXME: implement Bad Block Replacement (in nftl.c ??) */
  504. ret = -EIO;
  505. }
  506. dummy = ReadDOC(docptr, LastDataRead);
  507. /* Let the caller know we completed it */
  508. *retlen = len;
  509. return ret;
  510. }
  511. static int doc_read_oob(struct mtd_info *mtd, loff_t ofs,
  512. struct mtd_oob_ops *ops)
  513. {
  514. #ifndef USE_MEMCPY
  515. int i;
  516. #endif
  517. volatile char dummy;
  518. struct DiskOnChip *this = mtd->priv;
  519. void __iomem *docptr = this->virtadr;
  520. struct Nand *mychip = &this->chips[ofs >> this->chipshift];
  521. uint8_t *buf = ops->oobbuf;
  522. size_t len = ops->len;
  523. BUG_ON(ops->mode != MTD_OPS_PLACE_OOB);
  524. ofs += ops->ooboffs;
  525. /* Find the chip which is to be used and select it */
  526. if (this->curfloor != mychip->floor) {
  527. DoC_SelectFloor(docptr, mychip->floor);
  528. DoC_SelectChip(docptr, mychip->chip);
  529. } else if (this->curchip != mychip->chip) {
  530. DoC_SelectChip(docptr, mychip->chip);
  531. }
  532. this->curfloor = mychip->floor;
  533. this->curchip = mychip->chip;
  534. /* disable the ECC engine */
  535. WriteDOC (DOC_ECC_RESET, docptr, ECCConf);
  536. WriteDOC (DOC_ECC_DIS, docptr, ECCConf);
  537. /* issue the Read2 command to set the pointer to the Spare Data Area.
  538. Polling the Flash Ready bit after issue 3 bytes address in
  539. Sequence Read Mode, see Software Requirement 11.4 item 1.*/
  540. DoC_Command(docptr, NAND_CMD_READOOB, CDSN_CTRL_WP);
  541. DoC_Address(docptr, 3, ofs, CDSN_CTRL_WP, 0x00);
  542. DoC_WaitReady(docptr);
  543. /* Read the data out via the internal pipeline through CDSN IO register,
  544. see Pipelined Read Operations 11.3 */
  545. dummy = ReadDOC(docptr, ReadPipeInit);
  546. #ifndef USE_MEMCPY
  547. for (i = 0; i < len-1; i++) {
  548. /* N.B. you have to increase the source address in this way or the
  549. ECC logic will not work properly */
  550. buf[i] = ReadDOC(docptr, Mil_CDSN_IO + i);
  551. }
  552. #else
  553. memcpy_fromio(buf, docptr + DoC_Mil_CDSN_IO, len - 1);
  554. #endif
  555. buf[len - 1] = ReadDOC(docptr, LastDataRead);
  556. ops->retlen = len;
  557. return 0;
  558. }
  559. static int doc_write_oob(struct mtd_info *mtd, loff_t ofs,
  560. struct mtd_oob_ops *ops)
  561. {
  562. #ifndef USE_MEMCPY
  563. int i;
  564. #endif
  565. volatile char dummy;
  566. int ret = 0;
  567. struct DiskOnChip *this = mtd->priv;
  568. void __iomem *docptr = this->virtadr;
  569. struct Nand *mychip = &this->chips[ofs >> this->chipshift];
  570. uint8_t *buf = ops->oobbuf;
  571. size_t len = ops->len;
  572. BUG_ON(ops->mode != MTD_OPS_PLACE_OOB);
  573. ofs += ops->ooboffs;
  574. /* Find the chip which is to be used and select it */
  575. if (this->curfloor != mychip->floor) {
  576. DoC_SelectFloor(docptr, mychip->floor);
  577. DoC_SelectChip(docptr, mychip->chip);
  578. } else if (this->curchip != mychip->chip) {
  579. DoC_SelectChip(docptr, mychip->chip);
  580. }
  581. this->curfloor = mychip->floor;
  582. this->curchip = mychip->chip;
  583. /* disable the ECC engine */
  584. WriteDOC (DOC_ECC_RESET, docptr, ECCConf);
  585. WriteDOC (DOC_ECC_DIS, docptr, ECCConf);
  586. /* Reset the chip, see Software Requirement 11.4 item 1. */
  587. DoC_Command(docptr, NAND_CMD_RESET, CDSN_CTRL_WP);
  588. DoC_WaitReady(docptr);
  589. /* issue the Read2 command to set the pointer to the Spare Data Area. */
  590. DoC_Command(docptr, NAND_CMD_READOOB, CDSN_CTRL_WP);
  591. /* issue the Serial Data In command to initial the Page Program process */
  592. DoC_Command(docptr, NAND_CMD_SEQIN, 0x00);
  593. DoC_Address(docptr, 3, ofs, 0x00, 0x00);
  594. /* Write the data via the internal pipeline through CDSN IO register,
  595. see Pipelined Write Operations 11.2 */
  596. #ifndef USE_MEMCPY
  597. for (i = 0; i < len; i++) {
  598. /* N.B. you have to increase the source address in this way or the
  599. ECC logic will not work properly */
  600. WriteDOC(buf[i], docptr, Mil_CDSN_IO + i);
  601. }
  602. #else
  603. memcpy_toio(docptr + DoC_Mil_CDSN_IO, buf, len);
  604. #endif
  605. WriteDOC(0x00, docptr, WritePipeTerm);
  606. /* Commit the Page Program command and wait for ready
  607. see Software Requirement 11.4 item 1.*/
  608. DoC_Command(docptr, NAND_CMD_PAGEPROG, 0x00);
  609. DoC_WaitReady(docptr);
  610. /* Read the status of the flash device through CDSN IO register
  611. see Software Requirement 11.4 item 5.*/
  612. DoC_Command(docptr, NAND_CMD_STATUS, 0x00);
  613. dummy = ReadDOC(docptr, ReadPipeInit);
  614. DoC_Delay(docptr, 2);
  615. if (ReadDOC(docptr, Mil_CDSN_IO) & 1) {
  616. printk("Error programming oob data\n");
  617. /* FIXME: implement Bad Block Replacement (in nftl.c ??) */
  618. ops->retlen = 0;
  619. ret = -EIO;
  620. }
  621. dummy = ReadDOC(docptr, LastDataRead);
  622. ops->retlen = len;
  623. return ret;
  624. }
  625. int doc_erase (struct mtd_info *mtd, struct erase_info *instr)
  626. {
  627. volatile char dummy;
  628. struct DiskOnChip *this = mtd->priv;
  629. __u32 ofs = instr->addr;
  630. __u32 len = instr->len;
  631. void __iomem *docptr = this->virtadr;
  632. struct Nand *mychip = &this->chips[ofs >> this->chipshift];
  633. if (len != mtd->erasesize)
  634. printk(KERN_WARNING "Erase not right size (%x != %x)n",
  635. len, mtd->erasesize);
  636. /* Find the chip which is to be used and select it */
  637. if (this->curfloor != mychip->floor) {
  638. DoC_SelectFloor(docptr, mychip->floor);
  639. DoC_SelectChip(docptr, mychip->chip);
  640. } else if (this->curchip != mychip->chip) {
  641. DoC_SelectChip(docptr, mychip->chip);
  642. }
  643. this->curfloor = mychip->floor;
  644. this->curchip = mychip->chip;
  645. instr->state = MTD_ERASE_PENDING;
  646. /* issue the Erase Setup command */
  647. DoC_Command(docptr, NAND_CMD_ERASE1, 0x00);
  648. DoC_Address(docptr, 2, ofs, 0x00, 0x00);
  649. /* Commit the Erase Start command and wait for ready
  650. see Software Requirement 11.4 item 1.*/
  651. DoC_Command(docptr, NAND_CMD_ERASE2, 0x00);
  652. DoC_WaitReady(docptr);
  653. instr->state = MTD_ERASING;
  654. /* Read the status of the flash device through CDSN IO register
  655. see Software Requirement 11.4 item 5.
  656. FIXME: it seems that we are not wait long enough, some blocks are not
  657. erased fully */
  658. DoC_Command(docptr, NAND_CMD_STATUS, CDSN_CTRL_WP);
  659. dummy = ReadDOC(docptr, ReadPipeInit);
  660. DoC_Delay(docptr, 2);
  661. if (ReadDOC(docptr, Mil_CDSN_IO) & 1) {
  662. printk("Error Erasing at 0x%x\n", ofs);
  663. /* There was an error
  664. FIXME: implement Bad Block Replacement (in nftl.c ??) */
  665. instr->state = MTD_ERASE_FAILED;
  666. } else
  667. instr->state = MTD_ERASE_DONE;
  668. dummy = ReadDOC(docptr, LastDataRead);
  669. mtd_erase_callback(instr);
  670. return 0;
  671. }
  672. /****************************************************************************
  673. *
  674. * Module stuff
  675. *
  676. ****************************************************************************/
  677. static void __exit cleanup_doc2001(void)
  678. {
  679. struct mtd_info *mtd;
  680. struct DiskOnChip *this;
  681. while ((mtd=docmillist)) {
  682. this = mtd->priv;
  683. docmillist = this->nextdoc;
  684. mtd_device_unregister(mtd);
  685. iounmap(this->virtadr);
  686. kfree(this->chips);
  687. kfree(mtd);
  688. }
  689. }
  690. module_exit(cleanup_doc2001);
  691. MODULE_LICENSE("GPL");
  692. MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org> et al.");
  693. MODULE_DESCRIPTION("Alternative driver for DiskOnChip Millennium");