diskonchip.c 48 KB

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