nouveau_bios.c 187 KB

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