nand_base.c 78 KB

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