Path: blob/master/drivers/gpu/drm/nouveau/nouveau_bios.c
15112 views
/*1* Copyright 2005-2006 Erik Waling2* Copyright 2006 Stephane Marchesin3* Copyright 2007-2009 Stuart Bennett4*5* Permission is hereby granted, free of charge, to any person obtaining a6* copy of this software and associated documentation files (the "Software"),7* to deal in the Software without restriction, including without limitation8* the rights to use, copy, modify, merge, publish, distribute, sublicense,9* and/or sell copies of the Software, and to permit persons to whom the10* Software is furnished to do so, subject to the following conditions:11*12* The above copyright notice and this permission notice shall be included in13* all copies or substantial portions of the Software.14*15* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR16* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,17* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL18* THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,19* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF20* OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE21* SOFTWARE.22*/2324#include "drmP.h"25#define NV_DEBUG_NOTRACE26#include "nouveau_drv.h"27#include "nouveau_hw.h"28#include "nouveau_encoder.h"2930#include <linux/io-mapping.h>3132/* these defines are made up */33#define NV_CIO_CRE_44_HEADA 0x034#define NV_CIO_CRE_44_HEADB 0x335#define FEATURE_MOBILE 0x10 /* also FEATURE_QUADRO for BMP */36#define LEGACY_I2C_CRT 0x8037#define LEGACY_I2C_PANEL 0x8138#define LEGACY_I2C_TV 0x823940#define EDID1_LEN 1284142#define BIOSLOG(sip, fmt, arg...) NV_DEBUG(sip->dev, fmt, ##arg)43#define LOG_OLD_VALUE(x)4445struct init_exec {46bool execute;47bool repeat;48};4950static bool nv_cksum(const uint8_t *data, unsigned int length)51{52/*53* There's a few checksums in the BIOS, so here's a generic checking54* function.55*/56int i;57uint8_t sum = 0;5859for (i = 0; i < length; i++)60sum += data[i];6162if (sum)63return true;6465return false;66}6768static int69score_vbios(struct drm_device *dev, const uint8_t *data, const bool writeable)70{71if (!(data[0] == 0x55 && data[1] == 0xAA)) {72NV_TRACEWARN(dev, "... BIOS signature not found\n");73return 0;74}7576if (nv_cksum(data, data[2] * 512)) {77NV_TRACEWARN(dev, "... BIOS checksum invalid\n");78/* if a ro image is somewhat bad, it's probably all rubbish */79return writeable ? 2 : 1;80} else81NV_TRACE(dev, "... appears to be valid\n");8283return 3;84}8586static void load_vbios_prom(struct drm_device *dev, uint8_t *data)87{88struct drm_nouveau_private *dev_priv = dev->dev_private;89uint32_t pci_nv_20, save_pci_nv_20;90int pcir_ptr;91int i;9293if (dev_priv->card_type >= NV_50)94pci_nv_20 = 0x88050;95else96pci_nv_20 = NV_PBUS_PCI_NV_20;9798/* enable ROM access */99save_pci_nv_20 = nvReadMC(dev, pci_nv_20);100nvWriteMC(dev, pci_nv_20,101save_pci_nv_20 & ~NV_PBUS_PCI_NV_20_ROM_SHADOW_ENABLED);102103/* bail if no rom signature */104if (nv_rd08(dev, NV_PROM_OFFSET) != 0x55 ||105nv_rd08(dev, NV_PROM_OFFSET + 1) != 0xaa)106goto out;107108/* additional check (see note below) - read PCI record header */109pcir_ptr = nv_rd08(dev, NV_PROM_OFFSET + 0x18) |110nv_rd08(dev, NV_PROM_OFFSET + 0x19) << 8;111if (nv_rd08(dev, NV_PROM_OFFSET + pcir_ptr) != 'P' ||112nv_rd08(dev, NV_PROM_OFFSET + pcir_ptr + 1) != 'C' ||113nv_rd08(dev, NV_PROM_OFFSET + pcir_ptr + 2) != 'I' ||114nv_rd08(dev, NV_PROM_OFFSET + pcir_ptr + 3) != 'R')115goto out;116117/* on some 6600GT/6800LE prom reads are messed up. nvclock alleges a118* a good read may be obtained by waiting or re-reading (cargocult: 5x)119* each byte. we'll hope pramin has something usable instead120*/121for (i = 0; i < NV_PROM_SIZE; i++)122data[i] = nv_rd08(dev, NV_PROM_OFFSET + i);123124out:125/* disable ROM access */126nvWriteMC(dev, pci_nv_20,127save_pci_nv_20 | NV_PBUS_PCI_NV_20_ROM_SHADOW_ENABLED);128}129130static void load_vbios_pramin(struct drm_device *dev, uint8_t *data)131{132struct drm_nouveau_private *dev_priv = dev->dev_private;133uint32_t old_bar0_pramin = 0;134int i;135136if (dev_priv->card_type >= NV_50) {137uint32_t vbios_vram = (nv_rd32(dev, 0x619f04) & ~0xff) << 8;138139if (!vbios_vram)140vbios_vram = (nv_rd32(dev, 0x1700) << 16) + 0xf0000;141142old_bar0_pramin = nv_rd32(dev, 0x1700);143nv_wr32(dev, 0x1700, vbios_vram >> 16);144}145146/* bail if no rom signature */147if (nv_rd08(dev, NV_PRAMIN_OFFSET) != 0x55 ||148nv_rd08(dev, NV_PRAMIN_OFFSET + 1) != 0xaa)149goto out;150151for (i = 0; i < NV_PROM_SIZE; i++)152data[i] = nv_rd08(dev, NV_PRAMIN_OFFSET + i);153154out:155if (dev_priv->card_type >= NV_50)156nv_wr32(dev, 0x1700, old_bar0_pramin);157}158159static void load_vbios_pci(struct drm_device *dev, uint8_t *data)160{161void __iomem *rom = NULL;162size_t rom_len;163int ret;164165ret = pci_enable_rom(dev->pdev);166if (ret)167return;168169rom = pci_map_rom(dev->pdev, &rom_len);170if (!rom)171goto out;172memcpy_fromio(data, rom, rom_len);173pci_unmap_rom(dev->pdev, rom);174175out:176pci_disable_rom(dev->pdev);177}178179static void load_vbios_acpi(struct drm_device *dev, uint8_t *data)180{181int i;182int ret;183int size = 64 * 1024;184185if (!nouveau_acpi_rom_supported(dev->pdev))186return;187188for (i = 0; i < (size / ROM_BIOS_PAGE); i++) {189ret = nouveau_acpi_get_bios_chunk(data,190(i * ROM_BIOS_PAGE),191ROM_BIOS_PAGE);192if (ret <= 0)193break;194}195return;196}197198struct methods {199const char desc[8];200void (*loadbios)(struct drm_device *, uint8_t *);201const bool rw;202};203204static struct methods shadow_methods[] = {205{ "PRAMIN", load_vbios_pramin, true },206{ "PROM", load_vbios_prom, false },207{ "PCIROM", load_vbios_pci, true },208{ "ACPI", load_vbios_acpi, true },209};210#define NUM_SHADOW_METHODS ARRAY_SIZE(shadow_methods)211212static bool NVShadowVBIOS(struct drm_device *dev, uint8_t *data)213{214struct methods *methods = shadow_methods;215int testscore = 3;216int scores[NUM_SHADOW_METHODS], i;217218if (nouveau_vbios) {219for (i = 0; i < NUM_SHADOW_METHODS; i++)220if (!strcasecmp(nouveau_vbios, methods[i].desc))221break;222223if (i < NUM_SHADOW_METHODS) {224NV_INFO(dev, "Attempting to use BIOS image from %s\n",225methods[i].desc);226227methods[i].loadbios(dev, data);228if (score_vbios(dev, data, methods[i].rw))229return true;230}231232NV_ERROR(dev, "VBIOS source \'%s\' invalid\n", nouveau_vbios);233}234235for (i = 0; i < NUM_SHADOW_METHODS; i++) {236NV_TRACE(dev, "Attempting to load BIOS image from %s\n",237methods[i].desc);238data[0] = data[1] = 0; /* avoid reuse of previous image */239methods[i].loadbios(dev, data);240scores[i] = score_vbios(dev, data, methods[i].rw);241if (scores[i] == testscore)242return true;243}244245while (--testscore > 0) {246for (i = 0; i < NUM_SHADOW_METHODS; i++) {247if (scores[i] == testscore) {248NV_TRACE(dev, "Using BIOS image from %s\n",249methods[i].desc);250methods[i].loadbios(dev, data);251return true;252}253}254}255256NV_ERROR(dev, "No valid BIOS image found\n");257return false;258}259260struct init_tbl_entry {261char *name;262uint8_t id;263/* Return:264* > 0: success, length of opcode265* 0: success, but abort further parsing of table (INIT_DONE etc)266* < 0: failure, table parsing will be aborted267*/268int (*handler)(struct nvbios *, uint16_t, struct init_exec *);269};270271static int parse_init_table(struct nvbios *, uint16_t, struct init_exec *);272273#define MACRO_INDEX_SIZE 2274#define MACRO_SIZE 8275#define CONDITION_SIZE 12276#define IO_FLAG_CONDITION_SIZE 9277#define IO_CONDITION_SIZE 5278#define MEM_INIT_SIZE 66279280static void still_alive(void)281{282#if 0283sync();284mdelay(2);285#endif286}287288static uint32_t289munge_reg(struct nvbios *bios, uint32_t reg)290{291struct drm_nouveau_private *dev_priv = bios->dev->dev_private;292struct dcb_entry *dcbent = bios->display.output;293294if (dev_priv->card_type < NV_50)295return reg;296297if (reg & 0x40000000) {298BUG_ON(!dcbent);299300reg += (ffs(dcbent->or) - 1) * 0x800;301if ((reg & 0x20000000) && !(dcbent->sorconf.link & 1))302reg += 0x00000080;303}304305reg &= ~0x60000000;306return reg;307}308309static int310valid_reg(struct nvbios *bios, uint32_t reg)311{312struct drm_nouveau_private *dev_priv = bios->dev->dev_private;313struct drm_device *dev = bios->dev;314315/* C51 has misaligned regs on purpose. Marvellous */316if (reg & 0x2 ||317(reg & 0x1 && dev_priv->vbios.chip_version != 0x51))318NV_ERROR(dev, "======= misaligned reg 0x%08X =======\n", reg);319320/* warn on C51 regs that haven't been verified accessible in tracing */321if (reg & 0x1 && dev_priv->vbios.chip_version == 0x51 &&322reg != 0x130d && reg != 0x1311 && reg != 0x60081d)323NV_WARN(dev, "=== C51 misaligned reg 0x%08X not verified ===\n",324reg);325326if (reg >= (8*1024*1024)) {327NV_ERROR(dev, "=== reg 0x%08x out of mapped bounds ===\n", reg);328return 0;329}330331return 1;332}333334static bool335valid_idx_port(struct nvbios *bios, uint16_t port)336{337struct drm_nouveau_private *dev_priv = bios->dev->dev_private;338struct drm_device *dev = bios->dev;339340/*341* If adding more ports here, the read/write functions below will need342* updating so that the correct mmio range (PRMCIO, PRMDIO, PRMVIO) is343* used for the port in question344*/345if (dev_priv->card_type < NV_50) {346if (port == NV_CIO_CRX__COLOR)347return true;348if (port == NV_VIO_SRX)349return true;350} else {351if (port == NV_CIO_CRX__COLOR)352return true;353}354355NV_ERROR(dev, "========== unknown indexed io port 0x%04X ==========\n",356port);357358return false;359}360361static bool362valid_port(struct nvbios *bios, uint16_t port)363{364struct drm_device *dev = bios->dev;365366/*367* If adding more ports here, the read/write functions below will need368* updating so that the correct mmio range (PRMCIO, PRMDIO, PRMVIO) is369* used for the port in question370*/371if (port == NV_VIO_VSE2)372return true;373374NV_ERROR(dev, "========== unknown io port 0x%04X ==========\n", port);375376return false;377}378379static uint32_t380bios_rd32(struct nvbios *bios, uint32_t reg)381{382uint32_t data;383384reg = munge_reg(bios, reg);385if (!valid_reg(bios, reg))386return 0;387388/*389* C51 sometimes uses regs with bit0 set in the address. For these390* cases there should exist a translation in a BIOS table to an IO391* port address which the BIOS uses for accessing the reg392*393* These only seem to appear for the power control regs to a flat panel,394* and the GPIO regs at 0x60081*. In C51 mmio traces the normal regs395* for 0x1308 and 0x1310 are used - hence the mask below. An S3396* suspend-resume mmio trace from a C51 will be required to see if this397* is true for the power microcode in 0x14.., or whether the direct IO398* port access method is needed399*/400if (reg & 0x1)401reg &= ~0x1;402403data = nv_rd32(bios->dev, reg);404405BIOSLOG(bios, " Read: Reg: 0x%08X, Data: 0x%08X\n", reg, data);406407return data;408}409410static void411bios_wr32(struct nvbios *bios, uint32_t reg, uint32_t data)412{413struct drm_nouveau_private *dev_priv = bios->dev->dev_private;414415reg = munge_reg(bios, reg);416if (!valid_reg(bios, reg))417return;418419/* see note in bios_rd32 */420if (reg & 0x1)421reg &= 0xfffffffe;422423LOG_OLD_VALUE(bios_rd32(bios, reg));424BIOSLOG(bios, " Write: Reg: 0x%08X, Data: 0x%08X\n", reg, data);425426if (dev_priv->vbios.execute) {427still_alive();428nv_wr32(bios->dev, reg, data);429}430}431432static uint8_t433bios_idxprt_rd(struct nvbios *bios, uint16_t port, uint8_t index)434{435struct drm_nouveau_private *dev_priv = bios->dev->dev_private;436struct drm_device *dev = bios->dev;437uint8_t data;438439if (!valid_idx_port(bios, port))440return 0;441442if (dev_priv->card_type < NV_50) {443if (port == NV_VIO_SRX)444data = NVReadVgaSeq(dev, bios->state.crtchead, index);445else /* assume NV_CIO_CRX__COLOR */446data = NVReadVgaCrtc(dev, bios->state.crtchead, index);447} else {448uint32_t data32;449450data32 = bios_rd32(bios, NV50_PDISPLAY_VGACRTC(index & ~3));451data = (data32 >> ((index & 3) << 3)) & 0xff;452}453454BIOSLOG(bios, " Indexed IO read: Port: 0x%04X, Index: 0x%02X, "455"Head: 0x%02X, Data: 0x%02X\n",456port, index, bios->state.crtchead, data);457return data;458}459460static void461bios_idxprt_wr(struct nvbios *bios, uint16_t port, uint8_t index, uint8_t data)462{463struct drm_nouveau_private *dev_priv = bios->dev->dev_private;464struct drm_device *dev = bios->dev;465466if (!valid_idx_port(bios, port))467return;468469/*470* The current head is maintained in the nvbios member state.crtchead.471* We trap changes to CR44 and update the head variable and hence the472* register set written.473* As CR44 only exists on CRTC0, we update crtchead to head0 in advance474* of the write, and to head1 after the write475*/476if (port == NV_CIO_CRX__COLOR && index == NV_CIO_CRE_44 &&477data != NV_CIO_CRE_44_HEADB)478bios->state.crtchead = 0;479480LOG_OLD_VALUE(bios_idxprt_rd(bios, port, index));481BIOSLOG(bios, " Indexed IO write: Port: 0x%04X, Index: 0x%02X, "482"Head: 0x%02X, Data: 0x%02X\n",483port, index, bios->state.crtchead, data);484485if (bios->execute && dev_priv->card_type < NV_50) {486still_alive();487if (port == NV_VIO_SRX)488NVWriteVgaSeq(dev, bios->state.crtchead, index, data);489else /* assume NV_CIO_CRX__COLOR */490NVWriteVgaCrtc(dev, bios->state.crtchead, index, data);491} else492if (bios->execute) {493uint32_t data32, shift = (index & 3) << 3;494495still_alive();496497data32 = bios_rd32(bios, NV50_PDISPLAY_VGACRTC(index & ~3));498data32 &= ~(0xff << shift);499data32 |= (data << shift);500bios_wr32(bios, NV50_PDISPLAY_VGACRTC(index & ~3), data32);501}502503if (port == NV_CIO_CRX__COLOR &&504index == NV_CIO_CRE_44 && data == NV_CIO_CRE_44_HEADB)505bios->state.crtchead = 1;506}507508static uint8_t509bios_port_rd(struct nvbios *bios, uint16_t port)510{511uint8_t data, head = bios->state.crtchead;512513if (!valid_port(bios, port))514return 0;515516data = NVReadPRMVIO(bios->dev, head, NV_PRMVIO0_OFFSET + port);517518BIOSLOG(bios, " IO read: Port: 0x%04X, Head: 0x%02X, Data: 0x%02X\n",519port, head, data);520521return data;522}523524static void525bios_port_wr(struct nvbios *bios, uint16_t port, uint8_t data)526{527int head = bios->state.crtchead;528529if (!valid_port(bios, port))530return;531532LOG_OLD_VALUE(bios_port_rd(bios, port));533BIOSLOG(bios, " IO write: Port: 0x%04X, Head: 0x%02X, Data: 0x%02X\n",534port, head, data);535536if (!bios->execute)537return;538539still_alive();540NVWritePRMVIO(bios->dev, head, NV_PRMVIO0_OFFSET + port, data);541}542543static bool544io_flag_condition_met(struct nvbios *bios, uint16_t offset, uint8_t cond)545{546/*547* The IO flag condition entry has 2 bytes for the CRTC port; 1 byte548* for the CRTC index; 1 byte for the mask to apply to the value549* retrieved from the CRTC; 1 byte for the shift right to apply to the550* masked CRTC value; 2 bytes for the offset to the flag array, to551* which the shifted value is added; 1 byte for the mask applied to the552* value read from the flag array; and 1 byte for the value to compare553* against the masked byte from the flag table.554*/555556uint16_t condptr = bios->io_flag_condition_tbl_ptr + cond * IO_FLAG_CONDITION_SIZE;557uint16_t crtcport = ROM16(bios->data[condptr]);558uint8_t crtcindex = bios->data[condptr + 2];559uint8_t mask = bios->data[condptr + 3];560uint8_t shift = bios->data[condptr + 4];561uint16_t flagarray = ROM16(bios->data[condptr + 5]);562uint8_t flagarraymask = bios->data[condptr + 7];563uint8_t cmpval = bios->data[condptr + 8];564uint8_t data;565566BIOSLOG(bios, "0x%04X: Port: 0x%04X, Index: 0x%02X, Mask: 0x%02X, "567"Shift: 0x%02X, FlagArray: 0x%04X, FAMask: 0x%02X, "568"Cmpval: 0x%02X\n",569offset, crtcport, crtcindex, mask, shift, flagarray, flagarraymask, cmpval);570571data = bios_idxprt_rd(bios, crtcport, crtcindex);572573data = bios->data[flagarray + ((data & mask) >> shift)];574data &= flagarraymask;575576BIOSLOG(bios, "0x%04X: Checking if 0x%02X equals 0x%02X\n",577offset, data, cmpval);578579return (data == cmpval);580}581582static bool583bios_condition_met(struct nvbios *bios, uint16_t offset, uint8_t cond)584{585/*586* The condition table entry has 4 bytes for the address of the587* register to check, 4 bytes for a mask to apply to the register and588* 4 for a test comparison value589*/590591uint16_t condptr = bios->condition_tbl_ptr + cond * CONDITION_SIZE;592uint32_t reg = ROM32(bios->data[condptr]);593uint32_t mask = ROM32(bios->data[condptr + 4]);594uint32_t cmpval = ROM32(bios->data[condptr + 8]);595uint32_t data;596597BIOSLOG(bios, "0x%04X: Cond: 0x%02X, Reg: 0x%08X, Mask: 0x%08X\n",598offset, cond, reg, mask);599600data = bios_rd32(bios, reg) & mask;601602BIOSLOG(bios, "0x%04X: Checking if 0x%08X equals 0x%08X\n",603offset, data, cmpval);604605return (data == cmpval);606}607608static bool609io_condition_met(struct nvbios *bios, uint16_t offset, uint8_t cond)610{611/*612* The IO condition entry has 2 bytes for the IO port address; 1 byte613* for the index to write to io_port; 1 byte for the mask to apply to614* the byte read from io_port+1; and 1 byte for the value to compare615* against the masked byte.616*/617618uint16_t condptr = bios->io_condition_tbl_ptr + cond * IO_CONDITION_SIZE;619uint16_t io_port = ROM16(bios->data[condptr]);620uint8_t port_index = bios->data[condptr + 2];621uint8_t mask = bios->data[condptr + 3];622uint8_t cmpval = bios->data[condptr + 4];623624uint8_t data = bios_idxprt_rd(bios, io_port, port_index) & mask;625626BIOSLOG(bios, "0x%04X: Checking if 0x%02X equals 0x%02X\n",627offset, data, cmpval);628629return (data == cmpval);630}631632static int633nv50_pll_set(struct drm_device *dev, uint32_t reg, uint32_t clk)634{635struct drm_nouveau_private *dev_priv = dev->dev_private;636uint32_t reg0 = nv_rd32(dev, reg + 0);637uint32_t reg1 = nv_rd32(dev, reg + 4);638struct nouveau_pll_vals pll;639struct pll_lims pll_limits;640int ret;641642ret = get_pll_limits(dev, reg, &pll_limits);643if (ret)644return ret;645646clk = nouveau_calc_pll_mnp(dev, &pll_limits, clk, &pll);647if (!clk)648return -ERANGE;649650reg0 = (reg0 & 0xfff8ffff) | (pll.log2P << 16);651reg1 = (reg1 & 0xffff0000) | (pll.N1 << 8) | pll.M1;652653if (dev_priv->vbios.execute) {654still_alive();655nv_wr32(dev, reg + 4, reg1);656nv_wr32(dev, reg + 0, reg0);657}658659return 0;660}661662static int663setPLL(struct nvbios *bios, uint32_t reg, uint32_t clk)664{665struct drm_device *dev = bios->dev;666struct drm_nouveau_private *dev_priv = dev->dev_private;667/* clk in kHz */668struct pll_lims pll_lim;669struct nouveau_pll_vals pllvals;670int ret;671672if (dev_priv->card_type >= NV_50)673return nv50_pll_set(dev, reg, clk);674675/* high regs (such as in the mac g5 table) are not -= 4 */676ret = get_pll_limits(dev, reg > 0x405c ? reg : reg - 4, &pll_lim);677if (ret)678return ret;679680clk = nouveau_calc_pll_mnp(dev, &pll_lim, clk, &pllvals);681if (!clk)682return -ERANGE;683684if (bios->execute) {685still_alive();686nouveau_hw_setpll(dev, reg, &pllvals);687}688689return 0;690}691692static int dcb_entry_idx_from_crtchead(struct drm_device *dev)693{694struct drm_nouveau_private *dev_priv = dev->dev_private;695struct nvbios *bios = &dev_priv->vbios;696697/*698* For the results of this function to be correct, CR44 must have been699* set (using bios_idxprt_wr to set crtchead), CR58 set for CR57 = 0,700* and the DCB table parsed, before the script calling the function is701* run. run_digital_op_script is example of how to do such setup702*/703704uint8_t dcb_entry = NVReadVgaCrtc5758(dev, bios->state.crtchead, 0);705706if (dcb_entry > bios->dcb.entries) {707NV_ERROR(dev, "CR58 doesn't have a valid DCB entry currently "708"(%02X)\n", dcb_entry);709dcb_entry = 0x7f; /* unused / invalid marker */710}711712return dcb_entry;713}714715static int716read_dcb_i2c_entry(struct drm_device *dev, int dcb_version, uint8_t *i2ctable, int index, struct dcb_i2c_entry *i2c)717{718uint8_t dcb_i2c_ver = dcb_version, headerlen = 0, entry_len = 4;719int i2c_entries = DCB_MAX_NUM_I2C_ENTRIES;720int recordoffset = 0, rdofs = 1, wrofs = 0;721uint8_t port_type = 0;722723if (!i2ctable)724return -EINVAL;725726if (dcb_version >= 0x30) {727if (i2ctable[0] != dcb_version) /* necessary? */728NV_WARN(dev,729"DCB I2C table version mismatch (%02X vs %02X)\n",730i2ctable[0], dcb_version);731dcb_i2c_ver = i2ctable[0];732headerlen = i2ctable[1];733if (i2ctable[2] <= DCB_MAX_NUM_I2C_ENTRIES)734i2c_entries = i2ctable[2];735else736NV_WARN(dev,737"DCB I2C table has more entries than indexable "738"(%d entries, max %d)\n", i2ctable[2],739DCB_MAX_NUM_I2C_ENTRIES);740entry_len = i2ctable[3];741/* [4] is i2c_default_indices, read in parse_dcb_table() */742}743/*744* It's your own fault if you call this function on a DCB 1.1 BIOS --745* the test below is for DCB 1.2746*/747if (dcb_version < 0x14) {748recordoffset = 2;749rdofs = 0;750wrofs = 1;751}752753if (index == 0xf)754return 0;755if (index >= i2c_entries) {756NV_ERROR(dev, "DCB I2C index too big (%d >= %d)\n",757index, i2ctable[2]);758return -ENOENT;759}760if (i2ctable[headerlen + entry_len * index + 3] == 0xff) {761NV_ERROR(dev, "DCB I2C entry invalid\n");762return -EINVAL;763}764765if (dcb_i2c_ver >= 0x30) {766port_type = i2ctable[headerlen + recordoffset + 3 + entry_len * index];767768/*769* Fixup for chips using same address offset for read and770* write.771*/772if (port_type == 4) /* seen on C51 */773rdofs = wrofs = 1;774if (port_type >= 5) /* G80+ */775rdofs = wrofs = 0;776}777778if (dcb_i2c_ver >= 0x40) {779if (port_type != 5 && port_type != 6)780NV_WARN(dev, "DCB I2C table has port type %d\n", port_type);781782i2c->entry = ROM32(i2ctable[headerlen + recordoffset + entry_len * index]);783}784785i2c->port_type = port_type;786i2c->read = i2ctable[headerlen + recordoffset + rdofs + entry_len * index];787i2c->write = i2ctable[headerlen + recordoffset + wrofs + entry_len * index];788789return 0;790}791792static struct nouveau_i2c_chan *793init_i2c_device_find(struct drm_device *dev, int i2c_index)794{795struct drm_nouveau_private *dev_priv = dev->dev_private;796struct dcb_table *dcb = &dev_priv->vbios.dcb;797798if (i2c_index == 0xff) {799/* note: dcb_entry_idx_from_crtchead needs pre-script set-up */800int idx = dcb_entry_idx_from_crtchead(dev), shift = 0;801int default_indices = dcb->i2c_default_indices;802803if (idx != 0x7f && dcb->entry[idx].i2c_upper_default)804shift = 4;805806i2c_index = (default_indices >> shift) & 0xf;807}808if (i2c_index == 0x80) /* g80+ */809i2c_index = dcb->i2c_default_indices & 0xf;810else811if (i2c_index == 0x81)812i2c_index = (dcb->i2c_default_indices & 0xf0) >> 4;813814if (i2c_index >= DCB_MAX_NUM_I2C_ENTRIES) {815NV_ERROR(dev, "invalid i2c_index 0x%x\n", i2c_index);816return NULL;817}818819/* Make sure i2c table entry has been parsed, it may not820* have been if this is a bus not referenced by a DCB encoder821*/822read_dcb_i2c_entry(dev, dcb->version, dcb->i2c_table,823i2c_index, &dcb->i2c[i2c_index]);824825return nouveau_i2c_find(dev, i2c_index);826}827828static uint32_t829get_tmds_index_reg(struct drm_device *dev, uint8_t mlv)830{831/*832* For mlv < 0x80, it is an index into a table of TMDS base addresses.833* For mlv == 0x80 use the "or" value of the dcb_entry indexed by834* CR58 for CR57 = 0 to index a table of offsets to the basic835* 0x6808b0 address.836* For mlv == 0x81 use the "or" value of the dcb_entry indexed by837* CR58 for CR57 = 0 to index a table of offsets to the basic838* 0x6808b0 address, and then flip the offset by 8.839*/840841struct drm_nouveau_private *dev_priv = dev->dev_private;842struct nvbios *bios = &dev_priv->vbios;843const int pramdac_offset[13] = {8440, 0, 0x8, 0, 0x2000, 0, 0, 0, 0x2008, 0, 0, 0, 0x2000 };845const uint32_t pramdac_table[4] = {8460x6808b0, 0x6808b8, 0x6828b0, 0x6828b8 };847848if (mlv >= 0x80) {849int dcb_entry, dacoffset;850851/* note: dcb_entry_idx_from_crtchead needs pre-script set-up */852dcb_entry = dcb_entry_idx_from_crtchead(dev);853if (dcb_entry == 0x7f)854return 0;855dacoffset = pramdac_offset[bios->dcb.entry[dcb_entry].or];856if (mlv == 0x81)857dacoffset ^= 8;858return 0x6808b0 + dacoffset;859} else {860if (mlv >= ARRAY_SIZE(pramdac_table)) {861NV_ERROR(dev, "Magic Lookup Value too big (%02X)\n",862mlv);863return 0;864}865return pramdac_table[mlv];866}867}868869static int870init_io_restrict_prog(struct nvbios *bios, uint16_t offset,871struct init_exec *iexec)872{873/*874* INIT_IO_RESTRICT_PROG opcode: 0x32 ('2')875*876* offset (8 bit): opcode877* offset + 1 (16 bit): CRTC port878* offset + 3 (8 bit): CRTC index879* offset + 4 (8 bit): mask880* offset + 5 (8 bit): shift881* offset + 6 (8 bit): count882* offset + 7 (32 bit): register883* offset + 11 (32 bit): configuration 1884* ...885*886* Starting at offset + 11 there are "count" 32 bit values.887* To find out which value to use read index "CRTC index" on "CRTC888* port", AND this value with "mask" and then bit shift right "shift"889* bits. Read the appropriate value using this index and write to890* "register"891*/892893uint16_t crtcport = ROM16(bios->data[offset + 1]);894uint8_t crtcindex = bios->data[offset + 3];895uint8_t mask = bios->data[offset + 4];896uint8_t shift = bios->data[offset + 5];897uint8_t count = bios->data[offset + 6];898uint32_t reg = ROM32(bios->data[offset + 7]);899uint8_t config;900uint32_t configval;901int len = 11 + count * 4;902903if (!iexec->execute)904return len;905906BIOSLOG(bios, "0x%04X: Port: 0x%04X, Index: 0x%02X, Mask: 0x%02X, "907"Shift: 0x%02X, Count: 0x%02X, Reg: 0x%08X\n",908offset, crtcport, crtcindex, mask, shift, count, reg);909910config = (bios_idxprt_rd(bios, crtcport, crtcindex) & mask) >> shift;911if (config > count) {912NV_ERROR(bios->dev,913"0x%04X: Config 0x%02X exceeds maximal bound 0x%02X\n",914offset, config, count);915return len;916}917918configval = ROM32(bios->data[offset + 11 + config * 4]);919920BIOSLOG(bios, "0x%04X: Writing config %02X\n", offset, config);921922bios_wr32(bios, reg, configval);923924return len;925}926927static int928init_repeat(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)929{930/*931* INIT_REPEAT opcode: 0x33 ('3')932*933* offset (8 bit): opcode934* offset + 1 (8 bit): count935*936* Execute script following this opcode up to INIT_REPEAT_END937* "count" times938*/939940uint8_t count = bios->data[offset + 1];941uint8_t i;942943/* no iexec->execute check by design */944945BIOSLOG(bios, "0x%04X: Repeating following segment %d times\n",946offset, count);947948iexec->repeat = true;949950/*951* count - 1, as the script block will execute once when we leave this952* opcode -- this is compatible with bios behaviour as:953* a) the block is always executed at least once, even if count == 0954* b) the bios interpreter skips to the op following INIT_END_REPEAT,955* while we don't956*/957for (i = 0; i < count - 1; i++)958parse_init_table(bios, offset + 2, iexec);959960iexec->repeat = false;961962return 2;963}964965static int966init_io_restrict_pll(struct nvbios *bios, uint16_t offset,967struct init_exec *iexec)968{969/*970* INIT_IO_RESTRICT_PLL opcode: 0x34 ('4')971*972* offset (8 bit): opcode973* offset + 1 (16 bit): CRTC port974* offset + 3 (8 bit): CRTC index975* offset + 4 (8 bit): mask976* offset + 5 (8 bit): shift977* offset + 6 (8 bit): IO flag condition index978* offset + 7 (8 bit): count979* offset + 8 (32 bit): register980* offset + 12 (16 bit): frequency 1981* ...982*983* Starting at offset + 12 there are "count" 16 bit frequencies (10kHz).984* Set PLL register "register" to coefficients for frequency n,985* selected by reading index "CRTC index" of "CRTC port" ANDed with986* "mask" and shifted right by "shift".987*988* If "IO flag condition index" > 0, and condition met, double989* frequency before setting it.990*/991992uint16_t crtcport = ROM16(bios->data[offset + 1]);993uint8_t crtcindex = bios->data[offset + 3];994uint8_t mask = bios->data[offset + 4];995uint8_t shift = bios->data[offset + 5];996int8_t io_flag_condition_idx = bios->data[offset + 6];997uint8_t count = bios->data[offset + 7];998uint32_t reg = ROM32(bios->data[offset + 8]);999uint8_t config;1000uint16_t freq;1001int len = 12 + count * 2;10021003if (!iexec->execute)1004return len;10051006BIOSLOG(bios, "0x%04X: Port: 0x%04X, Index: 0x%02X, Mask: 0x%02X, "1007"Shift: 0x%02X, IO Flag Condition: 0x%02X, "1008"Count: 0x%02X, Reg: 0x%08X\n",1009offset, crtcport, crtcindex, mask, shift,1010io_flag_condition_idx, count, reg);10111012config = (bios_idxprt_rd(bios, crtcport, crtcindex) & mask) >> shift;1013if (config > count) {1014NV_ERROR(bios->dev,1015"0x%04X: Config 0x%02X exceeds maximal bound 0x%02X\n",1016offset, config, count);1017return len;1018}10191020freq = ROM16(bios->data[offset + 12 + config * 2]);10211022if (io_flag_condition_idx > 0) {1023if (io_flag_condition_met(bios, offset, io_flag_condition_idx)) {1024BIOSLOG(bios, "0x%04X: Condition fulfilled -- "1025"frequency doubled\n", offset);1026freq *= 2;1027} else1028BIOSLOG(bios, "0x%04X: Condition not fulfilled -- "1029"frequency unchanged\n", offset);1030}10311032BIOSLOG(bios, "0x%04X: Reg: 0x%08X, Config: 0x%02X, Freq: %d0kHz\n",1033offset, reg, config, freq);10341035setPLL(bios, reg, freq * 10);10361037return len;1038}10391040static int1041init_end_repeat(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)1042{1043/*1044* INIT_END_REPEAT opcode: 0x36 ('6')1045*1046* offset (8 bit): opcode1047*1048* Marks the end of the block for INIT_REPEAT to repeat1049*/10501051/* no iexec->execute check by design */10521053/*1054* iexec->repeat flag necessary to go past INIT_END_REPEAT opcode when1055* we're not in repeat mode1056*/1057if (iexec->repeat)1058return 0;10591060return 1;1061}10621063static int1064init_copy(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)1065{1066/*1067* INIT_COPY opcode: 0x37 ('7')1068*1069* offset (8 bit): opcode1070* offset + 1 (32 bit): register1071* offset + 5 (8 bit): shift1072* offset + 6 (8 bit): srcmask1073* offset + 7 (16 bit): CRTC port1074* offset + 9 (8 bit): CRTC index1075* offset + 10 (8 bit): mask1076*1077* Read index "CRTC index" on "CRTC port", AND with "mask", OR with1078* (REGVAL("register") >> "shift" & "srcmask") and write-back to CRTC1079* port1080*/10811082uint32_t reg = ROM32(bios->data[offset + 1]);1083uint8_t shift = bios->data[offset + 5];1084uint8_t srcmask = bios->data[offset + 6];1085uint16_t crtcport = ROM16(bios->data[offset + 7]);1086uint8_t crtcindex = bios->data[offset + 9];1087uint8_t mask = bios->data[offset + 10];1088uint32_t data;1089uint8_t crtcdata;10901091if (!iexec->execute)1092return 11;10931094BIOSLOG(bios, "0x%04X: Reg: 0x%08X, Shift: 0x%02X, SrcMask: 0x%02X, "1095"Port: 0x%04X, Index: 0x%02X, Mask: 0x%02X\n",1096offset, reg, shift, srcmask, crtcport, crtcindex, mask);10971098data = bios_rd32(bios, reg);10991100if (shift < 0x80)1101data >>= shift;1102else1103data <<= (0x100 - shift);11041105data &= srcmask;11061107crtcdata = bios_idxprt_rd(bios, crtcport, crtcindex) & mask;1108crtcdata |= (uint8_t)data;1109bios_idxprt_wr(bios, crtcport, crtcindex, crtcdata);11101111return 11;1112}11131114static int1115init_not(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)1116{1117/*1118* INIT_NOT opcode: 0x38 ('8')1119*1120* offset (8 bit): opcode1121*1122* Invert the current execute / no-execute condition (i.e. "else")1123*/1124if (iexec->execute)1125BIOSLOG(bios, "0x%04X: ------ Skipping following commands ------\n", offset);1126else1127BIOSLOG(bios, "0x%04X: ------ Executing following commands ------\n", offset);11281129iexec->execute = !iexec->execute;1130return 1;1131}11321133static int1134init_io_flag_condition(struct nvbios *bios, uint16_t offset,1135struct init_exec *iexec)1136{1137/*1138* INIT_IO_FLAG_CONDITION opcode: 0x39 ('9')1139*1140* offset (8 bit): opcode1141* offset + 1 (8 bit): condition number1142*1143* Check condition "condition number" in the IO flag condition table.1144* If condition not met skip subsequent opcodes until condition is1145* inverted (INIT_NOT), or we hit INIT_RESUME1146*/11471148uint8_t cond = bios->data[offset + 1];11491150if (!iexec->execute)1151return 2;11521153if (io_flag_condition_met(bios, offset, cond))1154BIOSLOG(bios, "0x%04X: Condition fulfilled -- continuing to execute\n", offset);1155else {1156BIOSLOG(bios, "0x%04X: Condition not fulfilled -- skipping following commands\n", offset);1157iexec->execute = false;1158}11591160return 2;1161}11621163static int1164init_dp_condition(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)1165{1166/*1167* INIT_DP_CONDITION opcode: 0x3A ('')1168*1169* offset (8 bit): opcode1170* offset + 1 (8 bit): "sub" opcode1171* offset + 2 (8 bit): unknown1172*1173*/11741175struct bit_displayport_encoder_table *dpe = NULL;1176struct dcb_entry *dcb = bios->display.output;1177struct drm_device *dev = bios->dev;1178uint8_t cond = bios->data[offset + 1];1179int dummy;11801181BIOSLOG(bios, "0x%04X: subop 0x%02X\n", offset, cond);11821183if (!iexec->execute)1184return 3;11851186dpe = nouveau_bios_dp_table(dev, dcb, &dummy);1187if (!dpe) {1188NV_ERROR(dev, "0x%04X: INIT_3A: no encoder table!!\n", offset);1189return 3;1190}11911192switch (cond) {1193case 0:1194{1195struct dcb_connector_table_entry *ent =1196&bios->dcb.connector.entry[dcb->connector];11971198if (ent->type != DCB_CONNECTOR_eDP)1199iexec->execute = false;1200}1201break;1202case 1:1203case 2:1204if (!(dpe->unknown & cond))1205iexec->execute = false;1206break;1207case 5:1208{1209struct nouveau_i2c_chan *auxch;1210int ret;12111212auxch = nouveau_i2c_find(dev, bios->display.output->i2c_index);1213if (!auxch) {1214NV_ERROR(dev, "0x%04X: couldn't get auxch\n", offset);1215return 3;1216}12171218ret = nouveau_dp_auxch(auxch, 9, 0xd, &cond, 1);1219if (ret) {1220NV_ERROR(dev, "0x%04X: auxch rd fail: %d\n", offset, ret);1221return 3;1222}12231224if (!(cond & 1))1225iexec->execute = false;1226}1227break;1228default:1229NV_WARN(dev, "0x%04X: unknown INIT_3A op: %d\n", offset, cond);1230break;1231}12321233if (iexec->execute)1234BIOSLOG(bios, "0x%04X: continuing to execute\n", offset);1235else1236BIOSLOG(bios, "0x%04X: skipping following commands\n", offset);12371238return 3;1239}12401241static int1242init_op_3b(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)1243{1244/*1245* INIT_3B opcode: 0x3B ('')1246*1247* offset (8 bit): opcode1248* offset + 1 (8 bit): crtc index1249*1250*/12511252uint8_t or = ffs(bios->display.output->or) - 1;1253uint8_t index = bios->data[offset + 1];1254uint8_t data;12551256if (!iexec->execute)1257return 2;12581259data = bios_idxprt_rd(bios, 0x3d4, index);1260bios_idxprt_wr(bios, 0x3d4, index, data & ~(1 << or));1261return 2;1262}12631264static int1265init_op_3c(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)1266{1267/*1268* INIT_3C opcode: 0x3C ('')1269*1270* offset (8 bit): opcode1271* offset + 1 (8 bit): crtc index1272*1273*/12741275uint8_t or = ffs(bios->display.output->or) - 1;1276uint8_t index = bios->data[offset + 1];1277uint8_t data;12781279if (!iexec->execute)1280return 2;12811282data = bios_idxprt_rd(bios, 0x3d4, index);1283bios_idxprt_wr(bios, 0x3d4, index, data | (1 << or));1284return 2;1285}12861287static int1288init_idx_addr_latched(struct nvbios *bios, uint16_t offset,1289struct init_exec *iexec)1290{1291/*1292* INIT_INDEX_ADDRESS_LATCHED opcode: 0x49 ('I')1293*1294* offset (8 bit): opcode1295* offset + 1 (32 bit): control register1296* offset + 5 (32 bit): data register1297* offset + 9 (32 bit): mask1298* offset + 13 (32 bit): data1299* offset + 17 (8 bit): count1300* offset + 18 (8 bit): address 11301* offset + 19 (8 bit): data 11302* ...1303*1304* For each of "count" address and data pairs, write "data n" to1305* "data register", read the current value of "control register",1306* and write it back once ANDed with "mask", ORed with "data",1307* and ORed with "address n"1308*/13091310uint32_t controlreg = ROM32(bios->data[offset + 1]);1311uint32_t datareg = ROM32(bios->data[offset + 5]);1312uint32_t mask = ROM32(bios->data[offset + 9]);1313uint32_t data = ROM32(bios->data[offset + 13]);1314uint8_t count = bios->data[offset + 17];1315int len = 18 + count * 2;1316uint32_t value;1317int i;13181319if (!iexec->execute)1320return len;13211322BIOSLOG(bios, "0x%04X: ControlReg: 0x%08X, DataReg: 0x%08X, "1323"Mask: 0x%08X, Data: 0x%08X, Count: 0x%02X\n",1324offset, controlreg, datareg, mask, data, count);13251326for (i = 0; i < count; i++) {1327uint8_t instaddress = bios->data[offset + 18 + i * 2];1328uint8_t instdata = bios->data[offset + 19 + i * 2];13291330BIOSLOG(bios, "0x%04X: Address: 0x%02X, Data: 0x%02X\n",1331offset, instaddress, instdata);13321333bios_wr32(bios, datareg, instdata);1334value = bios_rd32(bios, controlreg) & mask;1335value |= data;1336value |= instaddress;1337bios_wr32(bios, controlreg, value);1338}13391340return len;1341}13421343static int1344init_io_restrict_pll2(struct nvbios *bios, uint16_t offset,1345struct init_exec *iexec)1346{1347/*1348* INIT_IO_RESTRICT_PLL2 opcode: 0x4A ('J')1349*1350* offset (8 bit): opcode1351* offset + 1 (16 bit): CRTC port1352* offset + 3 (8 bit): CRTC index1353* offset + 4 (8 bit): mask1354* offset + 5 (8 bit): shift1355* offset + 6 (8 bit): count1356* offset + 7 (32 bit): register1357* offset + 11 (32 bit): frequency 11358* ...1359*1360* Starting at offset + 11 there are "count" 32 bit frequencies (kHz).1361* Set PLL register "register" to coefficients for frequency n,1362* selected by reading index "CRTC index" of "CRTC port" ANDed with1363* "mask" and shifted right by "shift".1364*/13651366uint16_t crtcport = ROM16(bios->data[offset + 1]);1367uint8_t crtcindex = bios->data[offset + 3];1368uint8_t mask = bios->data[offset + 4];1369uint8_t shift = bios->data[offset + 5];1370uint8_t count = bios->data[offset + 6];1371uint32_t reg = ROM32(bios->data[offset + 7]);1372int len = 11 + count * 4;1373uint8_t config;1374uint32_t freq;13751376if (!iexec->execute)1377return len;13781379BIOSLOG(bios, "0x%04X: Port: 0x%04X, Index: 0x%02X, Mask: 0x%02X, "1380"Shift: 0x%02X, Count: 0x%02X, Reg: 0x%08X\n",1381offset, crtcport, crtcindex, mask, shift, count, reg);13821383if (!reg)1384return len;13851386config = (bios_idxprt_rd(bios, crtcport, crtcindex) & mask) >> shift;1387if (config > count) {1388NV_ERROR(bios->dev,1389"0x%04X: Config 0x%02X exceeds maximal bound 0x%02X\n",1390offset, config, count);1391return len;1392}13931394freq = ROM32(bios->data[offset + 11 + config * 4]);13951396BIOSLOG(bios, "0x%04X: Reg: 0x%08X, Config: 0x%02X, Freq: %dkHz\n",1397offset, reg, config, freq);13981399setPLL(bios, reg, freq);14001401return len;1402}14031404static int1405init_pll2(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)1406{1407/*1408* INIT_PLL2 opcode: 0x4B ('K')1409*1410* offset (8 bit): opcode1411* offset + 1 (32 bit): register1412* offset + 5 (32 bit): freq1413*1414* Set PLL register "register" to coefficients for frequency "freq"1415*/14161417uint32_t reg = ROM32(bios->data[offset + 1]);1418uint32_t freq = ROM32(bios->data[offset + 5]);14191420if (!iexec->execute)1421return 9;14221423BIOSLOG(bios, "0x%04X: Reg: 0x%04X, Freq: %dkHz\n",1424offset, reg, freq);14251426setPLL(bios, reg, freq);1427return 9;1428}14291430static int1431init_i2c_byte(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)1432{1433/*1434* INIT_I2C_BYTE opcode: 0x4C ('L')1435*1436* offset (8 bit): opcode1437* offset + 1 (8 bit): DCB I2C table entry index1438* offset + 2 (8 bit): I2C slave address1439* offset + 3 (8 bit): count1440* offset + 4 (8 bit): I2C register 11441* offset + 5 (8 bit): mask 11442* offset + 6 (8 bit): data 11443* ...1444*1445* For each of "count" registers given by "I2C register n" on the device1446* addressed by "I2C slave address" on the I2C bus given by1447* "DCB I2C table entry index", read the register, AND the result with1448* "mask n" and OR it with "data n" before writing it back to the device1449*/14501451struct drm_device *dev = bios->dev;1452uint8_t i2c_index = bios->data[offset + 1];1453uint8_t i2c_address = bios->data[offset + 2] >> 1;1454uint8_t count = bios->data[offset + 3];1455struct nouveau_i2c_chan *chan;1456int len = 4 + count * 3;1457int ret, i;14581459if (!iexec->execute)1460return len;14611462BIOSLOG(bios, "0x%04X: DCBI2CIndex: 0x%02X, I2CAddress: 0x%02X, "1463"Count: 0x%02X\n",1464offset, i2c_index, i2c_address, count);14651466chan = init_i2c_device_find(dev, i2c_index);1467if (!chan) {1468NV_ERROR(dev, "0x%04X: i2c bus not found\n", offset);1469return len;1470}14711472for (i = 0; i < count; i++) {1473uint8_t reg = bios->data[offset + 4 + i * 3];1474uint8_t mask = bios->data[offset + 5 + i * 3];1475uint8_t data = bios->data[offset + 6 + i * 3];1476union i2c_smbus_data val;14771478ret = i2c_smbus_xfer(&chan->adapter, i2c_address, 0,1479I2C_SMBUS_READ, reg,1480I2C_SMBUS_BYTE_DATA, &val);1481if (ret < 0) {1482NV_ERROR(dev, "0x%04X: i2c rd fail: %d\n", offset, ret);1483return len;1484}14851486BIOSLOG(bios, "0x%04X: I2CReg: 0x%02X, Value: 0x%02X, "1487"Mask: 0x%02X, Data: 0x%02X\n",1488offset, reg, val.byte, mask, data);14891490if (!bios->execute)1491continue;14921493val.byte &= mask;1494val.byte |= data;1495ret = i2c_smbus_xfer(&chan->adapter, i2c_address, 0,1496I2C_SMBUS_WRITE, reg,1497I2C_SMBUS_BYTE_DATA, &val);1498if (ret < 0) {1499NV_ERROR(dev, "0x%04X: i2c wr fail: %d\n", offset, ret);1500return len;1501}1502}15031504return len;1505}15061507static int1508init_zm_i2c_byte(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)1509{1510/*1511* INIT_ZM_I2C_BYTE opcode: 0x4D ('M')1512*1513* offset (8 bit): opcode1514* offset + 1 (8 bit): DCB I2C table entry index1515* offset + 2 (8 bit): I2C slave address1516* offset + 3 (8 bit): count1517* offset + 4 (8 bit): I2C register 11518* offset + 5 (8 bit): data 11519* ...1520*1521* For each of "count" registers given by "I2C register n" on the device1522* addressed by "I2C slave address" on the I2C bus given by1523* "DCB I2C table entry index", set the register to "data n"1524*/15251526struct drm_device *dev = bios->dev;1527uint8_t i2c_index = bios->data[offset + 1];1528uint8_t i2c_address = bios->data[offset + 2] >> 1;1529uint8_t count = bios->data[offset + 3];1530struct nouveau_i2c_chan *chan;1531int len = 4 + count * 2;1532int ret, i;15331534if (!iexec->execute)1535return len;15361537BIOSLOG(bios, "0x%04X: DCBI2CIndex: 0x%02X, I2CAddress: 0x%02X, "1538"Count: 0x%02X\n",1539offset, i2c_index, i2c_address, count);15401541chan = init_i2c_device_find(dev, i2c_index);1542if (!chan) {1543NV_ERROR(dev, "0x%04X: i2c bus not found\n", offset);1544return len;1545}15461547for (i = 0; i < count; i++) {1548uint8_t reg = bios->data[offset + 4 + i * 2];1549union i2c_smbus_data val;15501551val.byte = bios->data[offset + 5 + i * 2];15521553BIOSLOG(bios, "0x%04X: I2CReg: 0x%02X, Data: 0x%02X\n",1554offset, reg, val.byte);15551556if (!bios->execute)1557continue;15581559ret = i2c_smbus_xfer(&chan->adapter, i2c_address, 0,1560I2C_SMBUS_WRITE, reg,1561I2C_SMBUS_BYTE_DATA, &val);1562if (ret < 0) {1563NV_ERROR(dev, "0x%04X: i2c wr fail: %d\n", offset, ret);1564return len;1565}1566}15671568return len;1569}15701571static int1572init_zm_i2c(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)1573{1574/*1575* INIT_ZM_I2C opcode: 0x4E ('N')1576*1577* offset (8 bit): opcode1578* offset + 1 (8 bit): DCB I2C table entry index1579* offset + 2 (8 bit): I2C slave address1580* offset + 3 (8 bit): count1581* offset + 4 (8 bit): data 11582* ...1583*1584* Send "count" bytes ("data n") to the device addressed by "I2C slave1585* address" on the I2C bus given by "DCB I2C table entry index"1586*/15871588struct drm_device *dev = bios->dev;1589uint8_t i2c_index = bios->data[offset + 1];1590uint8_t i2c_address = bios->data[offset + 2] >> 1;1591uint8_t count = bios->data[offset + 3];1592int len = 4 + count;1593struct nouveau_i2c_chan *chan;1594struct i2c_msg msg;1595uint8_t data[256];1596int ret, i;15971598if (!iexec->execute)1599return len;16001601BIOSLOG(bios, "0x%04X: DCBI2CIndex: 0x%02X, I2CAddress: 0x%02X, "1602"Count: 0x%02X\n",1603offset, i2c_index, i2c_address, count);16041605chan = init_i2c_device_find(dev, i2c_index);1606if (!chan) {1607NV_ERROR(dev, "0x%04X: i2c bus not found\n", offset);1608return len;1609}16101611for (i = 0; i < count; i++) {1612data[i] = bios->data[offset + 4 + i];16131614BIOSLOG(bios, "0x%04X: Data: 0x%02X\n", offset, data[i]);1615}16161617if (bios->execute) {1618msg.addr = i2c_address;1619msg.flags = 0;1620msg.len = count;1621msg.buf = data;1622ret = i2c_transfer(&chan->adapter, &msg, 1);1623if (ret != 1) {1624NV_ERROR(dev, "0x%04X: i2c wr fail: %d\n", offset, ret);1625return len;1626}1627}16281629return len;1630}16311632static int1633init_tmds(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)1634{1635/*1636* INIT_TMDS opcode: 0x4F ('O') (non-canon name)1637*1638* offset (8 bit): opcode1639* offset + 1 (8 bit): magic lookup value1640* offset + 2 (8 bit): TMDS address1641* offset + 3 (8 bit): mask1642* offset + 4 (8 bit): data1643*1644* Read the data reg for TMDS address "TMDS address", AND it with mask1645* and OR it with data, then write it back1646* "magic lookup value" determines which TMDS base address register is1647* used -- see get_tmds_index_reg()1648*/16491650struct drm_device *dev = bios->dev;1651uint8_t mlv = bios->data[offset + 1];1652uint32_t tmdsaddr = bios->data[offset + 2];1653uint8_t mask = bios->data[offset + 3];1654uint8_t data = bios->data[offset + 4];1655uint32_t reg, value;16561657if (!iexec->execute)1658return 5;16591660BIOSLOG(bios, "0x%04X: MagicLookupValue: 0x%02X, TMDSAddr: 0x%02X, "1661"Mask: 0x%02X, Data: 0x%02X\n",1662offset, mlv, tmdsaddr, mask, data);16631664reg = get_tmds_index_reg(bios->dev, mlv);1665if (!reg) {1666NV_ERROR(dev, "0x%04X: no tmds_index_reg\n", offset);1667return 5;1668}16691670bios_wr32(bios, reg,1671tmdsaddr | NV_PRAMDAC_FP_TMDS_CONTROL_WRITE_DISABLE);1672value = (bios_rd32(bios, reg + 4) & mask) | data;1673bios_wr32(bios, reg + 4, value);1674bios_wr32(bios, reg, tmdsaddr);16751676return 5;1677}16781679static int1680init_zm_tmds_group(struct nvbios *bios, uint16_t offset,1681struct init_exec *iexec)1682{1683/*1684* INIT_ZM_TMDS_GROUP opcode: 0x50 ('P') (non-canon name)1685*1686* offset (8 bit): opcode1687* offset + 1 (8 bit): magic lookup value1688* offset + 2 (8 bit): count1689* offset + 3 (8 bit): addr 11690* offset + 4 (8 bit): data 11691* ...1692*1693* For each of "count" TMDS address and data pairs write "data n" to1694* "addr n". "magic lookup value" determines which TMDS base address1695* register is used -- see get_tmds_index_reg()1696*/16971698struct drm_device *dev = bios->dev;1699uint8_t mlv = bios->data[offset + 1];1700uint8_t count = bios->data[offset + 2];1701int len = 3 + count * 2;1702uint32_t reg;1703int i;17041705if (!iexec->execute)1706return len;17071708BIOSLOG(bios, "0x%04X: MagicLookupValue: 0x%02X, Count: 0x%02X\n",1709offset, mlv, count);17101711reg = get_tmds_index_reg(bios->dev, mlv);1712if (!reg) {1713NV_ERROR(dev, "0x%04X: no tmds_index_reg\n", offset);1714return len;1715}17161717for (i = 0; i < count; i++) {1718uint8_t tmdsaddr = bios->data[offset + 3 + i * 2];1719uint8_t tmdsdata = bios->data[offset + 4 + i * 2];17201721bios_wr32(bios, reg + 4, tmdsdata);1722bios_wr32(bios, reg, tmdsaddr);1723}17241725return len;1726}17271728static int1729init_cr_idx_adr_latch(struct nvbios *bios, uint16_t offset,1730struct init_exec *iexec)1731{1732/*1733* INIT_CR_INDEX_ADDRESS_LATCHED opcode: 0x51 ('Q')1734*1735* offset (8 bit): opcode1736* offset + 1 (8 bit): CRTC index11737* offset + 2 (8 bit): CRTC index21738* offset + 3 (8 bit): baseaddr1739* offset + 4 (8 bit): count1740* offset + 5 (8 bit): data 11741* ...1742*1743* For each of "count" address and data pairs, write "baseaddr + n" to1744* "CRTC index1" and "data n" to "CRTC index2"1745* Once complete, restore initial value read from "CRTC index1"1746*/1747uint8_t crtcindex1 = bios->data[offset + 1];1748uint8_t crtcindex2 = bios->data[offset + 2];1749uint8_t baseaddr = bios->data[offset + 3];1750uint8_t count = bios->data[offset + 4];1751int len = 5 + count;1752uint8_t oldaddr, data;1753int i;17541755if (!iexec->execute)1756return len;17571758BIOSLOG(bios, "0x%04X: Index1: 0x%02X, Index2: 0x%02X, "1759"BaseAddr: 0x%02X, Count: 0x%02X\n",1760offset, crtcindex1, crtcindex2, baseaddr, count);17611762oldaddr = bios_idxprt_rd(bios, NV_CIO_CRX__COLOR, crtcindex1);17631764for (i = 0; i < count; i++) {1765bios_idxprt_wr(bios, NV_CIO_CRX__COLOR, crtcindex1,1766baseaddr + i);1767data = bios->data[offset + 5 + i];1768bios_idxprt_wr(bios, NV_CIO_CRX__COLOR, crtcindex2, data);1769}17701771bios_idxprt_wr(bios, NV_CIO_CRX__COLOR, crtcindex1, oldaddr);17721773return len;1774}17751776static int1777init_cr(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)1778{1779/*1780* INIT_CR opcode: 0x52 ('R')1781*1782* offset (8 bit): opcode1783* offset + 1 (8 bit): CRTC index1784* offset + 2 (8 bit): mask1785* offset + 3 (8 bit): data1786*1787* Assign the value of at "CRTC index" ANDed with mask and ORed with1788* data back to "CRTC index"1789*/17901791uint8_t crtcindex = bios->data[offset + 1];1792uint8_t mask = bios->data[offset + 2];1793uint8_t data = bios->data[offset + 3];1794uint8_t value;17951796if (!iexec->execute)1797return 4;17981799BIOSLOG(bios, "0x%04X: Index: 0x%02X, Mask: 0x%02X, Data: 0x%02X\n",1800offset, crtcindex, mask, data);18011802value = bios_idxprt_rd(bios, NV_CIO_CRX__COLOR, crtcindex) & mask;1803value |= data;1804bios_idxprt_wr(bios, NV_CIO_CRX__COLOR, crtcindex, value);18051806return 4;1807}18081809static int1810init_zm_cr(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)1811{1812/*1813* INIT_ZM_CR opcode: 0x53 ('S')1814*1815* offset (8 bit): opcode1816* offset + 1 (8 bit): CRTC index1817* offset + 2 (8 bit): value1818*1819* Assign "value" to CRTC register with index "CRTC index".1820*/18211822uint8_t crtcindex = ROM32(bios->data[offset + 1]);1823uint8_t data = bios->data[offset + 2];18241825if (!iexec->execute)1826return 3;18271828bios_idxprt_wr(bios, NV_CIO_CRX__COLOR, crtcindex, data);18291830return 3;1831}18321833static int1834init_zm_cr_group(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)1835{1836/*1837* INIT_ZM_CR_GROUP opcode: 0x54 ('T')1838*1839* offset (8 bit): opcode1840* offset + 1 (8 bit): count1841* offset + 2 (8 bit): CRTC index 11842* offset + 3 (8 bit): value 11843* ...1844*1845* For "count", assign "value n" to CRTC register with index1846* "CRTC index n".1847*/18481849uint8_t count = bios->data[offset + 1];1850int len = 2 + count * 2;1851int i;18521853if (!iexec->execute)1854return len;18551856for (i = 0; i < count; i++)1857init_zm_cr(bios, offset + 2 + 2 * i - 1, iexec);18581859return len;1860}18611862static int1863init_condition_time(struct nvbios *bios, uint16_t offset,1864struct init_exec *iexec)1865{1866/*1867* INIT_CONDITION_TIME opcode: 0x56 ('V')1868*1869* offset (8 bit): opcode1870* offset + 1 (8 bit): condition number1871* offset + 2 (8 bit): retries / 501872*1873* Check condition "condition number" in the condition table.1874* Bios code then sleeps for 2ms if the condition is not met, and1875* repeats up to "retries" times, but on one C51 this has proved1876* insufficient. In mmiotraces the driver sleeps for 20ms, so we do1877* this, and bail after "retries" times, or 2s, whichever is less.1878* If still not met after retries, clear execution flag for this table.1879*/18801881uint8_t cond = bios->data[offset + 1];1882uint16_t retries = bios->data[offset + 2] * 50;1883unsigned cnt;18841885if (!iexec->execute)1886return 3;18871888if (retries > 100)1889retries = 100;18901891BIOSLOG(bios, "0x%04X: Condition: 0x%02X, Retries: 0x%02X\n",1892offset, cond, retries);18931894if (!bios->execute) /* avoid 2s delays when "faking" execution */1895retries = 1;18961897for (cnt = 0; cnt < retries; cnt++) {1898if (bios_condition_met(bios, offset, cond)) {1899BIOSLOG(bios, "0x%04X: Condition met, continuing\n",1900offset);1901break;1902} else {1903BIOSLOG(bios, "0x%04X: "1904"Condition not met, sleeping for 20ms\n",1905offset);1906mdelay(20);1907}1908}19091910if (!bios_condition_met(bios, offset, cond)) {1911NV_WARN(bios->dev,1912"0x%04X: Condition still not met after %dms, "1913"skipping following opcodes\n", offset, 20 * retries);1914iexec->execute = false;1915}19161917return 3;1918}19191920static int1921init_ltime(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)1922{1923/*1924* INIT_LTIME opcode: 0x57 ('V')1925*1926* offset (8 bit): opcode1927* offset + 1 (16 bit): time1928*1929* Sleep for "time" milliseconds.1930*/19311932unsigned time = ROM16(bios->data[offset + 1]);19331934if (!iexec->execute)1935return 3;19361937BIOSLOG(bios, "0x%04X: Sleeping for 0x%04X milliseconds\n",1938offset, time);19391940mdelay(time);19411942return 3;1943}19441945static int1946init_zm_reg_sequence(struct nvbios *bios, uint16_t offset,1947struct init_exec *iexec)1948{1949/*1950* INIT_ZM_REG_SEQUENCE opcode: 0x58 ('X')1951*1952* offset (8 bit): opcode1953* offset + 1 (32 bit): base register1954* offset + 5 (8 bit): count1955* offset + 6 (32 bit): value 11956* ...1957*1958* Starting at offset + 6 there are "count" 32 bit values.1959* For "count" iterations set "base register" + 4 * current_iteration1960* to "value current_iteration"1961*/19621963uint32_t basereg = ROM32(bios->data[offset + 1]);1964uint32_t count = bios->data[offset + 5];1965int len = 6 + count * 4;1966int i;19671968if (!iexec->execute)1969return len;19701971BIOSLOG(bios, "0x%04X: BaseReg: 0x%08X, Count: 0x%02X\n",1972offset, basereg, count);19731974for (i = 0; i < count; i++) {1975uint32_t reg = basereg + i * 4;1976uint32_t data = ROM32(bios->data[offset + 6 + i * 4]);19771978bios_wr32(bios, reg, data);1979}19801981return len;1982}19831984static int1985init_sub_direct(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)1986{1987/*1988* INIT_SUB_DIRECT opcode: 0x5B ('[')1989*1990* offset (8 bit): opcode1991* offset + 1 (16 bit): subroutine offset (in bios)1992*1993* Calls a subroutine that will execute commands until INIT_DONE1994* is found.1995*/19961997uint16_t sub_offset = ROM16(bios->data[offset + 1]);19981999if (!iexec->execute)2000return 3;20012002BIOSLOG(bios, "0x%04X: Executing subroutine at 0x%04X\n",2003offset, sub_offset);20042005parse_init_table(bios, sub_offset, iexec);20062007BIOSLOG(bios, "0x%04X: End of 0x%04X subroutine\n", offset, sub_offset);20082009return 3;2010}20112012static int2013init_jump(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)2014{2015/*2016* INIT_JUMP opcode: 0x5C ('\')2017*2018* offset (8 bit): opcode2019* offset + 1 (16 bit): offset (in bios)2020*2021* Continue execution of init table from 'offset'2022*/20232024uint16_t jmp_offset = ROM16(bios->data[offset + 1]);20252026if (!iexec->execute)2027return 3;20282029BIOSLOG(bios, "0x%04X: Jump to 0x%04X\n", offset, jmp_offset);2030return jmp_offset - offset;2031}20322033static int2034init_i2c_if(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)2035{2036/*2037* INIT_I2C_IF opcode: 0x5E ('^')2038*2039* offset (8 bit): opcode2040* offset + 1 (8 bit): DCB I2C table entry index2041* offset + 2 (8 bit): I2C slave address2042* offset + 3 (8 bit): I2C register2043* offset + 4 (8 bit): mask2044* offset + 5 (8 bit): data2045*2046* Read the register given by "I2C register" on the device addressed2047* by "I2C slave address" on the I2C bus given by "DCB I2C table2048* entry index". Compare the result AND "mask" to "data".2049* If they're not equal, skip subsequent opcodes until condition is2050* inverted (INIT_NOT), or we hit INIT_RESUME2051*/20522053uint8_t i2c_index = bios->data[offset + 1];2054uint8_t i2c_address = bios->data[offset + 2] >> 1;2055uint8_t reg = bios->data[offset + 3];2056uint8_t mask = bios->data[offset + 4];2057uint8_t data = bios->data[offset + 5];2058struct nouveau_i2c_chan *chan;2059union i2c_smbus_data val;2060int ret;20612062/* no execute check by design */20632064BIOSLOG(bios, "0x%04X: DCBI2CIndex: 0x%02X, I2CAddress: 0x%02X\n",2065offset, i2c_index, i2c_address);20662067chan = init_i2c_device_find(bios->dev, i2c_index);2068if (!chan)2069return -ENODEV;20702071ret = i2c_smbus_xfer(&chan->adapter, i2c_address, 0,2072I2C_SMBUS_READ, reg,2073I2C_SMBUS_BYTE_DATA, &val);2074if (ret < 0) {2075BIOSLOG(bios, "0x%04X: I2CReg: 0x%02X, Value: [no device], "2076"Mask: 0x%02X, Data: 0x%02X\n",2077offset, reg, mask, data);2078iexec->execute = 0;2079return 6;2080}20812082BIOSLOG(bios, "0x%04X: I2CReg: 0x%02X, Value: 0x%02X, "2083"Mask: 0x%02X, Data: 0x%02X\n",2084offset, reg, val.byte, mask, data);20852086iexec->execute = ((val.byte & mask) == data);20872088return 6;2089}20902091static int2092init_copy_nv_reg(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)2093{2094/*2095* INIT_COPY_NV_REG opcode: 0x5F ('_')2096*2097* offset (8 bit): opcode2098* offset + 1 (32 bit): src reg2099* offset + 5 (8 bit): shift2100* offset + 6 (32 bit): src mask2101* offset + 10 (32 bit): xor2102* offset + 14 (32 bit): dst reg2103* offset + 18 (32 bit): dst mask2104*2105* Shift REGVAL("src reg") right by (signed) "shift", AND result with2106* "src mask", then XOR with "xor". Write this OR'd with2107* (REGVAL("dst reg") AND'd with "dst mask") to "dst reg"2108*/21092110uint32_t srcreg = *((uint32_t *)(&bios->data[offset + 1]));2111uint8_t shift = bios->data[offset + 5];2112uint32_t srcmask = *((uint32_t *)(&bios->data[offset + 6]));2113uint32_t xor = *((uint32_t *)(&bios->data[offset + 10]));2114uint32_t dstreg = *((uint32_t *)(&bios->data[offset + 14]));2115uint32_t dstmask = *((uint32_t *)(&bios->data[offset + 18]));2116uint32_t srcvalue, dstvalue;21172118if (!iexec->execute)2119return 22;21202121BIOSLOG(bios, "0x%04X: SrcReg: 0x%08X, Shift: 0x%02X, SrcMask: 0x%08X, "2122"Xor: 0x%08X, DstReg: 0x%08X, DstMask: 0x%08X\n",2123offset, srcreg, shift, srcmask, xor, dstreg, dstmask);21242125srcvalue = bios_rd32(bios, srcreg);21262127if (shift < 0x80)2128srcvalue >>= shift;2129else2130srcvalue <<= (0x100 - shift);21312132srcvalue = (srcvalue & srcmask) ^ xor;21332134dstvalue = bios_rd32(bios, dstreg) & dstmask;21352136bios_wr32(bios, dstreg, dstvalue | srcvalue);21372138return 22;2139}21402141static int2142init_zm_index_io(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)2143{2144/*2145* INIT_ZM_INDEX_IO opcode: 0x62 ('b')2146*2147* offset (8 bit): opcode2148* offset + 1 (16 bit): CRTC port2149* offset + 3 (8 bit): CRTC index2150* offset + 4 (8 bit): data2151*2152* Write "data" to index "CRTC index" of "CRTC port"2153*/2154uint16_t crtcport = ROM16(bios->data[offset + 1]);2155uint8_t crtcindex = bios->data[offset + 3];2156uint8_t data = bios->data[offset + 4];21572158if (!iexec->execute)2159return 5;21602161bios_idxprt_wr(bios, crtcport, crtcindex, data);21622163return 5;2164}21652166static inline void2167bios_md32(struct nvbios *bios, uint32_t reg,2168uint32_t mask, uint32_t val)2169{2170bios_wr32(bios, reg, (bios_rd32(bios, reg) & ~mask) | val);2171}21722173static uint32_t2174peek_fb(struct drm_device *dev, struct io_mapping *fb,2175uint32_t off)2176{2177uint32_t val = 0;21782179if (off < pci_resource_len(dev->pdev, 1)) {2180uint8_t __iomem *p =2181io_mapping_map_atomic_wc(fb, off & PAGE_MASK);21822183val = ioread32(p + (off & ~PAGE_MASK));21842185io_mapping_unmap_atomic(p);2186}21872188return val;2189}21902191static void2192poke_fb(struct drm_device *dev, struct io_mapping *fb,2193uint32_t off, uint32_t val)2194{2195if (off < pci_resource_len(dev->pdev, 1)) {2196uint8_t __iomem *p =2197io_mapping_map_atomic_wc(fb, off & PAGE_MASK);21982199iowrite32(val, p + (off & ~PAGE_MASK));2200wmb();22012202io_mapping_unmap_atomic(p);2203}2204}22052206static inline bool2207read_back_fb(struct drm_device *dev, struct io_mapping *fb,2208uint32_t off, uint32_t val)2209{2210poke_fb(dev, fb, off, val);2211return val == peek_fb(dev, fb, off);2212}22132214static int2215nv04_init_compute_mem(struct nvbios *bios)2216{2217struct drm_device *dev = bios->dev;2218uint32_t patt = 0xdeadbeef;2219struct io_mapping *fb;2220int i;22212222/* Map the framebuffer aperture */2223fb = io_mapping_create_wc(pci_resource_start(dev->pdev, 1),2224pci_resource_len(dev->pdev, 1));2225if (!fb)2226return -ENOMEM;22272228/* Sequencer and refresh off */2229NVWriteVgaSeq(dev, 0, 1, NVReadVgaSeq(dev, 0, 1) | 0x20);2230bios_md32(bios, NV04_PFB_DEBUG_0, 0, NV04_PFB_DEBUG_0_REFRESH_OFF);22312232bios_md32(bios, NV04_PFB_BOOT_0, ~0,2233NV04_PFB_BOOT_0_RAM_AMOUNT_16MB |2234NV04_PFB_BOOT_0_RAM_WIDTH_128 |2235NV04_PFB_BOOT_0_RAM_TYPE_SGRAM_16MBIT);22362237for (i = 0; i < 4; i++)2238poke_fb(dev, fb, 4 * i, patt);22392240poke_fb(dev, fb, 0x400000, patt + 1);22412242if (peek_fb(dev, fb, 0) == patt + 1) {2243bios_md32(bios, NV04_PFB_BOOT_0, NV04_PFB_BOOT_0_RAM_TYPE,2244NV04_PFB_BOOT_0_RAM_TYPE_SDRAM_16MBIT);2245bios_md32(bios, NV04_PFB_DEBUG_0,2246NV04_PFB_DEBUG_0_REFRESH_OFF, 0);22472248for (i = 0; i < 4; i++)2249poke_fb(dev, fb, 4 * i, patt);22502251if ((peek_fb(dev, fb, 0xc) & 0xffff) != (patt & 0xffff))2252bios_md32(bios, NV04_PFB_BOOT_0,2253NV04_PFB_BOOT_0_RAM_WIDTH_128 |2254NV04_PFB_BOOT_0_RAM_AMOUNT,2255NV04_PFB_BOOT_0_RAM_AMOUNT_8MB);22562257} else if ((peek_fb(dev, fb, 0xc) & 0xffff0000) !=2258(patt & 0xffff0000)) {2259bios_md32(bios, NV04_PFB_BOOT_0,2260NV04_PFB_BOOT_0_RAM_WIDTH_128 |2261NV04_PFB_BOOT_0_RAM_AMOUNT,2262NV04_PFB_BOOT_0_RAM_AMOUNT_4MB);22632264} else if (peek_fb(dev, fb, 0) != patt) {2265if (read_back_fb(dev, fb, 0x800000, patt))2266bios_md32(bios, NV04_PFB_BOOT_0,2267NV04_PFB_BOOT_0_RAM_AMOUNT,2268NV04_PFB_BOOT_0_RAM_AMOUNT_8MB);2269else2270bios_md32(bios, NV04_PFB_BOOT_0,2271NV04_PFB_BOOT_0_RAM_AMOUNT,2272NV04_PFB_BOOT_0_RAM_AMOUNT_4MB);22732274bios_md32(bios, NV04_PFB_BOOT_0, NV04_PFB_BOOT_0_RAM_TYPE,2275NV04_PFB_BOOT_0_RAM_TYPE_SGRAM_8MBIT);22762277} else if (!read_back_fb(dev, fb, 0x800000, patt)) {2278bios_md32(bios, NV04_PFB_BOOT_0, NV04_PFB_BOOT_0_RAM_AMOUNT,2279NV04_PFB_BOOT_0_RAM_AMOUNT_8MB);22802281}22822283/* Refresh on, sequencer on */2284bios_md32(bios, NV04_PFB_DEBUG_0, NV04_PFB_DEBUG_0_REFRESH_OFF, 0);2285NVWriteVgaSeq(dev, 0, 1, NVReadVgaSeq(dev, 0, 1) & ~0x20);22862287io_mapping_free(fb);2288return 0;2289}22902291static const uint8_t *2292nv05_memory_config(struct nvbios *bios)2293{2294/* Defaults for BIOSes lacking a memory config table */2295static const uint8_t default_config_tab[][2] = {2296{ 0x24, 0x00 },2297{ 0x28, 0x00 },2298{ 0x24, 0x01 },2299{ 0x1f, 0x00 },2300{ 0x0f, 0x00 },2301{ 0x17, 0x00 },2302{ 0x06, 0x00 },2303{ 0x00, 0x00 }2304};2305int i = (bios_rd32(bios, NV_PEXTDEV_BOOT_0) &2306NV_PEXTDEV_BOOT_0_RAMCFG) >> 2;23072308if (bios->legacy.mem_init_tbl_ptr)2309return &bios->data[bios->legacy.mem_init_tbl_ptr + 2 * i];2310else2311return default_config_tab[i];2312}23132314static int2315nv05_init_compute_mem(struct nvbios *bios)2316{2317struct drm_device *dev = bios->dev;2318const uint8_t *ramcfg = nv05_memory_config(bios);2319uint32_t patt = 0xdeadbeef;2320struct io_mapping *fb;2321int i, v;23222323/* Map the framebuffer aperture */2324fb = io_mapping_create_wc(pci_resource_start(dev->pdev, 1),2325pci_resource_len(dev->pdev, 1));2326if (!fb)2327return -ENOMEM;23282329/* Sequencer off */2330NVWriteVgaSeq(dev, 0, 1, NVReadVgaSeq(dev, 0, 1) | 0x20);23312332if (bios_rd32(bios, NV04_PFB_BOOT_0) & NV04_PFB_BOOT_0_UMA_ENABLE)2333goto out;23342335bios_md32(bios, NV04_PFB_DEBUG_0, NV04_PFB_DEBUG_0_REFRESH_OFF, 0);23362337/* If present load the hardcoded scrambling table */2338if (bios->legacy.mem_init_tbl_ptr) {2339uint32_t *scramble_tab = (uint32_t *)&bios->data[2340bios->legacy.mem_init_tbl_ptr + 0x10];23412342for (i = 0; i < 8; i++)2343bios_wr32(bios, NV04_PFB_SCRAMBLE(i),2344ROM32(scramble_tab[i]));2345}23462347/* Set memory type/width/length defaults depending on the straps */2348bios_md32(bios, NV04_PFB_BOOT_0, 0x3f, ramcfg[0]);23492350if (ramcfg[1] & 0x80)2351bios_md32(bios, NV04_PFB_CFG0, 0, NV04_PFB_CFG0_SCRAMBLE);23522353bios_md32(bios, NV04_PFB_CFG1, 0x700001, (ramcfg[1] & 1) << 20);2354bios_md32(bios, NV04_PFB_CFG1, 0, 1);23552356/* Probe memory bus width */2357for (i = 0; i < 4; i++)2358poke_fb(dev, fb, 4 * i, patt);23592360if (peek_fb(dev, fb, 0xc) != patt)2361bios_md32(bios, NV04_PFB_BOOT_0,2362NV04_PFB_BOOT_0_RAM_WIDTH_128, 0);23632364/* Probe memory length */2365v = bios_rd32(bios, NV04_PFB_BOOT_0) & NV04_PFB_BOOT_0_RAM_AMOUNT;23662367if (v == NV04_PFB_BOOT_0_RAM_AMOUNT_32MB &&2368(!read_back_fb(dev, fb, 0x1000000, ++patt) ||2369!read_back_fb(dev, fb, 0, ++patt)))2370bios_md32(bios, NV04_PFB_BOOT_0, NV04_PFB_BOOT_0_RAM_AMOUNT,2371NV04_PFB_BOOT_0_RAM_AMOUNT_16MB);23722373if (v == NV04_PFB_BOOT_0_RAM_AMOUNT_16MB &&2374!read_back_fb(dev, fb, 0x800000, ++patt))2375bios_md32(bios, NV04_PFB_BOOT_0, NV04_PFB_BOOT_0_RAM_AMOUNT,2376NV04_PFB_BOOT_0_RAM_AMOUNT_8MB);23772378if (!read_back_fb(dev, fb, 0x400000, ++patt))2379bios_md32(bios, NV04_PFB_BOOT_0, NV04_PFB_BOOT_0_RAM_AMOUNT,2380NV04_PFB_BOOT_0_RAM_AMOUNT_4MB);23812382out:2383/* Sequencer on */2384NVWriteVgaSeq(dev, 0, 1, NVReadVgaSeq(dev, 0, 1) & ~0x20);23852386io_mapping_free(fb);2387return 0;2388}23892390static int2391nv10_init_compute_mem(struct nvbios *bios)2392{2393struct drm_device *dev = bios->dev;2394struct drm_nouveau_private *dev_priv = bios->dev->dev_private;2395const int mem_width[] = { 0x10, 0x00, 0x20 };2396const int mem_width_count = (dev_priv->chipset >= 0x17 ? 3 : 2);2397uint32_t patt = 0xdeadbeef;2398struct io_mapping *fb;2399int i, j, k;24002401/* Map the framebuffer aperture */2402fb = io_mapping_create_wc(pci_resource_start(dev->pdev, 1),2403pci_resource_len(dev->pdev, 1));2404if (!fb)2405return -ENOMEM;24062407bios_wr32(bios, NV10_PFB_REFCTRL, NV10_PFB_REFCTRL_VALID_1);24082409/* Probe memory bus width */2410for (i = 0; i < mem_width_count; i++) {2411bios_md32(bios, NV04_PFB_CFG0, 0x30, mem_width[i]);24122413for (j = 0; j < 4; j++) {2414for (k = 0; k < 4; k++)2415poke_fb(dev, fb, 0x1c, 0);24162417poke_fb(dev, fb, 0x1c, patt);2418poke_fb(dev, fb, 0x3c, 0);24192420if (peek_fb(dev, fb, 0x1c) == patt)2421goto mem_width_found;2422}2423}24242425mem_width_found:2426patt <<= 1;24272428/* Probe amount of installed memory */2429for (i = 0; i < 4; i++) {2430int off = bios_rd32(bios, NV04_PFB_FIFO_DATA) - 0x100000;24312432poke_fb(dev, fb, off, patt);2433poke_fb(dev, fb, 0, 0);24342435peek_fb(dev, fb, 0);2436peek_fb(dev, fb, 0);2437peek_fb(dev, fb, 0);2438peek_fb(dev, fb, 0);24392440if (peek_fb(dev, fb, off) == patt)2441goto amount_found;2442}24432444/* IC missing - disable the upper half memory space. */2445bios_md32(bios, NV04_PFB_CFG0, 0x1000, 0);24462447amount_found:2448io_mapping_free(fb);2449return 0;2450}24512452static int2453nv20_init_compute_mem(struct nvbios *bios)2454{2455struct drm_device *dev = bios->dev;2456struct drm_nouveau_private *dev_priv = bios->dev->dev_private;2457uint32_t mask = (dev_priv->chipset >= 0x25 ? 0x300 : 0x900);2458uint32_t amount, off;2459struct io_mapping *fb;24602461/* Map the framebuffer aperture */2462fb = io_mapping_create_wc(pci_resource_start(dev->pdev, 1),2463pci_resource_len(dev->pdev, 1));2464if (!fb)2465return -ENOMEM;24662467bios_wr32(bios, NV10_PFB_REFCTRL, NV10_PFB_REFCTRL_VALID_1);24682469/* Allow full addressing */2470bios_md32(bios, NV04_PFB_CFG0, 0, mask);24712472amount = bios_rd32(bios, NV04_PFB_FIFO_DATA);2473for (off = amount; off > 0x2000000; off -= 0x2000000)2474poke_fb(dev, fb, off - 4, off);24752476amount = bios_rd32(bios, NV04_PFB_FIFO_DATA);2477if (amount != peek_fb(dev, fb, amount - 4))2478/* IC missing - disable the upper half memory space. */2479bios_md32(bios, NV04_PFB_CFG0, mask, 0);24802481io_mapping_free(fb);2482return 0;2483}24842485static int2486init_compute_mem(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)2487{2488/*2489* INIT_COMPUTE_MEM opcode: 0x63 ('c')2490*2491* offset (8 bit): opcode2492*2493* This opcode is meant to set the PFB memory config registers2494* appropriately so that we can correctly calculate how much VRAM it2495* has (on nv10 and better chipsets the amount of installed VRAM is2496* subsequently reported in NV_PFB_CSTATUS (0x10020C)).2497*2498* The implementation of this opcode in general consists of several2499* parts:2500*2501* 1) Determination of memory type and density. Only necessary for2502* really old chipsets, the memory type reported by the strap bits2503* (0x101000) is assumed to be accurate on nv05 and newer.2504*2505* 2) Determination of the memory bus width. Usually done by a cunning2506* combination of writes to offsets 0x1c and 0x3c in the fb, and2507* seeing whether the written values are read back correctly.2508*2509* Only necessary on nv0x-nv1x and nv34, on the other cards we can2510* trust the straps.2511*2512* 3) Determination of how many of the card's RAM pads have ICs2513* attached, usually done by a cunning combination of writes to an2514* offset slightly less than the maximum memory reported by2515* NV_PFB_CSTATUS, then seeing if the test pattern can be read back.2516*2517* This appears to be a NOP on IGPs and NV4x or newer chipsets, both io2518* logs of the VBIOS and kmmio traces of the binary driver POSTing the2519* card show nothing being done for this opcode. Why is it still listed2520* in the table?!2521*/25222523/* no iexec->execute check by design */25242525struct drm_nouveau_private *dev_priv = bios->dev->dev_private;2526int ret;25272528if (dev_priv->chipset >= 0x40 ||2529dev_priv->chipset == 0x1a ||2530dev_priv->chipset == 0x1f)2531ret = 0;2532else if (dev_priv->chipset >= 0x20 &&2533dev_priv->chipset != 0x34)2534ret = nv20_init_compute_mem(bios);2535else if (dev_priv->chipset >= 0x10)2536ret = nv10_init_compute_mem(bios);2537else if (dev_priv->chipset >= 0x5)2538ret = nv05_init_compute_mem(bios);2539else2540ret = nv04_init_compute_mem(bios);25412542if (ret)2543return ret;25442545return 1;2546}25472548static int2549init_reset(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)2550{2551/*2552* INIT_RESET opcode: 0x65 ('e')2553*2554* offset (8 bit): opcode2555* offset + 1 (32 bit): register2556* offset + 5 (32 bit): value12557* offset + 9 (32 bit): value22558*2559* Assign "value1" to "register", then assign "value2" to "register"2560*/25612562uint32_t reg = ROM32(bios->data[offset + 1]);2563uint32_t value1 = ROM32(bios->data[offset + 5]);2564uint32_t value2 = ROM32(bios->data[offset + 9]);2565uint32_t pci_nv_19, pci_nv_20;25662567/* no iexec->execute check by design */25682569pci_nv_19 = bios_rd32(bios, NV_PBUS_PCI_NV_19);2570bios_wr32(bios, NV_PBUS_PCI_NV_19, pci_nv_19 & ~0xf00);25712572bios_wr32(bios, reg, value1);25732574udelay(10);25752576bios_wr32(bios, reg, value2);2577bios_wr32(bios, NV_PBUS_PCI_NV_19, pci_nv_19);25782579pci_nv_20 = bios_rd32(bios, NV_PBUS_PCI_NV_20);2580pci_nv_20 &= ~NV_PBUS_PCI_NV_20_ROM_SHADOW_ENABLED; /* 0xfffffffe */2581bios_wr32(bios, NV_PBUS_PCI_NV_20, pci_nv_20);25822583return 13;2584}25852586static int2587init_configure_mem(struct nvbios *bios, uint16_t offset,2588struct init_exec *iexec)2589{2590/*2591* INIT_CONFIGURE_MEM opcode: 0x66 ('f')2592*2593* offset (8 bit): opcode2594*2595* Equivalent to INIT_DONE on bios version 3 or greater.2596* For early bios versions, sets up the memory registers, using values2597* taken from the memory init table2598*/25992600/* no iexec->execute check by design */26012602uint16_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);2603uint16_t seqtbloffs = bios->legacy.sdr_seq_tbl_ptr, meminitdata = meminitoffs + 6;2604uint32_t reg, data;26052606if (bios->major_version > 2)2607return 0;26082609bios_idxprt_wr(bios, NV_VIO_SRX, NV_VIO_SR_CLOCK_INDEX, bios_idxprt_rd(2610bios, NV_VIO_SRX, NV_VIO_SR_CLOCK_INDEX) | 0x20);26112612if (bios->data[meminitoffs] & 1)2613seqtbloffs = bios->legacy.ddr_seq_tbl_ptr;26142615for (reg = ROM32(bios->data[seqtbloffs]);2616reg != 0xffffffff;2617reg = ROM32(bios->data[seqtbloffs += 4])) {26182619switch (reg) {2620case NV04_PFB_PRE:2621data = NV04_PFB_PRE_CMD_PRECHARGE;2622break;2623case NV04_PFB_PAD:2624data = NV04_PFB_PAD_CKE_NORMAL;2625break;2626case NV04_PFB_REF:2627data = NV04_PFB_REF_CMD_REFRESH;2628break;2629default:2630data = ROM32(bios->data[meminitdata]);2631meminitdata += 4;2632if (data == 0xffffffff)2633continue;2634}26352636bios_wr32(bios, reg, data);2637}26382639return 1;2640}26412642static int2643init_configure_clk(struct nvbios *bios, uint16_t offset,2644struct init_exec *iexec)2645{2646/*2647* INIT_CONFIGURE_CLK opcode: 0x67 ('g')2648*2649* offset (8 bit): opcode2650*2651* Equivalent to INIT_DONE on bios version 3 or greater.2652* For early bios versions, sets up the NVClk and MClk PLLs, using2653* values taken from the memory init table2654*/26552656/* no iexec->execute check by design */26572658uint16_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);2659int clock;26602661if (bios->major_version > 2)2662return 0;26632664clock = ROM16(bios->data[meminitoffs + 4]) * 10;2665setPLL(bios, NV_PRAMDAC_NVPLL_COEFF, clock);26662667clock = ROM16(bios->data[meminitoffs + 2]) * 10;2668if (bios->data[meminitoffs] & 1) /* DDR */2669clock *= 2;2670setPLL(bios, NV_PRAMDAC_MPLL_COEFF, clock);26712672return 1;2673}26742675static int2676init_configure_preinit(struct nvbios *bios, uint16_t offset,2677struct init_exec *iexec)2678{2679/*2680* INIT_CONFIGURE_PREINIT opcode: 0x68 ('h')2681*2682* offset (8 bit): opcode2683*2684* Equivalent to INIT_DONE on bios version 3 or greater.2685* For early bios versions, does early init, loading ram and crystal2686* configuration from straps into CR3C2687*/26882689/* no iexec->execute check by design */26902691uint32_t straps = bios_rd32(bios, NV_PEXTDEV_BOOT_0);2692uint8_t cr3c = ((straps << 2) & 0xf0) | (straps & 0x40) >> 6;26932694if (bios->major_version > 2)2695return 0;26962697bios_idxprt_wr(bios, NV_CIO_CRX__COLOR,2698NV_CIO_CRE_SCRATCH4__INDEX, cr3c);26992700return 1;2701}27022703static int2704init_io(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)2705{2706/*2707* INIT_IO opcode: 0x69 ('i')2708*2709* offset (8 bit): opcode2710* offset + 1 (16 bit): CRTC port2711* offset + 3 (8 bit): mask2712* offset + 4 (8 bit): data2713*2714* Assign ((IOVAL("crtc port") & "mask") | "data") to "crtc port"2715*/27162717struct drm_nouveau_private *dev_priv = bios->dev->dev_private;2718uint16_t crtcport = ROM16(bios->data[offset + 1]);2719uint8_t mask = bios->data[offset + 3];2720uint8_t data = bios->data[offset + 4];27212722if (!iexec->execute)2723return 5;27242725BIOSLOG(bios, "0x%04X: Port: 0x%04X, Mask: 0x%02X, Data: 0x%02X\n",2726offset, crtcport, mask, data);27272728/*2729* I have no idea what this does, but NVIDIA do this magic sequence2730* in the places where this INIT_IO happens..2731*/2732if (dev_priv->card_type >= NV_50 && crtcport == 0x3c3 && data == 1) {2733int i;27342735bios_wr32(bios, 0x614100, (bios_rd32(2736bios, 0x614100) & 0x0fffffff) | 0x00800000);27372738bios_wr32(bios, 0x00e18c, bios_rd32(2739bios, 0x00e18c) | 0x00020000);27402741bios_wr32(bios, 0x614900, (bios_rd32(2742bios, 0x614900) & 0x0fffffff) | 0x00800000);27432744bios_wr32(bios, 0x000200, bios_rd32(2745bios, 0x000200) & ~0x40000000);27462747mdelay(10);27482749bios_wr32(bios, 0x00e18c, bios_rd32(2750bios, 0x00e18c) & ~0x00020000);27512752bios_wr32(bios, 0x000200, bios_rd32(2753bios, 0x000200) | 0x40000000);27542755bios_wr32(bios, 0x614100, 0x00800018);2756bios_wr32(bios, 0x614900, 0x00800018);27572758mdelay(10);27592760bios_wr32(bios, 0x614100, 0x10000018);2761bios_wr32(bios, 0x614900, 0x10000018);27622763for (i = 0; i < 3; i++)2764bios_wr32(bios, 0x614280 + (i*0x800), bios_rd32(2765bios, 0x614280 + (i*0x800)) & 0xf0f0f0f0);27662767for (i = 0; i < 2; i++)2768bios_wr32(bios, 0x614300 + (i*0x800), bios_rd32(2769bios, 0x614300 + (i*0x800)) & 0xfffff0f0);27702771for (i = 0; i < 3; i++)2772bios_wr32(bios, 0x614380 + (i*0x800), bios_rd32(2773bios, 0x614380 + (i*0x800)) & 0xfffff0f0);27742775for (i = 0; i < 2; i++)2776bios_wr32(bios, 0x614200 + (i*0x800), bios_rd32(2777bios, 0x614200 + (i*0x800)) & 0xfffffff0);27782779for (i = 0; i < 2; i++)2780bios_wr32(bios, 0x614108 + (i*0x800), bios_rd32(2781bios, 0x614108 + (i*0x800)) & 0x0fffffff);2782return 5;2783}27842785bios_port_wr(bios, crtcport, (bios_port_rd(bios, crtcport) & mask) |2786data);2787return 5;2788}27892790static int2791init_sub(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)2792{2793/*2794* INIT_SUB opcode: 0x6B ('k')2795*2796* offset (8 bit): opcode2797* offset + 1 (8 bit): script number2798*2799* Execute script number "script number", as a subroutine2800*/28012802uint8_t sub = bios->data[offset + 1];28032804if (!iexec->execute)2805return 2;28062807BIOSLOG(bios, "0x%04X: Calling script %d\n", offset, sub);28082809parse_init_table(bios,2810ROM16(bios->data[bios->init_script_tbls_ptr + sub * 2]),2811iexec);28122813BIOSLOG(bios, "0x%04X: End of script %d\n", offset, sub);28142815return 2;2816}28172818static int2819init_ram_condition(struct nvbios *bios, uint16_t offset,2820struct init_exec *iexec)2821{2822/*2823* INIT_RAM_CONDITION opcode: 0x6D ('m')2824*2825* offset (8 bit): opcode2826* offset + 1 (8 bit): mask2827* offset + 2 (8 bit): cmpval2828*2829* Test if (NV04_PFB_BOOT_0 & "mask") equals "cmpval".2830* If condition not met skip subsequent opcodes until condition is2831* inverted (INIT_NOT), or we hit INIT_RESUME2832*/28332834uint8_t mask = bios->data[offset + 1];2835uint8_t cmpval = bios->data[offset + 2];2836uint8_t data;28372838if (!iexec->execute)2839return 3;28402841data = bios_rd32(bios, NV04_PFB_BOOT_0) & mask;28422843BIOSLOG(bios, "0x%04X: Checking if 0x%08X equals 0x%08X\n",2844offset, data, cmpval);28452846if (data == cmpval)2847BIOSLOG(bios, "0x%04X: Condition fulfilled -- continuing to execute\n", offset);2848else {2849BIOSLOG(bios, "0x%04X: Condition not fulfilled -- skipping following commands\n", offset);2850iexec->execute = false;2851}28522853return 3;2854}28552856static int2857init_nv_reg(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)2858{2859/*2860* INIT_NV_REG opcode: 0x6E ('n')2861*2862* offset (8 bit): opcode2863* offset + 1 (32 bit): register2864* offset + 5 (32 bit): mask2865* offset + 9 (32 bit): data2866*2867* Assign ((REGVAL("register") & "mask") | "data") to "register"2868*/28692870uint32_t reg = ROM32(bios->data[offset + 1]);2871uint32_t mask = ROM32(bios->data[offset + 5]);2872uint32_t data = ROM32(bios->data[offset + 9]);28732874if (!iexec->execute)2875return 13;28762877BIOSLOG(bios, "0x%04X: Reg: 0x%08X, Mask: 0x%08X, Data: 0x%08X\n",2878offset, reg, mask, data);28792880bios_wr32(bios, reg, (bios_rd32(bios, reg) & mask) | data);28812882return 13;2883}28842885static int2886init_macro(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)2887{2888/*2889* INIT_MACRO opcode: 0x6F ('o')2890*2891* offset (8 bit): opcode2892* offset + 1 (8 bit): macro number2893*2894* Look up macro index "macro number" in the macro index table.2895* The macro index table entry has 1 byte for the index in the macro2896* table, and 1 byte for the number of times to repeat the macro.2897* The macro table entry has 4 bytes for the register address and2898* 4 bytes for the value to write to that register2899*/29002901uint8_t macro_index_tbl_idx = bios->data[offset + 1];2902uint16_t tmp = bios->macro_index_tbl_ptr + (macro_index_tbl_idx * MACRO_INDEX_SIZE);2903uint8_t macro_tbl_idx = bios->data[tmp];2904uint8_t count = bios->data[tmp + 1];2905uint32_t reg, data;2906int i;29072908if (!iexec->execute)2909return 2;29102911BIOSLOG(bios, "0x%04X: Macro: 0x%02X, MacroTableIndex: 0x%02X, "2912"Count: 0x%02X\n",2913offset, macro_index_tbl_idx, macro_tbl_idx, count);29142915for (i = 0; i < count; i++) {2916uint16_t macroentryptr = bios->macro_tbl_ptr + (macro_tbl_idx + i) * MACRO_SIZE;29172918reg = ROM32(bios->data[macroentryptr]);2919data = ROM32(bios->data[macroentryptr + 4]);29202921bios_wr32(bios, reg, data);2922}29232924return 2;2925}29262927static int2928init_done(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)2929{2930/*2931* INIT_DONE opcode: 0x71 ('q')2932*2933* offset (8 bit): opcode2934*2935* End the current script2936*/29372938/* mild retval abuse to stop parsing this table */2939return 0;2940}29412942static int2943init_resume(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)2944{2945/*2946* INIT_RESUME opcode: 0x72 ('r')2947*2948* offset (8 bit): opcode2949*2950* End the current execute / no-execute condition2951*/29522953if (iexec->execute)2954return 1;29552956iexec->execute = true;2957BIOSLOG(bios, "0x%04X: ---- Executing following commands ----\n", offset);29582959return 1;2960}29612962static int2963init_time(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)2964{2965/*2966* INIT_TIME opcode: 0x74 ('t')2967*2968* offset (8 bit): opcode2969* offset + 1 (16 bit): time2970*2971* Sleep for "time" microseconds.2972*/29732974unsigned time = ROM16(bios->data[offset + 1]);29752976if (!iexec->execute)2977return 3;29782979BIOSLOG(bios, "0x%04X: Sleeping for 0x%04X microseconds\n",2980offset, time);29812982if (time < 1000)2983udelay(time);2984else2985mdelay((time + 900) / 1000);29862987return 3;2988}29892990static int2991init_condition(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)2992{2993/*2994* INIT_CONDITION opcode: 0x75 ('u')2995*2996* offset (8 bit): opcode2997* offset + 1 (8 bit): condition number2998*2999* Check condition "condition number" in the condition table.3000* If condition not met skip subsequent opcodes until condition is3001* inverted (INIT_NOT), or we hit INIT_RESUME3002*/30033004uint8_t cond = bios->data[offset + 1];30053006if (!iexec->execute)3007return 2;30083009BIOSLOG(bios, "0x%04X: Condition: 0x%02X\n", offset, cond);30103011if (bios_condition_met(bios, offset, cond))3012BIOSLOG(bios, "0x%04X: Condition fulfilled -- continuing to execute\n", offset);3013else {3014BIOSLOG(bios, "0x%04X: Condition not fulfilled -- skipping following commands\n", offset);3015iexec->execute = false;3016}30173018return 2;3019}30203021static int3022init_io_condition(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)3023{3024/*3025* INIT_IO_CONDITION opcode: 0x763026*3027* offset (8 bit): opcode3028* offset + 1 (8 bit): condition number3029*3030* Check condition "condition number" in the io condition table.3031* If condition not met skip subsequent opcodes until condition is3032* inverted (INIT_NOT), or we hit INIT_RESUME3033*/30343035uint8_t cond = bios->data[offset + 1];30363037if (!iexec->execute)3038return 2;30393040BIOSLOG(bios, "0x%04X: IO condition: 0x%02X\n", offset, cond);30413042if (io_condition_met(bios, offset, cond))3043BIOSLOG(bios, "0x%04X: Condition fulfilled -- continuing to execute\n", offset);3044else {3045BIOSLOG(bios, "0x%04X: Condition not fulfilled -- skipping following commands\n", offset);3046iexec->execute = false;3047}30483049return 2;3050}30513052static int3053init_index_io(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)3054{3055/*3056* INIT_INDEX_IO opcode: 0x78 ('x')3057*3058* offset (8 bit): opcode3059* offset + 1 (16 bit): CRTC port3060* offset + 3 (8 bit): CRTC index3061* offset + 4 (8 bit): mask3062* offset + 5 (8 bit): data3063*3064* Read value at index "CRTC index" on "CRTC port", AND with "mask",3065* OR with "data", write-back3066*/30673068uint16_t crtcport = ROM16(bios->data[offset + 1]);3069uint8_t crtcindex = bios->data[offset + 3];3070uint8_t mask = bios->data[offset + 4];3071uint8_t data = bios->data[offset + 5];3072uint8_t value;30733074if (!iexec->execute)3075return 6;30763077BIOSLOG(bios, "0x%04X: Port: 0x%04X, Index: 0x%02X, Mask: 0x%02X, "3078"Data: 0x%02X\n",3079offset, crtcport, crtcindex, mask, data);30803081value = (bios_idxprt_rd(bios, crtcport, crtcindex) & mask) | data;3082bios_idxprt_wr(bios, crtcport, crtcindex, value);30833084return 6;3085}30863087static int3088init_pll(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)3089{3090/*3091* INIT_PLL opcode: 0x79 ('y')3092*3093* offset (8 bit): opcode3094* offset + 1 (32 bit): register3095* offset + 5 (16 bit): freq3096*3097* Set PLL register "register" to coefficients for frequency (10kHz)3098* "freq"3099*/31003101uint32_t reg = ROM32(bios->data[offset + 1]);3102uint16_t freq = ROM16(bios->data[offset + 5]);31033104if (!iexec->execute)3105return 7;31063107BIOSLOG(bios, "0x%04X: Reg: 0x%08X, Freq: %d0kHz\n", offset, reg, freq);31083109setPLL(bios, reg, freq * 10);31103111return 7;3112}31133114static int3115init_zm_reg(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)3116{3117/*3118* INIT_ZM_REG opcode: 0x7A ('z')3119*3120* offset (8 bit): opcode3121* offset + 1 (32 bit): register3122* offset + 5 (32 bit): value3123*3124* Assign "value" to "register"3125*/31263127uint32_t reg = ROM32(bios->data[offset + 1]);3128uint32_t value = ROM32(bios->data[offset + 5]);31293130if (!iexec->execute)3131return 9;31323133if (reg == 0x000200)3134value |= 1;31353136bios_wr32(bios, reg, value);31373138return 9;3139}31403141static int3142init_ram_restrict_pll(struct nvbios *bios, uint16_t offset,3143struct init_exec *iexec)3144{3145/*3146* INIT_RAM_RESTRICT_PLL opcode: 0x87 ('')3147*3148* offset (8 bit): opcode3149* offset + 1 (8 bit): PLL type3150* offset + 2 (32 bit): frequency 03151*3152* Uses the RAMCFG strap of PEXTDEV_BOOT as an index into the table at3153* ram_restrict_table_ptr. The value read from there is used to select3154* a frequency from the table starting at 'frequency 0' to be3155* programmed into the PLL corresponding to 'type'.3156*3157* The PLL limits table on cards using this opcode has a mapping of3158* 'type' to the relevant registers.3159*/31603161struct drm_device *dev = bios->dev;3162uint32_t strap = (bios_rd32(bios, NV_PEXTDEV_BOOT_0) & 0x0000003c) >> 2;3163uint8_t index = bios->data[bios->ram_restrict_tbl_ptr + strap];3164uint8_t type = bios->data[offset + 1];3165uint32_t freq = ROM32(bios->data[offset + 2 + (index * 4)]);3166uint8_t *pll_limits = &bios->data[bios->pll_limit_tbl_ptr], *entry;3167int len = 2 + bios->ram_restrict_group_count * 4;3168int i;31693170if (!iexec->execute)3171return len;31723173if (!bios->pll_limit_tbl_ptr || (pll_limits[0] & 0xf0) != 0x30) {3174NV_ERROR(dev, "PLL limits table not version 3.x\n");3175return len; /* deliberate, allow default clocks to remain */3176}31773178entry = pll_limits + pll_limits[1];3179for (i = 0; i < pll_limits[3]; i++, entry += pll_limits[2]) {3180if (entry[0] == type) {3181uint32_t reg = ROM32(entry[3]);31823183BIOSLOG(bios, "0x%04X: "3184"Type %02x Reg 0x%08x Freq %dKHz\n",3185offset, type, reg, freq);31863187setPLL(bios, reg, freq);3188return len;3189}3190}31913192NV_ERROR(dev, "PLL type 0x%02x not found in PLL limits table", type);3193return len;3194}31953196static int3197init_8c(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)3198{3199/*3200* INIT_8C opcode: 0x8C ('')3201*3202* NOP so far....3203*3204*/32053206return 1;3207}32083209static int3210init_8d(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)3211{3212/*3213* INIT_8D opcode: 0x8D ('')3214*3215* NOP so far....3216*3217*/32183219return 1;3220}32213222static int3223init_gpio(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)3224{3225/*3226* INIT_GPIO opcode: 0x8E ('')3227*3228* offset (8 bit): opcode3229*3230* Loop over all entries in the DCB GPIO table, and initialise3231* each GPIO according to various values listed in each entry3232*/32333234struct drm_nouveau_private *dev_priv = bios->dev->dev_private;3235struct nouveau_gpio_engine *pgpio = &dev_priv->engine.gpio;3236const uint32_t nv50_gpio_ctl[2] = { 0xe100, 0xe28c };3237int i;32383239if (dev_priv->card_type < NV_50) {3240NV_ERROR(bios->dev, "INIT_GPIO on unsupported chipset\n");3241return 1;3242}32433244if (!iexec->execute)3245return 1;32463247for (i = 0; i < bios->dcb.gpio.entries; i++) {3248struct dcb_gpio_entry *gpio = &bios->dcb.gpio.entry[i];3249uint32_t r, s, v;32503251BIOSLOG(bios, "0x%04X: Entry: 0x%08X\n", offset, gpio->entry);32523253BIOSLOG(bios, "0x%04X: set gpio 0x%02x, state %d\n",3254offset, gpio->tag, gpio->state_default);3255if (bios->execute)3256pgpio->set(bios->dev, gpio->tag, gpio->state_default);32573258/* The NVIDIA binary driver doesn't appear to actually do3259* any of this, my VBIOS does however.3260*/3261/* Not a clue, needs de-magicing */3262r = nv50_gpio_ctl[gpio->line >> 4];3263s = (gpio->line & 0x0f);3264v = bios_rd32(bios, r) & ~(0x00010001 << s);3265switch ((gpio->entry & 0x06000000) >> 25) {3266case 1:3267v |= (0x00000001 << s);3268break;3269case 2:3270v |= (0x00010000 << s);3271break;3272default:3273break;3274}3275bios_wr32(bios, r, v);3276}32773278return 1;3279}32803281static int3282init_ram_restrict_zm_reg_group(struct nvbios *bios, uint16_t offset,3283struct init_exec *iexec)3284{3285/*3286* INIT_RAM_RESTRICT_ZM_REG_GROUP opcode: 0x8F ('')3287*3288* offset (8 bit): opcode3289* offset + 1 (32 bit): reg3290* offset + 5 (8 bit): regincrement3291* offset + 6 (8 bit): count3292* offset + 7 (32 bit): value 1,13293* ...3294*3295* Use the RAMCFG strap of PEXTDEV_BOOT as an index into the table at3296* ram_restrict_table_ptr. The value read from here is 'n', and3297* "value 1,n" gets written to "reg". This repeats "count" times and on3298* each iteration 'm', "reg" increases by "regincrement" and3299* "value m,n" is used. The extent of n is limited by a number read3300* from the 'M' BIT table, herein called "blocklen"3301*/33023303uint32_t reg = ROM32(bios->data[offset + 1]);3304uint8_t regincrement = bios->data[offset + 5];3305uint8_t count = bios->data[offset + 6];3306uint32_t strap_ramcfg, data;3307/* previously set by 'M' BIT table */3308uint16_t blocklen = bios->ram_restrict_group_count * 4;3309int len = 7 + count * blocklen;3310uint8_t index;3311int i;33123313/* critical! to know the length of the opcode */;3314if (!blocklen) {3315NV_ERROR(bios->dev,3316"0x%04X: Zero block length - has the M table "3317"been parsed?\n", offset);3318return -EINVAL;3319}33203321if (!iexec->execute)3322return len;33233324strap_ramcfg = (bios_rd32(bios, NV_PEXTDEV_BOOT_0) >> 2) & 0xf;3325index = bios->data[bios->ram_restrict_tbl_ptr + strap_ramcfg];33263327BIOSLOG(bios, "0x%04X: Reg: 0x%08X, RegIncrement: 0x%02X, "3328"Count: 0x%02X, StrapRamCfg: 0x%02X, Index: 0x%02X\n",3329offset, reg, regincrement, count, strap_ramcfg, index);33303331for (i = 0; i < count; i++) {3332data = ROM32(bios->data[offset + 7 + index * 4 + blocklen * i]);33333334bios_wr32(bios, reg, data);33353336reg += regincrement;3337}33383339return len;3340}33413342static int3343init_copy_zm_reg(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)3344{3345/*3346* INIT_COPY_ZM_REG opcode: 0x90 ('')3347*3348* offset (8 bit): opcode3349* offset + 1 (32 bit): src reg3350* offset + 5 (32 bit): dst reg3351*3352* Put contents of "src reg" into "dst reg"3353*/33543355uint32_t srcreg = ROM32(bios->data[offset + 1]);3356uint32_t dstreg = ROM32(bios->data[offset + 5]);33573358if (!iexec->execute)3359return 9;33603361bios_wr32(bios, dstreg, bios_rd32(bios, srcreg));33623363return 9;3364}33653366static int3367init_zm_reg_group_addr_latched(struct nvbios *bios, uint16_t offset,3368struct init_exec *iexec)3369{3370/*3371* INIT_ZM_REG_GROUP_ADDRESS_LATCHED opcode: 0x91 ('')3372*3373* offset (8 bit): opcode3374* offset + 1 (32 bit): dst reg3375* offset + 5 (8 bit): count3376* offset + 6 (32 bit): data 13377* ...3378*3379* For each of "count" values write "data n" to "dst reg"3380*/33813382uint32_t reg = ROM32(bios->data[offset + 1]);3383uint8_t count = bios->data[offset + 5];3384int len = 6 + count * 4;3385int i;33863387if (!iexec->execute)3388return len;33893390for (i = 0; i < count; i++) {3391uint32_t data = ROM32(bios->data[offset + 6 + 4 * i]);3392bios_wr32(bios, reg, data);3393}33943395return len;3396}33973398static int3399init_reserved(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)3400{3401/*3402* INIT_RESERVED opcode: 0x92 ('')3403*3404* offset (8 bit): opcode3405*3406* Seemingly does nothing3407*/34083409return 1;3410}34113412static int3413init_96(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)3414{3415/*3416* INIT_96 opcode: 0x96 ('')3417*3418* offset (8 bit): opcode3419* offset + 1 (32 bit): sreg3420* offset + 5 (8 bit): sshift3421* offset + 6 (8 bit): smask3422* offset + 7 (8 bit): index3423* offset + 8 (32 bit): reg3424* offset + 12 (32 bit): mask3425* offset + 16 (8 bit): shift3426*3427*/34283429uint16_t xlatptr = bios->init96_tbl_ptr + (bios->data[offset + 7] * 2);3430uint32_t reg = ROM32(bios->data[offset + 8]);3431uint32_t mask = ROM32(bios->data[offset + 12]);3432uint32_t val;34333434val = bios_rd32(bios, ROM32(bios->data[offset + 1]));3435if (bios->data[offset + 5] < 0x80)3436val >>= bios->data[offset + 5];3437else3438val <<= (0x100 - bios->data[offset + 5]);3439val &= bios->data[offset + 6];34403441val = bios->data[ROM16(bios->data[xlatptr]) + val];3442val <<= bios->data[offset + 16];34433444if (!iexec->execute)3445return 17;34463447bios_wr32(bios, reg, (bios_rd32(bios, reg) & mask) | val);3448return 17;3449}34503451static int3452init_97(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)3453{3454/*3455* INIT_97 opcode: 0x97 ('')3456*3457* offset (8 bit): opcode3458* offset + 1 (32 bit): register3459* offset + 5 (32 bit): mask3460* offset + 9 (32 bit): value3461*3462* Adds "value" to "register" preserving the fields specified3463* by "mask"3464*/34653466uint32_t reg = ROM32(bios->data[offset + 1]);3467uint32_t mask = ROM32(bios->data[offset + 5]);3468uint32_t add = ROM32(bios->data[offset + 9]);3469uint32_t val;34703471val = bios_rd32(bios, reg);3472val = (val & mask) | ((val + add) & ~mask);34733474if (!iexec->execute)3475return 13;34763477bios_wr32(bios, reg, val);3478return 13;3479}34803481static int3482init_auxch(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)3483{3484/*3485* INIT_AUXCH opcode: 0x98 ('')3486*3487* offset (8 bit): opcode3488* offset + 1 (32 bit): address3489* offset + 5 (8 bit): count3490* offset + 6 (8 bit): mask 03491* offset + 7 (8 bit): data 03492* ...3493*3494*/34953496struct drm_device *dev = bios->dev;3497struct nouveau_i2c_chan *auxch;3498uint32_t addr = ROM32(bios->data[offset + 1]);3499uint8_t count = bios->data[offset + 5];3500int len = 6 + count * 2;3501int ret, i;35023503if (!bios->display.output) {3504NV_ERROR(dev, "INIT_AUXCH: no active output\n");3505return len;3506}35073508auxch = init_i2c_device_find(dev, bios->display.output->i2c_index);3509if (!auxch) {3510NV_ERROR(dev, "INIT_AUXCH: couldn't get auxch %d\n",3511bios->display.output->i2c_index);3512return len;3513}35143515if (!iexec->execute)3516return len;35173518offset += 6;3519for (i = 0; i < count; i++, offset += 2) {3520uint8_t data;35213522ret = nouveau_dp_auxch(auxch, 9, addr, &data, 1);3523if (ret) {3524NV_ERROR(dev, "INIT_AUXCH: rd auxch fail %d\n", ret);3525return len;3526}35273528data &= bios->data[offset + 0];3529data |= bios->data[offset + 1];35303531ret = nouveau_dp_auxch(auxch, 8, addr, &data, 1);3532if (ret) {3533NV_ERROR(dev, "INIT_AUXCH: wr auxch fail %d\n", ret);3534return len;3535}3536}35373538return len;3539}35403541static int3542init_zm_auxch(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)3543{3544/*3545* INIT_ZM_AUXCH opcode: 0x99 ('')3546*3547* offset (8 bit): opcode3548* offset + 1 (32 bit): address3549* offset + 5 (8 bit): count3550* offset + 6 (8 bit): data 03551* ...3552*3553*/35543555struct drm_device *dev = bios->dev;3556struct nouveau_i2c_chan *auxch;3557uint32_t addr = ROM32(bios->data[offset + 1]);3558uint8_t count = bios->data[offset + 5];3559int len = 6 + count;3560int ret, i;35613562if (!bios->display.output) {3563NV_ERROR(dev, "INIT_ZM_AUXCH: no active output\n");3564return len;3565}35663567auxch = init_i2c_device_find(dev, bios->display.output->i2c_index);3568if (!auxch) {3569NV_ERROR(dev, "INIT_ZM_AUXCH: couldn't get auxch %d\n",3570bios->display.output->i2c_index);3571return len;3572}35733574if (!iexec->execute)3575return len;35763577offset += 6;3578for (i = 0; i < count; i++, offset++) {3579ret = nouveau_dp_auxch(auxch, 8, addr, &bios->data[offset], 1);3580if (ret) {3581NV_ERROR(dev, "INIT_ZM_AUXCH: wr auxch fail %d\n", ret);3582return len;3583}3584}35853586return len;3587}35883589static int3590init_i2c_long_if(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)3591{3592/*3593* INIT_I2C_LONG_IF opcode: 0x9A ('')3594*3595* offset (8 bit): opcode3596* offset + 1 (8 bit): DCB I2C table entry index3597* offset + 2 (8 bit): I2C slave address3598* offset + 3 (16 bit): I2C register3599* offset + 5 (8 bit): mask3600* offset + 6 (8 bit): data3601*3602* Read the register given by "I2C register" on the device addressed3603* by "I2C slave address" on the I2C bus given by "DCB I2C table3604* entry index". Compare the result AND "mask" to "data".3605* If they're not equal, skip subsequent opcodes until condition is3606* inverted (INIT_NOT), or we hit INIT_RESUME3607*/36083609uint8_t i2c_index = bios->data[offset + 1];3610uint8_t i2c_address = bios->data[offset + 2] >> 1;3611uint8_t reglo = bios->data[offset + 3];3612uint8_t reghi = bios->data[offset + 4];3613uint8_t mask = bios->data[offset + 5];3614uint8_t data = bios->data[offset + 6];3615struct nouveau_i2c_chan *chan;3616uint8_t buf0[2] = { reghi, reglo };3617uint8_t buf1[1];3618struct i2c_msg msg[2] = {3619{ i2c_address, 0, 1, buf0 },3620{ i2c_address, I2C_M_RD, 1, buf1 },3621};3622int ret;36233624/* no execute check by design */36253626BIOSLOG(bios, "0x%04X: DCBI2CIndex: 0x%02X, I2CAddress: 0x%02X\n",3627offset, i2c_index, i2c_address);36283629chan = init_i2c_device_find(bios->dev, i2c_index);3630if (!chan)3631return -ENODEV;363236333634ret = i2c_transfer(&chan->adapter, msg, 2);3635if (ret < 0) {3636BIOSLOG(bios, "0x%04X: I2CReg: 0x%02X:0x%02X, Value: [no device], "3637"Mask: 0x%02X, Data: 0x%02X\n",3638offset, reghi, reglo, mask, data);3639iexec->execute = 0;3640return 7;3641}36423643BIOSLOG(bios, "0x%04X: I2CReg: 0x%02X:0x%02X, Value: 0x%02X, "3644"Mask: 0x%02X, Data: 0x%02X\n",3645offset, reghi, reglo, buf1[0], mask, data);36463647iexec->execute = ((buf1[0] & mask) == data);36483649return 7;3650}36513652static struct init_tbl_entry itbl_entry[] = {3653/* command name , id , length , offset , mult , command handler */3654/* INIT_PROG (0x31, 15, 10, 4) removed due to no example of use */3655{ "INIT_IO_RESTRICT_PROG" , 0x32, init_io_restrict_prog },3656{ "INIT_REPEAT" , 0x33, init_repeat },3657{ "INIT_IO_RESTRICT_PLL" , 0x34, init_io_restrict_pll },3658{ "INIT_END_REPEAT" , 0x36, init_end_repeat },3659{ "INIT_COPY" , 0x37, init_copy },3660{ "INIT_NOT" , 0x38, init_not },3661{ "INIT_IO_FLAG_CONDITION" , 0x39, init_io_flag_condition },3662{ "INIT_DP_CONDITION" , 0x3A, init_dp_condition },3663{ "INIT_OP_3B" , 0x3B, init_op_3b },3664{ "INIT_OP_3C" , 0x3C, init_op_3c },3665{ "INIT_INDEX_ADDRESS_LATCHED" , 0x49, init_idx_addr_latched },3666{ "INIT_IO_RESTRICT_PLL2" , 0x4A, init_io_restrict_pll2 },3667{ "INIT_PLL2" , 0x4B, init_pll2 },3668{ "INIT_I2C_BYTE" , 0x4C, init_i2c_byte },3669{ "INIT_ZM_I2C_BYTE" , 0x4D, init_zm_i2c_byte },3670{ "INIT_ZM_I2C" , 0x4E, init_zm_i2c },3671{ "INIT_TMDS" , 0x4F, init_tmds },3672{ "INIT_ZM_TMDS_GROUP" , 0x50, init_zm_tmds_group },3673{ "INIT_CR_INDEX_ADDRESS_LATCHED" , 0x51, init_cr_idx_adr_latch },3674{ "INIT_CR" , 0x52, init_cr },3675{ "INIT_ZM_CR" , 0x53, init_zm_cr },3676{ "INIT_ZM_CR_GROUP" , 0x54, init_zm_cr_group },3677{ "INIT_CONDITION_TIME" , 0x56, init_condition_time },3678{ "INIT_LTIME" , 0x57, init_ltime },3679{ "INIT_ZM_REG_SEQUENCE" , 0x58, init_zm_reg_sequence },3680/* INIT_INDIRECT_REG (0x5A, 7, 0, 0) removed due to no example of use */3681{ "INIT_SUB_DIRECT" , 0x5B, init_sub_direct },3682{ "INIT_JUMP" , 0x5C, init_jump },3683{ "INIT_I2C_IF" , 0x5E, init_i2c_if },3684{ "INIT_COPY_NV_REG" , 0x5F, init_copy_nv_reg },3685{ "INIT_ZM_INDEX_IO" , 0x62, init_zm_index_io },3686{ "INIT_COMPUTE_MEM" , 0x63, init_compute_mem },3687{ "INIT_RESET" , 0x65, init_reset },3688{ "INIT_CONFIGURE_MEM" , 0x66, init_configure_mem },3689{ "INIT_CONFIGURE_CLK" , 0x67, init_configure_clk },3690{ "INIT_CONFIGURE_PREINIT" , 0x68, init_configure_preinit },3691{ "INIT_IO" , 0x69, init_io },3692{ "INIT_SUB" , 0x6B, init_sub },3693{ "INIT_RAM_CONDITION" , 0x6D, init_ram_condition },3694{ "INIT_NV_REG" , 0x6E, init_nv_reg },3695{ "INIT_MACRO" , 0x6F, init_macro },3696{ "INIT_DONE" , 0x71, init_done },3697{ "INIT_RESUME" , 0x72, init_resume },3698/* INIT_RAM_CONDITION2 (0x73, 9, 0, 0) removed due to no example of use */3699{ "INIT_TIME" , 0x74, init_time },3700{ "INIT_CONDITION" , 0x75, init_condition },3701{ "INIT_IO_CONDITION" , 0x76, init_io_condition },3702{ "INIT_INDEX_IO" , 0x78, init_index_io },3703{ "INIT_PLL" , 0x79, init_pll },3704{ "INIT_ZM_REG" , 0x7A, init_zm_reg },3705{ "INIT_RAM_RESTRICT_PLL" , 0x87, init_ram_restrict_pll },3706{ "INIT_8C" , 0x8C, init_8c },3707{ "INIT_8D" , 0x8D, init_8d },3708{ "INIT_GPIO" , 0x8E, init_gpio },3709{ "INIT_RAM_RESTRICT_ZM_REG_GROUP" , 0x8F, init_ram_restrict_zm_reg_group },3710{ "INIT_COPY_ZM_REG" , 0x90, init_copy_zm_reg },3711{ "INIT_ZM_REG_GROUP_ADDRESS_LATCHED" , 0x91, init_zm_reg_group_addr_latched },3712{ "INIT_RESERVED" , 0x92, init_reserved },3713{ "INIT_96" , 0x96, init_96 },3714{ "INIT_97" , 0x97, init_97 },3715{ "INIT_AUXCH" , 0x98, init_auxch },3716{ "INIT_ZM_AUXCH" , 0x99, init_zm_auxch },3717{ "INIT_I2C_LONG_IF" , 0x9A, init_i2c_long_if },3718{ NULL , 0 , NULL }3719};37203721#define MAX_TABLE_OPS 100037223723static int3724parse_init_table(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)3725{3726/*3727* Parses all commands in an init table.3728*3729* We start out executing all commands found in the init table. Some3730* opcodes may change the status of iexec->execute to SKIP, which will3731* cause the following opcodes to perform no operation until the value3732* is changed back to EXECUTE.3733*/37343735int count = 0, i, ret;3736uint8_t id;37373738/*3739* Loop until INIT_DONE causes us to break out of the loop3740* (or until offset > bios length just in case... )3741* (and no more than MAX_TABLE_OPS iterations, just in case... )3742*/3743while ((offset < bios->length) && (count++ < MAX_TABLE_OPS)) {3744id = bios->data[offset];37453746/* Find matching id in itbl_entry */3747for (i = 0; itbl_entry[i].name && (itbl_entry[i].id != id); i++)3748;37493750if (!itbl_entry[i].name) {3751NV_ERROR(bios->dev,3752"0x%04X: Init table command not found: "3753"0x%02X\n", offset, id);3754return -ENOENT;3755}37563757BIOSLOG(bios, "0x%04X: [ (0x%02X) - %s ]\n", offset,3758itbl_entry[i].id, itbl_entry[i].name);37593760/* execute eventual command handler */3761ret = (*itbl_entry[i].handler)(bios, offset, iexec);3762if (ret < 0) {3763NV_ERROR(bios->dev, "0x%04X: Failed parsing init "3764"table opcode: %s %d\n", offset,3765itbl_entry[i].name, ret);3766}37673768if (ret <= 0)3769break;37703771/*3772* Add the offset of the current command including all data3773* of that command. The offset will then be pointing on the3774* next op code.3775*/3776offset += ret;3777}37783779if (offset >= bios->length)3780NV_WARN(bios->dev,3781"Offset 0x%04X greater than known bios image length. "3782"Corrupt image?\n", offset);3783if (count >= MAX_TABLE_OPS)3784NV_WARN(bios->dev,3785"More than %d opcodes to a table is unlikely, "3786"is the bios image corrupt?\n", MAX_TABLE_OPS);37873788return 0;3789}37903791static void3792parse_init_tables(struct nvbios *bios)3793{3794/* Loops and calls parse_init_table() for each present table. */37953796int i = 0;3797uint16_t table;3798struct init_exec iexec = {true, false};37993800if (bios->old_style_init) {3801if (bios->init_script_tbls_ptr)3802parse_init_table(bios, bios->init_script_tbls_ptr, &iexec);3803if (bios->extra_init_script_tbl_ptr)3804parse_init_table(bios, bios->extra_init_script_tbl_ptr, &iexec);38053806return;3807}38083809while ((table = ROM16(bios->data[bios->init_script_tbls_ptr + i]))) {3810NV_INFO(bios->dev,3811"Parsing VBIOS init table %d at offset 0x%04X\n",3812i / 2, table);3813BIOSLOG(bios, "0x%04X: ------ Executing following commands ------\n", table);38143815parse_init_table(bios, table, &iexec);3816i += 2;3817}3818}38193820static uint16_t clkcmptable(struct nvbios *bios, uint16_t clktable, int pxclk)3821{3822int compare_record_len, i = 0;3823uint16_t compareclk, scriptptr = 0;38243825if (bios->major_version < 5) /* pre BIT */3826compare_record_len = 3;3827else3828compare_record_len = 4;38293830do {3831compareclk = ROM16(bios->data[clktable + compare_record_len * i]);3832if (pxclk >= compareclk * 10) {3833if (bios->major_version < 5) {3834uint8_t tmdssub = bios->data[clktable + 2 + compare_record_len * i];3835scriptptr = ROM16(bios->data[bios->init_script_tbls_ptr + tmdssub * 2]);3836} else3837scriptptr = ROM16(bios->data[clktable + 2 + compare_record_len * i]);3838break;3839}3840i++;3841} while (compareclk);38423843return scriptptr;3844}38453846static void3847run_digital_op_script(struct drm_device *dev, uint16_t scriptptr,3848struct dcb_entry *dcbent, int head, bool dl)3849{3850struct drm_nouveau_private *dev_priv = dev->dev_private;3851struct nvbios *bios = &dev_priv->vbios;3852struct init_exec iexec = {true, false};38533854NV_TRACE(dev, "0x%04X: Parsing digital output script table\n",3855scriptptr);3856bios_idxprt_wr(bios, NV_CIO_CRX__COLOR, NV_CIO_CRE_44,3857head ? NV_CIO_CRE_44_HEADB : NV_CIO_CRE_44_HEADA);3858/* note: if dcb entries have been merged, index may be misleading */3859NVWriteVgaCrtc5758(dev, head, 0, dcbent->index);3860parse_init_table(bios, scriptptr, &iexec);38613862nv04_dfp_bind_head(dev, dcbent, head, dl);3863}38643865static int call_lvds_manufacturer_script(struct drm_device *dev, struct dcb_entry *dcbent, int head, enum LVDS_script script)3866{3867struct drm_nouveau_private *dev_priv = dev->dev_private;3868struct nvbios *bios = &dev_priv->vbios;3869uint8_t sub = bios->data[bios->fp.xlated_entry + script] + (bios->fp.link_c_increment && dcbent->or & OUTPUT_C ? 1 : 0);3870uint16_t scriptofs = ROM16(bios->data[bios->init_script_tbls_ptr + sub * 2]);38713872if (!bios->fp.xlated_entry || !sub || !scriptofs)3873return -EINVAL;38743875run_digital_op_script(dev, scriptofs, dcbent, head, bios->fp.dual_link);38763877if (script == LVDS_PANEL_OFF) {3878/* off-on delay in ms */3879mdelay(ROM16(bios->data[bios->fp.xlated_entry + 7]));3880}3881#ifdef __powerpc__3882/* Powerbook specific quirks */3883if (script == LVDS_RESET &&3884(dev->pci_device == 0x0179 || dev->pci_device == 0x0189 ||3885dev->pci_device == 0x0329))3886nv_write_tmds(dev, dcbent->or, 0, 0x02, 0x72);3887#endif38883889return 0;3890}38913892static int run_lvds_table(struct drm_device *dev, struct dcb_entry *dcbent, int head, enum LVDS_script script, int pxclk)3893{3894/*3895* The BIT LVDS table's header has the information to setup the3896* necessary registers. Following the standard 4 byte header are:3897* A bitmask byte and a dual-link transition pxclk value for use in3898* selecting the init script when not using straps; 4 script pointers3899* for panel power, selected by output and on/off; and 8 table pointers3900* for panel init, the needed one determined by output, and bits in the3901* conf byte. These tables are similar to the TMDS tables, consisting3902* of a list of pxclks and script pointers.3903*/3904struct drm_nouveau_private *dev_priv = dev->dev_private;3905struct nvbios *bios = &dev_priv->vbios;3906unsigned int outputset = (dcbent->or == 4) ? 1 : 0;3907uint16_t scriptptr = 0, clktable;39083909/*3910* For now we assume version 3.0 table - g80 support will need some3911* changes3912*/39133914switch (script) {3915case LVDS_INIT:3916return -ENOSYS;3917case LVDS_BACKLIGHT_ON:3918case LVDS_PANEL_ON:3919scriptptr = ROM16(bios->data[bios->fp.lvdsmanufacturerpointer + 7 + outputset * 2]);3920break;3921case LVDS_BACKLIGHT_OFF:3922case LVDS_PANEL_OFF:3923scriptptr = ROM16(bios->data[bios->fp.lvdsmanufacturerpointer + 11 + outputset * 2]);3924break;3925case LVDS_RESET:3926clktable = bios->fp.lvdsmanufacturerpointer + 15;3927if (dcbent->or == 4)3928clktable += 8;39293930if (dcbent->lvdsconf.use_straps_for_mode) {3931if (bios->fp.dual_link)3932clktable += 4;3933if (bios->fp.if_is_24bit)3934clktable += 2;3935} else {3936/* using EDID */3937int cmpval_24bit = (dcbent->or == 4) ? 4 : 1;39383939if (bios->fp.dual_link) {3940clktable += 4;3941cmpval_24bit <<= 1;3942}39433944if (bios->fp.strapless_is_24bit & cmpval_24bit)3945clktable += 2;3946}39473948clktable = ROM16(bios->data[clktable]);3949if (!clktable) {3950NV_ERROR(dev, "Pixel clock comparison table not found\n");3951return -ENOENT;3952}3953scriptptr = clkcmptable(bios, clktable, pxclk);3954}39553956if (!scriptptr) {3957NV_ERROR(dev, "LVDS output init script not found\n");3958return -ENOENT;3959}3960run_digital_op_script(dev, scriptptr, dcbent, head, bios->fp.dual_link);39613962return 0;3963}39643965int call_lvds_script(struct drm_device *dev, struct dcb_entry *dcbent, int head, enum LVDS_script script, int pxclk)3966{3967/*3968* LVDS operations are multiplexed in an effort to present a single API3969* which works with two vastly differing underlying structures.3970* This acts as the demux3971*/39723973struct drm_nouveau_private *dev_priv = dev->dev_private;3974struct nvbios *bios = &dev_priv->vbios;3975uint8_t lvds_ver = bios->data[bios->fp.lvdsmanufacturerpointer];3976uint32_t sel_clk_binding, sel_clk;3977int ret;39783979if (bios->fp.last_script_invoc == (script << 1 | head) || !lvds_ver ||3980(lvds_ver >= 0x30 && script == LVDS_INIT))3981return 0;39823983if (!bios->fp.lvds_init_run) {3984bios->fp.lvds_init_run = true;3985call_lvds_script(dev, dcbent, head, LVDS_INIT, pxclk);3986}39873988if (script == LVDS_PANEL_ON && bios->fp.reset_after_pclk_change)3989call_lvds_script(dev, dcbent, head, LVDS_RESET, pxclk);3990if (script == LVDS_RESET && bios->fp.power_off_for_reset)3991call_lvds_script(dev, dcbent, head, LVDS_PANEL_OFF, pxclk);39923993NV_TRACE(dev, "Calling LVDS script %d:\n", script);39943995/* don't let script change pll->head binding */3996sel_clk_binding = bios_rd32(bios, NV_PRAMDAC_SEL_CLK) & 0x50000;39973998if (lvds_ver < 0x30)3999ret = call_lvds_manufacturer_script(dev, dcbent, head, script);4000else4001ret = run_lvds_table(dev, dcbent, head, script, pxclk);40024003bios->fp.last_script_invoc = (script << 1 | head);40044005sel_clk = NVReadRAMDAC(dev, 0, NV_PRAMDAC_SEL_CLK) & ~0x50000;4006NVWriteRAMDAC(dev, 0, NV_PRAMDAC_SEL_CLK, sel_clk | sel_clk_binding);4007/* some scripts set a value in NV_PBUS_POWERCTRL_2 and break video overlay */4008nvWriteMC(dev, NV_PBUS_POWERCTRL_2, 0);40094010return ret;4011}40124013struct lvdstableheader {4014uint8_t lvds_ver, headerlen, recordlen;4015};40164017static int parse_lvds_manufacturer_table_header(struct drm_device *dev, struct nvbios *bios, struct lvdstableheader *lth)4018{4019/*4020* BMP version (0xa) LVDS table has a simple header of version and4021* record length. The BIT LVDS table has the typical BIT table header:4022* version byte, header length byte, record length byte, and a byte for4023* the maximum number of records that can be held in the table.4024*/40254026uint8_t lvds_ver, headerlen, recordlen;40274028memset(lth, 0, sizeof(struct lvdstableheader));40294030if (bios->fp.lvdsmanufacturerpointer == 0x0) {4031NV_ERROR(dev, "Pointer to LVDS manufacturer table invalid\n");4032return -EINVAL;4033}40344035lvds_ver = bios->data[bios->fp.lvdsmanufacturerpointer];40364037switch (lvds_ver) {4038case 0x0a: /* pre NV40 */4039headerlen = 2;4040recordlen = bios->data[bios->fp.lvdsmanufacturerpointer + 1];4041break;4042case 0x30: /* NV4x */4043headerlen = bios->data[bios->fp.lvdsmanufacturerpointer + 1];4044if (headerlen < 0x1f) {4045NV_ERROR(dev, "LVDS table header not understood\n");4046return -EINVAL;4047}4048recordlen = bios->data[bios->fp.lvdsmanufacturerpointer + 2];4049break;4050case 0x40: /* G80/G90 */4051headerlen = bios->data[bios->fp.lvdsmanufacturerpointer + 1];4052if (headerlen < 0x7) {4053NV_ERROR(dev, "LVDS table header not understood\n");4054return -EINVAL;4055}4056recordlen = bios->data[bios->fp.lvdsmanufacturerpointer + 2];4057break;4058default:4059NV_ERROR(dev,4060"LVDS table revision %d.%d not currently supported\n",4061lvds_ver >> 4, lvds_ver & 0xf);4062return -ENOSYS;4063}40644065lth->lvds_ver = lvds_ver;4066lth->headerlen = headerlen;4067lth->recordlen = recordlen;40684069return 0;4070}40714072static int4073get_fp_strap(struct drm_device *dev, struct nvbios *bios)4074{4075struct drm_nouveau_private *dev_priv = dev->dev_private;40764077/*4078* The fp strap is normally dictated by the "User Strap" in4079* PEXTDEV_BOOT_0[20:16], but on BMP cards when bit 2 of the4080* Internal_Flags struct at 0x48 is set, the user strap gets overriden4081* by the PCI subsystem ID during POST, but not before the previous user4082* strap has been committed to CR58 for CR57=0xf on head A, which may be4083* read and used instead4084*/40854086if (bios->major_version < 5 && bios->data[0x48] & 0x4)4087return NVReadVgaCrtc5758(dev, 0, 0xf) & 0xf;40884089if (dev_priv->card_type >= NV_50)4090return (bios_rd32(bios, NV_PEXTDEV_BOOT_0) >> 24) & 0xf;4091else4092return (bios_rd32(bios, NV_PEXTDEV_BOOT_0) >> 16) & 0xf;4093}40944095static int parse_fp_mode_table(struct drm_device *dev, struct nvbios *bios)4096{4097uint8_t *fptable;4098uint8_t fptable_ver, headerlen = 0, recordlen, fpentries = 0xf, fpindex;4099int ret, ofs, fpstrapping;4100struct lvdstableheader lth;41014102if (bios->fp.fptablepointer == 0x0) {4103/* Apple cards don't have the fp table; the laptops use DDC */4104/* The table is also missing on some x86 IGPs */4105#ifndef __powerpc__4106NV_ERROR(dev, "Pointer to flat panel table invalid\n");4107#endif4108bios->digital_min_front_porch = 0x4b;4109return 0;4110}41114112fptable = &bios->data[bios->fp.fptablepointer];4113fptable_ver = fptable[0];41144115switch (fptable_ver) {4116/*4117* BMP version 0x5.0x11 BIOSen have version 1 like tables, but no4118* version field, and miss one of the spread spectrum/PWM bytes.4119* This could affect early GF2Go parts (not seen any appropriate ROMs4120* though). Here we assume that a version of 0x05 matches this case4121* (combining with a BMP version check would be better), as the4122* common case for the panel type field is 0x0005, and that is in4123* fact what we are reading the first byte of.4124*/4125case 0x05: /* some NV10, 11, 15, 16 */4126recordlen = 42;4127ofs = -1;4128break;4129case 0x10: /* some NV15/16, and NV11+ */4130recordlen = 44;4131ofs = 0;4132break;4133case 0x20: /* NV40+ */4134headerlen = fptable[1];4135recordlen = fptable[2];4136fpentries = fptable[3];4137/*4138* fptable[4] is the minimum4139* RAMDAC_FP_HCRTC -> RAMDAC_FP_HSYNC_START gap4140*/4141bios->digital_min_front_porch = fptable[4];4142ofs = -7;4143break;4144default:4145NV_ERROR(dev,4146"FP table revision %d.%d not currently supported\n",4147fptable_ver >> 4, fptable_ver & 0xf);4148return -ENOSYS;4149}41504151if (!bios->is_mobile) /* !mobile only needs digital_min_front_porch */4152return 0;41534154ret = parse_lvds_manufacturer_table_header(dev, bios, <h);4155if (ret)4156return ret;41574158if (lth.lvds_ver == 0x30 || lth.lvds_ver == 0x40) {4159bios->fp.fpxlatetableptr = bios->fp.lvdsmanufacturerpointer +4160lth.headerlen + 1;4161bios->fp.xlatwidth = lth.recordlen;4162}4163if (bios->fp.fpxlatetableptr == 0x0) {4164NV_ERROR(dev, "Pointer to flat panel xlat table invalid\n");4165return -EINVAL;4166}41674168fpstrapping = get_fp_strap(dev, bios);41694170fpindex = bios->data[bios->fp.fpxlatetableptr +4171fpstrapping * bios->fp.xlatwidth];41724173if (fpindex > fpentries) {4174NV_ERROR(dev, "Bad flat panel table index\n");4175return -ENOENT;4176}41774178/* nv4x cards need both a strap value and fpindex of 0xf to use DDC */4179if (lth.lvds_ver > 0x10)4180bios->fp_no_ddc = fpstrapping != 0xf || fpindex != 0xf;41814182/*4183* If either the strap or xlated fpindex value are 0xf there is no4184* panel using a strap-derived bios mode present. this condition4185* includes, but is different from, the DDC panel indicator above4186*/4187if (fpstrapping == 0xf || fpindex == 0xf)4188return 0;41894190bios->fp.mode_ptr = bios->fp.fptablepointer + headerlen +4191recordlen * fpindex + ofs;41924193NV_TRACE(dev, "BIOS FP mode: %dx%d (%dkHz pixel clock)\n",4194ROM16(bios->data[bios->fp.mode_ptr + 11]) + 1,4195ROM16(bios->data[bios->fp.mode_ptr + 25]) + 1,4196ROM16(bios->data[bios->fp.mode_ptr + 7]) * 10);41974198return 0;4199}42004201bool nouveau_bios_fp_mode(struct drm_device *dev, struct drm_display_mode *mode)4202{4203struct drm_nouveau_private *dev_priv = dev->dev_private;4204struct nvbios *bios = &dev_priv->vbios;4205uint8_t *mode_entry = &bios->data[bios->fp.mode_ptr];42064207if (!mode) /* just checking whether we can produce a mode */4208return bios->fp.mode_ptr;42094210memset(mode, 0, sizeof(struct drm_display_mode));4211/*4212* For version 1.0 (version in byte 0):4213* bytes 1-2 are "panel type", including bits on whether Colour/mono,4214* single/dual link, and type (TFT etc.)4215* bytes 3-6 are bits per colour in RGBX4216*/4217mode->clock = ROM16(mode_entry[7]) * 10;4218/* bytes 9-10 is HActive */4219mode->hdisplay = ROM16(mode_entry[11]) + 1;4220/*4221* bytes 13-14 is HValid Start4222* bytes 15-16 is HValid End4223*/4224mode->hsync_start = ROM16(mode_entry[17]) + 1;4225mode->hsync_end = ROM16(mode_entry[19]) + 1;4226mode->htotal = ROM16(mode_entry[21]) + 1;4227/* bytes 23-24, 27-30 similarly, but vertical */4228mode->vdisplay = ROM16(mode_entry[25]) + 1;4229mode->vsync_start = ROM16(mode_entry[31]) + 1;4230mode->vsync_end = ROM16(mode_entry[33]) + 1;4231mode->vtotal = ROM16(mode_entry[35]) + 1;4232mode->flags |= (mode_entry[37] & 0x10) ?4233DRM_MODE_FLAG_PHSYNC : DRM_MODE_FLAG_NHSYNC;4234mode->flags |= (mode_entry[37] & 0x1) ?4235DRM_MODE_FLAG_PVSYNC : DRM_MODE_FLAG_NVSYNC;4236/*4237* bytes 38-39 relate to spread spectrum settings4238* bytes 40-43 are something to do with PWM4239*/42404241mode->status = MODE_OK;4242mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;4243drm_mode_set_name(mode);4244return bios->fp.mode_ptr;4245}42464247int nouveau_bios_parse_lvds_table(struct drm_device *dev, int pxclk, bool *dl, bool *if_is_24bit)4248{4249/*4250* The LVDS table header is (mostly) described in4251* parse_lvds_manufacturer_table_header(): the BIT header additionally4252* contains the dual-link transition pxclk (in 10s kHz), at byte 5 - if4253* straps are not being used for the panel, this specifies the frequency4254* at which modes should be set up in the dual link style.4255*4256* Following the header, the BMP (ver 0xa) table has several records,4257* indexed by a separate xlat table, indexed in turn by the fp strap in4258* EXTDEV_BOOT. Each record had a config byte, followed by 6 script4259* numbers for use by INIT_SUB which controlled panel init and power,4260* and finally a dword of ms to sleep between power off and on4261* operations.4262*4263* In the BIT versions, the table following the header serves as an4264* integrated config and xlat table: the records in the table are4265* indexed by the FP strap nibble in EXTDEV_BOOT, and each record has4266* two bytes - the first as a config byte, the second for indexing the4267* fp mode table pointed to by the BIT 'D' table4268*4269* DDC is not used until after card init, so selecting the correct table4270* entry and setting the dual link flag for EDID equipped panels,4271* requiring tests against the native-mode pixel clock, cannot be done4272* until later, when this function should be called with non-zero pxclk4273*/4274struct drm_nouveau_private *dev_priv = dev->dev_private;4275struct nvbios *bios = &dev_priv->vbios;4276int fpstrapping = get_fp_strap(dev, bios), lvdsmanufacturerindex = 0;4277struct lvdstableheader lth;4278uint16_t lvdsofs;4279int ret, chip_version = bios->chip_version;42804281ret = parse_lvds_manufacturer_table_header(dev, bios, <h);4282if (ret)4283return ret;42844285switch (lth.lvds_ver) {4286case 0x0a: /* pre NV40 */4287lvdsmanufacturerindex = bios->data[4288bios->fp.fpxlatemanufacturertableptr +4289fpstrapping];42904291/* we're done if this isn't the EDID panel case */4292if (!pxclk)4293break;42944295if (chip_version < 0x25) {4296/* nv17 behaviour4297*4298* It seems the old style lvds script pointer is reused4299* to select 18/24 bit colour depth for EDID panels.4300*/4301lvdsmanufacturerindex =4302(bios->legacy.lvds_single_a_script_ptr & 1) ?43032 : 0;4304if (pxclk >= bios->fp.duallink_transition_clk)4305lvdsmanufacturerindex++;4306} else if (chip_version < 0x30) {4307/* nv28 behaviour (off-chip encoder)4308*4309* nv28 does a complex dance of first using byte 121 of4310* the EDID to choose the lvdsmanufacturerindex, then4311* later attempting to match the EDID manufacturer and4312* product IDs in a table (signature 'pidt' (panel id4313* table?)), setting an lvdsmanufacturerindex of 0 and4314* an fp strap of the match index (or 0xf if none)4315*/4316lvdsmanufacturerindex = 0;4317} else {4318/* nv31, nv34 behaviour */4319lvdsmanufacturerindex = 0;4320if (pxclk >= bios->fp.duallink_transition_clk)4321lvdsmanufacturerindex = 2;4322if (pxclk >= 140000)4323lvdsmanufacturerindex = 3;4324}43254326/*4327* nvidia set the high nibble of (cr57=f, cr58) to4328* lvdsmanufacturerindex in this case; we don't4329*/4330break;4331case 0x30: /* NV4x */4332case 0x40: /* G80/G90 */4333lvdsmanufacturerindex = fpstrapping;4334break;4335default:4336NV_ERROR(dev, "LVDS table revision not currently supported\n");4337return -ENOSYS;4338}43394340lvdsofs = bios->fp.xlated_entry = bios->fp.lvdsmanufacturerpointer + lth.headerlen + lth.recordlen * lvdsmanufacturerindex;4341switch (lth.lvds_ver) {4342case 0x0a:4343bios->fp.power_off_for_reset = bios->data[lvdsofs] & 1;4344bios->fp.reset_after_pclk_change = bios->data[lvdsofs] & 2;4345bios->fp.dual_link = bios->data[lvdsofs] & 4;4346bios->fp.link_c_increment = bios->data[lvdsofs] & 8;4347*if_is_24bit = bios->data[lvdsofs] & 16;4348break;4349case 0x30:4350case 0x40:4351/*4352* No sign of the "power off for reset" or "reset for panel4353* on" bits, but it's safer to assume we should4354*/4355bios->fp.power_off_for_reset = true;4356bios->fp.reset_after_pclk_change = true;43574358/*4359* It's ok lvdsofs is wrong for nv4x edid case; dual_link is4360* over-written, and if_is_24bit isn't used4361*/4362bios->fp.dual_link = bios->data[lvdsofs] & 1;4363bios->fp.if_is_24bit = bios->data[lvdsofs] & 2;4364bios->fp.strapless_is_24bit = bios->data[bios->fp.lvdsmanufacturerpointer + 4];4365bios->fp.duallink_transition_clk = ROM16(bios->data[bios->fp.lvdsmanufacturerpointer + 5]) * 10;4366break;4367}43684369/* Dell Latitude D620 reports a too-high value for the dual-link4370* transition freq, causing us to program the panel incorrectly.4371*4372* It doesn't appear the VBIOS actually uses its transition freq4373* (90000kHz), instead it uses the "Number of LVDS channels" field4374* out of the panel ID structure (http://www.spwg.org/).4375*4376* For the moment, a quirk will do :)4377*/4378if (nv_match_device(dev, 0x01d7, 0x1028, 0x01c2))4379bios->fp.duallink_transition_clk = 80000;43804381/* set dual_link flag for EDID case */4382if (pxclk && (chip_version < 0x25 || chip_version > 0x28))4383bios->fp.dual_link = (pxclk >= bios->fp.duallink_transition_clk);43844385*dl = bios->fp.dual_link;43864387return 0;4388}43894390static uint8_t *4391bios_output_config_match(struct drm_device *dev, struct dcb_entry *dcbent,4392uint16_t record, int record_len, int record_nr,4393bool match_link)4394{4395struct drm_nouveau_private *dev_priv = dev->dev_private;4396struct nvbios *bios = &dev_priv->vbios;4397uint32_t entry;4398uint16_t table;4399int i, v;44004401switch (dcbent->type) {4402case OUTPUT_TMDS:4403case OUTPUT_LVDS:4404case OUTPUT_DP:4405break;4406default:4407match_link = false;4408break;4409}44104411for (i = 0; i < record_nr; i++, record += record_len) {4412table = ROM16(bios->data[record]);4413if (!table)4414continue;4415entry = ROM32(bios->data[table]);44164417if (match_link) {4418v = (entry & 0x00c00000) >> 22;4419if (!(v & dcbent->sorconf.link))4420continue;4421}44224423v = (entry & 0x000f0000) >> 16;4424if (!(v & dcbent->or))4425continue;44264427v = (entry & 0x000000f0) >> 4;4428if (v != dcbent->location)4429continue;44304431v = (entry & 0x0000000f);4432if (v != dcbent->type)4433continue;44344435return &bios->data[table];4436}44374438return NULL;4439}44404441void *4442nouveau_bios_dp_table(struct drm_device *dev, struct dcb_entry *dcbent,4443int *length)4444{4445struct drm_nouveau_private *dev_priv = dev->dev_private;4446struct nvbios *bios = &dev_priv->vbios;4447uint8_t *table;44484449if (!bios->display.dp_table_ptr) {4450NV_ERROR(dev, "No pointer to DisplayPort table\n");4451return NULL;4452}4453table = &bios->data[bios->display.dp_table_ptr];44544455if (table[0] != 0x20 && table[0] != 0x21) {4456NV_ERROR(dev, "DisplayPort table version 0x%02x unknown\n",4457table[0]);4458return NULL;4459}44604461*length = table[4];4462return bios_output_config_match(dev, dcbent,4463bios->display.dp_table_ptr + table[1],4464table[2], table[3], table[0] >= 0x21);4465}44664467int4468nouveau_bios_run_display_table(struct drm_device *dev, struct dcb_entry *dcbent,4469uint32_t sub, int pxclk)4470{4471/*4472* The display script table is located by the BIT 'U' table.4473*4474* It contains an array of pointers to various tables describing4475* a particular output type. The first 32-bits of the output4476* tables contains similar information to a DCB entry, and is4477* used to decide whether that particular table is suitable for4478* the output you want to access.4479*4480* The "record header length" field here seems to indicate the4481* offset of the first configuration entry in the output tables.4482* This is 10 on most cards I've seen, but 12 has been witnessed4483* on DP cards, and there's another script pointer within the4484* header.4485*4486* offset + 0 ( 8 bits): version4487* offset + 1 ( 8 bits): header length4488* offset + 2 ( 8 bits): record length4489* offset + 3 ( 8 bits): number of records4490* offset + 4 ( 8 bits): record header length4491* offset + 5 (16 bits): pointer to first output script table4492*/44934494struct drm_nouveau_private *dev_priv = dev->dev_private;4495struct nvbios *bios = &dev_priv->vbios;4496uint8_t *table = &bios->data[bios->display.script_table_ptr];4497uint8_t *otable = NULL;4498uint16_t script;4499int i = 0;45004501if (!bios->display.script_table_ptr) {4502NV_ERROR(dev, "No pointer to output script table\n");4503return 1;4504}45054506/*4507* Nothing useful has been in any of the pre-2.0 tables I've seen,4508* so until they are, we really don't need to care.4509*/4510if (table[0] < 0x20)4511return 1;45124513if (table[0] != 0x20 && table[0] != 0x21) {4514NV_ERROR(dev, "Output script table version 0x%02x unknown\n",4515table[0]);4516return 1;4517}45184519/*4520* The output script tables describing a particular output type4521* look as follows:4522*4523* offset + 0 (32 bits): output this table matches (hash of DCB)4524* offset + 4 ( 8 bits): unknown4525* offset + 5 ( 8 bits): number of configurations4526* offset + 6 (16 bits): pointer to some script4527* offset + 8 (16 bits): pointer to some script4528*4529* headerlen == 104530* offset + 10 : configuration 04531*4532* headerlen == 124533* offset + 10 : pointer to some script4534* offset + 12 : configuration 04535*4536* Each config entry is as follows:4537*4538* offset + 0 (16 bits): unknown, assumed to be a match value4539* offset + 2 (16 bits): pointer to script table (clock set?)4540* offset + 4 (16 bits): pointer to script table (reset?)4541*4542* There doesn't appear to be a count value to say how many4543* entries exist in each script table, instead, a 0 value in4544* the first 16-bit word seems to indicate both the end of the4545* list and the default entry. The second 16-bit word in the4546* script tables is a pointer to the script to execute.4547*/45484549NV_DEBUG_KMS(dev, "Searching for output entry for %d %d %d\n",4550dcbent->type, dcbent->location, dcbent->or);4551otable = bios_output_config_match(dev, dcbent, table[1] +4552bios->display.script_table_ptr,4553table[2], table[3], table[0] >= 0x21);4554if (!otable) {4555NV_DEBUG_KMS(dev, "failed to match any output table\n");4556return 1;4557}45584559if (pxclk < -2 || pxclk > 0) {4560/* Try to find matching script table entry */4561for (i = 0; i < otable[5]; i++) {4562if (ROM16(otable[table[4] + i*6]) == sub)4563break;4564}45654566if (i == otable[5]) {4567NV_ERROR(dev, "Table 0x%04x not found for %d/%d, "4568"using first\n",4569sub, dcbent->type, dcbent->or);4570i = 0;4571}4572}45734574if (pxclk == 0) {4575script = ROM16(otable[6]);4576if (!script) {4577NV_DEBUG_KMS(dev, "output script 0 not found\n");4578return 1;4579}45804581NV_DEBUG_KMS(dev, "0x%04X: parsing output script 0\n", script);4582nouveau_bios_run_init_table(dev, script, dcbent);4583} else4584if (pxclk == -1) {4585script = ROM16(otable[8]);4586if (!script) {4587NV_DEBUG_KMS(dev, "output script 1 not found\n");4588return 1;4589}45904591NV_DEBUG_KMS(dev, "0x%04X: parsing output script 1\n", script);4592nouveau_bios_run_init_table(dev, script, dcbent);4593} else4594if (pxclk == -2) {4595if (table[4] >= 12)4596script = ROM16(otable[10]);4597else4598script = 0;4599if (!script) {4600NV_DEBUG_KMS(dev, "output script 2 not found\n");4601return 1;4602}46034604NV_DEBUG_KMS(dev, "0x%04X: parsing output script 2\n", script);4605nouveau_bios_run_init_table(dev, script, dcbent);4606} else4607if (pxclk > 0) {4608script = ROM16(otable[table[4] + i*6 + 2]);4609if (script)4610script = clkcmptable(bios, script, pxclk);4611if (!script) {4612NV_DEBUG_KMS(dev, "clock script 0 not found\n");4613return 1;4614}46154616NV_DEBUG_KMS(dev, "0x%04X: parsing clock script 0\n", script);4617nouveau_bios_run_init_table(dev, script, dcbent);4618} else4619if (pxclk < 0) {4620script = ROM16(otable[table[4] + i*6 + 4]);4621if (script)4622script = clkcmptable(bios, script, -pxclk);4623if (!script) {4624NV_DEBUG_KMS(dev, "clock script 1 not found\n");4625return 1;4626}46274628NV_DEBUG_KMS(dev, "0x%04X: parsing clock script 1\n", script);4629nouveau_bios_run_init_table(dev, script, dcbent);4630}46314632return 0;4633}463446354636int run_tmds_table(struct drm_device *dev, struct dcb_entry *dcbent, int head, int pxclk)4637{4638/*4639* the pxclk parameter is in kHz4640*4641* This runs the TMDS regs setting code found on BIT bios cards4642*4643* For ffs(or) == 1 use the first table, for ffs(or) == 2 and4644* ffs(or) == 3, use the second.4645*/46464647struct drm_nouveau_private *dev_priv = dev->dev_private;4648struct nvbios *bios = &dev_priv->vbios;4649int cv = bios->chip_version;4650uint16_t clktable = 0, scriptptr;4651uint32_t sel_clk_binding, sel_clk;46524653/* pre-nv17 off-chip tmds uses scripts, post nv17 doesn't */4654if (cv >= 0x17 && cv != 0x1a && cv != 0x20 &&4655dcbent->location != DCB_LOC_ON_CHIP)4656return 0;46574658switch (ffs(dcbent->or)) {4659case 1:4660clktable = bios->tmds.output0_script_ptr;4661break;4662case 2:4663case 3:4664clktable = bios->tmds.output1_script_ptr;4665break;4666}46674668if (!clktable) {4669NV_ERROR(dev, "Pixel clock comparison table not found\n");4670return -EINVAL;4671}46724673scriptptr = clkcmptable(bios, clktable, pxclk);46744675if (!scriptptr) {4676NV_ERROR(dev, "TMDS output init script not found\n");4677return -ENOENT;4678}46794680/* don't let script change pll->head binding */4681sel_clk_binding = bios_rd32(bios, NV_PRAMDAC_SEL_CLK) & 0x50000;4682run_digital_op_script(dev, scriptptr, dcbent, head, pxclk >= 165000);4683sel_clk = NVReadRAMDAC(dev, 0, NV_PRAMDAC_SEL_CLK) & ~0x50000;4684NVWriteRAMDAC(dev, 0, NV_PRAMDAC_SEL_CLK, sel_clk | sel_clk_binding);46854686return 0;4687}46884689struct pll_mapping {4690u8 type;4691u32 reg;4692};46934694static struct pll_mapping nv04_pll_mapping[] = {4695{ PLL_CORE , NV_PRAMDAC_NVPLL_COEFF },4696{ PLL_MEMORY, NV_PRAMDAC_MPLL_COEFF },4697{ PLL_VPLL0 , NV_PRAMDAC_VPLL_COEFF },4698{ PLL_VPLL1 , NV_RAMDAC_VPLL2 },4699{}4700};47014702static struct pll_mapping nv40_pll_mapping[] = {4703{ PLL_CORE , 0x004000 },4704{ PLL_MEMORY, 0x004020 },4705{ PLL_VPLL0 , NV_PRAMDAC_VPLL_COEFF },4706{ PLL_VPLL1 , NV_RAMDAC_VPLL2 },4707{}4708};47094710static struct pll_mapping nv50_pll_mapping[] = {4711{ PLL_CORE , 0x004028 },4712{ PLL_SHADER, 0x004020 },4713{ PLL_UNK03 , 0x004000 },4714{ PLL_MEMORY, 0x004008 },4715{ PLL_UNK40 , 0x00e810 },4716{ PLL_UNK41 , 0x00e818 },4717{ PLL_UNK42 , 0x00e824 },4718{ PLL_VPLL0 , 0x614100 },4719{ PLL_VPLL1 , 0x614900 },4720{}4721};47224723static struct pll_mapping nv84_pll_mapping[] = {4724{ PLL_CORE , 0x004028 },4725{ PLL_SHADER, 0x004020 },4726{ PLL_MEMORY, 0x004008 },4727{ PLL_UNK05 , 0x004030 },4728{ PLL_UNK41 , 0x00e818 },4729{ PLL_VPLL0 , 0x614100 },4730{ PLL_VPLL1 , 0x614900 },4731{}4732};47334734u324735get_pll_register(struct drm_device *dev, enum pll_types type)4736{4737struct drm_nouveau_private *dev_priv = dev->dev_private;4738struct nvbios *bios = &dev_priv->vbios;4739struct pll_mapping *map;4740int i;47414742if (dev_priv->card_type < NV_40)4743map = nv04_pll_mapping;4744else4745if (dev_priv->card_type < NV_50)4746map = nv40_pll_mapping;4747else {4748u8 *plim = &bios->data[bios->pll_limit_tbl_ptr];47494750if (plim[0] >= 0x30) {4751u8 *entry = plim + plim[1];4752for (i = 0; i < plim[3]; i++, entry += plim[2]) {4753if (entry[0] == type)4754return ROM32(entry[3]);4755}47564757return 0;4758}47594760if (dev_priv->chipset == 0x50)4761map = nv50_pll_mapping;4762else4763map = nv84_pll_mapping;4764}47654766while (map->reg) {4767if (map->type == type)4768return map->reg;4769map++;4770}47714772return 0;4773}47744775int get_pll_limits(struct drm_device *dev, uint32_t limit_match, struct pll_lims *pll_lim)4776{4777/*4778* PLL limits table4779*4780* Version 0x10: NV30, NV314781* One byte header (version), one record of 24 bytes4782* Version 0x11: NV36 - Not implemented4783* Seems to have same record style as 0x10, but 3 records rather than 14784* Version 0x20: Found on Geforce 6 cards4785* Trivial 4 byte BIT header. 31 (0x1f) byte record length4786* Version 0x21: Found on Geforce 7, 8 and some Geforce 6 cards4787* 5 byte header, fifth byte of unknown purpose. 35 (0x23) byte record4788* length in general, some (integrated) have an extra configuration byte4789* Version 0x30: Found on Geforce 8, separates the register mapping4790* from the limits tables.4791*/47924793struct drm_nouveau_private *dev_priv = dev->dev_private;4794struct nvbios *bios = &dev_priv->vbios;4795int cv = bios->chip_version, pllindex = 0;4796uint8_t pll_lim_ver = 0, headerlen = 0, recordlen = 0, entries = 0;4797uint32_t crystal_strap_mask, crystal_straps;47984799if (!bios->pll_limit_tbl_ptr) {4800if (cv == 0x30 || cv == 0x31 || cv == 0x35 || cv == 0x36 ||4801cv >= 0x40) {4802NV_ERROR(dev, "Pointer to PLL limits table invalid\n");4803return -EINVAL;4804}4805} else4806pll_lim_ver = bios->data[bios->pll_limit_tbl_ptr];48074808crystal_strap_mask = 1 << 6;4809/* open coded dev->twoHeads test */4810if (cv > 0x10 && cv != 0x15 && cv != 0x1a && cv != 0x20)4811crystal_strap_mask |= 1 << 22;4812crystal_straps = nvReadEXTDEV(dev, NV_PEXTDEV_BOOT_0) &4813crystal_strap_mask;48144815switch (pll_lim_ver) {4816/*4817* We use version 0 to indicate a pre limit table bios (single stage4818* pll) and load the hard coded limits instead.4819*/4820case 0:4821break;4822case 0x10:4823case 0x11:4824/*4825* Strictly v0x11 has 3 entries, but the last two don't seem4826* to get used.4827*/4828headerlen = 1;4829recordlen = 0x18;4830entries = 1;4831pllindex = 0;4832break;4833case 0x20:4834case 0x21:4835case 0x30:4836case 0x40:4837headerlen = bios->data[bios->pll_limit_tbl_ptr + 1];4838recordlen = bios->data[bios->pll_limit_tbl_ptr + 2];4839entries = bios->data[bios->pll_limit_tbl_ptr + 3];4840break;4841default:4842NV_ERROR(dev, "PLL limits table revision 0x%X not currently "4843"supported\n", pll_lim_ver);4844return -ENOSYS;4845}48464847/* initialize all members to zero */4848memset(pll_lim, 0, sizeof(struct pll_lims));48494850/* if we were passed a type rather than a register, figure4851* out the register and store it4852*/4853if (limit_match > PLL_MAX)4854pll_lim->reg = limit_match;4855else {4856pll_lim->reg = get_pll_register(dev, limit_match);4857if (!pll_lim->reg)4858return -ENOENT;4859}48604861if (pll_lim_ver == 0x10 || pll_lim_ver == 0x11) {4862uint8_t *pll_rec = &bios->data[bios->pll_limit_tbl_ptr + headerlen + recordlen * pllindex];48634864pll_lim->vco1.minfreq = ROM32(pll_rec[0]);4865pll_lim->vco1.maxfreq = ROM32(pll_rec[4]);4866pll_lim->vco2.minfreq = ROM32(pll_rec[8]);4867pll_lim->vco2.maxfreq = ROM32(pll_rec[12]);4868pll_lim->vco1.min_inputfreq = ROM32(pll_rec[16]);4869pll_lim->vco2.min_inputfreq = ROM32(pll_rec[20]);4870pll_lim->vco1.max_inputfreq = pll_lim->vco2.max_inputfreq = INT_MAX;48714872/* these values taken from nv30/31/36 */4873pll_lim->vco1.min_n = 0x1;4874if (cv == 0x36)4875pll_lim->vco1.min_n = 0x5;4876pll_lim->vco1.max_n = 0xff;4877pll_lim->vco1.min_m = 0x1;4878pll_lim->vco1.max_m = 0xd;4879pll_lim->vco2.min_n = 0x4;4880/*4881* On nv30, 31, 36 (i.e. all cards with two stage PLLs with this4882* table version (apart from nv35)), N2 is compared to4883* maxN2 (0x46) and 10 * maxM2 (0x4), so set maxN2 to 0x28 and4884* save a comparison4885*/4886pll_lim->vco2.max_n = 0x28;4887if (cv == 0x30 || cv == 0x35)4888/* only 5 bits available for N2 on nv30/35 */4889pll_lim->vco2.max_n = 0x1f;4890pll_lim->vco2.min_m = 0x1;4891pll_lim->vco2.max_m = 0x4;4892pll_lim->max_log2p = 0x7;4893pll_lim->max_usable_log2p = 0x6;4894} else if (pll_lim_ver == 0x20 || pll_lim_ver == 0x21) {4895uint16_t plloffs = bios->pll_limit_tbl_ptr + headerlen;4896uint8_t *pll_rec;4897int i;48984899/*4900* First entry is default match, if nothing better. warn if4901* reg field nonzero4902*/4903if (ROM32(bios->data[plloffs]))4904NV_WARN(dev, "Default PLL limit entry has non-zero "4905"register field\n");49064907for (i = 1; i < entries; i++)4908if (ROM32(bios->data[plloffs + recordlen * i]) == pll_lim->reg) {4909pllindex = i;4910break;4911}49124913if ((dev_priv->card_type >= NV_50) && (pllindex == 0)) {4914NV_ERROR(dev, "Register 0x%08x not found in PLL "4915"limits table", pll_lim->reg);4916return -ENOENT;4917}49184919pll_rec = &bios->data[plloffs + recordlen * pllindex];49204921BIOSLOG(bios, "Loading PLL limits for reg 0x%08x\n",4922pllindex ? pll_lim->reg : 0);49234924/*4925* Frequencies are stored in tables in MHz, kHz are more4926* useful, so we convert.4927*/49284929/* What output frequencies can each VCO generate? */4930pll_lim->vco1.minfreq = ROM16(pll_rec[4]) * 1000;4931pll_lim->vco1.maxfreq = ROM16(pll_rec[6]) * 1000;4932pll_lim->vco2.minfreq = ROM16(pll_rec[8]) * 1000;4933pll_lim->vco2.maxfreq = ROM16(pll_rec[10]) * 1000;49344935/* What input frequencies they accept (past the m-divider)? */4936pll_lim->vco1.min_inputfreq = ROM16(pll_rec[12]) * 1000;4937pll_lim->vco2.min_inputfreq = ROM16(pll_rec[14]) * 1000;4938pll_lim->vco1.max_inputfreq = ROM16(pll_rec[16]) * 1000;4939pll_lim->vco2.max_inputfreq = ROM16(pll_rec[18]) * 1000;49404941/* What values are accepted as multiplier and divider? */4942pll_lim->vco1.min_n = pll_rec[20];4943pll_lim->vco1.max_n = pll_rec[21];4944pll_lim->vco1.min_m = pll_rec[22];4945pll_lim->vco1.max_m = pll_rec[23];4946pll_lim->vco2.min_n = pll_rec[24];4947pll_lim->vco2.max_n = pll_rec[25];4948pll_lim->vco2.min_m = pll_rec[26];4949pll_lim->vco2.max_m = pll_rec[27];49504951pll_lim->max_usable_log2p = pll_lim->max_log2p = pll_rec[29];4952if (pll_lim->max_log2p > 0x7)4953/* pll decoding in nv_hw.c assumes never > 7 */4954NV_WARN(dev, "Max log2 P value greater than 7 (%d)\n",4955pll_lim->max_log2p);4956if (cv < 0x60)4957pll_lim->max_usable_log2p = 0x6;4958pll_lim->log2p_bias = pll_rec[30];49594960if (recordlen > 0x22)4961pll_lim->refclk = ROM32(pll_rec[31]);49624963if (recordlen > 0x23 && pll_rec[35])4964NV_WARN(dev,4965"Bits set in PLL configuration byte (%x)\n",4966pll_rec[35]);49674968/* C51 special not seen elsewhere */4969if (cv == 0x51 && !pll_lim->refclk) {4970uint32_t sel_clk = bios_rd32(bios, NV_PRAMDAC_SEL_CLK);49714972if ((pll_lim->reg == NV_PRAMDAC_VPLL_COEFF && sel_clk & 0x20) ||4973(pll_lim->reg == NV_RAMDAC_VPLL2 && sel_clk & 0x80)) {4974if (bios_idxprt_rd(bios, NV_CIO_CRX__COLOR, NV_CIO_CRE_CHIP_ID_INDEX) < 0xa3)4975pll_lim->refclk = 200000;4976else4977pll_lim->refclk = 25000;4978}4979}4980} else if (pll_lim_ver == 0x30) { /* ver 0x30 */4981uint8_t *entry = &bios->data[bios->pll_limit_tbl_ptr + headerlen];4982uint8_t *record = NULL;4983int i;49844985BIOSLOG(bios, "Loading PLL limits for register 0x%08x\n",4986pll_lim->reg);49874988for (i = 0; i < entries; i++, entry += recordlen) {4989if (ROM32(entry[3]) == pll_lim->reg) {4990record = &bios->data[ROM16(entry[1])];4991break;4992}4993}49944995if (!record) {4996NV_ERROR(dev, "Register 0x%08x not found in PLL "4997"limits table", pll_lim->reg);4998return -ENOENT;4999}50005001pll_lim->vco1.minfreq = ROM16(record[0]) * 1000;5002pll_lim->vco1.maxfreq = ROM16(record[2]) * 1000;5003pll_lim->vco2.minfreq = ROM16(record[4]) * 1000;5004pll_lim->vco2.maxfreq = ROM16(record[6]) * 1000;5005pll_lim->vco1.min_inputfreq = ROM16(record[8]) * 1000;5006pll_lim->vco2.min_inputfreq = ROM16(record[10]) * 1000;5007pll_lim->vco1.max_inputfreq = ROM16(record[12]) * 1000;5008pll_lim->vco2.max_inputfreq = ROM16(record[14]) * 1000;5009pll_lim->vco1.min_n = record[16];5010pll_lim->vco1.max_n = record[17];5011pll_lim->vco1.min_m = record[18];5012pll_lim->vco1.max_m = record[19];5013pll_lim->vco2.min_n = record[20];5014pll_lim->vco2.max_n = record[21];5015pll_lim->vco2.min_m = record[22];5016pll_lim->vco2.max_m = record[23];5017pll_lim->max_usable_log2p = pll_lim->max_log2p = record[25];5018pll_lim->log2p_bias = record[27];5019pll_lim->refclk = ROM32(record[28]);5020} else if (pll_lim_ver) { /* ver 0x40 */5021uint8_t *entry = &bios->data[bios->pll_limit_tbl_ptr + headerlen];5022uint8_t *record = NULL;5023int i;50245025BIOSLOG(bios, "Loading PLL limits for register 0x%08x\n",5026pll_lim->reg);50275028for (i = 0; i < entries; i++, entry += recordlen) {5029if (ROM32(entry[3]) == pll_lim->reg) {5030record = &bios->data[ROM16(entry[1])];5031break;5032}5033}50345035if (!record) {5036NV_ERROR(dev, "Register 0x%08x not found in PLL "5037"limits table", pll_lim->reg);5038return -ENOENT;5039}50405041pll_lim->vco1.minfreq = ROM16(record[0]) * 1000;5042pll_lim->vco1.maxfreq = ROM16(record[2]) * 1000;5043pll_lim->vco1.min_inputfreq = ROM16(record[4]) * 1000;5044pll_lim->vco1.max_inputfreq = ROM16(record[6]) * 1000;5045pll_lim->vco1.min_m = record[8];5046pll_lim->vco1.max_m = record[9];5047pll_lim->vco1.min_n = record[10];5048pll_lim->vco1.max_n = record[11];5049pll_lim->min_p = record[12];5050pll_lim->max_p = record[13];5051pll_lim->refclk = ROM16(entry[9]) * 1000;5052}50535054/*5055* By now any valid limit table ought to have set a max frequency for5056* vco1, so if it's zero it's either a pre limit table bios, or one5057* with an empty limit table (seen on nv18)5058*/5059if (!pll_lim->vco1.maxfreq) {5060pll_lim->vco1.minfreq = bios->fminvco;5061pll_lim->vco1.maxfreq = bios->fmaxvco;5062pll_lim->vco1.min_inputfreq = 0;5063pll_lim->vco1.max_inputfreq = INT_MAX;5064pll_lim->vco1.min_n = 0x1;5065pll_lim->vco1.max_n = 0xff;5066pll_lim->vco1.min_m = 0x1;5067if (crystal_straps == 0) {5068/* nv05 does this, nv11 doesn't, nv10 unknown */5069if (cv < 0x11)5070pll_lim->vco1.min_m = 0x7;5071pll_lim->vco1.max_m = 0xd;5072} else {5073if (cv < 0x11)5074pll_lim->vco1.min_m = 0x8;5075pll_lim->vco1.max_m = 0xe;5076}5077if (cv < 0x17 || cv == 0x1a || cv == 0x20)5078pll_lim->max_log2p = 4;5079else5080pll_lim->max_log2p = 5;5081pll_lim->max_usable_log2p = pll_lim->max_log2p;5082}50835084if (!pll_lim->refclk)5085switch (crystal_straps) {5086case 0:5087pll_lim->refclk = 13500;5088break;5089case (1 << 6):5090pll_lim->refclk = 14318;5091break;5092case (1 << 22):5093pll_lim->refclk = 27000;5094break;5095case (1 << 22 | 1 << 6):5096pll_lim->refclk = 25000;5097break;5098}50995100NV_DEBUG(dev, "pll.vco1.minfreq: %d\n", pll_lim->vco1.minfreq);5101NV_DEBUG(dev, "pll.vco1.maxfreq: %d\n", pll_lim->vco1.maxfreq);5102NV_DEBUG(dev, "pll.vco1.min_inputfreq: %d\n", pll_lim->vco1.min_inputfreq);5103NV_DEBUG(dev, "pll.vco1.max_inputfreq: %d\n", pll_lim->vco1.max_inputfreq);5104NV_DEBUG(dev, "pll.vco1.min_n: %d\n", pll_lim->vco1.min_n);5105NV_DEBUG(dev, "pll.vco1.max_n: %d\n", pll_lim->vco1.max_n);5106NV_DEBUG(dev, "pll.vco1.min_m: %d\n", pll_lim->vco1.min_m);5107NV_DEBUG(dev, "pll.vco1.max_m: %d\n", pll_lim->vco1.max_m);5108if (pll_lim->vco2.maxfreq) {5109NV_DEBUG(dev, "pll.vco2.minfreq: %d\n", pll_lim->vco2.minfreq);5110NV_DEBUG(dev, "pll.vco2.maxfreq: %d\n", pll_lim->vco2.maxfreq);5111NV_DEBUG(dev, "pll.vco2.min_inputfreq: %d\n", pll_lim->vco2.min_inputfreq);5112NV_DEBUG(dev, "pll.vco2.max_inputfreq: %d\n", pll_lim->vco2.max_inputfreq);5113NV_DEBUG(dev, "pll.vco2.min_n: %d\n", pll_lim->vco2.min_n);5114NV_DEBUG(dev, "pll.vco2.max_n: %d\n", pll_lim->vco2.max_n);5115NV_DEBUG(dev, "pll.vco2.min_m: %d\n", pll_lim->vco2.min_m);5116NV_DEBUG(dev, "pll.vco2.max_m: %d\n", pll_lim->vco2.max_m);5117}5118if (!pll_lim->max_p) {5119NV_DEBUG(dev, "pll.max_log2p: %d\n", pll_lim->max_log2p);5120NV_DEBUG(dev, "pll.log2p_bias: %d\n", pll_lim->log2p_bias);5121} else {5122NV_DEBUG(dev, "pll.min_p: %d\n", pll_lim->min_p);5123NV_DEBUG(dev, "pll.max_p: %d\n", pll_lim->max_p);5124}5125NV_DEBUG(dev, "pll.refclk: %d\n", pll_lim->refclk);51265127return 0;5128}51295130static void parse_bios_version(struct drm_device *dev, struct nvbios *bios, uint16_t offset)5131{5132/*5133* offset + 0 (8 bits): Micro version5134* offset + 1 (8 bits): Minor version5135* offset + 2 (8 bits): Chip version5136* offset + 3 (8 bits): Major version5137*/51385139bios->major_version = bios->data[offset + 3];5140bios->chip_version = bios->data[offset + 2];5141NV_TRACE(dev, "Bios version %02x.%02x.%02x.%02x\n",5142bios->data[offset + 3], bios->data[offset + 2],5143bios->data[offset + 1], bios->data[offset]);5144}51455146static void parse_script_table_pointers(struct nvbios *bios, uint16_t offset)5147{5148/*5149* Parses the init table segment for pointers used in script execution.5150*5151* offset + 0 (16 bits): init script tables pointer5152* offset + 2 (16 bits): macro index table pointer5153* offset + 4 (16 bits): macro table pointer5154* offset + 6 (16 bits): condition table pointer5155* offset + 8 (16 bits): io condition table pointer5156* offset + 10 (16 bits): io flag condition table pointer5157* offset + 12 (16 bits): init function table pointer5158*/51595160bios->init_script_tbls_ptr = ROM16(bios->data[offset]);5161bios->macro_index_tbl_ptr = ROM16(bios->data[offset + 2]);5162bios->macro_tbl_ptr = ROM16(bios->data[offset + 4]);5163bios->condition_tbl_ptr = ROM16(bios->data[offset + 6]);5164bios->io_condition_tbl_ptr = ROM16(bios->data[offset + 8]);5165bios->io_flag_condition_tbl_ptr = ROM16(bios->data[offset + 10]);5166bios->init_function_tbl_ptr = ROM16(bios->data[offset + 12]);5167}51685169static int parse_bit_A_tbl_entry(struct drm_device *dev, struct nvbios *bios, struct bit_entry *bitentry)5170{5171/*5172* Parses the load detect values for g80 cards.5173*5174* offset + 0 (16 bits): loadval table pointer5175*/51765177uint16_t load_table_ptr;5178uint8_t version, headerlen, entrylen, num_entries;51795180if (bitentry->length != 3) {5181NV_ERROR(dev, "Do not understand BIT A table\n");5182return -EINVAL;5183}51845185load_table_ptr = ROM16(bios->data[bitentry->offset]);51865187if (load_table_ptr == 0x0) {5188NV_ERROR(dev, "Pointer to BIT loadval table invalid\n");5189return -EINVAL;5190}51915192version = bios->data[load_table_ptr];51935194if (version != 0x10) {5195NV_ERROR(dev, "BIT loadval table version %d.%d not supported\n",5196version >> 4, version & 0xF);5197return -ENOSYS;5198}51995200headerlen = bios->data[load_table_ptr + 1];5201entrylen = bios->data[load_table_ptr + 2];5202num_entries = bios->data[load_table_ptr + 3];52035204if (headerlen != 4 || entrylen != 4 || num_entries != 2) {5205NV_ERROR(dev, "Do not understand BIT loadval table\n");5206return -EINVAL;5207}52085209/* First entry is normal dac, 2nd tv-out perhaps? */5210bios->dactestval = ROM32(bios->data[load_table_ptr + headerlen]) & 0x3ff;52115212return 0;5213}52145215static int parse_bit_C_tbl_entry(struct drm_device *dev, struct nvbios *bios, struct bit_entry *bitentry)5216{5217/*5218* offset + 8 (16 bits): PLL limits table pointer5219*5220* There's more in here, but that's unknown.5221*/52225223if (bitentry->length < 10) {5224NV_ERROR(dev, "Do not understand BIT C table\n");5225return -EINVAL;5226}52275228bios->pll_limit_tbl_ptr = ROM16(bios->data[bitentry->offset + 8]);52295230return 0;5231}52325233static int parse_bit_display_tbl_entry(struct drm_device *dev, struct nvbios *bios, struct bit_entry *bitentry)5234{5235/*5236* Parses the flat panel table segment that the bit entry points to.5237* Starting at bitentry->offset:5238*5239* offset + 0 (16 bits): ??? table pointer - seems to have 18 byte5240* records beginning with a freq.5241* offset + 2 (16 bits): mode table pointer5242*/52435244if (bitentry->length != 4) {5245NV_ERROR(dev, "Do not understand BIT display table\n");5246return -EINVAL;5247}52485249bios->fp.fptablepointer = ROM16(bios->data[bitentry->offset + 2]);52505251return 0;5252}52535254static int parse_bit_init_tbl_entry(struct drm_device *dev, struct nvbios *bios, struct bit_entry *bitentry)5255{5256/*5257* Parses the init table segment that the bit entry points to.5258*5259* See parse_script_table_pointers for layout5260*/52615262if (bitentry->length < 14) {5263NV_ERROR(dev, "Do not understand init table\n");5264return -EINVAL;5265}52665267parse_script_table_pointers(bios, bitentry->offset);52685269if (bitentry->length >= 16)5270bios->some_script_ptr = ROM16(bios->data[bitentry->offset + 14]);5271if (bitentry->length >= 18)5272bios->init96_tbl_ptr = ROM16(bios->data[bitentry->offset + 16]);52735274return 0;5275}52765277static int parse_bit_i_tbl_entry(struct drm_device *dev, struct nvbios *bios, struct bit_entry *bitentry)5278{5279/*5280* BIT 'i' (info?) table5281*5282* offset + 0 (32 bits): BIOS version dword (as in B table)5283* offset + 5 (8 bits): BIOS feature byte (same as for BMP?)5284* offset + 13 (16 bits): pointer to table containing DAC load5285* detection comparison values5286*5287* There's other things in the table, purpose unknown5288*/52895290uint16_t daccmpoffset;5291uint8_t dacver, dacheaderlen;52925293if (bitentry->length < 6) {5294NV_ERROR(dev, "BIT i table too short for needed information\n");5295return -EINVAL;5296}52975298parse_bios_version(dev, bios, bitentry->offset);52995300/*5301* bit 4 seems to indicate a mobile bios (doesn't suffer from BMP's5302* Quadro identity crisis), other bits possibly as for BMP feature byte5303*/5304bios->feature_byte = bios->data[bitentry->offset + 5];5305bios->is_mobile = bios->feature_byte & FEATURE_MOBILE;53065307if (bitentry->length < 15) {5308NV_WARN(dev, "BIT i table not long enough for DAC load "5309"detection comparison table\n");5310return -EINVAL;5311}53125313daccmpoffset = ROM16(bios->data[bitentry->offset + 13]);53145315/* doesn't exist on g80 */5316if (!daccmpoffset)5317return 0;53185319/*5320* The first value in the table, following the header, is the5321* comparison value, the second entry is a comparison value for5322* TV load detection.5323*/53245325dacver = bios->data[daccmpoffset];5326dacheaderlen = bios->data[daccmpoffset + 1];53275328if (dacver != 0x00 && dacver != 0x10) {5329NV_WARN(dev, "DAC load detection comparison table version "5330"%d.%d not known\n", dacver >> 4, dacver & 0xf);5331return -ENOSYS;5332}53335334bios->dactestval = ROM32(bios->data[daccmpoffset + dacheaderlen]);5335bios->tvdactestval = ROM32(bios->data[daccmpoffset + dacheaderlen + 4]);53365337return 0;5338}53395340static int parse_bit_lvds_tbl_entry(struct drm_device *dev, struct nvbios *bios, struct bit_entry *bitentry)5341{5342/*5343* Parses the LVDS table segment that the bit entry points to.5344* Starting at bitentry->offset:5345*5346* offset + 0 (16 bits): LVDS strap xlate table pointer5347*/53485349if (bitentry->length != 2) {5350NV_ERROR(dev, "Do not understand BIT LVDS table\n");5351return -EINVAL;5352}53535354/*5355* No idea if it's still called the LVDS manufacturer table, but5356* the concept's close enough.5357*/5358bios->fp.lvdsmanufacturerpointer = ROM16(bios->data[bitentry->offset]);53595360return 0;5361}53625363static int5364parse_bit_M_tbl_entry(struct drm_device *dev, struct nvbios *bios,5365struct bit_entry *bitentry)5366{5367/*5368* offset + 2 (8 bits): number of options in an5369* INIT_RAM_RESTRICT_ZM_REG_GROUP opcode option set5370* offset + 3 (16 bits): pointer to strap xlate table for RAM5371* restrict option selection5372*5373* There's a bunch of bits in this table other than the RAM restrict5374* stuff that we don't use - their use currently unknown5375*/53765377/*5378* Older bios versions don't have a sufficiently long table for5379* what we want5380*/5381if (bitentry->length < 0x5)5382return 0;53835384if (bitentry->version < 2) {5385bios->ram_restrict_group_count = bios->data[bitentry->offset + 2];5386bios->ram_restrict_tbl_ptr = ROM16(bios->data[bitentry->offset + 3]);5387} else {5388bios->ram_restrict_group_count = bios->data[bitentry->offset + 0];5389bios->ram_restrict_tbl_ptr = ROM16(bios->data[bitentry->offset + 1]);5390}53915392return 0;5393}53945395static int parse_bit_tmds_tbl_entry(struct drm_device *dev, struct nvbios *bios, struct bit_entry *bitentry)5396{5397/*5398* Parses the pointer to the TMDS table5399*5400* Starting at bitentry->offset:5401*5402* offset + 0 (16 bits): TMDS table pointer5403*5404* The TMDS table is typically found just before the DCB table, with a5405* characteristic signature of 0x11,0x13 (1.1 being version, 0x13 being5406* length?)5407*5408* At offset +7 is a pointer to a script, which I don't know how to5409* run yet.5410* At offset +9 is a pointer to another script, likewise5411* Offset +11 has a pointer to a table where the first word is a pxclk5412* frequency and the second word a pointer to a script, which should be5413* run if the comparison pxclk frequency is less than the pxclk desired.5414* This repeats for decreasing comparison frequencies5415* Offset +13 has a pointer to a similar table5416* The selection of table (and possibly +7/+9 script) is dictated by5417* "or" from the DCB.5418*/54195420uint16_t tmdstableptr, script1, script2;54215422if (bitentry->length != 2) {5423NV_ERROR(dev, "Do not understand BIT TMDS table\n");5424return -EINVAL;5425}54265427tmdstableptr = ROM16(bios->data[bitentry->offset]);5428if (!tmdstableptr) {5429NV_ERROR(dev, "Pointer to TMDS table invalid\n");5430return -EINVAL;5431}54325433NV_INFO(dev, "TMDS table version %d.%d\n",5434bios->data[tmdstableptr] >> 4, bios->data[tmdstableptr] & 0xf);54355436/* nv50+ has v2.0, but we don't parse it atm */5437if (bios->data[tmdstableptr] != 0x11)5438return -ENOSYS;54395440/*5441* These two scripts are odd: they don't seem to get run even when5442* they are not stubbed.5443*/5444script1 = ROM16(bios->data[tmdstableptr + 7]);5445script2 = ROM16(bios->data[tmdstableptr + 9]);5446if (bios->data[script1] != 'q' || bios->data[script2] != 'q')5447NV_WARN(dev, "TMDS table script pointers not stubbed\n");54485449bios->tmds.output0_script_ptr = ROM16(bios->data[tmdstableptr + 11]);5450bios->tmds.output1_script_ptr = ROM16(bios->data[tmdstableptr + 13]);54515452return 0;5453}54545455static int5456parse_bit_U_tbl_entry(struct drm_device *dev, struct nvbios *bios,5457struct bit_entry *bitentry)5458{5459/*5460* Parses the pointer to the G80 output script tables5461*5462* Starting at bitentry->offset:5463*5464* offset + 0 (16 bits): output script table pointer5465*/54665467uint16_t outputscripttableptr;54685469if (bitentry->length != 3) {5470NV_ERROR(dev, "Do not understand BIT U table\n");5471return -EINVAL;5472}54735474outputscripttableptr = ROM16(bios->data[bitentry->offset]);5475bios->display.script_table_ptr = outputscripttableptr;5476return 0;5477}54785479static int5480parse_bit_displayport_tbl_entry(struct drm_device *dev, struct nvbios *bios,5481struct bit_entry *bitentry)5482{5483bios->display.dp_table_ptr = ROM16(bios->data[bitentry->offset]);5484return 0;5485}54865487struct bit_table {5488const char id;5489int (* const parse_fn)(struct drm_device *, struct nvbios *, struct bit_entry *);5490};54915492#define BIT_TABLE(id, funcid) ((struct bit_table){ id, parse_bit_##funcid##_tbl_entry })54935494int5495bit_table(struct drm_device *dev, u8 id, struct bit_entry *bit)5496{5497struct drm_nouveau_private *dev_priv = dev->dev_private;5498struct nvbios *bios = &dev_priv->vbios;5499u8 entries, *entry;55005501entries = bios->data[bios->offset + 10];5502entry = &bios->data[bios->offset + 12];5503while (entries--) {5504if (entry[0] == id) {5505bit->id = entry[0];5506bit->version = entry[1];5507bit->length = ROM16(entry[2]);5508bit->offset = ROM16(entry[4]);5509bit->data = ROMPTR(bios, entry[4]);5510return 0;5511}55125513entry += bios->data[bios->offset + 9];5514}55155516return -ENOENT;5517}55185519static int5520parse_bit_table(struct nvbios *bios, const uint16_t bitoffset,5521struct bit_table *table)5522{5523struct drm_device *dev = bios->dev;5524struct bit_entry bitentry;55255526if (bit_table(dev, table->id, &bitentry) == 0)5527return table->parse_fn(dev, bios, &bitentry);55285529NV_INFO(dev, "BIT table '%c' not found\n", table->id);5530return -ENOSYS;5531}55325533static int5534parse_bit_structure(struct nvbios *bios, const uint16_t bitoffset)5535{5536int ret;55375538/*5539* The only restriction on parsing order currently is having 'i' first5540* for use of bios->*_version or bios->feature_byte while parsing;5541* functions shouldn't be actually *doing* anything apart from pulling5542* data from the image into the bios struct, thus no interdependencies5543*/5544ret = parse_bit_table(bios, bitoffset, &BIT_TABLE('i', i));5545if (ret) /* info? */5546return ret;5547if (bios->major_version >= 0x60) /* g80+ */5548parse_bit_table(bios, bitoffset, &BIT_TABLE('A', A));5549ret = parse_bit_table(bios, bitoffset, &BIT_TABLE('C', C));5550if (ret)5551return ret;5552parse_bit_table(bios, bitoffset, &BIT_TABLE('D', display));5553ret = parse_bit_table(bios, bitoffset, &BIT_TABLE('I', init));5554if (ret)5555return ret;5556parse_bit_table(bios, bitoffset, &BIT_TABLE('M', M)); /* memory? */5557parse_bit_table(bios, bitoffset, &BIT_TABLE('L', lvds));5558parse_bit_table(bios, bitoffset, &BIT_TABLE('T', tmds));5559parse_bit_table(bios, bitoffset, &BIT_TABLE('U', U));5560parse_bit_table(bios, bitoffset, &BIT_TABLE('d', displayport));55615562return 0;5563}55645565static int parse_bmp_structure(struct drm_device *dev, struct nvbios *bios, unsigned int offset)5566{5567/*5568* Parses the BMP structure for useful things, but does not act on them5569*5570* offset + 5: BMP major version5571* offset + 6: BMP minor version5572* offset + 9: BMP feature byte5573* offset + 10: BCD encoded BIOS version5574*5575* offset + 18: init script table pointer (for bios versions < 5.10h)5576* offset + 20: extra init script table pointer (for bios5577* versions < 5.10h)5578*5579* offset + 24: memory init table pointer (used on early bios versions)5580* offset + 26: SDR memory sequencing setup data table5581* offset + 28: DDR memory sequencing setup data table5582*5583* offset + 54: index of I2C CRTC pair to use for CRT output5584* offset + 55: index of I2C CRTC pair to use for TV output5585* offset + 56: index of I2C CRTC pair to use for flat panel output5586* offset + 58: write CRTC index for I2C pair 05587* offset + 59: read CRTC index for I2C pair 05588* offset + 60: write CRTC index for I2C pair 15589* offset + 61: read CRTC index for I2C pair 15590*5591* offset + 67: maximum internal PLL frequency (single stage PLL)5592* offset + 71: minimum internal PLL frequency (single stage PLL)5593*5594* offset + 75: script table pointers, as described in5595* parse_script_table_pointers5596*5597* offset + 89: TMDS single link output A table pointer5598* offset + 91: TMDS single link output B table pointer5599* offset + 95: LVDS single link output A table pointer5600* offset + 105: flat panel timings table pointer5601* offset + 107: flat panel strapping translation table pointer5602* offset + 117: LVDS manufacturer panel config table pointer5603* offset + 119: LVDS manufacturer strapping translation table pointer5604*5605* offset + 142: PLL limits table pointer5606*5607* offset + 156: minimum pixel clock for LVDS dual link5608*/56095610uint8_t *bmp = &bios->data[offset], bmp_version_major, bmp_version_minor;5611uint16_t bmplength;5612uint16_t legacy_scripts_offset, legacy_i2c_offset;56135614/* load needed defaults in case we can't parse this info */5615bios->dcb.i2c[0].write = NV_CIO_CRE_DDC_WR__INDEX;5616bios->dcb.i2c[0].read = NV_CIO_CRE_DDC_STATUS__INDEX;5617bios->dcb.i2c[1].write = NV_CIO_CRE_DDC0_WR__INDEX;5618bios->dcb.i2c[1].read = NV_CIO_CRE_DDC0_STATUS__INDEX;5619bios->digital_min_front_porch = 0x4b;5620bios->fmaxvco = 256000;5621bios->fminvco = 128000;5622bios->fp.duallink_transition_clk = 90000;56235624bmp_version_major = bmp[5];5625bmp_version_minor = bmp[6];56265627NV_TRACE(dev, "BMP version %d.%d\n",5628bmp_version_major, bmp_version_minor);56295630/*5631* Make sure that 0x36 is blank and can't be mistaken for a DCB5632* pointer on early versions5633*/5634if (bmp_version_major < 5)5635*(uint16_t *)&bios->data[0x36] = 0;56365637/*5638* Seems that the minor version was 1 for all major versions prior5639* to 5. Version 6 could theoretically exist, but I suspect BIT5640* happened instead.5641*/5642if ((bmp_version_major < 5 && bmp_version_minor != 1) || bmp_version_major > 5) {5643NV_ERROR(dev, "You have an unsupported BMP version. "5644"Please send in your bios\n");5645return -ENOSYS;5646}56475648if (bmp_version_major == 0)5649/* nothing that's currently useful in this version */5650return 0;5651else if (bmp_version_major == 1)5652bmplength = 44; /* exact for 1.01 */5653else if (bmp_version_major == 2)5654bmplength = 48; /* exact for 2.01 */5655else if (bmp_version_major == 3)5656bmplength = 54;5657/* guessed - mem init tables added in this version */5658else if (bmp_version_major == 4 || bmp_version_minor < 0x1)5659/* don't know if 5.0 exists... */5660bmplength = 62;5661/* guessed - BMP I2C indices added in version 4*/5662else if (bmp_version_minor < 0x6)5663bmplength = 67; /* exact for 5.01 */5664else if (bmp_version_minor < 0x10)5665bmplength = 75; /* exact for 5.06 */5666else if (bmp_version_minor == 0x10)5667bmplength = 89; /* exact for 5.10h */5668else if (bmp_version_minor < 0x14)5669bmplength = 118; /* exact for 5.11h */5670else if (bmp_version_minor < 0x24)5671/*5672* Not sure of version where pll limits came in;5673* certainly exist by 0x24 though.5674*/5675/* length not exact: this is long enough to get lvds members */5676bmplength = 123;5677else if (bmp_version_minor < 0x27)5678/*5679* Length not exact: this is long enough to get pll limit5680* member5681*/5682bmplength = 144;5683else5684/*5685* Length not exact: this is long enough to get dual link5686* transition clock.5687*/5688bmplength = 158;56895690/* checksum */5691if (nv_cksum(bmp, 8)) {5692NV_ERROR(dev, "Bad BMP checksum\n");5693return -EINVAL;5694}56955696/*5697* Bit 4 seems to indicate either a mobile bios or a quadro card --5698* mobile behaviour consistent (nv11+), quadro only seen nv18gl-nv36gl5699* (not nv10gl), bit 5 that the flat panel tables are present, and5700* bit 6 a tv bios.5701*/5702bios->feature_byte = bmp[9];57035704parse_bios_version(dev, bios, offset + 10);57055706if (bmp_version_major < 5 || bmp_version_minor < 0x10)5707bios->old_style_init = true;5708legacy_scripts_offset = 18;5709if (bmp_version_major < 2)5710legacy_scripts_offset -= 4;5711bios->init_script_tbls_ptr = ROM16(bmp[legacy_scripts_offset]);5712bios->extra_init_script_tbl_ptr = ROM16(bmp[legacy_scripts_offset + 2]);57135714if (bmp_version_major > 2) { /* appears in BMP 3 */5715bios->legacy.mem_init_tbl_ptr = ROM16(bmp[24]);5716bios->legacy.sdr_seq_tbl_ptr = ROM16(bmp[26]);5717bios->legacy.ddr_seq_tbl_ptr = ROM16(bmp[28]);5718}57195720legacy_i2c_offset = 0x48; /* BMP version 2 & 3 */5721if (bmplength > 61)5722legacy_i2c_offset = offset + 54;5723bios->legacy.i2c_indices.crt = bios->data[legacy_i2c_offset];5724bios->legacy.i2c_indices.tv = bios->data[legacy_i2c_offset + 1];5725bios->legacy.i2c_indices.panel = bios->data[legacy_i2c_offset + 2];5726if (bios->data[legacy_i2c_offset + 4])5727bios->dcb.i2c[0].write = bios->data[legacy_i2c_offset + 4];5728if (bios->data[legacy_i2c_offset + 5])5729bios->dcb.i2c[0].read = bios->data[legacy_i2c_offset + 5];5730if (bios->data[legacy_i2c_offset + 6])5731bios->dcb.i2c[1].write = bios->data[legacy_i2c_offset + 6];5732if (bios->data[legacy_i2c_offset + 7])5733bios->dcb.i2c[1].read = bios->data[legacy_i2c_offset + 7];57345735if (bmplength > 74) {5736bios->fmaxvco = ROM32(bmp[67]);5737bios->fminvco = ROM32(bmp[71]);5738}5739if (bmplength > 88)5740parse_script_table_pointers(bios, offset + 75);5741if (bmplength > 94) {5742bios->tmds.output0_script_ptr = ROM16(bmp[89]);5743bios->tmds.output1_script_ptr = ROM16(bmp[91]);5744/*5745* Never observed in use with lvds scripts, but is reused for5746* 18/24 bit panel interface default for EDID equipped panels5747* (if_is_24bit not set directly to avoid any oscillation).5748*/5749bios->legacy.lvds_single_a_script_ptr = ROM16(bmp[95]);5750}5751if (bmplength > 108) {5752bios->fp.fptablepointer = ROM16(bmp[105]);5753bios->fp.fpxlatetableptr = ROM16(bmp[107]);5754bios->fp.xlatwidth = 1;5755}5756if (bmplength > 120) {5757bios->fp.lvdsmanufacturerpointer = ROM16(bmp[117]);5758bios->fp.fpxlatemanufacturertableptr = ROM16(bmp[119]);5759}5760if (bmplength > 143)5761bios->pll_limit_tbl_ptr = ROM16(bmp[142]);57625763if (bmplength > 157)5764bios->fp.duallink_transition_clk = ROM16(bmp[156]) * 10;57655766return 0;5767}57685769static uint16_t findstr(uint8_t *data, int n, const uint8_t *str, int len)5770{5771int i, j;57725773for (i = 0; i <= (n - len); i++) {5774for (j = 0; j < len; j++)5775if (data[i + j] != str[j])5776break;5777if (j == len)5778return i;5779}57805781return 0;5782}57835784static struct dcb_gpio_entry *5785new_gpio_entry(struct nvbios *bios)5786{5787struct drm_device *dev = bios->dev;5788struct dcb_gpio_table *gpio = &bios->dcb.gpio;57895790if (gpio->entries >= DCB_MAX_NUM_GPIO_ENTRIES) {5791NV_ERROR(dev, "exceeded maximum number of gpio entries!!\n");5792return NULL;5793}57945795return &gpio->entry[gpio->entries++];5796}57975798struct dcb_gpio_entry *5799nouveau_bios_gpio_entry(struct drm_device *dev, enum dcb_gpio_tag tag)5800{5801struct drm_nouveau_private *dev_priv = dev->dev_private;5802struct nvbios *bios = &dev_priv->vbios;5803int i;58045805for (i = 0; i < bios->dcb.gpio.entries; i++) {5806if (bios->dcb.gpio.entry[i].tag != tag)5807continue;58085809return &bios->dcb.gpio.entry[i];5810}58115812return NULL;5813}58145815static void5816parse_dcb_gpio_table(struct nvbios *bios)5817{5818struct drm_device *dev = bios->dev;5819struct dcb_gpio_entry *e;5820u8 headerlen, entries, recordlen;5821u8 *dcb, *gpio = NULL, *entry;5822int i;58235824dcb = ROMPTR(bios, bios->data[0x36]);5825if (dcb[0] >= 0x30) {5826gpio = ROMPTR(bios, dcb[10]);5827if (!gpio)5828goto no_table;58295830headerlen = gpio[1];5831entries = gpio[2];5832recordlen = gpio[3];5833} else5834if (dcb[0] >= 0x22 && dcb[-1] >= 0x13) {5835gpio = ROMPTR(bios, dcb[-15]);5836if (!gpio)5837goto no_table;58385839headerlen = 3;5840entries = gpio[2];5841recordlen = gpio[1];5842} else5843if (dcb[0] >= 0x22) {5844/* No GPIO table present, parse the TVDAC GPIO data. */5845uint8_t *tvdac_gpio = &dcb[-5];58465847if (tvdac_gpio[0] & 1) {5848e = new_gpio_entry(bios);5849e->tag = DCB_GPIO_TVDAC0;5850e->line = tvdac_gpio[1] >> 4;5851e->invert = tvdac_gpio[0] & 2;5852}58535854goto no_table;5855} else {5856NV_DEBUG(dev, "no/unknown gpio table on DCB 0x%02x\n", dcb[0]);5857goto no_table;5858}58595860entry = gpio + headerlen;5861for (i = 0; i < entries; i++, entry += recordlen) {5862e = new_gpio_entry(bios);5863if (!e)5864break;58655866if (gpio[0] < 0x40) {5867e->entry = ROM16(entry[0]);5868e->tag = (e->entry & 0x07e0) >> 5;5869if (e->tag == 0x3f) {5870bios->dcb.gpio.entries--;5871continue;5872}58735874e->line = (e->entry & 0x001f);5875e->invert = ((e->entry & 0xf800) >> 11) != 4;5876} else {5877e->entry = ROM32(entry[0]);5878e->tag = (e->entry & 0x0000ff00) >> 8;5879if (e->tag == 0xff) {5880bios->dcb.gpio.entries--;5881continue;5882}58835884e->line = (e->entry & 0x0000001f) >> 0;5885e->state_default = (e->entry & 0x01000000) >> 24;5886e->state[0] = (e->entry & 0x18000000) >> 27;5887e->state[1] = (e->entry & 0x60000000) >> 29;5888}5889}58905891no_table:5892/* Apple iMac G4 NV18 */5893if (nv_match_device(dev, 0x0189, 0x10de, 0x0010)) {5894e = new_gpio_entry(bios);5895if (e) {5896e->tag = DCB_GPIO_TVDAC0;5897e->line = 4;5898}5899}5900}59015902struct dcb_connector_table_entry *5903nouveau_bios_connector_entry(struct drm_device *dev, int index)5904{5905struct drm_nouveau_private *dev_priv = dev->dev_private;5906struct nvbios *bios = &dev_priv->vbios;5907struct dcb_connector_table_entry *cte;59085909if (index >= bios->dcb.connector.entries)5910return NULL;59115912cte = &bios->dcb.connector.entry[index];5913if (cte->type == 0xff)5914return NULL;59155916return cte;5917}59185919static enum dcb_connector_type5920divine_connector_type(struct nvbios *bios, int index)5921{5922struct dcb_table *dcb = &bios->dcb;5923unsigned encoders = 0, type = DCB_CONNECTOR_NONE;5924int i;59255926for (i = 0; i < dcb->entries; i++) {5927if (dcb->entry[i].connector == index)5928encoders |= (1 << dcb->entry[i].type);5929}59305931if (encoders & (1 << OUTPUT_DP)) {5932if (encoders & (1 << OUTPUT_TMDS))5933type = DCB_CONNECTOR_DP;5934else5935type = DCB_CONNECTOR_eDP;5936} else5937if (encoders & (1 << OUTPUT_TMDS)) {5938if (encoders & (1 << OUTPUT_ANALOG))5939type = DCB_CONNECTOR_DVI_I;5940else5941type = DCB_CONNECTOR_DVI_D;5942} else5943if (encoders & (1 << OUTPUT_ANALOG)) {5944type = DCB_CONNECTOR_VGA;5945} else5946if (encoders & (1 << OUTPUT_LVDS)) {5947type = DCB_CONNECTOR_LVDS;5948} else5949if (encoders & (1 << OUTPUT_TV)) {5950type = DCB_CONNECTOR_TV_0;5951}59525953return type;5954}59555956static void5957apply_dcb_connector_quirks(struct nvbios *bios, int idx)5958{5959struct dcb_connector_table_entry *cte = &bios->dcb.connector.entry[idx];5960struct drm_device *dev = bios->dev;59615962/* Gigabyte NX85T */5963if (nv_match_device(dev, 0x0421, 0x1458, 0x344c)) {5964if (cte->type == DCB_CONNECTOR_HDMI_1)5965cte->type = DCB_CONNECTOR_DVI_I;5966}5967}59685969static const u8 hpd_gpio[16] = {59700xff, 0x07, 0x08, 0xff, 0xff, 0x51, 0x52, 0xff,59710xff, 0xff, 0xff, 0xff, 0xff, 0x5e, 0x5f, 0x60,5972};59735974static void5975parse_dcb_connector_table(struct nvbios *bios)5976{5977struct drm_device *dev = bios->dev;5978struct dcb_connector_table *ct = &bios->dcb.connector;5979struct dcb_connector_table_entry *cte;5980uint8_t *conntab = &bios->data[bios->dcb.connector_table_ptr];5981uint8_t *entry;5982int i;59835984if (!bios->dcb.connector_table_ptr) {5985NV_DEBUG_KMS(dev, "No DCB connector table present\n");5986return;5987}59885989NV_INFO(dev, "DCB connector table: VHER 0x%02x %d %d %d\n",5990conntab[0], conntab[1], conntab[2], conntab[3]);5991if ((conntab[0] != 0x30 && conntab[0] != 0x40) ||5992(conntab[3] != 2 && conntab[3] != 4)) {5993NV_ERROR(dev, " Unknown! Please report.\n");5994return;5995}59965997ct->entries = conntab[2];59985999entry = conntab + conntab[1];6000cte = &ct->entry[0];6001for (i = 0; i < conntab[2]; i++, entry += conntab[3], cte++) {6002cte->index = i;6003if (conntab[3] == 2)6004cte->entry = ROM16(entry[0]);6005else6006cte->entry = ROM32(entry[0]);60076008cte->type = (cte->entry & 0x000000ff) >> 0;6009cte->index2 = (cte->entry & 0x00000f00) >> 8;60106011cte->gpio_tag = ffs((cte->entry & 0x07033000) >> 12);6012cte->gpio_tag = hpd_gpio[cte->gpio_tag];60136014if (cte->type == 0xff)6015continue;60166017apply_dcb_connector_quirks(bios, i);60186019NV_INFO(dev, " %d: 0x%08x: type 0x%02x idx %d tag 0x%02x\n",6020i, cte->entry, cte->type, cte->index, cte->gpio_tag);60216022/* check for known types, fallback to guessing the type6023* from attached encoders if we hit an unknown.6024*/6025switch (cte->type) {6026case DCB_CONNECTOR_VGA:6027case DCB_CONNECTOR_TV_0:6028case DCB_CONNECTOR_TV_1:6029case DCB_CONNECTOR_TV_3:6030case DCB_CONNECTOR_DVI_I:6031case DCB_CONNECTOR_DVI_D:6032case DCB_CONNECTOR_LVDS:6033case DCB_CONNECTOR_LVDS_SPWG:6034case DCB_CONNECTOR_DP:6035case DCB_CONNECTOR_eDP:6036case DCB_CONNECTOR_HDMI_0:6037case DCB_CONNECTOR_HDMI_1:6038break;6039default:6040cte->type = divine_connector_type(bios, cte->index);6041NV_WARN(dev, "unknown type, using 0x%02x\n", cte->type);6042break;6043}60446045if (nouveau_override_conntype) {6046int type = divine_connector_type(bios, cte->index);6047if (type != cte->type)6048NV_WARN(dev, " -> type 0x%02x\n", cte->type);6049}60506051}6052}60536054static struct dcb_entry *new_dcb_entry(struct dcb_table *dcb)6055{6056struct dcb_entry *entry = &dcb->entry[dcb->entries];60576058memset(entry, 0, sizeof(struct dcb_entry));6059entry->index = dcb->entries++;60606061return entry;6062}60636064static void fabricate_dcb_output(struct dcb_table *dcb, int type, int i2c,6065int heads, int or)6066{6067struct dcb_entry *entry = new_dcb_entry(dcb);60686069entry->type = type;6070entry->i2c_index = i2c;6071entry->heads = heads;6072if (type != OUTPUT_ANALOG)6073entry->location = !DCB_LOC_ON_CHIP; /* ie OFF CHIP */6074entry->or = or;6075}60766077static bool6078parse_dcb20_entry(struct drm_device *dev, struct dcb_table *dcb,6079uint32_t conn, uint32_t conf, struct dcb_entry *entry)6080{6081entry->type = conn & 0xf;6082entry->i2c_index = (conn >> 4) & 0xf;6083entry->heads = (conn >> 8) & 0xf;6084if (dcb->version >= 0x40)6085entry->connector = (conn >> 12) & 0xf;6086entry->bus = (conn >> 16) & 0xf;6087entry->location = (conn >> 20) & 0x3;6088entry->or = (conn >> 24) & 0xf;60896090switch (entry->type) {6091case OUTPUT_ANALOG:6092/*6093* Although the rest of a CRT conf dword is usually6094* zeros, mac biosen have stuff there so we must mask6095*/6096entry->crtconf.maxfreq = (dcb->version < 0x30) ?6097(conf & 0xffff) * 10 :6098(conf & 0xff) * 10000;6099break;6100case OUTPUT_LVDS:6101{6102uint32_t mask;6103if (conf & 0x1)6104entry->lvdsconf.use_straps_for_mode = true;6105if (dcb->version < 0x22) {6106mask = ~0xd;6107/*6108* The laptop in bug 14567 lies and claims to not use6109* straps when it does, so assume all DCB 2.0 laptops6110* use straps, until a broken EDID using one is produced6111*/6112entry->lvdsconf.use_straps_for_mode = true;6113/*6114* Both 0x4 and 0x8 show up in v2.0 tables; assume they6115* mean the same thing (probably wrong, but might work)6116*/6117if (conf & 0x4 || conf & 0x8)6118entry->lvdsconf.use_power_scripts = true;6119} else {6120mask = ~0x7;6121if (conf & 0x2)6122entry->lvdsconf.use_acpi_for_edid = true;6123if (conf & 0x4)6124entry->lvdsconf.use_power_scripts = true;6125entry->lvdsconf.sor.link = (conf & 0x00000030) >> 4;6126}6127if (conf & mask) {6128/*6129* Until we even try to use these on G8x, it's6130* useless reporting unknown bits. They all are.6131*/6132if (dcb->version >= 0x40)6133break;61346135NV_ERROR(dev, "Unknown LVDS configuration bits, "6136"please report\n");6137}6138break;6139}6140case OUTPUT_TV:6141{6142if (dcb->version >= 0x30)6143entry->tvconf.has_component_output = conf & (0x8 << 4);6144else6145entry->tvconf.has_component_output = false;61466147break;6148}6149case OUTPUT_DP:6150entry->dpconf.sor.link = (conf & 0x00000030) >> 4;6151entry->dpconf.link_bw = (conf & 0x00e00000) >> 21;6152switch ((conf & 0x0f000000) >> 24) {6153case 0xf:6154entry->dpconf.link_nr = 4;6155break;6156case 0x3:6157entry->dpconf.link_nr = 2;6158break;6159default:6160entry->dpconf.link_nr = 1;6161break;6162}6163break;6164case OUTPUT_TMDS:6165if (dcb->version >= 0x40)6166entry->tmdsconf.sor.link = (conf & 0x00000030) >> 4;6167else if (dcb->version >= 0x30)6168entry->tmdsconf.slave_addr = (conf & 0x00000700) >> 8;6169else if (dcb->version >= 0x22)6170entry->tmdsconf.slave_addr = (conf & 0x00000070) >> 4;61716172break;6173case OUTPUT_EOL:6174/* weird g80 mobile type that "nv" treats as a terminator */6175dcb->entries--;6176return false;6177default:6178break;6179}61806181if (dcb->version < 0x40) {6182/* Normal entries consist of a single bit, but dual link has6183* the next most significant bit set too6184*/6185entry->duallink_possible =6186((1 << (ffs(entry->or) - 1)) * 3 == entry->or);6187} else {6188entry->duallink_possible = (entry->sorconf.link == 3);6189}61906191/* unsure what DCB version introduces this, 3.0? */6192if (conf & 0x100000)6193entry->i2c_upper_default = true;61946195return true;6196}61976198static bool6199parse_dcb15_entry(struct drm_device *dev, struct dcb_table *dcb,6200uint32_t conn, uint32_t conf, struct dcb_entry *entry)6201{6202switch (conn & 0x0000000f) {6203case 0:6204entry->type = OUTPUT_ANALOG;6205break;6206case 1:6207entry->type = OUTPUT_TV;6208break;6209case 2:6210case 4:6211if (conn & 0x10)6212entry->type = OUTPUT_LVDS;6213else6214entry->type = OUTPUT_TMDS;6215break;6216case 3:6217entry->type = OUTPUT_LVDS;6218break;6219default:6220NV_ERROR(dev, "Unknown DCB type %d\n", conn & 0x0000000f);6221return false;6222}62236224entry->i2c_index = (conn & 0x0003c000) >> 14;6225entry->heads = ((conn & 0x001c0000) >> 18) + 1;6226entry->or = entry->heads; /* same as heads, hopefully safe enough */6227entry->location = (conn & 0x01e00000) >> 21;6228entry->bus = (conn & 0x0e000000) >> 25;6229entry->duallink_possible = false;62306231switch (entry->type) {6232case OUTPUT_ANALOG:6233entry->crtconf.maxfreq = (conf & 0xffff) * 10;6234break;6235case OUTPUT_TV:6236entry->tvconf.has_component_output = false;6237break;6238case OUTPUT_LVDS:6239if ((conn & 0x00003f00) >> 8 != 0x10)6240entry->lvdsconf.use_straps_for_mode = true;6241entry->lvdsconf.use_power_scripts = true;6242break;6243default:6244break;6245}62466247return true;6248}62496250static bool parse_dcb_entry(struct drm_device *dev, struct dcb_table *dcb,6251uint32_t conn, uint32_t conf)6252{6253struct dcb_entry *entry = new_dcb_entry(dcb);6254bool ret;62556256if (dcb->version >= 0x20)6257ret = parse_dcb20_entry(dev, dcb, conn, conf, entry);6258else6259ret = parse_dcb15_entry(dev, dcb, conn, conf, entry);6260if (!ret)6261return ret;62626263read_dcb_i2c_entry(dev, dcb->version, dcb->i2c_table,6264entry->i2c_index, &dcb->i2c[entry->i2c_index]);62656266return true;6267}62686269static6270void merge_like_dcb_entries(struct drm_device *dev, struct dcb_table *dcb)6271{6272/*6273* DCB v2.0 lists each output combination separately.6274* Here we merge compatible entries to have fewer outputs, with6275* more options6276*/62776278int i, newentries = 0;62796280for (i = 0; i < dcb->entries; i++) {6281struct dcb_entry *ient = &dcb->entry[i];6282int j;62836284for (j = i + 1; j < dcb->entries; j++) {6285struct dcb_entry *jent = &dcb->entry[j];62866287if (jent->type == 100) /* already merged entry */6288continue;62896290/* merge heads field when all other fields the same */6291if (jent->i2c_index == ient->i2c_index &&6292jent->type == ient->type &&6293jent->location == ient->location &&6294jent->or == ient->or) {6295NV_TRACE(dev, "Merging DCB entries %d and %d\n",6296i, j);6297ient->heads |= jent->heads;6298jent->type = 100; /* dummy value */6299}6300}6301}63026303/* Compact entries merged into others out of dcb */6304for (i = 0; i < dcb->entries; i++) {6305if (dcb->entry[i].type == 100)6306continue;63076308if (newentries != i) {6309dcb->entry[newentries] = dcb->entry[i];6310dcb->entry[newentries].index = newentries;6311}6312newentries++;6313}63146315dcb->entries = newentries;6316}63176318static bool6319apply_dcb_encoder_quirks(struct drm_device *dev, int idx, u32 *conn, u32 *conf)6320{6321struct drm_nouveau_private *dev_priv = dev->dev_private;6322struct dcb_table *dcb = &dev_priv->vbios.dcb;63236324/* Dell Precision M63006325* DCB entry 2: 02025312 000000106326* DCB entry 3: 02026312 000000206327*6328* Identical, except apparently a different connector on a6329* different SOR link. Not a clue how we're supposed to know6330* which one is in use if it even shares an i2c line...6331*6332* Ignore the connector on the second SOR link to prevent6333* nasty problems until this is sorted (assuming it's not a6334* VBIOS bug).6335*/6336if (nv_match_device(dev, 0x040d, 0x1028, 0x019b)) {6337if (*conn == 0x02026312 && *conf == 0x00000020)6338return false;6339}63406341/* GeForce3 Ti 2006342*6343* DCB reports an LVDS output that should be TMDS:6344* DCB entry 1: f2005014 ffffffff6345*/6346if (nv_match_device(dev, 0x0201, 0x1462, 0x8851)) {6347if (*conn == 0xf2005014 && *conf == 0xffffffff) {6348fabricate_dcb_output(dcb, OUTPUT_TMDS, 1, 1, 1);6349return false;6350}6351}63526353/* XFX GT-240X-YA6354*6355* So many things wrong here, replace the entire encoder table..6356*/6357if (nv_match_device(dev, 0x0ca3, 0x1682, 0x3003)) {6358if (idx == 0) {6359*conn = 0x02001300; /* VGA, connector 1 */6360*conf = 0x00000028;6361} else6362if (idx == 1) {6363*conn = 0x01010312; /* DVI, connector 0 */6364*conf = 0x00020030;6365} else6366if (idx == 2) {6367*conn = 0x01010310; /* VGA, connector 0 */6368*conf = 0x00000028;6369} else6370if (idx == 3) {6371*conn = 0x02022362; /* HDMI, connector 2 */6372*conf = 0x00020010;6373} else {6374*conn = 0x0000000e; /* EOL */6375*conf = 0x00000000;6376}6377}63786379return true;6380}63816382static void6383fabricate_dcb_encoder_table(struct drm_device *dev, struct nvbios *bios)6384{6385struct dcb_table *dcb = &bios->dcb;6386int all_heads = (nv_two_heads(dev) ? 3 : 1);63876388#ifdef __powerpc__6389/* Apple iMac G4 NV17 */6390if (of_machine_is_compatible("PowerMac4,5")) {6391fabricate_dcb_output(dcb, OUTPUT_TMDS, 0, all_heads, 1);6392fabricate_dcb_output(dcb, OUTPUT_ANALOG, 1, all_heads, 2);6393return;6394}6395#endif63966397/* Make up some sane defaults */6398fabricate_dcb_output(dcb, OUTPUT_ANALOG, LEGACY_I2C_CRT, 1, 1);63996400if (nv04_tv_identify(dev, bios->legacy.i2c_indices.tv) >= 0)6401fabricate_dcb_output(dcb, OUTPUT_TV, LEGACY_I2C_TV,6402all_heads, 0);64036404else if (bios->tmds.output0_script_ptr ||6405bios->tmds.output1_script_ptr)6406fabricate_dcb_output(dcb, OUTPUT_TMDS, LEGACY_I2C_PANEL,6407all_heads, 1);6408}64096410static int6411parse_dcb_table(struct drm_device *dev, struct nvbios *bios)6412{6413struct drm_nouveau_private *dev_priv = dev->dev_private;6414struct dcb_table *dcb = &bios->dcb;6415uint16_t dcbptr = 0, i2ctabptr = 0;6416uint8_t *dcbtable;6417uint8_t headerlen = 0x4, entries = DCB_MAX_NUM_ENTRIES;6418bool configblock = true;6419int recordlength = 8, confofs = 4;6420int i;64216422/* get the offset from 0x36 */6423if (dev_priv->card_type > NV_04) {6424dcbptr = ROM16(bios->data[0x36]);6425if (dcbptr == 0x0000)6426NV_WARN(dev, "No output data (DCB) found in BIOS\n");6427}64286429/* this situation likely means a really old card, pre DCB */6430if (dcbptr == 0x0) {6431fabricate_dcb_encoder_table(dev, bios);6432return 0;6433}64346435dcbtable = &bios->data[dcbptr];64366437/* get DCB version */6438dcb->version = dcbtable[0];6439NV_TRACE(dev, "Found Display Configuration Block version %d.%d\n",6440dcb->version >> 4, dcb->version & 0xf);64416442if (dcb->version >= 0x20) { /* NV17+ */6443uint32_t sig;64446445if (dcb->version >= 0x30) { /* NV40+ */6446headerlen = dcbtable[1];6447entries = dcbtable[2];6448recordlength = dcbtable[3];6449i2ctabptr = ROM16(dcbtable[4]);6450sig = ROM32(dcbtable[6]);6451dcb->gpio_table_ptr = ROM16(dcbtable[10]);6452dcb->connector_table_ptr = ROM16(dcbtable[20]);6453} else {6454i2ctabptr = ROM16(dcbtable[2]);6455sig = ROM32(dcbtable[4]);6456headerlen = 8;6457}64586459if (sig != 0x4edcbdcb) {6460NV_ERROR(dev, "Bad Display Configuration Block "6461"signature (%08X)\n", sig);6462return -EINVAL;6463}6464} else if (dcb->version >= 0x15) { /* some NV11 and NV20 */6465char sig[8] = { 0 };64666467strncpy(sig, (char *)&dcbtable[-7], 7);6468i2ctabptr = ROM16(dcbtable[2]);6469recordlength = 10;6470confofs = 6;64716472if (strcmp(sig, "DEV_REC")) {6473NV_ERROR(dev, "Bad Display Configuration Block "6474"signature (%s)\n", sig);6475return -EINVAL;6476}6477} else {6478/*6479* v1.4 (some NV15/16, NV11+) seems the same as v1.5, but always6480* has the same single (crt) entry, even when tv-out present, so6481* the conclusion is this version cannot really be used.6482* v1.2 tables (some NV6/10, and NV15+) normally have the same6483* 5 entries, which are not specific to the card and so no use.6484* v1.2 does have an I2C table that read_dcb_i2c_table can6485* handle, but cards exist (nv11 in #14821) with a bad i2c table6486* pointer, so use the indices parsed in parse_bmp_structure.6487* v1.1 (NV5+, maybe some NV4) is entirely unhelpful6488*/6489NV_TRACEWARN(dev, "No useful information in BIOS output table; "6490"adding all possible outputs\n");6491fabricate_dcb_encoder_table(dev, bios);6492return 0;6493}64946495if (!i2ctabptr)6496NV_WARN(dev, "No pointer to DCB I2C port table\n");6497else {6498dcb->i2c_table = &bios->data[i2ctabptr];6499if (dcb->version >= 0x30)6500dcb->i2c_default_indices = dcb->i2c_table[4];65016502/*6503* Parse the "management" I2C bus, used for hardware6504* monitoring and some external TMDS transmitters.6505*/6506if (dcb->version >= 0x22) {6507int idx = (dcb->version >= 0x40 ?6508dcb->i2c_default_indices & 0xf :65092);65106511read_dcb_i2c_entry(dev, dcb->version, dcb->i2c_table,6512idx, &dcb->i2c[idx]);6513}6514}65156516if (entries > DCB_MAX_NUM_ENTRIES)6517entries = DCB_MAX_NUM_ENTRIES;65186519for (i = 0; i < entries; i++) {6520uint32_t connection, config = 0;65216522connection = ROM32(dcbtable[headerlen + recordlength * i]);6523if (configblock)6524config = ROM32(dcbtable[headerlen + confofs + recordlength * i]);65256526/* seen on an NV11 with DCB v1.5 */6527if (connection == 0x00000000)6528break;65296530/* seen on an NV17 with DCB v2.0 */6531if (connection == 0xffffffff)6532break;65336534if ((connection & 0x0000000f) == 0x0000000f)6535continue;65366537if (!apply_dcb_encoder_quirks(dev, i, &connection, &config))6538continue;65396540NV_TRACEWARN(dev, "Raw DCB entry %d: %08x %08x\n",6541dcb->entries, connection, config);65426543if (!parse_dcb_entry(dev, dcb, connection, config))6544break;6545}65466547/*6548* apart for v2.1+ not being known for requiring merging, this6549* guarantees dcbent->index is the index of the entry in the rom image6550*/6551if (dcb->version < 0x21)6552merge_like_dcb_entries(dev, dcb);65536554if (!dcb->entries)6555return -ENXIO;65566557parse_dcb_gpio_table(bios);6558parse_dcb_connector_table(bios);6559return 0;6560}65616562static void6563fixup_legacy_connector(struct nvbios *bios)6564{6565struct dcb_table *dcb = &bios->dcb;6566int i, i2c, i2c_conn[DCB_MAX_NUM_I2C_ENTRIES] = { };65676568/*6569* DCB 3.0 also has the table in most cases, but there are some cards6570* where the table is filled with stub entries, and the DCB entriy6571* indices are all 0. We don't need the connector indices on pre-G806572* chips (yet?) so limit the use to DCB 4.0 and above.6573*/6574if (dcb->version >= 0x40)6575return;65766577dcb->connector.entries = 0;65786579/*6580* No known connector info before v3.0, so make it up. the rule here6581* is: anything on the same i2c bus is considered to be on the same6582* connector. any output without an associated i2c bus is assigned6583* its own unique connector index.6584*/6585for (i = 0; i < dcb->entries; i++) {6586/*6587* Ignore the I2C index for on-chip TV-out, as there6588* are cards with bogus values (nv31m in bug 23212),6589* and it's otherwise useless.6590*/6591if (dcb->entry[i].type == OUTPUT_TV &&6592dcb->entry[i].location == DCB_LOC_ON_CHIP)6593dcb->entry[i].i2c_index = 0xf;6594i2c = dcb->entry[i].i2c_index;65956596if (i2c_conn[i2c]) {6597dcb->entry[i].connector = i2c_conn[i2c] - 1;6598continue;6599}66006601dcb->entry[i].connector = dcb->connector.entries++;6602if (i2c != 0xf)6603i2c_conn[i2c] = dcb->connector.entries;6604}66056606/* Fake the connector table as well as just connector indices */6607for (i = 0; i < dcb->connector.entries; i++) {6608dcb->connector.entry[i].index = i;6609dcb->connector.entry[i].type = divine_connector_type(bios, i);6610dcb->connector.entry[i].gpio_tag = 0xff;6611}6612}66136614static void6615fixup_legacy_i2c(struct nvbios *bios)6616{6617struct dcb_table *dcb = &bios->dcb;6618int i;66196620for (i = 0; i < dcb->entries; i++) {6621if (dcb->entry[i].i2c_index == LEGACY_I2C_CRT)6622dcb->entry[i].i2c_index = bios->legacy.i2c_indices.crt;6623if (dcb->entry[i].i2c_index == LEGACY_I2C_PANEL)6624dcb->entry[i].i2c_index = bios->legacy.i2c_indices.panel;6625if (dcb->entry[i].i2c_index == LEGACY_I2C_TV)6626dcb->entry[i].i2c_index = bios->legacy.i2c_indices.tv;6627}6628}66296630static int load_nv17_hwsq_ucode_entry(struct drm_device *dev, struct nvbios *bios, uint16_t hwsq_offset, int entry)6631{6632/*6633* The header following the "HWSQ" signature has the number of entries,6634* and the entry size6635*6636* An entry consists of a dword to write to the sequencer control reg6637* (0x00001304), followed by the ucode bytes, written sequentially,6638* starting at reg 0x000014006639*/66406641uint8_t bytes_to_write;6642uint16_t hwsq_entry_offset;6643int i;66446645if (bios->data[hwsq_offset] <= entry) {6646NV_ERROR(dev, "Too few entries in HW sequencer table for "6647"requested entry\n");6648return -ENOENT;6649}66506651bytes_to_write = bios->data[hwsq_offset + 1];66526653if (bytes_to_write != 36) {6654NV_ERROR(dev, "Unknown HW sequencer entry size\n");6655return -EINVAL;6656}66576658NV_TRACE(dev, "Loading NV17 power sequencing microcode\n");66596660hwsq_entry_offset = hwsq_offset + 2 + entry * bytes_to_write;66616662/* set sequencer control */6663bios_wr32(bios, 0x00001304, ROM32(bios->data[hwsq_entry_offset]));6664bytes_to_write -= 4;66656666/* write ucode */6667for (i = 0; i < bytes_to_write; i += 4)6668bios_wr32(bios, 0x00001400 + i, ROM32(bios->data[hwsq_entry_offset + i + 4]));66696670/* twiddle NV_PBUS_DEBUG_4 */6671bios_wr32(bios, NV_PBUS_DEBUG_4, bios_rd32(bios, NV_PBUS_DEBUG_4) | 0x18);66726673return 0;6674}66756676static int load_nv17_hw_sequencer_ucode(struct drm_device *dev,6677struct nvbios *bios)6678{6679/*6680* BMP based cards, from NV17, need a microcode loading to correctly6681* control the GPIO etc for LVDS panels6682*6683* BIT based cards seem to do this directly in the init scripts6684*6685* The microcode entries are found by the "HWSQ" signature.6686*/66876688const uint8_t hwsq_signature[] = { 'H', 'W', 'S', 'Q' };6689const int sz = sizeof(hwsq_signature);6690int hwsq_offset;66916692hwsq_offset = findstr(bios->data, bios->length, hwsq_signature, sz);6693if (!hwsq_offset)6694return 0;66956696/* always use entry 0? */6697return load_nv17_hwsq_ucode_entry(dev, bios, hwsq_offset + sz, 0);6698}66996700uint8_t *nouveau_bios_embedded_edid(struct drm_device *dev)6701{6702struct drm_nouveau_private *dev_priv = dev->dev_private;6703struct nvbios *bios = &dev_priv->vbios;6704const uint8_t edid_sig[] = {67050x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00 };6706uint16_t offset = 0;6707uint16_t newoffset;6708int searchlen = NV_PROM_SIZE;67096710if (bios->fp.edid)6711return bios->fp.edid;67126713while (searchlen) {6714newoffset = findstr(&bios->data[offset], searchlen,6715edid_sig, 8);6716if (!newoffset)6717return NULL;6718offset += newoffset;6719if (!nv_cksum(&bios->data[offset], EDID1_LEN))6720break;67216722searchlen -= offset;6723offset++;6724}67256726NV_TRACE(dev, "Found EDID in BIOS\n");67276728return bios->fp.edid = &bios->data[offset];6729}67306731void6732nouveau_bios_run_init_table(struct drm_device *dev, uint16_t table,6733struct dcb_entry *dcbent)6734{6735struct drm_nouveau_private *dev_priv = dev->dev_private;6736struct nvbios *bios = &dev_priv->vbios;6737struct init_exec iexec = { true, false };67386739spin_lock_bh(&bios->lock);6740bios->display.output = dcbent;6741parse_init_table(bios, table, &iexec);6742bios->display.output = NULL;6743spin_unlock_bh(&bios->lock);6744}67456746static bool NVInitVBIOS(struct drm_device *dev)6747{6748struct drm_nouveau_private *dev_priv = dev->dev_private;6749struct nvbios *bios = &dev_priv->vbios;67506751memset(bios, 0, sizeof(struct nvbios));6752spin_lock_init(&bios->lock);6753bios->dev = dev;67546755if (!NVShadowVBIOS(dev, bios->data))6756return false;67576758bios->length = NV_PROM_SIZE;6759return true;6760}67616762static int nouveau_parse_vbios_struct(struct drm_device *dev)6763{6764struct drm_nouveau_private *dev_priv = dev->dev_private;6765struct nvbios *bios = &dev_priv->vbios;6766const uint8_t bit_signature[] = { 0xff, 0xb8, 'B', 'I', 'T' };6767const uint8_t bmp_signature[] = { 0xff, 0x7f, 'N', 'V', 0x0 };6768int offset;67696770offset = findstr(bios->data, bios->length,6771bit_signature, sizeof(bit_signature));6772if (offset) {6773NV_TRACE(dev, "BIT BIOS found\n");6774bios->type = NVBIOS_BIT;6775bios->offset = offset;6776return parse_bit_structure(bios, offset + 6);6777}67786779offset = findstr(bios->data, bios->length,6780bmp_signature, sizeof(bmp_signature));6781if (offset) {6782NV_TRACE(dev, "BMP BIOS found\n");6783bios->type = NVBIOS_BMP;6784bios->offset = offset;6785return parse_bmp_structure(dev, bios, offset);6786}67876788NV_ERROR(dev, "No known BIOS signature found\n");6789return -ENODEV;6790}67916792int6793nouveau_run_vbios_init(struct drm_device *dev)6794{6795struct drm_nouveau_private *dev_priv = dev->dev_private;6796struct nvbios *bios = &dev_priv->vbios;6797int i, ret = 0;67986799/* Reset the BIOS head to 0. */6800bios->state.crtchead = 0;68016802if (bios->major_version < 5) /* BMP only */6803load_nv17_hw_sequencer_ucode(dev, bios);68046805if (bios->execute) {6806bios->fp.last_script_invoc = 0;6807bios->fp.lvds_init_run = false;6808}68096810parse_init_tables(bios);68116812/*6813* Runs some additional script seen on G8x VBIOSen. The VBIOS'6814* parser will run this right after the init tables, the binary6815* driver appears to run it at some point later.6816*/6817if (bios->some_script_ptr) {6818struct init_exec iexec = {true, false};68196820NV_INFO(dev, "Parsing VBIOS init table at offset 0x%04X\n",6821bios->some_script_ptr);6822parse_init_table(bios, bios->some_script_ptr, &iexec);6823}68246825if (dev_priv->card_type >= NV_50) {6826for (i = 0; i < bios->dcb.entries; i++) {6827nouveau_bios_run_display_table(dev,6828&bios->dcb.entry[i],68290, 0);6830}6831}68326833return ret;6834}68356836static void6837nouveau_bios_i2c_devices_takedown(struct drm_device *dev)6838{6839struct drm_nouveau_private *dev_priv = dev->dev_private;6840struct nvbios *bios = &dev_priv->vbios;6841struct dcb_i2c_entry *entry;6842int i;68436844entry = &bios->dcb.i2c[0];6845for (i = 0; i < DCB_MAX_NUM_I2C_ENTRIES; i++, entry++)6846nouveau_i2c_fini(dev, entry);6847}68486849static bool6850nouveau_bios_posted(struct drm_device *dev)6851{6852struct drm_nouveau_private *dev_priv = dev->dev_private;6853unsigned htotal;68546855if (dev_priv->card_type >= NV_50) {6856if (NVReadVgaCrtc(dev, 0, 0x00) == 0 &&6857NVReadVgaCrtc(dev, 0, 0x1a) == 0)6858return false;6859return true;6860}68616862htotal = NVReadVgaCrtc(dev, 0, 0x06);6863htotal |= (NVReadVgaCrtc(dev, 0, 0x07) & 0x01) << 8;6864htotal |= (NVReadVgaCrtc(dev, 0, 0x07) & 0x20) << 4;6865htotal |= (NVReadVgaCrtc(dev, 0, 0x25) & 0x01) << 10;6866htotal |= (NVReadVgaCrtc(dev, 0, 0x41) & 0x01) << 11;68676868return (htotal != 0);6869}68706871int6872nouveau_bios_init(struct drm_device *dev)6873{6874struct drm_nouveau_private *dev_priv = dev->dev_private;6875struct nvbios *bios = &dev_priv->vbios;6876int ret;68776878if (!NVInitVBIOS(dev))6879return -ENODEV;68806881ret = nouveau_parse_vbios_struct(dev);6882if (ret)6883return ret;68846885ret = parse_dcb_table(dev, bios);6886if (ret)6887return ret;68886889fixup_legacy_i2c(bios);6890fixup_legacy_connector(bios);68916892if (!bios->major_version) /* we don't run version 0 bios */6893return 0;68946895/* init script execution disabled */6896bios->execute = false;68976898/* ... unless card isn't POSTed already */6899if (!nouveau_bios_posted(dev)) {6900NV_INFO(dev, "Adaptor not initialised, "6901"running VBIOS init tables.\n");6902bios->execute = true;6903}6904if (nouveau_force_post)6905bios->execute = true;69066907ret = nouveau_run_vbios_init(dev);6908if (ret)6909return ret;69106911/* feature_byte on BMP is poor, but init always sets CR4B */6912if (bios->major_version < 5)6913bios->is_mobile = NVReadVgaCrtc(dev, 0, NV_CIO_CRE_4B) & 0x40;69146915/* all BIT systems need p_f_m_t for digital_min_front_porch */6916if (bios->is_mobile || bios->major_version >= 5)6917ret = parse_fp_mode_table(dev, bios);69186919/* allow subsequent scripts to execute */6920bios->execute = true;69216922return 0;6923}69246925void6926nouveau_bios_takedown(struct drm_device *dev)6927{6928nouveau_bios_i2c_devices_takedown(dev);6929}693069316932