nand_base.c 118 KB

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