nand_base.c 99 KB

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