nand_base.c 74 KB

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