nand_base.c 78 KB

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