0072-mtd-backport-v4.7-0day-patches-from-Boris.patch 157 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480
  1. From a369af5149e6eb442b22ce89b564dd7a76e03638 Mon Sep 17 00:00:00 2001
  2. From: John Crispin <[email protected]>
  3. Date: Tue, 26 Apr 2016 19:05:01 +0200
  4. Subject: [PATCH 072/102] mtd: backport v4.7-0day patches from Boris
  5. Signed-off-by: John Crispin <[email protected]>
  6. ---
  7. drivers/mtd/Kconfig | 4 +-
  8. drivers/mtd/cmdlinepart.c | 3 +-
  9. drivers/mtd/devices/m25p80.c | 44 +--
  10. drivers/mtd/maps/physmap_of.c | 6 +-
  11. drivers/mtd/mtdchar.c | 123 ++++++--
  12. drivers/mtd/mtdconcat.c | 2 +-
  13. drivers/mtd/mtdcore.c | 428 ++++++++++++++++++++++++--
  14. drivers/mtd/mtdcore.h | 7 +-
  15. drivers/mtd/mtdpart.c | 161 ++++++----
  16. drivers/mtd/mtdswap.c | 24 +-
  17. drivers/mtd/nand/Kconfig | 21 +-
  18. drivers/mtd/nand/Makefile | 2 +
  19. drivers/mtd/nand/nand_base.c | 571 +++++++++++++++++++----------------
  20. drivers/mtd/nand/nand_bbt.c | 34 +--
  21. drivers/mtd/nand/nand_bch.c | 52 ++--
  22. drivers/mtd/nand/nand_ecc.c | 6 +-
  23. drivers/mtd/nand/nand_ids.c | 4 +-
  24. drivers/mtd/nand/nandsim.c | 43 +--
  25. drivers/mtd/ofpart.c | 53 ++--
  26. drivers/mtd/spi-nor/Kconfig | 10 +-
  27. drivers/mtd/spi-nor/Makefile | 1 +
  28. drivers/mtd/spi-nor/mtk-quadspi.c | 485 +++++++++++++++++++++++++++++
  29. drivers/mtd/spi-nor/spi-nor.c | 321 +++++++++++++-------
  30. drivers/mtd/tests/mtd_nandecctest.c | 2 +-
  31. drivers/mtd/tests/oobtest.c | 49 ++-
  32. drivers/mtd/tests/pagetest.c | 3 +-
  33. drivers/mtd/ubi/cdev.c | 4 +-
  34. drivers/mtd/ubi/misc.c | 49 +++
  35. drivers/mtd/ubi/ubi.h | 16 +-
  36. drivers/mtd/ubi/upd.c | 2 +-
  37. drivers/mtd/ubi/wl.c | 21 +-
  38. include/linux/mtd/bbm.h | 1 -
  39. include/linux/mtd/fsmc.h | 18 --
  40. include/linux/mtd/inftl.h | 1 -
  41. include/linux/mtd/map.h | 9 +-
  42. include/linux/mtd/mtd.h | 80 ++++-
  43. include/linux/mtd/nand.h | 94 ++++--
  44. include/linux/mtd/nand_bch.h | 10 +-
  45. include/linux/mtd/nftl.h | 1 -
  46. include/linux/mtd/onenand.h | 2 -
  47. include/linux/mtd/partitions.h | 27 +-
  48. include/linux/mtd/sh_flctl.h | 4 +-
  49. include/linux/mtd/sharpsl.h | 2 +-
  50. include/linux/mtd/spi-nor.h | 23 +-
  51. include/uapi/mtd/mtd-abi.h | 2 +-
  52. 45 files changed, 2077 insertions(+), 748 deletions(-)
  53. create mode 100644 drivers/mtd/spi-nor/mtk-quadspi.c
  54. --- a/drivers/mtd/Kconfig
  55. +++ b/drivers/mtd/Kconfig
  56. @@ -131,7 +131,7 @@
  57. config MTD_AFS_PARTS
  58. tristate "ARM Firmware Suite partition parsing"
  59. - depends on ARM
  60. + depends on (ARM || ARM64)
  61. ---help---
  62. The ARM Firmware Suite allows the user to divide flash devices into
  63. multiple 'images'. Each such image has a header containing its name
  64. @@ -161,7 +161,7 @@
  65. config MTD_BCM63XX_PARTS
  66. tristate "BCM63XX CFE partitioning support"
  67. - depends on BCM63XX
  68. + depends on BCM63XX || BMIPS_GENERIC || COMPILE_TEST
  69. select CRC32
  70. help
  71. This provides partions parsing for BCM63xx devices with CFE
  72. --- a/drivers/mtd/cmdlinepart.c
  73. +++ b/drivers/mtd/cmdlinepart.c
  74. @@ -304,7 +304,7 @@
  75. * the first one in the chain if a NULL mtd_id is passed in.
  76. */
  77. static int parse_cmdline_partitions(struct mtd_info *master,
  78. - struct mtd_partition **pparts,
  79. + const struct mtd_partition **pparts,
  80. struct mtd_part_parser_data *data)
  81. {
  82. unsigned long long offset;
  83. @@ -382,7 +382,6 @@
  84. __setup("mtdparts=", mtdpart_setup);
  85. static struct mtd_part_parser cmdline_parser = {
  86. - .owner = THIS_MODULE,
  87. .parse_fn = parse_cmdline_partitions,
  88. .name = "cmdlinepart",
  89. };
  90. --- a/drivers/mtd/devices/m25p80.c
  91. +++ b/drivers/mtd/devices/m25p80.c
  92. @@ -174,22 +174,6 @@
  93. return 0;
  94. }
  95. -static int m25p80_erase(struct spi_nor *nor, loff_t offset)
  96. -{
  97. - struct m25p *flash = nor->priv;
  98. -
  99. - dev_dbg(nor->dev, "%dKiB at 0x%08x\n",
  100. - flash->spi_nor.mtd.erasesize / 1024, (u32)offset);
  101. -
  102. - /* Set up command buffer. */
  103. - flash->command[0] = nor->erase_opcode;
  104. - m25p_addr2cmd(nor, offset, flash->command);
  105. -
  106. - spi_write(flash->spi, flash->command, m25p_cmdsz(nor));
  107. -
  108. - return 0;
  109. -}
  110. -
  111. /*
  112. * board specific setup should have ensured the SPI clock used here
  113. * matches what the READ command supports, at least until this driver
  114. @@ -197,12 +181,11 @@
  115. */
  116. static int m25p_probe(struct spi_device *spi)
  117. {
  118. - struct mtd_part_parser_data ppdata;
  119. struct flash_platform_data *data;
  120. struct m25p *flash;
  121. struct spi_nor *nor;
  122. enum read_mode mode = SPI_NOR_NORMAL;
  123. - char *flash_name = NULL;
  124. + char *flash_name;
  125. int ret;
  126. data = dev_get_platdata(&spi->dev);
  127. @@ -216,12 +199,11 @@
  128. /* install the hooks */
  129. nor->read = m25p80_read;
  130. nor->write = m25p80_write;
  131. - nor->erase = m25p80_erase;
  132. nor->write_reg = m25p80_write_reg;
  133. nor->read_reg = m25p80_read_reg;
  134. nor->dev = &spi->dev;
  135. - nor->flash_node = spi->dev.of_node;
  136. + spi_nor_set_flash_node(nor, spi->dev.of_node);
  137. nor->priv = flash;
  138. spi_set_drvdata(spi, flash);
  139. @@ -242,6 +224,8 @@
  140. */
  141. if (data && data->type)
  142. flash_name = data->type;
  143. + else if (!strcmp(spi->modalias, "spi-nor"))
  144. + flash_name = NULL; /* auto-detect */
  145. else
  146. flash_name = spi->modalias;
  147. @@ -249,11 +233,8 @@
  148. if (ret)
  149. return ret;
  150. - ppdata.of_node = spi->dev.of_node;
  151. -
  152. - return mtd_device_parse_register(&nor->mtd, NULL, &ppdata,
  153. - data ? data->parts : NULL,
  154. - data ? data->nr_parts : 0);
  155. + return mtd_device_register(&nor->mtd, data ? data->parts : NULL,
  156. + data ? data->nr_parts : 0);
  157. }
  158. @@ -279,14 +260,21 @@
  159. */
  160. static const struct spi_device_id m25p_ids[] = {
  161. /*
  162. + * Allow non-DT platform devices to bind to the "spi-nor" modalias, and
  163. + * hack around the fact that the SPI core does not provide uevent
  164. + * matching for .of_match_table
  165. + */
  166. + {"spi-nor"},
  167. +
  168. + /*
  169. * Entries not used in DTs that should be safe to drop after replacing
  170. - * them with "nor-jedec" in platform data.
  171. + * them with "spi-nor" in platform data.
  172. */
  173. {"s25sl064a"}, {"w25x16"}, {"m25p10"}, {"m25px64"},
  174. /*
  175. - * Entries that were used in DTs without "nor-jedec" fallback and should
  176. - * be kept for backward compatibility.
  177. + * Entries that were used in DTs without "jedec,spi-nor" fallback and
  178. + * should be kept for backward compatibility.
  179. */
  180. {"at25df321a"}, {"at25df641"}, {"at26df081a"},
  181. {"mr25h256"},
  182. --- a/drivers/mtd/maps/physmap_of.c
  183. +++ b/drivers/mtd/maps/physmap_of.c
  184. @@ -128,7 +128,6 @@
  185. int reg_tuple_size;
  186. struct mtd_info **mtd_list = NULL;
  187. resource_size_t res_size;
  188. - struct mtd_part_parser_data ppdata;
  189. bool map_indirect;
  190. const char *mtd_name = NULL;
  191. @@ -272,8 +271,9 @@
  192. if (err)
  193. goto err_out;
  194. - ppdata.of_node = dp;
  195. - mtd_device_parse_register(info->cmtd, part_probe_types_def, &ppdata,
  196. + info->cmtd->dev.parent = &dev->dev;
  197. + mtd_set_of_node(info->cmtd, dp);
  198. + mtd_device_parse_register(info->cmtd, part_probe_types_def, NULL,
  199. NULL, 0);
  200. kfree(mtd_list);
  201. --- a/drivers/mtd/mtdchar.c
  202. +++ b/drivers/mtd/mtdchar.c
  203. @@ -465,38 +465,111 @@
  204. }
  205. /*
  206. - * Copies (and truncates, if necessary) data from the larger struct,
  207. - * nand_ecclayout, to the smaller, deprecated layout struct,
  208. - * nand_ecclayout_user. This is necessary only to support the deprecated
  209. - * API ioctl ECCGETLAYOUT while allowing all new functionality to use
  210. - * nand_ecclayout flexibly (i.e. the struct may change size in new
  211. - * releases without requiring major rewrites).
  212. + * Copies (and truncates, if necessary) OOB layout information to the
  213. + * deprecated layout struct, nand_ecclayout_user. This is necessary only to
  214. + * support the deprecated API ioctl ECCGETLAYOUT while allowing all new
  215. + * functionality to use mtd_ooblayout_ops flexibly (i.e. mtd_ooblayout_ops
  216. + * can describe any kind of OOB layout with almost zero overhead from a
  217. + * memory usage point of view).
  218. */
  219. -static int shrink_ecclayout(const struct nand_ecclayout *from,
  220. - struct nand_ecclayout_user *to)
  221. +static int shrink_ecclayout(struct mtd_info *mtd,
  222. + struct nand_ecclayout_user *to)
  223. {
  224. - int i;
  225. + struct mtd_oob_region oobregion;
  226. + int i, section = 0, ret;
  227. - if (!from || !to)
  228. + if (!mtd || !to)
  229. return -EINVAL;
  230. memset(to, 0, sizeof(*to));
  231. - to->eccbytes = min((int)from->eccbytes, MTD_MAX_ECCPOS_ENTRIES);
  232. - for (i = 0; i < to->eccbytes; i++)
  233. - to->eccpos[i] = from->eccpos[i];
  234. + to->eccbytes = 0;
  235. + for (i = 0; i < MTD_MAX_ECCPOS_ENTRIES;) {
  236. + u32 eccpos;
  237. +
  238. + ret = mtd_ooblayout_ecc(mtd, section, &oobregion);
  239. + if (ret < 0) {
  240. + if (ret != -ERANGE)
  241. + return ret;
  242. +
  243. + break;
  244. + }
  245. +
  246. + eccpos = oobregion.offset;
  247. + for (; i < MTD_MAX_ECCPOS_ENTRIES &&
  248. + eccpos < oobregion.offset + oobregion.length; i++) {
  249. + to->eccpos[i] = eccpos++;
  250. + to->eccbytes++;
  251. + }
  252. + }
  253. for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES; i++) {
  254. - if (from->oobfree[i].length == 0 &&
  255. - from->oobfree[i].offset == 0)
  256. + ret = mtd_ooblayout_free(mtd, i, &oobregion);
  257. + if (ret < 0) {
  258. + if (ret != -ERANGE)
  259. + return ret;
  260. +
  261. break;
  262. - to->oobavail += from->oobfree[i].length;
  263. - to->oobfree[i] = from->oobfree[i];
  264. + }
  265. +
  266. + to->oobfree[i].offset = oobregion.offset;
  267. + to->oobfree[i].length = oobregion.length;
  268. + to->oobavail += to->oobfree[i].length;
  269. }
  270. return 0;
  271. }
  272. +static int get_oobinfo(struct mtd_info *mtd, struct nand_oobinfo *to)
  273. +{
  274. + struct mtd_oob_region oobregion;
  275. + int i, section = 0, ret;
  276. +
  277. + if (!mtd || !to)
  278. + return -EINVAL;
  279. +
  280. + memset(to, 0, sizeof(*to));
  281. +
  282. + to->eccbytes = 0;
  283. + for (i = 0; i < ARRAY_SIZE(to->eccpos);) {
  284. + u32 eccpos;
  285. +
  286. + ret = mtd_ooblayout_ecc(mtd, section, &oobregion);
  287. + if (ret < 0) {
  288. + if (ret != -ERANGE)
  289. + return ret;
  290. +
  291. + break;
  292. + }
  293. +
  294. + if (oobregion.length + i > ARRAY_SIZE(to->eccpos))
  295. + return -EINVAL;
  296. +
  297. + eccpos = oobregion.offset;
  298. + for (; eccpos < oobregion.offset + oobregion.length; i++) {
  299. + to->eccpos[i] = eccpos++;
  300. + to->eccbytes++;
  301. + }
  302. + }
  303. +
  304. + for (i = 0; i < 8; i++) {
  305. + ret = mtd_ooblayout_free(mtd, i, &oobregion);
  306. + if (ret < 0) {
  307. + if (ret != -ERANGE)
  308. + return ret;
  309. +
  310. + break;
  311. + }
  312. +
  313. + to->oobfree[i][0] = oobregion.offset;
  314. + to->oobfree[i][1] = oobregion.length;
  315. + }
  316. +
  317. + to->useecc = MTD_NANDECC_AUTOPLACE;
  318. +
  319. + return 0;
  320. +}
  321. +
  322. static int mtdchar_blkpg_ioctl(struct mtd_info *mtd,
  323. struct blkpg_ioctl_arg *arg)
  324. {
  325. @@ -815,16 +888,12 @@
  326. {
  327. struct nand_oobinfo oi;
  328. - if (!mtd->ecclayout)
  329. + if (!mtd->ooblayout)
  330. return -EOPNOTSUPP;
  331. - if (mtd->ecclayout->eccbytes > ARRAY_SIZE(oi.eccpos))
  332. - return -EINVAL;
  333. - oi.useecc = MTD_NANDECC_AUTOPLACE;
  334. - memcpy(&oi.eccpos, mtd->ecclayout->eccpos, sizeof(oi.eccpos));
  335. - memcpy(&oi.oobfree, mtd->ecclayout->oobfree,
  336. - sizeof(oi.oobfree));
  337. - oi.eccbytes = mtd->ecclayout->eccbytes;
  338. + ret = get_oobinfo(mtd, &oi);
  339. + if (ret)
  340. + return ret;
  341. if (copy_to_user(argp, &oi, sizeof(struct nand_oobinfo)))
  342. return -EFAULT;
  343. @@ -913,14 +982,14 @@
  344. {
  345. struct nand_ecclayout_user *usrlay;
  346. - if (!mtd->ecclayout)
  347. + if (!mtd->ooblayout)
  348. return -EOPNOTSUPP;
  349. usrlay = kmalloc(sizeof(*usrlay), GFP_KERNEL);
  350. if (!usrlay)
  351. return -ENOMEM;
  352. - shrink_ecclayout(mtd->ecclayout, usrlay);
  353. + shrink_ecclayout(mtd, usrlay);
  354. if (copy_to_user(argp, usrlay, sizeof(*usrlay)))
  355. ret = -EFAULT;
  356. --- a/drivers/mtd/mtdconcat.c
  357. +++ b/drivers/mtd/mtdconcat.c
  358. @@ -777,7 +777,7 @@
  359. }
  360. - concat->mtd.ecclayout = subdev[0]->ecclayout;
  361. + mtd_set_ooblayout(&concat->mtd, subdev[0]->ooblayout);
  362. concat->num_subdev = num_devs;
  363. concat->mtd.name = name;
  364. --- a/drivers/mtd/mtdcore.c
  365. +++ b/drivers/mtd/mtdcore.c
  366. @@ -32,6 +32,7 @@
  367. #include <linux/err.h>
  368. #include <linux/ioctl.h>
  369. #include <linux/init.h>
  370. +#include <linux/of.h>
  371. #include <linux/proc_fs.h>
  372. #include <linux/idr.h>
  373. #include <linux/backing-dev.h>
  374. @@ -446,6 +447,7 @@
  375. mtd->dev.devt = MTD_DEVT(i);
  376. dev_set_name(&mtd->dev, "mtd%d", i);
  377. dev_set_drvdata(&mtd->dev, mtd);
  378. + of_node_get(mtd_get_of_node(mtd));
  379. error = device_register(&mtd->dev);
  380. if (error)
  381. goto fail_added;
  382. @@ -477,6 +479,7 @@
  383. return 0;
  384. fail_added:
  385. + of_node_put(mtd_get_of_node(mtd));
  386. idr_remove(&mtd_idr, i);
  387. fail_locked:
  388. mutex_unlock(&mtd_table_mutex);
  389. @@ -518,6 +521,7 @@
  390. device_unregister(&mtd->dev);
  391. idr_remove(&mtd_idr, mtd->index);
  392. + of_node_put(mtd_get_of_node(mtd));
  393. module_put(THIS_MODULE);
  394. ret = 0;
  395. @@ -529,9 +533,10 @@
  396. }
  397. static int mtd_add_device_partitions(struct mtd_info *mtd,
  398. - struct mtd_partition *real_parts,
  399. - int nbparts)
  400. + struct mtd_partitions *parts)
  401. {
  402. + const struct mtd_partition *real_parts = parts->parts;
  403. + int nbparts = parts->nr_parts;
  404. int ret;
  405. if (nbparts == 0 || IS_ENABLED(CONFIG_MTD_PARTITIONED_MASTER)) {
  406. @@ -600,29 +605,29 @@
  407. const struct mtd_partition *parts,
  408. int nr_parts)
  409. {
  410. + struct mtd_partitions parsed;
  411. int ret;
  412. - struct mtd_partition *real_parts = NULL;
  413. mtd_set_dev_defaults(mtd);
  414. - ret = parse_mtd_partitions(mtd, types, &real_parts, parser_data);
  415. - if (ret <= 0 && nr_parts && parts) {
  416. - real_parts = kmemdup(parts, sizeof(*parts) * nr_parts,
  417. - GFP_KERNEL);
  418. - if (!real_parts)
  419. - ret = -ENOMEM;
  420. - else
  421. - ret = nr_parts;
  422. - }
  423. - /* Didn't come up with either parsed OR fallback partitions */
  424. - if (ret < 0) {
  425. - pr_info("mtd: failed to find partitions; one or more parsers reports errors (%d)\n",
  426. + memset(&parsed, 0, sizeof(parsed));
  427. +
  428. + ret = parse_mtd_partitions(mtd, types, &parsed, parser_data);
  429. + if ((ret < 0 || parsed.nr_parts == 0) && parts && nr_parts) {
  430. + /* Fall back to driver-provided partitions */
  431. + parsed = (struct mtd_partitions){
  432. + .parts = parts,
  433. + .nr_parts = nr_parts,
  434. + };
  435. + } else if (ret < 0) {
  436. + /* Didn't come up with parsed OR fallback partitions */
  437. + pr_info("mtd: failed to find partitions; one or more parsers reports errors (%d)\n",
  438. ret);
  439. /* Don't abort on errors; we can still use unpartitioned MTD */
  440. - ret = 0;
  441. + memset(&parsed, 0, sizeof(parsed));
  442. }
  443. - ret = mtd_add_device_partitions(mtd, real_parts, ret);
  444. + ret = mtd_add_device_partitions(mtd, ret);
  445. if (ret)
  446. goto out;
  447. @@ -642,7 +647,8 @@
  448. }
  449. out:
  450. - kfree(real_parts);
  451. + /* Cleanup any parsed partitions */
  452. + mtd_part_parser_cleanup(&parsed);
  453. return ret;
  454. }
  455. EXPORT_SYMBOL_GPL(mtd_device_parse_register);
  456. @@ -767,7 +773,6 @@
  457. }
  458. EXPORT_SYMBOL_GPL(get_mtd_device);
  459. -
  460. int __get_mtd_device(struct mtd_info *mtd)
  461. {
  462. int err;
  463. @@ -1001,6 +1006,366 @@
  464. }
  465. EXPORT_SYMBOL_GPL(mtd_read_oob);
  466. +/**
  467. + * mtd_ooblayout_ecc - Get the OOB region definition of a specific ECC section
  468. + * @mtd: MTD device structure
  469. + * @section: ECC section. Depending on the layout you may have all the ECC
  470. + * bytes stored in a single contiguous section, or one section
  471. + * per ECC chunk (and sometime several sections for a single ECC
  472. + * ECC chunk)
  473. + * @oobecc: OOB region struct filled with the appropriate ECC position
  474. + * information
  475. + *
  476. + * This functions return ECC section information in the OOB area. I you want
  477. + * to get all the ECC bytes information, then you should call
  478. + * mtd_ooblayout_ecc(mtd, section++, oobecc) until it returns -ERANGE.
  479. + *
  480. + * Returns zero on success, a negative error code otherwise.
  481. + */
  482. +int mtd_ooblayout_ecc(struct mtd_info *mtd, int section,
  483. + struct mtd_oob_region *oobecc)
  484. +{
  485. + memset(oobecc, 0, sizeof(*oobecc));
  486. +
  487. + if (!mtd || section < 0)
  488. + return -EINVAL;
  489. +
  490. + if (!mtd->ooblayout || !mtd->ooblayout->ecc)
  491. + return -ENOTSUPP;
  492. +
  493. + return mtd->ooblayout->ecc(mtd, section, oobecc);
  494. +}
  495. +EXPORT_SYMBOL_GPL(mtd_ooblayout_ecc);
  496. +
  497. +/**
  498. + * mtd_ooblayout_free - Get the OOB region definition of a specific free
  499. + * section
  500. + * @mtd: MTD device structure
  501. + * @section: Free section you are interested in. Depending on the layout
  502. + * you may have all the free bytes stored in a single contiguous
  503. + * section, or one section per ECC chunk plus an extra section
  504. + * for the remaining bytes (or other funky layout).
  505. + * @oobfree: OOB region struct filled with the appropriate free position
  506. + * information
  507. + *
  508. + * This functions return free bytes position in the OOB area. I you want
  509. + * to get all the free bytes information, then you should call
  510. + * mtd_ooblayout_free(mtd, section++, oobfree) until it returns -ERANGE.
  511. + *
  512. + * Returns zero on success, a negative error code otherwise.
  513. + */
  514. +int mtd_ooblayout_free(struct mtd_info *mtd, int section,
  515. + struct mtd_oob_region *oobfree)
  516. +{
  517. + memset(oobfree, 0, sizeof(*oobfree));
  518. +
  519. + if (!mtd || section < 0)
  520. + return -EINVAL;
  521. +
  522. + if (!mtd->ooblayout || !mtd->ooblayout->free)
  523. + return -ENOTSUPP;
  524. +
  525. + return mtd->ooblayout->free(mtd, section, oobfree);
  526. +}
  527. +EXPORT_SYMBOL_GPL(mtd_ooblayout_free);
  528. +
  529. +/**
  530. + * mtd_ooblayout_find_region - Find the region attached to a specific byte
  531. + * @mtd: mtd info structure
  532. + * @byte: the byte we are searching for
  533. + * @sectionp: pointer where the section id will be stored
  534. + * @oobregion: used to retrieve the ECC position
  535. + * @iter: iterator function. Should be either mtd_ooblayout_free or
  536. + * mtd_ooblayout_ecc depending on the region type you're searching for
  537. + *
  538. + * This functions returns the section id and oobregion information of a
  539. + * specific byte. For example, say you want to know where the 4th ECC byte is
  540. + * stored, you'll use:
  541. + *
  542. + * mtd_ooblayout_find_region(mtd, 3, &section, &oobregion, mtd_ooblayout_ecc);
  543. + *
  544. + * Returns zero on success, a negative error code otherwise.
  545. + */
  546. +static int mtd_ooblayout_find_region(struct mtd_info *mtd, int byte,
  547. + int *sectionp, struct mtd_oob_region *oobregion,
  548. + int (*iter)(struct mtd_info *,
  549. + int section,
  550. + struct mtd_oob_region *oobregion))
  551. +{
  552. + int pos = 0, ret, section = 0;
  553. +
  554. + memset(oobregion, 0, sizeof(*oobregion));
  555. +
  556. + while (1) {
  557. + ret = iter(mtd, section, oobregion);
  558. + if (ret)
  559. + return ret;
  560. +
  561. + if (pos + oobregion->length > byte)
  562. + break;
  563. +
  564. + pos += oobregion->length;
  565. + section++;
  566. + }
  567. +
  568. + /*
  569. + * Adjust region info to make it start at the beginning at the
  570. + * 'start' ECC byte.
  571. + */
  572. + oobregion->offset += byte - pos;
  573. + oobregion->length -= byte - pos;
  574. + *sectionp = section;
  575. +
  576. + return 0;
  577. +}
  578. +
  579. +/**
  580. + * mtd_ooblayout_find_eccregion - Find the ECC region attached to a specific
  581. + * ECC byte
  582. + * @mtd: mtd info structure
  583. + * @eccbyte: the byte we are searching for
  584. + * @sectionp: pointer where the section id will be stored
  585. + * @oobregion: OOB region information
  586. + *
  587. + * Works like mtd_ooblayout_find_region() except it searches for a specific ECC
  588. + * byte.
  589. + *
  590. + * Returns zero on success, a negative error code otherwise.
  591. + */
  592. +int mtd_ooblayout_find_eccregion(struct mtd_info *mtd, int eccbyte,
  593. + int *section,
  594. + struct mtd_oob_region *oobregion)
  595. +{
  596. + return mtd_ooblayout_find_region(mtd, eccbyte, section, oobregion,
  597. + mtd_ooblayout_ecc);
  598. +}
  599. +EXPORT_SYMBOL_GPL(mtd_ooblayout_find_eccregion);
  600. +
  601. +/**
  602. + * mtd_ooblayout_get_bytes - Extract OOB bytes from the oob buffer
  603. + * @mtd: mtd info structure
  604. + * @buf: destination buffer to store OOB bytes
  605. + * @oobbuf: OOB buffer
  606. + * @start: first byte to retrieve
  607. + * @nbytes: number of bytes to retrieve
  608. + * @iter: section iterator
  609. + *
  610. + * Extract bytes attached to a specific category (ECC or free)
  611. + * from the OOB buffer and copy them into buf.
  612. + *
  613. + * Returns zero on success, a negative error code otherwise.
  614. + */
  615. +static int mtd_ooblayout_get_bytes(struct mtd_info *mtd, u8 *buf,
  616. + const u8 *oobbuf, int start, int nbytes,
  617. + int (*iter)(struct mtd_info *,
  618. + int section,
  619. + struct mtd_oob_region *oobregion))
  620. +{
  621. + struct mtd_oob_region oobregion = { };
  622. + int section = 0, ret;
  623. +
  624. + ret = mtd_ooblayout_find_region(mtd, start, &section,
  625. + &oobregion, iter);
  626. +
  627. + while (!ret) {
  628. + int cnt;
  629. +
  630. + cnt = oobregion.length > nbytes ? nbytes : oobregion.length;
  631. + memcpy(buf, oobbuf + oobregion.offset, cnt);
  632. + buf += cnt;
  633. + nbytes -= cnt;
  634. +
  635. + if (!nbytes)
  636. + break;
  637. +
  638. + ret = iter(mtd, ++section, &oobregion);
  639. + }
  640. +
  641. + return ret;
  642. +}
  643. +
  644. +/**
  645. + * mtd_ooblayout_set_bytes - put OOB bytes into the oob buffer
  646. + * @mtd: mtd info structure
  647. + * @buf: source buffer to get OOB bytes from
  648. + * @oobbuf: OOB buffer
  649. + * @start: first OOB byte to set
  650. + * @nbytes: number of OOB bytes to set
  651. + * @iter: section iterator
  652. + *
  653. + * Fill the OOB buffer with data provided in buf. The category (ECC or free)
  654. + * is selected by passing the appropriate iterator.
  655. + *
  656. + * Returns zero on success, a negative error code otherwise.
  657. + */
  658. +static int mtd_ooblayout_set_bytes(struct mtd_info *mtd, const u8 *buf,
  659. + u8 *oobbuf, int start, int nbytes,
  660. + int (*iter)(struct mtd_info *,
  661. + int section,
  662. + struct mtd_oob_region *oobregion))
  663. +{
  664. + struct mtd_oob_region oobregion = { };
  665. + int section = 0, ret;
  666. +
  667. + ret = mtd_ooblayout_find_region(mtd, start, &section,
  668. + &oobregion, iter);
  669. +
  670. + while (!ret) {
  671. + int cnt;
  672. +
  673. + cnt = oobregion.length > nbytes ? nbytes : oobregion.length;
  674. + memcpy(oobbuf + oobregion.offset, buf, cnt);
  675. + buf += cnt;
  676. + nbytes -= cnt;
  677. +
  678. + if (!nbytes)
  679. + break;
  680. +
  681. + ret = iter(mtd, ++section, &oobregion);
  682. + }
  683. +
  684. + return ret;
  685. +}
  686. +
  687. +/**
  688. + * mtd_ooblayout_count_bytes - count the number of bytes in a OOB category
  689. + * @mtd: mtd info structure
  690. + * @iter: category iterator
  691. + *
  692. + * Count the number of bytes in a given category.
  693. + *
  694. + * Returns a positive value on success, a negative error code otherwise.
  695. + */
  696. +static int mtd_ooblayout_count_bytes(struct mtd_info *mtd,
  697. + int (*iter)(struct mtd_info *,
  698. + int section,
  699. + struct mtd_oob_region *oobregion))
  700. +{
  701. + struct mtd_oob_region oobregion = { };
  702. + int section = 0, ret, nbytes = 0;
  703. +
  704. + while (1) {
  705. + ret = iter(mtd, section++, &oobregion);
  706. + if (ret) {
  707. + if (ret == -ERANGE)
  708. + ret = nbytes;
  709. + break;
  710. + }
  711. +
  712. + nbytes += oobregion.length;
  713. + }
  714. +
  715. + return ret;
  716. +}
  717. +
  718. +/**
  719. + * mtd_ooblayout_get_eccbytes - extract ECC bytes from the oob buffer
  720. + * @mtd: mtd info structure
  721. + * @eccbuf: destination buffer to store ECC bytes
  722. + * @oobbuf: OOB buffer
  723. + * @start: first ECC byte to retrieve
  724. + * @nbytes: number of ECC bytes to retrieve
  725. + *
  726. + * Works like mtd_ooblayout_get_bytes(), except it acts on ECC bytes.
  727. + *
  728. + * Returns zero on success, a negative error code otherwise.
  729. + */
  730. +int mtd_ooblayout_get_eccbytes(struct mtd_info *mtd, u8 *eccbuf,
  731. + const u8 *oobbuf, int start, int nbytes)
  732. +{
  733. + return mtd_ooblayout_get_bytes(mtd, eccbuf, oobbuf, start, nbytes,
  734. + mtd_ooblayout_ecc);
  735. +}
  736. +EXPORT_SYMBOL_GPL(mtd_ooblayout_get_eccbytes);
  737. +
  738. +/**
  739. + * mtd_ooblayout_set_eccbytes - set ECC bytes into the oob buffer
  740. + * @mtd: mtd info structure
  741. + * @eccbuf: source buffer to get ECC bytes from
  742. + * @oobbuf: OOB buffer
  743. + * @start: first ECC byte to set
  744. + * @nbytes: number of ECC bytes to set
  745. + *
  746. + * Works like mtd_ooblayout_set_bytes(), except it acts on ECC bytes.
  747. + *
  748. + * Returns zero on success, a negative error code otherwise.
  749. + */
  750. +int mtd_ooblayout_set_eccbytes(struct mtd_info *mtd, const u8 *eccbuf,
  751. + u8 *oobbuf, int start, int nbytes)
  752. +{
  753. + return mtd_ooblayout_set_bytes(mtd, eccbuf, oobbuf, start, nbytes,
  754. + mtd_ooblayout_ecc);
  755. +}
  756. +EXPORT_SYMBOL_GPL(mtd_ooblayout_set_eccbytes);
  757. +
  758. +/**
  759. + * mtd_ooblayout_get_databytes - extract data bytes from the oob buffer
  760. + * @mtd: mtd info structure
  761. + * @databuf: destination buffer to store ECC bytes
  762. + * @oobbuf: OOB buffer
  763. + * @start: first ECC byte to retrieve
  764. + * @nbytes: number of ECC bytes to retrieve
  765. + *
  766. + * Works like mtd_ooblayout_get_bytes(), except it acts on free bytes.
  767. + *
  768. + * Returns zero on success, a negative error code otherwise.
  769. + */
  770. +int mtd_ooblayout_get_databytes(struct mtd_info *mtd, u8 *databuf,
  771. + const u8 *oobbuf, int start, int nbytes)
  772. +{
  773. + return mtd_ooblayout_get_bytes(mtd, databuf, oobbuf, start, nbytes,
  774. + mtd_ooblayout_free);
  775. +}
  776. +EXPORT_SYMBOL_GPL(mtd_ooblayout_get_databytes);
  777. +
  778. +/**
  779. + * mtd_ooblayout_get_eccbytes - set data bytes into the oob buffer
  780. + * @mtd: mtd info structure
  781. + * @eccbuf: source buffer to get data bytes from
  782. + * @oobbuf: OOB buffer
  783. + * @start: first ECC byte to set
  784. + * @nbytes: number of ECC bytes to set
  785. + *
  786. + * Works like mtd_ooblayout_get_bytes(), except it acts on free bytes.
  787. + *
  788. + * Returns zero on success, a negative error code otherwise.
  789. + */
  790. +int mtd_ooblayout_set_databytes(struct mtd_info *mtd, const u8 *databuf,
  791. + u8 *oobbuf, int start, int nbytes)
  792. +{
  793. + return mtd_ooblayout_set_bytes(mtd, databuf, oobbuf, start, nbytes,
  794. + mtd_ooblayout_free);
  795. +}
  796. +EXPORT_SYMBOL_GPL(mtd_ooblayout_set_databytes);
  797. +
  798. +/**
  799. + * mtd_ooblayout_count_freebytes - count the number of free bytes in OOB
  800. + * @mtd: mtd info structure
  801. + *
  802. + * Works like mtd_ooblayout_count_bytes(), except it count free bytes.
  803. + *
  804. + * Returns zero on success, a negative error code otherwise.
  805. + */
  806. +int mtd_ooblayout_count_freebytes(struct mtd_info *mtd)
  807. +{
  808. + return mtd_ooblayout_count_bytes(mtd, mtd_ooblayout_free);
  809. +}
  810. +EXPORT_SYMBOL_GPL(mtd_ooblayout_count_freebytes);
  811. +
  812. +/**
  813. + * mtd_ooblayout_count_freebytes - count the number of ECC bytes in OOB
  814. + * @mtd: mtd info structure
  815. + *
  816. + * Works like mtd_ooblayout_count_bytes(), except it count ECC bytes.
  817. + *
  818. + * Returns zero on success, a negative error code otherwise.
  819. + */
  820. +int mtd_ooblayout_count_eccbytes(struct mtd_info *mtd)
  821. +{
  822. + return mtd_ooblayout_count_bytes(mtd, mtd_ooblayout_ecc);
  823. +}
  824. +EXPORT_SYMBOL_GPL(mtd_ooblayout_count_eccbytes);
  825. +
  826. /*
  827. * Method to access the protection register area, present in some flash
  828. * devices. The user data is one time programmable but the factory data is read
  829. --- a/drivers/mtd/mtdcore.h
  830. +++ b/drivers/mtd/mtdcore.h
  831. @@ -10,10 +10,15 @@
  832. int del_mtd_device(struct mtd_info *mtd);
  833. int add_mtd_partitions(struct mtd_info *, const struct mtd_partition *, int);
  834. int del_mtd_partitions(struct mtd_info *);
  835. +
  836. +struct mtd_partitions;
  837. +
  838. int parse_mtd_partitions(struct mtd_info *master, const char * const *types,
  839. - struct mtd_partition **pparts,
  840. + struct mtd_partitions *pparts,
  841. struct mtd_part_parser_data *data);
  842. +void mtd_part_parser_cleanup(struct mtd_partitions *parts);
  843. +
  844. int __init init_mtdchar(void);
  845. void __exit cleanup_mtdchar(void);
  846. --- a/drivers/mtd/mtdpart.c
  847. +++ b/drivers/mtd/mtdpart.c
  848. @@ -55,9 +55,12 @@
  849. /*
  850. * Given a pointer to the MTD object in the mtd_part structure, we can retrieve
  851. - * the pointer to that structure with this macro.
  852. + * the pointer to that structure.
  853. */
  854. -#define PART(x) ((struct mtd_part *)(x))
  855. +static inline struct mtd_part *mtd_to_part(const struct mtd_info *mtd)
  856. +{
  857. + return container_of(mtd, struct mtd_part, mtd);
  858. +}
  859. /*
  860. @@ -68,7 +71,7 @@
  861. static int part_read(struct mtd_info *mtd, loff_t from, size_t len,
  862. size_t *retlen, u_char *buf)
  863. {
  864. - struct mtd_part *part = PART(mtd);
  865. + struct mtd_part *part = mtd_to_part(mtd);
  866. struct mtd_ecc_stats stats;
  867. int res;
  868. @@ -87,7 +90,7 @@
  869. static int part_point(struct mtd_info *mtd, loff_t from, size_t len,
  870. size_t *retlen, void **virt, resource_size_t *phys)
  871. {
  872. - struct mtd_part *part = PART(mtd);
  873. + struct mtd_part *part = mtd_to_part(mtd);
  874. return part->master->_point(part->master, from + part->offset, len,
  875. retlen, virt, phys);
  876. @@ -95,7 +98,7 @@
  877. static int part_unpoint(struct mtd_info *mtd, loff_t from, size_t len)
  878. {
  879. - struct mtd_part *part = PART(mtd);
  880. + struct mtd_part *part = mtd_to_part(mtd);
  881. return part->master->_unpoint(part->master, from + part->offset, len);
  882. }
  883. @@ -105,7 +108,7 @@
  884. unsigned long offset,
  885. unsigned long flags)
  886. {
  887. - struct mtd_part *part = PART(mtd);
  888. + struct mtd_part *part = mtd_to_part(mtd);
  889. offset += part->offset;
  890. return part->master->_get_unmapped_area(part->master, len, offset,
  891. @@ -115,7 +118,7 @@
  892. static int part_read_oob(struct mtd_info *mtd, loff_t from,
  893. struct mtd_oob_ops *ops)
  894. {
  895. - struct mtd_part *part = PART(mtd);
  896. + struct mtd_part *part = mtd_to_part(mtd);
  897. int res;
  898. if (from >= mtd->size)
  899. @@ -130,10 +133,7 @@
  900. if (ops->oobbuf) {
  901. size_t len, pages;
  902. - if (ops->mode == MTD_OPS_AUTO_OOB)
  903. - len = mtd->oobavail;
  904. - else
  905. - len = mtd->oobsize;
  906. + len = mtd_oobavail(mtd, ops);
  907. pages = mtd_div_by_ws(mtd->size, mtd);
  908. pages -= mtd_div_by_ws(from, mtd);
  909. if (ops->ooboffs + ops->ooblen > pages * len)
  910. @@ -153,7 +153,7 @@
  911. static int part_read_user_prot_reg(struct mtd_info *mtd, loff_t from,
  912. size_t len, size_t *retlen, u_char *buf)
  913. {
  914. - struct mtd_part *part = PART(mtd);
  915. + struct mtd_part *part = mtd_to_part(mtd);
  916. return part->master->_read_user_prot_reg(part->master, from, len,
  917. retlen, buf);
  918. }
  919. @@ -161,7 +161,7 @@
  920. static int part_get_user_prot_info(struct mtd_info *mtd, size_t len,
  921. size_t *retlen, struct otp_info *buf)
  922. {
  923. - struct mtd_part *part = PART(mtd);
  924. + struct mtd_part *part = mtd_to_part(mtd);
  925. return part->master->_get_user_prot_info(part->master, len, retlen,
  926. buf);
  927. }
  928. @@ -169,7 +169,7 @@
  929. static int part_read_fact_prot_reg(struct mtd_info *mtd, loff_t from,
  930. size_t len, size_t *retlen, u_char *buf)
  931. {
  932. - struct mtd_part *part = PART(mtd);
  933. + struct mtd_part *part = mtd_to_part(mtd);
  934. return part->master->_read_fact_prot_reg(part->master, from, len,
  935. retlen, buf);
  936. }
  937. @@ -177,7 +177,7 @@
  938. static int part_get_fact_prot_info(struct mtd_info *mtd, size_t len,
  939. size_t *retlen, struct otp_info *buf)
  940. {
  941. - struct mtd_part *part = PART(mtd);
  942. + struct mtd_part *part = mtd_to_part(mtd);
  943. return part->master->_get_fact_prot_info(part->master, len, retlen,
  944. buf);
  945. }
  946. @@ -185,7 +185,7 @@
  947. static int part_write(struct mtd_info *mtd, loff_t to, size_t len,
  948. size_t *retlen, const u_char *buf)
  949. {
  950. - struct mtd_part *part = PART(mtd);
  951. + struct mtd_part *part = mtd_to_part(mtd);
  952. return part->master->_write(part->master, to + part->offset, len,
  953. retlen, buf);
  954. }
  955. @@ -193,7 +193,7 @@
  956. static int part_panic_write(struct mtd_info *mtd, loff_t to, size_t len,
  957. size_t *retlen, const u_char *buf)
  958. {
  959. - struct mtd_part *part = PART(mtd);
  960. + struct mtd_part *part = mtd_to_part(mtd);
  961. return part->master->_panic_write(part->master, to + part->offset, len,
  962. retlen, buf);
  963. }
  964. @@ -201,7 +201,7 @@
  965. static int part_write_oob(struct mtd_info *mtd, loff_t to,
  966. struct mtd_oob_ops *ops)
  967. {
  968. - struct mtd_part *part = PART(mtd);
  969. + struct mtd_part *part = mtd_to_part(mtd);
  970. if (to >= mtd->size)
  971. return -EINVAL;
  972. @@ -213,7 +213,7 @@
  973. static int part_write_user_prot_reg(struct mtd_info *mtd, loff_t from,
  974. size_t len, size_t *retlen, u_char *buf)
  975. {
  976. - struct mtd_part *part = PART(mtd);
  977. + struct mtd_part *part = mtd_to_part(mtd);
  978. return part->master->_write_user_prot_reg(part->master, from, len,
  979. retlen, buf);
  980. }
  981. @@ -221,21 +221,21 @@
  982. static int part_lock_user_prot_reg(struct mtd_info *mtd, loff_t from,
  983. size_t len)
  984. {
  985. - struct mtd_part *part = PART(mtd);
  986. + struct mtd_part *part = mtd_to_part(mtd);
  987. return part->master->_lock_user_prot_reg(part->master, from, len);
  988. }
  989. static int part_writev(struct mtd_info *mtd, const struct kvec *vecs,
  990. unsigned long count, loff_t to, size_t *retlen)
  991. {
  992. - struct mtd_part *part = PART(mtd);
  993. + struct mtd_part *part = mtd_to_part(mtd);
  994. return part->master->_writev(part->master, vecs, count,
  995. to + part->offset, retlen);
  996. }
  997. static int part_erase(struct mtd_info *mtd, struct erase_info *instr)
  998. {
  999. - struct mtd_part *part = PART(mtd);
  1000. + struct mtd_part *part = mtd_to_part(mtd);
  1001. int ret;
  1002. @@ -299,7 +299,7 @@
  1003. void mtd_erase_callback(struct erase_info *instr)
  1004. {
  1005. if (instr->mtd->_erase == part_erase) {
  1006. - struct mtd_part *part = PART(instr->mtd);
  1007. + struct mtd_part *part = mtd_to_part(instr->mtd);
  1008. size_t wrlen = 0;
  1009. if (instr->mtd->flags & MTD_ERASE_PARTIAL) {
  1010. @@ -330,13 +330,13 @@
  1011. static int part_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  1012. {
  1013. - struct mtd_part *part = PART(mtd);
  1014. + struct mtd_part *part = mtd_to_part(mtd);
  1015. return part->master->_lock(part->master, ofs + part->offset, len);
  1016. }
  1017. static int part_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  1018. {
  1019. - struct mtd_part *part = PART(mtd);
  1020. + struct mtd_part *part = mtd_to_part(mtd);
  1021. ofs += part->offset;
  1022. if (mtd->flags & MTD_ERASE_PARTIAL) {
  1023. @@ -349,45 +349,45 @@
  1024. static int part_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  1025. {
  1026. - struct mtd_part *part = PART(mtd);
  1027. + struct mtd_part *part = mtd_to_part(mtd);
  1028. return part->master->_is_locked(part->master, ofs + part->offset, len);
  1029. }
  1030. static void part_sync(struct mtd_info *mtd)
  1031. {
  1032. - struct mtd_part *part = PART(mtd);
  1033. + struct mtd_part *part = mtd_to_part(mtd);
  1034. part->master->_sync(part->master);
  1035. }
  1036. static int part_suspend(struct mtd_info *mtd)
  1037. {
  1038. - struct mtd_part *part = PART(mtd);
  1039. + struct mtd_part *part = mtd_to_part(mtd);
  1040. return part->master->_suspend(part->master);
  1041. }
  1042. static void part_resume(struct mtd_info *mtd)
  1043. {
  1044. - struct mtd_part *part = PART(mtd);
  1045. + struct mtd_part *part = mtd_to_part(mtd);
  1046. part->master->_resume(part->master);
  1047. }
  1048. static int part_block_isreserved(struct mtd_info *mtd, loff_t ofs)
  1049. {
  1050. - struct mtd_part *part = PART(mtd);
  1051. + struct mtd_part *part = mtd_to_part(mtd);
  1052. ofs += part->offset;
  1053. return part->master->_block_isreserved(part->master, ofs);
  1054. }
  1055. static int part_block_isbad(struct mtd_info *mtd, loff_t ofs)
  1056. {
  1057. - struct mtd_part *part = PART(mtd);
  1058. + struct mtd_part *part = mtd_to_part(mtd);
  1059. ofs += part->offset;
  1060. return part->master->_block_isbad(part->master, ofs);
  1061. }
  1062. static int part_block_markbad(struct mtd_info *mtd, loff_t ofs)
  1063. {
  1064. - struct mtd_part *part = PART(mtd);
  1065. + struct mtd_part *part = mtd_to_part(mtd);
  1066. int res;
  1067. ofs += part->offset;
  1068. @@ -397,6 +397,27 @@
  1069. return res;
  1070. }
  1071. +static int part_ooblayout_ecc(struct mtd_info *mtd, int section,
  1072. + struct mtd_oob_region *oobregion)
  1073. +{
  1074. + struct mtd_part *part = mtd_to_part(mtd);
  1075. +
  1076. + return mtd_ooblayout_ecc(part->master, section, oobregion);
  1077. +}
  1078. +
  1079. +static int part_ooblayout_free(struct mtd_info *mtd, int section,
  1080. + struct mtd_oob_region *oobregion)
  1081. +{
  1082. + struct mtd_part *part = mtd_to_part(mtd);
  1083. +
  1084. + return mtd_ooblayout_free(part->master, section, oobregion);
  1085. +}
  1086. +
  1087. +static const struct mtd_ooblayout_ops part_ooblayout_ops = {
  1088. + .ecc = part_ooblayout_ecc,
  1089. + .free = part_ooblayout_free,
  1090. +};
  1091. +
  1092. static inline void free_partition(struct mtd_part *p)
  1093. {
  1094. kfree(p->mtd.name);
  1095. @@ -614,7 +635,7 @@
  1096. slave->mtd.erasesize = slave->mtd.size;
  1097. }
  1098. - slave->mtd.ecclayout = master->ecclayout;
  1099. + mtd_set_ooblayout(&slave->mtd, &part_ooblayout_ops);
  1100. slave->mtd.ecc_step_size = master->ecc_step_size;
  1101. slave->mtd.ecc_strength = master->ecc_strength;
  1102. slave->mtd.bitflip_threshold = master->bitflip_threshold;
  1103. @@ -639,7 +660,7 @@
  1104. struct device_attribute *attr, char *buf)
  1105. {
  1106. struct mtd_info *mtd = dev_get_drvdata(dev);
  1107. - struct mtd_part *part = PART(mtd);
  1108. + struct mtd_part *part = mtd_to_part(mtd);
  1109. return snprintf(buf, PAGE_SIZE, "%lld\n", part->offset);
  1110. }
  1111. @@ -677,11 +698,10 @@
  1112. if (length <= 0)
  1113. return -EINVAL;
  1114. + memset(&part, 0, sizeof(part));
  1115. part.name = name;
  1116. part.size = length;
  1117. part.offset = offset;
  1118. - part.mask_flags = 0;
  1119. - part.ecclayout = NULL;
  1120. new = allocate_partition(master, &part, -1, offset);
  1121. if (IS_ERR(new))
  1122. @@ -845,7 +865,7 @@
  1123. static DEFINE_SPINLOCK(part_parser_lock);
  1124. static LIST_HEAD(part_parsers);
  1125. -static struct mtd_part_parser *get_partition_parser(const char *name)
  1126. +static struct mtd_part_parser *mtd_part_parser_get(const char *name)
  1127. {
  1128. struct mtd_part_parser *p, *ret = NULL;
  1129. @@ -862,7 +882,20 @@
  1130. return ret;
  1131. }
  1132. -#define put_partition_parser(p) do { module_put((p)->owner); } while (0)
  1133. +static inline void mtd_part_parser_put(const struct mtd_part_parser *p)
  1134. +{
  1135. + module_put(p->owner);
  1136. +}
  1137. +
  1138. +/*
  1139. + * Many partition parsers just expected the core to kfree() all their data in
  1140. + * one chunk. Do that by default.
  1141. + */
  1142. +static void mtd_part_parser_cleanup_default(const struct mtd_partition *pparts,
  1143. + int nr_parts)
  1144. +{
  1145. + kfree(pparts);
  1146. +}
  1147. static struct mtd_part_parser *
  1148. get_partition_parser_by_type(enum mtd_parser_type type,
  1149. @@ -874,7 +907,7 @@
  1150. p = list_prepare_entry(start, &part_parsers, list);
  1151. if (start)
  1152. - put_partition_parser(start);
  1153. + mtd_part_parser_put(start);
  1154. list_for_each_entry_continue(p, &part_parsers, list) {
  1155. if (p->type == type && try_module_get(p->owner)) {
  1156. @@ -888,13 +921,19 @@
  1157. return ret;
  1158. }
  1159. -void register_mtd_parser(struct mtd_part_parser *p)
  1160. -{
  1161. +int __register_mtd_parser(struct mtd_part_parser *p, struct module *owner)
  1162. + {
  1163. + p->owner = owner;
  1164. +
  1165. + if (!p->cleanup)
  1166. + p->cleanup = &mtd_part_parser_cleanup_default;
  1167. +
  1168. spin_lock(&part_parser_lock);
  1169. list_add(&p->list, &part_parsers);
  1170. spin_unlock(&part_parser_lock);
  1171. + return 0;
  1172. }
  1173. -EXPORT_SYMBOL_GPL(register_mtd_parser);
  1174. +EXPORT_SYMBOL_GPL(__register_mtd_parser);
  1175. void deregister_mtd_parser(struct mtd_part_parser *p)
  1176. {
  1177. @@ -954,7 +993,7 @@
  1178. * parse_mtd_partitions - parse MTD partitions
  1179. * @master: the master partition (describes whole MTD device)
  1180. * @types: names of partition parsers to try or %NULL
  1181. - * @pparts: array of partitions found is returned here
  1182. + * @pparts: info about partitions found is returned here
  1183. * @data: MTD partition parser-specific data
  1184. *
  1185. * This function tries to find partition on MTD device @master. It uses MTD
  1186. @@ -966,45 +1005,42 @@
  1187. *
  1188. * This function may return:
  1189. * o a negative error code in case of failure
  1190. - * o zero if no partitions were found
  1191. - * o a positive number of found partitions, in which case on exit @pparts will
  1192. - * point to an array containing this number of &struct mtd_info objects.
  1193. + * o zero otherwise, and @pparts will describe the partitions, number of
  1194. + * partitions, and the parser which parsed them. Caller must release
  1195. + * resources with mtd_part_parser_cleanup() when finished with the returned
  1196. + * data.
  1197. */
  1198. int parse_mtd_partitions(struct mtd_info *master, const char *const *types,
  1199. - struct mtd_partition **pparts,
  1200. + struct mtd_partitions *pparts,
  1201. struct mtd_part_parser_data *data)
  1202. {
  1203. struct mtd_part_parser *parser;
  1204. int ret, err = 0;
  1205. const char *const *types_of = NULL;
  1206. - if (data && data->of_node) {
  1207. - types_of = of_get_probes(data->of_node);
  1208. - if (types_of != NULL)
  1209. - types = types_of;
  1210. - }
  1211. -
  1212. if (!types)
  1213. types = default_mtd_part_types;
  1214. for ( ; *types; types++) {
  1215. pr_debug("%s: parsing partitions %s\n", master->name, *types);
  1216. - parser = get_partition_parser(*types);
  1217. + parser = mtd_part_parser_get(*types);
  1218. if (!parser && !request_module("%s", *types))
  1219. - parser = get_partition_parser(*types);
  1220. + parser = mtd_part_parser_get(*types);
  1221. pr_debug("%s: got parser %s\n", master->name,
  1222. parser ? parser->name : NULL);
  1223. if (!parser)
  1224. continue;
  1225. - ret = (*parser->parse_fn)(master, pparts, data);
  1226. + ret = (*parser->parse_fn)(master, &pparts->parts, data);
  1227. pr_debug("%s: parser %s: %i\n",
  1228. master->name, parser->name, ret);
  1229. - put_partition_parser(parser);
  1230. if (ret > 0) {
  1231. printk(KERN_NOTICE "%d %s partitions found on MTD device %s\n",
  1232. ret, parser->name, master->name);
  1233. - return ret;
  1234. + pparts->nr_parts = ret;
  1235. + pparts->parser = parser;
  1236. + return 0;
  1237. }
  1238. + mtd_part_parser_put(parser);
  1239. /*
  1240. * Stash the first error we see; only report it if no parser
  1241. * succeeds
  1242. @@ -1034,7 +1070,7 @@
  1243. ret = (*parser->parse_fn)(master, pparts, data);
  1244. if (ret > 0) {
  1245. - put_partition_parser(parser);
  1246. + mtd_part_parser_put(parser);
  1247. printk(KERN_NOTICE
  1248. "%d %s partitions found on MTD device %s\n",
  1249. ret, parser->name, master->name);
  1250. @@ -1048,6 +1084,22 @@
  1251. }
  1252. EXPORT_SYMBOL_GPL(parse_mtd_partitions_by_type);
  1253. +void mtd_part_parser_cleanup(struct mtd_partitions *parts)
  1254. +{
  1255. + const struct mtd_part_parser *parser;
  1256. +
  1257. + if (!parts)
  1258. + return;
  1259. +
  1260. + parser = parts->parser;
  1261. + if (parser) {
  1262. + if (parser->cleanup)
  1263. + parser->cleanup(parts->parts, parts->nr_parts);
  1264. +
  1265. + mtd_part_parser_put(parser);
  1266. + }
  1267. +}
  1268. +
  1269. int mtd_is_partition(const struct mtd_info *mtd)
  1270. {
  1271. struct mtd_part *part;
  1272. @@ -1070,7 +1122,7 @@
  1273. if (!mtd_is_partition(mtd))
  1274. return (struct mtd_info *)mtd;
  1275. - return PART(mtd)->master;
  1276. + return mtd_to_part(mtd)->master;
  1277. }
  1278. EXPORT_SYMBOL_GPL(mtdpart_get_master);
  1279. @@ -1079,7 +1131,7 @@
  1280. if (!mtd_is_partition(mtd))
  1281. return 0;
  1282. - return PART(mtd)->offset;
  1283. + return mtd_to_part(mtd)->offset;
  1284. }
  1285. EXPORT_SYMBOL_GPL(mtdpart_get_offset);
  1286. @@ -1089,6 +1141,6 @@
  1287. if (!mtd_is_partition(mtd))
  1288. return mtd->size;
  1289. - return PART(mtd)->master->size;
  1290. + return mtd_to_part(mtd)->master->size;
  1291. }
  1292. EXPORT_SYMBOL_GPL(mtd_get_device_size);
  1293. --- a/drivers/mtd/mtdswap.c
  1294. +++ b/drivers/mtd/mtdswap.c
  1295. @@ -346,7 +346,7 @@
  1296. if (mtd_can_have_bb(d->mtd) && mtd_block_isbad(d->mtd, offset))
  1297. return MTDSWAP_SCANNED_BAD;
  1298. - ops.ooblen = 2 * d->mtd->ecclayout->oobavail;
  1299. + ops.ooblen = 2 * d->mtd->oobavail;
  1300. ops.oobbuf = d->oob_buf;
  1301. ops.ooboffs = 0;
  1302. ops.datbuf = NULL;
  1303. @@ -359,7 +359,7 @@
  1304. data = (struct mtdswap_oobdata *)d->oob_buf;
  1305. data2 = (struct mtdswap_oobdata *)
  1306. - (d->oob_buf + d->mtd->ecclayout->oobavail);
  1307. + (d->oob_buf + d->mtd->oobavail);
  1308. if (le16_to_cpu(data->magic) == MTDSWAP_MAGIC_CLEAN) {
  1309. eb->erase_count = le32_to_cpu(data->count);
  1310. @@ -933,7 +933,7 @@
  1311. ops.mode = MTD_OPS_AUTO_OOB;
  1312. ops.len = mtd->writesize;
  1313. - ops.ooblen = mtd->ecclayout->oobavail;
  1314. + ops.ooblen = mtd->oobavail;
  1315. ops.ooboffs = 0;
  1316. ops.datbuf = d->page_buf;
  1317. ops.oobbuf = d->oob_buf;
  1318. @@ -945,7 +945,7 @@
  1319. for (i = 0; i < mtd_pages; i++) {
  1320. patt = mtdswap_test_patt(test + i);
  1321. memset(d->page_buf, patt, mtd->writesize);
  1322. - memset(d->oob_buf, patt, mtd->ecclayout->oobavail);
  1323. + memset(d->oob_buf, patt, mtd->oobavail);
  1324. ret = mtd_write_oob(mtd, pos, &ops);
  1325. if (ret)
  1326. goto error;
  1327. @@ -964,7 +964,7 @@
  1328. if (p1[j] != patt)
  1329. goto error;
  1330. - for (j = 0; j < mtd->ecclayout->oobavail; j++)
  1331. + for (j = 0; j < mtd->oobavail; j++)
  1332. if (p2[j] != (unsigned char)patt)
  1333. goto error;
  1334. @@ -1387,7 +1387,7 @@
  1335. if (!d->page_buf)
  1336. goto page_buf_fail;
  1337. - d->oob_buf = kmalloc(2 * mtd->ecclayout->oobavail, GFP_KERNEL);
  1338. + d->oob_buf = kmalloc(2 * mtd->oobavail, GFP_KERNEL);
  1339. if (!d->oob_buf)
  1340. goto oob_buf_fail;
  1341. @@ -1417,7 +1417,6 @@
  1342. unsigned long part;
  1343. unsigned int eblocks, eavailable, bad_blocks, spare_cnt;
  1344. uint64_t swap_size, use_size, size_limit;
  1345. - struct nand_ecclayout *oinfo;
  1346. int ret;
  1347. parts = &partitions[0];
  1348. @@ -1447,17 +1446,10 @@
  1349. return;
  1350. }
  1351. - oinfo = mtd->ecclayout;
  1352. - if (!oinfo) {
  1353. - printk(KERN_ERR "%s: mtd%d does not have OOB\n",
  1354. - MTDSWAP_PREFIX, mtd->index);
  1355. - return;
  1356. - }
  1357. -
  1358. - if (!mtd->oobsize || oinfo->oobavail < MTDSWAP_OOBSIZE) {
  1359. + if (!mtd->oobsize || mtd->oobavail < MTDSWAP_OOBSIZE) {
  1360. printk(KERN_ERR "%s: Not enough free bytes in OOB, "
  1361. "%d available, %zu needed.\n",
  1362. - MTDSWAP_PREFIX, oinfo->oobavail, MTDSWAP_OOBSIZE);
  1363. + MTDSWAP_PREFIX, mtd->oobavail, MTDSWAP_OOBSIZE);
  1364. return;
  1365. }
  1366. --- a/drivers/mtd/nand/Kconfig
  1367. +++ b/drivers/mtd/nand/Kconfig
  1368. @@ -55,7 +55,7 @@
  1369. config MTD_NAND_DENALI_DT
  1370. tristate "Support Denali NAND controller as a DT device"
  1371. select MTD_NAND_DENALI
  1372. - depends on HAS_DMA && HAVE_CLK
  1373. + depends on HAS_DMA && HAVE_CLK && OF
  1374. help
  1375. Enable the driver for NAND flash on platforms using a Denali NAND
  1376. controller as a DT device.
  1377. @@ -74,6 +74,7 @@
  1378. config MTD_NAND_GPIO
  1379. tristate "GPIO assisted NAND Flash driver"
  1380. depends on GPIOLIB || COMPILE_TEST
  1381. + depends on HAS_IOMEM
  1382. help
  1383. This enables a NAND flash driver where control signals are
  1384. connected to GPIO pins, and commands and data are communicated
  1385. @@ -310,6 +311,7 @@
  1386. config MTD_NAND_CS553X
  1387. tristate "NAND support for CS5535/CS5536 (AMD Geode companion chip)"
  1388. depends on X86_32
  1389. + depends on !UML && HAS_IOMEM
  1390. help
  1391. The CS553x companion chips for the AMD Geode processor
  1392. include NAND flash controllers with built-in hardware ECC
  1393. @@ -463,6 +465,7 @@
  1394. config MTD_NAND_VF610_NFC
  1395. tristate "Support for Freescale NFC for VF610/MPC5125"
  1396. depends on (SOC_VF610 || COMPILE_TEST)
  1397. + depends on HAS_IOMEM
  1398. help
  1399. Enables support for NAND Flash Controller on some Freescale
  1400. processors like the VF610, MPC5125, MCF54418 or Kinetis K70.
  1401. @@ -480,7 +483,7 @@
  1402. config MTD_NAND_SH_FLCTL
  1403. tristate "Support for NAND on Renesas SuperH FLCTL"
  1404. - depends on SUPERH || ARCH_SHMOBILE || COMPILE_TEST
  1405. + depends on SUPERH || COMPILE_TEST
  1406. depends on HAS_IOMEM
  1407. depends on HAS_DMA
  1408. help
  1409. @@ -519,6 +522,13 @@
  1410. help
  1411. Enables support for NAND Flash on JZ4740 SoC based boards.
  1412. +config MTD_NAND_JZ4780
  1413. + tristate "Support for NAND on JZ4780 SoC"
  1414. + depends on MACH_JZ4780 && JZ4780_NEMC
  1415. + help
  1416. + Enables support for NAND Flash connected to the NEMC on JZ4780 SoC
  1417. + based boards, using the BCH controller for hardware error correction.
  1418. +
  1419. config MTD_NAND_FSMC
  1420. tristate "Support for NAND on ST Micros FSMC"
  1421. depends on PLAT_SPEAR || ARCH_NOMADIK || ARCH_U8500 || MACH_U300
  1422. @@ -546,4 +556,11 @@
  1423. help
  1424. Enables support for NAND controller on Hisilicon SoC Hip04.
  1425. +config MTD_NAND_QCOM
  1426. + tristate "Support for NAND on QCOM SoCs"
  1427. + depends on ARCH_QCOM
  1428. + help
  1429. + Enables support for NAND flash chips on SoCs containing the EBI2 NAND
  1430. + controller. This controller is found on IPQ806x SoC.
  1431. +
  1432. endif # MTD_NAND
  1433. --- a/drivers/mtd/nand/Makefile
  1434. +++ b/drivers/mtd/nand/Makefile
  1435. @@ -49,11 +49,13 @@
  1436. obj-$(CONFIG_MTD_NAND_VF610_NFC) += vf610_nfc.o
  1437. obj-$(CONFIG_MTD_NAND_RICOH) += r852.o
  1438. obj-$(CONFIG_MTD_NAND_JZ4740) += jz4740_nand.o
  1439. +obj-$(CONFIG_MTD_NAND_JZ4780) += jz4780_nand.o jz4780_bch.o
  1440. obj-$(CONFIG_MTD_NAND_GPMI_NAND) += gpmi-nand/
  1441. obj-$(CONFIG_MTD_NAND_XWAY) += xway_nand.o
  1442. obj-$(CONFIG_MTD_NAND_BCM47XXNFLASH) += bcm47xxnflash/
  1443. obj-$(CONFIG_MTD_NAND_SUNXI) += sunxi_nand.o
  1444. obj-$(CONFIG_MTD_NAND_HISI504) += hisi504_nand.o
  1445. obj-$(CONFIG_MTD_NAND_BRCMNAND) += brcmnand/
  1446. +obj-$(CONFIG_MTD_NAND_QCOM) += qcom_nandc.o
  1447. nand-objs := nand_base.o nand_bbt.o nand_timings.o
  1448. --- a/drivers/mtd/nand/nand_base.c
  1449. +++ b/drivers/mtd/nand/nand_base.c
  1450. @@ -48,50 +48,6 @@
  1451. #include <linux/mtd/partitions.h>
  1452. #include <linux/of_mtd.h>
  1453. -/* Define default oob placement schemes for large and small page devices */
  1454. -static struct nand_ecclayout nand_oob_8 = {
  1455. - .eccbytes = 3,
  1456. - .eccpos = {0, 1, 2},
  1457. - .oobfree = {
  1458. - {.offset = 3,
  1459. - .length = 2},
  1460. - {.offset = 6,
  1461. - .length = 2} }
  1462. -};
  1463. -
  1464. -static struct nand_ecclayout nand_oob_16 = {
  1465. - .eccbytes = 6,
  1466. - .eccpos = {0, 1, 2, 3, 6, 7},
  1467. - .oobfree = {
  1468. - {.offset = 8,
  1469. - . length = 8} }
  1470. -};
  1471. -
  1472. -static struct nand_ecclayout nand_oob_64 = {
  1473. - .eccbytes = 24,
  1474. - .eccpos = {
  1475. - 40, 41, 42, 43, 44, 45, 46, 47,
  1476. - 48, 49, 50, 51, 52, 53, 54, 55,
  1477. - 56, 57, 58, 59, 60, 61, 62, 63},
  1478. - .oobfree = {
  1479. - {.offset = 2,
  1480. - .length = 38} }
  1481. -};
  1482. -
  1483. -static struct nand_ecclayout nand_oob_128 = {
  1484. - .eccbytes = 48,
  1485. - .eccpos = {
  1486. - 80, 81, 82, 83, 84, 85, 86, 87,
  1487. - 88, 89, 90, 91, 92, 93, 94, 95,
  1488. - 96, 97, 98, 99, 100, 101, 102, 103,
  1489. - 104, 105, 106, 107, 108, 109, 110, 111,
  1490. - 112, 113, 114, 115, 116, 117, 118, 119,
  1491. - 120, 121, 122, 123, 124, 125, 126, 127},
  1492. - .oobfree = {
  1493. - {.offset = 2,
  1494. - .length = 78} }
  1495. -};
  1496. -
  1497. static int nand_get_device(struct mtd_info *mtd, int new_state);
  1498. static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
  1499. @@ -103,10 +59,96 @@
  1500. */
  1501. DEFINE_LED_TRIGGER(nand_led_trigger);
  1502. +/* Define default oob placement schemes for large and small page devices */
  1503. +static int nand_ooblayout_ecc_sp(struct mtd_info *mtd, int section,
  1504. + struct mtd_oob_region *oobregion)
  1505. +{
  1506. + struct nand_chip *chip = mtd_to_nand(mtd);
  1507. + struct nand_ecc_ctrl *ecc = &chip->ecc;
  1508. +
  1509. + if (section > 1)
  1510. + return -ERANGE;
  1511. +
  1512. + if (!section) {
  1513. + oobregion->offset = 0;
  1514. + oobregion->length = 4;
  1515. + } else {
  1516. + oobregion->offset = 6;
  1517. + oobregion->length = ecc->total - 4;
  1518. + }
  1519. +
  1520. + return 0;
  1521. +}
  1522. +
  1523. +static int nand_ooblayout_free_sp(struct mtd_info *mtd, int section,
  1524. + struct mtd_oob_region *oobregion)
  1525. +{
  1526. + if (section > 1)
  1527. + return -ERANGE;
  1528. +
  1529. + if (mtd->oobsize == 16) {
  1530. + if (section)
  1531. + return -ERANGE;
  1532. +
  1533. + oobregion->length = 8;
  1534. + oobregion->offset = 8;
  1535. + } else {
  1536. + oobregion->length = 2;
  1537. + if (!section)
  1538. + oobregion->offset = 3;
  1539. + else
  1540. + oobregion->offset = 6;
  1541. + }
  1542. +
  1543. + return 0;
  1544. +}
  1545. +
  1546. +const struct mtd_ooblayout_ops nand_ooblayout_sp_ops = {
  1547. + .ecc = nand_ooblayout_ecc_sp,
  1548. + .free = nand_ooblayout_free_sp,
  1549. +};
  1550. +EXPORT_SYMBOL_GPL(nand_ooblayout_sp_ops);
  1551. +
  1552. +static int nand_ooblayout_ecc_lp(struct mtd_info *mtd, int section,
  1553. + struct mtd_oob_region *oobregion)
  1554. +{
  1555. + struct nand_chip *chip = mtd_to_nand(mtd);
  1556. + struct nand_ecc_ctrl *ecc = &chip->ecc;
  1557. +
  1558. + if (section)
  1559. + return -ERANGE;
  1560. +
  1561. + oobregion->length = ecc->total;
  1562. + oobregion->offset = mtd->oobsize - oobregion->length;
  1563. +
  1564. + return 0;
  1565. +}
  1566. +
  1567. +static int nand_ooblayout_free_lp(struct mtd_info *mtd, int section,
  1568. + struct mtd_oob_region *oobregion)
  1569. +{
  1570. + struct nand_chip *chip = mtd_to_nand(mtd);
  1571. + struct nand_ecc_ctrl *ecc = &chip->ecc;
  1572. +
  1573. + if (section)
  1574. + return -ERANGE;
  1575. +
  1576. + oobregion->length = mtd->oobsize - ecc->total - 2;
  1577. + oobregion->offset = 2;
  1578. +
  1579. + return 0;
  1580. +}
  1581. +
  1582. +const struct mtd_ooblayout_ops nand_ooblayout_lp_ops = {
  1583. + .ecc = nand_ooblayout_ecc_lp,
  1584. + .free = nand_ooblayout_free_lp,
  1585. +};
  1586. +EXPORT_SYMBOL_GPL(nand_ooblayout_lp_ops);
  1587. +
  1588. static int check_offs_len(struct mtd_info *mtd,
  1589. loff_t ofs, uint64_t len)
  1590. {
  1591. - struct nand_chip *chip = mtd->priv;
  1592. + struct nand_chip *chip = mtd_to_nand(mtd);
  1593. int ret = 0;
  1594. /* Start address must align on block boundary */
  1595. @@ -132,7 +174,7 @@
  1596. */
  1597. static void nand_release_device(struct mtd_info *mtd)
  1598. {
  1599. - struct nand_chip *chip = mtd->priv;
  1600. + struct nand_chip *chip = mtd_to_nand(mtd);
  1601. /* Release the controller and the chip */
  1602. spin_lock(&chip->controller->lock);
  1603. @@ -150,7 +192,7 @@
  1604. */
  1605. static uint8_t nand_read_byte(struct mtd_info *mtd)
  1606. {
  1607. - struct nand_chip *chip = mtd->priv;
  1608. + struct nand_chip *chip = mtd_to_nand(mtd);
  1609. return readb(chip->IO_ADDR_R);
  1610. }
  1611. @@ -163,7 +205,7 @@
  1612. */
  1613. static uint8_t nand_read_byte16(struct mtd_info *mtd)
  1614. {
  1615. - struct nand_chip *chip = mtd->priv;
  1616. + struct nand_chip *chip = mtd_to_nand(mtd);
  1617. return (uint8_t) cpu_to_le16(readw(chip->IO_ADDR_R));
  1618. }
  1619. @@ -175,7 +217,7 @@
  1620. */
  1621. static u16 nand_read_word(struct mtd_info *mtd)
  1622. {
  1623. - struct nand_chip *chip = mtd->priv;
  1624. + struct nand_chip *chip = mtd_to_nand(mtd);
  1625. return readw(chip->IO_ADDR_R);
  1626. }
  1627. @@ -188,7 +230,7 @@
  1628. */
  1629. static void nand_select_chip(struct mtd_info *mtd, int chipnr)
  1630. {
  1631. - struct nand_chip *chip = mtd->priv;
  1632. + struct nand_chip *chip = mtd_to_nand(mtd);
  1633. switch (chipnr) {
  1634. case -1:
  1635. @@ -211,7 +253,7 @@
  1636. */
  1637. static void nand_write_byte(struct mtd_info *mtd, uint8_t byte)
  1638. {
  1639. - struct nand_chip *chip = mtd->priv;
  1640. + struct nand_chip *chip = mtd_to_nand(mtd);
  1641. chip->write_buf(mtd, &byte, 1);
  1642. }
  1643. @@ -225,7 +267,7 @@
  1644. */
  1645. static void nand_write_byte16(struct mtd_info *mtd, uint8_t byte)
  1646. {
  1647. - struct nand_chip *chip = mtd->priv;
  1648. + struct nand_chip *chip = mtd_to_nand(mtd);
  1649. uint16_t word = byte;
  1650. /*
  1651. @@ -257,7 +299,7 @@
  1652. */
  1653. static void nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
  1654. {
  1655. - struct nand_chip *chip = mtd->priv;
  1656. + struct nand_chip *chip = mtd_to_nand(mtd);
  1657. iowrite8_rep(chip->IO_ADDR_W, buf, len);
  1658. }
  1659. @@ -272,7 +314,7 @@
  1660. */
  1661. static void nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
  1662. {
  1663. - struct nand_chip *chip = mtd->priv;
  1664. + struct nand_chip *chip = mtd_to_nand(mtd);
  1665. ioread8_rep(chip->IO_ADDR_R, buf, len);
  1666. }
  1667. @@ -287,7 +329,7 @@
  1668. */
  1669. static void nand_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
  1670. {
  1671. - struct nand_chip *chip = mtd->priv;
  1672. + struct nand_chip *chip = mtd_to_nand(mtd);
  1673. u16 *p = (u16 *) buf;
  1674. iowrite16_rep(chip->IO_ADDR_W, p, len >> 1);
  1675. @@ -303,7 +345,7 @@
  1676. */
  1677. static void nand_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len)
  1678. {
  1679. - struct nand_chip *chip = mtd->priv;
  1680. + struct nand_chip *chip = mtd_to_nand(mtd);
  1681. u16 *p = (u16 *) buf;
  1682. ioread16_rep(chip->IO_ADDR_R, p, len >> 1);
  1683. @@ -313,14 +355,13 @@
  1684. * nand_block_bad - [DEFAULT] Read bad block marker from the chip
  1685. * @mtd: MTD device structure
  1686. * @ofs: offset from device start
  1687. - * @getchip: 0, if the chip is already selected
  1688. *
  1689. * Check, if the block is bad.
  1690. */
  1691. -static int nand_block_bad(struct mtd_info *mtd, loff_t ofs, int getchip)
  1692. +static int nand_block_bad(struct mtd_info *mtd, loff_t ofs)
  1693. {
  1694. - int page, chipnr, res = 0, i = 0;
  1695. - struct nand_chip *chip = mtd->priv;
  1696. + int page, res = 0, i = 0;
  1697. + struct nand_chip *chip = mtd_to_nand(mtd);
  1698. u16 bad;
  1699. if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
  1700. @@ -328,15 +369,6 @@
  1701. page = (int)(ofs >> chip->page_shift) & chip->pagemask;
  1702. - if (getchip) {
  1703. - chipnr = (int)(ofs >> chip->chip_shift);
  1704. -
  1705. - nand_get_device(mtd, FL_READING);
  1706. -
  1707. - /* Select the NAND device */
  1708. - chip->select_chip(mtd, chipnr);
  1709. - }
  1710. -
  1711. do {
  1712. if (chip->options & NAND_BUSWIDTH_16) {
  1713. chip->cmdfunc(mtd, NAND_CMD_READOOB,
  1714. @@ -361,11 +393,6 @@
  1715. i++;
  1716. } while (!res && i < 2 && (chip->bbt_options & NAND_BBT_SCAN2NDPAGE));
  1717. - if (getchip) {
  1718. - chip->select_chip(mtd, -1);
  1719. - nand_release_device(mtd);
  1720. - }
  1721. -
  1722. return res;
  1723. }
  1724. @@ -380,7 +407,7 @@
  1725. */
  1726. static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
  1727. {
  1728. - struct nand_chip *chip = mtd->priv;
  1729. + struct nand_chip *chip = mtd_to_nand(mtd);
  1730. struct mtd_oob_ops ops;
  1731. uint8_t buf[2] = { 0, 0 };
  1732. int ret = 0, res, i = 0;
  1733. @@ -430,7 +457,7 @@
  1734. */
  1735. static int nand_block_markbad_lowlevel(struct mtd_info *mtd, loff_t ofs)
  1736. {
  1737. - struct nand_chip *chip = mtd->priv;
  1738. + struct nand_chip *chip = mtd_to_nand(mtd);
  1739. int res, ret = 0;
  1740. if (!(chip->bbt_options & NAND_BBT_NO_OOB_BBM)) {
  1741. @@ -471,7 +498,7 @@
  1742. */
  1743. static int nand_check_wp(struct mtd_info *mtd)
  1744. {
  1745. - struct nand_chip *chip = mtd->priv;
  1746. + struct nand_chip *chip = mtd_to_nand(mtd);
  1747. /* Broken xD cards report WP despite being writable */
  1748. if (chip->options & NAND_BROKEN_XD)
  1749. @@ -491,7 +518,7 @@
  1750. */
  1751. static int nand_block_isreserved(struct mtd_info *mtd, loff_t ofs)
  1752. {
  1753. - struct nand_chip *chip = mtd->priv;
  1754. + struct nand_chip *chip = mtd_to_nand(mtd);
  1755. if (!chip->bbt)
  1756. return 0;
  1757. @@ -503,19 +530,17 @@
  1758. * nand_block_checkbad - [GENERIC] Check if a block is marked bad
  1759. * @mtd: MTD device structure
  1760. * @ofs: offset from device start
  1761. - * @getchip: 0, if the chip is already selected
  1762. * @allowbbt: 1, if its allowed to access the bbt area
  1763. *
  1764. * Check, if the block is bad. Either by reading the bad block table or
  1765. * calling of the scan function.
  1766. */
  1767. -static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int getchip,
  1768. - int allowbbt)
  1769. +static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int allowbbt)
  1770. {
  1771. - struct nand_chip *chip = mtd->priv;
  1772. + struct nand_chip *chip = mtd_to_nand(mtd);
  1773. if (!chip->bbt)
  1774. - return chip->block_bad(mtd, ofs, getchip);
  1775. + return chip->block_bad(mtd, ofs);
  1776. /* Return info from the table */
  1777. return nand_isbad_bbt(mtd, ofs, allowbbt);
  1778. @@ -531,7 +556,7 @@
  1779. */
  1780. static void panic_nand_wait_ready(struct mtd_info *mtd, unsigned long timeo)
  1781. {
  1782. - struct nand_chip *chip = mtd->priv;
  1783. + struct nand_chip *chip = mtd_to_nand(mtd);
  1784. int i;
  1785. /* Wait for the device to get ready */
  1786. @@ -551,7 +576,7 @@
  1787. */
  1788. void nand_wait_ready(struct mtd_info *mtd)
  1789. {
  1790. - struct nand_chip *chip = mtd->priv;
  1791. + struct nand_chip *chip = mtd_to_nand(mtd);
  1792. unsigned long timeo = 400;
  1793. if (in_interrupt() || oops_in_progress)
  1794. @@ -566,8 +591,8 @@
  1795. cond_resched();
  1796. } while (time_before(jiffies, timeo));
  1797. - pr_warn_ratelimited(
  1798. - "timeout while waiting for chip to become ready\n");
  1799. + if (!chip->dev_ready(mtd))
  1800. + pr_warn_ratelimited("timeout while waiting for chip to become ready\n");
  1801. out:
  1802. led_trigger_event(nand_led_trigger, LED_OFF);
  1803. }
  1804. @@ -582,7 +607,7 @@
  1805. */
  1806. static void nand_wait_status_ready(struct mtd_info *mtd, unsigned long timeo)
  1807. {
  1808. - register struct nand_chip *chip = mtd->priv;
  1809. + register struct nand_chip *chip = mtd_to_nand(mtd);
  1810. timeo = jiffies + msecs_to_jiffies(timeo);
  1811. do {
  1812. @@ -605,7 +630,7 @@
  1813. static void nand_command(struct mtd_info *mtd, unsigned int command,
  1814. int column, int page_addr)
  1815. {
  1816. - register struct nand_chip *chip = mtd->priv;
  1817. + register struct nand_chip *chip = mtd_to_nand(mtd);
  1818. int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE;
  1819. /* Write out the command to the device */
  1820. @@ -708,7 +733,7 @@
  1821. static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
  1822. int column, int page_addr)
  1823. {
  1824. - register struct nand_chip *chip = mtd->priv;
  1825. + register struct nand_chip *chip = mtd_to_nand(mtd);
  1826. /* Emulate NAND_CMD_READOOB */
  1827. if (command == NAND_CMD_READOOB) {
  1828. @@ -832,7 +857,7 @@
  1829. static int
  1830. nand_get_device(struct mtd_info *mtd, int new_state)
  1831. {
  1832. - struct nand_chip *chip = mtd->priv;
  1833. + struct nand_chip *chip = mtd_to_nand(mtd);
  1834. spinlock_t *lock = &chip->controller->lock;
  1835. wait_queue_head_t *wq = &chip->controller->wq;
  1836. DECLARE_WAITQUEUE(wait, current);
  1837. @@ -952,7 +977,7 @@
  1838. {
  1839. int ret = 0;
  1840. int status, page;
  1841. - struct nand_chip *chip = mtd->priv;
  1842. + struct nand_chip *chip = mtd_to_nand(mtd);
  1843. /* Submit address of first page to unlock */
  1844. page = ofs >> chip->page_shift;
  1845. @@ -987,7 +1012,7 @@
  1846. {
  1847. int ret = 0;
  1848. int chipnr;
  1849. - struct nand_chip *chip = mtd->priv;
  1850. + struct nand_chip *chip = mtd_to_nand(mtd);
  1851. pr_debug("%s: start = 0x%012llx, len = %llu\n",
  1852. __func__, (unsigned long long)ofs, len);
  1853. @@ -1050,7 +1075,7 @@
  1854. {
  1855. int ret = 0;
  1856. int chipnr, status, page;
  1857. - struct nand_chip *chip = mtd->priv;
  1858. + struct nand_chip *chip = mtd_to_nand(mtd);
  1859. pr_debug("%s: start = 0x%012llx, len = %llu\n",
  1860. __func__, (unsigned long long)ofs, len);
  1861. @@ -1309,13 +1334,12 @@
  1862. static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
  1863. uint8_t *buf, int oob_required, int page)
  1864. {
  1865. - int i, eccsize = chip->ecc.size;
  1866. + int i, eccsize = chip->ecc.size, ret;
  1867. int eccbytes = chip->ecc.bytes;
  1868. int eccsteps = chip->ecc.steps;
  1869. uint8_t *p = buf;
  1870. uint8_t *ecc_calc = chip->buffers->ecccalc;
  1871. uint8_t *ecc_code = chip->buffers->ecccode;
  1872. - uint32_t *eccpos = chip->ecc.layout->eccpos;
  1873. unsigned int max_bitflips = 0;
  1874. chip->ecc.read_page_raw(mtd, chip, buf, 1, page);
  1875. @@ -1323,8 +1347,10 @@
  1876. for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
  1877. chip->ecc.calculate(mtd, p, &ecc_calc[i]);
  1878. - for (i = 0; i < chip->ecc.total; i++)
  1879. - ecc_code[i] = chip->oob_poi[eccpos[i]];
  1880. + ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
  1881. + chip->ecc.total);
  1882. + if (ret)
  1883. + return ret;
  1884. eccsteps = chip->ecc.steps;
  1885. p = buf;
  1886. @@ -1356,14 +1382,14 @@
  1887. uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi,
  1888. int page)
  1889. {
  1890. - int start_step, end_step, num_steps;
  1891. - uint32_t *eccpos = chip->ecc.layout->eccpos;
  1892. + int start_step, end_step, num_steps, ret;
  1893. uint8_t *p;
  1894. int data_col_addr, i, gaps = 0;
  1895. int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
  1896. int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
  1897. - int index;
  1898. + int index, section = 0;
  1899. unsigned int max_bitflips = 0;
  1900. + struct mtd_oob_region oobregion = { };
  1901. /* Column address within the page aligned to ECC size (256bytes) */
  1902. start_step = data_offs / chip->ecc.size;
  1903. @@ -1391,12 +1417,13 @@
  1904. * The performance is faster if we position offsets according to
  1905. * ecc.pos. Let's make sure that there are no gaps in ECC positions.
  1906. */
  1907. - for (i = 0; i < eccfrag_len - 1; i++) {
  1908. - if (eccpos[i + index] + 1 != eccpos[i + index + 1]) {
  1909. - gaps = 1;
  1910. - break;
  1911. - }
  1912. - }
  1913. + ret = mtd_ooblayout_find_eccregion(mtd, index, &section, &oobregion);
  1914. + if (ret)
  1915. + return ret;
  1916. +
  1917. + if (oobregion.length < eccfrag_len)
  1918. + gaps = 1;
  1919. +
  1920. if (gaps) {
  1921. chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
  1922. chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
  1923. @@ -1405,20 +1432,23 @@
  1924. * Send the command to read the particular ECC bytes take care
  1925. * about buswidth alignment in read_buf.
  1926. */
  1927. - aligned_pos = eccpos[index] & ~(busw - 1);
  1928. + aligned_pos = oobregion.offset & ~(busw - 1);
  1929. aligned_len = eccfrag_len;
  1930. - if (eccpos[index] & (busw - 1))
  1931. + if (oobregion.offset & (busw - 1))
  1932. aligned_len++;
  1933. - if (eccpos[index + (num_steps * chip->ecc.bytes)] & (busw - 1))
  1934. + if ((oobregion.offset + (num_steps * chip->ecc.bytes)) &
  1935. + (busw - 1))
  1936. aligned_len++;
  1937. chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
  1938. - mtd->writesize + aligned_pos, -1);
  1939. + mtd->writesize + aligned_pos, -1);
  1940. chip->read_buf(mtd, &chip->oob_poi[aligned_pos], aligned_len);
  1941. }
  1942. - for (i = 0; i < eccfrag_len; i++)
  1943. - chip->buffers->ecccode[i] = chip->oob_poi[eccpos[i + index]];
  1944. + ret = mtd_ooblayout_get_eccbytes(mtd, chip->buffers->ecccode,
  1945. + chip->oob_poi, index, eccfrag_len);
  1946. + if (ret)
  1947. + return ret;
  1948. p = bufpoi + data_col_addr;
  1949. for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
  1950. @@ -1426,6 +1456,16 @@
  1951. stat = chip->ecc.correct(mtd, p,
  1952. &chip->buffers->ecccode[i], &chip->buffers->ecccalc[i]);
  1953. + if (stat == -EBADMSG &&
  1954. + (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
  1955. + /* check for empty pages with bitflips */
  1956. + stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
  1957. + &chip->buffers->ecccode[i],
  1958. + chip->ecc.bytes,
  1959. + NULL, 0,
  1960. + chip->ecc.strength);
  1961. + }
  1962. +
  1963. if (stat < 0) {
  1964. mtd->ecc_stats.failed++;
  1965. } else {
  1966. @@ -1449,13 +1489,12 @@
  1967. static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
  1968. uint8_t *buf, int oob_required, int page)
  1969. {
  1970. - int i, eccsize = chip->ecc.size;
  1971. + int i, eccsize = chip->ecc.size, ret;
  1972. int eccbytes = chip->ecc.bytes;
  1973. int eccsteps = chip->ecc.steps;
  1974. uint8_t *p = buf;
  1975. uint8_t *ecc_calc = chip->buffers->ecccalc;
  1976. uint8_t *ecc_code = chip->buffers->ecccode;
  1977. - uint32_t *eccpos = chip->ecc.layout->eccpos;
  1978. unsigned int max_bitflips = 0;
  1979. for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
  1980. @@ -1465,8 +1504,10 @@
  1981. }
  1982. chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
  1983. - for (i = 0; i < chip->ecc.total; i++)
  1984. - ecc_code[i] = chip->oob_poi[eccpos[i]];
  1985. + ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
  1986. + chip->ecc.total);
  1987. + if (ret)
  1988. + return ret;
  1989. eccsteps = chip->ecc.steps;
  1990. p = buf;
  1991. @@ -1475,6 +1516,15 @@
  1992. int stat;
  1993. stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
  1994. + if (stat == -EBADMSG &&
  1995. + (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
  1996. + /* check for empty pages with bitflips */
  1997. + stat = nand_check_erased_ecc_chunk(p, eccsize,
  1998. + &ecc_code[i], eccbytes,
  1999. + NULL, 0,
  2000. + chip->ecc.strength);
  2001. + }
  2002. +
  2003. if (stat < 0) {
  2004. mtd->ecc_stats.failed++;
  2005. } else {
  2006. @@ -1502,12 +1552,11 @@
  2007. static int nand_read_page_hwecc_oob_first(struct mtd_info *mtd,
  2008. struct nand_chip *chip, uint8_t *buf, int oob_required, int page)
  2009. {
  2010. - int i, eccsize = chip->ecc.size;
  2011. + int i, eccsize = chip->ecc.size, ret;
  2012. int eccbytes = chip->ecc.bytes;
  2013. int eccsteps = chip->ecc.steps;
  2014. uint8_t *p = buf;
  2015. uint8_t *ecc_code = chip->buffers->ecccode;
  2016. - uint32_t *eccpos = chip->ecc.layout->eccpos;
  2017. uint8_t *ecc_calc = chip->buffers->ecccalc;
  2018. unsigned int max_bitflips = 0;
  2019. @@ -1516,8 +1565,10 @@
  2020. chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
  2021. chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
  2022. - for (i = 0; i < chip->ecc.total; i++)
  2023. - ecc_code[i] = chip->oob_poi[eccpos[i]];
  2024. + ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
  2025. + chip->ecc.total);
  2026. + if (ret)
  2027. + return ret;
  2028. for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
  2029. int stat;
  2030. @@ -1527,6 +1578,15 @@
  2031. chip->ecc.calculate(mtd, p, &ecc_calc[i]);
  2032. stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL);
  2033. + if (stat == -EBADMSG &&
  2034. + (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
  2035. + /* check for empty pages with bitflips */
  2036. + stat = nand_check_erased_ecc_chunk(p, eccsize,
  2037. + &ecc_code[i], eccbytes,
  2038. + NULL, 0,
  2039. + chip->ecc.strength);
  2040. + }
  2041. +
  2042. if (stat < 0) {
  2043. mtd->ecc_stats.failed++;
  2044. } else {
  2045. @@ -1554,6 +1614,7 @@
  2046. int i, eccsize = chip->ecc.size;
  2047. int eccbytes = chip->ecc.bytes;
  2048. int eccsteps = chip->ecc.steps;
  2049. + int eccpadbytes = eccbytes + chip->ecc.prepad + chip->ecc.postpad;
  2050. uint8_t *p = buf;
  2051. uint8_t *oob = chip->oob_poi;
  2052. unsigned int max_bitflips = 0;
  2053. @@ -1573,19 +1634,29 @@
  2054. chip->read_buf(mtd, oob, eccbytes);
  2055. stat = chip->ecc.correct(mtd, p, oob, NULL);
  2056. - if (stat < 0) {
  2057. - mtd->ecc_stats.failed++;
  2058. - } else {
  2059. - mtd->ecc_stats.corrected += stat;
  2060. - max_bitflips = max_t(unsigned int, max_bitflips, stat);
  2061. - }
  2062. -
  2063. oob += eccbytes;
  2064. if (chip->ecc.postpad) {
  2065. chip->read_buf(mtd, oob, chip->ecc.postpad);
  2066. oob += chip->ecc.postpad;
  2067. }
  2068. +
  2069. + if (stat == -EBADMSG &&
  2070. + (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
  2071. + /* check for empty pages with bitflips */
  2072. + stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
  2073. + oob - eccpadbytes,
  2074. + eccpadbytes,
  2075. + NULL, 0,
  2076. + chip->ecc.strength);
  2077. + }
  2078. +
  2079. + if (stat < 0) {
  2080. + mtd->ecc_stats.failed++;
  2081. + } else {
  2082. + mtd->ecc_stats.corrected += stat;
  2083. + max_bitflips = max_t(unsigned int, max_bitflips, stat);
  2084. + }
  2085. }
  2086. /* Calculate remaining oob bytes */
  2087. @@ -1598,14 +1669,17 @@
  2088. /**
  2089. * nand_transfer_oob - [INTERN] Transfer oob to client buffer
  2090. - * @chip: nand chip structure
  2091. + * @mtd: mtd info structure
  2092. * @oob: oob destination address
  2093. * @ops: oob ops structure
  2094. * @len: size of oob to transfer
  2095. */
  2096. -static uint8_t *nand_transfer_oob(struct nand_chip *chip, uint8_t *oob,
  2097. +static uint8_t *nand_transfer_oob(struct mtd_info *mtd, uint8_t *oob,
  2098. struct mtd_oob_ops *ops, size_t len)
  2099. {
  2100. + struct nand_chip *chip = mtd_to_nand(mtd);
  2101. + int ret;
  2102. +
  2103. switch (ops->mode) {
  2104. case MTD_OPS_PLACE_OOB:
  2105. @@ -1613,31 +1687,12 @@
  2106. memcpy(oob, chip->oob_poi + ops->ooboffs, len);
  2107. return oob + len;
  2108. - case MTD_OPS_AUTO_OOB: {
  2109. - struct nand_oobfree *free = chip->ecc.layout->oobfree;
  2110. - uint32_t boffs = 0, roffs = ops->ooboffs;
  2111. - size_t bytes = 0;
  2112. -
  2113. - for (; free->length && len; free++, len -= bytes) {
  2114. - /* Read request not from offset 0? */
  2115. - if (unlikely(roffs)) {
  2116. - if (roffs >= free->length) {
  2117. - roffs -= free->length;
  2118. - continue;
  2119. - }
  2120. - boffs = free->offset + roffs;
  2121. - bytes = min_t(size_t, len,
  2122. - (free->length - roffs));
  2123. - roffs = 0;
  2124. - } else {
  2125. - bytes = min_t(size_t, len, free->length);
  2126. - boffs = free->offset;
  2127. - }
  2128. - memcpy(oob, chip->oob_poi + boffs, bytes);
  2129. - oob += bytes;
  2130. - }
  2131. - return oob;
  2132. - }
  2133. + case MTD_OPS_AUTO_OOB:
  2134. + ret = mtd_ooblayout_get_databytes(mtd, oob, chip->oob_poi,
  2135. + ops->ooboffs, len);
  2136. + BUG_ON(ret);
  2137. + return oob + len;
  2138. +
  2139. default:
  2140. BUG();
  2141. }
  2142. @@ -1655,7 +1710,7 @@
  2143. */
  2144. static int nand_setup_read_retry(struct mtd_info *mtd, int retry_mode)
  2145. {
  2146. - struct nand_chip *chip = mtd->priv;
  2147. + struct nand_chip *chip = mtd_to_nand(mtd);
  2148. pr_debug("setting READ RETRY mode %d\n", retry_mode);
  2149. @@ -1680,12 +1735,11 @@
  2150. struct mtd_oob_ops *ops)
  2151. {
  2152. int chipnr, page, realpage, col, bytes, aligned, oob_required;
  2153. - struct nand_chip *chip = mtd->priv;
  2154. + struct nand_chip *chip = mtd_to_nand(mtd);
  2155. int ret = 0;
  2156. uint32_t readlen = ops->len;
  2157. uint32_t oobreadlen = ops->ooblen;
  2158. - uint32_t max_oobsize = ops->mode == MTD_OPS_AUTO_OOB ?
  2159. - mtd->oobavail : mtd->oobsize;
  2160. + uint32_t max_oobsize = mtd_oobavail(mtd, ops);
  2161. uint8_t *bufpoi, *oob, *buf;
  2162. int use_bufpoi;
  2163. @@ -1772,7 +1826,7 @@
  2164. int toread = min(oobreadlen, max_oobsize);
  2165. if (toread) {
  2166. - oob = nand_transfer_oob(chip,
  2167. + oob = nand_transfer_oob(mtd,
  2168. oob, ops, toread);
  2169. oobreadlen -= toread;
  2170. }
  2171. @@ -2024,7 +2078,7 @@
  2172. struct mtd_oob_ops *ops)
  2173. {
  2174. int page, realpage, chipnr;
  2175. - struct nand_chip *chip = mtd->priv;
  2176. + struct nand_chip *chip = mtd_to_nand(mtd);
  2177. struct mtd_ecc_stats stats;
  2178. int readlen = ops->ooblen;
  2179. int len;
  2180. @@ -2036,10 +2090,7 @@
  2181. stats = mtd->ecc_stats;
  2182. - if (ops->mode == MTD_OPS_AUTO_OOB)
  2183. - len = chip->ecc.layout->oobavail;
  2184. - else
  2185. - len = mtd->oobsize;
  2186. + len = mtd_oobavail(mtd, ops);
  2187. if (unlikely(ops->ooboffs >= len)) {
  2188. pr_debug("%s: attempt to start read outside oob\n",
  2189. @@ -2073,7 +2124,7 @@
  2190. break;
  2191. len = min(len, readlen);
  2192. - buf = nand_transfer_oob(chip, buf, ops, len);
  2193. + buf = nand_transfer_oob(mtd, buf, ops, len);
  2194. if (chip->options & NAND_NEED_READRDY) {
  2195. /* Apply delay or wait for ready/busy pin */
  2196. @@ -2232,19 +2283,20 @@
  2197. const uint8_t *buf, int oob_required,
  2198. int page)
  2199. {
  2200. - int i, eccsize = chip->ecc.size;
  2201. + int i, eccsize = chip->ecc.size, ret;
  2202. int eccbytes = chip->ecc.bytes;
  2203. int eccsteps = chip->ecc.steps;
  2204. uint8_t *ecc_calc = chip->buffers->ecccalc;
  2205. const uint8_t *p = buf;
  2206. - uint32_t *eccpos = chip->ecc.layout->eccpos;
  2207. /* Software ECC calculation */
  2208. for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
  2209. chip->ecc.calculate(mtd, p, &ecc_calc[i]);
  2210. - for (i = 0; i < chip->ecc.total; i++)
  2211. - chip->oob_poi[eccpos[i]] = ecc_calc[i];
  2212. + ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
  2213. + chip->ecc.total);
  2214. + if (ret)
  2215. + return ret;
  2216. return chip->ecc.write_page_raw(mtd, chip, buf, 1, page);
  2217. }
  2218. @@ -2261,12 +2313,11 @@
  2219. const uint8_t *buf, int oob_required,
  2220. int page)
  2221. {
  2222. - int i, eccsize = chip->ecc.size;
  2223. + int i, eccsize = chip->ecc.size, ret;
  2224. int eccbytes = chip->ecc.bytes;
  2225. int eccsteps = chip->ecc.steps;
  2226. uint8_t *ecc_calc = chip->buffers->ecccalc;
  2227. const uint8_t *p = buf;
  2228. - uint32_t *eccpos = chip->ecc.layout->eccpos;
  2229. for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
  2230. chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
  2231. @@ -2274,8 +2325,10 @@
  2232. chip->ecc.calculate(mtd, p, &ecc_calc[i]);
  2233. }
  2234. - for (i = 0; i < chip->ecc.total; i++)
  2235. - chip->oob_poi[eccpos[i]] = ecc_calc[i];
  2236. + ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
  2237. + chip->ecc.total);
  2238. + if (ret)
  2239. + return ret;
  2240. chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
  2241. @@ -2303,11 +2356,10 @@
  2242. int ecc_size = chip->ecc.size;
  2243. int ecc_bytes = chip->ecc.bytes;
  2244. int ecc_steps = chip->ecc.steps;
  2245. - uint32_t *eccpos = chip->ecc.layout->eccpos;
  2246. uint32_t start_step = offset / ecc_size;
  2247. uint32_t end_step = (offset + data_len - 1) / ecc_size;
  2248. int oob_bytes = mtd->oobsize / ecc_steps;
  2249. - int step, i;
  2250. + int step, ret;
  2251. for (step = 0; step < ecc_steps; step++) {
  2252. /* configure controller for WRITE access */
  2253. @@ -2335,8 +2387,10 @@
  2254. /* copy calculated ECC for whole page to chip->buffer->oob */
  2255. /* this include masked-value(0xFF) for unwritten subpages */
  2256. ecc_calc = chip->buffers->ecccalc;
  2257. - for (i = 0; i < chip->ecc.total; i++)
  2258. - chip->oob_poi[eccpos[i]] = ecc_calc[i];
  2259. + ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
  2260. + chip->ecc.total);
  2261. + if (ret)
  2262. + return ret;
  2263. /* write OOB buffer to NAND device */
  2264. chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
  2265. @@ -2472,7 +2526,8 @@
  2266. static uint8_t *nand_fill_oob(struct mtd_info *mtd, uint8_t *oob, size_t len,
  2267. struct mtd_oob_ops *ops)
  2268. {
  2269. - struct nand_chip *chip = mtd->priv;
  2270. + struct nand_chip *chip = mtd_to_nand(mtd);
  2271. + int ret;
  2272. /*
  2273. * Initialise to all 0xFF, to avoid the possibility of left over OOB
  2274. @@ -2487,31 +2542,12 @@
  2275. memcpy(chip->oob_poi + ops->ooboffs, oob, len);
  2276. return oob + len;
  2277. - case MTD_OPS_AUTO_OOB: {
  2278. - struct nand_oobfree *free = chip->ecc.layout->oobfree;
  2279. - uint32_t boffs = 0, woffs = ops->ooboffs;
  2280. - size_t bytes = 0;
  2281. -
  2282. - for (; free->length && len; free++, len -= bytes) {
  2283. - /* Write request not from offset 0? */
  2284. - if (unlikely(woffs)) {
  2285. - if (woffs >= free->length) {
  2286. - woffs -= free->length;
  2287. - continue;
  2288. - }
  2289. - boffs = free->offset + woffs;
  2290. - bytes = min_t(size_t, len,
  2291. - (free->length - woffs));
  2292. - woffs = 0;
  2293. - } else {
  2294. - bytes = min_t(size_t, len, free->length);
  2295. - boffs = free->offset;
  2296. - }
  2297. - memcpy(chip->oob_poi + boffs, oob, bytes);
  2298. - oob += bytes;
  2299. - }
  2300. - return oob;
  2301. - }
  2302. + case MTD_OPS_AUTO_OOB:
  2303. + ret = mtd_ooblayout_set_databytes(mtd, oob, chip->oob_poi,
  2304. + ops->ooboffs, len);
  2305. + BUG_ON(ret);
  2306. + return oob + len;
  2307. +
  2308. default:
  2309. BUG();
  2310. }
  2311. @@ -2532,12 +2568,11 @@
  2312. struct mtd_oob_ops *ops)
  2313. {
  2314. int chipnr, realpage, page, blockmask, column;
  2315. - struct nand_chip *chip = mtd->priv;
  2316. + struct nand_chip *chip = mtd_to_nand(mtd);
  2317. uint32_t writelen = ops->len;
  2318. uint32_t oobwritelen = ops->ooblen;
  2319. - uint32_t oobmaxlen = ops->mode == MTD_OPS_AUTO_OOB ?
  2320. - mtd->oobavail : mtd->oobsize;
  2321. + uint32_t oobmaxlen = mtd_oobavail(mtd, ops);
  2322. uint8_t *oob = ops->oobbuf;
  2323. uint8_t *buf = ops->datbuf;
  2324. @@ -2662,7 +2697,7 @@
  2325. static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
  2326. size_t *retlen, const uint8_t *buf)
  2327. {
  2328. - struct nand_chip *chip = mtd->priv;
  2329. + struct nand_chip *chip = mtd_to_nand(mtd);
  2330. struct mtd_oob_ops ops;
  2331. int ret;
  2332. @@ -2722,15 +2757,12 @@
  2333. struct mtd_oob_ops *ops)
  2334. {
  2335. int chipnr, page, status, len;
  2336. - struct nand_chip *chip = mtd->priv;
  2337. + struct nand_chip *chip = mtd_to_nand(mtd);
  2338. pr_debug("%s: to = 0x%08x, len = %i\n",
  2339. __func__, (unsigned int)to, (int)ops->ooblen);
  2340. - if (ops->mode == MTD_OPS_AUTO_OOB)
  2341. - len = chip->ecc.layout->oobavail;
  2342. - else
  2343. - len = mtd->oobsize;
  2344. + len = mtd_oobavail(mtd, ops);
  2345. /* Do not allow write past end of page */
  2346. if ((ops->ooboffs + ops->ooblen) > len) {
  2347. @@ -2847,7 +2879,7 @@
  2348. */
  2349. static int single_erase(struct mtd_info *mtd, int page)
  2350. {
  2351. - struct nand_chip *chip = mtd->priv;
  2352. + struct nand_chip *chip = mtd_to_nand(mtd);
  2353. /* Send commands to erase a block */
  2354. chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
  2355. chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
  2356. @@ -2879,7 +2911,7 @@
  2357. int allowbbt)
  2358. {
  2359. int page, status, pages_per_block, ret, chipnr;
  2360. - struct nand_chip *chip = mtd->priv;
  2361. + struct nand_chip *chip = mtd_to_nand(mtd);
  2362. loff_t len;
  2363. pr_debug("%s: start = 0x%012llx, len = %llu\n",
  2364. @@ -2918,7 +2950,7 @@
  2365. while (len) {
  2366. /* Check if we have a bad block, we do not erase bad blocks! */
  2367. if (nand_block_checkbad(mtd, ((loff_t) page) <<
  2368. - chip->page_shift, 0, allowbbt)) {
  2369. + chip->page_shift, allowbbt)) {
  2370. pr_warn("%s: attempt to erase a bad block at page 0x%08x\n",
  2371. __func__, page);
  2372. instr->state = MTD_ERASE_FAILED;
  2373. @@ -3005,7 +3037,20 @@
  2374. */
  2375. static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
  2376. {
  2377. - return nand_block_checkbad(mtd, offs, 1, 0);
  2378. + struct nand_chip *chip = mtd_to_nand(mtd);
  2379. + int chipnr = (int)(offs >> chip->chip_shift);
  2380. + int ret;
  2381. +
  2382. + /* Select the NAND device */
  2383. + nand_get_device(mtd, FL_READING);
  2384. + chip->select_chip(mtd, chipnr);
  2385. +
  2386. + ret = nand_block_checkbad(mtd, offs, 0);
  2387. +
  2388. + chip->select_chip(mtd, -1);
  2389. + nand_release_device(mtd);
  2390. +
  2391. + return ret;
  2392. }
  2393. /**
  2394. @@ -3094,7 +3139,7 @@
  2395. */
  2396. static void nand_resume(struct mtd_info *mtd)
  2397. {
  2398. - struct nand_chip *chip = mtd->priv;
  2399. + struct nand_chip *chip = mtd_to_nand(mtd);
  2400. if (chip->state == FL_PM_SUSPENDED)
  2401. nand_release_device(mtd);
  2402. @@ -3266,7 +3311,7 @@
  2403. static int nand_setup_read_retry_micron(struct mtd_info *mtd, int retry_mode)
  2404. {
  2405. - struct nand_chip *chip = mtd->priv;
  2406. + struct nand_chip *chip = mtd_to_nand(mtd);
  2407. uint8_t feature[ONFI_SUBFEATURE_PARAM_LEN] = {retry_mode};
  2408. return chip->onfi_set_features(mtd, chip, ONFI_FEATURE_ADDR_READ_RETRY,
  2409. @@ -3937,10 +3982,13 @@
  2410. return type;
  2411. }
  2412. -static int nand_dt_init(struct mtd_info *mtd, struct nand_chip *chip,
  2413. - struct device_node *dn)
  2414. +static int nand_dt_init(struct nand_chip *chip)
  2415. {
  2416. - int ecc_mode, ecc_strength, ecc_step;
  2417. + struct device_node *dn = nand_get_flash_node(chip);
  2418. + int ecc_mode, ecc_algo, ecc_strength, ecc_step;
  2419. +
  2420. + if (!dn)
  2421. + return 0;
  2422. if (of_get_nand_bus_width(dn) == 16)
  2423. chip->options |= NAND_BUSWIDTH_16;
  2424. @@ -3949,6 +3997,7 @@
  2425. chip->bbt_options |= NAND_BBT_USE_FLASH;
  2426. ecc_mode = of_get_nand_ecc_mode(dn);
  2427. + ecc_algo = of_get_nand_ecc_algo(dn);
  2428. ecc_strength = of_get_nand_ecc_strength(dn);
  2429. ecc_step = of_get_nand_ecc_step_size(dn);
  2430. @@ -3961,6 +4010,9 @@
  2431. if (ecc_mode >= 0)
  2432. chip->ecc.mode = ecc_mode;
  2433. + if (ecc_algo >= 0)
  2434. + chip->ecc.algo = ecc_algo;
  2435. +
  2436. if (ecc_strength >= 0)
  2437. chip->ecc.strength = ecc_strength;
  2438. @@ -3984,15 +4036,16 @@
  2439. struct nand_flash_dev *table)
  2440. {
  2441. int i, nand_maf_id, nand_dev_id;
  2442. - struct nand_chip *chip = mtd->priv;
  2443. + struct nand_chip *chip = mtd_to_nand(mtd);
  2444. struct nand_flash_dev *type;
  2445. int ret;
  2446. - if (chip->flash_node) {
  2447. - ret = nand_dt_init(mtd, chip, chip->flash_node);
  2448. - if (ret)
  2449. - return ret;
  2450. - }
  2451. + ret = nand_dt_init(chip);
  2452. + if (ret)
  2453. + return ret;
  2454. +
  2455. + if (!mtd->name && mtd->dev.parent)
  2456. + mtd->name = dev_name(mtd->dev.parent);
  2457. if (!mtd->name && mtd->dev.parent)
  2458. mtd->name = dev_name(mtd->dev.parent);
  2459. @@ -4055,7 +4108,7 @@
  2460. */
  2461. static bool nand_ecc_strength_good(struct mtd_info *mtd)
  2462. {
  2463. - struct nand_chip *chip = mtd->priv;
  2464. + struct nand_chip *chip = mtd_to_nand(mtd);
  2465. struct nand_ecc_ctrl *ecc = &chip->ecc;
  2466. int corr, ds_corr;
  2467. @@ -4083,10 +4136,10 @@
  2468. */
  2469. int nand_scan_tail(struct mtd_info *mtd)
  2470. {
  2471. - int i;
  2472. - struct nand_chip *chip = mtd->priv;
  2473. + struct nand_chip *chip = mtd_to_nand(mtd);
  2474. struct nand_ecc_ctrl *ecc = &chip->ecc;
  2475. struct nand_buffers *nbuf;
  2476. + int ret;
  2477. /* New bad blocks should be marked in OOB, flash-based BBT, or both */
  2478. BUG_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) &&
  2479. @@ -4113,19 +4166,15 @@
  2480. /*
  2481. * If no default placement scheme is given, select an appropriate one.
  2482. */
  2483. - if (!ecc->layout && (ecc->mode != NAND_ECC_SOFT_BCH)) {
  2484. + if (!mtd->ooblayout && (ecc->mode != NAND_ECC_SOFT_BCH)) {
  2485. switch (mtd->oobsize) {
  2486. case 8:
  2487. - ecc->layout = &nand_oob_8;
  2488. - break;
  2489. case 16:
  2490. - ecc->layout = &nand_oob_16;
  2491. + mtd_set_ooblayout(mtd, &nand_ooblayout_sp_ops);
  2492. break;
  2493. case 64:
  2494. - ecc->layout = &nand_oob_64;
  2495. - break;
  2496. case 128:
  2497. - ecc->layout = &nand_oob_128;
  2498. + mtd_set_ooblayout(mtd, &nand_ooblayout_lp_ops);
  2499. break;
  2500. default:
  2501. pr_warn("No oob scheme defined for oobsize %d\n",
  2502. @@ -4168,7 +4217,7 @@
  2503. ecc->write_oob = nand_write_oob_std;
  2504. if (!ecc->read_subpage)
  2505. ecc->read_subpage = nand_read_subpage;
  2506. - if (!ecc->write_subpage)
  2507. + if (!ecc->write_subpage && ecc->hwctl && ecc->calculate)
  2508. ecc->write_subpage = nand_write_subpage_hwecc;
  2509. case NAND_ECC_HW_SYNDROME:
  2510. @@ -4246,10 +4295,8 @@
  2511. }
  2512. /* See nand_bch_init() for details. */
  2513. - ecc->bytes = DIV_ROUND_UP(
  2514. - ecc->strength * fls(8 * ecc->size), 8);
  2515. - ecc->priv = nand_bch_init(mtd, ecc->size, ecc->bytes,
  2516. - &ecc->layout);
  2517. + ecc->bytes = 0;
  2518. + ecc->priv = nand_bch_init(mtd);
  2519. if (!ecc->priv) {
  2520. pr_warn("BCH ECC initialization failed!\n");
  2521. BUG();
  2522. @@ -4280,20 +4327,9 @@
  2523. if (!ecc->write_oob_raw)
  2524. ecc->write_oob_raw = ecc->write_oob;
  2525. - /*
  2526. - * The number of bytes available for a client to place data into
  2527. - * the out of band area.
  2528. - */
  2529. - ecc->layout->oobavail = 0;
  2530. - for (i = 0; ecc->layout->oobfree[i].length
  2531. - && i < ARRAY_SIZE(ecc->layout->oobfree); i++)
  2532. - ecc->layout->oobavail += ecc->layout->oobfree[i].length;
  2533. - mtd->oobavail = ecc->layout->oobavail;
  2534. -
  2535. - /* ECC sanity check: warn if it's too weak */
  2536. - if (!nand_ecc_strength_good(mtd))
  2537. - pr_warn("WARNING: %s: the ECC used on your system is too weak compared to the one required by the NAND chip\n",
  2538. - mtd->name);
  2539. + /* propagate ecc info to mtd_info */
  2540. + mtd->ecc_strength = ecc->strength;
  2541. + mtd->ecc_step_size = ecc->size;
  2542. /*
  2543. * Set the number of read / write steps for one page depending on ECC
  2544. @@ -4306,6 +4342,21 @@
  2545. }
  2546. ecc->total = ecc->steps * ecc->bytes;
  2547. + /*
  2548. + * The number of bytes available for a client to place data into
  2549. + * the out of band area.
  2550. + */
  2551. + ret = mtd_ooblayout_count_freebytes(mtd);
  2552. + if (ret < 0)
  2553. + ret = 0;
  2554. +
  2555. + mtd->oobavail = ret;
  2556. +
  2557. + /* ECC sanity check: warn if it's too weak */
  2558. + if (!nand_ecc_strength_good(mtd))
  2559. + pr_warn("WARNING: %s: the ECC used on your system is too weak compared to the one required by the NAND chip\n",
  2560. + mtd->name);
  2561. +
  2562. /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */
  2563. if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && nand_is_slc(chip)) {
  2564. switch (ecc->steps) {
  2565. @@ -4362,10 +4413,6 @@
  2566. mtd->_block_markbad = nand_block_markbad;
  2567. mtd->writebufsize = mtd->writesize;
  2568. - /* propagate ecc info to mtd_info */
  2569. - mtd->ecclayout = ecc->layout;
  2570. - mtd->ecc_strength = ecc->strength;
  2571. - mtd->ecc_step_size = ecc->size;
  2572. /*
  2573. * Initialize bitflip_threshold to its default prior scan_bbt() call.
  2574. * scan_bbt() might invoke mtd_read(), thus bitflip_threshold must be
  2575. @@ -4421,7 +4468,7 @@
  2576. */
  2577. void nand_release(struct mtd_info *mtd)
  2578. {
  2579. - struct nand_chip *chip = mtd->priv;
  2580. + struct nand_chip *chip = mtd_to_nand(mtd);
  2581. if (chip->ecc.mode == NAND_ECC_SOFT_BCH)
  2582. nand_bch_free((struct nand_bch_control *)chip->ecc.priv);
  2583. --- a/drivers/mtd/nand/nand_bbt.c
  2584. +++ b/drivers/mtd/nand/nand_bbt.c
  2585. @@ -172,7 +172,7 @@
  2586. struct nand_bbt_descr *td, int offs)
  2587. {
  2588. int res, ret = 0, i, j, act = 0;
  2589. - struct nand_chip *this = mtd->priv;
  2590. + struct nand_chip *this = mtd_to_nand(mtd);
  2591. size_t retlen, len, totlen;
  2592. loff_t from;
  2593. int bits = td->options & NAND_BBT_NRBITS_MSK;
  2594. @@ -263,7 +263,7 @@
  2595. */
  2596. static int read_abs_bbt(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *td, int chip)
  2597. {
  2598. - struct nand_chip *this = mtd->priv;
  2599. + struct nand_chip *this = mtd_to_nand(mtd);
  2600. int res = 0, i;
  2601. if (td->options & NAND_BBT_PERCHIP) {
  2602. @@ -388,7 +388,7 @@
  2603. static void read_abs_bbts(struct mtd_info *mtd, uint8_t *buf,
  2604. struct nand_bbt_descr *td, struct nand_bbt_descr *md)
  2605. {
  2606. - struct nand_chip *this = mtd->priv;
  2607. + struct nand_chip *this = mtd_to_nand(mtd);
  2608. /* Read the primary version, if available */
  2609. if (td->options & NAND_BBT_VERSION) {
  2610. @@ -454,7 +454,7 @@
  2611. static int create_bbt(struct mtd_info *mtd, uint8_t *buf,
  2612. struct nand_bbt_descr *bd, int chip)
  2613. {
  2614. - struct nand_chip *this = mtd->priv;
  2615. + struct nand_chip *this = mtd_to_nand(mtd);
  2616. int i, numblocks, numpages;
  2617. int startblock;
  2618. loff_t from;
  2619. @@ -523,7 +523,7 @@
  2620. */
  2621. static int search_bbt(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *td)
  2622. {
  2623. - struct nand_chip *this = mtd->priv;
  2624. + struct nand_chip *this = mtd_to_nand(mtd);
  2625. int i, chips;
  2626. int startblock, block, dir;
  2627. int scanlen = mtd->writesize + mtd->oobsize;
  2628. @@ -618,7 +618,7 @@
  2629. struct nand_bbt_descr *td, struct nand_bbt_descr *md,
  2630. int chipsel)
  2631. {
  2632. - struct nand_chip *this = mtd->priv;
  2633. + struct nand_chip *this = mtd_to_nand(mtd);
  2634. struct erase_info einfo;
  2635. int i, res, chip = 0;
  2636. int bits, startblock, dir, page, offs, numblocks, sft, sftmsk;
  2637. @@ -819,7 +819,7 @@
  2638. */
  2639. static inline int nand_memory_bbt(struct mtd_info *mtd, struct nand_bbt_descr *bd)
  2640. {
  2641. - struct nand_chip *this = mtd->priv;
  2642. + struct nand_chip *this = mtd_to_nand(mtd);
  2643. return create_bbt(mtd, this->buffers->databuf, bd, -1);
  2644. }
  2645. @@ -838,7 +838,7 @@
  2646. static int check_create(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *bd)
  2647. {
  2648. int i, chips, writeops, create, chipsel, res, res2;
  2649. - struct nand_chip *this = mtd->priv;
  2650. + struct nand_chip *this = mtd_to_nand(mtd);
  2651. struct nand_bbt_descr *td = this->bbt_td;
  2652. struct nand_bbt_descr *md = this->bbt_md;
  2653. struct nand_bbt_descr *rd, *rd2;
  2654. @@ -962,7 +962,7 @@
  2655. */
  2656. static void mark_bbt_region(struct mtd_info *mtd, struct nand_bbt_descr *td)
  2657. {
  2658. - struct nand_chip *this = mtd->priv;
  2659. + struct nand_chip *this = mtd_to_nand(mtd);
  2660. int i, j, chips, block, nrblocks, update;
  2661. uint8_t oldval;
  2662. @@ -1022,7 +1022,7 @@
  2663. */
  2664. static void verify_bbt_descr(struct mtd_info *mtd, struct nand_bbt_descr *bd)
  2665. {
  2666. - struct nand_chip *this = mtd->priv;
  2667. + struct nand_chip *this = mtd_to_nand(mtd);
  2668. u32 pattern_len;
  2669. u32 bits;
  2670. u32 table_size;
  2671. @@ -1074,7 +1074,7 @@
  2672. */
  2673. static int nand_scan_bbt(struct mtd_info *mtd, struct nand_bbt_descr *bd)
  2674. {
  2675. - struct nand_chip *this = mtd->priv;
  2676. + struct nand_chip *this = mtd_to_nand(mtd);
  2677. int len, res;
  2678. uint8_t *buf;
  2679. struct nand_bbt_descr *td = this->bbt_td;
  2680. @@ -1147,7 +1147,7 @@
  2681. */
  2682. static int nand_update_bbt(struct mtd_info *mtd, loff_t offs)
  2683. {
  2684. - struct nand_chip *this = mtd->priv;
  2685. + struct nand_chip *this = mtd_to_nand(mtd);
  2686. int len, res = 0;
  2687. int chip, chipsel;
  2688. uint8_t *buf;
  2689. @@ -1281,7 +1281,7 @@
  2690. */
  2691. int nand_default_bbt(struct mtd_info *mtd)
  2692. {
  2693. - struct nand_chip *this = mtd->priv;
  2694. + struct nand_chip *this = mtd_to_nand(mtd);
  2695. int ret;
  2696. /* Is a flash based bad block table requested? */
  2697. @@ -1317,7 +1317,7 @@
  2698. */
  2699. int nand_isreserved_bbt(struct mtd_info *mtd, loff_t offs)
  2700. {
  2701. - struct nand_chip *this = mtd->priv;
  2702. + struct nand_chip *this = mtd_to_nand(mtd);
  2703. int block;
  2704. block = (int)(offs >> this->bbt_erase_shift);
  2705. @@ -1332,7 +1332,7 @@
  2706. */
  2707. int nand_isbad_bbt(struct mtd_info *mtd, loff_t offs, int allowbbt)
  2708. {
  2709. - struct nand_chip *this = mtd->priv;
  2710. + struct nand_chip *this = mtd_to_nand(mtd);
  2711. int block, res;
  2712. block = (int)(offs >> this->bbt_erase_shift);
  2713. @@ -1359,7 +1359,7 @@
  2714. */
  2715. int nand_markbad_bbt(struct mtd_info *mtd, loff_t offs)
  2716. {
  2717. - struct nand_chip *this = mtd->priv;
  2718. + struct nand_chip *this = mtd_to_nand(mtd);
  2719. int block, ret = 0;
  2720. block = (int)(offs >> this->bbt_erase_shift);
  2721. @@ -1373,5 +1373,3 @@
  2722. return ret;
  2723. }
  2724. -
  2725. -EXPORT_SYMBOL(nand_scan_bbt);
  2726. --- a/drivers/mtd/nand/nand_bch.c
  2727. +++ b/drivers/mtd/nand/nand_bch.c
  2728. @@ -32,13 +32,11 @@
  2729. /**
  2730. * struct nand_bch_control - private NAND BCH control structure
  2731. * @bch: BCH control structure
  2732. - * @ecclayout: private ecc layout for this BCH configuration
  2733. * @errloc: error location array
  2734. * @eccmask: XOR ecc mask, allows erased pages to be decoded as valid
  2735. */
  2736. struct nand_bch_control {
  2737. struct bch_control *bch;
  2738. - struct nand_ecclayout ecclayout;
  2739. unsigned int *errloc;
  2740. unsigned char *eccmask;
  2741. };
  2742. @@ -52,7 +50,7 @@
  2743. int nand_bch_calculate_ecc(struct mtd_info *mtd, const unsigned char *buf,
  2744. unsigned char *code)
  2745. {
  2746. - const struct nand_chip *chip = mtd->priv;
  2747. + const struct nand_chip *chip = mtd_to_nand(mtd);
  2748. struct nand_bch_control *nbc = chip->ecc.priv;
  2749. unsigned int i;
  2750. @@ -79,7 +77,7 @@
  2751. int nand_bch_correct_data(struct mtd_info *mtd, unsigned char *buf,
  2752. unsigned char *read_ecc, unsigned char *calc_ecc)
  2753. {
  2754. - const struct nand_chip *chip = mtd->priv;
  2755. + const struct nand_chip *chip = mtd_to_nand(mtd);
  2756. struct nand_bch_control *nbc = chip->ecc.priv;
  2757. unsigned int *errloc = nbc->errloc;
  2758. int i, count;
  2759. @@ -98,7 +96,7 @@
  2760. }
  2761. } else if (count < 0) {
  2762. printk(KERN_ERR "ecc unrecoverable error\n");
  2763. - count = -1;
  2764. + count = -EBADMSG;
  2765. }
  2766. return count;
  2767. }
  2768. @@ -107,9 +105,6 @@
  2769. /**
  2770. * nand_bch_init - [NAND Interface] Initialize NAND BCH error correction
  2771. * @mtd: MTD block structure
  2772. - * @eccsize: ecc block size in bytes
  2773. - * @eccbytes: ecc length in bytes
  2774. - * @ecclayout: output default layout
  2775. *
  2776. * Returns:
  2777. * a pointer to a new NAND BCH control structure, or NULL upon failure
  2778. @@ -123,14 +118,20 @@
  2779. * @eccsize = 512 (thus, m=13 is the smallest integer such that 2^m-1 > 512*8)
  2780. * @eccbytes = 7 (7 bytes are required to store m*t = 13*4 = 52 bits)
  2781. */
  2782. -struct nand_bch_control *
  2783. -nand_bch_init(struct mtd_info *mtd, unsigned int eccsize, unsigned int eccbytes,
  2784. - struct nand_ecclayout **ecclayout)
  2785. +struct nand_bch_control *nand_bch_init(struct mtd_info *mtd)
  2786. {
  2787. + struct nand_chip *nand = mtd_to_nand(mtd);
  2788. unsigned int m, t, eccsteps, i;
  2789. - struct nand_ecclayout *layout;
  2790. struct nand_bch_control *nbc = NULL;
  2791. unsigned char *erased_page;
  2792. + unsigned int eccsize = nand->ecc.size;
  2793. + unsigned int eccbytes = nand->ecc.bytes;
  2794. + unsigned int eccstrength = nand->ecc.strength;
  2795. +
  2796. + if (!eccbytes && eccstrength) {
  2797. + eccbytes = DIV_ROUND_UP(eccstrength * fls(8 * eccsize), 8);
  2798. + nand->ecc.bytes = eccbytes;
  2799. + }
  2800. if (!eccsize || !eccbytes) {
  2801. printk(KERN_WARNING "ecc parameters not supplied\n");
  2802. @@ -158,7 +159,7 @@
  2803. eccsteps = mtd->writesize/eccsize;
  2804. /* if no ecc placement scheme was provided, build one */
  2805. - if (!*ecclayout) {
  2806. + if (!mtd->ooblayout) {
  2807. /* handle large page devices only */
  2808. if (mtd->oobsize < 64) {
  2809. @@ -167,24 +168,7 @@
  2810. goto fail;
  2811. }
  2812. - layout = &nbc->ecclayout;
  2813. - layout->eccbytes = eccsteps*eccbytes;
  2814. -
  2815. - /* reserve 2 bytes for bad block marker */
  2816. - if (layout->eccbytes+2 > mtd->oobsize) {
  2817. - printk(KERN_WARNING "no suitable oob scheme available "
  2818. - "for oobsize %d eccbytes %u\n", mtd->oobsize,
  2819. - eccbytes);
  2820. - goto fail;
  2821. - }
  2822. - /* put ecc bytes at oob tail */
  2823. - for (i = 0; i < layout->eccbytes; i++)
  2824. - layout->eccpos[i] = mtd->oobsize-layout->eccbytes+i;
  2825. -
  2826. - layout->oobfree[0].offset = 2;
  2827. - layout->oobfree[0].length = mtd->oobsize-2-layout->eccbytes;
  2828. -
  2829. - *ecclayout = layout;
  2830. + mtd_set_ooblayout(mtd, &nand_ooblayout_lp_ops);
  2831. }
  2832. /* sanity checks */
  2833. @@ -192,7 +176,8 @@
  2834. printk(KERN_WARNING "eccsize %u is too large\n", eccsize);
  2835. goto fail;
  2836. }
  2837. - if ((*ecclayout)->eccbytes != (eccsteps*eccbytes)) {
  2838. +
  2839. + if (mtd_ooblayout_count_eccbytes(mtd) != (eccsteps*eccbytes)) {
  2840. printk(KERN_WARNING "invalid ecc layout\n");
  2841. goto fail;
  2842. }
  2843. @@ -216,6 +201,9 @@
  2844. for (i = 0; i < eccbytes; i++)
  2845. nbc->eccmask[i] ^= 0xff;
  2846. + if (!eccstrength)
  2847. + nand->ecc.strength = (eccbytes * 8) / fls(8 * eccsize);
  2848. +
  2849. return nbc;
  2850. fail:
  2851. nand_bch_free(nbc);
  2852. --- a/drivers/mtd/nand/nand_ecc.c
  2853. +++ b/drivers/mtd/nand/nand_ecc.c
  2854. @@ -424,7 +424,7 @@
  2855. unsigned char *code)
  2856. {
  2857. __nand_calculate_ecc(buf,
  2858. - ((struct nand_chip *)mtd->priv)->ecc.size, code);
  2859. + mtd_to_nand(mtd)->ecc.size, code);
  2860. return 0;
  2861. }
  2862. @@ -524,7 +524,7 @@
  2863. unsigned char *read_ecc, unsigned char *calc_ecc)
  2864. {
  2865. return __nand_correct_data(buf, read_ecc, calc_ecc,
  2866. - ((struct nand_chip *)mtd->priv)->ecc.size);
  2867. + mtd_to_nand(mtd)->ecc.size);
  2868. }
  2869. EXPORT_SYMBOL(nand_correct_data);
  2870. --- a/drivers/mtd/nand/nand_ids.c
  2871. +++ b/drivers/mtd/nand/nand_ids.c
  2872. @@ -50,8 +50,8 @@
  2873. SZ_16K, SZ_8K, SZ_4M, 0, 6, 1280, NAND_ECC_INFO(40, SZ_1K) },
  2874. {"H27UCG8T2ATR-BC 64G 3.3V 8-bit",
  2875. { .id = {0xad, 0xde, 0x94, 0xda, 0x74, 0xc4} },
  2876. - SZ_8K, SZ_8K, SZ_2M, 0, 6, 640, NAND_ECC_INFO(40, SZ_1K),
  2877. - 4 },
  2878. + SZ_8K, SZ_8K, SZ_2M, NAND_NEED_SCRAMBLING, 6, 640,
  2879. + NAND_ECC_INFO(40, SZ_1K), 4 },
  2880. LEGACY_ID_NAND("NAND 4MiB 5V 8-bit", 0x6B, 4, SZ_8K, SP_OPTIONS),
  2881. LEGACY_ID_NAND("NAND 4MiB 3,3V 8-bit", 0xE3, 4, SZ_8K, SP_OPTIONS),
  2882. --- a/drivers/mtd/nand/nandsim.c
  2883. +++ b/drivers/mtd/nand/nandsim.c
  2884. @@ -666,8 +666,8 @@
  2885. */
  2886. static int init_nandsim(struct mtd_info *mtd)
  2887. {
  2888. - struct nand_chip *chip = mtd->priv;
  2889. - struct nandsim *ns = chip->priv;
  2890. + struct nand_chip *chip = mtd_to_nand(mtd);
  2891. + struct nandsim *ns = nand_get_controller_data(chip);
  2892. int i, ret = 0;
  2893. uint64_t remains;
  2894. uint64_t next_offset;
  2895. @@ -1908,7 +1908,8 @@
  2896. static u_char ns_nand_read_byte(struct mtd_info *mtd)
  2897. {
  2898. - struct nandsim *ns = ((struct nand_chip *)mtd->priv)->priv;
  2899. + struct nand_chip *chip = mtd_to_nand(mtd);
  2900. + struct nandsim *ns = nand_get_controller_data(chip);
  2901. u_char outb = 0x00;
  2902. /* Sanity and correctness checks */
  2903. @@ -1969,7 +1970,8 @@
  2904. static void ns_nand_write_byte(struct mtd_info *mtd, u_char byte)
  2905. {
  2906. - struct nandsim *ns = ((struct nand_chip *)mtd->priv)->priv;
  2907. + struct nand_chip *chip = mtd_to_nand(mtd);
  2908. + struct nandsim *ns = nand_get_controller_data(chip);
  2909. /* Sanity and correctness checks */
  2910. if (!ns->lines.ce) {
  2911. @@ -2123,7 +2125,8 @@
  2912. static void ns_hwcontrol(struct mtd_info *mtd, int cmd, unsigned int bitmask)
  2913. {
  2914. - struct nandsim *ns = ((struct nand_chip *)mtd->priv)->priv;
  2915. + struct nand_chip *chip = mtd_to_nand(mtd);
  2916. + struct nandsim *ns = nand_get_controller_data(chip);
  2917. ns->lines.cle = bitmask & NAND_CLE ? 1 : 0;
  2918. ns->lines.ale = bitmask & NAND_ALE ? 1 : 0;
  2919. @@ -2141,7 +2144,7 @@
  2920. static uint16_t ns_nand_read_word(struct mtd_info *mtd)
  2921. {
  2922. - struct nand_chip *chip = (struct nand_chip *)mtd->priv;
  2923. + struct nand_chip *chip = mtd_to_nand(mtd);
  2924. NS_DBG("read_word\n");
  2925. @@ -2150,7 +2153,8 @@
  2926. static void ns_nand_write_buf(struct mtd_info *mtd, const u_char *buf, int len)
  2927. {
  2928. - struct nandsim *ns = ((struct nand_chip *)mtd->priv)->priv;
  2929. + struct nand_chip *chip = mtd_to_nand(mtd);
  2930. + struct nandsim *ns = nand_get_controller_data(chip);
  2931. /* Check that chip is expecting data input */
  2932. if (!(ns->state & STATE_DATAIN_MASK)) {
  2933. @@ -2177,7 +2181,8 @@
  2934. static void ns_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len)
  2935. {
  2936. - struct nandsim *ns = ((struct nand_chip *)mtd->priv)->priv;
  2937. + struct nand_chip *chip = mtd_to_nand(mtd);
  2938. + struct nandsim *ns = nand_get_controller_data(chip);
  2939. /* Sanity and correctness checks */
  2940. if (!ns->lines.ce) {
  2941. @@ -2198,7 +2203,7 @@
  2942. int i;
  2943. for (i = 0; i < len; i++)
  2944. - buf[i] = ((struct nand_chip *)mtd->priv)->read_byte(mtd);
  2945. + buf[i] = mtd_to_nand(mtd)->read_byte(mtd);
  2946. return;
  2947. }
  2948. @@ -2236,16 +2241,15 @@
  2949. }
  2950. /* Allocate and initialize mtd_info, nand_chip and nandsim structures */
  2951. - nsmtd = kzalloc(sizeof(struct mtd_info) + sizeof(struct nand_chip)
  2952. - + sizeof(struct nandsim), GFP_KERNEL);
  2953. - if (!nsmtd) {
  2954. + chip = kzalloc(sizeof(struct nand_chip) + sizeof(struct nandsim),
  2955. + GFP_KERNEL);
  2956. + if (!chip) {
  2957. NS_ERR("unable to allocate core structures.\n");
  2958. return -ENOMEM;
  2959. }
  2960. - chip = (struct nand_chip *)(nsmtd + 1);
  2961. - nsmtd->priv = (void *)chip;
  2962. + nsmtd = nand_to_mtd(chip);
  2963. nand = (struct nandsim *)(chip + 1);
  2964. - chip->priv = (void *)nand;
  2965. + nand_set_controller_data(chip, (void *)nand);
  2966. /*
  2967. * Register simulator's callbacks.
  2968. @@ -2257,6 +2261,7 @@
  2969. chip->read_buf = ns_nand_read_buf;
  2970. chip->read_word = ns_nand_read_word;
  2971. chip->ecc.mode = NAND_ECC_SOFT;
  2972. + chip->ecc.algo = NAND_ECC_HAMMING;
  2973. /* The NAND_SKIP_BBTSCAN option is necessary for 'overridesize' */
  2974. /* and 'badblocks' parameters to work */
  2975. chip->options |= NAND_SKIP_BBTSCAN;
  2976. @@ -2335,6 +2340,7 @@
  2977. goto error;
  2978. }
  2979. chip->ecc.mode = NAND_ECC_SOFT_BCH;
  2980. + chip->ecc.algo = NAND_ECC_BCH;
  2981. chip->ecc.size = 512;
  2982. chip->ecc.strength = bch;
  2983. chip->ecc.bytes = eccbytes;
  2984. @@ -2392,7 +2398,7 @@
  2985. for (i = 0;i < ARRAY_SIZE(nand->partitions); ++i)
  2986. kfree(nand->partitions[i].name);
  2987. error:
  2988. - kfree(nsmtd);
  2989. + kfree(chip);
  2990. free_lists();
  2991. return retval;
  2992. @@ -2405,7 +2411,8 @@
  2993. */
  2994. static void __exit ns_cleanup_module(void)
  2995. {
  2996. - struct nandsim *ns = ((struct nand_chip *)nsmtd->priv)->priv;
  2997. + struct nand_chip *chip = mtd_to_nand(nsmtd);
  2998. + struct nandsim *ns = nand_get_controller_data(chip);
  2999. int i;
  3000. nandsim_debugfs_remove(ns);
  3001. @@ -2413,7 +2420,7 @@
  3002. nand_release(nsmtd); /* Unregister driver */
  3003. for (i = 0;i < ARRAY_SIZE(ns->partitions); ++i)
  3004. kfree(ns->partitions[i].name);
  3005. - kfree(nsmtd); /* Free other structures */
  3006. + kfree(mtd_to_nand(nsmtd)); /* Free other structures */
  3007. free_lists();
  3008. }
  3009. --- a/drivers/mtd/ofpart.c
  3010. +++ b/drivers/mtd/ofpart.c
  3011. @@ -26,9 +26,10 @@
  3012. }
  3013. static int parse_ofpart_partitions(struct mtd_info *master,
  3014. - struct mtd_partition **pparts,
  3015. + const struct mtd_partition **pparts,
  3016. struct mtd_part_parser_data *data)
  3017. {
  3018. + struct mtd_partition *parts;
  3019. struct device_node *mtd_node;
  3020. struct device_node *ofpart_node;
  3021. const char *partname;
  3022. @@ -37,10 +38,8 @@
  3023. bool dedicated = true;
  3024. - if (!data)
  3025. - return 0;
  3026. -
  3027. - mtd_node = data->of_node;
  3028. + /* Pull of_node from the master device node */
  3029. + mtd_node = mtd_get_of_node(master);
  3030. if (!mtd_node)
  3031. return 0;
  3032. @@ -72,8 +71,8 @@
  3033. if (nr_parts == 0)
  3034. return 0;
  3035. - *pparts = kzalloc(nr_parts * sizeof(**pparts), GFP_KERNEL);
  3036. - if (!*pparts)
  3037. + parts = kzalloc(nr_parts * sizeof(*parts), GFP_KERNEL);
  3038. + if (!parts)
  3039. return -ENOMEM;
  3040. i = 0;
  3041. @@ -107,19 +106,19 @@
  3042. goto ofpart_fail;
  3043. }
  3044. - (*pparts)[i].offset = of_read_number(reg, a_cells);
  3045. - (*pparts)[i].size = of_read_number(reg + a_cells, s_cells);
  3046. + parts[i].offset = of_read_number(reg, a_cells);
  3047. + parts[i].size = of_read_number(reg + a_cells, s_cells);
  3048. partname = of_get_property(pp, "label", &len);
  3049. if (!partname)
  3050. partname = of_get_property(pp, "name", &len);
  3051. - (*pparts)[i].name = partname;
  3052. + parts[i].name = partname;
  3053. if (of_get_property(pp, "read-only", &len))
  3054. - (*pparts)[i].mask_flags |= MTD_WRITEABLE;
  3055. + parts[i].mask_flags |= MTD_WRITEABLE;
  3056. if (of_get_property(pp, "lock", &len))
  3057. - (*pparts)[i].mask_flags |= MTD_POWERUP_LOCK;
  3058. + parts[i].mask_flags |= MTD_POWERUP_LOCK;
  3059. i++;
  3060. }
  3061. @@ -127,6 +126,7 @@
  3062. if (!nr_parts)
  3063. goto ofpart_none;
  3064. + *pparts = parts;
  3065. return nr_parts;
  3066. ofpart_fail:
  3067. @@ -135,21 +135,20 @@
  3068. ret = -EINVAL;
  3069. ofpart_none:
  3070. of_node_put(pp);
  3071. - kfree(*pparts);
  3072. - *pparts = NULL;
  3073. + kfree(parts);
  3074. return ret;
  3075. }
  3076. static struct mtd_part_parser ofpart_parser = {
  3077. - .owner = THIS_MODULE,
  3078. .parse_fn = parse_ofpart_partitions,
  3079. .name = "ofpart",
  3080. };
  3081. static int parse_ofoldpart_partitions(struct mtd_info *master,
  3082. - struct mtd_partition **pparts,
  3083. + const struct mtd_partition **pparts,
  3084. struct mtd_part_parser_data *data)
  3085. {
  3086. + struct mtd_partition *parts;
  3087. struct device_node *dp;
  3088. int i, plen, nr_parts;
  3089. const struct {
  3090. @@ -157,10 +156,8 @@
  3091. } *part;
  3092. const char *names;
  3093. - if (!data)
  3094. - return 0;
  3095. -
  3096. - dp = data->of_node;
  3097. + /* Pull of_node from the master device node */
  3098. + dp = mtd_get_of_node(master);
  3099. if (!dp)
  3100. return 0;
  3101. @@ -173,37 +170,37 @@
  3102. nr_parts = plen / sizeof(part[0]);
  3103. - *pparts = kzalloc(nr_parts * sizeof(*(*pparts)), GFP_KERNEL);
  3104. - if (!*pparts)
  3105. + parts = kzalloc(nr_parts * sizeof(*parts), GFP_KERNEL);
  3106. + if (!parts)
  3107. return -ENOMEM;
  3108. names = of_get_property(dp, "partition-names", &plen);
  3109. for (i = 0; i < nr_parts; i++) {
  3110. - (*pparts)[i].offset = be32_to_cpu(part->offset);
  3111. - (*pparts)[i].size = be32_to_cpu(part->len) & ~1;
  3112. + parts[i].offset = be32_to_cpu(part->offset);
  3113. + parts[i].size = be32_to_cpu(part->len) & ~1;
  3114. /* bit 0 set signifies read only partition */
  3115. if (be32_to_cpu(part->len) & 1)
  3116. - (*pparts)[i].mask_flags = MTD_WRITEABLE;
  3117. + parts[i].mask_flags = MTD_WRITEABLE;
  3118. if (names && (plen > 0)) {
  3119. int len = strlen(names) + 1;
  3120. - (*pparts)[i].name = names;
  3121. + parts[i].name = names;
  3122. plen -= len;
  3123. names += len;
  3124. } else {
  3125. - (*pparts)[i].name = "unnamed";
  3126. + parts[i].name = "unnamed";
  3127. }
  3128. part++;
  3129. }
  3130. + *pparts = parts;
  3131. return nr_parts;
  3132. }
  3133. static struct mtd_part_parser ofoldpart_parser = {
  3134. - .owner = THIS_MODULE,
  3135. .parse_fn = parse_ofoldpart_partitions,
  3136. .name = "ofoldpart",
  3137. };
  3138. --- a/drivers/mtd/spi-nor/Kconfig
  3139. +++ b/drivers/mtd/spi-nor/Kconfig
  3140. @@ -7,6 +7,14 @@
  3141. if MTD_SPI_NOR
  3142. +config MTD_MT81xx_NOR
  3143. + tristate "Mediatek MT81xx SPI NOR flash controller"
  3144. + depends on HAS_IOMEM
  3145. + help
  3146. + This enables access to SPI NOR flash, using MT81xx SPI NOR flash
  3147. + controller. This controller does not support generic SPI BUS, it only
  3148. + supports SPI NOR Flash.
  3149. +
  3150. config MTD_SPI_NOR_USE_4K_SECTORS
  3151. bool "Use small 4096 B erase sectors"
  3152. default y
  3153. @@ -23,7 +31,7 @@
  3154. config SPI_FSL_QUADSPI
  3155. tristate "Freescale Quad SPI controller"
  3156. - depends on ARCH_MXC || COMPILE_TEST
  3157. + depends on ARCH_MXC || SOC_LS1021A || ARCH_LAYERSCAPE || COMPILE_TEST
  3158. depends on HAS_IOMEM
  3159. help
  3160. This enables support for the Quad SPI controller in master mode.
  3161. --- a/drivers/mtd/spi-nor/Makefile
  3162. +++ b/drivers/mtd/spi-nor/Makefile
  3163. @@ -1,3 +1,4 @@
  3164. obj-$(CONFIG_MTD_SPI_NOR) += spi-nor.o
  3165. obj-$(CONFIG_SPI_FSL_QUADSPI) += fsl-quadspi.o
  3166. +obj-$(CONFIG_MTD_MT81xx_NOR) += mtk-quadspi.o
  3167. obj-$(CONFIG_SPI_NXP_SPIFI) += nxp-spifi.o
  3168. --- /dev/null
  3169. +++ b/drivers/mtd/spi-nor/mtk-quadspi.c
  3170. @@ -0,0 +1,485 @@
  3171. +/*
  3172. + * Copyright (c) 2015 MediaTek Inc.
  3173. + * Author: Bayi Cheng <[email protected]>
  3174. + *
  3175. + * This program is free software; you can redistribute it and/or modify
  3176. + * it under the terms of the GNU General Public License version 2 as
  3177. + * published by the Free Software Foundation.
  3178. + *
  3179. + * This program is distributed in the hope that it will be useful,
  3180. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  3181. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  3182. + * GNU General Public License for more details.
  3183. + */
  3184. +
  3185. +#include <linux/clk.h>
  3186. +#include <linux/delay.h>
  3187. +#include <linux/device.h>
  3188. +#include <linux/init.h>
  3189. +#include <linux/io.h>
  3190. +#include <linux/iopoll.h>
  3191. +#include <linux/ioport.h>
  3192. +#include <linux/math64.h>
  3193. +#include <linux/module.h>
  3194. +#include <linux/mtd/mtd.h>
  3195. +#include <linux/mutex.h>
  3196. +#include <linux/of.h>
  3197. +#include <linux/of_device.h>
  3198. +#include <linux/pinctrl/consumer.h>
  3199. +#include <linux/platform_device.h>
  3200. +#include <linux/slab.h>
  3201. +#include <linux/mtd/mtd.h>
  3202. +#include <linux/mtd/partitions.h>
  3203. +#include <linux/mtd/spi-nor.h>
  3204. +
  3205. +#define MTK_NOR_CMD_REG 0x00
  3206. +#define MTK_NOR_CNT_REG 0x04
  3207. +#define MTK_NOR_RDSR_REG 0x08
  3208. +#define MTK_NOR_RDATA_REG 0x0c
  3209. +#define MTK_NOR_RADR0_REG 0x10
  3210. +#define MTK_NOR_RADR1_REG 0x14
  3211. +#define MTK_NOR_RADR2_REG 0x18
  3212. +#define MTK_NOR_WDATA_REG 0x1c
  3213. +#define MTK_NOR_PRGDATA0_REG 0x20
  3214. +#define MTK_NOR_PRGDATA1_REG 0x24
  3215. +#define MTK_NOR_PRGDATA2_REG 0x28
  3216. +#define MTK_NOR_PRGDATA3_REG 0x2c
  3217. +#define MTK_NOR_PRGDATA4_REG 0x30
  3218. +#define MTK_NOR_PRGDATA5_REG 0x34
  3219. +#define MTK_NOR_SHREG0_REG 0x38
  3220. +#define MTK_NOR_SHREG1_REG 0x3c
  3221. +#define MTK_NOR_SHREG2_REG 0x40
  3222. +#define MTK_NOR_SHREG3_REG 0x44
  3223. +#define MTK_NOR_SHREG4_REG 0x48
  3224. +#define MTK_NOR_SHREG5_REG 0x4c
  3225. +#define MTK_NOR_SHREG6_REG 0x50
  3226. +#define MTK_NOR_SHREG7_REG 0x54
  3227. +#define MTK_NOR_SHREG8_REG 0x58
  3228. +#define MTK_NOR_SHREG9_REG 0x5c
  3229. +#define MTK_NOR_CFG1_REG 0x60
  3230. +#define MTK_NOR_CFG2_REG 0x64
  3231. +#define MTK_NOR_CFG3_REG 0x68
  3232. +#define MTK_NOR_STATUS0_REG 0x70
  3233. +#define MTK_NOR_STATUS1_REG 0x74
  3234. +#define MTK_NOR_STATUS2_REG 0x78
  3235. +#define MTK_NOR_STATUS3_REG 0x7c
  3236. +#define MTK_NOR_FLHCFG_REG 0x84
  3237. +#define MTK_NOR_TIME_REG 0x94
  3238. +#define MTK_NOR_PP_DATA_REG 0x98
  3239. +#define MTK_NOR_PREBUF_STUS_REG 0x9c
  3240. +#define MTK_NOR_DELSEL0_REG 0xa0
  3241. +#define MTK_NOR_DELSEL1_REG 0xa4
  3242. +#define MTK_NOR_INTRSTUS_REG 0xa8
  3243. +#define MTK_NOR_INTREN_REG 0xac
  3244. +#define MTK_NOR_CHKSUM_CTL_REG 0xb8
  3245. +#define MTK_NOR_CHKSUM_REG 0xbc
  3246. +#define MTK_NOR_CMD2_REG 0xc0
  3247. +#define MTK_NOR_WRPROT_REG 0xc4
  3248. +#define MTK_NOR_RADR3_REG 0xc8
  3249. +#define MTK_NOR_DUAL_REG 0xcc
  3250. +#define MTK_NOR_DELSEL2_REG 0xd0
  3251. +#define MTK_NOR_DELSEL3_REG 0xd4
  3252. +#define MTK_NOR_DELSEL4_REG 0xd8
  3253. +
  3254. +/* commands for mtk nor controller */
  3255. +#define MTK_NOR_READ_CMD 0x0
  3256. +#define MTK_NOR_RDSR_CMD 0x2
  3257. +#define MTK_NOR_PRG_CMD 0x4
  3258. +#define MTK_NOR_WR_CMD 0x10
  3259. +#define MTK_NOR_PIO_WR_CMD 0x90
  3260. +#define MTK_NOR_WRSR_CMD 0x20
  3261. +#define MTK_NOR_PIO_READ_CMD 0x81
  3262. +#define MTK_NOR_WR_BUF_ENABLE 0x1
  3263. +#define MTK_NOR_WR_BUF_DISABLE 0x0
  3264. +#define MTK_NOR_ENABLE_SF_CMD 0x30
  3265. +#define MTK_NOR_DUAD_ADDR_EN 0x8
  3266. +#define MTK_NOR_QUAD_READ_EN 0x4
  3267. +#define MTK_NOR_DUAL_ADDR_EN 0x2
  3268. +#define MTK_NOR_DUAL_READ_EN 0x1
  3269. +#define MTK_NOR_DUAL_DISABLE 0x0
  3270. +#define MTK_NOR_FAST_READ 0x1
  3271. +
  3272. +#define SFLASH_WRBUF_SIZE 128
  3273. +
  3274. +/* Can shift up to 48 bits (6 bytes) of TX/RX */
  3275. +#define MTK_NOR_MAX_RX_TX_SHIFT 6
  3276. +/* can shift up to 56 bits (7 bytes) transfer by MTK_NOR_PRG_CMD */
  3277. +#define MTK_NOR_MAX_SHIFT 7
  3278. +
  3279. +/* Helpers for accessing the program data / shift data registers */
  3280. +#define MTK_NOR_PRG_REG(n) (MTK_NOR_PRGDATA0_REG + 4 * (n))
  3281. +#define MTK_NOR_SHREG(n) (MTK_NOR_SHREG0_REG + 4 * (n))
  3282. +
  3283. +struct mt8173_nor {
  3284. + struct spi_nor nor;
  3285. + struct device *dev;
  3286. + void __iomem *base; /* nor flash base address */
  3287. + struct clk *spi_clk;
  3288. + struct clk *nor_clk;
  3289. +};
  3290. +
  3291. +static void mt8173_nor_set_read_mode(struct mt8173_nor *mt8173_nor)
  3292. +{
  3293. + struct spi_nor *nor = &mt8173_nor->nor;
  3294. +
  3295. + switch (nor->flash_read) {
  3296. + case SPI_NOR_FAST:
  3297. + writeb(nor->read_opcode, mt8173_nor->base +
  3298. + MTK_NOR_PRGDATA3_REG);
  3299. + writeb(MTK_NOR_FAST_READ, mt8173_nor->base +
  3300. + MTK_NOR_CFG1_REG);
  3301. + break;
  3302. + case SPI_NOR_DUAL:
  3303. + writeb(nor->read_opcode, mt8173_nor->base +
  3304. + MTK_NOR_PRGDATA3_REG);
  3305. + writeb(MTK_NOR_DUAL_READ_EN, mt8173_nor->base +
  3306. + MTK_NOR_DUAL_REG);
  3307. + break;
  3308. + case SPI_NOR_QUAD:
  3309. + writeb(nor->read_opcode, mt8173_nor->base +
  3310. + MTK_NOR_PRGDATA4_REG);
  3311. + writeb(MTK_NOR_QUAD_READ_EN, mt8173_nor->base +
  3312. + MTK_NOR_DUAL_REG);
  3313. + break;
  3314. + default:
  3315. + writeb(MTK_NOR_DUAL_DISABLE, mt8173_nor->base +
  3316. + MTK_NOR_DUAL_REG);
  3317. + break;
  3318. + }
  3319. +}
  3320. +
  3321. +static int mt8173_nor_execute_cmd(struct mt8173_nor *mt8173_nor, u8 cmdval)
  3322. +{
  3323. + int reg;
  3324. + u8 val = cmdval & 0x1f;
  3325. +
  3326. + writeb(cmdval, mt8173_nor->base + MTK_NOR_CMD_REG);
  3327. + return readl_poll_timeout(mt8173_nor->base + MTK_NOR_CMD_REG, reg,
  3328. + !(reg & val), 100, 10000);
  3329. +}
  3330. +
  3331. +static int mt8173_nor_do_tx_rx(struct mt8173_nor *mt8173_nor, u8 op,
  3332. + u8 *tx, int txlen, u8 *rx, int rxlen)
  3333. +{
  3334. + int len = 1 + txlen + rxlen;
  3335. + int i, ret, idx;
  3336. +
  3337. + if (len > MTK_NOR_MAX_SHIFT)
  3338. + return -EINVAL;
  3339. +
  3340. + writeb(len * 8, mt8173_nor->base + MTK_NOR_CNT_REG);
  3341. +
  3342. + /* start at PRGDATA5, go down to PRGDATA0 */
  3343. + idx = MTK_NOR_MAX_RX_TX_SHIFT - 1;
  3344. +
  3345. + /* opcode */
  3346. + writeb(op, mt8173_nor->base + MTK_NOR_PRG_REG(idx));
  3347. + idx--;
  3348. +
  3349. + /* program TX data */
  3350. + for (i = 0; i < txlen; i++, idx--)
  3351. + writeb(tx[i], mt8173_nor->base + MTK_NOR_PRG_REG(idx));
  3352. +
  3353. + /* clear out rest of TX registers */
  3354. + while (idx >= 0) {
  3355. + writeb(0, mt8173_nor->base + MTK_NOR_PRG_REG(idx));
  3356. + idx--;
  3357. + }
  3358. +
  3359. + ret = mt8173_nor_execute_cmd(mt8173_nor, MTK_NOR_PRG_CMD);
  3360. + if (ret)
  3361. + return ret;
  3362. +
  3363. + /* restart at first RX byte */
  3364. + idx = rxlen - 1;
  3365. +
  3366. + /* read out RX data */
  3367. + for (i = 0; i < rxlen; i++, idx--)
  3368. + rx[i] = readb(mt8173_nor->base + MTK_NOR_SHREG(idx));
  3369. +
  3370. + return 0;
  3371. +}
  3372. +
  3373. +/* Do a WRSR (Write Status Register) command */
  3374. +static int mt8173_nor_wr_sr(struct mt8173_nor *mt8173_nor, u8 sr)
  3375. +{
  3376. + writeb(sr, mt8173_nor->base + MTK_NOR_PRGDATA5_REG);
  3377. + writeb(8, mt8173_nor->base + MTK_NOR_CNT_REG);
  3378. + return mt8173_nor_execute_cmd(mt8173_nor, MTK_NOR_WRSR_CMD);
  3379. +}
  3380. +
  3381. +static int mt8173_nor_write_buffer_enable(struct mt8173_nor *mt8173_nor)
  3382. +{
  3383. + u8 reg;
  3384. +
  3385. + /* the bit0 of MTK_NOR_CFG2_REG is pre-fetch buffer
  3386. + * 0: pre-fetch buffer use for read
  3387. + * 1: pre-fetch buffer use for page program
  3388. + */
  3389. + writel(MTK_NOR_WR_BUF_ENABLE, mt8173_nor->base + MTK_NOR_CFG2_REG);
  3390. + return readb_poll_timeout(mt8173_nor->base + MTK_NOR_CFG2_REG, reg,
  3391. + 0x01 == (reg & 0x01), 100, 10000);
  3392. +}
  3393. +
  3394. +static int mt8173_nor_write_buffer_disable(struct mt8173_nor *mt8173_nor)
  3395. +{
  3396. + u8 reg;
  3397. +
  3398. + writel(MTK_NOR_WR_BUF_DISABLE, mt8173_nor->base + MTK_NOR_CFG2_REG);
  3399. + return readb_poll_timeout(mt8173_nor->base + MTK_NOR_CFG2_REG, reg,
  3400. + MTK_NOR_WR_BUF_DISABLE == (reg & 0x1), 100,
  3401. + 10000);
  3402. +}
  3403. +
  3404. +static void mt8173_nor_set_addr(struct mt8173_nor *mt8173_nor, u32 addr)
  3405. +{
  3406. + int i;
  3407. +
  3408. + for (i = 0; i < 3; i++) {
  3409. + writeb(addr & 0xff, mt8173_nor->base + MTK_NOR_RADR0_REG + i * 4);
  3410. + addr >>= 8;
  3411. + }
  3412. + /* Last register is non-contiguous */
  3413. + writeb(addr & 0xff, mt8173_nor->base + MTK_NOR_RADR3_REG);
  3414. +}
  3415. +
  3416. +static int mt8173_nor_read(struct spi_nor *nor, loff_t from, size_t length,
  3417. + size_t *retlen, u_char *buffer)
  3418. +{
  3419. + int i, ret;
  3420. + int addr = (int)from;
  3421. + u8 *buf = (u8 *)buffer;
  3422. + struct mt8173_nor *mt8173_nor = nor->priv;
  3423. +
  3424. + /* set mode for fast read mode ,dual mode or quad mode */
  3425. + mt8173_nor_set_read_mode(mt8173_nor);
  3426. + mt8173_nor_set_addr(mt8173_nor, addr);
  3427. +
  3428. + for (i = 0; i < length; i++, (*retlen)++) {
  3429. + ret = mt8173_nor_execute_cmd(mt8173_nor, MTK_NOR_PIO_READ_CMD);
  3430. + if (ret < 0)
  3431. + return ret;
  3432. + buf[i] = readb(mt8173_nor->base + MTK_NOR_RDATA_REG);
  3433. + }
  3434. + return 0;
  3435. +}
  3436. +
  3437. +static int mt8173_nor_write_single_byte(struct mt8173_nor *mt8173_nor,
  3438. + int addr, int length, u8 *data)
  3439. +{
  3440. + int i, ret;
  3441. +
  3442. + mt8173_nor_set_addr(mt8173_nor, addr);
  3443. +
  3444. + for (i = 0; i < length; i++) {
  3445. + writeb(*data++, mt8173_nor->base + MTK_NOR_WDATA_REG);
  3446. + ret = mt8173_nor_execute_cmd(mt8173_nor, MTK_NOR_PIO_WR_CMD);
  3447. + if (ret < 0)
  3448. + return ret;
  3449. + }
  3450. + return 0;
  3451. +}
  3452. +
  3453. +static int mt8173_nor_write_buffer(struct mt8173_nor *mt8173_nor, int addr,
  3454. + const u8 *buf)
  3455. +{
  3456. + int i, bufidx, data;
  3457. +
  3458. + mt8173_nor_set_addr(mt8173_nor, addr);
  3459. +
  3460. + bufidx = 0;
  3461. + for (i = 0; i < SFLASH_WRBUF_SIZE; i += 4) {
  3462. + data = buf[bufidx + 3]<<24 | buf[bufidx + 2]<<16 |
  3463. + buf[bufidx + 1]<<8 | buf[bufidx];
  3464. + bufidx += 4;
  3465. + writel(data, mt8173_nor->base + MTK_NOR_PP_DATA_REG);
  3466. + }
  3467. + return mt8173_nor_execute_cmd(mt8173_nor, MTK_NOR_WR_CMD);
  3468. +}
  3469. +
  3470. +static void mt8173_nor_write(struct spi_nor *nor, loff_t to, size_t len,
  3471. + size_t *retlen, const u_char *buf)
  3472. +{
  3473. + int ret;
  3474. + struct mt8173_nor *mt8173_nor = nor->priv;
  3475. +
  3476. + ret = mt8173_nor_write_buffer_enable(mt8173_nor);
  3477. + if (ret < 0)
  3478. + dev_warn(mt8173_nor->dev, "write buffer enable failed!\n");
  3479. +
  3480. + while (len >= SFLASH_WRBUF_SIZE) {
  3481. + ret = mt8173_nor_write_buffer(mt8173_nor, to, buf);
  3482. + if (ret < 0)
  3483. + dev_err(mt8173_nor->dev, "write buffer failed!\n");
  3484. + len -= SFLASH_WRBUF_SIZE;
  3485. + to += SFLASH_WRBUF_SIZE;
  3486. + buf += SFLASH_WRBUF_SIZE;
  3487. + (*retlen) += SFLASH_WRBUF_SIZE;
  3488. + }
  3489. + ret = mt8173_nor_write_buffer_disable(mt8173_nor);
  3490. + if (ret < 0)
  3491. + dev_warn(mt8173_nor->dev, "write buffer disable failed!\n");
  3492. +
  3493. + if (len) {
  3494. + ret = mt8173_nor_write_single_byte(mt8173_nor, to, (int)len,
  3495. + (u8 *)buf);
  3496. + if (ret < 0)
  3497. + dev_err(mt8173_nor->dev, "write single byte failed!\n");
  3498. + (*retlen) += len;
  3499. + }
  3500. +}
  3501. +
  3502. +static int mt8173_nor_read_reg(struct spi_nor *nor, u8 opcode, u8 *buf, int len)
  3503. +{
  3504. + int ret;
  3505. + struct mt8173_nor *mt8173_nor = nor->priv;
  3506. +
  3507. + switch (opcode) {
  3508. + case SPINOR_OP_RDSR:
  3509. + ret = mt8173_nor_execute_cmd(mt8173_nor, MTK_NOR_RDSR_CMD);
  3510. + if (ret < 0)
  3511. + return ret;
  3512. + if (len == 1)
  3513. + *buf = readb(mt8173_nor->base + MTK_NOR_RDSR_REG);
  3514. + else
  3515. + dev_err(mt8173_nor->dev, "len should be 1 for read status!\n");
  3516. + break;
  3517. + default:
  3518. + ret = mt8173_nor_do_tx_rx(mt8173_nor, opcode, NULL, 0, buf, len);
  3519. + break;
  3520. + }
  3521. + return ret;
  3522. +}
  3523. +
  3524. +static int mt8173_nor_write_reg(struct spi_nor *nor, u8 opcode, u8 *buf,
  3525. + int len)
  3526. +{
  3527. + int ret;
  3528. + struct mt8173_nor *mt8173_nor = nor->priv;
  3529. +
  3530. + switch (opcode) {
  3531. + case SPINOR_OP_WRSR:
  3532. + /* We only handle 1 byte */
  3533. + ret = mt8173_nor_wr_sr(mt8173_nor, *buf);
  3534. + break;
  3535. + default:
  3536. + ret = mt8173_nor_do_tx_rx(mt8173_nor, opcode, buf, len, NULL, 0);
  3537. + if (ret)
  3538. + dev_warn(mt8173_nor->dev, "write reg failure!\n");
  3539. + break;
  3540. + }
  3541. + return ret;
  3542. +}
  3543. +
  3544. +static int mtk_nor_init(struct mt8173_nor *mt8173_nor,
  3545. + struct device_node *flash_node)
  3546. +{
  3547. + int ret;
  3548. + struct spi_nor *nor;
  3549. +
  3550. + /* initialize controller to accept commands */
  3551. + writel(MTK_NOR_ENABLE_SF_CMD, mt8173_nor->base + MTK_NOR_WRPROT_REG);
  3552. +
  3553. + nor = &mt8173_nor->nor;
  3554. + nor->dev = mt8173_nor->dev;
  3555. + nor->priv = mt8173_nor;
  3556. + spi_nor_set_flash_node(nor, flash_node);
  3557. +
  3558. + /* fill the hooks to spi nor */
  3559. + nor->read = mt8173_nor_read;
  3560. + nor->read_reg = mt8173_nor_read_reg;
  3561. + nor->write = mt8173_nor_write;
  3562. + nor->write_reg = mt8173_nor_write_reg;
  3563. + nor->mtd.name = "mtk_nor";
  3564. + /* initialized with NULL */
  3565. + ret = spi_nor_scan(nor, NULL, SPI_NOR_DUAL);
  3566. + if (ret)
  3567. + return ret;
  3568. +
  3569. + return mtd_device_register(&nor->mtd, NULL, 0);
  3570. +}
  3571. +
  3572. +static int mtk_nor_drv_probe(struct platform_device *pdev)
  3573. +{
  3574. + struct device_node *flash_np;
  3575. + struct resource *res;
  3576. + int ret;
  3577. + struct mt8173_nor *mt8173_nor;
  3578. +
  3579. + if (!pdev->dev.of_node) {
  3580. + dev_err(&pdev->dev, "No DT found\n");
  3581. + return -EINVAL;
  3582. + }
  3583. +
  3584. + mt8173_nor = devm_kzalloc(&pdev->dev, sizeof(*mt8173_nor), GFP_KERNEL);
  3585. + if (!mt8173_nor)
  3586. + return -ENOMEM;
  3587. + platform_set_drvdata(pdev, mt8173_nor);
  3588. +
  3589. + res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  3590. + mt8173_nor->base = devm_ioremap_resource(&pdev->dev, res);
  3591. + if (IS_ERR(mt8173_nor->base))
  3592. + return PTR_ERR(mt8173_nor->base);
  3593. +
  3594. + mt8173_nor->spi_clk = devm_clk_get(&pdev->dev, "spi");
  3595. + if (IS_ERR(mt8173_nor->spi_clk))
  3596. + return PTR_ERR(mt8173_nor->spi_clk);
  3597. +
  3598. + mt8173_nor->nor_clk = devm_clk_get(&pdev->dev, "sf");
  3599. + if (IS_ERR(mt8173_nor->nor_clk))
  3600. + return PTR_ERR(mt8173_nor->nor_clk);
  3601. +
  3602. + mt8173_nor->dev = &pdev->dev;
  3603. + ret = clk_prepare_enable(mt8173_nor->spi_clk);
  3604. + if (ret)
  3605. + return ret;
  3606. +
  3607. + ret = clk_prepare_enable(mt8173_nor->nor_clk);
  3608. + if (ret) {
  3609. + clk_disable_unprepare(mt8173_nor->spi_clk);
  3610. + return ret;
  3611. + }
  3612. + /* only support one attached flash */
  3613. + flash_np = of_get_next_available_child(pdev->dev.of_node, NULL);
  3614. + if (!flash_np) {
  3615. + dev_err(&pdev->dev, "no SPI flash device to configure\n");
  3616. + ret = -ENODEV;
  3617. + goto nor_free;
  3618. + }
  3619. + ret = mtk_nor_init(mt8173_nor, flash_np);
  3620. +
  3621. +nor_free:
  3622. + if (ret) {
  3623. + clk_disable_unprepare(mt8173_nor->spi_clk);
  3624. + clk_disable_unprepare(mt8173_nor->nor_clk);
  3625. + }
  3626. + return ret;
  3627. +}
  3628. +
  3629. +static int mtk_nor_drv_remove(struct platform_device *pdev)
  3630. +{
  3631. + struct mt8173_nor *mt8173_nor = platform_get_drvdata(pdev);
  3632. +
  3633. + clk_disable_unprepare(mt8173_nor->spi_clk);
  3634. + clk_disable_unprepare(mt8173_nor->nor_clk);
  3635. + return 0;
  3636. +}
  3637. +
  3638. +static const struct of_device_id mtk_nor_of_ids[] = {
  3639. + { .compatible = "mediatek,mt8173-nor"},
  3640. + { /* sentinel */ }
  3641. +};
  3642. +MODULE_DEVICE_TABLE(of, mtk_nor_of_ids);
  3643. +
  3644. +static struct platform_driver mtk_nor_driver = {
  3645. + .probe = mtk_nor_drv_probe,
  3646. + .remove = mtk_nor_drv_remove,
  3647. + .driver = {
  3648. + .name = "mtk-nor",
  3649. + .of_match_table = mtk_nor_of_ids,
  3650. + },
  3651. +};
  3652. +
  3653. +module_platform_driver(mtk_nor_driver);
  3654. +MODULE_LICENSE("GPL v2");
  3655. +MODULE_DESCRIPTION("MediaTek SPI NOR Flash Driver");
  3656. --- a/drivers/mtd/spi-nor/spi-nor.c
  3657. +++ b/drivers/mtd/spi-nor/spi-nor.c
  3658. @@ -38,6 +38,7 @@
  3659. #define CHIP_ERASE_2MB_READY_WAIT_JIFFIES (40UL * HZ)
  3660. #define SPI_NOR_MAX_ID_LEN 6
  3661. +#define SPI_NOR_MAX_ADDR_WIDTH 4
  3662. struct flash_info {
  3663. char *name;
  3664. @@ -60,14 +61,20 @@
  3665. u16 addr_width;
  3666. u16 flags;
  3667. -#define SECT_4K 0x01 /* SPINOR_OP_BE_4K works uniformly */
  3668. -#define SPI_NOR_NO_ERASE 0x02 /* No erase command needed */
  3669. -#define SST_WRITE 0x04 /* use SST byte programming */
  3670. -#define SPI_NOR_NO_FR 0x08 /* Can't do fastread */
  3671. -#define SECT_4K_PMC 0x10 /* SPINOR_OP_BE_4K_PMC works uniformly */
  3672. -#define SPI_NOR_DUAL_READ 0x20 /* Flash supports Dual Read */
  3673. -#define SPI_NOR_QUAD_READ 0x40 /* Flash supports Quad Read */
  3674. -#define USE_FSR 0x80 /* use flag status register */
  3675. +#define SECT_4K BIT(0) /* SPINOR_OP_BE_4K works uniformly */
  3676. +#define SPI_NOR_NO_ERASE BIT(1) /* No erase command needed */
  3677. +#define SST_WRITE BIT(2) /* use SST byte programming */
  3678. +#define SPI_NOR_NO_FR BIT(3) /* Can't do fastread */
  3679. +#define SECT_4K_PMC BIT(4) /* SPINOR_OP_BE_4K_PMC works uniformly */
  3680. +#define SPI_NOR_DUAL_READ BIT(5) /* Flash supports Dual Read */
  3681. +#define SPI_NOR_QUAD_READ BIT(6) /* Flash supports Quad Read */
  3682. +#define USE_FSR BIT(7) /* use flag status register */
  3683. +#define SPI_NOR_HAS_LOCK BIT(8) /* Flash supports lock/unlock via SR */
  3684. +#define SPI_NOR_HAS_TB BIT(9) /*
  3685. + * Flash SR has Top/Bottom (TB) protect
  3686. + * bit. Must be used with
  3687. + * SPI_NOR_HAS_LOCK.
  3688. + */
  3689. };
  3690. #define JEDEC_MFR(info) ((info)->id[0])
  3691. @@ -313,6 +320,29 @@
  3692. }
  3693. /*
  3694. + * Initiate the erasure of a single sector
  3695. + */
  3696. +static int spi_nor_erase_sector(struct spi_nor *nor, u32 addr)
  3697. +{
  3698. + u8 buf[SPI_NOR_MAX_ADDR_WIDTH];
  3699. + int i;
  3700. +
  3701. + if (nor->erase)
  3702. + return nor->erase(nor, addr);
  3703. +
  3704. + /*
  3705. + * Default implementation, if driver doesn't have a specialized HW
  3706. + * control
  3707. + */
  3708. + for (i = nor->addr_width - 1; i >= 0; i--) {
  3709. + buf[i] = addr & 0xff;
  3710. + addr >>= 8;
  3711. + }
  3712. +
  3713. + return nor->write_reg(nor, nor->erase_opcode, buf, nor->addr_width);
  3714. +}
  3715. +
  3716. +/*
  3717. * Erase an address range on the nor chip. The address range may extend
  3718. * one or more erase sectors. Return an error is there is a problem erasing.
  3719. */
  3720. @@ -371,10 +401,9 @@
  3721. while (len) {
  3722. write_enable(nor);
  3723. - if (nor->erase(nor, addr)) {
  3724. - ret = -EIO;
  3725. + ret = spi_nor_erase_sector(nor, addr);
  3726. + if (ret)
  3727. goto erase_err;
  3728. - }
  3729. addr += mtd->erasesize;
  3730. len -= mtd->erasesize;
  3731. @@ -387,17 +416,13 @@
  3732. write_disable(nor);
  3733. +erase_err:
  3734. spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_ERASE);
  3735. - instr->state = MTD_ERASE_DONE;
  3736. + instr->state = ret ? MTD_ERASE_FAILED : MTD_ERASE_DONE;
  3737. mtd_erase_callback(instr);
  3738. return ret;
  3739. -
  3740. -erase_err:
  3741. - spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_ERASE);
  3742. - instr->state = MTD_ERASE_FAILED;
  3743. - return ret;
  3744. }
  3745. static void stm_get_locked_range(struct spi_nor *nor, u8 sr, loff_t *ofs,
  3746. @@ -415,32 +440,58 @@
  3747. } else {
  3748. pow = ((sr & mask) ^ mask) >> shift;
  3749. *len = mtd->size >> pow;
  3750. - *ofs = mtd->size - *len;
  3751. + if (nor->flags & SNOR_F_HAS_SR_TB && sr & SR_TB)
  3752. + *ofs = 0;
  3753. + else
  3754. + *ofs = mtd->size - *len;
  3755. }
  3756. }
  3757. /*
  3758. - * Return 1 if the entire region is locked, 0 otherwise
  3759. + * Return 1 if the entire region is locked (if @locked is true) or unlocked (if
  3760. + * @locked is false); 0 otherwise
  3761. */
  3762. -static int stm_is_locked_sr(struct spi_nor *nor, loff_t ofs, uint64_t len,
  3763. - u8 sr)
  3764. +static int stm_check_lock_status_sr(struct spi_nor *nor, loff_t ofs, uint64_t len,
  3765. + u8 sr, bool locked)
  3766. {
  3767. loff_t lock_offs;
  3768. uint64_t lock_len;
  3769. + if (!len)
  3770. + return 1;
  3771. +
  3772. stm_get_locked_range(nor, sr, &lock_offs, &lock_len);
  3773. - return (ofs + len <= lock_offs + lock_len) && (ofs >= lock_offs);
  3774. + if (locked)
  3775. + /* Requested range is a sub-range of locked range */
  3776. + return (ofs + len <= lock_offs + lock_len) && (ofs >= lock_offs);
  3777. + else
  3778. + /* Requested range does not overlap with locked range */
  3779. + return (ofs >= lock_offs + lock_len) || (ofs + len <= lock_offs);
  3780. +}
  3781. +
  3782. +static int stm_is_locked_sr(struct spi_nor *nor, loff_t ofs, uint64_t len,
  3783. + u8 sr)
  3784. +{
  3785. + return stm_check_lock_status_sr(nor, ofs, len, sr, true);
  3786. +}
  3787. +
  3788. +static int stm_is_unlocked_sr(struct spi_nor *nor, loff_t ofs, uint64_t len,
  3789. + u8 sr)
  3790. +{
  3791. + return stm_check_lock_status_sr(nor, ofs, len, sr, false);
  3792. }
  3793. /*
  3794. * Lock a region of the flash. Compatible with ST Micro and similar flash.
  3795. - * Supports only the block protection bits BP{0,1,2} in the status register
  3796. + * Supports the block protection bits BP{0,1,2} in the status register
  3797. * (SR). Does not support these features found in newer SR bitfields:
  3798. - * - TB: top/bottom protect - only handle TB=0 (top protect)
  3799. * - SEC: sector/block protect - only handle SEC=0 (block protect)
  3800. * - CMP: complement protect - only support CMP=0 (range is not complemented)
  3801. *
  3802. + * Support for the following is provided conditionally for some flash:
  3803. + * - TB: top/bottom protect
  3804. + *
  3805. * Sample table portion for 8MB flash (Winbond w25q64fw):
  3806. *
  3807. * SEC | TB | BP2 | BP1 | BP0 | Prot Length | Protected Portion
  3808. @@ -453,26 +504,55 @@
  3809. * 0 | 0 | 1 | 0 | 1 | 2 MB | Upper 1/4
  3810. * 0 | 0 | 1 | 1 | 0 | 4 MB | Upper 1/2
  3811. * X | X | 1 | 1 | 1 | 8 MB | ALL
  3812. + * ------|-------|-------|-------|-------|---------------|-------------------
  3813. + * 0 | 1 | 0 | 0 | 1 | 128 KB | Lower 1/64
  3814. + * 0 | 1 | 0 | 1 | 0 | 256 KB | Lower 1/32
  3815. + * 0 | 1 | 0 | 1 | 1 | 512 KB | Lower 1/16
  3816. + * 0 | 1 | 1 | 0 | 0 | 1 MB | Lower 1/8
  3817. + * 0 | 1 | 1 | 0 | 1 | 2 MB | Lower 1/4
  3818. + * 0 | 1 | 1 | 1 | 0 | 4 MB | Lower 1/2
  3819. *
  3820. * Returns negative on errors, 0 on success.
  3821. */
  3822. static int stm_lock(struct spi_nor *nor, loff_t ofs, uint64_t len)
  3823. {
  3824. struct mtd_info *mtd = &nor->mtd;
  3825. - u8 status_old, status_new;
  3826. + int status_old, status_new;
  3827. u8 mask = SR_BP2 | SR_BP1 | SR_BP0;
  3828. u8 shift = ffs(mask) - 1, pow, val;
  3829. + loff_t lock_len;
  3830. + bool can_be_top = true, can_be_bottom = nor->flags & SNOR_F_HAS_SR_TB;
  3831. + bool use_top;
  3832. + int ret;
  3833. status_old = read_sr(nor);
  3834. + if (status_old < 0)
  3835. + return status_old;
  3836. - /* SPI NOR always locks to the end */
  3837. - if (ofs + len != mtd->size) {
  3838. - /* Does combined region extend to end? */
  3839. - if (!stm_is_locked_sr(nor, ofs + len, mtd->size - ofs - len,
  3840. - status_old))
  3841. - return -EINVAL;
  3842. - len = mtd->size - ofs;
  3843. - }
  3844. + /* If nothing in our range is unlocked, we don't need to do anything */
  3845. + if (stm_is_locked_sr(nor, ofs, len, status_old))
  3846. + return 0;
  3847. +
  3848. + /* If anything below us is unlocked, we can't use 'bottom' protection */
  3849. + if (!stm_is_locked_sr(nor, 0, ofs, status_old))
  3850. + can_be_bottom = false;
  3851. +
  3852. + /* If anything above us is unlocked, we can't use 'top' protection */
  3853. + if (!stm_is_locked_sr(nor, ofs + len, mtd->size - (ofs + len),
  3854. + status_old))
  3855. + can_be_top = false;
  3856. +
  3857. + if (!can_be_bottom && !can_be_top)
  3858. + return -EINVAL;
  3859. +
  3860. + /* Prefer top, if both are valid */
  3861. + use_top = can_be_top;
  3862. +
  3863. + /* lock_len: length of region that should end up locked */
  3864. + if (use_top)
  3865. + lock_len = mtd->size - ofs;
  3866. + else
  3867. + lock_len = ofs + len;
  3868. /*
  3869. * Need smallest pow such that:
  3870. @@ -483,7 +563,7 @@
  3871. *
  3872. * pow = ceil(log2(size / len)) = log2(size) - floor(log2(len))
  3873. */
  3874. - pow = ilog2(mtd->size) - ilog2(len);
  3875. + pow = ilog2(mtd->size) - ilog2(lock_len);
  3876. val = mask - (pow << shift);
  3877. if (val & ~mask)
  3878. return -EINVAL;
  3879. @@ -491,14 +571,27 @@
  3880. if (!(val & mask))
  3881. return -EINVAL;
  3882. - status_new = (status_old & ~mask) | val;
  3883. + status_new = (status_old & ~mask & ~SR_TB) | val;
  3884. +
  3885. + /* Disallow further writes if WP pin is asserted */
  3886. + status_new |= SR_SRWD;
  3887. +
  3888. + if (!use_top)
  3889. + status_new |= SR_TB;
  3890. +
  3891. + /* Don't bother if they're the same */
  3892. + if (status_new == status_old)
  3893. + return 0;
  3894. /* Only modify protection if it will not unlock other areas */
  3895. - if ((status_new & mask) <= (status_old & mask))
  3896. + if ((status_new & mask) < (status_old & mask))
  3897. return -EINVAL;
  3898. write_enable(nor);
  3899. - return write_sr(nor, status_new);
  3900. + ret = write_sr(nor, status_new);
  3901. + if (ret)
  3902. + return ret;
  3903. + return spi_nor_wait_till_ready(nor);
  3904. }
  3905. /*
  3906. @@ -509,17 +602,43 @@
  3907. static int stm_unlock(struct spi_nor *nor, loff_t ofs, uint64_t len)
  3908. {
  3909. struct mtd_info *mtd = &nor->mtd;
  3910. - uint8_t status_old, status_new;
  3911. + int status_old, status_new;
  3912. u8 mask = SR_BP2 | SR_BP1 | SR_BP0;
  3913. u8 shift = ffs(mask) - 1, pow, val;
  3914. + loff_t lock_len;
  3915. + bool can_be_top = true, can_be_bottom = nor->flags & SNOR_F_HAS_SR_TB;
  3916. + bool use_top;
  3917. + int ret;
  3918. status_old = read_sr(nor);
  3919. + if (status_old < 0)
  3920. + return status_old;
  3921. +
  3922. + /* If nothing in our range is locked, we don't need to do anything */
  3923. + if (stm_is_unlocked_sr(nor, ofs, len, status_old))
  3924. + return 0;
  3925. +
  3926. + /* If anything below us is locked, we can't use 'top' protection */
  3927. + if (!stm_is_unlocked_sr(nor, 0, ofs, status_old))
  3928. + can_be_top = false;
  3929. +
  3930. + /* If anything above us is locked, we can't use 'bottom' protection */
  3931. + if (!stm_is_unlocked_sr(nor, ofs + len, mtd->size - (ofs + len),
  3932. + status_old))
  3933. + can_be_bottom = false;
  3934. - /* Cannot unlock; would unlock larger region than requested */
  3935. - if (stm_is_locked_sr(nor, ofs - mtd->erasesize, mtd->erasesize,
  3936. - status_old))
  3937. + if (!can_be_bottom && !can_be_top)
  3938. return -EINVAL;
  3939. + /* Prefer top, if both are valid */
  3940. + use_top = can_be_top;
  3941. +
  3942. + /* lock_len: length of region that should remain locked */
  3943. + if (use_top)
  3944. + lock_len = mtd->size - (ofs + len);
  3945. + else
  3946. + lock_len = ofs;
  3947. +
  3948. /*
  3949. * Need largest pow such that:
  3950. *
  3951. @@ -529,8 +648,8 @@
  3952. *
  3953. * pow = floor(log2(size / len)) = log2(size) - ceil(log2(len))
  3954. */
  3955. - pow = ilog2(mtd->size) - order_base_2(mtd->size - (ofs + len));
  3956. - if (ofs + len == mtd->size) {
  3957. + pow = ilog2(mtd->size) - order_base_2(lock_len);
  3958. + if (lock_len == 0) {
  3959. val = 0; /* fully unlocked */
  3960. } else {
  3961. val = mask - (pow << shift);
  3962. @@ -539,14 +658,28 @@
  3963. return -EINVAL;
  3964. }
  3965. - status_new = (status_old & ~mask) | val;
  3966. + status_new = (status_old & ~mask & ~SR_TB) | val;
  3967. +
  3968. + /* Don't protect status register if we're fully unlocked */
  3969. + if (lock_len == mtd->size)
  3970. + status_new &= ~SR_SRWD;
  3971. +
  3972. + if (!use_top)
  3973. + status_new |= SR_TB;
  3974. +
  3975. + /* Don't bother if they're the same */
  3976. + if (status_new == status_old)
  3977. + return 0;
  3978. /* Only modify protection if it will not lock other areas */
  3979. - if ((status_new & mask) >= (status_old & mask))
  3980. + if ((status_new & mask) > (status_old & mask))
  3981. return -EINVAL;
  3982. write_enable(nor);
  3983. - return write_sr(nor, status_new);
  3984. + ret = write_sr(nor, status_new);
  3985. + if (ret)
  3986. + return ret;
  3987. + return spi_nor_wait_till_ready(nor);
  3988. }
  3989. /*
  3990. @@ -715,9 +848,9 @@
  3991. { "mx25l4005a", INFO(0xc22013, 0, 64 * 1024, 8, SECT_4K) },
  3992. { "mx25l8005", INFO(0xc22014, 0, 64 * 1024, 16, 0) },
  3993. { "mx25l1606e", INFO(0xc22015, 0, 64 * 1024, 32, SECT_4K) },
  3994. - { "mx25l3205d", INFO(0xc22016, 0, 64 * 1024, 64, 0) },
  3995. + { "mx25l3205d", INFO(0xc22016, 0, 64 * 1024, 64, SECT_4K) },
  3996. { "mx25l3255e", INFO(0xc29e16, 0, 64 * 1024, 64, SECT_4K) },
  3997. - { "mx25l6405d", INFO(0xc22017, 0, 64 * 1024, 128, 0) },
  3998. + { "mx25l6405d", INFO(0xc22017, 0, 64 * 1024, 128, SECT_4K) },
  3999. { "mx25u6435f", INFO(0xc22537, 0, 64 * 1024, 128, SECT_4K) },
  4000. { "mx25l12805d", INFO(0xc22018, 0, 64 * 1024, 256, 0) },
  4001. { "mx25l12855e", INFO(0xc22618, 0, 64 * 1024, 256, 0) },
  4002. @@ -731,8 +864,8 @@
  4003. { "n25q032a", INFO(0x20bb16, 0, 64 * 1024, 64, SPI_NOR_QUAD_READ) },
  4004. { "n25q064", INFO(0x20ba17, 0, 64 * 1024, 128, SECT_4K | SPI_NOR_QUAD_READ) },
  4005. { "n25q064a", INFO(0x20bb17, 0, 64 * 1024, 128, SECT_4K | SPI_NOR_QUAD_READ) },
  4006. - { "n25q128a11", INFO(0x20bb18, 0, 64 * 1024, 256, SPI_NOR_QUAD_READ) },
  4007. - { "n25q128a13", INFO(0x20ba18, 0, 64 * 1024, 256, SPI_NOR_QUAD_READ) },
  4008. + { "n25q128a11", INFO(0x20bb18, 0, 64 * 1024, 256, SECT_4K | SPI_NOR_QUAD_READ) },
  4009. + { "n25q128a13", INFO(0x20ba18, 0, 64 * 1024, 256, SECT_4K | SPI_NOR_QUAD_READ) },
  4010. { "n25q256a", INFO(0x20ba19, 0, 64 * 1024, 512, SECT_4K | SPI_NOR_QUAD_READ) },
  4011. { "n25q512a", INFO(0x20bb20, 0, 64 * 1024, 1024, SECT_4K | USE_FSR | SPI_NOR_QUAD_READ) },
  4012. { "n25q512ax3", INFO(0x20ba20, 0, 64 * 1024, 1024, SECT_4K | USE_FSR | SPI_NOR_QUAD_READ) },
  4013. @@ -766,6 +899,7 @@
  4014. { "s25fl008k", INFO(0xef4014, 0, 64 * 1024, 16, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
  4015. { "s25fl016k", INFO(0xef4015, 0, 64 * 1024, 32, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
  4016. { "s25fl064k", INFO(0xef4017, 0, 64 * 1024, 128, SECT_4K) },
  4017. + { "s25fl116k", INFO(0x014015, 0, 64 * 1024, 32, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
  4018. { "s25fl132k", INFO(0x014016, 0, 64 * 1024, 64, SECT_4K) },
  4019. { "s25fl164k", INFO(0x014017, 0, 64 * 1024, 128, SECT_4K) },
  4020. { "s25fl204k", INFO(0x014013, 0, 64 * 1024, 8, SECT_4K | SPI_NOR_DUAL_READ) },
  4021. @@ -829,11 +963,23 @@
  4022. { "w25x16", INFO(0xef3015, 0, 64 * 1024, 32, SECT_4K) },
  4023. { "w25x32", INFO(0xef3016, 0, 64 * 1024, 64, SECT_4K) },
  4024. { "w25q32", INFO(0xef4016, 0, 64 * 1024, 64, SECT_4K) },
  4025. - { "w25q32dw", INFO(0xef6016, 0, 64 * 1024, 64, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
  4026. + {
  4027. + "w25q32dw", INFO(0xef6016, 0, 64 * 1024, 64,
  4028. + SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
  4029. + SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB)
  4030. + },
  4031. { "w25x64", INFO(0xef3017, 0, 64 * 1024, 128, SECT_4K) },
  4032. { "w25q64", INFO(0xef4017, 0, 64 * 1024, 128, SECT_4K) },
  4033. - { "w25q64dw", INFO(0xef6017, 0, 64 * 1024, 128, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
  4034. - { "w25q128fw", INFO(0xef6018, 0, 64 * 1024, 256, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
  4035. + {
  4036. + "w25q64dw", INFO(0xef6017, 0, 64 * 1024, 128,
  4037. + SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
  4038. + SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB)
  4039. + },
  4040. + {
  4041. + "w25q128fw", INFO(0xef6018, 0, 64 * 1024, 256,
  4042. + SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
  4043. + SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB)
  4044. + },
  4045. { "w25q80", INFO(0xef5014, 0, 64 * 1024, 16, SECT_4K) },
  4046. { "w25q80bl", INFO(0xef4014, 0, 64 * 1024, 16, SECT_4K) },
  4047. { "w25q128", INFO(0xef4018, 0, 64 * 1024, 256, SECT_4K) },
  4048. @@ -856,7 +1002,7 @@
  4049. tmp = nor->read_reg(nor, SPINOR_OP_RDID, id, SPI_NOR_MAX_ID_LEN);
  4050. if (tmp < 0) {
  4051. - dev_dbg(nor->dev, " error %d reading JEDEC ID\n", tmp);
  4052. + dev_dbg(nor->dev, "error %d reading JEDEC ID\n", tmp);
  4053. return ERR_PTR(tmp);
  4054. }
  4055. @@ -867,7 +1013,7 @@
  4056. return &spi_nor_ids[tmp];
  4057. }
  4058. }
  4059. - dev_err(nor->dev, "unrecognized JEDEC id bytes: %02x, %2x, %2x\n",
  4060. + dev_err(nor->dev, "unrecognized JEDEC id bytes: %02x, %02x, %02x\n",
  4061. id[0], id[1], id[2]);
  4062. return ERR_PTR(-ENODEV);
  4063. }
  4064. @@ -1013,6 +1159,8 @@
  4065. int ret, val;
  4066. val = read_sr(nor);
  4067. + if (val < 0)
  4068. + return val;
  4069. write_enable(nor);
  4070. write_sr(nor, val | SR_QUAD_EN_MX);
  4071. @@ -1094,7 +1242,7 @@
  4072. static int spi_nor_check(struct spi_nor *nor)
  4073. {
  4074. if (!nor->dev || !nor->read || !nor->write ||
  4075. - !nor->read_reg || !nor->write_reg || !nor->erase) {
  4076. + !nor->read_reg || !nor->write_reg) {
  4077. pr_err("spi-nor: please fill all the necessary fields!\n");
  4078. return -EINVAL;
  4079. }
  4080. @@ -1107,7 +1255,7 @@
  4081. const struct flash_info *info = NULL;
  4082. struct device *dev = nor->dev;
  4083. struct mtd_info *mtd = &nor->mtd;
  4084. - struct device_node *np = nor->flash_node;
  4085. + struct device_node *np = spi_nor_get_flash_node(nor);
  4086. int ret;
  4087. int i;
  4088. @@ -1157,9 +1305,11 @@
  4089. if (JEDEC_MFR(info) == SNOR_MFR_ATMEL ||
  4090. JEDEC_MFR(info) == SNOR_MFR_INTEL ||
  4091. JEDEC_MFR(info) == SNOR_MFR_MACRONIX ||
  4092. - JEDEC_MFR(info) == SNOR_MFR_SST) {
  4093. + JEDEC_MFR(info) == SNOR_MFR_SST ||
  4094. + info->flags & SPI_NOR_HAS_LOCK) {
  4095. write_enable(nor);
  4096. write_sr(nor, 0);
  4097. + spi_nor_wait_till_ready(nor);
  4098. }
  4099. if (!mtd->name)
  4100. @@ -1173,7 +1323,8 @@
  4101. mtd->_read = spi_nor_read;
  4102. /* NOR protection support for STmicro/Micron chips and similar */
  4103. - if (JEDEC_MFR(info) == SNOR_MFR_MICRON) {
  4104. + if (JEDEC_MFR(info) == SNOR_MFR_MICRON ||
  4105. + info->flags & SPI_NOR_HAS_LOCK) {
  4106. nor->flash_lock = stm_lock;
  4107. nor->flash_unlock = stm_unlock;
  4108. nor->flash_is_locked = stm_is_locked;
  4109. @@ -1193,6 +1344,8 @@
  4110. if (info->flags & USE_FSR)
  4111. nor->flags |= SNOR_F_USE_FSR;
  4112. + if (info->flags & SPI_NOR_HAS_TB)
  4113. + nor->flags |= SNOR_F_HAS_SR_TB;
  4114. #ifdef CONFIG_MTD_SPI_NOR_USE_4K_SECTORS
  4115. /* prefer "small sector" erase if possible */
  4116. @@ -1295,6 +1448,12 @@
  4117. nor->addr_width = 3;
  4118. }
  4119. + if (nor->addr_width > SPI_NOR_MAX_ADDR_WIDTH) {
  4120. + dev_err(dev, "address width is too large: %u\n",
  4121. + nor->addr_width);
  4122. + return -EINVAL;
  4123. + }
  4124. +
  4125. nor->read_dummy = spi_nor_read_dummy_cycles(nor);
  4126. dev_info(dev, "%s (%lld Kbytes)\n", info->name,
  4127. --- a/drivers/mtd/tests/mtd_nandecctest.c
  4128. +++ b/drivers/mtd/tests/mtd_nandecctest.c
  4129. @@ -187,7 +187,7 @@
  4130. __nand_calculate_ecc(error_data, size, calc_ecc);
  4131. ret = __nand_correct_data(error_data, error_ecc, calc_ecc, size);
  4132. - return (ret == -1) ? 0 : -EINVAL;
  4133. + return (ret == -EBADMSG) ? 0 : -EINVAL;
  4134. }
  4135. static const struct nand_ecc_test nand_ecc_test[] = {
  4136. --- a/drivers/mtd/tests/oobtest.c
  4137. +++ b/drivers/mtd/tests/oobtest.c
  4138. @@ -215,19 +215,19 @@
  4139. pr_info("ignoring error as within bitflip_limit\n");
  4140. }
  4141. - if (use_offset != 0 || use_len < mtd->ecclayout->oobavail) {
  4142. + if (use_offset != 0 || use_len < mtd->oobavail) {
  4143. int k;
  4144. ops.mode = MTD_OPS_AUTO_OOB;
  4145. ops.len = 0;
  4146. ops.retlen = 0;
  4147. - ops.ooblen = mtd->ecclayout->oobavail;
  4148. + ops.ooblen = mtd->oobavail;
  4149. ops.oobretlen = 0;
  4150. ops.ooboffs = 0;
  4151. ops.datbuf = NULL;
  4152. ops.oobbuf = readbuf;
  4153. err = mtd_read_oob(mtd, addr, &ops);
  4154. - if (err || ops.oobretlen != mtd->ecclayout->oobavail) {
  4155. + if (err || ops.oobretlen != mtd->oobavail) {
  4156. pr_err("error: readoob failed at %#llx\n",
  4157. (long long)addr);
  4158. errcnt += 1;
  4159. @@ -244,7 +244,7 @@
  4160. /* verify post-(use_offset + use_len) area for 0xff */
  4161. k = use_offset + use_len;
  4162. bitflips += memffshow(addr, k, readbuf + k,
  4163. - mtd->ecclayout->oobavail - k);
  4164. + mtd->oobavail - k);
  4165. if (bitflips > bitflip_limit) {
  4166. pr_err("error: verify failed at %#llx\n",
  4167. @@ -269,8 +269,8 @@
  4168. struct mtd_oob_ops ops;
  4169. int err = 0;
  4170. loff_t addr = (loff_t)ebnum * mtd->erasesize;
  4171. - size_t len = mtd->ecclayout->oobavail * pgcnt;
  4172. - size_t oobavail = mtd->ecclayout->oobavail;
  4173. + size_t len = mtd->oobavail * pgcnt;
  4174. + size_t oobavail = mtd->oobavail;
  4175. size_t bitflips;
  4176. int i;
  4177. @@ -394,8 +394,8 @@
  4178. goto out;
  4179. use_offset = 0;
  4180. - use_len = mtd->ecclayout->oobavail;
  4181. - use_len_max = mtd->ecclayout->oobavail;
  4182. + use_len = mtd->oobavail;
  4183. + use_len_max = mtd->oobavail;
  4184. vary_offset = 0;
  4185. /* First test: write all OOB, read it back and verify */
  4186. @@ -460,8 +460,8 @@
  4187. /* Write all eraseblocks */
  4188. use_offset = 0;
  4189. - use_len = mtd->ecclayout->oobavail;
  4190. - use_len_max = mtd->ecclayout->oobavail;
  4191. + use_len = mtd->oobavail;
  4192. + use_len_max = mtd->oobavail;
  4193. vary_offset = 1;
  4194. prandom_seed_state(&rnd_state, 5);
  4195. @@ -471,8 +471,8 @@
  4196. /* Check all eraseblocks */
  4197. use_offset = 0;
  4198. - use_len = mtd->ecclayout->oobavail;
  4199. - use_len_max = mtd->ecclayout->oobavail;
  4200. + use_len = mtd->oobavail;
  4201. + use_len_max = mtd->oobavail;
  4202. vary_offset = 1;
  4203. prandom_seed_state(&rnd_state, 5);
  4204. err = verify_all_eraseblocks();
  4205. @@ -480,8 +480,8 @@
  4206. goto out;
  4207. use_offset = 0;
  4208. - use_len = mtd->ecclayout->oobavail;
  4209. - use_len_max = mtd->ecclayout->oobavail;
  4210. + use_len = mtd->oobavail;
  4211. + use_len_max = mtd->oobavail;
  4212. vary_offset = 0;
  4213. /* Fourth test: try to write off end of device */
  4214. @@ -501,7 +501,7 @@
  4215. ops.retlen = 0;
  4216. ops.ooblen = 1;
  4217. ops.oobretlen = 0;
  4218. - ops.ooboffs = mtd->ecclayout->oobavail;
  4219. + ops.ooboffs = mtd->oobavail;
  4220. ops.datbuf = NULL;
  4221. ops.oobbuf = writebuf;
  4222. pr_info("attempting to start write past end of OOB\n");
  4223. @@ -521,7 +521,7 @@
  4224. ops.retlen = 0;
  4225. ops.ooblen = 1;
  4226. ops.oobretlen = 0;
  4227. - ops.ooboffs = mtd->ecclayout->oobavail;
  4228. + ops.ooboffs = mtd->oobavail;
  4229. ops.datbuf = NULL;
  4230. ops.oobbuf = readbuf;
  4231. pr_info("attempting to start read past end of OOB\n");
  4232. @@ -543,7 +543,7 @@
  4233. ops.mode = MTD_OPS_AUTO_OOB;
  4234. ops.len = 0;
  4235. ops.retlen = 0;
  4236. - ops.ooblen = mtd->ecclayout->oobavail + 1;
  4237. + ops.ooblen = mtd->oobavail + 1;
  4238. ops.oobretlen = 0;
  4239. ops.ooboffs = 0;
  4240. ops.datbuf = NULL;
  4241. @@ -563,7 +563,7 @@
  4242. ops.mode = MTD_OPS_AUTO_OOB;
  4243. ops.len = 0;
  4244. ops.retlen = 0;
  4245. - ops.ooblen = mtd->ecclayout->oobavail + 1;
  4246. + ops.ooblen = mtd->oobavail + 1;
  4247. ops.oobretlen = 0;
  4248. ops.ooboffs = 0;
  4249. ops.datbuf = NULL;
  4250. @@ -587,7 +587,7 @@
  4251. ops.mode = MTD_OPS_AUTO_OOB;
  4252. ops.len = 0;
  4253. ops.retlen = 0;
  4254. - ops.ooblen = mtd->ecclayout->oobavail;
  4255. + ops.ooblen = mtd->oobavail;
  4256. ops.oobretlen = 0;
  4257. ops.ooboffs = 1;
  4258. ops.datbuf = NULL;
  4259. @@ -607,7 +607,7 @@
  4260. ops.mode = MTD_OPS_AUTO_OOB;
  4261. ops.len = 0;
  4262. ops.retlen = 0;
  4263. - ops.ooblen = mtd->ecclayout->oobavail;
  4264. + ops.ooblen = mtd->oobavail;
  4265. ops.oobretlen = 0;
  4266. ops.ooboffs = 1;
  4267. ops.datbuf = NULL;
  4268. @@ -638,7 +638,7 @@
  4269. for (i = 0; i < ebcnt - 1; ++i) {
  4270. int cnt = 2;
  4271. int pg;
  4272. - size_t sz = mtd->ecclayout->oobavail;
  4273. + size_t sz = mtd->oobavail;
  4274. if (bbt[i] || bbt[i + 1])
  4275. continue;
  4276. addr = (loff_t)(i + 1) * mtd->erasesize - mtd->writesize;
  4277. @@ -673,13 +673,12 @@
  4278. for (i = 0; i < ebcnt - 1; ++i) {
  4279. if (bbt[i] || bbt[i + 1])
  4280. continue;
  4281. - prandom_bytes_state(&rnd_state, writebuf,
  4282. - mtd->ecclayout->oobavail * 2);
  4283. + prandom_bytes_state(&rnd_state, writebuf, mtd->oobavail * 2);
  4284. addr = (loff_t)(i + 1) * mtd->erasesize - mtd->writesize;
  4285. ops.mode = MTD_OPS_AUTO_OOB;
  4286. ops.len = 0;
  4287. ops.retlen = 0;
  4288. - ops.ooblen = mtd->ecclayout->oobavail * 2;
  4289. + ops.ooblen = mtd->oobavail * 2;
  4290. ops.oobretlen = 0;
  4291. ops.ooboffs = 0;
  4292. ops.datbuf = NULL;
  4293. @@ -688,7 +687,7 @@
  4294. if (err)
  4295. goto out;
  4296. if (memcmpshow(addr, readbuf, writebuf,
  4297. - mtd->ecclayout->oobavail * 2)) {
  4298. + mtd->oobavail * 2)) {
  4299. pr_err("error: verify failed at %#llx\n",
  4300. (long long)addr);
  4301. errcnt += 1;
  4302. --- a/drivers/mtd/tests/pagetest.c
  4303. +++ b/drivers/mtd/tests/pagetest.c
  4304. @@ -127,13 +127,12 @@
  4305. unsigned char *pp1, *pp2, *pp3, *pp4;
  4306. pr_info("crosstest\n");
  4307. - pp1 = kmalloc(pgsize * 4, GFP_KERNEL);
  4308. + pp1 = kzalloc(pgsize * 4, GFP_KERNEL);
  4309. if (!pp1)
  4310. return -ENOMEM;
  4311. pp2 = pp1 + pgsize;
  4312. pp3 = pp2 + pgsize;
  4313. pp4 = pp3 + pgsize;
  4314. - memset(pp1, 0, pgsize * 4);
  4315. addr0 = 0;
  4316. for (i = 0; i < ebcnt && bbt[i]; ++i)
  4317. --- a/drivers/mtd/ubi/cdev.c
  4318. +++ b/drivers/mtd/ubi/cdev.c
  4319. @@ -174,9 +174,9 @@
  4320. struct ubi_device *ubi = desc->vol->ubi;
  4321. struct inode *inode = file_inode(file);
  4322. int err;
  4323. - mutex_lock(&inode->i_mutex);
  4324. + inode_lock(inode);
  4325. err = ubi_sync(ubi->ubi_num);
  4326. - mutex_unlock(&inode->i_mutex);
  4327. + inode_unlock(inode);
  4328. return err;
  4329. }
  4330. --- a/drivers/mtd/ubi/misc.c
  4331. +++ b/drivers/mtd/ubi/misc.c
  4332. @@ -153,3 +153,52 @@
  4333. return 0;
  4334. return 1;
  4335. }
  4336. +
  4337. +/* Normal UBI messages */
  4338. +void ubi_msg(const struct ubi_device *ubi, const char *fmt, ...)
  4339. +{
  4340. + struct va_format vaf;
  4341. + va_list args;
  4342. +
  4343. + va_start(args, fmt);
  4344. +
  4345. + vaf.fmt = fmt;
  4346. + vaf.va = &args;
  4347. +
  4348. + pr_notice(UBI_NAME_STR "%d: %pV\n", ubi->ubi_num, &vaf);
  4349. +
  4350. + va_end(args);
  4351. +}
  4352. +
  4353. +/* UBI warning messages */
  4354. +void ubi_warn(const struct ubi_device *ubi, const char *fmt, ...)
  4355. +{
  4356. + struct va_format vaf;
  4357. + va_list args;
  4358. +
  4359. + va_start(args, fmt);
  4360. +
  4361. + vaf.fmt = fmt;
  4362. + vaf.va = &args;
  4363. +
  4364. + pr_warn(UBI_NAME_STR "%d warning: %ps: %pV\n",
  4365. + ubi->ubi_num, __builtin_return_address(0), &vaf);
  4366. +
  4367. + va_end(args);
  4368. +}
  4369. +
  4370. +/* UBI error messages */
  4371. +void ubi_err(const struct ubi_device *ubi, const char *fmt, ...)
  4372. +{
  4373. + struct va_format vaf;
  4374. + va_list args;
  4375. +
  4376. + va_start(args, fmt);
  4377. +
  4378. + vaf.fmt = fmt;
  4379. + vaf.va = &args;
  4380. +
  4381. + pr_err(UBI_NAME_STR "%d error: %ps: %pV\n",
  4382. + ubi->ubi_num, __builtin_return_address(0), &vaf);
  4383. + va_end(args);
  4384. +}
  4385. --- a/drivers/mtd/ubi/ubi.h
  4386. +++ b/drivers/mtd/ubi/ubi.h
  4387. @@ -49,15 +49,19 @@
  4388. /* UBI name used for character devices, sysfs, etc */
  4389. #define UBI_NAME_STR "ubi"
  4390. +struct ubi_device;
  4391. +
  4392. /* Normal UBI messages */
  4393. -#define ubi_msg(ubi, fmt, ...) pr_notice(UBI_NAME_STR "%d: " fmt "\n", \
  4394. - ubi->ubi_num, ##__VA_ARGS__)
  4395. +__printf(2, 3)
  4396. +void ubi_msg(const struct ubi_device *ubi, const char *fmt, ...);
  4397. +
  4398. /* UBI warning messages */
  4399. -#define ubi_warn(ubi, fmt, ...) pr_warn(UBI_NAME_STR "%d warning: %s: " fmt "\n", \
  4400. - ubi->ubi_num, __func__, ##__VA_ARGS__)
  4401. +__printf(2, 3)
  4402. +void ubi_warn(const struct ubi_device *ubi, const char *fmt, ...);
  4403. +
  4404. /* UBI error messages */
  4405. -#define ubi_err(ubi, fmt, ...) pr_err(UBI_NAME_STR "%d error: %s: " fmt "\n", \
  4406. - ubi->ubi_num, __func__, ##__VA_ARGS__)
  4407. +__printf(2, 3)
  4408. +void ubi_err(const struct ubi_device *ubi, const char *fmt, ...);
  4409. /* Background thread name pattern */
  4410. #define UBI_BGT_NAME_PATTERN "ubi_bgt%dd"
  4411. --- a/drivers/mtd/ubi/wl.c
  4412. +++ b/drivers/mtd/ubi/wl.c
  4413. @@ -628,6 +628,7 @@
  4414. return __erase_worker(ubi, &wl_wrk);
  4415. }
  4416. +static int ensure_wear_leveling(struct ubi_device *ubi, int nested);
  4417. /**
  4418. * wear_leveling_worker - wear-leveling worker function.
  4419. * @ubi: UBI device description object
  4420. @@ -649,6 +650,7 @@
  4421. #endif
  4422. struct ubi_wl_entry *e1, *e2;
  4423. struct ubi_vid_hdr *vid_hdr;
  4424. + int dst_leb_clean = 0;
  4425. kfree(wrk);
  4426. if (shutdown)
  4427. @@ -753,6 +755,7 @@
  4428. err = ubi_io_read_vid_hdr(ubi, e1->pnum, vid_hdr, 0);
  4429. if (err && err != UBI_IO_BITFLIPS) {
  4430. + dst_leb_clean = 1;
  4431. if (err == UBI_IO_FF) {
  4432. /*
  4433. * We are trying to move PEB without a VID header. UBI
  4434. @@ -798,10 +801,12 @@
  4435. * protection queue.
  4436. */
  4437. protect = 1;
  4438. + dst_leb_clean = 1;
  4439. goto out_not_moved;
  4440. }
  4441. if (err == MOVE_RETRY) {
  4442. scrubbing = 1;
  4443. + dst_leb_clean = 1;
  4444. goto out_not_moved;
  4445. }
  4446. if (err == MOVE_TARGET_BITFLIPS || err == MOVE_TARGET_WR_ERR ||
  4447. @@ -827,6 +832,7 @@
  4448. ubi->erroneous_peb_count);
  4449. goto out_error;
  4450. }
  4451. + dst_leb_clean = 1;
  4452. erroneous = 1;
  4453. goto out_not_moved;
  4454. }
  4455. @@ -897,15 +903,24 @@
  4456. wl_tree_add(e1, &ubi->scrub);
  4457. else
  4458. wl_tree_add(e1, &ubi->used);
  4459. + if (dst_leb_clean) {
  4460. + wl_tree_add(e2, &ubi->free);
  4461. + ubi->free_count++;
  4462. + }
  4463. +
  4464. ubi_assert(!ubi->move_to_put);
  4465. ubi->move_from = ubi->move_to = NULL;
  4466. ubi->wl_scheduled = 0;
  4467. spin_unlock(&ubi->wl_lock);
  4468. ubi_free_vid_hdr(ubi, vid_hdr);
  4469. - err = do_sync_erase(ubi, e2, vol_id, lnum, torture);
  4470. - if (err)
  4471. - goto out_ro;
  4472. + if (dst_leb_clean) {
  4473. + ensure_wear_leveling(ubi, 1);
  4474. + } else {
  4475. + err = do_sync_erase(ubi, e2, vol_id, lnum, torture);
  4476. + if (err)
  4477. + goto out_ro;
  4478. + }
  4479. mutex_unlock(&ubi->move_mutex);
  4480. return 0;
  4481. --- a/include/linux/mtd/bbm.h
  4482. +++ b/include/linux/mtd/bbm.h
  4483. @@ -166,7 +166,6 @@
  4484. };
  4485. /* OneNAND BBT interface */
  4486. -extern int onenand_scan_bbt(struct mtd_info *mtd, struct nand_bbt_descr *bd);
  4487. extern int onenand_default_bbt(struct mtd_info *mtd);
  4488. #endif /* __LINUX_MTD_BBM_H */
  4489. --- a/include/linux/mtd/fsmc.h
  4490. +++ b/include/linux/mtd/fsmc.h
  4491. @@ -103,24 +103,6 @@
  4492. #define FSMC_BUSY_WAIT_TIMEOUT (1 * HZ)
  4493. -/*
  4494. - * There are 13 bytes of ecc for every 512 byte block in FSMC version 8
  4495. - * and it has to be read consecutively and immediately after the 512
  4496. - * byte data block for hardware to generate the error bit offsets
  4497. - * Managing the ecc bytes in the following way is easier. This way is
  4498. - * similar to oobfree structure maintained already in u-boot nand driver
  4499. - */
  4500. -#define MAX_ECCPLACE_ENTRIES 32
  4501. -
  4502. -struct fsmc_nand_eccplace {
  4503. - uint8_t offset;
  4504. - uint8_t length;
  4505. -};
  4506. -
  4507. -struct fsmc_eccplace {
  4508. - struct fsmc_nand_eccplace eccplace[MAX_ECCPLACE_ENTRIES];
  4509. -};
  4510. -
  4511. struct fsmc_nand_timings {
  4512. uint8_t tclr;
  4513. uint8_t tar;
  4514. --- a/include/linux/mtd/inftl.h
  4515. +++ b/include/linux/mtd/inftl.h
  4516. @@ -44,7 +44,6 @@
  4517. unsigned int nb_blocks; /* number of physical blocks */
  4518. unsigned int nb_boot_blocks; /* number of blocks used by the bios */
  4519. struct erase_info instr;
  4520. - struct nand_ecclayout oobinfo;
  4521. };
  4522. int INFTL_mount(struct INFTLrecord *s);
  4523. --- a/include/linux/mtd/map.h
  4524. +++ b/include/linux/mtd/map.h
  4525. @@ -142,7 +142,9 @@
  4526. #endif
  4527. #ifndef map_bankwidth
  4528. +#ifdef CONFIG_MTD
  4529. #warning "No CONFIG_MTD_MAP_BANK_WIDTH_xx selected. No NOR chip support can work"
  4530. +#endif
  4531. static inline int map_bankwidth(void *map)
  4532. {
  4533. BUG();
  4534. @@ -238,8 +240,11 @@
  4535. If there is no cache to care about this can be set to NULL. */
  4536. void (*inval_cache)(struct map_info *, unsigned long, ssize_t);
  4537. - /* set_vpp() must handle being reentered -- enable, enable, disable
  4538. - must leave it enabled. */
  4539. + /* This will be called with 1 as parameter when the first map user
  4540. + * needs VPP, and called with 0 when the last user exits. The map
  4541. + * core maintains a reference counter, and assumes that VPP is a
  4542. + * global resource applying to all mapped flash chips on the system.
  4543. + */
  4544. void (*set_vpp)(struct map_info *, int);
  4545. unsigned long pfow_base;
  4546. --- a/include/linux/mtd/mtd.h
  4547. +++ b/include/linux/mtd/mtd.h
  4548. @@ -100,17 +100,35 @@
  4549. #define MTD_MAX_OOBFREE_ENTRIES_LARGE 32
  4550. #define MTD_MAX_ECCPOS_ENTRIES_LARGE 640
  4551. +/**
  4552. + * struct mtd_oob_region - oob region definition
  4553. + * @offset: region offset
  4554. + * @length: region length
  4555. + *
  4556. + * This structure describes a region of the OOB area, and is used
  4557. + * to retrieve ECC or free bytes sections.
  4558. + * Each section is defined by an offset within the OOB area and a
  4559. + * length.
  4560. + */
  4561. +struct mtd_oob_region {
  4562. + u32 offset;
  4563. + u32 length;
  4564. +};
  4565. +
  4566. /*
  4567. - * Internal ECC layout control structure. For historical reasons, there is a
  4568. - * similar, smaller struct nand_ecclayout_user (in mtd-abi.h) that is retained
  4569. - * for export to user-space via the ECCGETLAYOUT ioctl.
  4570. - * nand_ecclayout should be expandable in the future simply by the above macros.
  4571. + * struct mtd_ooblayout_ops - NAND OOB layout operations
  4572. + * @ecc: function returning an ECC region in the OOB area.
  4573. + * Should return -ERANGE if %section exceeds the total number of
  4574. + * ECC sections.
  4575. + * @free: function returning a free region in the OOB area.
  4576. + * Should return -ERANGE if %section exceeds the total number of
  4577. + * free sections.
  4578. */
  4579. -struct nand_ecclayout {
  4580. - __u32 eccbytes;
  4581. - __u32 eccpos[MTD_MAX_ECCPOS_ENTRIES_LARGE];
  4582. - __u32 oobavail;
  4583. - struct nand_oobfree oobfree[MTD_MAX_OOBFREE_ENTRIES_LARGE];
  4584. +struct mtd_ooblayout_ops {
  4585. + int (*ecc)(struct mtd_info *mtd, int section,
  4586. + struct mtd_oob_region *oobecc);
  4587. + int (*free)(struct mtd_info *mtd, int section,
  4588. + struct mtd_oob_region *oobfree);
  4589. };
  4590. struct module; /* only needed for owner field in mtd_info */
  4591. @@ -171,8 +189,8 @@
  4592. const char *name;
  4593. int index;
  4594. - /* ECC layout structure pointer - read only! */
  4595. - struct nand_ecclayout *ecclayout;
  4596. + /* OOB layout description */
  4597. + const struct mtd_ooblayout_ops *ooblayout;
  4598. /* the ecc step size. */
  4599. unsigned int ecc_step_size;
  4600. @@ -258,6 +276,46 @@
  4601. int usecount;
  4602. };
  4603. +int mtd_ooblayout_ecc(struct mtd_info *mtd, int section,
  4604. + struct mtd_oob_region *oobecc);
  4605. +int mtd_ooblayout_find_eccregion(struct mtd_info *mtd, int eccbyte,
  4606. + int *section,
  4607. + struct mtd_oob_region *oobregion);
  4608. +int mtd_ooblayout_get_eccbytes(struct mtd_info *mtd, u8 *eccbuf,
  4609. + const u8 *oobbuf, int start, int nbytes);
  4610. +int mtd_ooblayout_set_eccbytes(struct mtd_info *mtd, const u8 *eccbuf,
  4611. + u8 *oobbuf, int start, int nbytes);
  4612. +int mtd_ooblayout_free(struct mtd_info *mtd, int section,
  4613. + struct mtd_oob_region *oobfree);
  4614. +int mtd_ooblayout_get_databytes(struct mtd_info *mtd, u8 *databuf,
  4615. + const u8 *oobbuf, int start, int nbytes);
  4616. +int mtd_ooblayout_set_databytes(struct mtd_info *mtd, const u8 *databuf,
  4617. + u8 *oobbuf, int start, int nbytes);
  4618. +int mtd_ooblayout_count_freebytes(struct mtd_info *mtd);
  4619. +int mtd_ooblayout_count_eccbytes(struct mtd_info *mtd);
  4620. +
  4621. +static inline void mtd_set_ooblayout(struct mtd_info *mtd,
  4622. + const struct mtd_ooblayout_ops *ooblayout)
  4623. +{
  4624. + mtd->ooblayout = ooblayout;
  4625. +}
  4626. +
  4627. +static inline void mtd_set_of_node(struct mtd_info *mtd,
  4628. + struct device_node *np)
  4629. +{
  4630. + mtd->dev.of_node = np;
  4631. +}
  4632. +
  4633. +static inline struct device_node *mtd_get_of_node(struct mtd_info *mtd)
  4634. +{
  4635. + return mtd->dev.of_node;
  4636. +}
  4637. +
  4638. +static inline int mtd_oobavail(struct mtd_info *mtd, struct mtd_oob_ops *ops)
  4639. +{
  4640. + return ops->mode == MTD_OPS_AUTO_OOB ? mtd->oobavail : mtd->oobsize;
  4641. +}
  4642. +
  4643. int mtd_erase(struct mtd_info *mtd, struct erase_info *instr);
  4644. int mtd_point(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen,
  4645. void **virt, resource_size_t *phys);
  4646. --- a/include/linux/mtd/nand.h
  4647. +++ b/include/linux/mtd/nand.h
  4648. @@ -119,6 +119,12 @@
  4649. NAND_ECC_SOFT_BCH,
  4650. } nand_ecc_modes_t;
  4651. +enum nand_ecc_algo {
  4652. + NAND_ECC_UNKNOWN,
  4653. + NAND_ECC_HAMMING,
  4654. + NAND_ECC_BCH,
  4655. +};
  4656. +
  4657. /*
  4658. * Constants for Hardware ECC
  4659. */
  4660. @@ -129,6 +135,14 @@
  4661. /* Enable Hardware ECC before syndrome is read back from flash */
  4662. #define NAND_ECC_READSYN 2
  4663. +/*
  4664. + * Enable generic NAND 'page erased' check. This check is only done when
  4665. + * ecc.correct() returns -EBADMSG.
  4666. + * Set this flag if your implementation does not fix bitflips in erased
  4667. + * pages and you want to rely on the default implementation.
  4668. + */
  4669. +#define NAND_ECC_GENERIC_ERASED_CHECK BIT(0)
  4670. +
  4671. /* Bit mask for flags passed to do_nand_read_ecc */
  4672. #define NAND_GET_DEVICE 0x80
  4673. @@ -160,6 +174,12 @@
  4674. /* Device supports subpage reads */
  4675. #define NAND_SUBPAGE_READ 0x00001000
  4676. +/*
  4677. + * Some MLC NANDs need data scrambling to limit bitflips caused by repeated
  4678. + * patterns.
  4679. + */
  4680. +#define NAND_NEED_SCRAMBLING 0x00002000
  4681. +
  4682. /* Options valid for Samsung large page devices */
  4683. #define NAND_SAMSUNG_LP_OPTIONS NAND_CACHEPRG
  4684. @@ -276,15 +296,15 @@
  4685. __le16 t_r;
  4686. __le16 t_ccs;
  4687. __le16 src_sync_timing_mode;
  4688. - __le16 src_ssync_features;
  4689. + u8 src_ssync_features;
  4690. __le16 clk_pin_capacitance_typ;
  4691. __le16 io_pin_capacitance_typ;
  4692. __le16 input_pin_capacitance_typ;
  4693. u8 input_pin_capacitance_max;
  4694. u8 driver_strength_support;
  4695. __le16 t_int_r;
  4696. - __le16 t_ald;
  4697. - u8 reserved4[7];
  4698. + __le16 t_adl;
  4699. + u8 reserved4[8];
  4700. /* vendor */
  4701. __le16 vendor_revision;
  4702. @@ -407,7 +427,7 @@
  4703. __le16 input_pin_capacitance_typ;
  4704. __le16 clk_pin_capacitance_typ;
  4705. u8 driver_strength_support;
  4706. - __le16 t_ald;
  4707. + __le16 t_adl;
  4708. u8 reserved4[36];
  4709. /* ECC and endurance block */
  4710. @@ -444,6 +464,7 @@
  4711. /**
  4712. * struct nand_ecc_ctrl - Control structure for ECC
  4713. * @mode: ECC mode
  4714. + * @algo: ECC algorithm
  4715. * @steps: number of ECC steps per page
  4716. * @size: data bytes per ECC step
  4717. * @bytes: ECC bytes per step
  4718. @@ -451,12 +472,18 @@
  4719. * @total: total number of ECC bytes per page
  4720. * @prepad: padding information for syndrome based ECC generators
  4721. * @postpad: padding information for syndrome based ECC generators
  4722. - * @layout: ECC layout control struct pointer
  4723. + * @options: ECC specific options (see NAND_ECC_XXX flags defined above)
  4724. * @priv: pointer to private ECC control data
  4725. * @hwctl: function to control hardware ECC generator. Must only
  4726. * be provided if an hardware ECC is available
  4727. * @calculate: function for ECC calculation or readback from ECC hardware
  4728. - * @correct: function for ECC correction, matching to ECC generator (sw/hw)
  4729. + * @correct: function for ECC correction, matching to ECC generator (sw/hw).
  4730. + * Should return a positive number representing the number of
  4731. + * corrected bitflips, -EBADMSG if the number of bitflips exceed
  4732. + * ECC strength, or any other error code if the error is not
  4733. + * directly related to correction.
  4734. + * If -EBADMSG is returned the input buffers should be left
  4735. + * untouched.
  4736. * @read_page_raw: function to read a raw page without ECC. This function
  4737. * should hide the specific layout used by the ECC
  4738. * controller and always return contiguous in-band and
  4739. @@ -487,6 +514,7 @@
  4740. */
  4741. struct nand_ecc_ctrl {
  4742. nand_ecc_modes_t mode;
  4743. + enum nand_ecc_algo algo;
  4744. int steps;
  4745. int size;
  4746. int bytes;
  4747. @@ -494,7 +522,7 @@
  4748. int strength;
  4749. int prepad;
  4750. int postpad;
  4751. - struct nand_ecclayout *layout;
  4752. + unsigned int options;
  4753. void *priv;
  4754. void (*hwctl)(struct mtd_info *mtd, int mode);
  4755. int (*calculate)(struct mtd_info *mtd, const uint8_t *dat,
  4756. @@ -540,11 +568,11 @@
  4757. /**
  4758. * struct nand_chip - NAND Private Flash Chip Data
  4759. + * @mtd: MTD device registered to the MTD framework
  4760. * @IO_ADDR_R: [BOARDSPECIFIC] address to read the 8 I/O lines of the
  4761. * flash device
  4762. * @IO_ADDR_W: [BOARDSPECIFIC] address to write the 8 I/O lines of the
  4763. * flash device.
  4764. - * @flash_node: [BOARDSPECIFIC] device node describing this instance
  4765. * @read_byte: [REPLACEABLE] read one byte from the chip
  4766. * @read_word: [REPLACEABLE] read one word from the chip
  4767. * @write_byte: [REPLACEABLE] write a single byte to the chip on the
  4768. @@ -640,18 +668,17 @@
  4769. */
  4770. struct nand_chip {
  4771. + struct mtd_info mtd;
  4772. void __iomem *IO_ADDR_R;
  4773. void __iomem *IO_ADDR_W;
  4774. - struct device_node *flash_node;
  4775. -
  4776. uint8_t (*read_byte)(struct mtd_info *mtd);
  4777. u16 (*read_word)(struct mtd_info *mtd);
  4778. void (*write_byte)(struct mtd_info *mtd, uint8_t byte);
  4779. void (*write_buf)(struct mtd_info *mtd, const uint8_t *buf, int len);
  4780. void (*read_buf)(struct mtd_info *mtd, uint8_t *buf, int len);
  4781. void (*select_chip)(struct mtd_info *mtd, int chip);
  4782. - int (*block_bad)(struct mtd_info *mtd, loff_t ofs, int getchip);
  4783. + int (*block_bad)(struct mtd_info *mtd, loff_t ofs);
  4784. int (*block_markbad)(struct mtd_info *mtd, loff_t ofs);
  4785. void (*cmd_ctrl)(struct mtd_info *mtd, int dat, unsigned int ctrl);
  4786. int (*dev_ready)(struct mtd_info *mtd);
  4787. @@ -719,6 +746,40 @@
  4788. void *priv;
  4789. };
  4790. +extern const struct mtd_ooblayout_ops nand_ooblayout_sp_ops;
  4791. +extern const struct mtd_ooblayout_ops nand_ooblayout_lp_ops;
  4792. +
  4793. +static inline void nand_set_flash_node(struct nand_chip *chip,
  4794. + struct device_node *np)
  4795. +{
  4796. + mtd_set_of_node(&chip->mtd, np);
  4797. +}
  4798. +
  4799. +static inline struct device_node *nand_get_flash_node(struct nand_chip *chip)
  4800. +{
  4801. + return mtd_get_of_node(&chip->mtd);
  4802. +}
  4803. +
  4804. +static inline struct nand_chip *mtd_to_nand(struct mtd_info *mtd)
  4805. +{
  4806. + return container_of(mtd, struct nand_chip, mtd);
  4807. +}
  4808. +
  4809. +static inline struct mtd_info *nand_to_mtd(struct nand_chip *chip)
  4810. +{
  4811. + return &chip->mtd;
  4812. +}
  4813. +
  4814. +static inline void *nand_get_controller_data(struct nand_chip *chip)
  4815. +{
  4816. + return chip->priv;
  4817. +}
  4818. +
  4819. +static inline void nand_set_controller_data(struct nand_chip *chip, void *priv)
  4820. +{
  4821. + chip->priv = priv;
  4822. +}
  4823. +
  4824. /*
  4825. * NAND Flash Manufacturer ID Codes
  4826. */
  4827. @@ -850,7 +911,6 @@
  4828. * @chip_delay: R/B delay value in us
  4829. * @options: Option flags, e.g. 16bit buswidth
  4830. * @bbt_options: BBT option flags, e.g. NAND_BBT_USE_FLASH
  4831. - * @ecclayout: ECC layout info structure
  4832. * @part_probe_types: NULL-terminated array of probe types
  4833. */
  4834. struct platform_nand_chip {
  4835. @@ -858,7 +918,6 @@
  4836. int chip_offset;
  4837. int nr_partitions;
  4838. struct mtd_partition *partitions;
  4839. - struct nand_ecclayout *ecclayout;
  4840. int chip_delay;
  4841. unsigned int options;
  4842. unsigned int bbt_options;
  4843. @@ -908,15 +967,6 @@
  4844. struct platform_nand_ctrl ctrl;
  4845. };
  4846. -/* Some helpers to access the data structures */
  4847. -static inline
  4848. -struct platform_nand_chip *get_platform_nandchip(struct mtd_info *mtd)
  4849. -{
  4850. - struct nand_chip *chip = mtd->priv;
  4851. -
  4852. - return chip->priv;
  4853. -}
  4854. -
  4855. /* return the supported features. */
  4856. static inline int onfi_feature(struct nand_chip *chip)
  4857. {
  4858. --- a/include/linux/mtd/nand_bch.h
  4859. +++ b/include/linux/mtd/nand_bch.h
  4860. @@ -32,9 +32,7 @@
  4861. /*
  4862. * Initialize BCH encoder/decoder
  4863. */
  4864. -struct nand_bch_control *
  4865. -nand_bch_init(struct mtd_info *mtd, unsigned int eccsize,
  4866. - unsigned int eccbytes, struct nand_ecclayout **ecclayout);
  4867. +struct nand_bch_control *nand_bch_init(struct mtd_info *mtd);
  4868. /*
  4869. * Release BCH encoder/decoder resources
  4870. */
  4871. @@ -55,12 +53,10 @@
  4872. nand_bch_correct_data(struct mtd_info *mtd, unsigned char *buf,
  4873. unsigned char *read_ecc, unsigned char *calc_ecc)
  4874. {
  4875. - return -1;
  4876. + return -ENOTSUPP;
  4877. }
  4878. -static inline struct nand_bch_control *
  4879. -nand_bch_init(struct mtd_info *mtd, unsigned int eccsize,
  4880. - unsigned int eccbytes, struct nand_ecclayout **ecclayout)
  4881. +static inline struct nand_bch_control *nand_bch_init(struct mtd_info *mtd)
  4882. {
  4883. return NULL;
  4884. }
  4885. --- a/include/linux/mtd/nftl.h
  4886. +++ b/include/linux/mtd/nftl.h
  4887. @@ -50,7 +50,6 @@
  4888. unsigned int nb_blocks; /* number of physical blocks */
  4889. unsigned int nb_boot_blocks; /* number of blocks used by the bios */
  4890. struct erase_info instr;
  4891. - struct nand_ecclayout oobinfo;
  4892. };
  4893. int NFTL_mount(struct NFTLrecord *s);
  4894. --- a/include/linux/mtd/onenand.h
  4895. +++ b/include/linux/mtd/onenand.h
  4896. @@ -80,7 +80,6 @@
  4897. * @page_buf: [INTERN] page main data buffer
  4898. * @oob_buf: [INTERN] page oob data buffer
  4899. * @subpagesize: [INTERN] holds the subpagesize
  4900. - * @ecclayout: [REPLACEABLE] the default ecc placement scheme
  4901. * @bbm: [REPLACEABLE] pointer to Bad Block Management
  4902. * @priv: [OPTIONAL] pointer to private chip date
  4903. */
  4904. @@ -134,7 +133,6 @@
  4905. #endif
  4906. int subpagesize;
  4907. - struct nand_ecclayout *ecclayout;
  4908. void *bbm;
  4909. --- a/include/linux/mtd/partitions.h
  4910. +++ b/include/linux/mtd/partitions.h
  4911. @@ -42,7 +42,6 @@
  4912. uint64_t size; /* partition size */
  4913. uint64_t offset; /* offset within the master MTD space */
  4914. uint32_t mask_flags; /* master MTD flags to mask out for this partition */
  4915. - struct nand_ecclayout *ecclayout; /* out of band layout for this partition (NAND only) */
  4916. };
  4917. #define MTDPART_OFS_RETAIN (-3)
  4918. @@ -56,11 +55,9 @@
  4919. /**
  4920. * struct mtd_part_parser_data - used to pass data to MTD partition parsers.
  4921. * @origin: for RedBoot, start address of MTD device
  4922. - * @of_node: for OF parsers, device node containing partitioning information
  4923. */
  4924. struct mtd_part_parser_data {
  4925. unsigned long origin;
  4926. - struct device_node *of_node;
  4927. };
  4928. @@ -78,14 +75,34 @@
  4929. struct list_head list;
  4930. struct module *owner;
  4931. const char *name;
  4932. - int (*parse_fn)(struct mtd_info *, struct mtd_partition **,
  4933. + int (*parse_fn)(struct mtd_info *, const struct mtd_partition **,
  4934. struct mtd_part_parser_data *);
  4935. + void (*cleanup)(const struct mtd_partition *pparts, int nr_parts);
  4936. enum mtd_parser_type type;
  4937. };
  4938. -extern void register_mtd_parser(struct mtd_part_parser *parser);
  4939. +/* Container for passing around a set of parsed partitions */
  4940. +struct mtd_partitions {
  4941. + const struct mtd_partition *parts;
  4942. + int nr_parts;
  4943. + const struct mtd_part_parser *parser;
  4944. +};
  4945. +
  4946. +extern int __register_mtd_parser(struct mtd_part_parser *parser,
  4947. + struct module *owner);
  4948. +#define register_mtd_parser(parser) __register_mtd_parser(parser, THIS_MODULE)
  4949. +
  4950. extern void deregister_mtd_parser(struct mtd_part_parser *parser);
  4951. +/*
  4952. + * module_mtd_part_parser() - Helper macro for MTD partition parsers that don't
  4953. + * do anything special in module init/exit. Each driver may only use this macro
  4954. + * once, and calling it replaces module_init() and module_exit().
  4955. + */
  4956. +#define module_mtd_part_parser(__mtd_part_parser) \
  4957. + module_driver(__mtd_part_parser, register_mtd_parser, \
  4958. + deregister_mtd_parser)
  4959. +
  4960. int mtd_is_partition(const struct mtd_info *mtd);
  4961. int mtd_add_partition(struct mtd_info *master, const char *name,
  4962. long long offset, long long length);
  4963. --- a/include/linux/mtd/sh_flctl.h
  4964. +++ b/include/linux/mtd/sh_flctl.h
  4965. @@ -143,11 +143,11 @@
  4966. struct dma_chan;
  4967. struct sh_flctl {
  4968. - struct mtd_info mtd;
  4969. struct nand_chip chip;
  4970. struct platform_device *pdev;
  4971. struct dev_pm_qos_request pm_qos;
  4972. void __iomem *reg;
  4973. + resource_size_t fifo;
  4974. uint8_t done_buff[2048 + 64]; /* max size 2048 + 64 */
  4975. int read_bytes;
  4976. @@ -186,7 +186,7 @@
  4977. static inline struct sh_flctl *mtd_to_flctl(struct mtd_info *mtdinfo)
  4978. {
  4979. - return container_of(mtdinfo, struct sh_flctl, mtd);
  4980. + return container_of(mtd_to_nand(mtdinfo), struct sh_flctl, chip);
  4981. }
  4982. #endif /* __SH_FLCTL_H__ */
  4983. --- a/include/linux/mtd/sharpsl.h
  4984. +++ b/include/linux/mtd/sharpsl.h
  4985. @@ -14,7 +14,7 @@
  4986. struct sharpsl_nand_platform_data {
  4987. struct nand_bbt_descr *badblock_pattern;
  4988. - struct nand_ecclayout *ecc_layout;
  4989. + const struct mtd_ooblayout_ops *ecc_layout;
  4990. struct mtd_partition *partitions;
  4991. unsigned int nr_partitions;
  4992. };
  4993. --- a/include/uapi/mtd/mtd-abi.h
  4994. +++ b/include/uapi/mtd/mtd-abi.h
  4995. @@ -228,7 +228,7 @@
  4996. * complete set of ECC information. The ioctl truncates the larger internal
  4997. * structure to retain binary compatibility with the static declaration of the
  4998. * ioctl. Note that the "MTD_MAX_..._ENTRIES" macros represent the max size of
  4999. - * the user struct, not the MAX size of the internal struct nand_ecclayout.
  5000. + * the user struct, not the MAX size of the internal OOB layout representation.
  5001. */
  5002. struct nand_ecclayout_user {
  5003. __u32 eccbytes;
  5004. --- a/fs/jffs2/wbuf.c
  5005. +++ b/fs/jffs2/wbuf.c
  5006. @@ -1183,22 +1183,20 @@
  5007. int jffs2_nand_flash_setup(struct jffs2_sb_info *c)
  5008. {
  5009. - struct nand_ecclayout *oinfo = c->mtd->ecclayout;
  5010. -
  5011. if (!c->mtd->oobsize)
  5012. return 0;
  5013. /* Cleanmarker is out-of-band, so inline size zero */
  5014. c->cleanmarker_size = 0;
  5015. - if (!oinfo || oinfo->oobavail == 0) {
  5016. + if (c->oobavail == 0) {
  5017. pr_err("inconsistent device description\n");
  5018. return -EINVAL;
  5019. }
  5020. jffs2_dbg(1, "using OOB on NAND\n");
  5021. - c->oobavail = oinfo->oobavail;
  5022. + c->oobavail = c->mtd->oobavail;
  5023. /* Initialise write buffer */
  5024. init_rwsem(&c->wbuf_sem);
  5025. --- a/include/linux/mtd/spi-nor.h
  5026. +++ b/include/linux/mtd/spi-nor.h
  5027. @@ -85,6 +85,7 @@
  5028. #define SR_BP0 BIT(2) /* Block protect 0 */
  5029. #define SR_BP1 BIT(3) /* Block protect 1 */
  5030. #define SR_BP2 BIT(4) /* Block protect 2 */
  5031. +#define SR_TB BIT(5) /* Top/Bottom protect */
  5032. #define SR_SRWD BIT(7) /* SR write protect */
  5033. #define SR_QUAD_EN_MX BIT(6) /* Macronix Quad I/O */
  5034. @@ -116,6 +117,7 @@
  5035. enum spi_nor_option_flags {
  5036. SNOR_F_USE_FSR = BIT(0),
  5037. + SNOR_F_HAS_SR_TB = BIT(1),
  5038. };
  5039. /**
  5040. @@ -123,7 +125,6 @@
  5041. * @mtd: point to a mtd_info structure
  5042. * @lock: the lock for the read/write/erase/lock/unlock operations
  5043. * @dev: point to a spi device, or a spi nor controller device.
  5044. - * @flash_node: point to a device node describing this flash instance.
  5045. * @page_size: the page size of the SPI NOR
  5046. * @addr_width: number of address bytes
  5047. * @erase_opcode: the opcode for erasing a sector
  5048. @@ -143,7 +144,8 @@
  5049. * @read: [DRIVER-SPECIFIC] read data from the SPI NOR
  5050. * @write: [DRIVER-SPECIFIC] write data to the SPI NOR
  5051. * @erase: [DRIVER-SPECIFIC] erase a sector of the SPI NOR
  5052. - * at the offset @offs
  5053. + * at the offset @offs; if not provided by the driver,
  5054. + * spi-nor will send the erase opcode via write_reg()
  5055. * @flash_lock: [FLASH-SPECIFIC] lock a region of the SPI NOR
  5056. * @flash_unlock: [FLASH-SPECIFIC] unlock a region of the SPI NOR
  5057. * @flash_is_locked: [FLASH-SPECIFIC] check if a region of the SPI NOR is
  5058. @@ -154,7 +156,6 @@
  5059. struct mtd_info mtd;
  5060. struct mutex lock;
  5061. struct device *dev;
  5062. - struct device_node *flash_node;
  5063. u32 page_size;
  5064. u8 addr_width;
  5065. u8 erase_opcode;
  5066. @@ -184,6 +185,17 @@
  5067. void *priv;
  5068. };
  5069. +static inline void spi_nor_set_flash_node(struct spi_nor *nor,
  5070. + struct device_node *np)
  5071. +{
  5072. + mtd_set_of_node(&nor->mtd, np);
  5073. +}
  5074. +
  5075. +static inline struct device_node *spi_nor_get_flash_node(struct spi_nor *nor)
  5076. +{
  5077. + return mtd_get_of_node(&nor->mtd);
  5078. +}
  5079. +
  5080. /**
  5081. * spi_nor_scan() - scan the SPI NOR
  5082. * @nor: the spi_nor structure