doc2001plus.c 31 KB

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