nand_base.c 75 KB

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