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