nand_base.c 75 KB

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