nand_base.c 93 KB

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