Home | History | Annotate | Line # | Download | only in pci
sisfb.c revision 1.1.6.1
      1      1.1  bouyer /*	$OpenBSD: sisfb.c,v 1.2 2010/12/26 15:40:59 miod Exp $	*/
      2      1.1  bouyer 
      3      1.1  bouyer /*
      4      1.1  bouyer  * Copyright (c) 2010 Miodrag Vallat.
      5      1.1  bouyer  *
      6      1.1  bouyer  * Permission to use, copy, modify, and distribute this software for any
      7      1.1  bouyer  * purpose with or without fee is hereby granted, provided that the above
      8      1.1  bouyer  * copyright notice and this permission notice appear in all copies.
      9      1.1  bouyer  *
     10      1.1  bouyer  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
     11      1.1  bouyer  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     12      1.1  bouyer  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     13      1.1  bouyer  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     14      1.1  bouyer  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     15      1.1  bouyer  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
     16      1.1  bouyer  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     17      1.1  bouyer  */
     18      1.1  bouyer 
     19      1.1  bouyer /*
     20      1.1  bouyer  * Minimalistic driver for the SIS315 Pro frame buffer found on the
     21      1.1  bouyer  * Lemote Fuloong 2F systems.
     22      1.1  bouyer  * Does not support accelaration, mode change, secondary output, or
     23      1.1  bouyer  * anything fancy.
     24      1.1  bouyer  */
     25      1.1  bouyer 
     26      1.1  bouyer #include <sys/cdefs.h>
     27  1.1.6.1     mrg __KERNEL_RCSID(0, "$NetBSD: sisfb.c,v 1.1.6.1 2012/02/18 07:34:52 mrg Exp $");
     28      1.1  bouyer 
     29      1.1  bouyer #include <sys/param.h>
     30      1.1  bouyer #include <sys/systm.h>
     31      1.1  bouyer #include <sys/device.h>
     32      1.1  bouyer #include <sys/bus.h>
     33      1.1  bouyer 
     34      1.1  bouyer #include <uvm/uvm_extern.h>
     35      1.1  bouyer 
     36      1.1  bouyer #include <dev/pci/pcireg.h>
     37      1.1  bouyer #include <dev/pci/pcivar.h>
     38      1.1  bouyer #include <dev/pci/pcidevs.h>
     39      1.1  bouyer #include <dev/pci/pciio.h>
     40      1.1  bouyer 
     41      1.1  bouyer #include <dev/videomode/videomode.h>
     42      1.1  bouyer 
     43      1.1  bouyer #include <dev/wscons/wsdisplayvar.h>
     44      1.1  bouyer #include <dev/wscons/wsconsio.h>
     45      1.1  bouyer #include <dev/wsfont/wsfont.h>
     46      1.1  bouyer #include <dev/rasops/rasops.h>
     47      1.1  bouyer #include <dev/wscons/wsdisplay_vconsvar.h>
     48      1.1  bouyer #include <dev/pci/wsdisplay_pci.h>
     49      1.1  bouyer 
     50      1.1  bouyer #include <dev/i2c/i2cvar.h>
     51      1.1  bouyer 
     52      1.1  bouyer #include <dev/pci/sisfb.h>
     53      1.1  bouyer 
     54      1.1  bouyer struct sisfb_softc;
     55      1.1  bouyer 
     56      1.1  bouyer /* minimal frame buffer information, suitable for early console */
     57      1.1  bouyer 
     58      1.1  bouyer struct sisfb {
     59      1.1  bouyer 	struct sisfb_softc	*sc;
     60      1.1  bouyer 	uint8_t			 cmap[256 * 3];
     61      1.1  bouyer 
     62      1.1  bouyer 	bus_space_tag_t		 fbt;
     63      1.1  bouyer 	bus_space_handle_t	 fbh;
     64      1.1  bouyer 
     65      1.1  bouyer 	bus_space_tag_t		 mmiot;
     66      1.1  bouyer 	bus_space_handle_t	 mmioh;
     67      1.1  bouyer 
     68      1.1  bouyer 	bus_space_tag_t		 iot;
     69      1.1  bouyer 	bus_space_handle_t	 ioh;
     70      1.1  bouyer 
     71      1.1  bouyer 	struct vcons_screen	 vcs;
     72      1.1  bouyer 	struct wsscreen_descr	 wsd;
     73      1.1  bouyer 
     74      1.1  bouyer 	int			 fb_depth;
     75      1.1  bouyer 	int			 fb_width;
     76      1.1  bouyer 	int			 fb_height;
     77      1.1  bouyer 	int			 fb_stride;
     78      1.1  bouyer 	void 			 *fb_addr;
     79      1.1  bouyer };
     80      1.1  bouyer 
     81      1.1  bouyer struct sisfb_softc {
     82      1.1  bouyer 	device_t		 sc_dev;
     83      1.1  bouyer 	struct sisfb		*sc_fb;
     84      1.1  bouyer 	struct sisfb		 sc_fb_store;
     85      1.1  bouyer 
     86      1.1  bouyer 	struct vcons_data	vd;
     87      1.1  bouyer 	struct wsscreen_list	sc_wsl;
     88      1.1  bouyer 	const struct wsscreen_descr	*sc_scrlist[1];
     89      1.1  bouyer 	int			sc_nscr;
     90      1.1  bouyer 	int			sc_mode;
     91      1.1  bouyer };
     92      1.1  bouyer 
     93      1.1  bouyer int	sisfb_match(device_t, cfdata_t, void *);
     94      1.1  bouyer void	sisfb_attach(device_t, device_t, void *);
     95      1.1  bouyer 
     96      1.1  bouyer CFATTACH_DECL_NEW(sisfb, sizeof(struct sisfb_softc),
     97      1.1  bouyer     sisfb_match, sisfb_attach, NULL, NULL);
     98      1.1  bouyer 
     99      1.1  bouyer 
    100      1.1  bouyer int	sisfb_alloc_screen(void *, const struct wsscreen_descr *, void **, int *,
    101      1.1  bouyer 	    int *, long *);
    102      1.1  bouyer void	sisfb_free_screen(void *, void *);
    103      1.1  bouyer int	sisfb_ioctl(void *, void *, u_long, void *, int, struct lwp *);
    104      1.1  bouyer int	sisfb_show_screen(void *, void *, int, void (*)(void *, int, int),
    105      1.1  bouyer 	    void *);
    106      1.1  bouyer paddr_t	sisfb_mmap(void *, void *, off_t, int);
    107      1.1  bouyer void	sisfb_init_screen(void *, struct vcons_screen *, int, long *);
    108      1.1  bouyer 
    109      1.1  bouyer 
    110      1.1  bouyer struct wsdisplay_accessops sisfb_accessops = {
    111      1.1  bouyer 	sisfb_ioctl,
    112      1.1  bouyer 	sisfb_mmap,
    113      1.1  bouyer 	sisfb_alloc_screen,
    114      1.1  bouyer 	sisfb_free_screen,
    115      1.1  bouyer 	sisfb_show_screen,
    116      1.1  bouyer 	NULL,	/* load_font */
    117      1.1  bouyer 	NULL,	/* poolc */
    118      1.1  bouyer 	NULL	/* scroll */
    119      1.1  bouyer };
    120      1.1  bouyer 
    121      1.1  bouyer int	sisfb_getcmap(uint8_t *, struct wsdisplay_cmap *);
    122      1.1  bouyer void	sisfb_loadcmap(struct sisfb *, int, int);
    123      1.1  bouyer int	sisfb_putcmap(uint8_t *, struct wsdisplay_cmap *);
    124      1.1  bouyer int	sisfb_setup(struct sisfb *);
    125      1.1  bouyer 
    126      1.1  bouyer static struct sisfb sisfbcn;
    127      1.1  bouyer 
    128      1.1  bouyer /*
    129      1.1  bouyer  * Control Register access
    130      1.1  bouyer  *
    131      1.1  bouyer  * These are 8 bit registers; the choice of larger width types is intentional.
    132      1.1  bouyer  */
    133      1.1  bouyer 
    134      1.1  bouyer #define	SIS_VGA_PORT_OFFSET	0x380
    135      1.1  bouyer 
    136      1.1  bouyer #define	SEQ_ADDR		(0x3c4 - SIS_VGA_PORT_OFFSET)
    137      1.1  bouyer #define	SEQ_DATA		(0x3c5 - SIS_VGA_PORT_OFFSET)
    138      1.1  bouyer #define	DAC_ADDR		(0x3c8 - SIS_VGA_PORT_OFFSET)
    139      1.1  bouyer #define	DAC_DATA		(0x3c9 - SIS_VGA_PORT_OFFSET)
    140      1.1  bouyer #undef	CRTC_ADDR
    141      1.1  bouyer #define	CRTC_ADDR		(0x3d4 - SIS_VGA_PORT_OFFSET)
    142      1.1  bouyer #define	CRTC_DATA		(0x3d5 - SIS_VGA_PORT_OFFSET)
    143      1.1  bouyer 
    144      1.1  bouyer #define	CRTC_HDISPLE	0x01	/* horizontal display end */
    145      1.1  bouyer #define	CRTC_OVERFLL	0x07	/* overflow low */
    146      1.1  bouyer #define	CRTC_STARTADRH	0x0C	/* linear start	address mid */
    147      1.1  bouyer #define	CRTC_STARTADRL	0x0D	/* linear start	address low */
    148      1.1  bouyer #define	CRTC_VDE	0x12	/* vertical display end */
    149      1.1  bouyer 
    150      1.1  bouyer 
    151      1.1  bouyer static inline uint sisfb_crtc_read(struct sisfb *, uint);
    152      1.1  bouyer static inline void sisfb_crtc_write(struct sisfb *, uint, uint);
    153      1.1  bouyer static inline uint sisfb_seq_read(struct sisfb *, uint);
    154      1.1  bouyer static inline void sisfb_seq_write(struct sisfb *, uint, uint);
    155      1.1  bouyer 
    156      1.1  bouyer static inline uint
    157      1.1  bouyer sisfb_crtc_read(struct sisfb *fb, uint idx)
    158      1.1  bouyer {
    159      1.1  bouyer 	uint val;
    160      1.1  bouyer 	bus_space_write_1(fb->iot, fb->ioh, CRTC_ADDR, idx);
    161      1.1  bouyer 	val = bus_space_read_1(fb->iot, fb->ioh, CRTC_DATA);
    162      1.1  bouyer #ifdef SIS_DEBUG
    163      1.1  bouyer 	printf("CRTC %04x -> %02x\n", idx, val);
    164      1.1  bouyer #endif
    165      1.1  bouyer 	return val;
    166      1.1  bouyer }
    167      1.1  bouyer 
    168      1.1  bouyer static inline void
    169      1.1  bouyer sisfb_crtc_write(struct sisfb *fb, uint idx, uint val)
    170      1.1  bouyer {
    171      1.1  bouyer #ifdef SIS_DEBUG
    172      1.1  bouyer 	printf("CRTC %04x <- %02x\n", idx, val);
    173      1.1  bouyer #endif
    174      1.1  bouyer 	bus_space_write_1(fb->iot, fb->ioh, CRTC_ADDR, idx);
    175      1.1  bouyer 	bus_space_write_1(fb->iot, fb->ioh, CRTC_DATA, val);
    176      1.1  bouyer }
    177      1.1  bouyer 
    178      1.1  bouyer static inline uint
    179      1.1  bouyer sisfb_seq_read(struct sisfb *fb, uint idx)
    180      1.1  bouyer {
    181      1.1  bouyer 	uint val;
    182      1.1  bouyer 	bus_space_write_1(fb->iot, fb->ioh, SEQ_ADDR, idx);
    183      1.1  bouyer 	val = bus_space_read_1(fb->iot, fb->ioh, SEQ_DATA);
    184      1.1  bouyer #ifdef SIS_DEBUG
    185      1.1  bouyer 	printf("SEQ %04x -> %02x\n", idx, val);
    186      1.1  bouyer #endif
    187      1.1  bouyer 	return val;
    188      1.1  bouyer }
    189      1.1  bouyer 
    190      1.1  bouyer static inline void
    191      1.1  bouyer sisfb_seq_write(struct sisfb *fb, uint idx, uint val)
    192      1.1  bouyer {
    193      1.1  bouyer #ifdef SIS_DEBUG
    194      1.1  bouyer 	printf("SEQ %04x <- %02x\n", idx, val);
    195      1.1  bouyer #endif
    196      1.1  bouyer 	bus_space_write_1(fb->iot, fb->ioh, SEQ_ADDR, idx);
    197      1.1  bouyer 	bus_space_write_1(fb->iot, fb->ioh, SEQ_DATA, val);
    198      1.1  bouyer }
    199      1.1  bouyer 
    200      1.1  bouyer int
    201      1.1  bouyer sisfb_match(device_t parent, cfdata_t match, void *aux)
    202      1.1  bouyer {
    203      1.1  bouyer 	struct pci_attach_args *pa = (struct pci_attach_args *)aux;
    204      1.1  bouyer 
    205      1.1  bouyer 	if (PCI_CLASS(pa->pa_class) != PCI_CLASS_DISPLAY)
    206      1.1  bouyer 		return 0;
    207      1.1  bouyer 
    208      1.1  bouyer 	if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_SIS)
    209      1.1  bouyer 		return 0;
    210      1.1  bouyer 
    211      1.1  bouyer 	if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_SIS_315PRO_VGA)
    212      1.1  bouyer 		return 100;
    213      1.1  bouyer 	return (0);
    214      1.1  bouyer }
    215      1.1  bouyer 
    216      1.1  bouyer void
    217      1.1  bouyer sisfb_attach(device_t parent, device_t self, void *aux)
    218      1.1  bouyer {
    219      1.1  bouyer 	struct sisfb_softc *sc = device_private(self);
    220      1.1  bouyer 	struct pci_attach_args *pa = (struct pci_attach_args *)aux;
    221      1.1  bouyer 	struct rasops_info *ri;
    222      1.1  bouyer 	struct wsemuldisplaydev_attach_args waa;
    223      1.1  bouyer 	bus_size_t fbsize, mmiosize, iosize;
    224      1.1  bouyer 	struct sisfb *fb;
    225      1.1  bouyer 	int console;
    226      1.1  bouyer 	unsigned long defattr;
    227      1.1  bouyer 
    228      1.1  bouyer 	sc->sc_dev = self;
    229      1.1  bouyer 	console = sisfbcn.vcs.scr_ri.ri_hw != NULL;
    230      1.1  bouyer 
    231      1.1  bouyer 	if (console)
    232      1.1  bouyer 		fb = &sisfbcn;
    233      1.1  bouyer 	else {
    234      1.1  bouyer 		fb = &sc->sc_fb_store;
    235      1.1  bouyer 	}
    236      1.1  bouyer 
    237      1.1  bouyer 	sc->sc_fb = fb;
    238      1.1  bouyer 	fb->sc = sc;
    239      1.1  bouyer 
    240  1.1.6.1     mrg 	pci_aprint_devinfo(pa, NULL);
    241      1.1  bouyer 
    242      1.1  bouyer 	if (!console) {
    243      1.1  bouyer 		fb->fbt = pa->pa_memt;
    244      1.1  bouyer 		fb->mmiot = pa->pa_memt;
    245      1.1  bouyer 		fb->iot = pa->pa_iot;
    246      1.1  bouyer 		if (pci_mapreg_map(pa, PCI_MAPREG_START, PCI_MAPREG_TYPE_MEM,
    247      1.1  bouyer 		    BUS_SPACE_MAP_LINEAR, &fb->fbt, &fb->fbh, NULL, &fbsize) != 0) {
    248      1.1  bouyer 			aprint_error_dev(self, ": can't map frame buffer\n");
    249      1.1  bouyer 			return;
    250      1.1  bouyer 		}
    251      1.1  bouyer 
    252      1.1  bouyer 		if (pci_mapreg_map(pa, PCI_MAPREG_START + 4, PCI_MAPREG_TYPE_MEM,
    253      1.1  bouyer 		    0, &fb->mmiot, &fb->mmioh, NULL, &mmiosize) != 0) {
    254      1.1  bouyer 			aprint_error_dev(self, ": can't map mmio area\n");
    255      1.1  bouyer 			goto fail1;
    256      1.1  bouyer 		}
    257      1.1  bouyer 
    258      1.1  bouyer 		if (pci_mapreg_map(pa, PCI_MAPREG_START + 8, PCI_MAPREG_TYPE_IO,
    259      1.1  bouyer 		    0, &fb->iot, &fb->ioh, NULL, &iosize) != 0) {
    260      1.1  bouyer 			aprint_error_dev(self, ": can't map registers\n");
    261      1.1  bouyer 			goto fail2;
    262      1.1  bouyer 		}
    263      1.1  bouyer 
    264      1.1  bouyer 
    265      1.1  bouyer 		if (sisfb_setup(sc->sc_fb) != 0) {
    266      1.1  bouyer 			aprint_error_dev(self, ": can't setup frame buffer\n");
    267      1.1  bouyer 			goto fail3;
    268      1.1  bouyer 		}
    269      1.1  bouyer 	}
    270      1.1  bouyer 
    271      1.1  bouyer 	aprint_normal_dev(self, ": %dx%dx%d frame buffer\n",
    272      1.1  bouyer 	    fb->vcs.scr_ri.ri_width, fb->vcs.scr_ri.ri_height, fb->vcs.scr_ri.ri_depth);
    273      1.1  bouyer 
    274      1.1  bouyer 	fb->wsd = (struct wsscreen_descr){
    275      1.1  bouyer 		"default",
    276      1.1  bouyer 		0, 0,
    277      1.1  bouyer 		NULL,
    278      1.1  bouyer 		8, 16,
    279      1.1  bouyer 		WSSCREEN_WSCOLORS | WSSCREEN_HILIT,
    280      1.1  bouyer 		NULL
    281      1.1  bouyer 	};
    282      1.1  bouyer 	sc->sc_scrlist[0] = &fb->wsd;
    283      1.1  bouyer 	sc->sc_wsl = (struct wsscreen_list){1, sc->sc_scrlist};
    284      1.1  bouyer 	sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
    285      1.1  bouyer 
    286      1.1  bouyer 
    287      1.1  bouyer 	vcons_init(&sc->vd, sc, &fb->wsd, &sisfb_accessops);
    288      1.1  bouyer 	sc->vd.init_screen = sisfb_init_screen;
    289      1.1  bouyer 
    290      1.1  bouyer 	ri = &fb->vcs.scr_ri;
    291      1.1  bouyer 	if (console) {
    292      1.1  bouyer 		vcons_init_screen(&sc->vd, &fb->vcs, 1, &defattr);
    293      1.1  bouyer 		fb->vcs.scr_flags |= VCONS_SCREEN_IS_STATIC;
    294      1.1  bouyer 		fb->wsd.textops = &ri->ri_ops;
    295      1.1  bouyer 		fb->wsd.capabilities = ri->ri_caps;
    296      1.1  bouyer 		fb->wsd.nrows = ri->ri_rows;
    297      1.1  bouyer 		fb->wsd.ncols = ri->ri_cols;
    298      1.1  bouyer 		wsdisplay_cnattach(&fb->wsd, ri, 0, 0, defattr);
    299      1.1  bouyer 		vcons_replay_msgbuf(&fb->vcs);
    300      1.1  bouyer 	} else {
    301      1.1  bouyer 		/*
    302      1.1  bouyer 		 * since we're not the console we can postpone the rest
    303      1.1  bouyer 		 * until someone actually allocates a screen for us
    304      1.1  bouyer 		 */
    305      1.1  bouyer 		(*ri->ri_ops.allocattr)(ri, 0, 0, 0, &defattr);
    306      1.1  bouyer 	}
    307      1.1  bouyer 
    308      1.1  bouyer 	waa.console = console;
    309      1.1  bouyer 	waa.scrdata = &sc->sc_wsl;
    310      1.1  bouyer 	waa.accessops = &sisfb_accessops;
    311      1.1  bouyer 	waa.accesscookie = &sc->vd;
    312      1.1  bouyer 
    313      1.1  bouyer 	config_found(sc->sc_dev, &waa, wsemuldisplaydevprint);
    314      1.1  bouyer 	return;
    315      1.1  bouyer 
    316      1.1  bouyer fail3:
    317      1.1  bouyer 	bus_space_unmap(fb->iot, fb->ioh, iosize);
    318      1.1  bouyer fail2:
    319      1.1  bouyer 	bus_space_unmap(fb->mmiot, fb->mmioh, mmiosize);
    320      1.1  bouyer fail1:
    321      1.1  bouyer 	bus_space_unmap(fb->fbt, fb->fbh, fbsize);
    322      1.1  bouyer }
    323      1.1  bouyer 
    324      1.1  bouyer /*
    325      1.1  bouyer  * wsdisplay accesops
    326      1.1  bouyer  */
    327      1.1  bouyer 
    328      1.1  bouyer int
    329      1.1  bouyer sisfb_alloc_screen(void *v, const struct wsscreen_descr *type, void **cookiep,
    330      1.1  bouyer     int *curxp, int *curyp, long *attrp)
    331      1.1  bouyer {
    332      1.1  bouyer 	struct sisfb_softc *sc = (struct sisfb_softc *)v;
    333      1.1  bouyer 	struct rasops_info *ri = &sc->sc_fb->vcs.scr_ri;
    334      1.1  bouyer 
    335      1.1  bouyer 	if (sc->sc_nscr > 0)
    336      1.1  bouyer 		return ENOMEM;
    337      1.1  bouyer 
    338      1.1  bouyer 	*cookiep = ri;
    339      1.1  bouyer 	*curxp = *curyp = 0;
    340      1.1  bouyer 	ri->ri_ops.allocattr(ri, 0, 0, 0, attrp);
    341      1.1  bouyer 	sc->sc_nscr++;
    342      1.1  bouyer 
    343      1.1  bouyer 	return 0;
    344      1.1  bouyer }
    345      1.1  bouyer 
    346      1.1  bouyer void
    347      1.1  bouyer sisfb_free_screen(void *v, void *cookie)
    348      1.1  bouyer {
    349      1.1  bouyer 	struct sisfb_softc *sc = (struct sisfb_softc *)v;
    350      1.1  bouyer 
    351      1.1  bouyer 	sc->sc_nscr--;
    352      1.1  bouyer }
    353      1.1  bouyer 
    354      1.1  bouyer int
    355      1.1  bouyer sisfb_ioctl(void *v, void *vs, u_long cmd, void *data, int flags,
    356      1.1  bouyer     struct lwp *l)
    357      1.1  bouyer {
    358      1.1  bouyer 	struct vcons_data *vd = v;
    359      1.1  bouyer 	struct sisfb_softc *sc = vd->cookie;
    360      1.1  bouyer 	struct sisfb *fb = sc->sc_fb;
    361      1.1  bouyer 	struct rasops_info *ri = &fb->vcs.scr_ri;
    362      1.1  bouyer 	struct wsdisplay_cmap *cm;
    363      1.1  bouyer 	struct wsdisplay_fbinfo *wdf;
    364      1.1  bouyer 	int rc;
    365      1.1  bouyer 
    366      1.1  bouyer 	switch (cmd) {
    367      1.1  bouyer 	case WSDISPLAYIO_GTYPE:
    368      1.1  bouyer 		*(uint *)data = WSDISPLAY_TYPE_PCIMISC;
    369      1.1  bouyer 		return 0;
    370      1.1  bouyer 	case WSDISPLAYIO_GINFO:
    371      1.1  bouyer 		wdf = (struct wsdisplay_fbinfo *)data;
    372      1.1  bouyer 		wdf->width = ri->ri_width;
    373      1.1  bouyer 		wdf->height = ri->ri_height;
    374      1.1  bouyer 		wdf->depth = ri->ri_depth;
    375      1.1  bouyer 		wdf->cmsize = 256;
    376      1.1  bouyer 		return 0;
    377      1.1  bouyer 	case WSDISPLAYIO_LINEBYTES:
    378      1.1  bouyer 		*(uint *)data = ri->ri_stride;
    379      1.1  bouyer 		break;
    380      1.1  bouyer 	case WSDISPLAYIO_GETCMAP:
    381      1.1  bouyer 		cm = (struct wsdisplay_cmap *)data;
    382      1.1  bouyer 		rc = sisfb_getcmap(fb->cmap, cm);
    383      1.1  bouyer 		return rc;
    384      1.1  bouyer 	case WSDISPLAYIO_PUTCMAP:
    385      1.1  bouyer 		cm = (struct wsdisplay_cmap *)data;
    386      1.1  bouyer 		rc = sisfb_putcmap(fb->cmap, cm);
    387      1.1  bouyer 		if (rc != 0)
    388      1.1  bouyer 			return rc;
    389      1.1  bouyer 		if (ri->ri_depth == 8)
    390      1.1  bouyer 			sisfb_loadcmap(fb, cm->index, cm->count);
    391      1.1  bouyer 		return 0;
    392      1.1  bouyer 	}
    393      1.1  bouyer 	return EPASSTHROUGH;
    394      1.1  bouyer }
    395      1.1  bouyer 
    396      1.1  bouyer int
    397      1.1  bouyer sisfb_show_screen(void *v, void *cookie, int waitok,
    398      1.1  bouyer     void (*cb)(void *, int, int), void *cbarg)
    399      1.1  bouyer {
    400      1.1  bouyer 	return 0;
    401      1.1  bouyer }
    402      1.1  bouyer 
    403      1.1  bouyer paddr_t
    404      1.1  bouyer sisfb_mmap(void *v, void *vs, off_t offset, int prot)
    405      1.1  bouyer {
    406      1.1  bouyer 	struct vcons_data *vd = v;
    407      1.1  bouyer 	struct sisfb_softc *sc = vd->cookie;
    408      1.1  bouyer 	struct rasops_info *ri = &sc->sc_fb->vcs.scr_ri;
    409      1.1  bouyer 
    410      1.1  bouyer 	if ((offset & PAGE_MASK) != 0)
    411      1.1  bouyer 		return -1;
    412      1.1  bouyer 
    413      1.1  bouyer 	if (offset < 0 || offset >= ri->ri_stride * ri->ri_height)
    414      1.1  bouyer 		return -1;
    415      1.1  bouyer 
    416      1.1  bouyer 	/*
    417      1.1  bouyer 	 * Don't allow mmap if the frame buffer area is not page aligned.
    418      1.1  bouyer 	 * XXX we should reprogram it to a page aligned boundary at attach
    419      1.1  bouyer 	 * XXX time if this isn't the case.
    420      1.1  bouyer 	 */
    421      1.1  bouyer 	if (((paddr_t)ri->ri_bits & PAGE_MASK) != 0)
    422      1.1  bouyer 		return -1;
    423      1.1  bouyer 
    424      1.1  bouyer 	/* return XKPHYS_TO_PHYS((paddr_t)ri->ri_bits) + offset; */
    425      1.1  bouyer 	/* XXX */ return -1;
    426      1.1  bouyer }
    427      1.1  bouyer 
    428      1.1  bouyer void
    429      1.1  bouyer sisfb_init_screen(void *cookie, struct vcons_screen *scr,
    430      1.1  bouyer     int existing, long *defattr)
    431      1.1  bouyer {
    432      1.1  bouyer 	struct sisfb_softc *sc = cookie;
    433      1.1  bouyer 	struct sisfb *fb = sc->sc_fb;
    434      1.1  bouyer 	struct rasops_info *ri = &scr->scr_ri;
    435      1.1  bouyer 
    436      1.1  bouyer 	ri->ri_depth = fb->fb_depth;
    437      1.1  bouyer 	ri->ri_width = fb->fb_width;
    438      1.1  bouyer 	ri->ri_height = fb->fb_height;
    439      1.1  bouyer 	ri->ri_stride = fb->fb_stride;
    440      1.1  bouyer 	ri->ri_flg = RI_CENTER | RI_FULLCLEAR;
    441      1.1  bouyer 
    442      1.1  bouyer 	ri->ri_bits = fb->fb_addr;
    443      1.1  bouyer 
    444      1.1  bouyer 	if (existing) {
    445      1.1  bouyer 		ri->ri_flg |= RI_CLEAR;
    446      1.1  bouyer 	}
    447      1.1  bouyer 
    448      1.1  bouyer 	rasops_init(ri, fb->fb_height / 8, fb->fb_width / 8);
    449      1.1  bouyer 	ri->ri_caps = WSSCREEN_WSCOLORS;
    450      1.1  bouyer 	rasops_reconfig(ri, fb->fb_height / ri->ri_font->fontheight,
    451      1.1  bouyer 		    fb->fb_width / ri->ri_font->fontwidth);
    452      1.1  bouyer 
    453      1.1  bouyer 	ri->ri_hw = scr;
    454      1.1  bouyer }
    455      1.1  bouyer 
    456      1.1  bouyer 
    457      1.1  bouyer /*
    458      1.1  bouyer  * Frame buffer initialization.
    459      1.1  bouyer  */
    460      1.1  bouyer 
    461      1.1  bouyer int
    462      1.1  bouyer sisfb_setup(struct sisfb *fb)
    463      1.1  bouyer {
    464      1.1  bouyer 	struct rasops_info *ri = &fb->vcs.scr_ri;
    465      1.1  bouyer 	uint width, height, bpp;
    466      1.1  bouyer 	bus_size_t fbaddr;
    467      1.1  bouyer 	uint tmp;
    468      1.1  bouyer 
    469      1.1  bouyer 	/*
    470      1.1  bouyer 	 * Unlock access to extended registers.
    471      1.1  bouyer 	 */
    472      1.1  bouyer 
    473      1.1  bouyer 	sisfb_seq_write(fb, 0x05, 0x86);
    474      1.1  bouyer 
    475      1.1  bouyer 	/*
    476      1.1  bouyer 	 * Try and figure out display settings.
    477      1.1  bouyer 	 */
    478      1.1  bouyer 
    479      1.1  bouyer 	height = sisfb_crtc_read(fb, CRTC_VDE);
    480      1.1  bouyer 	tmp = sisfb_crtc_read(fb, CRTC_OVERFLL);
    481      1.1  bouyer 	if (ISSET(tmp, 1 << 1))
    482      1.1  bouyer 		height |= 1 << 8;
    483      1.1  bouyer 	if (ISSET(tmp, 1 << 6))
    484      1.1  bouyer 		height |= 1 << 9;
    485      1.1  bouyer 	tmp = sisfb_seq_read(fb, 0x0a);
    486      1.1  bouyer 	if (ISSET(tmp, 1 << 1))
    487      1.1  bouyer 		height |= 1 << 10;
    488      1.1  bouyer 	height++;
    489      1.1  bouyer 
    490      1.1  bouyer 	width = sisfb_crtc_read(fb, CRTC_HDISPLE);
    491      1.1  bouyer 	tmp = sisfb_seq_read(fb, 0x0b);
    492      1.1  bouyer 	if (ISSET(tmp, 1 << 2))
    493      1.1  bouyer 		width |= 1 << 8;
    494      1.1  bouyer 	if (ISSET(tmp, 1 << 3))
    495      1.1  bouyer 		width |= 1 << 9;
    496      1.1  bouyer 	width++;
    497      1.1  bouyer 	width <<= 3;
    498      1.1  bouyer 
    499      1.1  bouyer #ifdef SIS_DEBUG
    500      1.1  bouyer 	printf("height %d width %d\n", height, width);
    501      1.1  bouyer #endif
    502      1.1  bouyer 
    503      1.1  bouyer 	fbaddr = sisfb_crtc_read(fb, CRTC_STARTADRL) |
    504      1.1  bouyer 	    (sisfb_crtc_read(fb, CRTC_STARTADRH) << 8) |
    505      1.1  bouyer 	    (sisfb_seq_read(fb, 0x0d) << 16) |
    506      1.1  bouyer 	    ((sisfb_seq_read(fb, 0x37) & 0x03) << 24);
    507      1.1  bouyer 	fbaddr <<= 2;
    508      1.1  bouyer #ifdef SIS_DEBUG
    509      1.1  bouyer 	printf("FBADDR %lx\n", fbaddr);
    510      1.1  bouyer #endif
    511      1.1  bouyer 
    512      1.1  bouyer 	tmp = sisfb_seq_read(fb, 0x06);
    513      1.1  bouyer 	switch (tmp & 0x1c) {
    514      1.1  bouyer 	case 0x00:
    515      1.1  bouyer 		bpp = 8;
    516      1.1  bouyer 		break;
    517      1.1  bouyer 	case 0x04:
    518      1.1  bouyer 		bpp = 15;
    519      1.1  bouyer 		break;
    520      1.1  bouyer 	case 0x08:
    521      1.1  bouyer 		bpp = 16;
    522      1.1  bouyer 		break;
    523      1.1  bouyer 	case 0x10:
    524      1.1  bouyer 		bpp = 32;
    525      1.1  bouyer 		break;
    526      1.1  bouyer 	default:
    527      1.1  bouyer 		aprint_error("unknown bpp for 0x%x\n", tmp);
    528      1.1  bouyer 		return EINVAL;
    529      1.1  bouyer 	}
    530      1.1  bouyer #ifdef SIS_DEBUG
    531      1.1  bouyer 	printf("BPP %d\n", bpp);
    532      1.1  bouyer #endif
    533      1.1  bouyer 
    534      1.1  bouyer 	fb->fb_width = ri->ri_width = width;
    535      1.1  bouyer 	fb->fb_height = ri->ri_height = height;
    536      1.1  bouyer 	fb->fb_depth = ri->ri_depth = bpp;
    537      1.1  bouyer 	fb->fb_stride = ri->ri_stride = (ri->ri_width * ri->ri_depth) / 8;
    538      1.1  bouyer 	ri->ri_flg = /* RI_CENTER | RI_CLEAR | RI_FULLCLEAR */ RI_CENTER | RI_NO_AUTO;
    539      1.1  bouyer 	fb->fb_addr = ri->ri_bits =
    540      1.1  bouyer 	    (void *)((char *)bus_space_vaddr(fb->fbt, fb->fbh) + fbaddr);
    541      1.1  bouyer 	ri->ri_hw = fb;
    542      1.1  bouyer 
    543      1.1  bouyer #ifdef SIS_DEBUG
    544      1.1  bouyer 	printf("ri_bits %p\n", ri->ri_bits);
    545      1.1  bouyer #endif
    546      1.1  bouyer 
    547      1.1  bouyer #ifdef __MIPSEL__
    548      1.1  bouyer 	/* swap B and R */
    549      1.1  bouyer 	switch (bpp) {
    550      1.1  bouyer 	case 15:
    551      1.1  bouyer 		ri->ri_rnum = 5;
    552      1.1  bouyer 		ri->ri_rpos = 10;
    553      1.1  bouyer 		ri->ri_gnum = 5;
    554      1.1  bouyer 		ri->ri_gpos = 5;
    555      1.1  bouyer 		ri->ri_bnum = 5;
    556      1.1  bouyer 		ri->ri_bpos = 0;
    557      1.1  bouyer 		break;
    558      1.1  bouyer 	case 16:
    559      1.1  bouyer 		ri->ri_rnum = 5;
    560      1.1  bouyer 		ri->ri_rpos = 11;
    561      1.1  bouyer 		ri->ri_gnum = 6;
    562      1.1  bouyer 		ri->ri_gpos = 5;
    563      1.1  bouyer 		ri->ri_bnum = 5;
    564      1.1  bouyer 		ri->ri_bpos = 0;
    565      1.1  bouyer 		break;
    566      1.1  bouyer 	}
    567      1.1  bouyer #endif
    568      1.1  bouyer 
    569      1.1  bouyer 	bcopy(rasops_cmap, fb->cmap, sizeof(fb->cmap));
    570      1.1  bouyer 	if (bpp == 8) {
    571      1.1  bouyer 		sisfb_loadcmap(fb, 0, 256);
    572      1.1  bouyer 	}
    573      1.1  bouyer 
    574      1.1  bouyer 	rasops_init(ri, 30, 80);
    575      1.1  bouyer 	rasops_reconfig(ri, ri->ri_height / ri->ri_font->fontheight,
    576      1.1  bouyer 	    ri->ri_width / ri->ri_font->fontwidth);
    577      1.1  bouyer 
    578      1.1  bouyer 	fb->wsd.name = "std";
    579      1.1  bouyer 	fb->wsd.ncols = ri->ri_cols;
    580      1.1  bouyer 	fb->wsd.nrows = ri->ri_rows;
    581      1.1  bouyer 	fb->wsd.textops = &ri->ri_ops;
    582      1.1  bouyer 	fb->wsd.fontwidth = ri->ri_font->fontwidth;
    583      1.1  bouyer 	fb->wsd.fontheight = ri->ri_font->fontheight;
    584      1.1  bouyer 	fb->wsd.capabilities = ri->ri_caps;
    585      1.1  bouyer 
    586      1.1  bouyer 	return 0;
    587      1.1  bouyer }
    588      1.1  bouyer 
    589      1.1  bouyer /*
    590      1.1  bouyer  * Colormap handling routines.
    591      1.1  bouyer  */
    592      1.1  bouyer 
    593      1.1  bouyer void
    594      1.1  bouyer sisfb_loadcmap(struct sisfb *fb, int baseidx, int count)
    595      1.1  bouyer {
    596      1.1  bouyer 	uint8_t *cmap = fb->cmap + baseidx * 3;
    597      1.1  bouyer 
    598      1.1  bouyer 	bus_space_write_1(fb->iot, fb->ioh, DAC_ADDR, baseidx);
    599      1.1  bouyer 	while (count-- != 0) {
    600      1.1  bouyer 		bus_space_write_1(fb->iot, fb->ioh, DAC_DATA, *cmap++ >> 2);
    601      1.1  bouyer 		bus_space_write_1(fb->iot, fb->ioh, DAC_DATA, *cmap++ >> 2);
    602      1.1  bouyer 		bus_space_write_1(fb->iot, fb->ioh, DAC_DATA, *cmap++ >> 2);
    603      1.1  bouyer 	}
    604      1.1  bouyer }
    605      1.1  bouyer 
    606      1.1  bouyer int
    607      1.1  bouyer sisfb_getcmap(uint8_t *cmap, struct wsdisplay_cmap *cm)
    608      1.1  bouyer {
    609      1.1  bouyer 	uint index = cm->index, count = cm->count, i;
    610      1.1  bouyer 	uint8_t ramp[256], *dst, *src;
    611      1.1  bouyer 	int rc;
    612      1.1  bouyer 
    613      1.1  bouyer 	if (index >= 256 || count > 256 - index)
    614      1.1  bouyer 		return EINVAL;
    615      1.1  bouyer 
    616      1.1  bouyer 	index *= 3;
    617      1.1  bouyer 
    618      1.1  bouyer 	src = cmap + index;
    619      1.1  bouyer 	dst = ramp;
    620      1.1  bouyer 	for (i = 0; i < count; i++)
    621      1.1  bouyer 		*dst++ = *src, src += 3;
    622      1.1  bouyer 	rc = copyout(ramp, cm->red, count);
    623      1.1  bouyer 	if (rc != 0)
    624      1.1  bouyer 		return rc;
    625      1.1  bouyer 
    626      1.1  bouyer 	src = cmap + index + 1;
    627      1.1  bouyer 	dst = ramp;
    628      1.1  bouyer 	for (i = 0; i < count; i++)
    629      1.1  bouyer 		*dst++ = *src, src += 3;
    630      1.1  bouyer 	rc = copyout(ramp, cm->green, count);
    631      1.1  bouyer 	if (rc != 0)
    632      1.1  bouyer 		return rc;
    633      1.1  bouyer 
    634      1.1  bouyer 	src = cmap + index + 2;
    635      1.1  bouyer 	dst = ramp;
    636      1.1  bouyer 	for (i = 0; i < count; i++)
    637      1.1  bouyer 		*dst++ = *src, src += 3;
    638      1.1  bouyer 	rc = copyout(ramp, cm->blue, count);
    639      1.1  bouyer 	if (rc != 0)
    640      1.1  bouyer 		return rc;
    641      1.1  bouyer 
    642      1.1  bouyer 	return 0;
    643      1.1  bouyer }
    644      1.1  bouyer 
    645      1.1  bouyer int
    646      1.1  bouyer sisfb_putcmap(uint8_t *cmap, struct wsdisplay_cmap *cm)
    647      1.1  bouyer {
    648      1.1  bouyer 	uint index = cm->index, count = cm->count, i;
    649      1.1  bouyer 	uint8_t ramp[256], *dst, *src;
    650      1.1  bouyer 	int rc;
    651      1.1  bouyer 
    652      1.1  bouyer 	if (index >= 256 || count > 256 - index)
    653      1.1  bouyer 		return EINVAL;
    654      1.1  bouyer 
    655      1.1  bouyer 	index *= 3;
    656      1.1  bouyer 
    657      1.1  bouyer 	rc = copyin(cm->red, ramp, count);
    658      1.1  bouyer 	if (rc != 0)
    659      1.1  bouyer 		return rc;
    660      1.1  bouyer 	dst = cmap + index;
    661      1.1  bouyer 	src = ramp;
    662      1.1  bouyer 	for (i = 0; i < count; i++)
    663      1.1  bouyer 		*dst = *src++, dst += 3;
    664      1.1  bouyer 
    665      1.1  bouyer 	rc = copyin(cm->green, ramp, count);
    666      1.1  bouyer 	if (rc != 0)
    667      1.1  bouyer 		return rc;
    668      1.1  bouyer 	dst = cmap + index + 1;
    669      1.1  bouyer 	src = ramp;
    670      1.1  bouyer 	for (i = 0; i < count; i++)
    671      1.1  bouyer 		*dst = *src++, dst += 3;
    672      1.1  bouyer 
    673      1.1  bouyer 	rc = copyin(cm->blue, ramp, count);
    674      1.1  bouyer 	if (rc != 0)
    675      1.1  bouyer 		return rc;
    676      1.1  bouyer 	dst = cmap + index + 2;
    677      1.1  bouyer 	src = ramp;
    678      1.1  bouyer 	for (i = 0; i < count; i++)
    679      1.1  bouyer 		*dst = *src++, dst += 3;
    680      1.1  bouyer 
    681      1.1  bouyer 	return 0;
    682      1.1  bouyer }
    683      1.1  bouyer 
    684      1.1  bouyer /*
    685      1.1  bouyer  * Early console code
    686      1.1  bouyer  */
    687      1.1  bouyer 
    688      1.1  bouyer int
    689      1.1  bouyer sisfb_cnattach(bus_space_tag_t memt, bus_space_tag_t iot,
    690      1.1  bouyer     pci_chipset_tag_t pc, pcitag_t tag, pcireg_t id)
    691      1.1  bouyer {
    692      1.1  bouyer 	long defattr;
    693      1.1  bouyer 	struct rasops_info * const ri = &sisfbcn.vcs.scr_ri;
    694      1.1  bouyer 	pcireg_t bar;
    695      1.1  bouyer 	int rc;
    696      1.1  bouyer 
    697      1.1  bouyer 
    698      1.1  bouyer 	/* filter out unrecognized devices */
    699      1.1  bouyer 	switch (id) {
    700      1.1  bouyer 	default:
    701      1.1  bouyer 		return ENODEV;
    702      1.1  bouyer 	case PCI_ID_CODE(PCI_VENDOR_SIS, PCI_PRODUCT_SIS_315PRO_VGA):
    703      1.1  bouyer 		break;
    704      1.1  bouyer 	}
    705      1.1  bouyer 
    706      1.1  bouyer 	bar = pci_conf_read(pc, tag, PCI_MAPREG_START);
    707      1.1  bouyer 	if (PCI_MAPREG_TYPE(bar) != PCI_MAPREG_TYPE_MEM)
    708      1.1  bouyer 		return EINVAL;
    709      1.1  bouyer 	sisfbcn.fbt = memt;
    710      1.1  bouyer 	rc = bus_space_map(memt, PCI_MAPREG_MEM_ADDR(bar), 1 /* XXX */,
    711      1.1  bouyer 	    BUS_SPACE_MAP_LINEAR, &sisfbcn.fbh);
    712      1.1  bouyer #ifdef SIS_DEBUG
    713      1.1  bouyer 	printf("sisfb_cnattach(memt, 0x%x rc %d\n", PCI_MAPREG_MEM_ADDR(bar), rc);
    714      1.1  bouyer #endif
    715      1.1  bouyer 	if (rc != 0)
    716      1.1  bouyer 		return rc;
    717      1.1  bouyer 
    718      1.1  bouyer 	bar = pci_conf_read(pc, tag, PCI_MAPREG_START + 4);
    719      1.1  bouyer 	if (PCI_MAPREG_TYPE(bar) != PCI_MAPREG_TYPE_MEM)
    720      1.1  bouyer 		return EINVAL;
    721      1.1  bouyer 	sisfbcn.mmiot = memt;
    722      1.1  bouyer 	rc = bus_space_map(memt, PCI_MAPREG_MEM_ADDR(bar), 1 /* XXX */,
    723      1.1  bouyer 	    BUS_SPACE_MAP_LINEAR, &sisfbcn.mmioh);
    724      1.1  bouyer #ifdef SIS_DEBUG
    725      1.1  bouyer 	printf("sisfb_cnattach(memt2, 0x%x rc %d\n", PCI_MAPREG_MEM_ADDR(bar), rc);
    726      1.1  bouyer #endif
    727      1.1  bouyer 	if (rc != 0)
    728      1.1  bouyer 		return rc;
    729      1.1  bouyer 
    730      1.1  bouyer 	bar = pci_conf_read(pc, tag, PCI_MAPREG_START + 8);
    731      1.1  bouyer 	if (PCI_MAPREG_TYPE(bar) != PCI_MAPREG_TYPE_IO)
    732      1.1  bouyer 		return EINVAL;
    733      1.1  bouyer 	sisfbcn.iot = iot;
    734      1.1  bouyer 	rc = bus_space_map(iot, PCI_MAPREG_MEM_ADDR(bar), 1 /* XXX */,
    735      1.1  bouyer 	    0, &sisfbcn.ioh);
    736      1.1  bouyer #ifdef SIS_DEBUG
    737      1.1  bouyer 	printf("sisfb_cnattach(iot, 0x%x rc %d\n", PCI_MAPREG_MEM_ADDR(bar), rc);
    738      1.1  bouyer #endif
    739      1.1  bouyer 	if (rc != 0)
    740      1.1  bouyer 		return rc;
    741      1.1  bouyer 
    742      1.1  bouyer 	rc = sisfb_setup(&sisfbcn);
    743      1.1  bouyer #ifdef SIS_DEBUG
    744      1.1  bouyer 	printf("sisfb_setup %d %p\n", rc, sisfbcn.vcs.scr_ri.ri_hw);
    745      1.1  bouyer #endif
    746      1.1  bouyer 	if (rc != 0)
    747      1.1  bouyer 		return rc;
    748      1.1  bouyer 
    749      1.1  bouyer 	ri->ri_ops.allocattr(ri, 0,  ri->ri_rows - 1, 0, &defattr);
    750      1.1  bouyer 	wsdisplay_preattach(&sisfbcn.wsd, ri, 0, 0, defattr);
    751      1.1  bouyer 
    752      1.1  bouyer 	return 0;
    753      1.1  bouyer }
    754