nand_base.c 175 KB

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