nand_base.c 83 KB

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