nouveau_bios.c 176 KB

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