nand_base.c 80 KB

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