nand_base.c 83 KB

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