nouveau_bios.c 188 KB

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