doc2000.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177
  1. /*
  2. * Linux driver for Disk-On-Chip 2000 and Millennium
  3. * (c) 1999 Machine Vision Holdings, Inc.
  4. * (c) 1999, 2000 David Woodhouse <dwmw2@infradead.org>
  5. */
  6. #include <linux/kernel.h>
  7. #include <linux/module.h>
  8. #include <asm/errno.h>
  9. #include <asm/io.h>
  10. #include <asm/uaccess.h>
  11. #include <linux/delay.h>
  12. #include <linux/slab.h>
  13. #include <linux/sched.h>
  14. #include <linux/init.h>
  15. #include <linux/types.h>
  16. #include <linux/bitops.h>
  17. #include <linux/mutex.h>
  18. #include <linux/mtd/mtd.h>
  19. #include <linux/mtd/nand.h>
  20. #include <linux/mtd/doc2000.h>
  21. #define DOC_SUPPORT_2000
  22. #define DOC_SUPPORT_2000TSOP
  23. #define DOC_SUPPORT_MILLENNIUM
  24. #ifdef DOC_SUPPORT_2000
  25. #define DoC_is_2000(doc) (doc->ChipID == DOC_ChipID_Doc2k)
  26. #else
  27. #define DoC_is_2000(doc) (0)
  28. #endif
  29. #if defined(DOC_SUPPORT_2000TSOP) || defined(DOC_SUPPORT_MILLENNIUM)
  30. #define DoC_is_Millennium(doc) (doc->ChipID == DOC_ChipID_DocMil)
  31. #else
  32. #define DoC_is_Millennium(doc) (0)
  33. #endif
  34. /* #define ECC_DEBUG */
  35. /* I have no idea why some DoC chips can not use memcpy_from|to_io().
  36. * This may be due to the different revisions of the ASIC controller built-in or
  37. * simplily a QA/Bug issue. Who knows ?? If you have trouble, please uncomment
  38. * this:
  39. #undef USE_MEMCPY
  40. */
  41. static int doc_read(struct mtd_info *mtd, loff_t from, size_t len,
  42. size_t *retlen, u_char *buf);
  43. static int doc_write(struct mtd_info *mtd, loff_t to, size_t len,
  44. size_t *retlen, const u_char *buf);
  45. static int doc_read_oob(struct mtd_info *mtd, loff_t ofs,
  46. struct mtd_oob_ops *ops);
  47. static int doc_write_oob(struct mtd_info *mtd, loff_t ofs,
  48. struct mtd_oob_ops *ops);
  49. static int doc_write_oob_nolock(struct mtd_info *mtd, loff_t ofs, size_t len,
  50. size_t *retlen, const u_char *buf);
  51. static int doc_erase (struct mtd_info *mtd, struct erase_info *instr);
  52. static struct mtd_info *doc2klist = NULL;
  53. /* Perform the required delay cycles by reading from the appropriate register */
  54. static void DoC_Delay(struct DiskOnChip *doc, unsigned short cycles)
  55. {
  56. volatile char dummy;
  57. int i;
  58. for (i = 0; i < cycles; i++) {
  59. if (DoC_is_Millennium(doc))
  60. dummy = ReadDOC(doc->virtadr, NOP);
  61. else
  62. dummy = ReadDOC(doc->virtadr, DOCStatus);
  63. }
  64. }
  65. /* DOC_WaitReady: Wait for RDY line to be asserted by the flash chip */
  66. static int _DoC_WaitReady(struct DiskOnChip *doc)
  67. {
  68. void __iomem *docptr = doc->virtadr;
  69. unsigned long timeo = jiffies + (HZ * 10);
  70. pr_debug("_DoC_WaitReady called for out-of-line wait\n");
  71. /* Out-of-line routine to wait for chip response */
  72. while (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B)) {
  73. /* issue 2 read from NOP register after reading from CDSNControl register
  74. see Software Requirement 11.4 item 2. */
  75. DoC_Delay(doc, 2);
  76. if (time_after(jiffies, timeo)) {
  77. pr_debug("_DoC_WaitReady timed out.\n");
  78. return -EIO;
  79. }
  80. udelay(1);
  81. cond_resched();
  82. }
  83. return 0;
  84. }
  85. static inline int DoC_WaitReady(struct DiskOnChip *doc)
  86. {
  87. void __iomem *docptr = doc->virtadr;
  88. /* This is inline, to optimise the common case, where it's ready instantly */
  89. int ret = 0;
  90. /* 4 read form NOP register should be issued in prior to the read from CDSNControl
  91. see Software Requirement 11.4 item 2. */
  92. DoC_Delay(doc, 4);
  93. if (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B))
  94. /* Call the out-of-line routine to wait */
  95. ret = _DoC_WaitReady(doc);
  96. /* issue 2 read from NOP register after reading from CDSNControl register
  97. see Software Requirement 11.4 item 2. */
  98. DoC_Delay(doc, 2);
  99. return ret;
  100. }
  101. /* DoC_Command: Send a flash command to the flash chip through the CDSN Slow IO register to
  102. bypass the internal pipeline. Each of 4 delay cycles (read from the NOP register) is
  103. required after writing to CDSN Control register, see Software Requirement 11.4 item 3. */
  104. static int DoC_Command(struct DiskOnChip *doc, unsigned char command,
  105. unsigned char xtraflags)
  106. {
  107. void __iomem *docptr = doc->virtadr;
  108. if (DoC_is_2000(doc))
  109. xtraflags |= CDSN_CTRL_FLASH_IO;
  110. /* Assert the CLE (Command Latch Enable) line to the flash chip */
  111. WriteDOC(xtraflags | CDSN_CTRL_CLE | CDSN_CTRL_CE, docptr, CDSNControl);
  112. DoC_Delay(doc, 4); /* Software requirement 11.4.3 for Millennium */
  113. if (DoC_is_Millennium(doc))
  114. WriteDOC(command, docptr, CDSNSlowIO);
  115. /* Send the command */
  116. WriteDOC_(command, docptr, doc->ioreg);
  117. if (DoC_is_Millennium(doc))
  118. WriteDOC(command, docptr, WritePipeTerm);
  119. /* Lower the CLE line */
  120. WriteDOC(xtraflags | CDSN_CTRL_CE, docptr, CDSNControl);
  121. DoC_Delay(doc, 4); /* Software requirement 11.4.3 for Millennium */
  122. /* Wait for the chip to respond - Software requirement 11.4.1 (extended for any command) */
  123. return DoC_WaitReady(doc);
  124. }
  125. /* DoC_Address: Set the current address for the flash chip through the CDSN Slow IO register to
  126. bypass the internal pipeline. Each of 4 delay cycles (read from the NOP register) is
  127. required after writing to CDSN Control register, see Software Requirement 11.4 item 3. */
  128. static int DoC_Address(struct DiskOnChip *doc, int numbytes, unsigned long ofs,
  129. unsigned char xtraflags1, unsigned char xtraflags2)
  130. {
  131. int i;
  132. void __iomem *docptr = doc->virtadr;
  133. if (DoC_is_2000(doc))
  134. xtraflags1 |= CDSN_CTRL_FLASH_IO;
  135. /* Assert the ALE (Address Latch Enable) line to the flash chip */
  136. WriteDOC(xtraflags1 | CDSN_CTRL_ALE | CDSN_CTRL_CE, docptr, CDSNControl);
  137. DoC_Delay(doc, 4); /* Software requirement 11.4.3 for Millennium */
  138. /* Send the address */
  139. /* Devices with 256-byte page are addressed as:
  140. Column (bits 0-7), Page (bits 8-15, 16-23, 24-31)
  141. * there is no device on the market with page256
  142. and more than 24 bits.
  143. Devices with 512-byte page are addressed as:
  144. Column (bits 0-7), Page (bits 9-16, 17-24, 25-31)
  145. * 25-31 is sent only if the chip support it.
  146. * bit 8 changes the read command to be sent
  147. (NAND_CMD_READ0 or NAND_CMD_READ1).
  148. */
  149. if (numbytes == ADDR_COLUMN || numbytes == ADDR_COLUMN_PAGE) {
  150. if (DoC_is_Millennium(doc))
  151. WriteDOC(ofs & 0xff, docptr, CDSNSlowIO);
  152. WriteDOC_(ofs & 0xff, docptr, doc->ioreg);
  153. }
  154. if (doc->page256) {
  155. ofs = ofs >> 8;
  156. } else {
  157. ofs = ofs >> 9;
  158. }
  159. if (numbytes == ADDR_PAGE || numbytes == ADDR_COLUMN_PAGE) {
  160. for (i = 0; i < doc->pageadrlen; i++, ofs = ofs >> 8) {
  161. if (DoC_is_Millennium(doc))
  162. WriteDOC(ofs & 0xff, docptr, CDSNSlowIO);
  163. WriteDOC_(ofs & 0xff, docptr, doc->ioreg);
  164. }
  165. }
  166. if (DoC_is_Millennium(doc))
  167. WriteDOC(ofs & 0xff, docptr, WritePipeTerm);
  168. DoC_Delay(doc, 2); /* Needed for some slow flash chips. mf. */
  169. /* FIXME: The SlowIO's for millennium could be replaced by
  170. a single WritePipeTerm here. mf. */
  171. /* Lower the ALE line */
  172. WriteDOC(xtraflags1 | xtraflags2 | CDSN_CTRL_CE, docptr,
  173. CDSNControl);
  174. DoC_Delay(doc, 4); /* Software requirement 11.4.3 for Millennium */
  175. /* Wait for the chip to respond - Software requirement 11.4.1 */
  176. return DoC_WaitReady(doc);
  177. }
  178. /* Read a buffer from DoC, taking care of Millennium odditys */
  179. static void DoC_ReadBuf(struct DiskOnChip *doc, u_char * buf, int len)
  180. {
  181. volatile int dummy;
  182. int modulus = 0xffff;
  183. void __iomem *docptr = doc->virtadr;
  184. int i;
  185. if (len <= 0)
  186. return;
  187. if (DoC_is_Millennium(doc)) {
  188. /* Read the data via the internal pipeline through CDSN IO register,
  189. see Pipelined Read Operations 11.3 */
  190. dummy = ReadDOC(docptr, ReadPipeInit);
  191. /* Millennium should use the LastDataRead register - Pipeline Reads */
  192. len--;
  193. /* This is needed for correctly ECC calculation */
  194. modulus = 0xff;
  195. }
  196. for (i = 0; i < len; i++)
  197. buf[i] = ReadDOC_(docptr, doc->ioreg + (i & modulus));
  198. if (DoC_is_Millennium(doc)) {
  199. buf[i] = ReadDOC(docptr, LastDataRead);
  200. }
  201. }
  202. /* Write a buffer to DoC, taking care of Millennium odditys */
  203. static void DoC_WriteBuf(struct DiskOnChip *doc, const u_char * buf, int len)
  204. {
  205. void __iomem *docptr = doc->virtadr;
  206. int i;
  207. if (len <= 0)
  208. return;
  209. for (i = 0; i < len; i++)
  210. WriteDOC_(buf[i], docptr, doc->ioreg + i);
  211. if (DoC_is_Millennium(doc)) {
  212. WriteDOC(0x00, docptr, WritePipeTerm);
  213. }
  214. }
  215. /* DoC_SelectChip: Select a given flash chip within the current floor */
  216. static inline int DoC_SelectChip(struct DiskOnChip *doc, int chip)
  217. {
  218. void __iomem *docptr = doc->virtadr;
  219. /* Software requirement 11.4.4 before writing DeviceSelect */
  220. /* Deassert the CE line to eliminate glitches on the FCE# outputs */
  221. WriteDOC(CDSN_CTRL_WP, docptr, CDSNControl);
  222. DoC_Delay(doc, 4); /* Software requirement 11.4.3 for Millennium */
  223. /* Select the individual flash chip requested */
  224. WriteDOC(chip, docptr, CDSNDeviceSelect);
  225. DoC_Delay(doc, 4);
  226. /* Reassert the CE line */
  227. WriteDOC(CDSN_CTRL_CE | CDSN_CTRL_FLASH_IO | CDSN_CTRL_WP, docptr,
  228. CDSNControl);
  229. DoC_Delay(doc, 4); /* Software requirement 11.4.3 for Millennium */
  230. /* Wait for it to be ready */
  231. return DoC_WaitReady(doc);
  232. }
  233. /* DoC_SelectFloor: Select a given floor (bank of flash chips) */
  234. static inline int DoC_SelectFloor(struct DiskOnChip *doc, int floor)
  235. {
  236. void __iomem *docptr = doc->virtadr;
  237. /* Select the floor (bank) of chips required */
  238. WriteDOC(floor, docptr, FloorSelect);
  239. /* Wait for the chip to be ready */
  240. return DoC_WaitReady(doc);
  241. }
  242. /* DoC_IdentChip: Identify a given NAND chip given {floor,chip} */
  243. static int DoC_IdentChip(struct DiskOnChip *doc, int floor, int chip)
  244. {
  245. int mfr, id, i, j;
  246. volatile char dummy;
  247. /* Page in the required floor/chip */
  248. DoC_SelectFloor(doc, floor);
  249. DoC_SelectChip(doc, chip);
  250. /* Reset the chip */
  251. if (DoC_Command(doc, NAND_CMD_RESET, CDSN_CTRL_WP)) {
  252. pr_debug("DoC_Command (reset) for %d,%d returned true\n",
  253. floor, chip);
  254. return 0;
  255. }
  256. /* Read the NAND chip ID: 1. Send ReadID command */
  257. if (DoC_Command(doc, NAND_CMD_READID, CDSN_CTRL_WP)) {
  258. pr_debug("DoC_Command (ReadID) for %d,%d returned true\n",
  259. floor, chip);
  260. return 0;
  261. }
  262. /* Read the NAND chip ID: 2. Send address byte zero */
  263. DoC_Address(doc, ADDR_COLUMN, 0, CDSN_CTRL_WP, 0);
  264. /* Read the manufacturer and device id codes from the device */
  265. if (DoC_is_Millennium(doc)) {
  266. DoC_Delay(doc, 2);
  267. dummy = ReadDOC(doc->virtadr, ReadPipeInit);
  268. mfr = ReadDOC(doc->virtadr, LastDataRead);
  269. DoC_Delay(doc, 2);
  270. dummy = ReadDOC(doc->virtadr, ReadPipeInit);
  271. id = ReadDOC(doc->virtadr, LastDataRead);
  272. } else {
  273. /* CDSN Slow IO register see Software Req 11.4 item 5. */
  274. dummy = ReadDOC(doc->virtadr, CDSNSlowIO);
  275. DoC_Delay(doc, 2);
  276. mfr = ReadDOC_(doc->virtadr, doc->ioreg);
  277. /* CDSN Slow IO register see Software Req 11.4 item 5. */
  278. dummy = ReadDOC(doc->virtadr, CDSNSlowIO);
  279. DoC_Delay(doc, 2);
  280. id = ReadDOC_(doc->virtadr, doc->ioreg);
  281. }
  282. /* No response - return failure */
  283. if (mfr == 0xff || mfr == 0)
  284. return 0;
  285. /* Check it's the same as the first chip we identified.
  286. * M-Systems say that any given DiskOnChip device should only
  287. * contain _one_ type of flash part, although that's not a
  288. * hardware restriction. */
  289. if (doc->mfr) {
  290. if (doc->mfr == mfr && doc->id == id)
  291. return 1; /* This is the same as the first */
  292. else
  293. printk(KERN_WARNING
  294. "Flash chip at floor %d, chip %d is different:\n",
  295. floor, chip);
  296. }
  297. /* Print and store the manufacturer and ID codes. */
  298. for (i = 0; nand_flash_ids[i].name != NULL; i++) {
  299. if (id == nand_flash_ids[i].id) {
  300. /* Try to identify manufacturer */
  301. for (j = 0; nand_manuf_ids[j].id != 0x0; j++) {
  302. if (nand_manuf_ids[j].id == mfr)
  303. break;
  304. }
  305. printk(KERN_INFO
  306. "Flash chip found: Manufacturer ID: %2.2X, "
  307. "Chip ID: %2.2X (%s:%s)\n", mfr, id,
  308. nand_manuf_ids[j].name, nand_flash_ids[i].name);
  309. if (!doc->mfr) {
  310. doc->mfr = mfr;
  311. doc->id = id;
  312. doc->chipshift =
  313. ffs((nand_flash_ids[i].chipsize << 20)) - 1;
  314. doc->page256 = (nand_flash_ids[i].pagesize == 256) ? 1 : 0;
  315. doc->pageadrlen = doc->chipshift > 25 ? 3 : 2;
  316. doc->erasesize =
  317. nand_flash_ids[i].erasesize;
  318. return 1;
  319. }
  320. return 0;
  321. }
  322. }
  323. /* We haven't fully identified the chip. Print as much as we know. */
  324. printk(KERN_WARNING "Unknown flash chip found: %2.2X %2.2X\n",
  325. id, mfr);
  326. printk(KERN_WARNING "Please report to dwmw2@infradead.org\n");
  327. return 0;
  328. }
  329. /* DoC_ScanChips: Find all NAND chips present in a DiskOnChip, and identify them */
  330. static void DoC_ScanChips(struct DiskOnChip *this, int maxchips)
  331. {
  332. int floor, chip;
  333. int numchips[MAX_FLOORS];
  334. int ret = 1;
  335. this->numchips = 0;
  336. this->mfr = 0;
  337. this->id = 0;
  338. /* For each floor, find the number of valid chips it contains */
  339. for (floor = 0; floor < MAX_FLOORS; floor++) {
  340. ret = 1;
  341. numchips[floor] = 0;
  342. for (chip = 0; chip < maxchips && ret != 0; chip++) {
  343. ret = DoC_IdentChip(this, floor, chip);
  344. if (ret) {
  345. numchips[floor]++;
  346. this->numchips++;
  347. }
  348. }
  349. }
  350. /* If there are none at all that we recognise, bail */
  351. if (!this->numchips) {
  352. printk(KERN_NOTICE "No flash chips recognised.\n");
  353. return;
  354. }
  355. /* Allocate an array to hold the information for each chip */
  356. this->chips = kmalloc(sizeof(struct Nand) * this->numchips, GFP_KERNEL);
  357. if (!this->chips) {
  358. printk(KERN_NOTICE "No memory for allocating chip info structures\n");
  359. return;
  360. }
  361. ret = 0;
  362. /* Fill out the chip array with {floor, chipno} for each
  363. * detected chip in the device. */
  364. for (floor = 0; floor < MAX_FLOORS; floor++) {
  365. for (chip = 0; chip < numchips[floor]; chip++) {
  366. this->chips[ret].floor = floor;
  367. this->chips[ret].chip = chip;
  368. this->chips[ret].curadr = 0;
  369. this->chips[ret].curmode = 0x50;
  370. ret++;
  371. }
  372. }
  373. /* Calculate and print the total size of the device */
  374. this->totlen = this->numchips * (1 << this->chipshift);
  375. printk(KERN_INFO "%d flash chips found. Total DiskOnChip size: %ld MiB\n",
  376. this->numchips, this->totlen >> 20);
  377. }
  378. static int DoC2k_is_alias(struct DiskOnChip *doc1, struct DiskOnChip *doc2)
  379. {
  380. int tmp1, tmp2, retval;
  381. if (doc1->physadr == doc2->physadr)
  382. return 1;
  383. /* Use the alias resolution register which was set aside for this
  384. * purpose. If it's value is the same on both chips, they might
  385. * be the same chip, and we write to one and check for a change in
  386. * the other. It's unclear if this register is usuable in the
  387. * DoC 2000 (it's in the Millennium docs), but it seems to work. */
  388. tmp1 = ReadDOC(doc1->virtadr, AliasResolution);
  389. tmp2 = ReadDOC(doc2->virtadr, AliasResolution);
  390. if (tmp1 != tmp2)
  391. return 0;
  392. WriteDOC((tmp1 + 1) % 0xff, doc1->virtadr, AliasResolution);
  393. tmp2 = ReadDOC(doc2->virtadr, AliasResolution);
  394. if (tmp2 == (tmp1 + 1) % 0xff)
  395. retval = 1;
  396. else
  397. retval = 0;
  398. /* Restore register contents. May not be necessary, but do it just to
  399. * be safe. */
  400. WriteDOC(tmp1, doc1->virtadr, AliasResolution);
  401. return retval;
  402. }
  403. /* This routine is found from the docprobe code by symbol_get(),
  404. * which will bump the use count of this module. */
  405. void DoC2k_init(struct mtd_info *mtd)
  406. {
  407. struct DiskOnChip *this = mtd->priv;
  408. struct DiskOnChip *old = NULL;
  409. int maxchips;
  410. /* We must avoid being called twice for the same device. */
  411. if (doc2klist)
  412. old = doc2klist->priv;
  413. while (old) {
  414. if (DoC2k_is_alias(old, this)) {
  415. printk(KERN_NOTICE
  416. "Ignoring DiskOnChip 2000 at 0x%lX - already configured\n",
  417. this->physadr);
  418. iounmap(this->virtadr);
  419. kfree(mtd);
  420. return;
  421. }
  422. if (old->nextdoc)
  423. old = old->nextdoc->priv;
  424. else
  425. old = NULL;
  426. }
  427. switch (this->ChipID) {
  428. case DOC_ChipID_Doc2kTSOP:
  429. mtd->name = "DiskOnChip 2000 TSOP";
  430. this->ioreg = DoC_Mil_CDSN_IO;
  431. /* Pretend it's a Millennium */
  432. this->ChipID = DOC_ChipID_DocMil;
  433. maxchips = MAX_CHIPS;
  434. break;
  435. case DOC_ChipID_Doc2k:
  436. mtd->name = "DiskOnChip 2000";
  437. this->ioreg = DoC_2k_CDSN_IO;
  438. maxchips = MAX_CHIPS;
  439. break;
  440. case DOC_ChipID_DocMil:
  441. mtd->name = "DiskOnChip Millennium";
  442. this->ioreg = DoC_Mil_CDSN_IO;
  443. maxchips = MAX_CHIPS_MIL;
  444. break;
  445. default:
  446. printk("Unknown ChipID 0x%02x\n", this->ChipID);
  447. kfree(mtd);
  448. iounmap(this->virtadr);
  449. return;
  450. }
  451. printk(KERN_NOTICE "%s found at address 0x%lX\n", mtd->name,
  452. this->physadr);
  453. mtd->type = MTD_NANDFLASH;
  454. mtd->flags = MTD_CAP_NANDFLASH;
  455. mtd->writebufsize = mtd->writesize = 512;
  456. mtd->oobsize = 16;
  457. mtd->owner = THIS_MODULE;
  458. mtd->_erase = doc_erase;
  459. mtd->_read = doc_read;
  460. mtd->_write = doc_write;
  461. mtd->_read_oob = doc_read_oob;
  462. mtd->_write_oob = doc_write_oob;
  463. this->curfloor = -1;
  464. this->curchip = -1;
  465. mutex_init(&this->lock);
  466. /* Ident all the chips present. */
  467. DoC_ScanChips(this, maxchips);
  468. if (!this->totlen) {
  469. kfree(mtd);
  470. iounmap(this->virtadr);
  471. } else {
  472. this->nextdoc = doc2klist;
  473. doc2klist = mtd;
  474. mtd->size = this->totlen;
  475. mtd->erasesize = this->erasesize;
  476. mtd_device_register(mtd, NULL, 0);
  477. return;
  478. }
  479. }
  480. EXPORT_SYMBOL_GPL(DoC2k_init);
  481. static int doc_read(struct mtd_info *mtd, loff_t from, size_t len,
  482. size_t * retlen, u_char * buf)
  483. {
  484. struct DiskOnChip *this = mtd->priv;
  485. void __iomem *docptr = this->virtadr;
  486. struct Nand *mychip;
  487. unsigned char syndrome[6], eccbuf[6];
  488. volatile char dummy;
  489. int i, len256 = 0, ret=0;
  490. size_t left = len;
  491. mutex_lock(&this->lock);
  492. while (left) {
  493. len = left;
  494. /* Don't allow a single read to cross a 512-byte block boundary */
  495. if (from + len > ((from | 0x1ff) + 1))
  496. len = ((from | 0x1ff) + 1) - from;
  497. /* The ECC will not be calculated correctly if less than 512 is read */
  498. if (len != 0x200)
  499. printk(KERN_WARNING
  500. "ECC needs a full sector read (adr: %lx size %lx)\n",
  501. (long) from, (long) len);
  502. /* printk("DoC_Read (adr: %lx size %lx)\n", (long) from, (long) len); */
  503. /* Find the chip which is to be used and select it */
  504. mychip = &this->chips[from >> (this->chipshift)];
  505. if (this->curfloor != mychip->floor) {
  506. DoC_SelectFloor(this, mychip->floor);
  507. DoC_SelectChip(this, mychip->chip);
  508. } else if (this->curchip != mychip->chip) {
  509. DoC_SelectChip(this, mychip->chip);
  510. }
  511. this->curfloor = mychip->floor;
  512. this->curchip = mychip->chip;
  513. DoC_Command(this,
  514. (!this->page256
  515. && (from & 0x100)) ? NAND_CMD_READ1 : NAND_CMD_READ0,
  516. CDSN_CTRL_WP);
  517. DoC_Address(this, ADDR_COLUMN_PAGE, from, CDSN_CTRL_WP,
  518. CDSN_CTRL_ECC_IO);
  519. /* Prime the ECC engine */
  520. WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
  521. WriteDOC(DOC_ECC_EN, docptr, ECCConf);
  522. /* treat crossing 256-byte sector for 2M x 8bits devices */
  523. if (this->page256 && from + len > (from | 0xff) + 1) {
  524. len256 = (from | 0xff) + 1 - from;
  525. DoC_ReadBuf(this, buf, len256);
  526. DoC_Command(this, NAND_CMD_READ0, CDSN_CTRL_WP);
  527. DoC_Address(this, ADDR_COLUMN_PAGE, from + len256,
  528. CDSN_CTRL_WP, CDSN_CTRL_ECC_IO);
  529. }
  530. DoC_ReadBuf(this, &buf[len256], len - len256);
  531. /* Let the caller know we completed it */
  532. *retlen += len;
  533. /* Read the ECC data through the DiskOnChip ECC logic */
  534. /* Note: this will work even with 2M x 8bit devices as */
  535. /* they have 8 bytes of OOB per 256 page. mf. */
  536. DoC_ReadBuf(this, eccbuf, 6);
  537. /* Flush the pipeline */
  538. if (DoC_is_Millennium(this)) {
  539. dummy = ReadDOC(docptr, ECCConf);
  540. dummy = ReadDOC(docptr, ECCConf);
  541. i = ReadDOC(docptr, ECCConf);
  542. } else {
  543. dummy = ReadDOC(docptr, 2k_ECCStatus);
  544. dummy = ReadDOC(docptr, 2k_ECCStatus);
  545. i = ReadDOC(docptr, 2k_ECCStatus);
  546. }
  547. /* Check the ECC Status */
  548. if (i & 0x80) {
  549. int nb_errors;
  550. /* There was an ECC error */
  551. #ifdef ECC_DEBUG
  552. printk(KERN_ERR "DiskOnChip ECC Error: Read at %lx\n", (long)from);
  553. #endif
  554. /* Read the ECC syndrome through the DiskOnChip ECC
  555. logic. These syndrome will be all ZERO when there
  556. is no error */
  557. for (i = 0; i < 6; i++) {
  558. syndrome[i] =
  559. ReadDOC(docptr, ECCSyndrome0 + i);
  560. }
  561. nb_errors = doc_decode_ecc(buf, syndrome);
  562. #ifdef ECC_DEBUG
  563. printk(KERN_ERR "Errors corrected: %x\n", nb_errors);
  564. #endif
  565. if (nb_errors < 0) {
  566. /* We return error, but have actually done the
  567. read. Not that this can be told to
  568. user-space, via sys_read(), but at least
  569. MTD-aware stuff can know about it by
  570. checking *retlen */
  571. ret = -EIO;
  572. }
  573. }
  574. #ifdef PSYCHO_DEBUG
  575. printk(KERN_DEBUG "ECC DATA at %lxB: %2.2X %2.2X %2.2X %2.2X %2.2X %2.2X\n",
  576. (long)from, eccbuf[0], eccbuf[1], eccbuf[2],
  577. eccbuf[3], eccbuf[4], eccbuf[5]);
  578. #endif
  579. /* disable the ECC engine */
  580. WriteDOC(DOC_ECC_DIS, docptr , ECCConf);
  581. /* according to 11.4.1, we need to wait for the busy line
  582. * drop if we read to the end of the page. */
  583. if(0 == ((from + len) & 0x1ff))
  584. {
  585. DoC_WaitReady(this);
  586. }
  587. from += len;
  588. left -= len;
  589. buf += len;
  590. }
  591. mutex_unlock(&this->lock);
  592. return ret;
  593. }
  594. static int doc_write(struct mtd_info *mtd, loff_t to, size_t len,
  595. size_t * retlen, const u_char * buf)
  596. {
  597. struct DiskOnChip *this = mtd->priv;
  598. int di; /* Yes, DI is a hangover from when I was disassembling the binary driver */
  599. void __iomem *docptr = this->virtadr;
  600. unsigned char eccbuf[6];
  601. volatile char dummy;
  602. int len256 = 0;
  603. struct Nand *mychip;
  604. size_t left = len;
  605. int status;
  606. mutex_lock(&this->lock);
  607. while (left) {
  608. len = left;
  609. /* Don't allow a single write to cross a 512-byte block boundary */
  610. if (to + len > ((to | 0x1ff) + 1))
  611. len = ((to | 0x1ff) + 1) - to;
  612. /* The ECC will not be calculated correctly if less than 512 is written */
  613. /* DBB-
  614. if (len != 0x200 && eccbuf)
  615. printk(KERN_WARNING
  616. "ECC needs a full sector write (adr: %lx size %lx)\n",
  617. (long) to, (long) len);
  618. -DBB */
  619. /* printk("DoC_Write (adr: %lx size %lx)\n", (long) to, (long) len); */
  620. /* Find the chip which is to be used and select it */
  621. mychip = &this->chips[to >> (this->chipshift)];
  622. if (this->curfloor != mychip->floor) {
  623. DoC_SelectFloor(this, mychip->floor);
  624. DoC_SelectChip(this, mychip->chip);
  625. } else if (this->curchip != mychip->chip) {
  626. DoC_SelectChip(this, mychip->chip);
  627. }
  628. this->curfloor = mychip->floor;
  629. this->curchip = mychip->chip;
  630. /* Set device to main plane of flash */
  631. DoC_Command(this, NAND_CMD_RESET, CDSN_CTRL_WP);
  632. DoC_Command(this,
  633. (!this->page256
  634. && (to & 0x100)) ? NAND_CMD_READ1 : NAND_CMD_READ0,
  635. CDSN_CTRL_WP);
  636. DoC_Command(this, NAND_CMD_SEQIN, 0);
  637. DoC_Address(this, ADDR_COLUMN_PAGE, to, 0, CDSN_CTRL_ECC_IO);
  638. /* Prime the ECC engine */
  639. WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
  640. WriteDOC(DOC_ECC_EN | DOC_ECC_RW, docptr, ECCConf);
  641. /* treat crossing 256-byte sector for 2M x 8bits devices */
  642. if (this->page256 && to + len > (to | 0xff) + 1) {
  643. len256 = (to | 0xff) + 1 - to;
  644. DoC_WriteBuf(this, buf, len256);
  645. DoC_Command(this, NAND_CMD_PAGEPROG, 0);
  646. DoC_Command(this, NAND_CMD_STATUS, CDSN_CTRL_WP);
  647. /* There's an implicit DoC_WaitReady() in DoC_Command */
  648. dummy = ReadDOC(docptr, CDSNSlowIO);
  649. DoC_Delay(this, 2);
  650. if (ReadDOC_(docptr, this->ioreg) & 1) {
  651. printk(KERN_ERR "Error programming flash\n");
  652. /* Error in programming */
  653. *retlen = 0;
  654. mutex_unlock(&this->lock);
  655. return -EIO;
  656. }
  657. DoC_Command(this, NAND_CMD_SEQIN, 0);
  658. DoC_Address(this, ADDR_COLUMN_PAGE, to + len256, 0,
  659. CDSN_CTRL_ECC_IO);
  660. }
  661. DoC_WriteBuf(this, &buf[len256], len - len256);
  662. WriteDOC(CDSN_CTRL_ECC_IO | CDSN_CTRL_CE, docptr, CDSNControl);
  663. if (DoC_is_Millennium(this)) {
  664. WriteDOC(0, docptr, NOP);
  665. WriteDOC(0, docptr, NOP);
  666. WriteDOC(0, docptr, NOP);
  667. } else {
  668. WriteDOC_(0, docptr, this->ioreg);
  669. WriteDOC_(0, docptr, this->ioreg);
  670. WriteDOC_(0, docptr, this->ioreg);
  671. }
  672. WriteDOC(CDSN_CTRL_ECC_IO | CDSN_CTRL_FLASH_IO | CDSN_CTRL_CE, docptr,
  673. CDSNControl);
  674. /* Read the ECC data through the DiskOnChip ECC logic */
  675. for (di = 0; di < 6; di++) {
  676. eccbuf[di] = ReadDOC(docptr, ECCSyndrome0 + di);
  677. }
  678. /* Reset the ECC engine */
  679. WriteDOC(DOC_ECC_DIS, docptr, ECCConf);
  680. #ifdef PSYCHO_DEBUG
  681. printk
  682. ("OOB data at %lx is %2.2X %2.2X %2.2X %2.2X %2.2X %2.2X\n",
  683. (long) to, eccbuf[0], eccbuf[1], eccbuf[2], eccbuf[3],
  684. eccbuf[4], eccbuf[5]);
  685. #endif
  686. DoC_Command(this, NAND_CMD_PAGEPROG, 0);
  687. DoC_Command(this, NAND_CMD_STATUS, CDSN_CTRL_WP);
  688. /* There's an implicit DoC_WaitReady() in DoC_Command */
  689. if (DoC_is_Millennium(this)) {
  690. ReadDOC(docptr, ReadPipeInit);
  691. status = ReadDOC(docptr, LastDataRead);
  692. } else {
  693. dummy = ReadDOC(docptr, CDSNSlowIO);
  694. DoC_Delay(this, 2);
  695. status = ReadDOC_(docptr, this->ioreg);
  696. }
  697. if (status & 1) {
  698. printk(KERN_ERR "Error programming flash\n");
  699. /* Error in programming */
  700. *retlen = 0;
  701. mutex_unlock(&this->lock);
  702. return -EIO;
  703. }
  704. /* Let the caller know we completed it */
  705. *retlen += len;
  706. {
  707. unsigned char x[8];
  708. size_t dummy;
  709. int ret;
  710. /* Write the ECC data to flash */
  711. for (di=0; di<6; di++)
  712. x[di] = eccbuf[di];
  713. x[6]=0x55;
  714. x[7]=0x55;
  715. ret = doc_write_oob_nolock(mtd, to, 8, &dummy, x);
  716. if (ret) {
  717. mutex_unlock(&this->lock);
  718. return ret;
  719. }
  720. }
  721. to += len;
  722. left -= len;
  723. buf += len;
  724. }
  725. mutex_unlock(&this->lock);
  726. return 0;
  727. }
  728. static int doc_read_oob(struct mtd_info *mtd, loff_t ofs,
  729. struct mtd_oob_ops *ops)
  730. {
  731. struct DiskOnChip *this = mtd->priv;
  732. int len256 = 0, ret;
  733. struct Nand *mychip;
  734. uint8_t *buf = ops->oobbuf;
  735. size_t len = ops->len;
  736. BUG_ON(ops->mode != MTD_OPS_PLACE_OOB);
  737. ofs += ops->ooboffs;
  738. mutex_lock(&this->lock);
  739. mychip = &this->chips[ofs >> this->chipshift];
  740. if (this->curfloor != mychip->floor) {
  741. DoC_SelectFloor(this, mychip->floor);
  742. DoC_SelectChip(this, mychip->chip);
  743. } else if (this->curchip != mychip->chip) {
  744. DoC_SelectChip(this, mychip->chip);
  745. }
  746. this->curfloor = mychip->floor;
  747. this->curchip = mychip->chip;
  748. /* update address for 2M x 8bit devices. OOB starts on the second */
  749. /* page to maintain compatibility with doc_read_ecc. */
  750. if (this->page256) {
  751. if (!(ofs & 0x8))
  752. ofs += 0x100;
  753. else
  754. ofs -= 0x8;
  755. }
  756. DoC_Command(this, NAND_CMD_READOOB, CDSN_CTRL_WP);
  757. DoC_Address(this, ADDR_COLUMN_PAGE, ofs, CDSN_CTRL_WP, 0);
  758. /* treat crossing 8-byte OOB data for 2M x 8bit devices */
  759. /* Note: datasheet says it should automaticaly wrap to the */
  760. /* next OOB block, but it didn't work here. mf. */
  761. if (this->page256 && ofs + len > (ofs | 0x7) + 1) {
  762. len256 = (ofs | 0x7) + 1 - ofs;
  763. DoC_ReadBuf(this, buf, len256);
  764. DoC_Command(this, NAND_CMD_READOOB, CDSN_CTRL_WP);
  765. DoC_Address(this, ADDR_COLUMN_PAGE, ofs & (~0x1ff),
  766. CDSN_CTRL_WP, 0);
  767. }
  768. DoC_ReadBuf(this, &buf[len256], len - len256);
  769. ops->retlen = len;
  770. /* Reading the full OOB data drops us off of the end of the page,
  771. * causing the flash device to go into busy mode, so we need
  772. * to wait until ready 11.4.1 and Toshiba TC58256FT docs */
  773. ret = DoC_WaitReady(this);
  774. mutex_unlock(&this->lock);
  775. return ret;
  776. }
  777. static int doc_write_oob_nolock(struct mtd_info *mtd, loff_t ofs, size_t len,
  778. size_t * retlen, const u_char * buf)
  779. {
  780. struct DiskOnChip *this = mtd->priv;
  781. int len256 = 0;
  782. void __iomem *docptr = this->virtadr;
  783. struct Nand *mychip = &this->chips[ofs >> this->chipshift];
  784. volatile int dummy;
  785. int status;
  786. // printk("doc_write_oob(%lx, %d): %2.2X %2.2X %2.2X %2.2X ... %2.2X %2.2X .. %2.2X %2.2X\n",(long)ofs, len,
  787. // buf[0], buf[1], buf[2], buf[3], buf[8], buf[9], buf[14],buf[15]);
  788. /* Find the chip which is to be used and select it */
  789. if (this->curfloor != mychip->floor) {
  790. DoC_SelectFloor(this, mychip->floor);
  791. DoC_SelectChip(this, mychip->chip);
  792. } else if (this->curchip != mychip->chip) {
  793. DoC_SelectChip(this, mychip->chip);
  794. }
  795. this->curfloor = mychip->floor;
  796. this->curchip = mychip->chip;
  797. /* disable the ECC engine */
  798. WriteDOC (DOC_ECC_RESET, docptr, ECCConf);
  799. WriteDOC (DOC_ECC_DIS, docptr, ECCConf);
  800. /* Reset the chip, see Software Requirement 11.4 item 1. */
  801. DoC_Command(this, NAND_CMD_RESET, CDSN_CTRL_WP);
  802. /* issue the Read2 command to set the pointer to the Spare Data Area. */
  803. DoC_Command(this, NAND_CMD_READOOB, CDSN_CTRL_WP);
  804. /* update address for 2M x 8bit devices. OOB starts on the second */
  805. /* page to maintain compatibility with doc_read_ecc. */
  806. if (this->page256) {
  807. if (!(ofs & 0x8))
  808. ofs += 0x100;
  809. else
  810. ofs -= 0x8;
  811. }
  812. /* issue the Serial Data In command to initial the Page Program process */
  813. DoC_Command(this, NAND_CMD_SEQIN, 0);
  814. DoC_Address(this, ADDR_COLUMN_PAGE, ofs, 0, 0);
  815. /* treat crossing 8-byte OOB data for 2M x 8bit devices */
  816. /* Note: datasheet says it should automaticaly wrap to the */
  817. /* next OOB block, but it didn't work here. mf. */
  818. if (this->page256 && ofs + len > (ofs | 0x7) + 1) {
  819. len256 = (ofs | 0x7) + 1 - ofs;
  820. DoC_WriteBuf(this, buf, len256);
  821. DoC_Command(this, NAND_CMD_PAGEPROG, 0);
  822. DoC_Command(this, NAND_CMD_STATUS, 0);
  823. /* DoC_WaitReady() is implicit in DoC_Command */
  824. if (DoC_is_Millennium(this)) {
  825. ReadDOC(docptr, ReadPipeInit);
  826. status = ReadDOC(docptr, LastDataRead);
  827. } else {
  828. dummy = ReadDOC(docptr, CDSNSlowIO);
  829. DoC_Delay(this, 2);
  830. status = ReadDOC_(docptr, this->ioreg);
  831. }
  832. if (status & 1) {
  833. printk(KERN_ERR "Error programming oob data\n");
  834. /* There was an error */
  835. *retlen = 0;
  836. return -EIO;
  837. }
  838. DoC_Command(this, NAND_CMD_SEQIN, 0);
  839. DoC_Address(this, ADDR_COLUMN_PAGE, ofs & (~0x1ff), 0, 0);
  840. }
  841. DoC_WriteBuf(this, &buf[len256], len - len256);
  842. DoC_Command(this, NAND_CMD_PAGEPROG, 0);
  843. DoC_Command(this, NAND_CMD_STATUS, 0);
  844. /* DoC_WaitReady() is implicit in DoC_Command */
  845. if (DoC_is_Millennium(this)) {
  846. ReadDOC(docptr, ReadPipeInit);
  847. status = ReadDOC(docptr, LastDataRead);
  848. } else {
  849. dummy = ReadDOC(docptr, CDSNSlowIO);
  850. DoC_Delay(this, 2);
  851. status = ReadDOC_(docptr, this->ioreg);
  852. }
  853. if (status & 1) {
  854. printk(KERN_ERR "Error programming oob data\n");
  855. /* There was an error */
  856. *retlen = 0;
  857. return -EIO;
  858. }
  859. *retlen = len;
  860. return 0;
  861. }
  862. static int doc_write_oob(struct mtd_info *mtd, loff_t ofs,
  863. struct mtd_oob_ops *ops)
  864. {
  865. struct DiskOnChip *this = mtd->priv;
  866. int ret;
  867. BUG_ON(ops->mode != MTD_OPS_PLACE_OOB);
  868. mutex_lock(&this->lock);
  869. ret = doc_write_oob_nolock(mtd, ofs + ops->ooboffs, ops->len,
  870. &ops->retlen, ops->oobbuf);
  871. mutex_unlock(&this->lock);
  872. return ret;
  873. }
  874. static int doc_erase(struct mtd_info *mtd, struct erase_info *instr)
  875. {
  876. struct DiskOnChip *this = mtd->priv;
  877. __u32 ofs = instr->addr;
  878. __u32 len = instr->len;
  879. volatile int dummy;
  880. void __iomem *docptr = this->virtadr;
  881. struct Nand *mychip;
  882. int status;
  883. mutex_lock(&this->lock);
  884. if (ofs & (mtd->erasesize-1) || len & (mtd->erasesize-1)) {
  885. mutex_unlock(&this->lock);
  886. return -EINVAL;
  887. }
  888. instr->state = MTD_ERASING;
  889. /* FIXME: Do this in the background. Use timers or schedule_task() */
  890. while(len) {
  891. mychip = &this->chips[ofs >> this->chipshift];
  892. if (this->curfloor != mychip->floor) {
  893. DoC_SelectFloor(this, mychip->floor);
  894. DoC_SelectChip(this, mychip->chip);
  895. } else if (this->curchip != mychip->chip) {
  896. DoC_SelectChip(this, mychip->chip);
  897. }
  898. this->curfloor = mychip->floor;
  899. this->curchip = mychip->chip;
  900. DoC_Command(this, NAND_CMD_ERASE1, 0);
  901. DoC_Address(this, ADDR_PAGE, ofs, 0, 0);
  902. DoC_Command(this, NAND_CMD_ERASE2, 0);
  903. DoC_Command(this, NAND_CMD_STATUS, CDSN_CTRL_WP);
  904. if (DoC_is_Millennium(this)) {
  905. ReadDOC(docptr, ReadPipeInit);
  906. status = ReadDOC(docptr, LastDataRead);
  907. } else {
  908. dummy = ReadDOC(docptr, CDSNSlowIO);
  909. DoC_Delay(this, 2);
  910. status = ReadDOC_(docptr, this->ioreg);
  911. }
  912. if (status & 1) {
  913. printk(KERN_ERR "Error erasing at 0x%x\n", ofs);
  914. /* There was an error */
  915. instr->state = MTD_ERASE_FAILED;
  916. goto callback;
  917. }
  918. ofs += mtd->erasesize;
  919. len -= mtd->erasesize;
  920. }
  921. instr->state = MTD_ERASE_DONE;
  922. callback:
  923. mtd_erase_callback(instr);
  924. mutex_unlock(&this->lock);
  925. return 0;
  926. }
  927. /****************************************************************************
  928. *
  929. * Module stuff
  930. *
  931. ****************************************************************************/
  932. static void __exit cleanup_doc2000(void)
  933. {
  934. struct mtd_info *mtd;
  935. struct DiskOnChip *this;
  936. while ((mtd = doc2klist)) {
  937. this = mtd->priv;
  938. doc2klist = this->nextdoc;
  939. mtd_device_unregister(mtd);
  940. iounmap(this->virtadr);
  941. kfree(this->chips);
  942. kfree(mtd);
  943. }
  944. }
  945. module_exit(cleanup_doc2000);
  946. MODULE_LICENSE("GPL");
  947. MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org> et al.");
  948. MODULE_DESCRIPTION("MTD driver for DiskOnChip 2000 and Millennium");