doc2001plus.c 31 KB

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