nouveau_bios.c 169 KB

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