nand_base.c 84 KB

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