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

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