doc2001plus.c 31 KB

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