nand_base.c 65 KB

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