diskonchip.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708
  1. /*
  2. * (C) 2003 Red Hat, Inc.
  3. * (C) 2004 Dan Brown <dan_brown@ieee.org>
  4. * (C) 2004 Kalev Lember <kalev@smartlink.ee>
  5. *
  6. * Author: David Woodhouse <dwmw2@infradead.org>
  7. * Additional Diskonchip 2000 and Millennium support by Dan Brown <dan_brown@ieee.org>
  8. * Diskonchip Millennium Plus support by Kalev Lember <kalev@smartlink.ee>
  9. *
  10. * Error correction code lifted from the old docecc code
  11. * Author: Fabrice Bellard (fabrice.bellard@netgem.com)
  12. * Copyright (C) 2000 Netgem S.A.
  13. * converted to the generic Reed-Solomon library by Thomas Gleixner <tglx@linutronix.de>
  14. *
  15. * Interface to generic NAND code for M-Systems DiskOnChip devices
  16. */
  17. #include <linux/kernel.h>
  18. #include <linux/init.h>
  19. #include <linux/sched.h>
  20. #include <linux/delay.h>
  21. #include <linux/rslib.h>
  22. #include <linux/moduleparam.h>
  23. #include <linux/slab.h>
  24. #include <linux/io.h>
  25. #include <linux/mtd/mtd.h>
  26. #include <linux/mtd/rawnand.h>
  27. #include <linux/mtd/doc2000.h>
  28. #include <linux/mtd/partitions.h>
  29. #include <linux/mtd/inftl.h>
  30. #include <linux/module.h>
  31. /* Where to look for the devices? */
  32. #ifndef CONFIG_MTD_NAND_DISKONCHIP_PROBE_ADDRESS
  33. #define CONFIG_MTD_NAND_DISKONCHIP_PROBE_ADDRESS 0
  34. #endif
  35. static unsigned long doc_locations[] __initdata = {
  36. #if defined (__alpha__) || defined(__i386__) || defined(__x86_64__)
  37. #ifdef CONFIG_MTD_NAND_DISKONCHIP_PROBE_HIGH
  38. 0xfffc8000, 0xfffca000, 0xfffcc000, 0xfffce000,
  39. 0xfffd0000, 0xfffd2000, 0xfffd4000, 0xfffd6000,
  40. 0xfffd8000, 0xfffda000, 0xfffdc000, 0xfffde000,
  41. 0xfffe0000, 0xfffe2000, 0xfffe4000, 0xfffe6000,
  42. 0xfffe8000, 0xfffea000, 0xfffec000, 0xfffee000,
  43. #else
  44. 0xc8000, 0xca000, 0xcc000, 0xce000,
  45. 0xd0000, 0xd2000, 0xd4000, 0xd6000,
  46. 0xd8000, 0xda000, 0xdc000, 0xde000,
  47. 0xe0000, 0xe2000, 0xe4000, 0xe6000,
  48. 0xe8000, 0xea000, 0xec000, 0xee000,
  49. #endif
  50. #endif
  51. 0xffffffff };
  52. static struct mtd_info *doclist = NULL;
  53. struct doc_priv {
  54. void __iomem *virtadr;
  55. unsigned long physadr;
  56. u_char ChipID;
  57. u_char CDSNControl;
  58. int chips_per_floor; /* The number of chips detected on each floor */
  59. int curfloor;
  60. int curchip;
  61. int mh0_page;
  62. int mh1_page;
  63. struct rs_control *rs_decoder;
  64. struct mtd_info *nextdoc;
  65. /* Handle the last stage of initialization (BBT scan, partitioning) */
  66. int (*late_init)(struct mtd_info *mtd);
  67. };
  68. /* This is the ecc value computed by the HW ecc generator upon writing an empty
  69. page, one with all 0xff for data. */
  70. static u_char empty_write_ecc[6] = { 0x4b, 0x00, 0xe2, 0x0e, 0x93, 0xf7 };
  71. #define INFTL_BBT_RESERVED_BLOCKS 4
  72. #define DoC_is_MillenniumPlus(doc) ((doc)->ChipID == DOC_ChipID_DocMilPlus16 || (doc)->ChipID == DOC_ChipID_DocMilPlus32)
  73. #define DoC_is_Millennium(doc) ((doc)->ChipID == DOC_ChipID_DocMil)
  74. #define DoC_is_2000(doc) ((doc)->ChipID == DOC_ChipID_Doc2k)
  75. static void doc200x_hwcontrol(struct mtd_info *mtd, int cmd,
  76. unsigned int bitmask);
  77. static void doc200x_select_chip(struct mtd_info *mtd, int chip);
  78. static int debug = 0;
  79. module_param(debug, int, 0);
  80. static int try_dword = 1;
  81. module_param(try_dword, int, 0);
  82. static int no_ecc_failures = 0;
  83. module_param(no_ecc_failures, int, 0);
  84. static int no_autopart = 0;
  85. module_param(no_autopart, int, 0);
  86. static int show_firmware_partition = 0;
  87. module_param(show_firmware_partition, int, 0);
  88. #ifdef CONFIG_MTD_NAND_DISKONCHIP_BBTWRITE
  89. static int inftl_bbt_write = 1;
  90. #else
  91. static int inftl_bbt_write = 0;
  92. #endif
  93. module_param(inftl_bbt_write, int, 0);
  94. static unsigned long doc_config_location = CONFIG_MTD_NAND_DISKONCHIP_PROBE_ADDRESS;
  95. module_param(doc_config_location, ulong, 0);
  96. MODULE_PARM_DESC(doc_config_location, "Physical memory address at which to probe for DiskOnChip");
  97. /* Sector size for HW ECC */
  98. #define SECTOR_SIZE 512
  99. /* The sector bytes are packed into NB_DATA 10 bit words */
  100. #define NB_DATA (((SECTOR_SIZE + 1) * 8 + 6) / 10)
  101. /* Number of roots */
  102. #define NROOTS 4
  103. /* First consective root */
  104. #define FCR 510
  105. /* Number of symbols */
  106. #define NN 1023
  107. /*
  108. * The HW decoder in the DoC ASIC's provides us a error syndrome,
  109. * which we must convert to a standard syndrome usable by the generic
  110. * Reed-Solomon library code.
  111. *
  112. * Fabrice Bellard figured this out in the old docecc code. I added
  113. * some comments, improved a minor bit and converted it to make use
  114. * of the generic Reed-Solomon library. tglx
  115. */
  116. static int doc_ecc_decode(struct rs_control *rs, uint8_t *data, uint8_t *ecc)
  117. {
  118. int i, j, nerr, errpos[8];
  119. uint8_t parity;
  120. uint16_t ds[4], s[5], tmp, errval[8], syn[4];
  121. struct rs_codec *cd = rs->codec;
  122. memset(syn, 0, sizeof(syn));
  123. /* Convert the ecc bytes into words */
  124. ds[0] = ((ecc[4] & 0xff) >> 0) | ((ecc[5] & 0x03) << 8);
  125. ds[1] = ((ecc[5] & 0xfc) >> 2) | ((ecc[2] & 0x0f) << 6);
  126. ds[2] = ((ecc[2] & 0xf0) >> 4) | ((ecc[3] & 0x3f) << 4);
  127. ds[3] = ((ecc[3] & 0xc0) >> 6) | ((ecc[0] & 0xff) << 2);
  128. parity = ecc[1];
  129. /* Initialize the syndrome buffer */
  130. for (i = 0; i < NROOTS; i++)
  131. s[i] = ds[0];
  132. /*
  133. * Evaluate
  134. * s[i] = ds[3]x^3 + ds[2]x^2 + ds[1]x^1 + ds[0]
  135. * where x = alpha^(FCR + i)
  136. */
  137. for (j = 1; j < NROOTS; j++) {
  138. if (ds[j] == 0)
  139. continue;
  140. tmp = cd->index_of[ds[j]];
  141. for (i = 0; i < NROOTS; i++)
  142. s[i] ^= cd->alpha_to[rs_modnn(cd, tmp + (FCR + i) * j)];
  143. }
  144. /* Calc syn[i] = s[i] / alpha^(v + i) */
  145. for (i = 0; i < NROOTS; i++) {
  146. if (s[i])
  147. syn[i] = rs_modnn(cd, cd->index_of[s[i]] + (NN - FCR - i));
  148. }
  149. /* Call the decoder library */
  150. nerr = decode_rs16(rs, NULL, NULL, 1019, syn, 0, errpos, 0, errval);
  151. /* Incorrectable errors ? */
  152. if (nerr < 0)
  153. return nerr;
  154. /*
  155. * Correct the errors. The bitpositions are a bit of magic,
  156. * but they are given by the design of the de/encoder circuit
  157. * in the DoC ASIC's.
  158. */
  159. for (i = 0; i < nerr; i++) {
  160. int index, bitpos, pos = 1015 - errpos[i];
  161. uint8_t val;
  162. if (pos >= NB_DATA && pos < 1019)
  163. continue;
  164. if (pos < NB_DATA) {
  165. /* extract bit position (MSB first) */
  166. pos = 10 * (NB_DATA - 1 - pos) - 6;
  167. /* now correct the following 10 bits. At most two bytes
  168. can be modified since pos is even */
  169. index = (pos >> 3) ^ 1;
  170. bitpos = pos & 7;
  171. if ((index >= 0 && index < SECTOR_SIZE) || index == (SECTOR_SIZE + 1)) {
  172. val = (uint8_t) (errval[i] >> (2 + bitpos));
  173. parity ^= val;
  174. if (index < SECTOR_SIZE)
  175. data[index] ^= val;
  176. }
  177. index = ((pos >> 3) + 1) ^ 1;
  178. bitpos = (bitpos + 10) & 7;
  179. if (bitpos == 0)
  180. bitpos = 8;
  181. if ((index >= 0 && index < SECTOR_SIZE) || index == (SECTOR_SIZE + 1)) {
  182. val = (uint8_t) (errval[i] << (8 - bitpos));
  183. parity ^= val;
  184. if (index < SECTOR_SIZE)
  185. data[index] ^= val;
  186. }
  187. }
  188. }
  189. /* If the parity is wrong, no rescue possible */
  190. return parity ? -EBADMSG : nerr;
  191. }
  192. static void DoC_Delay(struct doc_priv *doc, unsigned short cycles)
  193. {
  194. volatile char dummy;
  195. int i;
  196. for (i = 0; i < cycles; i++) {
  197. if (DoC_is_Millennium(doc))
  198. dummy = ReadDOC(doc->virtadr, NOP);
  199. else if (DoC_is_MillenniumPlus(doc))
  200. dummy = ReadDOC(doc->virtadr, Mplus_NOP);
  201. else
  202. dummy = ReadDOC(doc->virtadr, DOCStatus);
  203. }
  204. }
  205. #define CDSN_CTRL_FR_B_MASK (CDSN_CTRL_FR_B0 | CDSN_CTRL_FR_B1)
  206. /* DOC_WaitReady: Wait for RDY line to be asserted by the flash chip */
  207. static int _DoC_WaitReady(struct doc_priv *doc)
  208. {
  209. void __iomem *docptr = doc->virtadr;
  210. unsigned long timeo = jiffies + (HZ * 10);
  211. if (debug)
  212. printk("_DoC_WaitReady...\n");
  213. /* Out-of-line routine to wait for chip response */
  214. if (DoC_is_MillenniumPlus(doc)) {
  215. while ((ReadDOC(docptr, Mplus_FlashControl) & CDSN_CTRL_FR_B_MASK) != CDSN_CTRL_FR_B_MASK) {
  216. if (time_after(jiffies, timeo)) {
  217. printk("_DoC_WaitReady timed out.\n");
  218. return -EIO;
  219. }
  220. udelay(1);
  221. cond_resched();
  222. }
  223. } else {
  224. while (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B)) {
  225. if (time_after(jiffies, timeo)) {
  226. printk("_DoC_WaitReady timed out.\n");
  227. return -EIO;
  228. }
  229. udelay(1);
  230. cond_resched();
  231. }
  232. }
  233. return 0;
  234. }
  235. static inline int DoC_WaitReady(struct doc_priv *doc)
  236. {
  237. void __iomem *docptr = doc->virtadr;
  238. int ret = 0;
  239. if (DoC_is_MillenniumPlus(doc)) {
  240. DoC_Delay(doc, 4);
  241. if ((ReadDOC(docptr, Mplus_FlashControl) & CDSN_CTRL_FR_B_MASK) != CDSN_CTRL_FR_B_MASK)
  242. /* Call the out-of-line routine to wait */
  243. ret = _DoC_WaitReady(doc);
  244. } else {
  245. DoC_Delay(doc, 4);
  246. if (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B))
  247. /* Call the out-of-line routine to wait */
  248. ret = _DoC_WaitReady(doc);
  249. DoC_Delay(doc, 2);
  250. }
  251. if (debug)
  252. printk("DoC_WaitReady OK\n");
  253. return ret;
  254. }
  255. static void doc2000_write_byte(struct mtd_info *mtd, u_char datum)
  256. {
  257. struct nand_chip *this = mtd_to_nand(mtd);
  258. struct doc_priv *doc = nand_get_controller_data(this);
  259. void __iomem *docptr = doc->virtadr;
  260. if (debug)
  261. printk("write_byte %02x\n", datum);
  262. WriteDOC(datum, docptr, CDSNSlowIO);
  263. WriteDOC(datum, docptr, 2k_CDSN_IO);
  264. }
  265. static u_char doc2000_read_byte(struct mtd_info *mtd)
  266. {
  267. struct nand_chip *this = mtd_to_nand(mtd);
  268. struct doc_priv *doc = nand_get_controller_data(this);
  269. void __iomem *docptr = doc->virtadr;
  270. u_char ret;
  271. ReadDOC(docptr, CDSNSlowIO);
  272. DoC_Delay(doc, 2);
  273. ret = ReadDOC(docptr, 2k_CDSN_IO);
  274. if (debug)
  275. printk("read_byte returns %02x\n", ret);
  276. return ret;
  277. }
  278. static void doc2000_writebuf(struct mtd_info *mtd, const u_char *buf, int len)
  279. {
  280. struct nand_chip *this = mtd_to_nand(mtd);
  281. struct doc_priv *doc = nand_get_controller_data(this);
  282. void __iomem *docptr = doc->virtadr;
  283. int i;
  284. if (debug)
  285. printk("writebuf of %d bytes: ", len);
  286. for (i = 0; i < len; i++) {
  287. WriteDOC_(buf[i], docptr, DoC_2k_CDSN_IO + i);
  288. if (debug && i < 16)
  289. printk("%02x ", buf[i]);
  290. }
  291. if (debug)
  292. printk("\n");
  293. }
  294. static void doc2000_readbuf(struct mtd_info *mtd, u_char *buf, int len)
  295. {
  296. struct nand_chip *this = mtd_to_nand(mtd);
  297. struct doc_priv *doc = nand_get_controller_data(this);
  298. void __iomem *docptr = doc->virtadr;
  299. int i;
  300. if (debug)
  301. printk("readbuf of %d bytes: ", len);
  302. for (i = 0; i < len; i++) {
  303. buf[i] = ReadDOC(docptr, 2k_CDSN_IO + i);
  304. }
  305. }
  306. static void doc2000_readbuf_dword(struct mtd_info *mtd, u_char *buf, int len)
  307. {
  308. struct nand_chip *this = mtd_to_nand(mtd);
  309. struct doc_priv *doc = nand_get_controller_data(this);
  310. void __iomem *docptr = doc->virtadr;
  311. int i;
  312. if (debug)
  313. printk("readbuf_dword of %d bytes: ", len);
  314. if (unlikely((((unsigned long)buf) | len) & 3)) {
  315. for (i = 0; i < len; i++) {
  316. *(uint8_t *) (&buf[i]) = ReadDOC(docptr, 2k_CDSN_IO + i);
  317. }
  318. } else {
  319. for (i = 0; i < len; i += 4) {
  320. *(uint32_t *) (&buf[i]) = readl(docptr + DoC_2k_CDSN_IO + i);
  321. }
  322. }
  323. }
  324. static uint16_t __init doc200x_ident_chip(struct mtd_info *mtd, int nr)
  325. {
  326. struct nand_chip *this = mtd_to_nand(mtd);
  327. struct doc_priv *doc = nand_get_controller_data(this);
  328. uint16_t ret;
  329. doc200x_select_chip(mtd, nr);
  330. doc200x_hwcontrol(mtd, NAND_CMD_READID,
  331. NAND_CTRL_CLE | NAND_CTRL_CHANGE);
  332. doc200x_hwcontrol(mtd, 0, NAND_CTRL_ALE | NAND_CTRL_CHANGE);
  333. doc200x_hwcontrol(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
  334. /* We can't use dev_ready here, but at least we wait for the
  335. * command to complete
  336. */
  337. udelay(50);
  338. ret = this->read_byte(mtd) << 8;
  339. ret |= this->read_byte(mtd);
  340. if (doc->ChipID == DOC_ChipID_Doc2k && try_dword && !nr) {
  341. /* First chip probe. See if we get same results by 32-bit access */
  342. union {
  343. uint32_t dword;
  344. uint8_t byte[4];
  345. } ident;
  346. void __iomem *docptr = doc->virtadr;
  347. doc200x_hwcontrol(mtd, NAND_CMD_READID,
  348. NAND_CTRL_CLE | NAND_CTRL_CHANGE);
  349. doc200x_hwcontrol(mtd, 0, NAND_CTRL_ALE | NAND_CTRL_CHANGE);
  350. doc200x_hwcontrol(mtd, NAND_CMD_NONE,
  351. NAND_NCE | NAND_CTRL_CHANGE);
  352. udelay(50);
  353. ident.dword = readl(docptr + DoC_2k_CDSN_IO);
  354. if (((ident.byte[0] << 8) | ident.byte[1]) == ret) {
  355. pr_info("DiskOnChip 2000 responds to DWORD access\n");
  356. this->read_buf = &doc2000_readbuf_dword;
  357. }
  358. }
  359. return ret;
  360. }
  361. static void __init doc2000_count_chips(struct mtd_info *mtd)
  362. {
  363. struct nand_chip *this = mtd_to_nand(mtd);
  364. struct doc_priv *doc = nand_get_controller_data(this);
  365. uint16_t mfrid;
  366. int i;
  367. /* Max 4 chips per floor on DiskOnChip 2000 */
  368. doc->chips_per_floor = 4;
  369. /* Find out what the first chip is */
  370. mfrid = doc200x_ident_chip(mtd, 0);
  371. /* Find how many chips in each floor. */
  372. for (i = 1; i < 4; i++) {
  373. if (doc200x_ident_chip(mtd, i) != mfrid)
  374. break;
  375. }
  376. doc->chips_per_floor = i;
  377. pr_debug("Detected %d chips per floor.\n", i);
  378. }
  379. static int doc200x_wait(struct mtd_info *mtd, struct nand_chip *this)
  380. {
  381. struct doc_priv *doc = nand_get_controller_data(this);
  382. int status;
  383. DoC_WaitReady(doc);
  384. nand_status_op(this, NULL);
  385. DoC_WaitReady(doc);
  386. status = (int)this->read_byte(mtd);
  387. return status;
  388. }
  389. static void doc2001_write_byte(struct mtd_info *mtd, u_char datum)
  390. {
  391. struct nand_chip *this = mtd_to_nand(mtd);
  392. struct doc_priv *doc = nand_get_controller_data(this);
  393. void __iomem *docptr = doc->virtadr;
  394. WriteDOC(datum, docptr, CDSNSlowIO);
  395. WriteDOC(datum, docptr, Mil_CDSN_IO);
  396. WriteDOC(datum, docptr, WritePipeTerm);
  397. }
  398. static u_char doc2001_read_byte(struct mtd_info *mtd)
  399. {
  400. struct nand_chip *this = mtd_to_nand(mtd);
  401. struct doc_priv *doc = nand_get_controller_data(this);
  402. void __iomem *docptr = doc->virtadr;
  403. //ReadDOC(docptr, CDSNSlowIO);
  404. /* 11.4.5 -- delay twice to allow extended length cycle */
  405. DoC_Delay(doc, 2);
  406. ReadDOC(docptr, ReadPipeInit);
  407. //return ReadDOC(docptr, Mil_CDSN_IO);
  408. return ReadDOC(docptr, LastDataRead);
  409. }
  410. static void doc2001_writebuf(struct mtd_info *mtd, const u_char *buf, int len)
  411. {
  412. struct nand_chip *this = mtd_to_nand(mtd);
  413. struct doc_priv *doc = nand_get_controller_data(this);
  414. void __iomem *docptr = doc->virtadr;
  415. int i;
  416. for (i = 0; i < len; i++)
  417. WriteDOC_(buf[i], docptr, DoC_Mil_CDSN_IO + i);
  418. /* Terminate write pipeline */
  419. WriteDOC(0x00, docptr, WritePipeTerm);
  420. }
  421. static void doc2001_readbuf(struct mtd_info *mtd, u_char *buf, int len)
  422. {
  423. struct nand_chip *this = mtd_to_nand(mtd);
  424. struct doc_priv *doc = nand_get_controller_data(this);
  425. void __iomem *docptr = doc->virtadr;
  426. int i;
  427. /* Start read pipeline */
  428. ReadDOC(docptr, ReadPipeInit);
  429. for (i = 0; i < len - 1; i++)
  430. buf[i] = ReadDOC(docptr, Mil_CDSN_IO + (i & 0xff));
  431. /* Terminate read pipeline */
  432. buf[i] = ReadDOC(docptr, LastDataRead);
  433. }
  434. static u_char doc2001plus_read_byte(struct mtd_info *mtd)
  435. {
  436. struct nand_chip *this = mtd_to_nand(mtd);
  437. struct doc_priv *doc = nand_get_controller_data(this);
  438. void __iomem *docptr = doc->virtadr;
  439. u_char ret;
  440. ReadDOC(docptr, Mplus_ReadPipeInit);
  441. ReadDOC(docptr, Mplus_ReadPipeInit);
  442. ret = ReadDOC(docptr, Mplus_LastDataRead);
  443. if (debug)
  444. printk("read_byte returns %02x\n", ret);
  445. return ret;
  446. }
  447. static void doc2001plus_writebuf(struct mtd_info *mtd, const u_char *buf, int len)
  448. {
  449. struct nand_chip *this = mtd_to_nand(mtd);
  450. struct doc_priv *doc = nand_get_controller_data(this);
  451. void __iomem *docptr = doc->virtadr;
  452. int i;
  453. if (debug)
  454. printk("writebuf of %d bytes: ", len);
  455. for (i = 0; i < len; i++) {
  456. WriteDOC_(buf[i], docptr, DoC_Mil_CDSN_IO + i);
  457. if (debug && i < 16)
  458. printk("%02x ", buf[i]);
  459. }
  460. if (debug)
  461. printk("\n");
  462. }
  463. static void doc2001plus_readbuf(struct mtd_info *mtd, u_char *buf, int len)
  464. {
  465. struct nand_chip *this = mtd_to_nand(mtd);
  466. struct doc_priv *doc = nand_get_controller_data(this);
  467. void __iomem *docptr = doc->virtadr;
  468. int i;
  469. if (debug)
  470. printk("readbuf of %d bytes: ", len);
  471. /* Start read pipeline */
  472. ReadDOC(docptr, Mplus_ReadPipeInit);
  473. ReadDOC(docptr, Mplus_ReadPipeInit);
  474. for (i = 0; i < len - 2; i++) {
  475. buf[i] = ReadDOC(docptr, Mil_CDSN_IO);
  476. if (debug && i < 16)
  477. printk("%02x ", buf[i]);
  478. }
  479. /* Terminate read pipeline */
  480. buf[len - 2] = ReadDOC(docptr, Mplus_LastDataRead);
  481. if (debug && i < 16)
  482. printk("%02x ", buf[len - 2]);
  483. buf[len - 1] = ReadDOC(docptr, Mplus_LastDataRead);
  484. if (debug && i < 16)
  485. printk("%02x ", buf[len - 1]);
  486. if (debug)
  487. printk("\n");
  488. }
  489. static void doc2001plus_select_chip(struct mtd_info *mtd, int chip)
  490. {
  491. struct nand_chip *this = mtd_to_nand(mtd);
  492. struct doc_priv *doc = nand_get_controller_data(this);
  493. void __iomem *docptr = doc->virtadr;
  494. int floor = 0;
  495. if (debug)
  496. printk("select chip (%d)\n", chip);
  497. if (chip == -1) {
  498. /* Disable flash internally */
  499. WriteDOC(0, docptr, Mplus_FlashSelect);
  500. return;
  501. }
  502. floor = chip / doc->chips_per_floor;
  503. chip -= (floor * doc->chips_per_floor);
  504. /* Assert ChipEnable and deassert WriteProtect */
  505. WriteDOC((DOC_FLASH_CE), docptr, Mplus_FlashSelect);
  506. nand_reset_op(this);
  507. doc->curchip = chip;
  508. doc->curfloor = floor;
  509. }
  510. static void doc200x_select_chip(struct mtd_info *mtd, int chip)
  511. {
  512. struct nand_chip *this = mtd_to_nand(mtd);
  513. struct doc_priv *doc = nand_get_controller_data(this);
  514. void __iomem *docptr = doc->virtadr;
  515. int floor = 0;
  516. if (debug)
  517. printk("select chip (%d)\n", chip);
  518. if (chip == -1)
  519. return;
  520. floor = chip / doc->chips_per_floor;
  521. chip -= (floor * doc->chips_per_floor);
  522. /* 11.4.4 -- deassert CE before changing chip */
  523. doc200x_hwcontrol(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
  524. WriteDOC(floor, docptr, FloorSelect);
  525. WriteDOC(chip, docptr, CDSNDeviceSelect);
  526. doc200x_hwcontrol(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
  527. doc->curchip = chip;
  528. doc->curfloor = floor;
  529. }
  530. #define CDSN_CTRL_MSK (CDSN_CTRL_CE | CDSN_CTRL_CLE | CDSN_CTRL_ALE)
  531. static void doc200x_hwcontrol(struct mtd_info *mtd, int cmd,
  532. unsigned int ctrl)
  533. {
  534. struct nand_chip *this = mtd_to_nand(mtd);
  535. struct doc_priv *doc = nand_get_controller_data(this);
  536. void __iomem *docptr = doc->virtadr;
  537. if (ctrl & NAND_CTRL_CHANGE) {
  538. doc->CDSNControl &= ~CDSN_CTRL_MSK;
  539. doc->CDSNControl |= ctrl & CDSN_CTRL_MSK;
  540. if (debug)
  541. printk("hwcontrol(%d): %02x\n", cmd, doc->CDSNControl);
  542. WriteDOC(doc->CDSNControl, docptr, CDSNControl);
  543. /* 11.4.3 -- 4 NOPs after CSDNControl write */
  544. DoC_Delay(doc, 4);
  545. }
  546. if (cmd != NAND_CMD_NONE) {
  547. if (DoC_is_2000(doc))
  548. doc2000_write_byte(mtd, cmd);
  549. else
  550. doc2001_write_byte(mtd, cmd);
  551. }
  552. }
  553. static void doc2001plus_command(struct mtd_info *mtd, unsigned command, int column, int page_addr)
  554. {
  555. struct nand_chip *this = mtd_to_nand(mtd);
  556. struct doc_priv *doc = nand_get_controller_data(this);
  557. void __iomem *docptr = doc->virtadr;
  558. /*
  559. * Must terminate write pipeline before sending any commands
  560. * to the device.
  561. */
  562. if (command == NAND_CMD_PAGEPROG) {
  563. WriteDOC(0x00, docptr, Mplus_WritePipeTerm);
  564. WriteDOC(0x00, docptr, Mplus_WritePipeTerm);
  565. }
  566. /*
  567. * Write out the command to the device.
  568. */
  569. if (command == NAND_CMD_SEQIN) {
  570. int readcmd;
  571. if (column >= mtd->writesize) {
  572. /* OOB area */
  573. column -= mtd->writesize;
  574. readcmd = NAND_CMD_READOOB;
  575. } else if (column < 256) {
  576. /* First 256 bytes --> READ0 */
  577. readcmd = NAND_CMD_READ0;
  578. } else {
  579. column -= 256;
  580. readcmd = NAND_CMD_READ1;
  581. }
  582. WriteDOC(readcmd, docptr, Mplus_FlashCmd);
  583. }
  584. WriteDOC(command, docptr, Mplus_FlashCmd);
  585. WriteDOC(0, docptr, Mplus_WritePipeTerm);
  586. WriteDOC(0, docptr, Mplus_WritePipeTerm);
  587. if (column != -1 || page_addr != -1) {
  588. /* Serially input address */
  589. if (column != -1) {
  590. /* Adjust columns for 16 bit buswidth */
  591. if (this->options & NAND_BUSWIDTH_16 &&
  592. !nand_opcode_8bits(command))
  593. column >>= 1;
  594. WriteDOC(column, docptr, Mplus_FlashAddress);
  595. }
  596. if (page_addr != -1) {
  597. WriteDOC((unsigned char)(page_addr & 0xff), docptr, Mplus_FlashAddress);
  598. WriteDOC((unsigned char)((page_addr >> 8) & 0xff), docptr, Mplus_FlashAddress);
  599. if (this->options & NAND_ROW_ADDR_3) {
  600. WriteDOC((unsigned char)((page_addr >> 16) & 0x0f), docptr, Mplus_FlashAddress);
  601. printk("high density\n");
  602. }
  603. }
  604. WriteDOC(0, docptr, Mplus_WritePipeTerm);
  605. WriteDOC(0, docptr, Mplus_WritePipeTerm);
  606. /* deassert ALE */
  607. if (command == NAND_CMD_READ0 || command == NAND_CMD_READ1 ||
  608. command == NAND_CMD_READOOB || command == NAND_CMD_READID)
  609. WriteDOC(0, docptr, Mplus_FlashControl);
  610. }
  611. /*
  612. * program and erase have their own busy handlers
  613. * status and sequential in needs no delay
  614. */
  615. switch (command) {
  616. case NAND_CMD_PAGEPROG:
  617. case NAND_CMD_ERASE1:
  618. case NAND_CMD_ERASE2:
  619. case NAND_CMD_SEQIN:
  620. case NAND_CMD_STATUS:
  621. return;
  622. case NAND_CMD_RESET:
  623. if (this->dev_ready)
  624. break;
  625. udelay(this->chip_delay);
  626. WriteDOC(NAND_CMD_STATUS, docptr, Mplus_FlashCmd);
  627. WriteDOC(0, docptr, Mplus_WritePipeTerm);
  628. WriteDOC(0, docptr, Mplus_WritePipeTerm);
  629. while (!(this->read_byte(mtd) & 0x40)) ;
  630. return;
  631. /* This applies to read commands */
  632. default:
  633. /*
  634. * If we don't have access to the busy pin, we apply the given
  635. * command delay
  636. */
  637. if (!this->dev_ready) {
  638. udelay(this->chip_delay);
  639. return;
  640. }
  641. }
  642. /* Apply this short delay always to ensure that we do wait tWB in
  643. * any case on any machine. */
  644. ndelay(100);
  645. /* wait until command is processed */
  646. while (!this->dev_ready(mtd)) ;
  647. }
  648. static int doc200x_dev_ready(struct mtd_info *mtd)
  649. {
  650. struct nand_chip *this = mtd_to_nand(mtd);
  651. struct doc_priv *doc = nand_get_controller_data(this);
  652. void __iomem *docptr = doc->virtadr;
  653. if (DoC_is_MillenniumPlus(doc)) {
  654. /* 11.4.2 -- must NOP four times before checking FR/B# */
  655. DoC_Delay(doc, 4);
  656. if ((ReadDOC(docptr, Mplus_FlashControl) & CDSN_CTRL_FR_B_MASK) != CDSN_CTRL_FR_B_MASK) {
  657. if (debug)
  658. printk("not ready\n");
  659. return 0;
  660. }
  661. if (debug)
  662. printk("was ready\n");
  663. return 1;
  664. } else {
  665. /* 11.4.2 -- must NOP four times before checking FR/B# */
  666. DoC_Delay(doc, 4);
  667. if (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B)) {
  668. if (debug)
  669. printk("not ready\n");
  670. return 0;
  671. }
  672. /* 11.4.2 -- Must NOP twice if it's ready */
  673. DoC_Delay(doc, 2);
  674. if (debug)
  675. printk("was ready\n");
  676. return 1;
  677. }
  678. }
  679. static int doc200x_block_bad(struct mtd_info *mtd, loff_t ofs)
  680. {
  681. /* This is our last resort if we couldn't find or create a BBT. Just
  682. pretend all blocks are good. */
  683. return 0;
  684. }
  685. static void doc200x_enable_hwecc(struct mtd_info *mtd, int mode)
  686. {
  687. struct nand_chip *this = mtd_to_nand(mtd);
  688. struct doc_priv *doc = nand_get_controller_data(this);
  689. void __iomem *docptr = doc->virtadr;
  690. /* Prime the ECC engine */
  691. switch (mode) {
  692. case NAND_ECC_READ:
  693. WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
  694. WriteDOC(DOC_ECC_EN, docptr, ECCConf);
  695. break;
  696. case NAND_ECC_WRITE:
  697. WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
  698. WriteDOC(DOC_ECC_EN | DOC_ECC_RW, docptr, ECCConf);
  699. break;
  700. }
  701. }
  702. static void doc2001plus_enable_hwecc(struct mtd_info *mtd, int mode)
  703. {
  704. struct nand_chip *this = mtd_to_nand(mtd);
  705. struct doc_priv *doc = nand_get_controller_data(this);
  706. void __iomem *docptr = doc->virtadr;
  707. /* Prime the ECC engine */
  708. switch (mode) {
  709. case NAND_ECC_READ:
  710. WriteDOC(DOC_ECC_RESET, docptr, Mplus_ECCConf);
  711. WriteDOC(DOC_ECC_EN, docptr, Mplus_ECCConf);
  712. break;
  713. case NAND_ECC_WRITE:
  714. WriteDOC(DOC_ECC_RESET, docptr, Mplus_ECCConf);
  715. WriteDOC(DOC_ECC_EN | DOC_ECC_RW, docptr, Mplus_ECCConf);
  716. break;
  717. }
  718. }
  719. /* This code is only called on write */
  720. static int doc200x_calculate_ecc(struct mtd_info *mtd, const u_char *dat, unsigned char *ecc_code)
  721. {
  722. struct nand_chip *this = mtd_to_nand(mtd);
  723. struct doc_priv *doc = nand_get_controller_data(this);
  724. void __iomem *docptr = doc->virtadr;
  725. int i;
  726. int emptymatch = 1;
  727. /* flush the pipeline */
  728. if (DoC_is_2000(doc)) {
  729. WriteDOC(doc->CDSNControl & ~CDSN_CTRL_FLASH_IO, docptr, CDSNControl);
  730. WriteDOC(0, docptr, 2k_CDSN_IO);
  731. WriteDOC(0, docptr, 2k_CDSN_IO);
  732. WriteDOC(0, docptr, 2k_CDSN_IO);
  733. WriteDOC(doc->CDSNControl, docptr, CDSNControl);
  734. } else if (DoC_is_MillenniumPlus(doc)) {
  735. WriteDOC(0, docptr, Mplus_NOP);
  736. WriteDOC(0, docptr, Mplus_NOP);
  737. WriteDOC(0, docptr, Mplus_NOP);
  738. } else {
  739. WriteDOC(0, docptr, NOP);
  740. WriteDOC(0, docptr, NOP);
  741. WriteDOC(0, docptr, NOP);
  742. }
  743. for (i = 0; i < 6; i++) {
  744. if (DoC_is_MillenniumPlus(doc))
  745. ecc_code[i] = ReadDOC_(docptr, DoC_Mplus_ECCSyndrome0 + i);
  746. else
  747. ecc_code[i] = ReadDOC_(docptr, DoC_ECCSyndrome0 + i);
  748. if (ecc_code[i] != empty_write_ecc[i])
  749. emptymatch = 0;
  750. }
  751. if (DoC_is_MillenniumPlus(doc))
  752. WriteDOC(DOC_ECC_DIS, docptr, Mplus_ECCConf);
  753. else
  754. WriteDOC(DOC_ECC_DIS, docptr, ECCConf);
  755. #if 0
  756. /* If emptymatch=1, we might have an all-0xff data buffer. Check. */
  757. if (emptymatch) {
  758. /* Note: this somewhat expensive test should not be triggered
  759. often. It could be optimized away by examining the data in
  760. the writebuf routine, and remembering the result. */
  761. for (i = 0; i < 512; i++) {
  762. if (dat[i] == 0xff)
  763. continue;
  764. emptymatch = 0;
  765. break;
  766. }
  767. }
  768. /* If emptymatch still =1, we do have an all-0xff data buffer.
  769. Return all-0xff ecc value instead of the computed one, so
  770. it'll look just like a freshly-erased page. */
  771. if (emptymatch)
  772. memset(ecc_code, 0xff, 6);
  773. #endif
  774. return 0;
  775. }
  776. static int doc200x_correct_data(struct mtd_info *mtd, u_char *dat,
  777. u_char *read_ecc, u_char *isnull)
  778. {
  779. int i, ret = 0;
  780. struct nand_chip *this = mtd_to_nand(mtd);
  781. struct doc_priv *doc = nand_get_controller_data(this);
  782. void __iomem *docptr = doc->virtadr;
  783. uint8_t calc_ecc[6];
  784. volatile u_char dummy;
  785. /* flush the pipeline */
  786. if (DoC_is_2000(doc)) {
  787. dummy = ReadDOC(docptr, 2k_ECCStatus);
  788. dummy = ReadDOC(docptr, 2k_ECCStatus);
  789. dummy = ReadDOC(docptr, 2k_ECCStatus);
  790. } else if (DoC_is_MillenniumPlus(doc)) {
  791. dummy = ReadDOC(docptr, Mplus_ECCConf);
  792. dummy = ReadDOC(docptr, Mplus_ECCConf);
  793. dummy = ReadDOC(docptr, Mplus_ECCConf);
  794. } else {
  795. dummy = ReadDOC(docptr, ECCConf);
  796. dummy = ReadDOC(docptr, ECCConf);
  797. dummy = ReadDOC(docptr, ECCConf);
  798. }
  799. /* Error occurred ? */
  800. if (dummy & 0x80) {
  801. for (i = 0; i < 6; i++) {
  802. if (DoC_is_MillenniumPlus(doc))
  803. calc_ecc[i] = ReadDOC_(docptr, DoC_Mplus_ECCSyndrome0 + i);
  804. else
  805. calc_ecc[i] = ReadDOC_(docptr, DoC_ECCSyndrome0 + i);
  806. }
  807. ret = doc_ecc_decode(doc->rs_decoder, dat, calc_ecc);
  808. if (ret > 0)
  809. pr_err("doc200x_correct_data corrected %d errors\n",
  810. ret);
  811. }
  812. if (DoC_is_MillenniumPlus(doc))
  813. WriteDOC(DOC_ECC_DIS, docptr, Mplus_ECCConf);
  814. else
  815. WriteDOC(DOC_ECC_DIS, docptr, ECCConf);
  816. if (no_ecc_failures && mtd_is_eccerr(ret)) {
  817. pr_err("suppressing ECC failure\n");
  818. ret = 0;
  819. }
  820. return ret;
  821. }
  822. //u_char mydatabuf[528];
  823. static int doc200x_ooblayout_ecc(struct mtd_info *mtd, int section,
  824. struct mtd_oob_region *oobregion)
  825. {
  826. if (section)
  827. return -ERANGE;
  828. oobregion->offset = 0;
  829. oobregion->length = 6;
  830. return 0;
  831. }
  832. static int doc200x_ooblayout_free(struct mtd_info *mtd, int section,
  833. struct mtd_oob_region *oobregion)
  834. {
  835. if (section > 1)
  836. return -ERANGE;
  837. /*
  838. * The strange out-of-order free bytes definition is a (possibly
  839. * unneeded) attempt to retain compatibility. It used to read:
  840. * .oobfree = { {8, 8} }
  841. * Since that leaves two bytes unusable, it was changed. But the
  842. * following scheme might affect existing jffs2 installs by moving the
  843. * cleanmarker:
  844. * .oobfree = { {6, 10} }
  845. * jffs2 seems to handle the above gracefully, but the current scheme
  846. * seems safer. The only problem with it is that any code retrieving
  847. * free bytes position must be able to handle out-of-order segments.
  848. */
  849. if (!section) {
  850. oobregion->offset = 8;
  851. oobregion->length = 8;
  852. } else {
  853. oobregion->offset = 6;
  854. oobregion->length = 2;
  855. }
  856. return 0;
  857. }
  858. static const struct mtd_ooblayout_ops doc200x_ooblayout_ops = {
  859. .ecc = doc200x_ooblayout_ecc,
  860. .free = doc200x_ooblayout_free,
  861. };
  862. /* Find the (I)NFTL Media Header, and optionally also the mirror media header.
  863. On successful return, buf will contain a copy of the media header for
  864. further processing. id is the string to scan for, and will presumably be
  865. either "ANAND" or "BNAND". If findmirror=1, also look for the mirror media
  866. header. The page #s of the found media headers are placed in mh0_page and
  867. mh1_page in the DOC private structure. */
  868. static int __init find_media_headers(struct mtd_info *mtd, u_char *buf, const char *id, int findmirror)
  869. {
  870. struct nand_chip *this = mtd_to_nand(mtd);
  871. struct doc_priv *doc = nand_get_controller_data(this);
  872. unsigned offs;
  873. int ret;
  874. size_t retlen;
  875. for (offs = 0; offs < mtd->size; offs += mtd->erasesize) {
  876. ret = mtd_read(mtd, offs, mtd->writesize, &retlen, buf);
  877. if (retlen != mtd->writesize)
  878. continue;
  879. if (ret) {
  880. pr_warn("ECC error scanning DOC at 0x%x\n", offs);
  881. }
  882. if (memcmp(buf, id, 6))
  883. continue;
  884. pr_info("Found DiskOnChip %s Media Header at 0x%x\n", id, offs);
  885. if (doc->mh0_page == -1) {
  886. doc->mh0_page = offs >> this->page_shift;
  887. if (!findmirror)
  888. return 1;
  889. continue;
  890. }
  891. doc->mh1_page = offs >> this->page_shift;
  892. return 2;
  893. }
  894. if (doc->mh0_page == -1) {
  895. pr_warn("DiskOnChip %s Media Header not found.\n", id);
  896. return 0;
  897. }
  898. /* Only one mediaheader was found. We want buf to contain a
  899. mediaheader on return, so we'll have to re-read the one we found. */
  900. offs = doc->mh0_page << this->page_shift;
  901. ret = mtd_read(mtd, offs, mtd->writesize, &retlen, buf);
  902. if (retlen != mtd->writesize) {
  903. /* Insanity. Give up. */
  904. pr_err("Read DiskOnChip Media Header once, but can't reread it???\n");
  905. return 0;
  906. }
  907. return 1;
  908. }
  909. static inline int __init nftl_partscan(struct mtd_info *mtd, struct mtd_partition *parts)
  910. {
  911. struct nand_chip *this = mtd_to_nand(mtd);
  912. struct doc_priv *doc = nand_get_controller_data(this);
  913. int ret = 0;
  914. u_char *buf;
  915. struct NFTLMediaHeader *mh;
  916. const unsigned psize = 1 << this->page_shift;
  917. int numparts = 0;
  918. unsigned blocks, maxblocks;
  919. int offs, numheaders;
  920. buf = kmalloc(mtd->writesize, GFP_KERNEL);
  921. if (!buf) {
  922. return 0;
  923. }
  924. if (!(numheaders = find_media_headers(mtd, buf, "ANAND", 1)))
  925. goto out;
  926. mh = (struct NFTLMediaHeader *)buf;
  927. le16_to_cpus(&mh->NumEraseUnits);
  928. le16_to_cpus(&mh->FirstPhysicalEUN);
  929. le32_to_cpus(&mh->FormattedSize);
  930. pr_info(" DataOrgID = %s\n"
  931. " NumEraseUnits = %d\n"
  932. " FirstPhysicalEUN = %d\n"
  933. " FormattedSize = %d\n"
  934. " UnitSizeFactor = %d\n",
  935. mh->DataOrgID, mh->NumEraseUnits,
  936. mh->FirstPhysicalEUN, mh->FormattedSize,
  937. mh->UnitSizeFactor);
  938. blocks = mtd->size >> this->phys_erase_shift;
  939. maxblocks = min(32768U, mtd->erasesize - psize);
  940. if (mh->UnitSizeFactor == 0x00) {
  941. /* Auto-determine UnitSizeFactor. The constraints are:
  942. - There can be at most 32768 virtual blocks.
  943. - There can be at most (virtual block size - page size)
  944. virtual blocks (because MediaHeader+BBT must fit in 1).
  945. */
  946. mh->UnitSizeFactor = 0xff;
  947. while (blocks > maxblocks) {
  948. blocks >>= 1;
  949. maxblocks = min(32768U, (maxblocks << 1) + psize);
  950. mh->UnitSizeFactor--;
  951. }
  952. pr_warn("UnitSizeFactor=0x00 detected. Correct value is assumed to be 0x%02x.\n", mh->UnitSizeFactor);
  953. }
  954. /* NOTE: The lines below modify internal variables of the NAND and MTD
  955. layers; variables with have already been configured by nand_scan.
  956. Unfortunately, we didn't know before this point what these values
  957. should be. Thus, this code is somewhat dependent on the exact
  958. implementation of the NAND layer. */
  959. if (mh->UnitSizeFactor != 0xff) {
  960. this->bbt_erase_shift += (0xff - mh->UnitSizeFactor);
  961. mtd->erasesize <<= (0xff - mh->UnitSizeFactor);
  962. pr_info("Setting virtual erase size to %d\n", mtd->erasesize);
  963. blocks = mtd->size >> this->bbt_erase_shift;
  964. maxblocks = min(32768U, mtd->erasesize - psize);
  965. }
  966. if (blocks > maxblocks) {
  967. pr_err("UnitSizeFactor of 0x%02x is inconsistent with device size. Aborting.\n", mh->UnitSizeFactor);
  968. goto out;
  969. }
  970. /* Skip past the media headers. */
  971. offs = max(doc->mh0_page, doc->mh1_page);
  972. offs <<= this->page_shift;
  973. offs += mtd->erasesize;
  974. if (show_firmware_partition == 1) {
  975. parts[0].name = " DiskOnChip Firmware / Media Header partition";
  976. parts[0].offset = 0;
  977. parts[0].size = offs;
  978. numparts = 1;
  979. }
  980. parts[numparts].name = " DiskOnChip BDTL partition";
  981. parts[numparts].offset = offs;
  982. parts[numparts].size = (mh->NumEraseUnits - numheaders) << this->bbt_erase_shift;
  983. offs += parts[numparts].size;
  984. numparts++;
  985. if (offs < mtd->size) {
  986. parts[numparts].name = " DiskOnChip Remainder partition";
  987. parts[numparts].offset = offs;
  988. parts[numparts].size = mtd->size - offs;
  989. numparts++;
  990. }
  991. ret = numparts;
  992. out:
  993. kfree(buf);
  994. return ret;
  995. }
  996. /* This is a stripped-down copy of the code in inftlmount.c */
  997. static inline int __init inftl_partscan(struct mtd_info *mtd, struct mtd_partition *parts)
  998. {
  999. struct nand_chip *this = mtd_to_nand(mtd);
  1000. struct doc_priv *doc = nand_get_controller_data(this);
  1001. int ret = 0;
  1002. u_char *buf;
  1003. struct INFTLMediaHeader *mh;
  1004. struct INFTLPartition *ip;
  1005. int numparts = 0;
  1006. int blocks;
  1007. int vshift, lastvunit = 0;
  1008. int i;
  1009. int end = mtd->size;
  1010. if (inftl_bbt_write)
  1011. end -= (INFTL_BBT_RESERVED_BLOCKS << this->phys_erase_shift);
  1012. buf = kmalloc(mtd->writesize, GFP_KERNEL);
  1013. if (!buf) {
  1014. return 0;
  1015. }
  1016. if (!find_media_headers(mtd, buf, "BNAND", 0))
  1017. goto out;
  1018. doc->mh1_page = doc->mh0_page + (4096 >> this->page_shift);
  1019. mh = (struct INFTLMediaHeader *)buf;
  1020. le32_to_cpus(&mh->NoOfBootImageBlocks);
  1021. le32_to_cpus(&mh->NoOfBinaryPartitions);
  1022. le32_to_cpus(&mh->NoOfBDTLPartitions);
  1023. le32_to_cpus(&mh->BlockMultiplierBits);
  1024. le32_to_cpus(&mh->FormatFlags);
  1025. le32_to_cpus(&mh->PercentUsed);
  1026. pr_info(" bootRecordID = %s\n"
  1027. " NoOfBootImageBlocks = %d\n"
  1028. " NoOfBinaryPartitions = %d\n"
  1029. " NoOfBDTLPartitions = %d\n"
  1030. " BlockMultiplerBits = %d\n"
  1031. " FormatFlgs = %d\n"
  1032. " OsakVersion = %d.%d.%d.%d\n"
  1033. " PercentUsed = %d\n",
  1034. mh->bootRecordID, mh->NoOfBootImageBlocks,
  1035. mh->NoOfBinaryPartitions,
  1036. mh->NoOfBDTLPartitions,
  1037. mh->BlockMultiplierBits, mh->FormatFlags,
  1038. ((unsigned char *) &mh->OsakVersion)[0] & 0xf,
  1039. ((unsigned char *) &mh->OsakVersion)[1] & 0xf,
  1040. ((unsigned char *) &mh->OsakVersion)[2] & 0xf,
  1041. ((unsigned char *) &mh->OsakVersion)[3] & 0xf,
  1042. mh->PercentUsed);
  1043. vshift = this->phys_erase_shift + mh->BlockMultiplierBits;
  1044. blocks = mtd->size >> vshift;
  1045. if (blocks > 32768) {
  1046. pr_err("BlockMultiplierBits=%d is inconsistent with device size. Aborting.\n", mh->BlockMultiplierBits);
  1047. goto out;
  1048. }
  1049. blocks = doc->chips_per_floor << (this->chip_shift - this->phys_erase_shift);
  1050. if (inftl_bbt_write && (blocks > mtd->erasesize)) {
  1051. pr_err("Writeable BBTs spanning more than one erase block are not yet supported. FIX ME!\n");
  1052. goto out;
  1053. }
  1054. /* Scan the partitions */
  1055. for (i = 0; (i < 4); i++) {
  1056. ip = &(mh->Partitions[i]);
  1057. le32_to_cpus(&ip->virtualUnits);
  1058. le32_to_cpus(&ip->firstUnit);
  1059. le32_to_cpus(&ip->lastUnit);
  1060. le32_to_cpus(&ip->flags);
  1061. le32_to_cpus(&ip->spareUnits);
  1062. le32_to_cpus(&ip->Reserved0);
  1063. pr_info(" PARTITION[%d] ->\n"
  1064. " virtualUnits = %d\n"
  1065. " firstUnit = %d\n"
  1066. " lastUnit = %d\n"
  1067. " flags = 0x%x\n"
  1068. " spareUnits = %d\n",
  1069. i, ip->virtualUnits, ip->firstUnit,
  1070. ip->lastUnit, ip->flags,
  1071. ip->spareUnits);
  1072. if ((show_firmware_partition == 1) &&
  1073. (i == 0) && (ip->firstUnit > 0)) {
  1074. parts[0].name = " DiskOnChip IPL / Media Header partition";
  1075. parts[0].offset = 0;
  1076. parts[0].size = mtd->erasesize * ip->firstUnit;
  1077. numparts = 1;
  1078. }
  1079. if (ip->flags & INFTL_BINARY)
  1080. parts[numparts].name = " DiskOnChip BDK partition";
  1081. else
  1082. parts[numparts].name = " DiskOnChip BDTL partition";
  1083. parts[numparts].offset = ip->firstUnit << vshift;
  1084. parts[numparts].size = (1 + ip->lastUnit - ip->firstUnit) << vshift;
  1085. numparts++;
  1086. if (ip->lastUnit > lastvunit)
  1087. lastvunit = ip->lastUnit;
  1088. if (ip->flags & INFTL_LAST)
  1089. break;
  1090. }
  1091. lastvunit++;
  1092. if ((lastvunit << vshift) < end) {
  1093. parts[numparts].name = " DiskOnChip Remainder partition";
  1094. parts[numparts].offset = lastvunit << vshift;
  1095. parts[numparts].size = end - parts[numparts].offset;
  1096. numparts++;
  1097. }
  1098. ret = numparts;
  1099. out:
  1100. kfree(buf);
  1101. return ret;
  1102. }
  1103. static int __init nftl_scan_bbt(struct mtd_info *mtd)
  1104. {
  1105. int ret, numparts;
  1106. struct nand_chip *this = mtd_to_nand(mtd);
  1107. struct doc_priv *doc = nand_get_controller_data(this);
  1108. struct mtd_partition parts[2];
  1109. memset((char *)parts, 0, sizeof(parts));
  1110. /* On NFTL, we have to find the media headers before we can read the
  1111. BBTs, since they're stored in the media header eraseblocks. */
  1112. numparts = nftl_partscan(mtd, parts);
  1113. if (!numparts)
  1114. return -EIO;
  1115. this->bbt_td->options = NAND_BBT_ABSPAGE | NAND_BBT_8BIT |
  1116. NAND_BBT_SAVECONTENT | NAND_BBT_WRITE |
  1117. NAND_BBT_VERSION;
  1118. this->bbt_td->veroffs = 7;
  1119. this->bbt_td->pages[0] = doc->mh0_page + 1;
  1120. if (doc->mh1_page != -1) {
  1121. this->bbt_md->options = NAND_BBT_ABSPAGE | NAND_BBT_8BIT |
  1122. NAND_BBT_SAVECONTENT | NAND_BBT_WRITE |
  1123. NAND_BBT_VERSION;
  1124. this->bbt_md->veroffs = 7;
  1125. this->bbt_md->pages[0] = doc->mh1_page + 1;
  1126. } else {
  1127. this->bbt_md = NULL;
  1128. }
  1129. ret = this->scan_bbt(mtd);
  1130. if (ret)
  1131. return ret;
  1132. return mtd_device_register(mtd, parts, no_autopart ? 0 : numparts);
  1133. }
  1134. static int __init inftl_scan_bbt(struct mtd_info *mtd)
  1135. {
  1136. int ret, numparts;
  1137. struct nand_chip *this = mtd_to_nand(mtd);
  1138. struct doc_priv *doc = nand_get_controller_data(this);
  1139. struct mtd_partition parts[5];
  1140. if (this->numchips > doc->chips_per_floor) {
  1141. pr_err("Multi-floor INFTL devices not yet supported.\n");
  1142. return -EIO;
  1143. }
  1144. if (DoC_is_MillenniumPlus(doc)) {
  1145. this->bbt_td->options = NAND_BBT_2BIT | NAND_BBT_ABSPAGE;
  1146. if (inftl_bbt_write)
  1147. this->bbt_td->options |= NAND_BBT_WRITE;
  1148. this->bbt_td->pages[0] = 2;
  1149. this->bbt_md = NULL;
  1150. } else {
  1151. this->bbt_td->options = NAND_BBT_LASTBLOCK | NAND_BBT_8BIT | NAND_BBT_VERSION;
  1152. if (inftl_bbt_write)
  1153. this->bbt_td->options |= NAND_BBT_WRITE;
  1154. this->bbt_td->offs = 8;
  1155. this->bbt_td->len = 8;
  1156. this->bbt_td->veroffs = 7;
  1157. this->bbt_td->maxblocks = INFTL_BBT_RESERVED_BLOCKS;
  1158. this->bbt_td->reserved_block_code = 0x01;
  1159. this->bbt_td->pattern = "MSYS_BBT";
  1160. this->bbt_md->options = NAND_BBT_LASTBLOCK | NAND_BBT_8BIT | NAND_BBT_VERSION;
  1161. if (inftl_bbt_write)
  1162. this->bbt_md->options |= NAND_BBT_WRITE;
  1163. this->bbt_md->offs = 8;
  1164. this->bbt_md->len = 8;
  1165. this->bbt_md->veroffs = 7;
  1166. this->bbt_md->maxblocks = INFTL_BBT_RESERVED_BLOCKS;
  1167. this->bbt_md->reserved_block_code = 0x01;
  1168. this->bbt_md->pattern = "TBB_SYSM";
  1169. }
  1170. ret = this->scan_bbt(mtd);
  1171. if (ret)
  1172. return ret;
  1173. memset((char *)parts, 0, sizeof(parts));
  1174. numparts = inftl_partscan(mtd, parts);
  1175. /* At least for now, require the INFTL Media Header. We could probably
  1176. do without it for non-INFTL use, since all it gives us is
  1177. autopartitioning, but I want to give it more thought. */
  1178. if (!numparts)
  1179. return -EIO;
  1180. return mtd_device_register(mtd, parts, no_autopart ? 0 : numparts);
  1181. }
  1182. static inline int __init doc2000_init(struct mtd_info *mtd)
  1183. {
  1184. struct nand_chip *this = mtd_to_nand(mtd);
  1185. struct doc_priv *doc = nand_get_controller_data(this);
  1186. this->read_byte = doc2000_read_byte;
  1187. this->write_buf = doc2000_writebuf;
  1188. this->read_buf = doc2000_readbuf;
  1189. doc->late_init = nftl_scan_bbt;
  1190. doc->CDSNControl = CDSN_CTRL_FLASH_IO | CDSN_CTRL_ECC_IO;
  1191. doc2000_count_chips(mtd);
  1192. mtd->name = "DiskOnChip 2000 (NFTL Model)";
  1193. return (4 * doc->chips_per_floor);
  1194. }
  1195. static inline int __init doc2001_init(struct mtd_info *mtd)
  1196. {
  1197. struct nand_chip *this = mtd_to_nand(mtd);
  1198. struct doc_priv *doc = nand_get_controller_data(this);
  1199. this->read_byte = doc2001_read_byte;
  1200. this->write_buf = doc2001_writebuf;
  1201. this->read_buf = doc2001_readbuf;
  1202. ReadDOC(doc->virtadr, ChipID);
  1203. ReadDOC(doc->virtadr, ChipID);
  1204. ReadDOC(doc->virtadr, ChipID);
  1205. if (ReadDOC(doc->virtadr, ChipID) != DOC_ChipID_DocMil) {
  1206. /* It's not a Millennium; it's one of the newer
  1207. DiskOnChip 2000 units with a similar ASIC.
  1208. Treat it like a Millennium, except that it
  1209. can have multiple chips. */
  1210. doc2000_count_chips(mtd);
  1211. mtd->name = "DiskOnChip 2000 (INFTL Model)";
  1212. doc->late_init = inftl_scan_bbt;
  1213. return (4 * doc->chips_per_floor);
  1214. } else {
  1215. /* Bog-standard Millennium */
  1216. doc->chips_per_floor = 1;
  1217. mtd->name = "DiskOnChip Millennium";
  1218. doc->late_init = nftl_scan_bbt;
  1219. return 1;
  1220. }
  1221. }
  1222. static inline int __init doc2001plus_init(struct mtd_info *mtd)
  1223. {
  1224. struct nand_chip *this = mtd_to_nand(mtd);
  1225. struct doc_priv *doc = nand_get_controller_data(this);
  1226. this->read_byte = doc2001plus_read_byte;
  1227. this->write_buf = doc2001plus_writebuf;
  1228. this->read_buf = doc2001plus_readbuf;
  1229. doc->late_init = inftl_scan_bbt;
  1230. this->cmd_ctrl = NULL;
  1231. this->select_chip = doc2001plus_select_chip;
  1232. this->cmdfunc = doc2001plus_command;
  1233. this->ecc.hwctl = doc2001plus_enable_hwecc;
  1234. doc->chips_per_floor = 1;
  1235. mtd->name = "DiskOnChip Millennium Plus";
  1236. return 1;
  1237. }
  1238. static int __init doc_probe(unsigned long physadr)
  1239. {
  1240. struct nand_chip *nand = NULL;
  1241. struct doc_priv *doc = NULL;
  1242. unsigned char ChipID;
  1243. struct mtd_info *mtd;
  1244. void __iomem *virtadr;
  1245. unsigned char save_control;
  1246. unsigned char tmp, tmpb, tmpc;
  1247. int reg, len, numchips;
  1248. int ret = 0;
  1249. if (!request_mem_region(physadr, DOC_IOREMAP_LEN, "DiskOnChip"))
  1250. return -EBUSY;
  1251. virtadr = ioremap(physadr, DOC_IOREMAP_LEN);
  1252. if (!virtadr) {
  1253. pr_err("Diskonchip ioremap failed: 0x%x bytes at 0x%lx\n",
  1254. DOC_IOREMAP_LEN, physadr);
  1255. ret = -EIO;
  1256. goto error_ioremap;
  1257. }
  1258. /* It's not possible to cleanly detect the DiskOnChip - the
  1259. * bootup procedure will put the device into reset mode, and
  1260. * it's not possible to talk to it without actually writing
  1261. * to the DOCControl register. So we store the current contents
  1262. * of the DOCControl register's location, in case we later decide
  1263. * that it's not a DiskOnChip, and want to put it back how we
  1264. * found it.
  1265. */
  1266. save_control = ReadDOC(virtadr, DOCControl);
  1267. /* Reset the DiskOnChip ASIC */
  1268. WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_RESET, virtadr, DOCControl);
  1269. WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_RESET, virtadr, DOCControl);
  1270. /* Enable the DiskOnChip ASIC */
  1271. WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_NORMAL, virtadr, DOCControl);
  1272. WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_NORMAL, virtadr, DOCControl);
  1273. ChipID = ReadDOC(virtadr, ChipID);
  1274. switch (ChipID) {
  1275. case DOC_ChipID_Doc2k:
  1276. reg = DoC_2k_ECCStatus;
  1277. break;
  1278. case DOC_ChipID_DocMil:
  1279. reg = DoC_ECCConf;
  1280. break;
  1281. case DOC_ChipID_DocMilPlus16:
  1282. case DOC_ChipID_DocMilPlus32:
  1283. case 0:
  1284. /* Possible Millennium Plus, need to do more checks */
  1285. /* Possibly release from power down mode */
  1286. for (tmp = 0; (tmp < 4); tmp++)
  1287. ReadDOC(virtadr, Mplus_Power);
  1288. /* Reset the Millennium Plus ASIC */
  1289. tmp = DOC_MODE_RESET | DOC_MODE_MDWREN | DOC_MODE_RST_LAT | DOC_MODE_BDECT;
  1290. WriteDOC(tmp, virtadr, Mplus_DOCControl);
  1291. WriteDOC(~tmp, virtadr, Mplus_CtrlConfirm);
  1292. usleep_range(1000, 2000);
  1293. /* Enable the Millennium Plus ASIC */
  1294. tmp = DOC_MODE_NORMAL | DOC_MODE_MDWREN | DOC_MODE_RST_LAT | DOC_MODE_BDECT;
  1295. WriteDOC(tmp, virtadr, Mplus_DOCControl);
  1296. WriteDOC(~tmp, virtadr, Mplus_CtrlConfirm);
  1297. usleep_range(1000, 2000);
  1298. ChipID = ReadDOC(virtadr, ChipID);
  1299. switch (ChipID) {
  1300. case DOC_ChipID_DocMilPlus16:
  1301. reg = DoC_Mplus_Toggle;
  1302. break;
  1303. case DOC_ChipID_DocMilPlus32:
  1304. pr_err("DiskOnChip Millennium Plus 32MB is not supported, ignoring.\n");
  1305. default:
  1306. ret = -ENODEV;
  1307. goto notfound;
  1308. }
  1309. break;
  1310. default:
  1311. ret = -ENODEV;
  1312. goto notfound;
  1313. }
  1314. /* Check the TOGGLE bit in the ECC register */
  1315. tmp = ReadDOC_(virtadr, reg) & DOC_TOGGLE_BIT;
  1316. tmpb = ReadDOC_(virtadr, reg) & DOC_TOGGLE_BIT;
  1317. tmpc = ReadDOC_(virtadr, reg) & DOC_TOGGLE_BIT;
  1318. if ((tmp == tmpb) || (tmp != tmpc)) {
  1319. pr_warn("Possible DiskOnChip at 0x%lx failed TOGGLE test, dropping.\n", physadr);
  1320. ret = -ENODEV;
  1321. goto notfound;
  1322. }
  1323. for (mtd = doclist; mtd; mtd = doc->nextdoc) {
  1324. unsigned char oldval;
  1325. unsigned char newval;
  1326. nand = mtd_to_nand(mtd);
  1327. doc = nand_get_controller_data(nand);
  1328. /* Use the alias resolution register to determine if this is
  1329. in fact the same DOC aliased to a new address. If writes
  1330. to one chip's alias resolution register change the value on
  1331. the other chip, they're the same chip. */
  1332. if (ChipID == DOC_ChipID_DocMilPlus16) {
  1333. oldval = ReadDOC(doc->virtadr, Mplus_AliasResolution);
  1334. newval = ReadDOC(virtadr, Mplus_AliasResolution);
  1335. } else {
  1336. oldval = ReadDOC(doc->virtadr, AliasResolution);
  1337. newval = ReadDOC(virtadr, AliasResolution);
  1338. }
  1339. if (oldval != newval)
  1340. continue;
  1341. if (ChipID == DOC_ChipID_DocMilPlus16) {
  1342. WriteDOC(~newval, virtadr, Mplus_AliasResolution);
  1343. oldval = ReadDOC(doc->virtadr, Mplus_AliasResolution);
  1344. WriteDOC(newval, virtadr, Mplus_AliasResolution); // restore it
  1345. } else {
  1346. WriteDOC(~newval, virtadr, AliasResolution);
  1347. oldval = ReadDOC(doc->virtadr, AliasResolution);
  1348. WriteDOC(newval, virtadr, AliasResolution); // restore it
  1349. }
  1350. newval = ~newval;
  1351. if (oldval == newval) {
  1352. pr_debug("Found alias of DOC at 0x%lx to 0x%lx\n",
  1353. doc->physadr, physadr);
  1354. goto notfound;
  1355. }
  1356. }
  1357. pr_notice("DiskOnChip found at 0x%lx\n", physadr);
  1358. len = sizeof(struct nand_chip) + sizeof(struct doc_priv) +
  1359. (2 * sizeof(struct nand_bbt_descr));
  1360. nand = kzalloc(len, GFP_KERNEL);
  1361. if (!nand) {
  1362. ret = -ENOMEM;
  1363. goto fail;
  1364. }
  1365. /*
  1366. * Allocate a RS codec instance
  1367. *
  1368. * Symbolsize is 10 (bits)
  1369. * Primitve polynomial is x^10+x^3+1
  1370. * First consecutive root is 510
  1371. * Primitve element to generate roots = 1
  1372. * Generator polinomial degree = 4
  1373. */
  1374. doc = (struct doc_priv *) (nand + 1);
  1375. doc->rs_decoder = init_rs(10, 0x409, FCR, 1, NROOTS);
  1376. if (!doc->rs_decoder) {
  1377. pr_err("DiskOnChip: Could not create a RS codec\n");
  1378. ret = -ENOMEM;
  1379. goto fail;
  1380. }
  1381. mtd = nand_to_mtd(nand);
  1382. nand->bbt_td = (struct nand_bbt_descr *) (doc + 1);
  1383. nand->bbt_md = nand->bbt_td + 1;
  1384. mtd->owner = THIS_MODULE;
  1385. mtd_set_ooblayout(mtd, &doc200x_ooblayout_ops);
  1386. nand_set_controller_data(nand, doc);
  1387. nand->select_chip = doc200x_select_chip;
  1388. nand->cmd_ctrl = doc200x_hwcontrol;
  1389. nand->dev_ready = doc200x_dev_ready;
  1390. nand->waitfunc = doc200x_wait;
  1391. nand->block_bad = doc200x_block_bad;
  1392. nand->ecc.hwctl = doc200x_enable_hwecc;
  1393. nand->ecc.calculate = doc200x_calculate_ecc;
  1394. nand->ecc.correct = doc200x_correct_data;
  1395. nand->ecc.mode = NAND_ECC_HW_SYNDROME;
  1396. nand->ecc.size = 512;
  1397. nand->ecc.bytes = 6;
  1398. nand->ecc.strength = 2;
  1399. nand->ecc.options = NAND_ECC_GENERIC_ERASED_CHECK;
  1400. nand->bbt_options = NAND_BBT_USE_FLASH;
  1401. /* Skip the automatic BBT scan so we can run it manually */
  1402. nand->options |= NAND_SKIP_BBTSCAN;
  1403. doc->physadr = physadr;
  1404. doc->virtadr = virtadr;
  1405. doc->ChipID = ChipID;
  1406. doc->curfloor = -1;
  1407. doc->curchip = -1;
  1408. doc->mh0_page = -1;
  1409. doc->mh1_page = -1;
  1410. doc->nextdoc = doclist;
  1411. if (ChipID == DOC_ChipID_Doc2k)
  1412. numchips = doc2000_init(mtd);
  1413. else if (ChipID == DOC_ChipID_DocMilPlus16)
  1414. numchips = doc2001plus_init(mtd);
  1415. else
  1416. numchips = doc2001_init(mtd);
  1417. if ((ret = nand_scan(mtd, numchips)) || (ret = doc->late_init(mtd))) {
  1418. /* DBB note: i believe nand_release is necessary here, as
  1419. buffers may have been allocated in nand_base. Check with
  1420. Thomas. FIX ME! */
  1421. /* nand_release will call mtd_device_unregister, but we
  1422. haven't yet added it. This is handled without incident by
  1423. mtd_device_unregister, as far as I can tell. */
  1424. nand_release(mtd);
  1425. goto fail;
  1426. }
  1427. /* Success! */
  1428. doclist = mtd;
  1429. return 0;
  1430. notfound:
  1431. /* Put back the contents of the DOCControl register, in case it's not
  1432. actually a DiskOnChip. */
  1433. WriteDOC(save_control, virtadr, DOCControl);
  1434. fail:
  1435. if (doc)
  1436. free_rs(doc->rs_decoder);
  1437. kfree(nand);
  1438. iounmap(virtadr);
  1439. error_ioremap:
  1440. release_mem_region(physadr, DOC_IOREMAP_LEN);
  1441. return ret;
  1442. }
  1443. static void release_nanddoc(void)
  1444. {
  1445. struct mtd_info *mtd, *nextmtd;
  1446. struct nand_chip *nand;
  1447. struct doc_priv *doc;
  1448. for (mtd = doclist; mtd; mtd = nextmtd) {
  1449. nand = mtd_to_nand(mtd);
  1450. doc = nand_get_controller_data(nand);
  1451. nextmtd = doc->nextdoc;
  1452. nand_release(mtd);
  1453. iounmap(doc->virtadr);
  1454. release_mem_region(doc->physadr, DOC_IOREMAP_LEN);
  1455. free_rs(doc->rs_decoder);
  1456. kfree(nand);
  1457. }
  1458. }
  1459. static int __init init_nanddoc(void)
  1460. {
  1461. int i, ret = 0;
  1462. if (doc_config_location) {
  1463. pr_info("Using configured DiskOnChip probe address 0x%lx\n",
  1464. doc_config_location);
  1465. ret = doc_probe(doc_config_location);
  1466. if (ret < 0)
  1467. return ret;
  1468. } else {
  1469. for (i = 0; (doc_locations[i] != 0xffffffff); i++) {
  1470. doc_probe(doc_locations[i]);
  1471. }
  1472. }
  1473. /* No banner message any more. Print a message if no DiskOnChip
  1474. found, so the user knows we at least tried. */
  1475. if (!doclist) {
  1476. pr_info("No valid DiskOnChip devices found\n");
  1477. ret = -ENODEV;
  1478. }
  1479. return ret;
  1480. }
  1481. static void __exit cleanup_nanddoc(void)
  1482. {
  1483. /* Cleanup the nand/DoC resources */
  1484. release_nanddoc();
  1485. }
  1486. module_init(init_nanddoc);
  1487. module_exit(cleanup_nanddoc);
  1488. MODULE_LICENSE("GPL");
  1489. MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
  1490. MODULE_DESCRIPTION("M-Systems DiskOnChip 2000, Millennium and Millennium Plus device driver");