diskonchip.c 50 KB

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