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