nouveau_bios.c 179 KB

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