doc2001.c 25 KB

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