nand_base.c 75 KB

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