2 * Copyright 2005-2006 Erik Waling
3 * Copyright 2006 Stephane Marchesin
4 * Copyright 2007-2008 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
25 #include "nv_include.h"
28 /* FIXME: put these somewhere */
29 #define SEQ_INDEX VGA_SEQ_INDEX
30 #define NV_VGA_CRTCX_OWNER_HEADA 0x0
31 #define NV_VGA_CRTCX_OWNER_HEADB 0x3
32 #define NV_PRAMIN_ROM_OFFSET 0x00700000
33 #define FEATURE_MOBILE 0x10
37 static int crtchead = 0;
39 /* this will need remembering across a suspend */
40 static uint32_t saved_nv_pfb_cfg0;
47 static uint16_t le16_to_cpu(const uint16_t x)
49 #if X_BYTE_ORDER == X_BIG_ENDIAN
56 static uint32_t le32_to_cpu(const uint32_t x)
58 #if X_BYTE_ORDER == X_BIG_ENDIAN
65 static bool nv_cksum(const uint8_t *data, unsigned int length)
67 /* there's a few checksums in the BIOS, so here's a generic checking function */
71 for (i = 0; i < length; i++)
80 static int NVValidVBIOS(ScrnInfoPtr pScrn, const uint8_t *data)
82 /* check for BIOS signature */
83 if (!(data[0] == 0x55 && data[1] == 0xAA)) {
84 xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
85 "... BIOS signature not found\n");
89 if (nv_cksum(data, data[2] * 512)) {
90 xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
91 "... BIOS checksum invalid\n");
92 /* probably ought to set a do_not_execute flag for table parsing here,
93 * assuming most BIOSen are valid */
96 xf86DrvMsg(pScrn->scrnIndex, X_INFO, "... appears to be valid\n");
101 static void NVShadowVBIOS_PROM(ScrnInfoPtr pScrn, uint8_t *data)
103 NVPtr pNv = NVPTR(pScrn);
106 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
107 "Attempting to locate BIOS image in PROM\n");
109 /* enable ROM access */
110 nvWriteMC(pNv, NV_PBUS_PCI_NV_20, NV_PBUS_PCI_NV_20_ROM_SHADOW_DISABLED);
111 for (i = 0; i < NV_PROM_SIZE; i++) {
112 /* according to nvclock, we need that to work around a 6600GT/6800LE bug */
113 data[i] = NV_RD08(pNv->REGS, NV_PROM_OFFSET + i);
114 data[i] = NV_RD08(pNv->REGS, NV_PROM_OFFSET + i);
115 data[i] = NV_RD08(pNv->REGS, NV_PROM_OFFSET + i);
116 data[i] = NV_RD08(pNv->REGS, NV_PROM_OFFSET + i);
117 data[i] = NV_RD08(pNv->REGS, NV_PROM_OFFSET + i);
119 /* disable ROM access */
120 nvWriteMC(pNv, NV_PBUS_PCI_NV_20, NV_PBUS_PCI_NV_20_ROM_SHADOW_ENABLED);
123 static void NVShadowVBIOS_PRAMIN(ScrnInfoPtr pScrn, uint8_t *data)
125 NVPtr pNv = NVPTR(pScrn);
126 uint32_t old_bar0_pramin = 0;
129 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
130 "Attempting to locate BIOS image in PRAMIN\n");
132 if (pNv->Architecture >= NV_ARCH_50) {
133 uint32_t vbios_vram = (NV_RD32(pNv->REGS, 0x619f04) & ~0xff) << 8;
136 vbios_vram = (NV_RD32(pNv->REGS, 0x1700) << 16) + 0xf0000;
138 old_bar0_pramin = NV_RD32(pNv->REGS, 0x1700);
139 NV_WR32(pNv->REGS, 0x1700, vbios_vram >> 16);
142 for (i = 0; i < NV_PROM_SIZE; i++)
143 data[i] = NV_RD08(pNv->REGS, NV_PRAMIN_ROM_OFFSET + i);
145 if (pNv->Architecture >= NV_ARCH_50)
146 NV_WR32(pNv->REGS, 0x1700, old_bar0_pramin);
149 static void NVVBIOS_PCIROM(ScrnInfoPtr pScrn, uint8_t *data)
151 NVPtr pNv = NVPTR(pScrn);
153 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
154 "Attempting to use PCI ROM BIOS image\n");
156 #if XSERVER_LIBPCIACCESS
157 pci_device_read_rom(pNv->PciInfo, data);
159 xf86ReadPciBIOS(0, pNv->PciTag, 0, data, NV_PROM_SIZE);
163 static bool NVShadowVBIOS(ScrnInfoPtr pScrn, uint8_t *data)
165 NVShadowVBIOS_PROM(pScrn, data);
166 if (NVValidVBIOS(pScrn, data) == 2)
169 NVShadowVBIOS_PRAMIN(pScrn, data);
170 if (NVValidVBIOS(pScrn, data))
174 NVVBIOS_PCIROM(pScrn, data);
175 if (NVValidVBIOS(pScrn, data))
187 int length_multiplier;
188 bool (*handler)(ScrnInfoPtr pScrn, bios_t *, uint16_t, init_exec_t *);
197 static void parse_init_table(ScrnInfoPtr pScrn, bios_t *bios, unsigned int offset, init_exec_t *iexec);
199 #define MACRO_INDEX_SIZE 2
201 #define CONDITION_SIZE 12
202 #define IO_FLAG_CONDITION_SIZE 9
203 #define MEM_INIT_SIZE 66
205 static void still_alive(void)
211 static int nv_valid_reg(ScrnInfoPtr pScrn, uint32_t reg)
213 NVPtr pNv = NVPTR(pScrn);
215 /* C51 has misaligned regs on purpose. Marvellous */
216 if ((reg & 0x3 && pNv->VBIOS.chip_version != 0x51) ||
217 (reg & 0x2 && pNv->VBIOS.chip_version == 0x51)) {
218 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
219 "========== misaligned reg 0x%08X ==========\n", reg);
223 #define WITHIN(x,y,z) ((x>=y)&&(x<=y+z))
224 if (WITHIN(reg,NV_PMC_OFFSET,NV_PMC_SIZE))
226 if (WITHIN(reg,NV_PBUS_OFFSET,NV_PBUS_SIZE))
228 if (WITHIN(reg,NV_PFIFO_OFFSET,NV_PFIFO_SIZE))
230 if (pNv->VBIOS.chip_version >= 0x30 && WITHIN(reg,0x4000,0x600))
232 if (pNv->VBIOS.chip_version >= 0x40 && WITHIN(reg,0xc000,0x48))
234 if (pNv->VBIOS.chip_version >= 0x17 && reg == 0x0000d204)
236 if (pNv->VBIOS.chip_version >= 0x40) {
237 if (reg == 0x00011014 || reg == 0x00020328)
239 if (WITHIN(reg,0x88000,NV_PBUS_SIZE)) /* new PBUS */
242 if (WITHIN(reg,NV_PFB_OFFSET,NV_PFB_SIZE))
244 if (WITHIN(reg,NV_PEXTDEV_OFFSET,NV_PEXTDEV_SIZE))
246 if (WITHIN(reg,NV_PCRTC0_OFFSET,NV_PCRTC0_SIZE * 2))
248 if (WITHIN(reg,NV_PRAMDAC0_OFFSET,NV_PRAMDAC0_SIZE * 2))
250 if (pNv->VBIOS.chip_version >= 0x17 && reg == 0x0070fff0)
252 if (pNv->VBIOS.chip_version == 0x51 && WITHIN(reg,NV_PRAMIN_OFFSET,NV_PRAMIN_SIZE))
256 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
257 "========== unknown reg 0x%08X ==========\n", reg);
262 static bool nv_valid_idx_port(ScrnInfoPtr pScrn, uint16_t port)
264 /* if adding more ports here, the read/write functions below will need
265 * updating so that the correct mmio range (PCIO, PDIO, PVIO) is used
266 * for the port in question
268 if (port == CRTC_INDEX_COLOR)
270 if (port == SEQ_INDEX)
273 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
274 "========== unknown indexed io port 0x%04X ==========\n", port);
279 static bool nv_valid_port(ScrnInfoPtr pScrn, uint16_t port)
281 /* if adding more ports here, the read/write functions below will need
282 * updating so that the correct mmio range (PCIO, PDIO, PVIO) is used
283 * for the port in question
285 if (port == VGA_ENABLE)
288 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
289 "========== unknown io port 0x%04X ==========\n", port);
294 static uint32_t nv32_rd(ScrnInfoPtr pScrn, uint32_t reg)
296 NVPtr pNv = NVPTR(pScrn);
299 if (!nv_valid_reg(pScrn, reg))
302 /* C51 sometimes uses regs with bit0 set in the address. For these
303 * cases there should exist a translation in a BIOS table to an IO
304 * port address which the BIOS uses for accessing the reg
306 * These only seem to appear for the power control regs to a flat panel
307 * and in C51 mmio traces the normal regs for 0x1308 and 0x1310 are
308 * used - hence the mask below. An S3 suspend-resume mmio trace from a
309 * C51 will be required to see if this is true for the power microcode
310 * in 0x14.., or whether the direct IO port access method is needed
315 data = NV_RD32(pNv->REGS, reg);
318 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
319 " Read: Reg: 0x%08X, Data: 0x%08X\n", reg, data);
324 static void nv32_wr(ScrnInfoPtr pScrn, uint32_t reg, uint32_t data)
326 NVPtr pNv = NVPTR(pScrn);
328 if (!nv_valid_reg(pScrn, reg))
331 /* see note in nv32_rd */
338 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
339 " Write: Reg: 0x%08X, Data: 0x%08X\n", reg, data);
341 if (pNv->VBIOS.execute) {
343 NV_WR32(pNv->REGS, reg, data);
347 static uint8_t nv_idx_port_rd(ScrnInfoPtr pScrn, uint16_t port, uint8_t index)
349 NVPtr pNv = NVPTR(pScrn);
352 if (!nv_valid_idx_port(pScrn, port))
355 if (port == SEQ_INDEX)
356 data = NVReadVgaSeq(pNv, crtchead, index);
357 else /* assume CRTC_INDEX_COLOR */
358 data = NVReadVgaCrtc(pNv, crtchead, index);
361 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
362 " Indexed IO read: Port: 0x%04X, Index: 0x%02X, Head: 0x%02X, Data: 0x%02X\n",
363 port, index, crtchead, data);
368 static void nv_idx_port_wr(ScrnInfoPtr pScrn, uint16_t port, uint8_t index, uint8_t data)
370 NVPtr pNv = NVPTR(pScrn);
372 if (!nv_valid_idx_port(pScrn, port))
375 /* The current head is maintained in a file scope variable crtchead.
376 * We trap changes to CRTCX_OWNER and update the head variable
377 * and hence the register set written.
378 * As CRTCX_OWNER only exists on CRTC0, we update crtchead to head0
379 * in advance of the write, and to head1 after the write
381 if (port == CRTC_INDEX_COLOR && index == NV_VGA_CRTCX_OWNER && data != NV_VGA_CRTCX_OWNER_HEADB)
385 nv_idx_port_rd(pScrn, port, index);
387 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
388 " Indexed IO write: Port: 0x%04X, Index: 0x%02X, Head: 0x%02X, Data: 0x%02X\n",
389 port, index, crtchead, data);
391 if (pNv->VBIOS.execute) {
393 if (port == SEQ_INDEX)
394 NVWriteVgaSeq(pNv, crtchead, index, data);
395 else /* assume CRTC_INDEX_COLOR */
396 NVWriteVgaCrtc(pNv, crtchead, index, data);
399 if (port == CRTC_INDEX_COLOR && index == NV_VGA_CRTCX_OWNER && data == NV_VGA_CRTCX_OWNER_HEADB)
403 static uint8_t nv_port_rd(ScrnInfoPtr pScrn, uint16_t port)
405 NVPtr pNv = NVPTR(pScrn);
408 if (!nv_valid_port(pScrn, port))
411 data = NVReadPVIO(pNv, crtchead, port);
414 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
415 " IO read: Port: 0x%04X, Head: 0x%02X, Data: 0x%02X\n",
416 port, crtchead, data);
421 static void nv_port_wr(ScrnInfoPtr pScrn, uint16_t port, uint8_t data)
423 NVPtr pNv = NVPTR(pScrn);
425 if (!nv_valid_port(pScrn, port))
429 nv_port_rd(pScrn, port);
431 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
432 " IO write: Port: 0x%04X, Head: 0x%02X, Data: 0x%02X\n",
433 port, crtchead, data);
435 if (pNv->VBIOS.execute) {
437 NVWritePVIO(pNv, crtchead, port, data);
441 #define ACCESS_UNLOCK 0
442 #define ACCESS_LOCK 1
443 static void crtc_access(NVPtr pNv, bool lock)
447 NVLockVgaCrtc(pNv, 0, lock);
450 NVLockVgaCrtc(pNv, 1, lock);
451 NVSetOwner(pNv, crtchead);
455 static bool io_flag_condition(ScrnInfoPtr pScrn, bios_t *bios, uint16_t offset, uint8_t cond)
457 /* The IO flag condition entry has 2 bytes for the CRTC port; 1 byte
458 * for the CRTC index; 1 byte for the mask to apply to the value
459 * retrieved from the CRTC; 1 byte for the shift right to apply to the
460 * masked CRTC value; 2 bytes for the offset to the flag array, to
461 * which the shifted value is added; 1 byte for the mask applied to the
462 * value read from the flag array; and 1 byte for the value to compare
463 * against the masked byte from the flag table.
466 uint16_t condptr = bios->io_flag_condition_tbl_ptr + cond * IO_FLAG_CONDITION_SIZE;
467 uint16_t crtcport = le16_to_cpu(*((uint16_t *)(&bios->data[condptr])));
468 uint8_t crtcindex = bios->data[condptr + 2];
469 uint8_t mask = bios->data[condptr + 3];
470 uint8_t shift = bios->data[condptr + 4];
471 uint16_t flagarray = le16_to_cpu(*((uint16_t *)(&bios->data[condptr + 5])));
472 uint8_t flagarraymask = bios->data[condptr + 7];
473 uint8_t cmpval = bios->data[condptr + 8];
477 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
478 "0x%04X: Port: 0x%04X, Index: 0x%02X, Mask: 0x%02X, Shift: 0x%02X, FlagArray: 0x%04X, FAMask: 0x%02X, Cmpval: 0x%02X\n",
479 offset, crtcport, crtcindex, mask, shift, flagarray, flagarraymask, cmpval);
481 data = nv_idx_port_rd(pScrn, crtcport, crtcindex);
483 data = bios->data[flagarray + ((data & mask) >> shift)];
484 data &= flagarraymask;
487 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
488 "0x%04X: Checking if 0x%02X equals 0x%02X\n",
489 offset, data, cmpval);
497 int getMNP_single(ScrnInfoPtr pScrn, struct pll_lims *pll_lim, int clk, int *bestNM, int *bestlog2P)
499 /* Find M, N and P for a single stage PLL
501 * Note that some bioses (NV3x) have lookup tables of precomputed MNP
502 * values, but we're too lazy to use those atm
504 * "clk" parameter in kHz
505 * returns calculated clock
508 bios_t *bios = &NVPTR(pScrn)->VBIOS;
509 int minvco = pll_lim->vco1.minfreq, maxvco = pll_lim->vco1.maxfreq;
510 int minM = pll_lim->vco1.min_m, maxM = pll_lim->vco1.max_m;
511 int minN = pll_lim->vco1.min_n, maxN = pll_lim->vco1.max_n;
512 int minU = pll_lim->vco1.min_inputfreq, maxU = pll_lim->vco1.max_inputfreq;
514 int crystal = pll_lim->refclk;
517 int delta, bestdelta = INT_MAX;
520 /* this division verified for nv20, nv18, nv28 (Haiku), and nv34 */
521 /* possibly correlated with introduction of 27MHz crystal */
522 if (bios->chip_version <= 0x16 || bios->chip_version == 0x20) {
528 } else if (bios->chip_version < 0x40) {
536 } else /* nv4x may be subject to the nv17+ limits, but assume not for now */
539 if ((clk << maxlog2P) < minvco) {
540 minvco = clk << maxlog2P;
543 if (clk + clk/200 > maxvco) /* +0.5% */
544 maxvco = clk + clk/200;
546 /* NV34 goes maxlog2P->0, NV20 goes 0->maxlog2P */
547 for (log2P = 0; log2P <= maxlog2P; log2P++) {
556 for (M = minM; M <= maxM; M++) {
557 if (crystal/M < minU)
559 if (crystal/M > maxU)
562 /* add crystal/2 to round better */
563 N = (clkP * M + crystal/2) / crystal;
570 /* more rounding additions */
571 calcclk = ((N * crystal + P/2) / P + M/2) / M;
572 delta = abs(calcclk - clk);
573 /* we do an exhaustive search rather than terminating
574 * on an optimality condition...
576 if (delta < bestdelta) {
579 *bestNM = N << 8 | M;
581 if (delta == 0) /* except this one */
590 int getMNP_double(ScrnInfoPtr pScrn, struct pll_lims *pll_lim, int clk, int *bestNM1, int *bestNM2, int *bestlog2P)
592 /* Find M, N and P for a two stage PLL
594 * Note that some bioses (NV30+) have lookup tables of precomputed MNP
595 * values, but we're too lazy to use those atm
597 * "clk" parameter in kHz
598 * returns calculated clock
601 int minvco1 = pll_lim->vco1.minfreq, maxvco1 = pll_lim->vco1.maxfreq;
602 int minvco2 = pll_lim->vco2.minfreq, maxvco2 = pll_lim->vco2.maxfreq;
603 int minU1 = pll_lim->vco1.min_inputfreq, minU2 = pll_lim->vco2.min_inputfreq;
604 int maxU1 = pll_lim->vco1.max_inputfreq, maxU2 = pll_lim->vco2.max_inputfreq;
605 int minM1 = pll_lim->vco1.min_m, maxM1 = pll_lim->vco1.max_m;
606 int minN1 = pll_lim->vco1.min_n, maxN1 = pll_lim->vco1.max_n;
607 int minM2 = pll_lim->vco2.min_m, maxM2 = pll_lim->vco2.max_m;
608 int minN2 = pll_lim->vco2.min_n, maxN2 = pll_lim->vco2.max_n;
609 int crystal = pll_lim->refclk;
610 bool fixedgain2 = (minM2 == maxM2 && minN2 == maxN2);
611 int M1, N1, M2, N2, log2P;
612 int clkP, calcclk1, calcclk2, calcclkout;
613 int delta, bestdelta = INT_MAX;
616 int vco2 = (maxvco2 - maxvco2/200) / 2;
617 for (log2P = 0; log2P < 6 && clk <= (vco2 >> log2P); log2P++) /* log2P is maximum of 6 */
621 if (maxvco2 < clk + clk/200) /* +0.5% */
622 maxvco2 = clk + clk/200;
624 for (M1 = minM1; M1 <= maxM1; M1++) {
625 if (crystal/M1 < minU1)
627 if (crystal/M1 > maxU1)
630 for (N1 = minN1; N1 <= maxN1; N1++) {
631 calcclk1 = crystal * N1 / M1;
632 if (calcclk1 < minvco1)
634 if (calcclk1 > maxvco1)
637 for (M2 = minM2; M2 <= maxM2; M2++) {
638 if (calcclk1/M2 < minU2)
640 if (calcclk1/M2 > maxU2)
643 /* add calcclk1/2 to round better */
644 N2 = (clkP * M2 + calcclk1/2) / calcclk1;
651 if (N2/M2 < 4 || N2/M2 > 10)
654 calcclk2 = calcclk1 * N2 / M2;
655 if (calcclk2 < minvco2)
657 if (calcclk2 > maxvco2)
662 calcclkout = calcclk2 >> log2P;
663 delta = abs(calcclkout - clk);
664 /* we do an exhaustive search rather than terminating
665 * on an optimality condition...
667 if (delta < bestdelta) {
669 bestclk = calcclkout;
670 *bestNM1 = N1 << 8 | M1;
671 *bestNM2 = N2 << 8 | M2;
673 if (delta == 0) /* except this one */
683 static void setPLL_single(ScrnInfoPtr pScrn, uint32_t reg, int NM, int log2P)
685 bios_t *bios = &NVPTR(pScrn)->VBIOS;
686 uint32_t oldpll = nv32_rd(pScrn, reg);
687 uint32_t pll = (oldpll & 0xfff80000) | log2P << 16 | NM;
688 uint32_t saved_powerctrl_1 = 0;
689 int shift_powerctrl_1 = -4;
692 return; /* already set */
694 /* nv18 doesn't change POWERCTRL_1 for VPLL*; does gf4 need special-casing? */
695 if (bios->chip_version >= 0x17 && bios->chip_version != 0x20) {
697 case NV_RAMDAC_VPLL2:
698 shift_powerctrl_1 += 4;
700 shift_powerctrl_1 += 4;
702 shift_powerctrl_1 += 4;
703 case NV_RAMDAC_NVPLL:
704 shift_powerctrl_1 += 4;
707 if (shift_powerctrl_1 >= 0) {
708 saved_powerctrl_1 = nv32_rd(pScrn, NV_PBUS_POWERCTRL_1);
709 nv32_wr(pScrn, NV_PBUS_POWERCTRL_1, (saved_powerctrl_1 & ~(0xf << shift_powerctrl_1)) | 1 << shift_powerctrl_1);
714 nv32_wr(pScrn, reg, (oldpll & 0xffff0000) | NM);
720 /* then write P as well */
721 nv32_wr(pScrn, reg, pll);
723 if (shift_powerctrl_1 >= 0)
724 nv32_wr(pScrn, NV_PBUS_POWERCTRL_1, saved_powerctrl_1);
727 static void setPLL_double_highregs(ScrnInfoPtr pScrn, uint32_t reg1, int NM1, int NM2, int log2P)
729 bios_t *bios = &NVPTR(pScrn)->VBIOS;
730 uint32_t reg2 = reg1 + ((reg1 == NV_RAMDAC_VPLL2) ? 0x5c : 0x70);
731 uint32_t oldpll1 = nv32_rd(pScrn, reg1), oldpll2 = nv32_rd(pScrn, reg2);
732 uint32_t pll1 = (oldpll1 & 0xfff80000) | log2P << 16 | NM1;
733 uint32_t pll2 = (oldpll2 & 0x7fff0000) | 1 << 31 | NM2;
734 uint32_t saved_powerctrl_1 = 0, savedc040 = 0, maskc040 = ~0;
735 int shift_powerctrl_1 = -1;
737 if (oldpll1 == pll1 && oldpll2 == pll2)
738 return; /* already set */
740 if (reg1 == NV_RAMDAC_NVPLL) {
741 shift_powerctrl_1 = 0;
742 maskc040 = ~(3 << 20);
744 if (reg1 == NV_RAMDAC_MPLL) {
745 shift_powerctrl_1 = 4;
746 maskc040 = ~(3 << 22);
748 if (shift_powerctrl_1 >= 0) {
749 saved_powerctrl_1 = nv32_rd(pScrn, NV_PBUS_POWERCTRL_1);
750 nv32_wr(pScrn, NV_PBUS_POWERCTRL_1, (saved_powerctrl_1 & ~(0xf << shift_powerctrl_1)) | 1 << shift_powerctrl_1);
753 if (bios->chip_version >= 0x40) {
754 savedc040 = nv32_rd(pScrn, 0xc040);
755 nv32_wr(pScrn, 0xc040, savedc040 & maskc040);
757 if (reg1 == NV_RAMDAC_VPLL)
758 nv32_wr(pScrn, NV_RAMDAC_580, nv32_rd(pScrn, NV_RAMDAC_580) & ~NV_RAMDAC_580_VPLL2_ACTIVE);
759 if (reg1 == NV_RAMDAC_VPLL2)
760 nv32_wr(pScrn, NV_RAMDAC_580, nv32_rd(pScrn, NV_RAMDAC_580) & ~NV_RAMDAC_580_VPLL1_ACTIVE);
764 /* NM2 will not be 0, the way we calculate MNPs at present */
765 /* something like this will be needed if we set single pll modes on double pll chips */
767 if (crtchead == NV_VGA_CRTCX_OWNER_HEADA)
768 nv32_wr(NV_RAMDAC_580, nv32_rd(NV_RAMDAC_580) | NV_RAMDAC_580_VPLL1_ACTIVE);
770 nv32_wr(NV_RAMDAC_580, nv32_rd(NV_RAMDAC_580) | NV_RAMDAC_580_VPLL2_ACTIVE);
775 nv32_wr(pScrn, reg2, pll2);
776 nv32_wr(pScrn, reg1, pll1);
778 if (shift_powerctrl_1 >= 0) {
779 nv32_wr(pScrn, NV_PBUS_POWERCTRL_1, saved_powerctrl_1);
780 if (bios->chip_version >= 0x40)
781 nv32_wr(pScrn, 0xc040, savedc040);
785 static void setPLL_double_lowregs(ScrnInfoPtr pScrn, uint32_t NMNMreg, int NM1, int NM2, int log2P)
787 /* When setting PLLs, there is a merry game of disabling and enabling
788 * various bits of hardware during the process. This function is a
789 * synthesis of six nv40 traces, nearly each card doing a subtly
790 * different thing. With luck all the necessary bits for each card are
791 * combined herein. Without luck it deviates from each card's formula
792 * so as to not work on any :)
795 uint32_t Preg = NMNMreg - 4;
796 uint32_t oldPval = nv32_rd(pScrn, Preg);
797 uint32_t NMNM = NM2 << 16 | NM1;
798 uint32_t Pval = (oldPval & ((Preg == 0x4020) ? ~(0x11 << 16) : ~(1 << 16))) | 0xc << 28 | log2P << 16;
799 uint32_t saved4600 = 0;
800 /* some cards have different maskc040s */
801 uint32_t maskc040 = ~(3 << 14), savedc040;
803 if (nv32_rd(pScrn, NMNMreg) == NMNM && (oldPval & 0xc0070000) == Pval)
809 maskc040 = ~(3 << 26);
811 if (Preg == 0x4020) {
812 struct pll_lims pll_lim;
815 if (!get_pll_limits(pScrn, Preg, &pll_lim))
818 Pval2 = log2P + pll_lim.log2p_bias;
819 if (Pval2 > pll_lim.max_log2p_bias)
820 Pval2 = pll_lim.max_log2p_bias;
821 Pval |= 1 << 28 | Pval2 << 20;
823 saved4600 = nv32_rd(pScrn, 0x4600);
824 nv32_wr(pScrn, 0x4600, saved4600 | 1 << 31);
827 nv32_wr(pScrn, Preg, oldPval | 1 << 28);
828 nv32_wr(pScrn, Preg, Pval & ~(1 << 30));
829 if (Preg == 0x4020) {
830 Pval |= 1 << 23 | 1 << 12;
831 nv32_wr(pScrn, 0x4020, Pval & ~(3 << 30));
832 nv32_wr(pScrn, 0x4038, Pval & ~(3 << 30));
835 savedc040 = nv32_rd(pScrn, 0xc040);
836 nv32_wr(pScrn, 0xc040, savedc040 & maskc040);
838 nv32_wr(pScrn, NMNMreg, NMNM);
839 if (NMNMreg == 0x4024)
840 nv32_wr(pScrn, 0x403c, NMNM);
842 nv32_wr(pScrn, Preg, Pval);
843 if (Preg == 0x4020) {
845 nv32_wr(pScrn, 0x4020, Pval);
846 nv32_wr(pScrn, 0x4038, Pval);
847 nv32_wr(pScrn, 0x4600, saved4600);
850 nv32_wr(pScrn, 0xc040, savedc040);
852 if (Preg == 0x4020) {
853 nv32_wr(pScrn, 0x4020, Pval & ~(1 << 28));
854 nv32_wr(pScrn, 0x4038, Pval & ~(1 << 28));
858 static void setPLL(ScrnInfoPtr pScrn, bios_t *bios, uint32_t reg, uint32_t clk)
861 struct pll_lims pll_lim;
862 int NM1 = 0xbeef, NM2 = 0xdead, log2P;
864 /* high regs (such as in the mac g5 table) are not -= 4 */
865 if (!get_pll_limits(pScrn, reg > 0x405c ? reg : reg - 4, &pll_lim))
868 if (bios->chip_version >= 0x40 || bios->chip_version == 0x31 || bios->chip_version == 0x36) {
869 getMNP_double(pScrn, &pll_lim, clk, &NM1, &NM2, &log2P);
871 setPLL_double_highregs(pScrn, reg, NM1, NM2, log2P);
873 setPLL_double_lowregs(pScrn, reg, NM1, NM2, log2P);
875 getMNP_single(pScrn, &pll_lim, clk, &NM1, &log2P);
876 setPLL_single(pScrn, reg, NM1, log2P);
881 static bool init_prog(ScrnInfoPtr pScrn, bios_t *bios, CARD16 offset, init_exec_t *iexec)
883 /* INIT_PROG opcode: 0x31
885 * offset (8 bit): opcode
886 * offset + 1 (32 bit): reg
887 * offset + 5 (32 bit): and mask
888 * offset + 9 (8 bit): shift right
889 * offset + 10 (8 bit): number of configurations
890 * offset + 11 (32 bit): register
891 * offset + 15 (32 bit): configuration 1
894 * Starting at offset + 15 there are "number of configurations"
895 * 32 bit values. To find out which configuration value to use
896 * read "CRTC reg" on the CRTC controller with index "CRTC index"
897 * and bitwise AND this value with "and mask" and then bit shift the
898 * result "shift right" bits to the right.
899 * Assign "register" with appropriate configuration value.
902 CARD32 reg = *((CARD32 *) (&bios->data[offset + 1]));
903 CARD32 and = *((CARD32 *) (&bios->data[offset + 5]));
904 CARD8 shiftr = *((CARD8 *) (&bios->data[offset + 9]));
905 CARD8 nr = *((CARD8 *) (&bios->data[offset + 10]));
906 CARD32 reg2 = *((CARD32 *) (&bios->data[offset + 11]));
908 CARD32 configval, tmp;
910 if (iexec->execute) {
911 xf86DrvMsg(pScrn->scrnIndex, X_INFO, "0x%04X: REG: 0x%04X\n", offset,
914 tmp = nv32_rd(pScrn, reg);
915 configuration = (tmp & and) >> shiftr;
917 xf86DrvMsg(pScrn->scrnIndex, X_INFO, "0x%04X: CONFIGURATION TO USE: 0x%02X\n",
918 offset, configuration);
920 if (configuration <= nr) {
923 *((CARD32 *) (&bios->data[offset + 15 + configuration * 4]));
925 xf86DrvMsg(pScrn->scrnIndex, X_INFO, "0x%04X: REG: 0x%08X, VALUE: 0x%08X\n", offset,
928 tmp = nv32_rd(pScrn, reg2);
929 xf86DrvMsg(pScrn->scrnIndex, X_INFO, "0x%04X: CURRENT VALUE IS: 0x%08X\n",
931 nv32_wr(pScrn, reg2, configval);
938 static bool init_io_restrict_prog(ScrnInfoPtr pScrn, bios_t *bios, uint16_t offset, init_exec_t *iexec)
940 /* INIT_IO_RESTRICT_PROG opcode: 0x32 ('2')
942 * offset (8 bit): opcode
943 * offset + 1 (16 bit): CRTC port
944 * offset + 3 (8 bit): CRTC index
945 * offset + 4 (8 bit): mask
946 * offset + 5 (8 bit): shift
947 * offset + 6 (8 bit): count
948 * offset + 7 (32 bit): register
949 * offset + 11 (32 bit): configuration 1
952 * Starting at offset + 11 there are "count" 32 bit values.
953 * To find out which value to use read index "CRTC index" on "CRTC port",
954 * AND this value with "mask" and then bit shift right "shift" bits.
955 * Read the appropriate value using this index and write to "register"
958 uint16_t crtcport = le16_to_cpu(*((uint16_t *)(&bios->data[offset + 1])));
959 uint8_t crtcindex = bios->data[offset + 3];
960 uint8_t mask = bios->data[offset + 4];
961 uint8_t shift = bios->data[offset + 5];
962 uint8_t count = bios->data[offset + 6];
963 uint32_t reg = le32_to_cpu(*((uint32_t *)(&bios->data[offset + 7])));
971 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
972 "0x%04X: Port: 0x%04X, Index: 0x%02X, Mask: 0x%02X, Shift: 0x%02X, Count: 0x%02X, Reg: 0x%08X\n",
973 offset, crtcport, crtcindex, mask, shift, count, reg);
975 config = (nv_idx_port_rd(pScrn, crtcport, crtcindex) & mask) >> shift;
976 if (config > count) {
977 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
978 "0x%04X: Config 0x%02X exceeds maximal bound 0x%02X\n",
979 offset, config, count);
983 configval = le32_to_cpu(*((uint32_t *)(&bios->data[offset + 11 + config * 4])));
986 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
987 "0x%04X: Writing config %02X\n", offset, config);
989 nv32_wr(pScrn, reg, configval);
994 static bool init_repeat(ScrnInfoPtr pScrn, bios_t *bios, uint16_t offset, init_exec_t *iexec)
996 /* INIT_REPEAT opcode: 0x33 ('3')
998 * offset (8 bit): opcode
999 * offset + 1 (8 bit): count
1001 * Execute script following this opcode up to INIT_REPEAT_END
1005 uint8_t count = bios->data[offset + 1];
1008 /* no iexec->execute check by design */
1010 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
1011 "0x%04X: REPEATING FOLLOWING SEGMENT %d TIMES\n",
1014 iexec->repeat = true;
1016 /* count - 1, as the script block will execute once when we leave this
1017 * opcode -- this is compatible with bios behaviour as:
1018 * a) the block is always executed at least once, even if count == 0
1019 * b) the bios interpreter skips to the op following INIT_END_REPEAT,
1022 for (i = 0; i < count - 1; i++)
1023 parse_init_table(pScrn, bios, offset + 2, iexec);
1025 iexec->repeat = false;
1030 static bool init_io_restrict_pll(ScrnInfoPtr pScrn, bios_t *bios, uint16_t offset, init_exec_t *iexec)
1032 /* INIT_IO_RESTRICT_PLL opcode: 0x34 ('4')
1034 * offset (8 bit): opcode
1035 * offset + 1 (16 bit): CRTC port
1036 * offset + 3 (8 bit): CRTC index
1037 * offset + 4 (8 bit): mask
1038 * offset + 5 (8 bit): shift
1039 * offset + 6 (8 bit): IO flag condition index
1040 * offset + 7 (8 bit): count
1041 * offset + 8 (32 bit): register
1042 * offset + 12 (16 bit): frequency 1
1045 * Starting at offset + 12 there are "count" 16 bit frequencies (10kHz).
1046 * Set PLL register "register" to coefficients for frequency n,
1047 * selected by reading index "CRTC index" of "CRTC port" ANDed with
1048 * "mask" and shifted right by "shift". If "IO flag condition index" > 0,
1049 * and condition met, double frequency before setting it.
1052 uint16_t crtcport = le16_to_cpu(*((uint16_t *)(&bios->data[offset + 1])));
1053 uint8_t crtcindex = bios->data[offset + 3];
1054 uint8_t mask = bios->data[offset + 4];
1055 uint8_t shift = bios->data[offset + 5];
1056 int8_t io_flag_condition_idx = bios->data[offset + 6];
1057 uint8_t count = bios->data[offset + 7];
1058 uint32_t reg = le32_to_cpu(*((uint32_t *)(&bios->data[offset + 8])));
1062 if (!iexec->execute)
1065 if (DEBUGLEVEL >= 6)
1066 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
1067 "0x%04X: Port: 0x%04X, Index: 0x%02X, Mask: 0x%02X, Shift: 0x%02X, IO Flag Condition: 0x%02X, Count: 0x%02X, Reg: 0x%08X\n",
1068 offset, crtcport, crtcindex, mask, shift, io_flag_condition_idx, count, reg);
1070 config = (nv_idx_port_rd(pScrn, crtcport, crtcindex) & mask) >> shift;
1071 if (config > count) {
1072 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
1073 "0x%04X: Config 0x%02X exceeds maximal bound 0x%02X\n",
1074 offset, config, count);
1078 freq = le16_to_cpu(*((uint16_t *)(&bios->data[offset + 12 + config * 2])));
1080 if (io_flag_condition_idx > 0) {
1081 if (io_flag_condition(pScrn, bios, offset, io_flag_condition_idx)) {
1082 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
1083 "0x%04X: CONDITION FULFILLED - FREQ DOUBLED\n", offset);
1086 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
1087 "0x%04X: CONDITION IS NOT FULFILLED. FREQ UNCHANGED\n", offset);
1090 if (DEBUGLEVEL >= 6)
1091 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
1092 "0x%04X: Reg: 0x%08X, Config: 0x%02X, Freq: %d0kHz\n",
1093 offset, reg, config, freq);
1095 setPLL(pScrn, bios, reg, freq * 10);
1100 static bool init_end_repeat(ScrnInfoPtr pScrn, bios_t *bios, uint16_t offset, init_exec_t *iexec)
1102 /* INIT_END_REPEAT opcode: 0x36 ('6')
1104 * offset (8 bit): opcode
1106 * Marks the end of the block for INIT_REPEAT to repeat
1109 /* no iexec->execute check by design */
1111 /* iexec->repeat flag necessary to go past INIT_END_REPEAT opcode when
1112 * we're not in repeat mode
1120 static bool init_copy(ScrnInfoPtr pScrn, bios_t *bios, uint16_t offset, init_exec_t *iexec)
1122 /* INIT_COPY opcode: 0x37 ('7')
1124 * offset (8 bit): opcode
1125 * offset + 1 (32 bit): register
1126 * offset + 5 (8 bit): shift
1127 * offset + 6 (8 bit): srcmask
1128 * offset + 7 (16 bit): CRTC port
1129 * offset + 9 (8 bit): CRTC index
1130 * offset + 10 (8 bit): mask
1132 * Read index "CRTC index" on "CRTC port", AND with "mask", OR with
1133 * (REGVAL("register") >> "shift" & "srcmask") and write-back to CRTC port
1136 uint32_t reg = le32_to_cpu(*((uint32_t *)(&bios->data[offset + 1])));
1137 uint8_t shift = bios->data[offset + 5];
1138 uint8_t srcmask = bios->data[offset + 6];
1139 uint16_t crtcport = le16_to_cpu(*((uint16_t *)(&bios->data[offset + 7])));
1140 uint8_t crtcindex = bios->data[offset + 9];
1141 uint8_t mask = bios->data[offset + 10];
1145 if (!iexec->execute)
1148 if (DEBUGLEVEL >= 6)
1149 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
1150 "0x%04X: Reg: 0x%08X, Shift: 0x%02X, SrcMask: 0x%02X, Port: 0x%04X, Index: 0x%02X, Mask: 0x%02X\n",
1151 offset, reg, shift, srcmask, crtcport, crtcindex, mask);
1153 data = nv32_rd(pScrn, reg);
1158 data <<= (0x100 - shift);
1162 crtcdata = (nv_idx_port_rd(pScrn, crtcport, crtcindex) & mask) | (uint8_t)data;
1163 nv_idx_port_wr(pScrn, crtcport, crtcindex, crtcdata);
1168 static bool init_not(ScrnInfoPtr pScrn, bios_t *bios, uint16_t offset, init_exec_t *iexec)
1170 /* INIT_NOT opcode: 0x38 ('8')
1172 * offset (8 bit): opcode
1174 * Invert the current execute / no-execute condition (i.e. "else")
1177 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
1178 "0x%04X: ------ SKIPPING FOLLOWING COMMANDS ------\n", offset);
1180 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
1181 "0x%04X: ------ EXECUTING FOLLOWING COMMANDS ------\n", offset);
1183 iexec->execute = !iexec->execute;
1187 static bool init_io_flag_condition(ScrnInfoPtr pScrn, bios_t *bios, uint16_t offset, init_exec_t *iexec)
1189 /* INIT_IO_FLAG_CONDITION opcode: 0x39 ('9')
1191 * offset (8 bit): opcode
1192 * offset + 1 (8 bit): condition number
1194 * Check condition "condition number" in the IO flag condition table.
1195 * If condition not met skip subsequent opcodes until condition is
1196 * inverted (INIT_NOT), or we hit INIT_RESUME
1199 uint8_t cond = bios->data[offset + 1];
1201 if (!iexec->execute)
1204 if (io_flag_condition(pScrn, bios, offset, cond))
1205 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
1206 "0x%04X: CONDITION FULFILLED - CONTINUING TO EXECUTE\n", offset);
1208 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
1209 "0x%04X: CONDITION IS NOT FULFILLED\n", offset);
1210 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
1211 "0x%04X: ------ SKIPPING FOLLOWING COMMANDS ------\n", offset);
1212 iexec->execute = false;
1218 static bool init_idx_addr_latched(ScrnInfoPtr pScrn, bios_t *bios, uint16_t offset, init_exec_t *iexec)
1220 /* INIT_INDEX_ADDRESS_LATCHED opcode: 0x49 ('I')
1222 * offset (8 bit): opcode
1223 * offset + 1 (32 bit): control register
1224 * offset + 5 (32 bit): data register
1225 * offset + 9 (32 bit): mask
1226 * offset + 13 (32 bit): data
1227 * offset + 17 (8 bit): count
1228 * offset + 18 (8 bit): address 1
1229 * offset + 19 (8 bit): data 1
1232 * For each of "count" address and data pairs, write "data n" to "data register",
1233 * read the current value of "control register", and write it back once ANDed
1234 * with "mask", ORed with "data", and ORed with "address n"
1237 uint32_t controlreg = le32_to_cpu(*((uint32_t *)(&bios->data[offset + 1])));
1238 uint32_t datareg = le32_to_cpu(*((uint32_t *)(&bios->data[offset + 5])));
1239 uint32_t mask = le32_to_cpu(*((uint32_t *)(&bios->data[offset + 9])));
1240 uint32_t data = le32_to_cpu(*((uint32_t *)(&bios->data[offset + 13])));
1241 uint8_t count = bios->data[offset + 17];
1245 if (!iexec->execute)
1248 if (DEBUGLEVEL >= 6)
1249 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
1250 "0x%04X: ControlReg: 0x%08X, DataReg: 0x%08X, Mask: 0x%08X, Data: 0x%08X, Count: 0x%02X\n",
1251 offset, controlreg, datareg, mask, data, count);
1253 for (i = 0; i < count; i++) {
1254 uint8_t instaddress = bios->data[offset + 18 + i * 2];
1255 uint8_t instdata = bios->data[offset + 19 + i * 2];
1257 if (DEBUGLEVEL >= 6)
1258 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
1259 "0x%04X: Address: 0x%02X, Data: 0x%02X\n", offset, instaddress, instdata);
1261 nv32_wr(pScrn, datareg, instdata);
1262 value = (nv32_rd(pScrn, controlreg) & mask) | data | instaddress;
1263 nv32_wr(pScrn, controlreg, value);
1269 static bool init_io_restrict_pll2(ScrnInfoPtr pScrn, bios_t *bios, uint16_t offset, init_exec_t *iexec)
1271 /* INIT_IO_RESTRICT_PLL2 opcode: 0x4A ('J')
1273 * offset (8 bit): opcode
1274 * offset + 1 (16 bit): CRTC port
1275 * offset + 3 (8 bit): CRTC index
1276 * offset + 4 (8 bit): mask
1277 * offset + 5 (8 bit): shift
1278 * offset + 6 (8 bit): count
1279 * offset + 7 (32 bit): register
1280 * offset + 11 (32 bit): frequency 1
1283 * Starting at offset + 11 there are "count" 32 bit frequencies (kHz).
1284 * Set PLL register "register" to coefficients for frequency n,
1285 * selected by reading index "CRTC index" of "CRTC port" ANDed with
1286 * "mask" and shifted right by "shift".
1289 uint16_t crtcport = le16_to_cpu(*((uint16_t *)(&bios->data[offset + 1])));
1290 uint8_t crtcindex = bios->data[offset + 3];
1291 uint8_t mask = bios->data[offset + 4];
1292 uint8_t shift = bios->data[offset + 5];
1293 uint8_t count = bios->data[offset + 6];
1294 uint32_t reg = le32_to_cpu(*((uint32_t *)(&bios->data[offset + 7])));
1298 if (!iexec->execute)
1301 if (DEBUGLEVEL >= 6)
1302 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
1303 "0x%04X: Port: 0x%04X, Index: 0x%02X, Mask: 0x%02X, Shift: 0x%02X, Count: 0x%02X, Reg: 0x%08X\n",
1304 offset, crtcport, crtcindex, mask, shift, count, reg);
1309 config = (nv_idx_port_rd(pScrn, crtcport, crtcindex) & mask) >> shift;
1310 if (config > count) {
1311 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
1312 "0x%04X: Config 0x%02X exceeds maximal bound 0x%02X\n",
1313 offset, config, count);
1317 freq = le32_to_cpu(*((uint32_t *)(&bios->data[offset + 11 + config * 4])));
1319 if (DEBUGLEVEL >= 6)
1320 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
1321 "0x%04X: Reg: 0x%08X, Config: 0x%02X, Freq: %dkHz\n",
1322 offset, reg, config, freq);
1324 setPLL(pScrn, bios, reg, freq);
1329 static bool init_pll2(ScrnInfoPtr pScrn, bios_t *bios, uint16_t offset, init_exec_t *iexec)
1331 /* INIT_PLL2 opcode: 0x4B ('K')
1333 * offset (8 bit): opcode
1334 * offset + 1 (32 bit): register
1335 * offset + 5 (32 bit): freq
1337 * Set PLL register "register" to coefficients for frequency "freq"
1340 uint32_t reg = le32_to_cpu(*((uint32_t *)(&bios->data[offset + 1])));
1341 uint32_t freq = le32_to_cpu(*((uint32_t *)(&bios->data[offset + 5])));
1343 if (!iexec->execute)
1346 if (DEBUGLEVEL >= 6)
1347 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
1348 "0x%04X: Reg: 0x%04X, Freq: %dkHz\n",
1351 setPLL(pScrn, bios, reg, freq);
1356 static uint32_t get_tmds_index_reg(ScrnInfoPtr pScrn, uint8_t mlv)
1358 /* For mlv < 0x80, it is an index into a table of TMDS base addresses
1359 * For mlv == 0x80 use the "or" value of the dcb_entry indexed by CR58 for CR57 = 0
1360 * to index a table of offsets to the basic 0x6808b0 address
1361 * For mlv == 0x81 use the "or" value of the dcb_entry indexed by CR58 for CR57 = 0
1362 * to index a table of offsets to the basic 0x6808b0 address, and then flip the offset by 8
1365 NVPtr pNv = NVPTR(pScrn);
1366 int pramdac_offset[13] = {0, 0, 0x8, 0, 0x2000, 0, 0, 0, 0x2008, 0, 0, 0, 0x2000};
1367 uint32_t pramdac_table[4] = {0x6808b0, 0x6808b8, 0x6828b0, 0x6828b8};
1370 /* here we assume that the DCB table has already been parsed */
1373 /* This register needs to be written to set index for reading CR58 */
1374 nv_idx_port_wr(pScrn, CRTC_INDEX_COLOR, 0x57, 0);
1375 dcb_entry = nv_idx_port_rd(pScrn, CRTC_INDEX_COLOR, 0x58);
1376 if (dcb_entry > pNv->dcb_table.entries) {
1377 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
1378 "CR58 doesn't have a valid DCB entry currently (%02X)\n", dcb_entry);
1381 dacoffset = pramdac_offset[pNv->dcb_table.entry[dcb_entry].or];
1384 return (0x6808b0 + dacoffset);
1386 if (mlv > (sizeof(pramdac_table) / sizeof(uint32_t))) {
1387 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
1388 "Magic Lookup Value too big (%02X)\n", mlv);
1391 return pramdac_table[mlv];
1395 static bool init_tmds(ScrnInfoPtr pScrn, bios_t *bios, uint16_t offset, init_exec_t *iexec)
1397 /* INIT_TMDS opcode: 0x4F ('O') (non-canon name)
1399 * offset (8 bit): opcode
1400 * offset + 1 (8 bit): magic lookup value
1401 * offset + 2 (8 bit): TMDS address
1402 * offset + 3 (8 bit): mask
1403 * offset + 4 (8 bit): data
1405 * Read the data reg for TMDS address "TMDS address", AND it with mask
1406 * and OR it with data, then write it back
1407 * "magic lookup value" determines which TMDS base address register is used --
1408 * see get_tmds_index_reg()
1411 uint8_t mlv = bios->data[offset + 1];
1412 uint32_t tmdsaddr = bios->data[offset + 2];
1413 uint8_t mask = bios->data[offset + 3];
1414 uint8_t data = bios->data[offset + 4];
1415 uint32_t reg, value;
1417 if (!iexec->execute)
1420 if (DEBUGLEVEL >= 6)
1421 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
1422 "0x%04X: MagicLookupValue: 0x%02X, TMDSAddr: 0x%02X, Mask: 0x%02X, Data: 0x%02X\n",
1423 offset, mlv, tmdsaddr, mask, data);
1425 if (!(reg = get_tmds_index_reg(pScrn, mlv)))
1428 nv32_wr(pScrn, reg, tmdsaddr | 0x10000);
1429 value = (nv32_rd(pScrn, reg + 4) & mask) | data;
1430 nv32_wr(pScrn, reg + 4, value);
1431 nv32_wr(pScrn, reg, tmdsaddr);
1436 static bool init_zm_tmds_group(ScrnInfoPtr pScrn, bios_t *bios, uint16_t offset, init_exec_t *iexec)
1438 /* INIT_ZM_TMDS_GROUP opcode: 0x50 ('P') (non-canon name)
1440 * offset (8 bit): opcode
1441 * offset + 1 (8 bit): magic lookup value
1442 * offset + 2 (8 bit): count
1443 * offset + 3 (8 bit): addr 1
1444 * offset + 4 (8 bit): data 1
1447 * For each of "count" TMDS address and data pairs write "data n" to "addr n"
1448 * "magic lookup value" determines which TMDS base address register is used --
1449 * see get_tmds_index_reg()
1452 uint8_t mlv = bios->data[offset + 1];
1453 uint8_t count = bios->data[offset + 2];
1457 if (!iexec->execute)
1460 if (DEBUGLEVEL >= 6)
1461 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
1462 "0x%04X: MagicLookupValue: 0x%02X, Count: 0x%02X\n",
1463 offset, mlv, count);
1465 if (!(reg = get_tmds_index_reg(pScrn, mlv)))
1468 for (i = 0; i < count; i++) {
1469 uint8_t tmdsaddr = bios->data[offset + 3 + i * 2];
1470 uint8_t tmdsdata = bios->data[offset + 4 + i * 2];
1472 nv32_wr(pScrn, reg + 4, tmdsdata);
1473 nv32_wr(pScrn, reg, tmdsaddr);
1479 static bool init_cr_idx_adr_latch(ScrnInfoPtr pScrn, bios_t *bios, uint16_t offset, init_exec_t *iexec)
1481 /* INIT_CR_INDEX_ADDRESS_LATCHED opcode: 0x51 ('Q')
1483 * offset (8 bit): opcode
1484 * offset + 1 (8 bit): CRTC index1
1485 * offset + 2 (8 bit): CRTC index2
1486 * offset + 3 (8 bit): baseaddr
1487 * offset + 4 (8 bit): count
1488 * offset + 5 (8 bit): data 1
1491 * For each of "count" address and data pairs, write "baseaddr + n" to
1492 * "CRTC index1" and "data n" to "CRTC index2"
1493 * Once complete, restore initial value read from "CRTC index1"
1495 uint8_t crtcindex1 = bios->data[offset + 1];
1496 uint8_t crtcindex2 = bios->data[offset + 2];
1497 uint8_t baseaddr = bios->data[offset + 3];
1498 uint8_t count = bios->data[offset + 4];
1499 uint8_t oldaddr, data;
1502 if (!iexec->execute)
1505 if (DEBUGLEVEL >= 6)
1506 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
1507 "0x%04X: Index1: 0x%02X, Index2: 0x%02X, BaseAddr: 0x%02X, Count: 0x%02X\n",
1508 offset, crtcindex1, crtcindex2, baseaddr, count);
1510 oldaddr = nv_idx_port_rd(pScrn, CRTC_INDEX_COLOR, crtcindex1);
1512 for (i = 0; i < count; i++) {
1513 nv_idx_port_wr(pScrn, CRTC_INDEX_COLOR, crtcindex1, baseaddr + i);
1515 data = bios->data[offset + 5 + i];
1516 nv_idx_port_wr(pScrn, CRTC_INDEX_COLOR, crtcindex2, data);
1519 nv_idx_port_wr(pScrn, CRTC_INDEX_COLOR, crtcindex1, oldaddr);
1524 static bool init_cr(ScrnInfoPtr pScrn, bios_t *bios, uint16_t offset, init_exec_t *iexec)
1526 /* INIT_CR opcode: 0x52 ('R')
1528 * offset (8 bit): opcode
1529 * offset + 1 (8 bit): CRTC index
1530 * offset + 2 (8 bit): mask
1531 * offset + 3 (8 bit): data
1533 * Assign the value of at "CRTC index" ANDed with mask and ORed with data
1534 * back to "CRTC index"
1537 uint8_t crtcindex = bios->data[offset + 1];
1538 uint8_t mask = bios->data[offset + 2];
1539 uint8_t data = bios->data[offset + 3];
1542 if (!iexec->execute)
1545 if (DEBUGLEVEL >= 6)
1546 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
1547 "0x%04X: Index: 0x%02X, Mask: 0x%02X, Data: 0x%02X\n",
1548 offset, crtcindex, mask, data);
1550 value = (nv_idx_port_rd(pScrn, CRTC_INDEX_COLOR, crtcindex) & mask) | data;
1551 nv_idx_port_wr(pScrn, CRTC_INDEX_COLOR, crtcindex, value);
1556 static bool init_zm_cr(ScrnInfoPtr pScrn, bios_t *bios, uint16_t offset, init_exec_t *iexec)
1558 /* INIT_ZM_CR opcode: 0x53 ('S')
1560 * offset (8 bit): opcode
1561 * offset + 1 (8 bit): CRTC index
1562 * offset + 2 (8 bit): value
1564 * Assign "value" to CRTC register with index "CRTC index".
1567 uint8_t crtcindex = le32_to_cpu(*((uint32_t *)(&bios->data[offset + 1])));
1568 uint8_t data = bios->data[offset + 2];
1570 if (!iexec->execute)
1573 nv_idx_port_wr(pScrn, CRTC_INDEX_COLOR, crtcindex, data);
1578 static bool init_zm_cr_group(ScrnInfoPtr pScrn, bios_t *bios, uint16_t offset, init_exec_t *iexec)
1580 /* INIT_ZM_CR_GROUP opcode: 0x54 ('T')
1582 * offset (8 bit): opcode
1583 * offset + 1 (8 bit): count
1584 * offset + 2 (8 bit): CRTC index 1
1585 * offset + 3 (8 bit): value 1
1588 * For "count", assign "value n" to CRTC register with index "CRTC index n".
1591 uint8_t count = bios->data[offset + 1];
1594 if (!iexec->execute)
1597 for (i = 0; i < count; i++)
1598 init_zm_cr(pScrn, bios, offset + 2 + 2 * i - 1, iexec);
1603 static bool init_condition_time(ScrnInfoPtr pScrn, bios_t *bios, uint16_t offset, init_exec_t *iexec)
1605 /* INIT_CONDITION_TIME opcode: 0x56 ('V')
1607 * offset (8 bit): opcode
1608 * offset + 1 (8 bit): condition number
1609 * offset + 2 (8 bit): retries / 50
1611 * Check condition "condition number" in the condition table.
1612 * The condition table entry has 4 bytes for the address of the
1613 * register to check, 4 bytes for a mask and 4 for a test value.
1614 * If condition not met sleep for 2ms, and repeat upto "retries" times.
1615 * If still not met after retries, clear execution flag for this table.
1618 uint8_t cond = bios->data[offset + 1];
1619 uint16_t retries = bios->data[offset + 2];
1620 uint16_t condptr = bios->condition_tbl_ptr + cond * CONDITION_SIZE;
1621 uint32_t reg = le32_to_cpu(*((uint32_t *)(&bios->data[condptr])));
1622 uint32_t mask = le32_to_cpu(*((uint32_t *)(&bios->data[condptr + 4])));
1623 uint32_t cmpval = le32_to_cpu(*((uint32_t *)(&bios->data[condptr + 8])));
1626 if (!iexec->execute)
1631 if (DEBUGLEVEL >= 6)
1632 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
1633 "0x%04X: Cond: 0x%02X, Retries: 0x%02X\n", offset, cond, retries);
1635 for (; retries > 0; retries--) {
1636 data = nv32_rd(pScrn, reg) & mask;
1638 if (DEBUGLEVEL >= 6)
1639 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
1640 "0x%04X: Checking if 0x%08X equals 0x%08X\n",
1641 offset, data, cmpval);
1643 if (data != cmpval) {
1644 if (DEBUGLEVEL >= 6)
1645 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
1646 "0x%04X: Condition not met, sleeping for 2ms\n", offset);
1649 if (DEBUGLEVEL >= 6)
1650 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
1651 "0x%04X: Condition met, continuing\n", offset);
1656 if (data != cmpval) {
1657 if (DEBUGLEVEL >= 6)
1658 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
1659 "0x%04X: Condition still not met, skiping following opcodes\n", offset);
1660 iexec->execute = false;
1666 static bool init_zm_reg_sequence(ScrnInfoPtr pScrn, bios_t *bios, uint16_t offset, init_exec_t *iexec)
1668 /* INIT_ZM_REG_SEQUENCE opcode: 0x58 ('X')
1670 * offset (8 bit): opcode
1671 * offset + 1 (32 bit): base register
1672 * offset + 5 (8 bit): count
1673 * offset + 6 (32 bit): value 1
1676 * Starting at offset + 6 there are "count" 32 bit values.
1677 * For "count" iterations set "base register" + 4 * current_iteration
1678 * to "value current_iteration"
1681 uint32_t basereg = le32_to_cpu(*((uint32_t *)(&bios->data[offset + 1])));
1682 uint32_t count = bios->data[offset + 5];
1685 if (!iexec->execute)
1688 if (DEBUGLEVEL >= 6)
1689 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
1690 "0x%04X: BaseReg: 0x%08X, Count: 0x%02X\n",
1691 offset, basereg, count);
1693 for (i = 0; i < count; i++) {
1694 uint32_t reg = basereg + i * 4;
1695 uint32_t data = le32_to_cpu(*((uint32_t *)(&bios->data[offset + 6 + i * 4])));
1697 nv32_wr(pScrn, reg, data);
1704 static bool init_indirect_reg(ScrnInfoPtr pScrn, bios_t *bios, CARD16 offset, init_exec_t *iexec)
1706 /* INIT_INDIRECT_REG opcode: 0x5A
1708 * offset (8 bit): opcode
1709 * offset + 1 (32 bit): register
1710 * offset + 5 (16 bit): adress offset (in bios)
1712 * Lookup value at offset data in the bios and write it to reg
1714 CARD32 reg = *((CARD32 *) (&bios->data[offset + 1]));
1715 CARD16 data = le16_to_cpu(*((CARD16 *) (&bios->data[offset + 5])));
1716 CARD32 data2 = bios->data[data];
1718 if (iexec->execute) {
1719 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
1720 "0x%04X: REG: 0x%04X, DATA AT: 0x%04X, VALUE IS: 0x%08X\n",
1721 offset, reg, data, data2);
1723 if (DEBUGLEVEL >= 6) {
1725 tmpval = nv32_rd(pScrn, reg);
1726 xf86DrvMsg(pScrn->scrnIndex, X_INFO, "0x%04X: CURRENT VALUE IS: 0x%08X\n", offset, tmpval);
1729 nv32_wr(pScrn, reg, data2);
1735 static bool init_sub_direct(ScrnInfoPtr pScrn, bios_t *bios, uint16_t offset, init_exec_t *iexec)
1737 /* INIT_SUB_DIRECT opcode: 0x5B ('[')
1739 * offset (8 bit): opcode
1740 * offset + 1 (16 bit): subroutine offset (in bios)
1742 * Calls a subroutine that will execute commands until INIT_DONE
1746 uint16_t sub_offset = le16_to_cpu(*((uint16_t *)(&bios->data[offset + 1])));
1748 if (!iexec->execute)
1751 xf86DrvMsg(pScrn->scrnIndex, X_INFO, "0x%04X: EXECUTING SUB-ROUTINE AT 0x%04X\n",
1752 offset, sub_offset);
1754 parse_init_table(pScrn, bios, sub_offset, iexec);
1756 xf86DrvMsg(pScrn->scrnIndex, X_INFO, "0x%04X: END OF SUB-ROUTINE AT 0x%04X\n",
1757 offset, sub_offset);
1762 static bool init_copy_nv_reg(ScrnInfoPtr pScrn, bios_t *bios, uint16_t offset, init_exec_t *iexec)
1764 /* INIT_COPY_NV_REG opcode: 0x5F ('_')
1766 * offset (8 bit): opcode
1767 * offset + 1 (32 bit): src reg
1768 * offset + 5 (8 bit): shift
1769 * offset + 6 (32 bit): src mask
1770 * offset + 10 (32 bit): xor
1771 * offset + 14 (32 bit): dst reg
1772 * offset + 18 (32 bit): dst mask
1774 * Shift REGVAL("src reg") right by (signed) "shift", AND result with
1775 * "src mask", then XOR with "xor". Write this OR'd with
1776 * (REGVAL("dst reg") AND'd with "dst mask") to "dst reg"
1779 uint32_t srcreg = *((uint32_t *)(&bios->data[offset + 1]));
1780 uint8_t shift = bios->data[offset + 5];
1781 uint32_t srcmask = *((uint32_t *)(&bios->data[offset + 6]));
1782 uint32_t xor = *((uint32_t *)(&bios->data[offset + 10]));
1783 uint32_t dstreg = *((uint32_t *)(&bios->data[offset + 14]));
1784 uint32_t dstmask = *((uint32_t *)(&bios->data[offset + 18]));
1785 uint32_t srcvalue, dstvalue;
1787 if (!iexec->execute)
1790 if (DEBUGLEVEL >= 6)
1791 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
1792 "0x%04X: SrcReg: 0x%08X, Shift: 0x%02X, SrcMask: 0x%08X, Xor: 0x%08X, DstReg: 0x%08X, DstMask: 0x%08X\n",
1793 offset, srcreg, shift, srcmask, xor, dstreg, dstmask);
1795 srcvalue = nv32_rd(pScrn, srcreg);
1800 srcvalue <<= (0x100 - shift);
1802 srcvalue = (srcvalue & srcmask) ^ xor;
1804 dstvalue = nv32_rd(pScrn, dstreg) & dstmask;
1806 nv32_wr(pScrn, dstreg, dstvalue | srcvalue);
1811 static bool init_zm_index_io(ScrnInfoPtr pScrn, bios_t *bios, uint16_t offset, init_exec_t *iexec)
1813 /* INIT_ZM_INDEX_IO opcode: 0x62 ('b')
1815 * offset (8 bit): opcode
1816 * offset + 1 (16 bit): CRTC port
1817 * offset + 3 (8 bit): CRTC index
1818 * offset + 4 (8 bit): data
1820 * Write "data" to index "CRTC index" of "CRTC port"
1822 uint16_t crtcport = le16_to_cpu(*((uint16_t *)(&bios->data[offset + 1])));
1823 uint8_t crtcindex = bios->data[offset + 3];
1824 uint8_t data = bios->data[offset + 4];
1826 if (!iexec->execute)
1829 nv_idx_port_wr(pScrn, crtcport, crtcindex, data);
1834 static bool init_compute_mem(ScrnInfoPtr pScrn, bios_t *bios, uint16_t offset, init_exec_t *iexec)
1836 /* INIT_COMPUTE_MEM opcode: 0x63 ('c')
1838 * offset (8 bit): opcode
1840 * This opcode is meant to set NV_PFB_CFG0 (0x100200) appropriately so
1841 * that the hardware can correctly calculate how much VRAM it has
1842 * (and subsequently report that value in 0x10020C)
1844 * The implementation of this opcode in general consists of two parts:
1845 * 1) determination of the memory bus width
1846 * 2) determination of how many of the card's RAM pads have ICs attached
1848 * 1) is done by a cunning combination of writes to offsets 0x1c and
1849 * 0x3c in the framebuffer, and seeing whether the written values are
1850 * read back correctly. This then affects bits 4-7 of NV_PFB_CFG0
1852 * 2) is done by a cunning combination of writes to an offset slightly
1853 * less than the maximum memory reported by 0x10020C, then seeing if
1854 * the test pattern can be read back. This then affects bits 12-15 of
1857 * In this context a "cunning combination" may include multiple reads
1858 * and writes to varying locations, often alternating the test pattern
1859 * and 0, doubtless to make sure buffers are filled, residual charges
1860 * on tracks are removed etc.
1862 * Unfortunately, the "cunning combination"s mentioned above, and the
1863 * changes to the bits in NV_PFB_CFG0 differ with nearly every bios
1866 * Therefore, we cheat and assume the value of NV_PFB_CFG0 with which
1867 * we started was correct, and use that instead
1870 /* no iexec->execute check by design */
1872 /* on every card I've seen, this step gets done for us earlier in the init scripts
1873 uint8_t crdata = nv_idx_port_rd(pScrn, SEQ_INDEX, 0x01);
1874 nv_idx_port_wr(pScrn, SEQ_INDEX, 0x01, crdata | 0x20);
1877 /* this also has probably been done in the scripts, but an mmio trace of
1878 * s3 resume shows nvidia doing it anyway (unlike the SEQ_INDEX write)
1880 nv32_wr(pScrn, NV_PFB_REFCTRL, NV_PFB_REFCTRL_VALID_1);
1882 /* write back the saved configuration value */
1883 nv32_wr(pScrn, NV_PFB_CFG0, saved_nv_pfb_cfg0);
1888 static bool init_reset(ScrnInfoPtr pScrn, bios_t *bios, uint16_t offset, init_exec_t *iexec)
1890 /* INIT_RESET opcode: 0x65 ('e')
1892 * offset (8 bit): opcode
1893 * offset + 1 (32 bit): register
1894 * offset + 5 (32 bit): value1
1895 * offset + 9 (32 bit): value2
1897 * Assign "value1" to "register", then assign "value2" to "register"
1900 uint32_t reg = le32_to_cpu(*((uint32_t *)(&bios->data[offset + 1])));
1901 uint32_t value1 = le32_to_cpu(*((uint32_t *)(&bios->data[offset + 5])));
1902 uint32_t value2 = le32_to_cpu(*((uint32_t *)(&bios->data[offset + 9])));
1903 uint32_t pci_nv_19, pci_nv_20;
1905 /* no iexec->execute check by design */
1907 pci_nv_19 = nv32_rd(pScrn, NV_PBUS_PCI_NV_19);
1908 nv32_wr(pScrn, NV_PBUS_PCI_NV_19, 0);
1909 nv32_wr(pScrn, reg, value1);
1913 nv32_wr(pScrn, reg, value2);
1914 nv32_wr(pScrn, NV_PBUS_PCI_NV_19, pci_nv_19);
1916 pci_nv_20 = nv32_rd(pScrn, NV_PBUS_PCI_NV_20);
1917 pci_nv_20 &= ~NV_PBUS_PCI_NV_20_ROM_SHADOW_ENABLED; /* 0xfffffffe */
1918 nv32_wr(pScrn, NV_PBUS_PCI_NV_20, pci_nv_20);
1923 static bool init_configure_mem(ScrnInfoPtr pScrn, bios_t *bios, uint16_t offset, init_exec_t *iexec)
1925 /* INIT_CONFIGURE_MEM opcode: 0x66 ('f')
1927 * offset (8 bit): opcode
1929 * Equivalent to INIT_DONE on bios version 3 or greater.
1930 * For early bios versions, sets up the memory registers, using values
1931 * taken from the memory init table
1934 /* no iexec->execute check by design */
1936 uint16_t meminitoffs = bios->legacy.mem_init_tbl_ptr + MEM_INIT_SIZE * (nv_idx_port_rd(pScrn, CRTC_INDEX_COLOR, NV_VGA_CRTCX_SCRATCH4) >> 4);
1937 uint16_t seqtbloffs = bios->legacy.sdr_seq_tbl_ptr, meminitdata = meminitoffs + 6;
1940 if (bios->major_version > 2)
1943 nv_idx_port_wr(pScrn, SEQ_INDEX, 0x01, nv_idx_port_rd(pScrn, SEQ_INDEX, 0x01) | 0x20);
1945 if (bios->data[meminitoffs] & 1)
1946 seqtbloffs = bios->legacy.ddr_seq_tbl_ptr;
1948 for (reg = le32_to_cpu(*(uint32_t *)&bios->data[seqtbloffs]);
1950 reg = le32_to_cpu(*(uint32_t *)&bios->data[seqtbloffs += 4])) {
1954 data = NV_PFB_PRE_CMD_PRECHARGE;
1957 data = NV_PFB_PAD_CKE_NORMAL;
1960 data = NV_PFB_REF_CMD_REFRESH;
1963 data = le32_to_cpu(*(uint32_t *)&bios->data[meminitdata]);
1965 if (data == 0xffffffff)
1969 nv32_wr(pScrn, reg, data);
1975 static bool init_configure_clk(ScrnInfoPtr pScrn, bios_t *bios, uint16_t offset, init_exec_t *iexec)
1977 /* INIT_CONFIGURE_CLK opcode: 0x67 ('g')
1979 * offset (8 bit): opcode
1981 * Equivalent to INIT_DONE on bios version 3 or greater.
1982 * For early bios versions, sets up the NVClk and MClk PLLs, using
1983 * values taken from the memory init table
1986 /* no iexec->execute check by design */
1988 uint16_t meminitoffs = bios->legacy.mem_init_tbl_ptr + MEM_INIT_SIZE * (nv_idx_port_rd(pScrn, CRTC_INDEX_COLOR, NV_VGA_CRTCX_SCRATCH4) >> 4);
1991 if (bios->major_version > 2)
1994 clock = le16_to_cpu(*(uint16_t *)&bios->data[meminitoffs + 4]) * 10;
1995 setPLL(pScrn, bios, NV_RAMDAC_NVPLL, clock);
1997 clock = le16_to_cpu(*(uint16_t *)&bios->data[meminitoffs + 2]) * 10;
1998 if (bios->data[meminitoffs] & 1) /* DDR */
2000 setPLL(pScrn, bios, NV_RAMDAC_MPLL, clock);
2005 static bool init_configure_preinit(ScrnInfoPtr pScrn, bios_t *bios, uint16_t offset, init_exec_t *iexec)
2007 /* INIT_CONFIGURE_PREINIT opcode: 0x68 ('h')
2009 * offset (8 bit): opcode
2011 * Equivalent to INIT_DONE on bios version 3 or greater.
2012 * For early bios versions, does early init, loading ram and crystal
2013 * configuration from straps into CR3C
2016 /* no iexec->execute check by design */
2018 uint32_t straps = nv32_rd(pScrn, NV_PEXTDEV_BOOT_0);
2019 uint8_t cr3c = ((straps << 2) & 0xf0) | (straps & (1 << 6));
2021 if (bios->major_version > 2)
2024 nv_idx_port_wr(pScrn, CRTC_INDEX_COLOR, NV_VGA_CRTCX_SCRATCH4, cr3c);
2029 static bool init_io(ScrnInfoPtr pScrn, bios_t *bios, uint16_t offset, init_exec_t *iexec)
2031 /* INIT_IO opcode: 0x69 ('i')
2033 * offset (8 bit): opcode
2034 * offset + 1 (16 bit): CRTC port
2035 * offset + 3 (8 bit): mask
2036 * offset + 4 (8 bit): data
2038 * Assign ((IOVAL("crtc port") & "mask") | "data") to "crtc port"
2041 uint16_t crtcport = le16_to_cpu(*((uint16_t *)(&bios->data[offset + 1])));
2042 uint8_t mask = bios->data[offset + 3];
2043 uint8_t data = bios->data[offset + 4];
2045 if (!iexec->execute)
2048 if (DEBUGLEVEL >= 6)
2049 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
2050 "0x%04X: Port: 0x%04X, Mask: 0x%02X, Data: 0x%02X\n",
2051 offset, crtcport, mask, data);
2053 nv_port_wr(pScrn, crtcport, (nv_port_rd(pScrn, crtcport) & mask) | data);
2058 static bool init_sub(ScrnInfoPtr pScrn, bios_t *bios, uint16_t offset, init_exec_t *iexec)
2060 /* INIT_SUB opcode: 0x6B ('k')
2062 * offset (8 bit): opcode
2063 * offset + 1 (8 bit): script number
2065 * Execute script number "script number", as a subroutine
2068 uint8_t sub = bios->data[offset + 1];
2070 if (!iexec->execute)
2073 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
2074 "0x%04X: EXECUTING SUB-SCRIPT %d\n", offset, sub);
2076 parse_init_table(pScrn, bios,
2077 le16_to_cpu(*((uint16_t *)(&bios->data[bios->init_script_tbls_ptr + sub * 2]))),
2080 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
2081 "0x%04X: END OF SUB-SCRIPT %d\n", offset, sub);
2086 static bool init_ram_condition(ScrnInfoPtr pScrn, bios_t *bios, uint16_t offset, init_exec_t *iexec)
2088 /* INIT_RAM_CONDITION opcode: 0x6D ('m')
2090 * offset (8 bit): opcode
2091 * offset + 1 (8 bit): mask
2092 * offset + 2 (8 bit): cmpval
2094 * Test if (NV_PFB_BOOT_0 & "mask") equals "cmpval".
2095 * If condition not met skip subsequent opcodes until condition is
2096 * inverted (INIT_NOT), or we hit INIT_RESUME
2099 uint8_t mask = bios->data[offset + 1];
2100 uint8_t cmpval = bios->data[offset + 2];
2103 if (!iexec->execute)
2106 data = nv32_rd(pScrn, NV_PFB_BOOT_0) & mask;
2108 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
2109 "0x%04X: Checking if 0x%08X equals 0x%08X\n", offset, data, cmpval);
2112 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
2113 "0x%04X: CONDITION FULFILLED - CONTINUING TO EXECUTE\n", offset);
2115 xf86DrvMsg(pScrn->scrnIndex, X_INFO, "0x%04X: CONDITION IS NOT FULFILLED\n", offset);
2116 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
2117 "0x%04X: ------ SKIPPING FOLLOWING COMMANDS ------\n", offset);
2118 iexec->execute = false;
2124 static bool init_nv_reg(ScrnInfoPtr pScrn, bios_t *bios, uint16_t offset, init_exec_t *iexec)
2126 /* INIT_NV_REG opcode: 0x6E ('n')
2128 * offset (8 bit): opcode
2129 * offset + 1 (32 bit): register
2130 * offset + 5 (32 bit): mask
2131 * offset + 9 (32 bit): data
2133 * Assign ((REGVAL("register") & "mask") | "data") to "register"
2136 uint32_t reg = le32_to_cpu(*((uint32_t *)(&bios->data[offset + 1])));
2137 uint32_t mask = le32_to_cpu(*((uint32_t *)(&bios->data[offset + 5])));
2138 uint32_t data = le32_to_cpu(*((uint32_t *)(&bios->data[offset + 9])));
2140 if (!iexec->execute)
2143 if (DEBUGLEVEL >= 6)
2144 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
2145 "0x%04X: Reg: 0x%08X, Mask: 0x%08X, Data: 0x%08X\n",
2146 offset, reg, mask, data);
2148 nv32_wr(pScrn, reg, (nv32_rd(pScrn, reg) & mask) | data);
2153 static bool init_macro(ScrnInfoPtr pScrn, bios_t *bios, uint16_t offset, init_exec_t *iexec)
2155 /* INIT_MACRO opcode: 0x6F ('o')
2157 * offset (8 bit): opcode
2158 * offset + 1 (8 bit): macro number
2160 * Look up macro index "macro number" in the macro index table.
2161 * The macro index table entry has 1 byte for the index in the macro table,
2162 * and 1 byte for the number of times to repeat the macro.
2163 * The macro table entry has 4 bytes for the register address and
2164 * 4 bytes for the value to write to that register
2167 uint8_t macro_index_tbl_idx = bios->data[offset + 1];
2168 uint16_t tmp = bios->macro_index_tbl_ptr + (macro_index_tbl_idx * MACRO_INDEX_SIZE);
2169 uint8_t macro_tbl_idx = bios->data[tmp];
2170 uint8_t count = bios->data[tmp + 1];
2174 if (!iexec->execute)
2177 if (DEBUGLEVEL >= 6)
2178 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
2179 "0x%04X: Macro: 0x%02X, MacroTableIndex: 0x%02X, Count: 0x%02X\n",
2180 offset, macro_index_tbl_idx, macro_tbl_idx, count);
2182 for (i = 0; i < count; i++) {
2183 uint16_t macroentryptr = bios->macro_tbl_ptr + (macro_tbl_idx + i) * MACRO_SIZE;
2185 reg = le32_to_cpu(*((uint32_t *)(&bios->data[macroentryptr])));
2186 data = le32_to_cpu(*((uint32_t *)(&bios->data[macroentryptr + 4])));
2188 nv32_wr(pScrn, reg, data);
2194 static bool init_done(ScrnInfoPtr pScrn, bios_t *bios, uint16_t offset, init_exec_t *iexec)
2196 /* INIT_DONE opcode: 0x71 ('q')
2198 * offset (8 bit): opcode
2200 * End the current script
2203 /* mild retval abuse to stop parsing this table */
2207 static bool init_resume(ScrnInfoPtr pScrn, bios_t *bios, uint16_t offset, init_exec_t *iexec)
2209 /* INIT_RESUME opcode: 0x72 ('r')
2211 * offset (8 bit): opcode
2213 * End the current execute / no-execute condition
2219 iexec->execute = true;
2220 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
2221 "0x%04X: ---- EXECUTING FOLLOWING COMMANDS ----\n", offset);
2227 static bool init_ram_condition2(ScrnInfoPtr pScrn, bios_t *bios, CARD16 offset, init_exec_t *iexec)
2229 /* INIT_RAM_CONDITION2 opcode: 0x73
2231 * offset (8 bit): opcode
2232 * offset + 1 (8 bit): and mask
2233 * offset + 2 (8 bit): cmpval
2235 * Test if (NV_EXTDEV_BOOT & and mask) matches cmpval
2237 NVPtr pNv = NVPTR(pScrn);
2238 CARD32 and = *((CARD32 *) (&bios->data[offset + 1]));
2239 CARD32 cmpval = *((CARD32 *) (&bios->data[offset + 5]));
2242 if (iexec->execute) {
2243 data=(nvReadEXTDEV(pNv, NV_PEXTDEV_BOOT))∧
2245 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
2246 "0x%04X: CHECKING IF REGVAL: 0x%08X equals COND: 0x%08X\n",
2247 offset, data, cmpval);
2249 if (data == cmpval) {
2250 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
2251 "0x%04X: CONDITION FULFILLED - CONTINUING TO EXECUTE\n",
2254 xf86DrvMsg(pScrn->scrnIndex, X_INFO, "0x%04X: CONDITION IS NOT FULFILLED\n", offset);
2255 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
2256 "0x%04X: ------ SKIPPING FOLLOWING COMMANDS ------\n", offset);
2257 iexec->execute = false;
2264 static bool init_time(ScrnInfoPtr pScrn, bios_t *bios, uint16_t offset, init_exec_t *iexec)
2266 /* INIT_TIME opcode: 0x74 ('t')
2268 * offset (8 bit): opcode
2269 * offset + 1 (16 bit): time
2271 * Sleep for "time" microseconds.
2274 uint16_t time = le16_to_cpu(*((uint16_t *)(&bios->data[offset + 1])));
2276 if (!iexec->execute)
2279 if (DEBUGLEVEL >= 6)
2280 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
2281 "0x%04X: Sleeping for 0x%04X microseconds\n", offset, time);
2288 static bool init_condition(ScrnInfoPtr pScrn, bios_t *bios, uint16_t offset, init_exec_t *iexec)
2290 /* INIT_CONDITION opcode: 0x75 ('u')
2292 * offset (8 bit): opcode
2293 * offset + 1 (8 bit): condition number
2295 * Check condition "condition number" in the condition table.
2296 * The condition table entry has 4 bytes for the address of the
2297 * register to check, 4 bytes for a mask and 4 for a test value.
2298 * If condition not met skip subsequent opcodes until condition is
2299 * inverted (INIT_NOT), or we hit INIT_RESUME
2302 uint8_t cond = bios->data[offset + 1];
2303 uint16_t condptr = bios->condition_tbl_ptr + cond * CONDITION_SIZE;
2304 uint32_t reg = le32_to_cpu(*((uint32_t *)(&bios->data[condptr])));
2305 uint32_t mask = le32_to_cpu(*((uint32_t *)(&bios->data[condptr + 4])));
2306 uint32_t cmpval = le32_to_cpu(*((uint32_t *)(&bios->data[condptr + 8])));
2309 if (!iexec->execute)
2312 if (DEBUGLEVEL >= 6)
2313 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
2314 "0x%04X: Cond: 0x%02X, Reg: 0x%08X, Mask: 0x%08X, Cmpval: 0x%08X\n",
2315 offset, cond, reg, mask, cmpval);
2317 data = nv32_rd(pScrn, reg) & mask;
2319 if (DEBUGLEVEL >= 6)
2320 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
2321 "0x%04X: Checking if 0x%08X equals 0x%08X\n",
2322 offset, data, cmpval);
2324 if (data == cmpval) {
2325 if (DEBUGLEVEL >= 6)
2326 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
2327 "0x%04X: CONDITION FULFILLED - CONTINUING TO EXECUTE\n", offset);
2329 if (DEBUGLEVEL >= 6)
2330 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
2331 "0x%04X: CONDITION IS NOT FULFILLED\n", offset);
2332 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
2333 "0x%04X: ------ SKIPPING FOLLOWING COMMANDS ------\n", offset);
2334 iexec->execute = false;
2340 static bool init_index_io(ScrnInfoPtr pScrn, bios_t *bios, uint16_t offset, init_exec_t *iexec)
2342 /* INIT_INDEX_IO opcode: 0x78 ('x')
2344 * offset (8 bit): opcode
2345 * offset + 1 (16 bit): CRTC port
2346 * offset + 3 (8 bit): CRTC index
2347 * offset + 4 (8 bit): mask
2348 * offset + 5 (8 bit): data
2350 * Read value at index "CRTC index" on "CRTC port", AND with "mask", OR with "data", write-back
2353 uint16_t crtcport = le16_to_cpu(*((uint16_t *)(&bios->data[offset + 1])));
2354 uint8_t crtcindex = bios->data[offset + 3];
2355 uint8_t mask = bios->data[offset + 4];
2356 uint8_t data = bios->data[offset + 5];
2359 if (!iexec->execute)
2362 if (DEBUGLEVEL >= 6)
2363 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
2364 "0x%04X: Port: 0x%04X, Index: 0x%02X, Mask: 0x%02X, Data: 0x%02X\n",
2365 offset, crtcport, crtcindex, mask, data);
2367 value = (nv_idx_port_rd(pScrn, crtcport, crtcindex) & mask) | data;
2368 nv_idx_port_wr(pScrn, crtcport, crtcindex, value);
2373 static bool init_pll(ScrnInfoPtr pScrn, bios_t *bios, uint16_t offset, init_exec_t *iexec)
2375 /* INIT_PLL opcode: 0x79 ('y')
2377 * offset (8 bit): opcode
2378 * offset + 1 (32 bit): register
2379 * offset + 5 (16 bit): freq
2381 * Set PLL register "register" to coefficients for frequency (10kHz) "freq"
2384 uint32_t reg = le32_to_cpu(*((uint32_t *)(&bios->data[offset + 1])));
2385 uint16_t freq = le16_to_cpu(*((uint16_t *)(&bios->data[offset + 5])));
2387 if (!iexec->execute)
2390 if (DEBUGLEVEL >= 6)
2391 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
2392 "0x%04X: Reg: 0x%08X, Freq: %d0kHz\n",
2395 setPLL(pScrn, bios, reg, freq * 10);
2400 static bool init_zm_reg(ScrnInfoPtr pScrn, bios_t *bios, uint16_t offset, init_exec_t *iexec)
2402 /* INIT_ZM_REG opcode: 0x7A ('z')
2404 * offset (8 bit): opcode
2405 * offset + 1 (32 bit): register
2406 * offset + 5 (32 bit): value
2408 * Assign "value" to "register"
2411 uint32_t reg = le32_to_cpu(*((uint32_t *)(&bios->data[offset + 1])));
2412 uint32_t value = le32_to_cpu(*((uint32_t *)(&bios->data[offset + 5])));
2414 if (!iexec->execute)
2417 nv32_wr(pScrn, reg, value);
2422 static bool init_8e(ScrnInfoPtr pScrn, bios_t *bios, uint16_t offset, init_exec_t *iexec)
2424 /* INIT_8E opcode: 0x8E ('')
2426 * offset (8 bit): opcode
2428 * The purpose of this opcode is unclear (being for nv50 cards), and
2429 * the literal functionality can be seen in the code below.
2431 * A brief synopsis is that for each entry in a table pointed to by the
2432 * DCB table header, depending on the settings of various bits, various
2433 * other bits in registers 0xe100, 0xe104, and 0xe108, are set or
2437 uint16_t dcbptr = le16_to_cpu(*(uint16_t *)&bios->data[0x36]);
2439 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
2440 "No Display Configuration Block pointer found\n");
2443 if (bios->data[dcbptr] != 0x40) {
2444 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
2445 "DCB table not version 4.0\n");
2448 uint16_t init8etblptr = le16_to_cpu(*(uint16_t *)&bios->data[dcbptr + 10]);
2449 if (!init8etblptr) {
2450 xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
2451 "Invalid pointer to INIT_8E table\n");
2454 uint8_t headerlen = bios->data[init8etblptr + 1];
2455 uint8_t entries = bios->data[init8etblptr + 2];
2456 uint8_t recordlen = bios->data[init8etblptr + 3];
2459 for (i = 0; i < entries; i++) {
2460 uint32_t entry = le32_to_cpu(*(uint32_t *)&bios->data[init8etblptr + headerlen + recordlen * i]);
2461 int shift = (entry & 0x1f) * 4;
2463 uint32_t reg = 0xe104;
2466 if ((entry & 0xff00) == 0xff00)
2475 mask = ~(3 << shift);
2476 if (entry & (1 << 24))
2477 data = (entry >> 21);
2479 data = (entry >> 19);
2480 data = ((data & 3) ^ 2) << shift;
2482 if (DEBUGLEVEL >= 6)
2483 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
2484 "0x%04X: Entry: 0x%08X, Reg: 0x%08X, Shift: 0x%02X, Mask: 0x%08X, Data: 0x%08X\n",
2485 offset, entry, reg, shift, mask, data);
2487 nv32_wr(pScrn, reg, (nv32_rd(pScrn, reg) & mask) | data);
2490 shift = entry & 0x1f;
2492 mask = ~(1 << 16 | 1);
2493 mask = mask << shift | mask >> (32 - shift);
2495 if ((entry & (3 << 25)) == (1 << 25))
2497 if ((entry & (3 << 25)) == (2 << 25))
2501 if (DEBUGLEVEL >= 6)
2502 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
2503 "0x%04X: Entry: 0x%08X, Reg: 0x%08X, Shift: 0x%02X, Mask: 0x%08X, Data: 0x%08X\n",
2504 offset, entry, reg, shift, mask, data);
2506 nv32_wr(pScrn, reg, (nv32_rd(pScrn, reg) & mask) | data);
2512 /* hack to avoid moving the itbl_entry array before this function */
2513 int init_ram_restrict_zm_reg_group_blocklen = 0;
2515 static bool init_ram_restrict_zm_reg_group(ScrnInfoPtr pScrn, bios_t *bios, uint16_t offset, init_exec_t *iexec)
2517 /* INIT_RAM_RESTRICT_ZM_REG_GROUP opcode: 0x8F ('')
2519 * offset (8 bit): opcode
2520 * offset + 1 (32 bit): reg
2521 * offset + 5 (8 bit): regincrement
2522 * offset + 6 (8 bit): count
2523 * offset + 7 (32 bit): value 1,1
2526 * Use the RAMCFG strap of PEXTDEV_BOOT as an index into the table at
2527 * ram_restrict_table_ptr. The value read from here is 'n', and
2528 * "value 1,n" gets written to "reg". This repeats "count" times and on
2529 * each iteration 'm', "reg" increases by "regincrement" and
2530 * "value m,n" is used. The extent of n is limited by a number read
2531 * from the 'M' BIT table, herein called "blocklen"
2534 uint32_t reg = le32_to_cpu(*((uint32_t *)(&bios->data[offset + 1])));
2535 uint8_t regincrement = bios->data[offset + 5];
2536 uint8_t count = bios->data[offset + 6];
2537 uint32_t strap_ramcfg, data;
2542 /* previously set by 'M' BIT table */
2543 blocklen = init_ram_restrict_zm_reg_group_blocklen;
2545 if (!iexec->execute)
2549 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
2550 "0x%04X: Zero block length - has the M table been parsed?\n", offset);
2554 strap_ramcfg = (nv32_rd(pScrn, NV_PEXTDEV_BOOT_0) >> 2) & 0xf;
2555 index = bios->data[bios->ram_restrict_tbl_ptr + strap_ramcfg];
2557 if (DEBUGLEVEL >= 6)
2558 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
2559 "0x%04X: Reg: 0x%08X, RegIncrement: 0x%02X, Count: 0x%02X, StrapRamCfg: 0x%02X, Index: 0x%02X\n",
2560 offset, reg, regincrement, count, strap_ramcfg, index);
2562 for (i = 0; i < count; i++) {
2563 data = le32_to_cpu(*((uint32_t *)(&bios->data[offset + 7 + index * 4 + blocklen * i])));
2565 nv32_wr(pScrn, reg, data);
2567 reg += regincrement;
2573 static bool init_copy_zm_reg(ScrnInfoPtr pScrn, bios_t *bios, uint16_t offset, init_exec_t *iexec)
2575 /* INIT_COPY_ZM_REG opcode: 0x90 ('')
2577 * offset (8 bit): opcode
2578 * offset + 1 (32 bit): src reg
2579 * offset + 5 (32 bit): dst reg
2581 * Put contents of "src reg" into "dst reg"
2584 uint32_t srcreg = le32_to_cpu(*((uint32_t *)(&bios->data[offset + 1])));
2585 uint32_t dstreg = le32_to_cpu(*((uint32_t *)(&bios->data[offset + 5])));
2587 if (!iexec->execute)
2590 nv32_wr(pScrn, dstreg, nv32_rd(pScrn, srcreg));
2595 static bool init_zm_reg_group_addr_latched(ScrnInfoPtr pScrn, bios_t *bios, uint16_t offset, init_exec_t *iexec)
2597 /* INIT_ZM_REG_GROUP_ADDRESS_LATCHED opcode: 0x91 ('')
2599 * offset (8 bit): opcode
2600 * offset + 1 (32 bit): dst reg
2601 * offset + 5 (8 bit): count
2602 * offset + 6 (32 bit): data 1
2605 * For each of "count" values write "data n" to "dst reg"
2608 uint32_t reg = le32_to_cpu(*((uint32_t *)(&bios->data[offset + 1])));
2609 uint8_t count = bios->data[offset + 5];
2612 if (!iexec->execute)
2615 for (i = 0; i < count; i++) {
2616 uint32_t data = le32_to_cpu(*((uint32_t *)(&bios->data[offset + 6 + 4 * i])));
2617 nv32_wr(pScrn, reg, data);
2623 static bool init_reserved(ScrnInfoPtr pScrn, bios_t *bios, uint16_t offset, init_exec_t *iexec)
2625 /* INIT_RESERVED opcode: 0x92 ('')
2627 * offset (8 bit): opcode
2629 * Seemingly does nothing
2635 static init_tbl_entry_t itbl_entry[] = {
2636 /* command name , id , length , offset , mult , command handler */
2637 // { "INIT_PROG" , 0x31, 15 , 10 , 4 , init_prog },
2638 { "INIT_IO_RESTRICT_PROG" , 0x32, 11 , 6 , 4 , init_io_restrict_prog },
2639 { "INIT_REPEAT" , 0x33, 2 , 0 , 0 , init_repeat },
2640 { "INIT_IO_RESTRICT_PLL" , 0x34, 12 , 7 , 2 , init_io_restrict_pll },
2641 { "INIT_END_REPEAT" , 0x36, 1 , 0 , 0 , init_end_repeat },
2642 { "INIT_COPY" , 0x37, 11 , 0 , 0 , init_copy },
2643 { "INIT_NOT" , 0x38, 1 , 0 , 0 , init_not },
2644 { "INIT_IO_FLAG_CONDITION" , 0x39, 2 , 0 , 0 , init_io_flag_condition },
2645 { "INIT_INDEX_ADDRESS_LATCHED" , 0x49, 18 , 17 , 2 , init_idx_addr_latched },
2646 { "INIT_IO_RESTRICT_PLL2" , 0x4A, 11 , 6 , 4 , init_io_restrict_pll2 },
2647 { "INIT_PLL2" , 0x4B, 9 , 0 , 0 , init_pll2 },
2648 /* { "INIT_I2C_BYTE" , 0x4C, x , x , x , init_i2c_byte }, */
2649 /* { "INIT_ZM_I2C_BYTE" , 0x4D, x , x , x , init_zm_i2c_byte }, */
2650 /* { "INIT_ZM_I2C" , 0x4E, x , x , x , init_zm_i2c }, */
2651 { "INIT_TMDS" , 0x4F, 5 , 0 , 0 , init_tmds },
2652 { "INIT_ZM_TMDS_GROUP" , 0x50, 3 , 2 , 2 , init_zm_tmds_group },
2653 { "INIT_CR_INDEX_ADDRESS_LATCHED" , 0x51, 5 , 4 , 1 , init_cr_idx_adr_latch },
2654 { "INIT_CR" , 0x52, 4 , 0 , 0 , init_cr },
2655 { "INIT_ZM_CR" , 0x53, 3 , 0 , 0 , init_zm_cr },
2656 { "INIT_ZM_CR_GROUP" , 0x54, 2 , 1 , 2 , init_zm_cr_group },
2657 { "INIT_CONDITION_TIME" , 0x56, 3 , 0 , 0 , init_condition_time },
2658 { "INIT_ZM_REG_SEQUENCE" , 0x58, 6 , 5 , 4 , init_zm_reg_sequence },
2659 // { "INIT_INDIRECT_REG" , 0x5A, 7 , 0 , 0 , init_indirect_reg },
2660 { "INIT_SUB_DIRECT" , 0x5B, 3 , 0 , 0 , init_sub_direct },
2661 { "INIT_COPY_NV_REG" , 0x5F, 22 , 0 , 0 , init_copy_nv_reg },
2662 { "INIT_ZM_INDEX_IO" , 0x62, 5 , 0 , 0 , init_zm_index_io },
2663 { "INIT_COMPUTE_MEM" , 0x63, 1 , 0 , 0 , init_compute_mem },
2664 { "INIT_RESET" , 0x65, 13 , 0 , 0 , init_reset },
2665 { "INIT_CONFIGURE_MEM" , 0x66, 1 , 0 , 0 , init_configure_mem },
2666 { "INIT_CONFIGURE_CLK" , 0x67, 1 , 0 , 0 , init_configure_clk },
2667 { "INIT_CONFIGURE_PREINIT" , 0x68, 1 , 0 , 0 , init_configure_preinit },
2668 { "INIT_IO" , 0x69, 5 , 0 , 0 , init_io },
2669 { "INIT_SUB" , 0x6B, 2 , 0 , 0 , init_sub },
2670 { "INIT_RAM_CONDITION" , 0x6D, 3 , 0 , 0 , init_ram_condition },
2671 { "INIT_NV_REG" , 0x6E, 13 , 0 , 0 , init_nv_reg },
2672 { "INIT_MACRO" , 0x6F, 2 , 0 , 0 , init_macro },
2673 { "INIT_DONE" , 0x71, 1 , 0 , 0 , init_done },
2674 { "INIT_RESUME" , 0x72, 1 , 0 , 0 , init_resume },
2675 // { "INIT_RAM_CONDITION2" , 0x73, 9 , 0 , 0 , init_ram_condition2 },
2676 { "INIT_TIME" , 0x74, 3 , 0 , 0 , init_time },
2677 { "INIT_CONDITION" , 0x75, 2 , 0 , 0 , init_condition },
2678 /* { "INIT_IO_CONDITION" , 0x76, x , x , x , init_io_condition }, */
2679 { "INIT_INDEX_IO" , 0x78, 6 , 0 , 0 , init_index_io },
2680 { "INIT_PLL" , 0x79, 7 , 0 , 0 , init_pll },
2681 { "INIT_ZM_REG" , 0x7A, 9 , 0 , 0 , init_zm_reg },
2682 { "INIT_8E" , 0x8E, 1 , 0 , 0 , init_8e },
2683 /* INIT_RAM_RESTRICT_ZM_REG_GROUP's mult is loaded by M table in BIT */
2684 { "INIT_RAM_RESTRICT_ZM_REG_GROUP" , 0x8F, 7 , 6 , 0 , init_ram_restrict_zm_reg_group },
2685 { "INIT_COPY_ZM_REG" , 0x90, 9 , 0 , 0 , init_copy_zm_reg },
2686 { "INIT_ZM_REG_GROUP_ADDRESS_LATCHED" , 0x91, 6 , 5 , 4 , init_zm_reg_group_addr_latched },
2687 { "INIT_RESERVED" , 0x92, 1 , 0 , 0 , init_reserved },
2688 { 0 , 0 , 0 , 0 , 0 , 0 }
2691 static unsigned int get_init_table_entry_length(bios_t *bios, unsigned int offset, int i)
2693 /* Calculates the length of a given init table entry. */
2694 return itbl_entry[i].length + bios->data[offset + itbl_entry[i].length_offset]*itbl_entry[i].length_multiplier;
2697 static void parse_init_table(ScrnInfoPtr pScrn, bios_t *bios, unsigned int offset, init_exec_t *iexec)
2699 /* Parses all commands in a init table. */
2701 /* We start out executing all commands found in the
2702 * init table. Some op codes may change the status
2703 * of this variable to SKIP, which will cause
2704 * the following op codes to perform no operation until
2705 * the value is changed back to EXECUTE.
2711 /* Loop until INIT_DONE causes us to break out of the loop
2712 * (or until offset > bios length just in case... )
2713 * (and no more than 10000 iterations just in case... ) */
2714 while ((offset < bios->length) && (count++ < 10000)) {
2715 id = bios->data[offset];
2717 /* Find matching id in itbl_entry */
2718 for (i = 0; itbl_entry[i].name && (itbl_entry[i].id != id); i++)
2721 if (itbl_entry[i].name) {
2722 xf86DrvMsg(pScrn->scrnIndex, X_INFO, "0x%04X: [ (0x%02X) - %s ]\n",
2723 offset, itbl_entry[i].id, itbl_entry[i].name);
2725 /* execute eventual command handler */
2726 if (itbl_entry[i].handler)
2727 if (!(*itbl_entry[i].handler)(pScrn, bios, offset, iexec))
2730 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
2731 "0x%04X: Init table command not found: 0x%02X\n", offset, id);
2735 /* Add the offset of the current command including all data
2736 * of that command. The offset will then be pointing on the
2739 offset += get_init_table_entry_length(bios, offset, i);
2743 static void parse_init_tables(ScrnInfoPtr pScrn, bios_t *bios)
2745 /* Loops and calls parse_init_table() for each present table. */
2749 init_exec_t iexec = {true, false};
2751 while ((table = le16_to_cpu(*((uint16_t *)(&bios->data[bios->init_script_tbls_ptr + i]))))) {
2752 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
2753 "0x%04X: Parsing init table %d\n", table, i / 2);
2754 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
2755 "0x%04X: ------ EXECUTING FOLLOWING COMMANDS ------\n", table);
2757 parse_init_table(pScrn, bios, table, &iexec);
2762 static void link_head_and_output(ScrnInfoPtr pScrn, int head, int dcb_entry)
2764 /* The BIOS scripts don't do this for us, sadly
2765 * Luckily we do know the values ;-)
2767 * head < 0 indicates we wish to force a setting with the overrideval
2768 * (for VT restore etc.)
2771 NVPtr pNv = NVPTR(pScrn);
2772 struct dcb_entry *dcbent = &pNv->dcb_table.entry[dcb_entry];
2773 int preferred_output = (ffs(dcbent->or) & OUTPUT_1) >> 1;
2774 uint8_t tmds04 = 0x80;
2775 uint32_t tmds_ctrl, tmds_ctrl2;
2777 if (head != preferred_output)
2780 if (dcbent->type == OUTPUT_LVDS)
2783 tmds_ctrl = (preferred_output ? NV_PRAMDAC0_SIZE : 0) + NV_RAMDAC_FP_TMDS_CONTROL;
2784 tmds_ctrl2 = (preferred_output ? NV_PRAMDAC0_SIZE : 0) + NV_RAMDAC_FP_TMDS_CONTROL_2;
2786 nv32_wr(pScrn, tmds_ctrl + 4, tmds04);
2787 nv32_wr(pScrn, tmds_ctrl, 0x04);
2789 /* does tmds_ctrl2 need setting at all for OUTPUT_TMDS? */
2790 if (dcbent->type == OUTPUT_TMDS) {
2791 nv32_wr(pScrn, tmds_ctrl2 + 4, 0x0);
2792 nv32_wr(pScrn, tmds_ctrl2, 0x04);
2793 } else if (dcbent->type == OUTPUT_LVDS && pNv->VBIOS.fp.dual_link) {
2794 nv32_wr(pScrn, tmds_ctrl2 + 4, tmds04 ^ 0x08);
2795 nv32_wr(pScrn, tmds_ctrl2, 0x04);
2799 static void call_lvds_manufacturer_script(ScrnInfoPtr pScrn, int head, int dcb_entry, enum LVDS_script script)
2801 NVPtr pNv = NVPTR(pScrn);
2802 bios_t *bios = &pNv->VBIOS;
2803 init_exec_t iexec = {true, false};
2805 uint8_t sub = bios->data[bios->fp.xlated_entry + script] + (bios->fp.link_c_increment && pNv->dcb_table.entry[dcb_entry].or & 4 ? 1 : 0);
2806 uint16_t scriptofs = le16_to_cpu(*((uint16_t *)(&bios->data[bios->init_script_tbls_ptr + sub * 2])));
2807 bool power_off_for_reset;
2808 uint16_t off_on_delay;
2810 if (!bios->fp.xlated_entry || !sub || !scriptofs)
2813 if (script == LVDS_INIT && bios->data[scriptofs] != 'q')
2814 xf86DrvMsg(pScrn->scrnIndex, X_WARNING, "LVDS init script not stubbed\n");
2816 power_off_for_reset = bios->data[bios->fp.xlated_entry] & 1;
2817 off_on_delay = le16_to_cpu(*(uint16_t *)&bios->data[bios->fp.xlated_entry + 7]);
2819 if (script == LVDS_PANEL_ON && bios->fp.reset_after_pclk_change)
2820 call_lvds_manufacturer_script(pScrn, head, dcb_entry, LVDS_RESET);
2821 if (script == LVDS_RESET && power_off_for_reset)
2822 call_lvds_manufacturer_script(pScrn, head, dcb_entry, LVDS_PANEL_OFF);
2824 xf86DrvMsg(pScrn->scrnIndex, X_INFO, "Calling LVDS script %d:\n", script);
2825 nv_idx_port_wr(pScrn, CRTC_INDEX_COLOR, NV_VGA_CRTCX_OWNER,
2826 head ? NV_VGA_CRTCX_OWNER_HEADB : NV_VGA_CRTCX_OWNER_HEADA);
2827 parse_init_table(pScrn, bios, scriptofs, &iexec);
2829 if (script == LVDS_PANEL_OFF)
2830 usleep(off_on_delay * 1000);
2831 if (script == LVDS_RESET) {
2833 /* Powerbook specific quirk */
2834 if ((pNv->Chipset & 0xffff) == 0x0329) {
2835 nv32_wr(pScrn, 0x006828b4, 0x72);
2836 nv32_wr(pScrn, 0x006828b0, 0x02);
2837 nv32_wr(pScrn, 0x00001588, 0);
2840 link_head_and_output(pScrn, head, dcb_entry);
2844 static uint16_t clkcmptable(bios_t *bios, uint16_t clktable, int pxclk)
2846 int compare_record_len, i = 0;
2847 uint16_t compareclk, scriptptr = 0;
2849 if (bios->major_version < 5) /* pre BIT */
2850 compare_record_len = 3;
2852 compare_record_len = 4;
2855 compareclk = le16_to_cpu(*((uint16_t *)&bios->data[clktable + compare_record_len * i]));
2856 if (pxclk >= compareclk * 10) {
2857 if (bios->major_version < 5) {
2858 uint8_t tmdssub = bios->data[clktable + 2 + compare_record_len * i];
2859 scriptptr = le16_to_cpu(*((uint16_t *)(&bios->data[bios->init_script_tbls_ptr + tmdssub * 2])));
2861 scriptptr = le16_to_cpu(*((uint16_t *)&bios->data[clktable + 2 + compare_record_len * i]));
2865 } while (compareclk);
2870 static void rundigitaloutscript(ScrnInfoPtr pScrn, uint16_t scriptptr, int head, int dcb_entry)
2872 bios_t *bios = &NVPTR(pScrn)->VBIOS;
2873 init_exec_t iexec = {true, false};
2875 xf86DrvMsg(pScrn->scrnIndex, X_INFO, "0x%04X: Parsing digital output script table\n", scriptptr);
2876 nv_idx_port_wr(pScrn, CRTC_INDEX_COLOR, NV_VGA_CRTCX_OWNER,
2877 head ? NV_VGA_CRTCX_OWNER_HEADB : NV_VGA_CRTCX_OWNER_HEADA);
2878 nv_idx_port_wr(pScrn, CRTC_INDEX_COLOR, NV_VGA_CRTCX_57, 0);
2879 nv_idx_port_wr(pScrn, CRTC_INDEX_COLOR, NV_VGA_CRTCX_58, dcb_entry);
2880 parse_init_table(pScrn, bios, scriptptr, &iexec);
2882 link_head_and_output(pScrn, head, dcb_entry);
2885 static void run_lvds_table(ScrnInfoPtr pScrn, int head, int dcb_entry, enum LVDS_script script, int pxclk)
2887 /* The BIT LVDS table's header has the information to setup the
2888 * necessary registers. Following the standard 4 byte header are:
2889 * A bitmask byte and a dual-link transition pxclk value for use in
2890 * selecting the init script when not using straps; 4 script pointers
2891 * for panel power, selected by output and on/off; and 8 table pointers
2892 * for panel init, the needed one determined by output, and bits in the
2893 * conf byte. These tables are similar to the TMDS tables, consisting
2894 * of a list of pxclks and script pointers.
2897 NVPtr pNv = NVPTR(pScrn);
2898 bios_t *bios = &pNv->VBIOS;
2899 unsigned int outputset = (pNv->dcb_table.entry[dcb_entry].or == 4) ? 1 : 0;
2900 uint16_t scriptptr = 0, clktable;
2901 uint8_t clktableptr = 0;
2903 if (script == LVDS_PANEL_ON && bios->fp.reset_after_pclk_change)
2904 run_lvds_table(pScrn, head, dcb_entry, LVDS_RESET, pxclk);
2905 /* no sign of the "panel off for reset" bit, but it's safer to assume we should */
2906 if (script == LVDS_RESET)
2907 run_lvds_table(pScrn, head, dcb_entry, LVDS_PANEL_OFF, pxclk);
2909 xf86DrvMsg(pScrn->scrnIndex, X_INFO, "Calling LVDS script %d:\n", script);
2911 /* for now we assume version 3.0 table - g80 support will need some changes */
2916 case LVDS_BACKLIGHT_ON: // check applicability of the script for this
2918 scriptptr = le16_to_cpu(*(uint16_t *)&bios->data[bios->fp.lvdsmanufacturerpointer + 7 + outputset * 2]);
2920 case LVDS_BACKLIGHT_OFF: // check applicability of the script for this
2921 case LVDS_PANEL_OFF:
2922 scriptptr = le16_to_cpu(*(uint16_t *)&bios->data[bios->fp.lvdsmanufacturerpointer + 11 + outputset * 2]);
2925 if (pNv->dcb_table.entry[dcb_entry].lvdsconf.use_straps_for_mode) {
2926 if (bios->fp.dual_link)
2928 if (bios->fp.BITbit1)
2931 uint8_t fallback = bios->data[bios->fp.lvdsmanufacturerpointer + 4];
2932 int fallbackcmpval = (pNv->dcb_table.entry[dcb_entry].or == 4) ? 4 : 1;
2934 if (bios->fp.dual_link) {
2936 fallbackcmpval *= 2;
2938 if (fallbackcmpval & fallback)
2942 /* adding outputset * 8 may not be correct */
2943 clktable = le16_to_cpu(*(uint16_t *)&bios->data[bios->fp.lvdsmanufacturerpointer + 15 + clktableptr * 2 + outputset * 8]);
2945 xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "Pixel clock comparison table not found\n");
2948 scriptptr = clkcmptable(bios, clktable, pxclk);
2952 xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "LVDS output init script not found\n");
2955 rundigitaloutscript(pScrn, scriptptr, head, dcb_entry);
2958 void call_lvds_script(ScrnInfoPtr pScrn, int head, int dcb_entry, enum LVDS_script script, int pxclk)
2960 /* LVDS operations are multiplexed in an effort to present a single API
2961 * which works with two vastly differing underlying structures.
2962 * This acts as the demux
2965 bios_t *bios = &NVPTR(pScrn)->VBIOS;
2966 uint8_t lvds_ver = bios->data[bios->fp.lvdsmanufacturerpointer];
2971 if (lvds_ver < 0x30)
2972 call_lvds_manufacturer_script(pScrn, head, dcb_entry, script);
2974 run_lvds_table(pScrn, head, dcb_entry, script, pxclk);
2978 uint16_t fptablepointer;
2979 uint16_t fpxlatetableptr;
2980 uint16_t fpxlatemanufacturertableptr;
2984 static void parse_fp_mode_table(ScrnInfoPtr pScrn, bios_t *bios, struct fppointers *fpp)
2987 uint8_t fptable_ver, headerlen = 0, recordlen, fpentries = 0xf, fpindex;
2990 DisplayModePtr mode;
2992 if (fpp->fptablepointer == 0x0 || fpp->fpxlatetableptr == 0x0) {
2993 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
2994 "Pointers to flat panel table invalid\n");
2998 fptable = &bios->data[fpp->fptablepointer];
3000 fptable_ver = fptable[0];
3002 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
3003 "Found flat panel mode table revision %d.%d\n",
3004 fptable_ver >> 4, fptable_ver & 0xf);
3006 switch (fptable_ver) {
3007 /* BMP version 0x5.0x11 BIOSen have version 1 like tables, but no version field,
3008 * and miss one of the spread spectrum/PWM bytes.
3009 * This could affect early GF2Go parts (not seen any appropriate ROMs though).
3010 * Here we assume that a version of 0x05 matches this case (combining with a
3011 * BMP version check would be better), as the common case for the panel type
3012 * field is 0x0005, and that is in fact what we are reading the first byte of. */
3013 case 0x05: /* some NV10, 11, 15, 16 */
3017 case 0x10: /* some NV15/16, and NV11+ */
3021 case 0x20: /* NV40+ */
3022 headerlen = fptable[1];
3023 recordlen = fptable[2];
3024 fpentries = fptable[3];
3025 /* fptable[4] is the minimum RAMDAC_FP_HCRTC->RAMDAC_FP_HSYNC_START gap.
3026 * Only seen 0x4b (=75) which is what is used in nv_crtc.c anyway,
3027 * so we're not using this table value for now
3032 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
3033 "FP Table revision not currently supported\n");
3037 fpindex = bios->data[fpp->fpxlatetableptr + bios->fp.strapping * fpp->xlatwidth];
3038 bios->fp.strapping |= fpindex << 4;
3039 if (fpindex > fpentries) {
3040 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
3041 "Bad flat panel table index\n");
3045 /* reserved values - means that ddc or hard coded edid should be used */
3046 if (bios->fp.strapping == 0xff) {
3047 xf86DrvMsg(pScrn->scrnIndex, X_INFO, "Ignoring FP table\n");
3051 if (!(mode = xcalloc(1, sizeof(DisplayModeRec))))
3054 modeofs = headerlen + recordlen * fpindex + ofs;
3055 mode->Clock = le16_to_cpu(*(uint16_t *)&fptable[modeofs]) * 10;
3056 mode->HDisplay = le16_to_cpu(*(uint16_t *)&fptable[modeofs + 4] + 1);
3057 mode->HSyncStart = le16_to_cpu(*(uint16_t *)&fptable[modeofs + 10] + 1);
3058 mode->HSyncEnd = le16_to_cpu(*(uint16_t *)&fptable[modeofs + 12] + 1);
3059 mode->HTotal = le16_to_cpu(*(uint16_t *)&fptable[modeofs + 14] + 1);
3060 mode->VDisplay = le16_to_cpu(*(uint16_t *)&fptable[modeofs + 18] + 1);
3061 mode->VSyncStart = le16_to_cpu(*(uint16_t *)&fptable[modeofs + 24] + 1);
3062 mode->VSyncEnd = le16_to_cpu(*(uint16_t *)&fptable[modeofs + 26] + 1);
3063 mode->VTotal = le16_to_cpu(*(uint16_t *)&fptable[modeofs + 28] + 1);
3064 mode->Flags |= (fptable[modeofs + 30] & 0x10) ? V_PHSYNC : V_NHSYNC;
3065 mode->Flags |= (fptable[modeofs + 30] & 0x1) ? V_PVSYNC : V_NVSYNC;
3068 * bytes 1-2 are "panel type", including bits on whether Colour/mono, single/dual link, and type (TFT etc.)
3069 * bytes 3-6 are bits per colour in RGBX
3072 * 13-14 is HValid Start
3073 * 15-16 is HValid End
3074 * bytes 38-39 relate to spread spectrum settings
3075 * bytes 40-43 are something to do with PWM */
3077 mode->prev = mode->next = NULL;
3078 mode->status = MODE_OK;
3079 mode->type = M_T_DRIVER | M_T_PREFERRED;
3080 xf86SetModeDefaultName(mode);
3082 // if (XF86_CRTC_CONFIG_PTR(pScrn)->debug_modes) {
3083 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
3084 "Found flat panel mode in BIOS tables:\n");
3085 xf86PrintModeline(pScrn->scrnIndex, mode);
3088 bios->fp.native_mode = mode;
3091 static void parse_lvds_manufacturer_table_init(ScrnInfoPtr pScrn, bios_t *bios, struct fppointers *fpp)
3093 /* The LVDS table changed considerably with BIT bioses. Previously
3094 * there was a header of version and record length, followed by several
3095 * records, indexed by a seperate xlat table, indexed in turn by the fp
3096 * strap in EXTDEV_BOOT. Each record had a config byte, followed by 6
3097 * script numbers for use by INIT_SUB which controlled panel init and
3098 * power, and finally a dword of ms to sleep between power off and on
3101 * The BIT LVDS table has the typical BIT table header: version byte,
3102 * header length byte, record length byte, and a byte for the maximum
3103 * number of records that can be held in the table. At byte 5 in the
3104 * header is the dual-link transition pxclk (in 10s kHz) - if straps
3105 * are not being used for the panel, this specifies the frequency at
3106 * which modes should be set up in the dual link style.
3108 * The table following the header serves as an integrated config and
3109 * xlat table: the records in the table are indexed by the FP strap
3110 * nibble in EXTDEV_BOOT, and each record has two bytes - the first as
3111 * a config byte, the second for indexing the fp mode table pointed to
3112 * by the BIT 'D' table
3115 unsigned int lvdsmanufacturerindex = 0;
3116 uint8_t lvds_ver, headerlen, recordlen;
3119 bios->fp.strapping = (nv32_rd(pScrn, NV_PEXTDEV_BOOT_0) >> 16) & 0xf;
3121 if (bios->fp.lvdsmanufacturerpointer == 0x0) {
3122 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
3123 "Pointer to LVDS manufacturer table invalid\n");
3127 lvds_ver = bios->data[bios->fp.lvdsmanufacturerpointer];
3129 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
3130 "Found LVDS manufacturer table revision %d.%d\n",
3131 lvds_ver >> 4, lvds_ver & 0xf);
3134 case 0x0a: /* pre NV40 */
3135 lvdsmanufacturerindex = bios->data[fpp->fpxlatemanufacturertableptr + bios->fp.strapping];
3137 /* adjust some things if straps are invalid (implies the panel has EDID) */
3138 if (bios->fp.strapping == 0xf) {
3139 bios->data[fpp->fpxlatetableptr + 0xf] = 0xf;
3140 lvdsmanufacturerindex = bios->fp.if_is_24bit ? 2 : 0;
3141 /* nvidia set the high nibble of (cr57=f, cr58) to
3142 * lvdsmanufacturerindex in this case; we don't */
3146 recordlen = bios->data[bios->fp.lvdsmanufacturerpointer + 1];
3149 case 0x30: /* NV4x */
3150 lvdsmanufacturerindex = bios->fp.strapping;
3151 headerlen = bios->data[bios->fp.lvdsmanufacturerpointer + 1];
3152 if (headerlen < 0x1f) {
3153 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
3154 "LVDS table header not understood\n");
3157 recordlen = bios->data[bios->fp.lvdsmanufacturerpointer + 2];
3159 case 0x40: /* It changed again with gf8 :o( */
3161 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
3162 "LVDS table revision not currently supported\n");
3166 lvdsofs = bios->fp.xlated_entry = bios->fp.lvdsmanufacturerpointer + headerlen + recordlen * lvdsmanufacturerindex;
3169 bios->fp.reset_after_pclk_change = bios->data[lvdsofs] & 2;
3170 bios->fp.dual_link = bios->data[lvdsofs] & 4;
3171 bios->fp.link_c_increment = bios->data[lvdsofs] & 8;
3172 bios->fp.if_is_24bit = bios->data[lvdsofs] & 16;
3173 call_lvds_script(pScrn, 0, 0, LVDS_INIT, 0);
3176 /* My money would be on there being a 24 bit interface bit in this table,
3177 * but I have no example of a laptop bios with a 24 bit panel to confirm that.
3178 * Hence we shout loudly if any bit other than bit 0 is set (I've not even
3181 if (bios->data[lvdsofs] > 1)
3182 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
3183 "You have a very unusual laptop display; please report it\n");
3184 /* no sign of the "reset for panel on" bit, but it's safer to assume we should */
3185 bios->fp.reset_after_pclk_change = true;
3186 bios->fp.dual_link = bios->data[lvdsofs] & 1;
3187 bios->fp.BITbit1 = bios->data[lvdsofs] & 2;
3188 bios->fp.duallink_transition_clk = le16_to_cpu(*(uint16_t *)&bios->data[bios->fp.lvdsmanufacturerpointer + 5]) * 10;
3189 fpp->fpxlatetableptr = bios->fp.lvdsmanufacturerpointer + headerlen + 1;
3190 fpp->xlatwidth = recordlen;
3195 void setup_edid_dual_link_lvds(ScrnInfoPtr pScrn, int pxclk)
3197 /* Due to the stage at which DDC is used, the EDID res for a panel isn't
3198 * known at init, so the dual link flag (which tests against a
3199 * transition frequency) cannot be set until later
3201 * Here the flag and the LVDS script set pointer are updated (only once
3202 * per driver incarnation)
3204 * This function should *not* be called in the case where the panel
3205 * config is set by the straps
3208 bios_t *bios = &NVPTR(pScrn)->VBIOS;
3209 static bool dual_link_correction_done = false;
3211 if ((bios->fp.strapping & 0xf) != 0xf || dual_link_correction_done)
3213 dual_link_correction_done = true;
3215 if (pxclk >= bios->fp.duallink_transition_clk) {
3216 bios->fp.dual_link = true;
3217 /* move to (entry + 1) for BMP bioses (BIT doesn't use this) */
3218 bios->fp.xlated_entry += bios->data[bios->fp.lvdsmanufacturerpointer + 1];
3220 bios->fp.dual_link = false;
3223 void run_tmds_table(ScrnInfoPtr pScrn, int dcb_entry, int head, int pxclk)
3225 /* the dcb_entry parameter is the index of the appropriate DCB entry
3226 * the pxclk parameter is in kHz
3228 * This runs the TMDS regs setting code found on BIT bios cards
3230 * For ffs(or) == 1 use the first table, for ffs(or) == 2 and
3231 * ffs(or) == 3, use the second.
3234 NVPtr pNv = NVPTR(pScrn);
3235 bios_t *bios = &pNv->VBIOS;
3236 uint16_t clktable = 0, scriptptr;
3238 if (pNv->dcb_table.entry[dcb_entry].location) /* off chip */
3241 switch (ffs(pNv->dcb_table.entry[dcb_entry].or)) {
3243 clktable = bios->tmds.output0_script_ptr;
3247 clktable = bios->tmds.output1_script_ptr;
3252 xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "Pixel clock comparison table not found\n");
3256 scriptptr = clkcmptable(bios, clktable, pxclk);
3259 xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "TMDS output init script not found\n");
3263 rundigitaloutscript(pScrn, scriptptr, head, dcb_entry);
3266 static void parse_bios_version(ScrnInfoPtr pScrn, bios_t *bios, uint16_t offset)
3268 /* offset + 0 (8 bits): Micro version
3269 * offset + 1 (8 bits): Minor version
3270 * offset + 2 (8 bits): Chip version
3271 * offset + 3 (8 bits): Major version
3274 bios->major_version = bios->data[offset + 3];
3275 bios->chip_version = bios->data[offset + 2];
3276 xf86DrvMsg(pScrn->scrnIndex, X_INFO, "Bios version %02x.%02x.%02x.%02x\n",
3277 bios->data[offset + 3], bios->data[offset + 2],
3278 bios->data[offset + 1], bios->data[offset]);
3281 bool get_pll_limits(ScrnInfoPtr pScrn, uint32_t limit_match, struct pll_lims *pll_lim)
3285 * Version 0x10: NV31
3286 * One byte header (version), one record of 24 bytes
3287 * Version 0x11: NV36 - Not implemented
3288 * Seems to have same record style as 0x10, but 3 records rather than 1
3289 * Version 0x20: Found on Geforce 6 cards
3290 * Trivial 4 byte BIT header. 31 (0x1f) byte record length
3291 * Version 0x21: Found on Geforce 7, 8 and some Geforce 6 cards
3292 * 5 byte header, fifth byte of unknown purpose. 35 (0x23) byte record length
3295 bios_t *bios = &NVPTR(pScrn)->VBIOS;
3296 uint8_t pll_lim_ver = 0, headerlen = 0, recordlen = 0, entries = 0;
3298 uint32_t crystal_strap_mask, crystal_straps;
3300 if (!bios->pll_limit_tbl_ptr) {
3301 if (bios->chip_version >= 0x40 || bios->chip_version == 0x31 || bios->chip_version == 0x36) {
3302 xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "Pointer to PLL limits table invalid\n");
3306 pll_lim_ver = bios->data[bios->pll_limit_tbl_ptr];
3308 if (DEBUGLEVEL >= 6)
3309 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
3310 "Found PLL limits table version 0x%X\n", pll_lim_ver);
3313 crystal_strap_mask = 1 << 6;
3314 /* open coded pNv->twoHeads test */
3315 if (bios->chip_version > 0x10 && bios->chip_version != 0x15 &&
3316 bios->chip_version != 0x1a && bios->chip_version != 0x20)
3317 crystal_strap_mask |= 1 << 22;
3318 crystal_straps = nv32_rd(pScrn, NV_PEXTDEV_BOOT_0) & crystal_strap_mask;
3320 switch (pll_lim_ver) {
3321 /* we use version 0 to indicate a pre limit table bios (single stage pll)
3322 * and load the hard coded limits instead */
3333 headerlen = bios->data[bios->pll_limit_tbl_ptr + 1];
3334 recordlen = bios->data[bios->pll_limit_tbl_ptr + 2];
3335 entries = bios->data[bios->pll_limit_tbl_ptr + 3];
3338 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
3339 "PLL limits table revision not currently supported\n");
3343 /* initialize all members to zero */
3344 memset(pll_lim, 0, sizeof(struct pll_lims));
3346 if (pll_lim_ver == 0x10) {
3347 uint16_t plloffs = bios->pll_limit_tbl_ptr + headerlen + recordlen * pllindex;
3349 pll_lim->vco1.minfreq = le32_to_cpu(*((uint32_t *)(&bios->data[plloffs])));
3350 pll_lim->vco1.maxfreq = le32_to_cpu(*((uint32_t *)(&bios->data[plloffs + 4])));
3351 pll_lim->vco2.minfreq = le32_to_cpu(*((uint32_t *)(&bios->data[plloffs + 8])));
3352 pll_lim->vco2.maxfreq = le32_to_cpu(*((uint32_t *)(&bios->data[plloffs + 12])));
3353 pll_lim->vco1.min_inputfreq = le32_to_cpu(*((uint32_t *)(&bios->data[plloffs + 16])));
3354 pll_lim->vco2.min_inputfreq = le32_to_cpu(*((uint32_t *)(&bios->data[plloffs + 20])));
3355 pll_lim->vco1.max_inputfreq = pll_lim->vco2.max_inputfreq = INT_MAX;
3357 /* these values taken from nv31. nv30, nv36 might do better with different ones */
3358 pll_lim->vco1.min_n = 0x1;
3359 pll_lim->vco1.max_n = 0xff;
3360 pll_lim->vco1.min_m = 0x1;
3361 pll_lim->vco1.max_m = 0xd;
3362 pll_lim->vco2.min_n = 0x4;
3363 pll_lim->vco2.max_n = 0x46;
3364 if (bios->chip_version == 0x30)
3365 /* only 5 bits available for N2 on nv30 */
3366 pll_lim->vco2.max_n = 0x1f;
3367 if (bios->chip_version == 0x31)
3368 /* on nv31, N2 is compared to maxN2 (0x46) and maxM2 (0x4),
3369 * so set maxN2 to 0x4 and save a comparison
3371 pll_lim->vco2.max_n = 0x4;
3372 pll_lim->vco2.min_m = 0x1;
3373 pll_lim->vco2.max_m = 0x4;
3374 } else if (pll_lim_ver) { /* ver 0x20, 0x21 */
3375 uint16_t plloffs = bios->pll_limit_tbl_ptr + headerlen;
3376 uint32_t reg = 0; /* default match */
3379 /* first entry is default match, if nothing better. warn if reg field nonzero */
3380 if (le32_to_cpu(*((uint32_t *)&bios->data[plloffs])))
3381 xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
3382 "Default PLL limit entry has non-zero register field\n");
3384 if (limit_match > MAX_PLL_TYPES)
3385 /* we've been passed a reg as the match */
3387 else /* limit match is a pll type */
3388 for (i = 1; i < entries && !reg; i++) {
3389 uint32_t cmpreg = le32_to_cpu(*((uint32_t *)(&bios->data[plloffs + recordlen * i])));
3391 if (limit_match == VPLL1 && (cmpreg == NV_RAMDAC_VPLL || cmpreg == 0x4010))
3393 if (limit_match == VPLL2 && (cmpreg == NV_RAMDAC_VPLL2 || cmpreg == 0x4018))
3397 for (i = 1; i < entries; i++)
3398 if (le32_to_cpu(*((uint32_t *)&bios->data[plloffs + recordlen * i])) == reg) {
3403 plloffs += recordlen * pllindex;
3405 if (DEBUGLEVEL >= 6)
3406 xf86DrvMsg(pScrn->scrnIndex, X_INFO, "Loading PLL limits for reg 0x%08x\n",
3407 pllindex ? reg : 0);
3409 /* frequencies are stored in tables in MHz, kHz are more useful, so we convert */
3411 /* What output frequencies can each VCO generate? */
3412 pll_lim->vco1.minfreq = le16_to_cpu(*((uint16_t *)(&bios->data[plloffs + 4]))) * 1000;
3413 pll_lim->vco1.maxfreq = le16_to_cpu(*((uint16_t *)(&bios->data[plloffs + 6]))) * 1000;
3414 pll_lim->vco2.minfreq = le16_to_cpu(*((uint16_t *)(&bios->data[plloffs + 8]))) * 1000;
3415 pll_lim->vco2.maxfreq = le16_to_cpu(*((uint16_t *)(&bios->data[plloffs + 10]))) * 1000;
3417 /* What input frequencies do they accept (past the m-divider)? */
3418 pll_lim->vco1.min_inputfreq = le16_to_cpu(*((uint16_t *)(&bios->data[plloffs + 12]))) * 1000;
3419 pll_lim->vco2.min_inputfreq = le16_to_cpu(*((uint16_t *)(&bios->data[plloffs + 14]))) * 1000;
3420 pll_lim->vco1.max_inputfreq = le16_to_cpu(*((uint16_t *)(&bios->data[plloffs + 16]))) * 1000;
3421 pll_lim->vco2.max_inputfreq = le16_to_cpu(*((uint16_t *)(&bios->data[plloffs + 18]))) * 1000;
3423 /* What values are accepted as multiplier and divider? */
3424 pll_lim->vco1.min_n = bios->data[plloffs + 20];
3425 pll_lim->vco1.max_n = bios->data[plloffs + 21];
3426 pll_lim->vco1.min_m = bios->data[plloffs + 22];
3427 pll_lim->vco1.max_m = bios->data[plloffs + 23];
3428 pll_lim->vco2.min_n = bios->data[plloffs + 24];
3429 pll_lim->vco2.max_n = bios->data[plloffs + 25];
3430 pll_lim->vco2.min_m = bios->data[plloffs + 26];
3431 pll_lim->vco2.max_m = bios->data[plloffs + 27];
3433 pll_lim->unk1c = bios->data[plloffs + 28];
3434 pll_lim->max_log2p_bias = bios->data[plloffs + 29];
3435 pll_lim->log2p_bias = bios->data[plloffs + 30];
3437 if (recordlen > 0x22)
3438 pll_lim->refclk = le32_to_cpu(*((uint32_t *)&bios->data[plloffs + 31]));
3440 /* C51 special not seen elsewhere */
3441 if (bios->chip_version == 0x51 && !pll_lim->refclk) {
3442 uint32_t sel_clk = nv32_rd(pScrn, NV_RAMDAC_SEL_CLK);
3444 if (((limit_match == NV_RAMDAC_VPLL || limit_match == VPLL1) && sel_clk & 0x20) || ((limit_match == NV_RAMDAC_VPLL2 || limit_match == VPLL2) && sel_clk & 0x80)) {
3445 if (nv_idx_port_rd(pScrn, CRTC_INDEX_COLOR, NV_VGA_CRTCX_27) < 0xa3)
3446 pll_lim->refclk = 200000;
3448 pll_lim->refclk = 25000;
3453 /* By now any valid limit table ought to have set a max frequency for
3454 * vco1, so if it's zero it's either a pre limit table bios, or one
3455 * with an empty limit table (seen on nv18)
3457 if (!pll_lim->vco1.maxfreq) {
3458 pll_lim->vco1.minfreq = bios->fminvco;
3459 pll_lim->vco1.maxfreq = bios->fmaxvco;
3460 pll_lim->vco1.min_n = 0x1;
3461 pll_lim->vco1.max_n = 0xff;
3462 pll_lim->vco1.min_m = 0x1;
3463 if (crystal_straps == 0) {
3464 /* nv05 does this, nv11 doesn't, nv10 unknown */
3465 if (bios->chip_version < 0x11)
3466 pll_lim->vco1.min_m = 0x7;
3467 pll_lim->vco1.max_m = 0xd;
3469 if (bios->chip_version < 0x11)
3470 pll_lim->vco1.min_m = 0x8;
3471 pll_lim->vco1.max_m = 0xe;
3473 pll_lim->vco1.min_inputfreq = 0;
3474 pll_lim->vco1.max_inputfreq = INT_MAX;
3477 if (!pll_lim->refclk)
3478 switch (crystal_straps) {
3480 pll_lim->refclk = 13500;
3483 pll_lim->refclk = 14318;
3486 pll_lim->refclk = 27000;
3488 case (1 << 22 | 1 << 6):
3489 pll_lim->refclk = 25000;
3493 #if 0 /* for easy debugging */
3494 ErrorF("pll.vco1.minfreq: %d\n", pll_lim->vco1.minfreq);
3495 ErrorF("pll.vco1.maxfreq: %d\n", pll_lim->vco1.maxfreq);
3496 ErrorF("pll.vco2.minfreq: %d\n", pll_lim->vco2.minfreq);
3497 ErrorF("pll.vco2.maxfreq: %d\n", pll_lim->vco2.maxfreq);
3499 ErrorF("pll.vco1.min_inputfreq: %d\n", pll_lim->vco1.min_inputfreq);
3500 ErrorF("pll.vco1.max_inputfreq: %d\n", pll_lim->vco1.max_inputfreq);
3501 ErrorF("pll.vco2.min_inputfreq: %d\n", pll_lim->vco2.min_inputfreq);
3502 ErrorF("pll.vco2.max_inputfreq: %d\n", pll_lim->vco2.max_inputfreq);
3504 ErrorF("pll.vco1.min_n: %d\n", pll_lim->vco1.min_n);
3505 ErrorF("pll.vco1.max_n: %d\n", pll_lim->vco1.max_n);
3506 ErrorF("pll.vco1.min_m: %d\n", pll_lim->vco1.min_m);
3507 ErrorF("pll.vco1.max_m: %d\n", pll_lim->vco1.max_m);
3508 ErrorF("pll.vco2.min_n: %d\n", pll_lim->vco2.min_n);
3509 ErrorF("pll.vco2.max_n: %d\n", pll_lim->vco2.max_n);
3510 ErrorF("pll.vco2.min_m: %d\n", pll_lim->vco2.min_m);
3511 ErrorF("pll.vco2.max_m: %d\n", pll_lim->vco2.max_m);
3513 ErrorF("pll.unk1c: %d\n", pll_lim->unk1c);
3514 ErrorF("pll.max_log2p_bias: %d\n", pll_lim->max_log2p_bias);
3515 ErrorF("pll.log2p_bias: %d\n", pll_lim->log2p_bias);
3517 ErrorF("pll.refclk: %d\n", pll_lim->refclk);
3523 static int parse_bit_C_tbl_entry(ScrnInfoPtr pScrn, bios_t *bios, bit_entry_t *bitentry)
3525 /* offset + 8 (16 bits): PLL limits table pointer
3527 * There's more in here, but that's unknown.
3530 if (bitentry->length < 10) {
3531 xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "Do not understand BIT C table\n");
3535 bios->pll_limit_tbl_ptr = le16_to_cpu(*((uint16_t *)(&bios->data[bitentry->offset + 8])));
3540 static int parse_bit_display_tbl_entry(ScrnInfoPtr pScrn, bios_t *bios, bit_entry_t *bitentry, struct fppointers *fpp)
3542 /* Parses the flat panel table segment that the bit entry points to.
3543 * Starting at bitentry->offset:
3545 * offset + 0 (16 bits): ??? table pointer - seems to have 18 byte records beginning with a freq
3546 * offset + 2 (16 bits): mode table pointer
3549 if (bitentry->length != 4) {
3550 xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "Do not understand BIT display table\n");
3554 fpp->fptablepointer = le16_to_cpu(*((uint16_t *)(&bios->data[bitentry->offset + 2])));
3556 parse_fp_mode_table(pScrn, bios, fpp);
3561 static unsigned int parse_bit_init_tbl_entry(ScrnInfoPtr pScrn, bios_t *bios, bit_entry_t *bitentry)
3563 /* Parses the init table segment that the bit entry points to.
3564 * Starting at bitentry->offset:
3566 * offset + 0 (16 bits): init script tables pointer
3567 * offset + 2 (16 bits): macro index table pointer
3568 * offset + 4 (16 bits): macro table pointer
3569 * offset + 6 (16 bits): condition table pointer
3570 * offset + 8 (16 bits): io condition table pointer
3571 * offset + 10 (16 bits): io flag condition table pointer
3572 * offset + 12 (16 bits): init function table pointer
3576 if (bitentry->length < 14) {
3577 xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "Do not understand init table\n");
3581 bios->init_script_tbls_ptr = le16_to_cpu(*((uint16_t *)(&bios->data[bitentry->offset])));
3582 bios->macro_index_tbl_ptr = le16_to_cpu(*((uint16_t *)(&bios->data[bitentry->offset + 2])));
3583 bios->macro_tbl_ptr = le16_to_cpu(*((uint16_t *)(&bios->data[bitentry->offset + 4])));
3584 bios->condition_tbl_ptr = le16_to_cpu(*((uint16_t *)(&bios->data[bitentry->offset + 6])));
3585 bios->io_condition_tbl_ptr = le16_to_cpu(*((uint16_t *)(&bios->data[bitentry->offset + 8])));
3586 bios->io_flag_condition_tbl_ptr = le16_to_cpu(*((uint16_t *)(&bios->data[bitentry->offset + 10])));
3587 bios->init_function_tbl_ptr = le16_to_cpu(*((uint16_t *)(&bios->data[bitentry->offset + 12])));
3592 static int parse_bit_i_tbl_entry(ScrnInfoPtr pScrn, bios_t *bios, bit_entry_t *bitentry)
3594 /* BIT 'i' (info?) table
3596 * offset + 0 (32 bits): BIOS version dword (as in B table)
3597 * offset + 5 (8 bits): BIOS feature byte (same as for BMP?)
3598 * offset + 13 (16 bits): pointer to table containing DAC load detection comparison values
3600 * There's other things in the table, purpose unknown
3603 uint16_t daccmpoffset;
3604 uint8_t dacversion, dacheaderlen;
3606 if (bitentry->length < 6) {
3607 xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
3608 "BIT i table not long enough for BIOS version and feature byte\n");
3612 parse_bios_version(pScrn, bios, bitentry->offset);
3614 /* bit 4 seems to indicate a mobile bios, other bits possibly as for BMP feature byte */
3615 bios->feature_byte = bios->data[bitentry->offset + 5];
3617 if (bitentry->length < 15) {
3618 xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
3619 "BIT i table not long enough for DAC load detection comparison table\n");
3623 daccmpoffset = le16_to_cpu(*((uint16_t *)(&bios->data[bitentry->offset + 13])));
3625 /* doesn't exist on g80 */
3629 /* The first value in the table, following the header, is the comparison value
3630 * Purpose of subsequent values unknown -- TV load detection?
3633 dacversion = bios->data[daccmpoffset];
3634 dacheaderlen = bios->data[daccmpoffset + 1];
3636 if (dacversion != 0x00 && dacversion != 0x10) {
3637 xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
3638 "DAC load detection comparison table version %d.%d not known\n",
3639 dacversion >> 4, dacversion & 0xf);
3642 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
3643 "DAC load detection comparison table version %x found\n", dacversion);
3645 bios->dactestval = le32_to_cpu(*((uint32_t *)(&bios->data[daccmpoffset + dacheaderlen])));
3650 static int parse_bit_lvds_tbl_entry(ScrnInfoPtr pScrn, bios_t *bios, bit_entry_t *bitentry, struct fppointers *fpp)
3652 /* Parses the LVDS table segment that the bit entry points to.
3653 * Starting at bitentry->offset:
3655 * offset + 0 (16 bits): LVDS strap xlate table pointer
3658 if (bitentry->length != 2) {
3659 xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "Do not understand BIT LVDS table\n");
3663 /* no idea if it's still called the LVDS manufacturer table, but the concept's close enough */
3664 bios->fp.lvdsmanufacturerpointer = le16_to_cpu(*((uint16_t *)(&bios->data[bitentry->offset])));
3666 parse_lvds_manufacturer_table_init(pScrn, bios, fpp);
3671 static int parse_bit_M_tbl_entry(ScrnInfoPtr pScrn, bios_t *bios, bit_entry_t *bitentry)
3673 /* offset + 2 (8 bits): number of options in an INIT_RAM_RESTRICT_ZM_REG_GROUP opcode option set
3674 * offset + 3 (16 bits): pointer to strap xlate table for RAM restrict option selection
3676 * There's a bunch of bits in this table other than the RAM restrict
3677 * stuff that we don't use - their use currently unknown
3682 /* Older bios versions don't have a sufficiently long table for what we want */
3683 if (bitentry->length < 0x5)
3686 /* set up multiplier for INIT_RAM_RESTRICT_ZM_REG_GROUP */
3687 for (i = 0; itbl_entry[i].name && (itbl_entry[i].id != 0x8f); i++)
3689 itbl_entry[i].length_multiplier = bios->data[bitentry->offset + 2] * 4;
3690 init_ram_restrict_zm_reg_group_blocklen = itbl_entry[i].length_multiplier;
3692 bios->ram_restrict_tbl_ptr = le16_to_cpu(*((uint16_t *)(&bios->data[bitentry->offset + 3])));
3697 static int parse_bit_tmds_tbl_entry(ScrnInfoPtr pScrn, bios_t *bios, bit_entry_t *bitentry)
3699 /* Parses the pointer to the TMDS table
3701 * Starting at bitentry->offset:
3703 * offset + 0 (16 bits): TMDS table pointer
3705 * The TMDS table is typically found just before the DCB table, with a
3706 * characteristic signature of 0x11,0x13 (1.1 being version, 0x13 being
3709 * At offset +7 is a pointer to a script, which I don't know how to run yet
3710 * At offset +9 is a pointer to another script, likewise
3711 * Offset +11 has a pointer to a table where the first word is a pxclk
3712 * frequency and the second word a pointer to a script, which should be
3713 * run if the comparison pxclk frequency is less than the pxclk desired.
3714 * This repeats for decreasing comparison frequencies
3715 * Offset +13 has a pointer to a similar table
3716 * The selection of table (and possibly +7/+9 script) is dictated by
3717 * "or" from the DCB.
3720 uint16_t tmdstableptr, script1, script2;
3722 if (bitentry->length != 2) {
3723 xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "Do not understand BIT TMDS table\n");
3727 tmdstableptr = le16_to_cpu(*((uint16_t *)(&bios->data[bitentry->offset])));
3729 if (tmdstableptr == 0x0) {
3730 xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "Pointer to TMDS table invalid\n");
3734 xf86DrvMsg(pScrn->scrnIndex, X_INFO, "Found TMDS table revision %d.%d\n",
3735 bios->data[tmdstableptr] >> 4, bios->data[tmdstableptr] & 0xf);
3737 /* These two scripts are odd: they don't seem to get run even when they are not stubbed */
3738 script1 = le16_to_cpu(*((uint16_t *)&bios->data[tmdstableptr + 7]));
3739 script2 = le16_to_cpu(*((uint16_t *)&bios->data[tmdstableptr + 9]));
3740 if (bios->data[script1] != 'q' || bios->data[script2] != 'q')
3741 xf86DrvMsg(pScrn->scrnIndex, X_WARNING, "TMDS table script pointers not stubbed\n");
3743 bios->tmds.output0_script_ptr = le16_to_cpu(*((uint16_t *)&bios->data[tmdstableptr + 11]));
3744 bios->tmds.output1_script_ptr = le16_to_cpu(*((uint16_t *)&bios->data[tmdstableptr + 13]));
3749 static void parse_bit_structure(ScrnInfoPtr pScrn, bios_t *bios, const uint16_t bitoffset)
3751 /* parse i first, I next (which needs C & M before it), and L before D */
3752 char parseorder[] = "iCMILDT";
3753 bit_entry_t bitentry;
3755 struct fppointers fpp;
3757 memset(&fpp, 0, sizeof(struct fppointers));
3759 for (i = 0; i < sizeof(parseorder); i++) {
3760 uint16_t offset = bitoffset;
3763 bitentry.id[0] = bios->data[offset];
3764 bitentry.id[1] = bios->data[offset + 1];
3765 bitentry.length = le16_to_cpu(*((uint16_t *)&bios->data[offset + 2]));
3766 bitentry.offset = le16_to_cpu(*((uint16_t *)&bios->data[offset + 4]));
3768 offset += sizeof(bit_entry_t);
3770 if (bitentry.id[0] != parseorder[i])
3773 switch (bitentry.id[0]) {
3775 parse_bit_C_tbl_entry(pScrn, bios, &bitentry);
3778 if (bios->feature_byte & FEATURE_MOBILE)
3779 parse_bit_display_tbl_entry(pScrn, bios, &bitentry, &fpp);
3782 parse_bit_init_tbl_entry(pScrn, bios, &bitentry);
3783 parse_init_tables(pScrn, bios);
3785 case 'i': /* info? */
3786 parse_bit_i_tbl_entry(pScrn, bios, &bitentry);
3789 if (bios->feature_byte & FEATURE_MOBILE)
3790 parse_bit_lvds_tbl_entry(pScrn, bios, &bitentry, &fpp);
3792 case 'M': /* memory? */
3793 parse_bit_M_tbl_entry(pScrn, bios, &bitentry);
3796 parse_bit_tmds_tbl_entry(pScrn, bios, &bitentry);
3800 /* id[0] = 0 and id[1] = 0 => end of BIT struture */
3801 } while (bitentry.id[0] + bitentry.id[1] != 0);
3805 static void parse_bmp_structure(ScrnInfoPtr pScrn, bios_t *bios, unsigned int offset)
3807 /* Parse the BMP structure for useful things
3809 * offset + 5: BMP major version
3810 * offset + 6: BMP minor version
3811 * offset + 10: BCD encoded BIOS version
3813 * offset + 18: init script table pointer (for bios versions < 5.10h)
3814 * offset + 20: extra init script table pointer (for bios versions < 5.10h)
3816 * offset + 24: memory init table pointer (used on early bios versions)
3817 * offset + 26: SDR memory sequencing setup data table
3818 * offset + 28: DDR memory sequencing setup data table
3820 * offset + 54: index of I2C CRTC pair to use for CRT output
3821 * offset + 55: index of I2C CRTC pair to use for TV output
3822 * offset + 56: index of I2C CRTC pair to use for flat panel output
3823 * offset + 58: write CRTC index for I2C pair 0
3824 * offset + 59: read CRTC index for I2C pair 0
3825 * offset + 60: write CRTC index for I2C pair 1
3826 * offset + 61: read CRTC index for I2C pair 1
3828 * offset + 67: maximum internal PLL frequency (single stage PLL)
3829 * offset + 71: minimum internal PLL frequency (single stage PLL)
3831 * offset + 75: script table pointers, as for parse_bit_init_tbl_entry
3833 * offset + 89: TMDS single link output A table pointer
3834 * offset + 91: TMDS single link output B table pointer
3835 * offset + 105: flat panel timings table pointer
3836 * offset + 107: flat panel strapping translation table pointer
3837 * offset + 117: LVDS manufacturer panel config table pointer
3838 * offset + 119: LVDS manufacturer strapping translation table pointer
3840 * offset + 142: PLL limits table pointer
3843 NVPtr pNv = NVPTR(pScrn);
3844 uint8_t bmp_version_major, bmp_version_minor;
3846 struct fppointers fpp;
3847 memset(&fpp, 0, sizeof(struct fppointers));
3849 /* load needed defaults in case we can't parse this info */
3850 pNv->dcb_table.i2c_write[0] = 0x3f;
3851 pNv->dcb_table.i2c_read[0] = 0x3e;
3852 pNv->dcb_table.i2c_write[1] = 0x37;
3853 pNv->dcb_table.i2c_read[1] = 0x36;
3854 bios->fmaxvco = 256000;
3855 bios->fminvco = 128000;
3856 bios->fp.duallink_transition_clk = 90000;
3858 bmp_version_major = bios->data[offset + 5];
3859 bmp_version_minor = bios->data[offset + 6];
3861 xf86DrvMsg(pScrn->scrnIndex, X_INFO, "BMP version %d.%d\n",
3862 bmp_version_major, bmp_version_minor);
3864 /* Make sure that 0x36 is blank and can't be mistaken for a DCB pointer on early versions */
3865 if (bmp_version_major < 5)
3866 *(uint16_t *)&bios->data[0x36] = 0;
3868 /* Seems that the minor version was 1 for all major versions prior to 5 */
3869 /* Version 6 could theoretically exist, but I suspect BIT happened instead */
3870 if ((bmp_version_major < 5 && bmp_version_minor != 1) || bmp_version_major > 5) {
3871 xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "You have an unsupported BMP version. Please send in your bios\n");
3875 if (bmp_version_major == 0) /* nothing that's currently useful in this version */
3877 else if (bmp_version_major == 1)
3878 bmplength = 44; /* exact for 1.01 */
3879 else if (bmp_version_major == 2)
3880 bmplength = 48; /* exact for 2.01 */
3881 else if (bmp_version_major == 3)
3882 bmplength = 54; /* guessed - mem init tables added in this version */
3883 else if (bmp_version_major == 4 || bmp_version_minor < 0x1) /* don't know if 5.0 exists... */
3884 bmplength = 62; /* guessed - BMP I2C indices added in version 4*/
3885 else if (bmp_version_minor < 0x6)
3886 bmplength = 67; /* exact for 5.01 */
3887 else if (bmp_version_minor < 0x10)
3888 bmplength = 75; /* exact for 5.06 */
3889 else if (bmp_version_minor == 0x10)
3890 bmplength = 89; /* exact for 5.10h */
3891 else if (bmp_version_minor < 0x14)
3892 bmplength = 118; /* exact for 5.11h */
3893 else if (bmp_version_minor < 0x24) /* not sure of version where pll limits came in;
3894 * certainly exist by 0x24 though */
3895 /* length not exact: this is long enough to get lvds members */
3897 else if (bmp_version_minor < 0x27)
3898 /* length not exact: this is long enough to get pll limit member */
3901 /* length not exact: this is long enough to get dual link transition clock */
3905 if (nv_cksum(bios->data + offset, 8)) {
3906 xf86DrvMsg(pScrn->scrnIndex, X_WARNING, "Bad BMP checksum\n");
3910 /* bit 4 seems to indicate a mobile bios, bit 5 that the flat panel
3911 * tables are present, and bit 6 a tv bios */
3912 bios->feature_byte = bios->data[offset + 9];
3914 parse_bios_version(pScrn, bios, offset + 10);
3916 uint16_t legacy_scripts_offset = offset + 18;
3917 if (bmp_version_major < 2)
3918 legacy_scripts_offset -= 4;
3919 bios->init_script_tbls_ptr = le16_to_cpu(*(uint16_t *)&bios->data[legacy_scripts_offset]);
3920 bios->extra_init_script_tbl_ptr = le16_to_cpu(*(uint16_t *)&bios->data[legacy_scripts_offset + 2]);
3922 if (bmp_version_major > 2) { /* appears in BMP 3 */
3923 bios->legacy.mem_init_tbl_ptr = le16_to_cpu(*(uint16_t *)&bios->data[offset + 24]);
3924 bios->legacy.sdr_seq_tbl_ptr = le16_to_cpu(*(uint16_t *)&bios->data[offset + 26]);
3925 bios->legacy.ddr_seq_tbl_ptr = le16_to_cpu(*(uint16_t *)&bios->data[offset + 28]);
3928 uint16_t legacy_i2c_offset = 0x48; /* BMP version 2 & 3 */
3930 legacy_i2c_offset = offset + 54;
3931 bios->legacy.i2c_indices.crt = bios->data[legacy_i2c_offset];
3932 bios->legacy.i2c_indices.tv = bios->data[legacy_i2c_offset + 1];
3933 bios->legacy.i2c_indices.panel = bios->data[legacy_i2c_offset + 2];
3934 pNv->dcb_table.i2c_write[0] = bios->data[legacy_i2c_offset + 4];
3935 pNv->dcb_table.i2c_read[0] = bios->data[legacy_i2c_offset + 5];
3936 pNv->dcb_table.i2c_write[1] = bios->data[legacy_i2c_offset + 6];
3937 pNv->dcb_table.i2c_read[1] = bios->data[legacy_i2c_offset + 7];
3939 if (bmplength > 74) {
3940 bios->fmaxvco = le32_to_cpu(*((uint32_t *)&bios->data[offset + 67]));
3941 bios->fminvco = le32_to_cpu(*((uint32_t *)&bios->data[offset + 71]));
3943 if (bmplength > 88) {
3944 bit_entry_t initbitentry;
3945 initbitentry.length = 14;
3946 initbitentry.offset = offset + 75;
3947 parse_bit_init_tbl_entry(pScrn, bios, &initbitentry);
3949 if (bmplength > 94) {
3950 bios->tmds.output0_script_ptr = le16_to_cpu(*((uint16_t *)&bios->data[offset + 89]));
3951 bios->tmds.output1_script_ptr = le16_to_cpu(*((uint16_t *)&bios->data[offset + 91]));
3952 /* it seems the old style lvds script pointer (which I've not observed in use) gets
3953 * reused as the 18/24 bit panel interface default for EDID equipped panels */
3954 bios->fp.if_is_24bit = bios->data[offset + 95] & 1;
3956 if (bmplength > 108) {
3957 fpp.fptablepointer = le16_to_cpu(*((uint16_t *)(&bios->data[offset + 105])));
3958 fpp.fpxlatetableptr = le16_to_cpu(*((uint16_t *)(&bios->data[offset + 107])));
3961 if (bmplength > 120) {
3962 bios->fp.lvdsmanufacturerpointer = le16_to_cpu(*((uint16_t *)(&bios->data[offset + 117])));
3963 fpp.fpxlatemanufacturertableptr = le16_to_cpu(*((uint16_t *)(&bios->data[offset + 119])));
3965 if (bmplength > 143)
3966 bios->pll_limit_tbl_ptr = le16_to_cpu(*((uint16_t *)(&bios->data[offset + 142])));
3968 if (bmplength > 157)
3969 bios->fp.duallink_transition_clk = le16_to_cpu(*((uint16_t *)&bios->data[offset + 156])) * 10;
3971 /* want pll_limit_tbl_ptr set (if available) before init is run */
3972 if (bmp_version_major < 5 || bmp_version_minor < 0x10) {
3973 init_exec_t iexec = {true, false};
3974 if (bios->init_script_tbls_ptr)
3975 parse_init_table(pScrn, bios, bios->init_script_tbls_ptr, &iexec);
3976 if (bios->extra_init_script_tbl_ptr)
3977 parse_init_table(pScrn, bios, bios->extra_init_script_tbl_ptr, &iexec);
3979 parse_init_tables(pScrn, bios);
3981 /* If it's not a laptop, you probably don't care about fptables */
3982 if (!(bios->feature_byte & FEATURE_MOBILE))
3985 parse_lvds_manufacturer_table_init(pScrn, bios, &fpp);
3986 parse_fp_mode_table(pScrn, bios, &fpp);
3989 static uint16_t findstr(uint8_t *data, int n, const uint8_t *str, int len)
3993 for (i = 0; i <= (n - len); i++) {
3994 for (j = 0; j < len; j++)
3995 if (data[i + j] != str[j])
4004 static bool parse_dcb_entry(ScrnInfoPtr pScrn, uint8_t dcb_version, uint32_t conn, uint32_t conf, struct dcb_entry *entry)
4006 NVPtr pNv = NVPTR(pScrn);
4008 memset(entry, 0, sizeof (struct dcb_entry));
4010 /* safe defaults for a crt */
4012 entry->i2c_index = 0;
4015 entry->location = 0;
4017 entry->duallink_possible = false;
4019 if (dcb_version >= 0x20) {
4020 entry->type = conn & 0xf;
4021 entry->i2c_index = (conn >> 4) & 0xf;
4022 entry->heads = (conn >> 8) & 0xf;
4023 entry->bus = (conn >> 16) & 0xf;
4024 entry->location = (conn >> 20) & 0xf;
4025 entry->or = (conn >> 24) & 0xf;
4026 /* Normal entries consist of a single bit, but dual link has the
4027 * adjacent more significant bit set too
4029 if ((1 << (ffs(entry->or) - 1)) * 3 == entry->or)
4030 entry->duallink_possible = true;
4032 switch (entry->type) {
4037 entry->lvdsconf.use_straps_for_mode = true;
4038 if (dcb_version < 0x22) {
4040 /* both 0x4 and 0x8 show up in v2.0 tables; assume they mean
4041 * the same thing, which is probably wrong, but might work */
4042 if (conf & 0x4 || conf & 0x8)
4043 entry->lvdsconf.use_power_scripts = true;
4047 entry->lvdsconf.use_power_scripts = true;
4050 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
4051 "Unknown LVDS configuration bits, please report\n");
4052 /* cause output setting to fail, so message is seen */
4053 pNv->dcb_table.entries = 0;
4059 } else if (dcb_version >= 0x14 ) {
4060 if (conn != 0xf0003f00 && conn != 0xf2247f10 && conn != 0xf2204001 && conn != 0xf2204301 && conn != 0xf2244311 && conn != 0xf2045f14 && conn != 0xf2205004 && conn != 0xf2208001 && conn != 0xf4204011 && conn != 0xf4208011 && conn != 0xf4248011) {
4061 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
4062 "Unknown DCB 1.4 / 1.5 entry, please report\n");
4063 /* cause output setting to fail, so message is seen */
4064 pNv->dcb_table.entries = 0;
4067 /* most of the below is a "best guess" atm */
4068 entry->type = conn & 0xf;
4069 if (entry->type == 4) { /* digital */
4071 entry->type = OUTPUT_LVDS;
4073 entry->type = OUTPUT_TMDS;
4075 /* what's in bits 5-13? could be some brooktree/chrontel/philips thing, in tv case */
4076 entry->i2c_index = (conn >> 14) & 0xf;
4077 /* raw heads field is in range 0-1, so move to 1-2 */
4078 entry->heads = ((conn >> 18) & 0x7) + 1;
4079 entry->location = (conn >> 21) & 0xf;
4080 entry->bus = (conn >> 25) & 0x7;
4081 /* set or to be same as heads -- hopefully safe enough */
4082 entry->or = entry->heads;
4084 switch (entry->type) {
4086 /* this is probably buried in conn's unknown bits */
4087 entry->lvdsconf.use_power_scripts = true;
4090 /* invent a DVI-A output, by copying the fields of the DVI-D output
4091 * reported to work by math_b on an NV20(!) */
4092 memcpy(&entry[1], &entry[0], sizeof(struct dcb_entry));
4093 entry[1].type = OUTPUT_ANALOG;
4094 pNv->dcb_table.entries++;
4096 } else if (dcb_version >= 0x12) {
4097 /* v1.2 tables normally have the same 5 entries, which are not
4098 * specific to the card, so use the defaults for a crt */
4099 } else { /* pre DCB / v1.1 - use the safe defaults for a crt */
4100 xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
4101 "No information in BIOS output table; assuming a CRT output exists\n");
4102 entry->i2c_index = pNv->VBIOS.legacy.i2c_indices.crt;
4105 if (entry->type == OUTPUT_LVDS && pNv->VBIOS.fp.strapping != 0xff)
4106 entry->lvdsconf.use_straps_for_mode = true;
4108 pNv->dcb_table.entries++;
4114 read_dcb_i2c_table(ScrnInfoPtr pScrn, bios_t *bios, uint8_t dcb_version, uint16_t i2ctabptr)
4116 NVPtr pNv = NVPTR(pScrn);
4117 uint8_t *i2ctable = &bios->data[i2ctabptr];
4118 uint8_t headerlen = 0;
4119 int i2c_entries = MAX_NUM_DCB_ENTRIES;
4120 int recordoffset = 0, rdofs = 1, wrofs = 0;
4123 if (dcb_version >= 0x30) {
4124 if (i2ctable[0] != dcb_version) { /* necessary? */
4125 xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
4126 "DCB I2C table version mismatch (%02X vs %02X)\n",
4127 i2ctable[0], dcb_version);
4129 headerlen = i2ctable[1];
4130 i2c_entries = i2ctable[2];
4131 if (i2ctable[0] >= 0x40) {
4132 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
4133 "G80 DCB I2C table detected, arrgh\n"); /* they're plain weird */
4137 /* it's your own fault if you call this function on a DCB 1.1 BIOS --
4138 * the below assumes DCB 1.2
4140 if (dcb_version < 0x14) {
4146 for (i = 0; i < i2c_entries; i++)
4147 if (i2ctable[headerlen + 4 * i + 3] != 0xff) {
4148 pNv->dcb_table.i2c_read[i] = i2ctable[headerlen + recordoffset + rdofs + 4 * i];
4149 pNv->dcb_table.i2c_write[i] = i2ctable[headerlen + recordoffset + wrofs + 4 * i];
4153 static unsigned int parse_dcb_table(ScrnInfoPtr pScrn, bios_t *bios)
4155 NVPtr pNv = NVPTR(pScrn);
4156 uint16_t dcbptr, i2ctabptr = 0;
4158 uint8_t dcb_version, headerlen = 0x4, entries = MAX_NUM_DCB_ENTRIES;
4159 bool configblock = true;
4160 int recordlength = 8, confofs = 4;
4163 pNv->dcb_table.entries = 0;
4165 /* get the offset from 0x36 */
4166 dcbptr = le16_to_cpu(*(uint16_t *)&bios->data[0x36]);
4168 if (dcbptr == 0x0) {
4169 xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
4170 "No Display Configuration Block pointer found\n");
4171 /* this situation likely means a really old card, pre DCB, so we'll add the safe CRT entry */
4172 parse_dcb_entry(pScrn, 0, 0, 0, &pNv->dcb_table.entry[0]);
4176 dcbtable = &bios->data[dcbptr];
4178 /* get DCB version */
4179 dcb_version = dcbtable[0];
4180 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
4181 "Display Configuration Block version %d.%d found\n",
4182 dcb_version >> 4, dcb_version & 0xf);
4184 if (dcb_version >= 0x20) { /* NV17+ */
4187 if (dcb_version >= 0x30) { /* NV40+ */
4188 headerlen = dcbtable[1];
4189 entries = dcbtable[2];
4190 recordlength = dcbtable[3];
4191 i2ctabptr = le16_to_cpu(*(uint16_t *)&dcbtable[4]);
4192 sig = le32_to_cpu(*(uint32_t *)&dcbtable[6]);
4194 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
4195 "DCB header length %d, with %d possible entries\n",
4196 headerlen, entries);
4198 i2ctabptr = le16_to_cpu(*(uint16_t *)&dcbtable[2]);
4199 sig = le32_to_cpu(*(uint32_t *)&dcbtable[4]);
4203 if (sig != 0x4edcbdcb) {
4204 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
4205 "Bad Display Configuration Block signature (%08X)\n", sig);
4208 } else if (dcb_version >= 0x14) { /* some NV15/16, and NV11+ */
4212 strncpy(sig, (char *)&dcbtable[-7], 7);
4213 i2ctabptr = le16_to_cpu(*(uint16_t *)&dcbtable[2]);
4217 if (strcmp(sig, "DEV_REC")) {
4218 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
4219 "Bad Display Configuration Block signature (%s)\n", sig);
4222 } else if (dcb_version >= 0x12) { /* some NV6/10, and NV15+ */
4223 i2ctabptr = le16_to_cpu(*(uint16_t *)&dcbtable[2]);
4224 configblock = false;
4225 } else { /* NV5+, maybe NV4 */
4226 /* DCB 1.1 seems to be quite unhelpful - we'll just add the safe CRT entry */
4227 parse_dcb_entry(pScrn, dcb_version, 0, 0, &pNv->dcb_table.entry[0]);
4231 if (entries >= MAX_NUM_DCB_ENTRIES)
4232 entries = MAX_NUM_DCB_ENTRIES;
4234 for (i = 0; i < entries; i++) {
4235 uint32_t connection, config = 0;
4237 connection = le32_to_cpu(*(uint32_t *)&dcbtable[headerlen + recordlength * i]);
4239 config = le32_to_cpu(*(uint32_t *)&dcbtable[headerlen + confofs + recordlength * i]);
4241 /* Should we allow discontinuous DCBs? Certainly DCB I2C tables can be discontinuous */
4242 if ((connection & 0x0000000f) == 0x0000000f) /* end of records */
4244 if (connection == 0x00000000) /* seen on an NV11 with DCB v1.5 */
4247 ErrorF("Raw DCB entry %d: %08x %08x\n", i, connection, config);
4248 if (!parse_dcb_entry(pScrn, dcb_version, connection, config, &pNv->dcb_table.entry[pNv->dcb_table.entries]))
4252 /* DCB v1.2 does have an I2C table that read_dcb_i2c_table can handle, but cards
4253 * exist (seen on nv11) where the pointer to the table points to the wrong
4254 * place, so for now, we rely on the indices parsed in parse_bmp_structure
4255 * If that fails, we'll have to do some kind of heuristic/quirk...
4257 if (dcb_version > 0x12)
4258 read_dcb_i2c_table(pScrn, bios, dcb_version, i2ctabptr);
4260 /* DCB v2.0, in particular, lists each output combination separately.
4261 * Here we merge compatible entries to have fewer outputs, with more options
4263 for (i = 0; i < pNv->dcb_table.entries; i++) {
4264 struct dcb_entry *ient = &pNv->dcb_table.entry[i];
4267 for (j = i + 1; j < pNv->dcb_table.entries; j++) {
4268 struct dcb_entry *jent = &pNv->dcb_table.entry[j];
4270 if (jent->type == 100) /* already merged entry */
4273 if (jent->i2c_index == ient->i2c_index && jent->type == ient->type && jent->location == ient->location) {
4274 /* only merge heads field when output field is the same --
4275 * we could merge output field for same heads, but dual link,
4276 * the resultant need to make several merging passes, and lack
4277 * of applicable real life cases has deterred this so far
4279 if (jent->or == ient->or) {
4280 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
4281 "Merging DCB entries %d and %d\n", i, j);
4282 ient->heads |= jent->heads;
4283 jent->type = 100; /* dummy value */
4289 /* Compact entries merged into others out of dcb_table */
4291 for (i = 0; i < pNv->dcb_table.entries; i++) {
4292 if ( pNv->dcb_table.entry[i].type == 100 )
4295 if (newentries != i)
4296 memcpy(&pNv->dcb_table.entry[newentries], &pNv->dcb_table.entry[i], sizeof(struct dcb_entry));
4300 pNv->dcb_table.entries = newentries;
4302 return pNv->dcb_table.entries;
4305 static void load_nv17_hw_sequencer_ucode(ScrnInfoPtr pScrn, bios_t *bios, uint16_t hwsq_offset, int entry)
4307 /* BMP based cards, from NV17, need a microcode loading to correctly
4308 * control the GPIO etc for LVDS panels
4310 * BIT based cards seem to do this directly in the init scripts
4312 * The microcode entries are found by the "HWSQ" signature.
4313 * The header following has the number of entries, and the entry size
4315 * An entry consists of a dword to write to the sequencer control reg
4316 * (0x00001304), followed by the ucode bytes, written sequentially,
4317 * starting at reg 0x00001400
4320 uint8_t bytes_to_write;
4321 uint16_t hwsq_entry_offset;
4324 if (bios->data[hwsq_offset] <= entry) {
4325 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
4326 "Too few entries in HW sequencer table for requested entry\n");
4330 bytes_to_write = bios->data[hwsq_offset + 1];
4332 if (bytes_to_write != 36) {
4333 xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "Unknown HW sequencer entry size\n");
4337 xf86DrvMsg(pScrn->scrnIndex, X_INFO, "Loading NV17 power sequencing microcode\n");
4339 hwsq_entry_offset = hwsq_offset + 2 + entry * bytes_to_write;
4341 /* set sequencer control */
4342 nv32_wr(pScrn, 0x00001304, le32_to_cpu(*(uint32_t *)&bios->data[hwsq_entry_offset]));
4343 bytes_to_write -= 4;
4346 for (i = 0; i < bytes_to_write; i += 4)
4347 nv32_wr(pScrn, 0x00001400 + i, le32_to_cpu(*(uint32_t *)&bios->data[hwsq_entry_offset + i + 4]));
4349 /* twiddle NV_PBUS_DEBUG_4 */
4350 nv32_wr(pScrn, NV_PBUS_DEBUG_4, nv32_rd(pScrn, NV_PBUS_DEBUG_4) | 0x18);
4353 static void read_bios_edid(ScrnInfoPtr pScrn)
4355 bios_t *bios = &NVPTR(pScrn)->VBIOS;
4356 const uint8_t edid_sig[] = { 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00 };
4357 uint16_t offset = 0, newoffset;
4358 int searchlen = NV_PROM_SIZE, i;
4361 if (!(newoffset = findstr(&bios->data[offset], searchlen, edid_sig, 8)))
4363 offset += newoffset;
4364 if (!nv_cksum(&bios->data[offset], EDID1_LEN))
4367 searchlen -= offset;
4371 xf86DrvMsg(pScrn->scrnIndex, X_INFO, "Found EDID in BIOS\n");
4373 bios->fp.edid = xalloc(EDID1_LEN);
4374 for (i = 0; i < EDID1_LEN; i++)
4375 bios->fp.edid[i] = bios->data[offset + i];
4378 bool NVInitVBIOS(ScrnInfoPtr pScrn)
4380 NVPtr pNv = NVPTR(pScrn);
4382 memset(&pNv->VBIOS, 0, sizeof(bios_t));
4383 pNv->VBIOS.data = xalloc(NV_PROM_SIZE);
4385 if (!NVShadowVBIOS(pScrn, pNv->VBIOS.data)) {
4386 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
4387 "No valid BIOS image found\n");
4388 xfree(pNv->VBIOS.data);
4392 pNv->VBIOS.length = pNv->VBIOS.data[2] * 512;
4393 if (pNv->VBIOS.length > NV_PROM_SIZE)
4394 pNv->VBIOS.length = NV_PROM_SIZE;
4399 bool NVRunVBIOSInit(ScrnInfoPtr pScrn)
4401 NVPtr pNv = NVPTR(pScrn);
4402 const uint8_t bmp_signature[] = { 0xff, 0x7f, 'N', 'V', 0x0 };
4403 const uint8_t bit_signature[] = { 'B', 'I', 'T' };
4404 int offset, ret = 0;
4406 crtc_access(pNv, ACCESS_UNLOCK);
4408 if ((offset = findstr(pNv->VBIOS.data, pNv->VBIOS.length, bit_signature, sizeof(bit_signature)))) {
4409 xf86DrvMsg(pScrn->scrnIndex, X_INFO, "BIT BIOS found\n");
4410 parse_bit_structure(pScrn, &pNv->VBIOS, offset + 4);
4411 } else if ((offset = findstr(pNv->VBIOS.data, pNv->VBIOS.length, bmp_signature, sizeof(bmp_signature)))) {
4412 const uint8_t hwsq_signature[] = { 'H', 'W', 'S', 'Q' };
4415 if ((hwsq_offset = findstr(pNv->VBIOS.data, pNv->VBIOS.length, hwsq_signature, sizeof(hwsq_signature))))
4416 /* always use entry 0? */
4417 load_nv17_hw_sequencer_ucode(pScrn, &pNv->VBIOS, hwsq_offset + sizeof(hwsq_signature), 0);
4419 xf86DrvMsg(pScrn->scrnIndex, X_INFO, "BMP BIOS found\n");
4420 parse_bmp_structure(pScrn, &pNv->VBIOS, offset);
4422 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
4423 "No known BIOS signature found\n");
4427 crtc_access(pNv, ACCESS_LOCK);
4435 unsigned int NVParseBios(ScrnInfoPtr pScrn)
4437 NVPtr pNv = NVPTR(pScrn);
4438 uint32_t saved_nv_pextdev_boot_0;
4440 if (!NVInitVBIOS(pScrn))
4443 /* these will need remembering across a suspend */
4444 saved_nv_pextdev_boot_0 = nv32_rd(pScrn, NV_PEXTDEV_BOOT_0);
4445 saved_nv_pfb_cfg0 = nv32_rd(pScrn, NV_PFB_CFG0);
4447 /* init script execution disabled */
4448 pNv->VBIOS.execute = false;
4450 nv32_wr(pScrn, NV_PEXTDEV_BOOT_0, saved_nv_pextdev_boot_0);
4452 if (!NVRunVBIOSInit(pScrn))
4455 if (parse_dcb_table(pScrn, &pNv->VBIOS))
4456 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
4457 "Found %d entries in DCB\n", pNv->dcb_table.entries);
4459 if (pNv->VBIOS.feature_byte & FEATURE_MOBILE && !pNv->VBIOS.fp.native_mode)
4460 read_bios_edid(pScrn);
4462 /* allow subsequent scripts to execute */
4463 pNv->VBIOS.execute = true;