nouveau_bios.c 167 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112
  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. const uint32_t nv50_gpio_reg[4] = { 0xe104, 0xe108, 0xe280, 0xe284 };
  2113. const uint32_t nv50_gpio_ctl[2] = { 0xe100, 0xe28c };
  2114. const uint8_t *gpio_table = &bios->data[bios->dcb.gpio_table_ptr];
  2115. const uint8_t *gpio_entry;
  2116. int i;
  2117. if (!iexec->execute)
  2118. return 1;
  2119. if (bios->dcb.version != 0x40) {
  2120. NV_ERROR(bios->dev, "DCB table not version 4.0\n");
  2121. return 0;
  2122. }
  2123. if (!bios->dcb.gpio_table_ptr) {
  2124. NV_WARN(bios->dev, "Invalid pointer to INIT_8E table\n");
  2125. return 0;
  2126. }
  2127. gpio_entry = gpio_table + gpio_table[1];
  2128. for (i = 0; i < gpio_table[2]; i++, gpio_entry += gpio_table[3]) {
  2129. uint32_t entry = ROM32(gpio_entry[0]), r, s, v;
  2130. int line = (entry & 0x0000001f);
  2131. BIOSLOG(bios, "0x%04X: Entry: 0x%08X\n", offset, entry);
  2132. if ((entry & 0x0000ff00) == 0x0000ff00)
  2133. continue;
  2134. r = nv50_gpio_reg[line >> 3];
  2135. s = (line & 0x07) << 2;
  2136. v = bios_rd32(bios, r) & ~(0x00000003 << s);
  2137. if (entry & 0x01000000)
  2138. v |= (((entry & 0x60000000) >> 29) ^ 2) << s;
  2139. else
  2140. v |= (((entry & 0x18000000) >> 27) ^ 2) << s;
  2141. bios_wr32(bios, r, v);
  2142. r = nv50_gpio_ctl[line >> 4];
  2143. s = (line & 0x0f);
  2144. v = bios_rd32(bios, r) & ~(0x00010001 << s);
  2145. switch ((entry & 0x06000000) >> 25) {
  2146. case 1:
  2147. v |= (0x00000001 << s);
  2148. break;
  2149. case 2:
  2150. v |= (0x00010000 << s);
  2151. break;
  2152. default:
  2153. break;
  2154. }
  2155. bios_wr32(bios, r, v);
  2156. }
  2157. return 1;
  2158. }
  2159. static int
  2160. init_ram_restrict_zm_reg_group(struct nvbios *bios, uint16_t offset,
  2161. struct init_exec *iexec)
  2162. {
  2163. /*
  2164. * INIT_RAM_RESTRICT_ZM_REG_GROUP opcode: 0x8F ('')
  2165. *
  2166. * offset (8 bit): opcode
  2167. * offset + 1 (32 bit): reg
  2168. * offset + 5 (8 bit): regincrement
  2169. * offset + 6 (8 bit): count
  2170. * offset + 7 (32 bit): value 1,1
  2171. * ...
  2172. *
  2173. * Use the RAMCFG strap of PEXTDEV_BOOT as an index into the table at
  2174. * ram_restrict_table_ptr. The value read from here is 'n', and
  2175. * "value 1,n" gets written to "reg". This repeats "count" times and on
  2176. * each iteration 'm', "reg" increases by "regincrement" and
  2177. * "value m,n" is used. The extent of n is limited by a number read
  2178. * from the 'M' BIT table, herein called "blocklen"
  2179. */
  2180. uint32_t reg = ROM32(bios->data[offset + 1]);
  2181. uint8_t regincrement = bios->data[offset + 5];
  2182. uint8_t count = bios->data[offset + 6];
  2183. uint32_t strap_ramcfg, data;
  2184. /* previously set by 'M' BIT table */
  2185. uint16_t blocklen = bios->ram_restrict_group_count * 4;
  2186. int len = 7 + count * blocklen;
  2187. uint8_t index;
  2188. int i;
  2189. if (!iexec->execute)
  2190. return len;
  2191. if (!blocklen) {
  2192. NV_ERROR(bios->dev,
  2193. "0x%04X: Zero block length - has the M table "
  2194. "been parsed?\n", offset);
  2195. return 0;
  2196. }
  2197. strap_ramcfg = (bios_rd32(bios, NV_PEXTDEV_BOOT_0) >> 2) & 0xf;
  2198. index = bios->data[bios->ram_restrict_tbl_ptr + strap_ramcfg];
  2199. BIOSLOG(bios, "0x%04X: Reg: 0x%08X, RegIncrement: 0x%02X, "
  2200. "Count: 0x%02X, StrapRamCfg: 0x%02X, Index: 0x%02X\n",
  2201. offset, reg, regincrement, count, strap_ramcfg, index);
  2202. for (i = 0; i < count; i++) {
  2203. data = ROM32(bios->data[offset + 7 + index * 4 + blocklen * i]);
  2204. bios_wr32(bios, reg, data);
  2205. reg += regincrement;
  2206. }
  2207. return len;
  2208. }
  2209. static int
  2210. init_copy_zm_reg(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
  2211. {
  2212. /*
  2213. * INIT_COPY_ZM_REG opcode: 0x90 ('')
  2214. *
  2215. * offset (8 bit): opcode
  2216. * offset + 1 (32 bit): src reg
  2217. * offset + 5 (32 bit): dst reg
  2218. *
  2219. * Put contents of "src reg" into "dst reg"
  2220. */
  2221. uint32_t srcreg = ROM32(bios->data[offset + 1]);
  2222. uint32_t dstreg = ROM32(bios->data[offset + 5]);
  2223. if (!iexec->execute)
  2224. return 9;
  2225. bios_wr32(bios, dstreg, bios_rd32(bios, srcreg));
  2226. return 9;
  2227. }
  2228. static int
  2229. init_zm_reg_group_addr_latched(struct nvbios *bios, uint16_t offset,
  2230. struct init_exec *iexec)
  2231. {
  2232. /*
  2233. * INIT_ZM_REG_GROUP_ADDRESS_LATCHED opcode: 0x91 ('')
  2234. *
  2235. * offset (8 bit): opcode
  2236. * offset + 1 (32 bit): dst reg
  2237. * offset + 5 (8 bit): count
  2238. * offset + 6 (32 bit): data 1
  2239. * ...
  2240. *
  2241. * For each of "count" values write "data n" to "dst reg"
  2242. */
  2243. uint32_t reg = ROM32(bios->data[offset + 1]);
  2244. uint8_t count = bios->data[offset + 5];
  2245. int len = 6 + count * 4;
  2246. int i;
  2247. if (!iexec->execute)
  2248. return len;
  2249. for (i = 0; i < count; i++) {
  2250. uint32_t data = ROM32(bios->data[offset + 6 + 4 * i]);
  2251. bios_wr32(bios, reg, data);
  2252. }
  2253. return len;
  2254. }
  2255. static int
  2256. init_reserved(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
  2257. {
  2258. /*
  2259. * INIT_RESERVED opcode: 0x92 ('')
  2260. *
  2261. * offset (8 bit): opcode
  2262. *
  2263. * Seemingly does nothing
  2264. */
  2265. return 1;
  2266. }
  2267. static int
  2268. init_96(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
  2269. {
  2270. /*
  2271. * INIT_96 opcode: 0x96 ('')
  2272. *
  2273. * offset (8 bit): opcode
  2274. * offset + 1 (32 bit): sreg
  2275. * offset + 5 (8 bit): sshift
  2276. * offset + 6 (8 bit): smask
  2277. * offset + 7 (8 bit): index
  2278. * offset + 8 (32 bit): reg
  2279. * offset + 12 (32 bit): mask
  2280. * offset + 16 (8 bit): shift
  2281. *
  2282. */
  2283. uint16_t xlatptr = bios->init96_tbl_ptr + (bios->data[offset + 7] * 2);
  2284. uint32_t reg = ROM32(bios->data[offset + 8]);
  2285. uint32_t mask = ROM32(bios->data[offset + 12]);
  2286. uint32_t val;
  2287. val = bios_rd32(bios, ROM32(bios->data[offset + 1]));
  2288. if (bios->data[offset + 5] < 0x80)
  2289. val >>= bios->data[offset + 5];
  2290. else
  2291. val <<= (0x100 - bios->data[offset + 5]);
  2292. val &= bios->data[offset + 6];
  2293. val = bios->data[ROM16(bios->data[xlatptr]) + val];
  2294. val <<= bios->data[offset + 16];
  2295. if (!iexec->execute)
  2296. return 17;
  2297. bios_wr32(bios, reg, (bios_rd32(bios, reg) & mask) | val);
  2298. return 17;
  2299. }
  2300. static int
  2301. init_97(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
  2302. {
  2303. /*
  2304. * INIT_97 opcode: 0x97 ('')
  2305. *
  2306. * offset (8 bit): opcode
  2307. * offset + 1 (32 bit): register
  2308. * offset + 5 (32 bit): mask
  2309. * offset + 9 (32 bit): value
  2310. *
  2311. * Adds "value" to "register" preserving the fields specified
  2312. * by "mask"
  2313. */
  2314. uint32_t reg = ROM32(bios->data[offset + 1]);
  2315. uint32_t mask = ROM32(bios->data[offset + 5]);
  2316. uint32_t add = ROM32(bios->data[offset + 9]);
  2317. uint32_t val;
  2318. val = bios_rd32(bios, reg);
  2319. val = (val & mask) | ((val + add) & ~mask);
  2320. if (!iexec->execute)
  2321. return 13;
  2322. bios_wr32(bios, reg, val);
  2323. return 13;
  2324. }
  2325. static int
  2326. init_auxch(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
  2327. {
  2328. /*
  2329. * INIT_AUXCH opcode: 0x98 ('')
  2330. *
  2331. * offset (8 bit): opcode
  2332. * offset + 1 (32 bit): address
  2333. * offset + 5 (8 bit): count
  2334. * offset + 6 (8 bit): mask 0
  2335. * offset + 7 (8 bit): data 0
  2336. * ...
  2337. *
  2338. */
  2339. struct drm_device *dev = bios->dev;
  2340. struct nouveau_i2c_chan *auxch;
  2341. uint32_t addr = ROM32(bios->data[offset + 1]);
  2342. uint8_t count = bios->data[offset + 5];
  2343. int len = 6 + count * 2;
  2344. int ret, i;
  2345. if (!bios->display.output) {
  2346. NV_ERROR(dev, "INIT_AUXCH: no active output\n");
  2347. return 0;
  2348. }
  2349. auxch = init_i2c_device_find(dev, bios->display.output->i2c_index);
  2350. if (!auxch) {
  2351. NV_ERROR(dev, "INIT_AUXCH: couldn't get auxch %d\n",
  2352. bios->display.output->i2c_index);
  2353. return 0;
  2354. }
  2355. if (!iexec->execute)
  2356. return len;
  2357. offset += 6;
  2358. for (i = 0; i < count; i++, offset += 2) {
  2359. uint8_t data;
  2360. ret = nouveau_dp_auxch(auxch, 9, addr, &data, 1);
  2361. if (ret) {
  2362. NV_ERROR(dev, "INIT_AUXCH: rd auxch fail %d\n", ret);
  2363. return 0;
  2364. }
  2365. data &= bios->data[offset + 0];
  2366. data |= bios->data[offset + 1];
  2367. ret = nouveau_dp_auxch(auxch, 8, addr, &data, 1);
  2368. if (ret) {
  2369. NV_ERROR(dev, "INIT_AUXCH: wr auxch fail %d\n", ret);
  2370. return 0;
  2371. }
  2372. }
  2373. return len;
  2374. }
  2375. static int
  2376. init_zm_auxch(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
  2377. {
  2378. /*
  2379. * INIT_ZM_AUXCH opcode: 0x99 ('')
  2380. *
  2381. * offset (8 bit): opcode
  2382. * offset + 1 (32 bit): address
  2383. * offset + 5 (8 bit): count
  2384. * offset + 6 (8 bit): data 0
  2385. * ...
  2386. *
  2387. */
  2388. struct drm_device *dev = bios->dev;
  2389. struct nouveau_i2c_chan *auxch;
  2390. uint32_t addr = ROM32(bios->data[offset + 1]);
  2391. uint8_t count = bios->data[offset + 5];
  2392. int len = 6 + count;
  2393. int ret, i;
  2394. if (!bios->display.output) {
  2395. NV_ERROR(dev, "INIT_ZM_AUXCH: no active output\n");
  2396. return 0;
  2397. }
  2398. auxch = init_i2c_device_find(dev, bios->display.output->i2c_index);
  2399. if (!auxch) {
  2400. NV_ERROR(dev, "INIT_ZM_AUXCH: couldn't get auxch %d\n",
  2401. bios->display.output->i2c_index);
  2402. return 0;
  2403. }
  2404. if (!iexec->execute)
  2405. return len;
  2406. offset += 6;
  2407. for (i = 0; i < count; i++, offset++) {
  2408. ret = nouveau_dp_auxch(auxch, 8, addr, &bios->data[offset], 1);
  2409. if (ret) {
  2410. NV_ERROR(dev, "INIT_ZM_AUXCH: wr auxch fail %d\n", ret);
  2411. return 0;
  2412. }
  2413. }
  2414. return len;
  2415. }
  2416. static struct init_tbl_entry itbl_entry[] = {
  2417. /* command name , id , length , offset , mult , command handler */
  2418. /* INIT_PROG (0x31, 15, 10, 4) removed due to no example of use */
  2419. { "INIT_IO_RESTRICT_PROG" , 0x32, init_io_restrict_prog },
  2420. { "INIT_REPEAT" , 0x33, init_repeat },
  2421. { "INIT_IO_RESTRICT_PLL" , 0x34, init_io_restrict_pll },
  2422. { "INIT_END_REPEAT" , 0x36, init_end_repeat },
  2423. { "INIT_COPY" , 0x37, init_copy },
  2424. { "INIT_NOT" , 0x38, init_not },
  2425. { "INIT_IO_FLAG_CONDITION" , 0x39, init_io_flag_condition },
  2426. { "INIT_INDEX_ADDRESS_LATCHED" , 0x49, init_idx_addr_latched },
  2427. { "INIT_IO_RESTRICT_PLL2" , 0x4A, init_io_restrict_pll2 },
  2428. { "INIT_PLL2" , 0x4B, init_pll2 },
  2429. { "INIT_I2C_BYTE" , 0x4C, init_i2c_byte },
  2430. { "INIT_ZM_I2C_BYTE" , 0x4D, init_zm_i2c_byte },
  2431. { "INIT_ZM_I2C" , 0x4E, init_zm_i2c },
  2432. { "INIT_TMDS" , 0x4F, init_tmds },
  2433. { "INIT_ZM_TMDS_GROUP" , 0x50, init_zm_tmds_group },
  2434. { "INIT_CR_INDEX_ADDRESS_LATCHED" , 0x51, init_cr_idx_adr_latch },
  2435. { "INIT_CR" , 0x52, init_cr },
  2436. { "INIT_ZM_CR" , 0x53, init_zm_cr },
  2437. { "INIT_ZM_CR_GROUP" , 0x54, init_zm_cr_group },
  2438. { "INIT_CONDITION_TIME" , 0x56, init_condition_time },
  2439. { "INIT_ZM_REG_SEQUENCE" , 0x58, init_zm_reg_sequence },
  2440. /* INIT_INDIRECT_REG (0x5A, 7, 0, 0) removed due to no example of use */
  2441. { "INIT_SUB_DIRECT" , 0x5B, init_sub_direct },
  2442. { "INIT_COPY_NV_REG" , 0x5F, init_copy_nv_reg },
  2443. { "INIT_ZM_INDEX_IO" , 0x62, init_zm_index_io },
  2444. { "INIT_COMPUTE_MEM" , 0x63, init_compute_mem },
  2445. { "INIT_RESET" , 0x65, init_reset },
  2446. { "INIT_CONFIGURE_MEM" , 0x66, init_configure_mem },
  2447. { "INIT_CONFIGURE_CLK" , 0x67, init_configure_clk },
  2448. { "INIT_CONFIGURE_PREINIT" , 0x68, init_configure_preinit },
  2449. { "INIT_IO" , 0x69, init_io },
  2450. { "INIT_SUB" , 0x6B, init_sub },
  2451. { "INIT_RAM_CONDITION" , 0x6D, init_ram_condition },
  2452. { "INIT_NV_REG" , 0x6E, init_nv_reg },
  2453. { "INIT_MACRO" , 0x6F, init_macro },
  2454. { "INIT_DONE" , 0x71, init_done },
  2455. { "INIT_RESUME" , 0x72, init_resume },
  2456. /* INIT_RAM_CONDITION2 (0x73, 9, 0, 0) removed due to no example of use */
  2457. { "INIT_TIME" , 0x74, init_time },
  2458. { "INIT_CONDITION" , 0x75, init_condition },
  2459. { "INIT_IO_CONDITION" , 0x76, init_io_condition },
  2460. { "INIT_INDEX_IO" , 0x78, init_index_io },
  2461. { "INIT_PLL" , 0x79, init_pll },
  2462. { "INIT_ZM_REG" , 0x7A, init_zm_reg },
  2463. { "INIT_RAM_RESTRICT_PLL" , 0x87, init_ram_restrict_pll },
  2464. { "INIT_8C" , 0x8C, init_8c },
  2465. { "INIT_8D" , 0x8D, init_8d },
  2466. { "INIT_GPIO" , 0x8E, init_gpio },
  2467. { "INIT_RAM_RESTRICT_ZM_REG_GROUP" , 0x8F, init_ram_restrict_zm_reg_group },
  2468. { "INIT_COPY_ZM_REG" , 0x90, init_copy_zm_reg },
  2469. { "INIT_ZM_REG_GROUP_ADDRESS_LATCHED" , 0x91, init_zm_reg_group_addr_latched },
  2470. { "INIT_RESERVED" , 0x92, init_reserved },
  2471. { "INIT_96" , 0x96, init_96 },
  2472. { "INIT_97" , 0x97, init_97 },
  2473. { "INIT_AUXCH" , 0x98, init_auxch },
  2474. { "INIT_ZM_AUXCH" , 0x99, init_zm_auxch },
  2475. { NULL , 0 , NULL }
  2476. };
  2477. #define MAX_TABLE_OPS 1000
  2478. static int
  2479. parse_init_table(struct nvbios *bios, unsigned int offset,
  2480. struct init_exec *iexec)
  2481. {
  2482. /*
  2483. * Parses all commands in an init table.
  2484. *
  2485. * We start out executing all commands found in the init table. Some
  2486. * opcodes may change the status of iexec->execute to SKIP, which will
  2487. * cause the following opcodes to perform no operation until the value
  2488. * is changed back to EXECUTE.
  2489. */
  2490. int count = 0, i, res;
  2491. uint8_t id;
  2492. /*
  2493. * Loop until INIT_DONE causes us to break out of the loop
  2494. * (or until offset > bios length just in case... )
  2495. * (and no more than MAX_TABLE_OPS iterations, just in case... )
  2496. */
  2497. while ((offset < bios->length) && (count++ < MAX_TABLE_OPS)) {
  2498. id = bios->data[offset];
  2499. /* Find matching id in itbl_entry */
  2500. for (i = 0; itbl_entry[i].name && (itbl_entry[i].id != id); i++)
  2501. ;
  2502. if (itbl_entry[i].name) {
  2503. BIOSLOG(bios, "0x%04X: [ (0x%02X) - %s ]\n",
  2504. offset, itbl_entry[i].id, itbl_entry[i].name);
  2505. /* execute eventual command handler */
  2506. res = (*itbl_entry[i].handler)(bios, offset, iexec);
  2507. if (!res)
  2508. break;
  2509. /*
  2510. * Add the offset of the current command including all data
  2511. * of that command. The offset will then be pointing on the
  2512. * next op code.
  2513. */
  2514. offset += res;
  2515. } else {
  2516. NV_ERROR(bios->dev,
  2517. "0x%04X: Init table command not found: "
  2518. "0x%02X\n", offset, id);
  2519. return -ENOENT;
  2520. }
  2521. }
  2522. if (offset >= bios->length)
  2523. NV_WARN(bios->dev,
  2524. "Offset 0x%04X greater than known bios image length. "
  2525. "Corrupt image?\n", offset);
  2526. if (count >= MAX_TABLE_OPS)
  2527. NV_WARN(bios->dev,
  2528. "More than %d opcodes to a table is unlikely, "
  2529. "is the bios image corrupt?\n", MAX_TABLE_OPS);
  2530. return 0;
  2531. }
  2532. static void
  2533. parse_init_tables(struct nvbios *bios)
  2534. {
  2535. /* Loops and calls parse_init_table() for each present table. */
  2536. int i = 0;
  2537. uint16_t table;
  2538. struct init_exec iexec = {true, false};
  2539. if (bios->old_style_init) {
  2540. if (bios->init_script_tbls_ptr)
  2541. parse_init_table(bios, bios->init_script_tbls_ptr, &iexec);
  2542. if (bios->extra_init_script_tbl_ptr)
  2543. parse_init_table(bios, bios->extra_init_script_tbl_ptr, &iexec);
  2544. return;
  2545. }
  2546. while ((table = ROM16(bios->data[bios->init_script_tbls_ptr + i]))) {
  2547. NV_INFO(bios->dev,
  2548. "Parsing VBIOS init table %d at offset 0x%04X\n",
  2549. i / 2, table);
  2550. BIOSLOG(bios, "0x%04X: ------ Executing following commands ------\n", table);
  2551. parse_init_table(bios, table, &iexec);
  2552. i += 2;
  2553. }
  2554. }
  2555. static uint16_t clkcmptable(struct nvbios *bios, uint16_t clktable, int pxclk)
  2556. {
  2557. int compare_record_len, i = 0;
  2558. uint16_t compareclk, scriptptr = 0;
  2559. if (bios->major_version < 5) /* pre BIT */
  2560. compare_record_len = 3;
  2561. else
  2562. compare_record_len = 4;
  2563. do {
  2564. compareclk = ROM16(bios->data[clktable + compare_record_len * i]);
  2565. if (pxclk >= compareclk * 10) {
  2566. if (bios->major_version < 5) {
  2567. uint8_t tmdssub = bios->data[clktable + 2 + compare_record_len * i];
  2568. scriptptr = ROM16(bios->data[bios->init_script_tbls_ptr + tmdssub * 2]);
  2569. } else
  2570. scriptptr = ROM16(bios->data[clktable + 2 + compare_record_len * i]);
  2571. break;
  2572. }
  2573. i++;
  2574. } while (compareclk);
  2575. return scriptptr;
  2576. }
  2577. static void
  2578. run_digital_op_script(struct drm_device *dev, uint16_t scriptptr,
  2579. struct dcb_entry *dcbent, int head, bool dl)
  2580. {
  2581. struct drm_nouveau_private *dev_priv = dev->dev_private;
  2582. struct nvbios *bios = &dev_priv->vbios;
  2583. struct init_exec iexec = {true, false};
  2584. NV_TRACE(dev, "0x%04X: Parsing digital output script table\n",
  2585. scriptptr);
  2586. bios_idxprt_wr(bios, NV_CIO_CRX__COLOR, NV_CIO_CRE_44,
  2587. head ? NV_CIO_CRE_44_HEADB : NV_CIO_CRE_44_HEADA);
  2588. /* note: if dcb entries have been merged, index may be misleading */
  2589. NVWriteVgaCrtc5758(dev, head, 0, dcbent->index);
  2590. parse_init_table(bios, scriptptr, &iexec);
  2591. nv04_dfp_bind_head(dev, dcbent, head, dl);
  2592. }
  2593. static int call_lvds_manufacturer_script(struct drm_device *dev, struct dcb_entry *dcbent, int head, enum LVDS_script script)
  2594. {
  2595. struct drm_nouveau_private *dev_priv = dev->dev_private;
  2596. struct nvbios *bios = &dev_priv->vbios;
  2597. uint8_t sub = bios->data[bios->fp.xlated_entry + script] + (bios->fp.link_c_increment && dcbent->or & OUTPUT_C ? 1 : 0);
  2598. uint16_t scriptofs = ROM16(bios->data[bios->init_script_tbls_ptr + sub * 2]);
  2599. if (!bios->fp.xlated_entry || !sub || !scriptofs)
  2600. return -EINVAL;
  2601. run_digital_op_script(dev, scriptofs, dcbent, head, bios->fp.dual_link);
  2602. if (script == LVDS_PANEL_OFF) {
  2603. /* off-on delay in ms */
  2604. msleep(ROM16(bios->data[bios->fp.xlated_entry + 7]));
  2605. }
  2606. #ifdef __powerpc__
  2607. /* Powerbook specific quirks */
  2608. if ((dev->pci_device & 0xffff) == 0x0179 ||
  2609. (dev->pci_device & 0xffff) == 0x0189 ||
  2610. (dev->pci_device & 0xffff) == 0x0329) {
  2611. if (script == LVDS_RESET) {
  2612. nv_write_tmds(dev, dcbent->or, 0, 0x02, 0x72);
  2613. } else if (script == LVDS_PANEL_ON) {
  2614. bios_wr32(bios, NV_PBUS_DEBUG_DUALHEAD_CTL,
  2615. bios_rd32(bios, NV_PBUS_DEBUG_DUALHEAD_CTL)
  2616. | (1 << 31));
  2617. bios_wr32(bios, NV_PCRTC_GPIO_EXT,
  2618. bios_rd32(bios, NV_PCRTC_GPIO_EXT) | 1);
  2619. } else if (script == LVDS_PANEL_OFF) {
  2620. bios_wr32(bios, NV_PBUS_DEBUG_DUALHEAD_CTL,
  2621. bios_rd32(bios, NV_PBUS_DEBUG_DUALHEAD_CTL)
  2622. & ~(1 << 31));
  2623. bios_wr32(bios, NV_PCRTC_GPIO_EXT,
  2624. bios_rd32(bios, NV_PCRTC_GPIO_EXT) & ~3);
  2625. }
  2626. }
  2627. #endif
  2628. return 0;
  2629. }
  2630. static int run_lvds_table(struct drm_device *dev, struct dcb_entry *dcbent, int head, enum LVDS_script script, int pxclk)
  2631. {
  2632. /*
  2633. * The BIT LVDS table's header has the information to setup the
  2634. * necessary registers. Following the standard 4 byte header are:
  2635. * A bitmask byte and a dual-link transition pxclk value for use in
  2636. * selecting the init script when not using straps; 4 script pointers
  2637. * for panel power, selected by output and on/off; and 8 table pointers
  2638. * for panel init, the needed one determined by output, and bits in the
  2639. * conf byte. These tables are similar to the TMDS tables, consisting
  2640. * of a list of pxclks and script pointers.
  2641. */
  2642. struct drm_nouveau_private *dev_priv = dev->dev_private;
  2643. struct nvbios *bios = &dev_priv->vbios;
  2644. unsigned int outputset = (dcbent->or == 4) ? 1 : 0;
  2645. uint16_t scriptptr = 0, clktable;
  2646. uint8_t clktableptr = 0;
  2647. /*
  2648. * For now we assume version 3.0 table - g80 support will need some
  2649. * changes
  2650. */
  2651. switch (script) {
  2652. case LVDS_INIT:
  2653. return -ENOSYS;
  2654. case LVDS_BACKLIGHT_ON:
  2655. case LVDS_PANEL_ON:
  2656. scriptptr = ROM16(bios->data[bios->fp.lvdsmanufacturerpointer + 7 + outputset * 2]);
  2657. break;
  2658. case LVDS_BACKLIGHT_OFF:
  2659. case LVDS_PANEL_OFF:
  2660. scriptptr = ROM16(bios->data[bios->fp.lvdsmanufacturerpointer + 11 + outputset * 2]);
  2661. break;
  2662. case LVDS_RESET:
  2663. if (dcbent->lvdsconf.use_straps_for_mode) {
  2664. if (bios->fp.dual_link)
  2665. clktableptr += 2;
  2666. if (bios->fp.BITbit1)
  2667. clktableptr++;
  2668. } else {
  2669. /* using EDID */
  2670. uint8_t fallback = bios->data[bios->fp.lvdsmanufacturerpointer + 4];
  2671. int fallbackcmpval = (dcbent->or == 4) ? 4 : 1;
  2672. if (bios->fp.dual_link) {
  2673. clktableptr += 2;
  2674. fallbackcmpval *= 2;
  2675. }
  2676. if (fallbackcmpval & fallback)
  2677. clktableptr++;
  2678. }
  2679. /* adding outputset * 8 may not be correct */
  2680. clktable = ROM16(bios->data[bios->fp.lvdsmanufacturerpointer + 15 + clktableptr * 2 + outputset * 8]);
  2681. if (!clktable) {
  2682. NV_ERROR(dev, "Pixel clock comparison table not found\n");
  2683. return -ENOENT;
  2684. }
  2685. scriptptr = clkcmptable(bios, clktable, pxclk);
  2686. }
  2687. if (!scriptptr) {
  2688. NV_ERROR(dev, "LVDS output init script not found\n");
  2689. return -ENOENT;
  2690. }
  2691. run_digital_op_script(dev, scriptptr, dcbent, head, bios->fp.dual_link);
  2692. return 0;
  2693. }
  2694. int call_lvds_script(struct drm_device *dev, struct dcb_entry *dcbent, int head, enum LVDS_script script, int pxclk)
  2695. {
  2696. /*
  2697. * LVDS operations are multiplexed in an effort to present a single API
  2698. * which works with two vastly differing underlying structures.
  2699. * This acts as the demux
  2700. */
  2701. struct drm_nouveau_private *dev_priv = dev->dev_private;
  2702. struct nvbios *bios = &dev_priv->vbios;
  2703. uint8_t lvds_ver = bios->data[bios->fp.lvdsmanufacturerpointer];
  2704. uint32_t sel_clk_binding, sel_clk;
  2705. int ret;
  2706. if (bios->fp.last_script_invoc == (script << 1 | head) || !lvds_ver ||
  2707. (lvds_ver >= 0x30 && script == LVDS_INIT))
  2708. return 0;
  2709. if (!bios->fp.lvds_init_run) {
  2710. bios->fp.lvds_init_run = true;
  2711. call_lvds_script(dev, dcbent, head, LVDS_INIT, pxclk);
  2712. }
  2713. if (script == LVDS_PANEL_ON && bios->fp.reset_after_pclk_change)
  2714. call_lvds_script(dev, dcbent, head, LVDS_RESET, pxclk);
  2715. if (script == LVDS_RESET && bios->fp.power_off_for_reset)
  2716. call_lvds_script(dev, dcbent, head, LVDS_PANEL_OFF, pxclk);
  2717. NV_TRACE(dev, "Calling LVDS script %d:\n", script);
  2718. /* don't let script change pll->head binding */
  2719. sel_clk_binding = bios_rd32(bios, NV_PRAMDAC_SEL_CLK) & 0x50000;
  2720. if (lvds_ver < 0x30)
  2721. ret = call_lvds_manufacturer_script(dev, dcbent, head, script);
  2722. else
  2723. ret = run_lvds_table(dev, dcbent, head, script, pxclk);
  2724. bios->fp.last_script_invoc = (script << 1 | head);
  2725. sel_clk = NVReadRAMDAC(dev, 0, NV_PRAMDAC_SEL_CLK) & ~0x50000;
  2726. NVWriteRAMDAC(dev, 0, NV_PRAMDAC_SEL_CLK, sel_clk | sel_clk_binding);
  2727. /* some scripts set a value in NV_PBUS_POWERCTRL_2 and break video overlay */
  2728. nvWriteMC(dev, NV_PBUS_POWERCTRL_2, 0);
  2729. return ret;
  2730. }
  2731. struct lvdstableheader {
  2732. uint8_t lvds_ver, headerlen, recordlen;
  2733. };
  2734. static int parse_lvds_manufacturer_table_header(struct drm_device *dev, struct nvbios *bios, struct lvdstableheader *lth)
  2735. {
  2736. /*
  2737. * BMP version (0xa) LVDS table has a simple header of version and
  2738. * record length. The BIT LVDS table has the typical BIT table header:
  2739. * version byte, header length byte, record length byte, and a byte for
  2740. * the maximum number of records that can be held in the table.
  2741. */
  2742. uint8_t lvds_ver, headerlen, recordlen;
  2743. memset(lth, 0, sizeof(struct lvdstableheader));
  2744. if (bios->fp.lvdsmanufacturerpointer == 0x0) {
  2745. NV_ERROR(dev, "Pointer to LVDS manufacturer table invalid\n");
  2746. return -EINVAL;
  2747. }
  2748. lvds_ver = bios->data[bios->fp.lvdsmanufacturerpointer];
  2749. switch (lvds_ver) {
  2750. case 0x0a: /* pre NV40 */
  2751. headerlen = 2;
  2752. recordlen = bios->data[bios->fp.lvdsmanufacturerpointer + 1];
  2753. break;
  2754. case 0x30: /* NV4x */
  2755. headerlen = bios->data[bios->fp.lvdsmanufacturerpointer + 1];
  2756. if (headerlen < 0x1f) {
  2757. NV_ERROR(dev, "LVDS table header not understood\n");
  2758. return -EINVAL;
  2759. }
  2760. recordlen = bios->data[bios->fp.lvdsmanufacturerpointer + 2];
  2761. break;
  2762. case 0x40: /* G80/G90 */
  2763. headerlen = bios->data[bios->fp.lvdsmanufacturerpointer + 1];
  2764. if (headerlen < 0x7) {
  2765. NV_ERROR(dev, "LVDS table header not understood\n");
  2766. return -EINVAL;
  2767. }
  2768. recordlen = bios->data[bios->fp.lvdsmanufacturerpointer + 2];
  2769. break;
  2770. default:
  2771. NV_ERROR(dev,
  2772. "LVDS table revision %d.%d not currently supported\n",
  2773. lvds_ver >> 4, lvds_ver & 0xf);
  2774. return -ENOSYS;
  2775. }
  2776. lth->lvds_ver = lvds_ver;
  2777. lth->headerlen = headerlen;
  2778. lth->recordlen = recordlen;
  2779. return 0;
  2780. }
  2781. static int
  2782. get_fp_strap(struct drm_device *dev, struct nvbios *bios)
  2783. {
  2784. struct drm_nouveau_private *dev_priv = dev->dev_private;
  2785. /*
  2786. * The fp strap is normally dictated by the "User Strap" in
  2787. * PEXTDEV_BOOT_0[20:16], but on BMP cards when bit 2 of the
  2788. * Internal_Flags struct at 0x48 is set, the user strap gets overriden
  2789. * by the PCI subsystem ID during POST, but not before the previous user
  2790. * strap has been committed to CR58 for CR57=0xf on head A, which may be
  2791. * read and used instead
  2792. */
  2793. if (bios->major_version < 5 && bios->data[0x48] & 0x4)
  2794. return NVReadVgaCrtc5758(dev, 0, 0xf) & 0xf;
  2795. if (dev_priv->card_type >= NV_50)
  2796. return (bios_rd32(bios, NV_PEXTDEV_BOOT_0) >> 24) & 0xf;
  2797. else
  2798. return (bios_rd32(bios, NV_PEXTDEV_BOOT_0) >> 16) & 0xf;
  2799. }
  2800. static int parse_fp_mode_table(struct drm_device *dev, struct nvbios *bios)
  2801. {
  2802. uint8_t *fptable;
  2803. uint8_t fptable_ver, headerlen = 0, recordlen, fpentries = 0xf, fpindex;
  2804. int ret, ofs, fpstrapping;
  2805. struct lvdstableheader lth;
  2806. if (bios->fp.fptablepointer == 0x0) {
  2807. /* Apple cards don't have the fp table; the laptops use DDC */
  2808. /* The table is also missing on some x86 IGPs */
  2809. #ifndef __powerpc__
  2810. NV_ERROR(dev, "Pointer to flat panel table invalid\n");
  2811. #endif
  2812. bios->digital_min_front_porch = 0x4b;
  2813. return 0;
  2814. }
  2815. fptable = &bios->data[bios->fp.fptablepointer];
  2816. fptable_ver = fptable[0];
  2817. switch (fptable_ver) {
  2818. /*
  2819. * BMP version 0x5.0x11 BIOSen have version 1 like tables, but no
  2820. * version field, and miss one of the spread spectrum/PWM bytes.
  2821. * This could affect early GF2Go parts (not seen any appropriate ROMs
  2822. * though). Here we assume that a version of 0x05 matches this case
  2823. * (combining with a BMP version check would be better), as the
  2824. * common case for the panel type field is 0x0005, and that is in
  2825. * fact what we are reading the first byte of.
  2826. */
  2827. case 0x05: /* some NV10, 11, 15, 16 */
  2828. recordlen = 42;
  2829. ofs = -1;
  2830. break;
  2831. case 0x10: /* some NV15/16, and NV11+ */
  2832. recordlen = 44;
  2833. ofs = 0;
  2834. break;
  2835. case 0x20: /* NV40+ */
  2836. headerlen = fptable[1];
  2837. recordlen = fptable[2];
  2838. fpentries = fptable[3];
  2839. /*
  2840. * fptable[4] is the minimum
  2841. * RAMDAC_FP_HCRTC -> RAMDAC_FP_HSYNC_START gap
  2842. */
  2843. bios->digital_min_front_porch = fptable[4];
  2844. ofs = -7;
  2845. break;
  2846. default:
  2847. NV_ERROR(dev,
  2848. "FP table revision %d.%d not currently supported\n",
  2849. fptable_ver >> 4, fptable_ver & 0xf);
  2850. return -ENOSYS;
  2851. }
  2852. if (!bios->is_mobile) /* !mobile only needs digital_min_front_porch */
  2853. return 0;
  2854. ret = parse_lvds_manufacturer_table_header(dev, bios, &lth);
  2855. if (ret)
  2856. return ret;
  2857. if (lth.lvds_ver == 0x30 || lth.lvds_ver == 0x40) {
  2858. bios->fp.fpxlatetableptr = bios->fp.lvdsmanufacturerpointer +
  2859. lth.headerlen + 1;
  2860. bios->fp.xlatwidth = lth.recordlen;
  2861. }
  2862. if (bios->fp.fpxlatetableptr == 0x0) {
  2863. NV_ERROR(dev, "Pointer to flat panel xlat table invalid\n");
  2864. return -EINVAL;
  2865. }
  2866. fpstrapping = get_fp_strap(dev, bios);
  2867. fpindex = bios->data[bios->fp.fpxlatetableptr +
  2868. fpstrapping * bios->fp.xlatwidth];
  2869. if (fpindex > fpentries) {
  2870. NV_ERROR(dev, "Bad flat panel table index\n");
  2871. return -ENOENT;
  2872. }
  2873. /* nv4x cards need both a strap value and fpindex of 0xf to use DDC */
  2874. if (lth.lvds_ver > 0x10)
  2875. bios->fp_no_ddc = fpstrapping != 0xf || fpindex != 0xf;
  2876. /*
  2877. * If either the strap or xlated fpindex value are 0xf there is no
  2878. * panel using a strap-derived bios mode present. this condition
  2879. * includes, but is different from, the DDC panel indicator above
  2880. */
  2881. if (fpstrapping == 0xf || fpindex == 0xf)
  2882. return 0;
  2883. bios->fp.mode_ptr = bios->fp.fptablepointer + headerlen +
  2884. recordlen * fpindex + ofs;
  2885. NV_TRACE(dev, "BIOS FP mode: %dx%d (%dkHz pixel clock)\n",
  2886. ROM16(bios->data[bios->fp.mode_ptr + 11]) + 1,
  2887. ROM16(bios->data[bios->fp.mode_ptr + 25]) + 1,
  2888. ROM16(bios->data[bios->fp.mode_ptr + 7]) * 10);
  2889. return 0;
  2890. }
  2891. bool nouveau_bios_fp_mode(struct drm_device *dev, struct drm_display_mode *mode)
  2892. {
  2893. struct drm_nouveau_private *dev_priv = dev->dev_private;
  2894. struct nvbios *bios = &dev_priv->vbios;
  2895. uint8_t *mode_entry = &bios->data[bios->fp.mode_ptr];
  2896. if (!mode) /* just checking whether we can produce a mode */
  2897. return bios->fp.mode_ptr;
  2898. memset(mode, 0, sizeof(struct drm_display_mode));
  2899. /*
  2900. * For version 1.0 (version in byte 0):
  2901. * bytes 1-2 are "panel type", including bits on whether Colour/mono,
  2902. * single/dual link, and type (TFT etc.)
  2903. * bytes 3-6 are bits per colour in RGBX
  2904. */
  2905. mode->clock = ROM16(mode_entry[7]) * 10;
  2906. /* bytes 9-10 is HActive */
  2907. mode->hdisplay = ROM16(mode_entry[11]) + 1;
  2908. /*
  2909. * bytes 13-14 is HValid Start
  2910. * bytes 15-16 is HValid End
  2911. */
  2912. mode->hsync_start = ROM16(mode_entry[17]) + 1;
  2913. mode->hsync_end = ROM16(mode_entry[19]) + 1;
  2914. mode->htotal = ROM16(mode_entry[21]) + 1;
  2915. /* bytes 23-24, 27-30 similarly, but vertical */
  2916. mode->vdisplay = ROM16(mode_entry[25]) + 1;
  2917. mode->vsync_start = ROM16(mode_entry[31]) + 1;
  2918. mode->vsync_end = ROM16(mode_entry[33]) + 1;
  2919. mode->vtotal = ROM16(mode_entry[35]) + 1;
  2920. mode->flags |= (mode_entry[37] & 0x10) ?
  2921. DRM_MODE_FLAG_PHSYNC : DRM_MODE_FLAG_NHSYNC;
  2922. mode->flags |= (mode_entry[37] & 0x1) ?
  2923. DRM_MODE_FLAG_PVSYNC : DRM_MODE_FLAG_NVSYNC;
  2924. /*
  2925. * bytes 38-39 relate to spread spectrum settings
  2926. * bytes 40-43 are something to do with PWM
  2927. */
  2928. mode->status = MODE_OK;
  2929. mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
  2930. drm_mode_set_name(mode);
  2931. return bios->fp.mode_ptr;
  2932. }
  2933. int nouveau_bios_parse_lvds_table(struct drm_device *dev, int pxclk, bool *dl, bool *if_is_24bit)
  2934. {
  2935. /*
  2936. * The LVDS table header is (mostly) described in
  2937. * parse_lvds_manufacturer_table_header(): the BIT header additionally
  2938. * contains the dual-link transition pxclk (in 10s kHz), at byte 5 - if
  2939. * straps are not being used for the panel, this specifies the frequency
  2940. * at which modes should be set up in the dual link style.
  2941. *
  2942. * Following the header, the BMP (ver 0xa) table has several records,
  2943. * indexed by a seperate xlat table, indexed in turn by the fp strap in
  2944. * EXTDEV_BOOT. Each record had a config byte, followed by 6 script
  2945. * numbers for use by INIT_SUB which controlled panel init and power,
  2946. * and finally a dword of ms to sleep between power off and on
  2947. * operations.
  2948. *
  2949. * In the BIT versions, the table following the header serves as an
  2950. * integrated config and xlat table: the records in the table are
  2951. * indexed by the FP strap nibble in EXTDEV_BOOT, and each record has
  2952. * two bytes - the first as a config byte, the second for indexing the
  2953. * fp mode table pointed to by the BIT 'D' table
  2954. *
  2955. * DDC is not used until after card init, so selecting the correct table
  2956. * entry and setting the dual link flag for EDID equipped panels,
  2957. * requiring tests against the native-mode pixel clock, cannot be done
  2958. * until later, when this function should be called with non-zero pxclk
  2959. */
  2960. struct drm_nouveau_private *dev_priv = dev->dev_private;
  2961. struct nvbios *bios = &dev_priv->vbios;
  2962. int fpstrapping = get_fp_strap(dev, bios), lvdsmanufacturerindex = 0;
  2963. struct lvdstableheader lth;
  2964. uint16_t lvdsofs;
  2965. int ret, chip_version = bios->chip_version;
  2966. ret = parse_lvds_manufacturer_table_header(dev, bios, &lth);
  2967. if (ret)
  2968. return ret;
  2969. switch (lth.lvds_ver) {
  2970. case 0x0a: /* pre NV40 */
  2971. lvdsmanufacturerindex = bios->data[
  2972. bios->fp.fpxlatemanufacturertableptr +
  2973. fpstrapping];
  2974. /* we're done if this isn't the EDID panel case */
  2975. if (!pxclk)
  2976. break;
  2977. if (chip_version < 0x25) {
  2978. /* nv17 behaviour
  2979. *
  2980. * It seems the old style lvds script pointer is reused
  2981. * to select 18/24 bit colour depth for EDID panels.
  2982. */
  2983. lvdsmanufacturerindex =
  2984. (bios->legacy.lvds_single_a_script_ptr & 1) ?
  2985. 2 : 0;
  2986. if (pxclk >= bios->fp.duallink_transition_clk)
  2987. lvdsmanufacturerindex++;
  2988. } else if (chip_version < 0x30) {
  2989. /* nv28 behaviour (off-chip encoder)
  2990. *
  2991. * nv28 does a complex dance of first using byte 121 of
  2992. * the EDID to choose the lvdsmanufacturerindex, then
  2993. * later attempting to match the EDID manufacturer and
  2994. * product IDs in a table (signature 'pidt' (panel id
  2995. * table?)), setting an lvdsmanufacturerindex of 0 and
  2996. * an fp strap of the match index (or 0xf if none)
  2997. */
  2998. lvdsmanufacturerindex = 0;
  2999. } else {
  3000. /* nv31, nv34 behaviour */
  3001. lvdsmanufacturerindex = 0;
  3002. if (pxclk >= bios->fp.duallink_transition_clk)
  3003. lvdsmanufacturerindex = 2;
  3004. if (pxclk >= 140000)
  3005. lvdsmanufacturerindex = 3;
  3006. }
  3007. /*
  3008. * nvidia set the high nibble of (cr57=f, cr58) to
  3009. * lvdsmanufacturerindex in this case; we don't
  3010. */
  3011. break;
  3012. case 0x30: /* NV4x */
  3013. case 0x40: /* G80/G90 */
  3014. lvdsmanufacturerindex = fpstrapping;
  3015. break;
  3016. default:
  3017. NV_ERROR(dev, "LVDS table revision not currently supported\n");
  3018. return -ENOSYS;
  3019. }
  3020. lvdsofs = bios->fp.xlated_entry = bios->fp.lvdsmanufacturerpointer + lth.headerlen + lth.recordlen * lvdsmanufacturerindex;
  3021. switch (lth.lvds_ver) {
  3022. case 0x0a:
  3023. bios->fp.power_off_for_reset = bios->data[lvdsofs] & 1;
  3024. bios->fp.reset_after_pclk_change = bios->data[lvdsofs] & 2;
  3025. bios->fp.dual_link = bios->data[lvdsofs] & 4;
  3026. bios->fp.link_c_increment = bios->data[lvdsofs] & 8;
  3027. *if_is_24bit = bios->data[lvdsofs] & 16;
  3028. break;
  3029. case 0x30:
  3030. /*
  3031. * My money would be on there being a 24 bit interface bit in
  3032. * this table, but I have no example of a laptop bios with a
  3033. * 24 bit panel to confirm that. Hence we shout loudly if any
  3034. * bit other than bit 0 is set (I've not even seen bit 1)
  3035. */
  3036. if (bios->data[lvdsofs] > 1)
  3037. NV_ERROR(dev,
  3038. "You have a very unusual laptop display; please report it\n");
  3039. /*
  3040. * No sign of the "power off for reset" or "reset for panel
  3041. * on" bits, but it's safer to assume we should
  3042. */
  3043. bios->fp.power_off_for_reset = true;
  3044. bios->fp.reset_after_pclk_change = true;
  3045. /*
  3046. * It's ok lvdsofs is wrong for nv4x edid case; dual_link is
  3047. * over-written, and BITbit1 isn't used
  3048. */
  3049. bios->fp.dual_link = bios->data[lvdsofs] & 1;
  3050. bios->fp.BITbit1 = bios->data[lvdsofs] & 2;
  3051. bios->fp.duallink_transition_clk = ROM16(bios->data[bios->fp.lvdsmanufacturerpointer + 5]) * 10;
  3052. break;
  3053. case 0x40:
  3054. bios->fp.dual_link = bios->data[lvdsofs] & 1;
  3055. bios->fp.if_is_24bit = bios->data[lvdsofs] & 2;
  3056. bios->fp.strapless_is_24bit = bios->data[bios->fp.lvdsmanufacturerpointer + 4];
  3057. bios->fp.duallink_transition_clk = ROM16(bios->data[bios->fp.lvdsmanufacturerpointer + 5]) * 10;
  3058. break;
  3059. }
  3060. /* set dual_link flag for EDID case */
  3061. if (pxclk && (chip_version < 0x25 || chip_version > 0x28))
  3062. bios->fp.dual_link = (pxclk >= bios->fp.duallink_transition_clk);
  3063. *dl = bios->fp.dual_link;
  3064. return 0;
  3065. }
  3066. static uint8_t *
  3067. bios_output_config_match(struct drm_device *dev, struct dcb_entry *dcbent,
  3068. uint16_t record, int record_len, int record_nr)
  3069. {
  3070. struct drm_nouveau_private *dev_priv = dev->dev_private;
  3071. struct nvbios *bios = &dev_priv->vbios;
  3072. uint32_t entry;
  3073. uint16_t table;
  3074. int i, v;
  3075. for (i = 0; i < record_nr; i++, record += record_len) {
  3076. table = ROM16(bios->data[record]);
  3077. if (!table)
  3078. continue;
  3079. entry = ROM32(bios->data[table]);
  3080. v = (entry & 0x000f0000) >> 16;
  3081. if (!(v & dcbent->or))
  3082. continue;
  3083. v = (entry & 0x000000f0) >> 4;
  3084. if (v != dcbent->location)
  3085. continue;
  3086. v = (entry & 0x0000000f);
  3087. if (v != dcbent->type)
  3088. continue;
  3089. return &bios->data[table];
  3090. }
  3091. return NULL;
  3092. }
  3093. void *
  3094. nouveau_bios_dp_table(struct drm_device *dev, struct dcb_entry *dcbent,
  3095. int *length)
  3096. {
  3097. struct drm_nouveau_private *dev_priv = dev->dev_private;
  3098. struct nvbios *bios = &dev_priv->vbios;
  3099. uint8_t *table;
  3100. if (!bios->display.dp_table_ptr) {
  3101. NV_ERROR(dev, "No pointer to DisplayPort table\n");
  3102. return NULL;
  3103. }
  3104. table = &bios->data[bios->display.dp_table_ptr];
  3105. if (table[0] != 0x20 && table[0] != 0x21) {
  3106. NV_ERROR(dev, "DisplayPort table version 0x%02x unknown\n",
  3107. table[0]);
  3108. return NULL;
  3109. }
  3110. *length = table[4];
  3111. return bios_output_config_match(dev, dcbent,
  3112. bios->display.dp_table_ptr + table[1],
  3113. table[2], table[3]);
  3114. }
  3115. int
  3116. nouveau_bios_run_display_table(struct drm_device *dev, struct dcb_entry *dcbent,
  3117. uint32_t sub, int pxclk)
  3118. {
  3119. /*
  3120. * The display script table is located by the BIT 'U' table.
  3121. *
  3122. * It contains an array of pointers to various tables describing
  3123. * a particular output type. The first 32-bits of the output
  3124. * tables contains similar information to a DCB entry, and is
  3125. * used to decide whether that particular table is suitable for
  3126. * the output you want to access.
  3127. *
  3128. * The "record header length" field here seems to indicate the
  3129. * offset of the first configuration entry in the output tables.
  3130. * This is 10 on most cards I've seen, but 12 has been witnessed
  3131. * on DP cards, and there's another script pointer within the
  3132. * header.
  3133. *
  3134. * offset + 0 ( 8 bits): version
  3135. * offset + 1 ( 8 bits): header length
  3136. * offset + 2 ( 8 bits): record length
  3137. * offset + 3 ( 8 bits): number of records
  3138. * offset + 4 ( 8 bits): record header length
  3139. * offset + 5 (16 bits): pointer to first output script table
  3140. */
  3141. struct drm_nouveau_private *dev_priv = dev->dev_private;
  3142. struct nvbios *bios = &dev_priv->vbios;
  3143. uint8_t *table = &bios->data[bios->display.script_table_ptr];
  3144. uint8_t *otable = NULL;
  3145. uint16_t script;
  3146. int i = 0;
  3147. if (!bios->display.script_table_ptr) {
  3148. NV_ERROR(dev, "No pointer to output script table\n");
  3149. return 1;
  3150. }
  3151. /*
  3152. * Nothing useful has been in any of the pre-2.0 tables I've seen,
  3153. * so until they are, we really don't need to care.
  3154. */
  3155. if (table[0] < 0x20)
  3156. return 1;
  3157. if (table[0] != 0x20 && table[0] != 0x21) {
  3158. NV_ERROR(dev, "Output script table version 0x%02x unknown\n",
  3159. table[0]);
  3160. return 1;
  3161. }
  3162. /*
  3163. * The output script tables describing a particular output type
  3164. * look as follows:
  3165. *
  3166. * offset + 0 (32 bits): output this table matches (hash of DCB)
  3167. * offset + 4 ( 8 bits): unknown
  3168. * offset + 5 ( 8 bits): number of configurations
  3169. * offset + 6 (16 bits): pointer to some script
  3170. * offset + 8 (16 bits): pointer to some script
  3171. *
  3172. * headerlen == 10
  3173. * offset + 10 : configuration 0
  3174. *
  3175. * headerlen == 12
  3176. * offset + 10 : pointer to some script
  3177. * offset + 12 : configuration 0
  3178. *
  3179. * Each config entry is as follows:
  3180. *
  3181. * offset + 0 (16 bits): unknown, assumed to be a match value
  3182. * offset + 2 (16 bits): pointer to script table (clock set?)
  3183. * offset + 4 (16 bits): pointer to script table (reset?)
  3184. *
  3185. * There doesn't appear to be a count value to say how many
  3186. * entries exist in each script table, instead, a 0 value in
  3187. * the first 16-bit word seems to indicate both the end of the
  3188. * list and the default entry. The second 16-bit word in the
  3189. * script tables is a pointer to the script to execute.
  3190. */
  3191. NV_DEBUG_KMS(dev, "Searching for output entry for %d %d %d\n",
  3192. dcbent->type, dcbent->location, dcbent->or);
  3193. otable = bios_output_config_match(dev, dcbent, table[1] +
  3194. bios->display.script_table_ptr,
  3195. table[2], table[3]);
  3196. if (!otable) {
  3197. NV_ERROR(dev, "Couldn't find matching output script table\n");
  3198. return 1;
  3199. }
  3200. if (pxclk < -2 || pxclk > 0) {
  3201. /* Try to find matching script table entry */
  3202. for (i = 0; i < otable[5]; i++) {
  3203. if (ROM16(otable[table[4] + i*6]) == sub)
  3204. break;
  3205. }
  3206. if (i == otable[5]) {
  3207. NV_ERROR(dev, "Table 0x%04x not found for %d/%d, "
  3208. "using first\n",
  3209. sub, dcbent->type, dcbent->or);
  3210. i = 0;
  3211. }
  3212. }
  3213. if (pxclk == 0) {
  3214. script = ROM16(otable[6]);
  3215. if (!script) {
  3216. NV_DEBUG_KMS(dev, "output script 0 not found\n");
  3217. return 1;
  3218. }
  3219. NV_TRACE(dev, "0x%04X: parsing output script 0\n", script);
  3220. nouveau_bios_run_init_table(dev, script, dcbent);
  3221. } else
  3222. if (pxclk == -1) {
  3223. script = ROM16(otable[8]);
  3224. if (!script) {
  3225. NV_DEBUG_KMS(dev, "output script 1 not found\n");
  3226. return 1;
  3227. }
  3228. NV_TRACE(dev, "0x%04X: parsing output script 1\n", script);
  3229. nouveau_bios_run_init_table(dev, script, dcbent);
  3230. } else
  3231. if (pxclk == -2) {
  3232. if (table[4] >= 12)
  3233. script = ROM16(otable[10]);
  3234. else
  3235. script = 0;
  3236. if (!script) {
  3237. NV_DEBUG_KMS(dev, "output script 2 not found\n");
  3238. return 1;
  3239. }
  3240. NV_TRACE(dev, "0x%04X: parsing output script 2\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 + 2]);
  3245. if (script)
  3246. script = clkcmptable(bios, script, pxclk);
  3247. if (!script) {
  3248. NV_ERROR(dev, "clock script 0 not found\n");
  3249. return 1;
  3250. }
  3251. NV_TRACE(dev, "0x%04X: parsing clock script 0\n", script);
  3252. nouveau_bios_run_init_table(dev, script, dcbent);
  3253. } else
  3254. if (pxclk < 0) {
  3255. script = ROM16(otable[table[4] + i*6 + 4]);
  3256. if (script)
  3257. script = clkcmptable(bios, script, -pxclk);
  3258. if (!script) {
  3259. NV_DEBUG_KMS(dev, "clock script 1 not found\n");
  3260. return 1;
  3261. }
  3262. NV_TRACE(dev, "0x%04X: parsing clock script 1\n", script);
  3263. nouveau_bios_run_init_table(dev, script, dcbent);
  3264. }
  3265. return 0;
  3266. }
  3267. int run_tmds_table(struct drm_device *dev, struct dcb_entry *dcbent, int head, int pxclk)
  3268. {
  3269. /*
  3270. * the pxclk parameter is in kHz
  3271. *
  3272. * This runs the TMDS regs setting code found on BIT bios cards
  3273. *
  3274. * For ffs(or) == 1 use the first table, for ffs(or) == 2 and
  3275. * ffs(or) == 3, use the second.
  3276. */
  3277. struct drm_nouveau_private *dev_priv = dev->dev_private;
  3278. struct nvbios *bios = &dev_priv->vbios;
  3279. int cv = bios->chip_version;
  3280. uint16_t clktable = 0, scriptptr;
  3281. uint32_t sel_clk_binding, sel_clk;
  3282. /* pre-nv17 off-chip tmds uses scripts, post nv17 doesn't */
  3283. if (cv >= 0x17 && cv != 0x1a && cv != 0x20 &&
  3284. dcbent->location != DCB_LOC_ON_CHIP)
  3285. return 0;
  3286. switch (ffs(dcbent->or)) {
  3287. case 1:
  3288. clktable = bios->tmds.output0_script_ptr;
  3289. break;
  3290. case 2:
  3291. case 3:
  3292. clktable = bios->tmds.output1_script_ptr;
  3293. break;
  3294. }
  3295. if (!clktable) {
  3296. NV_ERROR(dev, "Pixel clock comparison table not found\n");
  3297. return -EINVAL;
  3298. }
  3299. scriptptr = clkcmptable(bios, clktable, pxclk);
  3300. if (!scriptptr) {
  3301. NV_ERROR(dev, "TMDS output init script not found\n");
  3302. return -ENOENT;
  3303. }
  3304. /* don't let script change pll->head binding */
  3305. sel_clk_binding = bios_rd32(bios, NV_PRAMDAC_SEL_CLK) & 0x50000;
  3306. run_digital_op_script(dev, scriptptr, dcbent, head, pxclk >= 165000);
  3307. sel_clk = NVReadRAMDAC(dev, 0, NV_PRAMDAC_SEL_CLK) & ~0x50000;
  3308. NVWriteRAMDAC(dev, 0, NV_PRAMDAC_SEL_CLK, sel_clk | sel_clk_binding);
  3309. return 0;
  3310. }
  3311. int get_pll_limits(struct drm_device *dev, uint32_t limit_match, struct pll_lims *pll_lim)
  3312. {
  3313. /*
  3314. * PLL limits table
  3315. *
  3316. * Version 0x10: NV30, NV31
  3317. * One byte header (version), one record of 24 bytes
  3318. * Version 0x11: NV36 - Not implemented
  3319. * Seems to have same record style as 0x10, but 3 records rather than 1
  3320. * Version 0x20: Found on Geforce 6 cards
  3321. * Trivial 4 byte BIT header. 31 (0x1f) byte record length
  3322. * Version 0x21: Found on Geforce 7, 8 and some Geforce 6 cards
  3323. * 5 byte header, fifth byte of unknown purpose. 35 (0x23) byte record
  3324. * length in general, some (integrated) have an extra configuration byte
  3325. * Version 0x30: Found on Geforce 8, separates the register mapping
  3326. * from the limits tables.
  3327. */
  3328. struct drm_nouveau_private *dev_priv = dev->dev_private;
  3329. struct nvbios *bios = &dev_priv->vbios;
  3330. int cv = bios->chip_version, pllindex = 0;
  3331. uint8_t pll_lim_ver = 0, headerlen = 0, recordlen = 0, entries = 0;
  3332. uint32_t crystal_strap_mask, crystal_straps;
  3333. if (!bios->pll_limit_tbl_ptr) {
  3334. if (cv == 0x30 || cv == 0x31 || cv == 0x35 || cv == 0x36 ||
  3335. cv >= 0x40) {
  3336. NV_ERROR(dev, "Pointer to PLL limits table invalid\n");
  3337. return -EINVAL;
  3338. }
  3339. } else
  3340. pll_lim_ver = bios->data[bios->pll_limit_tbl_ptr];
  3341. crystal_strap_mask = 1 << 6;
  3342. /* open coded dev->twoHeads test */
  3343. if (cv > 0x10 && cv != 0x15 && cv != 0x1a && cv != 0x20)
  3344. crystal_strap_mask |= 1 << 22;
  3345. crystal_straps = nvReadEXTDEV(dev, NV_PEXTDEV_BOOT_0) &
  3346. crystal_strap_mask;
  3347. switch (pll_lim_ver) {
  3348. /*
  3349. * We use version 0 to indicate a pre limit table bios (single stage
  3350. * pll) and load the hard coded limits instead.
  3351. */
  3352. case 0:
  3353. break;
  3354. case 0x10:
  3355. case 0x11:
  3356. /*
  3357. * Strictly v0x11 has 3 entries, but the last two don't seem
  3358. * to get used.
  3359. */
  3360. headerlen = 1;
  3361. recordlen = 0x18;
  3362. entries = 1;
  3363. pllindex = 0;
  3364. break;
  3365. case 0x20:
  3366. case 0x21:
  3367. case 0x30:
  3368. case 0x40:
  3369. headerlen = bios->data[bios->pll_limit_tbl_ptr + 1];
  3370. recordlen = bios->data[bios->pll_limit_tbl_ptr + 2];
  3371. entries = bios->data[bios->pll_limit_tbl_ptr + 3];
  3372. break;
  3373. default:
  3374. NV_ERROR(dev, "PLL limits table revision 0x%X not currently "
  3375. "supported\n", pll_lim_ver);
  3376. return -ENOSYS;
  3377. }
  3378. /* initialize all members to zero */
  3379. memset(pll_lim, 0, sizeof(struct pll_lims));
  3380. if (pll_lim_ver == 0x10 || pll_lim_ver == 0x11) {
  3381. uint8_t *pll_rec = &bios->data[bios->pll_limit_tbl_ptr + headerlen + recordlen * pllindex];
  3382. pll_lim->vco1.minfreq = ROM32(pll_rec[0]);
  3383. pll_lim->vco1.maxfreq = ROM32(pll_rec[4]);
  3384. pll_lim->vco2.minfreq = ROM32(pll_rec[8]);
  3385. pll_lim->vco2.maxfreq = ROM32(pll_rec[12]);
  3386. pll_lim->vco1.min_inputfreq = ROM32(pll_rec[16]);
  3387. pll_lim->vco2.min_inputfreq = ROM32(pll_rec[20]);
  3388. pll_lim->vco1.max_inputfreq = pll_lim->vco2.max_inputfreq = INT_MAX;
  3389. /* these values taken from nv30/31/36 */
  3390. pll_lim->vco1.min_n = 0x1;
  3391. if (cv == 0x36)
  3392. pll_lim->vco1.min_n = 0x5;
  3393. pll_lim->vco1.max_n = 0xff;
  3394. pll_lim->vco1.min_m = 0x1;
  3395. pll_lim->vco1.max_m = 0xd;
  3396. pll_lim->vco2.min_n = 0x4;
  3397. /*
  3398. * On nv30, 31, 36 (i.e. all cards with two stage PLLs with this
  3399. * table version (apart from nv35)), N2 is compared to
  3400. * maxN2 (0x46) and 10 * maxM2 (0x4), so set maxN2 to 0x28 and
  3401. * save a comparison
  3402. */
  3403. pll_lim->vco2.max_n = 0x28;
  3404. if (cv == 0x30 || cv == 0x35)
  3405. /* only 5 bits available for N2 on nv30/35 */
  3406. pll_lim->vco2.max_n = 0x1f;
  3407. pll_lim->vco2.min_m = 0x1;
  3408. pll_lim->vco2.max_m = 0x4;
  3409. pll_lim->max_log2p = 0x7;
  3410. pll_lim->max_usable_log2p = 0x6;
  3411. } else if (pll_lim_ver == 0x20 || pll_lim_ver == 0x21) {
  3412. uint16_t plloffs = bios->pll_limit_tbl_ptr + headerlen;
  3413. uint32_t reg = 0; /* default match */
  3414. uint8_t *pll_rec;
  3415. int i;
  3416. /*
  3417. * First entry is default match, if nothing better. warn if
  3418. * reg field nonzero
  3419. */
  3420. if (ROM32(bios->data[plloffs]))
  3421. NV_WARN(dev, "Default PLL limit entry has non-zero "
  3422. "register field\n");
  3423. if (limit_match > MAX_PLL_TYPES)
  3424. /* we've been passed a reg as the match */
  3425. reg = limit_match;
  3426. else /* limit match is a pll type */
  3427. for (i = 1; i < entries && !reg; i++) {
  3428. uint32_t cmpreg = ROM32(bios->data[plloffs + recordlen * i]);
  3429. if (limit_match == NVPLL &&
  3430. (cmpreg == NV_PRAMDAC_NVPLL_COEFF || cmpreg == 0x4000))
  3431. reg = cmpreg;
  3432. if (limit_match == MPLL &&
  3433. (cmpreg == NV_PRAMDAC_MPLL_COEFF || cmpreg == 0x4020))
  3434. reg = cmpreg;
  3435. if (limit_match == VPLL1 &&
  3436. (cmpreg == NV_PRAMDAC_VPLL_COEFF || cmpreg == 0x4010))
  3437. reg = cmpreg;
  3438. if (limit_match == VPLL2 &&
  3439. (cmpreg == NV_RAMDAC_VPLL2 || cmpreg == 0x4018))
  3440. reg = cmpreg;
  3441. }
  3442. for (i = 1; i < entries; i++)
  3443. if (ROM32(bios->data[plloffs + recordlen * i]) == reg) {
  3444. pllindex = i;
  3445. break;
  3446. }
  3447. pll_rec = &bios->data[plloffs + recordlen * pllindex];
  3448. BIOSLOG(bios, "Loading PLL limits for reg 0x%08x\n",
  3449. pllindex ? reg : 0);
  3450. /*
  3451. * Frequencies are stored in tables in MHz, kHz are more
  3452. * useful, so we convert.
  3453. */
  3454. /* What output frequencies can each VCO generate? */
  3455. pll_lim->vco1.minfreq = ROM16(pll_rec[4]) * 1000;
  3456. pll_lim->vco1.maxfreq = ROM16(pll_rec[6]) * 1000;
  3457. pll_lim->vco2.minfreq = ROM16(pll_rec[8]) * 1000;
  3458. pll_lim->vco2.maxfreq = ROM16(pll_rec[10]) * 1000;
  3459. /* What input frequencies they accept (past the m-divider)? */
  3460. pll_lim->vco1.min_inputfreq = ROM16(pll_rec[12]) * 1000;
  3461. pll_lim->vco2.min_inputfreq = ROM16(pll_rec[14]) * 1000;
  3462. pll_lim->vco1.max_inputfreq = ROM16(pll_rec[16]) * 1000;
  3463. pll_lim->vco2.max_inputfreq = ROM16(pll_rec[18]) * 1000;
  3464. /* What values are accepted as multiplier and divider? */
  3465. pll_lim->vco1.min_n = pll_rec[20];
  3466. pll_lim->vco1.max_n = pll_rec[21];
  3467. pll_lim->vco1.min_m = pll_rec[22];
  3468. pll_lim->vco1.max_m = pll_rec[23];
  3469. pll_lim->vco2.min_n = pll_rec[24];
  3470. pll_lim->vco2.max_n = pll_rec[25];
  3471. pll_lim->vco2.min_m = pll_rec[26];
  3472. pll_lim->vco2.max_m = pll_rec[27];
  3473. pll_lim->max_usable_log2p = pll_lim->max_log2p = pll_rec[29];
  3474. if (pll_lim->max_log2p > 0x7)
  3475. /* pll decoding in nv_hw.c assumes never > 7 */
  3476. NV_WARN(dev, "Max log2 P value greater than 7 (%d)\n",
  3477. pll_lim->max_log2p);
  3478. if (cv < 0x60)
  3479. pll_lim->max_usable_log2p = 0x6;
  3480. pll_lim->log2p_bias = pll_rec[30];
  3481. if (recordlen > 0x22)
  3482. pll_lim->refclk = ROM32(pll_rec[31]);
  3483. if (recordlen > 0x23 && pll_rec[35])
  3484. NV_WARN(dev,
  3485. "Bits set in PLL configuration byte (%x)\n",
  3486. pll_rec[35]);
  3487. /* C51 special not seen elsewhere */
  3488. if (cv == 0x51 && !pll_lim->refclk) {
  3489. uint32_t sel_clk = bios_rd32(bios, NV_PRAMDAC_SEL_CLK);
  3490. if (((limit_match == NV_PRAMDAC_VPLL_COEFF || limit_match == VPLL1) && sel_clk & 0x20) ||
  3491. ((limit_match == NV_RAMDAC_VPLL2 || limit_match == VPLL2) && sel_clk & 0x80)) {
  3492. if (bios_idxprt_rd(bios, NV_CIO_CRX__COLOR, NV_CIO_CRE_CHIP_ID_INDEX) < 0xa3)
  3493. pll_lim->refclk = 200000;
  3494. else
  3495. pll_lim->refclk = 25000;
  3496. }
  3497. }
  3498. } else if (pll_lim_ver == 0x30) { /* ver 0x30 */
  3499. uint8_t *entry = &bios->data[bios->pll_limit_tbl_ptr + headerlen];
  3500. uint8_t *record = NULL;
  3501. int i;
  3502. BIOSLOG(bios, "Loading PLL limits for register 0x%08x\n",
  3503. limit_match);
  3504. for (i = 0; i < entries; i++, entry += recordlen) {
  3505. if (ROM32(entry[3]) == limit_match) {
  3506. record = &bios->data[ROM16(entry[1])];
  3507. break;
  3508. }
  3509. }
  3510. if (!record) {
  3511. NV_ERROR(dev, "Register 0x%08x not found in PLL "
  3512. "limits table", limit_match);
  3513. return -ENOENT;
  3514. }
  3515. pll_lim->vco1.minfreq = ROM16(record[0]) * 1000;
  3516. pll_lim->vco1.maxfreq = ROM16(record[2]) * 1000;
  3517. pll_lim->vco2.minfreq = ROM16(record[4]) * 1000;
  3518. pll_lim->vco2.maxfreq = ROM16(record[6]) * 1000;
  3519. pll_lim->vco1.min_inputfreq = ROM16(record[8]) * 1000;
  3520. pll_lim->vco2.min_inputfreq = ROM16(record[10]) * 1000;
  3521. pll_lim->vco1.max_inputfreq = ROM16(record[12]) * 1000;
  3522. pll_lim->vco2.max_inputfreq = ROM16(record[14]) * 1000;
  3523. pll_lim->vco1.min_n = record[16];
  3524. pll_lim->vco1.max_n = record[17];
  3525. pll_lim->vco1.min_m = record[18];
  3526. pll_lim->vco1.max_m = record[19];
  3527. pll_lim->vco2.min_n = record[20];
  3528. pll_lim->vco2.max_n = record[21];
  3529. pll_lim->vco2.min_m = record[22];
  3530. pll_lim->vco2.max_m = record[23];
  3531. pll_lim->max_usable_log2p = pll_lim->max_log2p = record[25];
  3532. pll_lim->log2p_bias = record[27];
  3533. pll_lim->refclk = ROM32(record[28]);
  3534. } else if (pll_lim_ver) { /* ver 0x40 */
  3535. uint8_t *entry = &bios->data[bios->pll_limit_tbl_ptr + headerlen];
  3536. uint8_t *record = NULL;
  3537. int i;
  3538. BIOSLOG(bios, "Loading PLL limits for register 0x%08x\n",
  3539. limit_match);
  3540. for (i = 0; i < entries; i++, entry += recordlen) {
  3541. if (ROM32(entry[3]) == limit_match) {
  3542. record = &bios->data[ROM16(entry[1])];
  3543. break;
  3544. }
  3545. }
  3546. if (!record) {
  3547. NV_ERROR(dev, "Register 0x%08x not found in PLL "
  3548. "limits table", limit_match);
  3549. return -ENOENT;
  3550. }
  3551. pll_lim->vco1.minfreq = ROM16(record[0]) * 1000;
  3552. pll_lim->vco1.maxfreq = ROM16(record[2]) * 1000;
  3553. pll_lim->vco1.min_inputfreq = ROM16(record[4]) * 1000;
  3554. pll_lim->vco1.max_inputfreq = ROM16(record[6]) * 1000;
  3555. pll_lim->vco1.min_m = record[8];
  3556. pll_lim->vco1.max_m = record[9];
  3557. pll_lim->vco1.min_n = record[10];
  3558. pll_lim->vco1.max_n = record[11];
  3559. pll_lim->min_p = record[12];
  3560. pll_lim->max_p = record[13];
  3561. /* where did this go to?? */
  3562. if (limit_match == 0x00614100 || limit_match == 0x00614900)
  3563. pll_lim->refclk = 27000;
  3564. else
  3565. pll_lim->refclk = 100000;
  3566. }
  3567. /*
  3568. * By now any valid limit table ought to have set a max frequency for
  3569. * vco1, so if it's zero it's either a pre limit table bios, or one
  3570. * with an empty limit table (seen on nv18)
  3571. */
  3572. if (!pll_lim->vco1.maxfreq) {
  3573. pll_lim->vco1.minfreq = bios->fminvco;
  3574. pll_lim->vco1.maxfreq = bios->fmaxvco;
  3575. pll_lim->vco1.min_inputfreq = 0;
  3576. pll_lim->vco1.max_inputfreq = INT_MAX;
  3577. pll_lim->vco1.min_n = 0x1;
  3578. pll_lim->vco1.max_n = 0xff;
  3579. pll_lim->vco1.min_m = 0x1;
  3580. if (crystal_straps == 0) {
  3581. /* nv05 does this, nv11 doesn't, nv10 unknown */
  3582. if (cv < 0x11)
  3583. pll_lim->vco1.min_m = 0x7;
  3584. pll_lim->vco1.max_m = 0xd;
  3585. } else {
  3586. if (cv < 0x11)
  3587. pll_lim->vco1.min_m = 0x8;
  3588. pll_lim->vco1.max_m = 0xe;
  3589. }
  3590. if (cv < 0x17 || cv == 0x1a || cv == 0x20)
  3591. pll_lim->max_log2p = 4;
  3592. else
  3593. pll_lim->max_log2p = 5;
  3594. pll_lim->max_usable_log2p = pll_lim->max_log2p;
  3595. }
  3596. if (!pll_lim->refclk)
  3597. switch (crystal_straps) {
  3598. case 0:
  3599. pll_lim->refclk = 13500;
  3600. break;
  3601. case (1 << 6):
  3602. pll_lim->refclk = 14318;
  3603. break;
  3604. case (1 << 22):
  3605. pll_lim->refclk = 27000;
  3606. break;
  3607. case (1 << 22 | 1 << 6):
  3608. pll_lim->refclk = 25000;
  3609. break;
  3610. }
  3611. #if 0 /* for easy debugging */
  3612. ErrorF("pll.vco1.minfreq: %d\n", pll_lim->vco1.minfreq);
  3613. ErrorF("pll.vco1.maxfreq: %d\n", pll_lim->vco1.maxfreq);
  3614. ErrorF("pll.vco2.minfreq: %d\n", pll_lim->vco2.minfreq);
  3615. ErrorF("pll.vco2.maxfreq: %d\n", pll_lim->vco2.maxfreq);
  3616. ErrorF("pll.vco1.min_inputfreq: %d\n", pll_lim->vco1.min_inputfreq);
  3617. ErrorF("pll.vco1.max_inputfreq: %d\n", pll_lim->vco1.max_inputfreq);
  3618. ErrorF("pll.vco2.min_inputfreq: %d\n", pll_lim->vco2.min_inputfreq);
  3619. ErrorF("pll.vco2.max_inputfreq: %d\n", pll_lim->vco2.max_inputfreq);
  3620. ErrorF("pll.vco1.min_n: %d\n", pll_lim->vco1.min_n);
  3621. ErrorF("pll.vco1.max_n: %d\n", pll_lim->vco1.max_n);
  3622. ErrorF("pll.vco1.min_m: %d\n", pll_lim->vco1.min_m);
  3623. ErrorF("pll.vco1.max_m: %d\n", pll_lim->vco1.max_m);
  3624. ErrorF("pll.vco2.min_n: %d\n", pll_lim->vco2.min_n);
  3625. ErrorF("pll.vco2.max_n: %d\n", pll_lim->vco2.max_n);
  3626. ErrorF("pll.vco2.min_m: %d\n", pll_lim->vco2.min_m);
  3627. ErrorF("pll.vco2.max_m: %d\n", pll_lim->vco2.max_m);
  3628. ErrorF("pll.max_log2p: %d\n", pll_lim->max_log2p);
  3629. ErrorF("pll.log2p_bias: %d\n", pll_lim->log2p_bias);
  3630. ErrorF("pll.refclk: %d\n", pll_lim->refclk);
  3631. #endif
  3632. return 0;
  3633. }
  3634. static void parse_bios_version(struct drm_device *dev, struct nvbios *bios, uint16_t offset)
  3635. {
  3636. /*
  3637. * offset + 0 (8 bits): Micro version
  3638. * offset + 1 (8 bits): Minor version
  3639. * offset + 2 (8 bits): Chip version
  3640. * offset + 3 (8 bits): Major version
  3641. */
  3642. bios->major_version = bios->data[offset + 3];
  3643. bios->chip_version = bios->data[offset + 2];
  3644. NV_TRACE(dev, "Bios version %02x.%02x.%02x.%02x\n",
  3645. bios->data[offset + 3], bios->data[offset + 2],
  3646. bios->data[offset + 1], bios->data[offset]);
  3647. }
  3648. static void parse_script_table_pointers(struct nvbios *bios, uint16_t offset)
  3649. {
  3650. /*
  3651. * Parses the init table segment for pointers used in script execution.
  3652. *
  3653. * offset + 0 (16 bits): init script tables pointer
  3654. * offset + 2 (16 bits): macro index table pointer
  3655. * offset + 4 (16 bits): macro table pointer
  3656. * offset + 6 (16 bits): condition table pointer
  3657. * offset + 8 (16 bits): io condition table pointer
  3658. * offset + 10 (16 bits): io flag condition table pointer
  3659. * offset + 12 (16 bits): init function table pointer
  3660. */
  3661. bios->init_script_tbls_ptr = ROM16(bios->data[offset]);
  3662. bios->macro_index_tbl_ptr = ROM16(bios->data[offset + 2]);
  3663. bios->macro_tbl_ptr = ROM16(bios->data[offset + 4]);
  3664. bios->condition_tbl_ptr = ROM16(bios->data[offset + 6]);
  3665. bios->io_condition_tbl_ptr = ROM16(bios->data[offset + 8]);
  3666. bios->io_flag_condition_tbl_ptr = ROM16(bios->data[offset + 10]);
  3667. bios->init_function_tbl_ptr = ROM16(bios->data[offset + 12]);
  3668. }
  3669. static int parse_bit_A_tbl_entry(struct drm_device *dev, struct nvbios *bios, struct bit_entry *bitentry)
  3670. {
  3671. /*
  3672. * Parses the load detect values for g80 cards.
  3673. *
  3674. * offset + 0 (16 bits): loadval table pointer
  3675. */
  3676. uint16_t load_table_ptr;
  3677. uint8_t version, headerlen, entrylen, num_entries;
  3678. if (bitentry->length != 3) {
  3679. NV_ERROR(dev, "Do not understand BIT A table\n");
  3680. return -EINVAL;
  3681. }
  3682. load_table_ptr = ROM16(bios->data[bitentry->offset]);
  3683. if (load_table_ptr == 0x0) {
  3684. NV_ERROR(dev, "Pointer to BIT loadval table invalid\n");
  3685. return -EINVAL;
  3686. }
  3687. version = bios->data[load_table_ptr];
  3688. if (version != 0x10) {
  3689. NV_ERROR(dev, "BIT loadval table version %d.%d not supported\n",
  3690. version >> 4, version & 0xF);
  3691. return -ENOSYS;
  3692. }
  3693. headerlen = bios->data[load_table_ptr + 1];
  3694. entrylen = bios->data[load_table_ptr + 2];
  3695. num_entries = bios->data[load_table_ptr + 3];
  3696. if (headerlen != 4 || entrylen != 4 || num_entries != 2) {
  3697. NV_ERROR(dev, "Do not understand BIT loadval table\n");
  3698. return -EINVAL;
  3699. }
  3700. /* First entry is normal dac, 2nd tv-out perhaps? */
  3701. bios->dactestval = ROM32(bios->data[load_table_ptr + headerlen]) & 0x3ff;
  3702. return 0;
  3703. }
  3704. static int parse_bit_C_tbl_entry(struct drm_device *dev, struct nvbios *bios, struct bit_entry *bitentry)
  3705. {
  3706. /*
  3707. * offset + 8 (16 bits): PLL limits table pointer
  3708. *
  3709. * There's more in here, but that's unknown.
  3710. */
  3711. if (bitentry->length < 10) {
  3712. NV_ERROR(dev, "Do not understand BIT C table\n");
  3713. return -EINVAL;
  3714. }
  3715. bios->pll_limit_tbl_ptr = ROM16(bios->data[bitentry->offset + 8]);
  3716. return 0;
  3717. }
  3718. static int parse_bit_display_tbl_entry(struct drm_device *dev, struct nvbios *bios, struct bit_entry *bitentry)
  3719. {
  3720. /*
  3721. * Parses the flat panel table segment that the bit entry points to.
  3722. * Starting at bitentry->offset:
  3723. *
  3724. * offset + 0 (16 bits): ??? table pointer - seems to have 18 byte
  3725. * records beginning with a freq.
  3726. * offset + 2 (16 bits): mode table pointer
  3727. */
  3728. if (bitentry->length != 4) {
  3729. NV_ERROR(dev, "Do not understand BIT display table\n");
  3730. return -EINVAL;
  3731. }
  3732. bios->fp.fptablepointer = ROM16(bios->data[bitentry->offset + 2]);
  3733. return 0;
  3734. }
  3735. static int parse_bit_init_tbl_entry(struct drm_device *dev, struct nvbios *bios, struct bit_entry *bitentry)
  3736. {
  3737. /*
  3738. * Parses the init table segment that the bit entry points to.
  3739. *
  3740. * See parse_script_table_pointers for layout
  3741. */
  3742. if (bitentry->length < 14) {
  3743. NV_ERROR(dev, "Do not understand init table\n");
  3744. return -EINVAL;
  3745. }
  3746. parse_script_table_pointers(bios, bitentry->offset);
  3747. if (bitentry->length >= 16)
  3748. bios->some_script_ptr = ROM16(bios->data[bitentry->offset + 14]);
  3749. if (bitentry->length >= 18)
  3750. bios->init96_tbl_ptr = ROM16(bios->data[bitentry->offset + 16]);
  3751. return 0;
  3752. }
  3753. static int parse_bit_i_tbl_entry(struct drm_device *dev, struct nvbios *bios, struct bit_entry *bitentry)
  3754. {
  3755. /*
  3756. * BIT 'i' (info?) table
  3757. *
  3758. * offset + 0 (32 bits): BIOS version dword (as in B table)
  3759. * offset + 5 (8 bits): BIOS feature byte (same as for BMP?)
  3760. * offset + 13 (16 bits): pointer to table containing DAC load
  3761. * detection comparison values
  3762. *
  3763. * There's other things in the table, purpose unknown
  3764. */
  3765. uint16_t daccmpoffset;
  3766. uint8_t dacver, dacheaderlen;
  3767. if (bitentry->length < 6) {
  3768. NV_ERROR(dev, "BIT i table too short for needed information\n");
  3769. return -EINVAL;
  3770. }
  3771. parse_bios_version(dev, bios, bitentry->offset);
  3772. /*
  3773. * bit 4 seems to indicate a mobile bios (doesn't suffer from BMP's
  3774. * Quadro identity crisis), other bits possibly as for BMP feature byte
  3775. */
  3776. bios->feature_byte = bios->data[bitentry->offset + 5];
  3777. bios->is_mobile = bios->feature_byte & FEATURE_MOBILE;
  3778. if (bitentry->length < 15) {
  3779. NV_WARN(dev, "BIT i table not long enough for DAC load "
  3780. "detection comparison table\n");
  3781. return -EINVAL;
  3782. }
  3783. daccmpoffset = ROM16(bios->data[bitentry->offset + 13]);
  3784. /* doesn't exist on g80 */
  3785. if (!daccmpoffset)
  3786. return 0;
  3787. /*
  3788. * The first value in the table, following the header, is the
  3789. * comparison value, the second entry is a comparison value for
  3790. * TV load detection.
  3791. */
  3792. dacver = bios->data[daccmpoffset];
  3793. dacheaderlen = bios->data[daccmpoffset + 1];
  3794. if (dacver != 0x00 && dacver != 0x10) {
  3795. NV_WARN(dev, "DAC load detection comparison table version "
  3796. "%d.%d not known\n", dacver >> 4, dacver & 0xf);
  3797. return -ENOSYS;
  3798. }
  3799. bios->dactestval = ROM32(bios->data[daccmpoffset + dacheaderlen]);
  3800. bios->tvdactestval = ROM32(bios->data[daccmpoffset + dacheaderlen + 4]);
  3801. return 0;
  3802. }
  3803. static int parse_bit_lvds_tbl_entry(struct drm_device *dev, struct nvbios *bios, struct bit_entry *bitentry)
  3804. {
  3805. /*
  3806. * Parses the LVDS table segment that the bit entry points to.
  3807. * Starting at bitentry->offset:
  3808. *
  3809. * offset + 0 (16 bits): LVDS strap xlate table pointer
  3810. */
  3811. if (bitentry->length != 2) {
  3812. NV_ERROR(dev, "Do not understand BIT LVDS table\n");
  3813. return -EINVAL;
  3814. }
  3815. /*
  3816. * No idea if it's still called the LVDS manufacturer table, but
  3817. * the concept's close enough.
  3818. */
  3819. bios->fp.lvdsmanufacturerpointer = ROM16(bios->data[bitentry->offset]);
  3820. return 0;
  3821. }
  3822. static int
  3823. parse_bit_M_tbl_entry(struct drm_device *dev, struct nvbios *bios,
  3824. struct bit_entry *bitentry)
  3825. {
  3826. /*
  3827. * offset + 2 (8 bits): number of options in an
  3828. * INIT_RAM_RESTRICT_ZM_REG_GROUP opcode option set
  3829. * offset + 3 (16 bits): pointer to strap xlate table for RAM
  3830. * restrict option selection
  3831. *
  3832. * There's a bunch of bits in this table other than the RAM restrict
  3833. * stuff that we don't use - their use currently unknown
  3834. */
  3835. /*
  3836. * Older bios versions don't have a sufficiently long table for
  3837. * what we want
  3838. */
  3839. if (bitentry->length < 0x5)
  3840. return 0;
  3841. if (bitentry->id[1] < 2) {
  3842. bios->ram_restrict_group_count = bios->data[bitentry->offset + 2];
  3843. bios->ram_restrict_tbl_ptr = ROM16(bios->data[bitentry->offset + 3]);
  3844. } else {
  3845. bios->ram_restrict_group_count = bios->data[bitentry->offset + 0];
  3846. bios->ram_restrict_tbl_ptr = ROM16(bios->data[bitentry->offset + 1]);
  3847. }
  3848. return 0;
  3849. }
  3850. static int parse_bit_tmds_tbl_entry(struct drm_device *dev, struct nvbios *bios, struct bit_entry *bitentry)
  3851. {
  3852. /*
  3853. * Parses the pointer to the TMDS table
  3854. *
  3855. * Starting at bitentry->offset:
  3856. *
  3857. * offset + 0 (16 bits): TMDS table pointer
  3858. *
  3859. * The TMDS table is typically found just before the DCB table, with a
  3860. * characteristic signature of 0x11,0x13 (1.1 being version, 0x13 being
  3861. * length?)
  3862. *
  3863. * At offset +7 is a pointer to a script, which I don't know how to
  3864. * run yet.
  3865. * At offset +9 is a pointer to another script, likewise
  3866. * Offset +11 has a pointer to a table where the first word is a pxclk
  3867. * frequency and the second word a pointer to a script, which should be
  3868. * run if the comparison pxclk frequency is less than the pxclk desired.
  3869. * This repeats for decreasing comparison frequencies
  3870. * Offset +13 has a pointer to a similar table
  3871. * The selection of table (and possibly +7/+9 script) is dictated by
  3872. * "or" from the DCB.
  3873. */
  3874. uint16_t tmdstableptr, script1, script2;
  3875. if (bitentry->length != 2) {
  3876. NV_ERROR(dev, "Do not understand BIT TMDS table\n");
  3877. return -EINVAL;
  3878. }
  3879. tmdstableptr = ROM16(bios->data[bitentry->offset]);
  3880. if (tmdstableptr == 0x0) {
  3881. NV_ERROR(dev, "Pointer to TMDS table invalid\n");
  3882. return -EINVAL;
  3883. }
  3884. /* nv50+ has v2.0, but we don't parse it atm */
  3885. if (bios->data[tmdstableptr] != 0x11) {
  3886. NV_WARN(dev,
  3887. "TMDS table revision %d.%d not currently supported\n",
  3888. bios->data[tmdstableptr] >> 4, bios->data[tmdstableptr] & 0xf);
  3889. return -ENOSYS;
  3890. }
  3891. /*
  3892. * These two scripts are odd: they don't seem to get run even when
  3893. * they are not stubbed.
  3894. */
  3895. script1 = ROM16(bios->data[tmdstableptr + 7]);
  3896. script2 = ROM16(bios->data[tmdstableptr + 9]);
  3897. if (bios->data[script1] != 'q' || bios->data[script2] != 'q')
  3898. NV_WARN(dev, "TMDS table script pointers not stubbed\n");
  3899. bios->tmds.output0_script_ptr = ROM16(bios->data[tmdstableptr + 11]);
  3900. bios->tmds.output1_script_ptr = ROM16(bios->data[tmdstableptr + 13]);
  3901. return 0;
  3902. }
  3903. static int
  3904. parse_bit_U_tbl_entry(struct drm_device *dev, struct nvbios *bios,
  3905. struct bit_entry *bitentry)
  3906. {
  3907. /*
  3908. * Parses the pointer to the G80 output script tables
  3909. *
  3910. * Starting at bitentry->offset:
  3911. *
  3912. * offset + 0 (16 bits): output script table pointer
  3913. */
  3914. uint16_t outputscripttableptr;
  3915. if (bitentry->length != 3) {
  3916. NV_ERROR(dev, "Do not understand BIT U table\n");
  3917. return -EINVAL;
  3918. }
  3919. outputscripttableptr = ROM16(bios->data[bitentry->offset]);
  3920. bios->display.script_table_ptr = outputscripttableptr;
  3921. return 0;
  3922. }
  3923. static int
  3924. parse_bit_displayport_tbl_entry(struct drm_device *dev, struct nvbios *bios,
  3925. struct bit_entry *bitentry)
  3926. {
  3927. bios->display.dp_table_ptr = ROM16(bios->data[bitentry->offset]);
  3928. return 0;
  3929. }
  3930. struct bit_table {
  3931. const char id;
  3932. int (* const parse_fn)(struct drm_device *, struct nvbios *, struct bit_entry *);
  3933. };
  3934. #define BIT_TABLE(id, funcid) ((struct bit_table){ id, parse_bit_##funcid##_tbl_entry })
  3935. static int
  3936. parse_bit_table(struct nvbios *bios, const uint16_t bitoffset,
  3937. struct bit_table *table)
  3938. {
  3939. struct drm_device *dev = bios->dev;
  3940. uint8_t maxentries = bios->data[bitoffset + 4];
  3941. int i, offset;
  3942. struct bit_entry bitentry;
  3943. for (i = 0, offset = bitoffset + 6; i < maxentries; i++, offset += 6) {
  3944. bitentry.id[0] = bios->data[offset];
  3945. if (bitentry.id[0] != table->id)
  3946. continue;
  3947. bitentry.id[1] = bios->data[offset + 1];
  3948. bitentry.length = ROM16(bios->data[offset + 2]);
  3949. bitentry.offset = ROM16(bios->data[offset + 4]);
  3950. return table->parse_fn(dev, bios, &bitentry);
  3951. }
  3952. NV_INFO(dev, "BIT table '%c' not found\n", table->id);
  3953. return -ENOSYS;
  3954. }
  3955. static int
  3956. parse_bit_structure(struct nvbios *bios, const uint16_t bitoffset)
  3957. {
  3958. int ret;
  3959. /*
  3960. * The only restriction on parsing order currently is having 'i' first
  3961. * for use of bios->*_version or bios->feature_byte while parsing;
  3962. * functions shouldn't be actually *doing* anything apart from pulling
  3963. * data from the image into the bios struct, thus no interdependencies
  3964. */
  3965. ret = parse_bit_table(bios, bitoffset, &BIT_TABLE('i', i));
  3966. if (ret) /* info? */
  3967. return ret;
  3968. if (bios->major_version >= 0x60) /* g80+ */
  3969. parse_bit_table(bios, bitoffset, &BIT_TABLE('A', A));
  3970. ret = parse_bit_table(bios, bitoffset, &BIT_TABLE('C', C));
  3971. if (ret)
  3972. return ret;
  3973. parse_bit_table(bios, bitoffset, &BIT_TABLE('D', display));
  3974. ret = parse_bit_table(bios, bitoffset, &BIT_TABLE('I', init));
  3975. if (ret)
  3976. return ret;
  3977. parse_bit_table(bios, bitoffset, &BIT_TABLE('M', M)); /* memory? */
  3978. parse_bit_table(bios, bitoffset, &BIT_TABLE('L', lvds));
  3979. parse_bit_table(bios, bitoffset, &BIT_TABLE('T', tmds));
  3980. parse_bit_table(bios, bitoffset, &BIT_TABLE('U', U));
  3981. parse_bit_table(bios, bitoffset, &BIT_TABLE('d', displayport));
  3982. return 0;
  3983. }
  3984. static int parse_bmp_structure(struct drm_device *dev, struct nvbios *bios, unsigned int offset)
  3985. {
  3986. /*
  3987. * Parses the BMP structure for useful things, but does not act on them
  3988. *
  3989. * offset + 5: BMP major version
  3990. * offset + 6: BMP minor version
  3991. * offset + 9: BMP feature byte
  3992. * offset + 10: BCD encoded BIOS version
  3993. *
  3994. * offset + 18: init script table pointer (for bios versions < 5.10h)
  3995. * offset + 20: extra init script table pointer (for bios
  3996. * versions < 5.10h)
  3997. *
  3998. * offset + 24: memory init table pointer (used on early bios versions)
  3999. * offset + 26: SDR memory sequencing setup data table
  4000. * offset + 28: DDR memory sequencing setup data table
  4001. *
  4002. * offset + 54: index of I2C CRTC pair to use for CRT output
  4003. * offset + 55: index of I2C CRTC pair to use for TV output
  4004. * offset + 56: index of I2C CRTC pair to use for flat panel output
  4005. * offset + 58: write CRTC index for I2C pair 0
  4006. * offset + 59: read CRTC index for I2C pair 0
  4007. * offset + 60: write CRTC index for I2C pair 1
  4008. * offset + 61: read CRTC index for I2C pair 1
  4009. *
  4010. * offset + 67: maximum internal PLL frequency (single stage PLL)
  4011. * offset + 71: minimum internal PLL frequency (single stage PLL)
  4012. *
  4013. * offset + 75: script table pointers, as described in
  4014. * parse_script_table_pointers
  4015. *
  4016. * offset + 89: TMDS single link output A table pointer
  4017. * offset + 91: TMDS single link output B table pointer
  4018. * offset + 95: LVDS single link output A table pointer
  4019. * offset + 105: flat panel timings table pointer
  4020. * offset + 107: flat panel strapping translation table pointer
  4021. * offset + 117: LVDS manufacturer panel config table pointer
  4022. * offset + 119: LVDS manufacturer strapping translation table pointer
  4023. *
  4024. * offset + 142: PLL limits table pointer
  4025. *
  4026. * offset + 156: minimum pixel clock for LVDS dual link
  4027. */
  4028. uint8_t *bmp = &bios->data[offset], bmp_version_major, bmp_version_minor;
  4029. uint16_t bmplength;
  4030. uint16_t legacy_scripts_offset, legacy_i2c_offset;
  4031. /* load needed defaults in case we can't parse this info */
  4032. bios->dcb.i2c[0].write = NV_CIO_CRE_DDC_WR__INDEX;
  4033. bios->dcb.i2c[0].read = NV_CIO_CRE_DDC_STATUS__INDEX;
  4034. bios->dcb.i2c[1].write = NV_CIO_CRE_DDC0_WR__INDEX;
  4035. bios->dcb.i2c[1].read = NV_CIO_CRE_DDC0_STATUS__INDEX;
  4036. bios->digital_min_front_porch = 0x4b;
  4037. bios->fmaxvco = 256000;
  4038. bios->fminvco = 128000;
  4039. bios->fp.duallink_transition_clk = 90000;
  4040. bmp_version_major = bmp[5];
  4041. bmp_version_minor = bmp[6];
  4042. NV_TRACE(dev, "BMP version %d.%d\n",
  4043. bmp_version_major, bmp_version_minor);
  4044. /*
  4045. * Make sure that 0x36 is blank and can't be mistaken for a DCB
  4046. * pointer on early versions
  4047. */
  4048. if (bmp_version_major < 5)
  4049. *(uint16_t *)&bios->data[0x36] = 0;
  4050. /*
  4051. * Seems that the minor version was 1 for all major versions prior
  4052. * to 5. Version 6 could theoretically exist, but I suspect BIT
  4053. * happened instead.
  4054. */
  4055. if ((bmp_version_major < 5 && bmp_version_minor != 1) || bmp_version_major > 5) {
  4056. NV_ERROR(dev, "You have an unsupported BMP version. "
  4057. "Please send in your bios\n");
  4058. return -ENOSYS;
  4059. }
  4060. if (bmp_version_major == 0)
  4061. /* nothing that's currently useful in this version */
  4062. return 0;
  4063. else if (bmp_version_major == 1)
  4064. bmplength = 44; /* exact for 1.01 */
  4065. else if (bmp_version_major == 2)
  4066. bmplength = 48; /* exact for 2.01 */
  4067. else if (bmp_version_major == 3)
  4068. bmplength = 54;
  4069. /* guessed - mem init tables added in this version */
  4070. else if (bmp_version_major == 4 || bmp_version_minor < 0x1)
  4071. /* don't know if 5.0 exists... */
  4072. bmplength = 62;
  4073. /* guessed - BMP I2C indices added in version 4*/
  4074. else if (bmp_version_minor < 0x6)
  4075. bmplength = 67; /* exact for 5.01 */
  4076. else if (bmp_version_minor < 0x10)
  4077. bmplength = 75; /* exact for 5.06 */
  4078. else if (bmp_version_minor == 0x10)
  4079. bmplength = 89; /* exact for 5.10h */
  4080. else if (bmp_version_minor < 0x14)
  4081. bmplength = 118; /* exact for 5.11h */
  4082. else if (bmp_version_minor < 0x24)
  4083. /*
  4084. * Not sure of version where pll limits came in;
  4085. * certainly exist by 0x24 though.
  4086. */
  4087. /* length not exact: this is long enough to get lvds members */
  4088. bmplength = 123;
  4089. else if (bmp_version_minor < 0x27)
  4090. /*
  4091. * Length not exact: this is long enough to get pll limit
  4092. * member
  4093. */
  4094. bmplength = 144;
  4095. else
  4096. /*
  4097. * Length not exact: this is long enough to get dual link
  4098. * transition clock.
  4099. */
  4100. bmplength = 158;
  4101. /* checksum */
  4102. if (nv_cksum(bmp, 8)) {
  4103. NV_ERROR(dev, "Bad BMP checksum\n");
  4104. return -EINVAL;
  4105. }
  4106. /*
  4107. * Bit 4 seems to indicate either a mobile bios or a quadro card --
  4108. * mobile behaviour consistent (nv11+), quadro only seen nv18gl-nv36gl
  4109. * (not nv10gl), bit 5 that the flat panel tables are present, and
  4110. * bit 6 a tv bios.
  4111. */
  4112. bios->feature_byte = bmp[9];
  4113. parse_bios_version(dev, bios, offset + 10);
  4114. if (bmp_version_major < 5 || bmp_version_minor < 0x10)
  4115. bios->old_style_init = true;
  4116. legacy_scripts_offset = 18;
  4117. if (bmp_version_major < 2)
  4118. legacy_scripts_offset -= 4;
  4119. bios->init_script_tbls_ptr = ROM16(bmp[legacy_scripts_offset]);
  4120. bios->extra_init_script_tbl_ptr = ROM16(bmp[legacy_scripts_offset + 2]);
  4121. if (bmp_version_major > 2) { /* appears in BMP 3 */
  4122. bios->legacy.mem_init_tbl_ptr = ROM16(bmp[24]);
  4123. bios->legacy.sdr_seq_tbl_ptr = ROM16(bmp[26]);
  4124. bios->legacy.ddr_seq_tbl_ptr = ROM16(bmp[28]);
  4125. }
  4126. legacy_i2c_offset = 0x48; /* BMP version 2 & 3 */
  4127. if (bmplength > 61)
  4128. legacy_i2c_offset = offset + 54;
  4129. bios->legacy.i2c_indices.crt = bios->data[legacy_i2c_offset];
  4130. bios->legacy.i2c_indices.tv = bios->data[legacy_i2c_offset + 1];
  4131. bios->legacy.i2c_indices.panel = bios->data[legacy_i2c_offset + 2];
  4132. bios->dcb.i2c[0].write = bios->data[legacy_i2c_offset + 4];
  4133. bios->dcb.i2c[0].read = bios->data[legacy_i2c_offset + 5];
  4134. bios->dcb.i2c[1].write = bios->data[legacy_i2c_offset + 6];
  4135. bios->dcb.i2c[1].read = bios->data[legacy_i2c_offset + 7];
  4136. if (bmplength > 74) {
  4137. bios->fmaxvco = ROM32(bmp[67]);
  4138. bios->fminvco = ROM32(bmp[71]);
  4139. }
  4140. if (bmplength > 88)
  4141. parse_script_table_pointers(bios, offset + 75);
  4142. if (bmplength > 94) {
  4143. bios->tmds.output0_script_ptr = ROM16(bmp[89]);
  4144. bios->tmds.output1_script_ptr = ROM16(bmp[91]);
  4145. /*
  4146. * Never observed in use with lvds scripts, but is reused for
  4147. * 18/24 bit panel interface default for EDID equipped panels
  4148. * (if_is_24bit not set directly to avoid any oscillation).
  4149. */
  4150. bios->legacy.lvds_single_a_script_ptr = ROM16(bmp[95]);
  4151. }
  4152. if (bmplength > 108) {
  4153. bios->fp.fptablepointer = ROM16(bmp[105]);
  4154. bios->fp.fpxlatetableptr = ROM16(bmp[107]);
  4155. bios->fp.xlatwidth = 1;
  4156. }
  4157. if (bmplength > 120) {
  4158. bios->fp.lvdsmanufacturerpointer = ROM16(bmp[117]);
  4159. bios->fp.fpxlatemanufacturertableptr = ROM16(bmp[119]);
  4160. }
  4161. if (bmplength > 143)
  4162. bios->pll_limit_tbl_ptr = ROM16(bmp[142]);
  4163. if (bmplength > 157)
  4164. bios->fp.duallink_transition_clk = ROM16(bmp[156]) * 10;
  4165. return 0;
  4166. }
  4167. static uint16_t findstr(uint8_t *data, int n, const uint8_t *str, int len)
  4168. {
  4169. int i, j;
  4170. for (i = 0; i <= (n - len); i++) {
  4171. for (j = 0; j < len; j++)
  4172. if (data[i + j] != str[j])
  4173. break;
  4174. if (j == len)
  4175. return i;
  4176. }
  4177. return 0;
  4178. }
  4179. static int
  4180. read_dcb_i2c_entry(struct drm_device *dev, int dcb_version, uint8_t *i2ctable, int index, struct dcb_i2c_entry *i2c)
  4181. {
  4182. uint8_t dcb_i2c_ver = dcb_version, headerlen = 0, entry_len = 4;
  4183. int i2c_entries = DCB_MAX_NUM_I2C_ENTRIES;
  4184. int recordoffset = 0, rdofs = 1, wrofs = 0;
  4185. uint8_t port_type = 0;
  4186. if (!i2ctable)
  4187. return -EINVAL;
  4188. if (dcb_version >= 0x30) {
  4189. if (i2ctable[0] != dcb_version) /* necessary? */
  4190. NV_WARN(dev,
  4191. "DCB I2C table version mismatch (%02X vs %02X)\n",
  4192. i2ctable[0], dcb_version);
  4193. dcb_i2c_ver = i2ctable[0];
  4194. headerlen = i2ctable[1];
  4195. if (i2ctable[2] <= DCB_MAX_NUM_I2C_ENTRIES)
  4196. i2c_entries = i2ctable[2];
  4197. else
  4198. NV_WARN(dev,
  4199. "DCB I2C table has more entries than indexable "
  4200. "(%d entries, max %d)\n", i2ctable[2],
  4201. DCB_MAX_NUM_I2C_ENTRIES);
  4202. entry_len = i2ctable[3];
  4203. /* [4] is i2c_default_indices, read in parse_dcb_table() */
  4204. }
  4205. /*
  4206. * It's your own fault if you call this function on a DCB 1.1 BIOS --
  4207. * the test below is for DCB 1.2
  4208. */
  4209. if (dcb_version < 0x14) {
  4210. recordoffset = 2;
  4211. rdofs = 0;
  4212. wrofs = 1;
  4213. }
  4214. if (index == 0xf)
  4215. return 0;
  4216. if (index >= i2c_entries) {
  4217. NV_ERROR(dev, "DCB I2C index too big (%d >= %d)\n",
  4218. index, i2ctable[2]);
  4219. return -ENOENT;
  4220. }
  4221. if (i2ctable[headerlen + entry_len * index + 3] == 0xff) {
  4222. NV_ERROR(dev, "DCB I2C entry invalid\n");
  4223. return -EINVAL;
  4224. }
  4225. if (dcb_i2c_ver >= 0x30) {
  4226. port_type = i2ctable[headerlen + recordoffset + 3 + entry_len * index];
  4227. /*
  4228. * Fixup for chips using same address offset for read and
  4229. * write.
  4230. */
  4231. if (port_type == 4) /* seen on C51 */
  4232. rdofs = wrofs = 1;
  4233. if (port_type >= 5) /* G80+ */
  4234. rdofs = wrofs = 0;
  4235. }
  4236. if (dcb_i2c_ver >= 0x40 && port_type != 5 && port_type != 6)
  4237. NV_WARN(dev, "DCB I2C table has port type %d\n", port_type);
  4238. i2c->port_type = port_type;
  4239. i2c->read = i2ctable[headerlen + recordoffset + rdofs + entry_len * index];
  4240. i2c->write = i2ctable[headerlen + recordoffset + wrofs + entry_len * index];
  4241. return 0;
  4242. }
  4243. static struct dcb_gpio_entry *
  4244. new_gpio_entry(struct nvbios *bios)
  4245. {
  4246. struct dcb_gpio_table *gpio = &bios->dcb.gpio;
  4247. return &gpio->entry[gpio->entries++];
  4248. }
  4249. struct dcb_gpio_entry *
  4250. nouveau_bios_gpio_entry(struct drm_device *dev, enum dcb_gpio_tag tag)
  4251. {
  4252. struct drm_nouveau_private *dev_priv = dev->dev_private;
  4253. struct nvbios *bios = &dev_priv->vbios;
  4254. int i;
  4255. for (i = 0; i < bios->dcb.gpio.entries; i++) {
  4256. if (bios->dcb.gpio.entry[i].tag != tag)
  4257. continue;
  4258. return &bios->dcb.gpio.entry[i];
  4259. }
  4260. return NULL;
  4261. }
  4262. static void
  4263. parse_dcb30_gpio_entry(struct nvbios *bios, uint16_t offset)
  4264. {
  4265. struct dcb_gpio_entry *gpio;
  4266. uint16_t ent = ROM16(bios->data[offset]);
  4267. uint8_t line = ent & 0x1f,
  4268. tag = ent >> 5 & 0x3f,
  4269. flags = ent >> 11 & 0x1f;
  4270. if (tag == 0x3f)
  4271. return;
  4272. gpio = new_gpio_entry(bios);
  4273. gpio->tag = tag;
  4274. gpio->line = line;
  4275. gpio->invert = flags != 4;
  4276. }
  4277. static void
  4278. parse_dcb40_gpio_entry(struct nvbios *bios, uint16_t offset)
  4279. {
  4280. struct dcb_gpio_entry *gpio;
  4281. uint32_t ent = ROM32(bios->data[offset]);
  4282. uint8_t line = ent & 0x1f,
  4283. tag = ent >> 8 & 0xff;
  4284. if (tag == 0xff)
  4285. return;
  4286. gpio = new_gpio_entry(bios);
  4287. /* Currently unused, we may need more fields parsed at some
  4288. * point. */
  4289. gpio->tag = tag;
  4290. gpio->line = line;
  4291. }
  4292. static void
  4293. parse_dcb_gpio_table(struct nvbios *bios)
  4294. {
  4295. struct drm_device *dev = bios->dev;
  4296. uint16_t gpio_table_ptr = bios->dcb.gpio_table_ptr;
  4297. uint8_t *gpio_table = &bios->data[gpio_table_ptr];
  4298. int header_len = gpio_table[1],
  4299. entries = gpio_table[2],
  4300. entry_len = gpio_table[3];
  4301. void (*parse_entry)(struct nvbios *, uint16_t) = NULL;
  4302. int i;
  4303. if (bios->dcb.version >= 0x40) {
  4304. if (gpio_table_ptr && entry_len != 4) {
  4305. NV_WARN(dev, "Invalid DCB GPIO table entry length.\n");
  4306. return;
  4307. }
  4308. parse_entry = parse_dcb40_gpio_entry;
  4309. } else if (bios->dcb.version >= 0x30) {
  4310. if (gpio_table_ptr && entry_len != 2) {
  4311. NV_WARN(dev, "Invalid DCB GPIO table entry length.\n");
  4312. return;
  4313. }
  4314. parse_entry = parse_dcb30_gpio_entry;
  4315. } else if (bios->dcb.version >= 0x22) {
  4316. /*
  4317. * DCBs older than v3.0 don't really have a GPIO
  4318. * table, instead they keep some GPIO info at fixed
  4319. * locations.
  4320. */
  4321. uint16_t dcbptr = ROM16(bios->data[0x36]);
  4322. uint8_t *tvdac_gpio = &bios->data[dcbptr - 5];
  4323. if (tvdac_gpio[0] & 1) {
  4324. struct dcb_gpio_entry *gpio = new_gpio_entry(bios);
  4325. gpio->tag = DCB_GPIO_TVDAC0;
  4326. gpio->line = tvdac_gpio[1] >> 4;
  4327. gpio->invert = tvdac_gpio[0] & 2;
  4328. }
  4329. }
  4330. if (!gpio_table_ptr)
  4331. return;
  4332. if (entries > DCB_MAX_NUM_GPIO_ENTRIES) {
  4333. NV_WARN(dev, "Too many entries in the DCB GPIO table.\n");
  4334. entries = DCB_MAX_NUM_GPIO_ENTRIES;
  4335. }
  4336. for (i = 0; i < entries; i++)
  4337. parse_entry(bios, gpio_table_ptr + header_len + entry_len * i);
  4338. }
  4339. struct dcb_connector_table_entry *
  4340. nouveau_bios_connector_entry(struct drm_device *dev, int index)
  4341. {
  4342. struct drm_nouveau_private *dev_priv = dev->dev_private;
  4343. struct nvbios *bios = &dev_priv->vbios;
  4344. struct dcb_connector_table_entry *cte;
  4345. if (index >= bios->dcb.connector.entries)
  4346. return NULL;
  4347. cte = &bios->dcb.connector.entry[index];
  4348. if (cte->type == 0xff)
  4349. return NULL;
  4350. return cte;
  4351. }
  4352. static enum dcb_connector_type
  4353. divine_connector_type(struct nvbios *bios, int index)
  4354. {
  4355. struct dcb_table *dcb = &bios->dcb;
  4356. unsigned encoders = 0, type = DCB_CONNECTOR_NONE;
  4357. int i;
  4358. for (i = 0; i < dcb->entries; i++) {
  4359. if (dcb->entry[i].connector == index)
  4360. encoders |= (1 << dcb->entry[i].type);
  4361. }
  4362. if (encoders & (1 << OUTPUT_DP)) {
  4363. if (encoders & (1 << OUTPUT_TMDS))
  4364. type = DCB_CONNECTOR_DP;
  4365. else
  4366. type = DCB_CONNECTOR_eDP;
  4367. } else
  4368. if (encoders & (1 << OUTPUT_TMDS)) {
  4369. if (encoders & (1 << OUTPUT_ANALOG))
  4370. type = DCB_CONNECTOR_DVI_I;
  4371. else
  4372. type = DCB_CONNECTOR_DVI_D;
  4373. } else
  4374. if (encoders & (1 << OUTPUT_ANALOG)) {
  4375. type = DCB_CONNECTOR_VGA;
  4376. } else
  4377. if (encoders & (1 << OUTPUT_LVDS)) {
  4378. type = DCB_CONNECTOR_LVDS;
  4379. } else
  4380. if (encoders & (1 << OUTPUT_TV)) {
  4381. type = DCB_CONNECTOR_TV_0;
  4382. }
  4383. return type;
  4384. }
  4385. static void
  4386. parse_dcb_connector_table(struct nvbios *bios)
  4387. {
  4388. struct drm_device *dev = bios->dev;
  4389. struct dcb_connector_table *ct = &bios->dcb.connector;
  4390. struct dcb_connector_table_entry *cte;
  4391. uint8_t *conntab = &bios->data[bios->dcb.connector_table_ptr];
  4392. uint8_t *entry;
  4393. int i;
  4394. if (!bios->dcb.connector_table_ptr) {
  4395. NV_DEBUG_KMS(dev, "No DCB connector table present\n");
  4396. return;
  4397. }
  4398. NV_INFO(dev, "DCB connector table: VHER 0x%02x %d %d %d\n",
  4399. conntab[0], conntab[1], conntab[2], conntab[3]);
  4400. if ((conntab[0] != 0x30 && conntab[0] != 0x40) ||
  4401. (conntab[3] != 2 && conntab[3] != 4)) {
  4402. NV_ERROR(dev, " Unknown! Please report.\n");
  4403. return;
  4404. }
  4405. ct->entries = conntab[2];
  4406. entry = conntab + conntab[1];
  4407. cte = &ct->entry[0];
  4408. for (i = 0; i < conntab[2]; i++, entry += conntab[3], cte++) {
  4409. if (conntab[3] == 2)
  4410. cte->entry = ROM16(entry[0]);
  4411. else
  4412. cte->entry = ROM32(entry[0]);
  4413. cte->type = (cte->entry & 0x000000ff) >> 0;
  4414. cte->index = (cte->entry & 0x00000f00) >> 8;
  4415. switch (cte->entry & 0x00033000) {
  4416. case 0x00001000:
  4417. cte->gpio_tag = 0x07;
  4418. break;
  4419. case 0x00002000:
  4420. cte->gpio_tag = 0x08;
  4421. break;
  4422. case 0x00010000:
  4423. cte->gpio_tag = 0x51;
  4424. break;
  4425. case 0x00020000:
  4426. cte->gpio_tag = 0x52;
  4427. break;
  4428. default:
  4429. cte->gpio_tag = 0xff;
  4430. break;
  4431. }
  4432. if (cte->type == 0xff)
  4433. continue;
  4434. NV_INFO(dev, " %d: 0x%08x: type 0x%02x idx %d tag 0x%02x\n",
  4435. i, cte->entry, cte->type, cte->index, cte->gpio_tag);
  4436. /* check for known types, fallback to guessing the type
  4437. * from attached encoders if we hit an unknown.
  4438. */
  4439. switch (cte->type) {
  4440. case DCB_CONNECTOR_VGA:
  4441. case DCB_CONNECTOR_TV_0:
  4442. case DCB_CONNECTOR_TV_1:
  4443. case DCB_CONNECTOR_TV_3:
  4444. case DCB_CONNECTOR_DVI_I:
  4445. case DCB_CONNECTOR_DVI_D:
  4446. case DCB_CONNECTOR_LVDS:
  4447. case DCB_CONNECTOR_DP:
  4448. case DCB_CONNECTOR_eDP:
  4449. case DCB_CONNECTOR_HDMI_0:
  4450. case DCB_CONNECTOR_HDMI_1:
  4451. break;
  4452. default:
  4453. cte->type = divine_connector_type(bios, cte->index);
  4454. NV_WARN(dev, "unknown type, using 0x%02x", cte->type);
  4455. break;
  4456. }
  4457. }
  4458. }
  4459. static struct dcb_entry *new_dcb_entry(struct dcb_table *dcb)
  4460. {
  4461. struct dcb_entry *entry = &dcb->entry[dcb->entries];
  4462. memset(entry, 0, sizeof(struct dcb_entry));
  4463. entry->index = dcb->entries++;
  4464. return entry;
  4465. }
  4466. static void fabricate_vga_output(struct dcb_table *dcb, int i2c, int heads)
  4467. {
  4468. struct dcb_entry *entry = new_dcb_entry(dcb);
  4469. entry->type = 0;
  4470. entry->i2c_index = i2c;
  4471. entry->heads = heads;
  4472. entry->location = DCB_LOC_ON_CHIP;
  4473. /* "or" mostly unused in early gen crt modesetting, 0 is fine */
  4474. }
  4475. static void fabricate_dvi_i_output(struct dcb_table *dcb, bool twoHeads)
  4476. {
  4477. struct dcb_entry *entry = new_dcb_entry(dcb);
  4478. entry->type = 2;
  4479. entry->i2c_index = LEGACY_I2C_PANEL;
  4480. entry->heads = twoHeads ? 3 : 1;
  4481. entry->location = !DCB_LOC_ON_CHIP; /* ie OFF CHIP */
  4482. entry->or = 1; /* means |0x10 gets set on CRE_LCD__INDEX */
  4483. entry->duallink_possible = false; /* SiI164 and co. are single link */
  4484. #if 0
  4485. /*
  4486. * For dvi-a either crtc probably works, but my card appears to only
  4487. * support dvi-d. "nvidia" still attempts to program it for dvi-a,
  4488. * doing the full fp output setup (program 0x6808.. fp dimension regs,
  4489. * setting 0x680848 to 0x10000111 to enable, maybe setting 0x680880);
  4490. * the monitor picks up the mode res ok and lights up, but no pixel
  4491. * data appears, so the board manufacturer probably connected up the
  4492. * sync lines, but missed the video traces / components
  4493. *
  4494. * with this introduction, dvi-a left as an exercise for the reader.
  4495. */
  4496. fabricate_vga_output(dcb, LEGACY_I2C_PANEL, entry->heads);
  4497. #endif
  4498. }
  4499. static void fabricate_tv_output(struct dcb_table *dcb, bool twoHeads)
  4500. {
  4501. struct dcb_entry *entry = new_dcb_entry(dcb);
  4502. entry->type = 1;
  4503. entry->i2c_index = LEGACY_I2C_TV;
  4504. entry->heads = twoHeads ? 3 : 1;
  4505. entry->location = !DCB_LOC_ON_CHIP; /* ie OFF CHIP */
  4506. }
  4507. static bool
  4508. parse_dcb20_entry(struct drm_device *dev, struct dcb_table *dcb,
  4509. uint32_t conn, uint32_t conf, struct dcb_entry *entry)
  4510. {
  4511. entry->type = conn & 0xf;
  4512. entry->i2c_index = (conn >> 4) & 0xf;
  4513. entry->heads = (conn >> 8) & 0xf;
  4514. if (dcb->version >= 0x40)
  4515. entry->connector = (conn >> 12) & 0xf;
  4516. entry->bus = (conn >> 16) & 0xf;
  4517. entry->location = (conn >> 20) & 0x3;
  4518. entry->or = (conn >> 24) & 0xf;
  4519. /*
  4520. * Normal entries consist of a single bit, but dual link has the
  4521. * next most significant bit set too
  4522. */
  4523. entry->duallink_possible =
  4524. ((1 << (ffs(entry->or) - 1)) * 3 == entry->or);
  4525. switch (entry->type) {
  4526. case OUTPUT_ANALOG:
  4527. /*
  4528. * Although the rest of a CRT conf dword is usually
  4529. * zeros, mac biosen have stuff there so we must mask
  4530. */
  4531. entry->crtconf.maxfreq = (dcb->version < 0x30) ?
  4532. (conf & 0xffff) * 10 :
  4533. (conf & 0xff) * 10000;
  4534. break;
  4535. case OUTPUT_LVDS:
  4536. {
  4537. uint32_t mask;
  4538. if (conf & 0x1)
  4539. entry->lvdsconf.use_straps_for_mode = true;
  4540. if (dcb->version < 0x22) {
  4541. mask = ~0xd;
  4542. /*
  4543. * The laptop in bug 14567 lies and claims to not use
  4544. * straps when it does, so assume all DCB 2.0 laptops
  4545. * use straps, until a broken EDID using one is produced
  4546. */
  4547. entry->lvdsconf.use_straps_for_mode = true;
  4548. /*
  4549. * Both 0x4 and 0x8 show up in v2.0 tables; assume they
  4550. * mean the same thing (probably wrong, but might work)
  4551. */
  4552. if (conf & 0x4 || conf & 0x8)
  4553. entry->lvdsconf.use_power_scripts = true;
  4554. } else {
  4555. mask = ~0x5;
  4556. if (conf & 0x4)
  4557. entry->lvdsconf.use_power_scripts = true;
  4558. }
  4559. if (conf & mask) {
  4560. /*
  4561. * Until we even try to use these on G8x, it's
  4562. * useless reporting unknown bits. They all are.
  4563. */
  4564. if (dcb->version >= 0x40)
  4565. break;
  4566. NV_ERROR(dev, "Unknown LVDS configuration bits, "
  4567. "please report\n");
  4568. }
  4569. break;
  4570. }
  4571. case OUTPUT_TV:
  4572. {
  4573. if (dcb->version >= 0x30)
  4574. entry->tvconf.has_component_output = conf & (0x8 << 4);
  4575. else
  4576. entry->tvconf.has_component_output = false;
  4577. break;
  4578. }
  4579. case OUTPUT_DP:
  4580. entry->dpconf.sor.link = (conf & 0x00000030) >> 4;
  4581. entry->dpconf.link_bw = (conf & 0x00e00000) >> 21;
  4582. switch ((conf & 0x0f000000) >> 24) {
  4583. case 0xf:
  4584. entry->dpconf.link_nr = 4;
  4585. break;
  4586. case 0x3:
  4587. entry->dpconf.link_nr = 2;
  4588. break;
  4589. default:
  4590. entry->dpconf.link_nr = 1;
  4591. break;
  4592. }
  4593. break;
  4594. case OUTPUT_TMDS:
  4595. entry->tmdsconf.sor.link = (conf & 0x00000030) >> 4;
  4596. break;
  4597. case 0xe:
  4598. /* weird g80 mobile type that "nv" treats as a terminator */
  4599. dcb->entries--;
  4600. return false;
  4601. default:
  4602. break;
  4603. }
  4604. /* unsure what DCB version introduces this, 3.0? */
  4605. if (conf & 0x100000)
  4606. entry->i2c_upper_default = true;
  4607. return true;
  4608. }
  4609. static bool
  4610. parse_dcb15_entry(struct drm_device *dev, struct dcb_table *dcb,
  4611. uint32_t conn, uint32_t conf, struct dcb_entry *entry)
  4612. {
  4613. switch (conn & 0x0000000f) {
  4614. case 0:
  4615. entry->type = OUTPUT_ANALOG;
  4616. break;
  4617. case 1:
  4618. entry->type = OUTPUT_TV;
  4619. break;
  4620. case 2:
  4621. case 3:
  4622. entry->type = OUTPUT_LVDS;
  4623. break;
  4624. case 4:
  4625. switch ((conn & 0x000000f0) >> 4) {
  4626. case 0:
  4627. entry->type = OUTPUT_TMDS;
  4628. break;
  4629. case 1:
  4630. entry->type = OUTPUT_LVDS;
  4631. break;
  4632. default:
  4633. NV_ERROR(dev, "Unknown DCB subtype 4/%d\n",
  4634. (conn & 0x000000f0) >> 4);
  4635. return false;
  4636. }
  4637. break;
  4638. default:
  4639. NV_ERROR(dev, "Unknown DCB type %d\n", conn & 0x0000000f);
  4640. return false;
  4641. }
  4642. entry->i2c_index = (conn & 0x0003c000) >> 14;
  4643. entry->heads = ((conn & 0x001c0000) >> 18) + 1;
  4644. entry->or = entry->heads; /* same as heads, hopefully safe enough */
  4645. entry->location = (conn & 0x01e00000) >> 21;
  4646. entry->bus = (conn & 0x0e000000) >> 25;
  4647. entry->duallink_possible = false;
  4648. switch (entry->type) {
  4649. case OUTPUT_ANALOG:
  4650. entry->crtconf.maxfreq = (conf & 0xffff) * 10;
  4651. break;
  4652. case OUTPUT_TV:
  4653. entry->tvconf.has_component_output = false;
  4654. break;
  4655. case OUTPUT_TMDS:
  4656. /*
  4657. * Invent a DVI-A output, by copying the fields of the DVI-D
  4658. * output; reported to work by math_b on an NV20(!).
  4659. */
  4660. fabricate_vga_output(dcb, entry->i2c_index, entry->heads);
  4661. break;
  4662. case OUTPUT_LVDS:
  4663. if ((conn & 0x00003f00) != 0x10)
  4664. entry->lvdsconf.use_straps_for_mode = true;
  4665. entry->lvdsconf.use_power_scripts = true;
  4666. break;
  4667. default:
  4668. break;
  4669. }
  4670. return true;
  4671. }
  4672. static bool parse_dcb_entry(struct drm_device *dev, struct dcb_table *dcb,
  4673. uint32_t conn, uint32_t conf)
  4674. {
  4675. struct dcb_entry *entry = new_dcb_entry(dcb);
  4676. bool ret;
  4677. if (dcb->version >= 0x20)
  4678. ret = parse_dcb20_entry(dev, dcb, conn, conf, entry);
  4679. else
  4680. ret = parse_dcb15_entry(dev, dcb, conn, conf, entry);
  4681. if (!ret)
  4682. return ret;
  4683. read_dcb_i2c_entry(dev, dcb->version, dcb->i2c_table,
  4684. entry->i2c_index, &dcb->i2c[entry->i2c_index]);
  4685. return true;
  4686. }
  4687. static
  4688. void merge_like_dcb_entries(struct drm_device *dev, struct dcb_table *dcb)
  4689. {
  4690. /*
  4691. * DCB v2.0 lists each output combination separately.
  4692. * Here we merge compatible entries to have fewer outputs, with
  4693. * more options
  4694. */
  4695. int i, newentries = 0;
  4696. for (i = 0; i < dcb->entries; i++) {
  4697. struct dcb_entry *ient = &dcb->entry[i];
  4698. int j;
  4699. for (j = i + 1; j < dcb->entries; j++) {
  4700. struct dcb_entry *jent = &dcb->entry[j];
  4701. if (jent->type == 100) /* already merged entry */
  4702. continue;
  4703. /* merge heads field when all other fields the same */
  4704. if (jent->i2c_index == ient->i2c_index &&
  4705. jent->type == ient->type &&
  4706. jent->location == ient->location &&
  4707. jent->or == ient->or) {
  4708. NV_TRACE(dev, "Merging DCB entries %d and %d\n",
  4709. i, j);
  4710. ient->heads |= jent->heads;
  4711. jent->type = 100; /* dummy value */
  4712. }
  4713. }
  4714. }
  4715. /* Compact entries merged into others out of dcb */
  4716. for (i = 0; i < dcb->entries; i++) {
  4717. if (dcb->entry[i].type == 100)
  4718. continue;
  4719. if (newentries != i) {
  4720. dcb->entry[newentries] = dcb->entry[i];
  4721. dcb->entry[newentries].index = newentries;
  4722. }
  4723. newentries++;
  4724. }
  4725. dcb->entries = newentries;
  4726. }
  4727. static int
  4728. parse_dcb_table(struct drm_device *dev, struct nvbios *bios, bool twoHeads)
  4729. {
  4730. struct drm_nouveau_private *dev_priv = dev->dev_private;
  4731. struct dcb_table *dcb = &bios->dcb;
  4732. uint16_t dcbptr = 0, i2ctabptr = 0;
  4733. uint8_t *dcbtable;
  4734. uint8_t headerlen = 0x4, entries = DCB_MAX_NUM_ENTRIES;
  4735. bool configblock = true;
  4736. int recordlength = 8, confofs = 4;
  4737. int i;
  4738. /* get the offset from 0x36 */
  4739. if (dev_priv->card_type > NV_04) {
  4740. dcbptr = ROM16(bios->data[0x36]);
  4741. if (dcbptr == 0x0000)
  4742. NV_WARN(dev, "No output data (DCB) found in BIOS\n");
  4743. }
  4744. /* this situation likely means a really old card, pre DCB */
  4745. if (dcbptr == 0x0) {
  4746. NV_INFO(dev, "Assuming a CRT output exists\n");
  4747. fabricate_vga_output(dcb, LEGACY_I2C_CRT, 1);
  4748. if (nv04_tv_identify(dev, bios->legacy.i2c_indices.tv) >= 0)
  4749. fabricate_tv_output(dcb, twoHeads);
  4750. return 0;
  4751. }
  4752. dcbtable = &bios->data[dcbptr];
  4753. /* get DCB version */
  4754. dcb->version = dcbtable[0];
  4755. NV_TRACE(dev, "Found Display Configuration Block version %d.%d\n",
  4756. dcb->version >> 4, dcb->version & 0xf);
  4757. if (dcb->version >= 0x20) { /* NV17+ */
  4758. uint32_t sig;
  4759. if (dcb->version >= 0x30) { /* NV40+ */
  4760. headerlen = dcbtable[1];
  4761. entries = dcbtable[2];
  4762. recordlength = dcbtable[3];
  4763. i2ctabptr = ROM16(dcbtable[4]);
  4764. sig = ROM32(dcbtable[6]);
  4765. dcb->gpio_table_ptr = ROM16(dcbtable[10]);
  4766. dcb->connector_table_ptr = ROM16(dcbtable[20]);
  4767. } else {
  4768. i2ctabptr = ROM16(dcbtable[2]);
  4769. sig = ROM32(dcbtable[4]);
  4770. headerlen = 8;
  4771. }
  4772. if (sig != 0x4edcbdcb) {
  4773. NV_ERROR(dev, "Bad Display Configuration Block "
  4774. "signature (%08X)\n", sig);
  4775. return -EINVAL;
  4776. }
  4777. } else if (dcb->version >= 0x15) { /* some NV11 and NV20 */
  4778. char sig[8] = { 0 };
  4779. strncpy(sig, (char *)&dcbtable[-7], 7);
  4780. i2ctabptr = ROM16(dcbtable[2]);
  4781. recordlength = 10;
  4782. confofs = 6;
  4783. if (strcmp(sig, "DEV_REC")) {
  4784. NV_ERROR(dev, "Bad Display Configuration Block "
  4785. "signature (%s)\n", sig);
  4786. return -EINVAL;
  4787. }
  4788. } else {
  4789. /*
  4790. * v1.4 (some NV15/16, NV11+) seems the same as v1.5, but always
  4791. * has the same single (crt) entry, even when tv-out present, so
  4792. * the conclusion is this version cannot really be used.
  4793. * v1.2 tables (some NV6/10, and NV15+) normally have the same
  4794. * 5 entries, which are not specific to the card and so no use.
  4795. * v1.2 does have an I2C table that read_dcb_i2c_table can
  4796. * handle, but cards exist (nv11 in #14821) with a bad i2c table
  4797. * pointer, so use the indices parsed in parse_bmp_structure.
  4798. * v1.1 (NV5+, maybe some NV4) is entirely unhelpful
  4799. */
  4800. NV_TRACEWARN(dev, "No useful information in BIOS output table; "
  4801. "adding all possible outputs\n");
  4802. fabricate_vga_output(dcb, LEGACY_I2C_CRT, 1);
  4803. /*
  4804. * Attempt to detect TV before DVI because the test
  4805. * for the former is more accurate and it rules the
  4806. * latter out.
  4807. */
  4808. if (nv04_tv_identify(dev,
  4809. bios->legacy.i2c_indices.tv) >= 0)
  4810. fabricate_tv_output(dcb, twoHeads);
  4811. else if (bios->tmds.output0_script_ptr ||
  4812. bios->tmds.output1_script_ptr)
  4813. fabricate_dvi_i_output(dcb, twoHeads);
  4814. return 0;
  4815. }
  4816. if (!i2ctabptr)
  4817. NV_WARN(dev, "No pointer to DCB I2C port table\n");
  4818. else {
  4819. dcb->i2c_table = &bios->data[i2ctabptr];
  4820. if (dcb->version >= 0x30)
  4821. dcb->i2c_default_indices = dcb->i2c_table[4];
  4822. }
  4823. if (entries > DCB_MAX_NUM_ENTRIES)
  4824. entries = DCB_MAX_NUM_ENTRIES;
  4825. for (i = 0; i < entries; i++) {
  4826. uint32_t connection, config = 0;
  4827. connection = ROM32(dcbtable[headerlen + recordlength * i]);
  4828. if (configblock)
  4829. config = ROM32(dcbtable[headerlen + confofs + recordlength * i]);
  4830. /* seen on an NV11 with DCB v1.5 */
  4831. if (connection == 0x00000000)
  4832. break;
  4833. /* seen on an NV17 with DCB v2.0 */
  4834. if (connection == 0xffffffff)
  4835. break;
  4836. if ((connection & 0x0000000f) == 0x0000000f)
  4837. continue;
  4838. NV_TRACEWARN(dev, "Raw DCB entry %d: %08x %08x\n",
  4839. dcb->entries, connection, config);
  4840. if (!parse_dcb_entry(dev, dcb, connection, config))
  4841. break;
  4842. }
  4843. /*
  4844. * apart for v2.1+ not being known for requiring merging, this
  4845. * guarantees dcbent->index is the index of the entry in the rom image
  4846. */
  4847. if (dcb->version < 0x21)
  4848. merge_like_dcb_entries(dev, dcb);
  4849. if (!dcb->entries)
  4850. return -ENXIO;
  4851. parse_dcb_gpio_table(bios);
  4852. parse_dcb_connector_table(bios);
  4853. return 0;
  4854. }
  4855. static void
  4856. fixup_legacy_connector(struct nvbios *bios)
  4857. {
  4858. struct dcb_table *dcb = &bios->dcb;
  4859. int i, i2c, i2c_conn[DCB_MAX_NUM_I2C_ENTRIES] = { };
  4860. /*
  4861. * DCB 3.0 also has the table in most cases, but there are some cards
  4862. * where the table is filled with stub entries, and the DCB entriy
  4863. * indices are all 0. We don't need the connector indices on pre-G80
  4864. * chips (yet?) so limit the use to DCB 4.0 and above.
  4865. */
  4866. if (dcb->version >= 0x40)
  4867. return;
  4868. dcb->connector.entries = 0;
  4869. /*
  4870. * No known connector info before v3.0, so make it up. the rule here
  4871. * is: anything on the same i2c bus is considered to be on the same
  4872. * connector. any output without an associated i2c bus is assigned
  4873. * its own unique connector index.
  4874. */
  4875. for (i = 0; i < dcb->entries; i++) {
  4876. /*
  4877. * Ignore the I2C index for on-chip TV-out, as there
  4878. * are cards with bogus values (nv31m in bug 23212),
  4879. * and it's otherwise useless.
  4880. */
  4881. if (dcb->entry[i].type == OUTPUT_TV &&
  4882. dcb->entry[i].location == DCB_LOC_ON_CHIP)
  4883. dcb->entry[i].i2c_index = 0xf;
  4884. i2c = dcb->entry[i].i2c_index;
  4885. if (i2c_conn[i2c]) {
  4886. dcb->entry[i].connector = i2c_conn[i2c] - 1;
  4887. continue;
  4888. }
  4889. dcb->entry[i].connector = dcb->connector.entries++;
  4890. if (i2c != 0xf)
  4891. i2c_conn[i2c] = dcb->connector.entries;
  4892. }
  4893. /* Fake the connector table as well as just connector indices */
  4894. for (i = 0; i < dcb->connector.entries; i++) {
  4895. dcb->connector.entry[i].index = i;
  4896. dcb->connector.entry[i].type = divine_connector_type(bios, i);
  4897. dcb->connector.entry[i].gpio_tag = 0xff;
  4898. }
  4899. }
  4900. static void
  4901. fixup_legacy_i2c(struct nvbios *bios)
  4902. {
  4903. struct dcb_table *dcb = &bios->dcb;
  4904. int i;
  4905. for (i = 0; i < dcb->entries; i++) {
  4906. if (dcb->entry[i].i2c_index == LEGACY_I2C_CRT)
  4907. dcb->entry[i].i2c_index = bios->legacy.i2c_indices.crt;
  4908. if (dcb->entry[i].i2c_index == LEGACY_I2C_PANEL)
  4909. dcb->entry[i].i2c_index = bios->legacy.i2c_indices.panel;
  4910. if (dcb->entry[i].i2c_index == LEGACY_I2C_TV)
  4911. dcb->entry[i].i2c_index = bios->legacy.i2c_indices.tv;
  4912. }
  4913. }
  4914. static int load_nv17_hwsq_ucode_entry(struct drm_device *dev, struct nvbios *bios, uint16_t hwsq_offset, int entry)
  4915. {
  4916. /*
  4917. * The header following the "HWSQ" signature has the number of entries,
  4918. * and the entry size
  4919. *
  4920. * An entry consists of a dword to write to the sequencer control reg
  4921. * (0x00001304), followed by the ucode bytes, written sequentially,
  4922. * starting at reg 0x00001400
  4923. */
  4924. uint8_t bytes_to_write;
  4925. uint16_t hwsq_entry_offset;
  4926. int i;
  4927. if (bios->data[hwsq_offset] <= entry) {
  4928. NV_ERROR(dev, "Too few entries in HW sequencer table for "
  4929. "requested entry\n");
  4930. return -ENOENT;
  4931. }
  4932. bytes_to_write = bios->data[hwsq_offset + 1];
  4933. if (bytes_to_write != 36) {
  4934. NV_ERROR(dev, "Unknown HW sequencer entry size\n");
  4935. return -EINVAL;
  4936. }
  4937. NV_TRACE(dev, "Loading NV17 power sequencing microcode\n");
  4938. hwsq_entry_offset = hwsq_offset + 2 + entry * bytes_to_write;
  4939. /* set sequencer control */
  4940. bios_wr32(bios, 0x00001304, ROM32(bios->data[hwsq_entry_offset]));
  4941. bytes_to_write -= 4;
  4942. /* write ucode */
  4943. for (i = 0; i < bytes_to_write; i += 4)
  4944. bios_wr32(bios, 0x00001400 + i, ROM32(bios->data[hwsq_entry_offset + i + 4]));
  4945. /* twiddle NV_PBUS_DEBUG_4 */
  4946. bios_wr32(bios, NV_PBUS_DEBUG_4, bios_rd32(bios, NV_PBUS_DEBUG_4) | 0x18);
  4947. return 0;
  4948. }
  4949. static int load_nv17_hw_sequencer_ucode(struct drm_device *dev,
  4950. struct nvbios *bios)
  4951. {
  4952. /*
  4953. * BMP based cards, from NV17, need a microcode loading to correctly
  4954. * control the GPIO etc for LVDS panels
  4955. *
  4956. * BIT based cards seem to do this directly in the init scripts
  4957. *
  4958. * The microcode entries are found by the "HWSQ" signature.
  4959. */
  4960. const uint8_t hwsq_signature[] = { 'H', 'W', 'S', 'Q' };
  4961. const int sz = sizeof(hwsq_signature);
  4962. int hwsq_offset;
  4963. hwsq_offset = findstr(bios->data, bios->length, hwsq_signature, sz);
  4964. if (!hwsq_offset)
  4965. return 0;
  4966. /* always use entry 0? */
  4967. return load_nv17_hwsq_ucode_entry(dev, bios, hwsq_offset + sz, 0);
  4968. }
  4969. uint8_t *nouveau_bios_embedded_edid(struct drm_device *dev)
  4970. {
  4971. struct drm_nouveau_private *dev_priv = dev->dev_private;
  4972. struct nvbios *bios = &dev_priv->vbios;
  4973. const uint8_t edid_sig[] = {
  4974. 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00 };
  4975. uint16_t offset = 0;
  4976. uint16_t newoffset;
  4977. int searchlen = NV_PROM_SIZE;
  4978. if (bios->fp.edid)
  4979. return bios->fp.edid;
  4980. while (searchlen) {
  4981. newoffset = findstr(&bios->data[offset], searchlen,
  4982. edid_sig, 8);
  4983. if (!newoffset)
  4984. return NULL;
  4985. offset += newoffset;
  4986. if (!nv_cksum(&bios->data[offset], EDID1_LEN))
  4987. break;
  4988. searchlen -= offset;
  4989. offset++;
  4990. }
  4991. NV_TRACE(dev, "Found EDID in BIOS\n");
  4992. return bios->fp.edid = &bios->data[offset];
  4993. }
  4994. void
  4995. nouveau_bios_run_init_table(struct drm_device *dev, uint16_t table,
  4996. struct dcb_entry *dcbent)
  4997. {
  4998. struct drm_nouveau_private *dev_priv = dev->dev_private;
  4999. struct nvbios *bios = &dev_priv->vbios;
  5000. struct init_exec iexec = { true, false };
  5001. mutex_lock(&bios->lock);
  5002. bios->display.output = dcbent;
  5003. parse_init_table(bios, table, &iexec);
  5004. bios->display.output = NULL;
  5005. mutex_unlock(&bios->lock);
  5006. }
  5007. static bool NVInitVBIOS(struct drm_device *dev)
  5008. {
  5009. struct drm_nouveau_private *dev_priv = dev->dev_private;
  5010. struct nvbios *bios = &dev_priv->vbios;
  5011. memset(bios, 0, sizeof(struct nvbios));
  5012. mutex_init(&bios->lock);
  5013. bios->dev = dev;
  5014. if (!NVShadowVBIOS(dev, bios->data))
  5015. return false;
  5016. bios->length = NV_PROM_SIZE;
  5017. return true;
  5018. }
  5019. static int nouveau_parse_vbios_struct(struct drm_device *dev)
  5020. {
  5021. struct drm_nouveau_private *dev_priv = dev->dev_private;
  5022. struct nvbios *bios = &dev_priv->vbios;
  5023. const uint8_t bit_signature[] = { 0xff, 0xb8, 'B', 'I', 'T' };
  5024. const uint8_t bmp_signature[] = { 0xff, 0x7f, 'N', 'V', 0x0 };
  5025. int offset;
  5026. offset = findstr(bios->data, bios->length,
  5027. bit_signature, sizeof(bit_signature));
  5028. if (offset) {
  5029. NV_TRACE(dev, "BIT BIOS found\n");
  5030. return parse_bit_structure(bios, offset + 6);
  5031. }
  5032. offset = findstr(bios->data, bios->length,
  5033. bmp_signature, sizeof(bmp_signature));
  5034. if (offset) {
  5035. NV_TRACE(dev, "BMP BIOS found\n");
  5036. return parse_bmp_structure(dev, bios, offset);
  5037. }
  5038. NV_ERROR(dev, "No known BIOS signature found\n");
  5039. return -ENODEV;
  5040. }
  5041. int
  5042. nouveau_run_vbios_init(struct drm_device *dev)
  5043. {
  5044. struct drm_nouveau_private *dev_priv = dev->dev_private;
  5045. struct nvbios *bios = &dev_priv->vbios;
  5046. int i, ret = 0;
  5047. NVLockVgaCrtcs(dev, false);
  5048. if (nv_two_heads(dev))
  5049. NVSetOwner(dev, bios->state.crtchead);
  5050. if (bios->major_version < 5) /* BMP only */
  5051. load_nv17_hw_sequencer_ucode(dev, bios);
  5052. if (bios->execute) {
  5053. bios->fp.last_script_invoc = 0;
  5054. bios->fp.lvds_init_run = false;
  5055. }
  5056. parse_init_tables(bios);
  5057. /*
  5058. * Runs some additional script seen on G8x VBIOSen. The VBIOS'
  5059. * parser will run this right after the init tables, the binary
  5060. * driver appears to run it at some point later.
  5061. */
  5062. if (bios->some_script_ptr) {
  5063. struct init_exec iexec = {true, false};
  5064. NV_INFO(dev, "Parsing VBIOS init table at offset 0x%04X\n",
  5065. bios->some_script_ptr);
  5066. parse_init_table(bios, bios->some_script_ptr, &iexec);
  5067. }
  5068. if (dev_priv->card_type >= NV_50) {
  5069. for (i = 0; i < bios->dcb.entries; i++) {
  5070. nouveau_bios_run_display_table(dev,
  5071. &bios->dcb.entry[i],
  5072. 0, 0);
  5073. }
  5074. }
  5075. NVLockVgaCrtcs(dev, true);
  5076. return ret;
  5077. }
  5078. static void
  5079. nouveau_bios_i2c_devices_takedown(struct drm_device *dev)
  5080. {
  5081. struct drm_nouveau_private *dev_priv = dev->dev_private;
  5082. struct nvbios *bios = &dev_priv->vbios;
  5083. struct dcb_i2c_entry *entry;
  5084. int i;
  5085. entry = &bios->dcb.i2c[0];
  5086. for (i = 0; i < DCB_MAX_NUM_I2C_ENTRIES; i++, entry++)
  5087. nouveau_i2c_fini(dev, entry);
  5088. }
  5089. int
  5090. nouveau_bios_init(struct drm_device *dev)
  5091. {
  5092. struct drm_nouveau_private *dev_priv = dev->dev_private;
  5093. struct nvbios *bios = &dev_priv->vbios;
  5094. uint32_t saved_nv_pextdev_boot_0;
  5095. bool was_locked;
  5096. int ret;
  5097. if (!NVInitVBIOS(dev))
  5098. return -ENODEV;
  5099. ret = nouveau_parse_vbios_struct(dev);
  5100. if (ret)
  5101. return ret;
  5102. ret = parse_dcb_table(dev, bios, nv_two_heads(dev));
  5103. if (ret)
  5104. return ret;
  5105. fixup_legacy_i2c(bios);
  5106. fixup_legacy_connector(bios);
  5107. if (!bios->major_version) /* we don't run version 0 bios */
  5108. return 0;
  5109. /* these will need remembering across a suspend */
  5110. saved_nv_pextdev_boot_0 = bios_rd32(bios, NV_PEXTDEV_BOOT_0);
  5111. bios->state.saved_nv_pfb_cfg0 = bios_rd32(bios, NV_PFB_CFG0);
  5112. /* init script execution disabled */
  5113. bios->execute = false;
  5114. /* ... unless card isn't POSTed already */
  5115. if (dev_priv->card_type >= NV_10 &&
  5116. NVReadVgaCrtc(dev, 0, 0x00) == 0 &&
  5117. NVReadVgaCrtc(dev, 0, 0x1a) == 0) {
  5118. NV_INFO(dev, "Adaptor not initialised\n");
  5119. if (dev_priv->card_type < NV_50) {
  5120. NV_ERROR(dev, "Unable to POST this chipset\n");
  5121. return -ENODEV;
  5122. }
  5123. NV_INFO(dev, "Running VBIOS init tables\n");
  5124. bios->execute = true;
  5125. }
  5126. bios_wr32(bios, NV_PEXTDEV_BOOT_0, saved_nv_pextdev_boot_0);
  5127. ret = nouveau_run_vbios_init(dev);
  5128. if (ret)
  5129. return ret;
  5130. /* feature_byte on BMP is poor, but init always sets CR4B */
  5131. was_locked = NVLockVgaCrtcs(dev, false);
  5132. if (bios->major_version < 5)
  5133. bios->is_mobile = NVReadVgaCrtc(dev, 0, NV_CIO_CRE_4B) & 0x40;
  5134. /* all BIT systems need p_f_m_t for digital_min_front_porch */
  5135. if (bios->is_mobile || bios->major_version >= 5)
  5136. ret = parse_fp_mode_table(dev, bios);
  5137. NVLockVgaCrtcs(dev, was_locked);
  5138. /* allow subsequent scripts to execute */
  5139. bios->execute = true;
  5140. return 0;
  5141. }
  5142. void
  5143. nouveau_bios_takedown(struct drm_device *dev)
  5144. {
  5145. nouveau_bios_i2c_devices_takedown(dev);
  5146. }