nand_base.c 80 KB

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