diskonchip.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * (C) 2003 Red Hat, Inc.
  4. * (C) 2004 Dan Brown <[email protected]>
  5. * (C) 2004 Kalev Lember <[email protected]>
  6. *
  7. * Author: David Woodhouse <[email protected]>
  8. * Additional Diskonchip 2000 and Millennium support by Dan Brown <[email protected]>
  9. * Diskonchip Millennium Plus support by Kalev Lember <[email protected]>
  10. *
  11. * Error correction code lifted from the old docecc code
  12. * Author: Fabrice Bellard ([email protected])
  13. * Copyright (C) 2000 Netgem S.A.
  14. * converted to the generic Reed-Solomon library by Thomas Gleixner <[email protected]>
  15. *
  16. * Interface to generic NAND code for M-Systems DiskOnChip devices
  17. */
  18. #include <linux/kernel.h>
  19. #include <linux/init.h>
  20. #include <linux/sched.h>
  21. #include <linux/delay.h>
  22. #include <linux/rslib.h>
  23. #include <linux/moduleparam.h>
  24. #include <linux/slab.h>
  25. #include <linux/io.h>
  26. #include <linux/mtd/mtd.h>
  27. #include <linux/mtd/rawnand.h>
  28. #include <linux/mtd/doc2000.h>
  29. #include <linux/mtd/partitions.h>
  30. #include <linux/mtd/inftl.h>
  31. #include <linux/module.h>
  32. /* Where to look for the devices? */
  33. #ifndef CONFIG_MTD_NAND_DISKONCHIP_PROBE_ADDRESS
  34. #define CONFIG_MTD_NAND_DISKONCHIP_PROBE_ADDRESS 0
  35. #endif
  36. static unsigned long doc_locations[] __initdata = {
  37. #if defined (__alpha__) || defined(__i386__) || defined(__x86_64__)
  38. #ifdef CONFIG_MTD_NAND_DISKONCHIP_PROBE_HIGH
  39. 0xfffc8000, 0xfffca000, 0xfffcc000, 0xfffce000,
  40. 0xfffd0000, 0xfffd2000, 0xfffd4000, 0xfffd6000,
  41. 0xfffd8000, 0xfffda000, 0xfffdc000, 0xfffde000,
  42. 0xfffe0000, 0xfffe2000, 0xfffe4000, 0xfffe6000,
  43. 0xfffe8000, 0xfffea000, 0xfffec000, 0xfffee000,
  44. #else
  45. 0xc8000, 0xca000, 0xcc000, 0xce000,
  46. 0xd0000, 0xd2000, 0xd4000, 0xd6000,
  47. 0xd8000, 0xda000, 0xdc000, 0xde000,
  48. 0xe0000, 0xe2000, 0xe4000, 0xe6000,
  49. 0xe8000, 0xea000, 0xec000, 0xee000,
  50. #endif
  51. #endif
  52. 0xffffffff };
  53. static struct mtd_info *doclist = NULL;
  54. struct doc_priv {
  55. struct nand_controller base;
  56. void __iomem *virtadr;
  57. unsigned long physadr;
  58. u_char ChipID;
  59. u_char CDSNControl;
  60. int chips_per_floor; /* The number of chips detected on each floor */
  61. int curfloor;
  62. int curchip;
  63. int mh0_page;
  64. int mh1_page;
  65. struct rs_control *rs_decoder;
  66. struct mtd_info *nextdoc;
  67. bool supports_32b_reads;
  68. /* Handle the last stage of initialization (BBT scan, partitioning) */
  69. int (*late_init)(struct mtd_info *mtd);
  70. };
  71. /* This is the ecc value computed by the HW ecc generator upon writing an empty
  72. page, one with all 0xff for data. */
  73. static u_char empty_write_ecc[6] = { 0x4b, 0x00, 0xe2, 0x0e, 0x93, 0xf7 };
  74. #define INFTL_BBT_RESERVED_BLOCKS 4
  75. #define DoC_is_MillenniumPlus(doc) ((doc)->ChipID == DOC_ChipID_DocMilPlus16 || (doc)->ChipID == DOC_ChipID_DocMilPlus32)
  76. #define DoC_is_Millennium(doc) ((doc)->ChipID == DOC_ChipID_DocMil)
  77. #define DoC_is_2000(doc) ((doc)->ChipID == DOC_ChipID_Doc2k)
  78. static int debug = 0;
  79. module_param(debug, int, 0);
  80. static int try_dword = 1;
  81. module_param(try_dword, int, 0);
  82. static int no_ecc_failures = 0;
  83. module_param(no_ecc_failures, int, 0);
  84. static int no_autopart = 0;
  85. module_param(no_autopart, int, 0);
  86. static int show_firmware_partition = 0;
  87. module_param(show_firmware_partition, int, 0);
  88. #ifdef CONFIG_MTD_NAND_DISKONCHIP_BBTWRITE
  89. static int inftl_bbt_write = 1;
  90. #else
  91. static int inftl_bbt_write = 0;
  92. #endif
  93. module_param(inftl_bbt_write, int, 0);
  94. static unsigned long doc_config_location = CONFIG_MTD_NAND_DISKONCHIP_PROBE_ADDRESS;
  95. module_param(doc_config_location, ulong, 0);
  96. MODULE_PARM_DESC(doc_config_location, "Physical memory address at which to probe for DiskOnChip");
  97. /* Sector size for HW ECC */
  98. #define SECTOR_SIZE 512
  99. /* The sector bytes are packed into NB_DATA 10 bit words */
  100. #define NB_DATA (((SECTOR_SIZE + 1) * 8 + 6) / 10)
  101. /* Number of roots */
  102. #define NROOTS 4
  103. /* First consective root */
  104. #define FCR 510
  105. /* Number of symbols */
  106. #define NN 1023
  107. /*
  108. * The HW decoder in the DoC ASIC's provides us a error syndrome,
  109. * which we must convert to a standard syndrome usable by the generic
  110. * Reed-Solomon library code.
  111. *
  112. * Fabrice Bellard figured this out in the old docecc code. I added
  113. * some comments, improved a minor bit and converted it to make use
  114. * of the generic Reed-Solomon library. tglx
  115. */
  116. static int doc_ecc_decode(struct rs_control *rs, uint8_t *data, uint8_t *ecc)
  117. {
  118. int i, j, nerr, errpos[8];
  119. uint8_t parity;
  120. uint16_t ds[4], s[5], tmp, errval[8], syn[4];
  121. struct rs_codec *cd = rs->codec;
  122. memset(syn, 0, sizeof(syn));
  123. /* Convert the ecc bytes into words */
  124. ds[0] = ((ecc[4] & 0xff) >> 0) | ((ecc[5] & 0x03) << 8);
  125. ds[1] = ((ecc[5] & 0xfc) >> 2) | ((ecc[2] & 0x0f) << 6);
  126. ds[2] = ((ecc[2] & 0xf0) >> 4) | ((ecc[3] & 0x3f) << 4);
  127. ds[3] = ((ecc[3] & 0xc0) >> 6) | ((ecc[0] & 0xff) << 2);
  128. parity = ecc[1];
  129. /* Initialize the syndrome buffer */
  130. for (i = 0; i < NROOTS; i++)
  131. s[i] = ds[0];
  132. /*
  133. * Evaluate
  134. * s[i] = ds[3]x^3 + ds[2]x^2 + ds[1]x^1 + ds[0]
  135. * where x = alpha^(FCR + i)
  136. */
  137. for (j = 1; j < NROOTS; j++) {
  138. if (ds[j] == 0)
  139. continue;
  140. tmp = cd->index_of[ds[j]];
  141. for (i = 0; i < NROOTS; i++)
  142. s[i] ^= cd->alpha_to[rs_modnn(cd, tmp + (FCR + i) * j)];
  143. }
  144. /* Calc syn[i] = s[i] / alpha^(v + i) */
  145. for (i = 0; i < NROOTS; i++) {
  146. if (s[i])
  147. syn[i] = rs_modnn(cd, cd->index_of[s[i]] + (NN - FCR - i));
  148. }
  149. /* Call the decoder library */
  150. nerr = decode_rs16(rs, NULL, NULL, 1019, syn, 0, errpos, 0, errval);
  151. /* Incorrectable errors ? */
  152. if (nerr < 0)
  153. return nerr;
  154. /*
  155. * Correct the errors. The bitpositions are a bit of magic,
  156. * but they are given by the design of the de/encoder circuit
  157. * in the DoC ASIC's.
  158. */
  159. for (i = 0; i < nerr; i++) {
  160. int index, bitpos, pos = 1015 - errpos[i];
  161. uint8_t val;
  162. if (pos >= NB_DATA && pos < 1019)
  163. continue;
  164. if (pos < NB_DATA) {
  165. /* extract bit position (MSB first) */
  166. pos = 10 * (NB_DATA - 1 - pos) - 6;
  167. /* now correct the following 10 bits. At most two bytes
  168. can be modified since pos is even */
  169. index = (pos >> 3) ^ 1;
  170. bitpos = pos & 7;
  171. if ((index >= 0 && index < SECTOR_SIZE) || index == (SECTOR_SIZE + 1)) {
  172. val = (uint8_t) (errval[i] >> (2 + bitpos));
  173. parity ^= val;
  174. if (index < SECTOR_SIZE)
  175. data[index] ^= val;
  176. }
  177. index = ((pos >> 3) + 1) ^ 1;
  178. bitpos = (bitpos + 10) & 7;
  179. if (bitpos == 0)
  180. bitpos = 8;
  181. if ((index >= 0 && index < SECTOR_SIZE) || index == (SECTOR_SIZE + 1)) {
  182. val = (uint8_t) (errval[i] << (8 - bitpos));
  183. parity ^= val;
  184. if (index < SECTOR_SIZE)
  185. data[index] ^= val;
  186. }
  187. }
  188. }
  189. /* If the parity is wrong, no rescue possible */
  190. return parity ? -EBADMSG : nerr;
  191. }
  192. static void DoC_Delay(struct doc_priv *doc, unsigned short cycles)
  193. {
  194. volatile char __always_unused dummy;
  195. int i;
  196. for (i = 0; i < cycles; i++) {
  197. if (DoC_is_Millennium(doc))
  198. dummy = ReadDOC(doc->virtadr, NOP);
  199. else if (DoC_is_MillenniumPlus(doc))
  200. dummy = ReadDOC(doc->virtadr, Mplus_NOP);
  201. else
  202. dummy = ReadDOC(doc->virtadr, DOCStatus);
  203. }
  204. }
  205. #define CDSN_CTRL_FR_B_MASK (CDSN_CTRL_FR_B0 | CDSN_CTRL_FR_B1)
  206. /* DOC_WaitReady: Wait for RDY line to be asserted by the flash chip */
  207. static int _DoC_WaitReady(struct doc_priv *doc)
  208. {
  209. void __iomem *docptr = doc->virtadr;
  210. unsigned long timeo = jiffies + (HZ * 10);
  211. if (debug)
  212. printk("_DoC_WaitReady...\n");
  213. /* Out-of-line routine to wait for chip response */
  214. if (DoC_is_MillenniumPlus(doc)) {
  215. while ((ReadDOC(docptr, Mplus_FlashControl) & CDSN_CTRL_FR_B_MASK) != CDSN_CTRL_FR_B_MASK) {
  216. if (time_after(jiffies, timeo)) {
  217. printk("_DoC_WaitReady timed out.\n");
  218. return -EIO;
  219. }
  220. udelay(1);
  221. cond_resched();
  222. }
  223. } else {
  224. while (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B)) {
  225. if (time_after(jiffies, timeo)) {
  226. printk("_DoC_WaitReady timed out.\n");
  227. return -EIO;
  228. }
  229. udelay(1);
  230. cond_resched();
  231. }
  232. }
  233. return 0;
  234. }
  235. static inline int DoC_WaitReady(struct doc_priv *doc)
  236. {
  237. void __iomem *docptr = doc->virtadr;
  238. int ret = 0;
  239. if (DoC_is_MillenniumPlus(doc)) {
  240. DoC_Delay(doc, 4);
  241. if ((ReadDOC(docptr, Mplus_FlashControl) & CDSN_CTRL_FR_B_MASK) != CDSN_CTRL_FR_B_MASK)
  242. /* Call the out-of-line routine to wait */
  243. ret = _DoC_WaitReady(doc);
  244. } else {
  245. DoC_Delay(doc, 4);
  246. if (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B))
  247. /* Call the out-of-line routine to wait */
  248. ret = _DoC_WaitReady(doc);
  249. DoC_Delay(doc, 2);
  250. }
  251. if (debug)
  252. printk("DoC_WaitReady OK\n");
  253. return ret;
  254. }
  255. static void doc2000_write_byte(struct nand_chip *this, u_char datum)
  256. {
  257. struct doc_priv *doc = nand_get_controller_data(this);
  258. void __iomem *docptr = doc->virtadr;
  259. if (debug)
  260. printk("write_byte %02x\n", datum);
  261. WriteDOC(datum, docptr, CDSNSlowIO);
  262. WriteDOC(datum, docptr, 2k_CDSN_IO);
  263. }
  264. static void doc2000_writebuf(struct nand_chip *this, const u_char *buf,
  265. int len)
  266. {
  267. struct doc_priv *doc = nand_get_controller_data(this);
  268. void __iomem *docptr = doc->virtadr;
  269. int i;
  270. if (debug)
  271. printk("writebuf of %d bytes: ", len);
  272. for (i = 0; i < len; i++) {
  273. WriteDOC_(buf[i], docptr, DoC_2k_CDSN_IO + i);
  274. if (debug && i < 16)
  275. printk("%02x ", buf[i]);
  276. }
  277. if (debug)
  278. printk("\n");
  279. }
  280. static void doc2000_readbuf(struct nand_chip *this, u_char *buf, int len)
  281. {
  282. struct doc_priv *doc = nand_get_controller_data(this);
  283. void __iomem *docptr = doc->virtadr;
  284. u32 *buf32 = (u32 *)buf;
  285. int i;
  286. if (debug)
  287. printk("readbuf of %d bytes: ", len);
  288. if (!doc->supports_32b_reads ||
  289. ((((unsigned long)buf) | len) & 3)) {
  290. for (i = 0; i < len; i++)
  291. buf[i] = ReadDOC(docptr, 2k_CDSN_IO + i);
  292. } else {
  293. for (i = 0; i < len / 4; i++)
  294. buf32[i] = readl(docptr + DoC_2k_CDSN_IO + i);
  295. }
  296. }
  297. /*
  298. * We need our own readid() here because it's called before the NAND chip
  299. * has been initialized, and calling nand_op_readid() would lead to a NULL
  300. * pointer exception when dereferencing the NAND timings.
  301. */
  302. static void doc200x_readid(struct nand_chip *this, unsigned int cs, u8 *id)
  303. {
  304. u8 addr = 0;
  305. struct nand_op_instr instrs[] = {
  306. NAND_OP_CMD(NAND_CMD_READID, 0),
  307. NAND_OP_ADDR(1, &addr, 50),
  308. NAND_OP_8BIT_DATA_IN(2, id, 0),
  309. };
  310. struct nand_operation op = NAND_OPERATION(cs, instrs);
  311. if (!id)
  312. op.ninstrs--;
  313. this->controller->ops->exec_op(this, &op, false);
  314. }
  315. static uint16_t __init doc200x_ident_chip(struct mtd_info *mtd, int nr)
  316. {
  317. struct nand_chip *this = mtd_to_nand(mtd);
  318. struct doc_priv *doc = nand_get_controller_data(this);
  319. uint16_t ret;
  320. u8 id[2];
  321. doc200x_readid(this, nr, id);
  322. ret = ((u16)id[0] << 8) | id[1];
  323. if (doc->ChipID == DOC_ChipID_Doc2k && try_dword && !nr) {
  324. /* First chip probe. See if we get same results by 32-bit access */
  325. union {
  326. uint32_t dword;
  327. uint8_t byte[4];
  328. } ident;
  329. void __iomem *docptr = doc->virtadr;
  330. doc200x_readid(this, nr, NULL);
  331. ident.dword = readl(docptr + DoC_2k_CDSN_IO);
  332. if (((ident.byte[0] << 8) | ident.byte[1]) == ret) {
  333. pr_info("DiskOnChip 2000 responds to DWORD access\n");
  334. doc->supports_32b_reads = true;
  335. }
  336. }
  337. return ret;
  338. }
  339. static void __init doc2000_count_chips(struct mtd_info *mtd)
  340. {
  341. struct nand_chip *this = mtd_to_nand(mtd);
  342. struct doc_priv *doc = nand_get_controller_data(this);
  343. uint16_t mfrid;
  344. int i;
  345. /* Max 4 chips per floor on DiskOnChip 2000 */
  346. doc->chips_per_floor = 4;
  347. /* Find out what the first chip is */
  348. mfrid = doc200x_ident_chip(mtd, 0);
  349. /* Find how many chips in each floor. */
  350. for (i = 1; i < 4; i++) {
  351. if (doc200x_ident_chip(mtd, i) != mfrid)
  352. break;
  353. }
  354. doc->chips_per_floor = i;
  355. pr_debug("Detected %d chips per floor.\n", i);
  356. }
  357. static void doc2001_write_byte(struct nand_chip *this, u_char datum)
  358. {
  359. struct doc_priv *doc = nand_get_controller_data(this);
  360. void __iomem *docptr = doc->virtadr;
  361. WriteDOC(datum, docptr, CDSNSlowIO);
  362. WriteDOC(datum, docptr, Mil_CDSN_IO);
  363. WriteDOC(datum, docptr, WritePipeTerm);
  364. }
  365. static void doc2001_writebuf(struct nand_chip *this, const u_char *buf, int len)
  366. {
  367. struct doc_priv *doc = nand_get_controller_data(this);
  368. void __iomem *docptr = doc->virtadr;
  369. int i;
  370. for (i = 0; i < len; i++)
  371. WriteDOC_(buf[i], docptr, DoC_Mil_CDSN_IO + i);
  372. /* Terminate write pipeline */
  373. WriteDOC(0x00, docptr, WritePipeTerm);
  374. }
  375. static void doc2001_readbuf(struct nand_chip *this, u_char *buf, int len)
  376. {
  377. struct doc_priv *doc = nand_get_controller_data(this);
  378. void __iomem *docptr = doc->virtadr;
  379. int i;
  380. /* Start read pipeline */
  381. ReadDOC(docptr, ReadPipeInit);
  382. for (i = 0; i < len - 1; i++)
  383. buf[i] = ReadDOC(docptr, Mil_CDSN_IO + (i & 0xff));
  384. /* Terminate read pipeline */
  385. buf[i] = ReadDOC(docptr, LastDataRead);
  386. }
  387. static void doc2001plus_writebuf(struct nand_chip *this, const u_char *buf, int len)
  388. {
  389. struct doc_priv *doc = nand_get_controller_data(this);
  390. void __iomem *docptr = doc->virtadr;
  391. int i;
  392. if (debug)
  393. printk("writebuf of %d bytes: ", len);
  394. for (i = 0; i < len; i++) {
  395. WriteDOC_(buf[i], docptr, DoC_Mil_CDSN_IO + i);
  396. if (debug && i < 16)
  397. printk("%02x ", buf[i]);
  398. }
  399. if (debug)
  400. printk("\n");
  401. }
  402. static void doc2001plus_readbuf(struct nand_chip *this, u_char *buf, int len)
  403. {
  404. struct doc_priv *doc = nand_get_controller_data(this);
  405. void __iomem *docptr = doc->virtadr;
  406. int i;
  407. if (debug)
  408. printk("readbuf of %d bytes: ", len);
  409. /* Start read pipeline */
  410. ReadDOC(docptr, Mplus_ReadPipeInit);
  411. ReadDOC(docptr, Mplus_ReadPipeInit);
  412. for (i = 0; i < len - 2; i++) {
  413. buf[i] = ReadDOC(docptr, Mil_CDSN_IO);
  414. if (debug && i < 16)
  415. printk("%02x ", buf[i]);
  416. }
  417. /* Terminate read pipeline */
  418. if (len >= 2) {
  419. buf[len - 2] = ReadDOC(docptr, Mplus_LastDataRead);
  420. if (debug && i < 16)
  421. printk("%02x ", buf[len - 2]);
  422. }
  423. buf[len - 1] = ReadDOC(docptr, Mplus_LastDataRead);
  424. if (debug && i < 16)
  425. printk("%02x ", buf[len - 1]);
  426. if (debug)
  427. printk("\n");
  428. }
  429. static void doc200x_write_control(struct doc_priv *doc, u8 value)
  430. {
  431. WriteDOC(value, doc->virtadr, CDSNControl);
  432. /* 11.4.3 -- 4 NOPs after CSDNControl write */
  433. DoC_Delay(doc, 4);
  434. }
  435. static void doc200x_exec_instr(struct nand_chip *this,
  436. const struct nand_op_instr *instr)
  437. {
  438. struct doc_priv *doc = nand_get_controller_data(this);
  439. unsigned int i;
  440. switch (instr->type) {
  441. case NAND_OP_CMD_INSTR:
  442. doc200x_write_control(doc, CDSN_CTRL_CE | CDSN_CTRL_CLE);
  443. doc2000_write_byte(this, instr->ctx.cmd.opcode);
  444. break;
  445. case NAND_OP_ADDR_INSTR:
  446. doc200x_write_control(doc, CDSN_CTRL_CE | CDSN_CTRL_ALE);
  447. for (i = 0; i < instr->ctx.addr.naddrs; i++) {
  448. u8 addr = instr->ctx.addr.addrs[i];
  449. if (DoC_is_2000(doc))
  450. doc2000_write_byte(this, addr);
  451. else
  452. doc2001_write_byte(this, addr);
  453. }
  454. break;
  455. case NAND_OP_DATA_IN_INSTR:
  456. doc200x_write_control(doc, CDSN_CTRL_CE);
  457. if (DoC_is_2000(doc))
  458. doc2000_readbuf(this, instr->ctx.data.buf.in,
  459. instr->ctx.data.len);
  460. else
  461. doc2001_readbuf(this, instr->ctx.data.buf.in,
  462. instr->ctx.data.len);
  463. break;
  464. case NAND_OP_DATA_OUT_INSTR:
  465. doc200x_write_control(doc, CDSN_CTRL_CE);
  466. if (DoC_is_2000(doc))
  467. doc2000_writebuf(this, instr->ctx.data.buf.out,
  468. instr->ctx.data.len);
  469. else
  470. doc2001_writebuf(this, instr->ctx.data.buf.out,
  471. instr->ctx.data.len);
  472. break;
  473. case NAND_OP_WAITRDY_INSTR:
  474. DoC_WaitReady(doc);
  475. break;
  476. }
  477. if (instr->delay_ns)
  478. ndelay(instr->delay_ns);
  479. }
  480. static int doc200x_exec_op(struct nand_chip *this,
  481. const struct nand_operation *op,
  482. bool check_only)
  483. {
  484. struct doc_priv *doc = nand_get_controller_data(this);
  485. unsigned int i;
  486. if (check_only)
  487. return true;
  488. doc->curchip = op->cs % doc->chips_per_floor;
  489. doc->curfloor = op->cs / doc->chips_per_floor;
  490. WriteDOC(doc->curfloor, doc->virtadr, FloorSelect);
  491. WriteDOC(doc->curchip, doc->virtadr, CDSNDeviceSelect);
  492. /* Assert CE pin */
  493. doc200x_write_control(doc, CDSN_CTRL_CE);
  494. for (i = 0; i < op->ninstrs; i++)
  495. doc200x_exec_instr(this, &op->instrs[i]);
  496. /* De-assert CE pin */
  497. doc200x_write_control(doc, 0);
  498. return 0;
  499. }
  500. static void doc2001plus_write_pipe_term(struct doc_priv *doc)
  501. {
  502. WriteDOC(0x00, doc->virtadr, Mplus_WritePipeTerm);
  503. WriteDOC(0x00, doc->virtadr, Mplus_WritePipeTerm);
  504. }
  505. static void doc2001plus_exec_instr(struct nand_chip *this,
  506. const struct nand_op_instr *instr)
  507. {
  508. struct doc_priv *doc = nand_get_controller_data(this);
  509. unsigned int i;
  510. switch (instr->type) {
  511. case NAND_OP_CMD_INSTR:
  512. WriteDOC(instr->ctx.cmd.opcode, doc->virtadr, Mplus_FlashCmd);
  513. doc2001plus_write_pipe_term(doc);
  514. break;
  515. case NAND_OP_ADDR_INSTR:
  516. for (i = 0; i < instr->ctx.addr.naddrs; i++) {
  517. u8 addr = instr->ctx.addr.addrs[i];
  518. WriteDOC(addr, doc->virtadr, Mplus_FlashAddress);
  519. }
  520. doc2001plus_write_pipe_term(doc);
  521. /* deassert ALE */
  522. WriteDOC(0, doc->virtadr, Mplus_FlashControl);
  523. break;
  524. case NAND_OP_DATA_IN_INSTR:
  525. doc2001plus_readbuf(this, instr->ctx.data.buf.in,
  526. instr->ctx.data.len);
  527. break;
  528. case NAND_OP_DATA_OUT_INSTR:
  529. doc2001plus_writebuf(this, instr->ctx.data.buf.out,
  530. instr->ctx.data.len);
  531. doc2001plus_write_pipe_term(doc);
  532. break;
  533. case NAND_OP_WAITRDY_INSTR:
  534. DoC_WaitReady(doc);
  535. break;
  536. }
  537. if (instr->delay_ns)
  538. ndelay(instr->delay_ns);
  539. }
  540. static int doc2001plus_exec_op(struct nand_chip *this,
  541. const struct nand_operation *op,
  542. bool check_only)
  543. {
  544. struct doc_priv *doc = nand_get_controller_data(this);
  545. unsigned int i;
  546. if (check_only)
  547. return true;
  548. doc->curchip = op->cs % doc->chips_per_floor;
  549. doc->curfloor = op->cs / doc->chips_per_floor;
  550. /* Assert ChipEnable and deassert WriteProtect */
  551. WriteDOC(DOC_FLASH_CE, doc->virtadr, Mplus_FlashSelect);
  552. for (i = 0; i < op->ninstrs; i++)
  553. doc2001plus_exec_instr(this, &op->instrs[i]);
  554. /* De-assert ChipEnable */
  555. WriteDOC(0, doc->virtadr, Mplus_FlashSelect);
  556. return 0;
  557. }
  558. static void doc200x_enable_hwecc(struct nand_chip *this, int mode)
  559. {
  560. struct doc_priv *doc = nand_get_controller_data(this);
  561. void __iomem *docptr = doc->virtadr;
  562. /* Prime the ECC engine */
  563. switch (mode) {
  564. case NAND_ECC_READ:
  565. WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
  566. WriteDOC(DOC_ECC_EN, docptr, ECCConf);
  567. break;
  568. case NAND_ECC_WRITE:
  569. WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
  570. WriteDOC(DOC_ECC_EN | DOC_ECC_RW, docptr, ECCConf);
  571. break;
  572. }
  573. }
  574. static void doc2001plus_enable_hwecc(struct nand_chip *this, int mode)
  575. {
  576. struct doc_priv *doc = nand_get_controller_data(this);
  577. void __iomem *docptr = doc->virtadr;
  578. /* Prime the ECC engine */
  579. switch (mode) {
  580. case NAND_ECC_READ:
  581. WriteDOC(DOC_ECC_RESET, docptr, Mplus_ECCConf);
  582. WriteDOC(DOC_ECC_EN, docptr, Mplus_ECCConf);
  583. break;
  584. case NAND_ECC_WRITE:
  585. WriteDOC(DOC_ECC_RESET, docptr, Mplus_ECCConf);
  586. WriteDOC(DOC_ECC_EN | DOC_ECC_RW, docptr, Mplus_ECCConf);
  587. break;
  588. }
  589. }
  590. /* This code is only called on write */
  591. static int doc200x_calculate_ecc(struct nand_chip *this, const u_char *dat,
  592. unsigned char *ecc_code)
  593. {
  594. struct doc_priv *doc = nand_get_controller_data(this);
  595. void __iomem *docptr = doc->virtadr;
  596. int i;
  597. int __always_unused emptymatch = 1;
  598. /* flush the pipeline */
  599. if (DoC_is_2000(doc)) {
  600. WriteDOC(doc->CDSNControl & ~CDSN_CTRL_FLASH_IO, docptr, CDSNControl);
  601. WriteDOC(0, docptr, 2k_CDSN_IO);
  602. WriteDOC(0, docptr, 2k_CDSN_IO);
  603. WriteDOC(0, docptr, 2k_CDSN_IO);
  604. WriteDOC(doc->CDSNControl, docptr, CDSNControl);
  605. } else if (DoC_is_MillenniumPlus(doc)) {
  606. WriteDOC(0, docptr, Mplus_NOP);
  607. WriteDOC(0, docptr, Mplus_NOP);
  608. WriteDOC(0, docptr, Mplus_NOP);
  609. } else {
  610. WriteDOC(0, docptr, NOP);
  611. WriteDOC(0, docptr, NOP);
  612. WriteDOC(0, docptr, NOP);
  613. }
  614. for (i = 0; i < 6; i++) {
  615. if (DoC_is_MillenniumPlus(doc))
  616. ecc_code[i] = ReadDOC_(docptr, DoC_Mplus_ECCSyndrome0 + i);
  617. else
  618. ecc_code[i] = ReadDOC_(docptr, DoC_ECCSyndrome0 + i);
  619. if (ecc_code[i] != empty_write_ecc[i])
  620. emptymatch = 0;
  621. }
  622. if (DoC_is_MillenniumPlus(doc))
  623. WriteDOC(DOC_ECC_DIS, docptr, Mplus_ECCConf);
  624. else
  625. WriteDOC(DOC_ECC_DIS, docptr, ECCConf);
  626. #if 0
  627. /* If emptymatch=1, we might have an all-0xff data buffer. Check. */
  628. if (emptymatch) {
  629. /* Note: this somewhat expensive test should not be triggered
  630. often. It could be optimized away by examining the data in
  631. the writebuf routine, and remembering the result. */
  632. for (i = 0; i < 512; i++) {
  633. if (dat[i] == 0xff)
  634. continue;
  635. emptymatch = 0;
  636. break;
  637. }
  638. }
  639. /* If emptymatch still =1, we do have an all-0xff data buffer.
  640. Return all-0xff ecc value instead of the computed one, so
  641. it'll look just like a freshly-erased page. */
  642. if (emptymatch)
  643. memset(ecc_code, 0xff, 6);
  644. #endif
  645. return 0;
  646. }
  647. static int doc200x_correct_data(struct nand_chip *this, u_char *dat,
  648. u_char *read_ecc, u_char *isnull)
  649. {
  650. int i, ret = 0;
  651. struct doc_priv *doc = nand_get_controller_data(this);
  652. void __iomem *docptr = doc->virtadr;
  653. uint8_t calc_ecc[6];
  654. volatile u_char dummy;
  655. /* flush the pipeline */
  656. if (DoC_is_2000(doc)) {
  657. dummy = ReadDOC(docptr, 2k_ECCStatus);
  658. dummy = ReadDOC(docptr, 2k_ECCStatus);
  659. dummy = ReadDOC(docptr, 2k_ECCStatus);
  660. } else if (DoC_is_MillenniumPlus(doc)) {
  661. dummy = ReadDOC(docptr, Mplus_ECCConf);
  662. dummy = ReadDOC(docptr, Mplus_ECCConf);
  663. dummy = ReadDOC(docptr, Mplus_ECCConf);
  664. } else {
  665. dummy = ReadDOC(docptr, ECCConf);
  666. dummy = ReadDOC(docptr, ECCConf);
  667. dummy = ReadDOC(docptr, ECCConf);
  668. }
  669. /* Error occurred ? */
  670. if (dummy & 0x80) {
  671. for (i = 0; i < 6; i++) {
  672. if (DoC_is_MillenniumPlus(doc))
  673. calc_ecc[i] = ReadDOC_(docptr, DoC_Mplus_ECCSyndrome0 + i);
  674. else
  675. calc_ecc[i] = ReadDOC_(docptr, DoC_ECCSyndrome0 + i);
  676. }
  677. ret = doc_ecc_decode(doc->rs_decoder, dat, calc_ecc);
  678. if (ret > 0)
  679. pr_err("doc200x_correct_data corrected %d errors\n",
  680. ret);
  681. }
  682. if (DoC_is_MillenniumPlus(doc))
  683. WriteDOC(DOC_ECC_DIS, docptr, Mplus_ECCConf);
  684. else
  685. WriteDOC(DOC_ECC_DIS, docptr, ECCConf);
  686. if (no_ecc_failures && mtd_is_eccerr(ret)) {
  687. pr_err("suppressing ECC failure\n");
  688. ret = 0;
  689. }
  690. return ret;
  691. }
  692. //u_char mydatabuf[528];
  693. static int doc200x_ooblayout_ecc(struct mtd_info *mtd, int section,
  694. struct mtd_oob_region *oobregion)
  695. {
  696. if (section)
  697. return -ERANGE;
  698. oobregion->offset = 0;
  699. oobregion->length = 6;
  700. return 0;
  701. }
  702. static int doc200x_ooblayout_free(struct mtd_info *mtd, int section,
  703. struct mtd_oob_region *oobregion)
  704. {
  705. if (section > 1)
  706. return -ERANGE;
  707. /*
  708. * The strange out-of-order free bytes definition is a (possibly
  709. * unneeded) attempt to retain compatibility. It used to read:
  710. * .oobfree = { {8, 8} }
  711. * Since that leaves two bytes unusable, it was changed. But the
  712. * following scheme might affect existing jffs2 installs by moving the
  713. * cleanmarker:
  714. * .oobfree = { {6, 10} }
  715. * jffs2 seems to handle the above gracefully, but the current scheme
  716. * seems safer. The only problem with it is that any code retrieving
  717. * free bytes position must be able to handle out-of-order segments.
  718. */
  719. if (!section) {
  720. oobregion->offset = 8;
  721. oobregion->length = 8;
  722. } else {
  723. oobregion->offset = 6;
  724. oobregion->length = 2;
  725. }
  726. return 0;
  727. }
  728. static const struct mtd_ooblayout_ops doc200x_ooblayout_ops = {
  729. .ecc = doc200x_ooblayout_ecc,
  730. .free = doc200x_ooblayout_free,
  731. };
  732. /* Find the (I)NFTL Media Header, and optionally also the mirror media header.
  733. On successful return, buf will contain a copy of the media header for
  734. further processing. id is the string to scan for, and will presumably be
  735. either "ANAND" or "BNAND". If findmirror=1, also look for the mirror media
  736. header. The page #s of the found media headers are placed in mh0_page and
  737. mh1_page in the DOC private structure. */
  738. static int __init find_media_headers(struct mtd_info *mtd, u_char *buf, const char *id, int findmirror)
  739. {
  740. struct nand_chip *this = mtd_to_nand(mtd);
  741. struct doc_priv *doc = nand_get_controller_data(this);
  742. unsigned offs;
  743. int ret;
  744. size_t retlen;
  745. for (offs = 0; offs < mtd->size; offs += mtd->erasesize) {
  746. ret = mtd_read(mtd, offs, mtd->writesize, &retlen, buf);
  747. if (retlen != mtd->writesize)
  748. continue;
  749. if (ret) {
  750. pr_warn("ECC error scanning DOC at 0x%x\n", offs);
  751. }
  752. if (memcmp(buf, id, 6))
  753. continue;
  754. pr_info("Found DiskOnChip %s Media Header at 0x%x\n", id, offs);
  755. if (doc->mh0_page == -1) {
  756. doc->mh0_page = offs >> this->page_shift;
  757. if (!findmirror)
  758. return 1;
  759. continue;
  760. }
  761. doc->mh1_page = offs >> this->page_shift;
  762. return 2;
  763. }
  764. if (doc->mh0_page == -1) {
  765. pr_warn("DiskOnChip %s Media Header not found.\n", id);
  766. return 0;
  767. }
  768. /* Only one mediaheader was found. We want buf to contain a
  769. mediaheader on return, so we'll have to re-read the one we found. */
  770. offs = doc->mh0_page << this->page_shift;
  771. ret = mtd_read(mtd, offs, mtd->writesize, &retlen, buf);
  772. if (retlen != mtd->writesize) {
  773. /* Insanity. Give up. */
  774. pr_err("Read DiskOnChip Media Header once, but can't reread it???\n");
  775. return 0;
  776. }
  777. return 1;
  778. }
  779. static inline int __init nftl_partscan(struct mtd_info *mtd, struct mtd_partition *parts)
  780. {
  781. struct nand_chip *this = mtd_to_nand(mtd);
  782. struct doc_priv *doc = nand_get_controller_data(this);
  783. struct nand_memory_organization *memorg;
  784. int ret = 0;
  785. u_char *buf;
  786. struct NFTLMediaHeader *mh;
  787. const unsigned psize = 1 << this->page_shift;
  788. int numparts = 0;
  789. unsigned blocks, maxblocks;
  790. int offs, numheaders;
  791. memorg = nanddev_get_memorg(&this->base);
  792. buf = kmalloc(mtd->writesize, GFP_KERNEL);
  793. if (!buf) {
  794. return 0;
  795. }
  796. if (!(numheaders = find_media_headers(mtd, buf, "ANAND", 1)))
  797. goto out;
  798. mh = (struct NFTLMediaHeader *)buf;
  799. le16_to_cpus(&mh->NumEraseUnits);
  800. le16_to_cpus(&mh->FirstPhysicalEUN);
  801. le32_to_cpus(&mh->FormattedSize);
  802. pr_info(" DataOrgID = %s\n"
  803. " NumEraseUnits = %d\n"
  804. " FirstPhysicalEUN = %d\n"
  805. " FormattedSize = %d\n"
  806. " UnitSizeFactor = %d\n",
  807. mh->DataOrgID, mh->NumEraseUnits,
  808. mh->FirstPhysicalEUN, mh->FormattedSize,
  809. mh->UnitSizeFactor);
  810. blocks = mtd->size >> this->phys_erase_shift;
  811. maxblocks = min(32768U, mtd->erasesize - psize);
  812. if (mh->UnitSizeFactor == 0x00) {
  813. /* Auto-determine UnitSizeFactor. The constraints are:
  814. - There can be at most 32768 virtual blocks.
  815. - There can be at most (virtual block size - page size)
  816. virtual blocks (because MediaHeader+BBT must fit in 1).
  817. */
  818. mh->UnitSizeFactor = 0xff;
  819. while (blocks > maxblocks) {
  820. blocks >>= 1;
  821. maxblocks = min(32768U, (maxblocks << 1) + psize);
  822. mh->UnitSizeFactor--;
  823. }
  824. pr_warn("UnitSizeFactor=0x00 detected. Correct value is assumed to be 0x%02x.\n", mh->UnitSizeFactor);
  825. }
  826. /* NOTE: The lines below modify internal variables of the NAND and MTD
  827. layers; variables with have already been configured by nand_scan.
  828. Unfortunately, we didn't know before this point what these values
  829. should be. Thus, this code is somewhat dependent on the exact
  830. implementation of the NAND layer. */
  831. if (mh->UnitSizeFactor != 0xff) {
  832. this->bbt_erase_shift += (0xff - mh->UnitSizeFactor);
  833. memorg->pages_per_eraseblock <<= (0xff - mh->UnitSizeFactor);
  834. mtd->erasesize <<= (0xff - mh->UnitSizeFactor);
  835. pr_info("Setting virtual erase size to %d\n", mtd->erasesize);
  836. blocks = mtd->size >> this->bbt_erase_shift;
  837. maxblocks = min(32768U, mtd->erasesize - psize);
  838. }
  839. if (blocks > maxblocks) {
  840. pr_err("UnitSizeFactor of 0x%02x is inconsistent with device size. Aborting.\n", mh->UnitSizeFactor);
  841. goto out;
  842. }
  843. /* Skip past the media headers. */
  844. offs = max(doc->mh0_page, doc->mh1_page);
  845. offs <<= this->page_shift;
  846. offs += mtd->erasesize;
  847. if (show_firmware_partition == 1) {
  848. parts[0].name = " DiskOnChip Firmware / Media Header partition";
  849. parts[0].offset = 0;
  850. parts[0].size = offs;
  851. numparts = 1;
  852. }
  853. parts[numparts].name = " DiskOnChip BDTL partition";
  854. parts[numparts].offset = offs;
  855. parts[numparts].size = (mh->NumEraseUnits - numheaders) << this->bbt_erase_shift;
  856. offs += parts[numparts].size;
  857. numparts++;
  858. if (offs < mtd->size) {
  859. parts[numparts].name = " DiskOnChip Remainder partition";
  860. parts[numparts].offset = offs;
  861. parts[numparts].size = mtd->size - offs;
  862. numparts++;
  863. }
  864. ret = numparts;
  865. out:
  866. kfree(buf);
  867. return ret;
  868. }
  869. /* This is a stripped-down copy of the code in inftlmount.c */
  870. static inline int __init inftl_partscan(struct mtd_info *mtd, struct mtd_partition *parts)
  871. {
  872. struct nand_chip *this = mtd_to_nand(mtd);
  873. struct doc_priv *doc = nand_get_controller_data(this);
  874. int ret = 0;
  875. u_char *buf;
  876. struct INFTLMediaHeader *mh;
  877. struct INFTLPartition *ip;
  878. int numparts = 0;
  879. int blocks;
  880. int vshift, lastvunit = 0;
  881. int i;
  882. int end = mtd->size;
  883. if (inftl_bbt_write)
  884. end -= (INFTL_BBT_RESERVED_BLOCKS << this->phys_erase_shift);
  885. buf = kmalloc(mtd->writesize, GFP_KERNEL);
  886. if (!buf) {
  887. return 0;
  888. }
  889. if (!find_media_headers(mtd, buf, "BNAND", 0))
  890. goto out;
  891. doc->mh1_page = doc->mh0_page + (4096 >> this->page_shift);
  892. mh = (struct INFTLMediaHeader *)buf;
  893. le32_to_cpus(&mh->NoOfBootImageBlocks);
  894. le32_to_cpus(&mh->NoOfBinaryPartitions);
  895. le32_to_cpus(&mh->NoOfBDTLPartitions);
  896. le32_to_cpus(&mh->BlockMultiplierBits);
  897. le32_to_cpus(&mh->FormatFlags);
  898. le32_to_cpus(&mh->PercentUsed);
  899. pr_info(" bootRecordID = %s\n"
  900. " NoOfBootImageBlocks = %d\n"
  901. " NoOfBinaryPartitions = %d\n"
  902. " NoOfBDTLPartitions = %d\n"
  903. " BlockMultiplierBits = %d\n"
  904. " FormatFlgs = %d\n"
  905. " OsakVersion = %d.%d.%d.%d\n"
  906. " PercentUsed = %d\n",
  907. mh->bootRecordID, mh->NoOfBootImageBlocks,
  908. mh->NoOfBinaryPartitions,
  909. mh->NoOfBDTLPartitions,
  910. mh->BlockMultiplierBits, mh->FormatFlags,
  911. ((unsigned char *) &mh->OsakVersion)[0] & 0xf,
  912. ((unsigned char *) &mh->OsakVersion)[1] & 0xf,
  913. ((unsigned char *) &mh->OsakVersion)[2] & 0xf,
  914. ((unsigned char *) &mh->OsakVersion)[3] & 0xf,
  915. mh->PercentUsed);
  916. vshift = this->phys_erase_shift + mh->BlockMultiplierBits;
  917. blocks = mtd->size >> vshift;
  918. if (blocks > 32768) {
  919. pr_err("BlockMultiplierBits=%d is inconsistent with device size. Aborting.\n", mh->BlockMultiplierBits);
  920. goto out;
  921. }
  922. blocks = doc->chips_per_floor << (this->chip_shift - this->phys_erase_shift);
  923. if (inftl_bbt_write && (blocks > mtd->erasesize)) {
  924. pr_err("Writeable BBTs spanning more than one erase block are not yet supported. FIX ME!\n");
  925. goto out;
  926. }
  927. /* Scan the partitions */
  928. for (i = 0; (i < 4); i++) {
  929. ip = &(mh->Partitions[i]);
  930. le32_to_cpus(&ip->virtualUnits);
  931. le32_to_cpus(&ip->firstUnit);
  932. le32_to_cpus(&ip->lastUnit);
  933. le32_to_cpus(&ip->flags);
  934. le32_to_cpus(&ip->spareUnits);
  935. le32_to_cpus(&ip->Reserved0);
  936. pr_info(" PARTITION[%d] ->\n"
  937. " virtualUnits = %d\n"
  938. " firstUnit = %d\n"
  939. " lastUnit = %d\n"
  940. " flags = 0x%x\n"
  941. " spareUnits = %d\n",
  942. i, ip->virtualUnits, ip->firstUnit,
  943. ip->lastUnit, ip->flags,
  944. ip->spareUnits);
  945. if ((show_firmware_partition == 1) &&
  946. (i == 0) && (ip->firstUnit > 0)) {
  947. parts[0].name = " DiskOnChip IPL / Media Header partition";
  948. parts[0].offset = 0;
  949. parts[0].size = mtd->erasesize * ip->firstUnit;
  950. numparts = 1;
  951. }
  952. if (ip->flags & INFTL_BINARY)
  953. parts[numparts].name = " DiskOnChip BDK partition";
  954. else
  955. parts[numparts].name = " DiskOnChip BDTL partition";
  956. parts[numparts].offset = ip->firstUnit << vshift;
  957. parts[numparts].size = (1 + ip->lastUnit - ip->firstUnit) << vshift;
  958. numparts++;
  959. if (ip->lastUnit > lastvunit)
  960. lastvunit = ip->lastUnit;
  961. if (ip->flags & INFTL_LAST)
  962. break;
  963. }
  964. lastvunit++;
  965. if ((lastvunit << vshift) < end) {
  966. parts[numparts].name = " DiskOnChip Remainder partition";
  967. parts[numparts].offset = lastvunit << vshift;
  968. parts[numparts].size = end - parts[numparts].offset;
  969. numparts++;
  970. }
  971. ret = numparts;
  972. out:
  973. kfree(buf);
  974. return ret;
  975. }
  976. static int __init nftl_scan_bbt(struct mtd_info *mtd)
  977. {
  978. int ret, numparts;
  979. struct nand_chip *this = mtd_to_nand(mtd);
  980. struct doc_priv *doc = nand_get_controller_data(this);
  981. struct mtd_partition parts[2];
  982. memset((char *)parts, 0, sizeof(parts));
  983. /* On NFTL, we have to find the media headers before we can read the
  984. BBTs, since they're stored in the media header eraseblocks. */
  985. numparts = nftl_partscan(mtd, parts);
  986. if (!numparts)
  987. return -EIO;
  988. this->bbt_td->options = NAND_BBT_ABSPAGE | NAND_BBT_8BIT |
  989. NAND_BBT_SAVECONTENT | NAND_BBT_WRITE |
  990. NAND_BBT_VERSION;
  991. this->bbt_td->veroffs = 7;
  992. this->bbt_td->pages[0] = doc->mh0_page + 1;
  993. if (doc->mh1_page != -1) {
  994. this->bbt_md->options = NAND_BBT_ABSPAGE | NAND_BBT_8BIT |
  995. NAND_BBT_SAVECONTENT | NAND_BBT_WRITE |
  996. NAND_BBT_VERSION;
  997. this->bbt_md->veroffs = 7;
  998. this->bbt_md->pages[0] = doc->mh1_page + 1;
  999. } else {
  1000. this->bbt_md = NULL;
  1001. }
  1002. ret = nand_create_bbt(this);
  1003. if (ret)
  1004. return ret;
  1005. return mtd_device_register(mtd, parts, no_autopart ? 0 : numparts);
  1006. }
  1007. static int __init inftl_scan_bbt(struct mtd_info *mtd)
  1008. {
  1009. int ret, numparts;
  1010. struct nand_chip *this = mtd_to_nand(mtd);
  1011. struct doc_priv *doc = nand_get_controller_data(this);
  1012. struct mtd_partition parts[5];
  1013. if (nanddev_ntargets(&this->base) > doc->chips_per_floor) {
  1014. pr_err("Multi-floor INFTL devices not yet supported.\n");
  1015. return -EIO;
  1016. }
  1017. if (DoC_is_MillenniumPlus(doc)) {
  1018. this->bbt_td->options = NAND_BBT_2BIT | NAND_BBT_ABSPAGE;
  1019. if (inftl_bbt_write)
  1020. this->bbt_td->options |= NAND_BBT_WRITE;
  1021. this->bbt_td->pages[0] = 2;
  1022. this->bbt_md = NULL;
  1023. } else {
  1024. this->bbt_td->options = NAND_BBT_LASTBLOCK | NAND_BBT_8BIT | NAND_BBT_VERSION;
  1025. if (inftl_bbt_write)
  1026. this->bbt_td->options |= NAND_BBT_WRITE;
  1027. this->bbt_td->offs = 8;
  1028. this->bbt_td->len = 8;
  1029. this->bbt_td->veroffs = 7;
  1030. this->bbt_td->maxblocks = INFTL_BBT_RESERVED_BLOCKS;
  1031. this->bbt_td->reserved_block_code = 0x01;
  1032. this->bbt_td->pattern = "MSYS_BBT";
  1033. this->bbt_md->options = NAND_BBT_LASTBLOCK | NAND_BBT_8BIT | NAND_BBT_VERSION;
  1034. if (inftl_bbt_write)
  1035. this->bbt_md->options |= NAND_BBT_WRITE;
  1036. this->bbt_md->offs = 8;
  1037. this->bbt_md->len = 8;
  1038. this->bbt_md->veroffs = 7;
  1039. this->bbt_md->maxblocks = INFTL_BBT_RESERVED_BLOCKS;
  1040. this->bbt_md->reserved_block_code = 0x01;
  1041. this->bbt_md->pattern = "TBB_SYSM";
  1042. }
  1043. ret = nand_create_bbt(this);
  1044. if (ret)
  1045. return ret;
  1046. memset((char *)parts, 0, sizeof(parts));
  1047. numparts = inftl_partscan(mtd, parts);
  1048. /* At least for now, require the INFTL Media Header. We could probably
  1049. do without it for non-INFTL use, since all it gives us is
  1050. autopartitioning, but I want to give it more thought. */
  1051. if (!numparts)
  1052. return -EIO;
  1053. return mtd_device_register(mtd, parts, no_autopart ? 0 : numparts);
  1054. }
  1055. static inline int __init doc2000_init(struct mtd_info *mtd)
  1056. {
  1057. struct nand_chip *this = mtd_to_nand(mtd);
  1058. struct doc_priv *doc = nand_get_controller_data(this);
  1059. doc->late_init = nftl_scan_bbt;
  1060. doc->CDSNControl = CDSN_CTRL_FLASH_IO | CDSN_CTRL_ECC_IO;
  1061. doc2000_count_chips(mtd);
  1062. mtd->name = "DiskOnChip 2000 (NFTL Model)";
  1063. return (4 * doc->chips_per_floor);
  1064. }
  1065. static inline int __init doc2001_init(struct mtd_info *mtd)
  1066. {
  1067. struct nand_chip *this = mtd_to_nand(mtd);
  1068. struct doc_priv *doc = nand_get_controller_data(this);
  1069. ReadDOC(doc->virtadr, ChipID);
  1070. ReadDOC(doc->virtadr, ChipID);
  1071. ReadDOC(doc->virtadr, ChipID);
  1072. if (ReadDOC(doc->virtadr, ChipID) != DOC_ChipID_DocMil) {
  1073. /* It's not a Millennium; it's one of the newer
  1074. DiskOnChip 2000 units with a similar ASIC.
  1075. Treat it like a Millennium, except that it
  1076. can have multiple chips. */
  1077. doc2000_count_chips(mtd);
  1078. mtd->name = "DiskOnChip 2000 (INFTL Model)";
  1079. doc->late_init = inftl_scan_bbt;
  1080. return (4 * doc->chips_per_floor);
  1081. } else {
  1082. /* Bog-standard Millennium */
  1083. doc->chips_per_floor = 1;
  1084. mtd->name = "DiskOnChip Millennium";
  1085. doc->late_init = nftl_scan_bbt;
  1086. return 1;
  1087. }
  1088. }
  1089. static inline int __init doc2001plus_init(struct mtd_info *mtd)
  1090. {
  1091. struct nand_chip *this = mtd_to_nand(mtd);
  1092. struct doc_priv *doc = nand_get_controller_data(this);
  1093. doc->late_init = inftl_scan_bbt;
  1094. this->ecc.hwctl = doc2001plus_enable_hwecc;
  1095. doc->chips_per_floor = 1;
  1096. mtd->name = "DiskOnChip Millennium Plus";
  1097. return 1;
  1098. }
  1099. static int doc200x_attach_chip(struct nand_chip *chip)
  1100. {
  1101. if (chip->ecc.engine_type != NAND_ECC_ENGINE_TYPE_ON_HOST)
  1102. return 0;
  1103. chip->ecc.placement = NAND_ECC_PLACEMENT_INTERLEAVED;
  1104. chip->ecc.size = 512;
  1105. chip->ecc.bytes = 6;
  1106. chip->ecc.strength = 2;
  1107. chip->ecc.options = NAND_ECC_GENERIC_ERASED_CHECK;
  1108. chip->ecc.hwctl = doc200x_enable_hwecc;
  1109. chip->ecc.calculate = doc200x_calculate_ecc;
  1110. chip->ecc.correct = doc200x_correct_data;
  1111. return 0;
  1112. }
  1113. static const struct nand_controller_ops doc200x_ops = {
  1114. .exec_op = doc200x_exec_op,
  1115. .attach_chip = doc200x_attach_chip,
  1116. };
  1117. static const struct nand_controller_ops doc2001plus_ops = {
  1118. .exec_op = doc2001plus_exec_op,
  1119. .attach_chip = doc200x_attach_chip,
  1120. };
  1121. static int __init doc_probe(unsigned long physadr)
  1122. {
  1123. struct nand_chip *nand = NULL;
  1124. struct doc_priv *doc = NULL;
  1125. unsigned char ChipID;
  1126. struct mtd_info *mtd;
  1127. void __iomem *virtadr;
  1128. unsigned char save_control;
  1129. unsigned char tmp, tmpb, tmpc;
  1130. int reg, len, numchips;
  1131. int ret = 0;
  1132. if (!request_mem_region(physadr, DOC_IOREMAP_LEN, "DiskOnChip"))
  1133. return -EBUSY;
  1134. virtadr = ioremap(physadr, DOC_IOREMAP_LEN);
  1135. if (!virtadr) {
  1136. pr_err("Diskonchip ioremap failed: 0x%x bytes at 0x%lx\n",
  1137. DOC_IOREMAP_LEN, physadr);
  1138. ret = -EIO;
  1139. goto error_ioremap;
  1140. }
  1141. /* It's not possible to cleanly detect the DiskOnChip - the
  1142. * bootup procedure will put the device into reset mode, and
  1143. * it's not possible to talk to it without actually writing
  1144. * to the DOCControl register. So we store the current contents
  1145. * of the DOCControl register's location, in case we later decide
  1146. * that it's not a DiskOnChip, and want to put it back how we
  1147. * found it.
  1148. */
  1149. save_control = ReadDOC(virtadr, DOCControl);
  1150. /* Reset the DiskOnChip ASIC */
  1151. WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_RESET, virtadr, DOCControl);
  1152. WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_RESET, virtadr, DOCControl);
  1153. /* Enable the DiskOnChip ASIC */
  1154. WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_NORMAL, virtadr, DOCControl);
  1155. WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_NORMAL, virtadr, DOCControl);
  1156. ChipID = ReadDOC(virtadr, ChipID);
  1157. switch (ChipID) {
  1158. case DOC_ChipID_Doc2k:
  1159. reg = DoC_2k_ECCStatus;
  1160. break;
  1161. case DOC_ChipID_DocMil:
  1162. reg = DoC_ECCConf;
  1163. break;
  1164. case DOC_ChipID_DocMilPlus16:
  1165. case DOC_ChipID_DocMilPlus32:
  1166. case 0:
  1167. /* Possible Millennium Plus, need to do more checks */
  1168. /* Possibly release from power down mode */
  1169. for (tmp = 0; (tmp < 4); tmp++)
  1170. ReadDOC(virtadr, Mplus_Power);
  1171. /* Reset the Millennium Plus ASIC */
  1172. tmp = DOC_MODE_RESET | DOC_MODE_MDWREN | DOC_MODE_RST_LAT | DOC_MODE_BDECT;
  1173. WriteDOC(tmp, virtadr, Mplus_DOCControl);
  1174. WriteDOC(~tmp, virtadr, Mplus_CtrlConfirm);
  1175. usleep_range(1000, 2000);
  1176. /* Enable the Millennium Plus ASIC */
  1177. tmp = DOC_MODE_NORMAL | DOC_MODE_MDWREN | DOC_MODE_RST_LAT | DOC_MODE_BDECT;
  1178. WriteDOC(tmp, virtadr, Mplus_DOCControl);
  1179. WriteDOC(~tmp, virtadr, Mplus_CtrlConfirm);
  1180. usleep_range(1000, 2000);
  1181. ChipID = ReadDOC(virtadr, ChipID);
  1182. switch (ChipID) {
  1183. case DOC_ChipID_DocMilPlus16:
  1184. reg = DoC_Mplus_Toggle;
  1185. break;
  1186. case DOC_ChipID_DocMilPlus32:
  1187. pr_err("DiskOnChip Millennium Plus 32MB is not supported, ignoring.\n");
  1188. fallthrough;
  1189. default:
  1190. ret = -ENODEV;
  1191. goto notfound;
  1192. }
  1193. break;
  1194. default:
  1195. ret = -ENODEV;
  1196. goto notfound;
  1197. }
  1198. /* Check the TOGGLE bit in the ECC register */
  1199. tmp = ReadDOC_(virtadr, reg) & DOC_TOGGLE_BIT;
  1200. tmpb = ReadDOC_(virtadr, reg) & DOC_TOGGLE_BIT;
  1201. tmpc = ReadDOC_(virtadr, reg) & DOC_TOGGLE_BIT;
  1202. if ((tmp == tmpb) || (tmp != tmpc)) {
  1203. pr_warn("Possible DiskOnChip at 0x%lx failed TOGGLE test, dropping.\n", physadr);
  1204. ret = -ENODEV;
  1205. goto notfound;
  1206. }
  1207. for (mtd = doclist; mtd; mtd = doc->nextdoc) {
  1208. unsigned char oldval;
  1209. unsigned char newval;
  1210. nand = mtd_to_nand(mtd);
  1211. doc = nand_get_controller_data(nand);
  1212. /* Use the alias resolution register to determine if this is
  1213. in fact the same DOC aliased to a new address. If writes
  1214. to one chip's alias resolution register change the value on
  1215. the other chip, they're the same chip. */
  1216. if (ChipID == DOC_ChipID_DocMilPlus16) {
  1217. oldval = ReadDOC(doc->virtadr, Mplus_AliasResolution);
  1218. newval = ReadDOC(virtadr, Mplus_AliasResolution);
  1219. } else {
  1220. oldval = ReadDOC(doc->virtadr, AliasResolution);
  1221. newval = ReadDOC(virtadr, AliasResolution);
  1222. }
  1223. if (oldval != newval)
  1224. continue;
  1225. if (ChipID == DOC_ChipID_DocMilPlus16) {
  1226. WriteDOC(~newval, virtadr, Mplus_AliasResolution);
  1227. oldval = ReadDOC(doc->virtadr, Mplus_AliasResolution);
  1228. WriteDOC(newval, virtadr, Mplus_AliasResolution); // restore it
  1229. } else {
  1230. WriteDOC(~newval, virtadr, AliasResolution);
  1231. oldval = ReadDOC(doc->virtadr, AliasResolution);
  1232. WriteDOC(newval, virtadr, AliasResolution); // restore it
  1233. }
  1234. newval = ~newval;
  1235. if (oldval == newval) {
  1236. pr_debug("Found alias of DOC at 0x%lx to 0x%lx\n",
  1237. doc->physadr, physadr);
  1238. goto notfound;
  1239. }
  1240. }
  1241. pr_notice("DiskOnChip found at 0x%lx\n", physadr);
  1242. len = sizeof(struct nand_chip) + sizeof(struct doc_priv) +
  1243. (2 * sizeof(struct nand_bbt_descr));
  1244. nand = kzalloc(len, GFP_KERNEL);
  1245. if (!nand) {
  1246. ret = -ENOMEM;
  1247. goto fail;
  1248. }
  1249. /*
  1250. * Allocate a RS codec instance
  1251. *
  1252. * Symbolsize is 10 (bits)
  1253. * Primitve polynomial is x^10+x^3+1
  1254. * First consecutive root is 510
  1255. * Primitve element to generate roots = 1
  1256. * Generator polinomial degree = 4
  1257. */
  1258. doc = (struct doc_priv *) (nand + 1);
  1259. doc->rs_decoder = init_rs(10, 0x409, FCR, 1, NROOTS);
  1260. if (!doc->rs_decoder) {
  1261. pr_err("DiskOnChip: Could not create a RS codec\n");
  1262. ret = -ENOMEM;
  1263. goto fail;
  1264. }
  1265. nand_controller_init(&doc->base);
  1266. if (ChipID == DOC_ChipID_DocMilPlus16)
  1267. doc->base.ops = &doc2001plus_ops;
  1268. else
  1269. doc->base.ops = &doc200x_ops;
  1270. mtd = nand_to_mtd(nand);
  1271. nand->bbt_td = (struct nand_bbt_descr *) (doc + 1);
  1272. nand->bbt_md = nand->bbt_td + 1;
  1273. mtd->owner = THIS_MODULE;
  1274. mtd_set_ooblayout(mtd, &doc200x_ooblayout_ops);
  1275. nand->controller = &doc->base;
  1276. nand_set_controller_data(nand, doc);
  1277. nand->bbt_options = NAND_BBT_USE_FLASH;
  1278. /* Skip the automatic BBT scan so we can run it manually */
  1279. nand->options |= NAND_SKIP_BBTSCAN | NAND_NO_BBM_QUIRK;
  1280. doc->physadr = physadr;
  1281. doc->virtadr = virtadr;
  1282. doc->ChipID = ChipID;
  1283. doc->curfloor = -1;
  1284. doc->curchip = -1;
  1285. doc->mh0_page = -1;
  1286. doc->mh1_page = -1;
  1287. doc->nextdoc = doclist;
  1288. if (ChipID == DOC_ChipID_Doc2k)
  1289. numchips = doc2000_init(mtd);
  1290. else if (ChipID == DOC_ChipID_DocMilPlus16)
  1291. numchips = doc2001plus_init(mtd);
  1292. else
  1293. numchips = doc2001_init(mtd);
  1294. if ((ret = nand_scan(nand, numchips)) || (ret = doc->late_init(mtd))) {
  1295. /* DBB note: i believe nand_cleanup is necessary here, as
  1296. buffers may have been allocated in nand_base. Check with
  1297. Thomas. FIX ME! */
  1298. nand_cleanup(nand);
  1299. goto fail;
  1300. }
  1301. /* Success! */
  1302. doclist = mtd;
  1303. return 0;
  1304. notfound:
  1305. /* Put back the contents of the DOCControl register, in case it's not
  1306. actually a DiskOnChip. */
  1307. WriteDOC(save_control, virtadr, DOCControl);
  1308. fail:
  1309. if (doc)
  1310. free_rs(doc->rs_decoder);
  1311. kfree(nand);
  1312. iounmap(virtadr);
  1313. error_ioremap:
  1314. release_mem_region(physadr, DOC_IOREMAP_LEN);
  1315. return ret;
  1316. }
  1317. static void release_nanddoc(void)
  1318. {
  1319. struct mtd_info *mtd, *nextmtd;
  1320. struct nand_chip *nand;
  1321. struct doc_priv *doc;
  1322. int ret;
  1323. for (mtd = doclist; mtd; mtd = nextmtd) {
  1324. nand = mtd_to_nand(mtd);
  1325. doc = nand_get_controller_data(nand);
  1326. nextmtd = doc->nextdoc;
  1327. ret = mtd_device_unregister(mtd);
  1328. WARN_ON(ret);
  1329. nand_cleanup(nand);
  1330. iounmap(doc->virtadr);
  1331. release_mem_region(doc->physadr, DOC_IOREMAP_LEN);
  1332. free_rs(doc->rs_decoder);
  1333. kfree(nand);
  1334. }
  1335. }
  1336. static int __init init_nanddoc(void)
  1337. {
  1338. int i, ret = 0;
  1339. if (doc_config_location) {
  1340. pr_info("Using configured DiskOnChip probe address 0x%lx\n",
  1341. doc_config_location);
  1342. ret = doc_probe(doc_config_location);
  1343. if (ret < 0)
  1344. return ret;
  1345. } else {
  1346. for (i = 0; (doc_locations[i] != 0xffffffff); i++) {
  1347. doc_probe(doc_locations[i]);
  1348. }
  1349. }
  1350. /* No banner message any more. Print a message if no DiskOnChip
  1351. found, so the user knows we at least tried. */
  1352. if (!doclist) {
  1353. pr_info("No valid DiskOnChip devices found\n");
  1354. ret = -ENODEV;
  1355. }
  1356. return ret;
  1357. }
  1358. static void __exit cleanup_nanddoc(void)
  1359. {
  1360. /* Cleanup the nand/DoC resources */
  1361. release_nanddoc();
  1362. }
  1363. module_init(init_nanddoc);
  1364. module_exit(cleanup_nanddoc);
  1365. MODULE_LICENSE("GPL");
  1366. MODULE_AUTHOR("David Woodhouse <[email protected]>");
  1367. MODULE_DESCRIPTION("M-Systems DiskOnChip 2000, Millennium and Millennium Plus device driver");