diskonchip.c 49 KB

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