doc2001.c 25 KB

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