doc2001.c 25 KB

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