170688Snsouch/*-
270688Snsouch * Copyright (c) 2000 Alcove - Nicolas Souchu <nsouch@freebsd.org>
370688Snsouch * All rights reserved.
470688Snsouch *
570688Snsouch * Code based on Peter Horton <pdh@colonel-panic.com> patch.
670688Snsouch *
770688Snsouch * Redistribution and use in source and binary forms, with or without
870688Snsouch * modification, are permitted provided that the following conditions
970688Snsouch * are met:
1070688Snsouch * 1. Redistributions of source code must retain the above copyright
1170688Snsouch *    notice, this list of conditions and the following disclaimer.
1270688Snsouch * 2. Redistributions in binary form must reproduce the above copyright
1370688Snsouch *    notice, this list of conditions and the following disclaimer in the
1470688Snsouch *    documentation and/or other materials provided with the distribution.
1570688Snsouch *
1670688Snsouch * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1770688Snsouch * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1870688Snsouch * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1970688Snsouch * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2070688Snsouch * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2170688Snsouch * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2270688Snsouch * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2370688Snsouch * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2470688Snsouch * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2570688Snsouch * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2670688Snsouch * SUCH DAMAGE.
2770688Snsouch */
2870688Snsouch
29119418Sobrien#include <sys/cdefs.h>
30119418Sobrien__FBSDID("$FreeBSD: releng/11.0/sys/dev/fb/s3_pci.c 295790 2016-02-19 03:37:56Z jhibbits $");
31119418Sobrien
3270688Snsouch/* Enable LFB on S3 cards that has only VESA 1.2 BIOS */
3370688Snsouch
3470688Snsouch#include <sys/param.h>
3570688Snsouch#include <sys/systm.h>
3670688Snsouch#include <sys/kernel.h>
3770688Snsouch#include <machine/bus.h>
3870688Snsouch
3970688Snsouch#include <vm/vm.h>
4070688Snsouch#include <vm/vm_extern.h>
4170688Snsouch#include <vm/vm_kern.h>
4270688Snsouch#include <vm/pmap.h>
4370688Snsouch
4470688Snsouch#include <sys/uio.h>
4570688Snsouch#include <sys/module.h>
4670688Snsouch#include <sys/bus.h>
4770688Snsouch#include <sys/rman.h>
4870688Snsouch#include <machine/resource.h>
4970688Snsouch
5070688Snsouch#include <sys/malloc.h>
5170688Snsouch#include <sys/fbio.h>
5270688Snsouch
53119277Simp#include <dev/pci/pcireg.h>
54119277Simp#include <dev/pci/pcivar.h>
5570688Snsouch
5670688Snsouch#include <machine/md_var.h>
5770688Snsouch#include <machine/pc/bios.h>
58197025Sdelphij#include <dev/fb/vesa.h>
5970688Snsouch
6070688Snsouch#include <dev/fb/fbreg.h>
6170688Snsouch#include <dev/fb/vgareg.h>
6270688Snsouch
6370688Snsouch#define S3PCI_DEBUG 1
6470688Snsouch
6570688Snsouch#define PCI_S3_VENDOR_ID	0x5333
6670688Snsouch
6770688Snsouch#define S3_CONFIG_IO		0x3c0	/* VGA standard config io ports */
6870688Snsouch#define S3_CONFIG_IO_SIZE	0x20
6970688Snsouch
7070688Snsouch#define S3_ENHANCED_IO		0x4ae8	/* Extended config register */
7170688Snsouch#define S3_ENHANCED_IO_SIZE	1
7270688Snsouch
7370688Snsouch#define S3_CRTC_ADDR		0x14
7470688Snsouch#define S3_CRTC_VALUE		0x15
7570688Snsouch
7670688Snsouch#define PCI_BASE_MEMORY		0x10
7770688Snsouch
7870688Snsouch#define outb_p(value, offset) bus_space_write_1(sc->st, sc->sh, offset, value)
7970688Snsouch#define inb_p(offset) (bus_space_read_1(sc->st, sc->sh, offset))
8070688Snsouch#define outb_enh(value, offset) bus_space_write_1(sc->enh_st, sc->enh_sh, \
8170688Snsouch								offset, value)
8270688Snsouch#define inb_enh(offset) (bus_space_read_1(sc->enh_st, sc->enh_sh, offset))
8370688Snsouch
8470688Snsouchstruct s3pci_softc {
8570688Snsouch	bus_space_tag_t st;
8670688Snsouch	bus_space_handle_t sh;
8770688Snsouch	bus_space_tag_t enh_st;
8870688Snsouch	bus_space_handle_t enh_sh;
8970688Snsouch	struct resource *port_res;
9070688Snsouch	struct resource *enh_res;
9170688Snsouch	struct resource *mem_res;
9270688Snsouch	u_long mem_base;
9370688Snsouch	u_long mem_size;
9470688Snsouch};
9570688Snsouch
9670688Snsouchstatic int			s3lfb_error(void);
9770688Snsouchstatic vi_probe_t		s3lfb_probe;
9870688Snsouchstatic vi_init_t		s3lfb_init;
9970688Snsouchstatic vi_get_info_t		s3lfb_get_info;
10070688Snsouchstatic vi_query_mode_t		s3lfb_query_mode;
10170688Snsouchstatic vi_set_mode_t		s3lfb_set_mode;
10270688Snsouchstatic vi_save_font_t		s3lfb_save_font;
10370688Snsouchstatic vi_load_font_t		s3lfb_load_font;
10470688Snsouchstatic vi_show_font_t		s3lfb_show_font;
10570688Snsouchstatic vi_save_palette_t	s3lfb_save_palette;
10670688Snsouchstatic vi_load_palette_t	s3lfb_load_palette;
10770688Snsouchstatic vi_set_border_t		s3lfb_set_border;
10870688Snsouchstatic vi_save_state_t		s3lfb_save_state;
10970688Snsouchstatic vi_load_state_t		s3lfb_load_state;
11070688Snsouchstatic vi_set_win_org_t		s3lfb_set_origin;
11170688Snsouchstatic vi_read_hw_cursor_t	s3lfb_read_hw_cursor;
11270688Snsouchstatic vi_set_hw_cursor_t	s3lfb_set_hw_cursor;
11370688Snsouchstatic vi_set_hw_cursor_shape_t	s3lfb_set_hw_cursor_shape;
11470688Snsouchstatic vi_blank_display_t	s3lfb_blank_display;
11570688Snsouchstatic vi_mmap_t		s3lfb_mmap;
11670688Snsouchstatic vi_ioctl_t		s3lfb_ioctl;
11770688Snsouchstatic vi_clear_t		s3lfb_clear;
11870688Snsouchstatic vi_fill_rect_t		s3lfb_fill_rect;
11970688Snsouchstatic vi_bitblt_t		s3lfb_bitblt;
12070688Snsouchstatic vi_diag_t		s3lfb_diag;
12170688Snsouch
12270688Snsouchstatic video_switch_t s3lfbvidsw = {
12370688Snsouch	s3lfb_probe,
12470688Snsouch	s3lfb_init,
12570688Snsouch	s3lfb_get_info,
12670688Snsouch	s3lfb_query_mode,
12770688Snsouch	s3lfb_set_mode,
12870688Snsouch	s3lfb_save_font,
12970688Snsouch	s3lfb_load_font,
13070688Snsouch	s3lfb_show_font,
13170688Snsouch	s3lfb_save_palette,
13270688Snsouch	s3lfb_load_palette,
13370688Snsouch	s3lfb_set_border,
13470688Snsouch	s3lfb_save_state,
13570688Snsouch	s3lfb_load_state,
13670688Snsouch	s3lfb_set_origin,
13770688Snsouch	s3lfb_read_hw_cursor,
13870688Snsouch	s3lfb_set_hw_cursor,
13970688Snsouch	s3lfb_set_hw_cursor_shape,
14070688Snsouch	s3lfb_blank_display,
14170688Snsouch	s3lfb_mmap,
14270688Snsouch	s3lfb_ioctl,
14370688Snsouch	s3lfb_clear,
14470688Snsouch	s3lfb_fill_rect,
14570688Snsouch	s3lfb_bitblt,
14670688Snsouch	s3lfb_error,
14770688Snsouch	s3lfb_error,
14870688Snsouch	s3lfb_diag,
14970688Snsouch};
15070688Snsouch
15170688Snsouchstatic video_switch_t *prevvidsw;
15270688Snsouchstatic device_t s3pci_dev = NULL;
15370688Snsouch
15470688Snsouchstatic int
15570688Snsouchs3lfb_probe(int unit, video_adapter_t **adpp, void *arg, int flags)
15670688Snsouch{
15770688Snsouch	return (*prevvidsw->probe)(unit, adpp, arg, flags);
15870688Snsouch}
15970688Snsouch
16070688Snsouchstatic int
16170688Snsouchs3lfb_init(int unit, video_adapter_t *adp, int flags)
16270688Snsouch{
16370688Snsouch	return (*prevvidsw->init)(unit, adp, flags);
16470688Snsouch}
16570688Snsouch
16670688Snsouchstatic int
16770688Snsouchs3lfb_get_info(video_adapter_t *adp, int mode, video_info_t *info)
16870688Snsouch{
16971465Sjhb#if 0
17070688Snsouch	device_t dev = s3pci_dev;			/* XXX */
17170688Snsouch	struct s3pci_softc *sc = (struct s3pci_softc *)device_get_softc(dev);
17271465Sjhb#endif
17370688Snsouch	int error;
17470688Snsouch
17570688Snsouch	if ((error = (*prevvidsw->get_info)(adp, mode, info)))
17670688Snsouch		return error;
17770688Snsouch
17870688Snsouch#if 0
17970688Snsouch	/* Don't use linear addressing with text modes
18070688Snsouch	 */
18170688Snsouch	if ((mode > M_VESA_BASE) &&
18270688Snsouch		(info->vi_flags & V_INFO_GRAPHICS) &&
18370688Snsouch		!(info->vi_flags & V_INFO_LINEAR)) {
18470688Snsouch
18570688Snsouch		info->vi_flags |= V_INFO_LINEAR;
18670688Snsouch		info->vi_buffer = sc->mem_base;
18770688Snsouch
18870688Snsouch	} else {
18970688Snsouch		info->vi_buffer = 0;
19070688Snsouch	}
19170688Snsouch#endif
19270688Snsouch
19370688Snsouch	return 0;
19470688Snsouch}
19570688Snsouch
19670688Snsouchstatic int
19770688Snsouchs3lfb_query_mode(video_adapter_t *adp, video_info_t *info)
19870688Snsouch{
19970688Snsouch	return (*prevvidsw->query_mode)(adp, info);
20070688Snsouch}
20170688Snsouch
20270688Snsouchstatic vm_offset_t
20370688Snsouchs3lfb_map_buffer(u_int paddr, size_t size)
20470688Snsouch{
20570688Snsouch	vm_offset_t vaddr;
20670688Snsouch	u_int off;
20770688Snsouch
20870688Snsouch	off = paddr - trunc_page(paddr);
20970688Snsouch	vaddr = (vm_offset_t)pmap_mapdev(paddr - off, size + off);
21070688Snsouch
21170688Snsouch	return (vaddr + off);
21270688Snsouch}
21370688Snsouch
21470688Snsouchstatic int
21570688Snsouchs3lfb_set_mode(video_adapter_t *adp, int mode)
21670688Snsouch{
21770688Snsouch	device_t dev = s3pci_dev;			/* XXX */
21870688Snsouch	struct s3pci_softc *sc = (struct s3pci_softc *)device_get_softc(dev);
21971465Sjhb#if 0
22070688Snsouch	unsigned char tmp;
22171465Sjhb#endif
22270688Snsouch	int error;
22370688Snsouch
22470688Snsouch	/* First, set the mode as if it was a classic VESA card
22570688Snsouch	 */
22670688Snsouch	if ((error = (*prevvidsw->set_mode)(adp, mode)))
22770688Snsouch		return error;
22870688Snsouch
22970688Snsouch	/* If not in a linear mode (according to s3lfb_get_info() called
23070688Snsouch	 * by vesa_set_mode in the (*vidsw[adp->va_index]->get_info)...
23170688Snsouch	 * sequence, return with no error
23270688Snsouch	 */
23370688Snsouch#if 0
23470688Snsouch	if (!(adp->va_info.vi_flags & V_INFO_LINEAR))
23570688Snsouch		return 0;
23670688Snsouch#endif
23770688Snsouch
23870688Snsouch	if ((mode <= M_VESA_BASE) ||
23970688Snsouch		!(adp->va_info.vi_flags & V_INFO_GRAPHICS) ||
24070688Snsouch		(adp->va_info.vi_flags & V_INFO_LINEAR))
24170688Snsouch		return 0;
24270688Snsouch
24370688Snsouch	/* Ok, now apply the configuration to the card */
24470688Snsouch
24570688Snsouch	outb_p(0x38, S3_CRTC_ADDR); outb_p(0x48, S3_CRTC_VALUE);
24670688Snsouch	outb_p(0x39, S3_CRTC_ADDR); outb_p(0xa5, S3_CRTC_VALUE);
24770688Snsouch
24870688Snsouch       /* check that CR47 is read/write */
24970688Snsouch
25070688Snsouch#if 0
25170688Snsouch	outb_p(0x47, S3_CRTC_ADDR); outb_p(0xff, S3_CRTC_VALUE);
25270688Snsouch	tmp = inb_p(S3_CRTC_VALUE);
25370688Snsouch	outb_p(0x00, S3_CRTC_VALUE);
25470688Snsouch	if ((tmp != 0xff) || (inb_p(S3_CRTC_VALUE)))
25570688Snsouch	{
25670688Snsouch		/* lock S3 registers */
25770688Snsouch
25870688Snsouch		outb_p(0x39, S3_CRTC_ADDR); outb_p(0x5a, S3_CRTC_VALUE);
25970688Snsouch		outb_p(0x38, S3_CRTC_ADDR); outb_p(0x00, S3_CRTC_VALUE);
26070688Snsouch
26170688Snsouch		return ENXIO;
26270688Snsouch	}
26370688Snsouch#endif
26470688Snsouch
26570688Snsouch	/* enable enhanced register access */
26670688Snsouch
26770688Snsouch	outb_p(0x40, S3_CRTC_ADDR);
26870688Snsouch	outb_p(inb_p(S3_CRTC_VALUE) | 1, S3_CRTC_VALUE);
26970688Snsouch
27070688Snsouch	/* enable enhanced functions */
27170688Snsouch
27270688Snsouch	outb_enh(inb_enh(0) | 1, 0x0);
27370688Snsouch
27470688Snsouch	/* enable enhanced mode memory mapping */
27570688Snsouch
27670688Snsouch	outb_p(0x31, S3_CRTC_ADDR);
27770688Snsouch	outb_p(inb_p(S3_CRTC_VALUE) | 8, S3_CRTC_VALUE);
27870688Snsouch
27970688Snsouch	/* enable linear frame buffer and set address window to max */
28070688Snsouch
28170688Snsouch	outb_p(0x58, S3_CRTC_ADDR);
28270688Snsouch	outb_p(inb_p(S3_CRTC_VALUE) | 0x13, S3_CRTC_VALUE);
28370688Snsouch
28470688Snsouch	/* disabled enhanced register access */
28570688Snsouch
28670688Snsouch	outb_p(0x40, S3_CRTC_ADDR);
28770688Snsouch	outb_p(inb_p(S3_CRTC_VALUE) & ~1, S3_CRTC_VALUE);
28870688Snsouch
28970688Snsouch	/* lock S3 registers */
29070688Snsouch
29170688Snsouch	outb_p(0x39, S3_CRTC_ADDR); outb_p(0x5a, S3_CRTC_VALUE);
29270688Snsouch	outb_p(0x38, S3_CRTC_ADDR); outb_p(0x00, S3_CRTC_VALUE);
29370688Snsouch
29470688Snsouch	adp->va_info.vi_flags |= V_INFO_LINEAR;
29570688Snsouch	adp->va_info.vi_buffer = sc->mem_base;
29670688Snsouch	adp->va_buffer = s3lfb_map_buffer(adp->va_info.vi_buffer,
29770688Snsouch				adp->va_info.vi_buffer_size);
29870688Snsouch	adp->va_buffer_size = adp->va_info.vi_buffer_size;
29970688Snsouch	adp->va_window = adp->va_buffer;
30070688Snsouch	adp->va_window_size = adp->va_info.vi_buffer_size/adp->va_info.vi_planes;
30170688Snsouch	adp->va_window_gran = adp->va_info.vi_buffer_size/adp->va_info.vi_planes;
30270688Snsouch
30370688Snsouch	return 0;
30470688Snsouch}
30570688Snsouch
30670688Snsouchstatic int
307150686Smariuss3lfb_save_font(video_adapter_t *adp, int page, int fontsize, int fontwidth,
308150686Smarius	       u_char *data, int ch, int count)
30970688Snsouch{
310150686Smarius	return (*prevvidsw->save_font)(adp, page, fontsize, fontwidth, data,
311150686Smarius		ch, count);
31270688Snsouch}
31370688Snsouch
31470688Snsouchstatic int
315150686Smariuss3lfb_load_font(video_adapter_t *adp, int page, int fontsize, int fontwidth,
316150686Smarius	       u_char *data, int ch, int count)
31770688Snsouch{
318150686Smarius	return (*prevvidsw->load_font)(adp, page, fontsize, fontwidth, data,
319150686Smarius		ch, count);
32070688Snsouch}
32170688Snsouch
32270688Snsouchstatic int
32370688Snsouchs3lfb_show_font(video_adapter_t *adp, int page)
32470688Snsouch{
32570688Snsouch	return (*prevvidsw->show_font)(adp, page);
32670688Snsouch}
32770688Snsouch
32870688Snsouchstatic int
32970688Snsouchs3lfb_save_palette(video_adapter_t *adp, u_char *palette)
33070688Snsouch{
33170688Snsouch	return (*prevvidsw->save_palette)(adp, palette);
33270688Snsouch}
33370688Snsouch
33470688Snsouchstatic int
33570688Snsouchs3lfb_load_palette(video_adapter_t *adp, u_char *palette)
33670688Snsouch{
33770688Snsouch	return (*prevvidsw->load_palette)(adp, palette);
33870688Snsouch}
33970688Snsouch
34070688Snsouchstatic int
34170688Snsouchs3lfb_set_border(video_adapter_t *adp, int color)
34270688Snsouch{
34370688Snsouch	return (*prevvidsw->set_border)(adp, color);
34470688Snsouch}
34570688Snsouch
34670688Snsouchstatic int
34770688Snsouchs3lfb_save_state(video_adapter_t *adp, void *p, size_t size)
34870688Snsouch{
34970688Snsouch	return (*prevvidsw->save_state)(adp, p, size);
35070688Snsouch}
35170688Snsouch
35270688Snsouchstatic int
35370688Snsouchs3lfb_load_state(video_adapter_t *adp, void *p)
35470688Snsouch{
35570688Snsouch	return (*prevvidsw->load_state)(adp, p);
35670688Snsouch}
35770688Snsouch
35870688Snsouchstatic int
35970688Snsouchs3lfb_set_origin(video_adapter_t *adp, off_t offset)
36070688Snsouch{
36170688Snsouch	return (*prevvidsw->set_win_org)(adp, offset);
36270688Snsouch}
36370688Snsouch
36470688Snsouchstatic int
36570688Snsouchs3lfb_read_hw_cursor(video_adapter_t *adp, int *col, int *row)
36670688Snsouch{
36770688Snsouch	return (*prevvidsw->read_hw_cursor)(adp, col, row);
36870688Snsouch}
36970688Snsouch
37070688Snsouchstatic int
37170688Snsouchs3lfb_set_hw_cursor(video_adapter_t *adp, int col, int row)
37270688Snsouch{
37370688Snsouch	return (*prevvidsw->set_hw_cursor)(adp, col, row);
37470688Snsouch}
37570688Snsouch
37670688Snsouchstatic int
37770688Snsouchs3lfb_set_hw_cursor_shape(video_adapter_t *adp, int base, int height,
37870688Snsouch			 int celsize, int blink)
37970688Snsouch{
38070688Snsouch	return (*prevvidsw->set_hw_cursor_shape)(adp, base, height,
38170688Snsouch			celsize, blink);
38270688Snsouch}
38370688Snsouch
38470688Snsouchstatic int
38570688Snsouchs3lfb_blank_display(video_adapter_t *adp, int mode)
38670688Snsouch{
38770688Snsouch	return (*prevvidsw->blank_display)(adp, mode);
38870688Snsouch}
38970688Snsouch
39070688Snsouchstatic int
391201223Srnolands3lfb_mmap(video_adapter_t *adp, vm_ooffset_t offset, vm_paddr_t *paddr,
392201223Srnoland	  int prot, vm_memattr_t *memattr)
39370688Snsouch{
394201223Srnoland	return (*prevvidsw->mmap)(adp, offset, paddr, prot, memattr);
39570688Snsouch}
39670688Snsouch
39770688Snsouchstatic int
39870688Snsouchs3lfb_clear(video_adapter_t *adp)
39970688Snsouch{
40070688Snsouch	return (*prevvidsw->clear)(adp);
40170688Snsouch}
40270688Snsouch
40370688Snsouchstatic int
40470688Snsouchs3lfb_fill_rect(video_adapter_t *adp, int val, int x, int y, int cx, int cy)
40570688Snsouch{
40670688Snsouch	return (*prevvidsw->fill_rect)(adp, val, x, y, cx, cy);
40770688Snsouch}
40870688Snsouch
40970688Snsouchstatic int
41070688Snsouchs3lfb_bitblt(video_adapter_t *adp,...)
41170688Snsouch{
41270688Snsouch	return (*prevvidsw->bitblt)(adp);		/* XXX */
41370688Snsouch}
41470688Snsouch
41570688Snsouchstatic int
41670688Snsouchs3lfb_ioctl(video_adapter_t *adp, u_long cmd, caddr_t arg)
41770688Snsouch{
41870688Snsouch	return (*prevvidsw->ioctl)(adp, cmd, arg);
41970688Snsouch}
42070688Snsouch
42170688Snsouchstatic int
42270688Snsouchs3lfb_diag(video_adapter_t *adp, int level)
42370688Snsouch{
42470688Snsouch	return (*prevvidsw->diag)(adp, level);
42570688Snsouch}
42670688Snsouch
42770688Snsouchstatic int
42870688Snsouchs3lfb_error(void)
42970688Snsouch{
43070688Snsouch	return 1;
43170688Snsouch}
43270688Snsouch
43370688Snsouch/***********************************/
43470688Snsouch/* PCI detection/attachement stuff */
43570688Snsouch/***********************************/
43670688Snsouch
43770688Snsouchstatic int
43870688Snsouchs3pci_probe(device_t dev)
43970688Snsouch{
44070688Snsouch	u_int32_t vendor, class, subclass, device_id;
44170688Snsouch
44270688Snsouch	device_id = pci_get_devid(dev);
44370688Snsouch	vendor = device_id & 0xffff;
44470688Snsouch	class = pci_get_class(dev);
44570688Snsouch	subclass = pci_get_subclass(dev);
44670688Snsouch
44770688Snsouch	if ((class != PCIC_DISPLAY) || (subclass != PCIS_DISPLAY_VGA) ||
44870688Snsouch		(vendor != PCI_S3_VENDOR_ID))
44970688Snsouch		return ENXIO;
45070688Snsouch
45170688Snsouch	device_set_desc(dev, "S3 graphic card");
45270688Snsouch
45370688Snsouch	bus_set_resource(dev, SYS_RES_IOPORT, 0,
45470688Snsouch				S3_CONFIG_IO, S3_CONFIG_IO_SIZE);
45570688Snsouch	bus_set_resource(dev, SYS_RES_IOPORT, 1,
45670688Snsouch				S3_ENHANCED_IO, S3_ENHANCED_IO_SIZE);
45770688Snsouch
458143161Simp	return BUS_PROBE_DEFAULT;
45970688Snsouch
46070688Snsouch};
46170688Snsouch
46270688Snsouchstatic int
46370688Snsouchs3pci_attach(device_t dev)
46470688Snsouch{
46570688Snsouch	struct s3pci_softc* sc = (struct s3pci_softc*)device_get_softc(dev);
46670688Snsouch	video_adapter_t *adp;
46770688Snsouch
46870688Snsouch#if 0
46970688Snsouch	unsigned char tmp;
47070688Snsouch#endif
47170688Snsouch	int rid, i;
47270688Snsouch
47370688Snsouch	if (s3pci_dev) {
47487599Sobrien		printf("%s: driver already attached!\n", __func__);
47570688Snsouch		goto error;
47670688Snsouch	}
47770688Snsouch
47870688Snsouch	/* Allocate resources
47970688Snsouch	 */
48070688Snsouch	rid = 0;
481295790Sjhibbits	if (!(sc->port_res = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid,
482295790Sjhibbits				RF_ACTIVE | RF_SHAREABLE))) {
48387599Sobrien		printf("%s: port resource allocation failed!\n", __func__);
48470688Snsouch		goto error;
48570688Snsouch	}
48670688Snsouch	sc->st = rman_get_bustag(sc->port_res);
48770688Snsouch	sc->sh = rman_get_bushandle(sc->port_res);
48870688Snsouch
48970688Snsouch	rid = 1;
490295790Sjhibbits	if (!(sc->enh_res = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid,
491295790Sjhibbits				RF_ACTIVE | RF_SHAREABLE))) {
49270688Snsouch		printf("%s: enhanced port resource allocation failed!\n",
49387599Sobrien			__func__);
49470688Snsouch		goto error;
49570688Snsouch	}
49670688Snsouch	sc->enh_st = rman_get_bustag(sc->enh_res);
49770688Snsouch	sc->enh_sh = rman_get_bushandle(sc->enh_res);
49870688Snsouch
49970688Snsouch	rid = PCI_BASE_MEMORY;
500127135Snjl	if (!(sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
501127135Snjl				 RF_ACTIVE))) {
50270688Snsouch
50387599Sobrien		printf("%s: mem resource allocation failed!\n", __func__);
50470688Snsouch		goto error;
50570688Snsouch	}
50670688Snsouch
50770688Snsouch	/* The memory base address will be our LFB base address
50870688Snsouch	 */
50970688Snsouch	/* sc->mem_base = (u_long)rman_get_virtual(sc->mem_res); */
51070688Snsouch	sc->mem_base = bus_get_resource_start(dev, SYS_RES_MEMORY, rid);
51170688Snsouch	sc->mem_size = bus_get_resource_count(dev, SYS_RES_MEMORY, rid);
51270688Snsouch
51370688Snsouch	/* Attach the driver to the VGA/VESA framework
51470688Snsouch	 */
51570688Snsouch	for (i = 0; (adp = vid_get_adapter(i)) != NULL; ++i) {
516234362Sjkim		if (adp->va_type == KD_VGA)
51770688Snsouch			break;
51870688Snsouch	}
51970688Snsouch
52070688Snsouch	/* If the VESA module hasn't been loaded, or VGA doesn't
52170688Snsouch	 * exist, abort
52270688Snsouch	 */
52370688Snsouch	if ((adp == NULL) || !(adp->va_flags & V_ADP_VESA)) {
52470688Snsouch		printf("%s: VGA adapter not found or VESA module not loaded!\n",
52587599Sobrien			__func__);
52670688Snsouch		goto error;
52770688Snsouch	}
52870688Snsouch
52970688Snsouch	/* Replace the VESA video switch by owers
53070688Snsouch	 */
53170688Snsouch	prevvidsw = vidsw[adp->va_index];
53270688Snsouch	vidsw[adp->va_index] = &s3lfbvidsw;
53370688Snsouch
53470688Snsouch	/* Remember who we are on the bus */
53570688Snsouch	s3pci_dev = (void *)dev;			/* XXX */
53670688Snsouch
53770688Snsouch	return 0;
53870688Snsouch
53970688Snsoucherror:
54070688Snsouch	if (sc->mem_res)
54170688Snsouch		bus_release_resource(dev, SYS_RES_MEMORY, PCI_BASE_MEMORY, sc->mem_res);
54270688Snsouch
54370688Snsouch	if (sc->enh_res)
54470688Snsouch		bus_release_resource(dev, SYS_RES_IOPORT, 1, sc->enh_res);
54570688Snsouch
54670688Snsouch	if (sc->port_res)
54770688Snsouch		bus_release_resource(dev, SYS_RES_IOPORT, 0, sc->port_res);
54870688Snsouch
54970688Snsouch	return ENXIO;
55070688Snsouch};
55170688Snsouch
55270688Snsouchstatic device_method_t s3pci_methods[] = {
55370688Snsouch
55470688Snsouch        DEVMETHOD(device_probe, s3pci_probe),
55570688Snsouch        DEVMETHOD(device_attach, s3pci_attach),
55670688Snsouch        {0,0}
55770688Snsouch};
55870688Snsouch
55970688Snsouchstatic driver_t s3pci_driver = {
56070688Snsouch	"s3pci",
56170688Snsouch	s3pci_methods,
56270688Snsouch	sizeof(struct s3pci_softc),
56370688Snsouch};
56470688Snsouch
56570688Snsouchstatic devclass_t s3pci_devclass;
56670688Snsouch
56770688SnsouchDRIVER_MODULE(s3pci, pci, s3pci_driver, s3pci_devclass, 0, 0);
568