nand_base.c 149 KB

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