nouveau_bios.c 171 KB

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