nouveau_bios.c 185 KB

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