nand_base.c 75 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966
  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. */
  792. static int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
  793. uint8_t *buf, int page)
  794. {
  795. chip->read_buf(mtd, buf, mtd->writesize);
  796. chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
  797. return 0;
  798. }
  799. /**
  800. * nand_read_page_swecc - [REPLACABLE] software ecc based page read function
  801. * @mtd: mtd info structure
  802. * @chip: nand chip info structure
  803. * @buf: buffer to store read data
  804. */
  805. static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
  806. uint8_t *buf, int page)
  807. {
  808. int i, eccsize = chip->ecc.size;
  809. int eccbytes = chip->ecc.bytes;
  810. int eccsteps = chip->ecc.steps;
  811. uint8_t *p = buf;
  812. uint8_t *ecc_calc = chip->buffers->ecccalc;
  813. uint8_t *ecc_code = chip->buffers->ecccode;
  814. uint32_t *eccpos = chip->ecc.layout->eccpos;
  815. chip->ecc.read_page_raw(mtd, chip, buf, page);
  816. for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
  817. chip->ecc.calculate(mtd, p, &ecc_calc[i]);
  818. for (i = 0; i < chip->ecc.total; i++)
  819. ecc_code[i] = chip->oob_poi[eccpos[i]];
  820. eccsteps = chip->ecc.steps;
  821. p = buf;
  822. for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
  823. int stat;
  824. stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
  825. if (stat < 0)
  826. mtd->ecc_stats.failed++;
  827. else
  828. mtd->ecc_stats.corrected += stat;
  829. }
  830. return 0;
  831. }
  832. /**
  833. * nand_read_subpage - [REPLACABLE] software ecc based sub-page read function
  834. * @mtd: mtd info structure
  835. * @chip: nand chip info structure
  836. * @dataofs offset of requested data within the page
  837. * @readlen data length
  838. * @buf: buffer to store read data
  839. */
  840. static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip, uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi)
  841. {
  842. int start_step, end_step, num_steps;
  843. uint32_t *eccpos = chip->ecc.layout->eccpos;
  844. uint8_t *p;
  845. int data_col_addr, i, gaps = 0;
  846. int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
  847. int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
  848. /* Column address wihin the page aligned to ECC size (256bytes). */
  849. start_step = data_offs / chip->ecc.size;
  850. end_step = (data_offs + readlen - 1) / chip->ecc.size;
  851. num_steps = end_step - start_step + 1;
  852. /* Data size aligned to ECC ecc.size*/
  853. datafrag_len = num_steps * chip->ecc.size;
  854. eccfrag_len = num_steps * chip->ecc.bytes;
  855. data_col_addr = start_step * chip->ecc.size;
  856. /* If we read not a page aligned data */
  857. if (data_col_addr != 0)
  858. chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_col_addr, -1);
  859. p = bufpoi + data_col_addr;
  860. chip->read_buf(mtd, p, datafrag_len);
  861. /* Calculate ECC */
  862. for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
  863. chip->ecc.calculate(mtd, p, &chip->buffers->ecccalc[i]);
  864. /* The performance is faster if to position offsets
  865. according to ecc.pos. Let make sure here that
  866. there are no gaps in ecc positions */
  867. for (i = 0; i < eccfrag_len - 1; i++) {
  868. if (eccpos[i + start_step * chip->ecc.bytes] + 1 !=
  869. eccpos[i + start_step * chip->ecc.bytes + 1]) {
  870. gaps = 1;
  871. break;
  872. }
  873. }
  874. if (gaps) {
  875. chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
  876. chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
  877. } else {
  878. /* send the command to read the particular ecc bytes */
  879. /* take care about buswidth alignment in read_buf */
  880. aligned_pos = eccpos[start_step * chip->ecc.bytes] & ~(busw - 1);
  881. aligned_len = eccfrag_len;
  882. if (eccpos[start_step * chip->ecc.bytes] & (busw - 1))
  883. aligned_len++;
  884. if (eccpos[(start_step + num_steps) * chip->ecc.bytes] & (busw - 1))
  885. aligned_len++;
  886. chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize + aligned_pos, -1);
  887. chip->read_buf(mtd, &chip->oob_poi[aligned_pos], aligned_len);
  888. }
  889. for (i = 0; i < eccfrag_len; i++)
  890. chip->buffers->ecccode[i] = chip->oob_poi[eccpos[i + start_step * chip->ecc.bytes]];
  891. p = bufpoi + data_col_addr;
  892. for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
  893. int stat;
  894. stat = chip->ecc.correct(mtd, p, &chip->buffers->ecccode[i], &chip->buffers->ecccalc[i]);
  895. if (stat < 0)
  896. mtd->ecc_stats.failed++;
  897. else
  898. mtd->ecc_stats.corrected += stat;
  899. }
  900. return 0;
  901. }
  902. /**
  903. * nand_read_page_hwecc - [REPLACABLE] hardware ecc based page read function
  904. * @mtd: mtd info structure
  905. * @chip: nand chip info structure
  906. * @buf: buffer to store read data
  907. *
  908. * Not for syndrome calculating ecc controllers which need a special oob layout
  909. */
  910. static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
  911. uint8_t *buf, int page)
  912. {
  913. int i, eccsize = chip->ecc.size;
  914. int eccbytes = chip->ecc.bytes;
  915. int eccsteps = chip->ecc.steps;
  916. uint8_t *p = buf;
  917. uint8_t *ecc_calc = chip->buffers->ecccalc;
  918. uint8_t *ecc_code = chip->buffers->ecccode;
  919. uint32_t *eccpos = chip->ecc.layout->eccpos;
  920. for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
  921. chip->ecc.hwctl(mtd, NAND_ECC_READ);
  922. chip->read_buf(mtd, p, eccsize);
  923. chip->ecc.calculate(mtd, p, &ecc_calc[i]);
  924. }
  925. chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
  926. for (i = 0; i < chip->ecc.total; i++)
  927. ecc_code[i] = chip->oob_poi[eccpos[i]];
  928. eccsteps = chip->ecc.steps;
  929. p = buf;
  930. for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
  931. int stat;
  932. stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
  933. if (stat == -1)
  934. mtd->ecc_stats.failed++;
  935. else
  936. mtd->ecc_stats.corrected += stat;
  937. }
  938. return 0;
  939. }
  940. /**
  941. * nand_read_page_syndrome - [REPLACABLE] hardware ecc syndrom based page read
  942. * @mtd: mtd info structure
  943. * @chip: nand chip info structure
  944. * @buf: buffer to store read data
  945. *
  946. * The hw generator calculates the error syndrome automatically. Therefor
  947. * we need a special oob layout and handling.
  948. */
  949. static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
  950. uint8_t *buf, int page)
  951. {
  952. int i, eccsize = chip->ecc.size;
  953. int eccbytes = chip->ecc.bytes;
  954. int eccsteps = chip->ecc.steps;
  955. uint8_t *p = buf;
  956. uint8_t *oob = chip->oob_poi;
  957. for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
  958. int stat;
  959. chip->ecc.hwctl(mtd, NAND_ECC_READ);
  960. chip->read_buf(mtd, p, eccsize);
  961. if (chip->ecc.prepad) {
  962. chip->read_buf(mtd, oob, chip->ecc.prepad);
  963. oob += chip->ecc.prepad;
  964. }
  965. chip->ecc.hwctl(mtd, NAND_ECC_READSYN);
  966. chip->read_buf(mtd, oob, eccbytes);
  967. stat = chip->ecc.correct(mtd, p, oob, NULL);
  968. if (stat < 0)
  969. mtd->ecc_stats.failed++;
  970. else
  971. mtd->ecc_stats.corrected += stat;
  972. oob += eccbytes;
  973. if (chip->ecc.postpad) {
  974. chip->read_buf(mtd, oob, chip->ecc.postpad);
  975. oob += chip->ecc.postpad;
  976. }
  977. }
  978. /* Calculate remaining oob bytes */
  979. i = mtd->oobsize - (oob - chip->oob_poi);
  980. if (i)
  981. chip->read_buf(mtd, oob, i);
  982. return 0;
  983. }
  984. /**
  985. * nand_transfer_oob - [Internal] Transfer oob to client buffer
  986. * @chip: nand chip structure
  987. * @oob: oob destination address
  988. * @ops: oob ops structure
  989. * @len: size of oob to transfer
  990. */
  991. static uint8_t *nand_transfer_oob(struct nand_chip *chip, uint8_t *oob,
  992. struct mtd_oob_ops *ops, size_t len)
  993. {
  994. switch(ops->mode) {
  995. case MTD_OOB_PLACE:
  996. case MTD_OOB_RAW:
  997. memcpy(oob, chip->oob_poi + ops->ooboffs, len);
  998. return oob + len;
  999. case MTD_OOB_AUTO: {
  1000. struct nand_oobfree *free = chip->ecc.layout->oobfree;
  1001. uint32_t boffs = 0, roffs = ops->ooboffs;
  1002. size_t bytes = 0;
  1003. for(; free->length && len; free++, len -= bytes) {
  1004. /* Read request not from offset 0 ? */
  1005. if (unlikely(roffs)) {
  1006. if (roffs >= free->length) {
  1007. roffs -= free->length;
  1008. continue;
  1009. }
  1010. boffs = free->offset + roffs;
  1011. bytes = min_t(size_t, len,
  1012. (free->length - roffs));
  1013. roffs = 0;
  1014. } else {
  1015. bytes = min_t(size_t, len, free->length);
  1016. boffs = free->offset;
  1017. }
  1018. memcpy(oob, chip->oob_poi + boffs, bytes);
  1019. oob += bytes;
  1020. }
  1021. return oob;
  1022. }
  1023. default:
  1024. BUG();
  1025. }
  1026. return NULL;
  1027. }
  1028. /**
  1029. * nand_do_read_ops - [Internal] Read data with ECC
  1030. *
  1031. * @mtd: MTD device structure
  1032. * @from: offset to read from
  1033. * @ops: oob ops structure
  1034. *
  1035. * Internal function. Called with chip held.
  1036. */
  1037. static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
  1038. struct mtd_oob_ops *ops)
  1039. {
  1040. int chipnr, page, realpage, col, bytes, aligned;
  1041. struct nand_chip *chip = mtd->priv;
  1042. struct mtd_ecc_stats stats;
  1043. int blkcheck = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
  1044. int sndcmd = 1;
  1045. int ret = 0;
  1046. uint32_t readlen = ops->len;
  1047. uint32_t oobreadlen = ops->ooblen;
  1048. uint8_t *bufpoi, *oob, *buf;
  1049. stats = mtd->ecc_stats;
  1050. chipnr = (int)(from >> chip->chip_shift);
  1051. chip->select_chip(mtd, chipnr);
  1052. realpage = (int)(from >> chip->page_shift);
  1053. page = realpage & chip->pagemask;
  1054. col = (int)(from & (mtd->writesize - 1));
  1055. buf = ops->datbuf;
  1056. oob = ops->oobbuf;
  1057. while(1) {
  1058. bytes = min(mtd->writesize - col, readlen);
  1059. aligned = (bytes == mtd->writesize);
  1060. /* Is the current page in the buffer ? */
  1061. if (realpage != chip->pagebuf || oob) {
  1062. bufpoi = aligned ? buf : chip->buffers->databuf;
  1063. if (likely(sndcmd)) {
  1064. chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
  1065. sndcmd = 0;
  1066. }
  1067. /* Now read the page into the buffer */
  1068. if (unlikely(ops->mode == MTD_OOB_RAW))
  1069. ret = chip->ecc.read_page_raw(mtd, chip,
  1070. bufpoi, page);
  1071. else if (!aligned && NAND_SUBPAGE_READ(chip) && !oob)
  1072. ret = chip->ecc.read_subpage(mtd, chip, col, bytes, bufpoi);
  1073. else
  1074. ret = chip->ecc.read_page(mtd, chip, bufpoi,
  1075. page);
  1076. if (ret < 0)
  1077. break;
  1078. /* Transfer not aligned data */
  1079. if (!aligned) {
  1080. if (!NAND_SUBPAGE_READ(chip) && !oob)
  1081. chip->pagebuf = realpage;
  1082. memcpy(buf, chip->buffers->databuf + col, bytes);
  1083. }
  1084. buf += bytes;
  1085. if (unlikely(oob)) {
  1086. /* Raw mode does data:oob:data:oob */
  1087. if (ops->mode != MTD_OOB_RAW) {
  1088. int toread = min(oobreadlen,
  1089. chip->ecc.layout->oobavail);
  1090. if (toread) {
  1091. oob = nand_transfer_oob(chip,
  1092. oob, ops, toread);
  1093. oobreadlen -= toread;
  1094. }
  1095. } else
  1096. buf = nand_transfer_oob(chip,
  1097. buf, ops, mtd->oobsize);
  1098. }
  1099. if (!(chip->options & NAND_NO_READRDY)) {
  1100. /*
  1101. * Apply delay or wait for ready/busy pin. Do
  1102. * this before the AUTOINCR check, so no
  1103. * problems arise if a chip which does auto
  1104. * increment is marked as NOAUTOINCR by the
  1105. * board driver.
  1106. */
  1107. if (!chip->dev_ready)
  1108. udelay(chip->chip_delay);
  1109. else
  1110. nand_wait_ready(mtd);
  1111. }
  1112. } else {
  1113. memcpy(buf, chip->buffers->databuf + col, bytes);
  1114. buf += bytes;
  1115. }
  1116. readlen -= bytes;
  1117. if (!readlen)
  1118. break;
  1119. /* For subsequent reads align to page boundary. */
  1120. col = 0;
  1121. /* Increment page address */
  1122. realpage++;
  1123. page = realpage & chip->pagemask;
  1124. /* Check, if we cross a chip boundary */
  1125. if (!page) {
  1126. chipnr++;
  1127. chip->select_chip(mtd, -1);
  1128. chip->select_chip(mtd, chipnr);
  1129. }
  1130. /* Check, if the chip supports auto page increment
  1131. * or if we have hit a block boundary.
  1132. */
  1133. if (!NAND_CANAUTOINCR(chip) || !(page & blkcheck))
  1134. sndcmd = 1;
  1135. }
  1136. ops->retlen = ops->len - (size_t) readlen;
  1137. if (oob)
  1138. ops->oobretlen = ops->ooblen - oobreadlen;
  1139. if (ret)
  1140. return ret;
  1141. if (mtd->ecc_stats.failed - stats.failed)
  1142. return -EBADMSG;
  1143. return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
  1144. }
  1145. /**
  1146. * nand_read - [MTD Interface] MTD compability function for nand_do_read_ecc
  1147. * @mtd: MTD device structure
  1148. * @from: offset to read from
  1149. * @len: number of bytes to read
  1150. * @retlen: pointer to variable to store the number of read bytes
  1151. * @buf: the databuffer to put data
  1152. *
  1153. * Get hold of the chip and call nand_do_read
  1154. */
  1155. static int nand_read(struct mtd_info *mtd, loff_t from, size_t len,
  1156. size_t *retlen, uint8_t *buf)
  1157. {
  1158. struct nand_chip *chip = mtd->priv;
  1159. int ret;
  1160. /* Do not allow reads past end of device */
  1161. if ((from + len) > mtd->size)
  1162. return -EINVAL;
  1163. if (!len)
  1164. return 0;
  1165. nand_get_device(chip, mtd, FL_READING);
  1166. chip->ops.len = len;
  1167. chip->ops.datbuf = buf;
  1168. chip->ops.oobbuf = NULL;
  1169. ret = nand_do_read_ops(mtd, from, &chip->ops);
  1170. *retlen = chip->ops.retlen;
  1171. nand_release_device(mtd);
  1172. return ret;
  1173. }
  1174. /**
  1175. * nand_read_oob_std - [REPLACABLE] the most common OOB data read function
  1176. * @mtd: mtd info structure
  1177. * @chip: nand chip info structure
  1178. * @page: page number to read
  1179. * @sndcmd: flag whether to issue read command or not
  1180. */
  1181. static int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
  1182. int page, int sndcmd)
  1183. {
  1184. if (sndcmd) {
  1185. chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
  1186. sndcmd = 0;
  1187. }
  1188. chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
  1189. return sndcmd;
  1190. }
  1191. /**
  1192. * nand_read_oob_syndrome - [REPLACABLE] OOB data read function for HW ECC
  1193. * with syndromes
  1194. * @mtd: mtd info structure
  1195. * @chip: nand chip info structure
  1196. * @page: page number to read
  1197. * @sndcmd: flag whether to issue read command or not
  1198. */
  1199. static int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
  1200. int page, int sndcmd)
  1201. {
  1202. uint8_t *buf = chip->oob_poi;
  1203. int length = mtd->oobsize;
  1204. int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
  1205. int eccsize = chip->ecc.size;
  1206. uint8_t *bufpoi = buf;
  1207. int i, toread, sndrnd = 0, pos;
  1208. chip->cmdfunc(mtd, NAND_CMD_READ0, chip->ecc.size, page);
  1209. for (i = 0; i < chip->ecc.steps; i++) {
  1210. if (sndrnd) {
  1211. pos = eccsize + i * (eccsize + chunk);
  1212. if (mtd->writesize > 512)
  1213. chip->cmdfunc(mtd, NAND_CMD_RNDOUT, pos, -1);
  1214. else
  1215. chip->cmdfunc(mtd, NAND_CMD_READ0, pos, page);
  1216. } else
  1217. sndrnd = 1;
  1218. toread = min_t(int, length, chunk);
  1219. chip->read_buf(mtd, bufpoi, toread);
  1220. bufpoi += toread;
  1221. length -= toread;
  1222. }
  1223. if (length > 0)
  1224. chip->read_buf(mtd, bufpoi, length);
  1225. return 1;
  1226. }
  1227. /**
  1228. * nand_write_oob_std - [REPLACABLE] the most common OOB data write function
  1229. * @mtd: mtd info structure
  1230. * @chip: nand chip info structure
  1231. * @page: page number to write
  1232. */
  1233. static int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
  1234. int page)
  1235. {
  1236. int status = 0;
  1237. const uint8_t *buf = chip->oob_poi;
  1238. int length = mtd->oobsize;
  1239. chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
  1240. chip->write_buf(mtd, buf, length);
  1241. /* Send command to program the OOB data */
  1242. chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
  1243. status = chip->waitfunc(mtd, chip);
  1244. return status & NAND_STATUS_FAIL ? -EIO : 0;
  1245. }
  1246. /**
  1247. * nand_write_oob_syndrome - [REPLACABLE] OOB data write function for HW ECC
  1248. * with syndrome - only for large page flash !
  1249. * @mtd: mtd info structure
  1250. * @chip: nand chip info structure
  1251. * @page: page number to write
  1252. */
  1253. static int nand_write_oob_syndrome(struct mtd_info *mtd,
  1254. struct nand_chip *chip, int page)
  1255. {
  1256. int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
  1257. int eccsize = chip->ecc.size, length = mtd->oobsize;
  1258. int i, len, pos, status = 0, sndcmd = 0, steps = chip->ecc.steps;
  1259. const uint8_t *bufpoi = chip->oob_poi;
  1260. /*
  1261. * data-ecc-data-ecc ... ecc-oob
  1262. * or
  1263. * data-pad-ecc-pad-data-pad .... ecc-pad-oob
  1264. */
  1265. if (!chip->ecc.prepad && !chip->ecc.postpad) {
  1266. pos = steps * (eccsize + chunk);
  1267. steps = 0;
  1268. } else
  1269. pos = eccsize;
  1270. chip->cmdfunc(mtd, NAND_CMD_SEQIN, pos, page);
  1271. for (i = 0; i < steps; i++) {
  1272. if (sndcmd) {
  1273. if (mtd->writesize <= 512) {
  1274. uint32_t fill = 0xFFFFFFFF;
  1275. len = eccsize;
  1276. while (len > 0) {
  1277. int num = min_t(int, len, 4);
  1278. chip->write_buf(mtd, (uint8_t *)&fill,
  1279. num);
  1280. len -= num;
  1281. }
  1282. } else {
  1283. pos = eccsize + i * (eccsize + chunk);
  1284. chip->cmdfunc(mtd, NAND_CMD_RNDIN, pos, -1);
  1285. }
  1286. } else
  1287. sndcmd = 1;
  1288. len = min_t(int, length, chunk);
  1289. chip->write_buf(mtd, bufpoi, len);
  1290. bufpoi += len;
  1291. length -= len;
  1292. }
  1293. if (length > 0)
  1294. chip->write_buf(mtd, bufpoi, length);
  1295. chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
  1296. status = chip->waitfunc(mtd, chip);
  1297. return status & NAND_STATUS_FAIL ? -EIO : 0;
  1298. }
  1299. /**
  1300. * nand_do_read_oob - [Intern] NAND read out-of-band
  1301. * @mtd: MTD device structure
  1302. * @from: offset to read from
  1303. * @ops: oob operations description structure
  1304. *
  1305. * NAND read out-of-band data from the spare area
  1306. */
  1307. static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
  1308. struct mtd_oob_ops *ops)
  1309. {
  1310. int page, realpage, chipnr, sndcmd = 1;
  1311. struct nand_chip *chip = mtd->priv;
  1312. int blkcheck = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
  1313. int readlen = ops->ooblen;
  1314. int len;
  1315. uint8_t *buf = ops->oobbuf;
  1316. MTDDEBUG (MTD_DEBUG_LEVEL3, "nand_read_oob: from = 0x%08Lx, len = %i\n",
  1317. (unsigned long long)from, readlen);
  1318. if (ops->mode == MTD_OOB_AUTO)
  1319. len = chip->ecc.layout->oobavail;
  1320. else
  1321. len = mtd->oobsize;
  1322. if (unlikely(ops->ooboffs >= len)) {
  1323. MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_read_oob: "
  1324. "Attempt to start read outside oob\n");
  1325. return -EINVAL;
  1326. }
  1327. /* Do not allow reads past end of device */
  1328. if (unlikely(from >= mtd->size ||
  1329. ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) -
  1330. (from >> chip->page_shift)) * len)) {
  1331. MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_read_oob: "
  1332. "Attempt read beyond end of device\n");
  1333. return -EINVAL;
  1334. }
  1335. chipnr = (int)(from >> chip->chip_shift);
  1336. chip->select_chip(mtd, chipnr);
  1337. /* Shift to get page */
  1338. realpage = (int)(from >> chip->page_shift);
  1339. page = realpage & chip->pagemask;
  1340. while(1) {
  1341. sndcmd = chip->ecc.read_oob(mtd, chip, page, sndcmd);
  1342. len = min(len, readlen);
  1343. buf = nand_transfer_oob(chip, buf, ops, len);
  1344. if (!(chip->options & NAND_NO_READRDY)) {
  1345. /*
  1346. * Apply delay or wait for ready/busy pin. Do this
  1347. * before the AUTOINCR check, so no problems arise if a
  1348. * chip which does auto increment is marked as
  1349. * NOAUTOINCR by the board driver.
  1350. */
  1351. if (!chip->dev_ready)
  1352. udelay(chip->chip_delay);
  1353. else
  1354. nand_wait_ready(mtd);
  1355. }
  1356. readlen -= len;
  1357. if (!readlen)
  1358. break;
  1359. /* Increment page address */
  1360. realpage++;
  1361. page = realpage & chip->pagemask;
  1362. /* Check, if we cross a chip boundary */
  1363. if (!page) {
  1364. chipnr++;
  1365. chip->select_chip(mtd, -1);
  1366. chip->select_chip(mtd, chipnr);
  1367. }
  1368. /* Check, if the chip supports auto page increment
  1369. * or if we have hit a block boundary.
  1370. */
  1371. if (!NAND_CANAUTOINCR(chip) || !(page & blkcheck))
  1372. sndcmd = 1;
  1373. }
  1374. ops->oobretlen = ops->ooblen;
  1375. return 0;
  1376. }
  1377. /**
  1378. * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
  1379. * @mtd: MTD device structure
  1380. * @from: offset to read from
  1381. * @ops: oob operation description structure
  1382. *
  1383. * NAND read data and/or out-of-band data
  1384. */
  1385. static int nand_read_oob(struct mtd_info *mtd, loff_t from,
  1386. struct mtd_oob_ops *ops)
  1387. {
  1388. struct nand_chip *chip = mtd->priv;
  1389. int ret = -ENOTSUPP;
  1390. ops->retlen = 0;
  1391. /* Do not allow reads past end of device */
  1392. if (ops->datbuf && (from + ops->len) > mtd->size) {
  1393. MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_read_oob: "
  1394. "Attempt read beyond end of device\n");
  1395. return -EINVAL;
  1396. }
  1397. nand_get_device(chip, mtd, FL_READING);
  1398. switch(ops->mode) {
  1399. case MTD_OOB_PLACE:
  1400. case MTD_OOB_AUTO:
  1401. case MTD_OOB_RAW:
  1402. break;
  1403. default:
  1404. goto out;
  1405. }
  1406. if (!ops->datbuf)
  1407. ret = nand_do_read_oob(mtd, from, ops);
  1408. else
  1409. ret = nand_do_read_ops(mtd, from, ops);
  1410. out:
  1411. nand_release_device(mtd);
  1412. return ret;
  1413. }
  1414. /**
  1415. * nand_write_page_raw - [Intern] raw page write function
  1416. * @mtd: mtd info structure
  1417. * @chip: nand chip info structure
  1418. * @buf: data buffer
  1419. */
  1420. static void nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
  1421. const uint8_t *buf)
  1422. {
  1423. chip->write_buf(mtd, buf, mtd->writesize);
  1424. chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
  1425. }
  1426. /**
  1427. * nand_write_page_swecc - [REPLACABLE] software ecc based page write function
  1428. * @mtd: mtd info structure
  1429. * @chip: nand chip info structure
  1430. * @buf: data buffer
  1431. */
  1432. static void nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
  1433. const uint8_t *buf)
  1434. {
  1435. int i, eccsize = chip->ecc.size;
  1436. int eccbytes = chip->ecc.bytes;
  1437. int eccsteps = chip->ecc.steps;
  1438. uint8_t *ecc_calc = chip->buffers->ecccalc;
  1439. const uint8_t *p = buf;
  1440. uint32_t *eccpos = chip->ecc.layout->eccpos;
  1441. /* Software ecc calculation */
  1442. for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
  1443. chip->ecc.calculate(mtd, p, &ecc_calc[i]);
  1444. for (i = 0; i < chip->ecc.total; i++)
  1445. chip->oob_poi[eccpos[i]] = ecc_calc[i];
  1446. chip->ecc.write_page_raw(mtd, chip, buf);
  1447. }
  1448. /**
  1449. * nand_write_page_hwecc - [REPLACABLE] hardware ecc based page write function
  1450. * @mtd: mtd info structure
  1451. * @chip: nand chip info structure
  1452. * @buf: data buffer
  1453. */
  1454. static void nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
  1455. const uint8_t *buf)
  1456. {
  1457. int i, eccsize = chip->ecc.size;
  1458. int eccbytes = chip->ecc.bytes;
  1459. int eccsteps = chip->ecc.steps;
  1460. uint8_t *ecc_calc = chip->buffers->ecccalc;
  1461. const uint8_t *p = buf;
  1462. uint32_t *eccpos = chip->ecc.layout->eccpos;
  1463. for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
  1464. chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
  1465. chip->write_buf(mtd, p, eccsize);
  1466. chip->ecc.calculate(mtd, p, &ecc_calc[i]);
  1467. }
  1468. for (i = 0; i < chip->ecc.total; i++)
  1469. chip->oob_poi[eccpos[i]] = ecc_calc[i];
  1470. chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
  1471. }
  1472. /**
  1473. * nand_write_page_syndrome - [REPLACABLE] hardware ecc syndrom based page write
  1474. * @mtd: mtd info structure
  1475. * @chip: nand chip info structure
  1476. * @buf: data buffer
  1477. *
  1478. * The hw generator calculates the error syndrome automatically. Therefor
  1479. * we need a special oob layout and handling.
  1480. */
  1481. static void nand_write_page_syndrome(struct mtd_info *mtd,
  1482. struct nand_chip *chip, const uint8_t *buf)
  1483. {
  1484. int i, eccsize = chip->ecc.size;
  1485. int eccbytes = chip->ecc.bytes;
  1486. int eccsteps = chip->ecc.steps;
  1487. const uint8_t *p = buf;
  1488. uint8_t *oob = chip->oob_poi;
  1489. for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
  1490. chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
  1491. chip->write_buf(mtd, p, eccsize);
  1492. if (chip->ecc.prepad) {
  1493. chip->write_buf(mtd, oob, chip->ecc.prepad);
  1494. oob += chip->ecc.prepad;
  1495. }
  1496. chip->ecc.calculate(mtd, p, oob);
  1497. chip->write_buf(mtd, oob, eccbytes);
  1498. oob += eccbytes;
  1499. if (chip->ecc.postpad) {
  1500. chip->write_buf(mtd, oob, chip->ecc.postpad);
  1501. oob += chip->ecc.postpad;
  1502. }
  1503. }
  1504. /* Calculate remaining oob bytes */
  1505. i = mtd->oobsize - (oob - chip->oob_poi);
  1506. if (i)
  1507. chip->write_buf(mtd, oob, i);
  1508. }
  1509. /**
  1510. * nand_write_page - [REPLACEABLE] write one page
  1511. * @mtd: MTD device structure
  1512. * @chip: NAND chip descriptor
  1513. * @buf: the data to write
  1514. * @page: page number to write
  1515. * @cached: cached programming
  1516. * @raw: use _raw version of write_page
  1517. */
  1518. static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
  1519. const uint8_t *buf, int page, int cached, int raw)
  1520. {
  1521. int status;
  1522. chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
  1523. if (unlikely(raw))
  1524. chip->ecc.write_page_raw(mtd, chip, buf);
  1525. else
  1526. chip->ecc.write_page(mtd, chip, buf);
  1527. /*
  1528. * Cached progamming disabled for now, Not sure if its worth the
  1529. * trouble. The speed gain is not very impressive. (2.3->2.6Mib/s)
  1530. */
  1531. cached = 0;
  1532. if (!cached || !(chip->options & NAND_CACHEPRG)) {
  1533. chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
  1534. status = chip->waitfunc(mtd, chip);
  1535. /*
  1536. * See if operation failed and additional status checks are
  1537. * available
  1538. */
  1539. if ((status & NAND_STATUS_FAIL) && (chip->errstat))
  1540. status = chip->errstat(mtd, chip, FL_WRITING, status,
  1541. page);
  1542. if (status & NAND_STATUS_FAIL)
  1543. return -EIO;
  1544. } else {
  1545. chip->cmdfunc(mtd, NAND_CMD_CACHEDPROG, -1, -1);
  1546. status = chip->waitfunc(mtd, chip);
  1547. }
  1548. #ifdef CONFIG_MTD_NAND_VERIFY_WRITE
  1549. /* Send command to read back the data */
  1550. chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
  1551. if (chip->verify_buf(mtd, buf, mtd->writesize))
  1552. return -EIO;
  1553. #endif
  1554. return 0;
  1555. }
  1556. /**
  1557. * nand_fill_oob - [Internal] Transfer client buffer to oob
  1558. * @chip: nand chip structure
  1559. * @oob: oob data buffer
  1560. * @ops: oob ops structure
  1561. */
  1562. static uint8_t *nand_fill_oob(struct nand_chip *chip, uint8_t *oob,
  1563. struct mtd_oob_ops *ops)
  1564. {
  1565. size_t len = ops->ooblen;
  1566. switch(ops->mode) {
  1567. case MTD_OOB_PLACE:
  1568. case MTD_OOB_RAW:
  1569. memcpy(chip->oob_poi + ops->ooboffs, oob, len);
  1570. return oob + len;
  1571. case MTD_OOB_AUTO: {
  1572. struct nand_oobfree *free = chip->ecc.layout->oobfree;
  1573. uint32_t boffs = 0, woffs = ops->ooboffs;
  1574. size_t bytes = 0;
  1575. for(; free->length && len; free++, len -= bytes) {
  1576. /* Write request not from offset 0 ? */
  1577. if (unlikely(woffs)) {
  1578. if (woffs >= free->length) {
  1579. woffs -= free->length;
  1580. continue;
  1581. }
  1582. boffs = free->offset + woffs;
  1583. bytes = min_t(size_t, len,
  1584. (free->length - woffs));
  1585. woffs = 0;
  1586. } else {
  1587. bytes = min_t(size_t, len, free->length);
  1588. boffs = free->offset;
  1589. }
  1590. memcpy(chip->oob_poi + boffs, oob, bytes);
  1591. oob += bytes;
  1592. }
  1593. return oob;
  1594. }
  1595. default:
  1596. BUG();
  1597. }
  1598. return NULL;
  1599. }
  1600. #define NOTALIGNED(x) (x & (chip->subpagesize - 1)) != 0
  1601. /**
  1602. * nand_do_write_ops - [Internal] NAND write with ECC
  1603. * @mtd: MTD device structure
  1604. * @to: offset to write to
  1605. * @ops: oob operations description structure
  1606. *
  1607. * NAND write with ECC
  1608. */
  1609. static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
  1610. struct mtd_oob_ops *ops)
  1611. {
  1612. int chipnr, realpage, page, blockmask, column;
  1613. struct nand_chip *chip = mtd->priv;
  1614. uint32_t writelen = ops->len;
  1615. uint8_t *oob = ops->oobbuf;
  1616. uint8_t *buf = ops->datbuf;
  1617. int ret, subpage;
  1618. ops->retlen = 0;
  1619. if (!writelen)
  1620. return 0;
  1621. /* reject writes, which are not page aligned */
  1622. if (NOTALIGNED(to) || NOTALIGNED(ops->len)) {
  1623. printk(KERN_NOTICE "nand_write: "
  1624. "Attempt to write not page aligned data\n");
  1625. return -EINVAL;
  1626. }
  1627. column = to & (mtd->writesize - 1);
  1628. subpage = column || (writelen & (mtd->writesize - 1));
  1629. if (subpage && oob)
  1630. return -EINVAL;
  1631. chipnr = (int)(to >> chip->chip_shift);
  1632. chip->select_chip(mtd, chipnr);
  1633. /* Check, if it is write protected */
  1634. if (nand_check_wp(mtd)) {
  1635. printk (KERN_NOTICE "nand_do_write_ops: Device is write protected\n");
  1636. return -EIO;
  1637. }
  1638. realpage = (int)(to >> chip->page_shift);
  1639. page = realpage & chip->pagemask;
  1640. blockmask = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
  1641. /* Invalidate the page cache, when we write to the cached page */
  1642. if (to <= (chip->pagebuf << chip->page_shift) &&
  1643. (chip->pagebuf << chip->page_shift) < (to + ops->len))
  1644. chip->pagebuf = -1;
  1645. /* If we're not given explicit OOB data, let it be 0xFF */
  1646. if (likely(!oob))
  1647. memset(chip->oob_poi, 0xff, mtd->oobsize);
  1648. while(1) {
  1649. int bytes = mtd->writesize;
  1650. int cached = writelen > bytes && page != blockmask;
  1651. uint8_t *wbuf = buf;
  1652. /* Partial page write ? */
  1653. if (unlikely(column || writelen < (mtd->writesize - 1))) {
  1654. cached = 0;
  1655. bytes = min_t(int, bytes - column, (int) writelen);
  1656. chip->pagebuf = -1;
  1657. memset(chip->buffers->databuf, 0xff, mtd->writesize);
  1658. memcpy(&chip->buffers->databuf[column], buf, bytes);
  1659. wbuf = chip->buffers->databuf;
  1660. }
  1661. if (unlikely(oob))
  1662. oob = nand_fill_oob(chip, oob, ops);
  1663. ret = chip->write_page(mtd, chip, wbuf, page, cached,
  1664. (ops->mode == MTD_OOB_RAW));
  1665. if (ret)
  1666. break;
  1667. writelen -= bytes;
  1668. if (!writelen)
  1669. break;
  1670. column = 0;
  1671. buf += bytes;
  1672. realpage++;
  1673. page = realpage & chip->pagemask;
  1674. /* Check, if we cross a chip boundary */
  1675. if (!page) {
  1676. chipnr++;
  1677. chip->select_chip(mtd, -1);
  1678. chip->select_chip(mtd, chipnr);
  1679. }
  1680. }
  1681. ops->retlen = ops->len - writelen;
  1682. if (unlikely(oob))
  1683. ops->oobretlen = ops->ooblen;
  1684. return ret;
  1685. }
  1686. /**
  1687. * nand_write - [MTD Interface] NAND write with ECC
  1688. * @mtd: MTD device structure
  1689. * @to: offset to write to
  1690. * @len: number of bytes to write
  1691. * @retlen: pointer to variable to store the number of written bytes
  1692. * @buf: the data to write
  1693. *
  1694. * NAND write with ECC
  1695. */
  1696. static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
  1697. size_t *retlen, const uint8_t *buf)
  1698. {
  1699. struct nand_chip *chip = mtd->priv;
  1700. int ret;
  1701. /* Do not allow reads past end of device */
  1702. if ((to + len) > mtd->size)
  1703. return -EINVAL;
  1704. if (!len)
  1705. return 0;
  1706. nand_get_device(chip, mtd, FL_WRITING);
  1707. chip->ops.len = len;
  1708. chip->ops.datbuf = (uint8_t *)buf;
  1709. chip->ops.oobbuf = NULL;
  1710. ret = nand_do_write_ops(mtd, to, &chip->ops);
  1711. *retlen = chip->ops.retlen;
  1712. nand_release_device(mtd);
  1713. return ret;
  1714. }
  1715. /**
  1716. * nand_do_write_oob - [MTD Interface] NAND write out-of-band
  1717. * @mtd: MTD device structure
  1718. * @to: offset to write to
  1719. * @ops: oob operation description structure
  1720. *
  1721. * NAND write out-of-band
  1722. */
  1723. static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
  1724. struct mtd_oob_ops *ops)
  1725. {
  1726. int chipnr, page, status, len;
  1727. struct nand_chip *chip = mtd->priv;
  1728. MTDDEBUG (MTD_DEBUG_LEVEL3, "nand_write_oob: to = 0x%08x, len = %i\n",
  1729. (unsigned int)to, (int)ops->ooblen);
  1730. if (ops->mode == MTD_OOB_AUTO)
  1731. len = chip->ecc.layout->oobavail;
  1732. else
  1733. len = mtd->oobsize;
  1734. /* Do not allow write past end of page */
  1735. if ((ops->ooboffs + ops->ooblen) > len) {
  1736. MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_write_oob: "
  1737. "Attempt to write past end of page\n");
  1738. return -EINVAL;
  1739. }
  1740. if (unlikely(ops->ooboffs >= len)) {
  1741. MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_read_oob: "
  1742. "Attempt to start write outside oob\n");
  1743. return -EINVAL;
  1744. }
  1745. /* Do not allow reads past end of device */
  1746. if (unlikely(to >= mtd->size ||
  1747. ops->ooboffs + ops->ooblen >
  1748. ((mtd->size >> chip->page_shift) -
  1749. (to >> chip->page_shift)) * len)) {
  1750. MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_read_oob: "
  1751. "Attempt write beyond end of device\n");
  1752. return -EINVAL;
  1753. }
  1754. chipnr = (int)(to >> chip->chip_shift);
  1755. chip->select_chip(mtd, chipnr);
  1756. /* Shift to get page */
  1757. page = (int)(to >> chip->page_shift);
  1758. /*
  1759. * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
  1760. * of my DiskOnChip 2000 test units) will clear the whole data page too
  1761. * if we don't do this. I have no clue why, but I seem to have 'fixed'
  1762. * it in the doc2000 driver in August 1999. dwmw2.
  1763. */
  1764. chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
  1765. /* Check, if it is write protected */
  1766. if (nand_check_wp(mtd))
  1767. return -EROFS;
  1768. /* Invalidate the page cache, if we write to the cached page */
  1769. if (page == chip->pagebuf)
  1770. chip->pagebuf = -1;
  1771. memset(chip->oob_poi, 0xff, mtd->oobsize);
  1772. nand_fill_oob(chip, ops->oobbuf, ops);
  1773. status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
  1774. memset(chip->oob_poi, 0xff, mtd->oobsize);
  1775. if (status)
  1776. return status;
  1777. ops->oobretlen = ops->ooblen;
  1778. return 0;
  1779. }
  1780. /**
  1781. * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
  1782. * @mtd: MTD device structure
  1783. * @to: offset to write to
  1784. * @ops: oob operation description structure
  1785. */
  1786. static int nand_write_oob(struct mtd_info *mtd, loff_t to,
  1787. struct mtd_oob_ops *ops)
  1788. {
  1789. struct nand_chip *chip = mtd->priv;
  1790. int ret = -ENOTSUPP;
  1791. ops->retlen = 0;
  1792. /* Do not allow writes past end of device */
  1793. if (ops->datbuf && (to + ops->len) > mtd->size) {
  1794. MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_read_oob: "
  1795. "Attempt read beyond end of device\n");
  1796. return -EINVAL;
  1797. }
  1798. nand_get_device(chip, mtd, FL_WRITING);
  1799. switch(ops->mode) {
  1800. case MTD_OOB_PLACE:
  1801. case MTD_OOB_AUTO:
  1802. case MTD_OOB_RAW:
  1803. break;
  1804. default:
  1805. goto out;
  1806. }
  1807. if (!ops->datbuf)
  1808. ret = nand_do_write_oob(mtd, to, ops);
  1809. else
  1810. ret = nand_do_write_ops(mtd, to, ops);
  1811. out:
  1812. nand_release_device(mtd);
  1813. return ret;
  1814. }
  1815. /**
  1816. * single_erease_cmd - [GENERIC] NAND standard block erase command function
  1817. * @mtd: MTD device structure
  1818. * @page: the page address of the block which will be erased
  1819. *
  1820. * Standard erase command for NAND chips
  1821. */
  1822. static void single_erase_cmd(struct mtd_info *mtd, int page)
  1823. {
  1824. struct nand_chip *chip = mtd->priv;
  1825. /* Send commands to erase a block */
  1826. chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
  1827. chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
  1828. }
  1829. /**
  1830. * multi_erease_cmd - [GENERIC] AND specific block erase command function
  1831. * @mtd: MTD device structure
  1832. * @page: the page address of the block which will be erased
  1833. *
  1834. * AND multi block erase command function
  1835. * Erase 4 consecutive blocks
  1836. */
  1837. static void multi_erase_cmd(struct mtd_info *mtd, int page)
  1838. {
  1839. struct nand_chip *chip = mtd->priv;
  1840. /* Send commands to erase a block */
  1841. chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
  1842. chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
  1843. chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
  1844. chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
  1845. chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
  1846. }
  1847. /**
  1848. * nand_erase - [MTD Interface] erase block(s)
  1849. * @mtd: MTD device structure
  1850. * @instr: erase instruction
  1851. *
  1852. * Erase one ore more blocks
  1853. */
  1854. static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
  1855. {
  1856. return nand_erase_nand(mtd, instr, 0);
  1857. }
  1858. #define BBT_PAGE_MASK 0xffffff3f
  1859. /**
  1860. * nand_erase_nand - [Internal] erase block(s)
  1861. * @mtd: MTD device structure
  1862. * @instr: erase instruction
  1863. * @allowbbt: allow erasing the bbt area
  1864. *
  1865. * Erase one ore more blocks
  1866. */
  1867. int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
  1868. int allowbbt)
  1869. {
  1870. int page, len, status, pages_per_block, ret, chipnr;
  1871. struct nand_chip *chip = mtd->priv;
  1872. int rewrite_bbt[CONFIG_SYS_NAND_MAX_CHIPS]={0};
  1873. unsigned int bbt_masked_page = 0xffffffff;
  1874. MTDDEBUG (MTD_DEBUG_LEVEL3, "nand_erase: start = 0x%08x, len = %i\n",
  1875. (unsigned int) instr->addr, (unsigned int) instr->len);
  1876. /* Start address must align on block boundary */
  1877. if (instr->addr & ((1 << chip->phys_erase_shift) - 1)) {
  1878. MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_erase: Unaligned address\n");
  1879. return -EINVAL;
  1880. }
  1881. /* Length must align on block boundary */
  1882. if (instr->len & ((1 << chip->phys_erase_shift) - 1)) {
  1883. MTDDEBUG (MTD_DEBUG_LEVEL0,
  1884. "nand_erase: Length not block aligned\n");
  1885. return -EINVAL;
  1886. }
  1887. /* Do not allow erase past end of device */
  1888. if ((instr->len + instr->addr) > mtd->size) {
  1889. MTDDEBUG (MTD_DEBUG_LEVEL0,
  1890. "nand_erase: Erase past end of device\n");
  1891. return -EINVAL;
  1892. }
  1893. instr->fail_addr = 0xffffffff;
  1894. /* Grab the lock and see if the device is available */
  1895. nand_get_device(chip, mtd, FL_ERASING);
  1896. /* Shift to get first page */
  1897. page = (int)(instr->addr >> chip->page_shift);
  1898. chipnr = (int)(instr->addr >> chip->chip_shift);
  1899. /* Calculate pages in each block */
  1900. pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
  1901. /* Select the NAND device */
  1902. chip->select_chip(mtd, chipnr);
  1903. /* Check, if it is write protected */
  1904. if (nand_check_wp(mtd)) {
  1905. MTDDEBUG (MTD_DEBUG_LEVEL0,
  1906. "nand_erase: Device is write protected!!!\n");
  1907. instr->state = MTD_ERASE_FAILED;
  1908. goto erase_exit;
  1909. }
  1910. /*
  1911. * If BBT requires refresh, set the BBT page mask to see if the BBT
  1912. * should be rewritten. Otherwise the mask is set to 0xffffffff which
  1913. * can not be matched. This is also done when the bbt is actually
  1914. * erased to avoid recusrsive updates
  1915. */
  1916. if (chip->options & BBT_AUTO_REFRESH && !allowbbt)
  1917. bbt_masked_page = chip->bbt_td->pages[chipnr] & BBT_PAGE_MASK;
  1918. /* Loop through the pages */
  1919. len = instr->len;
  1920. instr->state = MTD_ERASING;
  1921. while (len) {
  1922. /*
  1923. * heck if we have a bad block, we do not erase bad blocks !
  1924. */
  1925. if (nand_block_checkbad(mtd, ((loff_t) page) <<
  1926. chip->page_shift, 0, allowbbt)) {
  1927. printk(KERN_WARNING "nand_erase: attempt to erase a "
  1928. "bad block at page 0x%08x\n", page);
  1929. instr->state = MTD_ERASE_FAILED;
  1930. goto erase_exit;
  1931. }
  1932. /*
  1933. * Invalidate the page cache, if we erase the block which
  1934. * contains the current cached page
  1935. */
  1936. if (page <= chip->pagebuf && chip->pagebuf <
  1937. (page + pages_per_block))
  1938. chip->pagebuf = -1;
  1939. chip->erase_cmd(mtd, page & chip->pagemask);
  1940. status = chip->waitfunc(mtd, chip);
  1941. /*
  1942. * See if operation failed and additional status checks are
  1943. * available
  1944. */
  1945. if ((status & NAND_STATUS_FAIL) && (chip->errstat))
  1946. status = chip->errstat(mtd, chip, FL_ERASING,
  1947. status, page);
  1948. /* See if block erase succeeded */
  1949. if (status & NAND_STATUS_FAIL) {
  1950. MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_erase: "
  1951. "Failed erase, page 0x%08x\n", page);
  1952. instr->state = MTD_ERASE_FAILED;
  1953. instr->fail_addr = (page << chip->page_shift);
  1954. goto erase_exit;
  1955. }
  1956. /*
  1957. * If BBT requires refresh, set the BBT rewrite flag to the
  1958. * page being erased
  1959. */
  1960. if (bbt_masked_page != 0xffffffff &&
  1961. (page & BBT_PAGE_MASK) == bbt_masked_page)
  1962. rewrite_bbt[chipnr] = (page << chip->page_shift);
  1963. /* Increment page address and decrement length */
  1964. len -= (1 << chip->phys_erase_shift);
  1965. page += pages_per_block;
  1966. /* Check, if we cross a chip boundary */
  1967. if (len && !(page & chip->pagemask)) {
  1968. chipnr++;
  1969. chip->select_chip(mtd, -1);
  1970. chip->select_chip(mtd, chipnr);
  1971. /*
  1972. * If BBT requires refresh and BBT-PERCHIP, set the BBT
  1973. * page mask to see if this BBT should be rewritten
  1974. */
  1975. if (bbt_masked_page != 0xffffffff &&
  1976. (chip->bbt_td->options & NAND_BBT_PERCHIP))
  1977. bbt_masked_page = chip->bbt_td->pages[chipnr] &
  1978. BBT_PAGE_MASK;
  1979. }
  1980. }
  1981. instr->state = MTD_ERASE_DONE;
  1982. erase_exit:
  1983. ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
  1984. /* Deselect and wake up anyone waiting on the device */
  1985. nand_release_device(mtd);
  1986. /* Do call back function */
  1987. if (!ret)
  1988. mtd_erase_callback(instr);
  1989. /*
  1990. * If BBT requires refresh and erase was successful, rewrite any
  1991. * selected bad block tables
  1992. */
  1993. if (bbt_masked_page == 0xffffffff || ret)
  1994. return ret;
  1995. for (chipnr = 0; chipnr < chip->numchips; chipnr++) {
  1996. if (!rewrite_bbt[chipnr])
  1997. continue;
  1998. /* update the BBT for chip */
  1999. MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_erase_nand: nand_update_bbt "
  2000. "(%d:0x%0x 0x%0x)\n", chipnr, rewrite_bbt[chipnr],
  2001. chip->bbt_td->pages[chipnr]);
  2002. nand_update_bbt(mtd, rewrite_bbt[chipnr]);
  2003. }
  2004. /* Return more or less happy */
  2005. return ret;
  2006. }
  2007. /**
  2008. * nand_sync - [MTD Interface] sync
  2009. * @mtd: MTD device structure
  2010. *
  2011. * Sync is actually a wait for chip ready function
  2012. */
  2013. static void nand_sync(struct mtd_info *mtd)
  2014. {
  2015. struct nand_chip *chip = mtd->priv;
  2016. MTDDEBUG (MTD_DEBUG_LEVEL3, "nand_sync: called\n");
  2017. /* Grab the lock and see if the device is available */
  2018. nand_get_device(chip, mtd, FL_SYNCING);
  2019. /* Release it and go back */
  2020. nand_release_device(mtd);
  2021. }
  2022. /**
  2023. * nand_block_isbad - [MTD Interface] Check if block at offset is bad
  2024. * @mtd: MTD device structure
  2025. * @offs: offset relative to mtd start
  2026. */
  2027. static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
  2028. {
  2029. /* Check for invalid offset */
  2030. if (offs > mtd->size)
  2031. return -EINVAL;
  2032. return nand_block_checkbad(mtd, offs, 1, 0);
  2033. }
  2034. /**
  2035. * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
  2036. * @mtd: MTD device structure
  2037. * @ofs: offset relative to mtd start
  2038. */
  2039. static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
  2040. {
  2041. struct nand_chip *chip = mtd->priv;
  2042. int ret;
  2043. if ((ret = nand_block_isbad(mtd, ofs))) {
  2044. /* If it was bad already, return success and do nothing. */
  2045. if (ret > 0)
  2046. return 0;
  2047. return ret;
  2048. }
  2049. return chip->block_markbad(mtd, ofs);
  2050. }
  2051. /**
  2052. * nand_suspend - [MTD Interface] Suspend the NAND flash
  2053. * @mtd: MTD device structure
  2054. */
  2055. static int nand_suspend(struct mtd_info *mtd)
  2056. {
  2057. struct nand_chip *chip = mtd->priv;
  2058. return nand_get_device(chip, mtd, FL_PM_SUSPENDED);
  2059. }
  2060. /**
  2061. * nand_resume - [MTD Interface] Resume the NAND flash
  2062. * @mtd: MTD device structure
  2063. */
  2064. static void nand_resume(struct mtd_info *mtd)
  2065. {
  2066. struct nand_chip *chip = mtd->priv;
  2067. if (chip->state == FL_PM_SUSPENDED)
  2068. nand_release_device(mtd);
  2069. else
  2070. printk(KERN_ERR "nand_resume() called for a chip which is not "
  2071. "in suspended state\n");
  2072. }
  2073. /*
  2074. * Set default functions
  2075. */
  2076. static void nand_set_defaults(struct nand_chip *chip, int busw)
  2077. {
  2078. /* check for proper chip_delay setup, set 20us if not */
  2079. if (!chip->chip_delay)
  2080. chip->chip_delay = 20;
  2081. /* check, if a user supplied command function given */
  2082. if (chip->cmdfunc == NULL)
  2083. chip->cmdfunc = nand_command;
  2084. /* check, if a user supplied wait function given */
  2085. if (chip->waitfunc == NULL)
  2086. chip->waitfunc = nand_wait;
  2087. if (!chip->select_chip)
  2088. chip->select_chip = nand_select_chip;
  2089. if (!chip->read_byte)
  2090. chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
  2091. if (!chip->read_word)
  2092. chip->read_word = nand_read_word;
  2093. if (!chip->block_bad)
  2094. chip->block_bad = nand_block_bad;
  2095. if (!chip->block_markbad)
  2096. chip->block_markbad = nand_default_block_markbad;
  2097. if (!chip->write_buf)
  2098. chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
  2099. if (!chip->read_buf)
  2100. chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
  2101. if (!chip->verify_buf)
  2102. chip->verify_buf = busw ? nand_verify_buf16 : nand_verify_buf;
  2103. if (!chip->scan_bbt)
  2104. chip->scan_bbt = nand_default_bbt;
  2105. if (!chip->controller) {
  2106. chip->controller = &chip->hwcontrol;
  2107. /* XXX U-BOOT XXX */
  2108. #if 0
  2109. spin_lock_init(&chip->controller->lock);
  2110. init_waitqueue_head(&chip->controller->wq);
  2111. #endif
  2112. }
  2113. }
  2114. /*
  2115. * Get the flash and manufacturer id and lookup if the type is supported
  2116. */
  2117. static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
  2118. struct nand_chip *chip,
  2119. int busw, int *maf_id)
  2120. {
  2121. struct nand_flash_dev *type = NULL;
  2122. int i, dev_id, maf_idx;
  2123. int tmp_id, tmp_manf;
  2124. /* Select the device */
  2125. chip->select_chip(mtd, 0);
  2126. /*
  2127. * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
  2128. * after power-up
  2129. */
  2130. chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
  2131. /* Send the command for reading device ID */
  2132. chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
  2133. /* Read manufacturer and device IDs */
  2134. *maf_id = chip->read_byte(mtd);
  2135. dev_id = chip->read_byte(mtd);
  2136. /* Try again to make sure, as some systems the bus-hold or other
  2137. * interface concerns can cause random data which looks like a
  2138. * possibly credible NAND flash to appear. If the two results do
  2139. * not match, ignore the device completely.
  2140. */
  2141. chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
  2142. /* Read manufacturer and device IDs */
  2143. tmp_manf = chip->read_byte(mtd);
  2144. tmp_id = chip->read_byte(mtd);
  2145. if (tmp_manf != *maf_id || tmp_id != dev_id) {
  2146. printk(KERN_INFO "%s: second ID read did not match "
  2147. "%02x,%02x against %02x,%02x\n", __func__,
  2148. *maf_id, dev_id, tmp_manf, tmp_id);
  2149. return ERR_PTR(-ENODEV);
  2150. }
  2151. /* Lookup the flash id */
  2152. for (i = 0; nand_flash_ids[i].name != NULL; i++) {
  2153. if (dev_id == nand_flash_ids[i].id) {
  2154. type = &nand_flash_ids[i];
  2155. break;
  2156. }
  2157. }
  2158. if (!type)
  2159. return ERR_PTR(-ENODEV);
  2160. if (!mtd->name)
  2161. mtd->name = type->name;
  2162. chip->chipsize = type->chipsize << 20;
  2163. /* Newer devices have all the information in additional id bytes */
  2164. if (!type->pagesize) {
  2165. int extid;
  2166. /* The 3rd id byte holds MLC / multichip data */
  2167. chip->cellinfo = chip->read_byte(mtd);
  2168. /* The 4th id byte is the important one */
  2169. extid = chip->read_byte(mtd);
  2170. /* Calc pagesize */
  2171. mtd->writesize = 1024 << (extid & 0x3);
  2172. extid >>= 2;
  2173. /* Calc oobsize */
  2174. mtd->oobsize = (8 << (extid & 0x01)) * (mtd->writesize >> 9);
  2175. extid >>= 2;
  2176. /* Calc blocksize. Blocksize is multiples of 64KiB */
  2177. mtd->erasesize = (64 * 1024) << (extid & 0x03);
  2178. extid >>= 2;
  2179. /* Get buswidth information */
  2180. busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0;
  2181. } else {
  2182. /*
  2183. * Old devices have chip data hardcoded in the device id table
  2184. */
  2185. mtd->erasesize = type->erasesize;
  2186. mtd->writesize = type->pagesize;
  2187. mtd->oobsize = mtd->writesize / 32;
  2188. busw = type->options & NAND_BUSWIDTH_16;
  2189. }
  2190. /* Try to identify manufacturer */
  2191. for (maf_idx = 0; nand_manuf_ids[maf_idx].id != 0x0; maf_idx++) {
  2192. if (nand_manuf_ids[maf_idx].id == *maf_id)
  2193. break;
  2194. }
  2195. /*
  2196. * Check, if buswidth is correct. Hardware drivers should set
  2197. * chip correct !
  2198. */
  2199. if (busw != (chip->options & NAND_BUSWIDTH_16)) {
  2200. printk(KERN_INFO "NAND device: Manufacturer ID:"
  2201. " 0x%02x, Chip ID: 0x%02x (%s %s)\n", *maf_id,
  2202. dev_id, nand_manuf_ids[maf_idx].name, mtd->name);
  2203. printk(KERN_WARNING "NAND bus width %d instead %d bit\n",
  2204. (chip->options & NAND_BUSWIDTH_16) ? 16 : 8,
  2205. busw ? 16 : 8);
  2206. return ERR_PTR(-EINVAL);
  2207. }
  2208. /* Calculate the address shift from the page size */
  2209. chip->page_shift = ffs(mtd->writesize) - 1;
  2210. /* Convert chipsize to number of pages per chip -1. */
  2211. chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
  2212. chip->bbt_erase_shift = chip->phys_erase_shift =
  2213. ffs(mtd->erasesize) - 1;
  2214. chip->chip_shift = ffs(chip->chipsize) - 1;
  2215. /* Set the bad block position */
  2216. chip->badblockpos = mtd->writesize > 512 ?
  2217. NAND_LARGE_BADBLOCK_POS : NAND_SMALL_BADBLOCK_POS;
  2218. /* Get chip options, preserve non chip based options */
  2219. chip->options &= ~NAND_CHIPOPTIONS_MSK;
  2220. chip->options |= type->options & NAND_CHIPOPTIONS_MSK;
  2221. /*
  2222. * Set chip as a default. Board drivers can override it, if necessary
  2223. */
  2224. chip->options |= NAND_NO_AUTOINCR;
  2225. /* Check if chip is a not a samsung device. Do not clear the
  2226. * options for chips which are not having an extended id.
  2227. */
  2228. if (*maf_id != NAND_MFR_SAMSUNG && !type->pagesize)
  2229. chip->options &= ~NAND_SAMSUNG_LP_OPTIONS;
  2230. /* Check for AND chips with 4 page planes */
  2231. if (chip->options & NAND_4PAGE_ARRAY)
  2232. chip->erase_cmd = multi_erase_cmd;
  2233. else
  2234. chip->erase_cmd = single_erase_cmd;
  2235. /* Do not replace user supplied command function ! */
  2236. if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
  2237. chip->cmdfunc = nand_command_lp;
  2238. MTDDEBUG (MTD_DEBUG_LEVEL0, "NAND device: Manufacturer ID:"
  2239. " 0x%02x, Chip ID: 0x%02x (%s %s)\n", *maf_id, dev_id,
  2240. nand_manuf_ids[maf_idx].name, type->name);
  2241. return type;
  2242. }
  2243. /**
  2244. * nand_scan_ident - [NAND Interface] Scan for the NAND device
  2245. * @mtd: MTD device structure
  2246. * @maxchips: Number of chips to scan for
  2247. *
  2248. * This is the first phase of the normal nand_scan() function. It
  2249. * reads the flash ID and sets up MTD fields accordingly.
  2250. *
  2251. * The mtd->owner field must be set to the module of the caller.
  2252. */
  2253. int nand_scan_ident(struct mtd_info *mtd, int maxchips)
  2254. {
  2255. int i, busw, nand_maf_id;
  2256. struct nand_chip *chip = mtd->priv;
  2257. struct nand_flash_dev *type;
  2258. /* Get buswidth to select the correct functions */
  2259. busw = chip->options & NAND_BUSWIDTH_16;
  2260. /* Set the default functions */
  2261. nand_set_defaults(chip, busw);
  2262. /* Read the flash type */
  2263. type = nand_get_flash_type(mtd, chip, busw, &nand_maf_id);
  2264. if (IS_ERR(type)) {
  2265. #ifndef CONFIG_SYS_NAND_QUIET_TEST
  2266. printk(KERN_WARNING "No NAND device found!!!\n");
  2267. #endif
  2268. chip->select_chip(mtd, -1);
  2269. return PTR_ERR(type);
  2270. }
  2271. /* Check for a chip array */
  2272. for (i = 1; i < maxchips; i++) {
  2273. chip->select_chip(mtd, i);
  2274. /* See comment in nand_get_flash_type for reset */
  2275. chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
  2276. /* Send the command for reading device ID */
  2277. chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
  2278. /* Read manufacturer and device IDs */
  2279. if (nand_maf_id != chip->read_byte(mtd) ||
  2280. type->id != chip->read_byte(mtd))
  2281. break;
  2282. }
  2283. #ifdef DEBUG
  2284. if (i > 1)
  2285. printk(KERN_INFO "%d NAND chips detected\n", i);
  2286. #endif
  2287. /* Store the number of chips and calc total size for mtd */
  2288. chip->numchips = i;
  2289. mtd->size = i * chip->chipsize;
  2290. return 0;
  2291. }
  2292. /**
  2293. * nand_scan_tail - [NAND Interface] Scan for the NAND device
  2294. * @mtd: MTD device structure
  2295. * @maxchips: Number of chips to scan for
  2296. *
  2297. * This is the second phase of the normal nand_scan() function. It
  2298. * fills out all the uninitialized function pointers with the defaults
  2299. * and scans for a bad block table if appropriate.
  2300. */
  2301. int nand_scan_tail(struct mtd_info *mtd)
  2302. {
  2303. int i;
  2304. struct nand_chip *chip = mtd->priv;
  2305. if (!(chip->options & NAND_OWN_BUFFERS))
  2306. chip->buffers = kmalloc(sizeof(*chip->buffers), GFP_KERNEL);
  2307. if (!chip->buffers)
  2308. return -ENOMEM;
  2309. /* Set the internal oob buffer location, just after the page data */
  2310. chip->oob_poi = chip->buffers->databuf + mtd->writesize;
  2311. /*
  2312. * If no default placement scheme is given, select an appropriate one
  2313. */
  2314. if (!chip->ecc.layout) {
  2315. switch (mtd->oobsize) {
  2316. case 8:
  2317. chip->ecc.layout = &nand_oob_8;
  2318. break;
  2319. case 16:
  2320. chip->ecc.layout = &nand_oob_16;
  2321. break;
  2322. case 64:
  2323. chip->ecc.layout = &nand_oob_64;
  2324. break;
  2325. case 128:
  2326. chip->ecc.layout = &nand_oob_128;
  2327. break;
  2328. default:
  2329. printk(KERN_WARNING "No oob scheme defined for "
  2330. "oobsize %d\n", mtd->oobsize);
  2331. /* BUG(); */
  2332. }
  2333. }
  2334. if (!chip->write_page)
  2335. chip->write_page = nand_write_page;
  2336. /*
  2337. * check ECC mode, default to software if 3byte/512byte hardware ECC is
  2338. * selected and we have 256 byte pagesize fallback to software ECC
  2339. */
  2340. if (!chip->ecc.read_page_raw)
  2341. chip->ecc.read_page_raw = nand_read_page_raw;
  2342. if (!chip->ecc.write_page_raw)
  2343. chip->ecc.write_page_raw = nand_write_page_raw;
  2344. switch (chip->ecc.mode) {
  2345. case NAND_ECC_HW:
  2346. /* Use standard hwecc read page function ? */
  2347. if (!chip->ecc.read_page)
  2348. chip->ecc.read_page = nand_read_page_hwecc;
  2349. if (!chip->ecc.write_page)
  2350. chip->ecc.write_page = nand_write_page_hwecc;
  2351. if (!chip->ecc.read_oob)
  2352. chip->ecc.read_oob = nand_read_oob_std;
  2353. if (!chip->ecc.write_oob)
  2354. chip->ecc.write_oob = nand_write_oob_std;
  2355. case NAND_ECC_HW_SYNDROME:
  2356. if ((!chip->ecc.calculate || !chip->ecc.correct ||
  2357. !chip->ecc.hwctl) &&
  2358. (!chip->ecc.read_page ||
  2359. chip->ecc.read_page == nand_read_page_hwecc ||
  2360. !chip->ecc.write_page ||
  2361. chip->ecc.write_page == nand_write_page_hwecc)) {
  2362. printk(KERN_WARNING "No ECC functions supplied, "
  2363. "Hardware ECC not possible\n");
  2364. BUG();
  2365. }
  2366. /* Use standard syndrome read/write page function ? */
  2367. if (!chip->ecc.read_page)
  2368. chip->ecc.read_page = nand_read_page_syndrome;
  2369. if (!chip->ecc.write_page)
  2370. chip->ecc.write_page = nand_write_page_syndrome;
  2371. if (!chip->ecc.read_oob)
  2372. chip->ecc.read_oob = nand_read_oob_syndrome;
  2373. if (!chip->ecc.write_oob)
  2374. chip->ecc.write_oob = nand_write_oob_syndrome;
  2375. if (mtd->writesize >= chip->ecc.size)
  2376. break;
  2377. printk(KERN_WARNING "%d byte HW ECC not possible on "
  2378. "%d byte page size, fallback to SW ECC\n",
  2379. chip->ecc.size, mtd->writesize);
  2380. chip->ecc.mode = NAND_ECC_SOFT;
  2381. case NAND_ECC_SOFT:
  2382. chip->ecc.calculate = nand_calculate_ecc;
  2383. chip->ecc.correct = nand_correct_data;
  2384. chip->ecc.read_page = nand_read_page_swecc;
  2385. chip->ecc.read_subpage = nand_read_subpage;
  2386. chip->ecc.write_page = nand_write_page_swecc;
  2387. chip->ecc.read_oob = nand_read_oob_std;
  2388. chip->ecc.write_oob = nand_write_oob_std;
  2389. chip->ecc.size = 256;
  2390. chip->ecc.bytes = 3;
  2391. break;
  2392. case NAND_ECC_NONE:
  2393. printk(KERN_WARNING "NAND_ECC_NONE selected by board driver. "
  2394. "This is not recommended !!\n");
  2395. chip->ecc.read_page = nand_read_page_raw;
  2396. chip->ecc.write_page = nand_write_page_raw;
  2397. chip->ecc.read_oob = nand_read_oob_std;
  2398. chip->ecc.write_oob = nand_write_oob_std;
  2399. chip->ecc.size = mtd->writesize;
  2400. chip->ecc.bytes = 0;
  2401. break;
  2402. default:
  2403. printk(KERN_WARNING "Invalid NAND_ECC_MODE %d\n",
  2404. chip->ecc.mode);
  2405. BUG();
  2406. }
  2407. /*
  2408. * The number of bytes available for a client to place data into
  2409. * the out of band area
  2410. */
  2411. chip->ecc.layout->oobavail = 0;
  2412. for (i = 0; chip->ecc.layout->oobfree[i].length; i++)
  2413. chip->ecc.layout->oobavail +=
  2414. chip->ecc.layout->oobfree[i].length;
  2415. mtd->oobavail = chip->ecc.layout->oobavail;
  2416. /*
  2417. * Set the number of read / write steps for one page depending on ECC
  2418. * mode
  2419. */
  2420. chip->ecc.steps = mtd->writesize / chip->ecc.size;
  2421. if(chip->ecc.steps * chip->ecc.size != mtd->writesize) {
  2422. printk(KERN_WARNING "Invalid ecc parameters\n");
  2423. BUG();
  2424. }
  2425. chip->ecc.total = chip->ecc.steps * chip->ecc.bytes;
  2426. /*
  2427. * Allow subpage writes up to ecc.steps. Not possible for MLC
  2428. * FLASH.
  2429. */
  2430. if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
  2431. !(chip->cellinfo & NAND_CI_CELLTYPE_MSK)) {
  2432. switch(chip->ecc.steps) {
  2433. case 2:
  2434. mtd->subpage_sft = 1;
  2435. break;
  2436. case 4:
  2437. case 8:
  2438. mtd->subpage_sft = 2;
  2439. break;
  2440. }
  2441. }
  2442. chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
  2443. /* Initialize state */
  2444. chip->state = FL_READY;
  2445. /* De-select the device */
  2446. chip->select_chip(mtd, -1);
  2447. /* Invalidate the pagebuffer reference */
  2448. chip->pagebuf = -1;
  2449. /* Fill in remaining MTD driver data */
  2450. mtd->type = MTD_NANDFLASH;
  2451. mtd->flags = MTD_CAP_NANDFLASH;
  2452. mtd->erase = nand_erase;
  2453. mtd->point = NULL;
  2454. mtd->unpoint = NULL;
  2455. mtd->read = nand_read;
  2456. mtd->write = nand_write;
  2457. mtd->read_oob = nand_read_oob;
  2458. mtd->write_oob = nand_write_oob;
  2459. mtd->sync = nand_sync;
  2460. mtd->lock = NULL;
  2461. mtd->unlock = NULL;
  2462. mtd->suspend = nand_suspend;
  2463. mtd->resume = nand_resume;
  2464. mtd->block_isbad = nand_block_isbad;
  2465. mtd->block_markbad = nand_block_markbad;
  2466. /* propagate ecc.layout to mtd_info */
  2467. mtd->ecclayout = chip->ecc.layout;
  2468. /* Check, if we should skip the bad block table scan */
  2469. if (chip->options & NAND_SKIP_BBTSCAN)
  2470. chip->options |= NAND_BBT_SCANNED;
  2471. return 0;
  2472. }
  2473. /* module_text_address() isn't exported, and it's mostly a pointless
  2474. test if this is a module _anyway_ -- they'd have to try _really_ hard
  2475. to call us from in-kernel code if the core NAND support is modular. */
  2476. #ifdef MODULE
  2477. #define caller_is_module() (1)
  2478. #else
  2479. #define caller_is_module() \
  2480. module_text_address((unsigned long)__builtin_return_address(0))
  2481. #endif
  2482. /**
  2483. * nand_scan - [NAND Interface] Scan for the NAND device
  2484. * @mtd: MTD device structure
  2485. * @maxchips: Number of chips to scan for
  2486. *
  2487. * This fills out all the uninitialized function pointers
  2488. * with the defaults.
  2489. * The flash ID is read and the mtd/chip structures are
  2490. * filled with the appropriate values.
  2491. * The mtd->owner field must be set to the module of the caller
  2492. *
  2493. */
  2494. int nand_scan(struct mtd_info *mtd, int maxchips)
  2495. {
  2496. int ret;
  2497. /* Many callers got this wrong, so check for it for a while... */
  2498. /* XXX U-BOOT XXX */
  2499. #if 0
  2500. if (!mtd->owner && caller_is_module()) {
  2501. printk(KERN_CRIT "nand_scan() called with NULL mtd->owner!\n");
  2502. BUG();
  2503. }
  2504. #endif
  2505. ret = nand_scan_ident(mtd, maxchips);
  2506. if (!ret)
  2507. ret = nand_scan_tail(mtd);
  2508. return ret;
  2509. }
  2510. /**
  2511. * nand_release - [NAND Interface] Free resources held by the NAND device
  2512. * @mtd: MTD device structure
  2513. */
  2514. void nand_release(struct mtd_info *mtd)
  2515. {
  2516. struct nand_chip *chip = mtd->priv;
  2517. #ifdef CONFIG_MTD_PARTITIONS
  2518. /* Deregister partitions */
  2519. del_mtd_partitions(mtd);
  2520. #endif
  2521. /* Deregister the device */
  2522. /* XXX U-BOOT XXX */
  2523. #if 0
  2524. del_mtd_device(mtd);
  2525. #endif
  2526. /* Free bad block table memory */
  2527. kfree(chip->bbt);
  2528. if (!(chip->options & NAND_OWN_BUFFERS))
  2529. kfree(chip->buffers);
  2530. }
  2531. /* XXX U-BOOT XXX */
  2532. #if 0
  2533. EXPORT_SYMBOL_GPL(nand_scan);
  2534. EXPORT_SYMBOL_GPL(nand_scan_ident);
  2535. EXPORT_SYMBOL_GPL(nand_scan_tail);
  2536. EXPORT_SYMBOL_GPL(nand_release);
  2537. static int __init nand_base_init(void)
  2538. {
  2539. led_trigger_register_simple("nand-disk", &nand_led_trigger);
  2540. return 0;
  2541. }
  2542. static void __exit nand_base_exit(void)
  2543. {
  2544. led_trigger_unregister_simple(nand_led_trigger);
  2545. }
  2546. module_init(nand_base_init);
  2547. module_exit(nand_base_exit);
  2548. MODULE_LICENSE("GPL");
  2549. MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>, Thomas Gleixner <tglx@linutronix.de>");
  2550. MODULE_DESCRIPTION("Generic NAND flash driver code");
  2551. #endif