onenand_base.c 108 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163
  1. /*
  2. * linux/drivers/mtd/onenand/onenand_base.c
  3. *
  4. * Copyright © 2005-2009 Samsung Electronics
  5. * Copyright © 2007 Nokia Corporation
  6. *
  7. * Kyungmin Park <kyungmin.park@samsung.com>
  8. *
  9. * Credits:
  10. * Adrian Hunter <ext-adrian.hunter@nokia.com>:
  11. * auto-placement support, read-while load support, various fixes
  12. *
  13. * Vishak G <vishak.g at samsung.com>, Rohit Hagargundgi <h.rohit at samsung.com>
  14. * Flex-OneNAND support
  15. * Amul Kumar Saha <amul.saha at samsung.com>
  16. * OTP support
  17. *
  18. * This program is free software; you can redistribute it and/or modify
  19. * it under the terms of the GNU General Public License version 2 as
  20. * published by the Free Software Foundation.
  21. */
  22. #include <linux/kernel.h>
  23. #include <linux/module.h>
  24. #include <linux/moduleparam.h>
  25. #include <linux/slab.h>
  26. #include <linux/init.h>
  27. #include <linux/sched.h>
  28. #include <linux/delay.h>
  29. #include <linux/interrupt.h>
  30. #include <linux/jiffies.h>
  31. #include <linux/mtd/mtd.h>
  32. #include <linux/mtd/onenand.h>
  33. #include <linux/mtd/partitions.h>
  34. #include <asm/io.h>
  35. /*
  36. * Multiblock erase if number of blocks to erase is 2 or more.
  37. * Maximum number of blocks for simultaneous erase is 64.
  38. */
  39. #define MB_ERASE_MIN_BLK_COUNT 2
  40. #define MB_ERASE_MAX_BLK_COUNT 64
  41. /* Default Flex-OneNAND boundary and lock respectively */
  42. static int flex_bdry[MAX_DIES * 2] = { -1, 0, -1, 0 };
  43. module_param_array(flex_bdry, int, NULL, 0400);
  44. MODULE_PARM_DESC(flex_bdry, "SLC Boundary information for Flex-OneNAND"
  45. "Syntax:flex_bdry=DIE_BDRY,LOCK,..."
  46. "DIE_BDRY: SLC boundary of the die"
  47. "LOCK: Locking information for SLC boundary"
  48. " : 0->Set boundary in unlocked status"
  49. " : 1->Set boundary in locked status");
  50. /* Default OneNAND/Flex-OneNAND OTP options*/
  51. static int otp;
  52. module_param(otp, int, 0400);
  53. MODULE_PARM_DESC(otp, "Corresponding behaviour of OneNAND in OTP"
  54. "Syntax : otp=LOCK_TYPE"
  55. "LOCK_TYPE : Keys issued, for specific OTP Lock type"
  56. " : 0 -> Default (No Blocks Locked)"
  57. " : 1 -> OTP Block lock"
  58. " : 2 -> 1st Block lock"
  59. " : 3 -> BOTH OTP Block and 1st Block lock");
  60. /*
  61. * flexonenand_oob_128 - oob info for Flex-Onenand with 4KB page
  62. * For now, we expose only 64 out of 80 ecc bytes
  63. */
  64. static struct nand_ecclayout flexonenand_oob_128 = {
  65. .eccbytes = 64,
  66. .eccpos = {
  67. 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
  68. 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
  69. 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
  70. 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
  71. 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
  72. 86, 87, 88, 89, 90, 91, 92, 93, 94, 95,
  73. 102, 103, 104, 105
  74. },
  75. .oobfree = {
  76. {2, 4}, {18, 4}, {34, 4}, {50, 4},
  77. {66, 4}, {82, 4}, {98, 4}, {114, 4}
  78. }
  79. };
  80. /*
  81. * onenand_oob_128 - oob info for OneNAND with 4KB page
  82. *
  83. * Based on specification:
  84. * 4Gb M-die OneNAND Flash (KFM4G16Q4M, KFN8G16Q4M). Rev. 1.3, Apr. 2010
  85. *
  86. * For eccpos we expose only 64 bytes out of 72 (see struct nand_ecclayout)
  87. *
  88. * oobfree uses the spare area fields marked as
  89. * "Managed by internal ECC logic for Logical Sector Number area"
  90. */
  91. static struct nand_ecclayout onenand_oob_128 = {
  92. .eccbytes = 64,
  93. .eccpos = {
  94. 7, 8, 9, 10, 11, 12, 13, 14, 15,
  95. 23, 24, 25, 26, 27, 28, 29, 30, 31,
  96. 39, 40, 41, 42, 43, 44, 45, 46, 47,
  97. 55, 56, 57, 58, 59, 60, 61, 62, 63,
  98. 71, 72, 73, 74, 75, 76, 77, 78, 79,
  99. 87, 88, 89, 90, 91, 92, 93, 94, 95,
  100. 103, 104, 105, 106, 107, 108, 109, 110, 111,
  101. 119
  102. },
  103. .oobfree = {
  104. {2, 3}, {18, 3}, {34, 3}, {50, 3},
  105. {66, 3}, {82, 3}, {98, 3}, {114, 3}
  106. }
  107. };
  108. /**
  109. * onenand_oob_64 - oob info for large (2KB) page
  110. */
  111. static struct nand_ecclayout onenand_oob_64 = {
  112. .eccbytes = 20,
  113. .eccpos = {
  114. 8, 9, 10, 11, 12,
  115. 24, 25, 26, 27, 28,
  116. 40, 41, 42, 43, 44,
  117. 56, 57, 58, 59, 60,
  118. },
  119. .oobfree = {
  120. {2, 3}, {14, 2}, {18, 3}, {30, 2},
  121. {34, 3}, {46, 2}, {50, 3}, {62, 2}
  122. }
  123. };
  124. /**
  125. * onenand_oob_32 - oob info for middle (1KB) page
  126. */
  127. static struct nand_ecclayout onenand_oob_32 = {
  128. .eccbytes = 10,
  129. .eccpos = {
  130. 8, 9, 10, 11, 12,
  131. 24, 25, 26, 27, 28,
  132. },
  133. .oobfree = { {2, 3}, {14, 2}, {18, 3}, {30, 2} }
  134. };
  135. static const unsigned char ffchars[] = {
  136. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  137. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 16 */
  138. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  139. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 32 */
  140. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  141. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 48 */
  142. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  143. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 64 */
  144. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  145. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 80 */
  146. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  147. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 96 */
  148. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  149. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 112 */
  150. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  151. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 128 */
  152. };
  153. /**
  154. * onenand_readw - [OneNAND Interface] Read OneNAND register
  155. * @param addr address to read
  156. *
  157. * Read OneNAND register
  158. */
  159. static unsigned short onenand_readw(void __iomem *addr)
  160. {
  161. return readw(addr);
  162. }
  163. /**
  164. * onenand_writew - [OneNAND Interface] Write OneNAND register with value
  165. * @param value value to write
  166. * @param addr address to write
  167. *
  168. * Write OneNAND register with value
  169. */
  170. static void onenand_writew(unsigned short value, void __iomem *addr)
  171. {
  172. writew(value, addr);
  173. }
  174. /**
  175. * onenand_block_address - [DEFAULT] Get block address
  176. * @param this onenand chip data structure
  177. * @param block the block
  178. * @return translated block address if DDP, otherwise same
  179. *
  180. * Setup Start Address 1 Register (F100h)
  181. */
  182. static int onenand_block_address(struct onenand_chip *this, int block)
  183. {
  184. /* Device Flash Core select, NAND Flash Block Address */
  185. if (block & this->density_mask)
  186. return ONENAND_DDP_CHIP1 | (block ^ this->density_mask);
  187. return block;
  188. }
  189. /**
  190. * onenand_bufferram_address - [DEFAULT] Get bufferram address
  191. * @param this onenand chip data structure
  192. * @param block the block
  193. * @return set DBS value if DDP, otherwise 0
  194. *
  195. * Setup Start Address 2 Register (F101h) for DDP
  196. */
  197. static int onenand_bufferram_address(struct onenand_chip *this, int block)
  198. {
  199. /* Device BufferRAM Select */
  200. if (block & this->density_mask)
  201. return ONENAND_DDP_CHIP1;
  202. return ONENAND_DDP_CHIP0;
  203. }
  204. /**
  205. * onenand_page_address - [DEFAULT] Get page address
  206. * @param page the page address
  207. * @param sector the sector address
  208. * @return combined page and sector address
  209. *
  210. * Setup Start Address 8 Register (F107h)
  211. */
  212. static int onenand_page_address(int page, int sector)
  213. {
  214. /* Flash Page Address, Flash Sector Address */
  215. int fpa, fsa;
  216. fpa = page & ONENAND_FPA_MASK;
  217. fsa = sector & ONENAND_FSA_MASK;
  218. return ((fpa << ONENAND_FPA_SHIFT) | fsa);
  219. }
  220. /**
  221. * onenand_buffer_address - [DEFAULT] Get buffer address
  222. * @param dataram1 DataRAM index
  223. * @param sectors the sector address
  224. * @param count the number of sectors
  225. * @return the start buffer value
  226. *
  227. * Setup Start Buffer Register (F200h)
  228. */
  229. static int onenand_buffer_address(int dataram1, int sectors, int count)
  230. {
  231. int bsa, bsc;
  232. /* BufferRAM Sector Address */
  233. bsa = sectors & ONENAND_BSA_MASK;
  234. if (dataram1)
  235. bsa |= ONENAND_BSA_DATARAM1; /* DataRAM1 */
  236. else
  237. bsa |= ONENAND_BSA_DATARAM0; /* DataRAM0 */
  238. /* BufferRAM Sector Count */
  239. bsc = count & ONENAND_BSC_MASK;
  240. return ((bsa << ONENAND_BSA_SHIFT) | bsc);
  241. }
  242. /**
  243. * flexonenand_block- For given address return block number
  244. * @param this - OneNAND device structure
  245. * @param addr - Address for which block number is needed
  246. */
  247. static unsigned flexonenand_block(struct onenand_chip *this, loff_t addr)
  248. {
  249. unsigned boundary, blk, die = 0;
  250. if (ONENAND_IS_DDP(this) && addr >= this->diesize[0]) {
  251. die = 1;
  252. addr -= this->diesize[0];
  253. }
  254. boundary = this->boundary[die];
  255. blk = addr >> (this->erase_shift - 1);
  256. if (blk > boundary)
  257. blk = (blk + boundary + 1) >> 1;
  258. blk += die ? this->density_mask : 0;
  259. return blk;
  260. }
  261. inline unsigned onenand_block(struct onenand_chip *this, loff_t addr)
  262. {
  263. if (!FLEXONENAND(this))
  264. return addr >> this->erase_shift;
  265. return flexonenand_block(this, addr);
  266. }
  267. /**
  268. * flexonenand_addr - Return address of the block
  269. * @this: OneNAND device structure
  270. * @block: Block number on Flex-OneNAND
  271. *
  272. * Return address of the block
  273. */
  274. static loff_t flexonenand_addr(struct onenand_chip *this, int block)
  275. {
  276. loff_t ofs = 0;
  277. int die = 0, boundary;
  278. if (ONENAND_IS_DDP(this) && block >= this->density_mask) {
  279. block -= this->density_mask;
  280. die = 1;
  281. ofs = this->diesize[0];
  282. }
  283. boundary = this->boundary[die];
  284. ofs += (loff_t)block << (this->erase_shift - 1);
  285. if (block > (boundary + 1))
  286. ofs += (loff_t)(block - boundary - 1) << (this->erase_shift - 1);
  287. return ofs;
  288. }
  289. loff_t onenand_addr(struct onenand_chip *this, int block)
  290. {
  291. if (!FLEXONENAND(this))
  292. return (loff_t)block << this->erase_shift;
  293. return flexonenand_addr(this, block);
  294. }
  295. EXPORT_SYMBOL(onenand_addr);
  296. /**
  297. * onenand_get_density - [DEFAULT] Get OneNAND density
  298. * @param dev_id OneNAND device ID
  299. *
  300. * Get OneNAND density from device ID
  301. */
  302. static inline int onenand_get_density(int dev_id)
  303. {
  304. int density = dev_id >> ONENAND_DEVICE_DENSITY_SHIFT;
  305. return (density & ONENAND_DEVICE_DENSITY_MASK);
  306. }
  307. /**
  308. * flexonenand_region - [Flex-OneNAND] Return erase region of addr
  309. * @param mtd MTD device structure
  310. * @param addr address whose erase region needs to be identified
  311. */
  312. int flexonenand_region(struct mtd_info *mtd, loff_t addr)
  313. {
  314. int i;
  315. for (i = 0; i < mtd->numeraseregions; i++)
  316. if (addr < mtd->eraseregions[i].offset)
  317. break;
  318. return i - 1;
  319. }
  320. EXPORT_SYMBOL(flexonenand_region);
  321. /**
  322. * onenand_command - [DEFAULT] Send command to OneNAND device
  323. * @param mtd MTD device structure
  324. * @param cmd the command to be sent
  325. * @param addr offset to read from or write to
  326. * @param len number of bytes to read or write
  327. *
  328. * Send command to OneNAND device. This function is used for middle/large page
  329. * devices (1KB/2KB Bytes per page)
  330. */
  331. static int onenand_command(struct mtd_info *mtd, int cmd, loff_t addr, size_t len)
  332. {
  333. struct onenand_chip *this = mtd->priv;
  334. int value, block, page;
  335. /* Address translation */
  336. switch (cmd) {
  337. case ONENAND_CMD_UNLOCK:
  338. case ONENAND_CMD_LOCK:
  339. case ONENAND_CMD_LOCK_TIGHT:
  340. case ONENAND_CMD_UNLOCK_ALL:
  341. block = -1;
  342. page = -1;
  343. break;
  344. case FLEXONENAND_CMD_PI_ACCESS:
  345. /* addr contains die index */
  346. block = addr * this->density_mask;
  347. page = -1;
  348. break;
  349. case ONENAND_CMD_ERASE:
  350. case ONENAND_CMD_MULTIBLOCK_ERASE:
  351. case ONENAND_CMD_ERASE_VERIFY:
  352. case ONENAND_CMD_BUFFERRAM:
  353. case ONENAND_CMD_OTP_ACCESS:
  354. block = onenand_block(this, addr);
  355. page = -1;
  356. break;
  357. case FLEXONENAND_CMD_READ_PI:
  358. cmd = ONENAND_CMD_READ;
  359. block = addr * this->density_mask;
  360. page = 0;
  361. break;
  362. default:
  363. block = onenand_block(this, addr);
  364. if (FLEXONENAND(this))
  365. page = (int) (addr - onenand_addr(this, block))>>\
  366. this->page_shift;
  367. else
  368. page = (int) (addr >> this->page_shift);
  369. if (ONENAND_IS_2PLANE(this)) {
  370. /* Make the even block number */
  371. block &= ~1;
  372. /* Is it the odd plane? */
  373. if (addr & this->writesize)
  374. block++;
  375. page >>= 1;
  376. }
  377. page &= this->page_mask;
  378. break;
  379. }
  380. /* NOTE: The setting order of the registers is very important! */
  381. if (cmd == ONENAND_CMD_BUFFERRAM) {
  382. /* Select DataRAM for DDP */
  383. value = onenand_bufferram_address(this, block);
  384. this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
  385. if (ONENAND_IS_2PLANE(this) || ONENAND_IS_4KB_PAGE(this))
  386. /* It is always BufferRAM0 */
  387. ONENAND_SET_BUFFERRAM0(this);
  388. else
  389. /* Switch to the next data buffer */
  390. ONENAND_SET_NEXT_BUFFERRAM(this);
  391. return 0;
  392. }
  393. if (block != -1) {
  394. /* Write 'DFS, FBA' of Flash */
  395. value = onenand_block_address(this, block);
  396. this->write_word(value, this->base + ONENAND_REG_START_ADDRESS1);
  397. /* Select DataRAM for DDP */
  398. value = onenand_bufferram_address(this, block);
  399. this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
  400. }
  401. if (page != -1) {
  402. /* Now we use page size operation */
  403. int sectors = 0, count = 0;
  404. int dataram;
  405. switch (cmd) {
  406. case FLEXONENAND_CMD_RECOVER_LSB:
  407. case ONENAND_CMD_READ:
  408. case ONENAND_CMD_READOOB:
  409. if (ONENAND_IS_4KB_PAGE(this))
  410. /* It is always BufferRAM0 */
  411. dataram = ONENAND_SET_BUFFERRAM0(this);
  412. else
  413. dataram = ONENAND_SET_NEXT_BUFFERRAM(this);
  414. break;
  415. default:
  416. if (ONENAND_IS_2PLANE(this) && cmd == ONENAND_CMD_PROG)
  417. cmd = ONENAND_CMD_2X_PROG;
  418. dataram = ONENAND_CURRENT_BUFFERRAM(this);
  419. break;
  420. }
  421. /* Write 'FPA, FSA' of Flash */
  422. value = onenand_page_address(page, sectors);
  423. this->write_word(value, this->base + ONENAND_REG_START_ADDRESS8);
  424. /* Write 'BSA, BSC' of DataRAM */
  425. value = onenand_buffer_address(dataram, sectors, count);
  426. this->write_word(value, this->base + ONENAND_REG_START_BUFFER);
  427. }
  428. /* Interrupt clear */
  429. this->write_word(ONENAND_INT_CLEAR, this->base + ONENAND_REG_INTERRUPT);
  430. /* Write command */
  431. this->write_word(cmd, this->base + ONENAND_REG_COMMAND);
  432. return 0;
  433. }
  434. /**
  435. * onenand_read_ecc - return ecc status
  436. * @param this onenand chip structure
  437. */
  438. static inline int onenand_read_ecc(struct onenand_chip *this)
  439. {
  440. int ecc, i, result = 0;
  441. if (!FLEXONENAND(this) && !ONENAND_IS_4KB_PAGE(this))
  442. return this->read_word(this->base + ONENAND_REG_ECC_STATUS);
  443. for (i = 0; i < 4; i++) {
  444. ecc = this->read_word(this->base + ONENAND_REG_ECC_STATUS + i*2);
  445. if (likely(!ecc))
  446. continue;
  447. if (ecc & FLEXONENAND_UNCORRECTABLE_ERROR)
  448. return ONENAND_ECC_2BIT_ALL;
  449. else
  450. result = ONENAND_ECC_1BIT_ALL;
  451. }
  452. return result;
  453. }
  454. /**
  455. * onenand_wait - [DEFAULT] wait until the command is done
  456. * @param mtd MTD device structure
  457. * @param state state to select the max. timeout value
  458. *
  459. * Wait for command done. This applies to all OneNAND command
  460. * Read can take up to 30us, erase up to 2ms and program up to 350us
  461. * according to general OneNAND specs
  462. */
  463. static int onenand_wait(struct mtd_info *mtd, int state)
  464. {
  465. struct onenand_chip * this = mtd->priv;
  466. unsigned long timeout;
  467. unsigned int flags = ONENAND_INT_MASTER;
  468. unsigned int interrupt = 0;
  469. unsigned int ctrl;
  470. /* The 20 msec is enough */
  471. timeout = jiffies + msecs_to_jiffies(20);
  472. while (time_before(jiffies, timeout)) {
  473. interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
  474. if (interrupt & flags)
  475. break;
  476. if (state != FL_READING && state != FL_PREPARING_ERASE)
  477. cond_resched();
  478. }
  479. /* To get correct interrupt status in timeout case */
  480. interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
  481. ctrl = this->read_word(this->base + ONENAND_REG_CTRL_STATUS);
  482. /*
  483. * In the Spec. it checks the controller status first
  484. * However if you get the correct information in case of
  485. * power off recovery (POR) test, it should read ECC status first
  486. */
  487. if (interrupt & ONENAND_INT_READ) {
  488. int ecc = onenand_read_ecc(this);
  489. if (ecc) {
  490. if (ecc & ONENAND_ECC_2BIT_ALL) {
  491. printk(KERN_ERR "%s: ECC error = 0x%04x\n",
  492. __func__, ecc);
  493. mtd->ecc_stats.failed++;
  494. return -EBADMSG;
  495. } else if (ecc & ONENAND_ECC_1BIT_ALL) {
  496. printk(KERN_DEBUG "%s: correctable ECC error = 0x%04x\n",
  497. __func__, ecc);
  498. mtd->ecc_stats.corrected++;
  499. }
  500. }
  501. } else if (state == FL_READING) {
  502. printk(KERN_ERR "%s: read timeout! ctrl=0x%04x intr=0x%04x\n",
  503. __func__, ctrl, interrupt);
  504. return -EIO;
  505. }
  506. if (state == FL_PREPARING_ERASE && !(interrupt & ONENAND_INT_ERASE)) {
  507. printk(KERN_ERR "%s: mb erase timeout! ctrl=0x%04x intr=0x%04x\n",
  508. __func__, ctrl, interrupt);
  509. return -EIO;
  510. }
  511. if (!(interrupt & ONENAND_INT_MASTER)) {
  512. printk(KERN_ERR "%s: timeout! ctrl=0x%04x intr=0x%04x\n",
  513. __func__, ctrl, interrupt);
  514. return -EIO;
  515. }
  516. /* If there's controller error, it's a real error */
  517. if (ctrl & ONENAND_CTRL_ERROR) {
  518. printk(KERN_ERR "%s: controller error = 0x%04x\n",
  519. __func__, ctrl);
  520. if (ctrl & ONENAND_CTRL_LOCK)
  521. printk(KERN_ERR "%s: it's locked error.\n", __func__);
  522. return -EIO;
  523. }
  524. return 0;
  525. }
  526. /*
  527. * onenand_interrupt - [DEFAULT] onenand interrupt handler
  528. * @param irq onenand interrupt number
  529. * @param dev_id interrupt data
  530. *
  531. * complete the work
  532. */
  533. static irqreturn_t onenand_interrupt(int irq, void *data)
  534. {
  535. struct onenand_chip *this = data;
  536. /* To handle shared interrupt */
  537. if (!this->complete.done)
  538. complete(&this->complete);
  539. return IRQ_HANDLED;
  540. }
  541. /*
  542. * onenand_interrupt_wait - [DEFAULT] wait until the command is done
  543. * @param mtd MTD device structure
  544. * @param state state to select the max. timeout value
  545. *
  546. * Wait for command done.
  547. */
  548. static int onenand_interrupt_wait(struct mtd_info *mtd, int state)
  549. {
  550. struct onenand_chip *this = mtd->priv;
  551. wait_for_completion(&this->complete);
  552. return onenand_wait(mtd, state);
  553. }
  554. /*
  555. * onenand_try_interrupt_wait - [DEFAULT] try interrupt wait
  556. * @param mtd MTD device structure
  557. * @param state state to select the max. timeout value
  558. *
  559. * Try interrupt based wait (It is used one-time)
  560. */
  561. static int onenand_try_interrupt_wait(struct mtd_info *mtd, int state)
  562. {
  563. struct onenand_chip *this = mtd->priv;
  564. unsigned long remain, timeout;
  565. /* We use interrupt wait first */
  566. this->wait = onenand_interrupt_wait;
  567. timeout = msecs_to_jiffies(100);
  568. remain = wait_for_completion_timeout(&this->complete, timeout);
  569. if (!remain) {
  570. printk(KERN_INFO "OneNAND: There's no interrupt. "
  571. "We use the normal wait\n");
  572. /* Release the irq */
  573. free_irq(this->irq, this);
  574. this->wait = onenand_wait;
  575. }
  576. return onenand_wait(mtd, state);
  577. }
  578. /*
  579. * onenand_setup_wait - [OneNAND Interface] setup onenand wait method
  580. * @param mtd MTD device structure
  581. *
  582. * There's two method to wait onenand work
  583. * 1. polling - read interrupt status register
  584. * 2. interrupt - use the kernel interrupt method
  585. */
  586. static void onenand_setup_wait(struct mtd_info *mtd)
  587. {
  588. struct onenand_chip *this = mtd->priv;
  589. int syscfg;
  590. init_completion(&this->complete);
  591. if (this->irq <= 0) {
  592. this->wait = onenand_wait;
  593. return;
  594. }
  595. if (request_irq(this->irq, &onenand_interrupt,
  596. IRQF_SHARED, "onenand", this)) {
  597. /* If we can't get irq, use the normal wait */
  598. this->wait = onenand_wait;
  599. return;
  600. }
  601. /* Enable interrupt */
  602. syscfg = this->read_word(this->base + ONENAND_REG_SYS_CFG1);
  603. syscfg |= ONENAND_SYS_CFG1_IOBE;
  604. this->write_word(syscfg, this->base + ONENAND_REG_SYS_CFG1);
  605. this->wait = onenand_try_interrupt_wait;
  606. }
  607. /**
  608. * onenand_bufferram_offset - [DEFAULT] BufferRAM offset
  609. * @param mtd MTD data structure
  610. * @param area BufferRAM area
  611. * @return offset given area
  612. *
  613. * Return BufferRAM offset given area
  614. */
  615. static inline int onenand_bufferram_offset(struct mtd_info *mtd, int area)
  616. {
  617. struct onenand_chip *this = mtd->priv;
  618. if (ONENAND_CURRENT_BUFFERRAM(this)) {
  619. /* Note: the 'this->writesize' is a real page size */
  620. if (area == ONENAND_DATARAM)
  621. return this->writesize;
  622. if (area == ONENAND_SPARERAM)
  623. return mtd->oobsize;
  624. }
  625. return 0;
  626. }
  627. /**
  628. * onenand_read_bufferram - [OneNAND Interface] Read the bufferram area
  629. * @param mtd MTD data structure
  630. * @param area BufferRAM area
  631. * @param buffer the databuffer to put/get data
  632. * @param offset offset to read from or write to
  633. * @param count number of bytes to read/write
  634. *
  635. * Read the BufferRAM area
  636. */
  637. static int onenand_read_bufferram(struct mtd_info *mtd, int area,
  638. unsigned char *buffer, int offset, size_t count)
  639. {
  640. struct onenand_chip *this = mtd->priv;
  641. void __iomem *bufferram;
  642. bufferram = this->base + area;
  643. bufferram += onenand_bufferram_offset(mtd, area);
  644. if (ONENAND_CHECK_BYTE_ACCESS(count)) {
  645. unsigned short word;
  646. /* Align with word(16-bit) size */
  647. count--;
  648. /* Read word and save byte */
  649. word = this->read_word(bufferram + offset + count);
  650. buffer[count] = (word & 0xff);
  651. }
  652. memcpy(buffer, bufferram + offset, count);
  653. return 0;
  654. }
  655. /**
  656. * onenand_sync_read_bufferram - [OneNAND Interface] Read the bufferram area with Sync. Burst mode
  657. * @param mtd MTD data structure
  658. * @param area BufferRAM area
  659. * @param buffer the databuffer to put/get data
  660. * @param offset offset to read from or write to
  661. * @param count number of bytes to read/write
  662. *
  663. * Read the BufferRAM area with Sync. Burst Mode
  664. */
  665. static int onenand_sync_read_bufferram(struct mtd_info *mtd, int area,
  666. unsigned char *buffer, int offset, size_t count)
  667. {
  668. struct onenand_chip *this = mtd->priv;
  669. void __iomem *bufferram;
  670. bufferram = this->base + area;
  671. bufferram += onenand_bufferram_offset(mtd, area);
  672. this->mmcontrol(mtd, ONENAND_SYS_CFG1_SYNC_READ);
  673. if (ONENAND_CHECK_BYTE_ACCESS(count)) {
  674. unsigned short word;
  675. /* Align with word(16-bit) size */
  676. count--;
  677. /* Read word and save byte */
  678. word = this->read_word(bufferram + offset + count);
  679. buffer[count] = (word & 0xff);
  680. }
  681. memcpy(buffer, bufferram + offset, count);
  682. this->mmcontrol(mtd, 0);
  683. return 0;
  684. }
  685. /**
  686. * onenand_write_bufferram - [OneNAND Interface] Write the bufferram area
  687. * @param mtd MTD data structure
  688. * @param area BufferRAM area
  689. * @param buffer the databuffer to put/get data
  690. * @param offset offset to read from or write to
  691. * @param count number of bytes to read/write
  692. *
  693. * Write the BufferRAM area
  694. */
  695. static int onenand_write_bufferram(struct mtd_info *mtd, int area,
  696. const unsigned char *buffer, int offset, size_t count)
  697. {
  698. struct onenand_chip *this = mtd->priv;
  699. void __iomem *bufferram;
  700. bufferram = this->base + area;
  701. bufferram += onenand_bufferram_offset(mtd, area);
  702. if (ONENAND_CHECK_BYTE_ACCESS(count)) {
  703. unsigned short word;
  704. int byte_offset;
  705. /* Align with word(16-bit) size */
  706. count--;
  707. /* Calculate byte access offset */
  708. byte_offset = offset + count;
  709. /* Read word and save byte */
  710. word = this->read_word(bufferram + byte_offset);
  711. word = (word & ~0xff) | buffer[count];
  712. this->write_word(word, bufferram + byte_offset);
  713. }
  714. memcpy(bufferram + offset, buffer, count);
  715. return 0;
  716. }
  717. /**
  718. * onenand_get_2x_blockpage - [GENERIC] Get blockpage at 2x program mode
  719. * @param mtd MTD data structure
  720. * @param addr address to check
  721. * @return blockpage address
  722. *
  723. * Get blockpage address at 2x program mode
  724. */
  725. static int onenand_get_2x_blockpage(struct mtd_info *mtd, loff_t addr)
  726. {
  727. struct onenand_chip *this = mtd->priv;
  728. int blockpage, block, page;
  729. /* Calculate the even block number */
  730. block = (int) (addr >> this->erase_shift) & ~1;
  731. /* Is it the odd plane? */
  732. if (addr & this->writesize)
  733. block++;
  734. page = (int) (addr >> (this->page_shift + 1)) & this->page_mask;
  735. blockpage = (block << 7) | page;
  736. return blockpage;
  737. }
  738. /**
  739. * onenand_check_bufferram - [GENERIC] Check BufferRAM information
  740. * @param mtd MTD data structure
  741. * @param addr address to check
  742. * @return 1 if there are valid data, otherwise 0
  743. *
  744. * Check bufferram if there is data we required
  745. */
  746. static int onenand_check_bufferram(struct mtd_info *mtd, loff_t addr)
  747. {
  748. struct onenand_chip *this = mtd->priv;
  749. int blockpage, found = 0;
  750. unsigned int i;
  751. if (ONENAND_IS_2PLANE(this))
  752. blockpage = onenand_get_2x_blockpage(mtd, addr);
  753. else
  754. blockpage = (int) (addr >> this->page_shift);
  755. /* Is there valid data? */
  756. i = ONENAND_CURRENT_BUFFERRAM(this);
  757. if (this->bufferram[i].blockpage == blockpage)
  758. found = 1;
  759. else {
  760. /* Check another BufferRAM */
  761. i = ONENAND_NEXT_BUFFERRAM(this);
  762. if (this->bufferram[i].blockpage == blockpage) {
  763. ONENAND_SET_NEXT_BUFFERRAM(this);
  764. found = 1;
  765. }
  766. }
  767. if (found && ONENAND_IS_DDP(this)) {
  768. /* Select DataRAM for DDP */
  769. int block = onenand_block(this, addr);
  770. int value = onenand_bufferram_address(this, block);
  771. this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
  772. }
  773. return found;
  774. }
  775. /**
  776. * onenand_update_bufferram - [GENERIC] Update BufferRAM information
  777. * @param mtd MTD data structure
  778. * @param addr address to update
  779. * @param valid valid flag
  780. *
  781. * Update BufferRAM information
  782. */
  783. static void onenand_update_bufferram(struct mtd_info *mtd, loff_t addr,
  784. int valid)
  785. {
  786. struct onenand_chip *this = mtd->priv;
  787. int blockpage;
  788. unsigned int i;
  789. if (ONENAND_IS_2PLANE(this))
  790. blockpage = onenand_get_2x_blockpage(mtd, addr);
  791. else
  792. blockpage = (int) (addr >> this->page_shift);
  793. /* Invalidate another BufferRAM */
  794. i = ONENAND_NEXT_BUFFERRAM(this);
  795. if (this->bufferram[i].blockpage == blockpage)
  796. this->bufferram[i].blockpage = -1;
  797. /* Update BufferRAM */
  798. i = ONENAND_CURRENT_BUFFERRAM(this);
  799. if (valid)
  800. this->bufferram[i].blockpage = blockpage;
  801. else
  802. this->bufferram[i].blockpage = -1;
  803. }
  804. /**
  805. * onenand_invalidate_bufferram - [GENERIC] Invalidate BufferRAM information
  806. * @param mtd MTD data structure
  807. * @param addr start address to invalidate
  808. * @param len length to invalidate
  809. *
  810. * Invalidate BufferRAM information
  811. */
  812. static void onenand_invalidate_bufferram(struct mtd_info *mtd, loff_t addr,
  813. unsigned int len)
  814. {
  815. struct onenand_chip *this = mtd->priv;
  816. int i;
  817. loff_t end_addr = addr + len;
  818. /* Invalidate BufferRAM */
  819. for (i = 0; i < MAX_BUFFERRAM; i++) {
  820. loff_t buf_addr = this->bufferram[i].blockpage << this->page_shift;
  821. if (buf_addr >= addr && buf_addr < end_addr)
  822. this->bufferram[i].blockpage = -1;
  823. }
  824. }
  825. /**
  826. * onenand_get_device - [GENERIC] Get chip for selected access
  827. * @param mtd MTD device structure
  828. * @param new_state the state which is requested
  829. *
  830. * Get the device and lock it for exclusive access
  831. */
  832. static int onenand_get_device(struct mtd_info *mtd, int new_state)
  833. {
  834. struct onenand_chip *this = mtd->priv;
  835. DECLARE_WAITQUEUE(wait, current);
  836. /*
  837. * Grab the lock and see if the device is available
  838. */
  839. while (1) {
  840. spin_lock(&this->chip_lock);
  841. if (this->state == FL_READY) {
  842. this->state = new_state;
  843. spin_unlock(&this->chip_lock);
  844. if (new_state != FL_PM_SUSPENDED && this->enable)
  845. this->enable(mtd);
  846. break;
  847. }
  848. if (new_state == FL_PM_SUSPENDED) {
  849. spin_unlock(&this->chip_lock);
  850. return (this->state == FL_PM_SUSPENDED) ? 0 : -EAGAIN;
  851. }
  852. set_current_state(TASK_UNINTERRUPTIBLE);
  853. add_wait_queue(&this->wq, &wait);
  854. spin_unlock(&this->chip_lock);
  855. schedule();
  856. remove_wait_queue(&this->wq, &wait);
  857. }
  858. return 0;
  859. }
  860. /**
  861. * onenand_release_device - [GENERIC] release chip
  862. * @param mtd MTD device structure
  863. *
  864. * Deselect, release chip lock and wake up anyone waiting on the device
  865. */
  866. static void onenand_release_device(struct mtd_info *mtd)
  867. {
  868. struct onenand_chip *this = mtd->priv;
  869. if (this->state != FL_PM_SUSPENDED && this->disable)
  870. this->disable(mtd);
  871. /* Release the chip */
  872. spin_lock(&this->chip_lock);
  873. this->state = FL_READY;
  874. wake_up(&this->wq);
  875. spin_unlock(&this->chip_lock);
  876. }
  877. /**
  878. * onenand_transfer_auto_oob - [INTERN] oob auto-placement transfer
  879. * @param mtd MTD device structure
  880. * @param buf destination address
  881. * @param column oob offset to read from
  882. * @param thislen oob length to read
  883. */
  884. static int onenand_transfer_auto_oob(struct mtd_info *mtd, uint8_t *buf, int column,
  885. int thislen)
  886. {
  887. struct onenand_chip *this = mtd->priv;
  888. struct nand_oobfree *free;
  889. int readcol = column;
  890. int readend = column + thislen;
  891. int lastgap = 0;
  892. unsigned int i;
  893. uint8_t *oob_buf = this->oob_buf;
  894. free = this->ecclayout->oobfree;
  895. for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES && free->length; i++, free++) {
  896. if (readcol >= lastgap)
  897. readcol += free->offset - lastgap;
  898. if (readend >= lastgap)
  899. readend += free->offset - lastgap;
  900. lastgap = free->offset + free->length;
  901. }
  902. this->read_bufferram(mtd, ONENAND_SPARERAM, oob_buf, 0, mtd->oobsize);
  903. free = this->ecclayout->oobfree;
  904. for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES && free->length; i++, free++) {
  905. int free_end = free->offset + free->length;
  906. if (free->offset < readend && free_end > readcol) {
  907. int st = max_t(int,free->offset,readcol);
  908. int ed = min_t(int,free_end,readend);
  909. int n = ed - st;
  910. memcpy(buf, oob_buf + st, n);
  911. buf += n;
  912. } else if (column == 0)
  913. break;
  914. }
  915. return 0;
  916. }
  917. /**
  918. * onenand_recover_lsb - [Flex-OneNAND] Recover LSB page data
  919. * @param mtd MTD device structure
  920. * @param addr address to recover
  921. * @param status return value from onenand_wait / onenand_bbt_wait
  922. *
  923. * MLC NAND Flash cell has paired pages - LSB page and MSB page. LSB page has
  924. * lower page address and MSB page has higher page address in paired pages.
  925. * If power off occurs during MSB page program, the paired LSB page data can
  926. * become corrupt. LSB page recovery read is a way to read LSB page though page
  927. * data are corrupted. When uncorrectable error occurs as a result of LSB page
  928. * read after power up, issue LSB page recovery read.
  929. */
  930. static int onenand_recover_lsb(struct mtd_info *mtd, loff_t addr, int status)
  931. {
  932. struct onenand_chip *this = mtd->priv;
  933. int i;
  934. /* Recovery is only for Flex-OneNAND */
  935. if (!FLEXONENAND(this))
  936. return status;
  937. /* check if we failed due to uncorrectable error */
  938. if (!mtd_is_eccerr(status) && status != ONENAND_BBT_READ_ECC_ERROR)
  939. return status;
  940. /* check if address lies in MLC region */
  941. i = flexonenand_region(mtd, addr);
  942. if (mtd->eraseregions[i].erasesize < (1 << this->erase_shift))
  943. return status;
  944. /* We are attempting to reread, so decrement stats.failed
  945. * which was incremented by onenand_wait due to read failure
  946. */
  947. printk(KERN_INFO "%s: Attempting to recover from uncorrectable read\n",
  948. __func__);
  949. mtd->ecc_stats.failed--;
  950. /* Issue the LSB page recovery command */
  951. this->command(mtd, FLEXONENAND_CMD_RECOVER_LSB, addr, this->writesize);
  952. return this->wait(mtd, FL_READING);
  953. }
  954. /**
  955. * onenand_mlc_read_ops_nolock - MLC OneNAND read main and/or out-of-band
  956. * @param mtd MTD device structure
  957. * @param from offset to read from
  958. * @param ops: oob operation description structure
  959. *
  960. * MLC OneNAND / Flex-OneNAND has 4KB page size and 4KB dataram.
  961. * So, read-while-load is not present.
  962. */
  963. static int onenand_mlc_read_ops_nolock(struct mtd_info *mtd, loff_t from,
  964. struct mtd_oob_ops *ops)
  965. {
  966. struct onenand_chip *this = mtd->priv;
  967. struct mtd_ecc_stats stats;
  968. size_t len = ops->len;
  969. size_t ooblen = ops->ooblen;
  970. u_char *buf = ops->datbuf;
  971. u_char *oobbuf = ops->oobbuf;
  972. int read = 0, column, thislen;
  973. int oobread = 0, oobcolumn, thisooblen, oobsize;
  974. int ret = 0;
  975. int writesize = this->writesize;
  976. pr_debug("%s: from = 0x%08x, len = %i\n", __func__, (unsigned int)from,
  977. (int)len);
  978. if (ops->mode == MTD_OPS_AUTO_OOB)
  979. oobsize = this->ecclayout->oobavail;
  980. else
  981. oobsize = mtd->oobsize;
  982. oobcolumn = from & (mtd->oobsize - 1);
  983. /* Do not allow reads past end of device */
  984. if (from + len > mtd->size) {
  985. printk(KERN_ERR "%s: Attempt read beyond end of device\n",
  986. __func__);
  987. ops->retlen = 0;
  988. ops->oobretlen = 0;
  989. return -EINVAL;
  990. }
  991. stats = mtd->ecc_stats;
  992. while (read < len) {
  993. cond_resched();
  994. thislen = min_t(int, writesize, len - read);
  995. column = from & (writesize - 1);
  996. if (column + thislen > writesize)
  997. thislen = writesize - column;
  998. if (!onenand_check_bufferram(mtd, from)) {
  999. this->command(mtd, ONENAND_CMD_READ, from, writesize);
  1000. ret = this->wait(mtd, FL_READING);
  1001. if (unlikely(ret))
  1002. ret = onenand_recover_lsb(mtd, from, ret);
  1003. onenand_update_bufferram(mtd, from, !ret);
  1004. if (mtd_is_eccerr(ret))
  1005. ret = 0;
  1006. if (ret)
  1007. break;
  1008. }
  1009. this->read_bufferram(mtd, ONENAND_DATARAM, buf, column, thislen);
  1010. if (oobbuf) {
  1011. thisooblen = oobsize - oobcolumn;
  1012. thisooblen = min_t(int, thisooblen, ooblen - oobread);
  1013. if (ops->mode == MTD_OPS_AUTO_OOB)
  1014. onenand_transfer_auto_oob(mtd, oobbuf, oobcolumn, thisooblen);
  1015. else
  1016. this->read_bufferram(mtd, ONENAND_SPARERAM, oobbuf, oobcolumn, thisooblen);
  1017. oobread += thisooblen;
  1018. oobbuf += thisooblen;
  1019. oobcolumn = 0;
  1020. }
  1021. read += thislen;
  1022. if (read == len)
  1023. break;
  1024. from += thislen;
  1025. buf += thislen;
  1026. }
  1027. /*
  1028. * Return success, if no ECC failures, else -EBADMSG
  1029. * fs driver will take care of that, because
  1030. * retlen == desired len and result == -EBADMSG
  1031. */
  1032. ops->retlen = read;
  1033. ops->oobretlen = oobread;
  1034. if (ret)
  1035. return ret;
  1036. if (mtd->ecc_stats.failed - stats.failed)
  1037. return -EBADMSG;
  1038. return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
  1039. }
  1040. /**
  1041. * onenand_read_ops_nolock - [OneNAND Interface] OneNAND read main and/or out-of-band
  1042. * @param mtd MTD device structure
  1043. * @param from offset to read from
  1044. * @param ops: oob operation description structure
  1045. *
  1046. * OneNAND read main and/or out-of-band data
  1047. */
  1048. static int onenand_read_ops_nolock(struct mtd_info *mtd, loff_t from,
  1049. struct mtd_oob_ops *ops)
  1050. {
  1051. struct onenand_chip *this = mtd->priv;
  1052. struct mtd_ecc_stats stats;
  1053. size_t len = ops->len;
  1054. size_t ooblen = ops->ooblen;
  1055. u_char *buf = ops->datbuf;
  1056. u_char *oobbuf = ops->oobbuf;
  1057. int read = 0, column, thislen;
  1058. int oobread = 0, oobcolumn, thisooblen, oobsize;
  1059. int ret = 0, boundary = 0;
  1060. int writesize = this->writesize;
  1061. pr_debug("%s: from = 0x%08x, len = %i\n", __func__, (unsigned int)from,
  1062. (int)len);
  1063. if (ops->mode == MTD_OPS_AUTO_OOB)
  1064. oobsize = this->ecclayout->oobavail;
  1065. else
  1066. oobsize = mtd->oobsize;
  1067. oobcolumn = from & (mtd->oobsize - 1);
  1068. /* Do not allow reads past end of device */
  1069. if ((from + len) > mtd->size) {
  1070. printk(KERN_ERR "%s: Attempt read beyond end of device\n",
  1071. __func__);
  1072. ops->retlen = 0;
  1073. ops->oobretlen = 0;
  1074. return -EINVAL;
  1075. }
  1076. stats = mtd->ecc_stats;
  1077. /* Read-while-load method */
  1078. /* Do first load to bufferRAM */
  1079. if (read < len) {
  1080. if (!onenand_check_bufferram(mtd, from)) {
  1081. this->command(mtd, ONENAND_CMD_READ, from, writesize);
  1082. ret = this->wait(mtd, FL_READING);
  1083. onenand_update_bufferram(mtd, from, !ret);
  1084. if (mtd_is_eccerr(ret))
  1085. ret = 0;
  1086. }
  1087. }
  1088. thislen = min_t(int, writesize, len - read);
  1089. column = from & (writesize - 1);
  1090. if (column + thislen > writesize)
  1091. thislen = writesize - column;
  1092. while (!ret) {
  1093. /* If there is more to load then start next load */
  1094. from += thislen;
  1095. if (read + thislen < len) {
  1096. this->command(mtd, ONENAND_CMD_READ, from, writesize);
  1097. /*
  1098. * Chip boundary handling in DDP
  1099. * Now we issued chip 1 read and pointed chip 1
  1100. * bufferram so we have to point chip 0 bufferram.
  1101. */
  1102. if (ONENAND_IS_DDP(this) &&
  1103. unlikely(from == (this->chipsize >> 1))) {
  1104. this->write_word(ONENAND_DDP_CHIP0, this->base + ONENAND_REG_START_ADDRESS2);
  1105. boundary = 1;
  1106. } else
  1107. boundary = 0;
  1108. ONENAND_SET_PREV_BUFFERRAM(this);
  1109. }
  1110. /* While load is going, read from last bufferRAM */
  1111. this->read_bufferram(mtd, ONENAND_DATARAM, buf, column, thislen);
  1112. /* Read oob area if needed */
  1113. if (oobbuf) {
  1114. thisooblen = oobsize - oobcolumn;
  1115. thisooblen = min_t(int, thisooblen, ooblen - oobread);
  1116. if (ops->mode == MTD_OPS_AUTO_OOB)
  1117. onenand_transfer_auto_oob(mtd, oobbuf, oobcolumn, thisooblen);
  1118. else
  1119. this->read_bufferram(mtd, ONENAND_SPARERAM, oobbuf, oobcolumn, thisooblen);
  1120. oobread += thisooblen;
  1121. oobbuf += thisooblen;
  1122. oobcolumn = 0;
  1123. }
  1124. /* See if we are done */
  1125. read += thislen;
  1126. if (read == len)
  1127. break;
  1128. /* Set up for next read from bufferRAM */
  1129. if (unlikely(boundary))
  1130. this->write_word(ONENAND_DDP_CHIP1, this->base + ONENAND_REG_START_ADDRESS2);
  1131. ONENAND_SET_NEXT_BUFFERRAM(this);
  1132. buf += thislen;
  1133. thislen = min_t(int, writesize, len - read);
  1134. column = 0;
  1135. cond_resched();
  1136. /* Now wait for load */
  1137. ret = this->wait(mtd, FL_READING);
  1138. onenand_update_bufferram(mtd, from, !ret);
  1139. if (mtd_is_eccerr(ret))
  1140. ret = 0;
  1141. }
  1142. /*
  1143. * Return success, if no ECC failures, else -EBADMSG
  1144. * fs driver will take care of that, because
  1145. * retlen == desired len and result == -EBADMSG
  1146. */
  1147. ops->retlen = read;
  1148. ops->oobretlen = oobread;
  1149. if (ret)
  1150. return ret;
  1151. if (mtd->ecc_stats.failed - stats.failed)
  1152. return -EBADMSG;
  1153. return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
  1154. }
  1155. /**
  1156. * onenand_read_oob_nolock - [MTD Interface] OneNAND read out-of-band
  1157. * @param mtd MTD device structure
  1158. * @param from offset to read from
  1159. * @param ops: oob operation description structure
  1160. *
  1161. * OneNAND read out-of-band data from the spare area
  1162. */
  1163. static int onenand_read_oob_nolock(struct mtd_info *mtd, loff_t from,
  1164. struct mtd_oob_ops *ops)
  1165. {
  1166. struct onenand_chip *this = mtd->priv;
  1167. struct mtd_ecc_stats stats;
  1168. int read = 0, thislen, column, oobsize;
  1169. size_t len = ops->ooblen;
  1170. unsigned int mode = ops->mode;
  1171. u_char *buf = ops->oobbuf;
  1172. int ret = 0, readcmd;
  1173. from += ops->ooboffs;
  1174. pr_debug("%s: from = 0x%08x, len = %i\n", __func__, (unsigned int)from,
  1175. (int)len);
  1176. /* Initialize return length value */
  1177. ops->oobretlen = 0;
  1178. if (mode == MTD_OPS_AUTO_OOB)
  1179. oobsize = this->ecclayout->oobavail;
  1180. else
  1181. oobsize = mtd->oobsize;
  1182. column = from & (mtd->oobsize - 1);
  1183. if (unlikely(column >= oobsize)) {
  1184. printk(KERN_ERR "%s: Attempted to start read outside oob\n",
  1185. __func__);
  1186. return -EINVAL;
  1187. }
  1188. /* Do not allow reads past end of device */
  1189. if (unlikely(from >= mtd->size ||
  1190. column + len > ((mtd->size >> this->page_shift) -
  1191. (from >> this->page_shift)) * oobsize)) {
  1192. printk(KERN_ERR "%s: Attempted to read beyond end of device\n",
  1193. __func__);
  1194. return -EINVAL;
  1195. }
  1196. stats = mtd->ecc_stats;
  1197. readcmd = ONENAND_IS_4KB_PAGE(this) ? ONENAND_CMD_READ : ONENAND_CMD_READOOB;
  1198. while (read < len) {
  1199. cond_resched();
  1200. thislen = oobsize - column;
  1201. thislen = min_t(int, thislen, len);
  1202. this->command(mtd, readcmd, from, mtd->oobsize);
  1203. onenand_update_bufferram(mtd, from, 0);
  1204. ret = this->wait(mtd, FL_READING);
  1205. if (unlikely(ret))
  1206. ret = onenand_recover_lsb(mtd, from, ret);
  1207. if (ret && !mtd_is_eccerr(ret)) {
  1208. printk(KERN_ERR "%s: read failed = 0x%x\n",
  1209. __func__, ret);
  1210. break;
  1211. }
  1212. if (mode == MTD_OPS_AUTO_OOB)
  1213. onenand_transfer_auto_oob(mtd, buf, column, thislen);
  1214. else
  1215. this->read_bufferram(mtd, ONENAND_SPARERAM, buf, column, thislen);
  1216. read += thislen;
  1217. if (read == len)
  1218. break;
  1219. buf += thislen;
  1220. /* Read more? */
  1221. if (read < len) {
  1222. /* Page size */
  1223. from += mtd->writesize;
  1224. column = 0;
  1225. }
  1226. }
  1227. ops->oobretlen = read;
  1228. if (ret)
  1229. return ret;
  1230. if (mtd->ecc_stats.failed - stats.failed)
  1231. return -EBADMSG;
  1232. return 0;
  1233. }
  1234. /**
  1235. * onenand_read - [MTD Interface] Read data from flash
  1236. * @param mtd MTD device structure
  1237. * @param from offset to read from
  1238. * @param len number of bytes to read
  1239. * @param retlen pointer to variable to store the number of read bytes
  1240. * @param buf the databuffer to put data
  1241. *
  1242. * Read with ecc
  1243. */
  1244. static int onenand_read(struct mtd_info *mtd, loff_t from, size_t len,
  1245. size_t *retlen, u_char *buf)
  1246. {
  1247. struct onenand_chip *this = mtd->priv;
  1248. struct mtd_oob_ops ops = {
  1249. .len = len,
  1250. .ooblen = 0,
  1251. .datbuf = buf,
  1252. .oobbuf = NULL,
  1253. };
  1254. int ret;
  1255. onenand_get_device(mtd, FL_READING);
  1256. ret = ONENAND_IS_4KB_PAGE(this) ?
  1257. onenand_mlc_read_ops_nolock(mtd, from, &ops) :
  1258. onenand_read_ops_nolock(mtd, from, &ops);
  1259. onenand_release_device(mtd);
  1260. *retlen = ops.retlen;
  1261. return ret;
  1262. }
  1263. /**
  1264. * onenand_read_oob - [MTD Interface] Read main and/or out-of-band
  1265. * @param mtd: MTD device structure
  1266. * @param from: offset to read from
  1267. * @param ops: oob operation description structure
  1268. * Read main and/or out-of-band
  1269. */
  1270. static int onenand_read_oob(struct mtd_info *mtd, loff_t from,
  1271. struct mtd_oob_ops *ops)
  1272. {
  1273. struct onenand_chip *this = mtd->priv;
  1274. int ret;
  1275. switch (ops->mode) {
  1276. case MTD_OPS_PLACE_OOB:
  1277. case MTD_OPS_AUTO_OOB:
  1278. break;
  1279. case MTD_OPS_RAW:
  1280. /* Not implemented yet */
  1281. default:
  1282. return -EINVAL;
  1283. }
  1284. onenand_get_device(mtd, FL_READING);
  1285. if (ops->datbuf)
  1286. ret = ONENAND_IS_4KB_PAGE(this) ?
  1287. onenand_mlc_read_ops_nolock(mtd, from, ops) :
  1288. onenand_read_ops_nolock(mtd, from, ops);
  1289. else
  1290. ret = onenand_read_oob_nolock(mtd, from, ops);
  1291. onenand_release_device(mtd);
  1292. return ret;
  1293. }
  1294. /**
  1295. * onenand_bbt_wait - [DEFAULT] wait until the command is done
  1296. * @param mtd MTD device structure
  1297. * @param state state to select the max. timeout value
  1298. *
  1299. * Wait for command done.
  1300. */
  1301. static int onenand_bbt_wait(struct mtd_info *mtd, int state)
  1302. {
  1303. struct onenand_chip *this = mtd->priv;
  1304. unsigned long timeout;
  1305. unsigned int interrupt, ctrl, ecc, addr1, addr8;
  1306. /* The 20 msec is enough */
  1307. timeout = jiffies + msecs_to_jiffies(20);
  1308. while (time_before(jiffies, timeout)) {
  1309. interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
  1310. if (interrupt & ONENAND_INT_MASTER)
  1311. break;
  1312. }
  1313. /* To get correct interrupt status in timeout case */
  1314. interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
  1315. ctrl = this->read_word(this->base + ONENAND_REG_CTRL_STATUS);
  1316. addr1 = this->read_word(this->base + ONENAND_REG_START_ADDRESS1);
  1317. addr8 = this->read_word(this->base + ONENAND_REG_START_ADDRESS8);
  1318. if (interrupt & ONENAND_INT_READ) {
  1319. ecc = onenand_read_ecc(this);
  1320. if (ecc & ONENAND_ECC_2BIT_ALL) {
  1321. printk(KERN_DEBUG "%s: ecc 0x%04x ctrl 0x%04x "
  1322. "intr 0x%04x addr1 %#x addr8 %#x\n",
  1323. __func__, ecc, ctrl, interrupt, addr1, addr8);
  1324. return ONENAND_BBT_READ_ECC_ERROR;
  1325. }
  1326. } else {
  1327. printk(KERN_ERR "%s: read timeout! ctrl 0x%04x "
  1328. "intr 0x%04x addr1 %#x addr8 %#x\n",
  1329. __func__, ctrl, interrupt, addr1, addr8);
  1330. return ONENAND_BBT_READ_FATAL_ERROR;
  1331. }
  1332. /* Initial bad block case: 0x2400 or 0x0400 */
  1333. if (ctrl & ONENAND_CTRL_ERROR) {
  1334. printk(KERN_DEBUG "%s: ctrl 0x%04x intr 0x%04x addr1 %#x "
  1335. "addr8 %#x\n", __func__, ctrl, interrupt, addr1, addr8);
  1336. return ONENAND_BBT_READ_ERROR;
  1337. }
  1338. return 0;
  1339. }
  1340. /**
  1341. * onenand_bbt_read_oob - [MTD Interface] OneNAND read out-of-band for bbt scan
  1342. * @param mtd MTD device structure
  1343. * @param from offset to read from
  1344. * @param ops oob operation description structure
  1345. *
  1346. * OneNAND read out-of-band data from the spare area for bbt scan
  1347. */
  1348. int onenand_bbt_read_oob(struct mtd_info *mtd, loff_t from,
  1349. struct mtd_oob_ops *ops)
  1350. {
  1351. struct onenand_chip *this = mtd->priv;
  1352. int read = 0, thislen, column;
  1353. int ret = 0, readcmd;
  1354. size_t len = ops->ooblen;
  1355. u_char *buf = ops->oobbuf;
  1356. pr_debug("%s: from = 0x%08x, len = %zi\n", __func__, (unsigned int)from,
  1357. len);
  1358. /* Initialize return value */
  1359. ops->oobretlen = 0;
  1360. /* Do not allow reads past end of device */
  1361. if (unlikely((from + len) > mtd->size)) {
  1362. printk(KERN_ERR "%s: Attempt read beyond end of device\n",
  1363. __func__);
  1364. return ONENAND_BBT_READ_FATAL_ERROR;
  1365. }
  1366. /* Grab the lock and see if the device is available */
  1367. onenand_get_device(mtd, FL_READING);
  1368. column = from & (mtd->oobsize - 1);
  1369. readcmd = ONENAND_IS_4KB_PAGE(this) ? ONENAND_CMD_READ : ONENAND_CMD_READOOB;
  1370. while (read < len) {
  1371. cond_resched();
  1372. thislen = mtd->oobsize - column;
  1373. thislen = min_t(int, thislen, len);
  1374. this->command(mtd, readcmd, from, mtd->oobsize);
  1375. onenand_update_bufferram(mtd, from, 0);
  1376. ret = this->bbt_wait(mtd, FL_READING);
  1377. if (unlikely(ret))
  1378. ret = onenand_recover_lsb(mtd, from, ret);
  1379. if (ret)
  1380. break;
  1381. this->read_bufferram(mtd, ONENAND_SPARERAM, buf, column, thislen);
  1382. read += thislen;
  1383. if (read == len)
  1384. break;
  1385. buf += thislen;
  1386. /* Read more? */
  1387. if (read < len) {
  1388. /* Update Page size */
  1389. from += this->writesize;
  1390. column = 0;
  1391. }
  1392. }
  1393. /* Deselect and wake up anyone waiting on the device */
  1394. onenand_release_device(mtd);
  1395. ops->oobretlen = read;
  1396. return ret;
  1397. }
  1398. #ifdef CONFIG_MTD_ONENAND_VERIFY_WRITE
  1399. /**
  1400. * onenand_verify_oob - [GENERIC] verify the oob contents after a write
  1401. * @param mtd MTD device structure
  1402. * @param buf the databuffer to verify
  1403. * @param to offset to read from
  1404. */
  1405. static int onenand_verify_oob(struct mtd_info *mtd, const u_char *buf, loff_t to)
  1406. {
  1407. struct onenand_chip *this = mtd->priv;
  1408. u_char *oob_buf = this->oob_buf;
  1409. int status, i, readcmd;
  1410. readcmd = ONENAND_IS_4KB_PAGE(this) ? ONENAND_CMD_READ : ONENAND_CMD_READOOB;
  1411. this->command(mtd, readcmd, to, mtd->oobsize);
  1412. onenand_update_bufferram(mtd, to, 0);
  1413. status = this->wait(mtd, FL_READING);
  1414. if (status)
  1415. return status;
  1416. this->read_bufferram(mtd, ONENAND_SPARERAM, oob_buf, 0, mtd->oobsize);
  1417. for (i = 0; i < mtd->oobsize; i++)
  1418. if (buf[i] != 0xFF && buf[i] != oob_buf[i])
  1419. return -EBADMSG;
  1420. return 0;
  1421. }
  1422. /**
  1423. * onenand_verify - [GENERIC] verify the chip contents after a write
  1424. * @param mtd MTD device structure
  1425. * @param buf the databuffer to verify
  1426. * @param addr offset to read from
  1427. * @param len number of bytes to read and compare
  1428. */
  1429. static int onenand_verify(struct mtd_info *mtd, const u_char *buf, loff_t addr, size_t len)
  1430. {
  1431. struct onenand_chip *this = mtd->priv;
  1432. int ret = 0;
  1433. int thislen, column;
  1434. column = addr & (this->writesize - 1);
  1435. while (len != 0) {
  1436. thislen = min_t(int, this->writesize - column, len);
  1437. this->command(mtd, ONENAND_CMD_READ, addr, this->writesize);
  1438. onenand_update_bufferram(mtd, addr, 0);
  1439. ret = this->wait(mtd, FL_READING);
  1440. if (ret)
  1441. return ret;
  1442. onenand_update_bufferram(mtd, addr, 1);
  1443. this->read_bufferram(mtd, ONENAND_DATARAM, this->verify_buf, 0, mtd->writesize);
  1444. if (memcmp(buf, this->verify_buf + column, thislen))
  1445. return -EBADMSG;
  1446. len -= thislen;
  1447. buf += thislen;
  1448. addr += thislen;
  1449. column = 0;
  1450. }
  1451. return 0;
  1452. }
  1453. #else
  1454. #define onenand_verify(...) (0)
  1455. #define onenand_verify_oob(...) (0)
  1456. #endif
  1457. #define NOTALIGNED(x) ((x & (this->subpagesize - 1)) != 0)
  1458. static void onenand_panic_wait(struct mtd_info *mtd)
  1459. {
  1460. struct onenand_chip *this = mtd->priv;
  1461. unsigned int interrupt;
  1462. int i;
  1463. for (i = 0; i < 2000; i++) {
  1464. interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
  1465. if (interrupt & ONENAND_INT_MASTER)
  1466. break;
  1467. udelay(10);
  1468. }
  1469. }
  1470. /**
  1471. * onenand_panic_write - [MTD Interface] write buffer to FLASH in a panic context
  1472. * @param mtd MTD device structure
  1473. * @param to offset to write to
  1474. * @param len number of bytes to write
  1475. * @param retlen pointer to variable to store the number of written bytes
  1476. * @param buf the data to write
  1477. *
  1478. * Write with ECC
  1479. */
  1480. static int onenand_panic_write(struct mtd_info *mtd, loff_t to, size_t len,
  1481. size_t *retlen, const u_char *buf)
  1482. {
  1483. struct onenand_chip *this = mtd->priv;
  1484. int column, subpage;
  1485. int written = 0;
  1486. int ret = 0;
  1487. if (this->state == FL_PM_SUSPENDED)
  1488. return -EBUSY;
  1489. /* Wait for any existing operation to clear */
  1490. onenand_panic_wait(mtd);
  1491. pr_debug("%s: to = 0x%08x, len = %i\n", __func__, (unsigned int)to,
  1492. (int)len);
  1493. /* Reject writes, which are not page aligned */
  1494. if (unlikely(NOTALIGNED(to) || NOTALIGNED(len))) {
  1495. printk(KERN_ERR "%s: Attempt to write not page aligned data\n",
  1496. __func__);
  1497. return -EINVAL;
  1498. }
  1499. column = to & (mtd->writesize - 1);
  1500. /* Loop until all data write */
  1501. while (written < len) {
  1502. int thislen = min_t(int, mtd->writesize - column, len - written);
  1503. u_char *wbuf = (u_char *) buf;
  1504. this->command(mtd, ONENAND_CMD_BUFFERRAM, to, thislen);
  1505. /* Partial page write */
  1506. subpage = thislen < mtd->writesize;
  1507. if (subpage) {
  1508. memset(this->page_buf, 0xff, mtd->writesize);
  1509. memcpy(this->page_buf + column, buf, thislen);
  1510. wbuf = this->page_buf;
  1511. }
  1512. this->write_bufferram(mtd, ONENAND_DATARAM, wbuf, 0, mtd->writesize);
  1513. this->write_bufferram(mtd, ONENAND_SPARERAM, ffchars, 0, mtd->oobsize);
  1514. this->command(mtd, ONENAND_CMD_PROG, to, mtd->writesize);
  1515. onenand_panic_wait(mtd);
  1516. /* In partial page write we don't update bufferram */
  1517. onenand_update_bufferram(mtd, to, !ret && !subpage);
  1518. if (ONENAND_IS_2PLANE(this)) {
  1519. ONENAND_SET_BUFFERRAM1(this);
  1520. onenand_update_bufferram(mtd, to + this->writesize, !ret && !subpage);
  1521. }
  1522. if (ret) {
  1523. printk(KERN_ERR "%s: write failed %d\n", __func__, ret);
  1524. break;
  1525. }
  1526. written += thislen;
  1527. if (written == len)
  1528. break;
  1529. column = 0;
  1530. to += thislen;
  1531. buf += thislen;
  1532. }
  1533. *retlen = written;
  1534. return ret;
  1535. }
  1536. /**
  1537. * onenand_fill_auto_oob - [INTERN] oob auto-placement transfer
  1538. * @param mtd MTD device structure
  1539. * @param oob_buf oob buffer
  1540. * @param buf source address
  1541. * @param column oob offset to write to
  1542. * @param thislen oob length to write
  1543. */
  1544. static int onenand_fill_auto_oob(struct mtd_info *mtd, u_char *oob_buf,
  1545. const u_char *buf, int column, int thislen)
  1546. {
  1547. struct onenand_chip *this = mtd->priv;
  1548. struct nand_oobfree *free;
  1549. int writecol = column;
  1550. int writeend = column + thislen;
  1551. int lastgap = 0;
  1552. unsigned int i;
  1553. free = this->ecclayout->oobfree;
  1554. for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES && free->length; i++, free++) {
  1555. if (writecol >= lastgap)
  1556. writecol += free->offset - lastgap;
  1557. if (writeend >= lastgap)
  1558. writeend += free->offset - lastgap;
  1559. lastgap = free->offset + free->length;
  1560. }
  1561. free = this->ecclayout->oobfree;
  1562. for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES && free->length; i++, free++) {
  1563. int free_end = free->offset + free->length;
  1564. if (free->offset < writeend && free_end > writecol) {
  1565. int st = max_t(int,free->offset,writecol);
  1566. int ed = min_t(int,free_end,writeend);
  1567. int n = ed - st;
  1568. memcpy(oob_buf + st, buf, n);
  1569. buf += n;
  1570. } else if (column == 0)
  1571. break;
  1572. }
  1573. return 0;
  1574. }
  1575. /**
  1576. * onenand_write_ops_nolock - [OneNAND Interface] write main and/or out-of-band
  1577. * @param mtd MTD device structure
  1578. * @param to offset to write to
  1579. * @param ops oob operation description structure
  1580. *
  1581. * Write main and/or oob with ECC
  1582. */
  1583. static int onenand_write_ops_nolock(struct mtd_info *mtd, loff_t to,
  1584. struct mtd_oob_ops *ops)
  1585. {
  1586. struct onenand_chip *this = mtd->priv;
  1587. int written = 0, column, thislen = 0, subpage = 0;
  1588. int prev = 0, prevlen = 0, prev_subpage = 0, first = 1;
  1589. int oobwritten = 0, oobcolumn, thisooblen, oobsize;
  1590. size_t len = ops->len;
  1591. size_t ooblen = ops->ooblen;
  1592. const u_char *buf = ops->datbuf;
  1593. const u_char *oob = ops->oobbuf;
  1594. u_char *oobbuf;
  1595. int ret = 0, cmd;
  1596. pr_debug("%s: to = 0x%08x, len = %i\n", __func__, (unsigned int)to,
  1597. (int)len);
  1598. /* Initialize retlen, in case of early exit */
  1599. ops->retlen = 0;
  1600. ops->oobretlen = 0;
  1601. /* Reject writes, which are not page aligned */
  1602. if (unlikely(NOTALIGNED(to) || NOTALIGNED(len))) {
  1603. printk(KERN_ERR "%s: Attempt to write not page aligned data\n",
  1604. __func__);
  1605. return -EINVAL;
  1606. }
  1607. /* Check zero length */
  1608. if (!len)
  1609. return 0;
  1610. if (ops->mode == MTD_OPS_AUTO_OOB)
  1611. oobsize = this->ecclayout->oobavail;
  1612. else
  1613. oobsize = mtd->oobsize;
  1614. oobcolumn = to & (mtd->oobsize - 1);
  1615. column = to & (mtd->writesize - 1);
  1616. /* Loop until all data write */
  1617. while (1) {
  1618. if (written < len) {
  1619. u_char *wbuf = (u_char *) buf;
  1620. thislen = min_t(int, mtd->writesize - column, len - written);
  1621. thisooblen = min_t(int, oobsize - oobcolumn, ooblen - oobwritten);
  1622. cond_resched();
  1623. this->command(mtd, ONENAND_CMD_BUFFERRAM, to, thislen);
  1624. /* Partial page write */
  1625. subpage = thislen < mtd->writesize;
  1626. if (subpage) {
  1627. memset(this->page_buf, 0xff, mtd->writesize);
  1628. memcpy(this->page_buf + column, buf, thislen);
  1629. wbuf = this->page_buf;
  1630. }
  1631. this->write_bufferram(mtd, ONENAND_DATARAM, wbuf, 0, mtd->writesize);
  1632. if (oob) {
  1633. oobbuf = this->oob_buf;
  1634. /* We send data to spare ram with oobsize
  1635. * to prevent byte access */
  1636. memset(oobbuf, 0xff, mtd->oobsize);
  1637. if (ops->mode == MTD_OPS_AUTO_OOB)
  1638. onenand_fill_auto_oob(mtd, oobbuf, oob, oobcolumn, thisooblen);
  1639. else
  1640. memcpy(oobbuf + oobcolumn, oob, thisooblen);
  1641. oobwritten += thisooblen;
  1642. oob += thisooblen;
  1643. oobcolumn = 0;
  1644. } else
  1645. oobbuf = (u_char *) ffchars;
  1646. this->write_bufferram(mtd, ONENAND_SPARERAM, oobbuf, 0, mtd->oobsize);
  1647. } else
  1648. ONENAND_SET_NEXT_BUFFERRAM(this);
  1649. /*
  1650. * 2 PLANE, MLC, and Flex-OneNAND do not support
  1651. * write-while-program feature.
  1652. */
  1653. if (!ONENAND_IS_2PLANE(this) && !ONENAND_IS_4KB_PAGE(this) && !first) {
  1654. ONENAND_SET_PREV_BUFFERRAM(this);
  1655. ret = this->wait(mtd, FL_WRITING);
  1656. /* In partial page write we don't update bufferram */
  1657. onenand_update_bufferram(mtd, prev, !ret && !prev_subpage);
  1658. if (ret) {
  1659. written -= prevlen;
  1660. printk(KERN_ERR "%s: write failed %d\n",
  1661. __func__, ret);
  1662. break;
  1663. }
  1664. if (written == len) {
  1665. /* Only check verify write turn on */
  1666. ret = onenand_verify(mtd, buf - len, to - len, len);
  1667. if (ret)
  1668. printk(KERN_ERR "%s: verify failed %d\n",
  1669. __func__, ret);
  1670. break;
  1671. }
  1672. ONENAND_SET_NEXT_BUFFERRAM(this);
  1673. }
  1674. this->ongoing = 0;
  1675. cmd = ONENAND_CMD_PROG;
  1676. /* Exclude 1st OTP and OTP blocks for cache program feature */
  1677. if (ONENAND_IS_CACHE_PROGRAM(this) &&
  1678. likely(onenand_block(this, to) != 0) &&
  1679. ONENAND_IS_4KB_PAGE(this) &&
  1680. ((written + thislen) < len)) {
  1681. cmd = ONENAND_CMD_2X_CACHE_PROG;
  1682. this->ongoing = 1;
  1683. }
  1684. this->command(mtd, cmd, to, mtd->writesize);
  1685. /*
  1686. * 2 PLANE, MLC, and Flex-OneNAND wait here
  1687. */
  1688. if (ONENAND_IS_2PLANE(this) || ONENAND_IS_4KB_PAGE(this)) {
  1689. ret = this->wait(mtd, FL_WRITING);
  1690. /* In partial page write we don't update bufferram */
  1691. onenand_update_bufferram(mtd, to, !ret && !subpage);
  1692. if (ret) {
  1693. printk(KERN_ERR "%s: write failed %d\n",
  1694. __func__, ret);
  1695. break;
  1696. }
  1697. /* Only check verify write turn on */
  1698. ret = onenand_verify(mtd, buf, to, thislen);
  1699. if (ret) {
  1700. printk(KERN_ERR "%s: verify failed %d\n",
  1701. __func__, ret);
  1702. break;
  1703. }
  1704. written += thislen;
  1705. if (written == len)
  1706. break;
  1707. } else
  1708. written += thislen;
  1709. column = 0;
  1710. prev_subpage = subpage;
  1711. prev = to;
  1712. prevlen = thislen;
  1713. to += thislen;
  1714. buf += thislen;
  1715. first = 0;
  1716. }
  1717. /* In error case, clear all bufferrams */
  1718. if (written != len)
  1719. onenand_invalidate_bufferram(mtd, 0, -1);
  1720. ops->retlen = written;
  1721. ops->oobretlen = oobwritten;
  1722. return ret;
  1723. }
  1724. /**
  1725. * onenand_write_oob_nolock - [INTERN] OneNAND write out-of-band
  1726. * @param mtd MTD device structure
  1727. * @param to offset to write to
  1728. * @param len number of bytes to write
  1729. * @param retlen pointer to variable to store the number of written bytes
  1730. * @param buf the data to write
  1731. * @param mode operation mode
  1732. *
  1733. * OneNAND write out-of-band
  1734. */
  1735. static int onenand_write_oob_nolock(struct mtd_info *mtd, loff_t to,
  1736. struct mtd_oob_ops *ops)
  1737. {
  1738. struct onenand_chip *this = mtd->priv;
  1739. int column, ret = 0, oobsize;
  1740. int written = 0, oobcmd;
  1741. u_char *oobbuf;
  1742. size_t len = ops->ooblen;
  1743. const u_char *buf = ops->oobbuf;
  1744. unsigned int mode = ops->mode;
  1745. to += ops->ooboffs;
  1746. pr_debug("%s: to = 0x%08x, len = %i\n", __func__, (unsigned int)to,
  1747. (int)len);
  1748. /* Initialize retlen, in case of early exit */
  1749. ops->oobretlen = 0;
  1750. if (mode == MTD_OPS_AUTO_OOB)
  1751. oobsize = this->ecclayout->oobavail;
  1752. else
  1753. oobsize = mtd->oobsize;
  1754. column = to & (mtd->oobsize - 1);
  1755. if (unlikely(column >= oobsize)) {
  1756. printk(KERN_ERR "%s: Attempted to start write outside oob\n",
  1757. __func__);
  1758. return -EINVAL;
  1759. }
  1760. /* For compatibility with NAND: Do not allow write past end of page */
  1761. if (unlikely(column + len > oobsize)) {
  1762. printk(KERN_ERR "%s: Attempt to write past end of page\n",
  1763. __func__);
  1764. return -EINVAL;
  1765. }
  1766. /* Do not allow reads past end of device */
  1767. if (unlikely(to >= mtd->size ||
  1768. column + len > ((mtd->size >> this->page_shift) -
  1769. (to >> this->page_shift)) * oobsize)) {
  1770. printk(KERN_ERR "%s: Attempted to write past end of device\n",
  1771. __func__);
  1772. return -EINVAL;
  1773. }
  1774. oobbuf = this->oob_buf;
  1775. oobcmd = ONENAND_IS_4KB_PAGE(this) ? ONENAND_CMD_PROG : ONENAND_CMD_PROGOOB;
  1776. /* Loop until all data write */
  1777. while (written < len) {
  1778. int thislen = min_t(int, oobsize, len - written);
  1779. cond_resched();
  1780. this->command(mtd, ONENAND_CMD_BUFFERRAM, to, mtd->oobsize);
  1781. /* We send data to spare ram with oobsize
  1782. * to prevent byte access */
  1783. memset(oobbuf, 0xff, mtd->oobsize);
  1784. if (mode == MTD_OPS_AUTO_OOB)
  1785. onenand_fill_auto_oob(mtd, oobbuf, buf, column, thislen);
  1786. else
  1787. memcpy(oobbuf + column, buf, thislen);
  1788. this->write_bufferram(mtd, ONENAND_SPARERAM, oobbuf, 0, mtd->oobsize);
  1789. if (ONENAND_IS_4KB_PAGE(this)) {
  1790. /* Set main area of DataRAM to 0xff*/
  1791. memset(this->page_buf, 0xff, mtd->writesize);
  1792. this->write_bufferram(mtd, ONENAND_DATARAM,
  1793. this->page_buf, 0, mtd->writesize);
  1794. }
  1795. this->command(mtd, oobcmd, to, mtd->oobsize);
  1796. onenand_update_bufferram(mtd, to, 0);
  1797. if (ONENAND_IS_2PLANE(this)) {
  1798. ONENAND_SET_BUFFERRAM1(this);
  1799. onenand_update_bufferram(mtd, to + this->writesize, 0);
  1800. }
  1801. ret = this->wait(mtd, FL_WRITING);
  1802. if (ret) {
  1803. printk(KERN_ERR "%s: write failed %d\n", __func__, ret);
  1804. break;
  1805. }
  1806. ret = onenand_verify_oob(mtd, oobbuf, to);
  1807. if (ret) {
  1808. printk(KERN_ERR "%s: verify failed %d\n",
  1809. __func__, ret);
  1810. break;
  1811. }
  1812. written += thislen;
  1813. if (written == len)
  1814. break;
  1815. to += mtd->writesize;
  1816. buf += thislen;
  1817. column = 0;
  1818. }
  1819. ops->oobretlen = written;
  1820. return ret;
  1821. }
  1822. /**
  1823. * onenand_write - [MTD Interface] write buffer to FLASH
  1824. * @param mtd MTD device structure
  1825. * @param to offset to write to
  1826. * @param len number of bytes to write
  1827. * @param retlen pointer to variable to store the number of written bytes
  1828. * @param buf the data to write
  1829. *
  1830. * Write with ECC
  1831. */
  1832. static int onenand_write(struct mtd_info *mtd, loff_t to, size_t len,
  1833. size_t *retlen, const u_char *buf)
  1834. {
  1835. struct mtd_oob_ops ops = {
  1836. .len = len,
  1837. .ooblen = 0,
  1838. .datbuf = (u_char *) buf,
  1839. .oobbuf = NULL,
  1840. };
  1841. int ret;
  1842. onenand_get_device(mtd, FL_WRITING);
  1843. ret = onenand_write_ops_nolock(mtd, to, &ops);
  1844. onenand_release_device(mtd);
  1845. *retlen = ops.retlen;
  1846. return ret;
  1847. }
  1848. /**
  1849. * onenand_write_oob - [MTD Interface] NAND write data and/or out-of-band
  1850. * @param mtd: MTD device structure
  1851. * @param to: offset to write
  1852. * @param ops: oob operation description structure
  1853. */
  1854. static int onenand_write_oob(struct mtd_info *mtd, loff_t to,
  1855. struct mtd_oob_ops *ops)
  1856. {
  1857. int ret;
  1858. switch (ops->mode) {
  1859. case MTD_OPS_PLACE_OOB:
  1860. case MTD_OPS_AUTO_OOB:
  1861. break;
  1862. case MTD_OPS_RAW:
  1863. /* Not implemented yet */
  1864. default:
  1865. return -EINVAL;
  1866. }
  1867. onenand_get_device(mtd, FL_WRITING);
  1868. if (ops->datbuf)
  1869. ret = onenand_write_ops_nolock(mtd, to, ops);
  1870. else
  1871. ret = onenand_write_oob_nolock(mtd, to, ops);
  1872. onenand_release_device(mtd);
  1873. return ret;
  1874. }
  1875. /**
  1876. * onenand_block_isbad_nolock - [GENERIC] Check if a block is marked bad
  1877. * @param mtd MTD device structure
  1878. * @param ofs offset from device start
  1879. * @param allowbbt 1, if its allowed to access the bbt area
  1880. *
  1881. * Check, if the block is bad. Either by reading the bad block table or
  1882. * calling of the scan function.
  1883. */
  1884. static int onenand_block_isbad_nolock(struct mtd_info *mtd, loff_t ofs, int allowbbt)
  1885. {
  1886. struct onenand_chip *this = mtd->priv;
  1887. struct bbm_info *bbm = this->bbm;
  1888. /* Return info from the table */
  1889. return bbm->isbad_bbt(mtd, ofs, allowbbt);
  1890. }
  1891. static int onenand_multiblock_erase_verify(struct mtd_info *mtd,
  1892. struct erase_info *instr)
  1893. {
  1894. struct onenand_chip *this = mtd->priv;
  1895. loff_t addr = instr->addr;
  1896. int len = instr->len;
  1897. unsigned int block_size = (1 << this->erase_shift);
  1898. int ret = 0;
  1899. while (len) {
  1900. this->command(mtd, ONENAND_CMD_ERASE_VERIFY, addr, block_size);
  1901. ret = this->wait(mtd, FL_VERIFYING_ERASE);
  1902. if (ret) {
  1903. printk(KERN_ERR "%s: Failed verify, block %d\n",
  1904. __func__, onenand_block(this, addr));
  1905. instr->state = MTD_ERASE_FAILED;
  1906. instr->fail_addr = addr;
  1907. return -1;
  1908. }
  1909. len -= block_size;
  1910. addr += block_size;
  1911. }
  1912. return 0;
  1913. }
  1914. /**
  1915. * onenand_multiblock_erase - [INTERN] erase block(s) using multiblock erase
  1916. * @param mtd MTD device structure
  1917. * @param instr erase instruction
  1918. * @param region erase region
  1919. *
  1920. * Erase one or more blocks up to 64 block at a time
  1921. */
  1922. static int onenand_multiblock_erase(struct mtd_info *mtd,
  1923. struct erase_info *instr,
  1924. unsigned int block_size)
  1925. {
  1926. struct onenand_chip *this = mtd->priv;
  1927. loff_t addr = instr->addr;
  1928. int len = instr->len;
  1929. int eb_count = 0;
  1930. int ret = 0;
  1931. int bdry_block = 0;
  1932. instr->state = MTD_ERASING;
  1933. if (ONENAND_IS_DDP(this)) {
  1934. loff_t bdry_addr = this->chipsize >> 1;
  1935. if (addr < bdry_addr && (addr + len) > bdry_addr)
  1936. bdry_block = bdry_addr >> this->erase_shift;
  1937. }
  1938. /* Pre-check bbs */
  1939. while (len) {
  1940. /* Check if we have a bad block, we do not erase bad blocks */
  1941. if (onenand_block_isbad_nolock(mtd, addr, 0)) {
  1942. printk(KERN_WARNING "%s: attempt to erase a bad block "
  1943. "at addr 0x%012llx\n",
  1944. __func__, (unsigned long long) addr);
  1945. instr->state = MTD_ERASE_FAILED;
  1946. return -EIO;
  1947. }
  1948. len -= block_size;
  1949. addr += block_size;
  1950. }
  1951. len = instr->len;
  1952. addr = instr->addr;
  1953. /* loop over 64 eb batches */
  1954. while (len) {
  1955. struct erase_info verify_instr = *instr;
  1956. int max_eb_count = MB_ERASE_MAX_BLK_COUNT;
  1957. verify_instr.addr = addr;
  1958. verify_instr.len = 0;
  1959. /* do not cross chip boundary */
  1960. if (bdry_block) {
  1961. int this_block = (addr >> this->erase_shift);
  1962. if (this_block < bdry_block) {
  1963. max_eb_count = min(max_eb_count,
  1964. (bdry_block - this_block));
  1965. }
  1966. }
  1967. eb_count = 0;
  1968. while (len > block_size && eb_count < (max_eb_count - 1)) {
  1969. this->command(mtd, ONENAND_CMD_MULTIBLOCK_ERASE,
  1970. addr, block_size);
  1971. onenand_invalidate_bufferram(mtd, addr, block_size);
  1972. ret = this->wait(mtd, FL_PREPARING_ERASE);
  1973. if (ret) {
  1974. printk(KERN_ERR "%s: Failed multiblock erase, "
  1975. "block %d\n", __func__,
  1976. onenand_block(this, addr));
  1977. instr->state = MTD_ERASE_FAILED;
  1978. instr->fail_addr = MTD_FAIL_ADDR_UNKNOWN;
  1979. return -EIO;
  1980. }
  1981. len -= block_size;
  1982. addr += block_size;
  1983. eb_count++;
  1984. }
  1985. /* last block of 64-eb series */
  1986. cond_resched();
  1987. this->command(mtd, ONENAND_CMD_ERASE, addr, block_size);
  1988. onenand_invalidate_bufferram(mtd, addr, block_size);
  1989. ret = this->wait(mtd, FL_ERASING);
  1990. /* Check if it is write protected */
  1991. if (ret) {
  1992. printk(KERN_ERR "%s: Failed erase, block %d\n",
  1993. __func__, onenand_block(this, addr));
  1994. instr->state = MTD_ERASE_FAILED;
  1995. instr->fail_addr = MTD_FAIL_ADDR_UNKNOWN;
  1996. return -EIO;
  1997. }
  1998. len -= block_size;
  1999. addr += block_size;
  2000. eb_count++;
  2001. /* verify */
  2002. verify_instr.len = eb_count * block_size;
  2003. if (onenand_multiblock_erase_verify(mtd, &verify_instr)) {
  2004. instr->state = verify_instr.state;
  2005. instr->fail_addr = verify_instr.fail_addr;
  2006. return -EIO;
  2007. }
  2008. }
  2009. return 0;
  2010. }
  2011. /**
  2012. * onenand_block_by_block_erase - [INTERN] erase block(s) using regular erase
  2013. * @param mtd MTD device structure
  2014. * @param instr erase instruction
  2015. * @param region erase region
  2016. * @param block_size erase block size
  2017. *
  2018. * Erase one or more blocks one block at a time
  2019. */
  2020. static int onenand_block_by_block_erase(struct mtd_info *mtd,
  2021. struct erase_info *instr,
  2022. struct mtd_erase_region_info *region,
  2023. unsigned int block_size)
  2024. {
  2025. struct onenand_chip *this = mtd->priv;
  2026. loff_t addr = instr->addr;
  2027. int len = instr->len;
  2028. loff_t region_end = 0;
  2029. int ret = 0;
  2030. if (region) {
  2031. /* region is set for Flex-OneNAND */
  2032. region_end = region->offset + region->erasesize * region->numblocks;
  2033. }
  2034. instr->state = MTD_ERASING;
  2035. /* Loop through the blocks */
  2036. while (len) {
  2037. cond_resched();
  2038. /* Check if we have a bad block, we do not erase bad blocks */
  2039. if (onenand_block_isbad_nolock(mtd, addr, 0)) {
  2040. printk(KERN_WARNING "%s: attempt to erase a bad block "
  2041. "at addr 0x%012llx\n",
  2042. __func__, (unsigned long long) addr);
  2043. instr->state = MTD_ERASE_FAILED;
  2044. return -EIO;
  2045. }
  2046. this->command(mtd, ONENAND_CMD_ERASE, addr, block_size);
  2047. onenand_invalidate_bufferram(mtd, addr, block_size);
  2048. ret = this->wait(mtd, FL_ERASING);
  2049. /* Check, if it is write protected */
  2050. if (ret) {
  2051. printk(KERN_ERR "%s: Failed erase, block %d\n",
  2052. __func__, onenand_block(this, addr));
  2053. instr->state = MTD_ERASE_FAILED;
  2054. instr->fail_addr = addr;
  2055. return -EIO;
  2056. }
  2057. len -= block_size;
  2058. addr += block_size;
  2059. if (region && addr == region_end) {
  2060. if (!len)
  2061. break;
  2062. region++;
  2063. block_size = region->erasesize;
  2064. region_end = region->offset + region->erasesize * region->numblocks;
  2065. if (len & (block_size - 1)) {
  2066. /* FIXME: This should be handled at MTD partitioning level. */
  2067. printk(KERN_ERR "%s: Unaligned address\n",
  2068. __func__);
  2069. return -EIO;
  2070. }
  2071. }
  2072. }
  2073. return 0;
  2074. }
  2075. /**
  2076. * onenand_erase - [MTD Interface] erase block(s)
  2077. * @param mtd MTD device structure
  2078. * @param instr erase instruction
  2079. *
  2080. * Erase one or more blocks
  2081. */
  2082. static int onenand_erase(struct mtd_info *mtd, struct erase_info *instr)
  2083. {
  2084. struct onenand_chip *this = mtd->priv;
  2085. unsigned int block_size;
  2086. loff_t addr = instr->addr;
  2087. loff_t len = instr->len;
  2088. int ret = 0;
  2089. struct mtd_erase_region_info *region = NULL;
  2090. loff_t region_offset = 0;
  2091. pr_debug("%s: start=0x%012llx, len=%llu\n", __func__,
  2092. (unsigned long long)instr->addr,
  2093. (unsigned long long)instr->len);
  2094. if (FLEXONENAND(this)) {
  2095. /* Find the eraseregion of this address */
  2096. int i = flexonenand_region(mtd, addr);
  2097. region = &mtd->eraseregions[i];
  2098. block_size = region->erasesize;
  2099. /* Start address within region must align on block boundary.
  2100. * Erase region's start offset is always block start address.
  2101. */
  2102. region_offset = region->offset;
  2103. } else
  2104. block_size = 1 << this->erase_shift;
  2105. /* Start address must align on block boundary */
  2106. if (unlikely((addr - region_offset) & (block_size - 1))) {
  2107. printk(KERN_ERR "%s: Unaligned address\n", __func__);
  2108. return -EINVAL;
  2109. }
  2110. /* Length must align on block boundary */
  2111. if (unlikely(len & (block_size - 1))) {
  2112. printk(KERN_ERR "%s: Length not block aligned\n", __func__);
  2113. return -EINVAL;
  2114. }
  2115. /* Grab the lock and see if the device is available */
  2116. onenand_get_device(mtd, FL_ERASING);
  2117. if (ONENAND_IS_4KB_PAGE(this) || region ||
  2118. instr->len < MB_ERASE_MIN_BLK_COUNT * block_size) {
  2119. /* region is set for Flex-OneNAND (no mb erase) */
  2120. ret = onenand_block_by_block_erase(mtd, instr,
  2121. region, block_size);
  2122. } else {
  2123. ret = onenand_multiblock_erase(mtd, instr, block_size);
  2124. }
  2125. /* Deselect and wake up anyone waiting on the device */
  2126. onenand_release_device(mtd);
  2127. /* Do call back function */
  2128. if (!ret) {
  2129. instr->state = MTD_ERASE_DONE;
  2130. mtd_erase_callback(instr);
  2131. }
  2132. return ret;
  2133. }
  2134. /**
  2135. * onenand_sync - [MTD Interface] sync
  2136. * @param mtd MTD device structure
  2137. *
  2138. * Sync is actually a wait for chip ready function
  2139. */
  2140. static void onenand_sync(struct mtd_info *mtd)
  2141. {
  2142. pr_debug("%s: called\n", __func__);
  2143. /* Grab the lock and see if the device is available */
  2144. onenand_get_device(mtd, FL_SYNCING);
  2145. /* Release it and go back */
  2146. onenand_release_device(mtd);
  2147. }
  2148. /**
  2149. * onenand_block_isbad - [MTD Interface] Check whether the block at the given offset is bad
  2150. * @param mtd MTD device structure
  2151. * @param ofs offset relative to mtd start
  2152. *
  2153. * Check whether the block is bad
  2154. */
  2155. static int onenand_block_isbad(struct mtd_info *mtd, loff_t ofs)
  2156. {
  2157. int ret;
  2158. /* Check for invalid offset */
  2159. if (ofs > mtd->size)
  2160. return -EINVAL;
  2161. onenand_get_device(mtd, FL_READING);
  2162. ret = onenand_block_isbad_nolock(mtd, ofs, 0);
  2163. onenand_release_device(mtd);
  2164. return ret;
  2165. }
  2166. /**
  2167. * onenand_default_block_markbad - [DEFAULT] mark a block bad
  2168. * @param mtd MTD device structure
  2169. * @param ofs offset from device start
  2170. *
  2171. * This is the default implementation, which can be overridden by
  2172. * a hardware specific driver.
  2173. */
  2174. static int onenand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
  2175. {
  2176. struct onenand_chip *this = mtd->priv;
  2177. struct bbm_info *bbm = this->bbm;
  2178. u_char buf[2] = {0, 0};
  2179. struct mtd_oob_ops ops = {
  2180. .mode = MTD_OPS_PLACE_OOB,
  2181. .ooblen = 2,
  2182. .oobbuf = buf,
  2183. .ooboffs = 0,
  2184. };
  2185. int block;
  2186. /* Get block number */
  2187. block = onenand_block(this, ofs);
  2188. if (bbm->bbt)
  2189. bbm->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1);
  2190. /* We write two bytes, so we don't have to mess with 16-bit access */
  2191. ofs += mtd->oobsize + (bbm->badblockpos & ~0x01);
  2192. /* FIXME : What to do when marking SLC block in partition
  2193. * with MLC erasesize? For now, it is not advisable to
  2194. * create partitions containing both SLC and MLC regions.
  2195. */
  2196. return onenand_write_oob_nolock(mtd, ofs, &ops);
  2197. }
  2198. /**
  2199. * onenand_block_markbad - [MTD Interface] Mark the block at the given offset as bad
  2200. * @param mtd MTD device structure
  2201. * @param ofs offset relative to mtd start
  2202. *
  2203. * Mark the block as bad
  2204. */
  2205. static int onenand_block_markbad(struct mtd_info *mtd, loff_t ofs)
  2206. {
  2207. int ret;
  2208. ret = onenand_block_isbad(mtd, ofs);
  2209. if (ret) {
  2210. /* If it was bad already, return success and do nothing */
  2211. if (ret > 0)
  2212. return 0;
  2213. return ret;
  2214. }
  2215. onenand_get_device(mtd, FL_WRITING);
  2216. ret = mtd_block_markbad(mtd, ofs);
  2217. onenand_release_device(mtd);
  2218. return ret;
  2219. }
  2220. /**
  2221. * onenand_do_lock_cmd - [OneNAND Interface] Lock or unlock block(s)
  2222. * @param mtd MTD device structure
  2223. * @param ofs offset relative to mtd start
  2224. * @param len number of bytes to lock or unlock
  2225. * @param cmd lock or unlock command
  2226. *
  2227. * Lock or unlock one or more blocks
  2228. */
  2229. static int onenand_do_lock_cmd(struct mtd_info *mtd, loff_t ofs, size_t len, int cmd)
  2230. {
  2231. struct onenand_chip *this = mtd->priv;
  2232. int start, end, block, value, status;
  2233. int wp_status_mask;
  2234. start = onenand_block(this, ofs);
  2235. end = onenand_block(this, ofs + len) - 1;
  2236. if (cmd == ONENAND_CMD_LOCK)
  2237. wp_status_mask = ONENAND_WP_LS;
  2238. else
  2239. wp_status_mask = ONENAND_WP_US;
  2240. /* Continuous lock scheme */
  2241. if (this->options & ONENAND_HAS_CONT_LOCK) {
  2242. /* Set start block address */
  2243. this->write_word(start, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
  2244. /* Set end block address */
  2245. this->write_word(end, this->base + ONENAND_REG_END_BLOCK_ADDRESS);
  2246. /* Write lock command */
  2247. this->command(mtd, cmd, 0, 0);
  2248. /* There's no return value */
  2249. this->wait(mtd, FL_LOCKING);
  2250. /* Sanity check */
  2251. while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
  2252. & ONENAND_CTRL_ONGO)
  2253. continue;
  2254. /* Check lock status */
  2255. status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
  2256. if (!(status & wp_status_mask))
  2257. printk(KERN_ERR "%s: wp status = 0x%x\n",
  2258. __func__, status);
  2259. return 0;
  2260. }
  2261. /* Block lock scheme */
  2262. for (block = start; block < end + 1; block++) {
  2263. /* Set block address */
  2264. value = onenand_block_address(this, block);
  2265. this->write_word(value, this->base + ONENAND_REG_START_ADDRESS1);
  2266. /* Select DataRAM for DDP */
  2267. value = onenand_bufferram_address(this, block);
  2268. this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
  2269. /* Set start block address */
  2270. this->write_word(block, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
  2271. /* Write lock command */
  2272. this->command(mtd, cmd, 0, 0);
  2273. /* There's no return value */
  2274. this->wait(mtd, FL_LOCKING);
  2275. /* Sanity check */
  2276. while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
  2277. & ONENAND_CTRL_ONGO)
  2278. continue;
  2279. /* Check lock status */
  2280. status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
  2281. if (!(status & wp_status_mask))
  2282. printk(KERN_ERR "%s: block = %d, wp status = 0x%x\n",
  2283. __func__, block, status);
  2284. }
  2285. return 0;
  2286. }
  2287. /**
  2288. * onenand_lock - [MTD Interface] Lock block(s)
  2289. * @param mtd MTD device structure
  2290. * @param ofs offset relative to mtd start
  2291. * @param len number of bytes to unlock
  2292. *
  2293. * Lock one or more blocks
  2294. */
  2295. static int onenand_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  2296. {
  2297. int ret;
  2298. onenand_get_device(mtd, FL_LOCKING);
  2299. ret = onenand_do_lock_cmd(mtd, ofs, len, ONENAND_CMD_LOCK);
  2300. onenand_release_device(mtd);
  2301. return ret;
  2302. }
  2303. /**
  2304. * onenand_unlock - [MTD Interface] Unlock block(s)
  2305. * @param mtd MTD device structure
  2306. * @param ofs offset relative to mtd start
  2307. * @param len number of bytes to unlock
  2308. *
  2309. * Unlock one or more blocks
  2310. */
  2311. static int onenand_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  2312. {
  2313. int ret;
  2314. onenand_get_device(mtd, FL_LOCKING);
  2315. ret = onenand_do_lock_cmd(mtd, ofs, len, ONENAND_CMD_UNLOCK);
  2316. onenand_release_device(mtd);
  2317. return ret;
  2318. }
  2319. /**
  2320. * onenand_check_lock_status - [OneNAND Interface] Check lock status
  2321. * @param this onenand chip data structure
  2322. *
  2323. * Check lock status
  2324. */
  2325. static int onenand_check_lock_status(struct onenand_chip *this)
  2326. {
  2327. unsigned int value, block, status;
  2328. unsigned int end;
  2329. end = this->chipsize >> this->erase_shift;
  2330. for (block = 0; block < end; block++) {
  2331. /* Set block address */
  2332. value = onenand_block_address(this, block);
  2333. this->write_word(value, this->base + ONENAND_REG_START_ADDRESS1);
  2334. /* Select DataRAM for DDP */
  2335. value = onenand_bufferram_address(this, block);
  2336. this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
  2337. /* Set start block address */
  2338. this->write_word(block, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
  2339. /* Check lock status */
  2340. status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
  2341. if (!(status & ONENAND_WP_US)) {
  2342. printk(KERN_ERR "%s: block = %d, wp status = 0x%x\n",
  2343. __func__, block, status);
  2344. return 0;
  2345. }
  2346. }
  2347. return 1;
  2348. }
  2349. /**
  2350. * onenand_unlock_all - [OneNAND Interface] unlock all blocks
  2351. * @param mtd MTD device structure
  2352. *
  2353. * Unlock all blocks
  2354. */
  2355. static void onenand_unlock_all(struct mtd_info *mtd)
  2356. {
  2357. struct onenand_chip *this = mtd->priv;
  2358. loff_t ofs = 0;
  2359. loff_t len = mtd->size;
  2360. if (this->options & ONENAND_HAS_UNLOCK_ALL) {
  2361. /* Set start block address */
  2362. this->write_word(0, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
  2363. /* Write unlock command */
  2364. this->command(mtd, ONENAND_CMD_UNLOCK_ALL, 0, 0);
  2365. /* There's no return value */
  2366. this->wait(mtd, FL_LOCKING);
  2367. /* Sanity check */
  2368. while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
  2369. & ONENAND_CTRL_ONGO)
  2370. continue;
  2371. /* Don't check lock status */
  2372. if (this->options & ONENAND_SKIP_UNLOCK_CHECK)
  2373. return;
  2374. /* Check lock status */
  2375. if (onenand_check_lock_status(this))
  2376. return;
  2377. /* Workaround for all block unlock in DDP */
  2378. if (ONENAND_IS_DDP(this) && !FLEXONENAND(this)) {
  2379. /* All blocks on another chip */
  2380. ofs = this->chipsize >> 1;
  2381. len = this->chipsize >> 1;
  2382. }
  2383. }
  2384. onenand_do_lock_cmd(mtd, ofs, len, ONENAND_CMD_UNLOCK);
  2385. }
  2386. #ifdef CONFIG_MTD_ONENAND_OTP
  2387. /**
  2388. * onenand_otp_command - Send OTP specific command to OneNAND device
  2389. * @param mtd MTD device structure
  2390. * @param cmd the command to be sent
  2391. * @param addr offset to read from or write to
  2392. * @param len number of bytes to read or write
  2393. */
  2394. static int onenand_otp_command(struct mtd_info *mtd, int cmd, loff_t addr,
  2395. size_t len)
  2396. {
  2397. struct onenand_chip *this = mtd->priv;
  2398. int value, block, page;
  2399. /* Address translation */
  2400. switch (cmd) {
  2401. case ONENAND_CMD_OTP_ACCESS:
  2402. block = (int) (addr >> this->erase_shift);
  2403. page = -1;
  2404. break;
  2405. default:
  2406. block = (int) (addr >> this->erase_shift);
  2407. page = (int) (addr >> this->page_shift);
  2408. if (ONENAND_IS_2PLANE(this)) {
  2409. /* Make the even block number */
  2410. block &= ~1;
  2411. /* Is it the odd plane? */
  2412. if (addr & this->writesize)
  2413. block++;
  2414. page >>= 1;
  2415. }
  2416. page &= this->page_mask;
  2417. break;
  2418. }
  2419. if (block != -1) {
  2420. /* Write 'DFS, FBA' of Flash */
  2421. value = onenand_block_address(this, block);
  2422. this->write_word(value, this->base +
  2423. ONENAND_REG_START_ADDRESS1);
  2424. }
  2425. if (page != -1) {
  2426. /* Now we use page size operation */
  2427. int sectors = 4, count = 4;
  2428. int dataram;
  2429. switch (cmd) {
  2430. default:
  2431. if (ONENAND_IS_2PLANE(this) && cmd == ONENAND_CMD_PROG)
  2432. cmd = ONENAND_CMD_2X_PROG;
  2433. dataram = ONENAND_CURRENT_BUFFERRAM(this);
  2434. break;
  2435. }
  2436. /* Write 'FPA, FSA' of Flash */
  2437. value = onenand_page_address(page, sectors);
  2438. this->write_word(value, this->base +
  2439. ONENAND_REG_START_ADDRESS8);
  2440. /* Write 'BSA, BSC' of DataRAM */
  2441. value = onenand_buffer_address(dataram, sectors, count);
  2442. this->write_word(value, this->base + ONENAND_REG_START_BUFFER);
  2443. }
  2444. /* Interrupt clear */
  2445. this->write_word(ONENAND_INT_CLEAR, this->base + ONENAND_REG_INTERRUPT);
  2446. /* Write command */
  2447. this->write_word(cmd, this->base + ONENAND_REG_COMMAND);
  2448. return 0;
  2449. }
  2450. /**
  2451. * onenand_otp_write_oob_nolock - [INTERN] OneNAND write out-of-band, specific to OTP
  2452. * @param mtd MTD device structure
  2453. * @param to offset to write to
  2454. * @param len number of bytes to write
  2455. * @param retlen pointer to variable to store the number of written bytes
  2456. * @param buf the data to write
  2457. *
  2458. * OneNAND write out-of-band only for OTP
  2459. */
  2460. static int onenand_otp_write_oob_nolock(struct mtd_info *mtd, loff_t to,
  2461. struct mtd_oob_ops *ops)
  2462. {
  2463. struct onenand_chip *this = mtd->priv;
  2464. int column, ret = 0, oobsize;
  2465. int written = 0;
  2466. u_char *oobbuf;
  2467. size_t len = ops->ooblen;
  2468. const u_char *buf = ops->oobbuf;
  2469. int block, value, status;
  2470. to += ops->ooboffs;
  2471. /* Initialize retlen, in case of early exit */
  2472. ops->oobretlen = 0;
  2473. oobsize = mtd->oobsize;
  2474. column = to & (mtd->oobsize - 1);
  2475. oobbuf = this->oob_buf;
  2476. /* Loop until all data write */
  2477. while (written < len) {
  2478. int thislen = min_t(int, oobsize, len - written);
  2479. cond_resched();
  2480. block = (int) (to >> this->erase_shift);
  2481. /*
  2482. * Write 'DFS, FBA' of Flash
  2483. * Add: F100h DQ=DFS, FBA
  2484. */
  2485. value = onenand_block_address(this, block);
  2486. this->write_word(value, this->base +
  2487. ONENAND_REG_START_ADDRESS1);
  2488. /*
  2489. * Select DataRAM for DDP
  2490. * Add: F101h DQ=DBS
  2491. */
  2492. value = onenand_bufferram_address(this, block);
  2493. this->write_word(value, this->base +
  2494. ONENAND_REG_START_ADDRESS2);
  2495. ONENAND_SET_NEXT_BUFFERRAM(this);
  2496. /*
  2497. * Enter OTP access mode
  2498. */
  2499. this->command(mtd, ONENAND_CMD_OTP_ACCESS, 0, 0);
  2500. this->wait(mtd, FL_OTPING);
  2501. /* We send data to spare ram with oobsize
  2502. * to prevent byte access */
  2503. memcpy(oobbuf + column, buf, thislen);
  2504. /*
  2505. * Write Data into DataRAM
  2506. * Add: 8th Word
  2507. * in sector0/spare/page0
  2508. * DQ=XXFCh
  2509. */
  2510. this->write_bufferram(mtd, ONENAND_SPARERAM,
  2511. oobbuf, 0, mtd->oobsize);
  2512. onenand_otp_command(mtd, ONENAND_CMD_PROGOOB, to, mtd->oobsize);
  2513. onenand_update_bufferram(mtd, to, 0);
  2514. if (ONENAND_IS_2PLANE(this)) {
  2515. ONENAND_SET_BUFFERRAM1(this);
  2516. onenand_update_bufferram(mtd, to + this->writesize, 0);
  2517. }
  2518. ret = this->wait(mtd, FL_WRITING);
  2519. if (ret) {
  2520. printk(KERN_ERR "%s: write failed %d\n", __func__, ret);
  2521. break;
  2522. }
  2523. /* Exit OTP access mode */
  2524. this->command(mtd, ONENAND_CMD_RESET, 0, 0);
  2525. this->wait(mtd, FL_RESETING);
  2526. status = this->read_word(this->base + ONENAND_REG_CTRL_STATUS);
  2527. status &= 0x60;
  2528. if (status == 0x60) {
  2529. printk(KERN_DEBUG "\nBLOCK\tSTATUS\n");
  2530. printk(KERN_DEBUG "1st Block\tLOCKED\n");
  2531. printk(KERN_DEBUG "OTP Block\tLOCKED\n");
  2532. } else if (status == 0x20) {
  2533. printk(KERN_DEBUG "\nBLOCK\tSTATUS\n");
  2534. printk(KERN_DEBUG "1st Block\tLOCKED\n");
  2535. printk(KERN_DEBUG "OTP Block\tUN-LOCKED\n");
  2536. } else if (status == 0x40) {
  2537. printk(KERN_DEBUG "\nBLOCK\tSTATUS\n");
  2538. printk(KERN_DEBUG "1st Block\tUN-LOCKED\n");
  2539. printk(KERN_DEBUG "OTP Block\tLOCKED\n");
  2540. } else {
  2541. printk(KERN_DEBUG "Reboot to check\n");
  2542. }
  2543. written += thislen;
  2544. if (written == len)
  2545. break;
  2546. to += mtd->writesize;
  2547. buf += thislen;
  2548. column = 0;
  2549. }
  2550. ops->oobretlen = written;
  2551. return ret;
  2552. }
  2553. /* Internal OTP operation */
  2554. typedef int (*otp_op_t)(struct mtd_info *mtd, loff_t form, size_t len,
  2555. size_t *retlen, u_char *buf);
  2556. /**
  2557. * do_otp_read - [DEFAULT] Read OTP block area
  2558. * @param mtd MTD device structure
  2559. * @param from The offset to read
  2560. * @param len number of bytes to read
  2561. * @param retlen pointer to variable to store the number of readbytes
  2562. * @param buf the databuffer to put/get data
  2563. *
  2564. * Read OTP block area.
  2565. */
  2566. static int do_otp_read(struct mtd_info *mtd, loff_t from, size_t len,
  2567. size_t *retlen, u_char *buf)
  2568. {
  2569. struct onenand_chip *this = mtd->priv;
  2570. struct mtd_oob_ops ops = {
  2571. .len = len,
  2572. .ooblen = 0,
  2573. .datbuf = buf,
  2574. .oobbuf = NULL,
  2575. };
  2576. int ret;
  2577. /* Enter OTP access mode */
  2578. this->command(mtd, ONENAND_CMD_OTP_ACCESS, 0, 0);
  2579. this->wait(mtd, FL_OTPING);
  2580. ret = ONENAND_IS_4KB_PAGE(this) ?
  2581. onenand_mlc_read_ops_nolock(mtd, from, &ops) :
  2582. onenand_read_ops_nolock(mtd, from, &ops);
  2583. /* Exit OTP access mode */
  2584. this->command(mtd, ONENAND_CMD_RESET, 0, 0);
  2585. this->wait(mtd, FL_RESETING);
  2586. return ret;
  2587. }
  2588. /**
  2589. * do_otp_write - [DEFAULT] Write OTP block area
  2590. * @param mtd MTD device structure
  2591. * @param to The offset to write
  2592. * @param len number of bytes to write
  2593. * @param retlen pointer to variable to store the number of write bytes
  2594. * @param buf the databuffer to put/get data
  2595. *
  2596. * Write OTP block area.
  2597. */
  2598. static int do_otp_write(struct mtd_info *mtd, loff_t to, size_t len,
  2599. size_t *retlen, u_char *buf)
  2600. {
  2601. struct onenand_chip *this = mtd->priv;
  2602. unsigned char *pbuf = buf;
  2603. int ret;
  2604. struct mtd_oob_ops ops;
  2605. /* Force buffer page aligned */
  2606. if (len < mtd->writesize) {
  2607. memcpy(this->page_buf, buf, len);
  2608. memset(this->page_buf + len, 0xff, mtd->writesize - len);
  2609. pbuf = this->page_buf;
  2610. len = mtd->writesize;
  2611. }
  2612. /* Enter OTP access mode */
  2613. this->command(mtd, ONENAND_CMD_OTP_ACCESS, 0, 0);
  2614. this->wait(mtd, FL_OTPING);
  2615. ops.len = len;
  2616. ops.ooblen = 0;
  2617. ops.datbuf = pbuf;
  2618. ops.oobbuf = NULL;
  2619. ret = onenand_write_ops_nolock(mtd, to, &ops);
  2620. *retlen = ops.retlen;
  2621. /* Exit OTP access mode */
  2622. this->command(mtd, ONENAND_CMD_RESET, 0, 0);
  2623. this->wait(mtd, FL_RESETING);
  2624. return ret;
  2625. }
  2626. /**
  2627. * do_otp_lock - [DEFAULT] Lock OTP block area
  2628. * @param mtd MTD device structure
  2629. * @param from The offset to lock
  2630. * @param len number of bytes to lock
  2631. * @param retlen pointer to variable to store the number of lock bytes
  2632. * @param buf the databuffer to put/get data
  2633. *
  2634. * Lock OTP block area.
  2635. */
  2636. static int do_otp_lock(struct mtd_info *mtd, loff_t from, size_t len,
  2637. size_t *retlen, u_char *buf)
  2638. {
  2639. struct onenand_chip *this = mtd->priv;
  2640. struct mtd_oob_ops ops;
  2641. int ret;
  2642. if (FLEXONENAND(this)) {
  2643. /* Enter OTP access mode */
  2644. this->command(mtd, ONENAND_CMD_OTP_ACCESS, 0, 0);
  2645. this->wait(mtd, FL_OTPING);
  2646. /*
  2647. * For Flex-OneNAND, we write lock mark to 1st word of sector 4 of
  2648. * main area of page 49.
  2649. */
  2650. ops.len = mtd->writesize;
  2651. ops.ooblen = 0;
  2652. ops.datbuf = buf;
  2653. ops.oobbuf = NULL;
  2654. ret = onenand_write_ops_nolock(mtd, mtd->writesize * 49, &ops);
  2655. *retlen = ops.retlen;
  2656. /* Exit OTP access mode */
  2657. this->command(mtd, ONENAND_CMD_RESET, 0, 0);
  2658. this->wait(mtd, FL_RESETING);
  2659. } else {
  2660. ops.mode = MTD_OPS_PLACE_OOB;
  2661. ops.ooblen = len;
  2662. ops.oobbuf = buf;
  2663. ops.ooboffs = 0;
  2664. ret = onenand_otp_write_oob_nolock(mtd, from, &ops);
  2665. *retlen = ops.oobretlen;
  2666. }
  2667. return ret;
  2668. }
  2669. /**
  2670. * onenand_otp_walk - [DEFAULT] Handle OTP operation
  2671. * @param mtd MTD device structure
  2672. * @param from The offset to read/write
  2673. * @param len number of bytes to read/write
  2674. * @param retlen pointer to variable to store the number of read bytes
  2675. * @param buf the databuffer to put/get data
  2676. * @param action do given action
  2677. * @param mode specify user and factory
  2678. *
  2679. * Handle OTP operation.
  2680. */
  2681. static int onenand_otp_walk(struct mtd_info *mtd, loff_t from, size_t len,
  2682. size_t *retlen, u_char *buf,
  2683. otp_op_t action, int mode)
  2684. {
  2685. struct onenand_chip *this = mtd->priv;
  2686. int otp_pages;
  2687. int density;
  2688. int ret = 0;
  2689. *retlen = 0;
  2690. density = onenand_get_density(this->device_id);
  2691. if (density < ONENAND_DEVICE_DENSITY_512Mb)
  2692. otp_pages = 20;
  2693. else
  2694. otp_pages = 50;
  2695. if (mode == MTD_OTP_FACTORY) {
  2696. from += mtd->writesize * otp_pages;
  2697. otp_pages = ONENAND_PAGES_PER_BLOCK - otp_pages;
  2698. }
  2699. /* Check User/Factory boundary */
  2700. if (mode == MTD_OTP_USER) {
  2701. if (mtd->writesize * otp_pages < from + len)
  2702. return 0;
  2703. } else {
  2704. if (mtd->writesize * otp_pages < len)
  2705. return 0;
  2706. }
  2707. onenand_get_device(mtd, FL_OTPING);
  2708. while (len > 0 && otp_pages > 0) {
  2709. if (!action) { /* OTP Info functions */
  2710. struct otp_info *otpinfo;
  2711. len -= sizeof(struct otp_info);
  2712. if (len <= 0) {
  2713. ret = -ENOSPC;
  2714. break;
  2715. }
  2716. otpinfo = (struct otp_info *) buf;
  2717. otpinfo->start = from;
  2718. otpinfo->length = mtd->writesize;
  2719. otpinfo->locked = 0;
  2720. from += mtd->writesize;
  2721. buf += sizeof(struct otp_info);
  2722. *retlen += sizeof(struct otp_info);
  2723. } else {
  2724. size_t tmp_retlen;
  2725. ret = action(mtd, from, len, &tmp_retlen, buf);
  2726. buf += tmp_retlen;
  2727. len -= tmp_retlen;
  2728. *retlen += tmp_retlen;
  2729. if (ret)
  2730. break;
  2731. }
  2732. otp_pages--;
  2733. }
  2734. onenand_release_device(mtd);
  2735. return ret;
  2736. }
  2737. /**
  2738. * onenand_get_fact_prot_info - [MTD Interface] Read factory OTP info
  2739. * @param mtd MTD device structure
  2740. * @param buf the databuffer to put/get data
  2741. * @param len number of bytes to read
  2742. *
  2743. * Read factory OTP info.
  2744. */
  2745. static int onenand_get_fact_prot_info(struct mtd_info *mtd,
  2746. struct otp_info *buf, size_t len)
  2747. {
  2748. size_t retlen;
  2749. int ret;
  2750. ret = onenand_otp_walk(mtd, 0, len, &retlen, (u_char *) buf, NULL, MTD_OTP_FACTORY);
  2751. return ret ? : retlen;
  2752. }
  2753. /**
  2754. * onenand_read_fact_prot_reg - [MTD Interface] Read factory OTP area
  2755. * @param mtd MTD device structure
  2756. * @param from The offset to read
  2757. * @param len number of bytes to read
  2758. * @param retlen pointer to variable to store the number of read bytes
  2759. * @param buf the databuffer to put/get data
  2760. *
  2761. * Read factory OTP area.
  2762. */
  2763. static int onenand_read_fact_prot_reg(struct mtd_info *mtd, loff_t from,
  2764. size_t len, size_t *retlen, u_char *buf)
  2765. {
  2766. return onenand_otp_walk(mtd, from, len, retlen, buf, do_otp_read, MTD_OTP_FACTORY);
  2767. }
  2768. /**
  2769. * onenand_get_user_prot_info - [MTD Interface] Read user OTP info
  2770. * @param mtd MTD device structure
  2771. * @param buf the databuffer to put/get data
  2772. * @param len number of bytes to read
  2773. *
  2774. * Read user OTP info.
  2775. */
  2776. static int onenand_get_user_prot_info(struct mtd_info *mtd,
  2777. struct otp_info *buf, size_t len)
  2778. {
  2779. size_t retlen;
  2780. int ret;
  2781. ret = onenand_otp_walk(mtd, 0, len, &retlen, (u_char *) buf, NULL, MTD_OTP_USER);
  2782. return ret ? : retlen;
  2783. }
  2784. /**
  2785. * onenand_read_user_prot_reg - [MTD Interface] Read user OTP area
  2786. * @param mtd MTD device structure
  2787. * @param from The offset to read
  2788. * @param len number of bytes to read
  2789. * @param retlen pointer to variable to store the number of read bytes
  2790. * @param buf the databuffer to put/get data
  2791. *
  2792. * Read user OTP area.
  2793. */
  2794. static int onenand_read_user_prot_reg(struct mtd_info *mtd, loff_t from,
  2795. size_t len, size_t *retlen, u_char *buf)
  2796. {
  2797. return onenand_otp_walk(mtd, from, len, retlen, buf, do_otp_read, MTD_OTP_USER);
  2798. }
  2799. /**
  2800. * onenand_write_user_prot_reg - [MTD Interface] Write user OTP area
  2801. * @param mtd MTD device structure
  2802. * @param from The offset to write
  2803. * @param len number of bytes to write
  2804. * @param retlen pointer to variable to store the number of write bytes
  2805. * @param buf the databuffer to put/get data
  2806. *
  2807. * Write user OTP area.
  2808. */
  2809. static int onenand_write_user_prot_reg(struct mtd_info *mtd, loff_t from,
  2810. size_t len, size_t *retlen, u_char *buf)
  2811. {
  2812. return onenand_otp_walk(mtd, from, len, retlen, buf, do_otp_write, MTD_OTP_USER);
  2813. }
  2814. /**
  2815. * onenand_lock_user_prot_reg - [MTD Interface] Lock user OTP area
  2816. * @param mtd MTD device structure
  2817. * @param from The offset to lock
  2818. * @param len number of bytes to unlock
  2819. *
  2820. * Write lock mark on spare area in page 0 in OTP block
  2821. */
  2822. static int onenand_lock_user_prot_reg(struct mtd_info *mtd, loff_t from,
  2823. size_t len)
  2824. {
  2825. struct onenand_chip *this = mtd->priv;
  2826. u_char *buf = FLEXONENAND(this) ? this->page_buf : this->oob_buf;
  2827. size_t retlen;
  2828. int ret;
  2829. unsigned int otp_lock_offset = ONENAND_OTP_LOCK_OFFSET;
  2830. memset(buf, 0xff, FLEXONENAND(this) ? this->writesize
  2831. : mtd->oobsize);
  2832. /*
  2833. * Write lock mark to 8th word of sector0 of page0 of the spare0.
  2834. * We write 16 bytes spare area instead of 2 bytes.
  2835. * For Flex-OneNAND, we write lock mark to 1st word of sector 4 of
  2836. * main area of page 49.
  2837. */
  2838. from = 0;
  2839. len = FLEXONENAND(this) ? mtd->writesize : 16;
  2840. /*
  2841. * Note: OTP lock operation
  2842. * OTP block : 0xXXFC XX 1111 1100
  2843. * 1st block : 0xXXF3 (If chip support) XX 1111 0011
  2844. * Both : 0xXXF0 (If chip support) XX 1111 0000
  2845. */
  2846. if (FLEXONENAND(this))
  2847. otp_lock_offset = FLEXONENAND_OTP_LOCK_OFFSET;
  2848. /* ONENAND_OTP_AREA | ONENAND_OTP_BLOCK0 | ONENAND_OTP_AREA_BLOCK0 */
  2849. if (otp == 1)
  2850. buf[otp_lock_offset] = 0xFC;
  2851. else if (otp == 2)
  2852. buf[otp_lock_offset] = 0xF3;
  2853. else if (otp == 3)
  2854. buf[otp_lock_offset] = 0xF0;
  2855. else if (otp != 0)
  2856. printk(KERN_DEBUG "[OneNAND] Invalid option selected for OTP\n");
  2857. ret = onenand_otp_walk(mtd, from, len, &retlen, buf, do_otp_lock, MTD_OTP_USER);
  2858. return ret ? : retlen;
  2859. }
  2860. #endif /* CONFIG_MTD_ONENAND_OTP */
  2861. /**
  2862. * onenand_check_features - Check and set OneNAND features
  2863. * @param mtd MTD data structure
  2864. *
  2865. * Check and set OneNAND features
  2866. * - lock scheme
  2867. * - two plane
  2868. */
  2869. static void onenand_check_features(struct mtd_info *mtd)
  2870. {
  2871. struct onenand_chip *this = mtd->priv;
  2872. unsigned int density, process, numbufs;
  2873. /* Lock scheme depends on density and process */
  2874. density = onenand_get_density(this->device_id);
  2875. process = this->version_id >> ONENAND_VERSION_PROCESS_SHIFT;
  2876. numbufs = this->read_word(this->base + ONENAND_REG_NUM_BUFFERS) >> 8;
  2877. /* Lock scheme */
  2878. switch (density) {
  2879. case ONENAND_DEVICE_DENSITY_4Gb:
  2880. if (ONENAND_IS_DDP(this))
  2881. this->options |= ONENAND_HAS_2PLANE;
  2882. else if (numbufs == 1) {
  2883. this->options |= ONENAND_HAS_4KB_PAGE;
  2884. this->options |= ONENAND_HAS_CACHE_PROGRAM;
  2885. /*
  2886. * There are two different 4KiB pagesize chips
  2887. * and no way to detect it by H/W config values.
  2888. *
  2889. * To detect the correct NOP for each chips,
  2890. * It should check the version ID as workaround.
  2891. *
  2892. * Now it has as following
  2893. * KFM4G16Q4M has NOP 4 with version ID 0x0131
  2894. * KFM4G16Q5M has NOP 1 with versoin ID 0x013e
  2895. */
  2896. if ((this->version_id & 0xf) == 0xe)
  2897. this->options |= ONENAND_HAS_NOP_1;
  2898. }
  2899. case ONENAND_DEVICE_DENSITY_2Gb:
  2900. /* 2Gb DDP does not have 2 plane */
  2901. if (!ONENAND_IS_DDP(this))
  2902. this->options |= ONENAND_HAS_2PLANE;
  2903. this->options |= ONENAND_HAS_UNLOCK_ALL;
  2904. case ONENAND_DEVICE_DENSITY_1Gb:
  2905. /* A-Die has all block unlock */
  2906. if (process)
  2907. this->options |= ONENAND_HAS_UNLOCK_ALL;
  2908. break;
  2909. default:
  2910. /* Some OneNAND has continuous lock scheme */
  2911. if (!process)
  2912. this->options |= ONENAND_HAS_CONT_LOCK;
  2913. break;
  2914. }
  2915. /* The MLC has 4KiB pagesize. */
  2916. if (ONENAND_IS_MLC(this))
  2917. this->options |= ONENAND_HAS_4KB_PAGE;
  2918. if (ONENAND_IS_4KB_PAGE(this))
  2919. this->options &= ~ONENAND_HAS_2PLANE;
  2920. if (FLEXONENAND(this)) {
  2921. this->options &= ~ONENAND_HAS_CONT_LOCK;
  2922. this->options |= ONENAND_HAS_UNLOCK_ALL;
  2923. }
  2924. if (this->options & ONENAND_HAS_CONT_LOCK)
  2925. printk(KERN_DEBUG "Lock scheme is Continuous Lock\n");
  2926. if (this->options & ONENAND_HAS_UNLOCK_ALL)
  2927. printk(KERN_DEBUG "Chip support all block unlock\n");
  2928. if (this->options & ONENAND_HAS_2PLANE)
  2929. printk(KERN_DEBUG "Chip has 2 plane\n");
  2930. if (this->options & ONENAND_HAS_4KB_PAGE)
  2931. printk(KERN_DEBUG "Chip has 4KiB pagesize\n");
  2932. if (this->options & ONENAND_HAS_CACHE_PROGRAM)
  2933. printk(KERN_DEBUG "Chip has cache program feature\n");
  2934. }
  2935. /**
  2936. * onenand_print_device_info - Print device & version ID
  2937. * @param device device ID
  2938. * @param version version ID
  2939. *
  2940. * Print device & version ID
  2941. */
  2942. static void onenand_print_device_info(int device, int version)
  2943. {
  2944. int vcc, demuxed, ddp, density, flexonenand;
  2945. vcc = device & ONENAND_DEVICE_VCC_MASK;
  2946. demuxed = device & ONENAND_DEVICE_IS_DEMUX;
  2947. ddp = device & ONENAND_DEVICE_IS_DDP;
  2948. density = onenand_get_density(device);
  2949. flexonenand = device & DEVICE_IS_FLEXONENAND;
  2950. printk(KERN_INFO "%s%sOneNAND%s %dMB %sV 16-bit (0x%02x)\n",
  2951. demuxed ? "" : "Muxed ",
  2952. flexonenand ? "Flex-" : "",
  2953. ddp ? "(DDP)" : "",
  2954. (16 << density),
  2955. vcc ? "2.65/3.3" : "1.8",
  2956. device);
  2957. printk(KERN_INFO "OneNAND version = 0x%04x\n", version);
  2958. }
  2959. static const struct onenand_manufacturers onenand_manuf_ids[] = {
  2960. {ONENAND_MFR_SAMSUNG, "Samsung"},
  2961. {ONENAND_MFR_NUMONYX, "Numonyx"},
  2962. };
  2963. /**
  2964. * onenand_check_maf - Check manufacturer ID
  2965. * @param manuf manufacturer ID
  2966. *
  2967. * Check manufacturer ID
  2968. */
  2969. static int onenand_check_maf(int manuf)
  2970. {
  2971. int size = ARRAY_SIZE(onenand_manuf_ids);
  2972. char *name;
  2973. int i;
  2974. for (i = 0; i < size; i++)
  2975. if (manuf == onenand_manuf_ids[i].id)
  2976. break;
  2977. if (i < size)
  2978. name = onenand_manuf_ids[i].name;
  2979. else
  2980. name = "Unknown";
  2981. printk(KERN_DEBUG "OneNAND Manufacturer: %s (0x%0x)\n", name, manuf);
  2982. return (i == size);
  2983. }
  2984. /**
  2985. * flexonenand_get_boundary - Reads the SLC boundary
  2986. * @param onenand_info - onenand info structure
  2987. **/
  2988. static int flexonenand_get_boundary(struct mtd_info *mtd)
  2989. {
  2990. struct onenand_chip *this = mtd->priv;
  2991. unsigned die, bdry;
  2992. int ret, syscfg, locked;
  2993. /* Disable ECC */
  2994. syscfg = this->read_word(this->base + ONENAND_REG_SYS_CFG1);
  2995. this->write_word((syscfg | 0x0100), this->base + ONENAND_REG_SYS_CFG1);
  2996. for (die = 0; die < this->dies; die++) {
  2997. this->command(mtd, FLEXONENAND_CMD_PI_ACCESS, die, 0);
  2998. this->wait(mtd, FL_SYNCING);
  2999. this->command(mtd, FLEXONENAND_CMD_READ_PI, die, 0);
  3000. ret = this->wait(mtd, FL_READING);
  3001. bdry = this->read_word(this->base + ONENAND_DATARAM);
  3002. if ((bdry >> FLEXONENAND_PI_UNLOCK_SHIFT) == 3)
  3003. locked = 0;
  3004. else
  3005. locked = 1;
  3006. this->boundary[die] = bdry & FLEXONENAND_PI_MASK;
  3007. this->command(mtd, ONENAND_CMD_RESET, 0, 0);
  3008. ret = this->wait(mtd, FL_RESETING);
  3009. printk(KERN_INFO "Die %d boundary: %d%s\n", die,
  3010. this->boundary[die], locked ? "(Locked)" : "(Unlocked)");
  3011. }
  3012. /* Enable ECC */
  3013. this->write_word(syscfg, this->base + ONENAND_REG_SYS_CFG1);
  3014. return 0;
  3015. }
  3016. /**
  3017. * flexonenand_get_size - Fill up fields in onenand_chip and mtd_info
  3018. * boundary[], diesize[], mtd->size, mtd->erasesize
  3019. * @param mtd - MTD device structure
  3020. */
  3021. static void flexonenand_get_size(struct mtd_info *mtd)
  3022. {
  3023. struct onenand_chip *this = mtd->priv;
  3024. int die, i, eraseshift, density;
  3025. int blksperdie, maxbdry;
  3026. loff_t ofs;
  3027. density = onenand_get_density(this->device_id);
  3028. blksperdie = ((loff_t)(16 << density) << 20) >> (this->erase_shift);
  3029. blksperdie >>= ONENAND_IS_DDP(this) ? 1 : 0;
  3030. maxbdry = blksperdie - 1;
  3031. eraseshift = this->erase_shift - 1;
  3032. mtd->numeraseregions = this->dies << 1;
  3033. /* This fills up the device boundary */
  3034. flexonenand_get_boundary(mtd);
  3035. die = ofs = 0;
  3036. i = -1;
  3037. for (; die < this->dies; die++) {
  3038. if (!die || this->boundary[die-1] != maxbdry) {
  3039. i++;
  3040. mtd->eraseregions[i].offset = ofs;
  3041. mtd->eraseregions[i].erasesize = 1 << eraseshift;
  3042. mtd->eraseregions[i].numblocks =
  3043. this->boundary[die] + 1;
  3044. ofs += mtd->eraseregions[i].numblocks << eraseshift;
  3045. eraseshift++;
  3046. } else {
  3047. mtd->numeraseregions -= 1;
  3048. mtd->eraseregions[i].numblocks +=
  3049. this->boundary[die] + 1;
  3050. ofs += (this->boundary[die] + 1) << (eraseshift - 1);
  3051. }
  3052. if (this->boundary[die] != maxbdry) {
  3053. i++;
  3054. mtd->eraseregions[i].offset = ofs;
  3055. mtd->eraseregions[i].erasesize = 1 << eraseshift;
  3056. mtd->eraseregions[i].numblocks = maxbdry ^
  3057. this->boundary[die];
  3058. ofs += mtd->eraseregions[i].numblocks << eraseshift;
  3059. eraseshift--;
  3060. } else
  3061. mtd->numeraseregions -= 1;
  3062. }
  3063. /* Expose MLC erase size except when all blocks are SLC */
  3064. mtd->erasesize = 1 << this->erase_shift;
  3065. if (mtd->numeraseregions == 1)
  3066. mtd->erasesize >>= 1;
  3067. printk(KERN_INFO "Device has %d eraseregions\n", mtd->numeraseregions);
  3068. for (i = 0; i < mtd->numeraseregions; i++)
  3069. printk(KERN_INFO "[offset: 0x%08x, erasesize: 0x%05x,"
  3070. " numblocks: %04u]\n",
  3071. (unsigned int) mtd->eraseregions[i].offset,
  3072. mtd->eraseregions[i].erasesize,
  3073. mtd->eraseregions[i].numblocks);
  3074. for (die = 0, mtd->size = 0; die < this->dies; die++) {
  3075. this->diesize[die] = (loff_t)blksperdie << this->erase_shift;
  3076. this->diesize[die] -= (loff_t)(this->boundary[die] + 1)
  3077. << (this->erase_shift - 1);
  3078. mtd->size += this->diesize[die];
  3079. }
  3080. }
  3081. /**
  3082. * flexonenand_check_blocks_erased - Check if blocks are erased
  3083. * @param mtd_info - mtd info structure
  3084. * @param start - first erase block to check
  3085. * @param end - last erase block to check
  3086. *
  3087. * Converting an unerased block from MLC to SLC
  3088. * causes byte values to change. Since both data and its ECC
  3089. * have changed, reads on the block give uncorrectable error.
  3090. * This might lead to the block being detected as bad.
  3091. *
  3092. * Avoid this by ensuring that the block to be converted is
  3093. * erased.
  3094. */
  3095. static int flexonenand_check_blocks_erased(struct mtd_info *mtd, int start, int end)
  3096. {
  3097. struct onenand_chip *this = mtd->priv;
  3098. int i, ret;
  3099. int block;
  3100. struct mtd_oob_ops ops = {
  3101. .mode = MTD_OPS_PLACE_OOB,
  3102. .ooboffs = 0,
  3103. .ooblen = mtd->oobsize,
  3104. .datbuf = NULL,
  3105. .oobbuf = this->oob_buf,
  3106. };
  3107. loff_t addr;
  3108. printk(KERN_DEBUG "Check blocks from %d to %d\n", start, end);
  3109. for (block = start; block <= end; block++) {
  3110. addr = flexonenand_addr(this, block);
  3111. if (onenand_block_isbad_nolock(mtd, addr, 0))
  3112. continue;
  3113. /*
  3114. * Since main area write results in ECC write to spare,
  3115. * it is sufficient to check only ECC bytes for change.
  3116. */
  3117. ret = onenand_read_oob_nolock(mtd, addr, &ops);
  3118. if (ret)
  3119. return ret;
  3120. for (i = 0; i < mtd->oobsize; i++)
  3121. if (this->oob_buf[i] != 0xff)
  3122. break;
  3123. if (i != mtd->oobsize) {
  3124. printk(KERN_WARNING "%s: Block %d not erased.\n",
  3125. __func__, block);
  3126. return 1;
  3127. }
  3128. }
  3129. return 0;
  3130. }
  3131. /**
  3132. * flexonenand_set_boundary - Writes the SLC boundary
  3133. * @param mtd - mtd info structure
  3134. */
  3135. int flexonenand_set_boundary(struct mtd_info *mtd, int die,
  3136. int boundary, int lock)
  3137. {
  3138. struct onenand_chip *this = mtd->priv;
  3139. int ret, density, blksperdie, old, new, thisboundary;
  3140. loff_t addr;
  3141. /* Change only once for SDP Flex-OneNAND */
  3142. if (die && (!ONENAND_IS_DDP(this)))
  3143. return 0;
  3144. /* boundary value of -1 indicates no required change */
  3145. if (boundary < 0 || boundary == this->boundary[die])
  3146. return 0;
  3147. density = onenand_get_density(this->device_id);
  3148. blksperdie = ((16 << density) << 20) >> this->erase_shift;
  3149. blksperdie >>= ONENAND_IS_DDP(this) ? 1 : 0;
  3150. if (boundary >= blksperdie) {
  3151. printk(KERN_ERR "%s: Invalid boundary value. "
  3152. "Boundary not changed.\n", __func__);
  3153. return -EINVAL;
  3154. }
  3155. /* Check if converting blocks are erased */
  3156. old = this->boundary[die] + (die * this->density_mask);
  3157. new = boundary + (die * this->density_mask);
  3158. ret = flexonenand_check_blocks_erased(mtd, min(old, new) + 1, max(old, new));
  3159. if (ret) {
  3160. printk(KERN_ERR "%s: Please erase blocks "
  3161. "before boundary change\n", __func__);
  3162. return ret;
  3163. }
  3164. this->command(mtd, FLEXONENAND_CMD_PI_ACCESS, die, 0);
  3165. this->wait(mtd, FL_SYNCING);
  3166. /* Check is boundary is locked */
  3167. this->command(mtd, FLEXONENAND_CMD_READ_PI, die, 0);
  3168. ret = this->wait(mtd, FL_READING);
  3169. thisboundary = this->read_word(this->base + ONENAND_DATARAM);
  3170. if ((thisboundary >> FLEXONENAND_PI_UNLOCK_SHIFT) != 3) {
  3171. printk(KERN_ERR "%s: boundary locked\n", __func__);
  3172. ret = 1;
  3173. goto out;
  3174. }
  3175. printk(KERN_INFO "Changing die %d boundary: %d%s\n",
  3176. die, boundary, lock ? "(Locked)" : "(Unlocked)");
  3177. addr = die ? this->diesize[0] : 0;
  3178. boundary &= FLEXONENAND_PI_MASK;
  3179. boundary |= lock ? 0 : (3 << FLEXONENAND_PI_UNLOCK_SHIFT);
  3180. this->command(mtd, ONENAND_CMD_ERASE, addr, 0);
  3181. ret = this->wait(mtd, FL_ERASING);
  3182. if (ret) {
  3183. printk(KERN_ERR "%s: Failed PI erase for Die %d\n",
  3184. __func__, die);
  3185. goto out;
  3186. }
  3187. this->write_word(boundary, this->base + ONENAND_DATARAM);
  3188. this->command(mtd, ONENAND_CMD_PROG, addr, 0);
  3189. ret = this->wait(mtd, FL_WRITING);
  3190. if (ret) {
  3191. printk(KERN_ERR "%s: Failed PI write for Die %d\n",
  3192. __func__, die);
  3193. goto out;
  3194. }
  3195. this->command(mtd, FLEXONENAND_CMD_PI_UPDATE, die, 0);
  3196. ret = this->wait(mtd, FL_WRITING);
  3197. out:
  3198. this->write_word(ONENAND_CMD_RESET, this->base + ONENAND_REG_COMMAND);
  3199. this->wait(mtd, FL_RESETING);
  3200. if (!ret)
  3201. /* Recalculate device size on boundary change*/
  3202. flexonenand_get_size(mtd);
  3203. return ret;
  3204. }
  3205. /**
  3206. * onenand_chip_probe - [OneNAND Interface] The generic chip probe
  3207. * @param mtd MTD device structure
  3208. *
  3209. * OneNAND detection method:
  3210. * Compare the values from command with ones from register
  3211. */
  3212. static int onenand_chip_probe(struct mtd_info *mtd)
  3213. {
  3214. struct onenand_chip *this = mtd->priv;
  3215. int bram_maf_id, bram_dev_id, maf_id, dev_id;
  3216. int syscfg;
  3217. /* Save system configuration 1 */
  3218. syscfg = this->read_word(this->base + ONENAND_REG_SYS_CFG1);
  3219. /* Clear Sync. Burst Read mode to read BootRAM */
  3220. this->write_word((syscfg & ~ONENAND_SYS_CFG1_SYNC_READ & ~ONENAND_SYS_CFG1_SYNC_WRITE), this->base + ONENAND_REG_SYS_CFG1);
  3221. /* Send the command for reading device ID from BootRAM */
  3222. this->write_word(ONENAND_CMD_READID, this->base + ONENAND_BOOTRAM);
  3223. /* Read manufacturer and device IDs from BootRAM */
  3224. bram_maf_id = this->read_word(this->base + ONENAND_BOOTRAM + 0x0);
  3225. bram_dev_id = this->read_word(this->base + ONENAND_BOOTRAM + 0x2);
  3226. /* Reset OneNAND to read default register values */
  3227. this->write_word(ONENAND_CMD_RESET, this->base + ONENAND_BOOTRAM);
  3228. /* Wait reset */
  3229. this->wait(mtd, FL_RESETING);
  3230. /* Restore system configuration 1 */
  3231. this->write_word(syscfg, this->base + ONENAND_REG_SYS_CFG1);
  3232. /* Check manufacturer ID */
  3233. if (onenand_check_maf(bram_maf_id))
  3234. return -ENXIO;
  3235. /* Read manufacturer and device IDs from Register */
  3236. maf_id = this->read_word(this->base + ONENAND_REG_MANUFACTURER_ID);
  3237. dev_id = this->read_word(this->base + ONENAND_REG_DEVICE_ID);
  3238. /* Check OneNAND device */
  3239. if (maf_id != bram_maf_id || dev_id != bram_dev_id)
  3240. return -ENXIO;
  3241. return 0;
  3242. }
  3243. /**
  3244. * onenand_probe - [OneNAND Interface] Probe the OneNAND device
  3245. * @param mtd MTD device structure
  3246. */
  3247. static int onenand_probe(struct mtd_info *mtd)
  3248. {
  3249. struct onenand_chip *this = mtd->priv;
  3250. int maf_id, dev_id, ver_id;
  3251. int density;
  3252. int ret;
  3253. ret = this->chip_probe(mtd);
  3254. if (ret)
  3255. return ret;
  3256. /* Read manufacturer and device IDs from Register */
  3257. maf_id = this->read_word(this->base + ONENAND_REG_MANUFACTURER_ID);
  3258. dev_id = this->read_word(this->base + ONENAND_REG_DEVICE_ID);
  3259. ver_id = this->read_word(this->base + ONENAND_REG_VERSION_ID);
  3260. this->technology = this->read_word(this->base + ONENAND_REG_TECHNOLOGY);
  3261. /* Flash device information */
  3262. onenand_print_device_info(dev_id, ver_id);
  3263. this->device_id = dev_id;
  3264. this->version_id = ver_id;
  3265. /* Check OneNAND features */
  3266. onenand_check_features(mtd);
  3267. density = onenand_get_density(dev_id);
  3268. if (FLEXONENAND(this)) {
  3269. this->dies = ONENAND_IS_DDP(this) ? 2 : 1;
  3270. /* Maximum possible erase regions */
  3271. mtd->numeraseregions = this->dies << 1;
  3272. mtd->eraseregions = kzalloc(sizeof(struct mtd_erase_region_info)
  3273. * (this->dies << 1), GFP_KERNEL);
  3274. if (!mtd->eraseregions)
  3275. return -ENOMEM;
  3276. }
  3277. /*
  3278. * For Flex-OneNAND, chipsize represents maximum possible device size.
  3279. * mtd->size represents the actual device size.
  3280. */
  3281. this->chipsize = (16 << density) << 20;
  3282. /* OneNAND page size & block size */
  3283. /* The data buffer size is equal to page size */
  3284. mtd->writesize = this->read_word(this->base + ONENAND_REG_DATA_BUFFER_SIZE);
  3285. /* We use the full BufferRAM */
  3286. if (ONENAND_IS_4KB_PAGE(this))
  3287. mtd->writesize <<= 1;
  3288. mtd->oobsize = mtd->writesize >> 5;
  3289. /* Pages per a block are always 64 in OneNAND */
  3290. mtd->erasesize = mtd->writesize << 6;
  3291. /*
  3292. * Flex-OneNAND SLC area has 64 pages per block.
  3293. * Flex-OneNAND MLC area has 128 pages per block.
  3294. * Expose MLC erase size to find erase_shift and page_mask.
  3295. */
  3296. if (FLEXONENAND(this))
  3297. mtd->erasesize <<= 1;
  3298. this->erase_shift = ffs(mtd->erasesize) - 1;
  3299. this->page_shift = ffs(mtd->writesize) - 1;
  3300. this->page_mask = (1 << (this->erase_shift - this->page_shift)) - 1;
  3301. /* Set density mask. it is used for DDP */
  3302. if (ONENAND_IS_DDP(this))
  3303. this->density_mask = this->chipsize >> (this->erase_shift + 1);
  3304. /* It's real page size */
  3305. this->writesize = mtd->writesize;
  3306. /* REVISIT: Multichip handling */
  3307. if (FLEXONENAND(this))
  3308. flexonenand_get_size(mtd);
  3309. else
  3310. mtd->size = this->chipsize;
  3311. /*
  3312. * We emulate the 4KiB page and 256KiB erase block size
  3313. * But oobsize is still 64 bytes.
  3314. * It is only valid if you turn on 2X program support,
  3315. * Otherwise it will be ignored by compiler.
  3316. */
  3317. if (ONENAND_IS_2PLANE(this)) {
  3318. mtd->writesize <<= 1;
  3319. mtd->erasesize <<= 1;
  3320. }
  3321. return 0;
  3322. }
  3323. /**
  3324. * onenand_suspend - [MTD Interface] Suspend the OneNAND flash
  3325. * @param mtd MTD device structure
  3326. */
  3327. static int onenand_suspend(struct mtd_info *mtd)
  3328. {
  3329. return onenand_get_device(mtd, FL_PM_SUSPENDED);
  3330. }
  3331. /**
  3332. * onenand_resume - [MTD Interface] Resume the OneNAND flash
  3333. * @param mtd MTD device structure
  3334. */
  3335. static void onenand_resume(struct mtd_info *mtd)
  3336. {
  3337. struct onenand_chip *this = mtd->priv;
  3338. if (this->state == FL_PM_SUSPENDED)
  3339. onenand_release_device(mtd);
  3340. else
  3341. printk(KERN_ERR "%s: resume() called for the chip which is not "
  3342. "in suspended state\n", __func__);
  3343. }
  3344. /**
  3345. * onenand_scan - [OneNAND Interface] Scan for the OneNAND device
  3346. * @param mtd MTD device structure
  3347. * @param maxchips Number of chips to scan for
  3348. *
  3349. * This fills out all the not initialized function pointers
  3350. * with the defaults.
  3351. * The flash ID is read and the mtd/chip structures are
  3352. * filled with the appropriate values.
  3353. */
  3354. int onenand_scan(struct mtd_info *mtd, int maxchips)
  3355. {
  3356. int i, ret;
  3357. struct onenand_chip *this = mtd->priv;
  3358. if (!this->read_word)
  3359. this->read_word = onenand_readw;
  3360. if (!this->write_word)
  3361. this->write_word = onenand_writew;
  3362. if (!this->command)
  3363. this->command = onenand_command;
  3364. if (!this->wait)
  3365. onenand_setup_wait(mtd);
  3366. if (!this->bbt_wait)
  3367. this->bbt_wait = onenand_bbt_wait;
  3368. if (!this->unlock_all)
  3369. this->unlock_all = onenand_unlock_all;
  3370. if (!this->chip_probe)
  3371. this->chip_probe = onenand_chip_probe;
  3372. if (!this->read_bufferram)
  3373. this->read_bufferram = onenand_read_bufferram;
  3374. if (!this->write_bufferram)
  3375. this->write_bufferram = onenand_write_bufferram;
  3376. if (!this->block_markbad)
  3377. this->block_markbad = onenand_default_block_markbad;
  3378. if (!this->scan_bbt)
  3379. this->scan_bbt = onenand_default_bbt;
  3380. if (onenand_probe(mtd))
  3381. return -ENXIO;
  3382. /* Set Sync. Burst Read after probing */
  3383. if (this->mmcontrol) {
  3384. printk(KERN_INFO "OneNAND Sync. Burst Read support\n");
  3385. this->read_bufferram = onenand_sync_read_bufferram;
  3386. }
  3387. /* Allocate buffers, if necessary */
  3388. if (!this->page_buf) {
  3389. this->page_buf = kzalloc(mtd->writesize, GFP_KERNEL);
  3390. if (!this->page_buf) {
  3391. printk(KERN_ERR "%s: Can't allocate page_buf\n",
  3392. __func__);
  3393. return -ENOMEM;
  3394. }
  3395. #ifdef CONFIG_MTD_ONENAND_VERIFY_WRITE
  3396. this->verify_buf = kzalloc(mtd->writesize, GFP_KERNEL);
  3397. if (!this->verify_buf) {
  3398. kfree(this->page_buf);
  3399. return -ENOMEM;
  3400. }
  3401. #endif
  3402. this->options |= ONENAND_PAGEBUF_ALLOC;
  3403. }
  3404. if (!this->oob_buf) {
  3405. this->oob_buf = kzalloc(mtd->oobsize, GFP_KERNEL);
  3406. if (!this->oob_buf) {
  3407. printk(KERN_ERR "%s: Can't allocate oob_buf\n",
  3408. __func__);
  3409. if (this->options & ONENAND_PAGEBUF_ALLOC) {
  3410. this->options &= ~ONENAND_PAGEBUF_ALLOC;
  3411. kfree(this->page_buf);
  3412. }
  3413. return -ENOMEM;
  3414. }
  3415. this->options |= ONENAND_OOBBUF_ALLOC;
  3416. }
  3417. this->state = FL_READY;
  3418. init_waitqueue_head(&this->wq);
  3419. spin_lock_init(&this->chip_lock);
  3420. /*
  3421. * Allow subpage writes up to oobsize.
  3422. */
  3423. switch (mtd->oobsize) {
  3424. case 128:
  3425. if (FLEXONENAND(this)) {
  3426. this->ecclayout = &flexonenand_oob_128;
  3427. mtd->subpage_sft = 0;
  3428. } else {
  3429. this->ecclayout = &onenand_oob_128;
  3430. mtd->subpage_sft = 2;
  3431. }
  3432. if (ONENAND_IS_NOP_1(this))
  3433. mtd->subpage_sft = 0;
  3434. break;
  3435. case 64:
  3436. this->ecclayout = &onenand_oob_64;
  3437. mtd->subpage_sft = 2;
  3438. break;
  3439. case 32:
  3440. this->ecclayout = &onenand_oob_32;
  3441. mtd->subpage_sft = 1;
  3442. break;
  3443. default:
  3444. printk(KERN_WARNING "%s: No OOB scheme defined for oobsize %d\n",
  3445. __func__, mtd->oobsize);
  3446. mtd->subpage_sft = 0;
  3447. /* To prevent kernel oops */
  3448. this->ecclayout = &onenand_oob_32;
  3449. break;
  3450. }
  3451. this->subpagesize = mtd->writesize >> mtd->subpage_sft;
  3452. /*
  3453. * The number of bytes available for a client to place data into
  3454. * the out of band area
  3455. */
  3456. this->ecclayout->oobavail = 0;
  3457. for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES &&
  3458. this->ecclayout->oobfree[i].length; i++)
  3459. this->ecclayout->oobavail +=
  3460. this->ecclayout->oobfree[i].length;
  3461. mtd->oobavail = this->ecclayout->oobavail;
  3462. mtd->ecclayout = this->ecclayout;
  3463. mtd->ecc_strength = 1;
  3464. /* Fill in remaining MTD driver data */
  3465. mtd->type = ONENAND_IS_MLC(this) ? MTD_MLCNANDFLASH : MTD_NANDFLASH;
  3466. mtd->flags = MTD_CAP_NANDFLASH;
  3467. mtd->_erase = onenand_erase;
  3468. mtd->_point = NULL;
  3469. mtd->_unpoint = NULL;
  3470. mtd->_read = onenand_read;
  3471. mtd->_write = onenand_write;
  3472. mtd->_read_oob = onenand_read_oob;
  3473. mtd->_write_oob = onenand_write_oob;
  3474. mtd->_panic_write = onenand_panic_write;
  3475. #ifdef CONFIG_MTD_ONENAND_OTP
  3476. mtd->_get_fact_prot_info = onenand_get_fact_prot_info;
  3477. mtd->_read_fact_prot_reg = onenand_read_fact_prot_reg;
  3478. mtd->_get_user_prot_info = onenand_get_user_prot_info;
  3479. mtd->_read_user_prot_reg = onenand_read_user_prot_reg;
  3480. mtd->_write_user_prot_reg = onenand_write_user_prot_reg;
  3481. mtd->_lock_user_prot_reg = onenand_lock_user_prot_reg;
  3482. #endif
  3483. mtd->_sync = onenand_sync;
  3484. mtd->_lock = onenand_lock;
  3485. mtd->_unlock = onenand_unlock;
  3486. mtd->_suspend = onenand_suspend;
  3487. mtd->_resume = onenand_resume;
  3488. mtd->_block_isbad = onenand_block_isbad;
  3489. mtd->_block_markbad = onenand_block_markbad;
  3490. mtd->owner = THIS_MODULE;
  3491. mtd->writebufsize = mtd->writesize;
  3492. /* Unlock whole block */
  3493. if (!(this->options & ONENAND_SKIP_INITIAL_UNLOCKING))
  3494. this->unlock_all(mtd);
  3495. ret = this->scan_bbt(mtd);
  3496. if ((!FLEXONENAND(this)) || ret)
  3497. return ret;
  3498. /* Change Flex-OneNAND boundaries if required */
  3499. for (i = 0; i < MAX_DIES; i++)
  3500. flexonenand_set_boundary(mtd, i, flex_bdry[2 * i],
  3501. flex_bdry[(2 * i) + 1]);
  3502. return 0;
  3503. }
  3504. /**
  3505. * onenand_release - [OneNAND Interface] Free resources held by the OneNAND device
  3506. * @param mtd MTD device structure
  3507. */
  3508. void onenand_release(struct mtd_info *mtd)
  3509. {
  3510. struct onenand_chip *this = mtd->priv;
  3511. /* Deregister partitions */
  3512. mtd_device_unregister(mtd);
  3513. /* Free bad block table memory, if allocated */
  3514. if (this->bbm) {
  3515. struct bbm_info *bbm = this->bbm;
  3516. kfree(bbm->bbt);
  3517. kfree(this->bbm);
  3518. }
  3519. /* Buffers allocated by onenand_scan */
  3520. if (this->options & ONENAND_PAGEBUF_ALLOC) {
  3521. kfree(this->page_buf);
  3522. #ifdef CONFIG_MTD_ONENAND_VERIFY_WRITE
  3523. kfree(this->verify_buf);
  3524. #endif
  3525. }
  3526. if (this->options & ONENAND_OOBBUF_ALLOC)
  3527. kfree(this->oob_buf);
  3528. kfree(mtd->eraseregions);
  3529. }
  3530. EXPORT_SYMBOL_GPL(onenand_scan);
  3531. EXPORT_SYMBOL_GPL(onenand_release);
  3532. MODULE_LICENSE("GPL");
  3533. MODULE_AUTHOR("Kyungmin Park <kyungmin.park@samsung.com>");
  3534. MODULE_DESCRIPTION("Generic OneNAND flash driver code");