modesetting-101: rename modeflags, as to avoid conflicts with the xorg definitions
[platform/upstream/libdrm.git] / linux-core / 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 #if 0
34 unsigned char *
35 i830_bios_get_aim_data_block (ScrnInfoPtr pScrn, int aim, int data_block)
36 {
37     unsigned char   *bios;
38     int             bdb_off;
39     int             vbt_off;
40     int             aim_off;
41     struct vbt_header *vbt;
42     struct aimdb_header *aimdb;
43     struct aimdb_block *aimdb_block;
44
45     bios = i830_bios_get (pScrn);
46     if (!bios)
47         return NULL;
48
49     vbt_off = INTEL_BIOS_16(0x1a);
50     vbt = (struct vbt_header *)(bios + vbt_off);
51
52     aim_off = vbt->aim_offset[aim];
53     if (!aim_off)
54     {
55         free (bios);
56         return NULL;
57     }
58     xf86DrvMsg(pScrn->scrnIndex, X_INFO, "aim_off %d\n", aim_off);
59     aimdb = (struct aimdb_header *) (bios + vbt_off + aim_off);
60     bdb_off = aimdb->aimdb_header_size;
61     while (bdb_off < aimdb->aimdb_size)
62     {
63         aimdb_block = (struct aimdb_block *) (bios + vbt_off + aim_off + bdb_off);
64         if (aimdb_block->aimdb_id == data_block)
65         {
66             unsigned char   *aim = malloc (aimdb_block->aimdb_size + sizeof (struct aimdb_block));
67             if (!aim)
68             {
69                 free (bios);
70                 return NULL;
71             }
72             memcpy (aim, aimdb_block, aimdb_block->aimdb_size + sizeof (struct aimdb_block));
73             free (bios);
74             return aim;
75         }
76         bdb_off += aimdb_block->aimdb_size + sizeof (struct aimdb_block);
77     }
78     free (bios);
79     return NULL;
80 }
81 #endif
82
83 static void *
84 find_section(struct bdb_header *bdb, int section_id)
85 {
86         u8 *base = (u8 *)bdb;
87         int index = 0;
88         u16 total, current_size;
89         u8 current_id;
90
91         /* skip to first section */
92         index += bdb->header_size;
93         total = bdb->bdb_size;
94
95         /* walk the sections looking for section_id */
96         while (index < total) {
97                 current_id = *(base + index);
98                 index++;
99                 current_size = *((u16 *)(base + index));
100                 index += 2;
101                 if (current_id == section_id)
102                         return base + index;
103                 index += current_size;
104         }
105
106         return NULL;
107 }
108
109 /* Try to find panel data */
110 static void
111 parse_panel_data(struct drm_i915_private *dev_priv, struct bdb_header *bdb)
112 {
113         struct bdb_lvds_options *lvds_options;
114         struct bdb_lvds_lfp_data *lvds_lfp_data;
115         struct bdb_lvds_lfp_data_entry *entry;
116         struct lvds_dvo_timing *dvo_timing;
117         struct drm_display_mode *panel_fixed_mode;
118
119         /* Defaults if we can't find VBT info */
120         dev_priv->lvds_dither = 0;
121         dev_priv->lvds_vbt = 0;
122
123         lvds_options = find_section(bdb, BDB_LVDS_OPTIONS);
124         if (!lvds_options)
125                 return;
126
127         dev_priv->lvds_dither = lvds_options->pixel_dither;
128         if (lvds_options->panel_type == 0xff)
129                 return;
130
131         lvds_lfp_data = find_section(bdb, BDB_LVDS_LFP_DATA);
132         if (!lvds_lfp_data)
133                 return;
134
135         dev_priv->lvds_vbt = 1;
136
137         entry = &lvds_lfp_data->data[lvds_options->panel_type];
138         dvo_timing = &entry->dvo_timing;
139
140         panel_fixed_mode = drm_calloc(1, sizeof(*panel_fixed_mode),
141                                       DRM_MEM_DRIVER);
142
143         panel_fixed_mode->hdisplay = (dvo_timing->hactive_hi << 8) |
144                 dvo_timing->hactive_lo;
145         panel_fixed_mode->hsync_start = panel_fixed_mode->hdisplay +
146                 ((dvo_timing->hsync_off_hi << 8) | dvo_timing->hsync_off_lo);
147         panel_fixed_mode->hsync_end = panel_fixed_mode->hsync_start +
148                 dvo_timing->hsync_pulse_width;
149         panel_fixed_mode->htotal = panel_fixed_mode->hdisplay +
150                 ((dvo_timing->hblank_hi << 8) | dvo_timing->hblank_lo);
151
152         panel_fixed_mode->vdisplay = (dvo_timing->vactive_hi << 8) |
153                 dvo_timing->vactive_lo;
154         panel_fixed_mode->vsync_start = panel_fixed_mode->vdisplay +
155                 dvo_timing->vsync_off;
156         panel_fixed_mode->vsync_end = panel_fixed_mode->vsync_start +
157                 dvo_timing->vsync_pulse_width;
158         panel_fixed_mode->vtotal = panel_fixed_mode->vdisplay +
159                 ((dvo_timing->vblank_hi << 8) | dvo_timing->vblank_lo);
160         panel_fixed_mode->clock = dvo_timing->clock * 10;
161         panel_fixed_mode->type = DRM_MODE_TYPE_PREFERRED;
162
163         drm_mode_set_name(panel_fixed_mode);
164
165         dev_priv->vbt_mode = panel_fixed_mode;
166
167         DRM_DEBUG("Found panel mode in BIOS VBT tables:\n");
168         drm_mode_debug_printmodeline(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         }
188 }
189
190 /**
191  * intel_init_bios - initialize VBIOS settings & find VBT
192  * @dev: DRM device
193  *
194  * Loads the Video BIOS and checks that the VBT exists.  Sets scratch registers
195  * to appropriate values.
196  *
197  * VBT existence is a sanity check that is relied on by other i830_bios.c code.
198  * Note that it would be better to use a BIOS call to get the VBT, as BIOSes may
199  * feed an updated VBT back through that, compared to what we'll fetch using
200  * this method of groping around in the BIOS data.
201  *
202  * Returns 0 on success, nonzero on failure.
203  */
204 bool
205 intel_init_bios(struct drm_device *dev)
206 {
207         struct drm_i915_private *dev_priv = dev->dev_private;
208         struct pci_dev *pdev = dev->pdev;
209         struct vbt_header *vbt = NULL;
210         struct bdb_header *bdb;
211         u8 __iomem *bios;
212         size_t size;
213         int i;
214
215         bios = pci_map_rom(pdev, &size);
216         if (!bios)
217                 return -1;
218
219         /* Scour memory looking for the VBT signature */
220         for (i = 0; i + 4 < size; i++) {
221                 if (!memcmp(bios + i, "$VBT", 4)) {
222                         vbt = (struct vbt_header *)(bios + i);
223                         break;
224                 }
225         }
226
227         if (!vbt) {
228                 DRM_ERROR("VBT signature missing\n");
229                 pci_unmap_rom(pdev, bios);
230                 return -1;
231         }
232
233         bdb = (struct bdb_header *)(bios + i + vbt->bdb_offset);
234
235         /* Grab useful general definitions */
236         parse_general_features(dev_priv, bdb);
237         parse_panel_data(dev_priv, bdb);
238
239         pci_unmap_rom(pdev, bios);
240
241         return 0;
242 }