nand_base.c 128 KB

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