xref: /linux/drivers/gpu/drm/gma500/intel_bios.c (revision f6e8dc9edf963dbc99085e54f6ced6da9daa6100)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2006 Intel Corporation
4  *
5  * Authors:
6  *    Eric Anholt <eric@anholt.net>
7  */
8 
9 #include <drm/display/drm_dp_helper.h>
10 #include <drm/drm.h>
11 #include <drm/drm_print.h>
12 
13 #include "intel_bios.h"
14 #include "psb_drv.h"
15 #include "psb_intel_drv.h"
16 #include "psb_intel_reg.h"
17 
18 #define	TARGET_ADDR1	0x70
19 #define	TARGET_ADDR2	0x72
20 
21 static void *find_section(struct bdb_header *bdb, int section_id)
22 {
23 	u8 *base = (u8 *)bdb;
24 	int index = 0;
25 	u16 total, current_size;
26 	u8 current_id;
27 
28 	/* skip to first section */
29 	index += bdb->header_size;
30 	total = bdb->bdb_size;
31 
32 	/* walk the sections looking for section_id */
33 	while (index < total) {
34 		current_id = *(base + index);
35 		index++;
36 		current_size = *((u16 *)(base + index));
37 		index += 2;
38 		if (current_id == section_id)
39 			return base + index;
40 		index += current_size;
41 	}
42 
43 	return NULL;
44 }
45 
46 static void
47 parse_edp(struct drm_psb_private *dev_priv, struct bdb_header *bdb)
48 {
49 	struct bdb_edp *edp;
50 	struct edp_power_seq *edp_pps;
51 	struct edp_link_params *edp_link_params;
52 	uint8_t	panel_type;
53 
54 	edp = find_section(bdb, BDB_EDP);
55 
56 	dev_priv->edp.bpp = 18;
57 	if (!edp) {
58 		if (dev_priv->edp.support) {
59 			DRM_DEBUG_KMS("No eDP BDB found but eDP panel supported, assume %dbpp panel color depth.\n",
60 				      dev_priv->edp.bpp);
61 		}
62 		return;
63 	}
64 
65 	panel_type = dev_priv->panel_type;
66 	switch ((edp->color_depth >> (panel_type * 2)) & 3) {
67 	case EDP_18BPP:
68 		dev_priv->edp.bpp = 18;
69 		break;
70 	case EDP_24BPP:
71 		dev_priv->edp.bpp = 24;
72 		break;
73 	case EDP_30BPP:
74 		dev_priv->edp.bpp = 30;
75 		break;
76 	}
77 
78 	/* Get the eDP sequencing and link info */
79 	edp_pps = &edp->power_seqs[panel_type];
80 	edp_link_params = &edp->link_params[panel_type];
81 
82 	dev_priv->edp.pps = *edp_pps;
83 
84 	DRM_DEBUG_KMS("EDP timing in vbt t1_t3 %d t8 %d t9 %d t10 %d t11_t12 %d\n",
85 				dev_priv->edp.pps.t1_t3, dev_priv->edp.pps.t8,
86 				dev_priv->edp.pps.t9, dev_priv->edp.pps.t10,
87 				dev_priv->edp.pps.t11_t12);
88 
89 	dev_priv->edp.rate = edp_link_params->rate ? DP_LINK_BW_2_7 :
90 		DP_LINK_BW_1_62;
91 	switch (edp_link_params->lanes) {
92 	case 0:
93 		dev_priv->edp.lanes = 1;
94 		break;
95 	case 1:
96 		dev_priv->edp.lanes = 2;
97 		break;
98 	case 3:
99 	default:
100 		dev_priv->edp.lanes = 4;
101 		break;
102 	}
103 	DRM_DEBUG_KMS("VBT reports EDP: Lane_count %d, Lane_rate %d, Bpp %d\n",
104 			dev_priv->edp.lanes, dev_priv->edp.rate, dev_priv->edp.bpp);
105 
106 	switch (edp_link_params->preemphasis) {
107 	case 0:
108 		dev_priv->edp.preemphasis = DP_TRAIN_PRE_EMPH_LEVEL_0;
109 		break;
110 	case 1:
111 		dev_priv->edp.preemphasis = DP_TRAIN_PRE_EMPH_LEVEL_1;
112 		break;
113 	case 2:
114 		dev_priv->edp.preemphasis = DP_TRAIN_PRE_EMPH_LEVEL_2;
115 		break;
116 	case 3:
117 		dev_priv->edp.preemphasis = DP_TRAIN_PRE_EMPH_LEVEL_3;
118 		break;
119 	}
120 	switch (edp_link_params->vswing) {
121 	case 0:
122 		dev_priv->edp.vswing = DP_TRAIN_VOLTAGE_SWING_LEVEL_0;
123 		break;
124 	case 1:
125 		dev_priv->edp.vswing = DP_TRAIN_VOLTAGE_SWING_LEVEL_1;
126 		break;
127 	case 2:
128 		dev_priv->edp.vswing = DP_TRAIN_VOLTAGE_SWING_LEVEL_2;
129 		break;
130 	case 3:
131 		dev_priv->edp.vswing = DP_TRAIN_VOLTAGE_SWING_LEVEL_3;
132 		break;
133 	}
134 	DRM_DEBUG_KMS("VBT reports EDP: VSwing  %d, Preemph %d\n",
135 			dev_priv->edp.vswing, dev_priv->edp.preemphasis);
136 }
137 
138 static u16
139 get_blocksize(void *p)
140 {
141 	u16 *block_ptr, block_size;
142 
143 	block_ptr = (u16 *)((char *)p - 2);
144 	block_size = *block_ptr;
145 	return block_size;
146 }
147 
148 static void fill_detail_timing_data(struct drm_display_mode *panel_fixed_mode,
149 			struct lvds_dvo_timing *dvo_timing)
150 {
151 	panel_fixed_mode->hdisplay = (dvo_timing->hactive_hi << 8) |
152 		dvo_timing->hactive_lo;
153 	panel_fixed_mode->hsync_start = panel_fixed_mode->hdisplay +
154 		((dvo_timing->hsync_off_hi << 8) | dvo_timing->hsync_off_lo);
155 	panel_fixed_mode->hsync_end = panel_fixed_mode->hsync_start +
156 		dvo_timing->hsync_pulse_width;
157 	panel_fixed_mode->htotal = panel_fixed_mode->hdisplay +
158 		((dvo_timing->hblank_hi << 8) | dvo_timing->hblank_lo);
159 
160 	panel_fixed_mode->vdisplay = (dvo_timing->vactive_hi << 8) |
161 		dvo_timing->vactive_lo;
162 	panel_fixed_mode->vsync_start = panel_fixed_mode->vdisplay +
163 		dvo_timing->vsync_off;
164 	panel_fixed_mode->vsync_end = panel_fixed_mode->vsync_start +
165 		dvo_timing->vsync_pulse_width;
166 	panel_fixed_mode->vtotal = panel_fixed_mode->vdisplay +
167 		((dvo_timing->vblank_hi << 8) | dvo_timing->vblank_lo);
168 	panel_fixed_mode->clock = dvo_timing->clock * 10;
169 	panel_fixed_mode->type = DRM_MODE_TYPE_PREFERRED;
170 
171 	if (dvo_timing->hsync_positive)
172 		panel_fixed_mode->flags |= DRM_MODE_FLAG_PHSYNC;
173 	else
174 		panel_fixed_mode->flags |= DRM_MODE_FLAG_NHSYNC;
175 
176 	if (dvo_timing->vsync_positive)
177 		panel_fixed_mode->flags |= DRM_MODE_FLAG_PVSYNC;
178 	else
179 		panel_fixed_mode->flags |= DRM_MODE_FLAG_NVSYNC;
180 
181 	/* Some VBTs have bogus h/vtotal values */
182 	if (panel_fixed_mode->hsync_end > panel_fixed_mode->htotal)
183 		panel_fixed_mode->htotal = panel_fixed_mode->hsync_end + 1;
184 	if (panel_fixed_mode->vsync_end > panel_fixed_mode->vtotal)
185 		panel_fixed_mode->vtotal = panel_fixed_mode->vsync_end + 1;
186 
187 	drm_mode_set_name(panel_fixed_mode);
188 }
189 
190 static void parse_backlight_data(struct drm_psb_private *dev_priv,
191 				struct bdb_header *bdb)
192 {
193 	struct bdb_lvds_backlight *vbt_lvds_bl = NULL;
194 	struct bdb_lvds_backlight *lvds_bl;
195 	u8 p_type = 0;
196 	void *bl_start = NULL;
197 	struct bdb_lvds_options *lvds_opts
198 				= find_section(bdb, BDB_LVDS_OPTIONS);
199 
200 	dev_priv->lvds_bl = NULL;
201 
202 	if (lvds_opts)
203 		p_type = lvds_opts->panel_type;
204 	else
205 		return;
206 
207 	bl_start = find_section(bdb, BDB_LVDS_BACKLIGHT);
208 	vbt_lvds_bl = (struct bdb_lvds_backlight *)(bl_start + 1) + p_type;
209 
210 	lvds_bl = kmemdup(vbt_lvds_bl, sizeof(*vbt_lvds_bl), GFP_KERNEL);
211 	if (!lvds_bl) {
212 		dev_err(dev_priv->dev.dev, "out of memory for backlight data\n");
213 		return;
214 	}
215 	dev_priv->lvds_bl = lvds_bl;
216 }
217 
218 /* Try to find integrated panel data */
219 static void parse_lfp_panel_data(struct drm_psb_private *dev_priv,
220 			    struct bdb_header *bdb)
221 {
222 	struct bdb_lvds_options *lvds_options;
223 	struct bdb_lvds_lfp_data *lvds_lfp_data;
224 	struct bdb_lvds_lfp_data_entry *entry;
225 	struct lvds_dvo_timing *dvo_timing;
226 	struct drm_display_mode *panel_fixed_mode;
227 
228 	/* Defaults if we can't find VBT info */
229 	dev_priv->lvds_dither = 0;
230 	dev_priv->lvds_vbt = 0;
231 
232 	lvds_options = find_section(bdb, BDB_LVDS_OPTIONS);
233 	if (!lvds_options)
234 		return;
235 
236 	dev_priv->lvds_dither = lvds_options->pixel_dither;
237 	dev_priv->panel_type = lvds_options->panel_type;
238 
239 	if (lvds_options->panel_type == 0xff)
240 		return;
241 
242 	lvds_lfp_data = find_section(bdb, BDB_LVDS_LFP_DATA);
243 	if (!lvds_lfp_data)
244 		return;
245 
246 
247 	entry = &lvds_lfp_data->data[lvds_options->panel_type];
248 	dvo_timing = &entry->dvo_timing;
249 
250 	panel_fixed_mode = kzalloc(sizeof(*panel_fixed_mode),
251 				      GFP_KERNEL);
252 	if (panel_fixed_mode == NULL) {
253 		dev_err(dev_priv->dev.dev, "out of memory for fixed panel mode\n");
254 		return;
255 	}
256 
257 	dev_priv->lvds_vbt = 1;
258 	fill_detail_timing_data(panel_fixed_mode, dvo_timing);
259 
260 	if (panel_fixed_mode->htotal > 0 && panel_fixed_mode->vtotal > 0) {
261 		dev_priv->lfp_lvds_vbt_mode = panel_fixed_mode;
262 		drm_mode_debug_printmodeline(panel_fixed_mode);
263 	} else {
264 		dev_dbg(dev_priv->dev.dev, "ignoring invalid LVDS VBT\n");
265 		dev_priv->lvds_vbt = 0;
266 		kfree(panel_fixed_mode);
267 	}
268 	return;
269 }
270 
271 /* Try to find sdvo panel data */
272 static void parse_sdvo_panel_data(struct drm_psb_private *dev_priv,
273 		      struct bdb_header *bdb)
274 {
275 	struct bdb_sdvo_lvds_options *sdvo_lvds_options;
276 	struct lvds_dvo_timing *dvo_timing;
277 	struct drm_display_mode *panel_fixed_mode;
278 
279 	dev_priv->sdvo_lvds_vbt_mode = NULL;
280 
281 	sdvo_lvds_options = find_section(bdb, BDB_SDVO_LVDS_OPTIONS);
282 	if (!sdvo_lvds_options)
283 		return;
284 
285 	dvo_timing = find_section(bdb, BDB_SDVO_PANEL_DTDS);
286 	if (!dvo_timing)
287 		return;
288 
289 	panel_fixed_mode = kzalloc(sizeof(*panel_fixed_mode), GFP_KERNEL);
290 
291 	if (!panel_fixed_mode)
292 		return;
293 
294 	fill_detail_timing_data(panel_fixed_mode,
295 			dvo_timing + sdvo_lvds_options->panel_type);
296 
297 	dev_priv->sdvo_lvds_vbt_mode = panel_fixed_mode;
298 
299 	return;
300 }
301 
302 static void parse_general_features(struct drm_psb_private *dev_priv,
303 		       struct bdb_header *bdb)
304 {
305 	struct bdb_general_features *general;
306 
307 	/* Set sensible defaults in case we can't find the general block */
308 	dev_priv->int_tv_support = 1;
309 	dev_priv->int_crt_support = 1;
310 
311 	general = find_section(bdb, BDB_GENERAL_FEATURES);
312 	if (general) {
313 		dev_priv->int_tv_support = general->int_tv_support;
314 		dev_priv->int_crt_support = general->int_crt_support;
315 		dev_priv->lvds_use_ssc = general->enable_ssc;
316 
317 		if (dev_priv->lvds_use_ssc) {
318 			dev_priv->lvds_ssc_freq
319 				= general->ssc_freq ? 100 : 96;
320 		}
321 	}
322 }
323 
324 static void
325 parse_sdvo_device_mapping(struct drm_psb_private *dev_priv,
326 			  struct bdb_header *bdb)
327 {
328 	struct sdvo_device_mapping *p_mapping;
329 	struct bdb_general_definitions *p_defs;
330 	struct child_device_config *p_child;
331 	int i, child_device_num, count;
332 	u16	block_size;
333 
334 	p_defs = find_section(bdb, BDB_GENERAL_DEFINITIONS);
335 	if (!p_defs) {
336 		DRM_DEBUG_KMS("No general definition block is found, unable to construct sdvo mapping.\n");
337 		return;
338 	}
339 	/* judge whether the size of child device meets the requirements.
340 	 * If the child device size obtained from general definition block
341 	 * is different with sizeof(struct child_device_config), skip the
342 	 * parsing of sdvo device info
343 	 */
344 	if (p_defs->child_dev_size != sizeof(*p_child)) {
345 		/* different child dev size . Ignore it */
346 		DRM_DEBUG_KMS("different child size is found. Invalid.\n");
347 		return;
348 	}
349 	/* get the block size of general definitions */
350 	block_size = get_blocksize(p_defs);
351 	/* get the number of child device */
352 	child_device_num = (block_size - sizeof(*p_defs)) /
353 				sizeof(*p_child);
354 	count = 0;
355 	for (i = 0; i < child_device_num; i++) {
356 		p_child = &(p_defs->devices[i]);
357 		if (!p_child->device_type) {
358 			/* skip the device block if device type is invalid */
359 			continue;
360 		}
361 		if (p_child->target_addr != TARGET_ADDR1 &&
362 			p_child->target_addr != TARGET_ADDR2) {
363 			/*
364 			 * If the target address is neither 0x70 nor 0x72,
365 			 * it is not a SDVO device. Skip it.
366 			 */
367 			continue;
368 		}
369 		if (p_child->dvo_port != DEVICE_PORT_DVOB &&
370 			p_child->dvo_port != DEVICE_PORT_DVOC) {
371 			/* skip the incorrect SDVO port */
372 			DRM_DEBUG_KMS("Incorrect SDVO port. Skip it\n");
373 			continue;
374 		}
375 		DRM_DEBUG_KMS("the SDVO device with target addr %2x is found on"
376 				" %s port\n",
377 				p_child->target_addr,
378 				(p_child->dvo_port == DEVICE_PORT_DVOB) ?
379 					"SDVOB" : "SDVOC");
380 		p_mapping = &(dev_priv->sdvo_mappings[p_child->dvo_port - 1]);
381 		if (!p_mapping->initialized) {
382 			p_mapping->dvo_port = p_child->dvo_port;
383 			p_mapping->target_addr = p_child->target_addr;
384 			p_mapping->dvo_wiring = p_child->dvo_wiring;
385 			p_mapping->ddc_pin = p_child->ddc_pin;
386 			p_mapping->i2c_pin = p_child->i2c_pin;
387 			p_mapping->initialized = 1;
388 			DRM_DEBUG_KMS("SDVO device: dvo=%x, addr=%x, wiring=%d, ddc_pin=%d, i2c_pin=%d\n",
389 				      p_mapping->dvo_port,
390 				      p_mapping->target_addr,
391 				      p_mapping->dvo_wiring,
392 				      p_mapping->ddc_pin,
393 				      p_mapping->i2c_pin);
394 		} else {
395 			DRM_DEBUG_KMS("Maybe one SDVO port is shared by "
396 					 "two SDVO device.\n");
397 		}
398 		if (p_child->target2_addr) {
399 			/* Maybe this is a SDVO device with multiple inputs */
400 			/* And the mapping info is not added */
401 			DRM_DEBUG_KMS("there exists the target2_addr. Maybe this"
402 				" is a SDVO device with multiple inputs.\n");
403 		}
404 		count++;
405 	}
406 
407 	if (!count) {
408 		/* No SDVO device info is found */
409 		DRM_DEBUG_KMS("No SDVO device info is found in VBT\n");
410 	}
411 	return;
412 }
413 
414 
415 static void
416 parse_driver_features(struct drm_psb_private *dev_priv,
417 		      struct bdb_header *bdb)
418 {
419 	struct bdb_driver_features *driver;
420 
421 	driver = find_section(bdb, BDB_DRIVER_FEATURES);
422 	if (!driver)
423 		return;
424 
425 	if (driver->lvds_config == BDB_DRIVER_FEATURE_EDP)
426 		dev_priv->edp.support = 1;
427 
428 	dev_priv->lvds_enabled_in_vbt = driver->lvds_config != 0;
429 	DRM_DEBUG_KMS("LVDS VBT config bits: 0x%x\n", driver->lvds_config);
430 
431 	/* This bit means to use 96Mhz for DPLL_A or not */
432 	if (driver->primary_lfp_id)
433 		dev_priv->dplla_96mhz = true;
434 	else
435 		dev_priv->dplla_96mhz = false;
436 }
437 
438 static void
439 parse_device_mapping(struct drm_psb_private *dev_priv,
440 		       struct bdb_header *bdb)
441 {
442 	struct bdb_general_definitions *p_defs;
443 	struct child_device_config *p_child, *child_dev_ptr;
444 	int i, child_device_num, count;
445 	u16	block_size;
446 
447 	p_defs = find_section(bdb, BDB_GENERAL_DEFINITIONS);
448 	if (!p_defs) {
449 		DRM_DEBUG_KMS("No general definition block is found, no devices defined.\n");
450 		return;
451 	}
452 	/* judge whether the size of child device meets the requirements.
453 	 * If the child device size obtained from general definition block
454 	 * is different with sizeof(struct child_device_config), skip the
455 	 * parsing of sdvo device info
456 	 */
457 	if (p_defs->child_dev_size != sizeof(*p_child)) {
458 		/* different child dev size . Ignore it */
459 		DRM_DEBUG_KMS("different child size is found. Invalid.\n");
460 		return;
461 	}
462 	/* get the block size of general definitions */
463 	block_size = get_blocksize(p_defs);
464 	/* get the number of child device */
465 	child_device_num = (block_size - sizeof(*p_defs)) /
466 				sizeof(*p_child);
467 	count = 0;
468 	/* get the number of child devices that are present */
469 	for (i = 0; i < child_device_num; i++) {
470 		p_child = &(p_defs->devices[i]);
471 		if (!p_child->device_type) {
472 			/* skip the device block if device type is invalid */
473 			continue;
474 		}
475 		count++;
476 	}
477 	if (!count) {
478 		DRM_DEBUG_KMS("no child dev is parsed from VBT\n");
479 		return;
480 	}
481 	dev_priv->child_dev = kcalloc(count, sizeof(*p_child), GFP_KERNEL);
482 	if (!dev_priv->child_dev) {
483 		DRM_DEBUG_KMS("No memory space for child devices\n");
484 		return;
485 	}
486 
487 	dev_priv->child_dev_num = count;
488 	count = 0;
489 	for (i = 0; i < child_device_num; i++) {
490 		p_child = &(p_defs->devices[i]);
491 		if (!p_child->device_type) {
492 			/* skip the device block if device type is invalid */
493 			continue;
494 		}
495 		child_dev_ptr = dev_priv->child_dev + count;
496 		count++;
497 		memcpy((void *)child_dev_ptr, (void *)p_child,
498 					sizeof(*p_child));
499 	}
500 	return;
501 }
502 
503 
504 /**
505  * psb_intel_init_bios - initialize VBIOS settings & find VBT
506  * @dev: DRM device
507  *
508  * Loads the Video BIOS and checks that the VBT exists.  Sets scratch registers
509  * to appropriate values.
510  *
511  * VBT existence is a sanity check that is relied on by other i830_bios.c code.
512  * Note that it would be better to use a BIOS call to get the VBT, as BIOSes may
513  * feed an updated VBT back through that, compared to what we'll fetch using
514  * this method of groping around in the BIOS data.
515  *
516  * Returns 0 on success, nonzero on failure.
517  */
518 int psb_intel_init_bios(struct drm_device *dev)
519 {
520 	struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
521 	struct pci_dev *pdev = to_pci_dev(dev->dev);
522 	struct vbt_header *vbt = NULL;
523 	struct bdb_header *bdb = NULL;
524 	u8 __iomem *bios = NULL;
525 	size_t size;
526 	int i;
527 
528 
529 	dev_priv->panel_type = 0xff;
530 
531 	/* XXX Should this validation be moved to intel_opregion.c? */
532 	if (dev_priv->opregion.vbt) {
533 		struct vbt_header *vbt = dev_priv->opregion.vbt;
534 		if (memcmp(vbt->signature, "$VBT", 4) == 0) {
535 			DRM_DEBUG_KMS("Using VBT from OpRegion: %20s\n",
536 					 vbt->signature);
537 			bdb = (struct bdb_header *)((char *)vbt + vbt->bdb_offset);
538 		} else
539 			dev_priv->opregion.vbt = NULL;
540 	}
541 
542 	if (bdb == NULL) {
543 		bios = pci_map_rom(pdev, &size);
544 		if (!bios)
545 			return -1;
546 
547 		/* Scour memory looking for the VBT signature */
548 		for (i = 0; i + 4 < size; i++) {
549 			if (!memcmp(bios + i, "$VBT", 4)) {
550 				vbt = (struct vbt_header *)(bios + i);
551 				break;
552 			}
553 		}
554 
555 		if (!vbt) {
556 			dev_err(dev->dev, "VBT signature missing\n");
557 			pci_unmap_rom(pdev, bios);
558 			return -1;
559 		}
560 		bdb = (struct bdb_header *)(bios + i + vbt->bdb_offset);
561 	}
562 
563 	/* Grab useful general dxefinitions */
564 	parse_general_features(dev_priv, bdb);
565 	parse_driver_features(dev_priv, bdb);
566 	parse_lfp_panel_data(dev_priv, bdb);
567 	parse_sdvo_panel_data(dev_priv, bdb);
568 	parse_sdvo_device_mapping(dev_priv, bdb);
569 	parse_device_mapping(dev_priv, bdb);
570 	parse_backlight_data(dev_priv, bdb);
571 	parse_edp(dev_priv, bdb);
572 
573 	if (bios)
574 		pci_unmap_rom(pdev, bios);
575 
576 	return 0;
577 }
578 
579 /*
580  * Destroy and free VBT data
581  */
582 void psb_intel_destroy_bios(struct drm_device *dev)
583 {
584 	struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
585 
586 	kfree(dev_priv->sdvo_lvds_vbt_mode);
587 	kfree(dev_priv->lfp_lvds_vbt_mode);
588 	kfree(dev_priv->lvds_bl);
589 }
590