nand_base.c 91 KB

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