nand_base.c 114 KB

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