nand_base.c 128 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940
  1. /*
  2. * Overview:
  3. * This is the generic MTD driver for NAND flash devices. It should be
  4. * capable of working with almost all NAND chips currently available.
  5. *
  6. * Additional technical information is available on
  7. * http://www.linux-mtd.infradead.org/doc/nand.html
  8. *
  9. * Copyright (C) 2000 Steven J. Hill (sjhill@realitydiluted.com)
  10. * 2002-2006 Thomas Gleixner (tglx@linutronix.de)
  11. *
  12. * Credits:
  13. * David Woodhouse for adding multichip support
  14. *
  15. * Aleph One Ltd. and Toby Churchill Ltd. for supporting the
  16. * rework for 2K page size chips
  17. *
  18. * TODO:
  19. * Enable cached programming for 2k page size chips
  20. * Check, if mtd->ecctype should be set to MTD_ECC_HW
  21. * if we have HW ECC support.
  22. * BBT table is not serialized, has to be fixed
  23. *
  24. * This program is free software; you can redistribute it and/or modify
  25. * it under the terms of the GNU General Public License version 2 as
  26. * published by the Free Software Foundation.
  27. *
  28. */
  29. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  30. #include <linux/module.h>
  31. #include <linux/delay.h>
  32. #include <linux/errno.h>
  33. #include <linux/err.h>
  34. #include <linux/sched.h>
  35. #include <linux/slab.h>
  36. #include <linux/mm.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/io.h>
  45. #include <linux/mtd/partitions.h>
  46. #include <linux/of.h>
  47. static int nand_get_device(struct mtd_info *mtd, int new_state);
  48. static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
  49. struct mtd_oob_ops *ops);
  50. /* Define default oob placement schemes for large and small page devices */
  51. static int nand_ooblayout_ecc_sp(struct mtd_info *mtd, int section,
  52. struct mtd_oob_region *oobregion)
  53. {
  54. struct nand_chip *chip = mtd_to_nand(mtd);
  55. struct nand_ecc_ctrl *ecc = &chip->ecc;
  56. if (section > 1)
  57. return -ERANGE;
  58. if (!section) {
  59. oobregion->offset = 0;
  60. if (mtd->oobsize == 16)
  61. oobregion->length = 4;
  62. else
  63. oobregion->length = 3;
  64. } else {
  65. if (mtd->oobsize == 8)
  66. return -ERANGE;
  67. oobregion->offset = 6;
  68. oobregion->length = ecc->total - 4;
  69. }
  70. return 0;
  71. }
  72. static int nand_ooblayout_free_sp(struct mtd_info *mtd, int section,
  73. struct mtd_oob_region *oobregion)
  74. {
  75. if (section > 1)
  76. return -ERANGE;
  77. if (mtd->oobsize == 16) {
  78. if (section)
  79. return -ERANGE;
  80. oobregion->length = 8;
  81. oobregion->offset = 8;
  82. } else {
  83. oobregion->length = 2;
  84. if (!section)
  85. oobregion->offset = 3;
  86. else
  87. oobregion->offset = 6;
  88. }
  89. return 0;
  90. }
  91. const struct mtd_ooblayout_ops nand_ooblayout_sp_ops = {
  92. .ecc = nand_ooblayout_ecc_sp,
  93. .free = nand_ooblayout_free_sp,
  94. };
  95. EXPORT_SYMBOL_GPL(nand_ooblayout_sp_ops);
  96. static int nand_ooblayout_ecc_lp(struct mtd_info *mtd, int section,
  97. struct mtd_oob_region *oobregion)
  98. {
  99. struct nand_chip *chip = mtd_to_nand(mtd);
  100. struct nand_ecc_ctrl *ecc = &chip->ecc;
  101. if (section)
  102. return -ERANGE;
  103. oobregion->length = ecc->total;
  104. oobregion->offset = mtd->oobsize - oobregion->length;
  105. return 0;
  106. }
  107. static int nand_ooblayout_free_lp(struct mtd_info *mtd, int section,
  108. struct mtd_oob_region *oobregion)
  109. {
  110. struct nand_chip *chip = mtd_to_nand(mtd);
  111. struct nand_ecc_ctrl *ecc = &chip->ecc;
  112. if (section)
  113. return -ERANGE;
  114. oobregion->length = mtd->oobsize - ecc->total - 2;
  115. oobregion->offset = 2;
  116. return 0;
  117. }
  118. const struct mtd_ooblayout_ops nand_ooblayout_lp_ops = {
  119. .ecc = nand_ooblayout_ecc_lp,
  120. .free = nand_ooblayout_free_lp,
  121. };
  122. EXPORT_SYMBOL_GPL(nand_ooblayout_lp_ops);
  123. /*
  124. * Support the old "large page" layout used for 1-bit Hamming ECC where ECC
  125. * are placed at a fixed offset.
  126. */
  127. static int nand_ooblayout_ecc_lp_hamming(struct mtd_info *mtd, int section,
  128. struct mtd_oob_region *oobregion)
  129. {
  130. struct nand_chip *chip = mtd_to_nand(mtd);
  131. struct nand_ecc_ctrl *ecc = &chip->ecc;
  132. if (section)
  133. return -ERANGE;
  134. switch (mtd->oobsize) {
  135. case 64:
  136. oobregion->offset = 40;
  137. break;
  138. case 128:
  139. oobregion->offset = 80;
  140. break;
  141. default:
  142. return -EINVAL;
  143. }
  144. oobregion->length = ecc->total;
  145. if (oobregion->offset + oobregion->length > mtd->oobsize)
  146. return -ERANGE;
  147. return 0;
  148. }
  149. static int nand_ooblayout_free_lp_hamming(struct mtd_info *mtd, int section,
  150. struct mtd_oob_region *oobregion)
  151. {
  152. struct nand_chip *chip = mtd_to_nand(mtd);
  153. struct nand_ecc_ctrl *ecc = &chip->ecc;
  154. int ecc_offset = 0;
  155. if (section < 0 || section > 1)
  156. return -ERANGE;
  157. switch (mtd->oobsize) {
  158. case 64:
  159. ecc_offset = 40;
  160. break;
  161. case 128:
  162. ecc_offset = 80;
  163. break;
  164. default:
  165. return -EINVAL;
  166. }
  167. if (section == 0) {
  168. oobregion->offset = 2;
  169. oobregion->length = ecc_offset - 2;
  170. } else {
  171. oobregion->offset = ecc_offset + ecc->total;
  172. oobregion->length = mtd->oobsize - oobregion->offset;
  173. }
  174. return 0;
  175. }
  176. const struct mtd_ooblayout_ops nand_ooblayout_lp_hamming_ops = {
  177. .ecc = nand_ooblayout_ecc_lp_hamming,
  178. .free = nand_ooblayout_free_lp_hamming,
  179. };
  180. static int check_offs_len(struct mtd_info *mtd,
  181. loff_t ofs, uint64_t len)
  182. {
  183. struct nand_chip *chip = mtd_to_nand(mtd);
  184. int ret = 0;
  185. /* Start address must align on block boundary */
  186. if (ofs & ((1ULL << chip->phys_erase_shift) - 1)) {
  187. pr_debug("%s: unaligned address\n", __func__);
  188. ret = -EINVAL;
  189. }
  190. /* Length must align on block boundary */
  191. if (len & ((1ULL << chip->phys_erase_shift) - 1)) {
  192. pr_debug("%s: length not block aligned\n", __func__);
  193. ret = -EINVAL;
  194. }
  195. return ret;
  196. }
  197. /**
  198. * nand_release_device - [GENERIC] release chip
  199. * @mtd: MTD device structure
  200. *
  201. * Release chip lock and wake up anyone waiting on the device.
  202. */
  203. static void nand_release_device(struct mtd_info *mtd)
  204. {
  205. struct nand_chip *chip = mtd_to_nand(mtd);
  206. /* Release the controller and the chip */
  207. spin_lock(&chip->controller->lock);
  208. chip->controller->active = NULL;
  209. chip->state = FL_READY;
  210. wake_up(&chip->controller->wq);
  211. spin_unlock(&chip->controller->lock);
  212. }
  213. /**
  214. * nand_read_byte - [DEFAULT] read one byte from the chip
  215. * @mtd: MTD device structure
  216. *
  217. * Default read function for 8bit buswidth
  218. */
  219. static uint8_t nand_read_byte(struct mtd_info *mtd)
  220. {
  221. struct nand_chip *chip = mtd_to_nand(mtd);
  222. return readb(chip->IO_ADDR_R);
  223. }
  224. /**
  225. * nand_read_byte16 - [DEFAULT] read one byte endianness aware from the chip
  226. * @mtd: MTD device structure
  227. *
  228. * Default read function for 16bit buswidth with endianness conversion.
  229. *
  230. */
  231. static uint8_t nand_read_byte16(struct mtd_info *mtd)
  232. {
  233. struct nand_chip *chip = mtd_to_nand(mtd);
  234. return (uint8_t) cpu_to_le16(readw(chip->IO_ADDR_R));
  235. }
  236. /**
  237. * nand_read_word - [DEFAULT] read one word from the chip
  238. * @mtd: MTD device structure
  239. *
  240. * Default read function for 16bit buswidth without endianness conversion.
  241. */
  242. static u16 nand_read_word(struct mtd_info *mtd)
  243. {
  244. struct nand_chip *chip = mtd_to_nand(mtd);
  245. return readw(chip->IO_ADDR_R);
  246. }
  247. /**
  248. * nand_select_chip - [DEFAULT] control CE line
  249. * @mtd: MTD device structure
  250. * @chipnr: chipnumber to select, -1 for deselect
  251. *
  252. * Default select function for 1 chip devices.
  253. */
  254. static void nand_select_chip(struct mtd_info *mtd, int chipnr)
  255. {
  256. struct nand_chip *chip = mtd_to_nand(mtd);
  257. switch (chipnr) {
  258. case -1:
  259. chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
  260. break;
  261. case 0:
  262. break;
  263. default:
  264. BUG();
  265. }
  266. }
  267. /**
  268. * nand_write_byte - [DEFAULT] write single byte to chip
  269. * @mtd: MTD device structure
  270. * @byte: value to write
  271. *
  272. * Default function to write a byte to I/O[7:0]
  273. */
  274. static void nand_write_byte(struct mtd_info *mtd, uint8_t byte)
  275. {
  276. struct nand_chip *chip = mtd_to_nand(mtd);
  277. chip->write_buf(mtd, &byte, 1);
  278. }
  279. /**
  280. * nand_write_byte16 - [DEFAULT] write single byte to a chip with width 16
  281. * @mtd: MTD device structure
  282. * @byte: value to write
  283. *
  284. * Default function to write a byte to I/O[7:0] on a 16-bit wide chip.
  285. */
  286. static void nand_write_byte16(struct mtd_info *mtd, uint8_t byte)
  287. {
  288. struct nand_chip *chip = mtd_to_nand(mtd);
  289. uint16_t word = byte;
  290. /*
  291. * It's not entirely clear what should happen to I/O[15:8] when writing
  292. * a byte. The ONFi spec (Revision 3.1; 2012-09-19, Section 2.16) reads:
  293. *
  294. * When the host supports a 16-bit bus width, only data is
  295. * transferred at the 16-bit width. All address and command line
  296. * transfers shall use only the lower 8-bits of the data bus. During
  297. * command transfers, the host may place any value on the upper
  298. * 8-bits of the data bus. During address transfers, the host shall
  299. * set the upper 8-bits of the data bus to 00h.
  300. *
  301. * One user of the write_byte callback is nand_onfi_set_features. The
  302. * four parameters are specified to be written to I/O[7:0], but this is
  303. * neither an address nor a command transfer. Let's assume a 0 on the
  304. * upper I/O lines is OK.
  305. */
  306. chip->write_buf(mtd, (uint8_t *)&word, 2);
  307. }
  308. /**
  309. * nand_write_buf - [DEFAULT] write buffer to chip
  310. * @mtd: MTD device structure
  311. * @buf: data buffer
  312. * @len: number of bytes to write
  313. *
  314. * Default write function for 8bit buswidth.
  315. */
  316. static void nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
  317. {
  318. struct nand_chip *chip = mtd_to_nand(mtd);
  319. iowrite8_rep(chip->IO_ADDR_W, buf, len);
  320. }
  321. /**
  322. * nand_read_buf - [DEFAULT] read chip data into buffer
  323. * @mtd: MTD device structure
  324. * @buf: buffer to store date
  325. * @len: number of bytes to read
  326. *
  327. * Default read function for 8bit buswidth.
  328. */
  329. static void nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
  330. {
  331. struct nand_chip *chip = mtd_to_nand(mtd);
  332. ioread8_rep(chip->IO_ADDR_R, buf, len);
  333. }
  334. /**
  335. * nand_write_buf16 - [DEFAULT] write buffer to chip
  336. * @mtd: MTD device structure
  337. * @buf: data buffer
  338. * @len: number of bytes to write
  339. *
  340. * Default write function for 16bit buswidth.
  341. */
  342. static void nand_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
  343. {
  344. struct nand_chip *chip = mtd_to_nand(mtd);
  345. u16 *p = (u16 *) buf;
  346. iowrite16_rep(chip->IO_ADDR_W, p, len >> 1);
  347. }
  348. /**
  349. * nand_read_buf16 - [DEFAULT] read chip data into buffer
  350. * @mtd: MTD device structure
  351. * @buf: buffer to store date
  352. * @len: number of bytes to read
  353. *
  354. * Default read function for 16bit buswidth.
  355. */
  356. static void nand_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len)
  357. {
  358. struct nand_chip *chip = mtd_to_nand(mtd);
  359. u16 *p = (u16 *) buf;
  360. ioread16_rep(chip->IO_ADDR_R, p, len >> 1);
  361. }
  362. /**
  363. * nand_block_bad - [DEFAULT] Read bad block marker from the chip
  364. * @mtd: MTD device structure
  365. * @ofs: offset from device start
  366. *
  367. * Check, if the block is bad.
  368. */
  369. static int nand_block_bad(struct mtd_info *mtd, loff_t ofs)
  370. {
  371. int page, res = 0, i = 0;
  372. struct nand_chip *chip = mtd_to_nand(mtd);
  373. u16 bad;
  374. if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
  375. ofs += mtd->erasesize - mtd->writesize;
  376. page = (int)(ofs >> chip->page_shift) & chip->pagemask;
  377. do {
  378. if (chip->options & NAND_BUSWIDTH_16) {
  379. chip->cmdfunc(mtd, NAND_CMD_READOOB,
  380. chip->badblockpos & 0xFE, page);
  381. bad = cpu_to_le16(chip->read_word(mtd));
  382. if (chip->badblockpos & 0x1)
  383. bad >>= 8;
  384. else
  385. bad &= 0xFF;
  386. } else {
  387. chip->cmdfunc(mtd, NAND_CMD_READOOB, chip->badblockpos,
  388. page);
  389. bad = chip->read_byte(mtd);
  390. }
  391. if (likely(chip->badblockbits == 8))
  392. res = bad != 0xFF;
  393. else
  394. res = hweight8(bad) < chip->badblockbits;
  395. ofs += mtd->writesize;
  396. page = (int)(ofs >> chip->page_shift) & chip->pagemask;
  397. i++;
  398. } while (!res && i < 2 && (chip->bbt_options & NAND_BBT_SCAN2NDPAGE));
  399. return res;
  400. }
  401. /**
  402. * nand_default_block_markbad - [DEFAULT] mark a block bad via bad block marker
  403. * @mtd: MTD device structure
  404. * @ofs: offset from device start
  405. *
  406. * This is the default implementation, which can be overridden by a hardware
  407. * specific driver. It provides the details for writing a bad block marker to a
  408. * block.
  409. */
  410. static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
  411. {
  412. struct nand_chip *chip = mtd_to_nand(mtd);
  413. struct mtd_oob_ops ops;
  414. uint8_t buf[2] = { 0, 0 };
  415. int ret = 0, res, i = 0;
  416. memset(&ops, 0, sizeof(ops));
  417. ops.oobbuf = buf;
  418. ops.ooboffs = chip->badblockpos;
  419. if (chip->options & NAND_BUSWIDTH_16) {
  420. ops.ooboffs &= ~0x01;
  421. ops.len = ops.ooblen = 2;
  422. } else {
  423. ops.len = ops.ooblen = 1;
  424. }
  425. ops.mode = MTD_OPS_PLACE_OOB;
  426. /* Write to first/last page(s) if necessary */
  427. if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
  428. ofs += mtd->erasesize - mtd->writesize;
  429. do {
  430. res = nand_do_write_oob(mtd, ofs, &ops);
  431. if (!ret)
  432. ret = res;
  433. i++;
  434. ofs += mtd->writesize;
  435. } while ((chip->bbt_options & NAND_BBT_SCAN2NDPAGE) && i < 2);
  436. return ret;
  437. }
  438. /**
  439. * nand_block_markbad_lowlevel - mark a block bad
  440. * @mtd: MTD device structure
  441. * @ofs: offset from device start
  442. *
  443. * This function performs the generic NAND bad block marking steps (i.e., bad
  444. * block table(s) and/or marker(s)). We only allow the hardware driver to
  445. * specify how to write bad block markers to OOB (chip->block_markbad).
  446. *
  447. * We try operations in the following order:
  448. * (1) erase the affected block, to allow OOB marker to be written cleanly
  449. * (2) write bad block marker to OOB area of affected block (unless flag
  450. * NAND_BBT_NO_OOB_BBM is present)
  451. * (3) update the BBT
  452. * Note that we retain the first error encountered in (2) or (3), finish the
  453. * procedures, and dump the error in the end.
  454. */
  455. static int nand_block_markbad_lowlevel(struct mtd_info *mtd, loff_t ofs)
  456. {
  457. struct nand_chip *chip = mtd_to_nand(mtd);
  458. int res, ret = 0;
  459. if (!(chip->bbt_options & NAND_BBT_NO_OOB_BBM)) {
  460. struct erase_info einfo;
  461. /* Attempt erase before marking OOB */
  462. memset(&einfo, 0, sizeof(einfo));
  463. einfo.mtd = mtd;
  464. einfo.addr = ofs;
  465. einfo.len = 1ULL << chip->phys_erase_shift;
  466. nand_erase_nand(mtd, &einfo, 0);
  467. /* Write bad block marker to OOB */
  468. nand_get_device(mtd, FL_WRITING);
  469. ret = chip->block_markbad(mtd, ofs);
  470. nand_release_device(mtd);
  471. }
  472. /* Mark block bad in BBT */
  473. if (chip->bbt) {
  474. res = nand_markbad_bbt(mtd, ofs);
  475. if (!ret)
  476. ret = res;
  477. }
  478. if (!ret)
  479. mtd->ecc_stats.badblocks++;
  480. return ret;
  481. }
  482. /**
  483. * nand_check_wp - [GENERIC] check if the chip is write protected
  484. * @mtd: MTD device structure
  485. *
  486. * Check, if the device is write protected. The function expects, that the
  487. * device is already selected.
  488. */
  489. static int nand_check_wp(struct mtd_info *mtd)
  490. {
  491. struct nand_chip *chip = mtd_to_nand(mtd);
  492. /* Broken xD cards report WP despite being writable */
  493. if (chip->options & NAND_BROKEN_XD)
  494. return 0;
  495. /* Check the WP bit */
  496. chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
  497. return (chip->read_byte(mtd) & NAND_STATUS_WP) ? 0 : 1;
  498. }
  499. /**
  500. * nand_block_isreserved - [GENERIC] Check if a block is marked reserved.
  501. * @mtd: MTD device structure
  502. * @ofs: offset from device start
  503. *
  504. * Check if the block is marked as reserved.
  505. */
  506. static int nand_block_isreserved(struct mtd_info *mtd, loff_t ofs)
  507. {
  508. struct nand_chip *chip = mtd_to_nand(mtd);
  509. if (!chip->bbt)
  510. return 0;
  511. /* Return info from the table */
  512. return nand_isreserved_bbt(mtd, ofs);
  513. }
  514. /**
  515. * nand_block_checkbad - [GENERIC] Check if a block is marked bad
  516. * @mtd: MTD device structure
  517. * @ofs: offset from device start
  518. * @allowbbt: 1, if its allowed to access the bbt area
  519. *
  520. * Check, if the block is bad. Either by reading the bad block table or
  521. * calling of the scan function.
  522. */
  523. static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int allowbbt)
  524. {
  525. struct nand_chip *chip = mtd_to_nand(mtd);
  526. if (!chip->bbt)
  527. return chip->block_bad(mtd, ofs);
  528. /* Return info from the table */
  529. return nand_isbad_bbt(mtd, ofs, allowbbt);
  530. }
  531. /**
  532. * panic_nand_wait_ready - [GENERIC] Wait for the ready pin after commands.
  533. * @mtd: MTD device structure
  534. * @timeo: Timeout
  535. *
  536. * Helper function for nand_wait_ready used when needing to wait in interrupt
  537. * context.
  538. */
  539. static void panic_nand_wait_ready(struct mtd_info *mtd, unsigned long timeo)
  540. {
  541. struct nand_chip *chip = mtd_to_nand(mtd);
  542. int i;
  543. /* Wait for the device to get ready */
  544. for (i = 0; i < timeo; i++) {
  545. if (chip->dev_ready(mtd))
  546. break;
  547. touch_softlockup_watchdog();
  548. mdelay(1);
  549. }
  550. }
  551. /**
  552. * nand_wait_ready - [GENERIC] Wait for the ready pin after commands.
  553. * @mtd: MTD device structure
  554. *
  555. * Wait for the ready pin after a command, and warn if a timeout occurs.
  556. */
  557. void nand_wait_ready(struct mtd_info *mtd)
  558. {
  559. struct nand_chip *chip = mtd_to_nand(mtd);
  560. unsigned long timeo = 400;
  561. if (in_interrupt() || oops_in_progress)
  562. return panic_nand_wait_ready(mtd, timeo);
  563. /* Wait until command is processed or timeout occurs */
  564. timeo = jiffies + msecs_to_jiffies(timeo);
  565. do {
  566. if (chip->dev_ready(mtd))
  567. return;
  568. cond_resched();
  569. } while (time_before(jiffies, timeo));
  570. if (!chip->dev_ready(mtd))
  571. pr_warn_ratelimited("timeout while waiting for chip to become ready\n");
  572. }
  573. EXPORT_SYMBOL_GPL(nand_wait_ready);
  574. /**
  575. * nand_wait_status_ready - [GENERIC] Wait for the ready status after commands.
  576. * @mtd: MTD device structure
  577. * @timeo: Timeout in ms
  578. *
  579. * Wait for status ready (i.e. command done) or timeout.
  580. */
  581. static void nand_wait_status_ready(struct mtd_info *mtd, unsigned long timeo)
  582. {
  583. register struct nand_chip *chip = mtd_to_nand(mtd);
  584. timeo = jiffies + msecs_to_jiffies(timeo);
  585. do {
  586. if ((chip->read_byte(mtd) & NAND_STATUS_READY))
  587. break;
  588. touch_softlockup_watchdog();
  589. } while (time_before(jiffies, timeo));
  590. };
  591. /**
  592. * nand_command - [DEFAULT] Send command to NAND device
  593. * @mtd: MTD device structure
  594. * @command: the command to be sent
  595. * @column: the column address for this command, -1 if none
  596. * @page_addr: the page address for this command, -1 if none
  597. *
  598. * Send command to NAND device. This function is used for small page devices
  599. * (512 Bytes per page).
  600. */
  601. static void nand_command(struct mtd_info *mtd, unsigned int command,
  602. int column, int page_addr)
  603. {
  604. register struct nand_chip *chip = mtd_to_nand(mtd);
  605. int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE;
  606. /* Write out the command to the device */
  607. if (command == NAND_CMD_SEQIN) {
  608. int readcmd;
  609. if (column >= mtd->writesize) {
  610. /* OOB area */
  611. column -= mtd->writesize;
  612. readcmd = NAND_CMD_READOOB;
  613. } else if (column < 256) {
  614. /* First 256 bytes --> READ0 */
  615. readcmd = NAND_CMD_READ0;
  616. } else {
  617. column -= 256;
  618. readcmd = NAND_CMD_READ1;
  619. }
  620. chip->cmd_ctrl(mtd, readcmd, ctrl);
  621. ctrl &= ~NAND_CTRL_CHANGE;
  622. }
  623. chip->cmd_ctrl(mtd, command, ctrl);
  624. /* Address cycle, when necessary */
  625. ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE;
  626. /* Serially input address */
  627. if (column != -1) {
  628. /* Adjust columns for 16 bit buswidth */
  629. if (chip->options & NAND_BUSWIDTH_16 &&
  630. !nand_opcode_8bits(command))
  631. column >>= 1;
  632. chip->cmd_ctrl(mtd, column, ctrl);
  633. ctrl &= ~NAND_CTRL_CHANGE;
  634. }
  635. if (page_addr != -1) {
  636. chip->cmd_ctrl(mtd, page_addr, ctrl);
  637. ctrl &= ~NAND_CTRL_CHANGE;
  638. chip->cmd_ctrl(mtd, page_addr >> 8, ctrl);
  639. /* One more address cycle for devices > 32MiB */
  640. if (chip->chipsize > (32 << 20))
  641. chip->cmd_ctrl(mtd, page_addr >> 16, ctrl);
  642. }
  643. chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
  644. /*
  645. * Program and erase have their own busy handlers status and sequential
  646. * in needs no delay
  647. */
  648. switch (command) {
  649. case NAND_CMD_PAGEPROG:
  650. case NAND_CMD_ERASE1:
  651. case NAND_CMD_ERASE2:
  652. case NAND_CMD_SEQIN:
  653. case NAND_CMD_STATUS:
  654. return;
  655. case NAND_CMD_RESET:
  656. if (chip->dev_ready)
  657. break;
  658. udelay(chip->chip_delay);
  659. chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
  660. NAND_CTRL_CLE | NAND_CTRL_CHANGE);
  661. chip->cmd_ctrl(mtd,
  662. NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
  663. /* EZ-NAND can take upto 250ms as per ONFi v4.0 */
  664. nand_wait_status_ready(mtd, 250);
  665. return;
  666. /* This applies to read commands */
  667. default:
  668. /*
  669. * If we don't have access to the busy pin, we apply the given
  670. * command delay
  671. */
  672. if (!chip->dev_ready) {
  673. udelay(chip->chip_delay);
  674. return;
  675. }
  676. }
  677. /*
  678. * Apply this short delay always to ensure that we do wait tWB in
  679. * any case on any machine.
  680. */
  681. ndelay(100);
  682. nand_wait_ready(mtd);
  683. }
  684. /**
  685. * nand_command_lp - [DEFAULT] Send command to NAND large page device
  686. * @mtd: MTD device structure
  687. * @command: the command to be sent
  688. * @column: the column address for this command, -1 if none
  689. * @page_addr: the page address for this command, -1 if none
  690. *
  691. * Send command to NAND device. This is the version for the new large page
  692. * devices. We don't have the separate regions as we have in the small page
  693. * devices. We must emulate NAND_CMD_READOOB to keep the code compatible.
  694. */
  695. static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
  696. int column, int page_addr)
  697. {
  698. register struct nand_chip *chip = mtd_to_nand(mtd);
  699. /* Emulate NAND_CMD_READOOB */
  700. if (command == NAND_CMD_READOOB) {
  701. column += mtd->writesize;
  702. command = NAND_CMD_READ0;
  703. }
  704. /* Command latch cycle */
  705. chip->cmd_ctrl(mtd, command, NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
  706. if (column != -1 || page_addr != -1) {
  707. int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE;
  708. /* Serially input address */
  709. if (column != -1) {
  710. /* Adjust columns for 16 bit buswidth */
  711. if (chip->options & NAND_BUSWIDTH_16 &&
  712. !nand_opcode_8bits(command))
  713. column >>= 1;
  714. chip->cmd_ctrl(mtd, column, ctrl);
  715. ctrl &= ~NAND_CTRL_CHANGE;
  716. /* Only output a single addr cycle for 8bits opcodes. */
  717. if (!nand_opcode_8bits(command))
  718. chip->cmd_ctrl(mtd, column >> 8, ctrl);
  719. }
  720. if (page_addr != -1) {
  721. chip->cmd_ctrl(mtd, page_addr, ctrl);
  722. chip->cmd_ctrl(mtd, page_addr >> 8,
  723. NAND_NCE | NAND_ALE);
  724. /* One more address cycle for devices > 128MiB */
  725. if (chip->chipsize > (128 << 20))
  726. chip->cmd_ctrl(mtd, page_addr >> 16,
  727. NAND_NCE | NAND_ALE);
  728. }
  729. }
  730. chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
  731. /*
  732. * Program and erase have their own busy handlers status, sequential
  733. * in and status need no delay.
  734. */
  735. switch (command) {
  736. case NAND_CMD_CACHEDPROG:
  737. case NAND_CMD_PAGEPROG:
  738. case NAND_CMD_ERASE1:
  739. case NAND_CMD_ERASE2:
  740. case NAND_CMD_SEQIN:
  741. case NAND_CMD_RNDIN:
  742. case NAND_CMD_STATUS:
  743. return;
  744. case NAND_CMD_RESET:
  745. if (chip->dev_ready)
  746. break;
  747. udelay(chip->chip_delay);
  748. chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
  749. NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
  750. chip->cmd_ctrl(mtd, NAND_CMD_NONE,
  751. NAND_NCE | NAND_CTRL_CHANGE);
  752. /* EZ-NAND can take upto 250ms as per ONFi v4.0 */
  753. nand_wait_status_ready(mtd, 250);
  754. return;
  755. case NAND_CMD_RNDOUT:
  756. /* No ready / busy check necessary */
  757. chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART,
  758. NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
  759. chip->cmd_ctrl(mtd, NAND_CMD_NONE,
  760. NAND_NCE | NAND_CTRL_CHANGE);
  761. return;
  762. case NAND_CMD_READ0:
  763. chip->cmd_ctrl(mtd, NAND_CMD_READSTART,
  764. NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
  765. chip->cmd_ctrl(mtd, NAND_CMD_NONE,
  766. NAND_NCE | NAND_CTRL_CHANGE);
  767. /* This applies to read commands */
  768. default:
  769. /*
  770. * If we don't have access to the busy pin, we apply the given
  771. * command delay.
  772. */
  773. if (!chip->dev_ready) {
  774. udelay(chip->chip_delay);
  775. return;
  776. }
  777. }
  778. /*
  779. * Apply this short delay always to ensure that we do wait tWB in
  780. * any case on any machine.
  781. */
  782. ndelay(100);
  783. nand_wait_ready(mtd);
  784. }
  785. /**
  786. * panic_nand_get_device - [GENERIC] Get chip for selected access
  787. * @chip: the nand chip descriptor
  788. * @mtd: MTD device structure
  789. * @new_state: the state which is requested
  790. *
  791. * Used when in panic, no locks are taken.
  792. */
  793. static void panic_nand_get_device(struct nand_chip *chip,
  794. struct mtd_info *mtd, int new_state)
  795. {
  796. /* Hardware controller shared among independent devices */
  797. chip->controller->active = chip;
  798. chip->state = new_state;
  799. }
  800. /**
  801. * nand_get_device - [GENERIC] Get chip for selected access
  802. * @mtd: MTD device structure
  803. * @new_state: the state which is requested
  804. *
  805. * Get the device and lock it for exclusive access
  806. */
  807. static int
  808. nand_get_device(struct mtd_info *mtd, int new_state)
  809. {
  810. struct nand_chip *chip = mtd_to_nand(mtd);
  811. spinlock_t *lock = &chip->controller->lock;
  812. wait_queue_head_t *wq = &chip->controller->wq;
  813. DECLARE_WAITQUEUE(wait, current);
  814. retry:
  815. spin_lock(lock);
  816. /* Hardware controller shared among independent devices */
  817. if (!chip->controller->active)
  818. chip->controller->active = chip;
  819. if (chip->controller->active == chip && chip->state == FL_READY) {
  820. chip->state = new_state;
  821. spin_unlock(lock);
  822. return 0;
  823. }
  824. if (new_state == FL_PM_SUSPENDED) {
  825. if (chip->controller->active->state == FL_PM_SUSPENDED) {
  826. chip->state = FL_PM_SUSPENDED;
  827. spin_unlock(lock);
  828. return 0;
  829. }
  830. }
  831. set_current_state(TASK_UNINTERRUPTIBLE);
  832. add_wait_queue(wq, &wait);
  833. spin_unlock(lock);
  834. schedule();
  835. remove_wait_queue(wq, &wait);
  836. goto retry;
  837. }
  838. /**
  839. * panic_nand_wait - [GENERIC] wait until the command is done
  840. * @mtd: MTD device structure
  841. * @chip: NAND chip structure
  842. * @timeo: timeout
  843. *
  844. * Wait for command done. This is a helper function for nand_wait used when
  845. * we are in interrupt context. May happen when in panic and trying to write
  846. * an oops through mtdoops.
  847. */
  848. static void panic_nand_wait(struct mtd_info *mtd, struct nand_chip *chip,
  849. unsigned long timeo)
  850. {
  851. int i;
  852. for (i = 0; i < timeo; i++) {
  853. if (chip->dev_ready) {
  854. if (chip->dev_ready(mtd))
  855. break;
  856. } else {
  857. if (chip->read_byte(mtd) & NAND_STATUS_READY)
  858. break;
  859. }
  860. mdelay(1);
  861. }
  862. }
  863. /**
  864. * nand_wait - [DEFAULT] wait until the command is done
  865. * @mtd: MTD device structure
  866. * @chip: NAND chip structure
  867. *
  868. * Wait for command done. This applies to erase and program only.
  869. */
  870. static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip)
  871. {
  872. int status;
  873. unsigned long timeo = 400;
  874. /*
  875. * Apply this short delay always to ensure that we do wait tWB in any
  876. * case on any machine.
  877. */
  878. ndelay(100);
  879. chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
  880. if (in_interrupt() || oops_in_progress)
  881. panic_nand_wait(mtd, chip, timeo);
  882. else {
  883. timeo = jiffies + msecs_to_jiffies(timeo);
  884. do {
  885. if (chip->dev_ready) {
  886. if (chip->dev_ready(mtd))
  887. break;
  888. } else {
  889. if (chip->read_byte(mtd) & NAND_STATUS_READY)
  890. break;
  891. }
  892. cond_resched();
  893. } while (time_before(jiffies, timeo));
  894. }
  895. status = (int)chip->read_byte(mtd);
  896. /* This can happen if in case of timeout or buggy dev_ready */
  897. WARN_ON(!(status & NAND_STATUS_READY));
  898. return status;
  899. }
  900. /**
  901. * nand_reset_data_interface - Reset data interface and timings
  902. * @chip: The NAND chip
  903. *
  904. * Reset the Data interface and timings to ONFI mode 0.
  905. *
  906. * Returns 0 for success or negative error code otherwise.
  907. */
  908. static int nand_reset_data_interface(struct nand_chip *chip)
  909. {
  910. struct mtd_info *mtd = nand_to_mtd(chip);
  911. const struct nand_data_interface *conf;
  912. int ret;
  913. if (!chip->setup_data_interface)
  914. return 0;
  915. /*
  916. * The ONFI specification says:
  917. * "
  918. * To transition from NV-DDR or NV-DDR2 to the SDR data
  919. * interface, the host shall use the Reset (FFh) command
  920. * using SDR timing mode 0. A device in any timing mode is
  921. * required to recognize Reset (FFh) command issued in SDR
  922. * timing mode 0.
  923. * "
  924. *
  925. * Configure the data interface in SDR mode and set the
  926. * timings to timing mode 0.
  927. */
  928. conf = nand_get_default_data_interface();
  929. ret = chip->setup_data_interface(mtd, conf, false);
  930. if (ret)
  931. pr_err("Failed to configure data interface to SDR timing mode 0\n");
  932. return ret;
  933. }
  934. /**
  935. * nand_setup_data_interface - Setup the best data interface and timings
  936. * @chip: The NAND chip
  937. *
  938. * Find and configure the best data interface and NAND timings supported by
  939. * the chip and the driver.
  940. * First tries to retrieve supported timing modes from ONFI information,
  941. * and if the NAND chip does not support ONFI, relies on the
  942. * ->onfi_timing_mode_default specified in the nand_ids table.
  943. *
  944. * Returns 0 for success or negative error code otherwise.
  945. */
  946. static int nand_setup_data_interface(struct nand_chip *chip)
  947. {
  948. struct mtd_info *mtd = nand_to_mtd(chip);
  949. int ret;
  950. if (!chip->setup_data_interface || !chip->data_interface)
  951. return 0;
  952. /*
  953. * Ensure the timing mode has been changed on the chip side
  954. * before changing timings on the controller side.
  955. */
  956. if (chip->onfi_version &&
  957. (le16_to_cpu(chip->onfi_params.opt_cmd) &
  958. ONFI_OPT_CMD_SET_GET_FEATURES)) {
  959. u8 tmode_param[ONFI_SUBFEATURE_PARAM_LEN] = {
  960. chip->onfi_timing_mode_default,
  961. };
  962. ret = chip->onfi_set_features(mtd, chip,
  963. ONFI_FEATURE_ADDR_TIMING_MODE,
  964. tmode_param);
  965. if (ret)
  966. goto err;
  967. }
  968. ret = chip->setup_data_interface(mtd, chip->data_interface, false);
  969. err:
  970. return ret;
  971. }
  972. /**
  973. * nand_init_data_interface - find the best data interface and timings
  974. * @chip: The NAND chip
  975. *
  976. * Find the best data interface and NAND timings supported by the chip
  977. * and the driver.
  978. * First tries to retrieve supported timing modes from ONFI information,
  979. * and if the NAND chip does not support ONFI, relies on the
  980. * ->onfi_timing_mode_default specified in the nand_ids table. After this
  981. * function nand_chip->data_interface is initialized with the best timing mode
  982. * available.
  983. *
  984. * Returns 0 for success or negative error code otherwise.
  985. */
  986. static int nand_init_data_interface(struct nand_chip *chip)
  987. {
  988. struct mtd_info *mtd = nand_to_mtd(chip);
  989. int modes, mode, ret;
  990. if (!chip->setup_data_interface)
  991. return 0;
  992. /*
  993. * First try to identify the best timings from ONFI parameters and
  994. * if the NAND does not support ONFI, fallback to the default ONFI
  995. * timing mode.
  996. */
  997. modes = onfi_get_async_timing_mode(chip);
  998. if (modes == ONFI_TIMING_MODE_UNKNOWN) {
  999. if (!chip->onfi_timing_mode_default)
  1000. return 0;
  1001. modes = GENMASK(chip->onfi_timing_mode_default, 0);
  1002. }
  1003. chip->data_interface = kzalloc(sizeof(*chip->data_interface),
  1004. GFP_KERNEL);
  1005. if (!chip->data_interface)
  1006. return -ENOMEM;
  1007. for (mode = fls(modes) - 1; mode >= 0; mode--) {
  1008. ret = onfi_init_data_interface(chip, chip->data_interface,
  1009. NAND_SDR_IFACE, mode);
  1010. if (ret)
  1011. continue;
  1012. ret = chip->setup_data_interface(mtd, chip->data_interface,
  1013. true);
  1014. if (!ret) {
  1015. chip->onfi_timing_mode_default = mode;
  1016. break;
  1017. }
  1018. }
  1019. return 0;
  1020. }
  1021. static void nand_release_data_interface(struct nand_chip *chip)
  1022. {
  1023. kfree(chip->data_interface);
  1024. }
  1025. /**
  1026. * nand_reset - Reset and initialize a NAND device
  1027. * @chip: The NAND chip
  1028. * @chipnr: Internal die id
  1029. *
  1030. * Returns 0 for success or negative error code otherwise
  1031. */
  1032. int nand_reset(struct nand_chip *chip, int chipnr)
  1033. {
  1034. struct mtd_info *mtd = nand_to_mtd(chip);
  1035. int ret;
  1036. ret = nand_reset_data_interface(chip);
  1037. if (ret)
  1038. return ret;
  1039. /*
  1040. * The CS line has to be released before we can apply the new NAND
  1041. * interface settings, hence this weird ->select_chip() dance.
  1042. */
  1043. chip->select_chip(mtd, chipnr);
  1044. chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
  1045. chip->select_chip(mtd, -1);
  1046. chip->select_chip(mtd, chipnr);
  1047. ret = nand_setup_data_interface(chip);
  1048. chip->select_chip(mtd, -1);
  1049. if (ret)
  1050. return ret;
  1051. return 0;
  1052. }
  1053. /**
  1054. * __nand_unlock - [REPLACEABLE] unlocks specified locked blocks
  1055. * @mtd: mtd info
  1056. * @ofs: offset to start unlock from
  1057. * @len: length to unlock
  1058. * @invert: when = 0, unlock the range of blocks within the lower and
  1059. * upper boundary address
  1060. * when = 1, unlock the range of blocks outside the boundaries
  1061. * of the lower and upper boundary address
  1062. *
  1063. * Returs unlock status.
  1064. */
  1065. static int __nand_unlock(struct mtd_info *mtd, loff_t ofs,
  1066. uint64_t len, int invert)
  1067. {
  1068. int ret = 0;
  1069. int status, page;
  1070. struct nand_chip *chip = mtd_to_nand(mtd);
  1071. /* Submit address of first page to unlock */
  1072. page = ofs >> chip->page_shift;
  1073. chip->cmdfunc(mtd, NAND_CMD_UNLOCK1, -1, page & chip->pagemask);
  1074. /* Submit address of last page to unlock */
  1075. page = (ofs + len) >> chip->page_shift;
  1076. chip->cmdfunc(mtd, NAND_CMD_UNLOCK2, -1,
  1077. (page | invert) & chip->pagemask);
  1078. /* Call wait ready function */
  1079. status = chip->waitfunc(mtd, chip);
  1080. /* See if device thinks it succeeded */
  1081. if (status & NAND_STATUS_FAIL) {
  1082. pr_debug("%s: error status = 0x%08x\n",
  1083. __func__, status);
  1084. ret = -EIO;
  1085. }
  1086. return ret;
  1087. }
  1088. /**
  1089. * nand_unlock - [REPLACEABLE] unlocks specified locked blocks
  1090. * @mtd: mtd info
  1091. * @ofs: offset to start unlock from
  1092. * @len: length to unlock
  1093. *
  1094. * Returns unlock status.
  1095. */
  1096. int nand_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  1097. {
  1098. int ret = 0;
  1099. int chipnr;
  1100. struct nand_chip *chip = mtd_to_nand(mtd);
  1101. pr_debug("%s: start = 0x%012llx, len = %llu\n",
  1102. __func__, (unsigned long long)ofs, len);
  1103. if (check_offs_len(mtd, ofs, len))
  1104. return -EINVAL;
  1105. /* Align to last block address if size addresses end of the device */
  1106. if (ofs + len == mtd->size)
  1107. len -= mtd->erasesize;
  1108. nand_get_device(mtd, FL_UNLOCKING);
  1109. /* Shift to get chip number */
  1110. chipnr = ofs >> chip->chip_shift;
  1111. /*
  1112. * Reset the chip.
  1113. * If we want to check the WP through READ STATUS and check the bit 7
  1114. * we must reset the chip
  1115. * some operation can also clear the bit 7 of status register
  1116. * eg. erase/program a locked block
  1117. */
  1118. nand_reset(chip, chipnr);
  1119. chip->select_chip(mtd, chipnr);
  1120. /* Check, if it is write protected */
  1121. if (nand_check_wp(mtd)) {
  1122. pr_debug("%s: device is write protected!\n",
  1123. __func__);
  1124. ret = -EIO;
  1125. goto out;
  1126. }
  1127. ret = __nand_unlock(mtd, ofs, len, 0);
  1128. out:
  1129. chip->select_chip(mtd, -1);
  1130. nand_release_device(mtd);
  1131. return ret;
  1132. }
  1133. EXPORT_SYMBOL(nand_unlock);
  1134. /**
  1135. * nand_lock - [REPLACEABLE] locks all blocks present in the device
  1136. * @mtd: mtd info
  1137. * @ofs: offset to start unlock from
  1138. * @len: length to unlock
  1139. *
  1140. * This feature is not supported in many NAND parts. 'Micron' NAND parts do
  1141. * have this feature, but it allows only to lock all blocks, not for specified
  1142. * range for block. Implementing 'lock' feature by making use of 'unlock', for
  1143. * now.
  1144. *
  1145. * Returns lock status.
  1146. */
  1147. int nand_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  1148. {
  1149. int ret = 0;
  1150. int chipnr, status, page;
  1151. struct nand_chip *chip = mtd_to_nand(mtd);
  1152. pr_debug("%s: start = 0x%012llx, len = %llu\n",
  1153. __func__, (unsigned long long)ofs, len);
  1154. if (check_offs_len(mtd, ofs, len))
  1155. return -EINVAL;
  1156. nand_get_device(mtd, FL_LOCKING);
  1157. /* Shift to get chip number */
  1158. chipnr = ofs >> chip->chip_shift;
  1159. /*
  1160. * Reset the chip.
  1161. * If we want to check the WP through READ STATUS and check the bit 7
  1162. * we must reset the chip
  1163. * some operation can also clear the bit 7 of status register
  1164. * eg. erase/program a locked block
  1165. */
  1166. nand_reset(chip, chipnr);
  1167. chip->select_chip(mtd, chipnr);
  1168. /* Check, if it is write protected */
  1169. if (nand_check_wp(mtd)) {
  1170. pr_debug("%s: device is write protected!\n",
  1171. __func__);
  1172. status = MTD_ERASE_FAILED;
  1173. ret = -EIO;
  1174. goto out;
  1175. }
  1176. /* Submit address of first page to lock */
  1177. page = ofs >> chip->page_shift;
  1178. chip->cmdfunc(mtd, NAND_CMD_LOCK, -1, page & chip->pagemask);
  1179. /* Call wait ready function */
  1180. status = chip->waitfunc(mtd, chip);
  1181. /* See if device thinks it succeeded */
  1182. if (status & NAND_STATUS_FAIL) {
  1183. pr_debug("%s: error status = 0x%08x\n",
  1184. __func__, status);
  1185. ret = -EIO;
  1186. goto out;
  1187. }
  1188. ret = __nand_unlock(mtd, ofs, len, 0x1);
  1189. out:
  1190. chip->select_chip(mtd, -1);
  1191. nand_release_device(mtd);
  1192. return ret;
  1193. }
  1194. EXPORT_SYMBOL(nand_lock);
  1195. /**
  1196. * nand_check_erased_buf - check if a buffer contains (almost) only 0xff data
  1197. * @buf: buffer to test
  1198. * @len: buffer length
  1199. * @bitflips_threshold: maximum number of bitflips
  1200. *
  1201. * Check if a buffer contains only 0xff, which means the underlying region
  1202. * has been erased and is ready to be programmed.
  1203. * The bitflips_threshold specify the maximum number of bitflips before
  1204. * considering the region is not erased.
  1205. * Note: The logic of this function has been extracted from the memweight
  1206. * implementation, except that nand_check_erased_buf function exit before
  1207. * testing the whole buffer if the number of bitflips exceed the
  1208. * bitflips_threshold value.
  1209. *
  1210. * Returns a positive number of bitflips less than or equal to
  1211. * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
  1212. * threshold.
  1213. */
  1214. static int nand_check_erased_buf(void *buf, int len, int bitflips_threshold)
  1215. {
  1216. const unsigned char *bitmap = buf;
  1217. int bitflips = 0;
  1218. int weight;
  1219. for (; len && ((uintptr_t)bitmap) % sizeof(long);
  1220. len--, bitmap++) {
  1221. weight = hweight8(*bitmap);
  1222. bitflips += BITS_PER_BYTE - weight;
  1223. if (unlikely(bitflips > bitflips_threshold))
  1224. return -EBADMSG;
  1225. }
  1226. for (; len >= sizeof(long);
  1227. len -= sizeof(long), bitmap += sizeof(long)) {
  1228. weight = hweight_long(*((unsigned long *)bitmap));
  1229. bitflips += BITS_PER_LONG - weight;
  1230. if (unlikely(bitflips > bitflips_threshold))
  1231. return -EBADMSG;
  1232. }
  1233. for (; len > 0; len--, bitmap++) {
  1234. weight = hweight8(*bitmap);
  1235. bitflips += BITS_PER_BYTE - weight;
  1236. if (unlikely(bitflips > bitflips_threshold))
  1237. return -EBADMSG;
  1238. }
  1239. return bitflips;
  1240. }
  1241. /**
  1242. * nand_check_erased_ecc_chunk - check if an ECC chunk contains (almost) only
  1243. * 0xff data
  1244. * @data: data buffer to test
  1245. * @datalen: data length
  1246. * @ecc: ECC buffer
  1247. * @ecclen: ECC length
  1248. * @extraoob: extra OOB buffer
  1249. * @extraooblen: extra OOB length
  1250. * @bitflips_threshold: maximum number of bitflips
  1251. *
  1252. * Check if a data buffer and its associated ECC and OOB data contains only
  1253. * 0xff pattern, which means the underlying region has been erased and is
  1254. * ready to be programmed.
  1255. * The bitflips_threshold specify the maximum number of bitflips before
  1256. * considering the region as not erased.
  1257. *
  1258. * Note:
  1259. * 1/ ECC algorithms are working on pre-defined block sizes which are usually
  1260. * different from the NAND page size. When fixing bitflips, ECC engines will
  1261. * report the number of errors per chunk, and the NAND core infrastructure
  1262. * expect you to return the maximum number of bitflips for the whole page.
  1263. * This is why you should always use this function on a single chunk and
  1264. * not on the whole page. After checking each chunk you should update your
  1265. * max_bitflips value accordingly.
  1266. * 2/ When checking for bitflips in erased pages you should not only check
  1267. * the payload data but also their associated ECC data, because a user might
  1268. * have programmed almost all bits to 1 but a few. In this case, we
  1269. * shouldn't consider the chunk as erased, and checking ECC bytes prevent
  1270. * this case.
  1271. * 3/ The extraoob argument is optional, and should be used if some of your OOB
  1272. * data are protected by the ECC engine.
  1273. * It could also be used if you support subpages and want to attach some
  1274. * extra OOB data to an ECC chunk.
  1275. *
  1276. * Returns a positive number of bitflips less than or equal to
  1277. * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
  1278. * threshold. In case of success, the passed buffers are filled with 0xff.
  1279. */
  1280. int nand_check_erased_ecc_chunk(void *data, int datalen,
  1281. void *ecc, int ecclen,
  1282. void *extraoob, int extraooblen,
  1283. int bitflips_threshold)
  1284. {
  1285. int data_bitflips = 0, ecc_bitflips = 0, extraoob_bitflips = 0;
  1286. data_bitflips = nand_check_erased_buf(data, datalen,
  1287. bitflips_threshold);
  1288. if (data_bitflips < 0)
  1289. return data_bitflips;
  1290. bitflips_threshold -= data_bitflips;
  1291. ecc_bitflips = nand_check_erased_buf(ecc, ecclen, bitflips_threshold);
  1292. if (ecc_bitflips < 0)
  1293. return ecc_bitflips;
  1294. bitflips_threshold -= ecc_bitflips;
  1295. extraoob_bitflips = nand_check_erased_buf(extraoob, extraooblen,
  1296. bitflips_threshold);
  1297. if (extraoob_bitflips < 0)
  1298. return extraoob_bitflips;
  1299. if (data_bitflips)
  1300. memset(data, 0xff, datalen);
  1301. if (ecc_bitflips)
  1302. memset(ecc, 0xff, ecclen);
  1303. if (extraoob_bitflips)
  1304. memset(extraoob, 0xff, extraooblen);
  1305. return data_bitflips + ecc_bitflips + extraoob_bitflips;
  1306. }
  1307. EXPORT_SYMBOL(nand_check_erased_ecc_chunk);
  1308. /**
  1309. * nand_read_page_raw - [INTERN] read raw page data without ecc
  1310. * @mtd: mtd info structure
  1311. * @chip: nand chip info structure
  1312. * @buf: buffer to store read data
  1313. * @oob_required: caller requires OOB data read to chip->oob_poi
  1314. * @page: page number to read
  1315. *
  1316. * Not for syndrome calculating ECC controllers, which use a special oob layout.
  1317. */
  1318. static int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
  1319. uint8_t *buf, int oob_required, int page)
  1320. {
  1321. chip->read_buf(mtd, buf, mtd->writesize);
  1322. if (oob_required)
  1323. chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
  1324. return 0;
  1325. }
  1326. /**
  1327. * nand_read_page_raw_syndrome - [INTERN] read raw page data without ecc
  1328. * @mtd: mtd info structure
  1329. * @chip: nand chip info structure
  1330. * @buf: buffer to store read data
  1331. * @oob_required: caller requires OOB data read to chip->oob_poi
  1332. * @page: page number to read
  1333. *
  1334. * We need a special oob layout and handling even when OOB isn't used.
  1335. */
  1336. static int nand_read_page_raw_syndrome(struct mtd_info *mtd,
  1337. struct nand_chip *chip, uint8_t *buf,
  1338. int oob_required, int page)
  1339. {
  1340. int eccsize = chip->ecc.size;
  1341. int eccbytes = chip->ecc.bytes;
  1342. uint8_t *oob = chip->oob_poi;
  1343. int steps, size;
  1344. for (steps = chip->ecc.steps; steps > 0; steps--) {
  1345. chip->read_buf(mtd, buf, eccsize);
  1346. buf += eccsize;
  1347. if (chip->ecc.prepad) {
  1348. chip->read_buf(mtd, oob, chip->ecc.prepad);
  1349. oob += chip->ecc.prepad;
  1350. }
  1351. chip->read_buf(mtd, oob, eccbytes);
  1352. oob += eccbytes;
  1353. if (chip->ecc.postpad) {
  1354. chip->read_buf(mtd, oob, chip->ecc.postpad);
  1355. oob += chip->ecc.postpad;
  1356. }
  1357. }
  1358. size = mtd->oobsize - (oob - chip->oob_poi);
  1359. if (size)
  1360. chip->read_buf(mtd, oob, size);
  1361. return 0;
  1362. }
  1363. /**
  1364. * nand_read_page_swecc - [REPLACEABLE] software ECC based page read function
  1365. * @mtd: mtd info structure
  1366. * @chip: nand chip info structure
  1367. * @buf: buffer to store read data
  1368. * @oob_required: caller requires OOB data read to chip->oob_poi
  1369. * @page: page number to read
  1370. */
  1371. static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
  1372. uint8_t *buf, int oob_required, int page)
  1373. {
  1374. int i, eccsize = chip->ecc.size, ret;
  1375. int eccbytes = chip->ecc.bytes;
  1376. int eccsteps = chip->ecc.steps;
  1377. uint8_t *p = buf;
  1378. uint8_t *ecc_calc = chip->buffers->ecccalc;
  1379. uint8_t *ecc_code = chip->buffers->ecccode;
  1380. unsigned int max_bitflips = 0;
  1381. chip->ecc.read_page_raw(mtd, chip, buf, 1, page);
  1382. for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
  1383. chip->ecc.calculate(mtd, p, &ecc_calc[i]);
  1384. ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
  1385. chip->ecc.total);
  1386. if (ret)
  1387. return ret;
  1388. eccsteps = chip->ecc.steps;
  1389. p = buf;
  1390. for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
  1391. int stat;
  1392. stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
  1393. if (stat < 0) {
  1394. mtd->ecc_stats.failed++;
  1395. } else {
  1396. mtd->ecc_stats.corrected += stat;
  1397. max_bitflips = max_t(unsigned int, max_bitflips, stat);
  1398. }
  1399. }
  1400. return max_bitflips;
  1401. }
  1402. /**
  1403. * nand_read_subpage - [REPLACEABLE] ECC based sub-page read function
  1404. * @mtd: mtd info structure
  1405. * @chip: nand chip info structure
  1406. * @data_offs: offset of requested data within the page
  1407. * @readlen: data length
  1408. * @bufpoi: buffer to store read data
  1409. * @page: page number to read
  1410. */
  1411. static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip,
  1412. uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi,
  1413. int page)
  1414. {
  1415. int start_step, end_step, num_steps, ret;
  1416. uint8_t *p;
  1417. int data_col_addr, i, gaps = 0;
  1418. int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
  1419. int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
  1420. int index, section = 0;
  1421. unsigned int max_bitflips = 0;
  1422. struct mtd_oob_region oobregion = { };
  1423. /* Column address within the page aligned to ECC size (256bytes) */
  1424. start_step = data_offs / chip->ecc.size;
  1425. end_step = (data_offs + readlen - 1) / chip->ecc.size;
  1426. num_steps = end_step - start_step + 1;
  1427. index = start_step * chip->ecc.bytes;
  1428. /* Data size aligned to ECC ecc.size */
  1429. datafrag_len = num_steps * chip->ecc.size;
  1430. eccfrag_len = num_steps * chip->ecc.bytes;
  1431. data_col_addr = start_step * chip->ecc.size;
  1432. /* If we read not a page aligned data */
  1433. if (data_col_addr != 0)
  1434. chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_col_addr, -1);
  1435. p = bufpoi + data_col_addr;
  1436. chip->read_buf(mtd, p, datafrag_len);
  1437. /* Calculate ECC */
  1438. for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
  1439. chip->ecc.calculate(mtd, p, &chip->buffers->ecccalc[i]);
  1440. /*
  1441. * The performance is faster if we position offsets according to
  1442. * ecc.pos. Let's make sure that there are no gaps in ECC positions.
  1443. */
  1444. ret = mtd_ooblayout_find_eccregion(mtd, index, &section, &oobregion);
  1445. if (ret)
  1446. return ret;
  1447. if (oobregion.length < eccfrag_len)
  1448. gaps = 1;
  1449. if (gaps) {
  1450. chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
  1451. chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
  1452. } else {
  1453. /*
  1454. * Send the command to read the particular ECC bytes take care
  1455. * about buswidth alignment in read_buf.
  1456. */
  1457. aligned_pos = oobregion.offset & ~(busw - 1);
  1458. aligned_len = eccfrag_len;
  1459. if (oobregion.offset & (busw - 1))
  1460. aligned_len++;
  1461. if ((oobregion.offset + (num_steps * chip->ecc.bytes)) &
  1462. (busw - 1))
  1463. aligned_len++;
  1464. chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
  1465. mtd->writesize + aligned_pos, -1);
  1466. chip->read_buf(mtd, &chip->oob_poi[aligned_pos], aligned_len);
  1467. }
  1468. ret = mtd_ooblayout_get_eccbytes(mtd, chip->buffers->ecccode,
  1469. chip->oob_poi, index, eccfrag_len);
  1470. if (ret)
  1471. return ret;
  1472. p = bufpoi + data_col_addr;
  1473. for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
  1474. int stat;
  1475. stat = chip->ecc.correct(mtd, p,
  1476. &chip->buffers->ecccode[i], &chip->buffers->ecccalc[i]);
  1477. if (stat == -EBADMSG &&
  1478. (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
  1479. /* check for empty pages with bitflips */
  1480. stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
  1481. &chip->buffers->ecccode[i],
  1482. chip->ecc.bytes,
  1483. NULL, 0,
  1484. chip->ecc.strength);
  1485. }
  1486. if (stat < 0) {
  1487. mtd->ecc_stats.failed++;
  1488. } else {
  1489. mtd->ecc_stats.corrected += stat;
  1490. max_bitflips = max_t(unsigned int, max_bitflips, stat);
  1491. }
  1492. }
  1493. return max_bitflips;
  1494. }
  1495. /**
  1496. * nand_read_page_hwecc - [REPLACEABLE] hardware ECC based page read function
  1497. * @mtd: mtd info structure
  1498. * @chip: nand chip info structure
  1499. * @buf: buffer to store read data
  1500. * @oob_required: caller requires OOB data read to chip->oob_poi
  1501. * @page: page number to read
  1502. *
  1503. * Not for syndrome calculating ECC controllers which need a special oob layout.
  1504. */
  1505. static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
  1506. uint8_t *buf, int oob_required, int page)
  1507. {
  1508. int i, eccsize = chip->ecc.size, ret;
  1509. int eccbytes = chip->ecc.bytes;
  1510. int eccsteps = chip->ecc.steps;
  1511. uint8_t *p = buf;
  1512. uint8_t *ecc_calc = chip->buffers->ecccalc;
  1513. uint8_t *ecc_code = chip->buffers->ecccode;
  1514. unsigned int max_bitflips = 0;
  1515. for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
  1516. chip->ecc.hwctl(mtd, NAND_ECC_READ);
  1517. chip->read_buf(mtd, p, eccsize);
  1518. chip->ecc.calculate(mtd, p, &ecc_calc[i]);
  1519. }
  1520. chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
  1521. ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
  1522. chip->ecc.total);
  1523. if (ret)
  1524. return ret;
  1525. eccsteps = chip->ecc.steps;
  1526. p = buf;
  1527. for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
  1528. int stat;
  1529. stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
  1530. if (stat == -EBADMSG &&
  1531. (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
  1532. /* check for empty pages with bitflips */
  1533. stat = nand_check_erased_ecc_chunk(p, eccsize,
  1534. &ecc_code[i], eccbytes,
  1535. NULL, 0,
  1536. chip->ecc.strength);
  1537. }
  1538. if (stat < 0) {
  1539. mtd->ecc_stats.failed++;
  1540. } else {
  1541. mtd->ecc_stats.corrected += stat;
  1542. max_bitflips = max_t(unsigned int, max_bitflips, stat);
  1543. }
  1544. }
  1545. return max_bitflips;
  1546. }
  1547. /**
  1548. * nand_read_page_hwecc_oob_first - [REPLACEABLE] hw ecc, read oob first
  1549. * @mtd: mtd info structure
  1550. * @chip: nand chip info structure
  1551. * @buf: buffer to store read data
  1552. * @oob_required: caller requires OOB data read to chip->oob_poi
  1553. * @page: page number to read
  1554. *
  1555. * Hardware ECC for large page chips, require OOB to be read first. For this
  1556. * ECC mode, the write_page method is re-used from ECC_HW. These methods
  1557. * read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with
  1558. * multiple ECC steps, follows the "infix ECC" scheme and reads/writes ECC from
  1559. * the data area, by overwriting the NAND manufacturer bad block markings.
  1560. */
  1561. static int nand_read_page_hwecc_oob_first(struct mtd_info *mtd,
  1562. struct nand_chip *chip, uint8_t *buf, int oob_required, int page)
  1563. {
  1564. int i, eccsize = chip->ecc.size, ret;
  1565. int eccbytes = chip->ecc.bytes;
  1566. int eccsteps = chip->ecc.steps;
  1567. uint8_t *p = buf;
  1568. uint8_t *ecc_code = chip->buffers->ecccode;
  1569. uint8_t *ecc_calc = chip->buffers->ecccalc;
  1570. unsigned int max_bitflips = 0;
  1571. /* Read the OOB area first */
  1572. chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
  1573. chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
  1574. chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
  1575. ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
  1576. chip->ecc.total);
  1577. if (ret)
  1578. return ret;
  1579. for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
  1580. int stat;
  1581. chip->ecc.hwctl(mtd, NAND_ECC_READ);
  1582. chip->read_buf(mtd, p, eccsize);
  1583. chip->ecc.calculate(mtd, p, &ecc_calc[i]);
  1584. stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL);
  1585. if (stat == -EBADMSG &&
  1586. (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
  1587. /* check for empty pages with bitflips */
  1588. stat = nand_check_erased_ecc_chunk(p, eccsize,
  1589. &ecc_code[i], eccbytes,
  1590. NULL, 0,
  1591. chip->ecc.strength);
  1592. }
  1593. if (stat < 0) {
  1594. mtd->ecc_stats.failed++;
  1595. } else {
  1596. mtd->ecc_stats.corrected += stat;
  1597. max_bitflips = max_t(unsigned int, max_bitflips, stat);
  1598. }
  1599. }
  1600. return max_bitflips;
  1601. }
  1602. /**
  1603. * nand_read_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page read
  1604. * @mtd: mtd info structure
  1605. * @chip: nand chip info structure
  1606. * @buf: buffer to store read data
  1607. * @oob_required: caller requires OOB data read to chip->oob_poi
  1608. * @page: page number to read
  1609. *
  1610. * The hw generator calculates the error syndrome automatically. Therefore we
  1611. * need a special oob layout and handling.
  1612. */
  1613. static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
  1614. uint8_t *buf, int oob_required, int page)
  1615. {
  1616. int i, eccsize = chip->ecc.size;
  1617. int eccbytes = chip->ecc.bytes;
  1618. int eccsteps = chip->ecc.steps;
  1619. int eccpadbytes = eccbytes + chip->ecc.prepad + chip->ecc.postpad;
  1620. uint8_t *p = buf;
  1621. uint8_t *oob = chip->oob_poi;
  1622. unsigned int max_bitflips = 0;
  1623. for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
  1624. int stat;
  1625. chip->ecc.hwctl(mtd, NAND_ECC_READ);
  1626. chip->read_buf(mtd, p, eccsize);
  1627. if (chip->ecc.prepad) {
  1628. chip->read_buf(mtd, oob, chip->ecc.prepad);
  1629. oob += chip->ecc.prepad;
  1630. }
  1631. chip->ecc.hwctl(mtd, NAND_ECC_READSYN);
  1632. chip->read_buf(mtd, oob, eccbytes);
  1633. stat = chip->ecc.correct(mtd, p, oob, NULL);
  1634. oob += eccbytes;
  1635. if (chip->ecc.postpad) {
  1636. chip->read_buf(mtd, oob, chip->ecc.postpad);
  1637. oob += chip->ecc.postpad;
  1638. }
  1639. if (stat == -EBADMSG &&
  1640. (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
  1641. /* check for empty pages with bitflips */
  1642. stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
  1643. oob - eccpadbytes,
  1644. eccpadbytes,
  1645. NULL, 0,
  1646. chip->ecc.strength);
  1647. }
  1648. if (stat < 0) {
  1649. mtd->ecc_stats.failed++;
  1650. } else {
  1651. mtd->ecc_stats.corrected += stat;
  1652. max_bitflips = max_t(unsigned int, max_bitflips, stat);
  1653. }
  1654. }
  1655. /* Calculate remaining oob bytes */
  1656. i = mtd->oobsize - (oob - chip->oob_poi);
  1657. if (i)
  1658. chip->read_buf(mtd, oob, i);
  1659. return max_bitflips;
  1660. }
  1661. /**
  1662. * nand_transfer_oob - [INTERN] Transfer oob to client buffer
  1663. * @mtd: mtd info structure
  1664. * @oob: oob destination address
  1665. * @ops: oob ops structure
  1666. * @len: size of oob to transfer
  1667. */
  1668. static uint8_t *nand_transfer_oob(struct mtd_info *mtd, uint8_t *oob,
  1669. struct mtd_oob_ops *ops, size_t len)
  1670. {
  1671. struct nand_chip *chip = mtd_to_nand(mtd);
  1672. int ret;
  1673. switch (ops->mode) {
  1674. case MTD_OPS_PLACE_OOB:
  1675. case MTD_OPS_RAW:
  1676. memcpy(oob, chip->oob_poi + ops->ooboffs, len);
  1677. return oob + len;
  1678. case MTD_OPS_AUTO_OOB:
  1679. ret = mtd_ooblayout_get_databytes(mtd, oob, chip->oob_poi,
  1680. ops->ooboffs, len);
  1681. BUG_ON(ret);
  1682. return oob + len;
  1683. default:
  1684. BUG();
  1685. }
  1686. return NULL;
  1687. }
  1688. /**
  1689. * nand_setup_read_retry - [INTERN] Set the READ RETRY mode
  1690. * @mtd: MTD device structure
  1691. * @retry_mode: the retry mode to use
  1692. *
  1693. * Some vendors supply a special command to shift the Vt threshold, to be used
  1694. * when there are too many bitflips in a page (i.e., ECC error). After setting
  1695. * a new threshold, the host should retry reading the page.
  1696. */
  1697. static int nand_setup_read_retry(struct mtd_info *mtd, int retry_mode)
  1698. {
  1699. struct nand_chip *chip = mtd_to_nand(mtd);
  1700. pr_debug("setting READ RETRY mode %d\n", retry_mode);
  1701. if (retry_mode >= chip->read_retries)
  1702. return -EINVAL;
  1703. if (!chip->setup_read_retry)
  1704. return -EOPNOTSUPP;
  1705. return chip->setup_read_retry(mtd, retry_mode);
  1706. }
  1707. /**
  1708. * nand_do_read_ops - [INTERN] Read data with ECC
  1709. * @mtd: MTD device structure
  1710. * @from: offset to read from
  1711. * @ops: oob ops structure
  1712. *
  1713. * Internal function. Called with chip held.
  1714. */
  1715. static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
  1716. struct mtd_oob_ops *ops)
  1717. {
  1718. int chipnr, page, realpage, col, bytes, aligned, oob_required;
  1719. struct nand_chip *chip = mtd_to_nand(mtd);
  1720. int ret = 0;
  1721. uint32_t readlen = ops->len;
  1722. uint32_t oobreadlen = ops->ooblen;
  1723. uint32_t max_oobsize = mtd_oobavail(mtd, ops);
  1724. uint8_t *bufpoi, *oob, *buf;
  1725. int use_bufpoi;
  1726. unsigned int max_bitflips = 0;
  1727. int retry_mode = 0;
  1728. bool ecc_fail = false;
  1729. chipnr = (int)(from >> chip->chip_shift);
  1730. chip->select_chip(mtd, chipnr);
  1731. realpage = (int)(from >> chip->page_shift);
  1732. page = realpage & chip->pagemask;
  1733. col = (int)(from & (mtd->writesize - 1));
  1734. buf = ops->datbuf;
  1735. oob = ops->oobbuf;
  1736. oob_required = oob ? 1 : 0;
  1737. while (1) {
  1738. unsigned int ecc_failures = mtd->ecc_stats.failed;
  1739. bytes = min(mtd->writesize - col, readlen);
  1740. aligned = (bytes == mtd->writesize);
  1741. if (!aligned)
  1742. use_bufpoi = 1;
  1743. else if (chip->options & NAND_USE_BOUNCE_BUFFER)
  1744. use_bufpoi = !virt_addr_valid(buf);
  1745. else
  1746. use_bufpoi = 0;
  1747. /* Is the current page in the buffer? */
  1748. if (realpage != chip->pagebuf || oob) {
  1749. bufpoi = use_bufpoi ? chip->buffers->databuf : buf;
  1750. if (use_bufpoi && aligned)
  1751. pr_debug("%s: using read bounce buffer for buf@%p\n",
  1752. __func__, buf);
  1753. read_retry:
  1754. chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
  1755. /*
  1756. * Now read the page into the buffer. Absent an error,
  1757. * the read methods return max bitflips per ecc step.
  1758. */
  1759. if (unlikely(ops->mode == MTD_OPS_RAW))
  1760. ret = chip->ecc.read_page_raw(mtd, chip, bufpoi,
  1761. oob_required,
  1762. page);
  1763. else if (!aligned && NAND_HAS_SUBPAGE_READ(chip) &&
  1764. !oob)
  1765. ret = chip->ecc.read_subpage(mtd, chip,
  1766. col, bytes, bufpoi,
  1767. page);
  1768. else
  1769. ret = chip->ecc.read_page(mtd, chip, bufpoi,
  1770. oob_required, page);
  1771. if (ret < 0) {
  1772. if (use_bufpoi)
  1773. /* Invalidate page cache */
  1774. chip->pagebuf = -1;
  1775. break;
  1776. }
  1777. max_bitflips = max_t(unsigned int, max_bitflips, ret);
  1778. /* Transfer not aligned data */
  1779. if (use_bufpoi) {
  1780. if (!NAND_HAS_SUBPAGE_READ(chip) && !oob &&
  1781. !(mtd->ecc_stats.failed - ecc_failures) &&
  1782. (ops->mode != MTD_OPS_RAW)) {
  1783. chip->pagebuf = realpage;
  1784. chip->pagebuf_bitflips = ret;
  1785. } else {
  1786. /* Invalidate page cache */
  1787. chip->pagebuf = -1;
  1788. }
  1789. memcpy(buf, chip->buffers->databuf + col, bytes);
  1790. }
  1791. if (unlikely(oob)) {
  1792. int toread = min(oobreadlen, max_oobsize);
  1793. if (toread) {
  1794. oob = nand_transfer_oob(mtd,
  1795. oob, ops, toread);
  1796. oobreadlen -= toread;
  1797. }
  1798. }
  1799. if (chip->options & NAND_NEED_READRDY) {
  1800. /* Apply delay or wait for ready/busy pin */
  1801. if (!chip->dev_ready)
  1802. udelay(chip->chip_delay);
  1803. else
  1804. nand_wait_ready(mtd);
  1805. }
  1806. if (mtd->ecc_stats.failed - ecc_failures) {
  1807. if (retry_mode + 1 < chip->read_retries) {
  1808. retry_mode++;
  1809. ret = nand_setup_read_retry(mtd,
  1810. retry_mode);
  1811. if (ret < 0)
  1812. break;
  1813. /* Reset failures; retry */
  1814. mtd->ecc_stats.failed = ecc_failures;
  1815. goto read_retry;
  1816. } else {
  1817. /* No more retry modes; real failure */
  1818. ecc_fail = true;
  1819. }
  1820. }
  1821. buf += bytes;
  1822. } else {
  1823. memcpy(buf, chip->buffers->databuf + col, bytes);
  1824. buf += bytes;
  1825. max_bitflips = max_t(unsigned int, max_bitflips,
  1826. chip->pagebuf_bitflips);
  1827. }
  1828. readlen -= bytes;
  1829. /* Reset to retry mode 0 */
  1830. if (retry_mode) {
  1831. ret = nand_setup_read_retry(mtd, 0);
  1832. if (ret < 0)
  1833. break;
  1834. retry_mode = 0;
  1835. }
  1836. if (!readlen)
  1837. break;
  1838. /* For subsequent reads align to page boundary */
  1839. col = 0;
  1840. /* Increment page address */
  1841. realpage++;
  1842. page = realpage & chip->pagemask;
  1843. /* Check, if we cross a chip boundary */
  1844. if (!page) {
  1845. chipnr++;
  1846. chip->select_chip(mtd, -1);
  1847. chip->select_chip(mtd, chipnr);
  1848. }
  1849. }
  1850. chip->select_chip(mtd, -1);
  1851. ops->retlen = ops->len - (size_t) readlen;
  1852. if (oob)
  1853. ops->oobretlen = ops->ooblen - oobreadlen;
  1854. if (ret < 0)
  1855. return ret;
  1856. if (ecc_fail)
  1857. return -EBADMSG;
  1858. return max_bitflips;
  1859. }
  1860. /**
  1861. * nand_read - [MTD Interface] MTD compatibility function for nand_do_read_ecc
  1862. * @mtd: MTD device structure
  1863. * @from: offset to read from
  1864. * @len: number of bytes to read
  1865. * @retlen: pointer to variable to store the number of read bytes
  1866. * @buf: the databuffer to put data
  1867. *
  1868. * Get hold of the chip and call nand_do_read.
  1869. */
  1870. static int nand_read(struct mtd_info *mtd, loff_t from, size_t len,
  1871. size_t *retlen, uint8_t *buf)
  1872. {
  1873. struct mtd_oob_ops ops;
  1874. int ret;
  1875. nand_get_device(mtd, FL_READING);
  1876. memset(&ops, 0, sizeof(ops));
  1877. ops.len = len;
  1878. ops.datbuf = buf;
  1879. ops.mode = MTD_OPS_PLACE_OOB;
  1880. ret = nand_do_read_ops(mtd, from, &ops);
  1881. *retlen = ops.retlen;
  1882. nand_release_device(mtd);
  1883. return ret;
  1884. }
  1885. /**
  1886. * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function
  1887. * @mtd: mtd info structure
  1888. * @chip: nand chip info structure
  1889. * @page: page number to read
  1890. */
  1891. int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip, int page)
  1892. {
  1893. chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
  1894. chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
  1895. return 0;
  1896. }
  1897. EXPORT_SYMBOL(nand_read_oob_std);
  1898. /**
  1899. * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC
  1900. * with syndromes
  1901. * @mtd: mtd info structure
  1902. * @chip: nand chip info structure
  1903. * @page: page number to read
  1904. */
  1905. int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
  1906. int page)
  1907. {
  1908. int length = mtd->oobsize;
  1909. int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
  1910. int eccsize = chip->ecc.size;
  1911. uint8_t *bufpoi = chip->oob_poi;
  1912. int i, toread, sndrnd = 0, pos;
  1913. chip->cmdfunc(mtd, NAND_CMD_READ0, chip->ecc.size, page);
  1914. for (i = 0; i < chip->ecc.steps; i++) {
  1915. if (sndrnd) {
  1916. pos = eccsize + i * (eccsize + chunk);
  1917. if (mtd->writesize > 512)
  1918. chip->cmdfunc(mtd, NAND_CMD_RNDOUT, pos, -1);
  1919. else
  1920. chip->cmdfunc(mtd, NAND_CMD_READ0, pos, page);
  1921. } else
  1922. sndrnd = 1;
  1923. toread = min_t(int, length, chunk);
  1924. chip->read_buf(mtd, bufpoi, toread);
  1925. bufpoi += toread;
  1926. length -= toread;
  1927. }
  1928. if (length > 0)
  1929. chip->read_buf(mtd, bufpoi, length);
  1930. return 0;
  1931. }
  1932. EXPORT_SYMBOL(nand_read_oob_syndrome);
  1933. /**
  1934. * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function
  1935. * @mtd: mtd info structure
  1936. * @chip: nand chip info structure
  1937. * @page: page number to write
  1938. */
  1939. int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip, int page)
  1940. {
  1941. int status = 0;
  1942. const uint8_t *buf = chip->oob_poi;
  1943. int length = mtd->oobsize;
  1944. chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
  1945. chip->write_buf(mtd, buf, length);
  1946. /* Send command to program the OOB data */
  1947. chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
  1948. status = chip->waitfunc(mtd, chip);
  1949. return status & NAND_STATUS_FAIL ? -EIO : 0;
  1950. }
  1951. EXPORT_SYMBOL(nand_write_oob_std);
  1952. /**
  1953. * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC
  1954. * with syndrome - only for large page flash
  1955. * @mtd: mtd info structure
  1956. * @chip: nand chip info structure
  1957. * @page: page number to write
  1958. */
  1959. int nand_write_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
  1960. int page)
  1961. {
  1962. int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
  1963. int eccsize = chip->ecc.size, length = mtd->oobsize;
  1964. int i, len, pos, status = 0, sndcmd = 0, steps = chip->ecc.steps;
  1965. const uint8_t *bufpoi = chip->oob_poi;
  1966. /*
  1967. * data-ecc-data-ecc ... ecc-oob
  1968. * or
  1969. * data-pad-ecc-pad-data-pad .... ecc-pad-oob
  1970. */
  1971. if (!chip->ecc.prepad && !chip->ecc.postpad) {
  1972. pos = steps * (eccsize + chunk);
  1973. steps = 0;
  1974. } else
  1975. pos = eccsize;
  1976. chip->cmdfunc(mtd, NAND_CMD_SEQIN, pos, page);
  1977. for (i = 0; i < steps; i++) {
  1978. if (sndcmd) {
  1979. if (mtd->writesize <= 512) {
  1980. uint32_t fill = 0xFFFFFFFF;
  1981. len = eccsize;
  1982. while (len > 0) {
  1983. int num = min_t(int, len, 4);
  1984. chip->write_buf(mtd, (uint8_t *)&fill,
  1985. num);
  1986. len -= num;
  1987. }
  1988. } else {
  1989. pos = eccsize + i * (eccsize + chunk);
  1990. chip->cmdfunc(mtd, NAND_CMD_RNDIN, pos, -1);
  1991. }
  1992. } else
  1993. sndcmd = 1;
  1994. len = min_t(int, length, chunk);
  1995. chip->write_buf(mtd, bufpoi, len);
  1996. bufpoi += len;
  1997. length -= len;
  1998. }
  1999. if (length > 0)
  2000. chip->write_buf(mtd, bufpoi, length);
  2001. chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
  2002. status = chip->waitfunc(mtd, chip);
  2003. return status & NAND_STATUS_FAIL ? -EIO : 0;
  2004. }
  2005. EXPORT_SYMBOL(nand_write_oob_syndrome);
  2006. /**
  2007. * nand_do_read_oob - [INTERN] NAND read out-of-band
  2008. * @mtd: MTD device structure
  2009. * @from: offset to read from
  2010. * @ops: oob operations description structure
  2011. *
  2012. * NAND read out-of-band data from the spare area.
  2013. */
  2014. static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
  2015. struct mtd_oob_ops *ops)
  2016. {
  2017. int page, realpage, chipnr;
  2018. struct nand_chip *chip = mtd_to_nand(mtd);
  2019. struct mtd_ecc_stats stats;
  2020. int readlen = ops->ooblen;
  2021. int len;
  2022. uint8_t *buf = ops->oobbuf;
  2023. int ret = 0;
  2024. pr_debug("%s: from = 0x%08Lx, len = %i\n",
  2025. __func__, (unsigned long long)from, readlen);
  2026. stats = mtd->ecc_stats;
  2027. len = mtd_oobavail(mtd, ops);
  2028. if (unlikely(ops->ooboffs >= len)) {
  2029. pr_debug("%s: attempt to start read outside oob\n",
  2030. __func__);
  2031. return -EINVAL;
  2032. }
  2033. /* Do not allow reads past end of device */
  2034. if (unlikely(from >= mtd->size ||
  2035. ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) -
  2036. (from >> chip->page_shift)) * len)) {
  2037. pr_debug("%s: attempt to read beyond end of device\n",
  2038. __func__);
  2039. return -EINVAL;
  2040. }
  2041. chipnr = (int)(from >> chip->chip_shift);
  2042. chip->select_chip(mtd, chipnr);
  2043. /* Shift to get page */
  2044. realpage = (int)(from >> chip->page_shift);
  2045. page = realpage & chip->pagemask;
  2046. while (1) {
  2047. if (ops->mode == MTD_OPS_RAW)
  2048. ret = chip->ecc.read_oob_raw(mtd, chip, page);
  2049. else
  2050. ret = chip->ecc.read_oob(mtd, chip, page);
  2051. if (ret < 0)
  2052. break;
  2053. len = min(len, readlen);
  2054. buf = nand_transfer_oob(mtd, buf, ops, len);
  2055. if (chip->options & NAND_NEED_READRDY) {
  2056. /* Apply delay or wait for ready/busy pin */
  2057. if (!chip->dev_ready)
  2058. udelay(chip->chip_delay);
  2059. else
  2060. nand_wait_ready(mtd);
  2061. }
  2062. readlen -= len;
  2063. if (!readlen)
  2064. break;
  2065. /* Increment page address */
  2066. realpage++;
  2067. page = realpage & chip->pagemask;
  2068. /* Check, if we cross a chip boundary */
  2069. if (!page) {
  2070. chipnr++;
  2071. chip->select_chip(mtd, -1);
  2072. chip->select_chip(mtd, chipnr);
  2073. }
  2074. }
  2075. chip->select_chip(mtd, -1);
  2076. ops->oobretlen = ops->ooblen - readlen;
  2077. if (ret < 0)
  2078. return ret;
  2079. if (mtd->ecc_stats.failed - stats.failed)
  2080. return -EBADMSG;
  2081. return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
  2082. }
  2083. /**
  2084. * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
  2085. * @mtd: MTD device structure
  2086. * @from: offset to read from
  2087. * @ops: oob operation description structure
  2088. *
  2089. * NAND read data and/or out-of-band data.
  2090. */
  2091. static int nand_read_oob(struct mtd_info *mtd, loff_t from,
  2092. struct mtd_oob_ops *ops)
  2093. {
  2094. int ret;
  2095. ops->retlen = 0;
  2096. /* Do not allow reads past end of device */
  2097. if (ops->datbuf && (from + ops->len) > mtd->size) {
  2098. pr_debug("%s: attempt to read beyond end of device\n",
  2099. __func__);
  2100. return -EINVAL;
  2101. }
  2102. if (ops->mode != MTD_OPS_PLACE_OOB &&
  2103. ops->mode != MTD_OPS_AUTO_OOB &&
  2104. ops->mode != MTD_OPS_RAW)
  2105. return -ENOTSUPP;
  2106. nand_get_device(mtd, FL_READING);
  2107. if (!ops->datbuf)
  2108. ret = nand_do_read_oob(mtd, from, ops);
  2109. else
  2110. ret = nand_do_read_ops(mtd, from, ops);
  2111. nand_release_device(mtd);
  2112. return ret;
  2113. }
  2114. /**
  2115. * nand_write_page_raw - [INTERN] raw page write function
  2116. * @mtd: mtd info structure
  2117. * @chip: nand chip info structure
  2118. * @buf: data buffer
  2119. * @oob_required: must write chip->oob_poi to OOB
  2120. * @page: page number to write
  2121. *
  2122. * Not for syndrome calculating ECC controllers, which use a special oob layout.
  2123. */
  2124. static int nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
  2125. const uint8_t *buf, int oob_required, int page)
  2126. {
  2127. chip->write_buf(mtd, buf, mtd->writesize);
  2128. if (oob_required)
  2129. chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
  2130. return 0;
  2131. }
  2132. /**
  2133. * nand_write_page_raw_syndrome - [INTERN] raw page write function
  2134. * @mtd: mtd info structure
  2135. * @chip: nand chip info structure
  2136. * @buf: data buffer
  2137. * @oob_required: must write chip->oob_poi to OOB
  2138. * @page: page number to write
  2139. *
  2140. * We need a special oob layout and handling even when ECC isn't checked.
  2141. */
  2142. static int nand_write_page_raw_syndrome(struct mtd_info *mtd,
  2143. struct nand_chip *chip,
  2144. const uint8_t *buf, int oob_required,
  2145. int page)
  2146. {
  2147. int eccsize = chip->ecc.size;
  2148. int eccbytes = chip->ecc.bytes;
  2149. uint8_t *oob = chip->oob_poi;
  2150. int steps, size;
  2151. for (steps = chip->ecc.steps; steps > 0; steps--) {
  2152. chip->write_buf(mtd, buf, eccsize);
  2153. buf += eccsize;
  2154. if (chip->ecc.prepad) {
  2155. chip->write_buf(mtd, oob, chip->ecc.prepad);
  2156. oob += chip->ecc.prepad;
  2157. }
  2158. chip->write_buf(mtd, oob, eccbytes);
  2159. oob += eccbytes;
  2160. if (chip->ecc.postpad) {
  2161. chip->write_buf(mtd, oob, chip->ecc.postpad);
  2162. oob += chip->ecc.postpad;
  2163. }
  2164. }
  2165. size = mtd->oobsize - (oob - chip->oob_poi);
  2166. if (size)
  2167. chip->write_buf(mtd, oob, size);
  2168. return 0;
  2169. }
  2170. /**
  2171. * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function
  2172. * @mtd: mtd info structure
  2173. * @chip: nand chip info structure
  2174. * @buf: data buffer
  2175. * @oob_required: must write chip->oob_poi to OOB
  2176. * @page: page number to write
  2177. */
  2178. static int nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
  2179. const uint8_t *buf, int oob_required,
  2180. int page)
  2181. {
  2182. int i, eccsize = chip->ecc.size, ret;
  2183. int eccbytes = chip->ecc.bytes;
  2184. int eccsteps = chip->ecc.steps;
  2185. uint8_t *ecc_calc = chip->buffers->ecccalc;
  2186. const uint8_t *p = buf;
  2187. /* Software ECC calculation */
  2188. for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
  2189. chip->ecc.calculate(mtd, p, &ecc_calc[i]);
  2190. ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
  2191. chip->ecc.total);
  2192. if (ret)
  2193. return ret;
  2194. return chip->ecc.write_page_raw(mtd, chip, buf, 1, page);
  2195. }
  2196. /**
  2197. * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function
  2198. * @mtd: mtd info structure
  2199. * @chip: nand chip info structure
  2200. * @buf: data buffer
  2201. * @oob_required: must write chip->oob_poi to OOB
  2202. * @page: page number to write
  2203. */
  2204. static int nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
  2205. const uint8_t *buf, int oob_required,
  2206. int page)
  2207. {
  2208. int i, eccsize = chip->ecc.size, ret;
  2209. int eccbytes = chip->ecc.bytes;
  2210. int eccsteps = chip->ecc.steps;
  2211. uint8_t *ecc_calc = chip->buffers->ecccalc;
  2212. const uint8_t *p = buf;
  2213. for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
  2214. chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
  2215. chip->write_buf(mtd, p, eccsize);
  2216. chip->ecc.calculate(mtd, p, &ecc_calc[i]);
  2217. }
  2218. ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
  2219. chip->ecc.total);
  2220. if (ret)
  2221. return ret;
  2222. chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
  2223. return 0;
  2224. }
  2225. /**
  2226. * nand_write_subpage_hwecc - [REPLACEABLE] hardware ECC based subpage write
  2227. * @mtd: mtd info structure
  2228. * @chip: nand chip info structure
  2229. * @offset: column address of subpage within the page
  2230. * @data_len: data length
  2231. * @buf: data buffer
  2232. * @oob_required: must write chip->oob_poi to OOB
  2233. * @page: page number to write
  2234. */
  2235. static int nand_write_subpage_hwecc(struct mtd_info *mtd,
  2236. struct nand_chip *chip, uint32_t offset,
  2237. uint32_t data_len, const uint8_t *buf,
  2238. int oob_required, int page)
  2239. {
  2240. uint8_t *oob_buf = chip->oob_poi;
  2241. uint8_t *ecc_calc = chip->buffers->ecccalc;
  2242. int ecc_size = chip->ecc.size;
  2243. int ecc_bytes = chip->ecc.bytes;
  2244. int ecc_steps = chip->ecc.steps;
  2245. uint32_t start_step = offset / ecc_size;
  2246. uint32_t end_step = (offset + data_len - 1) / ecc_size;
  2247. int oob_bytes = mtd->oobsize / ecc_steps;
  2248. int step, ret;
  2249. for (step = 0; step < ecc_steps; step++) {
  2250. /* configure controller for WRITE access */
  2251. chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
  2252. /* write data (untouched subpages already masked by 0xFF) */
  2253. chip->write_buf(mtd, buf, ecc_size);
  2254. /* mask ECC of un-touched subpages by padding 0xFF */
  2255. if ((step < start_step) || (step > end_step))
  2256. memset(ecc_calc, 0xff, ecc_bytes);
  2257. else
  2258. chip->ecc.calculate(mtd, buf, ecc_calc);
  2259. /* mask OOB of un-touched subpages by padding 0xFF */
  2260. /* if oob_required, preserve OOB metadata of written subpage */
  2261. if (!oob_required || (step < start_step) || (step > end_step))
  2262. memset(oob_buf, 0xff, oob_bytes);
  2263. buf += ecc_size;
  2264. ecc_calc += ecc_bytes;
  2265. oob_buf += oob_bytes;
  2266. }
  2267. /* copy calculated ECC for whole page to chip->buffer->oob */
  2268. /* this include masked-value(0xFF) for unwritten subpages */
  2269. ecc_calc = chip->buffers->ecccalc;
  2270. ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
  2271. chip->ecc.total);
  2272. if (ret)
  2273. return ret;
  2274. /* write OOB buffer to NAND device */
  2275. chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
  2276. return 0;
  2277. }
  2278. /**
  2279. * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write
  2280. * @mtd: mtd info structure
  2281. * @chip: nand chip info structure
  2282. * @buf: data buffer
  2283. * @oob_required: must write chip->oob_poi to OOB
  2284. * @page: page number to write
  2285. *
  2286. * The hw generator calculates the error syndrome automatically. Therefore we
  2287. * need a special oob layout and handling.
  2288. */
  2289. static int nand_write_page_syndrome(struct mtd_info *mtd,
  2290. struct nand_chip *chip,
  2291. const uint8_t *buf, int oob_required,
  2292. int page)
  2293. {
  2294. int i, eccsize = chip->ecc.size;
  2295. int eccbytes = chip->ecc.bytes;
  2296. int eccsteps = chip->ecc.steps;
  2297. const uint8_t *p = buf;
  2298. uint8_t *oob = chip->oob_poi;
  2299. for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
  2300. chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
  2301. chip->write_buf(mtd, p, eccsize);
  2302. if (chip->ecc.prepad) {
  2303. chip->write_buf(mtd, oob, chip->ecc.prepad);
  2304. oob += chip->ecc.prepad;
  2305. }
  2306. chip->ecc.calculate(mtd, p, oob);
  2307. chip->write_buf(mtd, oob, eccbytes);
  2308. oob += eccbytes;
  2309. if (chip->ecc.postpad) {
  2310. chip->write_buf(mtd, oob, chip->ecc.postpad);
  2311. oob += chip->ecc.postpad;
  2312. }
  2313. }
  2314. /* Calculate remaining oob bytes */
  2315. i = mtd->oobsize - (oob - chip->oob_poi);
  2316. if (i)
  2317. chip->write_buf(mtd, oob, i);
  2318. return 0;
  2319. }
  2320. /**
  2321. * nand_write_page - [REPLACEABLE] write one page
  2322. * @mtd: MTD device structure
  2323. * @chip: NAND chip descriptor
  2324. * @offset: address offset within the page
  2325. * @data_len: length of actual data to be written
  2326. * @buf: the data to write
  2327. * @oob_required: must write chip->oob_poi to OOB
  2328. * @page: page number to write
  2329. * @cached: cached programming
  2330. * @raw: use _raw version of write_page
  2331. */
  2332. static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
  2333. uint32_t offset, int data_len, const uint8_t *buf,
  2334. int oob_required, int page, int cached, int raw)
  2335. {
  2336. int status, subpage;
  2337. if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
  2338. chip->ecc.write_subpage)
  2339. subpage = offset || (data_len < mtd->writesize);
  2340. else
  2341. subpage = 0;
  2342. chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
  2343. if (unlikely(raw))
  2344. status = chip->ecc.write_page_raw(mtd, chip, buf,
  2345. oob_required, page);
  2346. else if (subpage)
  2347. status = chip->ecc.write_subpage(mtd, chip, offset, data_len,
  2348. buf, oob_required, page);
  2349. else
  2350. status = chip->ecc.write_page(mtd, chip, buf, oob_required,
  2351. page);
  2352. if (status < 0)
  2353. return status;
  2354. /*
  2355. * Cached progamming disabled for now. Not sure if it's worth the
  2356. * trouble. The speed gain is not very impressive. (2.3->2.6Mib/s).
  2357. */
  2358. cached = 0;
  2359. if (!cached || !NAND_HAS_CACHEPROG(chip)) {
  2360. chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
  2361. status = chip->waitfunc(mtd, chip);
  2362. /*
  2363. * See if operation failed and additional status checks are
  2364. * available.
  2365. */
  2366. if ((status & NAND_STATUS_FAIL) && (chip->errstat))
  2367. status = chip->errstat(mtd, chip, FL_WRITING, status,
  2368. page);
  2369. if (status & NAND_STATUS_FAIL)
  2370. return -EIO;
  2371. } else {
  2372. chip->cmdfunc(mtd, NAND_CMD_CACHEDPROG, -1, -1);
  2373. status = chip->waitfunc(mtd, chip);
  2374. }
  2375. return 0;
  2376. }
  2377. /**
  2378. * nand_fill_oob - [INTERN] Transfer client buffer to oob
  2379. * @mtd: MTD device structure
  2380. * @oob: oob data buffer
  2381. * @len: oob data write length
  2382. * @ops: oob ops structure
  2383. */
  2384. static uint8_t *nand_fill_oob(struct mtd_info *mtd, uint8_t *oob, size_t len,
  2385. struct mtd_oob_ops *ops)
  2386. {
  2387. struct nand_chip *chip = mtd_to_nand(mtd);
  2388. int ret;
  2389. /*
  2390. * Initialise to all 0xFF, to avoid the possibility of left over OOB
  2391. * data from a previous OOB read.
  2392. */
  2393. memset(chip->oob_poi, 0xff, mtd->oobsize);
  2394. switch (ops->mode) {
  2395. case MTD_OPS_PLACE_OOB:
  2396. case MTD_OPS_RAW:
  2397. memcpy(chip->oob_poi + ops->ooboffs, oob, len);
  2398. return oob + len;
  2399. case MTD_OPS_AUTO_OOB:
  2400. ret = mtd_ooblayout_set_databytes(mtd, oob, chip->oob_poi,
  2401. ops->ooboffs, len);
  2402. BUG_ON(ret);
  2403. return oob + len;
  2404. default:
  2405. BUG();
  2406. }
  2407. return NULL;
  2408. }
  2409. #define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0)
  2410. /**
  2411. * nand_do_write_ops - [INTERN] NAND write with ECC
  2412. * @mtd: MTD device structure
  2413. * @to: offset to write to
  2414. * @ops: oob operations description structure
  2415. *
  2416. * NAND write with ECC.
  2417. */
  2418. static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
  2419. struct mtd_oob_ops *ops)
  2420. {
  2421. int chipnr, realpage, page, blockmask, column;
  2422. struct nand_chip *chip = mtd_to_nand(mtd);
  2423. uint32_t writelen = ops->len;
  2424. uint32_t oobwritelen = ops->ooblen;
  2425. uint32_t oobmaxlen = mtd_oobavail(mtd, ops);
  2426. uint8_t *oob = ops->oobbuf;
  2427. uint8_t *buf = ops->datbuf;
  2428. int ret;
  2429. int oob_required = oob ? 1 : 0;
  2430. ops->retlen = 0;
  2431. if (!writelen)
  2432. return 0;
  2433. /* Reject writes, which are not page aligned */
  2434. if (NOTALIGNED(to) || NOTALIGNED(ops->len)) {
  2435. pr_notice("%s: attempt to write non page aligned data\n",
  2436. __func__);
  2437. return -EINVAL;
  2438. }
  2439. column = to & (mtd->writesize - 1);
  2440. chipnr = (int)(to >> chip->chip_shift);
  2441. chip->select_chip(mtd, chipnr);
  2442. /* Check, if it is write protected */
  2443. if (nand_check_wp(mtd)) {
  2444. ret = -EIO;
  2445. goto err_out;
  2446. }
  2447. realpage = (int)(to >> chip->page_shift);
  2448. page = realpage & chip->pagemask;
  2449. blockmask = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
  2450. /* Invalidate the page cache, when we write to the cached page */
  2451. if (to <= ((loff_t)chip->pagebuf << chip->page_shift) &&
  2452. ((loff_t)chip->pagebuf << chip->page_shift) < (to + ops->len))
  2453. chip->pagebuf = -1;
  2454. /* Don't allow multipage oob writes with offset */
  2455. if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen)) {
  2456. ret = -EINVAL;
  2457. goto err_out;
  2458. }
  2459. while (1) {
  2460. int bytes = mtd->writesize;
  2461. int cached = writelen > bytes && page != blockmask;
  2462. uint8_t *wbuf = buf;
  2463. int use_bufpoi;
  2464. int part_pagewr = (column || writelen < mtd->writesize);
  2465. if (part_pagewr)
  2466. use_bufpoi = 1;
  2467. else if (chip->options & NAND_USE_BOUNCE_BUFFER)
  2468. use_bufpoi = !virt_addr_valid(buf);
  2469. else
  2470. use_bufpoi = 0;
  2471. /* Partial page write?, or need to use bounce buffer */
  2472. if (use_bufpoi) {
  2473. pr_debug("%s: using write bounce buffer for buf@%p\n",
  2474. __func__, buf);
  2475. cached = 0;
  2476. if (part_pagewr)
  2477. bytes = min_t(int, bytes - column, writelen);
  2478. chip->pagebuf = -1;
  2479. memset(chip->buffers->databuf, 0xff, mtd->writesize);
  2480. memcpy(&chip->buffers->databuf[column], buf, bytes);
  2481. wbuf = chip->buffers->databuf;
  2482. }
  2483. if (unlikely(oob)) {
  2484. size_t len = min(oobwritelen, oobmaxlen);
  2485. oob = nand_fill_oob(mtd, oob, len, ops);
  2486. oobwritelen -= len;
  2487. } else {
  2488. /* We still need to erase leftover OOB data */
  2489. memset(chip->oob_poi, 0xff, mtd->oobsize);
  2490. }
  2491. ret = chip->write_page(mtd, chip, column, bytes, wbuf,
  2492. oob_required, page, cached,
  2493. (ops->mode == MTD_OPS_RAW));
  2494. if (ret)
  2495. break;
  2496. writelen -= bytes;
  2497. if (!writelen)
  2498. break;
  2499. column = 0;
  2500. buf += bytes;
  2501. realpage++;
  2502. page = realpage & chip->pagemask;
  2503. /* Check, if we cross a chip boundary */
  2504. if (!page) {
  2505. chipnr++;
  2506. chip->select_chip(mtd, -1);
  2507. chip->select_chip(mtd, chipnr);
  2508. }
  2509. }
  2510. ops->retlen = ops->len - writelen;
  2511. if (unlikely(oob))
  2512. ops->oobretlen = ops->ooblen;
  2513. err_out:
  2514. chip->select_chip(mtd, -1);
  2515. return ret;
  2516. }
  2517. /**
  2518. * panic_nand_write - [MTD Interface] NAND write with ECC
  2519. * @mtd: MTD device structure
  2520. * @to: offset to write to
  2521. * @len: number of bytes to write
  2522. * @retlen: pointer to variable to store the number of written bytes
  2523. * @buf: the data to write
  2524. *
  2525. * NAND write with ECC. Used when performing writes in interrupt context, this
  2526. * may for example be called by mtdoops when writing an oops while in panic.
  2527. */
  2528. static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
  2529. size_t *retlen, const uint8_t *buf)
  2530. {
  2531. struct nand_chip *chip = mtd_to_nand(mtd);
  2532. struct mtd_oob_ops ops;
  2533. int ret;
  2534. /* Wait for the device to get ready */
  2535. panic_nand_wait(mtd, chip, 400);
  2536. /* Grab the device */
  2537. panic_nand_get_device(chip, mtd, FL_WRITING);
  2538. memset(&ops, 0, sizeof(ops));
  2539. ops.len = len;
  2540. ops.datbuf = (uint8_t *)buf;
  2541. ops.mode = MTD_OPS_PLACE_OOB;
  2542. ret = nand_do_write_ops(mtd, to, &ops);
  2543. *retlen = ops.retlen;
  2544. return ret;
  2545. }
  2546. /**
  2547. * nand_write - [MTD Interface] NAND write with ECC
  2548. * @mtd: MTD device structure
  2549. * @to: offset to write to
  2550. * @len: number of bytes to write
  2551. * @retlen: pointer to variable to store the number of written bytes
  2552. * @buf: the data to write
  2553. *
  2554. * NAND write with ECC.
  2555. */
  2556. static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
  2557. size_t *retlen, const uint8_t *buf)
  2558. {
  2559. struct mtd_oob_ops ops;
  2560. int ret;
  2561. nand_get_device(mtd, FL_WRITING);
  2562. memset(&ops, 0, sizeof(ops));
  2563. ops.len = len;
  2564. ops.datbuf = (uint8_t *)buf;
  2565. ops.mode = MTD_OPS_PLACE_OOB;
  2566. ret = nand_do_write_ops(mtd, to, &ops);
  2567. *retlen = ops.retlen;
  2568. nand_release_device(mtd);
  2569. return ret;
  2570. }
  2571. /**
  2572. * nand_do_write_oob - [MTD Interface] NAND write out-of-band
  2573. * @mtd: MTD device structure
  2574. * @to: offset to write to
  2575. * @ops: oob operation description structure
  2576. *
  2577. * NAND write out-of-band.
  2578. */
  2579. static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
  2580. struct mtd_oob_ops *ops)
  2581. {
  2582. int chipnr, page, status, len;
  2583. struct nand_chip *chip = mtd_to_nand(mtd);
  2584. pr_debug("%s: to = 0x%08x, len = %i\n",
  2585. __func__, (unsigned int)to, (int)ops->ooblen);
  2586. len = mtd_oobavail(mtd, ops);
  2587. /* Do not allow write past end of page */
  2588. if ((ops->ooboffs + ops->ooblen) > len) {
  2589. pr_debug("%s: attempt to write past end of page\n",
  2590. __func__);
  2591. return -EINVAL;
  2592. }
  2593. if (unlikely(ops->ooboffs >= len)) {
  2594. pr_debug("%s: attempt to start write outside oob\n",
  2595. __func__);
  2596. return -EINVAL;
  2597. }
  2598. /* Do not allow write past end of device */
  2599. if (unlikely(to >= mtd->size ||
  2600. ops->ooboffs + ops->ooblen >
  2601. ((mtd->size >> chip->page_shift) -
  2602. (to >> chip->page_shift)) * len)) {
  2603. pr_debug("%s: attempt to write beyond end of device\n",
  2604. __func__);
  2605. return -EINVAL;
  2606. }
  2607. chipnr = (int)(to >> chip->chip_shift);
  2608. /*
  2609. * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
  2610. * of my DiskOnChip 2000 test units) will clear the whole data page too
  2611. * if we don't do this. I have no clue why, but I seem to have 'fixed'
  2612. * it in the doc2000 driver in August 1999. dwmw2.
  2613. */
  2614. nand_reset(chip, chipnr);
  2615. chip->select_chip(mtd, chipnr);
  2616. /* Shift to get page */
  2617. page = (int)(to >> chip->page_shift);
  2618. /* Check, if it is write protected */
  2619. if (nand_check_wp(mtd)) {
  2620. chip->select_chip(mtd, -1);
  2621. return -EROFS;
  2622. }
  2623. /* Invalidate the page cache, if we write to the cached page */
  2624. if (page == chip->pagebuf)
  2625. chip->pagebuf = -1;
  2626. nand_fill_oob(mtd, ops->oobbuf, ops->ooblen, ops);
  2627. if (ops->mode == MTD_OPS_RAW)
  2628. status = chip->ecc.write_oob_raw(mtd, chip, page & chip->pagemask);
  2629. else
  2630. status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
  2631. chip->select_chip(mtd, -1);
  2632. if (status)
  2633. return status;
  2634. ops->oobretlen = ops->ooblen;
  2635. return 0;
  2636. }
  2637. /**
  2638. * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
  2639. * @mtd: MTD device structure
  2640. * @to: offset to write to
  2641. * @ops: oob operation description structure
  2642. */
  2643. static int nand_write_oob(struct mtd_info *mtd, loff_t to,
  2644. struct mtd_oob_ops *ops)
  2645. {
  2646. int ret = -ENOTSUPP;
  2647. ops->retlen = 0;
  2648. /* Do not allow writes past end of device */
  2649. if (ops->datbuf && (to + ops->len) > mtd->size) {
  2650. pr_debug("%s: attempt to write beyond end of device\n",
  2651. __func__);
  2652. return -EINVAL;
  2653. }
  2654. nand_get_device(mtd, FL_WRITING);
  2655. switch (ops->mode) {
  2656. case MTD_OPS_PLACE_OOB:
  2657. case MTD_OPS_AUTO_OOB:
  2658. case MTD_OPS_RAW:
  2659. break;
  2660. default:
  2661. goto out;
  2662. }
  2663. if (!ops->datbuf)
  2664. ret = nand_do_write_oob(mtd, to, ops);
  2665. else
  2666. ret = nand_do_write_ops(mtd, to, ops);
  2667. out:
  2668. nand_release_device(mtd);
  2669. return ret;
  2670. }
  2671. /**
  2672. * single_erase - [GENERIC] NAND standard block erase command function
  2673. * @mtd: MTD device structure
  2674. * @page: the page address of the block which will be erased
  2675. *
  2676. * Standard erase command for NAND chips. Returns NAND status.
  2677. */
  2678. static int single_erase(struct mtd_info *mtd, int page)
  2679. {
  2680. struct nand_chip *chip = mtd_to_nand(mtd);
  2681. /* Send commands to erase a block */
  2682. chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
  2683. chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
  2684. return chip->waitfunc(mtd, chip);
  2685. }
  2686. /**
  2687. * nand_erase - [MTD Interface] erase block(s)
  2688. * @mtd: MTD device structure
  2689. * @instr: erase instruction
  2690. *
  2691. * Erase one ore more blocks.
  2692. */
  2693. static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
  2694. {
  2695. return nand_erase_nand(mtd, instr, 0);
  2696. }
  2697. /**
  2698. * nand_erase_nand - [INTERN] erase block(s)
  2699. * @mtd: MTD device structure
  2700. * @instr: erase instruction
  2701. * @allowbbt: allow erasing the bbt area
  2702. *
  2703. * Erase one ore more blocks.
  2704. */
  2705. int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
  2706. int allowbbt)
  2707. {
  2708. int page, status, pages_per_block, ret, chipnr;
  2709. struct nand_chip *chip = mtd_to_nand(mtd);
  2710. loff_t len;
  2711. pr_debug("%s: start = 0x%012llx, len = %llu\n",
  2712. __func__, (unsigned long long)instr->addr,
  2713. (unsigned long long)instr->len);
  2714. if (check_offs_len(mtd, instr->addr, instr->len))
  2715. return -EINVAL;
  2716. /* Grab the lock and see if the device is available */
  2717. nand_get_device(mtd, FL_ERASING);
  2718. /* Shift to get first page */
  2719. page = (int)(instr->addr >> chip->page_shift);
  2720. chipnr = (int)(instr->addr >> chip->chip_shift);
  2721. /* Calculate pages in each block */
  2722. pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
  2723. /* Select the NAND device */
  2724. chip->select_chip(mtd, chipnr);
  2725. /* Check, if it is write protected */
  2726. if (nand_check_wp(mtd)) {
  2727. pr_debug("%s: device is write protected!\n",
  2728. __func__);
  2729. instr->state = MTD_ERASE_FAILED;
  2730. goto erase_exit;
  2731. }
  2732. /* Loop through the pages */
  2733. len = instr->len;
  2734. instr->state = MTD_ERASING;
  2735. while (len) {
  2736. /* Check if we have a bad block, we do not erase bad blocks! */
  2737. if (nand_block_checkbad(mtd, ((loff_t) page) <<
  2738. chip->page_shift, allowbbt)) {
  2739. pr_warn("%s: attempt to erase a bad block at page 0x%08x\n",
  2740. __func__, page);
  2741. instr->state = MTD_ERASE_FAILED;
  2742. goto erase_exit;
  2743. }
  2744. /*
  2745. * Invalidate the page cache, if we erase the block which
  2746. * contains the current cached page.
  2747. */
  2748. if (page <= chip->pagebuf && chip->pagebuf <
  2749. (page + pages_per_block))
  2750. chip->pagebuf = -1;
  2751. status = chip->erase(mtd, page & chip->pagemask);
  2752. /*
  2753. * See if operation failed and additional status checks are
  2754. * available
  2755. */
  2756. if ((status & NAND_STATUS_FAIL) && (chip->errstat))
  2757. status = chip->errstat(mtd, chip, FL_ERASING,
  2758. status, page);
  2759. /* See if block erase succeeded */
  2760. if (status & NAND_STATUS_FAIL) {
  2761. pr_debug("%s: failed erase, page 0x%08x\n",
  2762. __func__, page);
  2763. instr->state = MTD_ERASE_FAILED;
  2764. instr->fail_addr =
  2765. ((loff_t)page << chip->page_shift);
  2766. goto erase_exit;
  2767. }
  2768. /* Increment page address and decrement length */
  2769. len -= (1ULL << chip->phys_erase_shift);
  2770. page += pages_per_block;
  2771. /* Check, if we cross a chip boundary */
  2772. if (len && !(page & chip->pagemask)) {
  2773. chipnr++;
  2774. chip->select_chip(mtd, -1);
  2775. chip->select_chip(mtd, chipnr);
  2776. }
  2777. }
  2778. instr->state = MTD_ERASE_DONE;
  2779. erase_exit:
  2780. ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
  2781. /* Deselect and wake up anyone waiting on the device */
  2782. chip->select_chip(mtd, -1);
  2783. nand_release_device(mtd);
  2784. /* Do call back function */
  2785. if (!ret)
  2786. mtd_erase_callback(instr);
  2787. /* Return more or less happy */
  2788. return ret;
  2789. }
  2790. /**
  2791. * nand_sync - [MTD Interface] sync
  2792. * @mtd: MTD device structure
  2793. *
  2794. * Sync is actually a wait for chip ready function.
  2795. */
  2796. static void nand_sync(struct mtd_info *mtd)
  2797. {
  2798. pr_debug("%s: called\n", __func__);
  2799. /* Grab the lock and see if the device is available */
  2800. nand_get_device(mtd, FL_SYNCING);
  2801. /* Release it and go back */
  2802. nand_release_device(mtd);
  2803. }
  2804. /**
  2805. * nand_block_isbad - [MTD Interface] Check if block at offset is bad
  2806. * @mtd: MTD device structure
  2807. * @offs: offset relative to mtd start
  2808. */
  2809. static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
  2810. {
  2811. struct nand_chip *chip = mtd_to_nand(mtd);
  2812. int chipnr = (int)(offs >> chip->chip_shift);
  2813. int ret;
  2814. /* Select the NAND device */
  2815. nand_get_device(mtd, FL_READING);
  2816. chip->select_chip(mtd, chipnr);
  2817. ret = nand_block_checkbad(mtd, offs, 0);
  2818. chip->select_chip(mtd, -1);
  2819. nand_release_device(mtd);
  2820. return ret;
  2821. }
  2822. /**
  2823. * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
  2824. * @mtd: MTD device structure
  2825. * @ofs: offset relative to mtd start
  2826. */
  2827. static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
  2828. {
  2829. int ret;
  2830. ret = nand_block_isbad(mtd, ofs);
  2831. if (ret) {
  2832. /* If it was bad already, return success and do nothing */
  2833. if (ret > 0)
  2834. return 0;
  2835. return ret;
  2836. }
  2837. return nand_block_markbad_lowlevel(mtd, ofs);
  2838. }
  2839. /**
  2840. * nand_onfi_set_features- [REPLACEABLE] set features for ONFI nand
  2841. * @mtd: MTD device structure
  2842. * @chip: nand chip info structure
  2843. * @addr: feature address.
  2844. * @subfeature_param: the subfeature parameters, a four bytes array.
  2845. */
  2846. static int nand_onfi_set_features(struct mtd_info *mtd, struct nand_chip *chip,
  2847. int addr, uint8_t *subfeature_param)
  2848. {
  2849. int status;
  2850. int i;
  2851. if (!chip->onfi_version ||
  2852. !(le16_to_cpu(chip->onfi_params.opt_cmd)
  2853. & ONFI_OPT_CMD_SET_GET_FEATURES))
  2854. return -EINVAL;
  2855. chip->cmdfunc(mtd, NAND_CMD_SET_FEATURES, addr, -1);
  2856. for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
  2857. chip->write_byte(mtd, subfeature_param[i]);
  2858. status = chip->waitfunc(mtd, chip);
  2859. if (status & NAND_STATUS_FAIL)
  2860. return -EIO;
  2861. return 0;
  2862. }
  2863. /**
  2864. * nand_onfi_get_features- [REPLACEABLE] get features for ONFI nand
  2865. * @mtd: MTD device structure
  2866. * @chip: nand chip info structure
  2867. * @addr: feature address.
  2868. * @subfeature_param: the subfeature parameters, a four bytes array.
  2869. */
  2870. static int nand_onfi_get_features(struct mtd_info *mtd, struct nand_chip *chip,
  2871. int addr, uint8_t *subfeature_param)
  2872. {
  2873. int i;
  2874. if (!chip->onfi_version ||
  2875. !(le16_to_cpu(chip->onfi_params.opt_cmd)
  2876. & ONFI_OPT_CMD_SET_GET_FEATURES))
  2877. return -EINVAL;
  2878. chip->cmdfunc(mtd, NAND_CMD_GET_FEATURES, addr, -1);
  2879. for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
  2880. *subfeature_param++ = chip->read_byte(mtd);
  2881. return 0;
  2882. }
  2883. /**
  2884. * nand_suspend - [MTD Interface] Suspend the NAND flash
  2885. * @mtd: MTD device structure
  2886. */
  2887. static int nand_suspend(struct mtd_info *mtd)
  2888. {
  2889. return nand_get_device(mtd, FL_PM_SUSPENDED);
  2890. }
  2891. /**
  2892. * nand_resume - [MTD Interface] Resume the NAND flash
  2893. * @mtd: MTD device structure
  2894. */
  2895. static void nand_resume(struct mtd_info *mtd)
  2896. {
  2897. struct nand_chip *chip = mtd_to_nand(mtd);
  2898. if (chip->state == FL_PM_SUSPENDED)
  2899. nand_release_device(mtd);
  2900. else
  2901. pr_err("%s called for a chip which is not in suspended state\n",
  2902. __func__);
  2903. }
  2904. /**
  2905. * nand_shutdown - [MTD Interface] Finish the current NAND operation and
  2906. * prevent further operations
  2907. * @mtd: MTD device structure
  2908. */
  2909. static void nand_shutdown(struct mtd_info *mtd)
  2910. {
  2911. nand_get_device(mtd, FL_PM_SUSPENDED);
  2912. }
  2913. /* Set default functions */
  2914. static void nand_set_defaults(struct nand_chip *chip, int busw)
  2915. {
  2916. /* check for proper chip_delay setup, set 20us if not */
  2917. if (!chip->chip_delay)
  2918. chip->chip_delay = 20;
  2919. /* check, if a user supplied command function given */
  2920. if (chip->cmdfunc == NULL)
  2921. chip->cmdfunc = nand_command;
  2922. /* check, if a user supplied wait function given */
  2923. if (chip->waitfunc == NULL)
  2924. chip->waitfunc = nand_wait;
  2925. if (!chip->select_chip)
  2926. chip->select_chip = nand_select_chip;
  2927. /* set for ONFI nand */
  2928. if (!chip->onfi_set_features)
  2929. chip->onfi_set_features = nand_onfi_set_features;
  2930. if (!chip->onfi_get_features)
  2931. chip->onfi_get_features = nand_onfi_get_features;
  2932. /* If called twice, pointers that depend on busw may need to be reset */
  2933. if (!chip->read_byte || chip->read_byte == nand_read_byte)
  2934. chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
  2935. if (!chip->read_word)
  2936. chip->read_word = nand_read_word;
  2937. if (!chip->block_bad)
  2938. chip->block_bad = nand_block_bad;
  2939. if (!chip->block_markbad)
  2940. chip->block_markbad = nand_default_block_markbad;
  2941. if (!chip->write_buf || chip->write_buf == nand_write_buf)
  2942. chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
  2943. if (!chip->write_byte || chip->write_byte == nand_write_byte)
  2944. chip->write_byte = busw ? nand_write_byte16 : nand_write_byte;
  2945. if (!chip->read_buf || chip->read_buf == nand_read_buf)
  2946. chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
  2947. if (!chip->scan_bbt)
  2948. chip->scan_bbt = nand_default_bbt;
  2949. if (!chip->controller) {
  2950. chip->controller = &chip->hwcontrol;
  2951. nand_hw_control_init(chip->controller);
  2952. }
  2953. }
  2954. /* Sanitize ONFI strings so we can safely print them */
  2955. static void sanitize_string(uint8_t *s, size_t len)
  2956. {
  2957. ssize_t i;
  2958. /* Null terminate */
  2959. s[len - 1] = 0;
  2960. /* Remove non printable chars */
  2961. for (i = 0; i < len - 1; i++) {
  2962. if (s[i] < ' ' || s[i] > 127)
  2963. s[i] = '?';
  2964. }
  2965. /* Remove trailing spaces */
  2966. strim(s);
  2967. }
  2968. static u16 onfi_crc16(u16 crc, u8 const *p, size_t len)
  2969. {
  2970. int i;
  2971. while (len--) {
  2972. crc ^= *p++ << 8;
  2973. for (i = 0; i < 8; i++)
  2974. crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0);
  2975. }
  2976. return crc;
  2977. }
  2978. /* Parse the Extended Parameter Page. */
  2979. static int nand_flash_detect_ext_param_page(struct mtd_info *mtd,
  2980. struct nand_chip *chip, struct nand_onfi_params *p)
  2981. {
  2982. struct onfi_ext_param_page *ep;
  2983. struct onfi_ext_section *s;
  2984. struct onfi_ext_ecc_info *ecc;
  2985. uint8_t *cursor;
  2986. int ret = -EINVAL;
  2987. int len;
  2988. int i;
  2989. len = le16_to_cpu(p->ext_param_page_length) * 16;
  2990. ep = kmalloc(len, GFP_KERNEL);
  2991. if (!ep)
  2992. return -ENOMEM;
  2993. /* Send our own NAND_CMD_PARAM. */
  2994. chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
  2995. /* Use the Change Read Column command to skip the ONFI param pages. */
  2996. chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
  2997. sizeof(*p) * p->num_of_param_pages , -1);
  2998. /* Read out the Extended Parameter Page. */
  2999. chip->read_buf(mtd, (uint8_t *)ep, len);
  3000. if ((onfi_crc16(ONFI_CRC_BASE, ((uint8_t *)ep) + 2, len - 2)
  3001. != le16_to_cpu(ep->crc))) {
  3002. pr_debug("fail in the CRC.\n");
  3003. goto ext_out;
  3004. }
  3005. /*
  3006. * Check the signature.
  3007. * Do not strictly follow the ONFI spec, maybe changed in future.
  3008. */
  3009. if (strncmp(ep->sig, "EPPS", 4)) {
  3010. pr_debug("The signature is invalid.\n");
  3011. goto ext_out;
  3012. }
  3013. /* find the ECC section. */
  3014. cursor = (uint8_t *)(ep + 1);
  3015. for (i = 0; i < ONFI_EXT_SECTION_MAX; i++) {
  3016. s = ep->sections + i;
  3017. if (s->type == ONFI_SECTION_TYPE_2)
  3018. break;
  3019. cursor += s->length * 16;
  3020. }
  3021. if (i == ONFI_EXT_SECTION_MAX) {
  3022. pr_debug("We can not find the ECC section.\n");
  3023. goto ext_out;
  3024. }
  3025. /* get the info we want. */
  3026. ecc = (struct onfi_ext_ecc_info *)cursor;
  3027. if (!ecc->codeword_size) {
  3028. pr_debug("Invalid codeword size\n");
  3029. goto ext_out;
  3030. }
  3031. chip->ecc_strength_ds = ecc->ecc_bits;
  3032. chip->ecc_step_ds = 1 << ecc->codeword_size;
  3033. ret = 0;
  3034. ext_out:
  3035. kfree(ep);
  3036. return ret;
  3037. }
  3038. static int nand_setup_read_retry_micron(struct mtd_info *mtd, int retry_mode)
  3039. {
  3040. struct nand_chip *chip = mtd_to_nand(mtd);
  3041. uint8_t feature[ONFI_SUBFEATURE_PARAM_LEN] = {retry_mode};
  3042. return chip->onfi_set_features(mtd, chip, ONFI_FEATURE_ADDR_READ_RETRY,
  3043. feature);
  3044. }
  3045. /*
  3046. * Configure chip properties from Micron vendor-specific ONFI table
  3047. */
  3048. static void nand_onfi_detect_micron(struct nand_chip *chip,
  3049. struct nand_onfi_params *p)
  3050. {
  3051. struct nand_onfi_vendor_micron *micron = (void *)p->vendor;
  3052. if (le16_to_cpu(p->vendor_revision) < 1)
  3053. return;
  3054. chip->read_retries = micron->read_retry_options;
  3055. chip->setup_read_retry = nand_setup_read_retry_micron;
  3056. }
  3057. /*
  3058. * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise.
  3059. */
  3060. static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip,
  3061. int *busw)
  3062. {
  3063. struct nand_onfi_params *p = &chip->onfi_params;
  3064. int i, j;
  3065. int val;
  3066. /* Try ONFI for unknown chip or LP */
  3067. chip->cmdfunc(mtd, NAND_CMD_READID, 0x20, -1);
  3068. if (chip->read_byte(mtd) != 'O' || chip->read_byte(mtd) != 'N' ||
  3069. chip->read_byte(mtd) != 'F' || chip->read_byte(mtd) != 'I')
  3070. return 0;
  3071. chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
  3072. for (i = 0; i < 3; i++) {
  3073. for (j = 0; j < sizeof(*p); j++)
  3074. ((uint8_t *)p)[j] = chip->read_byte(mtd);
  3075. if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 254) ==
  3076. le16_to_cpu(p->crc)) {
  3077. break;
  3078. }
  3079. }
  3080. if (i == 3) {
  3081. pr_err("Could not find valid ONFI parameter page; aborting\n");
  3082. return 0;
  3083. }
  3084. /* Check version */
  3085. val = le16_to_cpu(p->revision);
  3086. if (val & (1 << 5))
  3087. chip->onfi_version = 23;
  3088. else if (val & (1 << 4))
  3089. chip->onfi_version = 22;
  3090. else if (val & (1 << 3))
  3091. chip->onfi_version = 21;
  3092. else if (val & (1 << 2))
  3093. chip->onfi_version = 20;
  3094. else if (val & (1 << 1))
  3095. chip->onfi_version = 10;
  3096. if (!chip->onfi_version) {
  3097. pr_info("unsupported ONFI version: %d\n", val);
  3098. return 0;
  3099. }
  3100. sanitize_string(p->manufacturer, sizeof(p->manufacturer));
  3101. sanitize_string(p->model, sizeof(p->model));
  3102. if (!mtd->name)
  3103. mtd->name = p->model;
  3104. mtd->writesize = le32_to_cpu(p->byte_per_page);
  3105. /*
  3106. * pages_per_block and blocks_per_lun may not be a power-of-2 size
  3107. * (don't ask me who thought of this...). MTD assumes that these
  3108. * dimensions will be power-of-2, so just truncate the remaining area.
  3109. */
  3110. mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
  3111. mtd->erasesize *= mtd->writesize;
  3112. mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
  3113. /* See erasesize comment */
  3114. chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
  3115. chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
  3116. chip->bits_per_cell = p->bits_per_cell;
  3117. if (onfi_feature(chip) & ONFI_FEATURE_16_BIT_BUS)
  3118. *busw = NAND_BUSWIDTH_16;
  3119. else
  3120. *busw = 0;
  3121. if (p->ecc_bits != 0xff) {
  3122. chip->ecc_strength_ds = p->ecc_bits;
  3123. chip->ecc_step_ds = 512;
  3124. } else if (chip->onfi_version >= 21 &&
  3125. (onfi_feature(chip) & ONFI_FEATURE_EXT_PARAM_PAGE)) {
  3126. /*
  3127. * The nand_flash_detect_ext_param_page() uses the
  3128. * Change Read Column command which maybe not supported
  3129. * by the chip->cmdfunc. So try to update the chip->cmdfunc
  3130. * now. We do not replace user supplied command function.
  3131. */
  3132. if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
  3133. chip->cmdfunc = nand_command_lp;
  3134. /* The Extended Parameter Page is supported since ONFI 2.1. */
  3135. if (nand_flash_detect_ext_param_page(mtd, chip, p))
  3136. pr_warn("Failed to detect ONFI extended param page\n");
  3137. } else {
  3138. pr_warn("Could not retrieve ONFI ECC requirements\n");
  3139. }
  3140. if (p->jedec_id == NAND_MFR_MICRON)
  3141. nand_onfi_detect_micron(chip, p);
  3142. return 1;
  3143. }
  3144. /*
  3145. * Check if the NAND chip is JEDEC compliant, returns 1 if it is, 0 otherwise.
  3146. */
  3147. static int nand_flash_detect_jedec(struct mtd_info *mtd, struct nand_chip *chip,
  3148. int *busw)
  3149. {
  3150. struct nand_jedec_params *p = &chip->jedec_params;
  3151. struct jedec_ecc_info *ecc;
  3152. int val;
  3153. int i, j;
  3154. /* Try JEDEC for unknown chip or LP */
  3155. chip->cmdfunc(mtd, NAND_CMD_READID, 0x40, -1);
  3156. if (chip->read_byte(mtd) != 'J' || chip->read_byte(mtd) != 'E' ||
  3157. chip->read_byte(mtd) != 'D' || chip->read_byte(mtd) != 'E' ||
  3158. chip->read_byte(mtd) != 'C')
  3159. return 0;
  3160. chip->cmdfunc(mtd, NAND_CMD_PARAM, 0x40, -1);
  3161. for (i = 0; i < 3; i++) {
  3162. for (j = 0; j < sizeof(*p); j++)
  3163. ((uint8_t *)p)[j] = chip->read_byte(mtd);
  3164. if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 510) ==
  3165. le16_to_cpu(p->crc))
  3166. break;
  3167. }
  3168. if (i == 3) {
  3169. pr_err("Could not find valid JEDEC parameter page; aborting\n");
  3170. return 0;
  3171. }
  3172. /* Check version */
  3173. val = le16_to_cpu(p->revision);
  3174. if (val & (1 << 2))
  3175. chip->jedec_version = 10;
  3176. else if (val & (1 << 1))
  3177. chip->jedec_version = 1; /* vendor specific version */
  3178. if (!chip->jedec_version) {
  3179. pr_info("unsupported JEDEC version: %d\n", val);
  3180. return 0;
  3181. }
  3182. sanitize_string(p->manufacturer, sizeof(p->manufacturer));
  3183. sanitize_string(p->model, sizeof(p->model));
  3184. if (!mtd->name)
  3185. mtd->name = p->model;
  3186. mtd->writesize = le32_to_cpu(p->byte_per_page);
  3187. /* Please reference to the comment for nand_flash_detect_onfi. */
  3188. mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
  3189. mtd->erasesize *= mtd->writesize;
  3190. mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
  3191. /* Please reference to the comment for nand_flash_detect_onfi. */
  3192. chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
  3193. chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
  3194. chip->bits_per_cell = p->bits_per_cell;
  3195. if (jedec_feature(chip) & JEDEC_FEATURE_16_BIT_BUS)
  3196. *busw = NAND_BUSWIDTH_16;
  3197. else
  3198. *busw = 0;
  3199. /* ECC info */
  3200. ecc = &p->ecc_info[0];
  3201. if (ecc->codeword_size >= 9) {
  3202. chip->ecc_strength_ds = ecc->ecc_bits;
  3203. chip->ecc_step_ds = 1 << ecc->codeword_size;
  3204. } else {
  3205. pr_warn("Invalid codeword size\n");
  3206. }
  3207. return 1;
  3208. }
  3209. /*
  3210. * nand_id_has_period - Check if an ID string has a given wraparound period
  3211. * @id_data: the ID string
  3212. * @arrlen: the length of the @id_data array
  3213. * @period: the period of repitition
  3214. *
  3215. * Check if an ID string is repeated within a given sequence of bytes at
  3216. * specific repetition interval period (e.g., {0x20,0x01,0x7F,0x20} has a
  3217. * period of 3). This is a helper function for nand_id_len(). Returns non-zero
  3218. * if the repetition has a period of @period; otherwise, returns zero.
  3219. */
  3220. static int nand_id_has_period(u8 *id_data, int arrlen, int period)
  3221. {
  3222. int i, j;
  3223. for (i = 0; i < period; i++)
  3224. for (j = i + period; j < arrlen; j += period)
  3225. if (id_data[i] != id_data[j])
  3226. return 0;
  3227. return 1;
  3228. }
  3229. /*
  3230. * nand_id_len - Get the length of an ID string returned by CMD_READID
  3231. * @id_data: the ID string
  3232. * @arrlen: the length of the @id_data array
  3233. * Returns the length of the ID string, according to known wraparound/trailing
  3234. * zero patterns. If no pattern exists, returns the length of the array.
  3235. */
  3236. static int nand_id_len(u8 *id_data, int arrlen)
  3237. {
  3238. int last_nonzero, period;
  3239. /* Find last non-zero byte */
  3240. for (last_nonzero = arrlen - 1; last_nonzero >= 0; last_nonzero--)
  3241. if (id_data[last_nonzero])
  3242. break;
  3243. /* All zeros */
  3244. if (last_nonzero < 0)
  3245. return 0;
  3246. /* Calculate wraparound period */
  3247. for (period = 1; period < arrlen; period++)
  3248. if (nand_id_has_period(id_data, arrlen, period))
  3249. break;
  3250. /* There's a repeated pattern */
  3251. if (period < arrlen)
  3252. return period;
  3253. /* There are trailing zeros */
  3254. if (last_nonzero < arrlen - 1)
  3255. return last_nonzero + 1;
  3256. /* No pattern detected */
  3257. return arrlen;
  3258. }
  3259. /* Extract the bits of per cell from the 3rd byte of the extended ID */
  3260. static int nand_get_bits_per_cell(u8 cellinfo)
  3261. {
  3262. int bits;
  3263. bits = cellinfo & NAND_CI_CELLTYPE_MSK;
  3264. bits >>= NAND_CI_CELLTYPE_SHIFT;
  3265. return bits + 1;
  3266. }
  3267. /*
  3268. * Many new NAND share similar device ID codes, which represent the size of the
  3269. * chip. The rest of the parameters must be decoded according to generic or
  3270. * manufacturer-specific "extended ID" decoding patterns.
  3271. */
  3272. static void nand_decode_ext_id(struct mtd_info *mtd, struct nand_chip *chip,
  3273. u8 id_data[8], int *busw)
  3274. {
  3275. int extid, id_len;
  3276. /* The 3rd id byte holds MLC / multichip data */
  3277. chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
  3278. /* The 4th id byte is the important one */
  3279. extid = id_data[3];
  3280. id_len = nand_id_len(id_data, 8);
  3281. /*
  3282. * Field definitions are in the following datasheets:
  3283. * Old style (4,5 byte ID): Samsung K9GAG08U0M (p.32)
  3284. * New Samsung (6 byte ID): Samsung K9GAG08U0F (p.44)
  3285. * Hynix MLC (6 byte ID): Hynix H27UBG8T2B (p.22)
  3286. *
  3287. * Check for ID length, non-zero 6th byte, cell type, and Hynix/Samsung
  3288. * ID to decide what to do.
  3289. */
  3290. if (id_len == 6 && id_data[0] == NAND_MFR_SAMSUNG &&
  3291. !nand_is_slc(chip) && id_data[5] != 0x00) {
  3292. /* Calc pagesize */
  3293. mtd->writesize = 2048 << (extid & 0x03);
  3294. extid >>= 2;
  3295. /* Calc oobsize */
  3296. switch (((extid >> 2) & 0x04) | (extid & 0x03)) {
  3297. case 1:
  3298. mtd->oobsize = 128;
  3299. break;
  3300. case 2:
  3301. mtd->oobsize = 218;
  3302. break;
  3303. case 3:
  3304. mtd->oobsize = 400;
  3305. break;
  3306. case 4:
  3307. mtd->oobsize = 436;
  3308. break;
  3309. case 5:
  3310. mtd->oobsize = 512;
  3311. break;
  3312. case 6:
  3313. mtd->oobsize = 640;
  3314. break;
  3315. case 7:
  3316. default: /* Other cases are "reserved" (unknown) */
  3317. mtd->oobsize = 1024;
  3318. break;
  3319. }
  3320. extid >>= 2;
  3321. /* Calc blocksize */
  3322. mtd->erasesize = (128 * 1024) <<
  3323. (((extid >> 1) & 0x04) | (extid & 0x03));
  3324. *busw = 0;
  3325. } else if (id_len == 6 && id_data[0] == NAND_MFR_HYNIX &&
  3326. !nand_is_slc(chip)) {
  3327. unsigned int tmp;
  3328. /* Calc pagesize */
  3329. mtd->writesize = 2048 << (extid & 0x03);
  3330. extid >>= 2;
  3331. /* Calc oobsize */
  3332. switch (((extid >> 2) & 0x04) | (extid & 0x03)) {
  3333. case 0:
  3334. mtd->oobsize = 128;
  3335. break;
  3336. case 1:
  3337. mtd->oobsize = 224;
  3338. break;
  3339. case 2:
  3340. mtd->oobsize = 448;
  3341. break;
  3342. case 3:
  3343. mtd->oobsize = 64;
  3344. break;
  3345. case 4:
  3346. mtd->oobsize = 32;
  3347. break;
  3348. case 5:
  3349. mtd->oobsize = 16;
  3350. break;
  3351. default:
  3352. mtd->oobsize = 640;
  3353. break;
  3354. }
  3355. extid >>= 2;
  3356. /* Calc blocksize */
  3357. tmp = ((extid >> 1) & 0x04) | (extid & 0x03);
  3358. if (tmp < 0x03)
  3359. mtd->erasesize = (128 * 1024) << tmp;
  3360. else if (tmp == 0x03)
  3361. mtd->erasesize = 768 * 1024;
  3362. else
  3363. mtd->erasesize = (64 * 1024) << tmp;
  3364. *busw = 0;
  3365. } else {
  3366. /* Calc pagesize */
  3367. mtd->writesize = 1024 << (extid & 0x03);
  3368. extid >>= 2;
  3369. /* Calc oobsize */
  3370. mtd->oobsize = (8 << (extid & 0x01)) *
  3371. (mtd->writesize >> 9);
  3372. extid >>= 2;
  3373. /* Calc blocksize. Blocksize is multiples of 64KiB */
  3374. mtd->erasesize = (64 * 1024) << (extid & 0x03);
  3375. extid >>= 2;
  3376. /* Get buswidth information */
  3377. *busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0;
  3378. /*
  3379. * Toshiba 24nm raw SLC (i.e., not BENAND) have 32B OOB per
  3380. * 512B page. For Toshiba SLC, we decode the 5th/6th byte as
  3381. * follows:
  3382. * - ID byte 6, bits[2:0]: 100b -> 43nm, 101b -> 32nm,
  3383. * 110b -> 24nm
  3384. * - ID byte 5, bit[7]: 1 -> BENAND, 0 -> raw SLC
  3385. */
  3386. if (id_len >= 6 && id_data[0] == NAND_MFR_TOSHIBA &&
  3387. nand_is_slc(chip) &&
  3388. (id_data[5] & 0x7) == 0x6 /* 24nm */ &&
  3389. !(id_data[4] & 0x80) /* !BENAND */) {
  3390. mtd->oobsize = 32 * mtd->writesize >> 9;
  3391. }
  3392. }
  3393. }
  3394. /*
  3395. * Old devices have chip data hardcoded in the device ID table. nand_decode_id
  3396. * decodes a matching ID table entry and assigns the MTD size parameters for
  3397. * the chip.
  3398. */
  3399. static void nand_decode_id(struct mtd_info *mtd, struct nand_chip *chip,
  3400. struct nand_flash_dev *type, u8 id_data[8],
  3401. int *busw)
  3402. {
  3403. int maf_id = id_data[0];
  3404. mtd->erasesize = type->erasesize;
  3405. mtd->writesize = type->pagesize;
  3406. mtd->oobsize = mtd->writesize / 32;
  3407. *busw = type->options & NAND_BUSWIDTH_16;
  3408. /* All legacy ID NAND are small-page, SLC */
  3409. chip->bits_per_cell = 1;
  3410. /*
  3411. * Check for Spansion/AMD ID + repeating 5th, 6th byte since
  3412. * some Spansion chips have erasesize that conflicts with size
  3413. * listed in nand_ids table.
  3414. * Data sheet (5 byte ID): Spansion S30ML-P ORNAND (p.39)
  3415. */
  3416. if (maf_id == NAND_MFR_AMD && id_data[4] != 0x00 && id_data[5] == 0x00
  3417. && id_data[6] == 0x00 && id_data[7] == 0x00
  3418. && mtd->writesize == 512) {
  3419. mtd->erasesize = 128 * 1024;
  3420. mtd->erasesize <<= ((id_data[3] & 0x03) << 1);
  3421. }
  3422. }
  3423. /*
  3424. * Set the bad block marker/indicator (BBM/BBI) patterns according to some
  3425. * heuristic patterns using various detected parameters (e.g., manufacturer,
  3426. * page size, cell-type information).
  3427. */
  3428. static void nand_decode_bbm_options(struct mtd_info *mtd,
  3429. struct nand_chip *chip, u8 id_data[8])
  3430. {
  3431. int maf_id = id_data[0];
  3432. /* Set the bad block position */
  3433. if (mtd->writesize > 512 || (chip->options & NAND_BUSWIDTH_16))
  3434. chip->badblockpos = NAND_LARGE_BADBLOCK_POS;
  3435. else
  3436. chip->badblockpos = NAND_SMALL_BADBLOCK_POS;
  3437. /*
  3438. * Bad block marker is stored in the last page of each block on Samsung
  3439. * and Hynix MLC devices; stored in first two pages of each block on
  3440. * Micron devices with 2KiB pages and on SLC Samsung, Hynix, Toshiba,
  3441. * AMD/Spansion, and Macronix. All others scan only the first page.
  3442. */
  3443. if (!nand_is_slc(chip) &&
  3444. (maf_id == NAND_MFR_SAMSUNG ||
  3445. maf_id == NAND_MFR_HYNIX))
  3446. chip->bbt_options |= NAND_BBT_SCANLASTPAGE;
  3447. else if ((nand_is_slc(chip) &&
  3448. (maf_id == NAND_MFR_SAMSUNG ||
  3449. maf_id == NAND_MFR_HYNIX ||
  3450. maf_id == NAND_MFR_TOSHIBA ||
  3451. maf_id == NAND_MFR_AMD ||
  3452. maf_id == NAND_MFR_MACRONIX)) ||
  3453. (mtd->writesize == 2048 &&
  3454. maf_id == NAND_MFR_MICRON))
  3455. chip->bbt_options |= NAND_BBT_SCAN2NDPAGE;
  3456. }
  3457. static inline bool is_full_id_nand(struct nand_flash_dev *type)
  3458. {
  3459. return type->id_len;
  3460. }
  3461. static bool find_full_id_nand(struct mtd_info *mtd, struct nand_chip *chip,
  3462. struct nand_flash_dev *type, u8 *id_data, int *busw)
  3463. {
  3464. if (!strncmp(type->id, id_data, type->id_len)) {
  3465. mtd->writesize = type->pagesize;
  3466. mtd->erasesize = type->erasesize;
  3467. mtd->oobsize = type->oobsize;
  3468. chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
  3469. chip->chipsize = (uint64_t)type->chipsize << 20;
  3470. chip->options |= type->options;
  3471. chip->ecc_strength_ds = NAND_ECC_STRENGTH(type);
  3472. chip->ecc_step_ds = NAND_ECC_STEP(type);
  3473. chip->onfi_timing_mode_default =
  3474. type->onfi_timing_mode_default;
  3475. *busw = type->options & NAND_BUSWIDTH_16;
  3476. if (!mtd->name)
  3477. mtd->name = type->name;
  3478. return true;
  3479. }
  3480. return false;
  3481. }
  3482. /*
  3483. * Get the flash and manufacturer id and lookup if the type is supported.
  3484. */
  3485. static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
  3486. struct nand_chip *chip,
  3487. int *maf_id, int *dev_id,
  3488. struct nand_flash_dev *type)
  3489. {
  3490. int busw;
  3491. int i, maf_idx;
  3492. u8 id_data[8];
  3493. /*
  3494. * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
  3495. * after power-up.
  3496. */
  3497. nand_reset(chip, 0);
  3498. /* Select the device */
  3499. chip->select_chip(mtd, 0);
  3500. /* Send the command for reading device ID */
  3501. chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
  3502. /* Read manufacturer and device IDs */
  3503. *maf_id = chip->read_byte(mtd);
  3504. *dev_id = chip->read_byte(mtd);
  3505. /*
  3506. * Try again to make sure, as some systems the bus-hold or other
  3507. * interface concerns can cause random data which looks like a
  3508. * possibly credible NAND flash to appear. If the two results do
  3509. * not match, ignore the device completely.
  3510. */
  3511. chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
  3512. /* Read entire ID string */
  3513. for (i = 0; i < 8; i++)
  3514. id_data[i] = chip->read_byte(mtd);
  3515. if (id_data[0] != *maf_id || id_data[1] != *dev_id) {
  3516. pr_info("second ID read did not match %02x,%02x against %02x,%02x\n",
  3517. *maf_id, *dev_id, id_data[0], id_data[1]);
  3518. return ERR_PTR(-ENODEV);
  3519. }
  3520. if (!type)
  3521. type = nand_flash_ids;
  3522. for (; type->name != NULL; type++) {
  3523. if (is_full_id_nand(type)) {
  3524. if (find_full_id_nand(mtd, chip, type, id_data, &busw))
  3525. goto ident_done;
  3526. } else if (*dev_id == type->dev_id) {
  3527. break;
  3528. }
  3529. }
  3530. chip->onfi_version = 0;
  3531. if (!type->name || !type->pagesize) {
  3532. /* Check if the chip is ONFI compliant */
  3533. if (nand_flash_detect_onfi(mtd, chip, &busw))
  3534. goto ident_done;
  3535. /* Check if the chip is JEDEC compliant */
  3536. if (nand_flash_detect_jedec(mtd, chip, &busw))
  3537. goto ident_done;
  3538. }
  3539. if (!type->name)
  3540. return ERR_PTR(-ENODEV);
  3541. if (!mtd->name)
  3542. mtd->name = type->name;
  3543. chip->chipsize = (uint64_t)type->chipsize << 20;
  3544. if (!type->pagesize) {
  3545. /* Decode parameters from extended ID */
  3546. nand_decode_ext_id(mtd, chip, id_data, &busw);
  3547. } else {
  3548. nand_decode_id(mtd, chip, type, id_data, &busw);
  3549. }
  3550. /* Get chip options */
  3551. chip->options |= type->options;
  3552. /*
  3553. * Check if chip is not a Samsung device. Do not clear the
  3554. * options for chips which do not have an extended id.
  3555. */
  3556. if (*maf_id != NAND_MFR_SAMSUNG && !type->pagesize)
  3557. chip->options &= ~NAND_SAMSUNG_LP_OPTIONS;
  3558. ident_done:
  3559. /* Try to identify manufacturer */
  3560. for (maf_idx = 0; nand_manuf_ids[maf_idx].id != 0x0; maf_idx++) {
  3561. if (nand_manuf_ids[maf_idx].id == *maf_id)
  3562. break;
  3563. }
  3564. if (chip->options & NAND_BUSWIDTH_AUTO) {
  3565. WARN_ON(chip->options & NAND_BUSWIDTH_16);
  3566. chip->options |= busw;
  3567. nand_set_defaults(chip, busw);
  3568. } else if (busw != (chip->options & NAND_BUSWIDTH_16)) {
  3569. /*
  3570. * Check, if buswidth is correct. Hardware drivers should set
  3571. * chip correct!
  3572. */
  3573. pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
  3574. *maf_id, *dev_id);
  3575. pr_info("%s %s\n", nand_manuf_ids[maf_idx].name, mtd->name);
  3576. pr_warn("bus width %d instead %d bit\n",
  3577. (chip->options & NAND_BUSWIDTH_16) ? 16 : 8,
  3578. busw ? 16 : 8);
  3579. return ERR_PTR(-EINVAL);
  3580. }
  3581. nand_decode_bbm_options(mtd, chip, id_data);
  3582. /* Calculate the address shift from the page size */
  3583. chip->page_shift = ffs(mtd->writesize) - 1;
  3584. /* Convert chipsize to number of pages per chip -1 */
  3585. chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
  3586. chip->bbt_erase_shift = chip->phys_erase_shift =
  3587. ffs(mtd->erasesize) - 1;
  3588. if (chip->chipsize & 0xffffffff)
  3589. chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
  3590. else {
  3591. chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32));
  3592. chip->chip_shift += 32 - 1;
  3593. }
  3594. chip->badblockbits = 8;
  3595. chip->erase = single_erase;
  3596. /* Do not replace user supplied command function! */
  3597. if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
  3598. chip->cmdfunc = nand_command_lp;
  3599. pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
  3600. *maf_id, *dev_id);
  3601. if (chip->onfi_version)
  3602. pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
  3603. chip->onfi_params.model);
  3604. else if (chip->jedec_version)
  3605. pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
  3606. chip->jedec_params.model);
  3607. else
  3608. pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
  3609. type->name);
  3610. pr_info("%d MiB, %s, erase size: %d KiB, page size: %d, OOB size: %d\n",
  3611. (int)(chip->chipsize >> 20), nand_is_slc(chip) ? "SLC" : "MLC",
  3612. mtd->erasesize >> 10, mtd->writesize, mtd->oobsize);
  3613. return type;
  3614. }
  3615. static const char * const nand_ecc_modes[] = {
  3616. [NAND_ECC_NONE] = "none",
  3617. [NAND_ECC_SOFT] = "soft",
  3618. [NAND_ECC_HW] = "hw",
  3619. [NAND_ECC_HW_SYNDROME] = "hw_syndrome",
  3620. [NAND_ECC_HW_OOB_FIRST] = "hw_oob_first",
  3621. };
  3622. static int of_get_nand_ecc_mode(struct device_node *np)
  3623. {
  3624. const char *pm;
  3625. int err, i;
  3626. err = of_property_read_string(np, "nand-ecc-mode", &pm);
  3627. if (err < 0)
  3628. return err;
  3629. for (i = 0; i < ARRAY_SIZE(nand_ecc_modes); i++)
  3630. if (!strcasecmp(pm, nand_ecc_modes[i]))
  3631. return i;
  3632. /*
  3633. * For backward compatibility we support few obsoleted values that don't
  3634. * have their mappings into nand_ecc_modes_t anymore (they were merged
  3635. * with other enums).
  3636. */
  3637. if (!strcasecmp(pm, "soft_bch"))
  3638. return NAND_ECC_SOFT;
  3639. return -ENODEV;
  3640. }
  3641. static const char * const nand_ecc_algos[] = {
  3642. [NAND_ECC_HAMMING] = "hamming",
  3643. [NAND_ECC_BCH] = "bch",
  3644. };
  3645. static int of_get_nand_ecc_algo(struct device_node *np)
  3646. {
  3647. const char *pm;
  3648. int err, i;
  3649. err = of_property_read_string(np, "nand-ecc-algo", &pm);
  3650. if (!err) {
  3651. for (i = NAND_ECC_HAMMING; i < ARRAY_SIZE(nand_ecc_algos); i++)
  3652. if (!strcasecmp(pm, nand_ecc_algos[i]))
  3653. return i;
  3654. return -ENODEV;
  3655. }
  3656. /*
  3657. * For backward compatibility we also read "nand-ecc-mode" checking
  3658. * for some obsoleted values that were specifying ECC algorithm.
  3659. */
  3660. err = of_property_read_string(np, "nand-ecc-mode", &pm);
  3661. if (err < 0)
  3662. return err;
  3663. if (!strcasecmp(pm, "soft"))
  3664. return NAND_ECC_HAMMING;
  3665. else if (!strcasecmp(pm, "soft_bch"))
  3666. return NAND_ECC_BCH;
  3667. return -ENODEV;
  3668. }
  3669. static int of_get_nand_ecc_step_size(struct device_node *np)
  3670. {
  3671. int ret;
  3672. u32 val;
  3673. ret = of_property_read_u32(np, "nand-ecc-step-size", &val);
  3674. return ret ? ret : val;
  3675. }
  3676. static int of_get_nand_ecc_strength(struct device_node *np)
  3677. {
  3678. int ret;
  3679. u32 val;
  3680. ret = of_property_read_u32(np, "nand-ecc-strength", &val);
  3681. return ret ? ret : val;
  3682. }
  3683. static int of_get_nand_bus_width(struct device_node *np)
  3684. {
  3685. u32 val;
  3686. if (of_property_read_u32(np, "nand-bus-width", &val))
  3687. return 8;
  3688. switch (val) {
  3689. case 8:
  3690. case 16:
  3691. return val;
  3692. default:
  3693. return -EIO;
  3694. }
  3695. }
  3696. static bool of_get_nand_on_flash_bbt(struct device_node *np)
  3697. {
  3698. return of_property_read_bool(np, "nand-on-flash-bbt");
  3699. }
  3700. static int nand_dt_init(struct nand_chip *chip)
  3701. {
  3702. struct device_node *dn = nand_get_flash_node(chip);
  3703. int ecc_mode, ecc_algo, ecc_strength, ecc_step;
  3704. if (!dn)
  3705. return 0;
  3706. if (of_get_nand_bus_width(dn) == 16)
  3707. chip->options |= NAND_BUSWIDTH_16;
  3708. if (of_get_nand_on_flash_bbt(dn))
  3709. chip->bbt_options |= NAND_BBT_USE_FLASH;
  3710. ecc_mode = of_get_nand_ecc_mode(dn);
  3711. ecc_algo = of_get_nand_ecc_algo(dn);
  3712. ecc_strength = of_get_nand_ecc_strength(dn);
  3713. ecc_step = of_get_nand_ecc_step_size(dn);
  3714. if ((ecc_step >= 0 && !(ecc_strength >= 0)) ||
  3715. (!(ecc_step >= 0) && ecc_strength >= 0)) {
  3716. pr_err("must set both strength and step size in DT\n");
  3717. return -EINVAL;
  3718. }
  3719. if (ecc_mode >= 0)
  3720. chip->ecc.mode = ecc_mode;
  3721. if (ecc_algo >= 0)
  3722. chip->ecc.algo = ecc_algo;
  3723. if (ecc_strength >= 0)
  3724. chip->ecc.strength = ecc_strength;
  3725. if (ecc_step > 0)
  3726. chip->ecc.size = ecc_step;
  3727. if (of_property_read_bool(dn, "nand-ecc-maximize"))
  3728. chip->ecc.options |= NAND_ECC_MAXIMIZE;
  3729. return 0;
  3730. }
  3731. /**
  3732. * nand_scan_ident - [NAND Interface] Scan for the NAND device
  3733. * @mtd: MTD device structure
  3734. * @maxchips: number of chips to scan for
  3735. * @table: alternative NAND ID table
  3736. *
  3737. * This is the first phase of the normal nand_scan() function. It reads the
  3738. * flash ID and sets up MTD fields accordingly.
  3739. *
  3740. */
  3741. int nand_scan_ident(struct mtd_info *mtd, int maxchips,
  3742. struct nand_flash_dev *table)
  3743. {
  3744. int i, nand_maf_id, nand_dev_id;
  3745. struct nand_chip *chip = mtd_to_nand(mtd);
  3746. struct nand_flash_dev *type;
  3747. int ret;
  3748. ret = nand_dt_init(chip);
  3749. if (ret)
  3750. return ret;
  3751. if (!mtd->name && mtd->dev.parent)
  3752. mtd->name = dev_name(mtd->dev.parent);
  3753. if ((!chip->cmdfunc || !chip->select_chip) && !chip->cmd_ctrl) {
  3754. /*
  3755. * Default functions assigned for chip_select() and
  3756. * cmdfunc() both expect cmd_ctrl() to be populated,
  3757. * so we need to check that that's the case
  3758. */
  3759. pr_err("chip.cmd_ctrl() callback is not provided");
  3760. return -EINVAL;
  3761. }
  3762. /* Set the default functions */
  3763. nand_set_defaults(chip, chip->options & NAND_BUSWIDTH_16);
  3764. /* Read the flash type */
  3765. type = nand_get_flash_type(mtd, chip, &nand_maf_id,
  3766. &nand_dev_id, table);
  3767. if (IS_ERR(type)) {
  3768. if (!(chip->options & NAND_SCAN_SILENT_NODEV))
  3769. pr_warn("No NAND device found\n");
  3770. chip->select_chip(mtd, -1);
  3771. return PTR_ERR(type);
  3772. }
  3773. /* Initialize the ->data_interface field. */
  3774. ret = nand_init_data_interface(chip);
  3775. if (ret)
  3776. return ret;
  3777. /*
  3778. * Setup the data interface correctly on the chip and controller side.
  3779. * This explicit call to nand_setup_data_interface() is only required
  3780. * for the first die, because nand_reset() has been called before
  3781. * ->data_interface and ->default_onfi_timing_mode were set.
  3782. * For the other dies, nand_reset() will automatically switch to the
  3783. * best mode for us.
  3784. */
  3785. ret = nand_setup_data_interface(chip);
  3786. if (ret)
  3787. return ret;
  3788. chip->select_chip(mtd, -1);
  3789. /* Check for a chip array */
  3790. for (i = 1; i < maxchips; i++) {
  3791. /* See comment in nand_get_flash_type for reset */
  3792. nand_reset(chip, i);
  3793. chip->select_chip(mtd, i);
  3794. /* Send the command for reading device ID */
  3795. chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
  3796. /* Read manufacturer and device IDs */
  3797. if (nand_maf_id != chip->read_byte(mtd) ||
  3798. nand_dev_id != chip->read_byte(mtd)) {
  3799. chip->select_chip(mtd, -1);
  3800. break;
  3801. }
  3802. chip->select_chip(mtd, -1);
  3803. }
  3804. if (i > 1)
  3805. pr_info("%d chips detected\n", i);
  3806. /* Store the number of chips and calc total size for mtd */
  3807. chip->numchips = i;
  3808. mtd->size = i * chip->chipsize;
  3809. return 0;
  3810. }
  3811. EXPORT_SYMBOL(nand_scan_ident);
  3812. static int nand_set_ecc_soft_ops(struct mtd_info *mtd)
  3813. {
  3814. struct nand_chip *chip = mtd_to_nand(mtd);
  3815. struct nand_ecc_ctrl *ecc = &chip->ecc;
  3816. if (WARN_ON(ecc->mode != NAND_ECC_SOFT))
  3817. return -EINVAL;
  3818. switch (ecc->algo) {
  3819. case NAND_ECC_HAMMING:
  3820. ecc->calculate = nand_calculate_ecc;
  3821. ecc->correct = nand_correct_data;
  3822. ecc->read_page = nand_read_page_swecc;
  3823. ecc->read_subpage = nand_read_subpage;
  3824. ecc->write_page = nand_write_page_swecc;
  3825. ecc->read_page_raw = nand_read_page_raw;
  3826. ecc->write_page_raw = nand_write_page_raw;
  3827. ecc->read_oob = nand_read_oob_std;
  3828. ecc->write_oob = nand_write_oob_std;
  3829. if (!ecc->size)
  3830. ecc->size = 256;
  3831. ecc->bytes = 3;
  3832. ecc->strength = 1;
  3833. return 0;
  3834. case NAND_ECC_BCH:
  3835. if (!mtd_nand_has_bch()) {
  3836. WARN(1, "CONFIG_MTD_NAND_ECC_BCH not enabled\n");
  3837. return -EINVAL;
  3838. }
  3839. ecc->calculate = nand_bch_calculate_ecc;
  3840. ecc->correct = nand_bch_correct_data;
  3841. ecc->read_page = nand_read_page_swecc;
  3842. ecc->read_subpage = nand_read_subpage;
  3843. ecc->write_page = nand_write_page_swecc;
  3844. ecc->read_page_raw = nand_read_page_raw;
  3845. ecc->write_page_raw = nand_write_page_raw;
  3846. ecc->read_oob = nand_read_oob_std;
  3847. ecc->write_oob = nand_write_oob_std;
  3848. /*
  3849. * Board driver should supply ecc.size and ecc.strength
  3850. * values to select how many bits are correctable.
  3851. * Otherwise, default to 4 bits for large page devices.
  3852. */
  3853. if (!ecc->size && (mtd->oobsize >= 64)) {
  3854. ecc->size = 512;
  3855. ecc->strength = 4;
  3856. }
  3857. /*
  3858. * if no ecc placement scheme was provided pickup the default
  3859. * large page one.
  3860. */
  3861. if (!mtd->ooblayout) {
  3862. /* handle large page devices only */
  3863. if (mtd->oobsize < 64) {
  3864. WARN(1, "OOB layout is required when using software BCH on small pages\n");
  3865. return -EINVAL;
  3866. }
  3867. mtd_set_ooblayout(mtd, &nand_ooblayout_lp_ops);
  3868. }
  3869. /*
  3870. * We can only maximize ECC config when the default layout is
  3871. * used, otherwise we don't know how many bytes can really be
  3872. * used.
  3873. */
  3874. if (mtd->ooblayout == &nand_ooblayout_lp_ops &&
  3875. ecc->options & NAND_ECC_MAXIMIZE) {
  3876. int steps, bytes;
  3877. /* Always prefer 1k blocks over 512bytes ones */
  3878. ecc->size = 1024;
  3879. steps = mtd->writesize / ecc->size;
  3880. /* Reserve 2 bytes for the BBM */
  3881. bytes = (mtd->oobsize - 2) / steps;
  3882. ecc->strength = bytes * 8 / fls(8 * ecc->size);
  3883. }
  3884. /* See nand_bch_init() for details. */
  3885. ecc->bytes = 0;
  3886. ecc->priv = nand_bch_init(mtd);
  3887. if (!ecc->priv) {
  3888. WARN(1, "BCH ECC initialization failed!\n");
  3889. return -EINVAL;
  3890. }
  3891. return 0;
  3892. default:
  3893. WARN(1, "Unsupported ECC algorithm!\n");
  3894. return -EINVAL;
  3895. }
  3896. }
  3897. /*
  3898. * Check if the chip configuration meet the datasheet requirements.
  3899. * If our configuration corrects A bits per B bytes and the minimum
  3900. * required correction level is X bits per Y bytes, then we must ensure
  3901. * both of the following are true:
  3902. *
  3903. * (1) A / B >= X / Y
  3904. * (2) A >= X
  3905. *
  3906. * Requirement (1) ensures we can correct for the required bitflip density.
  3907. * Requirement (2) ensures we can correct even when all bitflips are clumped
  3908. * in the same sector.
  3909. */
  3910. static bool nand_ecc_strength_good(struct mtd_info *mtd)
  3911. {
  3912. struct nand_chip *chip = mtd_to_nand(mtd);
  3913. struct nand_ecc_ctrl *ecc = &chip->ecc;
  3914. int corr, ds_corr;
  3915. if (ecc->size == 0 || chip->ecc_step_ds == 0)
  3916. /* Not enough information */
  3917. return true;
  3918. /*
  3919. * We get the number of corrected bits per page to compare
  3920. * the correction density.
  3921. */
  3922. corr = (mtd->writesize * ecc->strength) / ecc->size;
  3923. ds_corr = (mtd->writesize * chip->ecc_strength_ds) / chip->ecc_step_ds;
  3924. return corr >= ds_corr && ecc->strength >= chip->ecc_strength_ds;
  3925. }
  3926. /**
  3927. * nand_scan_tail - [NAND Interface] Scan for the NAND device
  3928. * @mtd: MTD device structure
  3929. *
  3930. * This is the second phase of the normal nand_scan() function. It fills out
  3931. * all the uninitialized function pointers with the defaults and scans for a
  3932. * bad block table if appropriate.
  3933. */
  3934. int nand_scan_tail(struct mtd_info *mtd)
  3935. {
  3936. struct nand_chip *chip = mtd_to_nand(mtd);
  3937. struct nand_ecc_ctrl *ecc = &chip->ecc;
  3938. struct nand_buffers *nbuf;
  3939. int ret;
  3940. /* New bad blocks should be marked in OOB, flash-based BBT, or both */
  3941. if (WARN_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) &&
  3942. !(chip->bbt_options & NAND_BBT_USE_FLASH)))
  3943. return -EINVAL;
  3944. if (!(chip->options & NAND_OWN_BUFFERS)) {
  3945. nbuf = kzalloc(sizeof(*nbuf) + mtd->writesize
  3946. + mtd->oobsize * 3, GFP_KERNEL);
  3947. if (!nbuf)
  3948. return -ENOMEM;
  3949. nbuf->ecccalc = (uint8_t *)(nbuf + 1);
  3950. nbuf->ecccode = nbuf->ecccalc + mtd->oobsize;
  3951. nbuf->databuf = nbuf->ecccode + mtd->oobsize;
  3952. chip->buffers = nbuf;
  3953. } else {
  3954. if (!chip->buffers)
  3955. return -ENOMEM;
  3956. }
  3957. /* Set the internal oob buffer location, just after the page data */
  3958. chip->oob_poi = chip->buffers->databuf + mtd->writesize;
  3959. /*
  3960. * If no default placement scheme is given, select an appropriate one.
  3961. */
  3962. if (!mtd->ooblayout &&
  3963. !(ecc->mode == NAND_ECC_SOFT && ecc->algo == NAND_ECC_BCH)) {
  3964. switch (mtd->oobsize) {
  3965. case 8:
  3966. case 16:
  3967. mtd_set_ooblayout(mtd, &nand_ooblayout_sp_ops);
  3968. break;
  3969. case 64:
  3970. case 128:
  3971. mtd_set_ooblayout(mtd, &nand_ooblayout_lp_hamming_ops);
  3972. break;
  3973. default:
  3974. WARN(1, "No oob scheme defined for oobsize %d\n",
  3975. mtd->oobsize);
  3976. ret = -EINVAL;
  3977. goto err_free;
  3978. }
  3979. }
  3980. if (!chip->write_page)
  3981. chip->write_page = nand_write_page;
  3982. /*
  3983. * Check ECC mode, default to software if 3byte/512byte hardware ECC is
  3984. * selected and we have 256 byte pagesize fallback to software ECC
  3985. */
  3986. switch (ecc->mode) {
  3987. case NAND_ECC_HW_OOB_FIRST:
  3988. /* Similar to NAND_ECC_HW, but a separate read_page handle */
  3989. if (!ecc->calculate || !ecc->correct || !ecc->hwctl) {
  3990. WARN(1, "No ECC functions supplied; hardware ECC not possible\n");
  3991. ret = -EINVAL;
  3992. goto err_free;
  3993. }
  3994. if (!ecc->read_page)
  3995. ecc->read_page = nand_read_page_hwecc_oob_first;
  3996. case NAND_ECC_HW:
  3997. /* Use standard hwecc read page function? */
  3998. if (!ecc->read_page)
  3999. ecc->read_page = nand_read_page_hwecc;
  4000. if (!ecc->write_page)
  4001. ecc->write_page = nand_write_page_hwecc;
  4002. if (!ecc->read_page_raw)
  4003. ecc->read_page_raw = nand_read_page_raw;
  4004. if (!ecc->write_page_raw)
  4005. ecc->write_page_raw = nand_write_page_raw;
  4006. if (!ecc->read_oob)
  4007. ecc->read_oob = nand_read_oob_std;
  4008. if (!ecc->write_oob)
  4009. ecc->write_oob = nand_write_oob_std;
  4010. if (!ecc->read_subpage)
  4011. ecc->read_subpage = nand_read_subpage;
  4012. if (!ecc->write_subpage && ecc->hwctl && ecc->calculate)
  4013. ecc->write_subpage = nand_write_subpage_hwecc;
  4014. case NAND_ECC_HW_SYNDROME:
  4015. if ((!ecc->calculate || !ecc->correct || !ecc->hwctl) &&
  4016. (!ecc->read_page ||
  4017. ecc->read_page == nand_read_page_hwecc ||
  4018. !ecc->write_page ||
  4019. ecc->write_page == nand_write_page_hwecc)) {
  4020. WARN(1, "No ECC functions supplied; hardware ECC not possible\n");
  4021. ret = -EINVAL;
  4022. goto err_free;
  4023. }
  4024. /* Use standard syndrome read/write page function? */
  4025. if (!ecc->read_page)
  4026. ecc->read_page = nand_read_page_syndrome;
  4027. if (!ecc->write_page)
  4028. ecc->write_page = nand_write_page_syndrome;
  4029. if (!ecc->read_page_raw)
  4030. ecc->read_page_raw = nand_read_page_raw_syndrome;
  4031. if (!ecc->write_page_raw)
  4032. ecc->write_page_raw = nand_write_page_raw_syndrome;
  4033. if (!ecc->read_oob)
  4034. ecc->read_oob = nand_read_oob_syndrome;
  4035. if (!ecc->write_oob)
  4036. ecc->write_oob = nand_write_oob_syndrome;
  4037. if (mtd->writesize >= ecc->size) {
  4038. if (!ecc->strength) {
  4039. WARN(1, "Driver must set ecc.strength when using hardware ECC\n");
  4040. ret = -EINVAL;
  4041. goto err_free;
  4042. }
  4043. break;
  4044. }
  4045. pr_warn("%d byte HW ECC not possible on %d byte page size, fallback to SW ECC\n",
  4046. ecc->size, mtd->writesize);
  4047. ecc->mode = NAND_ECC_SOFT;
  4048. ecc->algo = NAND_ECC_HAMMING;
  4049. case NAND_ECC_SOFT:
  4050. ret = nand_set_ecc_soft_ops(mtd);
  4051. if (ret) {
  4052. ret = -EINVAL;
  4053. goto err_free;
  4054. }
  4055. break;
  4056. case NAND_ECC_NONE:
  4057. pr_warn("NAND_ECC_NONE selected by board driver. This is not recommended!\n");
  4058. ecc->read_page = nand_read_page_raw;
  4059. ecc->write_page = nand_write_page_raw;
  4060. ecc->read_oob = nand_read_oob_std;
  4061. ecc->read_page_raw = nand_read_page_raw;
  4062. ecc->write_page_raw = nand_write_page_raw;
  4063. ecc->write_oob = nand_write_oob_std;
  4064. ecc->size = mtd->writesize;
  4065. ecc->bytes = 0;
  4066. ecc->strength = 0;
  4067. break;
  4068. default:
  4069. WARN(1, "Invalid NAND_ECC_MODE %d\n", ecc->mode);
  4070. ret = -EINVAL;
  4071. goto err_free;
  4072. }
  4073. /* For many systems, the standard OOB write also works for raw */
  4074. if (!ecc->read_oob_raw)
  4075. ecc->read_oob_raw = ecc->read_oob;
  4076. if (!ecc->write_oob_raw)
  4077. ecc->write_oob_raw = ecc->write_oob;
  4078. /* propagate ecc info to mtd_info */
  4079. mtd->ecc_strength = ecc->strength;
  4080. mtd->ecc_step_size = ecc->size;
  4081. /*
  4082. * Set the number of read / write steps for one page depending on ECC
  4083. * mode.
  4084. */
  4085. ecc->steps = mtd->writesize / ecc->size;
  4086. if (ecc->steps * ecc->size != mtd->writesize) {
  4087. WARN(1, "Invalid ECC parameters\n");
  4088. ret = -EINVAL;
  4089. goto err_free;
  4090. }
  4091. ecc->total = ecc->steps * ecc->bytes;
  4092. /*
  4093. * The number of bytes available for a client to place data into
  4094. * the out of band area.
  4095. */
  4096. ret = mtd_ooblayout_count_freebytes(mtd);
  4097. if (ret < 0)
  4098. ret = 0;
  4099. mtd->oobavail = ret;
  4100. /* ECC sanity check: warn if it's too weak */
  4101. if (!nand_ecc_strength_good(mtd))
  4102. pr_warn("WARNING: %s: the ECC used on your system is too weak compared to the one required by the NAND chip\n",
  4103. mtd->name);
  4104. /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */
  4105. if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && nand_is_slc(chip)) {
  4106. switch (ecc->steps) {
  4107. case 2:
  4108. mtd->subpage_sft = 1;
  4109. break;
  4110. case 4:
  4111. case 8:
  4112. case 16:
  4113. mtd->subpage_sft = 2;
  4114. break;
  4115. }
  4116. }
  4117. chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
  4118. /* Initialize state */
  4119. chip->state = FL_READY;
  4120. /* Invalidate the pagebuffer reference */
  4121. chip->pagebuf = -1;
  4122. /* Large page NAND with SOFT_ECC should support subpage reads */
  4123. switch (ecc->mode) {
  4124. case NAND_ECC_SOFT:
  4125. if (chip->page_shift > 9)
  4126. chip->options |= NAND_SUBPAGE_READ;
  4127. break;
  4128. default:
  4129. break;
  4130. }
  4131. /* Fill in remaining MTD driver data */
  4132. mtd->type = nand_is_slc(chip) ? MTD_NANDFLASH : MTD_MLCNANDFLASH;
  4133. mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM :
  4134. MTD_CAP_NANDFLASH;
  4135. mtd->_erase = nand_erase;
  4136. mtd->_point = NULL;
  4137. mtd->_unpoint = NULL;
  4138. mtd->_read = nand_read;
  4139. mtd->_write = nand_write;
  4140. mtd->_panic_write = panic_nand_write;
  4141. mtd->_read_oob = nand_read_oob;
  4142. mtd->_write_oob = nand_write_oob;
  4143. mtd->_sync = nand_sync;
  4144. mtd->_lock = NULL;
  4145. mtd->_unlock = NULL;
  4146. mtd->_suspend = nand_suspend;
  4147. mtd->_resume = nand_resume;
  4148. mtd->_reboot = nand_shutdown;
  4149. mtd->_block_isreserved = nand_block_isreserved;
  4150. mtd->_block_isbad = nand_block_isbad;
  4151. mtd->_block_markbad = nand_block_markbad;
  4152. mtd->writebufsize = mtd->writesize;
  4153. /*
  4154. * Initialize bitflip_threshold to its default prior scan_bbt() call.
  4155. * scan_bbt() might invoke mtd_read(), thus bitflip_threshold must be
  4156. * properly set.
  4157. */
  4158. if (!mtd->bitflip_threshold)
  4159. mtd->bitflip_threshold = DIV_ROUND_UP(mtd->ecc_strength * 3, 4);
  4160. /* Check, if we should skip the bad block table scan */
  4161. if (chip->options & NAND_SKIP_BBTSCAN)
  4162. return 0;
  4163. /* Build bad block table */
  4164. return chip->scan_bbt(mtd);
  4165. err_free:
  4166. if (!(chip->options & NAND_OWN_BUFFERS))
  4167. kfree(chip->buffers);
  4168. return ret;
  4169. }
  4170. EXPORT_SYMBOL(nand_scan_tail);
  4171. /*
  4172. * is_module_text_address() isn't exported, and it's mostly a pointless
  4173. * test if this is a module _anyway_ -- they'd have to try _really_ hard
  4174. * to call us from in-kernel code if the core NAND support is modular.
  4175. */
  4176. #ifdef MODULE
  4177. #define caller_is_module() (1)
  4178. #else
  4179. #define caller_is_module() \
  4180. is_module_text_address((unsigned long)__builtin_return_address(0))
  4181. #endif
  4182. /**
  4183. * nand_scan - [NAND Interface] Scan for the NAND device
  4184. * @mtd: MTD device structure
  4185. * @maxchips: number of chips to scan for
  4186. *
  4187. * This fills out all the uninitialized function pointers with the defaults.
  4188. * The flash ID is read and the mtd/chip structures are filled with the
  4189. * appropriate values.
  4190. */
  4191. int nand_scan(struct mtd_info *mtd, int maxchips)
  4192. {
  4193. int ret;
  4194. ret = nand_scan_ident(mtd, maxchips, NULL);
  4195. if (!ret)
  4196. ret = nand_scan_tail(mtd);
  4197. return ret;
  4198. }
  4199. EXPORT_SYMBOL(nand_scan);
  4200. /**
  4201. * nand_cleanup - [NAND Interface] Free resources held by the NAND device
  4202. * @chip: NAND chip object
  4203. */
  4204. void nand_cleanup(struct nand_chip *chip)
  4205. {
  4206. if (chip->ecc.mode == NAND_ECC_SOFT &&
  4207. chip->ecc.algo == NAND_ECC_BCH)
  4208. nand_bch_free((struct nand_bch_control *)chip->ecc.priv);
  4209. nand_release_data_interface(chip);
  4210. /* Free bad block table memory */
  4211. kfree(chip->bbt);
  4212. if (!(chip->options & NAND_OWN_BUFFERS))
  4213. kfree(chip->buffers);
  4214. /* Free bad block descriptor memory */
  4215. if (chip->badblock_pattern && chip->badblock_pattern->options
  4216. & NAND_BBT_DYNAMICSTRUCT)
  4217. kfree(chip->badblock_pattern);
  4218. }
  4219. EXPORT_SYMBOL_GPL(nand_cleanup);
  4220. /**
  4221. * nand_release - [NAND Interface] Unregister the MTD device and free resources
  4222. * held by the NAND device
  4223. * @mtd: MTD device structure
  4224. */
  4225. void nand_release(struct mtd_info *mtd)
  4226. {
  4227. mtd_device_unregister(mtd);
  4228. nand_cleanup(mtd_to_nand(mtd));
  4229. }
  4230. EXPORT_SYMBOL_GPL(nand_release);
  4231. MODULE_LICENSE("GPL");
  4232. MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>");
  4233. MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>");
  4234. MODULE_DESCRIPTION("Generic NAND flash driver code");