nouveau_bios.c 170 KB

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