nouveau_bios.c 188 KB

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