nand_base.c 87 KB

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