nand_base.c 120 KB

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