nouveau_bios.c 172 KB

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