diskonchip.c 48 KB

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