nouveau_bios.c 175 KB

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