nouveau_bios.c 186 KB

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