nouveau_bios.c 167 KB

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