nouveau_bios.c 186 KB

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