blob: 84b03e0a386507d70188c37c534bf1f22e967eca [file] [log] [blame]
Ben Skeggs6ee73862009-12-11 19:24:15 +10001/*
2 * Copyright 2005-2006 Erik Waling
3 * Copyright 2006 Stephane Marchesin
4 * Copyright 2007-2009 Stuart Bennett
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
21 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24
25#include "drmP.h"
26#define NV_DEBUG_NOTRACE
27#include "nouveau_drv.h"
28#include "nouveau_hw.h"
29
30/* these defines are made up */
31#define NV_CIO_CRE_44_HEADA 0x0
32#define NV_CIO_CRE_44_HEADB 0x3
33#define FEATURE_MOBILE 0x10 /* also FEATURE_QUADRO for BMP */
34#define LEGACY_I2C_CRT 0x80
35#define LEGACY_I2C_PANEL 0x81
36#define LEGACY_I2C_TV 0x82
37
38#define EDID1_LEN 128
39
40#define BIOSLOG(sip, fmt, arg...) NV_DEBUG(sip->dev, fmt, ##arg)
41#define LOG_OLD_VALUE(x)
42
43#define ROM16(x) le16_to_cpu(*(uint16_t *)&(x))
44#define ROM32(x) le32_to_cpu(*(uint32_t *)&(x))
45
46struct init_exec {
47 bool execute;
48 bool repeat;
49};
50
51static bool nv_cksum(const uint8_t *data, unsigned int length)
52{
53 /*
54 * There's a few checksums in the BIOS, so here's a generic checking
55 * function.
56 */
57 int i;
58 uint8_t sum = 0;
59
60 for (i = 0; i < length; i++)
61 sum += data[i];
62
63 if (sum)
64 return true;
65
66 return false;
67}
68
69static int
70score_vbios(struct drm_device *dev, const uint8_t *data, const bool writeable)
71{
72 if (!(data[0] == 0x55 && data[1] == 0xAA)) {
73 NV_TRACEWARN(dev, "... BIOS signature not found\n");
74 return 0;
75 }
76
77 if (nv_cksum(data, data[2] * 512)) {
78 NV_TRACEWARN(dev, "... BIOS checksum invalid\n");
79 /* if a ro image is somewhat bad, it's probably all rubbish */
80 return writeable ? 2 : 1;
81 } else
82 NV_TRACE(dev, "... appears to be valid\n");
83
84 return 3;
85}
86
87static void load_vbios_prom(struct drm_device *dev, uint8_t *data)
88{
89 struct drm_nouveau_private *dev_priv = dev->dev_private;
90 uint32_t pci_nv_20, save_pci_nv_20;
91 int pcir_ptr;
92 int i;
93
94 if (dev_priv->card_type >= NV_50)
95 pci_nv_20 = 0x88050;
96 else
97 pci_nv_20 = NV_PBUS_PCI_NV_20;
98
99 /* enable ROM access */
100 save_pci_nv_20 = nvReadMC(dev, pci_nv_20);
101 nvWriteMC(dev, pci_nv_20,
102 save_pci_nv_20 & ~NV_PBUS_PCI_NV_20_ROM_SHADOW_ENABLED);
103
104 /* bail if no rom signature */
105 if (nv_rd08(dev, NV_PROM_OFFSET) != 0x55 ||
106 nv_rd08(dev, NV_PROM_OFFSET + 1) != 0xaa)
107 goto out;
108
109 /* additional check (see note below) - read PCI record header */
110 pcir_ptr = nv_rd08(dev, NV_PROM_OFFSET + 0x18) |
111 nv_rd08(dev, NV_PROM_OFFSET + 0x19) << 8;
112 if (nv_rd08(dev, NV_PROM_OFFSET + pcir_ptr) != 'P' ||
113 nv_rd08(dev, NV_PROM_OFFSET + pcir_ptr + 1) != 'C' ||
114 nv_rd08(dev, NV_PROM_OFFSET + pcir_ptr + 2) != 'I' ||
115 nv_rd08(dev, NV_PROM_OFFSET + pcir_ptr + 3) != 'R')
116 goto out;
117
118 /* on some 6600GT/6800LE prom reads are messed up. nvclock alleges a
119 * a good read may be obtained by waiting or re-reading (cargocult: 5x)
120 * each byte. we'll hope pramin has something usable instead
121 */
122 for (i = 0; i < NV_PROM_SIZE; i++)
123 data[i] = nv_rd08(dev, NV_PROM_OFFSET + i);
124
125out:
126 /* disable ROM access */
127 nvWriteMC(dev, pci_nv_20,
128 save_pci_nv_20 | NV_PBUS_PCI_NV_20_ROM_SHADOW_ENABLED);
129}
130
131static void load_vbios_pramin(struct drm_device *dev, uint8_t *data)
132{
133 struct drm_nouveau_private *dev_priv = dev->dev_private;
134 uint32_t old_bar0_pramin = 0;
135 int i;
136
137 if (dev_priv->card_type >= NV_50) {
138 uint32_t vbios_vram = (nv_rd32(dev, 0x619f04) & ~0xff) << 8;
139
140 if (!vbios_vram)
141 vbios_vram = (nv_rd32(dev, 0x1700) << 16) + 0xf0000;
142
143 old_bar0_pramin = nv_rd32(dev, 0x1700);
144 nv_wr32(dev, 0x1700, vbios_vram >> 16);
145 }
146
147 /* bail if no rom signature */
148 if (nv_rd08(dev, NV_PRAMIN_OFFSET) != 0x55 ||
149 nv_rd08(dev, NV_PRAMIN_OFFSET + 1) != 0xaa)
150 goto out;
151
152 for (i = 0; i < NV_PROM_SIZE; i++)
153 data[i] = nv_rd08(dev, NV_PRAMIN_OFFSET + i);
154
155out:
156 if (dev_priv->card_type >= NV_50)
157 nv_wr32(dev, 0x1700, old_bar0_pramin);
158}
159
160static void load_vbios_pci(struct drm_device *dev, uint8_t *data)
161{
162 void __iomem *rom = NULL;
163 size_t rom_len;
164 int ret;
165
166 ret = pci_enable_rom(dev->pdev);
167 if (ret)
168 return;
169
170 rom = pci_map_rom(dev->pdev, &rom_len);
171 if (!rom)
172 goto out;
173 memcpy_fromio(data, rom, rom_len);
174 pci_unmap_rom(dev->pdev, rom);
175
176out:
177 pci_disable_rom(dev->pdev);
178}
179
180struct methods {
181 const char desc[8];
182 void (*loadbios)(struct drm_device *, uint8_t *);
183 const bool rw;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000184};
185
186static struct methods nv04_methods[] = {
187 { "PROM", load_vbios_prom, false },
188 { "PRAMIN", load_vbios_pramin, true },
189 { "PCIROM", load_vbios_pci, true },
Ben Skeggs6ee73862009-12-11 19:24:15 +1000190};
191
192static struct methods nv50_methods[] = {
193 { "PRAMIN", load_vbios_pramin, true },
194 { "PROM", load_vbios_prom, false },
195 { "PCIROM", load_vbios_pci, true },
Ben Skeggs6ee73862009-12-11 19:24:15 +1000196};
197
Marcin Kościelnicki657b6242009-12-15 00:37:30 +0000198#define METHODCNT 3
199
Ben Skeggs6ee73862009-12-11 19:24:15 +1000200static bool NVShadowVBIOS(struct drm_device *dev, uint8_t *data)
201{
202 struct drm_nouveau_private *dev_priv = dev->dev_private;
Marcin Kościelnicki657b6242009-12-15 00:37:30 +0000203 struct methods *methods;
204 int i;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000205 int testscore = 3;
Marcin Kościelnicki657b6242009-12-15 00:37:30 +0000206 int scores[METHODCNT];
Ben Skeggs6ee73862009-12-11 19:24:15 +1000207
208 if (nouveau_vbios) {
Marcin Kościelnicki657b6242009-12-15 00:37:30 +0000209 methods = nv04_methods;
210 for (i = 0; i < METHODCNT; i++)
211 if (!strcasecmp(nouveau_vbios, methods[i].desc))
Ben Skeggs6ee73862009-12-11 19:24:15 +1000212 break;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000213
Marcin Kościelnicki657b6242009-12-15 00:37:30 +0000214 if (i < METHODCNT) {
Ben Skeggs6ee73862009-12-11 19:24:15 +1000215 NV_INFO(dev, "Attempting to use BIOS image from %s\n",
Marcin Kościelnicki657b6242009-12-15 00:37:30 +0000216 methods[i].desc);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000217
Marcin Kościelnicki657b6242009-12-15 00:37:30 +0000218 methods[i].loadbios(dev, data);
219 if (score_vbios(dev, data, methods[i].rw))
Ben Skeggs6ee73862009-12-11 19:24:15 +1000220 return true;
221 }
222
223 NV_ERROR(dev, "VBIOS source \'%s\' invalid\n", nouveau_vbios);
224 }
225
226 if (dev_priv->card_type < NV_50)
227 methods = nv04_methods;
228 else
229 methods = nv50_methods;
230
Marcin Kościelnicki657b6242009-12-15 00:37:30 +0000231 for (i = 0; i < METHODCNT; i++) {
Ben Skeggs6ee73862009-12-11 19:24:15 +1000232 NV_TRACE(dev, "Attempting to load BIOS image from %s\n",
Marcin Kościelnicki657b6242009-12-15 00:37:30 +0000233 methods[i].desc);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000234 data[0] = data[1] = 0; /* avoid reuse of previous image */
Marcin Kościelnicki657b6242009-12-15 00:37:30 +0000235 methods[i].loadbios(dev, data);
236 scores[i] = score_vbios(dev, data, methods[i].rw);
237 if (scores[i] == testscore)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000238 return true;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000239 }
240
241 while (--testscore > 0) {
Marcin Kościelnicki657b6242009-12-15 00:37:30 +0000242 for (i = 0; i < METHODCNT; i++) {
243 if (scores[i] == testscore) {
Ben Skeggs6ee73862009-12-11 19:24:15 +1000244 NV_TRACE(dev, "Using BIOS image from %s\n",
Marcin Kościelnicki657b6242009-12-15 00:37:30 +0000245 methods[i].desc);
246 methods[i].loadbios(dev, data);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000247 return true;
248 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000249 }
250 }
251
252 NV_ERROR(dev, "No valid BIOS image found\n");
253 return false;
254}
255
256struct init_tbl_entry {
257 char *name;
258 uint8_t id;
Marcin Kościelnicki37383652009-12-15 00:37:31 +0000259 int (*handler)(struct nvbios *, uint16_t, struct init_exec *);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000260};
261
262struct bit_entry {
263 uint8_t id[2];
264 uint16_t length;
265 uint16_t offset;
266};
267
268static int parse_init_table(struct nvbios *, unsigned int, struct init_exec *);
269
270#define MACRO_INDEX_SIZE 2
271#define MACRO_SIZE 8
272#define CONDITION_SIZE 12
273#define IO_FLAG_CONDITION_SIZE 9
274#define IO_CONDITION_SIZE 5
275#define MEM_INIT_SIZE 66
276
277static void still_alive(void)
278{
279#if 0
280 sync();
281 msleep(2);
282#endif
283}
284
285static uint32_t
286munge_reg(struct nvbios *bios, uint32_t reg)
287{
288 struct drm_nouveau_private *dev_priv = bios->dev->dev_private;
289 struct dcb_entry *dcbent = bios->display.output;
290
291 if (dev_priv->card_type < NV_50)
292 return reg;
293
294 if (reg & 0x40000000) {
295 BUG_ON(!dcbent);
296
297 reg += (ffs(dcbent->or) - 1) * 0x800;
298 if ((reg & 0x20000000) && !(dcbent->sorconf.link & 1))
299 reg += 0x00000080;
300 }
301
302 reg &= ~0x60000000;
303 return reg;
304}
305
306static int
307valid_reg(struct nvbios *bios, uint32_t reg)
308{
309 struct drm_nouveau_private *dev_priv = bios->dev->dev_private;
310 struct drm_device *dev = bios->dev;
311
312 /* C51 has misaligned regs on purpose. Marvellous */
Ben Skeggs9855e582010-01-12 13:02:19 +1000313 if (reg & 0x2 ||
Ben Skeggs04a39c52010-02-24 10:03:05 +1000314 (reg & 0x1 && dev_priv->vbios.chip_version != 0x51))
Ben Skeggs9855e582010-01-12 13:02:19 +1000315 NV_ERROR(dev, "======= misaligned reg 0x%08X =======\n", reg);
316
317 /* warn on C51 regs that haven't been verified accessible in tracing */
Ben Skeggs04a39c52010-02-24 10:03:05 +1000318 if (reg & 0x1 && dev_priv->vbios.chip_version == 0x51 &&
Ben Skeggs6ee73862009-12-11 19:24:15 +1000319 reg != 0x130d && reg != 0x1311 && reg != 0x60081d)
320 NV_WARN(dev, "=== C51 misaligned reg 0x%08X not verified ===\n",
321 reg);
322
Ben Skeggs9855e582010-01-12 13:02:19 +1000323 if (reg >= (8*1024*1024)) {
324 NV_ERROR(dev, "=== reg 0x%08x out of mapped bounds ===\n", reg);
325 return 0;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000326 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000327
Ben Skeggs9855e582010-01-12 13:02:19 +1000328 return 1;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000329}
330
331static bool
332valid_idx_port(struct nvbios *bios, uint16_t port)
333{
334 struct drm_nouveau_private *dev_priv = bios->dev->dev_private;
335 struct drm_device *dev = bios->dev;
336
337 /*
338 * If adding more ports here, the read/write functions below will need
339 * updating so that the correct mmio range (PRMCIO, PRMDIO, PRMVIO) is
340 * used for the port in question
341 */
342 if (dev_priv->card_type < NV_50) {
343 if (port == NV_CIO_CRX__COLOR)
344 return true;
345 if (port == NV_VIO_SRX)
346 return true;
347 } else {
348 if (port == NV_CIO_CRX__COLOR)
349 return true;
350 }
351
352 NV_ERROR(dev, "========== unknown indexed io port 0x%04X ==========\n",
353 port);
354
355 return false;
356}
357
358static bool
359valid_port(struct nvbios *bios, uint16_t port)
360{
361 struct drm_device *dev = bios->dev;
362
363 /*
364 * If adding more ports here, the read/write functions below will need
365 * updating so that the correct mmio range (PRMCIO, PRMDIO, PRMVIO) is
366 * used for the port in question
367 */
368 if (port == NV_VIO_VSE2)
369 return true;
370
371 NV_ERROR(dev, "========== unknown io port 0x%04X ==========\n", port);
372
373 return false;
374}
375
376static uint32_t
377bios_rd32(struct nvbios *bios, uint32_t reg)
378{
379 uint32_t data;
380
381 reg = munge_reg(bios, reg);
382 if (!valid_reg(bios, reg))
383 return 0;
384
385 /*
386 * C51 sometimes uses regs with bit0 set in the address. For these
387 * cases there should exist a translation in a BIOS table to an IO
388 * port address which the BIOS uses for accessing the reg
389 *
390 * These only seem to appear for the power control regs to a flat panel,
391 * and the GPIO regs at 0x60081*. In C51 mmio traces the normal regs
392 * for 0x1308 and 0x1310 are used - hence the mask below. An S3
393 * suspend-resume mmio trace from a C51 will be required to see if this
394 * is true for the power microcode in 0x14.., or whether the direct IO
395 * port access method is needed
396 */
397 if (reg & 0x1)
398 reg &= ~0x1;
399
400 data = nv_rd32(bios->dev, reg);
401
402 BIOSLOG(bios, " Read: Reg: 0x%08X, Data: 0x%08X\n", reg, data);
403
404 return data;
405}
406
407static void
408bios_wr32(struct nvbios *bios, uint32_t reg, uint32_t data)
409{
410 struct drm_nouveau_private *dev_priv = bios->dev->dev_private;
411
412 reg = munge_reg(bios, reg);
413 if (!valid_reg(bios, reg))
414 return;
415
416 /* see note in bios_rd32 */
417 if (reg & 0x1)
418 reg &= 0xfffffffe;
419
420 LOG_OLD_VALUE(bios_rd32(bios, reg));
421 BIOSLOG(bios, " Write: Reg: 0x%08X, Data: 0x%08X\n", reg, data);
422
Ben Skeggs04a39c52010-02-24 10:03:05 +1000423 if (dev_priv->vbios.execute) {
Ben Skeggs6ee73862009-12-11 19:24:15 +1000424 still_alive();
425 nv_wr32(bios->dev, reg, data);
426 }
427}
428
429static uint8_t
430bios_idxprt_rd(struct nvbios *bios, uint16_t port, uint8_t index)
431{
432 struct drm_nouveau_private *dev_priv = bios->dev->dev_private;
433 struct drm_device *dev = bios->dev;
434 uint8_t data;
435
436 if (!valid_idx_port(bios, port))
437 return 0;
438
439 if (dev_priv->card_type < NV_50) {
440 if (port == NV_VIO_SRX)
441 data = NVReadVgaSeq(dev, bios->state.crtchead, index);
442 else /* assume NV_CIO_CRX__COLOR */
443 data = NVReadVgaCrtc(dev, bios->state.crtchead, index);
444 } else {
445 uint32_t data32;
446
447 data32 = bios_rd32(bios, NV50_PDISPLAY_VGACRTC(index & ~3));
448 data = (data32 >> ((index & 3) << 3)) & 0xff;
449 }
450
451 BIOSLOG(bios, " Indexed IO read: Port: 0x%04X, Index: 0x%02X, "
452 "Head: 0x%02X, Data: 0x%02X\n",
453 port, index, bios->state.crtchead, data);
454 return data;
455}
456
457static void
458bios_idxprt_wr(struct nvbios *bios, uint16_t port, uint8_t index, uint8_t data)
459{
460 struct drm_nouveau_private *dev_priv = bios->dev->dev_private;
461 struct drm_device *dev = bios->dev;
462
463 if (!valid_idx_port(bios, port))
464 return;
465
466 /*
467 * The current head is maintained in the nvbios member state.crtchead.
468 * We trap changes to CR44 and update the head variable and hence the
469 * register set written.
470 * As CR44 only exists on CRTC0, we update crtchead to head0 in advance
471 * of the write, and to head1 after the write
472 */
473 if (port == NV_CIO_CRX__COLOR && index == NV_CIO_CRE_44 &&
474 data != NV_CIO_CRE_44_HEADB)
475 bios->state.crtchead = 0;
476
477 LOG_OLD_VALUE(bios_idxprt_rd(bios, port, index));
478 BIOSLOG(bios, " Indexed IO write: Port: 0x%04X, Index: 0x%02X, "
479 "Head: 0x%02X, Data: 0x%02X\n",
480 port, index, bios->state.crtchead, data);
481
482 if (bios->execute && dev_priv->card_type < NV_50) {
483 still_alive();
484 if (port == NV_VIO_SRX)
485 NVWriteVgaSeq(dev, bios->state.crtchead, index, data);
486 else /* assume NV_CIO_CRX__COLOR */
487 NVWriteVgaCrtc(dev, bios->state.crtchead, index, data);
488 } else
489 if (bios->execute) {
490 uint32_t data32, shift = (index & 3) << 3;
491
492 still_alive();
493
494 data32 = bios_rd32(bios, NV50_PDISPLAY_VGACRTC(index & ~3));
495 data32 &= ~(0xff << shift);
496 data32 |= (data << shift);
497 bios_wr32(bios, NV50_PDISPLAY_VGACRTC(index & ~3), data32);
498 }
499
500 if (port == NV_CIO_CRX__COLOR &&
501 index == NV_CIO_CRE_44 && data == NV_CIO_CRE_44_HEADB)
502 bios->state.crtchead = 1;
503}
504
505static uint8_t
506bios_port_rd(struct nvbios *bios, uint16_t port)
507{
508 uint8_t data, head = bios->state.crtchead;
509
510 if (!valid_port(bios, port))
511 return 0;
512
513 data = NVReadPRMVIO(bios->dev, head, NV_PRMVIO0_OFFSET + port);
514
515 BIOSLOG(bios, " IO read: Port: 0x%04X, Head: 0x%02X, Data: 0x%02X\n",
516 port, head, data);
517
518 return data;
519}
520
521static void
522bios_port_wr(struct nvbios *bios, uint16_t port, uint8_t data)
523{
524 int head = bios->state.crtchead;
525
526 if (!valid_port(bios, port))
527 return;
528
529 LOG_OLD_VALUE(bios_port_rd(bios, port));
530 BIOSLOG(bios, " IO write: Port: 0x%04X, Head: 0x%02X, Data: 0x%02X\n",
531 port, head, data);
532
533 if (!bios->execute)
534 return;
535
536 still_alive();
537 NVWritePRMVIO(bios->dev, head, NV_PRMVIO0_OFFSET + port, data);
538}
539
540static bool
541io_flag_condition_met(struct nvbios *bios, uint16_t offset, uint8_t cond)
542{
543 /*
544 * The IO flag condition entry has 2 bytes for the CRTC port; 1 byte
545 * for the CRTC index; 1 byte for the mask to apply to the value
546 * retrieved from the CRTC; 1 byte for the shift right to apply to the
547 * masked CRTC value; 2 bytes for the offset to the flag array, to
548 * which the shifted value is added; 1 byte for the mask applied to the
549 * value read from the flag array; and 1 byte for the value to compare
550 * against the masked byte from the flag table.
551 */
552
553 uint16_t condptr = bios->io_flag_condition_tbl_ptr + cond * IO_FLAG_CONDITION_SIZE;
554 uint16_t crtcport = ROM16(bios->data[condptr]);
555 uint8_t crtcindex = bios->data[condptr + 2];
556 uint8_t mask = bios->data[condptr + 3];
557 uint8_t shift = bios->data[condptr + 4];
558 uint16_t flagarray = ROM16(bios->data[condptr + 5]);
559 uint8_t flagarraymask = bios->data[condptr + 7];
560 uint8_t cmpval = bios->data[condptr + 8];
561 uint8_t data;
562
563 BIOSLOG(bios, "0x%04X: Port: 0x%04X, Index: 0x%02X, Mask: 0x%02X, "
564 "Shift: 0x%02X, FlagArray: 0x%04X, FAMask: 0x%02X, "
565 "Cmpval: 0x%02X\n",
566 offset, crtcport, crtcindex, mask, shift, flagarray, flagarraymask, cmpval);
567
568 data = bios_idxprt_rd(bios, crtcport, crtcindex);
569
570 data = bios->data[flagarray + ((data & mask) >> shift)];
571 data &= flagarraymask;
572
573 BIOSLOG(bios, "0x%04X: Checking if 0x%02X equals 0x%02X\n",
574 offset, data, cmpval);
575
576 return (data == cmpval);
577}
578
579static bool
580bios_condition_met(struct nvbios *bios, uint16_t offset, uint8_t cond)
581{
582 /*
583 * The condition table entry has 4 bytes for the address of the
584 * register to check, 4 bytes for a mask to apply to the register and
585 * 4 for a test comparison value
586 */
587
588 uint16_t condptr = bios->condition_tbl_ptr + cond * CONDITION_SIZE;
589 uint32_t reg = ROM32(bios->data[condptr]);
590 uint32_t mask = ROM32(bios->data[condptr + 4]);
591 uint32_t cmpval = ROM32(bios->data[condptr + 8]);
592 uint32_t data;
593
594 BIOSLOG(bios, "0x%04X: Cond: 0x%02X, Reg: 0x%08X, Mask: 0x%08X\n",
595 offset, cond, reg, mask);
596
597 data = bios_rd32(bios, reg) & mask;
598
599 BIOSLOG(bios, "0x%04X: Checking if 0x%08X equals 0x%08X\n",
600 offset, data, cmpval);
601
602 return (data == cmpval);
603}
604
605static bool
606io_condition_met(struct nvbios *bios, uint16_t offset, uint8_t cond)
607{
608 /*
609 * The IO condition entry has 2 bytes for the IO port address; 1 byte
610 * for the index to write to io_port; 1 byte for the mask to apply to
611 * the byte read from io_port+1; and 1 byte for the value to compare
612 * against the masked byte.
613 */
614
615 uint16_t condptr = bios->io_condition_tbl_ptr + cond * IO_CONDITION_SIZE;
616 uint16_t io_port = ROM16(bios->data[condptr]);
617 uint8_t port_index = bios->data[condptr + 2];
618 uint8_t mask = bios->data[condptr + 3];
619 uint8_t cmpval = bios->data[condptr + 4];
620
621 uint8_t data = bios_idxprt_rd(bios, io_port, port_index) & mask;
622
623 BIOSLOG(bios, "0x%04X: Checking if 0x%02X equals 0x%02X\n",
624 offset, data, cmpval);
625
626 return (data == cmpval);
627}
628
629static int
630nv50_pll_set(struct drm_device *dev, uint32_t reg, uint32_t clk)
631{
632 struct drm_nouveau_private *dev_priv = dev->dev_private;
633 uint32_t reg0 = nv_rd32(dev, reg + 0);
634 uint32_t reg1 = nv_rd32(dev, reg + 4);
635 struct nouveau_pll_vals pll;
636 struct pll_lims pll_limits;
637 int ret;
638
639 ret = get_pll_limits(dev, reg, &pll_limits);
640 if (ret)
641 return ret;
642
643 clk = nouveau_calc_pll_mnp(dev, &pll_limits, clk, &pll);
644 if (!clk)
645 return -ERANGE;
646
647 reg0 = (reg0 & 0xfff8ffff) | (pll.log2P << 16);
648 reg1 = (reg1 & 0xffff0000) | (pll.N1 << 8) | pll.M1;
649
Ben Skeggs04a39c52010-02-24 10:03:05 +1000650 if (dev_priv->vbios.execute) {
Ben Skeggs6ee73862009-12-11 19:24:15 +1000651 still_alive();
652 nv_wr32(dev, reg + 4, reg1);
653 nv_wr32(dev, reg + 0, reg0);
654 }
655
656 return 0;
657}
658
659static int
660setPLL(struct nvbios *bios, uint32_t reg, uint32_t clk)
661{
662 struct drm_device *dev = bios->dev;
663 struct drm_nouveau_private *dev_priv = dev->dev_private;
664 /* clk in kHz */
665 struct pll_lims pll_lim;
666 struct nouveau_pll_vals pllvals;
667 int ret;
668
669 if (dev_priv->card_type >= NV_50)
670 return nv50_pll_set(dev, reg, clk);
671
672 /* high regs (such as in the mac g5 table) are not -= 4 */
673 ret = get_pll_limits(dev, reg > 0x405c ? reg : reg - 4, &pll_lim);
674 if (ret)
675 return ret;
676
677 clk = nouveau_calc_pll_mnp(dev, &pll_lim, clk, &pllvals);
678 if (!clk)
679 return -ERANGE;
680
681 if (bios->execute) {
682 still_alive();
683 nouveau_hw_setpll(dev, reg, &pllvals);
684 }
685
686 return 0;
687}
688
689static int dcb_entry_idx_from_crtchead(struct drm_device *dev)
690{
691 struct drm_nouveau_private *dev_priv = dev->dev_private;
Ben Skeggs04a39c52010-02-24 10:03:05 +1000692 struct nvbios *bios = &dev_priv->vbios;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000693
694 /*
695 * For the results of this function to be correct, CR44 must have been
696 * set (using bios_idxprt_wr to set crtchead), CR58 set for CR57 = 0,
697 * and the DCB table parsed, before the script calling the function is
698 * run. run_digital_op_script is example of how to do such setup
699 */
700
701 uint8_t dcb_entry = NVReadVgaCrtc5758(dev, bios->state.crtchead, 0);
702
Ben Skeggs7f245b22010-02-24 09:56:18 +1000703 if (dcb_entry > bios->dcb.entries) {
Ben Skeggs6ee73862009-12-11 19:24:15 +1000704 NV_ERROR(dev, "CR58 doesn't have a valid DCB entry currently "
705 "(%02X)\n", dcb_entry);
706 dcb_entry = 0x7f; /* unused / invalid marker */
707 }
708
709 return dcb_entry;
710}
711
712static struct nouveau_i2c_chan *
713init_i2c_device_find(struct drm_device *dev, int i2c_index)
714{
715 struct drm_nouveau_private *dev_priv = dev->dev_private;
Ben Skeggs04a39c52010-02-24 10:03:05 +1000716 struct dcb_table *dcb = &dev_priv->vbios.dcb;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000717
718 if (i2c_index == 0xff) {
719 /* note: dcb_entry_idx_from_crtchead needs pre-script set-up */
720 int idx = dcb_entry_idx_from_crtchead(dev), shift = 0;
Ben Skeggs7f245b22010-02-24 09:56:18 +1000721 int default_indices = dcb->i2c_default_indices;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000722
Ben Skeggs7f245b22010-02-24 09:56:18 +1000723 if (idx != 0x7f && dcb->entry[idx].i2c_upper_default)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000724 shift = 4;
725
726 i2c_index = (default_indices >> shift) & 0xf;
727 }
728 if (i2c_index == 0x80) /* g80+ */
Ben Skeggs7f245b22010-02-24 09:56:18 +1000729 i2c_index = dcb->i2c_default_indices & 0xf;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000730
731 return nouveau_i2c_find(dev, i2c_index);
732}
733
Ben Skeggs7f245b22010-02-24 09:56:18 +1000734static uint32_t
735get_tmds_index_reg(struct drm_device *dev, uint8_t mlv)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000736{
737 /*
738 * For mlv < 0x80, it is an index into a table of TMDS base addresses.
739 * For mlv == 0x80 use the "or" value of the dcb_entry indexed by
740 * CR58 for CR57 = 0 to index a table of offsets to the basic
741 * 0x6808b0 address.
742 * For mlv == 0x81 use the "or" value of the dcb_entry indexed by
743 * CR58 for CR57 = 0 to index a table of offsets to the basic
744 * 0x6808b0 address, and then flip the offset by 8.
745 */
746
747 struct drm_nouveau_private *dev_priv = dev->dev_private;
Ben Skeggs04a39c52010-02-24 10:03:05 +1000748 struct nvbios *bios = &dev_priv->vbios;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000749 const int pramdac_offset[13] = {
750 0, 0, 0x8, 0, 0x2000, 0, 0, 0, 0x2008, 0, 0, 0, 0x2000 };
751 const uint32_t pramdac_table[4] = {
752 0x6808b0, 0x6808b8, 0x6828b0, 0x6828b8 };
753
754 if (mlv >= 0x80) {
755 int dcb_entry, dacoffset;
756
757 /* note: dcb_entry_idx_from_crtchead needs pre-script set-up */
758 dcb_entry = dcb_entry_idx_from_crtchead(dev);
759 if (dcb_entry == 0x7f)
760 return 0;
Ben Skeggs7f245b22010-02-24 09:56:18 +1000761 dacoffset = pramdac_offset[bios->dcb.entry[dcb_entry].or];
Ben Skeggs6ee73862009-12-11 19:24:15 +1000762 if (mlv == 0x81)
763 dacoffset ^= 8;
764 return 0x6808b0 + dacoffset;
765 } else {
Marcin Slusarzdf31ef42010-02-17 19:04:00 +0100766 if (mlv >= ARRAY_SIZE(pramdac_table)) {
Ben Skeggs6ee73862009-12-11 19:24:15 +1000767 NV_ERROR(dev, "Magic Lookup Value too big (%02X)\n",
768 mlv);
769 return 0;
770 }
771 return pramdac_table[mlv];
772 }
773}
774
Marcin Kościelnicki37383652009-12-15 00:37:31 +0000775static int
Ben Skeggs6ee73862009-12-11 19:24:15 +1000776init_io_restrict_prog(struct nvbios *bios, uint16_t offset,
777 struct init_exec *iexec)
778{
779 /*
780 * INIT_IO_RESTRICT_PROG opcode: 0x32 ('2')
781 *
782 * offset (8 bit): opcode
783 * offset + 1 (16 bit): CRTC port
784 * offset + 3 (8 bit): CRTC index
785 * offset + 4 (8 bit): mask
786 * offset + 5 (8 bit): shift
787 * offset + 6 (8 bit): count
788 * offset + 7 (32 bit): register
789 * offset + 11 (32 bit): configuration 1
790 * ...
791 *
792 * Starting at offset + 11 there are "count" 32 bit values.
793 * To find out which value to use read index "CRTC index" on "CRTC
794 * port", AND this value with "mask" and then bit shift right "shift"
795 * bits. Read the appropriate value using this index and write to
796 * "register"
797 */
798
799 uint16_t crtcport = ROM16(bios->data[offset + 1]);
800 uint8_t crtcindex = bios->data[offset + 3];
801 uint8_t mask = bios->data[offset + 4];
802 uint8_t shift = bios->data[offset + 5];
803 uint8_t count = bios->data[offset + 6];
804 uint32_t reg = ROM32(bios->data[offset + 7]);
805 uint8_t config;
806 uint32_t configval;
Marcin Kościelnicki37383652009-12-15 00:37:31 +0000807 int len = 11 + count * 4;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000808
809 if (!iexec->execute)
Marcin Kościelnicki37383652009-12-15 00:37:31 +0000810 return len;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000811
812 BIOSLOG(bios, "0x%04X: Port: 0x%04X, Index: 0x%02X, Mask: 0x%02X, "
813 "Shift: 0x%02X, Count: 0x%02X, Reg: 0x%08X\n",
814 offset, crtcport, crtcindex, mask, shift, count, reg);
815
816 config = (bios_idxprt_rd(bios, crtcport, crtcindex) & mask) >> shift;
817 if (config > count) {
818 NV_ERROR(bios->dev,
819 "0x%04X: Config 0x%02X exceeds maximal bound 0x%02X\n",
820 offset, config, count);
Marcin Kościelnicki37383652009-12-15 00:37:31 +0000821 return 0;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000822 }
823
824 configval = ROM32(bios->data[offset + 11 + config * 4]);
825
826 BIOSLOG(bios, "0x%04X: Writing config %02X\n", offset, config);
827
828 bios_wr32(bios, reg, configval);
829
Marcin Kościelnicki37383652009-12-15 00:37:31 +0000830 return len;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000831}
832
Marcin Kościelnicki37383652009-12-15 00:37:31 +0000833static int
Ben Skeggs6ee73862009-12-11 19:24:15 +1000834init_repeat(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
835{
836 /*
837 * INIT_REPEAT opcode: 0x33 ('3')
838 *
839 * offset (8 bit): opcode
840 * offset + 1 (8 bit): count
841 *
842 * Execute script following this opcode up to INIT_REPEAT_END
843 * "count" times
844 */
845
846 uint8_t count = bios->data[offset + 1];
847 uint8_t i;
848
849 /* no iexec->execute check by design */
850
851 BIOSLOG(bios, "0x%04X: Repeating following segment %d times\n",
852 offset, count);
853
854 iexec->repeat = true;
855
856 /*
857 * count - 1, as the script block will execute once when we leave this
858 * opcode -- this is compatible with bios behaviour as:
859 * a) the block is always executed at least once, even if count == 0
860 * b) the bios interpreter skips to the op following INIT_END_REPEAT,
861 * while we don't
862 */
863 for (i = 0; i < count - 1; i++)
864 parse_init_table(bios, offset + 2, iexec);
865
866 iexec->repeat = false;
867
Marcin Kościelnicki37383652009-12-15 00:37:31 +0000868 return 2;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000869}
870
Marcin Kościelnicki37383652009-12-15 00:37:31 +0000871static int
Ben Skeggs6ee73862009-12-11 19:24:15 +1000872init_io_restrict_pll(struct nvbios *bios, uint16_t offset,
873 struct init_exec *iexec)
874{
875 /*
876 * INIT_IO_RESTRICT_PLL opcode: 0x34 ('4')
877 *
878 * offset (8 bit): opcode
879 * offset + 1 (16 bit): CRTC port
880 * offset + 3 (8 bit): CRTC index
881 * offset + 4 (8 bit): mask
882 * offset + 5 (8 bit): shift
883 * offset + 6 (8 bit): IO flag condition index
884 * offset + 7 (8 bit): count
885 * offset + 8 (32 bit): register
886 * offset + 12 (16 bit): frequency 1
887 * ...
888 *
889 * Starting at offset + 12 there are "count" 16 bit frequencies (10kHz).
890 * Set PLL register "register" to coefficients for frequency n,
891 * selected by reading index "CRTC index" of "CRTC port" ANDed with
892 * "mask" and shifted right by "shift".
893 *
894 * If "IO flag condition index" > 0, and condition met, double
895 * frequency before setting it.
896 */
897
898 uint16_t crtcport = ROM16(bios->data[offset + 1]);
899 uint8_t crtcindex = bios->data[offset + 3];
900 uint8_t mask = bios->data[offset + 4];
901 uint8_t shift = bios->data[offset + 5];
902 int8_t io_flag_condition_idx = bios->data[offset + 6];
903 uint8_t count = bios->data[offset + 7];
904 uint32_t reg = ROM32(bios->data[offset + 8]);
905 uint8_t config;
906 uint16_t freq;
Marcin Kościelnicki37383652009-12-15 00:37:31 +0000907 int len = 12 + count * 2;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000908
909 if (!iexec->execute)
Marcin Kościelnicki37383652009-12-15 00:37:31 +0000910 return len;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000911
912 BIOSLOG(bios, "0x%04X: Port: 0x%04X, Index: 0x%02X, Mask: 0x%02X, "
913 "Shift: 0x%02X, IO Flag Condition: 0x%02X, "
914 "Count: 0x%02X, Reg: 0x%08X\n",
915 offset, crtcport, crtcindex, mask, shift,
916 io_flag_condition_idx, count, reg);
917
918 config = (bios_idxprt_rd(bios, crtcport, crtcindex) & mask) >> shift;
919 if (config > count) {
920 NV_ERROR(bios->dev,
921 "0x%04X: Config 0x%02X exceeds maximal bound 0x%02X\n",
922 offset, config, count);
Marcin Kościelnicki37383652009-12-15 00:37:31 +0000923 return 0;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000924 }
925
926 freq = ROM16(bios->data[offset + 12 + config * 2]);
927
928 if (io_flag_condition_idx > 0) {
929 if (io_flag_condition_met(bios, offset, io_flag_condition_idx)) {
930 BIOSLOG(bios, "0x%04X: Condition fulfilled -- "
931 "frequency doubled\n", offset);
932 freq *= 2;
933 } else
934 BIOSLOG(bios, "0x%04X: Condition not fulfilled -- "
935 "frequency unchanged\n", offset);
936 }
937
938 BIOSLOG(bios, "0x%04X: Reg: 0x%08X, Config: 0x%02X, Freq: %d0kHz\n",
939 offset, reg, config, freq);
940
941 setPLL(bios, reg, freq * 10);
942
Marcin Kościelnicki37383652009-12-15 00:37:31 +0000943 return len;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000944}
945
Marcin Kościelnicki37383652009-12-15 00:37:31 +0000946static int
Ben Skeggs6ee73862009-12-11 19:24:15 +1000947init_end_repeat(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
948{
949 /*
950 * INIT_END_REPEAT opcode: 0x36 ('6')
951 *
952 * offset (8 bit): opcode
953 *
954 * Marks the end of the block for INIT_REPEAT to repeat
955 */
956
957 /* no iexec->execute check by design */
958
959 /*
960 * iexec->repeat flag necessary to go past INIT_END_REPEAT opcode when
961 * we're not in repeat mode
962 */
963 if (iexec->repeat)
Marcin Kościelnicki37383652009-12-15 00:37:31 +0000964 return 0;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000965
Marcin Kościelnicki37383652009-12-15 00:37:31 +0000966 return 1;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000967}
968
Marcin Kościelnicki37383652009-12-15 00:37:31 +0000969static int
Ben Skeggs6ee73862009-12-11 19:24:15 +1000970init_copy(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
971{
972 /*
973 * INIT_COPY opcode: 0x37 ('7')
974 *
975 * offset (8 bit): opcode
976 * offset + 1 (32 bit): register
977 * offset + 5 (8 bit): shift
978 * offset + 6 (8 bit): srcmask
979 * offset + 7 (16 bit): CRTC port
980 * offset + 9 (8 bit): CRTC index
981 * offset + 10 (8 bit): mask
982 *
983 * Read index "CRTC index" on "CRTC port", AND with "mask", OR with
984 * (REGVAL("register") >> "shift" & "srcmask") and write-back to CRTC
985 * port
986 */
987
988 uint32_t reg = ROM32(bios->data[offset + 1]);
989 uint8_t shift = bios->data[offset + 5];
990 uint8_t srcmask = bios->data[offset + 6];
991 uint16_t crtcport = ROM16(bios->data[offset + 7]);
992 uint8_t crtcindex = bios->data[offset + 9];
993 uint8_t mask = bios->data[offset + 10];
994 uint32_t data;
995 uint8_t crtcdata;
996
997 if (!iexec->execute)
Marcin Kościelnicki37383652009-12-15 00:37:31 +0000998 return 11;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000999
1000 BIOSLOG(bios, "0x%04X: Reg: 0x%08X, Shift: 0x%02X, SrcMask: 0x%02X, "
1001 "Port: 0x%04X, Index: 0x%02X, Mask: 0x%02X\n",
1002 offset, reg, shift, srcmask, crtcport, crtcindex, mask);
1003
1004 data = bios_rd32(bios, reg);
1005
1006 if (shift < 0x80)
1007 data >>= shift;
1008 else
1009 data <<= (0x100 - shift);
1010
1011 data &= srcmask;
1012
1013 crtcdata = bios_idxprt_rd(bios, crtcport, crtcindex) & mask;
1014 crtcdata |= (uint8_t)data;
1015 bios_idxprt_wr(bios, crtcport, crtcindex, crtcdata);
1016
Marcin Kościelnicki37383652009-12-15 00:37:31 +00001017 return 11;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001018}
1019
Marcin Kościelnicki37383652009-12-15 00:37:31 +00001020static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10001021init_not(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
1022{
1023 /*
1024 * INIT_NOT opcode: 0x38 ('8')
1025 *
1026 * offset (8 bit): opcode
1027 *
1028 * Invert the current execute / no-execute condition (i.e. "else")
1029 */
1030 if (iexec->execute)
1031 BIOSLOG(bios, "0x%04X: ------ Skipping following commands ------\n", offset);
1032 else
1033 BIOSLOG(bios, "0x%04X: ------ Executing following commands ------\n", offset);
1034
1035 iexec->execute = !iexec->execute;
Marcin Kościelnicki37383652009-12-15 00:37:31 +00001036 return 1;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001037}
1038
Marcin Kościelnicki37383652009-12-15 00:37:31 +00001039static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10001040init_io_flag_condition(struct nvbios *bios, uint16_t offset,
1041 struct init_exec *iexec)
1042{
1043 /*
1044 * INIT_IO_FLAG_CONDITION opcode: 0x39 ('9')
1045 *
1046 * offset (8 bit): opcode
1047 * offset + 1 (8 bit): condition number
1048 *
1049 * Check condition "condition number" in the IO flag condition table.
1050 * If condition not met skip subsequent opcodes until condition is
1051 * inverted (INIT_NOT), or we hit INIT_RESUME
1052 */
1053
1054 uint8_t cond = bios->data[offset + 1];
1055
1056 if (!iexec->execute)
Marcin Kościelnicki37383652009-12-15 00:37:31 +00001057 return 2;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001058
1059 if (io_flag_condition_met(bios, offset, cond))
1060 BIOSLOG(bios, "0x%04X: Condition fulfilled -- continuing to execute\n", offset);
1061 else {
1062 BIOSLOG(bios, "0x%04X: Condition not fulfilled -- skipping following commands\n", offset);
1063 iexec->execute = false;
1064 }
1065
Marcin Kościelnicki37383652009-12-15 00:37:31 +00001066 return 2;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001067}
1068
Marcin Kościelnicki37383652009-12-15 00:37:31 +00001069static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10001070init_idx_addr_latched(struct nvbios *bios, uint16_t offset,
1071 struct init_exec *iexec)
1072{
1073 /*
1074 * INIT_INDEX_ADDRESS_LATCHED opcode: 0x49 ('I')
1075 *
1076 * offset (8 bit): opcode
1077 * offset + 1 (32 bit): control register
1078 * offset + 5 (32 bit): data register
1079 * offset + 9 (32 bit): mask
1080 * offset + 13 (32 bit): data
1081 * offset + 17 (8 bit): count
1082 * offset + 18 (8 bit): address 1
1083 * offset + 19 (8 bit): data 1
1084 * ...
1085 *
1086 * For each of "count" address and data pairs, write "data n" to
1087 * "data register", read the current value of "control register",
1088 * and write it back once ANDed with "mask", ORed with "data",
1089 * and ORed with "address n"
1090 */
1091
1092 uint32_t controlreg = ROM32(bios->data[offset + 1]);
1093 uint32_t datareg = ROM32(bios->data[offset + 5]);
1094 uint32_t mask = ROM32(bios->data[offset + 9]);
1095 uint32_t data = ROM32(bios->data[offset + 13]);
1096 uint8_t count = bios->data[offset + 17];
Marcin Kościelnicki37383652009-12-15 00:37:31 +00001097 int len = 18 + count * 2;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001098 uint32_t value;
1099 int i;
1100
1101 if (!iexec->execute)
Marcin Kościelnicki37383652009-12-15 00:37:31 +00001102 return len;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001103
1104 BIOSLOG(bios, "0x%04X: ControlReg: 0x%08X, DataReg: 0x%08X, "
1105 "Mask: 0x%08X, Data: 0x%08X, Count: 0x%02X\n",
1106 offset, controlreg, datareg, mask, data, count);
1107
1108 for (i = 0; i < count; i++) {
1109 uint8_t instaddress = bios->data[offset + 18 + i * 2];
1110 uint8_t instdata = bios->data[offset + 19 + i * 2];
1111
1112 BIOSLOG(bios, "0x%04X: Address: 0x%02X, Data: 0x%02X\n",
1113 offset, instaddress, instdata);
1114
1115 bios_wr32(bios, datareg, instdata);
1116 value = bios_rd32(bios, controlreg) & mask;
1117 value |= data;
1118 value |= instaddress;
1119 bios_wr32(bios, controlreg, value);
1120 }
1121
Marcin Kościelnicki37383652009-12-15 00:37:31 +00001122 return len;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001123}
1124
Marcin Kościelnicki37383652009-12-15 00:37:31 +00001125static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10001126init_io_restrict_pll2(struct nvbios *bios, uint16_t offset,
1127 struct init_exec *iexec)
1128{
1129 /*
1130 * INIT_IO_RESTRICT_PLL2 opcode: 0x4A ('J')
1131 *
1132 * offset (8 bit): opcode
1133 * offset + 1 (16 bit): CRTC port
1134 * offset + 3 (8 bit): CRTC index
1135 * offset + 4 (8 bit): mask
1136 * offset + 5 (8 bit): shift
1137 * offset + 6 (8 bit): count
1138 * offset + 7 (32 bit): register
1139 * offset + 11 (32 bit): frequency 1
1140 * ...
1141 *
1142 * Starting at offset + 11 there are "count" 32 bit frequencies (kHz).
1143 * Set PLL register "register" to coefficients for frequency n,
1144 * selected by reading index "CRTC index" of "CRTC port" ANDed with
1145 * "mask" and shifted right by "shift".
1146 */
1147
1148 uint16_t crtcport = ROM16(bios->data[offset + 1]);
1149 uint8_t crtcindex = bios->data[offset + 3];
1150 uint8_t mask = bios->data[offset + 4];
1151 uint8_t shift = bios->data[offset + 5];
1152 uint8_t count = bios->data[offset + 6];
1153 uint32_t reg = ROM32(bios->data[offset + 7]);
Marcin Kościelnicki37383652009-12-15 00:37:31 +00001154 int len = 11 + count * 4;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001155 uint8_t config;
1156 uint32_t freq;
1157
1158 if (!iexec->execute)
Marcin Kościelnicki37383652009-12-15 00:37:31 +00001159 return len;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001160
1161 BIOSLOG(bios, "0x%04X: Port: 0x%04X, Index: 0x%02X, Mask: 0x%02X, "
1162 "Shift: 0x%02X, Count: 0x%02X, Reg: 0x%08X\n",
1163 offset, crtcport, crtcindex, mask, shift, count, reg);
1164
1165 if (!reg)
Marcin Kościelnicki37383652009-12-15 00:37:31 +00001166 return len;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001167
1168 config = (bios_idxprt_rd(bios, crtcport, crtcindex) & mask) >> shift;
1169 if (config > count) {
1170 NV_ERROR(bios->dev,
1171 "0x%04X: Config 0x%02X exceeds maximal bound 0x%02X\n",
1172 offset, config, count);
Marcin Kościelnicki37383652009-12-15 00:37:31 +00001173 return 0;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001174 }
1175
1176 freq = ROM32(bios->data[offset + 11 + config * 4]);
1177
1178 BIOSLOG(bios, "0x%04X: Reg: 0x%08X, Config: 0x%02X, Freq: %dkHz\n",
1179 offset, reg, config, freq);
1180
1181 setPLL(bios, reg, freq);
1182
Marcin Kościelnicki37383652009-12-15 00:37:31 +00001183 return len;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001184}
1185
Marcin Kościelnicki37383652009-12-15 00:37:31 +00001186static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10001187init_pll2(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
1188{
1189 /*
1190 * INIT_PLL2 opcode: 0x4B ('K')
1191 *
1192 * offset (8 bit): opcode
1193 * offset + 1 (32 bit): register
1194 * offset + 5 (32 bit): freq
1195 *
1196 * Set PLL register "register" to coefficients for frequency "freq"
1197 */
1198
1199 uint32_t reg = ROM32(bios->data[offset + 1]);
1200 uint32_t freq = ROM32(bios->data[offset + 5]);
1201
1202 if (!iexec->execute)
Marcin Kościelnicki37383652009-12-15 00:37:31 +00001203 return 9;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001204
1205 BIOSLOG(bios, "0x%04X: Reg: 0x%04X, Freq: %dkHz\n",
1206 offset, reg, freq);
1207
1208 setPLL(bios, reg, freq);
Marcin Kościelnicki37383652009-12-15 00:37:31 +00001209 return 9;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001210}
1211
Marcin Kościelnicki37383652009-12-15 00:37:31 +00001212static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10001213init_i2c_byte(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
1214{
1215 /*
1216 * INIT_I2C_BYTE opcode: 0x4C ('L')
1217 *
1218 * offset (8 bit): opcode
1219 * offset + 1 (8 bit): DCB I2C table entry index
1220 * offset + 2 (8 bit): I2C slave address
1221 * offset + 3 (8 bit): count
1222 * offset + 4 (8 bit): I2C register 1
1223 * offset + 5 (8 bit): mask 1
1224 * offset + 6 (8 bit): data 1
1225 * ...
1226 *
1227 * For each of "count" registers given by "I2C register n" on the device
1228 * addressed by "I2C slave address" on the I2C bus given by
1229 * "DCB I2C table entry index", read the register, AND the result with
1230 * "mask n" and OR it with "data n" before writing it back to the device
1231 */
1232
1233 uint8_t i2c_index = bios->data[offset + 1];
1234 uint8_t i2c_address = bios->data[offset + 2];
1235 uint8_t count = bios->data[offset + 3];
Marcin Kościelnicki37383652009-12-15 00:37:31 +00001236 int len = 4 + count * 3;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001237 struct nouveau_i2c_chan *chan;
1238 struct i2c_msg msg;
1239 int i;
1240
1241 if (!iexec->execute)
Marcin Kościelnicki37383652009-12-15 00:37:31 +00001242 return len;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001243
1244 BIOSLOG(bios, "0x%04X: DCBI2CIndex: 0x%02X, I2CAddress: 0x%02X, "
1245 "Count: 0x%02X\n",
1246 offset, i2c_index, i2c_address, count);
1247
1248 chan = init_i2c_device_find(bios->dev, i2c_index);
1249 if (!chan)
Marcin Kościelnicki37383652009-12-15 00:37:31 +00001250 return 0;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001251
1252 for (i = 0; i < count; i++) {
1253 uint8_t i2c_reg = bios->data[offset + 4 + i * 3];
1254 uint8_t mask = bios->data[offset + 5 + i * 3];
1255 uint8_t data = bios->data[offset + 6 + i * 3];
1256 uint8_t value;
1257
1258 msg.addr = i2c_address;
1259 msg.flags = I2C_M_RD;
1260 msg.len = 1;
1261 msg.buf = &value;
1262 if (i2c_transfer(&chan->adapter, &msg, 1) != 1)
Marcin Kościelnicki37383652009-12-15 00:37:31 +00001263 return 0;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001264
1265 BIOSLOG(bios, "0x%04X: I2CReg: 0x%02X, Value: 0x%02X, "
1266 "Mask: 0x%02X, Data: 0x%02X\n",
1267 offset, i2c_reg, value, mask, data);
1268
1269 value = (value & mask) | data;
1270
1271 if (bios->execute) {
1272 msg.addr = i2c_address;
1273 msg.flags = 0;
1274 msg.len = 1;
1275 msg.buf = &value;
1276 if (i2c_transfer(&chan->adapter, &msg, 1) != 1)
Marcin Kościelnicki37383652009-12-15 00:37:31 +00001277 return 0;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001278 }
1279 }
1280
Marcin Kościelnicki37383652009-12-15 00:37:31 +00001281 return len;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001282}
1283
Marcin Kościelnicki37383652009-12-15 00:37:31 +00001284static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10001285init_zm_i2c_byte(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
1286{
1287 /*
1288 * INIT_ZM_I2C_BYTE opcode: 0x4D ('M')
1289 *
1290 * offset (8 bit): opcode
1291 * offset + 1 (8 bit): DCB I2C table entry index
1292 * offset + 2 (8 bit): I2C slave address
1293 * offset + 3 (8 bit): count
1294 * offset + 4 (8 bit): I2C register 1
1295 * offset + 5 (8 bit): data 1
1296 * ...
1297 *
1298 * For each of "count" registers given by "I2C register n" on the device
1299 * addressed by "I2C slave address" on the I2C bus given by
1300 * "DCB I2C table entry index", set the register to "data n"
1301 */
1302
1303 uint8_t i2c_index = bios->data[offset + 1];
1304 uint8_t i2c_address = bios->data[offset + 2];
1305 uint8_t count = bios->data[offset + 3];
Marcin Kościelnicki37383652009-12-15 00:37:31 +00001306 int len = 4 + count * 2;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001307 struct nouveau_i2c_chan *chan;
1308 struct i2c_msg msg;
1309 int i;
1310
1311 if (!iexec->execute)
Marcin Kościelnicki37383652009-12-15 00:37:31 +00001312 return len;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001313
1314 BIOSLOG(bios, "0x%04X: DCBI2CIndex: 0x%02X, I2CAddress: 0x%02X, "
1315 "Count: 0x%02X\n",
1316 offset, i2c_index, i2c_address, count);
1317
1318 chan = init_i2c_device_find(bios->dev, i2c_index);
1319 if (!chan)
Marcin Kościelnicki37383652009-12-15 00:37:31 +00001320 return 0;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001321
1322 for (i = 0; i < count; i++) {
1323 uint8_t i2c_reg = bios->data[offset + 4 + i * 2];
1324 uint8_t data = bios->data[offset + 5 + i * 2];
1325
1326 BIOSLOG(bios, "0x%04X: I2CReg: 0x%02X, Data: 0x%02X\n",
1327 offset, i2c_reg, data);
1328
1329 if (bios->execute) {
1330 msg.addr = i2c_address;
1331 msg.flags = 0;
1332 msg.len = 1;
1333 msg.buf = &data;
1334 if (i2c_transfer(&chan->adapter, &msg, 1) != 1)
Marcin Kościelnicki37383652009-12-15 00:37:31 +00001335 return 0;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001336 }
1337 }
1338
Marcin Kościelnicki37383652009-12-15 00:37:31 +00001339 return len;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001340}
1341
Marcin Kościelnicki37383652009-12-15 00:37:31 +00001342static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10001343init_zm_i2c(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
1344{
1345 /*
1346 * INIT_ZM_I2C opcode: 0x4E ('N')
1347 *
1348 * offset (8 bit): opcode
1349 * offset + 1 (8 bit): DCB I2C table entry index
1350 * offset + 2 (8 bit): I2C slave address
1351 * offset + 3 (8 bit): count
1352 * offset + 4 (8 bit): data 1
1353 * ...
1354 *
1355 * Send "count" bytes ("data n") to the device addressed by "I2C slave
1356 * address" on the I2C bus given by "DCB I2C table entry index"
1357 */
1358
1359 uint8_t i2c_index = bios->data[offset + 1];
1360 uint8_t i2c_address = bios->data[offset + 2];
1361 uint8_t count = bios->data[offset + 3];
Marcin Kościelnicki37383652009-12-15 00:37:31 +00001362 int len = 4 + count;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001363 struct nouveau_i2c_chan *chan;
1364 struct i2c_msg msg;
1365 uint8_t data[256];
1366 int i;
1367
1368 if (!iexec->execute)
Marcin Kościelnicki37383652009-12-15 00:37:31 +00001369 return len;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001370
1371 BIOSLOG(bios, "0x%04X: DCBI2CIndex: 0x%02X, I2CAddress: 0x%02X, "
1372 "Count: 0x%02X\n",
1373 offset, i2c_index, i2c_address, count);
1374
1375 chan = init_i2c_device_find(bios->dev, i2c_index);
1376 if (!chan)
Marcin Kościelnicki37383652009-12-15 00:37:31 +00001377 return 0;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001378
1379 for (i = 0; i < count; i++) {
1380 data[i] = bios->data[offset + 4 + i];
1381
1382 BIOSLOG(bios, "0x%04X: Data: 0x%02X\n", offset, data[i]);
1383 }
1384
1385 if (bios->execute) {
1386 msg.addr = i2c_address;
1387 msg.flags = 0;
1388 msg.len = count;
1389 msg.buf = data;
1390 if (i2c_transfer(&chan->adapter, &msg, 1) != 1)
Marcin Kościelnicki37383652009-12-15 00:37:31 +00001391 return 0;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001392 }
1393
Marcin Kościelnicki37383652009-12-15 00:37:31 +00001394 return len;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001395}
1396
Marcin Kościelnicki37383652009-12-15 00:37:31 +00001397static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10001398init_tmds(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
1399{
1400 /*
1401 * INIT_TMDS opcode: 0x4F ('O') (non-canon name)
1402 *
1403 * offset (8 bit): opcode
1404 * offset + 1 (8 bit): magic lookup value
1405 * offset + 2 (8 bit): TMDS address
1406 * offset + 3 (8 bit): mask
1407 * offset + 4 (8 bit): data
1408 *
1409 * Read the data reg for TMDS address "TMDS address", AND it with mask
1410 * and OR it with data, then write it back
1411 * "magic lookup value" determines which TMDS base address register is
1412 * used -- see get_tmds_index_reg()
1413 */
1414
1415 uint8_t mlv = bios->data[offset + 1];
1416 uint32_t tmdsaddr = bios->data[offset + 2];
1417 uint8_t mask = bios->data[offset + 3];
1418 uint8_t data = bios->data[offset + 4];
1419 uint32_t reg, value;
1420
1421 if (!iexec->execute)
Marcin Kościelnicki37383652009-12-15 00:37:31 +00001422 return 5;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001423
1424 BIOSLOG(bios, "0x%04X: MagicLookupValue: 0x%02X, TMDSAddr: 0x%02X, "
1425 "Mask: 0x%02X, Data: 0x%02X\n",
1426 offset, mlv, tmdsaddr, mask, data);
1427
1428 reg = get_tmds_index_reg(bios->dev, mlv);
1429 if (!reg)
Marcin Kościelnicki37383652009-12-15 00:37:31 +00001430 return 0;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001431
1432 bios_wr32(bios, reg,
1433 tmdsaddr | NV_PRAMDAC_FP_TMDS_CONTROL_WRITE_DISABLE);
1434 value = (bios_rd32(bios, reg + 4) & mask) | data;
1435 bios_wr32(bios, reg + 4, value);
1436 bios_wr32(bios, reg, tmdsaddr);
1437
Marcin Kościelnicki37383652009-12-15 00:37:31 +00001438 return 5;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001439}
1440
Marcin Kościelnicki37383652009-12-15 00:37:31 +00001441static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10001442init_zm_tmds_group(struct nvbios *bios, uint16_t offset,
1443 struct init_exec *iexec)
1444{
1445 /*
1446 * INIT_ZM_TMDS_GROUP opcode: 0x50 ('P') (non-canon name)
1447 *
1448 * offset (8 bit): opcode
1449 * offset + 1 (8 bit): magic lookup value
1450 * offset + 2 (8 bit): count
1451 * offset + 3 (8 bit): addr 1
1452 * offset + 4 (8 bit): data 1
1453 * ...
1454 *
1455 * For each of "count" TMDS address and data pairs write "data n" to
1456 * "addr n". "magic lookup value" determines which TMDS base address
1457 * register is used -- see get_tmds_index_reg()
1458 */
1459
1460 uint8_t mlv = bios->data[offset + 1];
1461 uint8_t count = bios->data[offset + 2];
Marcin Kościelnicki37383652009-12-15 00:37:31 +00001462 int len = 3 + count * 2;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001463 uint32_t reg;
1464 int i;
1465
1466 if (!iexec->execute)
Marcin Kościelnicki37383652009-12-15 00:37:31 +00001467 return len;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001468
1469 BIOSLOG(bios, "0x%04X: MagicLookupValue: 0x%02X, Count: 0x%02X\n",
1470 offset, mlv, count);
1471
1472 reg = get_tmds_index_reg(bios->dev, mlv);
1473 if (!reg)
Marcin Kościelnicki37383652009-12-15 00:37:31 +00001474 return 0;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001475
1476 for (i = 0; i < count; i++) {
1477 uint8_t tmdsaddr = bios->data[offset + 3 + i * 2];
1478 uint8_t tmdsdata = bios->data[offset + 4 + i * 2];
1479
1480 bios_wr32(bios, reg + 4, tmdsdata);
1481 bios_wr32(bios, reg, tmdsaddr);
1482 }
1483
Marcin Kościelnicki37383652009-12-15 00:37:31 +00001484 return len;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001485}
1486
Marcin Kościelnicki37383652009-12-15 00:37:31 +00001487static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10001488init_cr_idx_adr_latch(struct nvbios *bios, uint16_t offset,
1489 struct init_exec *iexec)
1490{
1491 /*
1492 * INIT_CR_INDEX_ADDRESS_LATCHED opcode: 0x51 ('Q')
1493 *
1494 * offset (8 bit): opcode
1495 * offset + 1 (8 bit): CRTC index1
1496 * offset + 2 (8 bit): CRTC index2
1497 * offset + 3 (8 bit): baseaddr
1498 * offset + 4 (8 bit): count
1499 * offset + 5 (8 bit): data 1
1500 * ...
1501 *
1502 * For each of "count" address and data pairs, write "baseaddr + n" to
1503 * "CRTC index1" and "data n" to "CRTC index2"
1504 * Once complete, restore initial value read from "CRTC index1"
1505 */
1506 uint8_t crtcindex1 = bios->data[offset + 1];
1507 uint8_t crtcindex2 = bios->data[offset + 2];
1508 uint8_t baseaddr = bios->data[offset + 3];
1509 uint8_t count = bios->data[offset + 4];
Marcin Kościelnicki37383652009-12-15 00:37:31 +00001510 int len = 5 + count;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001511 uint8_t oldaddr, data;
1512 int i;
1513
1514 if (!iexec->execute)
Marcin Kościelnicki37383652009-12-15 00:37:31 +00001515 return len;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001516
1517 BIOSLOG(bios, "0x%04X: Index1: 0x%02X, Index2: 0x%02X, "
1518 "BaseAddr: 0x%02X, Count: 0x%02X\n",
1519 offset, crtcindex1, crtcindex2, baseaddr, count);
1520
1521 oldaddr = bios_idxprt_rd(bios, NV_CIO_CRX__COLOR, crtcindex1);
1522
1523 for (i = 0; i < count; i++) {
1524 bios_idxprt_wr(bios, NV_CIO_CRX__COLOR, crtcindex1,
1525 baseaddr + i);
1526 data = bios->data[offset + 5 + i];
1527 bios_idxprt_wr(bios, NV_CIO_CRX__COLOR, crtcindex2, data);
1528 }
1529
1530 bios_idxprt_wr(bios, NV_CIO_CRX__COLOR, crtcindex1, oldaddr);
1531
Marcin Kościelnicki37383652009-12-15 00:37:31 +00001532 return len;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001533}
1534
Marcin Kościelnicki37383652009-12-15 00:37:31 +00001535static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10001536init_cr(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
1537{
1538 /*
1539 * INIT_CR opcode: 0x52 ('R')
1540 *
1541 * offset (8 bit): opcode
1542 * offset + 1 (8 bit): CRTC index
1543 * offset + 2 (8 bit): mask
1544 * offset + 3 (8 bit): data
1545 *
1546 * Assign the value of at "CRTC index" ANDed with mask and ORed with
1547 * data back to "CRTC index"
1548 */
1549
1550 uint8_t crtcindex = bios->data[offset + 1];
1551 uint8_t mask = bios->data[offset + 2];
1552 uint8_t data = bios->data[offset + 3];
1553 uint8_t value;
1554
1555 if (!iexec->execute)
Marcin Kościelnicki37383652009-12-15 00:37:31 +00001556 return 4;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001557
1558 BIOSLOG(bios, "0x%04X: Index: 0x%02X, Mask: 0x%02X, Data: 0x%02X\n",
1559 offset, crtcindex, mask, data);
1560
1561 value = bios_idxprt_rd(bios, NV_CIO_CRX__COLOR, crtcindex) & mask;
1562 value |= data;
1563 bios_idxprt_wr(bios, NV_CIO_CRX__COLOR, crtcindex, value);
1564
Marcin Kościelnicki37383652009-12-15 00:37:31 +00001565 return 4;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001566}
1567
Marcin Kościelnicki37383652009-12-15 00:37:31 +00001568static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10001569init_zm_cr(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
1570{
1571 /*
1572 * INIT_ZM_CR opcode: 0x53 ('S')
1573 *
1574 * offset (8 bit): opcode
1575 * offset + 1 (8 bit): CRTC index
1576 * offset + 2 (8 bit): value
1577 *
1578 * Assign "value" to CRTC register with index "CRTC index".
1579 */
1580
1581 uint8_t crtcindex = ROM32(bios->data[offset + 1]);
1582 uint8_t data = bios->data[offset + 2];
1583
1584 if (!iexec->execute)
Marcin Kościelnicki37383652009-12-15 00:37:31 +00001585 return 3;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001586
1587 bios_idxprt_wr(bios, NV_CIO_CRX__COLOR, crtcindex, data);
1588
Marcin Kościelnicki37383652009-12-15 00:37:31 +00001589 return 3;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001590}
1591
Marcin Kościelnicki37383652009-12-15 00:37:31 +00001592static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10001593init_zm_cr_group(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
1594{
1595 /*
1596 * INIT_ZM_CR_GROUP opcode: 0x54 ('T')
1597 *
1598 * offset (8 bit): opcode
1599 * offset + 1 (8 bit): count
1600 * offset + 2 (8 bit): CRTC index 1
1601 * offset + 3 (8 bit): value 1
1602 * ...
1603 *
1604 * For "count", assign "value n" to CRTC register with index
1605 * "CRTC index n".
1606 */
1607
1608 uint8_t count = bios->data[offset + 1];
Marcin Kościelnicki37383652009-12-15 00:37:31 +00001609 int len = 2 + count * 2;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001610 int i;
1611
1612 if (!iexec->execute)
Marcin Kościelnicki37383652009-12-15 00:37:31 +00001613 return len;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001614
1615 for (i = 0; i < count; i++)
1616 init_zm_cr(bios, offset + 2 + 2 * i - 1, iexec);
1617
Marcin Kościelnicki37383652009-12-15 00:37:31 +00001618 return len;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001619}
1620
Marcin Kościelnicki37383652009-12-15 00:37:31 +00001621static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10001622init_condition_time(struct nvbios *bios, uint16_t offset,
1623 struct init_exec *iexec)
1624{
1625 /*
1626 * INIT_CONDITION_TIME opcode: 0x56 ('V')
1627 *
1628 * offset (8 bit): opcode
1629 * offset + 1 (8 bit): condition number
1630 * offset + 2 (8 bit): retries / 50
1631 *
1632 * Check condition "condition number" in the condition table.
1633 * Bios code then sleeps for 2ms if the condition is not met, and
1634 * repeats up to "retries" times, but on one C51 this has proved
1635 * insufficient. In mmiotraces the driver sleeps for 20ms, so we do
1636 * this, and bail after "retries" times, or 2s, whichever is less.
1637 * If still not met after retries, clear execution flag for this table.
1638 */
1639
1640 uint8_t cond = bios->data[offset + 1];
1641 uint16_t retries = bios->data[offset + 2] * 50;
1642 unsigned cnt;
1643
1644 if (!iexec->execute)
Marcin Kościelnicki37383652009-12-15 00:37:31 +00001645 return 3;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001646
1647 if (retries > 100)
1648 retries = 100;
1649
1650 BIOSLOG(bios, "0x%04X: Condition: 0x%02X, Retries: 0x%02X\n",
1651 offset, cond, retries);
1652
1653 if (!bios->execute) /* avoid 2s delays when "faking" execution */
1654 retries = 1;
1655
1656 for (cnt = 0; cnt < retries; cnt++) {
1657 if (bios_condition_met(bios, offset, cond)) {
1658 BIOSLOG(bios, "0x%04X: Condition met, continuing\n",
1659 offset);
1660 break;
1661 } else {
1662 BIOSLOG(bios, "0x%04X: "
1663 "Condition not met, sleeping for 20ms\n",
1664 offset);
1665 msleep(20);
1666 }
1667 }
1668
1669 if (!bios_condition_met(bios, offset, cond)) {
1670 NV_WARN(bios->dev,
1671 "0x%04X: Condition still not met after %dms, "
1672 "skipping following opcodes\n", offset, 20 * retries);
1673 iexec->execute = false;
1674 }
1675
Marcin Kościelnicki37383652009-12-15 00:37:31 +00001676 return 3;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001677}
1678
Marcin Kościelnicki37383652009-12-15 00:37:31 +00001679static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10001680init_zm_reg_sequence(struct nvbios *bios, uint16_t offset,
1681 struct init_exec *iexec)
1682{
1683 /*
1684 * INIT_ZM_REG_SEQUENCE opcode: 0x58 ('X')
1685 *
1686 * offset (8 bit): opcode
1687 * offset + 1 (32 bit): base register
1688 * offset + 5 (8 bit): count
1689 * offset + 6 (32 bit): value 1
1690 * ...
1691 *
1692 * Starting at offset + 6 there are "count" 32 bit values.
1693 * For "count" iterations set "base register" + 4 * current_iteration
1694 * to "value current_iteration"
1695 */
1696
1697 uint32_t basereg = ROM32(bios->data[offset + 1]);
1698 uint32_t count = bios->data[offset + 5];
Marcin Kościelnicki37383652009-12-15 00:37:31 +00001699 int len = 6 + count * 4;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001700 int i;
1701
1702 if (!iexec->execute)
Marcin Kościelnicki37383652009-12-15 00:37:31 +00001703 return len;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001704
1705 BIOSLOG(bios, "0x%04X: BaseReg: 0x%08X, Count: 0x%02X\n",
1706 offset, basereg, count);
1707
1708 for (i = 0; i < count; i++) {
1709 uint32_t reg = basereg + i * 4;
1710 uint32_t data = ROM32(bios->data[offset + 6 + i * 4]);
1711
1712 bios_wr32(bios, reg, data);
1713 }
1714
Marcin Kościelnicki37383652009-12-15 00:37:31 +00001715 return len;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001716}
1717
Marcin Kościelnicki37383652009-12-15 00:37:31 +00001718static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10001719init_sub_direct(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
1720{
1721 /*
1722 * INIT_SUB_DIRECT opcode: 0x5B ('[')
1723 *
1724 * offset (8 bit): opcode
1725 * offset + 1 (16 bit): subroutine offset (in bios)
1726 *
1727 * Calls a subroutine that will execute commands until INIT_DONE
1728 * is found.
1729 */
1730
1731 uint16_t sub_offset = ROM16(bios->data[offset + 1]);
1732
1733 if (!iexec->execute)
Marcin Kościelnicki37383652009-12-15 00:37:31 +00001734 return 3;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001735
1736 BIOSLOG(bios, "0x%04X: Executing subroutine at 0x%04X\n",
1737 offset, sub_offset);
1738
1739 parse_init_table(bios, sub_offset, iexec);
1740
1741 BIOSLOG(bios, "0x%04X: End of 0x%04X subroutine\n", offset, sub_offset);
1742
Marcin Kościelnicki37383652009-12-15 00:37:31 +00001743 return 3;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001744}
1745
Marcin Kościelnicki37383652009-12-15 00:37:31 +00001746static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10001747init_copy_nv_reg(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
1748{
1749 /*
1750 * INIT_COPY_NV_REG opcode: 0x5F ('_')
1751 *
1752 * offset (8 bit): opcode
1753 * offset + 1 (32 bit): src reg
1754 * offset + 5 (8 bit): shift
1755 * offset + 6 (32 bit): src mask
1756 * offset + 10 (32 bit): xor
1757 * offset + 14 (32 bit): dst reg
1758 * offset + 18 (32 bit): dst mask
1759 *
1760 * Shift REGVAL("src reg") right by (signed) "shift", AND result with
1761 * "src mask", then XOR with "xor". Write this OR'd with
1762 * (REGVAL("dst reg") AND'd with "dst mask") to "dst reg"
1763 */
1764
1765 uint32_t srcreg = *((uint32_t *)(&bios->data[offset + 1]));
1766 uint8_t shift = bios->data[offset + 5];
1767 uint32_t srcmask = *((uint32_t *)(&bios->data[offset + 6]));
1768 uint32_t xor = *((uint32_t *)(&bios->data[offset + 10]));
1769 uint32_t dstreg = *((uint32_t *)(&bios->data[offset + 14]));
1770 uint32_t dstmask = *((uint32_t *)(&bios->data[offset + 18]));
1771 uint32_t srcvalue, dstvalue;
1772
1773 if (!iexec->execute)
Marcin Kościelnicki37383652009-12-15 00:37:31 +00001774 return 22;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001775
1776 BIOSLOG(bios, "0x%04X: SrcReg: 0x%08X, Shift: 0x%02X, SrcMask: 0x%08X, "
1777 "Xor: 0x%08X, DstReg: 0x%08X, DstMask: 0x%08X\n",
1778 offset, srcreg, shift, srcmask, xor, dstreg, dstmask);
1779
1780 srcvalue = bios_rd32(bios, srcreg);
1781
1782 if (shift < 0x80)
1783 srcvalue >>= shift;
1784 else
1785 srcvalue <<= (0x100 - shift);
1786
1787 srcvalue = (srcvalue & srcmask) ^ xor;
1788
1789 dstvalue = bios_rd32(bios, dstreg) & dstmask;
1790
1791 bios_wr32(bios, dstreg, dstvalue | srcvalue);
1792
Marcin Kościelnicki37383652009-12-15 00:37:31 +00001793 return 22;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001794}
1795
Marcin Kościelnicki37383652009-12-15 00:37:31 +00001796static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10001797init_zm_index_io(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
1798{
1799 /*
1800 * INIT_ZM_INDEX_IO opcode: 0x62 ('b')
1801 *
1802 * offset (8 bit): opcode
1803 * offset + 1 (16 bit): CRTC port
1804 * offset + 3 (8 bit): CRTC index
1805 * offset + 4 (8 bit): data
1806 *
1807 * Write "data" to index "CRTC index" of "CRTC port"
1808 */
1809 uint16_t crtcport = ROM16(bios->data[offset + 1]);
1810 uint8_t crtcindex = bios->data[offset + 3];
1811 uint8_t data = bios->data[offset + 4];
1812
1813 if (!iexec->execute)
Marcin Kościelnicki37383652009-12-15 00:37:31 +00001814 return 5;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001815
1816 bios_idxprt_wr(bios, crtcport, crtcindex, data);
1817
Marcin Kościelnicki37383652009-12-15 00:37:31 +00001818 return 5;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001819}
1820
Marcin Kościelnicki37383652009-12-15 00:37:31 +00001821static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10001822init_compute_mem(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
1823{
1824 /*
1825 * INIT_COMPUTE_MEM opcode: 0x63 ('c')
1826 *
1827 * offset (8 bit): opcode
1828 *
1829 * This opcode is meant to set NV_PFB_CFG0 (0x100200) appropriately so
1830 * that the hardware can correctly calculate how much VRAM it has
1831 * (and subsequently report that value in NV_PFB_CSTATUS (0x10020C))
1832 *
1833 * The implementation of this opcode in general consists of two parts:
1834 * 1) determination of the memory bus width
1835 * 2) determination of how many of the card's RAM pads have ICs attached
1836 *
1837 * 1) is done by a cunning combination of writes to offsets 0x1c and
1838 * 0x3c in the framebuffer, and seeing whether the written values are
1839 * read back correctly. This then affects bits 4-7 of NV_PFB_CFG0
1840 *
1841 * 2) is done by a cunning combination of writes to an offset slightly
1842 * less than the maximum memory reported by NV_PFB_CSTATUS, then seeing
1843 * if the test pattern can be read back. This then affects bits 12-15 of
1844 * NV_PFB_CFG0
1845 *
1846 * In this context a "cunning combination" may include multiple reads
1847 * and writes to varying locations, often alternating the test pattern
1848 * and 0, doubtless to make sure buffers are filled, residual charges
1849 * on tracks are removed etc.
1850 *
1851 * Unfortunately, the "cunning combination"s mentioned above, and the
1852 * changes to the bits in NV_PFB_CFG0 differ with nearly every bios
1853 * trace I have.
1854 *
1855 * Therefore, we cheat and assume the value of NV_PFB_CFG0 with which
1856 * we started was correct, and use that instead
1857 */
1858
1859 /* no iexec->execute check by design */
1860
1861 /*
1862 * This appears to be a NOP on G8x chipsets, both io logs of the VBIOS
1863 * and kmmio traces of the binary driver POSTing the card show nothing
1864 * being done for this opcode. why is it still listed in the table?!
1865 */
1866
1867 struct drm_nouveau_private *dev_priv = bios->dev->dev_private;
1868
Ben Skeggse235c1f2010-01-22 13:17:28 +10001869 if (dev_priv->card_type >= NV_40)
Marcin Kościelnicki37383652009-12-15 00:37:31 +00001870 return 1;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001871
1872 /*
1873 * On every card I've seen, this step gets done for us earlier in
1874 * the init scripts
1875 uint8_t crdata = bios_idxprt_rd(dev, NV_VIO_SRX, 0x01);
1876 bios_idxprt_wr(dev, NV_VIO_SRX, 0x01, crdata | 0x20);
1877 */
1878
1879 /*
1880 * This also has probably been done in the scripts, but an mmio trace of
1881 * s3 resume shows nvidia doing it anyway (unlike the NV_VIO_SRX write)
1882 */
1883 bios_wr32(bios, NV_PFB_REFCTRL, NV_PFB_REFCTRL_VALID_1);
1884
1885 /* write back the saved configuration value */
1886 bios_wr32(bios, NV_PFB_CFG0, bios->state.saved_nv_pfb_cfg0);
1887
Marcin Kościelnicki37383652009-12-15 00:37:31 +00001888 return 1;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001889}
1890
Marcin Kościelnicki37383652009-12-15 00:37:31 +00001891static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10001892init_reset(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
1893{
1894 /*
1895 * INIT_RESET opcode: 0x65 ('e')
1896 *
1897 * offset (8 bit): opcode
1898 * offset + 1 (32 bit): register
1899 * offset + 5 (32 bit): value1
1900 * offset + 9 (32 bit): value2
1901 *
1902 * Assign "value1" to "register", then assign "value2" to "register"
1903 */
1904
1905 uint32_t reg = ROM32(bios->data[offset + 1]);
1906 uint32_t value1 = ROM32(bios->data[offset + 5]);
1907 uint32_t value2 = ROM32(bios->data[offset + 9]);
1908 uint32_t pci_nv_19, pci_nv_20;
1909
1910 /* no iexec->execute check by design */
1911
1912 pci_nv_19 = bios_rd32(bios, NV_PBUS_PCI_NV_19);
1913 bios_wr32(bios, NV_PBUS_PCI_NV_19, 0);
1914 bios_wr32(bios, reg, value1);
1915
1916 udelay(10);
1917
1918 bios_wr32(bios, reg, value2);
1919 bios_wr32(bios, NV_PBUS_PCI_NV_19, pci_nv_19);
1920
1921 pci_nv_20 = bios_rd32(bios, NV_PBUS_PCI_NV_20);
1922 pci_nv_20 &= ~NV_PBUS_PCI_NV_20_ROM_SHADOW_ENABLED; /* 0xfffffffe */
1923 bios_wr32(bios, NV_PBUS_PCI_NV_20, pci_nv_20);
1924
Marcin Kościelnicki37383652009-12-15 00:37:31 +00001925 return 13;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001926}
1927
Marcin Kościelnicki37383652009-12-15 00:37:31 +00001928static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10001929init_configure_mem(struct nvbios *bios, uint16_t offset,
1930 struct init_exec *iexec)
1931{
1932 /*
1933 * INIT_CONFIGURE_MEM opcode: 0x66 ('f')
1934 *
1935 * offset (8 bit): opcode
1936 *
1937 * Equivalent to INIT_DONE on bios version 3 or greater.
1938 * For early bios versions, sets up the memory registers, using values
1939 * taken from the memory init table
1940 */
1941
1942 /* no iexec->execute check by design */
1943
1944 uint16_t meminitoffs = bios->legacy.mem_init_tbl_ptr + MEM_INIT_SIZE * (bios_idxprt_rd(bios, NV_CIO_CRX__COLOR, NV_CIO_CRE_SCRATCH4__INDEX) >> 4);
1945 uint16_t seqtbloffs = bios->legacy.sdr_seq_tbl_ptr, meminitdata = meminitoffs + 6;
1946 uint32_t reg, data;
1947
1948 if (bios->major_version > 2)
Marcin Kościelnicki37383652009-12-15 00:37:31 +00001949 return 0;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001950
1951 bios_idxprt_wr(bios, NV_VIO_SRX, NV_VIO_SR_CLOCK_INDEX, bios_idxprt_rd(
1952 bios, NV_VIO_SRX, NV_VIO_SR_CLOCK_INDEX) | 0x20);
1953
1954 if (bios->data[meminitoffs] & 1)
1955 seqtbloffs = bios->legacy.ddr_seq_tbl_ptr;
1956
1957 for (reg = ROM32(bios->data[seqtbloffs]);
1958 reg != 0xffffffff;
1959 reg = ROM32(bios->data[seqtbloffs += 4])) {
1960
1961 switch (reg) {
1962 case NV_PFB_PRE:
1963 data = NV_PFB_PRE_CMD_PRECHARGE;
1964 break;
1965 case NV_PFB_PAD:
1966 data = NV_PFB_PAD_CKE_NORMAL;
1967 break;
1968 case NV_PFB_REF:
1969 data = NV_PFB_REF_CMD_REFRESH;
1970 break;
1971 default:
1972 data = ROM32(bios->data[meminitdata]);
1973 meminitdata += 4;
1974 if (data == 0xffffffff)
1975 continue;
1976 }
1977
1978 bios_wr32(bios, reg, data);
1979 }
1980
Marcin Kościelnicki37383652009-12-15 00:37:31 +00001981 return 1;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001982}
1983
Marcin Kościelnicki37383652009-12-15 00:37:31 +00001984static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10001985init_configure_clk(struct nvbios *bios, uint16_t offset,
1986 struct init_exec *iexec)
1987{
1988 /*
1989 * INIT_CONFIGURE_CLK opcode: 0x67 ('g')
1990 *
1991 * offset (8 bit): opcode
1992 *
1993 * Equivalent to INIT_DONE on bios version 3 or greater.
1994 * For early bios versions, sets up the NVClk and MClk PLLs, using
1995 * values taken from the memory init table
1996 */
1997
1998 /* no iexec->execute check by design */
1999
2000 uint16_t meminitoffs = bios->legacy.mem_init_tbl_ptr + MEM_INIT_SIZE * (bios_idxprt_rd(bios, NV_CIO_CRX__COLOR, NV_CIO_CRE_SCRATCH4__INDEX) >> 4);
2001 int clock;
2002
2003 if (bios->major_version > 2)
Marcin Kościelnicki37383652009-12-15 00:37:31 +00002004 return 0;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002005
2006 clock = ROM16(bios->data[meminitoffs + 4]) * 10;
2007 setPLL(bios, NV_PRAMDAC_NVPLL_COEFF, clock);
2008
2009 clock = ROM16(bios->data[meminitoffs + 2]) * 10;
2010 if (bios->data[meminitoffs] & 1) /* DDR */
2011 clock *= 2;
2012 setPLL(bios, NV_PRAMDAC_MPLL_COEFF, clock);
2013
Marcin Kościelnicki37383652009-12-15 00:37:31 +00002014 return 1;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002015}
2016
Marcin Kościelnicki37383652009-12-15 00:37:31 +00002017static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10002018init_configure_preinit(struct nvbios *bios, uint16_t offset,
2019 struct init_exec *iexec)
2020{
2021 /*
2022 * INIT_CONFIGURE_PREINIT opcode: 0x68 ('h')
2023 *
2024 * offset (8 bit): opcode
2025 *
2026 * Equivalent to INIT_DONE on bios version 3 or greater.
2027 * For early bios versions, does early init, loading ram and crystal
2028 * configuration from straps into CR3C
2029 */
2030
2031 /* no iexec->execute check by design */
2032
2033 uint32_t straps = bios_rd32(bios, NV_PEXTDEV_BOOT_0);
2034 uint8_t cr3c = ((straps << 2) & 0xf0) | (straps & (1 << 6));
2035
2036 if (bios->major_version > 2)
Marcin Kościelnicki37383652009-12-15 00:37:31 +00002037 return 0;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002038
2039 bios_idxprt_wr(bios, NV_CIO_CRX__COLOR,
2040 NV_CIO_CRE_SCRATCH4__INDEX, cr3c);
2041
Marcin Kościelnicki37383652009-12-15 00:37:31 +00002042 return 1;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002043}
2044
Marcin Kościelnicki37383652009-12-15 00:37:31 +00002045static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10002046init_io(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
2047{
2048 /*
2049 * INIT_IO opcode: 0x69 ('i')
2050 *
2051 * offset (8 bit): opcode
2052 * offset + 1 (16 bit): CRTC port
2053 * offset + 3 (8 bit): mask
2054 * offset + 4 (8 bit): data
2055 *
2056 * Assign ((IOVAL("crtc port") & "mask") | "data") to "crtc port"
2057 */
2058
2059 struct drm_nouveau_private *dev_priv = bios->dev->dev_private;
2060 uint16_t crtcport = ROM16(bios->data[offset + 1]);
2061 uint8_t mask = bios->data[offset + 3];
2062 uint8_t data = bios->data[offset + 4];
2063
2064 if (!iexec->execute)
Marcin Kościelnicki37383652009-12-15 00:37:31 +00002065 return 5;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002066
2067 BIOSLOG(bios, "0x%04X: Port: 0x%04X, Mask: 0x%02X, Data: 0x%02X\n",
2068 offset, crtcport, mask, data);
2069
2070 /*
2071 * I have no idea what this does, but NVIDIA do this magic sequence
2072 * in the places where this INIT_IO happens..
2073 */
2074 if (dev_priv->card_type >= NV_50 && crtcport == 0x3c3 && data == 1) {
2075 int i;
2076
2077 bios_wr32(bios, 0x614100, (bios_rd32(
2078 bios, 0x614100) & 0x0fffffff) | 0x00800000);
2079
2080 bios_wr32(bios, 0x00e18c, bios_rd32(
2081 bios, 0x00e18c) | 0x00020000);
2082
2083 bios_wr32(bios, 0x614900, (bios_rd32(
2084 bios, 0x614900) & 0x0fffffff) | 0x00800000);
2085
2086 bios_wr32(bios, 0x000200, bios_rd32(
2087 bios, 0x000200) & ~0x40000000);
2088
2089 mdelay(10);
2090
2091 bios_wr32(bios, 0x00e18c, bios_rd32(
2092 bios, 0x00e18c) & ~0x00020000);
2093
2094 bios_wr32(bios, 0x000200, bios_rd32(
2095 bios, 0x000200) | 0x40000000);
2096
2097 bios_wr32(bios, 0x614100, 0x00800018);
2098 bios_wr32(bios, 0x614900, 0x00800018);
2099
2100 mdelay(10);
2101
2102 bios_wr32(bios, 0x614100, 0x10000018);
2103 bios_wr32(bios, 0x614900, 0x10000018);
2104
2105 for (i = 0; i < 3; i++)
2106 bios_wr32(bios, 0x614280 + (i*0x800), bios_rd32(
2107 bios, 0x614280 + (i*0x800)) & 0xf0f0f0f0);
2108
2109 for (i = 0; i < 2; i++)
2110 bios_wr32(bios, 0x614300 + (i*0x800), bios_rd32(
2111 bios, 0x614300 + (i*0x800)) & 0xfffff0f0);
2112
2113 for (i = 0; i < 3; i++)
2114 bios_wr32(bios, 0x614380 + (i*0x800), bios_rd32(
2115 bios, 0x614380 + (i*0x800)) & 0xfffff0f0);
2116
2117 for (i = 0; i < 2; i++)
2118 bios_wr32(bios, 0x614200 + (i*0x800), bios_rd32(
2119 bios, 0x614200 + (i*0x800)) & 0xfffffff0);
2120
2121 for (i = 0; i < 2; i++)
2122 bios_wr32(bios, 0x614108 + (i*0x800), bios_rd32(
2123 bios, 0x614108 + (i*0x800)) & 0x0fffffff);
Marcin Kościelnicki37383652009-12-15 00:37:31 +00002124 return 5;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002125 }
2126
2127 bios_port_wr(bios, crtcport, (bios_port_rd(bios, crtcport) & mask) |
2128 data);
Marcin Kościelnicki37383652009-12-15 00:37:31 +00002129 return 5;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002130}
2131
Marcin Kościelnicki37383652009-12-15 00:37:31 +00002132static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10002133init_sub(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
2134{
2135 /*
2136 * INIT_SUB opcode: 0x6B ('k')
2137 *
2138 * offset (8 bit): opcode
2139 * offset + 1 (8 bit): script number
2140 *
2141 * Execute script number "script number", as a subroutine
2142 */
2143
2144 uint8_t sub = bios->data[offset + 1];
2145
2146 if (!iexec->execute)
Marcin Kościelnicki37383652009-12-15 00:37:31 +00002147 return 2;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002148
2149 BIOSLOG(bios, "0x%04X: Calling script %d\n", offset, sub);
2150
2151 parse_init_table(bios,
2152 ROM16(bios->data[bios->init_script_tbls_ptr + sub * 2]),
2153 iexec);
2154
2155 BIOSLOG(bios, "0x%04X: End of script %d\n", offset, sub);
2156
Marcin Kościelnicki37383652009-12-15 00:37:31 +00002157 return 2;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002158}
2159
Marcin Kościelnicki37383652009-12-15 00:37:31 +00002160static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10002161init_ram_condition(struct nvbios *bios, uint16_t offset,
2162 struct init_exec *iexec)
2163{
2164 /*
2165 * INIT_RAM_CONDITION opcode: 0x6D ('m')
2166 *
2167 * offset (8 bit): opcode
2168 * offset + 1 (8 bit): mask
2169 * offset + 2 (8 bit): cmpval
2170 *
2171 * Test if (NV_PFB_BOOT_0 & "mask") equals "cmpval".
2172 * If condition not met skip subsequent opcodes until condition is
2173 * inverted (INIT_NOT), or we hit INIT_RESUME
2174 */
2175
2176 uint8_t mask = bios->data[offset + 1];
2177 uint8_t cmpval = bios->data[offset + 2];
2178 uint8_t data;
2179
2180 if (!iexec->execute)
Marcin Kościelnicki37383652009-12-15 00:37:31 +00002181 return 3;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002182
2183 data = bios_rd32(bios, NV_PFB_BOOT_0) & mask;
2184
2185 BIOSLOG(bios, "0x%04X: Checking if 0x%08X equals 0x%08X\n",
2186 offset, data, cmpval);
2187
2188 if (data == cmpval)
2189 BIOSLOG(bios, "0x%04X: Condition fulfilled -- continuing to execute\n", offset);
2190 else {
2191 BIOSLOG(bios, "0x%04X: Condition not fulfilled -- skipping following commands\n", offset);
2192 iexec->execute = false;
2193 }
2194
Marcin Kościelnicki37383652009-12-15 00:37:31 +00002195 return 3;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002196}
2197
Marcin Kościelnicki37383652009-12-15 00:37:31 +00002198static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10002199init_nv_reg(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
2200{
2201 /*
2202 * INIT_NV_REG opcode: 0x6E ('n')
2203 *
2204 * offset (8 bit): opcode
2205 * offset + 1 (32 bit): register
2206 * offset + 5 (32 bit): mask
2207 * offset + 9 (32 bit): data
2208 *
2209 * Assign ((REGVAL("register") & "mask") | "data") to "register"
2210 */
2211
2212 uint32_t reg = ROM32(bios->data[offset + 1]);
2213 uint32_t mask = ROM32(bios->data[offset + 5]);
2214 uint32_t data = ROM32(bios->data[offset + 9]);
2215
2216 if (!iexec->execute)
Marcin Kościelnicki37383652009-12-15 00:37:31 +00002217 return 13;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002218
2219 BIOSLOG(bios, "0x%04X: Reg: 0x%08X, Mask: 0x%08X, Data: 0x%08X\n",
2220 offset, reg, mask, data);
2221
2222 bios_wr32(bios, reg, (bios_rd32(bios, reg) & mask) | data);
2223
Marcin Kościelnicki37383652009-12-15 00:37:31 +00002224 return 13;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002225}
2226
Marcin Kościelnicki37383652009-12-15 00:37:31 +00002227static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10002228init_macro(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
2229{
2230 /*
2231 * INIT_MACRO opcode: 0x6F ('o')
2232 *
2233 * offset (8 bit): opcode
2234 * offset + 1 (8 bit): macro number
2235 *
2236 * Look up macro index "macro number" in the macro index table.
2237 * The macro index table entry has 1 byte for the index in the macro
2238 * table, and 1 byte for the number of times to repeat the macro.
2239 * The macro table entry has 4 bytes for the register address and
2240 * 4 bytes for the value to write to that register
2241 */
2242
2243 uint8_t macro_index_tbl_idx = bios->data[offset + 1];
2244 uint16_t tmp = bios->macro_index_tbl_ptr + (macro_index_tbl_idx * MACRO_INDEX_SIZE);
2245 uint8_t macro_tbl_idx = bios->data[tmp];
2246 uint8_t count = bios->data[tmp + 1];
2247 uint32_t reg, data;
2248 int i;
2249
2250 if (!iexec->execute)
Marcin Kościelnicki37383652009-12-15 00:37:31 +00002251 return 2;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002252
2253 BIOSLOG(bios, "0x%04X: Macro: 0x%02X, MacroTableIndex: 0x%02X, "
2254 "Count: 0x%02X\n",
2255 offset, macro_index_tbl_idx, macro_tbl_idx, count);
2256
2257 for (i = 0; i < count; i++) {
2258 uint16_t macroentryptr = bios->macro_tbl_ptr + (macro_tbl_idx + i) * MACRO_SIZE;
2259
2260 reg = ROM32(bios->data[macroentryptr]);
2261 data = ROM32(bios->data[macroentryptr + 4]);
2262
2263 bios_wr32(bios, reg, data);
2264 }
2265
Marcin Kościelnicki37383652009-12-15 00:37:31 +00002266 return 2;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002267}
2268
Marcin Kościelnicki37383652009-12-15 00:37:31 +00002269static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10002270init_done(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
2271{
2272 /*
2273 * INIT_DONE opcode: 0x71 ('q')
2274 *
2275 * offset (8 bit): opcode
2276 *
2277 * End the current script
2278 */
2279
2280 /* mild retval abuse to stop parsing this table */
Marcin Kościelnicki37383652009-12-15 00:37:31 +00002281 return 0;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002282}
2283
Marcin Kościelnicki37383652009-12-15 00:37:31 +00002284static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10002285init_resume(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
2286{
2287 /*
2288 * INIT_RESUME opcode: 0x72 ('r')
2289 *
2290 * offset (8 bit): opcode
2291 *
2292 * End the current execute / no-execute condition
2293 */
2294
2295 if (iexec->execute)
Marcin Kościelnicki37383652009-12-15 00:37:31 +00002296 return 1;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002297
2298 iexec->execute = true;
2299 BIOSLOG(bios, "0x%04X: ---- Executing following commands ----\n", offset);
2300
Marcin Kościelnicki37383652009-12-15 00:37:31 +00002301 return 1;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002302}
2303
Marcin Kościelnicki37383652009-12-15 00:37:31 +00002304static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10002305init_time(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
2306{
2307 /*
2308 * INIT_TIME opcode: 0x74 ('t')
2309 *
2310 * offset (8 bit): opcode
2311 * offset + 1 (16 bit): time
2312 *
2313 * Sleep for "time" microseconds.
2314 */
2315
2316 unsigned time = ROM16(bios->data[offset + 1]);
2317
2318 if (!iexec->execute)
Marcin Kościelnicki37383652009-12-15 00:37:31 +00002319 return 3;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002320
2321 BIOSLOG(bios, "0x%04X: Sleeping for 0x%04X microseconds\n",
2322 offset, time);
2323
2324 if (time < 1000)
2325 udelay(time);
2326 else
2327 msleep((time + 900) / 1000);
2328
Marcin Kościelnicki37383652009-12-15 00:37:31 +00002329 return 3;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002330}
2331
Marcin Kościelnicki37383652009-12-15 00:37:31 +00002332static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10002333init_condition(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
2334{
2335 /*
2336 * INIT_CONDITION opcode: 0x75 ('u')
2337 *
2338 * offset (8 bit): opcode
2339 * offset + 1 (8 bit): condition number
2340 *
2341 * Check condition "condition number" in the condition table.
2342 * If condition not met skip subsequent opcodes until condition is
2343 * inverted (INIT_NOT), or we hit INIT_RESUME
2344 */
2345
2346 uint8_t cond = bios->data[offset + 1];
2347
2348 if (!iexec->execute)
Marcin Kościelnicki37383652009-12-15 00:37:31 +00002349 return 2;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002350
2351 BIOSLOG(bios, "0x%04X: Condition: 0x%02X\n", offset, cond);
2352
2353 if (bios_condition_met(bios, offset, cond))
2354 BIOSLOG(bios, "0x%04X: Condition fulfilled -- continuing to execute\n", offset);
2355 else {
2356 BIOSLOG(bios, "0x%04X: Condition not fulfilled -- skipping following commands\n", offset);
2357 iexec->execute = false;
2358 }
2359
Marcin Kościelnicki37383652009-12-15 00:37:31 +00002360 return 2;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002361}
2362
Marcin Kościelnicki37383652009-12-15 00:37:31 +00002363static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10002364init_io_condition(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
2365{
2366 /*
2367 * INIT_IO_CONDITION opcode: 0x76
2368 *
2369 * offset (8 bit): opcode
2370 * offset + 1 (8 bit): condition number
2371 *
2372 * Check condition "condition number" in the io condition table.
2373 * If condition not met skip subsequent opcodes until condition is
2374 * inverted (INIT_NOT), or we hit INIT_RESUME
2375 */
2376
2377 uint8_t cond = bios->data[offset + 1];
2378
2379 if (!iexec->execute)
Marcin Kościelnicki37383652009-12-15 00:37:31 +00002380 return 2;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002381
2382 BIOSLOG(bios, "0x%04X: IO condition: 0x%02X\n", offset, cond);
2383
2384 if (io_condition_met(bios, offset, cond))
2385 BIOSLOG(bios, "0x%04X: Condition fulfilled -- continuing to execute\n", offset);
2386 else {
2387 BIOSLOG(bios, "0x%04X: Condition not fulfilled -- skipping following commands\n", offset);
2388 iexec->execute = false;
2389 }
2390
Marcin Kościelnicki37383652009-12-15 00:37:31 +00002391 return 2;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002392}
2393
Marcin Kościelnicki37383652009-12-15 00:37:31 +00002394static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10002395init_index_io(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
2396{
2397 /*
2398 * INIT_INDEX_IO opcode: 0x78 ('x')
2399 *
2400 * offset (8 bit): opcode
2401 * offset + 1 (16 bit): CRTC port
2402 * offset + 3 (8 bit): CRTC index
2403 * offset + 4 (8 bit): mask
2404 * offset + 5 (8 bit): data
2405 *
2406 * Read value at index "CRTC index" on "CRTC port", AND with "mask",
2407 * OR with "data", write-back
2408 */
2409
2410 uint16_t crtcport = ROM16(bios->data[offset + 1]);
2411 uint8_t crtcindex = bios->data[offset + 3];
2412 uint8_t mask = bios->data[offset + 4];
2413 uint8_t data = bios->data[offset + 5];
2414 uint8_t value;
2415
2416 if (!iexec->execute)
Marcin Kościelnicki37383652009-12-15 00:37:31 +00002417 return 6;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002418
2419 BIOSLOG(bios, "0x%04X: Port: 0x%04X, Index: 0x%02X, Mask: 0x%02X, "
2420 "Data: 0x%02X\n",
2421 offset, crtcport, crtcindex, mask, data);
2422
2423 value = (bios_idxprt_rd(bios, crtcport, crtcindex) & mask) | data;
2424 bios_idxprt_wr(bios, crtcport, crtcindex, value);
2425
Marcin Kościelnicki37383652009-12-15 00:37:31 +00002426 return 6;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002427}
2428
Marcin Kościelnicki37383652009-12-15 00:37:31 +00002429static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10002430init_pll(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
2431{
2432 /*
2433 * INIT_PLL opcode: 0x79 ('y')
2434 *
2435 * offset (8 bit): opcode
2436 * offset + 1 (32 bit): register
2437 * offset + 5 (16 bit): freq
2438 *
2439 * Set PLL register "register" to coefficients for frequency (10kHz)
2440 * "freq"
2441 */
2442
2443 uint32_t reg = ROM32(bios->data[offset + 1]);
2444 uint16_t freq = ROM16(bios->data[offset + 5]);
2445
2446 if (!iexec->execute)
Marcin Kościelnicki37383652009-12-15 00:37:31 +00002447 return 7;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002448
2449 BIOSLOG(bios, "0x%04X: Reg: 0x%08X, Freq: %d0kHz\n", offset, reg, freq);
2450
2451 setPLL(bios, reg, freq * 10);
2452
Marcin Kościelnicki37383652009-12-15 00:37:31 +00002453 return 7;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002454}
2455
Marcin Kościelnicki37383652009-12-15 00:37:31 +00002456static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10002457init_zm_reg(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
2458{
2459 /*
2460 * INIT_ZM_REG opcode: 0x7A ('z')
2461 *
2462 * offset (8 bit): opcode
2463 * offset + 1 (32 bit): register
2464 * offset + 5 (32 bit): value
2465 *
2466 * Assign "value" to "register"
2467 */
2468
2469 uint32_t reg = ROM32(bios->data[offset + 1]);
2470 uint32_t value = ROM32(bios->data[offset + 5]);
2471
2472 if (!iexec->execute)
Marcin Kościelnicki37383652009-12-15 00:37:31 +00002473 return 9;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002474
2475 if (reg == 0x000200)
2476 value |= 1;
2477
2478 bios_wr32(bios, reg, value);
2479
Marcin Kościelnicki37383652009-12-15 00:37:31 +00002480 return 9;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002481}
2482
Marcin Kościelnicki37383652009-12-15 00:37:31 +00002483static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10002484init_ram_restrict_pll(struct nvbios *bios, uint16_t offset,
2485 struct init_exec *iexec)
2486{
2487 /*
2488 * INIT_RAM_RESTRICT_PLL opcode: 0x87 ('')
2489 *
2490 * offset (8 bit): opcode
2491 * offset + 1 (8 bit): PLL type
2492 * offset + 2 (32 bit): frequency 0
2493 *
2494 * Uses the RAMCFG strap of PEXTDEV_BOOT as an index into the table at
2495 * ram_restrict_table_ptr. The value read from there is used to select
2496 * a frequency from the table starting at 'frequency 0' to be
2497 * programmed into the PLL corresponding to 'type'.
2498 *
2499 * The PLL limits table on cards using this opcode has a mapping of
2500 * 'type' to the relevant registers.
2501 */
2502
2503 struct drm_device *dev = bios->dev;
2504 uint32_t strap = (bios_rd32(bios, NV_PEXTDEV_BOOT_0) & 0x0000003c) >> 2;
2505 uint8_t index = bios->data[bios->ram_restrict_tbl_ptr + strap];
2506 uint8_t type = bios->data[offset + 1];
2507 uint32_t freq = ROM32(bios->data[offset + 2 + (index * 4)]);
2508 uint8_t *pll_limits = &bios->data[bios->pll_limit_tbl_ptr], *entry;
Marcin Kościelnicki37383652009-12-15 00:37:31 +00002509 int len = 2 + bios->ram_restrict_group_count * 4;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002510 int i;
2511
2512 if (!iexec->execute)
Marcin Kościelnicki37383652009-12-15 00:37:31 +00002513 return len;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002514
2515 if (!bios->pll_limit_tbl_ptr || (pll_limits[0] & 0xf0) != 0x30) {
2516 NV_ERROR(dev, "PLL limits table not version 3.x\n");
Marcin Kościelnicki37383652009-12-15 00:37:31 +00002517 return len; /* deliberate, allow default clocks to remain */
Ben Skeggs6ee73862009-12-11 19:24:15 +10002518 }
2519
2520 entry = pll_limits + pll_limits[1];
2521 for (i = 0; i < pll_limits[3]; i++, entry += pll_limits[2]) {
2522 if (entry[0] == type) {
2523 uint32_t reg = ROM32(entry[3]);
2524
2525 BIOSLOG(bios, "0x%04X: "
2526 "Type %02x Reg 0x%08x Freq %dKHz\n",
2527 offset, type, reg, freq);
2528
2529 setPLL(bios, reg, freq);
Marcin Kościelnicki37383652009-12-15 00:37:31 +00002530 return len;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002531 }
2532 }
2533
2534 NV_ERROR(dev, "PLL type 0x%02x not found in PLL limits table", type);
Marcin Kościelnicki37383652009-12-15 00:37:31 +00002535 return len;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002536}
2537
Marcin Kościelnicki37383652009-12-15 00:37:31 +00002538static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10002539init_8c(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
2540{
2541 /*
2542 * INIT_8C opcode: 0x8C ('')
2543 *
2544 * NOP so far....
2545 *
2546 */
2547
Marcin Kościelnicki37383652009-12-15 00:37:31 +00002548 return 1;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002549}
2550
Marcin Kościelnicki37383652009-12-15 00:37:31 +00002551static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10002552init_8d(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
2553{
2554 /*
2555 * INIT_8D opcode: 0x8D ('')
2556 *
2557 * NOP so far....
2558 *
2559 */
2560
Marcin Kościelnicki37383652009-12-15 00:37:31 +00002561 return 1;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002562}
2563
Marcin Kościelnicki37383652009-12-15 00:37:31 +00002564static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10002565init_gpio(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
2566{
2567 /*
2568 * INIT_GPIO opcode: 0x8E ('')
2569 *
2570 * offset (8 bit): opcode
2571 *
2572 * Loop over all entries in the DCB GPIO table, and initialise
2573 * each GPIO according to various values listed in each entry
2574 */
2575
Ben Skeggs2535d712010-04-07 12:00:14 +10002576 struct drm_nouveau_private *dev_priv = bios->dev->dev_private;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002577 const uint32_t nv50_gpio_reg[4] = { 0xe104, 0xe108, 0xe280, 0xe284 };
2578 const uint32_t nv50_gpio_ctl[2] = { 0xe100, 0xe28c };
Ben Skeggs6ee73862009-12-11 19:24:15 +10002579 int i;
2580
Ben Skeggs2535d712010-04-07 12:00:14 +10002581 if (dev_priv->card_type != NV_50) {
2582 NV_ERROR(bios->dev, "INIT_GPIO on unsupported chipset\n");
2583 return -ENODEV;
2584 }
2585
Marcin Kościelnicki37383652009-12-15 00:37:31 +00002586 if (!iexec->execute)
2587 return 1;
2588
Ben Skeggs2535d712010-04-07 12:00:14 +10002589 for (i = 0; i < bios->dcb.gpio.entries; i++) {
2590 struct dcb_gpio_entry *gpio = &bios->dcb.gpio.entry[i];
2591 uint32_t r, s, v;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002592
Ben Skeggs2535d712010-04-07 12:00:14 +10002593 BIOSLOG(bios, "0x%04X: Entry: 0x%08X\n", offset, gpio->entry);
Ben Skeggs6ee73862009-12-11 19:24:15 +10002594
Ben Skeggs2535d712010-04-07 12:00:14 +10002595 r = nv50_gpio_reg[gpio->line >> 3];
2596 s = (gpio->line & 0x07) << 2;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002597 v = bios_rd32(bios, r) & ~(0x00000003 << s);
Ben Skeggs2535d712010-04-07 12:00:14 +10002598 if (gpio->entry & 0x01000000)
2599 v |= (((gpio->entry & 0x60000000) >> 29) ^ 2) << s;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002600 else
Ben Skeggs2535d712010-04-07 12:00:14 +10002601 v |= (((gpio->entry & 0x18000000) >> 27) ^ 2) << s;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002602 bios_wr32(bios, r, v);
2603
Ben Skeggs2535d712010-04-07 12:00:14 +10002604 r = nv50_gpio_ctl[gpio->line >> 4];
2605 s = (gpio->line & 0x0f);
Ben Skeggs6ee73862009-12-11 19:24:15 +10002606 v = bios_rd32(bios, r) & ~(0x00010001 << s);
Ben Skeggs2535d712010-04-07 12:00:14 +10002607 switch ((gpio->entry & 0x06000000) >> 25) {
Ben Skeggs6ee73862009-12-11 19:24:15 +10002608 case 1:
2609 v |= (0x00000001 << s);
2610 break;
2611 case 2:
2612 v |= (0x00010000 << s);
2613 break;
2614 default:
2615 break;
2616 }
2617 bios_wr32(bios, r, v);
2618 }
2619
Marcin Kościelnicki37383652009-12-15 00:37:31 +00002620 return 1;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002621}
2622
Marcin Kościelnicki37383652009-12-15 00:37:31 +00002623static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10002624init_ram_restrict_zm_reg_group(struct nvbios *bios, uint16_t offset,
2625 struct init_exec *iexec)
2626{
2627 /*
2628 * INIT_RAM_RESTRICT_ZM_REG_GROUP opcode: 0x8F ('')
2629 *
2630 * offset (8 bit): opcode
2631 * offset + 1 (32 bit): reg
2632 * offset + 5 (8 bit): regincrement
2633 * offset + 6 (8 bit): count
2634 * offset + 7 (32 bit): value 1,1
2635 * ...
2636 *
2637 * Use the RAMCFG strap of PEXTDEV_BOOT as an index into the table at
2638 * ram_restrict_table_ptr. The value read from here is 'n', and
2639 * "value 1,n" gets written to "reg". This repeats "count" times and on
2640 * each iteration 'm', "reg" increases by "regincrement" and
2641 * "value m,n" is used. The extent of n is limited by a number read
2642 * from the 'M' BIT table, herein called "blocklen"
2643 */
2644
2645 uint32_t reg = ROM32(bios->data[offset + 1]);
2646 uint8_t regincrement = bios->data[offset + 5];
2647 uint8_t count = bios->data[offset + 6];
2648 uint32_t strap_ramcfg, data;
Marcin Kościelnicki37383652009-12-15 00:37:31 +00002649 /* previously set by 'M' BIT table */
2650 uint16_t blocklen = bios->ram_restrict_group_count * 4;
2651 int len = 7 + count * blocklen;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002652 uint8_t index;
2653 int i;
2654
Ben Skeggs6ee73862009-12-11 19:24:15 +10002655
2656 if (!iexec->execute)
Marcin Kościelnicki37383652009-12-15 00:37:31 +00002657 return len;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002658
2659 if (!blocklen) {
2660 NV_ERROR(bios->dev,
2661 "0x%04X: Zero block length - has the M table "
2662 "been parsed?\n", offset);
Marcin Kościelnicki37383652009-12-15 00:37:31 +00002663 return 0;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002664 }
2665
2666 strap_ramcfg = (bios_rd32(bios, NV_PEXTDEV_BOOT_0) >> 2) & 0xf;
2667 index = bios->data[bios->ram_restrict_tbl_ptr + strap_ramcfg];
2668
2669 BIOSLOG(bios, "0x%04X: Reg: 0x%08X, RegIncrement: 0x%02X, "
2670 "Count: 0x%02X, StrapRamCfg: 0x%02X, Index: 0x%02X\n",
2671 offset, reg, regincrement, count, strap_ramcfg, index);
2672
2673 for (i = 0; i < count; i++) {
2674 data = ROM32(bios->data[offset + 7 + index * 4 + blocklen * i]);
2675
2676 bios_wr32(bios, reg, data);
2677
2678 reg += regincrement;
2679 }
2680
Marcin Kościelnicki37383652009-12-15 00:37:31 +00002681 return len;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002682}
2683
Marcin Kościelnicki37383652009-12-15 00:37:31 +00002684static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10002685init_copy_zm_reg(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
2686{
2687 /*
2688 * INIT_COPY_ZM_REG opcode: 0x90 ('')
2689 *
2690 * offset (8 bit): opcode
2691 * offset + 1 (32 bit): src reg
2692 * offset + 5 (32 bit): dst reg
2693 *
2694 * Put contents of "src reg" into "dst reg"
2695 */
2696
2697 uint32_t srcreg = ROM32(bios->data[offset + 1]);
2698 uint32_t dstreg = ROM32(bios->data[offset + 5]);
2699
2700 if (!iexec->execute)
Marcin Kościelnicki37383652009-12-15 00:37:31 +00002701 return 9;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002702
2703 bios_wr32(bios, dstreg, bios_rd32(bios, srcreg));
2704
Marcin Kościelnicki37383652009-12-15 00:37:31 +00002705 return 9;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002706}
2707
Marcin Kościelnicki37383652009-12-15 00:37:31 +00002708static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10002709init_zm_reg_group_addr_latched(struct nvbios *bios, uint16_t offset,
2710 struct init_exec *iexec)
2711{
2712 /*
2713 * INIT_ZM_REG_GROUP_ADDRESS_LATCHED opcode: 0x91 ('')
2714 *
2715 * offset (8 bit): opcode
2716 * offset + 1 (32 bit): dst reg
2717 * offset + 5 (8 bit): count
2718 * offset + 6 (32 bit): data 1
2719 * ...
2720 *
2721 * For each of "count" values write "data n" to "dst reg"
2722 */
2723
2724 uint32_t reg = ROM32(bios->data[offset + 1]);
2725 uint8_t count = bios->data[offset + 5];
Marcin Kościelnicki37383652009-12-15 00:37:31 +00002726 int len = 6 + count * 4;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002727 int i;
2728
2729 if (!iexec->execute)
Marcin Kościelnicki37383652009-12-15 00:37:31 +00002730 return len;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002731
2732 for (i = 0; i < count; i++) {
2733 uint32_t data = ROM32(bios->data[offset + 6 + 4 * i]);
2734 bios_wr32(bios, reg, data);
2735 }
2736
Marcin Kościelnicki37383652009-12-15 00:37:31 +00002737 return len;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002738}
2739
Marcin Kościelnicki37383652009-12-15 00:37:31 +00002740static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10002741init_reserved(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
2742{
2743 /*
2744 * INIT_RESERVED opcode: 0x92 ('')
2745 *
2746 * offset (8 bit): opcode
2747 *
2748 * Seemingly does nothing
2749 */
2750
Marcin Kościelnicki37383652009-12-15 00:37:31 +00002751 return 1;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002752}
2753
Marcin Kościelnicki37383652009-12-15 00:37:31 +00002754static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10002755init_96(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
2756{
2757 /*
2758 * INIT_96 opcode: 0x96 ('')
2759 *
2760 * offset (8 bit): opcode
2761 * offset + 1 (32 bit): sreg
2762 * offset + 5 (8 bit): sshift
2763 * offset + 6 (8 bit): smask
2764 * offset + 7 (8 bit): index
2765 * offset + 8 (32 bit): reg
2766 * offset + 12 (32 bit): mask
2767 * offset + 16 (8 bit): shift
2768 *
2769 */
2770
2771 uint16_t xlatptr = bios->init96_tbl_ptr + (bios->data[offset + 7] * 2);
2772 uint32_t reg = ROM32(bios->data[offset + 8]);
2773 uint32_t mask = ROM32(bios->data[offset + 12]);
2774 uint32_t val;
2775
2776 val = bios_rd32(bios, ROM32(bios->data[offset + 1]));
2777 if (bios->data[offset + 5] < 0x80)
2778 val >>= bios->data[offset + 5];
2779 else
2780 val <<= (0x100 - bios->data[offset + 5]);
2781 val &= bios->data[offset + 6];
2782
2783 val = bios->data[ROM16(bios->data[xlatptr]) + val];
2784 val <<= bios->data[offset + 16];
2785
2786 if (!iexec->execute)
Marcin Kościelnicki37383652009-12-15 00:37:31 +00002787 return 17;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002788
2789 bios_wr32(bios, reg, (bios_rd32(bios, reg) & mask) | val);
Marcin Kościelnicki37383652009-12-15 00:37:31 +00002790 return 17;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002791}
2792
Marcin Kościelnicki37383652009-12-15 00:37:31 +00002793static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10002794init_97(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
2795{
2796 /*
2797 * INIT_97 opcode: 0x97 ('')
2798 *
2799 * offset (8 bit): opcode
2800 * offset + 1 (32 bit): register
2801 * offset + 5 (32 bit): mask
2802 * offset + 9 (32 bit): value
2803 *
2804 * Adds "value" to "register" preserving the fields specified
2805 * by "mask"
2806 */
2807
2808 uint32_t reg = ROM32(bios->data[offset + 1]);
2809 uint32_t mask = ROM32(bios->data[offset + 5]);
2810 uint32_t add = ROM32(bios->data[offset + 9]);
2811 uint32_t val;
2812
2813 val = bios_rd32(bios, reg);
2814 val = (val & mask) | ((val + add) & ~mask);
2815
2816 if (!iexec->execute)
Marcin Kościelnicki37383652009-12-15 00:37:31 +00002817 return 13;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002818
2819 bios_wr32(bios, reg, val);
Marcin Kościelnicki37383652009-12-15 00:37:31 +00002820 return 13;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002821}
2822
Marcin Kościelnicki37383652009-12-15 00:37:31 +00002823static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10002824init_auxch(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
2825{
2826 /*
2827 * INIT_AUXCH opcode: 0x98 ('')
2828 *
2829 * offset (8 bit): opcode
2830 * offset + 1 (32 bit): address
2831 * offset + 5 (8 bit): count
2832 * offset + 6 (8 bit): mask 0
2833 * offset + 7 (8 bit): data 0
2834 * ...
2835 *
2836 */
2837
2838 struct drm_device *dev = bios->dev;
2839 struct nouveau_i2c_chan *auxch;
2840 uint32_t addr = ROM32(bios->data[offset + 1]);
Marcin Kościelnicki37383652009-12-15 00:37:31 +00002841 uint8_t count = bios->data[offset + 5];
2842 int len = 6 + count * 2;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002843 int ret, i;
2844
2845 if (!bios->display.output) {
2846 NV_ERROR(dev, "INIT_AUXCH: no active output\n");
Marcin Kościelnicki37383652009-12-15 00:37:31 +00002847 return 0;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002848 }
2849
2850 auxch = init_i2c_device_find(dev, bios->display.output->i2c_index);
2851 if (!auxch) {
2852 NV_ERROR(dev, "INIT_AUXCH: couldn't get auxch %d\n",
2853 bios->display.output->i2c_index);
Marcin Kościelnicki37383652009-12-15 00:37:31 +00002854 return 0;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002855 }
2856
2857 if (!iexec->execute)
Marcin Kościelnicki37383652009-12-15 00:37:31 +00002858 return len;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002859
2860 offset += 6;
Marcin Kościelnicki37383652009-12-15 00:37:31 +00002861 for (i = 0; i < count; i++, offset += 2) {
Ben Skeggs6ee73862009-12-11 19:24:15 +10002862 uint8_t data;
2863
2864 ret = nouveau_dp_auxch(auxch, 9, addr, &data, 1);
2865 if (ret) {
2866 NV_ERROR(dev, "INIT_AUXCH: rd auxch fail %d\n", ret);
Marcin Kościelnicki37383652009-12-15 00:37:31 +00002867 return 0;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002868 }
2869
2870 data &= bios->data[offset + 0];
2871 data |= bios->data[offset + 1];
2872
2873 ret = nouveau_dp_auxch(auxch, 8, addr, &data, 1);
2874 if (ret) {
2875 NV_ERROR(dev, "INIT_AUXCH: wr auxch fail %d\n", ret);
Marcin Kościelnicki37383652009-12-15 00:37:31 +00002876 return 0;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002877 }
2878 }
2879
Marcin Kościelnicki37383652009-12-15 00:37:31 +00002880 return len;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002881}
2882
Marcin Kościelnicki37383652009-12-15 00:37:31 +00002883static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10002884init_zm_auxch(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
2885{
2886 /*
2887 * INIT_ZM_AUXCH opcode: 0x99 ('')
2888 *
2889 * offset (8 bit): opcode
2890 * offset + 1 (32 bit): address
2891 * offset + 5 (8 bit): count
2892 * offset + 6 (8 bit): data 0
2893 * ...
2894 *
2895 */
2896
2897 struct drm_device *dev = bios->dev;
2898 struct nouveau_i2c_chan *auxch;
2899 uint32_t addr = ROM32(bios->data[offset + 1]);
Marcin Kościelnicki37383652009-12-15 00:37:31 +00002900 uint8_t count = bios->data[offset + 5];
2901 int len = 6 + count;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002902 int ret, i;
2903
2904 if (!bios->display.output) {
2905 NV_ERROR(dev, "INIT_ZM_AUXCH: no active output\n");
Marcin Kościelnicki37383652009-12-15 00:37:31 +00002906 return 0;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002907 }
2908
2909 auxch = init_i2c_device_find(dev, bios->display.output->i2c_index);
2910 if (!auxch) {
2911 NV_ERROR(dev, "INIT_ZM_AUXCH: couldn't get auxch %d\n",
2912 bios->display.output->i2c_index);
Marcin Kościelnicki37383652009-12-15 00:37:31 +00002913 return 0;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002914 }
2915
2916 if (!iexec->execute)
Marcin Kościelnicki37383652009-12-15 00:37:31 +00002917 return len;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002918
2919 offset += 6;
Marcin Kościelnicki37383652009-12-15 00:37:31 +00002920 for (i = 0; i < count; i++, offset++) {
Ben Skeggs6ee73862009-12-11 19:24:15 +10002921 ret = nouveau_dp_auxch(auxch, 8, addr, &bios->data[offset], 1);
2922 if (ret) {
2923 NV_ERROR(dev, "INIT_ZM_AUXCH: wr auxch fail %d\n", ret);
Marcin Kościelnicki37383652009-12-15 00:37:31 +00002924 return 0;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002925 }
2926 }
2927
Marcin Kościelnicki37383652009-12-15 00:37:31 +00002928 return len;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002929}
2930
2931static struct init_tbl_entry itbl_entry[] = {
2932 /* command name , id , length , offset , mult , command handler */
2933 /* INIT_PROG (0x31, 15, 10, 4) removed due to no example of use */
Marcin Kościelnicki37383652009-12-15 00:37:31 +00002934 { "INIT_IO_RESTRICT_PROG" , 0x32, init_io_restrict_prog },
2935 { "INIT_REPEAT" , 0x33, init_repeat },
2936 { "INIT_IO_RESTRICT_PLL" , 0x34, init_io_restrict_pll },
2937 { "INIT_END_REPEAT" , 0x36, init_end_repeat },
2938 { "INIT_COPY" , 0x37, init_copy },
2939 { "INIT_NOT" , 0x38, init_not },
2940 { "INIT_IO_FLAG_CONDITION" , 0x39, init_io_flag_condition },
2941 { "INIT_INDEX_ADDRESS_LATCHED" , 0x49, init_idx_addr_latched },
2942 { "INIT_IO_RESTRICT_PLL2" , 0x4A, init_io_restrict_pll2 },
2943 { "INIT_PLL2" , 0x4B, init_pll2 },
2944 { "INIT_I2C_BYTE" , 0x4C, init_i2c_byte },
2945 { "INIT_ZM_I2C_BYTE" , 0x4D, init_zm_i2c_byte },
2946 { "INIT_ZM_I2C" , 0x4E, init_zm_i2c },
2947 { "INIT_TMDS" , 0x4F, init_tmds },
2948 { "INIT_ZM_TMDS_GROUP" , 0x50, init_zm_tmds_group },
2949 { "INIT_CR_INDEX_ADDRESS_LATCHED" , 0x51, init_cr_idx_adr_latch },
2950 { "INIT_CR" , 0x52, init_cr },
2951 { "INIT_ZM_CR" , 0x53, init_zm_cr },
2952 { "INIT_ZM_CR_GROUP" , 0x54, init_zm_cr_group },
2953 { "INIT_CONDITION_TIME" , 0x56, init_condition_time },
2954 { "INIT_ZM_REG_SEQUENCE" , 0x58, init_zm_reg_sequence },
Ben Skeggs6ee73862009-12-11 19:24:15 +10002955 /* INIT_INDIRECT_REG (0x5A, 7, 0, 0) removed due to no example of use */
Marcin Kościelnicki37383652009-12-15 00:37:31 +00002956 { "INIT_SUB_DIRECT" , 0x5B, init_sub_direct },
2957 { "INIT_COPY_NV_REG" , 0x5F, init_copy_nv_reg },
2958 { "INIT_ZM_INDEX_IO" , 0x62, init_zm_index_io },
2959 { "INIT_COMPUTE_MEM" , 0x63, init_compute_mem },
2960 { "INIT_RESET" , 0x65, init_reset },
2961 { "INIT_CONFIGURE_MEM" , 0x66, init_configure_mem },
2962 { "INIT_CONFIGURE_CLK" , 0x67, init_configure_clk },
2963 { "INIT_CONFIGURE_PREINIT" , 0x68, init_configure_preinit },
2964 { "INIT_IO" , 0x69, init_io },
2965 { "INIT_SUB" , 0x6B, init_sub },
2966 { "INIT_RAM_CONDITION" , 0x6D, init_ram_condition },
2967 { "INIT_NV_REG" , 0x6E, init_nv_reg },
2968 { "INIT_MACRO" , 0x6F, init_macro },
2969 { "INIT_DONE" , 0x71, init_done },
2970 { "INIT_RESUME" , 0x72, init_resume },
Ben Skeggs6ee73862009-12-11 19:24:15 +10002971 /* INIT_RAM_CONDITION2 (0x73, 9, 0, 0) removed due to no example of use */
Marcin Kościelnicki37383652009-12-15 00:37:31 +00002972 { "INIT_TIME" , 0x74, init_time },
2973 { "INIT_CONDITION" , 0x75, init_condition },
2974 { "INIT_IO_CONDITION" , 0x76, init_io_condition },
2975 { "INIT_INDEX_IO" , 0x78, init_index_io },
2976 { "INIT_PLL" , 0x79, init_pll },
2977 { "INIT_ZM_REG" , 0x7A, init_zm_reg },
2978 { "INIT_RAM_RESTRICT_PLL" , 0x87, init_ram_restrict_pll },
2979 { "INIT_8C" , 0x8C, init_8c },
2980 { "INIT_8D" , 0x8D, init_8d },
2981 { "INIT_GPIO" , 0x8E, init_gpio },
2982 { "INIT_RAM_RESTRICT_ZM_REG_GROUP" , 0x8F, init_ram_restrict_zm_reg_group },
2983 { "INIT_COPY_ZM_REG" , 0x90, init_copy_zm_reg },
2984 { "INIT_ZM_REG_GROUP_ADDRESS_LATCHED" , 0x91, init_zm_reg_group_addr_latched },
2985 { "INIT_RESERVED" , 0x92, init_reserved },
2986 { "INIT_96" , 0x96, init_96 },
2987 { "INIT_97" , 0x97, init_97 },
2988 { "INIT_AUXCH" , 0x98, init_auxch },
2989 { "INIT_ZM_AUXCH" , 0x99, init_zm_auxch },
2990 { NULL , 0 , NULL }
Ben Skeggs6ee73862009-12-11 19:24:15 +10002991};
2992
Ben Skeggs6ee73862009-12-11 19:24:15 +10002993#define MAX_TABLE_OPS 1000
2994
2995static int
2996parse_init_table(struct nvbios *bios, unsigned int offset,
2997 struct init_exec *iexec)
2998{
2999 /*
3000 * Parses all commands in an init table.
3001 *
3002 * We start out executing all commands found in the init table. Some
3003 * opcodes may change the status of iexec->execute to SKIP, which will
3004 * cause the following opcodes to perform no operation until the value
3005 * is changed back to EXECUTE.
3006 */
3007
Marcin Kościelnicki37383652009-12-15 00:37:31 +00003008 int count = 0, i, res;
Ben Skeggs6ee73862009-12-11 19:24:15 +10003009 uint8_t id;
3010
3011 /*
3012 * Loop until INIT_DONE causes us to break out of the loop
3013 * (or until offset > bios length just in case... )
3014 * (and no more than MAX_TABLE_OPS iterations, just in case... )
3015 */
3016 while ((offset < bios->length) && (count++ < MAX_TABLE_OPS)) {
3017 id = bios->data[offset];
3018
3019 /* Find matching id in itbl_entry */
3020 for (i = 0; itbl_entry[i].name && (itbl_entry[i].id != id); i++)
3021 ;
3022
3023 if (itbl_entry[i].name) {
3024 BIOSLOG(bios, "0x%04X: [ (0x%02X) - %s ]\n",
3025 offset, itbl_entry[i].id, itbl_entry[i].name);
3026
3027 /* execute eventual command handler */
Marcin Kościelnicki37383652009-12-15 00:37:31 +00003028 res = (*itbl_entry[i].handler)(bios, offset, iexec);
3029 if (!res)
3030 break;
3031 /*
3032 * Add the offset of the current command including all data
3033 * of that command. The offset will then be pointing on the
3034 * next op code.
3035 */
3036 offset += res;
Ben Skeggs6ee73862009-12-11 19:24:15 +10003037 } else {
3038 NV_ERROR(bios->dev,
3039 "0x%04X: Init table command not found: "
3040 "0x%02X\n", offset, id);
3041 return -ENOENT;
3042 }
Ben Skeggs6ee73862009-12-11 19:24:15 +10003043 }
3044
3045 if (offset >= bios->length)
3046 NV_WARN(bios->dev,
3047 "Offset 0x%04X greater than known bios image length. "
3048 "Corrupt image?\n", offset);
3049 if (count >= MAX_TABLE_OPS)
3050 NV_WARN(bios->dev,
3051 "More than %d opcodes to a table is unlikely, "
3052 "is the bios image corrupt?\n", MAX_TABLE_OPS);
3053
3054 return 0;
3055}
3056
3057static void
3058parse_init_tables(struct nvbios *bios)
3059{
3060 /* Loops and calls parse_init_table() for each present table. */
3061
3062 int i = 0;
3063 uint16_t table;
3064 struct init_exec iexec = {true, false};
3065
3066 if (bios->old_style_init) {
3067 if (bios->init_script_tbls_ptr)
3068 parse_init_table(bios, bios->init_script_tbls_ptr, &iexec);
3069 if (bios->extra_init_script_tbl_ptr)
3070 parse_init_table(bios, bios->extra_init_script_tbl_ptr, &iexec);
3071
3072 return;
3073 }
3074
3075 while ((table = ROM16(bios->data[bios->init_script_tbls_ptr + i]))) {
3076 NV_INFO(bios->dev,
3077 "Parsing VBIOS init table %d at offset 0x%04X\n",
3078 i / 2, table);
3079 BIOSLOG(bios, "0x%04X: ------ Executing following commands ------\n", table);
3080
3081 parse_init_table(bios, table, &iexec);
3082 i += 2;
3083 }
3084}
3085
3086static uint16_t clkcmptable(struct nvbios *bios, uint16_t clktable, int pxclk)
3087{
3088 int compare_record_len, i = 0;
3089 uint16_t compareclk, scriptptr = 0;
3090
3091 if (bios->major_version < 5) /* pre BIT */
3092 compare_record_len = 3;
3093 else
3094 compare_record_len = 4;
3095
3096 do {
3097 compareclk = ROM16(bios->data[clktable + compare_record_len * i]);
3098 if (pxclk >= compareclk * 10) {
3099 if (bios->major_version < 5) {
3100 uint8_t tmdssub = bios->data[clktable + 2 + compare_record_len * i];
3101 scriptptr = ROM16(bios->data[bios->init_script_tbls_ptr + tmdssub * 2]);
3102 } else
3103 scriptptr = ROM16(bios->data[clktable + 2 + compare_record_len * i]);
3104 break;
3105 }
3106 i++;
3107 } while (compareclk);
3108
3109 return scriptptr;
3110}
3111
3112static void
3113run_digital_op_script(struct drm_device *dev, uint16_t scriptptr,
3114 struct dcb_entry *dcbent, int head, bool dl)
3115{
3116 struct drm_nouveau_private *dev_priv = dev->dev_private;
Ben Skeggs04a39c52010-02-24 10:03:05 +10003117 struct nvbios *bios = &dev_priv->vbios;
Ben Skeggs6ee73862009-12-11 19:24:15 +10003118 struct init_exec iexec = {true, false};
3119
3120 NV_TRACE(dev, "0x%04X: Parsing digital output script table\n",
3121 scriptptr);
3122 bios_idxprt_wr(bios, NV_CIO_CRX__COLOR, NV_CIO_CRE_44,
3123 head ? NV_CIO_CRE_44_HEADB : NV_CIO_CRE_44_HEADA);
3124 /* note: if dcb entries have been merged, index may be misleading */
3125 NVWriteVgaCrtc5758(dev, head, 0, dcbent->index);
3126 parse_init_table(bios, scriptptr, &iexec);
3127
3128 nv04_dfp_bind_head(dev, dcbent, head, dl);
3129}
3130
3131static int call_lvds_manufacturer_script(struct drm_device *dev, struct dcb_entry *dcbent, int head, enum LVDS_script script)
3132{
3133 struct drm_nouveau_private *dev_priv = dev->dev_private;
Ben Skeggs04a39c52010-02-24 10:03:05 +10003134 struct nvbios *bios = &dev_priv->vbios;
Ben Skeggs6ee73862009-12-11 19:24:15 +10003135 uint8_t sub = bios->data[bios->fp.xlated_entry + script] + (bios->fp.link_c_increment && dcbent->or & OUTPUT_C ? 1 : 0);
3136 uint16_t scriptofs = ROM16(bios->data[bios->init_script_tbls_ptr + sub * 2]);
3137
3138 if (!bios->fp.xlated_entry || !sub || !scriptofs)
3139 return -EINVAL;
3140
3141 run_digital_op_script(dev, scriptofs, dcbent, head, bios->fp.dual_link);
3142
3143 if (script == LVDS_PANEL_OFF) {
3144 /* off-on delay in ms */
3145 msleep(ROM16(bios->data[bios->fp.xlated_entry + 7]));
3146 }
3147#ifdef __powerpc__
3148 /* Powerbook specific quirks */
Francisco Jerez3d9aefb2010-01-14 14:56:54 +01003149 if ((dev->pci_device & 0xffff) == 0x0179 ||
3150 (dev->pci_device & 0xffff) == 0x0189 ||
3151 (dev->pci_device & 0xffff) == 0x0329) {
3152 if (script == LVDS_RESET) {
3153 nv_write_tmds(dev, dcbent->or, 0, 0x02, 0x72);
3154
3155 } else if (script == LVDS_PANEL_ON) {
3156 bios_wr32(bios, NV_PBUS_DEBUG_DUALHEAD_CTL,
3157 bios_rd32(bios, NV_PBUS_DEBUG_DUALHEAD_CTL)
3158 | (1 << 31));
3159 bios_wr32(bios, NV_PCRTC_GPIO_EXT,
3160 bios_rd32(bios, NV_PCRTC_GPIO_EXT) | 1);
3161
3162 } else if (script == LVDS_PANEL_OFF) {
3163 bios_wr32(bios, NV_PBUS_DEBUG_DUALHEAD_CTL,
3164 bios_rd32(bios, NV_PBUS_DEBUG_DUALHEAD_CTL)
3165 & ~(1 << 31));
3166 bios_wr32(bios, NV_PCRTC_GPIO_EXT,
3167 bios_rd32(bios, NV_PCRTC_GPIO_EXT) & ~3);
Ben Skeggs6ee73862009-12-11 19:24:15 +10003168 }
3169 }
3170#endif
3171
3172 return 0;
3173}
3174
3175static int run_lvds_table(struct drm_device *dev, struct dcb_entry *dcbent, int head, enum LVDS_script script, int pxclk)
3176{
3177 /*
3178 * The BIT LVDS table's header has the information to setup the
3179 * necessary registers. Following the standard 4 byte header are:
3180 * A bitmask byte and a dual-link transition pxclk value for use in
3181 * selecting the init script when not using straps; 4 script pointers
3182 * for panel power, selected by output and on/off; and 8 table pointers
3183 * for panel init, the needed one determined by output, and bits in the
3184 * conf byte. These tables are similar to the TMDS tables, consisting
3185 * of a list of pxclks and script pointers.
3186 */
3187 struct drm_nouveau_private *dev_priv = dev->dev_private;
Ben Skeggs04a39c52010-02-24 10:03:05 +10003188 struct nvbios *bios = &dev_priv->vbios;
Ben Skeggs6ee73862009-12-11 19:24:15 +10003189 unsigned int outputset = (dcbent->or == 4) ? 1 : 0;
3190 uint16_t scriptptr = 0, clktable;
Ben Skeggs6ee73862009-12-11 19:24:15 +10003191
3192 /*
3193 * For now we assume version 3.0 table - g80 support will need some
3194 * changes
3195 */
3196
3197 switch (script) {
3198 case LVDS_INIT:
3199 return -ENOSYS;
3200 case LVDS_BACKLIGHT_ON:
3201 case LVDS_PANEL_ON:
3202 scriptptr = ROM16(bios->data[bios->fp.lvdsmanufacturerpointer + 7 + outputset * 2]);
3203 break;
3204 case LVDS_BACKLIGHT_OFF:
3205 case LVDS_PANEL_OFF:
3206 scriptptr = ROM16(bios->data[bios->fp.lvdsmanufacturerpointer + 11 + outputset * 2]);
3207 break;
3208 case LVDS_RESET:
Ben Skeggsf3bbb9c2010-03-18 12:05:43 +10003209 clktable = bios->fp.lvdsmanufacturerpointer + 15;
3210 if (dcbent->or == 4)
3211 clktable += 8;
3212
Ben Skeggs6ee73862009-12-11 19:24:15 +10003213 if (dcbent->lvdsconf.use_straps_for_mode) {
3214 if (bios->fp.dual_link)
Ben Skeggsf3bbb9c2010-03-18 12:05:43 +10003215 clktable += 4;
3216 if (bios->fp.if_is_24bit)
3217 clktable += 2;
Ben Skeggs6ee73862009-12-11 19:24:15 +10003218 } else {
3219 /* using EDID */
Ben Skeggsf3bbb9c2010-03-18 12:05:43 +10003220 int cmpval_24bit = (dcbent->or == 4) ? 4 : 1;
Ben Skeggs6ee73862009-12-11 19:24:15 +10003221
3222 if (bios->fp.dual_link) {
Ben Skeggsf3bbb9c2010-03-18 12:05:43 +10003223 clktable += 4;
3224 cmpval_24bit <<= 1;
Ben Skeggs6ee73862009-12-11 19:24:15 +10003225 }
Ben Skeggsf3bbb9c2010-03-18 12:05:43 +10003226
3227 if (bios->fp.strapless_is_24bit & cmpval_24bit)
3228 clktable += 2;
Ben Skeggs6ee73862009-12-11 19:24:15 +10003229 }
3230
Ben Skeggsf3bbb9c2010-03-18 12:05:43 +10003231 clktable = ROM16(bios->data[clktable]);
Ben Skeggs6ee73862009-12-11 19:24:15 +10003232 if (!clktable) {
3233 NV_ERROR(dev, "Pixel clock comparison table not found\n");
3234 return -ENOENT;
3235 }
3236 scriptptr = clkcmptable(bios, clktable, pxclk);
3237 }
3238
3239 if (!scriptptr) {
3240 NV_ERROR(dev, "LVDS output init script not found\n");
3241 return -ENOENT;
3242 }
3243 run_digital_op_script(dev, scriptptr, dcbent, head, bios->fp.dual_link);
3244
3245 return 0;
3246}
3247
3248int call_lvds_script(struct drm_device *dev, struct dcb_entry *dcbent, int head, enum LVDS_script script, int pxclk)
3249{
3250 /*
3251 * LVDS operations are multiplexed in an effort to present a single API
3252 * which works with two vastly differing underlying structures.
3253 * This acts as the demux
3254 */
3255
3256 struct drm_nouveau_private *dev_priv = dev->dev_private;
Ben Skeggs04a39c52010-02-24 10:03:05 +10003257 struct nvbios *bios = &dev_priv->vbios;
Ben Skeggs6ee73862009-12-11 19:24:15 +10003258 uint8_t lvds_ver = bios->data[bios->fp.lvdsmanufacturerpointer];
3259 uint32_t sel_clk_binding, sel_clk;
3260 int ret;
3261
3262 if (bios->fp.last_script_invoc == (script << 1 | head) || !lvds_ver ||
3263 (lvds_ver >= 0x30 && script == LVDS_INIT))
3264 return 0;
3265
3266 if (!bios->fp.lvds_init_run) {
3267 bios->fp.lvds_init_run = true;
3268 call_lvds_script(dev, dcbent, head, LVDS_INIT, pxclk);
3269 }
3270
3271 if (script == LVDS_PANEL_ON && bios->fp.reset_after_pclk_change)
3272 call_lvds_script(dev, dcbent, head, LVDS_RESET, pxclk);
3273 if (script == LVDS_RESET && bios->fp.power_off_for_reset)
3274 call_lvds_script(dev, dcbent, head, LVDS_PANEL_OFF, pxclk);
3275
3276 NV_TRACE(dev, "Calling LVDS script %d:\n", script);
3277
3278 /* don't let script change pll->head binding */
3279 sel_clk_binding = bios_rd32(bios, NV_PRAMDAC_SEL_CLK) & 0x50000;
3280
3281 if (lvds_ver < 0x30)
3282 ret = call_lvds_manufacturer_script(dev, dcbent, head, script);
3283 else
3284 ret = run_lvds_table(dev, dcbent, head, script, pxclk);
3285
3286 bios->fp.last_script_invoc = (script << 1 | head);
3287
3288 sel_clk = NVReadRAMDAC(dev, 0, NV_PRAMDAC_SEL_CLK) & ~0x50000;
3289 NVWriteRAMDAC(dev, 0, NV_PRAMDAC_SEL_CLK, sel_clk | sel_clk_binding);
3290 /* some scripts set a value in NV_PBUS_POWERCTRL_2 and break video overlay */
3291 nvWriteMC(dev, NV_PBUS_POWERCTRL_2, 0);
3292
3293 return ret;
3294}
3295
3296struct lvdstableheader {
3297 uint8_t lvds_ver, headerlen, recordlen;
3298};
3299
3300static int parse_lvds_manufacturer_table_header(struct drm_device *dev, struct nvbios *bios, struct lvdstableheader *lth)
3301{
3302 /*
3303 * BMP version (0xa) LVDS table has a simple header of version and
3304 * record length. The BIT LVDS table has the typical BIT table header:
3305 * version byte, header length byte, record length byte, and a byte for
3306 * the maximum number of records that can be held in the table.
3307 */
3308
3309 uint8_t lvds_ver, headerlen, recordlen;
3310
3311 memset(lth, 0, sizeof(struct lvdstableheader));
3312
3313 if (bios->fp.lvdsmanufacturerpointer == 0x0) {
3314 NV_ERROR(dev, "Pointer to LVDS manufacturer table invalid\n");
3315 return -EINVAL;
3316 }
3317
3318 lvds_ver = bios->data[bios->fp.lvdsmanufacturerpointer];
3319
3320 switch (lvds_ver) {
3321 case 0x0a: /* pre NV40 */
3322 headerlen = 2;
3323 recordlen = bios->data[bios->fp.lvdsmanufacturerpointer + 1];
3324 break;
3325 case 0x30: /* NV4x */
3326 headerlen = bios->data[bios->fp.lvdsmanufacturerpointer + 1];
3327 if (headerlen < 0x1f) {
3328 NV_ERROR(dev, "LVDS table header not understood\n");
3329 return -EINVAL;
3330 }
3331 recordlen = bios->data[bios->fp.lvdsmanufacturerpointer + 2];
3332 break;
3333 case 0x40: /* G80/G90 */
3334 headerlen = bios->data[bios->fp.lvdsmanufacturerpointer + 1];
3335 if (headerlen < 0x7) {
3336 NV_ERROR(dev, "LVDS table header not understood\n");
3337 return -EINVAL;
3338 }
3339 recordlen = bios->data[bios->fp.lvdsmanufacturerpointer + 2];
3340 break;
3341 default:
3342 NV_ERROR(dev,
3343 "LVDS table revision %d.%d not currently supported\n",
3344 lvds_ver >> 4, lvds_ver & 0xf);
3345 return -ENOSYS;
3346 }
3347
3348 lth->lvds_ver = lvds_ver;
3349 lth->headerlen = headerlen;
3350 lth->recordlen = recordlen;
3351
3352 return 0;
3353}
3354
3355static int
3356get_fp_strap(struct drm_device *dev, struct nvbios *bios)
3357{
3358 struct drm_nouveau_private *dev_priv = dev->dev_private;
3359
3360 /*
3361 * The fp strap is normally dictated by the "User Strap" in
3362 * PEXTDEV_BOOT_0[20:16], but on BMP cards when bit 2 of the
3363 * Internal_Flags struct at 0x48 is set, the user strap gets overriden
3364 * by the PCI subsystem ID during POST, but not before the previous user
3365 * strap has been committed to CR58 for CR57=0xf on head A, which may be
3366 * read and used instead
3367 */
3368
3369 if (bios->major_version < 5 && bios->data[0x48] & 0x4)
3370 return NVReadVgaCrtc5758(dev, 0, 0xf) & 0xf;
3371
3372 if (dev_priv->card_type >= NV_50)
3373 return (bios_rd32(bios, NV_PEXTDEV_BOOT_0) >> 24) & 0xf;
3374 else
3375 return (bios_rd32(bios, NV_PEXTDEV_BOOT_0) >> 16) & 0xf;
3376}
3377
3378static int parse_fp_mode_table(struct drm_device *dev, struct nvbios *bios)
3379{
3380 uint8_t *fptable;
3381 uint8_t fptable_ver, headerlen = 0, recordlen, fpentries = 0xf, fpindex;
3382 int ret, ofs, fpstrapping;
3383 struct lvdstableheader lth;
3384
3385 if (bios->fp.fptablepointer == 0x0) {
3386 /* Apple cards don't have the fp table; the laptops use DDC */
3387 /* The table is also missing on some x86 IGPs */
3388#ifndef __powerpc__
3389 NV_ERROR(dev, "Pointer to flat panel table invalid\n");
3390#endif
Ben Skeggs04a39c52010-02-24 10:03:05 +10003391 bios->digital_min_front_porch = 0x4b;
Ben Skeggs6ee73862009-12-11 19:24:15 +10003392 return 0;
3393 }
3394
3395 fptable = &bios->data[bios->fp.fptablepointer];
3396 fptable_ver = fptable[0];
3397
3398 switch (fptable_ver) {
3399 /*
3400 * BMP version 0x5.0x11 BIOSen have version 1 like tables, but no
3401 * version field, and miss one of the spread spectrum/PWM bytes.
3402 * This could affect early GF2Go parts (not seen any appropriate ROMs
3403 * though). Here we assume that a version of 0x05 matches this case
3404 * (combining with a BMP version check would be better), as the
3405 * common case for the panel type field is 0x0005, and that is in
3406 * fact what we are reading the first byte of.
3407 */
3408 case 0x05: /* some NV10, 11, 15, 16 */
3409 recordlen = 42;
3410 ofs = -1;
3411 break;
3412 case 0x10: /* some NV15/16, and NV11+ */
3413 recordlen = 44;
3414 ofs = 0;
3415 break;
3416 case 0x20: /* NV40+ */
3417 headerlen = fptable[1];
3418 recordlen = fptable[2];
3419 fpentries = fptable[3];
3420 /*
3421 * fptable[4] is the minimum
3422 * RAMDAC_FP_HCRTC -> RAMDAC_FP_HSYNC_START gap
3423 */
Ben Skeggs04a39c52010-02-24 10:03:05 +10003424 bios->digital_min_front_porch = fptable[4];
Ben Skeggs6ee73862009-12-11 19:24:15 +10003425 ofs = -7;
3426 break;
3427 default:
3428 NV_ERROR(dev,
3429 "FP table revision %d.%d not currently supported\n",
3430 fptable_ver >> 4, fptable_ver & 0xf);
3431 return -ENOSYS;
3432 }
3433
3434 if (!bios->is_mobile) /* !mobile only needs digital_min_front_porch */
3435 return 0;
3436
3437 ret = parse_lvds_manufacturer_table_header(dev, bios, &lth);
3438 if (ret)
3439 return ret;
3440
3441 if (lth.lvds_ver == 0x30 || lth.lvds_ver == 0x40) {
3442 bios->fp.fpxlatetableptr = bios->fp.lvdsmanufacturerpointer +
3443 lth.headerlen + 1;
3444 bios->fp.xlatwidth = lth.recordlen;
3445 }
3446 if (bios->fp.fpxlatetableptr == 0x0) {
3447 NV_ERROR(dev, "Pointer to flat panel xlat table invalid\n");
3448 return -EINVAL;
3449 }
3450
3451 fpstrapping = get_fp_strap(dev, bios);
3452
3453 fpindex = bios->data[bios->fp.fpxlatetableptr +
3454 fpstrapping * bios->fp.xlatwidth];
3455
3456 if (fpindex > fpentries) {
3457 NV_ERROR(dev, "Bad flat panel table index\n");
3458 return -ENOENT;
3459 }
3460
3461 /* nv4x cards need both a strap value and fpindex of 0xf to use DDC */
3462 if (lth.lvds_ver > 0x10)
Ben Skeggs04a39c52010-02-24 10:03:05 +10003463 bios->fp_no_ddc = fpstrapping != 0xf || fpindex != 0xf;
Ben Skeggs6ee73862009-12-11 19:24:15 +10003464
3465 /*
3466 * If either the strap or xlated fpindex value are 0xf there is no
3467 * panel using a strap-derived bios mode present. this condition
3468 * includes, but is different from, the DDC panel indicator above
3469 */
3470 if (fpstrapping == 0xf || fpindex == 0xf)
3471 return 0;
3472
3473 bios->fp.mode_ptr = bios->fp.fptablepointer + headerlen +
3474 recordlen * fpindex + ofs;
3475
3476 NV_TRACE(dev, "BIOS FP mode: %dx%d (%dkHz pixel clock)\n",
3477 ROM16(bios->data[bios->fp.mode_ptr + 11]) + 1,
3478 ROM16(bios->data[bios->fp.mode_ptr + 25]) + 1,
3479 ROM16(bios->data[bios->fp.mode_ptr + 7]) * 10);
3480
3481 return 0;
3482}
3483
3484bool nouveau_bios_fp_mode(struct drm_device *dev, struct drm_display_mode *mode)
3485{
3486 struct drm_nouveau_private *dev_priv = dev->dev_private;
Ben Skeggs04a39c52010-02-24 10:03:05 +10003487 struct nvbios *bios = &dev_priv->vbios;
Ben Skeggs6ee73862009-12-11 19:24:15 +10003488 uint8_t *mode_entry = &bios->data[bios->fp.mode_ptr];
3489
3490 if (!mode) /* just checking whether we can produce a mode */
3491 return bios->fp.mode_ptr;
3492
3493 memset(mode, 0, sizeof(struct drm_display_mode));
3494 /*
3495 * For version 1.0 (version in byte 0):
3496 * bytes 1-2 are "panel type", including bits on whether Colour/mono,
3497 * single/dual link, and type (TFT etc.)
3498 * bytes 3-6 are bits per colour in RGBX
3499 */
3500 mode->clock = ROM16(mode_entry[7]) * 10;
3501 /* bytes 9-10 is HActive */
3502 mode->hdisplay = ROM16(mode_entry[11]) + 1;
3503 /*
3504 * bytes 13-14 is HValid Start
3505 * bytes 15-16 is HValid End
3506 */
3507 mode->hsync_start = ROM16(mode_entry[17]) + 1;
3508 mode->hsync_end = ROM16(mode_entry[19]) + 1;
3509 mode->htotal = ROM16(mode_entry[21]) + 1;
3510 /* bytes 23-24, 27-30 similarly, but vertical */
3511 mode->vdisplay = ROM16(mode_entry[25]) + 1;
3512 mode->vsync_start = ROM16(mode_entry[31]) + 1;
3513 mode->vsync_end = ROM16(mode_entry[33]) + 1;
3514 mode->vtotal = ROM16(mode_entry[35]) + 1;
3515 mode->flags |= (mode_entry[37] & 0x10) ?
3516 DRM_MODE_FLAG_PHSYNC : DRM_MODE_FLAG_NHSYNC;
3517 mode->flags |= (mode_entry[37] & 0x1) ?
3518 DRM_MODE_FLAG_PVSYNC : DRM_MODE_FLAG_NVSYNC;
3519 /*
3520 * bytes 38-39 relate to spread spectrum settings
3521 * bytes 40-43 are something to do with PWM
3522 */
3523
3524 mode->status = MODE_OK;
3525 mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
3526 drm_mode_set_name(mode);
3527 return bios->fp.mode_ptr;
3528}
3529
3530int nouveau_bios_parse_lvds_table(struct drm_device *dev, int pxclk, bool *dl, bool *if_is_24bit)
3531{
3532 /*
3533 * The LVDS table header is (mostly) described in
3534 * parse_lvds_manufacturer_table_header(): the BIT header additionally
3535 * contains the dual-link transition pxclk (in 10s kHz), at byte 5 - if
3536 * straps are not being used for the panel, this specifies the frequency
3537 * at which modes should be set up in the dual link style.
3538 *
3539 * Following the header, the BMP (ver 0xa) table has several records,
Daniel Mack3ad2f3f2010-02-03 08:01:28 +08003540 * indexed by a separate xlat table, indexed in turn by the fp strap in
Ben Skeggs6ee73862009-12-11 19:24:15 +10003541 * EXTDEV_BOOT. Each record had a config byte, followed by 6 script
3542 * numbers for use by INIT_SUB which controlled panel init and power,
3543 * and finally a dword of ms to sleep between power off and on
3544 * operations.
3545 *
3546 * In the BIT versions, the table following the header serves as an
3547 * integrated config and xlat table: the records in the table are
3548 * indexed by the FP strap nibble in EXTDEV_BOOT, and each record has
3549 * two bytes - the first as a config byte, the second for indexing the
3550 * fp mode table pointed to by the BIT 'D' table
3551 *
3552 * DDC is not used until after card init, so selecting the correct table
3553 * entry and setting the dual link flag for EDID equipped panels,
3554 * requiring tests against the native-mode pixel clock, cannot be done
3555 * until later, when this function should be called with non-zero pxclk
3556 */
3557 struct drm_nouveau_private *dev_priv = dev->dev_private;
Ben Skeggs04a39c52010-02-24 10:03:05 +10003558 struct nvbios *bios = &dev_priv->vbios;
Ben Skeggs6ee73862009-12-11 19:24:15 +10003559 int fpstrapping = get_fp_strap(dev, bios), lvdsmanufacturerindex = 0;
3560 struct lvdstableheader lth;
3561 uint16_t lvdsofs;
Ben Skeggs04a39c52010-02-24 10:03:05 +10003562 int ret, chip_version = bios->chip_version;
Ben Skeggs6ee73862009-12-11 19:24:15 +10003563
3564 ret = parse_lvds_manufacturer_table_header(dev, bios, &lth);
3565 if (ret)
3566 return ret;
3567
3568 switch (lth.lvds_ver) {
3569 case 0x0a: /* pre NV40 */
3570 lvdsmanufacturerindex = bios->data[
3571 bios->fp.fpxlatemanufacturertableptr +
3572 fpstrapping];
3573
3574 /* we're done if this isn't the EDID panel case */
3575 if (!pxclk)
3576 break;
3577
3578 if (chip_version < 0x25) {
3579 /* nv17 behaviour
3580 *
3581 * It seems the old style lvds script pointer is reused
3582 * to select 18/24 bit colour depth for EDID panels.
3583 */
3584 lvdsmanufacturerindex =
3585 (bios->legacy.lvds_single_a_script_ptr & 1) ?
3586 2 : 0;
3587 if (pxclk >= bios->fp.duallink_transition_clk)
3588 lvdsmanufacturerindex++;
3589 } else if (chip_version < 0x30) {
3590 /* nv28 behaviour (off-chip encoder)
3591 *
3592 * nv28 does a complex dance of first using byte 121 of
3593 * the EDID to choose the lvdsmanufacturerindex, then
3594 * later attempting to match the EDID manufacturer and
3595 * product IDs in a table (signature 'pidt' (panel id
3596 * table?)), setting an lvdsmanufacturerindex of 0 and
3597 * an fp strap of the match index (or 0xf if none)
3598 */
3599 lvdsmanufacturerindex = 0;
3600 } else {
3601 /* nv31, nv34 behaviour */
3602 lvdsmanufacturerindex = 0;
3603 if (pxclk >= bios->fp.duallink_transition_clk)
3604 lvdsmanufacturerindex = 2;
3605 if (pxclk >= 140000)
3606 lvdsmanufacturerindex = 3;
3607 }
3608
3609 /*
3610 * nvidia set the high nibble of (cr57=f, cr58) to
3611 * lvdsmanufacturerindex in this case; we don't
3612 */
3613 break;
3614 case 0x30: /* NV4x */
3615 case 0x40: /* G80/G90 */
3616 lvdsmanufacturerindex = fpstrapping;
3617 break;
3618 default:
3619 NV_ERROR(dev, "LVDS table revision not currently supported\n");
3620 return -ENOSYS;
3621 }
3622
3623 lvdsofs = bios->fp.xlated_entry = bios->fp.lvdsmanufacturerpointer + lth.headerlen + lth.recordlen * lvdsmanufacturerindex;
3624 switch (lth.lvds_ver) {
3625 case 0x0a:
3626 bios->fp.power_off_for_reset = bios->data[lvdsofs] & 1;
3627 bios->fp.reset_after_pclk_change = bios->data[lvdsofs] & 2;
3628 bios->fp.dual_link = bios->data[lvdsofs] & 4;
3629 bios->fp.link_c_increment = bios->data[lvdsofs] & 8;
3630 *if_is_24bit = bios->data[lvdsofs] & 16;
3631 break;
3632 case 0x30:
Ben Skeggsf3bbb9c2010-03-18 12:05:43 +10003633 case 0x40:
Ben Skeggs6ee73862009-12-11 19:24:15 +10003634 /*
3635 * No sign of the "power off for reset" or "reset for panel
3636 * on" bits, but it's safer to assume we should
3637 */
3638 bios->fp.power_off_for_reset = true;
3639 bios->fp.reset_after_pclk_change = true;
Ben Skeggsf3bbb9c2010-03-18 12:05:43 +10003640
Ben Skeggs6ee73862009-12-11 19:24:15 +10003641 /*
3642 * It's ok lvdsofs is wrong for nv4x edid case; dual_link is
Ben Skeggsf3bbb9c2010-03-18 12:05:43 +10003643 * over-written, and if_is_24bit isn't used
Ben Skeggs6ee73862009-12-11 19:24:15 +10003644 */
3645 bios->fp.dual_link = bios->data[lvdsofs] & 1;
Ben Skeggs6ee73862009-12-11 19:24:15 +10003646 bios->fp.if_is_24bit = bios->data[lvdsofs] & 2;
3647 bios->fp.strapless_is_24bit = bios->data[bios->fp.lvdsmanufacturerpointer + 4];
3648 bios->fp.duallink_transition_clk = ROM16(bios->data[bios->fp.lvdsmanufacturerpointer + 5]) * 10;
3649 break;
3650 }
3651
Ben Skeggs2eb92c82010-03-18 13:38:04 +10003652 /* Dell Latitude D620 reports a too-high value for the dual-link
3653 * transition freq, causing us to program the panel incorrectly.
3654 *
3655 * It doesn't appear the VBIOS actually uses its transition freq
3656 * (90000kHz), instead it uses the "Number of LVDS channels" field
3657 * out of the panel ID structure (http://www.spwg.org/).
3658 *
3659 * For the moment, a quirk will do :)
3660 */
3661 if ((dev->pdev->device == 0x01d7) &&
3662 (dev->pdev->subsystem_vendor == 0x1028) &&
3663 (dev->pdev->subsystem_device == 0x01c2)) {
3664 bios->fp.duallink_transition_clk = 80000;
3665 }
3666
Ben Skeggs6ee73862009-12-11 19:24:15 +10003667 /* set dual_link flag for EDID case */
3668 if (pxclk && (chip_version < 0x25 || chip_version > 0x28))
3669 bios->fp.dual_link = (pxclk >= bios->fp.duallink_transition_clk);
3670
3671 *dl = bios->fp.dual_link;
3672
3673 return 0;
3674}
3675
3676static uint8_t *
3677bios_output_config_match(struct drm_device *dev, struct dcb_entry *dcbent,
3678 uint16_t record, int record_len, int record_nr)
3679{
3680 struct drm_nouveau_private *dev_priv = dev->dev_private;
Ben Skeggs04a39c52010-02-24 10:03:05 +10003681 struct nvbios *bios = &dev_priv->vbios;
Ben Skeggs6ee73862009-12-11 19:24:15 +10003682 uint32_t entry;
3683 uint16_t table;
3684 int i, v;
3685
3686 for (i = 0; i < record_nr; i++, record += record_len) {
3687 table = ROM16(bios->data[record]);
3688 if (!table)
3689 continue;
3690 entry = ROM32(bios->data[table]);
3691
3692 v = (entry & 0x000f0000) >> 16;
3693 if (!(v & dcbent->or))
3694 continue;
3695
3696 v = (entry & 0x000000f0) >> 4;
3697 if (v != dcbent->location)
3698 continue;
3699
3700 v = (entry & 0x0000000f);
3701 if (v != dcbent->type)
3702 continue;
3703
3704 return &bios->data[table];
3705 }
3706
3707 return NULL;
3708}
3709
3710void *
3711nouveau_bios_dp_table(struct drm_device *dev, struct dcb_entry *dcbent,
3712 int *length)
3713{
3714 struct drm_nouveau_private *dev_priv = dev->dev_private;
Ben Skeggs04a39c52010-02-24 10:03:05 +10003715 struct nvbios *bios = &dev_priv->vbios;
Ben Skeggs6ee73862009-12-11 19:24:15 +10003716 uint8_t *table;
3717
3718 if (!bios->display.dp_table_ptr) {
3719 NV_ERROR(dev, "No pointer to DisplayPort table\n");
3720 return NULL;
3721 }
3722 table = &bios->data[bios->display.dp_table_ptr];
3723
Ben Skeggsc52e53f2010-02-25 11:53:00 +10003724 if (table[0] != 0x20 && table[0] != 0x21) {
Ben Skeggs6ee73862009-12-11 19:24:15 +10003725 NV_ERROR(dev, "DisplayPort table version 0x%02x unknown\n",
3726 table[0]);
3727 return NULL;
3728 }
3729
3730 *length = table[4];
3731 return bios_output_config_match(dev, dcbent,
3732 bios->display.dp_table_ptr + table[1],
3733 table[2], table[3]);
3734}
3735
3736int
3737nouveau_bios_run_display_table(struct drm_device *dev, struct dcb_entry *dcbent,
3738 uint32_t sub, int pxclk)
3739{
3740 /*
3741 * The display script table is located by the BIT 'U' table.
3742 *
3743 * It contains an array of pointers to various tables describing
3744 * a particular output type. The first 32-bits of the output
3745 * tables contains similar information to a DCB entry, and is
3746 * used to decide whether that particular table is suitable for
3747 * the output you want to access.
3748 *
3749 * The "record header length" field here seems to indicate the
3750 * offset of the first configuration entry in the output tables.
3751 * This is 10 on most cards I've seen, but 12 has been witnessed
3752 * on DP cards, and there's another script pointer within the
3753 * header.
3754 *
3755 * offset + 0 ( 8 bits): version
3756 * offset + 1 ( 8 bits): header length
3757 * offset + 2 ( 8 bits): record length
3758 * offset + 3 ( 8 bits): number of records
3759 * offset + 4 ( 8 bits): record header length
3760 * offset + 5 (16 bits): pointer to first output script table
3761 */
3762
3763 struct drm_nouveau_private *dev_priv = dev->dev_private;
Ben Skeggs04a39c52010-02-24 10:03:05 +10003764 struct nvbios *bios = &dev_priv->vbios;
Ben Skeggs6ee73862009-12-11 19:24:15 +10003765 uint8_t *table = &bios->data[bios->display.script_table_ptr];
3766 uint8_t *otable = NULL;
3767 uint16_t script;
3768 int i = 0;
3769
3770 if (!bios->display.script_table_ptr) {
3771 NV_ERROR(dev, "No pointer to output script table\n");
3772 return 1;
3773 }
3774
3775 /*
3776 * Nothing useful has been in any of the pre-2.0 tables I've seen,
3777 * so until they are, we really don't need to care.
3778 */
3779 if (table[0] < 0x20)
3780 return 1;
3781
3782 if (table[0] != 0x20 && table[0] != 0x21) {
3783 NV_ERROR(dev, "Output script table version 0x%02x unknown\n",
3784 table[0]);
3785 return 1;
3786 }
3787
3788 /*
3789 * The output script tables describing a particular output type
3790 * look as follows:
3791 *
3792 * offset + 0 (32 bits): output this table matches (hash of DCB)
3793 * offset + 4 ( 8 bits): unknown
3794 * offset + 5 ( 8 bits): number of configurations
3795 * offset + 6 (16 bits): pointer to some script
3796 * offset + 8 (16 bits): pointer to some script
3797 *
3798 * headerlen == 10
3799 * offset + 10 : configuration 0
3800 *
3801 * headerlen == 12
3802 * offset + 10 : pointer to some script
3803 * offset + 12 : configuration 0
3804 *
3805 * Each config entry is as follows:
3806 *
3807 * offset + 0 (16 bits): unknown, assumed to be a match value
3808 * offset + 2 (16 bits): pointer to script table (clock set?)
3809 * offset + 4 (16 bits): pointer to script table (reset?)
3810 *
3811 * There doesn't appear to be a count value to say how many
3812 * entries exist in each script table, instead, a 0 value in
3813 * the first 16-bit word seems to indicate both the end of the
3814 * list and the default entry. The second 16-bit word in the
3815 * script tables is a pointer to the script to execute.
3816 */
3817
Maarten Maathuisef2bb502009-12-13 16:53:12 +01003818 NV_DEBUG_KMS(dev, "Searching for output entry for %d %d %d\n",
Ben Skeggs6ee73862009-12-11 19:24:15 +10003819 dcbent->type, dcbent->location, dcbent->or);
3820 otable = bios_output_config_match(dev, dcbent, table[1] +
3821 bios->display.script_table_ptr,
3822 table[2], table[3]);
3823 if (!otable) {
3824 NV_ERROR(dev, "Couldn't find matching output script table\n");
3825 return 1;
3826 }
3827
3828 if (pxclk < -2 || pxclk > 0) {
3829 /* Try to find matching script table entry */
3830 for (i = 0; i < otable[5]; i++) {
3831 if (ROM16(otable[table[4] + i*6]) == sub)
3832 break;
3833 }
3834
3835 if (i == otable[5]) {
3836 NV_ERROR(dev, "Table 0x%04x not found for %d/%d, "
3837 "using first\n",
3838 sub, dcbent->type, dcbent->or);
3839 i = 0;
3840 }
3841 }
3842
Ben Skeggs6ee73862009-12-11 19:24:15 +10003843 if (pxclk == 0) {
3844 script = ROM16(otable[6]);
3845 if (!script) {
Maarten Maathuisef2bb502009-12-13 16:53:12 +01003846 NV_DEBUG_KMS(dev, "output script 0 not found\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +10003847 return 1;
3848 }
3849
3850 NV_TRACE(dev, "0x%04X: parsing output script 0\n", script);
Ben Skeggs39c9bfb2010-02-09 10:22:29 +10003851 nouveau_bios_run_init_table(dev, script, dcbent);
Ben Skeggs6ee73862009-12-11 19:24:15 +10003852 } else
3853 if (pxclk == -1) {
3854 script = ROM16(otable[8]);
3855 if (!script) {
Maarten Maathuisef2bb502009-12-13 16:53:12 +01003856 NV_DEBUG_KMS(dev, "output script 1 not found\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +10003857 return 1;
3858 }
3859
3860 NV_TRACE(dev, "0x%04X: parsing output script 1\n", script);
Ben Skeggs39c9bfb2010-02-09 10:22:29 +10003861 nouveau_bios_run_init_table(dev, script, dcbent);
Ben Skeggs6ee73862009-12-11 19:24:15 +10003862 } else
3863 if (pxclk == -2) {
3864 if (table[4] >= 12)
3865 script = ROM16(otable[10]);
3866 else
3867 script = 0;
3868 if (!script) {
Maarten Maathuisef2bb502009-12-13 16:53:12 +01003869 NV_DEBUG_KMS(dev, "output script 2 not found\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +10003870 return 1;
3871 }
3872
3873 NV_TRACE(dev, "0x%04X: parsing output script 2\n", script);
Ben Skeggs39c9bfb2010-02-09 10:22:29 +10003874 nouveau_bios_run_init_table(dev, script, dcbent);
Ben Skeggs6ee73862009-12-11 19:24:15 +10003875 } else
3876 if (pxclk > 0) {
3877 script = ROM16(otable[table[4] + i*6 + 2]);
3878 if (script)
3879 script = clkcmptable(bios, script, pxclk);
3880 if (!script) {
3881 NV_ERROR(dev, "clock script 0 not found\n");
3882 return 1;
3883 }
3884
3885 NV_TRACE(dev, "0x%04X: parsing clock script 0\n", script);
Ben Skeggs39c9bfb2010-02-09 10:22:29 +10003886 nouveau_bios_run_init_table(dev, script, dcbent);
Ben Skeggs6ee73862009-12-11 19:24:15 +10003887 } else
3888 if (pxclk < 0) {
3889 script = ROM16(otable[table[4] + i*6 + 4]);
3890 if (script)
3891 script = clkcmptable(bios, script, -pxclk);
3892 if (!script) {
Maarten Maathuisef2bb502009-12-13 16:53:12 +01003893 NV_DEBUG_KMS(dev, "clock script 1 not found\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +10003894 return 1;
3895 }
3896
3897 NV_TRACE(dev, "0x%04X: parsing clock script 1\n", script);
Ben Skeggs39c9bfb2010-02-09 10:22:29 +10003898 nouveau_bios_run_init_table(dev, script, dcbent);
Ben Skeggs6ee73862009-12-11 19:24:15 +10003899 }
3900
3901 return 0;
3902}
3903
3904
3905int run_tmds_table(struct drm_device *dev, struct dcb_entry *dcbent, int head, int pxclk)
3906{
3907 /*
3908 * the pxclk parameter is in kHz
3909 *
3910 * This runs the TMDS regs setting code found on BIT bios cards
3911 *
3912 * For ffs(or) == 1 use the first table, for ffs(or) == 2 and
3913 * ffs(or) == 3, use the second.
3914 */
3915
3916 struct drm_nouveau_private *dev_priv = dev->dev_private;
Ben Skeggs04a39c52010-02-24 10:03:05 +10003917 struct nvbios *bios = &dev_priv->vbios;
3918 int cv = bios->chip_version;
Ben Skeggs6ee73862009-12-11 19:24:15 +10003919 uint16_t clktable = 0, scriptptr;
3920 uint32_t sel_clk_binding, sel_clk;
3921
3922 /* pre-nv17 off-chip tmds uses scripts, post nv17 doesn't */
3923 if (cv >= 0x17 && cv != 0x1a && cv != 0x20 &&
3924 dcbent->location != DCB_LOC_ON_CHIP)
3925 return 0;
3926
3927 switch (ffs(dcbent->or)) {
3928 case 1:
3929 clktable = bios->tmds.output0_script_ptr;
3930 break;
3931 case 2:
3932 case 3:
3933 clktable = bios->tmds.output1_script_ptr;
3934 break;
3935 }
3936
3937 if (!clktable) {
3938 NV_ERROR(dev, "Pixel clock comparison table not found\n");
3939 return -EINVAL;
3940 }
3941
3942 scriptptr = clkcmptable(bios, clktable, pxclk);
3943
3944 if (!scriptptr) {
3945 NV_ERROR(dev, "TMDS output init script not found\n");
3946 return -ENOENT;
3947 }
3948
3949 /* don't let script change pll->head binding */
3950 sel_clk_binding = bios_rd32(bios, NV_PRAMDAC_SEL_CLK) & 0x50000;
3951 run_digital_op_script(dev, scriptptr, dcbent, head, pxclk >= 165000);
3952 sel_clk = NVReadRAMDAC(dev, 0, NV_PRAMDAC_SEL_CLK) & ~0x50000;
3953 NVWriteRAMDAC(dev, 0, NV_PRAMDAC_SEL_CLK, sel_clk | sel_clk_binding);
3954
3955 return 0;
3956}
3957
3958int get_pll_limits(struct drm_device *dev, uint32_t limit_match, struct pll_lims *pll_lim)
3959{
3960 /*
3961 * PLL limits table
3962 *
3963 * Version 0x10: NV30, NV31
3964 * One byte header (version), one record of 24 bytes
3965 * Version 0x11: NV36 - Not implemented
3966 * Seems to have same record style as 0x10, but 3 records rather than 1
3967 * Version 0x20: Found on Geforce 6 cards
3968 * Trivial 4 byte BIT header. 31 (0x1f) byte record length
3969 * Version 0x21: Found on Geforce 7, 8 and some Geforce 6 cards
3970 * 5 byte header, fifth byte of unknown purpose. 35 (0x23) byte record
3971 * length in general, some (integrated) have an extra configuration byte
3972 * Version 0x30: Found on Geforce 8, separates the register mapping
3973 * from the limits tables.
3974 */
3975
3976 struct drm_nouveau_private *dev_priv = dev->dev_private;
Ben Skeggs04a39c52010-02-24 10:03:05 +10003977 struct nvbios *bios = &dev_priv->vbios;
3978 int cv = bios->chip_version, pllindex = 0;
Ben Skeggs6ee73862009-12-11 19:24:15 +10003979 uint8_t pll_lim_ver = 0, headerlen = 0, recordlen = 0, entries = 0;
3980 uint32_t crystal_strap_mask, crystal_straps;
3981
3982 if (!bios->pll_limit_tbl_ptr) {
3983 if (cv == 0x30 || cv == 0x31 || cv == 0x35 || cv == 0x36 ||
3984 cv >= 0x40) {
3985 NV_ERROR(dev, "Pointer to PLL limits table invalid\n");
3986 return -EINVAL;
3987 }
3988 } else
3989 pll_lim_ver = bios->data[bios->pll_limit_tbl_ptr];
3990
3991 crystal_strap_mask = 1 << 6;
3992 /* open coded dev->twoHeads test */
3993 if (cv > 0x10 && cv != 0x15 && cv != 0x1a && cv != 0x20)
3994 crystal_strap_mask |= 1 << 22;
3995 crystal_straps = nvReadEXTDEV(dev, NV_PEXTDEV_BOOT_0) &
3996 crystal_strap_mask;
3997
3998 switch (pll_lim_ver) {
3999 /*
4000 * We use version 0 to indicate a pre limit table bios (single stage
4001 * pll) and load the hard coded limits instead.
4002 */
4003 case 0:
4004 break;
4005 case 0x10:
4006 case 0x11:
4007 /*
4008 * Strictly v0x11 has 3 entries, but the last two don't seem
4009 * to get used.
4010 */
4011 headerlen = 1;
4012 recordlen = 0x18;
4013 entries = 1;
4014 pllindex = 0;
4015 break;
4016 case 0x20:
4017 case 0x21:
4018 case 0x30:
4019 case 0x40:
4020 headerlen = bios->data[bios->pll_limit_tbl_ptr + 1];
4021 recordlen = bios->data[bios->pll_limit_tbl_ptr + 2];
4022 entries = bios->data[bios->pll_limit_tbl_ptr + 3];
4023 break;
4024 default:
4025 NV_ERROR(dev, "PLL limits table revision 0x%X not currently "
4026 "supported\n", pll_lim_ver);
4027 return -ENOSYS;
4028 }
4029
4030 /* initialize all members to zero */
4031 memset(pll_lim, 0, sizeof(struct pll_lims));
4032
4033 if (pll_lim_ver == 0x10 || pll_lim_ver == 0x11) {
4034 uint8_t *pll_rec = &bios->data[bios->pll_limit_tbl_ptr + headerlen + recordlen * pllindex];
4035
4036 pll_lim->vco1.minfreq = ROM32(pll_rec[0]);
4037 pll_lim->vco1.maxfreq = ROM32(pll_rec[4]);
4038 pll_lim->vco2.minfreq = ROM32(pll_rec[8]);
4039 pll_lim->vco2.maxfreq = ROM32(pll_rec[12]);
4040 pll_lim->vco1.min_inputfreq = ROM32(pll_rec[16]);
4041 pll_lim->vco2.min_inputfreq = ROM32(pll_rec[20]);
4042 pll_lim->vco1.max_inputfreq = pll_lim->vco2.max_inputfreq = INT_MAX;
4043
4044 /* these values taken from nv30/31/36 */
4045 pll_lim->vco1.min_n = 0x1;
4046 if (cv == 0x36)
4047 pll_lim->vco1.min_n = 0x5;
4048 pll_lim->vco1.max_n = 0xff;
4049 pll_lim->vco1.min_m = 0x1;
4050 pll_lim->vco1.max_m = 0xd;
4051 pll_lim->vco2.min_n = 0x4;
4052 /*
4053 * On nv30, 31, 36 (i.e. all cards with two stage PLLs with this
4054 * table version (apart from nv35)), N2 is compared to
4055 * maxN2 (0x46) and 10 * maxM2 (0x4), so set maxN2 to 0x28 and
4056 * save a comparison
4057 */
4058 pll_lim->vco2.max_n = 0x28;
4059 if (cv == 0x30 || cv == 0x35)
4060 /* only 5 bits available for N2 on nv30/35 */
4061 pll_lim->vco2.max_n = 0x1f;
4062 pll_lim->vco2.min_m = 0x1;
4063 pll_lim->vco2.max_m = 0x4;
4064 pll_lim->max_log2p = 0x7;
4065 pll_lim->max_usable_log2p = 0x6;
4066 } else if (pll_lim_ver == 0x20 || pll_lim_ver == 0x21) {
4067 uint16_t plloffs = bios->pll_limit_tbl_ptr + headerlen;
4068 uint32_t reg = 0; /* default match */
4069 uint8_t *pll_rec;
4070 int i;
4071
4072 /*
4073 * First entry is default match, if nothing better. warn if
4074 * reg field nonzero
4075 */
4076 if (ROM32(bios->data[plloffs]))
4077 NV_WARN(dev, "Default PLL limit entry has non-zero "
4078 "register field\n");
4079
4080 if (limit_match > MAX_PLL_TYPES)
4081 /* we've been passed a reg as the match */
4082 reg = limit_match;
4083 else /* limit match is a pll type */
4084 for (i = 1; i < entries && !reg; i++) {
4085 uint32_t cmpreg = ROM32(bios->data[plloffs + recordlen * i]);
4086
4087 if (limit_match == NVPLL &&
4088 (cmpreg == NV_PRAMDAC_NVPLL_COEFF || cmpreg == 0x4000))
4089 reg = cmpreg;
4090 if (limit_match == MPLL &&
4091 (cmpreg == NV_PRAMDAC_MPLL_COEFF || cmpreg == 0x4020))
4092 reg = cmpreg;
4093 if (limit_match == VPLL1 &&
4094 (cmpreg == NV_PRAMDAC_VPLL_COEFF || cmpreg == 0x4010))
4095 reg = cmpreg;
4096 if (limit_match == VPLL2 &&
4097 (cmpreg == NV_RAMDAC_VPLL2 || cmpreg == 0x4018))
4098 reg = cmpreg;
4099 }
4100
4101 for (i = 1; i < entries; i++)
4102 if (ROM32(bios->data[plloffs + recordlen * i]) == reg) {
4103 pllindex = i;
4104 break;
4105 }
4106
4107 pll_rec = &bios->data[plloffs + recordlen * pllindex];
4108
4109 BIOSLOG(bios, "Loading PLL limits for reg 0x%08x\n",
4110 pllindex ? reg : 0);
4111
4112 /*
4113 * Frequencies are stored in tables in MHz, kHz are more
4114 * useful, so we convert.
4115 */
4116
4117 /* What output frequencies can each VCO generate? */
4118 pll_lim->vco1.minfreq = ROM16(pll_rec[4]) * 1000;
4119 pll_lim->vco1.maxfreq = ROM16(pll_rec[6]) * 1000;
4120 pll_lim->vco2.minfreq = ROM16(pll_rec[8]) * 1000;
4121 pll_lim->vco2.maxfreq = ROM16(pll_rec[10]) * 1000;
4122
4123 /* What input frequencies they accept (past the m-divider)? */
4124 pll_lim->vco1.min_inputfreq = ROM16(pll_rec[12]) * 1000;
4125 pll_lim->vco2.min_inputfreq = ROM16(pll_rec[14]) * 1000;
4126 pll_lim->vco1.max_inputfreq = ROM16(pll_rec[16]) * 1000;
4127 pll_lim->vco2.max_inputfreq = ROM16(pll_rec[18]) * 1000;
4128
4129 /* What values are accepted as multiplier and divider? */
4130 pll_lim->vco1.min_n = pll_rec[20];
4131 pll_lim->vco1.max_n = pll_rec[21];
4132 pll_lim->vco1.min_m = pll_rec[22];
4133 pll_lim->vco1.max_m = pll_rec[23];
4134 pll_lim->vco2.min_n = pll_rec[24];
4135 pll_lim->vco2.max_n = pll_rec[25];
4136 pll_lim->vco2.min_m = pll_rec[26];
4137 pll_lim->vco2.max_m = pll_rec[27];
4138
4139 pll_lim->max_usable_log2p = pll_lim->max_log2p = pll_rec[29];
4140 if (pll_lim->max_log2p > 0x7)
4141 /* pll decoding in nv_hw.c assumes never > 7 */
4142 NV_WARN(dev, "Max log2 P value greater than 7 (%d)\n",
4143 pll_lim->max_log2p);
4144 if (cv < 0x60)
4145 pll_lim->max_usable_log2p = 0x6;
4146 pll_lim->log2p_bias = pll_rec[30];
4147
4148 if (recordlen > 0x22)
4149 pll_lim->refclk = ROM32(pll_rec[31]);
4150
4151 if (recordlen > 0x23 && pll_rec[35])
4152 NV_WARN(dev,
4153 "Bits set in PLL configuration byte (%x)\n",
4154 pll_rec[35]);
4155
4156 /* C51 special not seen elsewhere */
4157 if (cv == 0x51 && !pll_lim->refclk) {
4158 uint32_t sel_clk = bios_rd32(bios, NV_PRAMDAC_SEL_CLK);
4159
4160 if (((limit_match == NV_PRAMDAC_VPLL_COEFF || limit_match == VPLL1) && sel_clk & 0x20) ||
4161 ((limit_match == NV_RAMDAC_VPLL2 || limit_match == VPLL2) && sel_clk & 0x80)) {
4162 if (bios_idxprt_rd(bios, NV_CIO_CRX__COLOR, NV_CIO_CRE_CHIP_ID_INDEX) < 0xa3)
4163 pll_lim->refclk = 200000;
4164 else
4165 pll_lim->refclk = 25000;
4166 }
4167 }
4168 } else if (pll_lim_ver == 0x30) { /* ver 0x30 */
4169 uint8_t *entry = &bios->data[bios->pll_limit_tbl_ptr + headerlen];
4170 uint8_t *record = NULL;
4171 int i;
4172
4173 BIOSLOG(bios, "Loading PLL limits for register 0x%08x\n",
4174 limit_match);
4175
4176 for (i = 0; i < entries; i++, entry += recordlen) {
4177 if (ROM32(entry[3]) == limit_match) {
4178 record = &bios->data[ROM16(entry[1])];
4179 break;
4180 }
4181 }
4182
4183 if (!record) {
4184 NV_ERROR(dev, "Register 0x%08x not found in PLL "
4185 "limits table", limit_match);
4186 return -ENOENT;
4187 }
4188
4189 pll_lim->vco1.minfreq = ROM16(record[0]) * 1000;
4190 pll_lim->vco1.maxfreq = ROM16(record[2]) * 1000;
4191 pll_lim->vco2.minfreq = ROM16(record[4]) * 1000;
4192 pll_lim->vco2.maxfreq = ROM16(record[6]) * 1000;
4193 pll_lim->vco1.min_inputfreq = ROM16(record[8]) * 1000;
4194 pll_lim->vco2.min_inputfreq = ROM16(record[10]) * 1000;
4195 pll_lim->vco1.max_inputfreq = ROM16(record[12]) * 1000;
4196 pll_lim->vco2.max_inputfreq = ROM16(record[14]) * 1000;
4197 pll_lim->vco1.min_n = record[16];
4198 pll_lim->vco1.max_n = record[17];
4199 pll_lim->vco1.min_m = record[18];
4200 pll_lim->vco1.max_m = record[19];
4201 pll_lim->vco2.min_n = record[20];
4202 pll_lim->vco2.max_n = record[21];
4203 pll_lim->vco2.min_m = record[22];
4204 pll_lim->vco2.max_m = record[23];
4205 pll_lim->max_usable_log2p = pll_lim->max_log2p = record[25];
4206 pll_lim->log2p_bias = record[27];
4207 pll_lim->refclk = ROM32(record[28]);
4208 } else if (pll_lim_ver) { /* ver 0x40 */
4209 uint8_t *entry = &bios->data[bios->pll_limit_tbl_ptr + headerlen];
4210 uint8_t *record = NULL;
4211 int i;
4212
4213 BIOSLOG(bios, "Loading PLL limits for register 0x%08x\n",
4214 limit_match);
4215
4216 for (i = 0; i < entries; i++, entry += recordlen) {
4217 if (ROM32(entry[3]) == limit_match) {
4218 record = &bios->data[ROM16(entry[1])];
4219 break;
4220 }
4221 }
4222
4223 if (!record) {
4224 NV_ERROR(dev, "Register 0x%08x not found in PLL "
4225 "limits table", limit_match);
4226 return -ENOENT;
4227 }
4228
4229 pll_lim->vco1.minfreq = ROM16(record[0]) * 1000;
4230 pll_lim->vco1.maxfreq = ROM16(record[2]) * 1000;
4231 pll_lim->vco1.min_inputfreq = ROM16(record[4]) * 1000;
4232 pll_lim->vco1.max_inputfreq = ROM16(record[6]) * 1000;
4233 pll_lim->vco1.min_m = record[8];
4234 pll_lim->vco1.max_m = record[9];
4235 pll_lim->vco1.min_n = record[10];
4236 pll_lim->vco1.max_n = record[11];
4237 pll_lim->min_p = record[12];
4238 pll_lim->max_p = record[13];
4239 /* where did this go to?? */
4240 if (limit_match == 0x00614100 || limit_match == 0x00614900)
4241 pll_lim->refclk = 27000;
4242 else
4243 pll_lim->refclk = 100000;
4244 }
4245
4246 /*
4247 * By now any valid limit table ought to have set a max frequency for
4248 * vco1, so if it's zero it's either a pre limit table bios, or one
4249 * with an empty limit table (seen on nv18)
4250 */
4251 if (!pll_lim->vco1.maxfreq) {
4252 pll_lim->vco1.minfreq = bios->fminvco;
4253 pll_lim->vco1.maxfreq = bios->fmaxvco;
4254 pll_lim->vco1.min_inputfreq = 0;
4255 pll_lim->vco1.max_inputfreq = INT_MAX;
4256 pll_lim->vco1.min_n = 0x1;
4257 pll_lim->vco1.max_n = 0xff;
4258 pll_lim->vco1.min_m = 0x1;
4259 if (crystal_straps == 0) {
4260 /* nv05 does this, nv11 doesn't, nv10 unknown */
4261 if (cv < 0x11)
4262 pll_lim->vco1.min_m = 0x7;
4263 pll_lim->vco1.max_m = 0xd;
4264 } else {
4265 if (cv < 0x11)
4266 pll_lim->vco1.min_m = 0x8;
4267 pll_lim->vco1.max_m = 0xe;
4268 }
4269 if (cv < 0x17 || cv == 0x1a || cv == 0x20)
4270 pll_lim->max_log2p = 4;
4271 else
4272 pll_lim->max_log2p = 5;
4273 pll_lim->max_usable_log2p = pll_lim->max_log2p;
4274 }
4275
4276 if (!pll_lim->refclk)
4277 switch (crystal_straps) {
4278 case 0:
4279 pll_lim->refclk = 13500;
4280 break;
4281 case (1 << 6):
4282 pll_lim->refclk = 14318;
4283 break;
4284 case (1 << 22):
4285 pll_lim->refclk = 27000;
4286 break;
4287 case (1 << 22 | 1 << 6):
4288 pll_lim->refclk = 25000;
4289 break;
4290 }
4291
4292#if 0 /* for easy debugging */
4293 ErrorF("pll.vco1.minfreq: %d\n", pll_lim->vco1.minfreq);
4294 ErrorF("pll.vco1.maxfreq: %d\n", pll_lim->vco1.maxfreq);
4295 ErrorF("pll.vco2.minfreq: %d\n", pll_lim->vco2.minfreq);
4296 ErrorF("pll.vco2.maxfreq: %d\n", pll_lim->vco2.maxfreq);
4297
4298 ErrorF("pll.vco1.min_inputfreq: %d\n", pll_lim->vco1.min_inputfreq);
4299 ErrorF("pll.vco1.max_inputfreq: %d\n", pll_lim->vco1.max_inputfreq);
4300 ErrorF("pll.vco2.min_inputfreq: %d\n", pll_lim->vco2.min_inputfreq);
4301 ErrorF("pll.vco2.max_inputfreq: %d\n", pll_lim->vco2.max_inputfreq);
4302
4303 ErrorF("pll.vco1.min_n: %d\n", pll_lim->vco1.min_n);
4304 ErrorF("pll.vco1.max_n: %d\n", pll_lim->vco1.max_n);
4305 ErrorF("pll.vco1.min_m: %d\n", pll_lim->vco1.min_m);
4306 ErrorF("pll.vco1.max_m: %d\n", pll_lim->vco1.max_m);
4307 ErrorF("pll.vco2.min_n: %d\n", pll_lim->vco2.min_n);
4308 ErrorF("pll.vco2.max_n: %d\n", pll_lim->vco2.max_n);
4309 ErrorF("pll.vco2.min_m: %d\n", pll_lim->vco2.min_m);
4310 ErrorF("pll.vco2.max_m: %d\n", pll_lim->vco2.max_m);
4311
4312 ErrorF("pll.max_log2p: %d\n", pll_lim->max_log2p);
4313 ErrorF("pll.log2p_bias: %d\n", pll_lim->log2p_bias);
4314
4315 ErrorF("pll.refclk: %d\n", pll_lim->refclk);
4316#endif
4317
4318 return 0;
4319}
4320
4321static void parse_bios_version(struct drm_device *dev, struct nvbios *bios, uint16_t offset)
4322{
4323 /*
4324 * offset + 0 (8 bits): Micro version
4325 * offset + 1 (8 bits): Minor version
4326 * offset + 2 (8 bits): Chip version
4327 * offset + 3 (8 bits): Major version
4328 */
4329
4330 bios->major_version = bios->data[offset + 3];
Ben Skeggs04a39c52010-02-24 10:03:05 +10004331 bios->chip_version = bios->data[offset + 2];
Ben Skeggs6ee73862009-12-11 19:24:15 +10004332 NV_TRACE(dev, "Bios version %02x.%02x.%02x.%02x\n",
4333 bios->data[offset + 3], bios->data[offset + 2],
4334 bios->data[offset + 1], bios->data[offset]);
4335}
4336
4337static void parse_script_table_pointers(struct nvbios *bios, uint16_t offset)
4338{
4339 /*
4340 * Parses the init table segment for pointers used in script execution.
4341 *
4342 * offset + 0 (16 bits): init script tables pointer
4343 * offset + 2 (16 bits): macro index table pointer
4344 * offset + 4 (16 bits): macro table pointer
4345 * offset + 6 (16 bits): condition table pointer
4346 * offset + 8 (16 bits): io condition table pointer
4347 * offset + 10 (16 bits): io flag condition table pointer
4348 * offset + 12 (16 bits): init function table pointer
4349 */
4350
4351 bios->init_script_tbls_ptr = ROM16(bios->data[offset]);
4352 bios->macro_index_tbl_ptr = ROM16(bios->data[offset + 2]);
4353 bios->macro_tbl_ptr = ROM16(bios->data[offset + 4]);
4354 bios->condition_tbl_ptr = ROM16(bios->data[offset + 6]);
4355 bios->io_condition_tbl_ptr = ROM16(bios->data[offset + 8]);
4356 bios->io_flag_condition_tbl_ptr = ROM16(bios->data[offset + 10]);
4357 bios->init_function_tbl_ptr = ROM16(bios->data[offset + 12]);
4358}
4359
4360static int parse_bit_A_tbl_entry(struct drm_device *dev, struct nvbios *bios, struct bit_entry *bitentry)
4361{
4362 /*
4363 * Parses the load detect values for g80 cards.
4364 *
4365 * offset + 0 (16 bits): loadval table pointer
4366 */
4367
4368 uint16_t load_table_ptr;
4369 uint8_t version, headerlen, entrylen, num_entries;
4370
4371 if (bitentry->length != 3) {
4372 NV_ERROR(dev, "Do not understand BIT A table\n");
4373 return -EINVAL;
4374 }
4375
4376 load_table_ptr = ROM16(bios->data[bitentry->offset]);
4377
4378 if (load_table_ptr == 0x0) {
4379 NV_ERROR(dev, "Pointer to BIT loadval table invalid\n");
4380 return -EINVAL;
4381 }
4382
4383 version = bios->data[load_table_ptr];
4384
4385 if (version != 0x10) {
4386 NV_ERROR(dev, "BIT loadval table version %d.%d not supported\n",
4387 version >> 4, version & 0xF);
4388 return -ENOSYS;
4389 }
4390
4391 headerlen = bios->data[load_table_ptr + 1];
4392 entrylen = bios->data[load_table_ptr + 2];
4393 num_entries = bios->data[load_table_ptr + 3];
4394
4395 if (headerlen != 4 || entrylen != 4 || num_entries != 2) {
4396 NV_ERROR(dev, "Do not understand BIT loadval table\n");
4397 return -EINVAL;
4398 }
4399
4400 /* First entry is normal dac, 2nd tv-out perhaps? */
Ben Skeggs04a39c52010-02-24 10:03:05 +10004401 bios->dactestval = ROM32(bios->data[load_table_ptr + headerlen]) & 0x3ff;
Ben Skeggs6ee73862009-12-11 19:24:15 +10004402
4403 return 0;
4404}
4405
4406static int parse_bit_C_tbl_entry(struct drm_device *dev, struct nvbios *bios, struct bit_entry *bitentry)
4407{
4408 /*
4409 * offset + 8 (16 bits): PLL limits table pointer
4410 *
4411 * There's more in here, but that's unknown.
4412 */
4413
4414 if (bitentry->length < 10) {
4415 NV_ERROR(dev, "Do not understand BIT C table\n");
4416 return -EINVAL;
4417 }
4418
4419 bios->pll_limit_tbl_ptr = ROM16(bios->data[bitentry->offset + 8]);
4420
4421 return 0;
4422}
4423
4424static int parse_bit_display_tbl_entry(struct drm_device *dev, struct nvbios *bios, struct bit_entry *bitentry)
4425{
4426 /*
4427 * Parses the flat panel table segment that the bit entry points to.
4428 * Starting at bitentry->offset:
4429 *
4430 * offset + 0 (16 bits): ??? table pointer - seems to have 18 byte
4431 * records beginning with a freq.
4432 * offset + 2 (16 bits): mode table pointer
4433 */
4434
4435 if (bitentry->length != 4) {
4436 NV_ERROR(dev, "Do not understand BIT display table\n");
4437 return -EINVAL;
4438 }
4439
4440 bios->fp.fptablepointer = ROM16(bios->data[bitentry->offset + 2]);
4441
4442 return 0;
4443}
4444
4445static int parse_bit_init_tbl_entry(struct drm_device *dev, struct nvbios *bios, struct bit_entry *bitentry)
4446{
4447 /*
4448 * Parses the init table segment that the bit entry points to.
4449 *
4450 * See parse_script_table_pointers for layout
4451 */
4452
4453 if (bitentry->length < 14) {
4454 NV_ERROR(dev, "Do not understand init table\n");
4455 return -EINVAL;
4456 }
4457
4458 parse_script_table_pointers(bios, bitentry->offset);
4459
4460 if (bitentry->length >= 16)
4461 bios->some_script_ptr = ROM16(bios->data[bitentry->offset + 14]);
4462 if (bitentry->length >= 18)
4463 bios->init96_tbl_ptr = ROM16(bios->data[bitentry->offset + 16]);
4464
4465 return 0;
4466}
4467
4468static int parse_bit_i_tbl_entry(struct drm_device *dev, struct nvbios *bios, struct bit_entry *bitentry)
4469{
4470 /*
4471 * BIT 'i' (info?) table
4472 *
4473 * offset + 0 (32 bits): BIOS version dword (as in B table)
4474 * offset + 5 (8 bits): BIOS feature byte (same as for BMP?)
4475 * offset + 13 (16 bits): pointer to table containing DAC load
4476 * detection comparison values
4477 *
4478 * There's other things in the table, purpose unknown
4479 */
4480
4481 uint16_t daccmpoffset;
4482 uint8_t dacver, dacheaderlen;
4483
4484 if (bitentry->length < 6) {
4485 NV_ERROR(dev, "BIT i table too short for needed information\n");
4486 return -EINVAL;
4487 }
4488
4489 parse_bios_version(dev, bios, bitentry->offset);
4490
4491 /*
4492 * bit 4 seems to indicate a mobile bios (doesn't suffer from BMP's
4493 * Quadro identity crisis), other bits possibly as for BMP feature byte
4494 */
4495 bios->feature_byte = bios->data[bitentry->offset + 5];
4496 bios->is_mobile = bios->feature_byte & FEATURE_MOBILE;
4497
4498 if (bitentry->length < 15) {
4499 NV_WARN(dev, "BIT i table not long enough for DAC load "
4500 "detection comparison table\n");
4501 return -EINVAL;
4502 }
4503
4504 daccmpoffset = ROM16(bios->data[bitentry->offset + 13]);
4505
4506 /* doesn't exist on g80 */
4507 if (!daccmpoffset)
4508 return 0;
4509
4510 /*
4511 * The first value in the table, following the header, is the
4512 * comparison value, the second entry is a comparison value for
4513 * TV load detection.
4514 */
4515
4516 dacver = bios->data[daccmpoffset];
4517 dacheaderlen = bios->data[daccmpoffset + 1];
4518
4519 if (dacver != 0x00 && dacver != 0x10) {
4520 NV_WARN(dev, "DAC load detection comparison table version "
4521 "%d.%d not known\n", dacver >> 4, dacver & 0xf);
4522 return -ENOSYS;
4523 }
4524
Ben Skeggs04a39c52010-02-24 10:03:05 +10004525 bios->dactestval = ROM32(bios->data[daccmpoffset + dacheaderlen]);
4526 bios->tvdactestval = ROM32(bios->data[daccmpoffset + dacheaderlen + 4]);
Ben Skeggs6ee73862009-12-11 19:24:15 +10004527
4528 return 0;
4529}
4530
4531static int parse_bit_lvds_tbl_entry(struct drm_device *dev, struct nvbios *bios, struct bit_entry *bitentry)
4532{
4533 /*
4534 * Parses the LVDS table segment that the bit entry points to.
4535 * Starting at bitentry->offset:
4536 *
4537 * offset + 0 (16 bits): LVDS strap xlate table pointer
4538 */
4539
4540 if (bitentry->length != 2) {
4541 NV_ERROR(dev, "Do not understand BIT LVDS table\n");
4542 return -EINVAL;
4543 }
4544
4545 /*
4546 * No idea if it's still called the LVDS manufacturer table, but
4547 * the concept's close enough.
4548 */
4549 bios->fp.lvdsmanufacturerpointer = ROM16(bios->data[bitentry->offset]);
4550
4551 return 0;
4552}
4553
4554static int
4555parse_bit_M_tbl_entry(struct drm_device *dev, struct nvbios *bios,
4556 struct bit_entry *bitentry)
4557{
4558 /*
4559 * offset + 2 (8 bits): number of options in an
4560 * INIT_RAM_RESTRICT_ZM_REG_GROUP opcode option set
4561 * offset + 3 (16 bits): pointer to strap xlate table for RAM
4562 * restrict option selection
4563 *
4564 * There's a bunch of bits in this table other than the RAM restrict
4565 * stuff that we don't use - their use currently unknown
4566 */
4567
Ben Skeggs6ee73862009-12-11 19:24:15 +10004568 /*
4569 * Older bios versions don't have a sufficiently long table for
4570 * what we want
4571 */
4572 if (bitentry->length < 0x5)
4573 return 0;
4574
4575 if (bitentry->id[1] < 2) {
Marcin Kościelnicki37383652009-12-15 00:37:31 +00004576 bios->ram_restrict_group_count = bios->data[bitentry->offset + 2];
4577 bios->ram_restrict_tbl_ptr = ROM16(bios->data[bitentry->offset + 3]);
Ben Skeggs6ee73862009-12-11 19:24:15 +10004578 } else {
Marcin Kościelnicki37383652009-12-15 00:37:31 +00004579 bios->ram_restrict_group_count = bios->data[bitentry->offset + 0];
4580 bios->ram_restrict_tbl_ptr = ROM16(bios->data[bitentry->offset + 1]);
Ben Skeggs6ee73862009-12-11 19:24:15 +10004581 }
4582
Ben Skeggs6ee73862009-12-11 19:24:15 +10004583 return 0;
4584}
4585
4586static int parse_bit_tmds_tbl_entry(struct drm_device *dev, struct nvbios *bios, struct bit_entry *bitentry)
4587{
4588 /*
4589 * Parses the pointer to the TMDS table
4590 *
4591 * Starting at bitentry->offset:
4592 *
4593 * offset + 0 (16 bits): TMDS table pointer
4594 *
4595 * The TMDS table is typically found just before the DCB table, with a
4596 * characteristic signature of 0x11,0x13 (1.1 being version, 0x13 being
4597 * length?)
4598 *
4599 * At offset +7 is a pointer to a script, which I don't know how to
4600 * run yet.
4601 * At offset +9 is a pointer to another script, likewise
4602 * Offset +11 has a pointer to a table where the first word is a pxclk
4603 * frequency and the second word a pointer to a script, which should be
4604 * run if the comparison pxclk frequency is less than the pxclk desired.
4605 * This repeats for decreasing comparison frequencies
4606 * Offset +13 has a pointer to a similar table
4607 * The selection of table (and possibly +7/+9 script) is dictated by
4608 * "or" from the DCB.
4609 */
4610
4611 uint16_t tmdstableptr, script1, script2;
4612
4613 if (bitentry->length != 2) {
4614 NV_ERROR(dev, "Do not understand BIT TMDS table\n");
4615 return -EINVAL;
4616 }
4617
4618 tmdstableptr = ROM16(bios->data[bitentry->offset]);
4619
4620 if (tmdstableptr == 0x0) {
4621 NV_ERROR(dev, "Pointer to TMDS table invalid\n");
4622 return -EINVAL;
4623 }
4624
4625 /* nv50+ has v2.0, but we don't parse it atm */
4626 if (bios->data[tmdstableptr] != 0x11) {
4627 NV_WARN(dev,
4628 "TMDS table revision %d.%d not currently supported\n",
4629 bios->data[tmdstableptr] >> 4, bios->data[tmdstableptr] & 0xf);
4630 return -ENOSYS;
4631 }
4632
4633 /*
4634 * These two scripts are odd: they don't seem to get run even when
4635 * they are not stubbed.
4636 */
4637 script1 = ROM16(bios->data[tmdstableptr + 7]);
4638 script2 = ROM16(bios->data[tmdstableptr + 9]);
4639 if (bios->data[script1] != 'q' || bios->data[script2] != 'q')
4640 NV_WARN(dev, "TMDS table script pointers not stubbed\n");
4641
4642 bios->tmds.output0_script_ptr = ROM16(bios->data[tmdstableptr + 11]);
4643 bios->tmds.output1_script_ptr = ROM16(bios->data[tmdstableptr + 13]);
4644
4645 return 0;
4646}
4647
4648static int
4649parse_bit_U_tbl_entry(struct drm_device *dev, struct nvbios *bios,
4650 struct bit_entry *bitentry)
4651{
4652 /*
4653 * Parses the pointer to the G80 output script tables
4654 *
4655 * Starting at bitentry->offset:
4656 *
4657 * offset + 0 (16 bits): output script table pointer
4658 */
4659
4660 uint16_t outputscripttableptr;
4661
4662 if (bitentry->length != 3) {
4663 NV_ERROR(dev, "Do not understand BIT U table\n");
4664 return -EINVAL;
4665 }
4666
4667 outputscripttableptr = ROM16(bios->data[bitentry->offset]);
4668 bios->display.script_table_ptr = outputscripttableptr;
4669 return 0;
4670}
4671
4672static int
4673parse_bit_displayport_tbl_entry(struct drm_device *dev, struct nvbios *bios,
4674 struct bit_entry *bitentry)
4675{
4676 bios->display.dp_table_ptr = ROM16(bios->data[bitentry->offset]);
4677 return 0;
4678}
4679
4680struct bit_table {
4681 const char id;
4682 int (* const parse_fn)(struct drm_device *, struct nvbios *, struct bit_entry *);
4683};
4684
4685#define BIT_TABLE(id, funcid) ((struct bit_table){ id, parse_bit_##funcid##_tbl_entry })
4686
4687static int
4688parse_bit_table(struct nvbios *bios, const uint16_t bitoffset,
4689 struct bit_table *table)
4690{
4691 struct drm_device *dev = bios->dev;
4692 uint8_t maxentries = bios->data[bitoffset + 4];
4693 int i, offset;
4694 struct bit_entry bitentry;
4695
4696 for (i = 0, offset = bitoffset + 6; i < maxentries; i++, offset += 6) {
4697 bitentry.id[0] = bios->data[offset];
4698
4699 if (bitentry.id[0] != table->id)
4700 continue;
4701
4702 bitentry.id[1] = bios->data[offset + 1];
4703 bitentry.length = ROM16(bios->data[offset + 2]);
4704 bitentry.offset = ROM16(bios->data[offset + 4]);
4705
4706 return table->parse_fn(dev, bios, &bitentry);
4707 }
4708
4709 NV_INFO(dev, "BIT table '%c' not found\n", table->id);
4710 return -ENOSYS;
4711}
4712
4713static int
4714parse_bit_structure(struct nvbios *bios, const uint16_t bitoffset)
4715{
4716 int ret;
4717
4718 /*
4719 * The only restriction on parsing order currently is having 'i' first
4720 * for use of bios->*_version or bios->feature_byte while parsing;
4721 * functions shouldn't be actually *doing* anything apart from pulling
4722 * data from the image into the bios struct, thus no interdependencies
4723 */
4724 ret = parse_bit_table(bios, bitoffset, &BIT_TABLE('i', i));
4725 if (ret) /* info? */
4726 return ret;
4727 if (bios->major_version >= 0x60) /* g80+ */
4728 parse_bit_table(bios, bitoffset, &BIT_TABLE('A', A));
4729 ret = parse_bit_table(bios, bitoffset, &BIT_TABLE('C', C));
4730 if (ret)
4731 return ret;
4732 parse_bit_table(bios, bitoffset, &BIT_TABLE('D', display));
4733 ret = parse_bit_table(bios, bitoffset, &BIT_TABLE('I', init));
4734 if (ret)
4735 return ret;
4736 parse_bit_table(bios, bitoffset, &BIT_TABLE('M', M)); /* memory? */
4737 parse_bit_table(bios, bitoffset, &BIT_TABLE('L', lvds));
4738 parse_bit_table(bios, bitoffset, &BIT_TABLE('T', tmds));
4739 parse_bit_table(bios, bitoffset, &BIT_TABLE('U', U));
4740 parse_bit_table(bios, bitoffset, &BIT_TABLE('d', displayport));
4741
4742 return 0;
4743}
4744
4745static int parse_bmp_structure(struct drm_device *dev, struct nvbios *bios, unsigned int offset)
4746{
4747 /*
4748 * Parses the BMP structure for useful things, but does not act on them
4749 *
4750 * offset + 5: BMP major version
4751 * offset + 6: BMP minor version
4752 * offset + 9: BMP feature byte
4753 * offset + 10: BCD encoded BIOS version
4754 *
4755 * offset + 18: init script table pointer (for bios versions < 5.10h)
4756 * offset + 20: extra init script table pointer (for bios
4757 * versions < 5.10h)
4758 *
4759 * offset + 24: memory init table pointer (used on early bios versions)
4760 * offset + 26: SDR memory sequencing setup data table
4761 * offset + 28: DDR memory sequencing setup data table
4762 *
4763 * offset + 54: index of I2C CRTC pair to use for CRT output
4764 * offset + 55: index of I2C CRTC pair to use for TV output
4765 * offset + 56: index of I2C CRTC pair to use for flat panel output
4766 * offset + 58: write CRTC index for I2C pair 0
4767 * offset + 59: read CRTC index for I2C pair 0
4768 * offset + 60: write CRTC index for I2C pair 1
4769 * offset + 61: read CRTC index for I2C pair 1
4770 *
4771 * offset + 67: maximum internal PLL frequency (single stage PLL)
4772 * offset + 71: minimum internal PLL frequency (single stage PLL)
4773 *
4774 * offset + 75: script table pointers, as described in
4775 * parse_script_table_pointers
4776 *
4777 * offset + 89: TMDS single link output A table pointer
4778 * offset + 91: TMDS single link output B table pointer
4779 * offset + 95: LVDS single link output A table pointer
4780 * offset + 105: flat panel timings table pointer
4781 * offset + 107: flat panel strapping translation table pointer
4782 * offset + 117: LVDS manufacturer panel config table pointer
4783 * offset + 119: LVDS manufacturer strapping translation table pointer
4784 *
4785 * offset + 142: PLL limits table pointer
4786 *
4787 * offset + 156: minimum pixel clock for LVDS dual link
4788 */
4789
4790 uint8_t *bmp = &bios->data[offset], bmp_version_major, bmp_version_minor;
4791 uint16_t bmplength;
4792 uint16_t legacy_scripts_offset, legacy_i2c_offset;
4793
4794 /* load needed defaults in case we can't parse this info */
Ben Skeggs7f245b22010-02-24 09:56:18 +10004795 bios->dcb.i2c[0].write = NV_CIO_CRE_DDC_WR__INDEX;
4796 bios->dcb.i2c[0].read = NV_CIO_CRE_DDC_STATUS__INDEX;
4797 bios->dcb.i2c[1].write = NV_CIO_CRE_DDC0_WR__INDEX;
4798 bios->dcb.i2c[1].read = NV_CIO_CRE_DDC0_STATUS__INDEX;
Ben Skeggs04a39c52010-02-24 10:03:05 +10004799 bios->digital_min_front_porch = 0x4b;
Ben Skeggs6ee73862009-12-11 19:24:15 +10004800 bios->fmaxvco = 256000;
4801 bios->fminvco = 128000;
4802 bios->fp.duallink_transition_clk = 90000;
4803
4804 bmp_version_major = bmp[5];
4805 bmp_version_minor = bmp[6];
4806
4807 NV_TRACE(dev, "BMP version %d.%d\n",
4808 bmp_version_major, bmp_version_minor);
4809
4810 /*
4811 * Make sure that 0x36 is blank and can't be mistaken for a DCB
4812 * pointer on early versions
4813 */
4814 if (bmp_version_major < 5)
4815 *(uint16_t *)&bios->data[0x36] = 0;
4816
4817 /*
4818 * Seems that the minor version was 1 for all major versions prior
4819 * to 5. Version 6 could theoretically exist, but I suspect BIT
4820 * happened instead.
4821 */
4822 if ((bmp_version_major < 5 && bmp_version_minor != 1) || bmp_version_major > 5) {
4823 NV_ERROR(dev, "You have an unsupported BMP version. "
4824 "Please send in your bios\n");
4825 return -ENOSYS;
4826 }
4827
4828 if (bmp_version_major == 0)
4829 /* nothing that's currently useful in this version */
4830 return 0;
4831 else if (bmp_version_major == 1)
4832 bmplength = 44; /* exact for 1.01 */
4833 else if (bmp_version_major == 2)
4834 bmplength = 48; /* exact for 2.01 */
4835 else if (bmp_version_major == 3)
4836 bmplength = 54;
4837 /* guessed - mem init tables added in this version */
4838 else if (bmp_version_major == 4 || bmp_version_minor < 0x1)
4839 /* don't know if 5.0 exists... */
4840 bmplength = 62;
4841 /* guessed - BMP I2C indices added in version 4*/
4842 else if (bmp_version_minor < 0x6)
4843 bmplength = 67; /* exact for 5.01 */
4844 else if (bmp_version_minor < 0x10)
4845 bmplength = 75; /* exact for 5.06 */
4846 else if (bmp_version_minor == 0x10)
4847 bmplength = 89; /* exact for 5.10h */
4848 else if (bmp_version_minor < 0x14)
4849 bmplength = 118; /* exact for 5.11h */
4850 else if (bmp_version_minor < 0x24)
4851 /*
4852 * Not sure of version where pll limits came in;
4853 * certainly exist by 0x24 though.
4854 */
4855 /* length not exact: this is long enough to get lvds members */
4856 bmplength = 123;
4857 else if (bmp_version_minor < 0x27)
4858 /*
4859 * Length not exact: this is long enough to get pll limit
4860 * member
4861 */
4862 bmplength = 144;
4863 else
4864 /*
4865 * Length not exact: this is long enough to get dual link
4866 * transition clock.
4867 */
4868 bmplength = 158;
4869
4870 /* checksum */
4871 if (nv_cksum(bmp, 8)) {
4872 NV_ERROR(dev, "Bad BMP checksum\n");
4873 return -EINVAL;
4874 }
4875
4876 /*
4877 * Bit 4 seems to indicate either a mobile bios or a quadro card --
4878 * mobile behaviour consistent (nv11+), quadro only seen nv18gl-nv36gl
4879 * (not nv10gl), bit 5 that the flat panel tables are present, and
4880 * bit 6 a tv bios.
4881 */
4882 bios->feature_byte = bmp[9];
4883
4884 parse_bios_version(dev, bios, offset + 10);
4885
4886 if (bmp_version_major < 5 || bmp_version_minor < 0x10)
4887 bios->old_style_init = true;
4888 legacy_scripts_offset = 18;
4889 if (bmp_version_major < 2)
4890 legacy_scripts_offset -= 4;
4891 bios->init_script_tbls_ptr = ROM16(bmp[legacy_scripts_offset]);
4892 bios->extra_init_script_tbl_ptr = ROM16(bmp[legacy_scripts_offset + 2]);
4893
4894 if (bmp_version_major > 2) { /* appears in BMP 3 */
4895 bios->legacy.mem_init_tbl_ptr = ROM16(bmp[24]);
4896 bios->legacy.sdr_seq_tbl_ptr = ROM16(bmp[26]);
4897 bios->legacy.ddr_seq_tbl_ptr = ROM16(bmp[28]);
4898 }
4899
4900 legacy_i2c_offset = 0x48; /* BMP version 2 & 3 */
4901 if (bmplength > 61)
4902 legacy_i2c_offset = offset + 54;
4903 bios->legacy.i2c_indices.crt = bios->data[legacy_i2c_offset];
4904 bios->legacy.i2c_indices.tv = bios->data[legacy_i2c_offset + 1];
4905 bios->legacy.i2c_indices.panel = bios->data[legacy_i2c_offset + 2];
Ben Skeggs7f245b22010-02-24 09:56:18 +10004906 bios->dcb.i2c[0].write = bios->data[legacy_i2c_offset + 4];
4907 bios->dcb.i2c[0].read = bios->data[legacy_i2c_offset + 5];
4908 bios->dcb.i2c[1].write = bios->data[legacy_i2c_offset + 6];
4909 bios->dcb.i2c[1].read = bios->data[legacy_i2c_offset + 7];
Ben Skeggs6ee73862009-12-11 19:24:15 +10004910
4911 if (bmplength > 74) {
4912 bios->fmaxvco = ROM32(bmp[67]);
4913 bios->fminvco = ROM32(bmp[71]);
4914 }
4915 if (bmplength > 88)
4916 parse_script_table_pointers(bios, offset + 75);
4917 if (bmplength > 94) {
4918 bios->tmds.output0_script_ptr = ROM16(bmp[89]);
4919 bios->tmds.output1_script_ptr = ROM16(bmp[91]);
4920 /*
4921 * Never observed in use with lvds scripts, but is reused for
4922 * 18/24 bit panel interface default for EDID equipped panels
4923 * (if_is_24bit not set directly to avoid any oscillation).
4924 */
4925 bios->legacy.lvds_single_a_script_ptr = ROM16(bmp[95]);
4926 }
4927 if (bmplength > 108) {
4928 bios->fp.fptablepointer = ROM16(bmp[105]);
4929 bios->fp.fpxlatetableptr = ROM16(bmp[107]);
4930 bios->fp.xlatwidth = 1;
4931 }
4932 if (bmplength > 120) {
4933 bios->fp.lvdsmanufacturerpointer = ROM16(bmp[117]);
4934 bios->fp.fpxlatemanufacturertableptr = ROM16(bmp[119]);
4935 }
4936 if (bmplength > 143)
4937 bios->pll_limit_tbl_ptr = ROM16(bmp[142]);
4938
4939 if (bmplength > 157)
4940 bios->fp.duallink_transition_clk = ROM16(bmp[156]) * 10;
4941
4942 return 0;
4943}
4944
4945static uint16_t findstr(uint8_t *data, int n, const uint8_t *str, int len)
4946{
4947 int i, j;
4948
4949 for (i = 0; i <= (n - len); i++) {
4950 for (j = 0; j < len; j++)
4951 if (data[i + j] != str[j])
4952 break;
4953 if (j == len)
4954 return i;
4955 }
4956
4957 return 0;
4958}
4959
4960static int
4961read_dcb_i2c_entry(struct drm_device *dev, int dcb_version, uint8_t *i2ctable, int index, struct dcb_i2c_entry *i2c)
4962{
4963 uint8_t dcb_i2c_ver = dcb_version, headerlen = 0, entry_len = 4;
4964 int i2c_entries = DCB_MAX_NUM_I2C_ENTRIES;
4965 int recordoffset = 0, rdofs = 1, wrofs = 0;
4966 uint8_t port_type = 0;
4967
4968 if (!i2ctable)
4969 return -EINVAL;
4970
4971 if (dcb_version >= 0x30) {
4972 if (i2ctable[0] != dcb_version) /* necessary? */
4973 NV_WARN(dev,
4974 "DCB I2C table version mismatch (%02X vs %02X)\n",
4975 i2ctable[0], dcb_version);
4976 dcb_i2c_ver = i2ctable[0];
4977 headerlen = i2ctable[1];
4978 if (i2ctable[2] <= DCB_MAX_NUM_I2C_ENTRIES)
4979 i2c_entries = i2ctable[2];
4980 else
4981 NV_WARN(dev,
4982 "DCB I2C table has more entries than indexable "
Marcin Slusarz761c5a62010-02-15 23:24:49 +01004983 "(%d entries, max %d)\n", i2ctable[2],
4984 DCB_MAX_NUM_I2C_ENTRIES);
Ben Skeggs6ee73862009-12-11 19:24:15 +10004985 entry_len = i2ctable[3];
4986 /* [4] is i2c_default_indices, read in parse_dcb_table() */
4987 }
4988 /*
4989 * It's your own fault if you call this function on a DCB 1.1 BIOS --
4990 * the test below is for DCB 1.2
4991 */
4992 if (dcb_version < 0x14) {
4993 recordoffset = 2;
4994 rdofs = 0;
4995 wrofs = 1;
4996 }
4997
4998 if (index == 0xf)
4999 return 0;
Marcin Slusarz761c5a62010-02-15 23:24:49 +01005000 if (index >= i2c_entries) {
5001 NV_ERROR(dev, "DCB I2C index too big (%d >= %d)\n",
Ben Skeggs6ee73862009-12-11 19:24:15 +10005002 index, i2ctable[2]);
5003 return -ENOENT;
5004 }
5005 if (i2ctable[headerlen + entry_len * index + 3] == 0xff) {
5006 NV_ERROR(dev, "DCB I2C entry invalid\n");
5007 return -EINVAL;
5008 }
5009
5010 if (dcb_i2c_ver >= 0x30) {
5011 port_type = i2ctable[headerlen + recordoffset + 3 + entry_len * index];
5012
5013 /*
5014 * Fixup for chips using same address offset for read and
5015 * write.
5016 */
5017 if (port_type == 4) /* seen on C51 */
5018 rdofs = wrofs = 1;
5019 if (port_type >= 5) /* G80+ */
5020 rdofs = wrofs = 0;
5021 }
5022
5023 if (dcb_i2c_ver >= 0x40 && port_type != 5 && port_type != 6)
5024 NV_WARN(dev, "DCB I2C table has port type %d\n", port_type);
5025
5026 i2c->port_type = port_type;
5027 i2c->read = i2ctable[headerlen + recordoffset + rdofs + entry_len * index];
5028 i2c->write = i2ctable[headerlen + recordoffset + wrofs + entry_len * index];
5029
5030 return 0;
5031}
5032
5033static struct dcb_gpio_entry *
5034new_gpio_entry(struct nvbios *bios)
5035{
Ben Skeggs7f245b22010-02-24 09:56:18 +10005036 struct dcb_gpio_table *gpio = &bios->dcb.gpio;
Ben Skeggs6ee73862009-12-11 19:24:15 +10005037
5038 return &gpio->entry[gpio->entries++];
5039}
5040
5041struct dcb_gpio_entry *
5042nouveau_bios_gpio_entry(struct drm_device *dev, enum dcb_gpio_tag tag)
5043{
5044 struct drm_nouveau_private *dev_priv = dev->dev_private;
Ben Skeggs04a39c52010-02-24 10:03:05 +10005045 struct nvbios *bios = &dev_priv->vbios;
Ben Skeggs6ee73862009-12-11 19:24:15 +10005046 int i;
5047
Ben Skeggs7f245b22010-02-24 09:56:18 +10005048 for (i = 0; i < bios->dcb.gpio.entries; i++) {
5049 if (bios->dcb.gpio.entry[i].tag != tag)
Ben Skeggs6ee73862009-12-11 19:24:15 +10005050 continue;
5051
Ben Skeggs7f245b22010-02-24 09:56:18 +10005052 return &bios->dcb.gpio.entry[i];
Ben Skeggs6ee73862009-12-11 19:24:15 +10005053 }
5054
5055 return NULL;
5056}
5057
5058static void
5059parse_dcb30_gpio_entry(struct nvbios *bios, uint16_t offset)
5060{
5061 struct dcb_gpio_entry *gpio;
5062 uint16_t ent = ROM16(bios->data[offset]);
5063 uint8_t line = ent & 0x1f,
5064 tag = ent >> 5 & 0x3f,
5065 flags = ent >> 11 & 0x1f;
5066
5067 if (tag == 0x3f)
5068 return;
5069
5070 gpio = new_gpio_entry(bios);
5071
5072 gpio->tag = tag;
5073 gpio->line = line;
5074 gpio->invert = flags != 4;
Ben Skeggs2535d712010-04-07 12:00:14 +10005075 gpio->entry = ent;
Ben Skeggs6ee73862009-12-11 19:24:15 +10005076}
5077
5078static void
5079parse_dcb40_gpio_entry(struct nvbios *bios, uint16_t offset)
5080{
5081 struct dcb_gpio_entry *gpio;
5082 uint32_t ent = ROM32(bios->data[offset]);
5083 uint8_t line = ent & 0x1f,
5084 tag = ent >> 8 & 0xff;
5085
5086 if (tag == 0xff)
5087 return;
5088
5089 gpio = new_gpio_entry(bios);
5090
5091 /* Currently unused, we may need more fields parsed at some
5092 * point. */
5093 gpio->tag = tag;
5094 gpio->line = line;
Ben Skeggs2535d712010-04-07 12:00:14 +10005095 gpio->entry = ent;
Ben Skeggs6ee73862009-12-11 19:24:15 +10005096}
5097
5098static void
5099parse_dcb_gpio_table(struct nvbios *bios)
5100{
5101 struct drm_device *dev = bios->dev;
Ben Skeggs7f245b22010-02-24 09:56:18 +10005102 uint16_t gpio_table_ptr = bios->dcb.gpio_table_ptr;
Ben Skeggs6ee73862009-12-11 19:24:15 +10005103 uint8_t *gpio_table = &bios->data[gpio_table_ptr];
5104 int header_len = gpio_table[1],
5105 entries = gpio_table[2],
5106 entry_len = gpio_table[3];
5107 void (*parse_entry)(struct nvbios *, uint16_t) = NULL;
5108 int i;
5109
Ben Skeggs7f245b22010-02-24 09:56:18 +10005110 if (bios->dcb.version >= 0x40) {
Ben Skeggs6ee73862009-12-11 19:24:15 +10005111 if (gpio_table_ptr && entry_len != 4) {
5112 NV_WARN(dev, "Invalid DCB GPIO table entry length.\n");
5113 return;
5114 }
5115
5116 parse_entry = parse_dcb40_gpio_entry;
5117
Ben Skeggs7f245b22010-02-24 09:56:18 +10005118 } else if (bios->dcb.version >= 0x30) {
Ben Skeggs6ee73862009-12-11 19:24:15 +10005119 if (gpio_table_ptr && entry_len != 2) {
5120 NV_WARN(dev, "Invalid DCB GPIO table entry length.\n");
5121 return;
5122 }
5123
5124 parse_entry = parse_dcb30_gpio_entry;
5125
Ben Skeggs7f245b22010-02-24 09:56:18 +10005126 } else if (bios->dcb.version >= 0x22) {
Ben Skeggs6ee73862009-12-11 19:24:15 +10005127 /*
5128 * DCBs older than v3.0 don't really have a GPIO
5129 * table, instead they keep some GPIO info at fixed
5130 * locations.
5131 */
5132 uint16_t dcbptr = ROM16(bios->data[0x36]);
5133 uint8_t *tvdac_gpio = &bios->data[dcbptr - 5];
5134
5135 if (tvdac_gpio[0] & 1) {
5136 struct dcb_gpio_entry *gpio = new_gpio_entry(bios);
5137
5138 gpio->tag = DCB_GPIO_TVDAC0;
5139 gpio->line = tvdac_gpio[1] >> 4;
5140 gpio->invert = tvdac_gpio[0] & 2;
5141 }
5142 }
5143
5144 if (!gpio_table_ptr)
5145 return;
5146
5147 if (entries > DCB_MAX_NUM_GPIO_ENTRIES) {
5148 NV_WARN(dev, "Too many entries in the DCB GPIO table.\n");
5149 entries = DCB_MAX_NUM_GPIO_ENTRIES;
5150 }
5151
5152 for (i = 0; i < entries; i++)
5153 parse_entry(bios, gpio_table_ptr + header_len + entry_len * i);
5154}
5155
5156struct dcb_connector_table_entry *
5157nouveau_bios_connector_entry(struct drm_device *dev, int index)
5158{
5159 struct drm_nouveau_private *dev_priv = dev->dev_private;
Ben Skeggs04a39c52010-02-24 10:03:05 +10005160 struct nvbios *bios = &dev_priv->vbios;
Ben Skeggs6ee73862009-12-11 19:24:15 +10005161 struct dcb_connector_table_entry *cte;
5162
Ben Skeggs7f245b22010-02-24 09:56:18 +10005163 if (index >= bios->dcb.connector.entries)
Ben Skeggs6ee73862009-12-11 19:24:15 +10005164 return NULL;
5165
Ben Skeggs7f245b22010-02-24 09:56:18 +10005166 cte = &bios->dcb.connector.entry[index];
Ben Skeggs6ee73862009-12-11 19:24:15 +10005167 if (cte->type == 0xff)
5168 return NULL;
5169
5170 return cte;
5171}
5172
Ben Skeggsf66fa772010-02-24 11:09:20 +10005173static enum dcb_connector_type
5174divine_connector_type(struct nvbios *bios, int index)
5175{
5176 struct dcb_table *dcb = &bios->dcb;
5177 unsigned encoders = 0, type = DCB_CONNECTOR_NONE;
5178 int i;
5179
5180 for (i = 0; i < dcb->entries; i++) {
5181 if (dcb->entry[i].connector == index)
5182 encoders |= (1 << dcb->entry[i].type);
5183 }
5184
5185 if (encoders & (1 << OUTPUT_DP)) {
5186 if (encoders & (1 << OUTPUT_TMDS))
5187 type = DCB_CONNECTOR_DP;
5188 else
5189 type = DCB_CONNECTOR_eDP;
5190 } else
5191 if (encoders & (1 << OUTPUT_TMDS)) {
5192 if (encoders & (1 << OUTPUT_ANALOG))
5193 type = DCB_CONNECTOR_DVI_I;
5194 else
5195 type = DCB_CONNECTOR_DVI_D;
5196 } else
5197 if (encoders & (1 << OUTPUT_ANALOG)) {
5198 type = DCB_CONNECTOR_VGA;
5199 } else
5200 if (encoders & (1 << OUTPUT_LVDS)) {
5201 type = DCB_CONNECTOR_LVDS;
5202 } else
5203 if (encoders & (1 << OUTPUT_TV)) {
5204 type = DCB_CONNECTOR_TV_0;
5205 }
5206
5207 return type;
5208}
5209
Ben Skeggs6ee73862009-12-11 19:24:15 +10005210static void
Ben Skeggs53c44c32010-03-04 12:12:22 +10005211apply_dcb_connector_quirks(struct nvbios *bios, int idx)
5212{
5213 struct dcb_connector_table_entry *cte = &bios->dcb.connector.entry[idx];
5214 struct drm_device *dev = bios->dev;
5215
5216 /* Gigabyte NX85T */
5217 if ((dev->pdev->device == 0x0421) &&
5218 (dev->pdev->subsystem_vendor == 0x1458) &&
5219 (dev->pdev->subsystem_device == 0x344c)) {
5220 if (cte->type == DCB_CONNECTOR_HDMI_1)
5221 cte->type = DCB_CONNECTOR_DVI_I;
5222 }
5223}
5224
5225static void
Ben Skeggs6ee73862009-12-11 19:24:15 +10005226parse_dcb_connector_table(struct nvbios *bios)
5227{
5228 struct drm_device *dev = bios->dev;
Ben Skeggs7f245b22010-02-24 09:56:18 +10005229 struct dcb_connector_table *ct = &bios->dcb.connector;
Ben Skeggs6ee73862009-12-11 19:24:15 +10005230 struct dcb_connector_table_entry *cte;
Ben Skeggs7f245b22010-02-24 09:56:18 +10005231 uint8_t *conntab = &bios->data[bios->dcb.connector_table_ptr];
Ben Skeggs6ee73862009-12-11 19:24:15 +10005232 uint8_t *entry;
5233 int i;
5234
Ben Skeggs7f245b22010-02-24 09:56:18 +10005235 if (!bios->dcb.connector_table_ptr) {
Maarten Maathuisef2bb502009-12-13 16:53:12 +01005236 NV_DEBUG_KMS(dev, "No DCB connector table present\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +10005237 return;
5238 }
5239
5240 NV_INFO(dev, "DCB connector table: VHER 0x%02x %d %d %d\n",
5241 conntab[0], conntab[1], conntab[2], conntab[3]);
5242 if ((conntab[0] != 0x30 && conntab[0] != 0x40) ||
5243 (conntab[3] != 2 && conntab[3] != 4)) {
5244 NV_ERROR(dev, " Unknown! Please report.\n");
5245 return;
5246 }
5247
5248 ct->entries = conntab[2];
5249
5250 entry = conntab + conntab[1];
5251 cte = &ct->entry[0];
5252 for (i = 0; i < conntab[2]; i++, entry += conntab[3], cte++) {
Ben Skeggsd544d622010-03-10 15:52:43 +10005253 cte->index = i;
Ben Skeggs6ee73862009-12-11 19:24:15 +10005254 if (conntab[3] == 2)
5255 cte->entry = ROM16(entry[0]);
5256 else
5257 cte->entry = ROM32(entry[0]);
Ben Skeggsf66fa772010-02-24 11:09:20 +10005258
Ben Skeggs6ee73862009-12-11 19:24:15 +10005259 cte->type = (cte->entry & 0x000000ff) >> 0;
Ben Skeggsd544d622010-03-10 15:52:43 +10005260 cte->index2 = (cte->entry & 0x00000f00) >> 8;
Ben Skeggs6ee73862009-12-11 19:24:15 +10005261 switch (cte->entry & 0x00033000) {
5262 case 0x00001000:
5263 cte->gpio_tag = 0x07;
5264 break;
5265 case 0x00002000:
5266 cte->gpio_tag = 0x08;
5267 break;
5268 case 0x00010000:
5269 cte->gpio_tag = 0x51;
5270 break;
5271 case 0x00020000:
5272 cte->gpio_tag = 0x52;
5273 break;
5274 default:
5275 cte->gpio_tag = 0xff;
5276 break;
5277 }
5278
5279 if (cte->type == 0xff)
5280 continue;
5281
Ben Skeggs53c44c32010-03-04 12:12:22 +10005282 apply_dcb_connector_quirks(bios, i);
5283
Ben Skeggs6ee73862009-12-11 19:24:15 +10005284 NV_INFO(dev, " %d: 0x%08x: type 0x%02x idx %d tag 0x%02x\n",
5285 i, cte->entry, cte->type, cte->index, cte->gpio_tag);
Ben Skeggsf66fa772010-02-24 11:09:20 +10005286
5287 /* check for known types, fallback to guessing the type
5288 * from attached encoders if we hit an unknown.
5289 */
5290 switch (cte->type) {
5291 case DCB_CONNECTOR_VGA:
5292 case DCB_CONNECTOR_TV_0:
5293 case DCB_CONNECTOR_TV_1:
5294 case DCB_CONNECTOR_TV_3:
5295 case DCB_CONNECTOR_DVI_I:
5296 case DCB_CONNECTOR_DVI_D:
5297 case DCB_CONNECTOR_LVDS:
5298 case DCB_CONNECTOR_DP:
5299 case DCB_CONNECTOR_eDP:
5300 case DCB_CONNECTOR_HDMI_0:
5301 case DCB_CONNECTOR_HDMI_1:
5302 break;
5303 default:
5304 cte->type = divine_connector_type(bios, cte->index);
Ben Skeggsda647d52010-03-04 12:00:39 +10005305 NV_WARN(dev, "unknown type, using 0x%02x\n", cte->type);
Ben Skeggsf66fa772010-02-24 11:09:20 +10005306 break;
5307 }
5308
Ben Skeggsda647d52010-03-04 12:00:39 +10005309 if (nouveau_override_conntype) {
5310 int type = divine_connector_type(bios, cte->index);
5311 if (type != cte->type)
5312 NV_WARN(dev, " -> type 0x%02x\n", cte->type);
5313 }
5314
Ben Skeggs6ee73862009-12-11 19:24:15 +10005315 }
5316}
5317
Ben Skeggs7f245b22010-02-24 09:56:18 +10005318static struct dcb_entry *new_dcb_entry(struct dcb_table *dcb)
Ben Skeggs6ee73862009-12-11 19:24:15 +10005319{
5320 struct dcb_entry *entry = &dcb->entry[dcb->entries];
5321
5322 memset(entry, 0, sizeof(struct dcb_entry));
5323 entry->index = dcb->entries++;
5324
5325 return entry;
5326}
5327
Ben Skeggs7f245b22010-02-24 09:56:18 +10005328static void fabricate_vga_output(struct dcb_table *dcb, int i2c, int heads)
Ben Skeggs6ee73862009-12-11 19:24:15 +10005329{
5330 struct dcb_entry *entry = new_dcb_entry(dcb);
5331
5332 entry->type = 0;
5333 entry->i2c_index = i2c;
5334 entry->heads = heads;
5335 entry->location = DCB_LOC_ON_CHIP;
5336 /* "or" mostly unused in early gen crt modesetting, 0 is fine */
5337}
5338
Ben Skeggs7f245b22010-02-24 09:56:18 +10005339static void fabricate_dvi_i_output(struct dcb_table *dcb, bool twoHeads)
Ben Skeggs6ee73862009-12-11 19:24:15 +10005340{
5341 struct dcb_entry *entry = new_dcb_entry(dcb);
5342
5343 entry->type = 2;
5344 entry->i2c_index = LEGACY_I2C_PANEL;
5345 entry->heads = twoHeads ? 3 : 1;
5346 entry->location = !DCB_LOC_ON_CHIP; /* ie OFF CHIP */
5347 entry->or = 1; /* means |0x10 gets set on CRE_LCD__INDEX */
5348 entry->duallink_possible = false; /* SiI164 and co. are single link */
5349
5350#if 0
5351 /*
5352 * For dvi-a either crtc probably works, but my card appears to only
5353 * support dvi-d. "nvidia" still attempts to program it for dvi-a,
5354 * doing the full fp output setup (program 0x6808.. fp dimension regs,
5355 * setting 0x680848 to 0x10000111 to enable, maybe setting 0x680880);
5356 * the monitor picks up the mode res ok and lights up, but no pixel
5357 * data appears, so the board manufacturer probably connected up the
5358 * sync lines, but missed the video traces / components
5359 *
5360 * with this introduction, dvi-a left as an exercise for the reader.
5361 */
5362 fabricate_vga_output(dcb, LEGACY_I2C_PANEL, entry->heads);
5363#endif
5364}
5365
Ben Skeggs7f245b22010-02-24 09:56:18 +10005366static void fabricate_tv_output(struct dcb_table *dcb, bool twoHeads)
Ben Skeggs6ee73862009-12-11 19:24:15 +10005367{
5368 struct dcb_entry *entry = new_dcb_entry(dcb);
5369
5370 entry->type = 1;
5371 entry->i2c_index = LEGACY_I2C_TV;
5372 entry->heads = twoHeads ? 3 : 1;
5373 entry->location = !DCB_LOC_ON_CHIP; /* ie OFF CHIP */
5374}
5375
5376static bool
Ben Skeggs7f245b22010-02-24 09:56:18 +10005377parse_dcb20_entry(struct drm_device *dev, struct dcb_table *dcb,
Ben Skeggs6ee73862009-12-11 19:24:15 +10005378 uint32_t conn, uint32_t conf, struct dcb_entry *entry)
5379{
5380 entry->type = conn & 0xf;
5381 entry->i2c_index = (conn >> 4) & 0xf;
5382 entry->heads = (conn >> 8) & 0xf;
Ben Skeggs7f245b22010-02-24 09:56:18 +10005383 if (dcb->version >= 0x40)
Ben Skeggs6ee73862009-12-11 19:24:15 +10005384 entry->connector = (conn >> 12) & 0xf;
5385 entry->bus = (conn >> 16) & 0xf;
5386 entry->location = (conn >> 20) & 0x3;
5387 entry->or = (conn >> 24) & 0xf;
5388 /*
5389 * Normal entries consist of a single bit, but dual link has the
5390 * next most significant bit set too
5391 */
5392 entry->duallink_possible =
5393 ((1 << (ffs(entry->or) - 1)) * 3 == entry->or);
5394
5395 switch (entry->type) {
5396 case OUTPUT_ANALOG:
5397 /*
5398 * Although the rest of a CRT conf dword is usually
5399 * zeros, mac biosen have stuff there so we must mask
5400 */
Ben Skeggs7f245b22010-02-24 09:56:18 +10005401 entry->crtconf.maxfreq = (dcb->version < 0x30) ?
Ben Skeggs6ee73862009-12-11 19:24:15 +10005402 (conf & 0xffff) * 10 :
5403 (conf & 0xff) * 10000;
5404 break;
5405 case OUTPUT_LVDS:
5406 {
5407 uint32_t mask;
5408 if (conf & 0x1)
5409 entry->lvdsconf.use_straps_for_mode = true;
Ben Skeggs7f245b22010-02-24 09:56:18 +10005410 if (dcb->version < 0x22) {
Ben Skeggs6ee73862009-12-11 19:24:15 +10005411 mask = ~0xd;
5412 /*
5413 * The laptop in bug 14567 lies and claims to not use
5414 * straps when it does, so assume all DCB 2.0 laptops
5415 * use straps, until a broken EDID using one is produced
5416 */
5417 entry->lvdsconf.use_straps_for_mode = true;
5418 /*
5419 * Both 0x4 and 0x8 show up in v2.0 tables; assume they
5420 * mean the same thing (probably wrong, but might work)
5421 */
5422 if (conf & 0x4 || conf & 0x8)
5423 entry->lvdsconf.use_power_scripts = true;
5424 } else {
5425 mask = ~0x5;
5426 if (conf & 0x4)
5427 entry->lvdsconf.use_power_scripts = true;
5428 }
5429 if (conf & mask) {
5430 /*
5431 * Until we even try to use these on G8x, it's
5432 * useless reporting unknown bits. They all are.
5433 */
Ben Skeggs7f245b22010-02-24 09:56:18 +10005434 if (dcb->version >= 0x40)
Ben Skeggs6ee73862009-12-11 19:24:15 +10005435 break;
5436
5437 NV_ERROR(dev, "Unknown LVDS configuration bits, "
5438 "please report\n");
5439 }
5440 break;
5441 }
5442 case OUTPUT_TV:
5443 {
Ben Skeggs7f245b22010-02-24 09:56:18 +10005444 if (dcb->version >= 0x30)
Ben Skeggs6ee73862009-12-11 19:24:15 +10005445 entry->tvconf.has_component_output = conf & (0x8 << 4);
5446 else
5447 entry->tvconf.has_component_output = false;
5448
5449 break;
5450 }
5451 case OUTPUT_DP:
5452 entry->dpconf.sor.link = (conf & 0x00000030) >> 4;
5453 entry->dpconf.link_bw = (conf & 0x00e00000) >> 21;
5454 switch ((conf & 0x0f000000) >> 24) {
5455 case 0xf:
5456 entry->dpconf.link_nr = 4;
5457 break;
5458 case 0x3:
5459 entry->dpconf.link_nr = 2;
5460 break;
5461 default:
5462 entry->dpconf.link_nr = 1;
5463 break;
5464 }
5465 break;
5466 case OUTPUT_TMDS:
5467 entry->tmdsconf.sor.link = (conf & 0x00000030) >> 4;
5468 break;
5469 case 0xe:
5470 /* weird g80 mobile type that "nv" treats as a terminator */
Ben Skeggs7f245b22010-02-24 09:56:18 +10005471 dcb->entries--;
Ben Skeggs6ee73862009-12-11 19:24:15 +10005472 return false;
Ben Skeggse7cc51c2010-02-24 10:31:39 +10005473 default:
5474 break;
Ben Skeggs6ee73862009-12-11 19:24:15 +10005475 }
5476
5477 /* unsure what DCB version introduces this, 3.0? */
5478 if (conf & 0x100000)
5479 entry->i2c_upper_default = true;
5480
5481 return true;
5482}
5483
5484static bool
Ben Skeggs7f245b22010-02-24 09:56:18 +10005485parse_dcb15_entry(struct drm_device *dev, struct dcb_table *dcb,
Ben Skeggs6ee73862009-12-11 19:24:15 +10005486 uint32_t conn, uint32_t conf, struct dcb_entry *entry)
5487{
Ben Skeggsb0d2de82010-01-14 17:53:04 +10005488 switch (conn & 0x0000000f) {
5489 case 0:
5490 entry->type = OUTPUT_ANALOG;
5491 break;
5492 case 1:
5493 entry->type = OUTPUT_TV;
5494 break;
5495 case 2:
5496 case 3:
5497 entry->type = OUTPUT_LVDS;
5498 break;
5499 case 4:
5500 switch ((conn & 0x000000f0) >> 4) {
5501 case 0:
5502 entry->type = OUTPUT_TMDS;
5503 break;
5504 case 1:
5505 entry->type = OUTPUT_LVDS;
5506 break;
5507 default:
5508 NV_ERROR(dev, "Unknown DCB subtype 4/%d\n",
5509 (conn & 0x000000f0) >> 4);
5510 return false;
5511 }
5512 break;
5513 default:
5514 NV_ERROR(dev, "Unknown DCB type %d\n", conn & 0x0000000f);
Ben Skeggs6ee73862009-12-11 19:24:15 +10005515 return false;
5516 }
Ben Skeggsb0d2de82010-01-14 17:53:04 +10005517
5518 entry->i2c_index = (conn & 0x0003c000) >> 14;
5519 entry->heads = ((conn & 0x001c0000) >> 18) + 1;
5520 entry->or = entry->heads; /* same as heads, hopefully safe enough */
5521 entry->location = (conn & 0x01e00000) >> 21;
5522 entry->bus = (conn & 0x0e000000) >> 25;
Ben Skeggs6ee73862009-12-11 19:24:15 +10005523 entry->duallink_possible = false;
5524
5525 switch (entry->type) {
5526 case OUTPUT_ANALOG:
5527 entry->crtconf.maxfreq = (conf & 0xffff) * 10;
5528 break;
Ben Skeggsb0d2de82010-01-14 17:53:04 +10005529 case OUTPUT_TV:
5530 entry->tvconf.has_component_output = false;
Ben Skeggs6ee73862009-12-11 19:24:15 +10005531 break;
5532 case OUTPUT_TMDS:
5533 /*
5534 * Invent a DVI-A output, by copying the fields of the DVI-D
5535 * output; reported to work by math_b on an NV20(!).
5536 */
5537 fabricate_vga_output(dcb, entry->i2c_index, entry->heads);
5538 break;
Ben Skeggsb0d2de82010-01-14 17:53:04 +10005539 case OUTPUT_LVDS:
5540 if ((conn & 0x00003f00) != 0x10)
5541 entry->lvdsconf.use_straps_for_mode = true;
5542 entry->lvdsconf.use_power_scripts = true;
5543 break;
5544 default:
Ben Skeggs6ee73862009-12-11 19:24:15 +10005545 break;
5546 }
5547
5548 return true;
5549}
5550
Ben Skeggs7f245b22010-02-24 09:56:18 +10005551static bool parse_dcb_entry(struct drm_device *dev, struct dcb_table *dcb,
Ben Skeggs6ee73862009-12-11 19:24:15 +10005552 uint32_t conn, uint32_t conf)
5553{
Ben Skeggs7f245b22010-02-24 09:56:18 +10005554 struct dcb_entry *entry = new_dcb_entry(dcb);
Ben Skeggs6ee73862009-12-11 19:24:15 +10005555 bool ret;
5556
Ben Skeggs7f245b22010-02-24 09:56:18 +10005557 if (dcb->version >= 0x20)
5558 ret = parse_dcb20_entry(dev, dcb, conn, conf, entry);
Ben Skeggs6ee73862009-12-11 19:24:15 +10005559 else
Ben Skeggs7f245b22010-02-24 09:56:18 +10005560 ret = parse_dcb15_entry(dev, dcb, conn, conf, entry);
Ben Skeggs6ee73862009-12-11 19:24:15 +10005561 if (!ret)
5562 return ret;
5563
Ben Skeggs7f245b22010-02-24 09:56:18 +10005564 read_dcb_i2c_entry(dev, dcb->version, dcb->i2c_table,
5565 entry->i2c_index, &dcb->i2c[entry->i2c_index]);
Ben Skeggs6ee73862009-12-11 19:24:15 +10005566
5567 return true;
5568}
5569
5570static
Ben Skeggs7f245b22010-02-24 09:56:18 +10005571void merge_like_dcb_entries(struct drm_device *dev, struct dcb_table *dcb)
Ben Skeggs6ee73862009-12-11 19:24:15 +10005572{
5573 /*
5574 * DCB v2.0 lists each output combination separately.
5575 * Here we merge compatible entries to have fewer outputs, with
5576 * more options
5577 */
5578
5579 int i, newentries = 0;
5580
5581 for (i = 0; i < dcb->entries; i++) {
5582 struct dcb_entry *ient = &dcb->entry[i];
5583 int j;
5584
5585 for (j = i + 1; j < dcb->entries; j++) {
5586 struct dcb_entry *jent = &dcb->entry[j];
5587
5588 if (jent->type == 100) /* already merged entry */
5589 continue;
5590
5591 /* merge heads field when all other fields the same */
5592 if (jent->i2c_index == ient->i2c_index &&
5593 jent->type == ient->type &&
5594 jent->location == ient->location &&
5595 jent->or == ient->or) {
5596 NV_TRACE(dev, "Merging DCB entries %d and %d\n",
5597 i, j);
5598 ient->heads |= jent->heads;
5599 jent->type = 100; /* dummy value */
5600 }
5601 }
5602 }
5603
5604 /* Compact entries merged into others out of dcb */
5605 for (i = 0; i < dcb->entries; i++) {
5606 if (dcb->entry[i].type == 100)
5607 continue;
5608
5609 if (newentries != i) {
5610 dcb->entry[newentries] = dcb->entry[i];
5611 dcb->entry[newentries].index = newentries;
5612 }
5613 newentries++;
5614 }
5615
5616 dcb->entries = newentries;
5617}
5618
Ben Skeggsed42f822010-01-14 15:58:10 +10005619static int
5620parse_dcb_table(struct drm_device *dev, struct nvbios *bios, bool twoHeads)
Ben Skeggs6ee73862009-12-11 19:24:15 +10005621{
Ben Skeggsed42f822010-01-14 15:58:10 +10005622 struct drm_nouveau_private *dev_priv = dev->dev_private;
Ben Skeggs7f245b22010-02-24 09:56:18 +10005623 struct dcb_table *dcb = &bios->dcb;
Ben Skeggsed42f822010-01-14 15:58:10 +10005624 uint16_t dcbptr = 0, i2ctabptr = 0;
Ben Skeggs6ee73862009-12-11 19:24:15 +10005625 uint8_t *dcbtable;
5626 uint8_t headerlen = 0x4, entries = DCB_MAX_NUM_ENTRIES;
5627 bool configblock = true;
5628 int recordlength = 8, confofs = 4;
5629 int i;
5630
Ben Skeggs6ee73862009-12-11 19:24:15 +10005631 /* get the offset from 0x36 */
Ben Skeggsed42f822010-01-14 15:58:10 +10005632 if (dev_priv->card_type > NV_04) {
5633 dcbptr = ROM16(bios->data[0x36]);
5634 if (dcbptr == 0x0000)
5635 NV_WARN(dev, "No output data (DCB) found in BIOS\n");
5636 }
Ben Skeggs6ee73862009-12-11 19:24:15 +10005637
Ben Skeggsed42f822010-01-14 15:58:10 +10005638 /* this situation likely means a really old card, pre DCB */
Ben Skeggs6ee73862009-12-11 19:24:15 +10005639 if (dcbptr == 0x0) {
Ben Skeggsed42f822010-01-14 15:58:10 +10005640 NV_INFO(dev, "Assuming a CRT output exists\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +10005641 fabricate_vga_output(dcb, LEGACY_I2C_CRT, 1);
5642
Ben Skeggsed42f822010-01-14 15:58:10 +10005643 if (nv04_tv_identify(dev, bios->legacy.i2c_indices.tv) >= 0)
Ben Skeggs6ee73862009-12-11 19:24:15 +10005644 fabricate_tv_output(dcb, twoHeads);
5645
5646 return 0;
5647 }
5648
5649 dcbtable = &bios->data[dcbptr];
5650
5651 /* get DCB version */
Ben Skeggs7f245b22010-02-24 09:56:18 +10005652 dcb->version = dcbtable[0];
Ben Skeggs6ee73862009-12-11 19:24:15 +10005653 NV_TRACE(dev, "Found Display Configuration Block version %d.%d\n",
Ben Skeggs7f245b22010-02-24 09:56:18 +10005654 dcb->version >> 4, dcb->version & 0xf);
Ben Skeggs6ee73862009-12-11 19:24:15 +10005655
Ben Skeggs7f245b22010-02-24 09:56:18 +10005656 if (dcb->version >= 0x20) { /* NV17+ */
Ben Skeggs6ee73862009-12-11 19:24:15 +10005657 uint32_t sig;
5658
Ben Skeggs7f245b22010-02-24 09:56:18 +10005659 if (dcb->version >= 0x30) { /* NV40+ */
Ben Skeggs6ee73862009-12-11 19:24:15 +10005660 headerlen = dcbtable[1];
5661 entries = dcbtable[2];
5662 recordlength = dcbtable[3];
5663 i2ctabptr = ROM16(dcbtable[4]);
5664 sig = ROM32(dcbtable[6]);
Ben Skeggs7f245b22010-02-24 09:56:18 +10005665 dcb->gpio_table_ptr = ROM16(dcbtable[10]);
5666 dcb->connector_table_ptr = ROM16(dcbtable[20]);
Ben Skeggs6ee73862009-12-11 19:24:15 +10005667 } else {
5668 i2ctabptr = ROM16(dcbtable[2]);
5669 sig = ROM32(dcbtable[4]);
5670 headerlen = 8;
5671 }
5672
5673 if (sig != 0x4edcbdcb) {
5674 NV_ERROR(dev, "Bad Display Configuration Block "
5675 "signature (%08X)\n", sig);
5676 return -EINVAL;
5677 }
Ben Skeggs7f245b22010-02-24 09:56:18 +10005678 } else if (dcb->version >= 0x15) { /* some NV11 and NV20 */
Ben Skeggs6ee73862009-12-11 19:24:15 +10005679 char sig[8] = { 0 };
5680
5681 strncpy(sig, (char *)&dcbtable[-7], 7);
5682 i2ctabptr = ROM16(dcbtable[2]);
5683 recordlength = 10;
5684 confofs = 6;
5685
5686 if (strcmp(sig, "DEV_REC")) {
5687 NV_ERROR(dev, "Bad Display Configuration Block "
5688 "signature (%s)\n", sig);
5689 return -EINVAL;
5690 }
5691 } else {
5692 /*
5693 * v1.4 (some NV15/16, NV11+) seems the same as v1.5, but always
5694 * has the same single (crt) entry, even when tv-out present, so
5695 * the conclusion is this version cannot really be used.
5696 * v1.2 tables (some NV6/10, and NV15+) normally have the same
5697 * 5 entries, which are not specific to the card and so no use.
5698 * v1.2 does have an I2C table that read_dcb_i2c_table can
5699 * handle, but cards exist (nv11 in #14821) with a bad i2c table
5700 * pointer, so use the indices parsed in parse_bmp_structure.
5701 * v1.1 (NV5+, maybe some NV4) is entirely unhelpful
5702 */
5703 NV_TRACEWARN(dev, "No useful information in BIOS output table; "
5704 "adding all possible outputs\n");
5705 fabricate_vga_output(dcb, LEGACY_I2C_CRT, 1);
5706
5707 /*
5708 * Attempt to detect TV before DVI because the test
5709 * for the former is more accurate and it rules the
5710 * latter out.
5711 */
5712 if (nv04_tv_identify(dev,
5713 bios->legacy.i2c_indices.tv) >= 0)
5714 fabricate_tv_output(dcb, twoHeads);
5715
5716 else if (bios->tmds.output0_script_ptr ||
5717 bios->tmds.output1_script_ptr)
5718 fabricate_dvi_i_output(dcb, twoHeads);
5719
5720 return 0;
5721 }
5722
5723 if (!i2ctabptr)
5724 NV_WARN(dev, "No pointer to DCB I2C port table\n");
5725 else {
Ben Skeggs7f245b22010-02-24 09:56:18 +10005726 dcb->i2c_table = &bios->data[i2ctabptr];
5727 if (dcb->version >= 0x30)
5728 dcb->i2c_default_indices = dcb->i2c_table[4];
Ben Skeggs6ee73862009-12-11 19:24:15 +10005729 }
5730
Ben Skeggs6ee73862009-12-11 19:24:15 +10005731 if (entries > DCB_MAX_NUM_ENTRIES)
5732 entries = DCB_MAX_NUM_ENTRIES;
5733
5734 for (i = 0; i < entries; i++) {
5735 uint32_t connection, config = 0;
5736
5737 connection = ROM32(dcbtable[headerlen + recordlength * i]);
5738 if (configblock)
5739 config = ROM32(dcbtable[headerlen + confofs + recordlength * i]);
5740
5741 /* seen on an NV11 with DCB v1.5 */
5742 if (connection == 0x00000000)
5743 break;
5744
5745 /* seen on an NV17 with DCB v2.0 */
5746 if (connection == 0xffffffff)
5747 break;
5748
5749 if ((connection & 0x0000000f) == 0x0000000f)
5750 continue;
5751
5752 NV_TRACEWARN(dev, "Raw DCB entry %d: %08x %08x\n",
5753 dcb->entries, connection, config);
5754
Ben Skeggs7f245b22010-02-24 09:56:18 +10005755 if (!parse_dcb_entry(dev, dcb, connection, config))
Ben Skeggs6ee73862009-12-11 19:24:15 +10005756 break;
5757 }
5758
5759 /*
5760 * apart for v2.1+ not being known for requiring merging, this
5761 * guarantees dcbent->index is the index of the entry in the rom image
5762 */
Ben Skeggs7f245b22010-02-24 09:56:18 +10005763 if (dcb->version < 0x21)
Ben Skeggs6ee73862009-12-11 19:24:15 +10005764 merge_like_dcb_entries(dev, dcb);
5765
Ben Skeggs54abb5d2010-02-24 10:48:16 +10005766 if (!dcb->entries)
5767 return -ENXIO;
5768
5769 parse_dcb_gpio_table(bios);
5770 parse_dcb_connector_table(bios);
5771 return 0;
Ben Skeggs6ee73862009-12-11 19:24:15 +10005772}
5773
5774static void
5775fixup_legacy_connector(struct nvbios *bios)
5776{
Ben Skeggs7f245b22010-02-24 09:56:18 +10005777 struct dcb_table *dcb = &bios->dcb;
Ben Skeggsdc5bc4e2010-02-24 11:53:59 +10005778 int i, i2c, i2c_conn[DCB_MAX_NUM_I2C_ENTRIES] = { };
Ben Skeggs6ee73862009-12-11 19:24:15 +10005779
5780 /*
5781 * DCB 3.0 also has the table in most cases, but there are some cards
5782 * where the table is filled with stub entries, and the DCB entriy
5783 * indices are all 0. We don't need the connector indices on pre-G80
5784 * chips (yet?) so limit the use to DCB 4.0 and above.
5785 */
Ben Skeggs7f245b22010-02-24 09:56:18 +10005786 if (dcb->version >= 0x40)
Ben Skeggs6ee73862009-12-11 19:24:15 +10005787 return;
5788
Ben Skeggsdc5bc4e2010-02-24 11:53:59 +10005789 dcb->connector.entries = 0;
5790
Ben Skeggs6ee73862009-12-11 19:24:15 +10005791 /*
5792 * No known connector info before v3.0, so make it up. the rule here
5793 * is: anything on the same i2c bus is considered to be on the same
5794 * connector. any output without an associated i2c bus is assigned
5795 * its own unique connector index.
5796 */
5797 for (i = 0; i < dcb->entries; i++) {
Ben Skeggs6ee73862009-12-11 19:24:15 +10005798 /*
5799 * Ignore the I2C index for on-chip TV-out, as there
5800 * are cards with bogus values (nv31m in bug 23212),
5801 * and it's otherwise useless.
5802 */
5803 if (dcb->entry[i].type == OUTPUT_TV &&
Ben Skeggsdc5bc4e2010-02-24 11:53:59 +10005804 dcb->entry[i].location == DCB_LOC_ON_CHIP)
Ben Skeggs6ee73862009-12-11 19:24:15 +10005805 dcb->entry[i].i2c_index = 0xf;
Ben Skeggsdc5bc4e2010-02-24 11:53:59 +10005806 i2c = dcb->entry[i].i2c_index;
5807
5808 if (i2c_conn[i2c]) {
5809 dcb->entry[i].connector = i2c_conn[i2c] - 1;
Ben Skeggs6ee73862009-12-11 19:24:15 +10005810 continue;
5811 }
5812
Ben Skeggsdc5bc4e2010-02-24 11:53:59 +10005813 dcb->entry[i].connector = dcb->connector.entries++;
5814 if (i2c != 0xf)
5815 i2c_conn[i2c] = dcb->connector.entries;
Ben Skeggs6ee73862009-12-11 19:24:15 +10005816 }
5817
Ben Skeggsdc5bc4e2010-02-24 11:53:59 +10005818 /* Fake the connector table as well as just connector indices */
5819 for (i = 0; i < dcb->connector.entries; i++) {
5820 dcb->connector.entry[i].index = i;
5821 dcb->connector.entry[i].type = divine_connector_type(bios, i);
5822 dcb->connector.entry[i].gpio_tag = 0xff;
Ben Skeggs6ee73862009-12-11 19:24:15 +10005823 }
5824}
5825
5826static void
5827fixup_legacy_i2c(struct nvbios *bios)
5828{
Ben Skeggs7f245b22010-02-24 09:56:18 +10005829 struct dcb_table *dcb = &bios->dcb;
Ben Skeggs6ee73862009-12-11 19:24:15 +10005830 int i;
5831
5832 for (i = 0; i < dcb->entries; i++) {
5833 if (dcb->entry[i].i2c_index == LEGACY_I2C_CRT)
5834 dcb->entry[i].i2c_index = bios->legacy.i2c_indices.crt;
5835 if (dcb->entry[i].i2c_index == LEGACY_I2C_PANEL)
5836 dcb->entry[i].i2c_index = bios->legacy.i2c_indices.panel;
5837 if (dcb->entry[i].i2c_index == LEGACY_I2C_TV)
5838 dcb->entry[i].i2c_index = bios->legacy.i2c_indices.tv;
5839 }
5840}
5841
5842static int load_nv17_hwsq_ucode_entry(struct drm_device *dev, struct nvbios *bios, uint16_t hwsq_offset, int entry)
5843{
5844 /*
5845 * The header following the "HWSQ" signature has the number of entries,
5846 * and the entry size
5847 *
5848 * An entry consists of a dword to write to the sequencer control reg
5849 * (0x00001304), followed by the ucode bytes, written sequentially,
5850 * starting at reg 0x00001400
5851 */
5852
5853 uint8_t bytes_to_write;
5854 uint16_t hwsq_entry_offset;
5855 int i;
5856
5857 if (bios->data[hwsq_offset] <= entry) {
5858 NV_ERROR(dev, "Too few entries in HW sequencer table for "
5859 "requested entry\n");
5860 return -ENOENT;
5861 }
5862
5863 bytes_to_write = bios->data[hwsq_offset + 1];
5864
5865 if (bytes_to_write != 36) {
5866 NV_ERROR(dev, "Unknown HW sequencer entry size\n");
5867 return -EINVAL;
5868 }
5869
5870 NV_TRACE(dev, "Loading NV17 power sequencing microcode\n");
5871
5872 hwsq_entry_offset = hwsq_offset + 2 + entry * bytes_to_write;
5873
5874 /* set sequencer control */
5875 bios_wr32(bios, 0x00001304, ROM32(bios->data[hwsq_entry_offset]));
5876 bytes_to_write -= 4;
5877
5878 /* write ucode */
5879 for (i = 0; i < bytes_to_write; i += 4)
5880 bios_wr32(bios, 0x00001400 + i, ROM32(bios->data[hwsq_entry_offset + i + 4]));
5881
5882 /* twiddle NV_PBUS_DEBUG_4 */
5883 bios_wr32(bios, NV_PBUS_DEBUG_4, bios_rd32(bios, NV_PBUS_DEBUG_4) | 0x18);
5884
5885 return 0;
5886}
5887
5888static int load_nv17_hw_sequencer_ucode(struct drm_device *dev,
5889 struct nvbios *bios)
5890{
5891 /*
5892 * BMP based cards, from NV17, need a microcode loading to correctly
5893 * control the GPIO etc for LVDS panels
5894 *
5895 * BIT based cards seem to do this directly in the init scripts
5896 *
5897 * The microcode entries are found by the "HWSQ" signature.
5898 */
5899
5900 const uint8_t hwsq_signature[] = { 'H', 'W', 'S', 'Q' };
5901 const int sz = sizeof(hwsq_signature);
5902 int hwsq_offset;
5903
5904 hwsq_offset = findstr(bios->data, bios->length, hwsq_signature, sz);
5905 if (!hwsq_offset)
5906 return 0;
5907
5908 /* always use entry 0? */
5909 return load_nv17_hwsq_ucode_entry(dev, bios, hwsq_offset + sz, 0);
5910}
5911
5912uint8_t *nouveau_bios_embedded_edid(struct drm_device *dev)
5913{
5914 struct drm_nouveau_private *dev_priv = dev->dev_private;
Ben Skeggs04a39c52010-02-24 10:03:05 +10005915 struct nvbios *bios = &dev_priv->vbios;
Ben Skeggs6ee73862009-12-11 19:24:15 +10005916 const uint8_t edid_sig[] = {
5917 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00 };
5918 uint16_t offset = 0;
5919 uint16_t newoffset;
5920 int searchlen = NV_PROM_SIZE;
5921
5922 if (bios->fp.edid)
5923 return bios->fp.edid;
5924
5925 while (searchlen) {
5926 newoffset = findstr(&bios->data[offset], searchlen,
5927 edid_sig, 8);
5928 if (!newoffset)
5929 return NULL;
5930 offset += newoffset;
5931 if (!nv_cksum(&bios->data[offset], EDID1_LEN))
5932 break;
5933
5934 searchlen -= offset;
5935 offset++;
5936 }
5937
5938 NV_TRACE(dev, "Found EDID in BIOS\n");
5939
5940 return bios->fp.edid = &bios->data[offset];
5941}
5942
5943void
5944nouveau_bios_run_init_table(struct drm_device *dev, uint16_t table,
5945 struct dcb_entry *dcbent)
5946{
5947 struct drm_nouveau_private *dev_priv = dev->dev_private;
Ben Skeggs04a39c52010-02-24 10:03:05 +10005948 struct nvbios *bios = &dev_priv->vbios;
Ben Skeggs6ee73862009-12-11 19:24:15 +10005949 struct init_exec iexec = { true, false };
5950
Ben Skeggsd9184fa2010-02-16 11:14:14 +10005951 mutex_lock(&bios->lock);
Ben Skeggs6ee73862009-12-11 19:24:15 +10005952 bios->display.output = dcbent;
5953 parse_init_table(bios, table, &iexec);
5954 bios->display.output = NULL;
Ben Skeggsd9184fa2010-02-16 11:14:14 +10005955 mutex_unlock(&bios->lock);
Ben Skeggs6ee73862009-12-11 19:24:15 +10005956}
5957
5958static bool NVInitVBIOS(struct drm_device *dev)
5959{
5960 struct drm_nouveau_private *dev_priv = dev->dev_private;
Ben Skeggs04a39c52010-02-24 10:03:05 +10005961 struct nvbios *bios = &dev_priv->vbios;
Ben Skeggs6ee73862009-12-11 19:24:15 +10005962
5963 memset(bios, 0, sizeof(struct nvbios));
Ben Skeggsd9184fa2010-02-16 11:14:14 +10005964 mutex_init(&bios->lock);
Ben Skeggs6ee73862009-12-11 19:24:15 +10005965 bios->dev = dev;
5966
5967 if (!NVShadowVBIOS(dev, bios->data))
5968 return false;
5969
5970 bios->length = NV_PROM_SIZE;
5971 return true;
5972}
5973
5974static int nouveau_parse_vbios_struct(struct drm_device *dev)
5975{
5976 struct drm_nouveau_private *dev_priv = dev->dev_private;
Ben Skeggs04a39c52010-02-24 10:03:05 +10005977 struct nvbios *bios = &dev_priv->vbios;
Ben Skeggs6ee73862009-12-11 19:24:15 +10005978 const uint8_t bit_signature[] = { 0xff, 0xb8, 'B', 'I', 'T' };
5979 const uint8_t bmp_signature[] = { 0xff, 0x7f, 'N', 'V', 0x0 };
5980 int offset;
5981
5982 offset = findstr(bios->data, bios->length,
5983 bit_signature, sizeof(bit_signature));
5984 if (offset) {
5985 NV_TRACE(dev, "BIT BIOS found\n");
5986 return parse_bit_structure(bios, offset + 6);
5987 }
5988
5989 offset = findstr(bios->data, bios->length,
5990 bmp_signature, sizeof(bmp_signature));
5991 if (offset) {
5992 NV_TRACE(dev, "BMP BIOS found\n");
5993 return parse_bmp_structure(dev, bios, offset);
5994 }
5995
5996 NV_ERROR(dev, "No known BIOS signature found\n");
5997 return -ENODEV;
5998}
5999
6000int
6001nouveau_run_vbios_init(struct drm_device *dev)
6002{
6003 struct drm_nouveau_private *dev_priv = dev->dev_private;
Ben Skeggs04a39c52010-02-24 10:03:05 +10006004 struct nvbios *bios = &dev_priv->vbios;
Ben Skeggs6ee73862009-12-11 19:24:15 +10006005 int i, ret = 0;
6006
6007 NVLockVgaCrtcs(dev, false);
6008 if (nv_two_heads(dev))
6009 NVSetOwner(dev, bios->state.crtchead);
6010
6011 if (bios->major_version < 5) /* BMP only */
6012 load_nv17_hw_sequencer_ucode(dev, bios);
6013
6014 if (bios->execute) {
6015 bios->fp.last_script_invoc = 0;
6016 bios->fp.lvds_init_run = false;
6017 }
6018
6019 parse_init_tables(bios);
6020
6021 /*
6022 * Runs some additional script seen on G8x VBIOSen. The VBIOS'
6023 * parser will run this right after the init tables, the binary
6024 * driver appears to run it at some point later.
6025 */
6026 if (bios->some_script_ptr) {
6027 struct init_exec iexec = {true, false};
6028
6029 NV_INFO(dev, "Parsing VBIOS init table at offset 0x%04X\n",
6030 bios->some_script_ptr);
6031 parse_init_table(bios, bios->some_script_ptr, &iexec);
6032 }
6033
6034 if (dev_priv->card_type >= NV_50) {
Ben Skeggs7f245b22010-02-24 09:56:18 +10006035 for (i = 0; i < bios->dcb.entries; i++) {
Ben Skeggs6ee73862009-12-11 19:24:15 +10006036 nouveau_bios_run_display_table(dev,
Ben Skeggs7f245b22010-02-24 09:56:18 +10006037 &bios->dcb.entry[i],
Ben Skeggs6ee73862009-12-11 19:24:15 +10006038 0, 0);
6039 }
6040 }
6041
6042 NVLockVgaCrtcs(dev, true);
6043
6044 return ret;
6045}
6046
6047static void
6048nouveau_bios_i2c_devices_takedown(struct drm_device *dev)
6049{
6050 struct drm_nouveau_private *dev_priv = dev->dev_private;
Ben Skeggs04a39c52010-02-24 10:03:05 +10006051 struct nvbios *bios = &dev_priv->vbios;
Ben Skeggs6ee73862009-12-11 19:24:15 +10006052 struct dcb_i2c_entry *entry;
6053 int i;
6054
Ben Skeggs7f245b22010-02-24 09:56:18 +10006055 entry = &bios->dcb.i2c[0];
Ben Skeggs6ee73862009-12-11 19:24:15 +10006056 for (i = 0; i < DCB_MAX_NUM_I2C_ENTRIES; i++, entry++)
6057 nouveau_i2c_fini(dev, entry);
6058}
6059
6060int
6061nouveau_bios_init(struct drm_device *dev)
6062{
6063 struct drm_nouveau_private *dev_priv = dev->dev_private;
Ben Skeggs04a39c52010-02-24 10:03:05 +10006064 struct nvbios *bios = &dev_priv->vbios;
Ben Skeggs6ee73862009-12-11 19:24:15 +10006065 uint32_t saved_nv_pextdev_boot_0;
6066 bool was_locked;
6067 int ret;
6068
Ben Skeggs6ee73862009-12-11 19:24:15 +10006069 if (!NVInitVBIOS(dev))
6070 return -ENODEV;
6071
6072 ret = nouveau_parse_vbios_struct(dev);
6073 if (ret)
6074 return ret;
6075
6076 ret = parse_dcb_table(dev, bios, nv_two_heads(dev));
6077 if (ret)
6078 return ret;
6079
6080 fixup_legacy_i2c(bios);
6081 fixup_legacy_connector(bios);
6082
6083 if (!bios->major_version) /* we don't run version 0 bios */
6084 return 0;
6085
6086 /* these will need remembering across a suspend */
6087 saved_nv_pextdev_boot_0 = bios_rd32(bios, NV_PEXTDEV_BOOT_0);
6088 bios->state.saved_nv_pfb_cfg0 = bios_rd32(bios, NV_PFB_CFG0);
6089
6090 /* init script execution disabled */
6091 bios->execute = false;
6092
6093 /* ... unless card isn't POSTed already */
6094 if (dev_priv->card_type >= NV_10 &&
6095 NVReadVgaCrtc(dev, 0, 0x00) == 0 &&
6096 NVReadVgaCrtc(dev, 0, 0x1a) == 0) {
6097 NV_INFO(dev, "Adaptor not initialised\n");
6098 if (dev_priv->card_type < NV_50) {
6099 NV_ERROR(dev, "Unable to POST this chipset\n");
6100 return -ENODEV;
6101 }
6102
6103 NV_INFO(dev, "Running VBIOS init tables\n");
6104 bios->execute = true;
6105 }
6106
6107 bios_wr32(bios, NV_PEXTDEV_BOOT_0, saved_nv_pextdev_boot_0);
6108
6109 ret = nouveau_run_vbios_init(dev);
Ben Skeggs04a39c52010-02-24 10:03:05 +10006110 if (ret)
Ben Skeggs6ee73862009-12-11 19:24:15 +10006111 return ret;
Ben Skeggs6ee73862009-12-11 19:24:15 +10006112
6113 /* feature_byte on BMP is poor, but init always sets CR4B */
6114 was_locked = NVLockVgaCrtcs(dev, false);
6115 if (bios->major_version < 5)
6116 bios->is_mobile = NVReadVgaCrtc(dev, 0, NV_CIO_CRE_4B) & 0x40;
6117
6118 /* all BIT systems need p_f_m_t for digital_min_front_porch */
6119 if (bios->is_mobile || bios->major_version >= 5)
6120 ret = parse_fp_mode_table(dev, bios);
6121 NVLockVgaCrtcs(dev, was_locked);
6122
6123 /* allow subsequent scripts to execute */
6124 bios->execute = true;
6125
6126 return 0;
6127}
6128
6129void
6130nouveau_bios_takedown(struct drm_device *dev)
6131{
6132 nouveau_bios_i2c_devices_takedown(dev);
6133}