nand_base.c 77 KB

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