doc2001plus.c 31 KB

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