nand_base.c 78 KB

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