doc2000.c 32 KB

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