nouveau_bios.c 187 KB

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