nand_base.c 91 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543
  1. /*
  2. * drivers/mtd/nand.c
  3. *
  4. * Overview:
  5. * This is the generic MTD driver for NAND flash devices. It should be
  6. * capable of working with almost all NAND chips currently available.
  7. * Basic support for AG-AND chips is provided.
  8. *
  9. * Additional technical information is available on
  10. * http://www.linux-mtd.infradead.org/doc/nand.html
  11. *
  12. * Copyright (C) 2000 Steven J. Hill (sjhill@realitydiluted.com)
  13. * 2002-2006 Thomas Gleixner (tglx@linutronix.de)
  14. *
  15. * Credits:
  16. * David Woodhouse for adding multichip support
  17. *
  18. * Aleph One Ltd. and Toby Churchill Ltd. for supporting the
  19. * rework for 2K page size chips
  20. *
  21. * TODO:
  22. * Enable cached programming for 2k page size chips
  23. * Check, if mtd->ecctype should be set to MTD_ECC_HW
  24. * if we have HW ecc support.
  25. * The AG-AND chips have nice features for speed improvement,
  26. * which are not supported yet. Read / program 4 pages in one go.
  27. * BBT table is not serialized, has to be fixed
  28. *
  29. * This program is free software; you can redistribute it and/or modify
  30. * it under the terms of the GNU General Public License version 2 as
  31. * published by the Free Software Foundation.
  32. *
  33. */
  34. #include <linux/module.h>
  35. #include <linux/delay.h>
  36. #include <linux/errno.h>
  37. #include <linux/err.h>
  38. #include <linux/sched.h>
  39. #include <linux/slab.h>
  40. #include <linux/types.h>
  41. #include <linux/mtd/mtd.h>
  42. #include <linux/mtd/nand.h>
  43. #include <linux/mtd/nand_ecc.h>
  44. #include <linux/mtd/nand_bch.h>
  45. #include <linux/interrupt.h>
  46. #include <linux/bitops.h>
  47. #include <linux/leds.h>
  48. #include <linux/io.h>
  49. #include <linux/mtd/partitions.h>
  50. /* Define default oob placement schemes for large and small page devices */
  51. static struct nand_ecclayout nand_oob_8 = {
  52. .eccbytes = 3,
  53. .eccpos = {0, 1, 2},
  54. .oobfree = {
  55. {.offset = 3,
  56. .length = 2},
  57. {.offset = 6,
  58. .length = 2} }
  59. };
  60. static struct nand_ecclayout nand_oob_16 = {
  61. .eccbytes = 6,
  62. .eccpos = {0, 1, 2, 3, 6, 7},
  63. .oobfree = {
  64. {.offset = 8,
  65. . length = 8} }
  66. };
  67. static struct nand_ecclayout nand_oob_64 = {
  68. .eccbytes = 24,
  69. .eccpos = {
  70. 40, 41, 42, 43, 44, 45, 46, 47,
  71. 48, 49, 50, 51, 52, 53, 54, 55,
  72. 56, 57, 58, 59, 60, 61, 62, 63},
  73. .oobfree = {
  74. {.offset = 2,
  75. .length = 38} }
  76. };
  77. static struct nand_ecclayout nand_oob_128 = {
  78. .eccbytes = 48,
  79. .eccpos = {
  80. 80, 81, 82, 83, 84, 85, 86, 87,
  81. 88, 89, 90, 91, 92, 93, 94, 95,
  82. 96, 97, 98, 99, 100, 101, 102, 103,
  83. 104, 105, 106, 107, 108, 109, 110, 111,
  84. 112, 113, 114, 115, 116, 117, 118, 119,
  85. 120, 121, 122, 123, 124, 125, 126, 127},
  86. .oobfree = {
  87. {.offset = 2,
  88. .length = 78} }
  89. };
  90. static int nand_get_device(struct nand_chip *chip, struct mtd_info *mtd,
  91. int new_state);
  92. static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
  93. struct mtd_oob_ops *ops);
  94. /*
  95. * For devices which display every fart in the system on a separate LED. Is
  96. * compiled away when LED support is disabled.
  97. */
  98. DEFINE_LED_TRIGGER(nand_led_trigger);
  99. static int check_offs_len(struct mtd_info *mtd,
  100. loff_t ofs, uint64_t len)
  101. {
  102. struct nand_chip *chip = mtd->priv;
  103. int ret = 0;
  104. /* Start address must align on block boundary */
  105. if (ofs & ((1 << chip->phys_erase_shift) - 1)) {
  106. DEBUG(MTD_DEBUG_LEVEL0, "%s: Unaligned address\n", __func__);
  107. ret = -EINVAL;
  108. }
  109. /* Length must align on block boundary */
  110. if (len & ((1 << chip->phys_erase_shift) - 1)) {
  111. DEBUG(MTD_DEBUG_LEVEL0, "%s: Length not block aligned\n",
  112. __func__);
  113. ret = -EINVAL;
  114. }
  115. /* Do not allow past end of device */
  116. if (ofs + len > mtd->size) {
  117. DEBUG(MTD_DEBUG_LEVEL0, "%s: Past end of device\n",
  118. __func__);
  119. ret = -EINVAL;
  120. }
  121. return ret;
  122. }
  123. /**
  124. * nand_release_device - [GENERIC] release chip
  125. * @mtd: MTD device structure
  126. *
  127. * Deselect, release chip lock and wake up anyone waiting on the device.
  128. */
  129. static void nand_release_device(struct mtd_info *mtd)
  130. {
  131. struct nand_chip *chip = mtd->priv;
  132. /* De-select the NAND device */
  133. chip->select_chip(mtd, -1);
  134. /* Release the controller and the chip */
  135. spin_lock(&chip->controller->lock);
  136. chip->controller->active = NULL;
  137. chip->state = FL_READY;
  138. wake_up(&chip->controller->wq);
  139. spin_unlock(&chip->controller->lock);
  140. }
  141. /**
  142. * nand_read_byte - [DEFAULT] read one byte from the chip
  143. * @mtd: MTD device structure
  144. *
  145. * Default read function for 8bit buswith.
  146. */
  147. static uint8_t nand_read_byte(struct mtd_info *mtd)
  148. {
  149. struct nand_chip *chip = mtd->priv;
  150. return readb(chip->IO_ADDR_R);
  151. }
  152. /**
  153. * nand_read_byte16 - [DEFAULT] read one byte endianess aware from the chip
  154. * @mtd: MTD device structure
  155. *
  156. * Default read function for 16bit buswith with endianess conversion.
  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 buswith without endianess 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 buswith.
  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 buswith.
  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 buswith.
  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 buswith.
  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 buswith.
  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 buswith.
  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 catched 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 occures */
  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 dont 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 independend 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. DEBUG(MTD_DEBUG_LEVEL0, "%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. DEBUG(MTD_DEBUG_LEVEL3, "%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. DEBUG(MTD_DEBUG_LEVEL0, "%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. DEBUG(MTD_DEBUG_LEVEL3, "%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. DEBUG(MTD_DEBUG_LEVEL0, "%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. DEBUG(MTD_DEBUG_LEVEL0, "%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
  911. * layout.
  912. */
  913. static int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
  914. uint8_t *buf, int page)
  915. {
  916. chip->read_buf(mtd, buf, mtd->writesize);
  917. chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
  918. return 0;
  919. }
  920. /**
  921. * nand_read_page_raw_syndrome - [Intern] read raw page data without ecc
  922. * @mtd: mtd info structure
  923. * @chip: nand chip info structure
  924. * @buf: buffer to store read data
  925. * @page: page number to read
  926. *
  927. * We need a special oob layout and handling even when OOB isn't used.
  928. */
  929. static int nand_read_page_raw_syndrome(struct mtd_info *mtd,
  930. struct nand_chip *chip,
  931. uint8_t *buf, int page)
  932. {
  933. int eccsize = chip->ecc.size;
  934. int eccbytes = chip->ecc.bytes;
  935. uint8_t *oob = chip->oob_poi;
  936. int steps, size;
  937. for (steps = chip->ecc.steps; steps > 0; steps--) {
  938. chip->read_buf(mtd, buf, eccsize);
  939. buf += eccsize;
  940. if (chip->ecc.prepad) {
  941. chip->read_buf(mtd, oob, chip->ecc.prepad);
  942. oob += chip->ecc.prepad;
  943. }
  944. chip->read_buf(mtd, oob, eccbytes);
  945. oob += eccbytes;
  946. if (chip->ecc.postpad) {
  947. chip->read_buf(mtd, oob, chip->ecc.postpad);
  948. oob += chip->ecc.postpad;
  949. }
  950. }
  951. size = mtd->oobsize - (oob - chip->oob_poi);
  952. if (size)
  953. chip->read_buf(mtd, oob, size);
  954. return 0;
  955. }
  956. /**
  957. * nand_read_page_swecc - [REPLACABLE] software ecc based page read function
  958. * @mtd: mtd info structure
  959. * @chip: nand chip info structure
  960. * @buf: buffer to store read data
  961. * @page: page number to read
  962. */
  963. static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
  964. uint8_t *buf, int page)
  965. {
  966. int i, eccsize = chip->ecc.size;
  967. int eccbytes = chip->ecc.bytes;
  968. int eccsteps = chip->ecc.steps;
  969. uint8_t *p = buf;
  970. uint8_t *ecc_calc = chip->buffers->ecccalc;
  971. uint8_t *ecc_code = chip->buffers->ecccode;
  972. uint32_t *eccpos = chip->ecc.layout->eccpos;
  973. chip->ecc.read_page_raw(mtd, chip, buf, page);
  974. for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
  975. chip->ecc.calculate(mtd, p, &ecc_calc[i]);
  976. for (i = 0; i < chip->ecc.total; i++)
  977. ecc_code[i] = chip->oob_poi[eccpos[i]];
  978. eccsteps = chip->ecc.steps;
  979. p = buf;
  980. for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
  981. int stat;
  982. stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
  983. if (stat < 0)
  984. mtd->ecc_stats.failed++;
  985. else
  986. mtd->ecc_stats.corrected += stat;
  987. }
  988. return 0;
  989. }
  990. /**
  991. * nand_read_subpage - [REPLACABLE] software ecc based sub-page read function
  992. * @mtd: mtd info structure
  993. * @chip: nand chip info structure
  994. * @data_offs: offset of requested data within the page
  995. * @readlen: data length
  996. * @bufpoi: buffer to store read data
  997. */
  998. static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip,
  999. uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi)
  1000. {
  1001. int start_step, end_step, num_steps;
  1002. uint32_t *eccpos = chip->ecc.layout->eccpos;
  1003. uint8_t *p;
  1004. int data_col_addr, i, gaps = 0;
  1005. int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
  1006. int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
  1007. int index = 0;
  1008. /* Column address wihin the page aligned to ECC size (256bytes) */
  1009. start_step = data_offs / chip->ecc.size;
  1010. end_step = (data_offs + readlen - 1) / chip->ecc.size;
  1011. num_steps = end_step - start_step + 1;
  1012. /* Data size aligned to ECC ecc.size */
  1013. datafrag_len = num_steps * chip->ecc.size;
  1014. eccfrag_len = num_steps * chip->ecc.bytes;
  1015. data_col_addr = start_step * chip->ecc.size;
  1016. /* If we read not a page aligned data */
  1017. if (data_col_addr != 0)
  1018. chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_col_addr, -1);
  1019. p = bufpoi + data_col_addr;
  1020. chip->read_buf(mtd, p, datafrag_len);
  1021. /* Calculate ECC */
  1022. for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
  1023. chip->ecc.calculate(mtd, p, &chip->buffers->ecccalc[i]);
  1024. /*
  1025. * The performance is faster if we position offsets according to
  1026. * ecc.pos. Let's make sure that there are no gaps in ecc positions.
  1027. */
  1028. for (i = 0; i < eccfrag_len - 1; i++) {
  1029. if (eccpos[i + start_step * chip->ecc.bytes] + 1 !=
  1030. eccpos[i + start_step * chip->ecc.bytes + 1]) {
  1031. gaps = 1;
  1032. break;
  1033. }
  1034. }
  1035. if (gaps) {
  1036. chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
  1037. chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
  1038. } else {
  1039. /*
  1040. * Send the command to read the particular ecc bytes take care
  1041. * about buswidth alignment in read_buf.
  1042. */
  1043. index = start_step * chip->ecc.bytes;
  1044. aligned_pos = eccpos[index] & ~(busw - 1);
  1045. aligned_len = eccfrag_len;
  1046. if (eccpos[index] & (busw - 1))
  1047. aligned_len++;
  1048. if (eccpos[index + (num_steps * chip->ecc.bytes)] & (busw - 1))
  1049. aligned_len++;
  1050. chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
  1051. mtd->writesize + aligned_pos, -1);
  1052. chip->read_buf(mtd, &chip->oob_poi[aligned_pos], aligned_len);
  1053. }
  1054. for (i = 0; i < eccfrag_len; i++)
  1055. chip->buffers->ecccode[i] = chip->oob_poi[eccpos[i + index]];
  1056. p = bufpoi + data_col_addr;
  1057. for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
  1058. int stat;
  1059. stat = chip->ecc.correct(mtd, p,
  1060. &chip->buffers->ecccode[i], &chip->buffers->ecccalc[i]);
  1061. if (stat < 0)
  1062. mtd->ecc_stats.failed++;
  1063. else
  1064. mtd->ecc_stats.corrected += stat;
  1065. }
  1066. return 0;
  1067. }
  1068. /**
  1069. * nand_read_page_hwecc - [REPLACABLE] hardware ecc based page read function
  1070. * @mtd: mtd info structure
  1071. * @chip: nand chip info structure
  1072. * @buf: buffer to store read data
  1073. * @page: page number to read
  1074. *
  1075. * Not for syndrome calculating ecc controllers which need a special oob
  1076. * layout.
  1077. */
  1078. static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
  1079. uint8_t *buf, int page)
  1080. {
  1081. int i, eccsize = chip->ecc.size;
  1082. int eccbytes = chip->ecc.bytes;
  1083. int eccsteps = chip->ecc.steps;
  1084. uint8_t *p = buf;
  1085. uint8_t *ecc_calc = chip->buffers->ecccalc;
  1086. uint8_t *ecc_code = chip->buffers->ecccode;
  1087. uint32_t *eccpos = chip->ecc.layout->eccpos;
  1088. for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
  1089. chip->ecc.hwctl(mtd, NAND_ECC_READ);
  1090. chip->read_buf(mtd, p, eccsize);
  1091. chip->ecc.calculate(mtd, p, &ecc_calc[i]);
  1092. }
  1093. chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
  1094. for (i = 0; i < chip->ecc.total; i++)
  1095. ecc_code[i] = chip->oob_poi[eccpos[i]];
  1096. eccsteps = chip->ecc.steps;
  1097. p = buf;
  1098. for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
  1099. int stat;
  1100. stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
  1101. if (stat < 0)
  1102. mtd->ecc_stats.failed++;
  1103. else
  1104. mtd->ecc_stats.corrected += stat;
  1105. }
  1106. return 0;
  1107. }
  1108. /**
  1109. * nand_read_page_hwecc_oob_first - [REPLACABLE] hw ecc, read oob first
  1110. * @mtd: mtd info structure
  1111. * @chip: nand chip info structure
  1112. * @buf: buffer to store read data
  1113. * @page: page number to read
  1114. *
  1115. * Hardware ECC for large page chips, require OOB to be read first. For this
  1116. * ECC mode, the write_page method is re-used from ECC_HW. These methods
  1117. * read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with
  1118. * multiple ECC steps, follows the "infix ECC" scheme and reads/writes ECC from
  1119. * the data area, by overwriting the NAND manufacturer bad block markings.
  1120. */
  1121. static int nand_read_page_hwecc_oob_first(struct mtd_info *mtd,
  1122. struct nand_chip *chip, uint8_t *buf, int page)
  1123. {
  1124. int i, eccsize = chip->ecc.size;
  1125. int eccbytes = chip->ecc.bytes;
  1126. int eccsteps = chip->ecc.steps;
  1127. uint8_t *p = buf;
  1128. uint8_t *ecc_code = chip->buffers->ecccode;
  1129. uint32_t *eccpos = chip->ecc.layout->eccpos;
  1130. uint8_t *ecc_calc = chip->buffers->ecccalc;
  1131. /* Read the OOB area first */
  1132. chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
  1133. chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
  1134. chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
  1135. for (i = 0; i < chip->ecc.total; i++)
  1136. ecc_code[i] = chip->oob_poi[eccpos[i]];
  1137. for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
  1138. int stat;
  1139. chip->ecc.hwctl(mtd, NAND_ECC_READ);
  1140. chip->read_buf(mtd, p, eccsize);
  1141. chip->ecc.calculate(mtd, p, &ecc_calc[i]);
  1142. stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL);
  1143. if (stat < 0)
  1144. mtd->ecc_stats.failed++;
  1145. else
  1146. mtd->ecc_stats.corrected += stat;
  1147. }
  1148. return 0;
  1149. }
  1150. /**
  1151. * nand_read_page_syndrome - [REPLACABLE] hardware ecc syndrom based page read
  1152. * @mtd: mtd info structure
  1153. * @chip: nand chip info structure
  1154. * @buf: buffer to store read data
  1155. * @page: page number to read
  1156. *
  1157. * The hw generator calculates the error syndrome automatically. Therefore we
  1158. * need a special oob layout and handling.
  1159. */
  1160. static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
  1161. uint8_t *buf, int page)
  1162. {
  1163. int i, eccsize = chip->ecc.size;
  1164. int eccbytes = chip->ecc.bytes;
  1165. int eccsteps = chip->ecc.steps;
  1166. uint8_t *p = buf;
  1167. uint8_t *oob = chip->oob_poi;
  1168. for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
  1169. int stat;
  1170. chip->ecc.hwctl(mtd, NAND_ECC_READ);
  1171. chip->read_buf(mtd, p, eccsize);
  1172. if (chip->ecc.prepad) {
  1173. chip->read_buf(mtd, oob, chip->ecc.prepad);
  1174. oob += chip->ecc.prepad;
  1175. }
  1176. chip->ecc.hwctl(mtd, NAND_ECC_READSYN);
  1177. chip->read_buf(mtd, oob, eccbytes);
  1178. stat = chip->ecc.correct(mtd, p, oob, NULL);
  1179. if (stat < 0)
  1180. mtd->ecc_stats.failed++;
  1181. else
  1182. mtd->ecc_stats.corrected += stat;
  1183. oob += eccbytes;
  1184. if (chip->ecc.postpad) {
  1185. chip->read_buf(mtd, oob, chip->ecc.postpad);
  1186. oob += chip->ecc.postpad;
  1187. }
  1188. }
  1189. /* Calculate remaining oob bytes */
  1190. i = mtd->oobsize - (oob - chip->oob_poi);
  1191. if (i)
  1192. chip->read_buf(mtd, oob, i);
  1193. return 0;
  1194. }
  1195. /**
  1196. * nand_transfer_oob - [Internal] Transfer oob to client buffer
  1197. * @chip: nand chip structure
  1198. * @oob: oob destination address
  1199. * @ops: oob ops structure
  1200. * @len: size of oob to transfer
  1201. */
  1202. static uint8_t *nand_transfer_oob(struct nand_chip *chip, uint8_t *oob,
  1203. struct mtd_oob_ops *ops, size_t len)
  1204. {
  1205. switch (ops->mode) {
  1206. case MTD_OOB_PLACE:
  1207. case MTD_OOB_RAW:
  1208. memcpy(oob, chip->oob_poi + ops->ooboffs, len);
  1209. return oob + len;
  1210. case MTD_OOB_AUTO: {
  1211. struct nand_oobfree *free = chip->ecc.layout->oobfree;
  1212. uint32_t boffs = 0, roffs = ops->ooboffs;
  1213. size_t bytes = 0;
  1214. for (; free->length && len; free++, len -= bytes) {
  1215. /* Read request not from offset 0? */
  1216. if (unlikely(roffs)) {
  1217. if (roffs >= free->length) {
  1218. roffs -= free->length;
  1219. continue;
  1220. }
  1221. boffs = free->offset + roffs;
  1222. bytes = min_t(size_t, len,
  1223. (free->length - roffs));
  1224. roffs = 0;
  1225. } else {
  1226. bytes = min_t(size_t, len, free->length);
  1227. boffs = free->offset;
  1228. }
  1229. memcpy(oob, chip->oob_poi + boffs, bytes);
  1230. oob += bytes;
  1231. }
  1232. return oob;
  1233. }
  1234. default:
  1235. BUG();
  1236. }
  1237. return NULL;
  1238. }
  1239. /**
  1240. * nand_do_read_ops - [Internal] Read data with ECC
  1241. * @mtd: MTD device structure
  1242. * @from: offset to read from
  1243. * @ops: oob ops structure
  1244. *
  1245. * Internal function. Called with chip held.
  1246. */
  1247. static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
  1248. struct mtd_oob_ops *ops)
  1249. {
  1250. int chipnr, page, realpage, col, bytes, aligned;
  1251. struct nand_chip *chip = mtd->priv;
  1252. struct mtd_ecc_stats stats;
  1253. int blkcheck = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
  1254. int sndcmd = 1;
  1255. int ret = 0;
  1256. uint32_t readlen = ops->len;
  1257. uint32_t oobreadlen = ops->ooblen;
  1258. uint32_t max_oobsize = ops->mode == MTD_OOB_AUTO ?
  1259. mtd->oobavail : mtd->oobsize;
  1260. uint8_t *bufpoi, *oob, *buf;
  1261. stats = mtd->ecc_stats;
  1262. chipnr = (int)(from >> chip->chip_shift);
  1263. chip->select_chip(mtd, chipnr);
  1264. realpage = (int)(from >> chip->page_shift);
  1265. page = realpage & chip->pagemask;
  1266. col = (int)(from & (mtd->writesize - 1));
  1267. buf = ops->datbuf;
  1268. oob = ops->oobbuf;
  1269. while (1) {
  1270. bytes = min(mtd->writesize - col, readlen);
  1271. aligned = (bytes == mtd->writesize);
  1272. /* Is the current page in the buffer? */
  1273. if (realpage != chip->pagebuf || oob) {
  1274. bufpoi = aligned ? buf : chip->buffers->databuf;
  1275. if (likely(sndcmd)) {
  1276. chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
  1277. sndcmd = 0;
  1278. }
  1279. /* Now read the page into the buffer */
  1280. if (unlikely(ops->mode == MTD_OOB_RAW))
  1281. ret = chip->ecc.read_page_raw(mtd, chip,
  1282. bufpoi, page);
  1283. else if (!aligned && NAND_SUBPAGE_READ(chip) && !oob)
  1284. ret = chip->ecc.read_subpage(mtd, chip,
  1285. col, bytes, bufpoi);
  1286. else
  1287. ret = chip->ecc.read_page(mtd, chip, bufpoi,
  1288. page);
  1289. if (ret < 0)
  1290. break;
  1291. /* Transfer not aligned data */
  1292. if (!aligned) {
  1293. if (!NAND_SUBPAGE_READ(chip) && !oob &&
  1294. !(mtd->ecc_stats.failed - stats.failed))
  1295. chip->pagebuf = realpage;
  1296. memcpy(buf, chip->buffers->databuf + col, bytes);
  1297. }
  1298. buf += bytes;
  1299. if (unlikely(oob)) {
  1300. int toread = min(oobreadlen, max_oobsize);
  1301. if (toread) {
  1302. oob = nand_transfer_oob(chip,
  1303. oob, ops, toread);
  1304. oobreadlen -= toread;
  1305. }
  1306. }
  1307. if (!(chip->options & NAND_NO_READRDY)) {
  1308. /*
  1309. * Apply delay or wait for ready/busy pin. Do
  1310. * this before the AUTOINCR check, so no
  1311. * problems arise if a chip which does auto
  1312. * increment is marked as NOAUTOINCR by the
  1313. * board driver.
  1314. */
  1315. if (!chip->dev_ready)
  1316. udelay(chip->chip_delay);
  1317. else
  1318. nand_wait_ready(mtd);
  1319. }
  1320. } else {
  1321. memcpy(buf, chip->buffers->databuf + col, bytes);
  1322. buf += bytes;
  1323. }
  1324. readlen -= bytes;
  1325. if (!readlen)
  1326. break;
  1327. /* For subsequent reads align to page boundary */
  1328. col = 0;
  1329. /* Increment page address */
  1330. realpage++;
  1331. page = realpage & chip->pagemask;
  1332. /* Check, if we cross a chip boundary */
  1333. if (!page) {
  1334. chipnr++;
  1335. chip->select_chip(mtd, -1);
  1336. chip->select_chip(mtd, chipnr);
  1337. }
  1338. /*
  1339. * Check, if the chip supports auto page increment or if we
  1340. * have hit a block boundary.
  1341. */
  1342. if (!NAND_CANAUTOINCR(chip) || !(page & blkcheck))
  1343. sndcmd = 1;
  1344. }
  1345. ops->retlen = ops->len - (size_t) readlen;
  1346. if (oob)
  1347. ops->oobretlen = ops->ooblen - oobreadlen;
  1348. if (ret)
  1349. return ret;
  1350. if (mtd->ecc_stats.failed - stats.failed)
  1351. return -EBADMSG;
  1352. return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
  1353. }
  1354. /**
  1355. * nand_read - [MTD Interface] MTD compatibility function for nand_do_read_ecc
  1356. * @mtd: MTD device structure
  1357. * @from: offset to read from
  1358. * @len: number of bytes to read
  1359. * @retlen: pointer to variable to store the number of read bytes
  1360. * @buf: the databuffer to put data
  1361. *
  1362. * Get hold of the chip and call nand_do_read.
  1363. */
  1364. static int nand_read(struct mtd_info *mtd, loff_t from, size_t len,
  1365. size_t *retlen, uint8_t *buf)
  1366. {
  1367. struct nand_chip *chip = mtd->priv;
  1368. int ret;
  1369. /* Do not allow reads past end of device */
  1370. if ((from + len) > mtd->size)
  1371. return -EINVAL;
  1372. if (!len)
  1373. return 0;
  1374. nand_get_device(chip, mtd, FL_READING);
  1375. chip->ops.len = len;
  1376. chip->ops.datbuf = buf;
  1377. chip->ops.oobbuf = NULL;
  1378. ret = nand_do_read_ops(mtd, from, &chip->ops);
  1379. *retlen = chip->ops.retlen;
  1380. nand_release_device(mtd);
  1381. return ret;
  1382. }
  1383. /**
  1384. * nand_read_oob_std - [REPLACABLE] the most common OOB data read function
  1385. * @mtd: mtd info structure
  1386. * @chip: nand chip info structure
  1387. * @page: page number to read
  1388. * @sndcmd: flag whether to issue read command or not
  1389. */
  1390. static int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
  1391. int page, int sndcmd)
  1392. {
  1393. if (sndcmd) {
  1394. chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
  1395. sndcmd = 0;
  1396. }
  1397. chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
  1398. return sndcmd;
  1399. }
  1400. /**
  1401. * nand_read_oob_syndrome - [REPLACABLE] OOB data read function for HW ECC
  1402. * with syndromes
  1403. * @mtd: mtd info structure
  1404. * @chip: nand chip info structure
  1405. * @page: page number to read
  1406. * @sndcmd: flag whether to issue read command or not
  1407. */
  1408. static int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
  1409. int page, int sndcmd)
  1410. {
  1411. uint8_t *buf = chip->oob_poi;
  1412. int length = mtd->oobsize;
  1413. int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
  1414. int eccsize = chip->ecc.size;
  1415. uint8_t *bufpoi = buf;
  1416. int i, toread, sndrnd = 0, pos;
  1417. chip->cmdfunc(mtd, NAND_CMD_READ0, chip->ecc.size, page);
  1418. for (i = 0; i < chip->ecc.steps; i++) {
  1419. if (sndrnd) {
  1420. pos = eccsize + i * (eccsize + chunk);
  1421. if (mtd->writesize > 512)
  1422. chip->cmdfunc(mtd, NAND_CMD_RNDOUT, pos, -1);
  1423. else
  1424. chip->cmdfunc(mtd, NAND_CMD_READ0, pos, page);
  1425. } else
  1426. sndrnd = 1;
  1427. toread = min_t(int, length, chunk);
  1428. chip->read_buf(mtd, bufpoi, toread);
  1429. bufpoi += toread;
  1430. length -= toread;
  1431. }
  1432. if (length > 0)
  1433. chip->read_buf(mtd, bufpoi, length);
  1434. return 1;
  1435. }
  1436. /**
  1437. * nand_write_oob_std - [REPLACABLE] the most common OOB data write function
  1438. * @mtd: mtd info structure
  1439. * @chip: nand chip info structure
  1440. * @page: page number to write
  1441. */
  1442. static int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
  1443. int page)
  1444. {
  1445. int status = 0;
  1446. const uint8_t *buf = chip->oob_poi;
  1447. int length = mtd->oobsize;
  1448. chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
  1449. chip->write_buf(mtd, buf, length);
  1450. /* Send command to program the OOB data */
  1451. chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
  1452. status = chip->waitfunc(mtd, chip);
  1453. return status & NAND_STATUS_FAIL ? -EIO : 0;
  1454. }
  1455. /**
  1456. * nand_write_oob_syndrome - [REPLACABLE] OOB data write function for HW ECC
  1457. * with syndrome - only for large page flash
  1458. * @mtd: mtd info structure
  1459. * @chip: nand chip info structure
  1460. * @page: page number to write
  1461. */
  1462. static int nand_write_oob_syndrome(struct mtd_info *mtd,
  1463. struct nand_chip *chip, int page)
  1464. {
  1465. int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
  1466. int eccsize = chip->ecc.size, length = mtd->oobsize;
  1467. int i, len, pos, status = 0, sndcmd = 0, steps = chip->ecc.steps;
  1468. const uint8_t *bufpoi = chip->oob_poi;
  1469. /*
  1470. * data-ecc-data-ecc ... ecc-oob
  1471. * or
  1472. * data-pad-ecc-pad-data-pad .... ecc-pad-oob
  1473. */
  1474. if (!chip->ecc.prepad && !chip->ecc.postpad) {
  1475. pos = steps * (eccsize + chunk);
  1476. steps = 0;
  1477. } else
  1478. pos = eccsize;
  1479. chip->cmdfunc(mtd, NAND_CMD_SEQIN, pos, page);
  1480. for (i = 0; i < steps; i++) {
  1481. if (sndcmd) {
  1482. if (mtd->writesize <= 512) {
  1483. uint32_t fill = 0xFFFFFFFF;
  1484. len = eccsize;
  1485. while (len > 0) {
  1486. int num = min_t(int, len, 4);
  1487. chip->write_buf(mtd, (uint8_t *)&fill,
  1488. num);
  1489. len -= num;
  1490. }
  1491. } else {
  1492. pos = eccsize + i * (eccsize + chunk);
  1493. chip->cmdfunc(mtd, NAND_CMD_RNDIN, pos, -1);
  1494. }
  1495. } else
  1496. sndcmd = 1;
  1497. len = min_t(int, length, chunk);
  1498. chip->write_buf(mtd, bufpoi, len);
  1499. bufpoi += len;
  1500. length -= len;
  1501. }
  1502. if (length > 0)
  1503. chip->write_buf(mtd, bufpoi, length);
  1504. chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
  1505. status = chip->waitfunc(mtd, chip);
  1506. return status & NAND_STATUS_FAIL ? -EIO : 0;
  1507. }
  1508. /**
  1509. * nand_do_read_oob - [Intern] NAND read out-of-band
  1510. * @mtd: MTD device structure
  1511. * @from: offset to read from
  1512. * @ops: oob operations description structure
  1513. *
  1514. * NAND read out-of-band data from the spare area.
  1515. */
  1516. static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
  1517. struct mtd_oob_ops *ops)
  1518. {
  1519. int page, realpage, chipnr, sndcmd = 1;
  1520. struct nand_chip *chip = mtd->priv;
  1521. int blkcheck = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
  1522. int readlen = ops->ooblen;
  1523. int len;
  1524. uint8_t *buf = ops->oobbuf;
  1525. DEBUG(MTD_DEBUG_LEVEL3, "%s: from = 0x%08Lx, len = %i\n",
  1526. __func__, (unsigned long long)from, readlen);
  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. DEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt to start read "
  1533. "outside oob\n", __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. DEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt read beyond end "
  1541. "of device\n", __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. return 0;
  1586. }
  1587. /**
  1588. * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
  1589. * @mtd: MTD device structure
  1590. * @from: offset to read from
  1591. * @ops: oob operation description structure
  1592. *
  1593. * NAND read data and/or out-of-band data.
  1594. */
  1595. static int nand_read_oob(struct mtd_info *mtd, loff_t from,
  1596. struct mtd_oob_ops *ops)
  1597. {
  1598. struct nand_chip *chip = mtd->priv;
  1599. int ret = -ENOTSUPP;
  1600. ops->retlen = 0;
  1601. /* Do not allow reads past end of device */
  1602. if (ops->datbuf && (from + ops->len) > mtd->size) {
  1603. DEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt read "
  1604. "beyond end of device\n", __func__);
  1605. return -EINVAL;
  1606. }
  1607. nand_get_device(chip, mtd, FL_READING);
  1608. switch (ops->mode) {
  1609. case MTD_OOB_PLACE:
  1610. case MTD_OOB_AUTO:
  1611. case MTD_OOB_RAW:
  1612. break;
  1613. default:
  1614. goto out;
  1615. }
  1616. if (!ops->datbuf)
  1617. ret = nand_do_read_oob(mtd, from, ops);
  1618. else
  1619. ret = nand_do_read_ops(mtd, from, ops);
  1620. out:
  1621. nand_release_device(mtd);
  1622. return ret;
  1623. }
  1624. /**
  1625. * nand_write_page_raw - [Intern] raw page write function
  1626. * @mtd: mtd info structure
  1627. * @chip: nand chip info structure
  1628. * @buf: data buffer
  1629. *
  1630. * Not for syndrome calculating ecc controllers, which use a special oob
  1631. * layout.
  1632. */
  1633. static void nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
  1634. const uint8_t *buf)
  1635. {
  1636. chip->write_buf(mtd, buf, mtd->writesize);
  1637. chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
  1638. }
  1639. /**
  1640. * nand_write_page_raw_syndrome - [Intern] raw page write function
  1641. * @mtd: mtd info structure
  1642. * @chip: nand chip info structure
  1643. * @buf: data buffer
  1644. *
  1645. * We need a special oob layout and handling even when ECC isn't checked.
  1646. */
  1647. static void nand_write_page_raw_syndrome(struct mtd_info *mtd,
  1648. struct nand_chip *chip,
  1649. const uint8_t *buf)
  1650. {
  1651. int eccsize = chip->ecc.size;
  1652. int eccbytes = chip->ecc.bytes;
  1653. uint8_t *oob = chip->oob_poi;
  1654. int steps, size;
  1655. for (steps = chip->ecc.steps; steps > 0; steps--) {
  1656. chip->write_buf(mtd, buf, eccsize);
  1657. buf += eccsize;
  1658. if (chip->ecc.prepad) {
  1659. chip->write_buf(mtd, oob, chip->ecc.prepad);
  1660. oob += chip->ecc.prepad;
  1661. }
  1662. chip->read_buf(mtd, oob, eccbytes);
  1663. oob += eccbytes;
  1664. if (chip->ecc.postpad) {
  1665. chip->write_buf(mtd, oob, chip->ecc.postpad);
  1666. oob += chip->ecc.postpad;
  1667. }
  1668. }
  1669. size = mtd->oobsize - (oob - chip->oob_poi);
  1670. if (size)
  1671. chip->write_buf(mtd, oob, size);
  1672. }
  1673. /**
  1674. * nand_write_page_swecc - [REPLACABLE] software ecc based page write function
  1675. * @mtd: mtd info structure
  1676. * @chip: nand chip info structure
  1677. * @buf: data buffer
  1678. */
  1679. static void nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
  1680. const uint8_t *buf)
  1681. {
  1682. int i, eccsize = chip->ecc.size;
  1683. int eccbytes = chip->ecc.bytes;
  1684. int eccsteps = chip->ecc.steps;
  1685. uint8_t *ecc_calc = chip->buffers->ecccalc;
  1686. const uint8_t *p = buf;
  1687. uint32_t *eccpos = chip->ecc.layout->eccpos;
  1688. /* Software ecc calculation */
  1689. for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
  1690. chip->ecc.calculate(mtd, p, &ecc_calc[i]);
  1691. for (i = 0; i < chip->ecc.total; i++)
  1692. chip->oob_poi[eccpos[i]] = ecc_calc[i];
  1693. chip->ecc.write_page_raw(mtd, chip, buf);
  1694. }
  1695. /**
  1696. * nand_write_page_hwecc - [REPLACABLE] hardware ecc based page write function
  1697. * @mtd: mtd info structure
  1698. * @chip: nand chip info structure
  1699. * @buf: data buffer
  1700. */
  1701. static void nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
  1702. const uint8_t *buf)
  1703. {
  1704. int i, eccsize = chip->ecc.size;
  1705. int eccbytes = chip->ecc.bytes;
  1706. int eccsteps = chip->ecc.steps;
  1707. uint8_t *ecc_calc = chip->buffers->ecccalc;
  1708. const uint8_t *p = buf;
  1709. uint32_t *eccpos = chip->ecc.layout->eccpos;
  1710. for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
  1711. chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
  1712. chip->write_buf(mtd, p, eccsize);
  1713. chip->ecc.calculate(mtd, p, &ecc_calc[i]);
  1714. }
  1715. for (i = 0; i < chip->ecc.total; i++)
  1716. chip->oob_poi[eccpos[i]] = ecc_calc[i];
  1717. chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
  1718. }
  1719. /**
  1720. * nand_write_page_syndrome - [REPLACABLE] hardware ecc syndrom based page write
  1721. * @mtd: mtd info structure
  1722. * @chip: nand chip info structure
  1723. * @buf: data buffer
  1724. *
  1725. * The hw generator calculates the error syndrome automatically. Therefore we
  1726. * need a special oob layout and handling.
  1727. */
  1728. static void nand_write_page_syndrome(struct mtd_info *mtd,
  1729. struct nand_chip *chip, const uint8_t *buf)
  1730. {
  1731. int i, eccsize = chip->ecc.size;
  1732. int eccbytes = chip->ecc.bytes;
  1733. int eccsteps = chip->ecc.steps;
  1734. const uint8_t *p = buf;
  1735. uint8_t *oob = chip->oob_poi;
  1736. for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
  1737. chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
  1738. chip->write_buf(mtd, p, eccsize);
  1739. if (chip->ecc.prepad) {
  1740. chip->write_buf(mtd, oob, chip->ecc.prepad);
  1741. oob += chip->ecc.prepad;
  1742. }
  1743. chip->ecc.calculate(mtd, p, oob);
  1744. chip->write_buf(mtd, oob, eccbytes);
  1745. oob += eccbytes;
  1746. if (chip->ecc.postpad) {
  1747. chip->write_buf(mtd, oob, chip->ecc.postpad);
  1748. oob += chip->ecc.postpad;
  1749. }
  1750. }
  1751. /* Calculate remaining oob bytes */
  1752. i = mtd->oobsize - (oob - chip->oob_poi);
  1753. if (i)
  1754. chip->write_buf(mtd, oob, i);
  1755. }
  1756. /**
  1757. * nand_write_page - [REPLACEABLE] write one page
  1758. * @mtd: MTD device structure
  1759. * @chip: NAND chip descriptor
  1760. * @buf: the data to write
  1761. * @page: page number to write
  1762. * @cached: cached programming
  1763. * @raw: use _raw version of write_page
  1764. */
  1765. static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
  1766. const uint8_t *buf, int page, int cached, int raw)
  1767. {
  1768. int status;
  1769. chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
  1770. if (unlikely(raw))
  1771. chip->ecc.write_page_raw(mtd, chip, buf);
  1772. else
  1773. chip->ecc.write_page(mtd, chip, buf);
  1774. /*
  1775. * Cached progamming disabled for now, Not sure if its worth the
  1776. * trouble. The speed gain is not very impressive. (2.3->2.6Mib/s).
  1777. */
  1778. cached = 0;
  1779. if (!cached || !(chip->options & NAND_CACHEPRG)) {
  1780. chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
  1781. status = chip->waitfunc(mtd, chip);
  1782. /*
  1783. * See if operation failed and additional status checks are
  1784. * available.
  1785. */
  1786. if ((status & NAND_STATUS_FAIL) && (chip->errstat))
  1787. status = chip->errstat(mtd, chip, FL_WRITING, status,
  1788. page);
  1789. if (status & NAND_STATUS_FAIL)
  1790. return -EIO;
  1791. } else {
  1792. chip->cmdfunc(mtd, NAND_CMD_CACHEDPROG, -1, -1);
  1793. status = chip->waitfunc(mtd, chip);
  1794. }
  1795. #ifdef CONFIG_MTD_NAND_VERIFY_WRITE
  1796. /* Send command to read back the data */
  1797. chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
  1798. if (chip->verify_buf(mtd, buf, mtd->writesize))
  1799. return -EIO;
  1800. #endif
  1801. return 0;
  1802. }
  1803. /**
  1804. * nand_fill_oob - [Internal] Transfer client buffer to oob
  1805. * @mtd: MTD device structure
  1806. * @oob: oob data buffer
  1807. * @len: oob data write length
  1808. * @ops: oob ops structure
  1809. */
  1810. static uint8_t *nand_fill_oob(struct mtd_info *mtd, uint8_t *oob, size_t len,
  1811. struct mtd_oob_ops *ops)
  1812. {
  1813. struct nand_chip *chip = mtd->priv;
  1814. /*
  1815. * Initialise to all 0xFF, to avoid the possibility of left over OOB
  1816. * data from a previous OOB read.
  1817. */
  1818. memset(chip->oob_poi, 0xff, mtd->oobsize);
  1819. switch (ops->mode) {
  1820. case MTD_OOB_PLACE:
  1821. case MTD_OOB_RAW:
  1822. memcpy(chip->oob_poi + ops->ooboffs, oob, len);
  1823. return oob + len;
  1824. case MTD_OOB_AUTO: {
  1825. struct nand_oobfree *free = chip->ecc.layout->oobfree;
  1826. uint32_t boffs = 0, woffs = ops->ooboffs;
  1827. size_t bytes = 0;
  1828. for (; free->length && len; free++, len -= bytes) {
  1829. /* Write request not from offset 0? */
  1830. if (unlikely(woffs)) {
  1831. if (woffs >= free->length) {
  1832. woffs -= free->length;
  1833. continue;
  1834. }
  1835. boffs = free->offset + woffs;
  1836. bytes = min_t(size_t, len,
  1837. (free->length - woffs));
  1838. woffs = 0;
  1839. } else {
  1840. bytes = min_t(size_t, len, free->length);
  1841. boffs = free->offset;
  1842. }
  1843. memcpy(chip->oob_poi + boffs, oob, bytes);
  1844. oob += bytes;
  1845. }
  1846. return oob;
  1847. }
  1848. default:
  1849. BUG();
  1850. }
  1851. return NULL;
  1852. }
  1853. #define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0)
  1854. /**
  1855. * nand_do_write_ops - [Internal] NAND write with ECC
  1856. * @mtd: MTD device structure
  1857. * @to: offset to write to
  1858. * @ops: oob operations description structure
  1859. *
  1860. * NAND write with ECC.
  1861. */
  1862. static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
  1863. struct mtd_oob_ops *ops)
  1864. {
  1865. int chipnr, realpage, page, blockmask, column;
  1866. struct nand_chip *chip = mtd->priv;
  1867. uint32_t writelen = ops->len;
  1868. uint32_t oobwritelen = ops->ooblen;
  1869. uint32_t oobmaxlen = ops->mode == MTD_OOB_AUTO ?
  1870. mtd->oobavail : mtd->oobsize;
  1871. uint8_t *oob = ops->oobbuf;
  1872. uint8_t *buf = ops->datbuf;
  1873. int ret, subpage;
  1874. ops->retlen = 0;
  1875. if (!writelen)
  1876. return 0;
  1877. /* Reject writes, which are not page aligned */
  1878. if (NOTALIGNED(to) || NOTALIGNED(ops->len)) {
  1879. printk(KERN_NOTICE "%s: Attempt to write not "
  1880. "page aligned data\n", __func__);
  1881. return -EINVAL;
  1882. }
  1883. column = to & (mtd->writesize - 1);
  1884. subpage = column || (writelen & (mtd->writesize - 1));
  1885. if (subpage && oob)
  1886. return -EINVAL;
  1887. chipnr = (int)(to >> chip->chip_shift);
  1888. chip->select_chip(mtd, chipnr);
  1889. /* Check, if it is write protected */
  1890. if (nand_check_wp(mtd))
  1891. return -EIO;
  1892. realpage = (int)(to >> chip->page_shift);
  1893. page = realpage & chip->pagemask;
  1894. blockmask = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
  1895. /* Invalidate the page cache, when we write to the cached page */
  1896. if (to <= (chip->pagebuf << chip->page_shift) &&
  1897. (chip->pagebuf << chip->page_shift) < (to + ops->len))
  1898. chip->pagebuf = -1;
  1899. /* Don't allow multipage oob writes with offset */
  1900. if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen))
  1901. return -EINVAL;
  1902. while (1) {
  1903. int bytes = mtd->writesize;
  1904. int cached = writelen > bytes && page != blockmask;
  1905. uint8_t *wbuf = buf;
  1906. /* Partial page write? */
  1907. if (unlikely(column || writelen < (mtd->writesize - 1))) {
  1908. cached = 0;
  1909. bytes = min_t(int, bytes - column, (int) writelen);
  1910. chip->pagebuf = -1;
  1911. memset(chip->buffers->databuf, 0xff, mtd->writesize);
  1912. memcpy(&chip->buffers->databuf[column], buf, bytes);
  1913. wbuf = chip->buffers->databuf;
  1914. }
  1915. if (unlikely(oob)) {
  1916. size_t len = min(oobwritelen, oobmaxlen);
  1917. oob = nand_fill_oob(mtd, oob, len, ops);
  1918. oobwritelen -= len;
  1919. } else {
  1920. /* We still need to erase leftover OOB data */
  1921. memset(chip->oob_poi, 0xff, mtd->oobsize);
  1922. }
  1923. ret = chip->write_page(mtd, chip, wbuf, page, cached,
  1924. (ops->mode == MTD_OOB_RAW));
  1925. if (ret)
  1926. break;
  1927. writelen -= bytes;
  1928. if (!writelen)
  1929. break;
  1930. column = 0;
  1931. buf += bytes;
  1932. realpage++;
  1933. page = realpage & chip->pagemask;
  1934. /* Check, if we cross a chip boundary */
  1935. if (!page) {
  1936. chipnr++;
  1937. chip->select_chip(mtd, -1);
  1938. chip->select_chip(mtd, chipnr);
  1939. }
  1940. }
  1941. ops->retlen = ops->len - writelen;
  1942. if (unlikely(oob))
  1943. ops->oobretlen = ops->ooblen;
  1944. return ret;
  1945. }
  1946. /**
  1947. * panic_nand_write - [MTD Interface] NAND write with ECC
  1948. * @mtd: MTD device structure
  1949. * @to: offset to write to
  1950. * @len: number of bytes to write
  1951. * @retlen: pointer to variable to store the number of written bytes
  1952. * @buf: the data to write
  1953. *
  1954. * NAND write with ECC. Used when performing writes in interrupt context, this
  1955. * may for example be called by mtdoops when writing an oops while in panic.
  1956. */
  1957. static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
  1958. size_t *retlen, const uint8_t *buf)
  1959. {
  1960. struct nand_chip *chip = mtd->priv;
  1961. int ret;
  1962. /* Do not allow reads past end of device */
  1963. if ((to + len) > mtd->size)
  1964. return -EINVAL;
  1965. if (!len)
  1966. return 0;
  1967. /* Wait for the device to get ready */
  1968. panic_nand_wait(mtd, chip, 400);
  1969. /* Grab the device */
  1970. panic_nand_get_device(chip, mtd, FL_WRITING);
  1971. chip->ops.len = len;
  1972. chip->ops.datbuf = (uint8_t *)buf;
  1973. chip->ops.oobbuf = NULL;
  1974. ret = nand_do_write_ops(mtd, to, &chip->ops);
  1975. *retlen = chip->ops.retlen;
  1976. return ret;
  1977. }
  1978. /**
  1979. * nand_write - [MTD Interface] NAND write with ECC
  1980. * @mtd: MTD device structure
  1981. * @to: offset to write to
  1982. * @len: number of bytes to write
  1983. * @retlen: pointer to variable to store the number of written bytes
  1984. * @buf: the data to write
  1985. *
  1986. * NAND write with ECC.
  1987. */
  1988. static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
  1989. size_t *retlen, const uint8_t *buf)
  1990. {
  1991. struct nand_chip *chip = mtd->priv;
  1992. int ret;
  1993. /* Do not allow reads past end of device */
  1994. if ((to + len) > mtd->size)
  1995. return -EINVAL;
  1996. if (!len)
  1997. return 0;
  1998. nand_get_device(chip, mtd, FL_WRITING);
  1999. chip->ops.len = len;
  2000. chip->ops.datbuf = (uint8_t *)buf;
  2001. chip->ops.oobbuf = NULL;
  2002. ret = nand_do_write_ops(mtd, to, &chip->ops);
  2003. *retlen = chip->ops.retlen;
  2004. nand_release_device(mtd);
  2005. return ret;
  2006. }
  2007. /**
  2008. * nand_do_write_oob - [MTD Interface] NAND write out-of-band
  2009. * @mtd: MTD device structure
  2010. * @to: offset to write to
  2011. * @ops: oob operation description structure
  2012. *
  2013. * NAND write out-of-band.
  2014. */
  2015. static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
  2016. struct mtd_oob_ops *ops)
  2017. {
  2018. int chipnr, page, status, len;
  2019. struct nand_chip *chip = mtd->priv;
  2020. DEBUG(MTD_DEBUG_LEVEL3, "%s: to = 0x%08x, len = %i\n",
  2021. __func__, (unsigned int)to, (int)ops->ooblen);
  2022. if (ops->mode == MTD_OOB_AUTO)
  2023. len = chip->ecc.layout->oobavail;
  2024. else
  2025. len = mtd->oobsize;
  2026. /* Do not allow write past end of page */
  2027. if ((ops->ooboffs + ops->ooblen) > len) {
  2028. DEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt to write "
  2029. "past end of page\n", __func__);
  2030. return -EINVAL;
  2031. }
  2032. if (unlikely(ops->ooboffs >= len)) {
  2033. DEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt to start "
  2034. "write outside oob\n", __func__);
  2035. return -EINVAL;
  2036. }
  2037. /* Do not allow write past end of device */
  2038. if (unlikely(to >= mtd->size ||
  2039. ops->ooboffs + ops->ooblen >
  2040. ((mtd->size >> chip->page_shift) -
  2041. (to >> chip->page_shift)) * len)) {
  2042. DEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt write beyond "
  2043. "end of device\n", __func__);
  2044. return -EINVAL;
  2045. }
  2046. chipnr = (int)(to >> chip->chip_shift);
  2047. chip->select_chip(mtd, chipnr);
  2048. /* Shift to get page */
  2049. page = (int)(to >> chip->page_shift);
  2050. /*
  2051. * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
  2052. * of my DiskOnChip 2000 test units) will clear the whole data page too
  2053. * if we don't do this. I have no clue why, but I seem to have 'fixed'
  2054. * it in the doc2000 driver in August 1999. dwmw2.
  2055. */
  2056. chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
  2057. /* Check, if it is write protected */
  2058. if (nand_check_wp(mtd))
  2059. return -EROFS;
  2060. /* Invalidate the page cache, if we write to the cached page */
  2061. if (page == chip->pagebuf)
  2062. chip->pagebuf = -1;
  2063. nand_fill_oob(mtd, ops->oobbuf, ops->ooblen, ops);
  2064. status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
  2065. if (status)
  2066. return status;
  2067. ops->oobretlen = ops->ooblen;
  2068. return 0;
  2069. }
  2070. /**
  2071. * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
  2072. * @mtd: MTD device structure
  2073. * @to: offset to write to
  2074. * @ops: oob operation description structure
  2075. */
  2076. static int nand_write_oob(struct mtd_info *mtd, loff_t to,
  2077. struct mtd_oob_ops *ops)
  2078. {
  2079. struct nand_chip *chip = mtd->priv;
  2080. int ret = -ENOTSUPP;
  2081. ops->retlen = 0;
  2082. /* Do not allow writes past end of device */
  2083. if (ops->datbuf && (to + ops->len) > mtd->size) {
  2084. DEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt write beyond "
  2085. "end of device\n", __func__);
  2086. return -EINVAL;
  2087. }
  2088. nand_get_device(chip, mtd, FL_WRITING);
  2089. switch (ops->mode) {
  2090. case MTD_OOB_PLACE:
  2091. case MTD_OOB_AUTO:
  2092. case MTD_OOB_RAW:
  2093. break;
  2094. default:
  2095. goto out;
  2096. }
  2097. if (!ops->datbuf)
  2098. ret = nand_do_write_oob(mtd, to, ops);
  2099. else
  2100. ret = nand_do_write_ops(mtd, to, ops);
  2101. out:
  2102. nand_release_device(mtd);
  2103. return ret;
  2104. }
  2105. /**
  2106. * single_erease_cmd - [GENERIC] NAND standard block erase command function
  2107. * @mtd: MTD device structure
  2108. * @page: the page address of the block which will be erased
  2109. *
  2110. * Standard erase command for NAND chips.
  2111. */
  2112. static void single_erase_cmd(struct mtd_info *mtd, int page)
  2113. {
  2114. struct nand_chip *chip = mtd->priv;
  2115. /* Send commands to erase a block */
  2116. chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
  2117. chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
  2118. }
  2119. /**
  2120. * multi_erease_cmd - [GENERIC] AND specific block erase command function
  2121. * @mtd: MTD device structure
  2122. * @page: the page address of the block which will be erased
  2123. *
  2124. * AND multi block erase command function. Erase 4 consecutive blocks.
  2125. */
  2126. static void multi_erase_cmd(struct mtd_info *mtd, int page)
  2127. {
  2128. struct nand_chip *chip = mtd->priv;
  2129. /* Send commands to erase a block */
  2130. chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
  2131. chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
  2132. chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
  2133. chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
  2134. chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
  2135. }
  2136. /**
  2137. * nand_erase - [MTD Interface] erase block(s)
  2138. * @mtd: MTD device structure
  2139. * @instr: erase instruction
  2140. *
  2141. * Erase one ore more blocks.
  2142. */
  2143. static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
  2144. {
  2145. return nand_erase_nand(mtd, instr, 0);
  2146. }
  2147. #define BBT_PAGE_MASK 0xffffff3f
  2148. /**
  2149. * nand_erase_nand - [Internal] erase block(s)
  2150. * @mtd: MTD device structure
  2151. * @instr: erase instruction
  2152. * @allowbbt: allow erasing the bbt area
  2153. *
  2154. * Erase one ore more blocks.
  2155. */
  2156. int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
  2157. int allowbbt)
  2158. {
  2159. int page, status, pages_per_block, ret, chipnr;
  2160. struct nand_chip *chip = mtd->priv;
  2161. loff_t rewrite_bbt[NAND_MAX_CHIPS] = {0};
  2162. unsigned int bbt_masked_page = 0xffffffff;
  2163. loff_t len;
  2164. DEBUG(MTD_DEBUG_LEVEL3, "%s: start = 0x%012llx, len = %llu\n",
  2165. __func__, (unsigned long long)instr->addr,
  2166. (unsigned long long)instr->len);
  2167. if (check_offs_len(mtd, instr->addr, instr->len))
  2168. return -EINVAL;
  2169. instr->fail_addr = MTD_FAIL_ADDR_UNKNOWN;
  2170. /* Grab the lock and see if the device is available */
  2171. nand_get_device(chip, mtd, FL_ERASING);
  2172. /* Shift to get first page */
  2173. page = (int)(instr->addr >> chip->page_shift);
  2174. chipnr = (int)(instr->addr >> chip->chip_shift);
  2175. /* Calculate pages in each block */
  2176. pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
  2177. /* Select the NAND device */
  2178. chip->select_chip(mtd, chipnr);
  2179. /* Check, if it is write protected */
  2180. if (nand_check_wp(mtd)) {
  2181. DEBUG(MTD_DEBUG_LEVEL0, "%s: Device is write protected!!!\n",
  2182. __func__);
  2183. instr->state = MTD_ERASE_FAILED;
  2184. goto erase_exit;
  2185. }
  2186. /*
  2187. * If BBT requires refresh, set the BBT page mask to see if the BBT
  2188. * should be rewritten. Otherwise the mask is set to 0xffffffff which
  2189. * can not be matched. This is also done when the bbt is actually
  2190. * erased to avoid recusrsive updates.
  2191. */
  2192. if (chip->options & BBT_AUTO_REFRESH && !allowbbt)
  2193. bbt_masked_page = chip->bbt_td->pages[chipnr] & BBT_PAGE_MASK;
  2194. /* Loop through the pages */
  2195. len = instr->len;
  2196. instr->state = MTD_ERASING;
  2197. while (len) {
  2198. /* Heck if we have a bad block, we do not erase bad blocks! */
  2199. if (nand_block_checkbad(mtd, ((loff_t) page) <<
  2200. chip->page_shift, 0, allowbbt)) {
  2201. printk(KERN_WARNING "%s: attempt to erase a bad block "
  2202. "at page 0x%08x\n", __func__, page);
  2203. instr->state = MTD_ERASE_FAILED;
  2204. goto erase_exit;
  2205. }
  2206. /*
  2207. * Invalidate the page cache, if we erase the block which
  2208. * contains the current cached page.
  2209. */
  2210. if (page <= chip->pagebuf && chip->pagebuf <
  2211. (page + pages_per_block))
  2212. chip->pagebuf = -1;
  2213. chip->erase_cmd(mtd, page & chip->pagemask);
  2214. status = chip->waitfunc(mtd, chip);
  2215. /*
  2216. * See if operation failed and additional status checks are
  2217. * available
  2218. */
  2219. if ((status & NAND_STATUS_FAIL) && (chip->errstat))
  2220. status = chip->errstat(mtd, chip, FL_ERASING,
  2221. status, page);
  2222. /* See if block erase succeeded */
  2223. if (status & NAND_STATUS_FAIL) {
  2224. DEBUG(MTD_DEBUG_LEVEL0, "%s: Failed erase, "
  2225. "page 0x%08x\n", __func__, page);
  2226. instr->state = MTD_ERASE_FAILED;
  2227. instr->fail_addr =
  2228. ((loff_t)page << chip->page_shift);
  2229. goto erase_exit;
  2230. }
  2231. /*
  2232. * If BBT requires refresh, set the BBT rewrite flag to the
  2233. * page being erased.
  2234. */
  2235. if (bbt_masked_page != 0xffffffff &&
  2236. (page & BBT_PAGE_MASK) == bbt_masked_page)
  2237. rewrite_bbt[chipnr] =
  2238. ((loff_t)page << chip->page_shift);
  2239. /* Increment page address and decrement length */
  2240. len -= (1 << chip->phys_erase_shift);
  2241. page += pages_per_block;
  2242. /* Check, if we cross a chip boundary */
  2243. if (len && !(page & chip->pagemask)) {
  2244. chipnr++;
  2245. chip->select_chip(mtd, -1);
  2246. chip->select_chip(mtd, chipnr);
  2247. /*
  2248. * If BBT requires refresh and BBT-PERCHIP, set the BBT
  2249. * page mask to see if this BBT should be rewritten.
  2250. */
  2251. if (bbt_masked_page != 0xffffffff &&
  2252. (chip->bbt_td->options & NAND_BBT_PERCHIP))
  2253. bbt_masked_page = chip->bbt_td->pages[chipnr] &
  2254. BBT_PAGE_MASK;
  2255. }
  2256. }
  2257. instr->state = MTD_ERASE_DONE;
  2258. erase_exit:
  2259. ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
  2260. /* Deselect and wake up anyone waiting on the device */
  2261. nand_release_device(mtd);
  2262. /* Do call back function */
  2263. if (!ret)
  2264. mtd_erase_callback(instr);
  2265. /*
  2266. * If BBT requires refresh and erase was successful, rewrite any
  2267. * selected bad block tables.
  2268. */
  2269. if (bbt_masked_page == 0xffffffff || ret)
  2270. return ret;
  2271. for (chipnr = 0; chipnr < chip->numchips; chipnr++) {
  2272. if (!rewrite_bbt[chipnr])
  2273. continue;
  2274. /* Update the BBT for chip */
  2275. DEBUG(MTD_DEBUG_LEVEL0, "%s: nand_update_bbt "
  2276. "(%d:0x%0llx 0x%0x)\n", __func__, chipnr,
  2277. rewrite_bbt[chipnr], chip->bbt_td->pages[chipnr]);
  2278. nand_update_bbt(mtd, rewrite_bbt[chipnr]);
  2279. }
  2280. /* Return more or less happy */
  2281. return ret;
  2282. }
  2283. /**
  2284. * nand_sync - [MTD Interface] sync
  2285. * @mtd: MTD device structure
  2286. *
  2287. * Sync is actually a wait for chip ready function.
  2288. */
  2289. static void nand_sync(struct mtd_info *mtd)
  2290. {
  2291. struct nand_chip *chip = mtd->priv;
  2292. DEBUG(MTD_DEBUG_LEVEL3, "%s: called\n", __func__);
  2293. /* Grab the lock and see if the device is available */
  2294. nand_get_device(chip, mtd, FL_SYNCING);
  2295. /* Release it and go back */
  2296. nand_release_device(mtd);
  2297. }
  2298. /**
  2299. * nand_block_isbad - [MTD Interface] Check if block at offset is bad
  2300. * @mtd: MTD device structure
  2301. * @offs: offset relative to mtd start
  2302. */
  2303. static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
  2304. {
  2305. /* Check for invalid offset */
  2306. if (offs > mtd->size)
  2307. return -EINVAL;
  2308. return nand_block_checkbad(mtd, offs, 1, 0);
  2309. }
  2310. /**
  2311. * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
  2312. * @mtd: MTD device structure
  2313. * @ofs: offset relative to mtd start
  2314. */
  2315. static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
  2316. {
  2317. struct nand_chip *chip = mtd->priv;
  2318. int ret;
  2319. ret = nand_block_isbad(mtd, ofs);
  2320. if (ret) {
  2321. /* If it was bad already, return success and do nothing */
  2322. if (ret > 0)
  2323. return 0;
  2324. return ret;
  2325. }
  2326. return chip->block_markbad(mtd, ofs);
  2327. }
  2328. /**
  2329. * nand_suspend - [MTD Interface] Suspend the NAND flash
  2330. * @mtd: MTD device structure
  2331. */
  2332. static int nand_suspend(struct mtd_info *mtd)
  2333. {
  2334. struct nand_chip *chip = mtd->priv;
  2335. return nand_get_device(chip, mtd, FL_PM_SUSPENDED);
  2336. }
  2337. /**
  2338. * nand_resume - [MTD Interface] Resume the NAND flash
  2339. * @mtd: MTD device structure
  2340. */
  2341. static void nand_resume(struct mtd_info *mtd)
  2342. {
  2343. struct nand_chip *chip = mtd->priv;
  2344. if (chip->state == FL_PM_SUSPENDED)
  2345. nand_release_device(mtd);
  2346. else
  2347. printk(KERN_ERR "%s called for a chip which is not "
  2348. "in suspended state\n", __func__);
  2349. }
  2350. /* Set default functions */
  2351. static void nand_set_defaults(struct nand_chip *chip, int busw)
  2352. {
  2353. /* check for proper chip_delay setup, set 20us if not */
  2354. if (!chip->chip_delay)
  2355. chip->chip_delay = 20;
  2356. /* check, if a user supplied command function given */
  2357. if (chip->cmdfunc == NULL)
  2358. chip->cmdfunc = nand_command;
  2359. /* check, if a user supplied wait function given */
  2360. if (chip->waitfunc == NULL)
  2361. chip->waitfunc = nand_wait;
  2362. if (!chip->select_chip)
  2363. chip->select_chip = nand_select_chip;
  2364. if (!chip->read_byte)
  2365. chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
  2366. if (!chip->read_word)
  2367. chip->read_word = nand_read_word;
  2368. if (!chip->block_bad)
  2369. chip->block_bad = nand_block_bad;
  2370. if (!chip->block_markbad)
  2371. chip->block_markbad = nand_default_block_markbad;
  2372. if (!chip->write_buf)
  2373. chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
  2374. if (!chip->read_buf)
  2375. chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
  2376. if (!chip->verify_buf)
  2377. chip->verify_buf = busw ? nand_verify_buf16 : nand_verify_buf;
  2378. if (!chip->scan_bbt)
  2379. chip->scan_bbt = nand_default_bbt;
  2380. if (!chip->controller) {
  2381. chip->controller = &chip->hwcontrol;
  2382. spin_lock_init(&chip->controller->lock);
  2383. init_waitqueue_head(&chip->controller->wq);
  2384. }
  2385. }
  2386. /* Sanitize ONFI strings so we can safely print them */
  2387. static void sanitize_string(uint8_t *s, size_t len)
  2388. {
  2389. ssize_t i;
  2390. /* Null terminate */
  2391. s[len - 1] = 0;
  2392. /* Remove non printable chars */
  2393. for (i = 0; i < len - 1; i++) {
  2394. if (s[i] < ' ' || s[i] > 127)
  2395. s[i] = '?';
  2396. }
  2397. /* Remove trailing spaces */
  2398. strim(s);
  2399. }
  2400. static u16 onfi_crc16(u16 crc, u8 const *p, size_t len)
  2401. {
  2402. int i;
  2403. while (len--) {
  2404. crc ^= *p++ << 8;
  2405. for (i = 0; i < 8; i++)
  2406. crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0);
  2407. }
  2408. return crc;
  2409. }
  2410. /*
  2411. * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise.
  2412. */
  2413. static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip,
  2414. int busw)
  2415. {
  2416. struct nand_onfi_params *p = &chip->onfi_params;
  2417. int i;
  2418. int val;
  2419. /* Try ONFI for unknow chip or LP */
  2420. chip->cmdfunc(mtd, NAND_CMD_READID, 0x20, -1);
  2421. if (chip->read_byte(mtd) != 'O' || chip->read_byte(mtd) != 'N' ||
  2422. chip->read_byte(mtd) != 'F' || chip->read_byte(mtd) != 'I')
  2423. return 0;
  2424. printk(KERN_INFO "ONFI flash detected\n");
  2425. chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
  2426. for (i = 0; i < 3; i++) {
  2427. chip->read_buf(mtd, (uint8_t *)p, sizeof(*p));
  2428. if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 254) ==
  2429. le16_to_cpu(p->crc)) {
  2430. printk(KERN_INFO "ONFI param page %d valid\n", i);
  2431. break;
  2432. }
  2433. }
  2434. if (i == 3)
  2435. return 0;
  2436. /* Check version */
  2437. val = le16_to_cpu(p->revision);
  2438. if (val & (1 << 5))
  2439. chip->onfi_version = 23;
  2440. else if (val & (1 << 4))
  2441. chip->onfi_version = 22;
  2442. else if (val & (1 << 3))
  2443. chip->onfi_version = 21;
  2444. else if (val & (1 << 2))
  2445. chip->onfi_version = 20;
  2446. else if (val & (1 << 1))
  2447. chip->onfi_version = 10;
  2448. else
  2449. chip->onfi_version = 0;
  2450. if (!chip->onfi_version) {
  2451. printk(KERN_INFO "%s: unsupported ONFI version: %d\n",
  2452. __func__, val);
  2453. return 0;
  2454. }
  2455. sanitize_string(p->manufacturer, sizeof(p->manufacturer));
  2456. sanitize_string(p->model, sizeof(p->model));
  2457. if (!mtd->name)
  2458. mtd->name = p->model;
  2459. mtd->writesize = le32_to_cpu(p->byte_per_page);
  2460. mtd->erasesize = le32_to_cpu(p->pages_per_block) * mtd->writesize;
  2461. mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
  2462. chip->chipsize = (uint64_t)le32_to_cpu(p->blocks_per_lun) * mtd->erasesize;
  2463. busw = 0;
  2464. if (le16_to_cpu(p->features) & 1)
  2465. busw = NAND_BUSWIDTH_16;
  2466. chip->options &= ~NAND_CHIPOPTIONS_MSK;
  2467. chip->options |= (NAND_NO_READRDY |
  2468. NAND_NO_AUTOINCR) & NAND_CHIPOPTIONS_MSK;
  2469. return 1;
  2470. }
  2471. /*
  2472. * Get the flash and manufacturer id and lookup if the type is supported.
  2473. */
  2474. static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
  2475. struct nand_chip *chip,
  2476. int busw,
  2477. int *maf_id, int *dev_id,
  2478. struct nand_flash_dev *type)
  2479. {
  2480. int i, maf_idx;
  2481. u8 id_data[8];
  2482. int ret;
  2483. /* Select the device */
  2484. chip->select_chip(mtd, 0);
  2485. /*
  2486. * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
  2487. * after power-up.
  2488. */
  2489. chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
  2490. /* Send the command for reading device ID */
  2491. chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
  2492. /* Read manufacturer and device IDs */
  2493. *maf_id = chip->read_byte(mtd);
  2494. *dev_id = chip->read_byte(mtd);
  2495. /*
  2496. * Try again to make sure, as some systems the bus-hold or other
  2497. * interface concerns can cause random data which looks like a
  2498. * possibly credible NAND flash to appear. If the two results do
  2499. * not match, ignore the device completely.
  2500. */
  2501. chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
  2502. for (i = 0; i < 2; i++)
  2503. id_data[i] = chip->read_byte(mtd);
  2504. if (id_data[0] != *maf_id || id_data[1] != *dev_id) {
  2505. printk(KERN_INFO "%s: second ID read did not match "
  2506. "%02x,%02x against %02x,%02x\n", __func__,
  2507. *maf_id, *dev_id, id_data[0], id_data[1]);
  2508. return ERR_PTR(-ENODEV);
  2509. }
  2510. if (!type)
  2511. type = nand_flash_ids;
  2512. for (; type->name != NULL; type++)
  2513. if (*dev_id == type->id)
  2514. break;
  2515. chip->onfi_version = 0;
  2516. if (!type->name || !type->pagesize) {
  2517. /* Check is chip is ONFI compliant */
  2518. ret = nand_flash_detect_onfi(mtd, chip, busw);
  2519. if (ret)
  2520. goto ident_done;
  2521. }
  2522. chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
  2523. /* Read entire ID string */
  2524. for (i = 0; i < 8; i++)
  2525. id_data[i] = chip->read_byte(mtd);
  2526. if (!type->name)
  2527. return ERR_PTR(-ENODEV);
  2528. if (!mtd->name)
  2529. mtd->name = type->name;
  2530. chip->chipsize = (uint64_t)type->chipsize << 20;
  2531. if (!type->pagesize && chip->init_size) {
  2532. /* Set the pagesize, oobsize, erasesize by the driver */
  2533. busw = chip->init_size(mtd, chip, id_data);
  2534. } else if (!type->pagesize) {
  2535. int extid;
  2536. /* The 3rd id byte holds MLC / multichip data */
  2537. chip->cellinfo = id_data[2];
  2538. /* The 4th id byte is the important one */
  2539. extid = id_data[3];
  2540. /*
  2541. * Field definitions are in the following datasheets:
  2542. * Old style (4,5 byte ID): Samsung K9GAG08U0M (p.32)
  2543. * New style (6 byte ID): Samsung K9GBG08U0M (p.40)
  2544. *
  2545. * Check for wraparound + Samsung ID + nonzero 6th byte
  2546. * to decide what to do.
  2547. */
  2548. if (id_data[0] == id_data[6] && id_data[1] == id_data[7] &&
  2549. id_data[0] == NAND_MFR_SAMSUNG &&
  2550. (chip->cellinfo & NAND_CI_CELLTYPE_MSK) &&
  2551. id_data[5] != 0x00) {
  2552. /* Calc pagesize */
  2553. mtd->writesize = 2048 << (extid & 0x03);
  2554. extid >>= 2;
  2555. /* Calc oobsize */
  2556. switch (extid & 0x03) {
  2557. case 1:
  2558. mtd->oobsize = 128;
  2559. break;
  2560. case 2:
  2561. mtd->oobsize = 218;
  2562. break;
  2563. case 3:
  2564. mtd->oobsize = 400;
  2565. break;
  2566. default:
  2567. mtd->oobsize = 436;
  2568. break;
  2569. }
  2570. extid >>= 2;
  2571. /* Calc blocksize */
  2572. mtd->erasesize = (128 * 1024) <<
  2573. (((extid >> 1) & 0x04) | (extid & 0x03));
  2574. busw = 0;
  2575. } else {
  2576. /* Calc pagesize */
  2577. mtd->writesize = 1024 << (extid & 0x03);
  2578. extid >>= 2;
  2579. /* Calc oobsize */
  2580. mtd->oobsize = (8 << (extid & 0x01)) *
  2581. (mtd->writesize >> 9);
  2582. extid >>= 2;
  2583. /* Calc blocksize. Blocksize is multiples of 64KiB */
  2584. mtd->erasesize = (64 * 1024) << (extid & 0x03);
  2585. extid >>= 2;
  2586. /* Get buswidth information */
  2587. busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0;
  2588. }
  2589. } else {
  2590. /*
  2591. * Old devices have chip data hardcoded in the device id table.
  2592. */
  2593. mtd->erasesize = type->erasesize;
  2594. mtd->writesize = type->pagesize;
  2595. mtd->oobsize = mtd->writesize / 32;
  2596. busw = type->options & NAND_BUSWIDTH_16;
  2597. /*
  2598. * Check for Spansion/AMD ID + repeating 5th, 6th byte since
  2599. * some Spansion chips have erasesize that conflicts with size
  2600. * listed in nand_ids table.
  2601. * Data sheet (5 byte ID): Spansion S30ML-P ORNAND (p.39)
  2602. */
  2603. if (*maf_id == NAND_MFR_AMD && id_data[4] != 0x00 &&
  2604. id_data[5] == 0x00 && id_data[6] == 0x00 &&
  2605. id_data[7] == 0x00 && mtd->writesize == 512) {
  2606. mtd->erasesize = 128 * 1024;
  2607. mtd->erasesize <<= ((id_data[3] & 0x03) << 1);
  2608. }
  2609. }
  2610. /* Get chip options, preserve non chip based options */
  2611. chip->options &= ~NAND_CHIPOPTIONS_MSK;
  2612. chip->options |= type->options & NAND_CHIPOPTIONS_MSK;
  2613. /*
  2614. * Check if chip is not a Samsung device. Do not clear the
  2615. * options for chips which do not have an extended id.
  2616. */
  2617. if (*maf_id != NAND_MFR_SAMSUNG && !type->pagesize)
  2618. chip->options &= ~NAND_SAMSUNG_LP_OPTIONS;
  2619. ident_done:
  2620. /*
  2621. * Set chip as a default. Board drivers can override it, if necessary.
  2622. */
  2623. chip->options |= NAND_NO_AUTOINCR;
  2624. /* Try to identify manufacturer */
  2625. for (maf_idx = 0; nand_manuf_ids[maf_idx].id != 0x0; maf_idx++) {
  2626. if (nand_manuf_ids[maf_idx].id == *maf_id)
  2627. break;
  2628. }
  2629. /*
  2630. * Check, if buswidth is correct. Hardware drivers should set
  2631. * chip correct!
  2632. */
  2633. if (busw != (chip->options & NAND_BUSWIDTH_16)) {
  2634. printk(KERN_INFO "NAND device: Manufacturer ID:"
  2635. " 0x%02x, Chip ID: 0x%02x (%s %s)\n", *maf_id,
  2636. *dev_id, nand_manuf_ids[maf_idx].name, mtd->name);
  2637. printk(KERN_WARNING "NAND bus width %d instead %d bit\n",
  2638. (chip->options & NAND_BUSWIDTH_16) ? 16 : 8,
  2639. busw ? 16 : 8);
  2640. return ERR_PTR(-EINVAL);
  2641. }
  2642. /* Calculate the address shift from the page size */
  2643. chip->page_shift = ffs(mtd->writesize) - 1;
  2644. /* Convert chipsize to number of pages per chip -1 */
  2645. chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
  2646. chip->bbt_erase_shift = chip->phys_erase_shift =
  2647. ffs(mtd->erasesize) - 1;
  2648. if (chip->chipsize & 0xffffffff)
  2649. chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
  2650. else {
  2651. chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32));
  2652. chip->chip_shift += 32 - 1;
  2653. }
  2654. chip->badblockbits = 8;
  2655. /* Set the bad block position */
  2656. if (mtd->writesize > 512 || (busw & NAND_BUSWIDTH_16))
  2657. chip->badblockpos = NAND_LARGE_BADBLOCK_POS;
  2658. else
  2659. chip->badblockpos = NAND_SMALL_BADBLOCK_POS;
  2660. /*
  2661. * Bad block marker is stored in the last page of each block
  2662. * on Samsung and Hynix MLC devices; stored in first two pages
  2663. * of each block on Micron devices with 2KiB pages and on
  2664. * SLC Samsung, Hynix, Toshiba and AMD/Spansion. All others scan
  2665. * only the first page.
  2666. */
  2667. if ((chip->cellinfo & NAND_CI_CELLTYPE_MSK) &&
  2668. (*maf_id == NAND_MFR_SAMSUNG ||
  2669. *maf_id == NAND_MFR_HYNIX))
  2670. chip->bbt_options |= NAND_BBT_SCANLASTPAGE;
  2671. else if ((!(chip->cellinfo & NAND_CI_CELLTYPE_MSK) &&
  2672. (*maf_id == NAND_MFR_SAMSUNG ||
  2673. *maf_id == NAND_MFR_HYNIX ||
  2674. *maf_id == NAND_MFR_TOSHIBA ||
  2675. *maf_id == NAND_MFR_AMD)) ||
  2676. (mtd->writesize == 2048 &&
  2677. *maf_id == NAND_MFR_MICRON))
  2678. chip->bbt_options |= NAND_BBT_SCAN2NDPAGE;
  2679. /* Check for AND chips with 4 page planes */
  2680. if (chip->options & NAND_4PAGE_ARRAY)
  2681. chip->erase_cmd = multi_erase_cmd;
  2682. else
  2683. chip->erase_cmd = single_erase_cmd;
  2684. /* Do not replace user supplied command function! */
  2685. if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
  2686. chip->cmdfunc = nand_command_lp;
  2687. printk(KERN_INFO "NAND device: Manufacturer ID:"
  2688. " 0x%02x, Chip ID: 0x%02x (%s %s)\n", *maf_id, *dev_id,
  2689. nand_manuf_ids[maf_idx].name,
  2690. chip->onfi_version ? chip->onfi_params.model : type->name);
  2691. return type;
  2692. }
  2693. /**
  2694. * nand_scan_ident - [NAND Interface] Scan for the NAND device
  2695. * @mtd: MTD device structure
  2696. * @maxchips: number of chips to scan for
  2697. * @table: alternative NAND ID table
  2698. *
  2699. * This is the first phase of the normal nand_scan() function. It reads the
  2700. * flash ID and sets up MTD fields accordingly.
  2701. *
  2702. * The mtd->owner field must be set to the module of the caller.
  2703. */
  2704. int nand_scan_ident(struct mtd_info *mtd, int maxchips,
  2705. struct nand_flash_dev *table)
  2706. {
  2707. int i, busw, nand_maf_id, nand_dev_id;
  2708. struct nand_chip *chip = mtd->priv;
  2709. struct nand_flash_dev *type;
  2710. /* Get buswidth to select the correct functions */
  2711. busw = chip->options & NAND_BUSWIDTH_16;
  2712. /* Set the default functions */
  2713. nand_set_defaults(chip, busw);
  2714. /* Read the flash type */
  2715. type = nand_get_flash_type(mtd, chip, busw,
  2716. &nand_maf_id, &nand_dev_id, table);
  2717. if (IS_ERR(type)) {
  2718. if (!(chip->options & NAND_SCAN_SILENT_NODEV))
  2719. printk(KERN_WARNING "No NAND device found.\n");
  2720. chip->select_chip(mtd, -1);
  2721. return PTR_ERR(type);
  2722. }
  2723. /* Check for a chip array */
  2724. for (i = 1; i < maxchips; i++) {
  2725. chip->select_chip(mtd, i);
  2726. /* See comment in nand_get_flash_type for reset */
  2727. chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
  2728. /* Send the command for reading device ID */
  2729. chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
  2730. /* Read manufacturer and device IDs */
  2731. if (nand_maf_id != chip->read_byte(mtd) ||
  2732. nand_dev_id != chip->read_byte(mtd))
  2733. break;
  2734. }
  2735. if (i > 1)
  2736. printk(KERN_INFO "%d NAND chips detected\n", i);
  2737. /* Store the number of chips and calc total size for mtd */
  2738. chip->numchips = i;
  2739. mtd->size = i * chip->chipsize;
  2740. return 0;
  2741. }
  2742. EXPORT_SYMBOL(nand_scan_ident);
  2743. /**
  2744. * nand_scan_tail - [NAND Interface] Scan for the NAND device
  2745. * @mtd: MTD device structure
  2746. *
  2747. * This is the second phase of the normal nand_scan() function. It fills out
  2748. * all the uninitialized function pointers with the defaults and scans for a
  2749. * bad block table if appropriate.
  2750. */
  2751. int nand_scan_tail(struct mtd_info *mtd)
  2752. {
  2753. int i;
  2754. struct nand_chip *chip = mtd->priv;
  2755. if (!(chip->options & NAND_OWN_BUFFERS))
  2756. chip->buffers = kmalloc(sizeof(*chip->buffers), GFP_KERNEL);
  2757. if (!chip->buffers)
  2758. return -ENOMEM;
  2759. /* Set the internal oob buffer location, just after the page data */
  2760. chip->oob_poi = chip->buffers->databuf + mtd->writesize;
  2761. /*
  2762. * If no default placement scheme is given, select an appropriate one.
  2763. */
  2764. if (!chip->ecc.layout && (chip->ecc.mode != NAND_ECC_SOFT_BCH)) {
  2765. switch (mtd->oobsize) {
  2766. case 8:
  2767. chip->ecc.layout = &nand_oob_8;
  2768. break;
  2769. case 16:
  2770. chip->ecc.layout = &nand_oob_16;
  2771. break;
  2772. case 64:
  2773. chip->ecc.layout = &nand_oob_64;
  2774. break;
  2775. case 128:
  2776. chip->ecc.layout = &nand_oob_128;
  2777. break;
  2778. default:
  2779. printk(KERN_WARNING "No oob scheme defined for "
  2780. "oobsize %d\n", mtd->oobsize);
  2781. BUG();
  2782. }
  2783. }
  2784. if (!chip->write_page)
  2785. chip->write_page = nand_write_page;
  2786. /*
  2787. * Check ECC mode, default to software if 3byte/512byte hardware ECC is
  2788. * selected and we have 256 byte pagesize fallback to software ECC
  2789. */
  2790. switch (chip->ecc.mode) {
  2791. case NAND_ECC_HW_OOB_FIRST:
  2792. /* Similar to NAND_ECC_HW, but a separate read_page handle */
  2793. if (!chip->ecc.calculate || !chip->ecc.correct ||
  2794. !chip->ecc.hwctl) {
  2795. printk(KERN_WARNING "No ECC functions supplied; "
  2796. "Hardware ECC not possible\n");
  2797. BUG();
  2798. }
  2799. if (!chip->ecc.read_page)
  2800. chip->ecc.read_page = nand_read_page_hwecc_oob_first;
  2801. case NAND_ECC_HW:
  2802. /* Use standard hwecc read page function? */
  2803. if (!chip->ecc.read_page)
  2804. chip->ecc.read_page = nand_read_page_hwecc;
  2805. if (!chip->ecc.write_page)
  2806. chip->ecc.write_page = nand_write_page_hwecc;
  2807. if (!chip->ecc.read_page_raw)
  2808. chip->ecc.read_page_raw = nand_read_page_raw;
  2809. if (!chip->ecc.write_page_raw)
  2810. chip->ecc.write_page_raw = nand_write_page_raw;
  2811. if (!chip->ecc.read_oob)
  2812. chip->ecc.read_oob = nand_read_oob_std;
  2813. if (!chip->ecc.write_oob)
  2814. chip->ecc.write_oob = nand_write_oob_std;
  2815. case NAND_ECC_HW_SYNDROME:
  2816. if ((!chip->ecc.calculate || !chip->ecc.correct ||
  2817. !chip->ecc.hwctl) &&
  2818. (!chip->ecc.read_page ||
  2819. chip->ecc.read_page == nand_read_page_hwecc ||
  2820. !chip->ecc.write_page ||
  2821. chip->ecc.write_page == nand_write_page_hwecc)) {
  2822. printk(KERN_WARNING "No ECC functions supplied; "
  2823. "Hardware ECC not possible\n");
  2824. BUG();
  2825. }
  2826. /* Use standard syndrome read/write page function? */
  2827. if (!chip->ecc.read_page)
  2828. chip->ecc.read_page = nand_read_page_syndrome;
  2829. if (!chip->ecc.write_page)
  2830. chip->ecc.write_page = nand_write_page_syndrome;
  2831. if (!chip->ecc.read_page_raw)
  2832. chip->ecc.read_page_raw = nand_read_page_raw_syndrome;
  2833. if (!chip->ecc.write_page_raw)
  2834. chip->ecc.write_page_raw = nand_write_page_raw_syndrome;
  2835. if (!chip->ecc.read_oob)
  2836. chip->ecc.read_oob = nand_read_oob_syndrome;
  2837. if (!chip->ecc.write_oob)
  2838. chip->ecc.write_oob = nand_write_oob_syndrome;
  2839. if (mtd->writesize >= chip->ecc.size)
  2840. break;
  2841. printk(KERN_WARNING "%d byte HW ECC not possible on "
  2842. "%d byte page size, fallback to SW ECC\n",
  2843. chip->ecc.size, mtd->writesize);
  2844. chip->ecc.mode = NAND_ECC_SOFT;
  2845. case NAND_ECC_SOFT:
  2846. chip->ecc.calculate = nand_calculate_ecc;
  2847. chip->ecc.correct = nand_correct_data;
  2848. chip->ecc.read_page = nand_read_page_swecc;
  2849. chip->ecc.read_subpage = nand_read_subpage;
  2850. chip->ecc.write_page = nand_write_page_swecc;
  2851. chip->ecc.read_page_raw = nand_read_page_raw;
  2852. chip->ecc.write_page_raw = nand_write_page_raw;
  2853. chip->ecc.read_oob = nand_read_oob_std;
  2854. chip->ecc.write_oob = nand_write_oob_std;
  2855. if (!chip->ecc.size)
  2856. chip->ecc.size = 256;
  2857. chip->ecc.bytes = 3;
  2858. break;
  2859. case NAND_ECC_SOFT_BCH:
  2860. if (!mtd_nand_has_bch()) {
  2861. printk(KERN_WARNING "CONFIG_MTD_ECC_BCH not enabled\n");
  2862. BUG();
  2863. }
  2864. chip->ecc.calculate = nand_bch_calculate_ecc;
  2865. chip->ecc.correct = nand_bch_correct_data;
  2866. chip->ecc.read_page = nand_read_page_swecc;
  2867. chip->ecc.read_subpage = nand_read_subpage;
  2868. chip->ecc.write_page = nand_write_page_swecc;
  2869. chip->ecc.read_page_raw = nand_read_page_raw;
  2870. chip->ecc.write_page_raw = nand_write_page_raw;
  2871. chip->ecc.read_oob = nand_read_oob_std;
  2872. chip->ecc.write_oob = nand_write_oob_std;
  2873. /*
  2874. * Board driver should supply ecc.size and ecc.bytes values to
  2875. * select how many bits are correctable; see nand_bch_init()
  2876. * for details. Otherwise, default to 4 bits for large page
  2877. * devices.
  2878. */
  2879. if (!chip->ecc.size && (mtd->oobsize >= 64)) {
  2880. chip->ecc.size = 512;
  2881. chip->ecc.bytes = 7;
  2882. }
  2883. chip->ecc.priv = nand_bch_init(mtd,
  2884. chip->ecc.size,
  2885. chip->ecc.bytes,
  2886. &chip->ecc.layout);
  2887. if (!chip->ecc.priv) {
  2888. printk(KERN_WARNING "BCH ECC initialization failed!\n");
  2889. BUG();
  2890. }
  2891. break;
  2892. case NAND_ECC_NONE:
  2893. printk(KERN_WARNING "NAND_ECC_NONE selected by board driver. "
  2894. "This is not recommended !!\n");
  2895. chip->ecc.read_page = nand_read_page_raw;
  2896. chip->ecc.write_page = nand_write_page_raw;
  2897. chip->ecc.read_oob = nand_read_oob_std;
  2898. chip->ecc.read_page_raw = nand_read_page_raw;
  2899. chip->ecc.write_page_raw = nand_write_page_raw;
  2900. chip->ecc.write_oob = nand_write_oob_std;
  2901. chip->ecc.size = mtd->writesize;
  2902. chip->ecc.bytes = 0;
  2903. break;
  2904. default:
  2905. printk(KERN_WARNING "Invalid NAND_ECC_MODE %d\n",
  2906. chip->ecc.mode);
  2907. BUG();
  2908. }
  2909. /*
  2910. * The number of bytes available for a client to place data into
  2911. * the out of band area.
  2912. */
  2913. chip->ecc.layout->oobavail = 0;
  2914. for (i = 0; chip->ecc.layout->oobfree[i].length
  2915. && i < ARRAY_SIZE(chip->ecc.layout->oobfree); i++)
  2916. chip->ecc.layout->oobavail +=
  2917. chip->ecc.layout->oobfree[i].length;
  2918. mtd->oobavail = chip->ecc.layout->oobavail;
  2919. /*
  2920. * Set the number of read / write steps for one page depending on ECC
  2921. * mode.
  2922. */
  2923. chip->ecc.steps = mtd->writesize / chip->ecc.size;
  2924. if (chip->ecc.steps * chip->ecc.size != mtd->writesize) {
  2925. printk(KERN_WARNING "Invalid ecc parameters\n");
  2926. BUG();
  2927. }
  2928. chip->ecc.total = chip->ecc.steps * chip->ecc.bytes;
  2929. /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */
  2930. if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
  2931. !(chip->cellinfo & NAND_CI_CELLTYPE_MSK)) {
  2932. switch (chip->ecc.steps) {
  2933. case 2:
  2934. mtd->subpage_sft = 1;
  2935. break;
  2936. case 4:
  2937. case 8:
  2938. case 16:
  2939. mtd->subpage_sft = 2;
  2940. break;
  2941. }
  2942. }
  2943. chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
  2944. /* Initialize state */
  2945. chip->state = FL_READY;
  2946. /* De-select the device */
  2947. chip->select_chip(mtd, -1);
  2948. /* Invalidate the pagebuffer reference */
  2949. chip->pagebuf = -1;
  2950. /* Fill in remaining MTD driver data */
  2951. mtd->type = MTD_NANDFLASH;
  2952. mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM :
  2953. MTD_CAP_NANDFLASH;
  2954. mtd->erase = nand_erase;
  2955. mtd->point = NULL;
  2956. mtd->unpoint = NULL;
  2957. mtd->read = nand_read;
  2958. mtd->write = nand_write;
  2959. mtd->panic_write = panic_nand_write;
  2960. mtd->read_oob = nand_read_oob;
  2961. mtd->write_oob = nand_write_oob;
  2962. mtd->sync = nand_sync;
  2963. mtd->lock = NULL;
  2964. mtd->unlock = NULL;
  2965. mtd->suspend = nand_suspend;
  2966. mtd->resume = nand_resume;
  2967. mtd->block_isbad = nand_block_isbad;
  2968. mtd->block_markbad = nand_block_markbad;
  2969. mtd->writebufsize = mtd->writesize;
  2970. /* propagate ecc.layout to mtd_info */
  2971. mtd->ecclayout = chip->ecc.layout;
  2972. /* Check, if we should skip the bad block table scan */
  2973. if (chip->options & NAND_SKIP_BBTSCAN)
  2974. return 0;
  2975. /* Build bad block table */
  2976. return chip->scan_bbt(mtd);
  2977. }
  2978. EXPORT_SYMBOL(nand_scan_tail);
  2979. /*
  2980. * is_module_text_address() isn't exported, and it's mostly a pointless
  2981. * test if this is a module _anyway_ -- they'd have to try _really_ hard
  2982. * to call us from in-kernel code if the core NAND support is modular.
  2983. */
  2984. #ifdef MODULE
  2985. #define caller_is_module() (1)
  2986. #else
  2987. #define caller_is_module() \
  2988. is_module_text_address((unsigned long)__builtin_return_address(0))
  2989. #endif
  2990. /**
  2991. * nand_scan - [NAND Interface] Scan for the NAND device
  2992. * @mtd: MTD device structure
  2993. * @maxchips: number of chips to scan for
  2994. *
  2995. * This fills out all the uninitialized function pointers with the defaults.
  2996. * The flash ID is read and the mtd/chip structures are filled with the
  2997. * appropriate values. The mtd->owner field must be set to the module of the
  2998. * caller.
  2999. */
  3000. int nand_scan(struct mtd_info *mtd, int maxchips)
  3001. {
  3002. int ret;
  3003. /* Many callers got this wrong, so check for it for a while... */
  3004. if (!mtd->owner && caller_is_module()) {
  3005. printk(KERN_CRIT "%s called with NULL mtd->owner!\n",
  3006. __func__);
  3007. BUG();
  3008. }
  3009. ret = nand_scan_ident(mtd, maxchips, NULL);
  3010. if (!ret)
  3011. ret = nand_scan_tail(mtd);
  3012. return ret;
  3013. }
  3014. EXPORT_SYMBOL(nand_scan);
  3015. /**
  3016. * nand_release - [NAND Interface] Free resources held by the NAND device
  3017. * @mtd: MTD device structure
  3018. */
  3019. void nand_release(struct mtd_info *mtd)
  3020. {
  3021. struct nand_chip *chip = mtd->priv;
  3022. if (chip->ecc.mode == NAND_ECC_SOFT_BCH)
  3023. nand_bch_free((struct nand_bch_control *)chip->ecc.priv);
  3024. mtd_device_unregister(mtd);
  3025. /* Free bad block table memory */
  3026. kfree(chip->bbt);
  3027. if (!(chip->options & NAND_OWN_BUFFERS))
  3028. kfree(chip->buffers);
  3029. /* Free bad block descriptor memory */
  3030. if (chip->badblock_pattern && chip->badblock_pattern->options
  3031. & NAND_BBT_DYNAMICSTRUCT)
  3032. kfree(chip->badblock_pattern);
  3033. }
  3034. EXPORT_SYMBOL_GPL(nand_release);
  3035. static int __init nand_base_init(void)
  3036. {
  3037. led_trigger_register_simple("nand-disk", &nand_led_trigger);
  3038. return 0;
  3039. }
  3040. static void __exit nand_base_exit(void)
  3041. {
  3042. led_trigger_unregister_simple(nand_led_trigger);
  3043. }
  3044. module_init(nand_base_init);
  3045. module_exit(nand_base_exit);
  3046. MODULE_LICENSE("GPL");
  3047. MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>");
  3048. MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>");
  3049. MODULE_DESCRIPTION("Generic NAND flash driver code");