nand_base.c 174 KB

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