nand_base.c 105 KB

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