nouveau_bios.c 167 KB

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