nand_base.c 122 KB

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