nand_base.c 92 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587
  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. chip->ecc.read_page_raw(mtd, chip, buf, page);
  1000. for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
  1001. chip->ecc.calculate(mtd, p, &ecc_calc[i]);
  1002. for (i = 0; i < chip->ecc.total; i++)
  1003. ecc_code[i] = chip->oob_poi[eccpos[i]];
  1004. eccsteps = chip->ecc.steps;
  1005. p = buf;
  1006. for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
  1007. int stat;
  1008. stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
  1009. if (stat < 0)
  1010. mtd->ecc_stats.failed++;
  1011. else
  1012. mtd->ecc_stats.corrected += stat;
  1013. }
  1014. return 0;
  1015. }
  1016. /**
  1017. * nand_read_subpage - [REPLACEABLE] software ECC based sub-page read function
  1018. * @mtd: mtd info structure
  1019. * @chip: nand chip info structure
  1020. * @data_offs: offset of requested data within the page
  1021. * @readlen: data length
  1022. * @bufpoi: buffer to store read data
  1023. */
  1024. static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip,
  1025. uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi)
  1026. {
  1027. int start_step, end_step, num_steps;
  1028. uint32_t *eccpos = chip->ecc.layout->eccpos;
  1029. uint8_t *p;
  1030. int data_col_addr, i, gaps = 0;
  1031. int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
  1032. int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
  1033. int index = 0;
  1034. /* Column address within the page aligned to ECC size (256bytes) */
  1035. start_step = data_offs / chip->ecc.size;
  1036. end_step = (data_offs + readlen - 1) / chip->ecc.size;
  1037. num_steps = end_step - start_step + 1;
  1038. /* Data size aligned to ECC ecc.size */
  1039. datafrag_len = num_steps * chip->ecc.size;
  1040. eccfrag_len = num_steps * chip->ecc.bytes;
  1041. data_col_addr = start_step * chip->ecc.size;
  1042. /* If we read not a page aligned data */
  1043. if (data_col_addr != 0)
  1044. chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_col_addr, -1);
  1045. p = bufpoi + data_col_addr;
  1046. chip->read_buf(mtd, p, datafrag_len);
  1047. /* Calculate ECC */
  1048. for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
  1049. chip->ecc.calculate(mtd, p, &chip->buffers->ecccalc[i]);
  1050. /*
  1051. * The performance is faster if we position offsets according to
  1052. * ecc.pos. Let's make sure that there are no gaps in ECC positions.
  1053. */
  1054. for (i = 0; i < eccfrag_len - 1; i++) {
  1055. if (eccpos[i + start_step * chip->ecc.bytes] + 1 !=
  1056. eccpos[i + start_step * chip->ecc.bytes + 1]) {
  1057. gaps = 1;
  1058. break;
  1059. }
  1060. }
  1061. if (gaps) {
  1062. chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
  1063. chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
  1064. } else {
  1065. /*
  1066. * Send the command to read the particular ECC bytes take care
  1067. * about buswidth alignment in read_buf.
  1068. */
  1069. index = start_step * chip->ecc.bytes;
  1070. aligned_pos = eccpos[index] & ~(busw - 1);
  1071. aligned_len = eccfrag_len;
  1072. if (eccpos[index] & (busw - 1))
  1073. aligned_len++;
  1074. if (eccpos[index + (num_steps * chip->ecc.bytes)] & (busw - 1))
  1075. aligned_len++;
  1076. chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
  1077. mtd->writesize + aligned_pos, -1);
  1078. chip->read_buf(mtd, &chip->oob_poi[aligned_pos], aligned_len);
  1079. }
  1080. for (i = 0; i < eccfrag_len; i++)
  1081. chip->buffers->ecccode[i] = chip->oob_poi[eccpos[i + index]];
  1082. p = bufpoi + data_col_addr;
  1083. for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
  1084. int stat;
  1085. stat = chip->ecc.correct(mtd, p,
  1086. &chip->buffers->ecccode[i], &chip->buffers->ecccalc[i]);
  1087. if (stat < 0)
  1088. mtd->ecc_stats.failed++;
  1089. else
  1090. mtd->ecc_stats.corrected += stat;
  1091. }
  1092. return 0;
  1093. }
  1094. /**
  1095. * nand_read_page_hwecc - [REPLACEABLE] hardware ECC based page read function
  1096. * @mtd: mtd info structure
  1097. * @chip: nand chip info structure
  1098. * @buf: buffer to store read data
  1099. * @page: page number to read
  1100. *
  1101. * Not for syndrome calculating ECC controllers which need a special oob layout.
  1102. */
  1103. static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
  1104. uint8_t *buf, int page)
  1105. {
  1106. int i, eccsize = chip->ecc.size;
  1107. int eccbytes = chip->ecc.bytes;
  1108. int eccsteps = chip->ecc.steps;
  1109. uint8_t *p = buf;
  1110. uint8_t *ecc_calc = chip->buffers->ecccalc;
  1111. uint8_t *ecc_code = chip->buffers->ecccode;
  1112. uint32_t *eccpos = chip->ecc.layout->eccpos;
  1113. for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
  1114. chip->ecc.hwctl(mtd, NAND_ECC_READ);
  1115. chip->read_buf(mtd, p, eccsize);
  1116. chip->ecc.calculate(mtd, p, &ecc_calc[i]);
  1117. }
  1118. chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
  1119. for (i = 0; i < chip->ecc.total; i++)
  1120. ecc_code[i] = chip->oob_poi[eccpos[i]];
  1121. eccsteps = chip->ecc.steps;
  1122. p = buf;
  1123. for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
  1124. int stat;
  1125. stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
  1126. if (stat < 0)
  1127. mtd->ecc_stats.failed++;
  1128. else
  1129. mtd->ecc_stats.corrected += stat;
  1130. }
  1131. return 0;
  1132. }
  1133. /**
  1134. * nand_read_page_hwecc_oob_first - [REPLACEABLE] hw ecc, read oob first
  1135. * @mtd: mtd info structure
  1136. * @chip: nand chip info structure
  1137. * @buf: buffer to store read data
  1138. * @page: page number to read
  1139. *
  1140. * Hardware ECC for large page chips, require OOB to be read first. For this
  1141. * ECC mode, the write_page method is re-used from ECC_HW. These methods
  1142. * read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with
  1143. * multiple ECC steps, follows the "infix ECC" scheme and reads/writes ECC from
  1144. * the data area, by overwriting the NAND manufacturer bad block markings.
  1145. */
  1146. static int nand_read_page_hwecc_oob_first(struct mtd_info *mtd,
  1147. struct nand_chip *chip, uint8_t *buf, int page)
  1148. {
  1149. int i, eccsize = chip->ecc.size;
  1150. int eccbytes = chip->ecc.bytes;
  1151. int eccsteps = chip->ecc.steps;
  1152. uint8_t *p = buf;
  1153. uint8_t *ecc_code = chip->buffers->ecccode;
  1154. uint32_t *eccpos = chip->ecc.layout->eccpos;
  1155. uint8_t *ecc_calc = chip->buffers->ecccalc;
  1156. /* Read the OOB area first */
  1157. chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
  1158. chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
  1159. chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
  1160. for (i = 0; i < chip->ecc.total; i++)
  1161. ecc_code[i] = chip->oob_poi[eccpos[i]];
  1162. for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
  1163. int stat;
  1164. chip->ecc.hwctl(mtd, NAND_ECC_READ);
  1165. chip->read_buf(mtd, p, eccsize);
  1166. chip->ecc.calculate(mtd, p, &ecc_calc[i]);
  1167. stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL);
  1168. if (stat < 0)
  1169. mtd->ecc_stats.failed++;
  1170. else
  1171. mtd->ecc_stats.corrected += stat;
  1172. }
  1173. return 0;
  1174. }
  1175. /**
  1176. * nand_read_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page read
  1177. * @mtd: mtd info structure
  1178. * @chip: nand chip info structure
  1179. * @buf: buffer to store read data
  1180. * @page: page number to read
  1181. *
  1182. * The hw generator calculates the error syndrome automatically. Therefore we
  1183. * need a special oob layout and handling.
  1184. */
  1185. static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
  1186. uint8_t *buf, int page)
  1187. {
  1188. int i, eccsize = chip->ecc.size;
  1189. int eccbytes = chip->ecc.bytes;
  1190. int eccsteps = chip->ecc.steps;
  1191. uint8_t *p = buf;
  1192. uint8_t *oob = chip->oob_poi;
  1193. for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
  1194. int stat;
  1195. chip->ecc.hwctl(mtd, NAND_ECC_READ);
  1196. chip->read_buf(mtd, p, eccsize);
  1197. if (chip->ecc.prepad) {
  1198. chip->read_buf(mtd, oob, chip->ecc.prepad);
  1199. oob += chip->ecc.prepad;
  1200. }
  1201. chip->ecc.hwctl(mtd, NAND_ECC_READSYN);
  1202. chip->read_buf(mtd, oob, eccbytes);
  1203. stat = chip->ecc.correct(mtd, p, oob, NULL);
  1204. if (stat < 0)
  1205. mtd->ecc_stats.failed++;
  1206. else
  1207. mtd->ecc_stats.corrected += stat;
  1208. oob += eccbytes;
  1209. if (chip->ecc.postpad) {
  1210. chip->read_buf(mtd, oob, chip->ecc.postpad);
  1211. oob += chip->ecc.postpad;
  1212. }
  1213. }
  1214. /* Calculate remaining oob bytes */
  1215. i = mtd->oobsize - (oob - chip->oob_poi);
  1216. if (i)
  1217. chip->read_buf(mtd, oob, i);
  1218. return 0;
  1219. }
  1220. /**
  1221. * nand_transfer_oob - [INTERN] Transfer oob to client buffer
  1222. * @chip: nand chip structure
  1223. * @oob: oob destination address
  1224. * @ops: oob ops structure
  1225. * @len: size of oob to transfer
  1226. */
  1227. static uint8_t *nand_transfer_oob(struct nand_chip *chip, uint8_t *oob,
  1228. struct mtd_oob_ops *ops, size_t len)
  1229. {
  1230. switch (ops->mode) {
  1231. case MTD_OPS_PLACE_OOB:
  1232. case MTD_OPS_RAW:
  1233. memcpy(oob, chip->oob_poi + ops->ooboffs, len);
  1234. return oob + len;
  1235. case MTD_OPS_AUTO_OOB: {
  1236. struct nand_oobfree *free = chip->ecc.layout->oobfree;
  1237. uint32_t boffs = 0, roffs = ops->ooboffs;
  1238. size_t bytes = 0;
  1239. for (; free->length && len; free++, len -= bytes) {
  1240. /* Read request not from offset 0? */
  1241. if (unlikely(roffs)) {
  1242. if (roffs >= free->length) {
  1243. roffs -= free->length;
  1244. continue;
  1245. }
  1246. boffs = free->offset + roffs;
  1247. bytes = min_t(size_t, len,
  1248. (free->length - roffs));
  1249. roffs = 0;
  1250. } else {
  1251. bytes = min_t(size_t, len, free->length);
  1252. boffs = free->offset;
  1253. }
  1254. memcpy(oob, chip->oob_poi + boffs, bytes);
  1255. oob += bytes;
  1256. }
  1257. return oob;
  1258. }
  1259. default:
  1260. BUG();
  1261. }
  1262. return NULL;
  1263. }
  1264. /**
  1265. * nand_do_read_ops - [INTERN] Read data with ECC
  1266. * @mtd: MTD device structure
  1267. * @from: offset to read from
  1268. * @ops: oob ops structure
  1269. *
  1270. * Internal function. Called with chip held.
  1271. */
  1272. static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
  1273. struct mtd_oob_ops *ops)
  1274. {
  1275. int chipnr, page, realpage, col, bytes, aligned;
  1276. struct nand_chip *chip = mtd->priv;
  1277. struct mtd_ecc_stats stats;
  1278. int blkcheck = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
  1279. int sndcmd = 1;
  1280. int ret = 0;
  1281. uint32_t readlen = ops->len;
  1282. uint32_t oobreadlen = ops->ooblen;
  1283. uint32_t max_oobsize = ops->mode == MTD_OPS_AUTO_OOB ?
  1284. mtd->oobavail : mtd->oobsize;
  1285. uint8_t *bufpoi, *oob, *buf;
  1286. stats = mtd->ecc_stats;
  1287. chipnr = (int)(from >> chip->chip_shift);
  1288. chip->select_chip(mtd, chipnr);
  1289. realpage = (int)(from >> chip->page_shift);
  1290. page = realpage & chip->pagemask;
  1291. col = (int)(from & (mtd->writesize - 1));
  1292. buf = ops->datbuf;
  1293. oob = ops->oobbuf;
  1294. while (1) {
  1295. bytes = min(mtd->writesize - col, readlen);
  1296. aligned = (bytes == mtd->writesize);
  1297. /* Is the current page in the buffer? */
  1298. if (realpage != chip->pagebuf || oob) {
  1299. bufpoi = aligned ? buf : chip->buffers->databuf;
  1300. if (likely(sndcmd)) {
  1301. chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
  1302. sndcmd = 0;
  1303. }
  1304. /* Now read the page into the buffer */
  1305. if (unlikely(ops->mode == MTD_OPS_RAW))
  1306. ret = chip->ecc.read_page_raw(mtd, chip,
  1307. bufpoi, page);
  1308. else if (!aligned && NAND_SUBPAGE_READ(chip) && !oob)
  1309. ret = chip->ecc.read_subpage(mtd, chip,
  1310. col, bytes, bufpoi);
  1311. else
  1312. ret = chip->ecc.read_page(mtd, chip, bufpoi,
  1313. page);
  1314. if (ret < 0) {
  1315. if (!aligned)
  1316. /* Invalidate page cache */
  1317. chip->pagebuf = -1;
  1318. break;
  1319. }
  1320. /* Transfer not aligned data */
  1321. if (!aligned) {
  1322. if (!NAND_SUBPAGE_READ(chip) && !oob &&
  1323. !(mtd->ecc_stats.failed - stats.failed) &&
  1324. (ops->mode != MTD_OPS_RAW))
  1325. chip->pagebuf = realpage;
  1326. else
  1327. /* Invalidate page cache */
  1328. chip->pagebuf = -1;
  1329. memcpy(buf, chip->buffers->databuf + col, bytes);
  1330. }
  1331. buf += bytes;
  1332. if (unlikely(oob)) {
  1333. int toread = min(oobreadlen, max_oobsize);
  1334. if (toread) {
  1335. oob = nand_transfer_oob(chip,
  1336. oob, ops, toread);
  1337. oobreadlen -= toread;
  1338. }
  1339. }
  1340. if (!(chip->options & NAND_NO_READRDY)) {
  1341. /*
  1342. * Apply delay or wait for ready/busy pin. Do
  1343. * this before the AUTOINCR check, so no
  1344. * problems arise if a chip which does auto
  1345. * increment is marked as NOAUTOINCR by the
  1346. * board driver.
  1347. */
  1348. if (!chip->dev_ready)
  1349. udelay(chip->chip_delay);
  1350. else
  1351. nand_wait_ready(mtd);
  1352. }
  1353. } else {
  1354. memcpy(buf, chip->buffers->databuf + col, bytes);
  1355. buf += bytes;
  1356. }
  1357. readlen -= bytes;
  1358. if (!readlen)
  1359. break;
  1360. /* For subsequent reads align to page boundary */
  1361. col = 0;
  1362. /* Increment page address */
  1363. realpage++;
  1364. page = realpage & chip->pagemask;
  1365. /* Check, if we cross a chip boundary */
  1366. if (!page) {
  1367. chipnr++;
  1368. chip->select_chip(mtd, -1);
  1369. chip->select_chip(mtd, chipnr);
  1370. }
  1371. /*
  1372. * Check, if the chip supports auto page increment or if we
  1373. * have hit a block boundary.
  1374. */
  1375. if (!NAND_CANAUTOINCR(chip) || !(page & blkcheck))
  1376. sndcmd = 1;
  1377. }
  1378. ops->retlen = ops->len - (size_t) readlen;
  1379. if (oob)
  1380. ops->oobretlen = ops->ooblen - oobreadlen;
  1381. if (ret)
  1382. return ret;
  1383. if (mtd->ecc_stats.failed - stats.failed)
  1384. return -EBADMSG;
  1385. return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
  1386. }
  1387. /**
  1388. * nand_read - [MTD Interface] MTD compatibility function for nand_do_read_ecc
  1389. * @mtd: MTD device structure
  1390. * @from: offset to read from
  1391. * @len: number of bytes to read
  1392. * @retlen: pointer to variable to store the number of read bytes
  1393. * @buf: the databuffer to put data
  1394. *
  1395. * Get hold of the chip and call nand_do_read.
  1396. */
  1397. static int nand_read(struct mtd_info *mtd, loff_t from, size_t len,
  1398. size_t *retlen, uint8_t *buf)
  1399. {
  1400. struct nand_chip *chip = mtd->priv;
  1401. struct mtd_oob_ops ops;
  1402. int ret;
  1403. nand_get_device(chip, mtd, FL_READING);
  1404. ops.len = len;
  1405. ops.datbuf = buf;
  1406. ops.oobbuf = NULL;
  1407. ops.mode = 0;
  1408. ret = nand_do_read_ops(mtd, from, &ops);
  1409. *retlen = ops.retlen;
  1410. nand_release_device(mtd);
  1411. return ret;
  1412. }
  1413. /**
  1414. * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function
  1415. * @mtd: mtd info structure
  1416. * @chip: nand chip info structure
  1417. * @page: page number to read
  1418. * @sndcmd: flag whether to issue read command or not
  1419. */
  1420. static int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
  1421. int page, int sndcmd)
  1422. {
  1423. if (sndcmd) {
  1424. chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
  1425. sndcmd = 0;
  1426. }
  1427. chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
  1428. return sndcmd;
  1429. }
  1430. /**
  1431. * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC
  1432. * with syndromes
  1433. * @mtd: mtd info structure
  1434. * @chip: nand chip info structure
  1435. * @page: page number to read
  1436. * @sndcmd: flag whether to issue read command or not
  1437. */
  1438. static int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
  1439. int page, int sndcmd)
  1440. {
  1441. uint8_t *buf = chip->oob_poi;
  1442. int length = mtd->oobsize;
  1443. int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
  1444. int eccsize = chip->ecc.size;
  1445. uint8_t *bufpoi = buf;
  1446. int i, toread, sndrnd = 0, pos;
  1447. chip->cmdfunc(mtd, NAND_CMD_READ0, chip->ecc.size, page);
  1448. for (i = 0; i < chip->ecc.steps; i++) {
  1449. if (sndrnd) {
  1450. pos = eccsize + i * (eccsize + chunk);
  1451. if (mtd->writesize > 512)
  1452. chip->cmdfunc(mtd, NAND_CMD_RNDOUT, pos, -1);
  1453. else
  1454. chip->cmdfunc(mtd, NAND_CMD_READ0, pos, page);
  1455. } else
  1456. sndrnd = 1;
  1457. toread = min_t(int, length, chunk);
  1458. chip->read_buf(mtd, bufpoi, toread);
  1459. bufpoi += toread;
  1460. length -= toread;
  1461. }
  1462. if (length > 0)
  1463. chip->read_buf(mtd, bufpoi, length);
  1464. return 1;
  1465. }
  1466. /**
  1467. * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function
  1468. * @mtd: mtd info structure
  1469. * @chip: nand chip info structure
  1470. * @page: page number to write
  1471. */
  1472. static int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
  1473. int page)
  1474. {
  1475. int status = 0;
  1476. const uint8_t *buf = chip->oob_poi;
  1477. int length = mtd->oobsize;
  1478. chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
  1479. chip->write_buf(mtd, buf, length);
  1480. /* Send command to program the OOB data */
  1481. chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
  1482. status = chip->waitfunc(mtd, chip);
  1483. return status & NAND_STATUS_FAIL ? -EIO : 0;
  1484. }
  1485. /**
  1486. * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC
  1487. * with syndrome - only for large page flash
  1488. * @mtd: mtd info structure
  1489. * @chip: nand chip info structure
  1490. * @page: page number to write
  1491. */
  1492. static int nand_write_oob_syndrome(struct mtd_info *mtd,
  1493. struct nand_chip *chip, int page)
  1494. {
  1495. int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
  1496. int eccsize = chip->ecc.size, length = mtd->oobsize;
  1497. int i, len, pos, status = 0, sndcmd = 0, steps = chip->ecc.steps;
  1498. const uint8_t *bufpoi = chip->oob_poi;
  1499. /*
  1500. * data-ecc-data-ecc ... ecc-oob
  1501. * or
  1502. * data-pad-ecc-pad-data-pad .... ecc-pad-oob
  1503. */
  1504. if (!chip->ecc.prepad && !chip->ecc.postpad) {
  1505. pos = steps * (eccsize + chunk);
  1506. steps = 0;
  1507. } else
  1508. pos = eccsize;
  1509. chip->cmdfunc(mtd, NAND_CMD_SEQIN, pos, page);
  1510. for (i = 0; i < steps; i++) {
  1511. if (sndcmd) {
  1512. if (mtd->writesize <= 512) {
  1513. uint32_t fill = 0xFFFFFFFF;
  1514. len = eccsize;
  1515. while (len > 0) {
  1516. int num = min_t(int, len, 4);
  1517. chip->write_buf(mtd, (uint8_t *)&fill,
  1518. num);
  1519. len -= num;
  1520. }
  1521. } else {
  1522. pos = eccsize + i * (eccsize + chunk);
  1523. chip->cmdfunc(mtd, NAND_CMD_RNDIN, pos, -1);
  1524. }
  1525. } else
  1526. sndcmd = 1;
  1527. len = min_t(int, length, chunk);
  1528. chip->write_buf(mtd, bufpoi, len);
  1529. bufpoi += len;
  1530. length -= len;
  1531. }
  1532. if (length > 0)
  1533. chip->write_buf(mtd, bufpoi, length);
  1534. chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
  1535. status = chip->waitfunc(mtd, chip);
  1536. return status & NAND_STATUS_FAIL ? -EIO : 0;
  1537. }
  1538. /**
  1539. * nand_do_read_oob - [INTERN] NAND read out-of-band
  1540. * @mtd: MTD device structure
  1541. * @from: offset to read from
  1542. * @ops: oob operations description structure
  1543. *
  1544. * NAND read out-of-band data from the spare area.
  1545. */
  1546. static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
  1547. struct mtd_oob_ops *ops)
  1548. {
  1549. int page, realpage, chipnr, sndcmd = 1;
  1550. struct nand_chip *chip = mtd->priv;
  1551. struct mtd_ecc_stats stats;
  1552. int blkcheck = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
  1553. int readlen = ops->ooblen;
  1554. int len;
  1555. uint8_t *buf = ops->oobbuf;
  1556. pr_debug("%s: from = 0x%08Lx, len = %i\n",
  1557. __func__, (unsigned long long)from, readlen);
  1558. stats = mtd->ecc_stats;
  1559. if (ops->mode == MTD_OPS_AUTO_OOB)
  1560. len = chip->ecc.layout->oobavail;
  1561. else
  1562. len = mtd->oobsize;
  1563. if (unlikely(ops->ooboffs >= len)) {
  1564. pr_debug("%s: attempt to start read outside oob\n",
  1565. __func__);
  1566. return -EINVAL;
  1567. }
  1568. /* Do not allow reads past end of device */
  1569. if (unlikely(from >= mtd->size ||
  1570. ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) -
  1571. (from >> chip->page_shift)) * len)) {
  1572. pr_debug("%s: attempt to read beyond end of device\n",
  1573. __func__);
  1574. return -EINVAL;
  1575. }
  1576. chipnr = (int)(from >> chip->chip_shift);
  1577. chip->select_chip(mtd, chipnr);
  1578. /* Shift to get page */
  1579. realpage = (int)(from >> chip->page_shift);
  1580. page = realpage & chip->pagemask;
  1581. while (1) {
  1582. if (ops->mode == MTD_OPS_RAW)
  1583. sndcmd = chip->ecc.read_oob_raw(mtd, chip, page, sndcmd);
  1584. else
  1585. sndcmd = chip->ecc.read_oob(mtd, chip, page, sndcmd);
  1586. len = min(len, readlen);
  1587. buf = nand_transfer_oob(chip, buf, ops, len);
  1588. if (!(chip->options & NAND_NO_READRDY)) {
  1589. /*
  1590. * Apply delay or wait for ready/busy pin. Do this
  1591. * before the AUTOINCR check, so no problems arise if a
  1592. * chip which does auto increment is marked as
  1593. * NOAUTOINCR by the board driver.
  1594. */
  1595. if (!chip->dev_ready)
  1596. udelay(chip->chip_delay);
  1597. else
  1598. nand_wait_ready(mtd);
  1599. }
  1600. readlen -= len;
  1601. if (!readlen)
  1602. break;
  1603. /* Increment page address */
  1604. realpage++;
  1605. page = realpage & chip->pagemask;
  1606. /* Check, if we cross a chip boundary */
  1607. if (!page) {
  1608. chipnr++;
  1609. chip->select_chip(mtd, -1);
  1610. chip->select_chip(mtd, chipnr);
  1611. }
  1612. /*
  1613. * Check, if the chip supports auto page increment or if we
  1614. * have hit a block boundary.
  1615. */
  1616. if (!NAND_CANAUTOINCR(chip) || !(page & blkcheck))
  1617. sndcmd = 1;
  1618. }
  1619. ops->oobretlen = ops->ooblen;
  1620. if (mtd->ecc_stats.failed - stats.failed)
  1621. return -EBADMSG;
  1622. return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
  1623. }
  1624. /**
  1625. * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
  1626. * @mtd: MTD device structure
  1627. * @from: offset to read from
  1628. * @ops: oob operation description structure
  1629. *
  1630. * NAND read data and/or out-of-band data.
  1631. */
  1632. static int nand_read_oob(struct mtd_info *mtd, loff_t from,
  1633. struct mtd_oob_ops *ops)
  1634. {
  1635. struct nand_chip *chip = mtd->priv;
  1636. int ret = -ENOTSUPP;
  1637. ops->retlen = 0;
  1638. /* Do not allow reads past end of device */
  1639. if (ops->datbuf && (from + ops->len) > mtd->size) {
  1640. pr_debug("%s: attempt to read beyond end of device\n",
  1641. __func__);
  1642. return -EINVAL;
  1643. }
  1644. nand_get_device(chip, mtd, FL_READING);
  1645. switch (ops->mode) {
  1646. case MTD_OPS_PLACE_OOB:
  1647. case MTD_OPS_AUTO_OOB:
  1648. case MTD_OPS_RAW:
  1649. break;
  1650. default:
  1651. goto out;
  1652. }
  1653. if (!ops->datbuf)
  1654. ret = nand_do_read_oob(mtd, from, ops);
  1655. else
  1656. ret = nand_do_read_ops(mtd, from, ops);
  1657. out:
  1658. nand_release_device(mtd);
  1659. return ret;
  1660. }
  1661. /**
  1662. * nand_write_page_raw - [INTERN] raw page write function
  1663. * @mtd: mtd info structure
  1664. * @chip: nand chip info structure
  1665. * @buf: data buffer
  1666. *
  1667. * Not for syndrome calculating ECC controllers, which use a special oob layout.
  1668. */
  1669. static void nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
  1670. const uint8_t *buf)
  1671. {
  1672. chip->write_buf(mtd, buf, mtd->writesize);
  1673. chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
  1674. }
  1675. /**
  1676. * nand_write_page_raw_syndrome - [INTERN] raw page write function
  1677. * @mtd: mtd info structure
  1678. * @chip: nand chip info structure
  1679. * @buf: data buffer
  1680. *
  1681. * We need a special oob layout and handling even when ECC isn't checked.
  1682. */
  1683. static void nand_write_page_raw_syndrome(struct mtd_info *mtd,
  1684. struct nand_chip *chip,
  1685. const uint8_t *buf)
  1686. {
  1687. int eccsize = chip->ecc.size;
  1688. int eccbytes = chip->ecc.bytes;
  1689. uint8_t *oob = chip->oob_poi;
  1690. int steps, size;
  1691. for (steps = chip->ecc.steps; steps > 0; steps--) {
  1692. chip->write_buf(mtd, buf, eccsize);
  1693. buf += eccsize;
  1694. if (chip->ecc.prepad) {
  1695. chip->write_buf(mtd, oob, chip->ecc.prepad);
  1696. oob += chip->ecc.prepad;
  1697. }
  1698. chip->read_buf(mtd, oob, eccbytes);
  1699. oob += eccbytes;
  1700. if (chip->ecc.postpad) {
  1701. chip->write_buf(mtd, oob, chip->ecc.postpad);
  1702. oob += chip->ecc.postpad;
  1703. }
  1704. }
  1705. size = mtd->oobsize - (oob - chip->oob_poi);
  1706. if (size)
  1707. chip->write_buf(mtd, oob, size);
  1708. }
  1709. /**
  1710. * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function
  1711. * @mtd: mtd info structure
  1712. * @chip: nand chip info structure
  1713. * @buf: data buffer
  1714. */
  1715. static void nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
  1716. const uint8_t *buf)
  1717. {
  1718. int i, eccsize = chip->ecc.size;
  1719. int eccbytes = chip->ecc.bytes;
  1720. int eccsteps = chip->ecc.steps;
  1721. uint8_t *ecc_calc = chip->buffers->ecccalc;
  1722. const uint8_t *p = buf;
  1723. uint32_t *eccpos = chip->ecc.layout->eccpos;
  1724. /* Software ECC calculation */
  1725. for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
  1726. chip->ecc.calculate(mtd, p, &ecc_calc[i]);
  1727. for (i = 0; i < chip->ecc.total; i++)
  1728. chip->oob_poi[eccpos[i]] = ecc_calc[i];
  1729. chip->ecc.write_page_raw(mtd, chip, buf);
  1730. }
  1731. /**
  1732. * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function
  1733. * @mtd: mtd info structure
  1734. * @chip: nand chip info structure
  1735. * @buf: data buffer
  1736. */
  1737. static void nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
  1738. const uint8_t *buf)
  1739. {
  1740. int i, eccsize = chip->ecc.size;
  1741. int eccbytes = chip->ecc.bytes;
  1742. int eccsteps = chip->ecc.steps;
  1743. uint8_t *ecc_calc = chip->buffers->ecccalc;
  1744. const uint8_t *p = buf;
  1745. uint32_t *eccpos = chip->ecc.layout->eccpos;
  1746. for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
  1747. chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
  1748. chip->write_buf(mtd, p, eccsize);
  1749. chip->ecc.calculate(mtd, p, &ecc_calc[i]);
  1750. }
  1751. for (i = 0; i < chip->ecc.total; i++)
  1752. chip->oob_poi[eccpos[i]] = ecc_calc[i];
  1753. chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
  1754. }
  1755. /**
  1756. * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write
  1757. * @mtd: mtd info structure
  1758. * @chip: nand chip info structure
  1759. * @buf: data buffer
  1760. *
  1761. * The hw generator calculates the error syndrome automatically. Therefore we
  1762. * need a special oob layout and handling.
  1763. */
  1764. static void nand_write_page_syndrome(struct mtd_info *mtd,
  1765. struct nand_chip *chip, const uint8_t *buf)
  1766. {
  1767. int i, eccsize = chip->ecc.size;
  1768. int eccbytes = chip->ecc.bytes;
  1769. int eccsteps = chip->ecc.steps;
  1770. const uint8_t *p = buf;
  1771. uint8_t *oob = chip->oob_poi;
  1772. for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
  1773. chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
  1774. chip->write_buf(mtd, p, eccsize);
  1775. if (chip->ecc.prepad) {
  1776. chip->write_buf(mtd, oob, chip->ecc.prepad);
  1777. oob += chip->ecc.prepad;
  1778. }
  1779. chip->ecc.calculate(mtd, p, oob);
  1780. chip->write_buf(mtd, oob, eccbytes);
  1781. oob += eccbytes;
  1782. if (chip->ecc.postpad) {
  1783. chip->write_buf(mtd, oob, chip->ecc.postpad);
  1784. oob += chip->ecc.postpad;
  1785. }
  1786. }
  1787. /* Calculate remaining oob bytes */
  1788. i = mtd->oobsize - (oob - chip->oob_poi);
  1789. if (i)
  1790. chip->write_buf(mtd, oob, i);
  1791. }
  1792. /**
  1793. * nand_write_page - [REPLACEABLE] write one page
  1794. * @mtd: MTD device structure
  1795. * @chip: NAND chip descriptor
  1796. * @buf: the data to write
  1797. * @page: page number to write
  1798. * @cached: cached programming
  1799. * @raw: use _raw version of write_page
  1800. */
  1801. static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
  1802. const uint8_t *buf, int page, int cached, int raw)
  1803. {
  1804. int status;
  1805. chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
  1806. if (unlikely(raw))
  1807. chip->ecc.write_page_raw(mtd, chip, buf);
  1808. else
  1809. chip->ecc.write_page(mtd, chip, buf);
  1810. /*
  1811. * Cached progamming disabled for now. Not sure if it's worth the
  1812. * trouble. The speed gain is not very impressive. (2.3->2.6Mib/s).
  1813. */
  1814. cached = 0;
  1815. if (!cached || !(chip->options & NAND_CACHEPRG)) {
  1816. chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
  1817. status = chip->waitfunc(mtd, chip);
  1818. /*
  1819. * See if operation failed and additional status checks are
  1820. * available.
  1821. */
  1822. if ((status & NAND_STATUS_FAIL) && (chip->errstat))
  1823. status = chip->errstat(mtd, chip, FL_WRITING, status,
  1824. page);
  1825. if (status & NAND_STATUS_FAIL)
  1826. return -EIO;
  1827. } else {
  1828. chip->cmdfunc(mtd, NAND_CMD_CACHEDPROG, -1, -1);
  1829. status = chip->waitfunc(mtd, chip);
  1830. }
  1831. #ifdef CONFIG_MTD_NAND_VERIFY_WRITE
  1832. /* Send command to read back the data */
  1833. chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
  1834. if (chip->verify_buf(mtd, buf, mtd->writesize))
  1835. return -EIO;
  1836. #endif
  1837. return 0;
  1838. }
  1839. /**
  1840. * nand_fill_oob - [INTERN] Transfer client buffer to oob
  1841. * @mtd: MTD device structure
  1842. * @oob: oob data buffer
  1843. * @len: oob data write length
  1844. * @ops: oob ops structure
  1845. */
  1846. static uint8_t *nand_fill_oob(struct mtd_info *mtd, uint8_t *oob, size_t len,
  1847. struct mtd_oob_ops *ops)
  1848. {
  1849. struct nand_chip *chip = mtd->priv;
  1850. /*
  1851. * Initialise to all 0xFF, to avoid the possibility of left over OOB
  1852. * data from a previous OOB read.
  1853. */
  1854. memset(chip->oob_poi, 0xff, mtd->oobsize);
  1855. switch (ops->mode) {
  1856. case MTD_OPS_PLACE_OOB:
  1857. case MTD_OPS_RAW:
  1858. memcpy(chip->oob_poi + ops->ooboffs, oob, len);
  1859. return oob + len;
  1860. case MTD_OPS_AUTO_OOB: {
  1861. struct nand_oobfree *free = chip->ecc.layout->oobfree;
  1862. uint32_t boffs = 0, woffs = ops->ooboffs;
  1863. size_t bytes = 0;
  1864. for (; free->length && len; free++, len -= bytes) {
  1865. /* Write request not from offset 0? */
  1866. if (unlikely(woffs)) {
  1867. if (woffs >= free->length) {
  1868. woffs -= free->length;
  1869. continue;
  1870. }
  1871. boffs = free->offset + woffs;
  1872. bytes = min_t(size_t, len,
  1873. (free->length - woffs));
  1874. woffs = 0;
  1875. } else {
  1876. bytes = min_t(size_t, len, free->length);
  1877. boffs = free->offset;
  1878. }
  1879. memcpy(chip->oob_poi + boffs, oob, bytes);
  1880. oob += bytes;
  1881. }
  1882. return oob;
  1883. }
  1884. default:
  1885. BUG();
  1886. }
  1887. return NULL;
  1888. }
  1889. #define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0)
  1890. /**
  1891. * nand_do_write_ops - [INTERN] NAND write with ECC
  1892. * @mtd: MTD device structure
  1893. * @to: offset to write to
  1894. * @ops: oob operations description structure
  1895. *
  1896. * NAND write with ECC.
  1897. */
  1898. static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
  1899. struct mtd_oob_ops *ops)
  1900. {
  1901. int chipnr, realpage, page, blockmask, column;
  1902. struct nand_chip *chip = mtd->priv;
  1903. uint32_t writelen = ops->len;
  1904. uint32_t oobwritelen = ops->ooblen;
  1905. uint32_t oobmaxlen = ops->mode == MTD_OPS_AUTO_OOB ?
  1906. mtd->oobavail : mtd->oobsize;
  1907. uint8_t *oob = ops->oobbuf;
  1908. uint8_t *buf = ops->datbuf;
  1909. int ret, subpage;
  1910. ops->retlen = 0;
  1911. if (!writelen)
  1912. return 0;
  1913. /* Reject writes, which are not page aligned */
  1914. if (NOTALIGNED(to) || NOTALIGNED(ops->len)) {
  1915. pr_notice("%s: attempt to write non page aligned data\n",
  1916. __func__);
  1917. return -EINVAL;
  1918. }
  1919. column = to & (mtd->writesize - 1);
  1920. subpage = column || (writelen & (mtd->writesize - 1));
  1921. if (subpage && oob)
  1922. return -EINVAL;
  1923. chipnr = (int)(to >> chip->chip_shift);
  1924. chip->select_chip(mtd, chipnr);
  1925. /* Check, if it is write protected */
  1926. if (nand_check_wp(mtd))
  1927. return -EIO;
  1928. realpage = (int)(to >> chip->page_shift);
  1929. page = realpage & chip->pagemask;
  1930. blockmask = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
  1931. /* Invalidate the page cache, when we write to the cached page */
  1932. if (to <= (chip->pagebuf << chip->page_shift) &&
  1933. (chip->pagebuf << chip->page_shift) < (to + ops->len))
  1934. chip->pagebuf = -1;
  1935. /* Don't allow multipage oob writes with offset */
  1936. if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen))
  1937. return -EINVAL;
  1938. while (1) {
  1939. int bytes = mtd->writesize;
  1940. int cached = writelen > bytes && page != blockmask;
  1941. uint8_t *wbuf = buf;
  1942. /* Partial page write? */
  1943. if (unlikely(column || writelen < (mtd->writesize - 1))) {
  1944. cached = 0;
  1945. bytes = min_t(int, bytes - column, (int) writelen);
  1946. chip->pagebuf = -1;
  1947. memset(chip->buffers->databuf, 0xff, mtd->writesize);
  1948. memcpy(&chip->buffers->databuf[column], buf, bytes);
  1949. wbuf = chip->buffers->databuf;
  1950. }
  1951. if (unlikely(oob)) {
  1952. size_t len = min(oobwritelen, oobmaxlen);
  1953. oob = nand_fill_oob(mtd, oob, len, ops);
  1954. oobwritelen -= len;
  1955. } else {
  1956. /* We still need to erase leftover OOB data */
  1957. memset(chip->oob_poi, 0xff, mtd->oobsize);
  1958. }
  1959. ret = chip->write_page(mtd, chip, wbuf, page, cached,
  1960. (ops->mode == MTD_OPS_RAW));
  1961. if (ret)
  1962. break;
  1963. writelen -= bytes;
  1964. if (!writelen)
  1965. break;
  1966. column = 0;
  1967. buf += bytes;
  1968. realpage++;
  1969. page = realpage & chip->pagemask;
  1970. /* Check, if we cross a chip boundary */
  1971. if (!page) {
  1972. chipnr++;
  1973. chip->select_chip(mtd, -1);
  1974. chip->select_chip(mtd, chipnr);
  1975. }
  1976. }
  1977. ops->retlen = ops->len - writelen;
  1978. if (unlikely(oob))
  1979. ops->oobretlen = ops->ooblen;
  1980. return ret;
  1981. }
  1982. /**
  1983. * panic_nand_write - [MTD Interface] NAND write with ECC
  1984. * @mtd: MTD device structure
  1985. * @to: offset to write to
  1986. * @len: number of bytes to write
  1987. * @retlen: pointer to variable to store the number of written bytes
  1988. * @buf: the data to write
  1989. *
  1990. * NAND write with ECC. Used when performing writes in interrupt context, this
  1991. * may for example be called by mtdoops when writing an oops while in panic.
  1992. */
  1993. static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
  1994. size_t *retlen, const uint8_t *buf)
  1995. {
  1996. struct nand_chip *chip = mtd->priv;
  1997. struct mtd_oob_ops ops;
  1998. int ret;
  1999. /* Wait for the device to get ready */
  2000. panic_nand_wait(mtd, chip, 400);
  2001. /* Grab the device */
  2002. panic_nand_get_device(chip, mtd, FL_WRITING);
  2003. ops.len = len;
  2004. ops.datbuf = (uint8_t *)buf;
  2005. ops.oobbuf = NULL;
  2006. ops.mode = 0;
  2007. ret = nand_do_write_ops(mtd, to, &ops);
  2008. *retlen = ops.retlen;
  2009. return ret;
  2010. }
  2011. /**
  2012. * nand_write - [MTD Interface] NAND write with ECC
  2013. * @mtd: MTD device structure
  2014. * @to: offset to write to
  2015. * @len: number of bytes to write
  2016. * @retlen: pointer to variable to store the number of written bytes
  2017. * @buf: the data to write
  2018. *
  2019. * NAND write with ECC.
  2020. */
  2021. static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
  2022. size_t *retlen, const uint8_t *buf)
  2023. {
  2024. struct nand_chip *chip = mtd->priv;
  2025. struct mtd_oob_ops ops;
  2026. int ret;
  2027. nand_get_device(chip, mtd, FL_WRITING);
  2028. ops.len = len;
  2029. ops.datbuf = (uint8_t *)buf;
  2030. ops.oobbuf = NULL;
  2031. ops.mode = 0;
  2032. ret = nand_do_write_ops(mtd, to, &ops);
  2033. *retlen = ops.retlen;
  2034. nand_release_device(mtd);
  2035. return ret;
  2036. }
  2037. /**
  2038. * nand_do_write_oob - [MTD Interface] NAND write out-of-band
  2039. * @mtd: MTD device structure
  2040. * @to: offset to write to
  2041. * @ops: oob operation description structure
  2042. *
  2043. * NAND write out-of-band.
  2044. */
  2045. static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
  2046. struct mtd_oob_ops *ops)
  2047. {
  2048. int chipnr, page, status, len;
  2049. struct nand_chip *chip = mtd->priv;
  2050. pr_debug("%s: to = 0x%08x, len = %i\n",
  2051. __func__, (unsigned int)to, (int)ops->ooblen);
  2052. if (ops->mode == MTD_OPS_AUTO_OOB)
  2053. len = chip->ecc.layout->oobavail;
  2054. else
  2055. len = mtd->oobsize;
  2056. /* Do not allow write past end of page */
  2057. if ((ops->ooboffs + ops->ooblen) > len) {
  2058. pr_debug("%s: attempt to write past end of page\n",
  2059. __func__);
  2060. return -EINVAL;
  2061. }
  2062. if (unlikely(ops->ooboffs >= len)) {
  2063. pr_debug("%s: attempt to start write outside oob\n",
  2064. __func__);
  2065. return -EINVAL;
  2066. }
  2067. /* Do not allow write past end of device */
  2068. if (unlikely(to >= mtd->size ||
  2069. ops->ooboffs + ops->ooblen >
  2070. ((mtd->size >> chip->page_shift) -
  2071. (to >> chip->page_shift)) * len)) {
  2072. pr_debug("%s: attempt to write beyond end of device\n",
  2073. __func__);
  2074. return -EINVAL;
  2075. }
  2076. chipnr = (int)(to >> chip->chip_shift);
  2077. chip->select_chip(mtd, chipnr);
  2078. /* Shift to get page */
  2079. page = (int)(to >> chip->page_shift);
  2080. /*
  2081. * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
  2082. * of my DiskOnChip 2000 test units) will clear the whole data page too
  2083. * if we don't do this. I have no clue why, but I seem to have 'fixed'
  2084. * it in the doc2000 driver in August 1999. dwmw2.
  2085. */
  2086. chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
  2087. /* Check, if it is write protected */
  2088. if (nand_check_wp(mtd))
  2089. return -EROFS;
  2090. /* Invalidate the page cache, if we write to the cached page */
  2091. if (page == chip->pagebuf)
  2092. chip->pagebuf = -1;
  2093. nand_fill_oob(mtd, ops->oobbuf, ops->ooblen, ops);
  2094. if (ops->mode == MTD_OPS_RAW)
  2095. status = chip->ecc.write_oob_raw(mtd, chip, page & chip->pagemask);
  2096. else
  2097. status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
  2098. if (status)
  2099. return status;
  2100. ops->oobretlen = ops->ooblen;
  2101. return 0;
  2102. }
  2103. /**
  2104. * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
  2105. * @mtd: MTD device structure
  2106. * @to: offset to write to
  2107. * @ops: oob operation description structure
  2108. */
  2109. static int nand_write_oob(struct mtd_info *mtd, loff_t to,
  2110. struct mtd_oob_ops *ops)
  2111. {
  2112. struct nand_chip *chip = mtd->priv;
  2113. int ret = -ENOTSUPP;
  2114. ops->retlen = 0;
  2115. /* Do not allow writes past end of device */
  2116. if (ops->datbuf && (to + ops->len) > mtd->size) {
  2117. pr_debug("%s: attempt to write beyond end of device\n",
  2118. __func__);
  2119. return -EINVAL;
  2120. }
  2121. nand_get_device(chip, mtd, FL_WRITING);
  2122. switch (ops->mode) {
  2123. case MTD_OPS_PLACE_OOB:
  2124. case MTD_OPS_AUTO_OOB:
  2125. case MTD_OPS_RAW:
  2126. break;
  2127. default:
  2128. goto out;
  2129. }
  2130. if (!ops->datbuf)
  2131. ret = nand_do_write_oob(mtd, to, ops);
  2132. else
  2133. ret = nand_do_write_ops(mtd, to, ops);
  2134. out:
  2135. nand_release_device(mtd);
  2136. return ret;
  2137. }
  2138. /**
  2139. * single_erase_cmd - [GENERIC] NAND standard block erase command function
  2140. * @mtd: MTD device structure
  2141. * @page: the page address of the block which will be erased
  2142. *
  2143. * Standard erase command for NAND chips.
  2144. */
  2145. static void single_erase_cmd(struct mtd_info *mtd, int page)
  2146. {
  2147. struct nand_chip *chip = mtd->priv;
  2148. /* Send commands to erase a block */
  2149. chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
  2150. chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
  2151. }
  2152. /**
  2153. * multi_erase_cmd - [GENERIC] AND specific block erase command function
  2154. * @mtd: MTD device structure
  2155. * @page: the page address of the block which will be erased
  2156. *
  2157. * AND multi block erase command function. Erase 4 consecutive blocks.
  2158. */
  2159. static void multi_erase_cmd(struct mtd_info *mtd, int page)
  2160. {
  2161. struct nand_chip *chip = mtd->priv;
  2162. /* Send commands to erase a block */
  2163. chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
  2164. chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
  2165. chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
  2166. chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
  2167. chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
  2168. }
  2169. /**
  2170. * nand_erase - [MTD Interface] erase block(s)
  2171. * @mtd: MTD device structure
  2172. * @instr: erase instruction
  2173. *
  2174. * Erase one ore more blocks.
  2175. */
  2176. static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
  2177. {
  2178. return nand_erase_nand(mtd, instr, 0);
  2179. }
  2180. #define BBT_PAGE_MASK 0xffffff3f
  2181. /**
  2182. * nand_erase_nand - [INTERN] erase block(s)
  2183. * @mtd: MTD device structure
  2184. * @instr: erase instruction
  2185. * @allowbbt: allow erasing the bbt area
  2186. *
  2187. * Erase one ore more blocks.
  2188. */
  2189. int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
  2190. int allowbbt)
  2191. {
  2192. int page, status, pages_per_block, ret, chipnr;
  2193. struct nand_chip *chip = mtd->priv;
  2194. loff_t rewrite_bbt[NAND_MAX_CHIPS] = {0};
  2195. unsigned int bbt_masked_page = 0xffffffff;
  2196. loff_t len;
  2197. pr_debug("%s: start = 0x%012llx, len = %llu\n",
  2198. __func__, (unsigned long long)instr->addr,
  2199. (unsigned long long)instr->len);
  2200. if (check_offs_len(mtd, instr->addr, instr->len))
  2201. return -EINVAL;
  2202. /* Grab the lock and see if the device is available */
  2203. nand_get_device(chip, mtd, FL_ERASING);
  2204. /* Shift to get first page */
  2205. page = (int)(instr->addr >> chip->page_shift);
  2206. chipnr = (int)(instr->addr >> chip->chip_shift);
  2207. /* Calculate pages in each block */
  2208. pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
  2209. /* Select the NAND device */
  2210. chip->select_chip(mtd, chipnr);
  2211. /* Check, if it is write protected */
  2212. if (nand_check_wp(mtd)) {
  2213. pr_debug("%s: device is write protected!\n",
  2214. __func__);
  2215. instr->state = MTD_ERASE_FAILED;
  2216. goto erase_exit;
  2217. }
  2218. /*
  2219. * If BBT requires refresh, set the BBT page mask to see if the BBT
  2220. * should be rewritten. Otherwise the mask is set to 0xffffffff which
  2221. * can not be matched. This is also done when the bbt is actually
  2222. * erased to avoid recursive updates.
  2223. */
  2224. if (chip->options & BBT_AUTO_REFRESH && !allowbbt)
  2225. bbt_masked_page = chip->bbt_td->pages[chipnr] & BBT_PAGE_MASK;
  2226. /* Loop through the pages */
  2227. len = instr->len;
  2228. instr->state = MTD_ERASING;
  2229. while (len) {
  2230. /* Check if we have a bad block, we do not erase bad blocks! */
  2231. if (nand_block_checkbad(mtd, ((loff_t) page) <<
  2232. chip->page_shift, 0, allowbbt)) {
  2233. pr_warn("%s: attempt to erase a bad block at page 0x%08x\n",
  2234. __func__, page);
  2235. instr->state = MTD_ERASE_FAILED;
  2236. goto erase_exit;
  2237. }
  2238. /*
  2239. * Invalidate the page cache, if we erase the block which
  2240. * contains the current cached page.
  2241. */
  2242. if (page <= chip->pagebuf && chip->pagebuf <
  2243. (page + pages_per_block))
  2244. chip->pagebuf = -1;
  2245. chip->erase_cmd(mtd, page & chip->pagemask);
  2246. status = chip->waitfunc(mtd, chip);
  2247. /*
  2248. * See if operation failed and additional status checks are
  2249. * available
  2250. */
  2251. if ((status & NAND_STATUS_FAIL) && (chip->errstat))
  2252. status = chip->errstat(mtd, chip, FL_ERASING,
  2253. status, page);
  2254. /* See if block erase succeeded */
  2255. if (status & NAND_STATUS_FAIL) {
  2256. pr_debug("%s: failed erase, page 0x%08x\n",
  2257. __func__, page);
  2258. instr->state = MTD_ERASE_FAILED;
  2259. instr->fail_addr =
  2260. ((loff_t)page << chip->page_shift);
  2261. goto erase_exit;
  2262. }
  2263. /*
  2264. * If BBT requires refresh, set the BBT rewrite flag to the
  2265. * page being erased.
  2266. */
  2267. if (bbt_masked_page != 0xffffffff &&
  2268. (page & BBT_PAGE_MASK) == bbt_masked_page)
  2269. rewrite_bbt[chipnr] =
  2270. ((loff_t)page << chip->page_shift);
  2271. /* Increment page address and decrement length */
  2272. len -= (1 << chip->phys_erase_shift);
  2273. page += pages_per_block;
  2274. /* Check, if we cross a chip boundary */
  2275. if (len && !(page & chip->pagemask)) {
  2276. chipnr++;
  2277. chip->select_chip(mtd, -1);
  2278. chip->select_chip(mtd, chipnr);
  2279. /*
  2280. * If BBT requires refresh and BBT-PERCHIP, set the BBT
  2281. * page mask to see if this BBT should be rewritten.
  2282. */
  2283. if (bbt_masked_page != 0xffffffff &&
  2284. (chip->bbt_td->options & NAND_BBT_PERCHIP))
  2285. bbt_masked_page = chip->bbt_td->pages[chipnr] &
  2286. BBT_PAGE_MASK;
  2287. }
  2288. }
  2289. instr->state = MTD_ERASE_DONE;
  2290. erase_exit:
  2291. ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
  2292. /* Deselect and wake up anyone waiting on the device */
  2293. nand_release_device(mtd);
  2294. /* Do call back function */
  2295. if (!ret)
  2296. mtd_erase_callback(instr);
  2297. /*
  2298. * If BBT requires refresh and erase was successful, rewrite any
  2299. * selected bad block tables.
  2300. */
  2301. if (bbt_masked_page == 0xffffffff || ret)
  2302. return ret;
  2303. for (chipnr = 0; chipnr < chip->numchips; chipnr++) {
  2304. if (!rewrite_bbt[chipnr])
  2305. continue;
  2306. /* Update the BBT for chip */
  2307. pr_debug("%s: nand_update_bbt (%d:0x%0llx 0x%0x)\n",
  2308. __func__, chipnr, rewrite_bbt[chipnr],
  2309. chip->bbt_td->pages[chipnr]);
  2310. nand_update_bbt(mtd, rewrite_bbt[chipnr]);
  2311. }
  2312. /* Return more or less happy */
  2313. return ret;
  2314. }
  2315. /**
  2316. * nand_sync - [MTD Interface] sync
  2317. * @mtd: MTD device structure
  2318. *
  2319. * Sync is actually a wait for chip ready function.
  2320. */
  2321. static void nand_sync(struct mtd_info *mtd)
  2322. {
  2323. struct nand_chip *chip = mtd->priv;
  2324. pr_debug("%s: called\n", __func__);
  2325. /* Grab the lock and see if the device is available */
  2326. nand_get_device(chip, mtd, FL_SYNCING);
  2327. /* Release it and go back */
  2328. nand_release_device(mtd);
  2329. }
  2330. /**
  2331. * nand_block_isbad - [MTD Interface] Check if block at offset is bad
  2332. * @mtd: MTD device structure
  2333. * @offs: offset relative to mtd start
  2334. */
  2335. static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
  2336. {
  2337. return nand_block_checkbad(mtd, offs, 1, 0);
  2338. }
  2339. /**
  2340. * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
  2341. * @mtd: MTD device structure
  2342. * @ofs: offset relative to mtd start
  2343. */
  2344. static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
  2345. {
  2346. struct nand_chip *chip = mtd->priv;
  2347. int ret;
  2348. ret = nand_block_isbad(mtd, ofs);
  2349. if (ret) {
  2350. /* If it was bad already, return success and do nothing */
  2351. if (ret > 0)
  2352. return 0;
  2353. return ret;
  2354. }
  2355. return chip->block_markbad(mtd, ofs);
  2356. }
  2357. /**
  2358. * nand_suspend - [MTD Interface] Suspend the NAND flash
  2359. * @mtd: MTD device structure
  2360. */
  2361. static int nand_suspend(struct mtd_info *mtd)
  2362. {
  2363. struct nand_chip *chip = mtd->priv;
  2364. return nand_get_device(chip, mtd, FL_PM_SUSPENDED);
  2365. }
  2366. /**
  2367. * nand_resume - [MTD Interface] Resume the NAND flash
  2368. * @mtd: MTD device structure
  2369. */
  2370. static void nand_resume(struct mtd_info *mtd)
  2371. {
  2372. struct nand_chip *chip = mtd->priv;
  2373. if (chip->state == FL_PM_SUSPENDED)
  2374. nand_release_device(mtd);
  2375. else
  2376. pr_err("%s called for a chip which is not in suspended state\n",
  2377. __func__);
  2378. }
  2379. /* Set default functions */
  2380. static void nand_set_defaults(struct nand_chip *chip, int busw)
  2381. {
  2382. /* check for proper chip_delay setup, set 20us if not */
  2383. if (!chip->chip_delay)
  2384. chip->chip_delay = 20;
  2385. /* check, if a user supplied command function given */
  2386. if (chip->cmdfunc == NULL)
  2387. chip->cmdfunc = nand_command;
  2388. /* check, if a user supplied wait function given */
  2389. if (chip->waitfunc == NULL)
  2390. chip->waitfunc = nand_wait;
  2391. if (!chip->select_chip)
  2392. chip->select_chip = nand_select_chip;
  2393. if (!chip->read_byte)
  2394. chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
  2395. if (!chip->read_word)
  2396. chip->read_word = nand_read_word;
  2397. if (!chip->block_bad)
  2398. chip->block_bad = nand_block_bad;
  2399. if (!chip->block_markbad)
  2400. chip->block_markbad = nand_default_block_markbad;
  2401. if (!chip->write_buf)
  2402. chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
  2403. if (!chip->read_buf)
  2404. chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
  2405. if (!chip->verify_buf)
  2406. chip->verify_buf = busw ? nand_verify_buf16 : nand_verify_buf;
  2407. if (!chip->scan_bbt)
  2408. chip->scan_bbt = nand_default_bbt;
  2409. if (!chip->controller) {
  2410. chip->controller = &chip->hwcontrol;
  2411. spin_lock_init(&chip->controller->lock);
  2412. init_waitqueue_head(&chip->controller->wq);
  2413. }
  2414. }
  2415. /* Sanitize ONFI strings so we can safely print them */
  2416. static void sanitize_string(uint8_t *s, size_t len)
  2417. {
  2418. ssize_t i;
  2419. /* Null terminate */
  2420. s[len - 1] = 0;
  2421. /* Remove non printable chars */
  2422. for (i = 0; i < len - 1; i++) {
  2423. if (s[i] < ' ' || s[i] > 127)
  2424. s[i] = '?';
  2425. }
  2426. /* Remove trailing spaces */
  2427. strim(s);
  2428. }
  2429. static u16 onfi_crc16(u16 crc, u8 const *p, size_t len)
  2430. {
  2431. int i;
  2432. while (len--) {
  2433. crc ^= *p++ << 8;
  2434. for (i = 0; i < 8; i++)
  2435. crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0);
  2436. }
  2437. return crc;
  2438. }
  2439. /*
  2440. * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise.
  2441. */
  2442. static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip,
  2443. int *busw)
  2444. {
  2445. struct nand_onfi_params *p = &chip->onfi_params;
  2446. int i;
  2447. int val;
  2448. /* Try ONFI for unknown chip or LP */
  2449. chip->cmdfunc(mtd, NAND_CMD_READID, 0x20, -1);
  2450. if (chip->read_byte(mtd) != 'O' || chip->read_byte(mtd) != 'N' ||
  2451. chip->read_byte(mtd) != 'F' || chip->read_byte(mtd) != 'I')
  2452. return 0;
  2453. chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
  2454. for (i = 0; i < 3; i++) {
  2455. chip->read_buf(mtd, (uint8_t *)p, sizeof(*p));
  2456. if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 254) ==
  2457. le16_to_cpu(p->crc)) {
  2458. pr_info("ONFI param page %d valid\n", i);
  2459. break;
  2460. }
  2461. }
  2462. if (i == 3)
  2463. return 0;
  2464. /* Check version */
  2465. val = le16_to_cpu(p->revision);
  2466. if (val & (1 << 5))
  2467. chip->onfi_version = 23;
  2468. else if (val & (1 << 4))
  2469. chip->onfi_version = 22;
  2470. else if (val & (1 << 3))
  2471. chip->onfi_version = 21;
  2472. else if (val & (1 << 2))
  2473. chip->onfi_version = 20;
  2474. else if (val & (1 << 1))
  2475. chip->onfi_version = 10;
  2476. else
  2477. chip->onfi_version = 0;
  2478. if (!chip->onfi_version) {
  2479. pr_info("%s: unsupported ONFI version: %d\n", __func__, val);
  2480. return 0;
  2481. }
  2482. sanitize_string(p->manufacturer, sizeof(p->manufacturer));
  2483. sanitize_string(p->model, sizeof(p->model));
  2484. if (!mtd->name)
  2485. mtd->name = p->model;
  2486. mtd->writesize = le32_to_cpu(p->byte_per_page);
  2487. /*
  2488. * pages_per_block and blocks_per_lun may not be a power-of-2 size
  2489. * (don't ask me who thought of this...). MTD assumes that these
  2490. * dimensions will be power-of-2, so just truncate the remaining area.
  2491. */
  2492. mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
  2493. mtd->erasesize *= mtd->writesize;
  2494. mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
  2495. /* See erasesize comment */
  2496. chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
  2497. chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
  2498. *busw = 0;
  2499. if (le16_to_cpu(p->features) & 1)
  2500. *busw = NAND_BUSWIDTH_16;
  2501. chip->options |= NAND_NO_READRDY | NAND_NO_AUTOINCR;
  2502. pr_info("ONFI flash detected\n");
  2503. return 1;
  2504. }
  2505. /*
  2506. * Get the flash and manufacturer id and lookup if the type is supported.
  2507. */
  2508. static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
  2509. struct nand_chip *chip,
  2510. int busw,
  2511. int *maf_id, int *dev_id,
  2512. struct nand_flash_dev *type)
  2513. {
  2514. int i, maf_idx;
  2515. u8 id_data[8];
  2516. int ret;
  2517. /* Select the device */
  2518. chip->select_chip(mtd, 0);
  2519. /*
  2520. * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
  2521. * after power-up.
  2522. */
  2523. chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
  2524. /* Send the command for reading device ID */
  2525. chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
  2526. /* Read manufacturer and device IDs */
  2527. *maf_id = chip->read_byte(mtd);
  2528. *dev_id = chip->read_byte(mtd);
  2529. /*
  2530. * Try again to make sure, as some systems the bus-hold or other
  2531. * interface concerns can cause random data which looks like a
  2532. * possibly credible NAND flash to appear. If the two results do
  2533. * not match, ignore the device completely.
  2534. */
  2535. chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
  2536. for (i = 0; i < 2; i++)
  2537. id_data[i] = chip->read_byte(mtd);
  2538. if (id_data[0] != *maf_id || id_data[1] != *dev_id) {
  2539. pr_info("%s: second ID read did not match "
  2540. "%02x,%02x against %02x,%02x\n", __func__,
  2541. *maf_id, *dev_id, id_data[0], id_data[1]);
  2542. return ERR_PTR(-ENODEV);
  2543. }
  2544. if (!type)
  2545. type = nand_flash_ids;
  2546. for (; type->name != NULL; type++)
  2547. if (*dev_id == type->id)
  2548. break;
  2549. chip->onfi_version = 0;
  2550. if (!type->name || !type->pagesize) {
  2551. /* Check is chip is ONFI compliant */
  2552. ret = nand_flash_detect_onfi(mtd, chip, &busw);
  2553. if (ret)
  2554. goto ident_done;
  2555. }
  2556. chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
  2557. /* Read entire ID string */
  2558. for (i = 0; i < 8; i++)
  2559. id_data[i] = chip->read_byte(mtd);
  2560. if (!type->name)
  2561. return ERR_PTR(-ENODEV);
  2562. if (!mtd->name)
  2563. mtd->name = type->name;
  2564. chip->chipsize = (uint64_t)type->chipsize << 20;
  2565. if (!type->pagesize && chip->init_size) {
  2566. /* Set the pagesize, oobsize, erasesize by the driver */
  2567. busw = chip->init_size(mtd, chip, id_data);
  2568. } else if (!type->pagesize) {
  2569. int extid;
  2570. /* The 3rd id byte holds MLC / multichip data */
  2571. chip->cellinfo = id_data[2];
  2572. /* The 4th id byte is the important one */
  2573. extid = id_data[3];
  2574. /*
  2575. * Field definitions are in the following datasheets:
  2576. * Old style (4,5 byte ID): Samsung K9GAG08U0M (p.32)
  2577. * New style (6 byte ID): Samsung K9GBG08U0M (p.40)
  2578. *
  2579. * Check for wraparound + Samsung ID + nonzero 6th byte
  2580. * to decide what to do.
  2581. */
  2582. if (id_data[0] == id_data[6] && id_data[1] == id_data[7] &&
  2583. id_data[0] == NAND_MFR_SAMSUNG &&
  2584. (chip->cellinfo & NAND_CI_CELLTYPE_MSK) &&
  2585. id_data[5] != 0x00) {
  2586. /* Calc pagesize */
  2587. mtd->writesize = 2048 << (extid & 0x03);
  2588. extid >>= 2;
  2589. /* Calc oobsize */
  2590. switch (extid & 0x03) {
  2591. case 1:
  2592. mtd->oobsize = 128;
  2593. break;
  2594. case 2:
  2595. mtd->oobsize = 218;
  2596. break;
  2597. case 3:
  2598. mtd->oobsize = 400;
  2599. break;
  2600. default:
  2601. mtd->oobsize = 436;
  2602. break;
  2603. }
  2604. extid >>= 2;
  2605. /* Calc blocksize */
  2606. mtd->erasesize = (128 * 1024) <<
  2607. (((extid >> 1) & 0x04) | (extid & 0x03));
  2608. busw = 0;
  2609. } else {
  2610. /* Calc pagesize */
  2611. mtd->writesize = 1024 << (extid & 0x03);
  2612. extid >>= 2;
  2613. /* Calc oobsize */
  2614. mtd->oobsize = (8 << (extid & 0x01)) *
  2615. (mtd->writesize >> 9);
  2616. extid >>= 2;
  2617. /* Calc blocksize. Blocksize is multiples of 64KiB */
  2618. mtd->erasesize = (64 * 1024) << (extid & 0x03);
  2619. extid >>= 2;
  2620. /* Get buswidth information */
  2621. busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0;
  2622. }
  2623. } else {
  2624. /*
  2625. * Old devices have chip data hardcoded in the device id table.
  2626. */
  2627. mtd->erasesize = type->erasesize;
  2628. mtd->writesize = type->pagesize;
  2629. mtd->oobsize = mtd->writesize / 32;
  2630. busw = type->options & NAND_BUSWIDTH_16;
  2631. /*
  2632. * Check for Spansion/AMD ID + repeating 5th, 6th byte since
  2633. * some Spansion chips have erasesize that conflicts with size
  2634. * listed in nand_ids table.
  2635. * Data sheet (5 byte ID): Spansion S30ML-P ORNAND (p.39)
  2636. */
  2637. if (*maf_id == NAND_MFR_AMD && id_data[4] != 0x00 &&
  2638. id_data[5] == 0x00 && id_data[6] == 0x00 &&
  2639. id_data[7] == 0x00 && mtd->writesize == 512) {
  2640. mtd->erasesize = 128 * 1024;
  2641. mtd->erasesize <<= ((id_data[3] & 0x03) << 1);
  2642. }
  2643. }
  2644. /* Get chip options */
  2645. chip->options |= type->options;
  2646. /*
  2647. * Check if chip is not a Samsung device. Do not clear the
  2648. * options for chips which do not have an extended id.
  2649. */
  2650. if (*maf_id != NAND_MFR_SAMSUNG && !type->pagesize)
  2651. chip->options &= ~NAND_SAMSUNG_LP_OPTIONS;
  2652. ident_done:
  2653. /*
  2654. * Set chip as a default. Board drivers can override it, if necessary.
  2655. */
  2656. chip->options |= NAND_NO_AUTOINCR;
  2657. /* Try to identify manufacturer */
  2658. for (maf_idx = 0; nand_manuf_ids[maf_idx].id != 0x0; maf_idx++) {
  2659. if (nand_manuf_ids[maf_idx].id == *maf_id)
  2660. break;
  2661. }
  2662. /*
  2663. * Check, if buswidth is correct. Hardware drivers should set
  2664. * chip correct!
  2665. */
  2666. if (busw != (chip->options & NAND_BUSWIDTH_16)) {
  2667. pr_info("NAND device: Manufacturer ID:"
  2668. " 0x%02x, Chip ID: 0x%02x (%s %s)\n", *maf_id,
  2669. *dev_id, nand_manuf_ids[maf_idx].name, mtd->name);
  2670. pr_warn("NAND bus width %d instead %d bit\n",
  2671. (chip->options & NAND_BUSWIDTH_16) ? 16 : 8,
  2672. busw ? 16 : 8);
  2673. return ERR_PTR(-EINVAL);
  2674. }
  2675. /* Calculate the address shift from the page size */
  2676. chip->page_shift = ffs(mtd->writesize) - 1;
  2677. /* Convert chipsize to number of pages per chip -1 */
  2678. chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
  2679. chip->bbt_erase_shift = chip->phys_erase_shift =
  2680. ffs(mtd->erasesize) - 1;
  2681. if (chip->chipsize & 0xffffffff)
  2682. chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
  2683. else {
  2684. chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32));
  2685. chip->chip_shift += 32 - 1;
  2686. }
  2687. chip->badblockbits = 8;
  2688. /* Set the bad block position */
  2689. if (mtd->writesize > 512 || (busw & NAND_BUSWIDTH_16))
  2690. chip->badblockpos = NAND_LARGE_BADBLOCK_POS;
  2691. else
  2692. chip->badblockpos = NAND_SMALL_BADBLOCK_POS;
  2693. /*
  2694. * Bad block marker is stored in the last page of each block
  2695. * on Samsung and Hynix MLC devices; stored in first two pages
  2696. * of each block on Micron devices with 2KiB pages and on
  2697. * SLC Samsung, Hynix, Toshiba, AMD/Spansion, and Macronix.
  2698. * All others scan only the first page.
  2699. */
  2700. if ((chip->cellinfo & NAND_CI_CELLTYPE_MSK) &&
  2701. (*maf_id == NAND_MFR_SAMSUNG ||
  2702. *maf_id == NAND_MFR_HYNIX))
  2703. chip->bbt_options |= NAND_BBT_SCANLASTPAGE;
  2704. else if ((!(chip->cellinfo & NAND_CI_CELLTYPE_MSK) &&
  2705. (*maf_id == NAND_MFR_SAMSUNG ||
  2706. *maf_id == NAND_MFR_HYNIX ||
  2707. *maf_id == NAND_MFR_TOSHIBA ||
  2708. *maf_id == NAND_MFR_AMD ||
  2709. *maf_id == NAND_MFR_MACRONIX)) ||
  2710. (mtd->writesize == 2048 &&
  2711. *maf_id == NAND_MFR_MICRON))
  2712. chip->bbt_options |= NAND_BBT_SCAN2NDPAGE;
  2713. /* Check for AND chips with 4 page planes */
  2714. if (chip->options & NAND_4PAGE_ARRAY)
  2715. chip->erase_cmd = multi_erase_cmd;
  2716. else
  2717. chip->erase_cmd = single_erase_cmd;
  2718. /* Do not replace user supplied command function! */
  2719. if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
  2720. chip->cmdfunc = nand_command_lp;
  2721. pr_info("NAND device: Manufacturer ID:"
  2722. " 0x%02x, Chip ID: 0x%02x (%s %s)\n", *maf_id, *dev_id,
  2723. nand_manuf_ids[maf_idx].name,
  2724. chip->onfi_version ? chip->onfi_params.model : type->name);
  2725. return type;
  2726. }
  2727. /**
  2728. * nand_scan_ident - [NAND Interface] Scan for the NAND device
  2729. * @mtd: MTD device structure
  2730. * @maxchips: number of chips to scan for
  2731. * @table: alternative NAND ID table
  2732. *
  2733. * This is the first phase of the normal nand_scan() function. It reads the
  2734. * flash ID and sets up MTD fields accordingly.
  2735. *
  2736. * The mtd->owner field must be set to the module of the caller.
  2737. */
  2738. int nand_scan_ident(struct mtd_info *mtd, int maxchips,
  2739. struct nand_flash_dev *table)
  2740. {
  2741. int i, busw, nand_maf_id, nand_dev_id;
  2742. struct nand_chip *chip = mtd->priv;
  2743. struct nand_flash_dev *type;
  2744. /* Get buswidth to select the correct functions */
  2745. busw = chip->options & NAND_BUSWIDTH_16;
  2746. /* Set the default functions */
  2747. nand_set_defaults(chip, busw);
  2748. /* Read the flash type */
  2749. type = nand_get_flash_type(mtd, chip, busw,
  2750. &nand_maf_id, &nand_dev_id, table);
  2751. if (IS_ERR(type)) {
  2752. if (!(chip->options & NAND_SCAN_SILENT_NODEV))
  2753. pr_warn("No NAND device found\n");
  2754. chip->select_chip(mtd, -1);
  2755. return PTR_ERR(type);
  2756. }
  2757. /* Check for a chip array */
  2758. for (i = 1; i < maxchips; i++) {
  2759. chip->select_chip(mtd, i);
  2760. /* See comment in nand_get_flash_type for reset */
  2761. chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
  2762. /* Send the command for reading device ID */
  2763. chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
  2764. /* Read manufacturer and device IDs */
  2765. if (nand_maf_id != chip->read_byte(mtd) ||
  2766. nand_dev_id != chip->read_byte(mtd))
  2767. break;
  2768. }
  2769. if (i > 1)
  2770. pr_info("%d NAND chips detected\n", i);
  2771. /* Store the number of chips and calc total size for mtd */
  2772. chip->numchips = i;
  2773. mtd->size = i * chip->chipsize;
  2774. return 0;
  2775. }
  2776. EXPORT_SYMBOL(nand_scan_ident);
  2777. /**
  2778. * nand_scan_tail - [NAND Interface] Scan for the NAND device
  2779. * @mtd: MTD device structure
  2780. *
  2781. * This is the second phase of the normal nand_scan() function. It fills out
  2782. * all the uninitialized function pointers with the defaults and scans for a
  2783. * bad block table if appropriate.
  2784. */
  2785. int nand_scan_tail(struct mtd_info *mtd)
  2786. {
  2787. int i;
  2788. struct nand_chip *chip = mtd->priv;
  2789. /* New bad blocks should be marked in OOB, flash-based BBT, or both */
  2790. BUG_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) &&
  2791. !(chip->bbt_options & NAND_BBT_USE_FLASH));
  2792. if (!(chip->options & NAND_OWN_BUFFERS))
  2793. chip->buffers = kmalloc(sizeof(*chip->buffers), GFP_KERNEL);
  2794. if (!chip->buffers)
  2795. return -ENOMEM;
  2796. /* Set the internal oob buffer location, just after the page data */
  2797. chip->oob_poi = chip->buffers->databuf + mtd->writesize;
  2798. /*
  2799. * If no default placement scheme is given, select an appropriate one.
  2800. */
  2801. if (!chip->ecc.layout && (chip->ecc.mode != NAND_ECC_SOFT_BCH)) {
  2802. switch (mtd->oobsize) {
  2803. case 8:
  2804. chip->ecc.layout = &nand_oob_8;
  2805. break;
  2806. case 16:
  2807. chip->ecc.layout = &nand_oob_16;
  2808. break;
  2809. case 64:
  2810. chip->ecc.layout = &nand_oob_64;
  2811. break;
  2812. case 128:
  2813. chip->ecc.layout = &nand_oob_128;
  2814. break;
  2815. default:
  2816. pr_warn("No oob scheme defined for oobsize %d\n",
  2817. mtd->oobsize);
  2818. BUG();
  2819. }
  2820. }
  2821. if (!chip->write_page)
  2822. chip->write_page = nand_write_page;
  2823. /*
  2824. * Check ECC mode, default to software if 3byte/512byte hardware ECC is
  2825. * selected and we have 256 byte pagesize fallback to software ECC
  2826. */
  2827. switch (chip->ecc.mode) {
  2828. case NAND_ECC_HW_OOB_FIRST:
  2829. /* Similar to NAND_ECC_HW, but a separate read_page handle */
  2830. if (!chip->ecc.calculate || !chip->ecc.correct ||
  2831. !chip->ecc.hwctl) {
  2832. pr_warn("No ECC functions supplied; "
  2833. "hardware ECC not possible\n");
  2834. BUG();
  2835. }
  2836. if (!chip->ecc.read_page)
  2837. chip->ecc.read_page = nand_read_page_hwecc_oob_first;
  2838. case NAND_ECC_HW:
  2839. /* Use standard hwecc read page function? */
  2840. if (!chip->ecc.read_page)
  2841. chip->ecc.read_page = nand_read_page_hwecc;
  2842. if (!chip->ecc.write_page)
  2843. chip->ecc.write_page = nand_write_page_hwecc;
  2844. if (!chip->ecc.read_page_raw)
  2845. chip->ecc.read_page_raw = nand_read_page_raw;
  2846. if (!chip->ecc.write_page_raw)
  2847. chip->ecc.write_page_raw = nand_write_page_raw;
  2848. if (!chip->ecc.read_oob)
  2849. chip->ecc.read_oob = nand_read_oob_std;
  2850. if (!chip->ecc.write_oob)
  2851. chip->ecc.write_oob = nand_write_oob_std;
  2852. case NAND_ECC_HW_SYNDROME:
  2853. if ((!chip->ecc.calculate || !chip->ecc.correct ||
  2854. !chip->ecc.hwctl) &&
  2855. (!chip->ecc.read_page ||
  2856. chip->ecc.read_page == nand_read_page_hwecc ||
  2857. !chip->ecc.write_page ||
  2858. chip->ecc.write_page == nand_write_page_hwecc)) {
  2859. pr_warn("No ECC functions supplied; "
  2860. "hardware ECC not possible\n");
  2861. BUG();
  2862. }
  2863. /* Use standard syndrome read/write page function? */
  2864. if (!chip->ecc.read_page)
  2865. chip->ecc.read_page = nand_read_page_syndrome;
  2866. if (!chip->ecc.write_page)
  2867. chip->ecc.write_page = nand_write_page_syndrome;
  2868. if (!chip->ecc.read_page_raw)
  2869. chip->ecc.read_page_raw = nand_read_page_raw_syndrome;
  2870. if (!chip->ecc.write_page_raw)
  2871. chip->ecc.write_page_raw = nand_write_page_raw_syndrome;
  2872. if (!chip->ecc.read_oob)
  2873. chip->ecc.read_oob = nand_read_oob_syndrome;
  2874. if (!chip->ecc.write_oob)
  2875. chip->ecc.write_oob = nand_write_oob_syndrome;
  2876. if (mtd->writesize >= chip->ecc.size)
  2877. break;
  2878. pr_warn("%d byte HW ECC not possible on "
  2879. "%d byte page size, fallback to SW ECC\n",
  2880. chip->ecc.size, mtd->writesize);
  2881. chip->ecc.mode = NAND_ECC_SOFT;
  2882. case NAND_ECC_SOFT:
  2883. chip->ecc.calculate = nand_calculate_ecc;
  2884. chip->ecc.correct = nand_correct_data;
  2885. chip->ecc.read_page = nand_read_page_swecc;
  2886. chip->ecc.read_subpage = nand_read_subpage;
  2887. chip->ecc.write_page = nand_write_page_swecc;
  2888. chip->ecc.read_page_raw = nand_read_page_raw;
  2889. chip->ecc.write_page_raw = nand_write_page_raw;
  2890. chip->ecc.read_oob = nand_read_oob_std;
  2891. chip->ecc.write_oob = nand_write_oob_std;
  2892. if (!chip->ecc.size)
  2893. chip->ecc.size = 256;
  2894. chip->ecc.bytes = 3;
  2895. chip->ecc.strength = 1;
  2896. break;
  2897. case NAND_ECC_SOFT_BCH:
  2898. if (!mtd_nand_has_bch()) {
  2899. pr_warn("CONFIG_MTD_ECC_BCH not enabled\n");
  2900. BUG();
  2901. }
  2902. chip->ecc.calculate = nand_bch_calculate_ecc;
  2903. chip->ecc.correct = nand_bch_correct_data;
  2904. chip->ecc.read_page = nand_read_page_swecc;
  2905. chip->ecc.read_subpage = nand_read_subpage;
  2906. chip->ecc.write_page = nand_write_page_swecc;
  2907. chip->ecc.read_page_raw = nand_read_page_raw;
  2908. chip->ecc.write_page_raw = nand_write_page_raw;
  2909. chip->ecc.read_oob = nand_read_oob_std;
  2910. chip->ecc.write_oob = nand_write_oob_std;
  2911. /*
  2912. * Board driver should supply ecc.size and ecc.bytes values to
  2913. * select how many bits are correctable; see nand_bch_init()
  2914. * for details. Otherwise, default to 4 bits for large page
  2915. * devices.
  2916. */
  2917. if (!chip->ecc.size && (mtd->oobsize >= 64)) {
  2918. chip->ecc.size = 512;
  2919. chip->ecc.bytes = 7;
  2920. }
  2921. chip->ecc.priv = nand_bch_init(mtd,
  2922. chip->ecc.size,
  2923. chip->ecc.bytes,
  2924. &chip->ecc.layout);
  2925. if (!chip->ecc.priv) {
  2926. pr_warn("BCH ECC initialization failed!\n");
  2927. BUG();
  2928. }
  2929. chip->ecc.strength =
  2930. chip->ecc.bytes*8 / fls(8*chip->ecc.size);
  2931. break;
  2932. case NAND_ECC_NONE:
  2933. pr_warn("NAND_ECC_NONE selected by board driver. "
  2934. "This is not recommended!\n");
  2935. chip->ecc.read_page = nand_read_page_raw;
  2936. chip->ecc.write_page = nand_write_page_raw;
  2937. chip->ecc.read_oob = nand_read_oob_std;
  2938. chip->ecc.read_page_raw = nand_read_page_raw;
  2939. chip->ecc.write_page_raw = nand_write_page_raw;
  2940. chip->ecc.write_oob = nand_write_oob_std;
  2941. chip->ecc.size = mtd->writesize;
  2942. chip->ecc.bytes = 0;
  2943. chip->ecc.strength = 0;
  2944. break;
  2945. default:
  2946. pr_warn("Invalid NAND_ECC_MODE %d\n", chip->ecc.mode);
  2947. BUG();
  2948. }
  2949. /* For many systems, the standard OOB write also works for raw */
  2950. if (!chip->ecc.read_oob_raw)
  2951. chip->ecc.read_oob_raw = chip->ecc.read_oob;
  2952. if (!chip->ecc.write_oob_raw)
  2953. chip->ecc.write_oob_raw = chip->ecc.write_oob;
  2954. /*
  2955. * The number of bytes available for a client to place data into
  2956. * the out of band area.
  2957. */
  2958. chip->ecc.layout->oobavail = 0;
  2959. for (i = 0; chip->ecc.layout->oobfree[i].length
  2960. && i < ARRAY_SIZE(chip->ecc.layout->oobfree); i++)
  2961. chip->ecc.layout->oobavail +=
  2962. chip->ecc.layout->oobfree[i].length;
  2963. mtd->oobavail = chip->ecc.layout->oobavail;
  2964. /*
  2965. * Set the number of read / write steps for one page depending on ECC
  2966. * mode.
  2967. */
  2968. chip->ecc.steps = mtd->writesize / chip->ecc.size;
  2969. if (chip->ecc.steps * chip->ecc.size != mtd->writesize) {
  2970. pr_warn("Invalid ECC parameters\n");
  2971. BUG();
  2972. }
  2973. chip->ecc.total = chip->ecc.steps * chip->ecc.bytes;
  2974. /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */
  2975. if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
  2976. !(chip->cellinfo & NAND_CI_CELLTYPE_MSK)) {
  2977. switch (chip->ecc.steps) {
  2978. case 2:
  2979. mtd->subpage_sft = 1;
  2980. break;
  2981. case 4:
  2982. case 8:
  2983. case 16:
  2984. mtd->subpage_sft = 2;
  2985. break;
  2986. }
  2987. }
  2988. chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
  2989. /* Initialize state */
  2990. chip->state = FL_READY;
  2991. /* De-select the device */
  2992. chip->select_chip(mtd, -1);
  2993. /* Invalidate the pagebuffer reference */
  2994. chip->pagebuf = -1;
  2995. /* Fill in remaining MTD driver data */
  2996. mtd->type = MTD_NANDFLASH;
  2997. mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM :
  2998. MTD_CAP_NANDFLASH;
  2999. mtd->_erase = nand_erase;
  3000. mtd->_point = NULL;
  3001. mtd->_unpoint = NULL;
  3002. mtd->_read = nand_read;
  3003. mtd->_write = nand_write;
  3004. mtd->_panic_write = panic_nand_write;
  3005. mtd->_read_oob = nand_read_oob;
  3006. mtd->_write_oob = nand_write_oob;
  3007. mtd->_sync = nand_sync;
  3008. mtd->_lock = NULL;
  3009. mtd->_unlock = NULL;
  3010. mtd->_suspend = nand_suspend;
  3011. mtd->_resume = nand_resume;
  3012. mtd->_block_isbad = nand_block_isbad;
  3013. mtd->_block_markbad = nand_block_markbad;
  3014. mtd->writebufsize = mtd->writesize;
  3015. /* propagate ecc info to mtd_info */
  3016. mtd->ecclayout = chip->ecc.layout;
  3017. mtd->ecc_strength = chip->ecc.strength * chip->ecc.steps;
  3018. /* Check, if we should skip the bad block table scan */
  3019. if (chip->options & NAND_SKIP_BBTSCAN)
  3020. return 0;
  3021. /* Build bad block table */
  3022. return chip->scan_bbt(mtd);
  3023. }
  3024. EXPORT_SYMBOL(nand_scan_tail);
  3025. /*
  3026. * is_module_text_address() isn't exported, and it's mostly a pointless
  3027. * test if this is a module _anyway_ -- they'd have to try _really_ hard
  3028. * to call us from in-kernel code if the core NAND support is modular.
  3029. */
  3030. #ifdef MODULE
  3031. #define caller_is_module() (1)
  3032. #else
  3033. #define caller_is_module() \
  3034. is_module_text_address((unsigned long)__builtin_return_address(0))
  3035. #endif
  3036. /**
  3037. * nand_scan - [NAND Interface] Scan for the NAND device
  3038. * @mtd: MTD device structure
  3039. * @maxchips: number of chips to scan for
  3040. *
  3041. * This fills out all the uninitialized function pointers with the defaults.
  3042. * The flash ID is read and the mtd/chip structures are filled with the
  3043. * appropriate values. The mtd->owner field must be set to the module of the
  3044. * caller.
  3045. */
  3046. int nand_scan(struct mtd_info *mtd, int maxchips)
  3047. {
  3048. int ret;
  3049. /* Many callers got this wrong, so check for it for a while... */
  3050. if (!mtd->owner && caller_is_module()) {
  3051. pr_crit("%s called with NULL mtd->owner!\n", __func__);
  3052. BUG();
  3053. }
  3054. ret = nand_scan_ident(mtd, maxchips, NULL);
  3055. if (!ret)
  3056. ret = nand_scan_tail(mtd);
  3057. return ret;
  3058. }
  3059. EXPORT_SYMBOL(nand_scan);
  3060. /**
  3061. * nand_release - [NAND Interface] Free resources held by the NAND device
  3062. * @mtd: MTD device structure
  3063. */
  3064. void nand_release(struct mtd_info *mtd)
  3065. {
  3066. struct nand_chip *chip = mtd->priv;
  3067. if (chip->ecc.mode == NAND_ECC_SOFT_BCH)
  3068. nand_bch_free((struct nand_bch_control *)chip->ecc.priv);
  3069. mtd_device_unregister(mtd);
  3070. /* Free bad block table memory */
  3071. kfree(chip->bbt);
  3072. if (!(chip->options & NAND_OWN_BUFFERS))
  3073. kfree(chip->buffers);
  3074. /* Free bad block descriptor memory */
  3075. if (chip->badblock_pattern && chip->badblock_pattern->options
  3076. & NAND_BBT_DYNAMICSTRUCT)
  3077. kfree(chip->badblock_pattern);
  3078. }
  3079. EXPORT_SYMBOL_GPL(nand_release);
  3080. static int __init nand_base_init(void)
  3081. {
  3082. led_trigger_register_simple("nand-disk", &nand_led_trigger);
  3083. return 0;
  3084. }
  3085. static void __exit nand_base_exit(void)
  3086. {
  3087. led_trigger_unregister_simple(nand_led_trigger);
  3088. }
  3089. module_init(nand_base_init);
  3090. module_exit(nand_base_exit);
  3091. MODULE_LICENSE("GPL");
  3092. MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>");
  3093. MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>");
  3094. MODULE_DESCRIPTION("Generic NAND flash driver code");