nouveau_bios.c 167 KB

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