doc2001.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832
  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->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 read past end of device */
  324. if (from >= this->totlen)
  325. return -EINVAL;
  326. /* Don't allow a single read to cross a 512-byte block boundary */
  327. if (from + len > ((from | 0x1ff) + 1))
  328. len = ((from | 0x1ff) + 1) - from;
  329. /* Find the chip which is to be used and select it */
  330. if (this->curfloor != mychip->floor) {
  331. DoC_SelectFloor(docptr, mychip->floor);
  332. DoC_SelectChip(docptr, mychip->chip);
  333. } else if (this->curchip != mychip->chip) {
  334. DoC_SelectChip(docptr, mychip->chip);
  335. }
  336. this->curfloor = mychip->floor;
  337. this->curchip = mychip->chip;
  338. /* issue the Read0 or Read1 command depend on which half of the page
  339. we are accessing. Polling the Flash Ready bit after issue 3 bytes
  340. address in Sequence Read Mode, see Software Requirement 11.4 item 1.*/
  341. DoC_Command(docptr, (from >> 8) & 1, CDSN_CTRL_WP);
  342. DoC_Address(docptr, 3, from, CDSN_CTRL_WP, 0x00);
  343. DoC_WaitReady(docptr);
  344. /* init the ECC engine, see Reed-Solomon EDC/ECC 11.1 .*/
  345. WriteDOC (DOC_ECC_RESET, docptr, ECCConf);
  346. WriteDOC (DOC_ECC_EN, docptr, ECCConf);
  347. /* Read the data via the internal pipeline through CDSN IO register,
  348. see Pipelined Read Operations 11.3 */
  349. dummy = ReadDOC(docptr, ReadPipeInit);
  350. #ifndef USE_MEMCPY
  351. for (i = 0; i < len-1; i++) {
  352. /* N.B. you have to increase the source address in this way or the
  353. ECC logic will not work properly */
  354. buf[i] = ReadDOC(docptr, Mil_CDSN_IO + (i & 0xff));
  355. }
  356. #else
  357. memcpy_fromio(buf, docptr + DoC_Mil_CDSN_IO, len - 1);
  358. #endif
  359. buf[len - 1] = ReadDOC(docptr, LastDataRead);
  360. /* Let the caller know we completed it */
  361. *retlen = len;
  362. ret = 0;
  363. /* Read the ECC data from Spare Data Area,
  364. see Reed-Solomon EDC/ECC 11.1 */
  365. dummy = ReadDOC(docptr, ReadPipeInit);
  366. #ifndef USE_MEMCPY
  367. for (i = 0; i < 5; i++) {
  368. /* N.B. you have to increase the source address in this way or the
  369. ECC logic will not work properly */
  370. eccbuf[i] = ReadDOC(docptr, Mil_CDSN_IO + i);
  371. }
  372. #else
  373. memcpy_fromio(eccbuf, docptr + DoC_Mil_CDSN_IO, 5);
  374. #endif
  375. eccbuf[5] = ReadDOC(docptr, LastDataRead);
  376. /* Flush the pipeline */
  377. dummy = ReadDOC(docptr, ECCConf);
  378. dummy = ReadDOC(docptr, ECCConf);
  379. /* Check the ECC Status */
  380. if (ReadDOC(docptr, ECCConf) & 0x80) {
  381. int nb_errors;
  382. /* There was an ECC error */
  383. #ifdef ECC_DEBUG
  384. printk("DiskOnChip ECC Error: Read at %lx\n", (long)from);
  385. #endif
  386. /* Read the ECC syndrome through the DiskOnChip ECC logic.
  387. These syndrome will be all ZERO when there is no error */
  388. for (i = 0; i < 6; i++) {
  389. syndrome[i] = ReadDOC(docptr, ECCSyndrome0 + i);
  390. }
  391. nb_errors = doc_decode_ecc(buf, syndrome);
  392. #ifdef ECC_DEBUG
  393. printk("ECC Errors corrected: %x\n", nb_errors);
  394. #endif
  395. if (nb_errors < 0) {
  396. /* We return error, but have actually done the read. Not that
  397. this can be told to user-space, via sys_read(), but at least
  398. MTD-aware stuff can know about it by checking *retlen */
  399. ret = -EIO;
  400. }
  401. }
  402. #ifdef PSYCHO_DEBUG
  403. printk("ECC DATA at %lx: %2.2X %2.2X %2.2X %2.2X %2.2X %2.2X\n",
  404. (long)from, eccbuf[0], eccbuf[1], eccbuf[2], eccbuf[3],
  405. eccbuf[4], eccbuf[5]);
  406. #endif
  407. /* disable the ECC engine */
  408. WriteDOC(DOC_ECC_DIS, docptr , ECCConf);
  409. return ret;
  410. }
  411. static int doc_write (struct mtd_info *mtd, loff_t to, size_t len,
  412. size_t *retlen, const u_char *buf)
  413. {
  414. int i,ret = 0;
  415. char eccbuf[6];
  416. volatile char dummy;
  417. struct DiskOnChip *this = mtd->priv;
  418. void __iomem *docptr = this->virtadr;
  419. struct Nand *mychip = &this->chips[to >> (this->chipshift)];
  420. /* Don't allow write past end of device */
  421. if (to >= this->totlen)
  422. return -EINVAL;
  423. #if 0
  424. /* Don't allow a single write to cross a 512-byte block boundary */
  425. if (to + len > ( (to | 0x1ff) + 1))
  426. len = ((to | 0x1ff) + 1) - to;
  427. #else
  428. /* Don't allow writes which aren't exactly one block */
  429. if (to & 0x1ff || len != 0x200)
  430. return -EINVAL;
  431. #endif
  432. /* Find the chip which is to be used and select it */
  433. if (this->curfloor != mychip->floor) {
  434. DoC_SelectFloor(docptr, mychip->floor);
  435. DoC_SelectChip(docptr, mychip->chip);
  436. } else if (this->curchip != mychip->chip) {
  437. DoC_SelectChip(docptr, mychip->chip);
  438. }
  439. this->curfloor = mychip->floor;
  440. this->curchip = mychip->chip;
  441. /* Reset the chip, see Software Requirement 11.4 item 1. */
  442. DoC_Command(docptr, NAND_CMD_RESET, 0x00);
  443. DoC_WaitReady(docptr);
  444. /* Set device to main plane of flash */
  445. DoC_Command(docptr, NAND_CMD_READ0, 0x00);
  446. /* issue the Serial Data In command to initial the Page Program process */
  447. DoC_Command(docptr, NAND_CMD_SEQIN, 0x00);
  448. DoC_Address(docptr, 3, to, 0x00, 0x00);
  449. DoC_WaitReady(docptr);
  450. /* init the ECC engine, see Reed-Solomon EDC/ECC 11.1 .*/
  451. WriteDOC (DOC_ECC_RESET, docptr, ECCConf);
  452. WriteDOC (DOC_ECC_EN | DOC_ECC_RW, docptr, ECCConf);
  453. /* Write the data via the internal pipeline through CDSN IO register,
  454. see Pipelined Write Operations 11.2 */
  455. #ifndef USE_MEMCPY
  456. for (i = 0; i < len; i++) {
  457. /* N.B. you have to increase the source address in this way or the
  458. ECC logic will not work properly */
  459. WriteDOC(buf[i], docptr, Mil_CDSN_IO + i);
  460. }
  461. #else
  462. memcpy_toio(docptr + DoC_Mil_CDSN_IO, buf, len);
  463. #endif
  464. WriteDOC(0x00, docptr, WritePipeTerm);
  465. /* Write ECC data to flash, the ECC info is generated by the DiskOnChip ECC logic
  466. see Reed-Solomon EDC/ECC 11.1 */
  467. WriteDOC(0, docptr, NOP);
  468. WriteDOC(0, docptr, NOP);
  469. WriteDOC(0, docptr, NOP);
  470. /* Read the ECC data through the DiskOnChip ECC logic */
  471. for (i = 0; i < 6; i++) {
  472. eccbuf[i] = ReadDOC(docptr, ECCSyndrome0 + i);
  473. }
  474. /* ignore the ECC engine */
  475. WriteDOC(DOC_ECC_DIS, docptr , ECCConf);
  476. #ifndef USE_MEMCPY
  477. /* Write the ECC data to flash */
  478. for (i = 0; i < 6; i++) {
  479. /* N.B. you have to increase the source address in this way or the
  480. ECC logic will not work properly */
  481. WriteDOC(eccbuf[i], docptr, Mil_CDSN_IO + i);
  482. }
  483. #else
  484. memcpy_toio(docptr + DoC_Mil_CDSN_IO, eccbuf, 6);
  485. #endif
  486. /* write the block status BLOCK_USED (0x5555) at the end of ECC data
  487. FIXME: this is only a hack for programming the IPL area for LinuxBIOS
  488. and should be replace with proper codes in user space utilities */
  489. WriteDOC(0x55, docptr, Mil_CDSN_IO);
  490. WriteDOC(0x55, docptr, Mil_CDSN_IO + 1);
  491. WriteDOC(0x00, docptr, WritePipeTerm);
  492. #ifdef PSYCHO_DEBUG
  493. printk("OOB data at %lx is %2.2X %2.2X %2.2X %2.2X %2.2X %2.2X\n",
  494. (long) to, eccbuf[0], eccbuf[1], eccbuf[2], eccbuf[3],
  495. eccbuf[4], eccbuf[5]);
  496. #endif
  497. /* Commit the Page Program command and wait for ready
  498. see Software Requirement 11.4 item 1.*/
  499. DoC_Command(docptr, NAND_CMD_PAGEPROG, 0x00);
  500. DoC_WaitReady(docptr);
  501. /* Read the status of the flash device through CDSN IO register
  502. see Software Requirement 11.4 item 5.*/
  503. DoC_Command(docptr, NAND_CMD_STATUS, CDSN_CTRL_WP);
  504. dummy = ReadDOC(docptr, ReadPipeInit);
  505. DoC_Delay(docptr, 2);
  506. if (ReadDOC(docptr, Mil_CDSN_IO) & 1) {
  507. printk("Error programming flash\n");
  508. /* Error in programming
  509. FIXME: implement Bad Block Replacement (in nftl.c ??) */
  510. *retlen = 0;
  511. ret = -EIO;
  512. }
  513. dummy = ReadDOC(docptr, LastDataRead);
  514. /* Let the caller know we completed it */
  515. *retlen = len;
  516. return ret;
  517. }
  518. static int doc_read_oob(struct mtd_info *mtd, loff_t ofs,
  519. struct mtd_oob_ops *ops)
  520. {
  521. #ifndef USE_MEMCPY
  522. int i;
  523. #endif
  524. volatile char dummy;
  525. struct DiskOnChip *this = mtd->priv;
  526. void __iomem *docptr = this->virtadr;
  527. struct Nand *mychip = &this->chips[ofs >> this->chipshift];
  528. uint8_t *buf = ops->oobbuf;
  529. size_t len = ops->len;
  530. BUG_ON(ops->mode != MTD_OPS_PLACE_OOB);
  531. ofs += ops->ooboffs;
  532. /* Find the chip which is to be used and select it */
  533. if (this->curfloor != mychip->floor) {
  534. DoC_SelectFloor(docptr, mychip->floor);
  535. DoC_SelectChip(docptr, mychip->chip);
  536. } else if (this->curchip != mychip->chip) {
  537. DoC_SelectChip(docptr, mychip->chip);
  538. }
  539. this->curfloor = mychip->floor;
  540. this->curchip = mychip->chip;
  541. /* disable the ECC engine */
  542. WriteDOC (DOC_ECC_RESET, docptr, ECCConf);
  543. WriteDOC (DOC_ECC_DIS, docptr, ECCConf);
  544. /* issue the Read2 command to set the pointer to the Spare Data Area.
  545. Polling the Flash Ready bit after issue 3 bytes address in
  546. Sequence Read Mode, see Software Requirement 11.4 item 1.*/
  547. DoC_Command(docptr, NAND_CMD_READOOB, CDSN_CTRL_WP);
  548. DoC_Address(docptr, 3, ofs, CDSN_CTRL_WP, 0x00);
  549. DoC_WaitReady(docptr);
  550. /* Read the data out via the internal pipeline through CDSN IO register,
  551. see Pipelined Read Operations 11.3 */
  552. dummy = ReadDOC(docptr, ReadPipeInit);
  553. #ifndef USE_MEMCPY
  554. for (i = 0; i < len-1; i++) {
  555. /* N.B. you have to increase the source address in this way or the
  556. ECC logic will not work properly */
  557. buf[i] = ReadDOC(docptr, Mil_CDSN_IO + i);
  558. }
  559. #else
  560. memcpy_fromio(buf, docptr + DoC_Mil_CDSN_IO, len - 1);
  561. #endif
  562. buf[len - 1] = ReadDOC(docptr, LastDataRead);
  563. ops->retlen = len;
  564. return 0;
  565. }
  566. static int doc_write_oob(struct mtd_info *mtd, loff_t ofs,
  567. struct mtd_oob_ops *ops)
  568. {
  569. #ifndef USE_MEMCPY
  570. int i;
  571. #endif
  572. volatile char dummy;
  573. int ret = 0;
  574. struct DiskOnChip *this = mtd->priv;
  575. void __iomem *docptr = this->virtadr;
  576. struct Nand *mychip = &this->chips[ofs >> this->chipshift];
  577. uint8_t *buf = ops->oobbuf;
  578. size_t len = ops->len;
  579. BUG_ON(ops->mode != MTD_OPS_PLACE_OOB);
  580. ofs += ops->ooboffs;
  581. /* Find the chip which is to be used and select it */
  582. if (this->curfloor != mychip->floor) {
  583. DoC_SelectFloor(docptr, mychip->floor);
  584. DoC_SelectChip(docptr, mychip->chip);
  585. } else if (this->curchip != mychip->chip) {
  586. DoC_SelectChip(docptr, mychip->chip);
  587. }
  588. this->curfloor = mychip->floor;
  589. this->curchip = mychip->chip;
  590. /* disable the ECC engine */
  591. WriteDOC (DOC_ECC_RESET, docptr, ECCConf);
  592. WriteDOC (DOC_ECC_DIS, docptr, ECCConf);
  593. /* Reset the chip, see Software Requirement 11.4 item 1. */
  594. DoC_Command(docptr, NAND_CMD_RESET, CDSN_CTRL_WP);
  595. DoC_WaitReady(docptr);
  596. /* issue the Read2 command to set the pointer to the Spare Data Area. */
  597. DoC_Command(docptr, NAND_CMD_READOOB, CDSN_CTRL_WP);
  598. /* issue the Serial Data In command to initial the Page Program process */
  599. DoC_Command(docptr, NAND_CMD_SEQIN, 0x00);
  600. DoC_Address(docptr, 3, ofs, 0x00, 0x00);
  601. /* Write the data via the internal pipeline through CDSN IO register,
  602. see Pipelined Write Operations 11.2 */
  603. #ifndef USE_MEMCPY
  604. for (i = 0; i < len; i++) {
  605. /* N.B. you have to increase the source address in this way or the
  606. ECC logic will not work properly */
  607. WriteDOC(buf[i], docptr, Mil_CDSN_IO + i);
  608. }
  609. #else
  610. memcpy_toio(docptr + DoC_Mil_CDSN_IO, buf, len);
  611. #endif
  612. WriteDOC(0x00, docptr, WritePipeTerm);
  613. /* Commit the Page Program command and wait for ready
  614. see Software Requirement 11.4 item 1.*/
  615. DoC_Command(docptr, NAND_CMD_PAGEPROG, 0x00);
  616. DoC_WaitReady(docptr);
  617. /* Read the status of the flash device through CDSN IO register
  618. see Software Requirement 11.4 item 5.*/
  619. DoC_Command(docptr, NAND_CMD_STATUS, 0x00);
  620. dummy = ReadDOC(docptr, ReadPipeInit);
  621. DoC_Delay(docptr, 2);
  622. if (ReadDOC(docptr, Mil_CDSN_IO) & 1) {
  623. printk("Error programming oob data\n");
  624. /* FIXME: implement Bad Block Replacement (in nftl.c ??) */
  625. ops->retlen = 0;
  626. ret = -EIO;
  627. }
  628. dummy = ReadDOC(docptr, LastDataRead);
  629. ops->retlen = len;
  630. return ret;
  631. }
  632. int doc_erase (struct mtd_info *mtd, struct erase_info *instr)
  633. {
  634. volatile char dummy;
  635. struct DiskOnChip *this = mtd->priv;
  636. __u32 ofs = instr->addr;
  637. __u32 len = instr->len;
  638. void __iomem *docptr = this->virtadr;
  639. struct Nand *mychip = &this->chips[ofs >> this->chipshift];
  640. if (len != mtd->erasesize)
  641. printk(KERN_WARNING "Erase not right size (%x != %x)n",
  642. len, mtd->erasesize);
  643. /* Find the chip which is to be used and select it */
  644. if (this->curfloor != mychip->floor) {
  645. DoC_SelectFloor(docptr, mychip->floor);
  646. DoC_SelectChip(docptr, mychip->chip);
  647. } else if (this->curchip != mychip->chip) {
  648. DoC_SelectChip(docptr, mychip->chip);
  649. }
  650. this->curfloor = mychip->floor;
  651. this->curchip = mychip->chip;
  652. instr->state = MTD_ERASE_PENDING;
  653. /* issue the Erase Setup command */
  654. DoC_Command(docptr, NAND_CMD_ERASE1, 0x00);
  655. DoC_Address(docptr, 2, ofs, 0x00, 0x00);
  656. /* Commit the Erase Start command and wait for ready
  657. see Software Requirement 11.4 item 1.*/
  658. DoC_Command(docptr, NAND_CMD_ERASE2, 0x00);
  659. DoC_WaitReady(docptr);
  660. instr->state = MTD_ERASING;
  661. /* Read the status of the flash device through CDSN IO register
  662. see Software Requirement 11.4 item 5.
  663. FIXME: it seems that we are not wait long enough, some blocks are not
  664. erased fully */
  665. DoC_Command(docptr, NAND_CMD_STATUS, CDSN_CTRL_WP);
  666. dummy = ReadDOC(docptr, ReadPipeInit);
  667. DoC_Delay(docptr, 2);
  668. if (ReadDOC(docptr, Mil_CDSN_IO) & 1) {
  669. printk("Error Erasing at 0x%x\n", ofs);
  670. /* There was an error
  671. FIXME: implement Bad Block Replacement (in nftl.c ??) */
  672. instr->state = MTD_ERASE_FAILED;
  673. } else
  674. instr->state = MTD_ERASE_DONE;
  675. dummy = ReadDOC(docptr, LastDataRead);
  676. mtd_erase_callback(instr);
  677. return 0;
  678. }
  679. /****************************************************************************
  680. *
  681. * Module stuff
  682. *
  683. ****************************************************************************/
  684. static void __exit cleanup_doc2001(void)
  685. {
  686. struct mtd_info *mtd;
  687. struct DiskOnChip *this;
  688. while ((mtd=docmillist)) {
  689. this = mtd->priv;
  690. docmillist = this->nextdoc;
  691. mtd_device_unregister(mtd);
  692. iounmap(this->virtadr);
  693. kfree(this->chips);
  694. kfree(mtd);
  695. }
  696. }
  697. module_exit(cleanup_doc2001);
  698. MODULE_LICENSE("GPL");
  699. MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org> et al.");
  700. MODULE_DESCRIPTION("Alternative driver for DiskOnChip Millennium");