nand_base.c 64 KB

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