nand_base.c 77 KB

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