nand_base.c 111 KB

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