nand_base.c 67 KB

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