nouveau_bios.c 186 KB

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