nand_base.c 78 KB

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