2 * Copyright 2005-2006 Erik Waling
3 * Copyright 2006 Stephane Marchesin
4 * Copyright 2007-2009 Stuart Bennett
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
21 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 #define NV_DEBUG_NOTRACE
27 #include "nouveau_drv.h"
28 #include "nouveau_hw.h"
29 #include "nouveau_encoder.h"
31 #include <linux/io-mapping.h>
33 /* these defines are made up */
34 #define NV_CIO_CRE_44_HEADA 0x0
35 #define NV_CIO_CRE_44_HEADB 0x3
36 #define FEATURE_MOBILE 0x10 /* also FEATURE_QUADRO for BMP */
37 #define LEGACY_I2C_CRT 0x80
38 #define LEGACY_I2C_PANEL 0x81
39 #define LEGACY_I2C_TV 0x82
43 #define BIOSLOG(sip, fmt, arg...) NV_DEBUG(sip->dev, fmt, ##arg)
44 #define LOG_OLD_VALUE(x)
51 static bool nv_cksum(const uint8_t *data, unsigned int length)
54 * There's a few checksums in the BIOS, so here's a generic checking
60 for (i = 0; i < length; i++)
70 score_vbios(struct drm_device *dev, const uint8_t *data, const bool writeable)
72 if (!(data[0] == 0x55 && data[1] == 0xAA)) {
73 NV_TRACEWARN(dev, "... BIOS signature not found\n");
77 if (nv_cksum(data, data[2] * 512)) {
78 NV_TRACEWARN(dev, "... BIOS checksum invalid\n");
79 /* if a ro image is somewhat bad, it's probably all rubbish */
80 return writeable ? 2 : 1;
82 NV_TRACE(dev, "... appears to be valid\n");
87 static void load_vbios_prom(struct drm_device *dev, uint8_t *data)
89 struct drm_nouveau_private *dev_priv = dev->dev_private;
90 uint32_t pci_nv_20, save_pci_nv_20;
94 if (dev_priv->card_type >= NV_50)
97 pci_nv_20 = NV_PBUS_PCI_NV_20;
99 /* enable ROM access */
100 save_pci_nv_20 = nvReadMC(dev, pci_nv_20);
101 nvWriteMC(dev, pci_nv_20,
102 save_pci_nv_20 & ~NV_PBUS_PCI_NV_20_ROM_SHADOW_ENABLED);
104 /* bail if no rom signature */
105 if (nv_rd08(dev, NV_PROM_OFFSET) != 0x55 ||
106 nv_rd08(dev, NV_PROM_OFFSET + 1) != 0xaa)
109 /* additional check (see note below) - read PCI record header */
110 pcir_ptr = nv_rd08(dev, NV_PROM_OFFSET + 0x18) |
111 nv_rd08(dev, NV_PROM_OFFSET + 0x19) << 8;
112 if (nv_rd08(dev, NV_PROM_OFFSET + pcir_ptr) != 'P' ||
113 nv_rd08(dev, NV_PROM_OFFSET + pcir_ptr + 1) != 'C' ||
114 nv_rd08(dev, NV_PROM_OFFSET + pcir_ptr + 2) != 'I' ||
115 nv_rd08(dev, NV_PROM_OFFSET + pcir_ptr + 3) != 'R')
118 /* on some 6600GT/6800LE prom reads are messed up. nvclock alleges a
119 * a good read may be obtained by waiting or re-reading (cargocult: 5x)
120 * each byte. we'll hope pramin has something usable instead
122 for (i = 0; i < NV_PROM_SIZE; i++)
123 data[i] = nv_rd08(dev, NV_PROM_OFFSET + i);
126 /* disable ROM access */
127 nvWriteMC(dev, pci_nv_20,
128 save_pci_nv_20 | NV_PBUS_PCI_NV_20_ROM_SHADOW_ENABLED);
131 static void load_vbios_pramin(struct drm_device *dev, uint8_t *data)
133 struct drm_nouveau_private *dev_priv = dev->dev_private;
134 uint32_t old_bar0_pramin = 0;
137 if (dev_priv->card_type >= NV_50) {
138 uint32_t vbios_vram = (nv_rd32(dev, 0x619f04) & ~0xff) << 8;
141 vbios_vram = (nv_rd32(dev, 0x1700) << 16) + 0xf0000;
143 old_bar0_pramin = nv_rd32(dev, 0x1700);
144 nv_wr32(dev, 0x1700, vbios_vram >> 16);
147 /* bail if no rom signature */
148 if (nv_rd08(dev, NV_PRAMIN_OFFSET) != 0x55 ||
149 nv_rd08(dev, NV_PRAMIN_OFFSET + 1) != 0xaa)
152 for (i = 0; i < NV_PROM_SIZE; i++)
153 data[i] = nv_rd08(dev, NV_PRAMIN_OFFSET + i);
156 if (dev_priv->card_type >= NV_50)
157 nv_wr32(dev, 0x1700, old_bar0_pramin);
160 static void load_vbios_pci(struct drm_device *dev, uint8_t *data)
162 void __iomem *rom = NULL;
166 ret = pci_enable_rom(dev->pdev);
170 rom = pci_map_rom(dev->pdev, &rom_len);
173 memcpy_fromio(data, rom, rom_len);
174 pci_unmap_rom(dev->pdev, rom);
177 pci_disable_rom(dev->pdev);
180 static void load_vbios_acpi(struct drm_device *dev, uint8_t *data)
184 int size = 64 * 1024;
186 if (!nouveau_acpi_rom_supported(dev->pdev))
189 for (i = 0; i < (size / ROM_BIOS_PAGE); i++) {
190 ret = nouveau_acpi_get_bios_chunk(data,
201 void (*loadbios)(struct drm_device *, uint8_t *);
205 static struct methods shadow_methods[] = {
206 { "PRAMIN", load_vbios_pramin, true },
207 { "PROM", load_vbios_prom, false },
208 { "PCIROM", load_vbios_pci, true },
209 { "ACPI", load_vbios_acpi, true },
211 #define NUM_SHADOW_METHODS ARRAY_SIZE(shadow_methods)
213 static bool NVShadowVBIOS(struct drm_device *dev, uint8_t *data)
215 struct methods *methods = shadow_methods;
217 int scores[NUM_SHADOW_METHODS], i;
220 for (i = 0; i < NUM_SHADOW_METHODS; i++)
221 if (!strcasecmp(nouveau_vbios, methods[i].desc))
224 if (i < NUM_SHADOW_METHODS) {
225 NV_INFO(dev, "Attempting to use BIOS image from %s\n",
228 methods[i].loadbios(dev, data);
229 if (score_vbios(dev, data, methods[i].rw))
233 NV_ERROR(dev, "VBIOS source \'%s\' invalid\n", nouveau_vbios);
236 for (i = 0; i < NUM_SHADOW_METHODS; i++) {
237 NV_TRACE(dev, "Attempting to load BIOS image from %s\n",
239 data[0] = data[1] = 0; /* avoid reuse of previous image */
240 methods[i].loadbios(dev, data);
241 scores[i] = score_vbios(dev, data, methods[i].rw);
242 if (scores[i] == testscore)
246 while (--testscore > 0) {
247 for (i = 0; i < NUM_SHADOW_METHODS; i++) {
248 if (scores[i] == testscore) {
249 NV_TRACE(dev, "Using BIOS image from %s\n",
251 methods[i].loadbios(dev, data);
257 NV_ERROR(dev, "No valid BIOS image found\n");
261 struct init_tbl_entry {
265 * > 0: success, length of opcode
266 * 0: success, but abort further parsing of table (INIT_DONE etc)
267 * < 0: failure, table parsing will be aborted
269 int (*handler)(struct nvbios *, uint16_t, struct init_exec *);
272 static int parse_init_table(struct nvbios *, unsigned int, struct init_exec *);
274 #define MACRO_INDEX_SIZE 2
276 #define CONDITION_SIZE 12
277 #define IO_FLAG_CONDITION_SIZE 9
278 #define IO_CONDITION_SIZE 5
279 #define MEM_INIT_SIZE 66
281 static void still_alive(void)
290 munge_reg(struct nvbios *bios, uint32_t reg)
292 struct drm_nouveau_private *dev_priv = bios->dev->dev_private;
293 struct dcb_entry *dcbent = bios->display.output;
295 if (dev_priv->card_type < NV_50)
298 if (reg & 0x40000000) {
301 reg += (ffs(dcbent->or) - 1) * 0x800;
302 if ((reg & 0x20000000) && !(dcbent->sorconf.link & 1))
311 valid_reg(struct nvbios *bios, uint32_t reg)
313 struct drm_nouveau_private *dev_priv = bios->dev->dev_private;
314 struct drm_device *dev = bios->dev;
316 /* C51 has misaligned regs on purpose. Marvellous */
318 (reg & 0x1 && dev_priv->vbios.chip_version != 0x51))
319 NV_ERROR(dev, "======= misaligned reg 0x%08X =======\n", reg);
321 /* warn on C51 regs that haven't been verified accessible in tracing */
322 if (reg & 0x1 && dev_priv->vbios.chip_version == 0x51 &&
323 reg != 0x130d && reg != 0x1311 && reg != 0x60081d)
324 NV_WARN(dev, "=== C51 misaligned reg 0x%08X not verified ===\n",
327 if (reg >= (8*1024*1024)) {
328 NV_ERROR(dev, "=== reg 0x%08x out of mapped bounds ===\n", reg);
336 valid_idx_port(struct nvbios *bios, uint16_t port)
338 struct drm_nouveau_private *dev_priv = bios->dev->dev_private;
339 struct drm_device *dev = bios->dev;
342 * If adding more ports here, the read/write functions below will need
343 * updating so that the correct mmio range (PRMCIO, PRMDIO, PRMVIO) is
344 * used for the port in question
346 if (dev_priv->card_type < NV_50) {
347 if (port == NV_CIO_CRX__COLOR)
349 if (port == NV_VIO_SRX)
352 if (port == NV_CIO_CRX__COLOR)
356 NV_ERROR(dev, "========== unknown indexed io port 0x%04X ==========\n",
363 valid_port(struct nvbios *bios, uint16_t port)
365 struct drm_device *dev = bios->dev;
368 * If adding more ports here, the read/write functions below will need
369 * updating so that the correct mmio range (PRMCIO, PRMDIO, PRMVIO) is
370 * used for the port in question
372 if (port == NV_VIO_VSE2)
375 NV_ERROR(dev, "========== unknown io port 0x%04X ==========\n", port);
381 bios_rd32(struct nvbios *bios, uint32_t reg)
385 reg = munge_reg(bios, reg);
386 if (!valid_reg(bios, reg))
390 * C51 sometimes uses regs with bit0 set in the address. For these
391 * cases there should exist a translation in a BIOS table to an IO
392 * port address which the BIOS uses for accessing the reg
394 * These only seem to appear for the power control regs to a flat panel,
395 * and the GPIO regs at 0x60081*. In C51 mmio traces the normal regs
396 * for 0x1308 and 0x1310 are used - hence the mask below. An S3
397 * suspend-resume mmio trace from a C51 will be required to see if this
398 * is true for the power microcode in 0x14.., or whether the direct IO
399 * port access method is needed
404 data = nv_rd32(bios->dev, reg);
406 BIOSLOG(bios, " Read: Reg: 0x%08X, Data: 0x%08X\n", reg, data);
412 bios_wr32(struct nvbios *bios, uint32_t reg, uint32_t data)
414 struct drm_nouveau_private *dev_priv = bios->dev->dev_private;
416 reg = munge_reg(bios, reg);
417 if (!valid_reg(bios, reg))
420 /* see note in bios_rd32 */
424 LOG_OLD_VALUE(bios_rd32(bios, reg));
425 BIOSLOG(bios, " Write: Reg: 0x%08X, Data: 0x%08X\n", reg, data);
427 if (dev_priv->vbios.execute) {
429 nv_wr32(bios->dev, reg, data);
434 bios_idxprt_rd(struct nvbios *bios, uint16_t port, uint8_t index)
436 struct drm_nouveau_private *dev_priv = bios->dev->dev_private;
437 struct drm_device *dev = bios->dev;
440 if (!valid_idx_port(bios, port))
443 if (dev_priv->card_type < NV_50) {
444 if (port == NV_VIO_SRX)
445 data = NVReadVgaSeq(dev, bios->state.crtchead, index);
446 else /* assume NV_CIO_CRX__COLOR */
447 data = NVReadVgaCrtc(dev, bios->state.crtchead, index);
451 data32 = bios_rd32(bios, NV50_PDISPLAY_VGACRTC(index & ~3));
452 data = (data32 >> ((index & 3) << 3)) & 0xff;
455 BIOSLOG(bios, " Indexed IO read: Port: 0x%04X, Index: 0x%02X, "
456 "Head: 0x%02X, Data: 0x%02X\n",
457 port, index, bios->state.crtchead, data);
462 bios_idxprt_wr(struct nvbios *bios, uint16_t port, uint8_t index, uint8_t data)
464 struct drm_nouveau_private *dev_priv = bios->dev->dev_private;
465 struct drm_device *dev = bios->dev;
467 if (!valid_idx_port(bios, port))
471 * The current head is maintained in the nvbios member state.crtchead.
472 * We trap changes to CR44 and update the head variable and hence the
473 * register set written.
474 * As CR44 only exists on CRTC0, we update crtchead to head0 in advance
475 * of the write, and to head1 after the write
477 if (port == NV_CIO_CRX__COLOR && index == NV_CIO_CRE_44 &&
478 data != NV_CIO_CRE_44_HEADB)
479 bios->state.crtchead = 0;
481 LOG_OLD_VALUE(bios_idxprt_rd(bios, port, index));
482 BIOSLOG(bios, " Indexed IO write: Port: 0x%04X, Index: 0x%02X, "
483 "Head: 0x%02X, Data: 0x%02X\n",
484 port, index, bios->state.crtchead, data);
486 if (bios->execute && dev_priv->card_type < NV_50) {
488 if (port == NV_VIO_SRX)
489 NVWriteVgaSeq(dev, bios->state.crtchead, index, data);
490 else /* assume NV_CIO_CRX__COLOR */
491 NVWriteVgaCrtc(dev, bios->state.crtchead, index, data);
494 uint32_t data32, shift = (index & 3) << 3;
498 data32 = bios_rd32(bios, NV50_PDISPLAY_VGACRTC(index & ~3));
499 data32 &= ~(0xff << shift);
500 data32 |= (data << shift);
501 bios_wr32(bios, NV50_PDISPLAY_VGACRTC(index & ~3), data32);
504 if (port == NV_CIO_CRX__COLOR &&
505 index == NV_CIO_CRE_44 && data == NV_CIO_CRE_44_HEADB)
506 bios->state.crtchead = 1;
510 bios_port_rd(struct nvbios *bios, uint16_t port)
512 uint8_t data, head = bios->state.crtchead;
514 if (!valid_port(bios, port))
517 data = NVReadPRMVIO(bios->dev, head, NV_PRMVIO0_OFFSET + port);
519 BIOSLOG(bios, " IO read: Port: 0x%04X, Head: 0x%02X, Data: 0x%02X\n",
526 bios_port_wr(struct nvbios *bios, uint16_t port, uint8_t data)
528 int head = bios->state.crtchead;
530 if (!valid_port(bios, port))
533 LOG_OLD_VALUE(bios_port_rd(bios, port));
534 BIOSLOG(bios, " IO write: Port: 0x%04X, Head: 0x%02X, Data: 0x%02X\n",
541 NVWritePRMVIO(bios->dev, head, NV_PRMVIO0_OFFSET + port, data);
545 io_flag_condition_met(struct nvbios *bios, uint16_t offset, uint8_t cond)
548 * The IO flag condition entry has 2 bytes for the CRTC port; 1 byte
549 * for the CRTC index; 1 byte for the mask to apply to the value
550 * retrieved from the CRTC; 1 byte for the shift right to apply to the
551 * masked CRTC value; 2 bytes for the offset to the flag array, to
552 * which the shifted value is added; 1 byte for the mask applied to the
553 * value read from the flag array; and 1 byte for the value to compare
554 * against the masked byte from the flag table.
557 uint16_t condptr = bios->io_flag_condition_tbl_ptr + cond * IO_FLAG_CONDITION_SIZE;
558 uint16_t crtcport = ROM16(bios->data[condptr]);
559 uint8_t crtcindex = bios->data[condptr + 2];
560 uint8_t mask = bios->data[condptr + 3];
561 uint8_t shift = bios->data[condptr + 4];
562 uint16_t flagarray = ROM16(bios->data[condptr + 5]);
563 uint8_t flagarraymask = bios->data[condptr + 7];
564 uint8_t cmpval = bios->data[condptr + 8];
567 BIOSLOG(bios, "0x%04X: Port: 0x%04X, Index: 0x%02X, Mask: 0x%02X, "
568 "Shift: 0x%02X, FlagArray: 0x%04X, FAMask: 0x%02X, "
570 offset, crtcport, crtcindex, mask, shift, flagarray, flagarraymask, cmpval);
572 data = bios_idxprt_rd(bios, crtcport, crtcindex);
574 data = bios->data[flagarray + ((data & mask) >> shift)];
575 data &= flagarraymask;
577 BIOSLOG(bios, "0x%04X: Checking if 0x%02X equals 0x%02X\n",
578 offset, data, cmpval);
580 return (data == cmpval);
584 bios_condition_met(struct nvbios *bios, uint16_t offset, uint8_t cond)
587 * The condition table entry has 4 bytes for the address of the
588 * register to check, 4 bytes for a mask to apply to the register and
589 * 4 for a test comparison value
592 uint16_t condptr = bios->condition_tbl_ptr + cond * CONDITION_SIZE;
593 uint32_t reg = ROM32(bios->data[condptr]);
594 uint32_t mask = ROM32(bios->data[condptr + 4]);
595 uint32_t cmpval = ROM32(bios->data[condptr + 8]);
598 BIOSLOG(bios, "0x%04X: Cond: 0x%02X, Reg: 0x%08X, Mask: 0x%08X\n",
599 offset, cond, reg, mask);
601 data = bios_rd32(bios, reg) & mask;
603 BIOSLOG(bios, "0x%04X: Checking if 0x%08X equals 0x%08X\n",
604 offset, data, cmpval);
606 return (data == cmpval);
610 io_condition_met(struct nvbios *bios, uint16_t offset, uint8_t cond)
613 * The IO condition entry has 2 bytes for the IO port address; 1 byte
614 * for the index to write to io_port; 1 byte for the mask to apply to
615 * the byte read from io_port+1; and 1 byte for the value to compare
616 * against the masked byte.
619 uint16_t condptr = bios->io_condition_tbl_ptr + cond * IO_CONDITION_SIZE;
620 uint16_t io_port = ROM16(bios->data[condptr]);
621 uint8_t port_index = bios->data[condptr + 2];
622 uint8_t mask = bios->data[condptr + 3];
623 uint8_t cmpval = bios->data[condptr + 4];
625 uint8_t data = bios_idxprt_rd(bios, io_port, port_index) & mask;
627 BIOSLOG(bios, "0x%04X: Checking if 0x%02X equals 0x%02X\n",
628 offset, data, cmpval);
630 return (data == cmpval);
634 nv50_pll_set(struct drm_device *dev, uint32_t reg, uint32_t clk)
636 struct drm_nouveau_private *dev_priv = dev->dev_private;
637 uint32_t reg0 = nv_rd32(dev, reg + 0);
638 uint32_t reg1 = nv_rd32(dev, reg + 4);
639 struct nouveau_pll_vals pll;
640 struct pll_lims pll_limits;
643 ret = get_pll_limits(dev, reg, &pll_limits);
647 clk = nouveau_calc_pll_mnp(dev, &pll_limits, clk, &pll);
651 reg0 = (reg0 & 0xfff8ffff) | (pll.log2P << 16);
652 reg1 = (reg1 & 0xffff0000) | (pll.N1 << 8) | pll.M1;
654 if (dev_priv->vbios.execute) {
656 nv_wr32(dev, reg + 4, reg1);
657 nv_wr32(dev, reg + 0, reg0);
664 setPLL(struct nvbios *bios, uint32_t reg, uint32_t clk)
666 struct drm_device *dev = bios->dev;
667 struct drm_nouveau_private *dev_priv = dev->dev_private;
669 struct pll_lims pll_lim;
670 struct nouveau_pll_vals pllvals;
673 if (dev_priv->card_type >= NV_50)
674 return nv50_pll_set(dev, reg, clk);
676 /* high regs (such as in the mac g5 table) are not -= 4 */
677 ret = get_pll_limits(dev, reg > 0x405c ? reg : reg - 4, &pll_lim);
681 clk = nouveau_calc_pll_mnp(dev, &pll_lim, clk, &pllvals);
687 nouveau_hw_setpll(dev, reg, &pllvals);
693 static int dcb_entry_idx_from_crtchead(struct drm_device *dev)
695 struct drm_nouveau_private *dev_priv = dev->dev_private;
696 struct nvbios *bios = &dev_priv->vbios;
699 * For the results of this function to be correct, CR44 must have been
700 * set (using bios_idxprt_wr to set crtchead), CR58 set for CR57 = 0,
701 * and the DCB table parsed, before the script calling the function is
702 * run. run_digital_op_script is example of how to do such setup
705 uint8_t dcb_entry = NVReadVgaCrtc5758(dev, bios->state.crtchead, 0);
707 if (dcb_entry > bios->dcb.entries) {
708 NV_ERROR(dev, "CR58 doesn't have a valid DCB entry currently "
709 "(%02X)\n", dcb_entry);
710 dcb_entry = 0x7f; /* unused / invalid marker */
717 read_dcb_i2c_entry(struct drm_device *dev, int dcb_version, uint8_t *i2ctable, int index, struct dcb_i2c_entry *i2c)
719 uint8_t dcb_i2c_ver = dcb_version, headerlen = 0, entry_len = 4;
720 int i2c_entries = DCB_MAX_NUM_I2C_ENTRIES;
721 int recordoffset = 0, rdofs = 1, wrofs = 0;
722 uint8_t port_type = 0;
727 if (dcb_version >= 0x30) {
728 if (i2ctable[0] != dcb_version) /* necessary? */
730 "DCB I2C table version mismatch (%02X vs %02X)\n",
731 i2ctable[0], dcb_version);
732 dcb_i2c_ver = i2ctable[0];
733 headerlen = i2ctable[1];
734 if (i2ctable[2] <= DCB_MAX_NUM_I2C_ENTRIES)
735 i2c_entries = i2ctable[2];
738 "DCB I2C table has more entries than indexable "
739 "(%d entries, max %d)\n", i2ctable[2],
740 DCB_MAX_NUM_I2C_ENTRIES);
741 entry_len = i2ctable[3];
742 /* [4] is i2c_default_indices, read in parse_dcb_table() */
745 * It's your own fault if you call this function on a DCB 1.1 BIOS --
746 * the test below is for DCB 1.2
748 if (dcb_version < 0x14) {
756 if (index >= i2c_entries) {
757 NV_ERROR(dev, "DCB I2C index too big (%d >= %d)\n",
761 if (i2ctable[headerlen + entry_len * index + 3] == 0xff) {
762 NV_ERROR(dev, "DCB I2C entry invalid\n");
766 if (dcb_i2c_ver >= 0x30) {
767 port_type = i2ctable[headerlen + recordoffset + 3 + entry_len * index];
770 * Fixup for chips using same address offset for read and
773 if (port_type == 4) /* seen on C51 */
775 if (port_type >= 5) /* G80+ */
779 if (dcb_i2c_ver >= 0x40) {
780 if (port_type != 5 && port_type != 6)
781 NV_WARN(dev, "DCB I2C table has port type %d\n", port_type);
783 i2c->entry = ROM32(i2ctable[headerlen + recordoffset + entry_len * index]);
786 i2c->port_type = port_type;
787 i2c->read = i2ctable[headerlen + recordoffset + rdofs + entry_len * index];
788 i2c->write = i2ctable[headerlen + recordoffset + wrofs + entry_len * index];
793 static struct nouveau_i2c_chan *
794 init_i2c_device_find(struct drm_device *dev, int i2c_index)
796 struct drm_nouveau_private *dev_priv = dev->dev_private;
797 struct dcb_table *dcb = &dev_priv->vbios.dcb;
799 if (i2c_index == 0xff) {
800 /* note: dcb_entry_idx_from_crtchead needs pre-script set-up */
801 int idx = dcb_entry_idx_from_crtchead(dev), shift = 0;
802 int default_indices = dcb->i2c_default_indices;
804 if (idx != 0x7f && dcb->entry[idx].i2c_upper_default)
807 i2c_index = (default_indices >> shift) & 0xf;
809 if (i2c_index == 0x80) /* g80+ */
810 i2c_index = dcb->i2c_default_indices & 0xf;
812 if (i2c_index == 0x81)
813 i2c_index = (dcb->i2c_default_indices & 0xf0) >> 4;
815 if (i2c_index >= DCB_MAX_NUM_I2C_ENTRIES) {
816 NV_ERROR(dev, "invalid i2c_index 0x%x\n", i2c_index);
820 /* Make sure i2c table entry has been parsed, it may not
821 * have been if this is a bus not referenced by a DCB encoder
823 read_dcb_i2c_entry(dev, dcb->version, dcb->i2c_table,
824 i2c_index, &dcb->i2c[i2c_index]);
826 return nouveau_i2c_find(dev, i2c_index);
830 get_tmds_index_reg(struct drm_device *dev, uint8_t mlv)
833 * For mlv < 0x80, it is an index into a table of TMDS base addresses.
834 * For mlv == 0x80 use the "or" value of the dcb_entry indexed by
835 * CR58 for CR57 = 0 to index a table of offsets to the basic
837 * For mlv == 0x81 use the "or" value of the dcb_entry indexed by
838 * CR58 for CR57 = 0 to index a table of offsets to the basic
839 * 0x6808b0 address, and then flip the offset by 8.
842 struct drm_nouveau_private *dev_priv = dev->dev_private;
843 struct nvbios *bios = &dev_priv->vbios;
844 const int pramdac_offset[13] = {
845 0, 0, 0x8, 0, 0x2000, 0, 0, 0, 0x2008, 0, 0, 0, 0x2000 };
846 const uint32_t pramdac_table[4] = {
847 0x6808b0, 0x6808b8, 0x6828b0, 0x6828b8 };
850 int dcb_entry, dacoffset;
852 /* note: dcb_entry_idx_from_crtchead needs pre-script set-up */
853 dcb_entry = dcb_entry_idx_from_crtchead(dev);
854 if (dcb_entry == 0x7f)
856 dacoffset = pramdac_offset[bios->dcb.entry[dcb_entry].or];
859 return 0x6808b0 + dacoffset;
861 if (mlv >= ARRAY_SIZE(pramdac_table)) {
862 NV_ERROR(dev, "Magic Lookup Value too big (%02X)\n",
866 return pramdac_table[mlv];
871 init_io_restrict_prog(struct nvbios *bios, uint16_t offset,
872 struct init_exec *iexec)
875 * INIT_IO_RESTRICT_PROG opcode: 0x32 ('2')
877 * offset (8 bit): opcode
878 * offset + 1 (16 bit): CRTC port
879 * offset + 3 (8 bit): CRTC index
880 * offset + 4 (8 bit): mask
881 * offset + 5 (8 bit): shift
882 * offset + 6 (8 bit): count
883 * offset + 7 (32 bit): register
884 * offset + 11 (32 bit): configuration 1
887 * Starting at offset + 11 there are "count" 32 bit values.
888 * To find out which value to use read index "CRTC index" on "CRTC
889 * port", AND this value with "mask" and then bit shift right "shift"
890 * bits. Read the appropriate value using this index and write to
894 uint16_t crtcport = ROM16(bios->data[offset + 1]);
895 uint8_t crtcindex = bios->data[offset + 3];
896 uint8_t mask = bios->data[offset + 4];
897 uint8_t shift = bios->data[offset + 5];
898 uint8_t count = bios->data[offset + 6];
899 uint32_t reg = ROM32(bios->data[offset + 7]);
902 int len = 11 + count * 4;
907 BIOSLOG(bios, "0x%04X: Port: 0x%04X, Index: 0x%02X, Mask: 0x%02X, "
908 "Shift: 0x%02X, Count: 0x%02X, Reg: 0x%08X\n",
909 offset, crtcport, crtcindex, mask, shift, count, reg);
911 config = (bios_idxprt_rd(bios, crtcport, crtcindex) & mask) >> shift;
912 if (config > count) {
914 "0x%04X: Config 0x%02X exceeds maximal bound 0x%02X\n",
915 offset, config, count);
919 configval = ROM32(bios->data[offset + 11 + config * 4]);
921 BIOSLOG(bios, "0x%04X: Writing config %02X\n", offset, config);
923 bios_wr32(bios, reg, configval);
929 init_repeat(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
932 * INIT_REPEAT opcode: 0x33 ('3')
934 * offset (8 bit): opcode
935 * offset + 1 (8 bit): count
937 * Execute script following this opcode up to INIT_REPEAT_END
941 uint8_t count = bios->data[offset + 1];
944 /* no iexec->execute check by design */
946 BIOSLOG(bios, "0x%04X: Repeating following segment %d times\n",
949 iexec->repeat = true;
952 * count - 1, as the script block will execute once when we leave this
953 * opcode -- this is compatible with bios behaviour as:
954 * a) the block is always executed at least once, even if count == 0
955 * b) the bios interpreter skips to the op following INIT_END_REPEAT,
958 for (i = 0; i < count - 1; i++)
959 parse_init_table(bios, offset + 2, iexec);
961 iexec->repeat = false;
967 init_io_restrict_pll(struct nvbios *bios, uint16_t offset,
968 struct init_exec *iexec)
971 * INIT_IO_RESTRICT_PLL opcode: 0x34 ('4')
973 * offset (8 bit): opcode
974 * offset + 1 (16 bit): CRTC port
975 * offset + 3 (8 bit): CRTC index
976 * offset + 4 (8 bit): mask
977 * offset + 5 (8 bit): shift
978 * offset + 6 (8 bit): IO flag condition index
979 * offset + 7 (8 bit): count
980 * offset + 8 (32 bit): register
981 * offset + 12 (16 bit): frequency 1
984 * Starting at offset + 12 there are "count" 16 bit frequencies (10kHz).
985 * Set PLL register "register" to coefficients for frequency n,
986 * selected by reading index "CRTC index" of "CRTC port" ANDed with
987 * "mask" and shifted right by "shift".
989 * If "IO flag condition index" > 0, and condition met, double
990 * frequency before setting it.
993 uint16_t crtcport = ROM16(bios->data[offset + 1]);
994 uint8_t crtcindex = bios->data[offset + 3];
995 uint8_t mask = bios->data[offset + 4];
996 uint8_t shift = bios->data[offset + 5];
997 int8_t io_flag_condition_idx = bios->data[offset + 6];
998 uint8_t count = bios->data[offset + 7];
999 uint32_t reg = ROM32(bios->data[offset + 8]);
1002 int len = 12 + count * 2;
1004 if (!iexec->execute)
1007 BIOSLOG(bios, "0x%04X: Port: 0x%04X, Index: 0x%02X, Mask: 0x%02X, "
1008 "Shift: 0x%02X, IO Flag Condition: 0x%02X, "
1009 "Count: 0x%02X, Reg: 0x%08X\n",
1010 offset, crtcport, crtcindex, mask, shift,
1011 io_flag_condition_idx, count, reg);
1013 config = (bios_idxprt_rd(bios, crtcport, crtcindex) & mask) >> shift;
1014 if (config > count) {
1016 "0x%04X: Config 0x%02X exceeds maximal bound 0x%02X\n",
1017 offset, config, count);
1021 freq = ROM16(bios->data[offset + 12 + config * 2]);
1023 if (io_flag_condition_idx > 0) {
1024 if (io_flag_condition_met(bios, offset, io_flag_condition_idx)) {
1025 BIOSLOG(bios, "0x%04X: Condition fulfilled -- "
1026 "frequency doubled\n", offset);
1029 BIOSLOG(bios, "0x%04X: Condition not fulfilled -- "
1030 "frequency unchanged\n", offset);
1033 BIOSLOG(bios, "0x%04X: Reg: 0x%08X, Config: 0x%02X, Freq: %d0kHz\n",
1034 offset, reg, config, freq);
1036 setPLL(bios, reg, freq * 10);
1042 init_end_repeat(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
1045 * INIT_END_REPEAT opcode: 0x36 ('6')
1047 * offset (8 bit): opcode
1049 * Marks the end of the block for INIT_REPEAT to repeat
1052 /* no iexec->execute check by design */
1055 * iexec->repeat flag necessary to go past INIT_END_REPEAT opcode when
1056 * we're not in repeat mode
1065 init_copy(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
1068 * INIT_COPY opcode: 0x37 ('7')
1070 * offset (8 bit): opcode
1071 * offset + 1 (32 bit): register
1072 * offset + 5 (8 bit): shift
1073 * offset + 6 (8 bit): srcmask
1074 * offset + 7 (16 bit): CRTC port
1075 * offset + 9 (8 bit): CRTC index
1076 * offset + 10 (8 bit): mask
1078 * Read index "CRTC index" on "CRTC port", AND with "mask", OR with
1079 * (REGVAL("register") >> "shift" & "srcmask") and write-back to CRTC
1083 uint32_t reg = ROM32(bios->data[offset + 1]);
1084 uint8_t shift = bios->data[offset + 5];
1085 uint8_t srcmask = bios->data[offset + 6];
1086 uint16_t crtcport = ROM16(bios->data[offset + 7]);
1087 uint8_t crtcindex = bios->data[offset + 9];
1088 uint8_t mask = bios->data[offset + 10];
1092 if (!iexec->execute)
1095 BIOSLOG(bios, "0x%04X: Reg: 0x%08X, Shift: 0x%02X, SrcMask: 0x%02X, "
1096 "Port: 0x%04X, Index: 0x%02X, Mask: 0x%02X\n",
1097 offset, reg, shift, srcmask, crtcport, crtcindex, mask);
1099 data = bios_rd32(bios, reg);
1104 data <<= (0x100 - shift);
1108 crtcdata = bios_idxprt_rd(bios, crtcport, crtcindex) & mask;
1109 crtcdata |= (uint8_t)data;
1110 bios_idxprt_wr(bios, crtcport, crtcindex, crtcdata);
1116 init_not(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
1119 * INIT_NOT opcode: 0x38 ('8')
1121 * offset (8 bit): opcode
1123 * Invert the current execute / no-execute condition (i.e. "else")
1126 BIOSLOG(bios, "0x%04X: ------ Skipping following commands ------\n", offset);
1128 BIOSLOG(bios, "0x%04X: ------ Executing following commands ------\n", offset);
1130 iexec->execute = !iexec->execute;
1135 init_io_flag_condition(struct nvbios *bios, uint16_t offset,
1136 struct init_exec *iexec)
1139 * INIT_IO_FLAG_CONDITION opcode: 0x39 ('9')
1141 * offset (8 bit): opcode
1142 * offset + 1 (8 bit): condition number
1144 * Check condition "condition number" in the IO flag condition table.
1145 * If condition not met skip subsequent opcodes until condition is
1146 * inverted (INIT_NOT), or we hit INIT_RESUME
1149 uint8_t cond = bios->data[offset + 1];
1151 if (!iexec->execute)
1154 if (io_flag_condition_met(bios, offset, cond))
1155 BIOSLOG(bios, "0x%04X: Condition fulfilled -- continuing to execute\n", offset);
1157 BIOSLOG(bios, "0x%04X: Condition not fulfilled -- skipping following commands\n", offset);
1158 iexec->execute = false;
1165 init_dp_condition(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
1168 * INIT_DP_CONDITION opcode: 0x3A ('')
1170 * offset (8 bit): opcode
1171 * offset + 1 (8 bit): "sub" opcode
1172 * offset + 2 (8 bit): unknown
1176 struct bit_displayport_encoder_table *dpe = NULL;
1177 struct dcb_entry *dcb = bios->display.output;
1178 struct drm_device *dev = bios->dev;
1179 uint8_t cond = bios->data[offset + 1];
1182 BIOSLOG(bios, "0x%04X: subop 0x%02X\n", offset, cond);
1184 if (!iexec->execute)
1187 dpe = nouveau_bios_dp_table(dev, dcb, &dummy);
1189 NV_ERROR(dev, "0x%04X: INIT_3A: no encoder table!!\n", offset);
1196 struct dcb_connector_table_entry *ent =
1197 &bios->dcb.connector.entry[dcb->connector];
1199 if (ent->type != DCB_CONNECTOR_eDP)
1200 iexec->execute = false;
1205 if (!(dpe->unknown & cond))
1206 iexec->execute = false;
1210 struct nouveau_i2c_chan *auxch;
1213 auxch = nouveau_i2c_find(dev, bios->display.output->i2c_index);
1215 NV_ERROR(dev, "0x%04X: couldn't get auxch\n", offset);
1219 ret = nouveau_dp_auxch(auxch, 9, 0xd, &cond, 1);
1221 NV_ERROR(dev, "0x%04X: auxch rd fail: %d\n", offset, ret);
1226 iexec->execute = false;
1230 NV_WARN(dev, "0x%04X: unknown INIT_3A op: %d\n", offset, cond);
1235 BIOSLOG(bios, "0x%04X: continuing to execute\n", offset);
1237 BIOSLOG(bios, "0x%04X: skipping following commands\n", offset);
1243 init_op_3b(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
1246 * INIT_3B opcode: 0x3B ('')
1248 * offset (8 bit): opcode
1249 * offset + 1 (8 bit): crtc index
1253 uint8_t or = ffs(bios->display.output->or) - 1;
1254 uint8_t index = bios->data[offset + 1];
1257 if (!iexec->execute)
1260 data = bios_idxprt_rd(bios, 0x3d4, index);
1261 bios_idxprt_wr(bios, 0x3d4, index, data & ~(1 << or));
1266 init_op_3c(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
1269 * INIT_3C opcode: 0x3C ('')
1271 * offset (8 bit): opcode
1272 * offset + 1 (8 bit): crtc index
1276 uint8_t or = ffs(bios->display.output->or) - 1;
1277 uint8_t index = bios->data[offset + 1];
1280 if (!iexec->execute)
1283 data = bios_idxprt_rd(bios, 0x3d4, index);
1284 bios_idxprt_wr(bios, 0x3d4, index, data | (1 << or));
1289 init_idx_addr_latched(struct nvbios *bios, uint16_t offset,
1290 struct init_exec *iexec)
1293 * INIT_INDEX_ADDRESS_LATCHED opcode: 0x49 ('I')
1295 * offset (8 bit): opcode
1296 * offset + 1 (32 bit): control register
1297 * offset + 5 (32 bit): data register
1298 * offset + 9 (32 bit): mask
1299 * offset + 13 (32 bit): data
1300 * offset + 17 (8 bit): count
1301 * offset + 18 (8 bit): address 1
1302 * offset + 19 (8 bit): data 1
1305 * For each of "count" address and data pairs, write "data n" to
1306 * "data register", read the current value of "control register",
1307 * and write it back once ANDed with "mask", ORed with "data",
1308 * and ORed with "address n"
1311 uint32_t controlreg = ROM32(bios->data[offset + 1]);
1312 uint32_t datareg = ROM32(bios->data[offset + 5]);
1313 uint32_t mask = ROM32(bios->data[offset + 9]);
1314 uint32_t data = ROM32(bios->data[offset + 13]);
1315 uint8_t count = bios->data[offset + 17];
1316 int len = 18 + count * 2;
1320 if (!iexec->execute)
1323 BIOSLOG(bios, "0x%04X: ControlReg: 0x%08X, DataReg: 0x%08X, "
1324 "Mask: 0x%08X, Data: 0x%08X, Count: 0x%02X\n",
1325 offset, controlreg, datareg, mask, data, count);
1327 for (i = 0; i < count; i++) {
1328 uint8_t instaddress = bios->data[offset + 18 + i * 2];
1329 uint8_t instdata = bios->data[offset + 19 + i * 2];
1331 BIOSLOG(bios, "0x%04X: Address: 0x%02X, Data: 0x%02X\n",
1332 offset, instaddress, instdata);
1334 bios_wr32(bios, datareg, instdata);
1335 value = bios_rd32(bios, controlreg) & mask;
1337 value |= instaddress;
1338 bios_wr32(bios, controlreg, value);
1345 init_io_restrict_pll2(struct nvbios *bios, uint16_t offset,
1346 struct init_exec *iexec)
1349 * INIT_IO_RESTRICT_PLL2 opcode: 0x4A ('J')
1351 * offset (8 bit): opcode
1352 * offset + 1 (16 bit): CRTC port
1353 * offset + 3 (8 bit): CRTC index
1354 * offset + 4 (8 bit): mask
1355 * offset + 5 (8 bit): shift
1356 * offset + 6 (8 bit): count
1357 * offset + 7 (32 bit): register
1358 * offset + 11 (32 bit): frequency 1
1361 * Starting at offset + 11 there are "count" 32 bit frequencies (kHz).
1362 * Set PLL register "register" to coefficients for frequency n,
1363 * selected by reading index "CRTC index" of "CRTC port" ANDed with
1364 * "mask" and shifted right by "shift".
1367 uint16_t crtcport = ROM16(bios->data[offset + 1]);
1368 uint8_t crtcindex = bios->data[offset + 3];
1369 uint8_t mask = bios->data[offset + 4];
1370 uint8_t shift = bios->data[offset + 5];
1371 uint8_t count = bios->data[offset + 6];
1372 uint32_t reg = ROM32(bios->data[offset + 7]);
1373 int len = 11 + count * 4;
1377 if (!iexec->execute)
1380 BIOSLOG(bios, "0x%04X: Port: 0x%04X, Index: 0x%02X, Mask: 0x%02X, "
1381 "Shift: 0x%02X, Count: 0x%02X, Reg: 0x%08X\n",
1382 offset, crtcport, crtcindex, mask, shift, count, reg);
1387 config = (bios_idxprt_rd(bios, crtcport, crtcindex) & mask) >> shift;
1388 if (config > count) {
1390 "0x%04X: Config 0x%02X exceeds maximal bound 0x%02X\n",
1391 offset, config, count);
1395 freq = ROM32(bios->data[offset + 11 + config * 4]);
1397 BIOSLOG(bios, "0x%04X: Reg: 0x%08X, Config: 0x%02X, Freq: %dkHz\n",
1398 offset, reg, config, freq);
1400 setPLL(bios, reg, freq);
1406 init_pll2(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
1409 * INIT_PLL2 opcode: 0x4B ('K')
1411 * offset (8 bit): opcode
1412 * offset + 1 (32 bit): register
1413 * offset + 5 (32 bit): freq
1415 * Set PLL register "register" to coefficients for frequency "freq"
1418 uint32_t reg = ROM32(bios->data[offset + 1]);
1419 uint32_t freq = ROM32(bios->data[offset + 5]);
1421 if (!iexec->execute)
1424 BIOSLOG(bios, "0x%04X: Reg: 0x%04X, Freq: %dkHz\n",
1427 setPLL(bios, reg, freq);
1432 init_i2c_byte(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
1435 * INIT_I2C_BYTE opcode: 0x4C ('L')
1437 * offset (8 bit): opcode
1438 * offset + 1 (8 bit): DCB I2C table entry index
1439 * offset + 2 (8 bit): I2C slave address
1440 * offset + 3 (8 bit): count
1441 * offset + 4 (8 bit): I2C register 1
1442 * offset + 5 (8 bit): mask 1
1443 * offset + 6 (8 bit): data 1
1446 * For each of "count" registers given by "I2C register n" on the device
1447 * addressed by "I2C slave address" on the I2C bus given by
1448 * "DCB I2C table entry index", read the register, AND the result with
1449 * "mask n" and OR it with "data n" before writing it back to the device
1452 struct drm_device *dev = bios->dev;
1453 uint8_t i2c_index = bios->data[offset + 1];
1454 uint8_t i2c_address = bios->data[offset + 2] >> 1;
1455 uint8_t count = bios->data[offset + 3];
1456 struct nouveau_i2c_chan *chan;
1457 int len = 4 + count * 3;
1460 if (!iexec->execute)
1463 BIOSLOG(bios, "0x%04X: DCBI2CIndex: 0x%02X, I2CAddress: 0x%02X, "
1465 offset, i2c_index, i2c_address, count);
1467 chan = init_i2c_device_find(dev, i2c_index);
1469 NV_ERROR(dev, "0x%04X: i2c bus not found\n", offset);
1473 for (i = 0; i < count; i++) {
1474 uint8_t reg = bios->data[offset + 4 + i * 3];
1475 uint8_t mask = bios->data[offset + 5 + i * 3];
1476 uint8_t data = bios->data[offset + 6 + i * 3];
1477 union i2c_smbus_data val;
1479 ret = i2c_smbus_xfer(&chan->adapter, i2c_address, 0,
1480 I2C_SMBUS_READ, reg,
1481 I2C_SMBUS_BYTE_DATA, &val);
1483 NV_ERROR(dev, "0x%04X: i2c rd fail: %d\n", offset, ret);
1487 BIOSLOG(bios, "0x%04X: I2CReg: 0x%02X, Value: 0x%02X, "
1488 "Mask: 0x%02X, Data: 0x%02X\n",
1489 offset, reg, val.byte, mask, data);
1496 ret = i2c_smbus_xfer(&chan->adapter, i2c_address, 0,
1497 I2C_SMBUS_WRITE, reg,
1498 I2C_SMBUS_BYTE_DATA, &val);
1500 NV_ERROR(dev, "0x%04X: i2c wr fail: %d\n", offset, ret);
1509 init_zm_i2c_byte(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
1512 * INIT_ZM_I2C_BYTE opcode: 0x4D ('M')
1514 * offset (8 bit): opcode
1515 * offset + 1 (8 bit): DCB I2C table entry index
1516 * offset + 2 (8 bit): I2C slave address
1517 * offset + 3 (8 bit): count
1518 * offset + 4 (8 bit): I2C register 1
1519 * offset + 5 (8 bit): data 1
1522 * For each of "count" registers given by "I2C register n" on the device
1523 * addressed by "I2C slave address" on the I2C bus given by
1524 * "DCB I2C table entry index", set the register to "data n"
1527 struct drm_device *dev = bios->dev;
1528 uint8_t i2c_index = bios->data[offset + 1];
1529 uint8_t i2c_address = bios->data[offset + 2] >> 1;
1530 uint8_t count = bios->data[offset + 3];
1531 struct nouveau_i2c_chan *chan;
1532 int len = 4 + count * 2;
1535 if (!iexec->execute)
1538 BIOSLOG(bios, "0x%04X: DCBI2CIndex: 0x%02X, I2CAddress: 0x%02X, "
1540 offset, i2c_index, i2c_address, count);
1542 chan = init_i2c_device_find(dev, i2c_index);
1544 NV_ERROR(dev, "0x%04X: i2c bus not found\n", offset);
1548 for (i = 0; i < count; i++) {
1549 uint8_t reg = bios->data[offset + 4 + i * 2];
1550 union i2c_smbus_data val;
1552 val.byte = bios->data[offset + 5 + i * 2];
1554 BIOSLOG(bios, "0x%04X: I2CReg: 0x%02X, Data: 0x%02X\n",
1555 offset, reg, val.byte);
1560 ret = i2c_smbus_xfer(&chan->adapter, i2c_address, 0,
1561 I2C_SMBUS_WRITE, reg,
1562 I2C_SMBUS_BYTE_DATA, &val);
1564 NV_ERROR(dev, "0x%04X: i2c wr fail: %d\n", offset, ret);
1573 init_zm_i2c(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
1576 * INIT_ZM_I2C opcode: 0x4E ('N')
1578 * offset (8 bit): opcode
1579 * offset + 1 (8 bit): DCB I2C table entry index
1580 * offset + 2 (8 bit): I2C slave address
1581 * offset + 3 (8 bit): count
1582 * offset + 4 (8 bit): data 1
1585 * Send "count" bytes ("data n") to the device addressed by "I2C slave
1586 * address" on the I2C bus given by "DCB I2C table entry index"
1589 struct drm_device *dev = bios->dev;
1590 uint8_t i2c_index = bios->data[offset + 1];
1591 uint8_t i2c_address = bios->data[offset + 2] >> 1;
1592 uint8_t count = bios->data[offset + 3];
1593 int len = 4 + count;
1594 struct nouveau_i2c_chan *chan;
1599 if (!iexec->execute)
1602 BIOSLOG(bios, "0x%04X: DCBI2CIndex: 0x%02X, I2CAddress: 0x%02X, "
1604 offset, i2c_index, i2c_address, count);
1606 chan = init_i2c_device_find(dev, i2c_index);
1608 NV_ERROR(dev, "0x%04X: i2c bus not found\n", offset);
1612 for (i = 0; i < count; i++) {
1613 data[i] = bios->data[offset + 4 + i];
1615 BIOSLOG(bios, "0x%04X: Data: 0x%02X\n", offset, data[i]);
1618 if (bios->execute) {
1619 msg.addr = i2c_address;
1623 ret = i2c_transfer(&chan->adapter, &msg, 1);
1625 NV_ERROR(dev, "0x%04X: i2c wr fail: %d\n", offset, ret);
1634 init_tmds(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
1637 * INIT_TMDS opcode: 0x4F ('O') (non-canon name)
1639 * offset (8 bit): opcode
1640 * offset + 1 (8 bit): magic lookup value
1641 * offset + 2 (8 bit): TMDS address
1642 * offset + 3 (8 bit): mask
1643 * offset + 4 (8 bit): data
1645 * Read the data reg for TMDS address "TMDS address", AND it with mask
1646 * and OR it with data, then write it back
1647 * "magic lookup value" determines which TMDS base address register is
1648 * used -- see get_tmds_index_reg()
1651 struct drm_device *dev = bios->dev;
1652 uint8_t mlv = bios->data[offset + 1];
1653 uint32_t tmdsaddr = bios->data[offset + 2];
1654 uint8_t mask = bios->data[offset + 3];
1655 uint8_t data = bios->data[offset + 4];
1656 uint32_t reg, value;
1658 if (!iexec->execute)
1661 BIOSLOG(bios, "0x%04X: MagicLookupValue: 0x%02X, TMDSAddr: 0x%02X, "
1662 "Mask: 0x%02X, Data: 0x%02X\n",
1663 offset, mlv, tmdsaddr, mask, data);
1665 reg = get_tmds_index_reg(bios->dev, mlv);
1667 NV_ERROR(dev, "0x%04X: no tmds_index_reg\n", offset);
1671 bios_wr32(bios, reg,
1672 tmdsaddr | NV_PRAMDAC_FP_TMDS_CONTROL_WRITE_DISABLE);
1673 value = (bios_rd32(bios, reg + 4) & mask) | data;
1674 bios_wr32(bios, reg + 4, value);
1675 bios_wr32(bios, reg, tmdsaddr);
1681 init_zm_tmds_group(struct nvbios *bios, uint16_t offset,
1682 struct init_exec *iexec)
1685 * INIT_ZM_TMDS_GROUP opcode: 0x50 ('P') (non-canon name)
1687 * offset (8 bit): opcode
1688 * offset + 1 (8 bit): magic lookup value
1689 * offset + 2 (8 bit): count
1690 * offset + 3 (8 bit): addr 1
1691 * offset + 4 (8 bit): data 1
1694 * For each of "count" TMDS address and data pairs write "data n" to
1695 * "addr n". "magic lookup value" determines which TMDS base address
1696 * register is used -- see get_tmds_index_reg()
1699 struct drm_device *dev = bios->dev;
1700 uint8_t mlv = bios->data[offset + 1];
1701 uint8_t count = bios->data[offset + 2];
1702 int len = 3 + count * 2;
1706 if (!iexec->execute)
1709 BIOSLOG(bios, "0x%04X: MagicLookupValue: 0x%02X, Count: 0x%02X\n",
1710 offset, mlv, count);
1712 reg = get_tmds_index_reg(bios->dev, mlv);
1714 NV_ERROR(dev, "0x%04X: no tmds_index_reg\n", offset);
1718 for (i = 0; i < count; i++) {
1719 uint8_t tmdsaddr = bios->data[offset + 3 + i * 2];
1720 uint8_t tmdsdata = bios->data[offset + 4 + i * 2];
1722 bios_wr32(bios, reg + 4, tmdsdata);
1723 bios_wr32(bios, reg, tmdsaddr);
1730 init_cr_idx_adr_latch(struct nvbios *bios, uint16_t offset,
1731 struct init_exec *iexec)
1734 * INIT_CR_INDEX_ADDRESS_LATCHED opcode: 0x51 ('Q')
1736 * offset (8 bit): opcode
1737 * offset + 1 (8 bit): CRTC index1
1738 * offset + 2 (8 bit): CRTC index2
1739 * offset + 3 (8 bit): baseaddr
1740 * offset + 4 (8 bit): count
1741 * offset + 5 (8 bit): data 1
1744 * For each of "count" address and data pairs, write "baseaddr + n" to
1745 * "CRTC index1" and "data n" to "CRTC index2"
1746 * Once complete, restore initial value read from "CRTC index1"
1748 uint8_t crtcindex1 = bios->data[offset + 1];
1749 uint8_t crtcindex2 = bios->data[offset + 2];
1750 uint8_t baseaddr = bios->data[offset + 3];
1751 uint8_t count = bios->data[offset + 4];
1752 int len = 5 + count;
1753 uint8_t oldaddr, data;
1756 if (!iexec->execute)
1759 BIOSLOG(bios, "0x%04X: Index1: 0x%02X, Index2: 0x%02X, "
1760 "BaseAddr: 0x%02X, Count: 0x%02X\n",
1761 offset, crtcindex1, crtcindex2, baseaddr, count);
1763 oldaddr = bios_idxprt_rd(bios, NV_CIO_CRX__COLOR, crtcindex1);
1765 for (i = 0; i < count; i++) {
1766 bios_idxprt_wr(bios, NV_CIO_CRX__COLOR, crtcindex1,
1768 data = bios->data[offset + 5 + i];
1769 bios_idxprt_wr(bios, NV_CIO_CRX__COLOR, crtcindex2, data);
1772 bios_idxprt_wr(bios, NV_CIO_CRX__COLOR, crtcindex1, oldaddr);
1778 init_cr(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
1781 * INIT_CR opcode: 0x52 ('R')
1783 * offset (8 bit): opcode
1784 * offset + 1 (8 bit): CRTC index
1785 * offset + 2 (8 bit): mask
1786 * offset + 3 (8 bit): data
1788 * Assign the value of at "CRTC index" ANDed with mask and ORed with
1789 * data back to "CRTC index"
1792 uint8_t crtcindex = bios->data[offset + 1];
1793 uint8_t mask = bios->data[offset + 2];
1794 uint8_t data = bios->data[offset + 3];
1797 if (!iexec->execute)
1800 BIOSLOG(bios, "0x%04X: Index: 0x%02X, Mask: 0x%02X, Data: 0x%02X\n",
1801 offset, crtcindex, mask, data);
1803 value = bios_idxprt_rd(bios, NV_CIO_CRX__COLOR, crtcindex) & mask;
1805 bios_idxprt_wr(bios, NV_CIO_CRX__COLOR, crtcindex, value);
1811 init_zm_cr(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
1814 * INIT_ZM_CR opcode: 0x53 ('S')
1816 * offset (8 bit): opcode
1817 * offset + 1 (8 bit): CRTC index
1818 * offset + 2 (8 bit): value
1820 * Assign "value" to CRTC register with index "CRTC index".
1823 uint8_t crtcindex = ROM32(bios->data[offset + 1]);
1824 uint8_t data = bios->data[offset + 2];
1826 if (!iexec->execute)
1829 bios_idxprt_wr(bios, NV_CIO_CRX__COLOR, crtcindex, data);
1835 init_zm_cr_group(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
1838 * INIT_ZM_CR_GROUP opcode: 0x54 ('T')
1840 * offset (8 bit): opcode
1841 * offset + 1 (8 bit): count
1842 * offset + 2 (8 bit): CRTC index 1
1843 * offset + 3 (8 bit): value 1
1846 * For "count", assign "value n" to CRTC register with index
1850 uint8_t count = bios->data[offset + 1];
1851 int len = 2 + count * 2;
1854 if (!iexec->execute)
1857 for (i = 0; i < count; i++)
1858 init_zm_cr(bios, offset + 2 + 2 * i - 1, iexec);
1864 init_condition_time(struct nvbios *bios, uint16_t offset,
1865 struct init_exec *iexec)
1868 * INIT_CONDITION_TIME opcode: 0x56 ('V')
1870 * offset (8 bit): opcode
1871 * offset + 1 (8 bit): condition number
1872 * offset + 2 (8 bit): retries / 50
1874 * Check condition "condition number" in the condition table.
1875 * Bios code then sleeps for 2ms if the condition is not met, and
1876 * repeats up to "retries" times, but on one C51 this has proved
1877 * insufficient. In mmiotraces the driver sleeps for 20ms, so we do
1878 * this, and bail after "retries" times, or 2s, whichever is less.
1879 * If still not met after retries, clear execution flag for this table.
1882 uint8_t cond = bios->data[offset + 1];
1883 uint16_t retries = bios->data[offset + 2] * 50;
1886 if (!iexec->execute)
1892 BIOSLOG(bios, "0x%04X: Condition: 0x%02X, Retries: 0x%02X\n",
1893 offset, cond, retries);
1895 if (!bios->execute) /* avoid 2s delays when "faking" execution */
1898 for (cnt = 0; cnt < retries; cnt++) {
1899 if (bios_condition_met(bios, offset, cond)) {
1900 BIOSLOG(bios, "0x%04X: Condition met, continuing\n",
1904 BIOSLOG(bios, "0x%04X: "
1905 "Condition not met, sleeping for 20ms\n",
1911 if (!bios_condition_met(bios, offset, cond)) {
1913 "0x%04X: Condition still not met after %dms, "
1914 "skipping following opcodes\n", offset, 20 * retries);
1915 iexec->execute = false;
1922 init_ltime(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
1925 * INIT_LTIME opcode: 0x57 ('V')
1927 * offset (8 bit): opcode
1928 * offset + 1 (16 bit): time
1930 * Sleep for "time" milliseconds.
1933 unsigned time = ROM16(bios->data[offset + 1]);
1935 if (!iexec->execute)
1938 BIOSLOG(bios, "0x%04X: Sleeping for 0x%04X milliseconds\n",
1947 init_zm_reg_sequence(struct nvbios *bios, uint16_t offset,
1948 struct init_exec *iexec)
1951 * INIT_ZM_REG_SEQUENCE opcode: 0x58 ('X')
1953 * offset (8 bit): opcode
1954 * offset + 1 (32 bit): base register
1955 * offset + 5 (8 bit): count
1956 * offset + 6 (32 bit): value 1
1959 * Starting at offset + 6 there are "count" 32 bit values.
1960 * For "count" iterations set "base register" + 4 * current_iteration
1961 * to "value current_iteration"
1964 uint32_t basereg = ROM32(bios->data[offset + 1]);
1965 uint32_t count = bios->data[offset + 5];
1966 int len = 6 + count * 4;
1969 if (!iexec->execute)
1972 BIOSLOG(bios, "0x%04X: BaseReg: 0x%08X, Count: 0x%02X\n",
1973 offset, basereg, count);
1975 for (i = 0; i < count; i++) {
1976 uint32_t reg = basereg + i * 4;
1977 uint32_t data = ROM32(bios->data[offset + 6 + i * 4]);
1979 bios_wr32(bios, reg, data);
1986 init_sub_direct(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
1989 * INIT_SUB_DIRECT opcode: 0x5B ('[')
1991 * offset (8 bit): opcode
1992 * offset + 1 (16 bit): subroutine offset (in bios)
1994 * Calls a subroutine that will execute commands until INIT_DONE
1998 uint16_t sub_offset = ROM16(bios->data[offset + 1]);
2000 if (!iexec->execute)
2003 BIOSLOG(bios, "0x%04X: Executing subroutine at 0x%04X\n",
2004 offset, sub_offset);
2006 parse_init_table(bios, sub_offset, iexec);
2008 BIOSLOG(bios, "0x%04X: End of 0x%04X subroutine\n", offset, sub_offset);
2014 init_i2c_if(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
2017 * INIT_I2C_IF opcode: 0x5E ('^')
2019 * offset (8 bit): opcode
2020 * offset + 1 (8 bit): DCB I2C table entry index
2021 * offset + 2 (8 bit): I2C slave address
2022 * offset + 3 (8 bit): I2C register
2023 * offset + 4 (8 bit): mask
2024 * offset + 5 (8 bit): data
2026 * Read the register given by "I2C register" on the device addressed
2027 * by "I2C slave address" on the I2C bus given by "DCB I2C table
2028 * entry index". Compare the result AND "mask" to "data".
2029 * If they're not equal, skip subsequent opcodes until condition is
2030 * inverted (INIT_NOT), or we hit INIT_RESUME
2033 uint8_t i2c_index = bios->data[offset + 1];
2034 uint8_t i2c_address = bios->data[offset + 2] >> 1;
2035 uint8_t reg = bios->data[offset + 3];
2036 uint8_t mask = bios->data[offset + 4];
2037 uint8_t data = bios->data[offset + 5];
2038 struct nouveau_i2c_chan *chan;
2039 union i2c_smbus_data val;
2042 /* no execute check by design */
2044 BIOSLOG(bios, "0x%04X: DCBI2CIndex: 0x%02X, I2CAddress: 0x%02X\n",
2045 offset, i2c_index, i2c_address);
2047 chan = init_i2c_device_find(bios->dev, i2c_index);
2051 ret = i2c_smbus_xfer(&chan->adapter, i2c_address, 0,
2052 I2C_SMBUS_READ, reg,
2053 I2C_SMBUS_BYTE_DATA, &val);
2055 BIOSLOG(bios, "0x%04X: I2CReg: 0x%02X, Value: [no device], "
2056 "Mask: 0x%02X, Data: 0x%02X\n",
2057 offset, reg, mask, data);
2062 BIOSLOG(bios, "0x%04X: I2CReg: 0x%02X, Value: 0x%02X, "
2063 "Mask: 0x%02X, Data: 0x%02X\n",
2064 offset, reg, val.byte, mask, data);
2066 iexec->execute = ((val.byte & mask) == data);
2072 init_copy_nv_reg(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
2075 * INIT_COPY_NV_REG opcode: 0x5F ('_')
2077 * offset (8 bit): opcode
2078 * offset + 1 (32 bit): src reg
2079 * offset + 5 (8 bit): shift
2080 * offset + 6 (32 bit): src mask
2081 * offset + 10 (32 bit): xor
2082 * offset + 14 (32 bit): dst reg
2083 * offset + 18 (32 bit): dst mask
2085 * Shift REGVAL("src reg") right by (signed) "shift", AND result with
2086 * "src mask", then XOR with "xor". Write this OR'd with
2087 * (REGVAL("dst reg") AND'd with "dst mask") to "dst reg"
2090 uint32_t srcreg = *((uint32_t *)(&bios->data[offset + 1]));
2091 uint8_t shift = bios->data[offset + 5];
2092 uint32_t srcmask = *((uint32_t *)(&bios->data[offset + 6]));
2093 uint32_t xor = *((uint32_t *)(&bios->data[offset + 10]));
2094 uint32_t dstreg = *((uint32_t *)(&bios->data[offset + 14]));
2095 uint32_t dstmask = *((uint32_t *)(&bios->data[offset + 18]));
2096 uint32_t srcvalue, dstvalue;
2098 if (!iexec->execute)
2101 BIOSLOG(bios, "0x%04X: SrcReg: 0x%08X, Shift: 0x%02X, SrcMask: 0x%08X, "
2102 "Xor: 0x%08X, DstReg: 0x%08X, DstMask: 0x%08X\n",
2103 offset, srcreg, shift, srcmask, xor, dstreg, dstmask);
2105 srcvalue = bios_rd32(bios, srcreg);
2110 srcvalue <<= (0x100 - shift);
2112 srcvalue = (srcvalue & srcmask) ^ xor;
2114 dstvalue = bios_rd32(bios, dstreg) & dstmask;
2116 bios_wr32(bios, dstreg, dstvalue | srcvalue);
2122 init_zm_index_io(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
2125 * INIT_ZM_INDEX_IO opcode: 0x62 ('b')
2127 * offset (8 bit): opcode
2128 * offset + 1 (16 bit): CRTC port
2129 * offset + 3 (8 bit): CRTC index
2130 * offset + 4 (8 bit): data
2132 * Write "data" to index "CRTC index" of "CRTC port"
2134 uint16_t crtcport = ROM16(bios->data[offset + 1]);
2135 uint8_t crtcindex = bios->data[offset + 3];
2136 uint8_t data = bios->data[offset + 4];
2138 if (!iexec->execute)
2141 bios_idxprt_wr(bios, crtcport, crtcindex, data);
2147 bios_md32(struct nvbios *bios, uint32_t reg,
2148 uint32_t mask, uint32_t val)
2150 bios_wr32(bios, reg, (bios_rd32(bios, reg) & ~mask) | val);
2154 peek_fb(struct drm_device *dev, struct io_mapping *fb,
2159 if (off < pci_resource_len(dev->pdev, 1)) {
2160 uint8_t __iomem *p =
2161 io_mapping_map_atomic_wc(fb, off & PAGE_MASK);
2163 val = ioread32(p + (off & ~PAGE_MASK));
2165 io_mapping_unmap_atomic(p);
2172 poke_fb(struct drm_device *dev, struct io_mapping *fb,
2173 uint32_t off, uint32_t val)
2175 if (off < pci_resource_len(dev->pdev, 1)) {
2176 uint8_t __iomem *p =
2177 io_mapping_map_atomic_wc(fb, off & PAGE_MASK);
2179 iowrite32(val, p + (off & ~PAGE_MASK));
2182 io_mapping_unmap_atomic(p);
2187 read_back_fb(struct drm_device *dev, struct io_mapping *fb,
2188 uint32_t off, uint32_t val)
2190 poke_fb(dev, fb, off, val);
2191 return val == peek_fb(dev, fb, off);
2195 nv04_init_compute_mem(struct nvbios *bios)
2197 struct drm_device *dev = bios->dev;
2198 uint32_t patt = 0xdeadbeef;
2199 struct io_mapping *fb;
2202 /* Map the framebuffer aperture */
2203 fb = io_mapping_create_wc(pci_resource_start(dev->pdev, 1),
2204 pci_resource_len(dev->pdev, 1));
2208 /* Sequencer and refresh off */
2209 NVWriteVgaSeq(dev, 0, 1, NVReadVgaSeq(dev, 0, 1) | 0x20);
2210 bios_md32(bios, NV04_PFB_DEBUG_0, 0, NV04_PFB_DEBUG_0_REFRESH_OFF);
2212 bios_md32(bios, NV04_PFB_BOOT_0, ~0,
2213 NV04_PFB_BOOT_0_RAM_AMOUNT_16MB |
2214 NV04_PFB_BOOT_0_RAM_WIDTH_128 |
2215 NV04_PFB_BOOT_0_RAM_TYPE_SGRAM_16MBIT);
2217 for (i = 0; i < 4; i++)
2218 poke_fb(dev, fb, 4 * i, patt);
2220 poke_fb(dev, fb, 0x400000, patt + 1);
2222 if (peek_fb(dev, fb, 0) == patt + 1) {
2223 bios_md32(bios, NV04_PFB_BOOT_0, NV04_PFB_BOOT_0_RAM_TYPE,
2224 NV04_PFB_BOOT_0_RAM_TYPE_SDRAM_16MBIT);
2225 bios_md32(bios, NV04_PFB_DEBUG_0,
2226 NV04_PFB_DEBUG_0_REFRESH_OFF, 0);
2228 for (i = 0; i < 4; i++)
2229 poke_fb(dev, fb, 4 * i, patt);
2231 if ((peek_fb(dev, fb, 0xc) & 0xffff) != (patt & 0xffff))
2232 bios_md32(bios, NV04_PFB_BOOT_0,
2233 NV04_PFB_BOOT_0_RAM_WIDTH_128 |
2234 NV04_PFB_BOOT_0_RAM_AMOUNT,
2235 NV04_PFB_BOOT_0_RAM_AMOUNT_8MB);
2237 } else if ((peek_fb(dev, fb, 0xc) & 0xffff0000) !=
2238 (patt & 0xffff0000)) {
2239 bios_md32(bios, NV04_PFB_BOOT_0,
2240 NV04_PFB_BOOT_0_RAM_WIDTH_128 |
2241 NV04_PFB_BOOT_0_RAM_AMOUNT,
2242 NV04_PFB_BOOT_0_RAM_AMOUNT_4MB);
2244 } else if (peek_fb(dev, fb, 0) != patt) {
2245 if (read_back_fb(dev, fb, 0x800000, patt))
2246 bios_md32(bios, NV04_PFB_BOOT_0,
2247 NV04_PFB_BOOT_0_RAM_AMOUNT,
2248 NV04_PFB_BOOT_0_RAM_AMOUNT_8MB);
2250 bios_md32(bios, NV04_PFB_BOOT_0,
2251 NV04_PFB_BOOT_0_RAM_AMOUNT,
2252 NV04_PFB_BOOT_0_RAM_AMOUNT_4MB);
2254 bios_md32(bios, NV04_PFB_BOOT_0, NV04_PFB_BOOT_0_RAM_TYPE,
2255 NV04_PFB_BOOT_0_RAM_TYPE_SGRAM_8MBIT);
2257 } else if (!read_back_fb(dev, fb, 0x800000, patt)) {
2258 bios_md32(bios, NV04_PFB_BOOT_0, NV04_PFB_BOOT_0_RAM_AMOUNT,
2259 NV04_PFB_BOOT_0_RAM_AMOUNT_8MB);
2263 /* Refresh on, sequencer on */
2264 bios_md32(bios, NV04_PFB_DEBUG_0, NV04_PFB_DEBUG_0_REFRESH_OFF, 0);
2265 NVWriteVgaSeq(dev, 0, 1, NVReadVgaSeq(dev, 0, 1) & ~0x20);
2267 io_mapping_free(fb);
2271 static const uint8_t *
2272 nv05_memory_config(struct nvbios *bios)
2274 /* Defaults for BIOSes lacking a memory config table */
2275 static const uint8_t default_config_tab[][2] = {
2285 int i = (bios_rd32(bios, NV_PEXTDEV_BOOT_0) &
2286 NV_PEXTDEV_BOOT_0_RAMCFG) >> 2;
2288 if (bios->legacy.mem_init_tbl_ptr)
2289 return &bios->data[bios->legacy.mem_init_tbl_ptr + 2 * i];
2291 return default_config_tab[i];
2295 nv05_init_compute_mem(struct nvbios *bios)
2297 struct drm_device *dev = bios->dev;
2298 const uint8_t *ramcfg = nv05_memory_config(bios);
2299 uint32_t patt = 0xdeadbeef;
2300 struct io_mapping *fb;
2303 /* Map the framebuffer aperture */
2304 fb = io_mapping_create_wc(pci_resource_start(dev->pdev, 1),
2305 pci_resource_len(dev->pdev, 1));
2310 NVWriteVgaSeq(dev, 0, 1, NVReadVgaSeq(dev, 0, 1) | 0x20);
2312 if (bios_rd32(bios, NV04_PFB_BOOT_0) & NV04_PFB_BOOT_0_UMA_ENABLE)
2315 bios_md32(bios, NV04_PFB_DEBUG_0, NV04_PFB_DEBUG_0_REFRESH_OFF, 0);
2317 /* If present load the hardcoded scrambling table */
2318 if (bios->legacy.mem_init_tbl_ptr) {
2319 uint32_t *scramble_tab = (uint32_t *)&bios->data[
2320 bios->legacy.mem_init_tbl_ptr + 0x10];
2322 for (i = 0; i < 8; i++)
2323 bios_wr32(bios, NV04_PFB_SCRAMBLE(i),
2324 ROM32(scramble_tab[i]));
2327 /* Set memory type/width/length defaults depending on the straps */
2328 bios_md32(bios, NV04_PFB_BOOT_0, 0x3f, ramcfg[0]);
2330 if (ramcfg[1] & 0x80)
2331 bios_md32(bios, NV04_PFB_CFG0, 0, NV04_PFB_CFG0_SCRAMBLE);
2333 bios_md32(bios, NV04_PFB_CFG1, 0x700001, (ramcfg[1] & 1) << 20);
2334 bios_md32(bios, NV04_PFB_CFG1, 0, 1);
2336 /* Probe memory bus width */
2337 for (i = 0; i < 4; i++)
2338 poke_fb(dev, fb, 4 * i, patt);
2340 if (peek_fb(dev, fb, 0xc) != patt)
2341 bios_md32(bios, NV04_PFB_BOOT_0,
2342 NV04_PFB_BOOT_0_RAM_WIDTH_128, 0);
2344 /* Probe memory length */
2345 v = bios_rd32(bios, NV04_PFB_BOOT_0) & NV04_PFB_BOOT_0_RAM_AMOUNT;
2347 if (v == NV04_PFB_BOOT_0_RAM_AMOUNT_32MB &&
2348 (!read_back_fb(dev, fb, 0x1000000, ++patt) ||
2349 !read_back_fb(dev, fb, 0, ++patt)))
2350 bios_md32(bios, NV04_PFB_BOOT_0, NV04_PFB_BOOT_0_RAM_AMOUNT,
2351 NV04_PFB_BOOT_0_RAM_AMOUNT_16MB);
2353 if (v == NV04_PFB_BOOT_0_RAM_AMOUNT_16MB &&
2354 !read_back_fb(dev, fb, 0x800000, ++patt))
2355 bios_md32(bios, NV04_PFB_BOOT_0, NV04_PFB_BOOT_0_RAM_AMOUNT,
2356 NV04_PFB_BOOT_0_RAM_AMOUNT_8MB);
2358 if (!read_back_fb(dev, fb, 0x400000, ++patt))
2359 bios_md32(bios, NV04_PFB_BOOT_0, NV04_PFB_BOOT_0_RAM_AMOUNT,
2360 NV04_PFB_BOOT_0_RAM_AMOUNT_4MB);
2364 NVWriteVgaSeq(dev, 0, 1, NVReadVgaSeq(dev, 0, 1) & ~0x20);
2366 io_mapping_free(fb);
2371 nv10_init_compute_mem(struct nvbios *bios)
2373 struct drm_device *dev = bios->dev;
2374 struct drm_nouveau_private *dev_priv = bios->dev->dev_private;
2375 const int mem_width[] = { 0x10, 0x00, 0x20 };
2376 const int mem_width_count = (dev_priv->chipset >= 0x17 ? 3 : 2);
2377 uint32_t patt = 0xdeadbeef;
2378 struct io_mapping *fb;
2381 /* Map the framebuffer aperture */
2382 fb = io_mapping_create_wc(pci_resource_start(dev->pdev, 1),
2383 pci_resource_len(dev->pdev, 1));
2387 bios_wr32(bios, NV10_PFB_REFCTRL, NV10_PFB_REFCTRL_VALID_1);
2389 /* Probe memory bus width */
2390 for (i = 0; i < mem_width_count; i++) {
2391 bios_md32(bios, NV04_PFB_CFG0, 0x30, mem_width[i]);
2393 for (j = 0; j < 4; j++) {
2394 for (k = 0; k < 4; k++)
2395 poke_fb(dev, fb, 0x1c, 0);
2397 poke_fb(dev, fb, 0x1c, patt);
2398 poke_fb(dev, fb, 0x3c, 0);
2400 if (peek_fb(dev, fb, 0x1c) == patt)
2401 goto mem_width_found;
2408 /* Probe amount of installed memory */
2409 for (i = 0; i < 4; i++) {
2410 int off = bios_rd32(bios, NV04_PFB_FIFO_DATA) - 0x100000;
2412 poke_fb(dev, fb, off, patt);
2413 poke_fb(dev, fb, 0, 0);
2415 peek_fb(dev, fb, 0);
2416 peek_fb(dev, fb, 0);
2417 peek_fb(dev, fb, 0);
2418 peek_fb(dev, fb, 0);
2420 if (peek_fb(dev, fb, off) == patt)
2424 /* IC missing - disable the upper half memory space. */
2425 bios_md32(bios, NV04_PFB_CFG0, 0x1000, 0);
2428 io_mapping_free(fb);
2433 nv20_init_compute_mem(struct nvbios *bios)
2435 struct drm_device *dev = bios->dev;
2436 struct drm_nouveau_private *dev_priv = bios->dev->dev_private;
2437 uint32_t mask = (dev_priv->chipset >= 0x25 ? 0x300 : 0x900);
2438 uint32_t amount, off;
2439 struct io_mapping *fb;
2441 /* Map the framebuffer aperture */
2442 fb = io_mapping_create_wc(pci_resource_start(dev->pdev, 1),
2443 pci_resource_len(dev->pdev, 1));
2447 bios_wr32(bios, NV10_PFB_REFCTRL, NV10_PFB_REFCTRL_VALID_1);
2449 /* Allow full addressing */
2450 bios_md32(bios, NV04_PFB_CFG0, 0, mask);
2452 amount = bios_rd32(bios, NV04_PFB_FIFO_DATA);
2453 for (off = amount; off > 0x2000000; off -= 0x2000000)
2454 poke_fb(dev, fb, off - 4, off);
2456 amount = bios_rd32(bios, NV04_PFB_FIFO_DATA);
2457 if (amount != peek_fb(dev, fb, amount - 4))
2458 /* IC missing - disable the upper half memory space. */
2459 bios_md32(bios, NV04_PFB_CFG0, mask, 0);
2461 io_mapping_free(fb);
2466 init_compute_mem(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
2469 * INIT_COMPUTE_MEM opcode: 0x63 ('c')
2471 * offset (8 bit): opcode
2473 * This opcode is meant to set the PFB memory config registers
2474 * appropriately so that we can correctly calculate how much VRAM it
2475 * has (on nv10 and better chipsets the amount of installed VRAM is
2476 * subsequently reported in NV_PFB_CSTATUS (0x10020C)).
2478 * The implementation of this opcode in general consists of several
2481 * 1) Determination of memory type and density. Only necessary for
2482 * really old chipsets, the memory type reported by the strap bits
2483 * (0x101000) is assumed to be accurate on nv05 and newer.
2485 * 2) Determination of the memory bus width. Usually done by a cunning
2486 * combination of writes to offsets 0x1c and 0x3c in the fb, and
2487 * seeing whether the written values are read back correctly.
2489 * Only necessary on nv0x-nv1x and nv34, on the other cards we can
2492 * 3) Determination of how many of the card's RAM pads have ICs
2493 * attached, usually done by a cunning combination of writes to an
2494 * offset slightly less than the maximum memory reported by
2495 * NV_PFB_CSTATUS, then seeing if the test pattern can be read back.
2497 * This appears to be a NOP on IGPs and NV4x or newer chipsets, both io
2498 * logs of the VBIOS and kmmio traces of the binary driver POSTing the
2499 * card show nothing being done for this opcode. Why is it still listed
2503 /* no iexec->execute check by design */
2505 struct drm_nouveau_private *dev_priv = bios->dev->dev_private;
2508 if (dev_priv->chipset >= 0x40 ||
2509 dev_priv->chipset == 0x1a ||
2510 dev_priv->chipset == 0x1f)
2512 else if (dev_priv->chipset >= 0x20 &&
2513 dev_priv->chipset != 0x34)
2514 ret = nv20_init_compute_mem(bios);
2515 else if (dev_priv->chipset >= 0x10)
2516 ret = nv10_init_compute_mem(bios);
2517 else if (dev_priv->chipset >= 0x5)
2518 ret = nv05_init_compute_mem(bios);
2520 ret = nv04_init_compute_mem(bios);
2529 init_reset(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
2532 * INIT_RESET opcode: 0x65 ('e')
2534 * offset (8 bit): opcode
2535 * offset + 1 (32 bit): register
2536 * offset + 5 (32 bit): value1
2537 * offset + 9 (32 bit): value2
2539 * Assign "value1" to "register", then assign "value2" to "register"
2542 uint32_t reg = ROM32(bios->data[offset + 1]);
2543 uint32_t value1 = ROM32(bios->data[offset + 5]);
2544 uint32_t value2 = ROM32(bios->data[offset + 9]);
2545 uint32_t pci_nv_19, pci_nv_20;
2547 /* no iexec->execute check by design */
2549 pci_nv_19 = bios_rd32(bios, NV_PBUS_PCI_NV_19);
2550 bios_wr32(bios, NV_PBUS_PCI_NV_19, pci_nv_19 & ~0xf00);
2552 bios_wr32(bios, reg, value1);
2556 bios_wr32(bios, reg, value2);
2557 bios_wr32(bios, NV_PBUS_PCI_NV_19, pci_nv_19);
2559 pci_nv_20 = bios_rd32(bios, NV_PBUS_PCI_NV_20);
2560 pci_nv_20 &= ~NV_PBUS_PCI_NV_20_ROM_SHADOW_ENABLED; /* 0xfffffffe */
2561 bios_wr32(bios, NV_PBUS_PCI_NV_20, pci_nv_20);
2567 init_configure_mem(struct nvbios *bios, uint16_t offset,
2568 struct init_exec *iexec)
2571 * INIT_CONFIGURE_MEM opcode: 0x66 ('f')
2573 * offset (8 bit): opcode
2575 * Equivalent to INIT_DONE on bios version 3 or greater.
2576 * For early bios versions, sets up the memory registers, using values
2577 * taken from the memory init table
2580 /* no iexec->execute check by design */
2582 uint16_t meminitoffs = bios->legacy.mem_init_tbl_ptr + MEM_INIT_SIZE * (bios_idxprt_rd(bios, NV_CIO_CRX__COLOR, NV_CIO_CRE_SCRATCH4__INDEX) >> 4);
2583 uint16_t seqtbloffs = bios->legacy.sdr_seq_tbl_ptr, meminitdata = meminitoffs + 6;
2586 if (bios->major_version > 2)
2589 bios_idxprt_wr(bios, NV_VIO_SRX, NV_VIO_SR_CLOCK_INDEX, bios_idxprt_rd(
2590 bios, NV_VIO_SRX, NV_VIO_SR_CLOCK_INDEX) | 0x20);
2592 if (bios->data[meminitoffs] & 1)
2593 seqtbloffs = bios->legacy.ddr_seq_tbl_ptr;
2595 for (reg = ROM32(bios->data[seqtbloffs]);
2597 reg = ROM32(bios->data[seqtbloffs += 4])) {
2601 data = NV04_PFB_PRE_CMD_PRECHARGE;
2604 data = NV04_PFB_PAD_CKE_NORMAL;
2607 data = NV04_PFB_REF_CMD_REFRESH;
2610 data = ROM32(bios->data[meminitdata]);
2612 if (data == 0xffffffff)
2616 bios_wr32(bios, reg, data);
2623 init_configure_clk(struct nvbios *bios, uint16_t offset,
2624 struct init_exec *iexec)
2627 * INIT_CONFIGURE_CLK opcode: 0x67 ('g')
2629 * offset (8 bit): opcode
2631 * Equivalent to INIT_DONE on bios version 3 or greater.
2632 * For early bios versions, sets up the NVClk and MClk PLLs, using
2633 * values taken from the memory init table
2636 /* no iexec->execute check by design */
2638 uint16_t meminitoffs = bios->legacy.mem_init_tbl_ptr + MEM_INIT_SIZE * (bios_idxprt_rd(bios, NV_CIO_CRX__COLOR, NV_CIO_CRE_SCRATCH4__INDEX) >> 4);
2641 if (bios->major_version > 2)
2644 clock = ROM16(bios->data[meminitoffs + 4]) * 10;
2645 setPLL(bios, NV_PRAMDAC_NVPLL_COEFF, clock);
2647 clock = ROM16(bios->data[meminitoffs + 2]) * 10;
2648 if (bios->data[meminitoffs] & 1) /* DDR */
2650 setPLL(bios, NV_PRAMDAC_MPLL_COEFF, clock);
2656 init_configure_preinit(struct nvbios *bios, uint16_t offset,
2657 struct init_exec *iexec)
2660 * INIT_CONFIGURE_PREINIT opcode: 0x68 ('h')
2662 * offset (8 bit): opcode
2664 * Equivalent to INIT_DONE on bios version 3 or greater.
2665 * For early bios versions, does early init, loading ram and crystal
2666 * configuration from straps into CR3C
2669 /* no iexec->execute check by design */
2671 uint32_t straps = bios_rd32(bios, NV_PEXTDEV_BOOT_0);
2672 uint8_t cr3c = ((straps << 2) & 0xf0) | (straps & 0x40) >> 6;
2674 if (bios->major_version > 2)
2677 bios_idxprt_wr(bios, NV_CIO_CRX__COLOR,
2678 NV_CIO_CRE_SCRATCH4__INDEX, cr3c);
2684 init_io(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
2687 * INIT_IO opcode: 0x69 ('i')
2689 * offset (8 bit): opcode
2690 * offset + 1 (16 bit): CRTC port
2691 * offset + 3 (8 bit): mask
2692 * offset + 4 (8 bit): data
2694 * Assign ((IOVAL("crtc port") & "mask") | "data") to "crtc port"
2697 struct drm_nouveau_private *dev_priv = bios->dev->dev_private;
2698 uint16_t crtcport = ROM16(bios->data[offset + 1]);
2699 uint8_t mask = bios->data[offset + 3];
2700 uint8_t data = bios->data[offset + 4];
2702 if (!iexec->execute)
2705 BIOSLOG(bios, "0x%04X: Port: 0x%04X, Mask: 0x%02X, Data: 0x%02X\n",
2706 offset, crtcport, mask, data);
2709 * I have no idea what this does, but NVIDIA do this magic sequence
2710 * in the places where this INIT_IO happens..
2712 if (dev_priv->card_type >= NV_50 && crtcport == 0x3c3 && data == 1) {
2715 bios_wr32(bios, 0x614100, (bios_rd32(
2716 bios, 0x614100) & 0x0fffffff) | 0x00800000);
2718 bios_wr32(bios, 0x00e18c, bios_rd32(
2719 bios, 0x00e18c) | 0x00020000);
2721 bios_wr32(bios, 0x614900, (bios_rd32(
2722 bios, 0x614900) & 0x0fffffff) | 0x00800000);
2724 bios_wr32(bios, 0x000200, bios_rd32(
2725 bios, 0x000200) & ~0x40000000);
2729 bios_wr32(bios, 0x00e18c, bios_rd32(
2730 bios, 0x00e18c) & ~0x00020000);
2732 bios_wr32(bios, 0x000200, bios_rd32(
2733 bios, 0x000200) | 0x40000000);
2735 bios_wr32(bios, 0x614100, 0x00800018);
2736 bios_wr32(bios, 0x614900, 0x00800018);
2740 bios_wr32(bios, 0x614100, 0x10000018);
2741 bios_wr32(bios, 0x614900, 0x10000018);
2743 for (i = 0; i < 3; i++)
2744 bios_wr32(bios, 0x614280 + (i*0x800), bios_rd32(
2745 bios, 0x614280 + (i*0x800)) & 0xf0f0f0f0);
2747 for (i = 0; i < 2; i++)
2748 bios_wr32(bios, 0x614300 + (i*0x800), bios_rd32(
2749 bios, 0x614300 + (i*0x800)) & 0xfffff0f0);
2751 for (i = 0; i < 3; i++)
2752 bios_wr32(bios, 0x614380 + (i*0x800), bios_rd32(
2753 bios, 0x614380 + (i*0x800)) & 0xfffff0f0);
2755 for (i = 0; i < 2; i++)
2756 bios_wr32(bios, 0x614200 + (i*0x800), bios_rd32(
2757 bios, 0x614200 + (i*0x800)) & 0xfffffff0);
2759 for (i = 0; i < 2; i++)
2760 bios_wr32(bios, 0x614108 + (i*0x800), bios_rd32(
2761 bios, 0x614108 + (i*0x800)) & 0x0fffffff);
2765 bios_port_wr(bios, crtcport, (bios_port_rd(bios, crtcport) & mask) |
2771 init_sub(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
2774 * INIT_SUB opcode: 0x6B ('k')
2776 * offset (8 bit): opcode
2777 * offset + 1 (8 bit): script number
2779 * Execute script number "script number", as a subroutine
2782 uint8_t sub = bios->data[offset + 1];
2784 if (!iexec->execute)
2787 BIOSLOG(bios, "0x%04X: Calling script %d\n", offset, sub);
2789 parse_init_table(bios,
2790 ROM16(bios->data[bios->init_script_tbls_ptr + sub * 2]),
2793 BIOSLOG(bios, "0x%04X: End of script %d\n", offset, sub);
2799 init_ram_condition(struct nvbios *bios, uint16_t offset,
2800 struct init_exec *iexec)
2803 * INIT_RAM_CONDITION opcode: 0x6D ('m')
2805 * offset (8 bit): opcode
2806 * offset + 1 (8 bit): mask
2807 * offset + 2 (8 bit): cmpval
2809 * Test if (NV04_PFB_BOOT_0 & "mask") equals "cmpval".
2810 * If condition not met skip subsequent opcodes until condition is
2811 * inverted (INIT_NOT), or we hit INIT_RESUME
2814 uint8_t mask = bios->data[offset + 1];
2815 uint8_t cmpval = bios->data[offset + 2];
2818 if (!iexec->execute)
2821 data = bios_rd32(bios, NV04_PFB_BOOT_0) & mask;
2823 BIOSLOG(bios, "0x%04X: Checking if 0x%08X equals 0x%08X\n",
2824 offset, data, cmpval);
2827 BIOSLOG(bios, "0x%04X: Condition fulfilled -- continuing to execute\n", offset);
2829 BIOSLOG(bios, "0x%04X: Condition not fulfilled -- skipping following commands\n", offset);
2830 iexec->execute = false;
2837 init_nv_reg(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
2840 * INIT_NV_REG opcode: 0x6E ('n')
2842 * offset (8 bit): opcode
2843 * offset + 1 (32 bit): register
2844 * offset + 5 (32 bit): mask
2845 * offset + 9 (32 bit): data
2847 * Assign ((REGVAL("register") & "mask") | "data") to "register"
2850 uint32_t reg = ROM32(bios->data[offset + 1]);
2851 uint32_t mask = ROM32(bios->data[offset + 5]);
2852 uint32_t data = ROM32(bios->data[offset + 9]);
2854 if (!iexec->execute)
2857 BIOSLOG(bios, "0x%04X: Reg: 0x%08X, Mask: 0x%08X, Data: 0x%08X\n",
2858 offset, reg, mask, data);
2860 bios_wr32(bios, reg, (bios_rd32(bios, reg) & mask) | data);
2866 init_macro(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
2869 * INIT_MACRO opcode: 0x6F ('o')
2871 * offset (8 bit): opcode
2872 * offset + 1 (8 bit): macro number
2874 * Look up macro index "macro number" in the macro index table.
2875 * The macro index table entry has 1 byte for the index in the macro
2876 * table, and 1 byte for the number of times to repeat the macro.
2877 * The macro table entry has 4 bytes for the register address and
2878 * 4 bytes for the value to write to that register
2881 uint8_t macro_index_tbl_idx = bios->data[offset + 1];
2882 uint16_t tmp = bios->macro_index_tbl_ptr + (macro_index_tbl_idx * MACRO_INDEX_SIZE);
2883 uint8_t macro_tbl_idx = bios->data[tmp];
2884 uint8_t count = bios->data[tmp + 1];
2888 if (!iexec->execute)
2891 BIOSLOG(bios, "0x%04X: Macro: 0x%02X, MacroTableIndex: 0x%02X, "
2893 offset, macro_index_tbl_idx, macro_tbl_idx, count);
2895 for (i = 0; i < count; i++) {
2896 uint16_t macroentryptr = bios->macro_tbl_ptr + (macro_tbl_idx + i) * MACRO_SIZE;
2898 reg = ROM32(bios->data[macroentryptr]);
2899 data = ROM32(bios->data[macroentryptr + 4]);
2901 bios_wr32(bios, reg, data);
2908 init_done(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
2911 * INIT_DONE opcode: 0x71 ('q')
2913 * offset (8 bit): opcode
2915 * End the current script
2918 /* mild retval abuse to stop parsing this table */
2923 init_resume(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
2926 * INIT_RESUME opcode: 0x72 ('r')
2928 * offset (8 bit): opcode
2930 * End the current execute / no-execute condition
2936 iexec->execute = true;
2937 BIOSLOG(bios, "0x%04X: ---- Executing following commands ----\n", offset);
2943 init_time(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
2946 * INIT_TIME opcode: 0x74 ('t')
2948 * offset (8 bit): opcode
2949 * offset + 1 (16 bit): time
2951 * Sleep for "time" microseconds.
2954 unsigned time = ROM16(bios->data[offset + 1]);
2956 if (!iexec->execute)
2959 BIOSLOG(bios, "0x%04X: Sleeping for 0x%04X microseconds\n",
2965 msleep((time + 900) / 1000);
2971 init_condition(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
2974 * INIT_CONDITION opcode: 0x75 ('u')
2976 * offset (8 bit): opcode
2977 * offset + 1 (8 bit): condition number
2979 * Check condition "condition number" in the condition table.
2980 * If condition not met skip subsequent opcodes until condition is
2981 * inverted (INIT_NOT), or we hit INIT_RESUME
2984 uint8_t cond = bios->data[offset + 1];
2986 if (!iexec->execute)
2989 BIOSLOG(bios, "0x%04X: Condition: 0x%02X\n", offset, cond);
2991 if (bios_condition_met(bios, offset, cond))
2992 BIOSLOG(bios, "0x%04X: Condition fulfilled -- continuing to execute\n", offset);
2994 BIOSLOG(bios, "0x%04X: Condition not fulfilled -- skipping following commands\n", offset);
2995 iexec->execute = false;
3002 init_io_condition(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
3005 * INIT_IO_CONDITION opcode: 0x76
3007 * offset (8 bit): opcode
3008 * offset + 1 (8 bit): condition number
3010 * Check condition "condition number" in the io condition table.
3011 * If condition not met skip subsequent opcodes until condition is
3012 * inverted (INIT_NOT), or we hit INIT_RESUME
3015 uint8_t cond = bios->data[offset + 1];
3017 if (!iexec->execute)
3020 BIOSLOG(bios, "0x%04X: IO condition: 0x%02X\n", offset, cond);
3022 if (io_condition_met(bios, offset, cond))
3023 BIOSLOG(bios, "0x%04X: Condition fulfilled -- continuing to execute\n", offset);
3025 BIOSLOG(bios, "0x%04X: Condition not fulfilled -- skipping following commands\n", offset);
3026 iexec->execute = false;
3033 init_index_io(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
3036 * INIT_INDEX_IO opcode: 0x78 ('x')
3038 * offset (8 bit): opcode
3039 * offset + 1 (16 bit): CRTC port
3040 * offset + 3 (8 bit): CRTC index
3041 * offset + 4 (8 bit): mask
3042 * offset + 5 (8 bit): data
3044 * Read value at index "CRTC index" on "CRTC port", AND with "mask",
3045 * OR with "data", write-back
3048 uint16_t crtcport = ROM16(bios->data[offset + 1]);
3049 uint8_t crtcindex = bios->data[offset + 3];
3050 uint8_t mask = bios->data[offset + 4];
3051 uint8_t data = bios->data[offset + 5];
3054 if (!iexec->execute)
3057 BIOSLOG(bios, "0x%04X: Port: 0x%04X, Index: 0x%02X, Mask: 0x%02X, "
3059 offset, crtcport, crtcindex, mask, data);
3061 value = (bios_idxprt_rd(bios, crtcport, crtcindex) & mask) | data;
3062 bios_idxprt_wr(bios, crtcport, crtcindex, value);
3068 init_pll(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)