1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2006 Intel Corporation
9 #include <drm/drm_dp_helper.h>
11 #include "intel_bios.h"
13 #include "psb_intel_drv.h"
14 #include "psb_intel_reg.h"
16 #define SLAVE_ADDR1 0x70
17 #define SLAVE_ADDR2 0x72
19 static void *find_section(struct bdb_header *bdb, int section_id)
23 u16 total, current_size;
26 /* skip to first section */
27 index += bdb->header_size;
28 total = bdb->bdb_size;
30 /* walk the sections looking for section_id */
31 while (index < total) {
32 current_id = *(base + index);
34 current_size = *((u16 *)(base + index));
36 if (current_id == section_id)
38 index += current_size;
45 parse_edp(struct drm_psb_private *dev_priv, struct bdb_header *bdb)
48 struct edp_power_seq *edp_pps;
49 struct edp_link_params *edp_link_params;
52 edp = find_section(bdb, BDB_EDP);
54 dev_priv->edp.bpp = 18;
56 if (dev_priv->edp.support) {
57 DRM_DEBUG_KMS("No eDP BDB found but eDP panel supported, assume %dbpp panel color depth.\n",
63 panel_type = dev_priv->panel_type;
64 switch ((edp->color_depth >> (panel_type * 2)) & 3) {
66 dev_priv->edp.bpp = 18;
69 dev_priv->edp.bpp = 24;
72 dev_priv->edp.bpp = 30;
76 /* Get the eDP sequencing and link info */
77 edp_pps = &edp->power_seqs[panel_type];
78 edp_link_params = &edp->link_params[panel_type];
80 dev_priv->edp.pps = *edp_pps;
82 DRM_DEBUG_KMS("EDP timing in vbt t1_t3 %d t8 %d t9 %d t10 %d t11_t12 %d\n",
83 dev_priv->edp.pps.t1_t3, dev_priv->edp.pps.t8,
84 dev_priv->edp.pps.t9, dev_priv->edp.pps.t10,
85 dev_priv->edp.pps.t11_t12);
87 dev_priv->edp.rate = edp_link_params->rate ? DP_LINK_BW_2_7 :
89 switch (edp_link_params->lanes) {
91 dev_priv->edp.lanes = 1;
94 dev_priv->edp.lanes = 2;
98 dev_priv->edp.lanes = 4;
101 DRM_DEBUG_KMS("VBT reports EDP: Lane_count %d, Lane_rate %d, Bpp %d\n",
102 dev_priv->edp.lanes, dev_priv->edp.rate, dev_priv->edp.bpp);
104 switch (edp_link_params->preemphasis) {
106 dev_priv->edp.preemphasis = DP_TRAIN_PRE_EMPH_LEVEL_0;
109 dev_priv->edp.preemphasis = DP_TRAIN_PRE_EMPH_LEVEL_1;
112 dev_priv->edp.preemphasis = DP_TRAIN_PRE_EMPH_LEVEL_2;
115 dev_priv->edp.preemphasis = DP_TRAIN_PRE_EMPH_LEVEL_3;
118 switch (edp_link_params->vswing) {
120 dev_priv->edp.vswing = DP_TRAIN_VOLTAGE_SWING_LEVEL_0;
123 dev_priv->edp.vswing = DP_TRAIN_VOLTAGE_SWING_LEVEL_1;
126 dev_priv->edp.vswing = DP_TRAIN_VOLTAGE_SWING_LEVEL_2;
129 dev_priv->edp.vswing = DP_TRAIN_VOLTAGE_SWING_LEVEL_3;
132 DRM_DEBUG_KMS("VBT reports EDP: VSwing %d, Preemph %d\n",
133 dev_priv->edp.vswing, dev_priv->edp.preemphasis);
137 get_blocksize(void *p)
139 u16 *block_ptr, block_size;
141 block_ptr = (u16 *)((char *)p - 2);
142 block_size = *block_ptr;
146 static void fill_detail_timing_data(struct drm_display_mode *panel_fixed_mode,
147 struct lvds_dvo_timing *dvo_timing)
149 panel_fixed_mode->hdisplay = (dvo_timing->hactive_hi << 8) |
150 dvo_timing->hactive_lo;
151 panel_fixed_mode->hsync_start = panel_fixed_mode->hdisplay +
152 ((dvo_timing->hsync_off_hi << 8) | dvo_timing->hsync_off_lo);
153 panel_fixed_mode->hsync_end = panel_fixed_mode->hsync_start +
154 dvo_timing->hsync_pulse_width;
155 panel_fixed_mode->htotal = panel_fixed_mode->hdisplay +
156 ((dvo_timing->hblank_hi << 8) | dvo_timing->hblank_lo);
158 panel_fixed_mode->vdisplay = (dvo_timing->vactive_hi << 8) |
159 dvo_timing->vactive_lo;
160 panel_fixed_mode->vsync_start = panel_fixed_mode->vdisplay +
161 dvo_timing->vsync_off;
162 panel_fixed_mode->vsync_end = panel_fixed_mode->vsync_start +
163 dvo_timing->vsync_pulse_width;
164 panel_fixed_mode->vtotal = panel_fixed_mode->vdisplay +
165 ((dvo_timing->vblank_hi << 8) | dvo_timing->vblank_lo);
166 panel_fixed_mode->clock = dvo_timing->clock * 10;
167 panel_fixed_mode->type = DRM_MODE_TYPE_PREFERRED;
169 if (dvo_timing->hsync_positive)
170 panel_fixed_mode->flags |= DRM_MODE_FLAG_PHSYNC;
172 panel_fixed_mode->flags |= DRM_MODE_FLAG_NHSYNC;
174 if (dvo_timing->vsync_positive)
175 panel_fixed_mode->flags |= DRM_MODE_FLAG_PVSYNC;
177 panel_fixed_mode->flags |= DRM_MODE_FLAG_NVSYNC;
179 /* Some VBTs have bogus h/vtotal values */
180 if (panel_fixed_mode->hsync_end > panel_fixed_mode->htotal)
181 panel_fixed_mode->htotal = panel_fixed_mode->hsync_end + 1;
182 if (panel_fixed_mode->vsync_end > panel_fixed_mode->vtotal)
183 panel_fixed_mode->vtotal = panel_fixed_mode->vsync_end + 1;
185 drm_mode_set_name(panel_fixed_mode);
188 static void parse_backlight_data(struct drm_psb_private *dev_priv,
189 struct bdb_header *bdb)
191 struct bdb_lvds_backlight *vbt_lvds_bl = NULL;
192 struct bdb_lvds_backlight *lvds_bl;
194 void *bl_start = NULL;
195 struct bdb_lvds_options *lvds_opts
196 = find_section(bdb, BDB_LVDS_OPTIONS);
198 dev_priv->lvds_bl = NULL;
201 p_type = lvds_opts->panel_type;
205 bl_start = find_section(bdb, BDB_LVDS_BACKLIGHT);
206 vbt_lvds_bl = (struct bdb_lvds_backlight *)(bl_start + 1) + p_type;
208 lvds_bl = kmemdup(vbt_lvds_bl, sizeof(*vbt_lvds_bl), GFP_KERNEL);
210 dev_err(dev_priv->dev->dev, "out of memory for backlight data\n");
213 dev_priv->lvds_bl = lvds_bl;
216 /* Try to find integrated panel data */
217 static void parse_lfp_panel_data(struct drm_psb_private *dev_priv,
218 struct bdb_header *bdb)
220 struct bdb_lvds_options *lvds_options;
221 struct bdb_lvds_lfp_data *lvds_lfp_data;
222 struct bdb_lvds_lfp_data_entry *entry;
223 struct lvds_dvo_timing *dvo_timing;
224 struct drm_display_mode *panel_fixed_mode;
226 /* Defaults if we can't find VBT info */
227 dev_priv->lvds_dither = 0;
228 dev_priv->lvds_vbt = 0;
230 lvds_options = find_section(bdb, BDB_LVDS_OPTIONS);
234 dev_priv->lvds_dither = lvds_options->pixel_dither;
235 dev_priv->panel_type = lvds_options->panel_type;
237 if (lvds_options->panel_type == 0xff)
240 lvds_lfp_data = find_section(bdb, BDB_LVDS_LFP_DATA);
245 entry = &lvds_lfp_data->data[lvds_options->panel_type];
246 dvo_timing = &entry->dvo_timing;
248 panel_fixed_mode = kzalloc(sizeof(*panel_fixed_mode),
250 if (panel_fixed_mode == NULL) {
251 dev_err(dev_priv->dev->dev, "out of memory for fixed panel mode\n");
255 dev_priv->lvds_vbt = 1;
256 fill_detail_timing_data(panel_fixed_mode, dvo_timing);
258 if (panel_fixed_mode->htotal > 0 && panel_fixed_mode->vtotal > 0) {
259 dev_priv->lfp_lvds_vbt_mode = panel_fixed_mode;
260 drm_mode_debug_printmodeline(panel_fixed_mode);
262 dev_dbg(dev_priv->dev->dev, "ignoring invalid LVDS VBT\n");
263 dev_priv->lvds_vbt = 0;
264 kfree(panel_fixed_mode);
269 /* Try to find sdvo panel data */
270 static void parse_sdvo_panel_data(struct drm_psb_private *dev_priv,
271 struct bdb_header *bdb)
273 struct bdb_sdvo_lvds_options *sdvo_lvds_options;
274 struct lvds_dvo_timing *dvo_timing;
275 struct drm_display_mode *panel_fixed_mode;
277 dev_priv->sdvo_lvds_vbt_mode = NULL;
279 sdvo_lvds_options = find_section(bdb, BDB_SDVO_LVDS_OPTIONS);
280 if (!sdvo_lvds_options)
283 dvo_timing = find_section(bdb, BDB_SDVO_PANEL_DTDS);
287 panel_fixed_mode = kzalloc(sizeof(*panel_fixed_mode), GFP_KERNEL);
289 if (!panel_fixed_mode)
292 fill_detail_timing_data(panel_fixed_mode,
293 dvo_timing + sdvo_lvds_options->panel_type);
295 dev_priv->sdvo_lvds_vbt_mode = panel_fixed_mode;
300 static void parse_general_features(struct drm_psb_private *dev_priv,
301 struct bdb_header *bdb)
303 struct bdb_general_features *general;
305 /* Set sensible defaults in case we can't find the general block */
306 dev_priv->int_tv_support = 1;
307 dev_priv->int_crt_support = 1;
309 general = find_section(bdb, BDB_GENERAL_FEATURES);
311 dev_priv->int_tv_support = general->int_tv_support;
312 dev_priv->int_crt_support = general->int_crt_support;
313 dev_priv->lvds_use_ssc = general->enable_ssc;
315 if (dev_priv->lvds_use_ssc) {
316 dev_priv->lvds_ssc_freq
317 = general->ssc_freq ? 100 : 96;
323 parse_sdvo_device_mapping(struct drm_psb_private *dev_priv,
324 struct bdb_header *bdb)
326 struct sdvo_device_mapping *p_mapping;
327 struct bdb_general_definitions *p_defs;
328 struct child_device_config *p_child;
329 int i, child_device_num, count;
332 p_defs = find_section(bdb, BDB_GENERAL_DEFINITIONS);
334 DRM_DEBUG_KMS("No general definition block is found, unable to construct sdvo mapping.\n");
337 /* judge whether the size of child device meets the requirements.
338 * If the child device size obtained from general definition block
339 * is different with sizeof(struct child_device_config), skip the
340 * parsing of sdvo device info
342 if (p_defs->child_dev_size != sizeof(*p_child)) {
343 /* different child dev size . Ignore it */
344 DRM_DEBUG_KMS("different child size is found. Invalid.\n");
347 /* get the block size of general definitions */
348 block_size = get_blocksize(p_defs);
349 /* get the number of child device */
350 child_device_num = (block_size - sizeof(*p_defs)) /
353 for (i = 0; i < child_device_num; i++) {
354 p_child = &(p_defs->devices[i]);
355 if (!p_child->device_type) {
356 /* skip the device block if device type is invalid */
359 if (p_child->slave_addr != SLAVE_ADDR1 &&
360 p_child->slave_addr != SLAVE_ADDR2) {
362 * If the slave address is neither 0x70 nor 0x72,
363 * it is not a SDVO device. Skip it.
367 if (p_child->dvo_port != DEVICE_PORT_DVOB &&
368 p_child->dvo_port != DEVICE_PORT_DVOC) {
369 /* skip the incorrect SDVO port */
370 DRM_DEBUG_KMS("Incorrect SDVO port. Skip it\n");
373 DRM_DEBUG_KMS("the SDVO device with slave addr %2x is found on"
376 (p_child->dvo_port == DEVICE_PORT_DVOB) ?
378 p_mapping = &(dev_priv->sdvo_mappings[p_child->dvo_port - 1]);
379 if (!p_mapping->initialized) {
380 p_mapping->dvo_port = p_child->dvo_port;
381 p_mapping->slave_addr = p_child->slave_addr;
382 p_mapping->dvo_wiring = p_child->dvo_wiring;
383 p_mapping->ddc_pin = p_child->ddc_pin;
384 p_mapping->i2c_pin = p_child->i2c_pin;
385 p_mapping->initialized = 1;
386 DRM_DEBUG_KMS("SDVO device: dvo=%x, addr=%x, wiring=%d, ddc_pin=%d, i2c_pin=%d\n",
388 p_mapping->slave_addr,
389 p_mapping->dvo_wiring,
393 DRM_DEBUG_KMS("Maybe one SDVO port is shared by "
394 "two SDVO device.\n");
396 if (p_child->slave2_addr) {
397 /* Maybe this is a SDVO device with multiple inputs */
398 /* And the mapping info is not added */
399 DRM_DEBUG_KMS("there exists the slave2_addr. Maybe this"
400 " is a SDVO device with multiple inputs.\n");
406 /* No SDVO device info is found */
407 DRM_DEBUG_KMS("No SDVO device info is found in VBT\n");
414 parse_driver_features(struct drm_psb_private *dev_priv,
415 struct bdb_header *bdb)
417 struct bdb_driver_features *driver;
419 driver = find_section(bdb, BDB_DRIVER_FEATURES);
423 if (driver->lvds_config == BDB_DRIVER_FEATURE_EDP)
424 dev_priv->edp.support = 1;
426 dev_priv->lvds_enabled_in_vbt = driver->lvds_config != 0;
427 DRM_DEBUG_KMS("LVDS VBT config bits: 0x%x\n", driver->lvds_config);
429 /* This bit means to use 96Mhz for DPLL_A or not */
430 if (driver->primary_lfp_id)
431 dev_priv->dplla_96mhz = true;
433 dev_priv->dplla_96mhz = false;
437 parse_device_mapping(struct drm_psb_private *dev_priv,
438 struct bdb_header *bdb)
440 struct bdb_general_definitions *p_defs;
441 struct child_device_config *p_child, *child_dev_ptr;
442 int i, child_device_num, count;
445 p_defs = find_section(bdb, BDB_GENERAL_DEFINITIONS);
447 DRM_DEBUG_KMS("No general definition block is found, no devices defined.\n");
450 /* judge whether the size of child device meets the requirements.
451 * If the child device size obtained from general definition block
452 * is different with sizeof(struct child_device_config), skip the
453 * parsing of sdvo device info
455 if (p_defs->child_dev_size != sizeof(*p_child)) {
456 /* different child dev size . Ignore it */
457 DRM_DEBUG_KMS("different child size is found. Invalid.\n");
460 /* get the block size of general definitions */
461 block_size = get_blocksize(p_defs);
462 /* get the number of child device */
463 child_device_num = (block_size - sizeof(*p_defs)) /
466 /* get the number of child devices that are present */
467 for (i = 0; i < child_device_num; i++) {
468 p_child = &(p_defs->devices[i]);
469 if (!p_child->device_type) {
470 /* skip the device block if device type is invalid */
476 DRM_DEBUG_KMS("no child dev is parsed from VBT\n");
479 dev_priv->child_dev = kcalloc(count, sizeof(*p_child), GFP_KERNEL);
480 if (!dev_priv->child_dev) {
481 DRM_DEBUG_KMS("No memory space for child devices\n");
485 dev_priv->child_dev_num = count;
487 for (i = 0; i < child_device_num; i++) {
488 p_child = &(p_defs->devices[i]);
489 if (!p_child->device_type) {
490 /* skip the device block if device type is invalid */
493 child_dev_ptr = dev_priv->child_dev + count;
495 memcpy((void *)child_dev_ptr, (void *)p_child,
503 * psb_intel_init_bios - initialize VBIOS settings & find VBT
506 * Loads the Video BIOS and checks that the VBT exists. Sets scratch registers
507 * to appropriate values.
509 * VBT existence is a sanity check that is relied on by other i830_bios.c code.
510 * Note that it would be better to use a BIOS call to get the VBT, as BIOSes may
511 * feed an updated VBT back through that, compared to what we'll fetch using
512 * this method of groping around in the BIOS data.
514 * Returns 0 on success, nonzero on failure.
516 int psb_intel_init_bios(struct drm_device *dev)
518 struct drm_psb_private *dev_priv = dev->dev_private;
519 struct pci_dev *pdev = dev->pdev;
520 struct vbt_header *vbt = NULL;
521 struct bdb_header *bdb = NULL;
522 u8 __iomem *bios = NULL;
527 dev_priv->panel_type = 0xff;
529 /* XXX Should this validation be moved to intel_opregion.c? */
530 if (dev_priv->opregion.vbt) {
531 struct vbt_header *vbt = dev_priv->opregion.vbt;
532 if (memcmp(vbt->signature, "$VBT", 4) == 0) {
533 DRM_DEBUG_KMS("Using VBT from OpRegion: %20s\n",
535 bdb = (struct bdb_header *)((char *)vbt + vbt->bdb_offset);
537 dev_priv->opregion.vbt = NULL;
541 bios = pci_map_rom(pdev, &size);
545 /* Scour memory looking for the VBT signature */
546 for (i = 0; i + 4 < size; i++) {
547 if (!memcmp(bios + i, "$VBT", 4)) {
548 vbt = (struct vbt_header *)(bios + i);
554 dev_err(dev->dev, "VBT signature missing\n");
555 pci_unmap_rom(pdev, bios);
558 bdb = (struct bdb_header *)(bios + i + vbt->bdb_offset);
561 /* Grab useful general dxefinitions */
562 parse_general_features(dev_priv, bdb);
563 parse_driver_features(dev_priv, bdb);
564 parse_lfp_panel_data(dev_priv, bdb);
565 parse_sdvo_panel_data(dev_priv, bdb);
566 parse_sdvo_device_mapping(dev_priv, bdb);
567 parse_device_mapping(dev_priv, bdb);
568 parse_backlight_data(dev_priv, bdb);
569 parse_edp(dev_priv, bdb);
572 pci_unmap_rom(pdev, bios);
578 * Destroy and free VBT data
580 void psb_intel_destroy_bios(struct drm_device *dev)
582 struct drm_psb_private *dev_priv = dev->dev_private;
584 kfree(dev_priv->sdvo_lvds_vbt_mode);
585 kfree(dev_priv->lfp_lvds_vbt_mode);
586 kfree(dev_priv->lvds_bl);