doc2000.c 35 KB

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