nand_base.c 93 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612
  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. * @page: page number to read
  936. *
  937. * Not for syndrome calculating ECC controllers, which use a special oob layout.
  938. */
  939. static int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
  940. uint8_t *buf, int page)
  941. {
  942. chip->read_buf(mtd, buf, mtd->writesize);
  943. chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
  944. return 0;
  945. }
  946. /**
  947. * nand_read_page_raw_syndrome - [INTERN] read raw page data without ecc
  948. * @mtd: mtd info structure
  949. * @chip: nand chip info structure
  950. * @buf: buffer to store read data
  951. * @page: page number to read
  952. *
  953. * We need a special oob layout and handling even when OOB isn't used.
  954. */
  955. static int nand_read_page_raw_syndrome(struct mtd_info *mtd,
  956. struct nand_chip *chip,
  957. uint8_t *buf, int page)
  958. {
  959. int eccsize = chip->ecc.size;
  960. int eccbytes = chip->ecc.bytes;
  961. uint8_t *oob = chip->oob_poi;
  962. int steps, size;
  963. for (steps = chip->ecc.steps; steps > 0; steps--) {
  964. chip->read_buf(mtd, buf, eccsize);
  965. buf += eccsize;
  966. if (chip->ecc.prepad) {
  967. chip->read_buf(mtd, oob, chip->ecc.prepad);
  968. oob += chip->ecc.prepad;
  969. }
  970. chip->read_buf(mtd, oob, eccbytes);
  971. oob += eccbytes;
  972. if (chip->ecc.postpad) {
  973. chip->read_buf(mtd, oob, chip->ecc.postpad);
  974. oob += chip->ecc.postpad;
  975. }
  976. }
  977. size = mtd->oobsize - (oob - chip->oob_poi);
  978. if (size)
  979. chip->read_buf(mtd, oob, size);
  980. return 0;
  981. }
  982. /**
  983. * nand_read_page_swecc - [REPLACEABLE] software ECC based page read function
  984. * @mtd: mtd info structure
  985. * @chip: nand chip info structure
  986. * @buf: buffer to store read data
  987. * @page: page number to read
  988. */
  989. static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
  990. uint8_t *buf, int page)
  991. {
  992. int i, eccsize = chip->ecc.size;
  993. int eccbytes = chip->ecc.bytes;
  994. int eccsteps = chip->ecc.steps;
  995. uint8_t *p = buf;
  996. uint8_t *ecc_calc = chip->buffers->ecccalc;
  997. uint8_t *ecc_code = chip->buffers->ecccode;
  998. uint32_t *eccpos = chip->ecc.layout->eccpos;
  999. unsigned int max_bitflips = 0;
  1000. chip->ecc.read_page_raw(mtd, chip, buf, page);
  1001. for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
  1002. chip->ecc.calculate(mtd, p, &ecc_calc[i]);
  1003. for (i = 0; i < chip->ecc.total; i++)
  1004. ecc_code[i] = chip->oob_poi[eccpos[i]];
  1005. eccsteps = chip->ecc.steps;
  1006. p = buf;
  1007. for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
  1008. int stat;
  1009. stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
  1010. if (stat < 0) {
  1011. mtd->ecc_stats.failed++;
  1012. } else {
  1013. mtd->ecc_stats.corrected += stat;
  1014. max_bitflips = max_t(unsigned int, max_bitflips, stat);
  1015. }
  1016. }
  1017. return max_bitflips;
  1018. }
  1019. /**
  1020. * nand_read_subpage - [REPLACEABLE] software ECC based sub-page read function
  1021. * @mtd: mtd info structure
  1022. * @chip: nand chip info structure
  1023. * @data_offs: offset of requested data within the page
  1024. * @readlen: data length
  1025. * @bufpoi: buffer to store read data
  1026. */
  1027. static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip,
  1028. uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi)
  1029. {
  1030. int start_step, end_step, num_steps;
  1031. uint32_t *eccpos = chip->ecc.layout->eccpos;
  1032. uint8_t *p;
  1033. int data_col_addr, i, gaps = 0;
  1034. int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
  1035. int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
  1036. int index = 0;
  1037. unsigned int max_bitflips = 0;
  1038. /* Column address within the page aligned to ECC size (256bytes) */
  1039. start_step = data_offs / chip->ecc.size;
  1040. end_step = (data_offs + readlen - 1) / chip->ecc.size;
  1041. num_steps = end_step - start_step + 1;
  1042. /* Data size aligned to ECC ecc.size */
  1043. datafrag_len = num_steps * chip->ecc.size;
  1044. eccfrag_len = num_steps * chip->ecc.bytes;
  1045. data_col_addr = start_step * chip->ecc.size;
  1046. /* If we read not a page aligned data */
  1047. if (data_col_addr != 0)
  1048. chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_col_addr, -1);
  1049. p = bufpoi + data_col_addr;
  1050. chip->read_buf(mtd, p, datafrag_len);
  1051. /* Calculate ECC */
  1052. for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
  1053. chip->ecc.calculate(mtd, p, &chip->buffers->ecccalc[i]);
  1054. /*
  1055. * The performance is faster if we position offsets according to
  1056. * ecc.pos. Let's make sure that there are no gaps in ECC positions.
  1057. */
  1058. for (i = 0; i < eccfrag_len - 1; i++) {
  1059. if (eccpos[i + start_step * chip->ecc.bytes] + 1 !=
  1060. eccpos[i + start_step * chip->ecc.bytes + 1]) {
  1061. gaps = 1;
  1062. break;
  1063. }
  1064. }
  1065. if (gaps) {
  1066. chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
  1067. chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
  1068. } else {
  1069. /*
  1070. * Send the command to read the particular ECC bytes take care
  1071. * about buswidth alignment in read_buf.
  1072. */
  1073. index = start_step * chip->ecc.bytes;
  1074. aligned_pos = eccpos[index] & ~(busw - 1);
  1075. aligned_len = eccfrag_len;
  1076. if (eccpos[index] & (busw - 1))
  1077. aligned_len++;
  1078. if (eccpos[index + (num_steps * chip->ecc.bytes)] & (busw - 1))
  1079. aligned_len++;
  1080. chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
  1081. mtd->writesize + aligned_pos, -1);
  1082. chip->read_buf(mtd, &chip->oob_poi[aligned_pos], aligned_len);
  1083. }
  1084. for (i = 0; i < eccfrag_len; i++)
  1085. chip->buffers->ecccode[i] = chip->oob_poi[eccpos[i + index]];
  1086. p = bufpoi + data_col_addr;
  1087. for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
  1088. int stat;
  1089. stat = chip->ecc.correct(mtd, p,
  1090. &chip->buffers->ecccode[i], &chip->buffers->ecccalc[i]);
  1091. if (stat < 0) {
  1092. mtd->ecc_stats.failed++;
  1093. } else {
  1094. mtd->ecc_stats.corrected += stat;
  1095. max_bitflips = max_t(unsigned int, max_bitflips, stat);
  1096. }
  1097. }
  1098. return max_bitflips;
  1099. }
  1100. /**
  1101. * nand_read_page_hwecc - [REPLACEABLE] hardware ECC based page read function
  1102. * @mtd: mtd info structure
  1103. * @chip: nand chip info structure
  1104. * @buf: buffer to store read data
  1105. * @page: page number to read
  1106. *
  1107. * Not for syndrome calculating ECC controllers which need a special oob layout.
  1108. */
  1109. static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
  1110. uint8_t *buf, int page)
  1111. {
  1112. int i, eccsize = chip->ecc.size;
  1113. int eccbytes = chip->ecc.bytes;
  1114. int eccsteps = chip->ecc.steps;
  1115. uint8_t *p = buf;
  1116. uint8_t *ecc_calc = chip->buffers->ecccalc;
  1117. uint8_t *ecc_code = chip->buffers->ecccode;
  1118. uint32_t *eccpos = chip->ecc.layout->eccpos;
  1119. unsigned int max_bitflips = 0;
  1120. for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
  1121. chip->ecc.hwctl(mtd, NAND_ECC_READ);
  1122. chip->read_buf(mtd, p, eccsize);
  1123. chip->ecc.calculate(mtd, p, &ecc_calc[i]);
  1124. }
  1125. chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
  1126. for (i = 0; i < chip->ecc.total; i++)
  1127. ecc_code[i] = chip->oob_poi[eccpos[i]];
  1128. eccsteps = chip->ecc.steps;
  1129. p = buf;
  1130. for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
  1131. int stat;
  1132. stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
  1133. if (stat < 0) {
  1134. mtd->ecc_stats.failed++;
  1135. } else {
  1136. mtd->ecc_stats.corrected += stat;
  1137. max_bitflips = max_t(unsigned int, max_bitflips, stat);
  1138. }
  1139. }
  1140. return max_bitflips;
  1141. }
  1142. /**
  1143. * nand_read_page_hwecc_oob_first - [REPLACEABLE] hw ecc, read oob first
  1144. * @mtd: mtd info structure
  1145. * @chip: nand chip info structure
  1146. * @buf: buffer to store read data
  1147. * @page: page number to read
  1148. *
  1149. * Hardware ECC for large page chips, require OOB to be read first. For this
  1150. * ECC mode, the write_page method is re-used from ECC_HW. These methods
  1151. * read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with
  1152. * multiple ECC steps, follows the "infix ECC" scheme and reads/writes ECC from
  1153. * the data area, by overwriting the NAND manufacturer bad block markings.
  1154. */
  1155. static int nand_read_page_hwecc_oob_first(struct mtd_info *mtd,
  1156. struct nand_chip *chip, uint8_t *buf, int page)
  1157. {
  1158. int i, eccsize = chip->ecc.size;
  1159. int eccbytes = chip->ecc.bytes;
  1160. int eccsteps = chip->ecc.steps;
  1161. uint8_t *p = buf;
  1162. uint8_t *ecc_code = chip->buffers->ecccode;
  1163. uint32_t *eccpos = chip->ecc.layout->eccpos;
  1164. uint8_t *ecc_calc = chip->buffers->ecccalc;
  1165. unsigned int max_bitflips = 0;
  1166. /* Read the OOB area first */
  1167. chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
  1168. chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
  1169. chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
  1170. for (i = 0; i < chip->ecc.total; i++)
  1171. ecc_code[i] = chip->oob_poi[eccpos[i]];
  1172. for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
  1173. int stat;
  1174. chip->ecc.hwctl(mtd, NAND_ECC_READ);
  1175. chip->read_buf(mtd, p, eccsize);
  1176. chip->ecc.calculate(mtd, p, &ecc_calc[i]);
  1177. stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL);
  1178. if (stat < 0) {
  1179. mtd->ecc_stats.failed++;
  1180. } else {
  1181. mtd->ecc_stats.corrected += stat;
  1182. max_bitflips = max_t(unsigned int, max_bitflips, stat);
  1183. }
  1184. }
  1185. return max_bitflips;
  1186. }
  1187. /**
  1188. * nand_read_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page read
  1189. * @mtd: mtd info structure
  1190. * @chip: nand chip info structure
  1191. * @buf: buffer to store read data
  1192. * @page: page number to read
  1193. *
  1194. * The hw generator calculates the error syndrome automatically. Therefore we
  1195. * need a special oob layout and handling.
  1196. */
  1197. static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
  1198. uint8_t *buf, int page)
  1199. {
  1200. int i, eccsize = chip->ecc.size;
  1201. int eccbytes = chip->ecc.bytes;
  1202. int eccsteps = chip->ecc.steps;
  1203. uint8_t *p = buf;
  1204. uint8_t *oob = chip->oob_poi;
  1205. unsigned int max_bitflips = 0;
  1206. for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
  1207. int stat;
  1208. chip->ecc.hwctl(mtd, NAND_ECC_READ);
  1209. chip->read_buf(mtd, p, eccsize);
  1210. if (chip->ecc.prepad) {
  1211. chip->read_buf(mtd, oob, chip->ecc.prepad);
  1212. oob += chip->ecc.prepad;
  1213. }
  1214. chip->ecc.hwctl(mtd, NAND_ECC_READSYN);
  1215. chip->read_buf(mtd, oob, eccbytes);
  1216. stat = chip->ecc.correct(mtd, p, oob, NULL);
  1217. if (stat < 0) {
  1218. mtd->ecc_stats.failed++;
  1219. } else {
  1220. mtd->ecc_stats.corrected += stat;
  1221. max_bitflips = max_t(unsigned int, max_bitflips, stat);
  1222. }
  1223. oob += eccbytes;
  1224. if (chip->ecc.postpad) {
  1225. chip->read_buf(mtd, oob, chip->ecc.postpad);
  1226. oob += chip->ecc.postpad;
  1227. }
  1228. }
  1229. /* Calculate remaining oob bytes */
  1230. i = mtd->oobsize - (oob - chip->oob_poi);
  1231. if (i)
  1232. chip->read_buf(mtd, oob, i);
  1233. return max_bitflips;
  1234. }
  1235. /**
  1236. * nand_transfer_oob - [INTERN] Transfer oob to client buffer
  1237. * @chip: nand chip structure
  1238. * @oob: oob destination address
  1239. * @ops: oob ops structure
  1240. * @len: size of oob to transfer
  1241. */
  1242. static uint8_t *nand_transfer_oob(struct nand_chip *chip, uint8_t *oob,
  1243. struct mtd_oob_ops *ops, size_t len)
  1244. {
  1245. switch (ops->mode) {
  1246. case MTD_OPS_PLACE_OOB:
  1247. case MTD_OPS_RAW:
  1248. memcpy(oob, chip->oob_poi + ops->ooboffs, len);
  1249. return oob + len;
  1250. case MTD_OPS_AUTO_OOB: {
  1251. struct nand_oobfree *free = chip->ecc.layout->oobfree;
  1252. uint32_t boffs = 0, roffs = ops->ooboffs;
  1253. size_t bytes = 0;
  1254. for (; free->length && len; free++, len -= bytes) {
  1255. /* Read request not from offset 0? */
  1256. if (unlikely(roffs)) {
  1257. if (roffs >= free->length) {
  1258. roffs -= free->length;
  1259. continue;
  1260. }
  1261. boffs = free->offset + roffs;
  1262. bytes = min_t(size_t, len,
  1263. (free->length - roffs));
  1264. roffs = 0;
  1265. } else {
  1266. bytes = min_t(size_t, len, free->length);
  1267. boffs = free->offset;
  1268. }
  1269. memcpy(oob, chip->oob_poi + boffs, bytes);
  1270. oob += bytes;
  1271. }
  1272. return oob;
  1273. }
  1274. default:
  1275. BUG();
  1276. }
  1277. return NULL;
  1278. }
  1279. /**
  1280. * nand_do_read_ops - [INTERN] Read data with ECC
  1281. * @mtd: MTD device structure
  1282. * @from: offset to read from
  1283. * @ops: oob ops structure
  1284. *
  1285. * Internal function. Called with chip held.
  1286. */
  1287. static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
  1288. struct mtd_oob_ops *ops)
  1289. {
  1290. int chipnr, page, realpage, col, bytes, aligned;
  1291. struct nand_chip *chip = mtd->priv;
  1292. struct mtd_ecc_stats stats;
  1293. int blkcheck = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
  1294. int sndcmd = 1;
  1295. int ret = 0;
  1296. uint32_t readlen = ops->len;
  1297. uint32_t oobreadlen = ops->ooblen;
  1298. uint32_t max_oobsize = ops->mode == MTD_OPS_AUTO_OOB ?
  1299. mtd->oobavail : mtd->oobsize;
  1300. uint8_t *bufpoi, *oob, *buf;
  1301. unsigned int max_bitflips = 0;
  1302. stats = mtd->ecc_stats;
  1303. chipnr = (int)(from >> chip->chip_shift);
  1304. chip->select_chip(mtd, chipnr);
  1305. realpage = (int)(from >> chip->page_shift);
  1306. page = realpage & chip->pagemask;
  1307. col = (int)(from & (mtd->writesize - 1));
  1308. buf = ops->datbuf;
  1309. oob = ops->oobbuf;
  1310. while (1) {
  1311. bytes = min(mtd->writesize - col, readlen);
  1312. aligned = (bytes == mtd->writesize);
  1313. /* Is the current page in the buffer? */
  1314. if (realpage != chip->pagebuf || oob) {
  1315. bufpoi = aligned ? buf : chip->buffers->databuf;
  1316. if (likely(sndcmd)) {
  1317. chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
  1318. sndcmd = 0;
  1319. }
  1320. /*
  1321. * Now read the page into the buffer. Absent an error,
  1322. * the read methods return max bitflips per ecc step.
  1323. */
  1324. if (unlikely(ops->mode == MTD_OPS_RAW))
  1325. ret = chip->ecc.read_page_raw(mtd, chip,
  1326. bufpoi, page);
  1327. else if (!aligned && NAND_SUBPAGE_READ(chip) && !oob)
  1328. ret = chip->ecc.read_subpage(mtd, chip,
  1329. col, bytes, bufpoi);
  1330. else
  1331. ret = chip->ecc.read_page(mtd, chip, bufpoi,
  1332. page);
  1333. if (ret < 0) {
  1334. if (!aligned)
  1335. /* Invalidate page cache */
  1336. chip->pagebuf = -1;
  1337. break;
  1338. }
  1339. max_bitflips = max_t(unsigned int, max_bitflips, ret);
  1340. /* Transfer not aligned data */
  1341. if (!aligned) {
  1342. if (!NAND_SUBPAGE_READ(chip) && !oob &&
  1343. !(mtd->ecc_stats.failed - stats.failed) &&
  1344. (ops->mode != MTD_OPS_RAW)) {
  1345. chip->pagebuf = realpage;
  1346. chip->pagebuf_bitflips = ret;
  1347. } else {
  1348. /* Invalidate page cache */
  1349. chip->pagebuf = -1;
  1350. }
  1351. memcpy(buf, chip->buffers->databuf + col, bytes);
  1352. }
  1353. buf += bytes;
  1354. if (unlikely(oob)) {
  1355. int toread = min(oobreadlen, max_oobsize);
  1356. if (toread) {
  1357. oob = nand_transfer_oob(chip,
  1358. oob, ops, toread);
  1359. oobreadlen -= toread;
  1360. }
  1361. }
  1362. if (!(chip->options & NAND_NO_READRDY)) {
  1363. /*
  1364. * Apply delay or wait for ready/busy pin. Do
  1365. * this before the AUTOINCR check, so no
  1366. * problems arise if a chip which does auto
  1367. * increment is marked as NOAUTOINCR by the
  1368. * board driver.
  1369. */
  1370. if (!chip->dev_ready)
  1371. udelay(chip->chip_delay);
  1372. else
  1373. nand_wait_ready(mtd);
  1374. }
  1375. } else {
  1376. memcpy(buf, chip->buffers->databuf + col, bytes);
  1377. buf += bytes;
  1378. max_bitflips = max_t(unsigned int, max_bitflips,
  1379. chip->pagebuf_bitflips);
  1380. }
  1381. readlen -= bytes;
  1382. if (!readlen)
  1383. break;
  1384. /* For subsequent reads align to page boundary */
  1385. col = 0;
  1386. /* Increment page address */
  1387. realpage++;
  1388. page = realpage & chip->pagemask;
  1389. /* Check, if we cross a chip boundary */
  1390. if (!page) {
  1391. chipnr++;
  1392. chip->select_chip(mtd, -1);
  1393. chip->select_chip(mtd, chipnr);
  1394. }
  1395. /*
  1396. * Check, if the chip supports auto page increment or if we
  1397. * have hit a block boundary.
  1398. */
  1399. if (!NAND_CANAUTOINCR(chip) || !(page & blkcheck))
  1400. sndcmd = 1;
  1401. }
  1402. ops->retlen = ops->len - (size_t) readlen;
  1403. if (oob)
  1404. ops->oobretlen = ops->ooblen - oobreadlen;
  1405. if (ret < 0)
  1406. return ret;
  1407. if (mtd->ecc_stats.failed - stats.failed)
  1408. return -EBADMSG;
  1409. return max_bitflips;
  1410. }
  1411. /**
  1412. * nand_read - [MTD Interface] MTD compatibility function for nand_do_read_ecc
  1413. * @mtd: MTD device structure
  1414. * @from: offset to read from
  1415. * @len: number of bytes to read
  1416. * @retlen: pointer to variable to store the number of read bytes
  1417. * @buf: the databuffer to put data
  1418. *
  1419. * Get hold of the chip and call nand_do_read.
  1420. */
  1421. static int nand_read(struct mtd_info *mtd, loff_t from, size_t len,
  1422. size_t *retlen, uint8_t *buf)
  1423. {
  1424. struct nand_chip *chip = mtd->priv;
  1425. struct mtd_oob_ops ops;
  1426. int ret;
  1427. nand_get_device(chip, mtd, FL_READING);
  1428. ops.len = len;
  1429. ops.datbuf = buf;
  1430. ops.oobbuf = NULL;
  1431. ops.mode = 0;
  1432. ret = nand_do_read_ops(mtd, from, &ops);
  1433. *retlen = ops.retlen;
  1434. nand_release_device(mtd);
  1435. return ret;
  1436. }
  1437. /**
  1438. * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function
  1439. * @mtd: mtd info structure
  1440. * @chip: nand chip info structure
  1441. * @page: page number to read
  1442. * @sndcmd: flag whether to issue read command or not
  1443. */
  1444. static int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
  1445. int page, int sndcmd)
  1446. {
  1447. if (sndcmd) {
  1448. chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
  1449. sndcmd = 0;
  1450. }
  1451. chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
  1452. return sndcmd;
  1453. }
  1454. /**
  1455. * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC
  1456. * with syndromes
  1457. * @mtd: mtd info structure
  1458. * @chip: nand chip info structure
  1459. * @page: page number to read
  1460. * @sndcmd: flag whether to issue read command or not
  1461. */
  1462. static int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
  1463. int page, int sndcmd)
  1464. {
  1465. uint8_t *buf = chip->oob_poi;
  1466. int length = mtd->oobsize;
  1467. int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
  1468. int eccsize = chip->ecc.size;
  1469. uint8_t *bufpoi = buf;
  1470. int i, toread, sndrnd = 0, pos;
  1471. chip->cmdfunc(mtd, NAND_CMD_READ0, chip->ecc.size, page);
  1472. for (i = 0; i < chip->ecc.steps; i++) {
  1473. if (sndrnd) {
  1474. pos = eccsize + i * (eccsize + chunk);
  1475. if (mtd->writesize > 512)
  1476. chip->cmdfunc(mtd, NAND_CMD_RNDOUT, pos, -1);
  1477. else
  1478. chip->cmdfunc(mtd, NAND_CMD_READ0, pos, page);
  1479. } else
  1480. sndrnd = 1;
  1481. toread = min_t(int, length, chunk);
  1482. chip->read_buf(mtd, bufpoi, toread);
  1483. bufpoi += toread;
  1484. length -= toread;
  1485. }
  1486. if (length > 0)
  1487. chip->read_buf(mtd, bufpoi, length);
  1488. return 1;
  1489. }
  1490. /**
  1491. * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function
  1492. * @mtd: mtd info structure
  1493. * @chip: nand chip info structure
  1494. * @page: page number to write
  1495. */
  1496. static int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
  1497. int page)
  1498. {
  1499. int status = 0;
  1500. const uint8_t *buf = chip->oob_poi;
  1501. int length = mtd->oobsize;
  1502. chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
  1503. chip->write_buf(mtd, buf, length);
  1504. /* Send command to program the OOB data */
  1505. chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
  1506. status = chip->waitfunc(mtd, chip);
  1507. return status & NAND_STATUS_FAIL ? -EIO : 0;
  1508. }
  1509. /**
  1510. * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC
  1511. * with syndrome - only for large page flash
  1512. * @mtd: mtd info structure
  1513. * @chip: nand chip info structure
  1514. * @page: page number to write
  1515. */
  1516. static int nand_write_oob_syndrome(struct mtd_info *mtd,
  1517. struct nand_chip *chip, int page)
  1518. {
  1519. int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
  1520. int eccsize = chip->ecc.size, length = mtd->oobsize;
  1521. int i, len, pos, status = 0, sndcmd = 0, steps = chip->ecc.steps;
  1522. const uint8_t *bufpoi = chip->oob_poi;
  1523. /*
  1524. * data-ecc-data-ecc ... ecc-oob
  1525. * or
  1526. * data-pad-ecc-pad-data-pad .... ecc-pad-oob
  1527. */
  1528. if (!chip->ecc.prepad && !chip->ecc.postpad) {
  1529. pos = steps * (eccsize + chunk);
  1530. steps = 0;
  1531. } else
  1532. pos = eccsize;
  1533. chip->cmdfunc(mtd, NAND_CMD_SEQIN, pos, page);
  1534. for (i = 0; i < steps; i++) {
  1535. if (sndcmd) {
  1536. if (mtd->writesize <= 512) {
  1537. uint32_t fill = 0xFFFFFFFF;
  1538. len = eccsize;
  1539. while (len > 0) {
  1540. int num = min_t(int, len, 4);
  1541. chip->write_buf(mtd, (uint8_t *)&fill,
  1542. num);
  1543. len -= num;
  1544. }
  1545. } else {
  1546. pos = eccsize + i * (eccsize + chunk);
  1547. chip->cmdfunc(mtd, NAND_CMD_RNDIN, pos, -1);
  1548. }
  1549. } else
  1550. sndcmd = 1;
  1551. len = min_t(int, length, chunk);
  1552. chip->write_buf(mtd, bufpoi, len);
  1553. bufpoi += len;
  1554. length -= len;
  1555. }
  1556. if (length > 0)
  1557. chip->write_buf(mtd, bufpoi, length);
  1558. chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
  1559. status = chip->waitfunc(mtd, chip);
  1560. return status & NAND_STATUS_FAIL ? -EIO : 0;
  1561. }
  1562. /**
  1563. * nand_do_read_oob - [INTERN] NAND read out-of-band
  1564. * @mtd: MTD device structure
  1565. * @from: offset to read from
  1566. * @ops: oob operations description structure
  1567. *
  1568. * NAND read out-of-band data from the spare area.
  1569. */
  1570. static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
  1571. struct mtd_oob_ops *ops)
  1572. {
  1573. int page, realpage, chipnr, sndcmd = 1;
  1574. struct nand_chip *chip = mtd->priv;
  1575. struct mtd_ecc_stats stats;
  1576. int blkcheck = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
  1577. int readlen = ops->ooblen;
  1578. int len;
  1579. uint8_t *buf = ops->oobbuf;
  1580. pr_debug("%s: from = 0x%08Lx, len = %i\n",
  1581. __func__, (unsigned long long)from, readlen);
  1582. stats = mtd->ecc_stats;
  1583. if (ops->mode == MTD_OPS_AUTO_OOB)
  1584. len = chip->ecc.layout->oobavail;
  1585. else
  1586. len = mtd->oobsize;
  1587. if (unlikely(ops->ooboffs >= len)) {
  1588. pr_debug("%s: attempt to start read outside oob\n",
  1589. __func__);
  1590. return -EINVAL;
  1591. }
  1592. /* Do not allow reads past end of device */
  1593. if (unlikely(from >= mtd->size ||
  1594. ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) -
  1595. (from >> chip->page_shift)) * len)) {
  1596. pr_debug("%s: attempt to read beyond end of device\n",
  1597. __func__);
  1598. return -EINVAL;
  1599. }
  1600. chipnr = (int)(from >> chip->chip_shift);
  1601. chip->select_chip(mtd, chipnr);
  1602. /* Shift to get page */
  1603. realpage = (int)(from >> chip->page_shift);
  1604. page = realpage & chip->pagemask;
  1605. while (1) {
  1606. if (ops->mode == MTD_OPS_RAW)
  1607. sndcmd = chip->ecc.read_oob_raw(mtd, chip, page, sndcmd);
  1608. else
  1609. sndcmd = chip->ecc.read_oob(mtd, chip, page, sndcmd);
  1610. len = min(len, readlen);
  1611. buf = nand_transfer_oob(chip, buf, ops, len);
  1612. if (!(chip->options & NAND_NO_READRDY)) {
  1613. /*
  1614. * Apply delay or wait for ready/busy pin. Do this
  1615. * before the AUTOINCR check, so no problems arise if a
  1616. * chip which does auto increment is marked as
  1617. * NOAUTOINCR by the board driver.
  1618. */
  1619. if (!chip->dev_ready)
  1620. udelay(chip->chip_delay);
  1621. else
  1622. nand_wait_ready(mtd);
  1623. }
  1624. readlen -= len;
  1625. if (!readlen)
  1626. break;
  1627. /* Increment page address */
  1628. realpage++;
  1629. page = realpage & chip->pagemask;
  1630. /* Check, if we cross a chip boundary */
  1631. if (!page) {
  1632. chipnr++;
  1633. chip->select_chip(mtd, -1);
  1634. chip->select_chip(mtd, chipnr);
  1635. }
  1636. /*
  1637. * Check, if the chip supports auto page increment or if we
  1638. * have hit a block boundary.
  1639. */
  1640. if (!NAND_CANAUTOINCR(chip) || !(page & blkcheck))
  1641. sndcmd = 1;
  1642. }
  1643. ops->oobretlen = ops->ooblen;
  1644. if (mtd->ecc_stats.failed - stats.failed)
  1645. return -EBADMSG;
  1646. return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
  1647. }
  1648. /**
  1649. * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
  1650. * @mtd: MTD device structure
  1651. * @from: offset to read from
  1652. * @ops: oob operation description structure
  1653. *
  1654. * NAND read data and/or out-of-band data.
  1655. */
  1656. static int nand_read_oob(struct mtd_info *mtd, loff_t from,
  1657. struct mtd_oob_ops *ops)
  1658. {
  1659. struct nand_chip *chip = mtd->priv;
  1660. int ret = -ENOTSUPP;
  1661. ops->retlen = 0;
  1662. /* Do not allow reads past end of device */
  1663. if (ops->datbuf && (from + ops->len) > mtd->size) {
  1664. pr_debug("%s: attempt to read beyond end of device\n",
  1665. __func__);
  1666. return -EINVAL;
  1667. }
  1668. nand_get_device(chip, mtd, FL_READING);
  1669. switch (ops->mode) {
  1670. case MTD_OPS_PLACE_OOB:
  1671. case MTD_OPS_AUTO_OOB:
  1672. case MTD_OPS_RAW:
  1673. break;
  1674. default:
  1675. goto out;
  1676. }
  1677. if (!ops->datbuf)
  1678. ret = nand_do_read_oob(mtd, from, ops);
  1679. else
  1680. ret = nand_do_read_ops(mtd, from, ops);
  1681. out:
  1682. nand_release_device(mtd);
  1683. return ret;
  1684. }
  1685. /**
  1686. * nand_write_page_raw - [INTERN] raw page write function
  1687. * @mtd: mtd info structure
  1688. * @chip: nand chip info structure
  1689. * @buf: data buffer
  1690. *
  1691. * Not for syndrome calculating ECC controllers, which use a special oob layout.
  1692. */
  1693. static void nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
  1694. const uint8_t *buf)
  1695. {
  1696. chip->write_buf(mtd, buf, mtd->writesize);
  1697. chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
  1698. }
  1699. /**
  1700. * nand_write_page_raw_syndrome - [INTERN] raw page write function
  1701. * @mtd: mtd info structure
  1702. * @chip: nand chip info structure
  1703. * @buf: data buffer
  1704. *
  1705. * We need a special oob layout and handling even when ECC isn't checked.
  1706. */
  1707. static void nand_write_page_raw_syndrome(struct mtd_info *mtd,
  1708. struct nand_chip *chip,
  1709. const uint8_t *buf)
  1710. {
  1711. int eccsize = chip->ecc.size;
  1712. int eccbytes = chip->ecc.bytes;
  1713. uint8_t *oob = chip->oob_poi;
  1714. int steps, size;
  1715. for (steps = chip->ecc.steps; steps > 0; steps--) {
  1716. chip->write_buf(mtd, buf, eccsize);
  1717. buf += eccsize;
  1718. if (chip->ecc.prepad) {
  1719. chip->write_buf(mtd, oob, chip->ecc.prepad);
  1720. oob += chip->ecc.prepad;
  1721. }
  1722. chip->read_buf(mtd, oob, eccbytes);
  1723. oob += eccbytes;
  1724. if (chip->ecc.postpad) {
  1725. chip->write_buf(mtd, oob, chip->ecc.postpad);
  1726. oob += chip->ecc.postpad;
  1727. }
  1728. }
  1729. size = mtd->oobsize - (oob - chip->oob_poi);
  1730. if (size)
  1731. chip->write_buf(mtd, oob, size);
  1732. }
  1733. /**
  1734. * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function
  1735. * @mtd: mtd info structure
  1736. * @chip: nand chip info structure
  1737. * @buf: data buffer
  1738. */
  1739. static void nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
  1740. const uint8_t *buf)
  1741. {
  1742. int i, eccsize = chip->ecc.size;
  1743. int eccbytes = chip->ecc.bytes;
  1744. int eccsteps = chip->ecc.steps;
  1745. uint8_t *ecc_calc = chip->buffers->ecccalc;
  1746. const uint8_t *p = buf;
  1747. uint32_t *eccpos = chip->ecc.layout->eccpos;
  1748. /* Software ECC calculation */
  1749. for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
  1750. chip->ecc.calculate(mtd, p, &ecc_calc[i]);
  1751. for (i = 0; i < chip->ecc.total; i++)
  1752. chip->oob_poi[eccpos[i]] = ecc_calc[i];
  1753. chip->ecc.write_page_raw(mtd, chip, buf);
  1754. }
  1755. /**
  1756. * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function
  1757. * @mtd: mtd info structure
  1758. * @chip: nand chip info structure
  1759. * @buf: data buffer
  1760. */
  1761. static void nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
  1762. const uint8_t *buf)
  1763. {
  1764. int i, eccsize = chip->ecc.size;
  1765. int eccbytes = chip->ecc.bytes;
  1766. int eccsteps = chip->ecc.steps;
  1767. uint8_t *ecc_calc = chip->buffers->ecccalc;
  1768. const uint8_t *p = buf;
  1769. uint32_t *eccpos = chip->ecc.layout->eccpos;
  1770. for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
  1771. chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
  1772. chip->write_buf(mtd, p, eccsize);
  1773. chip->ecc.calculate(mtd, p, &ecc_calc[i]);
  1774. }
  1775. for (i = 0; i < chip->ecc.total; i++)
  1776. chip->oob_poi[eccpos[i]] = ecc_calc[i];
  1777. chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
  1778. }
  1779. /**
  1780. * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write
  1781. * @mtd: mtd info structure
  1782. * @chip: nand chip info structure
  1783. * @buf: data buffer
  1784. *
  1785. * The hw generator calculates the error syndrome automatically. Therefore we
  1786. * need a special oob layout and handling.
  1787. */
  1788. static void nand_write_page_syndrome(struct mtd_info *mtd,
  1789. struct nand_chip *chip, const uint8_t *buf)
  1790. {
  1791. int i, eccsize = chip->ecc.size;
  1792. int eccbytes = chip->ecc.bytes;
  1793. int eccsteps = chip->ecc.steps;
  1794. const uint8_t *p = buf;
  1795. uint8_t *oob = chip->oob_poi;
  1796. for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
  1797. chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
  1798. chip->write_buf(mtd, p, eccsize);
  1799. if (chip->ecc.prepad) {
  1800. chip->write_buf(mtd, oob, chip->ecc.prepad);
  1801. oob += chip->ecc.prepad;
  1802. }
  1803. chip->ecc.calculate(mtd, p, oob);
  1804. chip->write_buf(mtd, oob, eccbytes);
  1805. oob += eccbytes;
  1806. if (chip->ecc.postpad) {
  1807. chip->write_buf(mtd, oob, chip->ecc.postpad);
  1808. oob += chip->ecc.postpad;
  1809. }
  1810. }
  1811. /* Calculate remaining oob bytes */
  1812. i = mtd->oobsize - (oob - chip->oob_poi);
  1813. if (i)
  1814. chip->write_buf(mtd, oob, i);
  1815. }
  1816. /**
  1817. * nand_write_page - [REPLACEABLE] write one page
  1818. * @mtd: MTD device structure
  1819. * @chip: NAND chip descriptor
  1820. * @buf: the data to write
  1821. * @page: page number to write
  1822. * @cached: cached programming
  1823. * @raw: use _raw version of write_page
  1824. */
  1825. static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
  1826. const uint8_t *buf, int page, int cached, int raw)
  1827. {
  1828. int status;
  1829. chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
  1830. if (unlikely(raw))
  1831. chip->ecc.write_page_raw(mtd, chip, buf);
  1832. else
  1833. chip->ecc.write_page(mtd, chip, buf);
  1834. /*
  1835. * Cached progamming disabled for now. Not sure if it's worth the
  1836. * trouble. The speed gain is not very impressive. (2.3->2.6Mib/s).
  1837. */
  1838. cached = 0;
  1839. if (!cached || !(chip->options & NAND_CACHEPRG)) {
  1840. chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
  1841. status = chip->waitfunc(mtd, chip);
  1842. /*
  1843. * See if operation failed and additional status checks are
  1844. * available.
  1845. */
  1846. if ((status & NAND_STATUS_FAIL) && (chip->errstat))
  1847. status = chip->errstat(mtd, chip, FL_WRITING, status,
  1848. page);
  1849. if (status & NAND_STATUS_FAIL)
  1850. return -EIO;
  1851. } else {
  1852. chip->cmdfunc(mtd, NAND_CMD_CACHEDPROG, -1, -1);
  1853. status = chip->waitfunc(mtd, chip);
  1854. }
  1855. #ifdef CONFIG_MTD_NAND_VERIFY_WRITE
  1856. /* Send command to read back the data */
  1857. chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
  1858. if (chip->verify_buf(mtd, buf, mtd->writesize))
  1859. return -EIO;
  1860. /* Make sure the next page prog is preceded by a status read */
  1861. chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
  1862. #endif
  1863. return 0;
  1864. }
  1865. /**
  1866. * nand_fill_oob - [INTERN] Transfer client buffer to oob
  1867. * @mtd: MTD device structure
  1868. * @oob: oob data buffer
  1869. * @len: oob data write length
  1870. * @ops: oob ops structure
  1871. */
  1872. static uint8_t *nand_fill_oob(struct mtd_info *mtd, uint8_t *oob, size_t len,
  1873. struct mtd_oob_ops *ops)
  1874. {
  1875. struct nand_chip *chip = mtd->priv;
  1876. /*
  1877. * Initialise to all 0xFF, to avoid the possibility of left over OOB
  1878. * data from a previous OOB read.
  1879. */
  1880. memset(chip->oob_poi, 0xff, mtd->oobsize);
  1881. switch (ops->mode) {
  1882. case MTD_OPS_PLACE_OOB:
  1883. case MTD_OPS_RAW:
  1884. memcpy(chip->oob_poi + ops->ooboffs, oob, len);
  1885. return oob + len;
  1886. case MTD_OPS_AUTO_OOB: {
  1887. struct nand_oobfree *free = chip->ecc.layout->oobfree;
  1888. uint32_t boffs = 0, woffs = ops->ooboffs;
  1889. size_t bytes = 0;
  1890. for (; free->length && len; free++, len -= bytes) {
  1891. /* Write request not from offset 0? */
  1892. if (unlikely(woffs)) {
  1893. if (woffs >= free->length) {
  1894. woffs -= free->length;
  1895. continue;
  1896. }
  1897. boffs = free->offset + woffs;
  1898. bytes = min_t(size_t, len,
  1899. (free->length - woffs));
  1900. woffs = 0;
  1901. } else {
  1902. bytes = min_t(size_t, len, free->length);
  1903. boffs = free->offset;
  1904. }
  1905. memcpy(chip->oob_poi + boffs, oob, bytes);
  1906. oob += bytes;
  1907. }
  1908. return oob;
  1909. }
  1910. default:
  1911. BUG();
  1912. }
  1913. return NULL;
  1914. }
  1915. #define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0)
  1916. /**
  1917. * nand_do_write_ops - [INTERN] NAND write with ECC
  1918. * @mtd: MTD device structure
  1919. * @to: offset to write to
  1920. * @ops: oob operations description structure
  1921. *
  1922. * NAND write with ECC.
  1923. */
  1924. static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
  1925. struct mtd_oob_ops *ops)
  1926. {
  1927. int chipnr, realpage, page, blockmask, column;
  1928. struct nand_chip *chip = mtd->priv;
  1929. uint32_t writelen = ops->len;
  1930. uint32_t oobwritelen = ops->ooblen;
  1931. uint32_t oobmaxlen = ops->mode == MTD_OPS_AUTO_OOB ?
  1932. mtd->oobavail : mtd->oobsize;
  1933. uint8_t *oob = ops->oobbuf;
  1934. uint8_t *buf = ops->datbuf;
  1935. int ret, subpage;
  1936. ops->retlen = 0;
  1937. if (!writelen)
  1938. return 0;
  1939. /* Reject writes, which are not page aligned */
  1940. if (NOTALIGNED(to) || NOTALIGNED(ops->len)) {
  1941. pr_notice("%s: attempt to write non page aligned data\n",
  1942. __func__);
  1943. return -EINVAL;
  1944. }
  1945. column = to & (mtd->writesize - 1);
  1946. subpage = column || (writelen & (mtd->writesize - 1));
  1947. if (subpage && oob)
  1948. return -EINVAL;
  1949. chipnr = (int)(to >> chip->chip_shift);
  1950. chip->select_chip(mtd, chipnr);
  1951. /* Check, if it is write protected */
  1952. if (nand_check_wp(mtd))
  1953. return -EIO;
  1954. realpage = (int)(to >> chip->page_shift);
  1955. page = realpage & chip->pagemask;
  1956. blockmask = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
  1957. /* Invalidate the page cache, when we write to the cached page */
  1958. if (to <= (chip->pagebuf << chip->page_shift) &&
  1959. (chip->pagebuf << chip->page_shift) < (to + ops->len))
  1960. chip->pagebuf = -1;
  1961. /* Don't allow multipage oob writes with offset */
  1962. if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen))
  1963. return -EINVAL;
  1964. while (1) {
  1965. int bytes = mtd->writesize;
  1966. int cached = writelen > bytes && page != blockmask;
  1967. uint8_t *wbuf = buf;
  1968. /* Partial page write? */
  1969. if (unlikely(column || writelen < (mtd->writesize - 1))) {
  1970. cached = 0;
  1971. bytes = min_t(int, bytes - column, (int) writelen);
  1972. chip->pagebuf = -1;
  1973. memset(chip->buffers->databuf, 0xff, mtd->writesize);
  1974. memcpy(&chip->buffers->databuf[column], buf, bytes);
  1975. wbuf = chip->buffers->databuf;
  1976. }
  1977. if (unlikely(oob)) {
  1978. size_t len = min(oobwritelen, oobmaxlen);
  1979. oob = nand_fill_oob(mtd, oob, len, ops);
  1980. oobwritelen -= len;
  1981. } else {
  1982. /* We still need to erase leftover OOB data */
  1983. memset(chip->oob_poi, 0xff, mtd->oobsize);
  1984. }
  1985. ret = chip->write_page(mtd, chip, wbuf, page, cached,
  1986. (ops->mode == MTD_OPS_RAW));
  1987. if (ret)
  1988. break;
  1989. writelen -= bytes;
  1990. if (!writelen)
  1991. break;
  1992. column = 0;
  1993. buf += bytes;
  1994. realpage++;
  1995. page = realpage & chip->pagemask;
  1996. /* Check, if we cross a chip boundary */
  1997. if (!page) {
  1998. chipnr++;
  1999. chip->select_chip(mtd, -1);
  2000. chip->select_chip(mtd, chipnr);
  2001. }
  2002. }
  2003. ops->retlen = ops->len - writelen;
  2004. if (unlikely(oob))
  2005. ops->oobretlen = ops->ooblen;
  2006. return ret;
  2007. }
  2008. /**
  2009. * panic_nand_write - [MTD Interface] NAND write with ECC
  2010. * @mtd: MTD device structure
  2011. * @to: offset to write to
  2012. * @len: number of bytes to write
  2013. * @retlen: pointer to variable to store the number of written bytes
  2014. * @buf: the data to write
  2015. *
  2016. * NAND write with ECC. Used when performing writes in interrupt context, this
  2017. * may for example be called by mtdoops when writing an oops while in panic.
  2018. */
  2019. static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
  2020. size_t *retlen, const uint8_t *buf)
  2021. {
  2022. struct nand_chip *chip = mtd->priv;
  2023. struct mtd_oob_ops ops;
  2024. int ret;
  2025. /* Wait for the device to get ready */
  2026. panic_nand_wait(mtd, chip, 400);
  2027. /* Grab the device */
  2028. panic_nand_get_device(chip, mtd, FL_WRITING);
  2029. ops.len = len;
  2030. ops.datbuf = (uint8_t *)buf;
  2031. ops.oobbuf = NULL;
  2032. ops.mode = 0;
  2033. ret = nand_do_write_ops(mtd, to, &ops);
  2034. *retlen = ops.retlen;
  2035. return ret;
  2036. }
  2037. /**
  2038. * nand_write - [MTD Interface] NAND write with ECC
  2039. * @mtd: MTD device structure
  2040. * @to: offset to write to
  2041. * @len: number of bytes to write
  2042. * @retlen: pointer to variable to store the number of written bytes
  2043. * @buf: the data to write
  2044. *
  2045. * NAND write with ECC.
  2046. */
  2047. static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
  2048. size_t *retlen, const uint8_t *buf)
  2049. {
  2050. struct nand_chip *chip = mtd->priv;
  2051. struct mtd_oob_ops ops;
  2052. int ret;
  2053. nand_get_device(chip, mtd, FL_WRITING);
  2054. ops.len = len;
  2055. ops.datbuf = (uint8_t *)buf;
  2056. ops.oobbuf = NULL;
  2057. ops.mode = 0;
  2058. ret = nand_do_write_ops(mtd, to, &ops);
  2059. *retlen = ops.retlen;
  2060. nand_release_device(mtd);
  2061. return ret;
  2062. }
  2063. /**
  2064. * nand_do_write_oob - [MTD Interface] NAND write out-of-band
  2065. * @mtd: MTD device structure
  2066. * @to: offset to write to
  2067. * @ops: oob operation description structure
  2068. *
  2069. * NAND write out-of-band.
  2070. */
  2071. static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
  2072. struct mtd_oob_ops *ops)
  2073. {
  2074. int chipnr, page, status, len;
  2075. struct nand_chip *chip = mtd->priv;
  2076. pr_debug("%s: to = 0x%08x, len = %i\n",
  2077. __func__, (unsigned int)to, (int)ops->ooblen);
  2078. if (ops->mode == MTD_OPS_AUTO_OOB)
  2079. len = chip->ecc.layout->oobavail;
  2080. else
  2081. len = mtd->oobsize;
  2082. /* Do not allow write past end of page */
  2083. if ((ops->ooboffs + ops->ooblen) > len) {
  2084. pr_debug("%s: attempt to write past end of page\n",
  2085. __func__);
  2086. return -EINVAL;
  2087. }
  2088. if (unlikely(ops->ooboffs >= len)) {
  2089. pr_debug("%s: attempt to start write outside oob\n",
  2090. __func__);
  2091. return -EINVAL;
  2092. }
  2093. /* Do not allow write past end of device */
  2094. if (unlikely(to >= mtd->size ||
  2095. ops->ooboffs + ops->ooblen >
  2096. ((mtd->size >> chip->page_shift) -
  2097. (to >> chip->page_shift)) * len)) {
  2098. pr_debug("%s: attempt to write beyond end of device\n",
  2099. __func__);
  2100. return -EINVAL;
  2101. }
  2102. chipnr = (int)(to >> chip->chip_shift);
  2103. chip->select_chip(mtd, chipnr);
  2104. /* Shift to get page */
  2105. page = (int)(to >> chip->page_shift);
  2106. /*
  2107. * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
  2108. * of my DiskOnChip 2000 test units) will clear the whole data page too
  2109. * if we don't do this. I have no clue why, but I seem to have 'fixed'
  2110. * it in the doc2000 driver in August 1999. dwmw2.
  2111. */
  2112. chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
  2113. /* Check, if it is write protected */
  2114. if (nand_check_wp(mtd))
  2115. return -EROFS;
  2116. /* Invalidate the page cache, if we write to the cached page */
  2117. if (page == chip->pagebuf)
  2118. chip->pagebuf = -1;
  2119. nand_fill_oob(mtd, ops->oobbuf, ops->ooblen, ops);
  2120. if (ops->mode == MTD_OPS_RAW)
  2121. status = chip->ecc.write_oob_raw(mtd, chip, page & chip->pagemask);
  2122. else
  2123. status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
  2124. if (status)
  2125. return status;
  2126. ops->oobretlen = ops->ooblen;
  2127. return 0;
  2128. }
  2129. /**
  2130. * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
  2131. * @mtd: MTD device structure
  2132. * @to: offset to write to
  2133. * @ops: oob operation description structure
  2134. */
  2135. static int nand_write_oob(struct mtd_info *mtd, loff_t to,
  2136. struct mtd_oob_ops *ops)
  2137. {
  2138. struct nand_chip *chip = mtd->priv;
  2139. int ret = -ENOTSUPP;
  2140. ops->retlen = 0;
  2141. /* Do not allow writes past end of device */
  2142. if (ops->datbuf && (to + ops->len) > mtd->size) {
  2143. pr_debug("%s: attempt to write beyond end of device\n",
  2144. __func__);
  2145. return -EINVAL;
  2146. }
  2147. nand_get_device(chip, mtd, FL_WRITING);
  2148. switch (ops->mode) {
  2149. case MTD_OPS_PLACE_OOB:
  2150. case MTD_OPS_AUTO_OOB:
  2151. case MTD_OPS_RAW:
  2152. break;
  2153. default:
  2154. goto out;
  2155. }
  2156. if (!ops->datbuf)
  2157. ret = nand_do_write_oob(mtd, to, ops);
  2158. else
  2159. ret = nand_do_write_ops(mtd, to, ops);
  2160. out:
  2161. nand_release_device(mtd);
  2162. return ret;
  2163. }
  2164. /**
  2165. * single_erase_cmd - [GENERIC] NAND standard block erase command function
  2166. * @mtd: MTD device structure
  2167. * @page: the page address of the block which will be erased
  2168. *
  2169. * Standard erase command for NAND chips.
  2170. */
  2171. static void single_erase_cmd(struct mtd_info *mtd, int page)
  2172. {
  2173. struct nand_chip *chip = mtd->priv;
  2174. /* Send commands to erase a block */
  2175. chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
  2176. chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
  2177. }
  2178. /**
  2179. * multi_erase_cmd - [GENERIC] AND specific block erase command function
  2180. * @mtd: MTD device structure
  2181. * @page: the page address of the block which will be erased
  2182. *
  2183. * AND multi block erase command function. Erase 4 consecutive blocks.
  2184. */
  2185. static void multi_erase_cmd(struct mtd_info *mtd, int page)
  2186. {
  2187. struct nand_chip *chip = mtd->priv;
  2188. /* Send commands to erase a block */
  2189. chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
  2190. chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
  2191. chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
  2192. chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
  2193. chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
  2194. }
  2195. /**
  2196. * nand_erase - [MTD Interface] erase block(s)
  2197. * @mtd: MTD device structure
  2198. * @instr: erase instruction
  2199. *
  2200. * Erase one ore more blocks.
  2201. */
  2202. static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
  2203. {
  2204. return nand_erase_nand(mtd, instr, 0);
  2205. }
  2206. #define BBT_PAGE_MASK 0xffffff3f
  2207. /**
  2208. * nand_erase_nand - [INTERN] erase block(s)
  2209. * @mtd: MTD device structure
  2210. * @instr: erase instruction
  2211. * @allowbbt: allow erasing the bbt area
  2212. *
  2213. * Erase one ore more blocks.
  2214. */
  2215. int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
  2216. int allowbbt)
  2217. {
  2218. int page, status, pages_per_block, ret, chipnr;
  2219. struct nand_chip *chip = mtd->priv;
  2220. loff_t rewrite_bbt[NAND_MAX_CHIPS] = {0};
  2221. unsigned int bbt_masked_page = 0xffffffff;
  2222. loff_t len;
  2223. pr_debug("%s: start = 0x%012llx, len = %llu\n",
  2224. __func__, (unsigned long long)instr->addr,
  2225. (unsigned long long)instr->len);
  2226. if (check_offs_len(mtd, instr->addr, instr->len))
  2227. return -EINVAL;
  2228. /* Grab the lock and see if the device is available */
  2229. nand_get_device(chip, mtd, FL_ERASING);
  2230. /* Shift to get first page */
  2231. page = (int)(instr->addr >> chip->page_shift);
  2232. chipnr = (int)(instr->addr >> chip->chip_shift);
  2233. /* Calculate pages in each block */
  2234. pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
  2235. /* Select the NAND device */
  2236. chip->select_chip(mtd, chipnr);
  2237. /* Check, if it is write protected */
  2238. if (nand_check_wp(mtd)) {
  2239. pr_debug("%s: device is write protected!\n",
  2240. __func__);
  2241. instr->state = MTD_ERASE_FAILED;
  2242. goto erase_exit;
  2243. }
  2244. /*
  2245. * If BBT requires refresh, set the BBT page mask to see if the BBT
  2246. * should be rewritten. Otherwise the mask is set to 0xffffffff which
  2247. * can not be matched. This is also done when the bbt is actually
  2248. * erased to avoid recursive updates.
  2249. */
  2250. if (chip->options & BBT_AUTO_REFRESH && !allowbbt)
  2251. bbt_masked_page = chip->bbt_td->pages[chipnr] & BBT_PAGE_MASK;
  2252. /* Loop through the pages */
  2253. len = instr->len;
  2254. instr->state = MTD_ERASING;
  2255. while (len) {
  2256. /* Check if we have a bad block, we do not erase bad blocks! */
  2257. if (nand_block_checkbad(mtd, ((loff_t) page) <<
  2258. chip->page_shift, 0, allowbbt)) {
  2259. pr_warn("%s: attempt to erase a bad block at page 0x%08x\n",
  2260. __func__, page);
  2261. instr->state = MTD_ERASE_FAILED;
  2262. goto erase_exit;
  2263. }
  2264. /*
  2265. * Invalidate the page cache, if we erase the block which
  2266. * contains the current cached page.
  2267. */
  2268. if (page <= chip->pagebuf && chip->pagebuf <
  2269. (page + pages_per_block))
  2270. chip->pagebuf = -1;
  2271. chip->erase_cmd(mtd, page & chip->pagemask);
  2272. status = chip->waitfunc(mtd, chip);
  2273. /*
  2274. * See if operation failed and additional status checks are
  2275. * available
  2276. */
  2277. if ((status & NAND_STATUS_FAIL) && (chip->errstat))
  2278. status = chip->errstat(mtd, chip, FL_ERASING,
  2279. status, page);
  2280. /* See if block erase succeeded */
  2281. if (status & NAND_STATUS_FAIL) {
  2282. pr_debug("%s: failed erase, page 0x%08x\n",
  2283. __func__, page);
  2284. instr->state = MTD_ERASE_FAILED;
  2285. instr->fail_addr =
  2286. ((loff_t)page << chip->page_shift);
  2287. goto erase_exit;
  2288. }
  2289. /*
  2290. * If BBT requires refresh, set the BBT rewrite flag to the
  2291. * page being erased.
  2292. */
  2293. if (bbt_masked_page != 0xffffffff &&
  2294. (page & BBT_PAGE_MASK) == bbt_masked_page)
  2295. rewrite_bbt[chipnr] =
  2296. ((loff_t)page << chip->page_shift);
  2297. /* Increment page address and decrement length */
  2298. len -= (1 << chip->phys_erase_shift);
  2299. page += pages_per_block;
  2300. /* Check, if we cross a chip boundary */
  2301. if (len && !(page & chip->pagemask)) {
  2302. chipnr++;
  2303. chip->select_chip(mtd, -1);
  2304. chip->select_chip(mtd, chipnr);
  2305. /*
  2306. * If BBT requires refresh and BBT-PERCHIP, set the BBT
  2307. * page mask to see if this BBT should be rewritten.
  2308. */
  2309. if (bbt_masked_page != 0xffffffff &&
  2310. (chip->bbt_td->options & NAND_BBT_PERCHIP))
  2311. bbt_masked_page = chip->bbt_td->pages[chipnr] &
  2312. BBT_PAGE_MASK;
  2313. }
  2314. }
  2315. instr->state = MTD_ERASE_DONE;
  2316. erase_exit:
  2317. ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
  2318. /* Deselect and wake up anyone waiting on the device */
  2319. nand_release_device(mtd);
  2320. /* Do call back function */
  2321. if (!ret)
  2322. mtd_erase_callback(instr);
  2323. /*
  2324. * If BBT requires refresh and erase was successful, rewrite any
  2325. * selected bad block tables.
  2326. */
  2327. if (bbt_masked_page == 0xffffffff || ret)
  2328. return ret;
  2329. for (chipnr = 0; chipnr < chip->numchips; chipnr++) {
  2330. if (!rewrite_bbt[chipnr])
  2331. continue;
  2332. /* Update the BBT for chip */
  2333. pr_debug("%s: nand_update_bbt (%d:0x%0llx 0x%0x)\n",
  2334. __func__, chipnr, rewrite_bbt[chipnr],
  2335. chip->bbt_td->pages[chipnr]);
  2336. nand_update_bbt(mtd, rewrite_bbt[chipnr]);
  2337. }
  2338. /* Return more or less happy */
  2339. return ret;
  2340. }
  2341. /**
  2342. * nand_sync - [MTD Interface] sync
  2343. * @mtd: MTD device structure
  2344. *
  2345. * Sync is actually a wait for chip ready function.
  2346. */
  2347. static void nand_sync(struct mtd_info *mtd)
  2348. {
  2349. struct nand_chip *chip = mtd->priv;
  2350. pr_debug("%s: called\n", __func__);
  2351. /* Grab the lock and see if the device is available */
  2352. nand_get_device(chip, mtd, FL_SYNCING);
  2353. /* Release it and go back */
  2354. nand_release_device(mtd);
  2355. }
  2356. /**
  2357. * nand_block_isbad - [MTD Interface] Check if block at offset is bad
  2358. * @mtd: MTD device structure
  2359. * @offs: offset relative to mtd start
  2360. */
  2361. static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
  2362. {
  2363. return nand_block_checkbad(mtd, offs, 1, 0);
  2364. }
  2365. /**
  2366. * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
  2367. * @mtd: MTD device structure
  2368. * @ofs: offset relative to mtd start
  2369. */
  2370. static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
  2371. {
  2372. struct nand_chip *chip = mtd->priv;
  2373. int ret;
  2374. ret = nand_block_isbad(mtd, ofs);
  2375. if (ret) {
  2376. /* If it was bad already, return success and do nothing */
  2377. if (ret > 0)
  2378. return 0;
  2379. return ret;
  2380. }
  2381. return chip->block_markbad(mtd, ofs);
  2382. }
  2383. /**
  2384. * nand_suspend - [MTD Interface] Suspend the NAND flash
  2385. * @mtd: MTD device structure
  2386. */
  2387. static int nand_suspend(struct mtd_info *mtd)
  2388. {
  2389. struct nand_chip *chip = mtd->priv;
  2390. return nand_get_device(chip, mtd, FL_PM_SUSPENDED);
  2391. }
  2392. /**
  2393. * nand_resume - [MTD Interface] Resume the NAND flash
  2394. * @mtd: MTD device structure
  2395. */
  2396. static void nand_resume(struct mtd_info *mtd)
  2397. {
  2398. struct nand_chip *chip = mtd->priv;
  2399. if (chip->state == FL_PM_SUSPENDED)
  2400. nand_release_device(mtd);
  2401. else
  2402. pr_err("%s called for a chip which is not in suspended state\n",
  2403. __func__);
  2404. }
  2405. /* Set default functions */
  2406. static void nand_set_defaults(struct nand_chip *chip, int busw)
  2407. {
  2408. /* check for proper chip_delay setup, set 20us if not */
  2409. if (!chip->chip_delay)
  2410. chip->chip_delay = 20;
  2411. /* check, if a user supplied command function given */
  2412. if (chip->cmdfunc == NULL)
  2413. chip->cmdfunc = nand_command;
  2414. /* check, if a user supplied wait function given */
  2415. if (chip->waitfunc == NULL)
  2416. chip->waitfunc = nand_wait;
  2417. if (!chip->select_chip)
  2418. chip->select_chip = nand_select_chip;
  2419. if (!chip->read_byte)
  2420. chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
  2421. if (!chip->read_word)
  2422. chip->read_word = nand_read_word;
  2423. if (!chip->block_bad)
  2424. chip->block_bad = nand_block_bad;
  2425. if (!chip->block_markbad)
  2426. chip->block_markbad = nand_default_block_markbad;
  2427. if (!chip->write_buf)
  2428. chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
  2429. if (!chip->read_buf)
  2430. chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
  2431. if (!chip->verify_buf)
  2432. chip->verify_buf = busw ? nand_verify_buf16 : nand_verify_buf;
  2433. if (!chip->scan_bbt)
  2434. chip->scan_bbt = nand_default_bbt;
  2435. if (!chip->controller) {
  2436. chip->controller = &chip->hwcontrol;
  2437. spin_lock_init(&chip->controller->lock);
  2438. init_waitqueue_head(&chip->controller->wq);
  2439. }
  2440. }
  2441. /* Sanitize ONFI strings so we can safely print them */
  2442. static void sanitize_string(uint8_t *s, size_t len)
  2443. {
  2444. ssize_t i;
  2445. /* Null terminate */
  2446. s[len - 1] = 0;
  2447. /* Remove non printable chars */
  2448. for (i = 0; i < len - 1; i++) {
  2449. if (s[i] < ' ' || s[i] > 127)
  2450. s[i] = '?';
  2451. }
  2452. /* Remove trailing spaces */
  2453. strim(s);
  2454. }
  2455. static u16 onfi_crc16(u16 crc, u8 const *p, size_t len)
  2456. {
  2457. int i;
  2458. while (len--) {
  2459. crc ^= *p++ << 8;
  2460. for (i = 0; i < 8; i++)
  2461. crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0);
  2462. }
  2463. return crc;
  2464. }
  2465. /*
  2466. * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise.
  2467. */
  2468. static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip,
  2469. int *busw)
  2470. {
  2471. struct nand_onfi_params *p = &chip->onfi_params;
  2472. int i;
  2473. int val;
  2474. /* Try ONFI for unknown chip or LP */
  2475. chip->cmdfunc(mtd, NAND_CMD_READID, 0x20, -1);
  2476. if (chip->read_byte(mtd) != 'O' || chip->read_byte(mtd) != 'N' ||
  2477. chip->read_byte(mtd) != 'F' || chip->read_byte(mtd) != 'I')
  2478. return 0;
  2479. chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
  2480. for (i = 0; i < 3; i++) {
  2481. chip->read_buf(mtd, (uint8_t *)p, sizeof(*p));
  2482. if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 254) ==
  2483. le16_to_cpu(p->crc)) {
  2484. pr_info("ONFI param page %d valid\n", i);
  2485. break;
  2486. }
  2487. }
  2488. if (i == 3)
  2489. return 0;
  2490. /* Check version */
  2491. val = le16_to_cpu(p->revision);
  2492. if (val & (1 << 5))
  2493. chip->onfi_version = 23;
  2494. else if (val & (1 << 4))
  2495. chip->onfi_version = 22;
  2496. else if (val & (1 << 3))
  2497. chip->onfi_version = 21;
  2498. else if (val & (1 << 2))
  2499. chip->onfi_version = 20;
  2500. else if (val & (1 << 1))
  2501. chip->onfi_version = 10;
  2502. else
  2503. chip->onfi_version = 0;
  2504. if (!chip->onfi_version) {
  2505. pr_info("%s: unsupported ONFI version: %d\n", __func__, val);
  2506. return 0;
  2507. }
  2508. sanitize_string(p->manufacturer, sizeof(p->manufacturer));
  2509. sanitize_string(p->model, sizeof(p->model));
  2510. if (!mtd->name)
  2511. mtd->name = p->model;
  2512. mtd->writesize = le32_to_cpu(p->byte_per_page);
  2513. mtd->erasesize = le32_to_cpu(p->pages_per_block) * mtd->writesize;
  2514. mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
  2515. chip->chipsize = le32_to_cpu(p->blocks_per_lun);
  2516. chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
  2517. *busw = 0;
  2518. if (le16_to_cpu(p->features) & 1)
  2519. *busw = NAND_BUSWIDTH_16;
  2520. chip->options &= ~NAND_CHIPOPTIONS_MSK;
  2521. chip->options |= (NAND_NO_READRDY |
  2522. NAND_NO_AUTOINCR) & NAND_CHIPOPTIONS_MSK;
  2523. pr_info("ONFI flash detected\n");
  2524. return 1;
  2525. }
  2526. /*
  2527. * Get the flash and manufacturer id and lookup if the type is supported.
  2528. */
  2529. static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
  2530. struct nand_chip *chip,
  2531. int busw,
  2532. int *maf_id, int *dev_id,
  2533. struct nand_flash_dev *type)
  2534. {
  2535. int i, maf_idx;
  2536. u8 id_data[8];
  2537. int ret;
  2538. /* Select the device */
  2539. chip->select_chip(mtd, 0);
  2540. /*
  2541. * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
  2542. * after power-up.
  2543. */
  2544. chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
  2545. /* Send the command for reading device ID */
  2546. chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
  2547. /* Read manufacturer and device IDs */
  2548. *maf_id = chip->read_byte(mtd);
  2549. *dev_id = chip->read_byte(mtd);
  2550. /*
  2551. * Try again to make sure, as some systems the bus-hold or other
  2552. * interface concerns can cause random data which looks like a
  2553. * possibly credible NAND flash to appear. If the two results do
  2554. * not match, ignore the device completely.
  2555. */
  2556. chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
  2557. for (i = 0; i < 2; i++)
  2558. id_data[i] = chip->read_byte(mtd);
  2559. if (id_data[0] != *maf_id || id_data[1] != *dev_id) {
  2560. pr_info("%s: second ID read did not match "
  2561. "%02x,%02x against %02x,%02x\n", __func__,
  2562. *maf_id, *dev_id, id_data[0], id_data[1]);
  2563. return ERR_PTR(-ENODEV);
  2564. }
  2565. if (!type)
  2566. type = nand_flash_ids;
  2567. for (; type->name != NULL; type++)
  2568. if (*dev_id == type->id)
  2569. break;
  2570. chip->onfi_version = 0;
  2571. if (!type->name || !type->pagesize) {
  2572. /* Check is chip is ONFI compliant */
  2573. ret = nand_flash_detect_onfi(mtd, chip, &busw);
  2574. if (ret)
  2575. goto ident_done;
  2576. }
  2577. chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
  2578. /* Read entire ID string */
  2579. for (i = 0; i < 8; i++)
  2580. id_data[i] = chip->read_byte(mtd);
  2581. if (!type->name)
  2582. return ERR_PTR(-ENODEV);
  2583. if (!mtd->name)
  2584. mtd->name = type->name;
  2585. chip->chipsize = (uint64_t)type->chipsize << 20;
  2586. if (!type->pagesize && chip->init_size) {
  2587. /* Set the pagesize, oobsize, erasesize by the driver */
  2588. busw = chip->init_size(mtd, chip, id_data);
  2589. } else if (!type->pagesize) {
  2590. int extid;
  2591. /* The 3rd id byte holds MLC / multichip data */
  2592. chip->cellinfo = id_data[2];
  2593. /* The 4th id byte is the important one */
  2594. extid = id_data[3];
  2595. /*
  2596. * Field definitions are in the following datasheets:
  2597. * Old style (4,5 byte ID): Samsung K9GAG08U0M (p.32)
  2598. * New style (6 byte ID): Samsung K9GBG08U0M (p.40)
  2599. *
  2600. * Check for wraparound + Samsung ID + nonzero 6th byte
  2601. * to decide what to do.
  2602. */
  2603. if (id_data[0] == id_data[6] && id_data[1] == id_data[7] &&
  2604. id_data[0] == NAND_MFR_SAMSUNG &&
  2605. (chip->cellinfo & NAND_CI_CELLTYPE_MSK) &&
  2606. id_data[5] != 0x00) {
  2607. /* Calc pagesize */
  2608. mtd->writesize = 2048 << (extid & 0x03);
  2609. extid >>= 2;
  2610. /* Calc oobsize */
  2611. switch (extid & 0x03) {
  2612. case 1:
  2613. mtd->oobsize = 128;
  2614. break;
  2615. case 2:
  2616. mtd->oobsize = 218;
  2617. break;
  2618. case 3:
  2619. mtd->oobsize = 400;
  2620. break;
  2621. default:
  2622. mtd->oobsize = 436;
  2623. break;
  2624. }
  2625. extid >>= 2;
  2626. /* Calc blocksize */
  2627. mtd->erasesize = (128 * 1024) <<
  2628. (((extid >> 1) & 0x04) | (extid & 0x03));
  2629. busw = 0;
  2630. } else {
  2631. /* Calc pagesize */
  2632. mtd->writesize = 1024 << (extid & 0x03);
  2633. extid >>= 2;
  2634. /* Calc oobsize */
  2635. mtd->oobsize = (8 << (extid & 0x01)) *
  2636. (mtd->writesize >> 9);
  2637. extid >>= 2;
  2638. /* Calc blocksize. Blocksize is multiples of 64KiB */
  2639. mtd->erasesize = (64 * 1024) << (extid & 0x03);
  2640. extid >>= 2;
  2641. /* Get buswidth information */
  2642. busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0;
  2643. }
  2644. } else {
  2645. /*
  2646. * Old devices have chip data hardcoded in the device id table.
  2647. */
  2648. mtd->erasesize = type->erasesize;
  2649. mtd->writesize = type->pagesize;
  2650. mtd->oobsize = mtd->writesize / 32;
  2651. busw = type->options & NAND_BUSWIDTH_16;
  2652. /*
  2653. * Check for Spansion/AMD ID + repeating 5th, 6th byte since
  2654. * some Spansion chips have erasesize that conflicts with size
  2655. * listed in nand_ids table.
  2656. * Data sheet (5 byte ID): Spansion S30ML-P ORNAND (p.39)
  2657. */
  2658. if (*maf_id == NAND_MFR_AMD && id_data[4] != 0x00 &&
  2659. id_data[5] == 0x00 && id_data[6] == 0x00 &&
  2660. id_data[7] == 0x00 && mtd->writesize == 512) {
  2661. mtd->erasesize = 128 * 1024;
  2662. mtd->erasesize <<= ((id_data[3] & 0x03) << 1);
  2663. }
  2664. }
  2665. /* Get chip options, preserve non chip based options */
  2666. chip->options &= ~NAND_CHIPOPTIONS_MSK;
  2667. chip->options |= type->options & NAND_CHIPOPTIONS_MSK;
  2668. /*
  2669. * Check if chip is not a Samsung device. Do not clear the
  2670. * options for chips which do not have an extended id.
  2671. */
  2672. if (*maf_id != NAND_MFR_SAMSUNG && !type->pagesize)
  2673. chip->options &= ~NAND_SAMSUNG_LP_OPTIONS;
  2674. ident_done:
  2675. /*
  2676. * Set chip as a default. Board drivers can override it, if necessary.
  2677. */
  2678. chip->options |= NAND_NO_AUTOINCR;
  2679. /* Try to identify manufacturer */
  2680. for (maf_idx = 0; nand_manuf_ids[maf_idx].id != 0x0; maf_idx++) {
  2681. if (nand_manuf_ids[maf_idx].id == *maf_id)
  2682. break;
  2683. }
  2684. /*
  2685. * Check, if buswidth is correct. Hardware drivers should set
  2686. * chip correct!
  2687. */
  2688. if (busw != (chip->options & NAND_BUSWIDTH_16)) {
  2689. pr_info("NAND device: Manufacturer ID:"
  2690. " 0x%02x, Chip ID: 0x%02x (%s %s)\n", *maf_id,
  2691. *dev_id, nand_manuf_ids[maf_idx].name, mtd->name);
  2692. pr_warn("NAND bus width %d instead %d bit\n",
  2693. (chip->options & NAND_BUSWIDTH_16) ? 16 : 8,
  2694. busw ? 16 : 8);
  2695. return ERR_PTR(-EINVAL);
  2696. }
  2697. /* Calculate the address shift from the page size */
  2698. chip->page_shift = ffs(mtd->writesize) - 1;
  2699. /* Convert chipsize to number of pages per chip -1 */
  2700. chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
  2701. chip->bbt_erase_shift = chip->phys_erase_shift =
  2702. ffs(mtd->erasesize) - 1;
  2703. if (chip->chipsize & 0xffffffff)
  2704. chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
  2705. else {
  2706. chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32));
  2707. chip->chip_shift += 32 - 1;
  2708. }
  2709. chip->badblockbits = 8;
  2710. /* Set the bad block position */
  2711. if (mtd->writesize > 512 || (busw & NAND_BUSWIDTH_16))
  2712. chip->badblockpos = NAND_LARGE_BADBLOCK_POS;
  2713. else
  2714. chip->badblockpos = NAND_SMALL_BADBLOCK_POS;
  2715. /*
  2716. * Bad block marker is stored in the last page of each block
  2717. * on Samsung and Hynix MLC devices; stored in first two pages
  2718. * of each block on Micron devices with 2KiB pages and on
  2719. * SLC Samsung, Hynix, Toshiba, AMD/Spansion, and Macronix.
  2720. * All others scan only the first page.
  2721. */
  2722. if ((chip->cellinfo & NAND_CI_CELLTYPE_MSK) &&
  2723. (*maf_id == NAND_MFR_SAMSUNG ||
  2724. *maf_id == NAND_MFR_HYNIX))
  2725. chip->bbt_options |= NAND_BBT_SCANLASTPAGE;
  2726. else if ((!(chip->cellinfo & NAND_CI_CELLTYPE_MSK) &&
  2727. (*maf_id == NAND_MFR_SAMSUNG ||
  2728. *maf_id == NAND_MFR_HYNIX ||
  2729. *maf_id == NAND_MFR_TOSHIBA ||
  2730. *maf_id == NAND_MFR_AMD ||
  2731. *maf_id == NAND_MFR_MACRONIX)) ||
  2732. (mtd->writesize == 2048 &&
  2733. *maf_id == NAND_MFR_MICRON))
  2734. chip->bbt_options |= NAND_BBT_SCAN2NDPAGE;
  2735. /* Check for AND chips with 4 page planes */
  2736. if (chip->options & NAND_4PAGE_ARRAY)
  2737. chip->erase_cmd = multi_erase_cmd;
  2738. else
  2739. chip->erase_cmd = single_erase_cmd;
  2740. /* Do not replace user supplied command function! */
  2741. if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
  2742. chip->cmdfunc = nand_command_lp;
  2743. pr_info("NAND device: Manufacturer ID: 0x%02x, Chip ID: 0x%02x (%s %s),"
  2744. " page size: %d, OOB size: %d\n",
  2745. *maf_id, *dev_id, nand_manuf_ids[maf_idx].name,
  2746. chip->onfi_version ? chip->onfi_params.model : type->name,
  2747. mtd->writesize, mtd->oobsize);
  2748. return type;
  2749. }
  2750. /**
  2751. * nand_scan_ident - [NAND Interface] Scan for the NAND device
  2752. * @mtd: MTD device structure
  2753. * @maxchips: number of chips to scan for
  2754. * @table: alternative NAND ID table
  2755. *
  2756. * This is the first phase of the normal nand_scan() function. It reads the
  2757. * flash ID and sets up MTD fields accordingly.
  2758. *
  2759. * The mtd->owner field must be set to the module of the caller.
  2760. */
  2761. int nand_scan_ident(struct mtd_info *mtd, int maxchips,
  2762. struct nand_flash_dev *table)
  2763. {
  2764. int i, busw, nand_maf_id, nand_dev_id;
  2765. struct nand_chip *chip = mtd->priv;
  2766. struct nand_flash_dev *type;
  2767. /* Get buswidth to select the correct functions */
  2768. busw = chip->options & NAND_BUSWIDTH_16;
  2769. /* Set the default functions */
  2770. nand_set_defaults(chip, busw);
  2771. /* Read the flash type */
  2772. type = nand_get_flash_type(mtd, chip, busw,
  2773. &nand_maf_id, &nand_dev_id, table);
  2774. if (IS_ERR(type)) {
  2775. if (!(chip->options & NAND_SCAN_SILENT_NODEV))
  2776. pr_warn("No NAND device found\n");
  2777. chip->select_chip(mtd, -1);
  2778. return PTR_ERR(type);
  2779. }
  2780. /* Check for a chip array */
  2781. for (i = 1; i < maxchips; i++) {
  2782. chip->select_chip(mtd, i);
  2783. /* See comment in nand_get_flash_type for reset */
  2784. chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
  2785. /* Send the command for reading device ID */
  2786. chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
  2787. /* Read manufacturer and device IDs */
  2788. if (nand_maf_id != chip->read_byte(mtd) ||
  2789. nand_dev_id != chip->read_byte(mtd))
  2790. break;
  2791. }
  2792. if (i > 1)
  2793. pr_info("%d NAND chips detected\n", i);
  2794. /* Store the number of chips and calc total size for mtd */
  2795. chip->numchips = i;
  2796. mtd->size = i * chip->chipsize;
  2797. return 0;
  2798. }
  2799. EXPORT_SYMBOL(nand_scan_ident);
  2800. /**
  2801. * nand_scan_tail - [NAND Interface] Scan for the NAND device
  2802. * @mtd: MTD device structure
  2803. *
  2804. * This is the second phase of the normal nand_scan() function. It fills out
  2805. * all the uninitialized function pointers with the defaults and scans for a
  2806. * bad block table if appropriate.
  2807. */
  2808. int nand_scan_tail(struct mtd_info *mtd)
  2809. {
  2810. int i;
  2811. struct nand_chip *chip = mtd->priv;
  2812. /* New bad blocks should be marked in OOB, flash-based BBT, or both */
  2813. BUG_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) &&
  2814. !(chip->bbt_options & NAND_BBT_USE_FLASH));
  2815. if (!(chip->options & NAND_OWN_BUFFERS))
  2816. chip->buffers = kmalloc(sizeof(*chip->buffers), GFP_KERNEL);
  2817. if (!chip->buffers)
  2818. return -ENOMEM;
  2819. /* Set the internal oob buffer location, just after the page data */
  2820. chip->oob_poi = chip->buffers->databuf + mtd->writesize;
  2821. /*
  2822. * If no default placement scheme is given, select an appropriate one.
  2823. */
  2824. if (!chip->ecc.layout && (chip->ecc.mode != NAND_ECC_SOFT_BCH)) {
  2825. switch (mtd->oobsize) {
  2826. case 8:
  2827. chip->ecc.layout = &nand_oob_8;
  2828. break;
  2829. case 16:
  2830. chip->ecc.layout = &nand_oob_16;
  2831. break;
  2832. case 64:
  2833. chip->ecc.layout = &nand_oob_64;
  2834. break;
  2835. case 128:
  2836. chip->ecc.layout = &nand_oob_128;
  2837. break;
  2838. default:
  2839. pr_warn("No oob scheme defined for oobsize %d\n",
  2840. mtd->oobsize);
  2841. BUG();
  2842. }
  2843. }
  2844. if (!chip->write_page)
  2845. chip->write_page = nand_write_page;
  2846. /*
  2847. * Check ECC mode, default to software if 3byte/512byte hardware ECC is
  2848. * selected and we have 256 byte pagesize fallback to software ECC
  2849. */
  2850. switch (chip->ecc.mode) {
  2851. case NAND_ECC_HW_OOB_FIRST:
  2852. /* Similar to NAND_ECC_HW, but a separate read_page handle */
  2853. if (!chip->ecc.calculate || !chip->ecc.correct ||
  2854. !chip->ecc.hwctl) {
  2855. pr_warn("No ECC functions supplied; "
  2856. "hardware ECC not possible\n");
  2857. BUG();
  2858. }
  2859. if (!chip->ecc.read_page)
  2860. chip->ecc.read_page = nand_read_page_hwecc_oob_first;
  2861. case NAND_ECC_HW:
  2862. /* Use standard hwecc read page function? */
  2863. if (!chip->ecc.read_page)
  2864. chip->ecc.read_page = nand_read_page_hwecc;
  2865. if (!chip->ecc.write_page)
  2866. chip->ecc.write_page = nand_write_page_hwecc;
  2867. if (!chip->ecc.read_page_raw)
  2868. chip->ecc.read_page_raw = nand_read_page_raw;
  2869. if (!chip->ecc.write_page_raw)
  2870. chip->ecc.write_page_raw = nand_write_page_raw;
  2871. if (!chip->ecc.read_oob)
  2872. chip->ecc.read_oob = nand_read_oob_std;
  2873. if (!chip->ecc.write_oob)
  2874. chip->ecc.write_oob = nand_write_oob_std;
  2875. case NAND_ECC_HW_SYNDROME:
  2876. if ((!chip->ecc.calculate || !chip->ecc.correct ||
  2877. !chip->ecc.hwctl) &&
  2878. (!chip->ecc.read_page ||
  2879. chip->ecc.read_page == nand_read_page_hwecc ||
  2880. !chip->ecc.write_page ||
  2881. chip->ecc.write_page == nand_write_page_hwecc)) {
  2882. pr_warn("No ECC functions supplied; "
  2883. "hardware ECC not possible\n");
  2884. BUG();
  2885. }
  2886. /* Use standard syndrome read/write page function? */
  2887. if (!chip->ecc.read_page)
  2888. chip->ecc.read_page = nand_read_page_syndrome;
  2889. if (!chip->ecc.write_page)
  2890. chip->ecc.write_page = nand_write_page_syndrome;
  2891. if (!chip->ecc.read_page_raw)
  2892. chip->ecc.read_page_raw = nand_read_page_raw_syndrome;
  2893. if (!chip->ecc.write_page_raw)
  2894. chip->ecc.write_page_raw = nand_write_page_raw_syndrome;
  2895. if (!chip->ecc.read_oob)
  2896. chip->ecc.read_oob = nand_read_oob_syndrome;
  2897. if (!chip->ecc.write_oob)
  2898. chip->ecc.write_oob = nand_write_oob_syndrome;
  2899. if (mtd->writesize >= chip->ecc.size) {
  2900. if (!chip->ecc.strength) {
  2901. pr_warn("Driver must set ecc.strength when using hardware ECC\n");
  2902. BUG();
  2903. }
  2904. break;
  2905. }
  2906. pr_warn("%d byte HW ECC not possible on "
  2907. "%d byte page size, fallback to SW ECC\n",
  2908. chip->ecc.size, mtd->writesize);
  2909. chip->ecc.mode = NAND_ECC_SOFT;
  2910. case NAND_ECC_SOFT:
  2911. chip->ecc.calculate = nand_calculate_ecc;
  2912. chip->ecc.correct = nand_correct_data;
  2913. chip->ecc.read_page = nand_read_page_swecc;
  2914. chip->ecc.read_subpage = nand_read_subpage;
  2915. chip->ecc.write_page = nand_write_page_swecc;
  2916. chip->ecc.read_page_raw = nand_read_page_raw;
  2917. chip->ecc.write_page_raw = nand_write_page_raw;
  2918. chip->ecc.read_oob = nand_read_oob_std;
  2919. chip->ecc.write_oob = nand_write_oob_std;
  2920. if (!chip->ecc.size)
  2921. chip->ecc.size = 256;
  2922. chip->ecc.bytes = 3;
  2923. chip->ecc.strength = 1;
  2924. break;
  2925. case NAND_ECC_SOFT_BCH:
  2926. if (!mtd_nand_has_bch()) {
  2927. pr_warn("CONFIG_MTD_ECC_BCH not enabled\n");
  2928. BUG();
  2929. }
  2930. chip->ecc.calculate = nand_bch_calculate_ecc;
  2931. chip->ecc.correct = nand_bch_correct_data;
  2932. chip->ecc.read_page = nand_read_page_swecc;
  2933. chip->ecc.read_subpage = nand_read_subpage;
  2934. chip->ecc.write_page = nand_write_page_swecc;
  2935. chip->ecc.read_page_raw = nand_read_page_raw;
  2936. chip->ecc.write_page_raw = nand_write_page_raw;
  2937. chip->ecc.read_oob = nand_read_oob_std;
  2938. chip->ecc.write_oob = nand_write_oob_std;
  2939. /*
  2940. * Board driver should supply ecc.size and ecc.bytes values to
  2941. * select how many bits are correctable; see nand_bch_init()
  2942. * for details. Otherwise, default to 4 bits for large page
  2943. * devices.
  2944. */
  2945. if (!chip->ecc.size && (mtd->oobsize >= 64)) {
  2946. chip->ecc.size = 512;
  2947. chip->ecc.bytes = 7;
  2948. }
  2949. chip->ecc.priv = nand_bch_init(mtd,
  2950. chip->ecc.size,
  2951. chip->ecc.bytes,
  2952. &chip->ecc.layout);
  2953. if (!chip->ecc.priv) {
  2954. pr_warn("BCH ECC initialization failed!\n");
  2955. BUG();
  2956. }
  2957. chip->ecc.strength =
  2958. chip->ecc.bytes * 8 / fls(8 * chip->ecc.size);
  2959. break;
  2960. case NAND_ECC_NONE:
  2961. pr_warn("NAND_ECC_NONE selected by board driver. "
  2962. "This is not recommended!\n");
  2963. chip->ecc.read_page = nand_read_page_raw;
  2964. chip->ecc.write_page = nand_write_page_raw;
  2965. chip->ecc.read_oob = nand_read_oob_std;
  2966. chip->ecc.read_page_raw = nand_read_page_raw;
  2967. chip->ecc.write_page_raw = nand_write_page_raw;
  2968. chip->ecc.write_oob = nand_write_oob_std;
  2969. chip->ecc.size = mtd->writesize;
  2970. chip->ecc.bytes = 0;
  2971. chip->ecc.strength = 0;
  2972. break;
  2973. default:
  2974. pr_warn("Invalid NAND_ECC_MODE %d\n", chip->ecc.mode);
  2975. BUG();
  2976. }
  2977. /* For many systems, the standard OOB write also works for raw */
  2978. if (!chip->ecc.read_oob_raw)
  2979. chip->ecc.read_oob_raw = chip->ecc.read_oob;
  2980. if (!chip->ecc.write_oob_raw)
  2981. chip->ecc.write_oob_raw = chip->ecc.write_oob;
  2982. /*
  2983. * The number of bytes available for a client to place data into
  2984. * the out of band area.
  2985. */
  2986. chip->ecc.layout->oobavail = 0;
  2987. for (i = 0; chip->ecc.layout->oobfree[i].length
  2988. && i < ARRAY_SIZE(chip->ecc.layout->oobfree); i++)
  2989. chip->ecc.layout->oobavail +=
  2990. chip->ecc.layout->oobfree[i].length;
  2991. mtd->oobavail = chip->ecc.layout->oobavail;
  2992. /*
  2993. * Set the number of read / write steps for one page depending on ECC
  2994. * mode.
  2995. */
  2996. chip->ecc.steps = mtd->writesize / chip->ecc.size;
  2997. if (chip->ecc.steps * chip->ecc.size != mtd->writesize) {
  2998. pr_warn("Invalid ECC parameters\n");
  2999. BUG();
  3000. }
  3001. chip->ecc.total = chip->ecc.steps * chip->ecc.bytes;
  3002. /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */
  3003. if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
  3004. !(chip->cellinfo & NAND_CI_CELLTYPE_MSK)) {
  3005. switch (chip->ecc.steps) {
  3006. case 2:
  3007. mtd->subpage_sft = 1;
  3008. break;
  3009. case 4:
  3010. case 8:
  3011. case 16:
  3012. mtd->subpage_sft = 2;
  3013. break;
  3014. }
  3015. }
  3016. chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
  3017. /* Initialize state */
  3018. chip->state = FL_READY;
  3019. /* De-select the device */
  3020. chip->select_chip(mtd, -1);
  3021. /* Invalidate the pagebuffer reference */
  3022. chip->pagebuf = -1;
  3023. /* Fill in remaining MTD driver data */
  3024. mtd->type = MTD_NANDFLASH;
  3025. mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM :
  3026. MTD_CAP_NANDFLASH;
  3027. mtd->_erase = nand_erase;
  3028. mtd->_point = NULL;
  3029. mtd->_unpoint = NULL;
  3030. mtd->_read = nand_read;
  3031. mtd->_write = nand_write;
  3032. mtd->_panic_write = panic_nand_write;
  3033. mtd->_read_oob = nand_read_oob;
  3034. mtd->_write_oob = nand_write_oob;
  3035. mtd->_sync = nand_sync;
  3036. mtd->_lock = NULL;
  3037. mtd->_unlock = NULL;
  3038. mtd->_suspend = nand_suspend;
  3039. mtd->_resume = nand_resume;
  3040. mtd->_block_isbad = nand_block_isbad;
  3041. mtd->_block_markbad = nand_block_markbad;
  3042. mtd->writebufsize = mtd->writesize;
  3043. /* propagate ecc info to mtd_info */
  3044. mtd->ecclayout = chip->ecc.layout;
  3045. mtd->ecc_strength = chip->ecc.strength;
  3046. /* Check, if we should skip the bad block table scan */
  3047. if (chip->options & NAND_SKIP_BBTSCAN)
  3048. return 0;
  3049. /* Build bad block table */
  3050. return chip->scan_bbt(mtd);
  3051. }
  3052. EXPORT_SYMBOL(nand_scan_tail);
  3053. /*
  3054. * is_module_text_address() isn't exported, and it's mostly a pointless
  3055. * test if this is a module _anyway_ -- they'd have to try _really_ hard
  3056. * to call us from in-kernel code if the core NAND support is modular.
  3057. */
  3058. #ifdef MODULE
  3059. #define caller_is_module() (1)
  3060. #else
  3061. #define caller_is_module() \
  3062. is_module_text_address((unsigned long)__builtin_return_address(0))
  3063. #endif
  3064. /**
  3065. * nand_scan - [NAND Interface] Scan for the NAND device
  3066. * @mtd: MTD device structure
  3067. * @maxchips: number of chips to scan for
  3068. *
  3069. * This fills out all the uninitialized function pointers with the defaults.
  3070. * The flash ID is read and the mtd/chip structures are filled with the
  3071. * appropriate values. The mtd->owner field must be set to the module of the
  3072. * caller.
  3073. */
  3074. int nand_scan(struct mtd_info *mtd, int maxchips)
  3075. {
  3076. int ret;
  3077. /* Many callers got this wrong, so check for it for a while... */
  3078. if (!mtd->owner && caller_is_module()) {
  3079. pr_crit("%s called with NULL mtd->owner!\n", __func__);
  3080. BUG();
  3081. }
  3082. ret = nand_scan_ident(mtd, maxchips, NULL);
  3083. if (!ret)
  3084. ret = nand_scan_tail(mtd);
  3085. return ret;
  3086. }
  3087. EXPORT_SYMBOL(nand_scan);
  3088. /**
  3089. * nand_release - [NAND Interface] Free resources held by the NAND device
  3090. * @mtd: MTD device structure
  3091. */
  3092. void nand_release(struct mtd_info *mtd)
  3093. {
  3094. struct nand_chip *chip = mtd->priv;
  3095. if (chip->ecc.mode == NAND_ECC_SOFT_BCH)
  3096. nand_bch_free((struct nand_bch_control *)chip->ecc.priv);
  3097. mtd_device_unregister(mtd);
  3098. /* Free bad block table memory */
  3099. kfree(chip->bbt);
  3100. if (!(chip->options & NAND_OWN_BUFFERS))
  3101. kfree(chip->buffers);
  3102. /* Free bad block descriptor memory */
  3103. if (chip->badblock_pattern && chip->badblock_pattern->options
  3104. & NAND_BBT_DYNAMICSTRUCT)
  3105. kfree(chip->badblock_pattern);
  3106. }
  3107. EXPORT_SYMBOL_GPL(nand_release);
  3108. static int __init nand_base_init(void)
  3109. {
  3110. led_trigger_register_simple("nand-disk", &nand_led_trigger);
  3111. return 0;
  3112. }
  3113. static void __exit nand_base_exit(void)
  3114. {
  3115. led_trigger_unregister_simple(nand_led_trigger);
  3116. }
  3117. module_init(nand_base_init);
  3118. module_exit(nand_base_exit);
  3119. MODULE_LICENSE("GPL");
  3120. MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>");
  3121. MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>");
  3122. MODULE_DESCRIPTION("Generic NAND flash driver code");