nand_base.c 71 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822
  1. /*
  2. * drivers/mtd/nand.c
  3. *
  4. * Overview:
  5. * This is the generic MTD driver for NAND flash devices. It should be
  6. * capable of working with almost all NAND chips currently available.
  7. * Basic support for AG-AND chips is provided.
  8. *
  9. * Additional technical information is available on
  10. * http://www.linux-mtd.infradead.org/tech/nand.html
  11. *
  12. * Copyright (C) 2000 Steven J. Hill (sjhill@realitydiluted.com)
  13. * 2002-2006 Thomas Gleixner (tglx@linutronix.de)
  14. *
  15. * Credits:
  16. * David Woodhouse for adding multichip support
  17. *
  18. * Aleph One Ltd. and Toby Churchill Ltd. for supporting the
  19. * rework for 2K page size chips
  20. *
  21. * TODO:
  22. * Enable cached programming for 2k page size chips
  23. * Check, if mtd->ecctype should be set to MTD_ECC_HW
  24. * if we have HW ecc support.
  25. * The AG-AND chips have nice features for speed improvement,
  26. * which are not supported yet. Read / program 4 pages in one go.
  27. *
  28. * This program is free software; you can redistribute it and/or modify
  29. * it under the terms of the GNU General Public License version 2 as
  30. * published by the Free Software Foundation.
  31. *
  32. */
  33. /* XXX U-BOOT XXX */
  34. #if 0
  35. #include <linux/module.h>
  36. #include <linux/delay.h>
  37. #include <linux/errno.h>
  38. #include <linux/err.h>
  39. #include <linux/sched.h>
  40. #include <linux/slab.h>
  41. #include <linux/types.h>
  42. #include <linux/mtd/mtd.h>
  43. #include <linux/mtd/nand.h>
  44. #include <linux/mtd/nand_ecc.h>
  45. #include <linux/mtd/compatmac.h>
  46. #include <linux/interrupt.h>
  47. #include <linux/bitops.h>
  48. #include <linux/leds.h>
  49. #include <asm/io.h>
  50. #ifdef CONFIG_MTD_PARTITIONS
  51. #include <linux/mtd/partitions.h>
  52. #endif
  53. #endif
  54. #include <common.h>
  55. #define ENOTSUPP 524 /* Operation is not supported */
  56. #include <malloc.h>
  57. #include <watchdog.h>
  58. #include <linux/err.h>
  59. #include <linux/mtd/compat.h>
  60. #include <linux/mtd/mtd.h>
  61. #include <linux/mtd/nand.h>
  62. #include <linux/mtd/nand_ecc.h>
  63. #include <asm/io.h>
  64. #include <asm/errno.h>
  65. #ifdef CONFIG_JFFS2_NAND
  66. #include <jffs2/jffs2.h>
  67. #endif
  68. /* Define default oob placement schemes for large and small page devices */
  69. static struct nand_ecclayout nand_oob_8 = {
  70. .eccbytes = 3,
  71. .eccpos = {0, 1, 2},
  72. .oobfree = {
  73. {.offset = 3,
  74. .length = 2},
  75. {.offset = 6,
  76. .length = 2}}
  77. };
  78. static struct nand_ecclayout nand_oob_16 = {
  79. .eccbytes = 6,
  80. .eccpos = {0, 1, 2, 3, 6, 7},
  81. .oobfree = {
  82. {.offset = 8,
  83. . length = 8}}
  84. };
  85. static struct nand_ecclayout nand_oob_64 = {
  86. .eccbytes = 24,
  87. .eccpos = {
  88. 40, 41, 42, 43, 44, 45, 46, 47,
  89. 48, 49, 50, 51, 52, 53, 54, 55,
  90. 56, 57, 58, 59, 60, 61, 62, 63},
  91. .oobfree = {
  92. {.offset = 2,
  93. .length = 38}}
  94. };
  95. static struct nand_ecclayout nand_oob_128 = {
  96. .eccbytes = 48,
  97. .eccpos = {
  98. 80, 81, 82, 83, 84, 85, 86, 87,
  99. 88, 89, 90, 91, 92, 93, 94, 95,
  100. 96, 97, 98, 99, 100, 101, 102, 103,
  101. 104, 105, 106, 107, 108, 109, 110, 111,
  102. 112, 113, 114, 115, 116, 117, 118, 119,
  103. 120, 121, 122, 123, 124, 125, 126, 127},
  104. .oobfree = {
  105. {.offset = 2,
  106. .length = 78}}
  107. };
  108. static int nand_get_device(struct nand_chip *chip, struct mtd_info *mtd,
  109. int new_state);
  110. static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
  111. struct mtd_oob_ops *ops);
  112. static int nand_wait(struct mtd_info *mtd, struct nand_chip *this);
  113. /*
  114. * For devices which display every fart in the system on a seperate LED. Is
  115. * compiled away when LED support is disabled.
  116. */
  117. /* XXX U-BOOT XXX */
  118. #if 0
  119. DEFINE_LED_TRIGGER(nand_led_trigger);
  120. #endif
  121. /**
  122. * nand_release_device - [GENERIC] release chip
  123. * @mtd: MTD device structure
  124. *
  125. * Deselect, release chip lock and wake up anyone waiting on the device
  126. */
  127. /* XXX U-BOOT XXX */
  128. #if 0
  129. static void nand_release_device(struct mtd_info *mtd)
  130. {
  131. struct nand_chip *chip = mtd->priv;
  132. /* De-select the NAND device */
  133. chip->select_chip(mtd, -1);
  134. /* Release the controller and the chip */
  135. spin_lock(&chip->controller->lock);
  136. chip->controller->active = NULL;
  137. chip->state = FL_READY;
  138. wake_up(&chip->controller->wq);
  139. spin_unlock(&chip->controller->lock);
  140. }
  141. #else
  142. static void nand_release_device (struct mtd_info *mtd)
  143. {
  144. struct nand_chip *this = mtd->priv;
  145. this->select_chip(mtd, -1); /* De-select the NAND device */
  146. }
  147. #endif
  148. /**
  149. * nand_read_byte - [DEFAULT] read one byte from the chip
  150. * @mtd: MTD device structure
  151. *
  152. * Default read function for 8bit buswith
  153. */
  154. static uint8_t nand_read_byte(struct mtd_info *mtd)
  155. {
  156. struct nand_chip *chip = mtd->priv;
  157. return readb(chip->IO_ADDR_R);
  158. }
  159. /**
  160. * nand_read_byte16 - [DEFAULT] read one byte endianess aware from the chip
  161. * @mtd: MTD device structure
  162. *
  163. * Default read function for 16bit buswith with
  164. * endianess conversion
  165. */
  166. static uint8_t nand_read_byte16(struct mtd_info *mtd)
  167. {
  168. struct nand_chip *chip = mtd->priv;
  169. return (uint8_t) cpu_to_le16(readw(chip->IO_ADDR_R));
  170. }
  171. /**
  172. * nand_read_word - [DEFAULT] read one word from the chip
  173. * @mtd: MTD device structure
  174. *
  175. * Default read function for 16bit buswith without
  176. * endianess conversion
  177. */
  178. static u16 nand_read_word(struct mtd_info *mtd)
  179. {
  180. struct nand_chip *chip = mtd->priv;
  181. return readw(chip->IO_ADDR_R);
  182. }
  183. /**
  184. * nand_select_chip - [DEFAULT] control CE line
  185. * @mtd: MTD device structure
  186. * @chipnr: chipnumber to select, -1 for deselect
  187. *
  188. * Default select function for 1 chip devices.
  189. */
  190. static void nand_select_chip(struct mtd_info *mtd, int chipnr)
  191. {
  192. struct nand_chip *chip = mtd->priv;
  193. switch (chipnr) {
  194. case -1:
  195. chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
  196. break;
  197. case 0:
  198. break;
  199. default:
  200. BUG();
  201. }
  202. }
  203. /**
  204. * nand_write_buf - [DEFAULT] write buffer to chip
  205. * @mtd: MTD device structure
  206. * @buf: data buffer
  207. * @len: number of bytes to write
  208. *
  209. * Default write function for 8bit buswith
  210. */
  211. static void nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
  212. {
  213. int i;
  214. struct nand_chip *chip = mtd->priv;
  215. for (i = 0; i < len; i++)
  216. writeb(buf[i], chip->IO_ADDR_W);
  217. }
  218. /**
  219. * nand_read_buf - [DEFAULT] read chip data into buffer
  220. * @mtd: MTD device structure
  221. * @buf: buffer to store date
  222. * @len: number of bytes to read
  223. *
  224. * Default read function for 8bit buswith
  225. */
  226. static void nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
  227. {
  228. int i;
  229. struct nand_chip *chip = mtd->priv;
  230. for (i = 0; i < len; i++)
  231. buf[i] = readb(chip->IO_ADDR_R);
  232. }
  233. /**
  234. * nand_verify_buf - [DEFAULT] Verify chip data against buffer
  235. * @mtd: MTD device structure
  236. * @buf: buffer containing the data to compare
  237. * @len: number of bytes to compare
  238. *
  239. * Default verify function for 8bit buswith
  240. */
  241. static int nand_verify_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
  242. {
  243. int i;
  244. struct nand_chip *chip = mtd->priv;
  245. for (i = 0; i < len; i++)
  246. if (buf[i] != readb(chip->IO_ADDR_R))
  247. return -EFAULT;
  248. return 0;
  249. }
  250. /**
  251. * nand_write_buf16 - [DEFAULT] write buffer to chip
  252. * @mtd: MTD device structure
  253. * @buf: data buffer
  254. * @len: number of bytes to write
  255. *
  256. * Default write function for 16bit buswith
  257. */
  258. static void nand_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
  259. {
  260. int i;
  261. struct nand_chip *chip = mtd->priv;
  262. u16 *p = (u16 *) buf;
  263. len >>= 1;
  264. for (i = 0; i < len; i++)
  265. writew(p[i], chip->IO_ADDR_W);
  266. }
  267. /**
  268. * nand_read_buf16 - [DEFAULT] read chip data into buffer
  269. * @mtd: MTD device structure
  270. * @buf: buffer to store date
  271. * @len: number of bytes to read
  272. *
  273. * Default read function for 16bit buswith
  274. */
  275. static void nand_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len)
  276. {
  277. int i;
  278. struct nand_chip *chip = mtd->priv;
  279. u16 *p = (u16 *) buf;
  280. len >>= 1;
  281. for (i = 0; i < len; i++)
  282. p[i] = readw(chip->IO_ADDR_R);
  283. }
  284. /**
  285. * nand_verify_buf16 - [DEFAULT] Verify chip data against buffer
  286. * @mtd: MTD device structure
  287. * @buf: buffer containing the data to compare
  288. * @len: number of bytes to compare
  289. *
  290. * Default verify function for 16bit buswith
  291. */
  292. static int nand_verify_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
  293. {
  294. int i;
  295. struct nand_chip *chip = mtd->priv;
  296. u16 *p = (u16 *) buf;
  297. len >>= 1;
  298. for (i = 0; i < len; i++)
  299. if (p[i] != readw(chip->IO_ADDR_R))
  300. return -EFAULT;
  301. return 0;
  302. }
  303. /**
  304. * nand_block_bad - [DEFAULT] Read bad block marker from the chip
  305. * @mtd: MTD device structure
  306. * @ofs: offset from device start
  307. * @getchip: 0, if the chip is already selected
  308. *
  309. * Check, if the block is bad.
  310. */
  311. static int nand_block_bad(struct mtd_info *mtd, loff_t ofs, int getchip)
  312. {
  313. int page, chipnr, res = 0;
  314. struct nand_chip *chip = mtd->priv;
  315. u16 bad;
  316. page = (int)(ofs >> chip->page_shift) & chip->pagemask;
  317. if (getchip) {
  318. chipnr = (int)(ofs >> chip->chip_shift);
  319. nand_get_device(chip, mtd, FL_READING);
  320. /* Select the NAND device */
  321. chip->select_chip(mtd, chipnr);
  322. }
  323. if (chip->options & NAND_BUSWIDTH_16) {
  324. chip->cmdfunc(mtd, NAND_CMD_READOOB, chip->badblockpos & 0xFE,
  325. page);
  326. bad = cpu_to_le16(chip->read_word(mtd));
  327. if (chip->badblockpos & 0x1)
  328. bad >>= 8;
  329. if ((bad & 0xFF) != 0xff)
  330. res = 1;
  331. } else {
  332. chip->cmdfunc(mtd, NAND_CMD_READOOB, chip->badblockpos, page);
  333. if (chip->read_byte(mtd) != 0xff)
  334. res = 1;
  335. }
  336. if (getchip)
  337. nand_release_device(mtd);
  338. return res;
  339. }
  340. /**
  341. * nand_default_block_markbad - [DEFAULT] mark a block bad
  342. * @mtd: MTD device structure
  343. * @ofs: offset from device start
  344. *
  345. * This is the default implementation, which can be overridden by
  346. * a hardware specific driver.
  347. */
  348. static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
  349. {
  350. struct nand_chip *chip = mtd->priv;
  351. uint8_t buf[2] = { 0, 0 };
  352. int block, ret;
  353. /* Get block number */
  354. block = (int)(ofs >> chip->bbt_erase_shift);
  355. if (chip->bbt)
  356. chip->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1);
  357. /* Do we have a flash based bad block table ? */
  358. if (chip->options & NAND_USE_FLASH_BBT)
  359. ret = nand_update_bbt(mtd, ofs);
  360. else {
  361. /* We write two bytes, so we dont have to mess with 16 bit
  362. * access
  363. */
  364. ofs += mtd->oobsize;
  365. chip->ops.len = chip->ops.ooblen = 2;
  366. chip->ops.datbuf = NULL;
  367. chip->ops.oobbuf = buf;
  368. chip->ops.ooboffs = chip->badblockpos & ~0x01;
  369. ret = nand_do_write_oob(mtd, ofs, &chip->ops);
  370. }
  371. if (!ret)
  372. mtd->ecc_stats.badblocks++;
  373. return ret;
  374. }
  375. /**
  376. * nand_check_wp - [GENERIC] check if the chip is write protected
  377. * @mtd: MTD device structure
  378. * Check, if the device is write protected
  379. *
  380. * The function expects, that the device is already selected
  381. */
  382. static int nand_check_wp(struct mtd_info *mtd)
  383. {
  384. struct nand_chip *chip = mtd->priv;
  385. /* Check the WP bit */
  386. chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
  387. return (chip->read_byte(mtd) & NAND_STATUS_WP) ? 0 : 1;
  388. }
  389. /**
  390. * nand_block_checkbad - [GENERIC] Check if a block is marked bad
  391. * @mtd: MTD device structure
  392. * @ofs: offset from device start
  393. * @getchip: 0, if the chip is already selected
  394. * @allowbbt: 1, if its allowed to access the bbt area
  395. *
  396. * Check, if the block is bad. Either by reading the bad block table or
  397. * calling of the scan function.
  398. */
  399. static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int getchip,
  400. int allowbbt)
  401. {
  402. struct nand_chip *chip = mtd->priv;
  403. if (!(chip->options & NAND_BBT_SCANNED)) {
  404. chip->scan_bbt(mtd);
  405. chip->options |= NAND_BBT_SCANNED;
  406. }
  407. if (!chip->bbt)
  408. return chip->block_bad(mtd, ofs, getchip);
  409. /* Return info from the table */
  410. return nand_isbad_bbt(mtd, ofs, allowbbt);
  411. }
  412. /*
  413. * Wait for the ready pin, after a command
  414. * The timeout is catched later.
  415. */
  416. /* XXX U-BOOT XXX */
  417. #if 0
  418. void nand_wait_ready(struct mtd_info *mtd)
  419. {
  420. struct nand_chip *chip = mtd->priv;
  421. unsigned long timeo = jiffies + 2;
  422. led_trigger_event(nand_led_trigger, LED_FULL);
  423. /* wait until command is processed or timeout occures */
  424. do {
  425. if (chip->dev_ready(mtd))
  426. break;
  427. touch_softlockup_watchdog();
  428. } while (time_before(jiffies, timeo));
  429. led_trigger_event(nand_led_trigger, LED_OFF);
  430. }
  431. EXPORT_SYMBOL_GPL(nand_wait_ready);
  432. #else
  433. void nand_wait_ready(struct mtd_info *mtd)
  434. {
  435. struct nand_chip *chip = mtd->priv;
  436. u32 timeo = (CFG_HZ * 20) / 1000;
  437. reset_timer();
  438. /* wait until command is processed or timeout occures */
  439. while (get_timer(0) < timeo) {
  440. if (chip->dev_ready)
  441. if (chip->dev_ready(mtd))
  442. break;
  443. }
  444. }
  445. #endif
  446. /**
  447. * nand_command - [DEFAULT] Send command to NAND device
  448. * @mtd: MTD device structure
  449. * @command: the command to be sent
  450. * @column: the column address for this command, -1 if none
  451. * @page_addr: the page address for this command, -1 if none
  452. *
  453. * Send command to NAND device. This function is used for small page
  454. * devices (256/512 Bytes per page)
  455. */
  456. static void nand_command(struct mtd_info *mtd, unsigned int command,
  457. int column, int page_addr)
  458. {
  459. register struct nand_chip *chip = mtd->priv;
  460. int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE;
  461. /*
  462. * Write out the command to the device.
  463. */
  464. if (command == NAND_CMD_SEQIN) {
  465. int readcmd;
  466. if (column >= mtd->writesize) {
  467. /* OOB area */
  468. column -= mtd->writesize;
  469. readcmd = NAND_CMD_READOOB;
  470. } else if (column < 256) {
  471. /* First 256 bytes --> READ0 */
  472. readcmd = NAND_CMD_READ0;
  473. } else {
  474. column -= 256;
  475. readcmd = NAND_CMD_READ1;
  476. }
  477. chip->cmd_ctrl(mtd, readcmd, ctrl);
  478. ctrl &= ~NAND_CTRL_CHANGE;
  479. }
  480. chip->cmd_ctrl(mtd, command, ctrl);
  481. /*
  482. * Address cycle, when necessary
  483. */
  484. ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE;
  485. /* Serially input address */
  486. if (column != -1) {
  487. /* Adjust columns for 16 bit buswidth */
  488. if (chip->options & NAND_BUSWIDTH_16)
  489. column >>= 1;
  490. chip->cmd_ctrl(mtd, column, ctrl);
  491. ctrl &= ~NAND_CTRL_CHANGE;
  492. }
  493. if (page_addr != -1) {
  494. chip->cmd_ctrl(mtd, page_addr, ctrl);
  495. ctrl &= ~NAND_CTRL_CHANGE;
  496. chip->cmd_ctrl(mtd, page_addr >> 8, ctrl);
  497. /* One more address cycle for devices > 32MiB */
  498. if (chip->chipsize > (32 << 20))
  499. chip->cmd_ctrl(mtd, page_addr >> 16, ctrl);
  500. }
  501. chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
  502. /*
  503. * program and erase have their own busy handlers
  504. * status and sequential in needs no delay
  505. */
  506. switch (command) {
  507. case NAND_CMD_PAGEPROG:
  508. case NAND_CMD_ERASE1:
  509. case NAND_CMD_ERASE2:
  510. case NAND_CMD_SEQIN:
  511. case NAND_CMD_STATUS:
  512. return;
  513. case NAND_CMD_RESET:
  514. if (chip->dev_ready)
  515. break;
  516. udelay(chip->chip_delay);
  517. chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
  518. NAND_CTRL_CLE | NAND_CTRL_CHANGE);
  519. chip->cmd_ctrl(mtd,
  520. NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
  521. while (!(chip->read_byte(mtd) & NAND_STATUS_READY)) ;
  522. return;
  523. /* This applies to read commands */
  524. default:
  525. /*
  526. * If we don't have access to the busy pin, we apply the given
  527. * command delay
  528. */
  529. if (!chip->dev_ready) {
  530. udelay(chip->chip_delay);
  531. return;
  532. }
  533. }
  534. /* Apply this short delay always to ensure that we do wait tWB in
  535. * any case on any machine. */
  536. ndelay(100);
  537. nand_wait_ready(mtd);
  538. }
  539. /**
  540. * nand_command_lp - [DEFAULT] Send command to NAND large page device
  541. * @mtd: MTD device structure
  542. * @command: the command to be sent
  543. * @column: the column address for this command, -1 if none
  544. * @page_addr: the page address for this command, -1 if none
  545. *
  546. * Send command to NAND device. This is the version for the new large page
  547. * devices We dont have the separate regions as we have in the small page
  548. * devices. We must emulate NAND_CMD_READOOB to keep the code compatible.
  549. */
  550. static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
  551. int column, int page_addr)
  552. {
  553. register struct nand_chip *chip = mtd->priv;
  554. /* Emulate NAND_CMD_READOOB */
  555. if (command == NAND_CMD_READOOB) {
  556. column += mtd->writesize;
  557. command = NAND_CMD_READ0;
  558. }
  559. /* Command latch cycle */
  560. chip->cmd_ctrl(mtd, command & 0xff,
  561. NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
  562. if (column != -1 || page_addr != -1) {
  563. int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE;
  564. /* Serially input address */
  565. if (column != -1) {
  566. /* Adjust columns for 16 bit buswidth */
  567. if (chip->options & NAND_BUSWIDTH_16)
  568. column >>= 1;
  569. chip->cmd_ctrl(mtd, column, ctrl);
  570. ctrl &= ~NAND_CTRL_CHANGE;
  571. chip->cmd_ctrl(mtd, column >> 8, ctrl);
  572. }
  573. if (page_addr != -1) {
  574. chip->cmd_ctrl(mtd, page_addr, ctrl);
  575. chip->cmd_ctrl(mtd, page_addr >> 8,
  576. NAND_NCE | NAND_ALE);
  577. /* One more address cycle for devices > 128MiB */
  578. if (chip->chipsize > (128 << 20))
  579. chip->cmd_ctrl(mtd, page_addr >> 16,
  580. NAND_NCE | NAND_ALE);
  581. }
  582. }
  583. chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
  584. /*
  585. * program and erase have their own busy handlers
  586. * status, sequential in, and deplete1 need no delay
  587. */
  588. switch (command) {
  589. case NAND_CMD_CACHEDPROG:
  590. case NAND_CMD_PAGEPROG:
  591. case NAND_CMD_ERASE1:
  592. case NAND_CMD_ERASE2:
  593. case NAND_CMD_SEQIN:
  594. case NAND_CMD_RNDIN:
  595. case NAND_CMD_STATUS:
  596. case NAND_CMD_DEPLETE1:
  597. return;
  598. /*
  599. * read error status commands require only a short delay
  600. */
  601. case NAND_CMD_STATUS_ERROR:
  602. case NAND_CMD_STATUS_ERROR0:
  603. case NAND_CMD_STATUS_ERROR1:
  604. case NAND_CMD_STATUS_ERROR2:
  605. case NAND_CMD_STATUS_ERROR3:
  606. udelay(chip->chip_delay);
  607. return;
  608. case NAND_CMD_RESET:
  609. if (chip->dev_ready)
  610. break;
  611. udelay(chip->chip_delay);
  612. chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
  613. NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
  614. chip->cmd_ctrl(mtd, NAND_CMD_NONE,
  615. NAND_NCE | NAND_CTRL_CHANGE);
  616. while (!(chip->read_byte(mtd) & NAND_STATUS_READY)) ;
  617. return;
  618. case NAND_CMD_RNDOUT:
  619. /* No ready / busy check necessary */
  620. chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART,
  621. NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
  622. chip->cmd_ctrl(mtd, NAND_CMD_NONE,
  623. NAND_NCE | NAND_CTRL_CHANGE);
  624. return;
  625. case NAND_CMD_READ0:
  626. chip->cmd_ctrl(mtd, NAND_CMD_READSTART,
  627. NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
  628. chip->cmd_ctrl(mtd, NAND_CMD_NONE,
  629. NAND_NCE | NAND_CTRL_CHANGE);
  630. /* This applies to read commands */
  631. default:
  632. /*
  633. * If we don't have access to the busy pin, we apply the given
  634. * command delay
  635. */
  636. if (!chip->dev_ready) {
  637. udelay(chip->chip_delay);
  638. return;
  639. }
  640. }
  641. /* Apply this short delay always to ensure that we do wait tWB in
  642. * any case on any machine. */
  643. ndelay(100);
  644. nand_wait_ready(mtd);
  645. }
  646. /**
  647. * nand_get_device - [GENERIC] Get chip for selected access
  648. * @chip: the nand chip descriptor
  649. * @mtd: MTD device structure
  650. * @new_state: the state which is requested
  651. *
  652. * Get the device and lock it for exclusive access
  653. */
  654. /* XXX U-BOOT XXX */
  655. #if 0
  656. static int
  657. nand_get_device(struct nand_chip *chip, struct mtd_info *mtd, int new_state)
  658. {
  659. spinlock_t *lock = &chip->controller->lock;
  660. wait_queue_head_t *wq = &chip->controller->wq;
  661. DECLARE_WAITQUEUE(wait, current);
  662. retry:
  663. spin_lock(lock);
  664. /* Hardware controller shared among independend devices */
  665. /* Hardware controller shared among independend devices */
  666. if (!chip->controller->active)
  667. chip->controller->active = chip;
  668. if (chip->controller->active == chip && chip->state == FL_READY) {
  669. chip->state = new_state;
  670. spin_unlock(lock);
  671. return 0;
  672. }
  673. if (new_state == FL_PM_SUSPENDED) {
  674. spin_unlock(lock);
  675. return (chip->state == FL_PM_SUSPENDED) ? 0 : -EAGAIN;
  676. }
  677. set_current_state(TASK_UNINTERRUPTIBLE);
  678. add_wait_queue(wq, &wait);
  679. spin_unlock(lock);
  680. schedule();
  681. remove_wait_queue(wq, &wait);
  682. goto retry;
  683. }
  684. #else
  685. static int nand_get_device (struct nand_chip *this, struct mtd_info *mtd, int new_state)
  686. {
  687. this->state = new_state;
  688. return 0;
  689. }
  690. #endif
  691. /**
  692. * nand_wait - [DEFAULT] wait until the command is done
  693. * @mtd: MTD device structure
  694. * @chip: NAND chip structure
  695. *
  696. * Wait for command done. This applies to erase and program only
  697. * Erase can take up to 400ms and program up to 20ms according to
  698. * general NAND and SmartMedia specs
  699. */
  700. /* XXX U-BOOT XXX */
  701. #if 0
  702. static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip)
  703. {
  704. unsigned long timeo = jiffies;
  705. int status, state = chip->state;
  706. if (state == FL_ERASING)
  707. timeo += (HZ * 400) / 1000;
  708. else
  709. timeo += (HZ * 20) / 1000;
  710. led_trigger_event(nand_led_trigger, LED_FULL);
  711. /* Apply this short delay always to ensure that we do wait tWB in
  712. * any case on any machine. */
  713. ndelay(100);
  714. if ((state == FL_ERASING) && (chip->options & NAND_IS_AND))
  715. chip->cmdfunc(mtd, NAND_CMD_STATUS_MULTI, -1, -1);
  716. else
  717. chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
  718. while (time_before(jiffies, timeo)) {
  719. if (chip->dev_ready) {
  720. if (chip->dev_ready(mtd))
  721. break;
  722. } else {
  723. if (chip->read_byte(mtd) & NAND_STATUS_READY)
  724. break;
  725. }
  726. cond_resched();
  727. }
  728. led_trigger_event(nand_led_trigger, LED_OFF);
  729. status = (int)chip->read_byte(mtd);
  730. return status;
  731. }
  732. #else
  733. static int nand_wait(struct mtd_info *mtd, struct nand_chip *this)
  734. {
  735. unsigned long timeo;
  736. int state = this->state;
  737. if (state == FL_ERASING)
  738. timeo = (CFG_HZ * 400) / 1000;
  739. else
  740. timeo = (CFG_HZ * 20) / 1000;
  741. if ((state == FL_ERASING) && (this->options & NAND_IS_AND))
  742. this->cmdfunc(mtd, NAND_CMD_STATUS_MULTI, -1, -1);
  743. else
  744. this->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
  745. reset_timer();
  746. while (1) {
  747. if (get_timer(0) > timeo) {
  748. printf("Timeout!");
  749. return 0x01;
  750. }
  751. if (this->dev_ready) {
  752. if (this->dev_ready(mtd))
  753. break;
  754. } else {
  755. if (this->read_byte(mtd) & NAND_STATUS_READY)
  756. break;
  757. }
  758. }
  759. #ifdef PPCHAMELON_NAND_TIMER_HACK
  760. reset_timer();
  761. while (get_timer(0) < 10);
  762. #endif /* PPCHAMELON_NAND_TIMER_HACK */
  763. return this->read_byte(mtd);
  764. }
  765. #endif
  766. /**
  767. * nand_read_page_raw - [Intern] read raw page data without ecc
  768. * @mtd: mtd info structure
  769. * @chip: nand chip info structure
  770. * @buf: buffer to store read data
  771. */
  772. static int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
  773. uint8_t *buf)
  774. {
  775. chip->read_buf(mtd, buf, mtd->writesize);
  776. chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
  777. return 0;
  778. }
  779. /**
  780. * nand_read_page_swecc - [REPLACABLE] software ecc based page read function
  781. * @mtd: mtd info structure
  782. * @chip: nand chip info structure
  783. * @buf: buffer to store read data
  784. */
  785. static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
  786. uint8_t *buf)
  787. {
  788. int i, eccsize = chip->ecc.size;
  789. int eccbytes = chip->ecc.bytes;
  790. int eccsteps = chip->ecc.steps;
  791. uint8_t *p = buf;
  792. uint8_t *ecc_calc = chip->buffers->ecccalc;
  793. uint8_t *ecc_code = chip->buffers->ecccode;
  794. uint32_t *eccpos = chip->ecc.layout->eccpos;
  795. chip->ecc.read_page_raw(mtd, chip, buf);
  796. for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
  797. chip->ecc.calculate(mtd, p, &ecc_calc[i]);
  798. for (i = 0; i < chip->ecc.total; i++)
  799. ecc_code[i] = chip->oob_poi[eccpos[i]];
  800. eccsteps = chip->ecc.steps;
  801. p = buf;
  802. for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
  803. int stat;
  804. stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
  805. if (stat == -1)
  806. mtd->ecc_stats.failed++;
  807. else
  808. mtd->ecc_stats.corrected += stat;
  809. }
  810. return 0;
  811. }
  812. /**
  813. * nand_read_page_hwecc - [REPLACABLE] hardware ecc based page read function
  814. * @mtd: mtd info structure
  815. * @chip: nand chip info structure
  816. * @buf: buffer to store read data
  817. *
  818. * Not for syndrome calculating ecc controllers which need a special oob layout
  819. */
  820. static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
  821. uint8_t *buf)
  822. {
  823. int i, eccsize = chip->ecc.size;
  824. int eccbytes = chip->ecc.bytes;
  825. int eccsteps = chip->ecc.steps;
  826. uint8_t *p = buf;
  827. uint8_t *ecc_calc = chip->buffers->ecccalc;
  828. uint8_t *ecc_code = chip->buffers->ecccode;
  829. uint32_t *eccpos = chip->ecc.layout->eccpos;
  830. for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
  831. chip->ecc.hwctl(mtd, NAND_ECC_READ);
  832. chip->read_buf(mtd, p, eccsize);
  833. chip->ecc.calculate(mtd, p, &ecc_calc[i]);
  834. }
  835. chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
  836. for (i = 0; i < chip->ecc.total; i++)
  837. ecc_code[i] = chip->oob_poi[eccpos[i]];
  838. eccsteps = chip->ecc.steps;
  839. p = buf;
  840. for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
  841. int stat;
  842. stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
  843. if (stat == -1)
  844. mtd->ecc_stats.failed++;
  845. else
  846. mtd->ecc_stats.corrected += stat;
  847. }
  848. return 0;
  849. }
  850. /**
  851. * nand_read_page_syndrome - [REPLACABLE] hardware ecc syndrom based page read
  852. * @mtd: mtd info structure
  853. * @chip: nand chip info structure
  854. * @buf: buffer to store read data
  855. *
  856. * The hw generator calculates the error syndrome automatically. Therefor
  857. * we need a special oob layout and handling.
  858. */
  859. static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
  860. uint8_t *buf)
  861. {
  862. int i, eccsize = chip->ecc.size;
  863. int eccbytes = chip->ecc.bytes;
  864. int eccsteps = chip->ecc.steps;
  865. uint8_t *p = buf;
  866. uint8_t *oob = chip->oob_poi;
  867. for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
  868. int stat;
  869. chip->ecc.hwctl(mtd, NAND_ECC_READ);
  870. chip->read_buf(mtd, p, eccsize);
  871. if (chip->ecc.prepad) {
  872. chip->read_buf(mtd, oob, chip->ecc.prepad);
  873. oob += chip->ecc.prepad;
  874. }
  875. chip->ecc.hwctl(mtd, NAND_ECC_READSYN);
  876. chip->read_buf(mtd, oob, eccbytes);
  877. stat = chip->ecc.correct(mtd, p, oob, NULL);
  878. if (stat == -1)
  879. mtd->ecc_stats.failed++;
  880. else
  881. mtd->ecc_stats.corrected += stat;
  882. oob += eccbytes;
  883. if (chip->ecc.postpad) {
  884. chip->read_buf(mtd, oob, chip->ecc.postpad);
  885. oob += chip->ecc.postpad;
  886. }
  887. }
  888. /* Calculate remaining oob bytes */
  889. i = mtd->oobsize - (oob - chip->oob_poi);
  890. if (i)
  891. chip->read_buf(mtd, oob, i);
  892. return 0;
  893. }
  894. /**
  895. * nand_transfer_oob - [Internal] Transfer oob to client buffer
  896. * @chip: nand chip structure
  897. * @oob: oob destination address
  898. * @ops: oob ops structure
  899. * @len: size of oob to transfer
  900. */
  901. static uint8_t *nand_transfer_oob(struct nand_chip *chip, uint8_t *oob,
  902. struct mtd_oob_ops *ops, size_t len)
  903. {
  904. switch(ops->mode) {
  905. case MTD_OOB_PLACE:
  906. case MTD_OOB_RAW:
  907. memcpy(oob, chip->oob_poi + ops->ooboffs, len);
  908. return oob + len;
  909. case MTD_OOB_AUTO: {
  910. struct nand_oobfree *free = chip->ecc.layout->oobfree;
  911. uint32_t boffs = 0, roffs = ops->ooboffs;
  912. size_t bytes = 0;
  913. for(; free->length && len; free++, len -= bytes) {
  914. /* Read request not from offset 0 ? */
  915. if (unlikely(roffs)) {
  916. if (roffs >= free->length) {
  917. roffs -= free->length;
  918. continue;
  919. }
  920. boffs = free->offset + roffs;
  921. bytes = min_t(size_t, len,
  922. (free->length - roffs));
  923. roffs = 0;
  924. } else {
  925. bytes = min_t(size_t, len, free->length);
  926. boffs = free->offset;
  927. }
  928. memcpy(oob, chip->oob_poi + boffs, bytes);
  929. oob += bytes;
  930. }
  931. return oob;
  932. }
  933. default:
  934. BUG();
  935. }
  936. return NULL;
  937. }
  938. /**
  939. * nand_do_read_ops - [Internal] Read data with ECC
  940. *
  941. * @mtd: MTD device structure
  942. * @from: offset to read from
  943. * @ops: oob ops structure
  944. *
  945. * Internal function. Called with chip held.
  946. */
  947. static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
  948. struct mtd_oob_ops *ops)
  949. {
  950. int chipnr, page, realpage, col, bytes, aligned;
  951. struct nand_chip *chip = mtd->priv;
  952. struct mtd_ecc_stats stats;
  953. int blkcheck = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
  954. int sndcmd = 1;
  955. int ret = 0;
  956. uint32_t readlen = ops->len;
  957. uint32_t oobreadlen = ops->ooblen;
  958. uint8_t *bufpoi, *oob, *buf;
  959. stats = mtd->ecc_stats;
  960. chipnr = (int)(from >> chip->chip_shift);
  961. chip->select_chip(mtd, chipnr);
  962. realpage = (int)(from >> chip->page_shift);
  963. page = realpage & chip->pagemask;
  964. col = (int)(from & (mtd->writesize - 1));
  965. buf = ops->datbuf;
  966. oob = ops->oobbuf;
  967. while(1) {
  968. bytes = min(mtd->writesize - col, readlen);
  969. aligned = (bytes == mtd->writesize);
  970. /* Is the current page in the buffer ? */
  971. if (realpage != chip->pagebuf || oob) {
  972. bufpoi = aligned ? buf : chip->buffers->databuf;
  973. if (likely(sndcmd)) {
  974. chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
  975. sndcmd = 0;
  976. }
  977. /* Now read the page into the buffer */
  978. if (unlikely(ops->mode == MTD_OOB_RAW))
  979. ret = chip->ecc.read_page_raw(mtd, chip, bufpoi);
  980. else
  981. ret = chip->ecc.read_page(mtd, chip, bufpoi);
  982. if (ret < 0)
  983. break;
  984. /* Transfer not aligned data */
  985. if (!aligned) {
  986. chip->pagebuf = realpage;
  987. memcpy(buf, chip->buffers->databuf + col, bytes);
  988. }
  989. buf += bytes;
  990. if (unlikely(oob)) {
  991. /* Raw mode does data:oob:data:oob */
  992. if (ops->mode != MTD_OOB_RAW) {
  993. int toread = min(oobreadlen,
  994. chip->ecc.layout->oobavail);
  995. if (toread) {
  996. oob = nand_transfer_oob(chip,
  997. oob, ops, toread);
  998. oobreadlen -= toread;
  999. }
  1000. } else
  1001. buf = nand_transfer_oob(chip,
  1002. buf, ops, mtd->oobsize);
  1003. }
  1004. if (!(chip->options & NAND_NO_READRDY)) {
  1005. /*
  1006. * Apply delay or wait for ready/busy pin. Do
  1007. * this before the AUTOINCR check, so no
  1008. * problems arise if a chip which does auto
  1009. * increment is marked as NOAUTOINCR by the
  1010. * board driver.
  1011. */
  1012. if (!chip->dev_ready)
  1013. udelay(chip->chip_delay);
  1014. else
  1015. nand_wait_ready(mtd);
  1016. }
  1017. } else {
  1018. memcpy(buf, chip->buffers->databuf + col, bytes);
  1019. buf += bytes;
  1020. }
  1021. readlen -= bytes;
  1022. if (!readlen)
  1023. break;
  1024. /* For subsequent reads align to page boundary. */
  1025. col = 0;
  1026. /* Increment page address */
  1027. realpage++;
  1028. page = realpage & chip->pagemask;
  1029. /* Check, if we cross a chip boundary */
  1030. if (!page) {
  1031. chipnr++;
  1032. chip->select_chip(mtd, -1);
  1033. chip->select_chip(mtd, chipnr);
  1034. }
  1035. /* Check, if the chip supports auto page increment
  1036. * or if we have hit a block boundary.
  1037. */
  1038. if (!NAND_CANAUTOINCR(chip) || !(page & blkcheck))
  1039. sndcmd = 1;
  1040. }
  1041. ops->retlen = ops->len - (size_t) readlen;
  1042. if (oob)
  1043. ops->oobretlen = ops->ooblen - oobreadlen;
  1044. if (ret)
  1045. return ret;
  1046. if (mtd->ecc_stats.failed - stats.failed)
  1047. return -EBADMSG;
  1048. return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
  1049. }
  1050. /**
  1051. * nand_read - [MTD Interface] MTD compability function for nand_do_read_ecc
  1052. * @mtd: MTD device structure
  1053. * @from: offset to read from
  1054. * @len: number of bytes to read
  1055. * @retlen: pointer to variable to store the number of read bytes
  1056. * @buf: the databuffer to put data
  1057. *
  1058. * Get hold of the chip and call nand_do_read
  1059. */
  1060. static int nand_read(struct mtd_info *mtd, loff_t from, size_t len,
  1061. size_t *retlen, uint8_t *buf)
  1062. {
  1063. struct nand_chip *chip = mtd->priv;
  1064. int ret;
  1065. /* Do not allow reads past end of device */
  1066. if ((from + len) > mtd->size)
  1067. return -EINVAL;
  1068. if (!len)
  1069. return 0;
  1070. nand_get_device(chip, mtd, FL_READING);
  1071. chip->ops.len = len;
  1072. chip->ops.datbuf = buf;
  1073. chip->ops.oobbuf = NULL;
  1074. ret = nand_do_read_ops(mtd, from, &chip->ops);
  1075. *retlen = chip->ops.retlen;
  1076. nand_release_device(mtd);
  1077. return ret;
  1078. }
  1079. /**
  1080. * nand_read_oob_std - [REPLACABLE] the most common OOB data read function
  1081. * @mtd: mtd info structure
  1082. * @chip: nand chip info structure
  1083. * @page: page number to read
  1084. * @sndcmd: flag whether to issue read command or not
  1085. */
  1086. static int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
  1087. int page, int sndcmd)
  1088. {
  1089. if (sndcmd) {
  1090. chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
  1091. sndcmd = 0;
  1092. }
  1093. chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
  1094. return sndcmd;
  1095. }
  1096. /**
  1097. * nand_read_oob_syndrome - [REPLACABLE] OOB data read function for HW ECC
  1098. * with syndromes
  1099. * @mtd: mtd info structure
  1100. * @chip: nand chip info structure
  1101. * @page: page number to read
  1102. * @sndcmd: flag whether to issue read command or not
  1103. */
  1104. static int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
  1105. int page, int sndcmd)
  1106. {
  1107. uint8_t *buf = chip->oob_poi;
  1108. int length = mtd->oobsize;
  1109. int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
  1110. int eccsize = chip->ecc.size;
  1111. uint8_t *bufpoi = buf;
  1112. int i, toread, sndrnd = 0, pos;
  1113. chip->cmdfunc(mtd, NAND_CMD_READ0, chip->ecc.size, page);
  1114. for (i = 0; i < chip->ecc.steps; i++) {
  1115. if (sndrnd) {
  1116. pos = eccsize + i * (eccsize + chunk);
  1117. if (mtd->writesize > 512)
  1118. chip->cmdfunc(mtd, NAND_CMD_RNDOUT, pos, -1);
  1119. else
  1120. chip->cmdfunc(mtd, NAND_CMD_READ0, pos, page);
  1121. } else
  1122. sndrnd = 1;
  1123. toread = min_t(int, length, chunk);
  1124. chip->read_buf(mtd, bufpoi, toread);
  1125. bufpoi += toread;
  1126. length -= toread;
  1127. }
  1128. if (length > 0)
  1129. chip->read_buf(mtd, bufpoi, length);
  1130. return 1;
  1131. }
  1132. /**
  1133. * nand_write_oob_std - [REPLACABLE] the most common OOB data write function
  1134. * @mtd: mtd info structure
  1135. * @chip: nand chip info structure
  1136. * @page: page number to write
  1137. */
  1138. static int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
  1139. int page)
  1140. {
  1141. int status = 0;
  1142. const uint8_t *buf = chip->oob_poi;
  1143. int length = mtd->oobsize;
  1144. chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
  1145. chip->write_buf(mtd, buf, length);
  1146. /* Send command to program the OOB data */
  1147. chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
  1148. status = chip->waitfunc(mtd, chip);
  1149. return status & NAND_STATUS_FAIL ? -EIO : 0;
  1150. }
  1151. /**
  1152. * nand_write_oob_syndrome - [REPLACABLE] OOB data write function for HW ECC
  1153. * with syndrome - only for large page flash !
  1154. * @mtd: mtd info structure
  1155. * @chip: nand chip info structure
  1156. * @page: page number to write
  1157. */
  1158. static int nand_write_oob_syndrome(struct mtd_info *mtd,
  1159. struct nand_chip *chip, int page)
  1160. {
  1161. int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
  1162. int eccsize = chip->ecc.size, length = mtd->oobsize;
  1163. int i, len, pos, status = 0, sndcmd = 0, steps = chip->ecc.steps;
  1164. const uint8_t *bufpoi = chip->oob_poi;
  1165. /*
  1166. * data-ecc-data-ecc ... ecc-oob
  1167. * or
  1168. * data-pad-ecc-pad-data-pad .... ecc-pad-oob
  1169. */
  1170. if (!chip->ecc.prepad && !chip->ecc.postpad) {
  1171. pos = steps * (eccsize + chunk);
  1172. steps = 0;
  1173. } else
  1174. pos = eccsize;
  1175. chip->cmdfunc(mtd, NAND_CMD_SEQIN, pos, page);
  1176. for (i = 0; i < steps; i++) {
  1177. if (sndcmd) {
  1178. if (mtd->writesize <= 512) {
  1179. uint32_t fill = 0xFFFFFFFF;
  1180. len = eccsize;
  1181. while (len > 0) {
  1182. int num = min_t(int, len, 4);
  1183. chip->write_buf(mtd, (uint8_t *)&fill,
  1184. num);
  1185. len -= num;
  1186. }
  1187. } else {
  1188. pos = eccsize + i * (eccsize + chunk);
  1189. chip->cmdfunc(mtd, NAND_CMD_RNDIN, pos, -1);
  1190. }
  1191. } else
  1192. sndcmd = 1;
  1193. len = min_t(int, length, chunk);
  1194. chip->write_buf(mtd, bufpoi, len);
  1195. bufpoi += len;
  1196. length -= len;
  1197. }
  1198. if (length > 0)
  1199. chip->write_buf(mtd, bufpoi, length);
  1200. chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
  1201. status = chip->waitfunc(mtd, chip);
  1202. return status & NAND_STATUS_FAIL ? -EIO : 0;
  1203. }
  1204. /**
  1205. * nand_do_read_oob - [Intern] NAND read out-of-band
  1206. * @mtd: MTD device structure
  1207. * @from: offset to read from
  1208. * @ops: oob operations description structure
  1209. *
  1210. * NAND read out-of-band data from the spare area
  1211. */
  1212. static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
  1213. struct mtd_oob_ops *ops)
  1214. {
  1215. int page, realpage, chipnr, sndcmd = 1;
  1216. struct nand_chip *chip = mtd->priv;
  1217. int blkcheck = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
  1218. int readlen = ops->ooblen;
  1219. int len;
  1220. uint8_t *buf = ops->oobbuf;
  1221. MTDDEBUG (MTD_DEBUG_LEVEL3, "nand_read_oob: from = 0x%08Lx, len = %i\n",
  1222. (unsigned long long)from, readlen);
  1223. if (ops->mode == MTD_OOB_AUTO)
  1224. len = chip->ecc.layout->oobavail;
  1225. else
  1226. len = mtd->oobsize;
  1227. if (unlikely(ops->ooboffs >= len)) {
  1228. MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_read_oob: "
  1229. "Attempt to start read outside oob\n");
  1230. return -EINVAL;
  1231. }
  1232. /* Do not allow reads past end of device */
  1233. if (unlikely(from >= mtd->size ||
  1234. ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) -
  1235. (from >> chip->page_shift)) * len)) {
  1236. MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_read_oob: "
  1237. "Attempt read beyond end of device\n");
  1238. return -EINVAL;
  1239. }
  1240. chipnr = (int)(from >> chip->chip_shift);
  1241. chip->select_chip(mtd, chipnr);
  1242. /* Shift to get page */
  1243. realpage = (int)(from >> chip->page_shift);
  1244. page = realpage & chip->pagemask;
  1245. while(1) {
  1246. sndcmd = chip->ecc.read_oob(mtd, chip, page, sndcmd);
  1247. len = min(len, readlen);
  1248. buf = nand_transfer_oob(chip, buf, ops, len);
  1249. if (!(chip->options & NAND_NO_READRDY)) {
  1250. /*
  1251. * Apply delay or wait for ready/busy pin. Do this
  1252. * before the AUTOINCR check, so no problems arise if a
  1253. * chip which does auto increment is marked as
  1254. * NOAUTOINCR by the board driver.
  1255. */
  1256. if (!chip->dev_ready)
  1257. udelay(chip->chip_delay);
  1258. else
  1259. nand_wait_ready(mtd);
  1260. }
  1261. readlen -= len;
  1262. if (!readlen)
  1263. break;
  1264. /* Increment page address */
  1265. realpage++;
  1266. page = realpage & chip->pagemask;
  1267. /* Check, if we cross a chip boundary */
  1268. if (!page) {
  1269. chipnr++;
  1270. chip->select_chip(mtd, -1);
  1271. chip->select_chip(mtd, chipnr);
  1272. }
  1273. /* Check, if the chip supports auto page increment
  1274. * or if we have hit a block boundary.
  1275. */
  1276. if (!NAND_CANAUTOINCR(chip) || !(page & blkcheck))
  1277. sndcmd = 1;
  1278. }
  1279. ops->oobretlen = ops->ooblen;
  1280. return 0;
  1281. }
  1282. /**
  1283. * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
  1284. * @mtd: MTD device structure
  1285. * @from: offset to read from
  1286. * @ops: oob operation description structure
  1287. *
  1288. * NAND read data and/or out-of-band data
  1289. */
  1290. static int nand_read_oob(struct mtd_info *mtd, loff_t from,
  1291. struct mtd_oob_ops *ops)
  1292. {
  1293. struct nand_chip *chip = mtd->priv;
  1294. int ret = -ENOTSUPP;
  1295. ops->retlen = 0;
  1296. /* Do not allow reads past end of device */
  1297. if (ops->datbuf && (from + ops->len) > mtd->size) {
  1298. MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_read_oob: "
  1299. "Attempt read beyond end of device\n");
  1300. return -EINVAL;
  1301. }
  1302. nand_get_device(chip, mtd, FL_READING);
  1303. switch(ops->mode) {
  1304. case MTD_OOB_PLACE:
  1305. case MTD_OOB_AUTO:
  1306. case MTD_OOB_RAW:
  1307. break;
  1308. default:
  1309. goto out;
  1310. }
  1311. if (!ops->datbuf)
  1312. ret = nand_do_read_oob(mtd, from, ops);
  1313. else
  1314. ret = nand_do_read_ops(mtd, from, ops);
  1315. out:
  1316. nand_release_device(mtd);
  1317. return ret;
  1318. }
  1319. /**
  1320. * nand_write_page_raw - [Intern] raw page write function
  1321. * @mtd: mtd info structure
  1322. * @chip: nand chip info structure
  1323. * @buf: data buffer
  1324. */
  1325. static void nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
  1326. const uint8_t *buf)
  1327. {
  1328. chip->write_buf(mtd, buf, mtd->writesize);
  1329. chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
  1330. }
  1331. /**
  1332. * nand_write_page_swecc - [REPLACABLE] software ecc based page write function
  1333. * @mtd: mtd info structure
  1334. * @chip: nand chip info structure
  1335. * @buf: data buffer
  1336. */
  1337. static void nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
  1338. const uint8_t *buf)
  1339. {
  1340. int i, eccsize = chip->ecc.size;
  1341. int eccbytes = chip->ecc.bytes;
  1342. int eccsteps = chip->ecc.steps;
  1343. uint8_t *ecc_calc = chip->buffers->ecccalc;
  1344. const uint8_t *p = buf;
  1345. uint32_t *eccpos = chip->ecc.layout->eccpos;
  1346. /* Software ecc calculation */
  1347. for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
  1348. chip->ecc.calculate(mtd, p, &ecc_calc[i]);
  1349. for (i = 0; i < chip->ecc.total; i++)
  1350. chip->oob_poi[eccpos[i]] = ecc_calc[i];
  1351. chip->ecc.write_page_raw(mtd, chip, buf);
  1352. }
  1353. /**
  1354. * nand_write_page_hwecc - [REPLACABLE] hardware ecc based page write function
  1355. * @mtd: mtd info structure
  1356. * @chip: nand chip info structure
  1357. * @buf: data buffer
  1358. */
  1359. static void nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
  1360. const uint8_t *buf)
  1361. {
  1362. int i, eccsize = chip->ecc.size;
  1363. int eccbytes = chip->ecc.bytes;
  1364. int eccsteps = chip->ecc.steps;
  1365. uint8_t *ecc_calc = chip->buffers->ecccalc;
  1366. const uint8_t *p = buf;
  1367. uint32_t *eccpos = chip->ecc.layout->eccpos;
  1368. for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
  1369. chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
  1370. chip->write_buf(mtd, p, eccsize);
  1371. chip->ecc.calculate(mtd, p, &ecc_calc[i]);
  1372. }
  1373. for (i = 0; i < chip->ecc.total; i++)
  1374. chip->oob_poi[eccpos[i]] = ecc_calc[i];
  1375. chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
  1376. }
  1377. /**
  1378. * nand_write_page_syndrome - [REPLACABLE] hardware ecc syndrom based page write
  1379. * @mtd: mtd info structure
  1380. * @chip: nand chip info structure
  1381. * @buf: data buffer
  1382. *
  1383. * The hw generator calculates the error syndrome automatically. Therefor
  1384. * we need a special oob layout and handling.
  1385. */
  1386. static void nand_write_page_syndrome(struct mtd_info *mtd,
  1387. struct nand_chip *chip, const uint8_t *buf)
  1388. {
  1389. int i, eccsize = chip->ecc.size;
  1390. int eccbytes = chip->ecc.bytes;
  1391. int eccsteps = chip->ecc.steps;
  1392. const uint8_t *p = buf;
  1393. uint8_t *oob = chip->oob_poi;
  1394. for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
  1395. chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
  1396. chip->write_buf(mtd, p, eccsize);
  1397. if (chip->ecc.prepad) {
  1398. chip->write_buf(mtd, oob, chip->ecc.prepad);
  1399. oob += chip->ecc.prepad;
  1400. }
  1401. chip->ecc.calculate(mtd, p, oob);
  1402. chip->write_buf(mtd, oob, eccbytes);
  1403. oob += eccbytes;
  1404. if (chip->ecc.postpad) {
  1405. chip->write_buf(mtd, oob, chip->ecc.postpad);
  1406. oob += chip->ecc.postpad;
  1407. }
  1408. }
  1409. /* Calculate remaining oob bytes */
  1410. i = mtd->oobsize - (oob - chip->oob_poi);
  1411. if (i)
  1412. chip->write_buf(mtd, oob, i);
  1413. }
  1414. /**
  1415. * nand_write_page - [REPLACEABLE] write one page
  1416. * @mtd: MTD device structure
  1417. * @chip: NAND chip descriptor
  1418. * @buf: the data to write
  1419. * @page: page number to write
  1420. * @cached: cached programming
  1421. * @raw: use _raw version of write_page
  1422. */
  1423. static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
  1424. const uint8_t *buf, int page, int cached, int raw)
  1425. {
  1426. int status;
  1427. chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
  1428. if (unlikely(raw))
  1429. chip->ecc.write_page_raw(mtd, chip, buf);
  1430. else
  1431. chip->ecc.write_page(mtd, chip, buf);
  1432. /*
  1433. * Cached progamming disabled for now, Not sure if its worth the
  1434. * trouble. The speed gain is not very impressive. (2.3->2.6Mib/s)
  1435. */
  1436. cached = 0;
  1437. if (!cached || !(chip->options & NAND_CACHEPRG)) {
  1438. chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
  1439. status = chip->waitfunc(mtd, chip);
  1440. /*
  1441. * See if operation failed and additional status checks are
  1442. * available
  1443. */
  1444. if ((status & NAND_STATUS_FAIL) && (chip->errstat))
  1445. status = chip->errstat(mtd, chip, FL_WRITING, status,
  1446. page);
  1447. if (status & NAND_STATUS_FAIL)
  1448. return -EIO;
  1449. } else {
  1450. chip->cmdfunc(mtd, NAND_CMD_CACHEDPROG, -1, -1);
  1451. status = chip->waitfunc(mtd, chip);
  1452. }
  1453. #ifdef CONFIG_MTD_NAND_VERIFY_WRITE
  1454. /* Send command to read back the data */
  1455. chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
  1456. if (chip->verify_buf(mtd, buf, mtd->writesize))
  1457. return -EIO;
  1458. #endif
  1459. return 0;
  1460. }
  1461. /**
  1462. * nand_fill_oob - [Internal] Transfer client buffer to oob
  1463. * @chip: nand chip structure
  1464. * @oob: oob data buffer
  1465. * @ops: oob ops structure
  1466. */
  1467. static uint8_t *nand_fill_oob(struct nand_chip *chip, uint8_t *oob,
  1468. struct mtd_oob_ops *ops)
  1469. {
  1470. size_t len = ops->ooblen;
  1471. switch(ops->mode) {
  1472. case MTD_OOB_PLACE:
  1473. case MTD_OOB_RAW:
  1474. memcpy(chip->oob_poi + ops->ooboffs, oob, len);
  1475. return oob + len;
  1476. case MTD_OOB_AUTO: {
  1477. struct nand_oobfree *free = chip->ecc.layout->oobfree;
  1478. uint32_t boffs = 0, woffs = ops->ooboffs;
  1479. size_t bytes = 0;
  1480. for(; free->length && len; free++, len -= bytes) {
  1481. /* Write request not from offset 0 ? */
  1482. if (unlikely(woffs)) {
  1483. if (woffs >= free->length) {
  1484. woffs -= free->length;
  1485. continue;
  1486. }
  1487. boffs = free->offset + woffs;
  1488. bytes = min_t(size_t, len,
  1489. (free->length - woffs));
  1490. woffs = 0;
  1491. } else {
  1492. bytes = min_t(size_t, len, free->length);
  1493. boffs = free->offset;
  1494. }
  1495. memcpy(chip->oob_poi + boffs, oob, bytes);
  1496. oob += bytes;
  1497. }
  1498. return oob;
  1499. }
  1500. default:
  1501. BUG();
  1502. }
  1503. return NULL;
  1504. }
  1505. #define NOTALIGNED(x) (x & (chip->subpagesize - 1)) != 0
  1506. /**
  1507. * nand_do_write_ops - [Internal] NAND write with ECC
  1508. * @mtd: MTD device structure
  1509. * @to: offset to write to
  1510. * @ops: oob operations description structure
  1511. *
  1512. * NAND write with ECC
  1513. */
  1514. static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
  1515. struct mtd_oob_ops *ops)
  1516. {
  1517. int chipnr, realpage, page, blockmask, column;
  1518. struct nand_chip *chip = mtd->priv;
  1519. uint32_t writelen = ops->len;
  1520. uint8_t *oob = ops->oobbuf;
  1521. uint8_t *buf = ops->datbuf;
  1522. int ret, subpage;
  1523. ops->retlen = 0;
  1524. if (!writelen)
  1525. return 0;
  1526. /* reject writes, which are not page aligned */
  1527. if (NOTALIGNED(to) || NOTALIGNED(ops->len)) {
  1528. printk(KERN_NOTICE "nand_write: "
  1529. "Attempt to write not page aligned data\n");
  1530. return -EINVAL;
  1531. }
  1532. column = to & (mtd->writesize - 1);
  1533. subpage = column || (writelen & (mtd->writesize - 1));
  1534. if (subpage && oob)
  1535. return -EINVAL;
  1536. chipnr = (int)(to >> chip->chip_shift);
  1537. chip->select_chip(mtd, chipnr);
  1538. /* Check, if it is write protected */
  1539. if (nand_check_wp(mtd)) {
  1540. printk (KERN_NOTICE "nand_do_write_ops: Device is write protected\n");
  1541. return -EIO;
  1542. }
  1543. realpage = (int)(to >> chip->page_shift);
  1544. page = realpage & chip->pagemask;
  1545. blockmask = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
  1546. /* Invalidate the page cache, when we write to the cached page */
  1547. if (to <= (chip->pagebuf << chip->page_shift) &&
  1548. (chip->pagebuf << chip->page_shift) < (to + ops->len))
  1549. chip->pagebuf = -1;
  1550. /* If we're not given explicit OOB data, let it be 0xFF */
  1551. if (likely(!oob))
  1552. memset(chip->oob_poi, 0xff, mtd->oobsize);
  1553. while(1) {
  1554. int bytes = mtd->writesize;
  1555. int cached = writelen > bytes && page != blockmask;
  1556. uint8_t *wbuf = buf;
  1557. /* Partial page write ? */
  1558. if (unlikely(column || writelen < (mtd->writesize - 1))) {
  1559. cached = 0;
  1560. bytes = min_t(int, bytes - column, (int) writelen);
  1561. chip->pagebuf = -1;
  1562. memset(chip->buffers->databuf, 0xff, mtd->writesize);
  1563. memcpy(&chip->buffers->databuf[column], buf, bytes);
  1564. wbuf = chip->buffers->databuf;
  1565. }
  1566. if (unlikely(oob))
  1567. oob = nand_fill_oob(chip, oob, ops);
  1568. ret = chip->write_page(mtd, chip, wbuf, page, cached,
  1569. (ops->mode == MTD_OOB_RAW));
  1570. if (ret)
  1571. break;
  1572. writelen -= bytes;
  1573. if (!writelen)
  1574. break;
  1575. column = 0;
  1576. buf += bytes;
  1577. realpage++;
  1578. page = realpage & chip->pagemask;
  1579. /* Check, if we cross a chip boundary */
  1580. if (!page) {
  1581. chipnr++;
  1582. chip->select_chip(mtd, -1);
  1583. chip->select_chip(mtd, chipnr);
  1584. }
  1585. }
  1586. ops->retlen = ops->len - writelen;
  1587. if (unlikely(oob))
  1588. ops->oobretlen = ops->ooblen;
  1589. return ret;
  1590. }
  1591. /**
  1592. * nand_write - [MTD Interface] NAND write with ECC
  1593. * @mtd: MTD device structure
  1594. * @to: offset to write to
  1595. * @len: number of bytes to write
  1596. * @retlen: pointer to variable to store the number of written bytes
  1597. * @buf: the data to write
  1598. *
  1599. * NAND write with ECC
  1600. */
  1601. static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
  1602. size_t *retlen, const uint8_t *buf)
  1603. {
  1604. struct nand_chip *chip = mtd->priv;
  1605. int ret;
  1606. /* Do not allow reads past end of device */
  1607. if ((to + len) > mtd->size)
  1608. return -EINVAL;
  1609. if (!len)
  1610. return 0;
  1611. nand_get_device(chip, mtd, FL_WRITING);
  1612. chip->ops.len = len;
  1613. chip->ops.datbuf = (uint8_t *)buf;
  1614. chip->ops.oobbuf = NULL;
  1615. ret = nand_do_write_ops(mtd, to, &chip->ops);
  1616. *retlen = chip->ops.retlen;
  1617. nand_release_device(mtd);
  1618. return ret;
  1619. }
  1620. /**
  1621. * nand_do_write_oob - [MTD Interface] NAND write out-of-band
  1622. * @mtd: MTD device structure
  1623. * @to: offset to write to
  1624. * @ops: oob operation description structure
  1625. *
  1626. * NAND write out-of-band
  1627. */
  1628. static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
  1629. struct mtd_oob_ops *ops)
  1630. {
  1631. int chipnr, page, status, len;
  1632. struct nand_chip *chip = mtd->priv;
  1633. MTDDEBUG (MTD_DEBUG_LEVEL3, "nand_write_oob: to = 0x%08x, len = %i\n",
  1634. (unsigned int)to, (int)ops->ooblen);
  1635. if (ops->mode == MTD_OOB_AUTO)
  1636. len = chip->ecc.layout->oobavail;
  1637. else
  1638. len = mtd->oobsize;
  1639. /* Do not allow write past end of page */
  1640. if ((ops->ooboffs + ops->ooblen) > len) {
  1641. MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_write_oob: "
  1642. "Attempt to write past end of page\n");
  1643. return -EINVAL;
  1644. }
  1645. if (unlikely(ops->ooboffs >= len)) {
  1646. MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_read_oob: "
  1647. "Attempt to start write outside oob\n");
  1648. return -EINVAL;
  1649. }
  1650. /* Do not allow reads past end of device */
  1651. if (unlikely(to >= mtd->size ||
  1652. ops->ooboffs + ops->ooblen >
  1653. ((mtd->size >> chip->page_shift) -
  1654. (to >> chip->page_shift)) * len)) {
  1655. MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_read_oob: "
  1656. "Attempt write beyond end of device\n");
  1657. return -EINVAL;
  1658. }
  1659. chipnr = (int)(to >> chip->chip_shift);
  1660. chip->select_chip(mtd, chipnr);
  1661. /* Shift to get page */
  1662. page = (int)(to >> chip->page_shift);
  1663. /*
  1664. * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
  1665. * of my DiskOnChip 2000 test units) will clear the whole data page too
  1666. * if we don't do this. I have no clue why, but I seem to have 'fixed'
  1667. * it in the doc2000 driver in August 1999. dwmw2.
  1668. */
  1669. chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
  1670. /* Check, if it is write protected */
  1671. if (nand_check_wp(mtd))
  1672. return -EROFS;
  1673. /* Invalidate the page cache, if we write to the cached page */
  1674. if (page == chip->pagebuf)
  1675. chip->pagebuf = -1;
  1676. memset(chip->oob_poi, 0xff, mtd->oobsize);
  1677. nand_fill_oob(chip, ops->oobbuf, ops);
  1678. status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
  1679. memset(chip->oob_poi, 0xff, mtd->oobsize);
  1680. if (status)
  1681. return status;
  1682. ops->oobretlen = ops->ooblen;
  1683. return 0;
  1684. }
  1685. /**
  1686. * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
  1687. * @mtd: MTD device structure
  1688. * @to: offset to write to
  1689. * @ops: oob operation description structure
  1690. */
  1691. static int nand_write_oob(struct mtd_info *mtd, loff_t to,
  1692. struct mtd_oob_ops *ops)
  1693. {
  1694. struct nand_chip *chip = mtd->priv;
  1695. int ret = -ENOTSUPP;
  1696. ops->retlen = 0;
  1697. /* Do not allow writes past end of device */
  1698. if (ops->datbuf && (to + ops->len) > mtd->size) {
  1699. MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_read_oob: "
  1700. "Attempt read beyond end of device\n");
  1701. return -EINVAL;
  1702. }
  1703. nand_get_device(chip, mtd, FL_WRITING);
  1704. switch(ops->mode) {
  1705. case MTD_OOB_PLACE:
  1706. case MTD_OOB_AUTO:
  1707. case MTD_OOB_RAW:
  1708. break;
  1709. default:
  1710. goto out;
  1711. }
  1712. if (!ops->datbuf)
  1713. ret = nand_do_write_oob(mtd, to, ops);
  1714. else
  1715. ret = nand_do_write_ops(mtd, to, ops);
  1716. out:
  1717. nand_release_device(mtd);
  1718. return ret;
  1719. }
  1720. /**
  1721. * single_erease_cmd - [GENERIC] NAND standard block erase command function
  1722. * @mtd: MTD device structure
  1723. * @page: the page address of the block which will be erased
  1724. *
  1725. * Standard erase command for NAND chips
  1726. */
  1727. static void single_erase_cmd(struct mtd_info *mtd, int page)
  1728. {
  1729. struct nand_chip *chip = mtd->priv;
  1730. /* Send commands to erase a block */
  1731. chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
  1732. chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
  1733. }
  1734. /**
  1735. * multi_erease_cmd - [GENERIC] AND specific block erase command function
  1736. * @mtd: MTD device structure
  1737. * @page: the page address of the block which will be erased
  1738. *
  1739. * AND multi block erase command function
  1740. * Erase 4 consecutive blocks
  1741. */
  1742. static void multi_erase_cmd(struct mtd_info *mtd, int page)
  1743. {
  1744. struct nand_chip *chip = mtd->priv;
  1745. /* Send commands to erase a block */
  1746. chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
  1747. chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
  1748. chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
  1749. chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
  1750. chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
  1751. }
  1752. /**
  1753. * nand_erase - [MTD Interface] erase block(s)
  1754. * @mtd: MTD device structure
  1755. * @instr: erase instruction
  1756. *
  1757. * Erase one ore more blocks
  1758. */
  1759. static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
  1760. {
  1761. return nand_erase_nand(mtd, instr, 0);
  1762. }
  1763. #define BBT_PAGE_MASK 0xffffff3f
  1764. /**
  1765. * nand_erase_nand - [Internal] erase block(s)
  1766. * @mtd: MTD device structure
  1767. * @instr: erase instruction
  1768. * @allowbbt: allow erasing the bbt area
  1769. *
  1770. * Erase one ore more blocks
  1771. */
  1772. int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
  1773. int allowbbt)
  1774. {
  1775. int page, len, status, pages_per_block, ret, chipnr;
  1776. struct nand_chip *chip = mtd->priv;
  1777. int rewrite_bbt[NAND_MAX_CHIPS]={0};
  1778. unsigned int bbt_masked_page = 0xffffffff;
  1779. MTDDEBUG (MTD_DEBUG_LEVEL3, "nand_erase: start = 0x%08x, len = %i\n",
  1780. (unsigned int) instr->addr, (unsigned int) instr->len);
  1781. /* Start address must align on block boundary */
  1782. if (instr->addr & ((1 << chip->phys_erase_shift) - 1)) {
  1783. MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_erase: Unaligned address\n");
  1784. return -EINVAL;
  1785. }
  1786. /* Length must align on block boundary */
  1787. if (instr->len & ((1 << chip->phys_erase_shift) - 1)) {
  1788. MTDDEBUG (MTD_DEBUG_LEVEL0,
  1789. "nand_erase: Length not block aligned\n");
  1790. return -EINVAL;
  1791. }
  1792. /* Do not allow erase past end of device */
  1793. if ((instr->len + instr->addr) > mtd->size) {
  1794. MTDDEBUG (MTD_DEBUG_LEVEL0,
  1795. "nand_erase: Erase past end of device\n");
  1796. return -EINVAL;
  1797. }
  1798. instr->fail_addr = 0xffffffff;
  1799. /* Grab the lock and see if the device is available */
  1800. nand_get_device(chip, mtd, FL_ERASING);
  1801. /* Shift to get first page */
  1802. page = (int)(instr->addr >> chip->page_shift);
  1803. chipnr = (int)(instr->addr >> chip->chip_shift);
  1804. /* Calculate pages in each block */
  1805. pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
  1806. /* Select the NAND device */
  1807. chip->select_chip(mtd, chipnr);
  1808. /* Check, if it is write protected */
  1809. if (nand_check_wp(mtd)) {
  1810. MTDDEBUG (MTD_DEBUG_LEVEL0,
  1811. "nand_erase: Device is write protected!!!\n");
  1812. instr->state = MTD_ERASE_FAILED;
  1813. goto erase_exit;
  1814. }
  1815. /*
  1816. * If BBT requires refresh, set the BBT page mask to see if the BBT
  1817. * should be rewritten. Otherwise the mask is set to 0xffffffff which
  1818. * can not be matched. This is also done when the bbt is actually
  1819. * erased to avoid recusrsive updates
  1820. */
  1821. if (chip->options & BBT_AUTO_REFRESH && !allowbbt)
  1822. bbt_masked_page = chip->bbt_td->pages[chipnr] & BBT_PAGE_MASK;
  1823. /* Loop through the pages */
  1824. len = instr->len;
  1825. instr->state = MTD_ERASING;
  1826. while (len) {
  1827. /*
  1828. * heck if we have a bad block, we do not erase bad blocks !
  1829. */
  1830. if (nand_block_checkbad(mtd, ((loff_t) page) <<
  1831. chip->page_shift, 0, allowbbt)) {
  1832. printk(KERN_WARNING "nand_erase: attempt to erase a "
  1833. "bad block at page 0x%08x\n", page);
  1834. instr->state = MTD_ERASE_FAILED;
  1835. goto erase_exit;
  1836. }
  1837. /*
  1838. * Invalidate the page cache, if we erase the block which
  1839. * contains the current cached page
  1840. */
  1841. if (page <= chip->pagebuf && chip->pagebuf <
  1842. (page + pages_per_block))
  1843. chip->pagebuf = -1;
  1844. chip->erase_cmd(mtd, page & chip->pagemask);
  1845. status = chip->waitfunc(mtd, chip);
  1846. /*
  1847. * See if operation failed and additional status checks are
  1848. * available
  1849. */
  1850. if ((status & NAND_STATUS_FAIL) && (chip->errstat))
  1851. status = chip->errstat(mtd, chip, FL_ERASING,
  1852. status, page);
  1853. /* See if block erase succeeded */
  1854. if (status & NAND_STATUS_FAIL) {
  1855. MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_erase: "
  1856. "Failed erase, page 0x%08x\n", page);
  1857. instr->state = MTD_ERASE_FAILED;
  1858. instr->fail_addr = (page << chip->page_shift);
  1859. goto erase_exit;
  1860. }
  1861. /*
  1862. * If BBT requires refresh, set the BBT rewrite flag to the
  1863. * page being erased
  1864. */
  1865. if (bbt_masked_page != 0xffffffff &&
  1866. (page & BBT_PAGE_MASK) == bbt_masked_page)
  1867. rewrite_bbt[chipnr] = (page << chip->page_shift);
  1868. /* Increment page address and decrement length */
  1869. len -= (1 << chip->phys_erase_shift);
  1870. page += pages_per_block;
  1871. /* Check, if we cross a chip boundary */
  1872. if (len && !(page & chip->pagemask)) {
  1873. chipnr++;
  1874. chip->select_chip(mtd, -1);
  1875. chip->select_chip(mtd, chipnr);
  1876. /*
  1877. * If BBT requires refresh and BBT-PERCHIP, set the BBT
  1878. * page mask to see if this BBT should be rewritten
  1879. */
  1880. if (bbt_masked_page != 0xffffffff &&
  1881. (chip->bbt_td->options & NAND_BBT_PERCHIP))
  1882. bbt_masked_page = chip->bbt_td->pages[chipnr] &
  1883. BBT_PAGE_MASK;
  1884. }
  1885. }
  1886. instr->state = MTD_ERASE_DONE;
  1887. erase_exit:
  1888. ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
  1889. /* Do call back function */
  1890. if (!ret)
  1891. mtd_erase_callback(instr);
  1892. /* Deselect and wake up anyone waiting on the device */
  1893. nand_release_device(mtd);
  1894. /*
  1895. * If BBT requires refresh and erase was successful, rewrite any
  1896. * selected bad block tables
  1897. */
  1898. if (bbt_masked_page == 0xffffffff || ret)
  1899. return ret;
  1900. for (chipnr = 0; chipnr < chip->numchips; chipnr++) {
  1901. if (!rewrite_bbt[chipnr])
  1902. continue;
  1903. /* update the BBT for chip */
  1904. MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_erase_nand: nand_update_bbt "
  1905. "(%d:0x%0x 0x%0x)\n", chipnr, rewrite_bbt[chipnr],
  1906. chip->bbt_td->pages[chipnr]);
  1907. nand_update_bbt(mtd, rewrite_bbt[chipnr]);
  1908. }
  1909. /* Return more or less happy */
  1910. return ret;
  1911. }
  1912. /**
  1913. * nand_sync - [MTD Interface] sync
  1914. * @mtd: MTD device structure
  1915. *
  1916. * Sync is actually a wait for chip ready function
  1917. */
  1918. static void nand_sync(struct mtd_info *mtd)
  1919. {
  1920. struct nand_chip *chip = mtd->priv;
  1921. MTDDEBUG (MTD_DEBUG_LEVEL3, "nand_sync: called\n");
  1922. /* Grab the lock and see if the device is available */
  1923. nand_get_device(chip, mtd, FL_SYNCING);
  1924. /* Release it and go back */
  1925. nand_release_device(mtd);
  1926. }
  1927. /**
  1928. * nand_block_isbad - [MTD Interface] Check if block at offset is bad
  1929. * @mtd: MTD device structure
  1930. * @offs: offset relative to mtd start
  1931. */
  1932. static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
  1933. {
  1934. /* Check for invalid offset */
  1935. if (offs > mtd->size)
  1936. return -EINVAL;
  1937. return nand_block_checkbad(mtd, offs, 1, 0);
  1938. }
  1939. /**
  1940. * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
  1941. * @mtd: MTD device structure
  1942. * @ofs: offset relative to mtd start
  1943. */
  1944. static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
  1945. {
  1946. struct nand_chip *chip = mtd->priv;
  1947. int ret;
  1948. if ((ret = nand_block_isbad(mtd, ofs))) {
  1949. /* If it was bad already, return success and do nothing. */
  1950. if (ret > 0)
  1951. return 0;
  1952. return ret;
  1953. }
  1954. return chip->block_markbad(mtd, ofs);
  1955. }
  1956. /**
  1957. * nand_suspend - [MTD Interface] Suspend the NAND flash
  1958. * @mtd: MTD device structure
  1959. */
  1960. static int nand_suspend(struct mtd_info *mtd)
  1961. {
  1962. struct nand_chip *chip = mtd->priv;
  1963. return nand_get_device(chip, mtd, FL_PM_SUSPENDED);
  1964. }
  1965. /**
  1966. * nand_resume - [MTD Interface] Resume the NAND flash
  1967. * @mtd: MTD device structure
  1968. */
  1969. static void nand_resume(struct mtd_info *mtd)
  1970. {
  1971. struct nand_chip *chip = mtd->priv;
  1972. if (chip->state == FL_PM_SUSPENDED)
  1973. nand_release_device(mtd);
  1974. else
  1975. printk(KERN_ERR "nand_resume() called for a chip which is not "
  1976. "in suspended state\n");
  1977. }
  1978. /*
  1979. * Set default functions
  1980. */
  1981. static void nand_set_defaults(struct nand_chip *chip, int busw)
  1982. {
  1983. /* check for proper chip_delay setup, set 20us if not */
  1984. if (!chip->chip_delay)
  1985. chip->chip_delay = 20;
  1986. /* check, if a user supplied command function given */
  1987. if (chip->cmdfunc == NULL)
  1988. chip->cmdfunc = nand_command;
  1989. /* check, if a user supplied wait function given */
  1990. if (chip->waitfunc == NULL)
  1991. chip->waitfunc = nand_wait;
  1992. if (!chip->select_chip)
  1993. chip->select_chip = nand_select_chip;
  1994. if (!chip->read_byte)
  1995. chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
  1996. if (!chip->read_word)
  1997. chip->read_word = nand_read_word;
  1998. if (!chip->block_bad)
  1999. chip->block_bad = nand_block_bad;
  2000. if (!chip->block_markbad)
  2001. chip->block_markbad = nand_default_block_markbad;
  2002. if (!chip->write_buf)
  2003. chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
  2004. if (!chip->read_buf)
  2005. chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
  2006. if (!chip->verify_buf)
  2007. chip->verify_buf = busw ? nand_verify_buf16 : nand_verify_buf;
  2008. if (!chip->scan_bbt)
  2009. chip->scan_bbt = nand_default_bbt;
  2010. if (!chip->controller) {
  2011. chip->controller = &chip->hwcontrol;
  2012. /* XXX U-BOOT XXX */
  2013. #if 0
  2014. spin_lock_init(&chip->controller->lock);
  2015. init_waitqueue_head(&chip->controller->wq);
  2016. #endif
  2017. }
  2018. }
  2019. /*
  2020. * Get the flash and manufacturer id and lookup if the type is supported
  2021. */
  2022. static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
  2023. struct nand_chip *chip,
  2024. int busw, int *maf_id)
  2025. {
  2026. struct nand_flash_dev *type = NULL;
  2027. int i, dev_id, maf_idx;
  2028. /* Select the device */
  2029. chip->select_chip(mtd, 0);
  2030. /* Send the command for reading device ID */
  2031. chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
  2032. /* Read manufacturer and device IDs */
  2033. *maf_id = chip->read_byte(mtd);
  2034. dev_id = chip->read_byte(mtd);
  2035. /* Lookup the flash id */
  2036. for (i = 0; nand_flash_ids[i].name != NULL; i++) {
  2037. if (dev_id == nand_flash_ids[i].id) {
  2038. type = &nand_flash_ids[i];
  2039. break;
  2040. }
  2041. }
  2042. if (!type)
  2043. return ERR_PTR(-ENODEV);
  2044. if (!mtd->name)
  2045. mtd->name = type->name;
  2046. chip->chipsize = type->chipsize << 20;
  2047. /* Newer devices have all the information in additional id bytes */
  2048. if (!type->pagesize) {
  2049. int extid;
  2050. /* The 3rd id byte holds MLC / multichip data */
  2051. chip->cellinfo = chip->read_byte(mtd);
  2052. /* The 4th id byte is the important one */
  2053. extid = chip->read_byte(mtd);
  2054. /* Calc pagesize */
  2055. mtd->writesize = 1024 << (extid & 0x3);
  2056. extid >>= 2;
  2057. /* Calc oobsize */
  2058. mtd->oobsize = (8 << (extid & 0x01)) * (mtd->writesize >> 9);
  2059. extid >>= 2;
  2060. /* Calc blocksize. Blocksize is multiples of 64KiB */
  2061. mtd->erasesize = (64 * 1024) << (extid & 0x03);
  2062. extid >>= 2;
  2063. /* Get buswidth information */
  2064. busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0;
  2065. } else {
  2066. /*
  2067. * Old devices have chip data hardcoded in the device id table
  2068. */
  2069. mtd->erasesize = type->erasesize;
  2070. mtd->writesize = type->pagesize;
  2071. mtd->oobsize = mtd->writesize / 32;
  2072. busw = type->options & NAND_BUSWIDTH_16;
  2073. }
  2074. /* Try to identify manufacturer */
  2075. for (maf_idx = 0; nand_manuf_ids[maf_idx].id != 0x0; maf_idx++) {
  2076. if (nand_manuf_ids[maf_idx].id == *maf_id)
  2077. break;
  2078. }
  2079. /*
  2080. * Check, if buswidth is correct. Hardware drivers should set
  2081. * chip correct !
  2082. */
  2083. if (busw != (chip->options & NAND_BUSWIDTH_16)) {
  2084. printk(KERN_INFO "NAND device: Manufacturer ID:"
  2085. " 0x%02x, Chip ID: 0x%02x (%s %s)\n", *maf_id,
  2086. dev_id, nand_manuf_ids[maf_idx].name, mtd->name);
  2087. printk(KERN_WARNING "NAND bus width %d instead %d bit\n",
  2088. (chip->options & NAND_BUSWIDTH_16) ? 16 : 8,
  2089. busw ? 16 : 8);
  2090. return ERR_PTR(-EINVAL);
  2091. }
  2092. /* Calculate the address shift from the page size */
  2093. chip->page_shift = ffs(mtd->writesize) - 1;
  2094. /* Convert chipsize to number of pages per chip -1. */
  2095. chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
  2096. chip->bbt_erase_shift = chip->phys_erase_shift =
  2097. ffs(mtd->erasesize) - 1;
  2098. chip->chip_shift = ffs(chip->chipsize) - 1;
  2099. /* Set the bad block position */
  2100. chip->badblockpos = mtd->writesize > 512 ?
  2101. NAND_LARGE_BADBLOCK_POS : NAND_SMALL_BADBLOCK_POS;
  2102. /* Get chip options, preserve non chip based options */
  2103. chip->options &= ~NAND_CHIPOPTIONS_MSK;
  2104. chip->options |= type->options & NAND_CHIPOPTIONS_MSK;
  2105. /*
  2106. * Set chip as a default. Board drivers can override it, if necessary
  2107. */
  2108. chip->options |= NAND_NO_AUTOINCR;
  2109. /* Check if chip is a not a samsung device. Do not clear the
  2110. * options for chips which are not having an extended id.
  2111. */
  2112. if (*maf_id != NAND_MFR_SAMSUNG && !type->pagesize)
  2113. chip->options &= ~NAND_SAMSUNG_LP_OPTIONS;
  2114. /* Check for AND chips with 4 page planes */
  2115. if (chip->options & NAND_4PAGE_ARRAY)
  2116. chip->erase_cmd = multi_erase_cmd;
  2117. else
  2118. chip->erase_cmd = single_erase_cmd;
  2119. /* Do not replace user supplied command function ! */
  2120. if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
  2121. chip->cmdfunc = nand_command_lp;
  2122. MTDDEBUG (MTD_DEBUG_LEVEL0, "NAND device: Manufacturer ID:"
  2123. " 0x%02x, Chip ID: 0x%02x (%s %s)\n", *maf_id, dev_id,
  2124. nand_manuf_ids[maf_idx].name, type->name);
  2125. return type;
  2126. }
  2127. /**
  2128. * nand_scan_ident - [NAND Interface] Scan for the NAND device
  2129. * @mtd: MTD device structure
  2130. * @maxchips: Number of chips to scan for
  2131. *
  2132. * This is the first phase of the normal nand_scan() function. It
  2133. * reads the flash ID and sets up MTD fields accordingly.
  2134. *
  2135. * The mtd->owner field must be set to the module of the caller.
  2136. */
  2137. int nand_scan_ident(struct mtd_info *mtd, int maxchips)
  2138. {
  2139. int i, busw, nand_maf_id;
  2140. struct nand_chip *chip = mtd->priv;
  2141. struct nand_flash_dev *type;
  2142. /* Get buswidth to select the correct functions */
  2143. busw = chip->options & NAND_BUSWIDTH_16;
  2144. /* Set the default functions */
  2145. nand_set_defaults(chip, busw);
  2146. /* Read the flash type */
  2147. type = nand_get_flash_type(mtd, chip, busw, &nand_maf_id);
  2148. if (IS_ERR(type)) {
  2149. printk(KERN_WARNING "No NAND device found!!!\n");
  2150. chip->select_chip(mtd, -1);
  2151. return PTR_ERR(type);
  2152. }
  2153. /* Check for a chip array */
  2154. for (i = 1; i < maxchips; i++) {
  2155. chip->select_chip(mtd, i);
  2156. /* Send the command for reading device ID */
  2157. chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
  2158. /* Read manufacturer and device IDs */
  2159. if (nand_maf_id != chip->read_byte(mtd) ||
  2160. type->id != chip->read_byte(mtd))
  2161. break;
  2162. }
  2163. if (i > 1)
  2164. printk(KERN_INFO "%d NAND chips detected\n", i);
  2165. /* Store the number of chips and calc total size for mtd */
  2166. chip->numchips = i;
  2167. mtd->size = i * chip->chipsize;
  2168. return 0;
  2169. }
  2170. /**
  2171. * nand_scan_tail - [NAND Interface] Scan for the NAND device
  2172. * @mtd: MTD device structure
  2173. * @maxchips: Number of chips to scan for
  2174. *
  2175. * This is the second phase of the normal nand_scan() function. It
  2176. * fills out all the uninitialized function pointers with the defaults
  2177. * and scans for a bad block table if appropriate.
  2178. */
  2179. int nand_scan_tail(struct mtd_info *mtd)
  2180. {
  2181. int i;
  2182. struct nand_chip *chip = mtd->priv;
  2183. if (!(chip->options & NAND_OWN_BUFFERS))
  2184. chip->buffers = kmalloc(sizeof(*chip->buffers), GFP_KERNEL);
  2185. if (!chip->buffers)
  2186. return -ENOMEM;
  2187. /* Set the internal oob buffer location, just after the page data */
  2188. chip->oob_poi = chip->buffers->databuf + mtd->writesize;
  2189. /*
  2190. * If no default placement scheme is given, select an appropriate one
  2191. */
  2192. if (!chip->ecc.layout) {
  2193. switch (mtd->oobsize) {
  2194. case 8:
  2195. chip->ecc.layout = &nand_oob_8;
  2196. break;
  2197. case 16:
  2198. chip->ecc.layout = &nand_oob_16;
  2199. break;
  2200. case 64:
  2201. chip->ecc.layout = &nand_oob_64;
  2202. break;
  2203. case 128:
  2204. chip->ecc.layout = &nand_oob_128;
  2205. break;
  2206. default:
  2207. printk(KERN_WARNING "No oob scheme defined for "
  2208. "oobsize %d\n", mtd->oobsize);
  2209. /* BUG(); */
  2210. }
  2211. }
  2212. if (!chip->write_page)
  2213. chip->write_page = nand_write_page;
  2214. /*
  2215. * check ECC mode, default to software if 3byte/512byte hardware ECC is
  2216. * selected and we have 256 byte pagesize fallback to software ECC
  2217. */
  2218. if (!chip->ecc.read_page_raw)
  2219. chip->ecc.read_page_raw = nand_read_page_raw;
  2220. if (!chip->ecc.write_page_raw)
  2221. chip->ecc.write_page_raw = nand_write_page_raw;
  2222. switch (chip->ecc.mode) {
  2223. case NAND_ECC_HW:
  2224. /* Use standard hwecc read page function ? */
  2225. if (!chip->ecc.read_page)
  2226. chip->ecc.read_page = nand_read_page_hwecc;
  2227. if (!chip->ecc.write_page)
  2228. chip->ecc.write_page = nand_write_page_hwecc;
  2229. if (!chip->ecc.read_oob)
  2230. chip->ecc.read_oob = nand_read_oob_std;
  2231. if (!chip->ecc.write_oob)
  2232. chip->ecc.write_oob = nand_write_oob_std;
  2233. case NAND_ECC_HW_SYNDROME:
  2234. if ((!chip->ecc.calculate || !chip->ecc.correct ||
  2235. !chip->ecc.hwctl) &&
  2236. (!chip->ecc.read_page ||
  2237. chip->ecc.read_page == nand_read_page_hwecc ||
  2238. !chip->ecc.write_page ||
  2239. chip->ecc.write_page == nand_write_page_hwecc)) {
  2240. printk(KERN_WARNING "No ECC functions supplied, "
  2241. "Hardware ECC not possible\n");
  2242. BUG();
  2243. }
  2244. /* Use standard syndrome read/write page function ? */
  2245. if (!chip->ecc.read_page)
  2246. chip->ecc.read_page = nand_read_page_syndrome;
  2247. if (!chip->ecc.write_page)
  2248. chip->ecc.write_page = nand_write_page_syndrome;
  2249. if (!chip->ecc.read_oob)
  2250. chip->ecc.read_oob = nand_read_oob_syndrome;
  2251. if (!chip->ecc.write_oob)
  2252. chip->ecc.write_oob = nand_write_oob_syndrome;
  2253. if (mtd->writesize >= chip->ecc.size)
  2254. break;
  2255. printk(KERN_WARNING "%d byte HW ECC not possible on "
  2256. "%d byte page size, fallback to SW ECC\n",
  2257. chip->ecc.size, mtd->writesize);
  2258. chip->ecc.mode = NAND_ECC_SOFT;
  2259. case NAND_ECC_SOFT:
  2260. chip->ecc.calculate = nand_calculate_ecc;
  2261. chip->ecc.correct = nand_correct_data;
  2262. chip->ecc.read_page = nand_read_page_swecc;
  2263. chip->ecc.write_page = nand_write_page_swecc;
  2264. chip->ecc.read_oob = nand_read_oob_std;
  2265. chip->ecc.write_oob = nand_write_oob_std;
  2266. chip->ecc.size = 256;
  2267. chip->ecc.bytes = 3;
  2268. break;
  2269. case NAND_ECC_NONE:
  2270. printk(KERN_WARNING "NAND_ECC_NONE selected by board driver. "
  2271. "This is not recommended !!\n");
  2272. chip->ecc.read_page = nand_read_page_raw;
  2273. chip->ecc.write_page = nand_write_page_raw;
  2274. chip->ecc.read_oob = nand_read_oob_std;
  2275. chip->ecc.write_oob = nand_write_oob_std;
  2276. chip->ecc.size = mtd->writesize;
  2277. chip->ecc.bytes = 0;
  2278. break;
  2279. default:
  2280. printk(KERN_WARNING "Invalid NAND_ECC_MODE %d\n",
  2281. chip->ecc.mode);
  2282. BUG();
  2283. }
  2284. /*
  2285. * The number of bytes available for a client to place data into
  2286. * the out of band area
  2287. */
  2288. chip->ecc.layout->oobavail = 0;
  2289. for (i = 0; chip->ecc.layout->oobfree[i].length; i++)
  2290. chip->ecc.layout->oobavail +=
  2291. chip->ecc.layout->oobfree[i].length;
  2292. mtd->oobavail = chip->ecc.layout->oobavail;
  2293. /*
  2294. * Set the number of read / write steps for one page depending on ECC
  2295. * mode
  2296. */
  2297. chip->ecc.steps = mtd->writesize / chip->ecc.size;
  2298. if(chip->ecc.steps * chip->ecc.size != mtd->writesize) {
  2299. printk(KERN_WARNING "Invalid ecc parameters\n");
  2300. BUG();
  2301. }
  2302. chip->ecc.total = chip->ecc.steps * chip->ecc.bytes;
  2303. /*
  2304. * Allow subpage writes up to ecc.steps. Not possible for MLC
  2305. * FLASH.
  2306. */
  2307. if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
  2308. !(chip->cellinfo & NAND_CI_CELLTYPE_MSK)) {
  2309. switch(chip->ecc.steps) {
  2310. case 2:
  2311. mtd->subpage_sft = 1;
  2312. break;
  2313. case 4:
  2314. case 8:
  2315. mtd->subpage_sft = 2;
  2316. break;
  2317. }
  2318. }
  2319. chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
  2320. /* Initialize state */
  2321. chip->state = FL_READY;
  2322. /* De-select the device */
  2323. chip->select_chip(mtd, -1);
  2324. /* Invalidate the pagebuffer reference */
  2325. chip->pagebuf = -1;
  2326. /* Fill in remaining MTD driver data */
  2327. mtd->type = MTD_NANDFLASH;
  2328. mtd->flags = MTD_CAP_NANDFLASH;
  2329. mtd->erase = nand_erase;
  2330. mtd->point = NULL;
  2331. mtd->unpoint = NULL;
  2332. mtd->read = nand_read;
  2333. mtd->write = nand_write;
  2334. mtd->read_oob = nand_read_oob;
  2335. mtd->write_oob = nand_write_oob;
  2336. mtd->sync = nand_sync;
  2337. mtd->lock = NULL;
  2338. mtd->unlock = NULL;
  2339. mtd->suspend = nand_suspend;
  2340. mtd->resume = nand_resume;
  2341. mtd->block_isbad = nand_block_isbad;
  2342. mtd->block_markbad = nand_block_markbad;
  2343. /* propagate ecc.layout to mtd_info */
  2344. mtd->ecclayout = chip->ecc.layout;
  2345. /* Check, if we should skip the bad block table scan */
  2346. if (chip->options & NAND_SKIP_BBTSCAN)
  2347. chip->options |= NAND_BBT_SCANNED;
  2348. return 0;
  2349. }
  2350. /* module_text_address() isn't exported, and it's mostly a pointless
  2351. test if this is a module _anyway_ -- they'd have to try _really_ hard
  2352. to call us from in-kernel code if the core NAND support is modular. */
  2353. #ifdef MODULE
  2354. #define caller_is_module() (1)
  2355. #else
  2356. #define caller_is_module() \
  2357. module_text_address((unsigned long)__builtin_return_address(0))
  2358. #endif
  2359. /**
  2360. * nand_scan - [NAND Interface] Scan for the NAND device
  2361. * @mtd: MTD device structure
  2362. * @maxchips: Number of chips to scan for
  2363. *
  2364. * This fills out all the uninitialized function pointers
  2365. * with the defaults.
  2366. * The flash ID is read and the mtd/chip structures are
  2367. * filled with the appropriate values.
  2368. * The mtd->owner field must be set to the module of the caller
  2369. *
  2370. */
  2371. int nand_scan(struct mtd_info *mtd, int maxchips)
  2372. {
  2373. int ret;
  2374. /* Many callers got this wrong, so check for it for a while... */
  2375. /* XXX U-BOOT XXX */
  2376. #if 0
  2377. if (!mtd->owner && caller_is_module()) {
  2378. printk(KERN_CRIT "nand_scan() called with NULL mtd->owner!\n");
  2379. BUG();
  2380. }
  2381. #endif
  2382. ret = nand_scan_ident(mtd, maxchips);
  2383. if (!ret)
  2384. ret = nand_scan_tail(mtd);
  2385. return ret;
  2386. }
  2387. /**
  2388. * nand_release - [NAND Interface] Free resources held by the NAND device
  2389. * @mtd: MTD device structure
  2390. */
  2391. void nand_release(struct mtd_info *mtd)
  2392. {
  2393. struct nand_chip *chip = mtd->priv;
  2394. #ifdef CONFIG_MTD_PARTITIONS
  2395. /* Deregister partitions */
  2396. del_mtd_partitions(mtd);
  2397. #endif
  2398. /* Deregister the device */
  2399. /* XXX U-BOOT XXX */
  2400. #if 0
  2401. del_mtd_device(mtd);
  2402. #endif
  2403. /* Free bad block table memory */
  2404. kfree(chip->bbt);
  2405. if (!(chip->options & NAND_OWN_BUFFERS))
  2406. kfree(chip->buffers);
  2407. }
  2408. /* XXX U-BOOT XXX */
  2409. #if 0
  2410. EXPORT_SYMBOL_GPL(nand_scan);
  2411. EXPORT_SYMBOL_GPL(nand_scan_ident);
  2412. EXPORT_SYMBOL_GPL(nand_scan_tail);
  2413. EXPORT_SYMBOL_GPL(nand_release);
  2414. static int __init nand_base_init(void)
  2415. {
  2416. led_trigger_register_simple("nand-disk", &nand_led_trigger);
  2417. return 0;
  2418. }
  2419. static void __exit nand_base_exit(void)
  2420. {
  2421. led_trigger_unregister_simple(nand_led_trigger);
  2422. }
  2423. module_init(nand_base_init);
  2424. module_exit(nand_base_exit);
  2425. MODULE_LICENSE("GPL");
  2426. MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>, Thomas Gleixner <tglx@linutronix.de>");
  2427. MODULE_DESCRIPTION("Generic NAND flash driver code");
  2428. #endif