doc2000.c 35 KB

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