nand_base.c 71 KB

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