nand_base.c 61 KB

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