diskonchip.c 50 KB

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