nand_base.c 176 KB

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