doc2001.c 25 KB

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