nand_base.c 152 KB

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