nand_base.c 75 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913
  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 independend devices */
  616. /* Hardware controller shared among independend devices */
  617. if (!chip->controller->active)
  618. chip->controller->active = chip;
  619. if (chip->controller->active == chip && chip->state == FL_READY) {
  620. chip->state = new_state;
  621. spin_unlock(lock);
  622. return 0;
  623. }
  624. if (new_state == FL_PM_SUSPENDED) {
  625. spin_unlock(lock);
  626. return (chip->state == FL_PM_SUSPENDED) ? 0 : -EAGAIN;
  627. }
  628. set_current_state(TASK_UNINTERRUPTIBLE);
  629. add_wait_queue(wq, &wait);
  630. spin_unlock(lock);
  631. schedule();
  632. remove_wait_queue(wq, &wait);
  633. goto retry;
  634. }
  635. /**
  636. * nand_wait - [DEFAULT] wait until the command is done
  637. * @mtd: MTD device structure
  638. * @chip: NAND chip structure
  639. *
  640. * Wait for command done. This applies to erase and program only
  641. * Erase can take up to 400ms and program up to 20ms according to
  642. * general NAND and SmartMedia specs
  643. */
  644. static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip)
  645. {
  646. unsigned long timeo = jiffies;
  647. int status, state = chip->state;
  648. if (state == FL_ERASING)
  649. timeo += (HZ * 400) / 1000;
  650. else
  651. timeo += (HZ * 20) / 1000;
  652. led_trigger_event(nand_led_trigger, LED_FULL);
  653. /* Apply this short delay always to ensure that we do wait tWB in
  654. * any case on any machine. */
  655. ndelay(100);
  656. if ((state == FL_ERASING) && (chip->options & NAND_IS_AND))
  657. chip->cmdfunc(mtd, NAND_CMD_STATUS_MULTI, -1, -1);
  658. else
  659. chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
  660. while (time_before(jiffies, timeo)) {
  661. if (chip->dev_ready) {
  662. if (chip->dev_ready(mtd))
  663. break;
  664. } else {
  665. if (chip->read_byte(mtd) & NAND_STATUS_READY)
  666. break;
  667. }
  668. cond_resched();
  669. }
  670. led_trigger_event(nand_led_trigger, LED_OFF);
  671. status = (int)chip->read_byte(mtd);
  672. return status;
  673. }
  674. /**
  675. * nand_read_page_raw - [Intern] read raw page data without ecc
  676. * @mtd: mtd info structure
  677. * @chip: nand chip info structure
  678. * @buf: buffer to store read data
  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)
  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. *
  695. * We need a special oob layout and handling even when OOB isn't used.
  696. */
  697. static int nand_read_page_raw_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
  698. uint8_t *buf)
  699. {
  700. int eccsize = chip->ecc.size;
  701. int eccbytes = chip->ecc.bytes;
  702. uint8_t *oob = chip->oob_poi;
  703. int steps, size;
  704. for (steps = chip->ecc.steps; steps > 0; steps--) {
  705. chip->read_buf(mtd, buf, eccsize);
  706. buf += eccsize;
  707. if (chip->ecc.prepad) {
  708. chip->read_buf(mtd, oob, chip->ecc.prepad);
  709. oob += chip->ecc.prepad;
  710. }
  711. chip->read_buf(mtd, oob, eccbytes);
  712. oob += eccbytes;
  713. if (chip->ecc.postpad) {
  714. chip->read_buf(mtd, oob, chip->ecc.postpad);
  715. oob += chip->ecc.postpad;
  716. }
  717. }
  718. size = mtd->oobsize - (oob - chip->oob_poi);
  719. if (size)
  720. chip->read_buf(mtd, oob, size);
  721. return 0;
  722. }
  723. /**
  724. * nand_read_page_swecc - [REPLACABLE] software ecc based page read function
  725. * @mtd: mtd info structure
  726. * @chip: nand chip info structure
  727. * @buf: buffer to store read data
  728. */
  729. static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
  730. uint8_t *buf)
  731. {
  732. int i, eccsize = chip->ecc.size;
  733. int eccbytes = chip->ecc.bytes;
  734. int eccsteps = chip->ecc.steps;
  735. uint8_t *p = buf;
  736. uint8_t *ecc_calc = chip->buffers->ecccalc;
  737. uint8_t *ecc_code = chip->buffers->ecccode;
  738. uint32_t *eccpos = chip->ecc.layout->eccpos;
  739. chip->ecc.read_page_raw(mtd, chip, buf);
  740. for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
  741. chip->ecc.calculate(mtd, p, &ecc_calc[i]);
  742. for (i = 0; i < chip->ecc.total; i++)
  743. ecc_code[i] = chip->oob_poi[eccpos[i]];
  744. eccsteps = chip->ecc.steps;
  745. p = buf;
  746. for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
  747. int stat;
  748. stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
  749. if (stat < 0)
  750. mtd->ecc_stats.failed++;
  751. else
  752. mtd->ecc_stats.corrected += stat;
  753. }
  754. return 0;
  755. }
  756. /**
  757. * nand_read_subpage - [REPLACABLE] software ecc based sub-page read function
  758. * @mtd: mtd info structure
  759. * @chip: nand chip info structure
  760. * @data_offs: offset of requested data within the page
  761. * @readlen: data length
  762. * @bufpoi: buffer to store read data
  763. */
  764. static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip, uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi)
  765. {
  766. int start_step, end_step, num_steps;
  767. uint32_t *eccpos = chip->ecc.layout->eccpos;
  768. uint8_t *p;
  769. int data_col_addr, i, gaps = 0;
  770. int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
  771. int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
  772. /* Column address wihin the page aligned to ECC size (256bytes). */
  773. start_step = data_offs / chip->ecc.size;
  774. end_step = (data_offs + readlen - 1) / chip->ecc.size;
  775. num_steps = end_step - start_step + 1;
  776. /* Data size aligned to ECC ecc.size*/
  777. datafrag_len = num_steps * chip->ecc.size;
  778. eccfrag_len = num_steps * chip->ecc.bytes;
  779. data_col_addr = start_step * chip->ecc.size;
  780. /* If we read not a page aligned data */
  781. if (data_col_addr != 0)
  782. chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_col_addr, -1);
  783. p = bufpoi + data_col_addr;
  784. chip->read_buf(mtd, p, datafrag_len);
  785. /* Calculate ECC */
  786. for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
  787. chip->ecc.calculate(mtd, p, &chip->buffers->ecccalc[i]);
  788. /* The performance is faster if to position offsets
  789. according to ecc.pos. Let make sure here that
  790. there are no gaps in ecc positions */
  791. for (i = 0; i < eccfrag_len - 1; i++) {
  792. if (eccpos[i + start_step * chip->ecc.bytes] + 1 !=
  793. eccpos[i + start_step * chip->ecc.bytes + 1]) {
  794. gaps = 1;
  795. break;
  796. }
  797. }
  798. if (gaps) {
  799. chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
  800. chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
  801. } else {
  802. /* send the command to read the particular ecc bytes */
  803. /* take care about buswidth alignment in read_buf */
  804. aligned_pos = eccpos[start_step * chip->ecc.bytes] & ~(busw - 1);
  805. aligned_len = eccfrag_len;
  806. if (eccpos[start_step * chip->ecc.bytes] & (busw - 1))
  807. aligned_len++;
  808. if (eccpos[(start_step + num_steps) * chip->ecc.bytes] & (busw - 1))
  809. aligned_len++;
  810. chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize + aligned_pos, -1);
  811. chip->read_buf(mtd, &chip->oob_poi[aligned_pos], aligned_len);
  812. }
  813. for (i = 0; i < eccfrag_len; i++)
  814. chip->buffers->ecccode[i] = chip->oob_poi[eccpos[i + start_step * chip->ecc.bytes]];
  815. p = bufpoi + data_col_addr;
  816. for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
  817. int stat;
  818. stat = chip->ecc.correct(mtd, p, &chip->buffers->ecccode[i], &chip->buffers->ecccalc[i]);
  819. if (stat == -1)
  820. mtd->ecc_stats.failed++;
  821. else
  822. mtd->ecc_stats.corrected += stat;
  823. }
  824. return 0;
  825. }
  826. /**
  827. * nand_read_page_hwecc - [REPLACABLE] hardware ecc based page read function
  828. * @mtd: mtd info structure
  829. * @chip: nand chip info structure
  830. * @buf: buffer to store read data
  831. *
  832. * Not for syndrome calculating ecc controllers which need a special oob layout
  833. */
  834. static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
  835. uint8_t *buf)
  836. {
  837. int i, eccsize = chip->ecc.size;
  838. int eccbytes = chip->ecc.bytes;
  839. int eccsteps = chip->ecc.steps;
  840. uint8_t *p = buf;
  841. uint8_t *ecc_calc = chip->buffers->ecccalc;
  842. uint8_t *ecc_code = chip->buffers->ecccode;
  843. uint32_t *eccpos = chip->ecc.layout->eccpos;
  844. for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
  845. chip->ecc.hwctl(mtd, NAND_ECC_READ);
  846. chip->read_buf(mtd, p, eccsize);
  847. chip->ecc.calculate(mtd, p, &ecc_calc[i]);
  848. }
  849. chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
  850. for (i = 0; i < chip->ecc.total; i++)
  851. ecc_code[i] = chip->oob_poi[eccpos[i]];
  852. eccsteps = chip->ecc.steps;
  853. p = buf;
  854. for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
  855. int stat;
  856. stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
  857. if (stat < 0)
  858. mtd->ecc_stats.failed++;
  859. else
  860. mtd->ecc_stats.corrected += stat;
  861. }
  862. return 0;
  863. }
  864. /**
  865. * nand_read_page_syndrome - [REPLACABLE] hardware ecc syndrom based page read
  866. * @mtd: mtd info structure
  867. * @chip: nand chip info structure
  868. * @buf: buffer to store read data
  869. *
  870. * The hw generator calculates the error syndrome automatically. Therefor
  871. * we need a special oob layout and handling.
  872. */
  873. static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
  874. uint8_t *buf)
  875. {
  876. int i, eccsize = chip->ecc.size;
  877. int eccbytes = chip->ecc.bytes;
  878. int eccsteps = chip->ecc.steps;
  879. uint8_t *p = buf;
  880. uint8_t *oob = chip->oob_poi;
  881. for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
  882. int stat;
  883. chip->ecc.hwctl(mtd, NAND_ECC_READ);
  884. chip->read_buf(mtd, p, eccsize);
  885. if (chip->ecc.prepad) {
  886. chip->read_buf(mtd, oob, chip->ecc.prepad);
  887. oob += chip->ecc.prepad;
  888. }
  889. chip->ecc.hwctl(mtd, NAND_ECC_READSYN);
  890. chip->read_buf(mtd, oob, eccbytes);
  891. stat = chip->ecc.correct(mtd, p, oob, NULL);
  892. if (stat < 0)
  893. mtd->ecc_stats.failed++;
  894. else
  895. mtd->ecc_stats.corrected += stat;
  896. oob += eccbytes;
  897. if (chip->ecc.postpad) {
  898. chip->read_buf(mtd, oob, chip->ecc.postpad);
  899. oob += chip->ecc.postpad;
  900. }
  901. }
  902. /* Calculate remaining oob bytes */
  903. i = mtd->oobsize - (oob - chip->oob_poi);
  904. if (i)
  905. chip->read_buf(mtd, oob, i);
  906. return 0;
  907. }
  908. /**
  909. * nand_transfer_oob - [Internal] Transfer oob to client buffer
  910. * @chip: nand chip structure
  911. * @oob: oob destination address
  912. * @ops: oob ops structure
  913. * @len: size of oob to transfer
  914. */
  915. static uint8_t *nand_transfer_oob(struct nand_chip *chip, uint8_t *oob,
  916. struct mtd_oob_ops *ops, size_t len)
  917. {
  918. switch(ops->mode) {
  919. case MTD_OOB_PLACE:
  920. case MTD_OOB_RAW:
  921. memcpy(oob, chip->oob_poi + ops->ooboffs, len);
  922. return oob + len;
  923. case MTD_OOB_AUTO: {
  924. struct nand_oobfree *free = chip->ecc.layout->oobfree;
  925. uint32_t boffs = 0, roffs = ops->ooboffs;
  926. size_t bytes = 0;
  927. for(; free->length && len; free++, len -= bytes) {
  928. /* Read request not from offset 0 ? */
  929. if (unlikely(roffs)) {
  930. if (roffs >= free->length) {
  931. roffs -= free->length;
  932. continue;
  933. }
  934. boffs = free->offset + roffs;
  935. bytes = min_t(size_t, len,
  936. (free->length - roffs));
  937. roffs = 0;
  938. } else {
  939. bytes = min_t(size_t, len, free->length);
  940. boffs = free->offset;
  941. }
  942. memcpy(oob, chip->oob_poi + boffs, bytes);
  943. oob += bytes;
  944. }
  945. return oob;
  946. }
  947. default:
  948. BUG();
  949. }
  950. return NULL;
  951. }
  952. /**
  953. * nand_do_read_ops - [Internal] Read data with ECC
  954. *
  955. * @mtd: MTD device structure
  956. * @from: offset to read from
  957. * @ops: oob ops structure
  958. *
  959. * Internal function. Called with chip held.
  960. */
  961. static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
  962. struct mtd_oob_ops *ops)
  963. {
  964. int chipnr, page, realpage, col, bytes, aligned;
  965. struct nand_chip *chip = mtd->priv;
  966. struct mtd_ecc_stats stats;
  967. int blkcheck = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
  968. int sndcmd = 1;
  969. int ret = 0;
  970. uint32_t readlen = ops->len;
  971. uint32_t oobreadlen = ops->ooblen;
  972. uint8_t *bufpoi, *oob, *buf;
  973. stats = mtd->ecc_stats;
  974. chipnr = (int)(from >> chip->chip_shift);
  975. chip->select_chip(mtd, chipnr);
  976. realpage = (int)(from >> chip->page_shift);
  977. page = realpage & chip->pagemask;
  978. col = (int)(from & (mtd->writesize - 1));
  979. buf = ops->datbuf;
  980. oob = ops->oobbuf;
  981. while(1) {
  982. bytes = min(mtd->writesize - col, readlen);
  983. aligned = (bytes == mtd->writesize);
  984. /* Is the current page in the buffer ? */
  985. if (realpage != chip->pagebuf || oob) {
  986. bufpoi = aligned ? buf : chip->buffers->databuf;
  987. if (likely(sndcmd)) {
  988. chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
  989. sndcmd = 0;
  990. }
  991. /* Now read the page into the buffer */
  992. if (unlikely(ops->mode == MTD_OOB_RAW))
  993. ret = chip->ecc.read_page_raw(mtd, chip, bufpoi);
  994. else if (!aligned && NAND_SUBPAGE_READ(chip) && !oob)
  995. ret = chip->ecc.read_subpage(mtd, chip, col, bytes, bufpoi);
  996. else
  997. ret = chip->ecc.read_page(mtd, chip, bufpoi);
  998. if (ret < 0)
  999. break;
  1000. /* Transfer not aligned data */
  1001. if (!aligned) {
  1002. if (!NAND_SUBPAGE_READ(chip) && !oob)
  1003. chip->pagebuf = realpage;
  1004. memcpy(buf, chip->buffers->databuf + col, bytes);
  1005. }
  1006. buf += bytes;
  1007. if (unlikely(oob)) {
  1008. /* Raw mode does data:oob:data:oob */
  1009. if (ops->mode != MTD_OOB_RAW) {
  1010. int toread = min(oobreadlen,
  1011. chip->ecc.layout->oobavail);
  1012. if (toread) {
  1013. oob = nand_transfer_oob(chip,
  1014. oob, ops, toread);
  1015. oobreadlen -= toread;
  1016. }
  1017. } else
  1018. buf = nand_transfer_oob(chip,
  1019. buf, ops, mtd->oobsize);
  1020. }
  1021. if (!(chip->options & NAND_NO_READRDY)) {
  1022. /*
  1023. * Apply delay or wait for ready/busy pin. Do
  1024. * this before the AUTOINCR check, so no
  1025. * problems arise if a chip which does auto
  1026. * increment is marked as NOAUTOINCR by the
  1027. * board driver.
  1028. */
  1029. if (!chip->dev_ready)
  1030. udelay(chip->chip_delay);
  1031. else
  1032. nand_wait_ready(mtd);
  1033. }
  1034. } else {
  1035. memcpy(buf, chip->buffers->databuf + col, bytes);
  1036. buf += bytes;
  1037. }
  1038. readlen -= bytes;
  1039. if (!readlen)
  1040. break;
  1041. /* For subsequent reads align to page boundary. */
  1042. col = 0;
  1043. /* Increment page address */
  1044. realpage++;
  1045. page = realpage & chip->pagemask;
  1046. /* Check, if we cross a chip boundary */
  1047. if (!page) {
  1048. chipnr++;
  1049. chip->select_chip(mtd, -1);
  1050. chip->select_chip(mtd, chipnr);
  1051. }
  1052. /* Check, if the chip supports auto page increment
  1053. * or if we have hit a block boundary.
  1054. */
  1055. if (!NAND_CANAUTOINCR(chip) || !(page & blkcheck))
  1056. sndcmd = 1;
  1057. }
  1058. ops->retlen = ops->len - (size_t) readlen;
  1059. if (oob)
  1060. ops->oobretlen = ops->ooblen - oobreadlen;
  1061. if (ret)
  1062. return ret;
  1063. if (mtd->ecc_stats.failed - stats.failed)
  1064. return -EBADMSG;
  1065. return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
  1066. }
  1067. /**
  1068. * nand_read - [MTD Interface] MTD compability function for nand_do_read_ecc
  1069. * @mtd: MTD device structure
  1070. * @from: offset to read from
  1071. * @len: number of bytes to read
  1072. * @retlen: pointer to variable to store the number of read bytes
  1073. * @buf: the databuffer to put data
  1074. *
  1075. * Get hold of the chip and call nand_do_read
  1076. */
  1077. static int nand_read(struct mtd_info *mtd, loff_t from, size_t len,
  1078. size_t *retlen, uint8_t *buf)
  1079. {
  1080. struct nand_chip *chip = mtd->priv;
  1081. int ret;
  1082. /* Do not allow reads past end of device */
  1083. if ((from + len) > mtd->size)
  1084. return -EINVAL;
  1085. if (!len)
  1086. return 0;
  1087. nand_get_device(chip, mtd, FL_READING);
  1088. chip->ops.len = len;
  1089. chip->ops.datbuf = buf;
  1090. chip->ops.oobbuf = NULL;
  1091. ret = nand_do_read_ops(mtd, from, &chip->ops);
  1092. *retlen = chip->ops.retlen;
  1093. nand_release_device(mtd);
  1094. return ret;
  1095. }
  1096. /**
  1097. * nand_read_oob_std - [REPLACABLE] the most common OOB data read function
  1098. * @mtd: mtd info structure
  1099. * @chip: nand chip info structure
  1100. * @page: page number to read
  1101. * @sndcmd: flag whether to issue read command or not
  1102. */
  1103. static int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
  1104. int page, int sndcmd)
  1105. {
  1106. if (sndcmd) {
  1107. chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
  1108. sndcmd = 0;
  1109. }
  1110. chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
  1111. return sndcmd;
  1112. }
  1113. /**
  1114. * nand_read_oob_syndrome - [REPLACABLE] OOB data read function for HW ECC
  1115. * with syndromes
  1116. * @mtd: mtd info structure
  1117. * @chip: nand chip info structure
  1118. * @page: page number to read
  1119. * @sndcmd: flag whether to issue read command or not
  1120. */
  1121. static int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
  1122. int page, int sndcmd)
  1123. {
  1124. uint8_t *buf = chip->oob_poi;
  1125. int length = mtd->oobsize;
  1126. int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
  1127. int eccsize = chip->ecc.size;
  1128. uint8_t *bufpoi = buf;
  1129. int i, toread, sndrnd = 0, pos;
  1130. chip->cmdfunc(mtd, NAND_CMD_READ0, chip->ecc.size, page);
  1131. for (i = 0; i < chip->ecc.steps; i++) {
  1132. if (sndrnd) {
  1133. pos = eccsize + i * (eccsize + chunk);
  1134. if (mtd->writesize > 512)
  1135. chip->cmdfunc(mtd, NAND_CMD_RNDOUT, pos, -1);
  1136. else
  1137. chip->cmdfunc(mtd, NAND_CMD_READ0, pos, page);
  1138. } else
  1139. sndrnd = 1;
  1140. toread = min_t(int, length, chunk);
  1141. chip->read_buf(mtd, bufpoi, toread);
  1142. bufpoi += toread;
  1143. length -= toread;
  1144. }
  1145. if (length > 0)
  1146. chip->read_buf(mtd, bufpoi, length);
  1147. return 1;
  1148. }
  1149. /**
  1150. * nand_write_oob_std - [REPLACABLE] the most common OOB data write function
  1151. * @mtd: mtd info structure
  1152. * @chip: nand chip info structure
  1153. * @page: page number to write
  1154. */
  1155. static int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
  1156. int page)
  1157. {
  1158. int status = 0;
  1159. const uint8_t *buf = chip->oob_poi;
  1160. int length = mtd->oobsize;
  1161. chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
  1162. chip->write_buf(mtd, buf, length);
  1163. /* Send command to program the OOB data */
  1164. chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
  1165. status = chip->waitfunc(mtd, chip);
  1166. return status & NAND_STATUS_FAIL ? -EIO : 0;
  1167. }
  1168. /**
  1169. * nand_write_oob_syndrome - [REPLACABLE] OOB data write function for HW ECC
  1170. * with syndrome - only for large page flash !
  1171. * @mtd: mtd info structure
  1172. * @chip: nand chip info structure
  1173. * @page: page number to write
  1174. */
  1175. static int nand_write_oob_syndrome(struct mtd_info *mtd,
  1176. struct nand_chip *chip, int page)
  1177. {
  1178. int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
  1179. int eccsize = chip->ecc.size, length = mtd->oobsize;
  1180. int i, len, pos, status = 0, sndcmd = 0, steps = chip->ecc.steps;
  1181. const uint8_t *bufpoi = chip->oob_poi;
  1182. /*
  1183. * data-ecc-data-ecc ... ecc-oob
  1184. * or
  1185. * data-pad-ecc-pad-data-pad .... ecc-pad-oob
  1186. */
  1187. if (!chip->ecc.prepad && !chip->ecc.postpad) {
  1188. pos = steps * (eccsize + chunk);
  1189. steps = 0;
  1190. } else
  1191. pos = eccsize;
  1192. chip->cmdfunc(mtd, NAND_CMD_SEQIN, pos, page);
  1193. for (i = 0; i < steps; i++) {
  1194. if (sndcmd) {
  1195. if (mtd->writesize <= 512) {
  1196. uint32_t fill = 0xFFFFFFFF;
  1197. len = eccsize;
  1198. while (len > 0) {
  1199. int num = min_t(int, len, 4);
  1200. chip->write_buf(mtd, (uint8_t *)&fill,
  1201. num);
  1202. len -= num;
  1203. }
  1204. } else {
  1205. pos = eccsize + i * (eccsize + chunk);
  1206. chip->cmdfunc(mtd, NAND_CMD_RNDIN, pos, -1);
  1207. }
  1208. } else
  1209. sndcmd = 1;
  1210. len = min_t(int, length, chunk);
  1211. chip->write_buf(mtd, bufpoi, len);
  1212. bufpoi += len;
  1213. length -= len;
  1214. }
  1215. if (length > 0)
  1216. chip->write_buf(mtd, bufpoi, length);
  1217. chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
  1218. status = chip->waitfunc(mtd, chip);
  1219. return status & NAND_STATUS_FAIL ? -EIO : 0;
  1220. }
  1221. /**
  1222. * nand_do_read_oob - [Intern] NAND read out-of-band
  1223. * @mtd: MTD device structure
  1224. * @from: offset to read from
  1225. * @ops: oob operations description structure
  1226. *
  1227. * NAND read out-of-band data from the spare area
  1228. */
  1229. static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
  1230. struct mtd_oob_ops *ops)
  1231. {
  1232. int page, realpage, chipnr, sndcmd = 1;
  1233. struct nand_chip *chip = mtd->priv;
  1234. int blkcheck = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
  1235. int readlen = ops->ooblen;
  1236. int len;
  1237. uint8_t *buf = ops->oobbuf;
  1238. DEBUG(MTD_DEBUG_LEVEL3, "nand_read_oob: from = 0x%08Lx, len = %i\n",
  1239. (unsigned long long)from, readlen);
  1240. if (ops->mode == MTD_OOB_AUTO)
  1241. len = chip->ecc.layout->oobavail;
  1242. else
  1243. len = mtd->oobsize;
  1244. if (unlikely(ops->ooboffs >= len)) {
  1245. DEBUG(MTD_DEBUG_LEVEL0, "nand_read_oob: "
  1246. "Attempt to start read outside oob\n");
  1247. return -EINVAL;
  1248. }
  1249. /* Do not allow reads past end of device */
  1250. if (unlikely(from >= mtd->size ||
  1251. ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) -
  1252. (from >> chip->page_shift)) * len)) {
  1253. DEBUG(MTD_DEBUG_LEVEL0, "nand_read_oob: "
  1254. "Attempt read beyond end of device\n");
  1255. return -EINVAL;
  1256. }
  1257. chipnr = (int)(from >> chip->chip_shift);
  1258. chip->select_chip(mtd, chipnr);
  1259. /* Shift to get page */
  1260. realpage = (int)(from >> chip->page_shift);
  1261. page = realpage & chip->pagemask;
  1262. while(1) {
  1263. sndcmd = chip->ecc.read_oob(mtd, chip, page, sndcmd);
  1264. len = min(len, readlen);
  1265. buf = nand_transfer_oob(chip, buf, ops, len);
  1266. if (!(chip->options & NAND_NO_READRDY)) {
  1267. /*
  1268. * Apply delay or wait for ready/busy pin. Do this
  1269. * before the AUTOINCR check, so no problems arise if a
  1270. * chip which does auto increment is marked as
  1271. * NOAUTOINCR by the board driver.
  1272. */
  1273. if (!chip->dev_ready)
  1274. udelay(chip->chip_delay);
  1275. else
  1276. nand_wait_ready(mtd);
  1277. }
  1278. readlen -= len;
  1279. if (!readlen)
  1280. break;
  1281. /* Increment page address */
  1282. realpage++;
  1283. page = realpage & chip->pagemask;
  1284. /* Check, if we cross a chip boundary */
  1285. if (!page) {
  1286. chipnr++;
  1287. chip->select_chip(mtd, -1);
  1288. chip->select_chip(mtd, chipnr);
  1289. }
  1290. /* Check, if the chip supports auto page increment
  1291. * or if we have hit a block boundary.
  1292. */
  1293. if (!NAND_CANAUTOINCR(chip) || !(page & blkcheck))
  1294. sndcmd = 1;
  1295. }
  1296. ops->oobretlen = ops->ooblen;
  1297. return 0;
  1298. }
  1299. /**
  1300. * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
  1301. * @mtd: MTD device structure
  1302. * @from: offset to read from
  1303. * @ops: oob operation description structure
  1304. *
  1305. * NAND read data and/or out-of-band data
  1306. */
  1307. static int nand_read_oob(struct mtd_info *mtd, loff_t from,
  1308. struct mtd_oob_ops *ops)
  1309. {
  1310. struct nand_chip *chip = mtd->priv;
  1311. int ret = -ENOTSUPP;
  1312. ops->retlen = 0;
  1313. /* Do not allow reads past end of device */
  1314. if (ops->datbuf && (from + ops->len) > mtd->size) {
  1315. DEBUG(MTD_DEBUG_LEVEL0, "nand_read_oob: "
  1316. "Attempt read beyond end of device\n");
  1317. return -EINVAL;
  1318. }
  1319. nand_get_device(chip, mtd, FL_READING);
  1320. switch(ops->mode) {
  1321. case MTD_OOB_PLACE:
  1322. case MTD_OOB_AUTO:
  1323. case MTD_OOB_RAW:
  1324. break;
  1325. default:
  1326. goto out;
  1327. }
  1328. if (!ops->datbuf)
  1329. ret = nand_do_read_oob(mtd, from, ops);
  1330. else
  1331. ret = nand_do_read_ops(mtd, from, ops);
  1332. out:
  1333. nand_release_device(mtd);
  1334. return ret;
  1335. }
  1336. /**
  1337. * nand_write_page_raw - [Intern] raw page write function
  1338. * @mtd: mtd info structure
  1339. * @chip: nand chip info structure
  1340. * @buf: data buffer
  1341. *
  1342. * Not for syndrome calculating ecc controllers, which use a special oob layout
  1343. */
  1344. static void nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
  1345. const uint8_t *buf)
  1346. {
  1347. chip->write_buf(mtd, buf, mtd->writesize);
  1348. chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
  1349. }
  1350. /**
  1351. * nand_write_page_raw_syndrome - [Intern] raw page write function
  1352. * @mtd: mtd info structure
  1353. * @chip: nand chip info structure
  1354. * @buf: data buffer
  1355. *
  1356. * We need a special oob layout and handling even when ECC isn't checked.
  1357. */
  1358. static void nand_write_page_raw_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
  1359. const uint8_t *buf)
  1360. {
  1361. int eccsize = chip->ecc.size;
  1362. int eccbytes = chip->ecc.bytes;
  1363. uint8_t *oob = chip->oob_poi;
  1364. int steps, size;
  1365. for (steps = chip->ecc.steps; steps > 0; steps--) {
  1366. chip->write_buf(mtd, buf, eccsize);
  1367. buf += eccsize;
  1368. if (chip->ecc.prepad) {
  1369. chip->write_buf(mtd, oob, chip->ecc.prepad);
  1370. oob += chip->ecc.prepad;
  1371. }
  1372. chip->read_buf(mtd, oob, eccbytes);
  1373. oob += eccbytes;
  1374. if (chip->ecc.postpad) {
  1375. chip->write_buf(mtd, oob, chip->ecc.postpad);
  1376. oob += chip->ecc.postpad;
  1377. }
  1378. }
  1379. size = mtd->oobsize - (oob - chip->oob_poi);
  1380. if (size)
  1381. chip->write_buf(mtd, oob, size);
  1382. }
  1383. /**
  1384. * nand_write_page_swecc - [REPLACABLE] software ecc based page write function
  1385. * @mtd: mtd info structure
  1386. * @chip: nand chip info structure
  1387. * @buf: data buffer
  1388. */
  1389. static void nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
  1390. const uint8_t *buf)
  1391. {
  1392. int i, eccsize = chip->ecc.size;
  1393. int eccbytes = chip->ecc.bytes;
  1394. int eccsteps = chip->ecc.steps;
  1395. uint8_t *ecc_calc = chip->buffers->ecccalc;
  1396. const uint8_t *p = buf;
  1397. uint32_t *eccpos = chip->ecc.layout->eccpos;
  1398. /* Software ecc calculation */
  1399. for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
  1400. chip->ecc.calculate(mtd, p, &ecc_calc[i]);
  1401. for (i = 0; i < chip->ecc.total; i++)
  1402. chip->oob_poi[eccpos[i]] = ecc_calc[i];
  1403. chip->ecc.write_page_raw(mtd, chip, buf);
  1404. }
  1405. /**
  1406. * nand_write_page_hwecc - [REPLACABLE] hardware ecc based page write function
  1407. * @mtd: mtd info structure
  1408. * @chip: nand chip info structure
  1409. * @buf: data buffer
  1410. */
  1411. static void nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
  1412. const uint8_t *buf)
  1413. {
  1414. int i, eccsize = chip->ecc.size;
  1415. int eccbytes = chip->ecc.bytes;
  1416. int eccsteps = chip->ecc.steps;
  1417. uint8_t *ecc_calc = chip->buffers->ecccalc;
  1418. const uint8_t *p = buf;
  1419. uint32_t *eccpos = chip->ecc.layout->eccpos;
  1420. for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
  1421. chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
  1422. chip->write_buf(mtd, p, eccsize);
  1423. chip->ecc.calculate(mtd, p, &ecc_calc[i]);
  1424. }
  1425. for (i = 0; i < chip->ecc.total; i++)
  1426. chip->oob_poi[eccpos[i]] = ecc_calc[i];
  1427. chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
  1428. }
  1429. /**
  1430. * nand_write_page_syndrome - [REPLACABLE] hardware ecc syndrom based page write
  1431. * @mtd: mtd info structure
  1432. * @chip: nand chip info structure
  1433. * @buf: data buffer
  1434. *
  1435. * The hw generator calculates the error syndrome automatically. Therefor
  1436. * we need a special oob layout and handling.
  1437. */
  1438. static void nand_write_page_syndrome(struct mtd_info *mtd,
  1439. struct nand_chip *chip, 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. const uint8_t *p = buf;
  1445. uint8_t *oob = chip->oob_poi;
  1446. for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
  1447. chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
  1448. chip->write_buf(mtd, p, eccsize);
  1449. if (chip->ecc.prepad) {
  1450. chip->write_buf(mtd, oob, chip->ecc.prepad);
  1451. oob += chip->ecc.prepad;
  1452. }
  1453. chip->ecc.calculate(mtd, p, oob);
  1454. chip->write_buf(mtd, oob, eccbytes);
  1455. oob += eccbytes;
  1456. if (chip->ecc.postpad) {
  1457. chip->write_buf(mtd, oob, chip->ecc.postpad);
  1458. oob += chip->ecc.postpad;
  1459. }
  1460. }
  1461. /* Calculate remaining oob bytes */
  1462. i = mtd->oobsize - (oob - chip->oob_poi);
  1463. if (i)
  1464. chip->write_buf(mtd, oob, i);
  1465. }
  1466. /**
  1467. * nand_write_page - [REPLACEABLE] write one page
  1468. * @mtd: MTD device structure
  1469. * @chip: NAND chip descriptor
  1470. * @buf: the data to write
  1471. * @page: page number to write
  1472. * @cached: cached programming
  1473. * @raw: use _raw version of write_page
  1474. */
  1475. static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
  1476. const uint8_t *buf, int page, int cached, int raw)
  1477. {
  1478. int status;
  1479. chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
  1480. if (unlikely(raw))
  1481. chip->ecc.write_page_raw(mtd, chip, buf);
  1482. else
  1483. chip->ecc.write_page(mtd, chip, buf);
  1484. /*
  1485. * Cached progamming disabled for now, Not sure if its worth the
  1486. * trouble. The speed gain is not very impressive. (2.3->2.6Mib/s)
  1487. */
  1488. cached = 0;
  1489. if (!cached || !(chip->options & NAND_CACHEPRG)) {
  1490. chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
  1491. status = chip->waitfunc(mtd, chip);
  1492. /*
  1493. * See if operation failed and additional status checks are
  1494. * available
  1495. */
  1496. if ((status & NAND_STATUS_FAIL) && (chip->errstat))
  1497. status = chip->errstat(mtd, chip, FL_WRITING, status,
  1498. page);
  1499. if (status & NAND_STATUS_FAIL)
  1500. return -EIO;
  1501. } else {
  1502. chip->cmdfunc(mtd, NAND_CMD_CACHEDPROG, -1, -1);
  1503. status = chip->waitfunc(mtd, chip);
  1504. }
  1505. #ifdef CONFIG_MTD_NAND_VERIFY_WRITE
  1506. /* Send command to read back the data */
  1507. chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
  1508. if (chip->verify_buf(mtd, buf, mtd->writesize))
  1509. return -EIO;
  1510. #endif
  1511. return 0;
  1512. }
  1513. /**
  1514. * nand_fill_oob - [Internal] Transfer client buffer to oob
  1515. * @chip: nand chip structure
  1516. * @oob: oob data buffer
  1517. * @ops: oob ops structure
  1518. */
  1519. static uint8_t *nand_fill_oob(struct nand_chip *chip, uint8_t *oob,
  1520. struct mtd_oob_ops *ops)
  1521. {
  1522. size_t len = ops->ooblen;
  1523. switch(ops->mode) {
  1524. case MTD_OOB_PLACE:
  1525. case MTD_OOB_RAW:
  1526. memcpy(chip->oob_poi + ops->ooboffs, oob, len);
  1527. return oob + len;
  1528. case MTD_OOB_AUTO: {
  1529. struct nand_oobfree *free = chip->ecc.layout->oobfree;
  1530. uint32_t boffs = 0, woffs = ops->ooboffs;
  1531. size_t bytes = 0;
  1532. for(; free->length && len; free++, len -= bytes) {
  1533. /* Write request not from offset 0 ? */
  1534. if (unlikely(woffs)) {
  1535. if (woffs >= free->length) {
  1536. woffs -= free->length;
  1537. continue;
  1538. }
  1539. boffs = free->offset + woffs;
  1540. bytes = min_t(size_t, len,
  1541. (free->length - woffs));
  1542. woffs = 0;
  1543. } else {
  1544. bytes = min_t(size_t, len, free->length);
  1545. boffs = free->offset;
  1546. }
  1547. memcpy(chip->oob_poi + boffs, oob, bytes);
  1548. oob += bytes;
  1549. }
  1550. return oob;
  1551. }
  1552. default:
  1553. BUG();
  1554. }
  1555. return NULL;
  1556. }
  1557. #define NOTALIGNED(x) (x & (chip->subpagesize - 1)) != 0
  1558. /**
  1559. * nand_do_write_ops - [Internal] NAND write with ECC
  1560. * @mtd: MTD device structure
  1561. * @to: offset to write to
  1562. * @ops: oob operations description structure
  1563. *
  1564. * NAND write with ECC
  1565. */
  1566. static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
  1567. struct mtd_oob_ops *ops)
  1568. {
  1569. int chipnr, realpage, page, blockmask, column;
  1570. struct nand_chip *chip = mtd->priv;
  1571. uint32_t writelen = ops->len;
  1572. uint8_t *oob = ops->oobbuf;
  1573. uint8_t *buf = ops->datbuf;
  1574. int ret, subpage;
  1575. ops->retlen = 0;
  1576. if (!writelen)
  1577. return 0;
  1578. /* reject writes, which are not page aligned */
  1579. if (NOTALIGNED(to) || NOTALIGNED(ops->len)) {
  1580. printk(KERN_NOTICE "nand_write: "
  1581. "Attempt to write not page aligned data\n");
  1582. return -EINVAL;
  1583. }
  1584. column = to & (mtd->writesize - 1);
  1585. subpage = column || (writelen & (mtd->writesize - 1));
  1586. if (subpage && oob)
  1587. return -EINVAL;
  1588. chipnr = (int)(to >> chip->chip_shift);
  1589. chip->select_chip(mtd, chipnr);
  1590. /* Check, if it is write protected */
  1591. if (nand_check_wp(mtd))
  1592. return -EIO;
  1593. realpage = (int)(to >> chip->page_shift);
  1594. page = realpage & chip->pagemask;
  1595. blockmask = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
  1596. /* Invalidate the page cache, when we write to the cached page */
  1597. if (to <= (chip->pagebuf << chip->page_shift) &&
  1598. (chip->pagebuf << chip->page_shift) < (to + ops->len))
  1599. chip->pagebuf = -1;
  1600. /* If we're not given explicit OOB data, let it be 0xFF */
  1601. if (likely(!oob))
  1602. memset(chip->oob_poi, 0xff, mtd->oobsize);
  1603. while(1) {
  1604. int bytes = mtd->writesize;
  1605. int cached = writelen > bytes && page != blockmask;
  1606. uint8_t *wbuf = buf;
  1607. /* Partial page write ? */
  1608. if (unlikely(column || writelen < (mtd->writesize - 1))) {
  1609. cached = 0;
  1610. bytes = min_t(int, bytes - column, (int) writelen);
  1611. chip->pagebuf = -1;
  1612. memset(chip->buffers->databuf, 0xff, mtd->writesize);
  1613. memcpy(&chip->buffers->databuf[column], buf, bytes);
  1614. wbuf = chip->buffers->databuf;
  1615. }
  1616. if (unlikely(oob))
  1617. oob = nand_fill_oob(chip, oob, ops);
  1618. ret = chip->write_page(mtd, chip, wbuf, page, cached,
  1619. (ops->mode == MTD_OOB_RAW));
  1620. if (ret)
  1621. break;
  1622. writelen -= bytes;
  1623. if (!writelen)
  1624. break;
  1625. column = 0;
  1626. buf += bytes;
  1627. realpage++;
  1628. page = realpage & chip->pagemask;
  1629. /* Check, if we cross a chip boundary */
  1630. if (!page) {
  1631. chipnr++;
  1632. chip->select_chip(mtd, -1);
  1633. chip->select_chip(mtd, chipnr);
  1634. }
  1635. }
  1636. ops->retlen = ops->len - writelen;
  1637. if (unlikely(oob))
  1638. ops->oobretlen = ops->ooblen;
  1639. return ret;
  1640. }
  1641. /**
  1642. * nand_write - [MTD Interface] NAND write with ECC
  1643. * @mtd: MTD device structure
  1644. * @to: offset to write to
  1645. * @len: number of bytes to write
  1646. * @retlen: pointer to variable to store the number of written bytes
  1647. * @buf: the data to write
  1648. *
  1649. * NAND write with ECC
  1650. */
  1651. static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
  1652. size_t *retlen, const uint8_t *buf)
  1653. {
  1654. struct nand_chip *chip = mtd->priv;
  1655. int ret;
  1656. /* Do not allow reads past end of device */
  1657. if ((to + len) > mtd->size)
  1658. return -EINVAL;
  1659. if (!len)
  1660. return 0;
  1661. nand_get_device(chip, mtd, FL_WRITING);
  1662. chip->ops.len = len;
  1663. chip->ops.datbuf = (uint8_t *)buf;
  1664. chip->ops.oobbuf = NULL;
  1665. ret = nand_do_write_ops(mtd, to, &chip->ops);
  1666. *retlen = chip->ops.retlen;
  1667. nand_release_device(mtd);
  1668. return ret;
  1669. }
  1670. /**
  1671. * nand_do_write_oob - [MTD Interface] NAND write out-of-band
  1672. * @mtd: MTD device structure
  1673. * @to: offset to write to
  1674. * @ops: oob operation description structure
  1675. *
  1676. * NAND write out-of-band
  1677. */
  1678. static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
  1679. struct mtd_oob_ops *ops)
  1680. {
  1681. int chipnr, page, status, len;
  1682. struct nand_chip *chip = mtd->priv;
  1683. DEBUG(MTD_DEBUG_LEVEL3, "nand_write_oob: to = 0x%08x, len = %i\n",
  1684. (unsigned int)to, (int)ops->ooblen);
  1685. if (ops->mode == MTD_OOB_AUTO)
  1686. len = chip->ecc.layout->oobavail;
  1687. else
  1688. len = mtd->oobsize;
  1689. /* Do not allow write past end of page */
  1690. if ((ops->ooboffs + ops->ooblen) > len) {
  1691. DEBUG(MTD_DEBUG_LEVEL0, "nand_write_oob: "
  1692. "Attempt to write past end of page\n");
  1693. return -EINVAL;
  1694. }
  1695. if (unlikely(ops->ooboffs >= len)) {
  1696. DEBUG(MTD_DEBUG_LEVEL0, "nand_do_write_oob: "
  1697. "Attempt to start write outside oob\n");
  1698. return -EINVAL;
  1699. }
  1700. /* Do not allow reads past end of device */
  1701. if (unlikely(to >= mtd->size ||
  1702. ops->ooboffs + ops->ooblen >
  1703. ((mtd->size >> chip->page_shift) -
  1704. (to >> chip->page_shift)) * len)) {
  1705. DEBUG(MTD_DEBUG_LEVEL0, "nand_do_write_oob: "
  1706. "Attempt write beyond end of device\n");
  1707. return -EINVAL;
  1708. }
  1709. chipnr = (int)(to >> chip->chip_shift);
  1710. chip->select_chip(mtd, chipnr);
  1711. /* Shift to get page */
  1712. page = (int)(to >> chip->page_shift);
  1713. /*
  1714. * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
  1715. * of my DiskOnChip 2000 test units) will clear the whole data page too
  1716. * if we don't do this. I have no clue why, but I seem to have 'fixed'
  1717. * it in the doc2000 driver in August 1999. dwmw2.
  1718. */
  1719. chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
  1720. /* Check, if it is write protected */
  1721. if (nand_check_wp(mtd))
  1722. return -EROFS;
  1723. /* Invalidate the page cache, if we write to the cached page */
  1724. if (page == chip->pagebuf)
  1725. chip->pagebuf = -1;
  1726. memset(chip->oob_poi, 0xff, mtd->oobsize);
  1727. nand_fill_oob(chip, ops->oobbuf, ops);
  1728. status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
  1729. memset(chip->oob_poi, 0xff, mtd->oobsize);
  1730. if (status)
  1731. return status;
  1732. ops->oobretlen = ops->ooblen;
  1733. return 0;
  1734. }
  1735. /**
  1736. * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
  1737. * @mtd: MTD device structure
  1738. * @to: offset to write to
  1739. * @ops: oob operation description structure
  1740. */
  1741. static int nand_write_oob(struct mtd_info *mtd, loff_t to,
  1742. struct mtd_oob_ops *ops)
  1743. {
  1744. struct nand_chip *chip = mtd->priv;
  1745. int ret = -ENOTSUPP;
  1746. ops->retlen = 0;
  1747. /* Do not allow writes past end of device */
  1748. if (ops->datbuf && (to + ops->len) > mtd->size) {
  1749. DEBUG(MTD_DEBUG_LEVEL0, "nand_write_oob: "
  1750. "Attempt write beyond end of device\n");
  1751. return -EINVAL;
  1752. }
  1753. nand_get_device(chip, mtd, FL_WRITING);
  1754. switch(ops->mode) {
  1755. case MTD_OOB_PLACE:
  1756. case MTD_OOB_AUTO:
  1757. case MTD_OOB_RAW:
  1758. break;
  1759. default:
  1760. goto out;
  1761. }
  1762. if (!ops->datbuf)
  1763. ret = nand_do_write_oob(mtd, to, ops);
  1764. else
  1765. ret = nand_do_write_ops(mtd, to, ops);
  1766. out:
  1767. nand_release_device(mtd);
  1768. return ret;
  1769. }
  1770. /**
  1771. * single_erease_cmd - [GENERIC] NAND standard block erase command function
  1772. * @mtd: MTD device structure
  1773. * @page: the page address of the block which will be erased
  1774. *
  1775. * Standard erase command for NAND chips
  1776. */
  1777. static void single_erase_cmd(struct mtd_info *mtd, int page)
  1778. {
  1779. struct nand_chip *chip = mtd->priv;
  1780. /* Send commands to erase a block */
  1781. chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
  1782. chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
  1783. }
  1784. /**
  1785. * multi_erease_cmd - [GENERIC] AND specific block erase command function
  1786. * @mtd: MTD device structure
  1787. * @page: the page address of the block which will be erased
  1788. *
  1789. * AND multi block erase command function
  1790. * Erase 4 consecutive blocks
  1791. */
  1792. static void multi_erase_cmd(struct mtd_info *mtd, int page)
  1793. {
  1794. struct nand_chip *chip = mtd->priv;
  1795. /* Send commands to erase a block */
  1796. chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
  1797. chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
  1798. chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
  1799. chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
  1800. chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
  1801. }
  1802. /**
  1803. * nand_erase - [MTD Interface] erase block(s)
  1804. * @mtd: MTD device structure
  1805. * @instr: erase instruction
  1806. *
  1807. * Erase one ore more blocks
  1808. */
  1809. static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
  1810. {
  1811. return nand_erase_nand(mtd, instr, 0);
  1812. }
  1813. #define BBT_PAGE_MASK 0xffffff3f
  1814. /**
  1815. * nand_erase_nand - [Internal] erase block(s)
  1816. * @mtd: MTD device structure
  1817. * @instr: erase instruction
  1818. * @allowbbt: allow erasing the bbt area
  1819. *
  1820. * Erase one ore more blocks
  1821. */
  1822. int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
  1823. int allowbbt)
  1824. {
  1825. int page, status, pages_per_block, ret, chipnr;
  1826. struct nand_chip *chip = mtd->priv;
  1827. loff_t rewrite_bbt[NAND_MAX_CHIPS]={0};
  1828. unsigned int bbt_masked_page = 0xffffffff;
  1829. loff_t len;
  1830. DEBUG(MTD_DEBUG_LEVEL3, "nand_erase: start = 0x%012llx, len = %llu\n",
  1831. (unsigned long long)instr->addr, (unsigned long long)instr->len);
  1832. /* Start address must align on block boundary */
  1833. if (instr->addr & ((1 << chip->phys_erase_shift) - 1)) {
  1834. DEBUG(MTD_DEBUG_LEVEL0, "nand_erase: Unaligned address\n");
  1835. return -EINVAL;
  1836. }
  1837. /* Length must align on block boundary */
  1838. if (instr->len & ((1 << chip->phys_erase_shift) - 1)) {
  1839. DEBUG(MTD_DEBUG_LEVEL0, "nand_erase: "
  1840. "Length not block aligned\n");
  1841. return -EINVAL;
  1842. }
  1843. /* Do not allow erase past end of device */
  1844. if ((instr->len + instr->addr) > mtd->size) {
  1845. DEBUG(MTD_DEBUG_LEVEL0, "nand_erase: "
  1846. "Erase past end of device\n");
  1847. return -EINVAL;
  1848. }
  1849. instr->fail_addr = MTD_FAIL_ADDR_UNKNOWN;
  1850. /* Grab the lock and see if the device is available */
  1851. nand_get_device(chip, mtd, FL_ERASING);
  1852. /* Shift to get first page */
  1853. page = (int)(instr->addr >> chip->page_shift);
  1854. chipnr = (int)(instr->addr >> chip->chip_shift);
  1855. /* Calculate pages in each block */
  1856. pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
  1857. /* Select the NAND device */
  1858. chip->select_chip(mtd, chipnr);
  1859. /* Check, if it is write protected */
  1860. if (nand_check_wp(mtd)) {
  1861. DEBUG(MTD_DEBUG_LEVEL0, "nand_erase: "
  1862. "Device is write protected!!!\n");
  1863. instr->state = MTD_ERASE_FAILED;
  1864. goto erase_exit;
  1865. }
  1866. /*
  1867. * If BBT requires refresh, set the BBT page mask to see if the BBT
  1868. * should be rewritten. Otherwise the mask is set to 0xffffffff which
  1869. * can not be matched. This is also done when the bbt is actually
  1870. * erased to avoid recusrsive updates
  1871. */
  1872. if (chip->options & BBT_AUTO_REFRESH && !allowbbt)
  1873. bbt_masked_page = chip->bbt_td->pages[chipnr] & BBT_PAGE_MASK;
  1874. /* Loop through the pages */
  1875. len = instr->len;
  1876. instr->state = MTD_ERASING;
  1877. while (len) {
  1878. /*
  1879. * heck if we have a bad block, we do not erase bad blocks !
  1880. */
  1881. if (nand_block_checkbad(mtd, ((loff_t) page) <<
  1882. chip->page_shift, 0, allowbbt)) {
  1883. printk(KERN_WARNING "nand_erase: attempt to erase a "
  1884. "bad block at page 0x%08x\n", page);
  1885. instr->state = MTD_ERASE_FAILED;
  1886. goto erase_exit;
  1887. }
  1888. /*
  1889. * Invalidate the page cache, if we erase the block which
  1890. * contains the current cached page
  1891. */
  1892. if (page <= chip->pagebuf && chip->pagebuf <
  1893. (page + pages_per_block))
  1894. chip->pagebuf = -1;
  1895. chip->erase_cmd(mtd, page & chip->pagemask);
  1896. status = chip->waitfunc(mtd, chip);
  1897. /*
  1898. * See if operation failed and additional status checks are
  1899. * available
  1900. */
  1901. if ((status & NAND_STATUS_FAIL) && (chip->errstat))
  1902. status = chip->errstat(mtd, chip, FL_ERASING,
  1903. status, page);
  1904. /* See if block erase succeeded */
  1905. if (status & NAND_STATUS_FAIL) {
  1906. DEBUG(MTD_DEBUG_LEVEL0, "nand_erase: "
  1907. "Failed erase, page 0x%08x\n", page);
  1908. instr->state = MTD_ERASE_FAILED;
  1909. instr->fail_addr =
  1910. ((loff_t)page << chip->page_shift);
  1911. goto erase_exit;
  1912. }
  1913. /*
  1914. * If BBT requires refresh, set the BBT rewrite flag to the
  1915. * page being erased
  1916. */
  1917. if (bbt_masked_page != 0xffffffff &&
  1918. (page & BBT_PAGE_MASK) == bbt_masked_page)
  1919. rewrite_bbt[chipnr] =
  1920. ((loff_t)page << chip->page_shift);
  1921. /* Increment page address and decrement length */
  1922. len -= (1 << chip->phys_erase_shift);
  1923. page += pages_per_block;
  1924. /* Check, if we cross a chip boundary */
  1925. if (len && !(page & chip->pagemask)) {
  1926. chipnr++;
  1927. chip->select_chip(mtd, -1);
  1928. chip->select_chip(mtd, chipnr);
  1929. /*
  1930. * If BBT requires refresh and BBT-PERCHIP, set the BBT
  1931. * page mask to see if this BBT should be rewritten
  1932. */
  1933. if (bbt_masked_page != 0xffffffff &&
  1934. (chip->bbt_td->options & NAND_BBT_PERCHIP))
  1935. bbt_masked_page = chip->bbt_td->pages[chipnr] &
  1936. BBT_PAGE_MASK;
  1937. }
  1938. }
  1939. instr->state = MTD_ERASE_DONE;
  1940. erase_exit:
  1941. ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
  1942. /* Deselect and wake up anyone waiting on the device */
  1943. nand_release_device(mtd);
  1944. /* Do call back function */
  1945. if (!ret)
  1946. mtd_erase_callback(instr);
  1947. /*
  1948. * If BBT requires refresh and erase was successful, rewrite any
  1949. * selected bad block tables
  1950. */
  1951. if (bbt_masked_page == 0xffffffff || ret)
  1952. return ret;
  1953. for (chipnr = 0; chipnr < chip->numchips; chipnr++) {
  1954. if (!rewrite_bbt[chipnr])
  1955. continue;
  1956. /* update the BBT for chip */
  1957. DEBUG(MTD_DEBUG_LEVEL0, "nand_erase_nand: nand_update_bbt "
  1958. "(%d:0x%0llx 0x%0x)\n", chipnr, rewrite_bbt[chipnr],
  1959. chip->bbt_td->pages[chipnr]);
  1960. nand_update_bbt(mtd, rewrite_bbt[chipnr]);
  1961. }
  1962. /* Return more or less happy */
  1963. return ret;
  1964. }
  1965. /**
  1966. * nand_sync - [MTD Interface] sync
  1967. * @mtd: MTD device structure
  1968. *
  1969. * Sync is actually a wait for chip ready function
  1970. */
  1971. static void nand_sync(struct mtd_info *mtd)
  1972. {
  1973. struct nand_chip *chip = mtd->priv;
  1974. DEBUG(MTD_DEBUG_LEVEL3, "nand_sync: called\n");
  1975. /* Grab the lock and see if the device is available */
  1976. nand_get_device(chip, mtd, FL_SYNCING);
  1977. /* Release it and go back */
  1978. nand_release_device(mtd);
  1979. }
  1980. /**
  1981. * nand_block_isbad - [MTD Interface] Check if block at offset is bad
  1982. * @mtd: MTD device structure
  1983. * @offs: offset relative to mtd start
  1984. */
  1985. static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
  1986. {
  1987. /* Check for invalid offset */
  1988. if (offs > mtd->size)
  1989. return -EINVAL;
  1990. return nand_block_checkbad(mtd, offs, 1, 0);
  1991. }
  1992. /**
  1993. * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
  1994. * @mtd: MTD device structure
  1995. * @ofs: offset relative to mtd start
  1996. */
  1997. static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
  1998. {
  1999. struct nand_chip *chip = mtd->priv;
  2000. int ret;
  2001. if ((ret = nand_block_isbad(mtd, ofs))) {
  2002. /* If it was bad already, return success and do nothing. */
  2003. if (ret > 0)
  2004. return 0;
  2005. return ret;
  2006. }
  2007. return chip->block_markbad(mtd, ofs);
  2008. }
  2009. /**
  2010. * nand_suspend - [MTD Interface] Suspend the NAND flash
  2011. * @mtd: MTD device structure
  2012. */
  2013. static int nand_suspend(struct mtd_info *mtd)
  2014. {
  2015. struct nand_chip *chip = mtd->priv;
  2016. return nand_get_device(chip, mtd, FL_PM_SUSPENDED);
  2017. }
  2018. /**
  2019. * nand_resume - [MTD Interface] Resume the NAND flash
  2020. * @mtd: MTD device structure
  2021. */
  2022. static void nand_resume(struct mtd_info *mtd)
  2023. {
  2024. struct nand_chip *chip = mtd->priv;
  2025. if (chip->state == FL_PM_SUSPENDED)
  2026. nand_release_device(mtd);
  2027. else
  2028. printk(KERN_ERR "nand_resume() called for a chip which is not "
  2029. "in suspended state\n");
  2030. }
  2031. /*
  2032. * Set default functions
  2033. */
  2034. static void nand_set_defaults(struct nand_chip *chip, int busw)
  2035. {
  2036. /* check for proper chip_delay setup, set 20us if not */
  2037. if (!chip->chip_delay)
  2038. chip->chip_delay = 20;
  2039. /* check, if a user supplied command function given */
  2040. if (chip->cmdfunc == NULL)
  2041. chip->cmdfunc = nand_command;
  2042. /* check, if a user supplied wait function given */
  2043. if (chip->waitfunc == NULL)
  2044. chip->waitfunc = nand_wait;
  2045. if (!chip->select_chip)
  2046. chip->select_chip = nand_select_chip;
  2047. if (!chip->read_byte)
  2048. chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
  2049. if (!chip->read_word)
  2050. chip->read_word = nand_read_word;
  2051. if (!chip->block_bad)
  2052. chip->block_bad = nand_block_bad;
  2053. if (!chip->block_markbad)
  2054. chip->block_markbad = nand_default_block_markbad;
  2055. if (!chip->write_buf)
  2056. chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
  2057. if (!chip->read_buf)
  2058. chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
  2059. if (!chip->verify_buf)
  2060. chip->verify_buf = busw ? nand_verify_buf16 : nand_verify_buf;
  2061. if (!chip->scan_bbt)
  2062. chip->scan_bbt = nand_default_bbt;
  2063. if (!chip->controller) {
  2064. chip->controller = &chip->hwcontrol;
  2065. spin_lock_init(&chip->controller->lock);
  2066. init_waitqueue_head(&chip->controller->wq);
  2067. }
  2068. }
  2069. /*
  2070. * Get the flash and manufacturer id and lookup if the type is supported
  2071. */
  2072. static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
  2073. struct nand_chip *chip,
  2074. int busw, int *maf_id)
  2075. {
  2076. struct nand_flash_dev *type = NULL;
  2077. int i, dev_id, maf_idx;
  2078. int tmp_id, tmp_manf;
  2079. /* Select the device */
  2080. chip->select_chip(mtd, 0);
  2081. /*
  2082. * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
  2083. * after power-up
  2084. */
  2085. chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
  2086. /* Send the command for reading device ID */
  2087. chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
  2088. /* Read manufacturer and device IDs */
  2089. *maf_id = chip->read_byte(mtd);
  2090. dev_id = chip->read_byte(mtd);
  2091. /* Try again to make sure, as some systems the bus-hold or other
  2092. * interface concerns can cause random data which looks like a
  2093. * possibly credible NAND flash to appear. If the two results do
  2094. * not match, ignore the device completely.
  2095. */
  2096. chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
  2097. /* Read manufacturer and device IDs */
  2098. tmp_manf = chip->read_byte(mtd);
  2099. tmp_id = chip->read_byte(mtd);
  2100. if (tmp_manf != *maf_id || tmp_id != dev_id) {
  2101. printk(KERN_INFO "%s: second ID read did not match "
  2102. "%02x,%02x against %02x,%02x\n", __func__,
  2103. *maf_id, dev_id, tmp_manf, tmp_id);
  2104. return ERR_PTR(-ENODEV);
  2105. }
  2106. /* Lookup the flash id */
  2107. for (i = 0; nand_flash_ids[i].name != NULL; i++) {
  2108. if (dev_id == nand_flash_ids[i].id) {
  2109. type = &nand_flash_ids[i];
  2110. break;
  2111. }
  2112. }
  2113. if (!type)
  2114. return ERR_PTR(-ENODEV);
  2115. if (!mtd->name)
  2116. mtd->name = type->name;
  2117. chip->chipsize = (uint64_t)type->chipsize << 20;
  2118. /* Newer devices have all the information in additional id bytes */
  2119. if (!type->pagesize) {
  2120. int extid;
  2121. /* The 3rd id byte holds MLC / multichip data */
  2122. chip->cellinfo = chip->read_byte(mtd);
  2123. /* The 4th id byte is the important one */
  2124. extid = chip->read_byte(mtd);
  2125. /* Calc pagesize */
  2126. mtd->writesize = 1024 << (extid & 0x3);
  2127. extid >>= 2;
  2128. /* Calc oobsize */
  2129. mtd->oobsize = (8 << (extid & 0x01)) * (mtd->writesize >> 9);
  2130. extid >>= 2;
  2131. /* Calc blocksize. Blocksize is multiples of 64KiB */
  2132. mtd->erasesize = (64 * 1024) << (extid & 0x03);
  2133. extid >>= 2;
  2134. /* Get buswidth information */
  2135. busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0;
  2136. } else {
  2137. /*
  2138. * Old devices have chip data hardcoded in the device id table
  2139. */
  2140. mtd->erasesize = type->erasesize;
  2141. mtd->writesize = type->pagesize;
  2142. mtd->oobsize = mtd->writesize / 32;
  2143. busw = type->options & NAND_BUSWIDTH_16;
  2144. }
  2145. /* Try to identify manufacturer */
  2146. for (maf_idx = 0; nand_manuf_ids[maf_idx].id != 0x0; maf_idx++) {
  2147. if (nand_manuf_ids[maf_idx].id == *maf_id)
  2148. break;
  2149. }
  2150. /*
  2151. * Check, if buswidth is correct. Hardware drivers should set
  2152. * chip correct !
  2153. */
  2154. if (busw != (chip->options & NAND_BUSWIDTH_16)) {
  2155. printk(KERN_INFO "NAND device: Manufacturer ID:"
  2156. " 0x%02x, Chip ID: 0x%02x (%s %s)\n", *maf_id,
  2157. dev_id, nand_manuf_ids[maf_idx].name, mtd->name);
  2158. printk(KERN_WARNING "NAND bus width %d instead %d bit\n",
  2159. (chip->options & NAND_BUSWIDTH_16) ? 16 : 8,
  2160. busw ? 16 : 8);
  2161. return ERR_PTR(-EINVAL);
  2162. }
  2163. /* Calculate the address shift from the page size */
  2164. chip->page_shift = ffs(mtd->writesize) - 1;
  2165. /* Convert chipsize to number of pages per chip -1. */
  2166. chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
  2167. chip->bbt_erase_shift = chip->phys_erase_shift =
  2168. ffs(mtd->erasesize) - 1;
  2169. if (chip->chipsize & 0xffffffff)
  2170. chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
  2171. else
  2172. chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32)) + 32 - 1;
  2173. /* Set the bad block position */
  2174. chip->badblockpos = mtd->writesize > 512 ?
  2175. NAND_LARGE_BADBLOCK_POS : NAND_SMALL_BADBLOCK_POS;
  2176. /* Get chip options, preserve non chip based options */
  2177. chip->options &= ~NAND_CHIPOPTIONS_MSK;
  2178. chip->options |= type->options & NAND_CHIPOPTIONS_MSK;
  2179. /*
  2180. * Set chip as a default. Board drivers can override it, if necessary
  2181. */
  2182. chip->options |= NAND_NO_AUTOINCR;
  2183. /* Check if chip is a not a samsung device. Do not clear the
  2184. * options for chips which are not having an extended id.
  2185. */
  2186. if (*maf_id != NAND_MFR_SAMSUNG && !type->pagesize)
  2187. chip->options &= ~NAND_SAMSUNG_LP_OPTIONS;
  2188. /* Check for AND chips with 4 page planes */
  2189. if (chip->options & NAND_4PAGE_ARRAY)
  2190. chip->erase_cmd = multi_erase_cmd;
  2191. else
  2192. chip->erase_cmd = single_erase_cmd;
  2193. /* Do not replace user supplied command function ! */
  2194. if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
  2195. chip->cmdfunc = nand_command_lp;
  2196. printk(KERN_INFO "NAND device: Manufacturer ID:"
  2197. " 0x%02x, Chip ID: 0x%02x (%s %s)\n", *maf_id, dev_id,
  2198. nand_manuf_ids[maf_idx].name, type->name);
  2199. return type;
  2200. }
  2201. /**
  2202. * nand_scan_ident - [NAND Interface] Scan for the NAND device
  2203. * @mtd: MTD device structure
  2204. * @maxchips: Number of chips to scan for
  2205. *
  2206. * This is the first phase of the normal nand_scan() function. It
  2207. * reads the flash ID and sets up MTD fields accordingly.
  2208. *
  2209. * The mtd->owner field must be set to the module of the caller.
  2210. */
  2211. int nand_scan_ident(struct mtd_info *mtd, int maxchips)
  2212. {
  2213. int i, busw, nand_maf_id;
  2214. struct nand_chip *chip = mtd->priv;
  2215. struct nand_flash_dev *type;
  2216. /* Get buswidth to select the correct functions */
  2217. busw = chip->options & NAND_BUSWIDTH_16;
  2218. /* Set the default functions */
  2219. nand_set_defaults(chip, busw);
  2220. /* Read the flash type */
  2221. type = nand_get_flash_type(mtd, chip, busw, &nand_maf_id);
  2222. if (IS_ERR(type)) {
  2223. printk(KERN_WARNING "No NAND device found!!!\n");
  2224. chip->select_chip(mtd, -1);
  2225. return PTR_ERR(type);
  2226. }
  2227. /* Check for a chip array */
  2228. for (i = 1; i < maxchips; i++) {
  2229. chip->select_chip(mtd, i);
  2230. /* See comment in nand_get_flash_type for reset */
  2231. chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
  2232. /* Send the command for reading device ID */
  2233. chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
  2234. /* Read manufacturer and device IDs */
  2235. if (nand_maf_id != chip->read_byte(mtd) ||
  2236. type->id != chip->read_byte(mtd))
  2237. break;
  2238. }
  2239. if (i > 1)
  2240. printk(KERN_INFO "%d NAND chips detected\n", i);
  2241. /* Store the number of chips and calc total size for mtd */
  2242. chip->numchips = i;
  2243. mtd->size = i * chip->chipsize;
  2244. return 0;
  2245. }
  2246. /**
  2247. * nand_scan_tail - [NAND Interface] Scan for the NAND device
  2248. * @mtd: MTD device structure
  2249. *
  2250. * This is the second phase of the normal nand_scan() function. It
  2251. * fills out all the uninitialized function pointers with the defaults
  2252. * and scans for a bad block table if appropriate.
  2253. */
  2254. int nand_scan_tail(struct mtd_info *mtd)
  2255. {
  2256. int i;
  2257. struct nand_chip *chip = mtd->priv;
  2258. if (!(chip->options & NAND_OWN_BUFFERS))
  2259. chip->buffers = kmalloc(sizeof(*chip->buffers), GFP_KERNEL);
  2260. if (!chip->buffers)
  2261. return -ENOMEM;
  2262. /* Set the internal oob buffer location, just after the page data */
  2263. chip->oob_poi = chip->buffers->databuf + mtd->writesize;
  2264. /*
  2265. * If no default placement scheme is given, select an appropriate one
  2266. */
  2267. if (!chip->ecc.layout) {
  2268. switch (mtd->oobsize) {
  2269. case 8:
  2270. chip->ecc.layout = &nand_oob_8;
  2271. break;
  2272. case 16:
  2273. chip->ecc.layout = &nand_oob_16;
  2274. break;
  2275. case 64:
  2276. chip->ecc.layout = &nand_oob_64;
  2277. break;
  2278. case 128:
  2279. chip->ecc.layout = &nand_oob_128;
  2280. break;
  2281. default:
  2282. printk(KERN_WARNING "No oob scheme defined for "
  2283. "oobsize %d\n", mtd->oobsize);
  2284. BUG();
  2285. }
  2286. }
  2287. if (!chip->write_page)
  2288. chip->write_page = nand_write_page;
  2289. /*
  2290. * check ECC mode, default to software if 3byte/512byte hardware ECC is
  2291. * selected and we have 256 byte pagesize fallback to software ECC
  2292. */
  2293. switch (chip->ecc.mode) {
  2294. case NAND_ECC_HW:
  2295. /* Use standard hwecc read page function ? */
  2296. if (!chip->ecc.read_page)
  2297. chip->ecc.read_page = nand_read_page_hwecc;
  2298. if (!chip->ecc.write_page)
  2299. chip->ecc.write_page = nand_write_page_hwecc;
  2300. if (!chip->ecc.read_page_raw)
  2301. chip->ecc.read_page_raw = nand_read_page_raw;
  2302. if (!chip->ecc.write_page_raw)
  2303. chip->ecc.write_page_raw = nand_write_page_raw;
  2304. if (!chip->ecc.read_oob)
  2305. chip->ecc.read_oob = nand_read_oob_std;
  2306. if (!chip->ecc.write_oob)
  2307. chip->ecc.write_oob = nand_write_oob_std;
  2308. case NAND_ECC_HW_SYNDROME:
  2309. if ((!chip->ecc.calculate || !chip->ecc.correct ||
  2310. !chip->ecc.hwctl) &&
  2311. (!chip->ecc.read_page ||
  2312. chip->ecc.read_page == nand_read_page_hwecc ||
  2313. !chip->ecc.write_page ||
  2314. chip->ecc.write_page == nand_write_page_hwecc)) {
  2315. printk(KERN_WARNING "No ECC functions supplied, "
  2316. "Hardware ECC not possible\n");
  2317. BUG();
  2318. }
  2319. /* Use standard syndrome read/write page function ? */
  2320. if (!chip->ecc.read_page)
  2321. chip->ecc.read_page = nand_read_page_syndrome;
  2322. if (!chip->ecc.write_page)
  2323. chip->ecc.write_page = nand_write_page_syndrome;
  2324. if (!chip->ecc.read_page_raw)
  2325. chip->ecc.read_page_raw = nand_read_page_raw_syndrome;
  2326. if (!chip->ecc.write_page_raw)
  2327. chip->ecc.write_page_raw = nand_write_page_raw_syndrome;
  2328. if (!chip->ecc.read_oob)
  2329. chip->ecc.read_oob = nand_read_oob_syndrome;
  2330. if (!chip->ecc.write_oob)
  2331. chip->ecc.write_oob = nand_write_oob_syndrome;
  2332. if (mtd->writesize >= chip->ecc.size)
  2333. break;
  2334. printk(KERN_WARNING "%d byte HW ECC not possible on "
  2335. "%d byte page size, fallback to SW ECC\n",
  2336. chip->ecc.size, mtd->writesize);
  2337. chip->ecc.mode = NAND_ECC_SOFT;
  2338. case NAND_ECC_SOFT:
  2339. chip->ecc.calculate = nand_calculate_ecc;
  2340. chip->ecc.correct = nand_correct_data;
  2341. chip->ecc.read_page = nand_read_page_swecc;
  2342. chip->ecc.read_subpage = nand_read_subpage;
  2343. chip->ecc.write_page = nand_write_page_swecc;
  2344. chip->ecc.read_page_raw = nand_read_page_raw;
  2345. chip->ecc.write_page_raw = nand_write_page_raw;
  2346. chip->ecc.read_oob = nand_read_oob_std;
  2347. chip->ecc.write_oob = nand_write_oob_std;
  2348. chip->ecc.size = 256;
  2349. chip->ecc.bytes = 3;
  2350. break;
  2351. case NAND_ECC_NONE:
  2352. printk(KERN_WARNING "NAND_ECC_NONE selected by board driver. "
  2353. "This is not recommended !!\n");
  2354. chip->ecc.read_page = nand_read_page_raw;
  2355. chip->ecc.write_page = nand_write_page_raw;
  2356. chip->ecc.read_oob = nand_read_oob_std;
  2357. chip->ecc.read_page_raw = nand_read_page_raw;
  2358. chip->ecc.write_page_raw = nand_write_page_raw;
  2359. chip->ecc.write_oob = nand_write_oob_std;
  2360. chip->ecc.size = mtd->writesize;
  2361. chip->ecc.bytes = 0;
  2362. break;
  2363. default:
  2364. printk(KERN_WARNING "Invalid NAND_ECC_MODE %d\n",
  2365. chip->ecc.mode);
  2366. BUG();
  2367. }
  2368. /*
  2369. * The number of bytes available for a client to place data into
  2370. * the out of band area
  2371. */
  2372. chip->ecc.layout->oobavail = 0;
  2373. for (i = 0; chip->ecc.layout->oobfree[i].length
  2374. && i < ARRAY_SIZE(chip->ecc.layout->oobfree); i++)
  2375. chip->ecc.layout->oobavail +=
  2376. chip->ecc.layout->oobfree[i].length;
  2377. mtd->oobavail = chip->ecc.layout->oobavail;
  2378. /*
  2379. * Set the number of read / write steps for one page depending on ECC
  2380. * mode
  2381. */
  2382. chip->ecc.steps = mtd->writesize / chip->ecc.size;
  2383. if(chip->ecc.steps * chip->ecc.size != mtd->writesize) {
  2384. printk(KERN_WARNING "Invalid ecc parameters\n");
  2385. BUG();
  2386. }
  2387. chip->ecc.total = chip->ecc.steps * chip->ecc.bytes;
  2388. /*
  2389. * Allow subpage writes up to ecc.steps. Not possible for MLC
  2390. * FLASH.
  2391. */
  2392. if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
  2393. !(chip->cellinfo & NAND_CI_CELLTYPE_MSK)) {
  2394. switch(chip->ecc.steps) {
  2395. case 2:
  2396. mtd->subpage_sft = 1;
  2397. break;
  2398. case 4:
  2399. case 8:
  2400. case 16:
  2401. mtd->subpage_sft = 2;
  2402. break;
  2403. }
  2404. }
  2405. chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
  2406. /* Initialize state */
  2407. chip->state = FL_READY;
  2408. /* De-select the device */
  2409. chip->select_chip(mtd, -1);
  2410. /* Invalidate the pagebuffer reference */
  2411. chip->pagebuf = -1;
  2412. /* Fill in remaining MTD driver data */
  2413. mtd->type = MTD_NANDFLASH;
  2414. mtd->flags = MTD_CAP_NANDFLASH;
  2415. mtd->erase = nand_erase;
  2416. mtd->point = NULL;
  2417. mtd->unpoint = NULL;
  2418. mtd->read = nand_read;
  2419. mtd->write = nand_write;
  2420. mtd->read_oob = nand_read_oob;
  2421. mtd->write_oob = nand_write_oob;
  2422. mtd->sync = nand_sync;
  2423. mtd->lock = NULL;
  2424. mtd->unlock = NULL;
  2425. mtd->suspend = nand_suspend;
  2426. mtd->resume = nand_resume;
  2427. mtd->block_isbad = nand_block_isbad;
  2428. mtd->block_markbad = nand_block_markbad;
  2429. /* propagate ecc.layout to mtd_info */
  2430. mtd->ecclayout = chip->ecc.layout;
  2431. /* Check, if we should skip the bad block table scan */
  2432. if (chip->options & NAND_SKIP_BBTSCAN)
  2433. return 0;
  2434. /* Build bad block table */
  2435. return chip->scan_bbt(mtd);
  2436. }
  2437. /* is_module_text_address() isn't exported, and it's mostly a pointless
  2438. test if this is a module _anyway_ -- they'd have to try _really_ hard
  2439. to call us from in-kernel code if the core NAND support is modular. */
  2440. #ifdef MODULE
  2441. #define caller_is_module() (1)
  2442. #else
  2443. #define caller_is_module() \
  2444. is_module_text_address((unsigned long)__builtin_return_address(0))
  2445. #endif
  2446. /**
  2447. * nand_scan - [NAND Interface] Scan for the NAND device
  2448. * @mtd: MTD device structure
  2449. * @maxchips: Number of chips to scan for
  2450. *
  2451. * This fills out all the uninitialized function pointers
  2452. * with the defaults.
  2453. * The flash ID is read and the mtd/chip structures are
  2454. * filled with the appropriate values.
  2455. * The mtd->owner field must be set to the module of the caller
  2456. *
  2457. */
  2458. int nand_scan(struct mtd_info *mtd, int maxchips)
  2459. {
  2460. int ret;
  2461. /* Many callers got this wrong, so check for it for a while... */
  2462. if (!mtd->owner && caller_is_module()) {
  2463. printk(KERN_CRIT "nand_scan() called with NULL mtd->owner!\n");
  2464. BUG();
  2465. }
  2466. ret = nand_scan_ident(mtd, maxchips);
  2467. if (!ret)
  2468. ret = nand_scan_tail(mtd);
  2469. return ret;
  2470. }
  2471. /**
  2472. * nand_release - [NAND Interface] Free resources held by the NAND device
  2473. * @mtd: MTD device structure
  2474. */
  2475. void nand_release(struct mtd_info *mtd)
  2476. {
  2477. struct nand_chip *chip = mtd->priv;
  2478. #ifdef CONFIG_MTD_PARTITIONS
  2479. /* Deregister partitions */
  2480. del_mtd_partitions(mtd);
  2481. #endif
  2482. /* Deregister the device */
  2483. del_mtd_device(mtd);
  2484. /* Free bad block table memory */
  2485. kfree(chip->bbt);
  2486. if (!(chip->options & NAND_OWN_BUFFERS))
  2487. kfree(chip->buffers);
  2488. }
  2489. EXPORT_SYMBOL_GPL(nand_scan);
  2490. EXPORT_SYMBOL_GPL(nand_scan_ident);
  2491. EXPORT_SYMBOL_GPL(nand_scan_tail);
  2492. EXPORT_SYMBOL_GPL(nand_release);
  2493. static int __init nand_base_init(void)
  2494. {
  2495. led_trigger_register_simple("nand-disk", &nand_led_trigger);
  2496. return 0;
  2497. }
  2498. static void __exit nand_base_exit(void)
  2499. {
  2500. led_trigger_unregister_simple(nand_led_trigger);
  2501. }
  2502. module_init(nand_base_init);
  2503. module_exit(nand_base_exit);
  2504. MODULE_LICENSE("GPL");
  2505. MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>, Thomas Gleixner <tglx@linutronix.de>");
  2506. MODULE_DESCRIPTION("Generic NAND flash driver code");