nand_base.c 96 KB

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