nouveau_bios.c 172 KB

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