nand_base.c 91 KB

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