nouveau_bios.c 175 KB

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