doc2001plus.c 31 KB

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