blob: 865eddfa30a73faee6f985b0d7176ceefa7e48f9 [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
Ben Skeggs77145f12012-07-31 16:16:21 +100025#include <subdev/bios.h>
26
David Howells760285e2012-10-02 18:01:07 +010027#include <drm/drmP.h>
Linus Torvalds612a9aa2012-10-03 23:29:23 -070028
Ben Skeggs77145f12012-07-31 16:16:21 +100029#include "nouveau_drm.h"
30#include "nouveau_reg.h"
Ben Skeggs6ee73862009-12-11 19:24:15 +100031#include "nouveau_hw.h"
Ben Skeggs25908b72010-04-20 02:28:37 +100032#include "nouveau_encoder.h"
Ben Skeggs6ee73862009-12-11 19:24:15 +100033
Francisco Jerez67eda202010-07-13 15:59:50 +020034#include <linux/io-mapping.h>
Ben Skeggs78339fb2012-03-30 13:14:45 +100035#include <linux/firmware.h>
Francisco Jerez67eda202010-07-13 15:59:50 +020036
Ben Skeggs6ee73862009-12-11 19:24:15 +100037/* these defines are made up */
38#define NV_CIO_CRE_44_HEADA 0x0
39#define NV_CIO_CRE_44_HEADB 0x3
40#define FEATURE_MOBILE 0x10 /* also FEATURE_QUADRO for BMP */
Ben Skeggs6ee73862009-12-11 19:24:15 +100041
42#define EDID1_LEN 128
43
44#define BIOSLOG(sip, fmt, arg...) NV_DEBUG(sip->dev, fmt, ##arg)
45#define LOG_OLD_VALUE(x)
46
Ben Skeggs6ee73862009-12-11 19:24:15 +100047struct init_exec {
48 bool execute;
49 bool repeat;
50};
51
52static bool nv_cksum(const uint8_t *data, unsigned int length)
53{
54 /*
55 * There's a few checksums in the BIOS, so here's a generic checking
56 * function.
57 */
58 int i;
59 uint8_t sum = 0;
60
61 for (i = 0; i < length; i++)
62 sum += data[i];
63
64 if (sum)
65 return true;
66
67 return false;
68}
69
Ben Skeggs6ee73862009-12-11 19:24:15 +100070static uint16_t clkcmptable(struct nvbios *bios, uint16_t clktable, int pxclk)
71{
72 int compare_record_len, i = 0;
73 uint16_t compareclk, scriptptr = 0;
74
75 if (bios->major_version < 5) /* pre BIT */
76 compare_record_len = 3;
77 else
78 compare_record_len = 4;
79
80 do {
81 compareclk = ROM16(bios->data[clktable + compare_record_len * i]);
82 if (pxclk >= compareclk * 10) {
83 if (bios->major_version < 5) {
84 uint8_t tmdssub = bios->data[clktable + 2 + compare_record_len * i];
85 scriptptr = ROM16(bios->data[bios->init_script_tbls_ptr + tmdssub * 2]);
86 } else
87 scriptptr = ROM16(bios->data[clktable + 2 + compare_record_len * i]);
88 break;
89 }
90 i++;
91 } while (compareclk);
92
93 return scriptptr;
94}
95
96static void
97run_digital_op_script(struct drm_device *dev, uint16_t scriptptr,
Ben Skeggscb75d972012-07-11 10:44:20 +100098 struct dcb_output *dcbent, int head, bool dl)
Ben Skeggs6ee73862009-12-11 19:24:15 +100099{
Ben Skeggs77145f12012-07-31 16:16:21 +1000100 struct nouveau_drm *drm = nouveau_drm(dev);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000101
Ben Skeggs77145f12012-07-31 16:16:21 +1000102 NV_INFO(drm, "0x%04X: Parsing digital output script table\n",
Ben Skeggs6ee73862009-12-11 19:24:15 +1000103 scriptptr);
Ben Skeggscb75d972012-07-11 10:44:20 +1000104 NVWriteVgaCrtc(dev, 0, NV_CIO_CRE_44, head ? NV_CIO_CRE_44_HEADB :
105 NV_CIO_CRE_44_HEADA);
106 nouveau_bios_run_init_table(dev, scriptptr, dcbent, head);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000107
108 nv04_dfp_bind_head(dev, dcbent, head, dl);
109}
110
Ben Skeggscb75d972012-07-11 10:44:20 +1000111static int call_lvds_manufacturer_script(struct drm_device *dev, struct dcb_output *dcbent, int head, enum LVDS_script script)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000112{
Ben Skeggs77145f12012-07-31 16:16:21 +1000113 struct nouveau_drm *drm = nouveau_drm(dev);
114 struct nvbios *bios = &drm->vbios;
Ben Skeggscb75d972012-07-11 10:44:20 +1000115 uint8_t sub = bios->data[bios->fp.xlated_entry + script] + (bios->fp.link_c_increment && dcbent->or & DCB_OUTPUT_C ? 1 : 0);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000116 uint16_t scriptofs = ROM16(bios->data[bios->init_script_tbls_ptr + sub * 2]);
117
118 if (!bios->fp.xlated_entry || !sub || !scriptofs)
119 return -EINVAL;
120
121 run_digital_op_script(dev, scriptofs, dcbent, head, bios->fp.dual_link);
122
123 if (script == LVDS_PANEL_OFF) {
124 /* off-on delay in ms */
Ben Skeggsc7ca4d12011-02-03 20:10:49 +1000125 mdelay(ROM16(bios->data[bios->fp.xlated_entry + 7]));
Ben Skeggs6ee73862009-12-11 19:24:15 +1000126 }
127#ifdef __powerpc__
128 /* Powerbook specific quirks */
Francisco Jerezd31e0782010-08-20 14:19:45 +0200129 if (script == LVDS_RESET &&
130 (dev->pci_device == 0x0179 || dev->pci_device == 0x0189 ||
131 dev->pci_device == 0x0329))
132 nv_write_tmds(dev, dcbent->or, 0, 0x02, 0x72);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000133#endif
134
135 return 0;
136}
137
Ben Skeggscb75d972012-07-11 10:44:20 +1000138static int run_lvds_table(struct drm_device *dev, struct dcb_output *dcbent, int head, enum LVDS_script script, int pxclk)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000139{
140 /*
141 * The BIT LVDS table's header has the information to setup the
142 * necessary registers. Following the standard 4 byte header are:
143 * A bitmask byte and a dual-link transition pxclk value for use in
144 * selecting the init script when not using straps; 4 script pointers
145 * for panel power, selected by output and on/off; and 8 table pointers
146 * for panel init, the needed one determined by output, and bits in the
147 * conf byte. These tables are similar to the TMDS tables, consisting
148 * of a list of pxclks and script pointers.
149 */
Ben Skeggs77145f12012-07-31 16:16:21 +1000150 struct nouveau_drm *drm = nouveau_drm(dev);
151 struct nvbios *bios = &drm->vbios;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000152 unsigned int outputset = (dcbent->or == 4) ? 1 : 0;
153 uint16_t scriptptr = 0, clktable;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000154
155 /*
156 * For now we assume version 3.0 table - g80 support will need some
157 * changes
158 */
159
160 switch (script) {
161 case LVDS_INIT:
162 return -ENOSYS;
163 case LVDS_BACKLIGHT_ON:
164 case LVDS_PANEL_ON:
165 scriptptr = ROM16(bios->data[bios->fp.lvdsmanufacturerpointer + 7 + outputset * 2]);
166 break;
167 case LVDS_BACKLIGHT_OFF:
168 case LVDS_PANEL_OFF:
169 scriptptr = ROM16(bios->data[bios->fp.lvdsmanufacturerpointer + 11 + outputset * 2]);
170 break;
171 case LVDS_RESET:
Ben Skeggsf3bbb9c2010-03-18 12:05:43 +1000172 clktable = bios->fp.lvdsmanufacturerpointer + 15;
173 if (dcbent->or == 4)
174 clktable += 8;
175
Ben Skeggs6ee73862009-12-11 19:24:15 +1000176 if (dcbent->lvdsconf.use_straps_for_mode) {
177 if (bios->fp.dual_link)
Ben Skeggsf3bbb9c2010-03-18 12:05:43 +1000178 clktable += 4;
179 if (bios->fp.if_is_24bit)
180 clktable += 2;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000181 } else {
182 /* using EDID */
Ben Skeggsf3bbb9c2010-03-18 12:05:43 +1000183 int cmpval_24bit = (dcbent->or == 4) ? 4 : 1;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000184
185 if (bios->fp.dual_link) {
Ben Skeggsf3bbb9c2010-03-18 12:05:43 +1000186 clktable += 4;
187 cmpval_24bit <<= 1;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000188 }
Ben Skeggsf3bbb9c2010-03-18 12:05:43 +1000189
190 if (bios->fp.strapless_is_24bit & cmpval_24bit)
191 clktable += 2;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000192 }
193
Ben Skeggsf3bbb9c2010-03-18 12:05:43 +1000194 clktable = ROM16(bios->data[clktable]);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000195 if (!clktable) {
Ben Skeggs77145f12012-07-31 16:16:21 +1000196 NV_ERROR(drm, "Pixel clock comparison table not found\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +1000197 return -ENOENT;
198 }
199 scriptptr = clkcmptable(bios, clktable, pxclk);
200 }
201
202 if (!scriptptr) {
Ben Skeggs77145f12012-07-31 16:16:21 +1000203 NV_ERROR(drm, "LVDS output init script not found\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +1000204 return -ENOENT;
205 }
206 run_digital_op_script(dev, scriptptr, dcbent, head, bios->fp.dual_link);
207
208 return 0;
209}
210
Ben Skeggscb75d972012-07-11 10:44:20 +1000211int call_lvds_script(struct drm_device *dev, struct dcb_output *dcbent, int head, enum LVDS_script script, int pxclk)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000212{
213 /*
214 * LVDS operations are multiplexed in an effort to present a single API
215 * which works with two vastly differing underlying structures.
216 * This acts as the demux
217 */
218
Ben Skeggs77145f12012-07-31 16:16:21 +1000219 struct nouveau_drm *drm = nouveau_drm(dev);
220 struct nouveau_device *device = nv_device(drm->device);
221 struct nvbios *bios = &drm->vbios;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000222 uint8_t lvds_ver = bios->data[bios->fp.lvdsmanufacturerpointer];
223 uint32_t sel_clk_binding, sel_clk;
224 int ret;
225
226 if (bios->fp.last_script_invoc == (script << 1 | head) || !lvds_ver ||
227 (lvds_ver >= 0x30 && script == LVDS_INIT))
228 return 0;
229
230 if (!bios->fp.lvds_init_run) {
231 bios->fp.lvds_init_run = true;
232 call_lvds_script(dev, dcbent, head, LVDS_INIT, pxclk);
233 }
234
235 if (script == LVDS_PANEL_ON && bios->fp.reset_after_pclk_change)
236 call_lvds_script(dev, dcbent, head, LVDS_RESET, pxclk);
237 if (script == LVDS_RESET && bios->fp.power_off_for_reset)
238 call_lvds_script(dev, dcbent, head, LVDS_PANEL_OFF, pxclk);
239
Ben Skeggs77145f12012-07-31 16:16:21 +1000240 NV_INFO(drm, "Calling LVDS script %d:\n", script);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000241
242 /* don't let script change pll->head binding */
Ben Skeggs77145f12012-07-31 16:16:21 +1000243 sel_clk_binding = nv_rd32(device, NV_PRAMDAC_SEL_CLK) & 0x50000;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000244
245 if (lvds_ver < 0x30)
246 ret = call_lvds_manufacturer_script(dev, dcbent, head, script);
247 else
248 ret = run_lvds_table(dev, dcbent, head, script, pxclk);
249
250 bios->fp.last_script_invoc = (script << 1 | head);
251
252 sel_clk = NVReadRAMDAC(dev, 0, NV_PRAMDAC_SEL_CLK) & ~0x50000;
253 NVWriteRAMDAC(dev, 0, NV_PRAMDAC_SEL_CLK, sel_clk | sel_clk_binding);
254 /* some scripts set a value in NV_PBUS_POWERCTRL_2 and break video overlay */
Ben Skeggs77145f12012-07-31 16:16:21 +1000255 nv_wr32(device, NV_PBUS_POWERCTRL_2, 0);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000256
257 return ret;
258}
259
260struct lvdstableheader {
261 uint8_t lvds_ver, headerlen, recordlen;
262};
263
264static int parse_lvds_manufacturer_table_header(struct drm_device *dev, struct nvbios *bios, struct lvdstableheader *lth)
265{
266 /*
267 * BMP version (0xa) LVDS table has a simple header of version and
268 * record length. The BIT LVDS table has the typical BIT table header:
269 * version byte, header length byte, record length byte, and a byte for
270 * the maximum number of records that can be held in the table.
271 */
272
Ben Skeggs77145f12012-07-31 16:16:21 +1000273 struct nouveau_drm *drm = nouveau_drm(dev);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000274 uint8_t lvds_ver, headerlen, recordlen;
275
276 memset(lth, 0, sizeof(struct lvdstableheader));
277
278 if (bios->fp.lvdsmanufacturerpointer == 0x0) {
Ben Skeggs77145f12012-07-31 16:16:21 +1000279 NV_ERROR(drm, "Pointer to LVDS manufacturer table invalid\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +1000280 return -EINVAL;
281 }
282
283 lvds_ver = bios->data[bios->fp.lvdsmanufacturerpointer];
284
285 switch (lvds_ver) {
286 case 0x0a: /* pre NV40 */
287 headerlen = 2;
288 recordlen = bios->data[bios->fp.lvdsmanufacturerpointer + 1];
289 break;
290 case 0x30: /* NV4x */
291 headerlen = bios->data[bios->fp.lvdsmanufacturerpointer + 1];
292 if (headerlen < 0x1f) {
Ben Skeggs77145f12012-07-31 16:16:21 +1000293 NV_ERROR(drm, "LVDS table header not understood\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +1000294 return -EINVAL;
295 }
296 recordlen = bios->data[bios->fp.lvdsmanufacturerpointer + 2];
297 break;
298 case 0x40: /* G80/G90 */
299 headerlen = bios->data[bios->fp.lvdsmanufacturerpointer + 1];
300 if (headerlen < 0x7) {
Ben Skeggs77145f12012-07-31 16:16:21 +1000301 NV_ERROR(drm, "LVDS table header not understood\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +1000302 return -EINVAL;
303 }
304 recordlen = bios->data[bios->fp.lvdsmanufacturerpointer + 2];
305 break;
306 default:
Ben Skeggs77145f12012-07-31 16:16:21 +1000307 NV_ERROR(drm,
Ben Skeggs6ee73862009-12-11 19:24:15 +1000308 "LVDS table revision %d.%d not currently supported\n",
309 lvds_ver >> 4, lvds_ver & 0xf);
310 return -ENOSYS;
311 }
312
313 lth->lvds_ver = lvds_ver;
314 lth->headerlen = headerlen;
315 lth->recordlen = recordlen;
316
317 return 0;
318}
319
320static int
321get_fp_strap(struct drm_device *dev, struct nvbios *bios)
322{
Ben Skeggs77145f12012-07-31 16:16:21 +1000323 struct nouveau_device *device = nouveau_dev(dev);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000324
325 /*
326 * The fp strap is normally dictated by the "User Strap" in
327 * PEXTDEV_BOOT_0[20:16], but on BMP cards when bit 2 of the
328 * Internal_Flags struct at 0x48 is set, the user strap gets overriden
329 * by the PCI subsystem ID during POST, but not before the previous user
330 * strap has been committed to CR58 for CR57=0xf on head A, which may be
331 * read and used instead
332 */
333
334 if (bios->major_version < 5 && bios->data[0x48] & 0x4)
335 return NVReadVgaCrtc5758(dev, 0, 0xf) & 0xf;
336
Ben Skeggs77145f12012-07-31 16:16:21 +1000337 if (device->card_type >= NV_50)
338 return (nv_rd32(device, NV_PEXTDEV_BOOT_0) >> 24) & 0xf;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000339 else
Ben Skeggs77145f12012-07-31 16:16:21 +1000340 return (nv_rd32(device, NV_PEXTDEV_BOOT_0) >> 16) & 0xf;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000341}
342
343static int parse_fp_mode_table(struct drm_device *dev, struct nvbios *bios)
344{
Ben Skeggs77145f12012-07-31 16:16:21 +1000345 struct nouveau_drm *drm = nouveau_drm(dev);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000346 uint8_t *fptable;
347 uint8_t fptable_ver, headerlen = 0, recordlen, fpentries = 0xf, fpindex;
348 int ret, ofs, fpstrapping;
349 struct lvdstableheader lth;
350
351 if (bios->fp.fptablepointer == 0x0) {
352 /* Apple cards don't have the fp table; the laptops use DDC */
353 /* The table is also missing on some x86 IGPs */
354#ifndef __powerpc__
Ben Skeggs77145f12012-07-31 16:16:21 +1000355 NV_ERROR(drm, "Pointer to flat panel table invalid\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +1000356#endif
Ben Skeggs04a39c52010-02-24 10:03:05 +1000357 bios->digital_min_front_porch = 0x4b;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000358 return 0;
359 }
360
361 fptable = &bios->data[bios->fp.fptablepointer];
362 fptable_ver = fptable[0];
363
364 switch (fptable_ver) {
365 /*
366 * BMP version 0x5.0x11 BIOSen have version 1 like tables, but no
367 * version field, and miss one of the spread spectrum/PWM bytes.
368 * This could affect early GF2Go parts (not seen any appropriate ROMs
369 * though). Here we assume that a version of 0x05 matches this case
370 * (combining with a BMP version check would be better), as the
371 * common case for the panel type field is 0x0005, and that is in
372 * fact what we are reading the first byte of.
373 */
374 case 0x05: /* some NV10, 11, 15, 16 */
375 recordlen = 42;
376 ofs = -1;
377 break;
378 case 0x10: /* some NV15/16, and NV11+ */
379 recordlen = 44;
380 ofs = 0;
381 break;
382 case 0x20: /* NV40+ */
383 headerlen = fptable[1];
384 recordlen = fptable[2];
385 fpentries = fptable[3];
386 /*
387 * fptable[4] is the minimum
388 * RAMDAC_FP_HCRTC -> RAMDAC_FP_HSYNC_START gap
389 */
Ben Skeggs04a39c52010-02-24 10:03:05 +1000390 bios->digital_min_front_porch = fptable[4];
Ben Skeggs6ee73862009-12-11 19:24:15 +1000391 ofs = -7;
392 break;
393 default:
Ben Skeggs77145f12012-07-31 16:16:21 +1000394 NV_ERROR(drm,
Ben Skeggs6ee73862009-12-11 19:24:15 +1000395 "FP table revision %d.%d not currently supported\n",
396 fptable_ver >> 4, fptable_ver & 0xf);
397 return -ENOSYS;
398 }
399
400 if (!bios->is_mobile) /* !mobile only needs digital_min_front_porch */
401 return 0;
402
403 ret = parse_lvds_manufacturer_table_header(dev, bios, &lth);
404 if (ret)
405 return ret;
406
407 if (lth.lvds_ver == 0x30 || lth.lvds_ver == 0x40) {
408 bios->fp.fpxlatetableptr = bios->fp.lvdsmanufacturerpointer +
409 lth.headerlen + 1;
410 bios->fp.xlatwidth = lth.recordlen;
411 }
412 if (bios->fp.fpxlatetableptr == 0x0) {
Ben Skeggs77145f12012-07-31 16:16:21 +1000413 NV_ERROR(drm, "Pointer to flat panel xlat table invalid\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +1000414 return -EINVAL;
415 }
416
417 fpstrapping = get_fp_strap(dev, bios);
418
419 fpindex = bios->data[bios->fp.fpxlatetableptr +
420 fpstrapping * bios->fp.xlatwidth];
421
422 if (fpindex > fpentries) {
Ben Skeggs77145f12012-07-31 16:16:21 +1000423 NV_ERROR(drm, "Bad flat panel table index\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +1000424 return -ENOENT;
425 }
426
427 /* nv4x cards need both a strap value and fpindex of 0xf to use DDC */
428 if (lth.lvds_ver > 0x10)
Ben Skeggs04a39c52010-02-24 10:03:05 +1000429 bios->fp_no_ddc = fpstrapping != 0xf || fpindex != 0xf;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000430
431 /*
432 * If either the strap or xlated fpindex value are 0xf there is no
433 * panel using a strap-derived bios mode present. this condition
434 * includes, but is different from, the DDC panel indicator above
435 */
436 if (fpstrapping == 0xf || fpindex == 0xf)
437 return 0;
438
439 bios->fp.mode_ptr = bios->fp.fptablepointer + headerlen +
440 recordlen * fpindex + ofs;
441
Ben Skeggs77145f12012-07-31 16:16:21 +1000442 NV_INFO(drm, "BIOS FP mode: %dx%d (%dkHz pixel clock)\n",
Ben Skeggs6ee73862009-12-11 19:24:15 +1000443 ROM16(bios->data[bios->fp.mode_ptr + 11]) + 1,
444 ROM16(bios->data[bios->fp.mode_ptr + 25]) + 1,
445 ROM16(bios->data[bios->fp.mode_ptr + 7]) * 10);
446
447 return 0;
448}
449
450bool nouveau_bios_fp_mode(struct drm_device *dev, struct drm_display_mode *mode)
451{
Ben Skeggs77145f12012-07-31 16:16:21 +1000452 struct nouveau_drm *drm = nouveau_drm(dev);
453 struct nvbios *bios = &drm->vbios;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000454 uint8_t *mode_entry = &bios->data[bios->fp.mode_ptr];
455
456 if (!mode) /* just checking whether we can produce a mode */
457 return bios->fp.mode_ptr;
458
459 memset(mode, 0, sizeof(struct drm_display_mode));
460 /*
461 * For version 1.0 (version in byte 0):
462 * bytes 1-2 are "panel type", including bits on whether Colour/mono,
463 * single/dual link, and type (TFT etc.)
464 * bytes 3-6 are bits per colour in RGBX
465 */
466 mode->clock = ROM16(mode_entry[7]) * 10;
467 /* bytes 9-10 is HActive */
468 mode->hdisplay = ROM16(mode_entry[11]) + 1;
469 /*
470 * bytes 13-14 is HValid Start
471 * bytes 15-16 is HValid End
472 */
473 mode->hsync_start = ROM16(mode_entry[17]) + 1;
474 mode->hsync_end = ROM16(mode_entry[19]) + 1;
475 mode->htotal = ROM16(mode_entry[21]) + 1;
476 /* bytes 23-24, 27-30 similarly, but vertical */
477 mode->vdisplay = ROM16(mode_entry[25]) + 1;
478 mode->vsync_start = ROM16(mode_entry[31]) + 1;
479 mode->vsync_end = ROM16(mode_entry[33]) + 1;
480 mode->vtotal = ROM16(mode_entry[35]) + 1;
481 mode->flags |= (mode_entry[37] & 0x10) ?
482 DRM_MODE_FLAG_PHSYNC : DRM_MODE_FLAG_NHSYNC;
483 mode->flags |= (mode_entry[37] & 0x1) ?
484 DRM_MODE_FLAG_PVSYNC : DRM_MODE_FLAG_NVSYNC;
485 /*
486 * bytes 38-39 relate to spread spectrum settings
487 * bytes 40-43 are something to do with PWM
488 */
489
490 mode->status = MODE_OK;
491 mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
492 drm_mode_set_name(mode);
493 return bios->fp.mode_ptr;
494}
495
496int nouveau_bios_parse_lvds_table(struct drm_device *dev, int pxclk, bool *dl, bool *if_is_24bit)
497{
498 /*
499 * The LVDS table header is (mostly) described in
500 * parse_lvds_manufacturer_table_header(): the BIT header additionally
501 * contains the dual-link transition pxclk (in 10s kHz), at byte 5 - if
502 * straps are not being used for the panel, this specifies the frequency
503 * at which modes should be set up in the dual link style.
504 *
505 * Following the header, the BMP (ver 0xa) table has several records,
Daniel Mack3ad2f3f2010-02-03 08:01:28 +0800506 * indexed by a separate xlat table, indexed in turn by the fp strap in
Ben Skeggs6ee73862009-12-11 19:24:15 +1000507 * EXTDEV_BOOT. Each record had a config byte, followed by 6 script
508 * numbers for use by INIT_SUB which controlled panel init and power,
509 * and finally a dword of ms to sleep between power off and on
510 * operations.
511 *
512 * In the BIT versions, the table following the header serves as an
513 * integrated config and xlat table: the records in the table are
514 * indexed by the FP strap nibble in EXTDEV_BOOT, and each record has
515 * two bytes - the first as a config byte, the second for indexing the
516 * fp mode table pointed to by the BIT 'D' table
517 *
518 * DDC is not used until after card init, so selecting the correct table
519 * entry and setting the dual link flag for EDID equipped panels,
520 * requiring tests against the native-mode pixel clock, cannot be done
521 * until later, when this function should be called with non-zero pxclk
522 */
Ben Skeggs77145f12012-07-31 16:16:21 +1000523 struct nouveau_drm *drm = nouveau_drm(dev);
524 struct nvbios *bios = &drm->vbios;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000525 int fpstrapping = get_fp_strap(dev, bios), lvdsmanufacturerindex = 0;
526 struct lvdstableheader lth;
527 uint16_t lvdsofs;
Ben Skeggs04a39c52010-02-24 10:03:05 +1000528 int ret, chip_version = bios->chip_version;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000529
530 ret = parse_lvds_manufacturer_table_header(dev, bios, &lth);
531 if (ret)
532 return ret;
533
534 switch (lth.lvds_ver) {
535 case 0x0a: /* pre NV40 */
536 lvdsmanufacturerindex = bios->data[
537 bios->fp.fpxlatemanufacturertableptr +
538 fpstrapping];
539
540 /* we're done if this isn't the EDID panel case */
541 if (!pxclk)
542 break;
543
544 if (chip_version < 0x25) {
545 /* nv17 behaviour
546 *
547 * It seems the old style lvds script pointer is reused
548 * to select 18/24 bit colour depth for EDID panels.
549 */
550 lvdsmanufacturerindex =
551 (bios->legacy.lvds_single_a_script_ptr & 1) ?
552 2 : 0;
553 if (pxclk >= bios->fp.duallink_transition_clk)
554 lvdsmanufacturerindex++;
555 } else if (chip_version < 0x30) {
556 /* nv28 behaviour (off-chip encoder)
557 *
558 * nv28 does a complex dance of first using byte 121 of
559 * the EDID to choose the lvdsmanufacturerindex, then
560 * later attempting to match the EDID manufacturer and
561 * product IDs in a table (signature 'pidt' (panel id
562 * table?)), setting an lvdsmanufacturerindex of 0 and
563 * an fp strap of the match index (or 0xf if none)
564 */
565 lvdsmanufacturerindex = 0;
566 } else {
567 /* nv31, nv34 behaviour */
568 lvdsmanufacturerindex = 0;
569 if (pxclk >= bios->fp.duallink_transition_clk)
570 lvdsmanufacturerindex = 2;
571 if (pxclk >= 140000)
572 lvdsmanufacturerindex = 3;
573 }
574
575 /*
576 * nvidia set the high nibble of (cr57=f, cr58) to
577 * lvdsmanufacturerindex in this case; we don't
578 */
579 break;
580 case 0x30: /* NV4x */
581 case 0x40: /* G80/G90 */
582 lvdsmanufacturerindex = fpstrapping;
583 break;
584 default:
Ben Skeggs77145f12012-07-31 16:16:21 +1000585 NV_ERROR(drm, "LVDS table revision not currently supported\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +1000586 return -ENOSYS;
587 }
588
589 lvdsofs = bios->fp.xlated_entry = bios->fp.lvdsmanufacturerpointer + lth.headerlen + lth.recordlen * lvdsmanufacturerindex;
590 switch (lth.lvds_ver) {
591 case 0x0a:
592 bios->fp.power_off_for_reset = bios->data[lvdsofs] & 1;
593 bios->fp.reset_after_pclk_change = bios->data[lvdsofs] & 2;
594 bios->fp.dual_link = bios->data[lvdsofs] & 4;
595 bios->fp.link_c_increment = bios->data[lvdsofs] & 8;
596 *if_is_24bit = bios->data[lvdsofs] & 16;
597 break;
598 case 0x30:
Ben Skeggsf3bbb9c2010-03-18 12:05:43 +1000599 case 0x40:
Ben Skeggs6ee73862009-12-11 19:24:15 +1000600 /*
601 * No sign of the "power off for reset" or "reset for panel
602 * on" bits, but it's safer to assume we should
603 */
604 bios->fp.power_off_for_reset = true;
605 bios->fp.reset_after_pclk_change = true;
Ben Skeggsf3bbb9c2010-03-18 12:05:43 +1000606
Ben Skeggs6ee73862009-12-11 19:24:15 +1000607 /*
608 * It's ok lvdsofs is wrong for nv4x edid case; dual_link is
Ben Skeggsf3bbb9c2010-03-18 12:05:43 +1000609 * over-written, and if_is_24bit isn't used
Ben Skeggs6ee73862009-12-11 19:24:15 +1000610 */
611 bios->fp.dual_link = bios->data[lvdsofs] & 1;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000612 bios->fp.if_is_24bit = bios->data[lvdsofs] & 2;
613 bios->fp.strapless_is_24bit = bios->data[bios->fp.lvdsmanufacturerpointer + 4];
614 bios->fp.duallink_transition_clk = ROM16(bios->data[bios->fp.lvdsmanufacturerpointer + 5]) * 10;
615 break;
616 }
617
618 /* set dual_link flag for EDID case */
619 if (pxclk && (chip_version < 0x25 || chip_version > 0x28))
620 bios->fp.dual_link = (pxclk >= bios->fp.duallink_transition_clk);
621
622 *dl = bios->fp.dual_link;
623
624 return 0;
625}
626
Ben Skeggscb75d972012-07-11 10:44:20 +1000627int run_tmds_table(struct drm_device *dev, struct dcb_output *dcbent, int head, int pxclk)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000628{
629 /*
630 * the pxclk parameter is in kHz
631 *
632 * This runs the TMDS regs setting code found on BIT bios cards
633 *
634 * For ffs(or) == 1 use the first table, for ffs(or) == 2 and
635 * ffs(or) == 3, use the second.
636 */
637
Ben Skeggs77145f12012-07-31 16:16:21 +1000638 struct nouveau_drm *drm = nouveau_drm(dev);
639 struct nouveau_device *device = nv_device(drm->device);
640 struct nvbios *bios = &drm->vbios;
Ben Skeggs04a39c52010-02-24 10:03:05 +1000641 int cv = bios->chip_version;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000642 uint16_t clktable = 0, scriptptr;
643 uint32_t sel_clk_binding, sel_clk;
644
645 /* pre-nv17 off-chip tmds uses scripts, post nv17 doesn't */
646 if (cv >= 0x17 && cv != 0x1a && cv != 0x20 &&
647 dcbent->location != DCB_LOC_ON_CHIP)
648 return 0;
649
650 switch (ffs(dcbent->or)) {
651 case 1:
652 clktable = bios->tmds.output0_script_ptr;
653 break;
654 case 2:
655 case 3:
656 clktable = bios->tmds.output1_script_ptr;
657 break;
658 }
659
660 if (!clktable) {
Ben Skeggs77145f12012-07-31 16:16:21 +1000661 NV_ERROR(drm, "Pixel clock comparison table not found\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +1000662 return -EINVAL;
663 }
664
665 scriptptr = clkcmptable(bios, clktable, pxclk);
666
667 if (!scriptptr) {
Ben Skeggs77145f12012-07-31 16:16:21 +1000668 NV_ERROR(drm, "TMDS output init script not found\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +1000669 return -ENOENT;
670 }
671
672 /* don't let script change pll->head binding */
Ben Skeggs77145f12012-07-31 16:16:21 +1000673 sel_clk_binding = nv_rd32(device, NV_PRAMDAC_SEL_CLK) & 0x50000;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000674 run_digital_op_script(dev, scriptptr, dcbent, head, pxclk >= 165000);
675 sel_clk = NVReadRAMDAC(dev, 0, NV_PRAMDAC_SEL_CLK) & ~0x50000;
676 NVWriteRAMDAC(dev, 0, NV_PRAMDAC_SEL_CLK, sel_clk | sel_clk_binding);
677
678 return 0;
679}
680
Ben Skeggs6ee73862009-12-11 19:24:15 +1000681static void parse_bios_version(struct drm_device *dev, struct nvbios *bios, uint16_t offset)
682{
683 /*
684 * offset + 0 (8 bits): Micro version
685 * offset + 1 (8 bits): Minor version
686 * offset + 2 (8 bits): Chip version
687 * offset + 3 (8 bits): Major version
688 */
Ben Skeggs77145f12012-07-31 16:16:21 +1000689 struct nouveau_drm *drm = nouveau_drm(dev);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000690
691 bios->major_version = bios->data[offset + 3];
Ben Skeggs04a39c52010-02-24 10:03:05 +1000692 bios->chip_version = bios->data[offset + 2];
Ben Skeggs77145f12012-07-31 16:16:21 +1000693 NV_INFO(drm, "Bios version %02x.%02x.%02x.%02x\n",
Ben Skeggs6ee73862009-12-11 19:24:15 +1000694 bios->data[offset + 3], bios->data[offset + 2],
695 bios->data[offset + 1], bios->data[offset]);
696}
697
698static void parse_script_table_pointers(struct nvbios *bios, uint16_t offset)
699{
700 /*
701 * Parses the init table segment for pointers used in script execution.
702 *
703 * offset + 0 (16 bits): init script tables pointer
704 * offset + 2 (16 bits): macro index table pointer
705 * offset + 4 (16 bits): macro table pointer
706 * offset + 6 (16 bits): condition table pointer
707 * offset + 8 (16 bits): io condition table pointer
708 * offset + 10 (16 bits): io flag condition table pointer
709 * offset + 12 (16 bits): init function table pointer
710 */
711
712 bios->init_script_tbls_ptr = ROM16(bios->data[offset]);
713 bios->macro_index_tbl_ptr = ROM16(bios->data[offset + 2]);
714 bios->macro_tbl_ptr = ROM16(bios->data[offset + 4]);
715 bios->condition_tbl_ptr = ROM16(bios->data[offset + 6]);
716 bios->io_condition_tbl_ptr = ROM16(bios->data[offset + 8]);
717 bios->io_flag_condition_tbl_ptr = ROM16(bios->data[offset + 10]);
718 bios->init_function_tbl_ptr = ROM16(bios->data[offset + 12]);
719}
720
721static int parse_bit_A_tbl_entry(struct drm_device *dev, struct nvbios *bios, struct bit_entry *bitentry)
722{
723 /*
724 * Parses the load detect values for g80 cards.
725 *
726 * offset + 0 (16 bits): loadval table pointer
727 */
728
Ben Skeggs77145f12012-07-31 16:16:21 +1000729 struct nouveau_drm *drm = nouveau_drm(dev);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000730 uint16_t load_table_ptr;
731 uint8_t version, headerlen, entrylen, num_entries;
732
733 if (bitentry->length != 3) {
Ben Skeggs77145f12012-07-31 16:16:21 +1000734 NV_ERROR(drm, "Do not understand BIT A table\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +1000735 return -EINVAL;
736 }
737
738 load_table_ptr = ROM16(bios->data[bitentry->offset]);
739
740 if (load_table_ptr == 0x0) {
Ben Skeggs77145f12012-07-31 16:16:21 +1000741 NV_DEBUG(drm, "Pointer to BIT loadval table invalid\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +1000742 return -EINVAL;
743 }
744
745 version = bios->data[load_table_ptr];
746
747 if (version != 0x10) {
Ben Skeggs77145f12012-07-31 16:16:21 +1000748 NV_ERROR(drm, "BIT loadval table version %d.%d not supported\n",
Ben Skeggs6ee73862009-12-11 19:24:15 +1000749 version >> 4, version & 0xF);
750 return -ENOSYS;
751 }
752
753 headerlen = bios->data[load_table_ptr + 1];
754 entrylen = bios->data[load_table_ptr + 2];
755 num_entries = bios->data[load_table_ptr + 3];
756
757 if (headerlen != 4 || entrylen != 4 || num_entries != 2) {
Ben Skeggs77145f12012-07-31 16:16:21 +1000758 NV_ERROR(drm, "Do not understand BIT loadval table\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +1000759 return -EINVAL;
760 }
761
762 /* First entry is normal dac, 2nd tv-out perhaps? */
Ben Skeggs04a39c52010-02-24 10:03:05 +1000763 bios->dactestval = ROM32(bios->data[load_table_ptr + headerlen]) & 0x3ff;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000764
765 return 0;
766}
767
768static int parse_bit_C_tbl_entry(struct drm_device *dev, struct nvbios *bios, struct bit_entry *bitentry)
769{
770 /*
771 * offset + 8 (16 bits): PLL limits table pointer
772 *
773 * There's more in here, but that's unknown.
774 */
Ben Skeggs77145f12012-07-31 16:16:21 +1000775 struct nouveau_drm *drm = nouveau_drm(dev);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000776
777 if (bitentry->length < 10) {
Ben Skeggs77145f12012-07-31 16:16:21 +1000778 NV_ERROR(drm, "Do not understand BIT C table\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +1000779 return -EINVAL;
780 }
781
782 bios->pll_limit_tbl_ptr = ROM16(bios->data[bitentry->offset + 8]);
783
784 return 0;
785}
786
787static int parse_bit_display_tbl_entry(struct drm_device *dev, struct nvbios *bios, struct bit_entry *bitentry)
788{
789 /*
790 * Parses the flat panel table segment that the bit entry points to.
791 * Starting at bitentry->offset:
792 *
793 * offset + 0 (16 bits): ??? table pointer - seems to have 18 byte
794 * records beginning with a freq.
795 * offset + 2 (16 bits): mode table pointer
796 */
Ben Skeggs77145f12012-07-31 16:16:21 +1000797 struct nouveau_drm *drm = nouveau_drm(dev);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000798
799 if (bitentry->length != 4) {
Ben Skeggs77145f12012-07-31 16:16:21 +1000800 NV_ERROR(drm, "Do not understand BIT display table\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +1000801 return -EINVAL;
802 }
803
804 bios->fp.fptablepointer = ROM16(bios->data[bitentry->offset + 2]);
805
806 return 0;
807}
808
809static int parse_bit_init_tbl_entry(struct drm_device *dev, struct nvbios *bios, struct bit_entry *bitentry)
810{
811 /*
812 * Parses the init table segment that the bit entry points to.
813 *
814 * See parse_script_table_pointers for layout
815 */
Ben Skeggs77145f12012-07-31 16:16:21 +1000816 struct nouveau_drm *drm = nouveau_drm(dev);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000817
818 if (bitentry->length < 14) {
Ben Skeggs77145f12012-07-31 16:16:21 +1000819 NV_ERROR(drm, "Do not understand init table\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +1000820 return -EINVAL;
821 }
822
823 parse_script_table_pointers(bios, bitentry->offset);
824
825 if (bitentry->length >= 16)
826 bios->some_script_ptr = ROM16(bios->data[bitentry->offset + 14]);
827 if (bitentry->length >= 18)
828 bios->init96_tbl_ptr = ROM16(bios->data[bitentry->offset + 16]);
829
830 return 0;
831}
832
833static int parse_bit_i_tbl_entry(struct drm_device *dev, struct nvbios *bios, struct bit_entry *bitentry)
834{
835 /*
836 * BIT 'i' (info?) table
837 *
838 * offset + 0 (32 bits): BIOS version dword (as in B table)
839 * offset + 5 (8 bits): BIOS feature byte (same as for BMP?)
840 * offset + 13 (16 bits): pointer to table containing DAC load
841 * detection comparison values
842 *
843 * There's other things in the table, purpose unknown
844 */
845
Ben Skeggs77145f12012-07-31 16:16:21 +1000846 struct nouveau_drm *drm = nouveau_drm(dev);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000847 uint16_t daccmpoffset;
848 uint8_t dacver, dacheaderlen;
849
850 if (bitentry->length < 6) {
Ben Skeggs77145f12012-07-31 16:16:21 +1000851 NV_ERROR(drm, "BIT i table too short for needed information\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +1000852 return -EINVAL;
853 }
854
855 parse_bios_version(dev, bios, bitentry->offset);
856
857 /*
858 * bit 4 seems to indicate a mobile bios (doesn't suffer from BMP's
859 * Quadro identity crisis), other bits possibly as for BMP feature byte
860 */
861 bios->feature_byte = bios->data[bitentry->offset + 5];
862 bios->is_mobile = bios->feature_byte & FEATURE_MOBILE;
863
864 if (bitentry->length < 15) {
Ben Skeggs77145f12012-07-31 16:16:21 +1000865 NV_WARN(drm, "BIT i table not long enough for DAC load "
Ben Skeggs6ee73862009-12-11 19:24:15 +1000866 "detection comparison table\n");
867 return -EINVAL;
868 }
869
870 daccmpoffset = ROM16(bios->data[bitentry->offset + 13]);
871
872 /* doesn't exist on g80 */
873 if (!daccmpoffset)
874 return 0;
875
876 /*
877 * The first value in the table, following the header, is the
878 * comparison value, the second entry is a comparison value for
879 * TV load detection.
880 */
881
882 dacver = bios->data[daccmpoffset];
883 dacheaderlen = bios->data[daccmpoffset + 1];
884
885 if (dacver != 0x00 && dacver != 0x10) {
Ben Skeggs77145f12012-07-31 16:16:21 +1000886 NV_WARN(drm, "DAC load detection comparison table version "
Ben Skeggs6ee73862009-12-11 19:24:15 +1000887 "%d.%d not known\n", dacver >> 4, dacver & 0xf);
888 return -ENOSYS;
889 }
890
Ben Skeggs04a39c52010-02-24 10:03:05 +1000891 bios->dactestval = ROM32(bios->data[daccmpoffset + dacheaderlen]);
892 bios->tvdactestval = ROM32(bios->data[daccmpoffset + dacheaderlen + 4]);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000893
894 return 0;
895}
896
897static int parse_bit_lvds_tbl_entry(struct drm_device *dev, struct nvbios *bios, struct bit_entry *bitentry)
898{
899 /*
900 * Parses the LVDS table segment that the bit entry points to.
901 * Starting at bitentry->offset:
902 *
903 * offset + 0 (16 bits): LVDS strap xlate table pointer
904 */
905
Ben Skeggs77145f12012-07-31 16:16:21 +1000906 struct nouveau_drm *drm = nouveau_drm(dev);
907
Ben Skeggs6ee73862009-12-11 19:24:15 +1000908 if (bitentry->length != 2) {
Ben Skeggs77145f12012-07-31 16:16:21 +1000909 NV_ERROR(drm, "Do not understand BIT LVDS table\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +1000910 return -EINVAL;
911 }
912
913 /*
914 * No idea if it's still called the LVDS manufacturer table, but
915 * the concept's close enough.
916 */
917 bios->fp.lvdsmanufacturerpointer = ROM16(bios->data[bitentry->offset]);
918
919 return 0;
920}
921
922static int
923parse_bit_M_tbl_entry(struct drm_device *dev, struct nvbios *bios,
924 struct bit_entry *bitentry)
925{
926 /*
927 * offset + 2 (8 bits): number of options in an
928 * INIT_RAM_RESTRICT_ZM_REG_GROUP opcode option set
929 * offset + 3 (16 bits): pointer to strap xlate table for RAM
930 * restrict option selection
931 *
932 * There's a bunch of bits in this table other than the RAM restrict
933 * stuff that we don't use - their use currently unknown
934 */
935
Ben Skeggs6ee73862009-12-11 19:24:15 +1000936 /*
937 * Older bios versions don't have a sufficiently long table for
938 * what we want
939 */
940 if (bitentry->length < 0x5)
941 return 0;
942
Ben Skeggs4709bff2010-09-13 15:18:40 +1000943 if (bitentry->version < 2) {
Marcin Kościelnicki37383652009-12-15 00:37:31 +0000944 bios->ram_restrict_group_count = bios->data[bitentry->offset + 2];
945 bios->ram_restrict_tbl_ptr = ROM16(bios->data[bitentry->offset + 3]);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000946 } else {
Marcin Kościelnicki37383652009-12-15 00:37:31 +0000947 bios->ram_restrict_group_count = bios->data[bitentry->offset + 0];
948 bios->ram_restrict_tbl_ptr = ROM16(bios->data[bitentry->offset + 1]);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000949 }
950
Ben Skeggs6ee73862009-12-11 19:24:15 +1000951 return 0;
952}
953
954static int parse_bit_tmds_tbl_entry(struct drm_device *dev, struct nvbios *bios, struct bit_entry *bitentry)
955{
956 /*
957 * Parses the pointer to the TMDS table
958 *
959 * Starting at bitentry->offset:
960 *
961 * offset + 0 (16 bits): TMDS table pointer
962 *
963 * The TMDS table is typically found just before the DCB table, with a
964 * characteristic signature of 0x11,0x13 (1.1 being version, 0x13 being
965 * length?)
966 *
967 * At offset +7 is a pointer to a script, which I don't know how to
968 * run yet.
969 * At offset +9 is a pointer to another script, likewise
970 * Offset +11 has a pointer to a table where the first word is a pxclk
971 * frequency and the second word a pointer to a script, which should be
972 * run if the comparison pxclk frequency is less than the pxclk desired.
973 * This repeats for decreasing comparison frequencies
974 * Offset +13 has a pointer to a similar table
975 * The selection of table (and possibly +7/+9 script) is dictated by
976 * "or" from the DCB.
977 */
978
Ben Skeggs77145f12012-07-31 16:16:21 +1000979 struct nouveau_drm *drm = nouveau_drm(dev);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000980 uint16_t tmdstableptr, script1, script2;
981
982 if (bitentry->length != 2) {
Ben Skeggs77145f12012-07-31 16:16:21 +1000983 NV_ERROR(drm, "Do not understand BIT TMDS table\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +1000984 return -EINVAL;
985 }
986
987 tmdstableptr = ROM16(bios->data[bitentry->offset]);
Ben Skeggs98720bf2010-08-13 08:31:22 +1000988 if (!tmdstableptr) {
Ben Skeggs77145f12012-07-31 16:16:21 +1000989 NV_ERROR(drm, "Pointer to TMDS table invalid\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +1000990 return -EINVAL;
991 }
992
Ben Skeggs77145f12012-07-31 16:16:21 +1000993 NV_INFO(drm, "TMDS table version %d.%d\n",
Ben Skeggs98720bf2010-08-13 08:31:22 +1000994 bios->data[tmdstableptr] >> 4, bios->data[tmdstableptr] & 0xf);
995
Ben Skeggs6ee73862009-12-11 19:24:15 +1000996 /* nv50+ has v2.0, but we don't parse it atm */
Ben Skeggs98720bf2010-08-13 08:31:22 +1000997 if (bios->data[tmdstableptr] != 0x11)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000998 return -ENOSYS;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000999
1000 /*
1001 * These two scripts are odd: they don't seem to get run even when
1002 * they are not stubbed.
1003 */
1004 script1 = ROM16(bios->data[tmdstableptr + 7]);
1005 script2 = ROM16(bios->data[tmdstableptr + 9]);
1006 if (bios->data[script1] != 'q' || bios->data[script2] != 'q')
Ben Skeggs77145f12012-07-31 16:16:21 +10001007 NV_WARN(drm, "TMDS table script pointers not stubbed\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +10001008
1009 bios->tmds.output0_script_ptr = ROM16(bios->data[tmdstableptr + 11]);
1010 bios->tmds.output1_script_ptr = ROM16(bios->data[tmdstableptr + 13]);
1011
1012 return 0;
1013}
1014
Ben Skeggs6ee73862009-12-11 19:24:15 +10001015struct bit_table {
1016 const char id;
1017 int (* const parse_fn)(struct drm_device *, struct nvbios *, struct bit_entry *);
1018};
1019
1020#define BIT_TABLE(id, funcid) ((struct bit_table){ id, parse_bit_##funcid##_tbl_entry })
1021
Ben Skeggs4709bff2010-09-13 15:18:40 +10001022int
1023bit_table(struct drm_device *dev, u8 id, struct bit_entry *bit)
1024{
Ben Skeggs77145f12012-07-31 16:16:21 +10001025 struct nouveau_drm *drm = nouveau_drm(dev);
1026 struct nvbios *bios = &drm->vbios;
Ben Skeggs4709bff2010-09-13 15:18:40 +10001027 u8 entries, *entry;
1028
Ben Skeggsb4c26812011-10-12 16:36:42 +10001029 if (bios->type != NVBIOS_BIT)
1030 return -ENODEV;
1031
Ben Skeggs4709bff2010-09-13 15:18:40 +10001032 entries = bios->data[bios->offset + 10];
1033 entry = &bios->data[bios->offset + 12];
1034 while (entries--) {
1035 if (entry[0] == id) {
1036 bit->id = entry[0];
1037 bit->version = entry[1];
1038 bit->length = ROM16(entry[2]);
1039 bit->offset = ROM16(entry[4]);
Ben Skeggsf9f9f532011-10-12 16:48:48 +10001040 bit->data = ROMPTR(dev, entry[4]);
Ben Skeggs4709bff2010-09-13 15:18:40 +10001041 return 0;
1042 }
1043
1044 entry += bios->data[bios->offset + 9];
1045 }
1046
1047 return -ENOENT;
1048}
1049
Ben Skeggs6ee73862009-12-11 19:24:15 +10001050static int
1051parse_bit_table(struct nvbios *bios, const uint16_t bitoffset,
1052 struct bit_table *table)
1053{
1054 struct drm_device *dev = bios->dev;
Ben Skeggs77145f12012-07-31 16:16:21 +10001055 struct nouveau_drm *drm = nouveau_drm(dev);
Ben Skeggs6ee73862009-12-11 19:24:15 +10001056 struct bit_entry bitentry;
1057
Ben Skeggs4709bff2010-09-13 15:18:40 +10001058 if (bit_table(dev, table->id, &bitentry) == 0)
Ben Skeggs6ee73862009-12-11 19:24:15 +10001059 return table->parse_fn(dev, bios, &bitentry);
Ben Skeggs6ee73862009-12-11 19:24:15 +10001060
Ben Skeggs77145f12012-07-31 16:16:21 +10001061 NV_INFO(drm, "BIT table '%c' not found\n", table->id);
Ben Skeggs6ee73862009-12-11 19:24:15 +10001062 return -ENOSYS;
1063}
1064
1065static int
1066parse_bit_structure(struct nvbios *bios, const uint16_t bitoffset)
1067{
1068 int ret;
1069
1070 /*
1071 * The only restriction on parsing order currently is having 'i' first
1072 * for use of bios->*_version or bios->feature_byte while parsing;
1073 * functions shouldn't be actually *doing* anything apart from pulling
1074 * data from the image into the bios struct, thus no interdependencies
1075 */
1076 ret = parse_bit_table(bios, bitoffset, &BIT_TABLE('i', i));
1077 if (ret) /* info? */
1078 return ret;
1079 if (bios->major_version >= 0x60) /* g80+ */
1080 parse_bit_table(bios, bitoffset, &BIT_TABLE('A', A));
1081 ret = parse_bit_table(bios, bitoffset, &BIT_TABLE('C', C));
1082 if (ret)
1083 return ret;
1084 parse_bit_table(bios, bitoffset, &BIT_TABLE('D', display));
1085 ret = parse_bit_table(bios, bitoffset, &BIT_TABLE('I', init));
1086 if (ret)
1087 return ret;
1088 parse_bit_table(bios, bitoffset, &BIT_TABLE('M', M)); /* memory? */
1089 parse_bit_table(bios, bitoffset, &BIT_TABLE('L', lvds));
1090 parse_bit_table(bios, bitoffset, &BIT_TABLE('T', tmds));
Ben Skeggs6ee73862009-12-11 19:24:15 +10001091
1092 return 0;
1093}
1094
1095static int parse_bmp_structure(struct drm_device *dev, struct nvbios *bios, unsigned int offset)
1096{
1097 /*
1098 * Parses the BMP structure for useful things, but does not act on them
1099 *
1100 * offset + 5: BMP major version
1101 * offset + 6: BMP minor version
1102 * offset + 9: BMP feature byte
1103 * offset + 10: BCD encoded BIOS version
1104 *
1105 * offset + 18: init script table pointer (for bios versions < 5.10h)
1106 * offset + 20: extra init script table pointer (for bios
1107 * versions < 5.10h)
1108 *
1109 * offset + 24: memory init table pointer (used on early bios versions)
1110 * offset + 26: SDR memory sequencing setup data table
1111 * offset + 28: DDR memory sequencing setup data table
1112 *
1113 * offset + 54: index of I2C CRTC pair to use for CRT output
1114 * offset + 55: index of I2C CRTC pair to use for TV output
1115 * offset + 56: index of I2C CRTC pair to use for flat panel output
1116 * offset + 58: write CRTC index for I2C pair 0
1117 * offset + 59: read CRTC index for I2C pair 0
1118 * offset + 60: write CRTC index for I2C pair 1
1119 * offset + 61: read CRTC index for I2C pair 1
1120 *
1121 * offset + 67: maximum internal PLL frequency (single stage PLL)
1122 * offset + 71: minimum internal PLL frequency (single stage PLL)
1123 *
1124 * offset + 75: script table pointers, as described in
1125 * parse_script_table_pointers
1126 *
1127 * offset + 89: TMDS single link output A table pointer
1128 * offset + 91: TMDS single link output B table pointer
1129 * offset + 95: LVDS single link output A table pointer
1130 * offset + 105: flat panel timings table pointer
1131 * offset + 107: flat panel strapping translation table pointer
1132 * offset + 117: LVDS manufacturer panel config table pointer
1133 * offset + 119: LVDS manufacturer strapping translation table pointer
1134 *
1135 * offset + 142: PLL limits table pointer
1136 *
1137 * offset + 156: minimum pixel clock for LVDS dual link
1138 */
1139
Ben Skeggs77145f12012-07-31 16:16:21 +10001140 struct nouveau_drm *drm = nouveau_drm(dev);
Ben Skeggs6ee73862009-12-11 19:24:15 +10001141 uint8_t *bmp = &bios->data[offset], bmp_version_major, bmp_version_minor;
1142 uint16_t bmplength;
1143 uint16_t legacy_scripts_offset, legacy_i2c_offset;
1144
1145 /* load needed defaults in case we can't parse this info */
Ben Skeggs04a39c52010-02-24 10:03:05 +10001146 bios->digital_min_front_porch = 0x4b;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001147 bios->fmaxvco = 256000;
1148 bios->fminvco = 128000;
1149 bios->fp.duallink_transition_clk = 90000;
1150
1151 bmp_version_major = bmp[5];
1152 bmp_version_minor = bmp[6];
1153
Ben Skeggs77145f12012-07-31 16:16:21 +10001154 NV_INFO(drm, "BMP version %d.%d\n",
Ben Skeggs6ee73862009-12-11 19:24:15 +10001155 bmp_version_major, bmp_version_minor);
1156
1157 /*
1158 * Make sure that 0x36 is blank and can't be mistaken for a DCB
1159 * pointer on early versions
1160 */
1161 if (bmp_version_major < 5)
1162 *(uint16_t *)&bios->data[0x36] = 0;
1163
1164 /*
1165 * Seems that the minor version was 1 for all major versions prior
1166 * to 5. Version 6 could theoretically exist, but I suspect BIT
1167 * happened instead.
1168 */
1169 if ((bmp_version_major < 5 && bmp_version_minor != 1) || bmp_version_major > 5) {
Ben Skeggs77145f12012-07-31 16:16:21 +10001170 NV_ERROR(drm, "You have an unsupported BMP version. "
Ben Skeggs6ee73862009-12-11 19:24:15 +10001171 "Please send in your bios\n");
1172 return -ENOSYS;
1173 }
1174
1175 if (bmp_version_major == 0)
1176 /* nothing that's currently useful in this version */
1177 return 0;
1178 else if (bmp_version_major == 1)
1179 bmplength = 44; /* exact for 1.01 */
1180 else if (bmp_version_major == 2)
1181 bmplength = 48; /* exact for 2.01 */
1182 else if (bmp_version_major == 3)
1183 bmplength = 54;
1184 /* guessed - mem init tables added in this version */
1185 else if (bmp_version_major == 4 || bmp_version_minor < 0x1)
1186 /* don't know if 5.0 exists... */
1187 bmplength = 62;
1188 /* guessed - BMP I2C indices added in version 4*/
1189 else if (bmp_version_minor < 0x6)
1190 bmplength = 67; /* exact for 5.01 */
1191 else if (bmp_version_minor < 0x10)
1192 bmplength = 75; /* exact for 5.06 */
1193 else if (bmp_version_minor == 0x10)
1194 bmplength = 89; /* exact for 5.10h */
1195 else if (bmp_version_minor < 0x14)
1196 bmplength = 118; /* exact for 5.11h */
1197 else if (bmp_version_minor < 0x24)
1198 /*
1199 * Not sure of version where pll limits came in;
1200 * certainly exist by 0x24 though.
1201 */
1202 /* length not exact: this is long enough to get lvds members */
1203 bmplength = 123;
1204 else if (bmp_version_minor < 0x27)
1205 /*
1206 * Length not exact: this is long enough to get pll limit
1207 * member
1208 */
1209 bmplength = 144;
1210 else
1211 /*
1212 * Length not exact: this is long enough to get dual link
1213 * transition clock.
1214 */
1215 bmplength = 158;
1216
1217 /* checksum */
1218 if (nv_cksum(bmp, 8)) {
Ben Skeggs77145f12012-07-31 16:16:21 +10001219 NV_ERROR(drm, "Bad BMP checksum\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +10001220 return -EINVAL;
1221 }
1222
1223 /*
1224 * Bit 4 seems to indicate either a mobile bios or a quadro card --
1225 * mobile behaviour consistent (nv11+), quadro only seen nv18gl-nv36gl
1226 * (not nv10gl), bit 5 that the flat panel tables are present, and
1227 * bit 6 a tv bios.
1228 */
1229 bios->feature_byte = bmp[9];
1230
1231 parse_bios_version(dev, bios, offset + 10);
1232
1233 if (bmp_version_major < 5 || bmp_version_minor < 0x10)
1234 bios->old_style_init = true;
1235 legacy_scripts_offset = 18;
1236 if (bmp_version_major < 2)
1237 legacy_scripts_offset -= 4;
1238 bios->init_script_tbls_ptr = ROM16(bmp[legacy_scripts_offset]);
1239 bios->extra_init_script_tbl_ptr = ROM16(bmp[legacy_scripts_offset + 2]);
1240
1241 if (bmp_version_major > 2) { /* appears in BMP 3 */
1242 bios->legacy.mem_init_tbl_ptr = ROM16(bmp[24]);
1243 bios->legacy.sdr_seq_tbl_ptr = ROM16(bmp[26]);
1244 bios->legacy.ddr_seq_tbl_ptr = ROM16(bmp[28]);
1245 }
1246
1247 legacy_i2c_offset = 0x48; /* BMP version 2 & 3 */
1248 if (bmplength > 61)
1249 legacy_i2c_offset = offset + 54;
1250 bios->legacy.i2c_indices.crt = bios->data[legacy_i2c_offset];
1251 bios->legacy.i2c_indices.tv = bios->data[legacy_i2c_offset + 1];
1252 bios->legacy.i2c_indices.panel = bios->data[legacy_i2c_offset + 2];
Ben Skeggs6ee73862009-12-11 19:24:15 +10001253
1254 if (bmplength > 74) {
1255 bios->fmaxvco = ROM32(bmp[67]);
1256 bios->fminvco = ROM32(bmp[71]);
1257 }
1258 if (bmplength > 88)
1259 parse_script_table_pointers(bios, offset + 75);
1260 if (bmplength > 94) {
1261 bios->tmds.output0_script_ptr = ROM16(bmp[89]);
1262 bios->tmds.output1_script_ptr = ROM16(bmp[91]);
1263 /*
1264 * Never observed in use with lvds scripts, but is reused for
1265 * 18/24 bit panel interface default for EDID equipped panels
1266 * (if_is_24bit not set directly to avoid any oscillation).
1267 */
1268 bios->legacy.lvds_single_a_script_ptr = ROM16(bmp[95]);
1269 }
1270 if (bmplength > 108) {
1271 bios->fp.fptablepointer = ROM16(bmp[105]);
1272 bios->fp.fpxlatetableptr = ROM16(bmp[107]);
1273 bios->fp.xlatwidth = 1;
1274 }
1275 if (bmplength > 120) {
1276 bios->fp.lvdsmanufacturerpointer = ROM16(bmp[117]);
1277 bios->fp.fpxlatemanufacturertableptr = ROM16(bmp[119]);
1278 }
1279 if (bmplength > 143)
1280 bios->pll_limit_tbl_ptr = ROM16(bmp[142]);
1281
1282 if (bmplength > 157)
1283 bios->fp.duallink_transition_clk = ROM16(bmp[156]) * 10;
1284
1285 return 0;
1286}
1287
1288static uint16_t findstr(uint8_t *data, int n, const uint8_t *str, int len)
1289{
1290 int i, j;
1291
1292 for (i = 0; i <= (n - len); i++) {
1293 for (j = 0; j < len; j++)
1294 if (data[i + j] != str[j])
1295 break;
1296 if (j == len)
1297 return i;
1298 }
1299
1300 return 0;
1301}
1302
Ben Skeggs6b5a81a2011-11-10 15:42:55 +10001303void *
Ben Skeggse0996ae2012-07-10 12:20:17 +10001304olddcb_table(struct drm_device *dev)
Ben Skeggs6b5a81a2011-11-10 15:42:55 +10001305{
Ben Skeggs77145f12012-07-31 16:16:21 +10001306 struct nouveau_drm *drm = nouveau_drm(dev);
Ben Skeggs6b5a81a2011-11-10 15:42:55 +10001307 u8 *dcb = NULL;
1308
Ben Skeggs77145f12012-07-31 16:16:21 +10001309 if (nv_device(drm->device)->card_type > NV_04)
1310 dcb = ROMPTR(dev, drm->vbios.data[0x36]);
Ben Skeggs6b5a81a2011-11-10 15:42:55 +10001311 if (!dcb) {
Ben Skeggs77145f12012-07-31 16:16:21 +10001312 NV_WARN(drm, "No DCB data found in VBIOS\n");
Ben Skeggs6b5a81a2011-11-10 15:42:55 +10001313 return NULL;
1314 }
1315
1316 if (dcb[0] >= 0x41) {
Ben Skeggs77145f12012-07-31 16:16:21 +10001317 NV_WARN(drm, "DCB version 0x%02x unknown\n", dcb[0]);
Ben Skeggs6b5a81a2011-11-10 15:42:55 +10001318 return NULL;
1319 } else
1320 if (dcb[0] >= 0x30) {
1321 if (ROM32(dcb[6]) == 0x4edcbdcb)
1322 return dcb;
1323 } else
1324 if (dcb[0] >= 0x20) {
1325 if (ROM32(dcb[4]) == 0x4edcbdcb)
1326 return dcb;
1327 } else
1328 if (dcb[0] >= 0x15) {
1329 if (!memcmp(&dcb[-7], "DEV_REC", 7))
1330 return dcb;
1331 } else {
1332 /*
1333 * v1.4 (some NV15/16, NV11+) seems the same as v1.5, but
1334 * always has the same single (crt) entry, even when tv-out
1335 * present, so the conclusion is this version cannot really
1336 * be used.
1337 *
1338 * v1.2 tables (some NV6/10, and NV15+) normally have the
1339 * same 5 entries, which are not specific to the card and so
1340 * no use.
1341 *
1342 * v1.2 does have an I2C table that read_dcb_i2c_table can
1343 * handle, but cards exist (nv11 in #14821) with a bad i2c
1344 * table pointer, so use the indices parsed in
1345 * parse_bmp_structure.
1346 *
1347 * v1.1 (NV5+, maybe some NV4) is entirely unhelpful
1348 */
Ben Skeggs77145f12012-07-31 16:16:21 +10001349 NV_WARN(drm, "No useful DCB data in VBIOS\n");
Ben Skeggs6b5a81a2011-11-10 15:42:55 +10001350 return NULL;
1351 }
1352
Ben Skeggs77145f12012-07-31 16:16:21 +10001353 NV_WARN(drm, "DCB header validation failed\n");
Ben Skeggs6b5a81a2011-11-10 15:42:55 +10001354 return NULL;
1355}
1356
Ben Skeggsb4c26812011-10-12 16:36:42 +10001357void *
Ben Skeggse0996ae2012-07-10 12:20:17 +10001358olddcb_outp(struct drm_device *dev, u8 idx)
Ben Skeggs6b5a81a2011-11-10 15:42:55 +10001359{
Ben Skeggse0996ae2012-07-10 12:20:17 +10001360 u8 *dcb = olddcb_table(dev);
Ben Skeggs6b5a81a2011-11-10 15:42:55 +10001361 if (dcb && dcb[0] >= 0x30) {
1362 if (idx < dcb[2])
1363 return dcb + dcb[1] + (idx * dcb[3]);
1364 } else
1365 if (dcb && dcb[0] >= 0x20) {
1366 u8 *i2c = ROMPTR(dev, dcb[2]);
1367 u8 *ent = dcb + 8 + (idx * 8);
1368 if (i2c && ent < i2c)
1369 return ent;
1370 } else
1371 if (dcb && dcb[0] >= 0x15) {
1372 u8 *i2c = ROMPTR(dev, dcb[2]);
1373 u8 *ent = dcb + 4 + (idx * 10);
1374 if (i2c && ent < i2c)
1375 return ent;
1376 }
1377
1378 return NULL;
1379}
1380
1381int
Ben Skeggse0996ae2012-07-10 12:20:17 +10001382olddcb_outp_foreach(struct drm_device *dev, void *data,
Ben Skeggs6b5a81a2011-11-10 15:42:55 +10001383 int (*exec)(struct drm_device *, void *, int idx, u8 *outp))
1384{
1385 int ret, idx = -1;
1386 u8 *outp = NULL;
Ben Skeggse0996ae2012-07-10 12:20:17 +10001387 while ((outp = olddcb_outp(dev, ++idx))) {
Ben Skeggs6b5a81a2011-11-10 15:42:55 +10001388 if (ROM32(outp[0]) == 0x00000000)
1389 break; /* seen on an NV11 with DCB v1.5 */
1390 if (ROM32(outp[0]) == 0xffffffff)
1391 break; /* seen on an NV17 with DCB v2.0 */
1392
Ben Skeggscb75d972012-07-11 10:44:20 +10001393 if ((outp[0] & 0x0f) == DCB_OUTPUT_UNUSED)
Ben Skeggs6b5a81a2011-11-10 15:42:55 +10001394 continue;
Ben Skeggscb75d972012-07-11 10:44:20 +10001395 if ((outp[0] & 0x0f) == DCB_OUTPUT_EOL)
Ben Skeggs6b5a81a2011-11-10 15:42:55 +10001396 break;
1397
1398 ret = exec(dev, data, idx, outp);
1399 if (ret)
1400 return ret;
1401 }
1402
1403 return 0;
1404}
1405
Ben Skeggsbefb51e2011-11-18 10:23:59 +10001406u8 *
Ben Skeggscb75d972012-07-11 10:44:20 +10001407olddcb_conntab(struct drm_device *dev)
Ben Skeggsbefb51e2011-11-18 10:23:59 +10001408{
Ben Skeggse0996ae2012-07-10 12:20:17 +10001409 u8 *dcb = olddcb_table(dev);
Ben Skeggsbefb51e2011-11-18 10:23:59 +10001410 if (dcb && dcb[0] >= 0x30 && dcb[1] >= 0x16) {
1411 u8 *conntab = ROMPTR(dev, dcb[0x14]);
1412 if (conntab && conntab[0] >= 0x30 && conntab[0] <= 0x40)
1413 return conntab;
1414 }
1415 return NULL;
1416}
1417
1418u8 *
Ben Skeggscb75d972012-07-11 10:44:20 +10001419olddcb_conn(struct drm_device *dev, u8 idx)
Ben Skeggsbefb51e2011-11-18 10:23:59 +10001420{
Ben Skeggscb75d972012-07-11 10:44:20 +10001421 u8 *conntab = olddcb_conntab(dev);
Ben Skeggsbefb51e2011-11-18 10:23:59 +10001422 if (conntab && idx < conntab[2])
1423 return conntab + conntab[1] + (idx * conntab[3]);
1424 return NULL;
1425}
1426
Ben Skeggscb75d972012-07-11 10:44:20 +10001427static struct dcb_output *new_dcb_entry(struct dcb_table *dcb)
Ben Skeggs6ee73862009-12-11 19:24:15 +10001428{
Ben Skeggscb75d972012-07-11 10:44:20 +10001429 struct dcb_output *entry = &dcb->entry[dcb->entries];
Ben Skeggs6ee73862009-12-11 19:24:15 +10001430
Ben Skeggscb75d972012-07-11 10:44:20 +10001431 memset(entry, 0, sizeof(struct dcb_output));
Ben Skeggs6ee73862009-12-11 19:24:15 +10001432 entry->index = dcb->entries++;
1433
1434 return entry;
1435}
1436
Francisco Jerez2e5702a2010-11-19 18:08:47 +10001437static void fabricate_dcb_output(struct dcb_table *dcb, int type, int i2c,
1438 int heads, int or)
Ben Skeggs6ee73862009-12-11 19:24:15 +10001439{
Ben Skeggscb75d972012-07-11 10:44:20 +10001440 struct dcb_output *entry = new_dcb_entry(dcb);
Ben Skeggs6ee73862009-12-11 19:24:15 +10001441
Francisco Jerez2e5702a2010-11-19 18:08:47 +10001442 entry->type = type;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001443 entry->i2c_index = i2c;
1444 entry->heads = heads;
Ben Skeggscb75d972012-07-11 10:44:20 +10001445 if (type != DCB_OUTPUT_ANALOG)
Francisco Jerez2e5702a2010-11-19 18:08:47 +10001446 entry->location = !DCB_LOC_ON_CHIP; /* ie OFF CHIP */
1447 entry->or = or;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001448}
1449
1450static bool
Ben Skeggs7f245b22010-02-24 09:56:18 +10001451parse_dcb20_entry(struct drm_device *dev, struct dcb_table *dcb,
Ben Skeggscb75d972012-07-11 10:44:20 +10001452 uint32_t conn, uint32_t conf, struct dcb_output *entry)
Ben Skeggs6ee73862009-12-11 19:24:15 +10001453{
Ben Skeggs77145f12012-07-31 16:16:21 +10001454 struct nouveau_drm *drm = nouveau_drm(dev);
1455
Ben Skeggs6ee73862009-12-11 19:24:15 +10001456 entry->type = conn & 0xf;
1457 entry->i2c_index = (conn >> 4) & 0xf;
1458 entry->heads = (conn >> 8) & 0xf;
Ben Skeggsbefb51e2011-11-18 10:23:59 +10001459 entry->connector = (conn >> 12) & 0xf;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001460 entry->bus = (conn >> 16) & 0xf;
1461 entry->location = (conn >> 20) & 0x3;
1462 entry->or = (conn >> 24) & 0xf;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001463
1464 switch (entry->type) {
Ben Skeggscb75d972012-07-11 10:44:20 +10001465 case DCB_OUTPUT_ANALOG:
Ben Skeggs6ee73862009-12-11 19:24:15 +10001466 /*
1467 * Although the rest of a CRT conf dword is usually
1468 * zeros, mac biosen have stuff there so we must mask
1469 */
Ben Skeggs7f245b22010-02-24 09:56:18 +10001470 entry->crtconf.maxfreq = (dcb->version < 0x30) ?
Ben Skeggs6ee73862009-12-11 19:24:15 +10001471 (conf & 0xffff) * 10 :
1472 (conf & 0xff) * 10000;
1473 break;
Ben Skeggscb75d972012-07-11 10:44:20 +10001474 case DCB_OUTPUT_LVDS:
Ben Skeggs6ee73862009-12-11 19:24:15 +10001475 {
1476 uint32_t mask;
1477 if (conf & 0x1)
1478 entry->lvdsconf.use_straps_for_mode = true;
Ben Skeggs7f245b22010-02-24 09:56:18 +10001479 if (dcb->version < 0x22) {
Ben Skeggs6ee73862009-12-11 19:24:15 +10001480 mask = ~0xd;
1481 /*
1482 * The laptop in bug 14567 lies and claims to not use
1483 * straps when it does, so assume all DCB 2.0 laptops
1484 * use straps, until a broken EDID using one is produced
1485 */
1486 entry->lvdsconf.use_straps_for_mode = true;
1487 /*
1488 * Both 0x4 and 0x8 show up in v2.0 tables; assume they
1489 * mean the same thing (probably wrong, but might work)
1490 */
1491 if (conf & 0x4 || conf & 0x8)
1492 entry->lvdsconf.use_power_scripts = true;
1493 } else {
Ben Skeggsa6ed76d2010-07-12 15:33:07 +10001494 mask = ~0x7;
1495 if (conf & 0x2)
1496 entry->lvdsconf.use_acpi_for_edid = true;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001497 if (conf & 0x4)
1498 entry->lvdsconf.use_power_scripts = true;
Ben Skeggsc5875472010-07-16 16:17:27 +10001499 entry->lvdsconf.sor.link = (conf & 0x00000030) >> 4;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001500 }
1501 if (conf & mask) {
1502 /*
1503 * Until we even try to use these on G8x, it's
1504 * useless reporting unknown bits. They all are.
1505 */
Ben Skeggs7f245b22010-02-24 09:56:18 +10001506 if (dcb->version >= 0x40)
Ben Skeggs6ee73862009-12-11 19:24:15 +10001507 break;
1508
Ben Skeggs77145f12012-07-31 16:16:21 +10001509 NV_ERROR(drm, "Unknown LVDS configuration bits, "
Ben Skeggs6ee73862009-12-11 19:24:15 +10001510 "please report\n");
1511 }
1512 break;
1513 }
Ben Skeggscb75d972012-07-11 10:44:20 +10001514 case DCB_OUTPUT_TV:
Ben Skeggs6ee73862009-12-11 19:24:15 +10001515 {
Ben Skeggs7f245b22010-02-24 09:56:18 +10001516 if (dcb->version >= 0x30)
Ben Skeggs6ee73862009-12-11 19:24:15 +10001517 entry->tvconf.has_component_output = conf & (0x8 << 4);
1518 else
1519 entry->tvconf.has_component_output = false;
1520
1521 break;
1522 }
Ben Skeggscb75d972012-07-11 10:44:20 +10001523 case DCB_OUTPUT_DP:
Ben Skeggs6ee73862009-12-11 19:24:15 +10001524 entry->dpconf.sor.link = (conf & 0x00000030) >> 4;
Ben Skeggs75a1fcc2011-08-04 09:55:44 +10001525 switch ((conf & 0x00e00000) >> 21) {
1526 case 0:
1527 entry->dpconf.link_bw = 162000;
1528 break;
1529 default:
1530 entry->dpconf.link_bw = 270000;
1531 break;
1532 }
Ben Skeggs6ee73862009-12-11 19:24:15 +10001533 switch ((conf & 0x0f000000) >> 24) {
1534 case 0xf:
1535 entry->dpconf.link_nr = 4;
1536 break;
1537 case 0x3:
1538 entry->dpconf.link_nr = 2;
1539 break;
1540 default:
1541 entry->dpconf.link_nr = 1;
1542 break;
1543 }
1544 break;
Ben Skeggscb75d972012-07-11 10:44:20 +10001545 case DCB_OUTPUT_TMDS:
Francisco Jerez27d50fc2010-08-08 17:09:06 +02001546 if (dcb->version >= 0x40)
1547 entry->tmdsconf.sor.link = (conf & 0x00000030) >> 4;
Francisco Jerez4a9f8222010-07-20 16:48:08 +02001548 else if (dcb->version >= 0x30)
1549 entry->tmdsconf.slave_addr = (conf & 0x00000700) >> 8;
Francisco Jerez27d50fc2010-08-08 17:09:06 +02001550 else if (dcb->version >= 0x22)
1551 entry->tmdsconf.slave_addr = (conf & 0x00000070) >> 4;
Francisco Jerez4a9f8222010-07-20 16:48:08 +02001552
Ben Skeggs6ee73862009-12-11 19:24:15 +10001553 break;
Ben Skeggscb75d972012-07-11 10:44:20 +10001554 case DCB_OUTPUT_EOL:
Ben Skeggs6ee73862009-12-11 19:24:15 +10001555 /* weird g80 mobile type that "nv" treats as a terminator */
Ben Skeggs7f245b22010-02-24 09:56:18 +10001556 dcb->entries--;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001557 return false;
Ben Skeggse7cc51c2010-02-24 10:31:39 +10001558 default:
1559 break;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001560 }
1561
Ben Skeggs23484872010-05-28 09:39:11 +10001562 if (dcb->version < 0x40) {
1563 /* Normal entries consist of a single bit, but dual link has
1564 * the next most significant bit set too
1565 */
1566 entry->duallink_possible =
1567 ((1 << (ffs(entry->or) - 1)) * 3 == entry->or);
1568 } else {
1569 entry->duallink_possible = (entry->sorconf.link == 3);
1570 }
1571
Ben Skeggs6ee73862009-12-11 19:24:15 +10001572 /* unsure what DCB version introduces this, 3.0? */
1573 if (conf & 0x100000)
1574 entry->i2c_upper_default = true;
1575
1576 return true;
1577}
1578
1579static bool
Ben Skeggs7f245b22010-02-24 09:56:18 +10001580parse_dcb15_entry(struct drm_device *dev, struct dcb_table *dcb,
Ben Skeggscb75d972012-07-11 10:44:20 +10001581 uint32_t conn, uint32_t conf, struct dcb_output *entry)
Ben Skeggs6ee73862009-12-11 19:24:15 +10001582{
Ben Skeggs77145f12012-07-31 16:16:21 +10001583 struct nouveau_drm *drm = nouveau_drm(dev);
1584
Ben Skeggsb0d2de82010-01-14 17:53:04 +10001585 switch (conn & 0x0000000f) {
1586 case 0:
Ben Skeggscb75d972012-07-11 10:44:20 +10001587 entry->type = DCB_OUTPUT_ANALOG;
Ben Skeggsb0d2de82010-01-14 17:53:04 +10001588 break;
1589 case 1:
Ben Skeggscb75d972012-07-11 10:44:20 +10001590 entry->type = DCB_OUTPUT_TV;
Ben Skeggsb0d2de82010-01-14 17:53:04 +10001591 break;
1592 case 2:
Francisco Jerezfba67522010-08-24 23:02:02 +02001593 case 4:
1594 if (conn & 0x10)
Ben Skeggscb75d972012-07-11 10:44:20 +10001595 entry->type = DCB_OUTPUT_LVDS;
Francisco Jerezfba67522010-08-24 23:02:02 +02001596 else
Ben Skeggscb75d972012-07-11 10:44:20 +10001597 entry->type = DCB_OUTPUT_TMDS;
Francisco Jerezfba67522010-08-24 23:02:02 +02001598 break;
Ben Skeggsb0d2de82010-01-14 17:53:04 +10001599 case 3:
Ben Skeggscb75d972012-07-11 10:44:20 +10001600 entry->type = DCB_OUTPUT_LVDS;
Ben Skeggsb0d2de82010-01-14 17:53:04 +10001601 break;
Ben Skeggsb0d2de82010-01-14 17:53:04 +10001602 default:
Ben Skeggs77145f12012-07-31 16:16:21 +10001603 NV_ERROR(drm, "Unknown DCB type %d\n", conn & 0x0000000f);
Ben Skeggs6ee73862009-12-11 19:24:15 +10001604 return false;
1605 }
Ben Skeggsb0d2de82010-01-14 17:53:04 +10001606
1607 entry->i2c_index = (conn & 0x0003c000) >> 14;
1608 entry->heads = ((conn & 0x001c0000) >> 18) + 1;
1609 entry->or = entry->heads; /* same as heads, hopefully safe enough */
1610 entry->location = (conn & 0x01e00000) >> 21;
1611 entry->bus = (conn & 0x0e000000) >> 25;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001612 entry->duallink_possible = false;
1613
1614 switch (entry->type) {
Ben Skeggscb75d972012-07-11 10:44:20 +10001615 case DCB_OUTPUT_ANALOG:
Ben Skeggs6ee73862009-12-11 19:24:15 +10001616 entry->crtconf.maxfreq = (conf & 0xffff) * 10;
1617 break;
Ben Skeggscb75d972012-07-11 10:44:20 +10001618 case DCB_OUTPUT_TV:
Ben Skeggsb0d2de82010-01-14 17:53:04 +10001619 entry->tvconf.has_component_output = false;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001620 break;
Ben Skeggscb75d972012-07-11 10:44:20 +10001621 case DCB_OUTPUT_LVDS:
Francisco Jerez77b1d5d2011-02-03 01:56:32 +01001622 if ((conn & 0x00003f00) >> 8 != 0x10)
Ben Skeggsb0d2de82010-01-14 17:53:04 +10001623 entry->lvdsconf.use_straps_for_mode = true;
1624 entry->lvdsconf.use_power_scripts = true;
1625 break;
1626 default:
Ben Skeggs6ee73862009-12-11 19:24:15 +10001627 break;
1628 }
1629
1630 return true;
1631}
1632
Ben Skeggs6ee73862009-12-11 19:24:15 +10001633static
Ben Skeggs7f245b22010-02-24 09:56:18 +10001634void merge_like_dcb_entries(struct drm_device *dev, struct dcb_table *dcb)
Ben Skeggs6ee73862009-12-11 19:24:15 +10001635{
1636 /*
1637 * DCB v2.0 lists each output combination separately.
1638 * Here we merge compatible entries to have fewer outputs, with
1639 * more options
1640 */
1641
Ben Skeggs77145f12012-07-31 16:16:21 +10001642 struct nouveau_drm *drm = nouveau_drm(dev);
Ben Skeggs6ee73862009-12-11 19:24:15 +10001643 int i, newentries = 0;
1644
1645 for (i = 0; i < dcb->entries; i++) {
Ben Skeggscb75d972012-07-11 10:44:20 +10001646 struct dcb_output *ient = &dcb->entry[i];
Ben Skeggs6ee73862009-12-11 19:24:15 +10001647 int j;
1648
1649 for (j = i + 1; j < dcb->entries; j++) {
Ben Skeggscb75d972012-07-11 10:44:20 +10001650 struct dcb_output *jent = &dcb->entry[j];
Ben Skeggs6ee73862009-12-11 19:24:15 +10001651
1652 if (jent->type == 100) /* already merged entry */
1653 continue;
1654
1655 /* merge heads field when all other fields the same */
1656 if (jent->i2c_index == ient->i2c_index &&
1657 jent->type == ient->type &&
1658 jent->location == ient->location &&
1659 jent->or == ient->or) {
Ben Skeggs77145f12012-07-31 16:16:21 +10001660 NV_INFO(drm, "Merging DCB entries %d and %d\n",
Ben Skeggs6ee73862009-12-11 19:24:15 +10001661 i, j);
1662 ient->heads |= jent->heads;
1663 jent->type = 100; /* dummy value */
1664 }
1665 }
1666 }
1667
1668 /* Compact entries merged into others out of dcb */
1669 for (i = 0; i < dcb->entries; i++) {
1670 if (dcb->entry[i].type == 100)
1671 continue;
1672
1673 if (newentries != i) {
1674 dcb->entry[newentries] = dcb->entry[i];
1675 dcb->entry[newentries].index = newentries;
1676 }
1677 newentries++;
1678 }
1679
1680 dcb->entries = newentries;
1681}
1682
Ben Skeggsdf4cf1b2010-07-01 11:31:45 +10001683static bool
1684apply_dcb_encoder_quirks(struct drm_device *dev, int idx, u32 *conn, u32 *conf)
1685{
Ben Skeggs77145f12012-07-31 16:16:21 +10001686 struct nouveau_drm *drm = nouveau_drm(dev);
1687 struct dcb_table *dcb = &drm->vbios.dcb;
Francisco Jerez670820c2010-12-25 15:43:30 +01001688
Ben Skeggsdf4cf1b2010-07-01 11:31:45 +10001689 /* Dell Precision M6300
1690 * DCB entry 2: 02025312 00000010
1691 * DCB entry 3: 02026312 00000020
1692 *
1693 * Identical, except apparently a different connector on a
1694 * different SOR link. Not a clue how we're supposed to know
1695 * which one is in use if it even shares an i2c line...
1696 *
1697 * Ignore the connector on the second SOR link to prevent
1698 * nasty problems until this is sorted (assuming it's not a
1699 * VBIOS bug).
1700 */
Francisco Jerezacae1162010-08-15 14:31:31 +02001701 if (nv_match_device(dev, 0x040d, 0x1028, 0x019b)) {
Ben Skeggsdf4cf1b2010-07-01 11:31:45 +10001702 if (*conn == 0x02026312 && *conf == 0x00000020)
1703 return false;
1704 }
1705
Francisco Jerez670820c2010-12-25 15:43:30 +01001706 /* GeForce3 Ti 200
1707 *
1708 * DCB reports an LVDS output that should be TMDS:
1709 * DCB entry 1: f2005014 ffffffff
1710 */
1711 if (nv_match_device(dev, 0x0201, 0x1462, 0x8851)) {
1712 if (*conn == 0xf2005014 && *conf == 0xffffffff) {
Ben Skeggscb75d972012-07-11 10:44:20 +10001713 fabricate_dcb_output(dcb, DCB_OUTPUT_TMDS, 1, 1, 1);
Francisco Jerez670820c2010-12-25 15:43:30 +01001714 return false;
1715 }
1716 }
1717
Ben Skeggsc0929b42011-03-21 11:42:51 +10001718 /* XFX GT-240X-YA
1719 *
1720 * So many things wrong here, replace the entire encoder table..
1721 */
1722 if (nv_match_device(dev, 0x0ca3, 0x1682, 0x3003)) {
1723 if (idx == 0) {
1724 *conn = 0x02001300; /* VGA, connector 1 */
1725 *conf = 0x00000028;
1726 } else
1727 if (idx == 1) {
1728 *conn = 0x01010312; /* DVI, connector 0 */
1729 *conf = 0x00020030;
1730 } else
1731 if (idx == 2) {
1732 *conn = 0x01010310; /* VGA, connector 0 */
1733 *conf = 0x00000028;
1734 } else
1735 if (idx == 3) {
1736 *conn = 0x02022362; /* HDMI, connector 2 */
1737 *conf = 0x00020010;
1738 } else {
1739 *conn = 0x0000000e; /* EOL */
1740 *conf = 0x00000000;
1741 }
1742 }
1743
Ben Skeggse540afc2011-05-30 12:53:37 +10001744 /* Some other twisted XFX board (rhbz#694914)
1745 *
1746 * The DVI/VGA encoder combo that's supposed to represent the
1747 * DVI-I connector actually point at two different ones, and
1748 * the HDMI connector ends up paired with the VGA instead.
1749 *
1750 * Connector table is missing anything for VGA at all, pointing it
1751 * an invalid conntab entry 2 so we figure it out ourself.
1752 */
1753 if (nv_match_device(dev, 0x0615, 0x1682, 0x2605)) {
1754 if (idx == 0) {
1755 *conn = 0x02002300; /* VGA, connector 2 */
1756 *conf = 0x00000028;
1757 } else
1758 if (idx == 1) {
1759 *conn = 0x01010312; /* DVI, connector 0 */
1760 *conf = 0x00020030;
1761 } else
1762 if (idx == 2) {
1763 *conn = 0x04020310; /* VGA, connector 0 */
1764 *conf = 0x00000028;
1765 } else
1766 if (idx == 3) {
1767 *conn = 0x02021322; /* HDMI, connector 1 */
1768 *conf = 0x00020010;
1769 } else {
1770 *conn = 0x0000000e; /* EOL */
1771 *conf = 0x00000000;
1772 }
1773 }
1774
Ben Skeggs16fde6c2012-06-08 10:31:46 +10001775 /* fdo#50830: connector indices for VGA and DVI-I are backwards */
1776 if (nv_match_device(dev, 0x0421, 0x3842, 0xc793)) {
1777 if (idx == 0 && *conn == 0x02000300)
1778 *conn = 0x02011300;
1779 else
1780 if (idx == 1 && *conn == 0x04011310)
1781 *conn = 0x04000310;
1782 else
1783 if (idx == 2 && *conn == 0x02011312)
1784 *conn = 0x02000312;
1785 }
1786
Ben Skeggsdf4cf1b2010-07-01 11:31:45 +10001787 return true;
1788}
1789
Francisco Jerez2e5702a2010-11-19 18:08:47 +10001790static void
1791fabricate_dcb_encoder_table(struct drm_device *dev, struct nvbios *bios)
1792{
1793 struct dcb_table *dcb = &bios->dcb;
1794 int all_heads = (nv_two_heads(dev) ? 3 : 1);
1795
1796#ifdef __powerpc__
1797 /* Apple iMac G4 NV17 */
1798 if (of_machine_is_compatible("PowerMac4,5")) {
Ben Skeggscb75d972012-07-11 10:44:20 +10001799 fabricate_dcb_output(dcb, DCB_OUTPUT_TMDS, 0, all_heads, 1);
1800 fabricate_dcb_output(dcb, DCB_OUTPUT_ANALOG, 1, all_heads, 2);
Francisco Jerez2e5702a2010-11-19 18:08:47 +10001801 return;
1802 }
1803#endif
1804
1805 /* Make up some sane defaults */
Ben Skeggscb75d972012-07-11 10:44:20 +10001806 fabricate_dcb_output(dcb, DCB_OUTPUT_ANALOG,
Ben Skeggs0f8067c2011-11-10 14:49:19 +10001807 bios->legacy.i2c_indices.crt, 1, 1);
Francisco Jerez2e5702a2010-11-19 18:08:47 +10001808
1809 if (nv04_tv_identify(dev, bios->legacy.i2c_indices.tv) >= 0)
Ben Skeggscb75d972012-07-11 10:44:20 +10001810 fabricate_dcb_output(dcb, DCB_OUTPUT_TV,
Ben Skeggs0f8067c2011-11-10 14:49:19 +10001811 bios->legacy.i2c_indices.tv,
Francisco Jerez2e5702a2010-11-19 18:08:47 +10001812 all_heads, 0);
1813
1814 else if (bios->tmds.output0_script_ptr ||
1815 bios->tmds.output1_script_ptr)
Ben Skeggscb75d972012-07-11 10:44:20 +10001816 fabricate_dcb_output(dcb, DCB_OUTPUT_TMDS,
Ben Skeggs0f8067c2011-11-10 14:49:19 +10001817 bios->legacy.i2c_indices.panel,
Francisco Jerez2e5702a2010-11-19 18:08:47 +10001818 all_heads, 1);
1819}
1820
Ben Skeggsed42f822010-01-14 15:58:10 +10001821static int
Ben Skeggs6b5a81a2011-11-10 15:42:55 +10001822parse_dcb_entry(struct drm_device *dev, void *data, int idx, u8 *outp)
Ben Skeggs6ee73862009-12-11 19:24:15 +10001823{
Ben Skeggs77145f12012-07-31 16:16:21 +10001824 struct nouveau_drm *drm = nouveau_drm(dev);
1825 struct dcb_table *dcb = &drm->vbios.dcb;
Ben Skeggs6b5a81a2011-11-10 15:42:55 +10001826 u32 conf = (dcb->version >= 0x20) ? ROM32(outp[4]) : ROM32(outp[6]);
1827 u32 conn = ROM32(outp[0]);
1828 bool ret;
1829
1830 if (apply_dcb_encoder_quirks(dev, idx, &conn, &conf)) {
Ben Skeggscb75d972012-07-11 10:44:20 +10001831 struct dcb_output *entry = new_dcb_entry(dcb);
Ben Skeggs6b5a81a2011-11-10 15:42:55 +10001832
Ben Skeggs77145f12012-07-31 16:16:21 +10001833 NV_INFO(drm, "DCB outp %02d: %08x %08x\n", idx, conn, conf);
Ben Skeggs6b5a81a2011-11-10 15:42:55 +10001834
1835 if (dcb->version >= 0x20)
1836 ret = parse_dcb20_entry(dev, dcb, conn, conf, entry);
1837 else
1838 ret = parse_dcb15_entry(dev, dcb, conn, conf, entry);
1839 if (!ret)
1840 return 1; /* stop parsing */
Ben Skeggsbefb51e2011-11-18 10:23:59 +10001841
1842 /* Ignore the I2C index for on-chip TV-out, as there
1843 * are cards with bogus values (nv31m in bug 23212),
1844 * and it's otherwise useless.
1845 */
Ben Skeggscb75d972012-07-11 10:44:20 +10001846 if (entry->type == DCB_OUTPUT_TV &&
Ben Skeggsbefb51e2011-11-18 10:23:59 +10001847 entry->location == DCB_LOC_ON_CHIP)
1848 entry->i2c_index = 0x0f;
Ben Skeggs6b5a81a2011-11-10 15:42:55 +10001849 }
1850
1851 return 0;
1852}
1853
Ben Skeggsbefb51e2011-11-18 10:23:59 +10001854static void
1855dcb_fake_connectors(struct nvbios *bios)
1856{
1857 struct dcb_table *dcbt = &bios->dcb;
1858 u8 map[16] = { };
1859 int i, idx = 0;
1860
1861 /* heuristic: if we ever get a non-zero connector field, assume
1862 * that all the indices are valid and we don't need fake them.
Ben Skeggs5206b522012-04-13 19:22:30 +10001863 *
1864 * and, as usual, a blacklist of boards with bad bios data..
Ben Skeggsbefb51e2011-11-18 10:23:59 +10001865 */
Ben Skeggs5206b522012-04-13 19:22:30 +10001866 if (!nv_match_device(bios->dev, 0x0392, 0x107d, 0x20a2)) {
1867 for (i = 0; i < dcbt->entries; i++) {
1868 if (dcbt->entry[i].connector)
1869 return;
1870 }
Ben Skeggsbefb51e2011-11-18 10:23:59 +10001871 }
1872
1873 /* no useful connector info available, we need to make it up
1874 * ourselves. the rule here is: anything on the same i2c bus
1875 * is considered to be on the same connector. any output
1876 * without an associated i2c bus is assigned its own unique
1877 * connector index.
1878 */
1879 for (i = 0; i < dcbt->entries; i++) {
1880 u8 i2c = dcbt->entry[i].i2c_index;
1881 if (i2c == 0x0f) {
1882 dcbt->entry[i].connector = idx++;
1883 } else {
1884 if (!map[i2c])
1885 map[i2c] = ++idx;
1886 dcbt->entry[i].connector = map[i2c] - 1;
1887 }
1888 }
1889
1890 /* if we created more than one connector, destroy the connector
1891 * table - just in case it has random, rather than stub, entries.
1892 */
1893 if (i > 1) {
Ben Skeggscb75d972012-07-11 10:44:20 +10001894 u8 *conntab = olddcb_conntab(bios->dev);
Ben Skeggsbefb51e2011-11-18 10:23:59 +10001895 if (conntab)
1896 conntab[0] = 0x00;
1897 }
1898}
1899
Ben Skeggs6b5a81a2011-11-10 15:42:55 +10001900static int
1901parse_dcb_table(struct drm_device *dev, struct nvbios *bios)
1902{
Ben Skeggs77145f12012-07-31 16:16:21 +10001903 struct nouveau_drm *drm = nouveau_drm(dev);
Ben Skeggs7f245b22010-02-24 09:56:18 +10001904 struct dcb_table *dcb = &bios->dcb;
Ben Skeggsbefb51e2011-11-18 10:23:59 +10001905 u8 *dcbt, *conn;
1906 int idx;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001907
Ben Skeggse0996ae2012-07-10 12:20:17 +10001908 dcbt = olddcb_table(dev);
Ben Skeggs6b5a81a2011-11-10 15:42:55 +10001909 if (!dcbt) {
1910 /* handle pre-DCB boards */
1911 if (bios->type == NVBIOS_BMP) {
1912 fabricate_dcb_encoder_table(dev, bios);
1913 return 0;
1914 }
1915
1916 return -EINVAL;
Ben Skeggsed42f822010-01-14 15:58:10 +10001917 }
Ben Skeggs6ee73862009-12-11 19:24:15 +10001918
Ben Skeggs77145f12012-07-31 16:16:21 +10001919 NV_INFO(drm, "DCB version %d.%d\n", dcbt[0] >> 4, dcbt[0] & 0xf);
Ben Skeggs6ee73862009-12-11 19:24:15 +10001920
Ben Skeggs6b5a81a2011-11-10 15:42:55 +10001921 dcb->version = dcbt[0];
Ben Skeggse0996ae2012-07-10 12:20:17 +10001922 olddcb_outp_foreach(dev, NULL, parse_dcb_entry);
Ben Skeggs6ee73862009-12-11 19:24:15 +10001923
1924 /*
1925 * apart for v2.1+ not being known for requiring merging, this
1926 * guarantees dcbent->index is the index of the entry in the rom image
1927 */
Ben Skeggs7f245b22010-02-24 09:56:18 +10001928 if (dcb->version < 0x21)
Ben Skeggs6ee73862009-12-11 19:24:15 +10001929 merge_like_dcb_entries(dev, dcb);
1930
Ben Skeggs54abb5d2010-02-24 10:48:16 +10001931 if (!dcb->entries)
1932 return -ENXIO;
1933
Ben Skeggsbefb51e2011-11-18 10:23:59 +10001934 /* dump connector table entries to log, if any exist */
1935 idx = -1;
Ben Skeggscb75d972012-07-11 10:44:20 +10001936 while ((conn = olddcb_conn(dev, ++idx))) {
Ben Skeggsbefb51e2011-11-18 10:23:59 +10001937 if (conn[0] != 0xff) {
Ben Skeggs77145f12012-07-31 16:16:21 +10001938 NV_INFO(drm, "DCB conn %02d: ", idx);
Ben Skeggscb75d972012-07-11 10:44:20 +10001939 if (olddcb_conntab(dev)[3] < 4)
Ben Skeggsbefb51e2011-11-18 10:23:59 +10001940 printk("%04x\n", ROM16(conn[0]));
1941 else
1942 printk("%08x\n", ROM32(conn[0]));
Ben Skeggs6ee73862009-12-11 19:24:15 +10001943 }
Ben Skeggs6ee73862009-12-11 19:24:15 +10001944 }
Ben Skeggsbefb51e2011-11-18 10:23:59 +10001945 dcb_fake_connectors(bios);
Ben Skeggsbefb51e2011-11-18 10:23:59 +10001946 return 0;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001947}
1948
Ben Skeggs6ee73862009-12-11 19:24:15 +10001949static int load_nv17_hwsq_ucode_entry(struct drm_device *dev, struct nvbios *bios, uint16_t hwsq_offset, int entry)
1950{
1951 /*
1952 * The header following the "HWSQ" signature has the number of entries,
1953 * and the entry size
1954 *
1955 * An entry consists of a dword to write to the sequencer control reg
1956 * (0x00001304), followed by the ucode bytes, written sequentially,
1957 * starting at reg 0x00001400
1958 */
1959
Ben Skeggs77145f12012-07-31 16:16:21 +10001960 struct nouveau_drm *drm = nouveau_drm(dev);
1961 struct nouveau_device *device = nv_device(drm->device);
Ben Skeggs6ee73862009-12-11 19:24:15 +10001962 uint8_t bytes_to_write;
1963 uint16_t hwsq_entry_offset;
1964 int i;
1965
1966 if (bios->data[hwsq_offset] <= entry) {
Ben Skeggs77145f12012-07-31 16:16:21 +10001967 NV_ERROR(drm, "Too few entries in HW sequencer table for "
Ben Skeggs6ee73862009-12-11 19:24:15 +10001968 "requested entry\n");
1969 return -ENOENT;
1970 }
1971
1972 bytes_to_write = bios->data[hwsq_offset + 1];
1973
1974 if (bytes_to_write != 36) {
Ben Skeggs77145f12012-07-31 16:16:21 +10001975 NV_ERROR(drm, "Unknown HW sequencer entry size\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +10001976 return -EINVAL;
1977 }
1978
Ben Skeggs77145f12012-07-31 16:16:21 +10001979 NV_INFO(drm, "Loading NV17 power sequencing microcode\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +10001980
1981 hwsq_entry_offset = hwsq_offset + 2 + entry * bytes_to_write;
1982
1983 /* set sequencer control */
Ben Skeggs77145f12012-07-31 16:16:21 +10001984 nv_wr32(device, 0x00001304, ROM32(bios->data[hwsq_entry_offset]));
Ben Skeggs6ee73862009-12-11 19:24:15 +10001985 bytes_to_write -= 4;
1986
1987 /* write ucode */
1988 for (i = 0; i < bytes_to_write; i += 4)
Ben Skeggs77145f12012-07-31 16:16:21 +10001989 nv_wr32(device, 0x00001400 + i, ROM32(bios->data[hwsq_entry_offset + i + 4]));
Ben Skeggs6ee73862009-12-11 19:24:15 +10001990
1991 /* twiddle NV_PBUS_DEBUG_4 */
Ben Skeggs77145f12012-07-31 16:16:21 +10001992 nv_wr32(device, NV_PBUS_DEBUG_4, nv_rd32(device, NV_PBUS_DEBUG_4) | 0x18);
Ben Skeggs6ee73862009-12-11 19:24:15 +10001993
1994 return 0;
1995}
1996
1997static int load_nv17_hw_sequencer_ucode(struct drm_device *dev,
1998 struct nvbios *bios)
1999{
2000 /*
2001 * BMP based cards, from NV17, need a microcode loading to correctly
2002 * control the GPIO etc for LVDS panels
2003 *
2004 * BIT based cards seem to do this directly in the init scripts
2005 *
2006 * The microcode entries are found by the "HWSQ" signature.
2007 */
2008
2009 const uint8_t hwsq_signature[] = { 'H', 'W', 'S', 'Q' };
2010 const int sz = sizeof(hwsq_signature);
2011 int hwsq_offset;
2012
2013 hwsq_offset = findstr(bios->data, bios->length, hwsq_signature, sz);
2014 if (!hwsq_offset)
2015 return 0;
2016
2017 /* always use entry 0? */
2018 return load_nv17_hwsq_ucode_entry(dev, bios, hwsq_offset + sz, 0);
2019}
2020
2021uint8_t *nouveau_bios_embedded_edid(struct drm_device *dev)
2022{
Ben Skeggs77145f12012-07-31 16:16:21 +10002023 struct nouveau_drm *drm = nouveau_drm(dev);
2024 struct nvbios *bios = &drm->vbios;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002025 const uint8_t edid_sig[] = {
2026 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00 };
2027 uint16_t offset = 0;
2028 uint16_t newoffset;
2029 int searchlen = NV_PROM_SIZE;
2030
2031 if (bios->fp.edid)
2032 return bios->fp.edid;
2033
2034 while (searchlen) {
2035 newoffset = findstr(&bios->data[offset], searchlen,
2036 edid_sig, 8);
2037 if (!newoffset)
2038 return NULL;
2039 offset += newoffset;
2040 if (!nv_cksum(&bios->data[offset], EDID1_LEN))
2041 break;
2042
2043 searchlen -= offset;
2044 offset++;
2045 }
2046
Ben Skeggs77145f12012-07-31 16:16:21 +10002047 NV_INFO(drm, "Found EDID in BIOS\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +10002048
2049 return bios->fp.edid = &bios->data[offset];
2050}
2051
Ben Skeggs6ee73862009-12-11 19:24:15 +10002052static bool NVInitVBIOS(struct drm_device *dev)
2053{
Ben Skeggs77145f12012-07-31 16:16:21 +10002054 struct nouveau_drm *drm = nouveau_drm(dev);
2055 struct nvbios *bios = &drm->vbios;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002056
2057 memset(bios, 0, sizeof(struct nvbios));
Ben Skeggsc7ca4d12011-02-03 20:10:49 +10002058 spin_lock_init(&bios->lock);
Ben Skeggs6ee73862009-12-11 19:24:15 +10002059 bios->dev = dev;
2060
Ben Skeggs77145f12012-07-31 16:16:21 +10002061 bios->data = nouveau_bios(drm->device)->data;
2062 bios->length = nouveau_bios(drm->device)->size;
2063 return true;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002064}
2065
2066static int nouveau_parse_vbios_struct(struct drm_device *dev)
2067{
Ben Skeggs77145f12012-07-31 16:16:21 +10002068 struct nouveau_drm *drm = nouveau_drm(dev);
2069 struct nvbios *bios = &drm->vbios;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002070 const uint8_t bit_signature[] = { 0xff, 0xb8, 'B', 'I', 'T' };
2071 const uint8_t bmp_signature[] = { 0xff, 0x7f, 'N', 'V', 0x0 };
2072 int offset;
2073
2074 offset = findstr(bios->data, bios->length,
2075 bit_signature, sizeof(bit_signature));
2076 if (offset) {
Ben Skeggs77145f12012-07-31 16:16:21 +10002077 NV_INFO(drm, "BIT BIOS found\n");
Ben Skeggs4709bff2010-09-13 15:18:40 +10002078 bios->type = NVBIOS_BIT;
2079 bios->offset = offset;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002080 return parse_bit_structure(bios, offset + 6);
2081 }
2082
2083 offset = findstr(bios->data, bios->length,
2084 bmp_signature, sizeof(bmp_signature));
2085 if (offset) {
Ben Skeggs77145f12012-07-31 16:16:21 +10002086 NV_INFO(drm, "BMP BIOS found\n");
Ben Skeggs4709bff2010-09-13 15:18:40 +10002087 bios->type = NVBIOS_BMP;
2088 bios->offset = offset;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002089 return parse_bmp_structure(dev, bios, offset);
2090 }
2091
Ben Skeggs77145f12012-07-31 16:16:21 +10002092 NV_ERROR(drm, "No known BIOS signature found\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +10002093 return -ENODEV;
2094}
2095
2096int
2097nouveau_run_vbios_init(struct drm_device *dev)
2098{
Ben Skeggs77145f12012-07-31 16:16:21 +10002099 struct nouveau_drm *drm = nouveau_drm(dev);
2100 struct nvbios *bios = &drm->vbios;
Ben Skeggsb6e4ad22012-11-12 09:55:16 +10002101 int ret = 0;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002102
Francisco Jerez946fd352010-07-24 17:41:48 +02002103 /* Reset the BIOS head to 0. */
2104 bios->state.crtchead = 0;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002105
2106 if (bios->major_version < 5) /* BMP only */
2107 load_nv17_hw_sequencer_ucode(dev, bios);
2108
2109 if (bios->execute) {
2110 bios->fp.last_script_invoc = 0;
2111 bios->fp.lvds_init_run = false;
2112 }
2113
Ben Skeggs6ee73862009-12-11 19:24:15 +10002114 return ret;
2115}
2116
Ben Skeggsd13102c2010-05-25 13:47:16 +10002117static bool
2118nouveau_bios_posted(struct drm_device *dev)
2119{
Ben Skeggs77145f12012-07-31 16:16:21 +10002120 struct nouveau_drm *drm = nouveau_drm(dev);
Ben Skeggsd13102c2010-05-25 13:47:16 +10002121 unsigned htotal;
2122
Ben Skeggs77145f12012-07-31 16:16:21 +10002123 if (nv_device(drm->device)->card_type >= NV_50) {
Ben Skeggsd13102c2010-05-25 13:47:16 +10002124 if (NVReadVgaCrtc(dev, 0, 0x00) == 0 &&
2125 NVReadVgaCrtc(dev, 0, 0x1a) == 0)
2126 return false;
2127 return true;
2128 }
2129
Ben Skeggsd13102c2010-05-25 13:47:16 +10002130 htotal = NVReadVgaCrtc(dev, 0, 0x06);
2131 htotal |= (NVReadVgaCrtc(dev, 0, 0x07) & 0x01) << 8;
2132 htotal |= (NVReadVgaCrtc(dev, 0, 0x07) & 0x20) << 4;
2133 htotal |= (NVReadVgaCrtc(dev, 0, 0x25) & 0x01) << 10;
2134 htotal |= (NVReadVgaCrtc(dev, 0, 0x41) & 0x01) << 11;
Francisco Jerez03cd06c2010-07-20 03:08:25 +02002135
Ben Skeggsd13102c2010-05-25 13:47:16 +10002136 return (htotal != 0);
2137}
2138
Ben Skeggs6ee73862009-12-11 19:24:15 +10002139int
2140nouveau_bios_init(struct drm_device *dev)
2141{
Ben Skeggs77145f12012-07-31 16:16:21 +10002142 struct nouveau_drm *drm = nouveau_drm(dev);
2143 struct nvbios *bios = &drm->vbios;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002144 int ret;
2145
Ben Skeggs6ee73862009-12-11 19:24:15 +10002146 if (!NVInitVBIOS(dev))
2147 return -ENODEV;
2148
2149 ret = nouveau_parse_vbios_struct(dev);
2150 if (ret)
2151 return ret;
2152
Francisco Jerez2e5702a2010-11-19 18:08:47 +10002153 ret = parse_dcb_table(dev, bios);
Ben Skeggs6ee73862009-12-11 19:24:15 +10002154 if (ret)
2155 return ret;
2156
Ben Skeggs6ee73862009-12-11 19:24:15 +10002157 if (!bios->major_version) /* we don't run version 0 bios */
2158 return 0;
2159
Ben Skeggs6ee73862009-12-11 19:24:15 +10002160 /* init script execution disabled */
2161 bios->execute = false;
2162
2163 /* ... unless card isn't POSTed already */
Ben Skeggsd13102c2010-05-25 13:47:16 +10002164 if (!nouveau_bios_posted(dev)) {
Ben Skeggs77145f12012-07-31 16:16:21 +10002165 NV_INFO(drm, "Adaptor not initialised, "
Francisco Jerez67eda202010-07-13 15:59:50 +02002166 "running VBIOS init tables.\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +10002167 bios->execute = true;
2168 }
2169
Ben Skeggs6ee73862009-12-11 19:24:15 +10002170 ret = nouveau_run_vbios_init(dev);
Ben Skeggs04a39c52010-02-24 10:03:05 +10002171 if (ret)
Ben Skeggs6ee73862009-12-11 19:24:15 +10002172 return ret;
Ben Skeggs6ee73862009-12-11 19:24:15 +10002173
2174 /* feature_byte on BMP is poor, but init always sets CR4B */
Ben Skeggs6ee73862009-12-11 19:24:15 +10002175 if (bios->major_version < 5)
2176 bios->is_mobile = NVReadVgaCrtc(dev, 0, NV_CIO_CRE_4B) & 0x40;
2177
2178 /* all BIT systems need p_f_m_t for digital_min_front_porch */
2179 if (bios->is_mobile || bios->major_version >= 5)
2180 ret = parse_fp_mode_table(dev, bios);
Ben Skeggs6ee73862009-12-11 19:24:15 +10002181
2182 /* allow subsequent scripts to execute */
2183 bios->execute = true;
2184
2185 return 0;
2186}
2187
2188void
2189nouveau_bios_takedown(struct drm_device *dev)
2190{
Ben Skeggs6ee73862009-12-11 19:24:15 +10002191}