diskonchip.c 50 KB

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