diskonchip.c 48 KB

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