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