doc2001plus.c 31 KB

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