nand_base.c 87 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378
  1. /*
  2. * drivers/mtd/nand.c
  3. *
  4. * Overview:
  5. * This is the generic MTD driver for NAND flash devices. It should be
  6. * capable of working with almost all NAND chips currently available.
  7. * Basic support for AG-AND chips is provided.
  8. *
  9. * Additional technical information is available on
  10. * http://www.linux-mtd.infradead.org/doc/nand.html
  11. *
  12. * Copyright (C) 2000 Steven J. Hill (sjhill@realitydiluted.com)
  13. * 2002-2006 Thomas Gleixner (tglx@linutronix.de)
  14. *
  15. * Credits:
  16. * David Woodhouse for adding multichip support
  17. *
  18. * Aleph One Ltd. and Toby Churchill Ltd. for supporting the
  19. * rework for 2K page size chips
  20. *
  21. * TODO:
  22. * Enable cached programming for 2k page size chips
  23. * Check, if mtd->ecctype should be set to MTD_ECC_HW
  24. * if we have HW ecc support.
  25. * The AG-AND chips have nice features for speed improvement,
  26. * which are not supported yet. Read / program 4 pages in one go.
  27. * BBT table is not serialized, has to be fixed
  28. *
  29. * This program is free software; you can redistribute it and/or modify
  30. * it under the terms of the GNU General Public License version 2 as
  31. * published by the Free Software Foundation.
  32. *
  33. */
  34. #include <linux/module.h>
  35. #include <linux/delay.h>
  36. #include <linux/errno.h>
  37. #include <linux/err.h>
  38. #include <linux/sched.h>
  39. #include <linux/slab.h>
  40. #include <linux/types.h>
  41. #include <linux/mtd/mtd.h>
  42. #include <linux/mtd/nand.h>
  43. #include <linux/mtd/nand_ecc.h>
  44. #include <linux/mtd/compatmac.h>
  45. #include <linux/interrupt.h>
  46. #include <linux/bitops.h>
  47. #include <linux/leds.h>
  48. #include <asm/io.h>
  49. #ifdef CONFIG_MTD_PARTITIONS
  50. #include <linux/mtd/partitions.h>
  51. #endif
  52. /* Define default oob placement schemes for large and small page devices */
  53. static struct nand_ecclayout nand_oob_8 = {
  54. .eccbytes = 3,
  55. .eccpos = {0, 1, 2},
  56. .oobfree = {
  57. {.offset = 3,
  58. .length = 2},
  59. {.offset = 6,
  60. .length = 2}}
  61. };
  62. static struct nand_ecclayout nand_oob_16 = {
  63. .eccbytes = 6,
  64. .eccpos = {0, 1, 2, 3, 6, 7},
  65. .oobfree = {
  66. {.offset = 8,
  67. . length = 8}}
  68. };
  69. static struct nand_ecclayout nand_oob_64 = {
  70. .eccbytes = 24,
  71. .eccpos = {
  72. 40, 41, 42, 43, 44, 45, 46, 47,
  73. 48, 49, 50, 51, 52, 53, 54, 55,
  74. 56, 57, 58, 59, 60, 61, 62, 63},
  75. .oobfree = {
  76. {.offset = 2,
  77. .length = 38}}
  78. };
  79. static struct nand_ecclayout nand_oob_128 = {
  80. .eccbytes = 48,
  81. .eccpos = {
  82. 80, 81, 82, 83, 84, 85, 86, 87,
  83. 88, 89, 90, 91, 92, 93, 94, 95,
  84. 96, 97, 98, 99, 100, 101, 102, 103,
  85. 104, 105, 106, 107, 108, 109, 110, 111,
  86. 112, 113, 114, 115, 116, 117, 118, 119,
  87. 120, 121, 122, 123, 124, 125, 126, 127},
  88. .oobfree = {
  89. {.offset = 2,
  90. .length = 78}}
  91. };
  92. static int nand_get_device(struct nand_chip *chip, struct mtd_info *mtd,
  93. int new_state);
  94. static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
  95. struct mtd_oob_ops *ops);
  96. /*
  97. * For devices which display every fart in the system on a separate LED. Is
  98. * compiled away when LED support is disabled.
  99. */
  100. DEFINE_LED_TRIGGER(nand_led_trigger);
  101. static int check_offs_len(struct mtd_info *mtd,
  102. loff_t ofs, uint64_t len)
  103. {
  104. struct nand_chip *chip = mtd->priv;
  105. int ret = 0;
  106. /* Start address must align on block boundary */
  107. if (ofs & ((1 << chip->phys_erase_shift) - 1)) {
  108. DEBUG(MTD_DEBUG_LEVEL0, "%s: Unaligned address\n", __func__);
  109. ret = -EINVAL;
  110. }
  111. /* Length must align on block boundary */
  112. if (len & ((1 << chip->phys_erase_shift) - 1)) {
  113. DEBUG(MTD_DEBUG_LEVEL0, "%s: Length not block aligned\n",
  114. __func__);
  115. ret = -EINVAL;
  116. }
  117. /* Do not allow past end of device */
  118. if (ofs + len > mtd->size) {
  119. DEBUG(MTD_DEBUG_LEVEL0, "%s: Past end of device\n",
  120. __func__);
  121. ret = -EINVAL;
  122. }
  123. return ret;
  124. }
  125. /**
  126. * nand_release_device - [GENERIC] release chip
  127. * @mtd: MTD device structure
  128. *
  129. * Deselect, release chip lock and wake up anyone waiting on the device
  130. */
  131. static void nand_release_device(struct mtd_info *mtd)
  132. {
  133. struct nand_chip *chip = mtd->priv;
  134. /* De-select the NAND device */
  135. chip->select_chip(mtd, -1);
  136. /* Release the controller and the chip */
  137. spin_lock(&chip->controller->lock);
  138. chip->controller->active = NULL;
  139. chip->state = FL_READY;
  140. wake_up(&chip->controller->wq);
  141. spin_unlock(&chip->controller->lock);
  142. }
  143. /**
  144. * nand_read_byte - [DEFAULT] read one byte from the chip
  145. * @mtd: MTD device structure
  146. *
  147. * Default read function for 8bit buswith
  148. */
  149. static uint8_t nand_read_byte(struct mtd_info *mtd)
  150. {
  151. struct nand_chip *chip = mtd->priv;
  152. return readb(chip->IO_ADDR_R);
  153. }
  154. /**
  155. * nand_read_byte16 - [DEFAULT] read one byte endianess aware from the chip
  156. * @mtd: MTD device structure
  157. *
  158. * Default read function for 16bit buswith with
  159. * endianess conversion
  160. */
  161. static uint8_t nand_read_byte16(struct mtd_info *mtd)
  162. {
  163. struct nand_chip *chip = mtd->priv;
  164. return (uint8_t) cpu_to_le16(readw(chip->IO_ADDR_R));
  165. }
  166. /**
  167. * nand_read_word - [DEFAULT] read one word from the chip
  168. * @mtd: MTD device structure
  169. *
  170. * Default read function for 16bit buswith without
  171. * endianess conversion
  172. */
  173. static u16 nand_read_word(struct mtd_info *mtd)
  174. {
  175. struct nand_chip *chip = mtd->priv;
  176. return readw(chip->IO_ADDR_R);
  177. }
  178. /**
  179. * nand_select_chip - [DEFAULT] control CE line
  180. * @mtd: MTD device structure
  181. * @chipnr: chipnumber to select, -1 for deselect
  182. *
  183. * Default select function for 1 chip devices.
  184. */
  185. static void nand_select_chip(struct mtd_info *mtd, int chipnr)
  186. {
  187. struct nand_chip *chip = mtd->priv;
  188. switch (chipnr) {
  189. case -1:
  190. chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
  191. break;
  192. case 0:
  193. break;
  194. default:
  195. BUG();
  196. }
  197. }
  198. /**
  199. * nand_write_buf - [DEFAULT] write buffer to chip
  200. * @mtd: MTD device structure
  201. * @buf: data buffer
  202. * @len: number of bytes to write
  203. *
  204. * Default write function for 8bit buswith
  205. */
  206. static void nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
  207. {
  208. int i;
  209. struct nand_chip *chip = mtd->priv;
  210. for (i = 0; i < len; i++)
  211. writeb(buf[i], chip->IO_ADDR_W);
  212. }
  213. /**
  214. * nand_read_buf - [DEFAULT] read chip data into buffer
  215. * @mtd: MTD device structure
  216. * @buf: buffer to store date
  217. * @len: number of bytes to read
  218. *
  219. * Default read function for 8bit buswith
  220. */
  221. static void nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
  222. {
  223. int i;
  224. struct nand_chip *chip = mtd->priv;
  225. for (i = 0; i < len; i++)
  226. buf[i] = readb(chip->IO_ADDR_R);
  227. }
  228. /**
  229. * nand_verify_buf - [DEFAULT] Verify chip data against buffer
  230. * @mtd: MTD device structure
  231. * @buf: buffer containing the data to compare
  232. * @len: number of bytes to compare
  233. *
  234. * Default verify function for 8bit buswith
  235. */
  236. static int nand_verify_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
  237. {
  238. int i;
  239. struct nand_chip *chip = mtd->priv;
  240. for (i = 0; i < len; i++)
  241. if (buf[i] != readb(chip->IO_ADDR_R))
  242. return -EFAULT;
  243. return 0;
  244. }
  245. /**
  246. * nand_write_buf16 - [DEFAULT] write buffer to chip
  247. * @mtd: MTD device structure
  248. * @buf: data buffer
  249. * @len: number of bytes to write
  250. *
  251. * Default write function for 16bit buswith
  252. */
  253. static void nand_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
  254. {
  255. int i;
  256. struct nand_chip *chip = mtd->priv;
  257. u16 *p = (u16 *) buf;
  258. len >>= 1;
  259. for (i = 0; i < len; i++)
  260. writew(p[i], chip->IO_ADDR_W);
  261. }
  262. /**
  263. * nand_read_buf16 - [DEFAULT] read chip data into buffer
  264. * @mtd: MTD device structure
  265. * @buf: buffer to store date
  266. * @len: number of bytes to read
  267. *
  268. * Default read function for 16bit buswith
  269. */
  270. static void nand_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len)
  271. {
  272. int i;
  273. struct nand_chip *chip = mtd->priv;
  274. u16 *p = (u16 *) buf;
  275. len >>= 1;
  276. for (i = 0; i < len; i++)
  277. p[i] = readw(chip->IO_ADDR_R);
  278. }
  279. /**
  280. * nand_verify_buf16 - [DEFAULT] Verify chip data against buffer
  281. * @mtd: MTD device structure
  282. * @buf: buffer containing the data to compare
  283. * @len: number of bytes to compare
  284. *
  285. * Default verify function for 16bit buswith
  286. */
  287. static int nand_verify_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
  288. {
  289. int i;
  290. struct nand_chip *chip = mtd->priv;
  291. u16 *p = (u16 *) buf;
  292. len >>= 1;
  293. for (i = 0; i < len; i++)
  294. if (p[i] != readw(chip->IO_ADDR_R))
  295. return -EFAULT;
  296. return 0;
  297. }
  298. /**
  299. * nand_block_bad - [DEFAULT] Read bad block marker from the chip
  300. * @mtd: MTD device structure
  301. * @ofs: offset from device start
  302. * @getchip: 0, if the chip is already selected
  303. *
  304. * Check, if the block is bad.
  305. */
  306. static int nand_block_bad(struct mtd_info *mtd, loff_t ofs, int getchip)
  307. {
  308. int page, chipnr, res = 0;
  309. struct nand_chip *chip = mtd->priv;
  310. u16 bad;
  311. if (chip->options & NAND_BBT_SCANLASTPAGE)
  312. ofs += mtd->erasesize - mtd->writesize;
  313. page = (int)(ofs >> chip->page_shift) & chip->pagemask;
  314. if (getchip) {
  315. chipnr = (int)(ofs >> chip->chip_shift);
  316. nand_get_device(chip, mtd, FL_READING);
  317. /* Select the NAND device */
  318. chip->select_chip(mtd, chipnr);
  319. }
  320. if (chip->options & NAND_BUSWIDTH_16) {
  321. chip->cmdfunc(mtd, NAND_CMD_READOOB, chip->badblockpos & 0xFE,
  322. page);
  323. bad = cpu_to_le16(chip->read_word(mtd));
  324. if (chip->badblockpos & 0x1)
  325. bad >>= 8;
  326. else
  327. bad &= 0xFF;
  328. } else {
  329. chip->cmdfunc(mtd, NAND_CMD_READOOB, chip->badblockpos, page);
  330. bad = chip->read_byte(mtd);
  331. }
  332. if (likely(chip->badblockbits == 8))
  333. res = bad != 0xFF;
  334. else
  335. res = hweight8(bad) < chip->badblockbits;
  336. if (getchip)
  337. nand_release_device(mtd);
  338. return res;
  339. }
  340. /**
  341. * nand_default_block_markbad - [DEFAULT] mark a block bad
  342. * @mtd: MTD device structure
  343. * @ofs: offset from device start
  344. *
  345. * This is the default implementation, which can be overridden by
  346. * a hardware specific driver.
  347. */
  348. static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
  349. {
  350. struct nand_chip *chip = mtd->priv;
  351. uint8_t buf[2] = { 0, 0 };
  352. int block, ret, i = 0;
  353. if (chip->options & NAND_BBT_SCANLASTPAGE)
  354. ofs += mtd->erasesize - mtd->writesize;
  355. /* Get block number */
  356. block = (int)(ofs >> chip->bbt_erase_shift);
  357. if (chip->bbt)
  358. chip->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1);
  359. /* Do we have a flash based bad block table ? */
  360. if (chip->options & NAND_USE_FLASH_BBT)
  361. ret = nand_update_bbt(mtd, ofs);
  362. else {
  363. nand_get_device(chip, mtd, FL_WRITING);
  364. /* Write to first two pages and to byte 1 and 6 if necessary.
  365. * If we write to more than one location, the first error
  366. * encountered quits the procedure. We write two bytes per
  367. * location, so we dont have to mess with 16 bit access.
  368. */
  369. do {
  370. chip->ops.len = chip->ops.ooblen = 2;
  371. chip->ops.datbuf = NULL;
  372. chip->ops.oobbuf = buf;
  373. chip->ops.ooboffs = chip->badblockpos & ~0x01;
  374. ret = nand_do_write_oob(mtd, ofs, &chip->ops);
  375. if (!ret && (chip->options & NAND_BBT_SCANBYTE1AND6)) {
  376. chip->ops.ooboffs = NAND_SMALL_BADBLOCK_POS
  377. & ~0x01;
  378. ret = nand_do_write_oob(mtd, ofs, &chip->ops);
  379. }
  380. i++;
  381. ofs += mtd->writesize;
  382. } while (!ret && (chip->options & NAND_BBT_SCAN2NDPAGE) &&
  383. i < 2);
  384. nand_release_device(mtd);
  385. }
  386. if (!ret)
  387. mtd->ecc_stats.badblocks++;
  388. return ret;
  389. }
  390. /**
  391. * nand_check_wp - [GENERIC] check if the chip is write protected
  392. * @mtd: MTD device structure
  393. * Check, if the device is write protected
  394. *
  395. * The function expects, that the device is already selected
  396. */
  397. static int nand_check_wp(struct mtd_info *mtd)
  398. {
  399. struct nand_chip *chip = mtd->priv;
  400. /* broken xD cards report WP despite being writable */
  401. if (chip->options & NAND_BROKEN_XD)
  402. return 0;
  403. /* Check the WP bit */
  404. chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
  405. return (chip->read_byte(mtd) & NAND_STATUS_WP) ? 0 : 1;
  406. }
  407. /**
  408. * nand_block_checkbad - [GENERIC] Check if a block is marked bad
  409. * @mtd: MTD device structure
  410. * @ofs: offset from device start
  411. * @getchip: 0, if the chip is already selected
  412. * @allowbbt: 1, if its allowed to access the bbt area
  413. *
  414. * Check, if the block is bad. Either by reading the bad block table or
  415. * calling of the scan function.
  416. */
  417. static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int getchip,
  418. int allowbbt)
  419. {
  420. struct nand_chip *chip = mtd->priv;
  421. if (!chip->bbt)
  422. return chip->block_bad(mtd, ofs, getchip);
  423. /* Return info from the table */
  424. return nand_isbad_bbt(mtd, ofs, allowbbt);
  425. }
  426. /**
  427. * panic_nand_wait_ready - [GENERIC] Wait for the ready pin after commands.
  428. * @mtd: MTD device structure
  429. * @timeo: Timeout
  430. *
  431. * Helper function for nand_wait_ready used when needing to wait in interrupt
  432. * context.
  433. */
  434. static void panic_nand_wait_ready(struct mtd_info *mtd, unsigned long timeo)
  435. {
  436. struct nand_chip *chip = mtd->priv;
  437. int i;
  438. /* Wait for the device to get ready */
  439. for (i = 0; i < timeo; i++) {
  440. if (chip->dev_ready(mtd))
  441. break;
  442. touch_softlockup_watchdog();
  443. mdelay(1);
  444. }
  445. }
  446. /*
  447. * Wait for the ready pin, after a command
  448. * The timeout is catched later.
  449. */
  450. void nand_wait_ready(struct mtd_info *mtd)
  451. {
  452. struct nand_chip *chip = mtd->priv;
  453. unsigned long timeo = jiffies + 2;
  454. /* 400ms timeout */
  455. if (in_interrupt() || oops_in_progress)
  456. return panic_nand_wait_ready(mtd, 400);
  457. led_trigger_event(nand_led_trigger, LED_FULL);
  458. /* wait until command is processed or timeout occures */
  459. do {
  460. if (chip->dev_ready(mtd))
  461. break;
  462. touch_softlockup_watchdog();
  463. } while (time_before(jiffies, timeo));
  464. led_trigger_event(nand_led_trigger, LED_OFF);
  465. }
  466. EXPORT_SYMBOL_GPL(nand_wait_ready);
  467. /**
  468. * nand_command - [DEFAULT] Send command to NAND device
  469. * @mtd: MTD device structure
  470. * @command: the command to be sent
  471. * @column: the column address for this command, -1 if none
  472. * @page_addr: the page address for this command, -1 if none
  473. *
  474. * Send command to NAND device. This function is used for small page
  475. * devices (256/512 Bytes per page)
  476. */
  477. static void nand_command(struct mtd_info *mtd, unsigned int command,
  478. int column, int page_addr)
  479. {
  480. register struct nand_chip *chip = mtd->priv;
  481. int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE;
  482. /*
  483. * Write out the command to the device.
  484. */
  485. if (command == NAND_CMD_SEQIN) {
  486. int readcmd;
  487. if (column >= mtd->writesize) {
  488. /* OOB area */
  489. column -= mtd->writesize;
  490. readcmd = NAND_CMD_READOOB;
  491. } else if (column < 256) {
  492. /* First 256 bytes --> READ0 */
  493. readcmd = NAND_CMD_READ0;
  494. } else {
  495. column -= 256;
  496. readcmd = NAND_CMD_READ1;
  497. }
  498. chip->cmd_ctrl(mtd, readcmd, ctrl);
  499. ctrl &= ~NAND_CTRL_CHANGE;
  500. }
  501. chip->cmd_ctrl(mtd, command, ctrl);
  502. /*
  503. * Address cycle, when necessary
  504. */
  505. ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE;
  506. /* Serially input address */
  507. if (column != -1) {
  508. /* Adjust columns for 16 bit buswidth */
  509. if (chip->options & NAND_BUSWIDTH_16)
  510. column >>= 1;
  511. chip->cmd_ctrl(mtd, column, ctrl);
  512. ctrl &= ~NAND_CTRL_CHANGE;
  513. }
  514. if (page_addr != -1) {
  515. chip->cmd_ctrl(mtd, page_addr, ctrl);
  516. ctrl &= ~NAND_CTRL_CHANGE;
  517. chip->cmd_ctrl(mtd, page_addr >> 8, ctrl);
  518. /* One more address cycle for devices > 32MiB */
  519. if (chip->chipsize > (32 << 20))
  520. chip->cmd_ctrl(mtd, page_addr >> 16, ctrl);
  521. }
  522. chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
  523. /*
  524. * program and erase have their own busy handlers
  525. * status and sequential in needs no delay
  526. */
  527. switch (command) {
  528. case NAND_CMD_PAGEPROG:
  529. case NAND_CMD_ERASE1:
  530. case NAND_CMD_ERASE2:
  531. case NAND_CMD_SEQIN:
  532. case NAND_CMD_STATUS:
  533. return;
  534. case NAND_CMD_RESET:
  535. if (chip->dev_ready)
  536. break;
  537. udelay(chip->chip_delay);
  538. chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
  539. NAND_CTRL_CLE | NAND_CTRL_CHANGE);
  540. chip->cmd_ctrl(mtd,
  541. NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
  542. while (!(chip->read_byte(mtd) & NAND_STATUS_READY)) ;
  543. return;
  544. /* This applies to read commands */
  545. default:
  546. /*
  547. * If we don't have access to the busy pin, we apply the given
  548. * command delay
  549. */
  550. if (!chip->dev_ready) {
  551. udelay(chip->chip_delay);
  552. return;
  553. }
  554. }
  555. /* Apply this short delay always to ensure that we do wait tWB in
  556. * any case on any machine. */
  557. ndelay(100);
  558. nand_wait_ready(mtd);
  559. }
  560. /**
  561. * nand_command_lp - [DEFAULT] Send command to NAND large page device
  562. * @mtd: MTD device structure
  563. * @command: the command to be sent
  564. * @column: the column address for this command, -1 if none
  565. * @page_addr: the page address for this command, -1 if none
  566. *
  567. * Send command to NAND device. This is the version for the new large page
  568. * devices We dont have the separate regions as we have in the small page
  569. * devices. We must emulate NAND_CMD_READOOB to keep the code compatible.
  570. */
  571. static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
  572. int column, int page_addr)
  573. {
  574. register struct nand_chip *chip = mtd->priv;
  575. /* Emulate NAND_CMD_READOOB */
  576. if (command == NAND_CMD_READOOB) {
  577. column += mtd->writesize;
  578. command = NAND_CMD_READ0;
  579. }
  580. /* Command latch cycle */
  581. chip->cmd_ctrl(mtd, command & 0xff,
  582. NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
  583. if (column != -1 || page_addr != -1) {
  584. int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE;
  585. /* Serially input address */
  586. if (column != -1) {
  587. /* Adjust columns for 16 bit buswidth */
  588. if (chip->options & NAND_BUSWIDTH_16)
  589. column >>= 1;
  590. chip->cmd_ctrl(mtd, column, ctrl);
  591. ctrl &= ~NAND_CTRL_CHANGE;
  592. chip->cmd_ctrl(mtd, column >> 8, ctrl);
  593. }
  594. if (page_addr != -1) {
  595. chip->cmd_ctrl(mtd, page_addr, ctrl);
  596. chip->cmd_ctrl(mtd, page_addr >> 8,
  597. NAND_NCE | NAND_ALE);
  598. /* One more address cycle for devices > 128MiB */
  599. if (chip->chipsize > (128 << 20))
  600. chip->cmd_ctrl(mtd, page_addr >> 16,
  601. NAND_NCE | NAND_ALE);
  602. }
  603. }
  604. chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
  605. /*
  606. * program and erase have their own busy handlers
  607. * status, sequential in, and deplete1 need no delay
  608. */
  609. switch (command) {
  610. case NAND_CMD_CACHEDPROG:
  611. case NAND_CMD_PAGEPROG:
  612. case NAND_CMD_ERASE1:
  613. case NAND_CMD_ERASE2:
  614. case NAND_CMD_SEQIN:
  615. case NAND_CMD_RNDIN:
  616. case NAND_CMD_STATUS:
  617. case NAND_CMD_DEPLETE1:
  618. return;
  619. /*
  620. * read error status commands require only a short delay
  621. */
  622. case NAND_CMD_STATUS_ERROR:
  623. case NAND_CMD_STATUS_ERROR0:
  624. case NAND_CMD_STATUS_ERROR1:
  625. case NAND_CMD_STATUS_ERROR2:
  626. case NAND_CMD_STATUS_ERROR3:
  627. udelay(chip->chip_delay);
  628. return;
  629. case NAND_CMD_RESET:
  630. if (chip->dev_ready)
  631. break;
  632. udelay(chip->chip_delay);
  633. chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
  634. NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
  635. chip->cmd_ctrl(mtd, NAND_CMD_NONE,
  636. NAND_NCE | NAND_CTRL_CHANGE);
  637. while (!(chip->read_byte(mtd) & NAND_STATUS_READY)) ;
  638. return;
  639. case NAND_CMD_RNDOUT:
  640. /* No ready / busy check necessary */
  641. chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART,
  642. NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
  643. chip->cmd_ctrl(mtd, NAND_CMD_NONE,
  644. NAND_NCE | NAND_CTRL_CHANGE);
  645. return;
  646. case NAND_CMD_READ0:
  647. chip->cmd_ctrl(mtd, NAND_CMD_READSTART,
  648. NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
  649. chip->cmd_ctrl(mtd, NAND_CMD_NONE,
  650. NAND_NCE | NAND_CTRL_CHANGE);
  651. /* This applies to read commands */
  652. default:
  653. /*
  654. * If we don't have access to the busy pin, we apply the given
  655. * command delay
  656. */
  657. if (!chip->dev_ready) {
  658. udelay(chip->chip_delay);
  659. return;
  660. }
  661. }
  662. /* Apply this short delay always to ensure that we do wait tWB in
  663. * any case on any machine. */
  664. ndelay(100);
  665. nand_wait_ready(mtd);
  666. }
  667. /**
  668. * panic_nand_get_device - [GENERIC] Get chip for selected access
  669. * @chip: the nand chip descriptor
  670. * @mtd: MTD device structure
  671. * @new_state: the state which is requested
  672. *
  673. * Used when in panic, no locks are taken.
  674. */
  675. static void panic_nand_get_device(struct nand_chip *chip,
  676. struct mtd_info *mtd, int new_state)
  677. {
  678. /* Hardware controller shared among independend devices */
  679. chip->controller->active = chip;
  680. chip->state = new_state;
  681. }
  682. /**
  683. * nand_get_device - [GENERIC] Get chip for selected access
  684. * @chip: the nand chip descriptor
  685. * @mtd: MTD device structure
  686. * @new_state: the state which is requested
  687. *
  688. * Get the device and lock it for exclusive access
  689. */
  690. static int
  691. nand_get_device(struct nand_chip *chip, struct mtd_info *mtd, int new_state)
  692. {
  693. spinlock_t *lock = &chip->controller->lock;
  694. wait_queue_head_t *wq = &chip->controller->wq;
  695. DECLARE_WAITQUEUE(wait, current);
  696. retry:
  697. spin_lock(lock);
  698. /* Hardware controller shared among independent devices */
  699. if (!chip->controller->active)
  700. chip->controller->active = chip;
  701. if (chip->controller->active == chip && chip->state == FL_READY) {
  702. chip->state = new_state;
  703. spin_unlock(lock);
  704. return 0;
  705. }
  706. if (new_state == FL_PM_SUSPENDED) {
  707. if (chip->controller->active->state == FL_PM_SUSPENDED) {
  708. chip->state = FL_PM_SUSPENDED;
  709. spin_unlock(lock);
  710. return 0;
  711. }
  712. }
  713. set_current_state(TASK_UNINTERRUPTIBLE);
  714. add_wait_queue(wq, &wait);
  715. spin_unlock(lock);
  716. schedule();
  717. remove_wait_queue(wq, &wait);
  718. goto retry;
  719. }
  720. /**
  721. * panic_nand_wait - [GENERIC] wait until the command is done
  722. * @mtd: MTD device structure
  723. * @chip: NAND chip structure
  724. * @timeo: Timeout
  725. *
  726. * Wait for command done. This is a helper function for nand_wait used when
  727. * we are in interrupt context. May happen when in panic and trying to write
  728. * an oops trough mtdoops.
  729. */
  730. static void panic_nand_wait(struct mtd_info *mtd, struct nand_chip *chip,
  731. unsigned long timeo)
  732. {
  733. int i;
  734. for (i = 0; i < timeo; i++) {
  735. if (chip->dev_ready) {
  736. if (chip->dev_ready(mtd))
  737. break;
  738. } else {
  739. if (chip->read_byte(mtd) & NAND_STATUS_READY)
  740. break;
  741. }
  742. mdelay(1);
  743. }
  744. }
  745. /**
  746. * nand_wait - [DEFAULT] wait until the command is done
  747. * @mtd: MTD device structure
  748. * @chip: NAND chip structure
  749. *
  750. * Wait for command done. This applies to erase and program only
  751. * Erase can take up to 400ms and program up to 20ms according to
  752. * general NAND and SmartMedia specs
  753. */
  754. static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip)
  755. {
  756. unsigned long timeo = jiffies;
  757. int status, state = chip->state;
  758. if (state == FL_ERASING)
  759. timeo += (HZ * 400) / 1000;
  760. else
  761. timeo += (HZ * 20) / 1000;
  762. led_trigger_event(nand_led_trigger, LED_FULL);
  763. /* Apply this short delay always to ensure that we do wait tWB in
  764. * any case on any machine. */
  765. ndelay(100);
  766. if ((state == FL_ERASING) && (chip->options & NAND_IS_AND))
  767. chip->cmdfunc(mtd, NAND_CMD_STATUS_MULTI, -1, -1);
  768. else
  769. chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
  770. if (in_interrupt() || oops_in_progress)
  771. panic_nand_wait(mtd, chip, timeo);
  772. else {
  773. while (time_before(jiffies, timeo)) {
  774. if (chip->dev_ready) {
  775. if (chip->dev_ready(mtd))
  776. break;
  777. } else {
  778. if (chip->read_byte(mtd) & NAND_STATUS_READY)
  779. break;
  780. }
  781. cond_resched();
  782. }
  783. }
  784. led_trigger_event(nand_led_trigger, LED_OFF);
  785. status = (int)chip->read_byte(mtd);
  786. return status;
  787. }
  788. /**
  789. * __nand_unlock - [REPLACABLE] unlocks specified locked blockes
  790. *
  791. * @param mtd - mtd info
  792. * @param ofs - offset to start unlock from
  793. * @param len - length to unlock
  794. * @invert - when = 0, unlock the range of blocks within the lower and
  795. * upper boundary address
  796. * whne = 1, unlock the range of blocks outside the boundaries
  797. * of the lower and upper boundary address
  798. *
  799. * @return - unlock status
  800. */
  801. static int __nand_unlock(struct mtd_info *mtd, loff_t ofs,
  802. uint64_t len, int invert)
  803. {
  804. int ret = 0;
  805. int status, page;
  806. struct nand_chip *chip = mtd->priv;
  807. /* Submit address of first page to unlock */
  808. page = ofs >> chip->page_shift;
  809. chip->cmdfunc(mtd, NAND_CMD_UNLOCK1, -1, page & chip->pagemask);
  810. /* Submit address of last page to unlock */
  811. page = (ofs + len) >> chip->page_shift;
  812. chip->cmdfunc(mtd, NAND_CMD_UNLOCK2, -1,
  813. (page | invert) & chip->pagemask);
  814. /* Call wait ready function */
  815. status = chip->waitfunc(mtd, chip);
  816. udelay(1000);
  817. /* See if device thinks it succeeded */
  818. if (status & 0x01) {
  819. DEBUG(MTD_DEBUG_LEVEL0, "%s: Error status = 0x%08x\n",
  820. __func__, status);
  821. ret = -EIO;
  822. }
  823. return ret;
  824. }
  825. /**
  826. * nand_unlock - [REPLACABLE] unlocks specified locked blockes
  827. *
  828. * @param mtd - mtd info
  829. * @param ofs - offset to start unlock from
  830. * @param len - length to unlock
  831. *
  832. * @return - unlock status
  833. */
  834. int nand_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  835. {
  836. int ret = 0;
  837. int chipnr;
  838. struct nand_chip *chip = mtd->priv;
  839. DEBUG(MTD_DEBUG_LEVEL3, "%s: start = 0x%012llx, len = %llu\n",
  840. __func__, (unsigned long long)ofs, len);
  841. if (check_offs_len(mtd, ofs, len))
  842. ret = -EINVAL;
  843. /* Align to last block address if size addresses end of the device */
  844. if (ofs + len == mtd->size)
  845. len -= mtd->erasesize;
  846. nand_get_device(chip, mtd, FL_UNLOCKING);
  847. /* Shift to get chip number */
  848. chipnr = ofs >> chip->chip_shift;
  849. chip->select_chip(mtd, chipnr);
  850. /* Check, if it is write protected */
  851. if (nand_check_wp(mtd)) {
  852. DEBUG(MTD_DEBUG_LEVEL0, "%s: Device is write protected!!!\n",
  853. __func__);
  854. ret = -EIO;
  855. goto out;
  856. }
  857. ret = __nand_unlock(mtd, ofs, len, 0);
  858. out:
  859. /* de-select the NAND device */
  860. chip->select_chip(mtd, -1);
  861. nand_release_device(mtd);
  862. return ret;
  863. }
  864. /**
  865. * nand_lock - [REPLACABLE] locks all blockes present in the device
  866. *
  867. * @param mtd - mtd info
  868. * @param ofs - offset to start unlock from
  869. * @param len - length to unlock
  870. *
  871. * @return - lock status
  872. *
  873. * This feature is not support in many NAND parts. 'Micron' NAND parts
  874. * do have this feature, but it allows only to lock all blocks not for
  875. * specified range for block.
  876. *
  877. * Implementing 'lock' feature by making use of 'unlock', for now.
  878. */
  879. int nand_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  880. {
  881. int ret = 0;
  882. int chipnr, status, page;
  883. struct nand_chip *chip = mtd->priv;
  884. DEBUG(MTD_DEBUG_LEVEL3, "%s: start = 0x%012llx, len = %llu\n",
  885. __func__, (unsigned long long)ofs, len);
  886. if (check_offs_len(mtd, ofs, len))
  887. ret = -EINVAL;
  888. nand_get_device(chip, mtd, FL_LOCKING);
  889. /* Shift to get chip number */
  890. chipnr = ofs >> chip->chip_shift;
  891. chip->select_chip(mtd, chipnr);
  892. /* Check, if it is write protected */
  893. if (nand_check_wp(mtd)) {
  894. DEBUG(MTD_DEBUG_LEVEL0, "%s: Device is write protected!!!\n",
  895. __func__);
  896. status = MTD_ERASE_FAILED;
  897. ret = -EIO;
  898. goto out;
  899. }
  900. /* Submit address of first page to lock */
  901. page = ofs >> chip->page_shift;
  902. chip->cmdfunc(mtd, NAND_CMD_LOCK, -1, page & chip->pagemask);
  903. /* Call wait ready function */
  904. status = chip->waitfunc(mtd, chip);
  905. udelay(1000);
  906. /* See if device thinks it succeeded */
  907. if (status & 0x01) {
  908. DEBUG(MTD_DEBUG_LEVEL0, "%s: Error status = 0x%08x\n",
  909. __func__, status);
  910. ret = -EIO;
  911. goto out;
  912. }
  913. ret = __nand_unlock(mtd, ofs, len, 0x1);
  914. out:
  915. /* de-select the NAND device */
  916. chip->select_chip(mtd, -1);
  917. nand_release_device(mtd);
  918. return ret;
  919. }
  920. /**
  921. * nand_read_page_raw - [Intern] read raw page data without ecc
  922. * @mtd: mtd info structure
  923. * @chip: nand chip info structure
  924. * @buf: buffer to store read data
  925. * @page: page number to read
  926. *
  927. * Not for syndrome calculating ecc controllers, which use a special oob layout
  928. */
  929. static int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
  930. uint8_t *buf, int page)
  931. {
  932. chip->read_buf(mtd, buf, mtd->writesize);
  933. chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
  934. return 0;
  935. }
  936. /**
  937. * nand_read_page_raw_syndrome - [Intern] read raw page data without ecc
  938. * @mtd: mtd info structure
  939. * @chip: nand chip info structure
  940. * @buf: buffer to store read data
  941. * @page: page number to read
  942. *
  943. * We need a special oob layout and handling even when OOB isn't used.
  944. */
  945. static int nand_read_page_raw_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
  946. uint8_t *buf, int page)
  947. {
  948. int eccsize = chip->ecc.size;
  949. int eccbytes = chip->ecc.bytes;
  950. uint8_t *oob = chip->oob_poi;
  951. int steps, size;
  952. for (steps = chip->ecc.steps; steps > 0; steps--) {
  953. chip->read_buf(mtd, buf, eccsize);
  954. buf += eccsize;
  955. if (chip->ecc.prepad) {
  956. chip->read_buf(mtd, oob, chip->ecc.prepad);
  957. oob += chip->ecc.prepad;
  958. }
  959. chip->read_buf(mtd, oob, eccbytes);
  960. oob += eccbytes;
  961. if (chip->ecc.postpad) {
  962. chip->read_buf(mtd, oob, chip->ecc.postpad);
  963. oob += chip->ecc.postpad;
  964. }
  965. }
  966. size = mtd->oobsize - (oob - chip->oob_poi);
  967. if (size)
  968. chip->read_buf(mtd, oob, size);
  969. return 0;
  970. }
  971. /**
  972. * nand_read_page_swecc - [REPLACABLE] software ecc based page read function
  973. * @mtd: mtd info structure
  974. * @chip: nand chip info structure
  975. * @buf: buffer to store read data
  976. * @page: page number to read
  977. */
  978. static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
  979. uint8_t *buf, int page)
  980. {
  981. int i, eccsize = chip->ecc.size;
  982. int eccbytes = chip->ecc.bytes;
  983. int eccsteps = chip->ecc.steps;
  984. uint8_t *p = buf;
  985. uint8_t *ecc_calc = chip->buffers->ecccalc;
  986. uint8_t *ecc_code = chip->buffers->ecccode;
  987. uint32_t *eccpos = chip->ecc.layout->eccpos;
  988. chip->ecc.read_page_raw(mtd, chip, buf, page);
  989. for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
  990. chip->ecc.calculate(mtd, p, &ecc_calc[i]);
  991. for (i = 0; i < chip->ecc.total; i++)
  992. ecc_code[i] = chip->oob_poi[eccpos[i]];
  993. eccsteps = chip->ecc.steps;
  994. p = buf;
  995. for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
  996. int stat;
  997. stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
  998. if (stat < 0)
  999. mtd->ecc_stats.failed++;
  1000. else
  1001. mtd->ecc_stats.corrected += stat;
  1002. }
  1003. return 0;
  1004. }
  1005. /**
  1006. * nand_read_subpage - [REPLACABLE] software ecc based sub-page read function
  1007. * @mtd: mtd info structure
  1008. * @chip: nand chip info structure
  1009. * @data_offs: offset of requested data within the page
  1010. * @readlen: data length
  1011. * @bufpoi: buffer to store read data
  1012. */
  1013. static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip, uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi)
  1014. {
  1015. int start_step, end_step, num_steps;
  1016. uint32_t *eccpos = chip->ecc.layout->eccpos;
  1017. uint8_t *p;
  1018. int data_col_addr, i, gaps = 0;
  1019. int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
  1020. int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
  1021. /* Column address wihin the page aligned to ECC size (256bytes). */
  1022. start_step = data_offs / chip->ecc.size;
  1023. end_step = (data_offs + readlen - 1) / chip->ecc.size;
  1024. num_steps = end_step - start_step + 1;
  1025. /* Data size aligned to ECC ecc.size*/
  1026. datafrag_len = num_steps * chip->ecc.size;
  1027. eccfrag_len = num_steps * chip->ecc.bytes;
  1028. data_col_addr = start_step * chip->ecc.size;
  1029. /* If we read not a page aligned data */
  1030. if (data_col_addr != 0)
  1031. chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_col_addr, -1);
  1032. p = bufpoi + data_col_addr;
  1033. chip->read_buf(mtd, p, datafrag_len);
  1034. /* Calculate ECC */
  1035. for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
  1036. chip->ecc.calculate(mtd, p, &chip->buffers->ecccalc[i]);
  1037. /* The performance is faster if to position offsets
  1038. according to ecc.pos. Let make sure here that
  1039. there are no gaps in ecc positions */
  1040. for (i = 0; i < eccfrag_len - 1; i++) {
  1041. if (eccpos[i + start_step * chip->ecc.bytes] + 1 !=
  1042. eccpos[i + start_step * chip->ecc.bytes + 1]) {
  1043. gaps = 1;
  1044. break;
  1045. }
  1046. }
  1047. if (gaps) {
  1048. chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
  1049. chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
  1050. } else {
  1051. /* send the command to read the particular ecc bytes */
  1052. /* take care about buswidth alignment in read_buf */
  1053. aligned_pos = eccpos[start_step * chip->ecc.bytes] & ~(busw - 1);
  1054. aligned_len = eccfrag_len;
  1055. if (eccpos[start_step * chip->ecc.bytes] & (busw - 1))
  1056. aligned_len++;
  1057. if (eccpos[(start_step + num_steps) * chip->ecc.bytes] & (busw - 1))
  1058. aligned_len++;
  1059. chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize + aligned_pos, -1);
  1060. chip->read_buf(mtd, &chip->oob_poi[aligned_pos], aligned_len);
  1061. }
  1062. for (i = 0; i < eccfrag_len; i++)
  1063. chip->buffers->ecccode[i] = chip->oob_poi[eccpos[i + start_step * chip->ecc.bytes]];
  1064. p = bufpoi + data_col_addr;
  1065. for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
  1066. int stat;
  1067. stat = chip->ecc.correct(mtd, p, &chip->buffers->ecccode[i], &chip->buffers->ecccalc[i]);
  1068. if (stat == -1)
  1069. mtd->ecc_stats.failed++;
  1070. else
  1071. mtd->ecc_stats.corrected += stat;
  1072. }
  1073. return 0;
  1074. }
  1075. /**
  1076. * nand_read_page_hwecc - [REPLACABLE] hardware ecc based page read function
  1077. * @mtd: mtd info structure
  1078. * @chip: nand chip info structure
  1079. * @buf: buffer to store read data
  1080. * @page: page number to read
  1081. *
  1082. * Not for syndrome calculating ecc controllers which need a special oob layout
  1083. */
  1084. static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
  1085. uint8_t *buf, int page)
  1086. {
  1087. int i, eccsize = chip->ecc.size;
  1088. int eccbytes = chip->ecc.bytes;
  1089. int eccsteps = chip->ecc.steps;
  1090. uint8_t *p = buf;
  1091. uint8_t *ecc_calc = chip->buffers->ecccalc;
  1092. uint8_t *ecc_code = chip->buffers->ecccode;
  1093. uint32_t *eccpos = chip->ecc.layout->eccpos;
  1094. for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
  1095. chip->ecc.hwctl(mtd, NAND_ECC_READ);
  1096. chip->read_buf(mtd, p, eccsize);
  1097. chip->ecc.calculate(mtd, p, &ecc_calc[i]);
  1098. }
  1099. chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
  1100. for (i = 0; i < chip->ecc.total; i++)
  1101. ecc_code[i] = chip->oob_poi[eccpos[i]];
  1102. eccsteps = chip->ecc.steps;
  1103. p = buf;
  1104. for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
  1105. int stat;
  1106. stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
  1107. if (stat < 0)
  1108. mtd->ecc_stats.failed++;
  1109. else
  1110. mtd->ecc_stats.corrected += stat;
  1111. }
  1112. return 0;
  1113. }
  1114. /**
  1115. * nand_read_page_hwecc_oob_first - [REPLACABLE] hw ecc, read oob first
  1116. * @mtd: mtd info structure
  1117. * @chip: nand chip info structure
  1118. * @buf: buffer to store read data
  1119. * @page: page number to read
  1120. *
  1121. * Hardware ECC for large page chips, require OOB to be read first.
  1122. * For this ECC mode, the write_page method is re-used from ECC_HW.
  1123. * These methods read/write ECC from the OOB area, unlike the
  1124. * ECC_HW_SYNDROME support with multiple ECC steps, follows the
  1125. * "infix ECC" scheme and reads/writes ECC from the data area, by
  1126. * overwriting the NAND manufacturer bad block markings.
  1127. */
  1128. static int nand_read_page_hwecc_oob_first(struct mtd_info *mtd,
  1129. struct nand_chip *chip, uint8_t *buf, int page)
  1130. {
  1131. int i, eccsize = chip->ecc.size;
  1132. int eccbytes = chip->ecc.bytes;
  1133. int eccsteps = chip->ecc.steps;
  1134. uint8_t *p = buf;
  1135. uint8_t *ecc_code = chip->buffers->ecccode;
  1136. uint32_t *eccpos = chip->ecc.layout->eccpos;
  1137. uint8_t *ecc_calc = chip->buffers->ecccalc;
  1138. /* Read the OOB area first */
  1139. chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
  1140. chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
  1141. chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
  1142. for (i = 0; i < chip->ecc.total; i++)
  1143. ecc_code[i] = chip->oob_poi[eccpos[i]];
  1144. for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
  1145. int stat;
  1146. chip->ecc.hwctl(mtd, NAND_ECC_READ);
  1147. chip->read_buf(mtd, p, eccsize);
  1148. chip->ecc.calculate(mtd, p, &ecc_calc[i]);
  1149. stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL);
  1150. if (stat < 0)
  1151. mtd->ecc_stats.failed++;
  1152. else
  1153. mtd->ecc_stats.corrected += stat;
  1154. }
  1155. return 0;
  1156. }
  1157. /**
  1158. * nand_read_page_syndrome - [REPLACABLE] hardware ecc syndrom based page read
  1159. * @mtd: mtd info structure
  1160. * @chip: nand chip info structure
  1161. * @buf: buffer to store read data
  1162. * @page: page number to read
  1163. *
  1164. * The hw generator calculates the error syndrome automatically. Therefor
  1165. * we need a special oob layout and handling.
  1166. */
  1167. static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
  1168. uint8_t *buf, int page)
  1169. {
  1170. int i, eccsize = chip->ecc.size;
  1171. int eccbytes = chip->ecc.bytes;
  1172. int eccsteps = chip->ecc.steps;
  1173. uint8_t *p = buf;
  1174. uint8_t *oob = chip->oob_poi;
  1175. for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
  1176. int stat;
  1177. chip->ecc.hwctl(mtd, NAND_ECC_READ);
  1178. chip->read_buf(mtd, p, eccsize);
  1179. if (chip->ecc.prepad) {
  1180. chip->read_buf(mtd, oob, chip->ecc.prepad);
  1181. oob += chip->ecc.prepad;
  1182. }
  1183. chip->ecc.hwctl(mtd, NAND_ECC_READSYN);
  1184. chip->read_buf(mtd, oob, eccbytes);
  1185. stat = chip->ecc.correct(mtd, p, oob, NULL);
  1186. if (stat < 0)
  1187. mtd->ecc_stats.failed++;
  1188. else
  1189. mtd->ecc_stats.corrected += stat;
  1190. oob += eccbytes;
  1191. if (chip->ecc.postpad) {
  1192. chip->read_buf(mtd, oob, chip->ecc.postpad);
  1193. oob += chip->ecc.postpad;
  1194. }
  1195. }
  1196. /* Calculate remaining oob bytes */
  1197. i = mtd->oobsize - (oob - chip->oob_poi);
  1198. if (i)
  1199. chip->read_buf(mtd, oob, i);
  1200. return 0;
  1201. }
  1202. /**
  1203. * nand_transfer_oob - [Internal] Transfer oob to client buffer
  1204. * @chip: nand chip structure
  1205. * @oob: oob destination address
  1206. * @ops: oob ops structure
  1207. * @len: size of oob to transfer
  1208. */
  1209. static uint8_t *nand_transfer_oob(struct nand_chip *chip, uint8_t *oob,
  1210. struct mtd_oob_ops *ops, size_t len)
  1211. {
  1212. switch(ops->mode) {
  1213. case MTD_OOB_PLACE:
  1214. case MTD_OOB_RAW:
  1215. memcpy(oob, chip->oob_poi + ops->ooboffs, len);
  1216. return oob + len;
  1217. case MTD_OOB_AUTO: {
  1218. struct nand_oobfree *free = chip->ecc.layout->oobfree;
  1219. uint32_t boffs = 0, roffs = ops->ooboffs;
  1220. size_t bytes = 0;
  1221. for(; free->length && len; free++, len -= bytes) {
  1222. /* Read request not from offset 0 ? */
  1223. if (unlikely(roffs)) {
  1224. if (roffs >= free->length) {
  1225. roffs -= free->length;
  1226. continue;
  1227. }
  1228. boffs = free->offset + roffs;
  1229. bytes = min_t(size_t, len,
  1230. (free->length - roffs));
  1231. roffs = 0;
  1232. } else {
  1233. bytes = min_t(size_t, len, free->length);
  1234. boffs = free->offset;
  1235. }
  1236. memcpy(oob, chip->oob_poi + boffs, bytes);
  1237. oob += bytes;
  1238. }
  1239. return oob;
  1240. }
  1241. default:
  1242. BUG();
  1243. }
  1244. return NULL;
  1245. }
  1246. /**
  1247. * nand_do_read_ops - [Internal] Read data with ECC
  1248. *
  1249. * @mtd: MTD device structure
  1250. * @from: offset to read from
  1251. * @ops: oob ops structure
  1252. *
  1253. * Internal function. Called with chip held.
  1254. */
  1255. static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
  1256. struct mtd_oob_ops *ops)
  1257. {
  1258. int chipnr, page, realpage, col, bytes, aligned;
  1259. struct nand_chip *chip = mtd->priv;
  1260. struct mtd_ecc_stats stats;
  1261. int blkcheck = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
  1262. int sndcmd = 1;
  1263. int ret = 0;
  1264. uint32_t readlen = ops->len;
  1265. uint32_t oobreadlen = ops->ooblen;
  1266. uint32_t max_oobsize = ops->mode == MTD_OOB_AUTO ?
  1267. mtd->oobavail : mtd->oobsize;
  1268. uint8_t *bufpoi, *oob, *buf;
  1269. stats = mtd->ecc_stats;
  1270. chipnr = (int)(from >> chip->chip_shift);
  1271. chip->select_chip(mtd, chipnr);
  1272. realpage = (int)(from >> chip->page_shift);
  1273. page = realpage & chip->pagemask;
  1274. col = (int)(from & (mtd->writesize - 1));
  1275. buf = ops->datbuf;
  1276. oob = ops->oobbuf;
  1277. while(1) {
  1278. bytes = min(mtd->writesize - col, readlen);
  1279. aligned = (bytes == mtd->writesize);
  1280. /* Is the current page in the buffer ? */
  1281. if (realpage != chip->pagebuf || oob) {
  1282. bufpoi = aligned ? buf : chip->buffers->databuf;
  1283. if (likely(sndcmd)) {
  1284. chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
  1285. sndcmd = 0;
  1286. }
  1287. /* Now read the page into the buffer */
  1288. if (unlikely(ops->mode == MTD_OOB_RAW))
  1289. ret = chip->ecc.read_page_raw(mtd, chip,
  1290. bufpoi, page);
  1291. else if (!aligned && NAND_SUBPAGE_READ(chip) && !oob)
  1292. ret = chip->ecc.read_subpage(mtd, chip, col, bytes, bufpoi);
  1293. else
  1294. ret = chip->ecc.read_page(mtd, chip, bufpoi,
  1295. page);
  1296. if (ret < 0)
  1297. break;
  1298. /* Transfer not aligned data */
  1299. if (!aligned) {
  1300. if (!NAND_SUBPAGE_READ(chip) && !oob)
  1301. chip->pagebuf = realpage;
  1302. memcpy(buf, chip->buffers->databuf + col, bytes);
  1303. }
  1304. buf += bytes;
  1305. if (unlikely(oob)) {
  1306. int toread = min(oobreadlen, max_oobsize);
  1307. if (toread) {
  1308. oob = nand_transfer_oob(chip,
  1309. oob, ops, toread);
  1310. oobreadlen -= toread;
  1311. }
  1312. }
  1313. if (!(chip->options & NAND_NO_READRDY)) {
  1314. /*
  1315. * Apply delay or wait for ready/busy pin. Do
  1316. * this before the AUTOINCR check, so no
  1317. * problems arise if a chip which does auto
  1318. * increment is marked as NOAUTOINCR by the
  1319. * board driver.
  1320. */
  1321. if (!chip->dev_ready)
  1322. udelay(chip->chip_delay);
  1323. else
  1324. nand_wait_ready(mtd);
  1325. }
  1326. } else {
  1327. memcpy(buf, chip->buffers->databuf + col, bytes);
  1328. buf += bytes;
  1329. }
  1330. readlen -= bytes;
  1331. if (!readlen)
  1332. break;
  1333. /* For subsequent reads align to page boundary. */
  1334. col = 0;
  1335. /* Increment page address */
  1336. realpage++;
  1337. page = realpage & chip->pagemask;
  1338. /* Check, if we cross a chip boundary */
  1339. if (!page) {
  1340. chipnr++;
  1341. chip->select_chip(mtd, -1);
  1342. chip->select_chip(mtd, chipnr);
  1343. }
  1344. /* Check, if the chip supports auto page increment
  1345. * or if we have hit a block boundary.
  1346. */
  1347. if (!NAND_CANAUTOINCR(chip) || !(page & blkcheck))
  1348. sndcmd = 1;
  1349. }
  1350. ops->retlen = ops->len - (size_t) readlen;
  1351. if (oob)
  1352. ops->oobretlen = ops->ooblen - oobreadlen;
  1353. if (ret)
  1354. return ret;
  1355. if (mtd->ecc_stats.failed - stats.failed)
  1356. return -EBADMSG;
  1357. return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
  1358. }
  1359. /**
  1360. * nand_read - [MTD Interface] MTD compability function for nand_do_read_ecc
  1361. * @mtd: MTD device structure
  1362. * @from: offset to read from
  1363. * @len: number of bytes to read
  1364. * @retlen: pointer to variable to store the number of read bytes
  1365. * @buf: the databuffer to put data
  1366. *
  1367. * Get hold of the chip and call nand_do_read
  1368. */
  1369. static int nand_read(struct mtd_info *mtd, loff_t from, size_t len,
  1370. size_t *retlen, uint8_t *buf)
  1371. {
  1372. struct nand_chip *chip = mtd->priv;
  1373. int ret;
  1374. /* Do not allow reads past end of device */
  1375. if ((from + len) > mtd->size)
  1376. return -EINVAL;
  1377. if (!len)
  1378. return 0;
  1379. nand_get_device(chip, mtd, FL_READING);
  1380. chip->ops.len = len;
  1381. chip->ops.datbuf = buf;
  1382. chip->ops.oobbuf = NULL;
  1383. ret = nand_do_read_ops(mtd, from, &chip->ops);
  1384. *retlen = chip->ops.retlen;
  1385. nand_release_device(mtd);
  1386. return ret;
  1387. }
  1388. /**
  1389. * nand_read_oob_std - [REPLACABLE] the most common OOB data read function
  1390. * @mtd: mtd info structure
  1391. * @chip: nand chip info structure
  1392. * @page: page number to read
  1393. * @sndcmd: flag whether to issue read command or not
  1394. */
  1395. static int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
  1396. int page, int sndcmd)
  1397. {
  1398. if (sndcmd) {
  1399. chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
  1400. sndcmd = 0;
  1401. }
  1402. chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
  1403. return sndcmd;
  1404. }
  1405. /**
  1406. * nand_read_oob_syndrome - [REPLACABLE] OOB data read function for HW ECC
  1407. * with syndromes
  1408. * @mtd: mtd info structure
  1409. * @chip: nand chip info structure
  1410. * @page: page number to read
  1411. * @sndcmd: flag whether to issue read command or not
  1412. */
  1413. static int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
  1414. int page, int sndcmd)
  1415. {
  1416. uint8_t *buf = chip->oob_poi;
  1417. int length = mtd->oobsize;
  1418. int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
  1419. int eccsize = chip->ecc.size;
  1420. uint8_t *bufpoi = buf;
  1421. int i, toread, sndrnd = 0, pos;
  1422. chip->cmdfunc(mtd, NAND_CMD_READ0, chip->ecc.size, page);
  1423. for (i = 0; i < chip->ecc.steps; i++) {
  1424. if (sndrnd) {
  1425. pos = eccsize + i * (eccsize + chunk);
  1426. if (mtd->writesize > 512)
  1427. chip->cmdfunc(mtd, NAND_CMD_RNDOUT, pos, -1);
  1428. else
  1429. chip->cmdfunc(mtd, NAND_CMD_READ0, pos, page);
  1430. } else
  1431. sndrnd = 1;
  1432. toread = min_t(int, length, chunk);
  1433. chip->read_buf(mtd, bufpoi, toread);
  1434. bufpoi += toread;
  1435. length -= toread;
  1436. }
  1437. if (length > 0)
  1438. chip->read_buf(mtd, bufpoi, length);
  1439. return 1;
  1440. }
  1441. /**
  1442. * nand_write_oob_std - [REPLACABLE] the most common OOB data write function
  1443. * @mtd: mtd info structure
  1444. * @chip: nand chip info structure
  1445. * @page: page number to write
  1446. */
  1447. static int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
  1448. int page)
  1449. {
  1450. int status = 0;
  1451. const uint8_t *buf = chip->oob_poi;
  1452. int length = mtd->oobsize;
  1453. chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
  1454. chip->write_buf(mtd, buf, length);
  1455. /* Send command to program the OOB data */
  1456. chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
  1457. status = chip->waitfunc(mtd, chip);
  1458. return status & NAND_STATUS_FAIL ? -EIO : 0;
  1459. }
  1460. /**
  1461. * nand_write_oob_syndrome - [REPLACABLE] OOB data write function for HW ECC
  1462. * with syndrome - only for large page flash !
  1463. * @mtd: mtd info structure
  1464. * @chip: nand chip info structure
  1465. * @page: page number to write
  1466. */
  1467. static int nand_write_oob_syndrome(struct mtd_info *mtd,
  1468. struct nand_chip *chip, int page)
  1469. {
  1470. int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
  1471. int eccsize = chip->ecc.size, length = mtd->oobsize;
  1472. int i, len, pos, status = 0, sndcmd = 0, steps = chip->ecc.steps;
  1473. const uint8_t *bufpoi = chip->oob_poi;
  1474. /*
  1475. * data-ecc-data-ecc ... ecc-oob
  1476. * or
  1477. * data-pad-ecc-pad-data-pad .... ecc-pad-oob
  1478. */
  1479. if (!chip->ecc.prepad && !chip->ecc.postpad) {
  1480. pos = steps * (eccsize + chunk);
  1481. steps = 0;
  1482. } else
  1483. pos = eccsize;
  1484. chip->cmdfunc(mtd, NAND_CMD_SEQIN, pos, page);
  1485. for (i = 0; i < steps; i++) {
  1486. if (sndcmd) {
  1487. if (mtd->writesize <= 512) {
  1488. uint32_t fill = 0xFFFFFFFF;
  1489. len = eccsize;
  1490. while (len > 0) {
  1491. int num = min_t(int, len, 4);
  1492. chip->write_buf(mtd, (uint8_t *)&fill,
  1493. num);
  1494. len -= num;
  1495. }
  1496. } else {
  1497. pos = eccsize + i * (eccsize + chunk);
  1498. chip->cmdfunc(mtd, NAND_CMD_RNDIN, pos, -1);
  1499. }
  1500. } else
  1501. sndcmd = 1;
  1502. len = min_t(int, length, chunk);
  1503. chip->write_buf(mtd, bufpoi, len);
  1504. bufpoi += len;
  1505. length -= len;
  1506. }
  1507. if (length > 0)
  1508. chip->write_buf(mtd, bufpoi, length);
  1509. chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
  1510. status = chip->waitfunc(mtd, chip);
  1511. return status & NAND_STATUS_FAIL ? -EIO : 0;
  1512. }
  1513. /**
  1514. * nand_do_read_oob - [Intern] NAND read out-of-band
  1515. * @mtd: MTD device structure
  1516. * @from: offset to read from
  1517. * @ops: oob operations description structure
  1518. *
  1519. * NAND read out-of-band data from the spare area
  1520. */
  1521. static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
  1522. struct mtd_oob_ops *ops)
  1523. {
  1524. int page, realpage, chipnr, sndcmd = 1;
  1525. struct nand_chip *chip = mtd->priv;
  1526. int blkcheck = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
  1527. int readlen = ops->ooblen;
  1528. int len;
  1529. uint8_t *buf = ops->oobbuf;
  1530. DEBUG(MTD_DEBUG_LEVEL3, "%s: from = 0x%08Lx, len = %i\n",
  1531. __func__, (unsigned long long)from, readlen);
  1532. if (ops->mode == MTD_OOB_AUTO)
  1533. len = chip->ecc.layout->oobavail;
  1534. else
  1535. len = mtd->oobsize;
  1536. if (unlikely(ops->ooboffs >= len)) {
  1537. DEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt to start read "
  1538. "outside oob\n", __func__);
  1539. return -EINVAL;
  1540. }
  1541. /* Do not allow reads past end of device */
  1542. if (unlikely(from >= mtd->size ||
  1543. ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) -
  1544. (from >> chip->page_shift)) * len)) {
  1545. DEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt read beyond end "
  1546. "of device\n", __func__);
  1547. return -EINVAL;
  1548. }
  1549. chipnr = (int)(from >> chip->chip_shift);
  1550. chip->select_chip(mtd, chipnr);
  1551. /* Shift to get page */
  1552. realpage = (int)(from >> chip->page_shift);
  1553. page = realpage & chip->pagemask;
  1554. while(1) {
  1555. sndcmd = chip->ecc.read_oob(mtd, chip, page, sndcmd);
  1556. len = min(len, readlen);
  1557. buf = nand_transfer_oob(chip, buf, ops, len);
  1558. if (!(chip->options & NAND_NO_READRDY)) {
  1559. /*
  1560. * Apply delay or wait for ready/busy pin. Do this
  1561. * before the AUTOINCR check, so no problems arise if a
  1562. * chip which does auto increment is marked as
  1563. * NOAUTOINCR by the board driver.
  1564. */
  1565. if (!chip->dev_ready)
  1566. udelay(chip->chip_delay);
  1567. else
  1568. nand_wait_ready(mtd);
  1569. }
  1570. readlen -= len;
  1571. if (!readlen)
  1572. break;
  1573. /* Increment page address */
  1574. realpage++;
  1575. page = realpage & chip->pagemask;
  1576. /* Check, if we cross a chip boundary */
  1577. if (!page) {
  1578. chipnr++;
  1579. chip->select_chip(mtd, -1);
  1580. chip->select_chip(mtd, chipnr);
  1581. }
  1582. /* Check, if the chip supports auto page increment
  1583. * or if we have hit a block boundary.
  1584. */
  1585. if (!NAND_CANAUTOINCR(chip) || !(page & blkcheck))
  1586. sndcmd = 1;
  1587. }
  1588. ops->oobretlen = ops->ooblen;
  1589. return 0;
  1590. }
  1591. /**
  1592. * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
  1593. * @mtd: MTD device structure
  1594. * @from: offset to read from
  1595. * @ops: oob operation description structure
  1596. *
  1597. * NAND read data and/or out-of-band data
  1598. */
  1599. static int nand_read_oob(struct mtd_info *mtd, loff_t from,
  1600. struct mtd_oob_ops *ops)
  1601. {
  1602. struct nand_chip *chip = mtd->priv;
  1603. int ret = -ENOTSUPP;
  1604. ops->retlen = 0;
  1605. /* Do not allow reads past end of device */
  1606. if (ops->datbuf && (from + ops->len) > mtd->size) {
  1607. DEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt read "
  1608. "beyond end of device\n", __func__);
  1609. return -EINVAL;
  1610. }
  1611. nand_get_device(chip, mtd, FL_READING);
  1612. switch(ops->mode) {
  1613. case MTD_OOB_PLACE:
  1614. case MTD_OOB_AUTO:
  1615. case MTD_OOB_RAW:
  1616. break;
  1617. default:
  1618. goto out;
  1619. }
  1620. if (!ops->datbuf)
  1621. ret = nand_do_read_oob(mtd, from, ops);
  1622. else
  1623. ret = nand_do_read_ops(mtd, from, ops);
  1624. out:
  1625. nand_release_device(mtd);
  1626. return ret;
  1627. }
  1628. /**
  1629. * nand_write_page_raw - [Intern] raw page write function
  1630. * @mtd: mtd info structure
  1631. * @chip: nand chip info structure
  1632. * @buf: data buffer
  1633. *
  1634. * Not for syndrome calculating ecc controllers, which use a special oob layout
  1635. */
  1636. static void nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
  1637. const uint8_t *buf)
  1638. {
  1639. chip->write_buf(mtd, buf, mtd->writesize);
  1640. chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
  1641. }
  1642. /**
  1643. * nand_write_page_raw_syndrome - [Intern] raw page write function
  1644. * @mtd: mtd info structure
  1645. * @chip: nand chip info structure
  1646. * @buf: data buffer
  1647. *
  1648. * We need a special oob layout and handling even when ECC isn't checked.
  1649. */
  1650. static void nand_write_page_raw_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
  1651. const uint8_t *buf)
  1652. {
  1653. int eccsize = chip->ecc.size;
  1654. int eccbytes = chip->ecc.bytes;
  1655. uint8_t *oob = chip->oob_poi;
  1656. int steps, size;
  1657. for (steps = chip->ecc.steps; steps > 0; steps--) {
  1658. chip->write_buf(mtd, buf, eccsize);
  1659. buf += eccsize;
  1660. if (chip->ecc.prepad) {
  1661. chip->write_buf(mtd, oob, chip->ecc.prepad);
  1662. oob += chip->ecc.prepad;
  1663. }
  1664. chip->read_buf(mtd, oob, eccbytes);
  1665. oob += eccbytes;
  1666. if (chip->ecc.postpad) {
  1667. chip->write_buf(mtd, oob, chip->ecc.postpad);
  1668. oob += chip->ecc.postpad;
  1669. }
  1670. }
  1671. size = mtd->oobsize - (oob - chip->oob_poi);
  1672. if (size)
  1673. chip->write_buf(mtd, oob, size);
  1674. }
  1675. /**
  1676. * nand_write_page_swecc - [REPLACABLE] software ecc based page write function
  1677. * @mtd: mtd info structure
  1678. * @chip: nand chip info structure
  1679. * @buf: data buffer
  1680. */
  1681. static void nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
  1682. const uint8_t *buf)
  1683. {
  1684. int i, eccsize = chip->ecc.size;
  1685. int eccbytes = chip->ecc.bytes;
  1686. int eccsteps = chip->ecc.steps;
  1687. uint8_t *ecc_calc = chip->buffers->ecccalc;
  1688. const uint8_t *p = buf;
  1689. uint32_t *eccpos = chip->ecc.layout->eccpos;
  1690. /* Software ecc calculation */
  1691. for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
  1692. chip->ecc.calculate(mtd, p, &ecc_calc[i]);
  1693. for (i = 0; i < chip->ecc.total; i++)
  1694. chip->oob_poi[eccpos[i]] = ecc_calc[i];
  1695. chip->ecc.write_page_raw(mtd, chip, buf);
  1696. }
  1697. /**
  1698. * nand_write_page_hwecc - [REPLACABLE] hardware ecc based page write function
  1699. * @mtd: mtd info structure
  1700. * @chip: nand chip info structure
  1701. * @buf: data buffer
  1702. */
  1703. static void nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
  1704. const uint8_t *buf)
  1705. {
  1706. int i, eccsize = chip->ecc.size;
  1707. int eccbytes = chip->ecc.bytes;
  1708. int eccsteps = chip->ecc.steps;
  1709. uint8_t *ecc_calc = chip->buffers->ecccalc;
  1710. const uint8_t *p = buf;
  1711. uint32_t *eccpos = chip->ecc.layout->eccpos;
  1712. for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
  1713. chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
  1714. chip->write_buf(mtd, p, eccsize);
  1715. chip->ecc.calculate(mtd, p, &ecc_calc[i]);
  1716. }
  1717. for (i = 0; i < chip->ecc.total; i++)
  1718. chip->oob_poi[eccpos[i]] = ecc_calc[i];
  1719. chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
  1720. }
  1721. /**
  1722. * nand_write_page_syndrome - [REPLACABLE] hardware ecc syndrom based page write
  1723. * @mtd: mtd info structure
  1724. * @chip: nand chip info structure
  1725. * @buf: data buffer
  1726. *
  1727. * The hw generator calculates the error syndrome automatically. Therefor
  1728. * we need a special oob layout and handling.
  1729. */
  1730. static void nand_write_page_syndrome(struct mtd_info *mtd,
  1731. struct nand_chip *chip, const uint8_t *buf)
  1732. {
  1733. int i, eccsize = chip->ecc.size;
  1734. int eccbytes = chip->ecc.bytes;
  1735. int eccsteps = chip->ecc.steps;
  1736. const uint8_t *p = buf;
  1737. uint8_t *oob = chip->oob_poi;
  1738. for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
  1739. chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
  1740. chip->write_buf(mtd, p, eccsize);
  1741. if (chip->ecc.prepad) {
  1742. chip->write_buf(mtd, oob, chip->ecc.prepad);
  1743. oob += chip->ecc.prepad;
  1744. }
  1745. chip->ecc.calculate(mtd, p, oob);
  1746. chip->write_buf(mtd, oob, eccbytes);
  1747. oob += eccbytes;
  1748. if (chip->ecc.postpad) {
  1749. chip->write_buf(mtd, oob, chip->ecc.postpad);
  1750. oob += chip->ecc.postpad;
  1751. }
  1752. }
  1753. /* Calculate remaining oob bytes */
  1754. i = mtd->oobsize - (oob - chip->oob_poi);
  1755. if (i)
  1756. chip->write_buf(mtd, oob, i);
  1757. }
  1758. /**
  1759. * nand_write_page - [REPLACEABLE] write one page
  1760. * @mtd: MTD device structure
  1761. * @chip: NAND chip descriptor
  1762. * @buf: the data to write
  1763. * @page: page number to write
  1764. * @cached: cached programming
  1765. * @raw: use _raw version of write_page
  1766. */
  1767. static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
  1768. const uint8_t *buf, int page, int cached, int raw)
  1769. {
  1770. int status;
  1771. chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
  1772. if (unlikely(raw))
  1773. chip->ecc.write_page_raw(mtd, chip, buf);
  1774. else
  1775. chip->ecc.write_page(mtd, chip, buf);
  1776. /*
  1777. * Cached progamming disabled for now, Not sure if its worth the
  1778. * trouble. The speed gain is not very impressive. (2.3->2.6Mib/s)
  1779. */
  1780. cached = 0;
  1781. if (!cached || !(chip->options & NAND_CACHEPRG)) {
  1782. chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
  1783. status = chip->waitfunc(mtd, chip);
  1784. /*
  1785. * See if operation failed and additional status checks are
  1786. * available
  1787. */
  1788. if ((status & NAND_STATUS_FAIL) && (chip->errstat))
  1789. status = chip->errstat(mtd, chip, FL_WRITING, status,
  1790. page);
  1791. if (status & NAND_STATUS_FAIL)
  1792. return -EIO;
  1793. } else {
  1794. chip->cmdfunc(mtd, NAND_CMD_CACHEDPROG, -1, -1);
  1795. status = chip->waitfunc(mtd, chip);
  1796. }
  1797. #ifdef CONFIG_MTD_NAND_VERIFY_WRITE
  1798. /* Send command to read back the data */
  1799. chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
  1800. if (chip->verify_buf(mtd, buf, mtd->writesize))
  1801. return -EIO;
  1802. #endif
  1803. return 0;
  1804. }
  1805. /**
  1806. * nand_fill_oob - [Internal] Transfer client buffer to oob
  1807. * @chip: nand chip structure
  1808. * @oob: oob data buffer
  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. id_data[5] != 0x00) {
  2452. /* Calc pagesize */
  2453. mtd->writesize = 2048 << (extid & 0x03);
  2454. extid >>= 2;
  2455. /* Calc oobsize */
  2456. mtd->oobsize = (extid & 0x03) == 0x01 ? 128 : 218;
  2457. extid >>= 2;
  2458. /* Calc blocksize */
  2459. mtd->erasesize = (128 * 1024) <<
  2460. (((extid >> 1) & 0x04) | (extid & 0x03));
  2461. busw = 0;
  2462. } else {
  2463. /* Calc pagesize */
  2464. mtd->writesize = 1024 << (extid & 0x03);
  2465. extid >>= 2;
  2466. /* Calc oobsize */
  2467. mtd->oobsize = (8 << (extid & 0x01)) *
  2468. (mtd->writesize >> 9);
  2469. extid >>= 2;
  2470. /* Calc blocksize. Blocksize is multiples of 64KiB */
  2471. mtd->erasesize = (64 * 1024) << (extid & 0x03);
  2472. extid >>= 2;
  2473. /* Get buswidth information */
  2474. busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0;
  2475. }
  2476. } else {
  2477. /*
  2478. * Old devices have chip data hardcoded in the device id table
  2479. */
  2480. mtd->erasesize = type->erasesize;
  2481. mtd->writesize = type->pagesize;
  2482. mtd->oobsize = mtd->writesize / 32;
  2483. busw = type->options & NAND_BUSWIDTH_16;
  2484. }
  2485. /* Try to identify manufacturer */
  2486. for (maf_idx = 0; nand_manuf_ids[maf_idx].id != 0x0; maf_idx++) {
  2487. if (nand_manuf_ids[maf_idx].id == *maf_id)
  2488. break;
  2489. }
  2490. /*
  2491. * Check, if buswidth is correct. Hardware drivers should set
  2492. * chip correct !
  2493. */
  2494. if (busw != (chip->options & NAND_BUSWIDTH_16)) {
  2495. printk(KERN_INFO "NAND device: Manufacturer ID:"
  2496. " 0x%02x, Chip ID: 0x%02x (%s %s)\n", *maf_id,
  2497. dev_id, nand_manuf_ids[maf_idx].name, mtd->name);
  2498. printk(KERN_WARNING "NAND bus width %d instead %d bit\n",
  2499. (chip->options & NAND_BUSWIDTH_16) ? 16 : 8,
  2500. busw ? 16 : 8);
  2501. return ERR_PTR(-EINVAL);
  2502. }
  2503. /* Calculate the address shift from the page size */
  2504. chip->page_shift = ffs(mtd->writesize) - 1;
  2505. /* Convert chipsize to number of pages per chip -1. */
  2506. chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
  2507. chip->bbt_erase_shift = chip->phys_erase_shift =
  2508. ffs(mtd->erasesize) - 1;
  2509. if (chip->chipsize & 0xffffffff)
  2510. chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
  2511. else
  2512. chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32)) + 32 - 1;
  2513. /* Set the bad block position */
  2514. if (!(busw & NAND_BUSWIDTH_16) && (*maf_id == NAND_MFR_STMICRO ||
  2515. (*maf_id == NAND_MFR_SAMSUNG &&
  2516. mtd->writesize == 512) ||
  2517. *maf_id == NAND_MFR_AMD))
  2518. chip->badblockpos = NAND_SMALL_BADBLOCK_POS;
  2519. else
  2520. chip->badblockpos = NAND_LARGE_BADBLOCK_POS;
  2521. /* Get chip options, preserve non chip based options */
  2522. chip->options &= ~NAND_CHIPOPTIONS_MSK;
  2523. chip->options |= type->options & NAND_CHIPOPTIONS_MSK;
  2524. /*
  2525. * Set chip as a default. Board drivers can override it, if necessary
  2526. */
  2527. chip->options |= NAND_NO_AUTOINCR;
  2528. /* Check if chip is a not a samsung device. Do not clear the
  2529. * options for chips which are not having an extended id.
  2530. */
  2531. if (*maf_id != NAND_MFR_SAMSUNG && !type->pagesize)
  2532. chip->options &= ~NAND_SAMSUNG_LP_OPTIONS;
  2533. /*
  2534. * Bad block marker is stored in the last page of each block
  2535. * on Samsung and Hynix MLC devices; stored in first two pages
  2536. * of each block on Micron devices with 2KiB pages and on
  2537. * SLC Samsung, Hynix, and AMD/Spansion. All others scan only
  2538. * the first page.
  2539. */
  2540. if ((chip->cellinfo & NAND_CI_CELLTYPE_MSK) &&
  2541. (*maf_id == NAND_MFR_SAMSUNG ||
  2542. *maf_id == NAND_MFR_HYNIX))
  2543. chip->options |= NAND_BBT_SCANLASTPAGE;
  2544. else if ((!(chip->cellinfo & NAND_CI_CELLTYPE_MSK) &&
  2545. (*maf_id == NAND_MFR_SAMSUNG ||
  2546. *maf_id == NAND_MFR_HYNIX ||
  2547. *maf_id == NAND_MFR_AMD)) ||
  2548. (mtd->writesize == 2048 &&
  2549. *maf_id == NAND_MFR_MICRON))
  2550. chip->options |= NAND_BBT_SCAN2NDPAGE;
  2551. /*
  2552. * Numonyx/ST 2K pages, x8 bus use BOTH byte 1 and 6
  2553. */
  2554. if (!(busw & NAND_BUSWIDTH_16) &&
  2555. *maf_id == NAND_MFR_STMICRO &&
  2556. mtd->writesize == 2048) {
  2557. chip->options |= NAND_BBT_SCANBYTE1AND6;
  2558. chip->badblockpos = 0;
  2559. }
  2560. /* Check for AND chips with 4 page planes */
  2561. if (chip->options & NAND_4PAGE_ARRAY)
  2562. chip->erase_cmd = multi_erase_cmd;
  2563. else
  2564. chip->erase_cmd = single_erase_cmd;
  2565. /* Do not replace user supplied command function ! */
  2566. if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
  2567. chip->cmdfunc = nand_command_lp;
  2568. printk(KERN_INFO "NAND device: Manufacturer ID:"
  2569. " 0x%02x, Chip ID: 0x%02x (%s %s)\n", *maf_id, dev_id,
  2570. nand_manuf_ids[maf_idx].name, type->name);
  2571. return type;
  2572. }
  2573. /**
  2574. * nand_scan_ident - [NAND Interface] Scan for the NAND device
  2575. * @mtd: MTD device structure
  2576. * @maxchips: Number of chips to scan for
  2577. * @table: Alternative NAND ID table
  2578. *
  2579. * This is the first phase of the normal nand_scan() function. It
  2580. * reads the flash ID and sets up MTD fields accordingly.
  2581. *
  2582. * The mtd->owner field must be set to the module of the caller.
  2583. */
  2584. int nand_scan_ident(struct mtd_info *mtd, int maxchips,
  2585. struct nand_flash_dev *table)
  2586. {
  2587. int i, busw, nand_maf_id;
  2588. struct nand_chip *chip = mtd->priv;
  2589. struct nand_flash_dev *type;
  2590. /* Get buswidth to select the correct functions */
  2591. busw = chip->options & NAND_BUSWIDTH_16;
  2592. /* Set the default functions */
  2593. nand_set_defaults(chip, busw);
  2594. /* Read the flash type */
  2595. type = nand_get_flash_type(mtd, chip, busw, &nand_maf_id, table);
  2596. if (IS_ERR(type)) {
  2597. if (!(chip->options & NAND_SCAN_SILENT_NODEV))
  2598. printk(KERN_WARNING "No NAND device found.\n");
  2599. chip->select_chip(mtd, -1);
  2600. return PTR_ERR(type);
  2601. }
  2602. /* Check for a chip array */
  2603. for (i = 1; i < maxchips; i++) {
  2604. chip->select_chip(mtd, i);
  2605. /* See comment in nand_get_flash_type for reset */
  2606. chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
  2607. /* Send the command for reading device ID */
  2608. chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
  2609. /* Read manufacturer and device IDs */
  2610. if (nand_maf_id != chip->read_byte(mtd) ||
  2611. type->id != chip->read_byte(mtd))
  2612. break;
  2613. }
  2614. if (i > 1)
  2615. printk(KERN_INFO "%d NAND chips detected\n", i);
  2616. /* Store the number of chips and calc total size for mtd */
  2617. chip->numchips = i;
  2618. mtd->size = i * chip->chipsize;
  2619. return 0;
  2620. }
  2621. /**
  2622. * nand_scan_tail - [NAND Interface] Scan for the NAND device
  2623. * @mtd: MTD device structure
  2624. *
  2625. * This is the second phase of the normal nand_scan() function. It
  2626. * fills out all the uninitialized function pointers with the defaults
  2627. * and scans for a bad block table if appropriate.
  2628. */
  2629. int nand_scan_tail(struct mtd_info *mtd)
  2630. {
  2631. int i;
  2632. struct nand_chip *chip = mtd->priv;
  2633. if (!(chip->options & NAND_OWN_BUFFERS))
  2634. chip->buffers = kmalloc(sizeof(*chip->buffers), GFP_KERNEL);
  2635. if (!chip->buffers)
  2636. return -ENOMEM;
  2637. /* Set the internal oob buffer location, just after the page data */
  2638. chip->oob_poi = chip->buffers->databuf + mtd->writesize;
  2639. /*
  2640. * If no default placement scheme is given, select an appropriate one
  2641. */
  2642. if (!chip->ecc.layout) {
  2643. switch (mtd->oobsize) {
  2644. case 8:
  2645. chip->ecc.layout = &nand_oob_8;
  2646. break;
  2647. case 16:
  2648. chip->ecc.layout = &nand_oob_16;
  2649. break;
  2650. case 64:
  2651. chip->ecc.layout = &nand_oob_64;
  2652. break;
  2653. case 128:
  2654. chip->ecc.layout = &nand_oob_128;
  2655. break;
  2656. default:
  2657. printk(KERN_WARNING "No oob scheme defined for "
  2658. "oobsize %d\n", mtd->oobsize);
  2659. BUG();
  2660. }
  2661. }
  2662. if (!chip->write_page)
  2663. chip->write_page = nand_write_page;
  2664. /*
  2665. * check ECC mode, default to software if 3byte/512byte hardware ECC is
  2666. * selected and we have 256 byte pagesize fallback to software ECC
  2667. */
  2668. switch (chip->ecc.mode) {
  2669. case NAND_ECC_HW_OOB_FIRST:
  2670. /* Similar to NAND_ECC_HW, but a separate read_page handle */
  2671. if (!chip->ecc.calculate || !chip->ecc.correct ||
  2672. !chip->ecc.hwctl) {
  2673. printk(KERN_WARNING "No ECC functions supplied; "
  2674. "Hardware ECC not possible\n");
  2675. BUG();
  2676. }
  2677. if (!chip->ecc.read_page)
  2678. chip->ecc.read_page = nand_read_page_hwecc_oob_first;
  2679. case NAND_ECC_HW:
  2680. /* Use standard hwecc read page function ? */
  2681. if (!chip->ecc.read_page)
  2682. chip->ecc.read_page = nand_read_page_hwecc;
  2683. if (!chip->ecc.write_page)
  2684. chip->ecc.write_page = nand_write_page_hwecc;
  2685. if (!chip->ecc.read_page_raw)
  2686. chip->ecc.read_page_raw = nand_read_page_raw;
  2687. if (!chip->ecc.write_page_raw)
  2688. chip->ecc.write_page_raw = nand_write_page_raw;
  2689. if (!chip->ecc.read_oob)
  2690. chip->ecc.read_oob = nand_read_oob_std;
  2691. if (!chip->ecc.write_oob)
  2692. chip->ecc.write_oob = nand_write_oob_std;
  2693. case NAND_ECC_HW_SYNDROME:
  2694. if ((!chip->ecc.calculate || !chip->ecc.correct ||
  2695. !chip->ecc.hwctl) &&
  2696. (!chip->ecc.read_page ||
  2697. chip->ecc.read_page == nand_read_page_hwecc ||
  2698. !chip->ecc.write_page ||
  2699. chip->ecc.write_page == nand_write_page_hwecc)) {
  2700. printk(KERN_WARNING "No ECC functions supplied; "
  2701. "Hardware ECC not possible\n");
  2702. BUG();
  2703. }
  2704. /* Use standard syndrome read/write page function ? */
  2705. if (!chip->ecc.read_page)
  2706. chip->ecc.read_page = nand_read_page_syndrome;
  2707. if (!chip->ecc.write_page)
  2708. chip->ecc.write_page = nand_write_page_syndrome;
  2709. if (!chip->ecc.read_page_raw)
  2710. chip->ecc.read_page_raw = nand_read_page_raw_syndrome;
  2711. if (!chip->ecc.write_page_raw)
  2712. chip->ecc.write_page_raw = nand_write_page_raw_syndrome;
  2713. if (!chip->ecc.read_oob)
  2714. chip->ecc.read_oob = nand_read_oob_syndrome;
  2715. if (!chip->ecc.write_oob)
  2716. chip->ecc.write_oob = nand_write_oob_syndrome;
  2717. if (mtd->writesize >= chip->ecc.size)
  2718. break;
  2719. printk(KERN_WARNING "%d byte HW ECC not possible on "
  2720. "%d byte page size, fallback to SW ECC\n",
  2721. chip->ecc.size, mtd->writesize);
  2722. chip->ecc.mode = NAND_ECC_SOFT;
  2723. case NAND_ECC_SOFT:
  2724. chip->ecc.calculate = nand_calculate_ecc;
  2725. chip->ecc.correct = nand_correct_data;
  2726. chip->ecc.read_page = nand_read_page_swecc;
  2727. chip->ecc.read_subpage = nand_read_subpage;
  2728. chip->ecc.write_page = nand_write_page_swecc;
  2729. chip->ecc.read_page_raw = nand_read_page_raw;
  2730. chip->ecc.write_page_raw = nand_write_page_raw;
  2731. chip->ecc.read_oob = nand_read_oob_std;
  2732. chip->ecc.write_oob = nand_write_oob_std;
  2733. if (!chip->ecc.size)
  2734. chip->ecc.size = 256;
  2735. chip->ecc.bytes = 3;
  2736. break;
  2737. case NAND_ECC_NONE:
  2738. printk(KERN_WARNING "NAND_ECC_NONE selected by board driver. "
  2739. "This is not recommended !!\n");
  2740. chip->ecc.read_page = nand_read_page_raw;
  2741. chip->ecc.write_page = nand_write_page_raw;
  2742. chip->ecc.read_oob = nand_read_oob_std;
  2743. chip->ecc.read_page_raw = nand_read_page_raw;
  2744. chip->ecc.write_page_raw = nand_write_page_raw;
  2745. chip->ecc.write_oob = nand_write_oob_std;
  2746. chip->ecc.size = mtd->writesize;
  2747. chip->ecc.bytes = 0;
  2748. break;
  2749. default:
  2750. printk(KERN_WARNING "Invalid NAND_ECC_MODE %d\n",
  2751. chip->ecc.mode);
  2752. BUG();
  2753. }
  2754. /*
  2755. * The number of bytes available for a client to place data into
  2756. * the out of band area
  2757. */
  2758. chip->ecc.layout->oobavail = 0;
  2759. for (i = 0; chip->ecc.layout->oobfree[i].length
  2760. && i < ARRAY_SIZE(chip->ecc.layout->oobfree); i++)
  2761. chip->ecc.layout->oobavail +=
  2762. chip->ecc.layout->oobfree[i].length;
  2763. mtd->oobavail = chip->ecc.layout->oobavail;
  2764. /*
  2765. * Set the number of read / write steps for one page depending on ECC
  2766. * mode
  2767. */
  2768. chip->ecc.steps = mtd->writesize / chip->ecc.size;
  2769. if(chip->ecc.steps * chip->ecc.size != mtd->writesize) {
  2770. printk(KERN_WARNING "Invalid ecc parameters\n");
  2771. BUG();
  2772. }
  2773. chip->ecc.total = chip->ecc.steps * chip->ecc.bytes;
  2774. /*
  2775. * Allow subpage writes up to ecc.steps. Not possible for MLC
  2776. * FLASH.
  2777. */
  2778. if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
  2779. !(chip->cellinfo & NAND_CI_CELLTYPE_MSK)) {
  2780. switch(chip->ecc.steps) {
  2781. case 2:
  2782. mtd->subpage_sft = 1;
  2783. break;
  2784. case 4:
  2785. case 8:
  2786. case 16:
  2787. mtd->subpage_sft = 2;
  2788. break;
  2789. }
  2790. }
  2791. chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
  2792. /* Initialize state */
  2793. chip->state = FL_READY;
  2794. /* De-select the device */
  2795. chip->select_chip(mtd, -1);
  2796. /* Invalidate the pagebuffer reference */
  2797. chip->pagebuf = -1;
  2798. /* Fill in remaining MTD driver data */
  2799. mtd->type = MTD_NANDFLASH;
  2800. mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM :
  2801. MTD_CAP_NANDFLASH;
  2802. mtd->erase = nand_erase;
  2803. mtd->point = NULL;
  2804. mtd->unpoint = NULL;
  2805. mtd->read = nand_read;
  2806. mtd->write = nand_write;
  2807. mtd->panic_write = panic_nand_write;
  2808. mtd->read_oob = nand_read_oob;
  2809. mtd->write_oob = nand_write_oob;
  2810. mtd->sync = nand_sync;
  2811. mtd->lock = NULL;
  2812. mtd->unlock = NULL;
  2813. mtd->suspend = nand_suspend;
  2814. mtd->resume = nand_resume;
  2815. mtd->block_isbad = nand_block_isbad;
  2816. mtd->block_markbad = nand_block_markbad;
  2817. /* propagate ecc.layout to mtd_info */
  2818. mtd->ecclayout = chip->ecc.layout;
  2819. /* Check, if we should skip the bad block table scan */
  2820. if (chip->options & NAND_SKIP_BBTSCAN)
  2821. return 0;
  2822. /* Build bad block table */
  2823. return chip->scan_bbt(mtd);
  2824. }
  2825. /* is_module_text_address() isn't exported, and it's mostly a pointless
  2826. test if this is a module _anyway_ -- they'd have to try _really_ hard
  2827. to call us from in-kernel code if the core NAND support is modular. */
  2828. #ifdef MODULE
  2829. #define caller_is_module() (1)
  2830. #else
  2831. #define caller_is_module() \
  2832. is_module_text_address((unsigned long)__builtin_return_address(0))
  2833. #endif
  2834. /**
  2835. * nand_scan - [NAND Interface] Scan for the NAND device
  2836. * @mtd: MTD device structure
  2837. * @maxchips: Number of chips to scan for
  2838. *
  2839. * This fills out all the uninitialized function pointers
  2840. * with the defaults.
  2841. * The flash ID is read and the mtd/chip structures are
  2842. * filled with the appropriate values.
  2843. * The mtd->owner field must be set to the module of the caller
  2844. *
  2845. */
  2846. int nand_scan(struct mtd_info *mtd, int maxchips)
  2847. {
  2848. int ret;
  2849. /* Many callers got this wrong, so check for it for a while... */
  2850. if (!mtd->owner && caller_is_module()) {
  2851. printk(KERN_CRIT "%s called with NULL mtd->owner!\n",
  2852. __func__);
  2853. BUG();
  2854. }
  2855. ret = nand_scan_ident(mtd, maxchips, NULL);
  2856. if (!ret)
  2857. ret = nand_scan_tail(mtd);
  2858. return ret;
  2859. }
  2860. /**
  2861. * nand_release - [NAND Interface] Free resources held by the NAND device
  2862. * @mtd: MTD device structure
  2863. */
  2864. void nand_release(struct mtd_info *mtd)
  2865. {
  2866. struct nand_chip *chip = mtd->priv;
  2867. #ifdef CONFIG_MTD_PARTITIONS
  2868. /* Deregister partitions */
  2869. del_mtd_partitions(mtd);
  2870. #endif
  2871. /* Deregister the device */
  2872. del_mtd_device(mtd);
  2873. /* Free bad block table memory */
  2874. kfree(chip->bbt);
  2875. if (!(chip->options & NAND_OWN_BUFFERS))
  2876. kfree(chip->buffers);
  2877. /* Free bad block descriptor memory */
  2878. if (chip->badblock_pattern && chip->badblock_pattern->options
  2879. & NAND_BBT_DYNAMICSTRUCT)
  2880. kfree(chip->badblock_pattern);
  2881. }
  2882. EXPORT_SYMBOL_GPL(nand_lock);
  2883. EXPORT_SYMBOL_GPL(nand_unlock);
  2884. EXPORT_SYMBOL_GPL(nand_scan);
  2885. EXPORT_SYMBOL_GPL(nand_scan_ident);
  2886. EXPORT_SYMBOL_GPL(nand_scan_tail);
  2887. EXPORT_SYMBOL_GPL(nand_release);
  2888. static int __init nand_base_init(void)
  2889. {
  2890. led_trigger_register_simple("nand-disk", &nand_led_trigger);
  2891. return 0;
  2892. }
  2893. static void __exit nand_base_exit(void)
  2894. {
  2895. led_trigger_unregister_simple(nand_led_trigger);
  2896. }
  2897. module_init(nand_base_init);
  2898. module_exit(nand_base_exit);
  2899. MODULE_LICENSE("GPL");
  2900. MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>, Thomas Gleixner <tglx@linutronix.de>");
  2901. MODULE_DESCRIPTION("Generic NAND flash driver code");