onenand_base.c 108 KB

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