nouveau_bios.c 174 KB

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