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"
30 #include "nouveau_gpio.h"
32 #include <linux/io-mapping.h>
34 /* these defines are made up */
35 #define NV_CIO_CRE_44_HEADA 0x0
36 #define NV_CIO_CRE_44_HEADB 0x3
37 #define FEATURE_MOBILE 0x10 /* also FEATURE_QUADRO for BMP */
41 #define BIOSLOG(sip, fmt, arg...) NV_DEBUG(sip->dev, fmt, ##arg)
42 #define LOG_OLD_VALUE(x)
49 static bool nv_cksum(const uint8_t *data, unsigned int length)
52 * There's a few checksums in the BIOS, so here's a generic checking
58 for (i = 0; i < length; i++)
68 score_vbios(struct nvbios *bios, const bool writeable)
70 if (!bios->data || bios->data[0] != 0x55 || bios->data[1] != 0xAA) {
71 NV_TRACEWARN(bios->dev, "... BIOS signature not found\n");
75 if (nv_cksum(bios->data, bios->data[2] * 512)) {
76 NV_TRACEWARN(bios->dev, "... BIOS checksum invalid\n");
77 /* if a ro image is somewhat bad, it's probably all rubbish */
78 return writeable ? 2 : 1;
81 NV_TRACE(bios->dev, "... appears to be valid\n");
86 bios_shadow_prom(struct nvbios *bios)
88 struct drm_device *dev = bios->dev;
89 struct drm_nouveau_private *dev_priv = dev->dev_private;
94 /* enable access to rom */
95 if (dev_priv->card_type >= NV_50)
98 pcireg = NV_PBUS_PCI_NV_20;
99 access = nv_mask(dev, pcireg, 0x00000001, 0x00000000);
101 /* bail if no rom signature, with a workaround for a PROM reading
102 * issue on some chipsets. the first read after a period of
103 * inactivity returns the wrong result, so retry the first header
104 * byte a few times before giving up as a workaround
108 if (nv_rd08(dev, NV_PROM_OFFSET + 0) == 0x55)
112 if (!i || nv_rd08(dev, NV_PROM_OFFSET + 1) != 0xaa)
115 /* additional check (see note below) - read PCI record header */
116 pcir = nv_rd08(dev, NV_PROM_OFFSET + 0x18) |
117 nv_rd08(dev, NV_PROM_OFFSET + 0x19) << 8;
118 if (nv_rd08(dev, NV_PROM_OFFSET + pcir + 0) != 'P' ||
119 nv_rd08(dev, NV_PROM_OFFSET + pcir + 1) != 'C' ||
120 nv_rd08(dev, NV_PROM_OFFSET + pcir + 2) != 'I' ||
121 nv_rd08(dev, NV_PROM_OFFSET + pcir + 3) != 'R')
124 /* read entire bios image to system memory */
125 bios->length = nv_rd08(dev, NV_PROM_OFFSET + 2) * 512;
126 bios->data = kmalloc(bios->length, GFP_KERNEL);
128 for (i = 0; i < bios->length; i++)
129 bios->data[i] = nv_rd08(dev, NV_PROM_OFFSET + i);
133 /* disable access to rom */
134 nv_wr32(dev, pcireg, access);
138 bios_shadow_pramin(struct nvbios *bios)
140 struct drm_device *dev = bios->dev;
141 struct drm_nouveau_private *dev_priv = dev->dev_private;
145 if (dev_priv->card_type >= NV_50) {
146 u64 addr = (u64)(nv_rd32(dev, 0x619f04) & 0xffffff00) << 8;
148 addr = (u64)nv_rd32(dev, 0x001700) << 16;
152 bar0 = nv_mask(dev, 0x001700, 0xffffffff, addr >> 16);
155 /* bail if no rom signature */
156 if (nv_rd08(dev, NV_PRAMIN_OFFSET + 0) != 0x55 ||
157 nv_rd08(dev, NV_PRAMIN_OFFSET + 1) != 0xaa)
160 bios->length = nv_rd08(dev, NV_PRAMIN_OFFSET + 2) * 512;
161 bios->data = kmalloc(bios->length, GFP_KERNEL);
163 for (i = 0; i < bios->length; i++)
164 bios->data[i] = nv_rd08(dev, NV_PRAMIN_OFFSET + i);
168 if (dev_priv->card_type >= NV_50)
169 nv_wr32(dev, 0x001700, bar0);
173 bios_shadow_pci(struct nvbios *bios)
175 struct pci_dev *pdev = bios->dev->pdev;
178 if (!pci_enable_rom(pdev)) {
179 void __iomem *rom = pci_map_rom(pdev, &length);
181 bios->data = kmalloc(length, GFP_KERNEL);
183 memcpy_fromio(bios->data, rom, length);
184 bios->length = length;
186 pci_unmap_rom(pdev, rom);
189 pci_disable_rom(pdev);
194 bios_shadow_acpi(struct nvbios *bios)
196 struct pci_dev *pdev = bios->dev->pdev;
200 if (!nouveau_acpi_rom_supported(pdev))
203 ret = nouveau_acpi_get_bios_chunk(data, 0, sizeof(data));
204 if (ret != sizeof(data))
207 bios->length = min(data[2] * 512, 65536);
208 bios->data = kmalloc(bios->length, GFP_KERNEL);
215 int size = (len > ROM_BIOS_PAGE) ? ROM_BIOS_PAGE : len;
217 ret = nouveau_acpi_get_bios_chunk(bios->data, ptr, size);
231 void (*shadow)(struct nvbios *);
239 bios_shadow(struct drm_device *dev)
241 struct methods shadow_methods[] = {
242 { "PRAMIN", bios_shadow_pramin, true, 0, 0, NULL },
243 { "PROM", bios_shadow_prom, false, 0, 0, NULL },
244 { "ACPI", bios_shadow_acpi, true, 0, 0, NULL },
245 { "PCIROM", bios_shadow_pci, true, 0, 0, NULL },
248 struct drm_nouveau_private *dev_priv = dev->dev_private;
249 struct nvbios *bios = &dev_priv->vbios;
250 struct methods *mthd, *best;
253 mthd = shadow_methods;
255 if (strcasecmp(nouveau_vbios, mthd->desc))
257 NV_INFO(dev, "VBIOS source: %s\n", mthd->desc);
260 mthd->score = score_vbios(bios, mthd->rw);
263 } while ((++mthd)->shadow);
265 NV_ERROR(dev, "VBIOS source \'%s\' invalid\n", nouveau_vbios);
268 mthd = shadow_methods;
270 NV_TRACE(dev, "Checking %s for VBIOS\n", mthd->desc);
272 mthd->score = score_vbios(bios, mthd->rw);
273 mthd->size = bios->length;
274 mthd->data = bios->data;
275 } while (mthd->score != 3 && (++mthd)->shadow);
277 mthd = shadow_methods;
280 if (mthd->score > best->score) {
284 } while ((++mthd)->shadow);
287 NV_TRACE(dev, "Using VBIOS from %s\n", best->desc);
288 bios->length = best->size;
289 bios->data = best->data;
293 NV_ERROR(dev, "No valid VBIOS image found\n");
297 struct init_tbl_entry {
301 * > 0: success, length of opcode
302 * 0: success, but abort further parsing of table (INIT_DONE etc)
303 * < 0: failure, table parsing will be aborted
305 int (*handler)(struct nvbios *, uint16_t, struct init_exec *);
308 static int parse_init_table(struct nvbios *, uint16_t, struct init_exec *);
310 #define MACRO_INDEX_SIZE 2
312 #define CONDITION_SIZE 12
313 #define IO_FLAG_CONDITION_SIZE 9
314 #define IO_CONDITION_SIZE 5
315 #define MEM_INIT_SIZE 66
317 static void still_alive(void)
326 munge_reg(struct nvbios *bios, uint32_t reg)
328 struct drm_nouveau_private *dev_priv = bios->dev->dev_private;
329 struct dcb_entry *dcbent = bios->display.output;
331 if (dev_priv->card_type < NV_50)
334 if (reg & 0x80000000) {
335 BUG_ON(bios->display.crtc < 0);
336 reg += bios->display.crtc * 0x800;
339 if (reg & 0x40000000) {
342 reg += (ffs(dcbent->or) - 1) * 0x800;
343 if ((reg & 0x20000000) && !(dcbent->sorconf.link & 1))
352 valid_reg(struct nvbios *bios, uint32_t reg)
354 struct drm_nouveau_private *dev_priv = bios->dev->dev_private;
355 struct drm_device *dev = bios->dev;
357 /* C51 has misaligned regs on purpose. Marvellous */
359 (reg & 0x1 && dev_priv->vbios.chip_version != 0x51))
360 NV_ERROR(dev, "======= misaligned reg 0x%08X =======\n", reg);
362 /* warn on C51 regs that haven't been verified accessible in tracing */
363 if (reg & 0x1 && dev_priv->vbios.chip_version == 0x51 &&
364 reg != 0x130d && reg != 0x1311 && reg != 0x60081d)
365 NV_WARN(dev, "=== C51 misaligned reg 0x%08X not verified ===\n",
368 if (reg >= (8*1024*1024)) {
369 NV_ERROR(dev, "=== reg 0x%08x out of mapped bounds ===\n", reg);
377 valid_idx_port(struct nvbios *bios, uint16_t port)
379 struct drm_nouveau_private *dev_priv = bios->dev->dev_private;
380 struct drm_device *dev = bios->dev;
383 * If adding more ports here, the read/write functions below will need
384 * updating so that the correct mmio range (PRMCIO, PRMDIO, PRMVIO) is
385 * used for the port in question
387 if (dev_priv->card_type < NV_50) {
388 if (port == NV_CIO_CRX__COLOR)
390 if (port == NV_VIO_SRX)
393 if (port == NV_CIO_CRX__COLOR)
397 NV_ERROR(dev, "========== unknown indexed io port 0x%04X ==========\n",
404 valid_port(struct nvbios *bios, uint16_t port)
406 struct drm_device *dev = bios->dev;
409 * If adding more ports here, the read/write functions below will need
410 * updating so that the correct mmio range (PRMCIO, PRMDIO, PRMVIO) is
411 * used for the port in question
413 if (port == NV_VIO_VSE2)
416 NV_ERROR(dev, "========== unknown io port 0x%04X ==========\n", port);
422 bios_rd32(struct nvbios *bios, uint32_t reg)
426 reg = munge_reg(bios, reg);
427 if (!valid_reg(bios, reg))
431 * C51 sometimes uses regs with bit0 set in the address. For these
432 * cases there should exist a translation in a BIOS table to an IO
433 * port address which the BIOS uses for accessing the reg
435 * These only seem to appear for the power control regs to a flat panel,
436 * and the GPIO regs at 0x60081*. In C51 mmio traces the normal regs
437 * for 0x1308 and 0x1310 are used - hence the mask below. An S3
438 * suspend-resume mmio trace from a C51 will be required to see if this
439 * is true for the power microcode in 0x14.., or whether the direct IO
440 * port access method is needed
445 data = nv_rd32(bios->dev, reg);
447 BIOSLOG(bios, " Read: Reg: 0x%08X, Data: 0x%08X\n", reg, data);
453 bios_wr32(struct nvbios *bios, uint32_t reg, uint32_t data)
455 struct drm_nouveau_private *dev_priv = bios->dev->dev_private;
457 reg = munge_reg(bios, reg);
458 if (!valid_reg(bios, reg))
461 /* see note in bios_rd32 */
465 LOG_OLD_VALUE(bios_rd32(bios, reg));
466 BIOSLOG(bios, " Write: Reg: 0x%08X, Data: 0x%08X\n", reg, data);
468 if (dev_priv->vbios.execute) {
470 nv_wr32(bios->dev, reg, data);
475 bios_idxprt_rd(struct nvbios *bios, uint16_t port, uint8_t index)
477 struct drm_nouveau_private *dev_priv = bios->dev->dev_private;
478 struct drm_device *dev = bios->dev;
481 if (!valid_idx_port(bios, port))
484 if (dev_priv->card_type < NV_50) {
485 if (port == NV_VIO_SRX)
486 data = NVReadVgaSeq(dev, bios->state.crtchead, index);
487 else /* assume NV_CIO_CRX__COLOR */
488 data = NVReadVgaCrtc(dev, bios->state.crtchead, index);
492 data32 = bios_rd32(bios, NV50_PDISPLAY_VGACRTC(index & ~3));
493 data = (data32 >> ((index & 3) << 3)) & 0xff;
496 BIOSLOG(bios, " Indexed IO read: Port: 0x%04X, Index: 0x%02X, "
497 "Head: 0x%02X, Data: 0x%02X\n",
498 port, index, bios->state.crtchead, data);
503 bios_idxprt_wr(struct nvbios *bios, uint16_t port, uint8_t index, uint8_t data)
505 struct drm_nouveau_private *dev_priv = bios->dev->dev_private;
506 struct drm_device *dev = bios->dev;
508 if (!valid_idx_port(bios, port))
512 * The current head is maintained in the nvbios member state.crtchead.
513 * We trap changes to CR44 and update the head variable and hence the
514 * register set written.
515 * As CR44 only exists on CRTC0, we update crtchead to head0 in advance
516 * of the write, and to head1 after the write
518 if (port == NV_CIO_CRX__COLOR && index == NV_CIO_CRE_44 &&
519 data != NV_CIO_CRE_44_HEADB)
520 bios->state.crtchead = 0;
522 LOG_OLD_VALUE(bios_idxprt_rd(bios, port, index));
523 BIOSLOG(bios, " Indexed IO write: Port: 0x%04X, Index: 0x%02X, "
524 "Head: 0x%02X, Data: 0x%02X\n",
525 port, index, bios->state.crtchead, data);
527 if (bios->execute && dev_priv->card_type < NV_50) {
529 if (port == NV_VIO_SRX)
530 NVWriteVgaSeq(dev, bios->state.crtchead, index, data);
531 else /* assume NV_CIO_CRX__COLOR */
532 NVWriteVgaCrtc(dev, bios->state.crtchead, index, data);
535 uint32_t data32, shift = (index & 3) << 3;
539 data32 = bios_rd32(bios, NV50_PDISPLAY_VGACRTC(index & ~3));
540 data32 &= ~(0xff << shift);
541 data32 |= (data << shift);
542 bios_wr32(bios, NV50_PDISPLAY_VGACRTC(index & ~3), data32);
545 if (port == NV_CIO_CRX__COLOR &&
546 index == NV_CIO_CRE_44 && data == NV_CIO_CRE_44_HEADB)
547 bios->state.crtchead = 1;
551 bios_port_rd(struct nvbios *bios, uint16_t port)
553 uint8_t data, head = bios->state.crtchead;
555 if (!valid_port(bios, port))
558 data = NVReadPRMVIO(bios->dev, head, NV_PRMVIO0_OFFSET + port);
560 BIOSLOG(bios, " IO read: Port: 0x%04X, Head: 0x%02X, Data: 0x%02X\n",
567 bios_port_wr(struct nvbios *bios, uint16_t port, uint8_t data)
569 int head = bios->state.crtchead;
571 if (!valid_port(bios, port))
574 LOG_OLD_VALUE(bios_port_rd(bios, port));
575 BIOSLOG(bios, " IO write: Port: 0x%04X, Head: 0x%02X, Data: 0x%02X\n",
582 NVWritePRMVIO(bios->dev, head, NV_PRMVIO0_OFFSET + port, data);
586 io_flag_condition_met(struct nvbios *bios, uint16_t offset, uint8_t cond)
589 * The IO flag condition entry has 2 bytes for the CRTC port; 1 byte
590 * for the CRTC index; 1 byte for the mask to apply to the value
591 * retrieved from the CRTC; 1 byte for the shift right to apply to the
592 * masked CRTC value; 2 bytes for the offset to the flag array, to
593 * which the shifted value is added; 1 byte for the mask applied to the
594 * value read from the flag array; and 1 byte for the value to compare
595 * against the masked byte from the flag table.
598 uint16_t condptr = bios->io_flag_condition_tbl_ptr + cond * IO_FLAG_CONDITION_SIZE;
599 uint16_t crtcport = ROM16(bios->data[condptr]);
600 uint8_t crtcindex = bios->data[condptr + 2];
601 uint8_t mask = bios->data[condptr + 3];
602 uint8_t shift = bios->data[condptr + 4];
603 uint16_t flagarray = ROM16(bios->data[condptr + 5]);
604 uint8_t flagarraymask = bios->data[condptr + 7];
605 uint8_t cmpval = bios->data[condptr + 8];
608 BIOSLOG(bios, "0x%04X: Port: 0x%04X, Index: 0x%02X, Mask: 0x%02X, "
609 "Shift: 0x%02X, FlagArray: 0x%04X, FAMask: 0x%02X, "
611 offset, crtcport, crtcindex, mask, shift, flagarray, flagarraymask, cmpval);
613 data = bios_idxprt_rd(bios, crtcport, crtcindex);
615 data = bios->data[flagarray + ((data & mask) >> shift)];
616 data &= flagarraymask;
618 BIOSLOG(bios, "0x%04X: Checking if 0x%02X equals 0x%02X\n",
619 offset, data, cmpval);
621 return (data == cmpval);
625 bios_condition_met(struct nvbios *bios, uint16_t offset, uint8_t cond)
628 * The condition table entry has 4 bytes for the address of the
629 * register to check, 4 bytes for a mask to apply to the register and
630 * 4 for a test comparison value
633 uint16_t condptr = bios->condition_tbl_ptr + cond * CONDITION_SIZE;
634 uint32_t reg = ROM32(bios->data[condptr]);
635 uint32_t mask = ROM32(bios->data[condptr + 4]);
636 uint32_t cmpval = ROM32(bios->data[condptr + 8]);
639 BIOSLOG(bios, "0x%04X: Cond: 0x%02X, Reg: 0x%08X, Mask: 0x%08X\n",
640 offset, cond, reg, mask);
642 data = bios_rd32(bios, reg) & mask;
644 BIOSLOG(bios, "0x%04X: Checking if 0x%08X equals 0x%08X\n",
645 offset, data, cmpval);
647 return (data == cmpval);
651 io_condition_met(struct nvbios *bios, uint16_t offset, uint8_t cond)
654 * The IO condition entry has 2 bytes for the IO port address; 1 byte
655 * for the index to write to io_port; 1 byte for the mask to apply to
656 * the byte read from io_port+1; and 1 byte for the value to compare
657 * against the masked byte.
660 uint16_t condptr = bios->io_condition_tbl_ptr + cond * IO_CONDITION_SIZE;
661 uint16_t io_port = ROM16(bios->data[condptr]);
662 uint8_t port_index = bios->data[condptr + 2];
663 uint8_t mask = bios->data[condptr + 3];
664 uint8_t cmpval = bios->data[condptr + 4];
666 uint8_t data = bios_idxprt_rd(bios, io_port, port_index) & mask;
668 BIOSLOG(bios, "0x%04X: Checking if 0x%02X equals 0x%02X\n",
669 offset, data, cmpval);
671 return (data == cmpval);
675 nv50_pll_set(struct drm_device *dev, uint32_t reg, uint32_t clk)
677 struct drm_nouveau_private *dev_priv = dev->dev_private;
678 struct nouveau_pll_vals pll;
679 struct pll_lims pll_limits;
680 u32 ctrl, mask, coef;
683 ret = get_pll_limits(dev, reg, &pll_limits);
687 clk = nouveau_calc_pll_mnp(dev, &pll_limits, clk, &pll);
691 coef = pll.N1 << 8 | pll.M1;
692 ctrl = pll.log2P << 16;
694 if (reg == 0x004008) {
696 ctrl |= (pll_limits.log2p_bias << 19);
697 ctrl |= (pll.log2P << 22);
700 if (!dev_priv->vbios.execute)
703 nv_mask(dev, reg + 0, mask, ctrl);
704 nv_wr32(dev, reg + 4, coef);
709 setPLL(struct nvbios *bios, uint32_t reg, uint32_t clk)
711 struct drm_device *dev = bios->dev;
712 struct drm_nouveau_private *dev_priv = dev->dev_private;
714 struct pll_lims pll_lim;
715 struct nouveau_pll_vals pllvals;
718 if (dev_priv->card_type >= NV_50)
719 return nv50_pll_set(dev, reg, clk);
721 /* high regs (such as in the mac g5 table) are not -= 4 */
722 ret = get_pll_limits(dev, reg > 0x405c ? reg : reg - 4, &pll_lim);
726 clk = nouveau_calc_pll_mnp(dev, &pll_lim, clk, &pllvals);
732 nouveau_hw_setpll(dev, reg, &pllvals);
738 static int dcb_entry_idx_from_crtchead(struct drm_device *dev)
740 struct drm_nouveau_private *dev_priv = dev->dev_private;
741 struct nvbios *bios = &dev_priv->vbios;
744 * For the results of this function to be correct, CR44 must have been
745 * set (using bios_idxprt_wr to set crtchead), CR58 set for CR57 = 0,
746 * and the DCB table parsed, before the script calling the function is
747 * run. run_digital_op_script is example of how to do such setup
750 uint8_t dcb_entry = NVReadVgaCrtc5758(dev, bios->state.crtchead, 0);
752 if (dcb_entry > bios->dcb.entries) {
753 NV_ERROR(dev, "CR58 doesn't have a valid DCB entry currently "
754 "(%02X)\n", dcb_entry);
755 dcb_entry = 0x7f; /* unused / invalid marker */
761 static struct nouveau_i2c_chan *
762 init_i2c_device_find(struct drm_device *dev, int i2c_index)
764 if (i2c_index == 0xff) {
765 struct drm_nouveau_private *dev_priv = dev->dev_private;
766 struct dcb_table *dcb = &dev_priv->vbios.dcb;
767 /* note: dcb_entry_idx_from_crtchead needs pre-script set-up */
768 int idx = dcb_entry_idx_from_crtchead(dev);
770 i2c_index = NV_I2C_DEFAULT(0);
771 if (idx != 0x7f && dcb->entry[idx].i2c_upper_default)
772 i2c_index = NV_I2C_DEFAULT(1);
775 return nouveau_i2c_find(dev, i2c_index);
779 get_tmds_index_reg(struct drm_device *dev, uint8_t mlv)
782 * For mlv < 0x80, it is an index into a table of TMDS base addresses.
783 * For mlv == 0x80 use the "or" value of the dcb_entry indexed by
784 * CR58 for CR57 = 0 to index a table of offsets to the basic
786 * For mlv == 0x81 use the "or" value of the dcb_entry indexed by
787 * CR58 for CR57 = 0 to index a table of offsets to the basic
788 * 0x6808b0 address, and then flip the offset by 8.
791 struct drm_nouveau_private *dev_priv = dev->dev_private;
792 struct nvbios *bios = &dev_priv->vbios;
793 const int pramdac_offset[13] = {
794 0, 0, 0x8, 0, 0x2000, 0, 0, 0, 0x2008, 0, 0, 0, 0x2000 };
795 const uint32_t pramdac_table[4] = {
796 0x6808b0, 0x6808b8, 0x6828b0, 0x6828b8 };
799 int dcb_entry, dacoffset;
801 /* note: dcb_entry_idx_from_crtchead needs pre-script set-up */
802 dcb_entry = dcb_entry_idx_from_crtchead(dev);
803 if (dcb_entry == 0x7f)
805 dacoffset = pramdac_offset[bios->dcb.entry[dcb_entry].or];
808 return 0x6808b0 + dacoffset;
810 if (mlv >= ARRAY_SIZE(pramdac_table)) {
811 NV_ERROR(dev, "Magic Lookup Value too big (%02X)\n",
815 return pramdac_table[mlv];
820 init_io_restrict_prog(struct nvbios *bios, uint16_t offset,
821 struct init_exec *iexec)
824 * INIT_IO_RESTRICT_PROG opcode: 0x32 ('2')
826 * offset (8 bit): opcode
827 * offset + 1 (16 bit): CRTC port
828 * offset + 3 (8 bit): CRTC index
829 * offset + 4 (8 bit): mask
830 * offset + 5 (8 bit): shift
831 * offset + 6 (8 bit): count
832 * offset + 7 (32 bit): register
833 * offset + 11 (32 bit): configuration 1
836 * Starting at offset + 11 there are "count" 32 bit values.
837 * To find out which value to use read index "CRTC index" on "CRTC
838 * port", AND this value with "mask" and then bit shift right "shift"
839 * bits. Read the appropriate value using this index and write to
843 uint16_t crtcport = ROM16(bios->data[offset + 1]);
844 uint8_t crtcindex = bios->data[offset + 3];
845 uint8_t mask = bios->data[offset + 4];
846 uint8_t shift = bios->data[offset + 5];
847 uint8_t count = bios->data[offset + 6];
848 uint32_t reg = ROM32(bios->data[offset + 7]);
851 int len = 11 + count * 4;
856 BIOSLOG(bios, "0x%04X: Port: 0x%04X, Index: 0x%02X, Mask: 0x%02X, "
857 "Shift: 0x%02X, Count: 0x%02X, Reg: 0x%08X\n",
858 offset, crtcport, crtcindex, mask, shift, count, reg);
860 config = (bios_idxprt_rd(bios, crtcport, crtcindex) & mask) >> shift;
861 if (config > count) {
863 "0x%04X: Config 0x%02X exceeds maximal bound 0x%02X\n",
864 offset, config, count);
868 configval = ROM32(bios->data[offset + 11 + config * 4]);
870 BIOSLOG(bios, "0x%04X: Writing config %02X\n", offset, config);
872 bios_wr32(bios, reg, configval);
878 init_repeat(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
881 * INIT_REPEAT opcode: 0x33 ('3')
883 * offset (8 bit): opcode
884 * offset + 1 (8 bit): count
886 * Execute script following this opcode up to INIT_REPEAT_END
890 uint8_t count = bios->data[offset + 1];
893 /* no iexec->execute check by design */
895 BIOSLOG(bios, "0x%04X: Repeating following segment %d times\n",
898 iexec->repeat = true;
901 * count - 1, as the script block will execute once when we leave this
902 * opcode -- this is compatible with bios behaviour as:
903 * a) the block is always executed at least once, even if count == 0
904 * b) the bios interpreter skips to the op following INIT_END_REPEAT,
907 for (i = 0; i < count - 1; i++)
908 parse_init_table(bios, offset + 2, iexec);
910 iexec->repeat = false;
916 init_io_restrict_pll(struct nvbios *bios, uint16_t offset,
917 struct init_exec *iexec)
920 * INIT_IO_RESTRICT_PLL opcode: 0x34 ('4')
922 * offset (8 bit): opcode
923 * offset + 1 (16 bit): CRTC port
924 * offset + 3 (8 bit): CRTC index
925 * offset + 4 (8 bit): mask
926 * offset + 5 (8 bit): shift
927 * offset + 6 (8 bit): IO flag condition index
928 * offset + 7 (8 bit): count
929 * offset + 8 (32 bit): register
930 * offset + 12 (16 bit): frequency 1
933 * Starting at offset + 12 there are "count" 16 bit frequencies (10kHz).
934 * Set PLL register "register" to coefficients for frequency n,
935 * selected by reading index "CRTC index" of "CRTC port" ANDed with
936 * "mask" and shifted right by "shift".
938 * If "IO flag condition index" > 0, and condition met, double
939 * frequency before setting it.
942 uint16_t crtcport = ROM16(bios->data[offset + 1]);
943 uint8_t crtcindex = bios->data[offset + 3];
944 uint8_t mask = bios->data[offset + 4];
945 uint8_t shift = bios->data[offset + 5];
946 int8_t io_flag_condition_idx = bios->data[offset + 6];
947 uint8_t count = bios->data[offset + 7];
948 uint32_t reg = ROM32(bios->data[offset + 8]);
951 int len = 12 + count * 2;
956 BIOSLOG(bios, "0x%04X: Port: 0x%04X, Index: 0x%02X, Mask: 0x%02X, "
957 "Shift: 0x%02X, IO Flag Condition: 0x%02X, "
958 "Count: 0x%02X, Reg: 0x%08X\n",
959 offset, crtcport, crtcindex, mask, shift,
960 io_flag_condition_idx, count, reg);
962 config = (bios_idxprt_rd(bios, crtcport, crtcindex) & mask) >> shift;
963 if (config > count) {
965 "0x%04X: Config 0x%02X exceeds maximal bound 0x%02X\n",
966 offset, config, count);
970 freq = ROM16(bios->data[offset + 12 + config * 2]);
972 if (io_flag_condition_idx > 0) {
973 if (io_flag_condition_met(bios, offset, io_flag_condition_idx)) {
974 BIOSLOG(bios, "0x%04X: Condition fulfilled -- "
975 "frequency doubled\n", offset);
978 BIOSLOG(bios, "0x%04X: Condition not fulfilled -- "
979 "frequency unchanged\n", offset);
982 BIOSLOG(bios, "0x%04X: Reg: 0x%08X, Config: 0x%02X, Freq: %d0kHz\n",
983 offset, reg, config, freq);
985 setPLL(bios, reg, freq * 10);
991 init_end_repeat(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
994 * INIT_END_REPEAT opcode: 0x36 ('6')
996 * offset (8 bit): opcode
998 * Marks the end of the block for INIT_REPEAT to repeat
1001 /* no iexec->execute check by design */
1004 * iexec->repeat flag necessary to go past INIT_END_REPEAT opcode when
1005 * we're not in repeat mode
1014 init_copy(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
1017 * INIT_COPY opcode: 0x37 ('7')
1019 * offset (8 bit): opcode
1020 * offset + 1 (32 bit): register
1021 * offset + 5 (8 bit): shift
1022 * offset + 6 (8 bit): srcmask
1023 * offset + 7 (16 bit): CRTC port
1024 * offset + 9 (8 bit): CRTC index
1025 * offset + 10 (8 bit): mask
1027 * Read index "CRTC index" on "CRTC port", AND with "mask", OR with
1028 * (REGVAL("register") >> "shift" & "srcmask") and write-back to CRTC
1032 uint32_t reg = ROM32(bios->data[offset + 1]);
1033 uint8_t shift = bios->data[offset + 5];
1034 uint8_t srcmask = bios->data[offset + 6];
1035 uint16_t crtcport = ROM16(bios->data[offset + 7]);
1036 uint8_t crtcindex = bios->data[offset + 9];
1037 uint8_t mask = bios->data[offset + 10];
1041 if (!iexec->execute)
1044 BIOSLOG(bios, "0x%04X: Reg: 0x%08X, Shift: 0x%02X, SrcMask: 0x%02X, "
1045 "Port: 0x%04X, Index: 0x%02X, Mask: 0x%02X\n",
1046 offset, reg, shift, srcmask, crtcport, crtcindex, mask);
1048 data = bios_rd32(bios, reg);
1053 data <<= (0x100 - shift);
1057 crtcdata = bios_idxprt_rd(bios, crtcport, crtcindex) & mask;
1058 crtcdata |= (uint8_t)data;
1059 bios_idxprt_wr(bios, crtcport, crtcindex, crtcdata);
1065 init_not(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
1068 * INIT_NOT opcode: 0x38 ('8')
1070 * offset (8 bit): opcode
1072 * Invert the current execute / no-execute condition (i.e. "else")
1075 BIOSLOG(bios, "0x%04X: ------ Skipping following commands ------\n", offset);
1077 BIOSLOG(bios, "0x%04X: ------ Executing following commands ------\n", offset);
1079 iexec->execute = !iexec->execute;
1084 init_io_flag_condition(struct nvbios *bios, uint16_t offset,
1085 struct init_exec *iexec)
1088 * INIT_IO_FLAG_CONDITION opcode: 0x39 ('9')
1090 * offset (8 bit): opcode
1091 * offset + 1 (8 bit): condition number
1093 * Check condition "condition number" in the IO flag condition table.
1094 * If condition not met skip subsequent opcodes until condition is
1095 * inverted (INIT_NOT), or we hit INIT_RESUME
1098 uint8_t cond = bios->data[offset + 1];
1100 if (!iexec->execute)
1103 if (io_flag_condition_met(bios, offset, cond))
1104 BIOSLOG(bios, "0x%04X: Condition fulfilled -- continuing to execute\n", offset);
1106 BIOSLOG(bios, "0x%04X: Condition not fulfilled -- skipping following commands\n", offset);
1107 iexec->execute = false;
1114 init_dp_condition(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
1117 * INIT_DP_CONDITION opcode: 0x3A ('')
1119 * offset (8 bit): opcode
1120 * offset + 1 (8 bit): "sub" opcode
1121 * offset + 2 (8 bit): unknown
1125 struct dcb_entry *dcb = bios->display.output;
1126 struct drm_device *dev = bios->dev;
1127 uint8_t cond = bios->data[offset + 1];
1128 uint8_t *table, *entry;
1130 BIOSLOG(bios, "0x%04X: subop 0x%02X\n", offset, cond);
1132 if (!iexec->execute)
1135 table = nouveau_dp_bios_data(dev, dcb, &entry);
1141 entry = dcb_conn(dev, dcb->connector);
1142 if (!entry || entry[0] != DCB_CONNECTOR_eDP)
1143 iexec->execute = false;
1147 if (!(entry[5] & cond))
1148 iexec->execute = false;
1152 struct nouveau_i2c_chan *auxch;
1155 auxch = nouveau_i2c_find(dev, bios->display.output->i2c_index);
1157 NV_ERROR(dev, "0x%04X: couldn't get auxch\n", offset);
1161 ret = nouveau_dp_auxch(auxch, 9, 0xd, &cond, 1);
1163 NV_ERROR(dev, "0x%04X: auxch rd fail: %d\n", offset, ret);
1168 iexec->execute = false;
1172 NV_WARN(dev, "0x%04X: unknown INIT_3A op: %d\n", offset, cond);
1177 BIOSLOG(bios, "0x%04X: continuing to execute\n", offset);
1179 BIOSLOG(bios, "0x%04X: skipping following commands\n", offset);
1185 init_op_3b(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
1188 * INIT_3B opcode: 0x3B ('')
1190 * offset (8 bit): opcode
1191 * offset + 1 (8 bit): crtc index
1195 uint8_t or = ffs(bios->display.output->or) - 1;
1196 uint8_t index = bios->data[offset + 1];
1199 if (!iexec->execute)
1202 data = bios_idxprt_rd(bios, 0x3d4, index);
1203 bios_idxprt_wr(bios, 0x3d4, index, data & ~(1 << or));
1208 init_op_3c(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
1211 * INIT_3C opcode: 0x3C ('')
1213 * offset (8 bit): opcode
1214 * offset + 1 (8 bit): crtc index
1218 uint8_t or = ffs(bios->display.output->or) - 1;
1219 uint8_t index = bios->data[offset + 1];
1222 if (!iexec->execute)
1225 data = bios_idxprt_rd(bios, 0x3d4, index);
1226 bios_idxprt_wr(bios, 0x3d4, index, data | (1 << or));
1231 init_idx_addr_latched(struct nvbios *bios, uint16_t offset,
1232 struct init_exec *iexec)
1235 * INIT_INDEX_ADDRESS_LATCHED opcode: 0x49 ('I')
1237 * offset (8 bit): opcode
1238 * offset + 1 (32 bit): control register
1239 * offset + 5 (32 bit): data register
1240 * offset + 9 (32 bit): mask
1241 * offset + 13 (32 bit): data
1242 * offset + 17 (8 bit): count
1243 * offset + 18 (8 bit): address 1
1244 * offset + 19 (8 bit): data 1
1247 * For each of "count" address and data pairs, write "data n" to
1248 * "data register", read the current value of "control register",
1249 * and write it back once ANDed with "mask", ORed with "data",
1250 * and ORed with "address n"
1253 uint32_t controlreg = ROM32(bios->data[offset + 1]);
1254 uint32_t datareg = ROM32(bios->data[offset + 5]);
1255 uint32_t mask = ROM32(bios->data[offset + 9]);
1256 uint32_t data = ROM32(bios->data[offset + 13]);
1257 uint8_t count = bios->data[offset + 17];
1258 int len = 18 + count * 2;
1262 if (!iexec->execute)
1265 BIOSLOG(bios, "0x%04X: ControlReg: 0x%08X, DataReg: 0x%08X, "
1266 "Mask: 0x%08X, Data: 0x%08X, Count: 0x%02X\n",
1267 offset, controlreg, datareg, mask, data, count);
1269 for (i = 0; i < count; i++) {
1270 uint8_t instaddress = bios->data[offset + 18 + i * 2];
1271 uint8_t instdata = bios->data[offset + 19 + i * 2];
1273 BIOSLOG(bios, "0x%04X: Address: 0x%02X, Data: 0x%02X\n",
1274 offset, instaddress, instdata);
1276 bios_wr32(bios, datareg, instdata);
1277 value = bios_rd32(bios, controlreg) & mask;
1279 value |= instaddress;
1280 bios_wr32(bios, controlreg, value);
1287 init_io_restrict_pll2(struct nvbios *bios, uint16_t offset,
1288 struct init_exec *iexec)
1291 * INIT_IO_RESTRICT_PLL2 opcode: 0x4A ('J')
1293 * offset (8 bit): opcode
1294 * offset + 1 (16 bit): CRTC port
1295 * offset + 3 (8 bit): CRTC index
1296 * offset + 4 (8 bit): mask
1297 * offset + 5 (8 bit): shift
1298 * offset + 6 (8 bit): count
1299 * offset + 7 (32 bit): register
1300 * offset + 11 (32 bit): frequency 1
1303 * Starting at offset + 11 there are "count" 32 bit frequencies (kHz).
1304 * Set PLL register "register" to coefficients for frequency n,
1305 * selected by reading index "CRTC index" of "CRTC port" ANDed with
1306 * "mask" and shifted right by "shift".
1309 uint16_t crtcport = ROM16(bios->data[offset + 1]);
1310 uint8_t crtcindex = bios->data[offset + 3];
1311 uint8_t mask = bios->data[offset + 4];
1312 uint8_t shift = bios->data[offset + 5];
1313 uint8_t count = bios->data[offset + 6];
1314 uint32_t reg = ROM32(bios->data[offset + 7]);
1315 int len = 11 + count * 4;
1319 if (!iexec->execute)
1322 BIOSLOG(bios, "0x%04X: Port: 0x%04X, Index: 0x%02X, Mask: 0x%02X, "
1323 "Shift: 0x%02X, Count: 0x%02X, Reg: 0x%08X\n",
1324 offset, crtcport, crtcindex, mask, shift, count, reg);
1329 config = (bios_idxprt_rd(bios, crtcport, crtcindex) & mask) >> shift;
1330 if (config > count) {
1332 "0x%04X: Config 0x%02X exceeds maximal bound 0x%02X\n",
1333 offset, config, count);
1337 freq = ROM32(bios->data[offset + 11 + config * 4]);
1339 BIOSLOG(bios, "0x%04X: Reg: 0x%08X, Config: 0x%02X, Freq: %dkHz\n",
1340 offset, reg, config, freq);
1342 setPLL(bios, reg, freq);
1348 init_pll2(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
1351 * INIT_PLL2 opcode: 0x4B ('K')
1353 * offset (8 bit): opcode
1354 * offset + 1 (32 bit): register
1355 * offset + 5 (32 bit): freq
1357 * Set PLL register "register" to coefficients for frequency "freq"
1360 uint32_t reg = ROM32(bios->data[offset + 1]);
1361 uint32_t freq = ROM32(bios->data[offset + 5]);
1363 if (!iexec->execute)
1366 BIOSLOG(bios, "0x%04X: Reg: 0x%04X, Freq: %dkHz\n",
1369 setPLL(bios, reg, freq);
1374 init_i2c_byte(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
1377 * INIT_I2C_BYTE opcode: 0x4C ('L')
1379 * offset (8 bit): opcode
1380 * offset + 1 (8 bit): DCB I2C table entry index
1381 * offset + 2 (8 bit): I2C slave address
1382 * offset + 3 (8 bit): count
1383 * offset + 4 (8 bit): I2C register 1
1384 * offset + 5 (8 bit): mask 1
1385 * offset + 6 (8 bit): data 1
1388 * For each of "count" registers given by "I2C register n" on the device
1389 * addressed by "I2C slave address" on the I2C bus given by
1390 * "DCB I2C table entry index", read the register, AND the result with
1391 * "mask n" and OR it with "data n" before writing it back to the device
1394 struct drm_device *dev = bios->dev;
1395 uint8_t i2c_index = bios->data[offset + 1];
1396 uint8_t i2c_address = bios->data[offset + 2] >> 1;
1397 uint8_t count = bios->data[offset + 3];
1398 struct nouveau_i2c_chan *chan;
1399 int len = 4 + count * 3;
1402 if (!iexec->execute)
1405 BIOSLOG(bios, "0x%04X: DCBI2CIndex: 0x%02X, I2CAddress: 0x%02X, "
1407 offset, i2c_index, i2c_address, count);
1409 chan = init_i2c_device_find(dev, i2c_index);
1411 NV_ERROR(dev, "0x%04X: i2c bus not found\n", offset);
1415 for (i = 0; i < count; i++) {
1416 uint8_t reg = bios->data[offset + 4 + i * 3];
1417 uint8_t mask = bios->data[offset + 5 + i * 3];
1418 uint8_t data = bios->data[offset + 6 + i * 3];
1419 union i2c_smbus_data val;
1421 ret = i2c_smbus_xfer(&chan->adapter, i2c_address, 0,
1422 I2C_SMBUS_READ, reg,
1423 I2C_SMBUS_BYTE_DATA, &val);
1425 NV_ERROR(dev, "0x%04X: i2c rd fail: %d\n", offset, ret);
1429 BIOSLOG(bios, "0x%04X: I2CReg: 0x%02X, Value: 0x%02X, "
1430 "Mask: 0x%02X, Data: 0x%02X\n",
1431 offset, reg, val.byte, mask, data);
1438 ret = i2c_smbus_xfer(&chan->adapter, i2c_address, 0,
1439 I2C_SMBUS_WRITE, reg,
1440 I2C_SMBUS_BYTE_DATA, &val);
1442 NV_ERROR(dev, "0x%04X: i2c wr fail: %d\n", offset, ret);
1451 init_zm_i2c_byte(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
1454 * INIT_ZM_I2C_BYTE opcode: 0x4D ('M')
1456 * offset (8 bit): opcode
1457 * offset + 1 (8 bit): DCB I2C table entry index
1458 * offset + 2 (8 bit): I2C slave address
1459 * offset + 3 (8 bit): count
1460 * offset + 4 (8 bit): I2C register 1
1461 * offset + 5 (8 bit): data 1
1464 * For each of "count" registers given by "I2C register n" on the device
1465 * addressed by "I2C slave address" on the I2C bus given by
1466 * "DCB I2C table entry index", set the register to "data n"
1469 struct drm_device *dev = bios->dev;
1470 uint8_t i2c_index = bios->data[offset + 1];
1471 uint8_t i2c_address = bios->data[offset + 2] >> 1;
1472 uint8_t count = bios->data[offset + 3];
1473 struct nouveau_i2c_chan *chan;
1474 int len = 4 + count * 2;
1477 if (!iexec->execute)
1480 BIOSLOG(bios, "0x%04X: DCBI2CIndex: 0x%02X, I2CAddress: 0x%02X, "
1482 offset, i2c_index, i2c_address, count);
1484 chan = init_i2c_device_find(dev, i2c_index);
1486 NV_ERROR(dev, "0x%04X: i2c bus not found\n", offset);
1490 for (i = 0; i < count; i++) {
1491 uint8_t reg = bios->data[offset + 4 + i * 2];
1492 union i2c_smbus_data val;
1494 val.byte = bios->data[offset + 5 + i * 2];
1496 BIOSLOG(bios, "0x%04X: I2CReg: 0x%02X, Data: 0x%02X\n",
1497 offset, reg, val.byte);
1502 ret = i2c_smbus_xfer(&chan->adapter, i2c_address, 0,
1503 I2C_SMBUS_WRITE, reg,
1504 I2C_SMBUS_BYTE_DATA, &val);
1506 NV_ERROR(dev, "0x%04X: i2c wr fail: %d\n", offset, ret);
1515 init_zm_i2c(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
1518 * INIT_ZM_I2C opcode: 0x4E ('N')
1520 * offset (8 bit): opcode
1521 * offset + 1 (8 bit): DCB I2C table entry index
1522 * offset + 2 (8 bit): I2C slave address
1523 * offset + 3 (8 bit): count
1524 * offset + 4 (8 bit): data 1
1527 * Send "count" bytes ("data n") to the device addressed by "I2C slave
1528 * address" on the I2C bus given by "DCB I2C table entry index"
1531 struct drm_device *dev = bios->dev;
1532 uint8_t i2c_index = bios->data[offset + 1];
1533 uint8_t i2c_address = bios->data[offset + 2] >> 1;
1534 uint8_t count = bios->data[offset + 3];
1535 int len = 4 + count;
1536 struct nouveau_i2c_chan *chan;
1541 if (!iexec->execute)
1544 BIOSLOG(bios, "0x%04X: DCBI2CIndex: 0x%02X, I2CAddress: 0x%02X, "
1546 offset, i2c_index, i2c_address, count);
1548 chan = init_i2c_device_find(dev, i2c_index);
1550 NV_ERROR(dev, "0x%04X: i2c bus not found\n", offset);
1554 for (i = 0; i < count; i++) {
1555 data[i] = bios->data[offset + 4 + i];
1557 BIOSLOG(bios, "0x%04X: Data: 0x%02X\n", offset, data[i]);
1560 if (bios->execute) {
1561 msg.addr = i2c_address;
1565 ret = i2c_transfer(&chan->adapter, &msg, 1);
1567 NV_ERROR(dev, "0x%04X: i2c wr fail: %d\n", offset, ret);
1576 init_tmds(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
1579 * INIT_TMDS opcode: 0x4F ('O') (non-canon name)
1581 * offset (8 bit): opcode
1582 * offset + 1 (8 bit): magic lookup value
1583 * offset + 2 (8 bit): TMDS address
1584 * offset + 3 (8 bit): mask
1585 * offset + 4 (8 bit): data
1587 * Read the data reg for TMDS address "TMDS address", AND it with mask
1588 * and OR it with data, then write it back
1589 * "magic lookup value" determines which TMDS base address register is
1590 * used -- see get_tmds_index_reg()
1593 struct drm_device *dev = bios->dev;
1594 uint8_t mlv = bios->data[offset + 1];
1595 uint32_t tmdsaddr = bios->data[offset + 2];
1596 uint8_t mask = bios->data[offset + 3];
1597 uint8_t data = bios->data[offset + 4];
1598 uint32_t reg, value;
1600 if (!iexec->execute)
1603 BIOSLOG(bios, "0x%04X: MagicLookupValue: 0x%02X, TMDSAddr: 0x%02X, "
1604 "Mask: 0x%02X, Data: 0x%02X\n",
1605 offset, mlv, tmdsaddr, mask, data);
1607 reg = get_tmds_index_reg(bios->dev, mlv);
1609 NV_ERROR(dev, "0x%04X: no tmds_index_reg\n", offset);
1613 bios_wr32(bios, reg,
1614 tmdsaddr | NV_PRAMDAC_FP_TMDS_CONTROL_WRITE_DISABLE);
1615 value = (bios_rd32(bios, reg + 4) & mask) | data;
1616 bios_wr32(bios, reg + 4, value);
1617 bios_wr32(bios, reg, tmdsaddr);
1623 init_zm_tmds_group(struct nvbios *bios, uint16_t offset,
1624 struct init_exec *iexec)
1627 * INIT_ZM_TMDS_GROUP opcode: 0x50 ('P') (non-canon name)
1629 * offset (8 bit): opcode
1630 * offset + 1 (8 bit): magic lookup value
1631 * offset + 2 (8 bit): count
1632 * offset + 3 (8 bit): addr 1
1633 * offset + 4 (8 bit): data 1
1636 * For each of "count" TMDS address and data pairs write "data n" to
1637 * "addr n". "magic lookup value" determines which TMDS base address
1638 * register is used -- see get_tmds_index_reg()
1641 struct drm_device *dev = bios->dev;
1642 uint8_t mlv = bios->data[offset + 1];
1643 uint8_t count = bios->data[offset + 2];
1644 int len = 3 + count * 2;
1648 if (!iexec->execute)
1651 BIOSLOG(bios, "0x%04X: MagicLookupValue: 0x%02X, Count: 0x%02X\n",
1652 offset, mlv, count);
1654 reg = get_tmds_index_reg(bios->dev, mlv);
1656 NV_ERROR(dev, "0x%04X: no tmds_index_reg\n", offset);
1660 for (i = 0; i < count; i++) {
1661 uint8_t tmdsaddr = bios->data[offset + 3 + i * 2];
1662 uint8_t tmdsdata = bios->data[offset + 4 + i * 2];
1664 bios_wr32(bios, reg + 4, tmdsdata);
1665 bios_wr32(bios, reg, tmdsaddr);
1672 init_cr_idx_adr_latch(struct nvbios *bios, uint16_t offset,
1673 struct init_exec *iexec)
1676 * INIT_CR_INDEX_ADDRESS_LATCHED opcode: 0x51 ('Q')
1678 * offset (8 bit): opcode
1679 * offset + 1 (8 bit): CRTC index1
1680 * offset + 2 (8 bit): CRTC index2
1681 * offset + 3 (8 bit): baseaddr
1682 * offset + 4 (8 bit): count
1683 * offset + 5 (8 bit): data 1
1686 * For each of "count" address and data pairs, write "baseaddr + n" to
1687 * "CRTC index1" and "data n" to "CRTC index2"
1688 * Once complete, restore initial value read from "CRTC index1"
1690 uint8_t crtcindex1 = bios->data[offset + 1];
1691 uint8_t crtcindex2 = bios->data[offset + 2];
1692 uint8_t baseaddr = bios->data[offset + 3];
1693 uint8_t count = bios->data[offset + 4];
1694 int len = 5 + count;
1695 uint8_t oldaddr, data;
1698 if (!iexec->execute)
1701 BIOSLOG(bios, "0x%04X: Index1: 0x%02X, Index2: 0x%02X, "
1702 "BaseAddr: 0x%02X, Count: 0x%02X\n",
1703 offset, crtcindex1, crtcindex2, baseaddr, count);
1705 oldaddr = bios_idxprt_rd(bios, NV_CIO_CRX__COLOR, crtcindex1);
1707 for (i = 0; i < count; i++) {
1708 bios_idxprt_wr(bios, NV_CIO_CRX__COLOR, crtcindex1,
1710 data = bios->data[offset + 5 + i];
1711 bios_idxprt_wr(bios, NV_CIO_CRX__COLOR, crtcindex2, data);
1714 bios_idxprt_wr(bios, NV_CIO_CRX__COLOR, crtcindex1, oldaddr);
1720 init_cr(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
1723 * INIT_CR opcode: 0x52 ('R')
1725 * offset (8 bit): opcode
1726 * offset + 1 (8 bit): CRTC index
1727 * offset + 2 (8 bit): mask
1728 * offset + 3 (8 bit): data
1730 * Assign the value of at "CRTC index" ANDed with mask and ORed with
1731 * data back to "CRTC index"
1734 uint8_t crtcindex = bios->data[offset + 1];
1735 uint8_t mask = bios->data[offset + 2];
1736 uint8_t data = bios->data[offset + 3];
1739 if (!iexec->execute)
1742 BIOSLOG(bios, "0x%04X: Index: 0x%02X, Mask: 0x%02X, Data: 0x%02X\n",
1743 offset, crtcindex, mask, data);
1745 value = bios_idxprt_rd(bios, NV_CIO_CRX__COLOR, crtcindex) & mask;
1747 bios_idxprt_wr(bios, NV_CIO_CRX__COLOR, crtcindex, value);
1753 init_zm_cr(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
1756 * INIT_ZM_CR opcode: 0x53 ('S')
1758 * offset (8 bit): opcode
1759 * offset + 1 (8 bit): CRTC index
1760 * offset + 2 (8 bit): value
1762 * Assign "value" to CRTC register with index "CRTC index".
1765 uint8_t crtcindex = ROM32(bios->data[offset + 1]);
1766 uint8_t data = bios->data[offset + 2];
1768 if (!iexec->execute)
1771 bios_idxprt_wr(bios, NV_CIO_CRX__COLOR, crtcindex, data);
1777 init_zm_cr_group(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
1780 * INIT_ZM_CR_GROUP opcode: 0x54 ('T')
1782 * offset (8 bit): opcode
1783 * offset + 1 (8 bit): count
1784 * offset + 2 (8 bit): CRTC index 1
1785 * offset + 3 (8 bit): value 1
1788 * For "count", assign "value n" to CRTC register with index
1792 uint8_t count = bios->data[offset + 1];
1793 int len = 2 + count * 2;
1796 if (!iexec->execute)
1799 for (i = 0; i < count; i++)
1800 init_zm_cr(bios, offset + 2 + 2 * i - 1, iexec);
1806 init_condition_time(struct nvbios *bios, uint16_t offset,
1807 struct init_exec *iexec)
1810 * INIT_CONDITION_TIME opcode: 0x56 ('V')
1812 * offset (8 bit): opcode
1813 * offset + 1 (8 bit): condition number
1814 * offset + 2 (8 bit): retries / 50
1816 * Check condition "condition number" in the condition table.
1817 * Bios code then sleeps for 2ms if the condition is not met, and
1818 * repeats up to "retries" times, but on one C51 this has proved
1819 * insufficient. In mmiotraces the driver sleeps for 20ms, so we do
1820 * this, and bail after "retries" times, or 2s, whichever is less.
1821 * If still not met after retries, clear execution flag for this table.
1824 uint8_t cond = bios->data[offset + 1];
1825 uint16_t retries = bios->data[offset + 2] * 50;
1828 if (!iexec->execute)
1834 BIOSLOG(bios, "0x%04X: Condition: 0x%02X, Retries: 0x%02X\n",
1835 offset, cond, retries);
1837 if (!bios->execute) /* avoid 2s delays when "faking" execution */
1840 for (cnt = 0; cnt < retries; cnt++) {
1841 if (bios_condition_met(bios, offset, cond)) {
1842 BIOSLOG(bios, "0x%04X: Condition met, continuing\n",
1846 BIOSLOG(bios, "0x%04X: "
1847 "Condition not met, sleeping for 20ms\n",
1853 if (!bios_condition_met(bios, offset, cond)) {
1855 "0x%04X: Condition still not met after %dms, "
1856 "skipping following opcodes\n", offset, 20 * retries);
1857 iexec->execute = false;
1864 init_ltime(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
1867 * INIT_LTIME opcode: 0x57 ('V')
1869 * offset (8 bit): opcode
1870 * offset + 1 (16 bit): time
1872 * Sleep for "time" milliseconds.
1875 unsigned time = ROM16(bios->data[offset + 1]);
1877 if (!iexec->execute)
1880 BIOSLOG(bios, "0x%04X: Sleeping for 0x%04X milliseconds\n",
1889 init_zm_reg_sequence(struct nvbios *bios, uint16_t offset,
1890 struct init_exec *iexec)
1893 * INIT_ZM_REG_SEQUENCE opcode: 0x58 ('X')
1895 * offset (8 bit): opcode
1896 * offset + 1 (32 bit): base register
1897 * offset + 5 (8 bit): count
1898 * offset + 6 (32 bit): value 1
1901 * Starting at offset + 6 there are "count" 32 bit values.
1902 * For "count" iterations set "base register" + 4 * current_iteration
1903 * to "value current_iteration"
1906 uint32_t basereg = ROM32(bios->data[offset + 1]);
1907 uint32_t count = bios->data[offset + 5];
1908 int len = 6 + count * 4;
1911 if (!iexec->execute)
1914 BIOSLOG(bios, "0x%04X: BaseReg: 0x%08X, Count: 0x%02X\n",
1915 offset, basereg, count);
1917 for (i = 0; i < count; i++) {
1918 uint32_t reg = basereg + i * 4;
1919 uint32_t data = ROM32(bios->data[offset + 6 + i * 4]);
1921 bios_wr32(bios, reg, data);
1928 init_sub_direct(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
1931 * INIT_SUB_DIRECT opcode: 0x5B ('[')
1933 * offset (8 bit): opcode
1934 * offset + 1 (16 bit): subroutine offset (in bios)
1936 * Calls a subroutine that will execute commands until INIT_DONE
1940 uint16_t sub_offset = ROM16(bios->data[offset + 1]);
1942 if (!iexec->execute)
1945 BIOSLOG(bios, "0x%04X: Executing subroutine at 0x%04X\n",
1946 offset, sub_offset);
1948 parse_init_table(bios, sub_offset, iexec);
1950 BIOSLOG(bios, "0x%04X: End of 0x%04X subroutine\n", offset, sub_offset);
1956 init_jump(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
1959 * INIT_JUMP opcode: 0x5C ('\')
1961 * offset (8 bit): opcode
1962 * offset + 1 (16 bit): offset (in bios)
1964 * Continue execution of init table from 'offset'
1967 uint16_t jmp_offset = ROM16(bios->data[offset + 1]);
1969 if (!iexec->execute)
1972 BIOSLOG(bios, "0x%04X: Jump to 0x%04X\n", offset, jmp_offset);
1973 return jmp_offset - offset;
1977 init_i2c_if(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
1980 * INIT_I2C_IF opcode: 0x5E ('^')
1982 * offset (8 bit): opcode
1983 * offset + 1 (8 bit): DCB I2C table entry index
1984 * offset + 2 (8 bit): I2C slave address
1985 * offset + 3 (8 bit): I2C register
1986 * offset + 4 (8 bit): mask
1987 * offset + 5 (8 bit): data
1989 * Read the register given by "I2C register" on the device addressed
1990 * by "I2C slave address" on the I2C bus given by "DCB I2C table
1991 * entry index". Compare the result AND "mask" to "data".
1992 * If they're not equal, skip subsequent opcodes until condition is
1993 * inverted (INIT_NOT), or we hit INIT_RESUME
1996 uint8_t i2c_index = bios->data[offset + 1];
1997 uint8_t i2c_address = bios->data[offset + 2] >> 1;
1998 uint8_t reg = bios->data[offset + 3];
1999 uint8_t mask = bios->data[offset + 4];
2000 uint8_t data = bios->data[offset + 5];
2001 struct nouveau_i2c_chan *chan;
2002 union i2c_smbus_data val;
2005 /* no execute check by design */
2007 BIOSLOG(bios, "0x%04X: DCBI2CIndex: 0x%02X, I2CAddress: 0x%02X\n",
2008 offset, i2c_index, i2c_address);
2010 chan = init_i2c_device_find(bios->dev, i2c_index);
2014 ret = i2c_smbus_xfer(&chan->adapter, i2c_address, 0,
2015 I2C_SMBUS_READ, reg,
2016 I2C_SMBUS_BYTE_DATA, &val);
2018 BIOSLOG(bios, "0x%04X: I2CReg: 0x%02X, Value: [no device], "
2019 "Mask: 0x%02X, Data: 0x%02X\n",
2020 offset, reg, mask, data);
2025 BIOSLOG(bios, "0x%04X: I2CReg: 0x%02X, Value: 0x%02X, "
2026 "Mask: 0x%02X, Data: 0x%02X\n",
2027 offset, reg, val.byte, mask, data);
2029 iexec->execute = ((val.byte & mask) == data);
2035 init_copy_nv_reg(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
2038 * INIT_COPY_NV_REG opcode: 0x5F ('_')
2040 * offset (8 bit): opcode
2041 * offset + 1 (32 bit): src reg
2042 * offset + 5 (8 bit): shift
2043 * offset + 6 (32 bit): src mask
2044 * offset + 10 (32 bit): xor
2045 * offset + 14 (32 bit): dst reg
2046 * offset + 18 (32 bit): dst mask
2048 * Shift REGVAL("src reg") right by (signed) "shift", AND result with
2049 * "src mask", then XOR with "xor". Write this OR'd with
2050 * (REGVAL("dst reg") AND'd with "dst mask") to "dst reg"
2053 uint32_t srcreg = *((uint32_t *)(&bios->data[offset + 1]));
2054 uint8_t shift = bios->data[offset + 5];
2055 uint32_t srcmask = *((uint32_t *)(&bios->data[offset + 6]));
2056 uint32_t xor = *((uint32_t *)(&bios->data[offset + 10]));
2057 uint32_t dstreg = *((uint32_t *)(&bios->data[offset + 14]));
2058 uint32_t dstmask = *((uint32_t *)(&bios->data[offset + 18]));
2059 uint32_t srcvalue, dstvalue;
2061 if (!iexec->execute)
2064 BIOSLOG(bios, "0x%04X: SrcReg: 0x%08X, Shift: 0x%02X, SrcMask: 0x%08X, "
2065 "Xor: 0x%08X, DstReg: 0x%08X, DstMask: 0x%08X\n",
2066 offset, srcreg, shift, srcmask, xor, dstreg, dstmask);
2068 srcvalue = bios_rd32(bios, srcreg);
2073 srcvalue <<= (0x100 - shift);
2075 srcvalue = (srcvalue & srcmask) ^ xor;
2077 dstvalue = bios_rd32(bios, dstreg) & dstmask;
2079 bios_wr32(bios, dstreg, dstvalue | srcvalue);
2085 init_zm_index_io(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
2088 * INIT_ZM_INDEX_IO opcode: 0x62 ('b')
2090 * offset (8 bit): opcode
2091 * offset + 1 (16 bit): CRTC port
2092 * offset + 3 (8 bit): CRTC index
2093 * offset + 4 (8 bit): data
2095 * Write "data" to index "CRTC index" of "CRTC port"
2097 uint16_t crtcport = ROM16(bios->data[offset + 1]);
2098 uint8_t crtcindex = bios->data[offset + 3];
2099 uint8_t data = bios->data[offset + 4];
2101 if (!iexec->execute)
2104 bios_idxprt_wr(bios, crtcport, crtcindex, data);
2110 bios_md32(struct nvbios *bios, uint32_t reg,
2111 uint32_t mask, uint32_t val)
2113 bios_wr32(bios, reg, (bios_rd32(bios, reg) & ~mask) | val);
2117 peek_fb(struct drm_device *dev, struct io_mapping *fb,
2122 if (off < pci_resource_len(dev->pdev, 1)) {
2123 uint8_t __iomem *p =
2124 io_mapping_map_atomic_wc(fb, off & PAGE_MASK);
2126 val = ioread32(p + (off & ~PAGE_MASK));
2128 io_mapping_unmap_atomic(p);
2135 poke_fb(struct drm_device *dev, struct io_mapping *fb,
2136 uint32_t off, uint32_t val)
2138 if (off < pci_resource_len(dev->pdev, 1)) {
2139 uint8_t __iomem *p =
2140 io_mapping_map_atomic_wc(fb, off & PAGE_MASK);
2142 iowrite32(val, p + (off & ~PAGE_MASK));
2145 io_mapping_unmap_atomic(p);
2150 read_back_fb(struct drm_device *dev, struct io_mapping *fb,
2151 uint32_t off, uint32_t val)
2153 poke_fb(dev, fb, off, val);
2154 return val == peek_fb(dev, fb, off);
2158 nv04_init_compute_mem(struct nvbios *bios)
2160 struct drm_device *dev = bios->dev;
2161 uint32_t patt = 0xdeadbeef;
2162 struct io_mapping *fb;
2165 /* Map the framebuffer aperture */
2166 fb = io_mapping_create_wc(pci_resource_start(dev->pdev, 1),
2167 pci_resource_len(dev->pdev, 1));
2171 /* Sequencer and refresh off */
2172 NVWriteVgaSeq(dev, 0, 1, NVReadVgaSeq(dev, 0, 1) | 0x20);
2173 bios_md32(bios, NV04_PFB_DEBUG_0, 0, NV04_PFB_DEBUG_0_REFRESH_OFF);
2175 bios_md32(bios, NV04_PFB_BOOT_0, ~0,
2176 NV04_PFB_BOOT_0_RAM_AMOUNT_16MB |
2177 NV04_PFB_BOOT_0_RAM_WIDTH_128 |
2178 NV04_PFB_BOOT_0_RAM_TYPE_SGRAM_16MBIT);
2180 for (i = 0; i < 4; i++)
2181 poke_fb(dev, fb, 4 * i, patt);
2183 poke_fb(dev, fb, 0x400000, patt + 1);
2185 if (peek_fb(dev, fb, 0) == patt + 1) {
2186 bios_md32(bios, NV04_PFB_BOOT_0, NV04_PFB_BOOT_0_RAM_TYPE,
2187 NV04_PFB_BOOT_0_RAM_TYPE_SDRAM_16MBIT);
2188 bios_md32(bios, NV04_PFB_DEBUG_0,
2189 NV04_PFB_DEBUG_0_REFRESH_OFF, 0);
2191 for (i = 0; i < 4; i++)
2192 poke_fb(dev, fb, 4 * i, patt);
2194 if ((peek_fb(dev, fb, 0xc) & 0xffff) != (patt & 0xffff))
2195 bios_md32(bios, NV04_PFB_BOOT_0,
2196 NV04_PFB_BOOT_0_RAM_WIDTH_128 |
2197 NV04_PFB_BOOT_0_RAM_AMOUNT,
2198 NV04_PFB_BOOT_0_RAM_AMOUNT_8MB);
2200 } else if ((peek_fb(dev, fb, 0xc) & 0xffff0000) !=
2201 (patt & 0xffff0000)) {
2202 bios_md32(bios, NV04_PFB_BOOT_0,
2203 NV04_PFB_BOOT_0_RAM_WIDTH_128 |
2204 NV04_PFB_BOOT_0_RAM_AMOUNT,
2205 NV04_PFB_BOOT_0_RAM_AMOUNT_4MB);
2207 } else if (peek_fb(dev, fb, 0) != patt) {
2208 if (read_back_fb(dev, fb, 0x800000, patt))
2209 bios_md32(bios, NV04_PFB_BOOT_0,
2210 NV04_PFB_BOOT_0_RAM_AMOUNT,
2211 NV04_PFB_BOOT_0_RAM_AMOUNT_8MB);
2213 bios_md32(bios, NV04_PFB_BOOT_0,
2214 NV04_PFB_BOOT_0_RAM_AMOUNT,
2215 NV04_PFB_BOOT_0_RAM_AMOUNT_4MB);
2217 bios_md32(bios, NV04_PFB_BOOT_0, NV04_PFB_BOOT_0_RAM_TYPE,
2218 NV04_PFB_BOOT_0_RAM_TYPE_SGRAM_8MBIT);
2220 } else if (!read_back_fb(dev, fb, 0x800000, patt)) {
2221 bios_md32(bios, NV04_PFB_BOOT_0, NV04_PFB_BOOT_0_RAM_AMOUNT,
2222 NV04_PFB_BOOT_0_RAM_AMOUNT_8MB);
2226 /* Refresh on, sequencer on */
2227 bios_md32(bios, NV04_PFB_DEBUG_0, NV04_PFB_DEBUG_0_REFRESH_OFF, 0);
2228 NVWriteVgaSeq(dev, 0, 1, NVReadVgaSeq(dev, 0, 1) & ~0x20);
2230 io_mapping_free(fb);
2234 static const uint8_t *
2235 nv05_memory_config(struct nvbios *bios)
2237 /* Defaults for BIOSes lacking a memory config table */
2238 static const uint8_t default_config_tab[][2] = {
2248 int i = (bios_rd32(bios, NV_PEXTDEV_BOOT_0) &
2249 NV_PEXTDEV_BOOT_0_RAMCFG) >> 2;
2251 if (bios->legacy.mem_init_tbl_ptr)
2252 return &bios->data[bios->legacy.mem_init_tbl_ptr + 2 * i];
2254 return default_config_tab[i];
2258 nv05_init_compute_mem(struct nvbios *bios)
2260 struct drm_device *dev = bios->dev;
2261 const uint8_t *ramcfg = nv05_memory_config(bios);
2262 uint32_t patt = 0xdeadbeef;
2263 struct io_mapping *fb;
2266 /* Map the framebuffer aperture */
2267 fb = io_mapping_create_wc(pci_resource_start(dev->pdev, 1),
2268 pci_resource_len(dev->pdev, 1));
2273 NVWriteVgaSeq(dev, 0, 1, NVReadVgaSeq(dev, 0, 1) | 0x20);
2275 if (bios_rd32(bios, NV04_PFB_BOOT_0) & NV04_PFB_BOOT_0_UMA_ENABLE)
2278 bios_md32(bios, NV04_PFB_DEBUG_0, NV04_PFB_DEBUG_0_REFRESH_OFF, 0);
2280 /* If present load the hardcoded scrambling table */
2281 if (bios->legacy.mem_init_tbl_ptr) {
2282 uint32_t *scramble_tab = (uint32_t *)&bios->data[
2283 bios->legacy.mem_init_tbl_ptr + 0x10];
2285 for (i = 0; i < 8; i++)
2286 bios_wr32(bios, NV04_PFB_SCRAMBLE(i),
2287 ROM32(scramble_tab[i]));
2290 /* Set memory type/width/length defaults depending on the straps */
2291 bios_md32(bios, NV04_PFB_BOOT_0, 0x3f, ramcfg[0]);
2293 if (ramcfg[1] & 0x80)
2294 bios_md32(bios, NV04_PFB_CFG0, 0, NV04_PFB_CFG0_SCRAMBLE);
2296 bios_md32(bios, NV04_PFB_CFG1, 0x700001, (ramcfg[1] & 1) << 20);
2297 bios_md32(bios, NV04_PFB_CFG1, 0, 1);
2299 /* Probe memory bus width */
2300 for (i = 0; i < 4; i++)
2301 poke_fb(dev, fb, 4 * i, patt);
2303 if (peek_fb(dev, fb, 0xc) != patt)
2304 bios_md32(bios, NV04_PFB_BOOT_0,
2305 NV04_PFB_BOOT_0_RAM_WIDTH_128, 0);
2307 /* Probe memory length */
2308 v = bios_rd32(bios, NV04_PFB_BOOT_0) & NV04_PFB_BOOT_0_RAM_AMOUNT;
2310 if (v == NV04_PFB_BOOT_0_RAM_AMOUNT_32MB &&
2311 (!read_back_fb(dev, fb, 0x1000000, ++patt) ||
2312 !read_back_fb(dev, fb, 0, ++patt)))
2313 bios_md32(bios, NV04_PFB_BOOT_0, NV04_PFB_BOOT_0_RAM_AMOUNT,
2314 NV04_PFB_BOOT_0_RAM_AMOUNT_16MB);
2316 if (v == NV04_PFB_BOOT_0_RAM_AMOUNT_16MB &&
2317 !read_back_fb(dev, fb, 0x800000, ++patt))
2318 bios_md32(bios, NV04_PFB_BOOT_0, NV04_PFB_BOOT_0_RAM_AMOUNT,
2319 NV04_PFB_BOOT_0_RAM_AMOUNT_8MB);
2321 if (!read_back_fb(dev, fb, 0x400000, ++patt))
2322 bios_md32(bios, NV04_PFB_BOOT_0, NV04_PFB_BOOT_0_RAM_AMOUNT,
2323 NV04_PFB_BOOT_0_RAM_AMOUNT_4MB);
2327 NVWriteVgaSeq(dev, 0, 1, NVReadVgaSeq(dev, 0, 1) & ~0x20);
2329 io_mapping_free(fb);
2334 nv10_init_compute_mem(struct nvbios *bios)
2336 struct drm_device *dev = bios->dev;
2337 struct drm_nouveau_private *dev_priv = bios->dev->dev_private;
2338 const int mem_width[] = { 0x10, 0x00, 0x20 };
2339 const int mem_width_count = (dev_priv->chipset >= 0x17 ? 3 : 2);
2340 uint32_t patt = 0xdeadbeef;
2341 struct io_mapping *fb;
2344 /* Map the framebuffer aperture */
2345 fb = io_mapping_create_wc(pci_resource_start(dev->pdev, 1),
2346 pci_resource_len(dev->pdev, 1));
2350 bios_wr32(bios, NV10_PFB_REFCTRL, NV10_PFB_REFCTRL_VALID_1);
2352 /* Probe memory bus width */
2353 for (i = 0; i < mem_width_count; i++) {
2354 bios_md32(bios, NV04_PFB_CFG0, 0x30, mem_width[i]);
2356 for (j = 0; j < 4; j++) {
2357 for (k = 0; k < 4; k++)
2358 poke_fb(dev, fb, 0x1c, 0);
2360 poke_fb(dev, fb, 0x1c, patt);
2361 poke_fb(dev, fb, 0x3c, 0);
2363 if (peek_fb(dev, fb, 0x1c) == patt)
2364 goto mem_width_found;
2371 /* Probe amount of installed memory */
2372 for (i = 0; i < 4; i++) {
2373 int off = bios_rd32(bios, NV04_PFB_FIFO_DATA) - 0x100000;
2375 poke_fb(dev, fb, off, patt);
2376 poke_fb(dev, fb, 0, 0);
2378 peek_fb(dev, fb, 0);
2379 peek_fb(dev, fb, 0);
2380 peek_fb(dev, fb, 0);
2381 peek_fb(dev, fb, 0);
2383 if (peek_fb(dev, fb, off) == patt)
2387 /* IC missing - disable the upper half memory space. */
2388 bios_md32(bios, NV04_PFB_CFG0, 0x1000, 0);
2391 io_mapping_free(fb);
2396 nv20_init_compute_mem(struct nvbios *bios)
2398 struct drm_device *dev = bios->dev;
2399 struct drm_nouveau_private *dev_priv = bios->dev->dev_private;
2400 uint32_t mask = (dev_priv->chipset >= 0x25 ? 0x300 : 0x900);
2401 uint32_t amount, off;
2402 struct io_mapping *fb;
2404 /* Map the framebuffer aperture */
2405 fb = io_mapping_create_wc(pci_resource_start(dev->pdev, 1),
2406 pci_resource_len(dev->pdev, 1));
2410 bios_wr32(bios, NV10_PFB_REFCTRL, NV10_PFB_REFCTRL_VALID_1);
2412 /* Allow full addressing */
2413 bios_md32(bios, NV04_PFB_CFG0, 0, mask);
2415 amount = bios_rd32(bios, NV04_PFB_FIFO_DATA);
2416 for (off = amount; off > 0x2000000; off -= 0x2000000)
2417 poke_fb(dev, fb, off - 4, off);
2419 amount = bios_rd32(bios, NV04_PFB_FIFO_DATA);
2420 if (amount != peek_fb(dev, fb, amount - 4))
2421 /* IC missing - disable the upper half memory space. */
2422 bios_md32(bios, NV04_PFB_CFG0, mask, 0);
2424 io_mapping_free(fb);
2429 init_compute_mem(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
2432 * INIT_COMPUTE_MEM opcode: 0x63 ('c')
2434 * offset (8 bit): opcode
2436 * This opcode is meant to set the PFB memory config registers
2437 * appropriately so that we can correctly calculate how much VRAM it
2438 * has (on nv10 and better chipsets the amount of installed VRAM is
2439 * subsequently reported in NV_PFB_CSTATUS (0x10020C)).
2441 * The implementation of this opcode in general consists of several
2444 * 1) Determination of memory type and density. Only necessary for
2445 * really old chipsets, the memory type reported by the strap bits
2446 * (0x101000) is assumed to be accurate on nv05 and newer.
2448 * 2) Determination of the memory bus width. Usually done by a cunning
2449 * combination of writes to offsets 0x1c and 0x3c in the fb, and
2450 * seeing whether the written values are read back correctly.
2452 * Only necessary on nv0x-nv1x and nv34, on the other cards we can
2455 * 3) Determination of how many of the card's RAM pads have ICs
2456 * attached, usually done by a cunning combination of writes to an
2457 * offset slightly less than the maximum memory reported by
2458 * NV_PFB_CSTATUS, then seeing if the test pattern can be read back.
2460 * This appears to be a NOP on IGPs and NV4x or newer chipsets, both io
2461 * logs of the VBIOS and kmmio traces of the binary driver POSTing the
2462 * card show nothing being done for this opcode. Why is it still listed
2466 /* no iexec->execute check by design */
2468 struct drm_nouveau_private *dev_priv = bios->dev->dev_private;
2471 if (dev_priv->chipset >= 0x40 ||
2472 dev_priv->chipset == 0x1a ||
2473 dev_priv->chipset == 0x1f)
2475 else if (dev_priv->chipset >= 0x20 &&
2476 dev_priv->chipset != 0x34)
2477 ret = nv20_init_compute_mem(bios);
2478 else if (dev_priv->chipset >= 0x10)
2479 ret = nv10_init_compute_mem(bios);
2480 else if (dev_priv->chipset >= 0x5)
2481 ret = nv05_init_compute_mem(bios);
2483 ret = nv04_init_compute_mem(bios);
2492 init_reset(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
2495 * INIT_RESET opcode: 0x65 ('e')
2497 * offset (8 bit): opcode
2498 * offset + 1 (32 bit): register
2499 * offset + 5 (32 bit): value1
2500 * offset + 9 (32 bit): value2
2502 * Assign "value1" to "register", then assign "value2" to "register"
2505 uint32_t reg = ROM32(bios->data[offset + 1]);
2506 uint32_t value1 = ROM32(bios->data[offset + 5]);
2507 uint32_t value2 = ROM32(bios->data[offset + 9]);
2508 uint32_t pci_nv_19, pci_nv_20;
2510 /* no iexec->execute check by design */
2512 pci_nv_19 = bios_rd32(bios, NV_PBUS_PCI_NV_19);
2513 bios_wr32(bios, NV_PBUS_PCI_NV_19, pci_nv_19 & ~0xf00);
2515 bios_wr32(bios, reg, value1);
2519 bios_wr32(bios, reg, value2);
2520 bios_wr32(bios, NV_PBUS_PCI_NV_19, pci_nv_19);
2522 pci_nv_20 = bios_rd32(bios, NV_PBUS_PCI_NV_20);
2523 pci_nv_20 &= ~NV_PBUS_PCI_NV_20_ROM_SHADOW_ENABLED; /* 0xfffffffe */
2524 bios_wr32(bios, NV_PBUS_PCI_NV_20, pci_nv_20);
2530 init_configure_mem(struct nvbios *bios, uint16_t offset,
2531 struct init_exec *iexec)
2534 * INIT_CONFIGURE_MEM opcode: 0x66 ('f')
2536 * offset (8 bit): opcode
2538 * Equivalent to INIT_DONE on bios version 3 or greater.
2539 * For early bios versions, sets up the memory registers, using values
2540 * taken from the memory init table
2543 /* no iexec->execute check by design */
2545 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);
2546 uint16_t seqtbloffs = bios->legacy.sdr_seq_tbl_ptr, meminitdata = meminitoffs + 6;
2549 if (bios->major_version > 2)
2552 bios_idxprt_wr(bios, NV_VIO_SRX, NV_VIO_SR_CLOCK_INDEX, bios_idxprt_rd(
2553 bios, NV_VIO_SRX, NV_VIO_SR_CLOCK_INDEX) | 0x20);
2555 if (bios->data[meminitoffs] & 1)
2556 seqtbloffs = bios->legacy.ddr_seq_tbl_ptr;
2558 for (reg = ROM32(bios->data[seqtbloffs]);
2560 reg = ROM32(bios->data[seqtbloffs += 4])) {
2564 data = NV04_PFB_PRE_CMD_PRECHARGE;
2567 data = NV04_PFB_PAD_CKE_NORMAL;
2570 data = NV04_PFB_REF_CMD_REFRESH;
2573 data = ROM32(bios->data[meminitdata]);
2575 if (data == 0xffffffff)
2579 bios_wr32(bios, reg, data);
2586 init_configure_clk(struct nvbios *bios, uint16_t offset,
2587 struct init_exec *iexec)
2590 * INIT_CONFIGURE_CLK opcode: 0x67 ('g')
2592 * offset (8 bit): opcode
2594 * Equivalent to INIT_DONE on bios version 3 or greater.
2595 * For early bios versions, sets up the NVClk and MClk PLLs, using
2596 * values taken from the memory init table
2599 /* no iexec->execute check by design */
2601 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);
2604 if (bios->major_version > 2)
2607 clock = ROM16(bios->data[meminitoffs + 4]) * 10;
2608 setPLL(bios, NV_PRAMDAC_NVPLL_COEFF, clock);
2610 clock = ROM16(bios->data[meminitoffs + 2]) * 10;
2611 if (bios->data[meminitoffs] & 1) /* DDR */
2613 setPLL(bios, NV_PRAMDAC_MPLL_COEFF, clock);
2619 init_configure_preinit(struct nvbios *bios, uint16_t offset,
2620 struct init_exec *iexec)
2623 * INIT_CONFIGURE_PREINIT opcode: 0x68 ('h')
2625 * offset (8 bit): opcode
2627 * Equivalent to INIT_DONE on bios version 3 or greater.
2628 * For early bios versions, does early init, loading ram and crystal
2629 * configuration from straps into CR3C
2632 /* no iexec->execute check by design */
2634 uint32_t straps = bios_rd32(bios, NV_PEXTDEV_BOOT_0);
2635 uint8_t cr3c = ((straps << 2) & 0xf0) | (straps & 0x40) >> 6;
2637 if (bios->major_version > 2)
2640 bios_idxprt_wr(bios, NV_CIO_CRX__COLOR,
2641 NV_CIO_CRE_SCRATCH4__INDEX, cr3c);
2647 init_io(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
2650 * INIT_IO opcode: 0x69 ('i')
2652 * offset (8 bit): opcode
2653 * offset + 1 (16 bit): CRTC port
2654 * offset + 3 (8 bit): mask
2655 * offset + 4 (8 bit): data
2657 * Assign ((IOVAL("crtc port") & "mask") | "data") to "crtc port"
2660 struct drm_nouveau_private *dev_priv = bios->dev->dev_private;
2661 uint16_t crtcport = ROM16(bios->data[offset + 1]);
2662 uint8_t mask = bios->data[offset + 3];
2663 uint8_t data = bios->data[offset + 4];
2665 if (!iexec->execute)
2668 BIOSLOG(bios, "0x%04X: Port: 0x%04X, Mask: 0x%02X, Data: 0x%02X\n",
2669 offset, crtcport, mask, data);
2672 * I have no idea what this does, but NVIDIA do this magic sequence
2673 * in the places where this INIT_IO happens..
2675 if (dev_priv->card_type >= NV_50 && crtcport == 0x3c3 && data == 1) {
2678 bios_wr32(bios, 0x614100, (bios_rd32(
2679 bios, 0x614100) & 0x0fffffff) | 0x00800000);
2681 bios_wr32(bios, 0x00e18c, bios_rd32(
2682 bios, 0x00e18c) | 0x00020000);
2684 bios_wr32(bios, 0x614900, (bios_rd32(
2685 bios, 0x614900) & 0x0fffffff) | 0x00800000);
2687 bios_wr32(bios, 0x000200, bios_rd32(
2688 bios, 0x000200) & ~0x40000000);
2692 bios_wr32(bios, 0x00e18c, bios_rd32(
2693 bios, 0x00e18c) & ~0x00020000);
2695 bios_wr32(bios, 0x000200, bios_rd32(
2696 bios, 0x000200) | 0x40000000);
2698 bios_wr32(bios, 0x614100, 0x00800018);
2699 bios_wr32(bios, 0x614900, 0x00800018);
2703 bios_wr32(bios, 0x614100, 0x10000018);
2704 bios_wr32(bios, 0x614900, 0x10000018);
2706 for (i = 0; i < 3; i++)
2707 bios_wr32(bios, 0x614280 + (i*0x800), bios_rd32(
2708 bios, 0x614280 + (i*0x800)) & 0xf0f0f0f0);
2710 for (i = 0; i < 2; i++)
2711 bios_wr32(bios, 0x614300 + (i*0x800), bios_rd32(
2712 bios, 0x614300 + (i*0x800)) & 0xfffff0f0);
2714 for (i = 0; i < 3; i++)
2715 bios_wr32(bios, 0x614380 + (i*0x800), bios_rd32(
2716 bios, 0x614380 + (i*0x800)) & 0xfffff0f0);
2718 for (i = 0; i < 2; i++)
2719 bios_wr32(bios, 0x614200 + (i*0x800), bios_rd32(
2720 bios, 0x614200 + (i*0x800)) & 0xfffffff0);
2722 for (i = 0; i < 2; i++)
2723 bios_wr32(bios, 0x614108 + (i*0x800), bios_rd32(
2724 bios, 0x614108 + (i*0x800)) & 0x0fffffff);
2728 bios_port_wr(bios, crtcport, (bios_port_rd(bios, crtcport) & mask) |
2734 init_sub(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
2737 * INIT_SUB opcode: 0x6B ('k')
2739 * offset (8 bit): opcode
2740 * offset + 1 (8 bit): script number
2742 * Execute script number "script number", as a subroutine
2745 uint8_t sub = bios->data[offset + 1];
2747 if (!iexec->execute)
2750 BIOSLOG(bios, "0x%04X: Calling script %d\n", offset, sub);
2752 parse_init_table(bios,
2753 ROM16(bios->data[bios->init_script_tbls_ptr + sub * 2]),
2756 BIOSLOG(bios, "0x%04X: End of script %d\n", offset, sub);
2762 init_ram_condition(struct nvbios *bios, uint16_t offset,
2763 struct init_exec *iexec)
2766 * INIT_RAM_CONDITION opcode: 0x6D ('m')
2768 * offset (8 bit): opcode
2769 * offset + 1 (8 bit): mask
2770 * offset + 2 (8 bit): cmpval
2772 * Test if (NV04_PFB_BOOT_0 & "mask") equals "cmpval".
2773 * If condition not met skip subsequent opcodes until condition is
2774 * inverted (INIT_NOT), or we hit INIT_RESUME
2777 uint8_t mask = bios->data[offset + 1];
2778 uint8_t cmpval = bios->data[offset + 2];
2781 if (!iexec->execute)
2784 data = bios_rd32(bios, NV04_PFB_BOOT_0) & mask;
2786 BIOSLOG(bios, "0x%04X: Checking if 0x%08X equals 0x%08X\n",
2787 offset, data, cmpval);
2790 BIOSLOG(bios, "0x%04X: Condition fulfilled -- continuing to execute\n", offset);
2792 BIOSLOG(bios, "0x%04X: Condition not fulfilled -- skipping following commands\n", offset);
2793 iexec->execute = false;
2800 init_nv_reg(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
2803 * INIT_NV_REG opcode: 0x6E ('n')
2805 * offset (8 bit): opcode
2806 * offset + 1 (32 bit): register
2807 * offset + 5 (32 bit): mask
2808 * offset + 9 (32 bit): data
2810 * Assign ((REGVAL("register") & "mask") | "data") to "register"
2813 uint32_t reg = ROM32(bios->data[offset + 1]);
2814 uint32_t mask = ROM32(bios->data[offset + 5]);
2815 uint32_t data = ROM32(bios->data[offset + 9]);
2817 if (!iexec->execute)
2820 BIOSLOG(bios, "0x%04X: Reg: 0x%08X, Mask: 0x%08X, Data: 0x%08X\n",
2821 offset, reg, mask, data);
2823 bios_wr32(bios, reg, (bios_rd32(bios, reg) & mask) | data);
2829 init_macro(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
2832 * INIT_MACRO opcode: 0x6F ('o')
2834 * offset (8 bit): opcode
2835 * offset + 1 (8 bit): macro number
2837 * Look up macro index "macro number" in the macro index table.
2838 * The macro index table entry has 1 byte for the index in the macro
2839 * table, and 1 byte for the number of times to repeat the macro.
2840 * The macro table entry has 4 bytes for the register address and
2841 * 4 bytes for the value to write to that register
2844 uint8_t macro_index_tbl_idx = bios->data[offset + 1];
2845 uint16_t tmp = bios->macro_index_tbl_ptr + (macro_index_tbl_idx * MACRO_INDEX_SIZE);
2846 uint8_t macro_tbl_idx = bios->data[tmp];
2847 uint8_t count = bios->data[tmp + 1];
2851 if (!iexec->execute)
2854 BIOSLOG(bios, "0x%04X: Macro: 0x%02X, MacroTableIndex: 0x%02X, "
2856 offset, macro_index_tbl_idx, macro_tbl_idx, count);
2858 for (i = 0; i < count; i++) {
2859 uint16_t macroentryptr = bios->macro_tbl_ptr + (macro_tbl_idx + i) * MACRO_SIZE;
2861 reg = ROM32(bios->data[macroentryptr]);
2862 data = ROM32(bios->data[macroentryptr + 4]);
2864 bios_wr32(bios, reg, data);
2871 init_done(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
2874 * INIT_DONE opcode: 0x71 ('q')
2876 * offset (8 bit): opcode
2878 * End the current script
2881 /* mild retval abuse to stop parsing this table */
2886 init_resume(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
2889 * INIT_RESUME opcode: 0x72 ('r')
2891 * offset (8 bit): opcode
2893 * End the current execute / no-execute condition
2899 iexec->execute = true;
2900 BIOSLOG(bios, "0x%04X: ---- Executing following commands ----\n", offset);
2906 init_time(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
2909 * INIT_TIME opcode: 0x74 ('t')
2911 * offset (8 bit): opcode
2912 * offset + 1 (16 bit): time
2914 * Sleep for "time" microseconds.
2917 unsigned time = ROM16(bios->data[offset + 1]);
2919 if (!iexec->execute)
2922 BIOSLOG(bios, "0x%04X: Sleeping for 0x%04X microseconds\n",
2928 mdelay((time + 900) / 1000);
2934 init_condition(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
2937 * INIT_CONDITION opcode: 0x75 ('u')
2939 * offset (8 bit): opcode
2940 * offset + 1 (8 bit): condition number
2942 * Check condition "condition number" in the condition table.
2943 * If condition not met skip subsequent opcodes until condition is
2944 * inverted (INIT_NOT), or we hit INIT_RESUME
2947 uint8_t cond = bios->data[offset + 1];
2949 if (!iexec->execute)
2952 BIOSLOG(bios, "0x%04X: Condition: 0x%02X\n", offset, cond);
2954 if (bios_condition_met(bios, offset, cond))
2955 BIOSLOG(bios, "0x%04X: Condition fulfilled -- continuing to execute\n", offset);
2957 BIOSLOG(bios, "0x%04X: Condition not fulfilled -- skipping following commands\n", offset);
2958 iexec->execute = false;
2965 init_io_condition(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
2968 * INIT_IO_CONDITION opcode: 0x76
2970 * offset (8 bit): opcode
2971 * offset + 1 (8 bit): condition number
2973 * Check condition "condition number" in the io condition table.
2974 * If condition not met skip subsequent opcodes until condition is
2975 * inverted (INIT_NOT), or we hit INIT_RESUME
2978 uint8_t cond = bios->data[offset + 1];
2980 if (!iexec->execute)
2983 BIOSLOG(bios, "0x%04X: IO condition: 0x%02X\n", offset, cond);
2985 if (io_condition_met(bios, offset, cond))
2986 BIOSLOG(bios, "0x%04X: Condition fulfilled -- continuing to execute\n", offset);
2988 BIOSLOG(bios, "0x%04X: Condition not fulfilled -- skipping following commands\n", offset);
2989 iexec->execute = false;
2996 init_index_io(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
2999 * INIT_INDEX_IO opcode: 0x78 ('x')
3001 * offset (8 bit): opcode
3002 * offset + 1 (16 bit): CRTC port
3003 * offset + 3 (8 bit): CRTC index
3004 * offset + 4 (8 bit): mask
3005 * offset + 5 (8 bit): data
3007 * Read value at index "CRTC index" on "CRTC port", AND with "mask",
3008 * OR with "data", write-back
3011 uint16_t crtcport = ROM16(bios->data[offset + 1]);
3012 uint8_t crtcindex = bios->data[offset + 3];
3013 uint8_t mask = bios->data[offset + 4];
3014 uint8_t data = bios->data[offset + 5];
3017 if (!iexec->execute)
3020 BIOSLOG(bios, "0x%04X: Port: 0x%04X, Index: 0x%02X, Mask: 0x%02X, "
3022 offset, crtcport, crtcindex, mask, data);
3024 value = (bios_idxprt_rd(bios, crtcport, crtcindex) & mask) | data;
3025 bios_idxprt_wr(bios, crtcport, crtcindex, value);
3031 init_pll(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
3034 * INIT_PLL opcode: 0x79 ('y')
3036 * offset (8 bit): opcode
3037 * offset + 1 (32 bit): register
3038 * offset + 5 (16 bit): freq
3040 * Set PLL register "register" to coefficients for frequency (10kHz)
3044 uint32_t reg = ROM32(bios->data[offset + 1]);
3045 uint16_t freq = ROM16(bios->data[offset + 5]);
3047 if (!iexec->execute)
3050 BIOSLOG(bios, "0x%04X: Reg: 0x%08X, Freq: %d0kHz\n", offset, reg, freq);
3052 setPLL(bios, reg, freq * 10);
3058 init_zm_reg(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
3061 * INIT_ZM_REG opcode: 0x7A ('z')
3063 * offset (8 bit): opcode
3064 * offset + 1 (32 bit): register
3065 * offset + 5 (32 bit): value
3067 * Assign "value" to "register"
3070 uint32_t reg = ROM32(bios->data[offset + 1]);
3071 uint32_t value = ROM32(bios->data[offset + 5]);
3073 if (!iexec->execute)
3076 if (reg == 0x000200)
3079 bios_wr32(bios, reg, value);
3085 init_ram_restrict_pll(struct nvbios *bios, uint16_t offset,
3086 struct init_exec *iexec)
3089 * INIT_RAM_RESTRICT_PLL opcode: 0x87 ('')
3091 * offset (8 bit): opcode
3092 * offset + 1 (8 bit): PLL type
3093 * offset + 2 (32 bit): frequency 0
3095 * Uses the RAMCFG strap of PEXTDEV_BOOT as an index into the table at
3096 * ram_restrict_table_ptr. The value read from there is used to select
3097 * a frequency from the table starting at 'frequency 0' to be
3098 * programmed into the PLL corresponding to 'type'.
3100 * The PLL limits table on cards using this opcode has a mapping of
3101 * 'type' to the relevant registers.
3104 struct drm_device *dev = bios->dev;
3105 uint32_t strap = (bios_rd32(bios, NV_PEXTDEV_BOOT_0) & 0x0000003c) >> 2;
3106 uint8_t index = bios->data[bios->ram_restrict_tbl_ptr + strap];
3107 uint8_t type = bios->data[offset + 1];
3108 uint32_t freq = ROM32(bios->data[offset + 2 + (index * 4)]);
3109 uint8_t *pll_limits = &bios->data[bios->pll_limit_tbl_ptr], *entry;
3110 int len = 2 + bios->ram_restrict_group_count * 4;
3113 if (!iexec->execute)
3116 if (!bios->pll_limit_tbl_ptr || (pll_limits[0] & 0xf0) != 0x30) {
3117 NV_ERROR(dev, "PLL limits table not version 3.x\n");
3118 return len; /* deliberate, allow default clocks to remain */
3121 entry = pll_limits + pll_limits[1];
3122 for (i = 0; i < pll_limits[3]; i++, entry += pll_limits[2]) {
3123 if (entry[0] == type) {
3124 uint32_t reg = ROM32(entry[3]);
3126 BIOSLOG(bios, "0x%04X: "
3127 "Type %02x Reg 0x%08x Freq %dKHz\n",
3128 offset, type, reg, freq);
3130 setPLL(bios, reg, freq);
3135 NV_ERROR(dev, "PLL type 0x%02x not found in PLL limits table", type);
3140 init_8c(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
3143 * INIT_8C opcode: 0x8C ('')
3153 init_8d(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
3156 * INIT_8D opcode: 0x8D ('')
3166 init_gpio(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
3169 * INIT_GPIO opcode: 0x8E ('')
3171 * offset (8 bit): opcode
3173 * Loop over all entries in the DCB GPIO table, and initialise
3174 * each GPIO according to various values listed in each entry
3177 if (iexec->execute && bios->execute)
3178 nouveau_gpio_reset(bios->dev);
3184 init_ram_restrict_zm_reg_group(struct nvbios *bios, uint16_t offset,
3185 struct init_exec *iexec)
3188 * INIT_RAM_RESTRICT_ZM_REG_GROUP opcode: 0x8F ('')
3190 * offset (8 bit): opcode
3191 * offset + 1 (32 bit): reg
3192 * offset + 5 (8 bit): regincrement
3193 * offset + 6 (8 bit): count
3194 * offset + 7 (32 bit): value 1,1
3197 * Use the RAMCFG strap of PEXTDEV_BOOT as an index into the table at
3198 * ram_restrict_table_ptr. The value read from here is 'n', and
3199 * "value 1,n" gets written to "reg". This repeats "count" times and on
3200 * each iteration 'm', "reg" increases by "regincrement" and
3201 * "value m,n" is used. The extent of n is limited by a number read
3202 * from the 'M' BIT table, herein called "blocklen"
3205 uint32_t reg = ROM32(bios->data[offset + 1]);
3206 uint8_t regincrement = bios->data[offset + 5];
3207 uint8_t count = bios->data[offset + 6];
3208 uint32_t strap_ramcfg, data;
3209 /* previously set by 'M' BIT table */
3210 uint16_t blocklen = bios->ram_restrict_group_count * 4;
3211 int len = 7 + count * blocklen;
3215 /* critical! to know the length of the opcode */;
3218 "0x%04X: Zero block length - has the M table "
3219 "been parsed?\n", offset);
3223 if (!iexec->execute)
3226 strap_ramcfg = (bios_rd32(bios, NV_PEXTDEV_BOOT_0) >> 2) & 0xf;
3227 index = bios->data[bios->ram_restrict_tbl_ptr + strap_ramcfg];
3229 BIOSLOG(bios, "0x%04X: Reg: 0x%08X, RegIncrement: 0x%02X, "
3230 "Count: 0x%02X, StrapRamCfg: 0x%02X, Index: 0x%02X\n",
3231 offset, reg, regincrement, count, strap_ramcfg, index);
3233 for (i = 0; i < count; i++) {
3234 data = ROM32(bios->data[offset + 7 + index * 4 + blocklen * i]);
3236 bios_wr32(bios, reg, data);
3238 reg += regincrement;
3245 init_copy_zm_reg(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
3248 * INIT_COPY_ZM_REG opcode: 0x90 ('')
3250 * offset (8 bit): opcode
3251 * offset + 1 (32 bit): src reg
3252 * offset + 5 (32 bit): dst reg
3254 * Put contents of "src reg" into "dst reg"
3257 uint32_t srcreg = ROM32(bios->data[offset + 1]);
3258 uint32_t dstreg = ROM32(bios->data[offset + 5]);
3260 if (!iexec->execute)
3263 bios_wr32(bios, dstreg, bios_rd32(bios, srcreg));
3269 init_zm_reg_group_addr_latched(struct nvbios *bios, uint16_t offset,
3270 struct init_exec *iexec)
3273 * INIT_ZM_REG_GROUP_ADDRESS_LATCHED opcode: 0x91 ('')
3275 * offset (8 bit): opcode
3276 * offset + 1 (32 bit): dst reg
3277 * offset + 5 (8 bit): count
3278 * offset + 6 (32 bit): data 1
3281 * For each of "count" values write "data n" to "dst reg"
3284 uint32_t reg = ROM32(bios->data[offset + 1]);
3285 uint8_t count = bios->data[offset + 5];
3286 int len = 6 + count * 4;
3289 if (!iexec->execute)
3292 for (i = 0; i < count; i++) {
3293 uint32_t data = ROM32(bios->data[offset + 6 + 4 * i]);
3294 bios_wr32(bios, reg, data);
3301 init_reserved(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
3304 * INIT_RESERVED opcode: 0x92 ('')
3306 * offset (8 bit): opcode
3308 * Seemingly does nothing
3315 init_96(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
3318 * INIT_96 opcode: 0x96 ('')
3320 * offset (8 bit): opcode
3321 * offset + 1 (32 bit): sreg
3322 * offset + 5 (8 bit): sshift
3323 * offset + 6 (8 bit): smask
3324 * offset + 7 (8 bit): index
3325 * offset + 8 (32 bit): reg
3326 * offset + 12 (32 bit): mask
3327 * offset + 16 (8 bit): shift
3331 uint16_t xlatptr = bios->init96_tbl_ptr + (bios->data[offset + 7] * 2);
3332 uint32_t reg = ROM32(bios->data[offset + 8]);
3333 uint32_t mask = ROM32(bios->data[offset + 12]);
3336 val = bios_rd32(bios, ROM32(bios->data[offset + 1]));
3337 if (bios->data[offset + 5] < 0x80)
3338 val >>= bios->data[offset + 5];
3340 val <<= (0x100 - bios->data[offset + 5]);
3341 val &= bios->data[offset + 6];
3343 val = bios->data[ROM16(bios->data[xlatptr]) + val];
3344 val <<= bios->data[offset + 16];
3346 if (!iexec->execute)
3349 bios_wr32(bios, reg, (bios_rd32(bios, reg) & mask) | val);
3354 init_97(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
3357 * INIT_97 opcode: 0x97 ('')
3359 * offset (8 bit): opcode
3360 * offset + 1 (32 bit): register
3361 * offset + 5 (32 bit): mask
3362 * offset + 9 (32 bit): value
3364 * Adds "value" to "register" preserving the fields specified
3368 uint32_t reg = ROM32(bios->data[offset + 1]);
3369 uint32_t mask = ROM32(bios->data[offset + 5]);
3370 uint32_t add = ROM32(bios->data[offset + 9]);
3373 val = bios_rd32(bios, reg);
3374 val = (val & mask) | ((val + add) & ~mask);
3376 if (!iexec->execute)
3379 bios_wr32(bios, reg, val);
3384 init_auxch(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
3387 * INIT_AUXCH opcode: 0x98 ('')
3389 * offset (8 bit): opcode
3390 * offset + 1 (32 bit): address
3391 * offset + 5 (8 bit): count
3392 * offset + 6 (8 bit): mask 0
3393 * offset + 7 (8 bit): data 0
3398 struct drm_device *dev = bios->dev;
3399 struct nouveau_i2c_chan *auxch;
3400 uint32_t addr = ROM32(bios->data[offset + 1]);
3401 uint8_t count = bios->data[offset + 5];
3402 int len = 6 + count * 2;
3405 if (!bios->display.output) {
3406 NV_ERROR(dev, "INIT_AUXCH: no active output\n");
3410 auxch = init_i2c_device_find(dev, bios->display.output->i2c_index);
3412 NV_ERROR(dev, "INIT_AUXCH: couldn't get auxch %d\n",
3413 bios->display.output->i2c_index);
3417 if (!iexec->execute)
3421 for (i = 0; i < count; i++, offset += 2) {
3424 ret = nouveau_dp_auxch(auxch, 9, addr, &data, 1);
3426 NV_ERROR(dev, "INIT_AUXCH: rd auxch fail %d\n", ret);
3430 data &= bios->data[offset + 0];
3431 data |= bios->data[offset + 1];
3433 ret = nouveau_dp_auxch(auxch, 8, addr, &data, 1);
3435 NV_ERROR(dev, "INIT_AUXCH: wr auxch fail %d\n", ret);
3444 init_zm_auxch(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
3447 * INIT_ZM_AUXCH opcode: 0x99 ('')
3449 * offset (8 bit): opcode
3450 * offset + 1 (32 bit): address
3451 * offset + 5 (8 bit): count
3452 * offset + 6 (8 bit): data 0
3457 struct drm_device *dev = bios->dev;
3458 struct nouveau_i2c_chan *auxch;
3459 uint32_t addr = ROM32(bios->data[offset + 1]);
3460 uint8_t count = bios->data[offset + 5];
3461 int len = 6 + count;
3464 if (!bios->display.output) {
3465 NV_ERROR(dev, "INIT_ZM_AUXCH: no active output\n");
3469 auxch = init_i2c_device_find(dev, bios->display.output->i2c_index);
3471 NV_ERROR(dev, "INIT_ZM_AUXCH: couldn't get auxch %d\n",
3472 bios->display.output->i2c_index);
3476 if (!iexec->execute)
3480 for (i = 0; i < count; i++, offset++) {
3481 ret = nouveau_dp_auxch(auxch, 8, addr, &bios->data[offset], 1);
3483 NV_ERROR(dev, "INIT_ZM_AUXCH: wr auxch fail %d\n", ret);
3492 init_i2c_long_if(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
3495 * INIT_I2C_LONG_IF opcode: 0x9A ('')
3497 * offset (8 bit): opcode
3498 * offset + 1 (8 bit): DCB I2C table entry index
3499 * offset + 2 (8 bit): I2C slave address
3500 * offset + 3 (16 bit): I2C register
3501 * offset + 5 (8 bit): mask
3502 * offset + 6 (8 bit): data
3504 * Read the register given by "I2C register" on the device addressed
3505 * by "I2C slave address" on the I2C bus given by "DCB I2C table
3506 * entry index". Compare the result AND "mask" to "data".
3507 * If they're not equal, skip subsequent opcodes until condition is
3508 * inverted (INIT_NOT), or we hit INIT_RESUME
3511 uint8_t i2c_index = bios->data[offset + 1];
3512 uint8_t i2c_address = bios->data[offset + 2] >> 1;
3513 uint8_t reglo = bios->data[offset + 3];
3514 uint8_t reghi = bios->data[offset + 4];
3515 uint8_t mask = bios->data[offset + 5];
3516 uint8_t data = bios->data[offset + 6];
3517 struct nouveau_i2c_chan *chan;
3518 uint8_t buf0[2] = { reghi, reglo };
3520 struct i2c_msg msg[2] = {
3521 { i2c_address, 0, 1, buf0 },
3522 { i2c_address, I2C_M_RD, 1, buf1 },
3526 /* no execute check by design */
3528 BIOSLOG(bios, "0x%04X: DCBI2CIndex: 0x%02X, I2CAddress: 0x%02X\n",
3529 offset, i2c_index, i2c_address);
3531 chan = init_i2c_device_find(bios->dev, i2c_index);
3536 ret = i2c_transfer(&chan->adapter, msg, 2);
3538 BIOSLOG(bios, "0x%04X: I2CReg: 0x%02X:0x%02X, Value: [no device], "
3539 "Mask: 0x%02X, Data: 0x%02X\n",
3540 offset, reghi, reglo, mask, data);
3545 BIOSLOG(bios, "0x%04X: I2CReg: 0x%02X:0x%02X, Value: 0x%02X, "
3546 "Mask: 0x%02X, Data: 0x%02X\n",
3547 offset, reghi, reglo, buf1[0], mask, data);
3549 iexec->execute = ((buf1[0] & mask) == data);
3554 static struct init_tbl_entry itbl_entry[] = {
3555 /* command name , id , length , offset , mult , command handler */
3556 /* INIT_PROG (0x31, 15, 10, 4) removed due to no example of use */
3557 { "INIT_IO_RESTRICT_PROG" , 0x32, init_io_restrict_prog },
3558 { "INIT_REPEAT" , 0x33, init_repeat },
3559 { "INIT_IO_RESTRICT_PLL" , 0x34, init_io_restrict_pll },
3560 { "INIT_END_REPEAT" , 0x36, init_end_repeat },
3561 { "INIT_COPY" , 0x37, init_copy },
3562 { "INIT_NOT" , 0x38, init_not },
3563 { "INIT_IO_FLAG_CONDITION" , 0x39, init_io_flag_condition },
3564 { "INIT_DP_CONDITION" , 0x3A, init_dp_condition },
3565 { "INIT_OP_3B" , 0x3B, init_op_3b },
3566 { "INIT_OP_3C" , 0x3C, init_op_3c },
3567 { "INIT_INDEX_ADDRESS_LATCHED" , 0x49, init_idx_addr_latched },
3568 { "INIT_IO_RESTRICT_PLL2" , 0x4A, init_io_restrict_pll2 },
3569 { "INIT_PLL2" , 0x4B, init_pll2 },
3570 { "INIT_I2C_BYTE" , 0x4C, init_i2c_byte },
3571 { "INIT_ZM_I2C_BYTE" , 0x4D, init_zm_i2c_byte },
3572 { "INIT_ZM_I2C" , 0x4E, init_zm_i2c },
3573 { "INIT_TMDS" , 0x4F, init_tmds },
3574 { "INIT_ZM_TMDS_GROUP" , 0x50, init_zm_tmds_group },
3575 { "INIT_CR_INDEX_ADDRESS_LATCHED" , 0x51, init_cr_idx_adr_latch },
3576 { "INIT_CR" , 0x52, init_cr },
3577 { "INIT_ZM_CR" , 0x53, init_zm_cr },
3578 { "INIT_ZM_CR_GROUP" , 0x54, init_zm_cr_group },
3579 { "INIT_CONDITION_TIME" , 0x56, init_condition_time },
3580 { "INIT_LTIME" , 0x57, init_ltime },
3581 { "INIT_ZM_REG_SEQUENCE" , 0x58, init_zm_reg_sequence },
3582 /* INIT_INDIRECT_REG (0x5A, 7, 0, 0) removed due to no example of use */
3583 { "INIT_SUB_DIRECT" , 0x5B, init_sub_direct },
3584 { "INIT_JUMP" , 0x5C, init_jump },
3585 { "INIT_I2C_IF" , 0x5E, init_i2c_if },
3586 { "INIT_COPY_NV_REG" , 0x5F, init_copy_nv_reg },
3587 { "INIT_ZM_INDEX_IO" , 0x62, init_zm_index_io },
3588 { "INIT_COMPUTE_MEM" , 0x63, init_compute_mem },
3589 { "INIT_RESET" , 0x65, init_reset },
3590 { "INIT_CONFIGURE_MEM" , 0x66, init_configure_mem },
3591 { "INIT_CONFIGURE_CLK" , 0x67, init_configure_clk },
3592 { "INIT_CONFIGURE_PREINIT" , 0x68, init_configure_preinit },
3593 { "INIT_IO" , 0x69, init_io },
3594 { "INIT_SUB" , 0x6B, init_sub },
3595 { "INIT_RAM_CONDITION" , 0x6D, init_ram_condition },
3596 { "INIT_NV_REG" , 0x6E, init_nv_reg },
3597 { "INIT_MACRO" , 0x6F, init_macro },
3598 { "INIT_DONE" , 0x71, init_done },
3599 { "INIT_RESUME" , 0x72, init_resume },
3600 /* INIT_RAM_CONDITION2 (0x73, 9, 0, 0) removed due to no example of use */
3601 { "INIT_TIME" , 0x74, init_time },
3602 { "INIT_CONDITION" , 0x75, init_condition },
3603 { "INIT_IO_CONDITION" , 0x76, init_io_condition },
3604 { "INIT_INDEX_IO" , 0x78, init_index_io },
3605 { "INIT_PLL" , 0x79, init_pll },
3606 { "INIT_ZM_REG" , 0x7A, init_zm_reg },
3607 { "INIT_RAM_RESTRICT_PLL" , 0x87, init_ram_restrict_pll },
3608 { "INIT_8C" , 0x8C, init_8c },
3609 { "INIT_8D" , 0x8D, init_8d },
3610 { "INIT_GPIO" , 0x8E, init_gpio },
3611 { "INIT_RAM_RESTRICT_ZM_REG_GROUP" , 0x8F, init_ram_restrict_zm_reg_group },
3612 { "INIT_COPY_ZM_REG" , 0x90, init_copy_zm_reg },
3613 { "INIT_ZM_REG_GROUP_ADDRESS_LATCHED" , 0x91, init_zm_reg_group_addr_latched },
3614 { "INIT_RESERVED" , 0x92, init_reserved },
3615 { "INIT_96" , 0x96, init_96 },
3616 { "INIT_97" , 0x97, init_97 },
3617 { "INIT_AUXCH" , 0x98, init_auxch },
3618 { "INIT_ZM_AUXCH" , 0x99, init_zm_auxch },
3619 { "INIT_I2C_LONG_IF" , 0x9A, init_i2c_long_if },
3623 #define MAX_TABLE_OPS 1000
3626 parse_init_table(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
3629 * Parses all commands in an init table.
3631 * We start out executing all commands found in the init table. Some
3632 * opcodes may change the status of iexec->execute to SKIP, which will
3633 * cause the following opcodes to perform no operation until the value
3634 * is changed back to EXECUTE.
3637 int count = 0, i, ret;
3640 /* catch NULL script pointers */
3645 * Loop until INIT_DONE causes us to break out of the loop
3646 * (or until offset > bios length just in case... )
3647 * (and no more than MAX_TABLE_OPS iterations, just in case... )
3649 while ((offset < bios->length) && (count++ < MAX_TABLE_OPS)) {
3650 id = bios->data[offset];
3652 /* Find matching id in itbl_entry */
3653 for (i = 0; itbl_entry[i].name && (itbl_entry[i].id != id); i++)
3656 if (!itbl_entry[i].name) {
3658 "0x%04X: Init table command not found: "
3659 "0x%02X\n", offset, id);
3663 BIOSLOG(bios, "0x%04X: [ (0x%02X) - %s ]\n", offset,
3664 itbl_entry[i].id, itbl_entry[i].name);
3666 /* execute eventual command handler */
3667 ret = (*itbl_entry[i].handler)(bios, offset, iexec);
3669 NV_ERROR(bios->dev, "0x%04X: Failed parsing init "
3670 "table opcode: %s %d\n", offset,
3671 itbl_entry[i].name, ret);
3678 * Add the offset of the current command including all data
3679 * of that command. The offset will then be pointing on the
3685 if (offset >= bios->length)
3687 "Offset 0x%04X greater than known bios image length. "
3688 "Corrupt image?\n", offset);
3689 if (count >= MAX_TABLE_OPS)
3691 "More than %d opcodes to a table is unlikely, "
3692 "is the bios image corrupt?\n", MAX_TABLE_OPS);
3698 parse_init_tables(struct nvbios *bios)
3700 /* Loops and calls parse_init_table() for each present table. */
3704 struct init_exec iexec = {true, false};
3706 if (bios->old_style_init) {
3707 if (bios->init_script_tbls_ptr)
3708 parse_init_table(bios, bios->init_script_tbls_ptr, &iexec);
3709 if (bios->extra_init_script_tbl_ptr)
3710 parse_init_table(bios, bios->extra_init_script_tbl_ptr, &iexec);
3715 while ((table = ROM16(bios->data[bios->init_script_tbls_ptr + i]))) {
3717 "Parsing VBIOS init table %d at offset 0x%04X\n",
3719 BIOSLOG(bios, "0x%04X: ------ Executing following commands ------\n", table);
3721 parse_init_table(bios, table, &iexec);
3726 static uint16_t clkcmptable(struct nvbios *bios, uint16_t clktable, int pxclk)
3728 int compare_record_len, i = 0;
3729 uint16_t compareclk, scriptptr = 0;
3731 if (bios->major_version < 5) /* pre BIT */
3732 compare_record_len = 3;
3734 compare_record_len = 4;
3737 compareclk = ROM16(bios->data[clktable + compare_record_len * i]);
3738 if (pxclk >= compareclk * 10) {
3739 if (bios->major_version < 5) {
3740 uint8_t tmdssub = bios->data[clktable + 2 + compare_record_len * i];
3741 scriptptr = ROM16(bios->data[bios->init_script_tbls_ptr + tmdssub * 2]);
3743 scriptptr = ROM16(bios->data[clktable + 2 + compare_record_len * i]);
3747 } while (compareclk);
3753 run_digital_op_script(struct drm_device *dev, uint16_t scriptptr,
3754 struct dcb_entry *dcbent, int head, bool dl)
3756 struct drm_nouveau_private *dev_priv = dev->dev_private;
3757 struct nvbios *bios = &dev_priv->vbios;
3758 struct init_exec iexec = {true, false};
3760 NV_TRACE(dev, "0x%04X: Parsing digital output script table\n",
3762 bios_idxprt_wr(bios, NV_CIO_CRX__COLOR, NV_CIO_CRE_44,
3763 head ? NV_CIO_CRE_44_HEADB : NV_CIO_CRE_44_HEADA);
3764 /* note: if dcb entries have been merged, index may be misleading */
3765 NVWriteVgaCrtc5758(dev, head, 0, dcbent->index);
3766 parse_init_table(bios, scriptptr, &iexec);
3768 nv04_dfp_bind_head(dev, dcbent, head, dl);
3771 static int call_lvds_manufacturer_script(struct drm_device *dev, struct dcb_entry *dcbent, int head, enum LVDS_script script)
3773 struct drm_nouveau_private *dev_priv = dev->dev_private;
3774 struct nvbios *bios = &dev_priv->vbios;
3775 uint8_t sub = bios->data[bios->fp.xlated_entry + script] + (bios->fp.link_c_increment && dcbent->or & OUTPUT_C ? 1 : 0);
3776 uint16_t scriptofs = ROM16(bios->data[bios->init_script_tbls_ptr + sub * 2]);
3778 if (!bios->fp.xlated_entry || !sub || !scriptofs)
3781 run_digital_op_script(dev, scriptofs, dcbent, head, bios->fp.dual_link);
3783 if (script == LVDS_PANEL_OFF) {
3784 /* off-on delay in ms */
3785 mdelay(ROM16(bios->data[bios->fp.xlated_entry + 7]));
3788 /* Powerbook specific quirks */
3789 if (script == LVDS_RESET &&
3790 (dev->pci_device == 0x0179 || dev->pci_device == 0x0189 ||
3791 dev->pci_device == 0x0329))
3792 nv_write_tmds(dev, dcbent->or, 0, 0x02, 0x72);
3798 static int run_lvds_table(struct drm_device *dev, struct dcb_entry *dcbent, int head, enum LVDS_script script, int pxclk)
3801 * The BIT LVDS table's header has the information to setup the
3802 * necessary registers. Following the standard 4 byte header are:
3803 * A bitmask byte and a dual-link transition pxclk value for use in
3804 * selecting the init script when not using straps; 4 script pointers
3805 * for panel power, selected by output and on/off; and 8 table pointers
3806 * for panel init, the needed one determined by output, and bits in the
3807 * conf byte. These tables are similar to the TMDS tables, consisting
3808 * of a list of pxclks and script pointers.
3810 struct drm_nouveau_private *dev_priv = dev->dev_private;
3811 struct nvbios *bios = &dev_priv->vbios;
3812 unsigned int outputset = (dcbent->or == 4) ? 1 : 0;
3813 uint16_t scriptptr = 0, clktable;
3816 * For now we assume version 3.0 table - g80 support will need some
3823 case LVDS_BACKLIGHT_ON:
3825 scriptptr = ROM16(bios->data[bios->fp.lvdsmanufacturerpointer + 7 + outputset * 2]);
3827 case LVDS_BACKLIGHT_OFF:
3828 case LVDS_PANEL_OFF:
3829 scriptptr = ROM16(bios->data[bios->fp.lvdsmanufacturerpointer + 11 + outputset * 2]);
3832 clktable = bios->fp.lvdsmanufacturerpointer + 15;
3833 if (dcbent->or == 4)
3836 if (dcbent->lvdsconf.use_straps_for_mode) {
3837 if (bios->fp.dual_link)
3839 if (bios->fp.if_is_24bit)
3843 int cmpval_24bit = (dcbent->or == 4) ? 4 : 1;
3845 if (bios->fp.dual_link) {
3850 if (bios->fp.strapless_is_24bit & cmpval_24bit)
3854 clktable = ROM16(bios->data[clktable]);
3856 NV_ERROR(dev, "Pixel clock comparison table not found\n");
3859 scriptptr = clkcmptable(bios, clktable, pxclk);
3863 NV_ERROR(dev, "LVDS output init script not found\n");
3866 run_digital_op_script(dev, scriptptr, dcbent, head, bios->fp.dual_link);
3871 int call_lvds_script(struct drm_device *dev, struct dcb_entry *dcbent, int head, enum LVDS_script script, int pxclk)
3874 * LVDS operations are multiplexed in an effort to present a single API
3875 * which works with two vastly differing underlying structures.
3876 * This acts as the demux
3879 struct drm_nouveau_private *dev_priv = dev->dev_private;
3880 struct nvbios *bios = &dev_priv->vbios;
3881 uint8_t lvds_ver = bios->data[bios->fp.lvdsmanufacturerpointer];
3882 uint32_t sel_clk_binding, sel_clk;
3885 if (bios->fp.last_script_invoc == (script << 1 | head) || !lvds_ver ||
3886 (lvds_ver >= 0x30 && script == LVDS_INIT))
3889 if (!bios->fp.lvds_init_run) {
3890 bios->fp.lvds_init_run = true;
3891 call_lvds_script(dev, dcbent, head, LVDS_INIT, pxclk);
3894 if (script == LVDS_PANEL_ON && bios->fp.reset_after_pclk_change)
3895 call_lvds_script(dev, dcbent, head, LVDS_RESET, pxclk);
3896 if (script == LVDS_RESET && bios->fp.power_off_for_reset)
3897 call_lvds_script(dev, dcbent, head, LVDS_PANEL_OFF, pxclk);
3899 NV_TRACE(dev, "Calling LVDS script %d:\n", script);
3901 /* don't let script change pll->head binding */
3902 sel_clk_binding = bios_rd32(bios, NV_PRAMDAC_SEL_CLK) & 0x50000;
3904 if (lvds_ver < 0x30)
3905 ret = call_lvds_manufacturer_script(dev, dcbent, head, script);
3907 ret = run_lvds_table(dev, dcbent, head, script, pxclk);
3909 bios->fp.last_script_invoc = (script << 1 | head);
3911 sel_clk = NVReadRAMDAC(dev, 0, NV_PRAMDAC_SEL_CLK) & ~0x50000;
3912 NVWriteRAMDAC(dev, 0, NV_PRAMDAC_SEL_CLK, sel_clk | sel_clk_binding);
3913 /* some scripts set a value in NV_PBUS_POWERCTRL_2 and break video overlay */
3914 nvWriteMC(dev, NV_PBUS_POWERCTRL_2, 0);
3919 struct lvdstableheader {
3920 uint8_t lvds_ver, headerlen, recordlen;
3923 static int parse_lvds_manufacturer_table_header(struct drm_device *dev, struct nvbios *bios, struct lvdstableheader *lth)
3926 * BMP version (0xa) LVDS table has a simple header of version and
3927 * record length. The BIT LVDS table has the typical BIT table header:
3928 * version byte, header length byte, record length byte, and a byte for
3929 * the maximum number of records that can be held in the table.
3932 uint8_t lvds_ver, headerlen, recordlen;
3934 memset(lth, 0, sizeof(struct lvdstableheader));
3936 if (bios->fp.lvdsmanufacturerpointer == 0x0) {
3937 NV_ERROR(dev, "Pointer to LVDS manufacturer table invalid\n");
3941 lvds_ver = bios->data[bios->fp.lvdsmanufacturerpointer];
3944 case 0x0a: /* pre NV40 */
3946 recordlen = bios->data[bios->fp.lvdsmanufacturerpointer + 1];
3948 case 0x30: /* NV4x */
3949 headerlen = bios->data[bios->fp.lvdsmanufacturerpointer + 1];
3950 if (headerlen < 0x1f) {
3951 NV_ERROR(dev, "LVDS table header not understood\n");
3954 recordlen = bios->data[bios->fp.lvdsmanufacturerpointer + 2];
3956 case 0x40: /* G80/G90 */
3957 headerlen = bios->data[bios->fp.lvdsmanufacturerpointer + 1];
3958 if (headerlen < 0x7) {
3959 NV_ERROR(dev, "LVDS table header not understood\n");
3962 recordlen = bios->data[bios->fp.lvdsmanufacturerpointer + 2];
3966 "LVDS table revision %d.%d not currently supported\n",
3967 lvds_ver >> 4, lvds_ver & 0xf);
3971 lth->lvds_ver = lvds_ver;
3972 lth->headerlen = headerlen;
3973 lth->recordlen = recordlen;
3979 get_fp_strap(struct drm_device *dev, struct nvbios *bios)
3981 struct drm_nouveau_private *dev_priv = dev->dev_private;
3984 * The fp strap is normally dictated by the "User Strap" in
3985 * PEXTDEV_BOOT_0[20:16], but on BMP cards when bit 2 of the
3986 * Internal_Flags struct at 0x48 is set, the user strap gets overriden
3987 * by the PCI subsystem ID during POST, but not before the previous user
3988 * strap has been committed to CR58 for CR57=0xf on head A, which may be
3989 * read and used instead
3992 if (bios->major_version < 5 && bios->data[0x48] & 0x4)
3993 return NVReadVgaCrtc5758(dev, 0, 0xf) & 0xf;
3995 if (dev_priv->card_type >= NV_50)
3996 return (bios_rd32(bios, NV_PEXTDEV_BOOT_0) >> 24) & 0xf;
3998 return (bios_rd32(bios, NV_PEXTDEV_BOOT_0) >> 16) & 0xf;
4001 static int parse_fp_mode_table(struct drm_device *dev, struct nvbios *bios)
4004 uint8_t fptable_ver, headerlen = 0, recordlen, fpentries = 0xf, fpindex;
4005 int ret, ofs, fpstrapping;
4006 struct lvdstableheader lth;
4008 if (bios->fp.fptablepointer == 0x0) {
4009 /* Apple cards don't have the fp table; the laptops use DDC */
4010 /* The table is also missing on some x86 IGPs */
4012 NV_ERROR(dev, "Pointer to flat panel table invalid\n");
4014 bios->digital_min_front_porch = 0x4b;
4018 fptable = &bios->data[bios->fp.fptablepointer];
4019 fptable_ver = fptable[0];
4021 switch (fptable_ver) {
4023 * BMP version 0x5.0x11 BIOSen have version 1 like tables, but no
4024 * version field, and miss one of the spread spectrum/PWM bytes.
4025 * This could affect early GF2Go parts (not seen any appropriate ROMs
4026 * though). Here we assume that a version of 0x05 matches this case
4027 * (combining with a BMP version check would be better), as the
4028 * common case for the panel type field is 0x0005, and that is in
4029 * fact what we are reading the first byte of.
4031 case 0x05: /* some NV10, 11, 15, 16 */
4035 case 0x10: /* some NV15/16, and NV11+ */
4039 case 0x20: /* NV40+ */
4040 headerlen = fptable[1];
4041 recordlen = fptable[2];
4042 fpentries = fptable[3];
4044 * fptable[4] is the minimum
4045 * RAMDAC_FP_HCRTC -> RAMDAC_FP_HSYNC_START gap
4047 bios->digital_min_front_porch = fptable[4];
4052 "FP table revision %d.%d not currently supported\n",
4053 fptable_ver >> 4, fptable_ver & 0xf);
4057 if (!bios->is_mobile) /* !mobile only needs digital_min_front_porch */
4060 ret = parse_lvds_manufacturer_table_header(dev, bios, <h);
4064 if (lth.lvds_ver == 0x30 || lth.lvds_ver == 0x40) {
4065 bios->fp.fpxlatetableptr = bios->fp.lvdsmanufacturerpointer +
4067 bios->fp.xlatwidth = lth.recordlen;
4069 if (bios->fp.fpxlatetableptr == 0x0) {
4070 NV_ERROR(dev, "Pointer to flat panel xlat table invalid\n");
4074 fpstrapping = get_fp_strap(dev, bios);
4076 fpindex = bios->data[bios->fp.fpxlatetableptr +
4077 fpstrapping * bios->fp.xlatwidth];
4079 if (fpindex > fpentries) {
4080 NV_ERROR(dev, "Bad flat panel table index\n");
4084 /* nv4x cards need both a strap value and fpindex of 0xf to use DDC */
4085 if (lth.lvds_ver > 0x10)
4086 bios->fp_no_ddc = fpstrapping != 0xf || fpindex != 0xf;
4089 * If either the strap or xlated fpindex value are 0xf there is no
4090 * panel using a strap-derived bios mode present. this condition
4091 * includes, but is different from, the DDC panel indicator above
4093 if (fpstrapping == 0xf || fpindex == 0xf)
4096 bios->fp.mode_ptr = bios->fp.fptablepointer + headerlen +
4097 recordlen * fpindex + ofs;
4099 NV_TRACE(dev, "BIOS FP mode: %dx%d (%dkHz pixel clock)\n",
4100 ROM16(bios->data[bios->fp.mode_ptr + 11]) + 1,
4101 ROM16(bios->data[bios->fp.mode_ptr + 25]) + 1,
4102 ROM16(bios->data[bios->fp.mode_ptr + 7]) * 10);
4107 bool nouveau_bios_fp_mode(struct drm_device *dev, struct drm_display_mode *mode)
4109 struct drm_nouveau_private *dev_priv = dev->dev_private;
4110 struct nvbios *bios = &dev_priv->vbios;
4111 uint8_t *mode_entry = &bios->data[bios->fp.mode_ptr];
4113 if (!mode) /* just checking whether we can produce a mode */
4114 return bios->fp.mode_ptr;
4116 memset(mode, 0, sizeof(struct drm_display_mode));
4118 * For version 1.0 (version in byte 0):
4119 * bytes 1-2 are "panel type", including bits on whether Colour/mono,
4120 * single/dual link, and type (TFT etc.)
4121 * bytes 3-6 are bits per colour in RGBX
4123 mode->clock = ROM16(mode_entry[7]) * 10;
4124 /* bytes 9-10 is HActive */
4125 mode->hdisplay = ROM16(mode_entry[11]) + 1;
4127 * bytes 13-14 is HValid Start
4128 * bytes 15-16 is HValid End
4130 mode->hsync_start = ROM16(mode_entry[17]) + 1;
4131 mode->hsync_end = ROM16(mode_entry[19]) + 1;
4132 mode->htotal = ROM16(mode_entry[21]) + 1;
4133 /* bytes 23-24, 27-30 similarly, but vertical */
4134 mode->vdisplay = ROM16(mode_entry[25]) + 1;
4135 mode->vsync_start = ROM16(mode_entry[31]) + 1;
4136 mode->vsync_end = ROM16(mode_entry[33]) + 1;
4137 mode->vtotal = ROM16(mode_entry[35]) + 1;
4138 mode->flags |= (mode_entry[37] & 0x10) ?
4139 DRM_MODE_FLAG_PHSYNC : DRM_MODE_FLAG_NHSYNC;
4140 mode->flags |= (mode_entry[37] & 0x1) ?
4141 DRM_MODE_FLAG_PVSYNC : DRM_MODE_FLAG_NVSYNC;
4143 * bytes 38-39 relate to spread spectrum settings
4144 * bytes 40-43 are something to do with PWM
4147 mode->status = MODE_OK;
4148 mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
4149 drm_mode_set_name(mode);
4150 return bios->fp.mode_ptr;
4153 int nouveau_bios_parse_lvds_table(struct drm_device *dev, int pxclk, bool *dl, bool *if_is_24bit)
4156 * The LVDS table header is (mostly) described in
4157 * parse_lvds_manufacturer_table_header(): the BIT header additionally
4158 * contains the dual-link transition pxclk (in 10s kHz), at byte 5 - if
4159 * straps are not being used for the panel, this specifies the frequency
4160 * at which modes should be set up in the dual link style.
4162 * Following the header, the BMP (ver 0xa) table has several records,
4163 * indexed by a separate xlat table, indexed in turn by the fp strap in
4164 * EXTDEV_BOOT. Each record had a config byte, followed by 6 script
4165 * numbers for use by INIT_SUB which controlled panel init and power,
4166 * and finally a dword of ms to sleep between power off and on
4169 * In the BIT versions, the table following the header serves as an
4170 * integrated config and xlat table: the records in the table are
4171 * indexed by the FP strap nibble in EXTDEV_BOOT, and each record has
4172 * two bytes - the first as a config byte, the second for indexing the
4173 * fp mode table pointed to by the BIT 'D' table
4175 * DDC is not used until after card init, so selecting the correct table
4176 * entry and setting the dual link flag for EDID equipped panels,
4177 * requiring tests against the native-mode pixel clock, cannot be done
4178 * until later, when this function should be called with non-zero pxclk
4180 struct drm_nouveau_private *dev_priv = dev->dev_private;
4181 struct nvbios *bios = &dev_priv->vbios;
4182 int fpstrapping = get_fp_strap(dev, bios), lvdsmanufacturerindex = 0;
4183 struct lvdstableheader lth;
4185 int ret, chip_version = bios->chip_version;
4187 ret = parse_lvds_manufacturer_table_header(dev, bios, <h);
4191 switch (lth.lvds_ver) {
4192 case 0x0a: /* pre NV40 */
4193 lvdsmanufacturerindex = bios->data[
4194 bios->fp.fpxlatemanufacturertableptr +
4197 /* we're done if this isn't the EDID panel case */
4201 if (chip_version < 0x25) {
4204 * It seems the old style lvds script pointer is reused
4205 * to select 18/24 bit colour depth for EDID panels.
4207 lvdsmanufacturerindex =
4208 (bios->legacy.lvds_single_a_script_ptr & 1) ?
4210 if (pxclk >= bios->fp.duallink_transition_clk)
4211 lvdsmanufacturerindex++;
4212 } else if (chip_version < 0x30) {
4213 /* nv28 behaviour (off-chip encoder)
4215 * nv28 does a complex dance of first using byte 121 of
4216 * the EDID to choose the lvdsmanufacturerindex, then
4217 * later attempting to match the EDID manufacturer and
4218 * product IDs in a table (signature 'pidt' (panel id
4219 * table?)), setting an lvdsmanufacturerindex of 0 and
4220 * an fp strap of the match index (or 0xf if none)
4222 lvdsmanufacturerindex = 0;
4224 /* nv31, nv34 behaviour */
4225 lvdsmanufacturerindex = 0;
4226 if (pxclk >= bios->fp.duallink_transition_clk)
4227 lvdsmanufacturerindex = 2;
4228 if (pxclk >= 140000)
4229 lvdsmanufacturerindex = 3;
4233 * nvidia set the high nibble of (cr57=f, cr58) to
4234 * lvdsmanufacturerindex in this case; we don't
4237 case 0x30: /* NV4x */
4238 case 0x40: /* G80/G90 */
4239 lvdsmanufacturerindex = fpstrapping;
4242 NV_ERROR(dev, "LVDS table revision not currently supported\n");
4246 lvdsofs = bios->fp.xlated_entry = bios->fp.lvdsmanufacturerpointer + lth.headerlen + lth.recordlen * lvdsmanufacturerindex;
4247 switch (lth.lvds_ver) {
4249 bios->fp.power_off_for_reset = bios->data[lvdsofs] & 1;
4250 bios->fp.reset_after_pclk_change = bios->data[lvdsofs] & 2;
4251 bios->fp.dual_link = bios->data[lvdsofs] & 4;
4252 bios->fp.link_c_increment = bios->data[lvdsofs] & 8;
4253 *if_is_24bit = bios->data[lvdsofs] & 16;
4258 * No sign of the "power off for reset" or "reset for panel
4259 * on" bits, but it's safer to assume we should
4261 bios->fp.power_off_for_reset = true;
4262 bios->fp.reset_after_pclk_change = true;
4265 * It's ok lvdsofs is wrong for nv4x edid case; dual_link is
4266 * over-written, and if_is_24bit isn't used
4268 bios->fp.dual_link = bios->data[lvdsofs] & 1;
4269 bios->fp.if_is_24bit = bios->data[lvdsofs] & 2;
4270 bios->fp.strapless_is_24bit = bios->data[bios->fp.lvdsmanufacturerpointer + 4];
4271 bios->fp.duallink_transition_clk = ROM16(bios->data[bios->fp.lvdsmanufacturerpointer + 5]) * 10;
4275 /* set dual_link flag for EDID case */
4276 if (pxclk && (chip_version < 0x25 || chip_version > 0x28))
4277 bios->fp.dual_link = (pxclk >= bios->fp.duallink_transition_clk);
4279 *dl = bios->fp.dual_link;
4284 /* BIT 'U'/'d' table encoder subtables have hashes matching them to
4285 * a particular set of encoders.
4287 * This function returns true if a particular DCB entry matches.
4290 bios_encoder_match(struct dcb_entry *dcb, u32 hash)
4292 if ((hash & 0x000000f0) != (dcb->location << 4))
4294 if ((hash & 0x0000000f) != dcb->type)
4296 if (!(hash & (dcb->or << 16)))
4299 switch (dcb->type) {
4303 if (hash & 0x00c00000) {
4304 if (!(hash & (dcb->sorconf.link << 22)))
4313 nouveau_bios_run_display_table(struct drm_device *dev, u16 type, int pclk,
4314 struct dcb_entry *dcbent, int crtc)
4317 * The display script table is located by the BIT 'U' table.
4319 * It contains an array of pointers to various tables describing
4320 * a particular output type. The first 32-bits of the output
4321 * tables contains similar information to a DCB entry, and is
4322 * used to decide whether that particular table is suitable for
4323 * the output you want to access.
4325 * The "record header length" field here seems to indicate the
4326 * offset of the first configuration entry in the output tables.
4327 * This is 10 on most cards I've seen, but 12 has been witnessed
4328 * on DP cards, and there's another script pointer within the
4331 * offset + 0 ( 8 bits): version
4332 * offset + 1 ( 8 bits): header length
4333 * offset + 2 ( 8 bits): record length
4334 * offset + 3 ( 8 bits): number of records
4335 * offset + 4 ( 8 bits): record header length
4336 * offset + 5 (16 bits): pointer to first output script table
4339 struct drm_nouveau_private *dev_priv = dev->dev_private;
4340 struct nvbios *bios = &dev_priv->vbios;
4341 uint8_t *table = &bios->data[bios->display.script_table_ptr];
4342 uint8_t *otable = NULL;
4346 if (!bios->display.script_table_ptr) {
4347 NV_ERROR(dev, "No pointer to output script table\n");
4352 * Nothing useful has been in any of the pre-2.0 tables I've seen,
4353 * so until they are, we really don't need to care.
4355 if (table[0] < 0x20)
4358 if (table[0] != 0x20 && table[0] != 0x21) {
4359 NV_ERROR(dev, "Output script table version 0x%02x unknown\n",
4365 * The output script tables describing a particular output type
4368 * offset + 0 (32 bits): output this table matches (hash of DCB)
4369 * offset + 4 ( 8 bits): unknown
4370 * offset + 5 ( 8 bits): number of configurations
4371 * offset + 6 (16 bits): pointer to some script
4372 * offset + 8 (16 bits): pointer to some script
4375 * offset + 10 : configuration 0
4378 * offset + 10 : pointer to some script
4379 * offset + 12 : configuration 0
4381 * Each config entry is as follows:
4383 * offset + 0 (16 bits): unknown, assumed to be a match value
4384 * offset + 2 (16 bits): pointer to script table (clock set?)
4385 * offset + 4 (16 bits): pointer to script table (reset?)
4387 * There doesn't appear to be a count value to say how many
4388 * entries exist in each script table, instead, a 0 value in
4389 * the first 16-bit word seems to indicate both the end of the
4390 * list and the default entry. The second 16-bit word in the
4391 * script tables is a pointer to the script to execute.
4394 NV_DEBUG_KMS(dev, "Searching for output entry for %d %d %d\n",
4395 dcbent->type, dcbent->location, dcbent->or);
4396 for (i = 0; i < table[3]; i++) {
4397 otable = ROMPTR(dev, table[table[1] + (i * table[2])]);
4398 if (otable && bios_encoder_match(dcbent, ROM32(otable[0])))
4403 NV_DEBUG_KMS(dev, "failed to match any output table\n");
4407 if (pclk < -2 || pclk > 0) {
4408 /* Try to find matching script table entry */
4409 for (i = 0; i < otable[5]; i++) {
4410 if (ROM16(otable[table[4] + i*6]) == type)
4414 if (i == otable[5]) {
4415 NV_ERROR(dev, "Table 0x%04x not found for %d/%d, "
4417 type, dcbent->type, dcbent->or);
4423 script = ROM16(otable[6]);
4425 NV_DEBUG_KMS(dev, "output script 0 not found\n");
4429 NV_DEBUG_KMS(dev, "0x%04X: parsing output script 0\n", script);
4430 nouveau_bios_run_init_table(dev, script, dcbent, crtc);
4433 script = ROM16(otable[8]);
4435 NV_DEBUG_KMS(dev, "output script 1 not found\n");
4439 NV_DEBUG_KMS(dev, "0x%04X: parsing output script 1\n", script);
4440 nouveau_bios_run_init_table(dev, script, dcbent, crtc);
4444 script = ROM16(otable[10]);
4448 NV_DEBUG_KMS(dev, "output script 2 not found\n");
4452 NV_DEBUG_KMS(dev, "0x%04X: parsing output script 2\n", script);
4453 nouveau_bios_run_init_table(dev, script, dcbent, crtc);
4456 script = ROM16(otable[table[4] + i*6 + 2]);
4458 script = clkcmptable(bios, script, pclk);
4460 NV_DEBUG_KMS(dev, "clock script 0 not found\n");
4464 NV_DEBUG_KMS(dev, "0x%04X: parsing clock script 0\n", script);
4465 nouveau_bios_run_init_table(dev, script, dcbent, crtc);
4468 script = ROM16(otable[table[4] + i*6 + 4]);
4470 script = clkcmptable(bios, script, -pclk);
4472 NV_DEBUG_KMS(dev, "clock script 1 not found\n");
4476 NV_DEBUG_KMS(dev, "0x%04X: parsing clock script 1\n", script);
4477 nouveau_bios_run_init_table(dev, script, dcbent, crtc);
4484 int run_tmds_table(struct drm_device *dev, struct dcb_entry *dcbent, int head, int pxclk)
4487 * the pxclk parameter is in kHz
4489 * This runs the TMDS regs setting code found on BIT bios cards
4491 * For ffs(or) == 1 use the first table, for ffs(or) == 2 and
4492 * ffs(or) == 3, use the second.
4495 struct drm_nouveau_private *dev_priv = dev->dev_private;
4496 struct nvbios *bios = &dev_priv->vbios;
4497 int cv = bios->chip_version;
4498 uint16_t clktable = 0, scriptptr;
4499 uint32_t sel_clk_binding, sel_clk;
4501 /* pre-nv17 off-chip tmds uses scripts, post nv17 doesn't */
4502 if (cv >= 0x17 && cv != 0x1a && cv != 0x20 &&
4503 dcbent->location != DCB_LOC_ON_CHIP)
4506 switch (ffs(dcbent->or)) {
4508 clktable = bios->tmds.output0_script_ptr;
4512 clktable = bios->tmds.output1_script_ptr;
4517 NV_ERROR(dev, "Pixel clock comparison table not found\n");
4521 scriptptr = clkcmptable(bios, clktable, pxclk);
4524 NV_ERROR(dev, "TMDS output init script not found\n");
4528 /* don't let script change pll->head binding */
4529 sel_clk_binding = bios_rd32(bios, NV_PRAMDAC_SEL_CLK) & 0x50000;
4530 run_digital_op_script(dev, scriptptr, dcbent, head, pxclk >= 165000);
4531 sel_clk = NVReadRAMDAC(dev, 0, NV_PRAMDAC_SEL_CLK) & ~0x50000;
4532 NVWriteRAMDAC(dev, 0, NV_PRAMDAC_SEL_CLK, sel_clk | sel_clk_binding);
4537 struct pll_mapping {
4542 static struct pll_mapping nv04_pll_mapping[] = {
4543 { PLL_CORE , NV_PRAMDAC_NVPLL_COEFF },
4544 { PLL_MEMORY, NV_PRAMDAC_MPLL_COEFF },
4545 { PLL_VPLL0 , NV_PRAMDAC_VPLL_COEFF },
4546 { PLL_VPLL1 , NV_RAMDAC_VPLL2 },
4550 static struct pll_mapping nv40_pll_mapping[] = {
4551 { PLL_CORE , 0x004000 },
4552 { PLL_MEMORY, 0x004020 },
4553 { PLL_VPLL0 , NV_PRAMDAC_VPLL_COEFF },
4554 { PLL_VPLL1 , NV_RAMDAC_VPLL2 },
4558 static struct pll_mapping nv50_pll_mapping[] = {
4559 { PLL_CORE , 0x004028 },
4560 { PLL_SHADER, 0x004020 },
4561 { PLL_UNK03 , 0x004000 },
4562 { PLL_MEMORY, 0x004008 },
4563 { PLL_UNK40 , 0x00e810 },
4564 { PLL_UNK41 , 0x00e818 },
4565 { PLL_UNK42 , 0x00e824 },
4566 { PLL_VPLL0 , 0x614100 },
4567 { PLL_VPLL1 , 0x614900 },
4571 static struct pll_mapping nv84_pll_mapping[] = {
4572 { PLL_CORE , 0x004028 },
4573 { PLL_SHADER, 0x004020 },
4574 { PLL_MEMORY, 0x004008 },
4575 { PLL_VDEC , 0x004030 },
4576 { PLL_UNK41 , 0x00e818 },
4577 { PLL_VPLL0 , 0x614100 },
4578 { PLL_VPLL1 , 0x614900 },
4583 get_pll_register(struct drm_device *dev, enum pll_types type)
4585 struct drm_nouveau_private *dev_priv = dev->dev_private;
4586 struct nvbios *bios = &dev_priv->vbios;
4587 struct pll_mapping *map;
4590 if (dev_priv->card_type < NV_40)
4591 map = nv04_pll_mapping;
4593 if (dev_priv->card_type < NV_50)
4594 map = nv40_pll_mapping;
4596 u8 *plim = &bios->data[bios->pll_limit_tbl_ptr];
4598 if (plim[0] >= 0x30) {
4599 u8 *entry = plim + plim[1];
4600 for (i = 0; i < plim[3]; i++, entry += plim[2]) {
4601 if (entry[0] == type)
4602 return ROM32(entry[3]);
4608 if (dev_priv->chipset == 0x50)
4609 map = nv50_pll_mapping;
4611 map = nv84_pll_mapping;
4615 if (map->type == type)
4623 int get_pll_limits(struct drm_device *dev, uint32_t limit_match, struct pll_lims *pll_lim)
4628 * Version 0x10: NV30, NV31
4629 * One byte header (version), one record of 24 bytes
4630 * Version 0x11: NV36 - Not implemented
4631 * Seems to have same record style as 0x10, but 3 records rather than 1
4632 * Version 0x20: Found on Geforce 6 cards
4633 * Trivial 4 byte BIT header. 31 (0x1f) byte record length
4634 * Version 0x21: Found on Geforce 7, 8 and some Geforce 6 cards
4635 * 5 byte header, fifth byte of unknown purpose. 35 (0x23) byte record
4636 * length in general, some (integrated) have an extra configuration byte
4637 * Version 0x30: Found on Geforce 8, separates the register mapping
4638 * from the limits tables.
4641 struct drm_nouveau_private *dev_priv = dev->dev_private;
4642 struct nvbios *bios = &dev_priv->vbios;
4643 int cv = bios->chip_version, pllindex = 0;
4644 uint8_t pll_lim_ver = 0, headerlen = 0, recordlen = 0, entries = 0;
4645 uint32_t crystal_strap_mask, crystal_straps;
4647 if (!bios->pll_limit_tbl_ptr) {
4648 if (cv == 0x30 || cv == 0x31 || cv == 0x35 || cv == 0x36 ||
4650 NV_ERROR(dev, "Pointer to PLL limits table invalid\n");
4654 pll_lim_ver = bios->data[bios->pll_limit_tbl_ptr];
4656 crystal_strap_mask = 1 << 6;
4657 /* open coded dev->twoHeads test */
4658 if (cv > 0x10 && cv != 0x15 && cv != 0x1a && cv != 0x20)
4659 crystal_strap_mask |= 1 << 22;
4660 crystal_straps = nvReadEXTDEV(dev, NV_PEXTDEV_BOOT_0) &
4663 switch (pll_lim_ver) {
4665 * We use version 0 to indicate a pre limit table bios (single stage
4666 * pll) and load the hard coded limits instead.
4673 * Strictly v0x11 has 3 entries, but the last two don't seem
4685 headerlen = bios->data[bios->pll_limit_tbl_ptr + 1];
4686 recordlen = bios->data[bios->pll_limit_tbl_ptr + 2];
4687 entries = bios->data[bios->pll_limit_tbl_ptr + 3];
4690 NV_ERROR(dev, "PLL limits table revision 0x%X not currently "
4691 "supported\n", pll_lim_ver);
4695 /* initialize all members to zero */
4696 memset(pll_lim, 0, sizeof(struct pll_lims));
4698 /* if we were passed a type rather than a register, figure
4699 * out the register and store it
4701 if (limit_match > PLL_MAX)
4702 pll_lim->reg = limit_match;
4704 pll_lim->reg = get_pll_register(dev, limit_match);
4709 if (pll_lim_ver == 0x10 || pll_lim_ver == 0x11) {
4710 uint8_t *pll_rec = &bios->data[bios->pll_limit_tbl_ptr + headerlen + recordlen * pllindex];
4712 pll_lim->vco1.minfreq = ROM32(pll_rec[0]);
4713 pll_lim->vco1.maxfreq = ROM32(pll_rec[4]);
4714 pll_lim->vco2.minfreq = ROM32(pll_rec[8]);
4715 pll_lim->vco2.maxfreq = ROM32(pll_rec[12]);
4716 pll_lim->vco1.min_inputfreq = ROM32(pll_rec[16]);
4717 pll_lim->vco2.min_inputfreq = ROM32(pll_rec[20]);
4718 pll_lim->vco1.max_inputfreq = pll_lim->vco2.max_inputfreq = INT_MAX;
4720 /* these values taken from nv30/31/36 */
4721 pll_lim->vco1.min_n = 0x1;
4723 pll_lim->vco1.min_n = 0x5;
4724 pll_lim->vco1.max_n = 0xff;
4725 pll_lim->vco1.min_m = 0x1;
4726 pll_lim->vco1.max_m = 0xd;
4727 pll_lim->vco2.min_n = 0x4;
4729 * On nv30, 31, 36 (i.e. all cards with two stage PLLs with this
4730 * table version (apart from nv35)), N2 is compared to
4731 * maxN2 (0x46) and 10 * maxM2 (0x4), so set maxN2 to 0x28 and
4734 pll_lim->vco2.max_n = 0x28;
4735 if (cv == 0x30 || cv == 0x35)
4736 /* only 5 bits available for N2 on nv30/35 */
4737 pll_lim->vco2.max_n = 0x1f;
4738 pll_lim->vco2.min_m = 0x1;
4739 pll_lim->vco2.max_m = 0x4;
4740 pll_lim->max_log2p = 0x7;
4741 pll_lim->max_usable_log2p = 0x6;
4742 } else if (pll_lim_ver == 0x20 || pll_lim_ver == 0x21) {
4743 uint16_t plloffs = bios->pll_limit_tbl_ptr + headerlen;
4748 * First entry is default match, if nothing better. warn if
4751 if (ROM32(bios->data[plloffs]))
4752 NV_WARN(dev, "Default PLL limit entry has non-zero "
4753 "register field\n");
4755 for (i = 1; i < entries; i++)
4756 if (ROM32(bios->data[plloffs + recordlen * i]) == pll_lim->reg) {
4761 if ((dev_priv->card_type >= NV_50) && (pllindex == 0)) {
4762 NV_ERROR(dev, "Register 0x%08x not found in PLL "
4763 "limits table", pll_lim->reg);
4767 pll_rec = &bios->data[plloffs + recordlen * pllindex];
4769 BIOSLOG(bios, "Loading PLL limits for reg 0x%08x\n",
4770 pllindex ? pll_lim->reg : 0);
4773 * Frequencies are stored in tables in MHz, kHz are more
4774 * useful, so we convert.
4777 /* What output frequencies can each VCO generate? */
4778 pll_lim->vco1.minfreq = ROM16(pll_rec[4]) * 1000;
4779 pll_lim->vco1.maxfreq = ROM16(pll_rec[6]) * 1000;
4780 pll_lim->vco2.minfreq = ROM16(pll_rec[8]) * 1000;
4781 pll_lim->vco2.maxfreq = ROM16(pll_rec[10]) * 1000;
4783 /* What input frequencies they accept (past the m-divider)? */
4784 pll_lim->vco1.min_inputfreq = ROM16(pll_rec[12]) * 1000;
4785 pll_lim->vco2.min_inputfreq = ROM16(pll_rec[14]) * 1000;
4786 pll_lim->vco1.max_inputfreq = ROM16(pll_rec[16]) * 1000;
4787 pll_lim->vco2.max_inputfreq = ROM16(pll_rec[18]) * 1000;
4789 /* What values are accepted as multiplier and divider? */
4790 pll_lim->vco1.min_n = pll_rec[20];
4791 pll_lim->vco1.max_n = pll_rec[21];
4792 pll_lim->vco1.min_m = pll_rec[22];
4793 pll_lim->vco1.max_m = pll_rec[23];
4794 pll_lim->vco2.min_n = pll_rec[24];
4795 pll_lim->vco2.max_n = pll_rec[25];
4796 pll_lim->vco2.min_m = pll_rec[26];
4797 pll_lim->vco2.max_m = pll_rec[27];
4799 pll_lim->max_usable_log2p = pll_lim->max_log2p = pll_rec[29];
4800 if (pll_lim->max_log2p > 0x7)
4801 /* pll decoding in nv_hw.c assumes never > 7 */
4802 NV_WARN(dev, "Max log2 P value greater than 7 (%d)\n",
4803 pll_lim->max_log2p);
4805 pll_lim->max_usable_log2p = 0x6;
4806 pll_lim->log2p_bias = pll_rec[30];
4808 if (recordlen > 0x22)
4809 pll_lim->refclk = ROM32(pll_rec[31]);
4811 if (recordlen > 0x23 && pll_rec[35])
4813 "Bits set in PLL configuration byte (%x)\n",
4816 /* C51 special not seen elsewhere */
4817 if (cv == 0x51 && !pll_lim->refclk) {
4818 uint32_t sel_clk = bios_rd32(bios, NV_PRAMDAC_SEL_CLK);
4820 if ((pll_lim->reg == NV_PRAMDAC_VPLL_COEFF && sel_clk & 0x20) ||
4821 (pll_lim->reg == NV_RAMDAC_VPLL2 && sel_clk & 0x80)) {
4822 if (bios_idxprt_rd(bios, NV_CIO_CRX__COLOR, NV_CIO_CRE_CHIP_ID_INDEX) < 0xa3)
4823 pll_lim->refclk = 200000;
4825 pll_lim->refclk = 25000;
4828 } else if (pll_lim_ver == 0x30) { /* ver 0x30 */
4829 uint8_t *entry = &bios->data[bios->pll_limit_tbl_ptr + headerlen];
4830 uint8_t *record = NULL;
4833 BIOSLOG(bios, "Loading PLL limits for register 0x%08x\n",
4836 for (i = 0; i < entries; i++, entry += recordlen) {
4837 if (ROM32(entry[3]) == pll_lim->reg) {
4838 record = &bios->data[ROM16(entry[1])];
4844 NV_ERROR(dev, "Register 0x%08x not found in PLL "
4845 "limits table", pll_lim->reg);
4849 pll_lim->vco1.minfreq = ROM16(record[0]) * 1000;
4850 pll_lim->vco1.maxfreq = ROM16(record[2]) * 1000;
4851 pll_lim->vco2.minfreq = ROM16(record[4]) * 1000;
4852 pll_lim->vco2.maxfreq = ROM16(record[6]) * 1000;
4853 pll_lim->vco1.min_inputfreq = ROM16(record[8]) * 1000;
4854 pll_lim->vco2.min_inputfreq = ROM16(record[10]) * 1000;
4855 pll_lim->vco1.max_inputfreq = ROM16(record[12]) * 1000;
4856 pll_lim->vco2.max_inputfreq = ROM16(record[14]) * 1000;
4857 pll_lim->vco1.min_n = record[16];
4858 pll_lim->vco1.max_n = record[17];
4859 pll_lim->vco1.min_m = record[18];
4860 pll_lim->vco1.max_m = record[19];
4861 pll_lim->vco2.min_n = record[20];
4862 pll_lim->vco2.max_n = record[21];
4863 pll_lim->vco2.min_m = record[22];
4864 pll_lim->vco2.max_m = record[23];
4865 pll_lim->max_usable_log2p = pll_lim->max_log2p = record[25];
4866 pll_lim->log2p_bias = record[27];
4867 pll_lim->refclk = ROM32(record[28]);
4868 } else if (pll_lim_ver) { /* ver 0x40 */
4869 uint8_t *entry = &bios->data[bios->pll_limit_tbl_ptr + headerlen];
4870 uint8_t *record = NULL;
4873 BIOSLOG(bios, "Loading PLL limits for register 0x%08x\n",
4876 for (i = 0; i < entries; i++, entry += recordlen) {
4877 if (ROM32(entry[3]) == pll_lim->reg) {
4878 record = &bios->data[ROM16(entry[1])];
4884 NV_ERROR(dev, "Register 0x%08x not found in PLL "
4885 "limits table", pll_lim->reg);
4889 pll_lim->vco1.minfreq = ROM16(record[0]) * 1000;
4890 pll_lim->vco1.maxfreq = ROM16(record[2]) * 1000;
4891 pll_lim->vco1.min_inputfreq = ROM16(record[4]) * 1000;
4892 pll_lim->vco1.max_inputfreq = ROM16(record[6]) * 1000;
4893 pll_lim->vco1.min_m = record[8];
4894 pll_lim->vco1.max_m = record[9];
4895 pll_lim->vco1.min_n = record[10];
4896 pll_lim->vco1.max_n = record[11];
4897 pll_lim->min_p = record[12];
4898 pll_lim->max_p = record[13];
4899 pll_lim->refclk = ROM16(entry[9]) * 1000;
4903 * By now any valid limit table ought to have set a max frequency for
4904 * vco1, so if it's zero it's either a pre limit table bios, or one
4905 * with an empty limit table (seen on nv18)
4907 if (!pll_lim->vco1.maxfreq) {
4908 pll_lim->vco1.minfreq = bios->fminvco;
4909 pll_lim->vco1.maxfreq = bios->fmaxvco;
4910 pll_lim->vco1.min_inputfreq = 0;
4911 pll_lim->vco1.max_inputfreq = INT_MAX;
4912 pll_lim->vco1.min_n = 0x1;
4913 pll_lim->vco1.max_n = 0xff;
4914 pll_lim->vco1.min_m = 0x1;
4915 if (crystal_straps == 0) {
4916 /* nv05 does this, nv11 doesn't, nv10 unknown */
4918 pll_lim->vco1.min_m = 0x7;
4919 pll_lim->vco1.max_m = 0xd;
4922 pll_lim->vco1.min_m = 0x8;
4923 pll_lim->vco1.max_m = 0xe;
4925 if (cv < 0x17 || cv == 0x1a || cv == 0x20)
4926 pll_lim->max_log2p = 4;
4928 pll_lim->max_log2p = 5;
4929 pll_lim->max_usable_log2p = pll_lim->max_log2p;
4932 if (!pll_lim->refclk)
4933 switch (crystal_straps) {
4935 pll_lim->refclk = 13500;
4938 pll_lim->refclk = 14318;
4941 pll_lim->refclk = 27000;
4943 case (1 << 22 | 1 << 6):
4944 pll_lim->refclk = 25000;
4948 NV_DEBUG(dev, "pll.vco1.minfreq: %d\n", pll_lim->vco1.minfreq);
4949 NV_DEBUG(dev, "pll.vco1.maxfreq: %d\n", pll_lim->vco1.maxfreq);
4950 NV_DEBUG(dev, "pll.vco1.min_inputfreq: %d\n", pll_lim->vco1.min_inputfreq);
4951 NV_DEBUG(dev, "pll.vco1.max_inputfreq: %d\n", pll_lim->vco1.max_inputfreq);
4952 NV_DEBUG(dev, "pll.vco1.min_n: %d\n", pll_lim->vco1.min_n);
4953 NV_DEBUG(dev, "pll.vco1.max_n: %d\n", pll_lim->vco1.max_n);
4954 NV_DEBUG(dev, "pll.vco1.min_m: %d\n", pll_lim->vco1.min_m);
4955 NV_DEBUG(dev, "pll.vco1.max_m: %d\n", pll_lim->vco1.max_m);
4956 if (pll_lim->vco2.maxfreq) {
4957 NV_DEBUG(dev, "pll.vco2.minfreq: %d\n", pll_lim->vco2.minfreq);
4958 NV_DEBUG(dev, "pll.vco2.maxfreq: %d\n", pll_lim->vco2.maxfreq);
4959 NV_DEBUG(dev, "pll.vco2.min_inputfreq: %d\n", pll_lim->vco2.min_inputfreq);
4960 NV_DEBUG(dev, "pll.vco2.max_inputfreq: %d\n", pll_lim->vco2.max_inputfreq);
4961 NV_DEBUG(dev, "pll.vco2.min_n: %d\n", pll_lim->vco2.min_n);
4962 NV_DEBUG(dev, "pll.vco2.max_n: %d\n", pll_lim->vco2.max_n);
4963 NV_DEBUG(dev, "pll.vco2.min_m: %d\n", pll_lim->vco2.min_m);
4964 NV_DEBUG(dev, "pll.vco2.max_m: %d\n", pll_lim->vco2.max_m);
4966 if (!pll_lim->max_p) {
4967 NV_DEBUG(dev, "pll.max_log2p: %d\n", pll_lim->max_log2p);
4968 NV_DEBUG(dev, "pll.log2p_bias: %d\n", pll_lim->log2p_bias);
4970 NV_DEBUG(dev, "pll.min_p: %d\n", pll_lim->min_p);
4971 NV_DEBUG(dev, "pll.max_p: %d\n", pll_lim->max_p);
4973 NV_DEBUG(dev, "pll.refclk: %d\n", pll_lim->refclk);
4978 static void parse_bios_version(struct drm_device *dev, struct nvbios *bios, uint16_t offset)
4981 * offset + 0 (8 bits): Micro version
4982 * offset + 1 (8 bits): Minor version
4983 * offset + 2 (8 bits): Chip version
4984 * offset + 3 (8 bits): Major version
4987 bios->major_version = bios->data[offset + 3];
4988 bios->chip_version = bios->data[offset + 2];
4989 NV_TRACE(dev, "Bios version %02x.%02x.%02x.%02x\n",
4990 bios->data[offset + 3], bios->data[offset + 2],
4991 bios->data[offset + 1], bios->data[offset]);
4994 static void parse_script_table_pointers(struct nvbios *bios, uint16_t offset)
4997 * Parses the init table segment for pointers used in script execution.
4999 * offset + 0 (16 bits): init script tables pointer
5000 * offset + 2 (16 bits): macro index table pointer
5001 * offset + 4 (16 bits): macro table pointer
5002 * offset + 6 (16 bits): condition table pointer
5003 * offset + 8 (16 bits): io condition table pointer
5004 * offset + 10 (16 bits): io flag condition table pointer
5005 * offset + 12 (16 bits): init function table pointer
5008 bios->init_script_tbls_ptr = ROM16(bios->data[offset]);
5009 bios->macro_index_tbl_ptr = ROM16(bios->data[offset + 2]);
5010 bios->macro_tbl_ptr = ROM16(bios->data[offset + 4]);
5011 bios->condition_tbl_ptr = ROM16(bios->data[offset + 6]);
5012 bios->io_condition_tbl_ptr = ROM16(bios->data[offset + 8]);
5013 bios->io_flag_condition_tbl_ptr = ROM16(bios->data[offset + 10]);
5014 bios->init_function_tbl_ptr = ROM16(bios->data[offset + 12]);
5017 static int parse_bit_A_tbl_entry(struct drm_device *dev, struct nvbios *bios, struct bit_entry *bitentry)
5020 * Parses the load detect values for g80 cards.
5022 * offset + 0 (16 bits): loadval table pointer
5025 uint16_t load_table_ptr;
5026 uint8_t version, headerlen, entrylen, num_entries;
5028 if (bitentry->length != 3) {
5029 NV_ERROR(dev, "Do not understand BIT A table\n");
5033 load_table_ptr = ROM16(bios->data[bitentry->offset]);
5035 if (load_table_ptr == 0x0) {
5036 NV_DEBUG(dev, "Pointer to BIT loadval table invalid\n");
5040 version = bios->data[load_table_ptr];
5042 if (version != 0x10) {
5043 NV_ERROR(dev, "BIT loadval table version %d.%d not supported\n",
5044 version >> 4, version & 0xF);
5048 headerlen = bios->data[load_table_ptr + 1];
5049 entrylen = bios->data[load_table_ptr + 2];
5050 num_entries = bios->data[load_table_ptr + 3];
5052 if (headerlen != 4 || entrylen != 4 || num_entries != 2) {
5053 NV_ERROR(dev, "Do not understand BIT loadval table\n");
5057 /* First entry is normal dac, 2nd tv-out perhaps? */
5058 bios->dactestval = ROM32(bios->data[load_table_ptr + headerlen]) & 0x3ff;
5063 static int parse_bit_C_tbl_entry(struct drm_device *dev, struct nvbios *bios, struct bit_entry *bitentry)
5066 * offset + 8 (16 bits): PLL limits table pointer
5068 * There's more in here, but that's unknown.
5071 if (bitentry->length < 10) {
5072 NV_ERROR(dev, "Do not understand BIT C table\n");
5076 bios->pll_limit_tbl_ptr = ROM16(bios->data[bitentry->offset + 8]);
5081 static int parse_bit_display_tbl_entry(struct drm_device *dev, struct nvbios *bios, struct bit_entry *bitentry)
5084 * Parses the flat panel table segment that the bit entry points to.
5085 * Starting at bitentry->offset:
5087 * offset + 0 (16 bits): ??? table pointer - seems to have 18 byte
5088 * records beginning with a freq.
5089 * offset + 2 (16 bits): mode table pointer
5092 if (bitentry->length != 4) {
5093 NV_ERROR(dev, "Do not understand BIT display table\n");
5097 bios->fp.fptablepointer = ROM16(bios->data[bitentry->offset + 2]);
5102 static int parse_bit_init_tbl_entry(struct drm_device *dev, struct nvbios *bios, struct bit_entry *bitentry)
5105 * Parses the init table segment that the bit entry points to.
5107 * See parse_script_table_pointers for layout
5110 if (bitentry->length < 14) {
5111 NV_ERROR(dev, "Do not understand init table\n");
5115 parse_script_table_pointers(bios, bitentry->offset);
5117 if (bitentry->length >= 16)
5118 bios->some_script_ptr = ROM16(bios->data[bitentry->offset + 14]);
5119 if (bitentry->length >= 18)
5120 bios->init96_tbl_ptr = ROM16(bios->data[bitentry->offset + 16]);
5125 static int parse_bit_i_tbl_entry(struct drm_device *dev, struct nvbios *bios, struct bit_entry *bitentry)
5128 * BIT 'i' (info?) table
5130 * offset + 0 (32 bits): BIOS version dword (as in B table)
5131 * offset + 5 (8 bits): BIOS feature byte (same as for BMP?)
5132 * offset + 13 (16 bits): pointer to table containing DAC load
5133 * detection comparison values
5135 * There's other things in the table, purpose unknown
5138 uint16_t daccmpoffset;
5139 uint8_t dacver, dacheaderlen;
5141 if (bitentry->length < 6) {
5142 NV_ERROR(dev, "BIT i table too short for needed information\n");
5146 parse_bios_version(dev, bios, bitentry->offset);
5149 * bit 4 seems to indicate a mobile bios (doesn't suffer from BMP's
5150 * Quadro identity crisis), other bits possibly as for BMP feature byte
5152 bios->feature_byte = bios->data[bitentry->offset + 5];
5153 bios->is_mobile = bios->feature_byte & FEATURE_MOBILE;
5155 if (bitentry->length < 15) {
5156 NV_WARN(dev, "BIT i table not long enough for DAC load "
5157 "detection comparison table\n");
5161 daccmpoffset = ROM16(bios->data[bitentry->offset + 13]);
5163 /* doesn't exist on g80 */
5168 * The first value in the table, following the header, is the
5169 * comparison value, the second entry is a comparison value for
5170 * TV load detection.
5173 dacver = bios->data[daccmpoffset];
5174 dacheaderlen = bios->data[daccmpoffset + 1];
5176 if (dacver != 0x00 && dacver != 0x10) {
5177 NV_WARN(dev, "DAC load detection comparison table version "
5178 "%d.%d not known\n", dacver >> 4, dacver & 0xf);
5182 bios->dactestval = ROM32(bios->data[daccmpoffset + dacheaderlen]);
5183 bios->tvdactestval = ROM32(bios->data[daccmpoffset + dacheaderlen + 4]);
5188 static int parse_bit_lvds_tbl_entry(struct drm_device *dev, struct nvbios *bios, struct bit_entry *bitentry)
5191 * Parses the LVDS table segment that the bit entry points to.
5192 * Starting at bitentry->offset:
5194 * offset + 0 (16 bits): LVDS strap xlate table pointer
5197 if (bitentry->length != 2) {
5198 NV_ERROR(dev, "Do not understand BIT LVDS table\n");
5203 * No idea if it's still called the LVDS manufacturer table, but
5204 * the concept's close enough.
5206 bios->fp.lvdsmanufacturerpointer = ROM16(bios->data[bitentry->offset]);
5212 parse_bit_M_tbl_entry(struct drm_device *dev, struct nvbios *bios,
5213 struct bit_entry *bitentry)
5216 * offset + 2 (8 bits): number of options in an
5217 * INIT_RAM_RESTRICT_ZM_REG_GROUP opcode option set
5218 * offset + 3 (16 bits): pointer to strap xlate table for RAM
5219 * restrict option selection
5221 * There's a bunch of bits in this table other than the RAM restrict
5222 * stuff that we don't use - their use currently unknown
5226 * Older bios versions don't have a sufficiently long table for
5229 if (bitentry->length < 0x5)
5232 if (bitentry->version < 2) {
5233 bios->ram_restrict_group_count = bios->data[bitentry->offset + 2];
5234 bios->ram_restrict_tbl_ptr = ROM16(bios->data[bitentry->offset + 3]);
5236 bios->ram_restrict_group_count = bios->data[bitentry->offset + 0];
5237 bios->ram_restrict_tbl_ptr = ROM16(bios->data[bitentry->offset + 1]);
5243 static int parse_bit_tmds_tbl_entry(struct drm_device *dev, struct nvbios *bios, struct bit_entry *bitentry)
5246 * Parses the pointer to the TMDS table
5248 * Starting at bitentry->offset:
5250 * offset + 0 (16 bits): TMDS table pointer
5252 * The TMDS table is typically found just before the DCB table, with a
5253 * characteristic signature of 0x11,0x13 (1.1 being version, 0x13 being
5256 * At offset +7 is a pointer to a script, which I don't know how to
5258 * At offset +9 is a pointer to another script, likewise
5259 * Offset +11 has a pointer to a table where the first word is a pxclk
5260 * frequency and the second word a pointer to a script, which should be
5261 * run if the comparison pxclk frequency is less than the pxclk desired.
5262 * This repeats for decreasing comparison frequencies
5263 * Offset +13 has a pointer to a similar table
5264 * The selection of table (and possibly +7/+9 script) is dictated by
5265 * "or" from the DCB.
5268 uint16_t tmdstableptr, script1, script2;
5270 if (bitentry->length != 2) {
5271 NV_ERROR(dev, "Do not understand BIT TMDS table\n");
5275 tmdstableptr = ROM16(bios->data[bitentry->offset]);
5276 if (!tmdstableptr) {
5277 NV_ERROR(dev, "Pointer to TMDS table invalid\n");
5281 NV_INFO(dev, "TMDS table version %d.%d\n",
5282 bios->data[tmdstableptr] >> 4, bios->data[tmdstableptr] & 0xf);
5284 /* nv50+ has v2.0, but we don't parse it atm */
5285 if (bios->data[tmdstableptr] != 0x11)
5289 * These two scripts are odd: they don't seem to get run even when
5290 * they are not stubbed.
5292 script1 = ROM16(bios->data[tmdstableptr + 7]);
5293 script2 = ROM16(bios->data[tmdstableptr + 9]);
5294 if (bios->data[script1] != 'q' || bios->data[script2] != 'q')
5295 NV_WARN(dev, "TMDS table script pointers not stubbed\n");
5297 bios->tmds.output0_script_ptr = ROM16(bios->data[tmdstableptr + 11]);
5298 bios->tmds.output1_script_ptr = ROM16(bios->data[tmdstableptr + 13]);
5304 parse_bit_U_tbl_entry(struct drm_device *dev, struct nvbios *bios,
5305 struct bit_entry *bitentry)
5308 * Parses the pointer to the G80 output script tables
5310 * Starting at bitentry->offset:
5312 * offset + 0 (16 bits): output script table pointer
5315 uint16_t outputscripttableptr;
5317 if (bitentry->length != 3) {
5318 NV_ERROR(dev, "Do not understand BIT U table\n");
5322 outputscripttableptr = ROM16(bios->data[bitentry->offset]);
5323 bios->display.script_table_ptr = outputscripttableptr;
5329 int (* const parse_fn)(struct drm_device *, struct nvbios *, struct bit_entry *);
5332 #define BIT_TABLE(id, funcid) ((struct bit_table){ id, parse_bit_##funcid##_tbl_entry })
5335 bit_table(struct drm_device *dev, u8 id, struct bit_entry *bit)
5337 struct drm_nouveau_private *dev_priv = dev->dev_private;
5338 struct nvbios *bios = &dev_priv->vbios;
5341 if (bios->type != NVBIOS_BIT)
5344 entries = bios->data[bios->offset + 10];
5345 entry = &bios->data[bios->offset + 12];
5347 if (entry[0] == id) {
5349 bit->version = entry[1];
5350 bit->length = ROM16(entry[2]);
5351 bit->offset = ROM16(entry[4]);
5352 bit->data = ROMPTR(dev, entry[4]);
5356 entry += bios->data[bios->offset + 9];
5363 parse_bit_table(struct nvbios *bios, const uint16_t bitoffset,
5364 struct bit_table *table)
5366 struct drm_device *dev = bios->dev;
5367 struct bit_entry bitentry;
5369 if (bit_table(dev, table->id, &bitentry) == 0)
5370 return table->parse_fn(dev, bios, &bitentry);
5372 NV_INFO(dev, "BIT table '%c' not found\n", table->id);
5377 parse_bit_structure(struct nvbios *bios, const uint16_t bitoffset)
5382 * The only restriction on parsing order currently is having 'i' first
5383 * for use of bios->*_version or bios->feature_byte while parsing;
5384 * functions shouldn't be actually *doing* anything apart from pulling
5385 * data from the image into the bios struct, thus no interdependencies
5387 ret = parse_bit_table(bios, bitoffset, &BIT_TABLE('i', i));
5388 if (ret) /* info? */
5390 if (bios->major_version >= 0x60) /* g80+ */
5391 parse_bit_table(bios, bitoffset, &BIT_TABLE('A', A));
5392 ret = parse_bit_table(bios, bitoffset, &BIT_TABLE('C', C));
5395 parse_bit_table(bios, bitoffset, &BIT_TABLE('D', display));
5396 ret = parse_bit_table(bios, bitoffset, &BIT_TABLE('I', init));
5399 parse_bit_table(bios, bitoffset, &BIT_TABLE('M', M)); /* memory? */
5400 parse_bit_table(bios, bitoffset, &BIT_TABLE('L', lvds));
5401 parse_bit_table(bios, bitoffset, &BIT_TABLE('T', tmds));
5402 parse_bit_table(bios, bitoffset, &BIT_TABLE('U', U));
5407 static int parse_bmp_structure(struct drm_device *dev, struct nvbios *bios, unsigned int offset)
5410 * Parses the BMP structure for useful things, but does not act on them
5412 * offset + 5: BMP major version
5413 * offset + 6: BMP minor version
5414 * offset + 9: BMP feature byte
5415 * offset + 10: BCD encoded BIOS version
5417 * offset + 18: init script table pointer (for bios versions < 5.10h)
5418 * offset + 20: extra init script table pointer (for bios
5421 * offset + 24: memory init table pointer (used on early bios versions)
5422 * offset + 26: SDR memory sequencing setup data table
5423 * offset + 28: DDR memory sequencing setup data table
5425 * offset + 54: index of I2C CRTC pair to use for CRT output
5426 * offset + 55: index of I2C CRTC pair to use for TV output
5427 * offset + 56: index of I2C CRTC pair to use for flat panel output
5428 * offset + 58: write CRTC index for I2C pair 0
5429 * offset + 59: read CRTC index for I2C pair 0
5430 * offset + 60: write CRTC index for I2C pair 1
5431 * offset + 61: read CRTC index for I2C pair 1
5433 * offset + 67: maximum internal PLL frequency (single stage PLL)
5434 * offset + 71: minimum internal PLL frequency (single stage PLL)
5436 * offset + 75: script table pointers, as described in
5437 * parse_script_table_pointers
5439 * offset + 89: TMDS single link output A table pointer
5440 * offset + 91: TMDS single link output B table pointer
5441 * offset + 95: LVDS single link output A table pointer
5442 * offset + 105: flat panel timings table pointer
5443 * offset + 107: flat panel strapping translation table pointer
5444 * offset + 117: LVDS manufacturer panel config table pointer
5445 * offset + 119: LVDS manufacturer strapping translation table pointer
5447 * offset + 142: PLL limits table pointer
5449 * offset + 156: minimum pixel clock for LVDS dual link
5452 uint8_t *bmp = &bios->data[offset], bmp_version_major, bmp_version_minor;
5454 uint16_t legacy_scripts_offset, legacy_i2c_offset;
5456 /* load needed defaults in case we can't parse this info */
5457 bios->digital_min_front_porch = 0x4b;
5458 bios->fmaxvco = 256000;
5459 bios->fminvco = 128000;
5460 bios->fp.duallink_transition_clk = 90000;
5462 bmp_version_major = bmp[5];
5463 bmp_version_minor = bmp[6];
5465 NV_TRACE(dev, "BMP version %d.%d\n",
5466 bmp_version_major, bmp_version_minor);
5469 * Make sure that 0x36 is blank and can't be mistaken for a DCB
5470 * pointer on early versions
5472 if (bmp_version_major < 5)
5473 *(uint16_t *)&bios->data[0x36] = 0;
5476 * Seems that the minor version was 1 for all major versions prior
5477 * to 5. Version 6 could theoretically exist, but I suspect BIT
5480 if ((bmp_version_major < 5 && bmp_version_minor != 1) || bmp_version_major > 5) {
5481 NV_ERROR(dev, "You have an unsupported BMP version. "
5482 "Please send in your bios\n");
5486 if (bmp_version_major == 0)
5487 /* nothing that's currently useful in this version */
5489 else if (bmp_version_major == 1)
5490 bmplength = 44; /* exact for 1.01 */
5491 else if (bmp_version_major == 2)
5492 bmplength = 48; /* exact for 2.01 */
5493 else if (bmp_version_major == 3)
5495 /* guessed - mem init tables added in this version */
5496 else if (bmp_version_major == 4 || bmp_version_minor < 0x1)
5497 /* don't know if 5.0 exists... */
5499 /* guessed - BMP I2C indices added in version 4*/
5500 else if (bmp_version_minor < 0x6)
5501 bmplength = 67; /* exact for 5.01 */
5502 else if (bmp_version_minor < 0x10)
5503 bmplength = 75; /* exact for 5.06 */
5504 else if (bmp_version_minor == 0x10)
5505 bmplength = 89; /* exact for 5.10h */
5506 else if (bmp_version_minor < 0x14)
5507 bmplength = 118; /* exact for 5.11h */
5508 else if (bmp_version_minor < 0x24)
5510 * Not sure of version where pll limits came in;
5511 * certainly exist by 0x24 though.
5513 /* length not exact: this is long enough to get lvds members */
5515 else if (bmp_version_minor < 0x27)
5517 * Length not exact: this is long enough to get pll limit
5523 * Length not exact: this is long enough to get dual link
5529 if (nv_cksum(bmp, 8)) {
5530 NV_ERROR(dev, "Bad BMP checksum\n");
5535 * Bit 4 seems to indicate either a mobile bios or a quadro card --
5536 * mobile behaviour consistent (nv11+), quadro only seen nv18gl-nv36gl
5537 * (not nv10gl), bit 5 that the flat panel tables are present, and
5540 bios->feature_byte = bmp[9];
5542 parse_bios_version(dev, bios, offset + 10);
5544 if (bmp_version_major < 5 || bmp_version_minor < 0x10)
5545 bios->old_style_init = true;
5546 legacy_scripts_offset = 18;
5547 if (bmp_version_major < 2)
5548 legacy_scripts_offset -= 4;
5549 bios->init_script_tbls_ptr = ROM16(bmp[legacy_scripts_offset]);
5550 bios->extra_init_script_tbl_ptr = ROM16(bmp[legacy_scripts_offset + 2]);
5552 if (bmp_version_major > 2) { /* appears in BMP 3 */
5553 bios->legacy.mem_init_tbl_ptr = ROM16(bmp[24]);
5554 bios->legacy.sdr_seq_tbl_ptr = ROM16(bmp[26]);
5555 bios->legacy.ddr_seq_tbl_ptr = ROM16(bmp[28]);
5558 legacy_i2c_offset = 0x48; /* BMP version 2 & 3 */
5560 legacy_i2c_offset = offset + 54;
5561 bios->legacy.i2c_indices.crt = bios->data[legacy_i2c_offset];
5562 bios->legacy.i2c_indices.tv = bios->data[legacy_i2c_offset + 1];
5563 bios->legacy.i2c_indices.panel = bios->data[legacy_i2c_offset + 2];
5565 if (bmplength > 74) {
5566 bios->fmaxvco = ROM32(bmp[67]);
5567 bios->fminvco = ROM32(bmp[71]);
5570 parse_script_table_pointers(bios, offset + 75);
5571 if (bmplength > 94) {
5572 bios->tmds.output0_script_ptr = ROM16(bmp[89]);
5573 bios->tmds.output1_script_ptr = ROM16(bmp[91]);
5575 * Never observed in use with lvds scripts, but is reused for
5576 * 18/24 bit panel interface default for EDID equipped panels
5577 * (if_is_24bit not set directly to avoid any oscillation).
5579 bios->legacy.lvds_single_a_script_ptr = ROM16(bmp[95]);
5581 if (bmplength > 108) {
5582 bios->fp.fptablepointer = ROM16(bmp[105]);
5583 bios->fp.fpxlatetableptr = ROM16(bmp[107]);
5584 bios->fp.xlatwidth = 1;
5586 if (bmplength > 120) {
5587 bios->fp.lvdsmanufacturerpointer = ROM16(bmp[117]);
5588 bios->fp.fpxlatemanufacturertableptr = ROM16(bmp[119]);
5590 if (bmplength > 143)
5591 bios->pll_limit_tbl_ptr = ROM16(bmp[142]);
5593 if (bmplength > 157)
5594 bios->fp.duallink_transition_clk = ROM16(bmp[156]) * 10;
5599 static uint16_t findstr(uint8_t *data, int n, const uint8_t *str, int len)
5603 for (i = 0; i <= (n - len); i++) {
5604 for (j = 0; j < len; j++)
5605 if (data[i + j] != str[j])
5615 dcb_table(struct drm_device *dev)
5617 struct drm_nouveau_private *dev_priv = dev->dev_private;
5620 if (dev_priv->card_type > NV_04)
5621 dcb = ROMPTR(dev, dev_priv->vbios.data[0x36]);
5623 NV_WARNONCE(dev, "No DCB data found in VBIOS\n");
5627 if (dcb[0] >= 0x41) {
5628 NV_WARNONCE(dev, "DCB version 0x%02x unknown\n", dcb[0]);
5631 if (dcb[0] >= 0x30) {
5632 if (ROM32(dcb[6]) == 0x4edcbdcb)
5635 if (dcb[0] >= 0x20) {
5636 if (ROM32(dcb[4]) == 0x4edcbdcb)
5639 if (dcb[0] >= 0x15) {
5640 if (!memcmp(&dcb[-7], "DEV_REC", 7))
5644 * v1.4 (some NV15/16, NV11+) seems the same as v1.5, but
5645 * always has the same single (crt) entry, even when tv-out
5646 * present, so the conclusion is this version cannot really
5649 * v1.2 tables (some NV6/10, and NV15+) normally have the
5650 * same 5 entries, which are not specific to the card and so
5653 * v1.2 does have an I2C table that read_dcb_i2c_table can
5654 * handle, but cards exist (nv11 in #14821) with a bad i2c
5655 * table pointer, so use the indices parsed in
5656 * parse_bmp_structure.
5658 * v1.1 (NV5+, maybe some NV4) is entirely unhelpful
5660 NV_WARNONCE(dev, "No useful DCB data in VBIOS\n");
5664 NV_WARNONCE(dev, "DCB header validation failed\n");
5669 dcb_outp(struct drm_device *dev, u8 idx)
5671 u8 *dcb = dcb_table(dev);
5672 if (dcb && dcb[0] >= 0x30) {
5674 return dcb + dcb[1] + (idx * dcb[3]);
5676 if (dcb && dcb[0] >= 0x20) {
5677 u8 *i2c = ROMPTR(dev, dcb[2]);
5678 u8 *ent = dcb + 8 + (idx * 8);
5679 if (i2c && ent < i2c)
5682 if (dcb && dcb[0] >= 0x15) {
5683 u8 *i2c = ROMPTR(dev, dcb[2]);
5684 u8 *ent = dcb + 4 + (idx * 10);
5685 if (i2c && ent < i2c)
5693 dcb_outp_foreach(struct drm_device *dev, void *data,
5694 int (*exec)(struct drm_device *, void *, int idx, u8 *outp))
5698 while ((outp = dcb_outp(dev, ++idx))) {
5699 if (ROM32(outp[0]) == 0x00000000)
5700 break; /* seen on an NV11 with DCB v1.5 */
5701 if (ROM32(outp[0]) == 0xffffffff)
5702 break; /* seen on an NV17 with DCB v2.0 */
5704 if ((outp[0] & 0x0f) == OUTPUT_UNUSED)
5706 if ((outp[0] & 0x0f) == OUTPUT_EOL)
5709 ret = exec(dev, data, idx, outp);
5718 dcb_conntab(struct drm_device *dev)
5720 u8 *dcb = dcb_table(dev);
5721 if (dcb && dcb[0] >= 0x30 && dcb[1] >= 0x16) {
5722 u8 *conntab = ROMPTR(dev, dcb[0x14]);
5723 if (conntab && conntab[0] >= 0x30 && conntab[0] <= 0x40)
5730 dcb_conn(struct drm_device *dev, u8 idx)
5732 u8 *conntab = dcb_conntab(dev);
5733 if (conntab && idx < conntab[2])
5734 return conntab + conntab[1] + (idx * conntab[3]);
5738 static struct dcb_entry *new_dcb_entry(struct dcb_table *dcb)
5740 struct dcb_entry *entry = &dcb->entry[dcb->entries];
5742 memset(entry, 0, sizeof(struct dcb_entry));
5743 entry->index = dcb->entries++;
5748 static void fabricate_dcb_output(struct dcb_table *dcb, int type, int i2c,
5751 struct dcb_entry *entry = new_dcb_entry(dcb);
5754 entry->i2c_index = i2c;
5755 entry->heads = heads;
5756 if (type != OUTPUT_ANALOG)
5757 entry->location = !DCB_LOC_ON_CHIP; /* ie OFF CHIP */
5762 parse_dcb20_entry(struct drm_device *dev, struct dcb_table *dcb,
5763 uint32_t conn, uint32_t conf, struct dcb_entry *entry)
5765 entry->type = conn & 0xf;
5766 entry->i2c_index = (conn >> 4) & 0xf;
5767 entry->heads = (conn >> 8) & 0xf;
5768 entry->connector = (conn >> 12) & 0xf;
5769 entry->bus = (conn >> 16) & 0xf;
5770 entry->location = (conn >> 20) & 0x3;
5771 entry->or = (conn >> 24) & 0xf;
5773 switch (entry->type) {
5776 * Although the rest of a CRT conf dword is usually
5777 * zeros, mac biosen have stuff there so we must mask
5779 entry->crtconf.maxfreq = (dcb->version < 0x30) ?
5780 (conf & 0xffff) * 10 :
5781 (conf & 0xff) * 10000;
5787 entry->lvdsconf.use_straps_for_mode = true;
5788 if (dcb->version < 0x22) {
5791 * The laptop in bug 14567 lies and claims to not use
5792 * straps when it does, so assume all DCB 2.0 laptops
5793 * use straps, until a broken EDID using one is produced
5795 entry->lvdsconf.use_straps_for_mode = true;
5797 * Both 0x4 and 0x8 show up in v2.0 tables; assume they
5798 * mean the same thing (probably wrong, but might work)
5800 if (conf & 0x4 || conf & 0x8)
5801 entry->lvdsconf.use_power_scripts = true;
5805 entry->lvdsconf.use_acpi_for_edid = true;
5807 entry->lvdsconf.use_power_scripts = true;
5808 entry->lvdsconf.sor.link = (conf & 0x00000030) >> 4;
5812 * Until we even try to use these on G8x, it's
5813 * useless reporting unknown bits. They all are.
5815 if (dcb->version >= 0x40)
5818 NV_ERROR(dev, "Unknown LVDS configuration bits, "
5825 if (dcb->version >= 0x30)
5826 entry->tvconf.has_component_output = conf & (0x8 << 4);
5828 entry->tvconf.has_component_output = false;
5833 entry->dpconf.sor.link = (conf & 0x00000030) >> 4;
5834 switch ((conf & 0x00e00000) >> 21) {
5836 entry->dpconf.link_bw = 162000;
5839 entry->dpconf.link_bw = 270000;
5842 switch ((conf & 0x0f000000) >> 24) {
5844 entry->dpconf.link_nr = 4;
5847 entry->dpconf.link_nr = 2;
5850 entry->dpconf.link_nr = 1;
5855 if (dcb->version >= 0x40)
5856 entry->tmdsconf.sor.link = (conf & 0x00000030) >> 4;
5857 else if (dcb->version >= 0x30)
5858 entry->tmdsconf.slave_addr = (conf & 0x00000700) >> 8;
5859 else if (dcb->version >= 0x22)
5860 entry->tmdsconf.slave_addr = (conf & 0x00000070) >> 4;
5864 /* weird g80 mobile type that "nv" treats as a terminator */
5871 if (dcb->version < 0x40) {
5872 /* Normal entries consist of a single bit, but dual link has
5873 * the next most significant bit set too
5875 entry->duallink_possible =
5876 ((1 << (ffs(entry->or) - 1)) * 3 == entry->or);
5878 entry->duallink_possible = (entry->sorconf.link == 3);
5881 /* unsure what DCB version introduces this, 3.0? */
5882 if (conf & 0x100000)
5883 entry->i2c_upper_default = true;
5889 parse_dcb15_entry(struct drm_device *dev, struct dcb_table *dcb,
5890 uint32_t conn, uint32_t conf, struct dcb_entry *entry)
5892 switch (conn & 0x0000000f) {
5894 entry->type = OUTPUT_ANALOG;
5897 entry->type = OUTPUT_TV;
5902 entry->type = OUTPUT_LVDS;
5904 entry->type = OUTPUT_TMDS;
5907 entry->type = OUTPUT_LVDS;
5910 NV_ERROR(dev, "Unknown DCB type %d\n", conn & 0x0000000f);
5914 entry->i2c_index = (conn & 0x0003c000) >> 14;
5915 entry->heads = ((conn & 0x001c0000) >> 18) + 1;
5916 entry->or = entry->heads; /* same as heads, hopefully safe enough */
5917 entry->location = (conn & 0x01e00000) >> 21;
5918 entry->bus = (conn & 0x0e000000) >> 25;
5919 entry->duallink_possible = false;
5921 switch (entry->type) {
5923 entry->crtconf.maxfreq = (conf & 0xffff) * 10;
5926 entry->tvconf.has_component_output = false;
5929 if ((conn & 0x00003f00) >> 8 != 0x10)
5930 entry->lvdsconf.use_straps_for_mode = true;
5931 entry->lvdsconf.use_power_scripts = true;
5941 void merge_like_dcb_entries(struct drm_device *dev, struct dcb_table *dcb)
5944 * DCB v2.0 lists each output combination separately.
5945 * Here we merge compatible entries to have fewer outputs, with
5949 int i, newentries = 0;
5951 for (i = 0; i < dcb->entries; i++) {
5952 struct dcb_entry *ient = &dcb->entry[i];
5955 for (j = i + 1; j < dcb->entries; j++) {
5956 struct dcb_entry *jent = &dcb->entry[j];
5958 if (jent->type == 100) /* already merged entry */
5961 /* merge heads field when all other fields the same */
5962 if (jent->i2c_index == ient->i2c_index &&
5963 jent->type == ient->type &&
5964 jent->location == ient->location &&
5965 jent->or == ient->or) {
5966 NV_TRACE(dev, "Merging DCB entries %d and %d\n",
5968 ient->heads |= jent->heads;
5969 jent->type = 100; /* dummy value */
5974 /* Compact entries merged into others out of dcb */
5975 for (i = 0; i < dcb->entries; i++) {
5976 if (dcb->entry[i].type == 100)
5979 if (newentries != i) {
5980 dcb->entry[newentries] = dcb->entry[i];
5981 dcb->entry[newentries].index = newentries;
5986 dcb->entries = newentries;
5990 apply_dcb_encoder_quirks(struct drm_device *dev, int idx, u32 *conn, u32 *conf)
5992 struct drm_nouveau_private *dev_priv = dev->dev_private;
5993 struct dcb_table *dcb = &dev_priv->vbios.dcb;
5995 /* Dell Precision M6300
5996 * DCB entry 2: 02025312 00000010
5997 * DCB entry 3: 02026312 00000020
5999 * Identical, except apparently a different connector on a
6000 * different SOR link. Not a clue how we're supposed to know
6001 * which one is in use if it even shares an i2c line...
6003 * Ignore the connector on the second SOR link to prevent
6004 * nasty problems until this is sorted (assuming it's not a
6007 if (nv_match_device(dev, 0x040d, 0x1028, 0x019b)) {
6008 if (*conn == 0x02026312 && *conf == 0x00000020)
6014 * DCB reports an LVDS output that should be TMDS:
6015 * DCB entry 1: f2005014 ffffffff
6017 if (nv_match_device(dev, 0x0201, 0x1462, 0x8851)) {
6018 if (*conn == 0xf2005014 && *conf == 0xffffffff) {
6019 fabricate_dcb_output(dcb, OUTPUT_TMDS, 1, 1, 1);
6026 * So many things wrong here, replace the entire encoder table..
6028 if (nv_match_device(dev, 0x0ca3, 0x1682, 0x3003)) {
6030 *conn = 0x02001300; /* VGA, connector 1 */
6034 *conn = 0x01010312; /* DVI, connector 0 */
6038 *conn = 0x01010310; /* VGA, connector 0 */
6042 *conn = 0x02022362; /* HDMI, connector 2 */
6045 *conn = 0x0000000e; /* EOL */
6050 /* Some other twisted XFX board (rhbz#694914)
6052 * The DVI/VGA encoder combo that's supposed to represent the
6053 * DVI-I connector actually point at two different ones, and
6054 * the HDMI connector ends up paired with the VGA instead.
6056 * Connector table is missing anything for VGA at all, pointing it
6057 * an invalid conntab entry 2 so we figure it out ourself.
6059 if (nv_match_device(dev, 0x0615, 0x1682, 0x2605)) {
6061 *conn = 0x02002300; /* VGA, connector 2 */
6065 *conn = 0x01010312; /* DVI, connector 0 */
6069 *conn = 0x04020310; /* VGA, connector 0 */
6073 *conn = 0x02021322; /* HDMI, connector 1 */
6076 *conn = 0x0000000e; /* EOL */
6085 fabricate_dcb_encoder_table(struct drm_device *dev, struct nvbios *bios)
6087 struct dcb_table *dcb = &bios->dcb;
6088 int all_heads = (nv_two_heads(dev) ? 3 : 1);
6091 /* Apple iMac G4 NV17 */
6092 if (of_machine_is_compatible("PowerMac4,5")) {
6093 fabricate_dcb_output(dcb, OUTPUT_TMDS, 0, all_heads, 1);
6094 fabricate_dcb_output(dcb, OUTPUT_ANALOG, 1, all_heads, 2);
6099 /* Make up some sane defaults */
6100 fabricate_dcb_output(dcb, OUTPUT_ANALOG,
6101 bios->legacy.i2c_indices.crt, 1, 1);
6103 if (nv04_tv_identify(dev, bios->legacy.i2c_indices.tv) >= 0)
6104 fabricate_dcb_output(dcb, OUTPUT_TV,
6105 bios->legacy.i2c_indices.tv,
6108 else if (bios->tmds.output0_script_ptr ||
6109 bios->tmds.output1_script_ptr)
6110 fabricate_dcb_output(dcb, OUTPUT_TMDS,
6111 bios->legacy.i2c_indices.panel,
6116 parse_dcb_entry(struct drm_device *dev, void *data, int idx, u8 *outp)
6118 struct drm_nouveau_private *dev_priv = dev->dev_private;
6119 struct dcb_table *dcb = &dev_priv->vbios.dcb;
6120 u32 conf = (dcb->version >= 0x20) ? ROM32(outp[4]) : ROM32(outp[6]);
6121 u32 conn = ROM32(outp[0]);
6124 if (apply_dcb_encoder_quirks(dev, idx, &conn, &conf)) {
6125 struct dcb_entry *entry = new_dcb_entry(dcb);
6127 NV_TRACEWARN(dev, "DCB outp %02d: %08x %08x\n", idx, conn, conf);
6129 if (dcb->version >= 0x20)
6130 ret = parse_dcb20_entry(dev, dcb, conn, conf, entry);
6132 ret = parse_dcb15_entry(dev, dcb, conn, conf, entry);
6134 return 1; /* stop parsing */
6136 /* Ignore the I2C index for on-chip TV-out, as there
6137 * are cards with bogus values (nv31m in bug 23212),
6138 * and it's otherwise useless.
6140 if (entry->type == OUTPUT_TV &&
6141 entry->location == DCB_LOC_ON_CHIP)
6142 entry->i2c_index = 0x0f;
6149 dcb_fake_connectors(struct nvbios *bios)
6151 struct dcb_table *dcbt = &bios->dcb;
6155 /* heuristic: if we ever get a non-zero connector field, assume
6156 * that all the indices are valid and we don't need fake them.
6158 for (i = 0; i < dcbt->entries; i++) {
6159 if (dcbt->entry[i].connector)
6163 /* no useful connector info available, we need to make it up
6164 * ourselves. the rule here is: anything on the same i2c bus
6165 * is considered to be on the same connector. any output
6166 * without an associated i2c bus is assigned its own unique
6169 for (i = 0; i < dcbt->entries; i++) {
6170 u8 i2c = dcbt->entry[i].i2c_index;
6172 dcbt->entry[i].connector = idx++;
6176 dcbt->entry[i].connector = map[i2c] - 1;
6180 /* if we created more than one connector, destroy the connector
6181 * table - just in case it has random, rather than stub, entries.
6184 u8 *conntab = dcb_conntab(bios->dev);
6191 parse_dcb_table(struct drm_device *dev, struct nvbios *bios)
6193 struct dcb_table *dcb = &bios->dcb;
6197 dcbt = dcb_table(dev);
6199 /* handle pre-DCB boards */
6200 if (bios->type == NVBIOS_BMP) {
6201 fabricate_dcb_encoder_table(dev, bios);
6208 NV_TRACE(dev, "DCB version %d.%d\n", dcbt[0] >> 4, dcbt[0] & 0xf);
6210 dcb->version = dcbt[0];
6211 dcb_outp_foreach(dev, NULL, parse_dcb_entry);
6214 * apart for v2.1+ not being known for requiring merging, this
6215 * guarantees dcbent->index is the index of the entry in the rom image
6217 if (dcb->version < 0x21)
6218 merge_like_dcb_entries(dev, dcb);
6223 /* dump connector table entries to log, if any exist */
6225 while ((conn = dcb_conn(dev, ++idx))) {
6226 if (conn[0] != 0xff) {
6227 NV_TRACE(dev, "DCB conn %02d: ", idx);
6228 if (dcb_conntab(dev)[3] < 4)
6229 printk("%04x\n", ROM16(conn[0]));
6231 printk("%08x\n", ROM32(conn[0]));
6234 dcb_fake_connectors(bios);
6238 static int load_nv17_hwsq_ucode_entry(struct drm_device *dev, struct nvbios *bios, uint16_t hwsq_offset, int entry)
6241 * The header following the "HWSQ" signature has the number of entries,
6242 * and the entry size
6244 * An entry consists of a dword to write to the sequencer control reg
6245 * (0x00001304), followed by the ucode bytes, written sequentially,
6246 * starting at reg 0x00001400
6249 uint8_t bytes_to_write;
6250 uint16_t hwsq_entry_offset;
6253 if (bios->data[hwsq_offset] <= entry) {
6254 NV_ERROR(dev, "Too few entries in HW sequencer table for "
6255 "requested entry\n");
6259 bytes_to_write = bios->data[hwsq_offset + 1];
6261 if (bytes_to_write != 36) {
6262 NV_ERROR(dev, "Unknown HW sequencer entry size\n");
6266 NV_TRACE(dev, "Loading NV17 power sequencing microcode\n");
6268 hwsq_entry_offset = hwsq_offset + 2 + entry * bytes_to_write;
6270 /* set sequencer control */
6271 bios_wr32(bios, 0x00001304, ROM32(bios->data[hwsq_entry_offset]));
6272 bytes_to_write -= 4;
6275 for (i = 0; i < bytes_to_write; i += 4)
6276 bios_wr32(bios, 0x00001400 + i, ROM32(bios->data[hwsq_entry_offset + i + 4]));
6278 /* twiddle NV_PBUS_DEBUG_4 */
6279 bios_wr32(bios, NV_PBUS_DEBUG_4, bios_rd32(bios, NV_PBUS_DEBUG_4) | 0x18);
6284 static int load_nv17_hw_sequencer_ucode(struct drm_device *dev,
6285 struct nvbios *bios)
6288 * BMP based cards, from NV17, need a microcode loading to correctly
6289 * control the GPIO etc for LVDS panels
6291 * BIT based cards seem to do this directly in the init scripts
6293 * The microcode entries are found by the "HWSQ" signature.
6296 const uint8_t hwsq_signature[] = { 'H', 'W', 'S', 'Q' };
6297 const int sz = sizeof(hwsq_signature);
6300 hwsq_offset = findstr(bios->data, bios->length, hwsq_signature, sz);
6304 /* always use entry 0? */
6305 return load_nv17_hwsq_ucode_entry(dev, bios, hwsq_offset + sz, 0);
6308 uint8_t *nouveau_bios_embedded_edid(struct drm_device *dev)
6310 struct drm_nouveau_private *dev_priv = dev->dev_private;
6311 struct nvbios *bios = &dev_priv->vbios;
6312 const uint8_t edid_sig[] = {
6313 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00 };
6314 uint16_t offset = 0;
6316 int searchlen = NV_PROM_SIZE;
6319 return bios->fp.edid;
6322 newoffset = findstr(&bios->data[offset], searchlen,
6326 offset += newoffset;
6327 if (!nv_cksum(&bios->data[offset], EDID1_LEN))
6330 searchlen -= offset;
6334 NV_TRACE(dev, "Found EDID in BIOS\n");
6336 return bios->fp.edid = &bios->data[offset];
6340 nouveau_bios_run_init_table(struct drm_device *dev, uint16_t table,
6341 struct dcb_entry *dcbent, int crtc)
6343 struct drm_nouveau_private *dev_priv = dev->dev_private;
6344 struct nvbios *bios = &dev_priv->vbios;
6345 struct init_exec iexec = { true, false };
6347 spin_lock_bh(&bios->lock);
6348 bios->display.output = dcbent;
6349 bios->display.crtc = crtc;
6350 parse_init_table(bios, table, &iexec);
6351 bios->display.output = NULL;
6352 spin_unlock_bh(&bios->lock);
6356 nouveau_bios_init_exec(struct drm_device *dev, uint16_t table)
6358 struct drm_nouveau_private *dev_priv = dev->dev_private;
6359 struct nvbios *bios = &dev_priv->vbios;
6360 struct init_exec iexec = { true, false };
6362 parse_init_table(bios, table, &iexec);
6365 static bool NVInitVBIOS(struct drm_device *dev)
6367 struct drm_nouveau_private *dev_priv = dev->dev_private;
6368 struct nvbios *bios = &dev_priv->vbios;
6370 memset(bios, 0, sizeof(struct nvbios));
6371 spin_lock_init(&bios->lock);
6374 return bios_shadow(dev);
6377 static int nouveau_parse_vbios_struct(struct drm_device *dev)
6379 struct drm_nouveau_private *dev_priv = dev->dev_private;
6380 struct nvbios *bios = &dev_priv->vbios;
6381 const uint8_t bit_signature[] = { 0xff, 0xb8, 'B', 'I', 'T' };
6382 const uint8_t bmp_signature[] = { 0xff, 0x7f, 'N', 'V', 0x0 };
6385 offset = findstr(bios->data, bios->length,
6386 bit_signature, sizeof(bit_signature));
6388 NV_TRACE(dev, "BIT BIOS found\n");
6389 bios->type = NVBIOS_BIT;
6390 bios->offset = offset;
6391 return parse_bit_structure(bios, offset + 6);
6394 offset = findstr(bios->data, bios->length,
6395 bmp_signature, sizeof(bmp_signature));
6397 NV_TRACE(dev, "BMP BIOS found\n");
6398 bios->type = NVBIOS_BMP;
6399 bios->offset = offset;
6400 return parse_bmp_structure(dev, bios, offset);
6403 NV_ERROR(dev, "No known BIOS signature found\n");
6408 nouveau_run_vbios_init(struct drm_device *dev)
6410 struct drm_nouveau_private *dev_priv = dev->dev_private;
6411 struct nvbios *bios = &dev_priv->vbios;
6414 /* Reset the BIOS head to 0. */
6415 bios->state.crtchead = 0;
6417 if (bios->major_version < 5) /* BMP only */
6418 load_nv17_hw_sequencer_ucode(dev, bios);
6420 if (bios->execute) {
6421 bios->fp.last_script_invoc = 0;
6422 bios->fp.lvds_init_run = false;
6425 parse_init_tables(bios);
6428 * Runs some additional script seen on G8x VBIOSen. The VBIOS'
6429 * parser will run this right after the init tables, the binary
6430 * driver appears to run it at some point later.
6432 if (bios->some_script_ptr) {
6433 struct init_exec iexec = {true, false};
6435 NV_INFO(dev, "Parsing VBIOS init table at offset 0x%04X\n",
6436 bios->some_script_ptr);
6437 parse_init_table(bios, bios->some_script_ptr, &iexec);
6440 if (dev_priv->card_type >= NV_50) {
6441 for (i = 0; i < bios->dcb.entries; i++) {
6442 nouveau_bios_run_display_table(dev, 0, 0,
6443 &bios->dcb.entry[i], -1);
6451 nouveau_bios_posted(struct drm_device *dev)
6453 struct drm_nouveau_private *dev_priv = dev->dev_private;
6456 if (dev_priv->card_type >= NV_50) {
6457 if (NVReadVgaCrtc(dev, 0, 0x00) == 0 &&
6458 NVReadVgaCrtc(dev, 0, 0x1a) == 0)
6463 htotal = NVReadVgaCrtc(dev, 0, 0x06);
6464 htotal |= (NVReadVgaCrtc(dev, 0, 0x07) & 0x01) << 8;
6465 htotal |= (NVReadVgaCrtc(dev, 0, 0x07) & 0x20) << 4;
6466 htotal |= (NVReadVgaCrtc(dev, 0, 0x25) & 0x01) << 10;
6467 htotal |= (NVReadVgaCrtc(dev, 0, 0x41) & 0x01) << 11;
6469 return (htotal != 0);
6473 nouveau_bios_init(struct drm_device *dev)
6475 struct drm_nouveau_private *dev_priv = dev->dev_private;
6476 struct nvbios *bios = &dev_priv->vbios;
6479 if (!NVInitVBIOS(dev))
6482 ret = nouveau_parse_vbios_struct(dev);
6486 ret = nouveau_i2c_init(dev);
6490 ret = nouveau_mxm_init(dev);
6494 ret = parse_dcb_table(dev, bios);
6498 if (!bios->major_version) /* we don't run version 0 bios */
6501 /* init script execution disabled */
6502 bios->execute = false;
6504 /* ... unless card isn't POSTed already */
6505 if (!nouveau_bios_posted(dev)) {
6506 NV_INFO(dev, "Adaptor not initialised, "
6507 "running VBIOS init tables.\n");
6508 bios->execute = true;
6510 if (nouveau_force_post)
6511 bios->execute = true;
6513 ret = nouveau_run_vbios_init(dev);
6517 /* feature_byte on BMP is poor, but init always sets CR4B */
6518 if (bios->major_version < 5)
6519 bios->is_mobile = NVReadVgaCrtc(dev, 0, NV_CIO_CRE_4B) & 0x40;
6521 /* all BIT systems need p_f_m_t for digital_min_front_porch */
6522 if (bios->is_mobile || bios->major_version >= 5)
6523 ret = parse_fp_mode_table(dev, bios);
6525 /* allow subsequent scripts to execute */
6526 bios->execute = true;
6532 nouveau_bios_takedown(struct drm_device *dev)
6534 struct drm_nouveau_private *dev_priv = dev->dev_private;
6536 nouveau_mxm_fini(dev);
6537 nouveau_i2c_fini(dev);
6539 kfree(dev_priv->vbios.data);