doc2000.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310
  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. static const char im_name[] = "DoC2k_init";
  418. /* This routine is made available to other mtd code via
  419. * inter_module_register. It must only be accessed through
  420. * inter_module_get which will bump the use count of this module. The
  421. * addresses passed back in mtd are valid as long as the use count of
  422. * this module is non-zero, i.e. between inter_module_get and
  423. * inter_module_put. Keith Owens <kaos@ocs.com.au> 29 Oct 2000.
  424. */
  425. static void DoC2k_init(struct mtd_info *mtd)
  426. {
  427. struct DiskOnChip *this = mtd->priv;
  428. struct DiskOnChip *old = NULL;
  429. int maxchips;
  430. /* We must avoid being called twice for the same device. */
  431. if (doc2klist)
  432. old = doc2klist->priv;
  433. while (old) {
  434. if (DoC2k_is_alias(old, this)) {
  435. printk(KERN_NOTICE
  436. "Ignoring DiskOnChip 2000 at 0x%lX - already configured\n",
  437. this->physadr);
  438. iounmap(this->virtadr);
  439. kfree(mtd);
  440. return;
  441. }
  442. if (old->nextdoc)
  443. old = old->nextdoc->priv;
  444. else
  445. old = NULL;
  446. }
  447. switch (this->ChipID) {
  448. case DOC_ChipID_Doc2kTSOP:
  449. mtd->name = "DiskOnChip 2000 TSOP";
  450. this->ioreg = DoC_Mil_CDSN_IO;
  451. /* Pretend it's a Millennium */
  452. this->ChipID = DOC_ChipID_DocMil;
  453. maxchips = MAX_CHIPS;
  454. break;
  455. case DOC_ChipID_Doc2k:
  456. mtd->name = "DiskOnChip 2000";
  457. this->ioreg = DoC_2k_CDSN_IO;
  458. maxchips = MAX_CHIPS;
  459. break;
  460. case DOC_ChipID_DocMil:
  461. mtd->name = "DiskOnChip Millennium";
  462. this->ioreg = DoC_Mil_CDSN_IO;
  463. maxchips = MAX_CHIPS_MIL;
  464. break;
  465. default:
  466. printk("Unknown ChipID 0x%02x\n", this->ChipID);
  467. kfree(mtd);
  468. iounmap(this->virtadr);
  469. return;
  470. }
  471. printk(KERN_NOTICE "%s found at address 0x%lX\n", mtd->name,
  472. this->physadr);
  473. mtd->type = MTD_NANDFLASH;
  474. mtd->flags = MTD_CAP_NANDFLASH;
  475. mtd->ecctype = MTD_ECC_RS_DiskOnChip;
  476. mtd->size = 0;
  477. mtd->erasesize = 0;
  478. mtd->oobblock = 512;
  479. mtd->oobsize = 16;
  480. mtd->owner = THIS_MODULE;
  481. mtd->erase = doc_erase;
  482. mtd->point = NULL;
  483. mtd->unpoint = NULL;
  484. mtd->read = doc_read;
  485. mtd->write = doc_write;
  486. mtd->read_ecc = doc_read_ecc;
  487. mtd->write_ecc = doc_write_ecc;
  488. mtd->writev_ecc = doc_writev_ecc;
  489. mtd->read_oob = doc_read_oob;
  490. mtd->write_oob = doc_write_oob;
  491. mtd->sync = NULL;
  492. this->totlen = 0;
  493. this->numchips = 0;
  494. this->curfloor = -1;
  495. this->curchip = -1;
  496. mutex_init(&this->lock);
  497. /* Ident all the chips present. */
  498. DoC_ScanChips(this, maxchips);
  499. if (!this->totlen) {
  500. kfree(mtd);
  501. iounmap(this->virtadr);
  502. } else {
  503. this->nextdoc = doc2klist;
  504. doc2klist = mtd;
  505. mtd->size = this->totlen;
  506. mtd->erasesize = this->erasesize;
  507. add_mtd_device(mtd);
  508. return;
  509. }
  510. }
  511. static int doc_read(struct mtd_info *mtd, loff_t from, size_t len,
  512. size_t * retlen, u_char * buf)
  513. {
  514. /* Just a special case of doc_read_ecc */
  515. return doc_read_ecc(mtd, from, len, retlen, buf, NULL, NULL);
  516. }
  517. static int doc_read_ecc(struct mtd_info *mtd, loff_t from, size_t len,
  518. size_t * retlen, u_char * buf, u_char * eccbuf, struct nand_oobinfo *oobsel)
  519. {
  520. struct DiskOnChip *this = mtd->priv;
  521. void __iomem *docptr = this->virtadr;
  522. struct Nand *mychip;
  523. unsigned char syndrome[6];
  524. volatile char dummy;
  525. int i, len256 = 0, ret=0;
  526. size_t left = len;
  527. /* Don't allow read past end of device */
  528. if (from >= this->totlen)
  529. return -EINVAL;
  530. mutex_lock(&this->lock);
  531. *retlen = 0;
  532. while (left) {
  533. len = left;
  534. /* Don't allow a single read to cross a 512-byte block boundary */
  535. if (from + len > ((from | 0x1ff) + 1))
  536. len = ((from | 0x1ff) + 1) - from;
  537. /* The ECC will not be calculated correctly if less than 512 is read */
  538. if (len != 0x200 && eccbuf)
  539. printk(KERN_WARNING
  540. "ECC needs a full sector read (adr: %lx size %lx)\n",
  541. (long) from, (long) len);
  542. /* printk("DoC_Read (adr: %lx size %lx)\n", (long) from, (long) len); */
  543. /* Find the chip which is to be used and select it */
  544. mychip = &this->chips[from >> (this->chipshift)];
  545. if (this->curfloor != mychip->floor) {
  546. DoC_SelectFloor(this, mychip->floor);
  547. DoC_SelectChip(this, mychip->chip);
  548. } else if (this->curchip != mychip->chip) {
  549. DoC_SelectChip(this, mychip->chip);
  550. }
  551. this->curfloor = mychip->floor;
  552. this->curchip = mychip->chip;
  553. DoC_Command(this,
  554. (!this->page256
  555. && (from & 0x100)) ? NAND_CMD_READ1 : NAND_CMD_READ0,
  556. CDSN_CTRL_WP);
  557. DoC_Address(this, ADDR_COLUMN_PAGE, from, CDSN_CTRL_WP,
  558. CDSN_CTRL_ECC_IO);
  559. if (eccbuf) {
  560. /* Prime the ECC engine */
  561. WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
  562. WriteDOC(DOC_ECC_EN, docptr, ECCConf);
  563. } else {
  564. /* disable the ECC engine */
  565. WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
  566. WriteDOC(DOC_ECC_DIS, docptr, ECCConf);
  567. }
  568. /* treat crossing 256-byte sector for 2M x 8bits devices */
  569. if (this->page256 && from + len > (from | 0xff) + 1) {
  570. len256 = (from | 0xff) + 1 - from;
  571. DoC_ReadBuf(this, buf, len256);
  572. DoC_Command(this, NAND_CMD_READ0, CDSN_CTRL_WP);
  573. DoC_Address(this, ADDR_COLUMN_PAGE, from + len256,
  574. CDSN_CTRL_WP, CDSN_CTRL_ECC_IO);
  575. }
  576. DoC_ReadBuf(this, &buf[len256], len - len256);
  577. /* Let the caller know we completed it */
  578. *retlen += len;
  579. if (eccbuf) {
  580. /* Read the ECC data through the DiskOnChip ECC logic */
  581. /* Note: this will work even with 2M x 8bit devices as */
  582. /* they have 8 bytes of OOB per 256 page. mf. */
  583. DoC_ReadBuf(this, eccbuf, 6);
  584. /* Flush the pipeline */
  585. if (DoC_is_Millennium(this)) {
  586. dummy = ReadDOC(docptr, ECCConf);
  587. dummy = ReadDOC(docptr, ECCConf);
  588. i = ReadDOC(docptr, ECCConf);
  589. } else {
  590. dummy = ReadDOC(docptr, 2k_ECCStatus);
  591. dummy = ReadDOC(docptr, 2k_ECCStatus);
  592. i = ReadDOC(docptr, 2k_ECCStatus);
  593. }
  594. /* Check the ECC Status */
  595. if (i & 0x80) {
  596. int nb_errors;
  597. /* There was an ECC error */
  598. #ifdef ECC_DEBUG
  599. printk(KERN_ERR "DiskOnChip ECC Error: Read at %lx\n", (long)from);
  600. #endif
  601. /* Read the ECC syndrom through the DiskOnChip ECC logic.
  602. These syndrome will be all ZERO when there is no error */
  603. for (i = 0; i < 6; i++) {
  604. syndrome[i] =
  605. ReadDOC(docptr, ECCSyndrome0 + i);
  606. }
  607. nb_errors = doc_decode_ecc(buf, syndrome);
  608. #ifdef ECC_DEBUG
  609. printk(KERN_ERR "Errors corrected: %x\n", nb_errors);
  610. #endif
  611. if (nb_errors < 0) {
  612. /* We return error, but have actually done the read. Not that
  613. this can be told to user-space, via sys_read(), but at least
  614. MTD-aware stuff can know about it by checking *retlen */
  615. ret = -EIO;
  616. }
  617. }
  618. #ifdef PSYCHO_DEBUG
  619. printk(KERN_DEBUG "ECC DATA at %lxB: %2.2X %2.2X %2.2X %2.2X %2.2X %2.2X\n",
  620. (long)from, eccbuf[0], eccbuf[1], eccbuf[2],
  621. eccbuf[3], eccbuf[4], eccbuf[5]);
  622. #endif
  623. /* disable the ECC engine */
  624. WriteDOC(DOC_ECC_DIS, docptr , ECCConf);
  625. }
  626. /* according to 11.4.1, we need to wait for the busy line
  627. * drop if we read to the end of the page. */
  628. if(0 == ((from + len) & 0x1ff))
  629. {
  630. DoC_WaitReady(this);
  631. }
  632. from += len;
  633. left -= len;
  634. buf += len;
  635. }
  636. mutex_unlock(&this->lock);
  637. return ret;
  638. }
  639. static int doc_write(struct mtd_info *mtd, loff_t to, size_t len,
  640. size_t * retlen, const u_char * buf)
  641. {
  642. char eccbuf[6];
  643. return doc_write_ecc(mtd, to, len, retlen, buf, eccbuf, NULL);
  644. }
  645. static int doc_write_ecc(struct mtd_info *mtd, loff_t to, size_t len,
  646. size_t * retlen, const u_char * buf,
  647. u_char * eccbuf, struct nand_oobinfo *oobsel)
  648. {
  649. struct DiskOnChip *this = mtd->priv;
  650. int di; /* Yes, DI is a hangover from when I was disassembling the binary driver */
  651. void __iomem *docptr = this->virtadr;
  652. volatile char dummy;
  653. int len256 = 0;
  654. struct Nand *mychip;
  655. size_t left = len;
  656. int status;
  657. /* Don't allow write past end of device */
  658. if (to >= this->totlen)
  659. return -EINVAL;
  660. mutex_lock(&this->lock);
  661. *retlen = 0;
  662. while (left) {
  663. len = left;
  664. /* Don't allow a single write to cross a 512-byte block boundary */
  665. if (to + len > ((to | 0x1ff) + 1))
  666. len = ((to | 0x1ff) + 1) - to;
  667. /* The ECC will not be calculated correctly if less than 512 is written */
  668. /* DBB-
  669. if (len != 0x200 && eccbuf)
  670. printk(KERN_WARNING
  671. "ECC needs a full sector write (adr: %lx size %lx)\n",
  672. (long) to, (long) len);
  673. -DBB */
  674. /* printk("DoC_Write (adr: %lx size %lx)\n", (long) to, (long) len); */
  675. /* Find the chip which is to be used and select it */
  676. mychip = &this->chips[to >> (this->chipshift)];
  677. if (this->curfloor != mychip->floor) {
  678. DoC_SelectFloor(this, mychip->floor);
  679. DoC_SelectChip(this, mychip->chip);
  680. } else if (this->curchip != mychip->chip) {
  681. DoC_SelectChip(this, mychip->chip);
  682. }
  683. this->curfloor = mychip->floor;
  684. this->curchip = mychip->chip;
  685. /* Set device to main plane of flash */
  686. DoC_Command(this, NAND_CMD_RESET, CDSN_CTRL_WP);
  687. DoC_Command(this,
  688. (!this->page256
  689. && (to & 0x100)) ? NAND_CMD_READ1 : NAND_CMD_READ0,
  690. CDSN_CTRL_WP);
  691. DoC_Command(this, NAND_CMD_SEQIN, 0);
  692. DoC_Address(this, ADDR_COLUMN_PAGE, to, 0, CDSN_CTRL_ECC_IO);
  693. if (eccbuf) {
  694. /* Prime the ECC engine */
  695. WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
  696. WriteDOC(DOC_ECC_EN | DOC_ECC_RW, docptr, ECCConf);
  697. } else {
  698. /* disable the ECC engine */
  699. WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
  700. WriteDOC(DOC_ECC_DIS, docptr, ECCConf);
  701. }
  702. /* treat crossing 256-byte sector for 2M x 8bits devices */
  703. if (this->page256 && to + len > (to | 0xff) + 1) {
  704. len256 = (to | 0xff) + 1 - to;
  705. DoC_WriteBuf(this, buf, len256);
  706. DoC_Command(this, NAND_CMD_PAGEPROG, 0);
  707. DoC_Command(this, NAND_CMD_STATUS, CDSN_CTRL_WP);
  708. /* There's an implicit DoC_WaitReady() in DoC_Command */
  709. dummy = ReadDOC(docptr, CDSNSlowIO);
  710. DoC_Delay(this, 2);
  711. if (ReadDOC_(docptr, this->ioreg) & 1) {
  712. printk(KERN_ERR "Error programming flash\n");
  713. /* Error in programming */
  714. *retlen = 0;
  715. mutex_unlock(&this->lock);
  716. return -EIO;
  717. }
  718. DoC_Command(this, NAND_CMD_SEQIN, 0);
  719. DoC_Address(this, ADDR_COLUMN_PAGE, to + len256, 0,
  720. CDSN_CTRL_ECC_IO);
  721. }
  722. DoC_WriteBuf(this, &buf[len256], len - len256);
  723. if (eccbuf) {
  724. WriteDOC(CDSN_CTRL_ECC_IO | CDSN_CTRL_CE, docptr,
  725. CDSNControl);
  726. if (DoC_is_Millennium(this)) {
  727. WriteDOC(0, docptr, NOP);
  728. WriteDOC(0, docptr, NOP);
  729. WriteDOC(0, docptr, NOP);
  730. } else {
  731. WriteDOC_(0, docptr, this->ioreg);
  732. WriteDOC_(0, docptr, this->ioreg);
  733. WriteDOC_(0, docptr, this->ioreg);
  734. }
  735. WriteDOC(CDSN_CTRL_ECC_IO | CDSN_CTRL_FLASH_IO | CDSN_CTRL_CE, docptr,
  736. CDSNControl);
  737. /* Read the ECC data through the DiskOnChip ECC logic */
  738. for (di = 0; di < 6; di++) {
  739. eccbuf[di] = ReadDOC(docptr, ECCSyndrome0 + di);
  740. }
  741. /* Reset the ECC engine */
  742. WriteDOC(DOC_ECC_DIS, docptr, ECCConf);
  743. #ifdef PSYCHO_DEBUG
  744. printk
  745. ("OOB data at %lx is %2.2X %2.2X %2.2X %2.2X %2.2X %2.2X\n",
  746. (long) to, eccbuf[0], eccbuf[1], eccbuf[2], eccbuf[3],
  747. eccbuf[4], eccbuf[5]);
  748. #endif
  749. }
  750. DoC_Command(this, NAND_CMD_PAGEPROG, 0);
  751. DoC_Command(this, NAND_CMD_STATUS, CDSN_CTRL_WP);
  752. /* There's an implicit DoC_WaitReady() in DoC_Command */
  753. if (DoC_is_Millennium(this)) {
  754. ReadDOC(docptr, ReadPipeInit);
  755. status = ReadDOC(docptr, LastDataRead);
  756. } else {
  757. dummy = ReadDOC(docptr, CDSNSlowIO);
  758. DoC_Delay(this, 2);
  759. status = ReadDOC_(docptr, this->ioreg);
  760. }
  761. if (status & 1) {
  762. printk(KERN_ERR "Error programming flash\n");
  763. /* Error in programming */
  764. *retlen = 0;
  765. mutex_unlock(&this->lock);
  766. return -EIO;
  767. }
  768. /* Let the caller know we completed it */
  769. *retlen += len;
  770. if (eccbuf) {
  771. unsigned char x[8];
  772. size_t dummy;
  773. int ret;
  774. /* Write the ECC data to flash */
  775. for (di=0; di<6; di++)
  776. x[di] = eccbuf[di];
  777. x[6]=0x55;
  778. x[7]=0x55;
  779. ret = doc_write_oob_nolock(mtd, to, 8, &dummy, x);
  780. if (ret) {
  781. mutex_unlock(&this->lock);
  782. return ret;
  783. }
  784. }
  785. to += len;
  786. left -= len;
  787. buf += len;
  788. }
  789. mutex_unlock(&this->lock);
  790. return 0;
  791. }
  792. static int doc_writev_ecc(struct mtd_info *mtd, const struct kvec *vecs,
  793. unsigned long count, loff_t to, size_t *retlen,
  794. u_char *eccbuf, struct nand_oobinfo *oobsel)
  795. {
  796. static char static_buf[512];
  797. static DEFINE_MUTEX(writev_buf_mutex);
  798. size_t totretlen = 0;
  799. size_t thisvecofs = 0;
  800. int ret= 0;
  801. mutex_lock(&writev_buf_mutex);
  802. while(count) {
  803. size_t thislen, thisretlen;
  804. unsigned char *buf;
  805. buf = vecs->iov_base + thisvecofs;
  806. thislen = vecs->iov_len - thisvecofs;
  807. if (thislen >= 512) {
  808. thislen = thislen & ~(512-1);
  809. thisvecofs += thislen;
  810. } else {
  811. /* Not enough to fill a page. Copy into buf */
  812. memcpy(static_buf, buf, thislen);
  813. buf = &static_buf[thislen];
  814. while(count && thislen < 512) {
  815. vecs++;
  816. count--;
  817. thisvecofs = min((512-thislen), vecs->iov_len);
  818. memcpy(buf, vecs->iov_base, thisvecofs);
  819. thislen += thisvecofs;
  820. buf += thisvecofs;
  821. }
  822. buf = static_buf;
  823. }
  824. if (count && thisvecofs == vecs->iov_len) {
  825. thisvecofs = 0;
  826. vecs++;
  827. count--;
  828. }
  829. ret = doc_write_ecc(mtd, to, thislen, &thisretlen, buf, eccbuf, oobsel);
  830. totretlen += thisretlen;
  831. if (ret || thisretlen != thislen)
  832. break;
  833. to += thislen;
  834. }
  835. mutex_unlock(&writev_buf_mutex);
  836. *retlen = totretlen;
  837. return ret;
  838. }
  839. static int doc_read_oob(struct mtd_info *mtd, loff_t ofs, size_t len,
  840. size_t * retlen, u_char * buf)
  841. {
  842. struct DiskOnChip *this = mtd->priv;
  843. int len256 = 0, ret;
  844. struct Nand *mychip;
  845. mutex_lock(&this->lock);
  846. mychip = &this->chips[ofs >> this->chipshift];
  847. if (this->curfloor != mychip->floor) {
  848. DoC_SelectFloor(this, mychip->floor);
  849. DoC_SelectChip(this, mychip->chip);
  850. } else if (this->curchip != mychip->chip) {
  851. DoC_SelectChip(this, mychip->chip);
  852. }
  853. this->curfloor = mychip->floor;
  854. this->curchip = mychip->chip;
  855. /* update address for 2M x 8bit devices. OOB starts on the second */
  856. /* page to maintain compatibility with doc_read_ecc. */
  857. if (this->page256) {
  858. if (!(ofs & 0x8))
  859. ofs += 0x100;
  860. else
  861. ofs -= 0x8;
  862. }
  863. DoC_Command(this, NAND_CMD_READOOB, CDSN_CTRL_WP);
  864. DoC_Address(this, ADDR_COLUMN_PAGE, ofs, CDSN_CTRL_WP, 0);
  865. /* treat crossing 8-byte OOB data for 2M x 8bit devices */
  866. /* Note: datasheet says it should automaticaly wrap to the */
  867. /* next OOB block, but it didn't work here. mf. */
  868. if (this->page256 && ofs + len > (ofs | 0x7) + 1) {
  869. len256 = (ofs | 0x7) + 1 - ofs;
  870. DoC_ReadBuf(this, buf, len256);
  871. DoC_Command(this, NAND_CMD_READOOB, CDSN_CTRL_WP);
  872. DoC_Address(this, ADDR_COLUMN_PAGE, ofs & (~0x1ff),
  873. CDSN_CTRL_WP, 0);
  874. }
  875. DoC_ReadBuf(this, &buf[len256], len - len256);
  876. *retlen = len;
  877. /* Reading the full OOB data drops us off of the end of the page,
  878. * causing the flash device to go into busy mode, so we need
  879. * to wait until ready 11.4.1 and Toshiba TC58256FT docs */
  880. ret = DoC_WaitReady(this);
  881. mutex_unlock(&this->lock);
  882. return ret;
  883. }
  884. static int doc_write_oob_nolock(struct mtd_info *mtd, loff_t ofs, size_t len,
  885. size_t * retlen, const u_char * buf)
  886. {
  887. struct DiskOnChip *this = mtd->priv;
  888. int len256 = 0;
  889. void __iomem *docptr = this->virtadr;
  890. struct Nand *mychip = &this->chips[ofs >> this->chipshift];
  891. volatile int dummy;
  892. int status;
  893. // 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,
  894. // buf[0], buf[1], buf[2], buf[3], buf[8], buf[9], buf[14],buf[15]);
  895. /* Find the chip which is to be used and select it */
  896. if (this->curfloor != mychip->floor) {
  897. DoC_SelectFloor(this, mychip->floor);
  898. DoC_SelectChip(this, mychip->chip);
  899. } else if (this->curchip != mychip->chip) {
  900. DoC_SelectChip(this, mychip->chip);
  901. }
  902. this->curfloor = mychip->floor;
  903. this->curchip = mychip->chip;
  904. /* disable the ECC engine */
  905. WriteDOC (DOC_ECC_RESET, docptr, ECCConf);
  906. WriteDOC (DOC_ECC_DIS, docptr, ECCConf);
  907. /* Reset the chip, see Software Requirement 11.4 item 1. */
  908. DoC_Command(this, NAND_CMD_RESET, CDSN_CTRL_WP);
  909. /* issue the Read2 command to set the pointer to the Spare Data Area. */
  910. DoC_Command(this, NAND_CMD_READOOB, CDSN_CTRL_WP);
  911. /* update address for 2M x 8bit devices. OOB starts on the second */
  912. /* page to maintain compatibility with doc_read_ecc. */
  913. if (this->page256) {
  914. if (!(ofs & 0x8))
  915. ofs += 0x100;
  916. else
  917. ofs -= 0x8;
  918. }
  919. /* issue the Serial Data In command to initial the Page Program process */
  920. DoC_Command(this, NAND_CMD_SEQIN, 0);
  921. DoC_Address(this, ADDR_COLUMN_PAGE, ofs, 0, 0);
  922. /* treat crossing 8-byte OOB data for 2M x 8bit devices */
  923. /* Note: datasheet says it should automaticaly wrap to the */
  924. /* next OOB block, but it didn't work here. mf. */
  925. if (this->page256 && ofs + len > (ofs | 0x7) + 1) {
  926. len256 = (ofs | 0x7) + 1 - ofs;
  927. DoC_WriteBuf(this, buf, len256);
  928. DoC_Command(this, NAND_CMD_PAGEPROG, 0);
  929. DoC_Command(this, NAND_CMD_STATUS, 0);
  930. /* DoC_WaitReady() is implicit in DoC_Command */
  931. if (DoC_is_Millennium(this)) {
  932. ReadDOC(docptr, ReadPipeInit);
  933. status = ReadDOC(docptr, LastDataRead);
  934. } else {
  935. dummy = ReadDOC(docptr, CDSNSlowIO);
  936. DoC_Delay(this, 2);
  937. status = ReadDOC_(docptr, this->ioreg);
  938. }
  939. if (status & 1) {
  940. printk(KERN_ERR "Error programming oob data\n");
  941. /* There was an error */
  942. *retlen = 0;
  943. return -EIO;
  944. }
  945. DoC_Command(this, NAND_CMD_SEQIN, 0);
  946. DoC_Address(this, ADDR_COLUMN_PAGE, ofs & (~0x1ff), 0, 0);
  947. }
  948. DoC_WriteBuf(this, &buf[len256], len - len256);
  949. DoC_Command(this, NAND_CMD_PAGEPROG, 0);
  950. DoC_Command(this, NAND_CMD_STATUS, 0);
  951. /* DoC_WaitReady() is implicit in DoC_Command */
  952. if (DoC_is_Millennium(this)) {
  953. ReadDOC(docptr, ReadPipeInit);
  954. status = ReadDOC(docptr, LastDataRead);
  955. } else {
  956. dummy = ReadDOC(docptr, CDSNSlowIO);
  957. DoC_Delay(this, 2);
  958. status = ReadDOC_(docptr, this->ioreg);
  959. }
  960. if (status & 1) {
  961. printk(KERN_ERR "Error programming oob data\n");
  962. /* There was an error */
  963. *retlen = 0;
  964. return -EIO;
  965. }
  966. *retlen = len;
  967. return 0;
  968. }
  969. static int doc_write_oob(struct mtd_info *mtd, loff_t ofs, size_t len,
  970. size_t * retlen, const u_char * buf)
  971. {
  972. struct DiskOnChip *this = mtd->priv;
  973. int ret;
  974. mutex_lock(&this->lock);
  975. ret = doc_write_oob_nolock(mtd, ofs, len, retlen, buf);
  976. mutex_unlock(&this->lock);
  977. return ret;
  978. }
  979. static int doc_erase(struct mtd_info *mtd, struct erase_info *instr)
  980. {
  981. struct DiskOnChip *this = mtd->priv;
  982. __u32 ofs = instr->addr;
  983. __u32 len = instr->len;
  984. volatile int dummy;
  985. void __iomem *docptr = this->virtadr;
  986. struct Nand *mychip;
  987. int status;
  988. mutex_lock(&this->lock);
  989. if (ofs & (mtd->erasesize-1) || len & (mtd->erasesize-1)) {
  990. mutex_unlock(&this->lock);
  991. return -EINVAL;
  992. }
  993. instr->state = MTD_ERASING;
  994. /* FIXME: Do this in the background. Use timers or schedule_task() */
  995. while(len) {
  996. mychip = &this->chips[ofs >> this->chipshift];
  997. if (this->curfloor != mychip->floor) {
  998. DoC_SelectFloor(this, mychip->floor);
  999. DoC_SelectChip(this, mychip->chip);
  1000. } else if (this->curchip != mychip->chip) {
  1001. DoC_SelectChip(this, mychip->chip);
  1002. }
  1003. this->curfloor = mychip->floor;
  1004. this->curchip = mychip->chip;
  1005. DoC_Command(this, NAND_CMD_ERASE1, 0);
  1006. DoC_Address(this, ADDR_PAGE, ofs, 0, 0);
  1007. DoC_Command(this, NAND_CMD_ERASE2, 0);
  1008. DoC_Command(this, NAND_CMD_STATUS, CDSN_CTRL_WP);
  1009. if (DoC_is_Millennium(this)) {
  1010. ReadDOC(docptr, ReadPipeInit);
  1011. status = ReadDOC(docptr, LastDataRead);
  1012. } else {
  1013. dummy = ReadDOC(docptr, CDSNSlowIO);
  1014. DoC_Delay(this, 2);
  1015. status = ReadDOC_(docptr, this->ioreg);
  1016. }
  1017. if (status & 1) {
  1018. printk(KERN_ERR "Error erasing at 0x%x\n", ofs);
  1019. /* There was an error */
  1020. instr->state = MTD_ERASE_FAILED;
  1021. goto callback;
  1022. }
  1023. ofs += mtd->erasesize;
  1024. len -= mtd->erasesize;
  1025. }
  1026. instr->state = MTD_ERASE_DONE;
  1027. callback:
  1028. mtd_erase_callback(instr);
  1029. mutex_unlock(&this->lock);
  1030. return 0;
  1031. }
  1032. /****************************************************************************
  1033. *
  1034. * Module stuff
  1035. *
  1036. ****************************************************************************/
  1037. static int __init init_doc2000(void)
  1038. {
  1039. inter_module_register(im_name, THIS_MODULE, &DoC2k_init);
  1040. return 0;
  1041. }
  1042. static void __exit cleanup_doc2000(void)
  1043. {
  1044. struct mtd_info *mtd;
  1045. struct DiskOnChip *this;
  1046. while ((mtd = doc2klist)) {
  1047. this = mtd->priv;
  1048. doc2klist = this->nextdoc;
  1049. del_mtd_device(mtd);
  1050. iounmap(this->virtadr);
  1051. kfree(this->chips);
  1052. kfree(mtd);
  1053. }
  1054. inter_module_unregister(im_name);
  1055. }
  1056. module_exit(cleanup_doc2000);
  1057. module_init(init_doc2000);
  1058. MODULE_LICENSE("GPL");
  1059. MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org> et al.");
  1060. MODULE_DESCRIPTION("MTD driver for DiskOnChip 2000 and Millennium");