nand_base.c 131 KB

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