diskonchip.c 50 KB

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