nouveau_bios.c 185 KB

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