nouveau_bios.c 169 KB

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