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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281
  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. @@ -469,38 +469,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. @@ -819,16 +892,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. @@ -917,14 +986,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. @@ -710,7 +735,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. @@ -837,7 +862,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. @@ -957,7 +982,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. @@ -992,7 +1017,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. @@ -1055,7 +1080,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. @@ -1314,13 +1339,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. @@ -1328,8 +1352,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. @@ -1361,14 +1387,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. @@ -1396,12 +1422,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. @@ -1410,20 +1437,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. @@ -1431,6 +1461,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. @@ -1454,13 +1494,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. @@ -1470,8 +1509,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. @@ -1480,6 +1521,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. @@ -1507,12 +1557,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. @@ -1521,8 +1570,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. @@ -1532,6 +1583,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. @@ -1559,6 +1619,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. @@ -1578,19 +1639,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. @@ -1603,14 +1674,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. @@ -1618,31 +1692,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. @@ -1660,7 +1715,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. @@ -1685,12 +1740,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. @@ -1777,7 +1831,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. @@ -2030,7 +2084,7 @@ static int nand_do_read_oob(struct mtd_i
  2167. {
  2168. unsigned int max_bitflips = 0;
  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. @@ -2042,10 +2096,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. @@ -2079,7 +2130,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. @@ -2240,19 +2291,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. @@ -2269,12 +2321,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. @@ -2282,8 +2333,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. @@ -2311,11 +2364,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. @@ -2343,8 +2395,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. @@ -2480,7 +2534,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. @@ -2495,31 +2550,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. @@ -2540,12 +2576,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. @@ -2670,7 +2705,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. int chipnr = (int)(to >> chip->chip_shift);
  2326. struct mtd_oob_ops ops;
  2327. int ret;
  2328. @@ -2733,15 +2768,12 @@ static int nand_do_write_oob(struct mtd_
  2329. struct mtd_oob_ops *ops)
  2330. {
  2331. int chipnr, page, status, len;
  2332. - struct nand_chip *chip = mtd->priv;
  2333. + struct nand_chip *chip = mtd_to_nand(mtd);
  2334. pr_debug("%s: to = 0x%08x, len = %i\n",
  2335. __func__, (unsigned int)to, (int)ops->ooblen);
  2336. - if (ops->mode == MTD_OPS_AUTO_OOB)
  2337. - len = chip->ecc.layout->oobavail;
  2338. - else
  2339. - len = mtd->oobsize;
  2340. + len = mtd_oobavail(mtd, ops);
  2341. /* Do not allow write past end of page */
  2342. if ((ops->ooboffs + ops->ooblen) > len) {
  2343. @@ -2858,7 +2890,7 @@ out:
  2344. */
  2345. static int single_erase(struct mtd_info *mtd, int page)
  2346. {
  2347. - struct nand_chip *chip = mtd->priv;
  2348. + struct nand_chip *chip = mtd_to_nand(mtd);
  2349. /* Send commands to erase a block */
  2350. chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
  2351. chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
  2352. @@ -2890,7 +2922,7 @@ int nand_erase_nand(struct mtd_info *mtd
  2353. int allowbbt)
  2354. {
  2355. int page, status, pages_per_block, ret, chipnr;
  2356. - struct nand_chip *chip = mtd->priv;
  2357. + struct nand_chip *chip = mtd_to_nand(mtd);
  2358. loff_t len;
  2359. pr_debug("%s: start = 0x%012llx, len = %llu\n",
  2360. @@ -2929,7 +2961,7 @@ int nand_erase_nand(struct mtd_info *mtd
  2361. while (len) {
  2362. /* Check if we have a bad block, we do not erase bad blocks! */
  2363. if (nand_block_checkbad(mtd, ((loff_t) page) <<
  2364. - chip->page_shift, 0, allowbbt)) {
  2365. + chip->page_shift, allowbbt)) {
  2366. pr_warn("%s: attempt to erase a bad block at page 0x%08x\n",
  2367. __func__, page);
  2368. instr->state = MTD_ERASE_FAILED;
  2369. @@ -3016,7 +3048,20 @@ static void nand_sync(struct mtd_info *m
  2370. */
  2371. static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
  2372. {
  2373. - return nand_block_checkbad(mtd, offs, 1, 0);
  2374. + struct nand_chip *chip = mtd_to_nand(mtd);
  2375. + int chipnr = (int)(offs >> chip->chip_shift);
  2376. + int ret;
  2377. +
  2378. + /* Select the NAND device */
  2379. + nand_get_device(mtd, FL_READING);
  2380. + chip->select_chip(mtd, chipnr);
  2381. +
  2382. + ret = nand_block_checkbad(mtd, offs, 0);
  2383. +
  2384. + chip->select_chip(mtd, -1);
  2385. + nand_release_device(mtd);
  2386. +
  2387. + return ret;
  2388. }
  2389. /**
  2390. @@ -3105,7 +3150,7 @@ static int nand_suspend(struct mtd_info
  2391. */
  2392. static void nand_resume(struct mtd_info *mtd)
  2393. {
  2394. - struct nand_chip *chip = mtd->priv;
  2395. + struct nand_chip *chip = mtd_to_nand(mtd);
  2396. if (chip->state == FL_PM_SUSPENDED)
  2397. nand_release_device(mtd);
  2398. @@ -3277,7 +3322,7 @@ ext_out:
  2399. static int nand_setup_read_retry_micron(struct mtd_info *mtd, int retry_mode)
  2400. {
  2401. - struct nand_chip *chip = mtd->priv;
  2402. + struct nand_chip *chip = mtd_to_nand(mtd);
  2403. uint8_t feature[ONFI_SUBFEATURE_PARAM_LEN] = {retry_mode};
  2404. return chip->onfi_set_features(mtd, chip, ONFI_FEATURE_ADDR_READ_RETRY,
  2405. @@ -3948,10 +3993,13 @@ ident_done:
  2406. return type;
  2407. }
  2408. -static int nand_dt_init(struct mtd_info *mtd, struct nand_chip *chip,
  2409. - struct device_node *dn)
  2410. +static int nand_dt_init(struct nand_chip *chip)
  2411. {
  2412. - int ecc_mode, ecc_strength, ecc_step;
  2413. + struct device_node *dn = nand_get_flash_node(chip);
  2414. + int ecc_mode, ecc_algo, ecc_strength, ecc_step;
  2415. +
  2416. + if (!dn)
  2417. + return 0;
  2418. if (of_get_nand_bus_width(dn) == 16)
  2419. chip->options |= NAND_BUSWIDTH_16;
  2420. @@ -3960,6 +4008,7 @@ static int nand_dt_init(struct mtd_info
  2421. chip->bbt_options |= NAND_BBT_USE_FLASH;
  2422. ecc_mode = of_get_nand_ecc_mode(dn);
  2423. + ecc_algo = of_get_nand_ecc_algo(dn);
  2424. ecc_strength = of_get_nand_ecc_strength(dn);
  2425. ecc_step = of_get_nand_ecc_step_size(dn);
  2426. @@ -3972,6 +4021,9 @@ static int nand_dt_init(struct mtd_info
  2427. if (ecc_mode >= 0)
  2428. chip->ecc.mode = ecc_mode;
  2429. + if (ecc_algo >= 0)
  2430. + chip->ecc.algo = ecc_algo;
  2431. +
  2432. if (ecc_strength >= 0)
  2433. chip->ecc.strength = ecc_strength;
  2434. @@ -3995,15 +4047,16 @@ int nand_scan_ident(struct mtd_info *mtd
  2435. struct nand_flash_dev *table)
  2436. {
  2437. int i, nand_maf_id, nand_dev_id;
  2438. - struct nand_chip *chip = mtd->priv;
  2439. + struct nand_chip *chip = mtd_to_nand(mtd);
  2440. struct nand_flash_dev *type;
  2441. int ret;
  2442. - if (chip->flash_node) {
  2443. - ret = nand_dt_init(mtd, chip, chip->flash_node);
  2444. - if (ret)
  2445. - return ret;
  2446. - }
  2447. + ret = nand_dt_init(chip);
  2448. + if (ret)
  2449. + return ret;
  2450. +
  2451. + if (!mtd->name && mtd->dev.parent)
  2452. + mtd->name = dev_name(mtd->dev.parent);
  2453. if (!mtd->name && mtd->dev.parent)
  2454. mtd->name = dev_name(mtd->dev.parent);
  2455. @@ -4066,7 +4119,7 @@ EXPORT_SYMBOL(nand_scan_ident);
  2456. */
  2457. static bool nand_ecc_strength_good(struct mtd_info *mtd)
  2458. {
  2459. - struct nand_chip *chip = mtd->priv;
  2460. + struct nand_chip *chip = mtd_to_nand(mtd);
  2461. struct nand_ecc_ctrl *ecc = &chip->ecc;
  2462. int corr, ds_corr;
  2463. @@ -4094,10 +4147,10 @@ static bool nand_ecc_strength_good(struc
  2464. */
  2465. int nand_scan_tail(struct mtd_info *mtd)
  2466. {
  2467. - int i;
  2468. - struct nand_chip *chip = mtd->priv;
  2469. + struct nand_chip *chip = mtd_to_nand(mtd);
  2470. struct nand_ecc_ctrl *ecc = &chip->ecc;
  2471. struct nand_buffers *nbuf;
  2472. + int ret;
  2473. /* New bad blocks should be marked in OOB, flash-based BBT, or both */
  2474. BUG_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) &&
  2475. @@ -4124,19 +4177,15 @@ int nand_scan_tail(struct mtd_info *mtd)
  2476. /*
  2477. * If no default placement scheme is given, select an appropriate one.
  2478. */
  2479. - if (!ecc->layout && (ecc->mode != NAND_ECC_SOFT_BCH)) {
  2480. + if (!mtd->ooblayout && (ecc->mode != NAND_ECC_SOFT_BCH)) {
  2481. switch (mtd->oobsize) {
  2482. case 8:
  2483. - ecc->layout = &nand_oob_8;
  2484. - break;
  2485. case 16:
  2486. - ecc->layout = &nand_oob_16;
  2487. + mtd_set_ooblayout(mtd, &nand_ooblayout_sp_ops);
  2488. break;
  2489. case 64:
  2490. - ecc->layout = &nand_oob_64;
  2491. - break;
  2492. case 128:
  2493. - ecc->layout = &nand_oob_128;
  2494. + mtd_set_ooblayout(mtd, &nand_ooblayout_lp_ops);
  2495. break;
  2496. default:
  2497. pr_warn("No oob scheme defined for oobsize %d\n",
  2498. @@ -4179,7 +4228,7 @@ int nand_scan_tail(struct mtd_info *mtd)
  2499. ecc->write_oob = nand_write_oob_std;
  2500. if (!ecc->read_subpage)
  2501. ecc->read_subpage = nand_read_subpage;
  2502. - if (!ecc->write_subpage)
  2503. + if (!ecc->write_subpage && ecc->hwctl && ecc->calculate)
  2504. ecc->write_subpage = nand_write_subpage_hwecc;
  2505. case NAND_ECC_HW_SYNDROME:
  2506. @@ -4257,10 +4306,8 @@ int nand_scan_tail(struct mtd_info *mtd)
  2507. }
  2508. /* See nand_bch_init() for details. */
  2509. - ecc->bytes = DIV_ROUND_UP(
  2510. - ecc->strength * fls(8 * ecc->size), 8);
  2511. - ecc->priv = nand_bch_init(mtd, ecc->size, ecc->bytes,
  2512. - &ecc->layout);
  2513. + ecc->bytes = 0;
  2514. + ecc->priv = nand_bch_init(mtd);
  2515. if (!ecc->priv) {
  2516. pr_warn("BCH ECC initialization failed!\n");
  2517. BUG();
  2518. @@ -4291,20 +4338,9 @@ int nand_scan_tail(struct mtd_info *mtd)
  2519. if (!ecc->write_oob_raw)
  2520. ecc->write_oob_raw = ecc->write_oob;
  2521. - /*
  2522. - * The number of bytes available for a client to place data into
  2523. - * the out of band area.
  2524. - */
  2525. - ecc->layout->oobavail = 0;
  2526. - for (i = 0; ecc->layout->oobfree[i].length
  2527. - && i < ARRAY_SIZE(ecc->layout->oobfree); i++)
  2528. - ecc->layout->oobavail += ecc->layout->oobfree[i].length;
  2529. - mtd->oobavail = ecc->layout->oobavail;
  2530. -
  2531. - /* ECC sanity check: warn if it's too weak */
  2532. - if (!nand_ecc_strength_good(mtd))
  2533. - pr_warn("WARNING: %s: the ECC used on your system is too weak compared to the one required by the NAND chip\n",
  2534. - mtd->name);
  2535. + /* propagate ecc info to mtd_info */
  2536. + mtd->ecc_strength = ecc->strength;
  2537. + mtd->ecc_step_size = ecc->size;
  2538. /*
  2539. * Set the number of read / write steps for one page depending on ECC
  2540. @@ -4317,6 +4353,21 @@ int nand_scan_tail(struct mtd_info *mtd)
  2541. }
  2542. ecc->total = ecc->steps * ecc->bytes;
  2543. + /*
  2544. + * The number of bytes available for a client to place data into
  2545. + * the out of band area.
  2546. + */
  2547. + ret = mtd_ooblayout_count_freebytes(mtd);
  2548. + if (ret < 0)
  2549. + ret = 0;
  2550. +
  2551. + mtd->oobavail = ret;
  2552. +
  2553. + /* ECC sanity check: warn if it's too weak */
  2554. + if (!nand_ecc_strength_good(mtd))
  2555. + pr_warn("WARNING: %s: the ECC used on your system is too weak compared to the one required by the NAND chip\n",
  2556. + mtd->name);
  2557. +
  2558. /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */
  2559. if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && nand_is_slc(chip)) {
  2560. switch (ecc->steps) {
  2561. @@ -4373,10 +4424,6 @@ int nand_scan_tail(struct mtd_info *mtd)
  2562. mtd->_block_markbad = nand_block_markbad;
  2563. mtd->writebufsize = mtd->writesize;
  2564. - /* propagate ecc info to mtd_info */
  2565. - mtd->ecclayout = ecc->layout;
  2566. - mtd->ecc_strength = ecc->strength;
  2567. - mtd->ecc_step_size = ecc->size;
  2568. /*
  2569. * Initialize bitflip_threshold to its default prior scan_bbt() call.
  2570. * scan_bbt() might invoke mtd_read(), thus bitflip_threshold must be
  2571. @@ -4432,7 +4479,7 @@ EXPORT_SYMBOL(nand_scan);
  2572. */
  2573. void nand_release(struct mtd_info *mtd)
  2574. {
  2575. - struct nand_chip *chip = mtd->priv;
  2576. + struct nand_chip *chip = mtd_to_nand(mtd);
  2577. if (chip->ecc.mode == NAND_ECC_SOFT_BCH)
  2578. nand_bch_free((struct nand_bch_control *)chip->ecc.priv);
  2579. --- a/drivers/mtd/nand/nand_bbt.c
  2580. +++ b/drivers/mtd/nand/nand_bbt.c
  2581. @@ -172,7 +172,7 @@ static int read_bbt(struct mtd_info *mtd
  2582. struct nand_bbt_descr *td, int offs)
  2583. {
  2584. int res, ret = 0, i, j, act = 0;
  2585. - struct nand_chip *this = mtd->priv;
  2586. + struct nand_chip *this = mtd_to_nand(mtd);
  2587. size_t retlen, len, totlen;
  2588. loff_t from;
  2589. int bits = td->options & NAND_BBT_NRBITS_MSK;
  2590. @@ -263,7 +263,7 @@ static int read_bbt(struct mtd_info *mtd
  2591. */
  2592. static int read_abs_bbt(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *td, int chip)
  2593. {
  2594. - struct nand_chip *this = mtd->priv;
  2595. + struct nand_chip *this = mtd_to_nand(mtd);
  2596. int res = 0, i;
  2597. if (td->options & NAND_BBT_PERCHIP) {
  2598. @@ -388,7 +388,7 @@ static u32 bbt_get_ver_offs(struct mtd_i
  2599. static void read_abs_bbts(struct mtd_info *mtd, uint8_t *buf,
  2600. struct nand_bbt_descr *td, struct nand_bbt_descr *md)
  2601. {
  2602. - struct nand_chip *this = mtd->priv;
  2603. + struct nand_chip *this = mtd_to_nand(mtd);
  2604. /* Read the primary version, if available */
  2605. if (td->options & NAND_BBT_VERSION) {
  2606. @@ -454,7 +454,7 @@ static int scan_block_fast(struct mtd_in
  2607. static int create_bbt(struct mtd_info *mtd, uint8_t *buf,
  2608. struct nand_bbt_descr *bd, int chip)
  2609. {
  2610. - struct nand_chip *this = mtd->priv;
  2611. + struct nand_chip *this = mtd_to_nand(mtd);
  2612. int i, numblocks, numpages;
  2613. int startblock;
  2614. loff_t from;
  2615. @@ -523,7 +523,7 @@ static int create_bbt(struct mtd_info *m
  2616. */
  2617. static int search_bbt(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *td)
  2618. {
  2619. - struct nand_chip *this = mtd->priv;
  2620. + struct nand_chip *this = mtd_to_nand(mtd);
  2621. int i, chips;
  2622. int startblock, block, dir;
  2623. int scanlen = mtd->writesize + mtd->oobsize;
  2624. @@ -618,7 +618,7 @@ static int write_bbt(struct mtd_info *mt
  2625. struct nand_bbt_descr *td, struct nand_bbt_descr *md,
  2626. int chipsel)
  2627. {
  2628. - struct nand_chip *this = mtd->priv;
  2629. + struct nand_chip *this = mtd_to_nand(mtd);
  2630. struct erase_info einfo;
  2631. int i, res, chip = 0;
  2632. int bits, startblock, dir, page, offs, numblocks, sft, sftmsk;
  2633. @@ -819,7 +819,7 @@ static int write_bbt(struct mtd_info *mt
  2634. */
  2635. static inline int nand_memory_bbt(struct mtd_info *mtd, struct nand_bbt_descr *bd)
  2636. {
  2637. - struct nand_chip *this = mtd->priv;
  2638. + struct nand_chip *this = mtd_to_nand(mtd);
  2639. return create_bbt(mtd, this->buffers->databuf, bd, -1);
  2640. }
  2641. @@ -838,7 +838,7 @@ static inline int nand_memory_bbt(struct
  2642. static int check_create(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *bd)
  2643. {
  2644. int i, chips, writeops, create, chipsel, res, res2;
  2645. - struct nand_chip *this = mtd->priv;
  2646. + struct nand_chip *this = mtd_to_nand(mtd);
  2647. struct nand_bbt_descr *td = this->bbt_td;
  2648. struct nand_bbt_descr *md = this->bbt_md;
  2649. struct nand_bbt_descr *rd, *rd2;
  2650. @@ -962,7 +962,7 @@ static int check_create(struct mtd_info
  2651. */
  2652. static void mark_bbt_region(struct mtd_info *mtd, struct nand_bbt_descr *td)
  2653. {
  2654. - struct nand_chip *this = mtd->priv;
  2655. + struct nand_chip *this = mtd_to_nand(mtd);
  2656. int i, j, chips, block, nrblocks, update;
  2657. uint8_t oldval;
  2658. @@ -1022,7 +1022,7 @@ static void mark_bbt_region(struct mtd_i
  2659. */
  2660. static void verify_bbt_descr(struct mtd_info *mtd, struct nand_bbt_descr *bd)
  2661. {
  2662. - struct nand_chip *this = mtd->priv;
  2663. + struct nand_chip *this = mtd_to_nand(mtd);
  2664. u32 pattern_len;
  2665. u32 bits;
  2666. u32 table_size;
  2667. @@ -1074,7 +1074,7 @@ static void verify_bbt_descr(struct mtd_
  2668. */
  2669. static int nand_scan_bbt(struct mtd_info *mtd, struct nand_bbt_descr *bd)
  2670. {
  2671. - struct nand_chip *this = mtd->priv;
  2672. + struct nand_chip *this = mtd_to_nand(mtd);
  2673. int len, res;
  2674. uint8_t *buf;
  2675. struct nand_bbt_descr *td = this->bbt_td;
  2676. @@ -1147,7 +1147,7 @@ err:
  2677. */
  2678. static int nand_update_bbt(struct mtd_info *mtd, loff_t offs)
  2679. {
  2680. - struct nand_chip *this = mtd->priv;
  2681. + struct nand_chip *this = mtd_to_nand(mtd);
  2682. int len, res = 0;
  2683. int chip, chipsel;
  2684. uint8_t *buf;
  2685. @@ -1281,7 +1281,7 @@ static int nand_create_badblock_pattern(
  2686. */
  2687. int nand_default_bbt(struct mtd_info *mtd)
  2688. {
  2689. - struct nand_chip *this = mtd->priv;
  2690. + struct nand_chip *this = mtd_to_nand(mtd);
  2691. int ret;
  2692. /* Is a flash based bad block table requested? */
  2693. @@ -1317,7 +1317,7 @@ int nand_default_bbt(struct mtd_info *mt
  2694. */
  2695. int nand_isreserved_bbt(struct mtd_info *mtd, loff_t offs)
  2696. {
  2697. - struct nand_chip *this = mtd->priv;
  2698. + struct nand_chip *this = mtd_to_nand(mtd);
  2699. int block;
  2700. block = (int)(offs >> this->bbt_erase_shift);
  2701. @@ -1332,7 +1332,7 @@ int nand_isreserved_bbt(struct mtd_info
  2702. */
  2703. int nand_isbad_bbt(struct mtd_info *mtd, loff_t offs, int allowbbt)
  2704. {
  2705. - struct nand_chip *this = mtd->priv;
  2706. + struct nand_chip *this = mtd_to_nand(mtd);
  2707. int block, res;
  2708. block = (int)(offs >> this->bbt_erase_shift);
  2709. @@ -1359,7 +1359,7 @@ int nand_isbad_bbt(struct mtd_info *mtd,
  2710. */
  2711. int nand_markbad_bbt(struct mtd_info *mtd, loff_t offs)
  2712. {
  2713. - struct nand_chip *this = mtd->priv;
  2714. + struct nand_chip *this = mtd_to_nand(mtd);
  2715. int block, ret = 0;
  2716. block = (int)(offs >> this->bbt_erase_shift);
  2717. @@ -1373,5 +1373,3 @@ int nand_markbad_bbt(struct mtd_info *mt
  2718. return ret;
  2719. }
  2720. -
  2721. -EXPORT_SYMBOL(nand_scan_bbt);
  2722. --- a/drivers/mtd/nand/nand_bch.c
  2723. +++ b/drivers/mtd/nand/nand_bch.c
  2724. @@ -32,13 +32,11 @@
  2725. /**
  2726. * struct nand_bch_control - private NAND BCH control structure
  2727. * @bch: BCH control structure
  2728. - * @ecclayout: private ecc layout for this BCH configuration
  2729. * @errloc: error location array
  2730. * @eccmask: XOR ecc mask, allows erased pages to be decoded as valid
  2731. */
  2732. struct nand_bch_control {
  2733. struct bch_control *bch;
  2734. - struct nand_ecclayout ecclayout;
  2735. unsigned int *errloc;
  2736. unsigned char *eccmask;
  2737. };
  2738. @@ -52,7 +50,7 @@ struct nand_bch_control {
  2739. int nand_bch_calculate_ecc(struct mtd_info *mtd, const unsigned char *buf,
  2740. unsigned char *code)
  2741. {
  2742. - const struct nand_chip *chip = mtd->priv;
  2743. + const struct nand_chip *chip = mtd_to_nand(mtd);
  2744. struct nand_bch_control *nbc = chip->ecc.priv;
  2745. unsigned int i;
  2746. @@ -79,7 +77,7 @@ EXPORT_SYMBOL(nand_bch_calculate_ecc);
  2747. int nand_bch_correct_data(struct mtd_info *mtd, unsigned char *buf,
  2748. unsigned char *read_ecc, unsigned char *calc_ecc)
  2749. {
  2750. - const struct nand_chip *chip = mtd->priv;
  2751. + const struct nand_chip *chip = mtd_to_nand(mtd);
  2752. struct nand_bch_control *nbc = chip->ecc.priv;
  2753. unsigned int *errloc = nbc->errloc;
  2754. int i, count;
  2755. @@ -98,7 +96,7 @@ int nand_bch_correct_data(struct mtd_inf
  2756. }
  2757. } else if (count < 0) {
  2758. printk(KERN_ERR "ecc unrecoverable error\n");
  2759. - count = -1;
  2760. + count = -EBADMSG;
  2761. }
  2762. return count;
  2763. }
  2764. @@ -107,9 +105,6 @@ EXPORT_SYMBOL(nand_bch_correct_data);
  2765. /**
  2766. * nand_bch_init - [NAND Interface] Initialize NAND BCH error correction
  2767. * @mtd: MTD block structure
  2768. - * @eccsize: ecc block size in bytes
  2769. - * @eccbytes: ecc length in bytes
  2770. - * @ecclayout: output default layout
  2771. *
  2772. * Returns:
  2773. * a pointer to a new NAND BCH control structure, or NULL upon failure
  2774. @@ -123,14 +118,20 @@ EXPORT_SYMBOL(nand_bch_correct_data);
  2775. * @eccsize = 512 (thus, m=13 is the smallest integer such that 2^m-1 > 512*8)
  2776. * @eccbytes = 7 (7 bytes are required to store m*t = 13*4 = 52 bits)
  2777. */
  2778. -struct nand_bch_control *
  2779. -nand_bch_init(struct mtd_info *mtd, unsigned int eccsize, unsigned int eccbytes,
  2780. - struct nand_ecclayout **ecclayout)
  2781. +struct nand_bch_control *nand_bch_init(struct mtd_info *mtd)
  2782. {
  2783. + struct nand_chip *nand = mtd_to_nand(mtd);
  2784. unsigned int m, t, eccsteps, i;
  2785. - struct nand_ecclayout *layout;
  2786. struct nand_bch_control *nbc = NULL;
  2787. unsigned char *erased_page;
  2788. + unsigned int eccsize = nand->ecc.size;
  2789. + unsigned int eccbytes = nand->ecc.bytes;
  2790. + unsigned int eccstrength = nand->ecc.strength;
  2791. +
  2792. + if (!eccbytes && eccstrength) {
  2793. + eccbytes = DIV_ROUND_UP(eccstrength * fls(8 * eccsize), 8);
  2794. + nand->ecc.bytes = eccbytes;
  2795. + }
  2796. if (!eccsize || !eccbytes) {
  2797. printk(KERN_WARNING "ecc parameters not supplied\n");
  2798. @@ -158,7 +159,7 @@ nand_bch_init(struct mtd_info *mtd, unsi
  2799. eccsteps = mtd->writesize/eccsize;
  2800. /* if no ecc placement scheme was provided, build one */
  2801. - if (!*ecclayout) {
  2802. + if (!mtd->ooblayout) {
  2803. /* handle large page devices only */
  2804. if (mtd->oobsize < 64) {
  2805. @@ -167,24 +168,7 @@ nand_bch_init(struct mtd_info *mtd, unsi
  2806. goto fail;
  2807. }
  2808. - layout = &nbc->ecclayout;
  2809. - layout->eccbytes = eccsteps*eccbytes;
  2810. -
  2811. - /* reserve 2 bytes for bad block marker */
  2812. - if (layout->eccbytes+2 > mtd->oobsize) {
  2813. - printk(KERN_WARNING "no suitable oob scheme available "
  2814. - "for oobsize %d eccbytes %u\n", mtd->oobsize,
  2815. - eccbytes);
  2816. - goto fail;
  2817. - }
  2818. - /* put ecc bytes at oob tail */
  2819. - for (i = 0; i < layout->eccbytes; i++)
  2820. - layout->eccpos[i] = mtd->oobsize-layout->eccbytes+i;
  2821. -
  2822. - layout->oobfree[0].offset = 2;
  2823. - layout->oobfree[0].length = mtd->oobsize-2-layout->eccbytes;
  2824. -
  2825. - *ecclayout = layout;
  2826. + mtd_set_ooblayout(mtd, &nand_ooblayout_lp_ops);
  2827. }
  2828. /* sanity checks */
  2829. @@ -192,7 +176,8 @@ nand_bch_init(struct mtd_info *mtd, unsi
  2830. printk(KERN_WARNING "eccsize %u is too large\n", eccsize);
  2831. goto fail;
  2832. }
  2833. - if ((*ecclayout)->eccbytes != (eccsteps*eccbytes)) {
  2834. +
  2835. + if (mtd_ooblayout_count_eccbytes(mtd) != (eccsteps*eccbytes)) {
  2836. printk(KERN_WARNING "invalid ecc layout\n");
  2837. goto fail;
  2838. }
  2839. @@ -216,6 +201,9 @@ nand_bch_init(struct mtd_info *mtd, unsi
  2840. for (i = 0; i < eccbytes; i++)
  2841. nbc->eccmask[i] ^= 0xff;
  2842. + if (!eccstrength)
  2843. + nand->ecc.strength = (eccbytes * 8) / fls(8 * eccsize);
  2844. +
  2845. return nbc;
  2846. fail:
  2847. nand_bch_free(nbc);
  2848. --- a/drivers/mtd/nand/nand_ecc.c
  2849. +++ b/drivers/mtd/nand/nand_ecc.c
  2850. @@ -424,7 +424,7 @@ int nand_calculate_ecc(struct mtd_info *
  2851. unsigned char *code)
  2852. {
  2853. __nand_calculate_ecc(buf,
  2854. - ((struct nand_chip *)mtd->priv)->ecc.size, code);
  2855. + mtd_to_nand(mtd)->ecc.size, code);
  2856. return 0;
  2857. }
  2858. @@ -524,7 +524,7 @@ int nand_correct_data(struct mtd_info *m
  2859. unsigned char *read_ecc, unsigned char *calc_ecc)
  2860. {
  2861. return __nand_correct_data(buf, read_ecc, calc_ecc,
  2862. - ((struct nand_chip *)mtd->priv)->ecc.size);
  2863. + mtd_to_nand(mtd)->ecc.size);
  2864. }
  2865. EXPORT_SYMBOL(nand_correct_data);
  2866. --- a/drivers/mtd/nand/nand_ids.c
  2867. +++ b/drivers/mtd/nand/nand_ids.c
  2868. @@ -50,8 +50,8 @@ struct nand_flash_dev nand_flash_ids[] =
  2869. SZ_16K, SZ_8K, SZ_4M, 0, 6, 1280, NAND_ECC_INFO(40, SZ_1K) },
  2870. {"H27UCG8T2ATR-BC 64G 3.3V 8-bit",
  2871. { .id = {0xad, 0xde, 0x94, 0xda, 0x74, 0xc4} },
  2872. - SZ_8K, SZ_8K, SZ_2M, 0, 6, 640, NAND_ECC_INFO(40, SZ_1K),
  2873. - 4 },
  2874. + SZ_8K, SZ_8K, SZ_2M, NAND_NEED_SCRAMBLING, 6, 640,
  2875. + NAND_ECC_INFO(40, SZ_1K), 4 },
  2876. LEGACY_ID_NAND("NAND 4MiB 5V 8-bit", 0x6B, 4, SZ_8K, SP_OPTIONS),
  2877. LEGACY_ID_NAND("NAND 4MiB 3,3V 8-bit", 0xE3, 4, SZ_8K, SP_OPTIONS),
  2878. --- a/drivers/mtd/nand/nandsim.c
  2879. +++ b/drivers/mtd/nand/nandsim.c
  2880. @@ -666,8 +666,8 @@ static char *get_partition_name(int i)
  2881. */
  2882. static int init_nandsim(struct mtd_info *mtd)
  2883. {
  2884. - struct nand_chip *chip = mtd->priv;
  2885. - struct nandsim *ns = chip->priv;
  2886. + struct nand_chip *chip = mtd_to_nand(mtd);
  2887. + struct nandsim *ns = nand_get_controller_data(chip);
  2888. int i, ret = 0;
  2889. uint64_t remains;
  2890. uint64_t next_offset;
  2891. @@ -1908,7 +1908,8 @@ static void switch_state(struct nandsim
  2892. static u_char ns_nand_read_byte(struct mtd_info *mtd)
  2893. {
  2894. - struct nandsim *ns = ((struct nand_chip *)mtd->priv)->priv;
  2895. + struct nand_chip *chip = mtd_to_nand(mtd);
  2896. + struct nandsim *ns = nand_get_controller_data(chip);
  2897. u_char outb = 0x00;
  2898. /* Sanity and correctness checks */
  2899. @@ -1969,7 +1970,8 @@ static u_char ns_nand_read_byte(struct m
  2900. static void ns_nand_write_byte(struct mtd_info *mtd, u_char byte)
  2901. {
  2902. - struct nandsim *ns = ((struct nand_chip *)mtd->priv)->priv;
  2903. + struct nand_chip *chip = mtd_to_nand(mtd);
  2904. + struct nandsim *ns = nand_get_controller_data(chip);
  2905. /* Sanity and correctness checks */
  2906. if (!ns->lines.ce) {
  2907. @@ -2123,7 +2125,8 @@ static void ns_nand_write_byte(struct mt
  2908. static void ns_hwcontrol(struct mtd_info *mtd, int cmd, unsigned int bitmask)
  2909. {
  2910. - struct nandsim *ns = ((struct nand_chip *)mtd->priv)->priv;
  2911. + struct nand_chip *chip = mtd_to_nand(mtd);
  2912. + struct nandsim *ns = nand_get_controller_data(chip);
  2913. ns->lines.cle = bitmask & NAND_CLE ? 1 : 0;
  2914. ns->lines.ale = bitmask & NAND_ALE ? 1 : 0;
  2915. @@ -2141,7 +2144,7 @@ static int ns_device_ready(struct mtd_in
  2916. static uint16_t ns_nand_read_word(struct mtd_info *mtd)
  2917. {
  2918. - struct nand_chip *chip = (struct nand_chip *)mtd->priv;
  2919. + struct nand_chip *chip = mtd_to_nand(mtd);
  2920. NS_DBG("read_word\n");
  2921. @@ -2150,7 +2153,8 @@ static uint16_t ns_nand_read_word(struct
  2922. static void ns_nand_write_buf(struct mtd_info *mtd, const u_char *buf, int len)
  2923. {
  2924. - struct nandsim *ns = ((struct nand_chip *)mtd->priv)->priv;
  2925. + struct nand_chip *chip = mtd_to_nand(mtd);
  2926. + struct nandsim *ns = nand_get_controller_data(chip);
  2927. /* Check that chip is expecting data input */
  2928. if (!(ns->state & STATE_DATAIN_MASK)) {
  2929. @@ -2177,7 +2181,8 @@ static void ns_nand_write_buf(struct mtd
  2930. static void ns_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len)
  2931. {
  2932. - struct nandsim *ns = ((struct nand_chip *)mtd->priv)->priv;
  2933. + struct nand_chip *chip = mtd_to_nand(mtd);
  2934. + struct nandsim *ns = nand_get_controller_data(chip);
  2935. /* Sanity and correctness checks */
  2936. if (!ns->lines.ce) {
  2937. @@ -2198,7 +2203,7 @@ static void ns_nand_read_buf(struct mtd_
  2938. int i;
  2939. for (i = 0; i < len; i++)
  2940. - buf[i] = ((struct nand_chip *)mtd->priv)->read_byte(mtd);
  2941. + buf[i] = mtd_to_nand(mtd)->read_byte(mtd);
  2942. return;
  2943. }
  2944. @@ -2236,16 +2241,15 @@ static int __init ns_init_module(void)
  2945. }
  2946. /* Allocate and initialize mtd_info, nand_chip and nandsim structures */
  2947. - nsmtd = kzalloc(sizeof(struct mtd_info) + sizeof(struct nand_chip)
  2948. - + sizeof(struct nandsim), GFP_KERNEL);
  2949. - if (!nsmtd) {
  2950. + chip = kzalloc(sizeof(struct nand_chip) + sizeof(struct nandsim),
  2951. + GFP_KERNEL);
  2952. + if (!chip) {
  2953. NS_ERR("unable to allocate core structures.\n");
  2954. return -ENOMEM;
  2955. }
  2956. - chip = (struct nand_chip *)(nsmtd + 1);
  2957. - nsmtd->priv = (void *)chip;
  2958. + nsmtd = nand_to_mtd(chip);
  2959. nand = (struct nandsim *)(chip + 1);
  2960. - chip->priv = (void *)nand;
  2961. + nand_set_controller_data(chip, (void *)nand);
  2962. /*
  2963. * Register simulator's callbacks.
  2964. @@ -2257,6 +2261,7 @@ static int __init ns_init_module(void)
  2965. chip->read_buf = ns_nand_read_buf;
  2966. chip->read_word = ns_nand_read_word;
  2967. chip->ecc.mode = NAND_ECC_SOFT;
  2968. + chip->ecc.algo = NAND_ECC_HAMMING;
  2969. /* The NAND_SKIP_BBTSCAN option is necessary for 'overridesize' */
  2970. /* and 'badblocks' parameters to work */
  2971. chip->options |= NAND_SKIP_BBTSCAN;
  2972. @@ -2335,6 +2340,7 @@ static int __init ns_init_module(void)
  2973. goto error;
  2974. }
  2975. chip->ecc.mode = NAND_ECC_SOFT_BCH;
  2976. + chip->ecc.algo = NAND_ECC_BCH;
  2977. chip->ecc.size = 512;
  2978. chip->ecc.strength = bch;
  2979. chip->ecc.bytes = eccbytes;
  2980. @@ -2392,7 +2398,7 @@ err_exit:
  2981. for (i = 0;i < ARRAY_SIZE(nand->partitions); ++i)
  2982. kfree(nand->partitions[i].name);
  2983. error:
  2984. - kfree(nsmtd);
  2985. + kfree(chip);
  2986. free_lists();
  2987. return retval;
  2988. @@ -2405,7 +2411,8 @@ module_init(ns_init_module);
  2989. */
  2990. static void __exit ns_cleanup_module(void)
  2991. {
  2992. - struct nandsim *ns = ((struct nand_chip *)nsmtd->priv)->priv;
  2993. + struct nand_chip *chip = mtd_to_nand(nsmtd);
  2994. + struct nandsim *ns = nand_get_controller_data(chip);
  2995. int i;
  2996. nandsim_debugfs_remove(ns);
  2997. @@ -2413,7 +2420,7 @@ static void __exit ns_cleanup_module(voi
  2998. nand_release(nsmtd); /* Unregister driver */
  2999. for (i = 0;i < ARRAY_SIZE(ns->partitions); ++i)
  3000. kfree(ns->partitions[i].name);
  3001. - kfree(nsmtd); /* Free other structures */
  3002. + kfree(mtd_to_nand(nsmtd)); /* Free other structures */
  3003. free_lists();
  3004. }
  3005. --- a/drivers/mtd/ofpart.c
  3006. +++ b/drivers/mtd/ofpart.c
  3007. @@ -26,9 +26,10 @@ static bool node_has_compatible(struct d
  3008. }
  3009. static int parse_ofpart_partitions(struct mtd_info *master,
  3010. - struct mtd_partition **pparts,
  3011. + const struct mtd_partition **pparts,
  3012. struct mtd_part_parser_data *data)
  3013. {
  3014. + struct mtd_partition *parts;
  3015. struct device_node *mtd_node;
  3016. struct device_node *ofpart_node;
  3017. const char *partname;
  3018. @@ -37,10 +38,8 @@ static int parse_ofpart_partitions(struc
  3019. bool dedicated = true;
  3020. - if (!data)
  3021. - return 0;
  3022. -
  3023. - mtd_node = data->of_node;
  3024. + /* Pull of_node from the master device node */
  3025. + mtd_node = mtd_get_of_node(master);
  3026. if (!mtd_node)
  3027. return 0;
  3028. @@ -72,8 +71,8 @@ static int parse_ofpart_partitions(struc
  3029. if (nr_parts == 0)
  3030. return 0;
  3031. - *pparts = kzalloc(nr_parts * sizeof(**pparts), GFP_KERNEL);
  3032. - if (!*pparts)
  3033. + parts = kzalloc(nr_parts * sizeof(*parts), GFP_KERNEL);
  3034. + if (!parts)
  3035. return -ENOMEM;
  3036. i = 0;
  3037. @@ -107,19 +106,19 @@ static int parse_ofpart_partitions(struc
  3038. goto ofpart_fail;
  3039. }
  3040. - (*pparts)[i].offset = of_read_number(reg, a_cells);
  3041. - (*pparts)[i].size = of_read_number(reg + a_cells, s_cells);
  3042. + parts[i].offset = of_read_number(reg, a_cells);
  3043. + parts[i].size = of_read_number(reg + a_cells, s_cells);
  3044. partname = of_get_property(pp, "label", &len);
  3045. if (!partname)
  3046. partname = of_get_property(pp, "name", &len);
  3047. - (*pparts)[i].name = partname;
  3048. + parts[i].name = partname;
  3049. if (of_get_property(pp, "read-only", &len))
  3050. - (*pparts)[i].mask_flags |= MTD_WRITEABLE;
  3051. + parts[i].mask_flags |= MTD_WRITEABLE;
  3052. if (of_get_property(pp, "lock", &len))
  3053. - (*pparts)[i].mask_flags |= MTD_POWERUP_LOCK;
  3054. + parts[i].mask_flags |= MTD_POWERUP_LOCK;
  3055. i++;
  3056. }
  3057. @@ -127,6 +126,7 @@ static int parse_ofpart_partitions(struc
  3058. if (!nr_parts)
  3059. goto ofpart_none;
  3060. + *pparts = parts;
  3061. return nr_parts;
  3062. ofpart_fail:
  3063. @@ -135,21 +135,20 @@ ofpart_fail:
  3064. ret = -EINVAL;
  3065. ofpart_none:
  3066. of_node_put(pp);
  3067. - kfree(*pparts);
  3068. - *pparts = NULL;
  3069. + kfree(parts);
  3070. return ret;
  3071. }
  3072. static struct mtd_part_parser ofpart_parser = {
  3073. - .owner = THIS_MODULE,
  3074. .parse_fn = parse_ofpart_partitions,
  3075. .name = "ofpart",
  3076. };
  3077. static int parse_ofoldpart_partitions(struct mtd_info *master,
  3078. - struct mtd_partition **pparts,
  3079. + const struct mtd_partition **pparts,
  3080. struct mtd_part_parser_data *data)
  3081. {
  3082. + struct mtd_partition *parts;
  3083. struct device_node *dp;
  3084. int i, plen, nr_parts;
  3085. const struct {
  3086. @@ -157,10 +156,8 @@ static int parse_ofoldpart_partitions(st
  3087. } *part;
  3088. const char *names;
  3089. - if (!data)
  3090. - return 0;
  3091. -
  3092. - dp = data->of_node;
  3093. + /* Pull of_node from the master device node */
  3094. + dp = mtd_get_of_node(master);
  3095. if (!dp)
  3096. return 0;
  3097. @@ -173,37 +170,37 @@ static int parse_ofoldpart_partitions(st
  3098. nr_parts = plen / sizeof(part[0]);
  3099. - *pparts = kzalloc(nr_parts * sizeof(*(*pparts)), GFP_KERNEL);
  3100. - if (!*pparts)
  3101. + parts = kzalloc(nr_parts * sizeof(*parts), GFP_KERNEL);
  3102. + if (!parts)
  3103. return -ENOMEM;
  3104. names = of_get_property(dp, "partition-names", &plen);
  3105. for (i = 0; i < nr_parts; i++) {
  3106. - (*pparts)[i].offset = be32_to_cpu(part->offset);
  3107. - (*pparts)[i].size = be32_to_cpu(part->len) & ~1;
  3108. + parts[i].offset = be32_to_cpu(part->offset);
  3109. + parts[i].size = be32_to_cpu(part->len) & ~1;
  3110. /* bit 0 set signifies read only partition */
  3111. if (be32_to_cpu(part->len) & 1)
  3112. - (*pparts)[i].mask_flags = MTD_WRITEABLE;
  3113. + parts[i].mask_flags = MTD_WRITEABLE;
  3114. if (names && (plen > 0)) {
  3115. int len = strlen(names) + 1;
  3116. - (*pparts)[i].name = names;
  3117. + parts[i].name = names;
  3118. plen -= len;
  3119. names += len;
  3120. } else {
  3121. - (*pparts)[i].name = "unnamed";
  3122. + parts[i].name = "unnamed";
  3123. }
  3124. part++;
  3125. }
  3126. + *pparts = parts;
  3127. return nr_parts;
  3128. }
  3129. static struct mtd_part_parser ofoldpart_parser = {
  3130. - .owner = THIS_MODULE,
  3131. .parse_fn = parse_ofoldpart_partitions,
  3132. .name = "ofoldpart",
  3133. };
  3134. --- a/drivers/mtd/spi-nor/Kconfig
  3135. +++ b/drivers/mtd/spi-nor/Kconfig
  3136. @@ -7,6 +7,14 @@ menuconfig MTD_SPI_NOR
  3137. if MTD_SPI_NOR
  3138. +config MTD_MT81xx_NOR
  3139. + tristate "Mediatek MT81xx SPI NOR flash controller"
  3140. + depends on HAS_IOMEM
  3141. + help
  3142. + This enables access to SPI NOR flash, using MT81xx SPI NOR flash
  3143. + controller. This controller does not support generic SPI BUS, it only
  3144. + supports SPI NOR Flash.
  3145. +
  3146. config MTD_SPI_NOR_USE_4K_SECTORS
  3147. bool "Use small 4096 B erase sectors"
  3148. default y
  3149. @@ -23,7 +31,7 @@ config MTD_SPI_NOR_USE_4K_SECTORS
  3150. config SPI_FSL_QUADSPI
  3151. tristate "Freescale Quad SPI controller"
  3152. - depends on ARCH_MXC || COMPILE_TEST
  3153. + depends on ARCH_MXC || SOC_LS1021A || ARCH_LAYERSCAPE || COMPILE_TEST
  3154. depends on HAS_IOMEM
  3155. help
  3156. This enables support for the Quad SPI controller in master mode.
  3157. --- a/drivers/mtd/spi-nor/Makefile
  3158. +++ b/drivers/mtd/spi-nor/Makefile
  3159. @@ -1,3 +1,4 @@
  3160. obj-$(CONFIG_MTD_SPI_NOR) += spi-nor.o
  3161. obj-$(CONFIG_SPI_FSL_QUADSPI) += fsl-quadspi.o
  3162. +obj-$(CONFIG_MTD_MT81xx_NOR) += mtk-quadspi.o
  3163. obj-$(CONFIG_SPI_NXP_SPIFI) += nxp-spifi.o
  3164. --- /dev/null
  3165. +++ b/drivers/mtd/spi-nor/mtk-quadspi.c
  3166. @@ -0,0 +1,485 @@
  3167. +/*
  3168. + * Copyright (c) 2015 MediaTek Inc.
  3169. + * Author: Bayi Cheng <bayi.cheng@mediatek.com>
  3170. + *
  3171. + * This program is free software; you can redistribute it and/or modify
  3172. + * it under the terms of the GNU General Public License version 2 as
  3173. + * published by the Free Software Foundation.
  3174. + *
  3175. + * This program is distributed in the hope that it will be useful,
  3176. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  3177. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  3178. + * GNU General Public License for more details.
  3179. + */
  3180. +
  3181. +#include <linux/clk.h>
  3182. +#include <linux/delay.h>
  3183. +#include <linux/device.h>
  3184. +#include <linux/init.h>
  3185. +#include <linux/io.h>
  3186. +#include <linux/iopoll.h>
  3187. +#include <linux/ioport.h>
  3188. +#include <linux/math64.h>
  3189. +#include <linux/module.h>
  3190. +#include <linux/mtd/mtd.h>
  3191. +#include <linux/mutex.h>
  3192. +#include <linux/of.h>
  3193. +#include <linux/of_device.h>
  3194. +#include <linux/pinctrl/consumer.h>
  3195. +#include <linux/platform_device.h>
  3196. +#include <linux/slab.h>
  3197. +#include <linux/mtd/mtd.h>
  3198. +#include <linux/mtd/partitions.h>
  3199. +#include <linux/mtd/spi-nor.h>
  3200. +
  3201. +#define MTK_NOR_CMD_REG 0x00
  3202. +#define MTK_NOR_CNT_REG 0x04
  3203. +#define MTK_NOR_RDSR_REG 0x08
  3204. +#define MTK_NOR_RDATA_REG 0x0c
  3205. +#define MTK_NOR_RADR0_REG 0x10
  3206. +#define MTK_NOR_RADR1_REG 0x14
  3207. +#define MTK_NOR_RADR2_REG 0x18
  3208. +#define MTK_NOR_WDATA_REG 0x1c
  3209. +#define MTK_NOR_PRGDATA0_REG 0x20
  3210. +#define MTK_NOR_PRGDATA1_REG 0x24
  3211. +#define MTK_NOR_PRGDATA2_REG 0x28
  3212. +#define MTK_NOR_PRGDATA3_REG 0x2c
  3213. +#define MTK_NOR_PRGDATA4_REG 0x30
  3214. +#define MTK_NOR_PRGDATA5_REG 0x34
  3215. +#define MTK_NOR_SHREG0_REG 0x38
  3216. +#define MTK_NOR_SHREG1_REG 0x3c
  3217. +#define MTK_NOR_SHREG2_REG 0x40
  3218. +#define MTK_NOR_SHREG3_REG 0x44
  3219. +#define MTK_NOR_SHREG4_REG 0x48
  3220. +#define MTK_NOR_SHREG5_REG 0x4c
  3221. +#define MTK_NOR_SHREG6_REG 0x50
  3222. +#define MTK_NOR_SHREG7_REG 0x54
  3223. +#define MTK_NOR_SHREG8_REG 0x58
  3224. +#define MTK_NOR_SHREG9_REG 0x5c
  3225. +#define MTK_NOR_CFG1_REG 0x60
  3226. +#define MTK_NOR_CFG2_REG 0x64
  3227. +#define MTK_NOR_CFG3_REG 0x68
  3228. +#define MTK_NOR_STATUS0_REG 0x70
  3229. +#define MTK_NOR_STATUS1_REG 0x74
  3230. +#define MTK_NOR_STATUS2_REG 0x78
  3231. +#define MTK_NOR_STATUS3_REG 0x7c
  3232. +#define MTK_NOR_FLHCFG_REG 0x84
  3233. +#define MTK_NOR_TIME_REG 0x94
  3234. +#define MTK_NOR_PP_DATA_REG 0x98
  3235. +#define MTK_NOR_PREBUF_STUS_REG 0x9c
  3236. +#define MTK_NOR_DELSEL0_REG 0xa0
  3237. +#define MTK_NOR_DELSEL1_REG 0xa4
  3238. +#define MTK_NOR_INTRSTUS_REG 0xa8
  3239. +#define MTK_NOR_INTREN_REG 0xac
  3240. +#define MTK_NOR_CHKSUM_CTL_REG 0xb8
  3241. +#define MTK_NOR_CHKSUM_REG 0xbc
  3242. +#define MTK_NOR_CMD2_REG 0xc0
  3243. +#define MTK_NOR_WRPROT_REG 0xc4
  3244. +#define MTK_NOR_RADR3_REG 0xc8
  3245. +#define MTK_NOR_DUAL_REG 0xcc
  3246. +#define MTK_NOR_DELSEL2_REG 0xd0
  3247. +#define MTK_NOR_DELSEL3_REG 0xd4
  3248. +#define MTK_NOR_DELSEL4_REG 0xd8
  3249. +
  3250. +/* commands for mtk nor controller */
  3251. +#define MTK_NOR_READ_CMD 0x0
  3252. +#define MTK_NOR_RDSR_CMD 0x2
  3253. +#define MTK_NOR_PRG_CMD 0x4
  3254. +#define MTK_NOR_WR_CMD 0x10
  3255. +#define MTK_NOR_PIO_WR_CMD 0x90
  3256. +#define MTK_NOR_WRSR_CMD 0x20
  3257. +#define MTK_NOR_PIO_READ_CMD 0x81
  3258. +#define MTK_NOR_WR_BUF_ENABLE 0x1
  3259. +#define MTK_NOR_WR_BUF_DISABLE 0x0
  3260. +#define MTK_NOR_ENABLE_SF_CMD 0x30
  3261. +#define MTK_NOR_DUAD_ADDR_EN 0x8
  3262. +#define MTK_NOR_QUAD_READ_EN 0x4
  3263. +#define MTK_NOR_DUAL_ADDR_EN 0x2
  3264. +#define MTK_NOR_DUAL_READ_EN 0x1
  3265. +#define MTK_NOR_DUAL_DISABLE 0x0
  3266. +#define MTK_NOR_FAST_READ 0x1
  3267. +
  3268. +#define SFLASH_WRBUF_SIZE 128
  3269. +
  3270. +/* Can shift up to 48 bits (6 bytes) of TX/RX */
  3271. +#define MTK_NOR_MAX_RX_TX_SHIFT 6
  3272. +/* can shift up to 56 bits (7 bytes) transfer by MTK_NOR_PRG_CMD */
  3273. +#define MTK_NOR_MAX_SHIFT 7
  3274. +
  3275. +/* Helpers for accessing the program data / shift data registers */
  3276. +#define MTK_NOR_PRG_REG(n) (MTK_NOR_PRGDATA0_REG + 4 * (n))
  3277. +#define MTK_NOR_SHREG(n) (MTK_NOR_SHREG0_REG + 4 * (n))
  3278. +
  3279. +struct mt8173_nor {
  3280. + struct spi_nor nor;
  3281. + struct device *dev;
  3282. + void __iomem *base; /* nor flash base address */
  3283. + struct clk *spi_clk;
  3284. + struct clk *nor_clk;
  3285. +};
  3286. +
  3287. +static void mt8173_nor_set_read_mode(struct mt8173_nor *mt8173_nor)
  3288. +{
  3289. + struct spi_nor *nor = &mt8173_nor->nor;
  3290. +
  3291. + switch (nor->flash_read) {
  3292. + case SPI_NOR_FAST:
  3293. + writeb(nor->read_opcode, mt8173_nor->base +
  3294. + MTK_NOR_PRGDATA3_REG);
  3295. + writeb(MTK_NOR_FAST_READ, mt8173_nor->base +
  3296. + MTK_NOR_CFG1_REG);
  3297. + break;
  3298. + case SPI_NOR_DUAL:
  3299. + writeb(nor->read_opcode, mt8173_nor->base +
  3300. + MTK_NOR_PRGDATA3_REG);
  3301. + writeb(MTK_NOR_DUAL_READ_EN, mt8173_nor->base +
  3302. + MTK_NOR_DUAL_REG);
  3303. + break;
  3304. + case SPI_NOR_QUAD:
  3305. + writeb(nor->read_opcode, mt8173_nor->base +
  3306. + MTK_NOR_PRGDATA4_REG);
  3307. + writeb(MTK_NOR_QUAD_READ_EN, mt8173_nor->base +
  3308. + MTK_NOR_DUAL_REG);
  3309. + break;
  3310. + default:
  3311. + writeb(MTK_NOR_DUAL_DISABLE, mt8173_nor->base +
  3312. + MTK_NOR_DUAL_REG);
  3313. + break;
  3314. + }
  3315. +}
  3316. +
  3317. +static int mt8173_nor_execute_cmd(struct mt8173_nor *mt8173_nor, u8 cmdval)
  3318. +{
  3319. + int reg;
  3320. + u8 val = cmdval & 0x1f;
  3321. +
  3322. + writeb(cmdval, mt8173_nor->base + MTK_NOR_CMD_REG);
  3323. + return readl_poll_timeout(mt8173_nor->base + MTK_NOR_CMD_REG, reg,
  3324. + !(reg & val), 100, 10000);
  3325. +}
  3326. +
  3327. +static int mt8173_nor_do_tx_rx(struct mt8173_nor *mt8173_nor, u8 op,
  3328. + u8 *tx, int txlen, u8 *rx, int rxlen)
  3329. +{
  3330. + int len = 1 + txlen + rxlen;
  3331. + int i, ret, idx;
  3332. +
  3333. + if (len > MTK_NOR_MAX_SHIFT)
  3334. + return -EINVAL;
  3335. +
  3336. + writeb(len * 8, mt8173_nor->base + MTK_NOR_CNT_REG);
  3337. +
  3338. + /* start at PRGDATA5, go down to PRGDATA0 */
  3339. + idx = MTK_NOR_MAX_RX_TX_SHIFT - 1;
  3340. +
  3341. + /* opcode */
  3342. + writeb(op, mt8173_nor->base + MTK_NOR_PRG_REG(idx));
  3343. + idx--;
  3344. +
  3345. + /* program TX data */
  3346. + for (i = 0; i < txlen; i++, idx--)
  3347. + writeb(tx[i], mt8173_nor->base + MTK_NOR_PRG_REG(idx));
  3348. +
  3349. + /* clear out rest of TX registers */
  3350. + while (idx >= 0) {
  3351. + writeb(0, mt8173_nor->base + MTK_NOR_PRG_REG(idx));
  3352. + idx--;
  3353. + }
  3354. +
  3355. + ret = mt8173_nor_execute_cmd(mt8173_nor, MTK_NOR_PRG_CMD);
  3356. + if (ret)
  3357. + return ret;
  3358. +
  3359. + /* restart at first RX byte */
  3360. + idx = rxlen - 1;
  3361. +
  3362. + /* read out RX data */
  3363. + for (i = 0; i < rxlen; i++, idx--)
  3364. + rx[i] = readb(mt8173_nor->base + MTK_NOR_SHREG(idx));
  3365. +
  3366. + return 0;
  3367. +}
  3368. +
  3369. +/* Do a WRSR (Write Status Register) command */
  3370. +static int mt8173_nor_wr_sr(struct mt8173_nor *mt8173_nor, u8 sr)
  3371. +{
  3372. + writeb(sr, mt8173_nor->base + MTK_NOR_PRGDATA5_REG);
  3373. + writeb(8, mt8173_nor->base + MTK_NOR_CNT_REG);
  3374. + return mt8173_nor_execute_cmd(mt8173_nor, MTK_NOR_WRSR_CMD);
  3375. +}
  3376. +
  3377. +static int mt8173_nor_write_buffer_enable(struct mt8173_nor *mt8173_nor)
  3378. +{
  3379. + u8 reg;
  3380. +
  3381. + /* the bit0 of MTK_NOR_CFG2_REG is pre-fetch buffer
  3382. + * 0: pre-fetch buffer use for read
  3383. + * 1: pre-fetch buffer use for page program
  3384. + */
  3385. + writel(MTK_NOR_WR_BUF_ENABLE, mt8173_nor->base + MTK_NOR_CFG2_REG);
  3386. + return readb_poll_timeout(mt8173_nor->base + MTK_NOR_CFG2_REG, reg,
  3387. + 0x01 == (reg & 0x01), 100, 10000);
  3388. +}
  3389. +
  3390. +static int mt8173_nor_write_buffer_disable(struct mt8173_nor *mt8173_nor)
  3391. +{
  3392. + u8 reg;
  3393. +
  3394. + writel(MTK_NOR_WR_BUF_DISABLE, mt8173_nor->base + MTK_NOR_CFG2_REG);
  3395. + return readb_poll_timeout(mt8173_nor->base + MTK_NOR_CFG2_REG, reg,
  3396. + MTK_NOR_WR_BUF_DISABLE == (reg & 0x1), 100,
  3397. + 10000);
  3398. +}
  3399. +
  3400. +static void mt8173_nor_set_addr(struct mt8173_nor *mt8173_nor, u32 addr)
  3401. +{
  3402. + int i;
  3403. +
  3404. + for (i = 0; i < 3; i++) {
  3405. + writeb(addr & 0xff, mt8173_nor->base + MTK_NOR_RADR0_REG + i * 4);
  3406. + addr >>= 8;
  3407. + }
  3408. + /* Last register is non-contiguous */
  3409. + writeb(addr & 0xff, mt8173_nor->base + MTK_NOR_RADR3_REG);
  3410. +}
  3411. +
  3412. +static int mt8173_nor_read(struct spi_nor *nor, loff_t from, size_t length,
  3413. + size_t *retlen, u_char *buffer)
  3414. +{
  3415. + int i, ret;
  3416. + int addr = (int)from;
  3417. + u8 *buf = (u8 *)buffer;
  3418. + struct mt8173_nor *mt8173_nor = nor->priv;
  3419. +
  3420. + /* set mode for fast read mode ,dual mode or quad mode */
  3421. + mt8173_nor_set_read_mode(mt8173_nor);
  3422. + mt8173_nor_set_addr(mt8173_nor, addr);
  3423. +
  3424. + for (i = 0; i < length; i++, (*retlen)++) {
  3425. + ret = mt8173_nor_execute_cmd(mt8173_nor, MTK_NOR_PIO_READ_CMD);
  3426. + if (ret < 0)
  3427. + return ret;
  3428. + buf[i] = readb(mt8173_nor->base + MTK_NOR_RDATA_REG);
  3429. + }
  3430. + return 0;
  3431. +}
  3432. +
  3433. +static int mt8173_nor_write_single_byte(struct mt8173_nor *mt8173_nor,
  3434. + int addr, int length, u8 *data)
  3435. +{
  3436. + int i, ret;
  3437. +
  3438. + mt8173_nor_set_addr(mt8173_nor, addr);
  3439. +
  3440. + for (i = 0; i < length; i++) {
  3441. + writeb(*data++, mt8173_nor->base + MTK_NOR_WDATA_REG);
  3442. + ret = mt8173_nor_execute_cmd(mt8173_nor, MTK_NOR_PIO_WR_CMD);
  3443. + if (ret < 0)
  3444. + return ret;
  3445. + }
  3446. + return 0;
  3447. +}
  3448. +
  3449. +static int mt8173_nor_write_buffer(struct mt8173_nor *mt8173_nor, int addr,
  3450. + const u8 *buf)
  3451. +{
  3452. + int i, bufidx, data;
  3453. +
  3454. + mt8173_nor_set_addr(mt8173_nor, addr);
  3455. +
  3456. + bufidx = 0;
  3457. + for (i = 0; i < SFLASH_WRBUF_SIZE; i += 4) {
  3458. + data = buf[bufidx + 3]<<24 | buf[bufidx + 2]<<16 |
  3459. + buf[bufidx + 1]<<8 | buf[bufidx];
  3460. + bufidx += 4;
  3461. + writel(data, mt8173_nor->base + MTK_NOR_PP_DATA_REG);
  3462. + }
  3463. + return mt8173_nor_execute_cmd(mt8173_nor, MTK_NOR_WR_CMD);
  3464. +}
  3465. +
  3466. +static void mt8173_nor_write(struct spi_nor *nor, loff_t to, size_t len,
  3467. + size_t *retlen, const u_char *buf)
  3468. +{
  3469. + int ret;
  3470. + struct mt8173_nor *mt8173_nor = nor->priv;
  3471. +
  3472. + ret = mt8173_nor_write_buffer_enable(mt8173_nor);
  3473. + if (ret < 0)
  3474. + dev_warn(mt8173_nor->dev, "write buffer enable failed!\n");
  3475. +
  3476. + while (len >= SFLASH_WRBUF_SIZE) {
  3477. + ret = mt8173_nor_write_buffer(mt8173_nor, to, buf);
  3478. + if (ret < 0)
  3479. + dev_err(mt8173_nor->dev, "write buffer failed!\n");
  3480. + len -= SFLASH_WRBUF_SIZE;
  3481. + to += SFLASH_WRBUF_SIZE;
  3482. + buf += SFLASH_WRBUF_SIZE;
  3483. + (*retlen) += SFLASH_WRBUF_SIZE;
  3484. + }
  3485. + ret = mt8173_nor_write_buffer_disable(mt8173_nor);
  3486. + if (ret < 0)
  3487. + dev_warn(mt8173_nor->dev, "write buffer disable failed!\n");
  3488. +
  3489. + if (len) {
  3490. + ret = mt8173_nor_write_single_byte(mt8173_nor, to, (int)len,
  3491. + (u8 *)buf);
  3492. + if (ret < 0)
  3493. + dev_err(mt8173_nor->dev, "write single byte failed!\n");
  3494. + (*retlen) += len;
  3495. + }
  3496. +}
  3497. +
  3498. +static int mt8173_nor_read_reg(struct spi_nor *nor, u8 opcode, u8 *buf, int len)
  3499. +{
  3500. + int ret;
  3501. + struct mt8173_nor *mt8173_nor = nor->priv;
  3502. +
  3503. + switch (opcode) {
  3504. + case SPINOR_OP_RDSR:
  3505. + ret = mt8173_nor_execute_cmd(mt8173_nor, MTK_NOR_RDSR_CMD);
  3506. + if (ret < 0)
  3507. + return ret;
  3508. + if (len == 1)
  3509. + *buf = readb(mt8173_nor->base + MTK_NOR_RDSR_REG);
  3510. + else
  3511. + dev_err(mt8173_nor->dev, "len should be 1 for read status!\n");
  3512. + break;
  3513. + default:
  3514. + ret = mt8173_nor_do_tx_rx(mt8173_nor, opcode, NULL, 0, buf, len);
  3515. + break;
  3516. + }
  3517. + return ret;
  3518. +}
  3519. +
  3520. +static int mt8173_nor_write_reg(struct spi_nor *nor, u8 opcode, u8 *buf,
  3521. + int len)
  3522. +{
  3523. + int ret;
  3524. + struct mt8173_nor *mt8173_nor = nor->priv;
  3525. +
  3526. + switch (opcode) {
  3527. + case SPINOR_OP_WRSR:
  3528. + /* We only handle 1 byte */
  3529. + ret = mt8173_nor_wr_sr(mt8173_nor, *buf);
  3530. + break;
  3531. + default:
  3532. + ret = mt8173_nor_do_tx_rx(mt8173_nor, opcode, buf, len, NULL, 0);
  3533. + if (ret)
  3534. + dev_warn(mt8173_nor->dev, "write reg failure!\n");
  3535. + break;
  3536. + }
  3537. + return ret;
  3538. +}
  3539. +
  3540. +static int mtk_nor_init(struct mt8173_nor *mt8173_nor,
  3541. + struct device_node *flash_node)
  3542. +{
  3543. + int ret;
  3544. + struct spi_nor *nor;
  3545. +
  3546. + /* initialize controller to accept commands */
  3547. + writel(MTK_NOR_ENABLE_SF_CMD, mt8173_nor->base + MTK_NOR_WRPROT_REG);
  3548. +
  3549. + nor = &mt8173_nor->nor;
  3550. + nor->dev = mt8173_nor->dev;
  3551. + nor->priv = mt8173_nor;
  3552. + spi_nor_set_flash_node(nor, flash_node);
  3553. +
  3554. + /* fill the hooks to spi nor */
  3555. + nor->read = mt8173_nor_read;
  3556. + nor->read_reg = mt8173_nor_read_reg;
  3557. + nor->write = mt8173_nor_write;
  3558. + nor->write_reg = mt8173_nor_write_reg;
  3559. + nor->mtd.name = "mtk_nor";
  3560. + /* initialized with NULL */
  3561. + ret = spi_nor_scan(nor, NULL, SPI_NOR_DUAL);
  3562. + if (ret)
  3563. + return ret;
  3564. +
  3565. + return mtd_device_register(&nor->mtd, NULL, 0);
  3566. +}
  3567. +
  3568. +static int mtk_nor_drv_probe(struct platform_device *pdev)
  3569. +{
  3570. + struct device_node *flash_np;
  3571. + struct resource *res;
  3572. + int ret;
  3573. + struct mt8173_nor *mt8173_nor;
  3574. +
  3575. + if (!pdev->dev.of_node) {
  3576. + dev_err(&pdev->dev, "No DT found\n");
  3577. + return -EINVAL;
  3578. + }
  3579. +
  3580. + mt8173_nor = devm_kzalloc(&pdev->dev, sizeof(*mt8173_nor), GFP_KERNEL);
  3581. + if (!mt8173_nor)
  3582. + return -ENOMEM;
  3583. + platform_set_drvdata(pdev, mt8173_nor);
  3584. +
  3585. + res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  3586. + mt8173_nor->base = devm_ioremap_resource(&pdev->dev, res);
  3587. + if (IS_ERR(mt8173_nor->base))
  3588. + return PTR_ERR(mt8173_nor->base);
  3589. +
  3590. + mt8173_nor->spi_clk = devm_clk_get(&pdev->dev, "spi");
  3591. + if (IS_ERR(mt8173_nor->spi_clk))
  3592. + return PTR_ERR(mt8173_nor->spi_clk);
  3593. +
  3594. + mt8173_nor->nor_clk = devm_clk_get(&pdev->dev, "sf");
  3595. + if (IS_ERR(mt8173_nor->nor_clk))
  3596. + return PTR_ERR(mt8173_nor->nor_clk);
  3597. +
  3598. + mt8173_nor->dev = &pdev->dev;
  3599. + ret = clk_prepare_enable(mt8173_nor->spi_clk);
  3600. + if (ret)
  3601. + return ret;
  3602. +
  3603. + ret = clk_prepare_enable(mt8173_nor->nor_clk);
  3604. + if (ret) {
  3605. + clk_disable_unprepare(mt8173_nor->spi_clk);
  3606. + return ret;
  3607. + }
  3608. + /* only support one attached flash */
  3609. + flash_np = of_get_next_available_child(pdev->dev.of_node, NULL);
  3610. + if (!flash_np) {
  3611. + dev_err(&pdev->dev, "no SPI flash device to configure\n");
  3612. + ret = -ENODEV;
  3613. + goto nor_free;
  3614. + }
  3615. + ret = mtk_nor_init(mt8173_nor, flash_np);
  3616. +
  3617. +nor_free:
  3618. + if (ret) {
  3619. + clk_disable_unprepare(mt8173_nor->spi_clk);
  3620. + clk_disable_unprepare(mt8173_nor->nor_clk);
  3621. + }
  3622. + return ret;
  3623. +}
  3624. +
  3625. +static int mtk_nor_drv_remove(struct platform_device *pdev)
  3626. +{
  3627. + struct mt8173_nor *mt8173_nor = platform_get_drvdata(pdev);
  3628. +
  3629. + clk_disable_unprepare(mt8173_nor->spi_clk);
  3630. + clk_disable_unprepare(mt8173_nor->nor_clk);
  3631. + return 0;
  3632. +}
  3633. +
  3634. +static const struct of_device_id mtk_nor_of_ids[] = {
  3635. + { .compatible = "mediatek,mt8173-nor"},
  3636. + { /* sentinel */ }
  3637. +};
  3638. +MODULE_DEVICE_TABLE(of, mtk_nor_of_ids);
  3639. +
  3640. +static struct platform_driver mtk_nor_driver = {
  3641. + .probe = mtk_nor_drv_probe,
  3642. + .remove = mtk_nor_drv_remove,
  3643. + .driver = {
  3644. + .name = "mtk-nor",
  3645. + .of_match_table = mtk_nor_of_ids,
  3646. + },
  3647. +};
  3648. +
  3649. +module_platform_driver(mtk_nor_driver);
  3650. +MODULE_LICENSE("GPL v2");
  3651. +MODULE_DESCRIPTION("MediaTek SPI NOR Flash Driver");
  3652. --- a/drivers/mtd/spi-nor/spi-nor.c
  3653. +++ b/drivers/mtd/spi-nor/spi-nor.c
  3654. @@ -38,6 +38,7 @@
  3655. #define CHIP_ERASE_2MB_READY_WAIT_JIFFIES (40UL * HZ)
  3656. #define SPI_NOR_MAX_ID_LEN 6
  3657. +#define SPI_NOR_MAX_ADDR_WIDTH 4
  3658. struct flash_info {
  3659. char *name;
  3660. @@ -60,15 +61,20 @@ struct flash_info {
  3661. u16 addr_width;
  3662. u16 flags;
  3663. -#define SECT_4K 0x01 /* SPINOR_OP_BE_4K works uniformly */
  3664. -#define SPI_NOR_NO_ERASE 0x02 /* No erase command needed */
  3665. -#define SST_WRITE 0x04 /* use SST byte programming */
  3666. -#define SPI_NOR_NO_FR 0x08 /* Can't do fastread */
  3667. -#define SECT_4K_PMC 0x10 /* SPINOR_OP_BE_4K_PMC works uniformly */
  3668. -#define SPI_NOR_DUAL_READ 0x20 /* Flash supports Dual Read */
  3669. -#define SPI_NOR_QUAD_READ 0x40 /* Flash supports Quad Read */
  3670. -#define USE_FSR 0x80 /* use flag status register */
  3671. -#define SPI_NOR_HAS_LOCK 0x100 /* Flash supports lock/unlock via SR */
  3672. +#define SECT_4K BIT(0) /* SPINOR_OP_BE_4K works uniformly */
  3673. +#define SPI_NOR_NO_ERASE BIT(1) /* No erase command needed */
  3674. +#define SST_WRITE BIT(2) /* use SST byte programming */
  3675. +#define SPI_NOR_NO_FR BIT(3) /* Can't do fastread */
  3676. +#define SECT_4K_PMC BIT(4) /* SPINOR_OP_BE_4K_PMC works uniformly */
  3677. +#define SPI_NOR_DUAL_READ BIT(5) /* Flash supports Dual Read */
  3678. +#define SPI_NOR_QUAD_READ BIT(6) /* Flash supports Quad Read */
  3679. +#define USE_FSR BIT(7) /* use flag status register */
  3680. +#define SPI_NOR_HAS_LOCK BIT(8) /* Flash supports lock/unlock via SR */
  3681. +#define SPI_NOR_HAS_TB BIT(9) /*
  3682. + * Flash SR has Top/Bottom (TB) protect
  3683. + * bit. Must be used with
  3684. + * SPI_NOR_HAS_LOCK.
  3685. + */
  3686. };
  3687. #define JEDEC_MFR(info) ((info)->id[0])
  3688. @@ -314,6 +320,29 @@ static void spi_nor_unlock_and_unprep(st
  3689. }
  3690. /*
  3691. + * Initiate the erasure of a single sector
  3692. + */
  3693. +static int spi_nor_erase_sector(struct spi_nor *nor, u32 addr)
  3694. +{
  3695. + u8 buf[SPI_NOR_MAX_ADDR_WIDTH];
  3696. + int i;
  3697. +
  3698. + if (nor->erase)
  3699. + return nor->erase(nor, addr);
  3700. +
  3701. + /*
  3702. + * Default implementation, if driver doesn't have a specialized HW
  3703. + * control
  3704. + */
  3705. + for (i = nor->addr_width - 1; i >= 0; i--) {
  3706. + buf[i] = addr & 0xff;
  3707. + addr >>= 8;
  3708. + }
  3709. +
  3710. + return nor->write_reg(nor, nor->erase_opcode, buf, nor->addr_width);
  3711. +}
  3712. +
  3713. +/*
  3714. * Erase an address range on the nor chip. The address range may extend
  3715. * one or more erase sectors. Return an error is there is a problem erasing.
  3716. */
  3717. @@ -372,10 +401,9 @@ static int spi_nor_erase(struct mtd_info
  3718. while (len) {
  3719. write_enable(nor);
  3720. - if (nor->erase(nor, addr)) {
  3721. - ret = -EIO;
  3722. + ret = spi_nor_erase_sector(nor, addr);
  3723. + if (ret)
  3724. goto erase_err;
  3725. - }
  3726. addr += mtd->erasesize;
  3727. len -= mtd->erasesize;
  3728. @@ -388,17 +416,13 @@ static int spi_nor_erase(struct mtd_info
  3729. write_disable(nor);
  3730. +erase_err:
  3731. spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_ERASE);
  3732. - instr->state = MTD_ERASE_DONE;
  3733. + instr->state = ret ? MTD_ERASE_FAILED : MTD_ERASE_DONE;
  3734. mtd_erase_callback(instr);
  3735. return ret;
  3736. -
  3737. -erase_err:
  3738. - spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_ERASE);
  3739. - instr->state = MTD_ERASE_FAILED;
  3740. - return ret;
  3741. }
  3742. static void stm_get_locked_range(struct spi_nor *nor, u8 sr, loff_t *ofs,
  3743. @@ -416,32 +440,58 @@ static void stm_get_locked_range(struct
  3744. } else {
  3745. pow = ((sr & mask) ^ mask) >> shift;
  3746. *len = mtd->size >> pow;
  3747. - *ofs = mtd->size - *len;
  3748. + if (nor->flags & SNOR_F_HAS_SR_TB && sr & SR_TB)
  3749. + *ofs = 0;
  3750. + else
  3751. + *ofs = mtd->size - *len;
  3752. }
  3753. }
  3754. /*
  3755. - * Return 1 if the entire region is locked, 0 otherwise
  3756. + * Return 1 if the entire region is locked (if @locked is true) or unlocked (if
  3757. + * @locked is false); 0 otherwise
  3758. */
  3759. -static int stm_is_locked_sr(struct spi_nor *nor, loff_t ofs, uint64_t len,
  3760. - u8 sr)
  3761. +static int stm_check_lock_status_sr(struct spi_nor *nor, loff_t ofs, uint64_t len,
  3762. + u8 sr, bool locked)
  3763. {
  3764. loff_t lock_offs;
  3765. uint64_t lock_len;
  3766. + if (!len)
  3767. + return 1;
  3768. +
  3769. stm_get_locked_range(nor, sr, &lock_offs, &lock_len);
  3770. - return (ofs + len <= lock_offs + lock_len) && (ofs >= lock_offs);
  3771. + if (locked)
  3772. + /* Requested range is a sub-range of locked range */
  3773. + return (ofs + len <= lock_offs + lock_len) && (ofs >= lock_offs);
  3774. + else
  3775. + /* Requested range does not overlap with locked range */
  3776. + return (ofs >= lock_offs + lock_len) || (ofs + len <= lock_offs);
  3777. +}
  3778. +
  3779. +static int stm_is_locked_sr(struct spi_nor *nor, loff_t ofs, uint64_t len,
  3780. + u8 sr)
  3781. +{
  3782. + return stm_check_lock_status_sr(nor, ofs, len, sr, true);
  3783. +}
  3784. +
  3785. +static int stm_is_unlocked_sr(struct spi_nor *nor, loff_t ofs, uint64_t len,
  3786. + u8 sr)
  3787. +{
  3788. + return stm_check_lock_status_sr(nor, ofs, len, sr, false);
  3789. }
  3790. /*
  3791. * Lock a region of the flash. Compatible with ST Micro and similar flash.
  3792. - * Supports only the block protection bits BP{0,1,2} in the status register
  3793. + * Supports the block protection bits BP{0,1,2} in the status register
  3794. * (SR). Does not support these features found in newer SR bitfields:
  3795. - * - TB: top/bottom protect - only handle TB=0 (top protect)
  3796. * - SEC: sector/block protect - only handle SEC=0 (block protect)
  3797. * - CMP: complement protect - only support CMP=0 (range is not complemented)
  3798. *
  3799. + * Support for the following is provided conditionally for some flash:
  3800. + * - TB: top/bottom protect
  3801. + *
  3802. * Sample table portion for 8MB flash (Winbond w25q64fw):
  3803. *
  3804. * SEC | TB | BP2 | BP1 | BP0 | Prot Length | Protected Portion
  3805. @@ -454,26 +504,55 @@ static int stm_is_locked_sr(struct spi_n
  3806. * 0 | 0 | 1 | 0 | 1 | 2 MB | Upper 1/4
  3807. * 0 | 0 | 1 | 1 | 0 | 4 MB | Upper 1/2
  3808. * X | X | 1 | 1 | 1 | 8 MB | ALL
  3809. + * ------|-------|-------|-------|-------|---------------|-------------------
  3810. + * 0 | 1 | 0 | 0 | 1 | 128 KB | Lower 1/64
  3811. + * 0 | 1 | 0 | 1 | 0 | 256 KB | Lower 1/32
  3812. + * 0 | 1 | 0 | 1 | 1 | 512 KB | Lower 1/16
  3813. + * 0 | 1 | 1 | 0 | 0 | 1 MB | Lower 1/8
  3814. + * 0 | 1 | 1 | 0 | 1 | 2 MB | Lower 1/4
  3815. + * 0 | 1 | 1 | 1 | 0 | 4 MB | Lower 1/2
  3816. *
  3817. * Returns negative on errors, 0 on success.
  3818. */
  3819. static int stm_lock(struct spi_nor *nor, loff_t ofs, uint64_t len)
  3820. {
  3821. struct mtd_info *mtd = &nor->mtd;
  3822. - u8 status_old, status_new;
  3823. + int status_old, status_new;
  3824. u8 mask = SR_BP2 | SR_BP1 | SR_BP0;
  3825. u8 shift = ffs(mask) - 1, pow, val;
  3826. + loff_t lock_len;
  3827. + bool can_be_top = true, can_be_bottom = nor->flags & SNOR_F_HAS_SR_TB;
  3828. + bool use_top;
  3829. + int ret;
  3830. status_old = read_sr(nor);
  3831. + if (status_old < 0)
  3832. + return status_old;
  3833. - /* SPI NOR always locks to the end */
  3834. - if (ofs + len != mtd->size) {
  3835. - /* Does combined region extend to end? */
  3836. - if (!stm_is_locked_sr(nor, ofs + len, mtd->size - ofs - len,
  3837. - status_old))
  3838. - return -EINVAL;
  3839. - len = mtd->size - ofs;
  3840. - }
  3841. + /* If nothing in our range is unlocked, we don't need to do anything */
  3842. + if (stm_is_locked_sr(nor, ofs, len, status_old))
  3843. + return 0;
  3844. +
  3845. + /* If anything below us is unlocked, we can't use 'bottom' protection */
  3846. + if (!stm_is_locked_sr(nor, 0, ofs, status_old))
  3847. + can_be_bottom = false;
  3848. +
  3849. + /* If anything above us is unlocked, we can't use 'top' protection */
  3850. + if (!stm_is_locked_sr(nor, ofs + len, mtd->size - (ofs + len),
  3851. + status_old))
  3852. + can_be_top = false;
  3853. +
  3854. + if (!can_be_bottom && !can_be_top)
  3855. + return -EINVAL;
  3856. +
  3857. + /* Prefer top, if both are valid */
  3858. + use_top = can_be_top;
  3859. +
  3860. + /* lock_len: length of region that should end up locked */
  3861. + if (use_top)
  3862. + lock_len = mtd->size - ofs;
  3863. + else
  3864. + lock_len = ofs + len;
  3865. /*
  3866. * Need smallest pow such that:
  3867. @@ -484,7 +563,7 @@ static int stm_lock(struct spi_nor *nor,
  3868. *
  3869. * pow = ceil(log2(size / len)) = log2(size) - floor(log2(len))
  3870. */
  3871. - pow = ilog2(mtd->size) - ilog2(len);
  3872. + pow = ilog2(mtd->size) - ilog2(lock_len);
  3873. val = mask - (pow << shift);
  3874. if (val & ~mask)
  3875. return -EINVAL;
  3876. @@ -492,14 +571,27 @@ static int stm_lock(struct spi_nor *nor,
  3877. if (!(val & mask))
  3878. return -EINVAL;
  3879. - status_new = (status_old & ~mask) | val;
  3880. + status_new = (status_old & ~mask & ~SR_TB) | val;
  3881. +
  3882. + /* Disallow further writes if WP pin is asserted */
  3883. + status_new |= SR_SRWD;
  3884. +
  3885. + if (!use_top)
  3886. + status_new |= SR_TB;
  3887. +
  3888. + /* Don't bother if they're the same */
  3889. + if (status_new == status_old)
  3890. + return 0;
  3891. /* Only modify protection if it will not unlock other areas */
  3892. - if ((status_new & mask) <= (status_old & mask))
  3893. + if ((status_new & mask) < (status_old & mask))
  3894. return -EINVAL;
  3895. write_enable(nor);
  3896. - return write_sr(nor, status_new);
  3897. + ret = write_sr(nor, status_new);
  3898. + if (ret)
  3899. + return ret;
  3900. + return spi_nor_wait_till_ready(nor);
  3901. }
  3902. /*
  3903. @@ -510,17 +602,43 @@ static int stm_lock(struct spi_nor *nor,
  3904. static int stm_unlock(struct spi_nor *nor, loff_t ofs, uint64_t len)
  3905. {
  3906. struct mtd_info *mtd = &nor->mtd;
  3907. - uint8_t status_old, status_new;
  3908. + int status_old, status_new;
  3909. u8 mask = SR_BP2 | SR_BP1 | SR_BP0;
  3910. u8 shift = ffs(mask) - 1, pow, val;
  3911. + loff_t lock_len;
  3912. + bool can_be_top = true, can_be_bottom = nor->flags & SNOR_F_HAS_SR_TB;
  3913. + bool use_top;
  3914. + int ret;
  3915. status_old = read_sr(nor);
  3916. + if (status_old < 0)
  3917. + return status_old;
  3918. +
  3919. + /* If nothing in our range is locked, we don't need to do anything */
  3920. + if (stm_is_unlocked_sr(nor, ofs, len, status_old))
  3921. + return 0;
  3922. +
  3923. + /* If anything below us is locked, we can't use 'top' protection */
  3924. + if (!stm_is_unlocked_sr(nor, 0, ofs, status_old))
  3925. + can_be_top = false;
  3926. +
  3927. + /* If anything above us is locked, we can't use 'bottom' protection */
  3928. + if (!stm_is_unlocked_sr(nor, ofs + len, mtd->size - (ofs + len),
  3929. + status_old))
  3930. + can_be_bottom = false;
  3931. - /* Cannot unlock; would unlock larger region than requested */
  3932. - if (stm_is_locked_sr(nor, ofs - mtd->erasesize, mtd->erasesize,
  3933. - status_old))
  3934. + if (!can_be_bottom && !can_be_top)
  3935. return -EINVAL;
  3936. + /* Prefer top, if both are valid */
  3937. + use_top = can_be_top;
  3938. +
  3939. + /* lock_len: length of region that should remain locked */
  3940. + if (use_top)
  3941. + lock_len = mtd->size - (ofs + len);
  3942. + else
  3943. + lock_len = ofs;
  3944. +
  3945. /*
  3946. * Need largest pow such that:
  3947. *
  3948. @@ -530,8 +648,8 @@ static int stm_unlock(struct spi_nor *no
  3949. *
  3950. * pow = floor(log2(size / len)) = log2(size) - ceil(log2(len))
  3951. */
  3952. - pow = ilog2(mtd->size) - order_base_2(mtd->size - (ofs + len));
  3953. - if (ofs + len == mtd->size) {
  3954. + pow = ilog2(mtd->size) - order_base_2(lock_len);
  3955. + if (lock_len == 0) {
  3956. val = 0; /* fully unlocked */
  3957. } else {
  3958. val = mask - (pow << shift);
  3959. @@ -540,14 +658,28 @@ static int stm_unlock(struct spi_nor *no
  3960. return -EINVAL;
  3961. }
  3962. - status_new = (status_old & ~mask) | val;
  3963. + status_new = (status_old & ~mask & ~SR_TB) | val;
  3964. +
  3965. + /* Don't protect status register if we're fully unlocked */
  3966. + if (lock_len == mtd->size)
  3967. + status_new &= ~SR_SRWD;
  3968. +
  3969. + if (!use_top)
  3970. + status_new |= SR_TB;
  3971. +
  3972. + /* Don't bother if they're the same */
  3973. + if (status_new == status_old)
  3974. + return 0;
  3975. /* Only modify protection if it will not lock other areas */
  3976. - if ((status_new & mask) >= (status_old & mask))
  3977. + if ((status_new & mask) > (status_old & mask))
  3978. return -EINVAL;
  3979. write_enable(nor);
  3980. - return write_sr(nor, status_new);
  3981. + ret = write_sr(nor, status_new);
  3982. + if (ret)
  3983. + return ret;
  3984. + return spi_nor_wait_till_ready(nor);
  3985. }
  3986. /*
  3987. @@ -743,8 +875,8 @@ static const struct flash_info spi_nor_i
  3988. { "n25q032a", INFO(0x20bb16, 0, 64 * 1024, 64, SPI_NOR_QUAD_READ) },
  3989. { "n25q064", INFO(0x20ba17, 0, 64 * 1024, 128, SECT_4K | SPI_NOR_QUAD_READ) },
  3990. { "n25q064a", INFO(0x20bb17, 0, 64 * 1024, 128, SECT_4K | SPI_NOR_QUAD_READ) },
  3991. - { "n25q128a11", INFO(0x20bb18, 0, 64 * 1024, 256, SPI_NOR_QUAD_READ) },
  3992. - { "n25q128a13", INFO(0x20ba18, 0, 64 * 1024, 256, SPI_NOR_QUAD_READ) },
  3993. + { "n25q128a11", INFO(0x20bb18, 0, 64 * 1024, 256, SECT_4K | SPI_NOR_QUAD_READ) },
  3994. + { "n25q128a13", INFO(0x20ba18, 0, 64 * 1024, 256, SECT_4K | SPI_NOR_QUAD_READ) },
  3995. { "n25q256a", INFO(0x20ba19, 0, 64 * 1024, 512, SECT_4K | SPI_NOR_QUAD_READ) },
  3996. { "n25q512a", INFO(0x20bb20, 0, 64 * 1024, 1024, SECT_4K | USE_FSR | SPI_NOR_QUAD_READ) },
  3997. { "n25q512ax3", INFO(0x20ba20, 0, 64 * 1024, 1024, SECT_4K | USE_FSR | SPI_NOR_QUAD_READ) },
  3998. @@ -778,6 +910,7 @@ static const struct flash_info spi_nor_i
  3999. { "s25fl008k", INFO(0xef4014, 0, 64 * 1024, 16, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
  4000. { "s25fl016k", INFO(0xef4015, 0, 64 * 1024, 32, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
  4001. { "s25fl064k", INFO(0xef4017, 0, 64 * 1024, 128, SECT_4K) },
  4002. + { "s25fl116k", INFO(0x014015, 0, 64 * 1024, 32, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
  4003. { "s25fl132k", INFO(0x014016, 0, 64 * 1024, 64, SECT_4K) },
  4004. { "s25fl164k", INFO(0x014017, 0, 64 * 1024, 128, SECT_4K) },
  4005. { "s25fl204k", INFO(0x014013, 0, 64 * 1024, 8, SECT_4K | SPI_NOR_DUAL_READ) },
  4006. @@ -841,11 +974,23 @@ static const struct flash_info spi_nor_i
  4007. { "w25x16", INFO(0xef3015, 0, 64 * 1024, 32, SECT_4K) },
  4008. { "w25x32", INFO(0xef3016, 0, 64 * 1024, 64, SECT_4K) },
  4009. { "w25q32", INFO(0xef4016, 0, 64 * 1024, 64, SECT_4K) },
  4010. - { "w25q32dw", INFO(0xef6016, 0, 64 * 1024, 64, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
  4011. + {
  4012. + "w25q32dw", INFO(0xef6016, 0, 64 * 1024, 64,
  4013. + SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
  4014. + SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB)
  4015. + },
  4016. { "w25x64", INFO(0xef3017, 0, 64 * 1024, 128, SECT_4K) },
  4017. { "w25q64", INFO(0xef4017, 0, 64 * 1024, 128, SECT_4K) },
  4018. - { "w25q64dw", INFO(0xef6017, 0, 64 * 1024, 128, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
  4019. - { "w25q128fw", INFO(0xef6018, 0, 64 * 1024, 256, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
  4020. + {
  4021. + "w25q64dw", INFO(0xef6017, 0, 64 * 1024, 128,
  4022. + SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
  4023. + SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB)
  4024. + },
  4025. + {
  4026. + "w25q128fw", INFO(0xef6018, 0, 64 * 1024, 256,
  4027. + SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
  4028. + SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB)
  4029. + },
  4030. { "w25q80", INFO(0xef5014, 0, 64 * 1024, 16, SECT_4K) },
  4031. { "w25q80bl", INFO(0xef4014, 0, 64 * 1024, 16, SECT_4K) },
  4032. { "w25q128", INFO(0xef4018, 0, 64 * 1024, 256, SECT_4K) },
  4033. @@ -868,7 +1013,7 @@ static const struct flash_info *spi_nor_
  4034. tmp = nor->read_reg(nor, SPINOR_OP_RDID, id, SPI_NOR_MAX_ID_LEN);
  4035. if (tmp < 0) {
  4036. - dev_dbg(nor->dev, " error %d reading JEDEC ID\n", tmp);
  4037. + dev_dbg(nor->dev, "error %d reading JEDEC ID\n", tmp);
  4038. return ERR_PTR(tmp);
  4039. }
  4040. @@ -879,7 +1024,7 @@ static const struct flash_info *spi_nor_
  4041. return &spi_nor_ids[tmp];
  4042. }
  4043. }
  4044. - dev_err(nor->dev, "unrecognized JEDEC id bytes: %02x, %2x, %2x\n",
  4045. + dev_err(nor->dev, "unrecognized JEDEC id bytes: %02x, %02x, %02x\n",
  4046. id[0], id[1], id[2]);
  4047. return ERR_PTR(-ENODEV);
  4048. }
  4049. @@ -1025,6 +1170,8 @@ static int macronix_quad_enable(struct s
  4050. int ret, val;
  4051. val = read_sr(nor);
  4052. + if (val < 0)
  4053. + return val;
  4054. write_enable(nor);
  4055. write_sr(nor, val | SR_QUAD_EN_MX);
  4056. @@ -1113,7 +1260,7 @@ static int set_quad_mode(struct spi_nor
  4057. static int spi_nor_check(struct spi_nor *nor)
  4058. {
  4059. if (!nor->dev || !nor->read || !nor->write ||
  4060. - !nor->read_reg || !nor->write_reg || !nor->erase) {
  4061. + !nor->read_reg || !nor->write_reg) {
  4062. pr_err("spi-nor: please fill all the necessary fields!\n");
  4063. return -EINVAL;
  4064. }
  4065. @@ -1126,7 +1273,7 @@ int spi_nor_scan(struct spi_nor *nor, co
  4066. const struct flash_info *info = NULL;
  4067. struct device *dev = nor->dev;
  4068. struct mtd_info *mtd = &nor->mtd;
  4069. - struct device_node *np = nor->flash_node;
  4070. + struct device_node *np = spi_nor_get_flash_node(nor);
  4071. int ret;
  4072. int i;
  4073. @@ -1180,6 +1327,7 @@ int spi_nor_scan(struct spi_nor *nor, co
  4074. info->flags & SPI_NOR_HAS_LOCK) {
  4075. write_enable(nor);
  4076. write_sr(nor, 0);
  4077. + spi_nor_wait_till_ready(nor);
  4078. }
  4079. if (!mtd->name)
  4080. @@ -1214,6 +1362,8 @@ int spi_nor_scan(struct spi_nor *nor, co
  4081. if (info->flags & USE_FSR)
  4082. nor->flags |= SNOR_F_USE_FSR;
  4083. + if (info->flags & SPI_NOR_HAS_TB)
  4084. + nor->flags |= SNOR_F_HAS_SR_TB;
  4085. #ifdef CONFIG_MTD_SPI_NOR_USE_4K_SECTORS
  4086. /* prefer "small sector" erase if possible */
  4087. @@ -1316,6 +1466,12 @@ int spi_nor_scan(struct spi_nor *nor, co
  4088. nor->addr_width = 3;
  4089. }
  4090. + if (nor->addr_width > SPI_NOR_MAX_ADDR_WIDTH) {
  4091. + dev_err(dev, "address width is too large: %u\n",
  4092. + nor->addr_width);
  4093. + return -EINVAL;
  4094. + }
  4095. +
  4096. nor->read_dummy = spi_nor_read_dummy_cycles(nor);
  4097. dev_info(dev, "%s (%lld Kbytes)\n", info->name,
  4098. --- a/drivers/mtd/tests/mtd_nandecctest.c
  4099. +++ b/drivers/mtd/tests/mtd_nandecctest.c
  4100. @@ -187,7 +187,7 @@ static int double_bit_error_detect(void
  4101. __nand_calculate_ecc(error_data, size, calc_ecc);
  4102. ret = __nand_correct_data(error_data, error_ecc, calc_ecc, size);
  4103. - return (ret == -1) ? 0 : -EINVAL;
  4104. + return (ret == -EBADMSG) ? 0 : -EINVAL;
  4105. }
  4106. static const struct nand_ecc_test nand_ecc_test[] = {
  4107. --- a/drivers/mtd/tests/oobtest.c
  4108. +++ b/drivers/mtd/tests/oobtest.c
  4109. @@ -215,19 +215,19 @@ static int verify_eraseblock(int ebnum)
  4110. pr_info("ignoring error as within bitflip_limit\n");
  4111. }
  4112. - if (use_offset != 0 || use_len < mtd->ecclayout->oobavail) {
  4113. + if (use_offset != 0 || use_len < mtd->oobavail) {
  4114. int k;
  4115. ops.mode = MTD_OPS_AUTO_OOB;
  4116. ops.len = 0;
  4117. ops.retlen = 0;
  4118. - ops.ooblen = mtd->ecclayout->oobavail;
  4119. + ops.ooblen = mtd->oobavail;
  4120. ops.oobretlen = 0;
  4121. ops.ooboffs = 0;
  4122. ops.datbuf = NULL;
  4123. ops.oobbuf = readbuf;
  4124. err = mtd_read_oob(mtd, addr, &ops);
  4125. - if (err || ops.oobretlen != mtd->ecclayout->oobavail) {
  4126. + if (err || ops.oobretlen != mtd->oobavail) {
  4127. pr_err("error: readoob failed at %#llx\n",
  4128. (long long)addr);
  4129. errcnt += 1;
  4130. @@ -244,7 +244,7 @@ static int verify_eraseblock(int ebnum)
  4131. /* verify post-(use_offset + use_len) area for 0xff */
  4132. k = use_offset + use_len;
  4133. bitflips += memffshow(addr, k, readbuf + k,
  4134. - mtd->ecclayout->oobavail - k);
  4135. + mtd->oobavail - k);
  4136. if (bitflips > bitflip_limit) {
  4137. pr_err("error: verify failed at %#llx\n",
  4138. @@ -269,8 +269,8 @@ static int verify_eraseblock_in_one_go(i
  4139. struct mtd_oob_ops ops;
  4140. int err = 0;
  4141. loff_t addr = (loff_t)ebnum * mtd->erasesize;
  4142. - size_t len = mtd->ecclayout->oobavail * pgcnt;
  4143. - size_t oobavail = mtd->ecclayout->oobavail;
  4144. + size_t len = mtd->oobavail * pgcnt;
  4145. + size_t oobavail = mtd->oobavail;
  4146. size_t bitflips;
  4147. int i;
  4148. @@ -394,8 +394,8 @@ static int __init mtd_oobtest_init(void)
  4149. goto out;
  4150. use_offset = 0;
  4151. - use_len = mtd->ecclayout->oobavail;
  4152. - use_len_max = mtd->ecclayout->oobavail;
  4153. + use_len = mtd->oobavail;
  4154. + use_len_max = mtd->oobavail;
  4155. vary_offset = 0;
  4156. /* First test: write all OOB, read it back and verify */
  4157. @@ -460,8 +460,8 @@ static int __init mtd_oobtest_init(void)
  4158. /* Write all eraseblocks */
  4159. use_offset = 0;
  4160. - use_len = mtd->ecclayout->oobavail;
  4161. - use_len_max = mtd->ecclayout->oobavail;
  4162. + use_len = mtd->oobavail;
  4163. + use_len_max = mtd->oobavail;
  4164. vary_offset = 1;
  4165. prandom_seed_state(&rnd_state, 5);
  4166. @@ -471,8 +471,8 @@ static int __init mtd_oobtest_init(void)
  4167. /* Check all eraseblocks */
  4168. use_offset = 0;
  4169. - use_len = mtd->ecclayout->oobavail;
  4170. - use_len_max = mtd->ecclayout->oobavail;
  4171. + use_len = mtd->oobavail;
  4172. + use_len_max = mtd->oobavail;
  4173. vary_offset = 1;
  4174. prandom_seed_state(&rnd_state, 5);
  4175. err = verify_all_eraseblocks();
  4176. @@ -480,8 +480,8 @@ static int __init mtd_oobtest_init(void)
  4177. goto out;
  4178. use_offset = 0;
  4179. - use_len = mtd->ecclayout->oobavail;
  4180. - use_len_max = mtd->ecclayout->oobavail;
  4181. + use_len = mtd->oobavail;
  4182. + use_len_max = mtd->oobavail;
  4183. vary_offset = 0;
  4184. /* Fourth test: try to write off end of device */
  4185. @@ -501,7 +501,7 @@ static int __init mtd_oobtest_init(void)
  4186. ops.retlen = 0;
  4187. ops.ooblen = 1;
  4188. ops.oobretlen = 0;
  4189. - ops.ooboffs = mtd->ecclayout->oobavail;
  4190. + ops.ooboffs = mtd->oobavail;
  4191. ops.datbuf = NULL;
  4192. ops.oobbuf = writebuf;
  4193. pr_info("attempting to start write past end of OOB\n");
  4194. @@ -521,7 +521,7 @@ static int __init mtd_oobtest_init(void)
  4195. ops.retlen = 0;
  4196. ops.ooblen = 1;
  4197. ops.oobretlen = 0;
  4198. - ops.ooboffs = mtd->ecclayout->oobavail;
  4199. + ops.ooboffs = mtd->oobavail;
  4200. ops.datbuf = NULL;
  4201. ops.oobbuf = readbuf;
  4202. pr_info("attempting to start read past end of OOB\n");
  4203. @@ -543,7 +543,7 @@ static int __init mtd_oobtest_init(void)
  4204. ops.mode = MTD_OPS_AUTO_OOB;
  4205. ops.len = 0;
  4206. ops.retlen = 0;
  4207. - ops.ooblen = mtd->ecclayout->oobavail + 1;
  4208. + ops.ooblen = mtd->oobavail + 1;
  4209. ops.oobretlen = 0;
  4210. ops.ooboffs = 0;
  4211. ops.datbuf = NULL;
  4212. @@ -563,7 +563,7 @@ static int __init mtd_oobtest_init(void)
  4213. ops.mode = MTD_OPS_AUTO_OOB;
  4214. ops.len = 0;
  4215. ops.retlen = 0;
  4216. - ops.ooblen = mtd->ecclayout->oobavail + 1;
  4217. + ops.ooblen = mtd->oobavail + 1;
  4218. ops.oobretlen = 0;
  4219. ops.ooboffs = 0;
  4220. ops.datbuf = NULL;
  4221. @@ -587,7 +587,7 @@ static int __init mtd_oobtest_init(void)
  4222. ops.mode = MTD_OPS_AUTO_OOB;
  4223. ops.len = 0;
  4224. ops.retlen = 0;
  4225. - ops.ooblen = mtd->ecclayout->oobavail;
  4226. + ops.ooblen = mtd->oobavail;
  4227. ops.oobretlen = 0;
  4228. ops.ooboffs = 1;
  4229. ops.datbuf = NULL;
  4230. @@ -607,7 +607,7 @@ static int __init mtd_oobtest_init(void)
  4231. ops.mode = MTD_OPS_AUTO_OOB;
  4232. ops.len = 0;
  4233. ops.retlen = 0;
  4234. - ops.ooblen = mtd->ecclayout->oobavail;
  4235. + ops.ooblen = mtd->oobavail;
  4236. ops.oobretlen = 0;
  4237. ops.ooboffs = 1;
  4238. ops.datbuf = NULL;
  4239. @@ -638,7 +638,7 @@ static int __init mtd_oobtest_init(void)
  4240. for (i = 0; i < ebcnt - 1; ++i) {
  4241. int cnt = 2;
  4242. int pg;
  4243. - size_t sz = mtd->ecclayout->oobavail;
  4244. + size_t sz = mtd->oobavail;
  4245. if (bbt[i] || bbt[i + 1])
  4246. continue;
  4247. addr = (loff_t)(i + 1) * mtd->erasesize - mtd->writesize;
  4248. @@ -673,13 +673,12 @@ static int __init mtd_oobtest_init(void)
  4249. for (i = 0; i < ebcnt - 1; ++i) {
  4250. if (bbt[i] || bbt[i + 1])
  4251. continue;
  4252. - prandom_bytes_state(&rnd_state, writebuf,
  4253. - mtd->ecclayout->oobavail * 2);
  4254. + prandom_bytes_state(&rnd_state, writebuf, mtd->oobavail * 2);
  4255. addr = (loff_t)(i + 1) * mtd->erasesize - mtd->writesize;
  4256. ops.mode = MTD_OPS_AUTO_OOB;
  4257. ops.len = 0;
  4258. ops.retlen = 0;
  4259. - ops.ooblen = mtd->ecclayout->oobavail * 2;
  4260. + ops.ooblen = mtd->oobavail * 2;
  4261. ops.oobretlen = 0;
  4262. ops.ooboffs = 0;
  4263. ops.datbuf = NULL;
  4264. @@ -688,7 +687,7 @@ static int __init mtd_oobtest_init(void)
  4265. if (err)
  4266. goto out;
  4267. if (memcmpshow(addr, readbuf, writebuf,
  4268. - mtd->ecclayout->oobavail * 2)) {
  4269. + mtd->oobavail * 2)) {
  4270. pr_err("error: verify failed at %#llx\n",
  4271. (long long)addr);
  4272. errcnt += 1;
  4273. --- a/drivers/mtd/tests/pagetest.c
  4274. +++ b/drivers/mtd/tests/pagetest.c
  4275. @@ -127,13 +127,12 @@ static int crosstest(void)
  4276. unsigned char *pp1, *pp2, *pp3, *pp4;
  4277. pr_info("crosstest\n");
  4278. - pp1 = kmalloc(pgsize * 4, GFP_KERNEL);
  4279. + pp1 = kzalloc(pgsize * 4, GFP_KERNEL);
  4280. if (!pp1)
  4281. return -ENOMEM;
  4282. pp2 = pp1 + pgsize;
  4283. pp3 = pp2 + pgsize;
  4284. pp4 = pp3 + pgsize;
  4285. - memset(pp1, 0, pgsize * 4);
  4286. addr0 = 0;
  4287. for (i = 0; i < ebcnt && bbt[i]; ++i)
  4288. --- a/include/linux/mtd/bbm.h
  4289. +++ b/include/linux/mtd/bbm.h
  4290. @@ -166,7 +166,6 @@ struct bbm_info {
  4291. };
  4292. /* OneNAND BBT interface */
  4293. -extern int onenand_scan_bbt(struct mtd_info *mtd, struct nand_bbt_descr *bd);
  4294. extern int onenand_default_bbt(struct mtd_info *mtd);
  4295. #endif /* __LINUX_MTD_BBM_H */
  4296. --- a/include/linux/mtd/fsmc.h
  4297. +++ b/include/linux/mtd/fsmc.h
  4298. @@ -103,24 +103,6 @@
  4299. #define FSMC_BUSY_WAIT_TIMEOUT (1 * HZ)
  4300. -/*
  4301. - * There are 13 bytes of ecc for every 512 byte block in FSMC version 8
  4302. - * and it has to be read consecutively and immediately after the 512
  4303. - * byte data block for hardware to generate the error bit offsets
  4304. - * Managing the ecc bytes in the following way is easier. This way is
  4305. - * similar to oobfree structure maintained already in u-boot nand driver
  4306. - */
  4307. -#define MAX_ECCPLACE_ENTRIES 32
  4308. -
  4309. -struct fsmc_nand_eccplace {
  4310. - uint8_t offset;
  4311. - uint8_t length;
  4312. -};
  4313. -
  4314. -struct fsmc_eccplace {
  4315. - struct fsmc_nand_eccplace eccplace[MAX_ECCPLACE_ENTRIES];
  4316. -};
  4317. -
  4318. struct fsmc_nand_timings {
  4319. uint8_t tclr;
  4320. uint8_t tar;
  4321. --- a/include/linux/mtd/inftl.h
  4322. +++ b/include/linux/mtd/inftl.h
  4323. @@ -44,7 +44,6 @@ struct INFTLrecord {
  4324. unsigned int nb_blocks; /* number of physical blocks */
  4325. unsigned int nb_boot_blocks; /* number of blocks used by the bios */
  4326. struct erase_info instr;
  4327. - struct nand_ecclayout oobinfo;
  4328. };
  4329. int INFTL_mount(struct INFTLrecord *s);
  4330. --- a/include/linux/mtd/map.h
  4331. +++ b/include/linux/mtd/map.h
  4332. @@ -137,7 +137,9 @@
  4333. #endif
  4334. #ifndef map_bankwidth
  4335. +#ifdef CONFIG_MTD
  4336. #warning "No CONFIG_MTD_MAP_BANK_WIDTH_xx selected. No NOR chip support can work"
  4337. +#endif
  4338. static inline int map_bankwidth(void *map)
  4339. {
  4340. BUG();
  4341. @@ -233,8 +235,11 @@ struct map_info {
  4342. If there is no cache to care about this can be set to NULL. */
  4343. void (*inval_cache)(struct map_info *, unsigned long, ssize_t);
  4344. - /* set_vpp() must handle being reentered -- enable, enable, disable
  4345. - must leave it enabled. */
  4346. + /* This will be called with 1 as parameter when the first map user
  4347. + * needs VPP, and called with 0 when the last user exits. The map
  4348. + * core maintains a reference counter, and assumes that VPP is a
  4349. + * global resource applying to all mapped flash chips on the system.
  4350. + */
  4351. void (*set_vpp)(struct map_info *, int);
  4352. unsigned long pfow_base;
  4353. --- a/include/linux/mtd/mtd.h
  4354. +++ b/include/linux/mtd/mtd.h
  4355. @@ -100,17 +100,35 @@ struct mtd_oob_ops {
  4356. #define MTD_MAX_OOBFREE_ENTRIES_LARGE 32
  4357. #define MTD_MAX_ECCPOS_ENTRIES_LARGE 640
  4358. +/**
  4359. + * struct mtd_oob_region - oob region definition
  4360. + * @offset: region offset
  4361. + * @length: region length
  4362. + *
  4363. + * This structure describes a region of the OOB area, and is used
  4364. + * to retrieve ECC or free bytes sections.
  4365. + * Each section is defined by an offset within the OOB area and a
  4366. + * length.
  4367. + */
  4368. +struct mtd_oob_region {
  4369. + u32 offset;
  4370. + u32 length;
  4371. +};
  4372. +
  4373. /*
  4374. - * Internal ECC layout control structure. For historical reasons, there is a
  4375. - * similar, smaller struct nand_ecclayout_user (in mtd-abi.h) that is retained
  4376. - * for export to user-space via the ECCGETLAYOUT ioctl.
  4377. - * nand_ecclayout should be expandable in the future simply by the above macros.
  4378. + * struct mtd_ooblayout_ops - NAND OOB layout operations
  4379. + * @ecc: function returning an ECC region in the OOB area.
  4380. + * Should return -ERANGE if %section exceeds the total number of
  4381. + * ECC sections.
  4382. + * @free: function returning a free region in the OOB area.
  4383. + * Should return -ERANGE if %section exceeds the total number of
  4384. + * free sections.
  4385. */
  4386. -struct nand_ecclayout {
  4387. - __u32 eccbytes;
  4388. - __u32 eccpos[MTD_MAX_ECCPOS_ENTRIES_LARGE];
  4389. - __u32 oobavail;
  4390. - struct nand_oobfree oobfree[MTD_MAX_OOBFREE_ENTRIES_LARGE];
  4391. +struct mtd_ooblayout_ops {
  4392. + int (*ecc)(struct mtd_info *mtd, int section,
  4393. + struct mtd_oob_region *oobecc);
  4394. + int (*free)(struct mtd_info *mtd, int section,
  4395. + struct mtd_oob_region *oobfree);
  4396. };
  4397. struct module; /* only needed for owner field in mtd_info */
  4398. @@ -171,8 +189,8 @@ struct mtd_info {
  4399. const char *name;
  4400. int index;
  4401. - /* ECC layout structure pointer - read only! */
  4402. - struct nand_ecclayout *ecclayout;
  4403. + /* OOB layout description */
  4404. + const struct mtd_ooblayout_ops *ooblayout;
  4405. /* the ecc step size. */
  4406. unsigned int ecc_step_size;
  4407. @@ -258,6 +276,46 @@ struct mtd_info {
  4408. int usecount;
  4409. };
  4410. +int mtd_ooblayout_ecc(struct mtd_info *mtd, int section,
  4411. + struct mtd_oob_region *oobecc);
  4412. +int mtd_ooblayout_find_eccregion(struct mtd_info *mtd, int eccbyte,
  4413. + int *section,
  4414. + struct mtd_oob_region *oobregion);
  4415. +int mtd_ooblayout_get_eccbytes(struct mtd_info *mtd, u8 *eccbuf,
  4416. + const u8 *oobbuf, int start, int nbytes);
  4417. +int mtd_ooblayout_set_eccbytes(struct mtd_info *mtd, const u8 *eccbuf,
  4418. + u8 *oobbuf, int start, int nbytes);
  4419. +int mtd_ooblayout_free(struct mtd_info *mtd, int section,
  4420. + struct mtd_oob_region *oobfree);
  4421. +int mtd_ooblayout_get_databytes(struct mtd_info *mtd, u8 *databuf,
  4422. + const u8 *oobbuf, int start, int nbytes);
  4423. +int mtd_ooblayout_set_databytes(struct mtd_info *mtd, const u8 *databuf,
  4424. + u8 *oobbuf, int start, int nbytes);
  4425. +int mtd_ooblayout_count_freebytes(struct mtd_info *mtd);
  4426. +int mtd_ooblayout_count_eccbytes(struct mtd_info *mtd);
  4427. +
  4428. +static inline void mtd_set_ooblayout(struct mtd_info *mtd,
  4429. + const struct mtd_ooblayout_ops *ooblayout)
  4430. +{
  4431. + mtd->ooblayout = ooblayout;
  4432. +}
  4433. +
  4434. +static inline void mtd_set_of_node(struct mtd_info *mtd,
  4435. + struct device_node *np)
  4436. +{
  4437. + mtd->dev.of_node = np;
  4438. +}
  4439. +
  4440. +static inline struct device_node *mtd_get_of_node(struct mtd_info *mtd)
  4441. +{
  4442. + return mtd->dev.of_node;
  4443. +}
  4444. +
  4445. +static inline int mtd_oobavail(struct mtd_info *mtd, struct mtd_oob_ops *ops)
  4446. +{
  4447. + return ops->mode == MTD_OPS_AUTO_OOB ? mtd->oobavail : mtd->oobsize;
  4448. +}
  4449. +
  4450. int mtd_erase(struct mtd_info *mtd, struct erase_info *instr);
  4451. int mtd_point(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen,
  4452. void **virt, resource_size_t *phys);
  4453. --- a/include/linux/mtd/nand.h
  4454. +++ b/include/linux/mtd/nand.h
  4455. @@ -119,6 +119,12 @@ typedef enum {
  4456. NAND_ECC_SOFT_BCH,
  4457. } nand_ecc_modes_t;
  4458. +enum nand_ecc_algo {
  4459. + NAND_ECC_UNKNOWN,
  4460. + NAND_ECC_HAMMING,
  4461. + NAND_ECC_BCH,
  4462. +};
  4463. +
  4464. /*
  4465. * Constants for Hardware ECC
  4466. */
  4467. @@ -129,6 +135,14 @@ typedef enum {
  4468. /* Enable Hardware ECC before syndrome is read back from flash */
  4469. #define NAND_ECC_READSYN 2
  4470. +/*
  4471. + * Enable generic NAND 'page erased' check. This check is only done when
  4472. + * ecc.correct() returns -EBADMSG.
  4473. + * Set this flag if your implementation does not fix bitflips in erased
  4474. + * pages and you want to rely on the default implementation.
  4475. + */
  4476. +#define NAND_ECC_GENERIC_ERASED_CHECK BIT(0)
  4477. +
  4478. /* Bit mask for flags passed to do_nand_read_ecc */
  4479. #define NAND_GET_DEVICE 0x80
  4480. @@ -160,6 +174,12 @@ typedef enum {
  4481. /* Device supports subpage reads */
  4482. #define NAND_SUBPAGE_READ 0x00001000
  4483. +/*
  4484. + * Some MLC NANDs need data scrambling to limit bitflips caused by repeated
  4485. + * patterns.
  4486. + */
  4487. +#define NAND_NEED_SCRAMBLING 0x00002000
  4488. +
  4489. /* Options valid for Samsung large page devices */
  4490. #define NAND_SAMSUNG_LP_OPTIONS NAND_CACHEPRG
  4491. @@ -276,15 +296,15 @@ struct nand_onfi_params {
  4492. __le16 t_r;
  4493. __le16 t_ccs;
  4494. __le16 src_sync_timing_mode;
  4495. - __le16 src_ssync_features;
  4496. + u8 src_ssync_features;
  4497. __le16 clk_pin_capacitance_typ;
  4498. __le16 io_pin_capacitance_typ;
  4499. __le16 input_pin_capacitance_typ;
  4500. u8 input_pin_capacitance_max;
  4501. u8 driver_strength_support;
  4502. __le16 t_int_r;
  4503. - __le16 t_ald;
  4504. - u8 reserved4[7];
  4505. + __le16 t_adl;
  4506. + u8 reserved4[8];
  4507. /* vendor */
  4508. __le16 vendor_revision;
  4509. @@ -407,7 +427,7 @@ struct nand_jedec_params {
  4510. __le16 input_pin_capacitance_typ;
  4511. __le16 clk_pin_capacitance_typ;
  4512. u8 driver_strength_support;
  4513. - __le16 t_ald;
  4514. + __le16 t_adl;
  4515. u8 reserved4[36];
  4516. /* ECC and endurance block */
  4517. @@ -444,6 +464,7 @@ struct nand_hw_control {
  4518. /**
  4519. * struct nand_ecc_ctrl - Control structure for ECC
  4520. * @mode: ECC mode
  4521. + * @algo: ECC algorithm
  4522. * @steps: number of ECC steps per page
  4523. * @size: data bytes per ECC step
  4524. * @bytes: ECC bytes per step
  4525. @@ -451,12 +472,18 @@ struct nand_hw_control {
  4526. * @total: total number of ECC bytes per page
  4527. * @prepad: padding information for syndrome based ECC generators
  4528. * @postpad: padding information for syndrome based ECC generators
  4529. - * @layout: ECC layout control struct pointer
  4530. + * @options: ECC specific options (see NAND_ECC_XXX flags defined above)
  4531. * @priv: pointer to private ECC control data
  4532. * @hwctl: function to control hardware ECC generator. Must only
  4533. * be provided if an hardware ECC is available
  4534. * @calculate: function for ECC calculation or readback from ECC hardware
  4535. - * @correct: function for ECC correction, matching to ECC generator (sw/hw)
  4536. + * @correct: function for ECC correction, matching to ECC generator (sw/hw).
  4537. + * Should return a positive number representing the number of
  4538. + * corrected bitflips, -EBADMSG if the number of bitflips exceed
  4539. + * ECC strength, or any other error code if the error is not
  4540. + * directly related to correction.
  4541. + * If -EBADMSG is returned the input buffers should be left
  4542. + * untouched.
  4543. * @read_page_raw: function to read a raw page without ECC. This function
  4544. * should hide the specific layout used by the ECC
  4545. * controller and always return contiguous in-band and
  4546. @@ -487,6 +514,7 @@ struct nand_hw_control {
  4547. */
  4548. struct nand_ecc_ctrl {
  4549. nand_ecc_modes_t mode;
  4550. + enum nand_ecc_algo algo;
  4551. int steps;
  4552. int size;
  4553. int bytes;
  4554. @@ -494,7 +522,7 @@ struct nand_ecc_ctrl {
  4555. int strength;
  4556. int prepad;
  4557. int postpad;
  4558. - struct nand_ecclayout *layout;
  4559. + unsigned int options;
  4560. void *priv;
  4561. void (*hwctl)(struct mtd_info *mtd, int mode);
  4562. int (*calculate)(struct mtd_info *mtd, const uint8_t *dat,
  4563. @@ -540,11 +568,11 @@ struct nand_buffers {
  4564. /**
  4565. * struct nand_chip - NAND Private Flash Chip Data
  4566. + * @mtd: MTD device registered to the MTD framework
  4567. * @IO_ADDR_R: [BOARDSPECIFIC] address to read the 8 I/O lines of the
  4568. * flash device
  4569. * @IO_ADDR_W: [BOARDSPECIFIC] address to write the 8 I/O lines of the
  4570. * flash device.
  4571. - * @flash_node: [BOARDSPECIFIC] device node describing this instance
  4572. * @read_byte: [REPLACEABLE] read one byte from the chip
  4573. * @read_word: [REPLACEABLE] read one word from the chip
  4574. * @write_byte: [REPLACEABLE] write a single byte to the chip on the
  4575. @@ -640,18 +668,17 @@ struct nand_buffers {
  4576. */
  4577. struct nand_chip {
  4578. + struct mtd_info mtd;
  4579. void __iomem *IO_ADDR_R;
  4580. void __iomem *IO_ADDR_W;
  4581. - struct device_node *flash_node;
  4582. -
  4583. uint8_t (*read_byte)(struct mtd_info *mtd);
  4584. u16 (*read_word)(struct mtd_info *mtd);
  4585. void (*write_byte)(struct mtd_info *mtd, uint8_t byte);
  4586. void (*write_buf)(struct mtd_info *mtd, const uint8_t *buf, int len);
  4587. void (*read_buf)(struct mtd_info *mtd, uint8_t *buf, int len);
  4588. void (*select_chip)(struct mtd_info *mtd, int chip);
  4589. - int (*block_bad)(struct mtd_info *mtd, loff_t ofs, int getchip);
  4590. + int (*block_bad)(struct mtd_info *mtd, loff_t ofs);
  4591. int (*block_markbad)(struct mtd_info *mtd, loff_t ofs);
  4592. void (*cmd_ctrl)(struct mtd_info *mtd, int dat, unsigned int ctrl);
  4593. int (*dev_ready)(struct mtd_info *mtd);
  4594. @@ -719,6 +746,40 @@ struct nand_chip {
  4595. void *priv;
  4596. };
  4597. +extern const struct mtd_ooblayout_ops nand_ooblayout_sp_ops;
  4598. +extern const struct mtd_ooblayout_ops nand_ooblayout_lp_ops;
  4599. +
  4600. +static inline void nand_set_flash_node(struct nand_chip *chip,
  4601. + struct device_node *np)
  4602. +{
  4603. + mtd_set_of_node(&chip->mtd, np);
  4604. +}
  4605. +
  4606. +static inline struct device_node *nand_get_flash_node(struct nand_chip *chip)
  4607. +{
  4608. + return mtd_get_of_node(&chip->mtd);
  4609. +}
  4610. +
  4611. +static inline struct nand_chip *mtd_to_nand(struct mtd_info *mtd)
  4612. +{
  4613. + return container_of(mtd, struct nand_chip, mtd);
  4614. +}
  4615. +
  4616. +static inline struct mtd_info *nand_to_mtd(struct nand_chip *chip)
  4617. +{
  4618. + return &chip->mtd;
  4619. +}
  4620. +
  4621. +static inline void *nand_get_controller_data(struct nand_chip *chip)
  4622. +{
  4623. + return chip->priv;
  4624. +}
  4625. +
  4626. +static inline void nand_set_controller_data(struct nand_chip *chip, void *priv)
  4627. +{
  4628. + chip->priv = priv;
  4629. +}
  4630. +
  4631. /*
  4632. * NAND Flash Manufacturer ID Codes
  4633. */
  4634. @@ -850,7 +911,6 @@ extern int nand_do_read(struct mtd_info
  4635. * @chip_delay: R/B delay value in us
  4636. * @options: Option flags, e.g. 16bit buswidth
  4637. * @bbt_options: BBT option flags, e.g. NAND_BBT_USE_FLASH
  4638. - * @ecclayout: ECC layout info structure
  4639. * @part_probe_types: NULL-terminated array of probe types
  4640. */
  4641. struct platform_nand_chip {
  4642. @@ -858,7 +918,6 @@ struct platform_nand_chip {
  4643. int chip_offset;
  4644. int nr_partitions;
  4645. struct mtd_partition *partitions;
  4646. - struct nand_ecclayout *ecclayout;
  4647. int chip_delay;
  4648. unsigned int options;
  4649. unsigned int bbt_options;
  4650. @@ -908,15 +967,6 @@ struct platform_nand_data {
  4651. struct platform_nand_ctrl ctrl;
  4652. };
  4653. -/* Some helpers to access the data structures */
  4654. -static inline
  4655. -struct platform_nand_chip *get_platform_nandchip(struct mtd_info *mtd)
  4656. -{
  4657. - struct nand_chip *chip = mtd->priv;
  4658. -
  4659. - return chip->priv;
  4660. -}
  4661. -
  4662. /* return the supported features. */
  4663. static inline int onfi_feature(struct nand_chip *chip)
  4664. {
  4665. --- a/include/linux/mtd/nand_bch.h
  4666. +++ b/include/linux/mtd/nand_bch.h
  4667. @@ -32,9 +32,7 @@ int nand_bch_correct_data(struct mtd_inf
  4668. /*
  4669. * Initialize BCH encoder/decoder
  4670. */
  4671. -struct nand_bch_control *
  4672. -nand_bch_init(struct mtd_info *mtd, unsigned int eccsize,
  4673. - unsigned int eccbytes, struct nand_ecclayout **ecclayout);
  4674. +struct nand_bch_control *nand_bch_init(struct mtd_info *mtd);
  4675. /*
  4676. * Release BCH encoder/decoder resources
  4677. */
  4678. @@ -55,12 +53,10 @@ static inline int
  4679. nand_bch_correct_data(struct mtd_info *mtd, unsigned char *buf,
  4680. unsigned char *read_ecc, unsigned char *calc_ecc)
  4681. {
  4682. - return -1;
  4683. + return -ENOTSUPP;
  4684. }
  4685. -static inline struct nand_bch_control *
  4686. -nand_bch_init(struct mtd_info *mtd, unsigned int eccsize,
  4687. - unsigned int eccbytes, struct nand_ecclayout **ecclayout)
  4688. +static inline struct nand_bch_control *nand_bch_init(struct mtd_info *mtd)
  4689. {
  4690. return NULL;
  4691. }
  4692. --- a/include/linux/mtd/nftl.h
  4693. +++ b/include/linux/mtd/nftl.h
  4694. @@ -50,7 +50,6 @@ struct NFTLrecord {
  4695. unsigned int nb_blocks; /* number of physical blocks */
  4696. unsigned int nb_boot_blocks; /* number of blocks used by the bios */
  4697. struct erase_info instr;
  4698. - struct nand_ecclayout oobinfo;
  4699. };
  4700. int NFTL_mount(struct NFTLrecord *s);
  4701. --- a/include/linux/mtd/onenand.h
  4702. +++ b/include/linux/mtd/onenand.h
  4703. @@ -80,7 +80,6 @@ struct onenand_bufferram {
  4704. * @page_buf: [INTERN] page main data buffer
  4705. * @oob_buf: [INTERN] page oob data buffer
  4706. * @subpagesize: [INTERN] holds the subpagesize
  4707. - * @ecclayout: [REPLACEABLE] the default ecc placement scheme
  4708. * @bbm: [REPLACEABLE] pointer to Bad Block Management
  4709. * @priv: [OPTIONAL] pointer to private chip date
  4710. */
  4711. @@ -134,7 +133,6 @@ struct onenand_chip {
  4712. #endif
  4713. int subpagesize;
  4714. - struct nand_ecclayout *ecclayout;
  4715. void *bbm;
  4716. --- a/include/linux/mtd/partitions.h
  4717. +++ b/include/linux/mtd/partitions.h
  4718. @@ -42,7 +42,6 @@ struct mtd_partition {
  4719. uint64_t size; /* partition size */
  4720. uint64_t offset; /* offset within the master MTD space */
  4721. uint32_t mask_flags; /* master MTD flags to mask out for this partition */
  4722. - struct nand_ecclayout *ecclayout; /* out of band layout for this partition (NAND only) */
  4723. };
  4724. #define MTDPART_OFS_RETAIN (-3)
  4725. @@ -56,11 +55,9 @@ struct device_node;
  4726. /**
  4727. * struct mtd_part_parser_data - used to pass data to MTD partition parsers.
  4728. * @origin: for RedBoot, start address of MTD device
  4729. - * @of_node: for OF parsers, device node containing partitioning information
  4730. */
  4731. struct mtd_part_parser_data {
  4732. unsigned long origin;
  4733. - struct device_node *of_node;
  4734. };
  4735. @@ -78,14 +75,34 @@ struct mtd_part_parser {
  4736. struct list_head list;
  4737. struct module *owner;
  4738. const char *name;
  4739. - int (*parse_fn)(struct mtd_info *, struct mtd_partition **,
  4740. + int (*parse_fn)(struct mtd_info *, const struct mtd_partition **,
  4741. struct mtd_part_parser_data *);
  4742. + void (*cleanup)(const struct mtd_partition *pparts, int nr_parts);
  4743. enum mtd_parser_type type;
  4744. };
  4745. -extern void register_mtd_parser(struct mtd_part_parser *parser);
  4746. +/* Container for passing around a set of parsed partitions */
  4747. +struct mtd_partitions {
  4748. + const struct mtd_partition *parts;
  4749. + int nr_parts;
  4750. + const struct mtd_part_parser *parser;
  4751. +};
  4752. +
  4753. +extern int __register_mtd_parser(struct mtd_part_parser *parser,
  4754. + struct module *owner);
  4755. +#define register_mtd_parser(parser) __register_mtd_parser(parser, THIS_MODULE)
  4756. +
  4757. extern void deregister_mtd_parser(struct mtd_part_parser *parser);
  4758. +/*
  4759. + * module_mtd_part_parser() - Helper macro for MTD partition parsers that don't
  4760. + * do anything special in module init/exit. Each driver may only use this macro
  4761. + * once, and calling it replaces module_init() and module_exit().
  4762. + */
  4763. +#define module_mtd_part_parser(__mtd_part_parser) \
  4764. + module_driver(__mtd_part_parser, register_mtd_parser, \
  4765. + deregister_mtd_parser)
  4766. +
  4767. int mtd_is_partition(const struct mtd_info *mtd);
  4768. int mtd_add_partition(struct mtd_info *master, const char *name,
  4769. long long offset, long long length);
  4770. --- a/include/linux/mtd/sh_flctl.h
  4771. +++ b/include/linux/mtd/sh_flctl.h
  4772. @@ -143,7 +143,6 @@ enum flctl_ecc_res_t {
  4773. struct dma_chan;
  4774. struct sh_flctl {
  4775. - struct mtd_info mtd;
  4776. struct nand_chip chip;
  4777. struct platform_device *pdev;
  4778. struct dev_pm_qos_request pm_qos;
  4779. @@ -187,7 +186,7 @@ struct sh_flctl_platform_data {
  4780. static inline struct sh_flctl *mtd_to_flctl(struct mtd_info *mtdinfo)
  4781. {
  4782. - return container_of(mtdinfo, struct sh_flctl, mtd);
  4783. + return container_of(mtd_to_nand(mtdinfo), struct sh_flctl, chip);
  4784. }
  4785. #endif /* __SH_FLCTL_H__ */
  4786. --- a/include/linux/mtd/sharpsl.h
  4787. +++ b/include/linux/mtd/sharpsl.h
  4788. @@ -14,7 +14,7 @@
  4789. struct sharpsl_nand_platform_data {
  4790. struct nand_bbt_descr *badblock_pattern;
  4791. - struct nand_ecclayout *ecc_layout;
  4792. + const struct mtd_ooblayout_ops *ecc_layout;
  4793. struct mtd_partition *partitions;
  4794. unsigned int nr_partitions;
  4795. };
  4796. --- a/include/uapi/mtd/mtd-abi.h
  4797. +++ b/include/uapi/mtd/mtd-abi.h
  4798. @@ -228,7 +228,7 @@ struct nand_oobfree {
  4799. * complete set of ECC information. The ioctl truncates the larger internal
  4800. * structure to retain binary compatibility with the static declaration of the
  4801. * ioctl. Note that the "MTD_MAX_..._ENTRIES" macros represent the max size of
  4802. - * the user struct, not the MAX size of the internal struct nand_ecclayout.
  4803. + * the user struct, not the MAX size of the internal OOB layout representation.
  4804. */
  4805. struct nand_ecclayout_user {
  4806. __u32 eccbytes;
  4807. --- a/fs/jffs2/wbuf.c
  4808. +++ b/fs/jffs2/wbuf.c
  4809. @@ -1153,7 +1153,7 @@ static struct jffs2_sb_info *work_to_sb(
  4810. {
  4811. struct delayed_work *dwork;
  4812. - dwork = container_of(work, struct delayed_work, work);
  4813. + dwork = to_delayed_work(work);
  4814. return container_of(dwork, struct jffs2_sb_info, wbuf_dwork);
  4815. }
  4816. @@ -1183,22 +1183,20 @@ void jffs2_dirty_trigger(struct jffs2_sb
  4817. int jffs2_nand_flash_setup(struct jffs2_sb_info *c)
  4818. {
  4819. - struct nand_ecclayout *oinfo = c->mtd->ecclayout;
  4820. -
  4821. if (!c->mtd->oobsize)
  4822. return 0;
  4823. /* Cleanmarker is out-of-band, so inline size zero */
  4824. c->cleanmarker_size = 0;
  4825. - if (!oinfo || oinfo->oobavail == 0) {
  4826. + if (c->mtd->oobavail == 0) {
  4827. pr_err("inconsistent device description\n");
  4828. return -EINVAL;
  4829. }
  4830. jffs2_dbg(1, "using OOB on NAND\n");
  4831. - c->oobavail = oinfo->oobavail;
  4832. + c->oobavail = c->mtd->oobavail;
  4833. /* Initialise write buffer */
  4834. init_rwsem(&c->wbuf_sem);
  4835. --- a/include/linux/mtd/spi-nor.h
  4836. +++ b/include/linux/mtd/spi-nor.h
  4837. @@ -85,6 +85,7 @@
  4838. #define SR_BP0 BIT(2) /* Block protect 0 */
  4839. #define SR_BP1 BIT(3) /* Block protect 1 */
  4840. #define SR_BP2 BIT(4) /* Block protect 2 */
  4841. +#define SR_TB BIT(5) /* Top/Bottom protect */
  4842. #define SR_SRWD BIT(7) /* SR write protect */
  4843. #define SR_QUAD_EN_MX BIT(6) /* Macronix Quad I/O */
  4844. @@ -116,6 +117,7 @@ enum spi_nor_ops {
  4845. enum spi_nor_option_flags {
  4846. SNOR_F_USE_FSR = BIT(0),
  4847. + SNOR_F_HAS_SR_TB = BIT(1),
  4848. };
  4849. /**
  4850. @@ -123,7 +125,6 @@ enum spi_nor_option_flags {
  4851. * @mtd: point to a mtd_info structure
  4852. * @lock: the lock for the read/write/erase/lock/unlock operations
  4853. * @dev: point to a spi device, or a spi nor controller device.
  4854. - * @flash_node: point to a device node describing this flash instance.
  4855. * @page_size: the page size of the SPI NOR
  4856. * @addr_width: number of address bytes
  4857. * @erase_opcode: the opcode for erasing a sector
  4858. @@ -143,7 +144,8 @@ enum spi_nor_option_flags {
  4859. * @read: [DRIVER-SPECIFIC] read data from the SPI NOR
  4860. * @write: [DRIVER-SPECIFIC] write data to the SPI NOR
  4861. * @erase: [DRIVER-SPECIFIC] erase a sector of the SPI NOR
  4862. - * at the offset @offs
  4863. + * at the offset @offs; if not provided by the driver,
  4864. + * spi-nor will send the erase opcode via write_reg()
  4865. * @flash_lock: [FLASH-SPECIFIC] lock a region of the SPI NOR
  4866. * @flash_unlock: [FLASH-SPECIFIC] unlock a region of the SPI NOR
  4867. * @flash_is_locked: [FLASH-SPECIFIC] check if a region of the SPI NOR is
  4868. @@ -154,7 +156,6 @@ struct spi_nor {
  4869. struct mtd_info mtd;
  4870. struct mutex lock;
  4871. struct device *dev;
  4872. - struct device_node *flash_node;
  4873. u32 page_size;
  4874. u8 addr_width;
  4875. u8 erase_opcode;
  4876. @@ -184,6 +185,17 @@ struct spi_nor {
  4877. void *priv;
  4878. };
  4879. +static inline void spi_nor_set_flash_node(struct spi_nor *nor,
  4880. + struct device_node *np)
  4881. +{
  4882. + mtd_set_of_node(&nor->mtd, np);
  4883. +}
  4884. +
  4885. +static inline struct device_node *spi_nor_get_flash_node(struct spi_nor *nor)
  4886. +{
  4887. + return mtd_get_of_node(&nor->mtd);
  4888. +}
  4889. +
  4890. /**
  4891. * spi_nor_scan() - scan the SPI NOR
  4892. * @nor: the spi_nor structure