nand_base.c 91 KB

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