nand_base.c 101 KB

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