nand_base.c 80 KB

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