Merge branch 'omap-clock-for-next' of git://git.pwsan.com/linux-2.6 into devel
[pandora-kernel.git] / drivers / gpu / drm / i915 / intel_bios.c
1 /*
2  * Copyright © 2006 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21  * SOFTWARE.
22  *
23  * Authors:
24  *    Eric Anholt <eric@anholt.net>
25  *
26  */
27 #include "drmP.h"
28 #include "drm.h"
29 #include "i915_drm.h"
30 #include "i915_drv.h"
31 #include "intel_bios.h"
32
33 #define SLAVE_ADDR1     0x70
34 #define SLAVE_ADDR2     0x72
35
36 static void *
37 find_section(struct bdb_header *bdb, int section_id)
38 {
39         u8 *base = (u8 *)bdb;
40         int index = 0;
41         u16 total, current_size;
42         u8 current_id;
43
44         /* skip to first section */
45         index += bdb->header_size;
46         total = bdb->bdb_size;
47
48         /* walk the sections looking for section_id */
49         while (index < total) {
50                 current_id = *(base + index);
51                 index++;
52                 current_size = *((u16 *)(base + index));
53                 index += 2;
54                 if (current_id == section_id)
55                         return base + index;
56                 index += current_size;
57         }
58
59         return NULL;
60 }
61
62 static void
63 fill_detail_timing_data(struct drm_display_mode *panel_fixed_mode,
64                         struct lvds_dvo_timing *dvo_timing)
65 {
66         panel_fixed_mode->hdisplay = (dvo_timing->hactive_hi << 8) |
67                 dvo_timing->hactive_lo;
68         panel_fixed_mode->hsync_start = panel_fixed_mode->hdisplay +
69                 ((dvo_timing->hsync_off_hi << 8) | dvo_timing->hsync_off_lo);
70         panel_fixed_mode->hsync_end = panel_fixed_mode->hsync_start +
71                 dvo_timing->hsync_pulse_width;
72         panel_fixed_mode->htotal = panel_fixed_mode->hdisplay +
73                 ((dvo_timing->hblank_hi << 8) | dvo_timing->hblank_lo);
74
75         panel_fixed_mode->vdisplay = (dvo_timing->vactive_hi << 8) |
76                 dvo_timing->vactive_lo;
77         panel_fixed_mode->vsync_start = panel_fixed_mode->vdisplay +
78                 dvo_timing->vsync_off;
79         panel_fixed_mode->vsync_end = panel_fixed_mode->vsync_start +
80                 dvo_timing->vsync_pulse_width;
81         panel_fixed_mode->vtotal = panel_fixed_mode->vdisplay +
82                 ((dvo_timing->vblank_hi << 8) | dvo_timing->vblank_lo);
83         panel_fixed_mode->clock = dvo_timing->clock * 10;
84         panel_fixed_mode->type = DRM_MODE_TYPE_PREFERRED;
85
86         /* Some VBTs have bogus h/vtotal values */
87         if (panel_fixed_mode->hsync_end > panel_fixed_mode->htotal)
88                 panel_fixed_mode->htotal = panel_fixed_mode->hsync_end + 1;
89         if (panel_fixed_mode->vsync_end > panel_fixed_mode->vtotal)
90                 panel_fixed_mode->vtotal = panel_fixed_mode->vsync_end + 1;
91
92         drm_mode_set_name(panel_fixed_mode);
93 }
94
95 /* Try to find integrated panel data */
96 static void
97 parse_lfp_panel_data(struct drm_i915_private *dev_priv,
98                             struct bdb_header *bdb)
99 {
100         struct bdb_lvds_options *lvds_options;
101         struct bdb_lvds_lfp_data *lvds_lfp_data;
102         struct bdb_lvds_lfp_data_entry *entry;
103         struct lvds_dvo_timing *dvo_timing;
104         struct drm_display_mode *panel_fixed_mode;
105
106         /* Defaults if we can't find VBT info */
107         dev_priv->lvds_dither = 0;
108         dev_priv->lvds_vbt = 0;
109
110         lvds_options = find_section(bdb, BDB_LVDS_OPTIONS);
111         if (!lvds_options)
112                 return;
113
114         dev_priv->lvds_dither = lvds_options->pixel_dither;
115         if (lvds_options->panel_type == 0xff)
116                 return;
117
118         lvds_lfp_data = find_section(bdb, BDB_LVDS_LFP_DATA);
119         if (!lvds_lfp_data)
120                 return;
121
122         dev_priv->lvds_vbt = 1;
123
124         entry = &lvds_lfp_data->data[lvds_options->panel_type];
125         dvo_timing = &entry->dvo_timing;
126
127         panel_fixed_mode = drm_calloc(1, sizeof(*panel_fixed_mode),
128                                       DRM_MEM_DRIVER);
129
130         fill_detail_timing_data(panel_fixed_mode, dvo_timing);
131
132         dev_priv->lfp_lvds_vbt_mode = panel_fixed_mode;
133
134         DRM_DEBUG("Found panel mode in BIOS VBT tables:\n");
135         drm_mode_debug_printmodeline(panel_fixed_mode);
136
137         return;
138 }
139
140 /* Try to find sdvo panel data */
141 static void
142 parse_sdvo_panel_data(struct drm_i915_private *dev_priv,
143                       struct bdb_header *bdb)
144 {
145         struct bdb_sdvo_lvds_options *sdvo_lvds_options;
146         struct lvds_dvo_timing *dvo_timing;
147         struct drm_display_mode *panel_fixed_mode;
148
149         dev_priv->sdvo_lvds_vbt_mode = NULL;
150
151         sdvo_lvds_options = find_section(bdb, BDB_SDVO_LVDS_OPTIONS);
152         if (!sdvo_lvds_options)
153                 return;
154
155         dvo_timing = find_section(bdb, BDB_SDVO_PANEL_DTDS);
156         if (!dvo_timing)
157                 return;
158
159         panel_fixed_mode = drm_calloc(1, sizeof(*panel_fixed_mode),
160                                       DRM_MEM_DRIVER);
161
162         if (!panel_fixed_mode)
163                 return;
164
165         fill_detail_timing_data(panel_fixed_mode,
166                         dvo_timing + sdvo_lvds_options->panel_type);
167
168         dev_priv->sdvo_lvds_vbt_mode = panel_fixed_mode;
169
170         return;
171 }
172
173 static void
174 parse_general_features(struct drm_i915_private *dev_priv,
175                        struct bdb_header *bdb)
176 {
177         struct bdb_general_features *general;
178
179         /* Set sensible defaults in case we can't find the general block */
180         dev_priv->int_tv_support = 1;
181         dev_priv->int_crt_support = 1;
182
183         general = find_section(bdb, BDB_GENERAL_FEATURES);
184         if (general) {
185                 dev_priv->int_tv_support = general->int_tv_support;
186                 dev_priv->int_crt_support = general->int_crt_support;
187                 dev_priv->lvds_use_ssc = general->enable_ssc;
188
189                 if (dev_priv->lvds_use_ssc) {
190                   if (IS_I855(dev_priv->dev))
191                     dev_priv->lvds_ssc_freq = general->ssc_freq ? 66 : 48;
192                   else
193                     dev_priv->lvds_ssc_freq = general->ssc_freq ? 100 : 96;
194                 }
195         }
196 }
197
198 static void
199 parse_sdvo_device_mapping(struct drm_i915_private *dev_priv,
200                        struct bdb_header *bdb)
201 {
202         struct sdvo_device_mapping *p_mapping;
203         struct bdb_general_definitions *p_defs;
204         struct child_device_config *p_child;
205         int i, child_device_num, count;
206         u16     block_size, *block_ptr;
207
208         p_defs = find_section(bdb, BDB_GENERAL_DEFINITIONS);
209         if (!p_defs) {
210                 DRM_DEBUG("No general definition block is found\n");
211                 return;
212         }
213         /* judge whether the size of child device meets the requirements.
214          * If the child device size obtained from general definition block
215          * is different with sizeof(struct child_device_config), skip the
216          * parsing of sdvo device info
217          */
218         if (p_defs->child_dev_size != sizeof(*p_child)) {
219                 /* different child dev size . Ignore it */
220                 DRM_DEBUG("different child size is found. Invalid.\n");
221                 return;
222         }
223         /* get the block size of general definitions */
224         block_ptr = (u16 *)((char *)p_defs - 2);
225         block_size = *block_ptr;
226         /* get the number of child device */
227         child_device_num = (block_size - sizeof(*p_defs)) /
228                                 sizeof(*p_child);
229         count = 0;
230         for (i = 0; i < child_device_num; i++) {
231                 p_child = &(p_defs->devices[i]);
232                 if (!p_child->device_type) {
233                         /* skip the device block if device type is invalid */
234                         continue;
235                 }
236                 if (p_child->slave_addr != SLAVE_ADDR1 &&
237                         p_child->slave_addr != SLAVE_ADDR2) {
238                         /*
239                          * If the slave address is neither 0x70 nor 0x72,
240                          * it is not a SDVO device. Skip it.
241                          */
242                         continue;
243                 }
244                 if (p_child->dvo_port != DEVICE_PORT_DVOB &&
245                         p_child->dvo_port != DEVICE_PORT_DVOC) {
246                         /* skip the incorrect SDVO port */
247                         DRM_DEBUG("Incorrect SDVO port. Skip it \n");
248                         continue;
249                 }
250                 DRM_DEBUG("the SDVO device with slave addr %2x is found on "
251                                 "%s port\n",
252                                 p_child->slave_addr,
253                                 (p_child->dvo_port == DEVICE_PORT_DVOB) ?
254                                         "SDVOB" : "SDVOC");
255                 p_mapping = &(dev_priv->sdvo_mappings[p_child->dvo_port - 1]);
256                 if (!p_mapping->initialized) {
257                         p_mapping->dvo_port = p_child->dvo_port;
258                         p_mapping->slave_addr = p_child->slave_addr;
259                         p_mapping->dvo_wiring = p_child->dvo_wiring;
260                         p_mapping->initialized = 1;
261                 } else {
262                         DRM_DEBUG("Maybe one SDVO port is shared by "
263                                          "two SDVO device.\n");
264                 }
265                 if (p_child->slave2_addr) {
266                         /* Maybe this is a SDVO device with multiple inputs */
267                         /* And the mapping info is not added */
268                         DRM_DEBUG("there exists the slave2_addr. Maybe this "
269                                 "is a SDVO device with multiple inputs.\n");
270                 }
271                 count++;
272         }
273
274         if (!count) {
275                 /* No SDVO device info is found */
276                 DRM_DEBUG("No SDVO device info is found in VBT\n");
277         }
278         return;
279 }
280 /**
281  * intel_init_bios - initialize VBIOS settings & find VBT
282  * @dev: DRM device
283  *
284  * Loads the Video BIOS and checks that the VBT exists.  Sets scratch registers
285  * to appropriate values.
286  *
287  * VBT existence is a sanity check that is relied on by other i830_bios.c code.
288  * Note that it would be better to use a BIOS call to get the VBT, as BIOSes may
289  * feed an updated VBT back through that, compared to what we'll fetch using
290  * this method of groping around in the BIOS data.
291  *
292  * Returns 0 on success, nonzero on failure.
293  */
294 bool
295 intel_init_bios(struct drm_device *dev)
296 {
297         struct drm_i915_private *dev_priv = dev->dev_private;
298         struct pci_dev *pdev = dev->pdev;
299         struct vbt_header *vbt = NULL;
300         struct bdb_header *bdb;
301         u8 __iomem *bios;
302         size_t size;
303         int i;
304
305         bios = pci_map_rom(pdev, &size);
306         if (!bios)
307                 return -1;
308
309         /* Scour memory looking for the VBT signature */
310         for (i = 0; i + 4 < size; i++) {
311                 if (!memcmp(bios + i, "$VBT", 4)) {
312                         vbt = (struct vbt_header *)(bios + i);
313                         break;
314                 }
315         }
316
317         if (!vbt) {
318                 DRM_ERROR("VBT signature missing\n");
319                 pci_unmap_rom(pdev, bios);
320                 return -1;
321         }
322
323         bdb = (struct bdb_header *)(bios + i + vbt->bdb_offset);
324
325         /* Grab useful general definitions */
326         parse_general_features(dev_priv, bdb);
327         parse_lfp_panel_data(dev_priv, bdb);
328         parse_sdvo_panel_data(dev_priv, bdb);
329         parse_sdvo_device_mapping(dev_priv, bdb);
330         pci_unmap_rom(pdev, bios);
331
332         return 0;
333 }