2 * Copyright (c) 2006 Intel Corporation
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.
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
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.
18 * Eric Anholt <eric@anholt.net>
25 #include "psb_intel_drv.h"
26 #include "psb_intel_reg.h"
27 #include "intel_bios.h"
29 #define SLAVE_ADDR1 0x70
30 #define SLAVE_ADDR2 0x72
32 static void *find_section(struct bdb_header
*bdb
, int section_id
)
36 u16 total
, current_size
;
39 /* skip to first section */
40 index
+= bdb
->header_size
;
41 total
= bdb
->bdb_size
;
43 /* walk the sections looking for section_id */
44 while (index
< total
) {
45 current_id
= *(base
+ index
);
47 current_size
= *((u16
*)(base
+ index
));
49 if (current_id
== section_id
)
51 index
+= current_size
;
58 get_blocksize(void *p
)
60 u16
*block_ptr
, block_size
;
62 block_ptr
= (u16
*)((char *)p
- 2);
63 block_size
= *block_ptr
;
67 static void fill_detail_timing_data(struct drm_display_mode
*panel_fixed_mode
,
68 struct lvds_dvo_timing
*dvo_timing
)
70 panel_fixed_mode
->hdisplay
= (dvo_timing
->hactive_hi
<< 8) |
71 dvo_timing
->hactive_lo
;
72 panel_fixed_mode
->hsync_start
= panel_fixed_mode
->hdisplay
+
73 ((dvo_timing
->hsync_off_hi
<< 8) | dvo_timing
->hsync_off_lo
);
74 panel_fixed_mode
->hsync_end
= panel_fixed_mode
->hsync_start
+
75 dvo_timing
->hsync_pulse_width
;
76 panel_fixed_mode
->htotal
= panel_fixed_mode
->hdisplay
+
77 ((dvo_timing
->hblank_hi
<< 8) | dvo_timing
->hblank_lo
);
79 panel_fixed_mode
->vdisplay
= (dvo_timing
->vactive_hi
<< 8) |
80 dvo_timing
->vactive_lo
;
81 panel_fixed_mode
->vsync_start
= panel_fixed_mode
->vdisplay
+
82 dvo_timing
->vsync_off
;
83 panel_fixed_mode
->vsync_end
= panel_fixed_mode
->vsync_start
+
84 dvo_timing
->vsync_pulse_width
;
85 panel_fixed_mode
->vtotal
= panel_fixed_mode
->vdisplay
+
86 ((dvo_timing
->vblank_hi
<< 8) | dvo_timing
->vblank_lo
);
87 panel_fixed_mode
->clock
= dvo_timing
->clock
* 10;
88 panel_fixed_mode
->type
= DRM_MODE_TYPE_PREFERRED
;
90 if (dvo_timing
->hsync_positive
)
91 panel_fixed_mode
->flags
|= DRM_MODE_FLAG_PHSYNC
;
93 panel_fixed_mode
->flags
|= DRM_MODE_FLAG_NHSYNC
;
95 if (dvo_timing
->vsync_positive
)
96 panel_fixed_mode
->flags
|= DRM_MODE_FLAG_PVSYNC
;
98 panel_fixed_mode
->flags
|= DRM_MODE_FLAG_NVSYNC
;
100 /* Some VBTs have bogus h/vtotal values */
101 if (panel_fixed_mode
->hsync_end
> panel_fixed_mode
->htotal
)
102 panel_fixed_mode
->htotal
= panel_fixed_mode
->hsync_end
+ 1;
103 if (panel_fixed_mode
->vsync_end
> panel_fixed_mode
->vtotal
)
104 panel_fixed_mode
->vtotal
= panel_fixed_mode
->vsync_end
+ 1;
106 drm_mode_set_name(panel_fixed_mode
);
109 static void parse_backlight_data(struct drm_psb_private
*dev_priv
,
110 struct bdb_header
*bdb
)
112 struct bdb_lvds_backlight
*vbt_lvds_bl
= NULL
;
113 struct bdb_lvds_backlight
*lvds_bl
;
115 void *bl_start
= NULL
;
116 struct bdb_lvds_options
*lvds_opts
117 = find_section(bdb
, BDB_LVDS_OPTIONS
);
119 dev_priv
->lvds_bl
= NULL
;
122 p_type
= lvds_opts
->panel_type
;
126 bl_start
= find_section(bdb
, BDB_LVDS_BACKLIGHT
);
127 vbt_lvds_bl
= (struct bdb_lvds_backlight
*)(bl_start
+ 1) + p_type
;
129 lvds_bl
= kzalloc(sizeof(*vbt_lvds_bl
), GFP_KERNEL
);
131 dev_err(dev_priv
->dev
->dev
, "out of memory for backlight data\n");
134 memcpy(lvds_bl
, vbt_lvds_bl
, sizeof(*vbt_lvds_bl
));
135 dev_priv
->lvds_bl
= lvds_bl
;
138 /* Try to find integrated panel data */
139 static void parse_lfp_panel_data(struct drm_psb_private
*dev_priv
,
140 struct bdb_header
*bdb
)
142 struct bdb_lvds_options
*lvds_options
;
143 struct bdb_lvds_lfp_data
*lvds_lfp_data
;
144 struct bdb_lvds_lfp_data_entry
*entry
;
145 struct lvds_dvo_timing
*dvo_timing
;
146 struct drm_display_mode
*panel_fixed_mode
;
148 /* Defaults if we can't find VBT info */
149 dev_priv
->lvds_dither
= 0;
150 dev_priv
->lvds_vbt
= 0;
152 lvds_options
= find_section(bdb
, BDB_LVDS_OPTIONS
);
156 dev_priv
->lvds_dither
= lvds_options
->pixel_dither
;
157 if (lvds_options
->panel_type
== 0xff)
160 lvds_lfp_data
= find_section(bdb
, BDB_LVDS_LFP_DATA
);
165 entry
= &lvds_lfp_data
->data
[lvds_options
->panel_type
];
166 dvo_timing
= &entry
->dvo_timing
;
168 panel_fixed_mode
= kzalloc(sizeof(*panel_fixed_mode
),
170 if (panel_fixed_mode
== NULL
) {
171 dev_err(dev_priv
->dev
->dev
, "out of memory for fixed panel mode\n");
175 dev_priv
->lvds_vbt
= 1;
176 fill_detail_timing_data(panel_fixed_mode
, dvo_timing
);
178 if (panel_fixed_mode
->htotal
> 0 && panel_fixed_mode
->vtotal
> 0) {
179 dev_priv
->lfp_lvds_vbt_mode
= panel_fixed_mode
;
180 drm_mode_debug_printmodeline(panel_fixed_mode
);
182 dev_dbg(dev_priv
->dev
->dev
, "ignoring invalid LVDS VBT\n");
183 dev_priv
->lvds_vbt
= 0;
184 kfree(panel_fixed_mode
);
189 /* Try to find sdvo panel data */
190 static void parse_sdvo_panel_data(struct drm_psb_private
*dev_priv
,
191 struct bdb_header
*bdb
)
193 struct bdb_sdvo_lvds_options
*sdvo_lvds_options
;
194 struct lvds_dvo_timing
*dvo_timing
;
195 struct drm_display_mode
*panel_fixed_mode
;
197 dev_priv
->sdvo_lvds_vbt_mode
= NULL
;
199 sdvo_lvds_options
= find_section(bdb
, BDB_SDVO_LVDS_OPTIONS
);
200 if (!sdvo_lvds_options
)
203 dvo_timing
= find_section(bdb
, BDB_SDVO_PANEL_DTDS
);
207 panel_fixed_mode
= kzalloc(sizeof(*panel_fixed_mode
), GFP_KERNEL
);
209 if (!panel_fixed_mode
)
212 fill_detail_timing_data(panel_fixed_mode
,
213 dvo_timing
+ sdvo_lvds_options
->panel_type
);
215 dev_priv
->sdvo_lvds_vbt_mode
= panel_fixed_mode
;
220 static void parse_general_features(struct drm_psb_private
*dev_priv
,
221 struct bdb_header
*bdb
)
223 struct bdb_general_features
*general
;
225 /* Set sensible defaults in case we can't find the general block */
226 dev_priv
->int_tv_support
= 1;
227 dev_priv
->int_crt_support
= 1;
229 general
= find_section(bdb
, BDB_GENERAL_FEATURES
);
231 dev_priv
->int_tv_support
= general
->int_tv_support
;
232 dev_priv
->int_crt_support
= general
->int_crt_support
;
233 dev_priv
->lvds_use_ssc
= general
->enable_ssc
;
235 if (dev_priv
->lvds_use_ssc
) {
236 dev_priv
->lvds_ssc_freq
237 = general
->ssc_freq
? 100 : 96;
243 parse_sdvo_device_mapping(struct drm_psb_private
*dev_priv
,
244 struct bdb_header
*bdb
)
246 struct sdvo_device_mapping
*p_mapping
;
247 struct bdb_general_definitions
*p_defs
;
248 struct child_device_config
*p_child
;
249 int i
, child_device_num
, count
;
252 p_defs
= find_section(bdb
, BDB_GENERAL_DEFINITIONS
);
254 DRM_DEBUG_KMS("No general definition block is found, unable to construct sdvo mapping.\n");
257 /* judge whether the size of child device meets the requirements.
258 * If the child device size obtained from general definition block
259 * is different with sizeof(struct child_device_config), skip the
260 * parsing of sdvo device info
262 if (p_defs
->child_dev_size
!= sizeof(*p_child
)) {
263 /* different child dev size . Ignore it */
264 DRM_DEBUG_KMS("different child size is found. Invalid.\n");
267 /* get the block size of general definitions */
268 block_size
= get_blocksize(p_defs
);
269 /* get the number of child device */
270 child_device_num
= (block_size
- sizeof(*p_defs
)) /
273 for (i
= 0; i
< child_device_num
; i
++) {
274 p_child
= &(p_defs
->devices
[i
]);
275 if (!p_child
->device_type
) {
276 /* skip the device block if device type is invalid */
279 if (p_child
->slave_addr
!= SLAVE_ADDR1
&&
280 p_child
->slave_addr
!= SLAVE_ADDR2
) {
282 * If the slave address is neither 0x70 nor 0x72,
283 * it is not a SDVO device. Skip it.
287 if (p_child
->dvo_port
!= DEVICE_PORT_DVOB
&&
288 p_child
->dvo_port
!= DEVICE_PORT_DVOC
) {
289 /* skip the incorrect SDVO port */
290 DRM_DEBUG_KMS("Incorrect SDVO port. Skip it\n");
293 DRM_DEBUG_KMS("the SDVO device with slave addr %2x is found on"
296 (p_child
->dvo_port
== DEVICE_PORT_DVOB
) ?
298 p_mapping
= &(dev_priv
->sdvo_mappings
[p_child
->dvo_port
- 1]);
299 if (!p_mapping
->initialized
) {
300 p_mapping
->dvo_port
= p_child
->dvo_port
;
301 p_mapping
->slave_addr
= p_child
->slave_addr
;
302 p_mapping
->dvo_wiring
= p_child
->dvo_wiring
;
303 p_mapping
->ddc_pin
= p_child
->ddc_pin
;
304 p_mapping
->i2c_pin
= p_child
->i2c_pin
;
305 p_mapping
->initialized
= 1;
306 DRM_DEBUG_KMS("SDVO device: dvo=%x, addr=%x, wiring=%d, ddc_pin=%d, i2c_pin=%d\n",
308 p_mapping
->slave_addr
,
309 p_mapping
->dvo_wiring
,
313 DRM_DEBUG_KMS("Maybe one SDVO port is shared by "
314 "two SDVO device.\n");
316 if (p_child
->slave2_addr
) {
317 /* Maybe this is a SDVO device with multiple inputs */
318 /* And the mapping info is not added */
319 DRM_DEBUG_KMS("there exists the slave2_addr. Maybe this"
320 " is a SDVO device with multiple inputs.\n");
326 /* No SDVO device info is found */
327 DRM_DEBUG_KMS("No SDVO device info is found in VBT\n");
334 parse_driver_features(struct drm_psb_private
*dev_priv
,
335 struct bdb_header
*bdb
)
337 struct bdb_driver_features
*driver
;
339 driver
= find_section(bdb
, BDB_DRIVER_FEATURES
);
343 /* This bit means to use 96Mhz for DPLL_A or not */
344 if (driver
->primary_lfp_id
)
345 dev_priv
->dplla_96mhz
= true;
347 dev_priv
->dplla_96mhz
= false;
351 parse_device_mapping(struct drm_psb_private
*dev_priv
,
352 struct bdb_header
*bdb
)
354 struct bdb_general_definitions
*p_defs
;
355 struct child_device_config
*p_child
, *child_dev_ptr
;
356 int i
, child_device_num
, count
;
359 p_defs
= find_section(bdb
, BDB_GENERAL_DEFINITIONS
);
361 DRM_DEBUG_KMS("No general definition block is found, no devices defined.\n");
364 /* judge whether the size of child device meets the requirements.
365 * If the child device size obtained from general definition block
366 * is different with sizeof(struct child_device_config), skip the
367 * parsing of sdvo device info
369 if (p_defs
->child_dev_size
!= sizeof(*p_child
)) {
370 /* different child dev size . Ignore it */
371 DRM_DEBUG_KMS("different child size is found. Invalid.\n");
374 /* get the block size of general definitions */
375 block_size
= get_blocksize(p_defs
);
376 /* get the number of child device */
377 child_device_num
= (block_size
- sizeof(*p_defs
)) /
380 /* get the number of child devices that are present */
381 for (i
= 0; i
< child_device_num
; i
++) {
382 p_child
= &(p_defs
->devices
[i
]);
383 if (!p_child
->device_type
) {
384 /* skip the device block if device type is invalid */
390 DRM_DEBUG_KMS("no child dev is parsed from VBT\n");
393 dev_priv
->child_dev
= kcalloc(count
, sizeof(*p_child
), GFP_KERNEL
);
394 if (!dev_priv
->child_dev
) {
395 DRM_DEBUG_KMS("No memory space for child devices\n");
399 dev_priv
->child_dev_num
= count
;
401 for (i
= 0; i
< child_device_num
; i
++) {
402 p_child
= &(p_defs
->devices
[i
]);
403 if (!p_child
->device_type
) {
404 /* skip the device block if device type is invalid */
407 child_dev_ptr
= dev_priv
->child_dev
+ count
;
409 memcpy((void *)child_dev_ptr
, (void *)p_child
,
417 * psb_intel_init_bios - initialize VBIOS settings & find VBT
420 * Loads the Video BIOS and checks that the VBT exists. Sets scratch registers
421 * to appropriate values.
423 * VBT existence is a sanity check that is relied on by other i830_bios.c code.
424 * Note that it would be better to use a BIOS call to get the VBT, as BIOSes may
425 * feed an updated VBT back through that, compared to what we'll fetch using
426 * this method of groping around in the BIOS data.
428 * Returns 0 on success, nonzero on failure.
430 bool psb_intel_init_bios(struct drm_device
*dev
)
432 struct drm_psb_private
*dev_priv
= dev
->dev_private
;
433 struct pci_dev
*pdev
= dev
->pdev
;
434 struct vbt_header
*vbt
= NULL
;
435 struct bdb_header
*bdb
= NULL
;
436 u8 __iomem
*bios
= NULL
;
440 /* XXX Should this validation be moved to intel_opregion.c? */
441 if (dev_priv
->opregion
.vbt
) {
442 struct vbt_header
*vbt
= dev_priv
->opregion
.vbt
;
443 if (memcmp(vbt
->signature
, "$VBT", 4) == 0) {
444 DRM_DEBUG_KMS("Using VBT from OpRegion: %20s\n",
446 bdb
= (struct bdb_header
*)((char *)vbt
+ vbt
->bdb_offset
);
448 dev_priv
->opregion
.vbt
= NULL
;
452 bios
= pci_map_rom(pdev
, &size
);
456 /* Scour memory looking for the VBT signature */
457 for (i
= 0; i
+ 4 < size
; i
++) {
458 if (!memcmp(bios
+ i
, "$VBT", 4)) {
459 vbt
= (struct vbt_header
*)(bios
+ i
);
465 dev_err(dev
->dev
, "VBT signature missing\n");
466 pci_unmap_rom(pdev
, bios
);
469 bdb
= (struct bdb_header
*)(bios
+ i
+ vbt
->bdb_offset
);
472 /* Grab useful general dxefinitions */
473 parse_general_features(dev_priv
, bdb
);
474 parse_driver_features(dev_priv
, bdb
);
475 parse_lfp_panel_data(dev_priv
, bdb
);
476 parse_sdvo_panel_data(dev_priv
, bdb
);
477 parse_sdvo_device_mapping(dev_priv
, bdb
);
478 parse_device_mapping(dev_priv
, bdb
);
479 parse_backlight_data(dev_priv
, bdb
);
482 pci_unmap_rom(pdev
, bios
);
488 * Destroy and free VBT data
490 void psb_intel_destroy_bios(struct drm_device
*dev
)
492 struct drm_psb_private
*dev_priv
= dev
->dev_private
;
494 kfree(dev_priv
->sdvo_lvds_vbt_mode
);
495 kfree(dev_priv
->lfp_lvds_vbt_mode
);
496 kfree(dev_priv
->lvds_bl
);