nouveau_bios.c 172 KB

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