drm/nouveau/device: include the official chipset names
[GitHub/exynos8895/android_kernel_samsung_universal8895.git] / drivers / gpu / drm / nouveau / nouveau_bios.c
CommitLineData
6ee73861
BS
1/*
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"
25908b77 29#include "nouveau_encoder.h"
6ee73861 30
67eda20e 31#include <linux/io-mapping.h>
78339fb7 32#include <linux/firmware.h>
67eda20e 33
6ee73861
BS
34/* these defines are made up */
35#define NV_CIO_CRE_44_HEADA 0x0
36#define NV_CIO_CRE_44_HEADB 0x3
37#define FEATURE_MOBILE 0x10 /* also FEATURE_QUADRO for BMP */
6ee73861
BS
38
39#define EDID1_LEN 128
40
41#define BIOSLOG(sip, fmt, arg...) NV_DEBUG(sip->dev, fmt, ##arg)
42#define LOG_OLD_VALUE(x)
43
6ee73861
BS
44struct init_exec {
45 bool execute;
46 bool repeat;
47};
48
49static bool nv_cksum(const uint8_t *data, unsigned int length)
50{
51 /*
52 * There's a few checksums in the BIOS, so here's a generic checking
53 * function.
54 */
55 int i;
56 uint8_t sum = 0;
57
58 for (i = 0; i < length; i++)
59 sum += data[i];
60
61 if (sum)
62 return true;
63
64 return false;
65}
66
6ee73861
BS
67static uint16_t clkcmptable(struct nvbios *bios, uint16_t clktable, int pxclk)
68{
69 int compare_record_len, i = 0;
70 uint16_t compareclk, scriptptr = 0;
71
72 if (bios->major_version < 5) /* pre BIT */
73 compare_record_len = 3;
74 else
75 compare_record_len = 4;
76
77 do {
78 compareclk = ROM16(bios->data[clktable + compare_record_len * i]);
79 if (pxclk >= compareclk * 10) {
80 if (bios->major_version < 5) {
81 uint8_t tmdssub = bios->data[clktable + 2 + compare_record_len * i];
82 scriptptr = ROM16(bios->data[bios->init_script_tbls_ptr + tmdssub * 2]);
83 } else
84 scriptptr = ROM16(bios->data[clktable + 2 + compare_record_len * i]);
85 break;
86 }
87 i++;
88 } while (compareclk);
89
90 return scriptptr;
91}
92
93static void
94run_digital_op_script(struct drm_device *dev, uint16_t scriptptr,
cb75d97e 95 struct dcb_output *dcbent, int head, bool dl)
6ee73861 96{
6ee73861
BS
97 NV_TRACE(dev, "0x%04X: Parsing digital output script table\n",
98 scriptptr);
cb75d97e
BS
99 NVWriteVgaCrtc(dev, 0, NV_CIO_CRE_44, head ? NV_CIO_CRE_44_HEADB :
100 NV_CIO_CRE_44_HEADA);
101 nouveau_bios_run_init_table(dev, scriptptr, dcbent, head);
6ee73861
BS
102
103 nv04_dfp_bind_head(dev, dcbent, head, dl);
104}
105
cb75d97e 106static int call_lvds_manufacturer_script(struct drm_device *dev, struct dcb_output *dcbent, int head, enum LVDS_script script)
6ee73861
BS
107{
108 struct drm_nouveau_private *dev_priv = dev->dev_private;
04a39c57 109 struct nvbios *bios = &dev_priv->vbios;
cb75d97e 110 uint8_t sub = bios->data[bios->fp.xlated_entry + script] + (bios->fp.link_c_increment && dcbent->or & DCB_OUTPUT_C ? 1 : 0);
6ee73861
BS
111 uint16_t scriptofs = ROM16(bios->data[bios->init_script_tbls_ptr + sub * 2]);
112
113 if (!bios->fp.xlated_entry || !sub || !scriptofs)
114 return -EINVAL;
115
116 run_digital_op_script(dev, scriptofs, dcbent, head, bios->fp.dual_link);
117
118 if (script == LVDS_PANEL_OFF) {
119 /* off-on delay in ms */
c7ca4d1b 120 mdelay(ROM16(bios->data[bios->fp.xlated_entry + 7]));
6ee73861
BS
121 }
122#ifdef __powerpc__
123 /* Powerbook specific quirks */
d31e078d
FJ
124 if (script == LVDS_RESET &&
125 (dev->pci_device == 0x0179 || dev->pci_device == 0x0189 ||
126 dev->pci_device == 0x0329))
127 nv_write_tmds(dev, dcbent->or, 0, 0x02, 0x72);
6ee73861
BS
128#endif
129
130 return 0;
131}
132
cb75d97e 133static int run_lvds_table(struct drm_device *dev, struct dcb_output *dcbent, int head, enum LVDS_script script, int pxclk)
6ee73861
BS
134{
135 /*
136 * The BIT LVDS table's header has the information to setup the
137 * necessary registers. Following the standard 4 byte header are:
138 * A bitmask byte and a dual-link transition pxclk value for use in
139 * selecting the init script when not using straps; 4 script pointers
140 * for panel power, selected by output and on/off; and 8 table pointers
141 * for panel init, the needed one determined by output, and bits in the
142 * conf byte. These tables are similar to the TMDS tables, consisting
143 * of a list of pxclks and script pointers.
144 */
145 struct drm_nouveau_private *dev_priv = dev->dev_private;
04a39c57 146 struct nvbios *bios = &dev_priv->vbios;
6ee73861
BS
147 unsigned int outputset = (dcbent->or == 4) ? 1 : 0;
148 uint16_t scriptptr = 0, clktable;
6ee73861
BS
149
150 /*
151 * For now we assume version 3.0 table - g80 support will need some
152 * changes
153 */
154
155 switch (script) {
156 case LVDS_INIT:
157 return -ENOSYS;
158 case LVDS_BACKLIGHT_ON:
159 case LVDS_PANEL_ON:
160 scriptptr = ROM16(bios->data[bios->fp.lvdsmanufacturerpointer + 7 + outputset * 2]);
161 break;
162 case LVDS_BACKLIGHT_OFF:
163 case LVDS_PANEL_OFF:
164 scriptptr = ROM16(bios->data[bios->fp.lvdsmanufacturerpointer + 11 + outputset * 2]);
165 break;
166 case LVDS_RESET:
f3bbb9cc
BS
167 clktable = bios->fp.lvdsmanufacturerpointer + 15;
168 if (dcbent->or == 4)
169 clktable += 8;
170
6ee73861
BS
171 if (dcbent->lvdsconf.use_straps_for_mode) {
172 if (bios->fp.dual_link)
f3bbb9cc
BS
173 clktable += 4;
174 if (bios->fp.if_is_24bit)
175 clktable += 2;
6ee73861
BS
176 } else {
177 /* using EDID */
f3bbb9cc 178 int cmpval_24bit = (dcbent->or == 4) ? 4 : 1;
6ee73861
BS
179
180 if (bios->fp.dual_link) {
f3bbb9cc
BS
181 clktable += 4;
182 cmpval_24bit <<= 1;
6ee73861 183 }
f3bbb9cc
BS
184
185 if (bios->fp.strapless_is_24bit & cmpval_24bit)
186 clktable += 2;
6ee73861
BS
187 }
188
f3bbb9cc 189 clktable = ROM16(bios->data[clktable]);
6ee73861
BS
190 if (!clktable) {
191 NV_ERROR(dev, "Pixel clock comparison table not found\n");
192 return -ENOENT;
193 }
194 scriptptr = clkcmptable(bios, clktable, pxclk);
195 }
196
197 if (!scriptptr) {
198 NV_ERROR(dev, "LVDS output init script not found\n");
199 return -ENOENT;
200 }
201 run_digital_op_script(dev, scriptptr, dcbent, head, bios->fp.dual_link);
202
203 return 0;
204}
205
cb75d97e 206int call_lvds_script(struct drm_device *dev, struct dcb_output *dcbent, int head, enum LVDS_script script, int pxclk)
6ee73861
BS
207{
208 /*
209 * LVDS operations are multiplexed in an effort to present a single API
210 * which works with two vastly differing underlying structures.
211 * This acts as the demux
212 */
213
214 struct drm_nouveau_private *dev_priv = dev->dev_private;
04a39c57 215 struct nvbios *bios = &dev_priv->vbios;
6ee73861
BS
216 uint8_t lvds_ver = bios->data[bios->fp.lvdsmanufacturerpointer];
217 uint32_t sel_clk_binding, sel_clk;
218 int ret;
219
220 if (bios->fp.last_script_invoc == (script << 1 | head) || !lvds_ver ||
221 (lvds_ver >= 0x30 && script == LVDS_INIT))
222 return 0;
223
224 if (!bios->fp.lvds_init_run) {
225 bios->fp.lvds_init_run = true;
226 call_lvds_script(dev, dcbent, head, LVDS_INIT, pxclk);
227 }
228
229 if (script == LVDS_PANEL_ON && bios->fp.reset_after_pclk_change)
230 call_lvds_script(dev, dcbent, head, LVDS_RESET, pxclk);
231 if (script == LVDS_RESET && bios->fp.power_off_for_reset)
232 call_lvds_script(dev, dcbent, head, LVDS_PANEL_OFF, pxclk);
233
234 NV_TRACE(dev, "Calling LVDS script %d:\n", script);
235
236 /* don't let script change pll->head binding */
cb75d97e 237 sel_clk_binding = nv_rd32(dev, NV_PRAMDAC_SEL_CLK) & 0x50000;
6ee73861
BS
238
239 if (lvds_ver < 0x30)
240 ret = call_lvds_manufacturer_script(dev, dcbent, head, script);
241 else
242 ret = run_lvds_table(dev, dcbent, head, script, pxclk);
243
244 bios->fp.last_script_invoc = (script << 1 | head);
245
246 sel_clk = NVReadRAMDAC(dev, 0, NV_PRAMDAC_SEL_CLK) & ~0x50000;
247 NVWriteRAMDAC(dev, 0, NV_PRAMDAC_SEL_CLK, sel_clk | sel_clk_binding);
248 /* some scripts set a value in NV_PBUS_POWERCTRL_2 and break video overlay */
84058eb8 249 nv_wr32(dev, NV_PBUS_POWERCTRL_2, 0);
6ee73861
BS
250
251 return ret;
252}
253
254struct lvdstableheader {
255 uint8_t lvds_ver, headerlen, recordlen;
256};
257
258static int parse_lvds_manufacturer_table_header(struct drm_device *dev, struct nvbios *bios, struct lvdstableheader *lth)
259{
260 /*
261 * BMP version (0xa) LVDS table has a simple header of version and
262 * record length. The BIT LVDS table has the typical BIT table header:
263 * version byte, header length byte, record length byte, and a byte for
264 * the maximum number of records that can be held in the table.
265 */
266
267 uint8_t lvds_ver, headerlen, recordlen;
268
269 memset(lth, 0, sizeof(struct lvdstableheader));
270
271 if (bios->fp.lvdsmanufacturerpointer == 0x0) {
272 NV_ERROR(dev, "Pointer to LVDS manufacturer table invalid\n");
273 return -EINVAL;
274 }
275
276 lvds_ver = bios->data[bios->fp.lvdsmanufacturerpointer];
277
278 switch (lvds_ver) {
279 case 0x0a: /* pre NV40 */
280 headerlen = 2;
281 recordlen = bios->data[bios->fp.lvdsmanufacturerpointer + 1];
282 break;
283 case 0x30: /* NV4x */
284 headerlen = bios->data[bios->fp.lvdsmanufacturerpointer + 1];
285 if (headerlen < 0x1f) {
286 NV_ERROR(dev, "LVDS table header not understood\n");
287 return -EINVAL;
288 }
289 recordlen = bios->data[bios->fp.lvdsmanufacturerpointer + 2];
290 break;
291 case 0x40: /* G80/G90 */
292 headerlen = bios->data[bios->fp.lvdsmanufacturerpointer + 1];
293 if (headerlen < 0x7) {
294 NV_ERROR(dev, "LVDS table header not understood\n");
295 return -EINVAL;
296 }
297 recordlen = bios->data[bios->fp.lvdsmanufacturerpointer + 2];
298 break;
299 default:
300 NV_ERROR(dev,
301 "LVDS table revision %d.%d not currently supported\n",
302 lvds_ver >> 4, lvds_ver & 0xf);
303 return -ENOSYS;
304 }
305
306 lth->lvds_ver = lvds_ver;
307 lth->headerlen = headerlen;
308 lth->recordlen = recordlen;
309
310 return 0;
311}
312
313static int
314get_fp_strap(struct drm_device *dev, struct nvbios *bios)
315{
316 struct drm_nouveau_private *dev_priv = dev->dev_private;
317
318 /*
319 * The fp strap is normally dictated by the "User Strap" in
320 * PEXTDEV_BOOT_0[20:16], but on BMP cards when bit 2 of the
321 * Internal_Flags struct at 0x48 is set, the user strap gets overriden
322 * by the PCI subsystem ID during POST, but not before the previous user
323 * strap has been committed to CR58 for CR57=0xf on head A, which may be
324 * read and used instead
325 */
326
327 if (bios->major_version < 5 && bios->data[0x48] & 0x4)
328 return NVReadVgaCrtc5758(dev, 0, 0xf) & 0xf;
329
330 if (dev_priv->card_type >= NV_50)
cb75d97e 331 return (nv_rd32(dev, NV_PEXTDEV_BOOT_0) >> 24) & 0xf;
6ee73861 332 else
cb75d97e 333 return (nv_rd32(dev, NV_PEXTDEV_BOOT_0) >> 16) & 0xf;
6ee73861
BS
334}
335
336static int parse_fp_mode_table(struct drm_device *dev, struct nvbios *bios)
337{
338 uint8_t *fptable;
339 uint8_t fptable_ver, headerlen = 0, recordlen, fpentries = 0xf, fpindex;
340 int ret, ofs, fpstrapping;
341 struct lvdstableheader lth;
342
343 if (bios->fp.fptablepointer == 0x0) {
344 /* Apple cards don't have the fp table; the laptops use DDC */
345 /* The table is also missing on some x86 IGPs */
346#ifndef __powerpc__
347 NV_ERROR(dev, "Pointer to flat panel table invalid\n");
348#endif
04a39c57 349 bios->digital_min_front_porch = 0x4b;
6ee73861
BS
350 return 0;
351 }
352
353 fptable = &bios->data[bios->fp.fptablepointer];
354 fptable_ver = fptable[0];
355
356 switch (fptable_ver) {
357 /*
358 * BMP version 0x5.0x11 BIOSen have version 1 like tables, but no
359 * version field, and miss one of the spread spectrum/PWM bytes.
360 * This could affect early GF2Go parts (not seen any appropriate ROMs
361 * though). Here we assume that a version of 0x05 matches this case
362 * (combining with a BMP version check would be better), as the
363 * common case for the panel type field is 0x0005, and that is in
364 * fact what we are reading the first byte of.
365 */
366 case 0x05: /* some NV10, 11, 15, 16 */
367 recordlen = 42;
368 ofs = -1;
369 break;
370 case 0x10: /* some NV15/16, and NV11+ */
371 recordlen = 44;
372 ofs = 0;
373 break;
374 case 0x20: /* NV40+ */
375 headerlen = fptable[1];
376 recordlen = fptable[2];
377 fpentries = fptable[3];
378 /*
379 * fptable[4] is the minimum
380 * RAMDAC_FP_HCRTC -> RAMDAC_FP_HSYNC_START gap
381 */
04a39c57 382 bios->digital_min_front_porch = fptable[4];
6ee73861
BS
383 ofs = -7;
384 break;
385 default:
386 NV_ERROR(dev,
387 "FP table revision %d.%d not currently supported\n",
388 fptable_ver >> 4, fptable_ver & 0xf);
389 return -ENOSYS;
390 }
391
392 if (!bios->is_mobile) /* !mobile only needs digital_min_front_porch */
393 return 0;
394
395 ret = parse_lvds_manufacturer_table_header(dev, bios, &lth);
396 if (ret)
397 return ret;
398
399 if (lth.lvds_ver == 0x30 || lth.lvds_ver == 0x40) {
400 bios->fp.fpxlatetableptr = bios->fp.lvdsmanufacturerpointer +
401 lth.headerlen + 1;
402 bios->fp.xlatwidth = lth.recordlen;
403 }
404 if (bios->fp.fpxlatetableptr == 0x0) {
405 NV_ERROR(dev, "Pointer to flat panel xlat table invalid\n");
406 return -EINVAL;
407 }
408
409 fpstrapping = get_fp_strap(dev, bios);
410
411 fpindex = bios->data[bios->fp.fpxlatetableptr +
412 fpstrapping * bios->fp.xlatwidth];
413
414 if (fpindex > fpentries) {
415 NV_ERROR(dev, "Bad flat panel table index\n");
416 return -ENOENT;
417 }
418
419 /* nv4x cards need both a strap value and fpindex of 0xf to use DDC */
420 if (lth.lvds_ver > 0x10)
04a39c57 421 bios->fp_no_ddc = fpstrapping != 0xf || fpindex != 0xf;
6ee73861
BS
422
423 /*
424 * If either the strap or xlated fpindex value are 0xf there is no
425 * panel using a strap-derived bios mode present. this condition
426 * includes, but is different from, the DDC panel indicator above
427 */
428 if (fpstrapping == 0xf || fpindex == 0xf)
429 return 0;
430
431 bios->fp.mode_ptr = bios->fp.fptablepointer + headerlen +
432 recordlen * fpindex + ofs;
433
434 NV_TRACE(dev, "BIOS FP mode: %dx%d (%dkHz pixel clock)\n",
435 ROM16(bios->data[bios->fp.mode_ptr + 11]) + 1,
436 ROM16(bios->data[bios->fp.mode_ptr + 25]) + 1,
437 ROM16(bios->data[bios->fp.mode_ptr + 7]) * 10);
438
439 return 0;
440}
441
442bool nouveau_bios_fp_mode(struct drm_device *dev, struct drm_display_mode *mode)
443{
444 struct drm_nouveau_private *dev_priv = dev->dev_private;
04a39c57 445 struct nvbios *bios = &dev_priv->vbios;
6ee73861
BS
446 uint8_t *mode_entry = &bios->data[bios->fp.mode_ptr];
447
448 if (!mode) /* just checking whether we can produce a mode */
449 return bios->fp.mode_ptr;
450
451 memset(mode, 0, sizeof(struct drm_display_mode));
452 /*
453 * For version 1.0 (version in byte 0):
454 * bytes 1-2 are "panel type", including bits on whether Colour/mono,
455 * single/dual link, and type (TFT etc.)
456 * bytes 3-6 are bits per colour in RGBX
457 */
458 mode->clock = ROM16(mode_entry[7]) * 10;
459 /* bytes 9-10 is HActive */
460 mode->hdisplay = ROM16(mode_entry[11]) + 1;
461 /*
462 * bytes 13-14 is HValid Start
463 * bytes 15-16 is HValid End
464 */
465 mode->hsync_start = ROM16(mode_entry[17]) + 1;
466 mode->hsync_end = ROM16(mode_entry[19]) + 1;
467 mode->htotal = ROM16(mode_entry[21]) + 1;
468 /* bytes 23-24, 27-30 similarly, but vertical */
469 mode->vdisplay = ROM16(mode_entry[25]) + 1;
470 mode->vsync_start = ROM16(mode_entry[31]) + 1;
471 mode->vsync_end = ROM16(mode_entry[33]) + 1;
472 mode->vtotal = ROM16(mode_entry[35]) + 1;
473 mode->flags |= (mode_entry[37] & 0x10) ?
474 DRM_MODE_FLAG_PHSYNC : DRM_MODE_FLAG_NHSYNC;
475 mode->flags |= (mode_entry[37] & 0x1) ?
476 DRM_MODE_FLAG_PVSYNC : DRM_MODE_FLAG_NVSYNC;
477 /*
478 * bytes 38-39 relate to spread spectrum settings
479 * bytes 40-43 are something to do with PWM
480 */
481
482 mode->status = MODE_OK;
483 mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
484 drm_mode_set_name(mode);
485 return bios->fp.mode_ptr;
486}
487
488int nouveau_bios_parse_lvds_table(struct drm_device *dev, int pxclk, bool *dl, bool *if_is_24bit)
489{
490 /*
491 * The LVDS table header is (mostly) described in
492 * parse_lvds_manufacturer_table_header(): the BIT header additionally
493 * contains the dual-link transition pxclk (in 10s kHz), at byte 5 - if
494 * straps are not being used for the panel, this specifies the frequency
495 * at which modes should be set up in the dual link style.
496 *
497 * Following the header, the BMP (ver 0xa) table has several records,
3ad2f3fb 498 * indexed by a separate xlat table, indexed in turn by the fp strap in
6ee73861
BS
499 * EXTDEV_BOOT. Each record had a config byte, followed by 6 script
500 * numbers for use by INIT_SUB which controlled panel init and power,
501 * and finally a dword of ms to sleep between power off and on
502 * operations.
503 *
504 * In the BIT versions, the table following the header serves as an
505 * integrated config and xlat table: the records in the table are
506 * indexed by the FP strap nibble in EXTDEV_BOOT, and each record has
507 * two bytes - the first as a config byte, the second for indexing the
508 * fp mode table pointed to by the BIT 'D' table
509 *
510 * DDC is not used until after card init, so selecting the correct table
511 * entry and setting the dual link flag for EDID equipped panels,
512 * requiring tests against the native-mode pixel clock, cannot be done
513 * until later, when this function should be called with non-zero pxclk
514 */
515 struct drm_nouveau_private *dev_priv = dev->dev_private;
04a39c57 516 struct nvbios *bios = &dev_priv->vbios;
6ee73861
BS
517 int fpstrapping = get_fp_strap(dev, bios), lvdsmanufacturerindex = 0;
518 struct lvdstableheader lth;
519 uint16_t lvdsofs;
04a39c57 520 int ret, chip_version = bios->chip_version;
6ee73861
BS
521
522 ret = parse_lvds_manufacturer_table_header(dev, bios, &lth);
523 if (ret)
524 return ret;
525
526 switch (lth.lvds_ver) {
527 case 0x0a: /* pre NV40 */
528 lvdsmanufacturerindex = bios->data[
529 bios->fp.fpxlatemanufacturertableptr +
530 fpstrapping];
531
532 /* we're done if this isn't the EDID panel case */
533 if (!pxclk)
534 break;
535
536 if (chip_version < 0x25) {
537 /* nv17 behaviour
538 *
539 * It seems the old style lvds script pointer is reused
540 * to select 18/24 bit colour depth for EDID panels.
541 */
542 lvdsmanufacturerindex =
543 (bios->legacy.lvds_single_a_script_ptr & 1) ?
544 2 : 0;
545 if (pxclk >= bios->fp.duallink_transition_clk)
546 lvdsmanufacturerindex++;
547 } else if (chip_version < 0x30) {
548 /* nv28 behaviour (off-chip encoder)
549 *
550 * nv28 does a complex dance of first using byte 121 of
551 * the EDID to choose the lvdsmanufacturerindex, then
552 * later attempting to match the EDID manufacturer and
553 * product IDs in a table (signature 'pidt' (panel id
554 * table?)), setting an lvdsmanufacturerindex of 0 and
555 * an fp strap of the match index (or 0xf if none)
556 */
557 lvdsmanufacturerindex = 0;
558 } else {
559 /* nv31, nv34 behaviour */
560 lvdsmanufacturerindex = 0;
561 if (pxclk >= bios->fp.duallink_transition_clk)
562 lvdsmanufacturerindex = 2;
563 if (pxclk >= 140000)
564 lvdsmanufacturerindex = 3;
565 }
566
567 /*
568 * nvidia set the high nibble of (cr57=f, cr58) to
569 * lvdsmanufacturerindex in this case; we don't
570 */
571 break;
572 case 0x30: /* NV4x */
573 case 0x40: /* G80/G90 */
574 lvdsmanufacturerindex = fpstrapping;
575 break;
576 default:
577 NV_ERROR(dev, "LVDS table revision not currently supported\n");
578 return -ENOSYS;
579 }
580
581 lvdsofs = bios->fp.xlated_entry = bios->fp.lvdsmanufacturerpointer + lth.headerlen + lth.recordlen * lvdsmanufacturerindex;
582 switch (lth.lvds_ver) {
583 case 0x0a:
584 bios->fp.power_off_for_reset = bios->data[lvdsofs] & 1;
585 bios->fp.reset_after_pclk_change = bios->data[lvdsofs] & 2;
586 bios->fp.dual_link = bios->data[lvdsofs] & 4;
587 bios->fp.link_c_increment = bios->data[lvdsofs] & 8;
588 *if_is_24bit = bios->data[lvdsofs] & 16;
589 break;
590 case 0x30:
f3bbb9cc 591 case 0x40:
6ee73861
BS
592 /*
593 * No sign of the "power off for reset" or "reset for panel
594 * on" bits, but it's safer to assume we should
595 */
596 bios->fp.power_off_for_reset = true;
597 bios->fp.reset_after_pclk_change = true;
f3bbb9cc 598
6ee73861
BS
599 /*
600 * It's ok lvdsofs is wrong for nv4x edid case; dual_link is
f3bbb9cc 601 * over-written, and if_is_24bit isn't used
6ee73861
BS
602 */
603 bios->fp.dual_link = bios->data[lvdsofs] & 1;
6ee73861
BS
604 bios->fp.if_is_24bit = bios->data[lvdsofs] & 2;
605 bios->fp.strapless_is_24bit = bios->data[bios->fp.lvdsmanufacturerpointer + 4];
606 bios->fp.duallink_transition_clk = ROM16(bios->data[bios->fp.lvdsmanufacturerpointer + 5]) * 10;
607 break;
608 }
609
610 /* set dual_link flag for EDID case */
611 if (pxclk && (chip_version < 0x25 || chip_version > 0x28))
612 bios->fp.dual_link = (pxclk >= bios->fp.duallink_transition_clk);
613
614 *dl = bios->fp.dual_link;
615
616 return 0;
617}
618
721b0821
BS
619/* BIT 'U'/'d' table encoder subtables have hashes matching them to
620 * a particular set of encoders.
621 *
622 * This function returns true if a particular DCB entry matches.
623 */
624bool
cb75d97e 625bios_encoder_match(struct dcb_output *dcb, u32 hash)
6ee73861 626{
721b0821
BS
627 if ((hash & 0x000000f0) != (dcb->location << 4))
628 return false;
629 if ((hash & 0x0000000f) != dcb->type)
630 return false;
631 if (!(hash & (dcb->or << 16)))
632 return false;
633
634 switch (dcb->type) {
cb75d97e
BS
635 case DCB_OUTPUT_TMDS:
636 case DCB_OUTPUT_LVDS:
637 case DCB_OUTPUT_DP:
721b0821
BS
638 if (hash & 0x00c00000) {
639 if (!(hash & (dcb->sorconf.link << 22)))
640 return false;
1eb38100 641 }
721b0821
BS
642 default:
643 return true;
6ee73861 644 }
6ee73861
BS
645}
646
6ee73861 647int
02e4f587 648nouveau_bios_run_display_table(struct drm_device *dev, u16 type, int pclk,
cb75d97e 649 struct dcb_output *dcbent, int crtc)
6ee73861
BS
650{
651 /*
652 * The display script table is located by the BIT 'U' table.
653 *
654 * It contains an array of pointers to various tables describing
655 * a particular output type. The first 32-bits of the output
656 * tables contains similar information to a DCB entry, and is
657 * used to decide whether that particular table is suitable for
658 * the output you want to access.
659 *
660 * The "record header length" field here seems to indicate the
661 * offset of the first configuration entry in the output tables.
662 * This is 10 on most cards I've seen, but 12 has been witnessed
663 * on DP cards, and there's another script pointer within the
664 * header.
665 *
666 * offset + 0 ( 8 bits): version
667 * offset + 1 ( 8 bits): header length
668 * offset + 2 ( 8 bits): record length
669 * offset + 3 ( 8 bits): number of records
670 * offset + 4 ( 8 bits): record header length
671 * offset + 5 (16 bits): pointer to first output script table
672 */
673
674 struct drm_nouveau_private *dev_priv = dev->dev_private;
04a39c57 675 struct nvbios *bios = &dev_priv->vbios;
6ee73861
BS
676 uint8_t *table = &bios->data[bios->display.script_table_ptr];
677 uint8_t *otable = NULL;
678 uint16_t script;
721b0821 679 int i;
6ee73861
BS
680
681 if (!bios->display.script_table_ptr) {
682 NV_ERROR(dev, "No pointer to output script table\n");
683 return 1;
684 }
685
686 /*
687 * Nothing useful has been in any of the pre-2.0 tables I've seen,
688 * so until they are, we really don't need to care.
689 */
690 if (table[0] < 0x20)
691 return 1;
692
693 if (table[0] != 0x20 && table[0] != 0x21) {
694 NV_ERROR(dev, "Output script table version 0x%02x unknown\n",
695 table[0]);
696 return 1;
697 }
698
699 /*
700 * The output script tables describing a particular output type
701 * look as follows:
702 *
703 * offset + 0 (32 bits): output this table matches (hash of DCB)
704 * offset + 4 ( 8 bits): unknown
705 * offset + 5 ( 8 bits): number of configurations
706 * offset + 6 (16 bits): pointer to some script
707 * offset + 8 (16 bits): pointer to some script
708 *
709 * headerlen == 10
710 * offset + 10 : configuration 0
711 *
712 * headerlen == 12
713 * offset + 10 : pointer to some script
714 * offset + 12 : configuration 0
715 *
716 * Each config entry is as follows:
717 *
718 * offset + 0 (16 bits): unknown, assumed to be a match value
719 * offset + 2 (16 bits): pointer to script table (clock set?)
720 * offset + 4 (16 bits): pointer to script table (reset?)
721 *
722 * There doesn't appear to be a count value to say how many
723 * entries exist in each script table, instead, a 0 value in
724 * the first 16-bit word seems to indicate both the end of the
725 * list and the default entry. The second 16-bit word in the
726 * script tables is a pointer to the script to execute.
727 */
728
ef2bb506 729 NV_DEBUG_KMS(dev, "Searching for output entry for %d %d %d\n",
6ee73861 730 dcbent->type, dcbent->location, dcbent->or);
721b0821 731 for (i = 0; i < table[3]; i++) {
f9f9f536 732 otable = ROMPTR(dev, table[table[1] + (i * table[2])]);
721b0821
BS
733 if (otable && bios_encoder_match(dcbent, ROM32(otable[0])))
734 break;
735 }
736
6ee73861 737 if (!otable) {
54bf67de 738 NV_DEBUG_KMS(dev, "failed to match any output table\n");
6ee73861
BS
739 return 1;
740 }
741
02e4f587 742 if (pclk < -2 || pclk > 0) {
6ee73861
BS
743 /* Try to find matching script table entry */
744 for (i = 0; i < otable[5]; i++) {
02e4f587 745 if (ROM16(otable[table[4] + i*6]) == type)
6ee73861
BS
746 break;
747 }
748
749 if (i == otable[5]) {
750 NV_ERROR(dev, "Table 0x%04x not found for %d/%d, "
751 "using first\n",
02e4f587 752 type, dcbent->type, dcbent->or);
6ee73861
BS
753 i = 0;
754 }
755 }
756
02e4f587 757 if (pclk == 0) {
6ee73861
BS
758 script = ROM16(otable[6]);
759 if (!script) {
ef2bb506 760 NV_DEBUG_KMS(dev, "output script 0 not found\n");
6ee73861
BS
761 return 1;
762 }
763
45a68a07 764 NV_DEBUG_KMS(dev, "0x%04X: parsing output script 0\n", script);
02e4f587 765 nouveau_bios_run_init_table(dev, script, dcbent, crtc);
6ee73861 766 } else
02e4f587 767 if (pclk == -1) {
6ee73861
BS
768 script = ROM16(otable[8]);
769 if (!script) {
ef2bb506 770 NV_DEBUG_KMS(dev, "output script 1 not found\n");
6ee73861
BS
771 return 1;
772 }
773
45a68a07 774 NV_DEBUG_KMS(dev, "0x%04X: parsing output script 1\n", script);
02e4f587 775 nouveau_bios_run_init_table(dev, script, dcbent, crtc);
6ee73861 776 } else
02e4f587 777 if (pclk == -2) {
6ee73861
BS
778 if (table[4] >= 12)
779 script = ROM16(otable[10]);
780 else
781 script = 0;
782 if (!script) {
ef2bb506 783 NV_DEBUG_KMS(dev, "output script 2 not found\n");
6ee73861
BS
784 return 1;
785 }
786
45a68a07 787 NV_DEBUG_KMS(dev, "0x%04X: parsing output script 2\n", script);
02e4f587 788 nouveau_bios_run_init_table(dev, script, dcbent, crtc);
6ee73861 789 } else
02e4f587 790 if (pclk > 0) {
6ee73861
BS
791 script = ROM16(otable[table[4] + i*6 + 2]);
792 if (script)
02e4f587 793 script = clkcmptable(bios, script, pclk);
6ee73861 794 if (!script) {
54bf67de 795 NV_DEBUG_KMS(dev, "clock script 0 not found\n");
6ee73861
BS
796 return 1;
797 }
798
45a68a07 799 NV_DEBUG_KMS(dev, "0x%04X: parsing clock script 0\n", script);
02e4f587 800 nouveau_bios_run_init_table(dev, script, dcbent, crtc);
6ee73861 801 } else
02e4f587 802 if (pclk < 0) {
6ee73861
BS
803 script = ROM16(otable[table[4] + i*6 + 4]);
804 if (script)
02e4f587 805 script = clkcmptable(bios, script, -pclk);
6ee73861 806 if (!script) {
ef2bb506 807 NV_DEBUG_KMS(dev, "clock script 1 not found\n");
6ee73861
BS
808 return 1;
809 }
810
45a68a07 811 NV_DEBUG_KMS(dev, "0x%04X: parsing clock script 1\n", script);
02e4f587 812 nouveau_bios_run_init_table(dev, script, dcbent, crtc);
6ee73861
BS
813 }
814
815 return 0;
816}
817
818
cb75d97e 819int run_tmds_table(struct drm_device *dev, struct dcb_output *dcbent, int head, int pxclk)
6ee73861
BS
820{
821 /*
822 * the pxclk parameter is in kHz
823 *
824 * This runs the TMDS regs setting code found on BIT bios cards
825 *
826 * For ffs(or) == 1 use the first table, for ffs(or) == 2 and
827 * ffs(or) == 3, use the second.
828 */
829
830 struct drm_nouveau_private *dev_priv = dev->dev_private;
04a39c57
BS
831 struct nvbios *bios = &dev_priv->vbios;
832 int cv = bios->chip_version;
6ee73861
BS
833 uint16_t clktable = 0, scriptptr;
834 uint32_t sel_clk_binding, sel_clk;
835
836 /* pre-nv17 off-chip tmds uses scripts, post nv17 doesn't */
837 if (cv >= 0x17 && cv != 0x1a && cv != 0x20 &&
838 dcbent->location != DCB_LOC_ON_CHIP)
839 return 0;
840
841 switch (ffs(dcbent->or)) {
842 case 1:
843 clktable = bios->tmds.output0_script_ptr;
844 break;
845 case 2:
846 case 3:
847 clktable = bios->tmds.output1_script_ptr;
848 break;
849 }
850
851 if (!clktable) {
852 NV_ERROR(dev, "Pixel clock comparison table not found\n");
853 return -EINVAL;
854 }
855
856 scriptptr = clkcmptable(bios, clktable, pxclk);
857
858 if (!scriptptr) {
859 NV_ERROR(dev, "TMDS output init script not found\n");
860 return -ENOENT;
861 }
862
863 /* don't let script change pll->head binding */
cb75d97e 864 sel_clk_binding = nv_rd32(dev, NV_PRAMDAC_SEL_CLK) & 0x50000;
6ee73861
BS
865 run_digital_op_script(dev, scriptptr, dcbent, head, pxclk >= 165000);
866 sel_clk = NVReadRAMDAC(dev, 0, NV_PRAMDAC_SEL_CLK) & ~0x50000;
867 NVWriteRAMDAC(dev, 0, NV_PRAMDAC_SEL_CLK, sel_clk | sel_clk_binding);
868
869 return 0;
870}
871
6ee73861
BS
872static void parse_bios_version(struct drm_device *dev, struct nvbios *bios, uint16_t offset)
873{
874 /*
875 * offset + 0 (8 bits): Micro version
876 * offset + 1 (8 bits): Minor version
877 * offset + 2 (8 bits): Chip version
878 * offset + 3 (8 bits): Major version
879 */
880
881 bios->major_version = bios->data[offset + 3];
04a39c57 882 bios->chip_version = bios->data[offset + 2];
6ee73861
BS
883 NV_TRACE(dev, "Bios version %02x.%02x.%02x.%02x\n",
884 bios->data[offset + 3], bios->data[offset + 2],
885 bios->data[offset + 1], bios->data[offset]);
886}
887
888static void parse_script_table_pointers(struct nvbios *bios, uint16_t offset)
889{
890 /*
891 * Parses the init table segment for pointers used in script execution.
892 *
893 * offset + 0 (16 bits): init script tables pointer
894 * offset + 2 (16 bits): macro index table pointer
895 * offset + 4 (16 bits): macro table pointer
896 * offset + 6 (16 bits): condition table pointer
897 * offset + 8 (16 bits): io condition table pointer
898 * offset + 10 (16 bits): io flag condition table pointer
899 * offset + 12 (16 bits): init function table pointer
900 */
901
902 bios->init_script_tbls_ptr = ROM16(bios->data[offset]);
903 bios->macro_index_tbl_ptr = ROM16(bios->data[offset + 2]);
904 bios->macro_tbl_ptr = ROM16(bios->data[offset + 4]);
905 bios->condition_tbl_ptr = ROM16(bios->data[offset + 6]);
906 bios->io_condition_tbl_ptr = ROM16(bios->data[offset + 8]);
907 bios->io_flag_condition_tbl_ptr = ROM16(bios->data[offset + 10]);
908 bios->init_function_tbl_ptr = ROM16(bios->data[offset + 12]);
909}
910
911static int parse_bit_A_tbl_entry(struct drm_device *dev, struct nvbios *bios, struct bit_entry *bitentry)
912{
913 /*
914 * Parses the load detect values for g80 cards.
915 *
916 * offset + 0 (16 bits): loadval table pointer
917 */
918
919 uint16_t load_table_ptr;
920 uint8_t version, headerlen, entrylen, num_entries;
921
922 if (bitentry->length != 3) {
923 NV_ERROR(dev, "Do not understand BIT A table\n");
924 return -EINVAL;
925 }
926
927 load_table_ptr = ROM16(bios->data[bitentry->offset]);
928
929 if (load_table_ptr == 0x0) {
1562ffde 930 NV_DEBUG(dev, "Pointer to BIT loadval table invalid\n");
6ee73861
BS
931 return -EINVAL;
932 }
933
934 version = bios->data[load_table_ptr];
935
936 if (version != 0x10) {
937 NV_ERROR(dev, "BIT loadval table version %d.%d not supported\n",
938 version >> 4, version & 0xF);
939 return -ENOSYS;
940 }
941
942 headerlen = bios->data[load_table_ptr + 1];
943 entrylen = bios->data[load_table_ptr + 2];
944 num_entries = bios->data[load_table_ptr + 3];
945
946 if (headerlen != 4 || entrylen != 4 || num_entries != 2) {
947 NV_ERROR(dev, "Do not understand BIT loadval table\n");
948 return -EINVAL;
949 }
950
951 /* First entry is normal dac, 2nd tv-out perhaps? */
04a39c57 952 bios->dactestval = ROM32(bios->data[load_table_ptr + headerlen]) & 0x3ff;
6ee73861
BS
953
954 return 0;
955}
956
957static int parse_bit_C_tbl_entry(struct drm_device *dev, struct nvbios *bios, struct bit_entry *bitentry)
958{
959 /*
960 * offset + 8 (16 bits): PLL limits table pointer
961 *
962 * There's more in here, but that's unknown.
963 */
964
965 if (bitentry->length < 10) {
966 NV_ERROR(dev, "Do not understand BIT C table\n");
967 return -EINVAL;
968 }
969
970 bios->pll_limit_tbl_ptr = ROM16(bios->data[bitentry->offset + 8]);
971
972 return 0;
973}
974
975static int parse_bit_display_tbl_entry(struct drm_device *dev, struct nvbios *bios, struct bit_entry *bitentry)
976{
977 /*
978 * Parses the flat panel table segment that the bit entry points to.
979 * Starting at bitentry->offset:
980 *
981 * offset + 0 (16 bits): ??? table pointer - seems to have 18 byte
982 * records beginning with a freq.
983 * offset + 2 (16 bits): mode table pointer
984 */
985
986 if (bitentry->length != 4) {
987 NV_ERROR(dev, "Do not understand BIT display table\n");
988 return -EINVAL;
989 }
990
991 bios->fp.fptablepointer = ROM16(bios->data[bitentry->offset + 2]);
992
993 return 0;
994}
995
996static int parse_bit_init_tbl_entry(struct drm_device *dev, struct nvbios *bios, struct bit_entry *bitentry)
997{
998 /*
999 * Parses the init table segment that the bit entry points to.
1000 *
1001 * See parse_script_table_pointers for layout
1002 */
1003
1004 if (bitentry->length < 14) {
1005 NV_ERROR(dev, "Do not understand init table\n");
1006 return -EINVAL;
1007 }
1008
1009 parse_script_table_pointers(bios, bitentry->offset);
1010
1011 if (bitentry->length >= 16)
1012 bios->some_script_ptr = ROM16(bios->data[bitentry->offset + 14]);
1013 if (bitentry->length >= 18)
1014 bios->init96_tbl_ptr = ROM16(bios->data[bitentry->offset + 16]);
1015
1016 return 0;
1017}
1018
1019static int parse_bit_i_tbl_entry(struct drm_device *dev, struct nvbios *bios, struct bit_entry *bitentry)
1020{
1021 /*
1022 * BIT 'i' (info?) table
1023 *
1024 * offset + 0 (32 bits): BIOS version dword (as in B table)
1025 * offset + 5 (8 bits): BIOS feature byte (same as for BMP?)
1026 * offset + 13 (16 bits): pointer to table containing DAC load
1027 * detection comparison values
1028 *
1029 * There's other things in the table, purpose unknown
1030 */
1031
1032 uint16_t daccmpoffset;
1033 uint8_t dacver, dacheaderlen;
1034
1035 if (bitentry->length < 6) {
1036 NV_ERROR(dev, "BIT i table too short for needed information\n");
1037 return -EINVAL;
1038 }
1039
1040 parse_bios_version(dev, bios, bitentry->offset);
1041
1042 /*
1043 * bit 4 seems to indicate a mobile bios (doesn't suffer from BMP's
1044 * Quadro identity crisis), other bits possibly as for BMP feature byte
1045 */
1046 bios->feature_byte = bios->data[bitentry->offset + 5];
1047 bios->is_mobile = bios->feature_byte & FEATURE_MOBILE;
1048
1049 if (bitentry->length < 15) {
1050 NV_WARN(dev, "BIT i table not long enough for DAC load "
1051 "detection comparison table\n");
1052 return -EINVAL;
1053 }
1054
1055 daccmpoffset = ROM16(bios->data[bitentry->offset + 13]);
1056
1057 /* doesn't exist on g80 */
1058 if (!daccmpoffset)
1059 return 0;
1060
1061 /*
1062 * The first value in the table, following the header, is the
1063 * comparison value, the second entry is a comparison value for
1064 * TV load detection.
1065 */
1066
1067 dacver = bios->data[daccmpoffset];
1068 dacheaderlen = bios->data[daccmpoffset + 1];
1069
1070 if (dacver != 0x00 && dacver != 0x10) {
1071 NV_WARN(dev, "DAC load detection comparison table version "
1072 "%d.%d not known\n", dacver >> 4, dacver & 0xf);
1073 return -ENOSYS;
1074 }
1075
04a39c57
BS
1076 bios->dactestval = ROM32(bios->data[daccmpoffset + dacheaderlen]);
1077 bios->tvdactestval = ROM32(bios->data[daccmpoffset + dacheaderlen + 4]);
6ee73861
BS
1078
1079 return 0;
1080}
1081
1082static int parse_bit_lvds_tbl_entry(struct drm_device *dev, struct nvbios *bios, struct bit_entry *bitentry)
1083{
1084 /*
1085 * Parses the LVDS table segment that the bit entry points to.
1086 * Starting at bitentry->offset:
1087 *
1088 * offset + 0 (16 bits): LVDS strap xlate table pointer
1089 */
1090
1091 if (bitentry->length != 2) {
1092 NV_ERROR(dev, "Do not understand BIT LVDS table\n");
1093 return -EINVAL;
1094 }
1095
1096 /*
1097 * No idea if it's still called the LVDS manufacturer table, but
1098 * the concept's close enough.
1099 */
1100 bios->fp.lvdsmanufacturerpointer = ROM16(bios->data[bitentry->offset]);
1101
1102 return 0;
1103}
1104
1105static int
1106parse_bit_M_tbl_entry(struct drm_device *dev, struct nvbios *bios,
1107 struct bit_entry *bitentry)
1108{
1109 /*
1110 * offset + 2 (8 bits): number of options in an
1111 * INIT_RAM_RESTRICT_ZM_REG_GROUP opcode option set
1112 * offset + 3 (16 bits): pointer to strap xlate table for RAM
1113 * restrict option selection
1114 *
1115 * There's a bunch of bits in this table other than the RAM restrict
1116 * stuff that we don't use - their use currently unknown
1117 */
1118
6ee73861
BS
1119 /*
1120 * Older bios versions don't have a sufficiently long table for
1121 * what we want
1122 */
1123 if (bitentry->length < 0x5)
1124 return 0;
1125
4709bff0 1126 if (bitentry->version < 2) {
37383650
MK
1127 bios->ram_restrict_group_count = bios->data[bitentry->offset + 2];
1128 bios->ram_restrict_tbl_ptr = ROM16(bios->data[bitentry->offset + 3]);
6ee73861 1129 } else {
37383650
MK
1130 bios->ram_restrict_group_count = bios->data[bitentry->offset + 0];
1131 bios->ram_restrict_tbl_ptr = ROM16(bios->data[bitentry->offset + 1]);
6ee73861
BS
1132 }
1133
6ee73861
BS
1134 return 0;
1135}
1136
1137static int parse_bit_tmds_tbl_entry(struct drm_device *dev, struct nvbios *bios, struct bit_entry *bitentry)
1138{
1139 /*
1140 * Parses the pointer to the TMDS table
1141 *
1142 * Starting at bitentry->offset:
1143 *
1144 * offset + 0 (16 bits): TMDS table pointer
1145 *
1146 * The TMDS table is typically found just before the DCB table, with a
1147 * characteristic signature of 0x11,0x13 (1.1 being version, 0x13 being
1148 * length?)
1149 *
1150 * At offset +7 is a pointer to a script, which I don't know how to
1151 * run yet.
1152 * At offset +9 is a pointer to another script, likewise
1153 * Offset +11 has a pointer to a table where the first word is a pxclk
1154 * frequency and the second word a pointer to a script, which should be
1155 * run if the comparison pxclk frequency is less than the pxclk desired.
1156 * This repeats for decreasing comparison frequencies
1157 * Offset +13 has a pointer to a similar table
1158 * The selection of table (and possibly +7/+9 script) is dictated by
1159 * "or" from the DCB.
1160 */
1161
1162 uint16_t tmdstableptr, script1, script2;
1163
1164 if (bitentry->length != 2) {
1165 NV_ERROR(dev, "Do not understand BIT TMDS table\n");
1166 return -EINVAL;
1167 }
1168
1169 tmdstableptr = ROM16(bios->data[bitentry->offset]);
98720bf4 1170 if (!tmdstableptr) {
6ee73861
BS
1171 NV_ERROR(dev, "Pointer to TMDS table invalid\n");
1172 return -EINVAL;
1173 }
1174
98720bf4
BS
1175 NV_INFO(dev, "TMDS table version %d.%d\n",
1176 bios->data[tmdstableptr] >> 4, bios->data[tmdstableptr] & 0xf);
1177
6ee73861 1178 /* nv50+ has v2.0, but we don't parse it atm */
98720bf4 1179 if (bios->data[tmdstableptr] != 0x11)
6ee73861 1180 return -ENOSYS;
6ee73861
BS
1181
1182 /*
1183 * These two scripts are odd: they don't seem to get run even when
1184 * they are not stubbed.
1185 */
1186 script1 = ROM16(bios->data[tmdstableptr + 7]);
1187 script2 = ROM16(bios->data[tmdstableptr + 9]);
1188 if (bios->data[script1] != 'q' || bios->data[script2] != 'q')
1189 NV_WARN(dev, "TMDS table script pointers not stubbed\n");
1190
1191 bios->tmds.output0_script_ptr = ROM16(bios->data[tmdstableptr + 11]);
1192 bios->tmds.output1_script_ptr = ROM16(bios->data[tmdstableptr + 13]);
1193
1194 return 0;
1195}
1196
1197static int
1198parse_bit_U_tbl_entry(struct drm_device *dev, struct nvbios *bios,
1199 struct bit_entry *bitentry)
1200{
1201 /*
1202 * Parses the pointer to the G80 output script tables
1203 *
1204 * Starting at bitentry->offset:
1205 *
1206 * offset + 0 (16 bits): output script table pointer
1207 */
1208
1209 uint16_t outputscripttableptr;
1210
1211 if (bitentry->length != 3) {
1212 NV_ERROR(dev, "Do not understand BIT U table\n");
1213 return -EINVAL;
1214 }
1215
1216 outputscripttableptr = ROM16(bios->data[bitentry->offset]);
1217 bios->display.script_table_ptr = outputscripttableptr;
1218 return 0;
1219}
1220
6ee73861
BS
1221struct bit_table {
1222 const char id;
1223 int (* const parse_fn)(struct drm_device *, struct nvbios *, struct bit_entry *);
1224};
1225
1226#define BIT_TABLE(id, funcid) ((struct bit_table){ id, parse_bit_##funcid##_tbl_entry })
1227
4709bff0
BS
1228int
1229bit_table(struct drm_device *dev, u8 id, struct bit_entry *bit)
1230{
1231 struct drm_nouveau_private *dev_priv = dev->dev_private;
1232 struct nvbios *bios = &dev_priv->vbios;
1233 u8 entries, *entry;
1234
b4c26818
BS
1235 if (bios->type != NVBIOS_BIT)
1236 return -ENODEV;
1237
4709bff0
BS
1238 entries = bios->data[bios->offset + 10];
1239 entry = &bios->data[bios->offset + 12];
1240 while (entries--) {
1241 if (entry[0] == id) {
1242 bit->id = entry[0];
1243 bit->version = entry[1];
1244 bit->length = ROM16(entry[2]);
1245 bit->offset = ROM16(entry[4]);
f9f9f536 1246 bit->data = ROMPTR(dev, entry[4]);
4709bff0
BS
1247 return 0;
1248 }
1249
1250 entry += bios->data[bios->offset + 9];
1251 }
1252
1253 return -ENOENT;
1254}
1255
6ee73861
BS
1256static int
1257parse_bit_table(struct nvbios *bios, const uint16_t bitoffset,
1258 struct bit_table *table)
1259{
1260 struct drm_device *dev = bios->dev;
6ee73861
BS
1261 struct bit_entry bitentry;
1262
4709bff0 1263 if (bit_table(dev, table->id, &bitentry) == 0)
6ee73861 1264 return table->parse_fn(dev, bios, &bitentry);
6ee73861
BS
1265
1266 NV_INFO(dev, "BIT table '%c' not found\n", table->id);
1267 return -ENOSYS;
1268}
1269
1270static int
1271parse_bit_structure(struct nvbios *bios, const uint16_t bitoffset)
1272{
1273 int ret;
1274
1275 /*
1276 * The only restriction on parsing order currently is having 'i' first
1277 * for use of bios->*_version or bios->feature_byte while parsing;
1278 * functions shouldn't be actually *doing* anything apart from pulling
1279 * data from the image into the bios struct, thus no interdependencies
1280 */
1281 ret = parse_bit_table(bios, bitoffset, &BIT_TABLE('i', i));
1282 if (ret) /* info? */
1283 return ret;
1284 if (bios->major_version >= 0x60) /* g80+ */
1285 parse_bit_table(bios, bitoffset, &BIT_TABLE('A', A));
1286 ret = parse_bit_table(bios, bitoffset, &BIT_TABLE('C', C));
1287 if (ret)
1288 return ret;
1289 parse_bit_table(bios, bitoffset, &BIT_TABLE('D', display));
1290 ret = parse_bit_table(bios, bitoffset, &BIT_TABLE('I', init));
1291 if (ret)
1292 return ret;
1293 parse_bit_table(bios, bitoffset, &BIT_TABLE('M', M)); /* memory? */
1294 parse_bit_table(bios, bitoffset, &BIT_TABLE('L', lvds));
1295 parse_bit_table(bios, bitoffset, &BIT_TABLE('T', tmds));
1296 parse_bit_table(bios, bitoffset, &BIT_TABLE('U', U));
6ee73861
BS
1297
1298 return 0;
1299}
1300
1301static int parse_bmp_structure(struct drm_device *dev, struct nvbios *bios, unsigned int offset)
1302{
1303 /*
1304 * Parses the BMP structure for useful things, but does not act on them
1305 *
1306 * offset + 5: BMP major version
1307 * offset + 6: BMP minor version
1308 * offset + 9: BMP feature byte
1309 * offset + 10: BCD encoded BIOS version
1310 *
1311 * offset + 18: init script table pointer (for bios versions < 5.10h)
1312 * offset + 20: extra init script table pointer (for bios
1313 * versions < 5.10h)
1314 *
1315 * offset + 24: memory init table pointer (used on early bios versions)
1316 * offset + 26: SDR memory sequencing setup data table
1317 * offset + 28: DDR memory sequencing setup data table
1318 *
1319 * offset + 54: index of I2C CRTC pair to use for CRT output
1320 * offset + 55: index of I2C CRTC pair to use for TV output
1321 * offset + 56: index of I2C CRTC pair to use for flat panel output
1322 * offset + 58: write CRTC index for I2C pair 0
1323 * offset + 59: read CRTC index for I2C pair 0
1324 * offset + 60: write CRTC index for I2C pair 1
1325 * offset + 61: read CRTC index for I2C pair 1
1326 *
1327 * offset + 67: maximum internal PLL frequency (single stage PLL)
1328 * offset + 71: minimum internal PLL frequency (single stage PLL)
1329 *
1330 * offset + 75: script table pointers, as described in
1331 * parse_script_table_pointers
1332 *
1333 * offset + 89: TMDS single link output A table pointer
1334 * offset + 91: TMDS single link output B table pointer
1335 * offset + 95: LVDS single link output A table pointer
1336 * offset + 105: flat panel timings table pointer
1337 * offset + 107: flat panel strapping translation table pointer
1338 * offset + 117: LVDS manufacturer panel config table pointer
1339 * offset + 119: LVDS manufacturer strapping translation table pointer
1340 *
1341 * offset + 142: PLL limits table pointer
1342 *
1343 * offset + 156: minimum pixel clock for LVDS dual link
1344 */
1345
1346 uint8_t *bmp = &bios->data[offset], bmp_version_major, bmp_version_minor;
1347 uint16_t bmplength;
1348 uint16_t legacy_scripts_offset, legacy_i2c_offset;
1349
1350 /* load needed defaults in case we can't parse this info */
04a39c57 1351 bios->digital_min_front_porch = 0x4b;
6ee73861
BS
1352 bios->fmaxvco = 256000;
1353 bios->fminvco = 128000;
1354 bios->fp.duallink_transition_clk = 90000;
1355
1356 bmp_version_major = bmp[5];
1357 bmp_version_minor = bmp[6];
1358
1359 NV_TRACE(dev, "BMP version %d.%d\n",
1360 bmp_version_major, bmp_version_minor);
1361
1362 /*
1363 * Make sure that 0x36 is blank and can't be mistaken for a DCB
1364 * pointer on early versions
1365 */
1366 if (bmp_version_major < 5)
1367 *(uint16_t *)&bios->data[0x36] = 0;
1368
1369 /*
1370 * Seems that the minor version was 1 for all major versions prior
1371 * to 5. Version 6 could theoretically exist, but I suspect BIT
1372 * happened instead.
1373 */
1374 if ((bmp_version_major < 5 && bmp_version_minor != 1) || bmp_version_major > 5) {
1375 NV_ERROR(dev, "You have an unsupported BMP version. "
1376 "Please send in your bios\n");
1377 return -ENOSYS;
1378 }
1379
1380 if (bmp_version_major == 0)
1381 /* nothing that's currently useful in this version */
1382 return 0;
1383 else if (bmp_version_major == 1)
1384 bmplength = 44; /* exact for 1.01 */
1385 else if (bmp_version_major == 2)
1386 bmplength = 48; /* exact for 2.01 */
1387 else if (bmp_version_major == 3)
1388 bmplength = 54;
1389 /* guessed - mem init tables added in this version */
1390 else if (bmp_version_major == 4 || bmp_version_minor < 0x1)
1391 /* don't know if 5.0 exists... */
1392 bmplength = 62;
1393 /* guessed - BMP I2C indices added in version 4*/
1394 else if (bmp_version_minor < 0x6)
1395 bmplength = 67; /* exact for 5.01 */
1396 else if (bmp_version_minor < 0x10)
1397 bmplength = 75; /* exact for 5.06 */
1398 else if (bmp_version_minor == 0x10)
1399 bmplength = 89; /* exact for 5.10h */
1400 else if (bmp_version_minor < 0x14)
1401 bmplength = 118; /* exact for 5.11h */
1402 else if (bmp_version_minor < 0x24)
1403 /*
1404 * Not sure of version where pll limits came in;
1405 * certainly exist by 0x24 though.
1406 */
1407 /* length not exact: this is long enough to get lvds members */
1408 bmplength = 123;
1409 else if (bmp_version_minor < 0x27)
1410 /*
1411 * Length not exact: this is long enough to get pll limit
1412 * member
1413 */
1414 bmplength = 144;
1415 else
1416 /*
1417 * Length not exact: this is long enough to get dual link
1418 * transition clock.
1419 */
1420 bmplength = 158;
1421
1422 /* checksum */
1423 if (nv_cksum(bmp, 8)) {
1424 NV_ERROR(dev, "Bad BMP checksum\n");
1425 return -EINVAL;
1426 }
1427
1428 /*
1429 * Bit 4 seems to indicate either a mobile bios or a quadro card --
1430 * mobile behaviour consistent (nv11+), quadro only seen nv18gl-nv36gl
1431 * (not nv10gl), bit 5 that the flat panel tables are present, and
1432 * bit 6 a tv bios.
1433 */
1434 bios->feature_byte = bmp[9];
1435
1436 parse_bios_version(dev, bios, offset + 10);
1437
1438 if (bmp_version_major < 5 || bmp_version_minor < 0x10)
1439 bios->old_style_init = true;
1440 legacy_scripts_offset = 18;
1441 if (bmp_version_major < 2)
1442 legacy_scripts_offset -= 4;
1443 bios->init_script_tbls_ptr = ROM16(bmp[legacy_scripts_offset]);
1444 bios->extra_init_script_tbl_ptr = ROM16(bmp[legacy_scripts_offset + 2]);
1445
1446 if (bmp_version_major > 2) { /* appears in BMP 3 */
1447 bios->legacy.mem_init_tbl_ptr = ROM16(bmp[24]);
1448 bios->legacy.sdr_seq_tbl_ptr = ROM16(bmp[26]);
1449 bios->legacy.ddr_seq_tbl_ptr = ROM16(bmp[28]);
1450 }
1451
1452 legacy_i2c_offset = 0x48; /* BMP version 2 & 3 */
1453 if (bmplength > 61)
1454 legacy_i2c_offset = offset + 54;
1455 bios->legacy.i2c_indices.crt = bios->data[legacy_i2c_offset];
1456 bios->legacy.i2c_indices.tv = bios->data[legacy_i2c_offset + 1];
1457 bios->legacy.i2c_indices.panel = bios->data[legacy_i2c_offset + 2];
6ee73861
BS
1458
1459 if (bmplength > 74) {
1460 bios->fmaxvco = ROM32(bmp[67]);
1461 bios->fminvco = ROM32(bmp[71]);
1462 }
1463 if (bmplength > 88)
1464 parse_script_table_pointers(bios, offset + 75);
1465 if (bmplength > 94) {
1466 bios->tmds.output0_script_ptr = ROM16(bmp[89]);
1467 bios->tmds.output1_script_ptr = ROM16(bmp[91]);
1468 /*
1469 * Never observed in use with lvds scripts, but is reused for
1470 * 18/24 bit panel interface default for EDID equipped panels
1471 * (if_is_24bit not set directly to avoid any oscillation).
1472 */
1473 bios->legacy.lvds_single_a_script_ptr = ROM16(bmp[95]);
1474 }
1475 if (bmplength > 108) {
1476 bios->fp.fptablepointer = ROM16(bmp[105]);
1477 bios->fp.fpxlatetableptr = ROM16(bmp[107]);
1478 bios->fp.xlatwidth = 1;
1479 }
1480 if (bmplength > 120) {
1481 bios->fp.lvdsmanufacturerpointer = ROM16(bmp[117]);
1482 bios->fp.fpxlatemanufacturertableptr = ROM16(bmp[119]);
1483 }
1484 if (bmplength > 143)
1485 bios->pll_limit_tbl_ptr = ROM16(bmp[142]);
1486
1487 if (bmplength > 157)
1488 bios->fp.duallink_transition_clk = ROM16(bmp[156]) * 10;
1489
1490 return 0;
1491}
1492
1493static uint16_t findstr(uint8_t *data, int n, const uint8_t *str, int len)
1494{
1495 int i, j;
1496
1497 for (i = 0; i <= (n - len); i++) {
1498 for (j = 0; j < len; j++)
1499 if (data[i + j] != str[j])
1500 break;
1501 if (j == len)
1502 return i;
1503 }
1504
1505 return 0;
1506}
1507
6b5a81a2 1508void *
e0996aea 1509olddcb_table(struct drm_device *dev)
6b5a81a2
BS
1510{
1511 struct drm_nouveau_private *dev_priv = dev->dev_private;
1512 u8 *dcb = NULL;
1513
1514 if (dev_priv->card_type > NV_04)
1515 dcb = ROMPTR(dev, dev_priv->vbios.data[0x36]);
1516 if (!dcb) {
1517 NV_WARNONCE(dev, "No DCB data found in VBIOS\n");
1518 return NULL;
1519 }
1520
1521 if (dcb[0] >= 0x41) {
1522 NV_WARNONCE(dev, "DCB version 0x%02x unknown\n", dcb[0]);
1523 return NULL;
1524 } else
1525 if (dcb[0] >= 0x30) {
1526 if (ROM32(dcb[6]) == 0x4edcbdcb)
1527 return dcb;
1528 } else
1529 if (dcb[0] >= 0x20) {
1530 if (ROM32(dcb[4]) == 0x4edcbdcb)
1531 return dcb;
1532 } else
1533 if (dcb[0] >= 0x15) {
1534 if (!memcmp(&dcb[-7], "DEV_REC", 7))
1535 return dcb;
1536 } else {
1537 /*
1538 * v1.4 (some NV15/16, NV11+) seems the same as v1.5, but
1539 * always has the same single (crt) entry, even when tv-out
1540 * present, so the conclusion is this version cannot really
1541 * be used.
1542 *
1543 * v1.2 tables (some NV6/10, and NV15+) normally have the
1544 * same 5 entries, which are not specific to the card and so
1545 * no use.
1546 *
1547 * v1.2 does have an I2C table that read_dcb_i2c_table can
1548 * handle, but cards exist (nv11 in #14821) with a bad i2c
1549 * table pointer, so use the indices parsed in
1550 * parse_bmp_structure.
1551 *
1552 * v1.1 (NV5+, maybe some NV4) is entirely unhelpful
1553 */
1554 NV_WARNONCE(dev, "No useful DCB data in VBIOS\n");
1555 return NULL;
1556 }
1557
1558 NV_WARNONCE(dev, "DCB header validation failed\n");
1559 return NULL;
1560}
1561
b4c26818 1562void *
e0996aea 1563olddcb_outp(struct drm_device *dev, u8 idx)
6b5a81a2 1564{
e0996aea 1565 u8 *dcb = olddcb_table(dev);
6b5a81a2
BS
1566 if (dcb && dcb[0] >= 0x30) {
1567 if (idx < dcb[2])
1568 return dcb + dcb[1] + (idx * dcb[3]);
1569 } else
1570 if (dcb && dcb[0] >= 0x20) {
1571 u8 *i2c = ROMPTR(dev, dcb[2]);
1572 u8 *ent = dcb + 8 + (idx * 8);
1573 if (i2c && ent < i2c)
1574 return ent;
1575 } else
1576 if (dcb && dcb[0] >= 0x15) {
1577 u8 *i2c = ROMPTR(dev, dcb[2]);
1578 u8 *ent = dcb + 4 + (idx * 10);
1579 if (i2c && ent < i2c)
1580 return ent;
1581 }
1582
1583 return NULL;
1584}
1585
1586int
e0996aea 1587olddcb_outp_foreach(struct drm_device *dev, void *data,
6b5a81a2
BS
1588 int (*exec)(struct drm_device *, void *, int idx, u8 *outp))
1589{
1590 int ret, idx = -1;
1591 u8 *outp = NULL;
e0996aea 1592 while ((outp = olddcb_outp(dev, ++idx))) {
6b5a81a2
BS
1593 if (ROM32(outp[0]) == 0x00000000)
1594 break; /* seen on an NV11 with DCB v1.5 */
1595 if (ROM32(outp[0]) == 0xffffffff)
1596 break; /* seen on an NV17 with DCB v2.0 */
1597
cb75d97e 1598 if ((outp[0] & 0x0f) == DCB_OUTPUT_UNUSED)
6b5a81a2 1599 continue;
cb75d97e 1600 if ((outp[0] & 0x0f) == DCB_OUTPUT_EOL)
6b5a81a2
BS
1601 break;
1602
1603 ret = exec(dev, data, idx, outp);
1604 if (ret)
1605 return ret;
1606 }
1607
1608 return 0;
1609}
1610
befb51e9 1611u8 *
cb75d97e 1612olddcb_conntab(struct drm_device *dev)
befb51e9 1613{
e0996aea 1614 u8 *dcb = olddcb_table(dev);
befb51e9
BS
1615 if (dcb && dcb[0] >= 0x30 && dcb[1] >= 0x16) {
1616 u8 *conntab = ROMPTR(dev, dcb[0x14]);
1617 if (conntab && conntab[0] >= 0x30 && conntab[0] <= 0x40)
1618 return conntab;
1619 }
1620 return NULL;
1621}
1622
1623u8 *
cb75d97e 1624olddcb_conn(struct drm_device *dev, u8 idx)
befb51e9 1625{
cb75d97e 1626 u8 *conntab = olddcb_conntab(dev);
befb51e9
BS
1627 if (conntab && idx < conntab[2])
1628 return conntab + conntab[1] + (idx * conntab[3]);
1629 return NULL;
1630}
1631
cb75d97e 1632static struct dcb_output *new_dcb_entry(struct dcb_table *dcb)
6ee73861 1633{
cb75d97e 1634 struct dcb_output *entry = &dcb->entry[dcb->entries];
6ee73861 1635
cb75d97e 1636 memset(entry, 0, sizeof(struct dcb_output));
6ee73861
BS
1637 entry->index = dcb->entries++;
1638
1639 return entry;
1640}
1641
2e5702af
FJ
1642static void fabricate_dcb_output(struct dcb_table *dcb, int type, int i2c,
1643 int heads, int or)
6ee73861 1644{
cb75d97e 1645 struct dcb_output *entry = new_dcb_entry(dcb);
6ee73861 1646
2e5702af 1647 entry->type = type;
6ee73861
BS
1648 entry->i2c_index = i2c;
1649 entry->heads = heads;
cb75d97e 1650 if (type != DCB_OUTPUT_ANALOG)
2e5702af
FJ
1651 entry->location = !DCB_LOC_ON_CHIP; /* ie OFF CHIP */
1652 entry->or = or;
6ee73861
BS
1653}
1654
1655static bool
7f245b20 1656parse_dcb20_entry(struct drm_device *dev, struct dcb_table *dcb,
cb75d97e 1657 uint32_t conn, uint32_t conf, struct dcb_output *entry)
6ee73861
BS
1658{
1659 entry->type = conn & 0xf;
1660 entry->i2c_index = (conn >> 4) & 0xf;
1661 entry->heads = (conn >> 8) & 0xf;
befb51e9 1662 entry->connector = (conn >> 12) & 0xf;
6ee73861
BS
1663 entry->bus = (conn >> 16) & 0xf;
1664 entry->location = (conn >> 20) & 0x3;
1665 entry->or = (conn >> 24) & 0xf;
6ee73861
BS
1666
1667 switch (entry->type) {
cb75d97e 1668 case DCB_OUTPUT_ANALOG:
6ee73861
BS
1669 /*
1670 * Although the rest of a CRT conf dword is usually
1671 * zeros, mac biosen have stuff there so we must mask
1672 */
7f245b20 1673 entry->crtconf.maxfreq = (dcb->version < 0x30) ?
6ee73861
BS
1674 (conf & 0xffff) * 10 :
1675 (conf & 0xff) * 10000;
1676 break;
cb75d97e 1677 case DCB_OUTPUT_LVDS:
6ee73861
BS
1678 {
1679 uint32_t mask;
1680 if (conf & 0x1)
1681 entry->lvdsconf.use_straps_for_mode = true;
7f245b20 1682 if (dcb->version < 0x22) {
6ee73861
BS
1683 mask = ~0xd;
1684 /*
1685 * The laptop in bug 14567 lies and claims to not use
1686 * straps when it does, so assume all DCB 2.0 laptops
1687 * use straps, until a broken EDID using one is produced
1688 */
1689 entry->lvdsconf.use_straps_for_mode = true;
1690 /*
1691 * Both 0x4 and 0x8 show up in v2.0 tables; assume they
1692 * mean the same thing (probably wrong, but might work)
1693 */
1694 if (conf & 0x4 || conf & 0x8)
1695 entry->lvdsconf.use_power_scripts = true;
1696 } else {
a6ed76d7
BS
1697 mask = ~0x7;
1698 if (conf & 0x2)
1699 entry->lvdsconf.use_acpi_for_edid = true;
6ee73861
BS
1700 if (conf & 0x4)
1701 entry->lvdsconf.use_power_scripts = true;
c5875470 1702 entry->lvdsconf.sor.link = (conf & 0x00000030) >> 4;
6ee73861
BS
1703 }
1704 if (conf & mask) {
1705 /*
1706 * Until we even try to use these on G8x, it's
1707 * useless reporting unknown bits. They all are.
1708 */
7f245b20 1709 if (dcb->version >= 0x40)
6ee73861
BS
1710 break;
1711
1712 NV_ERROR(dev, "Unknown LVDS configuration bits, "
1713 "please report\n");
1714 }
1715 break;
1716 }
cb75d97e 1717 case DCB_OUTPUT_TV:
6ee73861 1718 {
7f245b20 1719 if (dcb->version >= 0x30)
6ee73861
BS
1720 entry->tvconf.has_component_output = conf & (0x8 << 4);
1721 else
1722 entry->tvconf.has_component_output = false;
1723
1724 break;
1725 }
cb75d97e 1726 case DCB_OUTPUT_DP:
6ee73861 1727 entry->dpconf.sor.link = (conf & 0x00000030) >> 4;
75a1fccf
BS
1728 switch ((conf & 0x00e00000) >> 21) {
1729 case 0:
1730 entry->dpconf.link_bw = 162000;
1731 break;
1732 default:
1733 entry->dpconf.link_bw = 270000;
1734 break;
1735 }
6ee73861
BS
1736 switch ((conf & 0x0f000000) >> 24) {
1737 case 0xf:
1738 entry->dpconf.link_nr = 4;
1739 break;
1740 case 0x3:
1741 entry->dpconf.link_nr = 2;
1742 break;
1743 default:
1744 entry->dpconf.link_nr = 1;
1745 break;
1746 }
1747 break;
cb75d97e 1748 case DCB_OUTPUT_TMDS:
27d50fcc
FJ
1749 if (dcb->version >= 0x40)
1750 entry->tmdsconf.sor.link = (conf & 0x00000030) >> 4;
4a9f822f
FJ
1751 else if (dcb->version >= 0x30)
1752 entry->tmdsconf.slave_addr = (conf & 0x00000700) >> 8;
27d50fcc
FJ
1753 else if (dcb->version >= 0x22)
1754 entry->tmdsconf.slave_addr = (conf & 0x00000070) >> 4;
4a9f822f 1755
6ee73861 1756 break;
cb75d97e 1757 case DCB_OUTPUT_EOL:
6ee73861 1758 /* weird g80 mobile type that "nv" treats as a terminator */
7f245b20 1759 dcb->entries--;
6ee73861 1760 return false;
e7cc51c5
BS
1761 default:
1762 break;
6ee73861
BS
1763 }
1764
23484874
BS
1765 if (dcb->version < 0x40) {
1766 /* Normal entries consist of a single bit, but dual link has
1767 * the next most significant bit set too
1768 */
1769 entry->duallink_possible =
1770 ((1 << (ffs(entry->or) - 1)) * 3 == entry->or);
1771 } else {
1772 entry->duallink_possible = (entry->sorconf.link == 3);
1773 }
1774
6ee73861
BS
1775 /* unsure what DCB version introduces this, 3.0? */
1776 if (conf & 0x100000)
1777 entry->i2c_upper_default = true;
1778
1779 return true;
1780}
1781
1782static bool
7f245b20 1783parse_dcb15_entry(struct drm_device *dev, struct dcb_table *dcb,
cb75d97e 1784 uint32_t conn, uint32_t conf, struct dcb_output *entry)
6ee73861 1785{
b0d2de86
BS
1786 switch (conn & 0x0000000f) {
1787 case 0:
cb75d97e 1788 entry->type = DCB_OUTPUT_ANALOG;
b0d2de86
BS
1789 break;
1790 case 1:
cb75d97e 1791 entry->type = DCB_OUTPUT_TV;
b0d2de86
BS
1792 break;
1793 case 2:
b0d2de86 1794 case 4:
fba67528 1795 if (conn & 0x10)
cb75d97e 1796 entry->type = DCB_OUTPUT_LVDS;
fba67528 1797 else
cb75d97e 1798 entry->type = DCB_OUTPUT_TMDS;
fba67528
FJ
1799 break;
1800 case 3:
cb75d97e 1801 entry->type = DCB_OUTPUT_LVDS;
b0d2de86
BS
1802 break;
1803 default:
1804 NV_ERROR(dev, "Unknown DCB type %d\n", conn & 0x0000000f);
1805 return false;
6ee73861 1806 }
b0d2de86
BS
1807
1808 entry->i2c_index = (conn & 0x0003c000) >> 14;
1809 entry->heads = ((conn & 0x001c0000) >> 18) + 1;
1810 entry->or = entry->heads; /* same as heads, hopefully safe enough */
1811 entry->location = (conn & 0x01e00000) >> 21;
1812 entry->bus = (conn & 0x0e000000) >> 25;
6ee73861
BS
1813 entry->duallink_possible = false;
1814
1815 switch (entry->type) {
cb75d97e 1816 case DCB_OUTPUT_ANALOG:
6ee73861
BS
1817 entry->crtconf.maxfreq = (conf & 0xffff) * 10;
1818 break;
cb75d97e 1819 case DCB_OUTPUT_TV:
b0d2de86 1820 entry->tvconf.has_component_output = false;
6ee73861 1821 break;
cb75d97e 1822 case DCB_OUTPUT_LVDS:
77b1d5dc 1823 if ((conn & 0x00003f00) >> 8 != 0x10)
b0d2de86
BS
1824 entry->lvdsconf.use_straps_for_mode = true;
1825 entry->lvdsconf.use_power_scripts = true;
1826 break;
1827 default:
6ee73861
BS
1828 break;
1829 }
1830
1831 return true;
1832}
1833
6ee73861 1834static
7f245b20 1835void merge_like_dcb_entries(struct drm_device *dev, struct dcb_table *dcb)
6ee73861
BS
1836{
1837 /*
1838 * DCB v2.0 lists each output combination separately.
1839 * Here we merge compatible entries to have fewer outputs, with
1840 * more options
1841 */
1842
1843 int i, newentries = 0;
1844
1845 for (i = 0; i < dcb->entries; i++) {
cb75d97e 1846 struct dcb_output *ient = &dcb->entry[i];
6ee73861
BS
1847 int j;
1848
1849 for (j = i + 1; j < dcb->entries; j++) {
cb75d97e 1850 struct dcb_output *jent = &dcb->entry[j];
6ee73861
BS
1851
1852 if (jent->type == 100) /* already merged entry */
1853 continue;
1854
1855 /* merge heads field when all other fields the same */
1856 if (jent->i2c_index == ient->i2c_index &&
1857 jent->type == ient->type &&
1858 jent->location == ient->location &&
1859 jent->or == ient->or) {
1860 NV_TRACE(dev, "Merging DCB entries %d and %d\n",
1861 i, j);
1862 ient->heads |= jent->heads;
1863 jent->type = 100; /* dummy value */
1864 }
1865 }
1866 }
1867
1868 /* Compact entries merged into others out of dcb */
1869 for (i = 0; i < dcb->entries; i++) {
1870 if (dcb->entry[i].type == 100)
1871 continue;
1872
1873 if (newentries != i) {
1874 dcb->entry[newentries] = dcb->entry[i];
1875 dcb->entry[newentries].index = newentries;
1876 }
1877 newentries++;
1878 }
1879
1880 dcb->entries = newentries;
1881}
1882
df4cf1b7
BS
1883static bool
1884apply_dcb_encoder_quirks(struct drm_device *dev, int idx, u32 *conn, u32 *conf)
1885{
670820c0
FJ
1886 struct drm_nouveau_private *dev_priv = dev->dev_private;
1887 struct dcb_table *dcb = &dev_priv->vbios.dcb;
1888
df4cf1b7
BS
1889 /* Dell Precision M6300
1890 * DCB entry 2: 02025312 00000010
1891 * DCB entry 3: 02026312 00000020
1892 *
1893 * Identical, except apparently a different connector on a
1894 * different SOR link. Not a clue how we're supposed to know
1895 * which one is in use if it even shares an i2c line...
1896 *
1897 * Ignore the connector on the second SOR link to prevent
1898 * nasty problems until this is sorted (assuming it's not a
1899 * VBIOS bug).
1900 */
acae116c 1901 if (nv_match_device(dev, 0x040d, 0x1028, 0x019b)) {
df4cf1b7
BS
1902 if (*conn == 0x02026312 && *conf == 0x00000020)
1903 return false;
1904 }
1905
670820c0
FJ
1906 /* GeForce3 Ti 200
1907 *
1908 * DCB reports an LVDS output that should be TMDS:
1909 * DCB entry 1: f2005014 ffffffff
1910 */
1911 if (nv_match_device(dev, 0x0201, 0x1462, 0x8851)) {
1912 if (*conn == 0xf2005014 && *conf == 0xffffffff) {
cb75d97e 1913 fabricate_dcb_output(dcb, DCB_OUTPUT_TMDS, 1, 1, 1);
670820c0
FJ
1914 return false;
1915 }
1916 }
1917
c0929b49
BS
1918 /* XFX GT-240X-YA
1919 *
1920 * So many things wrong here, replace the entire encoder table..
1921 */
1922 if (nv_match_device(dev, 0x0ca3, 0x1682, 0x3003)) {
1923 if (idx == 0) {
1924 *conn = 0x02001300; /* VGA, connector 1 */
1925 *conf = 0x00000028;
1926 } else
1927 if (idx == 1) {
1928 *conn = 0x01010312; /* DVI, connector 0 */
1929 *conf = 0x00020030;
1930 } else
1931 if (idx == 2) {
1932 *conn = 0x01010310; /* VGA, connector 0 */
1933 *conf = 0x00000028;
1934 } else
1935 if (idx == 3) {
1936 *conn = 0x02022362; /* HDMI, connector 2 */
1937 *conf = 0x00020010;
1938 } else {
1939 *conn = 0x0000000e; /* EOL */
1940 *conf = 0x00000000;
1941 }
1942 }
1943
e540afc3
BS
1944 /* Some other twisted XFX board (rhbz#694914)
1945 *
1946 * The DVI/VGA encoder combo that's supposed to represent the
1947 * DVI-I connector actually point at two different ones, and
1948 * the HDMI connector ends up paired with the VGA instead.
1949 *
1950 * Connector table is missing anything for VGA at all, pointing it
1951 * an invalid conntab entry 2 so we figure it out ourself.
1952 */
1953 if (nv_match_device(dev, 0x0615, 0x1682, 0x2605)) {
1954 if (idx == 0) {
1955 *conn = 0x02002300; /* VGA, connector 2 */
1956 *conf = 0x00000028;
1957 } else
1958 if (idx == 1) {
1959 *conn = 0x01010312; /* DVI, connector 0 */
1960 *conf = 0x00020030;
1961 } else
1962 if (idx == 2) {
1963 *conn = 0x04020310; /* VGA, connector 0 */
1964 *conf = 0x00000028;
1965 } else
1966 if (idx == 3) {
1967 *conn = 0x02021322; /* HDMI, connector 1 */
1968 *conf = 0x00020010;
1969 } else {
1970 *conn = 0x0000000e; /* EOL */
1971 *conf = 0x00000000;
1972 }
1973 }
1974
16fde6cd
BS
1975 /* fdo#50830: connector indices for VGA and DVI-I are backwards */
1976 if (nv_match_device(dev, 0x0421, 0x3842, 0xc793)) {
1977 if (idx == 0 && *conn == 0x02000300)
1978 *conn = 0x02011300;
1979 else
1980 if (idx == 1 && *conn == 0x04011310)
1981 *conn = 0x04000310;
1982 else
1983 if (idx == 2 && *conn == 0x02011312)
1984 *conn = 0x02000312;
1985 }
1986
df4cf1b7
BS
1987 return true;
1988}
1989
2e5702af
FJ
1990static void
1991fabricate_dcb_encoder_table(struct drm_device *dev, struct nvbios *bios)
1992{
1993 struct dcb_table *dcb = &bios->dcb;
1994 int all_heads = (nv_two_heads(dev) ? 3 : 1);
1995
1996#ifdef __powerpc__
1997 /* Apple iMac G4 NV17 */
1998 if (of_machine_is_compatible("PowerMac4,5")) {
cb75d97e
BS
1999 fabricate_dcb_output(dcb, DCB_OUTPUT_TMDS, 0, all_heads, 1);
2000 fabricate_dcb_output(dcb, DCB_OUTPUT_ANALOG, 1, all_heads, 2);
2e5702af
FJ
2001 return;
2002 }
2003#endif
2004
2005 /* Make up some sane defaults */
cb75d97e 2006 fabricate_dcb_output(dcb, DCB_OUTPUT_ANALOG,
0f8067c7 2007 bios->legacy.i2c_indices.crt, 1, 1);
2e5702af
FJ
2008
2009 if (nv04_tv_identify(dev, bios->legacy.i2c_indices.tv) >= 0)
cb75d97e 2010 fabricate_dcb_output(dcb, DCB_OUTPUT_TV,
0f8067c7 2011 bios->legacy.i2c_indices.tv,
2e5702af
FJ
2012 all_heads, 0);
2013
2014 else if (bios->tmds.output0_script_ptr ||
2015 bios->tmds.output1_script_ptr)
cb75d97e 2016 fabricate_dcb_output(dcb, DCB_OUTPUT_TMDS,
0f8067c7 2017 bios->legacy.i2c_indices.panel,
2e5702af
FJ
2018 all_heads, 1);
2019}
2020
ed42f824 2021static int
6b5a81a2 2022parse_dcb_entry(struct drm_device *dev, void *data, int idx, u8 *outp)
6ee73861 2023{
ed42f824 2024 struct drm_nouveau_private *dev_priv = dev->dev_private;
6b5a81a2
BS
2025 struct dcb_table *dcb = &dev_priv->vbios.dcb;
2026 u32 conf = (dcb->version >= 0x20) ? ROM32(outp[4]) : ROM32(outp[6]);
2027 u32 conn = ROM32(outp[0]);
2028 bool ret;
6ee73861 2029
6b5a81a2 2030 if (apply_dcb_encoder_quirks(dev, idx, &conn, &conf)) {
cb75d97e 2031 struct dcb_output *entry = new_dcb_entry(dcb);
6ee73861 2032
befb51e9 2033 NV_TRACEWARN(dev, "DCB outp %02d: %08x %08x\n", idx, conn, conf);
6ee73861 2034
6b5a81a2
BS
2035 if (dcb->version >= 0x20)
2036 ret = parse_dcb20_entry(dev, dcb, conn, conf, entry);
2037 else
2038 ret = parse_dcb15_entry(dev, dcb, conn, conf, entry);
2039 if (!ret)
2040 return 1; /* stop parsing */
befb51e9
BS
2041
2042 /* Ignore the I2C index for on-chip TV-out, as there
2043 * are cards with bogus values (nv31m in bug 23212),
2044 * and it's otherwise useless.
2045 */
cb75d97e 2046 if (entry->type == DCB_OUTPUT_TV &&
befb51e9
BS
2047 entry->location == DCB_LOC_ON_CHIP)
2048 entry->i2c_index = 0x0f;
6b5a81a2 2049 }
6ee73861 2050
6b5a81a2
BS
2051 return 0;
2052}
6ee73861 2053
befb51e9
BS
2054static void
2055dcb_fake_connectors(struct nvbios *bios)
2056{
2057 struct dcb_table *dcbt = &bios->dcb;
2058 u8 map[16] = { };
2059 int i, idx = 0;
2060
2061 /* heuristic: if we ever get a non-zero connector field, assume
2062 * that all the indices are valid and we don't need fake them.
5206b524
BS
2063 *
2064 * and, as usual, a blacklist of boards with bad bios data..
befb51e9 2065 */
5206b524
BS
2066 if (!nv_match_device(bios->dev, 0x0392, 0x107d, 0x20a2)) {
2067 for (i = 0; i < dcbt->entries; i++) {
2068 if (dcbt->entry[i].connector)
2069 return;
2070 }
befb51e9
BS
2071 }
2072
2073 /* no useful connector info available, we need to make it up
2074 * ourselves. the rule here is: anything on the same i2c bus
2075 * is considered to be on the same connector. any output
2076 * without an associated i2c bus is assigned its own unique
2077 * connector index.
2078 */
2079 for (i = 0; i < dcbt->entries; i++) {
2080 u8 i2c = dcbt->entry[i].i2c_index;
2081 if (i2c == 0x0f) {
2082 dcbt->entry[i].connector = idx++;
2083 } else {
2084 if (!map[i2c])
2085 map[i2c] = ++idx;
2086 dcbt->entry[i].connector = map[i2c] - 1;
2087 }
2088 }
2089
2090 /* if we created more than one connector, destroy the connector
2091 * table - just in case it has random, rather than stub, entries.
2092 */
2093 if (i > 1) {
cb75d97e 2094 u8 *conntab = olddcb_conntab(bios->dev);
befb51e9
BS
2095 if (conntab)
2096 conntab[0] = 0x00;
2097 }
2098}
2099
6b5a81a2
BS
2100static int
2101parse_dcb_table(struct drm_device *dev, struct nvbios *bios)
2102{
2103 struct dcb_table *dcb = &bios->dcb;
befb51e9
BS
2104 u8 *dcbt, *conn;
2105 int idx;
6b5a81a2 2106
e0996aea 2107 dcbt = olddcb_table(dev);
6b5a81a2
BS
2108 if (!dcbt) {
2109 /* handle pre-DCB boards */
2110 if (bios->type == NVBIOS_BMP) {
2111 fabricate_dcb_encoder_table(dev, bios);
2112 return 0;
6ee73861
BS
2113 }
2114
6b5a81a2
BS
2115 return -EINVAL;
2116 }
6ee73861 2117
6b5a81a2 2118 NV_TRACE(dev, "DCB version %d.%d\n", dcbt[0] >> 4, dcbt[0] & 0xf);
6ee73861 2119
6b5a81a2 2120 dcb->version = dcbt[0];
e0996aea 2121 olddcb_outp_foreach(dev, NULL, parse_dcb_entry);
6ee73861
BS
2122
2123 /*
2124 * apart for v2.1+ not being known for requiring merging, this
2125 * guarantees dcbent->index is the index of the entry in the rom image
2126 */
7f245b20 2127 if (dcb->version < 0x21)
6ee73861
BS
2128 merge_like_dcb_entries(dev, dcb);
2129
54abb5dd
BS
2130 if (!dcb->entries)
2131 return -ENXIO;
2132
befb51e9
BS
2133 /* dump connector table entries to log, if any exist */
2134 idx = -1;
cb75d97e 2135 while ((conn = olddcb_conn(dev, ++idx))) {
befb51e9
BS
2136 if (conn[0] != 0xff) {
2137 NV_TRACE(dev, "DCB conn %02d: ", idx);
cb75d97e 2138 if (olddcb_conntab(dev)[3] < 4)
befb51e9
BS
2139 printk("%04x\n", ROM16(conn[0]));
2140 else
2141 printk("%08x\n", ROM32(conn[0]));
6ee73861 2142 }
6ee73861 2143 }
befb51e9 2144 dcb_fake_connectors(bios);
befb51e9 2145 return 0;
6ee73861
BS
2146}
2147
6ee73861
BS
2148static int load_nv17_hwsq_ucode_entry(struct drm_device *dev, struct nvbios *bios, uint16_t hwsq_offset, int entry)
2149{
2150 /*
2151 * The header following the "HWSQ" signature has the number of entries,
2152 * and the entry size
2153 *
2154 * An entry consists of a dword to write to the sequencer control reg
2155 * (0x00001304), followed by the ucode bytes, written sequentially,
2156 * starting at reg 0x00001400
2157 */
2158
2159 uint8_t bytes_to_write;
2160 uint16_t hwsq_entry_offset;
2161 int i;
2162
2163 if (bios->data[hwsq_offset] <= entry) {
2164 NV_ERROR(dev, "Too few entries in HW sequencer table for "
2165 "requested entry\n");
2166 return -ENOENT;
2167 }
2168
2169 bytes_to_write = bios->data[hwsq_offset + 1];
2170
2171 if (bytes_to_write != 36) {
2172 NV_ERROR(dev, "Unknown HW sequencer entry size\n");
2173 return -EINVAL;
2174 }
2175
2176 NV_TRACE(dev, "Loading NV17 power sequencing microcode\n");
2177
2178 hwsq_entry_offset = hwsq_offset + 2 + entry * bytes_to_write;
2179
2180 /* set sequencer control */
cb75d97e 2181 nv_wr32(dev, 0x00001304, ROM32(bios->data[hwsq_entry_offset]));
6ee73861
BS
2182 bytes_to_write -= 4;
2183
2184 /* write ucode */
2185 for (i = 0; i < bytes_to_write; i += 4)
cb75d97e 2186 nv_wr32(dev, 0x00001400 + i, ROM32(bios->data[hwsq_entry_offset + i + 4]));
6ee73861
BS
2187
2188 /* twiddle NV_PBUS_DEBUG_4 */
cb75d97e 2189 nv_wr32(dev, NV_PBUS_DEBUG_4, nv_rd32(dev, NV_PBUS_DEBUG_4) | 0x18);
6ee73861
BS
2190
2191 return 0;
2192}
2193
2194static int load_nv17_hw_sequencer_ucode(struct drm_device *dev,
2195 struct nvbios *bios)
2196{
2197 /*
2198 * BMP based cards, from NV17, need a microcode loading to correctly
2199 * control the GPIO etc for LVDS panels
2200 *
2201 * BIT based cards seem to do this directly in the init scripts
2202 *
2203 * The microcode entries are found by the "HWSQ" signature.
2204 */
2205
2206 const uint8_t hwsq_signature[] = { 'H', 'W', 'S', 'Q' };
2207 const int sz = sizeof(hwsq_signature);
2208 int hwsq_offset;
2209
2210 hwsq_offset = findstr(bios->data, bios->length, hwsq_signature, sz);
2211 if (!hwsq_offset)
2212 return 0;
2213
2214 /* always use entry 0? */
2215 return load_nv17_hwsq_ucode_entry(dev, bios, hwsq_offset + sz, 0);
2216}
2217
2218uint8_t *nouveau_bios_embedded_edid(struct drm_device *dev)
2219{
2220 struct drm_nouveau_private *dev_priv = dev->dev_private;
04a39c57 2221 struct nvbios *bios = &dev_priv->vbios;
6ee73861
BS
2222 const uint8_t edid_sig[] = {
2223 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00 };
2224 uint16_t offset = 0;
2225 uint16_t newoffset;
2226 int searchlen = NV_PROM_SIZE;
2227
2228 if (bios->fp.edid)
2229 return bios->fp.edid;
2230
2231 while (searchlen) {
2232 newoffset = findstr(&bios->data[offset], searchlen,
2233 edid_sig, 8);
2234 if (!newoffset)
2235 return NULL;
2236 offset += newoffset;
2237 if (!nv_cksum(&bios->data[offset], EDID1_LEN))
2238 break;
2239
2240 searchlen -= offset;
2241 offset++;
2242 }
2243
2244 NV_TRACE(dev, "Found EDID in BIOS\n");
2245
2246 return bios->fp.edid = &bios->data[offset];
2247}
2248
6ee73861
BS
2249static bool NVInitVBIOS(struct drm_device *dev)
2250{
2251 struct drm_nouveau_private *dev_priv = dev->dev_private;
04a39c57 2252 struct nvbios *bios = &dev_priv->vbios;
6ee73861
BS
2253
2254 memset(bios, 0, sizeof(struct nvbios));
c7ca4d1b 2255 spin_lock_init(&bios->lock);
6ee73861
BS
2256 bios->dev = dev;
2257
cd42439d 2258 return _nv_bios(dev, &bios->data, &bios->length);
6ee73861
BS
2259}
2260
2261static int nouveau_parse_vbios_struct(struct drm_device *dev)
2262{
2263 struct drm_nouveau_private *dev_priv = dev->dev_private;
04a39c57 2264 struct nvbios *bios = &dev_priv->vbios;
6ee73861
BS
2265 const uint8_t bit_signature[] = { 0xff, 0xb8, 'B', 'I', 'T' };
2266 const uint8_t bmp_signature[] = { 0xff, 0x7f, 'N', 'V', 0x0 };
2267 int offset;
2268
2269 offset = findstr(bios->data, bios->length,
2270 bit_signature, sizeof(bit_signature));
2271 if (offset) {
2272 NV_TRACE(dev, "BIT BIOS found\n");
4709bff0
BS
2273 bios->type = NVBIOS_BIT;
2274 bios->offset = offset;
6ee73861
BS
2275 return parse_bit_structure(bios, offset + 6);
2276 }
2277
2278 offset = findstr(bios->data, bios->length,
2279 bmp_signature, sizeof(bmp_signature));
2280 if (offset) {
2281 NV_TRACE(dev, "BMP BIOS found\n");
4709bff0
BS
2282 bios->type = NVBIOS_BMP;
2283 bios->offset = offset;
6ee73861
BS
2284 return parse_bmp_structure(dev, bios, offset);
2285 }
2286
2287 NV_ERROR(dev, "No known BIOS signature found\n");
2288 return -ENODEV;
2289}
2290
2291int
2292nouveau_run_vbios_init(struct drm_device *dev)
2293{
2294 struct drm_nouveau_private *dev_priv = dev->dev_private;
04a39c57 2295 struct nvbios *bios = &dev_priv->vbios;
6ee73861
BS
2296 int i, ret = 0;
2297
946fd35f
FJ
2298 /* Reset the BIOS head to 0. */
2299 bios->state.crtchead = 0;
6ee73861
BS
2300
2301 if (bios->major_version < 5) /* BMP only */
2302 load_nv17_hw_sequencer_ucode(dev, bios);
2303
2304 if (bios->execute) {
2305 bios->fp.last_script_invoc = 0;
2306 bios->fp.lvds_init_run = false;
2307 }
2308
6ee73861 2309 if (dev_priv->card_type >= NV_50) {
cb75d97e 2310 for (i = 0; bios->execute && i < bios->dcb.entries; i++) {
02e4f587
BS
2311 nouveau_bios_run_display_table(dev, 0, 0,
2312 &bios->dcb.entry[i], -1);
6ee73861
BS
2313 }
2314 }
2315
6ee73861
BS
2316 return ret;
2317}
2318
d13102c6
BS
2319static bool
2320nouveau_bios_posted(struct drm_device *dev)
2321{
2322 struct drm_nouveau_private *dev_priv = dev->dev_private;
d13102c6
BS
2323 unsigned htotal;
2324
c1b60ece 2325 if (dev_priv->card_type >= NV_50) {
d13102c6
BS
2326 if (NVReadVgaCrtc(dev, 0, 0x00) == 0 &&
2327 NVReadVgaCrtc(dev, 0, 0x1a) == 0)
2328 return false;
2329 return true;
2330 }
2331
d13102c6
BS
2332 htotal = NVReadVgaCrtc(dev, 0, 0x06);
2333 htotal |= (NVReadVgaCrtc(dev, 0, 0x07) & 0x01) << 8;
2334 htotal |= (NVReadVgaCrtc(dev, 0, 0x07) & 0x20) << 4;
2335 htotal |= (NVReadVgaCrtc(dev, 0, 0x25) & 0x01) << 10;
2336 htotal |= (NVReadVgaCrtc(dev, 0, 0x41) & 0x01) << 11;
03cd06ca 2337
d13102c6
BS
2338 return (htotal != 0);
2339}
2340
6ee73861
BS
2341int
2342nouveau_bios_init(struct drm_device *dev)
2343{
2344 struct drm_nouveau_private *dev_priv = dev->dev_private;
04a39c57 2345 struct nvbios *bios = &dev_priv->vbios;
6ee73861
BS
2346 int ret;
2347
6ee73861
BS
2348 if (!NVInitVBIOS(dev))
2349 return -ENODEV;
2350
2351 ret = nouveau_parse_vbios_struct(dev);
2352 if (ret)
2353 return ret;
2354
2e5702af 2355 ret = parse_dcb_table(dev, bios);
6ee73861
BS
2356 if (ret)
2357 return ret;
2358
6ee73861
BS
2359 if (!bios->major_version) /* we don't run version 0 bios */
2360 return 0;
2361
6ee73861
BS
2362 /* init script execution disabled */
2363 bios->execute = false;
2364
2365 /* ... unless card isn't POSTed already */
d13102c6 2366 if (!nouveau_bios_posted(dev)) {
67eda20e
FJ
2367 NV_INFO(dev, "Adaptor not initialised, "
2368 "running VBIOS init tables.\n");
6ee73861
BS
2369 bios->execute = true;
2370 }
0cba1b76
MK
2371 if (nouveau_force_post)
2372 bios->execute = true;
6ee73861 2373
6ee73861 2374 ret = nouveau_run_vbios_init(dev);
04a39c57 2375 if (ret)
6ee73861 2376 return ret;
6ee73861
BS
2377
2378 /* feature_byte on BMP is poor, but init always sets CR4B */
6ee73861
BS
2379 if (bios->major_version < 5)
2380 bios->is_mobile = NVReadVgaCrtc(dev, 0, NV_CIO_CRE_4B) & 0x40;
2381
2382 /* all BIT systems need p_f_m_t for digital_min_front_porch */
2383 if (bios->is_mobile || bios->major_version >= 5)
2384 ret = parse_fp_mode_table(dev, bios);
6ee73861
BS
2385
2386 /* allow subsequent scripts to execute */
2387 bios->execute = true;
2388
2389 return 0;
2390}
2391
2392void
2393nouveau_bios_takedown(struct drm_device *dev)
2394{
6ee73861 2395}