diskonchip.c 50 KB

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