nouveau_bios.c 183 KB

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