doc2001.c 25 KB

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