nouveau_bios.c 175 KB

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