nand_base.c 101 KB

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