nand_base.c 149 KB

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