nand_base.c 111 KB

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