doc2001plus.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089
  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->writebufsize = mtd->writesize = 512;
  397. mtd->oobsize = 16;
  398. mtd->ecc_strength = 2;
  399. mtd->owner = THIS_MODULE;
  400. mtd->_erase = doc_erase;
  401. mtd->_read = doc_read;
  402. mtd->_write = doc_write;
  403. mtd->_read_oob = doc_read_oob;
  404. mtd->_write_oob = doc_write_oob;
  405. this->curfloor = -1;
  406. this->curchip = -1;
  407. /* Ident all the chips present. */
  408. DoC_ScanChips(this);
  409. if (!this->totlen) {
  410. kfree(mtd);
  411. iounmap(this->virtadr);
  412. } else {
  413. this->nextdoc = docmilpluslist;
  414. docmilpluslist = mtd;
  415. mtd->size = this->totlen;
  416. mtd->erasesize = this->erasesize;
  417. mtd_device_register(mtd, NULL, 0);
  418. return;
  419. }
  420. }
  421. EXPORT_SYMBOL_GPL(DoCMilPlus_init);
  422. #if 0
  423. static int doc_dumpblk(struct mtd_info *mtd, loff_t from)
  424. {
  425. int i;
  426. loff_t fofs;
  427. struct DiskOnChip *this = mtd->priv;
  428. void __iomem * docptr = this->virtadr;
  429. struct Nand *mychip = &this->chips[from >> (this->chipshift)];
  430. unsigned char *bp, buf[1056];
  431. char c[32];
  432. from &= ~0x3ff;
  433. /* Don't allow read past end of device */
  434. if (from >= this->totlen)
  435. return -EINVAL;
  436. DoC_CheckASIC(docptr);
  437. /* Find the chip which is to be used and select it */
  438. if (this->curfloor != mychip->floor) {
  439. DoC_SelectFloor(docptr, mychip->floor);
  440. DoC_SelectChip(docptr, mychip->chip);
  441. } else if (this->curchip != mychip->chip) {
  442. DoC_SelectChip(docptr, mychip->chip);
  443. }
  444. this->curfloor = mychip->floor;
  445. this->curchip = mychip->chip;
  446. /* Millennium Plus bus cycle sequence as per figure 2, section 2.4 */
  447. WriteDOC((DOC_FLASH_CE | DOC_FLASH_WP), docptr, Mplus_FlashSelect);
  448. /* Reset the chip, see Software Requirement 11.4 item 1. */
  449. DoC_Command(docptr, NAND_CMD_RESET, 0);
  450. DoC_WaitReady(docptr);
  451. fofs = from;
  452. DoC_Command(docptr, DoC_GetDataOffset(mtd, &fofs), 0);
  453. DoC_Address(this, 3, fofs, 0, 0x00);
  454. WriteDOC(0, docptr, Mplus_FlashControl);
  455. DoC_WaitReady(docptr);
  456. /* disable the ECC engine */
  457. WriteDOC(DOC_ECC_RESET, docptr, Mplus_ECCConf);
  458. ReadDOC(docptr, Mplus_ReadPipeInit);
  459. ReadDOC(docptr, Mplus_ReadPipeInit);
  460. /* Read the data via the internal pipeline through CDSN IO
  461. register, see Pipelined Read Operations 11.3 */
  462. MemReadDOC(docptr, buf, 1054);
  463. buf[1054] = ReadDOC(docptr, Mplus_LastDataRead);
  464. buf[1055] = ReadDOC(docptr, Mplus_LastDataRead);
  465. memset(&c[0], 0, sizeof(c));
  466. printk("DUMP OFFSET=%x:\n", (int)from);
  467. for (i = 0, bp = &buf[0]; (i < 1056); i++) {
  468. if ((i % 16) == 0)
  469. printk("%08x: ", i);
  470. printk(" %02x", *bp);
  471. c[(i & 0xf)] = ((*bp >= 0x20) && (*bp <= 0x7f)) ? *bp : '.';
  472. bp++;
  473. if (((i + 1) % 16) == 0)
  474. printk(" %s\n", c);
  475. }
  476. printk("\n");
  477. /* Disable flash internally */
  478. WriteDOC(0, docptr, Mplus_FlashSelect);
  479. return 0;
  480. }
  481. #endif
  482. static int doc_read(struct mtd_info *mtd, loff_t from, size_t len,
  483. size_t *retlen, u_char *buf)
  484. {
  485. int ret, i;
  486. volatile char dummy;
  487. loff_t fofs;
  488. unsigned char syndrome[6], eccbuf[6];
  489. struct DiskOnChip *this = mtd->priv;
  490. void __iomem * docptr = this->virtadr;
  491. struct Nand *mychip = &this->chips[from >> (this->chipshift)];
  492. /* Don't allow a single read to cross a 512-byte block boundary */
  493. if (from + len > ((from | 0x1ff) + 1))
  494. len = ((from | 0x1ff) + 1) - from;
  495. DoC_CheckASIC(docptr);
  496. /* Find the chip which is to be used and select it */
  497. if (this->curfloor != mychip->floor) {
  498. DoC_SelectFloor(docptr, mychip->floor);
  499. DoC_SelectChip(docptr, mychip->chip);
  500. } else if (this->curchip != mychip->chip) {
  501. DoC_SelectChip(docptr, mychip->chip);
  502. }
  503. this->curfloor = mychip->floor;
  504. this->curchip = mychip->chip;
  505. /* Millennium Plus bus cycle sequence as per figure 2, section 2.4 */
  506. WriteDOC((DOC_FLASH_CE | DOC_FLASH_WP), docptr, Mplus_FlashSelect);
  507. /* Reset the chip, see Software Requirement 11.4 item 1. */
  508. DoC_Command(docptr, NAND_CMD_RESET, 0);
  509. DoC_WaitReady(docptr);
  510. fofs = from;
  511. DoC_Command(docptr, DoC_GetDataOffset(mtd, &fofs), 0);
  512. DoC_Address(this, 3, fofs, 0, 0x00);
  513. WriteDOC(0, docptr, Mplus_FlashControl);
  514. DoC_WaitReady(docptr);
  515. /* init the ECC engine, see Reed-Solomon EDC/ECC 11.1 .*/
  516. WriteDOC(DOC_ECC_RESET, docptr, Mplus_ECCConf);
  517. WriteDOC(DOC_ECC_EN, docptr, Mplus_ECCConf);
  518. /* Let the caller know we completed it */
  519. *retlen = len;
  520. ret = 0;
  521. ReadDOC(docptr, Mplus_ReadPipeInit);
  522. ReadDOC(docptr, Mplus_ReadPipeInit);
  523. /* Read the data via the internal pipeline through CDSN IO
  524. register, see Pipelined Read Operations 11.3 */
  525. MemReadDOC(docptr, buf, len);
  526. /* Read the ECC data following raw data */
  527. MemReadDOC(docptr, eccbuf, 4);
  528. eccbuf[4] = ReadDOC(docptr, Mplus_LastDataRead);
  529. eccbuf[5] = ReadDOC(docptr, Mplus_LastDataRead);
  530. /* Flush the pipeline */
  531. dummy = ReadDOC(docptr, Mplus_ECCConf);
  532. dummy = ReadDOC(docptr, Mplus_ECCConf);
  533. /* Check the ECC Status */
  534. if (ReadDOC(docptr, Mplus_ECCConf) & 0x80) {
  535. int nb_errors;
  536. /* There was an ECC error */
  537. #ifdef ECC_DEBUG
  538. printk("DiskOnChip ECC Error: Read at %lx\n", (long)from);
  539. #endif
  540. /* Read the ECC syndrome through the DiskOnChip ECC logic.
  541. These syndrome will be all ZERO when there is no error */
  542. for (i = 0; i < 6; i++)
  543. syndrome[i] = ReadDOC(docptr, Mplus_ECCSyndrome0 + i);
  544. nb_errors = doc_decode_ecc(buf, syndrome);
  545. #ifdef ECC_DEBUG
  546. printk("ECC Errors corrected: %x\n", nb_errors);
  547. #endif
  548. if (nb_errors < 0) {
  549. /* We return error, but have actually done the
  550. read. Not that this can be told to user-space, via
  551. sys_read(), but at least MTD-aware stuff can know
  552. about it by checking *retlen */
  553. #ifdef ECC_DEBUG
  554. printk("%s(%d): Millennium Plus ECC error (from=0x%x:\n",
  555. __FILE__, __LINE__, (int)from);
  556. printk(" syndrome= %02x:%02x:%02x:%02x:%02x:"
  557. "%02x\n",
  558. syndrome[0], syndrome[1], syndrome[2],
  559. syndrome[3], syndrome[4], syndrome[5]);
  560. printk(" eccbuf= %02x:%02x:%02x:%02x:%02x:"
  561. "%02x\n",
  562. eccbuf[0], eccbuf[1], eccbuf[2],
  563. eccbuf[3], eccbuf[4], eccbuf[5]);
  564. #endif
  565. ret = -EIO;
  566. }
  567. }
  568. #ifdef PSYCHO_DEBUG
  569. printk("ECC DATA at %lx: %2.2X %2.2X %2.2X %2.2X %2.2X %2.2X\n",
  570. (long)from, eccbuf[0], eccbuf[1], eccbuf[2], eccbuf[3],
  571. eccbuf[4], eccbuf[5]);
  572. #endif
  573. /* disable the ECC engine */
  574. WriteDOC(DOC_ECC_DIS, docptr , Mplus_ECCConf);
  575. /* Disable flash internally */
  576. WriteDOC(0, docptr, Mplus_FlashSelect);
  577. return ret;
  578. }
  579. static int doc_write(struct mtd_info *mtd, loff_t to, size_t len,
  580. size_t *retlen, const u_char *buf)
  581. {
  582. int i, before, ret = 0;
  583. loff_t fto;
  584. volatile char dummy;
  585. char eccbuf[6];
  586. struct DiskOnChip *this = mtd->priv;
  587. void __iomem * docptr = this->virtadr;
  588. struct Nand *mychip = &this->chips[to >> (this->chipshift)];
  589. /* Don't allow writes which aren't exactly one block (512 bytes) */
  590. if ((to & 0x1ff) || (len != 0x200))
  591. return -EINVAL;
  592. /* Determine position of OOB flags, before or after data */
  593. before = (this->interleave && (to & 0x200));
  594. DoC_CheckASIC(docptr);
  595. /* Find the chip which is to be used and select it */
  596. if (this->curfloor != mychip->floor) {
  597. DoC_SelectFloor(docptr, mychip->floor);
  598. DoC_SelectChip(docptr, mychip->chip);
  599. } else if (this->curchip != mychip->chip) {
  600. DoC_SelectChip(docptr, mychip->chip);
  601. }
  602. this->curfloor = mychip->floor;
  603. this->curchip = mychip->chip;
  604. /* Millennium Plus bus cycle sequence as per figure 2, section 2.4 */
  605. WriteDOC(DOC_FLASH_CE, docptr, Mplus_FlashSelect);
  606. /* Reset the chip, see Software Requirement 11.4 item 1. */
  607. DoC_Command(docptr, NAND_CMD_RESET, 0);
  608. DoC_WaitReady(docptr);
  609. /* Set device to appropriate plane of flash */
  610. fto = to;
  611. WriteDOC(DoC_GetDataOffset(mtd, &fto), docptr, Mplus_FlashCmd);
  612. /* On interleaved devices the flags for 2nd half 512 are before data */
  613. if (before)
  614. fto -= 2;
  615. /* issue the Serial Data In command to initial the Page Program process */
  616. DoC_Command(docptr, NAND_CMD_SEQIN, 0x00);
  617. DoC_Address(this, 3, fto, 0x00, 0x00);
  618. /* Disable the ECC engine */
  619. WriteDOC(DOC_ECC_RESET, docptr, Mplus_ECCConf);
  620. if (before) {
  621. /* Write the block status BLOCK_USED (0x5555) */
  622. WriteDOC(0x55, docptr, Mil_CDSN_IO);
  623. WriteDOC(0x55, docptr, Mil_CDSN_IO);
  624. }
  625. /* init the ECC engine, see Reed-Solomon EDC/ECC 11.1 .*/
  626. WriteDOC(DOC_ECC_EN | DOC_ECC_RW, docptr, Mplus_ECCConf);
  627. MemWriteDOC(docptr, (unsigned char *) buf, len);
  628. /* Write ECC data to flash, the ECC info is generated by
  629. the DiskOnChip ECC logic see Reed-Solomon EDC/ECC 11.1 */
  630. DoC_Delay(docptr, 3);
  631. /* Read the ECC data through the DiskOnChip ECC logic */
  632. for (i = 0; i < 6; i++)
  633. eccbuf[i] = ReadDOC(docptr, Mplus_ECCSyndrome0 + i);
  634. /* disable the ECC engine */
  635. WriteDOC(DOC_ECC_DIS, docptr, Mplus_ECCConf);
  636. /* Write the ECC data to flash */
  637. MemWriteDOC(docptr, eccbuf, 6);
  638. if (!before) {
  639. /* Write the block status BLOCK_USED (0x5555) */
  640. WriteDOC(0x55, docptr, Mil_CDSN_IO+6);
  641. WriteDOC(0x55, docptr, Mil_CDSN_IO+7);
  642. }
  643. #ifdef PSYCHO_DEBUG
  644. printk("OOB data at %lx is %2.2X %2.2X %2.2X %2.2X %2.2X %2.2X\n",
  645. (long) to, eccbuf[0], eccbuf[1], eccbuf[2], eccbuf[3],
  646. eccbuf[4], eccbuf[5]);
  647. #endif
  648. WriteDOC(0x00, docptr, Mplus_WritePipeTerm);
  649. WriteDOC(0x00, docptr, Mplus_WritePipeTerm);
  650. /* Commit the Page Program command and wait for ready
  651. see Software Requirement 11.4 item 1.*/
  652. DoC_Command(docptr, NAND_CMD_PAGEPROG, 0x00);
  653. DoC_WaitReady(docptr);
  654. /* Read the status of the flash device through CDSN IO register
  655. see Software Requirement 11.4 item 5.*/
  656. DoC_Command(docptr, NAND_CMD_STATUS, 0);
  657. dummy = ReadDOC(docptr, Mplus_ReadPipeInit);
  658. dummy = ReadDOC(docptr, Mplus_ReadPipeInit);
  659. DoC_Delay(docptr, 2);
  660. if ((dummy = ReadDOC(docptr, Mplus_LastDataRead)) & 1) {
  661. printk("MTD: Error 0x%x programming at 0x%x\n", dummy, (int)to);
  662. /* Error in programming
  663. FIXME: implement Bad Block Replacement (in nftl.c ??) */
  664. ret = -EIO;
  665. }
  666. dummy = ReadDOC(docptr, Mplus_LastDataRead);
  667. /* Disable flash internally */
  668. WriteDOC(0, docptr, Mplus_FlashSelect);
  669. /* Let the caller know we completed it */
  670. *retlen = len;
  671. return ret;
  672. }
  673. static int doc_read_oob(struct mtd_info *mtd, loff_t ofs,
  674. struct mtd_oob_ops *ops)
  675. {
  676. loff_t fofs, base;
  677. struct DiskOnChip *this = mtd->priv;
  678. void __iomem * docptr = this->virtadr;
  679. struct Nand *mychip = &this->chips[ofs >> this->chipshift];
  680. size_t i, size, got, want;
  681. uint8_t *buf = ops->oobbuf;
  682. size_t len = ops->len;
  683. BUG_ON(ops->mode != MTD_OPS_PLACE_OOB);
  684. ofs += ops->ooboffs;
  685. DoC_CheckASIC(docptr);
  686. /* Find the chip which is to be used and select it */
  687. if (this->curfloor != mychip->floor) {
  688. DoC_SelectFloor(docptr, mychip->floor);
  689. DoC_SelectChip(docptr, mychip->chip);
  690. } else if (this->curchip != mychip->chip) {
  691. DoC_SelectChip(docptr, mychip->chip);
  692. }
  693. this->curfloor = mychip->floor;
  694. this->curchip = mychip->chip;
  695. /* Millennium Plus bus cycle sequence as per figure 2, section 2.4 */
  696. WriteDOC((DOC_FLASH_CE | DOC_FLASH_WP), docptr, Mplus_FlashSelect);
  697. /* disable the ECC engine */
  698. WriteDOC(DOC_ECC_RESET, docptr, Mplus_ECCConf);
  699. DoC_WaitReady(docptr);
  700. /* Maximum of 16 bytes in the OOB region, so limit read to that */
  701. if (len > 16)
  702. len = 16;
  703. got = 0;
  704. want = len;
  705. for (i = 0; ((i < 3) && (want > 0)); i++) {
  706. /* Figure out which region we are accessing... */
  707. fofs = ofs;
  708. base = ofs & 0xf;
  709. if (!this->interleave) {
  710. DoC_Command(docptr, NAND_CMD_READOOB, 0);
  711. size = 16 - base;
  712. } else if (base < 6) {
  713. DoC_Command(docptr, DoC_GetECCOffset(mtd, &fofs), 0);
  714. size = 6 - base;
  715. } else if (base < 8) {
  716. DoC_Command(docptr, DoC_GetFlagsOffset(mtd, &fofs), 0);
  717. size = 8 - base;
  718. } else {
  719. DoC_Command(docptr, DoC_GetHdrOffset(mtd, &fofs), 0);
  720. size = 16 - base;
  721. }
  722. if (size > want)
  723. size = want;
  724. /* Issue read command */
  725. DoC_Address(this, 3, fofs, 0, 0x00);
  726. WriteDOC(0, docptr, Mplus_FlashControl);
  727. DoC_WaitReady(docptr);
  728. ReadDOC(docptr, Mplus_ReadPipeInit);
  729. ReadDOC(docptr, Mplus_ReadPipeInit);
  730. MemReadDOC(docptr, &buf[got], size - 2);
  731. buf[got + size - 2] = ReadDOC(docptr, Mplus_LastDataRead);
  732. buf[got + size - 1] = ReadDOC(docptr, Mplus_LastDataRead);
  733. ofs += size;
  734. got += size;
  735. want -= size;
  736. }
  737. /* Disable flash internally */
  738. WriteDOC(0, docptr, Mplus_FlashSelect);
  739. ops->retlen = len;
  740. return 0;
  741. }
  742. static int doc_write_oob(struct mtd_info *mtd, loff_t ofs,
  743. struct mtd_oob_ops *ops)
  744. {
  745. volatile char dummy;
  746. loff_t fofs, base;
  747. struct DiskOnChip *this = mtd->priv;
  748. void __iomem * docptr = this->virtadr;
  749. struct Nand *mychip = &this->chips[ofs >> this->chipshift];
  750. size_t i, size, got, want;
  751. int ret = 0;
  752. uint8_t *buf = ops->oobbuf;
  753. size_t len = ops->len;
  754. BUG_ON(ops->mode != MTD_OPS_PLACE_OOB);
  755. ofs += ops->ooboffs;
  756. DoC_CheckASIC(docptr);
  757. /* Find the chip which is to be used and select it */
  758. if (this->curfloor != mychip->floor) {
  759. DoC_SelectFloor(docptr, mychip->floor);
  760. DoC_SelectChip(docptr, mychip->chip);
  761. } else if (this->curchip != mychip->chip) {
  762. DoC_SelectChip(docptr, mychip->chip);
  763. }
  764. this->curfloor = mychip->floor;
  765. this->curchip = mychip->chip;
  766. /* Millennium Plus bus cycle sequence as per figure 2, section 2.4 */
  767. WriteDOC(DOC_FLASH_CE, docptr, Mplus_FlashSelect);
  768. /* Maximum of 16 bytes in the OOB region, so limit write to that */
  769. if (len > 16)
  770. len = 16;
  771. got = 0;
  772. want = len;
  773. for (i = 0; ((i < 3) && (want > 0)); i++) {
  774. /* Reset the chip, see Software Requirement 11.4 item 1. */
  775. DoC_Command(docptr, NAND_CMD_RESET, 0);
  776. DoC_WaitReady(docptr);
  777. /* Figure out which region we are accessing... */
  778. fofs = ofs;
  779. base = ofs & 0x0f;
  780. if (!this->interleave) {
  781. WriteDOC(NAND_CMD_READOOB, docptr, Mplus_FlashCmd);
  782. size = 16 - base;
  783. } else if (base < 6) {
  784. WriteDOC(DoC_GetECCOffset(mtd, &fofs), docptr, Mplus_FlashCmd);
  785. size = 6 - base;
  786. } else if (base < 8) {
  787. WriteDOC(DoC_GetFlagsOffset(mtd, &fofs), docptr, Mplus_FlashCmd);
  788. size = 8 - base;
  789. } else {
  790. WriteDOC(DoC_GetHdrOffset(mtd, &fofs), docptr, Mplus_FlashCmd);
  791. size = 16 - base;
  792. }
  793. if (size > want)
  794. size = want;
  795. /* Issue the Serial Data In command to initial the Page Program process */
  796. DoC_Command(docptr, NAND_CMD_SEQIN, 0x00);
  797. DoC_Address(this, 3, fofs, 0, 0x00);
  798. /* Disable the ECC engine */
  799. WriteDOC(DOC_ECC_RESET, docptr, Mplus_ECCConf);
  800. /* Write the data via the internal pipeline through CDSN IO
  801. register, see Pipelined Write Operations 11.2 */
  802. MemWriteDOC(docptr, (unsigned char *) &buf[got], size);
  803. WriteDOC(0x00, docptr, Mplus_WritePipeTerm);
  804. WriteDOC(0x00, docptr, Mplus_WritePipeTerm);
  805. /* Commit the Page Program command and wait for ready
  806. see Software Requirement 11.4 item 1.*/
  807. DoC_Command(docptr, NAND_CMD_PAGEPROG, 0x00);
  808. DoC_WaitReady(docptr);
  809. /* Read the status of the flash device through CDSN IO register
  810. see Software Requirement 11.4 item 5.*/
  811. DoC_Command(docptr, NAND_CMD_STATUS, 0x00);
  812. dummy = ReadDOC(docptr, Mplus_ReadPipeInit);
  813. dummy = ReadDOC(docptr, Mplus_ReadPipeInit);
  814. DoC_Delay(docptr, 2);
  815. if ((dummy = ReadDOC(docptr, Mplus_LastDataRead)) & 1) {
  816. printk("MTD: Error 0x%x programming oob at 0x%x\n",
  817. dummy, (int)ofs);
  818. /* FIXME: implement Bad Block Replacement */
  819. ops->retlen = 0;
  820. ret = -EIO;
  821. }
  822. dummy = ReadDOC(docptr, Mplus_LastDataRead);
  823. ofs += size;
  824. got += size;
  825. want -= size;
  826. }
  827. /* Disable flash internally */
  828. WriteDOC(0, docptr, Mplus_FlashSelect);
  829. ops->retlen = len;
  830. return ret;
  831. }
  832. int doc_erase(struct mtd_info *mtd, struct erase_info *instr)
  833. {
  834. volatile char dummy;
  835. struct DiskOnChip *this = mtd->priv;
  836. __u32 ofs = instr->addr;
  837. __u32 len = instr->len;
  838. void __iomem * docptr = this->virtadr;
  839. struct Nand *mychip = &this->chips[ofs >> this->chipshift];
  840. DoC_CheckASIC(docptr);
  841. if (len != mtd->erasesize)
  842. printk(KERN_WARNING "MTD: Erase not right size (%x != %x)n",
  843. len, mtd->erasesize);
  844. /* Find the chip which is to be used and select it */
  845. if (this->curfloor != mychip->floor) {
  846. DoC_SelectFloor(docptr, mychip->floor);
  847. DoC_SelectChip(docptr, mychip->chip);
  848. } else if (this->curchip != mychip->chip) {
  849. DoC_SelectChip(docptr, mychip->chip);
  850. }
  851. this->curfloor = mychip->floor;
  852. this->curchip = mychip->chip;
  853. instr->state = MTD_ERASE_PENDING;
  854. /* Millennium Plus bus cycle sequence as per figure 2, section 2.4 */
  855. WriteDOC(DOC_FLASH_CE, docptr, Mplus_FlashSelect);
  856. DoC_Command(docptr, NAND_CMD_RESET, 0x00);
  857. DoC_WaitReady(docptr);
  858. DoC_Command(docptr, NAND_CMD_ERASE1, 0);
  859. DoC_Address(this, 2, ofs, 0, 0x00);
  860. DoC_Command(docptr, NAND_CMD_ERASE2, 0);
  861. DoC_WaitReady(docptr);
  862. instr->state = MTD_ERASING;
  863. /* Read the status of the flash device through CDSN IO register
  864. see Software Requirement 11.4 item 5. */
  865. DoC_Command(docptr, NAND_CMD_STATUS, 0);
  866. dummy = ReadDOC(docptr, Mplus_ReadPipeInit);
  867. dummy = ReadDOC(docptr, Mplus_ReadPipeInit);
  868. if ((dummy = ReadDOC(docptr, Mplus_LastDataRead)) & 1) {
  869. printk("MTD: Error 0x%x erasing at 0x%x\n", dummy, ofs);
  870. /* FIXME: implement Bad Block Replacement (in nftl.c ??) */
  871. instr->state = MTD_ERASE_FAILED;
  872. } else {
  873. instr->state = MTD_ERASE_DONE;
  874. }
  875. dummy = ReadDOC(docptr, Mplus_LastDataRead);
  876. /* Disable flash internally */
  877. WriteDOC(0, docptr, Mplus_FlashSelect);
  878. mtd_erase_callback(instr);
  879. return 0;
  880. }
  881. /****************************************************************************
  882. *
  883. * Module stuff
  884. *
  885. ****************************************************************************/
  886. static void __exit cleanup_doc2001plus(void)
  887. {
  888. struct mtd_info *mtd;
  889. struct DiskOnChip *this;
  890. while ((mtd=docmilpluslist)) {
  891. this = mtd->priv;
  892. docmilpluslist = this->nextdoc;
  893. mtd_device_unregister(mtd);
  894. iounmap(this->virtadr);
  895. kfree(this->chips);
  896. kfree(mtd);
  897. }
  898. }
  899. module_exit(cleanup_doc2001plus);
  900. MODULE_LICENSE("GPL");
  901. MODULE_AUTHOR("Greg Ungerer <gerg@snapgear.com> et al.");
  902. MODULE_DESCRIPTION("Driver for DiskOnChip Millennium Plus");