nand_base.c 168 KB

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