doc2001plus.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105
  1. /*
  2. * Linux driver for Disk-On-Chip Millennium Plus
  3. *
  4. * (c) 2002-2003 Greg Ungerer <gerg@snapgear.com>
  5. * (c) 2002-2003 SnapGear Inc
  6. * (c) 1999 Machine Vision Holdings, Inc.
  7. * (c) 1999, 2000 David Woodhouse <dwmw2@infradead.org>
  8. *
  9. * Released under GPL
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <asm/errno.h>
  14. #include <asm/io.h>
  15. #include <asm/uaccess.h>
  16. #include <linux/delay.h>
  17. #include <linux/slab.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 *docmilpluslist = NULL;
  40. /* Perform the required delay cycles by writing to the NOP register */
  41. static void DoC_Delay(void __iomem * docptr, int cycles)
  42. {
  43. int i;
  44. for (i = 0; (i < cycles); i++)
  45. WriteDOC(0, docptr, Mplus_NOP);
  46. }
  47. #define CDSN_CTRL_FR_B_MASK (CDSN_CTRL_FR_B0 | CDSN_CTRL_FR_B1)
  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 int c = 0xffff;
  52. pr_debug("_DoC_WaitReady called for out-of-line wait\n");
  53. /* Out-of-line routine to wait for chip response */
  54. while (((ReadDOC(docptr, Mplus_FlashControl) & CDSN_CTRL_FR_B_MASK) != CDSN_CTRL_FR_B_MASK) && --c)
  55. ;
  56. if (c == 0)
  57. pr_debug("_DoC_WaitReady timed out.\n");
  58. return (c == 0);
  59. }
  60. static inline int DoC_WaitReady(void __iomem * docptr)
  61. {
  62. /* This is inline, to optimise the common case, where it's ready instantly */
  63. int ret = 0;
  64. /* read form NOP register should be issued prior to the read from CDSNControl
  65. see Software Requirement 11.4 item 2. */
  66. DoC_Delay(docptr, 4);
  67. if ((ReadDOC(docptr, Mplus_FlashControl) & CDSN_CTRL_FR_B_MASK) != CDSN_CTRL_FR_B_MASK)
  68. /* Call the out-of-line routine to wait */
  69. ret = _DoC_WaitReady(docptr);
  70. return ret;
  71. }
  72. /* For some reason the Millennium Plus seems to occasionally put itself
  73. * into reset mode. For me this happens randomly, with no pattern that I
  74. * can detect. M-systems suggest always check this on any block level
  75. * operation and setting to normal mode if in reset mode.
  76. */
  77. static inline void DoC_CheckASIC(void __iomem * docptr)
  78. {
  79. /* Make sure the DoC is in normal mode */
  80. if ((ReadDOC(docptr, Mplus_DOCControl) & DOC_MODE_NORMAL) == 0) {
  81. WriteDOC((DOC_MODE_NORMAL | DOC_MODE_MDWREN), docptr, Mplus_DOCControl);
  82. WriteDOC(~(DOC_MODE_NORMAL | DOC_MODE_MDWREN), docptr, Mplus_CtrlConfirm);
  83. }
  84. }
  85. /* DoC_Command: Send a flash command to the flash chip through the Flash
  86. * command register. Need 2 Write Pipeline Terminates to complete send.
  87. */
  88. static void DoC_Command(void __iomem * docptr, unsigned char command,
  89. unsigned char xtraflags)
  90. {
  91. WriteDOC(command, docptr, Mplus_FlashCmd);
  92. WriteDOC(command, docptr, Mplus_WritePipeTerm);
  93. WriteDOC(command, docptr, Mplus_WritePipeTerm);
  94. }
  95. /* DoC_Address: Set the current address for the flash chip through the Flash
  96. * Address register. Need 2 Write Pipeline Terminates to complete send.
  97. */
  98. static inline void DoC_Address(struct DiskOnChip *doc, int numbytes,
  99. unsigned long ofs, unsigned char xtraflags1,
  100. unsigned char xtraflags2)
  101. {
  102. void __iomem * docptr = doc->virtadr;
  103. /* Allow for possible Mill Plus internal flash interleaving */
  104. ofs >>= doc->interleave;
  105. switch (numbytes) {
  106. case 1:
  107. /* Send single byte, bits 0-7. */
  108. WriteDOC(ofs & 0xff, docptr, Mplus_FlashAddress);
  109. break;
  110. case 2:
  111. /* Send bits 9-16 followed by 17-23 */
  112. WriteDOC((ofs >> 9) & 0xff, docptr, Mplus_FlashAddress);
  113. WriteDOC((ofs >> 17) & 0xff, docptr, Mplus_FlashAddress);
  114. break;
  115. case 3:
  116. /* Send 0-7, 9-16, then 17-23 */
  117. WriteDOC(ofs & 0xff, docptr, Mplus_FlashAddress);
  118. WriteDOC((ofs >> 9) & 0xff, docptr, Mplus_FlashAddress);
  119. WriteDOC((ofs >> 17) & 0xff, docptr, Mplus_FlashAddress);
  120. break;
  121. default:
  122. return;
  123. }
  124. WriteDOC(0x00, docptr, Mplus_WritePipeTerm);
  125. WriteDOC(0x00, docptr, Mplus_WritePipeTerm);
  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. /* No choice for flash chip on Millennium Plus */
  131. return 0;
  132. }
  133. /* DoC_SelectFloor: Select a given floor (bank of flash chips) */
  134. static int DoC_SelectFloor(void __iomem * docptr, int floor)
  135. {
  136. WriteDOC((floor & 0x3), docptr, Mplus_DeviceSelect);
  137. return 0;
  138. }
  139. /*
  140. * Translate the given offset into the appropriate command and offset.
  141. * This does the mapping using the 16bit interleave layout defined by
  142. * M-Systems, and looks like this for a sector pair:
  143. * +-----------+-------+-------+-------+--------------+---------+-----------+
  144. * | 0 --- 511 |512-517|518-519|520-521| 522 --- 1033 |1034-1039|1040 - 1055|
  145. * +-----------+-------+-------+-------+--------------+---------+-----------+
  146. * | Data 0 | ECC 0 |Flags0 |Flags1 | Data 1 |ECC 1 | OOB 1 + 2 |
  147. * +-----------+-------+-------+-------+--------------+---------+-----------+
  148. */
  149. /* FIXME: This lives in INFTL not here. Other users of flash devices
  150. may not want it */
  151. static unsigned int DoC_GetDataOffset(struct mtd_info *mtd, loff_t *from)
  152. {
  153. struct DiskOnChip *this = mtd->priv;
  154. if (this->interleave) {
  155. unsigned int ofs = *from & 0x3ff;
  156. unsigned int cmd;
  157. if (ofs < 512) {
  158. cmd = NAND_CMD_READ0;
  159. ofs &= 0x1ff;
  160. } else if (ofs < 1014) {
  161. cmd = NAND_CMD_READ1;
  162. ofs = (ofs & 0x1ff) + 10;
  163. } else {
  164. cmd = NAND_CMD_READOOB;
  165. ofs = ofs - 1014;
  166. }
  167. *from = (*from & ~0x3ff) | ofs;
  168. return cmd;
  169. } else {
  170. /* No interleave */
  171. if ((*from) & 0x100)
  172. return NAND_CMD_READ1;
  173. return NAND_CMD_READ0;
  174. }
  175. }
  176. static unsigned int DoC_GetECCOffset(struct mtd_info *mtd, loff_t *from)
  177. {
  178. unsigned int ofs, cmd;
  179. if (*from & 0x200) {
  180. cmd = NAND_CMD_READOOB;
  181. ofs = 10 + (*from & 0xf);
  182. } else {
  183. cmd = NAND_CMD_READ1;
  184. ofs = (*from & 0xf);
  185. }
  186. *from = (*from & ~0x3ff) | ofs;
  187. return cmd;
  188. }
  189. static unsigned int DoC_GetFlagsOffset(struct mtd_info *mtd, loff_t *from)
  190. {
  191. unsigned int ofs, cmd;
  192. cmd = NAND_CMD_READ1;
  193. ofs = (*from & 0x200) ? 8 : 6;
  194. *from = (*from & ~0x3ff) | ofs;
  195. return cmd;
  196. }
  197. static unsigned int DoC_GetHdrOffset(struct mtd_info *mtd, loff_t *from)
  198. {
  199. unsigned int ofs, cmd;
  200. cmd = NAND_CMD_READOOB;
  201. ofs = (*from & 0x200) ? 24 : 16;
  202. *from = (*from & ~0x3ff) | ofs;
  203. return cmd;
  204. }
  205. static inline void MemReadDOC(void __iomem * docptr, unsigned char *buf, int len)
  206. {
  207. #ifndef USE_MEMCPY
  208. int i;
  209. for (i = 0; i < len; i++)
  210. buf[i] = ReadDOC(docptr, Mil_CDSN_IO + i);
  211. #else
  212. memcpy_fromio(buf, docptr + DoC_Mil_CDSN_IO, len);
  213. #endif
  214. }
  215. static inline void MemWriteDOC(void __iomem * docptr, unsigned char *buf, int len)
  216. {
  217. #ifndef USE_MEMCPY
  218. int i;
  219. for (i = 0; i < len; i++)
  220. WriteDOC(buf[i], docptr, Mil_CDSN_IO + i);
  221. #else
  222. memcpy_toio(docptr + DoC_Mil_CDSN_IO, buf, len);
  223. #endif
  224. }
  225. /* DoC_IdentChip: Identify a given NAND chip given {floor,chip} */
  226. static int DoC_IdentChip(struct DiskOnChip *doc, int floor, int chip)
  227. {
  228. int mfr, id, i, j;
  229. volatile char dummy;
  230. void __iomem * docptr = doc->virtadr;
  231. /* Page in the required floor/chip */
  232. DoC_SelectFloor(docptr, floor);
  233. DoC_SelectChip(docptr, chip);
  234. /* Millennium Plus bus cycle sequence as per figure 2, section 2.4 */
  235. WriteDOC((DOC_FLASH_CE | DOC_FLASH_WP), docptr, Mplus_FlashSelect);
  236. /* Reset the chip, see Software Requirement 11.4 item 1. */
  237. DoC_Command(docptr, NAND_CMD_RESET, 0);
  238. DoC_WaitReady(docptr);
  239. /* Read the NAND chip ID: 1. Send ReadID command */
  240. DoC_Command(docptr, NAND_CMD_READID, 0);
  241. /* Read the NAND chip ID: 2. Send address byte zero */
  242. DoC_Address(doc, 1, 0x00, 0, 0x00);
  243. WriteDOC(0, docptr, Mplus_FlashControl);
  244. DoC_WaitReady(docptr);
  245. /* Read the manufacturer and device id codes of the flash device through
  246. CDSN IO register see Software Requirement 11.4 item 5.*/
  247. dummy = ReadDOC(docptr, Mplus_ReadPipeInit);
  248. dummy = ReadDOC(docptr, Mplus_ReadPipeInit);
  249. mfr = ReadDOC(docptr, Mil_CDSN_IO);
  250. if (doc->interleave)
  251. dummy = ReadDOC(docptr, Mil_CDSN_IO); /* 2 way interleave */
  252. id = ReadDOC(docptr, Mil_CDSN_IO);
  253. if (doc->interleave)
  254. dummy = ReadDOC(docptr, Mil_CDSN_IO); /* 2 way interleave */
  255. dummy = ReadDOC(docptr, Mplus_LastDataRead);
  256. dummy = ReadDOC(docptr, Mplus_LastDataRead);
  257. /* Disable flash internally */
  258. WriteDOC(0, docptr, Mplus_FlashSelect);
  259. /* No response - return failure */
  260. if (mfr == 0xff || mfr == 0)
  261. return 0;
  262. for (i = 0; nand_flash_ids[i].name != NULL; i++) {
  263. if (id == nand_flash_ids[i].id) {
  264. /* Try to identify manufacturer */
  265. for (j = 0; nand_manuf_ids[j].id != 0x0; j++) {
  266. if (nand_manuf_ids[j].id == mfr)
  267. break;
  268. }
  269. printk(KERN_INFO "Flash chip found: Manufacturer ID: %2.2X, "
  270. "Chip ID: %2.2X (%s:%s)\n", mfr, id,
  271. nand_manuf_ids[j].name, nand_flash_ids[i].name);
  272. doc->mfr = mfr;
  273. doc->id = id;
  274. doc->chipshift = ffs((nand_flash_ids[i].chipsize << 20)) - 1;
  275. doc->erasesize = nand_flash_ids[i].erasesize << doc->interleave;
  276. break;
  277. }
  278. }
  279. if (nand_flash_ids[i].name == NULL)
  280. return 0;
  281. return 1;
  282. }
  283. /* DoC_ScanChips: Find all NAND chips present in a DiskOnChip, and identify them */
  284. static void DoC_ScanChips(struct DiskOnChip *this)
  285. {
  286. int floor, chip;
  287. int numchips[MAX_FLOORS_MPLUS];
  288. int ret;
  289. this->numchips = 0;
  290. this->mfr = 0;
  291. this->id = 0;
  292. /* Work out the intended interleave setting */
  293. this->interleave = 0;
  294. if (this->ChipID == DOC_ChipID_DocMilPlus32)
  295. this->interleave = 1;
  296. /* Check the ASIC agrees */
  297. if ( (this->interleave << 2) !=
  298. (ReadDOC(this->virtadr, Mplus_Configuration) & 4)) {
  299. u_char conf = ReadDOC(this->virtadr, Mplus_Configuration);
  300. printk(KERN_NOTICE "Setting DiskOnChip Millennium Plus interleave to %s\n",
  301. this->interleave?"on (16-bit)":"off (8-bit)");
  302. conf ^= 4;
  303. WriteDOC(conf, this->virtadr, Mplus_Configuration);
  304. }
  305. /* For each floor, find the number of valid chips it contains */
  306. for (floor = 0,ret = 1; floor < MAX_FLOORS_MPLUS; floor++) {
  307. numchips[floor] = 0;
  308. for (chip = 0; chip < MAX_CHIPS_MPLUS && ret != 0; chip++) {
  309. ret = DoC_IdentChip(this, floor, chip);
  310. if (ret) {
  311. numchips[floor]++;
  312. this->numchips++;
  313. }
  314. }
  315. }
  316. /* If there are none at all that we recognise, bail */
  317. if (!this->numchips) {
  318. printk("No flash chips recognised.\n");
  319. return;
  320. }
  321. /* Allocate an array to hold the information for each chip */
  322. this->chips = kmalloc(sizeof(struct Nand) * this->numchips, GFP_KERNEL);
  323. if (!this->chips){
  324. printk("MTD: No memory for allocating chip info structures\n");
  325. return;
  326. }
  327. /* Fill out the chip array with {floor, chipno} for each
  328. * detected chip in the device. */
  329. for (floor = 0, ret = 0; floor < MAX_FLOORS_MPLUS; floor++) {
  330. for (chip = 0 ; chip < numchips[floor] ; chip++) {
  331. this->chips[ret].floor = floor;
  332. this->chips[ret].chip = chip;
  333. this->chips[ret].curadr = 0;
  334. this->chips[ret].curmode = 0x50;
  335. ret++;
  336. }
  337. }
  338. /* Calculate and print the total size of the device */
  339. this->totlen = this->numchips * (1 << this->chipshift);
  340. printk(KERN_INFO "%d flash chips found. Total DiskOnChip size: %ld MiB\n",
  341. this->numchips ,this->totlen >> 20);
  342. }
  343. static int DoCMilPlus_is_alias(struct DiskOnChip *doc1, struct DiskOnChip *doc2)
  344. {
  345. int tmp1, tmp2, retval;
  346. if (doc1->physadr == doc2->physadr)
  347. return 1;
  348. /* Use the alias resolution register which was set aside for this
  349. * purpose. If it's value is the same on both chips, they might
  350. * be the same chip, and we write to one and check for a change in
  351. * the other. It's unclear if this register is usuable in the
  352. * DoC 2000 (it's in the Millennium docs), but it seems to work. */
  353. tmp1 = ReadDOC(doc1->virtadr, Mplus_AliasResolution);
  354. tmp2 = ReadDOC(doc2->virtadr, Mplus_AliasResolution);
  355. if (tmp1 != tmp2)
  356. return 0;
  357. WriteDOC((tmp1+1) % 0xff, doc1->virtadr, Mplus_AliasResolution);
  358. tmp2 = ReadDOC(doc2->virtadr, Mplus_AliasResolution);
  359. if (tmp2 == (tmp1+1) % 0xff)
  360. retval = 1;
  361. else
  362. retval = 0;
  363. /* Restore register contents. May not be necessary, but do it just to
  364. * be safe. */
  365. WriteDOC(tmp1, doc1->virtadr, Mplus_AliasResolution);
  366. return retval;
  367. }
  368. /* This routine is found from the docprobe code by symbol_get(),
  369. * which will bump the use count of this module. */
  370. void DoCMilPlus_init(struct mtd_info *mtd)
  371. {
  372. struct DiskOnChip *this = mtd->priv;
  373. struct DiskOnChip *old = NULL;
  374. /* We must avoid being called twice for the same device. */
  375. if (docmilpluslist)
  376. old = docmilpluslist->priv;
  377. while (old) {
  378. if (DoCMilPlus_is_alias(this, old)) {
  379. printk(KERN_NOTICE "Ignoring DiskOnChip Millennium "
  380. "Plus at 0x%lX - already configured\n",
  381. this->physadr);
  382. iounmap(this->virtadr);
  383. kfree(mtd);
  384. return;
  385. }
  386. if (old->nextdoc)
  387. old = old->nextdoc->priv;
  388. else
  389. old = NULL;
  390. }
  391. mtd->name = "DiskOnChip Millennium Plus";
  392. printk(KERN_NOTICE "DiskOnChip Millennium Plus found at "
  393. "address 0x%lX\n", this->physadr);
  394. mtd->type = MTD_NANDFLASH;
  395. mtd->flags = MTD_CAP_NANDFLASH;
  396. mtd->size = 0;
  397. mtd->erasesize = 0;
  398. mtd->writesize = 512;
  399. mtd->oobsize = 16;
  400. mtd->owner = THIS_MODULE;
  401. mtd->erase = doc_erase;
  402. mtd->point = NULL;
  403. mtd->unpoint = NULL;
  404. mtd->read = doc_read;
  405. mtd->write = doc_write;
  406. mtd->read_oob = doc_read_oob;
  407. mtd->write_oob = doc_write_oob;
  408. mtd->sync = NULL;
  409. this->totlen = 0;
  410. this->numchips = 0;
  411. this->curfloor = -1;
  412. this->curchip = -1;
  413. /* Ident all the chips present. */
  414. DoC_ScanChips(this);
  415. if (!this->totlen) {
  416. kfree(mtd);
  417. iounmap(this->virtadr);
  418. } else {
  419. this->nextdoc = docmilpluslist;
  420. docmilpluslist = mtd;
  421. mtd->size = this->totlen;
  422. mtd->erasesize = this->erasesize;
  423. mtd_device_register(mtd, NULL, 0);
  424. return;
  425. }
  426. }
  427. EXPORT_SYMBOL_GPL(DoCMilPlus_init);
  428. #if 0
  429. static int doc_dumpblk(struct mtd_info *mtd, loff_t from)
  430. {
  431. int i;
  432. loff_t fofs;
  433. struct DiskOnChip *this = mtd->priv;
  434. void __iomem * docptr = this->virtadr;
  435. struct Nand *mychip = &this->chips[from >> (this->chipshift)];
  436. unsigned char *bp, buf[1056];
  437. char c[32];
  438. from &= ~0x3ff;
  439. /* Don't allow read past end of device */
  440. if (from >= this->totlen)
  441. return -EINVAL;
  442. DoC_CheckASIC(docptr);
  443. /* Find the chip which is to be used and select it */
  444. if (this->curfloor != mychip->floor) {
  445. DoC_SelectFloor(docptr, mychip->floor);
  446. DoC_SelectChip(docptr, mychip->chip);
  447. } else if (this->curchip != mychip->chip) {
  448. DoC_SelectChip(docptr, mychip->chip);
  449. }
  450. this->curfloor = mychip->floor;
  451. this->curchip = mychip->chip;
  452. /* Millennium Plus bus cycle sequence as per figure 2, section 2.4 */
  453. WriteDOC((DOC_FLASH_CE | DOC_FLASH_WP), docptr, Mplus_FlashSelect);
  454. /* Reset the chip, see Software Requirement 11.4 item 1. */
  455. DoC_Command(docptr, NAND_CMD_RESET, 0);
  456. DoC_WaitReady(docptr);
  457. fofs = from;
  458. DoC_Command(docptr, DoC_GetDataOffset(mtd, &fofs), 0);
  459. DoC_Address(this, 3, fofs, 0, 0x00);
  460. WriteDOC(0, docptr, Mplus_FlashControl);
  461. DoC_WaitReady(docptr);
  462. /* disable the ECC engine */
  463. WriteDOC(DOC_ECC_RESET, docptr, Mplus_ECCConf);
  464. ReadDOC(docptr, Mplus_ReadPipeInit);
  465. ReadDOC(docptr, Mplus_ReadPipeInit);
  466. /* Read the data via the internal pipeline through CDSN IO
  467. register, see Pipelined Read Operations 11.3 */
  468. MemReadDOC(docptr, buf, 1054);
  469. buf[1054] = ReadDOC(docptr, Mplus_LastDataRead);
  470. buf[1055] = ReadDOC(docptr, Mplus_LastDataRead);
  471. memset(&c[0], 0, sizeof(c));
  472. printk("DUMP OFFSET=%x:\n", (int)from);
  473. for (i = 0, bp = &buf[0]; (i < 1056); i++) {
  474. if ((i % 16) == 0)
  475. printk("%08x: ", i);
  476. printk(" %02x", *bp);
  477. c[(i & 0xf)] = ((*bp >= 0x20) && (*bp <= 0x7f)) ? *bp : '.';
  478. bp++;
  479. if (((i + 1) % 16) == 0)
  480. printk(" %s\n", c);
  481. }
  482. printk("\n");
  483. /* Disable flash internally */
  484. WriteDOC(0, docptr, Mplus_FlashSelect);
  485. return 0;
  486. }
  487. #endif
  488. static int doc_read(struct mtd_info *mtd, loff_t from, size_t len,
  489. size_t *retlen, u_char *buf)
  490. {
  491. int ret, i;
  492. volatile char dummy;
  493. loff_t fofs;
  494. unsigned char syndrome[6], eccbuf[6];
  495. struct DiskOnChip *this = mtd->priv;
  496. void __iomem * docptr = this->virtadr;
  497. struct Nand *mychip = &this->chips[from >> (this->chipshift)];
  498. /* Don't allow read past end of device */
  499. if (from >= this->totlen)
  500. return -EINVAL;
  501. /* Don't allow a single read to cross a 512-byte block boundary */
  502. if (from + len > ((from | 0x1ff) + 1))
  503. len = ((from | 0x1ff) + 1) - from;
  504. DoC_CheckASIC(docptr);
  505. /* Find the chip which is to be used and select it */
  506. if (this->curfloor != mychip->floor) {
  507. DoC_SelectFloor(docptr, mychip->floor);
  508. DoC_SelectChip(docptr, mychip->chip);
  509. } else if (this->curchip != mychip->chip) {
  510. DoC_SelectChip(docptr, mychip->chip);
  511. }
  512. this->curfloor = mychip->floor;
  513. this->curchip = mychip->chip;
  514. /* Millennium Plus bus cycle sequence as per figure 2, section 2.4 */
  515. WriteDOC((DOC_FLASH_CE | DOC_FLASH_WP), docptr, Mplus_FlashSelect);
  516. /* Reset the chip, see Software Requirement 11.4 item 1. */
  517. DoC_Command(docptr, NAND_CMD_RESET, 0);
  518. DoC_WaitReady(docptr);
  519. fofs = from;
  520. DoC_Command(docptr, DoC_GetDataOffset(mtd, &fofs), 0);
  521. DoC_Address(this, 3, fofs, 0, 0x00);
  522. WriteDOC(0, docptr, Mplus_FlashControl);
  523. DoC_WaitReady(docptr);
  524. /* init the ECC engine, see Reed-Solomon EDC/ECC 11.1 .*/
  525. WriteDOC(DOC_ECC_RESET, docptr, Mplus_ECCConf);
  526. WriteDOC(DOC_ECC_EN, docptr, Mplus_ECCConf);
  527. /* Let the caller know we completed it */
  528. *retlen = len;
  529. ret = 0;
  530. ReadDOC(docptr, Mplus_ReadPipeInit);
  531. ReadDOC(docptr, Mplus_ReadPipeInit);
  532. /* Read the data via the internal pipeline through CDSN IO
  533. register, see Pipelined Read Operations 11.3 */
  534. MemReadDOC(docptr, buf, len);
  535. /* Read the ECC data following raw data */
  536. MemReadDOC(docptr, eccbuf, 4);
  537. eccbuf[4] = ReadDOC(docptr, Mplus_LastDataRead);
  538. eccbuf[5] = ReadDOC(docptr, Mplus_LastDataRead);
  539. /* Flush the pipeline */
  540. dummy = ReadDOC(docptr, Mplus_ECCConf);
  541. dummy = ReadDOC(docptr, Mplus_ECCConf);
  542. /* Check the ECC Status */
  543. if (ReadDOC(docptr, Mplus_ECCConf) & 0x80) {
  544. int nb_errors;
  545. /* There was an ECC error */
  546. #ifdef ECC_DEBUG
  547. printk("DiskOnChip ECC Error: Read at %lx\n", (long)from);
  548. #endif
  549. /* Read the ECC syndrome through the DiskOnChip ECC logic.
  550. These syndrome will be all ZERO when there is no error */
  551. for (i = 0; i < 6; i++)
  552. syndrome[i] = ReadDOC(docptr, Mplus_ECCSyndrome0 + i);
  553. nb_errors = doc_decode_ecc(buf, syndrome);
  554. #ifdef ECC_DEBUG
  555. printk("ECC Errors corrected: %x\n", nb_errors);
  556. #endif
  557. if (nb_errors < 0) {
  558. /* We return error, but have actually done the
  559. read. Not that this can be told to user-space, via
  560. sys_read(), but at least MTD-aware stuff can know
  561. about it by checking *retlen */
  562. #ifdef ECC_DEBUG
  563. printk("%s(%d): Millennium Plus ECC error (from=0x%x:\n",
  564. __FILE__, __LINE__, (int)from);
  565. printk(" syndrome= %02x:%02x:%02x:%02x:%02x:"
  566. "%02x\n",
  567. syndrome[0], syndrome[1], syndrome[2],
  568. syndrome[3], syndrome[4], syndrome[5]);
  569. printk(" eccbuf= %02x:%02x:%02x:%02x:%02x:"
  570. "%02x\n",
  571. eccbuf[0], eccbuf[1], eccbuf[2],
  572. eccbuf[3], eccbuf[4], eccbuf[5]);
  573. #endif
  574. ret = -EIO;
  575. }
  576. }
  577. #ifdef PSYCHO_DEBUG
  578. printk("ECC DATA at %lx: %2.2X %2.2X %2.2X %2.2X %2.2X %2.2X\n",
  579. (long)from, eccbuf[0], eccbuf[1], eccbuf[2], eccbuf[3],
  580. eccbuf[4], eccbuf[5]);
  581. #endif
  582. /* disable the ECC engine */
  583. WriteDOC(DOC_ECC_DIS, docptr , Mplus_ECCConf);
  584. /* Disable flash internally */
  585. WriteDOC(0, docptr, Mplus_FlashSelect);
  586. return ret;
  587. }
  588. static int doc_write(struct mtd_info *mtd, loff_t to, size_t len,
  589. size_t *retlen, const u_char *buf)
  590. {
  591. int i, before, ret = 0;
  592. loff_t fto;
  593. volatile char dummy;
  594. char eccbuf[6];
  595. struct DiskOnChip *this = mtd->priv;
  596. void __iomem * docptr = this->virtadr;
  597. struct Nand *mychip = &this->chips[to >> (this->chipshift)];
  598. /* Don't allow write past end of device */
  599. if (to >= this->totlen)
  600. return -EINVAL;
  601. /* Don't allow writes which aren't exactly one block (512 bytes) */
  602. if ((to & 0x1ff) || (len != 0x200))
  603. return -EINVAL;
  604. /* Determine position of OOB flags, before or after data */
  605. before = (this->interleave && (to & 0x200));
  606. DoC_CheckASIC(docptr);
  607. /* Find the chip which is to be used and select it */
  608. if (this->curfloor != mychip->floor) {
  609. DoC_SelectFloor(docptr, mychip->floor);
  610. DoC_SelectChip(docptr, mychip->chip);
  611. } else if (this->curchip != mychip->chip) {
  612. DoC_SelectChip(docptr, mychip->chip);
  613. }
  614. this->curfloor = mychip->floor;
  615. this->curchip = mychip->chip;
  616. /* Millennium Plus bus cycle sequence as per figure 2, section 2.4 */
  617. WriteDOC(DOC_FLASH_CE, docptr, Mplus_FlashSelect);
  618. /* Reset the chip, see Software Requirement 11.4 item 1. */
  619. DoC_Command(docptr, NAND_CMD_RESET, 0);
  620. DoC_WaitReady(docptr);
  621. /* Set device to appropriate plane of flash */
  622. fto = to;
  623. WriteDOC(DoC_GetDataOffset(mtd, &fto), docptr, Mplus_FlashCmd);
  624. /* On interleaved devices the flags for 2nd half 512 are before data */
  625. if (before)
  626. fto -= 2;
  627. /* issue the Serial Data In command to initial the Page Program process */
  628. DoC_Command(docptr, NAND_CMD_SEQIN, 0x00);
  629. DoC_Address(this, 3, fto, 0x00, 0x00);
  630. /* Disable the ECC engine */
  631. WriteDOC(DOC_ECC_RESET, docptr, Mplus_ECCConf);
  632. if (before) {
  633. /* Write the block status BLOCK_USED (0x5555) */
  634. WriteDOC(0x55, docptr, Mil_CDSN_IO);
  635. WriteDOC(0x55, docptr, Mil_CDSN_IO);
  636. }
  637. /* init the ECC engine, see Reed-Solomon EDC/ECC 11.1 .*/
  638. WriteDOC(DOC_ECC_EN | DOC_ECC_RW, docptr, Mplus_ECCConf);
  639. MemWriteDOC(docptr, (unsigned char *) buf, len);
  640. /* Write ECC data to flash, the ECC info is generated by
  641. the DiskOnChip ECC logic see Reed-Solomon EDC/ECC 11.1 */
  642. DoC_Delay(docptr, 3);
  643. /* Read the ECC data through the DiskOnChip ECC logic */
  644. for (i = 0; i < 6; i++)
  645. eccbuf[i] = ReadDOC(docptr, Mplus_ECCSyndrome0 + i);
  646. /* disable the ECC engine */
  647. WriteDOC(DOC_ECC_DIS, docptr, Mplus_ECCConf);
  648. /* Write the ECC data to flash */
  649. MemWriteDOC(docptr, eccbuf, 6);
  650. if (!before) {
  651. /* Write the block status BLOCK_USED (0x5555) */
  652. WriteDOC(0x55, docptr, Mil_CDSN_IO+6);
  653. WriteDOC(0x55, docptr, Mil_CDSN_IO+7);
  654. }
  655. #ifdef PSYCHO_DEBUG
  656. printk("OOB data at %lx is %2.2X %2.2X %2.2X %2.2X %2.2X %2.2X\n",
  657. (long) to, eccbuf[0], eccbuf[1], eccbuf[2], eccbuf[3],
  658. eccbuf[4], eccbuf[5]);
  659. #endif
  660. WriteDOC(0x00, docptr, Mplus_WritePipeTerm);
  661. WriteDOC(0x00, docptr, Mplus_WritePipeTerm);
  662. /* Commit the Page Program command and wait for ready
  663. see Software Requirement 11.4 item 1.*/
  664. DoC_Command(docptr, NAND_CMD_PAGEPROG, 0x00);
  665. DoC_WaitReady(docptr);
  666. /* Read the status of the flash device through CDSN IO register
  667. see Software Requirement 11.4 item 5.*/
  668. DoC_Command(docptr, NAND_CMD_STATUS, 0);
  669. dummy = ReadDOC(docptr, Mplus_ReadPipeInit);
  670. dummy = ReadDOC(docptr, Mplus_ReadPipeInit);
  671. DoC_Delay(docptr, 2);
  672. if ((dummy = ReadDOC(docptr, Mplus_LastDataRead)) & 1) {
  673. printk("MTD: Error 0x%x programming at 0x%x\n", dummy, (int)to);
  674. /* Error in programming
  675. FIXME: implement Bad Block Replacement (in nftl.c ??) */
  676. *retlen = 0;
  677. ret = -EIO;
  678. }
  679. dummy = ReadDOC(docptr, Mplus_LastDataRead);
  680. /* Disable flash internally */
  681. WriteDOC(0, docptr, Mplus_FlashSelect);
  682. /* Let the caller know we completed it */
  683. *retlen = len;
  684. return ret;
  685. }
  686. static int doc_read_oob(struct mtd_info *mtd, loff_t ofs,
  687. struct mtd_oob_ops *ops)
  688. {
  689. loff_t fofs, base;
  690. struct DiskOnChip *this = mtd->priv;
  691. void __iomem * docptr = this->virtadr;
  692. struct Nand *mychip = &this->chips[ofs >> this->chipshift];
  693. size_t i, size, got, want;
  694. uint8_t *buf = ops->oobbuf;
  695. size_t len = ops->len;
  696. BUG_ON(ops->mode != MTD_OPS_PLACE_OOB);
  697. ofs += ops->ooboffs;
  698. DoC_CheckASIC(docptr);
  699. /* Find the chip which is to be used and select it */
  700. if (this->curfloor != mychip->floor) {
  701. DoC_SelectFloor(docptr, mychip->floor);
  702. DoC_SelectChip(docptr, mychip->chip);
  703. } else if (this->curchip != mychip->chip) {
  704. DoC_SelectChip(docptr, mychip->chip);
  705. }
  706. this->curfloor = mychip->floor;
  707. this->curchip = mychip->chip;
  708. /* Millennium Plus bus cycle sequence as per figure 2, section 2.4 */
  709. WriteDOC((DOC_FLASH_CE | DOC_FLASH_WP), docptr, Mplus_FlashSelect);
  710. /* disable the ECC engine */
  711. WriteDOC(DOC_ECC_RESET, docptr, Mplus_ECCConf);
  712. DoC_WaitReady(docptr);
  713. /* Maximum of 16 bytes in the OOB region, so limit read to that */
  714. if (len > 16)
  715. len = 16;
  716. got = 0;
  717. want = len;
  718. for (i = 0; ((i < 3) && (want > 0)); i++) {
  719. /* Figure out which region we are accessing... */
  720. fofs = ofs;
  721. base = ofs & 0xf;
  722. if (!this->interleave) {
  723. DoC_Command(docptr, NAND_CMD_READOOB, 0);
  724. size = 16 - base;
  725. } else if (base < 6) {
  726. DoC_Command(docptr, DoC_GetECCOffset(mtd, &fofs), 0);
  727. size = 6 - base;
  728. } else if (base < 8) {
  729. DoC_Command(docptr, DoC_GetFlagsOffset(mtd, &fofs), 0);
  730. size = 8 - base;
  731. } else {
  732. DoC_Command(docptr, DoC_GetHdrOffset(mtd, &fofs), 0);
  733. size = 16 - base;
  734. }
  735. if (size > want)
  736. size = want;
  737. /* Issue read command */
  738. DoC_Address(this, 3, fofs, 0, 0x00);
  739. WriteDOC(0, docptr, Mplus_FlashControl);
  740. DoC_WaitReady(docptr);
  741. ReadDOC(docptr, Mplus_ReadPipeInit);
  742. ReadDOC(docptr, Mplus_ReadPipeInit);
  743. MemReadDOC(docptr, &buf[got], size - 2);
  744. buf[got + size - 2] = ReadDOC(docptr, Mplus_LastDataRead);
  745. buf[got + size - 1] = ReadDOC(docptr, Mplus_LastDataRead);
  746. ofs += size;
  747. got += size;
  748. want -= size;
  749. }
  750. /* Disable flash internally */
  751. WriteDOC(0, docptr, Mplus_FlashSelect);
  752. ops->retlen = len;
  753. return 0;
  754. }
  755. static int doc_write_oob(struct mtd_info *mtd, loff_t ofs,
  756. struct mtd_oob_ops *ops)
  757. {
  758. volatile char dummy;
  759. loff_t fofs, base;
  760. struct DiskOnChip *this = mtd->priv;
  761. void __iomem * docptr = this->virtadr;
  762. struct Nand *mychip = &this->chips[ofs >> this->chipshift];
  763. size_t i, size, got, want;
  764. int ret = 0;
  765. uint8_t *buf = ops->oobbuf;
  766. size_t len = ops->len;
  767. BUG_ON(ops->mode != MTD_OPS_PLACE_OOB);
  768. ofs += ops->ooboffs;
  769. DoC_CheckASIC(docptr);
  770. /* Find the chip which is to be used and select it */
  771. if (this->curfloor != mychip->floor) {
  772. DoC_SelectFloor(docptr, mychip->floor);
  773. DoC_SelectChip(docptr, mychip->chip);
  774. } else if (this->curchip != mychip->chip) {
  775. DoC_SelectChip(docptr, mychip->chip);
  776. }
  777. this->curfloor = mychip->floor;
  778. this->curchip = mychip->chip;
  779. /* Millennium Plus bus cycle sequence as per figure 2, section 2.4 */
  780. WriteDOC(DOC_FLASH_CE, docptr, Mplus_FlashSelect);
  781. /* Maximum of 16 bytes in the OOB region, so limit write to that */
  782. if (len > 16)
  783. len = 16;
  784. got = 0;
  785. want = len;
  786. for (i = 0; ((i < 3) && (want > 0)); i++) {
  787. /* Reset the chip, see Software Requirement 11.4 item 1. */
  788. DoC_Command(docptr, NAND_CMD_RESET, 0);
  789. DoC_WaitReady(docptr);
  790. /* Figure out which region we are accessing... */
  791. fofs = ofs;
  792. base = ofs & 0x0f;
  793. if (!this->interleave) {
  794. WriteDOC(NAND_CMD_READOOB, docptr, Mplus_FlashCmd);
  795. size = 16 - base;
  796. } else if (base < 6) {
  797. WriteDOC(DoC_GetECCOffset(mtd, &fofs), docptr, Mplus_FlashCmd);
  798. size = 6 - base;
  799. } else if (base < 8) {
  800. WriteDOC(DoC_GetFlagsOffset(mtd, &fofs), docptr, Mplus_FlashCmd);
  801. size = 8 - base;
  802. } else {
  803. WriteDOC(DoC_GetHdrOffset(mtd, &fofs), docptr, Mplus_FlashCmd);
  804. size = 16 - base;
  805. }
  806. if (size > want)
  807. size = want;
  808. /* Issue the Serial Data In command to initial the Page Program process */
  809. DoC_Command(docptr, NAND_CMD_SEQIN, 0x00);
  810. DoC_Address(this, 3, fofs, 0, 0x00);
  811. /* Disable the ECC engine */
  812. WriteDOC(DOC_ECC_RESET, docptr, Mplus_ECCConf);
  813. /* Write the data via the internal pipeline through CDSN IO
  814. register, see Pipelined Write Operations 11.2 */
  815. MemWriteDOC(docptr, (unsigned char *) &buf[got], size);
  816. WriteDOC(0x00, docptr, Mplus_WritePipeTerm);
  817. WriteDOC(0x00, docptr, Mplus_WritePipeTerm);
  818. /* Commit the Page Program command and wait for ready
  819. see Software Requirement 11.4 item 1.*/
  820. DoC_Command(docptr, NAND_CMD_PAGEPROG, 0x00);
  821. DoC_WaitReady(docptr);
  822. /* Read the status of the flash device through CDSN IO register
  823. see Software Requirement 11.4 item 5.*/
  824. DoC_Command(docptr, NAND_CMD_STATUS, 0x00);
  825. dummy = ReadDOC(docptr, Mplus_ReadPipeInit);
  826. dummy = ReadDOC(docptr, Mplus_ReadPipeInit);
  827. DoC_Delay(docptr, 2);
  828. if ((dummy = ReadDOC(docptr, Mplus_LastDataRead)) & 1) {
  829. printk("MTD: Error 0x%x programming oob at 0x%x\n",
  830. dummy, (int)ofs);
  831. /* FIXME: implement Bad Block Replacement */
  832. ops->retlen = 0;
  833. ret = -EIO;
  834. }
  835. dummy = ReadDOC(docptr, Mplus_LastDataRead);
  836. ofs += size;
  837. got += size;
  838. want -= size;
  839. }
  840. /* Disable flash internally */
  841. WriteDOC(0, docptr, Mplus_FlashSelect);
  842. ops->retlen = len;
  843. return ret;
  844. }
  845. int doc_erase(struct mtd_info *mtd, struct erase_info *instr)
  846. {
  847. volatile char dummy;
  848. struct DiskOnChip *this = mtd->priv;
  849. __u32 ofs = instr->addr;
  850. __u32 len = instr->len;
  851. void __iomem * docptr = this->virtadr;
  852. struct Nand *mychip = &this->chips[ofs >> this->chipshift];
  853. DoC_CheckASIC(docptr);
  854. if (len != mtd->erasesize)
  855. printk(KERN_WARNING "MTD: Erase not right size (%x != %x)n",
  856. len, mtd->erasesize);
  857. /* Find the chip which is to be used and select it */
  858. if (this->curfloor != mychip->floor) {
  859. DoC_SelectFloor(docptr, mychip->floor);
  860. DoC_SelectChip(docptr, mychip->chip);
  861. } else if (this->curchip != mychip->chip) {
  862. DoC_SelectChip(docptr, mychip->chip);
  863. }
  864. this->curfloor = mychip->floor;
  865. this->curchip = mychip->chip;
  866. instr->state = MTD_ERASE_PENDING;
  867. /* Millennium Plus bus cycle sequence as per figure 2, section 2.4 */
  868. WriteDOC(DOC_FLASH_CE, docptr, Mplus_FlashSelect);
  869. DoC_Command(docptr, NAND_CMD_RESET, 0x00);
  870. DoC_WaitReady(docptr);
  871. DoC_Command(docptr, NAND_CMD_ERASE1, 0);
  872. DoC_Address(this, 2, ofs, 0, 0x00);
  873. DoC_Command(docptr, NAND_CMD_ERASE2, 0);
  874. DoC_WaitReady(docptr);
  875. instr->state = MTD_ERASING;
  876. /* Read the status of the flash device through CDSN IO register
  877. see Software Requirement 11.4 item 5. */
  878. DoC_Command(docptr, NAND_CMD_STATUS, 0);
  879. dummy = ReadDOC(docptr, Mplus_ReadPipeInit);
  880. dummy = ReadDOC(docptr, Mplus_ReadPipeInit);
  881. if ((dummy = ReadDOC(docptr, Mplus_LastDataRead)) & 1) {
  882. printk("MTD: Error 0x%x erasing at 0x%x\n", dummy, ofs);
  883. /* FIXME: implement Bad Block Replacement (in nftl.c ??) */
  884. instr->state = MTD_ERASE_FAILED;
  885. } else {
  886. instr->state = MTD_ERASE_DONE;
  887. }
  888. dummy = ReadDOC(docptr, Mplus_LastDataRead);
  889. /* Disable flash internally */
  890. WriteDOC(0, docptr, Mplus_FlashSelect);
  891. mtd_erase_callback(instr);
  892. return 0;
  893. }
  894. /****************************************************************************
  895. *
  896. * Module stuff
  897. *
  898. ****************************************************************************/
  899. static void __exit cleanup_doc2001plus(void)
  900. {
  901. struct mtd_info *mtd;
  902. struct DiskOnChip *this;
  903. while ((mtd=docmilpluslist)) {
  904. this = mtd->priv;
  905. docmilpluslist = this->nextdoc;
  906. mtd_device_unregister(mtd);
  907. iounmap(this->virtadr);
  908. kfree(this->chips);
  909. kfree(mtd);
  910. }
  911. }
  912. module_exit(cleanup_doc2001plus);
  913. MODULE_LICENSE("GPL");
  914. MODULE_AUTHOR("Greg Ungerer <gerg@snapgear.com> et al.");
  915. MODULE_DESCRIPTION("Driver for DiskOnChip Millennium Plus");