blob: 7922bb969d25d96a02b8e8ebdaa654912be9ab19 [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 */
Ben Skeggs6ee73862009-12-11 19:24:15 +100037
38#define EDID1_LEN 128
39
40#define BIOSLOG(sip, fmt, arg...) NV_DEBUG(sip->dev, fmt, ##arg)
41#define LOG_OLD_VALUE(x)
42
Ben Skeggs6ee73862009-12-11 19:24:15 +100043struct init_exec {
44 bool execute;
45 bool repeat;
46};
47
48static bool nv_cksum(const uint8_t *data, unsigned int length)
49{
50 /*
51 * There's a few checksums in the BIOS, so here's a generic checking
52 * function.
53 */
54 int i;
55 uint8_t sum = 0;
56
57 for (i = 0; i < length; i++)
58 sum += data[i];
59
60 if (sum)
61 return true;
62
63 return false;
64}
65
66static int
67score_vbios(struct drm_device *dev, const uint8_t *data, const bool writeable)
68{
69 if (!(data[0] == 0x55 && data[1] == 0xAA)) {
70 NV_TRACEWARN(dev, "... BIOS signature not found\n");
71 return 0;
72 }
73
74 if (nv_cksum(data, data[2] * 512)) {
75 NV_TRACEWARN(dev, "... BIOS checksum invalid\n");
76 /* if a ro image is somewhat bad, it's probably all rubbish */
77 return writeable ? 2 : 1;
78 } else
79 NV_TRACE(dev, "... appears to be valid\n");
80
81 return 3;
82}
83
84static void load_vbios_prom(struct drm_device *dev, uint8_t *data)
85{
86 struct drm_nouveau_private *dev_priv = dev->dev_private;
87 uint32_t pci_nv_20, save_pci_nv_20;
88 int pcir_ptr;
89 int i;
90
91 if (dev_priv->card_type >= NV_50)
92 pci_nv_20 = 0x88050;
93 else
94 pci_nv_20 = NV_PBUS_PCI_NV_20;
95
96 /* enable ROM access */
97 save_pci_nv_20 = nvReadMC(dev, pci_nv_20);
98 nvWriteMC(dev, pci_nv_20,
99 save_pci_nv_20 & ~NV_PBUS_PCI_NV_20_ROM_SHADOW_ENABLED);
100
101 /* bail if no rom signature */
102 if (nv_rd08(dev, NV_PROM_OFFSET) != 0x55 ||
103 nv_rd08(dev, NV_PROM_OFFSET + 1) != 0xaa)
104 goto out;
105
106 /* additional check (see note below) - read PCI record header */
107 pcir_ptr = nv_rd08(dev, NV_PROM_OFFSET + 0x18) |
108 nv_rd08(dev, NV_PROM_OFFSET + 0x19) << 8;
109 if (nv_rd08(dev, NV_PROM_OFFSET + pcir_ptr) != 'P' ||
110 nv_rd08(dev, NV_PROM_OFFSET + pcir_ptr + 1) != 'C' ||
111 nv_rd08(dev, NV_PROM_OFFSET + pcir_ptr + 2) != 'I' ||
112 nv_rd08(dev, NV_PROM_OFFSET + pcir_ptr + 3) != 'R')
113 goto out;
114
115 /* on some 6600GT/6800LE prom reads are messed up. nvclock alleges a
116 * a good read may be obtained by waiting or re-reading (cargocult: 5x)
117 * each byte. we'll hope pramin has something usable instead
118 */
119 for (i = 0; i < NV_PROM_SIZE; i++)
120 data[i] = nv_rd08(dev, NV_PROM_OFFSET + i);
121
122out:
123 /* disable ROM access */
124 nvWriteMC(dev, pci_nv_20,
125 save_pci_nv_20 | NV_PBUS_PCI_NV_20_ROM_SHADOW_ENABLED);
126}
127
128static void load_vbios_pramin(struct drm_device *dev, uint8_t *data)
129{
130 struct drm_nouveau_private *dev_priv = dev->dev_private;
131 uint32_t old_bar0_pramin = 0;
132 int i;
133
134 if (dev_priv->card_type >= NV_50) {
Ben Skeggs96177572011-06-24 08:18:23 +1000135 u64 addr = (u64)(nv_rd32(dev, 0x619f04) & 0xffffff00) << 8;
136 if (!addr) {
137 addr = (u64)nv_rd32(dev, 0x1700) << 16;
138 addr += 0xf0000;
139 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000140
141 old_bar0_pramin = nv_rd32(dev, 0x1700);
Ben Skeggs96177572011-06-24 08:18:23 +1000142 nv_wr32(dev, 0x1700, addr >> 16);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000143 }
144
145 /* bail if no rom signature */
146 if (nv_rd08(dev, NV_PRAMIN_OFFSET) != 0x55 ||
147 nv_rd08(dev, NV_PRAMIN_OFFSET + 1) != 0xaa)
148 goto out;
149
150 for (i = 0; i < NV_PROM_SIZE; i++)
151 data[i] = nv_rd08(dev, NV_PRAMIN_OFFSET + i);
152
153out:
154 if (dev_priv->card_type >= NV_50)
155 nv_wr32(dev, 0x1700, old_bar0_pramin);
156}
157
158static void load_vbios_pci(struct drm_device *dev, uint8_t *data)
159{
160 void __iomem *rom = NULL;
161 size_t rom_len;
162 int ret;
163
164 ret = pci_enable_rom(dev->pdev);
165 if (ret)
166 return;
167
168 rom = pci_map_rom(dev->pdev, &rom_len);
169 if (!rom)
170 goto out;
171 memcpy_fromio(data, rom, rom_len);
172 pci_unmap_rom(dev->pdev, rom);
173
174out:
175 pci_disable_rom(dev->pdev);
176}
177
Dave Airlieafeb3e12010-04-07 13:55:09 +1000178static void load_vbios_acpi(struct drm_device *dev, uint8_t *data)
179{
180 int i;
181 int ret;
182 int size = 64 * 1024;
183
184 if (!nouveau_acpi_rom_supported(dev->pdev))
185 return;
186
187 for (i = 0; i < (size / ROM_BIOS_PAGE); i++) {
188 ret = nouveau_acpi_get_bios_chunk(data,
189 (i * ROM_BIOS_PAGE),
190 ROM_BIOS_PAGE);
191 if (ret <= 0)
192 break;
193 }
194 return;
195}
196
Ben Skeggs6ee73862009-12-11 19:24:15 +1000197struct methods {
198 const char desc[8];
199 void (*loadbios)(struct drm_device *, uint8_t *);
200 const bool rw;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000201};
202
Ben Skeggs41090eb2010-07-12 13:15:44 +1000203static struct methods shadow_methods[] = {
Ben Skeggs6ee73862009-12-11 19:24:15 +1000204 { "PRAMIN", load_vbios_pramin, true },
Ben Skeggs41090eb2010-07-12 13:15:44 +1000205 { "PROM", load_vbios_prom, false },
Ben Skeggs6ee73862009-12-11 19:24:15 +1000206 { "PCIROM", load_vbios_pci, true },
Dave Airlieafeb3e12010-04-07 13:55:09 +1000207 { "ACPI", load_vbios_acpi, true },
Ben Skeggs6ee73862009-12-11 19:24:15 +1000208};
Francisco Jerezeae61922010-07-13 16:16:26 +0200209#define NUM_SHADOW_METHODS ARRAY_SIZE(shadow_methods)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000210
211static bool NVShadowVBIOS(struct drm_device *dev, uint8_t *data)
212{
Ben Skeggs41090eb2010-07-12 13:15:44 +1000213 struct methods *methods = shadow_methods;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000214 int testscore = 3;
Francisco Jerezeae61922010-07-13 16:16:26 +0200215 int scores[NUM_SHADOW_METHODS], i;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000216
217 if (nouveau_vbios) {
Francisco Jerezeae61922010-07-13 16:16:26 +0200218 for (i = 0; i < NUM_SHADOW_METHODS; i++)
Marcin Koƛcielnicki657b6242009-12-15 00:37:30 +0000219 if (!strcasecmp(nouveau_vbios, methods[i].desc))
Ben Skeggs6ee73862009-12-11 19:24:15 +1000220 break;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000221
Francisco Jerezeae61922010-07-13 16:16:26 +0200222 if (i < NUM_SHADOW_METHODS) {
Ben Skeggs6ee73862009-12-11 19:24:15 +1000223 NV_INFO(dev, "Attempting to use BIOS image from %s\n",
Marcin Koƛcielnicki657b6242009-12-15 00:37:30 +0000224 methods[i].desc);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000225
Marcin Koƛcielnicki657b6242009-12-15 00:37:30 +0000226 methods[i].loadbios(dev, data);
227 if (score_vbios(dev, data, methods[i].rw))
Ben Skeggs6ee73862009-12-11 19:24:15 +1000228 return true;
229 }
230
231 NV_ERROR(dev, "VBIOS source \'%s\' invalid\n", nouveau_vbios);
232 }
233
Francisco Jerezeae61922010-07-13 16:16:26 +0200234 for (i = 0; i < NUM_SHADOW_METHODS; i++) {
Ben Skeggs6ee73862009-12-11 19:24:15 +1000235 NV_TRACE(dev, "Attempting to load BIOS image from %s\n",
Marcin Koƛcielnicki657b6242009-12-15 00:37:30 +0000236 methods[i].desc);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000237 data[0] = data[1] = 0; /* avoid reuse of previous image */
Marcin Koƛcielnicki657b6242009-12-15 00:37:30 +0000238 methods[i].loadbios(dev, data);
239 scores[i] = score_vbios(dev, data, methods[i].rw);
240 if (scores[i] == testscore)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000241 return true;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000242 }
243
244 while (--testscore > 0) {
Francisco Jerezeae61922010-07-13 16:16:26 +0200245 for (i = 0; i < NUM_SHADOW_METHODS; i++) {
Marcin Koƛcielnicki657b6242009-12-15 00:37:30 +0000246 if (scores[i] == testscore) {
Ben Skeggs6ee73862009-12-11 19:24:15 +1000247 NV_TRACE(dev, "Using BIOS image from %s\n",
Marcin Koƛcielnicki657b6242009-12-15 00:37:30 +0000248 methods[i].desc);
249 methods[i].loadbios(dev, data);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000250 return true;
251 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000252 }
253 }
254
255 NV_ERROR(dev, "No valid BIOS image found\n");
256 return false;
257}
258
259struct init_tbl_entry {
260 char *name;
261 uint8_t id;
Ben Skeggs9170a822010-05-10 16:54:23 +1000262 /* Return:
263 * > 0: success, length of opcode
264 * 0: success, but abort further parsing of table (INIT_DONE etc)
265 * < 0: failure, table parsing will be aborted
266 */
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +0000267 int (*handler)(struct nvbios *, uint16_t, struct init_exec *);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000268};
269
Ben Skeggsec64a402011-03-21 21:31:21 +1000270static int parse_init_table(struct nvbios *, uint16_t, struct init_exec *);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000271
272#define MACRO_INDEX_SIZE 2
273#define MACRO_SIZE 8
274#define CONDITION_SIZE 12
275#define IO_FLAG_CONDITION_SIZE 9
276#define IO_CONDITION_SIZE 5
277#define MEM_INIT_SIZE 66
278
279static void still_alive(void)
280{
281#if 0
282 sync();
Ben Skeggsc7ca4d12011-02-03 20:10:49 +1000283 mdelay(2);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000284#endif
285}
286
287static uint32_t
288munge_reg(struct nvbios *bios, uint32_t reg)
289{
290 struct drm_nouveau_private *dev_priv = bios->dev->dev_private;
291 struct dcb_entry *dcbent = bios->display.output;
292
293 if (dev_priv->card_type < NV_50)
294 return reg;
295
Ben Skeggs02e4f582011-07-06 21:21:42 +1000296 if (reg & 0x80000000) {
297 BUG_ON(bios->display.crtc < 0);
298 reg += bios->display.crtc * 0x800;
299 }
300
Ben Skeggs6ee73862009-12-11 19:24:15 +1000301 if (reg & 0x40000000) {
302 BUG_ON(!dcbent);
303
304 reg += (ffs(dcbent->or) - 1) * 0x800;
305 if ((reg & 0x20000000) && !(dcbent->sorconf.link & 1))
306 reg += 0x00000080;
307 }
308
Ben Skeggs02e4f582011-07-06 21:21:42 +1000309 reg &= ~0xe0000000;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000310 return reg;
311}
312
313static int
314valid_reg(struct nvbios *bios, uint32_t reg)
315{
316 struct drm_nouveau_private *dev_priv = bios->dev->dev_private;
317 struct drm_device *dev = bios->dev;
318
319 /* C51 has misaligned regs on purpose. Marvellous */
Ben Skeggs9855e582010-01-12 13:02:19 +1000320 if (reg & 0x2 ||
Ben Skeggs04a39c52010-02-24 10:03:05 +1000321 (reg & 0x1 && dev_priv->vbios.chip_version != 0x51))
Ben Skeggs9855e582010-01-12 13:02:19 +1000322 NV_ERROR(dev, "======= misaligned reg 0x%08X =======\n", reg);
323
324 /* warn on C51 regs that haven't been verified accessible in tracing */
Ben Skeggs04a39c52010-02-24 10:03:05 +1000325 if (reg & 0x1 && dev_priv->vbios.chip_version == 0x51 &&
Ben Skeggs6ee73862009-12-11 19:24:15 +1000326 reg != 0x130d && reg != 0x1311 && reg != 0x60081d)
327 NV_WARN(dev, "=== C51 misaligned reg 0x%08X not verified ===\n",
328 reg);
329
Ben Skeggs9855e582010-01-12 13:02:19 +1000330 if (reg >= (8*1024*1024)) {
331 NV_ERROR(dev, "=== reg 0x%08x out of mapped bounds ===\n", reg);
332 return 0;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000333 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000334
Ben Skeggs9855e582010-01-12 13:02:19 +1000335 return 1;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000336}
337
338static bool
339valid_idx_port(struct nvbios *bios, uint16_t port)
340{
341 struct drm_nouveau_private *dev_priv = bios->dev->dev_private;
342 struct drm_device *dev = bios->dev;
343
344 /*
345 * If adding more ports here, the read/write functions below will need
346 * updating so that the correct mmio range (PRMCIO, PRMDIO, PRMVIO) is
347 * used for the port in question
348 */
349 if (dev_priv->card_type < NV_50) {
350 if (port == NV_CIO_CRX__COLOR)
351 return true;
352 if (port == NV_VIO_SRX)
353 return true;
354 } else {
355 if (port == NV_CIO_CRX__COLOR)
356 return true;
357 }
358
359 NV_ERROR(dev, "========== unknown indexed io port 0x%04X ==========\n",
360 port);
361
362 return false;
363}
364
365static bool
366valid_port(struct nvbios *bios, uint16_t port)
367{
368 struct drm_device *dev = bios->dev;
369
370 /*
371 * If adding more ports here, the read/write functions below will need
372 * updating so that the correct mmio range (PRMCIO, PRMDIO, PRMVIO) is
373 * used for the port in question
374 */
375 if (port == NV_VIO_VSE2)
376 return true;
377
378 NV_ERROR(dev, "========== unknown io port 0x%04X ==========\n", port);
379
380 return false;
381}
382
383static uint32_t
384bios_rd32(struct nvbios *bios, uint32_t reg)
385{
386 uint32_t data;
387
388 reg = munge_reg(bios, reg);
389 if (!valid_reg(bios, reg))
390 return 0;
391
392 /*
393 * C51 sometimes uses regs with bit0 set in the address. For these
394 * cases there should exist a translation in a BIOS table to an IO
395 * port address which the BIOS uses for accessing the reg
396 *
397 * These only seem to appear for the power control regs to a flat panel,
398 * and the GPIO regs at 0x60081*. In C51 mmio traces the normal regs
399 * for 0x1308 and 0x1310 are used - hence the mask below. An S3
400 * suspend-resume mmio trace from a C51 will be required to see if this
401 * is true for the power microcode in 0x14.., or whether the direct IO
402 * port access method is needed
403 */
404 if (reg & 0x1)
405 reg &= ~0x1;
406
407 data = nv_rd32(bios->dev, reg);
408
409 BIOSLOG(bios, " Read: Reg: 0x%08X, Data: 0x%08X\n", reg, data);
410
411 return data;
412}
413
414static void
415bios_wr32(struct nvbios *bios, uint32_t reg, uint32_t data)
416{
417 struct drm_nouveau_private *dev_priv = bios->dev->dev_private;
418
419 reg = munge_reg(bios, reg);
420 if (!valid_reg(bios, reg))
421 return;
422
423 /* see note in bios_rd32 */
424 if (reg & 0x1)
425 reg &= 0xfffffffe;
426
427 LOG_OLD_VALUE(bios_rd32(bios, reg));
428 BIOSLOG(bios, " Write: Reg: 0x%08X, Data: 0x%08X\n", reg, data);
429
Ben Skeggs04a39c52010-02-24 10:03:05 +1000430 if (dev_priv->vbios.execute) {
Ben Skeggs6ee73862009-12-11 19:24:15 +1000431 still_alive();
432 nv_wr32(bios->dev, reg, data);
433 }
434}
435
436static uint8_t
437bios_idxprt_rd(struct nvbios *bios, uint16_t port, uint8_t index)
438{
439 struct drm_nouveau_private *dev_priv = bios->dev->dev_private;
440 struct drm_device *dev = bios->dev;
441 uint8_t data;
442
443 if (!valid_idx_port(bios, port))
444 return 0;
445
446 if (dev_priv->card_type < NV_50) {
447 if (port == NV_VIO_SRX)
448 data = NVReadVgaSeq(dev, bios->state.crtchead, index);
449 else /* assume NV_CIO_CRX__COLOR */
450 data = NVReadVgaCrtc(dev, bios->state.crtchead, index);
451 } else {
452 uint32_t data32;
453
454 data32 = bios_rd32(bios, NV50_PDISPLAY_VGACRTC(index & ~3));
455 data = (data32 >> ((index & 3) << 3)) & 0xff;
456 }
457
458 BIOSLOG(bios, " Indexed IO read: Port: 0x%04X, Index: 0x%02X, "
459 "Head: 0x%02X, Data: 0x%02X\n",
460 port, index, bios->state.crtchead, data);
461 return data;
462}
463
464static void
465bios_idxprt_wr(struct nvbios *bios, uint16_t port, uint8_t index, uint8_t data)
466{
467 struct drm_nouveau_private *dev_priv = bios->dev->dev_private;
468 struct drm_device *dev = bios->dev;
469
470 if (!valid_idx_port(bios, port))
471 return;
472
473 /*
474 * The current head is maintained in the nvbios member state.crtchead.
475 * We trap changes to CR44 and update the head variable and hence the
476 * register set written.
477 * As CR44 only exists on CRTC0, we update crtchead to head0 in advance
478 * of the write, and to head1 after the write
479 */
480 if (port == NV_CIO_CRX__COLOR && index == NV_CIO_CRE_44 &&
481 data != NV_CIO_CRE_44_HEADB)
482 bios->state.crtchead = 0;
483
484 LOG_OLD_VALUE(bios_idxprt_rd(bios, port, index));
485 BIOSLOG(bios, " Indexed IO write: Port: 0x%04X, Index: 0x%02X, "
486 "Head: 0x%02X, Data: 0x%02X\n",
487 port, index, bios->state.crtchead, data);
488
489 if (bios->execute && dev_priv->card_type < NV_50) {
490 still_alive();
491 if (port == NV_VIO_SRX)
492 NVWriteVgaSeq(dev, bios->state.crtchead, index, data);
493 else /* assume NV_CIO_CRX__COLOR */
494 NVWriteVgaCrtc(dev, bios->state.crtchead, index, data);
495 } else
496 if (bios->execute) {
497 uint32_t data32, shift = (index & 3) << 3;
498
499 still_alive();
500
501 data32 = bios_rd32(bios, NV50_PDISPLAY_VGACRTC(index & ~3));
502 data32 &= ~(0xff << shift);
503 data32 |= (data << shift);
504 bios_wr32(bios, NV50_PDISPLAY_VGACRTC(index & ~3), data32);
505 }
506
507 if (port == NV_CIO_CRX__COLOR &&
508 index == NV_CIO_CRE_44 && data == NV_CIO_CRE_44_HEADB)
509 bios->state.crtchead = 1;
510}
511
512static uint8_t
513bios_port_rd(struct nvbios *bios, uint16_t port)
514{
515 uint8_t data, head = bios->state.crtchead;
516
517 if (!valid_port(bios, port))
518 return 0;
519
520 data = NVReadPRMVIO(bios->dev, head, NV_PRMVIO0_OFFSET + port);
521
522 BIOSLOG(bios, " IO read: Port: 0x%04X, Head: 0x%02X, Data: 0x%02X\n",
523 port, head, data);
524
525 return data;
526}
527
528static void
529bios_port_wr(struct nvbios *bios, uint16_t port, uint8_t data)
530{
531 int head = bios->state.crtchead;
532
533 if (!valid_port(bios, port))
534 return;
535
536 LOG_OLD_VALUE(bios_port_rd(bios, port));
537 BIOSLOG(bios, " IO write: Port: 0x%04X, Head: 0x%02X, Data: 0x%02X\n",
538 port, head, data);
539
540 if (!bios->execute)
541 return;
542
543 still_alive();
544 NVWritePRMVIO(bios->dev, head, NV_PRMVIO0_OFFSET + port, data);
545}
546
547static bool
548io_flag_condition_met(struct nvbios *bios, uint16_t offset, uint8_t cond)
549{
550 /*
551 * The IO flag condition entry has 2 bytes for the CRTC port; 1 byte
552 * for the CRTC index; 1 byte for the mask to apply to the value
553 * retrieved from the CRTC; 1 byte for the shift right to apply to the
554 * masked CRTC value; 2 bytes for the offset to the flag array, to
555 * which the shifted value is added; 1 byte for the mask applied to the
556 * value read from the flag array; and 1 byte for the value to compare
557 * against the masked byte from the flag table.
558 */
559
560 uint16_t condptr = bios->io_flag_condition_tbl_ptr + cond * IO_FLAG_CONDITION_SIZE;
561 uint16_t crtcport = ROM16(bios->data[condptr]);
562 uint8_t crtcindex = bios->data[condptr + 2];
563 uint8_t mask = bios->data[condptr + 3];
564 uint8_t shift = bios->data[condptr + 4];
565 uint16_t flagarray = ROM16(bios->data[condptr + 5]);
566 uint8_t flagarraymask = bios->data[condptr + 7];
567 uint8_t cmpval = bios->data[condptr + 8];
568 uint8_t data;
569
570 BIOSLOG(bios, "0x%04X: Port: 0x%04X, Index: 0x%02X, Mask: 0x%02X, "
571 "Shift: 0x%02X, FlagArray: 0x%04X, FAMask: 0x%02X, "
572 "Cmpval: 0x%02X\n",
573 offset, crtcport, crtcindex, mask, shift, flagarray, flagarraymask, cmpval);
574
575 data = bios_idxprt_rd(bios, crtcport, crtcindex);
576
577 data = bios->data[flagarray + ((data & mask) >> shift)];
578 data &= flagarraymask;
579
580 BIOSLOG(bios, "0x%04X: Checking if 0x%02X equals 0x%02X\n",
581 offset, data, cmpval);
582
583 return (data == cmpval);
584}
585
586static bool
587bios_condition_met(struct nvbios *bios, uint16_t offset, uint8_t cond)
588{
589 /*
590 * The condition table entry has 4 bytes for the address of the
591 * register to check, 4 bytes for a mask to apply to the register and
592 * 4 for a test comparison value
593 */
594
595 uint16_t condptr = bios->condition_tbl_ptr + cond * CONDITION_SIZE;
596 uint32_t reg = ROM32(bios->data[condptr]);
597 uint32_t mask = ROM32(bios->data[condptr + 4]);
598 uint32_t cmpval = ROM32(bios->data[condptr + 8]);
599 uint32_t data;
600
601 BIOSLOG(bios, "0x%04X: Cond: 0x%02X, Reg: 0x%08X, Mask: 0x%08X\n",
602 offset, cond, reg, mask);
603
604 data = bios_rd32(bios, reg) & mask;
605
606 BIOSLOG(bios, "0x%04X: Checking if 0x%08X equals 0x%08X\n",
607 offset, data, cmpval);
608
609 return (data == cmpval);
610}
611
612static bool
613io_condition_met(struct nvbios *bios, uint16_t offset, uint8_t cond)
614{
615 /*
616 * The IO condition entry has 2 bytes for the IO port address; 1 byte
617 * for the index to write to io_port; 1 byte for the mask to apply to
618 * the byte read from io_port+1; and 1 byte for the value to compare
619 * against the masked byte.
620 */
621
622 uint16_t condptr = bios->io_condition_tbl_ptr + cond * IO_CONDITION_SIZE;
623 uint16_t io_port = ROM16(bios->data[condptr]);
624 uint8_t port_index = bios->data[condptr + 2];
625 uint8_t mask = bios->data[condptr + 3];
626 uint8_t cmpval = bios->data[condptr + 4];
627
628 uint8_t data = bios_idxprt_rd(bios, io_port, port_index) & mask;
629
630 BIOSLOG(bios, "0x%04X: Checking if 0x%02X equals 0x%02X\n",
631 offset, data, cmpval);
632
633 return (data == cmpval);
634}
635
636static int
637nv50_pll_set(struct drm_device *dev, uint32_t reg, uint32_t clk)
638{
639 struct drm_nouveau_private *dev_priv = dev->dev_private;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000640 struct nouveau_pll_vals pll;
641 struct pll_lims pll_limits;
Ben Skeggsee9f7ef2011-09-23 15:37:38 +1000642 u32 ctrl, mask, coef;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000643 int ret;
644
645 ret = get_pll_limits(dev, reg, &pll_limits);
646 if (ret)
647 return ret;
648
649 clk = nouveau_calc_pll_mnp(dev, &pll_limits, clk, &pll);
650 if (!clk)
651 return -ERANGE;
652
Ben Skeggsee9f7ef2011-09-23 15:37:38 +1000653 coef = pll.N1 << 8 | pll.M1;
654 ctrl = pll.log2P << 16;
655 mask = 0x00070000;
656 if (reg == 0x004008) {
657 mask |= 0x01f80000;
658 ctrl |= (pll_limits.log2p_bias << 19);
659 ctrl |= (pll.log2P << 22);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000660 }
661
Ben Skeggsee9f7ef2011-09-23 15:37:38 +1000662 if (!dev_priv->vbios.execute)
663 return 0;
664
665 nv_mask(dev, reg + 0, mask, ctrl);
666 nv_wr32(dev, reg + 4, coef);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000667 return 0;
668}
669
670static int
671setPLL(struct nvbios *bios, uint32_t reg, uint32_t clk)
672{
673 struct drm_device *dev = bios->dev;
674 struct drm_nouveau_private *dev_priv = dev->dev_private;
675 /* clk in kHz */
676 struct pll_lims pll_lim;
677 struct nouveau_pll_vals pllvals;
678 int ret;
679
680 if (dev_priv->card_type >= NV_50)
681 return nv50_pll_set(dev, reg, clk);
682
683 /* high regs (such as in the mac g5 table) are not -= 4 */
684 ret = get_pll_limits(dev, reg > 0x405c ? reg : reg - 4, &pll_lim);
685 if (ret)
686 return ret;
687
688 clk = nouveau_calc_pll_mnp(dev, &pll_lim, clk, &pllvals);
689 if (!clk)
690 return -ERANGE;
691
692 if (bios->execute) {
693 still_alive();
694 nouveau_hw_setpll(dev, reg, &pllvals);
695 }
696
697 return 0;
698}
699
700static int dcb_entry_idx_from_crtchead(struct drm_device *dev)
701{
702 struct drm_nouveau_private *dev_priv = dev->dev_private;
Ben Skeggs04a39c52010-02-24 10:03:05 +1000703 struct nvbios *bios = &dev_priv->vbios;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000704
705 /*
706 * For the results of this function to be correct, CR44 must have been
707 * set (using bios_idxprt_wr to set crtchead), CR58 set for CR57 = 0,
708 * and the DCB table parsed, before the script calling the function is
709 * run. run_digital_op_script is example of how to do such setup
710 */
711
712 uint8_t dcb_entry = NVReadVgaCrtc5758(dev, bios->state.crtchead, 0);
713
Ben Skeggs7f245b22010-02-24 09:56:18 +1000714 if (dcb_entry > bios->dcb.entries) {
Ben Skeggs6ee73862009-12-11 19:24:15 +1000715 NV_ERROR(dev, "CR58 doesn't have a valid DCB entry currently "
716 "(%02X)\n", dcb_entry);
717 dcb_entry = 0x7f; /* unused / invalid marker */
718 }
719
720 return dcb_entry;
721}
722
723static struct nouveau_i2c_chan *
724init_i2c_device_find(struct drm_device *dev, int i2c_index)
725{
Ben Skeggs6ee73862009-12-11 19:24:15 +1000726 if (i2c_index == 0xff) {
Ben Skeggs486a45c2011-11-11 10:22:19 +1000727 struct drm_nouveau_private *dev_priv = dev->dev_private;
728 struct dcb_table *dcb = &dev_priv->vbios.dcb;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000729 /* note: dcb_entry_idx_from_crtchead needs pre-script set-up */
Ben Skeggs486a45c2011-11-11 10:22:19 +1000730 int idx = dcb_entry_idx_from_crtchead(dev);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000731
Ben Skeggs486a45c2011-11-11 10:22:19 +1000732 i2c_index = NV_I2C_DEFAULT(0);
Ben Skeggs7f245b22010-02-24 09:56:18 +1000733 if (idx != 0x7f && dcb->entry[idx].i2c_upper_default)
Ben Skeggs486a45c2011-11-11 10:22:19 +1000734 i2c_index = NV_I2C_DEFAULT(1);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000735 }
Ben Skeggsf8b0be12010-05-12 14:38:25 +1000736
Ben Skeggs6ee73862009-12-11 19:24:15 +1000737 return nouveau_i2c_find(dev, i2c_index);
738}
739
Ben Skeggs7f245b22010-02-24 09:56:18 +1000740static uint32_t
741get_tmds_index_reg(struct drm_device *dev, uint8_t mlv)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000742{
743 /*
744 * For mlv < 0x80, it is an index into a table of TMDS base addresses.
745 * For mlv == 0x80 use the "or" value of the dcb_entry indexed by
746 * CR58 for CR57 = 0 to index a table of offsets to the basic
747 * 0x6808b0 address.
748 * For mlv == 0x81 use the "or" value of the dcb_entry indexed by
749 * CR58 for CR57 = 0 to index a table of offsets to the basic
750 * 0x6808b0 address, and then flip the offset by 8.
751 */
752
753 struct drm_nouveau_private *dev_priv = dev->dev_private;
Ben Skeggs04a39c52010-02-24 10:03:05 +1000754 struct nvbios *bios = &dev_priv->vbios;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000755 const int pramdac_offset[13] = {
756 0, 0, 0x8, 0, 0x2000, 0, 0, 0, 0x2008, 0, 0, 0, 0x2000 };
757 const uint32_t pramdac_table[4] = {
758 0x6808b0, 0x6808b8, 0x6828b0, 0x6828b8 };
759
760 if (mlv >= 0x80) {
761 int dcb_entry, dacoffset;
762
763 /* note: dcb_entry_idx_from_crtchead needs pre-script set-up */
764 dcb_entry = dcb_entry_idx_from_crtchead(dev);
765 if (dcb_entry == 0x7f)
766 return 0;
Ben Skeggs7f245b22010-02-24 09:56:18 +1000767 dacoffset = pramdac_offset[bios->dcb.entry[dcb_entry].or];
Ben Skeggs6ee73862009-12-11 19:24:15 +1000768 if (mlv == 0x81)
769 dacoffset ^= 8;
770 return 0x6808b0 + dacoffset;
771 } else {
Marcin Slusarzdf31ef42010-02-17 19:04:00 +0100772 if (mlv >= ARRAY_SIZE(pramdac_table)) {
Ben Skeggs6ee73862009-12-11 19:24:15 +1000773 NV_ERROR(dev, "Magic Lookup Value too big (%02X)\n",
774 mlv);
775 return 0;
776 }
777 return pramdac_table[mlv];
778 }
779}
780
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +0000781static int
Ben Skeggs6ee73862009-12-11 19:24:15 +1000782init_io_restrict_prog(struct nvbios *bios, uint16_t offset,
783 struct init_exec *iexec)
784{
785 /*
786 * INIT_IO_RESTRICT_PROG opcode: 0x32 ('2')
787 *
788 * offset (8 bit): opcode
789 * offset + 1 (16 bit): CRTC port
790 * offset + 3 (8 bit): CRTC index
791 * offset + 4 (8 bit): mask
792 * offset + 5 (8 bit): shift
793 * offset + 6 (8 bit): count
794 * offset + 7 (32 bit): register
795 * offset + 11 (32 bit): configuration 1
796 * ...
797 *
798 * Starting at offset + 11 there are "count" 32 bit values.
799 * To find out which value to use read index "CRTC index" on "CRTC
800 * port", AND this value with "mask" and then bit shift right "shift"
801 * bits. Read the appropriate value using this index and write to
802 * "register"
803 */
804
805 uint16_t crtcport = ROM16(bios->data[offset + 1]);
806 uint8_t crtcindex = bios->data[offset + 3];
807 uint8_t mask = bios->data[offset + 4];
808 uint8_t shift = bios->data[offset + 5];
809 uint8_t count = bios->data[offset + 6];
810 uint32_t reg = ROM32(bios->data[offset + 7]);
811 uint8_t config;
812 uint32_t configval;
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +0000813 int len = 11 + count * 4;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000814
815 if (!iexec->execute)
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +0000816 return len;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000817
818 BIOSLOG(bios, "0x%04X: Port: 0x%04X, Index: 0x%02X, Mask: 0x%02X, "
819 "Shift: 0x%02X, Count: 0x%02X, Reg: 0x%08X\n",
820 offset, crtcport, crtcindex, mask, shift, count, reg);
821
822 config = (bios_idxprt_rd(bios, crtcport, crtcindex) & mask) >> shift;
823 if (config > count) {
824 NV_ERROR(bios->dev,
825 "0x%04X: Config 0x%02X exceeds maximal bound 0x%02X\n",
826 offset, config, count);
Ben Skeggs309b8c82010-06-29 16:09:24 +1000827 return len;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000828 }
829
830 configval = ROM32(bios->data[offset + 11 + config * 4]);
831
832 BIOSLOG(bios, "0x%04X: Writing config %02X\n", offset, config);
833
834 bios_wr32(bios, reg, configval);
835
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +0000836 return len;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000837}
838
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +0000839static int
Ben Skeggs6ee73862009-12-11 19:24:15 +1000840init_repeat(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
841{
842 /*
843 * INIT_REPEAT opcode: 0x33 ('3')
844 *
845 * offset (8 bit): opcode
846 * offset + 1 (8 bit): count
847 *
848 * Execute script following this opcode up to INIT_REPEAT_END
849 * "count" times
850 */
851
852 uint8_t count = bios->data[offset + 1];
853 uint8_t i;
854
855 /* no iexec->execute check by design */
856
857 BIOSLOG(bios, "0x%04X: Repeating following segment %d times\n",
858 offset, count);
859
860 iexec->repeat = true;
861
862 /*
863 * count - 1, as the script block will execute once when we leave this
864 * opcode -- this is compatible with bios behaviour as:
865 * a) the block is always executed at least once, even if count == 0
866 * b) the bios interpreter skips to the op following INIT_END_REPEAT,
867 * while we don't
868 */
869 for (i = 0; i < count - 1; i++)
870 parse_init_table(bios, offset + 2, iexec);
871
872 iexec->repeat = false;
873
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +0000874 return 2;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000875}
876
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +0000877static int
Ben Skeggs6ee73862009-12-11 19:24:15 +1000878init_io_restrict_pll(struct nvbios *bios, uint16_t offset,
879 struct init_exec *iexec)
880{
881 /*
882 * INIT_IO_RESTRICT_PLL opcode: 0x34 ('4')
883 *
884 * offset (8 bit): opcode
885 * offset + 1 (16 bit): CRTC port
886 * offset + 3 (8 bit): CRTC index
887 * offset + 4 (8 bit): mask
888 * offset + 5 (8 bit): shift
889 * offset + 6 (8 bit): IO flag condition index
890 * offset + 7 (8 bit): count
891 * offset + 8 (32 bit): register
892 * offset + 12 (16 bit): frequency 1
893 * ...
894 *
895 * Starting at offset + 12 there are "count" 16 bit frequencies (10kHz).
896 * Set PLL register "register" to coefficients for frequency n,
897 * selected by reading index "CRTC index" of "CRTC port" ANDed with
898 * "mask" and shifted right by "shift".
899 *
900 * If "IO flag condition index" > 0, and condition met, double
901 * frequency before setting it.
902 */
903
904 uint16_t crtcport = ROM16(bios->data[offset + 1]);
905 uint8_t crtcindex = bios->data[offset + 3];
906 uint8_t mask = bios->data[offset + 4];
907 uint8_t shift = bios->data[offset + 5];
908 int8_t io_flag_condition_idx = bios->data[offset + 6];
909 uint8_t count = bios->data[offset + 7];
910 uint32_t reg = ROM32(bios->data[offset + 8]);
911 uint8_t config;
912 uint16_t freq;
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +0000913 int len = 12 + count * 2;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000914
915 if (!iexec->execute)
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +0000916 return len;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000917
918 BIOSLOG(bios, "0x%04X: Port: 0x%04X, Index: 0x%02X, Mask: 0x%02X, "
919 "Shift: 0x%02X, IO Flag Condition: 0x%02X, "
920 "Count: 0x%02X, Reg: 0x%08X\n",
921 offset, crtcport, crtcindex, mask, shift,
922 io_flag_condition_idx, count, reg);
923
924 config = (bios_idxprt_rd(bios, crtcport, crtcindex) & mask) >> shift;
925 if (config > count) {
926 NV_ERROR(bios->dev,
927 "0x%04X: Config 0x%02X exceeds maximal bound 0x%02X\n",
928 offset, config, count);
Ben Skeggs309b8c82010-06-29 16:09:24 +1000929 return len;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000930 }
931
932 freq = ROM16(bios->data[offset + 12 + config * 2]);
933
934 if (io_flag_condition_idx > 0) {
935 if (io_flag_condition_met(bios, offset, io_flag_condition_idx)) {
936 BIOSLOG(bios, "0x%04X: Condition fulfilled -- "
937 "frequency doubled\n", offset);
938 freq *= 2;
939 } else
940 BIOSLOG(bios, "0x%04X: Condition not fulfilled -- "
941 "frequency unchanged\n", offset);
942 }
943
944 BIOSLOG(bios, "0x%04X: Reg: 0x%08X, Config: 0x%02X, Freq: %d0kHz\n",
945 offset, reg, config, freq);
946
947 setPLL(bios, reg, freq * 10);
948
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +0000949 return len;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000950}
951
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +0000952static int
Ben Skeggs6ee73862009-12-11 19:24:15 +1000953init_end_repeat(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
954{
955 /*
956 * INIT_END_REPEAT opcode: 0x36 ('6')
957 *
958 * offset (8 bit): opcode
959 *
960 * Marks the end of the block for INIT_REPEAT to repeat
961 */
962
963 /* no iexec->execute check by design */
964
965 /*
966 * iexec->repeat flag necessary to go past INIT_END_REPEAT opcode when
967 * we're not in repeat mode
968 */
969 if (iexec->repeat)
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +0000970 return 0;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000971
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +0000972 return 1;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000973}
974
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +0000975static int
Ben Skeggs6ee73862009-12-11 19:24:15 +1000976init_copy(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
977{
978 /*
979 * INIT_COPY opcode: 0x37 ('7')
980 *
981 * offset (8 bit): opcode
982 * offset + 1 (32 bit): register
983 * offset + 5 (8 bit): shift
984 * offset + 6 (8 bit): srcmask
985 * offset + 7 (16 bit): CRTC port
986 * offset + 9 (8 bit): CRTC index
987 * offset + 10 (8 bit): mask
988 *
989 * Read index "CRTC index" on "CRTC port", AND with "mask", OR with
990 * (REGVAL("register") >> "shift" & "srcmask") and write-back to CRTC
991 * port
992 */
993
994 uint32_t reg = ROM32(bios->data[offset + 1]);
995 uint8_t shift = bios->data[offset + 5];
996 uint8_t srcmask = bios->data[offset + 6];
997 uint16_t crtcport = ROM16(bios->data[offset + 7]);
998 uint8_t crtcindex = bios->data[offset + 9];
999 uint8_t mask = bios->data[offset + 10];
1000 uint32_t data;
1001 uint8_t crtcdata;
1002
1003 if (!iexec->execute)
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00001004 return 11;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001005
1006 BIOSLOG(bios, "0x%04X: Reg: 0x%08X, Shift: 0x%02X, SrcMask: 0x%02X, "
1007 "Port: 0x%04X, Index: 0x%02X, Mask: 0x%02X\n",
1008 offset, reg, shift, srcmask, crtcport, crtcindex, mask);
1009
1010 data = bios_rd32(bios, reg);
1011
1012 if (shift < 0x80)
1013 data >>= shift;
1014 else
1015 data <<= (0x100 - shift);
1016
1017 data &= srcmask;
1018
1019 crtcdata = bios_idxprt_rd(bios, crtcport, crtcindex) & mask;
1020 crtcdata |= (uint8_t)data;
1021 bios_idxprt_wr(bios, crtcport, crtcindex, crtcdata);
1022
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00001023 return 11;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001024}
1025
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00001026static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10001027init_not(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
1028{
1029 /*
1030 * INIT_NOT opcode: 0x38 ('8')
1031 *
1032 * offset (8 bit): opcode
1033 *
1034 * Invert the current execute / no-execute condition (i.e. "else")
1035 */
1036 if (iexec->execute)
1037 BIOSLOG(bios, "0x%04X: ------ Skipping following commands ------\n", offset);
1038 else
1039 BIOSLOG(bios, "0x%04X: ------ Executing following commands ------\n", offset);
1040
1041 iexec->execute = !iexec->execute;
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00001042 return 1;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001043}
1044
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00001045static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10001046init_io_flag_condition(struct nvbios *bios, uint16_t offset,
1047 struct init_exec *iexec)
1048{
1049 /*
1050 * INIT_IO_FLAG_CONDITION opcode: 0x39 ('9')
1051 *
1052 * offset (8 bit): opcode
1053 * offset + 1 (8 bit): condition number
1054 *
1055 * Check condition "condition number" in the IO flag condition table.
1056 * If condition not met skip subsequent opcodes until condition is
1057 * inverted (INIT_NOT), or we hit INIT_RESUME
1058 */
1059
1060 uint8_t cond = bios->data[offset + 1];
1061
1062 if (!iexec->execute)
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00001063 return 2;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001064
1065 if (io_flag_condition_met(bios, offset, cond))
1066 BIOSLOG(bios, "0x%04X: Condition fulfilled -- continuing to execute\n", offset);
1067 else {
1068 BIOSLOG(bios, "0x%04X: Condition not fulfilled -- skipping following commands\n", offset);
1069 iexec->execute = false;
1070 }
1071
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00001072 return 2;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001073}
1074
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00001075static int
Ben Skeggs25908b72010-04-20 02:28:37 +10001076init_dp_condition(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
1077{
1078 /*
1079 * INIT_DP_CONDITION opcode: 0x3A ('')
1080 *
1081 * offset (8 bit): opcode
1082 * offset + 1 (8 bit): "sub" opcode
1083 * offset + 2 (8 bit): unknown
1084 *
1085 */
1086
Ben Skeggs25908b72010-04-20 02:28:37 +10001087 struct dcb_entry *dcb = bios->display.output;
1088 struct drm_device *dev = bios->dev;
1089 uint8_t cond = bios->data[offset + 1];
Ben Skeggs5f1800b2011-08-05 14:07:04 +10001090 uint8_t *table, *entry;
Ben Skeggs25908b72010-04-20 02:28:37 +10001091
1092 BIOSLOG(bios, "0x%04X: subop 0x%02X\n", offset, cond);
1093
1094 if (!iexec->execute)
1095 return 3;
1096
Ben Skeggs5f1800b2011-08-05 14:07:04 +10001097 table = nouveau_dp_bios_data(dev, dcb, &entry);
1098 if (!table)
Ben Skeggs309b8c82010-06-29 16:09:24 +10001099 return 3;
Ben Skeggs25908b72010-04-20 02:28:37 +10001100
1101 switch (cond) {
1102 case 0:
1103 {
1104 struct dcb_connector_table_entry *ent =
1105 &bios->dcb.connector.entry[dcb->connector];
1106
1107 if (ent->type != DCB_CONNECTOR_eDP)
1108 iexec->execute = false;
1109 }
1110 break;
1111 case 1:
1112 case 2:
Ben Skeggs5f1800b2011-08-05 14:07:04 +10001113 if (!(entry[5] & cond))
Ben Skeggs25908b72010-04-20 02:28:37 +10001114 iexec->execute = false;
1115 break;
1116 case 5:
1117 {
1118 struct nouveau_i2c_chan *auxch;
1119 int ret;
1120
1121 auxch = nouveau_i2c_find(dev, bios->display.output->i2c_index);
Ben Skeggs309b8c82010-06-29 16:09:24 +10001122 if (!auxch) {
1123 NV_ERROR(dev, "0x%04X: couldn't get auxch\n", offset);
1124 return 3;
1125 }
Ben Skeggs25908b72010-04-20 02:28:37 +10001126
1127 ret = nouveau_dp_auxch(auxch, 9, 0xd, &cond, 1);
Ben Skeggs309b8c82010-06-29 16:09:24 +10001128 if (ret) {
1129 NV_ERROR(dev, "0x%04X: auxch rd fail: %d\n", offset, ret);
1130 return 3;
1131 }
Ben Skeggs25908b72010-04-20 02:28:37 +10001132
Ben Skeggs64d202b2010-09-21 12:10:51 +10001133 if (!(cond & 1))
Ben Skeggs25908b72010-04-20 02:28:37 +10001134 iexec->execute = false;
1135 }
1136 break;
1137 default:
1138 NV_WARN(dev, "0x%04X: unknown INIT_3A op: %d\n", offset, cond);
1139 break;
1140 }
1141
1142 if (iexec->execute)
1143 BIOSLOG(bios, "0x%04X: continuing to execute\n", offset);
1144 else
1145 BIOSLOG(bios, "0x%04X: skipping following commands\n", offset);
1146
1147 return 3;
1148}
1149
1150static int
1151init_op_3b(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
1152{
1153 /*
1154 * INIT_3B opcode: 0x3B ('')
1155 *
1156 * offset (8 bit): opcode
1157 * offset + 1 (8 bit): crtc index
1158 *
1159 */
1160
1161 uint8_t or = ffs(bios->display.output->or) - 1;
1162 uint8_t index = bios->data[offset + 1];
1163 uint8_t data;
1164
1165 if (!iexec->execute)
1166 return 2;
1167
1168 data = bios_idxprt_rd(bios, 0x3d4, index);
1169 bios_idxprt_wr(bios, 0x3d4, index, data & ~(1 << or));
1170 return 2;
1171}
1172
1173static int
1174init_op_3c(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
1175{
1176 /*
1177 * INIT_3C opcode: 0x3C ('')
1178 *
1179 * offset (8 bit): opcode
1180 * offset + 1 (8 bit): crtc index
1181 *
1182 */
1183
1184 uint8_t or = ffs(bios->display.output->or) - 1;
1185 uint8_t index = bios->data[offset + 1];
1186 uint8_t data;
1187
1188 if (!iexec->execute)
1189 return 2;
1190
1191 data = bios_idxprt_rd(bios, 0x3d4, index);
1192 bios_idxprt_wr(bios, 0x3d4, index, data | (1 << or));
1193 return 2;
1194}
1195
1196static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10001197init_idx_addr_latched(struct nvbios *bios, uint16_t offset,
1198 struct init_exec *iexec)
1199{
1200 /*
1201 * INIT_INDEX_ADDRESS_LATCHED opcode: 0x49 ('I')
1202 *
1203 * offset (8 bit): opcode
1204 * offset + 1 (32 bit): control register
1205 * offset + 5 (32 bit): data register
1206 * offset + 9 (32 bit): mask
1207 * offset + 13 (32 bit): data
1208 * offset + 17 (8 bit): count
1209 * offset + 18 (8 bit): address 1
1210 * offset + 19 (8 bit): data 1
1211 * ...
1212 *
1213 * For each of "count" address and data pairs, write "data n" to
1214 * "data register", read the current value of "control register",
1215 * and write it back once ANDed with "mask", ORed with "data",
1216 * and ORed with "address n"
1217 */
1218
1219 uint32_t controlreg = ROM32(bios->data[offset + 1]);
1220 uint32_t datareg = ROM32(bios->data[offset + 5]);
1221 uint32_t mask = ROM32(bios->data[offset + 9]);
1222 uint32_t data = ROM32(bios->data[offset + 13]);
1223 uint8_t count = bios->data[offset + 17];
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00001224 int len = 18 + count * 2;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001225 uint32_t value;
1226 int i;
1227
1228 if (!iexec->execute)
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00001229 return len;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001230
1231 BIOSLOG(bios, "0x%04X: ControlReg: 0x%08X, DataReg: 0x%08X, "
1232 "Mask: 0x%08X, Data: 0x%08X, Count: 0x%02X\n",
1233 offset, controlreg, datareg, mask, data, count);
1234
1235 for (i = 0; i < count; i++) {
1236 uint8_t instaddress = bios->data[offset + 18 + i * 2];
1237 uint8_t instdata = bios->data[offset + 19 + i * 2];
1238
1239 BIOSLOG(bios, "0x%04X: Address: 0x%02X, Data: 0x%02X\n",
1240 offset, instaddress, instdata);
1241
1242 bios_wr32(bios, datareg, instdata);
1243 value = bios_rd32(bios, controlreg) & mask;
1244 value |= data;
1245 value |= instaddress;
1246 bios_wr32(bios, controlreg, value);
1247 }
1248
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00001249 return len;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001250}
1251
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00001252static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10001253init_io_restrict_pll2(struct nvbios *bios, uint16_t offset,
1254 struct init_exec *iexec)
1255{
1256 /*
1257 * INIT_IO_RESTRICT_PLL2 opcode: 0x4A ('J')
1258 *
1259 * offset (8 bit): opcode
1260 * offset + 1 (16 bit): CRTC port
1261 * offset + 3 (8 bit): CRTC index
1262 * offset + 4 (8 bit): mask
1263 * offset + 5 (8 bit): shift
1264 * offset + 6 (8 bit): count
1265 * offset + 7 (32 bit): register
1266 * offset + 11 (32 bit): frequency 1
1267 * ...
1268 *
1269 * Starting at offset + 11 there are "count" 32 bit frequencies (kHz).
1270 * Set PLL register "register" to coefficients for frequency n,
1271 * selected by reading index "CRTC index" of "CRTC port" ANDed with
1272 * "mask" and shifted right by "shift".
1273 */
1274
1275 uint16_t crtcport = ROM16(bios->data[offset + 1]);
1276 uint8_t crtcindex = bios->data[offset + 3];
1277 uint8_t mask = bios->data[offset + 4];
1278 uint8_t shift = bios->data[offset + 5];
1279 uint8_t count = bios->data[offset + 6];
1280 uint32_t reg = ROM32(bios->data[offset + 7]);
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00001281 int len = 11 + count * 4;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001282 uint8_t config;
1283 uint32_t freq;
1284
1285 if (!iexec->execute)
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00001286 return len;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001287
1288 BIOSLOG(bios, "0x%04X: Port: 0x%04X, Index: 0x%02X, Mask: 0x%02X, "
1289 "Shift: 0x%02X, Count: 0x%02X, Reg: 0x%08X\n",
1290 offset, crtcport, crtcindex, mask, shift, count, reg);
1291
1292 if (!reg)
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00001293 return len;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001294
1295 config = (bios_idxprt_rd(bios, crtcport, crtcindex) & mask) >> shift;
1296 if (config > count) {
1297 NV_ERROR(bios->dev,
1298 "0x%04X: Config 0x%02X exceeds maximal bound 0x%02X\n",
1299 offset, config, count);
Ben Skeggs309b8c82010-06-29 16:09:24 +10001300 return len;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001301 }
1302
1303 freq = ROM32(bios->data[offset + 11 + config * 4]);
1304
1305 BIOSLOG(bios, "0x%04X: Reg: 0x%08X, Config: 0x%02X, Freq: %dkHz\n",
1306 offset, reg, config, freq);
1307
1308 setPLL(bios, reg, freq);
1309
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00001310 return len;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001311}
1312
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00001313static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10001314init_pll2(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
1315{
1316 /*
1317 * INIT_PLL2 opcode: 0x4B ('K')
1318 *
1319 * offset (8 bit): opcode
1320 * offset + 1 (32 bit): register
1321 * offset + 5 (32 bit): freq
1322 *
1323 * Set PLL register "register" to coefficients for frequency "freq"
1324 */
1325
1326 uint32_t reg = ROM32(bios->data[offset + 1]);
1327 uint32_t freq = ROM32(bios->data[offset + 5]);
1328
1329 if (!iexec->execute)
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00001330 return 9;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001331
1332 BIOSLOG(bios, "0x%04X: Reg: 0x%04X, Freq: %dkHz\n",
1333 offset, reg, freq);
1334
1335 setPLL(bios, reg, freq);
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00001336 return 9;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001337}
1338
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00001339static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10001340init_i2c_byte(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
1341{
1342 /*
1343 * INIT_I2C_BYTE opcode: 0x4C ('L')
1344 *
1345 * offset (8 bit): opcode
1346 * offset + 1 (8 bit): DCB I2C table entry index
1347 * offset + 2 (8 bit): I2C slave address
1348 * offset + 3 (8 bit): count
1349 * offset + 4 (8 bit): I2C register 1
1350 * offset + 5 (8 bit): mask 1
1351 * offset + 6 (8 bit): data 1
1352 * ...
1353 *
1354 * For each of "count" registers given by "I2C register n" on the device
1355 * addressed by "I2C slave address" on the I2C bus given by
1356 * "DCB I2C table entry index", read the register, AND the result with
1357 * "mask n" and OR it with "data n" before writing it back to the device
1358 */
1359
Ben Skeggs309b8c82010-06-29 16:09:24 +10001360 struct drm_device *dev = bios->dev;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001361 uint8_t i2c_index = bios->data[offset + 1];
Ben Skeggs893887ed2010-05-12 16:30:50 +10001362 uint8_t i2c_address = bios->data[offset + 2] >> 1;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001363 uint8_t count = bios->data[offset + 3];
1364 struct nouveau_i2c_chan *chan;
Ben Skeggs893887ed2010-05-12 16:30:50 +10001365 int len = 4 + count * 3;
1366 int ret, i;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001367
1368 if (!iexec->execute)
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00001369 return len;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001370
1371 BIOSLOG(bios, "0x%04X: DCBI2CIndex: 0x%02X, I2CAddress: 0x%02X, "
1372 "Count: 0x%02X\n",
1373 offset, i2c_index, i2c_address, count);
1374
Ben Skeggs309b8c82010-06-29 16:09:24 +10001375 chan = init_i2c_device_find(dev, i2c_index);
1376 if (!chan) {
1377 NV_ERROR(dev, "0x%04X: i2c bus not found\n", offset);
1378 return len;
1379 }
Ben Skeggs6ee73862009-12-11 19:24:15 +10001380
1381 for (i = 0; i < count; i++) {
Ben Skeggs893887ed2010-05-12 16:30:50 +10001382 uint8_t reg = bios->data[offset + 4 + i * 3];
Ben Skeggs6ee73862009-12-11 19:24:15 +10001383 uint8_t mask = bios->data[offset + 5 + i * 3];
1384 uint8_t data = bios->data[offset + 6 + i * 3];
Ben Skeggs893887ed2010-05-12 16:30:50 +10001385 union i2c_smbus_data val;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001386
Ben Skeggs893887ed2010-05-12 16:30:50 +10001387 ret = i2c_smbus_xfer(&chan->adapter, i2c_address, 0,
1388 I2C_SMBUS_READ, reg,
1389 I2C_SMBUS_BYTE_DATA, &val);
Ben Skeggs309b8c82010-06-29 16:09:24 +10001390 if (ret < 0) {
1391 NV_ERROR(dev, "0x%04X: i2c rd fail: %d\n", offset, ret);
1392 return len;
1393 }
Ben Skeggs6ee73862009-12-11 19:24:15 +10001394
1395 BIOSLOG(bios, "0x%04X: I2CReg: 0x%02X, Value: 0x%02X, "
1396 "Mask: 0x%02X, Data: 0x%02X\n",
Ben Skeggs893887ed2010-05-12 16:30:50 +10001397 offset, reg, val.byte, mask, data);
Ben Skeggs6ee73862009-12-11 19:24:15 +10001398
Ben Skeggs893887ed2010-05-12 16:30:50 +10001399 if (!bios->execute)
1400 continue;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001401
Ben Skeggs893887ed2010-05-12 16:30:50 +10001402 val.byte &= mask;
1403 val.byte |= data;
1404 ret = i2c_smbus_xfer(&chan->adapter, i2c_address, 0,
1405 I2C_SMBUS_WRITE, reg,
1406 I2C_SMBUS_BYTE_DATA, &val);
Ben Skeggs309b8c82010-06-29 16:09:24 +10001407 if (ret < 0) {
1408 NV_ERROR(dev, "0x%04X: i2c wr fail: %d\n", offset, ret);
1409 return len;
1410 }
Ben Skeggs6ee73862009-12-11 19:24:15 +10001411 }
1412
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00001413 return len;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001414}
1415
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00001416static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10001417init_zm_i2c_byte(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
1418{
1419 /*
1420 * INIT_ZM_I2C_BYTE opcode: 0x4D ('M')
1421 *
1422 * offset (8 bit): opcode
1423 * offset + 1 (8 bit): DCB I2C table entry index
1424 * offset + 2 (8 bit): I2C slave address
1425 * offset + 3 (8 bit): count
1426 * offset + 4 (8 bit): I2C register 1
1427 * offset + 5 (8 bit): data 1
1428 * ...
1429 *
1430 * For each of "count" registers given by "I2C register n" on the device
1431 * addressed by "I2C slave address" on the I2C bus given by
1432 * "DCB I2C table entry index", set the register to "data n"
1433 */
1434
Ben Skeggs309b8c82010-06-29 16:09:24 +10001435 struct drm_device *dev = bios->dev;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001436 uint8_t i2c_index = bios->data[offset + 1];
Ben Skeggs893887ed2010-05-12 16:30:50 +10001437 uint8_t i2c_address = bios->data[offset + 2] >> 1;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001438 uint8_t count = bios->data[offset + 3];
1439 struct nouveau_i2c_chan *chan;
Ben Skeggs893887ed2010-05-12 16:30:50 +10001440 int len = 4 + count * 2;
1441 int ret, i;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001442
1443 if (!iexec->execute)
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00001444 return len;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001445
1446 BIOSLOG(bios, "0x%04X: DCBI2CIndex: 0x%02X, I2CAddress: 0x%02X, "
1447 "Count: 0x%02X\n",
1448 offset, i2c_index, i2c_address, count);
1449
Ben Skeggs309b8c82010-06-29 16:09:24 +10001450 chan = init_i2c_device_find(dev, i2c_index);
1451 if (!chan) {
1452 NV_ERROR(dev, "0x%04X: i2c bus not found\n", offset);
1453 return len;
1454 }
Ben Skeggs6ee73862009-12-11 19:24:15 +10001455
1456 for (i = 0; i < count; i++) {
Ben Skeggs893887ed2010-05-12 16:30:50 +10001457 uint8_t reg = bios->data[offset + 4 + i * 2];
1458 union i2c_smbus_data val;
1459
1460 val.byte = bios->data[offset + 5 + i * 2];
Ben Skeggs6ee73862009-12-11 19:24:15 +10001461
1462 BIOSLOG(bios, "0x%04X: I2CReg: 0x%02X, Data: 0x%02X\n",
Ben Skeggs893887ed2010-05-12 16:30:50 +10001463 offset, reg, val.byte);
Ben Skeggs6ee73862009-12-11 19:24:15 +10001464
Ben Skeggs893887ed2010-05-12 16:30:50 +10001465 if (!bios->execute)
1466 continue;
1467
1468 ret = i2c_smbus_xfer(&chan->adapter, i2c_address, 0,
1469 I2C_SMBUS_WRITE, reg,
1470 I2C_SMBUS_BYTE_DATA, &val);
Ben Skeggs309b8c82010-06-29 16:09:24 +10001471 if (ret < 0) {
1472 NV_ERROR(dev, "0x%04X: i2c wr fail: %d\n", offset, ret);
1473 return len;
1474 }
Ben Skeggs6ee73862009-12-11 19:24:15 +10001475 }
1476
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00001477 return len;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001478}
1479
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00001480static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10001481init_zm_i2c(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
1482{
1483 /*
1484 * INIT_ZM_I2C opcode: 0x4E ('N')
1485 *
1486 * offset (8 bit): opcode
1487 * offset + 1 (8 bit): DCB I2C table entry index
1488 * offset + 2 (8 bit): I2C slave address
1489 * offset + 3 (8 bit): count
1490 * offset + 4 (8 bit): data 1
1491 * ...
1492 *
1493 * Send "count" bytes ("data n") to the device addressed by "I2C slave
1494 * address" on the I2C bus given by "DCB I2C table entry index"
1495 */
1496
Ben Skeggs309b8c82010-06-29 16:09:24 +10001497 struct drm_device *dev = bios->dev;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001498 uint8_t i2c_index = bios->data[offset + 1];
Ben Skeggs893887ed2010-05-12 16:30:50 +10001499 uint8_t i2c_address = bios->data[offset + 2] >> 1;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001500 uint8_t count = bios->data[offset + 3];
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00001501 int len = 4 + count;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001502 struct nouveau_i2c_chan *chan;
1503 struct i2c_msg msg;
1504 uint8_t data[256];
Ben Skeggs309b8c82010-06-29 16:09:24 +10001505 int ret, i;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001506
1507 if (!iexec->execute)
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00001508 return len;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001509
1510 BIOSLOG(bios, "0x%04X: DCBI2CIndex: 0x%02X, I2CAddress: 0x%02X, "
1511 "Count: 0x%02X\n",
1512 offset, i2c_index, i2c_address, count);
1513
Ben Skeggs309b8c82010-06-29 16:09:24 +10001514 chan = init_i2c_device_find(dev, i2c_index);
1515 if (!chan) {
1516 NV_ERROR(dev, "0x%04X: i2c bus not found\n", offset);
1517 return len;
1518 }
Ben Skeggs6ee73862009-12-11 19:24:15 +10001519
1520 for (i = 0; i < count; i++) {
1521 data[i] = bios->data[offset + 4 + i];
1522
1523 BIOSLOG(bios, "0x%04X: Data: 0x%02X\n", offset, data[i]);
1524 }
1525
1526 if (bios->execute) {
1527 msg.addr = i2c_address;
1528 msg.flags = 0;
1529 msg.len = count;
1530 msg.buf = data;
Ben Skeggs309b8c82010-06-29 16:09:24 +10001531 ret = i2c_transfer(&chan->adapter, &msg, 1);
1532 if (ret != 1) {
1533 NV_ERROR(dev, "0x%04X: i2c wr fail: %d\n", offset, ret);
1534 return len;
1535 }
Ben Skeggs6ee73862009-12-11 19:24:15 +10001536 }
1537
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00001538 return len;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001539}
1540
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00001541static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10001542init_tmds(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
1543{
1544 /*
1545 * INIT_TMDS opcode: 0x4F ('O') (non-canon name)
1546 *
1547 * offset (8 bit): opcode
1548 * offset + 1 (8 bit): magic lookup value
1549 * offset + 2 (8 bit): TMDS address
1550 * offset + 3 (8 bit): mask
1551 * offset + 4 (8 bit): data
1552 *
1553 * Read the data reg for TMDS address "TMDS address", AND it with mask
1554 * and OR it with data, then write it back
1555 * "magic lookup value" determines which TMDS base address register is
1556 * used -- see get_tmds_index_reg()
1557 */
1558
Ben Skeggs309b8c82010-06-29 16:09:24 +10001559 struct drm_device *dev = bios->dev;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001560 uint8_t mlv = bios->data[offset + 1];
1561 uint32_t tmdsaddr = bios->data[offset + 2];
1562 uint8_t mask = bios->data[offset + 3];
1563 uint8_t data = bios->data[offset + 4];
1564 uint32_t reg, value;
1565
1566 if (!iexec->execute)
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00001567 return 5;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001568
1569 BIOSLOG(bios, "0x%04X: MagicLookupValue: 0x%02X, TMDSAddr: 0x%02X, "
1570 "Mask: 0x%02X, Data: 0x%02X\n",
1571 offset, mlv, tmdsaddr, mask, data);
1572
1573 reg = get_tmds_index_reg(bios->dev, mlv);
Ben Skeggs309b8c82010-06-29 16:09:24 +10001574 if (!reg) {
1575 NV_ERROR(dev, "0x%04X: no tmds_index_reg\n", offset);
1576 return 5;
1577 }
Ben Skeggs6ee73862009-12-11 19:24:15 +10001578
1579 bios_wr32(bios, reg,
1580 tmdsaddr | NV_PRAMDAC_FP_TMDS_CONTROL_WRITE_DISABLE);
1581 value = (bios_rd32(bios, reg + 4) & mask) | data;
1582 bios_wr32(bios, reg + 4, value);
1583 bios_wr32(bios, reg, tmdsaddr);
1584
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00001585 return 5;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001586}
1587
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00001588static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10001589init_zm_tmds_group(struct nvbios *bios, uint16_t offset,
1590 struct init_exec *iexec)
1591{
1592 /*
1593 * INIT_ZM_TMDS_GROUP opcode: 0x50 ('P') (non-canon name)
1594 *
1595 * offset (8 bit): opcode
1596 * offset + 1 (8 bit): magic lookup value
1597 * offset + 2 (8 bit): count
1598 * offset + 3 (8 bit): addr 1
1599 * offset + 4 (8 bit): data 1
1600 * ...
1601 *
1602 * For each of "count" TMDS address and data pairs write "data n" to
1603 * "addr n". "magic lookup value" determines which TMDS base address
1604 * register is used -- see get_tmds_index_reg()
1605 */
1606
Ben Skeggs309b8c82010-06-29 16:09:24 +10001607 struct drm_device *dev = bios->dev;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001608 uint8_t mlv = bios->data[offset + 1];
1609 uint8_t count = bios->data[offset + 2];
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00001610 int len = 3 + count * 2;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001611 uint32_t reg;
1612 int i;
1613
1614 if (!iexec->execute)
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00001615 return len;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001616
1617 BIOSLOG(bios, "0x%04X: MagicLookupValue: 0x%02X, Count: 0x%02X\n",
1618 offset, mlv, count);
1619
1620 reg = get_tmds_index_reg(bios->dev, mlv);
Ben Skeggs309b8c82010-06-29 16:09:24 +10001621 if (!reg) {
1622 NV_ERROR(dev, "0x%04X: no tmds_index_reg\n", offset);
1623 return len;
1624 }
Ben Skeggs6ee73862009-12-11 19:24:15 +10001625
1626 for (i = 0; i < count; i++) {
1627 uint8_t tmdsaddr = bios->data[offset + 3 + i * 2];
1628 uint8_t tmdsdata = bios->data[offset + 4 + i * 2];
1629
1630 bios_wr32(bios, reg + 4, tmdsdata);
1631 bios_wr32(bios, reg, tmdsaddr);
1632 }
1633
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00001634 return len;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001635}
1636
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00001637static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10001638init_cr_idx_adr_latch(struct nvbios *bios, uint16_t offset,
1639 struct init_exec *iexec)
1640{
1641 /*
1642 * INIT_CR_INDEX_ADDRESS_LATCHED opcode: 0x51 ('Q')
1643 *
1644 * offset (8 bit): opcode
1645 * offset + 1 (8 bit): CRTC index1
1646 * offset + 2 (8 bit): CRTC index2
1647 * offset + 3 (8 bit): baseaddr
1648 * offset + 4 (8 bit): count
1649 * offset + 5 (8 bit): data 1
1650 * ...
1651 *
1652 * For each of "count" address and data pairs, write "baseaddr + n" to
1653 * "CRTC index1" and "data n" to "CRTC index2"
1654 * Once complete, restore initial value read from "CRTC index1"
1655 */
1656 uint8_t crtcindex1 = bios->data[offset + 1];
1657 uint8_t crtcindex2 = bios->data[offset + 2];
1658 uint8_t baseaddr = bios->data[offset + 3];
1659 uint8_t count = bios->data[offset + 4];
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00001660 int len = 5 + count;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001661 uint8_t oldaddr, data;
1662 int i;
1663
1664 if (!iexec->execute)
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00001665 return len;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001666
1667 BIOSLOG(bios, "0x%04X: Index1: 0x%02X, Index2: 0x%02X, "
1668 "BaseAddr: 0x%02X, Count: 0x%02X\n",
1669 offset, crtcindex1, crtcindex2, baseaddr, count);
1670
1671 oldaddr = bios_idxprt_rd(bios, NV_CIO_CRX__COLOR, crtcindex1);
1672
1673 for (i = 0; i < count; i++) {
1674 bios_idxprt_wr(bios, NV_CIO_CRX__COLOR, crtcindex1,
1675 baseaddr + i);
1676 data = bios->data[offset + 5 + i];
1677 bios_idxprt_wr(bios, NV_CIO_CRX__COLOR, crtcindex2, data);
1678 }
1679
1680 bios_idxprt_wr(bios, NV_CIO_CRX__COLOR, crtcindex1, oldaddr);
1681
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00001682 return len;
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_cr(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
1687{
1688 /*
1689 * INIT_CR opcode: 0x52 ('R')
1690 *
1691 * offset (8 bit): opcode
1692 * offset + 1 (8 bit): CRTC index
1693 * offset + 2 (8 bit): mask
1694 * offset + 3 (8 bit): data
1695 *
1696 * Assign the value of at "CRTC index" ANDed with mask and ORed with
1697 * data back to "CRTC index"
1698 */
1699
1700 uint8_t crtcindex = bios->data[offset + 1];
1701 uint8_t mask = bios->data[offset + 2];
1702 uint8_t data = bios->data[offset + 3];
1703 uint8_t value;
1704
1705 if (!iexec->execute)
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00001706 return 4;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001707
1708 BIOSLOG(bios, "0x%04X: Index: 0x%02X, Mask: 0x%02X, Data: 0x%02X\n",
1709 offset, crtcindex, mask, data);
1710
1711 value = bios_idxprt_rd(bios, NV_CIO_CRX__COLOR, crtcindex) & mask;
1712 value |= data;
1713 bios_idxprt_wr(bios, NV_CIO_CRX__COLOR, crtcindex, value);
1714
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00001715 return 4;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001716}
1717
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00001718static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10001719init_zm_cr(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
1720{
1721 /*
1722 * INIT_ZM_CR opcode: 0x53 ('S')
1723 *
1724 * offset (8 bit): opcode
1725 * offset + 1 (8 bit): CRTC index
1726 * offset + 2 (8 bit): value
1727 *
1728 * Assign "value" to CRTC register with index "CRTC index".
1729 */
1730
1731 uint8_t crtcindex = ROM32(bios->data[offset + 1]);
1732 uint8_t data = bios->data[offset + 2];
1733
1734 if (!iexec->execute)
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00001735 return 3;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001736
1737 bios_idxprt_wr(bios, NV_CIO_CRX__COLOR, crtcindex, data);
1738
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00001739 return 3;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001740}
1741
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00001742static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10001743init_zm_cr_group(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
1744{
1745 /*
1746 * INIT_ZM_CR_GROUP opcode: 0x54 ('T')
1747 *
1748 * offset (8 bit): opcode
1749 * offset + 1 (8 bit): count
1750 * offset + 2 (8 bit): CRTC index 1
1751 * offset + 3 (8 bit): value 1
1752 * ...
1753 *
1754 * For "count", assign "value n" to CRTC register with index
1755 * "CRTC index n".
1756 */
1757
1758 uint8_t count = bios->data[offset + 1];
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00001759 int len = 2 + count * 2;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001760 int i;
1761
1762 if (!iexec->execute)
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00001763 return len;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001764
1765 for (i = 0; i < count; i++)
1766 init_zm_cr(bios, offset + 2 + 2 * i - 1, iexec);
1767
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00001768 return len;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001769}
1770
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00001771static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10001772init_condition_time(struct nvbios *bios, uint16_t offset,
1773 struct init_exec *iexec)
1774{
1775 /*
1776 * INIT_CONDITION_TIME opcode: 0x56 ('V')
1777 *
1778 * offset (8 bit): opcode
1779 * offset + 1 (8 bit): condition number
1780 * offset + 2 (8 bit): retries / 50
1781 *
1782 * Check condition "condition number" in the condition table.
1783 * Bios code then sleeps for 2ms if the condition is not met, and
1784 * repeats up to "retries" times, but on one C51 this has proved
1785 * insufficient. In mmiotraces the driver sleeps for 20ms, so we do
1786 * this, and bail after "retries" times, or 2s, whichever is less.
1787 * If still not met after retries, clear execution flag for this table.
1788 */
1789
1790 uint8_t cond = bios->data[offset + 1];
1791 uint16_t retries = bios->data[offset + 2] * 50;
1792 unsigned cnt;
1793
1794 if (!iexec->execute)
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00001795 return 3;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001796
1797 if (retries > 100)
1798 retries = 100;
1799
1800 BIOSLOG(bios, "0x%04X: Condition: 0x%02X, Retries: 0x%02X\n",
1801 offset, cond, retries);
1802
1803 if (!bios->execute) /* avoid 2s delays when "faking" execution */
1804 retries = 1;
1805
1806 for (cnt = 0; cnt < retries; cnt++) {
1807 if (bios_condition_met(bios, offset, cond)) {
1808 BIOSLOG(bios, "0x%04X: Condition met, continuing\n",
1809 offset);
1810 break;
1811 } else {
1812 BIOSLOG(bios, "0x%04X: "
1813 "Condition not met, sleeping for 20ms\n",
1814 offset);
Ben Skeggsc7ca4d12011-02-03 20:10:49 +10001815 mdelay(20);
Ben Skeggs6ee73862009-12-11 19:24:15 +10001816 }
1817 }
1818
1819 if (!bios_condition_met(bios, offset, cond)) {
1820 NV_WARN(bios->dev,
1821 "0x%04X: Condition still not met after %dms, "
1822 "skipping following opcodes\n", offset, 20 * retries);
1823 iexec->execute = false;
1824 }
1825
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00001826 return 3;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001827}
1828
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00001829static int
Marcin Koƛcielnickie3a19242010-07-02 19:33:01 +00001830init_ltime(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
1831{
1832 /*
1833 * INIT_LTIME opcode: 0x57 ('V')
1834 *
1835 * offset (8 bit): opcode
1836 * offset + 1 (16 bit): time
1837 *
Stefan Weile8a8b252011-01-02 15:12:42 +01001838 * Sleep for "time" milliseconds.
Marcin Koƛcielnickie3a19242010-07-02 19:33:01 +00001839 */
1840
1841 unsigned time = ROM16(bios->data[offset + 1]);
1842
1843 if (!iexec->execute)
1844 return 3;
1845
Stefan Weile8a8b252011-01-02 15:12:42 +01001846 BIOSLOG(bios, "0x%04X: Sleeping for 0x%04X milliseconds\n",
Marcin Koƛcielnickie3a19242010-07-02 19:33:01 +00001847 offset, time);
1848
Ben Skeggsc7ca4d12011-02-03 20:10:49 +10001849 mdelay(time);
Marcin Koƛcielnickie3a19242010-07-02 19:33:01 +00001850
1851 return 3;
1852}
1853
1854static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10001855init_zm_reg_sequence(struct nvbios *bios, uint16_t offset,
1856 struct init_exec *iexec)
1857{
1858 /*
1859 * INIT_ZM_REG_SEQUENCE opcode: 0x58 ('X')
1860 *
1861 * offset (8 bit): opcode
1862 * offset + 1 (32 bit): base register
1863 * offset + 5 (8 bit): count
1864 * offset + 6 (32 bit): value 1
1865 * ...
1866 *
1867 * Starting at offset + 6 there are "count" 32 bit values.
1868 * For "count" iterations set "base register" + 4 * current_iteration
1869 * to "value current_iteration"
1870 */
1871
1872 uint32_t basereg = ROM32(bios->data[offset + 1]);
1873 uint32_t count = bios->data[offset + 5];
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00001874 int len = 6 + count * 4;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001875 int i;
1876
1877 if (!iexec->execute)
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00001878 return len;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001879
1880 BIOSLOG(bios, "0x%04X: BaseReg: 0x%08X, Count: 0x%02X\n",
1881 offset, basereg, count);
1882
1883 for (i = 0; i < count; i++) {
1884 uint32_t reg = basereg + i * 4;
1885 uint32_t data = ROM32(bios->data[offset + 6 + i * 4]);
1886
1887 bios_wr32(bios, reg, data);
1888 }
1889
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00001890 return len;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001891}
1892
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00001893static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10001894init_sub_direct(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
1895{
1896 /*
1897 * INIT_SUB_DIRECT opcode: 0x5B ('[')
1898 *
1899 * offset (8 bit): opcode
1900 * offset + 1 (16 bit): subroutine offset (in bios)
1901 *
1902 * Calls a subroutine that will execute commands until INIT_DONE
1903 * is found.
1904 */
1905
1906 uint16_t sub_offset = ROM16(bios->data[offset + 1]);
1907
1908 if (!iexec->execute)
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00001909 return 3;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001910
1911 BIOSLOG(bios, "0x%04X: Executing subroutine at 0x%04X\n",
1912 offset, sub_offset);
1913
1914 parse_init_table(bios, sub_offset, iexec);
1915
1916 BIOSLOG(bios, "0x%04X: End of 0x%04X subroutine\n", offset, sub_offset);
1917
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00001918 return 3;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001919}
1920
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00001921static int
Ben Skeggsec64a402011-03-21 21:31:21 +10001922init_jump(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
1923{
1924 /*
1925 * INIT_JUMP opcode: 0x5C ('\')
1926 *
1927 * offset (8 bit): opcode
1928 * offset + 1 (16 bit): offset (in bios)
1929 *
1930 * Continue execution of init table from 'offset'
1931 */
1932
1933 uint16_t jmp_offset = ROM16(bios->data[offset + 1]);
1934
1935 if (!iexec->execute)
1936 return 3;
1937
1938 BIOSLOG(bios, "0x%04X: Jump to 0x%04X\n", offset, jmp_offset);
1939 return jmp_offset - offset;
1940}
1941
1942static int
Marcin Koƛcielnickib715d642010-07-04 02:47:16 +00001943init_i2c_if(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
1944{
1945 /*
1946 * INIT_I2C_IF opcode: 0x5E ('^')
1947 *
1948 * offset (8 bit): opcode
1949 * offset + 1 (8 bit): DCB I2C table entry index
1950 * offset + 2 (8 bit): I2C slave address
1951 * offset + 3 (8 bit): I2C register
1952 * offset + 4 (8 bit): mask
1953 * offset + 5 (8 bit): data
1954 *
1955 * Read the register given by "I2C register" on the device addressed
1956 * by "I2C slave address" on the I2C bus given by "DCB I2C table
1957 * entry index". Compare the result AND "mask" to "data".
1958 * If they're not equal, skip subsequent opcodes until condition is
1959 * inverted (INIT_NOT), or we hit INIT_RESUME
1960 */
1961
1962 uint8_t i2c_index = bios->data[offset + 1];
1963 uint8_t i2c_address = bios->data[offset + 2] >> 1;
1964 uint8_t reg = bios->data[offset + 3];
1965 uint8_t mask = bios->data[offset + 4];
1966 uint8_t data = bios->data[offset + 5];
1967 struct nouveau_i2c_chan *chan;
1968 union i2c_smbus_data val;
1969 int ret;
1970
1971 /* no execute check by design */
1972
1973 BIOSLOG(bios, "0x%04X: DCBI2CIndex: 0x%02X, I2CAddress: 0x%02X\n",
1974 offset, i2c_index, i2c_address);
1975
1976 chan = init_i2c_device_find(bios->dev, i2c_index);
1977 if (!chan)
1978 return -ENODEV;
1979
1980 ret = i2c_smbus_xfer(&chan->adapter, i2c_address, 0,
1981 I2C_SMBUS_READ, reg,
1982 I2C_SMBUS_BYTE_DATA, &val);
1983 if (ret < 0) {
1984 BIOSLOG(bios, "0x%04X: I2CReg: 0x%02X, Value: [no device], "
1985 "Mask: 0x%02X, Data: 0x%02X\n",
1986 offset, reg, mask, data);
1987 iexec->execute = 0;
1988 return 6;
1989 }
1990
1991 BIOSLOG(bios, "0x%04X: I2CReg: 0x%02X, Value: 0x%02X, "
1992 "Mask: 0x%02X, Data: 0x%02X\n",
1993 offset, reg, val.byte, mask, data);
1994
1995 iexec->execute = ((val.byte & mask) == data);
1996
1997 return 6;
1998}
1999
2000static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10002001init_copy_nv_reg(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
2002{
2003 /*
2004 * INIT_COPY_NV_REG opcode: 0x5F ('_')
2005 *
2006 * offset (8 bit): opcode
2007 * offset + 1 (32 bit): src reg
2008 * offset + 5 (8 bit): shift
2009 * offset + 6 (32 bit): src mask
2010 * offset + 10 (32 bit): xor
2011 * offset + 14 (32 bit): dst reg
2012 * offset + 18 (32 bit): dst mask
2013 *
2014 * Shift REGVAL("src reg") right by (signed) "shift", AND result with
2015 * "src mask", then XOR with "xor". Write this OR'd with
2016 * (REGVAL("dst reg") AND'd with "dst mask") to "dst reg"
2017 */
2018
2019 uint32_t srcreg = *((uint32_t *)(&bios->data[offset + 1]));
2020 uint8_t shift = bios->data[offset + 5];
2021 uint32_t srcmask = *((uint32_t *)(&bios->data[offset + 6]));
2022 uint32_t xor = *((uint32_t *)(&bios->data[offset + 10]));
2023 uint32_t dstreg = *((uint32_t *)(&bios->data[offset + 14]));
2024 uint32_t dstmask = *((uint32_t *)(&bios->data[offset + 18]));
2025 uint32_t srcvalue, dstvalue;
2026
2027 if (!iexec->execute)
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00002028 return 22;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002029
2030 BIOSLOG(bios, "0x%04X: SrcReg: 0x%08X, Shift: 0x%02X, SrcMask: 0x%08X, "
2031 "Xor: 0x%08X, DstReg: 0x%08X, DstMask: 0x%08X\n",
2032 offset, srcreg, shift, srcmask, xor, dstreg, dstmask);
2033
2034 srcvalue = bios_rd32(bios, srcreg);
2035
2036 if (shift < 0x80)
2037 srcvalue >>= shift;
2038 else
2039 srcvalue <<= (0x100 - shift);
2040
2041 srcvalue = (srcvalue & srcmask) ^ xor;
2042
2043 dstvalue = bios_rd32(bios, dstreg) & dstmask;
2044
2045 bios_wr32(bios, dstreg, dstvalue | srcvalue);
2046
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00002047 return 22;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002048}
2049
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00002050static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10002051init_zm_index_io(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
2052{
2053 /*
2054 * INIT_ZM_INDEX_IO opcode: 0x62 ('b')
2055 *
2056 * offset (8 bit): opcode
2057 * offset + 1 (16 bit): CRTC port
2058 * offset + 3 (8 bit): CRTC index
2059 * offset + 4 (8 bit): data
2060 *
2061 * Write "data" to index "CRTC index" of "CRTC port"
2062 */
2063 uint16_t crtcport = ROM16(bios->data[offset + 1]);
2064 uint8_t crtcindex = bios->data[offset + 3];
2065 uint8_t data = bios->data[offset + 4];
2066
2067 if (!iexec->execute)
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00002068 return 5;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002069
2070 bios_idxprt_wr(bios, crtcport, crtcindex, data);
2071
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00002072 return 5;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002073}
2074
Francisco Jerez67eda202010-07-13 15:59:50 +02002075static inline void
2076bios_md32(struct nvbios *bios, uint32_t reg,
2077 uint32_t mask, uint32_t val)
2078{
2079 bios_wr32(bios, reg, (bios_rd32(bios, reg) & ~mask) | val);
2080}
2081
2082static uint32_t
2083peek_fb(struct drm_device *dev, struct io_mapping *fb,
2084 uint32_t off)
2085{
2086 uint32_t val = 0;
2087
2088 if (off < pci_resource_len(dev->pdev, 1)) {
Ben Skeggs625db6b2010-08-17 12:02:43 +10002089 uint8_t __iomem *p =
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -07002090 io_mapping_map_atomic_wc(fb, off & PAGE_MASK);
Francisco Jerez67eda202010-07-13 15:59:50 +02002091
Francisco Jerez0bf9b0e2010-08-04 05:10:57 +02002092 val = ioread32(p + (off & ~PAGE_MASK));
Francisco Jerez67eda202010-07-13 15:59:50 +02002093
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -07002094 io_mapping_unmap_atomic(p);
Francisco Jerez67eda202010-07-13 15:59:50 +02002095 }
2096
2097 return val;
2098}
2099
2100static void
2101poke_fb(struct drm_device *dev, struct io_mapping *fb,
2102 uint32_t off, uint32_t val)
2103{
2104 if (off < pci_resource_len(dev->pdev, 1)) {
Ben Skeggs625db6b2010-08-17 12:02:43 +10002105 uint8_t __iomem *p =
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -07002106 io_mapping_map_atomic_wc(fb, off & PAGE_MASK);
Francisco Jerez67eda202010-07-13 15:59:50 +02002107
Francisco Jerez0bf9b0e2010-08-04 05:10:57 +02002108 iowrite32(val, p + (off & ~PAGE_MASK));
Francisco Jerez67eda202010-07-13 15:59:50 +02002109 wmb();
2110
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -07002111 io_mapping_unmap_atomic(p);
Francisco Jerez67eda202010-07-13 15:59:50 +02002112 }
2113}
2114
2115static inline bool
2116read_back_fb(struct drm_device *dev, struct io_mapping *fb,
2117 uint32_t off, uint32_t val)
2118{
2119 poke_fb(dev, fb, off, val);
2120 return val == peek_fb(dev, fb, off);
2121}
2122
2123static int
2124nv04_init_compute_mem(struct nvbios *bios)
2125{
2126 struct drm_device *dev = bios->dev;
2127 uint32_t patt = 0xdeadbeef;
2128 struct io_mapping *fb;
2129 int i;
2130
2131 /* Map the framebuffer aperture */
2132 fb = io_mapping_create_wc(pci_resource_start(dev->pdev, 1),
2133 pci_resource_len(dev->pdev, 1));
2134 if (!fb)
2135 return -ENOMEM;
2136
2137 /* Sequencer and refresh off */
2138 NVWriteVgaSeq(dev, 0, 1, NVReadVgaSeq(dev, 0, 1) | 0x20);
2139 bios_md32(bios, NV04_PFB_DEBUG_0, 0, NV04_PFB_DEBUG_0_REFRESH_OFF);
2140
2141 bios_md32(bios, NV04_PFB_BOOT_0, ~0,
2142 NV04_PFB_BOOT_0_RAM_AMOUNT_16MB |
2143 NV04_PFB_BOOT_0_RAM_WIDTH_128 |
2144 NV04_PFB_BOOT_0_RAM_TYPE_SGRAM_16MBIT);
2145
2146 for (i = 0; i < 4; i++)
2147 poke_fb(dev, fb, 4 * i, patt);
2148
2149 poke_fb(dev, fb, 0x400000, patt + 1);
2150
2151 if (peek_fb(dev, fb, 0) == patt + 1) {
2152 bios_md32(bios, NV04_PFB_BOOT_0, NV04_PFB_BOOT_0_RAM_TYPE,
2153 NV04_PFB_BOOT_0_RAM_TYPE_SDRAM_16MBIT);
2154 bios_md32(bios, NV04_PFB_DEBUG_0,
2155 NV04_PFB_DEBUG_0_REFRESH_OFF, 0);
2156
2157 for (i = 0; i < 4; i++)
2158 poke_fb(dev, fb, 4 * i, patt);
2159
2160 if ((peek_fb(dev, fb, 0xc) & 0xffff) != (patt & 0xffff))
2161 bios_md32(bios, NV04_PFB_BOOT_0,
2162 NV04_PFB_BOOT_0_RAM_WIDTH_128 |
2163 NV04_PFB_BOOT_0_RAM_AMOUNT,
2164 NV04_PFB_BOOT_0_RAM_AMOUNT_8MB);
2165
2166 } else if ((peek_fb(dev, fb, 0xc) & 0xffff0000) !=
2167 (patt & 0xffff0000)) {
2168 bios_md32(bios, NV04_PFB_BOOT_0,
2169 NV04_PFB_BOOT_0_RAM_WIDTH_128 |
2170 NV04_PFB_BOOT_0_RAM_AMOUNT,
2171 NV04_PFB_BOOT_0_RAM_AMOUNT_4MB);
2172
Francisco Jerez0746b5d2010-08-05 22:58:42 +02002173 } else if (peek_fb(dev, fb, 0) != patt) {
Francisco Jerez67eda202010-07-13 15:59:50 +02002174 if (read_back_fb(dev, fb, 0x800000, patt))
2175 bios_md32(bios, NV04_PFB_BOOT_0,
2176 NV04_PFB_BOOT_0_RAM_AMOUNT,
2177 NV04_PFB_BOOT_0_RAM_AMOUNT_8MB);
2178 else
2179 bios_md32(bios, NV04_PFB_BOOT_0,
2180 NV04_PFB_BOOT_0_RAM_AMOUNT,
2181 NV04_PFB_BOOT_0_RAM_AMOUNT_4MB);
2182
2183 bios_md32(bios, NV04_PFB_BOOT_0, NV04_PFB_BOOT_0_RAM_TYPE,
2184 NV04_PFB_BOOT_0_RAM_TYPE_SGRAM_8MBIT);
2185
2186 } else if (!read_back_fb(dev, fb, 0x800000, patt)) {
2187 bios_md32(bios, NV04_PFB_BOOT_0, NV04_PFB_BOOT_0_RAM_AMOUNT,
2188 NV04_PFB_BOOT_0_RAM_AMOUNT_8MB);
2189
2190 }
2191
2192 /* Refresh on, sequencer on */
2193 bios_md32(bios, NV04_PFB_DEBUG_0, NV04_PFB_DEBUG_0_REFRESH_OFF, 0);
2194 NVWriteVgaSeq(dev, 0, 1, NVReadVgaSeq(dev, 0, 1) & ~0x20);
2195
2196 io_mapping_free(fb);
2197 return 0;
2198}
2199
2200static const uint8_t *
2201nv05_memory_config(struct nvbios *bios)
2202{
2203 /* Defaults for BIOSes lacking a memory config table */
2204 static const uint8_t default_config_tab[][2] = {
2205 { 0x24, 0x00 },
2206 { 0x28, 0x00 },
2207 { 0x24, 0x01 },
2208 { 0x1f, 0x00 },
2209 { 0x0f, 0x00 },
2210 { 0x17, 0x00 },
2211 { 0x06, 0x00 },
2212 { 0x00, 0x00 }
2213 };
2214 int i = (bios_rd32(bios, NV_PEXTDEV_BOOT_0) &
2215 NV_PEXTDEV_BOOT_0_RAMCFG) >> 2;
2216
2217 if (bios->legacy.mem_init_tbl_ptr)
2218 return &bios->data[bios->legacy.mem_init_tbl_ptr + 2 * i];
2219 else
2220 return default_config_tab[i];
2221}
2222
2223static int
2224nv05_init_compute_mem(struct nvbios *bios)
2225{
2226 struct drm_device *dev = bios->dev;
2227 const uint8_t *ramcfg = nv05_memory_config(bios);
2228 uint32_t patt = 0xdeadbeef;
2229 struct io_mapping *fb;
2230 int i, v;
2231
2232 /* Map the framebuffer aperture */
2233 fb = io_mapping_create_wc(pci_resource_start(dev->pdev, 1),
2234 pci_resource_len(dev->pdev, 1));
2235 if (!fb)
2236 return -ENOMEM;
2237
2238 /* Sequencer off */
2239 NVWriteVgaSeq(dev, 0, 1, NVReadVgaSeq(dev, 0, 1) | 0x20);
2240
2241 if (bios_rd32(bios, NV04_PFB_BOOT_0) & NV04_PFB_BOOT_0_UMA_ENABLE)
2242 goto out;
2243
2244 bios_md32(bios, NV04_PFB_DEBUG_0, NV04_PFB_DEBUG_0_REFRESH_OFF, 0);
2245
2246 /* If present load the hardcoded scrambling table */
2247 if (bios->legacy.mem_init_tbl_ptr) {
2248 uint32_t *scramble_tab = (uint32_t *)&bios->data[
2249 bios->legacy.mem_init_tbl_ptr + 0x10];
2250
2251 for (i = 0; i < 8; i++)
2252 bios_wr32(bios, NV04_PFB_SCRAMBLE(i),
2253 ROM32(scramble_tab[i]));
2254 }
2255
2256 /* Set memory type/width/length defaults depending on the straps */
2257 bios_md32(bios, NV04_PFB_BOOT_0, 0x3f, ramcfg[0]);
2258
2259 if (ramcfg[1] & 0x80)
2260 bios_md32(bios, NV04_PFB_CFG0, 0, NV04_PFB_CFG0_SCRAMBLE);
2261
2262 bios_md32(bios, NV04_PFB_CFG1, 0x700001, (ramcfg[1] & 1) << 20);
2263 bios_md32(bios, NV04_PFB_CFG1, 0, 1);
2264
2265 /* Probe memory bus width */
2266 for (i = 0; i < 4; i++)
2267 poke_fb(dev, fb, 4 * i, patt);
2268
2269 if (peek_fb(dev, fb, 0xc) != patt)
2270 bios_md32(bios, NV04_PFB_BOOT_0,
2271 NV04_PFB_BOOT_0_RAM_WIDTH_128, 0);
2272
2273 /* Probe memory length */
2274 v = bios_rd32(bios, NV04_PFB_BOOT_0) & NV04_PFB_BOOT_0_RAM_AMOUNT;
2275
2276 if (v == NV04_PFB_BOOT_0_RAM_AMOUNT_32MB &&
2277 (!read_back_fb(dev, fb, 0x1000000, ++patt) ||
2278 !read_back_fb(dev, fb, 0, ++patt)))
2279 bios_md32(bios, NV04_PFB_BOOT_0, NV04_PFB_BOOT_0_RAM_AMOUNT,
2280 NV04_PFB_BOOT_0_RAM_AMOUNT_16MB);
2281
2282 if (v == NV04_PFB_BOOT_0_RAM_AMOUNT_16MB &&
2283 !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 if (!read_back_fb(dev, fb, 0x400000, ++patt))
2288 bios_md32(bios, NV04_PFB_BOOT_0, NV04_PFB_BOOT_0_RAM_AMOUNT,
2289 NV04_PFB_BOOT_0_RAM_AMOUNT_4MB);
2290
2291out:
2292 /* Sequencer on */
2293 NVWriteVgaSeq(dev, 0, 1, NVReadVgaSeq(dev, 0, 1) & ~0x20);
2294
2295 io_mapping_free(fb);
2296 return 0;
2297}
2298
2299static int
2300nv10_init_compute_mem(struct nvbios *bios)
2301{
2302 struct drm_device *dev = bios->dev;
2303 struct drm_nouveau_private *dev_priv = bios->dev->dev_private;
2304 const int mem_width[] = { 0x10, 0x00, 0x20 };
2305 const int mem_width_count = (dev_priv->chipset >= 0x17 ? 3 : 2);
2306 uint32_t patt = 0xdeadbeef;
2307 struct io_mapping *fb;
2308 int i, j, k;
2309
2310 /* Map the framebuffer aperture */
2311 fb = io_mapping_create_wc(pci_resource_start(dev->pdev, 1),
2312 pci_resource_len(dev->pdev, 1));
2313 if (!fb)
2314 return -ENOMEM;
2315
2316 bios_wr32(bios, NV10_PFB_REFCTRL, NV10_PFB_REFCTRL_VALID_1);
2317
2318 /* Probe memory bus width */
2319 for (i = 0; i < mem_width_count; i++) {
2320 bios_md32(bios, NV04_PFB_CFG0, 0x30, mem_width[i]);
2321
2322 for (j = 0; j < 4; j++) {
2323 for (k = 0; k < 4; k++)
2324 poke_fb(dev, fb, 0x1c, 0);
2325
2326 poke_fb(dev, fb, 0x1c, patt);
2327 poke_fb(dev, fb, 0x3c, 0);
2328
2329 if (peek_fb(dev, fb, 0x1c) == patt)
2330 goto mem_width_found;
2331 }
2332 }
2333
2334mem_width_found:
2335 patt <<= 1;
2336
2337 /* Probe amount of installed memory */
2338 for (i = 0; i < 4; i++) {
2339 int off = bios_rd32(bios, NV04_PFB_FIFO_DATA) - 0x100000;
2340
2341 poke_fb(dev, fb, off, patt);
2342 poke_fb(dev, fb, 0, 0);
2343
2344 peek_fb(dev, fb, 0);
2345 peek_fb(dev, fb, 0);
2346 peek_fb(dev, fb, 0);
2347 peek_fb(dev, fb, 0);
2348
2349 if (peek_fb(dev, fb, off) == patt)
2350 goto amount_found;
2351 }
2352
2353 /* IC missing - disable the upper half memory space. */
2354 bios_md32(bios, NV04_PFB_CFG0, 0x1000, 0);
2355
2356amount_found:
2357 io_mapping_free(fb);
2358 return 0;
2359}
2360
2361static int
2362nv20_init_compute_mem(struct nvbios *bios)
2363{
2364 struct drm_device *dev = bios->dev;
2365 struct drm_nouveau_private *dev_priv = bios->dev->dev_private;
2366 uint32_t mask = (dev_priv->chipset >= 0x25 ? 0x300 : 0x900);
2367 uint32_t amount, off;
2368 struct io_mapping *fb;
2369
2370 /* Map the framebuffer aperture */
2371 fb = io_mapping_create_wc(pci_resource_start(dev->pdev, 1),
2372 pci_resource_len(dev->pdev, 1));
2373 if (!fb)
2374 return -ENOMEM;
2375
2376 bios_wr32(bios, NV10_PFB_REFCTRL, NV10_PFB_REFCTRL_VALID_1);
2377
2378 /* Allow full addressing */
2379 bios_md32(bios, NV04_PFB_CFG0, 0, mask);
2380
2381 amount = bios_rd32(bios, NV04_PFB_FIFO_DATA);
2382 for (off = amount; off > 0x2000000; off -= 0x2000000)
2383 poke_fb(dev, fb, off - 4, off);
2384
2385 amount = bios_rd32(bios, NV04_PFB_FIFO_DATA);
2386 if (amount != peek_fb(dev, fb, amount - 4))
2387 /* IC missing - disable the upper half memory space. */
2388 bios_md32(bios, NV04_PFB_CFG0, mask, 0);
2389
2390 io_mapping_free(fb);
2391 return 0;
2392}
2393
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00002394static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10002395init_compute_mem(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
2396{
2397 /*
2398 * INIT_COMPUTE_MEM opcode: 0x63 ('c')
2399 *
2400 * offset (8 bit): opcode
2401 *
Francisco Jerez67eda202010-07-13 15:59:50 +02002402 * This opcode is meant to set the PFB memory config registers
2403 * appropriately so that we can correctly calculate how much VRAM it
2404 * has (on nv10 and better chipsets the amount of installed VRAM is
2405 * subsequently reported in NV_PFB_CSTATUS (0x10020C)).
Ben Skeggs6ee73862009-12-11 19:24:15 +10002406 *
Francisco Jerez67eda202010-07-13 15:59:50 +02002407 * The implementation of this opcode in general consists of several
2408 * parts:
Ben Skeggs6ee73862009-12-11 19:24:15 +10002409 *
Francisco Jerez67eda202010-07-13 15:59:50 +02002410 * 1) Determination of memory type and density. Only necessary for
2411 * really old chipsets, the memory type reported by the strap bits
2412 * (0x101000) is assumed to be accurate on nv05 and newer.
Ben Skeggs6ee73862009-12-11 19:24:15 +10002413 *
Francisco Jerez67eda202010-07-13 15:59:50 +02002414 * 2) Determination of the memory bus width. Usually done by a cunning
2415 * combination of writes to offsets 0x1c and 0x3c in the fb, and
2416 * seeing whether the written values are read back correctly.
Ben Skeggs6ee73862009-12-11 19:24:15 +10002417 *
Francisco Jerez67eda202010-07-13 15:59:50 +02002418 * Only necessary on nv0x-nv1x and nv34, on the other cards we can
2419 * trust the straps.
Ben Skeggs6ee73862009-12-11 19:24:15 +10002420 *
Francisco Jerez67eda202010-07-13 15:59:50 +02002421 * 3) Determination of how many of the card's RAM pads have ICs
2422 * attached, usually done by a cunning combination of writes to an
2423 * offset slightly less than the maximum memory reported by
2424 * NV_PFB_CSTATUS, then seeing if the test pattern can be read back.
Ben Skeggs6ee73862009-12-11 19:24:15 +10002425 *
Francisco Jerez67eda202010-07-13 15:59:50 +02002426 * This appears to be a NOP on IGPs and NV4x or newer chipsets, both io
2427 * logs of the VBIOS and kmmio traces of the binary driver POSTing the
2428 * card show nothing being done for this opcode. Why is it still listed
2429 * in the table?!
Ben Skeggs6ee73862009-12-11 19:24:15 +10002430 */
2431
2432 /* no iexec->execute check by design */
2433
Ben Skeggs6ee73862009-12-11 19:24:15 +10002434 struct drm_nouveau_private *dev_priv = bios->dev->dev_private;
Francisco Jerez67eda202010-07-13 15:59:50 +02002435 int ret;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002436
Francisco Jerez67eda202010-07-13 15:59:50 +02002437 if (dev_priv->chipset >= 0x40 ||
2438 dev_priv->chipset == 0x1a ||
2439 dev_priv->chipset == 0x1f)
2440 ret = 0;
2441 else if (dev_priv->chipset >= 0x20 &&
2442 dev_priv->chipset != 0x34)
2443 ret = nv20_init_compute_mem(bios);
2444 else if (dev_priv->chipset >= 0x10)
2445 ret = nv10_init_compute_mem(bios);
2446 else if (dev_priv->chipset >= 0x5)
2447 ret = nv05_init_compute_mem(bios);
2448 else
2449 ret = nv04_init_compute_mem(bios);
Ben Skeggs6ee73862009-12-11 19:24:15 +10002450
Francisco Jerez67eda202010-07-13 15:59:50 +02002451 if (ret)
2452 return ret;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002453
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00002454 return 1;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002455}
2456
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00002457static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10002458init_reset(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
2459{
2460 /*
2461 * INIT_RESET opcode: 0x65 ('e')
2462 *
2463 * offset (8 bit): opcode
2464 * offset + 1 (32 bit): register
2465 * offset + 5 (32 bit): value1
2466 * offset + 9 (32 bit): value2
2467 *
2468 * Assign "value1" to "register", then assign "value2" to "register"
2469 */
2470
2471 uint32_t reg = ROM32(bios->data[offset + 1]);
2472 uint32_t value1 = ROM32(bios->data[offset + 5]);
2473 uint32_t value2 = ROM32(bios->data[offset + 9]);
2474 uint32_t pci_nv_19, pci_nv_20;
2475
2476 /* no iexec->execute check by design */
2477
2478 pci_nv_19 = bios_rd32(bios, NV_PBUS_PCI_NV_19);
Francisco Jerez190a4372010-06-17 12:42:14 +02002479 bios_wr32(bios, NV_PBUS_PCI_NV_19, pci_nv_19 & ~0xf00);
2480
Ben Skeggs6ee73862009-12-11 19:24:15 +10002481 bios_wr32(bios, reg, value1);
2482
2483 udelay(10);
2484
2485 bios_wr32(bios, reg, value2);
2486 bios_wr32(bios, NV_PBUS_PCI_NV_19, pci_nv_19);
2487
2488 pci_nv_20 = bios_rd32(bios, NV_PBUS_PCI_NV_20);
2489 pci_nv_20 &= ~NV_PBUS_PCI_NV_20_ROM_SHADOW_ENABLED; /* 0xfffffffe */
2490 bios_wr32(bios, NV_PBUS_PCI_NV_20, pci_nv_20);
2491
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00002492 return 13;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002493}
2494
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00002495static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10002496init_configure_mem(struct nvbios *bios, uint16_t offset,
2497 struct init_exec *iexec)
2498{
2499 /*
2500 * INIT_CONFIGURE_MEM opcode: 0x66 ('f')
2501 *
2502 * offset (8 bit): opcode
2503 *
2504 * Equivalent to INIT_DONE on bios version 3 or greater.
2505 * For early bios versions, sets up the memory registers, using values
2506 * taken from the memory init table
2507 */
2508
2509 /* no iexec->execute check by design */
2510
2511 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);
2512 uint16_t seqtbloffs = bios->legacy.sdr_seq_tbl_ptr, meminitdata = meminitoffs + 6;
2513 uint32_t reg, data;
2514
2515 if (bios->major_version > 2)
Francisco Jerezae553212010-07-03 20:47:44 +02002516 return 0;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002517
2518 bios_idxprt_wr(bios, NV_VIO_SRX, NV_VIO_SR_CLOCK_INDEX, bios_idxprt_rd(
2519 bios, NV_VIO_SRX, NV_VIO_SR_CLOCK_INDEX) | 0x20);
2520
2521 if (bios->data[meminitoffs] & 1)
2522 seqtbloffs = bios->legacy.ddr_seq_tbl_ptr;
2523
2524 for (reg = ROM32(bios->data[seqtbloffs]);
2525 reg != 0xffffffff;
2526 reg = ROM32(bios->data[seqtbloffs += 4])) {
2527
2528 switch (reg) {
Francisco Jerez3c7066b2010-07-13 15:50:23 +02002529 case NV04_PFB_PRE:
2530 data = NV04_PFB_PRE_CMD_PRECHARGE;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002531 break;
Francisco Jerez3c7066b2010-07-13 15:50:23 +02002532 case NV04_PFB_PAD:
2533 data = NV04_PFB_PAD_CKE_NORMAL;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002534 break;
Francisco Jerez3c7066b2010-07-13 15:50:23 +02002535 case NV04_PFB_REF:
2536 data = NV04_PFB_REF_CMD_REFRESH;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002537 break;
2538 default:
2539 data = ROM32(bios->data[meminitdata]);
2540 meminitdata += 4;
2541 if (data == 0xffffffff)
2542 continue;
2543 }
2544
2545 bios_wr32(bios, reg, data);
2546 }
2547
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00002548 return 1;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002549}
2550
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00002551static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10002552init_configure_clk(struct nvbios *bios, uint16_t offset,
2553 struct init_exec *iexec)
2554{
2555 /*
2556 * INIT_CONFIGURE_CLK opcode: 0x67 ('g')
2557 *
2558 * offset (8 bit): opcode
2559 *
2560 * Equivalent to INIT_DONE on bios version 3 or greater.
2561 * For early bios versions, sets up the NVClk and MClk PLLs, using
2562 * values taken from the memory init table
2563 */
2564
2565 /* no iexec->execute check by design */
2566
2567 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);
2568 int clock;
2569
2570 if (bios->major_version > 2)
Francisco Jerezae553212010-07-03 20:47:44 +02002571 return 0;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002572
2573 clock = ROM16(bios->data[meminitoffs + 4]) * 10;
2574 setPLL(bios, NV_PRAMDAC_NVPLL_COEFF, clock);
2575
2576 clock = ROM16(bios->data[meminitoffs + 2]) * 10;
2577 if (bios->data[meminitoffs] & 1) /* DDR */
2578 clock *= 2;
2579 setPLL(bios, NV_PRAMDAC_MPLL_COEFF, clock);
2580
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00002581 return 1;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002582}
2583
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00002584static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10002585init_configure_preinit(struct nvbios *bios, uint16_t offset,
2586 struct init_exec *iexec)
2587{
2588 /*
2589 * INIT_CONFIGURE_PREINIT opcode: 0x68 ('h')
2590 *
2591 * offset (8 bit): opcode
2592 *
2593 * Equivalent to INIT_DONE on bios version 3 or greater.
2594 * For early bios versions, does early init, loading ram and crystal
2595 * configuration from straps into CR3C
2596 */
2597
2598 /* no iexec->execute check by design */
2599
2600 uint32_t straps = bios_rd32(bios, NV_PEXTDEV_BOOT_0);
Francisco Jerez3c9b2532010-08-04 05:15:11 +02002601 uint8_t cr3c = ((straps << 2) & 0xf0) | (straps & 0x40) >> 6;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002602
2603 if (bios->major_version > 2)
Francisco Jerezae553212010-07-03 20:47:44 +02002604 return 0;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002605
2606 bios_idxprt_wr(bios, NV_CIO_CRX__COLOR,
2607 NV_CIO_CRE_SCRATCH4__INDEX, cr3c);
2608
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00002609 return 1;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002610}
2611
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00002612static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10002613init_io(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
2614{
2615 /*
2616 * INIT_IO opcode: 0x69 ('i')
2617 *
2618 * offset (8 bit): opcode
2619 * offset + 1 (16 bit): CRTC port
2620 * offset + 3 (8 bit): mask
2621 * offset + 4 (8 bit): data
2622 *
2623 * Assign ((IOVAL("crtc port") & "mask") | "data") to "crtc port"
2624 */
2625
2626 struct drm_nouveau_private *dev_priv = bios->dev->dev_private;
2627 uint16_t crtcport = ROM16(bios->data[offset + 1]);
2628 uint8_t mask = bios->data[offset + 3];
2629 uint8_t data = bios->data[offset + 4];
2630
2631 if (!iexec->execute)
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00002632 return 5;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002633
2634 BIOSLOG(bios, "0x%04X: Port: 0x%04X, Mask: 0x%02X, Data: 0x%02X\n",
2635 offset, crtcport, mask, data);
2636
2637 /*
2638 * I have no idea what this does, but NVIDIA do this magic sequence
2639 * in the places where this INIT_IO happens..
2640 */
2641 if (dev_priv->card_type >= NV_50 && crtcport == 0x3c3 && data == 1) {
2642 int i;
2643
2644 bios_wr32(bios, 0x614100, (bios_rd32(
2645 bios, 0x614100) & 0x0fffffff) | 0x00800000);
2646
2647 bios_wr32(bios, 0x00e18c, bios_rd32(
2648 bios, 0x00e18c) | 0x00020000);
2649
2650 bios_wr32(bios, 0x614900, (bios_rd32(
2651 bios, 0x614900) & 0x0fffffff) | 0x00800000);
2652
2653 bios_wr32(bios, 0x000200, bios_rd32(
2654 bios, 0x000200) & ~0x40000000);
2655
2656 mdelay(10);
2657
2658 bios_wr32(bios, 0x00e18c, bios_rd32(
2659 bios, 0x00e18c) & ~0x00020000);
2660
2661 bios_wr32(bios, 0x000200, bios_rd32(
2662 bios, 0x000200) | 0x40000000);
2663
2664 bios_wr32(bios, 0x614100, 0x00800018);
2665 bios_wr32(bios, 0x614900, 0x00800018);
2666
2667 mdelay(10);
2668
2669 bios_wr32(bios, 0x614100, 0x10000018);
2670 bios_wr32(bios, 0x614900, 0x10000018);
2671
2672 for (i = 0; i < 3; i++)
2673 bios_wr32(bios, 0x614280 + (i*0x800), bios_rd32(
2674 bios, 0x614280 + (i*0x800)) & 0xf0f0f0f0);
2675
2676 for (i = 0; i < 2; i++)
2677 bios_wr32(bios, 0x614300 + (i*0x800), bios_rd32(
2678 bios, 0x614300 + (i*0x800)) & 0xfffff0f0);
2679
2680 for (i = 0; i < 3; i++)
2681 bios_wr32(bios, 0x614380 + (i*0x800), bios_rd32(
2682 bios, 0x614380 + (i*0x800)) & 0xfffff0f0);
2683
2684 for (i = 0; i < 2; i++)
2685 bios_wr32(bios, 0x614200 + (i*0x800), bios_rd32(
2686 bios, 0x614200 + (i*0x800)) & 0xfffffff0);
2687
2688 for (i = 0; i < 2; i++)
2689 bios_wr32(bios, 0x614108 + (i*0x800), bios_rd32(
2690 bios, 0x614108 + (i*0x800)) & 0x0fffffff);
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00002691 return 5;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002692 }
2693
2694 bios_port_wr(bios, crtcport, (bios_port_rd(bios, crtcport) & mask) |
2695 data);
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00002696 return 5;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002697}
2698
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00002699static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10002700init_sub(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
2701{
2702 /*
2703 * INIT_SUB opcode: 0x6B ('k')
2704 *
2705 * offset (8 bit): opcode
2706 * offset + 1 (8 bit): script number
2707 *
2708 * Execute script number "script number", as a subroutine
2709 */
2710
2711 uint8_t sub = bios->data[offset + 1];
2712
2713 if (!iexec->execute)
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00002714 return 2;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002715
2716 BIOSLOG(bios, "0x%04X: Calling script %d\n", offset, sub);
2717
2718 parse_init_table(bios,
2719 ROM16(bios->data[bios->init_script_tbls_ptr + sub * 2]),
2720 iexec);
2721
2722 BIOSLOG(bios, "0x%04X: End of script %d\n", offset, sub);
2723
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00002724 return 2;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002725}
2726
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00002727static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10002728init_ram_condition(struct nvbios *bios, uint16_t offset,
2729 struct init_exec *iexec)
2730{
2731 /*
2732 * INIT_RAM_CONDITION opcode: 0x6D ('m')
2733 *
2734 * offset (8 bit): opcode
2735 * offset + 1 (8 bit): mask
2736 * offset + 2 (8 bit): cmpval
2737 *
Francisco Jerez3c7066b2010-07-13 15:50:23 +02002738 * Test if (NV04_PFB_BOOT_0 & "mask") equals "cmpval".
Ben Skeggs6ee73862009-12-11 19:24:15 +10002739 * If condition not met skip subsequent opcodes until condition is
2740 * inverted (INIT_NOT), or we hit INIT_RESUME
2741 */
2742
2743 uint8_t mask = bios->data[offset + 1];
2744 uint8_t cmpval = bios->data[offset + 2];
2745 uint8_t data;
2746
2747 if (!iexec->execute)
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00002748 return 3;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002749
Francisco Jerez3c7066b2010-07-13 15:50:23 +02002750 data = bios_rd32(bios, NV04_PFB_BOOT_0) & mask;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002751
2752 BIOSLOG(bios, "0x%04X: Checking if 0x%08X equals 0x%08X\n",
2753 offset, data, cmpval);
2754
2755 if (data == cmpval)
2756 BIOSLOG(bios, "0x%04X: Condition fulfilled -- continuing to execute\n", offset);
2757 else {
2758 BIOSLOG(bios, "0x%04X: Condition not fulfilled -- skipping following commands\n", offset);
2759 iexec->execute = false;
2760 }
2761
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00002762 return 3;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002763}
2764
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00002765static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10002766init_nv_reg(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
2767{
2768 /*
2769 * INIT_NV_REG opcode: 0x6E ('n')
2770 *
2771 * offset (8 bit): opcode
2772 * offset + 1 (32 bit): register
2773 * offset + 5 (32 bit): mask
2774 * offset + 9 (32 bit): data
2775 *
2776 * Assign ((REGVAL("register") & "mask") | "data") to "register"
2777 */
2778
2779 uint32_t reg = ROM32(bios->data[offset + 1]);
2780 uint32_t mask = ROM32(bios->data[offset + 5]);
2781 uint32_t data = ROM32(bios->data[offset + 9]);
2782
2783 if (!iexec->execute)
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00002784 return 13;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002785
2786 BIOSLOG(bios, "0x%04X: Reg: 0x%08X, Mask: 0x%08X, Data: 0x%08X\n",
2787 offset, reg, mask, data);
2788
2789 bios_wr32(bios, reg, (bios_rd32(bios, reg) & mask) | data);
2790
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00002791 return 13;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002792}
2793
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00002794static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10002795init_macro(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
2796{
2797 /*
2798 * INIT_MACRO opcode: 0x6F ('o')
2799 *
2800 * offset (8 bit): opcode
2801 * offset + 1 (8 bit): macro number
2802 *
2803 * Look up macro index "macro number" in the macro index table.
2804 * The macro index table entry has 1 byte for the index in the macro
2805 * table, and 1 byte for the number of times to repeat the macro.
2806 * The macro table entry has 4 bytes for the register address and
2807 * 4 bytes for the value to write to that register
2808 */
2809
2810 uint8_t macro_index_tbl_idx = bios->data[offset + 1];
2811 uint16_t tmp = bios->macro_index_tbl_ptr + (macro_index_tbl_idx * MACRO_INDEX_SIZE);
2812 uint8_t macro_tbl_idx = bios->data[tmp];
2813 uint8_t count = bios->data[tmp + 1];
2814 uint32_t reg, data;
2815 int i;
2816
2817 if (!iexec->execute)
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00002818 return 2;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002819
2820 BIOSLOG(bios, "0x%04X: Macro: 0x%02X, MacroTableIndex: 0x%02X, "
2821 "Count: 0x%02X\n",
2822 offset, macro_index_tbl_idx, macro_tbl_idx, count);
2823
2824 for (i = 0; i < count; i++) {
2825 uint16_t macroentryptr = bios->macro_tbl_ptr + (macro_tbl_idx + i) * MACRO_SIZE;
2826
2827 reg = ROM32(bios->data[macroentryptr]);
2828 data = ROM32(bios->data[macroentryptr + 4]);
2829
2830 bios_wr32(bios, reg, data);
2831 }
2832
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00002833 return 2;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002834}
2835
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00002836static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10002837init_done(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
2838{
2839 /*
2840 * INIT_DONE opcode: 0x71 ('q')
2841 *
2842 * offset (8 bit): opcode
2843 *
2844 * End the current script
2845 */
2846
2847 /* mild retval abuse to stop parsing this table */
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00002848 return 0;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002849}
2850
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00002851static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10002852init_resume(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
2853{
2854 /*
2855 * INIT_RESUME opcode: 0x72 ('r')
2856 *
2857 * offset (8 bit): opcode
2858 *
2859 * End the current execute / no-execute condition
2860 */
2861
2862 if (iexec->execute)
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00002863 return 1;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002864
2865 iexec->execute = true;
2866 BIOSLOG(bios, "0x%04X: ---- Executing following commands ----\n", offset);
2867
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00002868 return 1;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002869}
2870
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00002871static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10002872init_time(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
2873{
2874 /*
2875 * INIT_TIME opcode: 0x74 ('t')
2876 *
2877 * offset (8 bit): opcode
2878 * offset + 1 (16 bit): time
2879 *
2880 * Sleep for "time" microseconds.
2881 */
2882
2883 unsigned time = ROM16(bios->data[offset + 1]);
2884
2885 if (!iexec->execute)
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00002886 return 3;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002887
2888 BIOSLOG(bios, "0x%04X: Sleeping for 0x%04X microseconds\n",
2889 offset, time);
2890
2891 if (time < 1000)
2892 udelay(time);
2893 else
Ben Skeggsc7ca4d12011-02-03 20:10:49 +10002894 mdelay((time + 900) / 1000);
Ben Skeggs6ee73862009-12-11 19:24:15 +10002895
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00002896 return 3;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002897}
2898
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00002899static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10002900init_condition(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
2901{
2902 /*
2903 * INIT_CONDITION opcode: 0x75 ('u')
2904 *
2905 * offset (8 bit): opcode
2906 * offset + 1 (8 bit): condition number
2907 *
2908 * Check condition "condition number" in the condition table.
2909 * If condition not met skip subsequent opcodes until condition is
2910 * inverted (INIT_NOT), or we hit INIT_RESUME
2911 */
2912
2913 uint8_t cond = bios->data[offset + 1];
2914
2915 if (!iexec->execute)
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00002916 return 2;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002917
2918 BIOSLOG(bios, "0x%04X: Condition: 0x%02X\n", offset, cond);
2919
2920 if (bios_condition_met(bios, offset, cond))
2921 BIOSLOG(bios, "0x%04X: Condition fulfilled -- continuing to execute\n", offset);
2922 else {
2923 BIOSLOG(bios, "0x%04X: Condition not fulfilled -- skipping following commands\n", offset);
2924 iexec->execute = false;
2925 }
2926
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00002927 return 2;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002928}
2929
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00002930static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10002931init_io_condition(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
2932{
2933 /*
2934 * INIT_IO_CONDITION opcode: 0x76
2935 *
2936 * offset (8 bit): opcode
2937 * offset + 1 (8 bit): condition number
2938 *
2939 * Check condition "condition number" in the io condition table.
2940 * If condition not met skip subsequent opcodes until condition is
2941 * inverted (INIT_NOT), or we hit INIT_RESUME
2942 */
2943
2944 uint8_t cond = bios->data[offset + 1];
2945
2946 if (!iexec->execute)
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00002947 return 2;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002948
2949 BIOSLOG(bios, "0x%04X: IO condition: 0x%02X\n", offset, cond);
2950
2951 if (io_condition_met(bios, offset, cond))
2952 BIOSLOG(bios, "0x%04X: Condition fulfilled -- continuing to execute\n", offset);
2953 else {
2954 BIOSLOG(bios, "0x%04X: Condition not fulfilled -- skipping following commands\n", offset);
2955 iexec->execute = false;
2956 }
2957
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00002958 return 2;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002959}
2960
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00002961static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10002962init_index_io(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
2963{
2964 /*
2965 * INIT_INDEX_IO opcode: 0x78 ('x')
2966 *
2967 * offset (8 bit): opcode
2968 * offset + 1 (16 bit): CRTC port
2969 * offset + 3 (8 bit): CRTC index
2970 * offset + 4 (8 bit): mask
2971 * offset + 5 (8 bit): data
2972 *
2973 * Read value at index "CRTC index" on "CRTC port", AND with "mask",
2974 * OR with "data", write-back
2975 */
2976
2977 uint16_t crtcport = ROM16(bios->data[offset + 1]);
2978 uint8_t crtcindex = bios->data[offset + 3];
2979 uint8_t mask = bios->data[offset + 4];
2980 uint8_t data = bios->data[offset + 5];
2981 uint8_t value;
2982
2983 if (!iexec->execute)
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00002984 return 6;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002985
2986 BIOSLOG(bios, "0x%04X: Port: 0x%04X, Index: 0x%02X, Mask: 0x%02X, "
2987 "Data: 0x%02X\n",
2988 offset, crtcport, crtcindex, mask, data);
2989
2990 value = (bios_idxprt_rd(bios, crtcport, crtcindex) & mask) | data;
2991 bios_idxprt_wr(bios, crtcport, crtcindex, value);
2992
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00002993 return 6;
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_pll(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
2998{
2999 /*
3000 * INIT_PLL opcode: 0x79 ('y')
3001 *
3002 * offset (8 bit): opcode
3003 * offset + 1 (32 bit): register
3004 * offset + 5 (16 bit): freq
3005 *
3006 * Set PLL register "register" to coefficients for frequency (10kHz)
3007 * "freq"
3008 */
3009
3010 uint32_t reg = ROM32(bios->data[offset + 1]);
3011 uint16_t freq = ROM16(bios->data[offset + 5]);
3012
3013 if (!iexec->execute)
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00003014 return 7;
Ben Skeggs6ee73862009-12-11 19:24:15 +10003015
3016 BIOSLOG(bios, "0x%04X: Reg: 0x%08X, Freq: %d0kHz\n", offset, reg, freq);
3017
3018 setPLL(bios, reg, freq * 10);
3019
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00003020 return 7;
Ben Skeggs6ee73862009-12-11 19:24:15 +10003021}
3022
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00003023static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10003024init_zm_reg(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
3025{
3026 /*
3027 * INIT_ZM_REG opcode: 0x7A ('z')
3028 *
3029 * offset (8 bit): opcode
3030 * offset + 1 (32 bit): register
3031 * offset + 5 (32 bit): value
3032 *
3033 * Assign "value" to "register"
3034 */
3035
3036 uint32_t reg = ROM32(bios->data[offset + 1]);
3037 uint32_t value = ROM32(bios->data[offset + 5]);
3038
3039 if (!iexec->execute)
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00003040 return 9;
Ben Skeggs6ee73862009-12-11 19:24:15 +10003041
3042 if (reg == 0x000200)
3043 value |= 1;
3044
3045 bios_wr32(bios, reg, value);
3046
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00003047 return 9;
Ben Skeggs6ee73862009-12-11 19:24:15 +10003048}
3049
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00003050static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10003051init_ram_restrict_pll(struct nvbios *bios, uint16_t offset,
3052 struct init_exec *iexec)
3053{
3054 /*
3055 * INIT_RAM_RESTRICT_PLL opcode: 0x87 ('')
3056 *
3057 * offset (8 bit): opcode
3058 * offset + 1 (8 bit): PLL type
3059 * offset + 2 (32 bit): frequency 0
3060 *
3061 * Uses the RAMCFG strap of PEXTDEV_BOOT as an index into the table at
3062 * ram_restrict_table_ptr. The value read from there is used to select
3063 * a frequency from the table starting at 'frequency 0' to be
3064 * programmed into the PLL corresponding to 'type'.
3065 *
3066 * The PLL limits table on cards using this opcode has a mapping of
3067 * 'type' to the relevant registers.
3068 */
3069
3070 struct drm_device *dev = bios->dev;
3071 uint32_t strap = (bios_rd32(bios, NV_PEXTDEV_BOOT_0) & 0x0000003c) >> 2;
3072 uint8_t index = bios->data[bios->ram_restrict_tbl_ptr + strap];
3073 uint8_t type = bios->data[offset + 1];
3074 uint32_t freq = ROM32(bios->data[offset + 2 + (index * 4)]);
3075 uint8_t *pll_limits = &bios->data[bios->pll_limit_tbl_ptr], *entry;
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00003076 int len = 2 + bios->ram_restrict_group_count * 4;
Ben Skeggs6ee73862009-12-11 19:24:15 +10003077 int i;
3078
3079 if (!iexec->execute)
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00003080 return len;
Ben Skeggs6ee73862009-12-11 19:24:15 +10003081
3082 if (!bios->pll_limit_tbl_ptr || (pll_limits[0] & 0xf0) != 0x30) {
3083 NV_ERROR(dev, "PLL limits table not version 3.x\n");
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00003084 return len; /* deliberate, allow default clocks to remain */
Ben Skeggs6ee73862009-12-11 19:24:15 +10003085 }
3086
3087 entry = pll_limits + pll_limits[1];
3088 for (i = 0; i < pll_limits[3]; i++, entry += pll_limits[2]) {
3089 if (entry[0] == type) {
3090 uint32_t reg = ROM32(entry[3]);
3091
3092 BIOSLOG(bios, "0x%04X: "
3093 "Type %02x Reg 0x%08x Freq %dKHz\n",
3094 offset, type, reg, freq);
3095
3096 setPLL(bios, reg, freq);
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00003097 return len;
Ben Skeggs6ee73862009-12-11 19:24:15 +10003098 }
3099 }
3100
3101 NV_ERROR(dev, "PLL type 0x%02x not found in PLL limits table", type);
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00003102 return len;
Ben Skeggs6ee73862009-12-11 19:24:15 +10003103}
3104
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00003105static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10003106init_8c(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
3107{
3108 /*
3109 * INIT_8C opcode: 0x8C ('')
3110 *
3111 * NOP so far....
3112 *
3113 */
3114
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00003115 return 1;
Ben Skeggs6ee73862009-12-11 19:24:15 +10003116}
3117
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00003118static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10003119init_8d(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
3120{
3121 /*
3122 * INIT_8D opcode: 0x8D ('')
3123 *
3124 * NOP so far....
3125 *
3126 */
3127
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00003128 return 1;
Ben Skeggs6ee73862009-12-11 19:24:15 +10003129}
3130
Ben Skeggs75139062011-07-03 13:40:01 +10003131static void
3132init_gpio_unknv50(struct nvbios *bios, struct dcb_gpio_entry *gpio)
3133{
3134 const uint32_t nv50_gpio_ctl[2] = { 0xe100, 0xe28c };
3135 u32 r, s, v;
3136
3137 /* Not a clue, needs de-magicing */
3138 r = nv50_gpio_ctl[gpio->line >> 4];
3139 s = (gpio->line & 0x0f);
3140 v = bios_rd32(bios, r) & ~(0x00010001 << s);
3141 switch ((gpio->entry & 0x06000000) >> 25) {
3142 case 1:
3143 v |= (0x00000001 << s);
3144 break;
3145 case 2:
3146 v |= (0x00010000 << s);
3147 break;
3148 default:
3149 break;
3150 }
3151
3152 bios_wr32(bios, r, v);
3153}
3154
3155static void
3156init_gpio_unknvd0(struct nvbios *bios, struct dcb_gpio_entry *gpio)
3157{
3158 u32 v, i;
3159
3160 v = bios_rd32(bios, 0x00d610 + (gpio->line * 4));
3161 v &= 0xffffff00;
3162 v |= (gpio->entry & 0x00ff0000) >> 16;
3163 bios_wr32(bios, 0x00d610 + (gpio->line * 4), v);
3164
3165 i = (gpio->entry & 0x1f000000) >> 24;
3166 if (i) {
3167 v = bios_rd32(bios, 0x00d640 + ((i - 1) * 4));
3168 v &= 0xffffff00;
3169 v |= gpio->line;
3170 bios_wr32(bios, 0x00d640 + ((i - 1) * 4), v);
3171 }
3172}
3173
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00003174static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10003175init_gpio(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
3176{
3177 /*
3178 * INIT_GPIO opcode: 0x8E ('')
3179 *
3180 * offset (8 bit): opcode
3181 *
3182 * Loop over all entries in the DCB GPIO table, and initialise
3183 * each GPIO according to various values listed in each entry
3184 */
3185
Ben Skeggs2535d712010-04-07 12:00:14 +10003186 struct drm_nouveau_private *dev_priv = bios->dev->dev_private;
Ben Skeggsee2e0132010-07-26 09:28:25 +10003187 struct nouveau_gpio_engine *pgpio = &dev_priv->engine.gpio;
Ben Skeggs6ee73862009-12-11 19:24:15 +10003188 int i;
3189
Ben Skeggs080feda2010-08-04 13:40:50 +10003190 if (dev_priv->card_type < NV_50) {
Ben Skeggs2535d712010-04-07 12:00:14 +10003191 NV_ERROR(bios->dev, "INIT_GPIO on unsupported chipset\n");
Ben Skeggs309b8c82010-06-29 16:09:24 +10003192 return 1;
Ben Skeggs2535d712010-04-07 12:00:14 +10003193 }
3194
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00003195 if (!iexec->execute)
3196 return 1;
3197
Ben Skeggs2535d712010-04-07 12:00:14 +10003198 for (i = 0; i < bios->dcb.gpio.entries; i++) {
3199 struct dcb_gpio_entry *gpio = &bios->dcb.gpio.entry[i];
Ben Skeggs6ee73862009-12-11 19:24:15 +10003200
Ben Skeggs2535d712010-04-07 12:00:14 +10003201 BIOSLOG(bios, "0x%04X: Entry: 0x%08X\n", offset, gpio->entry);
Ben Skeggs6ee73862009-12-11 19:24:15 +10003202
Ben Skeggs73db4be2010-05-26 10:41:45 +10003203 BIOSLOG(bios, "0x%04X: set gpio 0x%02x, state %d\n",
3204 offset, gpio->tag, gpio->state_default);
Ben Skeggs6ee73862009-12-11 19:24:15 +10003205
Ben Skeggs75139062011-07-03 13:40:01 +10003206 if (!bios->execute)
3207 continue;
3208
3209 pgpio->set(bios->dev, gpio->tag, gpio->state_default);
3210 if (dev_priv->card_type < NV_D0)
3211 init_gpio_unknv50(bios, gpio);
3212 else
3213 init_gpio_unknvd0(bios, gpio);
Ben Skeggs6ee73862009-12-11 19:24:15 +10003214 }
3215
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00003216 return 1;
Ben Skeggs6ee73862009-12-11 19:24:15 +10003217}
3218
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00003219static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10003220init_ram_restrict_zm_reg_group(struct nvbios *bios, uint16_t offset,
3221 struct init_exec *iexec)
3222{
3223 /*
3224 * INIT_RAM_RESTRICT_ZM_REG_GROUP opcode: 0x8F ('')
3225 *
3226 * offset (8 bit): opcode
3227 * offset + 1 (32 bit): reg
3228 * offset + 5 (8 bit): regincrement
3229 * offset + 6 (8 bit): count
3230 * offset + 7 (32 bit): value 1,1
3231 * ...
3232 *
3233 * Use the RAMCFG strap of PEXTDEV_BOOT as an index into the table at
3234 * ram_restrict_table_ptr. The value read from here is 'n', and
3235 * "value 1,n" gets written to "reg". This repeats "count" times and on
3236 * each iteration 'm', "reg" increases by "regincrement" and
3237 * "value m,n" is used. The extent of n is limited by a number read
3238 * from the 'M' BIT table, herein called "blocklen"
3239 */
3240
3241 uint32_t reg = ROM32(bios->data[offset + 1]);
3242 uint8_t regincrement = bios->data[offset + 5];
3243 uint8_t count = bios->data[offset + 6];
3244 uint32_t strap_ramcfg, data;
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00003245 /* previously set by 'M' BIT table */
3246 uint16_t blocklen = bios->ram_restrict_group_count * 4;
3247 int len = 7 + count * blocklen;
Ben Skeggs6ee73862009-12-11 19:24:15 +10003248 uint8_t index;
3249 int i;
3250
Ben Skeggs309b8c82010-06-29 16:09:24 +10003251 /* critical! to know the length of the opcode */;
Ben Skeggs6ee73862009-12-11 19:24:15 +10003252 if (!blocklen) {
3253 NV_ERROR(bios->dev,
3254 "0x%04X: Zero block length - has the M table "
3255 "been parsed?\n", offset);
Ben Skeggs9170a822010-05-10 16:54:23 +10003256 return -EINVAL;
Ben Skeggs6ee73862009-12-11 19:24:15 +10003257 }
3258
Ben Skeggs309b8c82010-06-29 16:09:24 +10003259 if (!iexec->execute)
3260 return len;
3261
Ben Skeggs6ee73862009-12-11 19:24:15 +10003262 strap_ramcfg = (bios_rd32(bios, NV_PEXTDEV_BOOT_0) >> 2) & 0xf;
3263 index = bios->data[bios->ram_restrict_tbl_ptr + strap_ramcfg];
3264
3265 BIOSLOG(bios, "0x%04X: Reg: 0x%08X, RegIncrement: 0x%02X, "
3266 "Count: 0x%02X, StrapRamCfg: 0x%02X, Index: 0x%02X\n",
3267 offset, reg, regincrement, count, strap_ramcfg, index);
3268
3269 for (i = 0; i < count; i++) {
3270 data = ROM32(bios->data[offset + 7 + index * 4 + blocklen * i]);
3271
3272 bios_wr32(bios, reg, data);
3273
3274 reg += regincrement;
3275 }
3276
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00003277 return len;
Ben Skeggs6ee73862009-12-11 19:24:15 +10003278}
3279
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00003280static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10003281init_copy_zm_reg(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
3282{
3283 /*
3284 * INIT_COPY_ZM_REG opcode: 0x90 ('')
3285 *
3286 * offset (8 bit): opcode
3287 * offset + 1 (32 bit): src reg
3288 * offset + 5 (32 bit): dst reg
3289 *
3290 * Put contents of "src reg" into "dst reg"
3291 */
3292
3293 uint32_t srcreg = ROM32(bios->data[offset + 1]);
3294 uint32_t dstreg = ROM32(bios->data[offset + 5]);
3295
3296 if (!iexec->execute)
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00003297 return 9;
Ben Skeggs6ee73862009-12-11 19:24:15 +10003298
3299 bios_wr32(bios, dstreg, bios_rd32(bios, srcreg));
3300
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00003301 return 9;
Ben Skeggs6ee73862009-12-11 19:24:15 +10003302}
3303
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00003304static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10003305init_zm_reg_group_addr_latched(struct nvbios *bios, uint16_t offset,
3306 struct init_exec *iexec)
3307{
3308 /*
3309 * INIT_ZM_REG_GROUP_ADDRESS_LATCHED opcode: 0x91 ('')
3310 *
3311 * offset (8 bit): opcode
3312 * offset + 1 (32 bit): dst reg
3313 * offset + 5 (8 bit): count
3314 * offset + 6 (32 bit): data 1
3315 * ...
3316 *
3317 * For each of "count" values write "data n" to "dst reg"
3318 */
3319
3320 uint32_t reg = ROM32(bios->data[offset + 1]);
3321 uint8_t count = bios->data[offset + 5];
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00003322 int len = 6 + count * 4;
Ben Skeggs6ee73862009-12-11 19:24:15 +10003323 int i;
3324
3325 if (!iexec->execute)
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00003326 return len;
Ben Skeggs6ee73862009-12-11 19:24:15 +10003327
3328 for (i = 0; i < count; i++) {
3329 uint32_t data = ROM32(bios->data[offset + 6 + 4 * i]);
3330 bios_wr32(bios, reg, data);
3331 }
3332
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00003333 return len;
Ben Skeggs6ee73862009-12-11 19:24:15 +10003334}
3335
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00003336static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10003337init_reserved(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
3338{
3339 /*
3340 * INIT_RESERVED opcode: 0x92 ('')
3341 *
3342 * offset (8 bit): opcode
3343 *
3344 * Seemingly does nothing
3345 */
3346
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00003347 return 1;
Ben Skeggs6ee73862009-12-11 19:24:15 +10003348}
3349
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00003350static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10003351init_96(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
3352{
3353 /*
3354 * INIT_96 opcode: 0x96 ('')
3355 *
3356 * offset (8 bit): opcode
3357 * offset + 1 (32 bit): sreg
3358 * offset + 5 (8 bit): sshift
3359 * offset + 6 (8 bit): smask
3360 * offset + 7 (8 bit): index
3361 * offset + 8 (32 bit): reg
3362 * offset + 12 (32 bit): mask
3363 * offset + 16 (8 bit): shift
3364 *
3365 */
3366
3367 uint16_t xlatptr = bios->init96_tbl_ptr + (bios->data[offset + 7] * 2);
3368 uint32_t reg = ROM32(bios->data[offset + 8]);
3369 uint32_t mask = ROM32(bios->data[offset + 12]);
3370 uint32_t val;
3371
3372 val = bios_rd32(bios, ROM32(bios->data[offset + 1]));
3373 if (bios->data[offset + 5] < 0x80)
3374 val >>= bios->data[offset + 5];
3375 else
3376 val <<= (0x100 - bios->data[offset + 5]);
3377 val &= bios->data[offset + 6];
3378
3379 val = bios->data[ROM16(bios->data[xlatptr]) + val];
3380 val <<= bios->data[offset + 16];
3381
3382 if (!iexec->execute)
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00003383 return 17;
Ben Skeggs6ee73862009-12-11 19:24:15 +10003384
3385 bios_wr32(bios, reg, (bios_rd32(bios, reg) & mask) | val);
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00003386 return 17;
Ben Skeggs6ee73862009-12-11 19:24:15 +10003387}
3388
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00003389static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10003390init_97(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
3391{
3392 /*
3393 * INIT_97 opcode: 0x97 ('')
3394 *
3395 * offset (8 bit): opcode
3396 * offset + 1 (32 bit): register
3397 * offset + 5 (32 bit): mask
3398 * offset + 9 (32 bit): value
3399 *
3400 * Adds "value" to "register" preserving the fields specified
3401 * by "mask"
3402 */
3403
3404 uint32_t reg = ROM32(bios->data[offset + 1]);
3405 uint32_t mask = ROM32(bios->data[offset + 5]);
3406 uint32_t add = ROM32(bios->data[offset + 9]);
3407 uint32_t val;
3408
3409 val = bios_rd32(bios, reg);
3410 val = (val & mask) | ((val + add) & ~mask);
3411
3412 if (!iexec->execute)
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00003413 return 13;
Ben Skeggs6ee73862009-12-11 19:24:15 +10003414
3415 bios_wr32(bios, reg, val);
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00003416 return 13;
Ben Skeggs6ee73862009-12-11 19:24:15 +10003417}
3418
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00003419static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10003420init_auxch(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
3421{
3422 /*
3423 * INIT_AUXCH opcode: 0x98 ('')
3424 *
3425 * offset (8 bit): opcode
3426 * offset + 1 (32 bit): address
3427 * offset + 5 (8 bit): count
3428 * offset + 6 (8 bit): mask 0
3429 * offset + 7 (8 bit): data 0
3430 * ...
3431 *
3432 */
3433
3434 struct drm_device *dev = bios->dev;
3435 struct nouveau_i2c_chan *auxch;
3436 uint32_t addr = ROM32(bios->data[offset + 1]);
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00003437 uint8_t count = bios->data[offset + 5];
3438 int len = 6 + count * 2;
Ben Skeggs6ee73862009-12-11 19:24:15 +10003439 int ret, i;
3440
3441 if (!bios->display.output) {
3442 NV_ERROR(dev, "INIT_AUXCH: no active output\n");
Ben Skeggs309b8c82010-06-29 16:09:24 +10003443 return len;
Ben Skeggs6ee73862009-12-11 19:24:15 +10003444 }
3445
3446 auxch = init_i2c_device_find(dev, bios->display.output->i2c_index);
3447 if (!auxch) {
3448 NV_ERROR(dev, "INIT_AUXCH: couldn't get auxch %d\n",
3449 bios->display.output->i2c_index);
Ben Skeggs309b8c82010-06-29 16:09:24 +10003450 return len;
Ben Skeggs6ee73862009-12-11 19:24:15 +10003451 }
3452
3453 if (!iexec->execute)
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00003454 return len;
Ben Skeggs6ee73862009-12-11 19:24:15 +10003455
3456 offset += 6;
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00003457 for (i = 0; i < count; i++, offset += 2) {
Ben Skeggs6ee73862009-12-11 19:24:15 +10003458 uint8_t data;
3459
3460 ret = nouveau_dp_auxch(auxch, 9, addr, &data, 1);
3461 if (ret) {
3462 NV_ERROR(dev, "INIT_AUXCH: rd auxch fail %d\n", ret);
Ben Skeggs309b8c82010-06-29 16:09:24 +10003463 return len;
Ben Skeggs6ee73862009-12-11 19:24:15 +10003464 }
3465
3466 data &= bios->data[offset + 0];
3467 data |= bios->data[offset + 1];
3468
3469 ret = nouveau_dp_auxch(auxch, 8, addr, &data, 1);
3470 if (ret) {
3471 NV_ERROR(dev, "INIT_AUXCH: wr auxch fail %d\n", ret);
Ben Skeggs309b8c82010-06-29 16:09:24 +10003472 return len;
Ben Skeggs6ee73862009-12-11 19:24:15 +10003473 }
3474 }
3475
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00003476 return len;
Ben Skeggs6ee73862009-12-11 19:24:15 +10003477}
3478
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00003479static int
Ben Skeggs6ee73862009-12-11 19:24:15 +10003480init_zm_auxch(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
3481{
3482 /*
3483 * INIT_ZM_AUXCH opcode: 0x99 ('')
3484 *
3485 * offset (8 bit): opcode
3486 * offset + 1 (32 bit): address
3487 * offset + 5 (8 bit): count
3488 * offset + 6 (8 bit): data 0
3489 * ...
3490 *
3491 */
3492
3493 struct drm_device *dev = bios->dev;
3494 struct nouveau_i2c_chan *auxch;
3495 uint32_t addr = ROM32(bios->data[offset + 1]);
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00003496 uint8_t count = bios->data[offset + 5];
3497 int len = 6 + count;
Ben Skeggs6ee73862009-12-11 19:24:15 +10003498 int ret, i;
3499
3500 if (!bios->display.output) {
3501 NV_ERROR(dev, "INIT_ZM_AUXCH: no active output\n");
Ben Skeggs309b8c82010-06-29 16:09:24 +10003502 return len;
Ben Skeggs6ee73862009-12-11 19:24:15 +10003503 }
3504
3505 auxch = init_i2c_device_find(dev, bios->display.output->i2c_index);
3506 if (!auxch) {
3507 NV_ERROR(dev, "INIT_ZM_AUXCH: couldn't get auxch %d\n",
3508 bios->display.output->i2c_index);
Ben Skeggs309b8c82010-06-29 16:09:24 +10003509 return len;
Ben Skeggs6ee73862009-12-11 19:24:15 +10003510 }
3511
3512 if (!iexec->execute)
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00003513 return len;
Ben Skeggs6ee73862009-12-11 19:24:15 +10003514
3515 offset += 6;
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00003516 for (i = 0; i < count; i++, offset++) {
Ben Skeggs6ee73862009-12-11 19:24:15 +10003517 ret = nouveau_dp_auxch(auxch, 8, addr, &bios->data[offset], 1);
3518 if (ret) {
3519 NV_ERROR(dev, "INIT_ZM_AUXCH: wr auxch fail %d\n", ret);
Ben Skeggs309b8c82010-06-29 16:09:24 +10003520 return len;
Ben Skeggs6ee73862009-12-11 19:24:15 +10003521 }
3522 }
3523
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00003524 return len;
Ben Skeggs6ee73862009-12-11 19:24:15 +10003525}
3526
Marcin Koƛcielnickib715d642010-07-04 02:47:16 +00003527static int
3528init_i2c_long_if(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
3529{
3530 /*
3531 * INIT_I2C_LONG_IF opcode: 0x9A ('')
3532 *
3533 * offset (8 bit): opcode
3534 * offset + 1 (8 bit): DCB I2C table entry index
3535 * offset + 2 (8 bit): I2C slave address
3536 * offset + 3 (16 bit): I2C register
3537 * offset + 5 (8 bit): mask
3538 * offset + 6 (8 bit): data
3539 *
3540 * Read the register given by "I2C register" on the device addressed
3541 * by "I2C slave address" on the I2C bus given by "DCB I2C table
3542 * entry index". Compare the result AND "mask" to "data".
3543 * If they're not equal, skip subsequent opcodes until condition is
3544 * inverted (INIT_NOT), or we hit INIT_RESUME
3545 */
3546
3547 uint8_t i2c_index = bios->data[offset + 1];
3548 uint8_t i2c_address = bios->data[offset + 2] >> 1;
3549 uint8_t reglo = bios->data[offset + 3];
3550 uint8_t reghi = bios->data[offset + 4];
3551 uint8_t mask = bios->data[offset + 5];
3552 uint8_t data = bios->data[offset + 6];
3553 struct nouveau_i2c_chan *chan;
3554 uint8_t buf0[2] = { reghi, reglo };
3555 uint8_t buf1[1];
3556 struct i2c_msg msg[2] = {
3557 { i2c_address, 0, 1, buf0 },
3558 { i2c_address, I2C_M_RD, 1, buf1 },
3559 };
3560 int ret;
3561
3562 /* no execute check by design */
3563
3564 BIOSLOG(bios, "0x%04X: DCBI2CIndex: 0x%02X, I2CAddress: 0x%02X\n",
3565 offset, i2c_index, i2c_address);
3566
3567 chan = init_i2c_device_find(bios->dev, i2c_index);
3568 if (!chan)
3569 return -ENODEV;
3570
3571
3572 ret = i2c_transfer(&chan->adapter, msg, 2);
3573 if (ret < 0) {
3574 BIOSLOG(bios, "0x%04X: I2CReg: 0x%02X:0x%02X, Value: [no device], "
3575 "Mask: 0x%02X, Data: 0x%02X\n",
3576 offset, reghi, reglo, mask, data);
3577 iexec->execute = 0;
3578 return 7;
3579 }
3580
3581 BIOSLOG(bios, "0x%04X: I2CReg: 0x%02X:0x%02X, Value: 0x%02X, "
3582 "Mask: 0x%02X, Data: 0x%02X\n",
3583 offset, reghi, reglo, buf1[0], mask, data);
3584
3585 iexec->execute = ((buf1[0] & mask) == data);
3586
3587 return 7;
3588}
3589
Ben Skeggs6ee73862009-12-11 19:24:15 +10003590static struct init_tbl_entry itbl_entry[] = {
3591 /* command name , id , length , offset , mult , command handler */
3592 /* INIT_PROG (0x31, 15, 10, 4) removed due to no example of use */
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00003593 { "INIT_IO_RESTRICT_PROG" , 0x32, init_io_restrict_prog },
3594 { "INIT_REPEAT" , 0x33, init_repeat },
3595 { "INIT_IO_RESTRICT_PLL" , 0x34, init_io_restrict_pll },
3596 { "INIT_END_REPEAT" , 0x36, init_end_repeat },
3597 { "INIT_COPY" , 0x37, init_copy },
3598 { "INIT_NOT" , 0x38, init_not },
3599 { "INIT_IO_FLAG_CONDITION" , 0x39, init_io_flag_condition },
Ben Skeggs25908b72010-04-20 02:28:37 +10003600 { "INIT_DP_CONDITION" , 0x3A, init_dp_condition },
3601 { "INIT_OP_3B" , 0x3B, init_op_3b },
3602 { "INIT_OP_3C" , 0x3C, init_op_3c },
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00003603 { "INIT_INDEX_ADDRESS_LATCHED" , 0x49, init_idx_addr_latched },
3604 { "INIT_IO_RESTRICT_PLL2" , 0x4A, init_io_restrict_pll2 },
3605 { "INIT_PLL2" , 0x4B, init_pll2 },
3606 { "INIT_I2C_BYTE" , 0x4C, init_i2c_byte },
3607 { "INIT_ZM_I2C_BYTE" , 0x4D, init_zm_i2c_byte },
3608 { "INIT_ZM_I2C" , 0x4E, init_zm_i2c },
3609 { "INIT_TMDS" , 0x4F, init_tmds },
3610 { "INIT_ZM_TMDS_GROUP" , 0x50, init_zm_tmds_group },
3611 { "INIT_CR_INDEX_ADDRESS_LATCHED" , 0x51, init_cr_idx_adr_latch },
3612 { "INIT_CR" , 0x52, init_cr },
3613 { "INIT_ZM_CR" , 0x53, init_zm_cr },
3614 { "INIT_ZM_CR_GROUP" , 0x54, init_zm_cr_group },
3615 { "INIT_CONDITION_TIME" , 0x56, init_condition_time },
Marcin Koƛcielnickie3a19242010-07-02 19:33:01 +00003616 { "INIT_LTIME" , 0x57, init_ltime },
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00003617 { "INIT_ZM_REG_SEQUENCE" , 0x58, init_zm_reg_sequence },
Ben Skeggs6ee73862009-12-11 19:24:15 +10003618 /* INIT_INDIRECT_REG (0x5A, 7, 0, 0) removed due to no example of use */
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00003619 { "INIT_SUB_DIRECT" , 0x5B, init_sub_direct },
Ben Skeggsec64a402011-03-21 21:31:21 +10003620 { "INIT_JUMP" , 0x5C, init_jump },
Marcin Koƛcielnickib715d642010-07-04 02:47:16 +00003621 { "INIT_I2C_IF" , 0x5E, init_i2c_if },
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00003622 { "INIT_COPY_NV_REG" , 0x5F, init_copy_nv_reg },
3623 { "INIT_ZM_INDEX_IO" , 0x62, init_zm_index_io },
3624 { "INIT_COMPUTE_MEM" , 0x63, init_compute_mem },
3625 { "INIT_RESET" , 0x65, init_reset },
3626 { "INIT_CONFIGURE_MEM" , 0x66, init_configure_mem },
3627 { "INIT_CONFIGURE_CLK" , 0x67, init_configure_clk },
3628 { "INIT_CONFIGURE_PREINIT" , 0x68, init_configure_preinit },
3629 { "INIT_IO" , 0x69, init_io },
3630 { "INIT_SUB" , 0x6B, init_sub },
3631 { "INIT_RAM_CONDITION" , 0x6D, init_ram_condition },
3632 { "INIT_NV_REG" , 0x6E, init_nv_reg },
3633 { "INIT_MACRO" , 0x6F, init_macro },
3634 { "INIT_DONE" , 0x71, init_done },
3635 { "INIT_RESUME" , 0x72, init_resume },
Ben Skeggs6ee73862009-12-11 19:24:15 +10003636 /* INIT_RAM_CONDITION2 (0x73, 9, 0, 0) removed due to no example of use */
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00003637 { "INIT_TIME" , 0x74, init_time },
3638 { "INIT_CONDITION" , 0x75, init_condition },
3639 { "INIT_IO_CONDITION" , 0x76, init_io_condition },
3640 { "INIT_INDEX_IO" , 0x78, init_index_io },
3641 { "INIT_PLL" , 0x79, init_pll },
3642 { "INIT_ZM_REG" , 0x7A, init_zm_reg },
3643 { "INIT_RAM_RESTRICT_PLL" , 0x87, init_ram_restrict_pll },
3644 { "INIT_8C" , 0x8C, init_8c },
3645 { "INIT_8D" , 0x8D, init_8d },
3646 { "INIT_GPIO" , 0x8E, init_gpio },
3647 { "INIT_RAM_RESTRICT_ZM_REG_GROUP" , 0x8F, init_ram_restrict_zm_reg_group },
3648 { "INIT_COPY_ZM_REG" , 0x90, init_copy_zm_reg },
3649 { "INIT_ZM_REG_GROUP_ADDRESS_LATCHED" , 0x91, init_zm_reg_group_addr_latched },
3650 { "INIT_RESERVED" , 0x92, init_reserved },
3651 { "INIT_96" , 0x96, init_96 },
3652 { "INIT_97" , 0x97, init_97 },
3653 { "INIT_AUXCH" , 0x98, init_auxch },
3654 { "INIT_ZM_AUXCH" , 0x99, init_zm_auxch },
Marcin Koƛcielnickib715d642010-07-04 02:47:16 +00003655 { "INIT_I2C_LONG_IF" , 0x9A, init_i2c_long_if },
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00003656 { NULL , 0 , NULL }
Ben Skeggs6ee73862009-12-11 19:24:15 +10003657};
3658
Ben Skeggs6ee73862009-12-11 19:24:15 +10003659#define MAX_TABLE_OPS 1000
3660
3661static int
Ben Skeggsec64a402011-03-21 21:31:21 +10003662parse_init_table(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
Ben Skeggs6ee73862009-12-11 19:24:15 +10003663{
3664 /*
3665 * Parses all commands in an init table.
3666 *
3667 * We start out executing all commands found in the init table. Some
3668 * opcodes may change the status of iexec->execute to SKIP, which will
3669 * cause the following opcodes to perform no operation until the value
3670 * is changed back to EXECUTE.
3671 */
3672
Ben Skeggs92b96182010-05-10 16:59:42 +10003673 int count = 0, i, ret;
Ben Skeggs6ee73862009-12-11 19:24:15 +10003674 uint8_t id;
3675
Ben Skeggsa8e415d2011-08-04 13:57:33 +10003676 /* catch NULL script pointers */
3677 if (offset == 0)
3678 return 0;
3679
Ben Skeggs6ee73862009-12-11 19:24:15 +10003680 /*
3681 * Loop until INIT_DONE causes us to break out of the loop
3682 * (or until offset > bios length just in case... )
3683 * (and no more than MAX_TABLE_OPS iterations, just in case... )
3684 */
3685 while ((offset < bios->length) && (count++ < MAX_TABLE_OPS)) {
3686 id = bios->data[offset];
3687
3688 /* Find matching id in itbl_entry */
3689 for (i = 0; itbl_entry[i].name && (itbl_entry[i].id != id); i++)
3690 ;
3691
Ben Skeggs92b96182010-05-10 16:59:42 +10003692 if (!itbl_entry[i].name) {
Ben Skeggs6ee73862009-12-11 19:24:15 +10003693 NV_ERROR(bios->dev,
3694 "0x%04X: Init table command not found: "
3695 "0x%02X\n", offset, id);
3696 return -ENOENT;
3697 }
Ben Skeggs92b96182010-05-10 16:59:42 +10003698
3699 BIOSLOG(bios, "0x%04X: [ (0x%02X) - %s ]\n", offset,
3700 itbl_entry[i].id, itbl_entry[i].name);
3701
3702 /* execute eventual command handler */
3703 ret = (*itbl_entry[i].handler)(bios, offset, iexec);
3704 if (ret < 0) {
3705 NV_ERROR(bios->dev, "0x%04X: Failed parsing init "
3706 "table opcode: %s %d\n", offset,
3707 itbl_entry[i].name, ret);
3708 }
3709
3710 if (ret <= 0)
3711 break;
3712
3713 /*
3714 * Add the offset of the current command including all data
3715 * of that command. The offset will then be pointing on the
3716 * next op code.
3717 */
3718 offset += ret;
Ben Skeggs6ee73862009-12-11 19:24:15 +10003719 }
3720
3721 if (offset >= bios->length)
3722 NV_WARN(bios->dev,
3723 "Offset 0x%04X greater than known bios image length. "
3724 "Corrupt image?\n", offset);
3725 if (count >= MAX_TABLE_OPS)
3726 NV_WARN(bios->dev,
3727 "More than %d opcodes to a table is unlikely, "
3728 "is the bios image corrupt?\n", MAX_TABLE_OPS);
3729
3730 return 0;
3731}
3732
3733static void
3734parse_init_tables(struct nvbios *bios)
3735{
3736 /* Loops and calls parse_init_table() for each present table. */
3737
3738 int i = 0;
3739 uint16_t table;
3740 struct init_exec iexec = {true, false};
3741
3742 if (bios->old_style_init) {
3743 if (bios->init_script_tbls_ptr)
3744 parse_init_table(bios, bios->init_script_tbls_ptr, &iexec);
3745 if (bios->extra_init_script_tbl_ptr)
3746 parse_init_table(bios, bios->extra_init_script_tbl_ptr, &iexec);
3747
3748 return;
3749 }
3750
3751 while ((table = ROM16(bios->data[bios->init_script_tbls_ptr + i]))) {
3752 NV_INFO(bios->dev,
3753 "Parsing VBIOS init table %d at offset 0x%04X\n",
3754 i / 2, table);
3755 BIOSLOG(bios, "0x%04X: ------ Executing following commands ------\n", table);
3756
3757 parse_init_table(bios, table, &iexec);
3758 i += 2;
3759 }
3760}
3761
3762static uint16_t clkcmptable(struct nvbios *bios, uint16_t clktable, int pxclk)
3763{
3764 int compare_record_len, i = 0;
3765 uint16_t compareclk, scriptptr = 0;
3766
3767 if (bios->major_version < 5) /* pre BIT */
3768 compare_record_len = 3;
3769 else
3770 compare_record_len = 4;
3771
3772 do {
3773 compareclk = ROM16(bios->data[clktable + compare_record_len * i]);
3774 if (pxclk >= compareclk * 10) {
3775 if (bios->major_version < 5) {
3776 uint8_t tmdssub = bios->data[clktable + 2 + compare_record_len * i];
3777 scriptptr = ROM16(bios->data[bios->init_script_tbls_ptr + tmdssub * 2]);
3778 } else
3779 scriptptr = ROM16(bios->data[clktable + 2 + compare_record_len * i]);
3780 break;
3781 }
3782 i++;
3783 } while (compareclk);
3784
3785 return scriptptr;
3786}
3787
3788static void
3789run_digital_op_script(struct drm_device *dev, uint16_t scriptptr,
3790 struct dcb_entry *dcbent, int head, bool dl)
3791{
3792 struct drm_nouveau_private *dev_priv = dev->dev_private;
Ben Skeggs04a39c52010-02-24 10:03:05 +10003793 struct nvbios *bios = &dev_priv->vbios;
Ben Skeggs6ee73862009-12-11 19:24:15 +10003794 struct init_exec iexec = {true, false};
3795
3796 NV_TRACE(dev, "0x%04X: Parsing digital output script table\n",
3797 scriptptr);
3798 bios_idxprt_wr(bios, NV_CIO_CRX__COLOR, NV_CIO_CRE_44,
3799 head ? NV_CIO_CRE_44_HEADB : NV_CIO_CRE_44_HEADA);
3800 /* note: if dcb entries have been merged, index may be misleading */
3801 NVWriteVgaCrtc5758(dev, head, 0, dcbent->index);
3802 parse_init_table(bios, scriptptr, &iexec);
3803
3804 nv04_dfp_bind_head(dev, dcbent, head, dl);
3805}
3806
3807static int call_lvds_manufacturer_script(struct drm_device *dev, struct dcb_entry *dcbent, int head, enum LVDS_script script)
3808{
3809 struct drm_nouveau_private *dev_priv = dev->dev_private;
Ben Skeggs04a39c52010-02-24 10:03:05 +10003810 struct nvbios *bios = &dev_priv->vbios;
Ben Skeggs6ee73862009-12-11 19:24:15 +10003811 uint8_t sub = bios->data[bios->fp.xlated_entry + script] + (bios->fp.link_c_increment && dcbent->or & OUTPUT_C ? 1 : 0);
3812 uint16_t scriptofs = ROM16(bios->data[bios->init_script_tbls_ptr + sub * 2]);
3813
3814 if (!bios->fp.xlated_entry || !sub || !scriptofs)
3815 return -EINVAL;
3816
3817 run_digital_op_script(dev, scriptofs, dcbent, head, bios->fp.dual_link);
3818
3819 if (script == LVDS_PANEL_OFF) {
3820 /* off-on delay in ms */
Ben Skeggsc7ca4d12011-02-03 20:10:49 +10003821 mdelay(ROM16(bios->data[bios->fp.xlated_entry + 7]));
Ben Skeggs6ee73862009-12-11 19:24:15 +10003822 }
3823#ifdef __powerpc__
3824 /* Powerbook specific quirks */
Francisco Jerezd31e0782010-08-20 14:19:45 +02003825 if (script == LVDS_RESET &&
3826 (dev->pci_device == 0x0179 || dev->pci_device == 0x0189 ||
3827 dev->pci_device == 0x0329))
3828 nv_write_tmds(dev, dcbent->or, 0, 0x02, 0x72);
Ben Skeggs6ee73862009-12-11 19:24:15 +10003829#endif
3830
3831 return 0;
3832}
3833
3834static int run_lvds_table(struct drm_device *dev, struct dcb_entry *dcbent, int head, enum LVDS_script script, int pxclk)
3835{
3836 /*
3837 * The BIT LVDS table's header has the information to setup the
3838 * necessary registers. Following the standard 4 byte header are:
3839 * A bitmask byte and a dual-link transition pxclk value for use in
3840 * selecting the init script when not using straps; 4 script pointers
3841 * for panel power, selected by output and on/off; and 8 table pointers
3842 * for panel init, the needed one determined by output, and bits in the
3843 * conf byte. These tables are similar to the TMDS tables, consisting
3844 * of a list of pxclks and script pointers.
3845 */
3846 struct drm_nouveau_private *dev_priv = dev->dev_private;
Ben Skeggs04a39c52010-02-24 10:03:05 +10003847 struct nvbios *bios = &dev_priv->vbios;
Ben Skeggs6ee73862009-12-11 19:24:15 +10003848 unsigned int outputset = (dcbent->or == 4) ? 1 : 0;
3849 uint16_t scriptptr = 0, clktable;
Ben Skeggs6ee73862009-12-11 19:24:15 +10003850
3851 /*
3852 * For now we assume version 3.0 table - g80 support will need some
3853 * changes
3854 */
3855
3856 switch (script) {
3857 case LVDS_INIT:
3858 return -ENOSYS;
3859 case LVDS_BACKLIGHT_ON:
3860 case LVDS_PANEL_ON:
3861 scriptptr = ROM16(bios->data[bios->fp.lvdsmanufacturerpointer + 7 + outputset * 2]);
3862 break;
3863 case LVDS_BACKLIGHT_OFF:
3864 case LVDS_PANEL_OFF:
3865 scriptptr = ROM16(bios->data[bios->fp.lvdsmanufacturerpointer + 11 + outputset * 2]);
3866 break;
3867 case LVDS_RESET:
Ben Skeggsf3bbb9c2010-03-18 12:05:43 +10003868 clktable = bios->fp.lvdsmanufacturerpointer + 15;
3869 if (dcbent->or == 4)
3870 clktable += 8;
3871
Ben Skeggs6ee73862009-12-11 19:24:15 +10003872 if (dcbent->lvdsconf.use_straps_for_mode) {
3873 if (bios->fp.dual_link)
Ben Skeggsf3bbb9c2010-03-18 12:05:43 +10003874 clktable += 4;
3875 if (bios->fp.if_is_24bit)
3876 clktable += 2;
Ben Skeggs6ee73862009-12-11 19:24:15 +10003877 } else {
3878 /* using EDID */
Ben Skeggsf3bbb9c2010-03-18 12:05:43 +10003879 int cmpval_24bit = (dcbent->or == 4) ? 4 : 1;
Ben Skeggs6ee73862009-12-11 19:24:15 +10003880
3881 if (bios->fp.dual_link) {
Ben Skeggsf3bbb9c2010-03-18 12:05:43 +10003882 clktable += 4;
3883 cmpval_24bit <<= 1;
Ben Skeggs6ee73862009-12-11 19:24:15 +10003884 }
Ben Skeggsf3bbb9c2010-03-18 12:05:43 +10003885
3886 if (bios->fp.strapless_is_24bit & cmpval_24bit)
3887 clktable += 2;
Ben Skeggs6ee73862009-12-11 19:24:15 +10003888 }
3889
Ben Skeggsf3bbb9c2010-03-18 12:05:43 +10003890 clktable = ROM16(bios->data[clktable]);
Ben Skeggs6ee73862009-12-11 19:24:15 +10003891 if (!clktable) {
3892 NV_ERROR(dev, "Pixel clock comparison table not found\n");
3893 return -ENOENT;
3894 }
3895 scriptptr = clkcmptable(bios, clktable, pxclk);
3896 }
3897
3898 if (!scriptptr) {
3899 NV_ERROR(dev, "LVDS output init script not found\n");
3900 return -ENOENT;
3901 }
3902 run_digital_op_script(dev, scriptptr, dcbent, head, bios->fp.dual_link);
3903
3904 return 0;
3905}
3906
3907int call_lvds_script(struct drm_device *dev, struct dcb_entry *dcbent, int head, enum LVDS_script script, int pxclk)
3908{
3909 /*
3910 * LVDS operations are multiplexed in an effort to present a single API
3911 * which works with two vastly differing underlying structures.
3912 * This acts as the demux
3913 */
3914
3915 struct drm_nouveau_private *dev_priv = dev->dev_private;
Ben Skeggs04a39c52010-02-24 10:03:05 +10003916 struct nvbios *bios = &dev_priv->vbios;
Ben Skeggs6ee73862009-12-11 19:24:15 +10003917 uint8_t lvds_ver = bios->data[bios->fp.lvdsmanufacturerpointer];
3918 uint32_t sel_clk_binding, sel_clk;
3919 int ret;
3920
3921 if (bios->fp.last_script_invoc == (script << 1 | head) || !lvds_ver ||
3922 (lvds_ver >= 0x30 && script == LVDS_INIT))
3923 return 0;
3924
3925 if (!bios->fp.lvds_init_run) {
3926 bios->fp.lvds_init_run = true;
3927 call_lvds_script(dev, dcbent, head, LVDS_INIT, pxclk);
3928 }
3929
3930 if (script == LVDS_PANEL_ON && bios->fp.reset_after_pclk_change)
3931 call_lvds_script(dev, dcbent, head, LVDS_RESET, pxclk);
3932 if (script == LVDS_RESET && bios->fp.power_off_for_reset)
3933 call_lvds_script(dev, dcbent, head, LVDS_PANEL_OFF, pxclk);
3934
3935 NV_TRACE(dev, "Calling LVDS script %d:\n", script);
3936
3937 /* don't let script change pll->head binding */
3938 sel_clk_binding = bios_rd32(bios, NV_PRAMDAC_SEL_CLK) & 0x50000;
3939
3940 if (lvds_ver < 0x30)
3941 ret = call_lvds_manufacturer_script(dev, dcbent, head, script);
3942 else
3943 ret = run_lvds_table(dev, dcbent, head, script, pxclk);
3944
3945 bios->fp.last_script_invoc = (script << 1 | head);
3946
3947 sel_clk = NVReadRAMDAC(dev, 0, NV_PRAMDAC_SEL_CLK) & ~0x50000;
3948 NVWriteRAMDAC(dev, 0, NV_PRAMDAC_SEL_CLK, sel_clk | sel_clk_binding);
3949 /* some scripts set a value in NV_PBUS_POWERCTRL_2 and break video overlay */
3950 nvWriteMC(dev, NV_PBUS_POWERCTRL_2, 0);
3951
3952 return ret;
3953}
3954
3955struct lvdstableheader {
3956 uint8_t lvds_ver, headerlen, recordlen;
3957};
3958
3959static int parse_lvds_manufacturer_table_header(struct drm_device *dev, struct nvbios *bios, struct lvdstableheader *lth)
3960{
3961 /*
3962 * BMP version (0xa) LVDS table has a simple header of version and
3963 * record length. The BIT LVDS table has the typical BIT table header:
3964 * version byte, header length byte, record length byte, and a byte for
3965 * the maximum number of records that can be held in the table.
3966 */
3967
3968 uint8_t lvds_ver, headerlen, recordlen;
3969
3970 memset(lth, 0, sizeof(struct lvdstableheader));
3971
3972 if (bios->fp.lvdsmanufacturerpointer == 0x0) {
3973 NV_ERROR(dev, "Pointer to LVDS manufacturer table invalid\n");
3974 return -EINVAL;
3975 }
3976
3977 lvds_ver = bios->data[bios->fp.lvdsmanufacturerpointer];
3978
3979 switch (lvds_ver) {
3980 case 0x0a: /* pre NV40 */
3981 headerlen = 2;
3982 recordlen = bios->data[bios->fp.lvdsmanufacturerpointer + 1];
3983 break;
3984 case 0x30: /* NV4x */
3985 headerlen = bios->data[bios->fp.lvdsmanufacturerpointer + 1];
3986 if (headerlen < 0x1f) {
3987 NV_ERROR(dev, "LVDS table header not understood\n");
3988 return -EINVAL;
3989 }
3990 recordlen = bios->data[bios->fp.lvdsmanufacturerpointer + 2];
3991 break;
3992 case 0x40: /* G80/G90 */
3993 headerlen = bios->data[bios->fp.lvdsmanufacturerpointer + 1];
3994 if (headerlen < 0x7) {
3995 NV_ERROR(dev, "LVDS table header not understood\n");
3996 return -EINVAL;
3997 }
3998 recordlen = bios->data[bios->fp.lvdsmanufacturerpointer + 2];
3999 break;
4000 default:
4001 NV_ERROR(dev,
4002 "LVDS table revision %d.%d not currently supported\n",
4003 lvds_ver >> 4, lvds_ver & 0xf);
4004 return -ENOSYS;
4005 }
4006
4007 lth->lvds_ver = lvds_ver;
4008 lth->headerlen = headerlen;
4009 lth->recordlen = recordlen;
4010
4011 return 0;
4012}
4013
4014static int
4015get_fp_strap(struct drm_device *dev, struct nvbios *bios)
4016{
4017 struct drm_nouveau_private *dev_priv = dev->dev_private;
4018
4019 /*
4020 * The fp strap is normally dictated by the "User Strap" in
4021 * PEXTDEV_BOOT_0[20:16], but on BMP cards when bit 2 of the
4022 * Internal_Flags struct at 0x48 is set, the user strap gets overriden
4023 * by the PCI subsystem ID during POST, but not before the previous user
4024 * strap has been committed to CR58 for CR57=0xf on head A, which may be
4025 * read and used instead
4026 */
4027
4028 if (bios->major_version < 5 && bios->data[0x48] & 0x4)
4029 return NVReadVgaCrtc5758(dev, 0, 0xf) & 0xf;
4030
4031 if (dev_priv->card_type >= NV_50)
4032 return (bios_rd32(bios, NV_PEXTDEV_BOOT_0) >> 24) & 0xf;
4033 else
4034 return (bios_rd32(bios, NV_PEXTDEV_BOOT_0) >> 16) & 0xf;
4035}
4036
4037static int parse_fp_mode_table(struct drm_device *dev, struct nvbios *bios)
4038{
4039 uint8_t *fptable;
4040 uint8_t fptable_ver, headerlen = 0, recordlen, fpentries = 0xf, fpindex;
4041 int ret, ofs, fpstrapping;
4042 struct lvdstableheader lth;
4043
4044 if (bios->fp.fptablepointer == 0x0) {
4045 /* Apple cards don't have the fp table; the laptops use DDC */
4046 /* The table is also missing on some x86 IGPs */
4047#ifndef __powerpc__
4048 NV_ERROR(dev, "Pointer to flat panel table invalid\n");
4049#endif
Ben Skeggs04a39c52010-02-24 10:03:05 +10004050 bios->digital_min_front_porch = 0x4b;
Ben Skeggs6ee73862009-12-11 19:24:15 +10004051 return 0;
4052 }
4053
4054 fptable = &bios->data[bios->fp.fptablepointer];
4055 fptable_ver = fptable[0];
4056
4057 switch (fptable_ver) {
4058 /*
4059 * BMP version 0x5.0x11 BIOSen have version 1 like tables, but no
4060 * version field, and miss one of the spread spectrum/PWM bytes.
4061 * This could affect early GF2Go parts (not seen any appropriate ROMs
4062 * though). Here we assume that a version of 0x05 matches this case
4063 * (combining with a BMP version check would be better), as the
4064 * common case for the panel type field is 0x0005, and that is in
4065 * fact what we are reading the first byte of.
4066 */
4067 case 0x05: /* some NV10, 11, 15, 16 */
4068 recordlen = 42;
4069 ofs = -1;
4070 break;
4071 case 0x10: /* some NV15/16, and NV11+ */
4072 recordlen = 44;
4073 ofs = 0;
4074 break;
4075 case 0x20: /* NV40+ */
4076 headerlen = fptable[1];
4077 recordlen = fptable[2];
4078 fpentries = fptable[3];
4079 /*
4080 * fptable[4] is the minimum
4081 * RAMDAC_FP_HCRTC -> RAMDAC_FP_HSYNC_START gap
4082 */
Ben Skeggs04a39c52010-02-24 10:03:05 +10004083 bios->digital_min_front_porch = fptable[4];
Ben Skeggs6ee73862009-12-11 19:24:15 +10004084 ofs = -7;
4085 break;
4086 default:
4087 NV_ERROR(dev,
4088 "FP table revision %d.%d not currently supported\n",
4089 fptable_ver >> 4, fptable_ver & 0xf);
4090 return -ENOSYS;
4091 }
4092
4093 if (!bios->is_mobile) /* !mobile only needs digital_min_front_porch */
4094 return 0;
4095
4096 ret = parse_lvds_manufacturer_table_header(dev, bios, &lth);
4097 if (ret)
4098 return ret;
4099
4100 if (lth.lvds_ver == 0x30 || lth.lvds_ver == 0x40) {
4101 bios->fp.fpxlatetableptr = bios->fp.lvdsmanufacturerpointer +
4102 lth.headerlen + 1;
4103 bios->fp.xlatwidth = lth.recordlen;
4104 }
4105 if (bios->fp.fpxlatetableptr == 0x0) {
4106 NV_ERROR(dev, "Pointer to flat panel xlat table invalid\n");
4107 return -EINVAL;
4108 }
4109
4110 fpstrapping = get_fp_strap(dev, bios);
4111
4112 fpindex = bios->data[bios->fp.fpxlatetableptr +
4113 fpstrapping * bios->fp.xlatwidth];
4114
4115 if (fpindex > fpentries) {
4116 NV_ERROR(dev, "Bad flat panel table index\n");
4117 return -ENOENT;
4118 }
4119
4120 /* nv4x cards need both a strap value and fpindex of 0xf to use DDC */
4121 if (lth.lvds_ver > 0x10)
Ben Skeggs04a39c52010-02-24 10:03:05 +10004122 bios->fp_no_ddc = fpstrapping != 0xf || fpindex != 0xf;
Ben Skeggs6ee73862009-12-11 19:24:15 +10004123
4124 /*
4125 * If either the strap or xlated fpindex value are 0xf there is no
4126 * panel using a strap-derived bios mode present. this condition
4127 * includes, but is different from, the DDC panel indicator above
4128 */
4129 if (fpstrapping == 0xf || fpindex == 0xf)
4130 return 0;
4131
4132 bios->fp.mode_ptr = bios->fp.fptablepointer + headerlen +
4133 recordlen * fpindex + ofs;
4134
4135 NV_TRACE(dev, "BIOS FP mode: %dx%d (%dkHz pixel clock)\n",
4136 ROM16(bios->data[bios->fp.mode_ptr + 11]) + 1,
4137 ROM16(bios->data[bios->fp.mode_ptr + 25]) + 1,
4138 ROM16(bios->data[bios->fp.mode_ptr + 7]) * 10);
4139
4140 return 0;
4141}
4142
4143bool nouveau_bios_fp_mode(struct drm_device *dev, struct drm_display_mode *mode)
4144{
4145 struct drm_nouveau_private *dev_priv = dev->dev_private;
Ben Skeggs04a39c52010-02-24 10:03:05 +10004146 struct nvbios *bios = &dev_priv->vbios;
Ben Skeggs6ee73862009-12-11 19:24:15 +10004147 uint8_t *mode_entry = &bios->data[bios->fp.mode_ptr];
4148
4149 if (!mode) /* just checking whether we can produce a mode */
4150 return bios->fp.mode_ptr;
4151
4152 memset(mode, 0, sizeof(struct drm_display_mode));
4153 /*
4154 * For version 1.0 (version in byte 0):
4155 * bytes 1-2 are "panel type", including bits on whether Colour/mono,
4156 * single/dual link, and type (TFT etc.)
4157 * bytes 3-6 are bits per colour in RGBX
4158 */
4159 mode->clock = ROM16(mode_entry[7]) * 10;
4160 /* bytes 9-10 is HActive */
4161 mode->hdisplay = ROM16(mode_entry[11]) + 1;
4162 /*
4163 * bytes 13-14 is HValid Start
4164 * bytes 15-16 is HValid End
4165 */
4166 mode->hsync_start = ROM16(mode_entry[17]) + 1;
4167 mode->hsync_end = ROM16(mode_entry[19]) + 1;
4168 mode->htotal = ROM16(mode_entry[21]) + 1;
4169 /* bytes 23-24, 27-30 similarly, but vertical */
4170 mode->vdisplay = ROM16(mode_entry[25]) + 1;
4171 mode->vsync_start = ROM16(mode_entry[31]) + 1;
4172 mode->vsync_end = ROM16(mode_entry[33]) + 1;
4173 mode->vtotal = ROM16(mode_entry[35]) + 1;
4174 mode->flags |= (mode_entry[37] & 0x10) ?
4175 DRM_MODE_FLAG_PHSYNC : DRM_MODE_FLAG_NHSYNC;
4176 mode->flags |= (mode_entry[37] & 0x1) ?
4177 DRM_MODE_FLAG_PVSYNC : DRM_MODE_FLAG_NVSYNC;
4178 /*
4179 * bytes 38-39 relate to spread spectrum settings
4180 * bytes 40-43 are something to do with PWM
4181 */
4182
4183 mode->status = MODE_OK;
4184 mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
4185 drm_mode_set_name(mode);
4186 return bios->fp.mode_ptr;
4187}
4188
4189int nouveau_bios_parse_lvds_table(struct drm_device *dev, int pxclk, bool *dl, bool *if_is_24bit)
4190{
4191 /*
4192 * The LVDS table header is (mostly) described in
4193 * parse_lvds_manufacturer_table_header(): the BIT header additionally
4194 * contains the dual-link transition pxclk (in 10s kHz), at byte 5 - if
4195 * straps are not being used for the panel, this specifies the frequency
4196 * at which modes should be set up in the dual link style.
4197 *
4198 * Following the header, the BMP (ver 0xa) table has several records,
Daniel Mack3ad2f3fb2010-02-03 08:01:28 +08004199 * indexed by a separate xlat table, indexed in turn by the fp strap in
Ben Skeggs6ee73862009-12-11 19:24:15 +10004200 * EXTDEV_BOOT. Each record had a config byte, followed by 6 script
4201 * numbers for use by INIT_SUB which controlled panel init and power,
4202 * and finally a dword of ms to sleep between power off and on
4203 * operations.
4204 *
4205 * In the BIT versions, the table following the header serves as an
4206 * integrated config and xlat table: the records in the table are
4207 * indexed by the FP strap nibble in EXTDEV_BOOT, and each record has
4208 * two bytes - the first as a config byte, the second for indexing the
4209 * fp mode table pointed to by the BIT 'D' table
4210 *
4211 * DDC is not used until after card init, so selecting the correct table
4212 * entry and setting the dual link flag for EDID equipped panels,
4213 * requiring tests against the native-mode pixel clock, cannot be done
4214 * until later, when this function should be called with non-zero pxclk
4215 */
4216 struct drm_nouveau_private *dev_priv = dev->dev_private;
Ben Skeggs04a39c52010-02-24 10:03:05 +10004217 struct nvbios *bios = &dev_priv->vbios;
Ben Skeggs6ee73862009-12-11 19:24:15 +10004218 int fpstrapping = get_fp_strap(dev, bios), lvdsmanufacturerindex = 0;
4219 struct lvdstableheader lth;
4220 uint16_t lvdsofs;
Ben Skeggs04a39c52010-02-24 10:03:05 +10004221 int ret, chip_version = bios->chip_version;
Ben Skeggs6ee73862009-12-11 19:24:15 +10004222
4223 ret = parse_lvds_manufacturer_table_header(dev, bios, &lth);
4224 if (ret)
4225 return ret;
4226
4227 switch (lth.lvds_ver) {
4228 case 0x0a: /* pre NV40 */
4229 lvdsmanufacturerindex = bios->data[
4230 bios->fp.fpxlatemanufacturertableptr +
4231 fpstrapping];
4232
4233 /* we're done if this isn't the EDID panel case */
4234 if (!pxclk)
4235 break;
4236
4237 if (chip_version < 0x25) {
4238 /* nv17 behaviour
4239 *
4240 * It seems the old style lvds script pointer is reused
4241 * to select 18/24 bit colour depth for EDID panels.
4242 */
4243 lvdsmanufacturerindex =
4244 (bios->legacy.lvds_single_a_script_ptr & 1) ?
4245 2 : 0;
4246 if (pxclk >= bios->fp.duallink_transition_clk)
4247 lvdsmanufacturerindex++;
4248 } else if (chip_version < 0x30) {
4249 /* nv28 behaviour (off-chip encoder)
4250 *
4251 * nv28 does a complex dance of first using byte 121 of
4252 * the EDID to choose the lvdsmanufacturerindex, then
4253 * later attempting to match the EDID manufacturer and
4254 * product IDs in a table (signature 'pidt' (panel id
4255 * table?)), setting an lvdsmanufacturerindex of 0 and
4256 * an fp strap of the match index (or 0xf if none)
4257 */
4258 lvdsmanufacturerindex = 0;
4259 } else {
4260 /* nv31, nv34 behaviour */
4261 lvdsmanufacturerindex = 0;
4262 if (pxclk >= bios->fp.duallink_transition_clk)
4263 lvdsmanufacturerindex = 2;
4264 if (pxclk >= 140000)
4265 lvdsmanufacturerindex = 3;
4266 }
4267
4268 /*
4269 * nvidia set the high nibble of (cr57=f, cr58) to
4270 * lvdsmanufacturerindex in this case; we don't
4271 */
4272 break;
4273 case 0x30: /* NV4x */
4274 case 0x40: /* G80/G90 */
4275 lvdsmanufacturerindex = fpstrapping;
4276 break;
4277 default:
4278 NV_ERROR(dev, "LVDS table revision not currently supported\n");
4279 return -ENOSYS;
4280 }
4281
4282 lvdsofs = bios->fp.xlated_entry = bios->fp.lvdsmanufacturerpointer + lth.headerlen + lth.recordlen * lvdsmanufacturerindex;
4283 switch (lth.lvds_ver) {
4284 case 0x0a:
4285 bios->fp.power_off_for_reset = bios->data[lvdsofs] & 1;
4286 bios->fp.reset_after_pclk_change = bios->data[lvdsofs] & 2;
4287 bios->fp.dual_link = bios->data[lvdsofs] & 4;
4288 bios->fp.link_c_increment = bios->data[lvdsofs] & 8;
4289 *if_is_24bit = bios->data[lvdsofs] & 16;
4290 break;
4291 case 0x30:
Ben Skeggsf3bbb9c2010-03-18 12:05:43 +10004292 case 0x40:
Ben Skeggs6ee73862009-12-11 19:24:15 +10004293 /*
4294 * No sign of the "power off for reset" or "reset for panel
4295 * on" bits, but it's safer to assume we should
4296 */
4297 bios->fp.power_off_for_reset = true;
4298 bios->fp.reset_after_pclk_change = true;
Ben Skeggsf3bbb9c2010-03-18 12:05:43 +10004299
Ben Skeggs6ee73862009-12-11 19:24:15 +10004300 /*
4301 * It's ok lvdsofs is wrong for nv4x edid case; dual_link is
Ben Skeggsf3bbb9c2010-03-18 12:05:43 +10004302 * over-written, and if_is_24bit isn't used
Ben Skeggs6ee73862009-12-11 19:24:15 +10004303 */
4304 bios->fp.dual_link = bios->data[lvdsofs] & 1;
Ben Skeggs6ee73862009-12-11 19:24:15 +10004305 bios->fp.if_is_24bit = bios->data[lvdsofs] & 2;
4306 bios->fp.strapless_is_24bit = bios->data[bios->fp.lvdsmanufacturerpointer + 4];
4307 bios->fp.duallink_transition_clk = ROM16(bios->data[bios->fp.lvdsmanufacturerpointer + 5]) * 10;
4308 break;
4309 }
4310
Ben Skeggs2eb92c82010-03-18 13:38:04 +10004311 /* Dell Latitude D620 reports a too-high value for the dual-link
4312 * transition freq, causing us to program the panel incorrectly.
4313 *
4314 * It doesn't appear the VBIOS actually uses its transition freq
4315 * (90000kHz), instead it uses the "Number of LVDS channels" field
4316 * out of the panel ID structure (http://www.spwg.org/).
4317 *
4318 * For the moment, a quirk will do :)
4319 */
Francisco Jerezacae1162010-08-15 14:31:31 +02004320 if (nv_match_device(dev, 0x01d7, 0x1028, 0x01c2))
Ben Skeggs2eb92c82010-03-18 13:38:04 +10004321 bios->fp.duallink_transition_clk = 80000;
Ben Skeggs2eb92c82010-03-18 13:38:04 +10004322
Ben Skeggs6ee73862009-12-11 19:24:15 +10004323 /* set dual_link flag for EDID case */
4324 if (pxclk && (chip_version < 0x25 || chip_version > 0x28))
4325 bios->fp.dual_link = (pxclk >= bios->fp.duallink_transition_clk);
4326
4327 *dl = bios->fp.dual_link;
4328
4329 return 0;
4330}
4331
Ben Skeggs721b0822011-08-05 13:42:49 +10004332/* BIT 'U'/'d' table encoder subtables have hashes matching them to
4333 * a particular set of encoders.
4334 *
4335 * This function returns true if a particular DCB entry matches.
4336 */
4337bool
4338bios_encoder_match(struct dcb_entry *dcb, u32 hash)
Ben Skeggs6ee73862009-12-11 19:24:15 +10004339{
Ben Skeggs721b0822011-08-05 13:42:49 +10004340 if ((hash & 0x000000f0) != (dcb->location << 4))
4341 return false;
4342 if ((hash & 0x0000000f) != dcb->type)
4343 return false;
4344 if (!(hash & (dcb->or << 16)))
4345 return false;
Ben Skeggs6ee73862009-12-11 19:24:15 +10004346
Ben Skeggs721b0822011-08-05 13:42:49 +10004347 switch (dcb->type) {
Ben Skeggs1eb38102010-06-01 13:40:41 +10004348 case OUTPUT_TMDS:
4349 case OUTPUT_LVDS:
4350 case OUTPUT_DP:
Ben Skeggs721b0822011-08-05 13:42:49 +10004351 if (hash & 0x00c00000) {
4352 if (!(hash & (dcb->sorconf.link << 22)))
4353 return false;
Ben Skeggs1eb38102010-06-01 13:40:41 +10004354 }
Ben Skeggs721b0822011-08-05 13:42:49 +10004355 default:
4356 return true;
Ben Skeggs6ee73862009-12-11 19:24:15 +10004357 }
Ben Skeggs6ee73862009-12-11 19:24:15 +10004358}
4359
Ben Skeggs6ee73862009-12-11 19:24:15 +10004360int
Ben Skeggs02e4f582011-07-06 21:21:42 +10004361nouveau_bios_run_display_table(struct drm_device *dev, u16 type, int pclk,
4362 struct dcb_entry *dcbent, int crtc)
Ben Skeggs6ee73862009-12-11 19:24:15 +10004363{
4364 /*
4365 * The display script table is located by the BIT 'U' table.
4366 *
4367 * It contains an array of pointers to various tables describing
4368 * a particular output type. The first 32-bits of the output
4369 * tables contains similar information to a DCB entry, and is
4370 * used to decide whether that particular table is suitable for
4371 * the output you want to access.
4372 *
4373 * The "record header length" field here seems to indicate the
4374 * offset of the first configuration entry in the output tables.
4375 * This is 10 on most cards I've seen, but 12 has been witnessed
4376 * on DP cards, and there's another script pointer within the
4377 * header.
4378 *
4379 * offset + 0 ( 8 bits): version
4380 * offset + 1 ( 8 bits): header length
4381 * offset + 2 ( 8 bits): record length
4382 * offset + 3 ( 8 bits): number of records
4383 * offset + 4 ( 8 bits): record header length
4384 * offset + 5 (16 bits): pointer to first output script table
4385 */
4386
4387 struct drm_nouveau_private *dev_priv = dev->dev_private;
Ben Skeggs04a39c52010-02-24 10:03:05 +10004388 struct nvbios *bios = &dev_priv->vbios;
Ben Skeggs6ee73862009-12-11 19:24:15 +10004389 uint8_t *table = &bios->data[bios->display.script_table_ptr];
4390 uint8_t *otable = NULL;
4391 uint16_t script;
Ben Skeggs721b0822011-08-05 13:42:49 +10004392 int i;
Ben Skeggs6ee73862009-12-11 19:24:15 +10004393
4394 if (!bios->display.script_table_ptr) {
4395 NV_ERROR(dev, "No pointer to output script table\n");
4396 return 1;
4397 }
4398
4399 /*
4400 * Nothing useful has been in any of the pre-2.0 tables I've seen,
4401 * so until they are, we really don't need to care.
4402 */
4403 if (table[0] < 0x20)
4404 return 1;
4405
4406 if (table[0] != 0x20 && table[0] != 0x21) {
4407 NV_ERROR(dev, "Output script table version 0x%02x unknown\n",
4408 table[0]);
4409 return 1;
4410 }
4411
4412 /*
4413 * The output script tables describing a particular output type
4414 * look as follows:
4415 *
4416 * offset + 0 (32 bits): output this table matches (hash of DCB)
4417 * offset + 4 ( 8 bits): unknown
4418 * offset + 5 ( 8 bits): number of configurations
4419 * offset + 6 (16 bits): pointer to some script
4420 * offset + 8 (16 bits): pointer to some script
4421 *
4422 * headerlen == 10
4423 * offset + 10 : configuration 0
4424 *
4425 * headerlen == 12
4426 * offset + 10 : pointer to some script
4427 * offset + 12 : configuration 0
4428 *
4429 * Each config entry is as follows:
4430 *
4431 * offset + 0 (16 bits): unknown, assumed to be a match value
4432 * offset + 2 (16 bits): pointer to script table (clock set?)
4433 * offset + 4 (16 bits): pointer to script table (reset?)
4434 *
4435 * There doesn't appear to be a count value to say how many
4436 * entries exist in each script table, instead, a 0 value in
4437 * the first 16-bit word seems to indicate both the end of the
4438 * list and the default entry. The second 16-bit word in the
4439 * script tables is a pointer to the script to execute.
4440 */
4441
Maarten Maathuisef2bb502009-12-13 16:53:12 +01004442 NV_DEBUG_KMS(dev, "Searching for output entry for %d %d %d\n",
Ben Skeggs6ee73862009-12-11 19:24:15 +10004443 dcbent->type, dcbent->location, dcbent->or);
Ben Skeggs721b0822011-08-05 13:42:49 +10004444 for (i = 0; i < table[3]; i++) {
Ben Skeggsf9f9f532011-10-12 16:48:48 +10004445 otable = ROMPTR(dev, table[table[1] + (i * table[2])]);
Ben Skeggs721b0822011-08-05 13:42:49 +10004446 if (otable && bios_encoder_match(dcbent, ROM32(otable[0])))
4447 break;
4448 }
4449
Ben Skeggs6ee73862009-12-11 19:24:15 +10004450 if (!otable) {
Ben Skeggs54bf67d2010-08-04 23:09:30 +10004451 NV_DEBUG_KMS(dev, "failed to match any output table\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +10004452 return 1;
4453 }
4454
Ben Skeggs02e4f582011-07-06 21:21:42 +10004455 if (pclk < -2 || pclk > 0) {
Ben Skeggs6ee73862009-12-11 19:24:15 +10004456 /* Try to find matching script table entry */
4457 for (i = 0; i < otable[5]; i++) {
Ben Skeggs02e4f582011-07-06 21:21:42 +10004458 if (ROM16(otable[table[4] + i*6]) == type)
Ben Skeggs6ee73862009-12-11 19:24:15 +10004459 break;
4460 }
4461
4462 if (i == otable[5]) {
4463 NV_ERROR(dev, "Table 0x%04x not found for %d/%d, "
4464 "using first\n",
Ben Skeggs02e4f582011-07-06 21:21:42 +10004465 type, dcbent->type, dcbent->or);
Ben Skeggs6ee73862009-12-11 19:24:15 +10004466 i = 0;
4467 }
4468 }
4469
Ben Skeggs02e4f582011-07-06 21:21:42 +10004470 if (pclk == 0) {
Ben Skeggs6ee73862009-12-11 19:24:15 +10004471 script = ROM16(otable[6]);
4472 if (!script) {
Maarten Maathuisef2bb502009-12-13 16:53:12 +01004473 NV_DEBUG_KMS(dev, "output script 0 not found\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +10004474 return 1;
4475 }
4476
Ben Skeggs45a68a02010-08-13 08:37:55 +10004477 NV_DEBUG_KMS(dev, "0x%04X: parsing output script 0\n", script);
Ben Skeggs02e4f582011-07-06 21:21:42 +10004478 nouveau_bios_run_init_table(dev, script, dcbent, crtc);
Ben Skeggs6ee73862009-12-11 19:24:15 +10004479 } else
Ben Skeggs02e4f582011-07-06 21:21:42 +10004480 if (pclk == -1) {
Ben Skeggs6ee73862009-12-11 19:24:15 +10004481 script = ROM16(otable[8]);
4482 if (!script) {
Maarten Maathuisef2bb502009-12-13 16:53:12 +01004483 NV_DEBUG_KMS(dev, "output script 1 not found\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +10004484 return 1;
4485 }
4486
Ben Skeggs45a68a02010-08-13 08:37:55 +10004487 NV_DEBUG_KMS(dev, "0x%04X: parsing output script 1\n", script);
Ben Skeggs02e4f582011-07-06 21:21:42 +10004488 nouveau_bios_run_init_table(dev, script, dcbent, crtc);
Ben Skeggs6ee73862009-12-11 19:24:15 +10004489 } else
Ben Skeggs02e4f582011-07-06 21:21:42 +10004490 if (pclk == -2) {
Ben Skeggs6ee73862009-12-11 19:24:15 +10004491 if (table[4] >= 12)
4492 script = ROM16(otable[10]);
4493 else
4494 script = 0;
4495 if (!script) {
Maarten Maathuisef2bb502009-12-13 16:53:12 +01004496 NV_DEBUG_KMS(dev, "output script 2 not found\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +10004497 return 1;
4498 }
4499
Ben Skeggs45a68a02010-08-13 08:37:55 +10004500 NV_DEBUG_KMS(dev, "0x%04X: parsing output script 2\n", script);
Ben Skeggs02e4f582011-07-06 21:21:42 +10004501 nouveau_bios_run_init_table(dev, script, dcbent, crtc);
Ben Skeggs6ee73862009-12-11 19:24:15 +10004502 } else
Ben Skeggs02e4f582011-07-06 21:21:42 +10004503 if (pclk > 0) {
Ben Skeggs6ee73862009-12-11 19:24:15 +10004504 script = ROM16(otable[table[4] + i*6 + 2]);
4505 if (script)
Ben Skeggs02e4f582011-07-06 21:21:42 +10004506 script = clkcmptable(bios, script, pclk);
Ben Skeggs6ee73862009-12-11 19:24:15 +10004507 if (!script) {
Ben Skeggs54bf67d2010-08-04 23:09:30 +10004508 NV_DEBUG_KMS(dev, "clock script 0 not found\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +10004509 return 1;
4510 }
4511
Ben Skeggs45a68a02010-08-13 08:37:55 +10004512 NV_DEBUG_KMS(dev, "0x%04X: parsing clock script 0\n", script);
Ben Skeggs02e4f582011-07-06 21:21:42 +10004513 nouveau_bios_run_init_table(dev, script, dcbent, crtc);
Ben Skeggs6ee73862009-12-11 19:24:15 +10004514 } else
Ben Skeggs02e4f582011-07-06 21:21:42 +10004515 if (pclk < 0) {
Ben Skeggs6ee73862009-12-11 19:24:15 +10004516 script = ROM16(otable[table[4] + i*6 + 4]);
4517 if (script)
Ben Skeggs02e4f582011-07-06 21:21:42 +10004518 script = clkcmptable(bios, script, -pclk);
Ben Skeggs6ee73862009-12-11 19:24:15 +10004519 if (!script) {
Maarten Maathuisef2bb502009-12-13 16:53:12 +01004520 NV_DEBUG_KMS(dev, "clock script 1 not found\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +10004521 return 1;
4522 }
4523
Ben Skeggs45a68a02010-08-13 08:37:55 +10004524 NV_DEBUG_KMS(dev, "0x%04X: parsing clock script 1\n", script);
Ben Skeggs02e4f582011-07-06 21:21:42 +10004525 nouveau_bios_run_init_table(dev, script, dcbent, crtc);
Ben Skeggs6ee73862009-12-11 19:24:15 +10004526 }
4527
4528 return 0;
4529}
4530
4531
4532int run_tmds_table(struct drm_device *dev, struct dcb_entry *dcbent, int head, int pxclk)
4533{
4534 /*
4535 * the pxclk parameter is in kHz
4536 *
4537 * This runs the TMDS regs setting code found on BIT bios cards
4538 *
4539 * For ffs(or) == 1 use the first table, for ffs(or) == 2 and
4540 * ffs(or) == 3, use the second.
4541 */
4542
4543 struct drm_nouveau_private *dev_priv = dev->dev_private;
Ben Skeggs04a39c52010-02-24 10:03:05 +10004544 struct nvbios *bios = &dev_priv->vbios;
4545 int cv = bios->chip_version;
Ben Skeggs6ee73862009-12-11 19:24:15 +10004546 uint16_t clktable = 0, scriptptr;
4547 uint32_t sel_clk_binding, sel_clk;
4548
4549 /* pre-nv17 off-chip tmds uses scripts, post nv17 doesn't */
4550 if (cv >= 0x17 && cv != 0x1a && cv != 0x20 &&
4551 dcbent->location != DCB_LOC_ON_CHIP)
4552 return 0;
4553
4554 switch (ffs(dcbent->or)) {
4555 case 1:
4556 clktable = bios->tmds.output0_script_ptr;
4557 break;
4558 case 2:
4559 case 3:
4560 clktable = bios->tmds.output1_script_ptr;
4561 break;
4562 }
4563
4564 if (!clktable) {
4565 NV_ERROR(dev, "Pixel clock comparison table not found\n");
4566 return -EINVAL;
4567 }
4568
4569 scriptptr = clkcmptable(bios, clktable, pxclk);
4570
4571 if (!scriptptr) {
4572 NV_ERROR(dev, "TMDS output init script not found\n");
4573 return -ENOENT;
4574 }
4575
4576 /* don't let script change pll->head binding */
4577 sel_clk_binding = bios_rd32(bios, NV_PRAMDAC_SEL_CLK) & 0x50000;
4578 run_digital_op_script(dev, scriptptr, dcbent, head, pxclk >= 165000);
4579 sel_clk = NVReadRAMDAC(dev, 0, NV_PRAMDAC_SEL_CLK) & ~0x50000;
4580 NVWriteRAMDAC(dev, 0, NV_PRAMDAC_SEL_CLK, sel_clk | sel_clk_binding);
4581
4582 return 0;
4583}
4584
Ben Skeggs855a95e2010-09-16 15:25:25 +10004585struct pll_mapping {
4586 u8 type;
4587 u32 reg;
4588};
4589
4590static struct pll_mapping nv04_pll_mapping[] = {
4591 { PLL_CORE , NV_PRAMDAC_NVPLL_COEFF },
4592 { PLL_MEMORY, NV_PRAMDAC_MPLL_COEFF },
4593 { PLL_VPLL0 , NV_PRAMDAC_VPLL_COEFF },
4594 { PLL_VPLL1 , NV_RAMDAC_VPLL2 },
4595 {}
4596};
4597
4598static struct pll_mapping nv40_pll_mapping[] = {
4599 { PLL_CORE , 0x004000 },
4600 { PLL_MEMORY, 0x004020 },
4601 { PLL_VPLL0 , NV_PRAMDAC_VPLL_COEFF },
4602 { PLL_VPLL1 , NV_RAMDAC_VPLL2 },
4603 {}
4604};
4605
4606static struct pll_mapping nv50_pll_mapping[] = {
4607 { PLL_CORE , 0x004028 },
4608 { PLL_SHADER, 0x004020 },
4609 { PLL_UNK03 , 0x004000 },
4610 { PLL_MEMORY, 0x004008 },
4611 { PLL_UNK40 , 0x00e810 },
4612 { PLL_UNK41 , 0x00e818 },
4613 { PLL_UNK42 , 0x00e824 },
4614 { PLL_VPLL0 , 0x614100 },
4615 { PLL_VPLL1 , 0x614900 },
4616 {}
4617};
4618
4619static struct pll_mapping nv84_pll_mapping[] = {
4620 { PLL_CORE , 0x004028 },
4621 { PLL_SHADER, 0x004020 },
4622 { PLL_MEMORY, 0x004008 },
Martin Peresd4cca9e2011-10-06 23:47:58 +02004623 { PLL_VDEC , 0x004030 },
Ben Skeggs855a95e2010-09-16 15:25:25 +10004624 { PLL_UNK41 , 0x00e818 },
4625 { PLL_VPLL0 , 0x614100 },
4626 { PLL_VPLL1 , 0x614900 },
4627 {}
4628};
4629
4630u32
4631get_pll_register(struct drm_device *dev, enum pll_types type)
4632{
4633 struct drm_nouveau_private *dev_priv = dev->dev_private;
4634 struct nvbios *bios = &dev_priv->vbios;
4635 struct pll_mapping *map;
4636 int i;
4637
4638 if (dev_priv->card_type < NV_40)
4639 map = nv04_pll_mapping;
4640 else
4641 if (dev_priv->card_type < NV_50)
4642 map = nv40_pll_mapping;
4643 else {
4644 u8 *plim = &bios->data[bios->pll_limit_tbl_ptr];
4645
Ben Skeggs56edd962010-09-24 09:15:50 +10004646 if (plim[0] >= 0x30) {
Ben Skeggs855a95e2010-09-16 15:25:25 +10004647 u8 *entry = plim + plim[1];
4648 for (i = 0; i < plim[3]; i++, entry += plim[2]) {
4649 if (entry[0] == type)
4650 return ROM32(entry[3]);
4651 }
4652
4653 return 0;
4654 }
4655
4656 if (dev_priv->chipset == 0x50)
4657 map = nv50_pll_mapping;
4658 else
4659 map = nv84_pll_mapping;
4660 }
4661
4662 while (map->reg) {
4663 if (map->type == type)
4664 return map->reg;
4665 map++;
4666 }
4667
4668 return 0;
4669}
4670
Ben Skeggs6ee73862009-12-11 19:24:15 +10004671int get_pll_limits(struct drm_device *dev, uint32_t limit_match, struct pll_lims *pll_lim)
4672{
4673 /*
4674 * PLL limits table
4675 *
4676 * Version 0x10: NV30, NV31
4677 * One byte header (version), one record of 24 bytes
4678 * Version 0x11: NV36 - Not implemented
4679 * Seems to have same record style as 0x10, but 3 records rather than 1
4680 * Version 0x20: Found on Geforce 6 cards
4681 * Trivial 4 byte BIT header. 31 (0x1f) byte record length
4682 * Version 0x21: Found on Geforce 7, 8 and some Geforce 6 cards
4683 * 5 byte header, fifth byte of unknown purpose. 35 (0x23) byte record
4684 * length in general, some (integrated) have an extra configuration byte
4685 * Version 0x30: Found on Geforce 8, separates the register mapping
4686 * from the limits tables.
4687 */
4688
4689 struct drm_nouveau_private *dev_priv = dev->dev_private;
Ben Skeggs04a39c52010-02-24 10:03:05 +10004690 struct nvbios *bios = &dev_priv->vbios;
4691 int cv = bios->chip_version, pllindex = 0;
Ben Skeggs6ee73862009-12-11 19:24:15 +10004692 uint8_t pll_lim_ver = 0, headerlen = 0, recordlen = 0, entries = 0;
4693 uint32_t crystal_strap_mask, crystal_straps;
4694
4695 if (!bios->pll_limit_tbl_ptr) {
4696 if (cv == 0x30 || cv == 0x31 || cv == 0x35 || cv == 0x36 ||
4697 cv >= 0x40) {
4698 NV_ERROR(dev, "Pointer to PLL limits table invalid\n");
4699 return -EINVAL;
4700 }
4701 } else
4702 pll_lim_ver = bios->data[bios->pll_limit_tbl_ptr];
4703
4704 crystal_strap_mask = 1 << 6;
4705 /* open coded dev->twoHeads test */
4706 if (cv > 0x10 && cv != 0x15 && cv != 0x1a && cv != 0x20)
4707 crystal_strap_mask |= 1 << 22;
4708 crystal_straps = nvReadEXTDEV(dev, NV_PEXTDEV_BOOT_0) &
4709 crystal_strap_mask;
4710
4711 switch (pll_lim_ver) {
4712 /*
4713 * We use version 0 to indicate a pre limit table bios (single stage
4714 * pll) and load the hard coded limits instead.
4715 */
4716 case 0:
4717 break;
4718 case 0x10:
4719 case 0x11:
4720 /*
4721 * Strictly v0x11 has 3 entries, but the last two don't seem
4722 * to get used.
4723 */
4724 headerlen = 1;
4725 recordlen = 0x18;
4726 entries = 1;
4727 pllindex = 0;
4728 break;
4729 case 0x20:
4730 case 0x21:
4731 case 0x30:
4732 case 0x40:
4733 headerlen = bios->data[bios->pll_limit_tbl_ptr + 1];
4734 recordlen = bios->data[bios->pll_limit_tbl_ptr + 2];
4735 entries = bios->data[bios->pll_limit_tbl_ptr + 3];
4736 break;
4737 default:
4738 NV_ERROR(dev, "PLL limits table revision 0x%X not currently "
4739 "supported\n", pll_lim_ver);
4740 return -ENOSYS;
4741 }
4742
4743 /* initialize all members to zero */
4744 memset(pll_lim, 0, sizeof(struct pll_lims));
4745
Ben Skeggs855a95e2010-09-16 15:25:25 +10004746 /* if we were passed a type rather than a register, figure
4747 * out the register and store it
4748 */
4749 if (limit_match > PLL_MAX)
4750 pll_lim->reg = limit_match;
Ben Skeggs6f876982010-09-16 16:47:14 +10004751 else {
Ben Skeggs855a95e2010-09-16 15:25:25 +10004752 pll_lim->reg = get_pll_register(dev, limit_match);
Ben Skeggs6f876982010-09-16 16:47:14 +10004753 if (!pll_lim->reg)
4754 return -ENOENT;
4755 }
Ben Skeggs855a95e2010-09-16 15:25:25 +10004756
Ben Skeggs6ee73862009-12-11 19:24:15 +10004757 if (pll_lim_ver == 0x10 || pll_lim_ver == 0x11) {
4758 uint8_t *pll_rec = &bios->data[bios->pll_limit_tbl_ptr + headerlen + recordlen * pllindex];
4759
4760 pll_lim->vco1.minfreq = ROM32(pll_rec[0]);
4761 pll_lim->vco1.maxfreq = ROM32(pll_rec[4]);
4762 pll_lim->vco2.minfreq = ROM32(pll_rec[8]);
4763 pll_lim->vco2.maxfreq = ROM32(pll_rec[12]);
4764 pll_lim->vco1.min_inputfreq = ROM32(pll_rec[16]);
4765 pll_lim->vco2.min_inputfreq = ROM32(pll_rec[20]);
4766 pll_lim->vco1.max_inputfreq = pll_lim->vco2.max_inputfreq = INT_MAX;
4767
4768 /* these values taken from nv30/31/36 */
4769 pll_lim->vco1.min_n = 0x1;
4770 if (cv == 0x36)
4771 pll_lim->vco1.min_n = 0x5;
4772 pll_lim->vco1.max_n = 0xff;
4773 pll_lim->vco1.min_m = 0x1;
4774 pll_lim->vco1.max_m = 0xd;
4775 pll_lim->vco2.min_n = 0x4;
4776 /*
4777 * On nv30, 31, 36 (i.e. all cards with two stage PLLs with this
4778 * table version (apart from nv35)), N2 is compared to
4779 * maxN2 (0x46) and 10 * maxM2 (0x4), so set maxN2 to 0x28 and
4780 * save a comparison
4781 */
4782 pll_lim->vco2.max_n = 0x28;
4783 if (cv == 0x30 || cv == 0x35)
4784 /* only 5 bits available for N2 on nv30/35 */
4785 pll_lim->vco2.max_n = 0x1f;
4786 pll_lim->vco2.min_m = 0x1;
4787 pll_lim->vco2.max_m = 0x4;
4788 pll_lim->max_log2p = 0x7;
4789 pll_lim->max_usable_log2p = 0x6;
4790 } else if (pll_lim_ver == 0x20 || pll_lim_ver == 0x21) {
4791 uint16_t plloffs = bios->pll_limit_tbl_ptr + headerlen;
Ben Skeggs6ee73862009-12-11 19:24:15 +10004792 uint8_t *pll_rec;
4793 int i;
4794
4795 /*
4796 * First entry is default match, if nothing better. warn if
4797 * reg field nonzero
4798 */
4799 if (ROM32(bios->data[plloffs]))
4800 NV_WARN(dev, "Default PLL limit entry has non-zero "
4801 "register field\n");
4802
Ben Skeggs6ee73862009-12-11 19:24:15 +10004803 for (i = 1; i < entries; i++)
Ben Skeggs855a95e2010-09-16 15:25:25 +10004804 if (ROM32(bios->data[plloffs + recordlen * i]) == pll_lim->reg) {
Ben Skeggs6ee73862009-12-11 19:24:15 +10004805 pllindex = i;
4806 break;
4807 }
4808
Emil Velikoveadc69c2010-09-26 20:26:02 +01004809 if ((dev_priv->card_type >= NV_50) && (pllindex == 0)) {
4810 NV_ERROR(dev, "Register 0x%08x not found in PLL "
4811 "limits table", pll_lim->reg);
4812 return -ENOENT;
4813 }
4814
Ben Skeggs6ee73862009-12-11 19:24:15 +10004815 pll_rec = &bios->data[plloffs + recordlen * pllindex];
4816
4817 BIOSLOG(bios, "Loading PLL limits for reg 0x%08x\n",
Ben Skeggs855a95e2010-09-16 15:25:25 +10004818 pllindex ? pll_lim->reg : 0);
Ben Skeggs6ee73862009-12-11 19:24:15 +10004819
4820 /*
4821 * Frequencies are stored in tables in MHz, kHz are more
4822 * useful, so we convert.
4823 */
4824
4825 /* What output frequencies can each VCO generate? */
4826 pll_lim->vco1.minfreq = ROM16(pll_rec[4]) * 1000;
4827 pll_lim->vco1.maxfreq = ROM16(pll_rec[6]) * 1000;
4828 pll_lim->vco2.minfreq = ROM16(pll_rec[8]) * 1000;
4829 pll_lim->vco2.maxfreq = ROM16(pll_rec[10]) * 1000;
4830
4831 /* What input frequencies they accept (past the m-divider)? */
4832 pll_lim->vco1.min_inputfreq = ROM16(pll_rec[12]) * 1000;
4833 pll_lim->vco2.min_inputfreq = ROM16(pll_rec[14]) * 1000;
4834 pll_lim->vco1.max_inputfreq = ROM16(pll_rec[16]) * 1000;
4835 pll_lim->vco2.max_inputfreq = ROM16(pll_rec[18]) * 1000;
4836
4837 /* What values are accepted as multiplier and divider? */
4838 pll_lim->vco1.min_n = pll_rec[20];
4839 pll_lim->vco1.max_n = pll_rec[21];
4840 pll_lim->vco1.min_m = pll_rec[22];
4841 pll_lim->vco1.max_m = pll_rec[23];
4842 pll_lim->vco2.min_n = pll_rec[24];
4843 pll_lim->vco2.max_n = pll_rec[25];
4844 pll_lim->vco2.min_m = pll_rec[26];
4845 pll_lim->vco2.max_m = pll_rec[27];
4846
4847 pll_lim->max_usable_log2p = pll_lim->max_log2p = pll_rec[29];
4848 if (pll_lim->max_log2p > 0x7)
4849 /* pll decoding in nv_hw.c assumes never > 7 */
4850 NV_WARN(dev, "Max log2 P value greater than 7 (%d)\n",
4851 pll_lim->max_log2p);
4852 if (cv < 0x60)
4853 pll_lim->max_usable_log2p = 0x6;
4854 pll_lim->log2p_bias = pll_rec[30];
4855
4856 if (recordlen > 0x22)
4857 pll_lim->refclk = ROM32(pll_rec[31]);
4858
4859 if (recordlen > 0x23 && pll_rec[35])
4860 NV_WARN(dev,
4861 "Bits set in PLL configuration byte (%x)\n",
4862 pll_rec[35]);
4863
4864 /* C51 special not seen elsewhere */
4865 if (cv == 0x51 && !pll_lim->refclk) {
4866 uint32_t sel_clk = bios_rd32(bios, NV_PRAMDAC_SEL_CLK);
4867
Ben Skeggs855a95e2010-09-16 15:25:25 +10004868 if ((pll_lim->reg == NV_PRAMDAC_VPLL_COEFF && sel_clk & 0x20) ||
4869 (pll_lim->reg == NV_RAMDAC_VPLL2 && sel_clk & 0x80)) {
Ben Skeggs6ee73862009-12-11 19:24:15 +10004870 if (bios_idxprt_rd(bios, NV_CIO_CRX__COLOR, NV_CIO_CRE_CHIP_ID_INDEX) < 0xa3)
4871 pll_lim->refclk = 200000;
4872 else
4873 pll_lim->refclk = 25000;
4874 }
4875 }
4876 } else if (pll_lim_ver == 0x30) { /* ver 0x30 */
4877 uint8_t *entry = &bios->data[bios->pll_limit_tbl_ptr + headerlen];
4878 uint8_t *record = NULL;
4879 int i;
4880
4881 BIOSLOG(bios, "Loading PLL limits for register 0x%08x\n",
Ben Skeggs855a95e2010-09-16 15:25:25 +10004882 pll_lim->reg);
Ben Skeggs6ee73862009-12-11 19:24:15 +10004883
4884 for (i = 0; i < entries; i++, entry += recordlen) {
Ben Skeggs855a95e2010-09-16 15:25:25 +10004885 if (ROM32(entry[3]) == pll_lim->reg) {
Ben Skeggs6ee73862009-12-11 19:24:15 +10004886 record = &bios->data[ROM16(entry[1])];
4887 break;
4888 }
4889 }
4890
4891 if (!record) {
4892 NV_ERROR(dev, "Register 0x%08x not found in PLL "
Ben Skeggs855a95e2010-09-16 15:25:25 +10004893 "limits table", pll_lim->reg);
Ben Skeggs6ee73862009-12-11 19:24:15 +10004894 return -ENOENT;
4895 }
4896
4897 pll_lim->vco1.minfreq = ROM16(record[0]) * 1000;
4898 pll_lim->vco1.maxfreq = ROM16(record[2]) * 1000;
4899 pll_lim->vco2.minfreq = ROM16(record[4]) * 1000;
4900 pll_lim->vco2.maxfreq = ROM16(record[6]) * 1000;
4901 pll_lim->vco1.min_inputfreq = ROM16(record[8]) * 1000;
4902 pll_lim->vco2.min_inputfreq = ROM16(record[10]) * 1000;
4903 pll_lim->vco1.max_inputfreq = ROM16(record[12]) * 1000;
4904 pll_lim->vco2.max_inputfreq = ROM16(record[14]) * 1000;
4905 pll_lim->vco1.min_n = record[16];
4906 pll_lim->vco1.max_n = record[17];
4907 pll_lim->vco1.min_m = record[18];
4908 pll_lim->vco1.max_m = record[19];
4909 pll_lim->vco2.min_n = record[20];
4910 pll_lim->vco2.max_n = record[21];
4911 pll_lim->vco2.min_m = record[22];
4912 pll_lim->vco2.max_m = record[23];
4913 pll_lim->max_usable_log2p = pll_lim->max_log2p = record[25];
4914 pll_lim->log2p_bias = record[27];
4915 pll_lim->refclk = ROM32(record[28]);
4916 } else if (pll_lim_ver) { /* ver 0x40 */
4917 uint8_t *entry = &bios->data[bios->pll_limit_tbl_ptr + headerlen];
4918 uint8_t *record = NULL;
4919 int i;
4920
4921 BIOSLOG(bios, "Loading PLL limits for register 0x%08x\n",
Ben Skeggs855a95e2010-09-16 15:25:25 +10004922 pll_lim->reg);
Ben Skeggs6ee73862009-12-11 19:24:15 +10004923
4924 for (i = 0; i < entries; i++, entry += recordlen) {
Ben Skeggs855a95e2010-09-16 15:25:25 +10004925 if (ROM32(entry[3]) == pll_lim->reg) {
Ben Skeggs6ee73862009-12-11 19:24:15 +10004926 record = &bios->data[ROM16(entry[1])];
4927 break;
4928 }
4929 }
4930
4931 if (!record) {
4932 NV_ERROR(dev, "Register 0x%08x not found in PLL "
Ben Skeggs855a95e2010-09-16 15:25:25 +10004933 "limits table", pll_lim->reg);
Ben Skeggs6ee73862009-12-11 19:24:15 +10004934 return -ENOENT;
4935 }
4936
4937 pll_lim->vco1.minfreq = ROM16(record[0]) * 1000;
4938 pll_lim->vco1.maxfreq = ROM16(record[2]) * 1000;
4939 pll_lim->vco1.min_inputfreq = ROM16(record[4]) * 1000;
4940 pll_lim->vco1.max_inputfreq = ROM16(record[6]) * 1000;
4941 pll_lim->vco1.min_m = record[8];
4942 pll_lim->vco1.max_m = record[9];
4943 pll_lim->vco1.min_n = record[10];
4944 pll_lim->vco1.max_n = record[11];
4945 pll_lim->min_p = record[12];
4946 pll_lim->max_p = record[13];
Ben Skeggsce521842011-04-14 11:25:26 +10004947 pll_lim->refclk = ROM16(entry[9]) * 1000;
Ben Skeggs6ee73862009-12-11 19:24:15 +10004948 }
4949
4950 /*
4951 * By now any valid limit table ought to have set a max frequency for
4952 * vco1, so if it's zero it's either a pre limit table bios, or one
4953 * with an empty limit table (seen on nv18)
4954 */
4955 if (!pll_lim->vco1.maxfreq) {
4956 pll_lim->vco1.minfreq = bios->fminvco;
4957 pll_lim->vco1.maxfreq = bios->fmaxvco;
4958 pll_lim->vco1.min_inputfreq = 0;
4959 pll_lim->vco1.max_inputfreq = INT_MAX;
4960 pll_lim->vco1.min_n = 0x1;
4961 pll_lim->vco1.max_n = 0xff;
4962 pll_lim->vco1.min_m = 0x1;
4963 if (crystal_straps == 0) {
4964 /* nv05 does this, nv11 doesn't, nv10 unknown */
4965 if (cv < 0x11)
4966 pll_lim->vco1.min_m = 0x7;
4967 pll_lim->vco1.max_m = 0xd;
4968 } else {
4969 if (cv < 0x11)
4970 pll_lim->vco1.min_m = 0x8;
4971 pll_lim->vco1.max_m = 0xe;
4972 }
4973 if (cv < 0x17 || cv == 0x1a || cv == 0x20)
4974 pll_lim->max_log2p = 4;
4975 else
4976 pll_lim->max_log2p = 5;
4977 pll_lim->max_usable_log2p = pll_lim->max_log2p;
4978 }
4979
4980 if (!pll_lim->refclk)
4981 switch (crystal_straps) {
4982 case 0:
4983 pll_lim->refclk = 13500;
4984 break;
4985 case (1 << 6):
4986 pll_lim->refclk = 14318;
4987 break;
4988 case (1 << 22):
4989 pll_lim->refclk = 27000;
4990 break;
4991 case (1 << 22 | 1 << 6):
4992 pll_lim->refclk = 25000;
4993 break;
4994 }
4995
Ben Skeggs4c389f02010-04-23 03:08:02 +10004996 NV_DEBUG(dev, "pll.vco1.minfreq: %d\n", pll_lim->vco1.minfreq);
4997 NV_DEBUG(dev, "pll.vco1.maxfreq: %d\n", pll_lim->vco1.maxfreq);
4998 NV_DEBUG(dev, "pll.vco1.min_inputfreq: %d\n", pll_lim->vco1.min_inputfreq);
4999 NV_DEBUG(dev, "pll.vco1.max_inputfreq: %d\n", pll_lim->vco1.max_inputfreq);
5000 NV_DEBUG(dev, "pll.vco1.min_n: %d\n", pll_lim->vco1.min_n);
5001 NV_DEBUG(dev, "pll.vco1.max_n: %d\n", pll_lim->vco1.max_n);
5002 NV_DEBUG(dev, "pll.vco1.min_m: %d\n", pll_lim->vco1.min_m);
5003 NV_DEBUG(dev, "pll.vco1.max_m: %d\n", pll_lim->vco1.max_m);
5004 if (pll_lim->vco2.maxfreq) {
5005 NV_DEBUG(dev, "pll.vco2.minfreq: %d\n", pll_lim->vco2.minfreq);
5006 NV_DEBUG(dev, "pll.vco2.maxfreq: %d\n", pll_lim->vco2.maxfreq);
5007 NV_DEBUG(dev, "pll.vco2.min_inputfreq: %d\n", pll_lim->vco2.min_inputfreq);
5008 NV_DEBUG(dev, "pll.vco2.max_inputfreq: %d\n", pll_lim->vco2.max_inputfreq);
5009 NV_DEBUG(dev, "pll.vco2.min_n: %d\n", pll_lim->vco2.min_n);
5010 NV_DEBUG(dev, "pll.vco2.max_n: %d\n", pll_lim->vco2.max_n);
5011 NV_DEBUG(dev, "pll.vco2.min_m: %d\n", pll_lim->vco2.min_m);
5012 NV_DEBUG(dev, "pll.vco2.max_m: %d\n", pll_lim->vco2.max_m);
5013 }
5014 if (!pll_lim->max_p) {
5015 NV_DEBUG(dev, "pll.max_log2p: %d\n", pll_lim->max_log2p);
5016 NV_DEBUG(dev, "pll.log2p_bias: %d\n", pll_lim->log2p_bias);
5017 } else {
5018 NV_DEBUG(dev, "pll.min_p: %d\n", pll_lim->min_p);
5019 NV_DEBUG(dev, "pll.max_p: %d\n", pll_lim->max_p);
5020 }
5021 NV_DEBUG(dev, "pll.refclk: %d\n", pll_lim->refclk);
Ben Skeggs6ee73862009-12-11 19:24:15 +10005022
5023 return 0;
5024}
5025
5026static void parse_bios_version(struct drm_device *dev, struct nvbios *bios, uint16_t offset)
5027{
5028 /*
5029 * offset + 0 (8 bits): Micro version
5030 * offset + 1 (8 bits): Minor version
5031 * offset + 2 (8 bits): Chip version
5032 * offset + 3 (8 bits): Major version
5033 */
5034
5035 bios->major_version = bios->data[offset + 3];
Ben Skeggs04a39c52010-02-24 10:03:05 +10005036 bios->chip_version = bios->data[offset + 2];
Ben Skeggs6ee73862009-12-11 19:24:15 +10005037 NV_TRACE(dev, "Bios version %02x.%02x.%02x.%02x\n",
5038 bios->data[offset + 3], bios->data[offset + 2],
5039 bios->data[offset + 1], bios->data[offset]);
5040}
5041
5042static void parse_script_table_pointers(struct nvbios *bios, uint16_t offset)
5043{
5044 /*
5045 * Parses the init table segment for pointers used in script execution.
5046 *
5047 * offset + 0 (16 bits): init script tables pointer
5048 * offset + 2 (16 bits): macro index table pointer
5049 * offset + 4 (16 bits): macro table pointer
5050 * offset + 6 (16 bits): condition table pointer
5051 * offset + 8 (16 bits): io condition table pointer
5052 * offset + 10 (16 bits): io flag condition table pointer
5053 * offset + 12 (16 bits): init function table pointer
5054 */
5055
5056 bios->init_script_tbls_ptr = ROM16(bios->data[offset]);
5057 bios->macro_index_tbl_ptr = ROM16(bios->data[offset + 2]);
5058 bios->macro_tbl_ptr = ROM16(bios->data[offset + 4]);
5059 bios->condition_tbl_ptr = ROM16(bios->data[offset + 6]);
5060 bios->io_condition_tbl_ptr = ROM16(bios->data[offset + 8]);
5061 bios->io_flag_condition_tbl_ptr = ROM16(bios->data[offset + 10]);
5062 bios->init_function_tbl_ptr = ROM16(bios->data[offset + 12]);
5063}
5064
5065static int parse_bit_A_tbl_entry(struct drm_device *dev, struct nvbios *bios, struct bit_entry *bitentry)
5066{
5067 /*
5068 * Parses the load detect values for g80 cards.
5069 *
5070 * offset + 0 (16 bits): loadval table pointer
5071 */
5072
5073 uint16_t load_table_ptr;
5074 uint8_t version, headerlen, entrylen, num_entries;
5075
5076 if (bitentry->length != 3) {
5077 NV_ERROR(dev, "Do not understand BIT A table\n");
5078 return -EINVAL;
5079 }
5080
5081 load_table_ptr = ROM16(bios->data[bitentry->offset]);
5082
5083 if (load_table_ptr == 0x0) {
Ben Skeggs1562ffd2011-06-01 14:11:10 +10005084 NV_DEBUG(dev, "Pointer to BIT loadval table invalid\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +10005085 return -EINVAL;
5086 }
5087
5088 version = bios->data[load_table_ptr];
5089
5090 if (version != 0x10) {
5091 NV_ERROR(dev, "BIT loadval table version %d.%d not supported\n",
5092 version >> 4, version & 0xF);
5093 return -ENOSYS;
5094 }
5095
5096 headerlen = bios->data[load_table_ptr + 1];
5097 entrylen = bios->data[load_table_ptr + 2];
5098 num_entries = bios->data[load_table_ptr + 3];
5099
5100 if (headerlen != 4 || entrylen != 4 || num_entries != 2) {
5101 NV_ERROR(dev, "Do not understand BIT loadval table\n");
5102 return -EINVAL;
5103 }
5104
5105 /* First entry is normal dac, 2nd tv-out perhaps? */
Ben Skeggs04a39c52010-02-24 10:03:05 +10005106 bios->dactestval = ROM32(bios->data[load_table_ptr + headerlen]) & 0x3ff;
Ben Skeggs6ee73862009-12-11 19:24:15 +10005107
5108 return 0;
5109}
5110
5111static int parse_bit_C_tbl_entry(struct drm_device *dev, struct nvbios *bios, struct bit_entry *bitentry)
5112{
5113 /*
5114 * offset + 8 (16 bits): PLL limits table pointer
5115 *
5116 * There's more in here, but that's unknown.
5117 */
5118
5119 if (bitentry->length < 10) {
5120 NV_ERROR(dev, "Do not understand BIT C table\n");
5121 return -EINVAL;
5122 }
5123
5124 bios->pll_limit_tbl_ptr = ROM16(bios->data[bitentry->offset + 8]);
5125
5126 return 0;
5127}
5128
5129static int parse_bit_display_tbl_entry(struct drm_device *dev, struct nvbios *bios, struct bit_entry *bitentry)
5130{
5131 /*
5132 * Parses the flat panel table segment that the bit entry points to.
5133 * Starting at bitentry->offset:
5134 *
5135 * offset + 0 (16 bits): ??? table pointer - seems to have 18 byte
5136 * records beginning with a freq.
5137 * offset + 2 (16 bits): mode table pointer
5138 */
5139
5140 if (bitentry->length != 4) {
5141 NV_ERROR(dev, "Do not understand BIT display table\n");
5142 return -EINVAL;
5143 }
5144
5145 bios->fp.fptablepointer = ROM16(bios->data[bitentry->offset + 2]);
5146
5147 return 0;
5148}
5149
5150static int parse_bit_init_tbl_entry(struct drm_device *dev, struct nvbios *bios, struct bit_entry *bitentry)
5151{
5152 /*
5153 * Parses the init table segment that the bit entry points to.
5154 *
5155 * See parse_script_table_pointers for layout
5156 */
5157
5158 if (bitentry->length < 14) {
5159 NV_ERROR(dev, "Do not understand init table\n");
5160 return -EINVAL;
5161 }
5162
5163 parse_script_table_pointers(bios, bitentry->offset);
5164
5165 if (bitentry->length >= 16)
5166 bios->some_script_ptr = ROM16(bios->data[bitentry->offset + 14]);
5167 if (bitentry->length >= 18)
5168 bios->init96_tbl_ptr = ROM16(bios->data[bitentry->offset + 16]);
5169
5170 return 0;
5171}
5172
5173static int parse_bit_i_tbl_entry(struct drm_device *dev, struct nvbios *bios, struct bit_entry *bitentry)
5174{
5175 /*
5176 * BIT 'i' (info?) table
5177 *
5178 * offset + 0 (32 bits): BIOS version dword (as in B table)
5179 * offset + 5 (8 bits): BIOS feature byte (same as for BMP?)
5180 * offset + 13 (16 bits): pointer to table containing DAC load
5181 * detection comparison values
5182 *
5183 * There's other things in the table, purpose unknown
5184 */
5185
5186 uint16_t daccmpoffset;
5187 uint8_t dacver, dacheaderlen;
5188
5189 if (bitentry->length < 6) {
5190 NV_ERROR(dev, "BIT i table too short for needed information\n");
5191 return -EINVAL;
5192 }
5193
5194 parse_bios_version(dev, bios, bitentry->offset);
5195
5196 /*
5197 * bit 4 seems to indicate a mobile bios (doesn't suffer from BMP's
5198 * Quadro identity crisis), other bits possibly as for BMP feature byte
5199 */
5200 bios->feature_byte = bios->data[bitentry->offset + 5];
5201 bios->is_mobile = bios->feature_byte & FEATURE_MOBILE;
5202
5203 if (bitentry->length < 15) {
5204 NV_WARN(dev, "BIT i table not long enough for DAC load "
5205 "detection comparison table\n");
5206 return -EINVAL;
5207 }
5208
5209 daccmpoffset = ROM16(bios->data[bitentry->offset + 13]);
5210
5211 /* doesn't exist on g80 */
5212 if (!daccmpoffset)
5213 return 0;
5214
5215 /*
5216 * The first value in the table, following the header, is the
5217 * comparison value, the second entry is a comparison value for
5218 * TV load detection.
5219 */
5220
5221 dacver = bios->data[daccmpoffset];
5222 dacheaderlen = bios->data[daccmpoffset + 1];
5223
5224 if (dacver != 0x00 && dacver != 0x10) {
5225 NV_WARN(dev, "DAC load detection comparison table version "
5226 "%d.%d not known\n", dacver >> 4, dacver & 0xf);
5227 return -ENOSYS;
5228 }
5229
Ben Skeggs04a39c52010-02-24 10:03:05 +10005230 bios->dactestval = ROM32(bios->data[daccmpoffset + dacheaderlen]);
5231 bios->tvdactestval = ROM32(bios->data[daccmpoffset + dacheaderlen + 4]);
Ben Skeggs6ee73862009-12-11 19:24:15 +10005232
5233 return 0;
5234}
5235
5236static int parse_bit_lvds_tbl_entry(struct drm_device *dev, struct nvbios *bios, struct bit_entry *bitentry)
5237{
5238 /*
5239 * Parses the LVDS table segment that the bit entry points to.
5240 * Starting at bitentry->offset:
5241 *
5242 * offset + 0 (16 bits): LVDS strap xlate table pointer
5243 */
5244
5245 if (bitentry->length != 2) {
5246 NV_ERROR(dev, "Do not understand BIT LVDS table\n");
5247 return -EINVAL;
5248 }
5249
5250 /*
5251 * No idea if it's still called the LVDS manufacturer table, but
5252 * the concept's close enough.
5253 */
5254 bios->fp.lvdsmanufacturerpointer = ROM16(bios->data[bitentry->offset]);
5255
5256 return 0;
5257}
5258
5259static int
5260parse_bit_M_tbl_entry(struct drm_device *dev, struct nvbios *bios,
5261 struct bit_entry *bitentry)
5262{
5263 /*
5264 * offset + 2 (8 bits): number of options in an
5265 * INIT_RAM_RESTRICT_ZM_REG_GROUP opcode option set
5266 * offset + 3 (16 bits): pointer to strap xlate table for RAM
5267 * restrict option selection
5268 *
5269 * There's a bunch of bits in this table other than the RAM restrict
5270 * stuff that we don't use - their use currently unknown
5271 */
5272
Ben Skeggs6ee73862009-12-11 19:24:15 +10005273 /*
5274 * Older bios versions don't have a sufficiently long table for
5275 * what we want
5276 */
5277 if (bitentry->length < 0x5)
5278 return 0;
5279
Ben Skeggs4709bff2010-09-13 15:18:40 +10005280 if (bitentry->version < 2) {
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00005281 bios->ram_restrict_group_count = bios->data[bitentry->offset + 2];
5282 bios->ram_restrict_tbl_ptr = ROM16(bios->data[bitentry->offset + 3]);
Ben Skeggs6ee73862009-12-11 19:24:15 +10005283 } else {
Marcin Koƛcielnicki37383652009-12-15 00:37:31 +00005284 bios->ram_restrict_group_count = bios->data[bitentry->offset + 0];
5285 bios->ram_restrict_tbl_ptr = ROM16(bios->data[bitentry->offset + 1]);
Ben Skeggs6ee73862009-12-11 19:24:15 +10005286 }
5287
Ben Skeggs6ee73862009-12-11 19:24:15 +10005288 return 0;
5289}
5290
5291static int parse_bit_tmds_tbl_entry(struct drm_device *dev, struct nvbios *bios, struct bit_entry *bitentry)
5292{
5293 /*
5294 * Parses the pointer to the TMDS table
5295 *
5296 * Starting at bitentry->offset:
5297 *
5298 * offset + 0 (16 bits): TMDS table pointer
5299 *
5300 * The TMDS table is typically found just before the DCB table, with a
5301 * characteristic signature of 0x11,0x13 (1.1 being version, 0x13 being
5302 * length?)
5303 *
5304 * At offset +7 is a pointer to a script, which I don't know how to
5305 * run yet.
5306 * At offset +9 is a pointer to another script, likewise
5307 * Offset +11 has a pointer to a table where the first word is a pxclk
5308 * frequency and the second word a pointer to a script, which should be
5309 * run if the comparison pxclk frequency is less than the pxclk desired.
5310 * This repeats for decreasing comparison frequencies
5311 * Offset +13 has a pointer to a similar table
5312 * The selection of table (and possibly +7/+9 script) is dictated by
5313 * "or" from the DCB.
5314 */
5315
5316 uint16_t tmdstableptr, script1, script2;
5317
5318 if (bitentry->length != 2) {
5319 NV_ERROR(dev, "Do not understand BIT TMDS table\n");
5320 return -EINVAL;
5321 }
5322
5323 tmdstableptr = ROM16(bios->data[bitentry->offset]);
Ben Skeggs98720bf2010-08-13 08:31:22 +10005324 if (!tmdstableptr) {
Ben Skeggs6ee73862009-12-11 19:24:15 +10005325 NV_ERROR(dev, "Pointer to TMDS table invalid\n");
5326 return -EINVAL;
5327 }
5328
Ben Skeggs98720bf2010-08-13 08:31:22 +10005329 NV_INFO(dev, "TMDS table version %d.%d\n",
5330 bios->data[tmdstableptr] >> 4, bios->data[tmdstableptr] & 0xf);
5331
Ben Skeggs6ee73862009-12-11 19:24:15 +10005332 /* nv50+ has v2.0, but we don't parse it atm */
Ben Skeggs98720bf2010-08-13 08:31:22 +10005333 if (bios->data[tmdstableptr] != 0x11)
Ben Skeggs6ee73862009-12-11 19:24:15 +10005334 return -ENOSYS;
Ben Skeggs6ee73862009-12-11 19:24:15 +10005335
5336 /*
5337 * These two scripts are odd: they don't seem to get run even when
5338 * they are not stubbed.
5339 */
5340 script1 = ROM16(bios->data[tmdstableptr + 7]);
5341 script2 = ROM16(bios->data[tmdstableptr + 9]);
5342 if (bios->data[script1] != 'q' || bios->data[script2] != 'q')
5343 NV_WARN(dev, "TMDS table script pointers not stubbed\n");
5344
5345 bios->tmds.output0_script_ptr = ROM16(bios->data[tmdstableptr + 11]);
5346 bios->tmds.output1_script_ptr = ROM16(bios->data[tmdstableptr + 13]);
5347
5348 return 0;
5349}
5350
5351static int
5352parse_bit_U_tbl_entry(struct drm_device *dev, struct nvbios *bios,
5353 struct bit_entry *bitentry)
5354{
5355 /*
5356 * Parses the pointer to the G80 output script tables
5357 *
5358 * Starting at bitentry->offset:
5359 *
5360 * offset + 0 (16 bits): output script table pointer
5361 */
5362
5363 uint16_t outputscripttableptr;
5364
5365 if (bitentry->length != 3) {
5366 NV_ERROR(dev, "Do not understand BIT U table\n");
5367 return -EINVAL;
5368 }
5369
5370 outputscripttableptr = ROM16(bios->data[bitentry->offset]);
5371 bios->display.script_table_ptr = outputscripttableptr;
5372 return 0;
5373}
5374
Ben Skeggs6ee73862009-12-11 19:24:15 +10005375struct bit_table {
5376 const char id;
5377 int (* const parse_fn)(struct drm_device *, struct nvbios *, struct bit_entry *);
5378};
5379
5380#define BIT_TABLE(id, funcid) ((struct bit_table){ id, parse_bit_##funcid##_tbl_entry })
5381
Ben Skeggs4709bff2010-09-13 15:18:40 +10005382int
5383bit_table(struct drm_device *dev, u8 id, struct bit_entry *bit)
5384{
5385 struct drm_nouveau_private *dev_priv = dev->dev_private;
5386 struct nvbios *bios = &dev_priv->vbios;
5387 u8 entries, *entry;
5388
5389 entries = bios->data[bios->offset + 10];
5390 entry = &bios->data[bios->offset + 12];
5391 while (entries--) {
5392 if (entry[0] == id) {
5393 bit->id = entry[0];
5394 bit->version = entry[1];
5395 bit->length = ROM16(entry[2]);
5396 bit->offset = ROM16(entry[4]);
Ben Skeggsf9f9f532011-10-12 16:48:48 +10005397 bit->data = ROMPTR(dev, entry[4]);
Ben Skeggs4709bff2010-09-13 15:18:40 +10005398 return 0;
5399 }
5400
5401 entry += bios->data[bios->offset + 9];
5402 }
5403
5404 return -ENOENT;
5405}
5406
Ben Skeggs6ee73862009-12-11 19:24:15 +10005407static int
5408parse_bit_table(struct nvbios *bios, const uint16_t bitoffset,
5409 struct bit_table *table)
5410{
5411 struct drm_device *dev = bios->dev;
Ben Skeggs6ee73862009-12-11 19:24:15 +10005412 struct bit_entry bitentry;
5413
Ben Skeggs4709bff2010-09-13 15:18:40 +10005414 if (bit_table(dev, table->id, &bitentry) == 0)
Ben Skeggs6ee73862009-12-11 19:24:15 +10005415 return table->parse_fn(dev, bios, &bitentry);
Ben Skeggs6ee73862009-12-11 19:24:15 +10005416
5417 NV_INFO(dev, "BIT table '%c' not found\n", table->id);
5418 return -ENOSYS;
5419}
5420
5421static int
5422parse_bit_structure(struct nvbios *bios, const uint16_t bitoffset)
5423{
5424 int ret;
5425
5426 /*
5427 * The only restriction on parsing order currently is having 'i' first
5428 * for use of bios->*_version or bios->feature_byte while parsing;
5429 * functions shouldn't be actually *doing* anything apart from pulling
5430 * data from the image into the bios struct, thus no interdependencies
5431 */
5432 ret = parse_bit_table(bios, bitoffset, &BIT_TABLE('i', i));
5433 if (ret) /* info? */
5434 return ret;
5435 if (bios->major_version >= 0x60) /* g80+ */
5436 parse_bit_table(bios, bitoffset, &BIT_TABLE('A', A));
5437 ret = parse_bit_table(bios, bitoffset, &BIT_TABLE('C', C));
5438 if (ret)
5439 return ret;
5440 parse_bit_table(bios, bitoffset, &BIT_TABLE('D', display));
5441 ret = parse_bit_table(bios, bitoffset, &BIT_TABLE('I', init));
5442 if (ret)
5443 return ret;
5444 parse_bit_table(bios, bitoffset, &BIT_TABLE('M', M)); /* memory? */
5445 parse_bit_table(bios, bitoffset, &BIT_TABLE('L', lvds));
5446 parse_bit_table(bios, bitoffset, &BIT_TABLE('T', tmds));
5447 parse_bit_table(bios, bitoffset, &BIT_TABLE('U', U));
Ben Skeggs6ee73862009-12-11 19:24:15 +10005448
5449 return 0;
5450}
5451
5452static int parse_bmp_structure(struct drm_device *dev, struct nvbios *bios, unsigned int offset)
5453{
5454 /*
5455 * Parses the BMP structure for useful things, but does not act on them
5456 *
5457 * offset + 5: BMP major version
5458 * offset + 6: BMP minor version
5459 * offset + 9: BMP feature byte
5460 * offset + 10: BCD encoded BIOS version
5461 *
5462 * offset + 18: init script table pointer (for bios versions < 5.10h)
5463 * offset + 20: extra init script table pointer (for bios
5464 * versions < 5.10h)
5465 *
5466 * offset + 24: memory init table pointer (used on early bios versions)
5467 * offset + 26: SDR memory sequencing setup data table
5468 * offset + 28: DDR memory sequencing setup data table
5469 *
5470 * offset + 54: index of I2C CRTC pair to use for CRT output
5471 * offset + 55: index of I2C CRTC pair to use for TV output
5472 * offset + 56: index of I2C CRTC pair to use for flat panel output
5473 * offset + 58: write CRTC index for I2C pair 0
5474 * offset + 59: read CRTC index for I2C pair 0
5475 * offset + 60: write CRTC index for I2C pair 1
5476 * offset + 61: read CRTC index for I2C pair 1
5477 *
5478 * offset + 67: maximum internal PLL frequency (single stage PLL)
5479 * offset + 71: minimum internal PLL frequency (single stage PLL)
5480 *
5481 * offset + 75: script table pointers, as described in
5482 * parse_script_table_pointers
5483 *
5484 * offset + 89: TMDS single link output A table pointer
5485 * offset + 91: TMDS single link output B table pointer
5486 * offset + 95: LVDS single link output A table pointer
5487 * offset + 105: flat panel timings table pointer
5488 * offset + 107: flat panel strapping translation table pointer
5489 * offset + 117: LVDS manufacturer panel config table pointer
5490 * offset + 119: LVDS manufacturer strapping translation table pointer
5491 *
5492 * offset + 142: PLL limits table pointer
5493 *
5494 * offset + 156: minimum pixel clock for LVDS dual link
5495 */
5496
5497 uint8_t *bmp = &bios->data[offset], bmp_version_major, bmp_version_minor;
5498 uint16_t bmplength;
5499 uint16_t legacy_scripts_offset, legacy_i2c_offset;
5500
5501 /* load needed defaults in case we can't parse this info */
Ben Skeggs04a39c52010-02-24 10:03:05 +10005502 bios->digital_min_front_porch = 0x4b;
Ben Skeggs6ee73862009-12-11 19:24:15 +10005503 bios->fmaxvco = 256000;
5504 bios->fminvco = 128000;
5505 bios->fp.duallink_transition_clk = 90000;
5506
5507 bmp_version_major = bmp[5];
5508 bmp_version_minor = bmp[6];
5509
5510 NV_TRACE(dev, "BMP version %d.%d\n",
5511 bmp_version_major, bmp_version_minor);
5512
5513 /*
5514 * Make sure that 0x36 is blank and can't be mistaken for a DCB
5515 * pointer on early versions
5516 */
5517 if (bmp_version_major < 5)
5518 *(uint16_t *)&bios->data[0x36] = 0;
5519
5520 /*
5521 * Seems that the minor version was 1 for all major versions prior
5522 * to 5. Version 6 could theoretically exist, but I suspect BIT
5523 * happened instead.
5524 */
5525 if ((bmp_version_major < 5 && bmp_version_minor != 1) || bmp_version_major > 5) {
5526 NV_ERROR(dev, "You have an unsupported BMP version. "
5527 "Please send in your bios\n");
5528 return -ENOSYS;
5529 }
5530
5531 if (bmp_version_major == 0)
5532 /* nothing that's currently useful in this version */
5533 return 0;
5534 else if (bmp_version_major == 1)
5535 bmplength = 44; /* exact for 1.01 */
5536 else if (bmp_version_major == 2)
5537 bmplength = 48; /* exact for 2.01 */
5538 else if (bmp_version_major == 3)
5539 bmplength = 54;
5540 /* guessed - mem init tables added in this version */
5541 else if (bmp_version_major == 4 || bmp_version_minor < 0x1)
5542 /* don't know if 5.0 exists... */
5543 bmplength = 62;
5544 /* guessed - BMP I2C indices added in version 4*/
5545 else if (bmp_version_minor < 0x6)
5546 bmplength = 67; /* exact for 5.01 */
5547 else if (bmp_version_minor < 0x10)
5548 bmplength = 75; /* exact for 5.06 */
5549 else if (bmp_version_minor == 0x10)
5550 bmplength = 89; /* exact for 5.10h */
5551 else if (bmp_version_minor < 0x14)
5552 bmplength = 118; /* exact for 5.11h */
5553 else if (bmp_version_minor < 0x24)
5554 /*
5555 * Not sure of version where pll limits came in;
5556 * certainly exist by 0x24 though.
5557 */
5558 /* length not exact: this is long enough to get lvds members */
5559 bmplength = 123;
5560 else if (bmp_version_minor < 0x27)
5561 /*
5562 * Length not exact: this is long enough to get pll limit
5563 * member
5564 */
5565 bmplength = 144;
5566 else
5567 /*
5568 * Length not exact: this is long enough to get dual link
5569 * transition clock.
5570 */
5571 bmplength = 158;
5572
5573 /* checksum */
5574 if (nv_cksum(bmp, 8)) {
5575 NV_ERROR(dev, "Bad BMP checksum\n");
5576 return -EINVAL;
5577 }
5578
5579 /*
5580 * Bit 4 seems to indicate either a mobile bios or a quadro card --
5581 * mobile behaviour consistent (nv11+), quadro only seen nv18gl-nv36gl
5582 * (not nv10gl), bit 5 that the flat panel tables are present, and
5583 * bit 6 a tv bios.
5584 */
5585 bios->feature_byte = bmp[9];
5586
5587 parse_bios_version(dev, bios, offset + 10);
5588
5589 if (bmp_version_major < 5 || bmp_version_minor < 0x10)
5590 bios->old_style_init = true;
5591 legacy_scripts_offset = 18;
5592 if (bmp_version_major < 2)
5593 legacy_scripts_offset -= 4;
5594 bios->init_script_tbls_ptr = ROM16(bmp[legacy_scripts_offset]);
5595 bios->extra_init_script_tbl_ptr = ROM16(bmp[legacy_scripts_offset + 2]);
5596
5597 if (bmp_version_major > 2) { /* appears in BMP 3 */
5598 bios->legacy.mem_init_tbl_ptr = ROM16(bmp[24]);
5599 bios->legacy.sdr_seq_tbl_ptr = ROM16(bmp[26]);
5600 bios->legacy.ddr_seq_tbl_ptr = ROM16(bmp[28]);
5601 }
5602
5603 legacy_i2c_offset = 0x48; /* BMP version 2 & 3 */
5604 if (bmplength > 61)
5605 legacy_i2c_offset = offset + 54;
5606 bios->legacy.i2c_indices.crt = bios->data[legacy_i2c_offset];
5607 bios->legacy.i2c_indices.tv = bios->data[legacy_i2c_offset + 1];
5608 bios->legacy.i2c_indices.panel = bios->data[legacy_i2c_offset + 2];
Ben Skeggs6ee73862009-12-11 19:24:15 +10005609
5610 if (bmplength > 74) {
5611 bios->fmaxvco = ROM32(bmp[67]);
5612 bios->fminvco = ROM32(bmp[71]);
5613 }
5614 if (bmplength > 88)
5615 parse_script_table_pointers(bios, offset + 75);
5616 if (bmplength > 94) {
5617 bios->tmds.output0_script_ptr = ROM16(bmp[89]);
5618 bios->tmds.output1_script_ptr = ROM16(bmp[91]);
5619 /*
5620 * Never observed in use with lvds scripts, but is reused for
5621 * 18/24 bit panel interface default for EDID equipped panels
5622 * (if_is_24bit not set directly to avoid any oscillation).
5623 */
5624 bios->legacy.lvds_single_a_script_ptr = ROM16(bmp[95]);
5625 }
5626 if (bmplength > 108) {
5627 bios->fp.fptablepointer = ROM16(bmp[105]);
5628 bios->fp.fpxlatetableptr = ROM16(bmp[107]);
5629 bios->fp.xlatwidth = 1;
5630 }
5631 if (bmplength > 120) {
5632 bios->fp.lvdsmanufacturerpointer = ROM16(bmp[117]);
5633 bios->fp.fpxlatemanufacturertableptr = ROM16(bmp[119]);
5634 }
5635 if (bmplength > 143)
5636 bios->pll_limit_tbl_ptr = ROM16(bmp[142]);
5637
5638 if (bmplength > 157)
5639 bios->fp.duallink_transition_clk = ROM16(bmp[156]) * 10;
5640
5641 return 0;
5642}
5643
5644static uint16_t findstr(uint8_t *data, int n, const uint8_t *str, int len)
5645{
5646 int i, j;
5647
5648 for (i = 0; i <= (n - len); i++) {
5649 for (j = 0; j < len; j++)
5650 if (data[i + j] != str[j])
5651 break;
5652 if (j == len)
5653 return i;
5654 }
5655
5656 return 0;
5657}
5658
Ben Skeggs6ee73862009-12-11 19:24:15 +10005659static struct dcb_gpio_entry *
5660new_gpio_entry(struct nvbios *bios)
5661{
Ben Skeggse49f70f72010-09-20 10:06:50 +10005662 struct drm_device *dev = bios->dev;
Ben Skeggs7f245b22010-02-24 09:56:18 +10005663 struct dcb_gpio_table *gpio = &bios->dcb.gpio;
Ben Skeggs6ee73862009-12-11 19:24:15 +10005664
Ben Skeggse49f70f72010-09-20 10:06:50 +10005665 if (gpio->entries >= DCB_MAX_NUM_GPIO_ENTRIES) {
5666 NV_ERROR(dev, "exceeded maximum number of gpio entries!!\n");
5667 return NULL;
5668 }
5669
Ben Skeggs6ee73862009-12-11 19:24:15 +10005670 return &gpio->entry[gpio->entries++];
5671}
5672
5673struct dcb_gpio_entry *
5674nouveau_bios_gpio_entry(struct drm_device *dev, enum dcb_gpio_tag tag)
5675{
5676 struct drm_nouveau_private *dev_priv = dev->dev_private;
Ben Skeggs04a39c52010-02-24 10:03:05 +10005677 struct nvbios *bios = &dev_priv->vbios;
Ben Skeggs6ee73862009-12-11 19:24:15 +10005678 int i;
5679
Ben Skeggs7f245b22010-02-24 09:56:18 +10005680 for (i = 0; i < bios->dcb.gpio.entries; i++) {
5681 if (bios->dcb.gpio.entry[i].tag != tag)
Ben Skeggs6ee73862009-12-11 19:24:15 +10005682 continue;
5683
Ben Skeggs7f245b22010-02-24 09:56:18 +10005684 return &bios->dcb.gpio.entry[i];
Ben Skeggs6ee73862009-12-11 19:24:15 +10005685 }
5686
5687 return NULL;
5688}
5689
5690static void
Ben Skeggs6ee73862009-12-11 19:24:15 +10005691parse_dcb_gpio_table(struct nvbios *bios)
5692{
5693 struct drm_device *dev = bios->dev;
Ben Skeggse49f70f72010-09-20 10:06:50 +10005694 struct dcb_gpio_entry *e;
5695 u8 headerlen, entries, recordlen;
5696 u8 *dcb, *gpio = NULL, *entry;
Ben Skeggs6ee73862009-12-11 19:24:15 +10005697 int i;
5698
Ben Skeggsf9f9f532011-10-12 16:48:48 +10005699 dcb = ROMPTR(dev, bios->data[0x36]);
Ben Skeggse49f70f72010-09-20 10:06:50 +10005700 if (dcb[0] >= 0x30) {
Ben Skeggsf9f9f532011-10-12 16:48:48 +10005701 gpio = ROMPTR(dev, dcb[10]);
Ben Skeggse49f70f72010-09-20 10:06:50 +10005702 if (!gpio)
5703 goto no_table;
Ben Skeggs6ee73862009-12-11 19:24:15 +10005704
Ben Skeggse49f70f72010-09-20 10:06:50 +10005705 headerlen = gpio[1];
5706 entries = gpio[2];
5707 recordlen = gpio[3];
5708 } else
Francisco Jerez5e6a7442010-09-22 23:25:00 +02005709 if (dcb[0] >= 0x22 && dcb[-1] >= 0x13) {
Ben Skeggsf9f9f532011-10-12 16:48:48 +10005710 gpio = ROMPTR(dev, dcb[-15]);
Ben Skeggse49f70f72010-09-20 10:06:50 +10005711 if (!gpio)
5712 goto no_table;
Ben Skeggs6ee73862009-12-11 19:24:15 +10005713
Ben Skeggse49f70f72010-09-20 10:06:50 +10005714 headerlen = 3;
5715 entries = gpio[2];
5716 recordlen = gpio[1];
Francisco Jerez5e6a7442010-09-22 23:25:00 +02005717 } else
5718 if (dcb[0] >= 0x22) {
5719 /* No GPIO table present, parse the TVDAC GPIO data. */
5720 uint8_t *tvdac_gpio = &dcb[-5];
Ben Skeggs6ee73862009-12-11 19:24:15 +10005721
5722 if (tvdac_gpio[0] & 1) {
Francisco Jerez5e6a7442010-09-22 23:25:00 +02005723 e = new_gpio_entry(bios);
5724 e->tag = DCB_GPIO_TVDAC0;
5725 e->line = tvdac_gpio[1] >> 4;
Ben Skeggs85a2a362011-09-13 05:32:43 +10005726 e->state[0] = !!(tvdac_gpio[0] & 2);
5727 e->state[1] = !e->state[0];
Ben Skeggs6ee73862009-12-11 19:24:15 +10005728 }
Francisco Jerez5e6a7442010-09-22 23:25:00 +02005729
5730 goto no_table;
Francisco Jerez20d66da2010-08-15 14:32:49 +02005731 } else {
Ben Skeggse49f70f72010-09-20 10:06:50 +10005732 NV_DEBUG(dev, "no/unknown gpio table on DCB 0x%02x\n", dcb[0]);
5733 goto no_table;
5734 }
Francisco Jerez20d66da2010-08-15 14:32:49 +02005735
Ben Skeggse49f70f72010-09-20 10:06:50 +10005736 entry = gpio + headerlen;
5737 for (i = 0; i < entries; i++, entry += recordlen) {
5738 e = new_gpio_entry(bios);
5739 if (!e)
5740 break;
Francisco Jerez20d66da2010-08-15 14:32:49 +02005741
Ben Skeggse49f70f72010-09-20 10:06:50 +10005742 if (gpio[0] < 0x40) {
5743 e->entry = ROM16(entry[0]);
5744 e->tag = (e->entry & 0x07e0) >> 5;
5745 if (e->tag == 0x3f) {
5746 bios->dcb.gpio.entries--;
5747 continue;
5748 }
5749
5750 e->line = (e->entry & 0x001f);
Ben Skeggs85a2a362011-09-13 05:32:43 +10005751 e->state[0] = ((e->entry & 0xf800) >> 11) != 4;
5752 e->state[1] = !e->state[0];
Ben Skeggse49f70f72010-09-20 10:06:50 +10005753 } else {
5754 e->entry = ROM32(entry[0]);
5755 e->tag = (e->entry & 0x0000ff00) >> 8;
5756 if (e->tag == 0xff) {
5757 bios->dcb.gpio.entries--;
5758 continue;
5759 }
5760
5761 e->line = (e->entry & 0x0000001f) >> 0;
Ben Skeggsd7f81722011-07-03 02:57:35 +10005762 if (gpio[0] == 0x40) {
5763 e->state_default = (e->entry & 0x01000000) >> 24;
5764 e->state[0] = (e->entry & 0x18000000) >> 27;
5765 e->state[1] = (e->entry & 0x60000000) >> 29;
5766 } else {
5767 e->state_default = (e->entry & 0x00000080) >> 7;
5768 e->state[0] = (entry[4] >> 4) & 3;
5769 e->state[1] = (entry[4] >> 6) & 3;
5770 }
Francisco Jerez20d66da2010-08-15 14:32:49 +02005771 }
Ben Skeggs6ee73862009-12-11 19:24:15 +10005772 }
5773
Ben Skeggse49f70f72010-09-20 10:06:50 +10005774no_table:
5775 /* Apple iMac G4 NV18 */
5776 if (nv_match_device(dev, 0x0189, 0x10de, 0x0010)) {
5777 e = new_gpio_entry(bios);
5778 if (e) {
5779 e->tag = DCB_GPIO_TVDAC0;
5780 e->line = 4;
5781 }
Ben Skeggs6ee73862009-12-11 19:24:15 +10005782 }
Ben Skeggs6ee73862009-12-11 19:24:15 +10005783}
5784
5785struct dcb_connector_table_entry *
5786nouveau_bios_connector_entry(struct drm_device *dev, int index)
5787{
5788 struct drm_nouveau_private *dev_priv = dev->dev_private;
Ben Skeggs04a39c52010-02-24 10:03:05 +10005789 struct nvbios *bios = &dev_priv->vbios;
Ben Skeggs6ee73862009-12-11 19:24:15 +10005790 struct dcb_connector_table_entry *cte;
5791
Ben Skeggs7f245b22010-02-24 09:56:18 +10005792 if (index >= bios->dcb.connector.entries)
Ben Skeggs6ee73862009-12-11 19:24:15 +10005793 return NULL;
5794
Ben Skeggs7f245b22010-02-24 09:56:18 +10005795 cte = &bios->dcb.connector.entry[index];
Ben Skeggs6ee73862009-12-11 19:24:15 +10005796 if (cte->type == 0xff)
5797 return NULL;
5798
5799 return cte;
5800}
5801
Ben Skeggsf66fa772010-02-24 11:09:20 +10005802static enum dcb_connector_type
5803divine_connector_type(struct nvbios *bios, int index)
5804{
5805 struct dcb_table *dcb = &bios->dcb;
5806 unsigned encoders = 0, type = DCB_CONNECTOR_NONE;
5807 int i;
5808
5809 for (i = 0; i < dcb->entries; i++) {
5810 if (dcb->entry[i].connector == index)
5811 encoders |= (1 << dcb->entry[i].type);
5812 }
5813
5814 if (encoders & (1 << OUTPUT_DP)) {
5815 if (encoders & (1 << OUTPUT_TMDS))
5816 type = DCB_CONNECTOR_DP;
5817 else
5818 type = DCB_CONNECTOR_eDP;
5819 } else
5820 if (encoders & (1 << OUTPUT_TMDS)) {
5821 if (encoders & (1 << OUTPUT_ANALOG))
5822 type = DCB_CONNECTOR_DVI_I;
5823 else
5824 type = DCB_CONNECTOR_DVI_D;
5825 } else
5826 if (encoders & (1 << OUTPUT_ANALOG)) {
5827 type = DCB_CONNECTOR_VGA;
5828 } else
5829 if (encoders & (1 << OUTPUT_LVDS)) {
5830 type = DCB_CONNECTOR_LVDS;
5831 } else
5832 if (encoders & (1 << OUTPUT_TV)) {
5833 type = DCB_CONNECTOR_TV_0;
5834 }
5835
5836 return type;
5837}
5838
Ben Skeggs6ee73862009-12-11 19:24:15 +10005839static void
Ben Skeggs53c44c32010-03-04 12:12:22 +10005840apply_dcb_connector_quirks(struct nvbios *bios, int idx)
5841{
5842 struct dcb_connector_table_entry *cte = &bios->dcb.connector.entry[idx];
5843 struct drm_device *dev = bios->dev;
5844
5845 /* Gigabyte NX85T */
Francisco Jerezacae1162010-08-15 14:31:31 +02005846 if (nv_match_device(dev, 0x0421, 0x1458, 0x344c)) {
Ben Skeggs53c44c32010-03-04 12:12:22 +10005847 if (cte->type == DCB_CONNECTOR_HDMI_1)
5848 cte->type = DCB_CONNECTOR_DVI_I;
5849 }
Emil Velikovf0d07d62011-07-10 10:59:29 +01005850
5851 /* Gigabyte GV-NX86T512H */
5852 if (nv_match_device(dev, 0x0402, 0x1458, 0x3455)) {
5853 if (cte->type == DCB_CONNECTOR_HDMI_1)
5854 cte->type = DCB_CONNECTOR_DVI_I;
5855 }
Ben Skeggs53c44c32010-03-04 12:12:22 +10005856}
5857
Ben Skeggsa589e872011-02-17 08:03:53 +10005858static const u8 hpd_gpio[16] = {
5859 0xff, 0x07, 0x08, 0xff, 0xff, 0x51, 0x52, 0xff,
5860 0xff, 0xff, 0xff, 0xff, 0xff, 0x5e, 0x5f, 0x60,
5861};
5862
Ben Skeggs53c44c32010-03-04 12:12:22 +10005863static void
Ben Skeggs6ee73862009-12-11 19:24:15 +10005864parse_dcb_connector_table(struct nvbios *bios)
5865{
5866 struct drm_device *dev = bios->dev;
Ben Skeggs7f245b22010-02-24 09:56:18 +10005867 struct dcb_connector_table *ct = &bios->dcb.connector;
Ben Skeggs6ee73862009-12-11 19:24:15 +10005868 struct dcb_connector_table_entry *cte;
Ben Skeggs7f245b22010-02-24 09:56:18 +10005869 uint8_t *conntab = &bios->data[bios->dcb.connector_table_ptr];
Ben Skeggs6ee73862009-12-11 19:24:15 +10005870 uint8_t *entry;
5871 int i;
5872
Ben Skeggs7f245b22010-02-24 09:56:18 +10005873 if (!bios->dcb.connector_table_ptr) {
Maarten Maathuisef2bb502009-12-13 16:53:12 +01005874 NV_DEBUG_KMS(dev, "No DCB connector table present\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +10005875 return;
5876 }
5877
5878 NV_INFO(dev, "DCB connector table: VHER 0x%02x %d %d %d\n",
5879 conntab[0], conntab[1], conntab[2], conntab[3]);
5880 if ((conntab[0] != 0x30 && conntab[0] != 0x40) ||
5881 (conntab[3] != 2 && conntab[3] != 4)) {
5882 NV_ERROR(dev, " Unknown! Please report.\n");
5883 return;
5884 }
5885
5886 ct->entries = conntab[2];
5887
5888 entry = conntab + conntab[1];
5889 cte = &ct->entry[0];
5890 for (i = 0; i < conntab[2]; i++, entry += conntab[3], cte++) {
Ben Skeggsd544d622010-03-10 15:52:43 +10005891 cte->index = i;
Ben Skeggs6ee73862009-12-11 19:24:15 +10005892 if (conntab[3] == 2)
5893 cte->entry = ROM16(entry[0]);
5894 else
5895 cte->entry = ROM32(entry[0]);
Ben Skeggsf66fa772010-02-24 11:09:20 +10005896
Ben Skeggs6ee73862009-12-11 19:24:15 +10005897 cte->type = (cte->entry & 0x000000ff) >> 0;
Ben Skeggsd544d622010-03-10 15:52:43 +10005898 cte->index2 = (cte->entry & 0x00000f00) >> 8;
Ben Skeggsa589e872011-02-17 08:03:53 +10005899
5900 cte->gpio_tag = ffs((cte->entry & 0x07033000) >> 12);
5901 cte->gpio_tag = hpd_gpio[cte->gpio_tag];
Ben Skeggs6ee73862009-12-11 19:24:15 +10005902
5903 if (cte->type == 0xff)
5904 continue;
5905
Ben Skeggs53c44c32010-03-04 12:12:22 +10005906 apply_dcb_connector_quirks(bios, i);
5907
Ben Skeggs6ee73862009-12-11 19:24:15 +10005908 NV_INFO(dev, " %d: 0x%08x: type 0x%02x idx %d tag 0x%02x\n",
5909 i, cte->entry, cte->type, cte->index, cte->gpio_tag);
Ben Skeggsf66fa772010-02-24 11:09:20 +10005910
5911 /* check for known types, fallback to guessing the type
5912 * from attached encoders if we hit an unknown.
5913 */
5914 switch (cte->type) {
5915 case DCB_CONNECTOR_VGA:
5916 case DCB_CONNECTOR_TV_0:
5917 case DCB_CONNECTOR_TV_1:
5918 case DCB_CONNECTOR_TV_3:
5919 case DCB_CONNECTOR_DVI_I:
5920 case DCB_CONNECTOR_DVI_D:
5921 case DCB_CONNECTOR_LVDS:
Ben Skeggs8c3f6bb2011-04-18 09:57:48 +10005922 case DCB_CONNECTOR_LVDS_SPWG:
Ben Skeggsf66fa772010-02-24 11:09:20 +10005923 case DCB_CONNECTOR_DP:
5924 case DCB_CONNECTOR_eDP:
5925 case DCB_CONNECTOR_HDMI_0:
5926 case DCB_CONNECTOR_HDMI_1:
5927 break;
5928 default:
5929 cte->type = divine_connector_type(bios, cte->index);
Ben Skeggsda647d52010-03-04 12:00:39 +10005930 NV_WARN(dev, "unknown type, using 0x%02x\n", cte->type);
Ben Skeggsf66fa772010-02-24 11:09:20 +10005931 break;
5932 }
5933
Ben Skeggsda647d52010-03-04 12:00:39 +10005934 if (nouveau_override_conntype) {
5935 int type = divine_connector_type(bios, cte->index);
5936 if (type != cte->type)
5937 NV_WARN(dev, " -> type 0x%02x\n", cte->type);
5938 }
5939
Ben Skeggs6ee73862009-12-11 19:24:15 +10005940 }
5941}
5942
Ben Skeggs6b5a81a2011-11-10 15:42:55 +10005943void *
5944dcb_table(struct drm_device *dev)
5945{
5946 struct drm_nouveau_private *dev_priv = dev->dev_private;
5947 u8 *dcb = NULL;
5948
5949 if (dev_priv->card_type > NV_04)
5950 dcb = ROMPTR(dev, dev_priv->vbios.data[0x36]);
5951 if (!dcb) {
5952 NV_WARNONCE(dev, "No DCB data found in VBIOS\n");
5953 return NULL;
5954 }
5955
5956 if (dcb[0] >= 0x41) {
5957 NV_WARNONCE(dev, "DCB version 0x%02x unknown\n", dcb[0]);
5958 return NULL;
5959 } else
5960 if (dcb[0] >= 0x30) {
5961 if (ROM32(dcb[6]) == 0x4edcbdcb)
5962 return dcb;
5963 } else
5964 if (dcb[0] >= 0x20) {
5965 if (ROM32(dcb[4]) == 0x4edcbdcb)
5966 return dcb;
5967 } else
5968 if (dcb[0] >= 0x15) {
5969 if (!memcmp(&dcb[-7], "DEV_REC", 7))
5970 return dcb;
5971 } else {
5972 /*
5973 * v1.4 (some NV15/16, NV11+) seems the same as v1.5, but
5974 * always has the same single (crt) entry, even when tv-out
5975 * present, so the conclusion is this version cannot really
5976 * be used.
5977 *
5978 * v1.2 tables (some NV6/10, and NV15+) normally have the
5979 * same 5 entries, which are not specific to the card and so
5980 * no use.
5981 *
5982 * v1.2 does have an I2C table that read_dcb_i2c_table can
5983 * handle, but cards exist (nv11 in #14821) with a bad i2c
5984 * table pointer, so use the indices parsed in
5985 * parse_bmp_structure.
5986 *
5987 * v1.1 (NV5+, maybe some NV4) is entirely unhelpful
5988 */
5989 NV_WARNONCE(dev, "No useful DCB data in VBIOS\n");
5990 return NULL;
5991 }
5992
5993 NV_WARNONCE(dev, "DCB header validation failed\n");
5994 return NULL;
5995}
5996
5997u8 *
5998dcb_outp(struct drm_device *dev, u8 idx)
5999{
6000 u8 *dcb = dcb_table(dev);
6001 if (dcb && dcb[0] >= 0x30) {
6002 if (idx < dcb[2])
6003 return dcb + dcb[1] + (idx * dcb[3]);
6004 } else
6005 if (dcb && dcb[0] >= 0x20) {
6006 u8 *i2c = ROMPTR(dev, dcb[2]);
6007 u8 *ent = dcb + 8 + (idx * 8);
6008 if (i2c && ent < i2c)
6009 return ent;
6010 } else
6011 if (dcb && dcb[0] >= 0x15) {
6012 u8 *i2c = ROMPTR(dev, dcb[2]);
6013 u8 *ent = dcb + 4 + (idx * 10);
6014 if (i2c && ent < i2c)
6015 return ent;
6016 }
6017
6018 return NULL;
6019}
6020
6021int
6022dcb_outp_foreach(struct drm_device *dev, void *data,
6023 int (*exec)(struct drm_device *, void *, int idx, u8 *outp))
6024{
6025 int ret, idx = -1;
6026 u8 *outp = NULL;
6027 while ((outp = dcb_outp(dev, ++idx))) {
6028 if (ROM32(outp[0]) == 0x00000000)
6029 break; /* seen on an NV11 with DCB v1.5 */
6030 if (ROM32(outp[0]) == 0xffffffff)
6031 break; /* seen on an NV17 with DCB v2.0 */
6032
6033 if ((outp[0] & 0x0f) == OUTPUT_UNUSED)
6034 continue;
6035 if ((outp[0] & 0x0f) == OUTPUT_EOL)
6036 break;
6037
6038 ret = exec(dev, data, idx, outp);
6039 if (ret)
6040 return ret;
6041 }
6042
6043 return 0;
6044}
6045
Ben Skeggs7f245b22010-02-24 09:56:18 +10006046static struct dcb_entry *new_dcb_entry(struct dcb_table *dcb)
Ben Skeggs6ee73862009-12-11 19:24:15 +10006047{
6048 struct dcb_entry *entry = &dcb->entry[dcb->entries];
6049
6050 memset(entry, 0, sizeof(struct dcb_entry));
6051 entry->index = dcb->entries++;
6052
6053 return entry;
6054}
6055
Francisco Jerez2e5702a2010-11-19 18:08:47 +10006056static void fabricate_dcb_output(struct dcb_table *dcb, int type, int i2c,
6057 int heads, int or)
Ben Skeggs6ee73862009-12-11 19:24:15 +10006058{
6059 struct dcb_entry *entry = new_dcb_entry(dcb);
6060
Francisco Jerez2e5702a2010-11-19 18:08:47 +10006061 entry->type = type;
Ben Skeggs6ee73862009-12-11 19:24:15 +10006062 entry->i2c_index = i2c;
6063 entry->heads = heads;
Francisco Jerez2e5702a2010-11-19 18:08:47 +10006064 if (type != OUTPUT_ANALOG)
6065 entry->location = !DCB_LOC_ON_CHIP; /* ie OFF CHIP */
6066 entry->or = or;
Ben Skeggs6ee73862009-12-11 19:24:15 +10006067}
6068
6069static bool
Ben Skeggs7f245b22010-02-24 09:56:18 +10006070parse_dcb20_entry(struct drm_device *dev, struct dcb_table *dcb,
Ben Skeggs6ee73862009-12-11 19:24:15 +10006071 uint32_t conn, uint32_t conf, struct dcb_entry *entry)
6072{
6073 entry->type = conn & 0xf;
6074 entry->i2c_index = (conn >> 4) & 0xf;
6075 entry->heads = (conn >> 8) & 0xf;
Ben Skeggs7f245b22010-02-24 09:56:18 +10006076 if (dcb->version >= 0x40)
Ben Skeggs6ee73862009-12-11 19:24:15 +10006077 entry->connector = (conn >> 12) & 0xf;
6078 entry->bus = (conn >> 16) & 0xf;
6079 entry->location = (conn >> 20) & 0x3;
6080 entry->or = (conn >> 24) & 0xf;
Ben Skeggs6ee73862009-12-11 19:24:15 +10006081
6082 switch (entry->type) {
6083 case OUTPUT_ANALOG:
6084 /*
6085 * Although the rest of a CRT conf dword is usually
6086 * zeros, mac biosen have stuff there so we must mask
6087 */
Ben Skeggs7f245b22010-02-24 09:56:18 +10006088 entry->crtconf.maxfreq = (dcb->version < 0x30) ?
Ben Skeggs6ee73862009-12-11 19:24:15 +10006089 (conf & 0xffff) * 10 :
6090 (conf & 0xff) * 10000;
6091 break;
6092 case OUTPUT_LVDS:
6093 {
6094 uint32_t mask;
6095 if (conf & 0x1)
6096 entry->lvdsconf.use_straps_for_mode = true;
Ben Skeggs7f245b22010-02-24 09:56:18 +10006097 if (dcb->version < 0x22) {
Ben Skeggs6ee73862009-12-11 19:24:15 +10006098 mask = ~0xd;
6099 /*
6100 * The laptop in bug 14567 lies and claims to not use
6101 * straps when it does, so assume all DCB 2.0 laptops
6102 * use straps, until a broken EDID using one is produced
6103 */
6104 entry->lvdsconf.use_straps_for_mode = true;
6105 /*
6106 * Both 0x4 and 0x8 show up in v2.0 tables; assume they
6107 * mean the same thing (probably wrong, but might work)
6108 */
6109 if (conf & 0x4 || conf & 0x8)
6110 entry->lvdsconf.use_power_scripts = true;
6111 } else {
Ben Skeggsa6ed76d2010-07-12 15:33:07 +10006112 mask = ~0x7;
6113 if (conf & 0x2)
6114 entry->lvdsconf.use_acpi_for_edid = true;
Ben Skeggs6ee73862009-12-11 19:24:15 +10006115 if (conf & 0x4)
6116 entry->lvdsconf.use_power_scripts = true;
Ben Skeggsc5875472010-07-16 16:17:27 +10006117 entry->lvdsconf.sor.link = (conf & 0x00000030) >> 4;
Ben Skeggs6ee73862009-12-11 19:24:15 +10006118 }
6119 if (conf & mask) {
6120 /*
6121 * Until we even try to use these on G8x, it's
6122 * useless reporting unknown bits. They all are.
6123 */
Ben Skeggs7f245b22010-02-24 09:56:18 +10006124 if (dcb->version >= 0x40)
Ben Skeggs6ee73862009-12-11 19:24:15 +10006125 break;
6126
6127 NV_ERROR(dev, "Unknown LVDS configuration bits, "
6128 "please report\n");
6129 }
6130 break;
6131 }
6132 case OUTPUT_TV:
6133 {
Ben Skeggs7f245b22010-02-24 09:56:18 +10006134 if (dcb->version >= 0x30)
Ben Skeggs6ee73862009-12-11 19:24:15 +10006135 entry->tvconf.has_component_output = conf & (0x8 << 4);
6136 else
6137 entry->tvconf.has_component_output = false;
6138
6139 break;
6140 }
6141 case OUTPUT_DP:
6142 entry->dpconf.sor.link = (conf & 0x00000030) >> 4;
Ben Skeggs75a1fcc2011-08-04 09:55:44 +10006143 switch ((conf & 0x00e00000) >> 21) {
6144 case 0:
6145 entry->dpconf.link_bw = 162000;
6146 break;
6147 default:
6148 entry->dpconf.link_bw = 270000;
6149 break;
6150 }
Ben Skeggs6ee73862009-12-11 19:24:15 +10006151 switch ((conf & 0x0f000000) >> 24) {
6152 case 0xf:
6153 entry->dpconf.link_nr = 4;
6154 break;
6155 case 0x3:
6156 entry->dpconf.link_nr = 2;
6157 break;
6158 default:
6159 entry->dpconf.link_nr = 1;
6160 break;
6161 }
6162 break;
6163 case OUTPUT_TMDS:
Francisco Jerez27d50fc2010-08-08 17:09:06 +02006164 if (dcb->version >= 0x40)
6165 entry->tmdsconf.sor.link = (conf & 0x00000030) >> 4;
Francisco Jerez4a9f8222010-07-20 16:48:08 +02006166 else if (dcb->version >= 0x30)
6167 entry->tmdsconf.slave_addr = (conf & 0x00000700) >> 8;
Francisco Jerez27d50fc2010-08-08 17:09:06 +02006168 else if (dcb->version >= 0x22)
6169 entry->tmdsconf.slave_addr = (conf & 0x00000070) >> 4;
Francisco Jerez4a9f8222010-07-20 16:48:08 +02006170
Ben Skeggs6ee73862009-12-11 19:24:15 +10006171 break;
Ben Skeggs44a12462010-08-17 14:34:00 +10006172 case OUTPUT_EOL:
Ben Skeggs6ee73862009-12-11 19:24:15 +10006173 /* weird g80 mobile type that "nv" treats as a terminator */
Ben Skeggs7f245b22010-02-24 09:56:18 +10006174 dcb->entries--;
Ben Skeggs6ee73862009-12-11 19:24:15 +10006175 return false;
Ben Skeggse7cc51c2010-02-24 10:31:39 +10006176 default:
6177 break;
Ben Skeggs6ee73862009-12-11 19:24:15 +10006178 }
6179
Ben Skeggs23484872010-05-28 09:39:11 +10006180 if (dcb->version < 0x40) {
6181 /* Normal entries consist of a single bit, but dual link has
6182 * the next most significant bit set too
6183 */
6184 entry->duallink_possible =
6185 ((1 << (ffs(entry->or) - 1)) * 3 == entry->or);
6186 } else {
6187 entry->duallink_possible = (entry->sorconf.link == 3);
6188 }
6189
Ben Skeggs6ee73862009-12-11 19:24:15 +10006190 /* unsure what DCB version introduces this, 3.0? */
6191 if (conf & 0x100000)
6192 entry->i2c_upper_default = true;
6193
6194 return true;
6195}
6196
6197static bool
Ben Skeggs7f245b22010-02-24 09:56:18 +10006198parse_dcb15_entry(struct drm_device *dev, struct dcb_table *dcb,
Ben Skeggs6ee73862009-12-11 19:24:15 +10006199 uint32_t conn, uint32_t conf, struct dcb_entry *entry)
6200{
Ben Skeggsb0d2de82010-01-14 17:53:04 +10006201 switch (conn & 0x0000000f) {
6202 case 0:
6203 entry->type = OUTPUT_ANALOG;
6204 break;
6205 case 1:
6206 entry->type = OUTPUT_TV;
6207 break;
6208 case 2:
Francisco Jerezfba67522010-08-24 23:02:02 +02006209 case 4:
6210 if (conn & 0x10)
6211 entry->type = OUTPUT_LVDS;
6212 else
6213 entry->type = OUTPUT_TMDS;
6214 break;
Ben Skeggsb0d2de82010-01-14 17:53:04 +10006215 case 3:
6216 entry->type = OUTPUT_LVDS;
6217 break;
Ben Skeggsb0d2de82010-01-14 17:53:04 +10006218 default:
6219 NV_ERROR(dev, "Unknown DCB type %d\n", conn & 0x0000000f);
Ben Skeggs6ee73862009-12-11 19:24:15 +10006220 return false;
6221 }
Ben Skeggsb0d2de82010-01-14 17:53:04 +10006222
6223 entry->i2c_index = (conn & 0x0003c000) >> 14;
6224 entry->heads = ((conn & 0x001c0000) >> 18) + 1;
6225 entry->or = entry->heads; /* same as heads, hopefully safe enough */
6226 entry->location = (conn & 0x01e00000) >> 21;
6227 entry->bus = (conn & 0x0e000000) >> 25;
Ben Skeggs6ee73862009-12-11 19:24:15 +10006228 entry->duallink_possible = false;
6229
6230 switch (entry->type) {
6231 case OUTPUT_ANALOG:
6232 entry->crtconf.maxfreq = (conf & 0xffff) * 10;
6233 break;
Ben Skeggsb0d2de82010-01-14 17:53:04 +10006234 case OUTPUT_TV:
6235 entry->tvconf.has_component_output = false;
Ben Skeggs6ee73862009-12-11 19:24:15 +10006236 break;
Ben Skeggsb0d2de82010-01-14 17:53:04 +10006237 case OUTPUT_LVDS:
Francisco Jerez77b1d5d2011-02-03 01:56:32 +01006238 if ((conn & 0x00003f00) >> 8 != 0x10)
Ben Skeggsb0d2de82010-01-14 17:53:04 +10006239 entry->lvdsconf.use_straps_for_mode = true;
6240 entry->lvdsconf.use_power_scripts = true;
6241 break;
6242 default:
Ben Skeggs6ee73862009-12-11 19:24:15 +10006243 break;
6244 }
6245
6246 return true;
6247}
6248
Ben Skeggs6ee73862009-12-11 19:24:15 +10006249static
Ben Skeggs7f245b22010-02-24 09:56:18 +10006250void merge_like_dcb_entries(struct drm_device *dev, struct dcb_table *dcb)
Ben Skeggs6ee73862009-12-11 19:24:15 +10006251{
6252 /*
6253 * DCB v2.0 lists each output combination separately.
6254 * Here we merge compatible entries to have fewer outputs, with
6255 * more options
6256 */
6257
6258 int i, newentries = 0;
6259
6260 for (i = 0; i < dcb->entries; i++) {
6261 struct dcb_entry *ient = &dcb->entry[i];
6262 int j;
6263
6264 for (j = i + 1; j < dcb->entries; j++) {
6265 struct dcb_entry *jent = &dcb->entry[j];
6266
6267 if (jent->type == 100) /* already merged entry */
6268 continue;
6269
6270 /* merge heads field when all other fields the same */
6271 if (jent->i2c_index == ient->i2c_index &&
6272 jent->type == ient->type &&
6273 jent->location == ient->location &&
6274 jent->or == ient->or) {
6275 NV_TRACE(dev, "Merging DCB entries %d and %d\n",
6276 i, j);
6277 ient->heads |= jent->heads;
6278 jent->type = 100; /* dummy value */
6279 }
6280 }
6281 }
6282
6283 /* Compact entries merged into others out of dcb */
6284 for (i = 0; i < dcb->entries; i++) {
6285 if (dcb->entry[i].type == 100)
6286 continue;
6287
6288 if (newentries != i) {
6289 dcb->entry[newentries] = dcb->entry[i];
6290 dcb->entry[newentries].index = newentries;
6291 }
6292 newentries++;
6293 }
6294
6295 dcb->entries = newentries;
6296}
6297
Ben Skeggsdf4cf1b2010-07-01 11:31:45 +10006298static bool
6299apply_dcb_encoder_quirks(struct drm_device *dev, int idx, u32 *conn, u32 *conf)
6300{
Francisco Jerez670820c2010-12-25 15:43:30 +01006301 struct drm_nouveau_private *dev_priv = dev->dev_private;
6302 struct dcb_table *dcb = &dev_priv->vbios.dcb;
6303
Ben Skeggsdf4cf1b2010-07-01 11:31:45 +10006304 /* Dell Precision M6300
6305 * DCB entry 2: 02025312 00000010
6306 * DCB entry 3: 02026312 00000020
6307 *
6308 * Identical, except apparently a different connector on a
6309 * different SOR link. Not a clue how we're supposed to know
6310 * which one is in use if it even shares an i2c line...
6311 *
6312 * Ignore the connector on the second SOR link to prevent
6313 * nasty problems until this is sorted (assuming it's not a
6314 * VBIOS bug).
6315 */
Francisco Jerezacae1162010-08-15 14:31:31 +02006316 if (nv_match_device(dev, 0x040d, 0x1028, 0x019b)) {
Ben Skeggsdf4cf1b2010-07-01 11:31:45 +10006317 if (*conn == 0x02026312 && *conf == 0x00000020)
6318 return false;
6319 }
6320
Francisco Jerez670820c2010-12-25 15:43:30 +01006321 /* GeForce3 Ti 200
6322 *
6323 * DCB reports an LVDS output that should be TMDS:
6324 * DCB entry 1: f2005014 ffffffff
6325 */
6326 if (nv_match_device(dev, 0x0201, 0x1462, 0x8851)) {
6327 if (*conn == 0xf2005014 && *conf == 0xffffffff) {
6328 fabricate_dcb_output(dcb, OUTPUT_TMDS, 1, 1, 1);
6329 return false;
6330 }
6331 }
6332
Ben Skeggsc0929b42011-03-21 11:42:51 +10006333 /* XFX GT-240X-YA
6334 *
6335 * So many things wrong here, replace the entire encoder table..
6336 */
6337 if (nv_match_device(dev, 0x0ca3, 0x1682, 0x3003)) {
6338 if (idx == 0) {
6339 *conn = 0x02001300; /* VGA, connector 1 */
6340 *conf = 0x00000028;
6341 } else
6342 if (idx == 1) {
6343 *conn = 0x01010312; /* DVI, connector 0 */
6344 *conf = 0x00020030;
6345 } else
6346 if (idx == 2) {
6347 *conn = 0x01010310; /* VGA, connector 0 */
6348 *conf = 0x00000028;
6349 } else
6350 if (idx == 3) {
6351 *conn = 0x02022362; /* HDMI, connector 2 */
6352 *conf = 0x00020010;
6353 } else {
6354 *conn = 0x0000000e; /* EOL */
6355 *conf = 0x00000000;
6356 }
6357 }
6358
Ben Skeggse540afc2011-05-30 12:53:37 +10006359 /* Some other twisted XFX board (rhbz#694914)
6360 *
6361 * The DVI/VGA encoder combo that's supposed to represent the
6362 * DVI-I connector actually point at two different ones, and
6363 * the HDMI connector ends up paired with the VGA instead.
6364 *
6365 * Connector table is missing anything for VGA at all, pointing it
6366 * an invalid conntab entry 2 so we figure it out ourself.
6367 */
6368 if (nv_match_device(dev, 0x0615, 0x1682, 0x2605)) {
6369 if (idx == 0) {
6370 *conn = 0x02002300; /* VGA, connector 2 */
6371 *conf = 0x00000028;
6372 } else
6373 if (idx == 1) {
6374 *conn = 0x01010312; /* DVI, connector 0 */
6375 *conf = 0x00020030;
6376 } else
6377 if (idx == 2) {
6378 *conn = 0x04020310; /* VGA, connector 0 */
6379 *conf = 0x00000028;
6380 } else
6381 if (idx == 3) {
6382 *conn = 0x02021322; /* HDMI, connector 1 */
6383 *conf = 0x00020010;
6384 } else {
6385 *conn = 0x0000000e; /* EOL */
6386 *conf = 0x00000000;
6387 }
6388 }
6389
Ben Skeggsdf4cf1b2010-07-01 11:31:45 +10006390 return true;
6391}
6392
Francisco Jerez2e5702a2010-11-19 18:08:47 +10006393static void
6394fabricate_dcb_encoder_table(struct drm_device *dev, struct nvbios *bios)
6395{
6396 struct dcb_table *dcb = &bios->dcb;
6397 int all_heads = (nv_two_heads(dev) ? 3 : 1);
6398
6399#ifdef __powerpc__
6400 /* Apple iMac G4 NV17 */
6401 if (of_machine_is_compatible("PowerMac4,5")) {
6402 fabricate_dcb_output(dcb, OUTPUT_TMDS, 0, all_heads, 1);
6403 fabricate_dcb_output(dcb, OUTPUT_ANALOG, 1, all_heads, 2);
6404 return;
6405 }
6406#endif
6407
6408 /* Make up some sane defaults */
Ben Skeggs0f8067c2011-11-10 14:49:19 +10006409 fabricate_dcb_output(dcb, OUTPUT_ANALOG,
6410 bios->legacy.i2c_indices.crt, 1, 1);
Francisco Jerez2e5702a2010-11-19 18:08:47 +10006411
6412 if (nv04_tv_identify(dev, bios->legacy.i2c_indices.tv) >= 0)
Ben Skeggs0f8067c2011-11-10 14:49:19 +10006413 fabricate_dcb_output(dcb, OUTPUT_TV,
6414 bios->legacy.i2c_indices.tv,
Francisco Jerez2e5702a2010-11-19 18:08:47 +10006415 all_heads, 0);
6416
6417 else if (bios->tmds.output0_script_ptr ||
6418 bios->tmds.output1_script_ptr)
Ben Skeggs0f8067c2011-11-10 14:49:19 +10006419 fabricate_dcb_output(dcb, OUTPUT_TMDS,
6420 bios->legacy.i2c_indices.panel,
Francisco Jerez2e5702a2010-11-19 18:08:47 +10006421 all_heads, 1);
6422}
6423
Ben Skeggsed42f822010-01-14 15:58:10 +10006424static int
Ben Skeggs6b5a81a2011-11-10 15:42:55 +10006425parse_dcb_entry(struct drm_device *dev, void *data, int idx, u8 *outp)
Ben Skeggs6ee73862009-12-11 19:24:15 +10006426{
Ben Skeggsed42f822010-01-14 15:58:10 +10006427 struct drm_nouveau_private *dev_priv = dev->dev_private;
Ben Skeggs6b5a81a2011-11-10 15:42:55 +10006428 struct dcb_table *dcb = &dev_priv->vbios.dcb;
6429 u32 conf = (dcb->version >= 0x20) ? ROM32(outp[4]) : ROM32(outp[6]);
6430 u32 conn = ROM32(outp[0]);
6431 bool ret;
6432
6433 if (apply_dcb_encoder_quirks(dev, idx, &conn, &conf)) {
6434 struct dcb_entry *entry = new_dcb_entry(dcb);
6435
6436 NV_TRACEWARN(dev, "DCB entry %02d: %08x %08x\n", idx, conn, conf);
6437
6438 if (dcb->version >= 0x20)
6439 ret = parse_dcb20_entry(dev, dcb, conn, conf, entry);
6440 else
6441 ret = parse_dcb15_entry(dev, dcb, conn, conf, entry);
6442 if (!ret)
6443 return 1; /* stop parsing */
Ben Skeggs6b5a81a2011-11-10 15:42:55 +10006444 }
6445
6446 return 0;
6447}
6448
6449static int
6450parse_dcb_table(struct drm_device *dev, struct nvbios *bios)
6451{
Ben Skeggs7f245b22010-02-24 09:56:18 +10006452 struct dcb_table *dcb = &bios->dcb;
Ben Skeggs6b5a81a2011-11-10 15:42:55 +10006453 u8 *dcbt;
Ben Skeggs6ee73862009-12-11 19:24:15 +10006454
Ben Skeggs6b5a81a2011-11-10 15:42:55 +10006455 dcbt = dcb_table(dev);
6456 if (!dcbt) {
6457 /* handle pre-DCB boards */
6458 if (bios->type == NVBIOS_BMP) {
6459 fabricate_dcb_encoder_table(dev, bios);
6460 return 0;
6461 }
6462
6463 return -EINVAL;
Ben Skeggsed42f822010-01-14 15:58:10 +10006464 }
Ben Skeggs6ee73862009-12-11 19:24:15 +10006465
Ben Skeggs6b5a81a2011-11-10 15:42:55 +10006466 NV_TRACE(dev, "DCB version %d.%d\n", dcbt[0] >> 4, dcbt[0] & 0xf);
Ben Skeggs6ee73862009-12-11 19:24:15 +10006467
Ben Skeggs6b5a81a2011-11-10 15:42:55 +10006468 dcb->version = dcbt[0];
6469 if (dcb->version >= 0x30) {
Ben Skeggs6b5a81a2011-11-10 15:42:55 +10006470 dcb->gpio_table_ptr = ROM16(dcbt[10]);
6471 dcb->connector_table_ptr = ROM16(dcbt[20]);
Ben Skeggs6ee73862009-12-11 19:24:15 +10006472 }
6473
Ben Skeggs6b5a81a2011-11-10 15:42:55 +10006474 dcb_outp_foreach(dev, NULL, parse_dcb_entry);
Ben Skeggs6ee73862009-12-11 19:24:15 +10006475
6476 /*
6477 * apart for v2.1+ not being known for requiring merging, this
6478 * guarantees dcbent->index is the index of the entry in the rom image
6479 */
Ben Skeggs7f245b22010-02-24 09:56:18 +10006480 if (dcb->version < 0x21)
Ben Skeggs6ee73862009-12-11 19:24:15 +10006481 merge_like_dcb_entries(dev, dcb);
6482
Ben Skeggs54abb5d2010-02-24 10:48:16 +10006483 if (!dcb->entries)
6484 return -ENXIO;
6485
6486 parse_dcb_gpio_table(bios);
6487 parse_dcb_connector_table(bios);
6488 return 0;
Ben Skeggs6ee73862009-12-11 19:24:15 +10006489}
6490
6491static void
6492fixup_legacy_connector(struct nvbios *bios)
6493{
Ben Skeggs7f245b22010-02-24 09:56:18 +10006494 struct dcb_table *dcb = &bios->dcb;
Ben Skeggsdc5bc4e2010-02-24 11:53:59 +10006495 int i, i2c, i2c_conn[DCB_MAX_NUM_I2C_ENTRIES] = { };
Ben Skeggs6ee73862009-12-11 19:24:15 +10006496
6497 /*
6498 * DCB 3.0 also has the table in most cases, but there are some cards
6499 * where the table is filled with stub entries, and the DCB entriy
6500 * indices are all 0. We don't need the connector indices on pre-G80
6501 * chips (yet?) so limit the use to DCB 4.0 and above.
6502 */
Ben Skeggs7f245b22010-02-24 09:56:18 +10006503 if (dcb->version >= 0x40)
Ben Skeggs6ee73862009-12-11 19:24:15 +10006504 return;
6505
Ben Skeggsdc5bc4e2010-02-24 11:53:59 +10006506 dcb->connector.entries = 0;
6507
Ben Skeggs6ee73862009-12-11 19:24:15 +10006508 /*
6509 * No known connector info before v3.0, so make it up. the rule here
6510 * is: anything on the same i2c bus is considered to be on the same
6511 * connector. any output without an associated i2c bus is assigned
6512 * its own unique connector index.
6513 */
6514 for (i = 0; i < dcb->entries; i++) {
Ben Skeggs6ee73862009-12-11 19:24:15 +10006515 /*
6516 * Ignore the I2C index for on-chip TV-out, as there
6517 * are cards with bogus values (nv31m in bug 23212),
6518 * and it's otherwise useless.
6519 */
6520 if (dcb->entry[i].type == OUTPUT_TV &&
Ben Skeggsdc5bc4e2010-02-24 11:53:59 +10006521 dcb->entry[i].location == DCB_LOC_ON_CHIP)
Ben Skeggs6ee73862009-12-11 19:24:15 +10006522 dcb->entry[i].i2c_index = 0xf;
Ben Skeggsdc5bc4e2010-02-24 11:53:59 +10006523 i2c = dcb->entry[i].i2c_index;
6524
6525 if (i2c_conn[i2c]) {
6526 dcb->entry[i].connector = i2c_conn[i2c] - 1;
Ben Skeggs6ee73862009-12-11 19:24:15 +10006527 continue;
6528 }
6529
Ben Skeggsdc5bc4e2010-02-24 11:53:59 +10006530 dcb->entry[i].connector = dcb->connector.entries++;
6531 if (i2c != 0xf)
6532 i2c_conn[i2c] = dcb->connector.entries;
Ben Skeggs6ee73862009-12-11 19:24:15 +10006533 }
6534
Ben Skeggsdc5bc4e2010-02-24 11:53:59 +10006535 /* Fake the connector table as well as just connector indices */
6536 for (i = 0; i < dcb->connector.entries; i++) {
6537 dcb->connector.entry[i].index = i;
6538 dcb->connector.entry[i].type = divine_connector_type(bios, i);
6539 dcb->connector.entry[i].gpio_tag = 0xff;
Ben Skeggs6ee73862009-12-11 19:24:15 +10006540 }
6541}
6542
Ben Skeggs6ee73862009-12-11 19:24:15 +10006543static int load_nv17_hwsq_ucode_entry(struct drm_device *dev, struct nvbios *bios, uint16_t hwsq_offset, int entry)
6544{
6545 /*
6546 * The header following the "HWSQ" signature has the number of entries,
6547 * and the entry size
6548 *
6549 * An entry consists of a dword to write to the sequencer control reg
6550 * (0x00001304), followed by the ucode bytes, written sequentially,
6551 * starting at reg 0x00001400
6552 */
6553
6554 uint8_t bytes_to_write;
6555 uint16_t hwsq_entry_offset;
6556 int i;
6557
6558 if (bios->data[hwsq_offset] <= entry) {
6559 NV_ERROR(dev, "Too few entries in HW sequencer table for "
6560 "requested entry\n");
6561 return -ENOENT;
6562 }
6563
6564 bytes_to_write = bios->data[hwsq_offset + 1];
6565
6566 if (bytes_to_write != 36) {
6567 NV_ERROR(dev, "Unknown HW sequencer entry size\n");
6568 return -EINVAL;
6569 }
6570
6571 NV_TRACE(dev, "Loading NV17 power sequencing microcode\n");
6572
6573 hwsq_entry_offset = hwsq_offset + 2 + entry * bytes_to_write;
6574
6575 /* set sequencer control */
6576 bios_wr32(bios, 0x00001304, ROM32(bios->data[hwsq_entry_offset]));
6577 bytes_to_write -= 4;
6578
6579 /* write ucode */
6580 for (i = 0; i < bytes_to_write; i += 4)
6581 bios_wr32(bios, 0x00001400 + i, ROM32(bios->data[hwsq_entry_offset + i + 4]));
6582
6583 /* twiddle NV_PBUS_DEBUG_4 */
6584 bios_wr32(bios, NV_PBUS_DEBUG_4, bios_rd32(bios, NV_PBUS_DEBUG_4) | 0x18);
6585
6586 return 0;
6587}
6588
6589static int load_nv17_hw_sequencer_ucode(struct drm_device *dev,
6590 struct nvbios *bios)
6591{
6592 /*
6593 * BMP based cards, from NV17, need a microcode loading to correctly
6594 * control the GPIO etc for LVDS panels
6595 *
6596 * BIT based cards seem to do this directly in the init scripts
6597 *
6598 * The microcode entries are found by the "HWSQ" signature.
6599 */
6600
6601 const uint8_t hwsq_signature[] = { 'H', 'W', 'S', 'Q' };
6602 const int sz = sizeof(hwsq_signature);
6603 int hwsq_offset;
6604
6605 hwsq_offset = findstr(bios->data, bios->length, hwsq_signature, sz);
6606 if (!hwsq_offset)
6607 return 0;
6608
6609 /* always use entry 0? */
6610 return load_nv17_hwsq_ucode_entry(dev, bios, hwsq_offset + sz, 0);
6611}
6612
6613uint8_t *nouveau_bios_embedded_edid(struct drm_device *dev)
6614{
6615 struct drm_nouveau_private *dev_priv = dev->dev_private;
Ben Skeggs04a39c52010-02-24 10:03:05 +10006616 struct nvbios *bios = &dev_priv->vbios;
Ben Skeggs6ee73862009-12-11 19:24:15 +10006617 const uint8_t edid_sig[] = {
6618 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00 };
6619 uint16_t offset = 0;
6620 uint16_t newoffset;
6621 int searchlen = NV_PROM_SIZE;
6622
6623 if (bios->fp.edid)
6624 return bios->fp.edid;
6625
6626 while (searchlen) {
6627 newoffset = findstr(&bios->data[offset], searchlen,
6628 edid_sig, 8);
6629 if (!newoffset)
6630 return NULL;
6631 offset += newoffset;
6632 if (!nv_cksum(&bios->data[offset], EDID1_LEN))
6633 break;
6634
6635 searchlen -= offset;
6636 offset++;
6637 }
6638
6639 NV_TRACE(dev, "Found EDID in BIOS\n");
6640
6641 return bios->fp.edid = &bios->data[offset];
6642}
6643
6644void
6645nouveau_bios_run_init_table(struct drm_device *dev, uint16_t table,
Ben Skeggs02e4f582011-07-06 21:21:42 +10006646 struct dcb_entry *dcbent, int crtc)
Ben Skeggs6ee73862009-12-11 19:24:15 +10006647{
6648 struct drm_nouveau_private *dev_priv = dev->dev_private;
Ben Skeggs04a39c52010-02-24 10:03:05 +10006649 struct nvbios *bios = &dev_priv->vbios;
Ben Skeggs6ee73862009-12-11 19:24:15 +10006650 struct init_exec iexec = { true, false };
6651
Ben Skeggsc7ca4d12011-02-03 20:10:49 +10006652 spin_lock_bh(&bios->lock);
Ben Skeggs6ee73862009-12-11 19:24:15 +10006653 bios->display.output = dcbent;
Ben Skeggs02e4f582011-07-06 21:21:42 +10006654 bios->display.crtc = crtc;
Ben Skeggs6ee73862009-12-11 19:24:15 +10006655 parse_init_table(bios, table, &iexec);
6656 bios->display.output = NULL;
Ben Skeggsc7ca4d12011-02-03 20:10:49 +10006657 spin_unlock_bh(&bios->lock);
Ben Skeggs6ee73862009-12-11 19:24:15 +10006658}
6659
Ben Skeggs59ef9742011-08-12 10:05:43 +10006660void
6661nouveau_bios_init_exec(struct drm_device *dev, uint16_t table)
6662{
6663 struct drm_nouveau_private *dev_priv = dev->dev_private;
6664 struct nvbios *bios = &dev_priv->vbios;
6665 struct init_exec iexec = { true, false };
6666
6667 parse_init_table(bios, table, &iexec);
6668}
6669
Ben Skeggs6ee73862009-12-11 19:24:15 +10006670static bool NVInitVBIOS(struct drm_device *dev)
6671{
6672 struct drm_nouveau_private *dev_priv = dev->dev_private;
Ben Skeggs04a39c52010-02-24 10:03:05 +10006673 struct nvbios *bios = &dev_priv->vbios;
Ben Skeggs6ee73862009-12-11 19:24:15 +10006674
6675 memset(bios, 0, sizeof(struct nvbios));
Ben Skeggsc7ca4d12011-02-03 20:10:49 +10006676 spin_lock_init(&bios->lock);
Ben Skeggs6ee73862009-12-11 19:24:15 +10006677 bios->dev = dev;
6678
6679 if (!NVShadowVBIOS(dev, bios->data))
6680 return false;
6681
6682 bios->length = NV_PROM_SIZE;
6683 return true;
6684}
6685
6686static int nouveau_parse_vbios_struct(struct drm_device *dev)
6687{
6688 struct drm_nouveau_private *dev_priv = dev->dev_private;
Ben Skeggs04a39c52010-02-24 10:03:05 +10006689 struct nvbios *bios = &dev_priv->vbios;
Ben Skeggs6ee73862009-12-11 19:24:15 +10006690 const uint8_t bit_signature[] = { 0xff, 0xb8, 'B', 'I', 'T' };
6691 const uint8_t bmp_signature[] = { 0xff, 0x7f, 'N', 'V', 0x0 };
6692 int offset;
6693
6694 offset = findstr(bios->data, bios->length,
6695 bit_signature, sizeof(bit_signature));
6696 if (offset) {
6697 NV_TRACE(dev, "BIT BIOS found\n");
Ben Skeggs4709bff2010-09-13 15:18:40 +10006698 bios->type = NVBIOS_BIT;
6699 bios->offset = offset;
Ben Skeggs6ee73862009-12-11 19:24:15 +10006700 return parse_bit_structure(bios, offset + 6);
6701 }
6702
6703 offset = findstr(bios->data, bios->length,
6704 bmp_signature, sizeof(bmp_signature));
6705 if (offset) {
6706 NV_TRACE(dev, "BMP BIOS found\n");
Ben Skeggs4709bff2010-09-13 15:18:40 +10006707 bios->type = NVBIOS_BMP;
6708 bios->offset = offset;
Ben Skeggs6ee73862009-12-11 19:24:15 +10006709 return parse_bmp_structure(dev, bios, offset);
6710 }
6711
6712 NV_ERROR(dev, "No known BIOS signature found\n");
6713 return -ENODEV;
6714}
6715
6716int
6717nouveau_run_vbios_init(struct drm_device *dev)
6718{
6719 struct drm_nouveau_private *dev_priv = dev->dev_private;
Ben Skeggs04a39c52010-02-24 10:03:05 +10006720 struct nvbios *bios = &dev_priv->vbios;
Ben Skeggs6ee73862009-12-11 19:24:15 +10006721 int i, ret = 0;
6722
Francisco Jerez946fd352010-07-24 17:41:48 +02006723 /* Reset the BIOS head to 0. */
6724 bios->state.crtchead = 0;
Ben Skeggs6ee73862009-12-11 19:24:15 +10006725
6726 if (bios->major_version < 5) /* BMP only */
6727 load_nv17_hw_sequencer_ucode(dev, bios);
6728
6729 if (bios->execute) {
6730 bios->fp.last_script_invoc = 0;
6731 bios->fp.lvds_init_run = false;
6732 }
6733
6734 parse_init_tables(bios);
6735
6736 /*
6737 * Runs some additional script seen on G8x VBIOSen. The VBIOS'
6738 * parser will run this right after the init tables, the binary
6739 * driver appears to run it at some point later.
6740 */
6741 if (bios->some_script_ptr) {
6742 struct init_exec iexec = {true, false};
6743
6744 NV_INFO(dev, "Parsing VBIOS init table at offset 0x%04X\n",
6745 bios->some_script_ptr);
6746 parse_init_table(bios, bios->some_script_ptr, &iexec);
6747 }
6748
6749 if (dev_priv->card_type >= NV_50) {
Ben Skeggs7f245b22010-02-24 09:56:18 +10006750 for (i = 0; i < bios->dcb.entries; i++) {
Ben Skeggs02e4f582011-07-06 21:21:42 +10006751 nouveau_bios_run_display_table(dev, 0, 0,
6752 &bios->dcb.entry[i], -1);
Ben Skeggs6ee73862009-12-11 19:24:15 +10006753 }
6754 }
6755
Ben Skeggs6ee73862009-12-11 19:24:15 +10006756 return ret;
6757}
6758
Ben Skeggsd13102c2010-05-25 13:47:16 +10006759static bool
6760nouveau_bios_posted(struct drm_device *dev)
6761{
6762 struct drm_nouveau_private *dev_priv = dev->dev_private;
Ben Skeggsd13102c2010-05-25 13:47:16 +10006763 unsigned htotal;
6764
Francisco Jerezc1b60ec2010-11-11 00:56:37 +01006765 if (dev_priv->card_type >= NV_50) {
Ben Skeggsd13102c2010-05-25 13:47:16 +10006766 if (NVReadVgaCrtc(dev, 0, 0x00) == 0 &&
6767 NVReadVgaCrtc(dev, 0, 0x1a) == 0)
6768 return false;
6769 return true;
6770 }
6771
Ben Skeggsd13102c2010-05-25 13:47:16 +10006772 htotal = NVReadVgaCrtc(dev, 0, 0x06);
6773 htotal |= (NVReadVgaCrtc(dev, 0, 0x07) & 0x01) << 8;
6774 htotal |= (NVReadVgaCrtc(dev, 0, 0x07) & 0x20) << 4;
6775 htotal |= (NVReadVgaCrtc(dev, 0, 0x25) & 0x01) << 10;
6776 htotal |= (NVReadVgaCrtc(dev, 0, 0x41) & 0x01) << 11;
Francisco Jerez03cd06c2010-07-20 03:08:25 +02006777
Ben Skeggsd13102c2010-05-25 13:47:16 +10006778 return (htotal != 0);
6779}
6780
Ben Skeggs6ee73862009-12-11 19:24:15 +10006781int
6782nouveau_bios_init(struct drm_device *dev)
6783{
6784 struct drm_nouveau_private *dev_priv = dev->dev_private;
Ben Skeggs04a39c52010-02-24 10:03:05 +10006785 struct nvbios *bios = &dev_priv->vbios;
Ben Skeggs6ee73862009-12-11 19:24:15 +10006786 int ret;
6787
Ben Skeggs6ee73862009-12-11 19:24:15 +10006788 if (!NVInitVBIOS(dev))
6789 return -ENODEV;
6790
6791 ret = nouveau_parse_vbios_struct(dev);
6792 if (ret)
6793 return ret;
6794
Ben Skeggs486a45c2011-11-11 10:22:19 +10006795 ret = nouveau_i2c_init(dev);
6796 if (ret)
6797 return ret;
6798
Francisco Jerez2e5702a2010-11-19 18:08:47 +10006799 ret = parse_dcb_table(dev, bios);
Ben Skeggs6ee73862009-12-11 19:24:15 +10006800 if (ret)
6801 return ret;
6802
Ben Skeggs6ee73862009-12-11 19:24:15 +10006803 fixup_legacy_connector(bios);
6804
6805 if (!bios->major_version) /* we don't run version 0 bios */
6806 return 0;
6807
Ben Skeggs6ee73862009-12-11 19:24:15 +10006808 /* init script execution disabled */
6809 bios->execute = false;
6810
6811 /* ... unless card isn't POSTed already */
Ben Skeggsd13102c2010-05-25 13:47:16 +10006812 if (!nouveau_bios_posted(dev)) {
Francisco Jerez67eda202010-07-13 15:59:50 +02006813 NV_INFO(dev, "Adaptor not initialised, "
6814 "running VBIOS init tables.\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +10006815 bios->execute = true;
6816 }
Marcin Koƛcielnicki0cba1b72010-09-29 11:15:01 +00006817 if (nouveau_force_post)
6818 bios->execute = true;
Ben Skeggs6ee73862009-12-11 19:24:15 +10006819
Ben Skeggs6ee73862009-12-11 19:24:15 +10006820 ret = nouveau_run_vbios_init(dev);
Ben Skeggs04a39c52010-02-24 10:03:05 +10006821 if (ret)
Ben Skeggs6ee73862009-12-11 19:24:15 +10006822 return ret;
Ben Skeggs6ee73862009-12-11 19:24:15 +10006823
6824 /* feature_byte on BMP is poor, but init always sets CR4B */
Ben Skeggs6ee73862009-12-11 19:24:15 +10006825 if (bios->major_version < 5)
6826 bios->is_mobile = NVReadVgaCrtc(dev, 0, NV_CIO_CRE_4B) & 0x40;
6827
6828 /* all BIT systems need p_f_m_t for digital_min_front_porch */
6829 if (bios->is_mobile || bios->major_version >= 5)
6830 ret = parse_fp_mode_table(dev, bios);
Ben Skeggs6ee73862009-12-11 19:24:15 +10006831
6832 /* allow subsequent scripts to execute */
6833 bios->execute = true;
6834
6835 return 0;
6836}
6837
6838void
6839nouveau_bios_takedown(struct drm_device *dev)
6840{
Ben Skeggs486a45c2011-11-11 10:22:19 +10006841 nouveau_i2c_fini(dev);
Ben Skeggs6ee73862009-12-11 19:24:15 +10006842}