nand_base.c 75 KB

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