diskonchip.c 50 KB

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