source: grub-pc/trunk/fuentes/grub-core/video/sis315pro.c @ 22

Last change on this file since 22 was 22, checked in by mabarracus, 4 years ago

updated version and apply net.ifnames=0 into debian/rules

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