diskonchip.c 49 KB

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