doc2001.c 25 KB

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