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