nouveau_bios.c 186 KB

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