nand_base.c 129 KB

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