nand_base.c 71 KB

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