nand_base.c 97 KB

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