vaseboot/VasEBoot-core/video/sis315pro.c

460 lines
16 KiB
C

/*
* VAS_EBOOT -- GRand Unified Bootloader
* Copyright (C) 2005,2006,2007,2008,2009,2010 Free Software Foundation, Inc.
*
* VAS_EBOOT is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* VAS_EBOOT is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with VAS_EBOOT. If not, see <http://www.gnu.org/licenses/>.
*/
#define VasEBoot_video_render_target VasEBoot_video_fbrender_target
#include <VasEBoot/err.h>
#include <VasEBoot/types.h>
#include <VasEBoot/dl.h>
#include <VasEBoot/misc.h>
#include <VasEBoot/mm.h>
#include <VasEBoot/video.h>
#include <VasEBoot/video_fb.h>
#include <VasEBoot/pci.h>
#include <VasEBoot/vga.h>
#include <VasEBoot/cache.h>
#define VAS_EBOOT_SIS315PRO_PCIID 0x03251039
#define VAS_EBOOT_SIS315PRO_TOTAL_MEMORY_SPACE 0x800000
#define VAS_EBOOT_SIS315PRO_MMIO_SPACE 0x1000
static struct
{
struct VasEBoot_video_mode_info mode_info;
VasEBoot_uint8_t *ptr;
volatile VasEBoot_uint8_t *direct_ptr;
int mapped;
VasEBoot_uint32_t base;
VasEBoot_uint32_t mmiobase;
volatile VasEBoot_uint32_t *mmioptr;
VasEBoot_pci_device_t dev;
VasEBoot_port_t io;
} framebuffer;
static VasEBoot_uint8_t
read_sis_cmd (VasEBoot_uint8_t addr)
{
VasEBoot_outb (addr, framebuffer.io + 0x44);
return VasEBoot_inb (framebuffer.io + 0x45);
}
static void
write_sis_cmd (VasEBoot_uint8_t val, VasEBoot_uint8_t addr)
{
VasEBoot_outb (addr, framebuffer.io + 0x44);
VasEBoot_outb (val, framebuffer.io + 0x45);
}
#ifndef TEST
static VasEBoot_err_t
VasEBoot_video_sis315pro_video_init (void)
{
/* Reset frame buffer. */
VasEBoot_memset (&framebuffer, 0, sizeof(framebuffer));
return VasEBoot_video_fb_init ();
}
static VasEBoot_err_t
VasEBoot_video_sis315pro_video_fini (void)
{
if (framebuffer.mapped)
{
VasEBoot_pci_device_unmap_range (framebuffer.dev, framebuffer.ptr,
VAS_EBOOT_SIS315PRO_TOTAL_MEMORY_SPACE);
VasEBoot_pci_device_unmap_range (framebuffer.dev, framebuffer.direct_ptr,
VAS_EBOOT_SIS315PRO_TOTAL_MEMORY_SPACE);
}
return VasEBoot_video_fb_fini ();
}
#endif
#include "sis315_init.c"
#ifndef TEST
/* Helper for VasEBoot_video_sis315pro_setup. */
static int
find_card (VasEBoot_pci_device_t dev, VasEBoot_pci_id_t pciid, void *data)
{
int *found = data;
VasEBoot_pci_address_t addr;
VasEBoot_uint32_t class;
addr = VasEBoot_pci_make_address (dev, VAS_EBOOT_PCI_REG_CLASS);
class = VasEBoot_pci_read (addr);
if (((class >> 16) & 0xffff) != VAS_EBOOT_PCI_CLASS_DISPLAY_VGA
|| pciid != VAS_EBOOT_SIS315PRO_PCIID)
return 0;
*found = 1;
addr = VasEBoot_pci_make_address (dev, VAS_EBOOT_PCI_REG_ADDRESS_REG0);
framebuffer.base = VasEBoot_pci_read (addr) & VAS_EBOOT_PCI_ADDR_MEM_MASK;
addr = VasEBoot_pci_make_address (dev, VAS_EBOOT_PCI_REG_ADDRESS_REG1);
framebuffer.mmiobase = VasEBoot_pci_read (addr) & VAS_EBOOT_PCI_ADDR_MEM_MASK;
addr = VasEBoot_pci_make_address (dev, VAS_EBOOT_PCI_REG_ADDRESS_REG2);
framebuffer.io = (VasEBoot_pci_read (addr) & VAS_EBOOT_PCI_ADDR_IO_MASK)
+ VAS_EBOOT_MACHINE_PCI_IO_BASE;
framebuffer.dev = dev;
return 1;
}
#endif
static VasEBoot_err_t
VasEBoot_video_sis315pro_setup (unsigned int width, unsigned int height,
unsigned int mode_type,
unsigned int mode_mask __attribute__ ((unused)))
{
int depth;
VasEBoot_err_t err;
int found = 0;
unsigned i;
#ifndef TEST
/* Decode depth from mode_type. If it is zero, then autodetect. */
depth = (mode_type & VAS_EBOOT_VIDEO_MODE_TYPE_DEPTH_MASK)
>> VAS_EBOOT_VIDEO_MODE_TYPE_DEPTH_POS;
if ((width != 640 && width != 0) || (height != 480 && height != 0)
|| (depth != 8 && depth != 0))
return VasEBoot_error (VAS_EBOOT_ERR_NOT_IMPLEMENTED_YET,
"Only 640x480x8 is supported");
VasEBoot_pci_iterate (find_card, &found);
if (!found)
return VasEBoot_error (VAS_EBOOT_ERR_IO, "Couldn't find graphics card");
#endif
/* Fill mode info details. */
framebuffer.mode_info.width = 640;
framebuffer.mode_info.height = 480;
framebuffer.mode_info.mode_type = (VAS_EBOOT_VIDEO_MODE_TYPE_INDEX_COLOR
| VAS_EBOOT_VIDEO_MODE_TYPE_DOUBLE_BUFFERED
| VAS_EBOOT_VIDEO_MODE_TYPE_UPDATING_SWAP);
framebuffer.mode_info.bpp = 8;
framebuffer.mode_info.bytes_per_pixel = 1;
framebuffer.mode_info.pitch = 640 * 1;
framebuffer.mode_info.number_of_colors = 16;
framebuffer.mode_info.red_mask_size = 0;
framebuffer.mode_info.red_field_pos = 0;
framebuffer.mode_info.green_mask_size = 0;
framebuffer.mode_info.green_field_pos = 0;
framebuffer.mode_info.blue_mask_size = 0;
framebuffer.mode_info.blue_field_pos = 0;
framebuffer.mode_info.reserved_mask_size = 0;
framebuffer.mode_info.reserved_field_pos = 0;
#ifndef TEST
framebuffer.mode_info.blit_format
= VasEBoot_video_get_blit_format (&framebuffer.mode_info);
#endif
#ifndef TEST
if (found && (framebuffer.base == 0 || framebuffer.mmiobase == 0))
{
VasEBoot_pci_address_t addr;
/* FIXME: choose address dynamically if needed. */
framebuffer.base = 0x40000000;
framebuffer.mmiobase = 0x04000000;
framebuffer.io = 0xb300;
addr = VasEBoot_pci_make_address (framebuffer.dev, VAS_EBOOT_PCI_REG_ADDRESS_REG0);
VasEBoot_pci_write (addr, framebuffer.base | VAS_EBOOT_PCI_ADDR_MEM_PREFETCH);
addr = VasEBoot_pci_make_address (framebuffer.dev, VAS_EBOOT_PCI_REG_ADDRESS_REG1);
VasEBoot_pci_write (addr, framebuffer.mmiobase);
addr = VasEBoot_pci_make_address (framebuffer.dev, VAS_EBOOT_PCI_REG_ADDRESS_REG2);
VasEBoot_pci_write (addr, framebuffer.io | VAS_EBOOT_PCI_ADDR_SPACE_IO);
/* Set latency. */
addr = VasEBoot_pci_make_address (framebuffer.dev, VAS_EBOOT_PCI_REG_CACHELINE);
VasEBoot_pci_write (addr, 0x80004700);
/* Enable address spaces. */
addr = VasEBoot_pci_make_address (framebuffer.dev, VAS_EBOOT_PCI_REG_COMMAND);
VasEBoot_pci_write (addr, 0x7);
addr = VasEBoot_pci_make_address (framebuffer.dev, 0x30);
VasEBoot_pci_write (addr, 0x04060001);
framebuffer.io += VAS_EBOOT_MACHINE_PCI_IO_BASE;
}
#endif
/* We can safely discard volatile attribute. */
#ifndef TEST
framebuffer.ptr
= VasEBoot_pci_device_map_range_cached (framebuffer.dev,
framebuffer.base,
VAS_EBOOT_SIS315PRO_TOTAL_MEMORY_SPACE);
framebuffer.direct_ptr
= VasEBoot_pci_device_map_range (framebuffer.dev,
framebuffer.base,
VAS_EBOOT_SIS315PRO_TOTAL_MEMORY_SPACE);
framebuffer.mmioptr = VasEBoot_pci_device_map_range (framebuffer.dev,
framebuffer.mmiobase,
VAS_EBOOT_SIS315PRO_MMIO_SPACE);
#endif
framebuffer.mapped = 1;
#ifndef TEST
/* Prevent garbage from appearing on the screen. */
VasEBoot_memset (framebuffer.ptr, 0,
framebuffer.mode_info.height * framebuffer.mode_info.pitch);
VasEBoot_arch_sync_dma_caches (framebuffer.ptr,
framebuffer.mode_info.height
* framebuffer.mode_info.pitch);
#endif
VasEBoot_outb (VAS_EBOOT_VGA_IO_MISC_NEGATIVE_VERT_POLARITY
| VAS_EBOOT_VGA_IO_MISC_NEGATIVE_HORIZ_POLARITY
| VAS_EBOOT_VGA_IO_MISC_UPPER_64K
| VAS_EBOOT_VGA_IO_MISC_EXTERNAL_CLOCK_0
| VAS_EBOOT_VGA_IO_MISC_28MHZ
| VAS_EBOOT_VGA_IO_MISC_ENABLE_VRAM_ACCESS
| VAS_EBOOT_VGA_IO_MISC_COLOR,
VAS_EBOOT_VGA_IO_MISC_WRITE + VAS_EBOOT_MACHINE_PCI_IO_BASE);
VasEBoot_vga_sr_write (0x86, 5);
for (i = 6; i <= 0x27; i++)
VasEBoot_vga_sr_write (0, i);
for (i = 0x31; i <= 0x3d; i++)
VasEBoot_vga_sr_write (0, i);
for (i = 0; i < ARRAY_SIZE (sr_dump); i++)
VasEBoot_vga_sr_write (sr_dump[i].val, sr_dump[i].reg);
for (i = 0x30; i < 0x40; i++)
VasEBoot_vga_cr_write (0, i);
VasEBoot_vga_cr_write (0x77, 0x40);
VasEBoot_vga_cr_write (0x77, 0x41);
VasEBoot_vga_cr_write (0x00, 0x42);
VasEBoot_vga_cr_write (0x5b, 0x43);
VasEBoot_vga_cr_write (0x00, 0x44);
VasEBoot_vga_cr_write (0x23, 0x48);
VasEBoot_vga_cr_write (0xaa, 0x49);
VasEBoot_vga_cr_write (0x02, 0x37);
VasEBoot_vga_cr_write (0x20, 0x5b);
VasEBoot_vga_cr_write (0x00, 0x83);
VasEBoot_vga_cr_write (0x80, 0x63);
VasEBoot_vga_cr_write (0x0c, VAS_EBOOT_VGA_CR_VSYNC_END);
VasEBoot_vga_cr_write (0x5f, VAS_EBOOT_VGA_CR_HTOTAL);
VasEBoot_vga_cr_write (0x4f, VAS_EBOOT_VGA_CR_HORIZ_END);
VasEBoot_vga_cr_write (0x50, VAS_EBOOT_VGA_CR_HBLANK_START);
VasEBoot_vga_cr_write (0x82, VAS_EBOOT_VGA_CR_HBLANK_END);
VasEBoot_vga_cr_write (0x54, VAS_EBOOT_VGA_CR_HORIZ_SYNC_PULSE_START);
VasEBoot_vga_cr_write (0x80, VAS_EBOOT_VGA_CR_HORIZ_SYNC_PULSE_END);
VasEBoot_vga_cr_write (0x0b, VAS_EBOOT_VGA_CR_VERT_TOTAL);
VasEBoot_vga_cr_write (0x3e, VAS_EBOOT_VGA_CR_OVERFLOW);
VasEBoot_vga_cr_write (0x00, VAS_EBOOT_VGA_CR_BYTE_PANNING);
VasEBoot_vga_cr_write (0x40, VAS_EBOOT_VGA_CR_CELL_HEIGHT);
VasEBoot_vga_cr_write (0x00, VAS_EBOOT_VGA_CR_CURSOR_START);
VasEBoot_vga_cr_write (0x00, VAS_EBOOT_VGA_CR_CURSOR_END);
VasEBoot_vga_cr_write (0x00, VAS_EBOOT_VGA_CR_START_ADDR_HIGH_REGISTER);
VasEBoot_vga_cr_write (0x00, VAS_EBOOT_VGA_CR_START_ADDR_LOW_REGISTER);
VasEBoot_vga_cr_write (0x00, VAS_EBOOT_VGA_CR_CURSOR_ADDR_HIGH);
VasEBoot_vga_cr_write (0x00, VAS_EBOOT_VGA_CR_CURSOR_ADDR_LOW);
VasEBoot_vga_cr_write (0xea, VAS_EBOOT_VGA_CR_VSYNC_START);
VasEBoot_vga_cr_write (0x8c, VAS_EBOOT_VGA_CR_VSYNC_END);
VasEBoot_vga_cr_write (0xdf, VAS_EBOOT_VGA_CR_VDISPLAY_END);
VasEBoot_vga_cr_write (0x28, VAS_EBOOT_VGA_CR_PITCH);
VasEBoot_vga_cr_write (0x40, VAS_EBOOT_VGA_CR_UNDERLINE_LOCATION);
VasEBoot_vga_cr_write (0xe7, VAS_EBOOT_VGA_CR_VERTICAL_BLANK_START);
VasEBoot_vga_cr_write (0x04, VAS_EBOOT_VGA_CR_VERTICAL_BLANK_END);
VasEBoot_vga_cr_write (0xa3, VAS_EBOOT_VGA_CR_MODE);
VasEBoot_vga_cr_write (0xff, VAS_EBOOT_VGA_CR_LINE_COMPARE);
VasEBoot_vga_cr_write (0x0c, VAS_EBOOT_VGA_CR_VSYNC_END);
VasEBoot_vga_cr_write (0x5f, VAS_EBOOT_VGA_CR_HTOTAL);
VasEBoot_vga_cr_write (0x4f, VAS_EBOOT_VGA_CR_HORIZ_END);
VasEBoot_vga_cr_write (0x50, VAS_EBOOT_VGA_CR_HBLANK_START);
VasEBoot_vga_cr_write (0x82, VAS_EBOOT_VGA_CR_HBLANK_END);
VasEBoot_vga_cr_write (0x55, VAS_EBOOT_VGA_CR_HORIZ_SYNC_PULSE_START);
VasEBoot_vga_cr_write (0x81, VAS_EBOOT_VGA_CR_HORIZ_SYNC_PULSE_END);
VasEBoot_vga_cr_write (0x0b, VAS_EBOOT_VGA_CR_VERT_TOTAL);
VasEBoot_vga_cr_write (0x3e, VAS_EBOOT_VGA_CR_OVERFLOW);
VasEBoot_vga_cr_write (0xe9, VAS_EBOOT_VGA_CR_VSYNC_START);
VasEBoot_vga_cr_write (0x8b, VAS_EBOOT_VGA_CR_VSYNC_END);
VasEBoot_vga_cr_write (0xdf, VAS_EBOOT_VGA_CR_VDISPLAY_END);
VasEBoot_vga_cr_write (0xe7, VAS_EBOOT_VGA_CR_VERTICAL_BLANK_START);
VasEBoot_vga_cr_write (0x04, VAS_EBOOT_VGA_CR_VERTICAL_BLANK_END);
VasEBoot_vga_cr_write (0x40, VAS_EBOOT_VGA_CR_CELL_HEIGHT);
VasEBoot_vga_cr_write (0x50, VAS_EBOOT_VGA_CR_PITCH);
VasEBoot_vga_cr_write (0x00, 0x19);
VasEBoot_vga_cr_write (0x00, 0x1a);
VasEBoot_vga_cr_write (0x6c, 0x52);
VasEBoot_vga_cr_write (0x2e, 0x34);
VasEBoot_vga_cr_write (0x00, 0x31);
VasEBoot_vga_cr_write (0, VAS_EBOOT_VGA_CR_START_ADDR_HIGH_REGISTER);
VasEBoot_vga_cr_write (0, VAS_EBOOT_VGA_CR_START_ADDR_LOW_REGISTER);
for (i = 0; i < 16; i++)
VasEBoot_vga_write_arx (i, i);
VasEBoot_vga_write_arx (1, VAS_EBOOT_VGA_ARX_MODE);
VasEBoot_vga_write_arx (0, VAS_EBOOT_VGA_ARX_OVERSCAN);
VasEBoot_vga_write_arx (0, VAS_EBOOT_VGA_ARX_COLOR_PLANE_ENABLE);
VasEBoot_vga_write_arx (0, VAS_EBOOT_VGA_ARX_HORIZONTAL_PANNING);
VasEBoot_vga_write_arx (0, VAS_EBOOT_VGA_ARX_COLOR_SELECT);
VasEBoot_outb (0xff, VAS_EBOOT_VGA_IO_PIXEL_MASK + VAS_EBOOT_MACHINE_PCI_IO_BASE);
for (i = 0; i < ARRAY_SIZE (gr); i++)
VasEBoot_vga_gr_write (gr[i], i);
for (i = 0; i < VAS_EBOOT_VIDEO_FBSTD_NUMCOLORS; i++)
VasEBoot_vga_palette_write (i, VasEBoot_video_fbstd_colors[i].r,
VasEBoot_video_fbstd_colors[i].g,
VasEBoot_video_fbstd_colors[i].b);
#if 1
{
if (read_sis_cmd (0x5) != 0xa1)
write_sis_cmd (0x86, 0x5);
write_sis_cmd (read_sis_cmd (0x20) | 0xa1, 0x20);
write_sis_cmd (read_sis_cmd (0x1e) | 0xda, 0x1e);
#define IND_SIS_CMDQUEUE_SET 0x26
#define IND_SIS_CMDQUEUE_THRESHOLD 0x27
#define COMMAND_QUEUE_THRESHOLD 0x1F
#define SIS_CMD_QUEUE_RESET 0x01
#define SIS_AGP_CMDQUEUE_ENABLE 0x80 /* 315/330/340 series SR26 */
#define SIS_VRAM_CMDQUEUE_ENABLE 0x40
#define SIS_MMIO_CMD_ENABLE 0x20
#define SIS_CMD_QUEUE_SIZE_512k 0x00
#define SIS_CMD_QUEUE_SIZE_1M 0x04
#define SIS_CMD_QUEUE_SIZE_2M 0x08
#define SIS_CMD_QUEUE_SIZE_4M 0x0C
#define SIS_CMD_QUEUE_RESET 0x01
#define SIS_CMD_AUTO_CORR 0x02
write_sis_cmd (COMMAND_QUEUE_THRESHOLD, IND_SIS_CMDQUEUE_THRESHOLD);
write_sis_cmd (SIS_CMD_QUEUE_RESET, IND_SIS_CMDQUEUE_SET);
framebuffer.mmioptr[0x85C4 / 4] = framebuffer.mmioptr[0x85C8 / 4];
write_sis_cmd (SIS_MMIO_CMD_ENABLE | SIS_CMD_AUTO_CORR, IND_SIS_CMDQUEUE_SET);
framebuffer.mmioptr[0x85C0 / 4] = (0x1000000 - (512 * 1024));
}
#endif
#ifndef TEST
err = VasEBoot_video_fb_setup (mode_type, mode_mask,
&framebuffer.mode_info,
framebuffer.ptr, NULL, NULL);
if (err)
return err;
/* Copy default palette to initialize emulated palette. */
err = VasEBoot_video_fb_set_palette (0, VAS_EBOOT_VIDEO_FBSTD_EXT_NUMCOLORS,
VasEBoot_video_fbstd_colors);
#endif
return err;
}
#ifndef TEST
static VasEBoot_err_t
VasEBoot_video_sis315pro_swap_buffers (void)
{
VasEBoot_size_t s;
s = (framebuffer.mode_info.height
* framebuffer.mode_info.pitch
* framebuffer.mode_info.bytes_per_pixel);
VasEBoot_video_fb_swap_buffers ();
VasEBoot_arch_sync_dma_caches (framebuffer.ptr, s);
return VAS_EBOOT_ERR_NONE;
}
static VasEBoot_err_t
VasEBoot_video_sis315pro_get_info_and_fini (struct VasEBoot_video_mode_info *mode_info,
void **framebuf)
{
VasEBoot_memcpy (mode_info, &(framebuffer.mode_info), sizeof (*mode_info));
*framebuf = (void *) framebuffer.direct_ptr;
VasEBoot_video_fb_fini ();
return VAS_EBOOT_ERR_NONE;
}
static struct VasEBoot_video_adapter VasEBoot_video_sis315pro_adapter =
{
.name = "SIS315PRO Video Driver",
.id = VAS_EBOOT_VIDEO_DRIVER_SIS315PRO,
.prio = VAS_EBOOT_VIDEO_ADAPTER_PRIO_NATIVE,
.init = VasEBoot_video_sis315pro_video_init,
.fini = VasEBoot_video_sis315pro_video_fini,
.setup = VasEBoot_video_sis315pro_setup,
.get_info = VasEBoot_video_fb_get_info,
.get_info_and_fini = VasEBoot_video_sis315pro_get_info_and_fini,
.set_palette = VasEBoot_video_fb_set_palette,
.get_palette = VasEBoot_video_fb_get_palette,
.set_viewport = VasEBoot_video_fb_set_viewport,
.get_viewport = VasEBoot_video_fb_get_viewport,
.set_region = VasEBoot_video_fb_set_region,
.get_region = VasEBoot_video_fb_get_region,
.set_area_status = VasEBoot_video_fb_set_area_status,
.get_area_status = VasEBoot_video_fb_get_area_status,
.map_color = VasEBoot_video_fb_map_color,
.map_rgb = VasEBoot_video_fb_map_rgb,
.map_rgba = VasEBoot_video_fb_map_rgba,
.unmap_color = VasEBoot_video_fb_unmap_color,
.fill_rect = VasEBoot_video_fb_fill_rect,
.blit_bitmap = VasEBoot_video_fb_blit_bitmap,
.blit_render_target = VasEBoot_video_fb_blit_render_target,
.scroll = VasEBoot_video_fb_scroll,
.swap_buffers = VasEBoot_video_sis315pro_swap_buffers,
.create_render_target = VasEBoot_video_fb_create_render_target,
.delete_render_target = VasEBoot_video_fb_delete_render_target,
.set_active_render_target = VasEBoot_video_fb_set_active_render_target,
.get_active_render_target = VasEBoot_video_fb_get_active_render_target,
.next = 0
};
VAS_EBOOT_MOD_INIT(video_sis315pro)
{
VasEBoot_video_register (&VasEBoot_video_sis315pro_adapter);
}
VAS_EBOOT_MOD_FINI(video_sis315pro)
{
VasEBoot_video_unregister (&VasEBoot_video_sis315pro_adapter);
}
#else
int
main ()
{
VasEBoot_video_sis315pro_setup (640, 400, 0, 0);
}
#endif