nand_base.c 93 KB

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