blob: bea5df7cf93c32279e3ab9fa4d6c94dbf6a9d575 [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"
Ben Skeggs25908b72010-04-20 02:28:37 +100029#include "nouveau_encoder.h"
Ben Skeggs6ee73862009-12-11 19:24:15 +100030
Francisco Jerez67eda202010-07-13 15:59:50 +020031#include <linux/io-mapping.h>
32
Ben Skeggs6ee73862009-12-11 19:24:15 +100033/* these defines are made up */
34#define NV_CIO_CRE_44_HEADA 0x0
35#define NV_CIO_CRE_44_HEADB 0x3
36#define FEATURE_MOBILE 0x10 /* also FEATURE_QUADRO for BMP */
37#define LEGACY_I2C_CRT 0x80
38#define LEGACY_I2C_PANEL 0x81
39#define LEGACY_I2C_TV 0x82
40
41#define EDID1_LEN 128
42
43#define BIOSLOG(sip, fmt, arg...) NV_DEBUG(sip->dev, fmt, ##arg)
44#define LOG_OLD_VALUE(x)
45
Ben Skeggs6ee73862009-12-11 19:24:15 +100046struct 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) {
Ben Skeggs96177572011-06-24 08:18:23 +1000138 u64 addr = (u64)(nv_rd32(dev, 0x619f04) & 0xffffff00) << 8;
139 if (!addr) {
140 addr = (u64)nv_rd32(dev, 0x1700) << 16;
141 addr += 0xf0000;
142 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000143
144 old_bar0_pramin = nv_rd32(dev, 0x1700);
Ben Skeggs96177572011-06-24 08:18:23 +1000145 nv_wr32(dev, 0x1700, addr >> 16);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000146 }
147
148 /* bail if no rom signature */
149 if (nv_rd08(dev, NV_PRAMIN_OFFSET) != 0x55 ||
150 nv_rd08(dev, NV_PRAMIN_OFFSET + 1) != 0xaa)
151 goto out;
152
153 for (i = 0; i < NV_PROM_SIZE; i++)
154 data[i] = nv_rd08(dev, NV_PRAMIN_OFFSET + i);
155
156out:
157 if (dev_priv->card_type >= NV_50)
158 nv_wr32(dev, 0x1700, old_bar0_pramin);
159}
160
161static void load_vbios_pci(struct drm_device *dev, uint8_t *data)
162{
163 void __iomem *rom = NULL;
164 size_t rom_len;
165 int ret;
166
167 ret = pci_enable_rom(dev->pdev);
168 if (ret)
169 return;
170
171 rom = pci_map_rom(dev->pdev, &rom_len);
172 if (!rom)
173 goto out;
174 memcpy_fromio(data, rom, rom_len);
175 pci_unmap_rom(dev->pdev, rom);
176
177out:
178 pci_disable_rom(dev->pdev);
179}
180
Dave Airlieafeb3e12010-04-07 13:55:09 +1000181static void load_vbios_acpi(struct drm_device *dev, uint8_t *data)
182{
183 int i;
184 int ret;
185 int size = 64 * 1024;
186
187 if (!nouveau_acpi_rom_supported(dev->pdev))
188 return;
189
190 for (i = 0; i < (size / ROM_BIOS_PAGE); i++) {
191 ret = nouveau_acpi_get_bios_chunk(data,
192 (i * ROM_BIOS_PAGE),
193 ROM_BIOS_PAGE);
194 if (ret <= 0)
195 break;
196 }
197 return;
198}
199
Ben Skeggs6ee73862009-12-11 19:24:15 +1000200struct methods {
201 const char desc[8];
202 void (*loadbios)(struct drm_device *, uint8_t *);
203 const bool rw;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000204};
205
Ben Skeggs41090eb2010-07-12 13:15:44 +1000206static struct methods shadow_methods[] = {
Ben Skeggs6ee73862009-12-11 19:24:15 +1000207 { "PRAMIN", load_vbios_pramin, true },
Ben Skeggs41090eb2010-07-12 13:15:44 +1000208 { "PROM", load_vbios_prom, false },
Ben Skeggs6ee73862009-12-11 19:24:15 +1000209 { "PCIROM", load_vbios_pci, true },
Dave Airlieafeb3e12010-04-07 13:55:09 +1000210 { "ACPI", load_vbios_acpi, true },
Ben Skeggs6ee73862009-12-11 19:24:15 +1000211};
Francisco Jerezeae61922010-07-13 16:16:26 +0200212#define NUM_SHADOW_METHODS ARRAY_SIZE(shadow_methods)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000213
214static bool NVShadowVBIOS(struct drm_device *dev, uint8_t *data)
215{
Ben Skeggs41090eb2010-07-12 13:15:44 +1000216 struct methods *methods = shadow_methods;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000217 int testscore = 3;
Francisco Jerezeae61922010-07-13 16:16:26 +0200218 int scores[NUM_SHADOW_METHODS], i;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000219
220 if (nouveau_vbios) {
Francisco Jerezeae61922010-07-13 16:16:26 +0200221 for (i = 0; i < NUM_SHADOW_METHODS; i++)
Marcin Koƛcielnicki657b6242009-12-15 00:37:30 +0000222 if (!strcasecmp(nouveau_vbios, methods[i].desc))
Ben Skeggs6ee73862009-12-11 19:24:15 +1000223 break;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000224
Francisco Jerezeae61922010-07-13 16:16:26 +0200225 if (i < NUM_SHADOW_METHODS) {
Ben Skeggs6ee73862009-12-11 19:24:15 +1000226 NV_INFO(dev, "Attempting to use BIOS image from %s\n",
Marcin Koƛcielnicki657b6242009-12-15 00:37:30 +0000227 methods[i].desc);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000228
Marcin Koƛcielnicki657b6242009-12-15 00:37:30 +0000229 methods[i].loadbios(dev, data);
230 if (score_vbios(dev, data, methods[i].rw))
Ben Skeggs6ee73862009-12-11 19:24:15 +1000231 return true;
232 }
233
234 NV_ERROR(dev, "VBIOS source \'%s\' invalid\n", nouveau_vbios);
235 }
236
Francisco Jerezeae61922010-07-13 16:16:26 +0200237 for (i = 0; i < NUM_SHADOW_METHODS; i++) {
Ben Skeggs6ee73862009-12-11 19:24:15 +1000238 NV_TRACE(dev, "Attempting to load BIOS image from %s\n",
Marcin Koƛcielnicki657b6242009-12-15 00:37:30 +0000239 methods[i].desc);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000240 data[0] = data[1] = 0; /* avoid reuse of previous image */
Marcin Koƛcielnicki657b6242009-12-15 00:37:30 +0000241 methods[i].loadbios(dev, data);
242 scores[i] = score_vbios(dev, data, methods[i].rw);
243 if (scores[i] == testscore)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000244 return true;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000245 }
246
247 while (--testscore > 0) {
Francisco Jerezeae61922010-07-13 16:16:26 +0200248 for (i = 0; i < NUM_SHADOW_METHODS; i++) {
Marcin Koƛcielnicki657b6242009-12-15 00:37:30 +0000249 if (scores[i] == testscore) {
Ben Skeggs6ee73862009-12-11 19:24:15 +1000250 NV_TRACE(dev, "Using BIOS image from %s\n",
Marcin Koƛcielnicki657b6242009-12-15 00:37:30 +0000251 methods[i].desc);
252 methods[i].loadbios(dev, data);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000253 return true;
254 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000255 }
256 }
257
258 NV_ERROR(dev, "No valid BIOS image found\n");
259 return false;
260}
261
262struct init_tbl_entry {
263 char *name;
264 uint8_t id;
Ben Skeggs9170a822010-05-10 16:54:23 +1000265 /* Return:
266 * > 0: success, length of opcode
267 * 0: success, but abort further parsing of table (INIT_DONE etc)
268 * < 0: failure, table parsing will be aborted
269 */
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +0000270 int (*handler)(struct nvbios *, uint16_t, struct init_exec *);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000271};
272
Ben Skeggsec64a402011-03-21 21:31:21 +1000273static int parse_init_table(struct nvbios *, uint16_t, struct init_exec *);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000274
275#define MACRO_INDEX_SIZE 2
276#define MACRO_SIZE 8
277#define CONDITION_SIZE 12
278#define IO_FLAG_CONDITION_SIZE 9
279#define IO_CONDITION_SIZE 5
280#define MEM_INIT_SIZE 66
281
282static void still_alive(void)
283{
284#if 0
285 sync();
Ben Skeggsc7ca4d12011-02-03 20:10:49 +1000286 mdelay(2);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000287#endif
288}
289
290static uint32_t
291munge_reg(struct nvbios *bios, uint32_t reg)
292{
293 struct drm_nouveau_private *dev_priv = bios->dev->dev_private;
294 struct dcb_entry *dcbent = bios->display.output;
295
296 if (dev_priv->card_type < NV_50)
297 return reg;
298
Ben Skeggs02e4f582011-07-06 21:21:42 +1000299 if (reg & 0x80000000) {
300 BUG_ON(bios->display.crtc < 0);
301 reg += bios->display.crtc * 0x800;
302 }
303
Ben Skeggs6ee73862009-12-11 19:24:15 +1000304 if (reg & 0x40000000) {
305 BUG_ON(!dcbent);
306
307 reg += (ffs(dcbent->or) - 1) * 0x800;
308 if ((reg & 0x20000000) && !(dcbent->sorconf.link & 1))
309 reg += 0x00000080;
310 }
311
Ben Skeggs02e4f582011-07-06 21:21:42 +1000312 reg &= ~0xe0000000;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000313 return reg;
314}
315
316static int
317valid_reg(struct nvbios *bios, uint32_t reg)
318{
319 struct drm_nouveau_private *dev_priv = bios->dev->dev_private;
320 struct drm_device *dev = bios->dev;
321
322 /* C51 has misaligned regs on purpose. Marvellous */
Ben Skeggs9855e582010-01-12 13:02:19 +1000323 if (reg & 0x2 ||
Ben Skeggs04a39c52010-02-24 10:03:05 +1000324 (reg & 0x1 && dev_priv->vbios.chip_version != 0x51))
Ben Skeggs9855e582010-01-12 13:02:19 +1000325 NV_ERROR(dev, "======= misaligned reg 0x%08X =======\n", reg);
326
327 /* warn on C51 regs that haven't been verified accessible in tracing */
Ben Skeggs04a39c52010-02-24 10:03:05 +1000328 if (reg & 0x1 && dev_priv->vbios.chip_version == 0x51 &&
Ben Skeggs6ee73862009-12-11 19:24:15 +1000329 reg != 0x130d && reg != 0x1311 && reg != 0x60081d)
330 NV_WARN(dev, "=== C51 misaligned reg 0x%08X not verified ===\n",
331 reg);
332
Ben Skeggs9855e582010-01-12 13:02:19 +1000333 if (reg >= (8*1024*1024)) {
334 NV_ERROR(dev, "=== reg 0x%08x out of mapped bounds ===\n", reg);
335 return 0;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000336 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000337
Ben Skeggs9855e582010-01-12 13:02:19 +1000338 return 1;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000339}
340
341static bool
342valid_idx_port(struct nvbios *bios, uint16_t port)
343{
344 struct drm_nouveau_private *dev_priv = bios->dev->dev_private;
345 struct drm_device *dev = bios->dev;
346
347 /*
348 * If adding more ports here, the read/write functions below will need
349 * updating so that the correct mmio range (PRMCIO, PRMDIO, PRMVIO) is
350 * used for the port in question
351 */
352 if (dev_priv->card_type < NV_50) {
353 if (port == NV_CIO_CRX__COLOR)
354 return true;
355 if (port == NV_VIO_SRX)
356 return true;
357 } else {
358 if (port == NV_CIO_CRX__COLOR)
359 return true;
360 }
361
362 NV_ERROR(dev, "========== unknown indexed io port 0x%04X ==========\n",
363 port);
364
365 return false;
366}
367
368static bool
369valid_port(struct nvbios *bios, uint16_t port)
370{
371 struct drm_device *dev = bios->dev;
372
373 /*
374 * If adding more ports here, the read/write functions below will need
375 * updating so that the correct mmio range (PRMCIO, PRMDIO, PRMVIO) is
376 * used for the port in question
377 */
378 if (port == NV_VIO_VSE2)
379 return true;
380
381 NV_ERROR(dev, "========== unknown io port 0x%04X ==========\n", port);
382
383 return false;
384}
385
386static uint32_t
387bios_rd32(struct nvbios *bios, uint32_t reg)
388{
389 uint32_t data;
390
391 reg = munge_reg(bios, reg);
392 if (!valid_reg(bios, reg))
393 return 0;
394
395 /*
396 * C51 sometimes uses regs with bit0 set in the address. For these
397 * cases there should exist a translation in a BIOS table to an IO
398 * port address which the BIOS uses for accessing the reg
399 *
400 * These only seem to appear for the power control regs to a flat panel,
401 * and the GPIO regs at 0x60081*. In C51 mmio traces the normal regs
402 * for 0x1308 and 0x1310 are used - hence the mask below. An S3
403 * suspend-resume mmio trace from a C51 will be required to see if this
404 * is true for the power microcode in 0x14.., or whether the direct IO
405 * port access method is needed
406 */
407 if (reg & 0x1)
408 reg &= ~0x1;
409
410 data = nv_rd32(bios->dev, reg);
411
412 BIOSLOG(bios, " Read: Reg: 0x%08X, Data: 0x%08X\n", reg, data);
413
414 return data;
415}
416
417static void
418bios_wr32(struct nvbios *bios, uint32_t reg, uint32_t data)
419{
420 struct drm_nouveau_private *dev_priv = bios->dev->dev_private;
421
422 reg = munge_reg(bios, reg);
423 if (!valid_reg(bios, reg))
424 return;
425
426 /* see note in bios_rd32 */
427 if (reg & 0x1)
428 reg &= 0xfffffffe;
429
430 LOG_OLD_VALUE(bios_rd32(bios, reg));
431 BIOSLOG(bios, " Write: Reg: 0x%08X, Data: 0x%08X\n", reg, data);
432
Ben Skeggs04a39c52010-02-24 10:03:05 +1000433 if (dev_priv->vbios.execute) {
Ben Skeggs6ee73862009-12-11 19:24:15 +1000434 still_alive();
435 nv_wr32(bios->dev, reg, data);
436 }
437}
438
439static uint8_t
440bios_idxprt_rd(struct nvbios *bios, uint16_t port, uint8_t index)
441{
442 struct drm_nouveau_private *dev_priv = bios->dev->dev_private;
443 struct drm_device *dev = bios->dev;
444 uint8_t data;
445
446 if (!valid_idx_port(bios, port))
447 return 0;
448
449 if (dev_priv->card_type < NV_50) {
450 if (port == NV_VIO_SRX)
451 data = NVReadVgaSeq(dev, bios->state.crtchead, index);
452 else /* assume NV_CIO_CRX__COLOR */
453 data = NVReadVgaCrtc(dev, bios->state.crtchead, index);
454 } else {
455 uint32_t data32;
456
457 data32 = bios_rd32(bios, NV50_PDISPLAY_VGACRTC(index & ~3));
458 data = (data32 >> ((index & 3) << 3)) & 0xff;
459 }
460
461 BIOSLOG(bios, " Indexed IO read: Port: 0x%04X, Index: 0x%02X, "
462 "Head: 0x%02X, Data: 0x%02X\n",
463 port, index, bios->state.crtchead, data);
464 return data;
465}
466
467static void
468bios_idxprt_wr(struct nvbios *bios, uint16_t port, uint8_t index, uint8_t data)
469{
470 struct drm_nouveau_private *dev_priv = bios->dev->dev_private;
471 struct drm_device *dev = bios->dev;
472
473 if (!valid_idx_port(bios, port))
474 return;
475
476 /*
477 * The current head is maintained in the nvbios member state.crtchead.
478 * We trap changes to CR44 and update the head variable and hence the
479 * register set written.
480 * As CR44 only exists on CRTC0, we update crtchead to head0 in advance
481 * of the write, and to head1 after the write
482 */
483 if (port == NV_CIO_CRX__COLOR && index == NV_CIO_CRE_44 &&
484 data != NV_CIO_CRE_44_HEADB)
485 bios->state.crtchead = 0;
486
487 LOG_OLD_VALUE(bios_idxprt_rd(bios, port, index));
488 BIOSLOG(bios, " Indexed IO write: Port: 0x%04X, Index: 0x%02X, "
489 "Head: 0x%02X, Data: 0x%02X\n",
490 port, index, bios->state.crtchead, data);
491
492 if (bios->execute && dev_priv->card_type < NV_50) {
493 still_alive();
494 if (port == NV_VIO_SRX)
495 NVWriteVgaSeq(dev, bios->state.crtchead, index, data);
496 else /* assume NV_CIO_CRX__COLOR */
497 NVWriteVgaCrtc(dev, bios->state.crtchead, index, data);
498 } else
499 if (bios->execute) {
500 uint32_t data32, shift = (index & 3) << 3;
501
502 still_alive();
503
504 data32 = bios_rd32(bios, NV50_PDISPLAY_VGACRTC(index & ~3));
505 data32 &= ~(0xff << shift);
506 data32 |= (data << shift);
507 bios_wr32(bios, NV50_PDISPLAY_VGACRTC(index & ~3), data32);
508 }
509
510 if (port == NV_CIO_CRX__COLOR &&
511 index == NV_CIO_CRE_44 && data == NV_CIO_CRE_44_HEADB)
512 bios->state.crtchead = 1;
513}
514
515static uint8_t
516bios_port_rd(struct nvbios *bios, uint16_t port)
517{
518 uint8_t data, head = bios->state.crtchead;
519
520 if (!valid_port(bios, port))
521 return 0;
522
523 data = NVReadPRMVIO(bios->dev, head, NV_PRMVIO0_OFFSET + port);
524
525 BIOSLOG(bios, " IO read: Port: 0x%04X, Head: 0x%02X, Data: 0x%02X\n",
526 port, head, data);
527
528 return data;
529}
530
531static void
532bios_port_wr(struct nvbios *bios, uint16_t port, uint8_t data)
533{
534 int head = bios->state.crtchead;
535
536 if (!valid_port(bios, port))
537 return;
538
539 LOG_OLD_VALUE(bios_port_rd(bios, port));
540 BIOSLOG(bios, " IO write: Port: 0x%04X, Head: 0x%02X, Data: 0x%02X\n",
541 port, head, data);
542
543 if (!bios->execute)
544 return;
545
546 still_alive();
547 NVWritePRMVIO(bios->dev, head, NV_PRMVIO0_OFFSET + port, data);
548}
549
550static bool
551io_flag_condition_met(struct nvbios *bios, uint16_t offset, uint8_t cond)
552{
553 /*
554 * The IO flag condition entry has 2 bytes for the CRTC port; 1 byte
555 * for the CRTC index; 1 byte for the mask to apply to the value
556 * retrieved from the CRTC; 1 byte for the shift right to apply to the
557 * masked CRTC value; 2 bytes for the offset to the flag array, to
558 * which the shifted value is added; 1 byte for the mask applied to the
559 * value read from the flag array; and 1 byte for the value to compare
560 * against the masked byte from the flag table.
561 */
562
563 uint16_t condptr = bios->io_flag_condition_tbl_ptr + cond * IO_FLAG_CONDITION_SIZE;
564 uint16_t crtcport = ROM16(bios->data[condptr]);
565 uint8_t crtcindex = bios->data[condptr + 2];
566 uint8_t mask = bios->data[condptr + 3];
567 uint8_t shift = bios->data[condptr + 4];
568 uint16_t flagarray = ROM16(bios->data[condptr + 5]);
569 uint8_t flagarraymask = bios->data[condptr + 7];
570 uint8_t cmpval = bios->data[condptr + 8];
571 uint8_t data;
572
573 BIOSLOG(bios, "0x%04X: Port: 0x%04X, Index: 0x%02X, Mask: 0x%02X, "
574 "Shift: 0x%02X, FlagArray: 0x%04X, FAMask: 0x%02X, "
575 "Cmpval: 0x%02X\n",
576 offset, crtcport, crtcindex, mask, shift, flagarray, flagarraymask, cmpval);
577
578 data = bios_idxprt_rd(bios, crtcport, crtcindex);
579
580 data = bios->data[flagarray + ((data & mask) >> shift)];
581 data &= flagarraymask;
582
583 BIOSLOG(bios, "0x%04X: Checking if 0x%02X equals 0x%02X\n",
584 offset, data, cmpval);
585
586 return (data == cmpval);
587}
588
589static bool
590bios_condition_met(struct nvbios *bios, uint16_t offset, uint8_t cond)
591{
592 /*
593 * The condition table entry has 4 bytes for the address of the
594 * register to check, 4 bytes for a mask to apply to the register and
595 * 4 for a test comparison value
596 */
597
598 uint16_t condptr = bios->condition_tbl_ptr + cond * CONDITION_SIZE;
599 uint32_t reg = ROM32(bios->data[condptr]);
600 uint32_t mask = ROM32(bios->data[condptr + 4]);
601 uint32_t cmpval = ROM32(bios->data[condptr + 8]);
602 uint32_t data;
603
604 BIOSLOG(bios, "0x%04X: Cond: 0x%02X, Reg: 0x%08X, Mask: 0x%08X\n",
605 offset, cond, reg, mask);
606
607 data = bios_rd32(bios, reg) & mask;
608
609 BIOSLOG(bios, "0x%04X: Checking if 0x%08X equals 0x%08X\n",
610 offset, data, cmpval);
611
612 return (data == cmpval);
613}
614
615static bool
616io_condition_met(struct nvbios *bios, uint16_t offset, uint8_t cond)
617{
618 /*
619 * The IO condition entry has 2 bytes for the IO port address; 1 byte
620 * for the index to write to io_port; 1 byte for the mask to apply to
621 * the byte read from io_port+1; and 1 byte for the value to compare
622 * against the masked byte.
623 */
624
625 uint16_t condptr = bios->io_condition_tbl_ptr + cond * IO_CONDITION_SIZE;
626 uint16_t io_port = ROM16(bios->data[condptr]);
627 uint8_t port_index = bios->data[condptr + 2];
628 uint8_t mask = bios->data[condptr + 3];
629 uint8_t cmpval = bios->data[condptr + 4];
630
631 uint8_t data = bios_idxprt_rd(bios, io_port, port_index) & mask;
632
633 BIOSLOG(bios, "0x%04X: Checking if 0x%02X equals 0x%02X\n",
634 offset, data, cmpval);
635
636 return (data == cmpval);
637}
638
639static int
640nv50_pll_set(struct drm_device *dev, uint32_t reg, uint32_t clk)
641{
642 struct drm_nouveau_private *dev_priv = dev->dev_private;
643 uint32_t reg0 = nv_rd32(dev, reg + 0);
644 uint32_t reg1 = nv_rd32(dev, reg + 4);
645 struct nouveau_pll_vals pll;
646 struct pll_lims pll_limits;
647 int ret;
648
649 ret = get_pll_limits(dev, reg, &pll_limits);
650 if (ret)
651 return ret;
652
653 clk = nouveau_calc_pll_mnp(dev, &pll_limits, clk, &pll);
654 if (!clk)
655 return -ERANGE;
656
657 reg0 = (reg0 & 0xfff8ffff) | (pll.log2P << 16);
658 reg1 = (reg1 & 0xffff0000) | (pll.N1 << 8) | pll.M1;
659
Ben Skeggs04a39c52010-02-24 10:03:05 +1000660 if (dev_priv->vbios.execute) {
Ben Skeggs6ee73862009-12-11 19:24:15 +1000661 still_alive();
662 nv_wr32(dev, reg + 4, reg1);
663 nv_wr32(dev, reg + 0, reg0);
664 }
665
666 return 0;
667}
668
669static int
670setPLL(struct nvbios *bios, uint32_t reg, uint32_t clk)
671{
672 struct drm_device *dev = bios->dev;
673 struct drm_nouveau_private *dev_priv = dev->dev_private;
674 /* clk in kHz */
675 struct pll_lims pll_lim;
676 struct nouveau_pll_vals pllvals;
677 int ret;
678
679 if (dev_priv->card_type >= NV_50)
680 return nv50_pll_set(dev, reg, clk);
681
682 /* high regs (such as in the mac g5 table) are not -= 4 */
683 ret = get_pll_limits(dev, reg > 0x405c ? reg : reg - 4, &pll_lim);
684 if (ret)
685 return ret;
686
687 clk = nouveau_calc_pll_mnp(dev, &pll_lim, clk, &pllvals);
688 if (!clk)
689 return -ERANGE;
690
691 if (bios->execute) {
692 still_alive();
693 nouveau_hw_setpll(dev, reg, &pllvals);
694 }
695
696 return 0;
697}
698
699static int dcb_entry_idx_from_crtchead(struct drm_device *dev)
700{
701 struct drm_nouveau_private *dev_priv = dev->dev_private;
Ben Skeggs04a39c52010-02-24 10:03:05 +1000702 struct nvbios *bios = &dev_priv->vbios;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000703
704 /*
705 * For the results of this function to be correct, CR44 must have been
706 * set (using bios_idxprt_wr to set crtchead), CR58 set for CR57 = 0,
707 * and the DCB table parsed, before the script calling the function is
708 * run. run_digital_op_script is example of how to do such setup
709 */
710
711 uint8_t dcb_entry = NVReadVgaCrtc5758(dev, bios->state.crtchead, 0);
712
Ben Skeggs7f245b22010-02-24 09:56:18 +1000713 if (dcb_entry > bios->dcb.entries) {
Ben Skeggs6ee73862009-12-11 19:24:15 +1000714 NV_ERROR(dev, "CR58 doesn't have a valid DCB entry currently "
715 "(%02X)\n", dcb_entry);
716 dcb_entry = 0x7f; /* unused / invalid marker */
717 }
718
719 return dcb_entry;
720}
721
Ben Skeggsf8b0be12010-05-12 14:38:25 +1000722static int
723read_dcb_i2c_entry(struct drm_device *dev, int dcb_version, uint8_t *i2ctable, int index, struct dcb_i2c_entry *i2c)
724{
725 uint8_t dcb_i2c_ver = dcb_version, headerlen = 0, entry_len = 4;
726 int i2c_entries = DCB_MAX_NUM_I2C_ENTRIES;
727 int recordoffset = 0, rdofs = 1, wrofs = 0;
728 uint8_t port_type = 0;
729
730 if (!i2ctable)
731 return -EINVAL;
732
733 if (dcb_version >= 0x30) {
734 if (i2ctable[0] != dcb_version) /* necessary? */
735 NV_WARN(dev,
736 "DCB I2C table version mismatch (%02X vs %02X)\n",
737 i2ctable[0], dcb_version);
738 dcb_i2c_ver = i2ctable[0];
739 headerlen = i2ctable[1];
740 if (i2ctable[2] <= DCB_MAX_NUM_I2C_ENTRIES)
741 i2c_entries = i2ctable[2];
742 else
743 NV_WARN(dev,
744 "DCB I2C table has more entries than indexable "
745 "(%d entries, max %d)\n", i2ctable[2],
746 DCB_MAX_NUM_I2C_ENTRIES);
747 entry_len = i2ctable[3];
748 /* [4] is i2c_default_indices, read in parse_dcb_table() */
749 }
750 /*
751 * It's your own fault if you call this function on a DCB 1.1 BIOS --
752 * the test below is for DCB 1.2
753 */
754 if (dcb_version < 0x14) {
755 recordoffset = 2;
756 rdofs = 0;
757 wrofs = 1;
758 }
759
760 if (index == 0xf)
761 return 0;
762 if (index >= i2c_entries) {
763 NV_ERROR(dev, "DCB I2C index too big (%d >= %d)\n",
764 index, i2ctable[2]);
765 return -ENOENT;
766 }
767 if (i2ctable[headerlen + entry_len * index + 3] == 0xff) {
768 NV_ERROR(dev, "DCB I2C entry invalid\n");
769 return -EINVAL;
770 }
771
772 if (dcb_i2c_ver >= 0x30) {
773 port_type = i2ctable[headerlen + recordoffset + 3 + entry_len * index];
774
775 /*
776 * Fixup for chips using same address offset for read and
777 * write.
778 */
779 if (port_type == 4) /* seen on C51 */
780 rdofs = wrofs = 1;
781 if (port_type >= 5) /* G80+ */
782 rdofs = wrofs = 0;
783 }
784
785 if (dcb_i2c_ver >= 0x40) {
786 if (port_type != 5 && port_type != 6)
787 NV_WARN(dev, "DCB I2C table has port type %d\n", port_type);
788
789 i2c->entry = ROM32(i2ctable[headerlen + recordoffset + entry_len * index]);
790 }
791
792 i2c->port_type = port_type;
793 i2c->read = i2ctable[headerlen + recordoffset + rdofs + entry_len * index];
794 i2c->write = i2ctable[headerlen + recordoffset + wrofs + entry_len * index];
795
796 return 0;
797}
798
Ben Skeggs6ee73862009-12-11 19:24:15 +1000799static struct nouveau_i2c_chan *
800init_i2c_device_find(struct drm_device *dev, int i2c_index)
801{
802 struct drm_nouveau_private *dev_priv = dev->dev_private;
Ben Skeggs04a39c52010-02-24 10:03:05 +1000803 struct dcb_table *dcb = &dev_priv->vbios.dcb;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000804
805 if (i2c_index == 0xff) {
806 /* note: dcb_entry_idx_from_crtchead needs pre-script set-up */
807 int idx = dcb_entry_idx_from_crtchead(dev), shift = 0;
Ben Skeggs7f245b22010-02-24 09:56:18 +1000808 int default_indices = dcb->i2c_default_indices;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000809
Ben Skeggs7f245b22010-02-24 09:56:18 +1000810 if (idx != 0x7f && dcb->entry[idx].i2c_upper_default)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000811 shift = 4;
812
813 i2c_index = (default_indices >> shift) & 0xf;
814 }
815 if (i2c_index == 0x80) /* g80+ */
Ben Skeggs7f245b22010-02-24 09:56:18 +1000816 i2c_index = dcb->i2c_default_indices & 0xf;
Ben Skeggs04f542c2010-05-12 14:45:04 +1000817 else
818 if (i2c_index == 0x81)
819 i2c_index = (dcb->i2c_default_indices & 0xf0) >> 4;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000820
Dan Carpenter75047942010-05-25 11:52:27 +0200821 if (i2c_index >= DCB_MAX_NUM_I2C_ENTRIES) {
Ben Skeggsf8b0be12010-05-12 14:38:25 +1000822 NV_ERROR(dev, "invalid i2c_index 0x%x\n", i2c_index);
823 return NULL;
824 }
825
826 /* Make sure i2c table entry has been parsed, it may not
827 * have been if this is a bus not referenced by a DCB encoder
828 */
829 read_dcb_i2c_entry(dev, dcb->version, dcb->i2c_table,
830 i2c_index, &dcb->i2c[i2c_index]);
831
Ben Skeggs6ee73862009-12-11 19:24:15 +1000832 return nouveau_i2c_find(dev, i2c_index);
833}
834
Ben Skeggs7f245b22010-02-24 09:56:18 +1000835static uint32_t
836get_tmds_index_reg(struct drm_device *dev, uint8_t mlv)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000837{
838 /*
839 * For mlv < 0x80, it is an index into a table of TMDS base addresses.
840 * For mlv == 0x80 use the "or" value of the dcb_entry indexed by
841 * CR58 for CR57 = 0 to index a table of offsets to the basic
842 * 0x6808b0 address.
843 * For mlv == 0x81 use the "or" value of the dcb_entry indexed by
844 * CR58 for CR57 = 0 to index a table of offsets to the basic
845 * 0x6808b0 address, and then flip the offset by 8.
846 */
847
848 struct drm_nouveau_private *dev_priv = dev->dev_private;
Ben Skeggs04a39c52010-02-24 10:03:05 +1000849 struct nvbios *bios = &dev_priv->vbios;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000850 const int pramdac_offset[13] = {
851 0, 0, 0x8, 0, 0x2000, 0, 0, 0, 0x2008, 0, 0, 0, 0x2000 };
852 const uint32_t pramdac_table[4] = {
853 0x6808b0, 0x6808b8, 0x6828b0, 0x6828b8 };
854
855 if (mlv >= 0x80) {
856 int dcb_entry, dacoffset;
857
858 /* note: dcb_entry_idx_from_crtchead needs pre-script set-up */
859 dcb_entry = dcb_entry_idx_from_crtchead(dev);
860 if (dcb_entry == 0x7f)
861 return 0;
Ben Skeggs7f245b22010-02-24 09:56:18 +1000862 dacoffset = pramdac_offset[bios->dcb.entry[dcb_entry].or];
Ben Skeggs6ee73862009-12-11 19:24:15 +1000863 if (mlv == 0x81)
864 dacoffset ^= 8;
865 return 0x6808b0 + dacoffset;
866 } else {
Marcin Slusarzdf31ef42010-02-17 19:04:00 +0100867 if (mlv >= ARRAY_SIZE(pramdac_table)) {
Ben Skeggs6ee73862009-12-11 19:24:15 +1000868 NV_ERROR(dev, "Magic Lookup Value too big (%02X)\n",
869 mlv);
870 return 0;
871 }
872 return pramdac_table[mlv];
873 }
874}
875
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +0000876static int
Ben Skeggs6ee73862009-12-11 19:24:15 +1000877init_io_restrict_prog(struct nvbios *bios, uint16_t offset,
878 struct init_exec *iexec)
879{
880 /*
881 * INIT_IO_RESTRICT_PROG opcode: 0x32 ('2')
882 *
883 * offset (8 bit): opcode
884 * offset + 1 (16 bit): CRTC port
885 * offset + 3 (8 bit): CRTC index
886 * offset + 4 (8 bit): mask
887 * offset + 5 (8 bit): shift
888 * offset + 6 (8 bit): count
889 * offset + 7 (32 bit): register
890 * offset + 11 (32 bit): configuration 1
891 * ...
892 *
893 * Starting at offset + 11 there are "count" 32 bit values.
894 * To find out which value to use read index "CRTC index" on "CRTC
895 * port", AND this value with "mask" and then bit shift right "shift"
896 * bits. Read the appropriate value using this index and write to
897 * "register"
898 */
899
900 uint16_t crtcport = ROM16(bios->data[offset + 1]);
901 uint8_t crtcindex = bios->data[offset + 3];
902 uint8_t mask = bios->data[offset + 4];
903 uint8_t shift = bios->data[offset + 5];
904 uint8_t count = bios->data[offset + 6];
905 uint32_t reg = ROM32(bios->data[offset + 7]);
906 uint8_t config;
907 uint32_t configval;
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +0000908 int len = 11 + count * 4;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000909
910 if (!iexec->execute)
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +0000911 return len;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000912
913 BIOSLOG(bios, "0x%04X: Port: 0x%04X, Index: 0x%02X, Mask: 0x%02X, "
914 "Shift: 0x%02X, Count: 0x%02X, Reg: 0x%08X\n",
915 offset, crtcport, crtcindex, mask, shift, count, reg);
916
917 config = (bios_idxprt_rd(bios, crtcport, crtcindex) & mask) >> shift;
918 if (config > count) {
919 NV_ERROR(bios->dev,
920 "0x%04X: Config 0x%02X exceeds maximal bound 0x%02X\n",
921 offset, config, count);
Ben Skeggs309b8c82010-06-29 16:09:24 +1000922 return len;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000923 }
924
925 configval = ROM32(bios->data[offset + 11 + config * 4]);
926
927 BIOSLOG(bios, "0x%04X: Writing config %02X\n", offset, config);
928
929 bios_wr32(bios, reg, configval);
930
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +0000931 return len;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000932}
933
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +0000934static int
Ben Skeggs6ee73862009-12-11 19:24:15 +1000935init_repeat(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
936{
937 /*
938 * INIT_REPEAT opcode: 0x33 ('3')
939 *
940 * offset (8 bit): opcode
941 * offset + 1 (8 bit): count
942 *
943 * Execute script following this opcode up to INIT_REPEAT_END
944 * "count" times
945 */
946
947 uint8_t count = bios->data[offset + 1];
948 uint8_t i;
949
950 /* no iexec->execute check by design */
951
952 BIOSLOG(bios, "0x%04X: Repeating following segment %d times\n",
953 offset, count);
954
955 iexec->repeat = true;
956
957 /*
958 * count - 1, as the script block will execute once when we leave this
959 * opcode -- this is compatible with bios behaviour as:
960 * a) the block is always executed at least once, even if count == 0
961 * b) the bios interpreter skips to the op following INIT_END_REPEAT,
962 * while we don't
963 */
964 for (i = 0; i < count - 1; i++)
965 parse_init_table(bios, offset + 2, iexec);
966
967 iexec->repeat = false;
968
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +0000969 return 2;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000970}
971
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +0000972static int
Ben Skeggs6ee73862009-12-11 19:24:15 +1000973init_io_restrict_pll(struct nvbios *bios, uint16_t offset,
974 struct init_exec *iexec)
975{
976 /*
977 * INIT_IO_RESTRICT_PLL opcode: 0x34 ('4')
978 *
979 * offset (8 bit): opcode
980 * offset + 1 (16 bit): CRTC port
981 * offset + 3 (8 bit): CRTC index
982 * offset + 4 (8 bit): mask
983 * offset + 5 (8 bit): shift
984 * offset + 6 (8 bit): IO flag condition index
985 * offset + 7 (8 bit): count
986 * offset + 8 (32 bit): register
987 * offset + 12 (16 bit): frequency 1
988 * ...
989 *
990 * Starting at offset + 12 there are "count" 16 bit frequencies (10kHz).
991 * Set PLL register "register" to coefficients for frequency n,
992 * selected by reading index "CRTC index" of "CRTC port" ANDed with
993 * "mask" and shifted right by "shift".
994 *
995 * If "IO flag condition index" > 0, and condition met, double
996 * frequency before setting it.
997 */
998
999 uint16_t crtcport = ROM16(bios->data[offset + 1]);
1000 uint8_t crtcindex = bios->data[offset + 3];
1001 uint8_t mask = bios->data[offset + 4];
1002 uint8_t shift = bios->data[offset + 5];
1003 int8_t io_flag_condition_idx = bios->data[offset + 6];
1004 uint8_t count = bios->data[offset + 7];
1005 uint32_t reg = ROM32(bios->data[offset + 8]);
1006 uint8_t config;
1007 uint16_t freq;
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00001008 int len = 12 + count * 2;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001009
1010 if (!iexec->execute)
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00001011 return len;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001012
1013 BIOSLOG(bios, "0x%04X: Port: 0x%04X, Index: 0x%02X, Mask: 0x%02X, "
1014 "Shift: 0x%02X, IO Flag Condition: 0x%02X, "
1015 "Count: 0x%02X, Reg: 0x%08X\n",
1016 offset, crtcport, crtcindex, mask, shift,
1017 io_flag_condition_idx, count, reg);
1018
1019 config = (bios_idxprt_rd(bios, crtcport, crtcindex) & mask) >> shift;
1020 if (config > count) {
1021 NV_ERROR(bios->dev,
1022 "0x%04X: Config 0x%02X exceeds maximal bound 0x%02X\n",
1023 offset, config, count);
Ben Skeggs309b8c82010-06-29 16:09:24 +10001024 return len;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001025 }
1026
1027 freq = ROM16(bios->data[offset + 12 + config * 2]);
1028
1029 if (io_flag_condition_idx > 0) {
1030 if (io_flag_condition_met(bios, offset, io_flag_condition_idx)) {
1031 BIOSLOG(bios, "0x%04X: Condition fulfilled -- "
1032 "frequency doubled\n", offset);
1033 freq *= 2;
1034 } else
1035 BIOSLOG(bios, "0x%04X: Condition not fulfilled -- "
1036 "frequency unchanged\n", offset);
1037 }
1038
1039 BIOSLOG(bios, "0x%04X: Reg: 0x%08X, Config: 0x%02X, Freq: %d0kHz\n",
1040 offset, reg, config, freq);
1041
1042 setPLL(bios, reg, freq * 10);
1043
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00001044 return len;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001045}
1046
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00001047static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10001048init_end_repeat(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
1049{
1050 /*
1051 * INIT_END_REPEAT opcode: 0x36 ('6')
1052 *
1053 * offset (8 bit): opcode
1054 *
1055 * Marks the end of the block for INIT_REPEAT to repeat
1056 */
1057
1058 /* no iexec->execute check by design */
1059
1060 /*
1061 * iexec->repeat flag necessary to go past INIT_END_REPEAT opcode when
1062 * we're not in repeat mode
1063 */
1064 if (iexec->repeat)
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00001065 return 0;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001066
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00001067 return 1;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001068}
1069
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00001070static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10001071init_copy(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
1072{
1073 /*
1074 * INIT_COPY opcode: 0x37 ('7')
1075 *
1076 * offset (8 bit): opcode
1077 * offset + 1 (32 bit): register
1078 * offset + 5 (8 bit): shift
1079 * offset + 6 (8 bit): srcmask
1080 * offset + 7 (16 bit): CRTC port
1081 * offset + 9 (8 bit): CRTC index
1082 * offset + 10 (8 bit): mask
1083 *
1084 * Read index "CRTC index" on "CRTC port", AND with "mask", OR with
1085 * (REGVAL("register") >> "shift" & "srcmask") and write-back to CRTC
1086 * port
1087 */
1088
1089 uint32_t reg = ROM32(bios->data[offset + 1]);
1090 uint8_t shift = bios->data[offset + 5];
1091 uint8_t srcmask = bios->data[offset + 6];
1092 uint16_t crtcport = ROM16(bios->data[offset + 7]);
1093 uint8_t crtcindex = bios->data[offset + 9];
1094 uint8_t mask = bios->data[offset + 10];
1095 uint32_t data;
1096 uint8_t crtcdata;
1097
1098 if (!iexec->execute)
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00001099 return 11;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001100
1101 BIOSLOG(bios, "0x%04X: Reg: 0x%08X, Shift: 0x%02X, SrcMask: 0x%02X, "
1102 "Port: 0x%04X, Index: 0x%02X, Mask: 0x%02X\n",
1103 offset, reg, shift, srcmask, crtcport, crtcindex, mask);
1104
1105 data = bios_rd32(bios, reg);
1106
1107 if (shift < 0x80)
1108 data >>= shift;
1109 else
1110 data <<= (0x100 - shift);
1111
1112 data &= srcmask;
1113
1114 crtcdata = bios_idxprt_rd(bios, crtcport, crtcindex) & mask;
1115 crtcdata |= (uint8_t)data;
1116 bios_idxprt_wr(bios, crtcport, crtcindex, crtcdata);
1117
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00001118 return 11;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001119}
1120
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00001121static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10001122init_not(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
1123{
1124 /*
1125 * INIT_NOT opcode: 0x38 ('8')
1126 *
1127 * offset (8 bit): opcode
1128 *
1129 * Invert the current execute / no-execute condition (i.e. "else")
1130 */
1131 if (iexec->execute)
1132 BIOSLOG(bios, "0x%04X: ------ Skipping following commands ------\n", offset);
1133 else
1134 BIOSLOG(bios, "0x%04X: ------ Executing following commands ------\n", offset);
1135
1136 iexec->execute = !iexec->execute;
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00001137 return 1;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001138}
1139
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00001140static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10001141init_io_flag_condition(struct nvbios *bios, uint16_t offset,
1142 struct init_exec *iexec)
1143{
1144 /*
1145 * INIT_IO_FLAG_CONDITION opcode: 0x39 ('9')
1146 *
1147 * offset (8 bit): opcode
1148 * offset + 1 (8 bit): condition number
1149 *
1150 * Check condition "condition number" in the IO flag condition table.
1151 * If condition not met skip subsequent opcodes until condition is
1152 * inverted (INIT_NOT), or we hit INIT_RESUME
1153 */
1154
1155 uint8_t cond = bios->data[offset + 1];
1156
1157 if (!iexec->execute)
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00001158 return 2;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001159
1160 if (io_flag_condition_met(bios, offset, cond))
1161 BIOSLOG(bios, "0x%04X: Condition fulfilled -- continuing to execute\n", offset);
1162 else {
1163 BIOSLOG(bios, "0x%04X: Condition not fulfilled -- skipping following commands\n", offset);
1164 iexec->execute = false;
1165 }
1166
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00001167 return 2;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001168}
1169
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00001170static int
Ben Skeggs25908b72010-04-20 02:28:37 +10001171init_dp_condition(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
1172{
1173 /*
1174 * INIT_DP_CONDITION opcode: 0x3A ('')
1175 *
1176 * offset (8 bit): opcode
1177 * offset + 1 (8 bit): "sub" opcode
1178 * offset + 2 (8 bit): unknown
1179 *
1180 */
1181
Ben Skeggs25908b72010-04-20 02:28:37 +10001182 struct dcb_entry *dcb = bios->display.output;
1183 struct drm_device *dev = bios->dev;
1184 uint8_t cond = bios->data[offset + 1];
Ben Skeggs27a45982011-08-04 09:26:44 +10001185 uint8_t *table, headerlen;
Ben Skeggs25908b72010-04-20 02:28:37 +10001186
1187 BIOSLOG(bios, "0x%04X: subop 0x%02X\n", offset, cond);
1188
1189 if (!iexec->execute)
1190 return 3;
1191
Ben Skeggs27a45982011-08-04 09:26:44 +10001192 table = nouveau_bios_dp_table(dev, dcb, &headerlen);
1193 if (!table) {
Ben Skeggs25908b72010-04-20 02:28:37 +10001194 NV_ERROR(dev, "0x%04X: INIT_3A: no encoder table!!\n", offset);
Ben Skeggs309b8c82010-06-29 16:09:24 +10001195 return 3;
Ben Skeggs25908b72010-04-20 02:28:37 +10001196 }
1197
1198 switch (cond) {
1199 case 0:
1200 {
1201 struct dcb_connector_table_entry *ent =
1202 &bios->dcb.connector.entry[dcb->connector];
1203
1204 if (ent->type != DCB_CONNECTOR_eDP)
1205 iexec->execute = false;
1206 }
1207 break;
1208 case 1:
1209 case 2:
Ben Skeggs27a45982011-08-04 09:26:44 +10001210 if (!(table[5] & cond))
Ben Skeggs25908b72010-04-20 02:28:37 +10001211 iexec->execute = false;
1212 break;
1213 case 5:
1214 {
1215 struct nouveau_i2c_chan *auxch;
1216 int ret;
1217
1218 auxch = nouveau_i2c_find(dev, bios->display.output->i2c_index);
Ben Skeggs309b8c82010-06-29 16:09:24 +10001219 if (!auxch) {
1220 NV_ERROR(dev, "0x%04X: couldn't get auxch\n", offset);
1221 return 3;
1222 }
Ben Skeggs25908b72010-04-20 02:28:37 +10001223
1224 ret = nouveau_dp_auxch(auxch, 9, 0xd, &cond, 1);
Ben Skeggs309b8c82010-06-29 16:09:24 +10001225 if (ret) {
1226 NV_ERROR(dev, "0x%04X: auxch rd fail: %d\n", offset, ret);
1227 return 3;
1228 }
Ben Skeggs25908b72010-04-20 02:28:37 +10001229
Ben Skeggs64d202b2010-09-21 12:10:51 +10001230 if (!(cond & 1))
Ben Skeggs25908b72010-04-20 02:28:37 +10001231 iexec->execute = false;
1232 }
1233 break;
1234 default:
1235 NV_WARN(dev, "0x%04X: unknown INIT_3A op: %d\n", offset, cond);
1236 break;
1237 }
1238
1239 if (iexec->execute)
1240 BIOSLOG(bios, "0x%04X: continuing to execute\n", offset);
1241 else
1242 BIOSLOG(bios, "0x%04X: skipping following commands\n", offset);
1243
1244 return 3;
1245}
1246
1247static int
1248init_op_3b(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
1249{
1250 /*
1251 * INIT_3B opcode: 0x3B ('')
1252 *
1253 * offset (8 bit): opcode
1254 * offset + 1 (8 bit): crtc index
1255 *
1256 */
1257
1258 uint8_t or = ffs(bios->display.output->or) - 1;
1259 uint8_t index = bios->data[offset + 1];
1260 uint8_t data;
1261
1262 if (!iexec->execute)
1263 return 2;
1264
1265 data = bios_idxprt_rd(bios, 0x3d4, index);
1266 bios_idxprt_wr(bios, 0x3d4, index, data & ~(1 << or));
1267 return 2;
1268}
1269
1270static int
1271init_op_3c(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
1272{
1273 /*
1274 * INIT_3C opcode: 0x3C ('')
1275 *
1276 * offset (8 bit): opcode
1277 * offset + 1 (8 bit): crtc index
1278 *
1279 */
1280
1281 uint8_t or = ffs(bios->display.output->or) - 1;
1282 uint8_t index = bios->data[offset + 1];
1283 uint8_t data;
1284
1285 if (!iexec->execute)
1286 return 2;
1287
1288 data = bios_idxprt_rd(bios, 0x3d4, index);
1289 bios_idxprt_wr(bios, 0x3d4, index, data | (1 << or));
1290 return 2;
1291}
1292
1293static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10001294init_idx_addr_latched(struct nvbios *bios, uint16_t offset,
1295 struct init_exec *iexec)
1296{
1297 /*
1298 * INIT_INDEX_ADDRESS_LATCHED opcode: 0x49 ('I')
1299 *
1300 * offset (8 bit): opcode
1301 * offset + 1 (32 bit): control register
1302 * offset + 5 (32 bit): data register
1303 * offset + 9 (32 bit): mask
1304 * offset + 13 (32 bit): data
1305 * offset + 17 (8 bit): count
1306 * offset + 18 (8 bit): address 1
1307 * offset + 19 (8 bit): data 1
1308 * ...
1309 *
1310 * For each of "count" address and data pairs, write "data n" to
1311 * "data register", read the current value of "control register",
1312 * and write it back once ANDed with "mask", ORed with "data",
1313 * and ORed with "address n"
1314 */
1315
1316 uint32_t controlreg = ROM32(bios->data[offset + 1]);
1317 uint32_t datareg = ROM32(bios->data[offset + 5]);
1318 uint32_t mask = ROM32(bios->data[offset + 9]);
1319 uint32_t data = ROM32(bios->data[offset + 13]);
1320 uint8_t count = bios->data[offset + 17];
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00001321 int len = 18 + count * 2;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001322 uint32_t value;
1323 int i;
1324
1325 if (!iexec->execute)
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00001326 return len;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001327
1328 BIOSLOG(bios, "0x%04X: ControlReg: 0x%08X, DataReg: 0x%08X, "
1329 "Mask: 0x%08X, Data: 0x%08X, Count: 0x%02X\n",
1330 offset, controlreg, datareg, mask, data, count);
1331
1332 for (i = 0; i < count; i++) {
1333 uint8_t instaddress = bios->data[offset + 18 + i * 2];
1334 uint8_t instdata = bios->data[offset + 19 + i * 2];
1335
1336 BIOSLOG(bios, "0x%04X: Address: 0x%02X, Data: 0x%02X\n",
1337 offset, instaddress, instdata);
1338
1339 bios_wr32(bios, datareg, instdata);
1340 value = bios_rd32(bios, controlreg) & mask;
1341 value |= data;
1342 value |= instaddress;
1343 bios_wr32(bios, controlreg, value);
1344 }
1345
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00001346 return len;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001347}
1348
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00001349static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10001350init_io_restrict_pll2(struct nvbios *bios, uint16_t offset,
1351 struct init_exec *iexec)
1352{
1353 /*
1354 * INIT_IO_RESTRICT_PLL2 opcode: 0x4A ('J')
1355 *
1356 * offset (8 bit): opcode
1357 * offset + 1 (16 bit): CRTC port
1358 * offset + 3 (8 bit): CRTC index
1359 * offset + 4 (8 bit): mask
1360 * offset + 5 (8 bit): shift
1361 * offset + 6 (8 bit): count
1362 * offset + 7 (32 bit): register
1363 * offset + 11 (32 bit): frequency 1
1364 * ...
1365 *
1366 * Starting at offset + 11 there are "count" 32 bit frequencies (kHz).
1367 * Set PLL register "register" to coefficients for frequency n,
1368 * selected by reading index "CRTC index" of "CRTC port" ANDed with
1369 * "mask" and shifted right by "shift".
1370 */
1371
1372 uint16_t crtcport = ROM16(bios->data[offset + 1]);
1373 uint8_t crtcindex = bios->data[offset + 3];
1374 uint8_t mask = bios->data[offset + 4];
1375 uint8_t shift = bios->data[offset + 5];
1376 uint8_t count = bios->data[offset + 6];
1377 uint32_t reg = ROM32(bios->data[offset + 7]);
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00001378 int len = 11 + count * 4;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001379 uint8_t config;
1380 uint32_t freq;
1381
1382 if (!iexec->execute)
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00001383 return len;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001384
1385 BIOSLOG(bios, "0x%04X: Port: 0x%04X, Index: 0x%02X, Mask: 0x%02X, "
1386 "Shift: 0x%02X, Count: 0x%02X, Reg: 0x%08X\n",
1387 offset, crtcport, crtcindex, mask, shift, count, reg);
1388
1389 if (!reg)
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00001390 return len;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001391
1392 config = (bios_idxprt_rd(bios, crtcport, crtcindex) & mask) >> shift;
1393 if (config > count) {
1394 NV_ERROR(bios->dev,
1395 "0x%04X: Config 0x%02X exceeds maximal bound 0x%02X\n",
1396 offset, config, count);
Ben Skeggs309b8c82010-06-29 16:09:24 +10001397 return len;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001398 }
1399
1400 freq = ROM32(bios->data[offset + 11 + config * 4]);
1401
1402 BIOSLOG(bios, "0x%04X: Reg: 0x%08X, Config: 0x%02X, Freq: %dkHz\n",
1403 offset, reg, config, freq);
1404
1405 setPLL(bios, reg, freq);
1406
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00001407 return len;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001408}
1409
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00001410static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10001411init_pll2(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
1412{
1413 /*
1414 * INIT_PLL2 opcode: 0x4B ('K')
1415 *
1416 * offset (8 bit): opcode
1417 * offset + 1 (32 bit): register
1418 * offset + 5 (32 bit): freq
1419 *
1420 * Set PLL register "register" to coefficients for frequency "freq"
1421 */
1422
1423 uint32_t reg = ROM32(bios->data[offset + 1]);
1424 uint32_t freq = ROM32(bios->data[offset + 5]);
1425
1426 if (!iexec->execute)
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00001427 return 9;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001428
1429 BIOSLOG(bios, "0x%04X: Reg: 0x%04X, Freq: %dkHz\n",
1430 offset, reg, freq);
1431
1432 setPLL(bios, reg, freq);
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00001433 return 9;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001434}
1435
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00001436static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10001437init_i2c_byte(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
1438{
1439 /*
1440 * INIT_I2C_BYTE opcode: 0x4C ('L')
1441 *
1442 * offset (8 bit): opcode
1443 * offset + 1 (8 bit): DCB I2C table entry index
1444 * offset + 2 (8 bit): I2C slave address
1445 * offset + 3 (8 bit): count
1446 * offset + 4 (8 bit): I2C register 1
1447 * offset + 5 (8 bit): mask 1
1448 * offset + 6 (8 bit): data 1
1449 * ...
1450 *
1451 * For each of "count" registers given by "I2C register n" on the device
1452 * addressed by "I2C slave address" on the I2C bus given by
1453 * "DCB I2C table entry index", read the register, AND the result with
1454 * "mask n" and OR it with "data n" before writing it back to the device
1455 */
1456
Ben Skeggs309b8c82010-06-29 16:09:24 +10001457 struct drm_device *dev = bios->dev;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001458 uint8_t i2c_index = bios->data[offset + 1];
Ben Skeggs893887ed2010-05-12 16:30:50 +10001459 uint8_t i2c_address = bios->data[offset + 2] >> 1;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001460 uint8_t count = bios->data[offset + 3];
1461 struct nouveau_i2c_chan *chan;
Ben Skeggs893887ed2010-05-12 16:30:50 +10001462 int len = 4 + count * 3;
1463 int ret, i;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001464
1465 if (!iexec->execute)
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00001466 return len;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001467
1468 BIOSLOG(bios, "0x%04X: DCBI2CIndex: 0x%02X, I2CAddress: 0x%02X, "
1469 "Count: 0x%02X\n",
1470 offset, i2c_index, i2c_address, count);
1471
Ben Skeggs309b8c82010-06-29 16:09:24 +10001472 chan = init_i2c_device_find(dev, i2c_index);
1473 if (!chan) {
1474 NV_ERROR(dev, "0x%04X: i2c bus not found\n", offset);
1475 return len;
1476 }
Ben Skeggs6ee73862009-12-11 19:24:15 +10001477
1478 for (i = 0; i < count; i++) {
Ben Skeggs893887ed2010-05-12 16:30:50 +10001479 uint8_t reg = bios->data[offset + 4 + i * 3];
Ben Skeggs6ee73862009-12-11 19:24:15 +10001480 uint8_t mask = bios->data[offset + 5 + i * 3];
1481 uint8_t data = bios->data[offset + 6 + i * 3];
Ben Skeggs893887ed2010-05-12 16:30:50 +10001482 union i2c_smbus_data val;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001483
Ben Skeggs893887ed2010-05-12 16:30:50 +10001484 ret = i2c_smbus_xfer(&chan->adapter, i2c_address, 0,
1485 I2C_SMBUS_READ, reg,
1486 I2C_SMBUS_BYTE_DATA, &val);
Ben Skeggs309b8c82010-06-29 16:09:24 +10001487 if (ret < 0) {
1488 NV_ERROR(dev, "0x%04X: i2c rd fail: %d\n", offset, ret);
1489 return len;
1490 }
Ben Skeggs6ee73862009-12-11 19:24:15 +10001491
1492 BIOSLOG(bios, "0x%04X: I2CReg: 0x%02X, Value: 0x%02X, "
1493 "Mask: 0x%02X, Data: 0x%02X\n",
Ben Skeggs893887ed2010-05-12 16:30:50 +10001494 offset, reg, val.byte, mask, data);
Ben Skeggs6ee73862009-12-11 19:24:15 +10001495
Ben Skeggs893887ed2010-05-12 16:30:50 +10001496 if (!bios->execute)
1497 continue;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001498
Ben Skeggs893887ed2010-05-12 16:30:50 +10001499 val.byte &= mask;
1500 val.byte |= data;
1501 ret = i2c_smbus_xfer(&chan->adapter, i2c_address, 0,
1502 I2C_SMBUS_WRITE, reg,
1503 I2C_SMBUS_BYTE_DATA, &val);
Ben Skeggs309b8c82010-06-29 16:09:24 +10001504 if (ret < 0) {
1505 NV_ERROR(dev, "0x%04X: i2c wr fail: %d\n", offset, ret);
1506 return len;
1507 }
Ben Skeggs6ee73862009-12-11 19:24:15 +10001508 }
1509
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00001510 return len;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001511}
1512
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00001513static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10001514init_zm_i2c_byte(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
1515{
1516 /*
1517 * INIT_ZM_I2C_BYTE opcode: 0x4D ('M')
1518 *
1519 * offset (8 bit): opcode
1520 * offset + 1 (8 bit): DCB I2C table entry index
1521 * offset + 2 (8 bit): I2C slave address
1522 * offset + 3 (8 bit): count
1523 * offset + 4 (8 bit): I2C register 1
1524 * offset + 5 (8 bit): data 1
1525 * ...
1526 *
1527 * For each of "count" registers given by "I2C register n" on the device
1528 * addressed by "I2C slave address" on the I2C bus given by
1529 * "DCB I2C table entry index", set the register to "data n"
1530 */
1531
Ben Skeggs309b8c82010-06-29 16:09:24 +10001532 struct drm_device *dev = bios->dev;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001533 uint8_t i2c_index = bios->data[offset + 1];
Ben Skeggs893887ed2010-05-12 16:30:50 +10001534 uint8_t i2c_address = bios->data[offset + 2] >> 1;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001535 uint8_t count = bios->data[offset + 3];
1536 struct nouveau_i2c_chan *chan;
Ben Skeggs893887ed2010-05-12 16:30:50 +10001537 int len = 4 + count * 2;
1538 int ret, i;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001539
1540 if (!iexec->execute)
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00001541 return len;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001542
1543 BIOSLOG(bios, "0x%04X: DCBI2CIndex: 0x%02X, I2CAddress: 0x%02X, "
1544 "Count: 0x%02X\n",
1545 offset, i2c_index, i2c_address, count);
1546
Ben Skeggs309b8c82010-06-29 16:09:24 +10001547 chan = init_i2c_device_find(dev, i2c_index);
1548 if (!chan) {
1549 NV_ERROR(dev, "0x%04X: i2c bus not found\n", offset);
1550 return len;
1551 }
Ben Skeggs6ee73862009-12-11 19:24:15 +10001552
1553 for (i = 0; i < count; i++) {
Ben Skeggs893887ed2010-05-12 16:30:50 +10001554 uint8_t reg = bios->data[offset + 4 + i * 2];
1555 union i2c_smbus_data val;
1556
1557 val.byte = bios->data[offset + 5 + i * 2];
Ben Skeggs6ee73862009-12-11 19:24:15 +10001558
1559 BIOSLOG(bios, "0x%04X: I2CReg: 0x%02X, Data: 0x%02X\n",
Ben Skeggs893887ed2010-05-12 16:30:50 +10001560 offset, reg, val.byte);
Ben Skeggs6ee73862009-12-11 19:24:15 +10001561
Ben Skeggs893887ed2010-05-12 16:30:50 +10001562 if (!bios->execute)
1563 continue;
1564
1565 ret = i2c_smbus_xfer(&chan->adapter, i2c_address, 0,
1566 I2C_SMBUS_WRITE, reg,
1567 I2C_SMBUS_BYTE_DATA, &val);
Ben Skeggs309b8c82010-06-29 16:09:24 +10001568 if (ret < 0) {
1569 NV_ERROR(dev, "0x%04X: i2c wr fail: %d\n", offset, ret);
1570 return len;
1571 }
Ben Skeggs6ee73862009-12-11 19:24:15 +10001572 }
1573
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00001574 return len;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001575}
1576
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00001577static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10001578init_zm_i2c(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
1579{
1580 /*
1581 * INIT_ZM_I2C opcode: 0x4E ('N')
1582 *
1583 * offset (8 bit): opcode
1584 * offset + 1 (8 bit): DCB I2C table entry index
1585 * offset + 2 (8 bit): I2C slave address
1586 * offset + 3 (8 bit): count
1587 * offset + 4 (8 bit): data 1
1588 * ...
1589 *
1590 * Send "count" bytes ("data n") to the device addressed by "I2C slave
1591 * address" on the I2C bus given by "DCB I2C table entry index"
1592 */
1593
Ben Skeggs309b8c82010-06-29 16:09:24 +10001594 struct drm_device *dev = bios->dev;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001595 uint8_t i2c_index = bios->data[offset + 1];
Ben Skeggs893887ed2010-05-12 16:30:50 +10001596 uint8_t i2c_address = bios->data[offset + 2] >> 1;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001597 uint8_t count = bios->data[offset + 3];
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00001598 int len = 4 + count;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001599 struct nouveau_i2c_chan *chan;
1600 struct i2c_msg msg;
1601 uint8_t data[256];
Ben Skeggs309b8c82010-06-29 16:09:24 +10001602 int ret, i;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001603
1604 if (!iexec->execute)
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00001605 return len;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001606
1607 BIOSLOG(bios, "0x%04X: DCBI2CIndex: 0x%02X, I2CAddress: 0x%02X, "
1608 "Count: 0x%02X\n",
1609 offset, i2c_index, i2c_address, count);
1610
Ben Skeggs309b8c82010-06-29 16:09:24 +10001611 chan = init_i2c_device_find(dev, i2c_index);
1612 if (!chan) {
1613 NV_ERROR(dev, "0x%04X: i2c bus not found\n", offset);
1614 return len;
1615 }
Ben Skeggs6ee73862009-12-11 19:24:15 +10001616
1617 for (i = 0; i < count; i++) {
1618 data[i] = bios->data[offset + 4 + i];
1619
1620 BIOSLOG(bios, "0x%04X: Data: 0x%02X\n", offset, data[i]);
1621 }
1622
1623 if (bios->execute) {
1624 msg.addr = i2c_address;
1625 msg.flags = 0;
1626 msg.len = count;
1627 msg.buf = data;
Ben Skeggs309b8c82010-06-29 16:09:24 +10001628 ret = i2c_transfer(&chan->adapter, &msg, 1);
1629 if (ret != 1) {
1630 NV_ERROR(dev, "0x%04X: i2c wr fail: %d\n", offset, ret);
1631 return len;
1632 }
Ben Skeggs6ee73862009-12-11 19:24:15 +10001633 }
1634
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00001635 return len;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001636}
1637
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00001638static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10001639init_tmds(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
1640{
1641 /*
1642 * INIT_TMDS opcode: 0x4F ('O') (non-canon name)
1643 *
1644 * offset (8 bit): opcode
1645 * offset + 1 (8 bit): magic lookup value
1646 * offset + 2 (8 bit): TMDS address
1647 * offset + 3 (8 bit): mask
1648 * offset + 4 (8 bit): data
1649 *
1650 * Read the data reg for TMDS address "TMDS address", AND it with mask
1651 * and OR it with data, then write it back
1652 * "magic lookup value" determines which TMDS base address register is
1653 * used -- see get_tmds_index_reg()
1654 */
1655
Ben Skeggs309b8c82010-06-29 16:09:24 +10001656 struct drm_device *dev = bios->dev;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001657 uint8_t mlv = bios->data[offset + 1];
1658 uint32_t tmdsaddr = bios->data[offset + 2];
1659 uint8_t mask = bios->data[offset + 3];
1660 uint8_t data = bios->data[offset + 4];
1661 uint32_t reg, value;
1662
1663 if (!iexec->execute)
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00001664 return 5;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001665
1666 BIOSLOG(bios, "0x%04X: MagicLookupValue: 0x%02X, TMDSAddr: 0x%02X, "
1667 "Mask: 0x%02X, Data: 0x%02X\n",
1668 offset, mlv, tmdsaddr, mask, data);
1669
1670 reg = get_tmds_index_reg(bios->dev, mlv);
Ben Skeggs309b8c82010-06-29 16:09:24 +10001671 if (!reg) {
1672 NV_ERROR(dev, "0x%04X: no tmds_index_reg\n", offset);
1673 return 5;
1674 }
Ben Skeggs6ee73862009-12-11 19:24:15 +10001675
1676 bios_wr32(bios, reg,
1677 tmdsaddr | NV_PRAMDAC_FP_TMDS_CONTROL_WRITE_DISABLE);
1678 value = (bios_rd32(bios, reg + 4) & mask) | data;
1679 bios_wr32(bios, reg + 4, value);
1680 bios_wr32(bios, reg, tmdsaddr);
1681
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00001682 return 5;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001683}
1684
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00001685static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10001686init_zm_tmds_group(struct nvbios *bios, uint16_t offset,
1687 struct init_exec *iexec)
1688{
1689 /*
1690 * INIT_ZM_TMDS_GROUP opcode: 0x50 ('P') (non-canon name)
1691 *
1692 * offset (8 bit): opcode
1693 * offset + 1 (8 bit): magic lookup value
1694 * offset + 2 (8 bit): count
1695 * offset + 3 (8 bit): addr 1
1696 * offset + 4 (8 bit): data 1
1697 * ...
1698 *
1699 * For each of "count" TMDS address and data pairs write "data n" to
1700 * "addr n". "magic lookup value" determines which TMDS base address
1701 * register is used -- see get_tmds_index_reg()
1702 */
1703
Ben Skeggs309b8c82010-06-29 16:09:24 +10001704 struct drm_device *dev = bios->dev;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001705 uint8_t mlv = bios->data[offset + 1];
1706 uint8_t count = bios->data[offset + 2];
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00001707 int len = 3 + count * 2;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001708 uint32_t reg;
1709 int i;
1710
1711 if (!iexec->execute)
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00001712 return len;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001713
1714 BIOSLOG(bios, "0x%04X: MagicLookupValue: 0x%02X, Count: 0x%02X\n",
1715 offset, mlv, count);
1716
1717 reg = get_tmds_index_reg(bios->dev, mlv);
Ben Skeggs309b8c82010-06-29 16:09:24 +10001718 if (!reg) {
1719 NV_ERROR(dev, "0x%04X: no tmds_index_reg\n", offset);
1720 return len;
1721 }
Ben Skeggs6ee73862009-12-11 19:24:15 +10001722
1723 for (i = 0; i < count; i++) {
1724 uint8_t tmdsaddr = bios->data[offset + 3 + i * 2];
1725 uint8_t tmdsdata = bios->data[offset + 4 + i * 2];
1726
1727 bios_wr32(bios, reg + 4, tmdsdata);
1728 bios_wr32(bios, reg, tmdsaddr);
1729 }
1730
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00001731 return len;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001732}
1733
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00001734static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10001735init_cr_idx_adr_latch(struct nvbios *bios, uint16_t offset,
1736 struct init_exec *iexec)
1737{
1738 /*
1739 * INIT_CR_INDEX_ADDRESS_LATCHED opcode: 0x51 ('Q')
1740 *
1741 * offset (8 bit): opcode
1742 * offset + 1 (8 bit): CRTC index1
1743 * offset + 2 (8 bit): CRTC index2
1744 * offset + 3 (8 bit): baseaddr
1745 * offset + 4 (8 bit): count
1746 * offset + 5 (8 bit): data 1
1747 * ...
1748 *
1749 * For each of "count" address and data pairs, write "baseaddr + n" to
1750 * "CRTC index1" and "data n" to "CRTC index2"
1751 * Once complete, restore initial value read from "CRTC index1"
1752 */
1753 uint8_t crtcindex1 = bios->data[offset + 1];
1754 uint8_t crtcindex2 = bios->data[offset + 2];
1755 uint8_t baseaddr = bios->data[offset + 3];
1756 uint8_t count = bios->data[offset + 4];
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00001757 int len = 5 + count;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001758 uint8_t oldaddr, data;
1759 int i;
1760
1761 if (!iexec->execute)
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00001762 return len;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001763
1764 BIOSLOG(bios, "0x%04X: Index1: 0x%02X, Index2: 0x%02X, "
1765 "BaseAddr: 0x%02X, Count: 0x%02X\n",
1766 offset, crtcindex1, crtcindex2, baseaddr, count);
1767
1768 oldaddr = bios_idxprt_rd(bios, NV_CIO_CRX__COLOR, crtcindex1);
1769
1770 for (i = 0; i < count; i++) {
1771 bios_idxprt_wr(bios, NV_CIO_CRX__COLOR, crtcindex1,
1772 baseaddr + i);
1773 data = bios->data[offset + 5 + i];
1774 bios_idxprt_wr(bios, NV_CIO_CRX__COLOR, crtcindex2, data);
1775 }
1776
1777 bios_idxprt_wr(bios, NV_CIO_CRX__COLOR, crtcindex1, oldaddr);
1778
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00001779 return len;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001780}
1781
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00001782static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10001783init_cr(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
1784{
1785 /*
1786 * INIT_CR opcode: 0x52 ('R')
1787 *
1788 * offset (8 bit): opcode
1789 * offset + 1 (8 bit): CRTC index
1790 * offset + 2 (8 bit): mask
1791 * offset + 3 (8 bit): data
1792 *
1793 * Assign the value of at "CRTC index" ANDed with mask and ORed with
1794 * data back to "CRTC index"
1795 */
1796
1797 uint8_t crtcindex = bios->data[offset + 1];
1798 uint8_t mask = bios->data[offset + 2];
1799 uint8_t data = bios->data[offset + 3];
1800 uint8_t value;
1801
1802 if (!iexec->execute)
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00001803 return 4;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001804
1805 BIOSLOG(bios, "0x%04X: Index: 0x%02X, Mask: 0x%02X, Data: 0x%02X\n",
1806 offset, crtcindex, mask, data);
1807
1808 value = bios_idxprt_rd(bios, NV_CIO_CRX__COLOR, crtcindex) & mask;
1809 value |= data;
1810 bios_idxprt_wr(bios, NV_CIO_CRX__COLOR, crtcindex, value);
1811
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00001812 return 4;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001813}
1814
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00001815static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10001816init_zm_cr(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
1817{
1818 /*
1819 * INIT_ZM_CR opcode: 0x53 ('S')
1820 *
1821 * offset (8 bit): opcode
1822 * offset + 1 (8 bit): CRTC index
1823 * offset + 2 (8 bit): value
1824 *
1825 * Assign "value" to CRTC register with index "CRTC index".
1826 */
1827
1828 uint8_t crtcindex = ROM32(bios->data[offset + 1]);
1829 uint8_t data = bios->data[offset + 2];
1830
1831 if (!iexec->execute)
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00001832 return 3;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001833
1834 bios_idxprt_wr(bios, NV_CIO_CRX__COLOR, crtcindex, data);
1835
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00001836 return 3;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001837}
1838
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00001839static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10001840init_zm_cr_group(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
1841{
1842 /*
1843 * INIT_ZM_CR_GROUP opcode: 0x54 ('T')
1844 *
1845 * offset (8 bit): opcode
1846 * offset + 1 (8 bit): count
1847 * offset + 2 (8 bit): CRTC index 1
1848 * offset + 3 (8 bit): value 1
1849 * ...
1850 *
1851 * For "count", assign "value n" to CRTC register with index
1852 * "CRTC index n".
1853 */
1854
1855 uint8_t count = bios->data[offset + 1];
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00001856 int len = 2 + count * 2;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001857 int i;
1858
1859 if (!iexec->execute)
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00001860 return len;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001861
1862 for (i = 0; i < count; i++)
1863 init_zm_cr(bios, offset + 2 + 2 * i - 1, iexec);
1864
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00001865 return len;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001866}
1867
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00001868static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10001869init_condition_time(struct nvbios *bios, uint16_t offset,
1870 struct init_exec *iexec)
1871{
1872 /*
1873 * INIT_CONDITION_TIME opcode: 0x56 ('V')
1874 *
1875 * offset (8 bit): opcode
1876 * offset + 1 (8 bit): condition number
1877 * offset + 2 (8 bit): retries / 50
1878 *
1879 * Check condition "condition number" in the condition table.
1880 * Bios code then sleeps for 2ms if the condition is not met, and
1881 * repeats up to "retries" times, but on one C51 this has proved
1882 * insufficient. In mmiotraces the driver sleeps for 20ms, so we do
1883 * this, and bail after "retries" times, or 2s, whichever is less.
1884 * If still not met after retries, clear execution flag for this table.
1885 */
1886
1887 uint8_t cond = bios->data[offset + 1];
1888 uint16_t retries = bios->data[offset + 2] * 50;
1889 unsigned cnt;
1890
1891 if (!iexec->execute)
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00001892 return 3;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001893
1894 if (retries > 100)
1895 retries = 100;
1896
1897 BIOSLOG(bios, "0x%04X: Condition: 0x%02X, Retries: 0x%02X\n",
1898 offset, cond, retries);
1899
1900 if (!bios->execute) /* avoid 2s delays when "faking" execution */
1901 retries = 1;
1902
1903 for (cnt = 0; cnt < retries; cnt++) {
1904 if (bios_condition_met(bios, offset, cond)) {
1905 BIOSLOG(bios, "0x%04X: Condition met, continuing\n",
1906 offset);
1907 break;
1908 } else {
1909 BIOSLOG(bios, "0x%04X: "
1910 "Condition not met, sleeping for 20ms\n",
1911 offset);
Ben Skeggsc7ca4d12011-02-03 20:10:49 +10001912 mdelay(20);
Ben Skeggs6ee73862009-12-11 19:24:15 +10001913 }
1914 }
1915
1916 if (!bios_condition_met(bios, offset, cond)) {
1917 NV_WARN(bios->dev,
1918 "0x%04X: Condition still not met after %dms, "
1919 "skipping following opcodes\n", offset, 20 * retries);
1920 iexec->execute = false;
1921 }
1922
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00001923 return 3;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001924}
1925
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00001926static int
Marcin Koƛcielnickie3a19242010-07-02 19:33:01 +00001927init_ltime(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
1928{
1929 /*
1930 * INIT_LTIME opcode: 0x57 ('V')
1931 *
1932 * offset (8 bit): opcode
1933 * offset + 1 (16 bit): time
1934 *
Stefan Weile8a8b252011-01-02 15:12:42 +01001935 * Sleep for "time" milliseconds.
Marcin Koƛcielnickie3a19242010-07-02 19:33:01 +00001936 */
1937
1938 unsigned time = ROM16(bios->data[offset + 1]);
1939
1940 if (!iexec->execute)
1941 return 3;
1942
Stefan Weile8a8b252011-01-02 15:12:42 +01001943 BIOSLOG(bios, "0x%04X: Sleeping for 0x%04X milliseconds\n",
Marcin Koƛcielnickie3a19242010-07-02 19:33:01 +00001944 offset, time);
1945
Ben Skeggsc7ca4d12011-02-03 20:10:49 +10001946 mdelay(time);
Marcin Koƛcielnickie3a19242010-07-02 19:33:01 +00001947
1948 return 3;
1949}
1950
1951static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10001952init_zm_reg_sequence(struct nvbios *bios, uint16_t offset,
1953 struct init_exec *iexec)
1954{
1955 /*
1956 * INIT_ZM_REG_SEQUENCE opcode: 0x58 ('X')
1957 *
1958 * offset (8 bit): opcode
1959 * offset + 1 (32 bit): base register
1960 * offset + 5 (8 bit): count
1961 * offset + 6 (32 bit): value 1
1962 * ...
1963 *
1964 * Starting at offset + 6 there are "count" 32 bit values.
1965 * For "count" iterations set "base register" + 4 * current_iteration
1966 * to "value current_iteration"
1967 */
1968
1969 uint32_t basereg = ROM32(bios->data[offset + 1]);
1970 uint32_t count = bios->data[offset + 5];
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00001971 int len = 6 + count * 4;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001972 int i;
1973
1974 if (!iexec->execute)
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00001975 return len;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001976
1977 BIOSLOG(bios, "0x%04X: BaseReg: 0x%08X, Count: 0x%02X\n",
1978 offset, basereg, count);
1979
1980 for (i = 0; i < count; i++) {
1981 uint32_t reg = basereg + i * 4;
1982 uint32_t data = ROM32(bios->data[offset + 6 + i * 4]);
1983
1984 bios_wr32(bios, reg, data);
1985 }
1986
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00001987 return len;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001988}
1989
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00001990static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10001991init_sub_direct(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
1992{
1993 /*
1994 * INIT_SUB_DIRECT opcode: 0x5B ('[')
1995 *
1996 * offset (8 bit): opcode
1997 * offset + 1 (16 bit): subroutine offset (in bios)
1998 *
1999 * Calls a subroutine that will execute commands until INIT_DONE
2000 * is found.
2001 */
2002
2003 uint16_t sub_offset = ROM16(bios->data[offset + 1]);
2004
2005 if (!iexec->execute)
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00002006 return 3;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002007
2008 BIOSLOG(bios, "0x%04X: Executing subroutine at 0x%04X\n",
2009 offset, sub_offset);
2010
2011 parse_init_table(bios, sub_offset, iexec);
2012
2013 BIOSLOG(bios, "0x%04X: End of 0x%04X subroutine\n", offset, sub_offset);
2014
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00002015 return 3;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002016}
2017
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00002018static int
Ben Skeggsec64a402011-03-21 21:31:21 +10002019init_jump(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
2020{
2021 /*
2022 * INIT_JUMP opcode: 0x5C ('\')
2023 *
2024 * offset (8 bit): opcode
2025 * offset + 1 (16 bit): offset (in bios)
2026 *
2027 * Continue execution of init table from 'offset'
2028 */
2029
2030 uint16_t jmp_offset = ROM16(bios->data[offset + 1]);
2031
2032 if (!iexec->execute)
2033 return 3;
2034
2035 BIOSLOG(bios, "0x%04X: Jump to 0x%04X\n", offset, jmp_offset);
2036 return jmp_offset - offset;
2037}
2038
2039static int
Marcin Koƛcielnickib715d642010-07-04 02:47:16 +00002040init_i2c_if(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
2041{
2042 /*
2043 * INIT_I2C_IF opcode: 0x5E ('^')
2044 *
2045 * offset (8 bit): opcode
2046 * offset + 1 (8 bit): DCB I2C table entry index
2047 * offset + 2 (8 bit): I2C slave address
2048 * offset + 3 (8 bit): I2C register
2049 * offset + 4 (8 bit): mask
2050 * offset + 5 (8 bit): data
2051 *
2052 * Read the register given by "I2C register" on the device addressed
2053 * by "I2C slave address" on the I2C bus given by "DCB I2C table
2054 * entry index". Compare the result AND "mask" to "data".
2055 * If they're not equal, skip subsequent opcodes until condition is
2056 * inverted (INIT_NOT), or we hit INIT_RESUME
2057 */
2058
2059 uint8_t i2c_index = bios->data[offset + 1];
2060 uint8_t i2c_address = bios->data[offset + 2] >> 1;
2061 uint8_t reg = bios->data[offset + 3];
2062 uint8_t mask = bios->data[offset + 4];
2063 uint8_t data = bios->data[offset + 5];
2064 struct nouveau_i2c_chan *chan;
2065 union i2c_smbus_data val;
2066 int ret;
2067
2068 /* no execute check by design */
2069
2070 BIOSLOG(bios, "0x%04X: DCBI2CIndex: 0x%02X, I2CAddress: 0x%02X\n",
2071 offset, i2c_index, i2c_address);
2072
2073 chan = init_i2c_device_find(bios->dev, i2c_index);
2074 if (!chan)
2075 return -ENODEV;
2076
2077 ret = i2c_smbus_xfer(&chan->adapter, i2c_address, 0,
2078 I2C_SMBUS_READ, reg,
2079 I2C_SMBUS_BYTE_DATA, &val);
2080 if (ret < 0) {
2081 BIOSLOG(bios, "0x%04X: I2CReg: 0x%02X, Value: [no device], "
2082 "Mask: 0x%02X, Data: 0x%02X\n",
2083 offset, reg, mask, data);
2084 iexec->execute = 0;
2085 return 6;
2086 }
2087
2088 BIOSLOG(bios, "0x%04X: I2CReg: 0x%02X, Value: 0x%02X, "
2089 "Mask: 0x%02X, Data: 0x%02X\n",
2090 offset, reg, val.byte, mask, data);
2091
2092 iexec->execute = ((val.byte & mask) == data);
2093
2094 return 6;
2095}
2096
2097static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10002098init_copy_nv_reg(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
2099{
2100 /*
2101 * INIT_COPY_NV_REG opcode: 0x5F ('_')
2102 *
2103 * offset (8 bit): opcode
2104 * offset + 1 (32 bit): src reg
2105 * offset + 5 (8 bit): shift
2106 * offset + 6 (32 bit): src mask
2107 * offset + 10 (32 bit): xor
2108 * offset + 14 (32 bit): dst reg
2109 * offset + 18 (32 bit): dst mask
2110 *
2111 * Shift REGVAL("src reg") right by (signed) "shift", AND result with
2112 * "src mask", then XOR with "xor". Write this OR'd with
2113 * (REGVAL("dst reg") AND'd with "dst mask") to "dst reg"
2114 */
2115
2116 uint32_t srcreg = *((uint32_t *)(&bios->data[offset + 1]));
2117 uint8_t shift = bios->data[offset + 5];
2118 uint32_t srcmask = *((uint32_t *)(&bios->data[offset + 6]));
2119 uint32_t xor = *((uint32_t *)(&bios->data[offset + 10]));
2120 uint32_t dstreg = *((uint32_t *)(&bios->data[offset + 14]));
2121 uint32_t dstmask = *((uint32_t *)(&bios->data[offset + 18]));
2122 uint32_t srcvalue, dstvalue;
2123
2124 if (!iexec->execute)
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00002125 return 22;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002126
2127 BIOSLOG(bios, "0x%04X: SrcReg: 0x%08X, Shift: 0x%02X, SrcMask: 0x%08X, "
2128 "Xor: 0x%08X, DstReg: 0x%08X, DstMask: 0x%08X\n",
2129 offset, srcreg, shift, srcmask, xor, dstreg, dstmask);
2130
2131 srcvalue = bios_rd32(bios, srcreg);
2132
2133 if (shift < 0x80)
2134 srcvalue >>= shift;
2135 else
2136 srcvalue <<= (0x100 - shift);
2137
2138 srcvalue = (srcvalue & srcmask) ^ xor;
2139
2140 dstvalue = bios_rd32(bios, dstreg) & dstmask;
2141
2142 bios_wr32(bios, dstreg, dstvalue | srcvalue);
2143
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00002144 return 22;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002145}
2146
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00002147static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10002148init_zm_index_io(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
2149{
2150 /*
2151 * INIT_ZM_INDEX_IO opcode: 0x62 ('b')
2152 *
2153 * offset (8 bit): opcode
2154 * offset + 1 (16 bit): CRTC port
2155 * offset + 3 (8 bit): CRTC index
2156 * offset + 4 (8 bit): data
2157 *
2158 * Write "data" to index "CRTC index" of "CRTC port"
2159 */
2160 uint16_t crtcport = ROM16(bios->data[offset + 1]);
2161 uint8_t crtcindex = bios->data[offset + 3];
2162 uint8_t data = bios->data[offset + 4];
2163
2164 if (!iexec->execute)
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00002165 return 5;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002166
2167 bios_idxprt_wr(bios, crtcport, crtcindex, data);
2168
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00002169 return 5;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002170}
2171
Francisco Jerez67eda202010-07-13 15:59:50 +02002172static inline void
2173bios_md32(struct nvbios *bios, uint32_t reg,
2174 uint32_t mask, uint32_t val)
2175{
2176 bios_wr32(bios, reg, (bios_rd32(bios, reg) & ~mask) | val);
2177}
2178
2179static uint32_t
2180peek_fb(struct drm_device *dev, struct io_mapping *fb,
2181 uint32_t off)
2182{
2183 uint32_t val = 0;
2184
2185 if (off < pci_resource_len(dev->pdev, 1)) {
Ben Skeggs625db6b2010-08-17 12:02:43 +10002186 uint8_t __iomem *p =
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -07002187 io_mapping_map_atomic_wc(fb, off & PAGE_MASK);
Francisco Jerez67eda202010-07-13 15:59:50 +02002188
Francisco Jerez0bf9b0e2010-08-04 05:10:57 +02002189 val = ioread32(p + (off & ~PAGE_MASK));
Francisco Jerez67eda202010-07-13 15:59:50 +02002190
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -07002191 io_mapping_unmap_atomic(p);
Francisco Jerez67eda202010-07-13 15:59:50 +02002192 }
2193
2194 return val;
2195}
2196
2197static void
2198poke_fb(struct drm_device *dev, struct io_mapping *fb,
2199 uint32_t off, uint32_t val)
2200{
2201 if (off < pci_resource_len(dev->pdev, 1)) {
Ben Skeggs625db6b2010-08-17 12:02:43 +10002202 uint8_t __iomem *p =
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -07002203 io_mapping_map_atomic_wc(fb, off & PAGE_MASK);
Francisco Jerez67eda202010-07-13 15:59:50 +02002204
Francisco Jerez0bf9b0e2010-08-04 05:10:57 +02002205 iowrite32(val, p + (off & ~PAGE_MASK));
Francisco Jerez67eda202010-07-13 15:59:50 +02002206 wmb();
2207
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -07002208 io_mapping_unmap_atomic(p);
Francisco Jerez67eda202010-07-13 15:59:50 +02002209 }
2210}
2211
2212static inline bool
2213read_back_fb(struct drm_device *dev, struct io_mapping *fb,
2214 uint32_t off, uint32_t val)
2215{
2216 poke_fb(dev, fb, off, val);
2217 return val == peek_fb(dev, fb, off);
2218}
2219
2220static int
2221nv04_init_compute_mem(struct nvbios *bios)
2222{
2223 struct drm_device *dev = bios->dev;
2224 uint32_t patt = 0xdeadbeef;
2225 struct io_mapping *fb;
2226 int i;
2227
2228 /* Map the framebuffer aperture */
2229 fb = io_mapping_create_wc(pci_resource_start(dev->pdev, 1),
2230 pci_resource_len(dev->pdev, 1));
2231 if (!fb)
2232 return -ENOMEM;
2233
2234 /* Sequencer and refresh off */
2235 NVWriteVgaSeq(dev, 0, 1, NVReadVgaSeq(dev, 0, 1) | 0x20);
2236 bios_md32(bios, NV04_PFB_DEBUG_0, 0, NV04_PFB_DEBUG_0_REFRESH_OFF);
2237
2238 bios_md32(bios, NV04_PFB_BOOT_0, ~0,
2239 NV04_PFB_BOOT_0_RAM_AMOUNT_16MB |
2240 NV04_PFB_BOOT_0_RAM_WIDTH_128 |
2241 NV04_PFB_BOOT_0_RAM_TYPE_SGRAM_16MBIT);
2242
2243 for (i = 0; i < 4; i++)
2244 poke_fb(dev, fb, 4 * i, patt);
2245
2246 poke_fb(dev, fb, 0x400000, patt + 1);
2247
2248 if (peek_fb(dev, fb, 0) == patt + 1) {
2249 bios_md32(bios, NV04_PFB_BOOT_0, NV04_PFB_BOOT_0_RAM_TYPE,
2250 NV04_PFB_BOOT_0_RAM_TYPE_SDRAM_16MBIT);
2251 bios_md32(bios, NV04_PFB_DEBUG_0,
2252 NV04_PFB_DEBUG_0_REFRESH_OFF, 0);
2253
2254 for (i = 0; i < 4; i++)
2255 poke_fb(dev, fb, 4 * i, patt);
2256
2257 if ((peek_fb(dev, fb, 0xc) & 0xffff) != (patt & 0xffff))
2258 bios_md32(bios, NV04_PFB_BOOT_0,
2259 NV04_PFB_BOOT_0_RAM_WIDTH_128 |
2260 NV04_PFB_BOOT_0_RAM_AMOUNT,
2261 NV04_PFB_BOOT_0_RAM_AMOUNT_8MB);
2262
2263 } else if ((peek_fb(dev, fb, 0xc) & 0xffff0000) !=
2264 (patt & 0xffff0000)) {
2265 bios_md32(bios, NV04_PFB_BOOT_0,
2266 NV04_PFB_BOOT_0_RAM_WIDTH_128 |
2267 NV04_PFB_BOOT_0_RAM_AMOUNT,
2268 NV04_PFB_BOOT_0_RAM_AMOUNT_4MB);
2269
Francisco Jerez0746b5d2010-08-05 22:58:42 +02002270 } else if (peek_fb(dev, fb, 0) != patt) {
Francisco Jerez67eda202010-07-13 15:59:50 +02002271 if (read_back_fb(dev, fb, 0x800000, patt))
2272 bios_md32(bios, NV04_PFB_BOOT_0,
2273 NV04_PFB_BOOT_0_RAM_AMOUNT,
2274 NV04_PFB_BOOT_0_RAM_AMOUNT_8MB);
2275 else
2276 bios_md32(bios, NV04_PFB_BOOT_0,
2277 NV04_PFB_BOOT_0_RAM_AMOUNT,
2278 NV04_PFB_BOOT_0_RAM_AMOUNT_4MB);
2279
2280 bios_md32(bios, NV04_PFB_BOOT_0, NV04_PFB_BOOT_0_RAM_TYPE,
2281 NV04_PFB_BOOT_0_RAM_TYPE_SGRAM_8MBIT);
2282
2283 } else if (!read_back_fb(dev, fb, 0x800000, patt)) {
2284 bios_md32(bios, NV04_PFB_BOOT_0, NV04_PFB_BOOT_0_RAM_AMOUNT,
2285 NV04_PFB_BOOT_0_RAM_AMOUNT_8MB);
2286
2287 }
2288
2289 /* Refresh on, sequencer on */
2290 bios_md32(bios, NV04_PFB_DEBUG_0, NV04_PFB_DEBUG_0_REFRESH_OFF, 0);
2291 NVWriteVgaSeq(dev, 0, 1, NVReadVgaSeq(dev, 0, 1) & ~0x20);
2292
2293 io_mapping_free(fb);
2294 return 0;
2295}
2296
2297static const uint8_t *
2298nv05_memory_config(struct nvbios *bios)
2299{
2300 /* Defaults for BIOSes lacking a memory config table */
2301 static const uint8_t default_config_tab[][2] = {
2302 { 0x24, 0x00 },
2303 { 0x28, 0x00 },
2304 { 0x24, 0x01 },
2305 { 0x1f, 0x00 },
2306 { 0x0f, 0x00 },
2307 { 0x17, 0x00 },
2308 { 0x06, 0x00 },
2309 { 0x00, 0x00 }
2310 };
2311 int i = (bios_rd32(bios, NV_PEXTDEV_BOOT_0) &
2312 NV_PEXTDEV_BOOT_0_RAMCFG) >> 2;
2313
2314 if (bios->legacy.mem_init_tbl_ptr)
2315 return &bios->data[bios->legacy.mem_init_tbl_ptr + 2 * i];
2316 else
2317 return default_config_tab[i];
2318}
2319
2320static int
2321nv05_init_compute_mem(struct nvbios *bios)
2322{
2323 struct drm_device *dev = bios->dev;
2324 const uint8_t *ramcfg = nv05_memory_config(bios);
2325 uint32_t patt = 0xdeadbeef;
2326 struct io_mapping *fb;
2327 int i, v;
2328
2329 /* Map the framebuffer aperture */
2330 fb = io_mapping_create_wc(pci_resource_start(dev->pdev, 1),
2331 pci_resource_len(dev->pdev, 1));
2332 if (!fb)
2333 return -ENOMEM;
2334
2335 /* Sequencer off */
2336 NVWriteVgaSeq(dev, 0, 1, NVReadVgaSeq(dev, 0, 1) | 0x20);
2337
2338 if (bios_rd32(bios, NV04_PFB_BOOT_0) & NV04_PFB_BOOT_0_UMA_ENABLE)
2339 goto out;
2340
2341 bios_md32(bios, NV04_PFB_DEBUG_0, NV04_PFB_DEBUG_0_REFRESH_OFF, 0);
2342
2343 /* If present load the hardcoded scrambling table */
2344 if (bios->legacy.mem_init_tbl_ptr) {
2345 uint32_t *scramble_tab = (uint32_t *)&bios->data[
2346 bios->legacy.mem_init_tbl_ptr + 0x10];
2347
2348 for (i = 0; i < 8; i++)
2349 bios_wr32(bios, NV04_PFB_SCRAMBLE(i),
2350 ROM32(scramble_tab[i]));
2351 }
2352
2353 /* Set memory type/width/length defaults depending on the straps */
2354 bios_md32(bios, NV04_PFB_BOOT_0, 0x3f, ramcfg[0]);
2355
2356 if (ramcfg[1] & 0x80)
2357 bios_md32(bios, NV04_PFB_CFG0, 0, NV04_PFB_CFG0_SCRAMBLE);
2358
2359 bios_md32(bios, NV04_PFB_CFG1, 0x700001, (ramcfg[1] & 1) << 20);
2360 bios_md32(bios, NV04_PFB_CFG1, 0, 1);
2361
2362 /* Probe memory bus width */
2363 for (i = 0; i < 4; i++)
2364 poke_fb(dev, fb, 4 * i, patt);
2365
2366 if (peek_fb(dev, fb, 0xc) != patt)
2367 bios_md32(bios, NV04_PFB_BOOT_0,
2368 NV04_PFB_BOOT_0_RAM_WIDTH_128, 0);
2369
2370 /* Probe memory length */
2371 v = bios_rd32(bios, NV04_PFB_BOOT_0) & NV04_PFB_BOOT_0_RAM_AMOUNT;
2372
2373 if (v == NV04_PFB_BOOT_0_RAM_AMOUNT_32MB &&
2374 (!read_back_fb(dev, fb, 0x1000000, ++patt) ||
2375 !read_back_fb(dev, fb, 0, ++patt)))
2376 bios_md32(bios, NV04_PFB_BOOT_0, NV04_PFB_BOOT_0_RAM_AMOUNT,
2377 NV04_PFB_BOOT_0_RAM_AMOUNT_16MB);
2378
2379 if (v == NV04_PFB_BOOT_0_RAM_AMOUNT_16MB &&
2380 !read_back_fb(dev, fb, 0x800000, ++patt))
2381 bios_md32(bios, NV04_PFB_BOOT_0, NV04_PFB_BOOT_0_RAM_AMOUNT,
2382 NV04_PFB_BOOT_0_RAM_AMOUNT_8MB);
2383
2384 if (!read_back_fb(dev, fb, 0x400000, ++patt))
2385 bios_md32(bios, NV04_PFB_BOOT_0, NV04_PFB_BOOT_0_RAM_AMOUNT,
2386 NV04_PFB_BOOT_0_RAM_AMOUNT_4MB);
2387
2388out:
2389 /* Sequencer on */
2390 NVWriteVgaSeq(dev, 0, 1, NVReadVgaSeq(dev, 0, 1) & ~0x20);
2391
2392 io_mapping_free(fb);
2393 return 0;
2394}
2395
2396static int
2397nv10_init_compute_mem(struct nvbios *bios)
2398{
2399 struct drm_device *dev = bios->dev;
2400 struct drm_nouveau_private *dev_priv = bios->dev->dev_private;
2401 const int mem_width[] = { 0x10, 0x00, 0x20 };
2402 const int mem_width_count = (dev_priv->chipset >= 0x17 ? 3 : 2);
2403 uint32_t patt = 0xdeadbeef;
2404 struct io_mapping *fb;
2405 int i, j, k;
2406
2407 /* Map the framebuffer aperture */
2408 fb = io_mapping_create_wc(pci_resource_start(dev->pdev, 1),
2409 pci_resource_len(dev->pdev, 1));
2410 if (!fb)
2411 return -ENOMEM;
2412
2413 bios_wr32(bios, NV10_PFB_REFCTRL, NV10_PFB_REFCTRL_VALID_1);
2414
2415 /* Probe memory bus width */
2416 for (i = 0; i < mem_width_count; i++) {
2417 bios_md32(bios, NV04_PFB_CFG0, 0x30, mem_width[i]);
2418
2419 for (j = 0; j < 4; j++) {
2420 for (k = 0; k < 4; k++)
2421 poke_fb(dev, fb, 0x1c, 0);
2422
2423 poke_fb(dev, fb, 0x1c, patt);
2424 poke_fb(dev, fb, 0x3c, 0);
2425
2426 if (peek_fb(dev, fb, 0x1c) == patt)
2427 goto mem_width_found;
2428 }
2429 }
2430
2431mem_width_found:
2432 patt <<= 1;
2433
2434 /* Probe amount of installed memory */
2435 for (i = 0; i < 4; i++) {
2436 int off = bios_rd32(bios, NV04_PFB_FIFO_DATA) - 0x100000;
2437
2438 poke_fb(dev, fb, off, patt);
2439 poke_fb(dev, fb, 0, 0);
2440
2441 peek_fb(dev, fb, 0);
2442 peek_fb(dev, fb, 0);
2443 peek_fb(dev, fb, 0);
2444 peek_fb(dev, fb, 0);
2445
2446 if (peek_fb(dev, fb, off) == patt)
2447 goto amount_found;
2448 }
2449
2450 /* IC missing - disable the upper half memory space. */
2451 bios_md32(bios, NV04_PFB_CFG0, 0x1000, 0);
2452
2453amount_found:
2454 io_mapping_free(fb);
2455 return 0;
2456}
2457
2458static int
2459nv20_init_compute_mem(struct nvbios *bios)
2460{
2461 struct drm_device *dev = bios->dev;
2462 struct drm_nouveau_private *dev_priv = bios->dev->dev_private;
2463 uint32_t mask = (dev_priv->chipset >= 0x25 ? 0x300 : 0x900);
2464 uint32_t amount, off;
2465 struct io_mapping *fb;
2466
2467 /* Map the framebuffer aperture */
2468 fb = io_mapping_create_wc(pci_resource_start(dev->pdev, 1),
2469 pci_resource_len(dev->pdev, 1));
2470 if (!fb)
2471 return -ENOMEM;
2472
2473 bios_wr32(bios, NV10_PFB_REFCTRL, NV10_PFB_REFCTRL_VALID_1);
2474
2475 /* Allow full addressing */
2476 bios_md32(bios, NV04_PFB_CFG0, 0, mask);
2477
2478 amount = bios_rd32(bios, NV04_PFB_FIFO_DATA);
2479 for (off = amount; off > 0x2000000; off -= 0x2000000)
2480 poke_fb(dev, fb, off - 4, off);
2481
2482 amount = bios_rd32(bios, NV04_PFB_FIFO_DATA);
2483 if (amount != peek_fb(dev, fb, amount - 4))
2484 /* IC missing - disable the upper half memory space. */
2485 bios_md32(bios, NV04_PFB_CFG0, mask, 0);
2486
2487 io_mapping_free(fb);
2488 return 0;
2489}
2490
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00002491static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10002492init_compute_mem(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
2493{
2494 /*
2495 * INIT_COMPUTE_MEM opcode: 0x63 ('c')
2496 *
2497 * offset (8 bit): opcode
2498 *
Francisco Jerez67eda202010-07-13 15:59:50 +02002499 * This opcode is meant to set the PFB memory config registers
2500 * appropriately so that we can correctly calculate how much VRAM it
2501 * has (on nv10 and better chipsets the amount of installed VRAM is
2502 * subsequently reported in NV_PFB_CSTATUS (0x10020C)).
Ben Skeggs6ee73862009-12-11 19:24:15 +10002503 *
Francisco Jerez67eda202010-07-13 15:59:50 +02002504 * The implementation of this opcode in general consists of several
2505 * parts:
Ben Skeggs6ee73862009-12-11 19:24:15 +10002506 *
Francisco Jerez67eda202010-07-13 15:59:50 +02002507 * 1) Determination of memory type and density. Only necessary for
2508 * really old chipsets, the memory type reported by the strap bits
2509 * (0x101000) is assumed to be accurate on nv05 and newer.
Ben Skeggs6ee73862009-12-11 19:24:15 +10002510 *
Francisco Jerez67eda202010-07-13 15:59:50 +02002511 * 2) Determination of the memory bus width. Usually done by a cunning
2512 * combination of writes to offsets 0x1c and 0x3c in the fb, and
2513 * seeing whether the written values are read back correctly.
Ben Skeggs6ee73862009-12-11 19:24:15 +10002514 *
Francisco Jerez67eda202010-07-13 15:59:50 +02002515 * Only necessary on nv0x-nv1x and nv34, on the other cards we can
2516 * trust the straps.
Ben Skeggs6ee73862009-12-11 19:24:15 +10002517 *
Francisco Jerez67eda202010-07-13 15:59:50 +02002518 * 3) Determination of how many of the card's RAM pads have ICs
2519 * attached, usually done by a cunning combination of writes to an
2520 * offset slightly less than the maximum memory reported by
2521 * NV_PFB_CSTATUS, then seeing if the test pattern can be read back.
Ben Skeggs6ee73862009-12-11 19:24:15 +10002522 *
Francisco Jerez67eda202010-07-13 15:59:50 +02002523 * This appears to be a NOP on IGPs and NV4x or newer chipsets, both io
2524 * logs of the VBIOS and kmmio traces of the binary driver POSTing the
2525 * card show nothing being done for this opcode. Why is it still listed
2526 * in the table?!
Ben Skeggs6ee73862009-12-11 19:24:15 +10002527 */
2528
2529 /* no iexec->execute check by design */
2530
Ben Skeggs6ee73862009-12-11 19:24:15 +10002531 struct drm_nouveau_private *dev_priv = bios->dev->dev_private;
Francisco Jerez67eda202010-07-13 15:59:50 +02002532 int ret;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002533
Francisco Jerez67eda202010-07-13 15:59:50 +02002534 if (dev_priv->chipset >= 0x40 ||
2535 dev_priv->chipset == 0x1a ||
2536 dev_priv->chipset == 0x1f)
2537 ret = 0;
2538 else if (dev_priv->chipset >= 0x20 &&
2539 dev_priv->chipset != 0x34)
2540 ret = nv20_init_compute_mem(bios);
2541 else if (dev_priv->chipset >= 0x10)
2542 ret = nv10_init_compute_mem(bios);
2543 else if (dev_priv->chipset >= 0x5)
2544 ret = nv05_init_compute_mem(bios);
2545 else
2546 ret = nv04_init_compute_mem(bios);
Ben Skeggs6ee73862009-12-11 19:24:15 +10002547
Francisco Jerez67eda202010-07-13 15:59:50 +02002548 if (ret)
2549 return ret;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002550
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00002551 return 1;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002552}
2553
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00002554static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10002555init_reset(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
2556{
2557 /*
2558 * INIT_RESET opcode: 0x65 ('e')
2559 *
2560 * offset (8 bit): opcode
2561 * offset + 1 (32 bit): register
2562 * offset + 5 (32 bit): value1
2563 * offset + 9 (32 bit): value2
2564 *
2565 * Assign "value1" to "register", then assign "value2" to "register"
2566 */
2567
2568 uint32_t reg = ROM32(bios->data[offset + 1]);
2569 uint32_t value1 = ROM32(bios->data[offset + 5]);
2570 uint32_t value2 = ROM32(bios->data[offset + 9]);
2571 uint32_t pci_nv_19, pci_nv_20;
2572
2573 /* no iexec->execute check by design */
2574
2575 pci_nv_19 = bios_rd32(bios, NV_PBUS_PCI_NV_19);
Francisco Jerez190a4372010-06-17 12:42:14 +02002576 bios_wr32(bios, NV_PBUS_PCI_NV_19, pci_nv_19 & ~0xf00);
2577
Ben Skeggs6ee73862009-12-11 19:24:15 +10002578 bios_wr32(bios, reg, value1);
2579
2580 udelay(10);
2581
2582 bios_wr32(bios, reg, value2);
2583 bios_wr32(bios, NV_PBUS_PCI_NV_19, pci_nv_19);
2584
2585 pci_nv_20 = bios_rd32(bios, NV_PBUS_PCI_NV_20);
2586 pci_nv_20 &= ~NV_PBUS_PCI_NV_20_ROM_SHADOW_ENABLED; /* 0xfffffffe */
2587 bios_wr32(bios, NV_PBUS_PCI_NV_20, pci_nv_20);
2588
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00002589 return 13;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002590}
2591
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00002592static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10002593init_configure_mem(struct nvbios *bios, uint16_t offset,
2594 struct init_exec *iexec)
2595{
2596 /*
2597 * INIT_CONFIGURE_MEM opcode: 0x66 ('f')
2598 *
2599 * offset (8 bit): opcode
2600 *
2601 * Equivalent to INIT_DONE on bios version 3 or greater.
2602 * For early bios versions, sets up the memory registers, using values
2603 * taken from the memory init table
2604 */
2605
2606 /* no iexec->execute check by design */
2607
2608 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);
2609 uint16_t seqtbloffs = bios->legacy.sdr_seq_tbl_ptr, meminitdata = meminitoffs + 6;
2610 uint32_t reg, data;
2611
2612 if (bios->major_version > 2)
Francisco Jerezae553212010-07-03 20:47:44 +02002613 return 0;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002614
2615 bios_idxprt_wr(bios, NV_VIO_SRX, NV_VIO_SR_CLOCK_INDEX, bios_idxprt_rd(
2616 bios, NV_VIO_SRX, NV_VIO_SR_CLOCK_INDEX) | 0x20);
2617
2618 if (bios->data[meminitoffs] & 1)
2619 seqtbloffs = bios->legacy.ddr_seq_tbl_ptr;
2620
2621 for (reg = ROM32(bios->data[seqtbloffs]);
2622 reg != 0xffffffff;
2623 reg = ROM32(bios->data[seqtbloffs += 4])) {
2624
2625 switch (reg) {
Francisco Jerez3c7066b2010-07-13 15:50:23 +02002626 case NV04_PFB_PRE:
2627 data = NV04_PFB_PRE_CMD_PRECHARGE;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002628 break;
Francisco Jerez3c7066b2010-07-13 15:50:23 +02002629 case NV04_PFB_PAD:
2630 data = NV04_PFB_PAD_CKE_NORMAL;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002631 break;
Francisco Jerez3c7066b2010-07-13 15:50:23 +02002632 case NV04_PFB_REF:
2633 data = NV04_PFB_REF_CMD_REFRESH;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002634 break;
2635 default:
2636 data = ROM32(bios->data[meminitdata]);
2637 meminitdata += 4;
2638 if (data == 0xffffffff)
2639 continue;
2640 }
2641
2642 bios_wr32(bios, reg, data);
2643 }
2644
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00002645 return 1;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002646}
2647
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00002648static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10002649init_configure_clk(struct nvbios *bios, uint16_t offset,
2650 struct init_exec *iexec)
2651{
2652 /*
2653 * INIT_CONFIGURE_CLK opcode: 0x67 ('g')
2654 *
2655 * offset (8 bit): opcode
2656 *
2657 * Equivalent to INIT_DONE on bios version 3 or greater.
2658 * For early bios versions, sets up the NVClk and MClk PLLs, using
2659 * values taken from the memory init table
2660 */
2661
2662 /* no iexec->execute check by design */
2663
2664 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);
2665 int clock;
2666
2667 if (bios->major_version > 2)
Francisco Jerezae553212010-07-03 20:47:44 +02002668 return 0;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002669
2670 clock = ROM16(bios->data[meminitoffs + 4]) * 10;
2671 setPLL(bios, NV_PRAMDAC_NVPLL_COEFF, clock);
2672
2673 clock = ROM16(bios->data[meminitoffs + 2]) * 10;
2674 if (bios->data[meminitoffs] & 1) /* DDR */
2675 clock *= 2;
2676 setPLL(bios, NV_PRAMDAC_MPLL_COEFF, clock);
2677
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00002678 return 1;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002679}
2680
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00002681static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10002682init_configure_preinit(struct nvbios *bios, uint16_t offset,
2683 struct init_exec *iexec)
2684{
2685 /*
2686 * INIT_CONFIGURE_PREINIT opcode: 0x68 ('h')
2687 *
2688 * offset (8 bit): opcode
2689 *
2690 * Equivalent to INIT_DONE on bios version 3 or greater.
2691 * For early bios versions, does early init, loading ram and crystal
2692 * configuration from straps into CR3C
2693 */
2694
2695 /* no iexec->execute check by design */
2696
2697 uint32_t straps = bios_rd32(bios, NV_PEXTDEV_BOOT_0);
Francisco Jerez3c9b2532010-08-04 05:15:11 +02002698 uint8_t cr3c = ((straps << 2) & 0xf0) | (straps & 0x40) >> 6;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002699
2700 if (bios->major_version > 2)
Francisco Jerezae553212010-07-03 20:47:44 +02002701 return 0;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002702
2703 bios_idxprt_wr(bios, NV_CIO_CRX__COLOR,
2704 NV_CIO_CRE_SCRATCH4__INDEX, cr3c);
2705
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00002706 return 1;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002707}
2708
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00002709static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10002710init_io(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
2711{
2712 /*
2713 * INIT_IO opcode: 0x69 ('i')
2714 *
2715 * offset (8 bit): opcode
2716 * offset + 1 (16 bit): CRTC port
2717 * offset + 3 (8 bit): mask
2718 * offset + 4 (8 bit): data
2719 *
2720 * Assign ((IOVAL("crtc port") & "mask") | "data") to "crtc port"
2721 */
2722
2723 struct drm_nouveau_private *dev_priv = bios->dev->dev_private;
2724 uint16_t crtcport = ROM16(bios->data[offset + 1]);
2725 uint8_t mask = bios->data[offset + 3];
2726 uint8_t data = bios->data[offset + 4];
2727
2728 if (!iexec->execute)
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00002729 return 5;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002730
2731 BIOSLOG(bios, "0x%04X: Port: 0x%04X, Mask: 0x%02X, Data: 0x%02X\n",
2732 offset, crtcport, mask, data);
2733
2734 /*
2735 * I have no idea what this does, but NVIDIA do this magic sequence
2736 * in the places where this INIT_IO happens..
2737 */
2738 if (dev_priv->card_type >= NV_50 && crtcport == 0x3c3 && data == 1) {
2739 int i;
2740
2741 bios_wr32(bios, 0x614100, (bios_rd32(
2742 bios, 0x614100) & 0x0fffffff) | 0x00800000);
2743
2744 bios_wr32(bios, 0x00e18c, bios_rd32(
2745 bios, 0x00e18c) | 0x00020000);
2746
2747 bios_wr32(bios, 0x614900, (bios_rd32(
2748 bios, 0x614900) & 0x0fffffff) | 0x00800000);
2749
2750 bios_wr32(bios, 0x000200, bios_rd32(
2751 bios, 0x000200) & ~0x40000000);
2752
2753 mdelay(10);
2754
2755 bios_wr32(bios, 0x00e18c, bios_rd32(
2756 bios, 0x00e18c) & ~0x00020000);
2757
2758 bios_wr32(bios, 0x000200, bios_rd32(
2759 bios, 0x000200) | 0x40000000);
2760
2761 bios_wr32(bios, 0x614100, 0x00800018);
2762 bios_wr32(bios, 0x614900, 0x00800018);
2763
2764 mdelay(10);
2765
2766 bios_wr32(bios, 0x614100, 0x10000018);
2767 bios_wr32(bios, 0x614900, 0x10000018);
2768
2769 for (i = 0; i < 3; i++)
2770 bios_wr32(bios, 0x614280 + (i*0x800), bios_rd32(
2771 bios, 0x614280 + (i*0x800)) & 0xf0f0f0f0);
2772
2773 for (i = 0; i < 2; i++)
2774 bios_wr32(bios, 0x614300 + (i*0x800), bios_rd32(
2775 bios, 0x614300 + (i*0x800)) & 0xfffff0f0);
2776
2777 for (i = 0; i < 3; i++)
2778 bios_wr32(bios, 0x614380 + (i*0x800), bios_rd32(
2779 bios, 0x614380 + (i*0x800)) & 0xfffff0f0);
2780
2781 for (i = 0; i < 2; i++)
2782 bios_wr32(bios, 0x614200 + (i*0x800), bios_rd32(
2783 bios, 0x614200 + (i*0x800)) & 0xfffffff0);
2784
2785 for (i = 0; i < 2; i++)
2786 bios_wr32(bios, 0x614108 + (i*0x800), bios_rd32(
2787 bios, 0x614108 + (i*0x800)) & 0x0fffffff);
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00002788 return 5;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002789 }
2790
2791 bios_port_wr(bios, crtcport, (bios_port_rd(bios, crtcport) & mask) |
2792 data);
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00002793 return 5;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002794}
2795
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00002796static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10002797init_sub(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
2798{
2799 /*
2800 * INIT_SUB opcode: 0x6B ('k')
2801 *
2802 * offset (8 bit): opcode
2803 * offset + 1 (8 bit): script number
2804 *
2805 * Execute script number "script number", as a subroutine
2806 */
2807
2808 uint8_t sub = bios->data[offset + 1];
2809
2810 if (!iexec->execute)
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00002811 return 2;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002812
2813 BIOSLOG(bios, "0x%04X: Calling script %d\n", offset, sub);
2814
2815 parse_init_table(bios,
2816 ROM16(bios->data[bios->init_script_tbls_ptr + sub * 2]),
2817 iexec);
2818
2819 BIOSLOG(bios, "0x%04X: End of script %d\n", offset, sub);
2820
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00002821 return 2;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002822}
2823
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00002824static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10002825init_ram_condition(struct nvbios *bios, uint16_t offset,
2826 struct init_exec *iexec)
2827{
2828 /*
2829 * INIT_RAM_CONDITION opcode: 0x6D ('m')
2830 *
2831 * offset (8 bit): opcode
2832 * offset + 1 (8 bit): mask
2833 * offset + 2 (8 bit): cmpval
2834 *
Francisco Jerez3c7066b2010-07-13 15:50:23 +02002835 * Test if (NV04_PFB_BOOT_0 & "mask") equals "cmpval".
Ben Skeggs6ee73862009-12-11 19:24:15 +10002836 * If condition not met skip subsequent opcodes until condition is
2837 * inverted (INIT_NOT), or we hit INIT_RESUME
2838 */
2839
2840 uint8_t mask = bios->data[offset + 1];
2841 uint8_t cmpval = bios->data[offset + 2];
2842 uint8_t data;
2843
2844 if (!iexec->execute)
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00002845 return 3;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002846
Francisco Jerez3c7066b2010-07-13 15:50:23 +02002847 data = bios_rd32(bios, NV04_PFB_BOOT_0) & mask;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002848
2849 BIOSLOG(bios, "0x%04X: Checking if 0x%08X equals 0x%08X\n",
2850 offset, data, cmpval);
2851
2852 if (data == cmpval)
2853 BIOSLOG(bios, "0x%04X: Condition fulfilled -- continuing to execute\n", offset);
2854 else {
2855 BIOSLOG(bios, "0x%04X: Condition not fulfilled -- skipping following commands\n", offset);
2856 iexec->execute = false;
2857 }
2858
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00002859 return 3;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002860}
2861
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00002862static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10002863init_nv_reg(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
2864{
2865 /*
2866 * INIT_NV_REG opcode: 0x6E ('n')
2867 *
2868 * offset (8 bit): opcode
2869 * offset + 1 (32 bit): register
2870 * offset + 5 (32 bit): mask
2871 * offset + 9 (32 bit): data
2872 *
2873 * Assign ((REGVAL("register") & "mask") | "data") to "register"
2874 */
2875
2876 uint32_t reg = ROM32(bios->data[offset + 1]);
2877 uint32_t mask = ROM32(bios->data[offset + 5]);
2878 uint32_t data = ROM32(bios->data[offset + 9]);
2879
2880 if (!iexec->execute)
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00002881 return 13;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002882
2883 BIOSLOG(bios, "0x%04X: Reg: 0x%08X, Mask: 0x%08X, Data: 0x%08X\n",
2884 offset, reg, mask, data);
2885
2886 bios_wr32(bios, reg, (bios_rd32(bios, reg) & mask) | data);
2887
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00002888 return 13;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002889}
2890
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00002891static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10002892init_macro(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
2893{
2894 /*
2895 * INIT_MACRO opcode: 0x6F ('o')
2896 *
2897 * offset (8 bit): opcode
2898 * offset + 1 (8 bit): macro number
2899 *
2900 * Look up macro index "macro number" in the macro index table.
2901 * The macro index table entry has 1 byte for the index in the macro
2902 * table, and 1 byte for the number of times to repeat the macro.
2903 * The macro table entry has 4 bytes for the register address and
2904 * 4 bytes for the value to write to that register
2905 */
2906
2907 uint8_t macro_index_tbl_idx = bios->data[offset + 1];
2908 uint16_t tmp = bios->macro_index_tbl_ptr + (macro_index_tbl_idx * MACRO_INDEX_SIZE);
2909 uint8_t macro_tbl_idx = bios->data[tmp];
2910 uint8_t count = bios->data[tmp + 1];
2911 uint32_t reg, data;
2912 int i;
2913
2914 if (!iexec->execute)
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00002915 return 2;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002916
2917 BIOSLOG(bios, "0x%04X: Macro: 0x%02X, MacroTableIndex: 0x%02X, "
2918 "Count: 0x%02X\n",
2919 offset, macro_index_tbl_idx, macro_tbl_idx, count);
2920
2921 for (i = 0; i < count; i++) {
2922 uint16_t macroentryptr = bios->macro_tbl_ptr + (macro_tbl_idx + i) * MACRO_SIZE;
2923
2924 reg = ROM32(bios->data[macroentryptr]);
2925 data = ROM32(bios->data[macroentryptr + 4]);
2926
2927 bios_wr32(bios, reg, data);
2928 }
2929
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00002930 return 2;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002931}
2932
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00002933static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10002934init_done(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
2935{
2936 /*
2937 * INIT_DONE opcode: 0x71 ('q')
2938 *
2939 * offset (8 bit): opcode
2940 *
2941 * End the current script
2942 */
2943
2944 /* mild retval abuse to stop parsing this table */
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00002945 return 0;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002946}
2947
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00002948static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10002949init_resume(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
2950{
2951 /*
2952 * INIT_RESUME opcode: 0x72 ('r')
2953 *
2954 * offset (8 bit): opcode
2955 *
2956 * End the current execute / no-execute condition
2957 */
2958
2959 if (iexec->execute)
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00002960 return 1;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002961
2962 iexec->execute = true;
2963 BIOSLOG(bios, "0x%04X: ---- Executing following commands ----\n", offset);
2964
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00002965 return 1;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002966}
2967
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00002968static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10002969init_time(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
2970{
2971 /*
2972 * INIT_TIME opcode: 0x74 ('t')
2973 *
2974 * offset (8 bit): opcode
2975 * offset + 1 (16 bit): time
2976 *
2977 * Sleep for "time" microseconds.
2978 */
2979
2980 unsigned time = ROM16(bios->data[offset + 1]);
2981
2982 if (!iexec->execute)
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00002983 return 3;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002984
2985 BIOSLOG(bios, "0x%04X: Sleeping for 0x%04X microseconds\n",
2986 offset, time);
2987
2988 if (time < 1000)
2989 udelay(time);
2990 else
Ben Skeggsc7ca4d12011-02-03 20:10:49 +10002991 mdelay((time + 900) / 1000);
Ben Skeggs6ee73862009-12-11 19:24:15 +10002992
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00002993 return 3;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002994}
2995
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00002996static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10002997init_condition(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
2998{
2999 /*
3000 * INIT_CONDITION opcode: 0x75 ('u')
3001 *
3002 * offset (8 bit): opcode
3003 * offset + 1 (8 bit): condition number
3004 *
3005 * Check condition "condition number" in the condition table.
3006 * If condition not met skip subsequent opcodes until condition is
3007 * inverted (INIT_NOT), or we hit INIT_RESUME
3008 */
3009
3010 uint8_t cond = bios->data[offset + 1];
3011
3012 if (!iexec->execute)
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00003013 return 2;
Ben Skeggs6ee73862009-12-11 19:24:15 +10003014
3015 BIOSLOG(bios, "0x%04X: Condition: 0x%02X\n", offset, cond);
3016
3017 if (bios_condition_met(bios, offset, cond))
3018 BIOSLOG(bios, "0x%04X: Condition fulfilled -- continuing to execute\n", offset);
3019 else {
3020 BIOSLOG(bios, "0x%04X: Condition not fulfilled -- skipping following commands\n", offset);
3021 iexec->execute = false;
3022 }
3023
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00003024 return 2;
Ben Skeggs6ee73862009-12-11 19:24:15 +10003025}
3026
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00003027static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10003028init_io_condition(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
3029{
3030 /*
3031 * INIT_IO_CONDITION opcode: 0x76
3032 *
3033 * offset (8 bit): opcode
3034 * offset + 1 (8 bit): condition number
3035 *
3036 * Check condition "condition number" in the io condition table.
3037 * If condition not met skip subsequent opcodes until condition is
3038 * inverted (INIT_NOT), or we hit INIT_RESUME
3039 */
3040
3041 uint8_t cond = bios->data[offset + 1];
3042
3043 if (!iexec->execute)
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00003044 return 2;
Ben Skeggs6ee73862009-12-11 19:24:15 +10003045
3046 BIOSLOG(bios, "0x%04X: IO condition: 0x%02X\n", offset, cond);
3047
3048 if (io_condition_met(bios, offset, cond))
3049 BIOSLOG(bios, "0x%04X: Condition fulfilled -- continuing to execute\n", offset);
3050 else {
3051 BIOSLOG(bios, "0x%04X: Condition not fulfilled -- skipping following commands\n", offset);
3052 iexec->execute = false;
3053 }
3054
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00003055 return 2;
Ben Skeggs6ee73862009-12-11 19:24:15 +10003056}
3057
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00003058static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10003059init_index_io(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
3060{
3061 /*
3062 * INIT_INDEX_IO opcode: 0x78 ('x')
3063 *
3064 * offset (8 bit): opcode
3065 * offset + 1 (16 bit): CRTC port
3066 * offset + 3 (8 bit): CRTC index
3067 * offset + 4 (8 bit): mask
3068 * offset + 5 (8 bit): data
3069 *
3070 * Read value at index "CRTC index" on "CRTC port", AND with "mask",
3071 * OR with "data", write-back
3072 */
3073
3074 uint16_t crtcport = ROM16(bios->data[offset + 1]);
3075 uint8_t crtcindex = bios->data[offset + 3];
3076 uint8_t mask = bios->data[offset + 4];
3077 uint8_t data = bios->data[offset + 5];
3078 uint8_t value;
3079
3080 if (!iexec->execute)
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00003081 return 6;
Ben Skeggs6ee73862009-12-11 19:24:15 +10003082
3083 BIOSLOG(bios, "0x%04X: Port: 0x%04X, Index: 0x%02X, Mask: 0x%02X, "
3084 "Data: 0x%02X\n",
3085 offset, crtcport, crtcindex, mask, data);
3086
3087 value = (bios_idxprt_rd(bios, crtcport, crtcindex) & mask) | data;
3088 bios_idxprt_wr(bios, crtcport, crtcindex, value);
3089
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00003090 return 6;
Ben Skeggs6ee73862009-12-11 19:24:15 +10003091}
3092
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00003093static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10003094init_pll(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
3095{
3096 /*
3097 * INIT_PLL opcode: 0x79 ('y')
3098 *
3099 * offset (8 bit): opcode
3100 * offset + 1 (32 bit): register
3101 * offset + 5 (16 bit): freq
3102 *
3103 * Set PLL register "register" to coefficients for frequency (10kHz)
3104 * "freq"
3105 */
3106
3107 uint32_t reg = ROM32(bios->data[offset + 1]);
3108 uint16_t freq = ROM16(bios->data[offset + 5]);
3109
3110 if (!iexec->execute)
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00003111 return 7;
Ben Skeggs6ee73862009-12-11 19:24:15 +10003112
3113 BIOSLOG(bios, "0x%04X: Reg: 0x%08X, Freq: %d0kHz\n", offset, reg, freq);
3114
3115 setPLL(bios, reg, freq * 10);
3116
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00003117 return 7;
Ben Skeggs6ee73862009-12-11 19:24:15 +10003118}
3119
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00003120static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10003121init_zm_reg(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
3122{
3123 /*
3124 * INIT_ZM_REG opcode: 0x7A ('z')
3125 *
3126 * offset (8 bit): opcode
3127 * offset + 1 (32 bit): register
3128 * offset + 5 (32 bit): value
3129 *
3130 * Assign "value" to "register"
3131 */
3132
3133 uint32_t reg = ROM32(bios->data[offset + 1]);
3134 uint32_t value = ROM32(bios->data[offset + 5]);
3135
3136 if (!iexec->execute)
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00003137 return 9;
Ben Skeggs6ee73862009-12-11 19:24:15 +10003138
3139 if (reg == 0x000200)
3140 value |= 1;
3141
3142 bios_wr32(bios, reg, value);
3143
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00003144 return 9;
Ben Skeggs6ee73862009-12-11 19:24:15 +10003145}
3146
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00003147static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10003148init_ram_restrict_pll(struct nvbios *bios, uint16_t offset,
3149 struct init_exec *iexec)
3150{
3151 /*
3152 * INIT_RAM_RESTRICT_PLL opcode: 0x87 ('')
3153 *
3154 * offset (8 bit): opcode
3155 * offset + 1 (8 bit): PLL type
3156 * offset + 2 (32 bit): frequency 0
3157 *
3158 * Uses the RAMCFG strap of PEXTDEV_BOOT as an index into the table at
3159 * ram_restrict_table_ptr. The value read from there is used to select
3160 * a frequency from the table starting at 'frequency 0' to be
3161 * programmed into the PLL corresponding to 'type'.
3162 *
3163 * The PLL limits table on cards using this opcode has a mapping of
3164 * 'type' to the relevant registers.
3165 */
3166
3167 struct drm_device *dev = bios->dev;
3168 uint32_t strap = (bios_rd32(bios, NV_PEXTDEV_BOOT_0) & 0x0000003c) >> 2;
3169 uint8_t index = bios->data[bios->ram_restrict_tbl_ptr + strap];
3170 uint8_t type = bios->data[offset + 1];
3171 uint32_t freq = ROM32(bios->data[offset + 2 + (index * 4)]);
3172 uint8_t *pll_limits = &bios->data[bios->pll_limit_tbl_ptr], *entry;
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00003173 int len = 2 + bios->ram_restrict_group_count * 4;
Ben Skeggs6ee73862009-12-11 19:24:15 +10003174 int i;
3175
3176 if (!iexec->execute)
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00003177 return len;
Ben Skeggs6ee73862009-12-11 19:24:15 +10003178
3179 if (!bios->pll_limit_tbl_ptr || (pll_limits[0] & 0xf0) != 0x30) {
3180 NV_ERROR(dev, "PLL limits table not version 3.x\n");
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00003181 return len; /* deliberate, allow default clocks to remain */
Ben Skeggs6ee73862009-12-11 19:24:15 +10003182 }
3183
3184 entry = pll_limits + pll_limits[1];
3185 for (i = 0; i < pll_limits[3]; i++, entry += pll_limits[2]) {
3186 if (entry[0] == type) {
3187 uint32_t reg = ROM32(entry[3]);
3188
3189 BIOSLOG(bios, "0x%04X: "
3190 "Type %02x Reg 0x%08x Freq %dKHz\n",
3191 offset, type, reg, freq);
3192
3193 setPLL(bios, reg, freq);
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00003194 return len;
Ben Skeggs6ee73862009-12-11 19:24:15 +10003195 }
3196 }
3197
3198 NV_ERROR(dev, "PLL type 0x%02x not found in PLL limits table", type);
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00003199 return len;
Ben Skeggs6ee73862009-12-11 19:24:15 +10003200}
3201
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00003202static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10003203init_8c(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
3204{
3205 /*
3206 * INIT_8C opcode: 0x8C ('')
3207 *
3208 * NOP so far....
3209 *
3210 */
3211
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00003212 return 1;
Ben Skeggs6ee73862009-12-11 19:24:15 +10003213}
3214
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00003215static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10003216init_8d(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
3217{
3218 /*
3219 * INIT_8D opcode: 0x8D ('')
3220 *
3221 * NOP so far....
3222 *
3223 */
3224
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00003225 return 1;
Ben Skeggs6ee73862009-12-11 19:24:15 +10003226}
3227
Ben Skeggs75139062011-07-03 13:40:01 +10003228static void
3229init_gpio_unknv50(struct nvbios *bios, struct dcb_gpio_entry *gpio)
3230{
3231 const uint32_t nv50_gpio_ctl[2] = { 0xe100, 0xe28c };
3232 u32 r, s, v;
3233
3234 /* Not a clue, needs de-magicing */
3235 r = nv50_gpio_ctl[gpio->line >> 4];
3236 s = (gpio->line & 0x0f);
3237 v = bios_rd32(bios, r) & ~(0x00010001 << s);
3238 switch ((gpio->entry & 0x06000000) >> 25) {
3239 case 1:
3240 v |= (0x00000001 << s);
3241 break;
3242 case 2:
3243 v |= (0x00010000 << s);
3244 break;
3245 default:
3246 break;
3247 }
3248
3249 bios_wr32(bios, r, v);
3250}
3251
3252static void
3253init_gpio_unknvd0(struct nvbios *bios, struct dcb_gpio_entry *gpio)
3254{
3255 u32 v, i;
3256
3257 v = bios_rd32(bios, 0x00d610 + (gpio->line * 4));
3258 v &= 0xffffff00;
3259 v |= (gpio->entry & 0x00ff0000) >> 16;
3260 bios_wr32(bios, 0x00d610 + (gpio->line * 4), v);
3261
3262 i = (gpio->entry & 0x1f000000) >> 24;
3263 if (i) {
3264 v = bios_rd32(bios, 0x00d640 + ((i - 1) * 4));
3265 v &= 0xffffff00;
3266 v |= gpio->line;
3267 bios_wr32(bios, 0x00d640 + ((i - 1) * 4), v);
3268 }
3269}
3270
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00003271static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10003272init_gpio(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
3273{
3274 /*
3275 * INIT_GPIO opcode: 0x8E ('')
3276 *
3277 * offset (8 bit): opcode
3278 *
3279 * Loop over all entries in the DCB GPIO table, and initialise
3280 * each GPIO according to various values listed in each entry
3281 */
3282
Ben Skeggs2535d712010-04-07 12:00:14 +10003283 struct drm_nouveau_private *dev_priv = bios->dev->dev_private;
Ben Skeggsee2e0132010-07-26 09:28:25 +10003284 struct nouveau_gpio_engine *pgpio = &dev_priv->engine.gpio;
Ben Skeggs6ee73862009-12-11 19:24:15 +10003285 int i;
3286
Ben Skeggs080feda2010-08-04 13:40:50 +10003287 if (dev_priv->card_type < NV_50) {
Ben Skeggs2535d712010-04-07 12:00:14 +10003288 NV_ERROR(bios->dev, "INIT_GPIO on unsupported chipset\n");
Ben Skeggs309b8c82010-06-29 16:09:24 +10003289 return 1;
Ben Skeggs2535d712010-04-07 12:00:14 +10003290 }
3291
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00003292 if (!iexec->execute)
3293 return 1;
3294
Ben Skeggs2535d712010-04-07 12:00:14 +10003295 for (i = 0; i < bios->dcb.gpio.entries; i++) {
3296 struct dcb_gpio_entry *gpio = &bios->dcb.gpio.entry[i];
Ben Skeggs6ee73862009-12-11 19:24:15 +10003297
Ben Skeggs2535d712010-04-07 12:00:14 +10003298 BIOSLOG(bios, "0x%04X: Entry: 0x%08X\n", offset, gpio->entry);
Ben Skeggs6ee73862009-12-11 19:24:15 +10003299
Ben Skeggs73db4be2010-05-26 10:41:45 +10003300 BIOSLOG(bios, "0x%04X: set gpio 0x%02x, state %d\n",
3301 offset, gpio->tag, gpio->state_default);
Ben Skeggs6ee73862009-12-11 19:24:15 +10003302
Ben Skeggs75139062011-07-03 13:40:01 +10003303 if (!bios->execute)
3304 continue;
3305
3306 pgpio->set(bios->dev, gpio->tag, gpio->state_default);
3307 if (dev_priv->card_type < NV_D0)
3308 init_gpio_unknv50(bios, gpio);
3309 else
3310 init_gpio_unknvd0(bios, gpio);
Ben Skeggs6ee73862009-12-11 19:24:15 +10003311 }
3312
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00003313 return 1;
Ben Skeggs6ee73862009-12-11 19:24:15 +10003314}
3315
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00003316static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10003317init_ram_restrict_zm_reg_group(struct nvbios *bios, uint16_t offset,
3318 struct init_exec *iexec)
3319{
3320 /*
3321 * INIT_RAM_RESTRICT_ZM_REG_GROUP opcode: 0x8F ('')
3322 *
3323 * offset (8 bit): opcode
3324 * offset + 1 (32 bit): reg
3325 * offset + 5 (8 bit): regincrement
3326 * offset + 6 (8 bit): count
3327 * offset + 7 (32 bit): value 1,1
3328 * ...
3329 *
3330 * Use the RAMCFG strap of PEXTDEV_BOOT as an index into the table at
3331 * ram_restrict_table_ptr. The value read from here is 'n', and
3332 * "value 1,n" gets written to "reg". This repeats "count" times and on
3333 * each iteration 'm', "reg" increases by "regincrement" and
3334 * "value m,n" is used. The extent of n is limited by a number read
3335 * from the 'M' BIT table, herein called "blocklen"
3336 */
3337
3338 uint32_t reg = ROM32(bios->data[offset + 1]);
3339 uint8_t regincrement = bios->data[offset + 5];
3340 uint8_t count = bios->data[offset + 6];
3341 uint32_t strap_ramcfg, data;
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00003342 /* previously set by 'M' BIT table */
3343 uint16_t blocklen = bios->ram_restrict_group_count * 4;
3344 int len = 7 + count * blocklen;
Ben Skeggs6ee73862009-12-11 19:24:15 +10003345 uint8_t index;
3346 int i;
3347
Ben Skeggs309b8c82010-06-29 16:09:24 +10003348 /* critical! to know the length of the opcode */;
Ben Skeggs6ee73862009-12-11 19:24:15 +10003349 if (!blocklen) {
3350 NV_ERROR(bios->dev,
3351 "0x%04X: Zero block length - has the M table "
3352 "been parsed?\n", offset);
Ben Skeggs9170a822010-05-10 16:54:23 +10003353 return -EINVAL;
Ben Skeggs6ee73862009-12-11 19:24:15 +10003354 }
3355
Ben Skeggs309b8c82010-06-29 16:09:24 +10003356 if (!iexec->execute)
3357 return len;
3358
Ben Skeggs6ee73862009-12-11 19:24:15 +10003359 strap_ramcfg = (bios_rd32(bios, NV_PEXTDEV_BOOT_0) >> 2) & 0xf;
3360 index = bios->data[bios->ram_restrict_tbl_ptr + strap_ramcfg];
3361
3362 BIOSLOG(bios, "0x%04X: Reg: 0x%08X, RegIncrement: 0x%02X, "
3363 "Count: 0x%02X, StrapRamCfg: 0x%02X, Index: 0x%02X\n",
3364 offset, reg, regincrement, count, strap_ramcfg, index);
3365
3366 for (i = 0; i < count; i++) {
3367 data = ROM32(bios->data[offset + 7 + index * 4 + blocklen * i]);
3368
3369 bios_wr32(bios, reg, data);
3370
3371 reg += regincrement;
3372 }
3373
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00003374 return len;
Ben Skeggs6ee73862009-12-11 19:24:15 +10003375}
3376
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00003377static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10003378init_copy_zm_reg(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
3379{
3380 /*
3381 * INIT_COPY_ZM_REG opcode: 0x90 ('')
3382 *
3383 * offset (8 bit): opcode
3384 * offset + 1 (32 bit): src reg
3385 * offset + 5 (32 bit): dst reg
3386 *
3387 * Put contents of "src reg" into "dst reg"
3388 */
3389
3390 uint32_t srcreg = ROM32(bios->data[offset + 1]);
3391 uint32_t dstreg = ROM32(bios->data[offset + 5]);
3392
3393 if (!iexec->execute)
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00003394 return 9;
Ben Skeggs6ee73862009-12-11 19:24:15 +10003395
3396 bios_wr32(bios, dstreg, bios_rd32(bios, srcreg));
3397
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00003398 return 9;
Ben Skeggs6ee73862009-12-11 19:24:15 +10003399}
3400
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00003401static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10003402init_zm_reg_group_addr_latched(struct nvbios *bios, uint16_t offset,
3403 struct init_exec *iexec)
3404{
3405 /*
3406 * INIT_ZM_REG_GROUP_ADDRESS_LATCHED opcode: 0x91 ('')
3407 *
3408 * offset (8 bit): opcode
3409 * offset + 1 (32 bit): dst reg
3410 * offset + 5 (8 bit): count
3411 * offset + 6 (32 bit): data 1
3412 * ...
3413 *
3414 * For each of "count" values write "data n" to "dst reg"
3415 */
3416
3417 uint32_t reg = ROM32(bios->data[offset + 1]);
3418 uint8_t count = bios->data[offset + 5];
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00003419 int len = 6 + count * 4;
Ben Skeggs6ee73862009-12-11 19:24:15 +10003420 int i;
3421
3422 if (!iexec->execute)
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00003423 return len;
Ben Skeggs6ee73862009-12-11 19:24:15 +10003424
3425 for (i = 0; i < count; i++) {
3426 uint32_t data = ROM32(bios->data[offset + 6 + 4 * i]);
3427 bios_wr32(bios, reg, data);
3428 }
3429
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00003430 return len;
Ben Skeggs6ee73862009-12-11 19:24:15 +10003431}
3432
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00003433static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10003434init_reserved(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
3435{
3436 /*
3437 * INIT_RESERVED opcode: 0x92 ('')
3438 *
3439 * offset (8 bit): opcode
3440 *
3441 * Seemingly does nothing
3442 */
3443
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00003444 return 1;
Ben Skeggs6ee73862009-12-11 19:24:15 +10003445}
3446
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00003447static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10003448init_96(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
3449{
3450 /*
3451 * INIT_96 opcode: 0x96 ('')
3452 *
3453 * offset (8 bit): opcode
3454 * offset + 1 (32 bit): sreg
3455 * offset + 5 (8 bit): sshift
3456 * offset + 6 (8 bit): smask
3457 * offset + 7 (8 bit): index
3458 * offset + 8 (32 bit): reg
3459 * offset + 12 (32 bit): mask
3460 * offset + 16 (8 bit): shift
3461 *
3462 */
3463
3464 uint16_t xlatptr = bios->init96_tbl_ptr + (bios->data[offset + 7] * 2);
3465 uint32_t reg = ROM32(bios->data[offset + 8]);
3466 uint32_t mask = ROM32(bios->data[offset + 12]);
3467 uint32_t val;
3468
3469 val = bios_rd32(bios, ROM32(bios->data[offset + 1]));
3470 if (bios->data[offset + 5] < 0x80)
3471 val >>= bios->data[offset + 5];
3472 else
3473 val <<= (0x100 - bios->data[offset + 5]);
3474 val &= bios->data[offset + 6];
3475
3476 val = bios->data[ROM16(bios->data[xlatptr]) + val];
3477 val <<= bios->data[offset + 16];
3478
3479 if (!iexec->execute)
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00003480 return 17;
Ben Skeggs6ee73862009-12-11 19:24:15 +10003481
3482 bios_wr32(bios, reg, (bios_rd32(bios, reg) & mask) | val);
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00003483 return 17;
Ben Skeggs6ee73862009-12-11 19:24:15 +10003484}
3485
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00003486static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10003487init_97(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
3488{
3489 /*
3490 * INIT_97 opcode: 0x97 ('')
3491 *
3492 * offset (8 bit): opcode
3493 * offset + 1 (32 bit): register
3494 * offset + 5 (32 bit): mask
3495 * offset + 9 (32 bit): value
3496 *
3497 * Adds "value" to "register" preserving the fields specified
3498 * by "mask"
3499 */
3500
3501 uint32_t reg = ROM32(bios->data[offset + 1]);
3502 uint32_t mask = ROM32(bios->data[offset + 5]);
3503 uint32_t add = ROM32(bios->data[offset + 9]);
3504 uint32_t val;
3505
3506 val = bios_rd32(bios, reg);
3507 val = (val & mask) | ((val + add) & ~mask);
3508
3509 if (!iexec->execute)
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00003510 return 13;
Ben Skeggs6ee73862009-12-11 19:24:15 +10003511
3512 bios_wr32(bios, reg, val);
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00003513 return 13;
Ben Skeggs6ee73862009-12-11 19:24:15 +10003514}
3515
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00003516static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10003517init_auxch(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
3518{
3519 /*
3520 * INIT_AUXCH opcode: 0x98 ('')
3521 *
3522 * offset (8 bit): opcode
3523 * offset + 1 (32 bit): address
3524 * offset + 5 (8 bit): count
3525 * offset + 6 (8 bit): mask 0
3526 * offset + 7 (8 bit): data 0
3527 * ...
3528 *
3529 */
3530
3531 struct drm_device *dev = bios->dev;
3532 struct nouveau_i2c_chan *auxch;
3533 uint32_t addr = ROM32(bios->data[offset + 1]);
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00003534 uint8_t count = bios->data[offset + 5];
3535 int len = 6 + count * 2;
Ben Skeggs6ee73862009-12-11 19:24:15 +10003536 int ret, i;
3537
3538 if (!bios->display.output) {
3539 NV_ERROR(dev, "INIT_AUXCH: no active output\n");
Ben Skeggs309b8c82010-06-29 16:09:24 +10003540 return len;
Ben Skeggs6ee73862009-12-11 19:24:15 +10003541 }
3542
3543 auxch = init_i2c_device_find(dev, bios->display.output->i2c_index);
3544 if (!auxch) {
3545 NV_ERROR(dev, "INIT_AUXCH: couldn't get auxch %d\n",
3546 bios->display.output->i2c_index);
Ben Skeggs309b8c82010-06-29 16:09:24 +10003547 return len;
Ben Skeggs6ee73862009-12-11 19:24:15 +10003548 }
3549
3550 if (!iexec->execute)
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00003551 return len;
Ben Skeggs6ee73862009-12-11 19:24:15 +10003552
3553 offset += 6;
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00003554 for (i = 0; i < count; i++, offset += 2) {
Ben Skeggs6ee73862009-12-11 19:24:15 +10003555 uint8_t data;
3556
3557 ret = nouveau_dp_auxch(auxch, 9, addr, &data, 1);
3558 if (ret) {
3559 NV_ERROR(dev, "INIT_AUXCH: rd auxch fail %d\n", ret);
Ben Skeggs309b8c82010-06-29 16:09:24 +10003560 return len;
Ben Skeggs6ee73862009-12-11 19:24:15 +10003561 }
3562
3563 data &= bios->data[offset + 0];
3564 data |= bios->data[offset + 1];
3565
3566 ret = nouveau_dp_auxch(auxch, 8, addr, &data, 1);
3567 if (ret) {
3568 NV_ERROR(dev, "INIT_AUXCH: wr auxch fail %d\n", ret);
Ben Skeggs309b8c82010-06-29 16:09:24 +10003569 return len;
Ben Skeggs6ee73862009-12-11 19:24:15 +10003570 }
3571 }
3572
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00003573 return len;
Ben Skeggs6ee73862009-12-11 19:24:15 +10003574}
3575
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00003576static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10003577init_zm_auxch(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
3578{
3579 /*
3580 * INIT_ZM_AUXCH opcode: 0x99 ('')
3581 *
3582 * offset (8 bit): opcode
3583 * offset + 1 (32 bit): address
3584 * offset + 5 (8 bit): count
3585 * offset + 6 (8 bit): data 0
3586 * ...
3587 *
3588 */
3589
3590 struct drm_device *dev = bios->dev;
3591 struct nouveau_i2c_chan *auxch;
3592 uint32_t addr = ROM32(bios->data[offset + 1]);
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00003593 uint8_t count = bios->data[offset + 5];
3594 int len = 6 + count;
Ben Skeggs6ee73862009-12-11 19:24:15 +10003595 int ret, i;
3596
3597 if (!bios->display.output) {
3598 NV_ERROR(dev, "INIT_ZM_AUXCH: no active output\n");
Ben Skeggs309b8c82010-06-29 16:09:24 +10003599 return len;
Ben Skeggs6ee73862009-12-11 19:24:15 +10003600 }
3601
3602 auxch = init_i2c_device_find(dev, bios->display.output->i2c_index);
3603 if (!auxch) {
3604 NV_ERROR(dev, "INIT_ZM_AUXCH: couldn't get auxch %d\n",
3605 bios->display.output->i2c_index);
Ben Skeggs309b8c82010-06-29 16:09:24 +10003606 return len;
Ben Skeggs6ee73862009-12-11 19:24:15 +10003607 }
3608
3609 if (!iexec->execute)
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00003610 return len;
Ben Skeggs6ee73862009-12-11 19:24:15 +10003611
3612 offset += 6;
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00003613 for (i = 0; i < count; i++, offset++) {
Ben Skeggs6ee73862009-12-11 19:24:15 +10003614 ret = nouveau_dp_auxch(auxch, 8, addr, &bios->data[offset], 1);
3615 if (ret) {
3616 NV_ERROR(dev, "INIT_ZM_AUXCH: wr auxch fail %d\n", ret);
Ben Skeggs309b8c82010-06-29 16:09:24 +10003617 return len;
Ben Skeggs6ee73862009-12-11 19:24:15 +10003618 }
3619 }
3620
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00003621 return len;
Ben Skeggs6ee73862009-12-11 19:24:15 +10003622}
3623
Marcin Koƛcielnickib715d642010-07-04 02:47:16 +00003624static int
3625init_i2c_long_if(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
3626{
3627 /*
3628 * INIT_I2C_LONG_IF opcode: 0x9A ('')
3629 *
3630 * offset (8 bit): opcode
3631 * offset + 1 (8 bit): DCB I2C table entry index
3632 * offset + 2 (8 bit): I2C slave address
3633 * offset + 3 (16 bit): I2C register
3634 * offset + 5 (8 bit): mask
3635 * offset + 6 (8 bit): data
3636 *
3637 * Read the register given by "I2C register" on the device addressed
3638 * by "I2C slave address" on the I2C bus given by "DCB I2C table
3639 * entry index". Compare the result AND "mask" to "data".
3640 * If they're not equal, skip subsequent opcodes until condition is
3641 * inverted (INIT_NOT), or we hit INIT_RESUME
3642 */
3643
3644 uint8_t i2c_index = bios->data[offset + 1];
3645 uint8_t i2c_address = bios->data[offset + 2] >> 1;
3646 uint8_t reglo = bios->data[offset + 3];
3647 uint8_t reghi = bios->data[offset + 4];
3648 uint8_t mask = bios->data[offset + 5];
3649 uint8_t data = bios->data[offset + 6];
3650 struct nouveau_i2c_chan *chan;
3651 uint8_t buf0[2] = { reghi, reglo };
3652 uint8_t buf1[1];
3653 struct i2c_msg msg[2] = {
3654 { i2c_address, 0, 1, buf0 },
3655 { i2c_address, I2C_M_RD, 1, buf1 },
3656 };
3657 int ret;
3658
3659 /* no execute check by design */
3660
3661 BIOSLOG(bios, "0x%04X: DCBI2CIndex: 0x%02X, I2CAddress: 0x%02X\n",
3662 offset, i2c_index, i2c_address);
3663
3664 chan = init_i2c_device_find(bios->dev, i2c_index);
3665 if (!chan)
3666 return -ENODEV;
3667
3668
3669 ret = i2c_transfer(&chan->adapter, msg, 2);
3670 if (ret < 0) {
3671 BIOSLOG(bios, "0x%04X: I2CReg: 0x%02X:0x%02X, Value: [no device], "
3672 "Mask: 0x%02X, Data: 0x%02X\n",
3673 offset, reghi, reglo, mask, data);
3674 iexec->execute = 0;
3675 return 7;
3676 }
3677
3678 BIOSLOG(bios, "0x%04X: I2CReg: 0x%02X:0x%02X, Value: 0x%02X, "
3679 "Mask: 0x%02X, Data: 0x%02X\n",
3680 offset, reghi, reglo, buf1[0], mask, data);
3681
3682 iexec->execute = ((buf1[0] & mask) == data);
3683
3684 return 7;
3685}
3686
Ben Skeggs6ee73862009-12-11 19:24:15 +10003687static struct init_tbl_entry itbl_entry[] = {
3688 /* command name , id , length , offset , mult , command handler */
3689 /* INIT_PROG (0x31, 15, 10, 4) removed due to no example of use */
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00003690 { "INIT_IO_RESTRICT_PROG" , 0x32, init_io_restrict_prog },
3691 { "INIT_REPEAT" , 0x33, init_repeat },
3692 { "INIT_IO_RESTRICT_PLL" , 0x34, init_io_restrict_pll },
3693 { "INIT_END_REPEAT" , 0x36, init_end_repeat },
3694 { "INIT_COPY" , 0x37, init_copy },
3695 { "INIT_NOT" , 0x38, init_not },
3696 { "INIT_IO_FLAG_CONDITION" , 0x39, init_io_flag_condition },
Ben Skeggs25908b72010-04-20 02:28:37 +10003697 { "INIT_DP_CONDITION" , 0x3A, init_dp_condition },
3698 { "INIT_OP_3B" , 0x3B, init_op_3b },
3699 { "INIT_OP_3C" , 0x3C, init_op_3c },
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00003700 { "INIT_INDEX_ADDRESS_LATCHED" , 0x49, init_idx_addr_latched },
3701 { "INIT_IO_RESTRICT_PLL2" , 0x4A, init_io_restrict_pll2 },
3702 { "INIT_PLL2" , 0x4B, init_pll2 },
3703 { "INIT_I2C_BYTE" , 0x4C, init_i2c_byte },
3704 { "INIT_ZM_I2C_BYTE" , 0x4D, init_zm_i2c_byte },
3705 { "INIT_ZM_I2C" , 0x4E, init_zm_i2c },
3706 { "INIT_TMDS" , 0x4F, init_tmds },
3707 { "INIT_ZM_TMDS_GROUP" , 0x50, init_zm_tmds_group },
3708 { "INIT_CR_INDEX_ADDRESS_LATCHED" , 0x51, init_cr_idx_adr_latch },
3709 { "INIT_CR" , 0x52, init_cr },
3710 { "INIT_ZM_CR" , 0x53, init_zm_cr },
3711 { "INIT_ZM_CR_GROUP" , 0x54, init_zm_cr_group },
3712 { "INIT_CONDITION_TIME" , 0x56, init_condition_time },
Marcin Koƛcielnickie3a19242010-07-02 19:33:01 +00003713 { "INIT_LTIME" , 0x57, init_ltime },
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00003714 { "INIT_ZM_REG_SEQUENCE" , 0x58, init_zm_reg_sequence },
Ben Skeggs6ee73862009-12-11 19:24:15 +10003715 /* INIT_INDIRECT_REG (0x5A, 7, 0, 0) removed due to no example of use */
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00003716 { "INIT_SUB_DIRECT" , 0x5B, init_sub_direct },
Ben Skeggsec64a402011-03-21 21:31:21 +10003717 { "INIT_JUMP" , 0x5C, init_jump },
Marcin Koƛcielnickib715d642010-07-04 02:47:16 +00003718 { "INIT_I2C_IF" , 0x5E, init_i2c_if },
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00003719 { "INIT_COPY_NV_REG" , 0x5F, init_copy_nv_reg },
3720 { "INIT_ZM_INDEX_IO" , 0x62, init_zm_index_io },
3721 { "INIT_COMPUTE_MEM" , 0x63, init_compute_mem },
3722 { "INIT_RESET" , 0x65, init_reset },
3723 { "INIT_CONFIGURE_MEM" , 0x66, init_configure_mem },
3724 { "INIT_CONFIGURE_CLK" , 0x67, init_configure_clk },
3725 { "INIT_CONFIGURE_PREINIT" , 0x68, init_configure_preinit },
3726 { "INIT_IO" , 0x69, init_io },
3727 { "INIT_SUB" , 0x6B, init_sub },
3728 { "INIT_RAM_CONDITION" , 0x6D, init_ram_condition },
3729 { "INIT_NV_REG" , 0x6E, init_nv_reg },
3730 { "INIT_MACRO" , 0x6F, init_macro },
3731 { "INIT_DONE" , 0x71, init_done },
3732 { "INIT_RESUME" , 0x72, init_resume },
Ben Skeggs6ee73862009-12-11 19:24:15 +10003733 /* INIT_RAM_CONDITION2 (0x73, 9, 0, 0) removed due to no example of use */
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00003734 { "INIT_TIME" , 0x74, init_time },
3735 { "INIT_CONDITION" , 0x75, init_condition },
3736 { "INIT_IO_CONDITION" , 0x76, init_io_condition },
3737 { "INIT_INDEX_IO" , 0x78, init_index_io },
3738 { "INIT_PLL" , 0x79, init_pll },
3739 { "INIT_ZM_REG" , 0x7A, init_zm_reg },
3740 { "INIT_RAM_RESTRICT_PLL" , 0x87, init_ram_restrict_pll },
3741 { "INIT_8C" , 0x8C, init_8c },
3742 { "INIT_8D" , 0x8D, init_8d },
3743 { "INIT_GPIO" , 0x8E, init_gpio },
3744 { "INIT_RAM_RESTRICT_ZM_REG_GROUP" , 0x8F, init_ram_restrict_zm_reg_group },
3745 { "INIT_COPY_ZM_REG" , 0x90, init_copy_zm_reg },
3746 { "INIT_ZM_REG_GROUP_ADDRESS_LATCHED" , 0x91, init_zm_reg_group_addr_latched },
3747 { "INIT_RESERVED" , 0x92, init_reserved },
3748 { "INIT_96" , 0x96, init_96 },
3749 { "INIT_97" , 0x97, init_97 },
3750 { "INIT_AUXCH" , 0x98, init_auxch },
3751 { "INIT_ZM_AUXCH" , 0x99, init_zm_auxch },
Marcin Koƛcielnickib715d642010-07-04 02:47:16 +00003752 { "INIT_I2C_LONG_IF" , 0x9A, init_i2c_long_if },
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00003753 { NULL , 0 , NULL }
Ben Skeggs6ee73862009-12-11 19:24:15 +10003754};
3755
Ben Skeggs6ee73862009-12-11 19:24:15 +10003756#define MAX_TABLE_OPS 1000
3757
3758static int
Ben Skeggsec64a402011-03-21 21:31:21 +10003759parse_init_table(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
Ben Skeggs6ee73862009-12-11 19:24:15 +10003760{
3761 /*
3762 * Parses all commands in an init table.
3763 *
3764 * We start out executing all commands found in the init table. Some
3765 * opcodes may change the status of iexec->execute to SKIP, which will
3766 * cause the following opcodes to perform no operation until the value
3767 * is changed back to EXECUTE.
3768 */
3769
Ben Skeggs92b96182010-05-10 16:59:42 +10003770 int count = 0, i, ret;
Ben Skeggs6ee73862009-12-11 19:24:15 +10003771 uint8_t id;
3772
Ben Skeggsa8e415d2011-08-04 13:57:33 +10003773 /* catch NULL script pointers */
3774 if (offset == 0)
3775 return 0;
3776
Ben Skeggs6ee73862009-12-11 19:24:15 +10003777 /*
3778 * Loop until INIT_DONE causes us to break out of the loop
3779 * (or until offset > bios length just in case... )
3780 * (and no more than MAX_TABLE_OPS iterations, just in case... )
3781 */
3782 while ((offset < bios->length) && (count++ < MAX_TABLE_OPS)) {
3783 id = bios->data[offset];
3784
3785 /* Find matching id in itbl_entry */
3786 for (i = 0; itbl_entry[i].name && (itbl_entry[i].id != id); i++)
3787 ;
3788
Ben Skeggs92b96182010-05-10 16:59:42 +10003789 if (!itbl_entry[i].name) {
Ben Skeggs6ee73862009-12-11 19:24:15 +10003790 NV_ERROR(bios->dev,
3791 "0x%04X: Init table command not found: "
3792 "0x%02X\n", offset, id);
3793 return -ENOENT;
3794 }
Ben Skeggs92b96182010-05-10 16:59:42 +10003795
3796 BIOSLOG(bios, "0x%04X: [ (0x%02X) - %s ]\n", offset,
3797 itbl_entry[i].id, itbl_entry[i].name);
3798
3799 /* execute eventual command handler */
3800 ret = (*itbl_entry[i].handler)(bios, offset, iexec);
3801 if (ret < 0) {
3802 NV_ERROR(bios->dev, "0x%04X: Failed parsing init "
3803 "table opcode: %s %d\n", offset,
3804 itbl_entry[i].name, ret);
3805 }
3806
3807 if (ret <= 0)
3808 break;
3809
3810 /*
3811 * Add the offset of the current command including all data
3812 * of that command. The offset will then be pointing on the
3813 * next op code.
3814 */
3815 offset += ret;
Ben Skeggs6ee73862009-12-11 19:24:15 +10003816 }
3817
3818 if (offset >= bios->length)
3819 NV_WARN(bios->dev,
3820 "Offset 0x%04X greater than known bios image length. "
3821 "Corrupt image?\n", offset);
3822 if (count >= MAX_TABLE_OPS)
3823 NV_WARN(bios->dev,
3824 "More than %d opcodes to a table is unlikely, "
3825 "is the bios image corrupt?\n", MAX_TABLE_OPS);
3826
3827 return 0;
3828}
3829
3830static void
3831parse_init_tables(struct nvbios *bios)
3832{
3833 /* Loops and calls parse_init_table() for each present table. */
3834
3835 int i = 0;
3836 uint16_t table;
3837 struct init_exec iexec = {true, false};
3838
3839 if (bios->old_style_init) {
3840 if (bios->init_script_tbls_ptr)
3841 parse_init_table(bios, bios->init_script_tbls_ptr, &iexec);
3842 if (bios->extra_init_script_tbl_ptr)
3843 parse_init_table(bios, bios->extra_init_script_tbl_ptr, &iexec);
3844
3845 return;
3846 }
3847
3848 while ((table = ROM16(bios->data[bios->init_script_tbls_ptr + i]))) {
3849 NV_INFO(bios->dev,
3850 "Parsing VBIOS init table %d at offset 0x%04X\n",
3851 i / 2, table);
3852 BIOSLOG(bios, "0x%04X: ------ Executing following commands ------\n", table);
3853
3854 parse_init_table(bios, table, &iexec);
3855 i += 2;
3856 }
3857}
3858
3859static uint16_t clkcmptable(struct nvbios *bios, uint16_t clktable, int pxclk)
3860{
3861 int compare_record_len, i = 0;
3862 uint16_t compareclk, scriptptr = 0;
3863
3864 if (bios->major_version < 5) /* pre BIT */
3865 compare_record_len = 3;
3866 else
3867 compare_record_len = 4;
3868
3869 do {
3870 compareclk = ROM16(bios->data[clktable + compare_record_len * i]);
3871 if (pxclk >= compareclk * 10) {
3872 if (bios->major_version < 5) {
3873 uint8_t tmdssub = bios->data[clktable + 2 + compare_record_len * i];
3874 scriptptr = ROM16(bios->data[bios->init_script_tbls_ptr + tmdssub * 2]);
3875 } else
3876 scriptptr = ROM16(bios->data[clktable + 2 + compare_record_len * i]);
3877 break;
3878 }
3879 i++;
3880 } while (compareclk);
3881
3882 return scriptptr;
3883}
3884
3885static void
3886run_digital_op_script(struct drm_device *dev, uint16_t scriptptr,
3887 struct dcb_entry *dcbent, int head, bool dl)
3888{
3889 struct drm_nouveau_private *dev_priv = dev->dev_private;
Ben Skeggs04a39c52010-02-24 10:03:05 +10003890 struct nvbios *bios = &dev_priv->vbios;
Ben Skeggs6ee73862009-12-11 19:24:15 +10003891 struct init_exec iexec = {true, false};
3892
3893 NV_TRACE(dev, "0x%04X: Parsing digital output script table\n",
3894 scriptptr);
3895 bios_idxprt_wr(bios, NV_CIO_CRX__COLOR, NV_CIO_CRE_44,
3896 head ? NV_CIO_CRE_44_HEADB : NV_CIO_CRE_44_HEADA);
3897 /* note: if dcb entries have been merged, index may be misleading */
3898 NVWriteVgaCrtc5758(dev, head, 0, dcbent->index);
3899 parse_init_table(bios, scriptptr, &iexec);
3900
3901 nv04_dfp_bind_head(dev, dcbent, head, dl);
3902}
3903
3904static int call_lvds_manufacturer_script(struct drm_device *dev, struct dcb_entry *dcbent, int head, enum LVDS_script script)
3905{
3906 struct drm_nouveau_private *dev_priv = dev->dev_private;
Ben Skeggs04a39c52010-02-24 10:03:05 +10003907 struct nvbios *bios = &dev_priv->vbios;
Ben Skeggs6ee73862009-12-11 19:24:15 +10003908 uint8_t sub = bios->data[bios->fp.xlated_entry + script] + (bios->fp.link_c_increment && dcbent->or & OUTPUT_C ? 1 : 0);
3909 uint16_t scriptofs = ROM16(bios->data[bios->init_script_tbls_ptr + sub * 2]);
3910
3911 if (!bios->fp.xlated_entry || !sub || !scriptofs)
3912 return -EINVAL;
3913
3914 run_digital_op_script(dev, scriptofs, dcbent, head, bios->fp.dual_link);
3915
3916 if (script == LVDS_PANEL_OFF) {
3917 /* off-on delay in ms */
Ben Skeggsc7ca4d12011-02-03 20:10:49 +10003918 mdelay(ROM16(bios->data[bios->fp.xlated_entry + 7]));
Ben Skeggs6ee73862009-12-11 19:24:15 +10003919 }
3920#ifdef __powerpc__
3921 /* Powerbook specific quirks */
Francisco Jerezd31e0782010-08-20 14:19:45 +02003922 if (script == LVDS_RESET &&
3923 (dev->pci_device == 0x0179 || dev->pci_device == 0x0189 ||
3924 dev->pci_device == 0x0329))
3925 nv_write_tmds(dev, dcbent->or, 0, 0x02, 0x72);
Ben Skeggs6ee73862009-12-11 19:24:15 +10003926#endif
3927
3928 return 0;
3929}
3930
3931static int run_lvds_table(struct drm_device *dev, struct dcb_entry *dcbent, int head, enum LVDS_script script, int pxclk)
3932{
3933 /*
3934 * The BIT LVDS table's header has the information to setup the
3935 * necessary registers. Following the standard 4 byte header are:
3936 * A bitmask byte and a dual-link transition pxclk value for use in
3937 * selecting the init script when not using straps; 4 script pointers
3938 * for panel power, selected by output and on/off; and 8 table pointers
3939 * for panel init, the needed one determined by output, and bits in the
3940 * conf byte. These tables are similar to the TMDS tables, consisting
3941 * of a list of pxclks and script pointers.
3942 */
3943 struct drm_nouveau_private *dev_priv = dev->dev_private;
Ben Skeggs04a39c52010-02-24 10:03:05 +10003944 struct nvbios *bios = &dev_priv->vbios;
Ben Skeggs6ee73862009-12-11 19:24:15 +10003945 unsigned int outputset = (dcbent->or == 4) ? 1 : 0;
3946 uint16_t scriptptr = 0, clktable;
Ben Skeggs6ee73862009-12-11 19:24:15 +10003947
3948 /*
3949 * For now we assume version 3.0 table - g80 support will need some
3950 * changes
3951 */
3952
3953 switch (script) {
3954 case LVDS_INIT:
3955 return -ENOSYS;
3956 case LVDS_BACKLIGHT_ON:
3957 case LVDS_PANEL_ON:
3958 scriptptr = ROM16(bios->data[bios->fp.lvdsmanufacturerpointer + 7 + outputset * 2]);
3959 break;
3960 case LVDS_BACKLIGHT_OFF:
3961 case LVDS_PANEL_OFF:
3962 scriptptr = ROM16(bios->data[bios->fp.lvdsmanufacturerpointer + 11 + outputset * 2]);
3963 break;
3964 case LVDS_RESET:
Ben Skeggsf3bbb9c2010-03-18 12:05:43 +10003965 clktable = bios->fp.lvdsmanufacturerpointer + 15;
3966 if (dcbent->or == 4)
3967 clktable += 8;
3968
Ben Skeggs6ee73862009-12-11 19:24:15 +10003969 if (dcbent->lvdsconf.use_straps_for_mode) {
3970 if (bios->fp.dual_link)
Ben Skeggsf3bbb9c2010-03-18 12:05:43 +10003971 clktable += 4;
3972 if (bios->fp.if_is_24bit)
3973 clktable += 2;
Ben Skeggs6ee73862009-12-11 19:24:15 +10003974 } else {
3975 /* using EDID */
Ben Skeggsf3bbb9c2010-03-18 12:05:43 +10003976 int cmpval_24bit = (dcbent->or == 4) ? 4 : 1;
Ben Skeggs6ee73862009-12-11 19:24:15 +10003977
3978 if (bios->fp.dual_link) {
Ben Skeggsf3bbb9c2010-03-18 12:05:43 +10003979 clktable += 4;
3980 cmpval_24bit <<= 1;
Ben Skeggs6ee73862009-12-11 19:24:15 +10003981 }
Ben Skeggsf3bbb9c2010-03-18 12:05:43 +10003982
3983 if (bios->fp.strapless_is_24bit & cmpval_24bit)
3984 clktable += 2;
Ben Skeggs6ee73862009-12-11 19:24:15 +10003985 }
3986
Ben Skeggsf3bbb9c2010-03-18 12:05:43 +10003987 clktable = ROM16(bios->data[clktable]);
Ben Skeggs6ee73862009-12-11 19:24:15 +10003988 if (!clktable) {
3989 NV_ERROR(dev, "Pixel clock comparison table not found\n");
3990 return -ENOENT;
3991 }
3992 scriptptr = clkcmptable(bios, clktable, pxclk);
3993 }
3994
3995 if (!scriptptr) {
3996 NV_ERROR(dev, "LVDS output init script not found\n");
3997 return -ENOENT;
3998 }
3999 run_digital_op_script(dev, scriptptr, dcbent, head, bios->fp.dual_link);
4000
4001 return 0;
4002}
4003
4004int call_lvds_script(struct drm_device *dev, struct dcb_entry *dcbent, int head, enum LVDS_script script, int pxclk)
4005{
4006 /*
4007 * LVDS operations are multiplexed in an effort to present a single API
4008 * which works with two vastly differing underlying structures.
4009 * This acts as the demux
4010 */
4011
4012 struct drm_nouveau_private *dev_priv = dev->dev_private;
Ben Skeggs04a39c52010-02-24 10:03:05 +10004013 struct nvbios *bios = &dev_priv->vbios;
Ben Skeggs6ee73862009-12-11 19:24:15 +10004014 uint8_t lvds_ver = bios->data[bios->fp.lvdsmanufacturerpointer];
4015 uint32_t sel_clk_binding, sel_clk;
4016 int ret;
4017
4018 if (bios->fp.last_script_invoc == (script << 1 | head) || !lvds_ver ||
4019 (lvds_ver >= 0x30 && script == LVDS_INIT))
4020 return 0;
4021
4022 if (!bios->fp.lvds_init_run) {
4023 bios->fp.lvds_init_run = true;
4024 call_lvds_script(dev, dcbent, head, LVDS_INIT, pxclk);
4025 }
4026
4027 if (script == LVDS_PANEL_ON && bios->fp.reset_after_pclk_change)
4028 call_lvds_script(dev, dcbent, head, LVDS_RESET, pxclk);
4029 if (script == LVDS_RESET && bios->fp.power_off_for_reset)
4030 call_lvds_script(dev, dcbent, head, LVDS_PANEL_OFF, pxclk);
4031
4032 NV_TRACE(dev, "Calling LVDS script %d:\n", script);
4033
4034 /* don't let script change pll->head binding */
4035 sel_clk_binding = bios_rd32(bios, NV_PRAMDAC_SEL_CLK) & 0x50000;
4036
4037 if (lvds_ver < 0x30)
4038 ret = call_lvds_manufacturer_script(dev, dcbent, head, script);
4039 else
4040 ret = run_lvds_table(dev, dcbent, head, script, pxclk);
4041
4042 bios->fp.last_script_invoc = (script << 1 | head);
4043
4044 sel_clk = NVReadRAMDAC(dev, 0, NV_PRAMDAC_SEL_CLK) & ~0x50000;
4045 NVWriteRAMDAC(dev, 0, NV_PRAMDAC_SEL_CLK, sel_clk | sel_clk_binding);
4046 /* some scripts set a value in NV_PBUS_POWERCTRL_2 and break video overlay */
4047 nvWriteMC(dev, NV_PBUS_POWERCTRL_2, 0);
4048
4049 return ret;
4050}
4051
4052struct lvdstableheader {
4053 uint8_t lvds_ver, headerlen, recordlen;
4054};
4055
4056static int parse_lvds_manufacturer_table_header(struct drm_device *dev, struct nvbios *bios, struct lvdstableheader *lth)
4057{
4058 /*
4059 * BMP version (0xa) LVDS table has a simple header of version and
4060 * record length. The BIT LVDS table has the typical BIT table header:
4061 * version byte, header length byte, record length byte, and a byte for
4062 * the maximum number of records that can be held in the table.
4063 */
4064
4065 uint8_t lvds_ver, headerlen, recordlen;
4066
4067 memset(lth, 0, sizeof(struct lvdstableheader));
4068
4069 if (bios->fp.lvdsmanufacturerpointer == 0x0) {
4070 NV_ERROR(dev, "Pointer to LVDS manufacturer table invalid\n");
4071 return -EINVAL;
4072 }
4073
4074 lvds_ver = bios->data[bios->fp.lvdsmanufacturerpointer];
4075
4076 switch (lvds_ver) {
4077 case 0x0a: /* pre NV40 */
4078 headerlen = 2;
4079 recordlen = bios->data[bios->fp.lvdsmanufacturerpointer + 1];
4080 break;
4081 case 0x30: /* NV4x */
4082 headerlen = bios->data[bios->fp.lvdsmanufacturerpointer + 1];
4083 if (headerlen < 0x1f) {
4084 NV_ERROR(dev, "LVDS table header not understood\n");
4085 return -EINVAL;
4086 }
4087 recordlen = bios->data[bios->fp.lvdsmanufacturerpointer + 2];
4088 break;
4089 case 0x40: /* G80/G90 */
4090 headerlen = bios->data[bios->fp.lvdsmanufacturerpointer + 1];
4091 if (headerlen < 0x7) {
4092 NV_ERROR(dev, "LVDS table header not understood\n");
4093 return -EINVAL;
4094 }
4095 recordlen = bios->data[bios->fp.lvdsmanufacturerpointer + 2];
4096 break;
4097 default:
4098 NV_ERROR(dev,
4099 "LVDS table revision %d.%d not currently supported\n",
4100 lvds_ver >> 4, lvds_ver & 0xf);
4101 return -ENOSYS;
4102 }
4103
4104 lth->lvds_ver = lvds_ver;
4105 lth->headerlen = headerlen;
4106 lth->recordlen = recordlen;
4107
4108 return 0;
4109}
4110
4111static int
4112get_fp_strap(struct drm_device *dev, struct nvbios *bios)
4113{
4114 struct drm_nouveau_private *dev_priv = dev->dev_private;
4115
4116 /*
4117 * The fp strap is normally dictated by the "User Strap" in
4118 * PEXTDEV_BOOT_0[20:16], but on BMP cards when bit 2 of the
4119 * Internal_Flags struct at 0x48 is set, the user strap gets overriden
4120 * by the PCI subsystem ID during POST, but not before the previous user
4121 * strap has been committed to CR58 for CR57=0xf on head A, which may be
4122 * read and used instead
4123 */
4124
4125 if (bios->major_version < 5 && bios->data[0x48] & 0x4)
4126 return NVReadVgaCrtc5758(dev, 0, 0xf) & 0xf;
4127
4128 if (dev_priv->card_type >= NV_50)
4129 return (bios_rd32(bios, NV_PEXTDEV_BOOT_0) >> 24) & 0xf;
4130 else
4131 return (bios_rd32(bios, NV_PEXTDEV_BOOT_0) >> 16) & 0xf;
4132}
4133
4134static int parse_fp_mode_table(struct drm_device *dev, struct nvbios *bios)
4135{
4136 uint8_t *fptable;
4137 uint8_t fptable_ver, headerlen = 0, recordlen, fpentries = 0xf, fpindex;
4138 int ret, ofs, fpstrapping;
4139 struct lvdstableheader lth;
4140
4141 if (bios->fp.fptablepointer == 0x0) {
4142 /* Apple cards don't have the fp table; the laptops use DDC */
4143 /* The table is also missing on some x86 IGPs */
4144#ifndef __powerpc__
4145 NV_ERROR(dev, "Pointer to flat panel table invalid\n");
4146#endif
Ben Skeggs04a39c52010-02-24 10:03:05 +10004147 bios->digital_min_front_porch = 0x4b;
Ben Skeggs6ee73862009-12-11 19:24:15 +10004148 return 0;
4149 }
4150
4151 fptable = &bios->data[bios->fp.fptablepointer];
4152 fptable_ver = fptable[0];
4153
4154 switch (fptable_ver) {
4155 /*
4156 * BMP version 0x5.0x11 BIOSen have version 1 like tables, but no
4157 * version field, and miss one of the spread spectrum/PWM bytes.
4158 * This could affect early GF2Go parts (not seen any appropriate ROMs
4159 * though). Here we assume that a version of 0x05 matches this case
4160 * (combining with a BMP version check would be better), as the
4161 * common case for the panel type field is 0x0005, and that is in
4162 * fact what we are reading the first byte of.
4163 */
4164 case 0x05: /* some NV10, 11, 15, 16 */
4165 recordlen = 42;
4166 ofs = -1;
4167 break;
4168 case 0x10: /* some NV15/16, and NV11+ */
4169 recordlen = 44;
4170 ofs = 0;
4171 break;
4172 case 0x20: /* NV40+ */
4173 headerlen = fptable[1];
4174 recordlen = fptable[2];
4175 fpentries = fptable[3];
4176 /*
4177 * fptable[4] is the minimum
4178 * RAMDAC_FP_HCRTC -> RAMDAC_FP_HSYNC_START gap
4179 */
Ben Skeggs04a39c52010-02-24 10:03:05 +10004180 bios->digital_min_front_porch = fptable[4];
Ben Skeggs6ee73862009-12-11 19:24:15 +10004181 ofs = -7;
4182 break;
4183 default:
4184 NV_ERROR(dev,
4185 "FP table revision %d.%d not currently supported\n",
4186 fptable_ver >> 4, fptable_ver & 0xf);
4187 return -ENOSYS;
4188 }
4189
4190 if (!bios->is_mobile) /* !mobile only needs digital_min_front_porch */
4191 return 0;
4192
4193 ret = parse_lvds_manufacturer_table_header(dev, bios, &lth);
4194 if (ret)
4195 return ret;
4196
4197 if (lth.lvds_ver == 0x30 || lth.lvds_ver == 0x40) {
4198 bios->fp.fpxlatetableptr = bios->fp.lvdsmanufacturerpointer +
4199 lth.headerlen + 1;
4200 bios->fp.xlatwidth = lth.recordlen;
4201 }
4202 if (bios->fp.fpxlatetableptr == 0x0) {
4203 NV_ERROR(dev, "Pointer to flat panel xlat table invalid\n");
4204 return -EINVAL;
4205 }
4206
4207 fpstrapping = get_fp_strap(dev, bios);
4208
4209 fpindex = bios->data[bios->fp.fpxlatetableptr +
4210 fpstrapping * bios->fp.xlatwidth];
4211
4212 if (fpindex > fpentries) {
4213 NV_ERROR(dev, "Bad flat panel table index\n");
4214 return -ENOENT;
4215 }
4216
4217 /* nv4x cards need both a strap value and fpindex of 0xf to use DDC */
4218 if (lth.lvds_ver > 0x10)
Ben Skeggs04a39c52010-02-24 10:03:05 +10004219 bios->fp_no_ddc = fpstrapping != 0xf || fpindex != 0xf;
Ben Skeggs6ee73862009-12-11 19:24:15 +10004220
4221 /*
4222 * If either the strap or xlated fpindex value are 0xf there is no
4223 * panel using a strap-derived bios mode present. this condition
4224 * includes, but is different from, the DDC panel indicator above
4225 */
4226 if (fpstrapping == 0xf || fpindex == 0xf)
4227 return 0;
4228
4229 bios->fp.mode_ptr = bios->fp.fptablepointer + headerlen +
4230 recordlen * fpindex + ofs;
4231
4232 NV_TRACE(dev, "BIOS FP mode: %dx%d (%dkHz pixel clock)\n",
4233 ROM16(bios->data[bios->fp.mode_ptr + 11]) + 1,
4234 ROM16(bios->data[bios->fp.mode_ptr + 25]) + 1,
4235 ROM16(bios->data[bios->fp.mode_ptr + 7]) * 10);
4236
4237 return 0;
4238}
4239
4240bool nouveau_bios_fp_mode(struct drm_device *dev, struct drm_display_mode *mode)
4241{
4242 struct drm_nouveau_private *dev_priv = dev->dev_private;
Ben Skeggs04a39c52010-02-24 10:03:05 +10004243 struct nvbios *bios = &dev_priv->vbios;
Ben Skeggs6ee73862009-12-11 19:24:15 +10004244 uint8_t *mode_entry = &bios->data[bios->fp.mode_ptr];
4245
4246 if (!mode) /* just checking whether we can produce a mode */
4247 return bios->fp.mode_ptr;
4248
4249 memset(mode, 0, sizeof(struct drm_display_mode));
4250 /*
4251 * For version 1.0 (version in byte 0):
4252 * bytes 1-2 are "panel type", including bits on whether Colour/mono,
4253 * single/dual link, and type (TFT etc.)
4254 * bytes 3-6 are bits per colour in RGBX
4255 */
4256 mode->clock = ROM16(mode_entry[7]) * 10;
4257 /* bytes 9-10 is HActive */
4258 mode->hdisplay = ROM16(mode_entry[11]) + 1;
4259 /*
4260 * bytes 13-14 is HValid Start
4261 * bytes 15-16 is HValid End
4262 */
4263 mode->hsync_start = ROM16(mode_entry[17]) + 1;
4264 mode->hsync_end = ROM16(mode_entry[19]) + 1;
4265 mode->htotal = ROM16(mode_entry[21]) + 1;
4266 /* bytes 23-24, 27-30 similarly, but vertical */
4267 mode->vdisplay = ROM16(mode_entry[25]) + 1;
4268 mode->vsync_start = ROM16(mode_entry[31]) + 1;
4269 mode->vsync_end = ROM16(mode_entry[33]) + 1;
4270 mode->vtotal = ROM16(mode_entry[35]) + 1;
4271 mode->flags |= (mode_entry[37] & 0x10) ?
4272 DRM_MODE_FLAG_PHSYNC : DRM_MODE_FLAG_NHSYNC;
4273 mode->flags |= (mode_entry[37] & 0x1) ?
4274 DRM_MODE_FLAG_PVSYNC : DRM_MODE_FLAG_NVSYNC;
4275 /*
4276 * bytes 38-39 relate to spread spectrum settings
4277 * bytes 40-43 are something to do with PWM
4278 */
4279
4280 mode->status = MODE_OK;
4281 mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
4282 drm_mode_set_name(mode);
4283 return bios->fp.mode_ptr;
4284}
4285
4286int nouveau_bios_parse_lvds_table(struct drm_device *dev, int pxclk, bool *dl, bool *if_is_24bit)
4287{
4288 /*
4289 * The LVDS table header is (mostly) described in
4290 * parse_lvds_manufacturer_table_header(): the BIT header additionally
4291 * contains the dual-link transition pxclk (in 10s kHz), at byte 5 - if
4292 * straps are not being used for the panel, this specifies the frequency
4293 * at which modes should be set up in the dual link style.
4294 *
4295 * Following the header, the BMP (ver 0xa) table has several records,
Daniel Mack3ad2f3fb2010-02-03 08:01:28 +08004296 * indexed by a separate xlat table, indexed in turn by the fp strap in
Ben Skeggs6ee73862009-12-11 19:24:15 +10004297 * EXTDEV_BOOT. Each record had a config byte, followed by 6 script
4298 * numbers for use by INIT_SUB which controlled panel init and power,
4299 * and finally a dword of ms to sleep between power off and on
4300 * operations.
4301 *
4302 * In the BIT versions, the table following the header serves as an
4303 * integrated config and xlat table: the records in the table are
4304 * indexed by the FP strap nibble in EXTDEV_BOOT, and each record has
4305 * two bytes - the first as a config byte, the second for indexing the
4306 * fp mode table pointed to by the BIT 'D' table
4307 *
4308 * DDC is not used until after card init, so selecting the correct table
4309 * entry and setting the dual link flag for EDID equipped panels,
4310 * requiring tests against the native-mode pixel clock, cannot be done
4311 * until later, when this function should be called with non-zero pxclk
4312 */
4313 struct drm_nouveau_private *dev_priv = dev->dev_private;
Ben Skeggs04a39c52010-02-24 10:03:05 +10004314 struct nvbios *bios = &dev_priv->vbios;
Ben Skeggs6ee73862009-12-11 19:24:15 +10004315 int fpstrapping = get_fp_strap(dev, bios), lvdsmanufacturerindex = 0;
4316 struct lvdstableheader lth;
4317 uint16_t lvdsofs;
Ben Skeggs04a39c52010-02-24 10:03:05 +10004318 int ret, chip_version = bios->chip_version;
Ben Skeggs6ee73862009-12-11 19:24:15 +10004319
4320 ret = parse_lvds_manufacturer_table_header(dev, bios, &lth);
4321 if (ret)
4322 return ret;
4323
4324 switch (lth.lvds_ver) {
4325 case 0x0a: /* pre NV40 */
4326 lvdsmanufacturerindex = bios->data[
4327 bios->fp.fpxlatemanufacturertableptr +
4328 fpstrapping];
4329
4330 /* we're done if this isn't the EDID panel case */
4331 if (!pxclk)
4332 break;
4333
4334 if (chip_version < 0x25) {
4335 /* nv17 behaviour
4336 *
4337 * It seems the old style lvds script pointer is reused
4338 * to select 18/24 bit colour depth for EDID panels.
4339 */
4340 lvdsmanufacturerindex =
4341 (bios->legacy.lvds_single_a_script_ptr & 1) ?
4342 2 : 0;
4343 if (pxclk >= bios->fp.duallink_transition_clk)
4344 lvdsmanufacturerindex++;
4345 } else if (chip_version < 0x30) {
4346 /* nv28 behaviour (off-chip encoder)
4347 *
4348 * nv28 does a complex dance of first using byte 121 of
4349 * the EDID to choose the lvdsmanufacturerindex, then
4350 * later attempting to match the EDID manufacturer and
4351 * product IDs in a table (signature 'pidt' (panel id
4352 * table?)), setting an lvdsmanufacturerindex of 0 and
4353 * an fp strap of the match index (or 0xf if none)
4354 */
4355 lvdsmanufacturerindex = 0;
4356 } else {
4357 /* nv31, nv34 behaviour */
4358 lvdsmanufacturerindex = 0;
4359 if (pxclk >= bios->fp.duallink_transition_clk)
4360 lvdsmanufacturerindex = 2;
4361 if (pxclk >= 140000)
4362 lvdsmanufacturerindex = 3;
4363 }
4364
4365 /*
4366 * nvidia set the high nibble of (cr57=f, cr58) to
4367 * lvdsmanufacturerindex in this case; we don't
4368 */
4369 break;
4370 case 0x30: /* NV4x */
4371 case 0x40: /* G80/G90 */
4372 lvdsmanufacturerindex = fpstrapping;
4373 break;
4374 default:
4375 NV_ERROR(dev, "LVDS table revision not currently supported\n");
4376 return -ENOSYS;
4377 }
4378
4379 lvdsofs = bios->fp.xlated_entry = bios->fp.lvdsmanufacturerpointer + lth.headerlen + lth.recordlen * lvdsmanufacturerindex;
4380 switch (lth.lvds_ver) {
4381 case 0x0a:
4382 bios->fp.power_off_for_reset = bios->data[lvdsofs] & 1;
4383 bios->fp.reset_after_pclk_change = bios->data[lvdsofs] & 2;
4384 bios->fp.dual_link = bios->data[lvdsofs] & 4;
4385 bios->fp.link_c_increment = bios->data[lvdsofs] & 8;
4386 *if_is_24bit = bios->data[lvdsofs] & 16;
4387 break;
4388 case 0x30:
Ben Skeggsf3bbb9c2010-03-18 12:05:43 +10004389 case 0x40:
Ben Skeggs6ee73862009-12-11 19:24:15 +10004390 /*
4391 * No sign of the "power off for reset" or "reset for panel
4392 * on" bits, but it's safer to assume we should
4393 */
4394 bios->fp.power_off_for_reset = true;
4395 bios->fp.reset_after_pclk_change = true;
Ben Skeggsf3bbb9c2010-03-18 12:05:43 +10004396
Ben Skeggs6ee73862009-12-11 19:24:15 +10004397 /*
4398 * It's ok lvdsofs is wrong for nv4x edid case; dual_link is
Ben Skeggsf3bbb9c2010-03-18 12:05:43 +10004399 * over-written, and if_is_24bit isn't used
Ben Skeggs6ee73862009-12-11 19:24:15 +10004400 */
4401 bios->fp.dual_link = bios->data[lvdsofs] & 1;
Ben Skeggs6ee73862009-12-11 19:24:15 +10004402 bios->fp.if_is_24bit = bios->data[lvdsofs] & 2;
4403 bios->fp.strapless_is_24bit = bios->data[bios->fp.lvdsmanufacturerpointer + 4];
4404 bios->fp.duallink_transition_clk = ROM16(bios->data[bios->fp.lvdsmanufacturerpointer + 5]) * 10;
4405 break;
4406 }
4407
Ben Skeggs2eb92c82010-03-18 13:38:04 +10004408 /* Dell Latitude D620 reports a too-high value for the dual-link
4409 * transition freq, causing us to program the panel incorrectly.
4410 *
4411 * It doesn't appear the VBIOS actually uses its transition freq
4412 * (90000kHz), instead it uses the "Number of LVDS channels" field
4413 * out of the panel ID structure (http://www.spwg.org/).
4414 *
4415 * For the moment, a quirk will do :)
4416 */
Francisco Jerezacae1162010-08-15 14:31:31 +02004417 if (nv_match_device(dev, 0x01d7, 0x1028, 0x01c2))
Ben Skeggs2eb92c82010-03-18 13:38:04 +10004418 bios->fp.duallink_transition_clk = 80000;
Ben Skeggs2eb92c82010-03-18 13:38:04 +10004419
Ben Skeggs6ee73862009-12-11 19:24:15 +10004420 /* set dual_link flag for EDID case */
4421 if (pxclk && (chip_version < 0x25 || chip_version > 0x28))
4422 bios->fp.dual_link = (pxclk >= bios->fp.duallink_transition_clk);
4423
4424 *dl = bios->fp.dual_link;
4425
4426 return 0;
4427}
4428
4429static uint8_t *
4430bios_output_config_match(struct drm_device *dev, struct dcb_entry *dcbent,
Ben Skeggs1eb38102010-06-01 13:40:41 +10004431 uint16_t record, int record_len, int record_nr,
4432 bool match_link)
Ben Skeggs6ee73862009-12-11 19:24:15 +10004433{
4434 struct drm_nouveau_private *dev_priv = dev->dev_private;
Ben Skeggs04a39c52010-02-24 10:03:05 +10004435 struct nvbios *bios = &dev_priv->vbios;
Ben Skeggs6ee73862009-12-11 19:24:15 +10004436 uint32_t entry;
4437 uint16_t table;
4438 int i, v;
4439
Ben Skeggs1eb38102010-06-01 13:40:41 +10004440 switch (dcbent->type) {
4441 case OUTPUT_TMDS:
4442 case OUTPUT_LVDS:
4443 case OUTPUT_DP:
4444 break;
4445 default:
4446 match_link = false;
4447 break;
4448 }
4449
Ben Skeggs6ee73862009-12-11 19:24:15 +10004450 for (i = 0; i < record_nr; i++, record += record_len) {
4451 table = ROM16(bios->data[record]);
4452 if (!table)
4453 continue;
4454 entry = ROM32(bios->data[table]);
4455
Ben Skeggs1eb38102010-06-01 13:40:41 +10004456 if (match_link) {
4457 v = (entry & 0x00c00000) >> 22;
4458 if (!(v & dcbent->sorconf.link))
4459 continue;
4460 }
4461
Ben Skeggs6ee73862009-12-11 19:24:15 +10004462 v = (entry & 0x000f0000) >> 16;
4463 if (!(v & dcbent->or))
4464 continue;
4465
4466 v = (entry & 0x000000f0) >> 4;
4467 if (v != dcbent->location)
4468 continue;
4469
4470 v = (entry & 0x0000000f);
4471 if (v != dcbent->type)
4472 continue;
4473
4474 return &bios->data[table];
4475 }
4476
4477 return NULL;
4478}
4479
4480void *
4481nouveau_bios_dp_table(struct drm_device *dev, struct dcb_entry *dcbent,
Ben Skeggs27a45982011-08-04 09:26:44 +10004482 uint8_t *headerlen)
Ben Skeggs6ee73862009-12-11 19:24:15 +10004483{
4484 struct drm_nouveau_private *dev_priv = dev->dev_private;
Ben Skeggs04a39c52010-02-24 10:03:05 +10004485 struct nvbios *bios = &dev_priv->vbios;
Ben Skeggs6ee73862009-12-11 19:24:15 +10004486 uint8_t *table;
4487
4488 if (!bios->display.dp_table_ptr) {
4489 NV_ERROR(dev, "No pointer to DisplayPort table\n");
4490 return NULL;
4491 }
4492 table = &bios->data[bios->display.dp_table_ptr];
4493
Ben Skeggsc52e53f2010-02-25 11:53:00 +10004494 if (table[0] != 0x20 && table[0] != 0x21) {
Ben Skeggs6ee73862009-12-11 19:24:15 +10004495 NV_ERROR(dev, "DisplayPort table version 0x%02x unknown\n",
4496 table[0]);
4497 return NULL;
4498 }
4499
Ben Skeggs27a45982011-08-04 09:26:44 +10004500 *headerlen = table[4];
Ben Skeggs6ee73862009-12-11 19:24:15 +10004501 return bios_output_config_match(dev, dcbent,
4502 bios->display.dp_table_ptr + table[1],
Ben Skeggs1eb38102010-06-01 13:40:41 +10004503 table[2], table[3], table[0] >= 0x21);
Ben Skeggs6ee73862009-12-11 19:24:15 +10004504}
4505
4506int
Ben Skeggs02e4f582011-07-06 21:21:42 +10004507nouveau_bios_run_display_table(struct drm_device *dev, u16 type, int pclk,
4508 struct dcb_entry *dcbent, int crtc)
Ben Skeggs6ee73862009-12-11 19:24:15 +10004509{
4510 /*
4511 * The display script table is located by the BIT 'U' table.
4512 *
4513 * It contains an array of pointers to various tables describing
4514 * a particular output type. The first 32-bits of the output
4515 * tables contains similar information to a DCB entry, and is
4516 * used to decide whether that particular table is suitable for
4517 * the output you want to access.
4518 *
4519 * The "record header length" field here seems to indicate the
4520 * offset of the first configuration entry in the output tables.
4521 * This is 10 on most cards I've seen, but 12 has been witnessed
4522 * on DP cards, and there's another script pointer within the
4523 * header.
4524 *
4525 * offset + 0 ( 8 bits): version
4526 * offset + 1 ( 8 bits): header length
4527 * offset + 2 ( 8 bits): record length
4528 * offset + 3 ( 8 bits): number of records
4529 * offset + 4 ( 8 bits): record header length
4530 * offset + 5 (16 bits): pointer to first output script table
4531 */
4532
4533 struct drm_nouveau_private *dev_priv = dev->dev_private;
Ben Skeggs04a39c52010-02-24 10:03:05 +10004534 struct nvbios *bios = &dev_priv->vbios;
Ben Skeggs6ee73862009-12-11 19:24:15 +10004535 uint8_t *table = &bios->data[bios->display.script_table_ptr];
4536 uint8_t *otable = NULL;
4537 uint16_t script;
4538 int i = 0;
4539
4540 if (!bios->display.script_table_ptr) {
4541 NV_ERROR(dev, "No pointer to output script table\n");
4542 return 1;
4543 }
4544
4545 /*
4546 * Nothing useful has been in any of the pre-2.0 tables I've seen,
4547 * so until they are, we really don't need to care.
4548 */
4549 if (table[0] < 0x20)
4550 return 1;
4551
4552 if (table[0] != 0x20 && table[0] != 0x21) {
4553 NV_ERROR(dev, "Output script table version 0x%02x unknown\n",
4554 table[0]);
4555 return 1;
4556 }
4557
4558 /*
4559 * The output script tables describing a particular output type
4560 * look as follows:
4561 *
4562 * offset + 0 (32 bits): output this table matches (hash of DCB)
4563 * offset + 4 ( 8 bits): unknown
4564 * offset + 5 ( 8 bits): number of configurations
4565 * offset + 6 (16 bits): pointer to some script
4566 * offset + 8 (16 bits): pointer to some script
4567 *
4568 * headerlen == 10
4569 * offset + 10 : configuration 0
4570 *
4571 * headerlen == 12
4572 * offset + 10 : pointer to some script
4573 * offset + 12 : configuration 0
4574 *
4575 * Each config entry is as follows:
4576 *
4577 * offset + 0 (16 bits): unknown, assumed to be a match value
4578 * offset + 2 (16 bits): pointer to script table (clock set?)
4579 * offset + 4 (16 bits): pointer to script table (reset?)
4580 *
4581 * There doesn't appear to be a count value to say how many
4582 * entries exist in each script table, instead, a 0 value in
4583 * the first 16-bit word seems to indicate both the end of the
4584 * list and the default entry. The second 16-bit word in the
4585 * script tables is a pointer to the script to execute.
4586 */
4587
Maarten Maathuisef2bb502009-12-13 16:53:12 +01004588 NV_DEBUG_KMS(dev, "Searching for output entry for %d %d %d\n",
Ben Skeggs6ee73862009-12-11 19:24:15 +10004589 dcbent->type, dcbent->location, dcbent->or);
4590 otable = bios_output_config_match(dev, dcbent, table[1] +
4591 bios->display.script_table_ptr,
Ben Skeggs1eb38102010-06-01 13:40:41 +10004592 table[2], table[3], table[0] >= 0x21);
Ben Skeggs6ee73862009-12-11 19:24:15 +10004593 if (!otable) {
Ben Skeggs54bf67d2010-08-04 23:09:30 +10004594 NV_DEBUG_KMS(dev, "failed to match any output table\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +10004595 return 1;
4596 }
4597
Ben Skeggs02e4f582011-07-06 21:21:42 +10004598 if (pclk < -2 || pclk > 0) {
Ben Skeggs6ee73862009-12-11 19:24:15 +10004599 /* Try to find matching script table entry */
4600 for (i = 0; i < otable[5]; i++) {
Ben Skeggs02e4f582011-07-06 21:21:42 +10004601 if (ROM16(otable[table[4] + i*6]) == type)
Ben Skeggs6ee73862009-12-11 19:24:15 +10004602 break;
4603 }
4604
4605 if (i == otable[5]) {
4606 NV_ERROR(dev, "Table 0x%04x not found for %d/%d, "
4607 "using first\n",
Ben Skeggs02e4f582011-07-06 21:21:42 +10004608 type, dcbent->type, dcbent->or);
Ben Skeggs6ee73862009-12-11 19:24:15 +10004609 i = 0;
4610 }
4611 }
4612
Ben Skeggs02e4f582011-07-06 21:21:42 +10004613 if (pclk == 0) {
Ben Skeggs6ee73862009-12-11 19:24:15 +10004614 script = ROM16(otable[6]);
4615 if (!script) {
Maarten Maathuisef2bb502009-12-13 16:53:12 +01004616 NV_DEBUG_KMS(dev, "output script 0 not found\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +10004617 return 1;
4618 }
4619
Ben Skeggs45a68a02010-08-13 08:37:55 +10004620 NV_DEBUG_KMS(dev, "0x%04X: parsing output script 0\n", script);
Ben Skeggs02e4f582011-07-06 21:21:42 +10004621 nouveau_bios_run_init_table(dev, script, dcbent, crtc);
Ben Skeggs6ee73862009-12-11 19:24:15 +10004622 } else
Ben Skeggs02e4f582011-07-06 21:21:42 +10004623 if (pclk == -1) {
Ben Skeggs6ee73862009-12-11 19:24:15 +10004624 script = ROM16(otable[8]);
4625 if (!script) {
Maarten Maathuisef2bb502009-12-13 16:53:12 +01004626 NV_DEBUG_KMS(dev, "output script 1 not found\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +10004627 return 1;
4628 }
4629
Ben Skeggs45a68a02010-08-13 08:37:55 +10004630 NV_DEBUG_KMS(dev, "0x%04X: parsing output script 1\n", script);
Ben Skeggs02e4f582011-07-06 21:21:42 +10004631 nouveau_bios_run_init_table(dev, script, dcbent, crtc);
Ben Skeggs6ee73862009-12-11 19:24:15 +10004632 } else
Ben Skeggs02e4f582011-07-06 21:21:42 +10004633 if (pclk == -2) {
Ben Skeggs6ee73862009-12-11 19:24:15 +10004634 if (table[4] >= 12)
4635 script = ROM16(otable[10]);
4636 else
4637 script = 0;
4638 if (!script) {
Maarten Maathuisef2bb502009-12-13 16:53:12 +01004639 NV_DEBUG_KMS(dev, "output script 2 not found\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +10004640 return 1;
4641 }
4642
Ben Skeggs45a68a02010-08-13 08:37:55 +10004643 NV_DEBUG_KMS(dev, "0x%04X: parsing output script 2\n", script);
Ben Skeggs02e4f582011-07-06 21:21:42 +10004644 nouveau_bios_run_init_table(dev, script, dcbent, crtc);
Ben Skeggs6ee73862009-12-11 19:24:15 +10004645 } else
Ben Skeggs02e4f582011-07-06 21:21:42 +10004646 if (pclk > 0) {
Ben Skeggs6ee73862009-12-11 19:24:15 +10004647 script = ROM16(otable[table[4] + i*6 + 2]);
4648 if (script)
Ben Skeggs02e4f582011-07-06 21:21:42 +10004649 script = clkcmptable(bios, script, pclk);
Ben Skeggs6ee73862009-12-11 19:24:15 +10004650 if (!script) {
Ben Skeggs54bf67d2010-08-04 23:09:30 +10004651 NV_DEBUG_KMS(dev, "clock script 0 not found\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +10004652 return 1;
4653 }
4654
Ben Skeggs45a68a02010-08-13 08:37:55 +10004655 NV_DEBUG_KMS(dev, "0x%04X: parsing clock script 0\n", script);
Ben Skeggs02e4f582011-07-06 21:21:42 +10004656 nouveau_bios_run_init_table(dev, script, dcbent, crtc);
Ben Skeggs6ee73862009-12-11 19:24:15 +10004657 } else
Ben Skeggs02e4f582011-07-06 21:21:42 +10004658 if (pclk < 0) {
Ben Skeggs6ee73862009-12-11 19:24:15 +10004659 script = ROM16(otable[table[4] + i*6 + 4]);
4660 if (script)
Ben Skeggs02e4f582011-07-06 21:21:42 +10004661 script = clkcmptable(bios, script, -pclk);
Ben Skeggs6ee73862009-12-11 19:24:15 +10004662 if (!script) {
Maarten Maathuisef2bb502009-12-13 16:53:12 +01004663 NV_DEBUG_KMS(dev, "clock script 1 not found\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +10004664 return 1;
4665 }
4666
Ben Skeggs45a68a02010-08-13 08:37:55 +10004667 NV_DEBUG_KMS(dev, "0x%04X: parsing clock script 1\n", script);
Ben Skeggs02e4f582011-07-06 21:21:42 +10004668 nouveau_bios_run_init_table(dev, script, dcbent, crtc);
Ben Skeggs6ee73862009-12-11 19:24:15 +10004669 }
4670
4671 return 0;
4672}
4673
4674
4675int run_tmds_table(struct drm_device *dev, struct dcb_entry *dcbent, int head, int pxclk)
4676{
4677 /*
4678 * the pxclk parameter is in kHz
4679 *
4680 * This runs the TMDS regs setting code found on BIT bios cards
4681 *
4682 * For ffs(or) == 1 use the first table, for ffs(or) == 2 and
4683 * ffs(or) == 3, use the second.
4684 */
4685
4686 struct drm_nouveau_private *dev_priv = dev->dev_private;
Ben Skeggs04a39c52010-02-24 10:03:05 +10004687 struct nvbios *bios = &dev_priv->vbios;
4688 int cv = bios->chip_version;
Ben Skeggs6ee73862009-12-11 19:24:15 +10004689 uint16_t clktable = 0, scriptptr;
4690 uint32_t sel_clk_binding, sel_clk;
4691
4692 /* pre-nv17 off-chip tmds uses scripts, post nv17 doesn't */
4693 if (cv >= 0x17 && cv != 0x1a && cv != 0x20 &&
4694 dcbent->location != DCB_LOC_ON_CHIP)
4695 return 0;
4696
4697 switch (ffs(dcbent->or)) {
4698 case 1:
4699 clktable = bios->tmds.output0_script_ptr;
4700 break;
4701 case 2:
4702 case 3:
4703 clktable = bios->tmds.output1_script_ptr;
4704 break;
4705 }
4706
4707 if (!clktable) {
4708 NV_ERROR(dev, "Pixel clock comparison table not found\n");
4709 return -EINVAL;
4710 }
4711
4712 scriptptr = clkcmptable(bios, clktable, pxclk);
4713
4714 if (!scriptptr) {
4715 NV_ERROR(dev, "TMDS output init script not found\n");
4716 return -ENOENT;
4717 }
4718
4719 /* don't let script change pll->head binding */
4720 sel_clk_binding = bios_rd32(bios, NV_PRAMDAC_SEL_CLK) & 0x50000;
4721 run_digital_op_script(dev, scriptptr, dcbent, head, pxclk >= 165000);
4722 sel_clk = NVReadRAMDAC(dev, 0, NV_PRAMDAC_SEL_CLK) & ~0x50000;
4723 NVWriteRAMDAC(dev, 0, NV_PRAMDAC_SEL_CLK, sel_clk | sel_clk_binding);
4724
4725 return 0;
4726}
4727
Ben Skeggs855a95e2010-09-16 15:25:25 +10004728struct pll_mapping {
4729 u8 type;
4730 u32 reg;
4731};
4732
4733static struct pll_mapping nv04_pll_mapping[] = {
4734 { PLL_CORE , NV_PRAMDAC_NVPLL_COEFF },
4735 { PLL_MEMORY, NV_PRAMDAC_MPLL_COEFF },
4736 { PLL_VPLL0 , NV_PRAMDAC_VPLL_COEFF },
4737 { PLL_VPLL1 , NV_RAMDAC_VPLL2 },
4738 {}
4739};
4740
4741static struct pll_mapping nv40_pll_mapping[] = {
4742 { PLL_CORE , 0x004000 },
4743 { PLL_MEMORY, 0x004020 },
4744 { PLL_VPLL0 , NV_PRAMDAC_VPLL_COEFF },
4745 { PLL_VPLL1 , NV_RAMDAC_VPLL2 },
4746 {}
4747};
4748
4749static struct pll_mapping nv50_pll_mapping[] = {
4750 { PLL_CORE , 0x004028 },
4751 { PLL_SHADER, 0x004020 },
4752 { PLL_UNK03 , 0x004000 },
4753 { PLL_MEMORY, 0x004008 },
4754 { PLL_UNK40 , 0x00e810 },
4755 { PLL_UNK41 , 0x00e818 },
4756 { PLL_UNK42 , 0x00e824 },
4757 { PLL_VPLL0 , 0x614100 },
4758 { PLL_VPLL1 , 0x614900 },
4759 {}
4760};
4761
4762static struct pll_mapping nv84_pll_mapping[] = {
4763 { PLL_CORE , 0x004028 },
4764 { PLL_SHADER, 0x004020 },
4765 { PLL_MEMORY, 0x004008 },
4766 { PLL_UNK05 , 0x004030 },
4767 { PLL_UNK41 , 0x00e818 },
4768 { PLL_VPLL0 , 0x614100 },
4769 { PLL_VPLL1 , 0x614900 },
4770 {}
4771};
4772
4773u32
4774get_pll_register(struct drm_device *dev, enum pll_types type)
4775{
4776 struct drm_nouveau_private *dev_priv = dev->dev_private;
4777 struct nvbios *bios = &dev_priv->vbios;
4778 struct pll_mapping *map;
4779 int i;
4780
4781 if (dev_priv->card_type < NV_40)
4782 map = nv04_pll_mapping;
4783 else
4784 if (dev_priv->card_type < NV_50)
4785 map = nv40_pll_mapping;
4786 else {
4787 u8 *plim = &bios->data[bios->pll_limit_tbl_ptr];
4788
Ben Skeggs56edd962010-09-24 09:15:50 +10004789 if (plim[0] >= 0x30) {
Ben Skeggs855a95e2010-09-16 15:25:25 +10004790 u8 *entry = plim + plim[1];
4791 for (i = 0; i < plim[3]; i++, entry += plim[2]) {
4792 if (entry[0] == type)
4793 return ROM32(entry[3]);
4794 }
4795
4796 return 0;
4797 }
4798
4799 if (dev_priv->chipset == 0x50)
4800 map = nv50_pll_mapping;
4801 else
4802 map = nv84_pll_mapping;
4803 }
4804
4805 while (map->reg) {
4806 if (map->type == type)
4807 return map->reg;
4808 map++;
4809 }
4810
4811 return 0;
4812}
4813
Ben Skeggs6ee73862009-12-11 19:24:15 +10004814int get_pll_limits(struct drm_device *dev, uint32_t limit_match, struct pll_lims *pll_lim)
4815{
4816 /*
4817 * PLL limits table
4818 *
4819 * Version 0x10: NV30, NV31
4820 * One byte header (version), one record of 24 bytes
4821 * Version 0x11: NV36 - Not implemented
4822 * Seems to have same record style as 0x10, but 3 records rather than 1
4823 * Version 0x20: Found on Geforce 6 cards
4824 * Trivial 4 byte BIT header. 31 (0x1f) byte record length
4825 * Version 0x21: Found on Geforce 7, 8 and some Geforce 6 cards
4826 * 5 byte header, fifth byte of unknown purpose. 35 (0x23) byte record
4827 * length in general, some (integrated) have an extra configuration byte
4828 * Version 0x30: Found on Geforce 8, separates the register mapping
4829 * from the limits tables.
4830 */
4831
4832 struct drm_nouveau_private *dev_priv = dev->dev_private;
Ben Skeggs04a39c52010-02-24 10:03:05 +10004833 struct nvbios *bios = &dev_priv->vbios;
4834 int cv = bios->chip_version, pllindex = 0;
Ben Skeggs6ee73862009-12-11 19:24:15 +10004835 uint8_t pll_lim_ver = 0, headerlen = 0, recordlen = 0, entries = 0;
4836 uint32_t crystal_strap_mask, crystal_straps;
4837
4838 if (!bios->pll_limit_tbl_ptr) {
4839 if (cv == 0x30 || cv == 0x31 || cv == 0x35 || cv == 0x36 ||
4840 cv >= 0x40) {
4841 NV_ERROR(dev, "Pointer to PLL limits table invalid\n");
4842 return -EINVAL;
4843 }
4844 } else
4845 pll_lim_ver = bios->data[bios->pll_limit_tbl_ptr];
4846
4847 crystal_strap_mask = 1 << 6;
4848 /* open coded dev->twoHeads test */
4849 if (cv > 0x10 && cv != 0x15 && cv != 0x1a && cv != 0x20)
4850 crystal_strap_mask |= 1 << 22;
4851 crystal_straps = nvReadEXTDEV(dev, NV_PEXTDEV_BOOT_0) &
4852 crystal_strap_mask;
4853
4854 switch (pll_lim_ver) {
4855 /*
4856 * We use version 0 to indicate a pre limit table bios (single stage
4857 * pll) and load the hard coded limits instead.
4858 */
4859 case 0:
4860 break;
4861 case 0x10:
4862 case 0x11:
4863 /*
4864 * Strictly v0x11 has 3 entries, but the last two don't seem
4865 * to get used.
4866 */
4867 headerlen = 1;
4868 recordlen = 0x18;
4869 entries = 1;
4870 pllindex = 0;
4871 break;
4872 case 0x20:
4873 case 0x21:
4874 case 0x30:
4875 case 0x40:
4876 headerlen = bios->data[bios->pll_limit_tbl_ptr + 1];
4877 recordlen = bios->data[bios->pll_limit_tbl_ptr + 2];
4878 entries = bios->data[bios->pll_limit_tbl_ptr + 3];
4879 break;
4880 default:
4881 NV_ERROR(dev, "PLL limits table revision 0x%X not currently "
4882 "supported\n", pll_lim_ver);
4883 return -ENOSYS;
4884 }
4885
4886 /* initialize all members to zero */
4887 memset(pll_lim, 0, sizeof(struct pll_lims));
4888
Ben Skeggs855a95e2010-09-16 15:25:25 +10004889 /* if we were passed a type rather than a register, figure
4890 * out the register and store it
4891 */
4892 if (limit_match > PLL_MAX)
4893 pll_lim->reg = limit_match;
Ben Skeggs6f876982010-09-16 16:47:14 +10004894 else {
Ben Skeggs855a95e2010-09-16 15:25:25 +10004895 pll_lim->reg = get_pll_register(dev, limit_match);
Ben Skeggs6f876982010-09-16 16:47:14 +10004896 if (!pll_lim->reg)
4897 return -ENOENT;
4898 }
Ben Skeggs855a95e2010-09-16 15:25:25 +10004899
Ben Skeggs6ee73862009-12-11 19:24:15 +10004900 if (pll_lim_ver == 0x10 || pll_lim_ver == 0x11) {
4901 uint8_t *pll_rec = &bios->data[bios->pll_limit_tbl_ptr + headerlen + recordlen * pllindex];
4902
4903 pll_lim->vco1.minfreq = ROM32(pll_rec[0]);
4904 pll_lim->vco1.maxfreq = ROM32(pll_rec[4]);
4905 pll_lim->vco2.minfreq = ROM32(pll_rec[8]);
4906 pll_lim->vco2.maxfreq = ROM32(pll_rec[12]);
4907 pll_lim->vco1.min_inputfreq = ROM32(pll_rec[16]);
4908 pll_lim->vco2.min_inputfreq = ROM32(pll_rec[20]);
4909 pll_lim->vco1.max_inputfreq = pll_lim->vco2.max_inputfreq = INT_MAX;
4910
4911 /* these values taken from nv30/31/36 */
4912 pll_lim->vco1.min_n = 0x1;
4913 if (cv == 0x36)
4914 pll_lim->vco1.min_n = 0x5;
4915 pll_lim->vco1.max_n = 0xff;
4916 pll_lim->vco1.min_m = 0x1;
4917 pll_lim->vco1.max_m = 0xd;
4918 pll_lim->vco2.min_n = 0x4;
4919 /*
4920 * On nv30, 31, 36 (i.e. all cards with two stage PLLs with this
4921 * table version (apart from nv35)), N2 is compared to
4922 * maxN2 (0x46) and 10 * maxM2 (0x4), so set maxN2 to 0x28 and
4923 * save a comparison
4924 */
4925 pll_lim->vco2.max_n = 0x28;
4926 if (cv == 0x30 || cv == 0x35)
4927 /* only 5 bits available for N2 on nv30/35 */
4928 pll_lim->vco2.max_n = 0x1f;
4929 pll_lim->vco2.min_m = 0x1;
4930 pll_lim->vco2.max_m = 0x4;
4931 pll_lim->max_log2p = 0x7;
4932 pll_lim->max_usable_log2p = 0x6;
4933 } else if (pll_lim_ver == 0x20 || pll_lim_ver == 0x21) {
4934 uint16_t plloffs = bios->pll_limit_tbl_ptr + headerlen;
Ben Skeggs6ee73862009-12-11 19:24:15 +10004935 uint8_t *pll_rec;
4936 int i;
4937
4938 /*
4939 * First entry is default match, if nothing better. warn if
4940 * reg field nonzero
4941 */
4942 if (ROM32(bios->data[plloffs]))
4943 NV_WARN(dev, "Default PLL limit entry has non-zero "
4944 "register field\n");
4945
Ben Skeggs6ee73862009-12-11 19:24:15 +10004946 for (i = 1; i < entries; i++)
Ben Skeggs855a95e2010-09-16 15:25:25 +10004947 if (ROM32(bios->data[plloffs + recordlen * i]) == pll_lim->reg) {
Ben Skeggs6ee73862009-12-11 19:24:15 +10004948 pllindex = i;
4949 break;
4950 }
4951
Emil Velikoveadc69c2010-09-26 20:26:02 +01004952 if ((dev_priv->card_type >= NV_50) && (pllindex == 0)) {
4953 NV_ERROR(dev, "Register 0x%08x not found in PLL "
4954 "limits table", pll_lim->reg);
4955 return -ENOENT;
4956 }
4957
Ben Skeggs6ee73862009-12-11 19:24:15 +10004958 pll_rec = &bios->data[plloffs + recordlen * pllindex];
4959
4960 BIOSLOG(bios, "Loading PLL limits for reg 0x%08x\n",
Ben Skeggs855a95e2010-09-16 15:25:25 +10004961 pllindex ? pll_lim->reg : 0);
Ben Skeggs6ee73862009-12-11 19:24:15 +10004962
4963 /*
4964 * Frequencies are stored in tables in MHz, kHz are more
4965 * useful, so we convert.
4966 */
4967
4968 /* What output frequencies can each VCO generate? */
4969 pll_lim->vco1.minfreq = ROM16(pll_rec[4]) * 1000;
4970 pll_lim->vco1.maxfreq = ROM16(pll_rec[6]) * 1000;
4971 pll_lim->vco2.minfreq = ROM16(pll_rec[8]) * 1000;
4972 pll_lim->vco2.maxfreq = ROM16(pll_rec[10]) * 1000;
4973
4974 /* What input frequencies they accept (past the m-divider)? */
4975 pll_lim->vco1.min_inputfreq = ROM16(pll_rec[12]) * 1000;
4976 pll_lim->vco2.min_inputfreq = ROM16(pll_rec[14]) * 1000;
4977 pll_lim->vco1.max_inputfreq = ROM16(pll_rec[16]) * 1000;
4978 pll_lim->vco2.max_inputfreq = ROM16(pll_rec[18]) * 1000;
4979
4980 /* What values are accepted as multiplier and divider? */
4981 pll_lim->vco1.min_n = pll_rec[20];
4982 pll_lim->vco1.max_n = pll_rec[21];
4983 pll_lim->vco1.min_m = pll_rec[22];
4984 pll_lim->vco1.max_m = pll_rec[23];
4985 pll_lim->vco2.min_n = pll_rec[24];
4986 pll_lim->vco2.max_n = pll_rec[25];
4987 pll_lim->vco2.min_m = pll_rec[26];
4988 pll_lim->vco2.max_m = pll_rec[27];
4989
4990 pll_lim->max_usable_log2p = pll_lim->max_log2p = pll_rec[29];
4991 if (pll_lim->max_log2p > 0x7)
4992 /* pll decoding in nv_hw.c assumes never > 7 */
4993 NV_WARN(dev, "Max log2 P value greater than 7 (%d)\n",
4994 pll_lim->max_log2p);
4995 if (cv < 0x60)
4996 pll_lim->max_usable_log2p = 0x6;
4997 pll_lim->log2p_bias = pll_rec[30];
4998
4999 if (recordlen > 0x22)
5000 pll_lim->refclk = ROM32(pll_rec[31]);
5001
5002 if (recordlen > 0x23 && pll_rec[35])
5003 NV_WARN(dev,
5004 "Bits set in PLL configuration byte (%x)\n",
5005 pll_rec[35]);
5006
5007 /* C51 special not seen elsewhere */
5008 if (cv == 0x51 && !pll_lim->refclk) {
5009 uint32_t sel_clk = bios_rd32(bios, NV_PRAMDAC_SEL_CLK);
5010
Ben Skeggs855a95e2010-09-16 15:25:25 +10005011 if ((pll_lim->reg == NV_PRAMDAC_VPLL_COEFF && sel_clk & 0x20) ||
5012 (pll_lim->reg == NV_RAMDAC_VPLL2 && sel_clk & 0x80)) {
Ben Skeggs6ee73862009-12-11 19:24:15 +10005013 if (bios_idxprt_rd(bios, NV_CIO_CRX__COLOR, NV_CIO_CRE_CHIP_ID_INDEX) < 0xa3)
5014 pll_lim->refclk = 200000;
5015 else
5016 pll_lim->refclk = 25000;
5017 }
5018 }
5019 } else if (pll_lim_ver == 0x30) { /* ver 0x30 */
5020 uint8_t *entry = &bios->data[bios->pll_limit_tbl_ptr + headerlen];
5021 uint8_t *record = NULL;
5022 int i;
5023
5024 BIOSLOG(bios, "Loading PLL limits for register 0x%08x\n",
Ben Skeggs855a95e2010-09-16 15:25:25 +10005025 pll_lim->reg);
Ben Skeggs6ee73862009-12-11 19:24:15 +10005026
5027 for (i = 0; i < entries; i++, entry += recordlen) {
Ben Skeggs855a95e2010-09-16 15:25:25 +10005028 if (ROM32(entry[3]) == pll_lim->reg) {
Ben Skeggs6ee73862009-12-11 19:24:15 +10005029 record = &bios->data[ROM16(entry[1])];
5030 break;
5031 }
5032 }
5033
5034 if (!record) {
5035 NV_ERROR(dev, "Register 0x%08x not found in PLL "
Ben Skeggs855a95e2010-09-16 15:25:25 +10005036 "limits table", pll_lim->reg);
Ben Skeggs6ee73862009-12-11 19:24:15 +10005037 return -ENOENT;
5038 }
5039
5040 pll_lim->vco1.minfreq = ROM16(record[0]) * 1000;
5041 pll_lim->vco1.maxfreq = ROM16(record[2]) * 1000;
5042 pll_lim->vco2.minfreq = ROM16(record[4]) * 1000;
5043 pll_lim->vco2.maxfreq = ROM16(record[6]) * 1000;
5044 pll_lim->vco1.min_inputfreq = ROM16(record[8]) * 1000;
5045 pll_lim->vco2.min_inputfreq = ROM16(record[10]) * 1000;
5046 pll_lim->vco1.max_inputfreq = ROM16(record[12]) * 1000;
5047 pll_lim->vco2.max_inputfreq = ROM16(record[14]) * 1000;
5048 pll_lim->vco1.min_n = record[16];
5049 pll_lim->vco1.max_n = record[17];
5050 pll_lim->vco1.min_m = record[18];
5051 pll_lim->vco1.max_m = record[19];
5052 pll_lim->vco2.min_n = record[20];
5053 pll_lim->vco2.max_n = record[21];
5054 pll_lim->vco2.min_m = record[22];
5055 pll_lim->vco2.max_m = record[23];
5056 pll_lim->max_usable_log2p = pll_lim->max_log2p = record[25];
5057 pll_lim->log2p_bias = record[27];
5058 pll_lim->refclk = ROM32(record[28]);
5059 } else if (pll_lim_ver) { /* ver 0x40 */
5060 uint8_t *entry = &bios->data[bios->pll_limit_tbl_ptr + headerlen];
5061 uint8_t *record = NULL;
5062 int i;
5063
5064 BIOSLOG(bios, "Loading PLL limits for register 0x%08x\n",
Ben Skeggs855a95e2010-09-16 15:25:25 +10005065 pll_lim->reg);
Ben Skeggs6ee73862009-12-11 19:24:15 +10005066
5067 for (i = 0; i < entries; i++, entry += recordlen) {
Ben Skeggs855a95e2010-09-16 15:25:25 +10005068 if (ROM32(entry[3]) == pll_lim->reg) {
Ben Skeggs6ee73862009-12-11 19:24:15 +10005069 record = &bios->data[ROM16(entry[1])];
5070 break;
5071 }
5072 }
5073
5074 if (!record) {
5075 NV_ERROR(dev, "Register 0x%08x not found in PLL "
Ben Skeggs855a95e2010-09-16 15:25:25 +10005076 "limits table", pll_lim->reg);
Ben Skeggs6ee73862009-12-11 19:24:15 +10005077 return -ENOENT;
5078 }
5079
5080 pll_lim->vco1.minfreq = ROM16(record[0]) * 1000;
5081 pll_lim->vco1.maxfreq = ROM16(record[2]) * 1000;
5082 pll_lim->vco1.min_inputfreq = ROM16(record[4]) * 1000;
5083 pll_lim->vco1.max_inputfreq = ROM16(record[6]) * 1000;
5084 pll_lim->vco1.min_m = record[8];
5085 pll_lim->vco1.max_m = record[9];
5086 pll_lim->vco1.min_n = record[10];
5087 pll_lim->vco1.max_n = record[11];
5088 pll_lim->min_p = record[12];
5089 pll_lim->max_p = record[13];
Ben Skeggsce521842011-04-14 11:25:26 +10005090 pll_lim->refclk = ROM16(entry[9]) * 1000;
Ben Skeggs6ee73862009-12-11 19:24:15 +10005091 }
5092
5093 /*
5094 * By now any valid limit table ought to have set a max frequency for
5095 * vco1, so if it's zero it's either a pre limit table bios, or one
5096 * with an empty limit table (seen on nv18)
5097 */
5098 if (!pll_lim->vco1.maxfreq) {
5099 pll_lim->vco1.minfreq = bios->fminvco;
5100 pll_lim->vco1.maxfreq = bios->fmaxvco;
5101 pll_lim->vco1.min_inputfreq = 0;
5102 pll_lim->vco1.max_inputfreq = INT_MAX;
5103 pll_lim->vco1.min_n = 0x1;
5104 pll_lim->vco1.max_n = 0xff;
5105 pll_lim->vco1.min_m = 0x1;
5106 if (crystal_straps == 0) {
5107 /* nv05 does this, nv11 doesn't, nv10 unknown */
5108 if (cv < 0x11)
5109 pll_lim->vco1.min_m = 0x7;
5110 pll_lim->vco1.max_m = 0xd;
5111 } else {
5112 if (cv < 0x11)
5113 pll_lim->vco1.min_m = 0x8;
5114 pll_lim->vco1.max_m = 0xe;
5115 }
5116 if (cv < 0x17 || cv == 0x1a || cv == 0x20)
5117 pll_lim->max_log2p = 4;
5118 else
5119 pll_lim->max_log2p = 5;
5120 pll_lim->max_usable_log2p = pll_lim->max_log2p;
5121 }
5122
5123 if (!pll_lim->refclk)
5124 switch (crystal_straps) {
5125 case 0:
5126 pll_lim->refclk = 13500;
5127 break;
5128 case (1 << 6):
5129 pll_lim->refclk = 14318;
5130 break;
5131 case (1 << 22):
5132 pll_lim->refclk = 27000;
5133 break;
5134 case (1 << 22 | 1 << 6):
5135 pll_lim->refclk = 25000;
5136 break;
5137 }
5138
Ben Skeggs4c389f02010-04-23 03:08:02 +10005139 NV_DEBUG(dev, "pll.vco1.minfreq: %d\n", pll_lim->vco1.minfreq);
5140 NV_DEBUG(dev, "pll.vco1.maxfreq: %d\n", pll_lim->vco1.maxfreq);
5141 NV_DEBUG(dev, "pll.vco1.min_inputfreq: %d\n", pll_lim->vco1.min_inputfreq);
5142 NV_DEBUG(dev, "pll.vco1.max_inputfreq: %d\n", pll_lim->vco1.max_inputfreq);
5143 NV_DEBUG(dev, "pll.vco1.min_n: %d\n", pll_lim->vco1.min_n);
5144 NV_DEBUG(dev, "pll.vco1.max_n: %d\n", pll_lim->vco1.max_n);
5145 NV_DEBUG(dev, "pll.vco1.min_m: %d\n", pll_lim->vco1.min_m);
5146 NV_DEBUG(dev, "pll.vco1.max_m: %d\n", pll_lim->vco1.max_m);
5147 if (pll_lim->vco2.maxfreq) {
5148 NV_DEBUG(dev, "pll.vco2.minfreq: %d\n", pll_lim->vco2.minfreq);
5149 NV_DEBUG(dev, "pll.vco2.maxfreq: %d\n", pll_lim->vco2.maxfreq);
5150 NV_DEBUG(dev, "pll.vco2.min_inputfreq: %d\n", pll_lim->vco2.min_inputfreq);
5151 NV_DEBUG(dev, "pll.vco2.max_inputfreq: %d\n", pll_lim->vco2.max_inputfreq);
5152 NV_DEBUG(dev, "pll.vco2.min_n: %d\n", pll_lim->vco2.min_n);
5153 NV_DEBUG(dev, "pll.vco2.max_n: %d\n", pll_lim->vco2.max_n);
5154 NV_DEBUG(dev, "pll.vco2.min_m: %d\n", pll_lim->vco2.min_m);
5155 NV_DEBUG(dev, "pll.vco2.max_m: %d\n", pll_lim->vco2.max_m);
5156 }
5157 if (!pll_lim->max_p) {
5158 NV_DEBUG(dev, "pll.max_log2p: %d\n", pll_lim->max_log2p);
5159 NV_DEBUG(dev, "pll.log2p_bias: %d\n", pll_lim->log2p_bias);
5160 } else {
5161 NV_DEBUG(dev, "pll.min_p: %d\n", pll_lim->min_p);
5162 NV_DEBUG(dev, "pll.max_p: %d\n", pll_lim->max_p);
5163 }
5164 NV_DEBUG(dev, "pll.refclk: %d\n", pll_lim->refclk);
Ben Skeggs6ee73862009-12-11 19:24:15 +10005165
5166 return 0;
5167}
5168
5169static void parse_bios_version(struct drm_device *dev, struct nvbios *bios, uint16_t offset)
5170{
5171 /*
5172 * offset + 0 (8 bits): Micro version
5173 * offset + 1 (8 bits): Minor version
5174 * offset + 2 (8 bits): Chip version
5175 * offset + 3 (8 bits): Major version
5176 */
5177
5178 bios->major_version = bios->data[offset + 3];
Ben Skeggs04a39c52010-02-24 10:03:05 +10005179 bios->chip_version = bios->data[offset + 2];
Ben Skeggs6ee73862009-12-11 19:24:15 +10005180 NV_TRACE(dev, "Bios version %02x.%02x.%02x.%02x\n",
5181 bios->data[offset + 3], bios->data[offset + 2],
5182 bios->data[offset + 1], bios->data[offset]);
5183}
5184
5185static void parse_script_table_pointers(struct nvbios *bios, uint16_t offset)
5186{
5187 /*
5188 * Parses the init table segment for pointers used in script execution.
5189 *
5190 * offset + 0 (16 bits): init script tables pointer
5191 * offset + 2 (16 bits): macro index table pointer
5192 * offset + 4 (16 bits): macro table pointer
5193 * offset + 6 (16 bits): condition table pointer
5194 * offset + 8 (16 bits): io condition table pointer
5195 * offset + 10 (16 bits): io flag condition table pointer
5196 * offset + 12 (16 bits): init function table pointer
5197 */
5198
5199 bios->init_script_tbls_ptr = ROM16(bios->data[offset]);
5200 bios->macro_index_tbl_ptr = ROM16(bios->data[offset + 2]);
5201 bios->macro_tbl_ptr = ROM16(bios->data[offset + 4]);
5202 bios->condition_tbl_ptr = ROM16(bios->data[offset + 6]);
5203 bios->io_condition_tbl_ptr = ROM16(bios->data[offset + 8]);
5204 bios->io_flag_condition_tbl_ptr = ROM16(bios->data[offset + 10]);
5205 bios->init_function_tbl_ptr = ROM16(bios->data[offset + 12]);
5206}
5207
5208static int parse_bit_A_tbl_entry(struct drm_device *dev, struct nvbios *bios, struct bit_entry *bitentry)
5209{
5210 /*
5211 * Parses the load detect values for g80 cards.
5212 *
5213 * offset + 0 (16 bits): loadval table pointer
5214 */
5215
5216 uint16_t load_table_ptr;
5217 uint8_t version, headerlen, entrylen, num_entries;
5218
5219 if (bitentry->length != 3) {
5220 NV_ERROR(dev, "Do not understand BIT A table\n");
5221 return -EINVAL;
5222 }
5223
5224 load_table_ptr = ROM16(bios->data[bitentry->offset]);
5225
5226 if (load_table_ptr == 0x0) {
Ben Skeggs1562ffd2011-06-01 14:11:10 +10005227 NV_DEBUG(dev, "Pointer to BIT loadval table invalid\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +10005228 return -EINVAL;
5229 }
5230
5231 version = bios->data[load_table_ptr];
5232
5233 if (version != 0x10) {
5234 NV_ERROR(dev, "BIT loadval table version %d.%d not supported\n",
5235 version >> 4, version & 0xF);
5236 return -ENOSYS;
5237 }
5238
5239 headerlen = bios->data[load_table_ptr + 1];
5240 entrylen = bios->data[load_table_ptr + 2];
5241 num_entries = bios->data[load_table_ptr + 3];
5242
5243 if (headerlen != 4 || entrylen != 4 || num_entries != 2) {
5244 NV_ERROR(dev, "Do not understand BIT loadval table\n");
5245 return -EINVAL;
5246 }
5247
5248 /* First entry is normal dac, 2nd tv-out perhaps? */
Ben Skeggs04a39c52010-02-24 10:03:05 +10005249 bios->dactestval = ROM32(bios->data[load_table_ptr + headerlen]) & 0x3ff;
Ben Skeggs6ee73862009-12-11 19:24:15 +10005250
5251 return 0;
5252}
5253
5254static int parse_bit_C_tbl_entry(struct drm_device *dev, struct nvbios *bios, struct bit_entry *bitentry)
5255{
5256 /*
5257 * offset + 8 (16 bits): PLL limits table pointer
5258 *
5259 * There's more in here, but that's unknown.
5260 */
5261
5262 if (bitentry->length < 10) {
5263 NV_ERROR(dev, "Do not understand BIT C table\n");
5264 return -EINVAL;
5265 }
5266
5267 bios->pll_limit_tbl_ptr = ROM16(bios->data[bitentry->offset + 8]);
5268
5269 return 0;
5270}
5271
5272static int parse_bit_display_tbl_entry(struct drm_device *dev, struct nvbios *bios, struct bit_entry *bitentry)
5273{
5274 /*
5275 * Parses the flat panel table segment that the bit entry points to.
5276 * Starting at bitentry->offset:
5277 *
5278 * offset + 0 (16 bits): ??? table pointer - seems to have 18 byte
5279 * records beginning with a freq.
5280 * offset + 2 (16 bits): mode table pointer
5281 */
5282
5283 if (bitentry->length != 4) {
5284 NV_ERROR(dev, "Do not understand BIT display table\n");
5285 return -EINVAL;
5286 }
5287
5288 bios->fp.fptablepointer = ROM16(bios->data[bitentry->offset + 2]);
5289
5290 return 0;
5291}
5292
5293static int parse_bit_init_tbl_entry(struct drm_device *dev, struct nvbios *bios, struct bit_entry *bitentry)
5294{
5295 /*
5296 * Parses the init table segment that the bit entry points to.
5297 *
5298 * See parse_script_table_pointers for layout
5299 */
5300
5301 if (bitentry->length < 14) {
5302 NV_ERROR(dev, "Do not understand init table\n");
5303 return -EINVAL;
5304 }
5305
5306 parse_script_table_pointers(bios, bitentry->offset);
5307
5308 if (bitentry->length >= 16)
5309 bios->some_script_ptr = ROM16(bios->data[bitentry->offset + 14]);
5310 if (bitentry->length >= 18)
5311 bios->init96_tbl_ptr = ROM16(bios->data[bitentry->offset + 16]);
5312
5313 return 0;
5314}
5315
5316static int parse_bit_i_tbl_entry(struct drm_device *dev, struct nvbios *bios, struct bit_entry *bitentry)
5317{
5318 /*
5319 * BIT 'i' (info?) table
5320 *
5321 * offset + 0 (32 bits): BIOS version dword (as in B table)
5322 * offset + 5 (8 bits): BIOS feature byte (same as for BMP?)
5323 * offset + 13 (16 bits): pointer to table containing DAC load
5324 * detection comparison values
5325 *
5326 * There's other things in the table, purpose unknown
5327 */
5328
5329 uint16_t daccmpoffset;
5330 uint8_t dacver, dacheaderlen;
5331
5332 if (bitentry->length < 6) {
5333 NV_ERROR(dev, "BIT i table too short for needed information\n");
5334 return -EINVAL;
5335 }
5336
5337 parse_bios_version(dev, bios, bitentry->offset);
5338
5339 /*
5340 * bit 4 seems to indicate a mobile bios (doesn't suffer from BMP's
5341 * Quadro identity crisis), other bits possibly as for BMP feature byte
5342 */
5343 bios->feature_byte = bios->data[bitentry->offset + 5];
5344 bios->is_mobile = bios->feature_byte & FEATURE_MOBILE;
5345
5346 if (bitentry->length < 15) {
5347 NV_WARN(dev, "BIT i table not long enough for DAC load "
5348 "detection comparison table\n");
5349 return -EINVAL;
5350 }
5351
5352 daccmpoffset = ROM16(bios->data[bitentry->offset + 13]);
5353
5354 /* doesn't exist on g80 */
5355 if (!daccmpoffset)
5356 return 0;
5357
5358 /*
5359 * The first value in the table, following the header, is the
5360 * comparison value, the second entry is a comparison value for
5361 * TV load detection.
5362 */
5363
5364 dacver = bios->data[daccmpoffset];
5365 dacheaderlen = bios->data[daccmpoffset + 1];
5366
5367 if (dacver != 0x00 && dacver != 0x10) {
5368 NV_WARN(dev, "DAC load detection comparison table version "
5369 "%d.%d not known\n", dacver >> 4, dacver & 0xf);
5370 return -ENOSYS;
5371 }
5372
Ben Skeggs04a39c52010-02-24 10:03:05 +10005373 bios->dactestval = ROM32(bios->data[daccmpoffset + dacheaderlen]);
5374 bios->tvdactestval = ROM32(bios->data[daccmpoffset + dacheaderlen + 4]);
Ben Skeggs6ee73862009-12-11 19:24:15 +10005375
5376 return 0;
5377}
5378
5379static int parse_bit_lvds_tbl_entry(struct drm_device *dev, struct nvbios *bios, struct bit_entry *bitentry)
5380{
5381 /*
5382 * Parses the LVDS table segment that the bit entry points to.
5383 * Starting at bitentry->offset:
5384 *
5385 * offset + 0 (16 bits): LVDS strap xlate table pointer
5386 */
5387
5388 if (bitentry->length != 2) {
5389 NV_ERROR(dev, "Do not understand BIT LVDS table\n");
5390 return -EINVAL;
5391 }
5392
5393 /*
5394 * No idea if it's still called the LVDS manufacturer table, but
5395 * the concept's close enough.
5396 */
5397 bios->fp.lvdsmanufacturerpointer = ROM16(bios->data[bitentry->offset]);
5398
5399 return 0;
5400}
5401
5402static int
5403parse_bit_M_tbl_entry(struct drm_device *dev, struct nvbios *bios,
5404 struct bit_entry *bitentry)
5405{
5406 /*
5407 * offset + 2 (8 bits): number of options in an
5408 * INIT_RAM_RESTRICT_ZM_REG_GROUP opcode option set
5409 * offset + 3 (16 bits): pointer to strap xlate table for RAM
5410 * restrict option selection
5411 *
5412 * There's a bunch of bits in this table other than the RAM restrict
5413 * stuff that we don't use - their use currently unknown
5414 */
5415
Ben Skeggs6ee73862009-12-11 19:24:15 +10005416 /*
5417 * Older bios versions don't have a sufficiently long table for
5418 * what we want
5419 */
5420 if (bitentry->length < 0x5)
5421 return 0;
5422
Ben Skeggs4709bff2010-09-13 15:18:40 +10005423 if (bitentry->version < 2) {
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00005424 bios->ram_restrict_group_count = bios->data[bitentry->offset + 2];
5425 bios->ram_restrict_tbl_ptr = ROM16(bios->data[bitentry->offset + 3]);
Ben Skeggs6ee73862009-12-11 19:24:15 +10005426 } else {
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00005427 bios->ram_restrict_group_count = bios->data[bitentry->offset + 0];
5428 bios->ram_restrict_tbl_ptr = ROM16(bios->data[bitentry->offset + 1]);
Ben Skeggs6ee73862009-12-11 19:24:15 +10005429 }
5430
Ben Skeggs6ee73862009-12-11 19:24:15 +10005431 return 0;
5432}
5433
5434static int parse_bit_tmds_tbl_entry(struct drm_device *dev, struct nvbios *bios, struct bit_entry *bitentry)
5435{
5436 /*
5437 * Parses the pointer to the TMDS table
5438 *
5439 * Starting at bitentry->offset:
5440 *
5441 * offset + 0 (16 bits): TMDS table pointer
5442 *
5443 * The TMDS table is typically found just before the DCB table, with a
5444 * characteristic signature of 0x11,0x13 (1.1 being version, 0x13 being
5445 * length?)
5446 *
5447 * At offset +7 is a pointer to a script, which I don't know how to
5448 * run yet.
5449 * At offset +9 is a pointer to another script, likewise
5450 * Offset +11 has a pointer to a table where the first word is a pxclk
5451 * frequency and the second word a pointer to a script, which should be
5452 * run if the comparison pxclk frequency is less than the pxclk desired.
5453 * This repeats for decreasing comparison frequencies
5454 * Offset +13 has a pointer to a similar table
5455 * The selection of table (and possibly +7/+9 script) is dictated by
5456 * "or" from the DCB.
5457 */
5458
5459 uint16_t tmdstableptr, script1, script2;
5460
5461 if (bitentry->length != 2) {
5462 NV_ERROR(dev, "Do not understand BIT TMDS table\n");
5463 return -EINVAL;
5464 }
5465
5466 tmdstableptr = ROM16(bios->data[bitentry->offset]);
Ben Skeggs98720bf2010-08-13 08:31:22 +10005467 if (!tmdstableptr) {
Ben Skeggs6ee73862009-12-11 19:24:15 +10005468 NV_ERROR(dev, "Pointer to TMDS table invalid\n");
5469 return -EINVAL;
5470 }
5471
Ben Skeggs98720bf2010-08-13 08:31:22 +10005472 NV_INFO(dev, "TMDS table version %d.%d\n",
5473 bios->data[tmdstableptr] >> 4, bios->data[tmdstableptr] & 0xf);
5474
Ben Skeggs6ee73862009-12-11 19:24:15 +10005475 /* nv50+ has v2.0, but we don't parse it atm */
Ben Skeggs98720bf2010-08-13 08:31:22 +10005476 if (bios->data[tmdstableptr] != 0x11)
Ben Skeggs6ee73862009-12-11 19:24:15 +10005477 return -ENOSYS;
Ben Skeggs6ee73862009-12-11 19:24:15 +10005478
5479 /*
5480 * These two scripts are odd: they don't seem to get run even when
5481 * they are not stubbed.
5482 */
5483 script1 = ROM16(bios->data[tmdstableptr + 7]);
5484 script2 = ROM16(bios->data[tmdstableptr + 9]);
5485 if (bios->data[script1] != 'q' || bios->data[script2] != 'q')
5486 NV_WARN(dev, "TMDS table script pointers not stubbed\n");
5487
5488 bios->tmds.output0_script_ptr = ROM16(bios->data[tmdstableptr + 11]);
5489 bios->tmds.output1_script_ptr = ROM16(bios->data[tmdstableptr + 13]);
5490
5491 return 0;
5492}
5493
5494static int
5495parse_bit_U_tbl_entry(struct drm_device *dev, struct nvbios *bios,
5496 struct bit_entry *bitentry)
5497{
5498 /*
5499 * Parses the pointer to the G80 output script tables
5500 *
5501 * Starting at bitentry->offset:
5502 *
5503 * offset + 0 (16 bits): output script table pointer
5504 */
5505
5506 uint16_t outputscripttableptr;
5507
5508 if (bitentry->length != 3) {
5509 NV_ERROR(dev, "Do not understand BIT U table\n");
5510 return -EINVAL;
5511 }
5512
5513 outputscripttableptr = ROM16(bios->data[bitentry->offset]);
5514 bios->display.script_table_ptr = outputscripttableptr;
5515 return 0;
5516}
5517
5518static int
5519parse_bit_displayport_tbl_entry(struct drm_device *dev, struct nvbios *bios,
5520 struct bit_entry *bitentry)
5521{
5522 bios->display.dp_table_ptr = ROM16(bios->data[bitentry->offset]);
5523 return 0;
5524}
5525
5526struct bit_table {
5527 const char id;
5528 int (* const parse_fn)(struct drm_device *, struct nvbios *, struct bit_entry *);
5529};
5530
5531#define BIT_TABLE(id, funcid) ((struct bit_table){ id, parse_bit_##funcid##_tbl_entry })
5532
Ben Skeggs4709bff2010-09-13 15:18:40 +10005533int
5534bit_table(struct drm_device *dev, u8 id, struct bit_entry *bit)
5535{
5536 struct drm_nouveau_private *dev_priv = dev->dev_private;
5537 struct nvbios *bios = &dev_priv->vbios;
5538 u8 entries, *entry;
5539
5540 entries = bios->data[bios->offset + 10];
5541 entry = &bios->data[bios->offset + 12];
5542 while (entries--) {
5543 if (entry[0] == id) {
5544 bit->id = entry[0];
5545 bit->version = entry[1];
5546 bit->length = ROM16(entry[2]);
5547 bit->offset = ROM16(entry[4]);
5548 bit->data = ROMPTR(bios, entry[4]);
5549 return 0;
5550 }
5551
5552 entry += bios->data[bios->offset + 9];
5553 }
5554
5555 return -ENOENT;
5556}
5557
Ben Skeggs6ee73862009-12-11 19:24:15 +10005558static int
5559parse_bit_table(struct nvbios *bios, const uint16_t bitoffset,
5560 struct bit_table *table)
5561{
5562 struct drm_device *dev = bios->dev;
Ben Skeggs6ee73862009-12-11 19:24:15 +10005563 struct bit_entry bitentry;
5564
Ben Skeggs4709bff2010-09-13 15:18:40 +10005565 if (bit_table(dev, table->id, &bitentry) == 0)
Ben Skeggs6ee73862009-12-11 19:24:15 +10005566 return table->parse_fn(dev, bios, &bitentry);
Ben Skeggs6ee73862009-12-11 19:24:15 +10005567
5568 NV_INFO(dev, "BIT table '%c' not found\n", table->id);
5569 return -ENOSYS;
5570}
5571
5572static int
5573parse_bit_structure(struct nvbios *bios, const uint16_t bitoffset)
5574{
5575 int ret;
5576
5577 /*
5578 * The only restriction on parsing order currently is having 'i' first
5579 * for use of bios->*_version or bios->feature_byte while parsing;
5580 * functions shouldn't be actually *doing* anything apart from pulling
5581 * data from the image into the bios struct, thus no interdependencies
5582 */
5583 ret = parse_bit_table(bios, bitoffset, &BIT_TABLE('i', i));
5584 if (ret) /* info? */
5585 return ret;
5586 if (bios->major_version >= 0x60) /* g80+ */
5587 parse_bit_table(bios, bitoffset, &BIT_TABLE('A', A));
5588 ret = parse_bit_table(bios, bitoffset, &BIT_TABLE('C', C));
5589 if (ret)
5590 return ret;
5591 parse_bit_table(bios, bitoffset, &BIT_TABLE('D', display));
5592 ret = parse_bit_table(bios, bitoffset, &BIT_TABLE('I', init));
5593 if (ret)
5594 return ret;
5595 parse_bit_table(bios, bitoffset, &BIT_TABLE('M', M)); /* memory? */
5596 parse_bit_table(bios, bitoffset, &BIT_TABLE('L', lvds));
5597 parse_bit_table(bios, bitoffset, &BIT_TABLE('T', tmds));
5598 parse_bit_table(bios, bitoffset, &BIT_TABLE('U', U));
5599 parse_bit_table(bios, bitoffset, &BIT_TABLE('d', displayport));
5600
5601 return 0;
5602}
5603
5604static int parse_bmp_structure(struct drm_device *dev, struct nvbios *bios, unsigned int offset)
5605{
5606 /*
5607 * Parses the BMP structure for useful things, but does not act on them
5608 *
5609 * offset + 5: BMP major version
5610 * offset + 6: BMP minor version
5611 * offset + 9: BMP feature byte
5612 * offset + 10: BCD encoded BIOS version
5613 *
5614 * offset + 18: init script table pointer (for bios versions < 5.10h)
5615 * offset + 20: extra init script table pointer (for bios
5616 * versions < 5.10h)
5617 *
5618 * offset + 24: memory init table pointer (used on early bios versions)
5619 * offset + 26: SDR memory sequencing setup data table
5620 * offset + 28: DDR memory sequencing setup data table
5621 *
5622 * offset + 54: index of I2C CRTC pair to use for CRT output
5623 * offset + 55: index of I2C CRTC pair to use for TV output
5624 * offset + 56: index of I2C CRTC pair to use for flat panel output
5625 * offset + 58: write CRTC index for I2C pair 0
5626 * offset + 59: read CRTC index for I2C pair 0
5627 * offset + 60: write CRTC index for I2C pair 1
5628 * offset + 61: read CRTC index for I2C pair 1
5629 *
5630 * offset + 67: maximum internal PLL frequency (single stage PLL)
5631 * offset + 71: minimum internal PLL frequency (single stage PLL)
5632 *
5633 * offset + 75: script table pointers, as described in
5634 * parse_script_table_pointers
5635 *
5636 * offset + 89: TMDS single link output A table pointer
5637 * offset + 91: TMDS single link output B table pointer
5638 * offset + 95: LVDS single link output A table pointer
5639 * offset + 105: flat panel timings table pointer
5640 * offset + 107: flat panel strapping translation table pointer
5641 * offset + 117: LVDS manufacturer panel config table pointer
5642 * offset + 119: LVDS manufacturer strapping translation table pointer
5643 *
5644 * offset + 142: PLL limits table pointer
5645 *
5646 * offset + 156: minimum pixel clock for LVDS dual link
5647 */
5648
5649 uint8_t *bmp = &bios->data[offset], bmp_version_major, bmp_version_minor;
5650 uint16_t bmplength;
5651 uint16_t legacy_scripts_offset, legacy_i2c_offset;
5652
5653 /* load needed defaults in case we can't parse this info */
Ben Skeggs7f245b22010-02-24 09:56:18 +10005654 bios->dcb.i2c[0].write = NV_CIO_CRE_DDC_WR__INDEX;
5655 bios->dcb.i2c[0].read = NV_CIO_CRE_DDC_STATUS__INDEX;
5656 bios->dcb.i2c[1].write = NV_CIO_CRE_DDC0_WR__INDEX;
5657 bios->dcb.i2c[1].read = NV_CIO_CRE_DDC0_STATUS__INDEX;
Ben Skeggs04a39c52010-02-24 10:03:05 +10005658 bios->digital_min_front_porch = 0x4b;
Ben Skeggs6ee73862009-12-11 19:24:15 +10005659 bios->fmaxvco = 256000;
5660 bios->fminvco = 128000;
5661 bios->fp.duallink_transition_clk = 90000;
5662
5663 bmp_version_major = bmp[5];
5664 bmp_version_minor = bmp[6];
5665
5666 NV_TRACE(dev, "BMP version %d.%d\n",
5667 bmp_version_major, bmp_version_minor);
5668
5669 /*
5670 * Make sure that 0x36 is blank and can't be mistaken for a DCB
5671 * pointer on early versions
5672 */
5673 if (bmp_version_major < 5)
5674 *(uint16_t *)&bios->data[0x36] = 0;
5675
5676 /*
5677 * Seems that the minor version was 1 for all major versions prior
5678 * to 5. Version 6 could theoretically exist, but I suspect BIT
5679 * happened instead.
5680 */
5681 if ((bmp_version_major < 5 && bmp_version_minor != 1) || bmp_version_major > 5) {
5682 NV_ERROR(dev, "You have an unsupported BMP version. "
5683 "Please send in your bios\n");
5684 return -ENOSYS;
5685 }
5686
5687 if (bmp_version_major == 0)
5688 /* nothing that's currently useful in this version */
5689 return 0;
5690 else if (bmp_version_major == 1)
5691 bmplength = 44; /* exact for 1.01 */
5692 else if (bmp_version_major == 2)
5693 bmplength = 48; /* exact for 2.01 */
5694 else if (bmp_version_major == 3)
5695 bmplength = 54;
5696 /* guessed - mem init tables added in this version */
5697 else if (bmp_version_major == 4 || bmp_version_minor < 0x1)
5698 /* don't know if 5.0 exists... */
5699 bmplength = 62;
5700 /* guessed - BMP I2C indices added in version 4*/
5701 else if (bmp_version_minor < 0x6)
5702 bmplength = 67; /* exact for 5.01 */
5703 else if (bmp_version_minor < 0x10)
5704 bmplength = 75; /* exact for 5.06 */
5705 else if (bmp_version_minor == 0x10)
5706 bmplength = 89; /* exact for 5.10h */
5707 else if (bmp_version_minor < 0x14)
5708 bmplength = 118; /* exact for 5.11h */
5709 else if (bmp_version_minor < 0x24)
5710 /*
5711 * Not sure of version where pll limits came in;
5712 * certainly exist by 0x24 though.
5713 */
5714 /* length not exact: this is long enough to get lvds members */
5715 bmplength = 123;
5716 else if (bmp_version_minor < 0x27)
5717 /*
5718 * Length not exact: this is long enough to get pll limit
5719 * member
5720 */
5721 bmplength = 144;
5722 else
5723 /*
5724 * Length not exact: this is long enough to get dual link
5725 * transition clock.
5726 */
5727 bmplength = 158;
5728
5729 /* checksum */
5730 if (nv_cksum(bmp, 8)) {
5731 NV_ERROR(dev, "Bad BMP checksum\n");
5732 return -EINVAL;
5733 }
5734
5735 /*
5736 * Bit 4 seems to indicate either a mobile bios or a quadro card --
5737 * mobile behaviour consistent (nv11+), quadro only seen nv18gl-nv36gl
5738 * (not nv10gl), bit 5 that the flat panel tables are present, and
5739 * bit 6 a tv bios.
5740 */
5741 bios->feature_byte = bmp[9];
5742
5743 parse_bios_version(dev, bios, offset + 10);
5744
5745 if (bmp_version_major < 5 || bmp_version_minor < 0x10)
5746 bios->old_style_init = true;
5747 legacy_scripts_offset = 18;
5748 if (bmp_version_major < 2)
5749 legacy_scripts_offset -= 4;
5750 bios->init_script_tbls_ptr = ROM16(bmp[legacy_scripts_offset]);
5751 bios->extra_init_script_tbl_ptr = ROM16(bmp[legacy_scripts_offset + 2]);
5752
5753 if (bmp_version_major > 2) { /* appears in BMP 3 */
5754 bios->legacy.mem_init_tbl_ptr = ROM16(bmp[24]);
5755 bios->legacy.sdr_seq_tbl_ptr = ROM16(bmp[26]);
5756 bios->legacy.ddr_seq_tbl_ptr = ROM16(bmp[28]);
5757 }
5758
5759 legacy_i2c_offset = 0x48; /* BMP version 2 & 3 */
5760 if (bmplength > 61)
5761 legacy_i2c_offset = offset + 54;
5762 bios->legacy.i2c_indices.crt = bios->data[legacy_i2c_offset];
5763 bios->legacy.i2c_indices.tv = bios->data[legacy_i2c_offset + 1];
5764 bios->legacy.i2c_indices.panel = bios->data[legacy_i2c_offset + 2];
Francisco Jerez3af76452010-06-19 13:54:48 +02005765 if (bios->data[legacy_i2c_offset + 4])
5766 bios->dcb.i2c[0].write = bios->data[legacy_i2c_offset + 4];
5767 if (bios->data[legacy_i2c_offset + 5])
5768 bios->dcb.i2c[0].read = bios->data[legacy_i2c_offset + 5];
5769 if (bios->data[legacy_i2c_offset + 6])
5770 bios->dcb.i2c[1].write = bios->data[legacy_i2c_offset + 6];
5771 if (bios->data[legacy_i2c_offset + 7])
5772 bios->dcb.i2c[1].read = bios->data[legacy_i2c_offset + 7];
Ben Skeggs6ee73862009-12-11 19:24:15 +10005773
5774 if (bmplength > 74) {
5775 bios->fmaxvco = ROM32(bmp[67]);
5776 bios->fminvco = ROM32(bmp[71]);
5777 }
5778 if (bmplength > 88)
5779 parse_script_table_pointers(bios, offset + 75);
5780 if (bmplength > 94) {
5781 bios->tmds.output0_script_ptr = ROM16(bmp[89]);
5782 bios->tmds.output1_script_ptr = ROM16(bmp[91]);
5783 /*
5784 * Never observed in use with lvds scripts, but is reused for
5785 * 18/24 bit panel interface default for EDID equipped panels
5786 * (if_is_24bit not set directly to avoid any oscillation).
5787 */
5788 bios->legacy.lvds_single_a_script_ptr = ROM16(bmp[95]);
5789 }
5790 if (bmplength > 108) {
5791 bios->fp.fptablepointer = ROM16(bmp[105]);
5792 bios->fp.fpxlatetableptr = ROM16(bmp[107]);
5793 bios->fp.xlatwidth = 1;
5794 }
5795 if (bmplength > 120) {
5796 bios->fp.lvdsmanufacturerpointer = ROM16(bmp[117]);
5797 bios->fp.fpxlatemanufacturertableptr = ROM16(bmp[119]);
5798 }
5799 if (bmplength > 143)
5800 bios->pll_limit_tbl_ptr = ROM16(bmp[142]);
5801
5802 if (bmplength > 157)
5803 bios->fp.duallink_transition_clk = ROM16(bmp[156]) * 10;
5804
5805 return 0;
5806}
5807
5808static uint16_t findstr(uint8_t *data, int n, const uint8_t *str, int len)
5809{
5810 int i, j;
5811
5812 for (i = 0; i <= (n - len); i++) {
5813 for (j = 0; j < len; j++)
5814 if (data[i + j] != str[j])
5815 break;
5816 if (j == len)
5817 return i;
5818 }
5819
5820 return 0;
5821}
5822
Ben Skeggs6ee73862009-12-11 19:24:15 +10005823static struct dcb_gpio_entry *
5824new_gpio_entry(struct nvbios *bios)
5825{
Ben Skeggse49f70f72010-09-20 10:06:50 +10005826 struct drm_device *dev = bios->dev;
Ben Skeggs7f245b22010-02-24 09:56:18 +10005827 struct dcb_gpio_table *gpio = &bios->dcb.gpio;
Ben Skeggs6ee73862009-12-11 19:24:15 +10005828
Ben Skeggse49f70f72010-09-20 10:06:50 +10005829 if (gpio->entries >= DCB_MAX_NUM_GPIO_ENTRIES) {
5830 NV_ERROR(dev, "exceeded maximum number of gpio entries!!\n");
5831 return NULL;
5832 }
5833
Ben Skeggs6ee73862009-12-11 19:24:15 +10005834 return &gpio->entry[gpio->entries++];
5835}
5836
5837struct dcb_gpio_entry *
5838nouveau_bios_gpio_entry(struct drm_device *dev, enum dcb_gpio_tag tag)
5839{
5840 struct drm_nouveau_private *dev_priv = dev->dev_private;
Ben Skeggs04a39c52010-02-24 10:03:05 +10005841 struct nvbios *bios = &dev_priv->vbios;
Ben Skeggs6ee73862009-12-11 19:24:15 +10005842 int i;
5843
Ben Skeggs7f245b22010-02-24 09:56:18 +10005844 for (i = 0; i < bios->dcb.gpio.entries; i++) {
5845 if (bios->dcb.gpio.entry[i].tag != tag)
Ben Skeggs6ee73862009-12-11 19:24:15 +10005846 continue;
5847
Ben Skeggs7f245b22010-02-24 09:56:18 +10005848 return &bios->dcb.gpio.entry[i];
Ben Skeggs6ee73862009-12-11 19:24:15 +10005849 }
5850
5851 return NULL;
5852}
5853
5854static void
Ben Skeggs6ee73862009-12-11 19:24:15 +10005855parse_dcb_gpio_table(struct nvbios *bios)
5856{
5857 struct drm_device *dev = bios->dev;
Ben Skeggse49f70f72010-09-20 10:06:50 +10005858 struct dcb_gpio_entry *e;
5859 u8 headerlen, entries, recordlen;
5860 u8 *dcb, *gpio = NULL, *entry;
Ben Skeggs6ee73862009-12-11 19:24:15 +10005861 int i;
5862
Ben Skeggse49f70f72010-09-20 10:06:50 +10005863 dcb = ROMPTR(bios, bios->data[0x36]);
5864 if (dcb[0] >= 0x30) {
5865 gpio = ROMPTR(bios, dcb[10]);
5866 if (!gpio)
5867 goto no_table;
Ben Skeggs6ee73862009-12-11 19:24:15 +10005868
Ben Skeggse49f70f72010-09-20 10:06:50 +10005869 headerlen = gpio[1];
5870 entries = gpio[2];
5871 recordlen = gpio[3];
5872 } else
Francisco Jerez5e6a7442010-09-22 23:25:00 +02005873 if (dcb[0] >= 0x22 && dcb[-1] >= 0x13) {
Ben Skeggse49f70f72010-09-20 10:06:50 +10005874 gpio = ROMPTR(bios, dcb[-15]);
5875 if (!gpio)
5876 goto no_table;
Ben Skeggs6ee73862009-12-11 19:24:15 +10005877
Ben Skeggse49f70f72010-09-20 10:06:50 +10005878 headerlen = 3;
5879 entries = gpio[2];
5880 recordlen = gpio[1];
Francisco Jerez5e6a7442010-09-22 23:25:00 +02005881 } else
5882 if (dcb[0] >= 0x22) {
5883 /* No GPIO table present, parse the TVDAC GPIO data. */
5884 uint8_t *tvdac_gpio = &dcb[-5];
Ben Skeggs6ee73862009-12-11 19:24:15 +10005885
5886 if (tvdac_gpio[0] & 1) {
Francisco Jerez5e6a7442010-09-22 23:25:00 +02005887 e = new_gpio_entry(bios);
5888 e->tag = DCB_GPIO_TVDAC0;
5889 e->line = tvdac_gpio[1] >> 4;
5890 e->invert = tvdac_gpio[0] & 2;
Ben Skeggs6ee73862009-12-11 19:24:15 +10005891 }
Francisco Jerez5e6a7442010-09-22 23:25:00 +02005892
5893 goto no_table;
Francisco Jerez20d66da2010-08-15 14:32:49 +02005894 } else {
Ben Skeggse49f70f72010-09-20 10:06:50 +10005895 NV_DEBUG(dev, "no/unknown gpio table on DCB 0x%02x\n", dcb[0]);
5896 goto no_table;
5897 }
Francisco Jerez20d66da2010-08-15 14:32:49 +02005898
Ben Skeggse49f70f72010-09-20 10:06:50 +10005899 entry = gpio + headerlen;
5900 for (i = 0; i < entries; i++, entry += recordlen) {
5901 e = new_gpio_entry(bios);
5902 if (!e)
5903 break;
Francisco Jerez20d66da2010-08-15 14:32:49 +02005904
Ben Skeggse49f70f72010-09-20 10:06:50 +10005905 if (gpio[0] < 0x40) {
5906 e->entry = ROM16(entry[0]);
5907 e->tag = (e->entry & 0x07e0) >> 5;
5908 if (e->tag == 0x3f) {
5909 bios->dcb.gpio.entries--;
5910 continue;
5911 }
5912
5913 e->line = (e->entry & 0x001f);
5914 e->invert = ((e->entry & 0xf800) >> 11) != 4;
5915 } else {
5916 e->entry = ROM32(entry[0]);
5917 e->tag = (e->entry & 0x0000ff00) >> 8;
5918 if (e->tag == 0xff) {
5919 bios->dcb.gpio.entries--;
5920 continue;
5921 }
5922
5923 e->line = (e->entry & 0x0000001f) >> 0;
Ben Skeggsd7f81722011-07-03 02:57:35 +10005924 if (gpio[0] == 0x40) {
5925 e->state_default = (e->entry & 0x01000000) >> 24;
5926 e->state[0] = (e->entry & 0x18000000) >> 27;
5927 e->state[1] = (e->entry & 0x60000000) >> 29;
5928 } else {
5929 e->state_default = (e->entry & 0x00000080) >> 7;
5930 e->state[0] = (entry[4] >> 4) & 3;
5931 e->state[1] = (entry[4] >> 6) & 3;
5932 }
Francisco Jerez20d66da2010-08-15 14:32:49 +02005933 }
Ben Skeggs6ee73862009-12-11 19:24:15 +10005934 }
5935
Ben Skeggse49f70f72010-09-20 10:06:50 +10005936no_table:
5937 /* Apple iMac G4 NV18 */
5938 if (nv_match_device(dev, 0x0189, 0x10de, 0x0010)) {
5939 e = new_gpio_entry(bios);
5940 if (e) {
5941 e->tag = DCB_GPIO_TVDAC0;
5942 e->line = 4;
5943 }
Ben Skeggs6ee73862009-12-11 19:24:15 +10005944 }
Ben Skeggs6ee73862009-12-11 19:24:15 +10005945}
5946
5947struct dcb_connector_table_entry *
5948nouveau_bios_connector_entry(struct drm_device *dev, int index)
5949{
5950 struct drm_nouveau_private *dev_priv = dev->dev_private;
Ben Skeggs04a39c52010-02-24 10:03:05 +10005951 struct nvbios *bios = &dev_priv->vbios;
Ben Skeggs6ee73862009-12-11 19:24:15 +10005952 struct dcb_connector_table_entry *cte;
5953
Ben Skeggs7f245b22010-02-24 09:56:18 +10005954 if (index >= bios->dcb.connector.entries)
Ben Skeggs6ee73862009-12-11 19:24:15 +10005955 return NULL;
5956
Ben Skeggs7f245b22010-02-24 09:56:18 +10005957 cte = &bios->dcb.connector.entry[index];
Ben Skeggs6ee73862009-12-11 19:24:15 +10005958 if (cte->type == 0xff)
5959 return NULL;
5960
5961 return cte;
5962}
5963
Ben Skeggsf66fa772010-02-24 11:09:20 +10005964static enum dcb_connector_type
5965divine_connector_type(struct nvbios *bios, int index)
5966{
5967 struct dcb_table *dcb = &bios->dcb;
5968 unsigned encoders = 0, type = DCB_CONNECTOR_NONE;
5969 int i;
5970
5971 for (i = 0; i < dcb->entries; i++) {
5972 if (dcb->entry[i].connector == index)
5973 encoders |= (1 << dcb->entry[i].type);
5974 }
5975
5976 if (encoders & (1 << OUTPUT_DP)) {
5977 if (encoders & (1 << OUTPUT_TMDS))
5978 type = DCB_CONNECTOR_DP;
5979 else
5980 type = DCB_CONNECTOR_eDP;
5981 } else
5982 if (encoders & (1 << OUTPUT_TMDS)) {
5983 if (encoders & (1 << OUTPUT_ANALOG))
5984 type = DCB_CONNECTOR_DVI_I;
5985 else
5986 type = DCB_CONNECTOR_DVI_D;
5987 } else
5988 if (encoders & (1 << OUTPUT_ANALOG)) {
5989 type = DCB_CONNECTOR_VGA;
5990 } else
5991 if (encoders & (1 << OUTPUT_LVDS)) {
5992 type = DCB_CONNECTOR_LVDS;
5993 } else
5994 if (encoders & (1 << OUTPUT_TV)) {
5995 type = DCB_CONNECTOR_TV_0;
5996 }
5997
5998 return type;
5999}
6000
Ben Skeggs6ee73862009-12-11 19:24:15 +10006001static void
Ben Skeggs53c44c32010-03-04 12:12:22 +10006002apply_dcb_connector_quirks(struct nvbios *bios, int idx)
6003{
6004 struct dcb_connector_table_entry *cte = &bios->dcb.connector.entry[idx];
6005 struct drm_device *dev = bios->dev;
6006
6007 /* Gigabyte NX85T */
Francisco Jerezacae1162010-08-15 14:31:31 +02006008 if (nv_match_device(dev, 0x0421, 0x1458, 0x344c)) {
Ben Skeggs53c44c32010-03-04 12:12:22 +10006009 if (cte->type == DCB_CONNECTOR_HDMI_1)
6010 cte->type = DCB_CONNECTOR_DVI_I;
6011 }
Emil Velikovf0d07d62011-07-10 10:59:29 +01006012
6013 /* Gigabyte GV-NX86T512H */
6014 if (nv_match_device(dev, 0x0402, 0x1458, 0x3455)) {
6015 if (cte->type == DCB_CONNECTOR_HDMI_1)
6016 cte->type = DCB_CONNECTOR_DVI_I;
6017 }
Ben Skeggs53c44c32010-03-04 12:12:22 +10006018}
6019
Ben Skeggsa589e872011-02-17 08:03:53 +10006020static const u8 hpd_gpio[16] = {
6021 0xff, 0x07, 0x08, 0xff, 0xff, 0x51, 0x52, 0xff,
6022 0xff, 0xff, 0xff, 0xff, 0xff, 0x5e, 0x5f, 0x60,
6023};
6024
Ben Skeggs53c44c32010-03-04 12:12:22 +10006025static void
Ben Skeggs6ee73862009-12-11 19:24:15 +10006026parse_dcb_connector_table(struct nvbios *bios)
6027{
6028 struct drm_device *dev = bios->dev;
Ben Skeggs7f245b22010-02-24 09:56:18 +10006029 struct dcb_connector_table *ct = &bios->dcb.connector;
Ben Skeggs6ee73862009-12-11 19:24:15 +10006030 struct dcb_connector_table_entry *cte;
Ben Skeggs7f245b22010-02-24 09:56:18 +10006031 uint8_t *conntab = &bios->data[bios->dcb.connector_table_ptr];
Ben Skeggs6ee73862009-12-11 19:24:15 +10006032 uint8_t *entry;
6033 int i;
6034
Ben Skeggs7f245b22010-02-24 09:56:18 +10006035 if (!bios->dcb.connector_table_ptr) {
Maarten Maathuisef2bb502009-12-13 16:53:12 +01006036 NV_DEBUG_KMS(dev, "No DCB connector table present\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +10006037 return;
6038 }
6039
6040 NV_INFO(dev, "DCB connector table: VHER 0x%02x %d %d %d\n",
6041 conntab[0], conntab[1], conntab[2], conntab[3]);
6042 if ((conntab[0] != 0x30 && conntab[0] != 0x40) ||
6043 (conntab[3] != 2 && conntab[3] != 4)) {
6044 NV_ERROR(dev, " Unknown! Please report.\n");
6045 return;
6046 }
6047
6048 ct->entries = conntab[2];
6049
6050 entry = conntab + conntab[1];
6051 cte = &ct->entry[0];
6052 for (i = 0; i < conntab[2]; i++, entry += conntab[3], cte++) {
Ben Skeggsd544d622010-03-10 15:52:43 +10006053 cte->index = i;
Ben Skeggs6ee73862009-12-11 19:24:15 +10006054 if (conntab[3] == 2)
6055 cte->entry = ROM16(entry[0]);
6056 else
6057 cte->entry = ROM32(entry[0]);
Ben Skeggsf66fa772010-02-24 11:09:20 +10006058
Ben Skeggs6ee73862009-12-11 19:24:15 +10006059 cte->type = (cte->entry & 0x000000ff) >> 0;
Ben Skeggsd544d622010-03-10 15:52:43 +10006060 cte->index2 = (cte->entry & 0x00000f00) >> 8;
Ben Skeggsa589e872011-02-17 08:03:53 +10006061
6062 cte->gpio_tag = ffs((cte->entry & 0x07033000) >> 12);
6063 cte->gpio_tag = hpd_gpio[cte->gpio_tag];
Ben Skeggs6ee73862009-12-11 19:24:15 +10006064
6065 if (cte->type == 0xff)
6066 continue;
6067
Ben Skeggs53c44c32010-03-04 12:12:22 +10006068 apply_dcb_connector_quirks(bios, i);
6069
Ben Skeggs6ee73862009-12-11 19:24:15 +10006070 NV_INFO(dev, " %d: 0x%08x: type 0x%02x idx %d tag 0x%02x\n",
6071 i, cte->entry, cte->type, cte->index, cte->gpio_tag);
Ben Skeggsf66fa772010-02-24 11:09:20 +10006072
6073 /* check for known types, fallback to guessing the type
6074 * from attached encoders if we hit an unknown.
6075 */
6076 switch (cte->type) {
6077 case DCB_CONNECTOR_VGA:
6078 case DCB_CONNECTOR_TV_0:
6079 case DCB_CONNECTOR_TV_1:
6080 case DCB_CONNECTOR_TV_3:
6081 case DCB_CONNECTOR_DVI_I:
6082 case DCB_CONNECTOR_DVI_D:
6083 case DCB_CONNECTOR_LVDS:
Ben Skeggs8c3f6bb2011-04-18 09:57:48 +10006084 case DCB_CONNECTOR_LVDS_SPWG:
Ben Skeggsf66fa772010-02-24 11:09:20 +10006085 case DCB_CONNECTOR_DP:
6086 case DCB_CONNECTOR_eDP:
6087 case DCB_CONNECTOR_HDMI_0:
6088 case DCB_CONNECTOR_HDMI_1:
6089 break;
6090 default:
6091 cte->type = divine_connector_type(bios, cte->index);
Ben Skeggsda647d52010-03-04 12:00:39 +10006092 NV_WARN(dev, "unknown type, using 0x%02x\n", cte->type);
Ben Skeggsf66fa772010-02-24 11:09:20 +10006093 break;
6094 }
6095
Ben Skeggsda647d52010-03-04 12:00:39 +10006096 if (nouveau_override_conntype) {
6097 int type = divine_connector_type(bios, cte->index);
6098 if (type != cte->type)
6099 NV_WARN(dev, " -> type 0x%02x\n", cte->type);
6100 }
6101
Ben Skeggs6ee73862009-12-11 19:24:15 +10006102 }
6103}
6104
Ben Skeggs7f245b22010-02-24 09:56:18 +10006105static struct dcb_entry *new_dcb_entry(struct dcb_table *dcb)
Ben Skeggs6ee73862009-12-11 19:24:15 +10006106{
6107 struct dcb_entry *entry = &dcb->entry[dcb->entries];
6108
6109 memset(entry, 0, sizeof(struct dcb_entry));
6110 entry->index = dcb->entries++;
6111
6112 return entry;
6113}
6114
Francisco Jerez2e5702a2010-11-19 18:08:47 +10006115static void fabricate_dcb_output(struct dcb_table *dcb, int type, int i2c,
6116 int heads, int or)
Ben Skeggs6ee73862009-12-11 19:24:15 +10006117{
6118 struct dcb_entry *entry = new_dcb_entry(dcb);
6119
Francisco Jerez2e5702a2010-11-19 18:08:47 +10006120 entry->type = type;
Ben Skeggs6ee73862009-12-11 19:24:15 +10006121 entry->i2c_index = i2c;
6122 entry->heads = heads;
Francisco Jerez2e5702a2010-11-19 18:08:47 +10006123 if (type != OUTPUT_ANALOG)
6124 entry->location = !DCB_LOC_ON_CHIP; /* ie OFF CHIP */
6125 entry->or = or;
Ben Skeggs6ee73862009-12-11 19:24:15 +10006126}
6127
6128static bool
Ben Skeggs7f245b22010-02-24 09:56:18 +10006129parse_dcb20_entry(struct drm_device *dev, struct dcb_table *dcb,
Ben Skeggs6ee73862009-12-11 19:24:15 +10006130 uint32_t conn, uint32_t conf, struct dcb_entry *entry)
6131{
6132 entry->type = conn & 0xf;
6133 entry->i2c_index = (conn >> 4) & 0xf;
6134 entry->heads = (conn >> 8) & 0xf;
Ben Skeggs7f245b22010-02-24 09:56:18 +10006135 if (dcb->version >= 0x40)
Ben Skeggs6ee73862009-12-11 19:24:15 +10006136 entry->connector = (conn >> 12) & 0xf;
6137 entry->bus = (conn >> 16) & 0xf;
6138 entry->location = (conn >> 20) & 0x3;
6139 entry->or = (conn >> 24) & 0xf;
Ben Skeggs6ee73862009-12-11 19:24:15 +10006140
6141 switch (entry->type) {
6142 case OUTPUT_ANALOG:
6143 /*
6144 * Although the rest of a CRT conf dword is usually
6145 * zeros, mac biosen have stuff there so we must mask
6146 */
Ben Skeggs7f245b22010-02-24 09:56:18 +10006147 entry->crtconf.maxfreq = (dcb->version < 0x30) ?
Ben Skeggs6ee73862009-12-11 19:24:15 +10006148 (conf & 0xffff) * 10 :
6149 (conf & 0xff) * 10000;
6150 break;
6151 case OUTPUT_LVDS:
6152 {
6153 uint32_t mask;
6154 if (conf & 0x1)
6155 entry->lvdsconf.use_straps_for_mode = true;
Ben Skeggs7f245b22010-02-24 09:56:18 +10006156 if (dcb->version < 0x22) {
Ben Skeggs6ee73862009-12-11 19:24:15 +10006157 mask = ~0xd;
6158 /*
6159 * The laptop in bug 14567 lies and claims to not use
6160 * straps when it does, so assume all DCB 2.0 laptops
6161 * use straps, until a broken EDID using one is produced
6162 */
6163 entry->lvdsconf.use_straps_for_mode = true;
6164 /*
6165 * Both 0x4 and 0x8 show up in v2.0 tables; assume they
6166 * mean the same thing (probably wrong, but might work)
6167 */
6168 if (conf & 0x4 || conf & 0x8)
6169 entry->lvdsconf.use_power_scripts = true;
6170 } else {
Ben Skeggsa6ed76d2010-07-12 15:33:07 +10006171 mask = ~0x7;
6172 if (conf & 0x2)
6173 entry->lvdsconf.use_acpi_for_edid = true;
Ben Skeggs6ee73862009-12-11 19:24:15 +10006174 if (conf & 0x4)
6175 entry->lvdsconf.use_power_scripts = true;
Ben Skeggsc5875472010-07-16 16:17:27 +10006176 entry->lvdsconf.sor.link = (conf & 0x00000030) >> 4;
Ben Skeggs6ee73862009-12-11 19:24:15 +10006177 }
6178 if (conf & mask) {
6179 /*
6180 * Until we even try to use these on G8x, it's
6181 * useless reporting unknown bits. They all are.
6182 */
Ben Skeggs7f245b22010-02-24 09:56:18 +10006183 if (dcb->version >= 0x40)
Ben Skeggs6ee73862009-12-11 19:24:15 +10006184 break;
6185
6186 NV_ERROR(dev, "Unknown LVDS configuration bits, "
6187 "please report\n");
6188 }
6189 break;
6190 }
6191 case OUTPUT_TV:
6192 {
Ben Skeggs7f245b22010-02-24 09:56:18 +10006193 if (dcb->version >= 0x30)
Ben Skeggs6ee73862009-12-11 19:24:15 +10006194 entry->tvconf.has_component_output = conf & (0x8 << 4);
6195 else
6196 entry->tvconf.has_component_output = false;
6197
6198 break;
6199 }
6200 case OUTPUT_DP:
6201 entry->dpconf.sor.link = (conf & 0x00000030) >> 4;
Ben Skeggs75a1fcc2011-08-04 09:55:44 +10006202 switch ((conf & 0x00e00000) >> 21) {
6203 case 0:
6204 entry->dpconf.link_bw = 162000;
6205 break;
6206 default:
6207 entry->dpconf.link_bw = 270000;
6208 break;
6209 }
Ben Skeggs6ee73862009-12-11 19:24:15 +10006210 switch ((conf & 0x0f000000) >> 24) {
6211 case 0xf:
6212 entry->dpconf.link_nr = 4;
6213 break;
6214 case 0x3:
6215 entry->dpconf.link_nr = 2;
6216 break;
6217 default:
6218 entry->dpconf.link_nr = 1;
6219 break;
6220 }
6221 break;
6222 case OUTPUT_TMDS:
Francisco Jerez27d50fc2010-08-08 17:09:06 +02006223 if (dcb->version >= 0x40)
6224 entry->tmdsconf.sor.link = (conf & 0x00000030) >> 4;
Francisco Jerez4a9f8222010-07-20 16:48:08 +02006225 else if (dcb->version >= 0x30)
6226 entry->tmdsconf.slave_addr = (conf & 0x00000700) >> 8;
Francisco Jerez27d50fc2010-08-08 17:09:06 +02006227 else if (dcb->version >= 0x22)
6228 entry->tmdsconf.slave_addr = (conf & 0x00000070) >> 4;
Francisco Jerez4a9f8222010-07-20 16:48:08 +02006229
Ben Skeggs6ee73862009-12-11 19:24:15 +10006230 break;
Ben Skeggs44a12462010-08-17 14:34:00 +10006231 case OUTPUT_EOL:
Ben Skeggs6ee73862009-12-11 19:24:15 +10006232 /* weird g80 mobile type that "nv" treats as a terminator */
Ben Skeggs7f245b22010-02-24 09:56:18 +10006233 dcb->entries--;
Ben Skeggs6ee73862009-12-11 19:24:15 +10006234 return false;
Ben Skeggse7cc51c2010-02-24 10:31:39 +10006235 default:
6236 break;
Ben Skeggs6ee73862009-12-11 19:24:15 +10006237 }
6238
Ben Skeggs23484872010-05-28 09:39:11 +10006239 if (dcb->version < 0x40) {
6240 /* Normal entries consist of a single bit, but dual link has
6241 * the next most significant bit set too
6242 */
6243 entry->duallink_possible =
6244 ((1 << (ffs(entry->or) - 1)) * 3 == entry->or);
6245 } else {
6246 entry->duallink_possible = (entry->sorconf.link == 3);
6247 }
6248
Ben Skeggs6ee73862009-12-11 19:24:15 +10006249 /* unsure what DCB version introduces this, 3.0? */
6250 if (conf & 0x100000)
6251 entry->i2c_upper_default = true;
6252
6253 return true;
6254}
6255
6256static bool
Ben Skeggs7f245b22010-02-24 09:56:18 +10006257parse_dcb15_entry(struct drm_device *dev, struct dcb_table *dcb,
Ben Skeggs6ee73862009-12-11 19:24:15 +10006258 uint32_t conn, uint32_t conf, struct dcb_entry *entry)
6259{
Ben Skeggsb0d2de82010-01-14 17:53:04 +10006260 switch (conn & 0x0000000f) {
6261 case 0:
6262 entry->type = OUTPUT_ANALOG;
6263 break;
6264 case 1:
6265 entry->type = OUTPUT_TV;
6266 break;
6267 case 2:
Francisco Jerezfba67522010-08-24 23:02:02 +02006268 case 4:
6269 if (conn & 0x10)
6270 entry->type = OUTPUT_LVDS;
6271 else
6272 entry->type = OUTPUT_TMDS;
6273 break;
Ben Skeggsb0d2de82010-01-14 17:53:04 +10006274 case 3:
6275 entry->type = OUTPUT_LVDS;
6276 break;
Ben Skeggsb0d2de82010-01-14 17:53:04 +10006277 default:
6278 NV_ERROR(dev, "Unknown DCB type %d\n", conn & 0x0000000f);
Ben Skeggs6ee73862009-12-11 19:24:15 +10006279 return false;
6280 }
Ben Skeggsb0d2de82010-01-14 17:53:04 +10006281
6282 entry->i2c_index = (conn & 0x0003c000) >> 14;
6283 entry->heads = ((conn & 0x001c0000) >> 18) + 1;
6284 entry->or = entry->heads; /* same as heads, hopefully safe enough */
6285 entry->location = (conn & 0x01e00000) >> 21;
6286 entry->bus = (conn & 0x0e000000) >> 25;
Ben Skeggs6ee73862009-12-11 19:24:15 +10006287 entry->duallink_possible = false;
6288
6289 switch (entry->type) {
6290 case OUTPUT_ANALOG:
6291 entry->crtconf.maxfreq = (conf & 0xffff) * 10;
6292 break;
Ben Skeggsb0d2de82010-01-14 17:53:04 +10006293 case OUTPUT_TV:
6294 entry->tvconf.has_component_output = false;
Ben Skeggs6ee73862009-12-11 19:24:15 +10006295 break;
Ben Skeggsb0d2de82010-01-14 17:53:04 +10006296 case OUTPUT_LVDS:
Francisco Jerez77b1d5d2011-02-03 01:56:32 +01006297 if ((conn & 0x00003f00) >> 8 != 0x10)
Ben Skeggsb0d2de82010-01-14 17:53:04 +10006298 entry->lvdsconf.use_straps_for_mode = true;
6299 entry->lvdsconf.use_power_scripts = true;
6300 break;
6301 default:
Ben Skeggs6ee73862009-12-11 19:24:15 +10006302 break;
6303 }
6304
6305 return true;
6306}
6307
Ben Skeggs7f245b22010-02-24 09:56:18 +10006308static bool parse_dcb_entry(struct drm_device *dev, struct dcb_table *dcb,
Ben Skeggs6ee73862009-12-11 19:24:15 +10006309 uint32_t conn, uint32_t conf)
6310{
Ben Skeggs7f245b22010-02-24 09:56:18 +10006311 struct dcb_entry *entry = new_dcb_entry(dcb);
Ben Skeggs6ee73862009-12-11 19:24:15 +10006312 bool ret;
6313
Ben Skeggs7f245b22010-02-24 09:56:18 +10006314 if (dcb->version >= 0x20)
6315 ret = parse_dcb20_entry(dev, dcb, conn, conf, entry);
Ben Skeggs6ee73862009-12-11 19:24:15 +10006316 else
Ben Skeggs7f245b22010-02-24 09:56:18 +10006317 ret = parse_dcb15_entry(dev, dcb, conn, conf, entry);
Ben Skeggs6ee73862009-12-11 19:24:15 +10006318 if (!ret)
6319 return ret;
6320
Ben Skeggs7f245b22010-02-24 09:56:18 +10006321 read_dcb_i2c_entry(dev, dcb->version, dcb->i2c_table,
6322 entry->i2c_index, &dcb->i2c[entry->i2c_index]);
Ben Skeggs6ee73862009-12-11 19:24:15 +10006323
6324 return true;
6325}
6326
6327static
Ben Skeggs7f245b22010-02-24 09:56:18 +10006328void merge_like_dcb_entries(struct drm_device *dev, struct dcb_table *dcb)
Ben Skeggs6ee73862009-12-11 19:24:15 +10006329{
6330 /*
6331 * DCB v2.0 lists each output combination separately.
6332 * Here we merge compatible entries to have fewer outputs, with
6333 * more options
6334 */
6335
6336 int i, newentries = 0;
6337
6338 for (i = 0; i < dcb->entries; i++) {
6339 struct dcb_entry *ient = &dcb->entry[i];
6340 int j;
6341
6342 for (j = i + 1; j < dcb->entries; j++) {
6343 struct dcb_entry *jent = &dcb->entry[j];
6344
6345 if (jent->type == 100) /* already merged entry */
6346 continue;
6347
6348 /* merge heads field when all other fields the same */
6349 if (jent->i2c_index == ient->i2c_index &&
6350 jent->type == ient->type &&
6351 jent->location == ient->location &&
6352 jent->or == ient->or) {
6353 NV_TRACE(dev, "Merging DCB entries %d and %d\n",
6354 i, j);
6355 ient->heads |= jent->heads;
6356 jent->type = 100; /* dummy value */
6357 }
6358 }
6359 }
6360
6361 /* Compact entries merged into others out of dcb */
6362 for (i = 0; i < dcb->entries; i++) {
6363 if (dcb->entry[i].type == 100)
6364 continue;
6365
6366 if (newentries != i) {
6367 dcb->entry[newentries] = dcb->entry[i];
6368 dcb->entry[newentries].index = newentries;
6369 }
6370 newentries++;
6371 }
6372
6373 dcb->entries = newentries;
6374}
6375
Ben Skeggsdf4cf1b2010-07-01 11:31:45 +10006376static bool
6377apply_dcb_encoder_quirks(struct drm_device *dev, int idx, u32 *conn, u32 *conf)
6378{
Francisco Jerez670820c2010-12-25 15:43:30 +01006379 struct drm_nouveau_private *dev_priv = dev->dev_private;
6380 struct dcb_table *dcb = &dev_priv->vbios.dcb;
6381
Ben Skeggsdf4cf1b2010-07-01 11:31:45 +10006382 /* Dell Precision M6300
6383 * DCB entry 2: 02025312 00000010
6384 * DCB entry 3: 02026312 00000020
6385 *
6386 * Identical, except apparently a different connector on a
6387 * different SOR link. Not a clue how we're supposed to know
6388 * which one is in use if it even shares an i2c line...
6389 *
6390 * Ignore the connector on the second SOR link to prevent
6391 * nasty problems until this is sorted (assuming it's not a
6392 * VBIOS bug).
6393 */
Francisco Jerezacae1162010-08-15 14:31:31 +02006394 if (nv_match_device(dev, 0x040d, 0x1028, 0x019b)) {
Ben Skeggsdf4cf1b2010-07-01 11:31:45 +10006395 if (*conn == 0x02026312 && *conf == 0x00000020)
6396 return false;
6397 }
6398
Francisco Jerez670820c2010-12-25 15:43:30 +01006399 /* GeForce3 Ti 200
6400 *
6401 * DCB reports an LVDS output that should be TMDS:
6402 * DCB entry 1: f2005014 ffffffff
6403 */
6404 if (nv_match_device(dev, 0x0201, 0x1462, 0x8851)) {
6405 if (*conn == 0xf2005014 && *conf == 0xffffffff) {
6406 fabricate_dcb_output(dcb, OUTPUT_TMDS, 1, 1, 1);
6407 return false;
6408 }
6409 }
6410
Ben Skeggsc0929b42011-03-21 11:42:51 +10006411 /* XFX GT-240X-YA
6412 *
6413 * So many things wrong here, replace the entire encoder table..
6414 */
6415 if (nv_match_device(dev, 0x0ca3, 0x1682, 0x3003)) {
6416 if (idx == 0) {
6417 *conn = 0x02001300; /* VGA, connector 1 */
6418 *conf = 0x00000028;
6419 } else
6420 if (idx == 1) {
6421 *conn = 0x01010312; /* DVI, connector 0 */
6422 *conf = 0x00020030;
6423 } else
6424 if (idx == 2) {
6425 *conn = 0x01010310; /* VGA, connector 0 */
6426 *conf = 0x00000028;
6427 } else
6428 if (idx == 3) {
6429 *conn = 0x02022362; /* HDMI, connector 2 */
6430 *conf = 0x00020010;
6431 } else {
6432 *conn = 0x0000000e; /* EOL */
6433 *conf = 0x00000000;
6434 }
6435 }
6436
Ben Skeggse540afc2011-05-30 12:53:37 +10006437 /* Some other twisted XFX board (rhbz#694914)
6438 *
6439 * The DVI/VGA encoder combo that's supposed to represent the
6440 * DVI-I connector actually point at two different ones, and
6441 * the HDMI connector ends up paired with the VGA instead.
6442 *
6443 * Connector table is missing anything for VGA at all, pointing it
6444 * an invalid conntab entry 2 so we figure it out ourself.
6445 */
6446 if (nv_match_device(dev, 0x0615, 0x1682, 0x2605)) {
6447 if (idx == 0) {
6448 *conn = 0x02002300; /* VGA, connector 2 */
6449 *conf = 0x00000028;
6450 } else
6451 if (idx == 1) {
6452 *conn = 0x01010312; /* DVI, connector 0 */
6453 *conf = 0x00020030;
6454 } else
6455 if (idx == 2) {
6456 *conn = 0x04020310; /* VGA, connector 0 */
6457 *conf = 0x00000028;
6458 } else
6459 if (idx == 3) {
6460 *conn = 0x02021322; /* HDMI, connector 1 */
6461 *conf = 0x00020010;
6462 } else {
6463 *conn = 0x0000000e; /* EOL */
6464 *conf = 0x00000000;
6465 }
6466 }
6467
Ben Skeggsdf4cf1b2010-07-01 11:31:45 +10006468 return true;
6469}
6470
Francisco Jerez2e5702a2010-11-19 18:08:47 +10006471static void
6472fabricate_dcb_encoder_table(struct drm_device *dev, struct nvbios *bios)
6473{
6474 struct dcb_table *dcb = &bios->dcb;
6475 int all_heads = (nv_two_heads(dev) ? 3 : 1);
6476
6477#ifdef __powerpc__
6478 /* Apple iMac G4 NV17 */
6479 if (of_machine_is_compatible("PowerMac4,5")) {
6480 fabricate_dcb_output(dcb, OUTPUT_TMDS, 0, all_heads, 1);
6481 fabricate_dcb_output(dcb, OUTPUT_ANALOG, 1, all_heads, 2);
6482 return;
6483 }
6484#endif
6485
6486 /* Make up some sane defaults */
6487 fabricate_dcb_output(dcb, OUTPUT_ANALOG, LEGACY_I2C_CRT, 1, 1);
6488
6489 if (nv04_tv_identify(dev, bios->legacy.i2c_indices.tv) >= 0)
6490 fabricate_dcb_output(dcb, OUTPUT_TV, LEGACY_I2C_TV,
6491 all_heads, 0);
6492
6493 else if (bios->tmds.output0_script_ptr ||
6494 bios->tmds.output1_script_ptr)
6495 fabricate_dcb_output(dcb, OUTPUT_TMDS, LEGACY_I2C_PANEL,
6496 all_heads, 1);
6497}
6498
Ben Skeggsed42f822010-01-14 15:58:10 +10006499static int
Francisco Jerez2e5702a2010-11-19 18:08:47 +10006500parse_dcb_table(struct drm_device *dev, struct nvbios *bios)
Ben Skeggs6ee73862009-12-11 19:24:15 +10006501{
Ben Skeggsed42f822010-01-14 15:58:10 +10006502 struct drm_nouveau_private *dev_priv = dev->dev_private;
Ben Skeggs7f245b22010-02-24 09:56:18 +10006503 struct dcb_table *dcb = &bios->dcb;
Ben Skeggsed42f822010-01-14 15:58:10 +10006504 uint16_t dcbptr = 0, i2ctabptr = 0;
Ben Skeggs6ee73862009-12-11 19:24:15 +10006505 uint8_t *dcbtable;
6506 uint8_t headerlen = 0x4, entries = DCB_MAX_NUM_ENTRIES;
6507 bool configblock = true;
6508 int recordlength = 8, confofs = 4;
6509 int i;
6510
Ben Skeggs6ee73862009-12-11 19:24:15 +10006511 /* get the offset from 0x36 */
Ben Skeggsed42f822010-01-14 15:58:10 +10006512 if (dev_priv->card_type > NV_04) {
6513 dcbptr = ROM16(bios->data[0x36]);
6514 if (dcbptr == 0x0000)
6515 NV_WARN(dev, "No output data (DCB) found in BIOS\n");
6516 }
Ben Skeggs6ee73862009-12-11 19:24:15 +10006517
Ben Skeggsed42f822010-01-14 15:58:10 +10006518 /* this situation likely means a really old card, pre DCB */
Ben Skeggs6ee73862009-12-11 19:24:15 +10006519 if (dcbptr == 0x0) {
Francisco Jerez2e5702a2010-11-19 18:08:47 +10006520 fabricate_dcb_encoder_table(dev, bios);
Ben Skeggs6ee73862009-12-11 19:24:15 +10006521 return 0;
6522 }
6523
6524 dcbtable = &bios->data[dcbptr];
6525
6526 /* get DCB version */
Ben Skeggs7f245b22010-02-24 09:56:18 +10006527 dcb->version = dcbtable[0];
Ben Skeggs6ee73862009-12-11 19:24:15 +10006528 NV_TRACE(dev, "Found Display Configuration Block version %d.%d\n",
Ben Skeggs7f245b22010-02-24 09:56:18 +10006529 dcb->version >> 4, dcb->version & 0xf);
Ben Skeggs6ee73862009-12-11 19:24:15 +10006530
Ben Skeggs7f245b22010-02-24 09:56:18 +10006531 if (dcb->version >= 0x20) { /* NV17+ */
Ben Skeggs6ee73862009-12-11 19:24:15 +10006532 uint32_t sig;
6533
Ben Skeggs7f245b22010-02-24 09:56:18 +10006534 if (dcb->version >= 0x30) { /* NV40+ */
Ben Skeggs6ee73862009-12-11 19:24:15 +10006535 headerlen = dcbtable[1];
6536 entries = dcbtable[2];
6537 recordlength = dcbtable[3];
6538 i2ctabptr = ROM16(dcbtable[4]);
6539 sig = ROM32(dcbtable[6]);
Ben Skeggs7f245b22010-02-24 09:56:18 +10006540 dcb->gpio_table_ptr = ROM16(dcbtable[10]);
6541 dcb->connector_table_ptr = ROM16(dcbtable[20]);
Ben Skeggs6ee73862009-12-11 19:24:15 +10006542 } else {
6543 i2ctabptr = ROM16(dcbtable[2]);
6544 sig = ROM32(dcbtable[4]);
6545 headerlen = 8;
6546 }
6547
6548 if (sig != 0x4edcbdcb) {
6549 NV_ERROR(dev, "Bad Display Configuration Block "
6550 "signature (%08X)\n", sig);
6551 return -EINVAL;
6552 }
Ben Skeggs7f245b22010-02-24 09:56:18 +10006553 } else if (dcb->version >= 0x15) { /* some NV11 and NV20 */
Ben Skeggs6ee73862009-12-11 19:24:15 +10006554 char sig[8] = { 0 };
6555
6556 strncpy(sig, (char *)&dcbtable[-7], 7);
6557 i2ctabptr = ROM16(dcbtable[2]);
6558 recordlength = 10;
6559 confofs = 6;
6560
6561 if (strcmp(sig, "DEV_REC")) {
6562 NV_ERROR(dev, "Bad Display Configuration Block "
6563 "signature (%s)\n", sig);
6564 return -EINVAL;
6565 }
6566 } else {
6567 /*
6568 * v1.4 (some NV15/16, NV11+) seems the same as v1.5, but always
6569 * has the same single (crt) entry, even when tv-out present, so
6570 * the conclusion is this version cannot really be used.
6571 * v1.2 tables (some NV6/10, and NV15+) normally have the same
6572 * 5 entries, which are not specific to the card and so no use.
6573 * v1.2 does have an I2C table that read_dcb_i2c_table can
6574 * handle, but cards exist (nv11 in #14821) with a bad i2c table
6575 * pointer, so use the indices parsed in parse_bmp_structure.
6576 * v1.1 (NV5+, maybe some NV4) is entirely unhelpful
6577 */
6578 NV_TRACEWARN(dev, "No useful information in BIOS output table; "
6579 "adding all possible outputs\n");
Francisco Jerez2e5702a2010-11-19 18:08:47 +10006580 fabricate_dcb_encoder_table(dev, bios);
Ben Skeggs6ee73862009-12-11 19:24:15 +10006581 return 0;
6582 }
6583
6584 if (!i2ctabptr)
6585 NV_WARN(dev, "No pointer to DCB I2C port table\n");
6586 else {
Ben Skeggs7f245b22010-02-24 09:56:18 +10006587 dcb->i2c_table = &bios->data[i2ctabptr];
6588 if (dcb->version >= 0x30)
6589 dcb->i2c_default_indices = dcb->i2c_table[4];
Francisco Jerez4a9f8222010-07-20 16:48:08 +02006590
6591 /*
6592 * Parse the "management" I2C bus, used for hardware
6593 * monitoring and some external TMDS transmitters.
6594 */
6595 if (dcb->version >= 0x22) {
6596 int idx = (dcb->version >= 0x40 ?
6597 dcb->i2c_default_indices & 0xf :
6598 2);
6599
6600 read_dcb_i2c_entry(dev, dcb->version, dcb->i2c_table,
6601 idx, &dcb->i2c[idx]);
6602 }
Ben Skeggs6ee73862009-12-11 19:24:15 +10006603 }
6604
Ben Skeggs6ee73862009-12-11 19:24:15 +10006605 if (entries > DCB_MAX_NUM_ENTRIES)
6606 entries = DCB_MAX_NUM_ENTRIES;
6607
6608 for (i = 0; i < entries; i++) {
6609 uint32_t connection, config = 0;
6610
6611 connection = ROM32(dcbtable[headerlen + recordlength * i]);
6612 if (configblock)
6613 config = ROM32(dcbtable[headerlen + confofs + recordlength * i]);
6614
6615 /* seen on an NV11 with DCB v1.5 */
6616 if (connection == 0x00000000)
6617 break;
6618
6619 /* seen on an NV17 with DCB v2.0 */
6620 if (connection == 0xffffffff)
6621 break;
6622
6623 if ((connection & 0x0000000f) == 0x0000000f)
6624 continue;
6625
Ben Skeggsdf4cf1b2010-07-01 11:31:45 +10006626 if (!apply_dcb_encoder_quirks(dev, i, &connection, &config))
6627 continue;
6628
Ben Skeggs6ee73862009-12-11 19:24:15 +10006629 NV_TRACEWARN(dev, "Raw DCB entry %d: %08x %08x\n",
6630 dcb->entries, connection, config);
6631
Ben Skeggs7f245b22010-02-24 09:56:18 +10006632 if (!parse_dcb_entry(dev, dcb, connection, config))
Ben Skeggs6ee73862009-12-11 19:24:15 +10006633 break;
6634 }
6635
6636 /*
6637 * apart for v2.1+ not being known for requiring merging, this
6638 * guarantees dcbent->index is the index of the entry in the rom image
6639 */
Ben Skeggs7f245b22010-02-24 09:56:18 +10006640 if (dcb->version < 0x21)
Ben Skeggs6ee73862009-12-11 19:24:15 +10006641 merge_like_dcb_entries(dev, dcb);
6642
Ben Skeggs54abb5d2010-02-24 10:48:16 +10006643 if (!dcb->entries)
6644 return -ENXIO;
6645
6646 parse_dcb_gpio_table(bios);
6647 parse_dcb_connector_table(bios);
6648 return 0;
Ben Skeggs6ee73862009-12-11 19:24:15 +10006649}
6650
6651static void
6652fixup_legacy_connector(struct nvbios *bios)
6653{
Ben Skeggs7f245b22010-02-24 09:56:18 +10006654 struct dcb_table *dcb = &bios->dcb;
Ben Skeggsdc5bc4e2010-02-24 11:53:59 +10006655 int i, i2c, i2c_conn[DCB_MAX_NUM_I2C_ENTRIES] = { };
Ben Skeggs6ee73862009-12-11 19:24:15 +10006656
6657 /*
6658 * DCB 3.0 also has the table in most cases, but there are some cards
6659 * where the table is filled with stub entries, and the DCB entriy
6660 * indices are all 0. We don't need the connector indices on pre-G80
6661 * chips (yet?) so limit the use to DCB 4.0 and above.
6662 */
Ben Skeggs7f245b22010-02-24 09:56:18 +10006663 if (dcb->version >= 0x40)
Ben Skeggs6ee73862009-12-11 19:24:15 +10006664 return;
6665
Ben Skeggsdc5bc4e2010-02-24 11:53:59 +10006666 dcb->connector.entries = 0;
6667
Ben Skeggs6ee73862009-12-11 19:24:15 +10006668 /*
6669 * No known connector info before v3.0, so make it up. the rule here
6670 * is: anything on the same i2c bus is considered to be on the same
6671 * connector. any output without an associated i2c bus is assigned
6672 * its own unique connector index.
6673 */
6674 for (i = 0; i < dcb->entries; i++) {
Ben Skeggs6ee73862009-12-11 19:24:15 +10006675 /*
6676 * Ignore the I2C index for on-chip TV-out, as there
6677 * are cards with bogus values (nv31m in bug 23212),
6678 * and it's otherwise useless.
6679 */
6680 if (dcb->entry[i].type == OUTPUT_TV &&
Ben Skeggsdc5bc4e2010-02-24 11:53:59 +10006681 dcb->entry[i].location == DCB_LOC_ON_CHIP)
Ben Skeggs6ee73862009-12-11 19:24:15 +10006682 dcb->entry[i].i2c_index = 0xf;
Ben Skeggsdc5bc4e2010-02-24 11:53:59 +10006683 i2c = dcb->entry[i].i2c_index;
6684
6685 if (i2c_conn[i2c]) {
6686 dcb->entry[i].connector = i2c_conn[i2c] - 1;
Ben Skeggs6ee73862009-12-11 19:24:15 +10006687 continue;
6688 }
6689
Ben Skeggsdc5bc4e2010-02-24 11:53:59 +10006690 dcb->entry[i].connector = dcb->connector.entries++;
6691 if (i2c != 0xf)
6692 i2c_conn[i2c] = dcb->connector.entries;
Ben Skeggs6ee73862009-12-11 19:24:15 +10006693 }
6694
Ben Skeggsdc5bc4e2010-02-24 11:53:59 +10006695 /* Fake the connector table as well as just connector indices */
6696 for (i = 0; i < dcb->connector.entries; i++) {
6697 dcb->connector.entry[i].index = i;
6698 dcb->connector.entry[i].type = divine_connector_type(bios, i);
6699 dcb->connector.entry[i].gpio_tag = 0xff;
Ben Skeggs6ee73862009-12-11 19:24:15 +10006700 }
6701}
6702
6703static void
6704fixup_legacy_i2c(struct nvbios *bios)
6705{
Ben Skeggs7f245b22010-02-24 09:56:18 +10006706 struct dcb_table *dcb = &bios->dcb;
Ben Skeggs6ee73862009-12-11 19:24:15 +10006707 int i;
6708
6709 for (i = 0; i < dcb->entries; i++) {
6710 if (dcb->entry[i].i2c_index == LEGACY_I2C_CRT)
6711 dcb->entry[i].i2c_index = bios->legacy.i2c_indices.crt;
6712 if (dcb->entry[i].i2c_index == LEGACY_I2C_PANEL)
6713 dcb->entry[i].i2c_index = bios->legacy.i2c_indices.panel;
6714 if (dcb->entry[i].i2c_index == LEGACY_I2C_TV)
6715 dcb->entry[i].i2c_index = bios->legacy.i2c_indices.tv;
6716 }
6717}
6718
6719static int load_nv17_hwsq_ucode_entry(struct drm_device *dev, struct nvbios *bios, uint16_t hwsq_offset, int entry)
6720{
6721 /*
6722 * The header following the "HWSQ" signature has the number of entries,
6723 * and the entry size
6724 *
6725 * An entry consists of a dword to write to the sequencer control reg
6726 * (0x00001304), followed by the ucode bytes, written sequentially,
6727 * starting at reg 0x00001400
6728 */
6729
6730 uint8_t bytes_to_write;
6731 uint16_t hwsq_entry_offset;
6732 int i;
6733
6734 if (bios->data[hwsq_offset] <= entry) {
6735 NV_ERROR(dev, "Too few entries in HW sequencer table for "
6736 "requested entry\n");
6737 return -ENOENT;
6738 }
6739
6740 bytes_to_write = bios->data[hwsq_offset + 1];
6741
6742 if (bytes_to_write != 36) {
6743 NV_ERROR(dev, "Unknown HW sequencer entry size\n");
6744 return -EINVAL;
6745 }
6746
6747 NV_TRACE(dev, "Loading NV17 power sequencing microcode\n");
6748
6749 hwsq_entry_offset = hwsq_offset + 2 + entry * bytes_to_write;
6750
6751 /* set sequencer control */
6752 bios_wr32(bios, 0x00001304, ROM32(bios->data[hwsq_entry_offset]));
6753 bytes_to_write -= 4;
6754
6755 /* write ucode */
6756 for (i = 0; i < bytes_to_write; i += 4)
6757 bios_wr32(bios, 0x00001400 + i, ROM32(bios->data[hwsq_entry_offset + i + 4]));
6758
6759 /* twiddle NV_PBUS_DEBUG_4 */
6760 bios_wr32(bios, NV_PBUS_DEBUG_4, bios_rd32(bios, NV_PBUS_DEBUG_4) | 0x18);
6761
6762 return 0;
6763}
6764
6765static int load_nv17_hw_sequencer_ucode(struct drm_device *dev,
6766 struct nvbios *bios)
6767{
6768 /*
6769 * BMP based cards, from NV17, need a microcode loading to correctly
6770 * control the GPIO etc for LVDS panels
6771 *
6772 * BIT based cards seem to do this directly in the init scripts
6773 *
6774 * The microcode entries are found by the "HWSQ" signature.
6775 */
6776
6777 const uint8_t hwsq_signature[] = { 'H', 'W', 'S', 'Q' };
6778 const int sz = sizeof(hwsq_signature);
6779 int hwsq_offset;
6780
6781 hwsq_offset = findstr(bios->data, bios->length, hwsq_signature, sz);
6782 if (!hwsq_offset)
6783 return 0;
6784
6785 /* always use entry 0? */
6786 return load_nv17_hwsq_ucode_entry(dev, bios, hwsq_offset + sz, 0);
6787}
6788
6789uint8_t *nouveau_bios_embedded_edid(struct drm_device *dev)
6790{
6791 struct drm_nouveau_private *dev_priv = dev->dev_private;
Ben Skeggs04a39c52010-02-24 10:03:05 +10006792 struct nvbios *bios = &dev_priv->vbios;
Ben Skeggs6ee73862009-12-11 19:24:15 +10006793 const uint8_t edid_sig[] = {
6794 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00 };
6795 uint16_t offset = 0;
6796 uint16_t newoffset;
6797 int searchlen = NV_PROM_SIZE;
6798
6799 if (bios->fp.edid)
6800 return bios->fp.edid;
6801
6802 while (searchlen) {
6803 newoffset = findstr(&bios->data[offset], searchlen,
6804 edid_sig, 8);
6805 if (!newoffset)
6806 return NULL;
6807 offset += newoffset;
6808 if (!nv_cksum(&bios->data[offset], EDID1_LEN))
6809 break;
6810
6811 searchlen -= offset;
6812 offset++;
6813 }
6814
6815 NV_TRACE(dev, "Found EDID in BIOS\n");
6816
6817 return bios->fp.edid = &bios->data[offset];
6818}
6819
6820void
6821nouveau_bios_run_init_table(struct drm_device *dev, uint16_t table,
Ben Skeggs02e4f582011-07-06 21:21:42 +10006822 struct dcb_entry *dcbent, int crtc)
Ben Skeggs6ee73862009-12-11 19:24:15 +10006823{
6824 struct drm_nouveau_private *dev_priv = dev->dev_private;
Ben Skeggs04a39c52010-02-24 10:03:05 +10006825 struct nvbios *bios = &dev_priv->vbios;
Ben Skeggs6ee73862009-12-11 19:24:15 +10006826 struct init_exec iexec = { true, false };
6827
Ben Skeggsc7ca4d12011-02-03 20:10:49 +10006828 spin_lock_bh(&bios->lock);
Ben Skeggs6ee73862009-12-11 19:24:15 +10006829 bios->display.output = dcbent;
Ben Skeggs02e4f582011-07-06 21:21:42 +10006830 bios->display.crtc = crtc;
Ben Skeggs6ee73862009-12-11 19:24:15 +10006831 parse_init_table(bios, table, &iexec);
6832 bios->display.output = NULL;
Ben Skeggsc7ca4d12011-02-03 20:10:49 +10006833 spin_unlock_bh(&bios->lock);
Ben Skeggs6ee73862009-12-11 19:24:15 +10006834}
6835
6836static bool NVInitVBIOS(struct drm_device *dev)
6837{
6838 struct drm_nouveau_private *dev_priv = dev->dev_private;
Ben Skeggs04a39c52010-02-24 10:03:05 +10006839 struct nvbios *bios = &dev_priv->vbios;
Ben Skeggs6ee73862009-12-11 19:24:15 +10006840
6841 memset(bios, 0, sizeof(struct nvbios));
Ben Skeggsc7ca4d12011-02-03 20:10:49 +10006842 spin_lock_init(&bios->lock);
Ben Skeggs6ee73862009-12-11 19:24:15 +10006843 bios->dev = dev;
6844
6845 if (!NVShadowVBIOS(dev, bios->data))
6846 return false;
6847
6848 bios->length = NV_PROM_SIZE;
6849 return true;
6850}
6851
6852static int nouveau_parse_vbios_struct(struct drm_device *dev)
6853{
6854 struct drm_nouveau_private *dev_priv = dev->dev_private;
Ben Skeggs04a39c52010-02-24 10:03:05 +10006855 struct nvbios *bios = &dev_priv->vbios;
Ben Skeggs6ee73862009-12-11 19:24:15 +10006856 const uint8_t bit_signature[] = { 0xff, 0xb8, 'B', 'I', 'T' };
6857 const uint8_t bmp_signature[] = { 0xff, 0x7f, 'N', 'V', 0x0 };
6858 int offset;
6859
6860 offset = findstr(bios->data, bios->length,
6861 bit_signature, sizeof(bit_signature));
6862 if (offset) {
6863 NV_TRACE(dev, "BIT BIOS found\n");
Ben Skeggs4709bff2010-09-13 15:18:40 +10006864 bios->type = NVBIOS_BIT;
6865 bios->offset = offset;
Ben Skeggs6ee73862009-12-11 19:24:15 +10006866 return parse_bit_structure(bios, offset + 6);
6867 }
6868
6869 offset = findstr(bios->data, bios->length,
6870 bmp_signature, sizeof(bmp_signature));
6871 if (offset) {
6872 NV_TRACE(dev, "BMP BIOS found\n");
Ben Skeggs4709bff2010-09-13 15:18:40 +10006873 bios->type = NVBIOS_BMP;
6874 bios->offset = offset;
Ben Skeggs6ee73862009-12-11 19:24:15 +10006875 return parse_bmp_structure(dev, bios, offset);
6876 }
6877
6878 NV_ERROR(dev, "No known BIOS signature found\n");
6879 return -ENODEV;
6880}
6881
6882int
6883nouveau_run_vbios_init(struct drm_device *dev)
6884{
6885 struct drm_nouveau_private *dev_priv = dev->dev_private;
Ben Skeggs04a39c52010-02-24 10:03:05 +10006886 struct nvbios *bios = &dev_priv->vbios;
Ben Skeggs6ee73862009-12-11 19:24:15 +10006887 int i, ret = 0;
6888
Francisco Jerez946fd352010-07-24 17:41:48 +02006889 /* Reset the BIOS head to 0. */
6890 bios->state.crtchead = 0;
Ben Skeggs6ee73862009-12-11 19:24:15 +10006891
6892 if (bios->major_version < 5) /* BMP only */
6893 load_nv17_hw_sequencer_ucode(dev, bios);
6894
6895 if (bios->execute) {
6896 bios->fp.last_script_invoc = 0;
6897 bios->fp.lvds_init_run = false;
6898 }
6899
6900 parse_init_tables(bios);
6901
6902 /*
6903 * Runs some additional script seen on G8x VBIOSen. The VBIOS'
6904 * parser will run this right after the init tables, the binary
6905 * driver appears to run it at some point later.
6906 */
6907 if (bios->some_script_ptr) {
6908 struct init_exec iexec = {true, false};
6909
6910 NV_INFO(dev, "Parsing VBIOS init table at offset 0x%04X\n",
6911 bios->some_script_ptr);
6912 parse_init_table(bios, bios->some_script_ptr, &iexec);
6913 }
6914
6915 if (dev_priv->card_type >= NV_50) {
Ben Skeggs7f245b22010-02-24 09:56:18 +10006916 for (i = 0; i < bios->dcb.entries; i++) {
Ben Skeggs02e4f582011-07-06 21:21:42 +10006917 nouveau_bios_run_display_table(dev, 0, 0,
6918 &bios->dcb.entry[i], -1);
Ben Skeggs6ee73862009-12-11 19:24:15 +10006919 }
6920 }
6921
Ben Skeggs6ee73862009-12-11 19:24:15 +10006922 return ret;
6923}
6924
6925static void
6926nouveau_bios_i2c_devices_takedown(struct drm_device *dev)
6927{
6928 struct drm_nouveau_private *dev_priv = dev->dev_private;
Ben Skeggs04a39c52010-02-24 10:03:05 +10006929 struct nvbios *bios = &dev_priv->vbios;
Ben Skeggs6ee73862009-12-11 19:24:15 +10006930 struct dcb_i2c_entry *entry;
6931 int i;
6932
Ben Skeggs7f245b22010-02-24 09:56:18 +10006933 entry = &bios->dcb.i2c[0];
Ben Skeggs6ee73862009-12-11 19:24:15 +10006934 for (i = 0; i < DCB_MAX_NUM_I2C_ENTRIES; i++, entry++)
6935 nouveau_i2c_fini(dev, entry);
6936}
6937
Ben Skeggsd13102c2010-05-25 13:47:16 +10006938static bool
6939nouveau_bios_posted(struct drm_device *dev)
6940{
6941 struct drm_nouveau_private *dev_priv = dev->dev_private;
Ben Skeggsd13102c2010-05-25 13:47:16 +10006942 unsigned htotal;
6943
Francisco Jerezc1b60ec2010-11-11 00:56:37 +01006944 if (dev_priv->card_type >= NV_50) {
Ben Skeggsd13102c2010-05-25 13:47:16 +10006945 if (NVReadVgaCrtc(dev, 0, 0x00) == 0 &&
6946 NVReadVgaCrtc(dev, 0, 0x1a) == 0)
6947 return false;
6948 return true;
6949 }
6950
Ben Skeggsd13102c2010-05-25 13:47:16 +10006951 htotal = NVReadVgaCrtc(dev, 0, 0x06);
6952 htotal |= (NVReadVgaCrtc(dev, 0, 0x07) & 0x01) << 8;
6953 htotal |= (NVReadVgaCrtc(dev, 0, 0x07) & 0x20) << 4;
6954 htotal |= (NVReadVgaCrtc(dev, 0, 0x25) & 0x01) << 10;
6955 htotal |= (NVReadVgaCrtc(dev, 0, 0x41) & 0x01) << 11;
Francisco Jerez03cd06c2010-07-20 03:08:25 +02006956
Ben Skeggsd13102c2010-05-25 13:47:16 +10006957 return (htotal != 0);
6958}
6959
Ben Skeggs6ee73862009-12-11 19:24:15 +10006960int
6961nouveau_bios_init(struct drm_device *dev)
6962{
6963 struct drm_nouveau_private *dev_priv = dev->dev_private;
Ben Skeggs04a39c52010-02-24 10:03:05 +10006964 struct nvbios *bios = &dev_priv->vbios;
Ben Skeggs6ee73862009-12-11 19:24:15 +10006965 int ret;
6966
Ben Skeggs6ee73862009-12-11 19:24:15 +10006967 if (!NVInitVBIOS(dev))
6968 return -ENODEV;
6969
6970 ret = nouveau_parse_vbios_struct(dev);
6971 if (ret)
6972 return ret;
6973
Francisco Jerez2e5702a2010-11-19 18:08:47 +10006974 ret = parse_dcb_table(dev, bios);
Ben Skeggs6ee73862009-12-11 19:24:15 +10006975 if (ret)
6976 return ret;
6977
6978 fixup_legacy_i2c(bios);
6979 fixup_legacy_connector(bios);
6980
6981 if (!bios->major_version) /* we don't run version 0 bios */
6982 return 0;
6983
Ben Skeggs6ee73862009-12-11 19:24:15 +10006984 /* init script execution disabled */
6985 bios->execute = false;
6986
6987 /* ... unless card isn't POSTed already */
Ben Skeggsd13102c2010-05-25 13:47:16 +10006988 if (!nouveau_bios_posted(dev)) {
Francisco Jerez67eda202010-07-13 15:59:50 +02006989 NV_INFO(dev, "Adaptor not initialised, "
6990 "running VBIOS init tables.\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +10006991 bios->execute = true;
6992 }
Marcin Koƛcielnicki0cba1b72010-09-29 11:15:01 +00006993 if (nouveau_force_post)
6994 bios->execute = true;
Ben Skeggs6ee73862009-12-11 19:24:15 +10006995
Ben Skeggs6ee73862009-12-11 19:24:15 +10006996 ret = nouveau_run_vbios_init(dev);
Ben Skeggs04a39c52010-02-24 10:03:05 +10006997 if (ret)
Ben Skeggs6ee73862009-12-11 19:24:15 +10006998 return ret;
Ben Skeggs6ee73862009-12-11 19:24:15 +10006999
7000 /* feature_byte on BMP is poor, but init always sets CR4B */
Ben Skeggs6ee73862009-12-11 19:24:15 +10007001 if (bios->major_version < 5)
7002 bios->is_mobile = NVReadVgaCrtc(dev, 0, NV_CIO_CRE_4B) & 0x40;
7003
7004 /* all BIT systems need p_f_m_t for digital_min_front_porch */
7005 if (bios->is_mobile || bios->major_version >= 5)
7006 ret = parse_fp_mode_table(dev, bios);
Ben Skeggs6ee73862009-12-11 19:24:15 +10007007
7008 /* allow subsequent scripts to execute */
7009 bios->execute = true;
7010
7011 return 0;
7012}
7013
7014void
7015nouveau_bios_takedown(struct drm_device *dev)
7016{
7017 nouveau_bios_i2c_devices_takedown(dev);
7018}