xref: /netbsd-src/sys/dev/pci/sisfb.c (revision a5847cc334d9a7029f6352b847e9e8d71a0f9e0c)
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 2011/08/27 13:28:37 bouyer 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 	char devinfo[256];
226 	int console;
227 	unsigned long defattr;
228 
229 	sc->sc_dev = self;
230 	console = sisfbcn.vcs.scr_ri.ri_hw != NULL;
231 
232 	if (console)
233 		fb = &sisfbcn;
234 	else {
235 		fb = &sc->sc_fb_store;
236 	}
237 
238 	sc->sc_fb = fb;
239 	fb->sc = sc;
240 
241 	pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof(devinfo));
242 	aprint_normal(": %s\n", devinfo);
243 
244 	if (!console) {
245 		fb->fbt = pa->pa_memt;
246 		fb->mmiot = pa->pa_memt;
247 		fb->iot = pa->pa_iot;
248 		if (pci_mapreg_map(pa, PCI_MAPREG_START, PCI_MAPREG_TYPE_MEM,
249 		    BUS_SPACE_MAP_LINEAR, &fb->fbt, &fb->fbh, NULL, &fbsize) != 0) {
250 			aprint_error_dev(self, ": can't map frame buffer\n");
251 			return;
252 		}
253 
254 		if (pci_mapreg_map(pa, PCI_MAPREG_START + 4, PCI_MAPREG_TYPE_MEM,
255 		    0, &fb->mmiot, &fb->mmioh, NULL, &mmiosize) != 0) {
256 			aprint_error_dev(self, ": can't map mmio area\n");
257 			goto fail1;
258 		}
259 
260 		if (pci_mapreg_map(pa, PCI_MAPREG_START + 8, PCI_MAPREG_TYPE_IO,
261 		    0, &fb->iot, &fb->ioh, NULL, &iosize) != 0) {
262 			aprint_error_dev(self, ": can't map registers\n");
263 			goto fail2;
264 		}
265 
266 
267 		if (sisfb_setup(sc->sc_fb) != 0) {
268 			aprint_error_dev(self, ": can't setup frame buffer\n");
269 			goto fail3;
270 		}
271 	}
272 
273 	aprint_normal_dev(self, ": %dx%dx%d frame buffer\n",
274 	    fb->vcs.scr_ri.ri_width, fb->vcs.scr_ri.ri_height, fb->vcs.scr_ri.ri_depth);
275 
276 	fb->wsd = (struct wsscreen_descr){
277 		"default",
278 		0, 0,
279 		NULL,
280 		8, 16,
281 		WSSCREEN_WSCOLORS | WSSCREEN_HILIT,
282 		NULL
283 	};
284 	sc->sc_scrlist[0] = &fb->wsd;
285 	sc->sc_wsl = (struct wsscreen_list){1, sc->sc_scrlist};
286 	sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
287 
288 
289 	vcons_init(&sc->vd, sc, &fb->wsd, &sisfb_accessops);
290 	sc->vd.init_screen = sisfb_init_screen;
291 
292 	ri = &fb->vcs.scr_ri;
293 	if (console) {
294 		vcons_init_screen(&sc->vd, &fb->vcs, 1, &defattr);
295 		fb->vcs.scr_flags |= VCONS_SCREEN_IS_STATIC;
296 		fb->wsd.textops = &ri->ri_ops;
297 		fb->wsd.capabilities = ri->ri_caps;
298 		fb->wsd.nrows = ri->ri_rows;
299 		fb->wsd.ncols = ri->ri_cols;
300 		wsdisplay_cnattach(&fb->wsd, ri, 0, 0, defattr);
301 		vcons_replay_msgbuf(&fb->vcs);
302 	} else {
303 		/*
304 		 * since we're not the console we can postpone the rest
305 		 * until someone actually allocates a screen for us
306 		 */
307 		(*ri->ri_ops.allocattr)(ri, 0, 0, 0, &defattr);
308 	}
309 
310 	waa.console = console;
311 	waa.scrdata = &sc->sc_wsl;
312 	waa.accessops = &sisfb_accessops;
313 	waa.accesscookie = &sc->vd;
314 
315 	config_found(sc->sc_dev, &waa, wsemuldisplaydevprint);
316 	return;
317 
318 fail3:
319 	bus_space_unmap(fb->iot, fb->ioh, iosize);
320 fail2:
321 	bus_space_unmap(fb->mmiot, fb->mmioh, mmiosize);
322 fail1:
323 	bus_space_unmap(fb->fbt, fb->fbh, fbsize);
324 }
325 
326 /*
327  * wsdisplay accesops
328  */
329 
330 int
331 sisfb_alloc_screen(void *v, const struct wsscreen_descr *type, void **cookiep,
332     int *curxp, int *curyp, long *attrp)
333 {
334 	struct sisfb_softc *sc = (struct sisfb_softc *)v;
335 	struct rasops_info *ri = &sc->sc_fb->vcs.scr_ri;
336 
337 	if (sc->sc_nscr > 0)
338 		return ENOMEM;
339 
340 	*cookiep = ri;
341 	*curxp = *curyp = 0;
342 	ri->ri_ops.allocattr(ri, 0, 0, 0, attrp);
343 	sc->sc_nscr++;
344 
345 	return 0;
346 }
347 
348 void
349 sisfb_free_screen(void *v, void *cookie)
350 {
351 	struct sisfb_softc *sc = (struct sisfb_softc *)v;
352 
353 	sc->sc_nscr--;
354 }
355 
356 int
357 sisfb_ioctl(void *v, void *vs, u_long cmd, void *data, int flags,
358     struct lwp *l)
359 {
360 	struct vcons_data *vd = v;
361 	struct sisfb_softc *sc = vd->cookie;
362 	struct sisfb *fb = sc->sc_fb;
363 	struct rasops_info *ri = &fb->vcs.scr_ri;
364 	struct wsdisplay_cmap *cm;
365 	struct wsdisplay_fbinfo *wdf;
366 	int rc;
367 
368 	switch (cmd) {
369 	case WSDISPLAYIO_GTYPE:
370 		*(uint *)data = WSDISPLAY_TYPE_PCIMISC;
371 		return 0;
372 	case WSDISPLAYIO_GINFO:
373 		wdf = (struct wsdisplay_fbinfo *)data;
374 		wdf->width = ri->ri_width;
375 		wdf->height = ri->ri_height;
376 		wdf->depth = ri->ri_depth;
377 		wdf->cmsize = 256;
378 		return 0;
379 	case WSDISPLAYIO_LINEBYTES:
380 		*(uint *)data = ri->ri_stride;
381 		break;
382 	case WSDISPLAYIO_GETCMAP:
383 		cm = (struct wsdisplay_cmap *)data;
384 		rc = sisfb_getcmap(fb->cmap, cm);
385 		return rc;
386 	case WSDISPLAYIO_PUTCMAP:
387 		cm = (struct wsdisplay_cmap *)data;
388 		rc = sisfb_putcmap(fb->cmap, cm);
389 		if (rc != 0)
390 			return rc;
391 		if (ri->ri_depth == 8)
392 			sisfb_loadcmap(fb, cm->index, cm->count);
393 		return 0;
394 	}
395 	return EPASSTHROUGH;
396 }
397 
398 int
399 sisfb_show_screen(void *v, void *cookie, int waitok,
400     void (*cb)(void *, int, int), void *cbarg)
401 {
402 	return 0;
403 }
404 
405 paddr_t
406 sisfb_mmap(void *v, void *vs, off_t offset, int prot)
407 {
408 	struct vcons_data *vd = v;
409 	struct sisfb_softc *sc = vd->cookie;
410 	struct rasops_info *ri = &sc->sc_fb->vcs.scr_ri;
411 
412 	if ((offset & PAGE_MASK) != 0)
413 		return -1;
414 
415 	if (offset < 0 || offset >= ri->ri_stride * ri->ri_height)
416 		return -1;
417 
418 	/*
419 	 * Don't allow mmap if the frame buffer area is not page aligned.
420 	 * XXX we should reprogram it to a page aligned boundary at attach
421 	 * XXX time if this isn't the case.
422 	 */
423 	if (((paddr_t)ri->ri_bits & PAGE_MASK) != 0)
424 		return -1;
425 
426 	/* return XKPHYS_TO_PHYS((paddr_t)ri->ri_bits) + offset; */
427 	/* XXX */ return -1;
428 }
429 
430 void
431 sisfb_init_screen(void *cookie, struct vcons_screen *scr,
432     int existing, long *defattr)
433 {
434 	struct sisfb_softc *sc = cookie;
435 	struct sisfb *fb = sc->sc_fb;
436 	struct rasops_info *ri = &scr->scr_ri;
437 
438 	ri->ri_depth = fb->fb_depth;
439 	ri->ri_width = fb->fb_width;
440 	ri->ri_height = fb->fb_height;
441 	ri->ri_stride = fb->fb_stride;
442 	ri->ri_flg = RI_CENTER | RI_FULLCLEAR;
443 
444 	ri->ri_bits = fb->fb_addr;
445 
446 	if (existing) {
447 		ri->ri_flg |= RI_CLEAR;
448 	}
449 
450 	rasops_init(ri, fb->fb_height / 8, fb->fb_width / 8);
451 	ri->ri_caps = WSSCREEN_WSCOLORS;
452 	rasops_reconfig(ri, fb->fb_height / ri->ri_font->fontheight,
453 		    fb->fb_width / ri->ri_font->fontwidth);
454 
455 	ri->ri_hw = scr;
456 }
457 
458 
459 /*
460  * Frame buffer initialization.
461  */
462 
463 int
464 sisfb_setup(struct sisfb *fb)
465 {
466 	struct rasops_info *ri = &fb->vcs.scr_ri;
467 	uint width, height, bpp;
468 	bus_size_t fbaddr;
469 	uint tmp;
470 
471 	/*
472 	 * Unlock access to extended registers.
473 	 */
474 
475 	sisfb_seq_write(fb, 0x05, 0x86);
476 
477 	/*
478 	 * Try and figure out display settings.
479 	 */
480 
481 	height = sisfb_crtc_read(fb, CRTC_VDE);
482 	tmp = sisfb_crtc_read(fb, CRTC_OVERFLL);
483 	if (ISSET(tmp, 1 << 1))
484 		height |= 1 << 8;
485 	if (ISSET(tmp, 1 << 6))
486 		height |= 1 << 9;
487 	tmp = sisfb_seq_read(fb, 0x0a);
488 	if (ISSET(tmp, 1 << 1))
489 		height |= 1 << 10;
490 	height++;
491 
492 	width = sisfb_crtc_read(fb, CRTC_HDISPLE);
493 	tmp = sisfb_seq_read(fb, 0x0b);
494 	if (ISSET(tmp, 1 << 2))
495 		width |= 1 << 8;
496 	if (ISSET(tmp, 1 << 3))
497 		width |= 1 << 9;
498 	width++;
499 	width <<= 3;
500 
501 #ifdef SIS_DEBUG
502 	printf("height %d width %d\n", height, width);
503 #endif
504 
505 	fbaddr = sisfb_crtc_read(fb, CRTC_STARTADRL) |
506 	    (sisfb_crtc_read(fb, CRTC_STARTADRH) << 8) |
507 	    (sisfb_seq_read(fb, 0x0d) << 16) |
508 	    ((sisfb_seq_read(fb, 0x37) & 0x03) << 24);
509 	fbaddr <<= 2;
510 #ifdef SIS_DEBUG
511 	printf("FBADDR %lx\n", fbaddr);
512 #endif
513 
514 	tmp = sisfb_seq_read(fb, 0x06);
515 	switch (tmp & 0x1c) {
516 	case 0x00:
517 		bpp = 8;
518 		break;
519 	case 0x04:
520 		bpp = 15;
521 		break;
522 	case 0x08:
523 		bpp = 16;
524 		break;
525 	case 0x10:
526 		bpp = 32;
527 		break;
528 	default:
529 		aprint_error("unknown bpp for 0x%x\n", tmp);
530 		return EINVAL;
531 	}
532 #ifdef SIS_DEBUG
533 	printf("BPP %d\n", bpp);
534 #endif
535 
536 	fb->fb_width = ri->ri_width = width;
537 	fb->fb_height = ri->ri_height = height;
538 	fb->fb_depth = ri->ri_depth = bpp;
539 	fb->fb_stride = ri->ri_stride = (ri->ri_width * ri->ri_depth) / 8;
540 	ri->ri_flg = /* RI_CENTER | RI_CLEAR | RI_FULLCLEAR */ RI_CENTER | RI_NO_AUTO;
541 	fb->fb_addr = ri->ri_bits =
542 	    (void *)((char *)bus_space_vaddr(fb->fbt, fb->fbh) + fbaddr);
543 	ri->ri_hw = fb;
544 
545 #ifdef SIS_DEBUG
546 	printf("ri_bits %p\n", ri->ri_bits);
547 #endif
548 
549 #ifdef __MIPSEL__
550 	/* swap B and R */
551 	switch (bpp) {
552 	case 15:
553 		ri->ri_rnum = 5;
554 		ri->ri_rpos = 10;
555 		ri->ri_gnum = 5;
556 		ri->ri_gpos = 5;
557 		ri->ri_bnum = 5;
558 		ri->ri_bpos = 0;
559 		break;
560 	case 16:
561 		ri->ri_rnum = 5;
562 		ri->ri_rpos = 11;
563 		ri->ri_gnum = 6;
564 		ri->ri_gpos = 5;
565 		ri->ri_bnum = 5;
566 		ri->ri_bpos = 0;
567 		break;
568 	}
569 #endif
570 
571 	bcopy(rasops_cmap, fb->cmap, sizeof(fb->cmap));
572 	if (bpp == 8) {
573 		sisfb_loadcmap(fb, 0, 256);
574 	}
575 
576 	rasops_init(ri, 30, 80);
577 	rasops_reconfig(ri, ri->ri_height / ri->ri_font->fontheight,
578 	    ri->ri_width / ri->ri_font->fontwidth);
579 
580 	fb->wsd.name = "std";
581 	fb->wsd.ncols = ri->ri_cols;
582 	fb->wsd.nrows = ri->ri_rows;
583 	fb->wsd.textops = &ri->ri_ops;
584 	fb->wsd.fontwidth = ri->ri_font->fontwidth;
585 	fb->wsd.fontheight = ri->ri_font->fontheight;
586 	fb->wsd.capabilities = ri->ri_caps;
587 
588 	return 0;
589 }
590 
591 /*
592  * Colormap handling routines.
593  */
594 
595 void
596 sisfb_loadcmap(struct sisfb *fb, int baseidx, int count)
597 {
598 	uint8_t *cmap = fb->cmap + baseidx * 3;
599 
600 	bus_space_write_1(fb->iot, fb->ioh, DAC_ADDR, baseidx);
601 	while (count-- != 0) {
602 		bus_space_write_1(fb->iot, fb->ioh, DAC_DATA, *cmap++ >> 2);
603 		bus_space_write_1(fb->iot, fb->ioh, DAC_DATA, *cmap++ >> 2);
604 		bus_space_write_1(fb->iot, fb->ioh, DAC_DATA, *cmap++ >> 2);
605 	}
606 }
607 
608 int
609 sisfb_getcmap(uint8_t *cmap, struct wsdisplay_cmap *cm)
610 {
611 	uint index = cm->index, count = cm->count, i;
612 	uint8_t ramp[256], *dst, *src;
613 	int rc;
614 
615 	if (index >= 256 || count > 256 - index)
616 		return EINVAL;
617 
618 	index *= 3;
619 
620 	src = cmap + index;
621 	dst = ramp;
622 	for (i = 0; i < count; i++)
623 		*dst++ = *src, src += 3;
624 	rc = copyout(ramp, cm->red, count);
625 	if (rc != 0)
626 		return rc;
627 
628 	src = cmap + index + 1;
629 	dst = ramp;
630 	for (i = 0; i < count; i++)
631 		*dst++ = *src, src += 3;
632 	rc = copyout(ramp, cm->green, count);
633 	if (rc != 0)
634 		return rc;
635 
636 	src = cmap + index + 2;
637 	dst = ramp;
638 	for (i = 0; i < count; i++)
639 		*dst++ = *src, src += 3;
640 	rc = copyout(ramp, cm->blue, count);
641 	if (rc != 0)
642 		return rc;
643 
644 	return 0;
645 }
646 
647 int
648 sisfb_putcmap(uint8_t *cmap, struct wsdisplay_cmap *cm)
649 {
650 	uint index = cm->index, count = cm->count, i;
651 	uint8_t ramp[256], *dst, *src;
652 	int rc;
653 
654 	if (index >= 256 || count > 256 - index)
655 		return EINVAL;
656 
657 	index *= 3;
658 
659 	rc = copyin(cm->red, ramp, count);
660 	if (rc != 0)
661 		return rc;
662 	dst = cmap + index;
663 	src = ramp;
664 	for (i = 0; i < count; i++)
665 		*dst = *src++, dst += 3;
666 
667 	rc = copyin(cm->green, ramp, count);
668 	if (rc != 0)
669 		return rc;
670 	dst = cmap + index + 1;
671 	src = ramp;
672 	for (i = 0; i < count; i++)
673 		*dst = *src++, dst += 3;
674 
675 	rc = copyin(cm->blue, ramp, count);
676 	if (rc != 0)
677 		return rc;
678 	dst = cmap + index + 2;
679 	src = ramp;
680 	for (i = 0; i < count; i++)
681 		*dst = *src++, dst += 3;
682 
683 	return 0;
684 }
685 
686 /*
687  * Early console code
688  */
689 
690 int
691 sisfb_cnattach(bus_space_tag_t memt, bus_space_tag_t iot,
692     pci_chipset_tag_t pc, pcitag_t tag, pcireg_t id)
693 {
694 	long defattr;
695 	struct rasops_info * const ri = &sisfbcn.vcs.scr_ri;
696 	pcireg_t bar;
697 	int rc;
698 
699 
700 	/* filter out unrecognized devices */
701 	switch (id) {
702 	default:
703 		return ENODEV;
704 	case PCI_ID_CODE(PCI_VENDOR_SIS, PCI_PRODUCT_SIS_315PRO_VGA):
705 		break;
706 	}
707 
708 	bar = pci_conf_read(pc, tag, PCI_MAPREG_START);
709 	if (PCI_MAPREG_TYPE(bar) != PCI_MAPREG_TYPE_MEM)
710 		return EINVAL;
711 	sisfbcn.fbt = memt;
712 	rc = bus_space_map(memt, PCI_MAPREG_MEM_ADDR(bar), 1 /* XXX */,
713 	    BUS_SPACE_MAP_LINEAR, &sisfbcn.fbh);
714 #ifdef SIS_DEBUG
715 	printf("sisfb_cnattach(memt, 0x%x rc %d\n", PCI_MAPREG_MEM_ADDR(bar), rc);
716 #endif
717 	if (rc != 0)
718 		return rc;
719 
720 	bar = pci_conf_read(pc, tag, PCI_MAPREG_START + 4);
721 	if (PCI_MAPREG_TYPE(bar) != PCI_MAPREG_TYPE_MEM)
722 		return EINVAL;
723 	sisfbcn.mmiot = memt;
724 	rc = bus_space_map(memt, PCI_MAPREG_MEM_ADDR(bar), 1 /* XXX */,
725 	    BUS_SPACE_MAP_LINEAR, &sisfbcn.mmioh);
726 #ifdef SIS_DEBUG
727 	printf("sisfb_cnattach(memt2, 0x%x rc %d\n", PCI_MAPREG_MEM_ADDR(bar), rc);
728 #endif
729 	if (rc != 0)
730 		return rc;
731 
732 	bar = pci_conf_read(pc, tag, PCI_MAPREG_START + 8);
733 	if (PCI_MAPREG_TYPE(bar) != PCI_MAPREG_TYPE_IO)
734 		return EINVAL;
735 	sisfbcn.iot = iot;
736 	rc = bus_space_map(iot, PCI_MAPREG_MEM_ADDR(bar), 1 /* XXX */,
737 	    0, &sisfbcn.ioh);
738 #ifdef SIS_DEBUG
739 	printf("sisfb_cnattach(iot, 0x%x rc %d\n", PCI_MAPREG_MEM_ADDR(bar), rc);
740 #endif
741 	if (rc != 0)
742 		return rc;
743 
744 	rc = sisfb_setup(&sisfbcn);
745 #ifdef SIS_DEBUG
746 	printf("sisfb_setup %d %p\n", rc, sisfbcn.vcs.scr_ri.ri_hw);
747 #endif
748 	if (rc != 0)
749 		return rc;
750 
751 	ri->ri_ops.allocattr(ri, 0,  ri->ri_rows - 1, 0, &defattr);
752 	wsdisplay_preattach(&sisfbcn.wsd, ri, 0, 0, defattr);
753 
754 	return 0;
755 }
756