diskonchip.c 50 KB

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