nouveau_bios.c 170 KB

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