nand_base.c 89 KB

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