diskonchip.c 50 KB

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