nand_base.c 97 KB

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