diskonchip.c 47 KB

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