nouveau_bios.c 188 KB

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