nand_base.c 98 KB

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