diskonchip.c 48 KB

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