nand_base.c 92 KB

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