nouveau_bios.c 171 KB

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