nouveau_bios.c 187 KB

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