nand_base.c 59 KB

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