nouveau_bios.c 171 KB

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