nouveau_bios.c 170 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326
  1. /*
  2. * Copyright 2005-2006 Erik Waling
  3. * Copyright 2006 Stephane Marchesin
  4. * Copyright 2007-2009 Stuart Bennett
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a
  7. * copy of this software and associated documentation files (the "Software"),
  8. * to deal in the Software without restriction, including without limitation
  9. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  10. * and/or sell copies of the Software, and to permit persons to whom the
  11. * Software is furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  20. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
  21. * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. * SOFTWARE.
  23. */
  24. #include "drmP.h"
  25. #define NV_DEBUG_NOTRACE
  26. #include "nouveau_drv.h"
  27. #include "nouveau_hw.h"
  28. #include "nouveau_encoder.h"
  29. #include <subdev/gpio.h>
  30. #include <linux/io-mapping.h>
  31. #include <linux/firmware.h>
  32. /* these defines are made up */
  33. #define NV_CIO_CRE_44_HEADA 0x0
  34. #define NV_CIO_CRE_44_HEADB 0x3
  35. #define FEATURE_MOBILE 0x10 /* also FEATURE_QUADRO for BMP */
  36. #define EDID1_LEN 128
  37. #define BIOSLOG(sip, fmt, arg...) NV_DEBUG(sip->dev, fmt, ##arg)
  38. #define LOG_OLD_VALUE(x)
  39. struct init_exec {
  40. bool execute;
  41. bool repeat;
  42. };
  43. static bool nv_cksum(const uint8_t *data, unsigned int length)
  44. {
  45. /*
  46. * There's a few checksums in the BIOS, so here's a generic checking
  47. * function.
  48. */
  49. int i;
  50. uint8_t sum = 0;
  51. for (i = 0; i < length; i++)
  52. sum += data[i];
  53. if (sum)
  54. return true;
  55. return false;
  56. }
  57. struct init_tbl_entry {
  58. char *name;
  59. uint8_t id;
  60. /* Return:
  61. * > 0: success, length of opcode
  62. * 0: success, but abort further parsing of table (INIT_DONE etc)
  63. * < 0: failure, table parsing will be aborted
  64. */
  65. int (*handler)(struct nvbios *, uint16_t, struct init_exec *);
  66. };
  67. static int parse_init_table(struct nvbios *, uint16_t, struct init_exec *);
  68. #define MACRO_INDEX_SIZE 2
  69. #define MACRO_SIZE 8
  70. #define CONDITION_SIZE 12
  71. #define IO_FLAG_CONDITION_SIZE 9
  72. #define IO_CONDITION_SIZE 5
  73. #define MEM_INIT_SIZE 66
  74. static void still_alive(void)
  75. {
  76. #if 0
  77. sync();
  78. mdelay(2);
  79. #endif
  80. }
  81. static uint32_t
  82. munge_reg(struct nvbios *bios, uint32_t reg)
  83. {
  84. struct drm_nouveau_private *dev_priv = bios->dev->dev_private;
  85. struct dcb_entry *dcbent = bios->display.output;
  86. if (dev_priv->card_type < NV_50)
  87. return reg;
  88. if (reg & 0x80000000) {
  89. BUG_ON(bios->display.crtc < 0);
  90. reg += bios->display.crtc * 0x800;
  91. }
  92. if (reg & 0x40000000) {
  93. BUG_ON(!dcbent);
  94. reg += (ffs(dcbent->or) - 1) * 0x800;
  95. if ((reg & 0x20000000) && !(dcbent->sorconf.link & 1))
  96. reg += 0x00000080;
  97. }
  98. reg &= ~0xe0000000;
  99. return reg;
  100. }
  101. static int
  102. valid_reg(struct nvbios *bios, uint32_t reg)
  103. {
  104. struct drm_nouveau_private *dev_priv = bios->dev->dev_private;
  105. struct drm_device *dev = bios->dev;
  106. /* C51 has misaligned regs on purpose. Marvellous */
  107. if (reg & 0x2 ||
  108. (reg & 0x1 && dev_priv->vbios.chip_version != 0x51))
  109. NV_ERROR(dev, "======= misaligned reg 0x%08X =======\n", reg);
  110. /* warn on C51 regs that haven't been verified accessible in tracing */
  111. if (reg & 0x1 && dev_priv->vbios.chip_version == 0x51 &&
  112. reg != 0x130d && reg != 0x1311 && reg != 0x60081d)
  113. NV_WARN(dev, "=== C51 misaligned reg 0x%08X not verified ===\n",
  114. reg);
  115. if (reg >= (8*1024*1024)) {
  116. NV_ERROR(dev, "=== reg 0x%08x out of mapped bounds ===\n", reg);
  117. return 0;
  118. }
  119. return 1;
  120. }
  121. static bool
  122. valid_idx_port(struct nvbios *bios, uint16_t port)
  123. {
  124. struct drm_nouveau_private *dev_priv = bios->dev->dev_private;
  125. struct drm_device *dev = bios->dev;
  126. /*
  127. * If adding more ports here, the read/write functions below will need
  128. * updating so that the correct mmio range (PRMCIO, PRMDIO, PRMVIO) is
  129. * used for the port in question
  130. */
  131. if (dev_priv->card_type < NV_50) {
  132. if (port == NV_CIO_CRX__COLOR)
  133. return true;
  134. if (port == NV_VIO_SRX)
  135. return true;
  136. } else {
  137. if (port == NV_CIO_CRX__COLOR)
  138. return true;
  139. }
  140. NV_ERROR(dev, "========== unknown indexed io port 0x%04X ==========\n",
  141. port);
  142. return false;
  143. }
  144. static bool
  145. valid_port(struct nvbios *bios, uint16_t port)
  146. {
  147. struct drm_device *dev = bios->dev;
  148. /*
  149. * If adding more ports here, the read/write functions below will need
  150. * updating so that the correct mmio range (PRMCIO, PRMDIO, PRMVIO) is
  151. * used for the port in question
  152. */
  153. if (port == NV_VIO_VSE2)
  154. return true;
  155. NV_ERROR(dev, "========== unknown io port 0x%04X ==========\n", port);
  156. return false;
  157. }
  158. static uint32_t
  159. bios_rd32(struct nvbios *bios, uint32_t reg)
  160. {
  161. uint32_t data;
  162. reg = munge_reg(bios, reg);
  163. if (!valid_reg(bios, reg))
  164. return 0;
  165. /*
  166. * C51 sometimes uses regs with bit0 set in the address. For these
  167. * cases there should exist a translation in a BIOS table to an IO
  168. * port address which the BIOS uses for accessing the reg
  169. *
  170. * These only seem to appear for the power control regs to a flat panel,
  171. * and the GPIO regs at 0x60081*. In C51 mmio traces the normal regs
  172. * for 0x1308 and 0x1310 are used - hence the mask below. An S3
  173. * suspend-resume mmio trace from a C51 will be required to see if this
  174. * is true for the power microcode in 0x14.., or whether the direct IO
  175. * port access method is needed
  176. */
  177. if (reg & 0x1)
  178. reg &= ~0x1;
  179. data = nv_rd32(bios->dev, reg);
  180. BIOSLOG(bios, " Read: Reg: 0x%08X, Data: 0x%08X\n", reg, data);
  181. return data;
  182. }
  183. static void
  184. bios_wr32(struct nvbios *bios, uint32_t reg, uint32_t data)
  185. {
  186. struct drm_nouveau_private *dev_priv = bios->dev->dev_private;
  187. reg = munge_reg(bios, reg);
  188. if (!valid_reg(bios, reg))
  189. return;
  190. /* see note in bios_rd32 */
  191. if (reg & 0x1)
  192. reg &= 0xfffffffe;
  193. LOG_OLD_VALUE(bios_rd32(bios, reg));
  194. BIOSLOG(bios, " Write: Reg: 0x%08X, Data: 0x%08X\n", reg, data);
  195. if (dev_priv->vbios.execute) {
  196. still_alive();
  197. nv_wr32(bios->dev, reg, data);
  198. }
  199. }
  200. static uint8_t
  201. bios_idxprt_rd(struct nvbios *bios, uint16_t port, uint8_t index)
  202. {
  203. struct drm_nouveau_private *dev_priv = bios->dev->dev_private;
  204. struct drm_device *dev = bios->dev;
  205. uint8_t data;
  206. if (!valid_idx_port(bios, port))
  207. return 0;
  208. if (dev_priv->card_type < NV_50) {
  209. if (port == NV_VIO_SRX)
  210. data = NVReadVgaSeq(dev, bios->state.crtchead, index);
  211. else /* assume NV_CIO_CRX__COLOR */
  212. data = NVReadVgaCrtc(dev, bios->state.crtchead, index);
  213. } else {
  214. uint32_t data32;
  215. data32 = bios_rd32(bios, NV50_PDISPLAY_VGACRTC(index & ~3));
  216. data = (data32 >> ((index & 3) << 3)) & 0xff;
  217. }
  218. BIOSLOG(bios, " Indexed IO read: Port: 0x%04X, Index: 0x%02X, "
  219. "Head: 0x%02X, Data: 0x%02X\n",
  220. port, index, bios->state.crtchead, data);
  221. return data;
  222. }
  223. static void
  224. bios_idxprt_wr(struct nvbios *bios, uint16_t port, uint8_t index, uint8_t data)
  225. {
  226. struct drm_nouveau_private *dev_priv = bios->dev->dev_private;
  227. struct drm_device *dev = bios->dev;
  228. if (!valid_idx_port(bios, port))
  229. return;
  230. /*
  231. * The current head is maintained in the nvbios member state.crtchead.
  232. * We trap changes to CR44 and update the head variable and hence the
  233. * register set written.
  234. * As CR44 only exists on CRTC0, we update crtchead to head0 in advance
  235. * of the write, and to head1 after the write
  236. */
  237. if (port == NV_CIO_CRX__COLOR && index == NV_CIO_CRE_44 &&
  238. data != NV_CIO_CRE_44_HEADB)
  239. bios->state.crtchead = 0;
  240. LOG_OLD_VALUE(bios_idxprt_rd(bios, port, index));
  241. BIOSLOG(bios, " Indexed IO write: Port: 0x%04X, Index: 0x%02X, "
  242. "Head: 0x%02X, Data: 0x%02X\n",
  243. port, index, bios->state.crtchead, data);
  244. if (bios->execute && dev_priv->card_type < NV_50) {
  245. still_alive();
  246. if (port == NV_VIO_SRX)
  247. NVWriteVgaSeq(dev, bios->state.crtchead, index, data);
  248. else /* assume NV_CIO_CRX__COLOR */
  249. NVWriteVgaCrtc(dev, bios->state.crtchead, index, data);
  250. } else
  251. if (bios->execute) {
  252. uint32_t data32, shift = (index & 3) << 3;
  253. still_alive();
  254. data32 = bios_rd32(bios, NV50_PDISPLAY_VGACRTC(index & ~3));
  255. data32 &= ~(0xff << shift);
  256. data32 |= (data << shift);
  257. bios_wr32(bios, NV50_PDISPLAY_VGACRTC(index & ~3), data32);
  258. }
  259. if (port == NV_CIO_CRX__COLOR &&
  260. index == NV_CIO_CRE_44 && data == NV_CIO_CRE_44_HEADB)
  261. bios->state.crtchead = 1;
  262. }
  263. static uint8_t
  264. bios_port_rd(struct nvbios *bios, uint16_t port)
  265. {
  266. uint8_t data, head = bios->state.crtchead;
  267. if (!valid_port(bios, port))
  268. return 0;
  269. data = NVReadPRMVIO(bios->dev, head, NV_PRMVIO0_OFFSET + port);
  270. BIOSLOG(bios, " IO read: Port: 0x%04X, Head: 0x%02X, Data: 0x%02X\n",
  271. port, head, data);
  272. return data;
  273. }
  274. static void
  275. bios_port_wr(struct nvbios *bios, uint16_t port, uint8_t data)
  276. {
  277. int head = bios->state.crtchead;
  278. if (!valid_port(bios, port))
  279. return;
  280. LOG_OLD_VALUE(bios_port_rd(bios, port));
  281. BIOSLOG(bios, " IO write: Port: 0x%04X, Head: 0x%02X, Data: 0x%02X\n",
  282. port, head, data);
  283. if (!bios->execute)
  284. return;
  285. still_alive();
  286. NVWritePRMVIO(bios->dev, head, NV_PRMVIO0_OFFSET + port, data);
  287. }
  288. static bool
  289. io_flag_condition_met(struct nvbios *bios, uint16_t offset, uint8_t cond)
  290. {
  291. /*
  292. * The IO flag condition entry has 2 bytes for the CRTC port; 1 byte
  293. * for the CRTC index; 1 byte for the mask to apply to the value
  294. * retrieved from the CRTC; 1 byte for the shift right to apply to the
  295. * masked CRTC value; 2 bytes for the offset to the flag array, to
  296. * which the shifted value is added; 1 byte for the mask applied to the
  297. * value read from the flag array; and 1 byte for the value to compare
  298. * against the masked byte from the flag table.
  299. */
  300. uint16_t condptr = bios->io_flag_condition_tbl_ptr + cond * IO_FLAG_CONDITION_SIZE;
  301. uint16_t crtcport = ROM16(bios->data[condptr]);
  302. uint8_t crtcindex = bios->data[condptr + 2];
  303. uint8_t mask = bios->data[condptr + 3];
  304. uint8_t shift = bios->data[condptr + 4];
  305. uint16_t flagarray = ROM16(bios->data[condptr + 5]);
  306. uint8_t flagarraymask = bios->data[condptr + 7];
  307. uint8_t cmpval = bios->data[condptr + 8];
  308. uint8_t data;
  309. BIOSLOG(bios, "0x%04X: Port: 0x%04X, Index: 0x%02X, Mask: 0x%02X, "
  310. "Shift: 0x%02X, FlagArray: 0x%04X, FAMask: 0x%02X, "
  311. "Cmpval: 0x%02X\n",
  312. offset, crtcport, crtcindex, mask, shift, flagarray, flagarraymask, cmpval);
  313. data = bios_idxprt_rd(bios, crtcport, crtcindex);
  314. data = bios->data[flagarray + ((data & mask) >> shift)];
  315. data &= flagarraymask;
  316. BIOSLOG(bios, "0x%04X: Checking if 0x%02X equals 0x%02X\n",
  317. offset, data, cmpval);
  318. return (data == cmpval);
  319. }
  320. static bool
  321. bios_condition_met(struct nvbios *bios, uint16_t offset, uint8_t cond)
  322. {
  323. /*
  324. * The condition table entry has 4 bytes for the address of the
  325. * register to check, 4 bytes for a mask to apply to the register and
  326. * 4 for a test comparison value
  327. */
  328. uint16_t condptr = bios->condition_tbl_ptr + cond * CONDITION_SIZE;
  329. uint32_t reg = ROM32(bios->data[condptr]);
  330. uint32_t mask = ROM32(bios->data[condptr + 4]);
  331. uint32_t cmpval = ROM32(bios->data[condptr + 8]);
  332. uint32_t data;
  333. BIOSLOG(bios, "0x%04X: Cond: 0x%02X, Reg: 0x%08X, Mask: 0x%08X\n",
  334. offset, cond, reg, mask);
  335. data = bios_rd32(bios, reg) & mask;
  336. BIOSLOG(bios, "0x%04X: Checking if 0x%08X equals 0x%08X\n",
  337. offset, data, cmpval);
  338. return (data == cmpval);
  339. }
  340. static bool
  341. io_condition_met(struct nvbios *bios, uint16_t offset, uint8_t cond)
  342. {
  343. /*
  344. * The IO condition entry has 2 bytes for the IO port address; 1 byte
  345. * for the index to write to io_port; 1 byte for the mask to apply to
  346. * the byte read from io_port+1; and 1 byte for the value to compare
  347. * against the masked byte.
  348. */
  349. uint16_t condptr = bios->io_condition_tbl_ptr + cond * IO_CONDITION_SIZE;
  350. uint16_t io_port = ROM16(bios->data[condptr]);
  351. uint8_t port_index = bios->data[condptr + 2];
  352. uint8_t mask = bios->data[condptr + 3];
  353. uint8_t cmpval = bios->data[condptr + 4];
  354. uint8_t data = bios_idxprt_rd(bios, io_port, port_index) & mask;
  355. BIOSLOG(bios, "0x%04X: Checking if 0x%02X equals 0x%02X\n",
  356. offset, data, cmpval);
  357. return (data == cmpval);
  358. }
  359. static int
  360. nv50_pll_set(struct drm_device *dev, uint32_t reg, uint32_t clk)
  361. {
  362. struct drm_nouveau_private *dev_priv = dev->dev_private;
  363. struct nouveau_pll_vals pll;
  364. struct pll_lims pll_limits;
  365. u32 ctrl, mask, coef;
  366. int ret;
  367. ret = get_pll_limits(dev, reg, &pll_limits);
  368. if (ret)
  369. return ret;
  370. clk = nouveau_calc_pll_mnp(dev, &pll_limits, clk, &pll);
  371. if (!clk)
  372. return -ERANGE;
  373. coef = pll.N1 << 8 | pll.M1;
  374. ctrl = pll.log2P << 16;
  375. mask = 0x00070000;
  376. if (reg == 0x004008) {
  377. mask |= 0x01f80000;
  378. ctrl |= (pll_limits.log2p_bias << 19);
  379. ctrl |= (pll.log2P << 22);
  380. }
  381. if (!dev_priv->vbios.execute)
  382. return 0;
  383. nv_mask(dev, reg + 0, mask, ctrl);
  384. nv_wr32(dev, reg + 4, coef);
  385. return 0;
  386. }
  387. static int
  388. setPLL(struct nvbios *bios, uint32_t reg, uint32_t clk)
  389. {
  390. struct drm_device *dev = bios->dev;
  391. struct drm_nouveau_private *dev_priv = dev->dev_private;
  392. /* clk in kHz */
  393. struct pll_lims pll_lim;
  394. struct nouveau_pll_vals pllvals;
  395. int ret;
  396. if (dev_priv->card_type >= NV_50)
  397. return nv50_pll_set(dev, reg, clk);
  398. /* high regs (such as in the mac g5 table) are not -= 4 */
  399. ret = get_pll_limits(dev, reg > 0x405c ? reg : reg - 4, &pll_lim);
  400. if (ret)
  401. return ret;
  402. clk = nouveau_calc_pll_mnp(dev, &pll_lim, clk, &pllvals);
  403. if (!clk)
  404. return -ERANGE;
  405. if (bios->execute) {
  406. still_alive();
  407. nouveau_hw_setpll(dev, reg, &pllvals);
  408. }
  409. return 0;
  410. }
  411. static int dcb_entry_idx_from_crtchead(struct drm_device *dev)
  412. {
  413. struct drm_nouveau_private *dev_priv = dev->dev_private;
  414. struct nvbios *bios = &dev_priv->vbios;
  415. /*
  416. * For the results of this function to be correct, CR44 must have been
  417. * set (using bios_idxprt_wr to set crtchead), CR58 set for CR57 = 0,
  418. * and the DCB table parsed, before the script calling the function is
  419. * run. run_digital_op_script is example of how to do such setup
  420. */
  421. uint8_t dcb_entry = NVReadVgaCrtc5758(dev, bios->state.crtchead, 0);
  422. if (dcb_entry > bios->dcb.entries) {
  423. NV_ERROR(dev, "CR58 doesn't have a valid DCB entry currently "
  424. "(%02X)\n", dcb_entry);
  425. dcb_entry = 0x7f; /* unused / invalid marker */
  426. }
  427. return dcb_entry;
  428. }
  429. static struct nouveau_i2c_chan *
  430. init_i2c_device_find(struct drm_device *dev, int i2c_index)
  431. {
  432. if (i2c_index == 0xff) {
  433. struct drm_nouveau_private *dev_priv = dev->dev_private;
  434. struct dcb_table *dcb = &dev_priv->vbios.dcb;
  435. /* note: dcb_entry_idx_from_crtchead needs pre-script set-up */
  436. int idx = dcb_entry_idx_from_crtchead(dev);
  437. i2c_index = NV_I2C_DEFAULT(0);
  438. if (idx != 0x7f && dcb->entry[idx].i2c_upper_default)
  439. i2c_index = NV_I2C_DEFAULT(1);
  440. }
  441. return nouveau_i2c_find(dev, i2c_index);
  442. }
  443. static uint32_t
  444. get_tmds_index_reg(struct drm_device *dev, uint8_t mlv)
  445. {
  446. /*
  447. * For mlv < 0x80, it is an index into a table of TMDS base addresses.
  448. * For mlv == 0x80 use the "or" value of the dcb_entry indexed by
  449. * CR58 for CR57 = 0 to index a table of offsets to the basic
  450. * 0x6808b0 address.
  451. * For mlv == 0x81 use the "or" value of the dcb_entry indexed by
  452. * CR58 for CR57 = 0 to index a table of offsets to the basic
  453. * 0x6808b0 address, and then flip the offset by 8.
  454. */
  455. struct drm_nouveau_private *dev_priv = dev->dev_private;
  456. struct nvbios *bios = &dev_priv->vbios;
  457. const int pramdac_offset[13] = {
  458. 0, 0, 0x8, 0, 0x2000, 0, 0, 0, 0x2008, 0, 0, 0, 0x2000 };
  459. const uint32_t pramdac_table[4] = {
  460. 0x6808b0, 0x6808b8, 0x6828b0, 0x6828b8 };
  461. if (mlv >= 0x80) {
  462. int dcb_entry, dacoffset;
  463. /* note: dcb_entry_idx_from_crtchead needs pre-script set-up */
  464. dcb_entry = dcb_entry_idx_from_crtchead(dev);
  465. if (dcb_entry == 0x7f)
  466. return 0;
  467. dacoffset = pramdac_offset[bios->dcb.entry[dcb_entry].or];
  468. if (mlv == 0x81)
  469. dacoffset ^= 8;
  470. return 0x6808b0 + dacoffset;
  471. } else {
  472. if (mlv >= ARRAY_SIZE(pramdac_table)) {
  473. NV_ERROR(dev, "Magic Lookup Value too big (%02X)\n",
  474. mlv);
  475. return 0;
  476. }
  477. return pramdac_table[mlv];
  478. }
  479. }
  480. static int
  481. init_io_restrict_prog(struct nvbios *bios, uint16_t offset,
  482. struct init_exec *iexec)
  483. {
  484. /*
  485. * INIT_IO_RESTRICT_PROG opcode: 0x32 ('2')
  486. *
  487. * offset (8 bit): opcode
  488. * offset + 1 (16 bit): CRTC port
  489. * offset + 3 (8 bit): CRTC index
  490. * offset + 4 (8 bit): mask
  491. * offset + 5 (8 bit): shift
  492. * offset + 6 (8 bit): count
  493. * offset + 7 (32 bit): register
  494. * offset + 11 (32 bit): configuration 1
  495. * ...
  496. *
  497. * Starting at offset + 11 there are "count" 32 bit values.
  498. * To find out which value to use read index "CRTC index" on "CRTC
  499. * port", AND this value with "mask" and then bit shift right "shift"
  500. * bits. Read the appropriate value using this index and write to
  501. * "register"
  502. */
  503. uint16_t crtcport = ROM16(bios->data[offset + 1]);
  504. uint8_t crtcindex = bios->data[offset + 3];
  505. uint8_t mask = bios->data[offset + 4];
  506. uint8_t shift = bios->data[offset + 5];
  507. uint8_t count = bios->data[offset + 6];
  508. uint32_t reg = ROM32(bios->data[offset + 7]);
  509. uint8_t config;
  510. uint32_t configval;
  511. int len = 11 + count * 4;
  512. if (!iexec->execute)
  513. return len;
  514. BIOSLOG(bios, "0x%04X: Port: 0x%04X, Index: 0x%02X, Mask: 0x%02X, "
  515. "Shift: 0x%02X, Count: 0x%02X, Reg: 0x%08X\n",
  516. offset, crtcport, crtcindex, mask, shift, count, reg);
  517. config = (bios_idxprt_rd(bios, crtcport, crtcindex) & mask) >> shift;
  518. if (config > count) {
  519. NV_ERROR(bios->dev,
  520. "0x%04X: Config 0x%02X exceeds maximal bound 0x%02X\n",
  521. offset, config, count);
  522. return len;
  523. }
  524. configval = ROM32(bios->data[offset + 11 + config * 4]);
  525. BIOSLOG(bios, "0x%04X: Writing config %02X\n", offset, config);
  526. bios_wr32(bios, reg, configval);
  527. return len;
  528. }
  529. static int
  530. init_repeat(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
  531. {
  532. /*
  533. * INIT_REPEAT opcode: 0x33 ('3')
  534. *
  535. * offset (8 bit): opcode
  536. * offset + 1 (8 bit): count
  537. *
  538. * Execute script following this opcode up to INIT_REPEAT_END
  539. * "count" times
  540. */
  541. uint8_t count = bios->data[offset + 1];
  542. uint8_t i;
  543. /* no iexec->execute check by design */
  544. BIOSLOG(bios, "0x%04X: Repeating following segment %d times\n",
  545. offset, count);
  546. iexec->repeat = true;
  547. /*
  548. * count - 1, as the script block will execute once when we leave this
  549. * opcode -- this is compatible with bios behaviour as:
  550. * a) the block is always executed at least once, even if count == 0
  551. * b) the bios interpreter skips to the op following INIT_END_REPEAT,
  552. * while we don't
  553. */
  554. for (i = 0; i < count - 1; i++)
  555. parse_init_table(bios, offset + 2, iexec);
  556. iexec->repeat = false;
  557. return 2;
  558. }
  559. static int
  560. init_io_restrict_pll(struct nvbios *bios, uint16_t offset,
  561. struct init_exec *iexec)
  562. {
  563. /*
  564. * INIT_IO_RESTRICT_PLL opcode: 0x34 ('4')
  565. *
  566. * offset (8 bit): opcode
  567. * offset + 1 (16 bit): CRTC port
  568. * offset + 3 (8 bit): CRTC index
  569. * offset + 4 (8 bit): mask
  570. * offset + 5 (8 bit): shift
  571. * offset + 6 (8 bit): IO flag condition index
  572. * offset + 7 (8 bit): count
  573. * offset + 8 (32 bit): register
  574. * offset + 12 (16 bit): frequency 1
  575. * ...
  576. *
  577. * Starting at offset + 12 there are "count" 16 bit frequencies (10kHz).
  578. * Set PLL register "register" to coefficients for frequency n,
  579. * selected by reading index "CRTC index" of "CRTC port" ANDed with
  580. * "mask" and shifted right by "shift".
  581. *
  582. * If "IO flag condition index" > 0, and condition met, double
  583. * frequency before setting it.
  584. */
  585. uint16_t crtcport = ROM16(bios->data[offset + 1]);
  586. uint8_t crtcindex = bios->data[offset + 3];
  587. uint8_t mask = bios->data[offset + 4];
  588. uint8_t shift = bios->data[offset + 5];
  589. int8_t io_flag_condition_idx = bios->data[offset + 6];
  590. uint8_t count = bios->data[offset + 7];
  591. uint32_t reg = ROM32(bios->data[offset + 8]);
  592. uint8_t config;
  593. uint16_t freq;
  594. int len = 12 + count * 2;
  595. if (!iexec->execute)
  596. return len;
  597. BIOSLOG(bios, "0x%04X: Port: 0x%04X, Index: 0x%02X, Mask: 0x%02X, "
  598. "Shift: 0x%02X, IO Flag Condition: 0x%02X, "
  599. "Count: 0x%02X, Reg: 0x%08X\n",
  600. offset, crtcport, crtcindex, mask, shift,
  601. io_flag_condition_idx, count, reg);
  602. config = (bios_idxprt_rd(bios, crtcport, crtcindex) & mask) >> shift;
  603. if (config > count) {
  604. NV_ERROR(bios->dev,
  605. "0x%04X: Config 0x%02X exceeds maximal bound 0x%02X\n",
  606. offset, config, count);
  607. return len;
  608. }
  609. freq = ROM16(bios->data[offset + 12 + config * 2]);
  610. if (io_flag_condition_idx > 0) {
  611. if (io_flag_condition_met(bios, offset, io_flag_condition_idx)) {
  612. BIOSLOG(bios, "0x%04X: Condition fulfilled -- "
  613. "frequency doubled\n", offset);
  614. freq *= 2;
  615. } else
  616. BIOSLOG(bios, "0x%04X: Condition not fulfilled -- "
  617. "frequency unchanged\n", offset);
  618. }
  619. BIOSLOG(bios, "0x%04X: Reg: 0x%08X, Config: 0x%02X, Freq: %d0kHz\n",
  620. offset, reg, config, freq);
  621. setPLL(bios, reg, freq * 10);
  622. return len;
  623. }
  624. static int
  625. init_end_repeat(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
  626. {
  627. /*
  628. * INIT_END_REPEAT opcode: 0x36 ('6')
  629. *
  630. * offset (8 bit): opcode
  631. *
  632. * Marks the end of the block for INIT_REPEAT to repeat
  633. */
  634. /* no iexec->execute check by design */
  635. /*
  636. * iexec->repeat flag necessary to go past INIT_END_REPEAT opcode when
  637. * we're not in repeat mode
  638. */
  639. if (iexec->repeat)
  640. return 0;
  641. return 1;
  642. }
  643. static int
  644. init_copy(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
  645. {
  646. /*
  647. * INIT_COPY opcode: 0x37 ('7')
  648. *
  649. * offset (8 bit): opcode
  650. * offset + 1 (32 bit): register
  651. * offset + 5 (8 bit): shift
  652. * offset + 6 (8 bit): srcmask
  653. * offset + 7 (16 bit): CRTC port
  654. * offset + 9 (8 bit): CRTC index
  655. * offset + 10 (8 bit): mask
  656. *
  657. * Read index "CRTC index" on "CRTC port", AND with "mask", OR with
  658. * (REGVAL("register") >> "shift" & "srcmask") and write-back to CRTC
  659. * port
  660. */
  661. uint32_t reg = ROM32(bios->data[offset + 1]);
  662. uint8_t shift = bios->data[offset + 5];
  663. uint8_t srcmask = bios->data[offset + 6];
  664. uint16_t crtcport = ROM16(bios->data[offset + 7]);
  665. uint8_t crtcindex = bios->data[offset + 9];
  666. uint8_t mask = bios->data[offset + 10];
  667. uint32_t data;
  668. uint8_t crtcdata;
  669. if (!iexec->execute)
  670. return 11;
  671. BIOSLOG(bios, "0x%04X: Reg: 0x%08X, Shift: 0x%02X, SrcMask: 0x%02X, "
  672. "Port: 0x%04X, Index: 0x%02X, Mask: 0x%02X\n",
  673. offset, reg, shift, srcmask, crtcport, crtcindex, mask);
  674. data = bios_rd32(bios, reg);
  675. if (shift < 0x80)
  676. data >>= shift;
  677. else
  678. data <<= (0x100 - shift);
  679. data &= srcmask;
  680. crtcdata = bios_idxprt_rd(bios, crtcport, crtcindex) & mask;
  681. crtcdata |= (uint8_t)data;
  682. bios_idxprt_wr(bios, crtcport, crtcindex, crtcdata);
  683. return 11;
  684. }
  685. static int
  686. init_not(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
  687. {
  688. /*
  689. * INIT_NOT opcode: 0x38 ('8')
  690. *
  691. * offset (8 bit): opcode
  692. *
  693. * Invert the current execute / no-execute condition (i.e. "else")
  694. */
  695. if (iexec->execute)
  696. BIOSLOG(bios, "0x%04X: ------ Skipping following commands ------\n", offset);
  697. else
  698. BIOSLOG(bios, "0x%04X: ------ Executing following commands ------\n", offset);
  699. iexec->execute = !iexec->execute;
  700. return 1;
  701. }
  702. static int
  703. init_io_flag_condition(struct nvbios *bios, uint16_t offset,
  704. struct init_exec *iexec)
  705. {
  706. /*
  707. * INIT_IO_FLAG_CONDITION opcode: 0x39 ('9')
  708. *
  709. * offset (8 bit): opcode
  710. * offset + 1 (8 bit): condition number
  711. *
  712. * Check condition "condition number" in the IO flag condition table.
  713. * If condition not met skip subsequent opcodes until condition is
  714. * inverted (INIT_NOT), or we hit INIT_RESUME
  715. */
  716. uint8_t cond = bios->data[offset + 1];
  717. if (!iexec->execute)
  718. return 2;
  719. if (io_flag_condition_met(bios, offset, cond))
  720. BIOSLOG(bios, "0x%04X: Condition fulfilled -- continuing to execute\n", offset);
  721. else {
  722. BIOSLOG(bios, "0x%04X: Condition not fulfilled -- skipping following commands\n", offset);
  723. iexec->execute = false;
  724. }
  725. return 2;
  726. }
  727. static int
  728. init_dp_condition(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
  729. {
  730. /*
  731. * INIT_DP_CONDITION opcode: 0x3A ('')
  732. *
  733. * offset (8 bit): opcode
  734. * offset + 1 (8 bit): "sub" opcode
  735. * offset + 2 (8 bit): unknown
  736. *
  737. */
  738. struct dcb_entry *dcb = bios->display.output;
  739. struct drm_device *dev = bios->dev;
  740. uint8_t cond = bios->data[offset + 1];
  741. uint8_t *table, *entry;
  742. BIOSLOG(bios, "0x%04X: subop 0x%02X\n", offset, cond);
  743. if (!iexec->execute)
  744. return 3;
  745. table = nouveau_dp_bios_data(dev, dcb, &entry);
  746. if (!table)
  747. return 3;
  748. switch (cond) {
  749. case 0:
  750. entry = dcb_conn(dev, dcb->connector);
  751. if (!entry || entry[0] != DCB_CONNECTOR_eDP)
  752. iexec->execute = false;
  753. break;
  754. case 1:
  755. case 2:
  756. if ((table[0] < 0x40 && !(entry[5] & cond)) ||
  757. (table[0] == 0x40 && !(entry[4] & cond)))
  758. iexec->execute = false;
  759. break;
  760. case 5:
  761. {
  762. struct nouveau_i2c_chan *auxch;
  763. int ret;
  764. auxch = nouveau_i2c_find(dev, bios->display.output->i2c_index);
  765. if (!auxch) {
  766. NV_ERROR(dev, "0x%04X: couldn't get auxch\n", offset);
  767. return 3;
  768. }
  769. ret = nouveau_dp_auxch(auxch, 9, 0xd, &cond, 1);
  770. if (ret) {
  771. NV_ERROR(dev, "0x%04X: auxch rd fail: %d\n", offset, ret);
  772. return 3;
  773. }
  774. if (!(cond & 1))
  775. iexec->execute = false;
  776. }
  777. break;
  778. default:
  779. NV_WARN(dev, "0x%04X: unknown INIT_3A op: %d\n", offset, cond);
  780. break;
  781. }
  782. if (iexec->execute)
  783. BIOSLOG(bios, "0x%04X: continuing to execute\n", offset);
  784. else
  785. BIOSLOG(bios, "0x%04X: skipping following commands\n", offset);
  786. return 3;
  787. }
  788. static int
  789. init_op_3b(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
  790. {
  791. /*
  792. * INIT_3B opcode: 0x3B ('')
  793. *
  794. * offset (8 bit): opcode
  795. * offset + 1 (8 bit): crtc index
  796. *
  797. */
  798. uint8_t or = ffs(bios->display.output->or) - 1;
  799. uint8_t index = bios->data[offset + 1];
  800. uint8_t data;
  801. if (!iexec->execute)
  802. return 2;
  803. data = bios_idxprt_rd(bios, 0x3d4, index);
  804. bios_idxprt_wr(bios, 0x3d4, index, data & ~(1 << or));
  805. return 2;
  806. }
  807. static int
  808. init_op_3c(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
  809. {
  810. /*
  811. * INIT_3C opcode: 0x3C ('')
  812. *
  813. * offset (8 bit): opcode
  814. * offset + 1 (8 bit): crtc index
  815. *
  816. */
  817. uint8_t or = ffs(bios->display.output->or) - 1;
  818. uint8_t index = bios->data[offset + 1];
  819. uint8_t data;
  820. if (!iexec->execute)
  821. return 2;
  822. data = bios_idxprt_rd(bios, 0x3d4, index);
  823. bios_idxprt_wr(bios, 0x3d4, index, data | (1 << or));
  824. return 2;
  825. }
  826. static int
  827. init_idx_addr_latched(struct nvbios *bios, uint16_t offset,
  828. struct init_exec *iexec)
  829. {
  830. /*
  831. * INIT_INDEX_ADDRESS_LATCHED opcode: 0x49 ('I')
  832. *
  833. * offset (8 bit): opcode
  834. * offset + 1 (32 bit): control register
  835. * offset + 5 (32 bit): data register
  836. * offset + 9 (32 bit): mask
  837. * offset + 13 (32 bit): data
  838. * offset + 17 (8 bit): count
  839. * offset + 18 (8 bit): address 1
  840. * offset + 19 (8 bit): data 1
  841. * ...
  842. *
  843. * For each of "count" address and data pairs, write "data n" to
  844. * "data register", read the current value of "control register",
  845. * and write it back once ANDed with "mask", ORed with "data",
  846. * and ORed with "address n"
  847. */
  848. uint32_t controlreg = ROM32(bios->data[offset + 1]);
  849. uint32_t datareg = ROM32(bios->data[offset + 5]);
  850. uint32_t mask = ROM32(bios->data[offset + 9]);
  851. uint32_t data = ROM32(bios->data[offset + 13]);
  852. uint8_t count = bios->data[offset + 17];
  853. int len = 18 + count * 2;
  854. uint32_t value;
  855. int i;
  856. if (!iexec->execute)
  857. return len;
  858. BIOSLOG(bios, "0x%04X: ControlReg: 0x%08X, DataReg: 0x%08X, "
  859. "Mask: 0x%08X, Data: 0x%08X, Count: 0x%02X\n",
  860. offset, controlreg, datareg, mask, data, count);
  861. for (i = 0; i < count; i++) {
  862. uint8_t instaddress = bios->data[offset + 18 + i * 2];
  863. uint8_t instdata = bios->data[offset + 19 + i * 2];
  864. BIOSLOG(bios, "0x%04X: Address: 0x%02X, Data: 0x%02X\n",
  865. offset, instaddress, instdata);
  866. bios_wr32(bios, datareg, instdata);
  867. value = bios_rd32(bios, controlreg) & mask;
  868. value |= data;
  869. value |= instaddress;
  870. bios_wr32(bios, controlreg, value);
  871. }
  872. return len;
  873. }
  874. static int
  875. init_io_restrict_pll2(struct nvbios *bios, uint16_t offset,
  876. struct init_exec *iexec)
  877. {
  878. /*
  879. * INIT_IO_RESTRICT_PLL2 opcode: 0x4A ('J')
  880. *
  881. * offset (8 bit): opcode
  882. * offset + 1 (16 bit): CRTC port
  883. * offset + 3 (8 bit): CRTC index
  884. * offset + 4 (8 bit): mask
  885. * offset + 5 (8 bit): shift
  886. * offset + 6 (8 bit): count
  887. * offset + 7 (32 bit): register
  888. * offset + 11 (32 bit): frequency 1
  889. * ...
  890. *
  891. * Starting at offset + 11 there are "count" 32 bit frequencies (kHz).
  892. * Set PLL register "register" to coefficients for frequency n,
  893. * selected by reading index "CRTC index" of "CRTC port" ANDed with
  894. * "mask" and shifted right by "shift".
  895. */
  896. uint16_t crtcport = ROM16(bios->data[offset + 1]);
  897. uint8_t crtcindex = bios->data[offset + 3];
  898. uint8_t mask = bios->data[offset + 4];
  899. uint8_t shift = bios->data[offset + 5];
  900. uint8_t count = bios->data[offset + 6];
  901. uint32_t reg = ROM32(bios->data[offset + 7]);
  902. int len = 11 + count * 4;
  903. uint8_t config;
  904. uint32_t freq;
  905. if (!iexec->execute)
  906. return len;
  907. BIOSLOG(bios, "0x%04X: Port: 0x%04X, Index: 0x%02X, Mask: 0x%02X, "
  908. "Shift: 0x%02X, Count: 0x%02X, Reg: 0x%08X\n",
  909. offset, crtcport, crtcindex, mask, shift, count, reg);
  910. if (!reg)
  911. return len;
  912. config = (bios_idxprt_rd(bios, crtcport, crtcindex) & mask) >> shift;
  913. if (config > count) {
  914. NV_ERROR(bios->dev,
  915. "0x%04X: Config 0x%02X exceeds maximal bound 0x%02X\n",
  916. offset, config, count);
  917. return len;
  918. }
  919. freq = ROM32(bios->data[offset + 11 + config * 4]);
  920. BIOSLOG(bios, "0x%04X: Reg: 0x%08X, Config: 0x%02X, Freq: %dkHz\n",
  921. offset, reg, config, freq);
  922. setPLL(bios, reg, freq);
  923. return len;
  924. }
  925. static int
  926. init_pll2(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
  927. {
  928. /*
  929. * INIT_PLL2 opcode: 0x4B ('K')
  930. *
  931. * offset (8 bit): opcode
  932. * offset + 1 (32 bit): register
  933. * offset + 5 (32 bit): freq
  934. *
  935. * Set PLL register "register" to coefficients for frequency "freq"
  936. */
  937. uint32_t reg = ROM32(bios->data[offset + 1]);
  938. uint32_t freq = ROM32(bios->data[offset + 5]);
  939. if (!iexec->execute)
  940. return 9;
  941. BIOSLOG(bios, "0x%04X: Reg: 0x%04X, Freq: %dkHz\n",
  942. offset, reg, freq);
  943. setPLL(bios, reg, freq);
  944. return 9;
  945. }
  946. static int
  947. init_i2c_byte(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
  948. {
  949. /*
  950. * INIT_I2C_BYTE opcode: 0x4C ('L')
  951. *
  952. * offset (8 bit): opcode
  953. * offset + 1 (8 bit): DCB I2C table entry index
  954. * offset + 2 (8 bit): I2C slave address
  955. * offset + 3 (8 bit): count
  956. * offset + 4 (8 bit): I2C register 1
  957. * offset + 5 (8 bit): mask 1
  958. * offset + 6 (8 bit): data 1
  959. * ...
  960. *
  961. * For each of "count" registers given by "I2C register n" on the device
  962. * addressed by "I2C slave address" on the I2C bus given by
  963. * "DCB I2C table entry index", read the register, AND the result with
  964. * "mask n" and OR it with "data n" before writing it back to the device
  965. */
  966. struct drm_device *dev = bios->dev;
  967. uint8_t i2c_index = bios->data[offset + 1];
  968. uint8_t i2c_address = bios->data[offset + 2] >> 1;
  969. uint8_t count = bios->data[offset + 3];
  970. struct nouveau_i2c_chan *chan;
  971. int len = 4 + count * 3;
  972. int ret, i;
  973. if (!iexec->execute)
  974. return len;
  975. BIOSLOG(bios, "0x%04X: DCBI2CIndex: 0x%02X, I2CAddress: 0x%02X, "
  976. "Count: 0x%02X\n",
  977. offset, i2c_index, i2c_address, count);
  978. chan = init_i2c_device_find(dev, i2c_index);
  979. if (!chan) {
  980. NV_ERROR(dev, "0x%04X: i2c bus not found\n", offset);
  981. return len;
  982. }
  983. for (i = 0; i < count; i++) {
  984. uint8_t reg = bios->data[offset + 4 + i * 3];
  985. uint8_t mask = bios->data[offset + 5 + i * 3];
  986. uint8_t data = bios->data[offset + 6 + i * 3];
  987. union i2c_smbus_data val;
  988. ret = i2c_smbus_xfer(&chan->adapter, i2c_address, 0,
  989. I2C_SMBUS_READ, reg,
  990. I2C_SMBUS_BYTE_DATA, &val);
  991. if (ret < 0) {
  992. NV_ERROR(dev, "0x%04X: i2c rd fail: %d\n", offset, ret);
  993. return len;
  994. }
  995. BIOSLOG(bios, "0x%04X: I2CReg: 0x%02X, Value: 0x%02X, "
  996. "Mask: 0x%02X, Data: 0x%02X\n",
  997. offset, reg, val.byte, mask, data);
  998. if (!bios->execute)
  999. continue;
  1000. val.byte &= mask;
  1001. val.byte |= data;
  1002. ret = i2c_smbus_xfer(&chan->adapter, i2c_address, 0,
  1003. I2C_SMBUS_WRITE, reg,
  1004. I2C_SMBUS_BYTE_DATA, &val);
  1005. if (ret < 0) {
  1006. NV_ERROR(dev, "0x%04X: i2c wr fail: %d\n", offset, ret);
  1007. return len;
  1008. }
  1009. }
  1010. return len;
  1011. }
  1012. static int
  1013. init_zm_i2c_byte(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
  1014. {
  1015. /*
  1016. * INIT_ZM_I2C_BYTE opcode: 0x4D ('M')
  1017. *
  1018. * offset (8 bit): opcode
  1019. * offset + 1 (8 bit): DCB I2C table entry index
  1020. * offset + 2 (8 bit): I2C slave address
  1021. * offset + 3 (8 bit): count
  1022. * offset + 4 (8 bit): I2C register 1
  1023. * offset + 5 (8 bit): data 1
  1024. * ...
  1025. *
  1026. * For each of "count" registers given by "I2C register n" on the device
  1027. * addressed by "I2C slave address" on the I2C bus given by
  1028. * "DCB I2C table entry index", set the register to "data n"
  1029. */
  1030. struct drm_device *dev = bios->dev;
  1031. uint8_t i2c_index = bios->data[offset + 1];
  1032. uint8_t i2c_address = bios->data[offset + 2] >> 1;
  1033. uint8_t count = bios->data[offset + 3];
  1034. struct nouveau_i2c_chan *chan;
  1035. int len = 4 + count * 2;
  1036. int ret, i;
  1037. if (!iexec->execute)
  1038. return len;
  1039. BIOSLOG(bios, "0x%04X: DCBI2CIndex: 0x%02X, I2CAddress: 0x%02X, "
  1040. "Count: 0x%02X\n",
  1041. offset, i2c_index, i2c_address, count);
  1042. chan = init_i2c_device_find(dev, i2c_index);
  1043. if (!chan) {
  1044. NV_ERROR(dev, "0x%04X: i2c bus not found\n", offset);
  1045. return len;
  1046. }
  1047. for (i = 0; i < count; i++) {
  1048. uint8_t reg = bios->data[offset + 4 + i * 2];
  1049. union i2c_smbus_data val;
  1050. val.byte = bios->data[offset + 5 + i * 2];
  1051. BIOSLOG(bios, "0x%04X: I2CReg: 0x%02X, Data: 0x%02X\n",
  1052. offset, reg, val.byte);
  1053. if (!bios->execute)
  1054. continue;
  1055. ret = i2c_smbus_xfer(&chan->adapter, i2c_address, 0,
  1056. I2C_SMBUS_WRITE, reg,
  1057. I2C_SMBUS_BYTE_DATA, &val);
  1058. if (ret < 0) {
  1059. NV_ERROR(dev, "0x%04X: i2c wr fail: %d\n", offset, ret);
  1060. return len;
  1061. }
  1062. }
  1063. return len;
  1064. }
  1065. static int
  1066. init_zm_i2c(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
  1067. {
  1068. /*
  1069. * INIT_ZM_I2C opcode: 0x4E ('N')
  1070. *
  1071. * offset (8 bit): opcode
  1072. * offset + 1 (8 bit): DCB I2C table entry index
  1073. * offset + 2 (8 bit): I2C slave address
  1074. * offset + 3 (8 bit): count
  1075. * offset + 4 (8 bit): data 1
  1076. * ...
  1077. *
  1078. * Send "count" bytes ("data n") to the device addressed by "I2C slave
  1079. * address" on the I2C bus given by "DCB I2C table entry index"
  1080. */
  1081. struct drm_device *dev = bios->dev;
  1082. uint8_t i2c_index = bios->data[offset + 1];
  1083. uint8_t i2c_address = bios->data[offset + 2] >> 1;
  1084. uint8_t count = bios->data[offset + 3];
  1085. int len = 4 + count;
  1086. struct nouveau_i2c_chan *chan;
  1087. struct i2c_msg msg;
  1088. uint8_t data[256];
  1089. int ret, i;
  1090. if (!iexec->execute)
  1091. return len;
  1092. BIOSLOG(bios, "0x%04X: DCBI2CIndex: 0x%02X, I2CAddress: 0x%02X, "
  1093. "Count: 0x%02X\n",
  1094. offset, i2c_index, i2c_address, count);
  1095. chan = init_i2c_device_find(dev, i2c_index);
  1096. if (!chan) {
  1097. NV_ERROR(dev, "0x%04X: i2c bus not found\n", offset);
  1098. return len;
  1099. }
  1100. for (i = 0; i < count; i++) {
  1101. data[i] = bios->data[offset + 4 + i];
  1102. BIOSLOG(bios, "0x%04X: Data: 0x%02X\n", offset, data[i]);
  1103. }
  1104. if (bios->execute) {
  1105. msg.addr = i2c_address;
  1106. msg.flags = 0;
  1107. msg.len = count;
  1108. msg.buf = data;
  1109. ret = i2c_transfer(&chan->adapter, &msg, 1);
  1110. if (ret != 1) {
  1111. NV_ERROR(dev, "0x%04X: i2c wr fail: %d\n", offset, ret);
  1112. return len;
  1113. }
  1114. }
  1115. return len;
  1116. }
  1117. static int
  1118. init_tmds(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
  1119. {
  1120. /*
  1121. * INIT_TMDS opcode: 0x4F ('O') (non-canon name)
  1122. *
  1123. * offset (8 bit): opcode
  1124. * offset + 1 (8 bit): magic lookup value
  1125. * offset + 2 (8 bit): TMDS address
  1126. * offset + 3 (8 bit): mask
  1127. * offset + 4 (8 bit): data
  1128. *
  1129. * Read the data reg for TMDS address "TMDS address", AND it with mask
  1130. * and OR it with data, then write it back
  1131. * "magic lookup value" determines which TMDS base address register is
  1132. * used -- see get_tmds_index_reg()
  1133. */
  1134. struct drm_device *dev = bios->dev;
  1135. uint8_t mlv = bios->data[offset + 1];
  1136. uint32_t tmdsaddr = bios->data[offset + 2];
  1137. uint8_t mask = bios->data[offset + 3];
  1138. uint8_t data = bios->data[offset + 4];
  1139. uint32_t reg, value;
  1140. if (!iexec->execute)
  1141. return 5;
  1142. BIOSLOG(bios, "0x%04X: MagicLookupValue: 0x%02X, TMDSAddr: 0x%02X, "
  1143. "Mask: 0x%02X, Data: 0x%02X\n",
  1144. offset, mlv, tmdsaddr, mask, data);
  1145. reg = get_tmds_index_reg(bios->dev, mlv);
  1146. if (!reg) {
  1147. NV_ERROR(dev, "0x%04X: no tmds_index_reg\n", offset);
  1148. return 5;
  1149. }
  1150. bios_wr32(bios, reg,
  1151. tmdsaddr | NV_PRAMDAC_FP_TMDS_CONTROL_WRITE_DISABLE);
  1152. value = (bios_rd32(bios, reg + 4) & mask) | data;
  1153. bios_wr32(bios, reg + 4, value);
  1154. bios_wr32(bios, reg, tmdsaddr);
  1155. return 5;
  1156. }
  1157. static int
  1158. init_zm_tmds_group(struct nvbios *bios, uint16_t offset,
  1159. struct init_exec *iexec)
  1160. {
  1161. /*
  1162. * INIT_ZM_TMDS_GROUP opcode: 0x50 ('P') (non-canon name)
  1163. *
  1164. * offset (8 bit): opcode
  1165. * offset + 1 (8 bit): magic lookup value
  1166. * offset + 2 (8 bit): count
  1167. * offset + 3 (8 bit): addr 1
  1168. * offset + 4 (8 bit): data 1
  1169. * ...
  1170. *
  1171. * For each of "count" TMDS address and data pairs write "data n" to
  1172. * "addr n". "magic lookup value" determines which TMDS base address
  1173. * register is used -- see get_tmds_index_reg()
  1174. */
  1175. struct drm_device *dev = bios->dev;
  1176. uint8_t mlv = bios->data[offset + 1];
  1177. uint8_t count = bios->data[offset + 2];
  1178. int len = 3 + count * 2;
  1179. uint32_t reg;
  1180. int i;
  1181. if (!iexec->execute)
  1182. return len;
  1183. BIOSLOG(bios, "0x%04X: MagicLookupValue: 0x%02X, Count: 0x%02X\n",
  1184. offset, mlv, count);
  1185. reg = get_tmds_index_reg(bios->dev, mlv);
  1186. if (!reg) {
  1187. NV_ERROR(dev, "0x%04X: no tmds_index_reg\n", offset);
  1188. return len;
  1189. }
  1190. for (i = 0; i < count; i++) {
  1191. uint8_t tmdsaddr = bios->data[offset + 3 + i * 2];
  1192. uint8_t tmdsdata = bios->data[offset + 4 + i * 2];
  1193. bios_wr32(bios, reg + 4, tmdsdata);
  1194. bios_wr32(bios, reg, tmdsaddr);
  1195. }
  1196. return len;
  1197. }
  1198. static int
  1199. init_cr_idx_adr_latch(struct nvbios *bios, uint16_t offset,
  1200. struct init_exec *iexec)
  1201. {
  1202. /*
  1203. * INIT_CR_INDEX_ADDRESS_LATCHED opcode: 0x51 ('Q')
  1204. *
  1205. * offset (8 bit): opcode
  1206. * offset + 1 (8 bit): CRTC index1
  1207. * offset + 2 (8 bit): CRTC index2
  1208. * offset + 3 (8 bit): baseaddr
  1209. * offset + 4 (8 bit): count
  1210. * offset + 5 (8 bit): data 1
  1211. * ...
  1212. *
  1213. * For each of "count" address and data pairs, write "baseaddr + n" to
  1214. * "CRTC index1" and "data n" to "CRTC index2"
  1215. * Once complete, restore initial value read from "CRTC index1"
  1216. */
  1217. uint8_t crtcindex1 = bios->data[offset + 1];
  1218. uint8_t crtcindex2 = bios->data[offset + 2];
  1219. uint8_t baseaddr = bios->data[offset + 3];
  1220. uint8_t count = bios->data[offset + 4];
  1221. int len = 5 + count;
  1222. uint8_t oldaddr, data;
  1223. int i;
  1224. if (!iexec->execute)
  1225. return len;
  1226. BIOSLOG(bios, "0x%04X: Index1: 0x%02X, Index2: 0x%02X, "
  1227. "BaseAddr: 0x%02X, Count: 0x%02X\n",
  1228. offset, crtcindex1, crtcindex2, baseaddr, count);
  1229. oldaddr = bios_idxprt_rd(bios, NV_CIO_CRX__COLOR, crtcindex1);
  1230. for (i = 0; i < count; i++) {
  1231. bios_idxprt_wr(bios, NV_CIO_CRX__COLOR, crtcindex1,
  1232. baseaddr + i);
  1233. data = bios->data[offset + 5 + i];
  1234. bios_idxprt_wr(bios, NV_CIO_CRX__COLOR, crtcindex2, data);
  1235. }
  1236. bios_idxprt_wr(bios, NV_CIO_CRX__COLOR, crtcindex1, oldaddr);
  1237. return len;
  1238. }
  1239. static int
  1240. init_cr(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
  1241. {
  1242. /*
  1243. * INIT_CR opcode: 0x52 ('R')
  1244. *
  1245. * offset (8 bit): opcode
  1246. * offset + 1 (8 bit): CRTC index
  1247. * offset + 2 (8 bit): mask
  1248. * offset + 3 (8 bit): data
  1249. *
  1250. * Assign the value of at "CRTC index" ANDed with mask and ORed with
  1251. * data back to "CRTC index"
  1252. */
  1253. uint8_t crtcindex = bios->data[offset + 1];
  1254. uint8_t mask = bios->data[offset + 2];
  1255. uint8_t data = bios->data[offset + 3];
  1256. uint8_t value;
  1257. if (!iexec->execute)
  1258. return 4;
  1259. BIOSLOG(bios, "0x%04X: Index: 0x%02X, Mask: 0x%02X, Data: 0x%02X\n",
  1260. offset, crtcindex, mask, data);
  1261. value = bios_idxprt_rd(bios, NV_CIO_CRX__COLOR, crtcindex) & mask;
  1262. value |= data;
  1263. bios_idxprt_wr(bios, NV_CIO_CRX__COLOR, crtcindex, value);
  1264. return 4;
  1265. }
  1266. static int
  1267. init_zm_cr(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
  1268. {
  1269. /*
  1270. * INIT_ZM_CR opcode: 0x53 ('S')
  1271. *
  1272. * offset (8 bit): opcode
  1273. * offset + 1 (8 bit): CRTC index
  1274. * offset + 2 (8 bit): value
  1275. *
  1276. * Assign "value" to CRTC register with index "CRTC index".
  1277. */
  1278. uint8_t crtcindex = ROM32(bios->data[offset + 1]);
  1279. uint8_t data = bios->data[offset + 2];
  1280. if (!iexec->execute)
  1281. return 3;
  1282. bios_idxprt_wr(bios, NV_CIO_CRX__COLOR, crtcindex, data);
  1283. return 3;
  1284. }
  1285. static int
  1286. init_zm_cr_group(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
  1287. {
  1288. /*
  1289. * INIT_ZM_CR_GROUP opcode: 0x54 ('T')
  1290. *
  1291. * offset (8 bit): opcode
  1292. * offset + 1 (8 bit): count
  1293. * offset + 2 (8 bit): CRTC index 1
  1294. * offset + 3 (8 bit): value 1
  1295. * ...
  1296. *
  1297. * For "count", assign "value n" to CRTC register with index
  1298. * "CRTC index n".
  1299. */
  1300. uint8_t count = bios->data[offset + 1];
  1301. int len = 2 + count * 2;
  1302. int i;
  1303. if (!iexec->execute)
  1304. return len;
  1305. for (i = 0; i < count; i++)
  1306. init_zm_cr(bios, offset + 2 + 2 * i - 1, iexec);
  1307. return len;
  1308. }
  1309. static int
  1310. init_condition_time(struct nvbios *bios, uint16_t offset,
  1311. struct init_exec *iexec)
  1312. {
  1313. /*
  1314. * INIT_CONDITION_TIME opcode: 0x56 ('V')
  1315. *
  1316. * offset (8 bit): opcode
  1317. * offset + 1 (8 bit): condition number
  1318. * offset + 2 (8 bit): retries / 50
  1319. *
  1320. * Check condition "condition number" in the condition table.
  1321. * Bios code then sleeps for 2ms if the condition is not met, and
  1322. * repeats up to "retries" times, but on one C51 this has proved
  1323. * insufficient. In mmiotraces the driver sleeps for 20ms, so we do
  1324. * this, and bail after "retries" times, or 2s, whichever is less.
  1325. * If still not met after retries, clear execution flag for this table.
  1326. */
  1327. uint8_t cond = bios->data[offset + 1];
  1328. uint16_t retries = bios->data[offset + 2] * 50;
  1329. unsigned cnt;
  1330. if (!iexec->execute)
  1331. return 3;
  1332. if (retries > 100)
  1333. retries = 100;
  1334. BIOSLOG(bios, "0x%04X: Condition: 0x%02X, Retries: 0x%02X\n",
  1335. offset, cond, retries);
  1336. if (!bios->execute) /* avoid 2s delays when "faking" execution */
  1337. retries = 1;
  1338. for (cnt = 0; cnt < retries; cnt++) {
  1339. if (bios_condition_met(bios, offset, cond)) {
  1340. BIOSLOG(bios, "0x%04X: Condition met, continuing\n",
  1341. offset);
  1342. break;
  1343. } else {
  1344. BIOSLOG(bios, "0x%04X: "
  1345. "Condition not met, sleeping for 20ms\n",
  1346. offset);
  1347. mdelay(20);
  1348. }
  1349. }
  1350. if (!bios_condition_met(bios, offset, cond)) {
  1351. NV_WARN(bios->dev,
  1352. "0x%04X: Condition still not met after %dms, "
  1353. "skipping following opcodes\n", offset, 20 * retries);
  1354. iexec->execute = false;
  1355. }
  1356. return 3;
  1357. }
  1358. static int
  1359. init_ltime(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
  1360. {
  1361. /*
  1362. * INIT_LTIME opcode: 0x57 ('V')
  1363. *
  1364. * offset (8 bit): opcode
  1365. * offset + 1 (16 bit): time
  1366. *
  1367. * Sleep for "time" milliseconds.
  1368. */
  1369. unsigned time = ROM16(bios->data[offset + 1]);
  1370. if (!iexec->execute)
  1371. return 3;
  1372. BIOSLOG(bios, "0x%04X: Sleeping for 0x%04X milliseconds\n",
  1373. offset, time);
  1374. mdelay(time);
  1375. return 3;
  1376. }
  1377. static int
  1378. init_zm_reg_sequence(struct nvbios *bios, uint16_t offset,
  1379. struct init_exec *iexec)
  1380. {
  1381. /*
  1382. * INIT_ZM_REG_SEQUENCE opcode: 0x58 ('X')
  1383. *
  1384. * offset (8 bit): opcode
  1385. * offset + 1 (32 bit): base register
  1386. * offset + 5 (8 bit): count
  1387. * offset + 6 (32 bit): value 1
  1388. * ...
  1389. *
  1390. * Starting at offset + 6 there are "count" 32 bit values.
  1391. * For "count" iterations set "base register" + 4 * current_iteration
  1392. * to "value current_iteration"
  1393. */
  1394. uint32_t basereg = ROM32(bios->data[offset + 1]);
  1395. uint32_t count = bios->data[offset + 5];
  1396. int len = 6 + count * 4;
  1397. int i;
  1398. if (!iexec->execute)
  1399. return len;
  1400. BIOSLOG(bios, "0x%04X: BaseReg: 0x%08X, Count: 0x%02X\n",
  1401. offset, basereg, count);
  1402. for (i = 0; i < count; i++) {
  1403. uint32_t reg = basereg + i * 4;
  1404. uint32_t data = ROM32(bios->data[offset + 6 + i * 4]);
  1405. bios_wr32(bios, reg, data);
  1406. }
  1407. return len;
  1408. }
  1409. static int
  1410. init_sub_direct(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
  1411. {
  1412. /*
  1413. * INIT_SUB_DIRECT opcode: 0x5B ('[')
  1414. *
  1415. * offset (8 bit): opcode
  1416. * offset + 1 (16 bit): subroutine offset (in bios)
  1417. *
  1418. * Calls a subroutine that will execute commands until INIT_DONE
  1419. * is found.
  1420. */
  1421. uint16_t sub_offset = ROM16(bios->data[offset + 1]);
  1422. if (!iexec->execute)
  1423. return 3;
  1424. BIOSLOG(bios, "0x%04X: Executing subroutine at 0x%04X\n",
  1425. offset, sub_offset);
  1426. parse_init_table(bios, sub_offset, iexec);
  1427. BIOSLOG(bios, "0x%04X: End of 0x%04X subroutine\n", offset, sub_offset);
  1428. return 3;
  1429. }
  1430. static int
  1431. init_jump(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
  1432. {
  1433. /*
  1434. * INIT_JUMP opcode: 0x5C ('\')
  1435. *
  1436. * offset (8 bit): opcode
  1437. * offset + 1 (16 bit): offset (in bios)
  1438. *
  1439. * Continue execution of init table from 'offset'
  1440. */
  1441. uint16_t jmp_offset = ROM16(bios->data[offset + 1]);
  1442. if (!iexec->execute)
  1443. return 3;
  1444. BIOSLOG(bios, "0x%04X: Jump to 0x%04X\n", offset, jmp_offset);
  1445. return jmp_offset - offset;
  1446. }
  1447. static int
  1448. init_i2c_if(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
  1449. {
  1450. /*
  1451. * INIT_I2C_IF opcode: 0x5E ('^')
  1452. *
  1453. * offset (8 bit): opcode
  1454. * offset + 1 (8 bit): DCB I2C table entry index
  1455. * offset + 2 (8 bit): I2C slave address
  1456. * offset + 3 (8 bit): I2C register
  1457. * offset + 4 (8 bit): mask
  1458. * offset + 5 (8 bit): data
  1459. *
  1460. * Read the register given by "I2C register" on the device addressed
  1461. * by "I2C slave address" on the I2C bus given by "DCB I2C table
  1462. * entry index". Compare the result AND "mask" to "data".
  1463. * If they're not equal, skip subsequent opcodes until condition is
  1464. * inverted (INIT_NOT), or we hit INIT_RESUME
  1465. */
  1466. uint8_t i2c_index = bios->data[offset + 1];
  1467. uint8_t i2c_address = bios->data[offset + 2] >> 1;
  1468. uint8_t reg = bios->data[offset + 3];
  1469. uint8_t mask = bios->data[offset + 4];
  1470. uint8_t data = bios->data[offset + 5];
  1471. struct nouveau_i2c_chan *chan;
  1472. union i2c_smbus_data val;
  1473. int ret;
  1474. /* no execute check by design */
  1475. BIOSLOG(bios, "0x%04X: DCBI2CIndex: 0x%02X, I2CAddress: 0x%02X\n",
  1476. offset, i2c_index, i2c_address);
  1477. chan = init_i2c_device_find(bios->dev, i2c_index);
  1478. if (!chan)
  1479. return -ENODEV;
  1480. ret = i2c_smbus_xfer(&chan->adapter, i2c_address, 0,
  1481. I2C_SMBUS_READ, reg,
  1482. I2C_SMBUS_BYTE_DATA, &val);
  1483. if (ret < 0) {
  1484. BIOSLOG(bios, "0x%04X: I2CReg: 0x%02X, Value: [no device], "
  1485. "Mask: 0x%02X, Data: 0x%02X\n",
  1486. offset, reg, mask, data);
  1487. iexec->execute = 0;
  1488. return 6;
  1489. }
  1490. BIOSLOG(bios, "0x%04X: I2CReg: 0x%02X, Value: 0x%02X, "
  1491. "Mask: 0x%02X, Data: 0x%02X\n",
  1492. offset, reg, val.byte, mask, data);
  1493. iexec->execute = ((val.byte & mask) == data);
  1494. return 6;
  1495. }
  1496. static int
  1497. init_copy_nv_reg(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
  1498. {
  1499. /*
  1500. * INIT_COPY_NV_REG opcode: 0x5F ('_')
  1501. *
  1502. * offset (8 bit): opcode
  1503. * offset + 1 (32 bit): src reg
  1504. * offset + 5 (8 bit): shift
  1505. * offset + 6 (32 bit): src mask
  1506. * offset + 10 (32 bit): xor
  1507. * offset + 14 (32 bit): dst reg
  1508. * offset + 18 (32 bit): dst mask
  1509. *
  1510. * Shift REGVAL("src reg") right by (signed) "shift", AND result with
  1511. * "src mask", then XOR with "xor". Write this OR'd with
  1512. * (REGVAL("dst reg") AND'd with "dst mask") to "dst reg"
  1513. */
  1514. uint32_t srcreg = *((uint32_t *)(&bios->data[offset + 1]));
  1515. uint8_t shift = bios->data[offset + 5];
  1516. uint32_t srcmask = *((uint32_t *)(&bios->data[offset + 6]));
  1517. uint32_t xor = *((uint32_t *)(&bios->data[offset + 10]));
  1518. uint32_t dstreg = *((uint32_t *)(&bios->data[offset + 14]));
  1519. uint32_t dstmask = *((uint32_t *)(&bios->data[offset + 18]));
  1520. uint32_t srcvalue, dstvalue;
  1521. if (!iexec->execute)
  1522. return 22;
  1523. BIOSLOG(bios, "0x%04X: SrcReg: 0x%08X, Shift: 0x%02X, SrcMask: 0x%08X, "
  1524. "Xor: 0x%08X, DstReg: 0x%08X, DstMask: 0x%08X\n",
  1525. offset, srcreg, shift, srcmask, xor, dstreg, dstmask);
  1526. srcvalue = bios_rd32(bios, srcreg);
  1527. if (shift < 0x80)
  1528. srcvalue >>= shift;
  1529. else
  1530. srcvalue <<= (0x100 - shift);
  1531. srcvalue = (srcvalue & srcmask) ^ xor;
  1532. dstvalue = bios_rd32(bios, dstreg) & dstmask;
  1533. bios_wr32(bios, dstreg, dstvalue | srcvalue);
  1534. return 22;
  1535. }
  1536. static int
  1537. init_zm_index_io(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
  1538. {
  1539. /*
  1540. * INIT_ZM_INDEX_IO opcode: 0x62 ('b')
  1541. *
  1542. * offset (8 bit): opcode
  1543. * offset + 1 (16 bit): CRTC port
  1544. * offset + 3 (8 bit): CRTC index
  1545. * offset + 4 (8 bit): data
  1546. *
  1547. * Write "data" to index "CRTC index" of "CRTC port"
  1548. */
  1549. uint16_t crtcport = ROM16(bios->data[offset + 1]);
  1550. uint8_t crtcindex = bios->data[offset + 3];
  1551. uint8_t data = bios->data[offset + 4];
  1552. if (!iexec->execute)
  1553. return 5;
  1554. bios_idxprt_wr(bios, crtcport, crtcindex, data);
  1555. return 5;
  1556. }
  1557. static inline void
  1558. bios_md32(struct nvbios *bios, uint32_t reg,
  1559. uint32_t mask, uint32_t val)
  1560. {
  1561. bios_wr32(bios, reg, (bios_rd32(bios, reg) & ~mask) | val);
  1562. }
  1563. static uint32_t
  1564. peek_fb(struct drm_device *dev, struct io_mapping *fb,
  1565. uint32_t off)
  1566. {
  1567. uint32_t val = 0;
  1568. if (off < pci_resource_len(dev->pdev, 1)) {
  1569. uint8_t __iomem *p =
  1570. io_mapping_map_atomic_wc(fb, off & PAGE_MASK);
  1571. val = ioread32(p + (off & ~PAGE_MASK));
  1572. io_mapping_unmap_atomic(p);
  1573. }
  1574. return val;
  1575. }
  1576. static void
  1577. poke_fb(struct drm_device *dev, struct io_mapping *fb,
  1578. uint32_t off, uint32_t val)
  1579. {
  1580. if (off < pci_resource_len(dev->pdev, 1)) {
  1581. uint8_t __iomem *p =
  1582. io_mapping_map_atomic_wc(fb, off & PAGE_MASK);
  1583. iowrite32(val, p + (off & ~PAGE_MASK));
  1584. wmb();
  1585. io_mapping_unmap_atomic(p);
  1586. }
  1587. }
  1588. static inline bool
  1589. read_back_fb(struct drm_device *dev, struct io_mapping *fb,
  1590. uint32_t off, uint32_t val)
  1591. {
  1592. poke_fb(dev, fb, off, val);
  1593. return val == peek_fb(dev, fb, off);
  1594. }
  1595. static int
  1596. nv04_init_compute_mem(struct nvbios *bios)
  1597. {
  1598. struct drm_device *dev = bios->dev;
  1599. uint32_t patt = 0xdeadbeef;
  1600. struct io_mapping *fb;
  1601. int i;
  1602. /* Map the framebuffer aperture */
  1603. fb = io_mapping_create_wc(pci_resource_start(dev->pdev, 1),
  1604. pci_resource_len(dev->pdev, 1));
  1605. if (!fb)
  1606. return -ENOMEM;
  1607. /* Sequencer and refresh off */
  1608. NVWriteVgaSeq(dev, 0, 1, NVReadVgaSeq(dev, 0, 1) | 0x20);
  1609. bios_md32(bios, NV04_PFB_DEBUG_0, 0, NV04_PFB_DEBUG_0_REFRESH_OFF);
  1610. bios_md32(bios, NV04_PFB_BOOT_0, ~0,
  1611. NV04_PFB_BOOT_0_RAM_AMOUNT_16MB |
  1612. NV04_PFB_BOOT_0_RAM_WIDTH_128 |
  1613. NV04_PFB_BOOT_0_RAM_TYPE_SGRAM_16MBIT);
  1614. for (i = 0; i < 4; i++)
  1615. poke_fb(dev, fb, 4 * i, patt);
  1616. poke_fb(dev, fb, 0x400000, patt + 1);
  1617. if (peek_fb(dev, fb, 0) == patt + 1) {
  1618. bios_md32(bios, NV04_PFB_BOOT_0, NV04_PFB_BOOT_0_RAM_TYPE,
  1619. NV04_PFB_BOOT_0_RAM_TYPE_SDRAM_16MBIT);
  1620. bios_md32(bios, NV04_PFB_DEBUG_0,
  1621. NV04_PFB_DEBUG_0_REFRESH_OFF, 0);
  1622. for (i = 0; i < 4; i++)
  1623. poke_fb(dev, fb, 4 * i, patt);
  1624. if ((peek_fb(dev, fb, 0xc) & 0xffff) != (patt & 0xffff))
  1625. bios_md32(bios, NV04_PFB_BOOT_0,
  1626. NV04_PFB_BOOT_0_RAM_WIDTH_128 |
  1627. NV04_PFB_BOOT_0_RAM_AMOUNT,
  1628. NV04_PFB_BOOT_0_RAM_AMOUNT_8MB);
  1629. } else if ((peek_fb(dev, fb, 0xc) & 0xffff0000) !=
  1630. (patt & 0xffff0000)) {
  1631. bios_md32(bios, NV04_PFB_BOOT_0,
  1632. NV04_PFB_BOOT_0_RAM_WIDTH_128 |
  1633. NV04_PFB_BOOT_0_RAM_AMOUNT,
  1634. NV04_PFB_BOOT_0_RAM_AMOUNT_4MB);
  1635. } else if (peek_fb(dev, fb, 0) != patt) {
  1636. if (read_back_fb(dev, fb, 0x800000, patt))
  1637. bios_md32(bios, NV04_PFB_BOOT_0,
  1638. NV04_PFB_BOOT_0_RAM_AMOUNT,
  1639. NV04_PFB_BOOT_0_RAM_AMOUNT_8MB);
  1640. else
  1641. bios_md32(bios, NV04_PFB_BOOT_0,
  1642. NV04_PFB_BOOT_0_RAM_AMOUNT,
  1643. NV04_PFB_BOOT_0_RAM_AMOUNT_4MB);
  1644. bios_md32(bios, NV04_PFB_BOOT_0, NV04_PFB_BOOT_0_RAM_TYPE,
  1645. NV04_PFB_BOOT_0_RAM_TYPE_SGRAM_8MBIT);
  1646. } else if (!read_back_fb(dev, fb, 0x800000, patt)) {
  1647. bios_md32(bios, NV04_PFB_BOOT_0, NV04_PFB_BOOT_0_RAM_AMOUNT,
  1648. NV04_PFB_BOOT_0_RAM_AMOUNT_8MB);
  1649. }
  1650. /* Refresh on, sequencer on */
  1651. bios_md32(bios, NV04_PFB_DEBUG_0, NV04_PFB_DEBUG_0_REFRESH_OFF, 0);
  1652. NVWriteVgaSeq(dev, 0, 1, NVReadVgaSeq(dev, 0, 1) & ~0x20);
  1653. io_mapping_free(fb);
  1654. return 0;
  1655. }
  1656. static const uint8_t *
  1657. nv05_memory_config(struct nvbios *bios)
  1658. {
  1659. /* Defaults for BIOSes lacking a memory config table */
  1660. static const uint8_t default_config_tab[][2] = {
  1661. { 0x24, 0x00 },
  1662. { 0x28, 0x00 },
  1663. { 0x24, 0x01 },
  1664. { 0x1f, 0x00 },
  1665. { 0x0f, 0x00 },
  1666. { 0x17, 0x00 },
  1667. { 0x06, 0x00 },
  1668. { 0x00, 0x00 }
  1669. };
  1670. int i = (bios_rd32(bios, NV_PEXTDEV_BOOT_0) &
  1671. NV_PEXTDEV_BOOT_0_RAMCFG) >> 2;
  1672. if (bios->legacy.mem_init_tbl_ptr)
  1673. return &bios->data[bios->legacy.mem_init_tbl_ptr + 2 * i];
  1674. else
  1675. return default_config_tab[i];
  1676. }
  1677. static int
  1678. nv05_init_compute_mem(struct nvbios *bios)
  1679. {
  1680. struct drm_device *dev = bios->dev;
  1681. const uint8_t *ramcfg = nv05_memory_config(bios);
  1682. uint32_t patt = 0xdeadbeef;
  1683. struct io_mapping *fb;
  1684. int i, v;
  1685. /* Map the framebuffer aperture */
  1686. fb = io_mapping_create_wc(pci_resource_start(dev->pdev, 1),
  1687. pci_resource_len(dev->pdev, 1));
  1688. if (!fb)
  1689. return -ENOMEM;
  1690. /* Sequencer off */
  1691. NVWriteVgaSeq(dev, 0, 1, NVReadVgaSeq(dev, 0, 1) | 0x20);
  1692. if (bios_rd32(bios, NV04_PFB_BOOT_0) & NV04_PFB_BOOT_0_UMA_ENABLE)
  1693. goto out;
  1694. bios_md32(bios, NV04_PFB_DEBUG_0, NV04_PFB_DEBUG_0_REFRESH_OFF, 0);
  1695. /* If present load the hardcoded scrambling table */
  1696. if (bios->legacy.mem_init_tbl_ptr) {
  1697. uint32_t *scramble_tab = (uint32_t *)&bios->data[
  1698. bios->legacy.mem_init_tbl_ptr + 0x10];
  1699. for (i = 0; i < 8; i++)
  1700. bios_wr32(bios, NV04_PFB_SCRAMBLE(i),
  1701. ROM32(scramble_tab[i]));
  1702. }
  1703. /* Set memory type/width/length defaults depending on the straps */
  1704. bios_md32(bios, NV04_PFB_BOOT_0, 0x3f, ramcfg[0]);
  1705. if (ramcfg[1] & 0x80)
  1706. bios_md32(bios, NV04_PFB_CFG0, 0, NV04_PFB_CFG0_SCRAMBLE);
  1707. bios_md32(bios, NV04_PFB_CFG1, 0x700001, (ramcfg[1] & 1) << 20);
  1708. bios_md32(bios, NV04_PFB_CFG1, 0, 1);
  1709. /* Probe memory bus width */
  1710. for (i = 0; i < 4; i++)
  1711. poke_fb(dev, fb, 4 * i, patt);
  1712. if (peek_fb(dev, fb, 0xc) != patt)
  1713. bios_md32(bios, NV04_PFB_BOOT_0,
  1714. NV04_PFB_BOOT_0_RAM_WIDTH_128, 0);
  1715. /* Probe memory length */
  1716. v = bios_rd32(bios, NV04_PFB_BOOT_0) & NV04_PFB_BOOT_0_RAM_AMOUNT;
  1717. if (v == NV04_PFB_BOOT_0_RAM_AMOUNT_32MB &&
  1718. (!read_back_fb(dev, fb, 0x1000000, ++patt) ||
  1719. !read_back_fb(dev, fb, 0, ++patt)))
  1720. bios_md32(bios, NV04_PFB_BOOT_0, NV04_PFB_BOOT_0_RAM_AMOUNT,
  1721. NV04_PFB_BOOT_0_RAM_AMOUNT_16MB);
  1722. if (v == NV04_PFB_BOOT_0_RAM_AMOUNT_16MB &&
  1723. !read_back_fb(dev, fb, 0x800000, ++patt))
  1724. bios_md32(bios, NV04_PFB_BOOT_0, NV04_PFB_BOOT_0_RAM_AMOUNT,
  1725. NV04_PFB_BOOT_0_RAM_AMOUNT_8MB);
  1726. if (!read_back_fb(dev, fb, 0x400000, ++patt))
  1727. bios_md32(bios, NV04_PFB_BOOT_0, NV04_PFB_BOOT_0_RAM_AMOUNT,
  1728. NV04_PFB_BOOT_0_RAM_AMOUNT_4MB);
  1729. out:
  1730. /* Sequencer on */
  1731. NVWriteVgaSeq(dev, 0, 1, NVReadVgaSeq(dev, 0, 1) & ~0x20);
  1732. io_mapping_free(fb);
  1733. return 0;
  1734. }
  1735. static int
  1736. nv10_init_compute_mem(struct nvbios *bios)
  1737. {
  1738. struct drm_device *dev = bios->dev;
  1739. struct drm_nouveau_private *dev_priv = bios->dev->dev_private;
  1740. const int mem_width[] = { 0x10, 0x00, 0x20 };
  1741. const int mem_width_count = (dev_priv->chipset >= 0x17 ? 3 : 2);
  1742. uint32_t patt = 0xdeadbeef;
  1743. struct io_mapping *fb;
  1744. int i, j, k;
  1745. /* Map the framebuffer aperture */
  1746. fb = io_mapping_create_wc(pci_resource_start(dev->pdev, 1),
  1747. pci_resource_len(dev->pdev, 1));
  1748. if (!fb)
  1749. return -ENOMEM;
  1750. bios_wr32(bios, NV10_PFB_REFCTRL, NV10_PFB_REFCTRL_VALID_1);
  1751. /* Probe memory bus width */
  1752. for (i = 0; i < mem_width_count; i++) {
  1753. bios_md32(bios, NV04_PFB_CFG0, 0x30, mem_width[i]);
  1754. for (j = 0; j < 4; j++) {
  1755. for (k = 0; k < 4; k++)
  1756. poke_fb(dev, fb, 0x1c, 0);
  1757. poke_fb(dev, fb, 0x1c, patt);
  1758. poke_fb(dev, fb, 0x3c, 0);
  1759. if (peek_fb(dev, fb, 0x1c) == patt)
  1760. goto mem_width_found;
  1761. }
  1762. }
  1763. mem_width_found:
  1764. patt <<= 1;
  1765. /* Probe amount of installed memory */
  1766. for (i = 0; i < 4; i++) {
  1767. int off = bios_rd32(bios, NV04_PFB_FIFO_DATA) - 0x100000;
  1768. poke_fb(dev, fb, off, patt);
  1769. poke_fb(dev, fb, 0, 0);
  1770. peek_fb(dev, fb, 0);
  1771. peek_fb(dev, fb, 0);
  1772. peek_fb(dev, fb, 0);
  1773. peek_fb(dev, fb, 0);
  1774. if (peek_fb(dev, fb, off) == patt)
  1775. goto amount_found;
  1776. }
  1777. /* IC missing - disable the upper half memory space. */
  1778. bios_md32(bios, NV04_PFB_CFG0, 0x1000, 0);
  1779. amount_found:
  1780. io_mapping_free(fb);
  1781. return 0;
  1782. }
  1783. static int
  1784. nv20_init_compute_mem(struct nvbios *bios)
  1785. {
  1786. struct drm_device *dev = bios->dev;
  1787. struct drm_nouveau_private *dev_priv = bios->dev->dev_private;
  1788. uint32_t mask = (dev_priv->chipset >= 0x25 ? 0x300 : 0x900);
  1789. uint32_t amount, off;
  1790. struct io_mapping *fb;
  1791. /* Map the framebuffer aperture */
  1792. fb = io_mapping_create_wc(pci_resource_start(dev->pdev, 1),
  1793. pci_resource_len(dev->pdev, 1));
  1794. if (!fb)
  1795. return -ENOMEM;
  1796. bios_wr32(bios, NV10_PFB_REFCTRL, NV10_PFB_REFCTRL_VALID_1);
  1797. /* Allow full addressing */
  1798. bios_md32(bios, NV04_PFB_CFG0, 0, mask);
  1799. amount = bios_rd32(bios, NV04_PFB_FIFO_DATA);
  1800. for (off = amount; off > 0x2000000; off -= 0x2000000)
  1801. poke_fb(dev, fb, off - 4, off);
  1802. amount = bios_rd32(bios, NV04_PFB_FIFO_DATA);
  1803. if (amount != peek_fb(dev, fb, amount - 4))
  1804. /* IC missing - disable the upper half memory space. */
  1805. bios_md32(bios, NV04_PFB_CFG0, mask, 0);
  1806. io_mapping_free(fb);
  1807. return 0;
  1808. }
  1809. static int
  1810. init_compute_mem(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
  1811. {
  1812. /*
  1813. * INIT_COMPUTE_MEM opcode: 0x63 ('c')
  1814. *
  1815. * offset (8 bit): opcode
  1816. *
  1817. * This opcode is meant to set the PFB memory config registers
  1818. * appropriately so that we can correctly calculate how much VRAM it
  1819. * has (on nv10 and better chipsets the amount of installed VRAM is
  1820. * subsequently reported in NV_PFB_CSTATUS (0x10020C)).
  1821. *
  1822. * The implementation of this opcode in general consists of several
  1823. * parts:
  1824. *
  1825. * 1) Determination of memory type and density. Only necessary for
  1826. * really old chipsets, the memory type reported by the strap bits
  1827. * (0x101000) is assumed to be accurate on nv05 and newer.
  1828. *
  1829. * 2) Determination of the memory bus width. Usually done by a cunning
  1830. * combination of writes to offsets 0x1c and 0x3c in the fb, and
  1831. * seeing whether the written values are read back correctly.
  1832. *
  1833. * Only necessary on nv0x-nv1x and nv34, on the other cards we can
  1834. * trust the straps.
  1835. *
  1836. * 3) Determination of how many of the card's RAM pads have ICs
  1837. * attached, usually done by a cunning combination of writes to an
  1838. * offset slightly less than the maximum memory reported by
  1839. * NV_PFB_CSTATUS, then seeing if the test pattern can be read back.
  1840. *
  1841. * This appears to be a NOP on IGPs and NV4x or newer chipsets, both io
  1842. * logs of the VBIOS and kmmio traces of the binary driver POSTing the
  1843. * card show nothing being done for this opcode. Why is it still listed
  1844. * in the table?!
  1845. */
  1846. /* no iexec->execute check by design */
  1847. struct drm_nouveau_private *dev_priv = bios->dev->dev_private;
  1848. int ret;
  1849. if (dev_priv->chipset >= 0x40 ||
  1850. dev_priv->chipset == 0x1a ||
  1851. dev_priv->chipset == 0x1f)
  1852. ret = 0;
  1853. else if (dev_priv->chipset >= 0x20 &&
  1854. dev_priv->chipset != 0x34)
  1855. ret = nv20_init_compute_mem(bios);
  1856. else if (dev_priv->chipset >= 0x10)
  1857. ret = nv10_init_compute_mem(bios);
  1858. else if (dev_priv->chipset >= 0x5)
  1859. ret = nv05_init_compute_mem(bios);
  1860. else
  1861. ret = nv04_init_compute_mem(bios);
  1862. if (ret)
  1863. return ret;
  1864. return 1;
  1865. }
  1866. static int
  1867. init_reset(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
  1868. {
  1869. /*
  1870. * INIT_RESET opcode: 0x65 ('e')
  1871. *
  1872. * offset (8 bit): opcode
  1873. * offset + 1 (32 bit): register
  1874. * offset + 5 (32 bit): value1
  1875. * offset + 9 (32 bit): value2
  1876. *
  1877. * Assign "value1" to "register", then assign "value2" to "register"
  1878. */
  1879. uint32_t reg = ROM32(bios->data[offset + 1]);
  1880. uint32_t value1 = ROM32(bios->data[offset + 5]);
  1881. uint32_t value2 = ROM32(bios->data[offset + 9]);
  1882. uint32_t pci_nv_19, pci_nv_20;
  1883. /* no iexec->execute check by design */
  1884. pci_nv_19 = bios_rd32(bios, NV_PBUS_PCI_NV_19);
  1885. bios_wr32(bios, NV_PBUS_PCI_NV_19, pci_nv_19 & ~0xf00);
  1886. bios_wr32(bios, reg, value1);
  1887. udelay(10);
  1888. bios_wr32(bios, reg, value2);
  1889. bios_wr32(bios, NV_PBUS_PCI_NV_19, pci_nv_19);
  1890. pci_nv_20 = bios_rd32(bios, NV_PBUS_PCI_NV_20);
  1891. pci_nv_20 &= ~NV_PBUS_PCI_NV_20_ROM_SHADOW_ENABLED; /* 0xfffffffe */
  1892. bios_wr32(bios, NV_PBUS_PCI_NV_20, pci_nv_20);
  1893. return 13;
  1894. }
  1895. static int
  1896. init_configure_mem(struct nvbios *bios, uint16_t offset,
  1897. struct init_exec *iexec)
  1898. {
  1899. /*
  1900. * INIT_CONFIGURE_MEM opcode: 0x66 ('f')
  1901. *
  1902. * offset (8 bit): opcode
  1903. *
  1904. * Equivalent to INIT_DONE on bios version 3 or greater.
  1905. * For early bios versions, sets up the memory registers, using values
  1906. * taken from the memory init table
  1907. */
  1908. /* no iexec->execute check by design */
  1909. uint16_t meminitoffs = bios->legacy.mem_init_tbl_ptr + MEM_INIT_SIZE * (bios_idxprt_rd(bios, NV_CIO_CRX__COLOR, NV_CIO_CRE_SCRATCH4__INDEX) >> 4);
  1910. uint16_t seqtbloffs = bios->legacy.sdr_seq_tbl_ptr, meminitdata = meminitoffs + 6;
  1911. uint32_t reg, data;
  1912. if (bios->major_version > 2)
  1913. return 0;
  1914. bios_idxprt_wr(bios, NV_VIO_SRX, NV_VIO_SR_CLOCK_INDEX, bios_idxprt_rd(
  1915. bios, NV_VIO_SRX, NV_VIO_SR_CLOCK_INDEX) | 0x20);
  1916. if (bios->data[meminitoffs] & 1)
  1917. seqtbloffs = bios->legacy.ddr_seq_tbl_ptr;
  1918. for (reg = ROM32(bios->data[seqtbloffs]);
  1919. reg != 0xffffffff;
  1920. reg = ROM32(bios->data[seqtbloffs += 4])) {
  1921. switch (reg) {
  1922. case NV04_PFB_PRE:
  1923. data = NV04_PFB_PRE_CMD_PRECHARGE;
  1924. break;
  1925. case NV04_PFB_PAD:
  1926. data = NV04_PFB_PAD_CKE_NORMAL;
  1927. break;
  1928. case NV04_PFB_REF:
  1929. data = NV04_PFB_REF_CMD_REFRESH;
  1930. break;
  1931. default:
  1932. data = ROM32(bios->data[meminitdata]);
  1933. meminitdata += 4;
  1934. if (data == 0xffffffff)
  1935. continue;
  1936. }
  1937. bios_wr32(bios, reg, data);
  1938. }
  1939. return 1;
  1940. }
  1941. static int
  1942. init_configure_clk(struct nvbios *bios, uint16_t offset,
  1943. struct init_exec *iexec)
  1944. {
  1945. /*
  1946. * INIT_CONFIGURE_CLK opcode: 0x67 ('g')
  1947. *
  1948. * offset (8 bit): opcode
  1949. *
  1950. * Equivalent to INIT_DONE on bios version 3 or greater.
  1951. * For early bios versions, sets up the NVClk and MClk PLLs, using
  1952. * values taken from the memory init table
  1953. */
  1954. /* no iexec->execute check by design */
  1955. uint16_t meminitoffs = bios->legacy.mem_init_tbl_ptr + MEM_INIT_SIZE * (bios_idxprt_rd(bios, NV_CIO_CRX__COLOR, NV_CIO_CRE_SCRATCH4__INDEX) >> 4);
  1956. int clock;
  1957. if (bios->major_version > 2)
  1958. return 0;
  1959. clock = ROM16(bios->data[meminitoffs + 4]) * 10;
  1960. setPLL(bios, NV_PRAMDAC_NVPLL_COEFF, clock);
  1961. clock = ROM16(bios->data[meminitoffs + 2]) * 10;
  1962. if (bios->data[meminitoffs] & 1) /* DDR */
  1963. clock *= 2;
  1964. setPLL(bios, NV_PRAMDAC_MPLL_COEFF, clock);
  1965. return 1;
  1966. }
  1967. static int
  1968. init_configure_preinit(struct nvbios *bios, uint16_t offset,
  1969. struct init_exec *iexec)
  1970. {
  1971. /*
  1972. * INIT_CONFIGURE_PREINIT opcode: 0x68 ('h')
  1973. *
  1974. * offset (8 bit): opcode
  1975. *
  1976. * Equivalent to INIT_DONE on bios version 3 or greater.
  1977. * For early bios versions, does early init, loading ram and crystal
  1978. * configuration from straps into CR3C
  1979. */
  1980. /* no iexec->execute check by design */
  1981. uint32_t straps = bios_rd32(bios, NV_PEXTDEV_BOOT_0);
  1982. uint8_t cr3c = ((straps << 2) & 0xf0) | (straps & 0x40) >> 6;
  1983. if (bios->major_version > 2)
  1984. return 0;
  1985. bios_idxprt_wr(bios, NV_CIO_CRX__COLOR,
  1986. NV_CIO_CRE_SCRATCH4__INDEX, cr3c);
  1987. return 1;
  1988. }
  1989. static int
  1990. init_io(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
  1991. {
  1992. /*
  1993. * INIT_IO opcode: 0x69 ('i')
  1994. *
  1995. * offset (8 bit): opcode
  1996. * offset + 1 (16 bit): CRTC port
  1997. * offset + 3 (8 bit): mask
  1998. * offset + 4 (8 bit): data
  1999. *
  2000. * Assign ((IOVAL("crtc port") & "mask") | "data") to "crtc port"
  2001. */
  2002. struct drm_nouveau_private *dev_priv = bios->dev->dev_private;
  2003. uint16_t crtcport = ROM16(bios->data[offset + 1]);
  2004. uint8_t mask = bios->data[offset + 3];
  2005. uint8_t data = bios->data[offset + 4];
  2006. if (!iexec->execute)
  2007. return 5;
  2008. BIOSLOG(bios, "0x%04X: Port: 0x%04X, Mask: 0x%02X, Data: 0x%02X\n",
  2009. offset, crtcport, mask, data);
  2010. /*
  2011. * I have no idea what this does, but NVIDIA do this magic sequence
  2012. * in the places where this INIT_IO happens..
  2013. */
  2014. if (dev_priv->card_type >= NV_50 && crtcport == 0x3c3 && data == 1) {
  2015. int i;
  2016. bios_wr32(bios, 0x614100, (bios_rd32(
  2017. bios, 0x614100) & 0x0fffffff) | 0x00800000);
  2018. bios_wr32(bios, 0x00e18c, bios_rd32(
  2019. bios, 0x00e18c) | 0x00020000);
  2020. bios_wr32(bios, 0x614900, (bios_rd32(
  2021. bios, 0x614900) & 0x0fffffff) | 0x00800000);
  2022. bios_wr32(bios, 0x000200, bios_rd32(
  2023. bios, 0x000200) & ~0x40000000);
  2024. mdelay(10);
  2025. bios_wr32(bios, 0x00e18c, bios_rd32(
  2026. bios, 0x00e18c) & ~0x00020000);
  2027. bios_wr32(bios, 0x000200, bios_rd32(
  2028. bios, 0x000200) | 0x40000000);
  2029. bios_wr32(bios, 0x614100, 0x00800018);
  2030. bios_wr32(bios, 0x614900, 0x00800018);
  2031. mdelay(10);
  2032. bios_wr32(bios, 0x614100, 0x10000018);
  2033. bios_wr32(bios, 0x614900, 0x10000018);
  2034. for (i = 0; i < 3; i++)
  2035. bios_wr32(bios, 0x614280 + (i*0x800), bios_rd32(
  2036. bios, 0x614280 + (i*0x800)) & 0xf0f0f0f0);
  2037. for (i = 0; i < 2; i++)
  2038. bios_wr32(bios, 0x614300 + (i*0x800), bios_rd32(
  2039. bios, 0x614300 + (i*0x800)) & 0xfffff0f0);
  2040. for (i = 0; i < 3; i++)
  2041. bios_wr32(bios, 0x614380 + (i*0x800), bios_rd32(
  2042. bios, 0x614380 + (i*0x800)) & 0xfffff0f0);
  2043. for (i = 0; i < 2; i++)
  2044. bios_wr32(bios, 0x614200 + (i*0x800), bios_rd32(
  2045. bios, 0x614200 + (i*0x800)) & 0xfffffff0);
  2046. for (i = 0; i < 2; i++)
  2047. bios_wr32(bios, 0x614108 + (i*0x800), bios_rd32(
  2048. bios, 0x614108 + (i*0x800)) & 0x0fffffff);
  2049. return 5;
  2050. }
  2051. bios_port_wr(bios, crtcport, (bios_port_rd(bios, crtcport) & mask) |
  2052. data);
  2053. return 5;
  2054. }
  2055. static int
  2056. init_sub(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
  2057. {
  2058. /*
  2059. * INIT_SUB opcode: 0x6B ('k')
  2060. *
  2061. * offset (8 bit): opcode
  2062. * offset + 1 (8 bit): script number
  2063. *
  2064. * Execute script number "script number", as a subroutine
  2065. */
  2066. uint8_t sub = bios->data[offset + 1];
  2067. if (!iexec->execute)
  2068. return 2;
  2069. BIOSLOG(bios, "0x%04X: Calling script %d\n", offset, sub);
  2070. parse_init_table(bios,
  2071. ROM16(bios->data[bios->init_script_tbls_ptr + sub * 2]),
  2072. iexec);
  2073. BIOSLOG(bios, "0x%04X: End of script %d\n", offset, sub);
  2074. return 2;
  2075. }
  2076. static int
  2077. init_ram_condition(struct nvbios *bios, uint16_t offset,
  2078. struct init_exec *iexec)
  2079. {
  2080. /*
  2081. * INIT_RAM_CONDITION opcode: 0x6D ('m')
  2082. *
  2083. * offset (8 bit): opcode
  2084. * offset + 1 (8 bit): mask
  2085. * offset + 2 (8 bit): cmpval
  2086. *
  2087. * Test if (NV04_PFB_BOOT_0 & "mask") equals "cmpval".
  2088. * If condition not met skip subsequent opcodes until condition is
  2089. * inverted (INIT_NOT), or we hit INIT_RESUME
  2090. */
  2091. uint8_t mask = bios->data[offset + 1];
  2092. uint8_t cmpval = bios->data[offset + 2];
  2093. uint8_t data;
  2094. if (!iexec->execute)
  2095. return 3;
  2096. data = bios_rd32(bios, NV04_PFB_BOOT_0) & mask;
  2097. BIOSLOG(bios, "0x%04X: Checking if 0x%08X equals 0x%08X\n",
  2098. offset, data, cmpval);
  2099. if (data == cmpval)
  2100. BIOSLOG(bios, "0x%04X: Condition fulfilled -- continuing to execute\n", offset);
  2101. else {
  2102. BIOSLOG(bios, "0x%04X: Condition not fulfilled -- skipping following commands\n", offset);
  2103. iexec->execute = false;
  2104. }
  2105. return 3;
  2106. }
  2107. static int
  2108. init_nv_reg(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
  2109. {
  2110. /*
  2111. * INIT_NV_REG opcode: 0x6E ('n')
  2112. *
  2113. * offset (8 bit): opcode
  2114. * offset + 1 (32 bit): register
  2115. * offset + 5 (32 bit): mask
  2116. * offset + 9 (32 bit): data
  2117. *
  2118. * Assign ((REGVAL("register") & "mask") | "data") to "register"
  2119. */
  2120. uint32_t reg = ROM32(bios->data[offset + 1]);
  2121. uint32_t mask = ROM32(bios->data[offset + 5]);
  2122. uint32_t data = ROM32(bios->data[offset + 9]);
  2123. if (!iexec->execute)
  2124. return 13;
  2125. BIOSLOG(bios, "0x%04X: Reg: 0x%08X, Mask: 0x%08X, Data: 0x%08X\n",
  2126. offset, reg, mask, data);
  2127. bios_wr32(bios, reg, (bios_rd32(bios, reg) & mask) | data);
  2128. return 13;
  2129. }
  2130. static int
  2131. init_macro(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
  2132. {
  2133. /*
  2134. * INIT_MACRO opcode: 0x6F ('o')
  2135. *
  2136. * offset (8 bit): opcode
  2137. * offset + 1 (8 bit): macro number
  2138. *
  2139. * Look up macro index "macro number" in the macro index table.
  2140. * The macro index table entry has 1 byte for the index in the macro
  2141. * table, and 1 byte for the number of times to repeat the macro.
  2142. * The macro table entry has 4 bytes for the register address and
  2143. * 4 bytes for the value to write to that register
  2144. */
  2145. uint8_t macro_index_tbl_idx = bios->data[offset + 1];
  2146. uint16_t tmp = bios->macro_index_tbl_ptr + (macro_index_tbl_idx * MACRO_INDEX_SIZE);
  2147. uint8_t macro_tbl_idx = bios->data[tmp];
  2148. uint8_t count = bios->data[tmp + 1];
  2149. uint32_t reg, data;
  2150. int i;
  2151. if (!iexec->execute)
  2152. return 2;
  2153. BIOSLOG(bios, "0x%04X: Macro: 0x%02X, MacroTableIndex: 0x%02X, "
  2154. "Count: 0x%02X\n",
  2155. offset, macro_index_tbl_idx, macro_tbl_idx, count);
  2156. for (i = 0; i < count; i++) {
  2157. uint16_t macroentryptr = bios->macro_tbl_ptr + (macro_tbl_idx + i) * MACRO_SIZE;
  2158. reg = ROM32(bios->data[macroentryptr]);
  2159. data = ROM32(bios->data[macroentryptr + 4]);
  2160. bios_wr32(bios, reg, data);
  2161. }
  2162. return 2;
  2163. }
  2164. static int
  2165. init_done(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
  2166. {
  2167. /*
  2168. * INIT_DONE opcode: 0x71 ('q')
  2169. *
  2170. * offset (8 bit): opcode
  2171. *
  2172. * End the current script
  2173. */
  2174. /* mild retval abuse to stop parsing this table */
  2175. return 0;
  2176. }
  2177. static int
  2178. init_resume(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
  2179. {
  2180. /*
  2181. * INIT_RESUME opcode: 0x72 ('r')
  2182. *
  2183. * offset (8 bit): opcode
  2184. *
  2185. * End the current execute / no-execute condition
  2186. */
  2187. if (iexec->execute)
  2188. return 1;
  2189. iexec->execute = true;
  2190. BIOSLOG(bios, "0x%04X: ---- Executing following commands ----\n", offset);
  2191. return 1;
  2192. }
  2193. static int
  2194. init_time(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
  2195. {
  2196. /*
  2197. * INIT_TIME opcode: 0x74 ('t')
  2198. *
  2199. * offset (8 bit): opcode
  2200. * offset + 1 (16 bit): time
  2201. *
  2202. * Sleep for "time" microseconds.
  2203. */
  2204. unsigned time = ROM16(bios->data[offset + 1]);
  2205. if (!iexec->execute)
  2206. return 3;
  2207. BIOSLOG(bios, "0x%04X: Sleeping for 0x%04X microseconds\n",
  2208. offset, time);
  2209. if (time < 1000)
  2210. udelay(time);
  2211. else
  2212. mdelay((time + 900) / 1000);
  2213. return 3;
  2214. }
  2215. static int
  2216. init_condition(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
  2217. {
  2218. /*
  2219. * INIT_CONDITION opcode: 0x75 ('u')
  2220. *
  2221. * offset (8 bit): opcode
  2222. * offset + 1 (8 bit): condition number
  2223. *
  2224. * Check condition "condition number" in the condition table.
  2225. * If condition not met skip subsequent opcodes until condition is
  2226. * inverted (INIT_NOT), or we hit INIT_RESUME
  2227. */
  2228. uint8_t cond = bios->data[offset + 1];
  2229. if (!iexec->execute)
  2230. return 2;
  2231. BIOSLOG(bios, "0x%04X: Condition: 0x%02X\n", offset, cond);
  2232. if (bios_condition_met(bios, offset, cond))
  2233. BIOSLOG(bios, "0x%04X: Condition fulfilled -- continuing to execute\n", offset);
  2234. else {
  2235. BIOSLOG(bios, "0x%04X: Condition not fulfilled -- skipping following commands\n", offset);
  2236. iexec->execute = false;
  2237. }
  2238. return 2;
  2239. }
  2240. static int
  2241. init_io_condition(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
  2242. {
  2243. /*
  2244. * INIT_IO_CONDITION opcode: 0x76
  2245. *
  2246. * offset (8 bit): opcode
  2247. * offset + 1 (8 bit): condition number
  2248. *
  2249. * Check condition "condition number" in the io condition table.
  2250. * If condition not met skip subsequent opcodes until condition is
  2251. * inverted (INIT_NOT), or we hit INIT_RESUME
  2252. */
  2253. uint8_t cond = bios->data[offset + 1];
  2254. if (!iexec->execute)
  2255. return 2;
  2256. BIOSLOG(bios, "0x%04X: IO condition: 0x%02X\n", offset, cond);
  2257. if (io_condition_met(bios, offset, cond))
  2258. BIOSLOG(bios, "0x%04X: Condition fulfilled -- continuing to execute\n", offset);
  2259. else {
  2260. BIOSLOG(bios, "0x%04X: Condition not fulfilled -- skipping following commands\n", offset);
  2261. iexec->execute = false;
  2262. }
  2263. return 2;
  2264. }
  2265. static int
  2266. init_index_io(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
  2267. {
  2268. /*
  2269. * INIT_INDEX_IO opcode: 0x78 ('x')
  2270. *
  2271. * offset (8 bit): opcode
  2272. * offset + 1 (16 bit): CRTC port
  2273. * offset + 3 (8 bit): CRTC index
  2274. * offset + 4 (8 bit): mask
  2275. * offset + 5 (8 bit): data
  2276. *
  2277. * Read value at index "CRTC index" on "CRTC port", AND with "mask",
  2278. * OR with "data", write-back
  2279. */
  2280. uint16_t crtcport = ROM16(bios->data[offset + 1]);
  2281. uint8_t crtcindex = bios->data[offset + 3];
  2282. uint8_t mask = bios->data[offset + 4];
  2283. uint8_t data = bios->data[offset + 5];
  2284. uint8_t value;
  2285. if (!iexec->execute)
  2286. return 6;
  2287. BIOSLOG(bios, "0x%04X: Port: 0x%04X, Index: 0x%02X, Mask: 0x%02X, "
  2288. "Data: 0x%02X\n",
  2289. offset, crtcport, crtcindex, mask, data);
  2290. value = (bios_idxprt_rd(bios, crtcport, crtcindex) & mask) | data;
  2291. bios_idxprt_wr(bios, crtcport, crtcindex, value);
  2292. return 6;
  2293. }
  2294. static int
  2295. init_pll(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
  2296. {
  2297. /*
  2298. * INIT_PLL opcode: 0x79 ('y')
  2299. *
  2300. * offset (8 bit): opcode
  2301. * offset + 1 (32 bit): register
  2302. * offset + 5 (16 bit): freq
  2303. *
  2304. * Set PLL register "register" to coefficients for frequency (10kHz)
  2305. * "freq"
  2306. */
  2307. uint32_t reg = ROM32(bios->data[offset + 1]);
  2308. uint16_t freq = ROM16(bios->data[offset + 5]);
  2309. if (!iexec->execute)
  2310. return 7;
  2311. BIOSLOG(bios, "0x%04X: Reg: 0x%08X, Freq: %d0kHz\n", offset, reg, freq);
  2312. setPLL(bios, reg, freq * 10);
  2313. return 7;
  2314. }
  2315. static int
  2316. init_zm_reg(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
  2317. {
  2318. /*
  2319. * INIT_ZM_REG opcode: 0x7A ('z')
  2320. *
  2321. * offset (8 bit): opcode
  2322. * offset + 1 (32 bit): register
  2323. * offset + 5 (32 bit): value
  2324. *
  2325. * Assign "value" to "register"
  2326. */
  2327. uint32_t reg = ROM32(bios->data[offset + 1]);
  2328. uint32_t value = ROM32(bios->data[offset + 5]);
  2329. if (!iexec->execute)
  2330. return 9;
  2331. if (reg == 0x000200)
  2332. value |= 1;
  2333. bios_wr32(bios, reg, value);
  2334. return 9;
  2335. }
  2336. static int
  2337. init_ram_restrict_pll(struct nvbios *bios, uint16_t offset,
  2338. struct init_exec *iexec)
  2339. {
  2340. /*
  2341. * INIT_RAM_RESTRICT_PLL opcode: 0x87 ('')
  2342. *
  2343. * offset (8 bit): opcode
  2344. * offset + 1 (8 bit): PLL type
  2345. * offset + 2 (32 bit): frequency 0
  2346. *
  2347. * Uses the RAMCFG strap of PEXTDEV_BOOT as an index into the table at
  2348. * ram_restrict_table_ptr. The value read from there is used to select
  2349. * a frequency from the table starting at 'frequency 0' to be
  2350. * programmed into the PLL corresponding to 'type'.
  2351. *
  2352. * The PLL limits table on cards using this opcode has a mapping of
  2353. * 'type' to the relevant registers.
  2354. */
  2355. struct drm_device *dev = bios->dev;
  2356. uint32_t strap = (bios_rd32(bios, NV_PEXTDEV_BOOT_0) & 0x0000003c) >> 2;
  2357. uint8_t index = bios->data[bios->ram_restrict_tbl_ptr + strap];
  2358. uint8_t type = bios->data[offset + 1];
  2359. uint32_t freq = ROM32(bios->data[offset + 2 + (index * 4)]);
  2360. uint8_t *pll_limits = &bios->data[bios->pll_limit_tbl_ptr], *entry;
  2361. int len = 2 + bios->ram_restrict_group_count * 4;
  2362. int i;
  2363. if (!iexec->execute)
  2364. return len;
  2365. if (!bios->pll_limit_tbl_ptr || (pll_limits[0] & 0xf0) != 0x30) {
  2366. NV_ERROR(dev, "PLL limits table not version 3.x\n");
  2367. return len; /* deliberate, allow default clocks to remain */
  2368. }
  2369. entry = pll_limits + pll_limits[1];
  2370. for (i = 0; i < pll_limits[3]; i++, entry += pll_limits[2]) {
  2371. if (entry[0] == type) {
  2372. uint32_t reg = ROM32(entry[3]);
  2373. BIOSLOG(bios, "0x%04X: "
  2374. "Type %02x Reg 0x%08x Freq %dKHz\n",
  2375. offset, type, reg, freq);
  2376. setPLL(bios, reg, freq);
  2377. return len;
  2378. }
  2379. }
  2380. NV_ERROR(dev, "PLL type 0x%02x not found in PLL limits table", type);
  2381. return len;
  2382. }
  2383. static int
  2384. init_8c(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
  2385. {
  2386. /*
  2387. * INIT_8C opcode: 0x8C ('')
  2388. *
  2389. * NOP so far....
  2390. *
  2391. */
  2392. return 1;
  2393. }
  2394. static int
  2395. init_8d(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
  2396. {
  2397. /*
  2398. * INIT_8D opcode: 0x8D ('')
  2399. *
  2400. * NOP so far....
  2401. *
  2402. */
  2403. return 1;
  2404. }
  2405. static int
  2406. init_gpio(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
  2407. {
  2408. /*
  2409. * INIT_GPIO opcode: 0x8E ('')
  2410. *
  2411. * offset (8 bit): opcode
  2412. *
  2413. * Loop over all entries in the DCB GPIO table, and initialise
  2414. * each GPIO according to various values listed in each entry
  2415. */
  2416. if (iexec->execute && bios->execute)
  2417. nouveau_gpio_reset(bios->dev);
  2418. return 1;
  2419. }
  2420. static int
  2421. init_ram_restrict_zm_reg_group(struct nvbios *bios, uint16_t offset,
  2422. struct init_exec *iexec)
  2423. {
  2424. /*
  2425. * INIT_RAM_RESTRICT_ZM_REG_GROUP opcode: 0x8F ('')
  2426. *
  2427. * offset (8 bit): opcode
  2428. * offset + 1 (32 bit): reg
  2429. * offset + 5 (8 bit): regincrement
  2430. * offset + 6 (8 bit): count
  2431. * offset + 7 (32 bit): value 1,1
  2432. * ...
  2433. *
  2434. * Use the RAMCFG strap of PEXTDEV_BOOT as an index into the table at
  2435. * ram_restrict_table_ptr. The value read from here is 'n', and
  2436. * "value 1,n" gets written to "reg". This repeats "count" times and on
  2437. * each iteration 'm', "reg" increases by "regincrement" and
  2438. * "value m,n" is used. The extent of n is limited by a number read
  2439. * from the 'M' BIT table, herein called "blocklen"
  2440. */
  2441. uint32_t reg = ROM32(bios->data[offset + 1]);
  2442. uint8_t regincrement = bios->data[offset + 5];
  2443. uint8_t count = bios->data[offset + 6];
  2444. uint32_t strap_ramcfg, data;
  2445. /* previously set by 'M' BIT table */
  2446. uint16_t blocklen = bios->ram_restrict_group_count * 4;
  2447. int len = 7 + count * blocklen;
  2448. uint8_t index;
  2449. int i;
  2450. /* critical! to know the length of the opcode */;
  2451. if (!blocklen) {
  2452. NV_ERROR(bios->dev,
  2453. "0x%04X: Zero block length - has the M table "
  2454. "been parsed?\n", offset);
  2455. return -EINVAL;
  2456. }
  2457. if (!iexec->execute)
  2458. return len;
  2459. strap_ramcfg = (bios_rd32(bios, NV_PEXTDEV_BOOT_0) >> 2) & 0xf;
  2460. index = bios->data[bios->ram_restrict_tbl_ptr + strap_ramcfg];
  2461. BIOSLOG(bios, "0x%04X: Reg: 0x%08X, RegIncrement: 0x%02X, "
  2462. "Count: 0x%02X, StrapRamCfg: 0x%02X, Index: 0x%02X\n",
  2463. offset, reg, regincrement, count, strap_ramcfg, index);
  2464. for (i = 0; i < count; i++) {
  2465. data = ROM32(bios->data[offset + 7 + index * 4 + blocklen * i]);
  2466. bios_wr32(bios, reg, data);
  2467. reg += regincrement;
  2468. }
  2469. return len;
  2470. }
  2471. static int
  2472. init_copy_zm_reg(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
  2473. {
  2474. /*
  2475. * INIT_COPY_ZM_REG opcode: 0x90 ('')
  2476. *
  2477. * offset (8 bit): opcode
  2478. * offset + 1 (32 bit): src reg
  2479. * offset + 5 (32 bit): dst reg
  2480. *
  2481. * Put contents of "src reg" into "dst reg"
  2482. */
  2483. uint32_t srcreg = ROM32(bios->data[offset + 1]);
  2484. uint32_t dstreg = ROM32(bios->data[offset + 5]);
  2485. if (!iexec->execute)
  2486. return 9;
  2487. bios_wr32(bios, dstreg, bios_rd32(bios, srcreg));
  2488. return 9;
  2489. }
  2490. static int
  2491. init_zm_reg_group_addr_latched(struct nvbios *bios, uint16_t offset,
  2492. struct init_exec *iexec)
  2493. {
  2494. /*
  2495. * INIT_ZM_REG_GROUP_ADDRESS_LATCHED opcode: 0x91 ('')
  2496. *
  2497. * offset (8 bit): opcode
  2498. * offset + 1 (32 bit): dst reg
  2499. * offset + 5 (8 bit): count
  2500. * offset + 6 (32 bit): data 1
  2501. * ...
  2502. *
  2503. * For each of "count" values write "data n" to "dst reg"
  2504. */
  2505. uint32_t reg = ROM32(bios->data[offset + 1]);
  2506. uint8_t count = bios->data[offset + 5];
  2507. int len = 6 + count * 4;
  2508. int i;
  2509. if (!iexec->execute)
  2510. return len;
  2511. for (i = 0; i < count; i++) {
  2512. uint32_t data = ROM32(bios->data[offset + 6 + 4 * i]);
  2513. bios_wr32(bios, reg, data);
  2514. }
  2515. return len;
  2516. }
  2517. static int
  2518. init_reserved(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
  2519. {
  2520. /*
  2521. * INIT_RESERVED opcode: 0x92 ('')
  2522. *
  2523. * offset (8 bit): opcode
  2524. *
  2525. * Seemingly does nothing
  2526. */
  2527. return 1;
  2528. }
  2529. static int
  2530. init_96(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
  2531. {
  2532. /*
  2533. * INIT_96 opcode: 0x96 ('')
  2534. *
  2535. * offset (8 bit): opcode
  2536. * offset + 1 (32 bit): sreg
  2537. * offset + 5 (8 bit): sshift
  2538. * offset + 6 (8 bit): smask
  2539. * offset + 7 (8 bit): index
  2540. * offset + 8 (32 bit): reg
  2541. * offset + 12 (32 bit): mask
  2542. * offset + 16 (8 bit): shift
  2543. *
  2544. */
  2545. uint16_t xlatptr = bios->init96_tbl_ptr + (bios->data[offset + 7] * 2);
  2546. uint32_t reg = ROM32(bios->data[offset + 8]);
  2547. uint32_t mask = ROM32(bios->data[offset + 12]);
  2548. uint32_t val;
  2549. val = bios_rd32(bios, ROM32(bios->data[offset + 1]));
  2550. if (bios->data[offset + 5] < 0x80)
  2551. val >>= bios->data[offset + 5];
  2552. else
  2553. val <<= (0x100 - bios->data[offset + 5]);
  2554. val &= bios->data[offset + 6];
  2555. val = bios->data[ROM16(bios->data[xlatptr]) + val];
  2556. val <<= bios->data[offset + 16];
  2557. if (!iexec->execute)
  2558. return 17;
  2559. bios_wr32(bios, reg, (bios_rd32(bios, reg) & mask) | val);
  2560. return 17;
  2561. }
  2562. static int
  2563. init_97(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
  2564. {
  2565. /*
  2566. * INIT_97 opcode: 0x97 ('')
  2567. *
  2568. * offset (8 bit): opcode
  2569. * offset + 1 (32 bit): register
  2570. * offset + 5 (32 bit): mask
  2571. * offset + 9 (32 bit): value
  2572. *
  2573. * Adds "value" to "register" preserving the fields specified
  2574. * by "mask"
  2575. */
  2576. uint32_t reg = ROM32(bios->data[offset + 1]);
  2577. uint32_t mask = ROM32(bios->data[offset + 5]);
  2578. uint32_t add = ROM32(bios->data[offset + 9]);
  2579. uint32_t val;
  2580. val = bios_rd32(bios, reg);
  2581. val = (val & mask) | ((val + add) & ~mask);
  2582. if (!iexec->execute)
  2583. return 13;
  2584. bios_wr32(bios, reg, val);
  2585. return 13;
  2586. }
  2587. static int
  2588. init_auxch(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
  2589. {
  2590. /*
  2591. * INIT_AUXCH opcode: 0x98 ('')
  2592. *
  2593. * offset (8 bit): opcode
  2594. * offset + 1 (32 bit): address
  2595. * offset + 5 (8 bit): count
  2596. * offset + 6 (8 bit): mask 0
  2597. * offset + 7 (8 bit): data 0
  2598. * ...
  2599. *
  2600. */
  2601. struct drm_device *dev = bios->dev;
  2602. struct nouveau_i2c_chan *auxch;
  2603. uint32_t addr = ROM32(bios->data[offset + 1]);
  2604. uint8_t count = bios->data[offset + 5];
  2605. int len = 6 + count * 2;
  2606. int ret, i;
  2607. if (!bios->display.output) {
  2608. NV_ERROR(dev, "INIT_AUXCH: no active output\n");
  2609. return len;
  2610. }
  2611. auxch = init_i2c_device_find(dev, bios->display.output->i2c_index);
  2612. if (!auxch) {
  2613. NV_ERROR(dev, "INIT_AUXCH: couldn't get auxch %d\n",
  2614. bios->display.output->i2c_index);
  2615. return len;
  2616. }
  2617. if (!iexec->execute)
  2618. return len;
  2619. offset += 6;
  2620. for (i = 0; i < count; i++, offset += 2) {
  2621. uint8_t data;
  2622. ret = nouveau_dp_auxch(auxch, 9, addr, &data, 1);
  2623. if (ret) {
  2624. NV_ERROR(dev, "INIT_AUXCH: rd auxch fail %d\n", ret);
  2625. return len;
  2626. }
  2627. data &= bios->data[offset + 0];
  2628. data |= bios->data[offset + 1];
  2629. ret = nouveau_dp_auxch(auxch, 8, addr, &data, 1);
  2630. if (ret) {
  2631. NV_ERROR(dev, "INIT_AUXCH: wr auxch fail %d\n", ret);
  2632. return len;
  2633. }
  2634. }
  2635. return len;
  2636. }
  2637. static int
  2638. init_zm_auxch(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
  2639. {
  2640. /*
  2641. * INIT_ZM_AUXCH opcode: 0x99 ('')
  2642. *
  2643. * offset (8 bit): opcode
  2644. * offset + 1 (32 bit): address
  2645. * offset + 5 (8 bit): count
  2646. * offset + 6 (8 bit): data 0
  2647. * ...
  2648. *
  2649. */
  2650. struct drm_device *dev = bios->dev;
  2651. struct nouveau_i2c_chan *auxch;
  2652. uint32_t addr = ROM32(bios->data[offset + 1]);
  2653. uint8_t count = bios->data[offset + 5];
  2654. int len = 6 + count;
  2655. int ret, i;
  2656. if (!bios->display.output) {
  2657. NV_ERROR(dev, "INIT_ZM_AUXCH: no active output\n");
  2658. return len;
  2659. }
  2660. auxch = init_i2c_device_find(dev, bios->display.output->i2c_index);
  2661. if (!auxch) {
  2662. NV_ERROR(dev, "INIT_ZM_AUXCH: couldn't get auxch %d\n",
  2663. bios->display.output->i2c_index);
  2664. return len;
  2665. }
  2666. if (!iexec->execute)
  2667. return len;
  2668. offset += 6;
  2669. for (i = 0; i < count; i++, offset++) {
  2670. ret = nouveau_dp_auxch(auxch, 8, addr, &bios->data[offset], 1);
  2671. if (ret) {
  2672. NV_ERROR(dev, "INIT_ZM_AUXCH: wr auxch fail %d\n", ret);
  2673. return len;
  2674. }
  2675. }
  2676. return len;
  2677. }
  2678. static int
  2679. init_i2c_long_if(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
  2680. {
  2681. /*
  2682. * INIT_I2C_LONG_IF opcode: 0x9A ('')
  2683. *
  2684. * offset (8 bit): opcode
  2685. * offset + 1 (8 bit): DCB I2C table entry index
  2686. * offset + 2 (8 bit): I2C slave address
  2687. * offset + 3 (16 bit): I2C register
  2688. * offset + 5 (8 bit): mask
  2689. * offset + 6 (8 bit): data
  2690. *
  2691. * Read the register given by "I2C register" on the device addressed
  2692. * by "I2C slave address" on the I2C bus given by "DCB I2C table
  2693. * entry index". Compare the result AND "mask" to "data".
  2694. * If they're not equal, skip subsequent opcodes until condition is
  2695. * inverted (INIT_NOT), or we hit INIT_RESUME
  2696. */
  2697. uint8_t i2c_index = bios->data[offset + 1];
  2698. uint8_t i2c_address = bios->data[offset + 2] >> 1;
  2699. uint8_t reglo = bios->data[offset + 3];
  2700. uint8_t reghi = bios->data[offset + 4];
  2701. uint8_t mask = bios->data[offset + 5];
  2702. uint8_t data = bios->data[offset + 6];
  2703. struct nouveau_i2c_chan *chan;
  2704. uint8_t buf0[2] = { reghi, reglo };
  2705. uint8_t buf1[1];
  2706. struct i2c_msg msg[2] = {
  2707. { i2c_address, 0, 1, buf0 },
  2708. { i2c_address, I2C_M_RD, 1, buf1 },
  2709. };
  2710. int ret;
  2711. /* no execute check by design */
  2712. BIOSLOG(bios, "0x%04X: DCBI2CIndex: 0x%02X, I2CAddress: 0x%02X\n",
  2713. offset, i2c_index, i2c_address);
  2714. chan = init_i2c_device_find(bios->dev, i2c_index);
  2715. if (!chan)
  2716. return -ENODEV;
  2717. ret = i2c_transfer(&chan->adapter, msg, 2);
  2718. if (ret < 0) {
  2719. BIOSLOG(bios, "0x%04X: I2CReg: 0x%02X:0x%02X, Value: [no device], "
  2720. "Mask: 0x%02X, Data: 0x%02X\n",
  2721. offset, reghi, reglo, mask, data);
  2722. iexec->execute = 0;
  2723. return 7;
  2724. }
  2725. BIOSLOG(bios, "0x%04X: I2CReg: 0x%02X:0x%02X, Value: 0x%02X, "
  2726. "Mask: 0x%02X, Data: 0x%02X\n",
  2727. offset, reghi, reglo, buf1[0], mask, data);
  2728. iexec->execute = ((buf1[0] & mask) == data);
  2729. return 7;
  2730. }
  2731. static struct init_tbl_entry itbl_entry[] = {
  2732. /* command name , id , length , offset , mult , command handler */
  2733. /* INIT_PROG (0x31, 15, 10, 4) removed due to no example of use */
  2734. { "INIT_IO_RESTRICT_PROG" , 0x32, init_io_restrict_prog },
  2735. { "INIT_REPEAT" , 0x33, init_repeat },
  2736. { "INIT_IO_RESTRICT_PLL" , 0x34, init_io_restrict_pll },
  2737. { "INIT_END_REPEAT" , 0x36, init_end_repeat },
  2738. { "INIT_COPY" , 0x37, init_copy },
  2739. { "INIT_NOT" , 0x38, init_not },
  2740. { "INIT_IO_FLAG_CONDITION" , 0x39, init_io_flag_condition },
  2741. { "INIT_DP_CONDITION" , 0x3A, init_dp_condition },
  2742. { "INIT_OP_3B" , 0x3B, init_op_3b },
  2743. { "INIT_OP_3C" , 0x3C, init_op_3c },
  2744. { "INIT_INDEX_ADDRESS_LATCHED" , 0x49, init_idx_addr_latched },
  2745. { "INIT_IO_RESTRICT_PLL2" , 0x4A, init_io_restrict_pll2 },
  2746. { "INIT_PLL2" , 0x4B, init_pll2 },
  2747. { "INIT_I2C_BYTE" , 0x4C, init_i2c_byte },
  2748. { "INIT_ZM_I2C_BYTE" , 0x4D, init_zm_i2c_byte },
  2749. { "INIT_ZM_I2C" , 0x4E, init_zm_i2c },
  2750. { "INIT_TMDS" , 0x4F, init_tmds },
  2751. { "INIT_ZM_TMDS_GROUP" , 0x50, init_zm_tmds_group },
  2752. { "INIT_CR_INDEX_ADDRESS_LATCHED" , 0x51, init_cr_idx_adr_latch },
  2753. { "INIT_CR" , 0x52, init_cr },
  2754. { "INIT_ZM_CR" , 0x53, init_zm_cr },
  2755. { "INIT_ZM_CR_GROUP" , 0x54, init_zm_cr_group },
  2756. { "INIT_CONDITION_TIME" , 0x56, init_condition_time },
  2757. { "INIT_LTIME" , 0x57, init_ltime },
  2758. { "INIT_ZM_REG_SEQUENCE" , 0x58, init_zm_reg_sequence },
  2759. /* INIT_INDIRECT_REG (0x5A, 7, 0, 0) removed due to no example of use */
  2760. { "INIT_SUB_DIRECT" , 0x5B, init_sub_direct },
  2761. { "INIT_JUMP" , 0x5C, init_jump },
  2762. { "INIT_I2C_IF" , 0x5E, init_i2c_if },
  2763. { "INIT_COPY_NV_REG" , 0x5F, init_copy_nv_reg },
  2764. { "INIT_ZM_INDEX_IO" , 0x62, init_zm_index_io },
  2765. { "INIT_COMPUTE_MEM" , 0x63, init_compute_mem },
  2766. { "INIT_RESET" , 0x65, init_reset },
  2767. { "INIT_CONFIGURE_MEM" , 0x66, init_configure_mem },
  2768. { "INIT_CONFIGURE_CLK" , 0x67, init_configure_clk },
  2769. { "INIT_CONFIGURE_PREINIT" , 0x68, init_configure_preinit },
  2770. { "INIT_IO" , 0x69, init_io },
  2771. { "INIT_SUB" , 0x6B, init_sub },
  2772. { "INIT_RAM_CONDITION" , 0x6D, init_ram_condition },
  2773. { "INIT_NV_REG" , 0x6E, init_nv_reg },
  2774. { "INIT_MACRO" , 0x6F, init_macro },
  2775. { "INIT_DONE" , 0x71, init_done },
  2776. { "INIT_RESUME" , 0x72, init_resume },
  2777. /* INIT_RAM_CONDITION2 (0x73, 9, 0, 0) removed due to no example of use */
  2778. { "INIT_TIME" , 0x74, init_time },
  2779. { "INIT_CONDITION" , 0x75, init_condition },
  2780. { "INIT_IO_CONDITION" , 0x76, init_io_condition },
  2781. { "INIT_INDEX_IO" , 0x78, init_index_io },
  2782. { "INIT_PLL" , 0x79, init_pll },
  2783. { "INIT_ZM_REG" , 0x7A, init_zm_reg },
  2784. { "INIT_RAM_RESTRICT_PLL" , 0x87, init_ram_restrict_pll },
  2785. { "INIT_8C" , 0x8C, init_8c },
  2786. { "INIT_8D" , 0x8D, init_8d },
  2787. { "INIT_GPIO" , 0x8E, init_gpio },
  2788. { "INIT_RAM_RESTRICT_ZM_REG_GROUP" , 0x8F, init_ram_restrict_zm_reg_group },
  2789. { "INIT_COPY_ZM_REG" , 0x90, init_copy_zm_reg },
  2790. { "INIT_ZM_REG_GROUP_ADDRESS_LATCHED" , 0x91, init_zm_reg_group_addr_latched },
  2791. { "INIT_RESERVED" , 0x92, init_reserved },
  2792. { "INIT_96" , 0x96, init_96 },
  2793. { "INIT_97" , 0x97, init_97 },
  2794. { "INIT_AUXCH" , 0x98, init_auxch },
  2795. { "INIT_ZM_AUXCH" , 0x99, init_zm_auxch },
  2796. { "INIT_I2C_LONG_IF" , 0x9A, init_i2c_long_if },
  2797. { NULL , 0 , NULL }
  2798. };
  2799. #define MAX_TABLE_OPS 1000
  2800. static int
  2801. parse_init_table(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
  2802. {
  2803. /*
  2804. * Parses all commands in an init table.
  2805. *
  2806. * We start out executing all commands found in the init table. Some
  2807. * opcodes may change the status of iexec->execute to SKIP, which will
  2808. * cause the following opcodes to perform no operation until the value
  2809. * is changed back to EXECUTE.
  2810. */
  2811. int count = 0, i, ret;
  2812. uint8_t id;
  2813. /* catch NULL script pointers */
  2814. if (offset == 0)
  2815. return 0;
  2816. /*
  2817. * Loop until INIT_DONE causes us to break out of the loop
  2818. * (or until offset > bios length just in case... )
  2819. * (and no more than MAX_TABLE_OPS iterations, just in case... )
  2820. */
  2821. while ((offset < bios->length) && (count++ < MAX_TABLE_OPS)) {
  2822. id = bios->data[offset];
  2823. /* Find matching id in itbl_entry */
  2824. for (i = 0; itbl_entry[i].name && (itbl_entry[i].id != id); i++)
  2825. ;
  2826. if (!itbl_entry[i].name) {
  2827. NV_ERROR(bios->dev,
  2828. "0x%04X: Init table command not found: "
  2829. "0x%02X\n", offset, id);
  2830. return -ENOENT;
  2831. }
  2832. BIOSLOG(bios, "0x%04X: [ (0x%02X) - %s ]\n", offset,
  2833. itbl_entry[i].id, itbl_entry[i].name);
  2834. /* execute eventual command handler */
  2835. ret = (*itbl_entry[i].handler)(bios, offset, iexec);
  2836. if (ret < 0) {
  2837. NV_ERROR(bios->dev, "0x%04X: Failed parsing init "
  2838. "table opcode: %s %d\n", offset,
  2839. itbl_entry[i].name, ret);
  2840. }
  2841. if (ret <= 0)
  2842. break;
  2843. /*
  2844. * Add the offset of the current command including all data
  2845. * of that command. The offset will then be pointing on the
  2846. * next op code.
  2847. */
  2848. offset += ret;
  2849. }
  2850. if (offset >= bios->length)
  2851. NV_WARN(bios->dev,
  2852. "Offset 0x%04X greater than known bios image length. "
  2853. "Corrupt image?\n", offset);
  2854. if (count >= MAX_TABLE_OPS)
  2855. NV_WARN(bios->dev,
  2856. "More than %d opcodes to a table is unlikely, "
  2857. "is the bios image corrupt?\n", MAX_TABLE_OPS);
  2858. return 0;
  2859. }
  2860. static void
  2861. parse_init_tables(struct nvbios *bios)
  2862. {
  2863. /* Loops and calls parse_init_table() for each present table. */
  2864. int i = 0;
  2865. uint16_t table;
  2866. struct init_exec iexec = {true, false};
  2867. if (bios->old_style_init) {
  2868. if (bios->init_script_tbls_ptr)
  2869. parse_init_table(bios, bios->init_script_tbls_ptr, &iexec);
  2870. if (bios->extra_init_script_tbl_ptr)
  2871. parse_init_table(bios, bios->extra_init_script_tbl_ptr, &iexec);
  2872. return;
  2873. }
  2874. while ((table = ROM16(bios->data[bios->init_script_tbls_ptr + i]))) {
  2875. NV_INFO(bios->dev,
  2876. "Parsing VBIOS init table %d at offset 0x%04X\n",
  2877. i / 2, table);
  2878. BIOSLOG(bios, "0x%04X: ------ Executing following commands ------\n", table);
  2879. parse_init_table(bios, table, &iexec);
  2880. i += 2;
  2881. }
  2882. }
  2883. static uint16_t clkcmptable(struct nvbios *bios, uint16_t clktable, int pxclk)
  2884. {
  2885. int compare_record_len, i = 0;
  2886. uint16_t compareclk, scriptptr = 0;
  2887. if (bios->major_version < 5) /* pre BIT */
  2888. compare_record_len = 3;
  2889. else
  2890. compare_record_len = 4;
  2891. do {
  2892. compareclk = ROM16(bios->data[clktable + compare_record_len * i]);
  2893. if (pxclk >= compareclk * 10) {
  2894. if (bios->major_version < 5) {
  2895. uint8_t tmdssub = bios->data[clktable + 2 + compare_record_len * i];
  2896. scriptptr = ROM16(bios->data[bios->init_script_tbls_ptr + tmdssub * 2]);
  2897. } else
  2898. scriptptr = ROM16(bios->data[clktable + 2 + compare_record_len * i]);
  2899. break;
  2900. }
  2901. i++;
  2902. } while (compareclk);
  2903. return scriptptr;
  2904. }
  2905. static void
  2906. run_digital_op_script(struct drm_device *dev, uint16_t scriptptr,
  2907. struct dcb_entry *dcbent, int head, bool dl)
  2908. {
  2909. struct drm_nouveau_private *dev_priv = dev->dev_private;
  2910. struct nvbios *bios = &dev_priv->vbios;
  2911. struct init_exec iexec = {true, false};
  2912. NV_TRACE(dev, "0x%04X: Parsing digital output script table\n",
  2913. scriptptr);
  2914. bios_idxprt_wr(bios, NV_CIO_CRX__COLOR, NV_CIO_CRE_44,
  2915. head ? NV_CIO_CRE_44_HEADB : NV_CIO_CRE_44_HEADA);
  2916. /* note: if dcb entries have been merged, index may be misleading */
  2917. NVWriteVgaCrtc5758(dev, head, 0, dcbent->index);
  2918. parse_init_table(bios, scriptptr, &iexec);
  2919. nv04_dfp_bind_head(dev, dcbent, head, dl);
  2920. }
  2921. static int call_lvds_manufacturer_script(struct drm_device *dev, struct dcb_entry *dcbent, int head, enum LVDS_script script)
  2922. {
  2923. struct drm_nouveau_private *dev_priv = dev->dev_private;
  2924. struct nvbios *bios = &dev_priv->vbios;
  2925. uint8_t sub = bios->data[bios->fp.xlated_entry + script] + (bios->fp.link_c_increment && dcbent->or & OUTPUT_C ? 1 : 0);
  2926. uint16_t scriptofs = ROM16(bios->data[bios->init_script_tbls_ptr + sub * 2]);
  2927. if (!bios->fp.xlated_entry || !sub || !scriptofs)
  2928. return -EINVAL;
  2929. run_digital_op_script(dev, scriptofs, dcbent, head, bios->fp.dual_link);
  2930. if (script == LVDS_PANEL_OFF) {
  2931. /* off-on delay in ms */
  2932. mdelay(ROM16(bios->data[bios->fp.xlated_entry + 7]));
  2933. }
  2934. #ifdef __powerpc__
  2935. /* Powerbook specific quirks */
  2936. if (script == LVDS_RESET &&
  2937. (dev->pci_device == 0x0179 || dev->pci_device == 0x0189 ||
  2938. dev->pci_device == 0x0329))
  2939. nv_write_tmds(dev, dcbent->or, 0, 0x02, 0x72);
  2940. #endif
  2941. return 0;
  2942. }
  2943. static int run_lvds_table(struct drm_device *dev, struct dcb_entry *dcbent, int head, enum LVDS_script script, int pxclk)
  2944. {
  2945. /*
  2946. * The BIT LVDS table's header has the information to setup the
  2947. * necessary registers. Following the standard 4 byte header are:
  2948. * A bitmask byte and a dual-link transition pxclk value for use in
  2949. * selecting the init script when not using straps; 4 script pointers
  2950. * for panel power, selected by output and on/off; and 8 table pointers
  2951. * for panel init, the needed one determined by output, and bits in the
  2952. * conf byte. These tables are similar to the TMDS tables, consisting
  2953. * of a list of pxclks and script pointers.
  2954. */
  2955. struct drm_nouveau_private *dev_priv = dev->dev_private;
  2956. struct nvbios *bios = &dev_priv->vbios;
  2957. unsigned int outputset = (dcbent->or == 4) ? 1 : 0;
  2958. uint16_t scriptptr = 0, clktable;
  2959. /*
  2960. * For now we assume version 3.0 table - g80 support will need some
  2961. * changes
  2962. */
  2963. switch (script) {
  2964. case LVDS_INIT:
  2965. return -ENOSYS;
  2966. case LVDS_BACKLIGHT_ON:
  2967. case LVDS_PANEL_ON:
  2968. scriptptr = ROM16(bios->data[bios->fp.lvdsmanufacturerpointer + 7 + outputset * 2]);
  2969. break;
  2970. case LVDS_BACKLIGHT_OFF:
  2971. case LVDS_PANEL_OFF:
  2972. scriptptr = ROM16(bios->data[bios->fp.lvdsmanufacturerpointer + 11 + outputset * 2]);
  2973. break;
  2974. case LVDS_RESET:
  2975. clktable = bios->fp.lvdsmanufacturerpointer + 15;
  2976. if (dcbent->or == 4)
  2977. clktable += 8;
  2978. if (dcbent->lvdsconf.use_straps_for_mode) {
  2979. if (bios->fp.dual_link)
  2980. clktable += 4;
  2981. if (bios->fp.if_is_24bit)
  2982. clktable += 2;
  2983. } else {
  2984. /* using EDID */
  2985. int cmpval_24bit = (dcbent->or == 4) ? 4 : 1;
  2986. if (bios->fp.dual_link) {
  2987. clktable += 4;
  2988. cmpval_24bit <<= 1;
  2989. }
  2990. if (bios->fp.strapless_is_24bit & cmpval_24bit)
  2991. clktable += 2;
  2992. }
  2993. clktable = ROM16(bios->data[clktable]);
  2994. if (!clktable) {
  2995. NV_ERROR(dev, "Pixel clock comparison table not found\n");
  2996. return -ENOENT;
  2997. }
  2998. scriptptr = clkcmptable(bios, clktable, pxclk);
  2999. }
  3000. if (!scriptptr) {
  3001. NV_ERROR(dev, "LVDS output init script not found\n");
  3002. return -ENOENT;
  3003. }
  3004. run_digital_op_script(dev, scriptptr, dcbent, head, bios->fp.dual_link);
  3005. return 0;
  3006. }
  3007. int call_lvds_script(struct drm_device *dev, struct dcb_entry *dcbent, int head, enum LVDS_script script, int pxclk)
  3008. {
  3009. /*
  3010. * LVDS operations are multiplexed in an effort to present a single API
  3011. * which works with two vastly differing underlying structures.
  3012. * This acts as the demux
  3013. */
  3014. struct drm_nouveau_private *dev_priv = dev->dev_private;
  3015. struct nvbios *bios = &dev_priv->vbios;
  3016. uint8_t lvds_ver = bios->data[bios->fp.lvdsmanufacturerpointer];
  3017. uint32_t sel_clk_binding, sel_clk;
  3018. int ret;
  3019. if (bios->fp.last_script_invoc == (script << 1 | head) || !lvds_ver ||
  3020. (lvds_ver >= 0x30 && script == LVDS_INIT))
  3021. return 0;
  3022. if (!bios->fp.lvds_init_run) {
  3023. bios->fp.lvds_init_run = true;
  3024. call_lvds_script(dev, dcbent, head, LVDS_INIT, pxclk);
  3025. }
  3026. if (script == LVDS_PANEL_ON && bios->fp.reset_after_pclk_change)
  3027. call_lvds_script(dev, dcbent, head, LVDS_RESET, pxclk);
  3028. if (script == LVDS_RESET && bios->fp.power_off_for_reset)
  3029. call_lvds_script(dev, dcbent, head, LVDS_PANEL_OFF, pxclk);
  3030. NV_TRACE(dev, "Calling LVDS script %d:\n", script);
  3031. /* don't let script change pll->head binding */
  3032. sel_clk_binding = bios_rd32(bios, NV_PRAMDAC_SEL_CLK) & 0x50000;
  3033. if (lvds_ver < 0x30)
  3034. ret = call_lvds_manufacturer_script(dev, dcbent, head, script);
  3035. else
  3036. ret = run_lvds_table(dev, dcbent, head, script, pxclk);
  3037. bios->fp.last_script_invoc = (script << 1 | head);
  3038. sel_clk = NVReadRAMDAC(dev, 0, NV_PRAMDAC_SEL_CLK) & ~0x50000;
  3039. NVWriteRAMDAC(dev, 0, NV_PRAMDAC_SEL_CLK, sel_clk | sel_clk_binding);
  3040. /* some scripts set a value in NV_PBUS_POWERCTRL_2 and break video overlay */
  3041. nvWriteMC(dev, NV_PBUS_POWERCTRL_2, 0);
  3042. return ret;
  3043. }
  3044. struct lvdstableheader {
  3045. uint8_t lvds_ver, headerlen, recordlen;
  3046. };
  3047. static int parse_lvds_manufacturer_table_header(struct drm_device *dev, struct nvbios *bios, struct lvdstableheader *lth)
  3048. {
  3049. /*
  3050. * BMP version (0xa) LVDS table has a simple header of version and
  3051. * record length. The BIT LVDS table has the typical BIT table header:
  3052. * version byte, header length byte, record length byte, and a byte for
  3053. * the maximum number of records that can be held in the table.
  3054. */
  3055. uint8_t lvds_ver, headerlen, recordlen;
  3056. memset(lth, 0, sizeof(struct lvdstableheader));
  3057. if (bios->fp.lvdsmanufacturerpointer == 0x0) {
  3058. NV_ERROR(dev, "Pointer to LVDS manufacturer table invalid\n");
  3059. return -EINVAL;
  3060. }
  3061. lvds_ver = bios->data[bios->fp.lvdsmanufacturerpointer];
  3062. switch (lvds_ver) {
  3063. case 0x0a: /* pre NV40 */
  3064. headerlen = 2;
  3065. recordlen = bios->data[bios->fp.lvdsmanufacturerpointer + 1];
  3066. break;
  3067. case 0x30: /* NV4x */
  3068. headerlen = bios->data[bios->fp.lvdsmanufacturerpointer + 1];
  3069. if (headerlen < 0x1f) {
  3070. NV_ERROR(dev, "LVDS table header not understood\n");
  3071. return -EINVAL;
  3072. }
  3073. recordlen = bios->data[bios->fp.lvdsmanufacturerpointer + 2];
  3074. break;
  3075. case 0x40: /* G80/G90 */
  3076. headerlen = bios->data[bios->fp.lvdsmanufacturerpointer + 1];
  3077. if (headerlen < 0x7) {
  3078. NV_ERROR(dev, "LVDS table header not understood\n");
  3079. return -EINVAL;
  3080. }
  3081. recordlen = bios->data[bios->fp.lvdsmanufacturerpointer + 2];
  3082. break;
  3083. default:
  3084. NV_ERROR(dev,
  3085. "LVDS table revision %d.%d not currently supported\n",
  3086. lvds_ver >> 4, lvds_ver & 0xf);
  3087. return -ENOSYS;
  3088. }
  3089. lth->lvds_ver = lvds_ver;
  3090. lth->headerlen = headerlen;
  3091. lth->recordlen = recordlen;
  3092. return 0;
  3093. }
  3094. static int
  3095. get_fp_strap(struct drm_device *dev, struct nvbios *bios)
  3096. {
  3097. struct drm_nouveau_private *dev_priv = dev->dev_private;
  3098. /*
  3099. * The fp strap is normally dictated by the "User Strap" in
  3100. * PEXTDEV_BOOT_0[20:16], but on BMP cards when bit 2 of the
  3101. * Internal_Flags struct at 0x48 is set, the user strap gets overriden
  3102. * by the PCI subsystem ID during POST, but not before the previous user
  3103. * strap has been committed to CR58 for CR57=0xf on head A, which may be
  3104. * read and used instead
  3105. */
  3106. if (bios->major_version < 5 && bios->data[0x48] & 0x4)
  3107. return NVReadVgaCrtc5758(dev, 0, 0xf) & 0xf;
  3108. if (dev_priv->card_type >= NV_50)
  3109. return (bios_rd32(bios, NV_PEXTDEV_BOOT_0) >> 24) & 0xf;
  3110. else
  3111. return (bios_rd32(bios, NV_PEXTDEV_BOOT_0) >> 16) & 0xf;
  3112. }
  3113. static int parse_fp_mode_table(struct drm_device *dev, struct nvbios *bios)
  3114. {
  3115. uint8_t *fptable;
  3116. uint8_t fptable_ver, headerlen = 0, recordlen, fpentries = 0xf, fpindex;
  3117. int ret, ofs, fpstrapping;
  3118. struct lvdstableheader lth;
  3119. if (bios->fp.fptablepointer == 0x0) {
  3120. /* Apple cards don't have the fp table; the laptops use DDC */
  3121. /* The table is also missing on some x86 IGPs */
  3122. #ifndef __powerpc__
  3123. NV_ERROR(dev, "Pointer to flat panel table invalid\n");
  3124. #endif
  3125. bios->digital_min_front_porch = 0x4b;
  3126. return 0;
  3127. }
  3128. fptable = &bios->data[bios->fp.fptablepointer];
  3129. fptable_ver = fptable[0];
  3130. switch (fptable_ver) {
  3131. /*
  3132. * BMP version 0x5.0x11 BIOSen have version 1 like tables, but no
  3133. * version field, and miss one of the spread spectrum/PWM bytes.
  3134. * This could affect early GF2Go parts (not seen any appropriate ROMs
  3135. * though). Here we assume that a version of 0x05 matches this case
  3136. * (combining with a BMP version check would be better), as the
  3137. * common case for the panel type field is 0x0005, and that is in
  3138. * fact what we are reading the first byte of.
  3139. */
  3140. case 0x05: /* some NV10, 11, 15, 16 */
  3141. recordlen = 42;
  3142. ofs = -1;
  3143. break;
  3144. case 0x10: /* some NV15/16, and NV11+ */
  3145. recordlen = 44;
  3146. ofs = 0;
  3147. break;
  3148. case 0x20: /* NV40+ */
  3149. headerlen = fptable[1];
  3150. recordlen = fptable[2];
  3151. fpentries = fptable[3];
  3152. /*
  3153. * fptable[4] is the minimum
  3154. * RAMDAC_FP_HCRTC -> RAMDAC_FP_HSYNC_START gap
  3155. */
  3156. bios->digital_min_front_porch = fptable[4];
  3157. ofs = -7;
  3158. break;
  3159. default:
  3160. NV_ERROR(dev,
  3161. "FP table revision %d.%d not currently supported\n",
  3162. fptable_ver >> 4, fptable_ver & 0xf);
  3163. return -ENOSYS;
  3164. }
  3165. if (!bios->is_mobile) /* !mobile only needs digital_min_front_porch */
  3166. return 0;
  3167. ret = parse_lvds_manufacturer_table_header(dev, bios, &lth);
  3168. if (ret)
  3169. return ret;
  3170. if (lth.lvds_ver == 0x30 || lth.lvds_ver == 0x40) {
  3171. bios->fp.fpxlatetableptr = bios->fp.lvdsmanufacturerpointer +
  3172. lth.headerlen + 1;
  3173. bios->fp.xlatwidth = lth.recordlen;
  3174. }
  3175. if (bios->fp.fpxlatetableptr == 0x0) {
  3176. NV_ERROR(dev, "Pointer to flat panel xlat table invalid\n");
  3177. return -EINVAL;
  3178. }
  3179. fpstrapping = get_fp_strap(dev, bios);
  3180. fpindex = bios->data[bios->fp.fpxlatetableptr +
  3181. fpstrapping * bios->fp.xlatwidth];
  3182. if (fpindex > fpentries) {
  3183. NV_ERROR(dev, "Bad flat panel table index\n");
  3184. return -ENOENT;
  3185. }
  3186. /* nv4x cards need both a strap value and fpindex of 0xf to use DDC */
  3187. if (lth.lvds_ver > 0x10)
  3188. bios->fp_no_ddc = fpstrapping != 0xf || fpindex != 0xf;
  3189. /*
  3190. * If either the strap or xlated fpindex value are 0xf there is no
  3191. * panel using a strap-derived bios mode present. this condition
  3192. * includes, but is different from, the DDC panel indicator above
  3193. */
  3194. if (fpstrapping == 0xf || fpindex == 0xf)
  3195. return 0;
  3196. bios->fp.mode_ptr = bios->fp.fptablepointer + headerlen +
  3197. recordlen * fpindex + ofs;
  3198. NV_TRACE(dev, "BIOS FP mode: %dx%d (%dkHz pixel clock)\n",
  3199. ROM16(bios->data[bios->fp.mode_ptr + 11]) + 1,
  3200. ROM16(bios->data[bios->fp.mode_ptr + 25]) + 1,
  3201. ROM16(bios->data[bios->fp.mode_ptr + 7]) * 10);
  3202. return 0;
  3203. }
  3204. bool nouveau_bios_fp_mode(struct drm_device *dev, struct drm_display_mode *mode)
  3205. {
  3206. struct drm_nouveau_private *dev_priv = dev->dev_private;
  3207. struct nvbios *bios = &dev_priv->vbios;
  3208. uint8_t *mode_entry = &bios->data[bios->fp.mode_ptr];
  3209. if (!mode) /* just checking whether we can produce a mode */
  3210. return bios->fp.mode_ptr;
  3211. memset(mode, 0, sizeof(struct drm_display_mode));
  3212. /*
  3213. * For version 1.0 (version in byte 0):
  3214. * bytes 1-2 are "panel type", including bits on whether Colour/mono,
  3215. * single/dual link, and type (TFT etc.)
  3216. * bytes 3-6 are bits per colour in RGBX
  3217. */
  3218. mode->clock = ROM16(mode_entry[7]) * 10;
  3219. /* bytes 9-10 is HActive */
  3220. mode->hdisplay = ROM16(mode_entry[11]) + 1;
  3221. /*
  3222. * bytes 13-14 is HValid Start
  3223. * bytes 15-16 is HValid End
  3224. */
  3225. mode->hsync_start = ROM16(mode_entry[17]) + 1;
  3226. mode->hsync_end = ROM16(mode_entry[19]) + 1;
  3227. mode->htotal = ROM16(mode_entry[21]) + 1;
  3228. /* bytes 23-24, 27-30 similarly, but vertical */
  3229. mode->vdisplay = ROM16(mode_entry[25]) + 1;
  3230. mode->vsync_start = ROM16(mode_entry[31]) + 1;
  3231. mode->vsync_end = ROM16(mode_entry[33]) + 1;
  3232. mode->vtotal = ROM16(mode_entry[35]) + 1;
  3233. mode->flags |= (mode_entry[37] & 0x10) ?
  3234. DRM_MODE_FLAG_PHSYNC : DRM_MODE_FLAG_NHSYNC;
  3235. mode->flags |= (mode_entry[37] & 0x1) ?
  3236. DRM_MODE_FLAG_PVSYNC : DRM_MODE_FLAG_NVSYNC;
  3237. /*
  3238. * bytes 38-39 relate to spread spectrum settings
  3239. * bytes 40-43 are something to do with PWM
  3240. */
  3241. mode->status = MODE_OK;
  3242. mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
  3243. drm_mode_set_name(mode);
  3244. return bios->fp.mode_ptr;
  3245. }
  3246. int nouveau_bios_parse_lvds_table(struct drm_device *dev, int pxclk, bool *dl, bool *if_is_24bit)
  3247. {
  3248. /*
  3249. * The LVDS table header is (mostly) described in
  3250. * parse_lvds_manufacturer_table_header(): the BIT header additionally
  3251. * contains the dual-link transition pxclk (in 10s kHz), at byte 5 - if
  3252. * straps are not being used for the panel, this specifies the frequency
  3253. * at which modes should be set up in the dual link style.
  3254. *
  3255. * Following the header, the BMP (ver 0xa) table has several records,
  3256. * indexed by a separate xlat table, indexed in turn by the fp strap in
  3257. * EXTDEV_BOOT. Each record had a config byte, followed by 6 script
  3258. * numbers for use by INIT_SUB which controlled panel init and power,
  3259. * and finally a dword of ms to sleep between power off and on
  3260. * operations.
  3261. *
  3262. * In the BIT versions, the table following the header serves as an
  3263. * integrated config and xlat table: the records in the table are
  3264. * indexed by the FP strap nibble in EXTDEV_BOOT, and each record has
  3265. * two bytes - the first as a config byte, the second for indexing the
  3266. * fp mode table pointed to by the BIT 'D' table
  3267. *
  3268. * DDC is not used until after card init, so selecting the correct table
  3269. * entry and setting the dual link flag for EDID equipped panels,
  3270. * requiring tests against the native-mode pixel clock, cannot be done
  3271. * until later, when this function should be called with non-zero pxclk
  3272. */
  3273. struct drm_nouveau_private *dev_priv = dev->dev_private;
  3274. struct nvbios *bios = &dev_priv->vbios;
  3275. int fpstrapping = get_fp_strap(dev, bios), lvdsmanufacturerindex = 0;
  3276. struct lvdstableheader lth;
  3277. uint16_t lvdsofs;
  3278. int ret, chip_version = bios->chip_version;
  3279. ret = parse_lvds_manufacturer_table_header(dev, bios, &lth);
  3280. if (ret)
  3281. return ret;
  3282. switch (lth.lvds_ver) {
  3283. case 0x0a: /* pre NV40 */
  3284. lvdsmanufacturerindex = bios->data[
  3285. bios->fp.fpxlatemanufacturertableptr +
  3286. fpstrapping];
  3287. /* we're done if this isn't the EDID panel case */
  3288. if (!pxclk)
  3289. break;
  3290. if (chip_version < 0x25) {
  3291. /* nv17 behaviour
  3292. *
  3293. * It seems the old style lvds script pointer is reused
  3294. * to select 18/24 bit colour depth for EDID panels.
  3295. */
  3296. lvdsmanufacturerindex =
  3297. (bios->legacy.lvds_single_a_script_ptr & 1) ?
  3298. 2 : 0;
  3299. if (pxclk >= bios->fp.duallink_transition_clk)
  3300. lvdsmanufacturerindex++;
  3301. } else if (chip_version < 0x30) {
  3302. /* nv28 behaviour (off-chip encoder)
  3303. *
  3304. * nv28 does a complex dance of first using byte 121 of
  3305. * the EDID to choose the lvdsmanufacturerindex, then
  3306. * later attempting to match the EDID manufacturer and
  3307. * product IDs in a table (signature 'pidt' (panel id
  3308. * table?)), setting an lvdsmanufacturerindex of 0 and
  3309. * an fp strap of the match index (or 0xf if none)
  3310. */
  3311. lvdsmanufacturerindex = 0;
  3312. } else {
  3313. /* nv31, nv34 behaviour */
  3314. lvdsmanufacturerindex = 0;
  3315. if (pxclk >= bios->fp.duallink_transition_clk)
  3316. lvdsmanufacturerindex = 2;
  3317. if (pxclk >= 140000)
  3318. lvdsmanufacturerindex = 3;
  3319. }
  3320. /*
  3321. * nvidia set the high nibble of (cr57=f, cr58) to
  3322. * lvdsmanufacturerindex in this case; we don't
  3323. */
  3324. break;
  3325. case 0x30: /* NV4x */
  3326. case 0x40: /* G80/G90 */
  3327. lvdsmanufacturerindex = fpstrapping;
  3328. break;
  3329. default:
  3330. NV_ERROR(dev, "LVDS table revision not currently supported\n");
  3331. return -ENOSYS;
  3332. }
  3333. lvdsofs = bios->fp.xlated_entry = bios->fp.lvdsmanufacturerpointer + lth.headerlen + lth.recordlen * lvdsmanufacturerindex;
  3334. switch (lth.lvds_ver) {
  3335. case 0x0a:
  3336. bios->fp.power_off_for_reset = bios->data[lvdsofs] & 1;
  3337. bios->fp.reset_after_pclk_change = bios->data[lvdsofs] & 2;
  3338. bios->fp.dual_link = bios->data[lvdsofs] & 4;
  3339. bios->fp.link_c_increment = bios->data[lvdsofs] & 8;
  3340. *if_is_24bit = bios->data[lvdsofs] & 16;
  3341. break;
  3342. case 0x30:
  3343. case 0x40:
  3344. /*
  3345. * No sign of the "power off for reset" or "reset for panel
  3346. * on" bits, but it's safer to assume we should
  3347. */
  3348. bios->fp.power_off_for_reset = true;
  3349. bios->fp.reset_after_pclk_change = true;
  3350. /*
  3351. * It's ok lvdsofs is wrong for nv4x edid case; dual_link is
  3352. * over-written, and if_is_24bit isn't used
  3353. */
  3354. bios->fp.dual_link = bios->data[lvdsofs] & 1;
  3355. bios->fp.if_is_24bit = bios->data[lvdsofs] & 2;
  3356. bios->fp.strapless_is_24bit = bios->data[bios->fp.lvdsmanufacturerpointer + 4];
  3357. bios->fp.duallink_transition_clk = ROM16(bios->data[bios->fp.lvdsmanufacturerpointer + 5]) * 10;
  3358. break;
  3359. }
  3360. /* set dual_link flag for EDID case */
  3361. if (pxclk && (chip_version < 0x25 || chip_version > 0x28))
  3362. bios->fp.dual_link = (pxclk >= bios->fp.duallink_transition_clk);
  3363. *dl = bios->fp.dual_link;
  3364. return 0;
  3365. }
  3366. /* BIT 'U'/'d' table encoder subtables have hashes matching them to
  3367. * a particular set of encoders.
  3368. *
  3369. * This function returns true if a particular DCB entry matches.
  3370. */
  3371. bool
  3372. bios_encoder_match(struct dcb_entry *dcb, u32 hash)
  3373. {
  3374. if ((hash & 0x000000f0) != (dcb->location << 4))
  3375. return false;
  3376. if ((hash & 0x0000000f) != dcb->type)
  3377. return false;
  3378. if (!(hash & (dcb->or << 16)))
  3379. return false;
  3380. switch (dcb->type) {
  3381. case OUTPUT_TMDS:
  3382. case OUTPUT_LVDS:
  3383. case OUTPUT_DP:
  3384. if (hash & 0x00c00000) {
  3385. if (!(hash & (dcb->sorconf.link << 22)))
  3386. return false;
  3387. }
  3388. default:
  3389. return true;
  3390. }
  3391. }
  3392. int
  3393. nouveau_bios_run_display_table(struct drm_device *dev, u16 type, int pclk,
  3394. struct dcb_entry *dcbent, int crtc)
  3395. {
  3396. /*
  3397. * The display script table is located by the BIT 'U' table.
  3398. *
  3399. * It contains an array of pointers to various tables describing
  3400. * a particular output type. The first 32-bits of the output
  3401. * tables contains similar information to a DCB entry, and is
  3402. * used to decide whether that particular table is suitable for
  3403. * the output you want to access.
  3404. *
  3405. * The "record header length" field here seems to indicate the
  3406. * offset of the first configuration entry in the output tables.
  3407. * This is 10 on most cards I've seen, but 12 has been witnessed
  3408. * on DP cards, and there's another script pointer within the
  3409. * header.
  3410. *
  3411. * offset + 0 ( 8 bits): version
  3412. * offset + 1 ( 8 bits): header length
  3413. * offset + 2 ( 8 bits): record length
  3414. * offset + 3 ( 8 bits): number of records
  3415. * offset + 4 ( 8 bits): record header length
  3416. * offset + 5 (16 bits): pointer to first output script table
  3417. */
  3418. struct drm_nouveau_private *dev_priv = dev->dev_private;
  3419. struct nvbios *bios = &dev_priv->vbios;
  3420. uint8_t *table = &bios->data[bios->display.script_table_ptr];
  3421. uint8_t *otable = NULL;
  3422. uint16_t script;
  3423. int i;
  3424. if (!bios->display.script_table_ptr) {
  3425. NV_ERROR(dev, "No pointer to output script table\n");
  3426. return 1;
  3427. }
  3428. /*
  3429. * Nothing useful has been in any of the pre-2.0 tables I've seen,
  3430. * so until they are, we really don't need to care.
  3431. */
  3432. if (table[0] < 0x20)
  3433. return 1;
  3434. if (table[0] != 0x20 && table[0] != 0x21) {
  3435. NV_ERROR(dev, "Output script table version 0x%02x unknown\n",
  3436. table[0]);
  3437. return 1;
  3438. }
  3439. /*
  3440. * The output script tables describing a particular output type
  3441. * look as follows:
  3442. *
  3443. * offset + 0 (32 bits): output this table matches (hash of DCB)
  3444. * offset + 4 ( 8 bits): unknown
  3445. * offset + 5 ( 8 bits): number of configurations
  3446. * offset + 6 (16 bits): pointer to some script
  3447. * offset + 8 (16 bits): pointer to some script
  3448. *
  3449. * headerlen == 10
  3450. * offset + 10 : configuration 0
  3451. *
  3452. * headerlen == 12
  3453. * offset + 10 : pointer to some script
  3454. * offset + 12 : configuration 0
  3455. *
  3456. * Each config entry is as follows:
  3457. *
  3458. * offset + 0 (16 bits): unknown, assumed to be a match value
  3459. * offset + 2 (16 bits): pointer to script table (clock set?)
  3460. * offset + 4 (16 bits): pointer to script table (reset?)
  3461. *
  3462. * There doesn't appear to be a count value to say how many
  3463. * entries exist in each script table, instead, a 0 value in
  3464. * the first 16-bit word seems to indicate both the end of the
  3465. * list and the default entry. The second 16-bit word in the
  3466. * script tables is a pointer to the script to execute.
  3467. */
  3468. NV_DEBUG_KMS(dev, "Searching for output entry for %d %d %d\n",
  3469. dcbent->type, dcbent->location, dcbent->or);
  3470. for (i = 0; i < table[3]; i++) {
  3471. otable = ROMPTR(dev, table[table[1] + (i * table[2])]);
  3472. if (otable && bios_encoder_match(dcbent, ROM32(otable[0])))
  3473. break;
  3474. }
  3475. if (!otable) {
  3476. NV_DEBUG_KMS(dev, "failed to match any output table\n");
  3477. return 1;
  3478. }
  3479. if (pclk < -2 || pclk > 0) {
  3480. /* Try to find matching script table entry */
  3481. for (i = 0; i < otable[5]; i++) {
  3482. if (ROM16(otable[table[4] + i*6]) == type)
  3483. break;
  3484. }
  3485. if (i == otable[5]) {
  3486. NV_ERROR(dev, "Table 0x%04x not found for %d/%d, "
  3487. "using first\n",
  3488. type, dcbent->type, dcbent->or);
  3489. i = 0;
  3490. }
  3491. }
  3492. if (pclk == 0) {
  3493. script = ROM16(otable[6]);
  3494. if (!script) {
  3495. NV_DEBUG_KMS(dev, "output script 0 not found\n");
  3496. return 1;
  3497. }
  3498. NV_DEBUG_KMS(dev, "0x%04X: parsing output script 0\n", script);
  3499. nouveau_bios_run_init_table(dev, script, dcbent, crtc);
  3500. } else
  3501. if (pclk == -1) {
  3502. script = ROM16(otable[8]);
  3503. if (!script) {
  3504. NV_DEBUG_KMS(dev, "output script 1 not found\n");
  3505. return 1;
  3506. }
  3507. NV_DEBUG_KMS(dev, "0x%04X: parsing output script 1\n", script);
  3508. nouveau_bios_run_init_table(dev, script, dcbent, crtc);
  3509. } else
  3510. if (pclk == -2) {
  3511. if (table[4] >= 12)
  3512. script = ROM16(otable[10]);
  3513. else
  3514. script = 0;
  3515. if (!script) {
  3516. NV_DEBUG_KMS(dev, "output script 2 not found\n");
  3517. return 1;
  3518. }
  3519. NV_DEBUG_KMS(dev, "0x%04X: parsing output script 2\n", script);
  3520. nouveau_bios_run_init_table(dev, script, dcbent, crtc);
  3521. } else
  3522. if (pclk > 0) {
  3523. script = ROM16(otable[table[4] + i*6 + 2]);
  3524. if (script)
  3525. script = clkcmptable(bios, script, pclk);
  3526. if (!script) {
  3527. NV_DEBUG_KMS(dev, "clock script 0 not found\n");
  3528. return 1;
  3529. }
  3530. NV_DEBUG_KMS(dev, "0x%04X: parsing clock script 0\n", script);
  3531. nouveau_bios_run_init_table(dev, script, dcbent, crtc);
  3532. } else
  3533. if (pclk < 0) {
  3534. script = ROM16(otable[table[4] + i*6 + 4]);
  3535. if (script)
  3536. script = clkcmptable(bios, script, -pclk);
  3537. if (!script) {
  3538. NV_DEBUG_KMS(dev, "clock script 1 not found\n");
  3539. return 1;
  3540. }
  3541. NV_DEBUG_KMS(dev, "0x%04X: parsing clock script 1\n", script);
  3542. nouveau_bios_run_init_table(dev, script, dcbent, crtc);
  3543. }
  3544. return 0;
  3545. }
  3546. int run_tmds_table(struct drm_device *dev, struct dcb_entry *dcbent, int head, int pxclk)
  3547. {
  3548. /*
  3549. * the pxclk parameter is in kHz
  3550. *
  3551. * This runs the TMDS regs setting code found on BIT bios cards
  3552. *
  3553. * For ffs(or) == 1 use the first table, for ffs(or) == 2 and
  3554. * ffs(or) == 3, use the second.
  3555. */
  3556. struct drm_nouveau_private *dev_priv = dev->dev_private;
  3557. struct nvbios *bios = &dev_priv->vbios;
  3558. int cv = bios->chip_version;
  3559. uint16_t clktable = 0, scriptptr;
  3560. uint32_t sel_clk_binding, sel_clk;
  3561. /* pre-nv17 off-chip tmds uses scripts, post nv17 doesn't */
  3562. if (cv >= 0x17 && cv != 0x1a && cv != 0x20 &&
  3563. dcbent->location != DCB_LOC_ON_CHIP)
  3564. return 0;
  3565. switch (ffs(dcbent->or)) {
  3566. case 1:
  3567. clktable = bios->tmds.output0_script_ptr;
  3568. break;
  3569. case 2:
  3570. case 3:
  3571. clktable = bios->tmds.output1_script_ptr;
  3572. break;
  3573. }
  3574. if (!clktable) {
  3575. NV_ERROR(dev, "Pixel clock comparison table not found\n");
  3576. return -EINVAL;
  3577. }
  3578. scriptptr = clkcmptable(bios, clktable, pxclk);
  3579. if (!scriptptr) {
  3580. NV_ERROR(dev, "TMDS output init script not found\n");
  3581. return -ENOENT;
  3582. }
  3583. /* don't let script change pll->head binding */
  3584. sel_clk_binding = bios_rd32(bios, NV_PRAMDAC_SEL_CLK) & 0x50000;
  3585. run_digital_op_script(dev, scriptptr, dcbent, head, pxclk >= 165000);
  3586. sel_clk = NVReadRAMDAC(dev, 0, NV_PRAMDAC_SEL_CLK) & ~0x50000;
  3587. NVWriteRAMDAC(dev, 0, NV_PRAMDAC_SEL_CLK, sel_clk | sel_clk_binding);
  3588. return 0;
  3589. }
  3590. struct pll_mapping {
  3591. u8 type;
  3592. u32 reg;
  3593. };
  3594. static struct pll_mapping nv04_pll_mapping[] = {
  3595. { PLL_CORE , NV_PRAMDAC_NVPLL_COEFF },
  3596. { PLL_MEMORY, NV_PRAMDAC_MPLL_COEFF },
  3597. { PLL_VPLL0 , NV_PRAMDAC_VPLL_COEFF },
  3598. { PLL_VPLL1 , NV_RAMDAC_VPLL2 },
  3599. {}
  3600. };
  3601. static struct pll_mapping nv40_pll_mapping[] = {
  3602. { PLL_CORE , 0x004000 },
  3603. { PLL_MEMORY, 0x004020 },
  3604. { PLL_VPLL0 , NV_PRAMDAC_VPLL_COEFF },
  3605. { PLL_VPLL1 , NV_RAMDAC_VPLL2 },
  3606. {}
  3607. };
  3608. static struct pll_mapping nv50_pll_mapping[] = {
  3609. { PLL_CORE , 0x004028 },
  3610. { PLL_SHADER, 0x004020 },
  3611. { PLL_UNK03 , 0x004000 },
  3612. { PLL_MEMORY, 0x004008 },
  3613. { PLL_UNK40 , 0x00e810 },
  3614. { PLL_UNK41 , 0x00e818 },
  3615. { PLL_UNK42 , 0x00e824 },
  3616. { PLL_VPLL0 , 0x614100 },
  3617. { PLL_VPLL1 , 0x614900 },
  3618. {}
  3619. };
  3620. static struct pll_mapping nv84_pll_mapping[] = {
  3621. { PLL_CORE , 0x004028 },
  3622. { PLL_SHADER, 0x004020 },
  3623. { PLL_MEMORY, 0x004008 },
  3624. { PLL_VDEC , 0x004030 },
  3625. { PLL_UNK41 , 0x00e818 },
  3626. { PLL_VPLL0 , 0x614100 },
  3627. { PLL_VPLL1 , 0x614900 },
  3628. {}
  3629. };
  3630. u32
  3631. get_pll_register(struct drm_device *dev, enum pll_types type)
  3632. {
  3633. struct drm_nouveau_private *dev_priv = dev->dev_private;
  3634. struct nvbios *bios = &dev_priv->vbios;
  3635. struct pll_mapping *map;
  3636. int i;
  3637. if (dev_priv->card_type < NV_40)
  3638. map = nv04_pll_mapping;
  3639. else
  3640. if (dev_priv->card_type < NV_50)
  3641. map = nv40_pll_mapping;
  3642. else {
  3643. u8 *plim = &bios->data[bios->pll_limit_tbl_ptr];
  3644. if (plim[0] >= 0x30) {
  3645. u8 *entry = plim + plim[1];
  3646. for (i = 0; i < plim[3]; i++, entry += plim[2]) {
  3647. if (entry[0] == type)
  3648. return ROM32(entry[3]);
  3649. }
  3650. return 0;
  3651. }
  3652. if (dev_priv->chipset == 0x50)
  3653. map = nv50_pll_mapping;
  3654. else
  3655. map = nv84_pll_mapping;
  3656. }
  3657. while (map->reg) {
  3658. if (map->type == type)
  3659. return map->reg;
  3660. map++;
  3661. }
  3662. return 0;
  3663. }
  3664. int get_pll_limits(struct drm_device *dev, uint32_t limit_match, struct pll_lims *pll_lim)
  3665. {
  3666. /*
  3667. * PLL limits table
  3668. *
  3669. * Version 0x10: NV30, NV31
  3670. * One byte header (version), one record of 24 bytes
  3671. * Version 0x11: NV36 - Not implemented
  3672. * Seems to have same record style as 0x10, but 3 records rather than 1
  3673. * Version 0x20: Found on Geforce 6 cards
  3674. * Trivial 4 byte BIT header. 31 (0x1f) byte record length
  3675. * Version 0x21: Found on Geforce 7, 8 and some Geforce 6 cards
  3676. * 5 byte header, fifth byte of unknown purpose. 35 (0x23) byte record
  3677. * length in general, some (integrated) have an extra configuration byte
  3678. * Version 0x30: Found on Geforce 8, separates the register mapping
  3679. * from the limits tables.
  3680. */
  3681. struct drm_nouveau_private *dev_priv = dev->dev_private;
  3682. struct nvbios *bios = &dev_priv->vbios;
  3683. int cv = bios->chip_version, pllindex = 0;
  3684. uint8_t pll_lim_ver = 0, headerlen = 0, recordlen = 0, entries = 0;
  3685. uint32_t crystal_strap_mask, crystal_straps;
  3686. if (!bios->pll_limit_tbl_ptr) {
  3687. if (cv == 0x30 || cv == 0x31 || cv == 0x35 || cv == 0x36 ||
  3688. cv >= 0x40) {
  3689. NV_ERROR(dev, "Pointer to PLL limits table invalid\n");
  3690. return -EINVAL;
  3691. }
  3692. } else
  3693. pll_lim_ver = bios->data[bios->pll_limit_tbl_ptr];
  3694. crystal_strap_mask = 1 << 6;
  3695. /* open coded dev->twoHeads test */
  3696. if (cv > 0x10 && cv != 0x15 && cv != 0x1a && cv != 0x20)
  3697. crystal_strap_mask |= 1 << 22;
  3698. crystal_straps = nvReadEXTDEV(dev, NV_PEXTDEV_BOOT_0) &
  3699. crystal_strap_mask;
  3700. switch (pll_lim_ver) {
  3701. /*
  3702. * We use version 0 to indicate a pre limit table bios (single stage
  3703. * pll) and load the hard coded limits instead.
  3704. */
  3705. case 0:
  3706. break;
  3707. case 0x10:
  3708. case 0x11:
  3709. /*
  3710. * Strictly v0x11 has 3 entries, but the last two don't seem
  3711. * to get used.
  3712. */
  3713. headerlen = 1;
  3714. recordlen = 0x18;
  3715. entries = 1;
  3716. pllindex = 0;
  3717. break;
  3718. case 0x20:
  3719. case 0x21:
  3720. case 0x30:
  3721. case 0x40:
  3722. headerlen = bios->data[bios->pll_limit_tbl_ptr + 1];
  3723. recordlen = bios->data[bios->pll_limit_tbl_ptr + 2];
  3724. entries = bios->data[bios->pll_limit_tbl_ptr + 3];
  3725. break;
  3726. default:
  3727. NV_ERROR(dev, "PLL limits table revision 0x%X not currently "
  3728. "supported\n", pll_lim_ver);
  3729. return -ENOSYS;
  3730. }
  3731. /* initialize all members to zero */
  3732. memset(pll_lim, 0, sizeof(struct pll_lims));
  3733. /* if we were passed a type rather than a register, figure
  3734. * out the register and store it
  3735. */
  3736. if (limit_match > PLL_MAX)
  3737. pll_lim->reg = limit_match;
  3738. else {
  3739. pll_lim->reg = get_pll_register(dev, limit_match);
  3740. if (!pll_lim->reg)
  3741. return -ENOENT;
  3742. }
  3743. if (pll_lim_ver == 0x10 || pll_lim_ver == 0x11) {
  3744. uint8_t *pll_rec = &bios->data[bios->pll_limit_tbl_ptr + headerlen + recordlen * pllindex];
  3745. pll_lim->vco1.minfreq = ROM32(pll_rec[0]);
  3746. pll_lim->vco1.maxfreq = ROM32(pll_rec[4]);
  3747. pll_lim->vco2.minfreq = ROM32(pll_rec[8]);
  3748. pll_lim->vco2.maxfreq = ROM32(pll_rec[12]);
  3749. pll_lim->vco1.min_inputfreq = ROM32(pll_rec[16]);
  3750. pll_lim->vco2.min_inputfreq = ROM32(pll_rec[20]);
  3751. pll_lim->vco1.max_inputfreq = pll_lim->vco2.max_inputfreq = INT_MAX;
  3752. /* these values taken from nv30/31/36 */
  3753. pll_lim->vco1.min_n = 0x1;
  3754. if (cv == 0x36)
  3755. pll_lim->vco1.min_n = 0x5;
  3756. pll_lim->vco1.max_n = 0xff;
  3757. pll_lim->vco1.min_m = 0x1;
  3758. pll_lim->vco1.max_m = 0xd;
  3759. pll_lim->vco2.min_n = 0x4;
  3760. /*
  3761. * On nv30, 31, 36 (i.e. all cards with two stage PLLs with this
  3762. * table version (apart from nv35)), N2 is compared to
  3763. * maxN2 (0x46) and 10 * maxM2 (0x4), so set maxN2 to 0x28 and
  3764. * save a comparison
  3765. */
  3766. pll_lim->vco2.max_n = 0x28;
  3767. if (cv == 0x30 || cv == 0x35)
  3768. /* only 5 bits available for N2 on nv30/35 */
  3769. pll_lim->vco2.max_n = 0x1f;
  3770. pll_lim->vco2.min_m = 0x1;
  3771. pll_lim->vco2.max_m = 0x4;
  3772. pll_lim->max_log2p = 0x7;
  3773. pll_lim->max_usable_log2p = 0x6;
  3774. } else if (pll_lim_ver == 0x20 || pll_lim_ver == 0x21) {
  3775. uint16_t plloffs = bios->pll_limit_tbl_ptr + headerlen;
  3776. uint8_t *pll_rec;
  3777. int i;
  3778. /*
  3779. * First entry is default match, if nothing better. warn if
  3780. * reg field nonzero
  3781. */
  3782. if (ROM32(bios->data[plloffs]))
  3783. NV_WARN(dev, "Default PLL limit entry has non-zero "
  3784. "register field\n");
  3785. for (i = 1; i < entries; i++)
  3786. if (ROM32(bios->data[plloffs + recordlen * i]) == pll_lim->reg) {
  3787. pllindex = i;
  3788. break;
  3789. }
  3790. if ((dev_priv->card_type >= NV_50) && (pllindex == 0)) {
  3791. NV_ERROR(dev, "Register 0x%08x not found in PLL "
  3792. "limits table", pll_lim->reg);
  3793. return -ENOENT;
  3794. }
  3795. pll_rec = &bios->data[plloffs + recordlen * pllindex];
  3796. BIOSLOG(bios, "Loading PLL limits for reg 0x%08x\n",
  3797. pllindex ? pll_lim->reg : 0);
  3798. /*
  3799. * Frequencies are stored in tables in MHz, kHz are more
  3800. * useful, so we convert.
  3801. */
  3802. /* What output frequencies can each VCO generate? */
  3803. pll_lim->vco1.minfreq = ROM16(pll_rec[4]) * 1000;
  3804. pll_lim->vco1.maxfreq = ROM16(pll_rec[6]) * 1000;
  3805. pll_lim->vco2.minfreq = ROM16(pll_rec[8]) * 1000;
  3806. pll_lim->vco2.maxfreq = ROM16(pll_rec[10]) * 1000;
  3807. /* What input frequencies they accept (past the m-divider)? */
  3808. pll_lim->vco1.min_inputfreq = ROM16(pll_rec[12]) * 1000;
  3809. pll_lim->vco2.min_inputfreq = ROM16(pll_rec[14]) * 1000;
  3810. pll_lim->vco1.max_inputfreq = ROM16(pll_rec[16]) * 1000;
  3811. pll_lim->vco2.max_inputfreq = ROM16(pll_rec[18]) * 1000;
  3812. /* What values are accepted as multiplier and divider? */
  3813. pll_lim->vco1.min_n = pll_rec[20];
  3814. pll_lim->vco1.max_n = pll_rec[21];
  3815. pll_lim->vco1.min_m = pll_rec[22];
  3816. pll_lim->vco1.max_m = pll_rec[23];
  3817. pll_lim->vco2.min_n = pll_rec[24];
  3818. pll_lim->vco2.max_n = pll_rec[25];
  3819. pll_lim->vco2.min_m = pll_rec[26];
  3820. pll_lim->vco2.max_m = pll_rec[27];
  3821. pll_lim->max_usable_log2p = pll_lim->max_log2p = pll_rec[29];
  3822. if (pll_lim->max_log2p > 0x7)
  3823. /* pll decoding in nv_hw.c assumes never > 7 */
  3824. NV_WARN(dev, "Max log2 P value greater than 7 (%d)\n",
  3825. pll_lim->max_log2p);
  3826. if (cv < 0x60)
  3827. pll_lim->max_usable_log2p = 0x6;
  3828. pll_lim->log2p_bias = pll_rec[30];
  3829. if (recordlen > 0x22)
  3830. pll_lim->refclk = ROM32(pll_rec[31]);
  3831. if (recordlen > 0x23 && pll_rec[35])
  3832. NV_WARN(dev,
  3833. "Bits set in PLL configuration byte (%x)\n",
  3834. pll_rec[35]);
  3835. /* C51 special not seen elsewhere */
  3836. if (cv == 0x51 && !pll_lim->refclk) {
  3837. uint32_t sel_clk = bios_rd32(bios, NV_PRAMDAC_SEL_CLK);
  3838. if ((pll_lim->reg == NV_PRAMDAC_VPLL_COEFF && sel_clk & 0x20) ||
  3839. (pll_lim->reg == NV_RAMDAC_VPLL2 && sel_clk & 0x80)) {
  3840. if (bios_idxprt_rd(bios, NV_CIO_CRX__COLOR, NV_CIO_CRE_CHIP_ID_INDEX) < 0xa3)
  3841. pll_lim->refclk = 200000;
  3842. else
  3843. pll_lim->refclk = 25000;
  3844. }
  3845. }
  3846. } else if (pll_lim_ver == 0x30) { /* ver 0x30 */
  3847. uint8_t *entry = &bios->data[bios->pll_limit_tbl_ptr + headerlen];
  3848. uint8_t *record = NULL;
  3849. int i;
  3850. BIOSLOG(bios, "Loading PLL limits for register 0x%08x\n",
  3851. pll_lim->reg);
  3852. for (i = 0; i < entries; i++, entry += recordlen) {
  3853. if (ROM32(entry[3]) == pll_lim->reg) {
  3854. record = &bios->data[ROM16(entry[1])];
  3855. break;
  3856. }
  3857. }
  3858. if (!record) {
  3859. NV_ERROR(dev, "Register 0x%08x not found in PLL "
  3860. "limits table", pll_lim->reg);
  3861. return -ENOENT;
  3862. }
  3863. pll_lim->vco1.minfreq = ROM16(record[0]) * 1000;
  3864. pll_lim->vco1.maxfreq = ROM16(record[2]) * 1000;
  3865. pll_lim->vco2.minfreq = ROM16(record[4]) * 1000;
  3866. pll_lim->vco2.maxfreq = ROM16(record[6]) * 1000;
  3867. pll_lim->vco1.min_inputfreq = ROM16(record[8]) * 1000;
  3868. pll_lim->vco2.min_inputfreq = ROM16(record[10]) * 1000;
  3869. pll_lim->vco1.max_inputfreq = ROM16(record[12]) * 1000;
  3870. pll_lim->vco2.max_inputfreq = ROM16(record[14]) * 1000;
  3871. pll_lim->vco1.min_n = record[16];
  3872. pll_lim->vco1.max_n = record[17];
  3873. pll_lim->vco1.min_m = record[18];
  3874. pll_lim->vco1.max_m = record[19];
  3875. pll_lim->vco2.min_n = record[20];
  3876. pll_lim->vco2.max_n = record[21];
  3877. pll_lim->vco2.min_m = record[22];
  3878. pll_lim->vco2.max_m = record[23];
  3879. pll_lim->max_usable_log2p = pll_lim->max_log2p = record[25];
  3880. pll_lim->log2p_bias = record[27];
  3881. pll_lim->refclk = ROM32(record[28]);
  3882. } else if (pll_lim_ver) { /* ver 0x40 */
  3883. uint8_t *entry = &bios->data[bios->pll_limit_tbl_ptr + headerlen];
  3884. uint8_t *record = NULL;
  3885. int i;
  3886. BIOSLOG(bios, "Loading PLL limits for register 0x%08x\n",
  3887. pll_lim->reg);
  3888. for (i = 0; i < entries; i++, entry += recordlen) {
  3889. if (ROM32(entry[3]) == pll_lim->reg) {
  3890. record = &bios->data[ROM16(entry[1])];
  3891. break;
  3892. }
  3893. }
  3894. if (!record) {
  3895. NV_ERROR(dev, "Register 0x%08x not found in PLL "
  3896. "limits table", pll_lim->reg);
  3897. return -ENOENT;
  3898. }
  3899. pll_lim->vco1.minfreq = ROM16(record[0]) * 1000;
  3900. pll_lim->vco1.maxfreq = ROM16(record[2]) * 1000;
  3901. pll_lim->vco1.min_inputfreq = ROM16(record[4]) * 1000;
  3902. pll_lim->vco1.max_inputfreq = ROM16(record[6]) * 1000;
  3903. pll_lim->vco1.min_m = record[8];
  3904. pll_lim->vco1.max_m = record[9];
  3905. pll_lim->vco1.min_n = record[10];
  3906. pll_lim->vco1.max_n = record[11];
  3907. pll_lim->min_p = record[12];
  3908. pll_lim->max_p = record[13];
  3909. pll_lim->refclk = ROM16(entry[9]) * 1000;
  3910. }
  3911. /*
  3912. * By now any valid limit table ought to have set a max frequency for
  3913. * vco1, so if it's zero it's either a pre limit table bios, or one
  3914. * with an empty limit table (seen on nv18)
  3915. */
  3916. if (!pll_lim->vco1.maxfreq) {
  3917. pll_lim->vco1.minfreq = bios->fminvco;
  3918. pll_lim->vco1.maxfreq = bios->fmaxvco;
  3919. pll_lim->vco1.min_inputfreq = 0;
  3920. pll_lim->vco1.max_inputfreq = INT_MAX;
  3921. pll_lim->vco1.min_n = 0x1;
  3922. pll_lim->vco1.max_n = 0xff;
  3923. pll_lim->vco1.min_m = 0x1;
  3924. if (crystal_straps == 0) {
  3925. /* nv05 does this, nv11 doesn't, nv10 unknown */
  3926. if (cv < 0x11)
  3927. pll_lim->vco1.min_m = 0x7;
  3928. pll_lim->vco1.max_m = 0xd;
  3929. } else {
  3930. if (cv < 0x11)
  3931. pll_lim->vco1.min_m = 0x8;
  3932. pll_lim->vco1.max_m = 0xe;
  3933. }
  3934. if (cv < 0x17 || cv == 0x1a || cv == 0x20)
  3935. pll_lim->max_log2p = 4;
  3936. else
  3937. pll_lim->max_log2p = 5;
  3938. pll_lim->max_usable_log2p = pll_lim->max_log2p;
  3939. }
  3940. if (!pll_lim->refclk)
  3941. switch (crystal_straps) {
  3942. case 0:
  3943. pll_lim->refclk = 13500;
  3944. break;
  3945. case (1 << 6):
  3946. pll_lim->refclk = 14318;
  3947. break;
  3948. case (1 << 22):
  3949. pll_lim->refclk = 27000;
  3950. break;
  3951. case (1 << 22 | 1 << 6):
  3952. pll_lim->refclk = 25000;
  3953. break;
  3954. }
  3955. NV_DEBUG(dev, "pll.vco1.minfreq: %d\n", pll_lim->vco1.minfreq);
  3956. NV_DEBUG(dev, "pll.vco1.maxfreq: %d\n", pll_lim->vco1.maxfreq);
  3957. NV_DEBUG(dev, "pll.vco1.min_inputfreq: %d\n", pll_lim->vco1.min_inputfreq);
  3958. NV_DEBUG(dev, "pll.vco1.max_inputfreq: %d\n", pll_lim->vco1.max_inputfreq);
  3959. NV_DEBUG(dev, "pll.vco1.min_n: %d\n", pll_lim->vco1.min_n);
  3960. NV_DEBUG(dev, "pll.vco1.max_n: %d\n", pll_lim->vco1.max_n);
  3961. NV_DEBUG(dev, "pll.vco1.min_m: %d\n", pll_lim->vco1.min_m);
  3962. NV_DEBUG(dev, "pll.vco1.max_m: %d\n", pll_lim->vco1.max_m);
  3963. if (pll_lim->vco2.maxfreq) {
  3964. NV_DEBUG(dev, "pll.vco2.minfreq: %d\n", pll_lim->vco2.minfreq);
  3965. NV_DEBUG(dev, "pll.vco2.maxfreq: %d\n", pll_lim->vco2.maxfreq);
  3966. NV_DEBUG(dev, "pll.vco2.min_inputfreq: %d\n", pll_lim->vco2.min_inputfreq);
  3967. NV_DEBUG(dev, "pll.vco2.max_inputfreq: %d\n", pll_lim->vco2.max_inputfreq);
  3968. NV_DEBUG(dev, "pll.vco2.min_n: %d\n", pll_lim->vco2.min_n);
  3969. NV_DEBUG(dev, "pll.vco2.max_n: %d\n", pll_lim->vco2.max_n);
  3970. NV_DEBUG(dev, "pll.vco2.min_m: %d\n", pll_lim->vco2.min_m);
  3971. NV_DEBUG(dev, "pll.vco2.max_m: %d\n", pll_lim->vco2.max_m);
  3972. }
  3973. if (!pll_lim->max_p) {
  3974. NV_DEBUG(dev, "pll.max_log2p: %d\n", pll_lim->max_log2p);
  3975. NV_DEBUG(dev, "pll.log2p_bias: %d\n", pll_lim->log2p_bias);
  3976. } else {
  3977. NV_DEBUG(dev, "pll.min_p: %d\n", pll_lim->min_p);
  3978. NV_DEBUG(dev, "pll.max_p: %d\n", pll_lim->max_p);
  3979. }
  3980. NV_DEBUG(dev, "pll.refclk: %d\n", pll_lim->refclk);
  3981. return 0;
  3982. }
  3983. static void parse_bios_version(struct drm_device *dev, struct nvbios *bios, uint16_t offset)
  3984. {
  3985. /*
  3986. * offset + 0 (8 bits): Micro version
  3987. * offset + 1 (8 bits): Minor version
  3988. * offset + 2 (8 bits): Chip version
  3989. * offset + 3 (8 bits): Major version
  3990. */
  3991. bios->major_version = bios->data[offset + 3];
  3992. bios->chip_version = bios->data[offset + 2];
  3993. NV_TRACE(dev, "Bios version %02x.%02x.%02x.%02x\n",
  3994. bios->data[offset + 3], bios->data[offset + 2],
  3995. bios->data[offset + 1], bios->data[offset]);
  3996. }
  3997. static void parse_script_table_pointers(struct nvbios *bios, uint16_t offset)
  3998. {
  3999. /*
  4000. * Parses the init table segment for pointers used in script execution.
  4001. *
  4002. * offset + 0 (16 bits): init script tables pointer
  4003. * offset + 2 (16 bits): macro index table pointer
  4004. * offset + 4 (16 bits): macro table pointer
  4005. * offset + 6 (16 bits): condition table pointer
  4006. * offset + 8 (16 bits): io condition table pointer
  4007. * offset + 10 (16 bits): io flag condition table pointer
  4008. * offset + 12 (16 bits): init function table pointer
  4009. */
  4010. bios->init_script_tbls_ptr = ROM16(bios->data[offset]);
  4011. bios->macro_index_tbl_ptr = ROM16(bios->data[offset + 2]);
  4012. bios->macro_tbl_ptr = ROM16(bios->data[offset + 4]);
  4013. bios->condition_tbl_ptr = ROM16(bios->data[offset + 6]);
  4014. bios->io_condition_tbl_ptr = ROM16(bios->data[offset + 8]);
  4015. bios->io_flag_condition_tbl_ptr = ROM16(bios->data[offset + 10]);
  4016. bios->init_function_tbl_ptr = ROM16(bios->data[offset + 12]);
  4017. }
  4018. static int parse_bit_A_tbl_entry(struct drm_device *dev, struct nvbios *bios, struct bit_entry *bitentry)
  4019. {
  4020. /*
  4021. * Parses the load detect values for g80 cards.
  4022. *
  4023. * offset + 0 (16 bits): loadval table pointer
  4024. */
  4025. uint16_t load_table_ptr;
  4026. uint8_t version, headerlen, entrylen, num_entries;
  4027. if (bitentry->length != 3) {
  4028. NV_ERROR(dev, "Do not understand BIT A table\n");
  4029. return -EINVAL;
  4030. }
  4031. load_table_ptr = ROM16(bios->data[bitentry->offset]);
  4032. if (load_table_ptr == 0x0) {
  4033. NV_DEBUG(dev, "Pointer to BIT loadval table invalid\n");
  4034. return -EINVAL;
  4035. }
  4036. version = bios->data[load_table_ptr];
  4037. if (version != 0x10) {
  4038. NV_ERROR(dev, "BIT loadval table version %d.%d not supported\n",
  4039. version >> 4, version & 0xF);
  4040. return -ENOSYS;
  4041. }
  4042. headerlen = bios->data[load_table_ptr + 1];
  4043. entrylen = bios->data[load_table_ptr + 2];
  4044. num_entries = bios->data[load_table_ptr + 3];
  4045. if (headerlen != 4 || entrylen != 4 || num_entries != 2) {
  4046. NV_ERROR(dev, "Do not understand BIT loadval table\n");
  4047. return -EINVAL;
  4048. }
  4049. /* First entry is normal dac, 2nd tv-out perhaps? */
  4050. bios->dactestval = ROM32(bios->data[load_table_ptr + headerlen]) & 0x3ff;
  4051. return 0;
  4052. }
  4053. static int parse_bit_C_tbl_entry(struct drm_device *dev, struct nvbios *bios, struct bit_entry *bitentry)
  4054. {
  4055. /*
  4056. * offset + 8 (16 bits): PLL limits table pointer
  4057. *
  4058. * There's more in here, but that's unknown.
  4059. */
  4060. if (bitentry->length < 10) {
  4061. NV_ERROR(dev, "Do not understand BIT C table\n");
  4062. return -EINVAL;
  4063. }
  4064. bios->pll_limit_tbl_ptr = ROM16(bios->data[bitentry->offset + 8]);
  4065. return 0;
  4066. }
  4067. static int parse_bit_display_tbl_entry(struct drm_device *dev, struct nvbios *bios, struct bit_entry *bitentry)
  4068. {
  4069. /*
  4070. * Parses the flat panel table segment that the bit entry points to.
  4071. * Starting at bitentry->offset:
  4072. *
  4073. * offset + 0 (16 bits): ??? table pointer - seems to have 18 byte
  4074. * records beginning with a freq.
  4075. * offset + 2 (16 bits): mode table pointer
  4076. */
  4077. if (bitentry->length != 4) {
  4078. NV_ERROR(dev, "Do not understand BIT display table\n");
  4079. return -EINVAL;
  4080. }
  4081. bios->fp.fptablepointer = ROM16(bios->data[bitentry->offset + 2]);
  4082. return 0;
  4083. }
  4084. static int parse_bit_init_tbl_entry(struct drm_device *dev, struct nvbios *bios, struct bit_entry *bitentry)
  4085. {
  4086. /*
  4087. * Parses the init table segment that the bit entry points to.
  4088. *
  4089. * See parse_script_table_pointers for layout
  4090. */
  4091. if (bitentry->length < 14) {
  4092. NV_ERROR(dev, "Do not understand init table\n");
  4093. return -EINVAL;
  4094. }
  4095. parse_script_table_pointers(bios, bitentry->offset);
  4096. if (bitentry->length >= 16)
  4097. bios->some_script_ptr = ROM16(bios->data[bitentry->offset + 14]);
  4098. if (bitentry->length >= 18)
  4099. bios->init96_tbl_ptr = ROM16(bios->data[bitentry->offset + 16]);
  4100. return 0;
  4101. }
  4102. static int parse_bit_i_tbl_entry(struct drm_device *dev, struct nvbios *bios, struct bit_entry *bitentry)
  4103. {
  4104. /*
  4105. * BIT 'i' (info?) table
  4106. *
  4107. * offset + 0 (32 bits): BIOS version dword (as in B table)
  4108. * offset + 5 (8 bits): BIOS feature byte (same as for BMP?)
  4109. * offset + 13 (16 bits): pointer to table containing DAC load
  4110. * detection comparison values
  4111. *
  4112. * There's other things in the table, purpose unknown
  4113. */
  4114. uint16_t daccmpoffset;
  4115. uint8_t dacver, dacheaderlen;
  4116. if (bitentry->length < 6) {
  4117. NV_ERROR(dev, "BIT i table too short for needed information\n");
  4118. return -EINVAL;
  4119. }
  4120. parse_bios_version(dev, bios, bitentry->offset);
  4121. /*
  4122. * bit 4 seems to indicate a mobile bios (doesn't suffer from BMP's
  4123. * Quadro identity crisis), other bits possibly as for BMP feature byte
  4124. */
  4125. bios->feature_byte = bios->data[bitentry->offset + 5];
  4126. bios->is_mobile = bios->feature_byte & FEATURE_MOBILE;
  4127. if (bitentry->length < 15) {
  4128. NV_WARN(dev, "BIT i table not long enough for DAC load "
  4129. "detection comparison table\n");
  4130. return -EINVAL;
  4131. }
  4132. daccmpoffset = ROM16(bios->data[bitentry->offset + 13]);
  4133. /* doesn't exist on g80 */
  4134. if (!daccmpoffset)
  4135. return 0;
  4136. /*
  4137. * The first value in the table, following the header, is the
  4138. * comparison value, the second entry is a comparison value for
  4139. * TV load detection.
  4140. */
  4141. dacver = bios->data[daccmpoffset];
  4142. dacheaderlen = bios->data[daccmpoffset + 1];
  4143. if (dacver != 0x00 && dacver != 0x10) {
  4144. NV_WARN(dev, "DAC load detection comparison table version "
  4145. "%d.%d not known\n", dacver >> 4, dacver & 0xf);
  4146. return -ENOSYS;
  4147. }
  4148. bios->dactestval = ROM32(bios->data[daccmpoffset + dacheaderlen]);
  4149. bios->tvdactestval = ROM32(bios->data[daccmpoffset + dacheaderlen + 4]);
  4150. return 0;
  4151. }
  4152. static int parse_bit_lvds_tbl_entry(struct drm_device *dev, struct nvbios *bios, struct bit_entry *bitentry)
  4153. {
  4154. /*
  4155. * Parses the LVDS table segment that the bit entry points to.
  4156. * Starting at bitentry->offset:
  4157. *
  4158. * offset + 0 (16 bits): LVDS strap xlate table pointer
  4159. */
  4160. if (bitentry->length != 2) {
  4161. NV_ERROR(dev, "Do not understand BIT LVDS table\n");
  4162. return -EINVAL;
  4163. }
  4164. /*
  4165. * No idea if it's still called the LVDS manufacturer table, but
  4166. * the concept's close enough.
  4167. */
  4168. bios->fp.lvdsmanufacturerpointer = ROM16(bios->data[bitentry->offset]);
  4169. return 0;
  4170. }
  4171. static int
  4172. parse_bit_M_tbl_entry(struct drm_device *dev, struct nvbios *bios,
  4173. struct bit_entry *bitentry)
  4174. {
  4175. /*
  4176. * offset + 2 (8 bits): number of options in an
  4177. * INIT_RAM_RESTRICT_ZM_REG_GROUP opcode option set
  4178. * offset + 3 (16 bits): pointer to strap xlate table for RAM
  4179. * restrict option selection
  4180. *
  4181. * There's a bunch of bits in this table other than the RAM restrict
  4182. * stuff that we don't use - their use currently unknown
  4183. */
  4184. /*
  4185. * Older bios versions don't have a sufficiently long table for
  4186. * what we want
  4187. */
  4188. if (bitentry->length < 0x5)
  4189. return 0;
  4190. if (bitentry->version < 2) {
  4191. bios->ram_restrict_group_count = bios->data[bitentry->offset + 2];
  4192. bios->ram_restrict_tbl_ptr = ROM16(bios->data[bitentry->offset + 3]);
  4193. } else {
  4194. bios->ram_restrict_group_count = bios->data[bitentry->offset + 0];
  4195. bios->ram_restrict_tbl_ptr = ROM16(bios->data[bitentry->offset + 1]);
  4196. }
  4197. return 0;
  4198. }
  4199. static int parse_bit_tmds_tbl_entry(struct drm_device *dev, struct nvbios *bios, struct bit_entry *bitentry)
  4200. {
  4201. /*
  4202. * Parses the pointer to the TMDS table
  4203. *
  4204. * Starting at bitentry->offset:
  4205. *
  4206. * offset + 0 (16 bits): TMDS table pointer
  4207. *
  4208. * The TMDS table is typically found just before the DCB table, with a
  4209. * characteristic signature of 0x11,0x13 (1.1 being version, 0x13 being
  4210. * length?)
  4211. *
  4212. * At offset +7 is a pointer to a script, which I don't know how to
  4213. * run yet.
  4214. * At offset +9 is a pointer to another script, likewise
  4215. * Offset +11 has a pointer to a table where the first word is a pxclk
  4216. * frequency and the second word a pointer to a script, which should be
  4217. * run if the comparison pxclk frequency is less than the pxclk desired.
  4218. * This repeats for decreasing comparison frequencies
  4219. * Offset +13 has a pointer to a similar table
  4220. * The selection of table (and possibly +7/+9 script) is dictated by
  4221. * "or" from the DCB.
  4222. */
  4223. uint16_t tmdstableptr, script1, script2;
  4224. if (bitentry->length != 2) {
  4225. NV_ERROR(dev, "Do not understand BIT TMDS table\n");
  4226. return -EINVAL;
  4227. }
  4228. tmdstableptr = ROM16(bios->data[bitentry->offset]);
  4229. if (!tmdstableptr) {
  4230. NV_ERROR(dev, "Pointer to TMDS table invalid\n");
  4231. return -EINVAL;
  4232. }
  4233. NV_INFO(dev, "TMDS table version %d.%d\n",
  4234. bios->data[tmdstableptr] >> 4, bios->data[tmdstableptr] & 0xf);
  4235. /* nv50+ has v2.0, but we don't parse it atm */
  4236. if (bios->data[tmdstableptr] != 0x11)
  4237. return -ENOSYS;
  4238. /*
  4239. * These two scripts are odd: they don't seem to get run even when
  4240. * they are not stubbed.
  4241. */
  4242. script1 = ROM16(bios->data[tmdstableptr + 7]);
  4243. script2 = ROM16(bios->data[tmdstableptr + 9]);
  4244. if (bios->data[script1] != 'q' || bios->data[script2] != 'q')
  4245. NV_WARN(dev, "TMDS table script pointers not stubbed\n");
  4246. bios->tmds.output0_script_ptr = ROM16(bios->data[tmdstableptr + 11]);
  4247. bios->tmds.output1_script_ptr = ROM16(bios->data[tmdstableptr + 13]);
  4248. return 0;
  4249. }
  4250. static int
  4251. parse_bit_U_tbl_entry(struct drm_device *dev, struct nvbios *bios,
  4252. struct bit_entry *bitentry)
  4253. {
  4254. /*
  4255. * Parses the pointer to the G80 output script tables
  4256. *
  4257. * Starting at bitentry->offset:
  4258. *
  4259. * offset + 0 (16 bits): output script table pointer
  4260. */
  4261. uint16_t outputscripttableptr;
  4262. if (bitentry->length != 3) {
  4263. NV_ERROR(dev, "Do not understand BIT U table\n");
  4264. return -EINVAL;
  4265. }
  4266. outputscripttableptr = ROM16(bios->data[bitentry->offset]);
  4267. bios->display.script_table_ptr = outputscripttableptr;
  4268. return 0;
  4269. }
  4270. struct bit_table {
  4271. const char id;
  4272. int (* const parse_fn)(struct drm_device *, struct nvbios *, struct bit_entry *);
  4273. };
  4274. #define BIT_TABLE(id, funcid) ((struct bit_table){ id, parse_bit_##funcid##_tbl_entry })
  4275. int
  4276. bit_table(struct drm_device *dev, u8 id, struct bit_entry *bit)
  4277. {
  4278. struct drm_nouveau_private *dev_priv = dev->dev_private;
  4279. struct nvbios *bios = &dev_priv->vbios;
  4280. u8 entries, *entry;
  4281. if (bios->type != NVBIOS_BIT)
  4282. return -ENODEV;
  4283. entries = bios->data[bios->offset + 10];
  4284. entry = &bios->data[bios->offset + 12];
  4285. while (entries--) {
  4286. if (entry[0] == id) {
  4287. bit->id = entry[0];
  4288. bit->version = entry[1];
  4289. bit->length = ROM16(entry[2]);
  4290. bit->offset = ROM16(entry[4]);
  4291. bit->data = ROMPTR(dev, entry[4]);
  4292. return 0;
  4293. }
  4294. entry += bios->data[bios->offset + 9];
  4295. }
  4296. return -ENOENT;
  4297. }
  4298. static int
  4299. parse_bit_table(struct nvbios *bios, const uint16_t bitoffset,
  4300. struct bit_table *table)
  4301. {
  4302. struct drm_device *dev = bios->dev;
  4303. struct bit_entry bitentry;
  4304. if (bit_table(dev, table->id, &bitentry) == 0)
  4305. return table->parse_fn(dev, bios, &bitentry);
  4306. NV_INFO(dev, "BIT table '%c' not found\n", table->id);
  4307. return -ENOSYS;
  4308. }
  4309. static int
  4310. parse_bit_structure(struct nvbios *bios, const uint16_t bitoffset)
  4311. {
  4312. int ret;
  4313. /*
  4314. * The only restriction on parsing order currently is having 'i' first
  4315. * for use of bios->*_version or bios->feature_byte while parsing;
  4316. * functions shouldn't be actually *doing* anything apart from pulling
  4317. * data from the image into the bios struct, thus no interdependencies
  4318. */
  4319. ret = parse_bit_table(bios, bitoffset, &BIT_TABLE('i', i));
  4320. if (ret) /* info? */
  4321. return ret;
  4322. if (bios->major_version >= 0x60) /* g80+ */
  4323. parse_bit_table(bios, bitoffset, &BIT_TABLE('A', A));
  4324. ret = parse_bit_table(bios, bitoffset, &BIT_TABLE('C', C));
  4325. if (ret)
  4326. return ret;
  4327. parse_bit_table(bios, bitoffset, &BIT_TABLE('D', display));
  4328. ret = parse_bit_table(bios, bitoffset, &BIT_TABLE('I', init));
  4329. if (ret)
  4330. return ret;
  4331. parse_bit_table(bios, bitoffset, &BIT_TABLE('M', M)); /* memory? */
  4332. parse_bit_table(bios, bitoffset, &BIT_TABLE('L', lvds));
  4333. parse_bit_table(bios, bitoffset, &BIT_TABLE('T', tmds));
  4334. parse_bit_table(bios, bitoffset, &BIT_TABLE('U', U));
  4335. return 0;
  4336. }
  4337. static int parse_bmp_structure(struct drm_device *dev, struct nvbios *bios, unsigned int offset)
  4338. {
  4339. /*
  4340. * Parses the BMP structure for useful things, but does not act on them
  4341. *
  4342. * offset + 5: BMP major version
  4343. * offset + 6: BMP minor version
  4344. * offset + 9: BMP feature byte
  4345. * offset + 10: BCD encoded BIOS version
  4346. *
  4347. * offset + 18: init script table pointer (for bios versions < 5.10h)
  4348. * offset + 20: extra init script table pointer (for bios
  4349. * versions < 5.10h)
  4350. *
  4351. * offset + 24: memory init table pointer (used on early bios versions)
  4352. * offset + 26: SDR memory sequencing setup data table
  4353. * offset + 28: DDR memory sequencing setup data table
  4354. *
  4355. * offset + 54: index of I2C CRTC pair to use for CRT output
  4356. * offset + 55: index of I2C CRTC pair to use for TV output
  4357. * offset + 56: index of I2C CRTC pair to use for flat panel output
  4358. * offset + 58: write CRTC index for I2C pair 0
  4359. * offset + 59: read CRTC index for I2C pair 0
  4360. * offset + 60: write CRTC index for I2C pair 1
  4361. * offset + 61: read CRTC index for I2C pair 1
  4362. *
  4363. * offset + 67: maximum internal PLL frequency (single stage PLL)
  4364. * offset + 71: minimum internal PLL frequency (single stage PLL)
  4365. *
  4366. * offset + 75: script table pointers, as described in
  4367. * parse_script_table_pointers
  4368. *
  4369. * offset + 89: TMDS single link output A table pointer
  4370. * offset + 91: TMDS single link output B table pointer
  4371. * offset + 95: LVDS single link output A table pointer
  4372. * offset + 105: flat panel timings table pointer
  4373. * offset + 107: flat panel strapping translation table pointer
  4374. * offset + 117: LVDS manufacturer panel config table pointer
  4375. * offset + 119: LVDS manufacturer strapping translation table pointer
  4376. *
  4377. * offset + 142: PLL limits table pointer
  4378. *
  4379. * offset + 156: minimum pixel clock for LVDS dual link
  4380. */
  4381. uint8_t *bmp = &bios->data[offset], bmp_version_major, bmp_version_minor;
  4382. uint16_t bmplength;
  4383. uint16_t legacy_scripts_offset, legacy_i2c_offset;
  4384. /* load needed defaults in case we can't parse this info */
  4385. bios->digital_min_front_porch = 0x4b;
  4386. bios->fmaxvco = 256000;
  4387. bios->fminvco = 128000;
  4388. bios->fp.duallink_transition_clk = 90000;
  4389. bmp_version_major = bmp[5];
  4390. bmp_version_minor = bmp[6];
  4391. NV_TRACE(dev, "BMP version %d.%d\n",
  4392. bmp_version_major, bmp_version_minor);
  4393. /*
  4394. * Make sure that 0x36 is blank and can't be mistaken for a DCB
  4395. * pointer on early versions
  4396. */
  4397. if (bmp_version_major < 5)
  4398. *(uint16_t *)&bios->data[0x36] = 0;
  4399. /*
  4400. * Seems that the minor version was 1 for all major versions prior
  4401. * to 5. Version 6 could theoretically exist, but I suspect BIT
  4402. * happened instead.
  4403. */
  4404. if ((bmp_version_major < 5 && bmp_version_minor != 1) || bmp_version_major > 5) {
  4405. NV_ERROR(dev, "You have an unsupported BMP version. "
  4406. "Please send in your bios\n");
  4407. return -ENOSYS;
  4408. }
  4409. if (bmp_version_major == 0)
  4410. /* nothing that's currently useful in this version */
  4411. return 0;
  4412. else if (bmp_version_major == 1)
  4413. bmplength = 44; /* exact for 1.01 */
  4414. else if (bmp_version_major == 2)
  4415. bmplength = 48; /* exact for 2.01 */
  4416. else if (bmp_version_major == 3)
  4417. bmplength = 54;
  4418. /* guessed - mem init tables added in this version */
  4419. else if (bmp_version_major == 4 || bmp_version_minor < 0x1)
  4420. /* don't know if 5.0 exists... */
  4421. bmplength = 62;
  4422. /* guessed - BMP I2C indices added in version 4*/
  4423. else if (bmp_version_minor < 0x6)
  4424. bmplength = 67; /* exact for 5.01 */
  4425. else if (bmp_version_minor < 0x10)
  4426. bmplength = 75; /* exact for 5.06 */
  4427. else if (bmp_version_minor == 0x10)
  4428. bmplength = 89; /* exact for 5.10h */
  4429. else if (bmp_version_minor < 0x14)
  4430. bmplength = 118; /* exact for 5.11h */
  4431. else if (bmp_version_minor < 0x24)
  4432. /*
  4433. * Not sure of version where pll limits came in;
  4434. * certainly exist by 0x24 though.
  4435. */
  4436. /* length not exact: this is long enough to get lvds members */
  4437. bmplength = 123;
  4438. else if (bmp_version_minor < 0x27)
  4439. /*
  4440. * Length not exact: this is long enough to get pll limit
  4441. * member
  4442. */
  4443. bmplength = 144;
  4444. else
  4445. /*
  4446. * Length not exact: this is long enough to get dual link
  4447. * transition clock.
  4448. */
  4449. bmplength = 158;
  4450. /* checksum */
  4451. if (nv_cksum(bmp, 8)) {
  4452. NV_ERROR(dev, "Bad BMP checksum\n");
  4453. return -EINVAL;
  4454. }
  4455. /*
  4456. * Bit 4 seems to indicate either a mobile bios or a quadro card --
  4457. * mobile behaviour consistent (nv11+), quadro only seen nv18gl-nv36gl
  4458. * (not nv10gl), bit 5 that the flat panel tables are present, and
  4459. * bit 6 a tv bios.
  4460. */
  4461. bios->feature_byte = bmp[9];
  4462. parse_bios_version(dev, bios, offset + 10);
  4463. if (bmp_version_major < 5 || bmp_version_minor < 0x10)
  4464. bios->old_style_init = true;
  4465. legacy_scripts_offset = 18;
  4466. if (bmp_version_major < 2)
  4467. legacy_scripts_offset -= 4;
  4468. bios->init_script_tbls_ptr = ROM16(bmp[legacy_scripts_offset]);
  4469. bios->extra_init_script_tbl_ptr = ROM16(bmp[legacy_scripts_offset + 2]);
  4470. if (bmp_version_major > 2) { /* appears in BMP 3 */
  4471. bios->legacy.mem_init_tbl_ptr = ROM16(bmp[24]);
  4472. bios->legacy.sdr_seq_tbl_ptr = ROM16(bmp[26]);
  4473. bios->legacy.ddr_seq_tbl_ptr = ROM16(bmp[28]);
  4474. }
  4475. legacy_i2c_offset = 0x48; /* BMP version 2 & 3 */
  4476. if (bmplength > 61)
  4477. legacy_i2c_offset = offset + 54;
  4478. bios->legacy.i2c_indices.crt = bios->data[legacy_i2c_offset];
  4479. bios->legacy.i2c_indices.tv = bios->data[legacy_i2c_offset + 1];
  4480. bios->legacy.i2c_indices.panel = bios->data[legacy_i2c_offset + 2];
  4481. if (bmplength > 74) {
  4482. bios->fmaxvco = ROM32(bmp[67]);
  4483. bios->fminvco = ROM32(bmp[71]);
  4484. }
  4485. if (bmplength > 88)
  4486. parse_script_table_pointers(bios, offset + 75);
  4487. if (bmplength > 94) {
  4488. bios->tmds.output0_script_ptr = ROM16(bmp[89]);
  4489. bios->tmds.output1_script_ptr = ROM16(bmp[91]);
  4490. /*
  4491. * Never observed in use with lvds scripts, but is reused for
  4492. * 18/24 bit panel interface default for EDID equipped panels
  4493. * (if_is_24bit not set directly to avoid any oscillation).
  4494. */
  4495. bios->legacy.lvds_single_a_script_ptr = ROM16(bmp[95]);
  4496. }
  4497. if (bmplength > 108) {
  4498. bios->fp.fptablepointer = ROM16(bmp[105]);
  4499. bios->fp.fpxlatetableptr = ROM16(bmp[107]);
  4500. bios->fp.xlatwidth = 1;
  4501. }
  4502. if (bmplength > 120) {
  4503. bios->fp.lvdsmanufacturerpointer = ROM16(bmp[117]);
  4504. bios->fp.fpxlatemanufacturertableptr = ROM16(bmp[119]);
  4505. }
  4506. if (bmplength > 143)
  4507. bios->pll_limit_tbl_ptr = ROM16(bmp[142]);
  4508. if (bmplength > 157)
  4509. bios->fp.duallink_transition_clk = ROM16(bmp[156]) * 10;
  4510. return 0;
  4511. }
  4512. static uint16_t findstr(uint8_t *data, int n, const uint8_t *str, int len)
  4513. {
  4514. int i, j;
  4515. for (i = 0; i <= (n - len); i++) {
  4516. for (j = 0; j < len; j++)
  4517. if (data[i + j] != str[j])
  4518. break;
  4519. if (j == len)
  4520. return i;
  4521. }
  4522. return 0;
  4523. }
  4524. void *
  4525. dcb_table(struct drm_device *dev)
  4526. {
  4527. struct drm_nouveau_private *dev_priv = dev->dev_private;
  4528. u8 *dcb = NULL;
  4529. if (dev_priv->card_type > NV_04)
  4530. dcb = ROMPTR(dev, dev_priv->vbios.data[0x36]);
  4531. if (!dcb) {
  4532. NV_WARNONCE(dev, "No DCB data found in VBIOS\n");
  4533. return NULL;
  4534. }
  4535. if (dcb[0] >= 0x41) {
  4536. NV_WARNONCE(dev, "DCB version 0x%02x unknown\n", dcb[0]);
  4537. return NULL;
  4538. } else
  4539. if (dcb[0] >= 0x30) {
  4540. if (ROM32(dcb[6]) == 0x4edcbdcb)
  4541. return dcb;
  4542. } else
  4543. if (dcb[0] >= 0x20) {
  4544. if (ROM32(dcb[4]) == 0x4edcbdcb)
  4545. return dcb;
  4546. } else
  4547. if (dcb[0] >= 0x15) {
  4548. if (!memcmp(&dcb[-7], "DEV_REC", 7))
  4549. return dcb;
  4550. } else {
  4551. /*
  4552. * v1.4 (some NV15/16, NV11+) seems the same as v1.5, but
  4553. * always has the same single (crt) entry, even when tv-out
  4554. * present, so the conclusion is this version cannot really
  4555. * be used.
  4556. *
  4557. * v1.2 tables (some NV6/10, and NV15+) normally have the
  4558. * same 5 entries, which are not specific to the card and so
  4559. * no use.
  4560. *
  4561. * v1.2 does have an I2C table that read_dcb_i2c_table can
  4562. * handle, but cards exist (nv11 in #14821) with a bad i2c
  4563. * table pointer, so use the indices parsed in
  4564. * parse_bmp_structure.
  4565. *
  4566. * v1.1 (NV5+, maybe some NV4) is entirely unhelpful
  4567. */
  4568. NV_WARNONCE(dev, "No useful DCB data in VBIOS\n");
  4569. return NULL;
  4570. }
  4571. NV_WARNONCE(dev, "DCB header validation failed\n");
  4572. return NULL;
  4573. }
  4574. void *
  4575. dcb_outp(struct drm_device *dev, u8 idx)
  4576. {
  4577. u8 *dcb = dcb_table(dev);
  4578. if (dcb && dcb[0] >= 0x30) {
  4579. if (idx < dcb[2])
  4580. return dcb + dcb[1] + (idx * dcb[3]);
  4581. } else
  4582. if (dcb && dcb[0] >= 0x20) {
  4583. u8 *i2c = ROMPTR(dev, dcb[2]);
  4584. u8 *ent = dcb + 8 + (idx * 8);
  4585. if (i2c && ent < i2c)
  4586. return ent;
  4587. } else
  4588. if (dcb && dcb[0] >= 0x15) {
  4589. u8 *i2c = ROMPTR(dev, dcb[2]);
  4590. u8 *ent = dcb + 4 + (idx * 10);
  4591. if (i2c && ent < i2c)
  4592. return ent;
  4593. }
  4594. return NULL;
  4595. }
  4596. int
  4597. dcb_outp_foreach(struct drm_device *dev, void *data,
  4598. int (*exec)(struct drm_device *, void *, int idx, u8 *outp))
  4599. {
  4600. int ret, idx = -1;
  4601. u8 *outp = NULL;
  4602. while ((outp = dcb_outp(dev, ++idx))) {
  4603. if (ROM32(outp[0]) == 0x00000000)
  4604. break; /* seen on an NV11 with DCB v1.5 */
  4605. if (ROM32(outp[0]) == 0xffffffff)
  4606. break; /* seen on an NV17 with DCB v2.0 */
  4607. if ((outp[0] & 0x0f) == OUTPUT_UNUSED)
  4608. continue;
  4609. if ((outp[0] & 0x0f) == OUTPUT_EOL)
  4610. break;
  4611. ret = exec(dev, data, idx, outp);
  4612. if (ret)
  4613. return ret;
  4614. }
  4615. return 0;
  4616. }
  4617. u8 *
  4618. dcb_conntab(struct drm_device *dev)
  4619. {
  4620. u8 *dcb = dcb_table(dev);
  4621. if (dcb && dcb[0] >= 0x30 && dcb[1] >= 0x16) {
  4622. u8 *conntab = ROMPTR(dev, dcb[0x14]);
  4623. if (conntab && conntab[0] >= 0x30 && conntab[0] <= 0x40)
  4624. return conntab;
  4625. }
  4626. return NULL;
  4627. }
  4628. u8 *
  4629. dcb_conn(struct drm_device *dev, u8 idx)
  4630. {
  4631. u8 *conntab = dcb_conntab(dev);
  4632. if (conntab && idx < conntab[2])
  4633. return conntab + conntab[1] + (idx * conntab[3]);
  4634. return NULL;
  4635. }
  4636. static struct dcb_entry *new_dcb_entry(struct dcb_table *dcb)
  4637. {
  4638. struct dcb_entry *entry = &dcb->entry[dcb->entries];
  4639. memset(entry, 0, sizeof(struct dcb_entry));
  4640. entry->index = dcb->entries++;
  4641. return entry;
  4642. }
  4643. static void fabricate_dcb_output(struct dcb_table *dcb, int type, int i2c,
  4644. int heads, int or)
  4645. {
  4646. struct dcb_entry *entry = new_dcb_entry(dcb);
  4647. entry->type = type;
  4648. entry->i2c_index = i2c;
  4649. entry->heads = heads;
  4650. if (type != OUTPUT_ANALOG)
  4651. entry->location = !DCB_LOC_ON_CHIP; /* ie OFF CHIP */
  4652. entry->or = or;
  4653. }
  4654. static bool
  4655. parse_dcb20_entry(struct drm_device *dev, struct dcb_table *dcb,
  4656. uint32_t conn, uint32_t conf, struct dcb_entry *entry)
  4657. {
  4658. entry->type = conn & 0xf;
  4659. entry->i2c_index = (conn >> 4) & 0xf;
  4660. entry->heads = (conn >> 8) & 0xf;
  4661. entry->connector = (conn >> 12) & 0xf;
  4662. entry->bus = (conn >> 16) & 0xf;
  4663. entry->location = (conn >> 20) & 0x3;
  4664. entry->or = (conn >> 24) & 0xf;
  4665. switch (entry->type) {
  4666. case OUTPUT_ANALOG:
  4667. /*
  4668. * Although the rest of a CRT conf dword is usually
  4669. * zeros, mac biosen have stuff there so we must mask
  4670. */
  4671. entry->crtconf.maxfreq = (dcb->version < 0x30) ?
  4672. (conf & 0xffff) * 10 :
  4673. (conf & 0xff) * 10000;
  4674. break;
  4675. case OUTPUT_LVDS:
  4676. {
  4677. uint32_t mask;
  4678. if (conf & 0x1)
  4679. entry->lvdsconf.use_straps_for_mode = true;
  4680. if (dcb->version < 0x22) {
  4681. mask = ~0xd;
  4682. /*
  4683. * The laptop in bug 14567 lies and claims to not use
  4684. * straps when it does, so assume all DCB 2.0 laptops
  4685. * use straps, until a broken EDID using one is produced
  4686. */
  4687. entry->lvdsconf.use_straps_for_mode = true;
  4688. /*
  4689. * Both 0x4 and 0x8 show up in v2.0 tables; assume they
  4690. * mean the same thing (probably wrong, but might work)
  4691. */
  4692. if (conf & 0x4 || conf & 0x8)
  4693. entry->lvdsconf.use_power_scripts = true;
  4694. } else {
  4695. mask = ~0x7;
  4696. if (conf & 0x2)
  4697. entry->lvdsconf.use_acpi_for_edid = true;
  4698. if (conf & 0x4)
  4699. entry->lvdsconf.use_power_scripts = true;
  4700. entry->lvdsconf.sor.link = (conf & 0x00000030) >> 4;
  4701. }
  4702. if (conf & mask) {
  4703. /*
  4704. * Until we even try to use these on G8x, it's
  4705. * useless reporting unknown bits. They all are.
  4706. */
  4707. if (dcb->version >= 0x40)
  4708. break;
  4709. NV_ERROR(dev, "Unknown LVDS configuration bits, "
  4710. "please report\n");
  4711. }
  4712. break;
  4713. }
  4714. case OUTPUT_TV:
  4715. {
  4716. if (dcb->version >= 0x30)
  4717. entry->tvconf.has_component_output = conf & (0x8 << 4);
  4718. else
  4719. entry->tvconf.has_component_output = false;
  4720. break;
  4721. }
  4722. case OUTPUT_DP:
  4723. entry->dpconf.sor.link = (conf & 0x00000030) >> 4;
  4724. switch ((conf & 0x00e00000) >> 21) {
  4725. case 0:
  4726. entry->dpconf.link_bw = 162000;
  4727. break;
  4728. default:
  4729. entry->dpconf.link_bw = 270000;
  4730. break;
  4731. }
  4732. switch ((conf & 0x0f000000) >> 24) {
  4733. case 0xf:
  4734. entry->dpconf.link_nr = 4;
  4735. break;
  4736. case 0x3:
  4737. entry->dpconf.link_nr = 2;
  4738. break;
  4739. default:
  4740. entry->dpconf.link_nr = 1;
  4741. break;
  4742. }
  4743. break;
  4744. case OUTPUT_TMDS:
  4745. if (dcb->version >= 0x40)
  4746. entry->tmdsconf.sor.link = (conf & 0x00000030) >> 4;
  4747. else if (dcb->version >= 0x30)
  4748. entry->tmdsconf.slave_addr = (conf & 0x00000700) >> 8;
  4749. else if (dcb->version >= 0x22)
  4750. entry->tmdsconf.slave_addr = (conf & 0x00000070) >> 4;
  4751. break;
  4752. case OUTPUT_EOL:
  4753. /* weird g80 mobile type that "nv" treats as a terminator */
  4754. dcb->entries--;
  4755. return false;
  4756. default:
  4757. break;
  4758. }
  4759. if (dcb->version < 0x40) {
  4760. /* Normal entries consist of a single bit, but dual link has
  4761. * the next most significant bit set too
  4762. */
  4763. entry->duallink_possible =
  4764. ((1 << (ffs(entry->or) - 1)) * 3 == entry->or);
  4765. } else {
  4766. entry->duallink_possible = (entry->sorconf.link == 3);
  4767. }
  4768. /* unsure what DCB version introduces this, 3.0? */
  4769. if (conf & 0x100000)
  4770. entry->i2c_upper_default = true;
  4771. return true;
  4772. }
  4773. static bool
  4774. parse_dcb15_entry(struct drm_device *dev, struct dcb_table *dcb,
  4775. uint32_t conn, uint32_t conf, struct dcb_entry *entry)
  4776. {
  4777. switch (conn & 0x0000000f) {
  4778. case 0:
  4779. entry->type = OUTPUT_ANALOG;
  4780. break;
  4781. case 1:
  4782. entry->type = OUTPUT_TV;
  4783. break;
  4784. case 2:
  4785. case 4:
  4786. if (conn & 0x10)
  4787. entry->type = OUTPUT_LVDS;
  4788. else
  4789. entry->type = OUTPUT_TMDS;
  4790. break;
  4791. case 3:
  4792. entry->type = OUTPUT_LVDS;
  4793. break;
  4794. default:
  4795. NV_ERROR(dev, "Unknown DCB type %d\n", conn & 0x0000000f);
  4796. return false;
  4797. }
  4798. entry->i2c_index = (conn & 0x0003c000) >> 14;
  4799. entry->heads = ((conn & 0x001c0000) >> 18) + 1;
  4800. entry->or = entry->heads; /* same as heads, hopefully safe enough */
  4801. entry->location = (conn & 0x01e00000) >> 21;
  4802. entry->bus = (conn & 0x0e000000) >> 25;
  4803. entry->duallink_possible = false;
  4804. switch (entry->type) {
  4805. case OUTPUT_ANALOG:
  4806. entry->crtconf.maxfreq = (conf & 0xffff) * 10;
  4807. break;
  4808. case OUTPUT_TV:
  4809. entry->tvconf.has_component_output = false;
  4810. break;
  4811. case OUTPUT_LVDS:
  4812. if ((conn & 0x00003f00) >> 8 != 0x10)
  4813. entry->lvdsconf.use_straps_for_mode = true;
  4814. entry->lvdsconf.use_power_scripts = true;
  4815. break;
  4816. default:
  4817. break;
  4818. }
  4819. return true;
  4820. }
  4821. static
  4822. void merge_like_dcb_entries(struct drm_device *dev, struct dcb_table *dcb)
  4823. {
  4824. /*
  4825. * DCB v2.0 lists each output combination separately.
  4826. * Here we merge compatible entries to have fewer outputs, with
  4827. * more options
  4828. */
  4829. int i, newentries = 0;
  4830. for (i = 0; i < dcb->entries; i++) {
  4831. struct dcb_entry *ient = &dcb->entry[i];
  4832. int j;
  4833. for (j = i + 1; j < dcb->entries; j++) {
  4834. struct dcb_entry *jent = &dcb->entry[j];
  4835. if (jent->type == 100) /* already merged entry */
  4836. continue;
  4837. /* merge heads field when all other fields the same */
  4838. if (jent->i2c_index == ient->i2c_index &&
  4839. jent->type == ient->type &&
  4840. jent->location == ient->location &&
  4841. jent->or == ient->or) {
  4842. NV_TRACE(dev, "Merging DCB entries %d and %d\n",
  4843. i, j);
  4844. ient->heads |= jent->heads;
  4845. jent->type = 100; /* dummy value */
  4846. }
  4847. }
  4848. }
  4849. /* Compact entries merged into others out of dcb */
  4850. for (i = 0; i < dcb->entries; i++) {
  4851. if (dcb->entry[i].type == 100)
  4852. continue;
  4853. if (newentries != i) {
  4854. dcb->entry[newentries] = dcb->entry[i];
  4855. dcb->entry[newentries].index = newentries;
  4856. }
  4857. newentries++;
  4858. }
  4859. dcb->entries = newentries;
  4860. }
  4861. static bool
  4862. apply_dcb_encoder_quirks(struct drm_device *dev, int idx, u32 *conn, u32 *conf)
  4863. {
  4864. struct drm_nouveau_private *dev_priv = dev->dev_private;
  4865. struct dcb_table *dcb = &dev_priv->vbios.dcb;
  4866. /* Dell Precision M6300
  4867. * DCB entry 2: 02025312 00000010
  4868. * DCB entry 3: 02026312 00000020
  4869. *
  4870. * Identical, except apparently a different connector on a
  4871. * different SOR link. Not a clue how we're supposed to know
  4872. * which one is in use if it even shares an i2c line...
  4873. *
  4874. * Ignore the connector on the second SOR link to prevent
  4875. * nasty problems until this is sorted (assuming it's not a
  4876. * VBIOS bug).
  4877. */
  4878. if (nv_match_device(dev, 0x040d, 0x1028, 0x019b)) {
  4879. if (*conn == 0x02026312 && *conf == 0x00000020)
  4880. return false;
  4881. }
  4882. /* GeForce3 Ti 200
  4883. *
  4884. * DCB reports an LVDS output that should be TMDS:
  4885. * DCB entry 1: f2005014 ffffffff
  4886. */
  4887. if (nv_match_device(dev, 0x0201, 0x1462, 0x8851)) {
  4888. if (*conn == 0xf2005014 && *conf == 0xffffffff) {
  4889. fabricate_dcb_output(dcb, OUTPUT_TMDS, 1, 1, 1);
  4890. return false;
  4891. }
  4892. }
  4893. /* XFX GT-240X-YA
  4894. *
  4895. * So many things wrong here, replace the entire encoder table..
  4896. */
  4897. if (nv_match_device(dev, 0x0ca3, 0x1682, 0x3003)) {
  4898. if (idx == 0) {
  4899. *conn = 0x02001300; /* VGA, connector 1 */
  4900. *conf = 0x00000028;
  4901. } else
  4902. if (idx == 1) {
  4903. *conn = 0x01010312; /* DVI, connector 0 */
  4904. *conf = 0x00020030;
  4905. } else
  4906. if (idx == 2) {
  4907. *conn = 0x01010310; /* VGA, connector 0 */
  4908. *conf = 0x00000028;
  4909. } else
  4910. if (idx == 3) {
  4911. *conn = 0x02022362; /* HDMI, connector 2 */
  4912. *conf = 0x00020010;
  4913. } else {
  4914. *conn = 0x0000000e; /* EOL */
  4915. *conf = 0x00000000;
  4916. }
  4917. }
  4918. /* Some other twisted XFX board (rhbz#694914)
  4919. *
  4920. * The DVI/VGA encoder combo that's supposed to represent the
  4921. * DVI-I connector actually point at two different ones, and
  4922. * the HDMI connector ends up paired with the VGA instead.
  4923. *
  4924. * Connector table is missing anything for VGA at all, pointing it
  4925. * an invalid conntab entry 2 so we figure it out ourself.
  4926. */
  4927. if (nv_match_device(dev, 0x0615, 0x1682, 0x2605)) {
  4928. if (idx == 0) {
  4929. *conn = 0x02002300; /* VGA, connector 2 */
  4930. *conf = 0x00000028;
  4931. } else
  4932. if (idx == 1) {
  4933. *conn = 0x01010312; /* DVI, connector 0 */
  4934. *conf = 0x00020030;
  4935. } else
  4936. if (idx == 2) {
  4937. *conn = 0x04020310; /* VGA, connector 0 */
  4938. *conf = 0x00000028;
  4939. } else
  4940. if (idx == 3) {
  4941. *conn = 0x02021322; /* HDMI, connector 1 */
  4942. *conf = 0x00020010;
  4943. } else {
  4944. *conn = 0x0000000e; /* EOL */
  4945. *conf = 0x00000000;
  4946. }
  4947. }
  4948. /* fdo#50830: connector indices for VGA and DVI-I are backwards */
  4949. if (nv_match_device(dev, 0x0421, 0x3842, 0xc793)) {
  4950. if (idx == 0 && *conn == 0x02000300)
  4951. *conn = 0x02011300;
  4952. else
  4953. if (idx == 1 && *conn == 0x04011310)
  4954. *conn = 0x04000310;
  4955. else
  4956. if (idx == 2 && *conn == 0x02011312)
  4957. *conn = 0x02000312;
  4958. }
  4959. return true;
  4960. }
  4961. static void
  4962. fabricate_dcb_encoder_table(struct drm_device *dev, struct nvbios *bios)
  4963. {
  4964. struct dcb_table *dcb = &bios->dcb;
  4965. int all_heads = (nv_two_heads(dev) ? 3 : 1);
  4966. #ifdef __powerpc__
  4967. /* Apple iMac G4 NV17 */
  4968. if (of_machine_is_compatible("PowerMac4,5")) {
  4969. fabricate_dcb_output(dcb, OUTPUT_TMDS, 0, all_heads, 1);
  4970. fabricate_dcb_output(dcb, OUTPUT_ANALOG, 1, all_heads, 2);
  4971. return;
  4972. }
  4973. #endif
  4974. /* Make up some sane defaults */
  4975. fabricate_dcb_output(dcb, OUTPUT_ANALOG,
  4976. bios->legacy.i2c_indices.crt, 1, 1);
  4977. if (nv04_tv_identify(dev, bios->legacy.i2c_indices.tv) >= 0)
  4978. fabricate_dcb_output(dcb, OUTPUT_TV,
  4979. bios->legacy.i2c_indices.tv,
  4980. all_heads, 0);
  4981. else if (bios->tmds.output0_script_ptr ||
  4982. bios->tmds.output1_script_ptr)
  4983. fabricate_dcb_output(dcb, OUTPUT_TMDS,
  4984. bios->legacy.i2c_indices.panel,
  4985. all_heads, 1);
  4986. }
  4987. static int
  4988. parse_dcb_entry(struct drm_device *dev, void *data, int idx, u8 *outp)
  4989. {
  4990. struct drm_nouveau_private *dev_priv = dev->dev_private;
  4991. struct dcb_table *dcb = &dev_priv->vbios.dcb;
  4992. u32 conf = (dcb->version >= 0x20) ? ROM32(outp[4]) : ROM32(outp[6]);
  4993. u32 conn = ROM32(outp[0]);
  4994. bool ret;
  4995. if (apply_dcb_encoder_quirks(dev, idx, &conn, &conf)) {
  4996. struct dcb_entry *entry = new_dcb_entry(dcb);
  4997. NV_TRACEWARN(dev, "DCB outp %02d: %08x %08x\n", idx, conn, conf);
  4998. if (dcb->version >= 0x20)
  4999. ret = parse_dcb20_entry(dev, dcb, conn, conf, entry);
  5000. else
  5001. ret = parse_dcb15_entry(dev, dcb, conn, conf, entry);
  5002. if (!ret)
  5003. return 1; /* stop parsing */
  5004. /* Ignore the I2C index for on-chip TV-out, as there
  5005. * are cards with bogus values (nv31m in bug 23212),
  5006. * and it's otherwise useless.
  5007. */
  5008. if (entry->type == OUTPUT_TV &&
  5009. entry->location == DCB_LOC_ON_CHIP)
  5010. entry->i2c_index = 0x0f;
  5011. }
  5012. return 0;
  5013. }
  5014. static void
  5015. dcb_fake_connectors(struct nvbios *bios)
  5016. {
  5017. struct dcb_table *dcbt = &bios->dcb;
  5018. u8 map[16] = { };
  5019. int i, idx = 0;
  5020. /* heuristic: if we ever get a non-zero connector field, assume
  5021. * that all the indices are valid and we don't need fake them.
  5022. *
  5023. * and, as usual, a blacklist of boards with bad bios data..
  5024. */
  5025. if (!nv_match_device(bios->dev, 0x0392, 0x107d, 0x20a2)) {
  5026. for (i = 0; i < dcbt->entries; i++) {
  5027. if (dcbt->entry[i].connector)
  5028. return;
  5029. }
  5030. }
  5031. /* no useful connector info available, we need to make it up
  5032. * ourselves. the rule here is: anything on the same i2c bus
  5033. * is considered to be on the same connector. any output
  5034. * without an associated i2c bus is assigned its own unique
  5035. * connector index.
  5036. */
  5037. for (i = 0; i < dcbt->entries; i++) {
  5038. u8 i2c = dcbt->entry[i].i2c_index;
  5039. if (i2c == 0x0f) {
  5040. dcbt->entry[i].connector = idx++;
  5041. } else {
  5042. if (!map[i2c])
  5043. map[i2c] = ++idx;
  5044. dcbt->entry[i].connector = map[i2c] - 1;
  5045. }
  5046. }
  5047. /* if we created more than one connector, destroy the connector
  5048. * table - just in case it has random, rather than stub, entries.
  5049. */
  5050. if (i > 1) {
  5051. u8 *conntab = dcb_conntab(bios->dev);
  5052. if (conntab)
  5053. conntab[0] = 0x00;
  5054. }
  5055. }
  5056. static int
  5057. parse_dcb_table(struct drm_device *dev, struct nvbios *bios)
  5058. {
  5059. struct dcb_table *dcb = &bios->dcb;
  5060. u8 *dcbt, *conn;
  5061. int idx;
  5062. dcbt = dcb_table(dev);
  5063. if (!dcbt) {
  5064. /* handle pre-DCB boards */
  5065. if (bios->type == NVBIOS_BMP) {
  5066. fabricate_dcb_encoder_table(dev, bios);
  5067. return 0;
  5068. }
  5069. return -EINVAL;
  5070. }
  5071. NV_TRACE(dev, "DCB version %d.%d\n", dcbt[0] >> 4, dcbt[0] & 0xf);
  5072. dcb->version = dcbt[0];
  5073. dcb_outp_foreach(dev, NULL, parse_dcb_entry);
  5074. /*
  5075. * apart for v2.1+ not being known for requiring merging, this
  5076. * guarantees dcbent->index is the index of the entry in the rom image
  5077. */
  5078. if (dcb->version < 0x21)
  5079. merge_like_dcb_entries(dev, dcb);
  5080. if (!dcb->entries)
  5081. return -ENXIO;
  5082. /* dump connector table entries to log, if any exist */
  5083. idx = -1;
  5084. while ((conn = dcb_conn(dev, ++idx))) {
  5085. if (conn[0] != 0xff) {
  5086. NV_TRACE(dev, "DCB conn %02d: ", idx);
  5087. if (dcb_conntab(dev)[3] < 4)
  5088. printk("%04x\n", ROM16(conn[0]));
  5089. else
  5090. printk("%08x\n", ROM32(conn[0]));
  5091. }
  5092. }
  5093. dcb_fake_connectors(bios);
  5094. return 0;
  5095. }
  5096. static int load_nv17_hwsq_ucode_entry(struct drm_device *dev, struct nvbios *bios, uint16_t hwsq_offset, int entry)
  5097. {
  5098. /*
  5099. * The header following the "HWSQ" signature has the number of entries,
  5100. * and the entry size
  5101. *
  5102. * An entry consists of a dword to write to the sequencer control reg
  5103. * (0x00001304), followed by the ucode bytes, written sequentially,
  5104. * starting at reg 0x00001400
  5105. */
  5106. uint8_t bytes_to_write;
  5107. uint16_t hwsq_entry_offset;
  5108. int i;
  5109. if (bios->data[hwsq_offset] <= entry) {
  5110. NV_ERROR(dev, "Too few entries in HW sequencer table for "
  5111. "requested entry\n");
  5112. return -ENOENT;
  5113. }
  5114. bytes_to_write = bios->data[hwsq_offset + 1];
  5115. if (bytes_to_write != 36) {
  5116. NV_ERROR(dev, "Unknown HW sequencer entry size\n");
  5117. return -EINVAL;
  5118. }
  5119. NV_TRACE(dev, "Loading NV17 power sequencing microcode\n");
  5120. hwsq_entry_offset = hwsq_offset + 2 + entry * bytes_to_write;
  5121. /* set sequencer control */
  5122. bios_wr32(bios, 0x00001304, ROM32(bios->data[hwsq_entry_offset]));
  5123. bytes_to_write -= 4;
  5124. /* write ucode */
  5125. for (i = 0; i < bytes_to_write; i += 4)
  5126. bios_wr32(bios, 0x00001400 + i, ROM32(bios->data[hwsq_entry_offset + i + 4]));
  5127. /* twiddle NV_PBUS_DEBUG_4 */
  5128. bios_wr32(bios, NV_PBUS_DEBUG_4, bios_rd32(bios, NV_PBUS_DEBUG_4) | 0x18);
  5129. return 0;
  5130. }
  5131. static int load_nv17_hw_sequencer_ucode(struct drm_device *dev,
  5132. struct nvbios *bios)
  5133. {
  5134. /*
  5135. * BMP based cards, from NV17, need a microcode loading to correctly
  5136. * control the GPIO etc for LVDS panels
  5137. *
  5138. * BIT based cards seem to do this directly in the init scripts
  5139. *
  5140. * The microcode entries are found by the "HWSQ" signature.
  5141. */
  5142. const uint8_t hwsq_signature[] = { 'H', 'W', 'S', 'Q' };
  5143. const int sz = sizeof(hwsq_signature);
  5144. int hwsq_offset;
  5145. hwsq_offset = findstr(bios->data, bios->length, hwsq_signature, sz);
  5146. if (!hwsq_offset)
  5147. return 0;
  5148. /* always use entry 0? */
  5149. return load_nv17_hwsq_ucode_entry(dev, bios, hwsq_offset + sz, 0);
  5150. }
  5151. uint8_t *nouveau_bios_embedded_edid(struct drm_device *dev)
  5152. {
  5153. struct drm_nouveau_private *dev_priv = dev->dev_private;
  5154. struct nvbios *bios = &dev_priv->vbios;
  5155. const uint8_t edid_sig[] = {
  5156. 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00 };
  5157. uint16_t offset = 0;
  5158. uint16_t newoffset;
  5159. int searchlen = NV_PROM_SIZE;
  5160. if (bios->fp.edid)
  5161. return bios->fp.edid;
  5162. while (searchlen) {
  5163. newoffset = findstr(&bios->data[offset], searchlen,
  5164. edid_sig, 8);
  5165. if (!newoffset)
  5166. return NULL;
  5167. offset += newoffset;
  5168. if (!nv_cksum(&bios->data[offset], EDID1_LEN))
  5169. break;
  5170. searchlen -= offset;
  5171. offset++;
  5172. }
  5173. NV_TRACE(dev, "Found EDID in BIOS\n");
  5174. return bios->fp.edid = &bios->data[offset];
  5175. }
  5176. void
  5177. nouveau_bios_run_init_table(struct drm_device *dev, uint16_t table,
  5178. struct dcb_entry *dcbent, int crtc)
  5179. {
  5180. struct drm_nouveau_private *dev_priv = dev->dev_private;
  5181. struct nvbios *bios = &dev_priv->vbios;
  5182. struct init_exec iexec = { true, false };
  5183. spin_lock_bh(&bios->lock);
  5184. bios->display.output = dcbent;
  5185. bios->display.crtc = crtc;
  5186. parse_init_table(bios, table, &iexec);
  5187. bios->display.output = NULL;
  5188. spin_unlock_bh(&bios->lock);
  5189. }
  5190. void
  5191. nouveau_bios_init_exec(struct drm_device *dev, uint16_t table)
  5192. {
  5193. struct drm_nouveau_private *dev_priv = dev->dev_private;
  5194. struct nvbios *bios = &dev_priv->vbios;
  5195. struct init_exec iexec = { true, false };
  5196. parse_init_table(bios, table, &iexec);
  5197. }
  5198. static bool NVInitVBIOS(struct drm_device *dev)
  5199. {
  5200. struct drm_nouveau_private *dev_priv = dev->dev_private;
  5201. struct nvbios *bios = &dev_priv->vbios;
  5202. memset(bios, 0, sizeof(struct nvbios));
  5203. spin_lock_init(&bios->lock);
  5204. bios->dev = dev;
  5205. return _nv_bios(dev, &bios->data, &bios->length);
  5206. }
  5207. static int nouveau_parse_vbios_struct(struct drm_device *dev)
  5208. {
  5209. struct drm_nouveau_private *dev_priv = dev->dev_private;
  5210. struct nvbios *bios = &dev_priv->vbios;
  5211. const uint8_t bit_signature[] = { 0xff, 0xb8, 'B', 'I', 'T' };
  5212. const uint8_t bmp_signature[] = { 0xff, 0x7f, 'N', 'V', 0x0 };
  5213. int offset;
  5214. offset = findstr(bios->data, bios->length,
  5215. bit_signature, sizeof(bit_signature));
  5216. if (offset) {
  5217. NV_TRACE(dev, "BIT BIOS found\n");
  5218. bios->type = NVBIOS_BIT;
  5219. bios->offset = offset;
  5220. return parse_bit_structure(bios, offset + 6);
  5221. }
  5222. offset = findstr(bios->data, bios->length,
  5223. bmp_signature, sizeof(bmp_signature));
  5224. if (offset) {
  5225. NV_TRACE(dev, "BMP BIOS found\n");
  5226. bios->type = NVBIOS_BMP;
  5227. bios->offset = offset;
  5228. return parse_bmp_structure(dev, bios, offset);
  5229. }
  5230. NV_ERROR(dev, "No known BIOS signature found\n");
  5231. return -ENODEV;
  5232. }
  5233. int
  5234. nouveau_run_vbios_init(struct drm_device *dev)
  5235. {
  5236. struct drm_nouveau_private *dev_priv = dev->dev_private;
  5237. struct nvbios *bios = &dev_priv->vbios;
  5238. int i, ret = 0;
  5239. /* Reset the BIOS head to 0. */
  5240. bios->state.crtchead = 0;
  5241. if (bios->major_version < 5) /* BMP only */
  5242. load_nv17_hw_sequencer_ucode(dev, bios);
  5243. if (bios->execute) {
  5244. bios->fp.last_script_invoc = 0;
  5245. bios->fp.lvds_init_run = false;
  5246. }
  5247. parse_init_tables(bios);
  5248. /*
  5249. * Runs some additional script seen on G8x VBIOSen. The VBIOS'
  5250. * parser will run this right after the init tables, the binary
  5251. * driver appears to run it at some point later.
  5252. */
  5253. if (bios->some_script_ptr) {
  5254. struct init_exec iexec = {true, false};
  5255. NV_INFO(dev, "Parsing VBIOS init table at offset 0x%04X\n",
  5256. bios->some_script_ptr);
  5257. parse_init_table(bios, bios->some_script_ptr, &iexec);
  5258. }
  5259. if (dev_priv->card_type >= NV_50) {
  5260. for (i = 0; i < bios->dcb.entries; i++) {
  5261. nouveau_bios_run_display_table(dev, 0, 0,
  5262. &bios->dcb.entry[i], -1);
  5263. }
  5264. }
  5265. return ret;
  5266. }
  5267. static bool
  5268. nouveau_bios_posted(struct drm_device *dev)
  5269. {
  5270. struct drm_nouveau_private *dev_priv = dev->dev_private;
  5271. unsigned htotal;
  5272. if (dev_priv->card_type >= NV_50) {
  5273. if (NVReadVgaCrtc(dev, 0, 0x00) == 0 &&
  5274. NVReadVgaCrtc(dev, 0, 0x1a) == 0)
  5275. return false;
  5276. return true;
  5277. }
  5278. htotal = NVReadVgaCrtc(dev, 0, 0x06);
  5279. htotal |= (NVReadVgaCrtc(dev, 0, 0x07) & 0x01) << 8;
  5280. htotal |= (NVReadVgaCrtc(dev, 0, 0x07) & 0x20) << 4;
  5281. htotal |= (NVReadVgaCrtc(dev, 0, 0x25) & 0x01) << 10;
  5282. htotal |= (NVReadVgaCrtc(dev, 0, 0x41) & 0x01) << 11;
  5283. return (htotal != 0);
  5284. }
  5285. int
  5286. nouveau_bios_init(struct drm_device *dev)
  5287. {
  5288. struct drm_nouveau_private *dev_priv = dev->dev_private;
  5289. struct nvbios *bios = &dev_priv->vbios;
  5290. int ret;
  5291. if (!NVInitVBIOS(dev))
  5292. return -ENODEV;
  5293. ret = nouveau_parse_vbios_struct(dev);
  5294. if (ret)
  5295. return ret;
  5296. ret = nouveau_i2c_init(dev);
  5297. if (ret)
  5298. return ret;
  5299. ret = nouveau_mxm_init(dev);
  5300. if (ret)
  5301. return ret;
  5302. ret = parse_dcb_table(dev, bios);
  5303. if (ret)
  5304. return ret;
  5305. if (!bios->major_version) /* we don't run version 0 bios */
  5306. return 0;
  5307. /* init script execution disabled */
  5308. bios->execute = false;
  5309. /* ... unless card isn't POSTed already */
  5310. if (!nouveau_bios_posted(dev)) {
  5311. NV_INFO(dev, "Adaptor not initialised, "
  5312. "running VBIOS init tables.\n");
  5313. bios->execute = true;
  5314. }
  5315. if (nouveau_force_post)
  5316. bios->execute = true;
  5317. ret = nouveau_run_vbios_init(dev);
  5318. if (ret)
  5319. return ret;
  5320. /* feature_byte on BMP is poor, but init always sets CR4B */
  5321. if (bios->major_version < 5)
  5322. bios->is_mobile = NVReadVgaCrtc(dev, 0, NV_CIO_CRE_4B) & 0x40;
  5323. /* all BIT systems need p_f_m_t for digital_min_front_porch */
  5324. if (bios->is_mobile || bios->major_version >= 5)
  5325. ret = parse_fp_mode_table(dev, bios);
  5326. /* allow subsequent scripts to execute */
  5327. bios->execute = true;
  5328. return 0;
  5329. }
  5330. void
  5331. nouveau_bios_takedown(struct drm_device *dev)
  5332. {
  5333. struct drm_nouveau_private *dev_priv = dev->dev_private;
  5334. nouveau_mxm_fini(dev);
  5335. nouveau_i2c_fini(dev);
  5336. }