nand_base.c 92 KB

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