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