nand_base.c 91 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546
  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. DEBUG(MTD_DEBUG_LEVEL0, "%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. DEBUG(MTD_DEBUG_LEVEL0, "%s: Length not block aligned\n",
  112. __func__);
  113. ret = -EINVAL;
  114. }
  115. /* Do not allow past end of device */
  116. if (ofs + len > mtd->size) {
  117. DEBUG(MTD_DEBUG_LEVEL0, "%s: Past end of device\n",
  118. __func__);
  119. ret = -EINVAL;
  120. }
  121. return ret;
  122. }
  123. /**
  124. * nand_release_device - [GENERIC] release chip
  125. * @mtd: MTD device structure
  126. *
  127. * Deselect, release chip lock and wake up anyone waiting on the device.
  128. */
  129. static void nand_release_device(struct mtd_info *mtd)
  130. {
  131. struct nand_chip *chip = mtd->priv;
  132. /* De-select the NAND device */
  133. chip->select_chip(mtd, -1);
  134. /* Release the controller and the chip */
  135. spin_lock(&chip->controller->lock);
  136. chip->controller->active = NULL;
  137. chip->state = FL_READY;
  138. wake_up(&chip->controller->wq);
  139. spin_unlock(&chip->controller->lock);
  140. }
  141. /**
  142. * nand_read_byte - [DEFAULT] read one byte from the chip
  143. * @mtd: MTD device structure
  144. *
  145. * Default read function for 8bit buswidth
  146. */
  147. static uint8_t nand_read_byte(struct mtd_info *mtd)
  148. {
  149. struct nand_chip *chip = mtd->priv;
  150. return readb(chip->IO_ADDR_R);
  151. }
  152. /**
  153. * nand_read_byte16 - [DEFAULT] read one byte endianess aware from the chip
  154. * nand_read_byte16 - [DEFAULT] read one byte endianness aware from the chip
  155. * @mtd: MTD device structure
  156. *
  157. * Default read function for 16bit buswidth with endianness conversion.
  158. *
  159. */
  160. static uint8_t nand_read_byte16(struct mtd_info *mtd)
  161. {
  162. struct nand_chip *chip = mtd->priv;
  163. return (uint8_t) cpu_to_le16(readw(chip->IO_ADDR_R));
  164. }
  165. /**
  166. * nand_read_word - [DEFAULT] read one word from the chip
  167. * @mtd: MTD device structure
  168. *
  169. * Default read function for 16bit buswidth without endianness conversion.
  170. */
  171. static u16 nand_read_word(struct mtd_info *mtd)
  172. {
  173. struct nand_chip *chip = mtd->priv;
  174. return readw(chip->IO_ADDR_R);
  175. }
  176. /**
  177. * nand_select_chip - [DEFAULT] control CE line
  178. * @mtd: MTD device structure
  179. * @chipnr: chipnumber to select, -1 for deselect
  180. *
  181. * Default select function for 1 chip devices.
  182. */
  183. static void nand_select_chip(struct mtd_info *mtd, int chipnr)
  184. {
  185. struct nand_chip *chip = mtd->priv;
  186. switch (chipnr) {
  187. case -1:
  188. chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
  189. break;
  190. case 0:
  191. break;
  192. default:
  193. BUG();
  194. }
  195. }
  196. /**
  197. * nand_write_buf - [DEFAULT] write buffer to chip
  198. * @mtd: MTD device structure
  199. * @buf: data buffer
  200. * @len: number of bytes to write
  201. *
  202. * Default write function for 8bit buswidth.
  203. */
  204. static void nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
  205. {
  206. int i;
  207. struct nand_chip *chip = mtd->priv;
  208. for (i = 0; i < len; i++)
  209. writeb(buf[i], chip->IO_ADDR_W);
  210. }
  211. /**
  212. * nand_read_buf - [DEFAULT] read chip data into buffer
  213. * @mtd: MTD device structure
  214. * @buf: buffer to store date
  215. * @len: number of bytes to read
  216. *
  217. * Default read function for 8bit buswidth.
  218. */
  219. static void nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
  220. {
  221. int i;
  222. struct nand_chip *chip = mtd->priv;
  223. for (i = 0; i < len; i++)
  224. buf[i] = readb(chip->IO_ADDR_R);
  225. }
  226. /**
  227. * nand_verify_buf - [DEFAULT] Verify chip data against buffer
  228. * @mtd: MTD device structure
  229. * @buf: buffer containing the data to compare
  230. * @len: number of bytes to compare
  231. *
  232. * Default verify function for 8bit buswidth.
  233. */
  234. static int nand_verify_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
  235. {
  236. int i;
  237. struct nand_chip *chip = mtd->priv;
  238. for (i = 0; i < len; i++)
  239. if (buf[i] != readb(chip->IO_ADDR_R))
  240. return -EFAULT;
  241. return 0;
  242. }
  243. /**
  244. * nand_write_buf16 - [DEFAULT] write buffer to chip
  245. * @mtd: MTD device structure
  246. * @buf: data buffer
  247. * @len: number of bytes to write
  248. *
  249. * Default write function for 16bit buswidth.
  250. */
  251. static void nand_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
  252. {
  253. int i;
  254. struct nand_chip *chip = mtd->priv;
  255. u16 *p = (u16 *) buf;
  256. len >>= 1;
  257. for (i = 0; i < len; i++)
  258. writew(p[i], chip->IO_ADDR_W);
  259. }
  260. /**
  261. * nand_read_buf16 - [DEFAULT] read chip data into buffer
  262. * @mtd: MTD device structure
  263. * @buf: buffer to store date
  264. * @len: number of bytes to read
  265. *
  266. * Default read function for 16bit buswidth.
  267. */
  268. static void nand_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len)
  269. {
  270. int i;
  271. struct nand_chip *chip = mtd->priv;
  272. u16 *p = (u16 *) buf;
  273. len >>= 1;
  274. for (i = 0; i < len; i++)
  275. p[i] = readw(chip->IO_ADDR_R);
  276. }
  277. /**
  278. * nand_verify_buf16 - [DEFAULT] Verify chip data against buffer
  279. * @mtd: MTD device structure
  280. * @buf: buffer containing the data to compare
  281. * @len: number of bytes to compare
  282. *
  283. * Default verify function for 16bit buswidth.
  284. */
  285. static int nand_verify_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
  286. {
  287. int i;
  288. struct nand_chip *chip = mtd->priv;
  289. u16 *p = (u16 *) buf;
  290. len >>= 1;
  291. for (i = 0; i < len; i++)
  292. if (p[i] != readw(chip->IO_ADDR_R))
  293. return -EFAULT;
  294. return 0;
  295. }
  296. /**
  297. * nand_block_bad - [DEFAULT] Read bad block marker from the chip
  298. * @mtd: MTD device structure
  299. * @ofs: offset from device start
  300. * @getchip: 0, if the chip is already selected
  301. *
  302. * Check, if the block is bad.
  303. */
  304. static int nand_block_bad(struct mtd_info *mtd, loff_t ofs, int getchip)
  305. {
  306. int page, chipnr, res = 0;
  307. struct nand_chip *chip = mtd->priv;
  308. u16 bad;
  309. if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
  310. ofs += mtd->erasesize - mtd->writesize;
  311. page = (int)(ofs >> chip->page_shift) & chip->pagemask;
  312. if (getchip) {
  313. chipnr = (int)(ofs >> chip->chip_shift);
  314. nand_get_device(chip, mtd, FL_READING);
  315. /* Select the NAND device */
  316. chip->select_chip(mtd, chipnr);
  317. }
  318. if (chip->options & NAND_BUSWIDTH_16) {
  319. chip->cmdfunc(mtd, NAND_CMD_READOOB, chip->badblockpos & 0xFE,
  320. page);
  321. bad = cpu_to_le16(chip->read_word(mtd));
  322. if (chip->badblockpos & 0x1)
  323. bad >>= 8;
  324. else
  325. bad &= 0xFF;
  326. } else {
  327. chip->cmdfunc(mtd, NAND_CMD_READOOB, chip->badblockpos, page);
  328. bad = chip->read_byte(mtd);
  329. }
  330. if (likely(chip->badblockbits == 8))
  331. res = bad != 0xFF;
  332. else
  333. res = hweight8(bad) < chip->badblockbits;
  334. if (getchip)
  335. nand_release_device(mtd);
  336. return res;
  337. }
  338. /**
  339. * nand_default_block_markbad - [DEFAULT] mark a block bad
  340. * @mtd: MTD device structure
  341. * @ofs: offset from device start
  342. *
  343. * This is the default implementation, which can be overridden by a hardware
  344. * specific driver.
  345. */
  346. static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
  347. {
  348. struct nand_chip *chip = mtd->priv;
  349. uint8_t buf[2] = { 0, 0 };
  350. int block, ret, i = 0;
  351. if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
  352. ofs += mtd->erasesize - mtd->writesize;
  353. /* Get block number */
  354. block = (int)(ofs >> chip->bbt_erase_shift);
  355. if (chip->bbt)
  356. chip->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1);
  357. /* Do we have a flash based bad block table? */
  358. if (chip->bbt_options & NAND_BBT_USE_FLASH)
  359. ret = nand_update_bbt(mtd, ofs);
  360. else {
  361. nand_get_device(chip, mtd, FL_WRITING);
  362. /*
  363. * Write to first two pages if necessary. If we write to more
  364. * than one location, the first error encountered quits the
  365. * procedure. We write two bytes per location, so we dont have
  366. * to mess with 16 bit access.
  367. */
  368. do {
  369. chip->ops.len = chip->ops.ooblen = 2;
  370. chip->ops.datbuf = NULL;
  371. chip->ops.oobbuf = buf;
  372. chip->ops.ooboffs = chip->badblockpos & ~0x01;
  373. ret = nand_do_write_oob(mtd, ofs, &chip->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. DEBUG(MTD_DEBUG_LEVEL0, "%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. DEBUG(MTD_DEBUG_LEVEL3, "%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. DEBUG(MTD_DEBUG_LEVEL0, "%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. DEBUG(MTD_DEBUG_LEVEL3, "%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. DEBUG(MTD_DEBUG_LEVEL0, "%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. DEBUG(MTD_DEBUG_LEVEL0, "%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_OOB_PLACE:
  1207. case MTD_OOB_RAW:
  1208. memcpy(oob, chip->oob_poi + ops->ooboffs, len);
  1209. return oob + len;
  1210. case MTD_OOB_AUTO: {
  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_OOB_AUTO ?
  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_OOB_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. break;
  1291. /* Transfer not aligned data */
  1292. if (!aligned) {
  1293. if (!NAND_SUBPAGE_READ(chip) && !oob &&
  1294. !(mtd->ecc_stats.failed - stats.failed))
  1295. chip->pagebuf = realpage;
  1296. memcpy(buf, chip->buffers->databuf + col, bytes);
  1297. }
  1298. buf += bytes;
  1299. if (unlikely(oob)) {
  1300. int toread = min(oobreadlen, max_oobsize);
  1301. if (toread) {
  1302. oob = nand_transfer_oob(chip,
  1303. oob, ops, toread);
  1304. oobreadlen -= toread;
  1305. }
  1306. }
  1307. if (!(chip->options & NAND_NO_READRDY)) {
  1308. /*
  1309. * Apply delay or wait for ready/busy pin. Do
  1310. * this before the AUTOINCR check, so no
  1311. * problems arise if a chip which does auto
  1312. * increment is marked as NOAUTOINCR by the
  1313. * board driver.
  1314. */
  1315. if (!chip->dev_ready)
  1316. udelay(chip->chip_delay);
  1317. else
  1318. nand_wait_ready(mtd);
  1319. }
  1320. } else {
  1321. memcpy(buf, chip->buffers->databuf + col, bytes);
  1322. buf += bytes;
  1323. }
  1324. readlen -= bytes;
  1325. if (!readlen)
  1326. break;
  1327. /* For subsequent reads align to page boundary */
  1328. col = 0;
  1329. /* Increment page address */
  1330. realpage++;
  1331. page = realpage & chip->pagemask;
  1332. /* Check, if we cross a chip boundary */
  1333. if (!page) {
  1334. chipnr++;
  1335. chip->select_chip(mtd, -1);
  1336. chip->select_chip(mtd, chipnr);
  1337. }
  1338. /*
  1339. * Check, if the chip supports auto page increment or if we
  1340. * have hit a block boundary.
  1341. */
  1342. if (!NAND_CANAUTOINCR(chip) || !(page & blkcheck))
  1343. sndcmd = 1;
  1344. }
  1345. ops->retlen = ops->len - (size_t) readlen;
  1346. if (oob)
  1347. ops->oobretlen = ops->ooblen - oobreadlen;
  1348. if (ret)
  1349. return ret;
  1350. if (mtd->ecc_stats.failed - stats.failed)
  1351. return -EBADMSG;
  1352. return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
  1353. }
  1354. /**
  1355. * nand_read - [MTD Interface] MTD compatibility function for nand_do_read_ecc
  1356. * @mtd: MTD device structure
  1357. * @from: offset to read from
  1358. * @len: number of bytes to read
  1359. * @retlen: pointer to variable to store the number of read bytes
  1360. * @buf: the databuffer to put data
  1361. *
  1362. * Get hold of the chip and call nand_do_read.
  1363. */
  1364. static int nand_read(struct mtd_info *mtd, loff_t from, size_t len,
  1365. size_t *retlen, uint8_t *buf)
  1366. {
  1367. struct nand_chip *chip = mtd->priv;
  1368. int ret;
  1369. /* Do not allow reads past end of device */
  1370. if ((from + len) > mtd->size)
  1371. return -EINVAL;
  1372. if (!len)
  1373. return 0;
  1374. nand_get_device(chip, mtd, FL_READING);
  1375. chip->ops.len = len;
  1376. chip->ops.datbuf = buf;
  1377. chip->ops.oobbuf = NULL;
  1378. ret = nand_do_read_ops(mtd, from, &chip->ops);
  1379. *retlen = chip->ops.retlen;
  1380. nand_release_device(mtd);
  1381. return ret;
  1382. }
  1383. /**
  1384. * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function
  1385. * @mtd: mtd info structure
  1386. * @chip: nand chip info structure
  1387. * @page: page number to read
  1388. * @sndcmd: flag whether to issue read command or not
  1389. */
  1390. static int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
  1391. int page, int sndcmd)
  1392. {
  1393. if (sndcmd) {
  1394. chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
  1395. sndcmd = 0;
  1396. }
  1397. chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
  1398. return sndcmd;
  1399. }
  1400. /**
  1401. * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC
  1402. * with syndromes
  1403. * @mtd: mtd info structure
  1404. * @chip: nand chip info structure
  1405. * @page: page number to read
  1406. * @sndcmd: flag whether to issue read command or not
  1407. */
  1408. static int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
  1409. int page, int sndcmd)
  1410. {
  1411. uint8_t *buf = chip->oob_poi;
  1412. int length = mtd->oobsize;
  1413. int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
  1414. int eccsize = chip->ecc.size;
  1415. uint8_t *bufpoi = buf;
  1416. int i, toread, sndrnd = 0, pos;
  1417. chip->cmdfunc(mtd, NAND_CMD_READ0, chip->ecc.size, page);
  1418. for (i = 0; i < chip->ecc.steps; i++) {
  1419. if (sndrnd) {
  1420. pos = eccsize + i * (eccsize + chunk);
  1421. if (mtd->writesize > 512)
  1422. chip->cmdfunc(mtd, NAND_CMD_RNDOUT, pos, -1);
  1423. else
  1424. chip->cmdfunc(mtd, NAND_CMD_READ0, pos, page);
  1425. } else
  1426. sndrnd = 1;
  1427. toread = min_t(int, length, chunk);
  1428. chip->read_buf(mtd, bufpoi, toread);
  1429. bufpoi += toread;
  1430. length -= toread;
  1431. }
  1432. if (length > 0)
  1433. chip->read_buf(mtd, bufpoi, length);
  1434. return 1;
  1435. }
  1436. /**
  1437. * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function
  1438. * @mtd: mtd info structure
  1439. * @chip: nand chip info structure
  1440. * @page: page number to write
  1441. */
  1442. static int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
  1443. int page)
  1444. {
  1445. int status = 0;
  1446. const uint8_t *buf = chip->oob_poi;
  1447. int length = mtd->oobsize;
  1448. chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
  1449. chip->write_buf(mtd, buf, length);
  1450. /* Send command to program the OOB data */
  1451. chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
  1452. status = chip->waitfunc(mtd, chip);
  1453. return status & NAND_STATUS_FAIL ? -EIO : 0;
  1454. }
  1455. /**
  1456. * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC
  1457. * with syndrome - only for large page flash
  1458. * @mtd: mtd info structure
  1459. * @chip: nand chip info structure
  1460. * @page: page number to write
  1461. */
  1462. static int nand_write_oob_syndrome(struct mtd_info *mtd,
  1463. struct nand_chip *chip, int page)
  1464. {
  1465. int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
  1466. int eccsize = chip->ecc.size, length = mtd->oobsize;
  1467. int i, len, pos, status = 0, sndcmd = 0, steps = chip->ecc.steps;
  1468. const uint8_t *bufpoi = chip->oob_poi;
  1469. /*
  1470. * data-ecc-data-ecc ... ecc-oob
  1471. * or
  1472. * data-pad-ecc-pad-data-pad .... ecc-pad-oob
  1473. */
  1474. if (!chip->ecc.prepad && !chip->ecc.postpad) {
  1475. pos = steps * (eccsize + chunk);
  1476. steps = 0;
  1477. } else
  1478. pos = eccsize;
  1479. chip->cmdfunc(mtd, NAND_CMD_SEQIN, pos, page);
  1480. for (i = 0; i < steps; i++) {
  1481. if (sndcmd) {
  1482. if (mtd->writesize <= 512) {
  1483. uint32_t fill = 0xFFFFFFFF;
  1484. len = eccsize;
  1485. while (len > 0) {
  1486. int num = min_t(int, len, 4);
  1487. chip->write_buf(mtd, (uint8_t *)&fill,
  1488. num);
  1489. len -= num;
  1490. }
  1491. } else {
  1492. pos = eccsize + i * (eccsize + chunk);
  1493. chip->cmdfunc(mtd, NAND_CMD_RNDIN, pos, -1);
  1494. }
  1495. } else
  1496. sndcmd = 1;
  1497. len = min_t(int, length, chunk);
  1498. chip->write_buf(mtd, bufpoi, len);
  1499. bufpoi += len;
  1500. length -= len;
  1501. }
  1502. if (length > 0)
  1503. chip->write_buf(mtd, bufpoi, length);
  1504. chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
  1505. status = chip->waitfunc(mtd, chip);
  1506. return status & NAND_STATUS_FAIL ? -EIO : 0;
  1507. }
  1508. /**
  1509. * nand_do_read_oob - [INTERN] NAND read out-of-band
  1510. * @mtd: MTD device structure
  1511. * @from: offset to read from
  1512. * @ops: oob operations description structure
  1513. *
  1514. * NAND read out-of-band data from the spare area.
  1515. */
  1516. static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
  1517. struct mtd_oob_ops *ops)
  1518. {
  1519. int page, realpage, chipnr, sndcmd = 1;
  1520. struct nand_chip *chip = mtd->priv;
  1521. struct mtd_ecc_stats stats;
  1522. int blkcheck = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
  1523. int readlen = ops->ooblen;
  1524. int len;
  1525. uint8_t *buf = ops->oobbuf;
  1526. DEBUG(MTD_DEBUG_LEVEL3, "%s: from = 0x%08Lx, len = %i\n",
  1527. __func__, (unsigned long long)from, readlen);
  1528. stats = mtd->ecc_stats;
  1529. if (ops->mode == MTD_OOB_AUTO)
  1530. len = chip->ecc.layout->oobavail;
  1531. else
  1532. len = mtd->oobsize;
  1533. if (unlikely(ops->ooboffs >= len)) {
  1534. DEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt to start read "
  1535. "outside oob\n", __func__);
  1536. return -EINVAL;
  1537. }
  1538. /* Do not allow reads past end of device */
  1539. if (unlikely(from >= mtd->size ||
  1540. ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) -
  1541. (from >> chip->page_shift)) * len)) {
  1542. DEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt read beyond end "
  1543. "of device\n", __func__);
  1544. return -EINVAL;
  1545. }
  1546. chipnr = (int)(from >> chip->chip_shift);
  1547. chip->select_chip(mtd, chipnr);
  1548. /* Shift to get page */
  1549. realpage = (int)(from >> chip->page_shift);
  1550. page = realpage & chip->pagemask;
  1551. while (1) {
  1552. sndcmd = chip->ecc.read_oob(mtd, chip, page, sndcmd);
  1553. len = min(len, readlen);
  1554. buf = nand_transfer_oob(chip, buf, ops, len);
  1555. if (!(chip->options & NAND_NO_READRDY)) {
  1556. /*
  1557. * Apply delay or wait for ready/busy pin. Do this
  1558. * before the AUTOINCR check, so no problems arise if a
  1559. * chip which does auto increment is marked as
  1560. * NOAUTOINCR by the board driver.
  1561. */
  1562. if (!chip->dev_ready)
  1563. udelay(chip->chip_delay);
  1564. else
  1565. nand_wait_ready(mtd);
  1566. }
  1567. readlen -= len;
  1568. if (!readlen)
  1569. break;
  1570. /* Increment page address */
  1571. realpage++;
  1572. page = realpage & chip->pagemask;
  1573. /* Check, if we cross a chip boundary */
  1574. if (!page) {
  1575. chipnr++;
  1576. chip->select_chip(mtd, -1);
  1577. chip->select_chip(mtd, chipnr);
  1578. }
  1579. /*
  1580. * Check, if the chip supports auto page increment or if we
  1581. * have hit a block boundary.
  1582. */
  1583. if (!NAND_CANAUTOINCR(chip) || !(page & blkcheck))
  1584. sndcmd = 1;
  1585. }
  1586. ops->oobretlen = ops->ooblen;
  1587. if (mtd->ecc_stats.failed - stats.failed)
  1588. return -EBADMSG;
  1589. return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
  1590. }
  1591. /**
  1592. * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
  1593. * @mtd: MTD device structure
  1594. * @from: offset to read from
  1595. * @ops: oob operation description structure
  1596. *
  1597. * NAND read data and/or out-of-band data.
  1598. */
  1599. static int nand_read_oob(struct mtd_info *mtd, loff_t from,
  1600. struct mtd_oob_ops *ops)
  1601. {
  1602. struct nand_chip *chip = mtd->priv;
  1603. int ret = -ENOTSUPP;
  1604. ops->retlen = 0;
  1605. /* Do not allow reads past end of device */
  1606. if (ops->datbuf && (from + ops->len) > mtd->size) {
  1607. DEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt read "
  1608. "beyond end of device\n", __func__);
  1609. return -EINVAL;
  1610. }
  1611. nand_get_device(chip, mtd, FL_READING);
  1612. switch (ops->mode) {
  1613. case MTD_OOB_PLACE:
  1614. case MTD_OOB_AUTO:
  1615. case MTD_OOB_RAW:
  1616. break;
  1617. default:
  1618. goto out;
  1619. }
  1620. if (!ops->datbuf)
  1621. ret = nand_do_read_oob(mtd, from, ops);
  1622. else
  1623. ret = nand_do_read_ops(mtd, from, ops);
  1624. out:
  1625. nand_release_device(mtd);
  1626. return ret;
  1627. }
  1628. /**
  1629. * nand_write_page_raw - [INTERN] raw page write function
  1630. * @mtd: mtd info structure
  1631. * @chip: nand chip info structure
  1632. * @buf: data buffer
  1633. *
  1634. * Not for syndrome calculating ECC controllers, which use a special oob layout.
  1635. */
  1636. static void nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
  1637. const uint8_t *buf)
  1638. {
  1639. chip->write_buf(mtd, buf, mtd->writesize);
  1640. chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
  1641. }
  1642. /**
  1643. * nand_write_page_raw_syndrome - [INTERN] raw page write function
  1644. * @mtd: mtd info structure
  1645. * @chip: nand chip info structure
  1646. * @buf: data buffer
  1647. *
  1648. * We need a special oob layout and handling even when ECC isn't checked.
  1649. */
  1650. static void nand_write_page_raw_syndrome(struct mtd_info *mtd,
  1651. struct nand_chip *chip,
  1652. const uint8_t *buf)
  1653. {
  1654. int eccsize = chip->ecc.size;
  1655. int eccbytes = chip->ecc.bytes;
  1656. uint8_t *oob = chip->oob_poi;
  1657. int steps, size;
  1658. for (steps = chip->ecc.steps; steps > 0; steps--) {
  1659. chip->write_buf(mtd, buf, eccsize);
  1660. buf += eccsize;
  1661. if (chip->ecc.prepad) {
  1662. chip->write_buf(mtd, oob, chip->ecc.prepad);
  1663. oob += chip->ecc.prepad;
  1664. }
  1665. chip->read_buf(mtd, oob, eccbytes);
  1666. oob += eccbytes;
  1667. if (chip->ecc.postpad) {
  1668. chip->write_buf(mtd, oob, chip->ecc.postpad);
  1669. oob += chip->ecc.postpad;
  1670. }
  1671. }
  1672. size = mtd->oobsize - (oob - chip->oob_poi);
  1673. if (size)
  1674. chip->write_buf(mtd, oob, size);
  1675. }
  1676. /**
  1677. * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function
  1678. * @mtd: mtd info structure
  1679. * @chip: nand chip info structure
  1680. * @buf: data buffer
  1681. */
  1682. static void nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
  1683. const uint8_t *buf)
  1684. {
  1685. int i, eccsize = chip->ecc.size;
  1686. int eccbytes = chip->ecc.bytes;
  1687. int eccsteps = chip->ecc.steps;
  1688. uint8_t *ecc_calc = chip->buffers->ecccalc;
  1689. const uint8_t *p = buf;
  1690. uint32_t *eccpos = chip->ecc.layout->eccpos;
  1691. /* Software ECC calculation */
  1692. for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
  1693. chip->ecc.calculate(mtd, p, &ecc_calc[i]);
  1694. for (i = 0; i < chip->ecc.total; i++)
  1695. chip->oob_poi[eccpos[i]] = ecc_calc[i];
  1696. chip->ecc.write_page_raw(mtd, chip, buf);
  1697. }
  1698. /**
  1699. * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function
  1700. * @mtd: mtd info structure
  1701. * @chip: nand chip info structure
  1702. * @buf: data buffer
  1703. */
  1704. static void nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
  1705. const uint8_t *buf)
  1706. {
  1707. int i, eccsize = chip->ecc.size;
  1708. int eccbytes = chip->ecc.bytes;
  1709. int eccsteps = chip->ecc.steps;
  1710. uint8_t *ecc_calc = chip->buffers->ecccalc;
  1711. const uint8_t *p = buf;
  1712. uint32_t *eccpos = chip->ecc.layout->eccpos;
  1713. for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
  1714. chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
  1715. chip->write_buf(mtd, p, eccsize);
  1716. chip->ecc.calculate(mtd, p, &ecc_calc[i]);
  1717. }
  1718. for (i = 0; i < chip->ecc.total; i++)
  1719. chip->oob_poi[eccpos[i]] = ecc_calc[i];
  1720. chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
  1721. }
  1722. /**
  1723. * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write
  1724. * @mtd: mtd info structure
  1725. * @chip: nand chip info structure
  1726. * @buf: data buffer
  1727. *
  1728. * The hw generator calculates the error syndrome automatically. Therefore we
  1729. * need a special oob layout and handling.
  1730. */
  1731. static void nand_write_page_syndrome(struct mtd_info *mtd,
  1732. struct nand_chip *chip, const uint8_t *buf)
  1733. {
  1734. int i, eccsize = chip->ecc.size;
  1735. int eccbytes = chip->ecc.bytes;
  1736. int eccsteps = chip->ecc.steps;
  1737. const uint8_t *p = buf;
  1738. uint8_t *oob = chip->oob_poi;
  1739. for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
  1740. chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
  1741. chip->write_buf(mtd, p, eccsize);
  1742. if (chip->ecc.prepad) {
  1743. chip->write_buf(mtd, oob, chip->ecc.prepad);
  1744. oob += chip->ecc.prepad;
  1745. }
  1746. chip->ecc.calculate(mtd, p, oob);
  1747. chip->write_buf(mtd, oob, eccbytes);
  1748. oob += eccbytes;
  1749. if (chip->ecc.postpad) {
  1750. chip->write_buf(mtd, oob, chip->ecc.postpad);
  1751. oob += chip->ecc.postpad;
  1752. }
  1753. }
  1754. /* Calculate remaining oob bytes */
  1755. i = mtd->oobsize - (oob - chip->oob_poi);
  1756. if (i)
  1757. chip->write_buf(mtd, oob, i);
  1758. }
  1759. /**
  1760. * nand_write_page - [REPLACEABLE] write one page
  1761. * @mtd: MTD device structure
  1762. * @chip: NAND chip descriptor
  1763. * @buf: the data to write
  1764. * @page: page number to write
  1765. * @cached: cached programming
  1766. * @raw: use _raw version of write_page
  1767. */
  1768. static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
  1769. const uint8_t *buf, int page, int cached, int raw)
  1770. {
  1771. int status;
  1772. chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
  1773. if (unlikely(raw))
  1774. chip->ecc.write_page_raw(mtd, chip, buf);
  1775. else
  1776. chip->ecc.write_page(mtd, chip, buf);
  1777. /*
  1778. * Cached progamming disabled for now. Not sure if it's worth the
  1779. * trouble. The speed gain is not very impressive. (2.3->2.6Mib/s).
  1780. */
  1781. cached = 0;
  1782. if (!cached || !(chip->options & NAND_CACHEPRG)) {
  1783. chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
  1784. status = chip->waitfunc(mtd, chip);
  1785. /*
  1786. * See if operation failed and additional status checks are
  1787. * available.
  1788. */
  1789. if ((status & NAND_STATUS_FAIL) && (chip->errstat))
  1790. status = chip->errstat(mtd, chip, FL_WRITING, status,
  1791. page);
  1792. if (status & NAND_STATUS_FAIL)
  1793. return -EIO;
  1794. } else {
  1795. chip->cmdfunc(mtd, NAND_CMD_CACHEDPROG, -1, -1);
  1796. status = chip->waitfunc(mtd, chip);
  1797. }
  1798. #ifdef CONFIG_MTD_NAND_VERIFY_WRITE
  1799. /* Send command to read back the data */
  1800. chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
  1801. if (chip->verify_buf(mtd, buf, mtd->writesize))
  1802. return -EIO;
  1803. #endif
  1804. return 0;
  1805. }
  1806. /**
  1807. * nand_fill_oob - [INTERN] Transfer client buffer to oob
  1808. * @mtd: MTD device structure
  1809. * @oob: oob data buffer
  1810. * @len: oob data write length
  1811. * @ops: oob ops structure
  1812. */
  1813. static uint8_t *nand_fill_oob(struct mtd_info *mtd, uint8_t *oob, size_t len,
  1814. struct mtd_oob_ops *ops)
  1815. {
  1816. struct nand_chip *chip = mtd->priv;
  1817. /*
  1818. * Initialise to all 0xFF, to avoid the possibility of left over OOB
  1819. * data from a previous OOB read.
  1820. */
  1821. memset(chip->oob_poi, 0xff, mtd->oobsize);
  1822. switch (ops->mode) {
  1823. case MTD_OOB_PLACE:
  1824. case MTD_OOB_RAW:
  1825. memcpy(chip->oob_poi + ops->ooboffs, oob, len);
  1826. return oob + len;
  1827. case MTD_OOB_AUTO: {
  1828. struct nand_oobfree *free = chip->ecc.layout->oobfree;
  1829. uint32_t boffs = 0, woffs = ops->ooboffs;
  1830. size_t bytes = 0;
  1831. for (; free->length && len; free++, len -= bytes) {
  1832. /* Write request not from offset 0? */
  1833. if (unlikely(woffs)) {
  1834. if (woffs >= free->length) {
  1835. woffs -= free->length;
  1836. continue;
  1837. }
  1838. boffs = free->offset + woffs;
  1839. bytes = min_t(size_t, len,
  1840. (free->length - woffs));
  1841. woffs = 0;
  1842. } else {
  1843. bytes = min_t(size_t, len, free->length);
  1844. boffs = free->offset;
  1845. }
  1846. memcpy(chip->oob_poi + boffs, oob, bytes);
  1847. oob += bytes;
  1848. }
  1849. return oob;
  1850. }
  1851. default:
  1852. BUG();
  1853. }
  1854. return NULL;
  1855. }
  1856. #define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0)
  1857. /**
  1858. * nand_do_write_ops - [INTERN] NAND write with ECC
  1859. * @mtd: MTD device structure
  1860. * @to: offset to write to
  1861. * @ops: oob operations description structure
  1862. *
  1863. * NAND write with ECC.
  1864. */
  1865. static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
  1866. struct mtd_oob_ops *ops)
  1867. {
  1868. int chipnr, realpage, page, blockmask, column;
  1869. struct nand_chip *chip = mtd->priv;
  1870. uint32_t writelen = ops->len;
  1871. uint32_t oobwritelen = ops->ooblen;
  1872. uint32_t oobmaxlen = ops->mode == MTD_OOB_AUTO ?
  1873. mtd->oobavail : mtd->oobsize;
  1874. uint8_t *oob = ops->oobbuf;
  1875. uint8_t *buf = ops->datbuf;
  1876. int ret, subpage;
  1877. ops->retlen = 0;
  1878. if (!writelen)
  1879. return 0;
  1880. /* Reject writes, which are not page aligned */
  1881. if (NOTALIGNED(to) || NOTALIGNED(ops->len)) {
  1882. pr_notice("%s: attempt to write non page aligned data\n",
  1883. __func__);
  1884. return -EINVAL;
  1885. }
  1886. column = to & (mtd->writesize - 1);
  1887. subpage = column || (writelen & (mtd->writesize - 1));
  1888. if (subpage && oob)
  1889. return -EINVAL;
  1890. chipnr = (int)(to >> chip->chip_shift);
  1891. chip->select_chip(mtd, chipnr);
  1892. /* Check, if it is write protected */
  1893. if (nand_check_wp(mtd))
  1894. return -EIO;
  1895. realpage = (int)(to >> chip->page_shift);
  1896. page = realpage & chip->pagemask;
  1897. blockmask = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
  1898. /* Invalidate the page cache, when we write to the cached page */
  1899. if (to <= (chip->pagebuf << chip->page_shift) &&
  1900. (chip->pagebuf << chip->page_shift) < (to + ops->len))
  1901. chip->pagebuf = -1;
  1902. /* Don't allow multipage oob writes with offset */
  1903. if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen))
  1904. return -EINVAL;
  1905. while (1) {
  1906. int bytes = mtd->writesize;
  1907. int cached = writelen > bytes && page != blockmask;
  1908. uint8_t *wbuf = buf;
  1909. /* Partial page write? */
  1910. if (unlikely(column || writelen < (mtd->writesize - 1))) {
  1911. cached = 0;
  1912. bytes = min_t(int, bytes - column, (int) writelen);
  1913. chip->pagebuf = -1;
  1914. memset(chip->buffers->databuf, 0xff, mtd->writesize);
  1915. memcpy(&chip->buffers->databuf[column], buf, bytes);
  1916. wbuf = chip->buffers->databuf;
  1917. }
  1918. if (unlikely(oob)) {
  1919. size_t len = min(oobwritelen, oobmaxlen);
  1920. oob = nand_fill_oob(mtd, oob, len, ops);
  1921. oobwritelen -= len;
  1922. } else {
  1923. /* We still need to erase leftover OOB data */
  1924. memset(chip->oob_poi, 0xff, mtd->oobsize);
  1925. }
  1926. ret = chip->write_page(mtd, chip, wbuf, page, cached,
  1927. (ops->mode == MTD_OOB_RAW));
  1928. if (ret)
  1929. break;
  1930. writelen -= bytes;
  1931. if (!writelen)
  1932. break;
  1933. column = 0;
  1934. buf += bytes;
  1935. realpage++;
  1936. page = realpage & chip->pagemask;
  1937. /* Check, if we cross a chip boundary */
  1938. if (!page) {
  1939. chipnr++;
  1940. chip->select_chip(mtd, -1);
  1941. chip->select_chip(mtd, chipnr);
  1942. }
  1943. }
  1944. ops->retlen = ops->len - writelen;
  1945. if (unlikely(oob))
  1946. ops->oobretlen = ops->ooblen;
  1947. return ret;
  1948. }
  1949. /**
  1950. * panic_nand_write - [MTD Interface] NAND write with ECC
  1951. * @mtd: MTD device structure
  1952. * @to: offset to write to
  1953. * @len: number of bytes to write
  1954. * @retlen: pointer to variable to store the number of written bytes
  1955. * @buf: the data to write
  1956. *
  1957. * NAND write with ECC. Used when performing writes in interrupt context, this
  1958. * may for example be called by mtdoops when writing an oops while in panic.
  1959. */
  1960. static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
  1961. size_t *retlen, const uint8_t *buf)
  1962. {
  1963. struct nand_chip *chip = mtd->priv;
  1964. int ret;
  1965. /* Do not allow reads past end of device */
  1966. if ((to + len) > mtd->size)
  1967. return -EINVAL;
  1968. if (!len)
  1969. return 0;
  1970. /* Wait for the device to get ready */
  1971. panic_nand_wait(mtd, chip, 400);
  1972. /* Grab the device */
  1973. panic_nand_get_device(chip, mtd, FL_WRITING);
  1974. chip->ops.len = len;
  1975. chip->ops.datbuf = (uint8_t *)buf;
  1976. chip->ops.oobbuf = NULL;
  1977. ret = nand_do_write_ops(mtd, to, &chip->ops);
  1978. *retlen = chip->ops.retlen;
  1979. return ret;
  1980. }
  1981. /**
  1982. * nand_write - [MTD Interface] NAND write with ECC
  1983. * @mtd: MTD device structure
  1984. * @to: offset to write to
  1985. * @len: number of bytes to write
  1986. * @retlen: pointer to variable to store the number of written bytes
  1987. * @buf: the data to write
  1988. *
  1989. * NAND write with ECC.
  1990. */
  1991. static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
  1992. size_t *retlen, const uint8_t *buf)
  1993. {
  1994. struct nand_chip *chip = mtd->priv;
  1995. int ret;
  1996. /* Do not allow reads past end of device */
  1997. if ((to + len) > mtd->size)
  1998. return -EINVAL;
  1999. if (!len)
  2000. return 0;
  2001. nand_get_device(chip, mtd, FL_WRITING);
  2002. chip->ops.len = len;
  2003. chip->ops.datbuf = (uint8_t *)buf;
  2004. chip->ops.oobbuf = NULL;
  2005. ret = nand_do_write_ops(mtd, to, &chip->ops);
  2006. *retlen = chip->ops.retlen;
  2007. nand_release_device(mtd);
  2008. return ret;
  2009. }
  2010. /**
  2011. * nand_do_write_oob - [MTD Interface] NAND write out-of-band
  2012. * @mtd: MTD device structure
  2013. * @to: offset to write to
  2014. * @ops: oob operation description structure
  2015. *
  2016. * NAND write out-of-band.
  2017. */
  2018. static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
  2019. struct mtd_oob_ops *ops)
  2020. {
  2021. int chipnr, page, status, len;
  2022. struct nand_chip *chip = mtd->priv;
  2023. DEBUG(MTD_DEBUG_LEVEL3, "%s: to = 0x%08x, len = %i\n",
  2024. __func__, (unsigned int)to, (int)ops->ooblen);
  2025. if (ops->mode == MTD_OOB_AUTO)
  2026. len = chip->ecc.layout->oobavail;
  2027. else
  2028. len = mtd->oobsize;
  2029. /* Do not allow write past end of page */
  2030. if ((ops->ooboffs + ops->ooblen) > len) {
  2031. DEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt to write "
  2032. "past end of page\n", __func__);
  2033. return -EINVAL;
  2034. }
  2035. if (unlikely(ops->ooboffs >= len)) {
  2036. DEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt to start "
  2037. "write outside oob\n", __func__);
  2038. return -EINVAL;
  2039. }
  2040. /* Do not allow write past end of device */
  2041. if (unlikely(to >= mtd->size ||
  2042. ops->ooboffs + ops->ooblen >
  2043. ((mtd->size >> chip->page_shift) -
  2044. (to >> chip->page_shift)) * len)) {
  2045. DEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt write beyond "
  2046. "end of device\n", __func__);
  2047. return -EINVAL;
  2048. }
  2049. chipnr = (int)(to >> chip->chip_shift);
  2050. chip->select_chip(mtd, chipnr);
  2051. /* Shift to get page */
  2052. page = (int)(to >> chip->page_shift);
  2053. /*
  2054. * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
  2055. * of my DiskOnChip 2000 test units) will clear the whole data page too
  2056. * if we don't do this. I have no clue why, but I seem to have 'fixed'
  2057. * it in the doc2000 driver in August 1999. dwmw2.
  2058. */
  2059. chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
  2060. /* Check, if it is write protected */
  2061. if (nand_check_wp(mtd))
  2062. return -EROFS;
  2063. /* Invalidate the page cache, if we write to the cached page */
  2064. if (page == chip->pagebuf)
  2065. chip->pagebuf = -1;
  2066. nand_fill_oob(mtd, ops->oobbuf, ops->ooblen, ops);
  2067. status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
  2068. if (status)
  2069. return status;
  2070. ops->oobretlen = ops->ooblen;
  2071. return 0;
  2072. }
  2073. /**
  2074. * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
  2075. * @mtd: MTD device structure
  2076. * @to: offset to write to
  2077. * @ops: oob operation description structure
  2078. */
  2079. static int nand_write_oob(struct mtd_info *mtd, loff_t to,
  2080. struct mtd_oob_ops *ops)
  2081. {
  2082. struct nand_chip *chip = mtd->priv;
  2083. int ret = -ENOTSUPP;
  2084. ops->retlen = 0;
  2085. /* Do not allow writes past end of device */
  2086. if (ops->datbuf && (to + ops->len) > mtd->size) {
  2087. DEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt write beyond "
  2088. "end of device\n", __func__);
  2089. return -EINVAL;
  2090. }
  2091. nand_get_device(chip, mtd, FL_WRITING);
  2092. switch (ops->mode) {
  2093. case MTD_OOB_PLACE:
  2094. case MTD_OOB_AUTO:
  2095. case MTD_OOB_RAW:
  2096. break;
  2097. default:
  2098. goto out;
  2099. }
  2100. if (!ops->datbuf)
  2101. ret = nand_do_write_oob(mtd, to, ops);
  2102. else
  2103. ret = nand_do_write_ops(mtd, to, ops);
  2104. out:
  2105. nand_release_device(mtd);
  2106. return ret;
  2107. }
  2108. /**
  2109. * single_erase_cmd - [GENERIC] NAND standard block erase command function
  2110. * @mtd: MTD device structure
  2111. * @page: the page address of the block which will be erased
  2112. *
  2113. * Standard erase command for NAND chips.
  2114. */
  2115. static void single_erase_cmd(struct mtd_info *mtd, int page)
  2116. {
  2117. struct nand_chip *chip = mtd->priv;
  2118. /* Send commands to erase a block */
  2119. chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
  2120. chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
  2121. }
  2122. /**
  2123. * multi_erase_cmd - [GENERIC] AND specific block erase command function
  2124. * @mtd: MTD device structure
  2125. * @page: the page address of the block which will be erased
  2126. *
  2127. * AND multi block erase command function. Erase 4 consecutive blocks.
  2128. */
  2129. static void multi_erase_cmd(struct mtd_info *mtd, int page)
  2130. {
  2131. struct nand_chip *chip = mtd->priv;
  2132. /* Send commands to erase a block */
  2133. chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
  2134. chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
  2135. chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
  2136. chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
  2137. chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
  2138. }
  2139. /**
  2140. * nand_erase - [MTD Interface] erase block(s)
  2141. * @mtd: MTD device structure
  2142. * @instr: erase instruction
  2143. *
  2144. * Erase one ore more blocks.
  2145. */
  2146. static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
  2147. {
  2148. return nand_erase_nand(mtd, instr, 0);
  2149. }
  2150. #define BBT_PAGE_MASK 0xffffff3f
  2151. /**
  2152. * nand_erase_nand - [INTERN] erase block(s)
  2153. * @mtd: MTD device structure
  2154. * @instr: erase instruction
  2155. * @allowbbt: allow erasing the bbt area
  2156. *
  2157. * Erase one ore more blocks.
  2158. */
  2159. int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
  2160. int allowbbt)
  2161. {
  2162. int page, status, pages_per_block, ret, chipnr;
  2163. struct nand_chip *chip = mtd->priv;
  2164. loff_t rewrite_bbt[NAND_MAX_CHIPS] = {0};
  2165. unsigned int bbt_masked_page = 0xffffffff;
  2166. loff_t len;
  2167. DEBUG(MTD_DEBUG_LEVEL3, "%s: start = 0x%012llx, len = %llu\n",
  2168. __func__, (unsigned long long)instr->addr,
  2169. (unsigned long long)instr->len);
  2170. if (check_offs_len(mtd, instr->addr, instr->len))
  2171. return -EINVAL;
  2172. instr->fail_addr = MTD_FAIL_ADDR_UNKNOWN;
  2173. /* Grab the lock and see if the device is available */
  2174. nand_get_device(chip, mtd, FL_ERASING);
  2175. /* Shift to get first page */
  2176. page = (int)(instr->addr >> chip->page_shift);
  2177. chipnr = (int)(instr->addr >> chip->chip_shift);
  2178. /* Calculate pages in each block */
  2179. pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
  2180. /* Select the NAND device */
  2181. chip->select_chip(mtd, chipnr);
  2182. /* Check, if it is write protected */
  2183. if (nand_check_wp(mtd)) {
  2184. DEBUG(MTD_DEBUG_LEVEL0, "%s: Device is write protected!!!\n",
  2185. __func__);
  2186. instr->state = MTD_ERASE_FAILED;
  2187. goto erase_exit;
  2188. }
  2189. /*
  2190. * If BBT requires refresh, set the BBT page mask to see if the BBT
  2191. * should be rewritten. Otherwise the mask is set to 0xffffffff which
  2192. * can not be matched. This is also done when the bbt is actually
  2193. * erased to avoid recursive updates.
  2194. */
  2195. if (chip->options & BBT_AUTO_REFRESH && !allowbbt)
  2196. bbt_masked_page = chip->bbt_td->pages[chipnr] & BBT_PAGE_MASK;
  2197. /* Loop through the pages */
  2198. len = instr->len;
  2199. instr->state = MTD_ERASING;
  2200. while (len) {
  2201. /* Heck if we have a bad block, we do not erase bad blocks! */
  2202. if (nand_block_checkbad(mtd, ((loff_t) page) <<
  2203. chip->page_shift, 0, allowbbt)) {
  2204. pr_warn("%s: attempt to erase a bad block at page 0x%08x\n",
  2205. __func__, page);
  2206. instr->state = MTD_ERASE_FAILED;
  2207. goto erase_exit;
  2208. }
  2209. /*
  2210. * Invalidate the page cache, if we erase the block which
  2211. * contains the current cached page.
  2212. */
  2213. if (page <= chip->pagebuf && chip->pagebuf <
  2214. (page + pages_per_block))
  2215. chip->pagebuf = -1;
  2216. chip->erase_cmd(mtd, page & chip->pagemask);
  2217. status = chip->waitfunc(mtd, chip);
  2218. /*
  2219. * See if operation failed and additional status checks are
  2220. * available
  2221. */
  2222. if ((status & NAND_STATUS_FAIL) && (chip->errstat))
  2223. status = chip->errstat(mtd, chip, FL_ERASING,
  2224. status, page);
  2225. /* See if block erase succeeded */
  2226. if (status & NAND_STATUS_FAIL) {
  2227. DEBUG(MTD_DEBUG_LEVEL0, "%s: Failed erase, "
  2228. "page 0x%08x\n", __func__, page);
  2229. instr->state = MTD_ERASE_FAILED;
  2230. instr->fail_addr =
  2231. ((loff_t)page << chip->page_shift);
  2232. goto erase_exit;
  2233. }
  2234. /*
  2235. * If BBT requires refresh, set the BBT rewrite flag to the
  2236. * page being erased.
  2237. */
  2238. if (bbt_masked_page != 0xffffffff &&
  2239. (page & BBT_PAGE_MASK) == bbt_masked_page)
  2240. rewrite_bbt[chipnr] =
  2241. ((loff_t)page << chip->page_shift);
  2242. /* Increment page address and decrement length */
  2243. len -= (1 << chip->phys_erase_shift);
  2244. page += pages_per_block;
  2245. /* Check, if we cross a chip boundary */
  2246. if (len && !(page & chip->pagemask)) {
  2247. chipnr++;
  2248. chip->select_chip(mtd, -1);
  2249. chip->select_chip(mtd, chipnr);
  2250. /*
  2251. * If BBT requires refresh and BBT-PERCHIP, set the BBT
  2252. * page mask to see if this BBT should be rewritten.
  2253. */
  2254. if (bbt_masked_page != 0xffffffff &&
  2255. (chip->bbt_td->options & NAND_BBT_PERCHIP))
  2256. bbt_masked_page = chip->bbt_td->pages[chipnr] &
  2257. BBT_PAGE_MASK;
  2258. }
  2259. }
  2260. instr->state = MTD_ERASE_DONE;
  2261. erase_exit:
  2262. ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
  2263. /* Deselect and wake up anyone waiting on the device */
  2264. nand_release_device(mtd);
  2265. /* Do call back function */
  2266. if (!ret)
  2267. mtd_erase_callback(instr);
  2268. /*
  2269. * If BBT requires refresh and erase was successful, rewrite any
  2270. * selected bad block tables.
  2271. */
  2272. if (bbt_masked_page == 0xffffffff || ret)
  2273. return ret;
  2274. for (chipnr = 0; chipnr < chip->numchips; chipnr++) {
  2275. if (!rewrite_bbt[chipnr])
  2276. continue;
  2277. /* Update the BBT for chip */
  2278. DEBUG(MTD_DEBUG_LEVEL0, "%s: nand_update_bbt "
  2279. "(%d:0x%0llx 0x%0x)\n", __func__, chipnr,
  2280. rewrite_bbt[chipnr], chip->bbt_td->pages[chipnr]);
  2281. nand_update_bbt(mtd, rewrite_bbt[chipnr]);
  2282. }
  2283. /* Return more or less happy */
  2284. return ret;
  2285. }
  2286. /**
  2287. * nand_sync - [MTD Interface] sync
  2288. * @mtd: MTD device structure
  2289. *
  2290. * Sync is actually a wait for chip ready function.
  2291. */
  2292. static void nand_sync(struct mtd_info *mtd)
  2293. {
  2294. struct nand_chip *chip = mtd->priv;
  2295. DEBUG(MTD_DEBUG_LEVEL3, "%s: called\n", __func__);
  2296. /* Grab the lock and see if the device is available */
  2297. nand_get_device(chip, mtd, FL_SYNCING);
  2298. /* Release it and go back */
  2299. nand_release_device(mtd);
  2300. }
  2301. /**
  2302. * nand_block_isbad - [MTD Interface] Check if block at offset is bad
  2303. * @mtd: MTD device structure
  2304. * @offs: offset relative to mtd start
  2305. */
  2306. static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
  2307. {
  2308. /* Check for invalid offset */
  2309. if (offs > mtd->size)
  2310. return -EINVAL;
  2311. return nand_block_checkbad(mtd, offs, 1, 0);
  2312. }
  2313. /**
  2314. * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
  2315. * @mtd: MTD device structure
  2316. * @ofs: offset relative to mtd start
  2317. */
  2318. static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
  2319. {
  2320. struct nand_chip *chip = mtd->priv;
  2321. int ret;
  2322. ret = nand_block_isbad(mtd, ofs);
  2323. if (ret) {
  2324. /* If it was bad already, return success and do nothing */
  2325. if (ret > 0)
  2326. return 0;
  2327. return ret;
  2328. }
  2329. return chip->block_markbad(mtd, ofs);
  2330. }
  2331. /**
  2332. * nand_suspend - [MTD Interface] Suspend the NAND flash
  2333. * @mtd: MTD device structure
  2334. */
  2335. static int nand_suspend(struct mtd_info *mtd)
  2336. {
  2337. struct nand_chip *chip = mtd->priv;
  2338. return nand_get_device(chip, mtd, FL_PM_SUSPENDED);
  2339. }
  2340. /**
  2341. * nand_resume - [MTD Interface] Resume the NAND flash
  2342. * @mtd: MTD device structure
  2343. */
  2344. static void nand_resume(struct mtd_info *mtd)
  2345. {
  2346. struct nand_chip *chip = mtd->priv;
  2347. if (chip->state == FL_PM_SUSPENDED)
  2348. nand_release_device(mtd);
  2349. else
  2350. pr_err("%s called for a chip which is not in suspended state\n",
  2351. __func__);
  2352. }
  2353. /* Set default functions */
  2354. static void nand_set_defaults(struct nand_chip *chip, int busw)
  2355. {
  2356. /* check for proper chip_delay setup, set 20us if not */
  2357. if (!chip->chip_delay)
  2358. chip->chip_delay = 20;
  2359. /* check, if a user supplied command function given */
  2360. if (chip->cmdfunc == NULL)
  2361. chip->cmdfunc = nand_command;
  2362. /* check, if a user supplied wait function given */
  2363. if (chip->waitfunc == NULL)
  2364. chip->waitfunc = nand_wait;
  2365. if (!chip->select_chip)
  2366. chip->select_chip = nand_select_chip;
  2367. if (!chip->read_byte)
  2368. chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
  2369. if (!chip->read_word)
  2370. chip->read_word = nand_read_word;
  2371. if (!chip->block_bad)
  2372. chip->block_bad = nand_block_bad;
  2373. if (!chip->block_markbad)
  2374. chip->block_markbad = nand_default_block_markbad;
  2375. if (!chip->write_buf)
  2376. chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
  2377. if (!chip->read_buf)
  2378. chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
  2379. if (!chip->verify_buf)
  2380. chip->verify_buf = busw ? nand_verify_buf16 : nand_verify_buf;
  2381. if (!chip->scan_bbt)
  2382. chip->scan_bbt = nand_default_bbt;
  2383. if (!chip->controller) {
  2384. chip->controller = &chip->hwcontrol;
  2385. spin_lock_init(&chip->controller->lock);
  2386. init_waitqueue_head(&chip->controller->wq);
  2387. }
  2388. }
  2389. /* Sanitize ONFI strings so we can safely print them */
  2390. static void sanitize_string(uint8_t *s, size_t len)
  2391. {
  2392. ssize_t i;
  2393. /* Null terminate */
  2394. s[len - 1] = 0;
  2395. /* Remove non printable chars */
  2396. for (i = 0; i < len - 1; i++) {
  2397. if (s[i] < ' ' || s[i] > 127)
  2398. s[i] = '?';
  2399. }
  2400. /* Remove trailing spaces */
  2401. strim(s);
  2402. }
  2403. static u16 onfi_crc16(u16 crc, u8 const *p, size_t len)
  2404. {
  2405. int i;
  2406. while (len--) {
  2407. crc ^= *p++ << 8;
  2408. for (i = 0; i < 8; i++)
  2409. crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0);
  2410. }
  2411. return crc;
  2412. }
  2413. /*
  2414. * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise.
  2415. */
  2416. static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip,
  2417. int *busw)
  2418. {
  2419. struct nand_onfi_params *p = &chip->onfi_params;
  2420. int i;
  2421. int val;
  2422. /* Try ONFI for unknown chip or LP */
  2423. chip->cmdfunc(mtd, NAND_CMD_READID, 0x20, -1);
  2424. if (chip->read_byte(mtd) != 'O' || chip->read_byte(mtd) != 'N' ||
  2425. chip->read_byte(mtd) != 'F' || chip->read_byte(mtd) != 'I')
  2426. return 0;
  2427. pr_info("ONFI flash detected\n");
  2428. chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
  2429. for (i = 0; i < 3; i++) {
  2430. chip->read_buf(mtd, (uint8_t *)p, sizeof(*p));
  2431. if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 254) ==
  2432. le16_to_cpu(p->crc)) {
  2433. pr_info("ONFI param page %d valid\n", i);
  2434. break;
  2435. }
  2436. }
  2437. if (i == 3)
  2438. return 0;
  2439. /* Check version */
  2440. val = le16_to_cpu(p->revision);
  2441. if (val & (1 << 5))
  2442. chip->onfi_version = 23;
  2443. else if (val & (1 << 4))
  2444. chip->onfi_version = 22;
  2445. else if (val & (1 << 3))
  2446. chip->onfi_version = 21;
  2447. else if (val & (1 << 2))
  2448. chip->onfi_version = 20;
  2449. else if (val & (1 << 1))
  2450. chip->onfi_version = 10;
  2451. else
  2452. chip->onfi_version = 0;
  2453. if (!chip->onfi_version) {
  2454. pr_info("%s: unsupported ONFI version: %d\n", __func__, val);
  2455. return 0;
  2456. }
  2457. sanitize_string(p->manufacturer, sizeof(p->manufacturer));
  2458. sanitize_string(p->model, sizeof(p->model));
  2459. if (!mtd->name)
  2460. mtd->name = p->model;
  2461. mtd->writesize = le32_to_cpu(p->byte_per_page);
  2462. mtd->erasesize = le32_to_cpu(p->pages_per_block) * mtd->writesize;
  2463. mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
  2464. chip->chipsize = (uint64_t)le32_to_cpu(p->blocks_per_lun) * mtd->erasesize;
  2465. *busw = 0;
  2466. if (le16_to_cpu(p->features) & 1)
  2467. *busw = NAND_BUSWIDTH_16;
  2468. chip->options &= ~NAND_CHIPOPTIONS_MSK;
  2469. chip->options |= (NAND_NO_READRDY |
  2470. NAND_NO_AUTOINCR) & NAND_CHIPOPTIONS_MSK;
  2471. return 1;
  2472. }
  2473. /*
  2474. * Get the flash and manufacturer id and lookup if the type is supported.
  2475. */
  2476. static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
  2477. struct nand_chip *chip,
  2478. int busw,
  2479. int *maf_id, int *dev_id,
  2480. struct nand_flash_dev *type)
  2481. {
  2482. int i, maf_idx;
  2483. u8 id_data[8];
  2484. int ret;
  2485. /* Select the device */
  2486. chip->select_chip(mtd, 0);
  2487. /*
  2488. * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
  2489. * after power-up.
  2490. */
  2491. chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
  2492. /* Send the command for reading device ID */
  2493. chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
  2494. /* Read manufacturer and device IDs */
  2495. *maf_id = chip->read_byte(mtd);
  2496. *dev_id = chip->read_byte(mtd);
  2497. /*
  2498. * Try again to make sure, as some systems the bus-hold or other
  2499. * interface concerns can cause random data which looks like a
  2500. * possibly credible NAND flash to appear. If the two results do
  2501. * not match, ignore the device completely.
  2502. */
  2503. chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
  2504. for (i = 0; i < 2; i++)
  2505. id_data[i] = chip->read_byte(mtd);
  2506. if (id_data[0] != *maf_id || id_data[1] != *dev_id) {
  2507. pr_info("%s: second ID read did not match "
  2508. "%02x,%02x against %02x,%02x\n", __func__,
  2509. *maf_id, *dev_id, id_data[0], id_data[1]);
  2510. return ERR_PTR(-ENODEV);
  2511. }
  2512. if (!type)
  2513. type = nand_flash_ids;
  2514. for (; type->name != NULL; type++)
  2515. if (*dev_id == type->id)
  2516. break;
  2517. chip->onfi_version = 0;
  2518. if (!type->name || !type->pagesize) {
  2519. /* Check is chip is ONFI compliant */
  2520. ret = nand_flash_detect_onfi(mtd, chip, &busw);
  2521. if (ret)
  2522. goto ident_done;
  2523. }
  2524. chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
  2525. /* Read entire ID string */
  2526. for (i = 0; i < 8; i++)
  2527. id_data[i] = chip->read_byte(mtd);
  2528. if (!type->name)
  2529. return ERR_PTR(-ENODEV);
  2530. if (!mtd->name)
  2531. mtd->name = type->name;
  2532. chip->chipsize = (uint64_t)type->chipsize << 20;
  2533. if (!type->pagesize && chip->init_size) {
  2534. /* Set the pagesize, oobsize, erasesize by the driver */
  2535. busw = chip->init_size(mtd, chip, id_data);
  2536. } else if (!type->pagesize) {
  2537. int extid;
  2538. /* The 3rd id byte holds MLC / multichip data */
  2539. chip->cellinfo = id_data[2];
  2540. /* The 4th id byte is the important one */
  2541. extid = id_data[3];
  2542. /*
  2543. * Field definitions are in the following datasheets:
  2544. * Old style (4,5 byte ID): Samsung K9GAG08U0M (p.32)
  2545. * New style (6 byte ID): Samsung K9GBG08U0M (p.40)
  2546. *
  2547. * Check for wraparound + Samsung ID + nonzero 6th byte
  2548. * to decide what to do.
  2549. */
  2550. if (id_data[0] == id_data[6] && id_data[1] == id_data[7] &&
  2551. id_data[0] == NAND_MFR_SAMSUNG &&
  2552. (chip->cellinfo & NAND_CI_CELLTYPE_MSK) &&
  2553. id_data[5] != 0x00) {
  2554. /* Calc pagesize */
  2555. mtd->writesize = 2048 << (extid & 0x03);
  2556. extid >>= 2;
  2557. /* Calc oobsize */
  2558. switch (extid & 0x03) {
  2559. case 1:
  2560. mtd->oobsize = 128;
  2561. break;
  2562. case 2:
  2563. mtd->oobsize = 218;
  2564. break;
  2565. case 3:
  2566. mtd->oobsize = 400;
  2567. break;
  2568. default:
  2569. mtd->oobsize = 436;
  2570. break;
  2571. }
  2572. extid >>= 2;
  2573. /* Calc blocksize */
  2574. mtd->erasesize = (128 * 1024) <<
  2575. (((extid >> 1) & 0x04) | (extid & 0x03));
  2576. busw = 0;
  2577. } else {
  2578. /* Calc pagesize */
  2579. mtd->writesize = 1024 << (extid & 0x03);
  2580. extid >>= 2;
  2581. /* Calc oobsize */
  2582. mtd->oobsize = (8 << (extid & 0x01)) *
  2583. (mtd->writesize >> 9);
  2584. extid >>= 2;
  2585. /* Calc blocksize. Blocksize is multiples of 64KiB */
  2586. mtd->erasesize = (64 * 1024) << (extid & 0x03);
  2587. extid >>= 2;
  2588. /* Get buswidth information */
  2589. busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0;
  2590. }
  2591. } else {
  2592. /*
  2593. * Old devices have chip data hardcoded in the device id table.
  2594. */
  2595. mtd->erasesize = type->erasesize;
  2596. mtd->writesize = type->pagesize;
  2597. mtd->oobsize = mtd->writesize / 32;
  2598. busw = type->options & NAND_BUSWIDTH_16;
  2599. /*
  2600. * Check for Spansion/AMD ID + repeating 5th, 6th byte since
  2601. * some Spansion chips have erasesize that conflicts with size
  2602. * listed in nand_ids table.
  2603. * Data sheet (5 byte ID): Spansion S30ML-P ORNAND (p.39)
  2604. */
  2605. if (*maf_id == NAND_MFR_AMD && id_data[4] != 0x00 &&
  2606. id_data[5] == 0x00 && id_data[6] == 0x00 &&
  2607. id_data[7] == 0x00 && mtd->writesize == 512) {
  2608. mtd->erasesize = 128 * 1024;
  2609. mtd->erasesize <<= ((id_data[3] & 0x03) << 1);
  2610. }
  2611. }
  2612. /* Get chip options, preserve non chip based options */
  2613. chip->options &= ~NAND_CHIPOPTIONS_MSK;
  2614. chip->options |= type->options & NAND_CHIPOPTIONS_MSK;
  2615. /*
  2616. * Check if chip is not a Samsung device. Do not clear the
  2617. * options for chips which do not have an extended id.
  2618. */
  2619. if (*maf_id != NAND_MFR_SAMSUNG && !type->pagesize)
  2620. chip->options &= ~NAND_SAMSUNG_LP_OPTIONS;
  2621. ident_done:
  2622. /*
  2623. * Set chip as a default. Board drivers can override it, if necessary.
  2624. */
  2625. chip->options |= NAND_NO_AUTOINCR;
  2626. /* Try to identify manufacturer */
  2627. for (maf_idx = 0; nand_manuf_ids[maf_idx].id != 0x0; maf_idx++) {
  2628. if (nand_manuf_ids[maf_idx].id == *maf_id)
  2629. break;
  2630. }
  2631. /*
  2632. * Check, if buswidth is correct. Hardware drivers should set
  2633. * chip correct!
  2634. */
  2635. if (busw != (chip->options & NAND_BUSWIDTH_16)) {
  2636. pr_info("NAND device: Manufacturer ID:"
  2637. " 0x%02x, Chip ID: 0x%02x (%s %s)\n", *maf_id,
  2638. *dev_id, nand_manuf_ids[maf_idx].name, mtd->name);
  2639. pr_warn("NAND bus width %d instead %d bit\n",
  2640. (chip->options & NAND_BUSWIDTH_16) ? 16 : 8,
  2641. busw ? 16 : 8);
  2642. return ERR_PTR(-EINVAL);
  2643. }
  2644. /* Calculate the address shift from the page size */
  2645. chip->page_shift = ffs(mtd->writesize) - 1;
  2646. /* Convert chipsize to number of pages per chip -1 */
  2647. chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
  2648. chip->bbt_erase_shift = chip->phys_erase_shift =
  2649. ffs(mtd->erasesize) - 1;
  2650. if (chip->chipsize & 0xffffffff)
  2651. chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
  2652. else {
  2653. chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32));
  2654. chip->chip_shift += 32 - 1;
  2655. }
  2656. chip->badblockbits = 8;
  2657. /* Set the bad block position */
  2658. if (mtd->writesize > 512 || (busw & NAND_BUSWIDTH_16))
  2659. chip->badblockpos = NAND_LARGE_BADBLOCK_POS;
  2660. else
  2661. chip->badblockpos = NAND_SMALL_BADBLOCK_POS;
  2662. /*
  2663. * Bad block marker is stored in the last page of each block
  2664. * on Samsung and Hynix MLC devices; stored in first two pages
  2665. * of each block on Micron devices with 2KiB pages and on
  2666. * SLC Samsung, Hynix, Toshiba and AMD/Spansion. All others scan
  2667. * only the first page.
  2668. */
  2669. if ((chip->cellinfo & NAND_CI_CELLTYPE_MSK) &&
  2670. (*maf_id == NAND_MFR_SAMSUNG ||
  2671. *maf_id == NAND_MFR_HYNIX))
  2672. chip->bbt_options |= NAND_BBT_SCANLASTPAGE;
  2673. else if ((!(chip->cellinfo & NAND_CI_CELLTYPE_MSK) &&
  2674. (*maf_id == NAND_MFR_SAMSUNG ||
  2675. *maf_id == NAND_MFR_HYNIX ||
  2676. *maf_id == NAND_MFR_TOSHIBA ||
  2677. *maf_id == NAND_MFR_AMD)) ||
  2678. (mtd->writesize == 2048 &&
  2679. *maf_id == NAND_MFR_MICRON))
  2680. chip->bbt_options |= NAND_BBT_SCAN2NDPAGE;
  2681. /* Check for AND chips with 4 page planes */
  2682. if (chip->options & NAND_4PAGE_ARRAY)
  2683. chip->erase_cmd = multi_erase_cmd;
  2684. else
  2685. chip->erase_cmd = single_erase_cmd;
  2686. /* Do not replace user supplied command function! */
  2687. if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
  2688. chip->cmdfunc = nand_command_lp;
  2689. pr_info("NAND device: Manufacturer ID:"
  2690. " 0x%02x, Chip ID: 0x%02x (%s %s)\n", *maf_id, *dev_id,
  2691. nand_manuf_ids[maf_idx].name,
  2692. chip->onfi_version ? chip->onfi_params.model : type->name);
  2693. return type;
  2694. }
  2695. /**
  2696. * nand_scan_ident - [NAND Interface] Scan for the NAND device
  2697. * @mtd: MTD device structure
  2698. * @maxchips: number of chips to scan for
  2699. * @table: alternative NAND ID table
  2700. *
  2701. * This is the first phase of the normal nand_scan() function. It reads the
  2702. * flash ID and sets up MTD fields accordingly.
  2703. *
  2704. * The mtd->owner field must be set to the module of the caller.
  2705. */
  2706. int nand_scan_ident(struct mtd_info *mtd, int maxchips,
  2707. struct nand_flash_dev *table)
  2708. {
  2709. int i, busw, nand_maf_id, nand_dev_id;
  2710. struct nand_chip *chip = mtd->priv;
  2711. struct nand_flash_dev *type;
  2712. /* Get buswidth to select the correct functions */
  2713. busw = chip->options & NAND_BUSWIDTH_16;
  2714. /* Set the default functions */
  2715. nand_set_defaults(chip, busw);
  2716. /* Read the flash type */
  2717. type = nand_get_flash_type(mtd, chip, busw,
  2718. &nand_maf_id, &nand_dev_id, table);
  2719. if (IS_ERR(type)) {
  2720. if (!(chip->options & NAND_SCAN_SILENT_NODEV))
  2721. pr_warn("No NAND device found\n");
  2722. chip->select_chip(mtd, -1);
  2723. return PTR_ERR(type);
  2724. }
  2725. /* Check for a chip array */
  2726. for (i = 1; i < maxchips; i++) {
  2727. chip->select_chip(mtd, i);
  2728. /* See comment in nand_get_flash_type for reset */
  2729. chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
  2730. /* Send the command for reading device ID */
  2731. chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
  2732. /* Read manufacturer and device IDs */
  2733. if (nand_maf_id != chip->read_byte(mtd) ||
  2734. nand_dev_id != chip->read_byte(mtd))
  2735. break;
  2736. }
  2737. if (i > 1)
  2738. pr_info("%d NAND chips detected\n", i);
  2739. /* Store the number of chips and calc total size for mtd */
  2740. chip->numchips = i;
  2741. mtd->size = i * chip->chipsize;
  2742. return 0;
  2743. }
  2744. EXPORT_SYMBOL(nand_scan_ident);
  2745. /**
  2746. * nand_scan_tail - [NAND Interface] Scan for the NAND device
  2747. * @mtd: MTD device structure
  2748. *
  2749. * This is the second phase of the normal nand_scan() function. It fills out
  2750. * all the uninitialized function pointers with the defaults and scans for a
  2751. * bad block table if appropriate.
  2752. */
  2753. int nand_scan_tail(struct mtd_info *mtd)
  2754. {
  2755. int i;
  2756. struct nand_chip *chip = mtd->priv;
  2757. if (!(chip->options & NAND_OWN_BUFFERS))
  2758. chip->buffers = kmalloc(sizeof(*chip->buffers), GFP_KERNEL);
  2759. if (!chip->buffers)
  2760. return -ENOMEM;
  2761. /* Set the internal oob buffer location, just after the page data */
  2762. chip->oob_poi = chip->buffers->databuf + mtd->writesize;
  2763. /*
  2764. * If no default placement scheme is given, select an appropriate one.
  2765. */
  2766. if (!chip->ecc.layout && (chip->ecc.mode != NAND_ECC_SOFT_BCH)) {
  2767. switch (mtd->oobsize) {
  2768. case 8:
  2769. chip->ecc.layout = &nand_oob_8;
  2770. break;
  2771. case 16:
  2772. chip->ecc.layout = &nand_oob_16;
  2773. break;
  2774. case 64:
  2775. chip->ecc.layout = &nand_oob_64;
  2776. break;
  2777. case 128:
  2778. chip->ecc.layout = &nand_oob_128;
  2779. break;
  2780. default:
  2781. pr_warn("No oob scheme defined for oobsize %d\n",
  2782. mtd->oobsize);
  2783. BUG();
  2784. }
  2785. }
  2786. if (!chip->write_page)
  2787. chip->write_page = nand_write_page;
  2788. /*
  2789. * Check ECC mode, default to software if 3byte/512byte hardware ECC is
  2790. * selected and we have 256 byte pagesize fallback to software ECC
  2791. */
  2792. switch (chip->ecc.mode) {
  2793. case NAND_ECC_HW_OOB_FIRST:
  2794. /* Similar to NAND_ECC_HW, but a separate read_page handle */
  2795. if (!chip->ecc.calculate || !chip->ecc.correct ||
  2796. !chip->ecc.hwctl) {
  2797. pr_warn("No ECC functions supplied; "
  2798. "hardware ECC not possible\n");
  2799. BUG();
  2800. }
  2801. if (!chip->ecc.read_page)
  2802. chip->ecc.read_page = nand_read_page_hwecc_oob_first;
  2803. case NAND_ECC_HW:
  2804. /* Use standard hwecc read page function? */
  2805. if (!chip->ecc.read_page)
  2806. chip->ecc.read_page = nand_read_page_hwecc;
  2807. if (!chip->ecc.write_page)
  2808. chip->ecc.write_page = nand_write_page_hwecc;
  2809. if (!chip->ecc.read_page_raw)
  2810. chip->ecc.read_page_raw = nand_read_page_raw;
  2811. if (!chip->ecc.write_page_raw)
  2812. chip->ecc.write_page_raw = nand_write_page_raw;
  2813. if (!chip->ecc.read_oob)
  2814. chip->ecc.read_oob = nand_read_oob_std;
  2815. if (!chip->ecc.write_oob)
  2816. chip->ecc.write_oob = nand_write_oob_std;
  2817. case NAND_ECC_HW_SYNDROME:
  2818. if ((!chip->ecc.calculate || !chip->ecc.correct ||
  2819. !chip->ecc.hwctl) &&
  2820. (!chip->ecc.read_page ||
  2821. chip->ecc.read_page == nand_read_page_hwecc ||
  2822. !chip->ecc.write_page ||
  2823. chip->ecc.write_page == nand_write_page_hwecc)) {
  2824. pr_warn("No ECC functions supplied; "
  2825. "hardware ECC not possible\n");
  2826. BUG();
  2827. }
  2828. /* Use standard syndrome read/write page function? */
  2829. if (!chip->ecc.read_page)
  2830. chip->ecc.read_page = nand_read_page_syndrome;
  2831. if (!chip->ecc.write_page)
  2832. chip->ecc.write_page = nand_write_page_syndrome;
  2833. if (!chip->ecc.read_page_raw)
  2834. chip->ecc.read_page_raw = nand_read_page_raw_syndrome;
  2835. if (!chip->ecc.write_page_raw)
  2836. chip->ecc.write_page_raw = nand_write_page_raw_syndrome;
  2837. if (!chip->ecc.read_oob)
  2838. chip->ecc.read_oob = nand_read_oob_syndrome;
  2839. if (!chip->ecc.write_oob)
  2840. chip->ecc.write_oob = nand_write_oob_syndrome;
  2841. if (mtd->writesize >= chip->ecc.size)
  2842. break;
  2843. pr_warn("%d byte HW ECC not possible on "
  2844. "%d byte page size, fallback to SW ECC\n",
  2845. chip->ecc.size, mtd->writesize);
  2846. chip->ecc.mode = NAND_ECC_SOFT;
  2847. case NAND_ECC_SOFT:
  2848. chip->ecc.calculate = nand_calculate_ecc;
  2849. chip->ecc.correct = nand_correct_data;
  2850. chip->ecc.read_page = nand_read_page_swecc;
  2851. chip->ecc.read_subpage = nand_read_subpage;
  2852. chip->ecc.write_page = nand_write_page_swecc;
  2853. chip->ecc.read_page_raw = nand_read_page_raw;
  2854. chip->ecc.write_page_raw = nand_write_page_raw;
  2855. chip->ecc.read_oob = nand_read_oob_std;
  2856. chip->ecc.write_oob = nand_write_oob_std;
  2857. if (!chip->ecc.size)
  2858. chip->ecc.size = 256;
  2859. chip->ecc.bytes = 3;
  2860. break;
  2861. case NAND_ECC_SOFT_BCH:
  2862. if (!mtd_nand_has_bch()) {
  2863. pr_warn("CONFIG_MTD_ECC_BCH not enabled\n");
  2864. BUG();
  2865. }
  2866. chip->ecc.calculate = nand_bch_calculate_ecc;
  2867. chip->ecc.correct = nand_bch_correct_data;
  2868. chip->ecc.read_page = nand_read_page_swecc;
  2869. chip->ecc.read_subpage = nand_read_subpage;
  2870. chip->ecc.write_page = nand_write_page_swecc;
  2871. chip->ecc.read_page_raw = nand_read_page_raw;
  2872. chip->ecc.write_page_raw = nand_write_page_raw;
  2873. chip->ecc.read_oob = nand_read_oob_std;
  2874. chip->ecc.write_oob = nand_write_oob_std;
  2875. /*
  2876. * Board driver should supply ecc.size and ecc.bytes values to
  2877. * select how many bits are correctable; see nand_bch_init()
  2878. * for details. Otherwise, default to 4 bits for large page
  2879. * devices.
  2880. */
  2881. if (!chip->ecc.size && (mtd->oobsize >= 64)) {
  2882. chip->ecc.size = 512;
  2883. chip->ecc.bytes = 7;
  2884. }
  2885. chip->ecc.priv = nand_bch_init(mtd,
  2886. chip->ecc.size,
  2887. chip->ecc.bytes,
  2888. &chip->ecc.layout);
  2889. if (!chip->ecc.priv) {
  2890. pr_warn("BCH ECC initialization failed!\n");
  2891. BUG();
  2892. }
  2893. break;
  2894. case NAND_ECC_NONE:
  2895. pr_warn("NAND_ECC_NONE selected by board driver. "
  2896. "This is not recommended!\n");
  2897. chip->ecc.read_page = nand_read_page_raw;
  2898. chip->ecc.write_page = nand_write_page_raw;
  2899. chip->ecc.read_oob = nand_read_oob_std;
  2900. chip->ecc.read_page_raw = nand_read_page_raw;
  2901. chip->ecc.write_page_raw = nand_write_page_raw;
  2902. chip->ecc.write_oob = nand_write_oob_std;
  2903. chip->ecc.size = mtd->writesize;
  2904. chip->ecc.bytes = 0;
  2905. break;
  2906. default:
  2907. pr_warn("Invalid NAND_ECC_MODE %d\n", chip->ecc.mode);
  2908. BUG();
  2909. }
  2910. /*
  2911. * The number of bytes available for a client to place data into
  2912. * the out of band area.
  2913. */
  2914. chip->ecc.layout->oobavail = 0;
  2915. for (i = 0; chip->ecc.layout->oobfree[i].length
  2916. && i < ARRAY_SIZE(chip->ecc.layout->oobfree); i++)
  2917. chip->ecc.layout->oobavail +=
  2918. chip->ecc.layout->oobfree[i].length;
  2919. mtd->oobavail = chip->ecc.layout->oobavail;
  2920. /*
  2921. * Set the number of read / write steps for one page depending on ECC
  2922. * mode.
  2923. */
  2924. chip->ecc.steps = mtd->writesize / chip->ecc.size;
  2925. if (chip->ecc.steps * chip->ecc.size != mtd->writesize) {
  2926. pr_warn("Invalid ECC parameters\n");
  2927. BUG();
  2928. }
  2929. chip->ecc.total = chip->ecc.steps * chip->ecc.bytes;
  2930. /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */
  2931. if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
  2932. !(chip->cellinfo & NAND_CI_CELLTYPE_MSK)) {
  2933. switch (chip->ecc.steps) {
  2934. case 2:
  2935. mtd->subpage_sft = 1;
  2936. break;
  2937. case 4:
  2938. case 8:
  2939. case 16:
  2940. mtd->subpage_sft = 2;
  2941. break;
  2942. }
  2943. }
  2944. chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
  2945. /* Initialize state */
  2946. chip->state = FL_READY;
  2947. /* De-select the device */
  2948. chip->select_chip(mtd, -1);
  2949. /* Invalidate the pagebuffer reference */
  2950. chip->pagebuf = -1;
  2951. /* Fill in remaining MTD driver data */
  2952. mtd->type = MTD_NANDFLASH;
  2953. mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM :
  2954. MTD_CAP_NANDFLASH;
  2955. mtd->erase = nand_erase;
  2956. mtd->point = NULL;
  2957. mtd->unpoint = NULL;
  2958. mtd->read = nand_read;
  2959. mtd->write = nand_write;
  2960. mtd->panic_write = panic_nand_write;
  2961. mtd->read_oob = nand_read_oob;
  2962. mtd->write_oob = nand_write_oob;
  2963. mtd->sync = nand_sync;
  2964. mtd->lock = NULL;
  2965. mtd->unlock = NULL;
  2966. mtd->suspend = nand_suspend;
  2967. mtd->resume = nand_resume;
  2968. mtd->block_isbad = nand_block_isbad;
  2969. mtd->block_markbad = nand_block_markbad;
  2970. mtd->writebufsize = mtd->writesize;
  2971. /* propagate ecc.layout to mtd_info */
  2972. mtd->ecclayout = chip->ecc.layout;
  2973. /* Check, if we should skip the bad block table scan */
  2974. if (chip->options & NAND_SKIP_BBTSCAN)
  2975. return 0;
  2976. /* Build bad block table */
  2977. return chip->scan_bbt(mtd);
  2978. }
  2979. EXPORT_SYMBOL(nand_scan_tail);
  2980. /*
  2981. * is_module_text_address() isn't exported, and it's mostly a pointless
  2982. * test if this is a module _anyway_ -- they'd have to try _really_ hard
  2983. * to call us from in-kernel code if the core NAND support is modular.
  2984. */
  2985. #ifdef MODULE
  2986. #define caller_is_module() (1)
  2987. #else
  2988. #define caller_is_module() \
  2989. is_module_text_address((unsigned long)__builtin_return_address(0))
  2990. #endif
  2991. /**
  2992. * nand_scan - [NAND Interface] Scan for the NAND device
  2993. * @mtd: MTD device structure
  2994. * @maxchips: number of chips to scan for
  2995. *
  2996. * This fills out all the uninitialized function pointers with the defaults.
  2997. * The flash ID is read and the mtd/chip structures are filled with the
  2998. * appropriate values. The mtd->owner field must be set to the module of the
  2999. * caller.
  3000. */
  3001. int nand_scan(struct mtd_info *mtd, int maxchips)
  3002. {
  3003. int ret;
  3004. /* Many callers got this wrong, so check for it for a while... */
  3005. if (!mtd->owner && caller_is_module()) {
  3006. pr_crit("%s called with NULL mtd->owner!\n", __func__);
  3007. BUG();
  3008. }
  3009. ret = nand_scan_ident(mtd, maxchips, NULL);
  3010. if (!ret)
  3011. ret = nand_scan_tail(mtd);
  3012. return ret;
  3013. }
  3014. EXPORT_SYMBOL(nand_scan);
  3015. /**
  3016. * nand_release - [NAND Interface] Free resources held by the NAND device
  3017. * @mtd: MTD device structure
  3018. */
  3019. void nand_release(struct mtd_info *mtd)
  3020. {
  3021. struct nand_chip *chip = mtd->priv;
  3022. if (chip->ecc.mode == NAND_ECC_SOFT_BCH)
  3023. nand_bch_free((struct nand_bch_control *)chip->ecc.priv);
  3024. mtd_device_unregister(mtd);
  3025. /* Free bad block table memory */
  3026. kfree(chip->bbt);
  3027. if (!(chip->options & NAND_OWN_BUFFERS))
  3028. kfree(chip->buffers);
  3029. /* Free bad block descriptor memory */
  3030. if (chip->badblock_pattern && chip->badblock_pattern->options
  3031. & NAND_BBT_DYNAMICSTRUCT)
  3032. kfree(chip->badblock_pattern);
  3033. }
  3034. EXPORT_SYMBOL_GPL(nand_release);
  3035. static int __init nand_base_init(void)
  3036. {
  3037. led_trigger_register_simple("nand-disk", &nand_led_trigger);
  3038. return 0;
  3039. }
  3040. static void __exit nand_base_exit(void)
  3041. {
  3042. led_trigger_unregister_simple(nand_led_trigger);
  3043. }
  3044. module_init(nand_base_init);
  3045. module_exit(nand_base_exit);
  3046. MODULE_LICENSE("GPL");
  3047. MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>");
  3048. MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>");
  3049. MODULE_DESCRIPTION("Generic NAND flash driver code");