]> git.proxmox.com Git - grub2.git/blob - grub-core/video/sis315pro.c
LUKS and GELI support.
[grub2.git] / grub-core / video / sis315pro.c
1 /*
2 * GRUB -- GRand Unified Bootloader
3 * Copyright (C) 2005,2006,2007,2008,2009,2010 Free Software Foundation, Inc.
4 *
5 * GRUB is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * GRUB is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #define grub_video_render_target grub_video_fbrender_target
20
21 #include <grub/err.h>
22 #include <grub/types.h>
23 #include <grub/dl.h>
24 #include <grub/misc.h>
25 #include <grub/mm.h>
26 #include <grub/video.h>
27 #include <grub/video_fb.h>
28 #include <grub/pci.h>
29 #include <grub/vga.h>
30
31 #define GRUB_SIS315PRO_PCIID 0x03251039
32 #define GRUB_SIS315PRO_TOTAL_MEMORY_SPACE 0x800000
33 #define GRUB_SIS315PRO_MMIO_SPACE 0x1000
34
35 static struct
36 {
37 struct grub_video_mode_info mode_info;
38 struct grub_video_render_target *render_target;
39
40 grub_uint8_t *ptr;
41 int mapped;
42 grub_uint32_t base;
43 grub_uint32_t mmiobase;
44 volatile grub_uint32_t *mmioptr;
45 grub_pci_device_t dev;
46 grub_port_t io;
47 } framebuffer;
48
49 static grub_uint8_t
50 read_sis_cmd (grub_uint8_t addr)
51 {
52 grub_outb (addr, framebuffer.io + 0x44);
53 return grub_inb (framebuffer.io + 0x45);
54 }
55
56 static void
57 write_sis_cmd (grub_uint8_t val, grub_uint8_t addr)
58 {
59 grub_outb (addr, framebuffer.io + 0x44);
60 grub_outb (val, framebuffer.io + 0x45);
61 }
62
63 #ifndef TEST
64 static grub_err_t
65 grub_video_sis315pro_video_init (void)
66 {
67 /* Reset frame buffer. */
68 grub_memset (&framebuffer, 0, sizeof(framebuffer));
69
70 return grub_video_fb_init ();
71 }
72
73 static grub_err_t
74 grub_video_sis315pro_video_fini (void)
75 {
76 if (framebuffer.mapped)
77 grub_pci_device_unmap_range (framebuffer.dev, framebuffer.ptr,
78 GRUB_SIS315PRO_TOTAL_MEMORY_SPACE);
79
80 return grub_video_fb_fini ();
81 }
82 #endif
83
84 #include "sis315_init.c"
85
86 static grub_err_t
87 grub_video_sis315pro_setup (unsigned int width, unsigned int height,
88 unsigned int mode_type,
89 unsigned int mode_mask __attribute__ ((unused)))
90 {
91 int depth;
92 grub_err_t err;
93 int found = 0;
94 unsigned i;
95
96 #ifndef TEST
97 auto int NESTED_FUNC_ATTR find_card (grub_pci_device_t dev, grub_pci_id_t pciid __attribute__ ((unused)));
98 int NESTED_FUNC_ATTR find_card (grub_pci_device_t dev, grub_pci_id_t pciid __attribute__ ((unused)))
99 {
100 grub_pci_address_t addr;
101 grub_uint32_t class;
102
103 addr = grub_pci_make_address (dev, GRUB_PCI_REG_CLASS);
104 class = grub_pci_read (addr);
105
106 if (((class >> 16) & 0xffff) != GRUB_PCI_CLASS_SUBCLASS_VGA
107 || pciid != GRUB_SIS315PRO_PCIID)
108 return 0;
109
110 found = 1;
111
112 addr = grub_pci_make_address (dev, GRUB_PCI_REG_ADDRESS_REG0);
113 framebuffer.base = grub_pci_read (addr) & GRUB_PCI_ADDR_MEM_MASK;
114 addr = grub_pci_make_address (dev, GRUB_PCI_REG_ADDRESS_REG1);
115 framebuffer.mmiobase = grub_pci_read (addr) & GRUB_PCI_ADDR_MEM_MASK;
116 addr = grub_pci_make_address (dev, GRUB_PCI_REG_ADDRESS_REG2);
117 framebuffer.io = (grub_pci_read (addr) & GRUB_PCI_ADDR_IO_MASK)
118 + GRUB_MACHINE_PCI_IO_BASE;
119 framebuffer.dev = dev;
120
121 return 1;
122 }
123
124 /* Decode depth from mode_type. If it is zero, then autodetect. */
125 depth = (mode_type & GRUB_VIDEO_MODE_TYPE_DEPTH_MASK)
126 >> GRUB_VIDEO_MODE_TYPE_DEPTH_POS;
127
128 if ((width != 640 && width != 0) || (height != 480 && height != 0)
129 || (depth != 8 && depth != 0))
130 return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
131 "Only 640x400x8 is supported");
132
133 grub_pci_iterate (find_card);
134 if (!found)
135 return grub_error (GRUB_ERR_IO, "Couldn't find graphics card");
136 #endif
137 /* Fill mode info details. */
138 framebuffer.mode_info.width = 640;
139 framebuffer.mode_info.height = 480;
140 framebuffer.mode_info.mode_type = GRUB_VIDEO_MODE_TYPE_INDEX_COLOR;
141 framebuffer.mode_info.bpp = 8;
142 framebuffer.mode_info.bytes_per_pixel = 1;
143 framebuffer.mode_info.pitch = 640 * 1;
144 framebuffer.mode_info.number_of_colors = 256;
145 framebuffer.mode_info.red_mask_size = 0;
146 framebuffer.mode_info.red_field_pos = 0;
147 framebuffer.mode_info.green_mask_size = 0;
148 framebuffer.mode_info.green_field_pos = 0;
149 framebuffer.mode_info.blue_mask_size = 0;
150 framebuffer.mode_info.blue_field_pos = 0;
151 framebuffer.mode_info.reserved_mask_size = 0;
152 framebuffer.mode_info.reserved_field_pos = 0;
153 #ifndef TEST
154 framebuffer.mode_info.blit_format
155 = grub_video_get_blit_format (&framebuffer.mode_info);
156 #endif
157
158 #ifndef TEST
159 if (found && (framebuffer.base == 0 || framebuffer.mmiobase == 0))
160 {
161 grub_pci_address_t addr;
162 /* FIXME: choose address dynamically if needed. */
163 framebuffer.base = 0x40000000;
164 framebuffer.mmiobase = 0x04000000;
165 framebuffer.io = 0xb300;
166
167 addr = grub_pci_make_address (framebuffer.dev, GRUB_PCI_REG_ADDRESS_REG0);
168 grub_pci_write (addr, framebuffer.base | GRUB_PCI_ADDR_MEM_PREFETCH);
169
170 addr = grub_pci_make_address (framebuffer.dev, GRUB_PCI_REG_ADDRESS_REG1);
171 grub_pci_write (addr, framebuffer.mmiobase);
172
173 addr = grub_pci_make_address (framebuffer.dev, GRUB_PCI_REG_ADDRESS_REG2);
174 grub_pci_write (addr, framebuffer.io | GRUB_PCI_ADDR_SPACE_IO);
175
176 /* Set latency. */
177 addr = grub_pci_make_address (framebuffer.dev, GRUB_PCI_REG_CACHELINE);
178 grub_pci_write (addr, 0x80004700);
179
180 /* Enable address spaces. */
181 addr = grub_pci_make_address (framebuffer.dev, GRUB_PCI_REG_COMMAND);
182 grub_pci_write (addr, 0x7);
183
184 addr = grub_pci_make_address (framebuffer.dev, 0x30);
185 grub_pci_write (addr, 0x04060001);
186
187 framebuffer.io += GRUB_MACHINE_PCI_IO_BASE;
188 }
189 #endif
190
191
192 /* We can safely discard volatile attribute. */
193 #ifndef TEST
194 framebuffer.ptr
195 = (void *) grub_pci_device_map_range (framebuffer.dev,
196 framebuffer.base,
197 GRUB_SIS315PRO_TOTAL_MEMORY_SPACE);
198 framebuffer.mmioptr = grub_pci_device_map_range (framebuffer.dev,
199 framebuffer.mmiobase,
200 GRUB_SIS315PRO_MMIO_SPACE);
201 #endif
202 framebuffer.mapped = 1;
203
204 #ifndef TEST
205 /* Prevent garbage from appearing on the screen. */
206 grub_memset (framebuffer.ptr, 0,
207 framebuffer.mode_info.height * framebuffer.mode_info.pitch);
208 #endif
209
210 grub_outb (GRUB_VGA_IO_MISC_NEGATIVE_VERT_POLARITY
211 | GRUB_VGA_IO_MISC_NEGATIVE_HORIZ_POLARITY
212 | GRUB_VGA_IO_MISC_UPPER_64K
213 | GRUB_VGA_IO_MISC_EXTERNAL_CLOCK_0
214 | GRUB_VGA_IO_MISC_28MHZ
215 | GRUB_VGA_IO_MISC_ENABLE_VRAM_ACCESS
216 | GRUB_VGA_IO_MISC_COLOR,
217 GRUB_VGA_IO_MISC_WRITE + GRUB_MACHINE_PCI_IO_BASE);
218
219 grub_vga_sr_write (0x86, 5);
220 for (i = 6; i <= 0x27; i++)
221 grub_vga_sr_write (0, i);
222
223 for (i = 0x31; i <= 0x3d; i++)
224 grub_vga_sr_write (0, i);
225
226 for (i = 0; i < ARRAY_SIZE (sr_dump); i++)
227 grub_vga_sr_write (sr_dump[i].val, sr_dump[i].reg);
228
229 for (i = 0x30; i < 0x40; i++)
230 grub_vga_cr_write (0, i);
231
232 grub_vga_cr_write (0x77, 0x40);
233 grub_vga_cr_write (0x77, 0x41);
234 grub_vga_cr_write (0x00, 0x42);
235 grub_vga_cr_write (0x5b, 0x43);
236 grub_vga_cr_write (0x00, 0x44);
237 grub_vga_cr_write (0x23, 0x48);
238 grub_vga_cr_write (0xaa, 0x49);
239 grub_vga_cr_write (0x02, 0x37);
240 grub_vga_cr_write (0x20, 0x5b);
241 grub_vga_cr_write (0x00, 0x83);
242 grub_vga_cr_write (0x80, 0x63);
243
244 grub_vga_cr_write (0x0c, GRUB_VGA_CR_VSYNC_END);
245 grub_vga_cr_write (0x5f, GRUB_VGA_CR_HTOTAL);
246 grub_vga_cr_write (0x4f, GRUB_VGA_CR_HORIZ_END);
247 grub_vga_cr_write (0x50, GRUB_VGA_CR_HBLANK_START);
248 grub_vga_cr_write (0x82, GRUB_VGA_CR_HBLANK_END);
249 grub_vga_cr_write (0x54, GRUB_VGA_CR_HORIZ_SYNC_PULSE_START);
250 grub_vga_cr_write (0x80, GRUB_VGA_CR_HORIZ_SYNC_PULSE_END);
251 grub_vga_cr_write (0x0b, GRUB_VGA_CR_VERT_TOTAL);
252 grub_vga_cr_write (0x3e, GRUB_VGA_CR_OVERFLOW);
253 grub_vga_cr_write (0x00, GRUB_VGA_CR_BYTE_PANNING);
254 grub_vga_cr_write (0x40, GRUB_VGA_CR_CELL_HEIGHT);
255 grub_vga_cr_write (0x00, GRUB_VGA_CR_CURSOR_START);
256 grub_vga_cr_write (0x00, GRUB_VGA_CR_CURSOR_END);
257 grub_vga_cr_write (0x00, GRUB_VGA_CR_START_ADDR_HIGH_REGISTER);
258 grub_vga_cr_write (0x00, GRUB_VGA_CR_START_ADDR_LOW_REGISTER);
259 grub_vga_cr_write (0x00, GRUB_VGA_CR_CURSOR_ADDR_HIGH);
260 grub_vga_cr_write (0x00, GRUB_VGA_CR_CURSOR_ADDR_LOW);
261 grub_vga_cr_write (0xea, GRUB_VGA_CR_VSYNC_START);
262 grub_vga_cr_write (0x8c, GRUB_VGA_CR_VSYNC_END);
263 grub_vga_cr_write (0xdf, GRUB_VGA_CR_VDISPLAY_END);
264 grub_vga_cr_write (0x28, GRUB_VGA_CR_PITCH);
265 grub_vga_cr_write (0x40, GRUB_VGA_CR_UNDERLINE_LOCATION);
266 grub_vga_cr_write (0xe7, GRUB_VGA_CR_VERTICAL_BLANK_START);
267 grub_vga_cr_write (0x04, GRUB_VGA_CR_VERTICAL_BLANK_END);
268 grub_vga_cr_write (0xa3, GRUB_VGA_CR_MODE);
269 grub_vga_cr_write (0xff, GRUB_VGA_CR_LINE_COMPARE);
270
271 grub_vga_cr_write (0x0c, GRUB_VGA_CR_VSYNC_END);
272 grub_vga_cr_write (0x5f, GRUB_VGA_CR_HTOTAL);
273 grub_vga_cr_write (0x4f, GRUB_VGA_CR_HORIZ_END);
274 grub_vga_cr_write (0x50, GRUB_VGA_CR_HBLANK_START);
275 grub_vga_cr_write (0x82, GRUB_VGA_CR_HBLANK_END);
276 grub_vga_cr_write (0x55, GRUB_VGA_CR_HORIZ_SYNC_PULSE_START);
277 grub_vga_cr_write (0x81, GRUB_VGA_CR_HORIZ_SYNC_PULSE_END);
278 grub_vga_cr_write (0x0b, GRUB_VGA_CR_VERT_TOTAL);
279 grub_vga_cr_write (0x3e, GRUB_VGA_CR_OVERFLOW);
280 grub_vga_cr_write (0xe9, GRUB_VGA_CR_VSYNC_START);
281 grub_vga_cr_write (0x8b, GRUB_VGA_CR_VSYNC_END);
282 grub_vga_cr_write (0xdf, GRUB_VGA_CR_VDISPLAY_END);
283 grub_vga_cr_write (0xe7, GRUB_VGA_CR_VERTICAL_BLANK_START);
284 grub_vga_cr_write (0x04, GRUB_VGA_CR_VERTICAL_BLANK_END);
285 grub_vga_cr_write (0x40, GRUB_VGA_CR_CELL_HEIGHT);
286 grub_vga_cr_write (0x50, GRUB_VGA_CR_PITCH);
287
288 grub_vga_cr_write (0x00, 0x19);
289 grub_vga_cr_write (0x00, 0x1a);
290 grub_vga_cr_write (0x6c, 0x52);
291 grub_vga_cr_write (0x2e, 0x34);
292 grub_vga_cr_write (0x00, 0x31);
293
294
295 grub_vga_cr_write (0, GRUB_VGA_CR_START_ADDR_HIGH_REGISTER);
296 grub_vga_cr_write (0, GRUB_VGA_CR_START_ADDR_LOW_REGISTER);
297
298 for (i = 0; i < 16; i++)
299 grub_vga_write_arx (i, i);
300 grub_vga_write_arx (1, GRUB_VGA_ARX_MODE);
301 grub_vga_write_arx (0, GRUB_VGA_ARX_OVERSCAN);
302 grub_vga_write_arx (0, GRUB_VGA_ARX_COLOR_PLANE_ENABLE);
303 grub_vga_write_arx (0, GRUB_VGA_ARX_HORIZONTAL_PANNING);
304 grub_vga_write_arx (0, GRUB_VGA_ARX_COLOR_SELECT);
305
306 grub_outb (0xff, GRUB_VGA_IO_PIXEL_MASK + GRUB_MACHINE_PCI_IO_BASE);
307
308 for (i = 0; i < ARRAY_SIZE (gr); i++)
309 grub_vga_gr_write (gr[i], i);
310
311 for (i = 0; i < GRUB_VIDEO_FBSTD_NUMCOLORS; i++)
312 grub_vga_palette_write (i, grub_video_fbstd_colors[i].r,
313 grub_video_fbstd_colors[i].g,
314 grub_video_fbstd_colors[i].b);
315
316 #if 1
317 {
318 if (read_sis_cmd (0x5) != 0xa1)
319 write_sis_cmd (0x86, 0x5);
320
321 write_sis_cmd (read_sis_cmd (0x20) | 0xa1, 0x20);
322 write_sis_cmd (read_sis_cmd (0x1e) | 0xda, 0x1e);
323
324 #define IND_SIS_CMDQUEUE_SET 0x26
325 #define IND_SIS_CMDQUEUE_THRESHOLD 0x27
326
327 #define COMMAND_QUEUE_THRESHOLD 0x1F
328 #define SIS_CMD_QUEUE_RESET 0x01
329
330 #define SIS_AGP_CMDQUEUE_ENABLE 0x80 /* 315/330/340 series SR26 */
331 #define SIS_VRAM_CMDQUEUE_ENABLE 0x40
332 #define SIS_MMIO_CMD_ENABLE 0x20
333 #define SIS_CMD_QUEUE_SIZE_512k 0x00
334 #define SIS_CMD_QUEUE_SIZE_1M 0x04
335 #define SIS_CMD_QUEUE_SIZE_2M 0x08
336 #define SIS_CMD_QUEUE_SIZE_4M 0x0C
337 #define SIS_CMD_QUEUE_RESET 0x01
338 #define SIS_CMD_AUTO_CORR 0x02
339
340
341 write_sis_cmd (COMMAND_QUEUE_THRESHOLD, IND_SIS_CMDQUEUE_THRESHOLD);
342 write_sis_cmd (SIS_CMD_QUEUE_RESET, IND_SIS_CMDQUEUE_SET);
343 framebuffer.mmioptr[0x85C4 / 4] = framebuffer.mmioptr[0x85C8 / 4];
344 write_sis_cmd (SIS_MMIO_CMD_ENABLE | SIS_CMD_AUTO_CORR, IND_SIS_CMDQUEUE_SET);
345 framebuffer.mmioptr[0x85C0 / 4] = (0x1000000 - (512 * 1024));
346 }
347 #endif
348
349 #ifndef TEST
350 err = grub_video_fb_create_render_target_from_pointer (&framebuffer
351 .render_target,
352 &framebuffer.mode_info,
353 framebuffer.ptr);
354
355 if (err)
356 return err;
357
358 err = grub_video_fb_set_active_render_target (framebuffer.render_target);
359
360 if (err)
361 return err;
362
363 /* Copy default palette to initialize emulated palette. */
364 err = grub_video_fb_set_palette (0, GRUB_VIDEO_FBSTD_NUMCOLORS,
365 grub_video_fbstd_colors);
366 #endif
367 return err;
368 }
369
370 #ifndef TEST
371
372 static grub_err_t
373 grub_video_sis315pro_swap_buffers (void)
374 {
375 /* TODO: Implement buffer swapping. */
376 return GRUB_ERR_NONE;
377 }
378
379 static grub_err_t
380 grub_video_sis315pro_set_active_render_target (struct grub_video_render_target *target)
381 {
382 if (target == GRUB_VIDEO_RENDER_TARGET_DISPLAY)
383 target = framebuffer.render_target;
384
385 return grub_video_fb_set_active_render_target (target);
386 }
387
388 static grub_err_t
389 grub_video_sis315pro_get_info_and_fini (struct grub_video_mode_info *mode_info,
390 void **framebuf)
391 {
392 grub_memcpy (mode_info, &(framebuffer.mode_info), sizeof (*mode_info));
393 *framebuf = (char *) framebuffer.ptr;
394
395 grub_video_fb_fini ();
396
397 return GRUB_ERR_NONE;
398 }
399
400 static struct grub_video_adapter grub_video_sis315pro_adapter =
401 {
402 .name = "SIS315PRO Video Driver",
403 .id = GRUB_VIDEO_DRIVER_SIS315PRO,
404
405 .prio = GRUB_VIDEO_ADAPTER_PRIO_NATIVE,
406
407 .init = grub_video_sis315pro_video_init,
408 .fini = grub_video_sis315pro_video_fini,
409 .setup = grub_video_sis315pro_setup,
410 .get_info = grub_video_fb_get_info,
411 .get_info_and_fini = grub_video_sis315pro_get_info_and_fini,
412 .set_palette = grub_video_fb_set_palette,
413 .get_palette = grub_video_fb_get_palette,
414 .set_viewport = grub_video_fb_set_viewport,
415 .get_viewport = grub_video_fb_get_viewport,
416 .map_color = grub_video_fb_map_color,
417 .map_rgb = grub_video_fb_map_rgb,
418 .map_rgba = grub_video_fb_map_rgba,
419 .unmap_color = grub_video_fb_unmap_color,
420 .fill_rect = grub_video_fb_fill_rect,
421 .blit_bitmap = grub_video_fb_blit_bitmap,
422 .blit_render_target = grub_video_fb_blit_render_target,
423 .scroll = grub_video_fb_scroll,
424 .swap_buffers = grub_video_sis315pro_swap_buffers,
425 .create_render_target = grub_video_fb_create_render_target,
426 .delete_render_target = grub_video_fb_delete_render_target,
427 .set_active_render_target = grub_video_sis315pro_set_active_render_target,
428 .get_active_render_target = grub_video_fb_get_active_render_target,
429
430 .next = 0
431 };
432
433 GRUB_MOD_INIT(video_sis315pro)
434 {
435 grub_video_register (&grub_video_sis315pro_adapter);
436 }
437
438 GRUB_MOD_FINI(video_sis315pro)
439 {
440 grub_video_unregister (&grub_video_sis315pro_adapter);
441 }
442 #else
443 int
444 main ()
445 {
446 grub_video_sis315pro_setup (640, 400, 0, 0);
447 }
448 #endif