diskonchip.c 50 KB

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