nand_base.c 135 KB

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