nand_base.c 97 KB

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