diskonchip.c 50 KB

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