nouveau_bios.c 188 KB

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