diskonchip.c 50 KB

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