nand_base.c 176 KB

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