nouveau_bios.c 165 KB

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