xref: /netbsd-src/sys/dev/pci/r128fb.c (revision a4ddc2c8fb9af816efe3b1c375a5530aef0e89e9)
1 /*	$NetBSD: r128fb.c,v 1.36 2012/10/04 10:22:45 macallan Exp $	*/
2 
3 /*
4  * Copyright (c) 2007, 2012 Michael Lorenz
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 /*
29  * A console driver for ATI Rage 128 graphics controllers
30  * tested on macppc only so far
31  */
32 
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: r128fb.c,v 1.36 2012/10/04 10:22:45 macallan Exp $");
35 
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
39 #include <sys/device.h>
40 #include <sys/malloc.h>
41 #include <sys/lwp.h>
42 #include <sys/kauth.h>
43 
44 #include <dev/videomode/videomode.h>
45 
46 #include <dev/pci/pcivar.h>
47 #include <dev/pci/pcireg.h>
48 #include <dev/pci/pcidevs.h>
49 #include <dev/pci/pciio.h>
50 #include <dev/pci/r128fbreg.h>
51 
52 #include <dev/wscons/wsdisplayvar.h>
53 #include <dev/wscons/wsconsio.h>
54 #include <dev/wsfont/wsfont.h>
55 #include <dev/rasops/rasops.h>
56 #include <dev/wscons/wsdisplay_vconsvar.h>
57 #include <dev/pci/wsdisplay_pci.h>
58 #include <dev/wscons/wsdisplay_glyphcachevar.h>
59 
60 #include <dev/i2c/i2cvar.h>
61 
62 #include "opt_r128fb.h"
63 #include "opt_vcons.h"
64 
65 #ifdef R128FB_DEBUG
66 #define DPRINTF printf
67 #else
68 #define DPRINTF while(0) printf
69 #endif
70 
71 struct r128fb_softc {
72 	device_t sc_dev;
73 
74 	pci_chipset_tag_t sc_pc;
75 	pcitag_t sc_pcitag;
76 
77 	bus_space_tag_t sc_memt;
78 	bus_space_tag_t sc_iot;
79 
80 	bus_space_handle_t sc_regh;
81 	bus_addr_t sc_fb, sc_reg;
82 	bus_size_t sc_fbsize, sc_regsize;
83 
84 	int sc_width, sc_height, sc_depth, sc_stride;
85 	int sc_locked, sc_have_backlight, sc_bl_level, sc_bl_on;
86 	struct vcons_screen sc_console_screen;
87 	struct wsscreen_descr sc_defaultscreen_descr;
88 	const struct wsscreen_descr *sc_screens[1];
89 	struct wsscreen_list sc_screenlist;
90 	struct vcons_data vd;
91 	int sc_mode;
92 	u_char sc_cmap_red[256];
93 	u_char sc_cmap_green[256];
94 	u_char sc_cmap_blue[256];
95 	/* engine stuff */
96 	uint32_t sc_master_cntl;
97 	glyphcache sc_gc;
98 };
99 
100 static int	r128fb_match(device_t, cfdata_t, void *);
101 static void	r128fb_attach(device_t, device_t, void *);
102 
103 CFATTACH_DECL_NEW(r128fb, sizeof(struct r128fb_softc),
104     r128fb_match, r128fb_attach, NULL, NULL);
105 
106 static int	r128fb_ioctl(void *, void *, u_long, void *, int,
107 			     struct lwp *);
108 static paddr_t	r128fb_mmap(void *, void *, off_t, int);
109 static void	r128fb_init_screen(void *, struct vcons_screen *, int, long *);
110 
111 static int	r128fb_putcmap(struct r128fb_softc *, struct wsdisplay_cmap *);
112 static int 	r128fb_getcmap(struct r128fb_softc *, struct wsdisplay_cmap *);
113 static void	r128fb_restore_palette(struct r128fb_softc *);
114 static int 	r128fb_putpalreg(struct r128fb_softc *, uint8_t, uint8_t,
115 			    uint8_t, uint8_t);
116 
117 static void	r128fb_init(struct r128fb_softc *);
118 static void	r128fb_flush_engine(struct r128fb_softc *);
119 static void	r128fb_rectfill(struct r128fb_softc *, int, int, int, int,
120 			    uint32_t);
121 static void	r128fb_bitblt(void *, int, int, int, int, int,
122 			    int, int);
123 
124 static void	r128fb_cursor(void *, int, int, int);
125 static void	r128fb_putchar(void *, int, int, u_int, long);
126 static void	r128fb_putchar_aa(void *, int, int, u_int, long);
127 static void	r128fb_copycols(void *, int, int, int, int);
128 static void	r128fb_erasecols(void *, int, int, int, long);
129 static void	r128fb_copyrows(void *, int, int, int);
130 static void	r128fb_eraserows(void *, int, int, long);
131 
132 static void	r128fb_brightness_up(device_t);
133 static void	r128fb_brightness_down(device_t);
134 /* set backlight level */
135 static void	r128fb_set_backlight(struct r128fb_softc *, int);
136 /* turn backlight on and off without messing with the level */
137 static void	r128fb_switch_backlight(struct r128fb_softc *, int);
138 
139 struct wsdisplay_accessops r128fb_accessops = {
140 	r128fb_ioctl,
141 	r128fb_mmap,
142 	NULL,	/* alloc_screen */
143 	NULL,	/* free_screen */
144 	NULL,	/* show_screen */
145 	NULL, 	/* load_font */
146 	NULL,	/* pollc */
147 	NULL	/* scroll */
148 };
149 
150 static inline void
151 r128fb_wait(struct r128fb_softc *sc, int slots)
152 {
153 	uint32_t reg;
154 
155 	do {
156 		reg = (bus_space_read_4(sc->sc_memt, sc->sc_regh,
157 		    R128_GUI_STAT) & R128_GUI_FIFOCNT_MASK);
158 	} while (reg <= slots);
159 }
160 
161 static void
162 r128fb_flush_engine(struct r128fb_softc *sc)
163 {
164 	uint32_t reg;
165 
166 	reg = bus_space_read_4(sc->sc_memt, sc->sc_regh, R128_PC_NGUI_CTLSTAT);
167 	reg |= R128_PC_FLUSH_ALL;
168 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_PC_NGUI_CTLSTAT, reg);
169 	do {
170 		reg = bus_space_read_4(sc->sc_memt, sc->sc_regh,
171 		    R128_PC_NGUI_CTLSTAT);
172 	} while (reg & R128_PC_BUSY);
173 }
174 
175 static int
176 r128fb_match(device_t parent, cfdata_t match, void *aux)
177 {
178 	struct pci_attach_args *pa = (struct pci_attach_args *)aux;
179 
180 	if (PCI_CLASS(pa->pa_class) != PCI_CLASS_DISPLAY)
181 		return 0;
182 	if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_ATI)
183 		return 0;
184 
185 	/* only cards tested on so far - likely need a list */
186 	if ((PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_ATI_RAGE1AGP4XT) ||
187 	    (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_ATI_RAGE3AGP4XT) ||
188 	    (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_ATI_RAGEGLPCI) ||
189 	    (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_ATI_RAGE_MOB_M3_AGP))
190 		return 100;
191 	return (0);
192 }
193 
194 static void
195 r128fb_attach(device_t parent, device_t self, void *aux)
196 {
197 	struct r128fb_softc	*sc = device_private(self);
198 	struct pci_attach_args	*pa = aux;
199 	struct rasops_info	*ri;
200 	bus_space_tag_t		tag;
201 	struct wsemuldisplaydev_attach_args aa;
202 	prop_dictionary_t	dict;
203 	unsigned long		defattr;
204 	bool			is_console;
205 	int			i, j;
206 	uint32_t		reg, flags;
207 	uint8_t			cmap[768];
208 
209 	sc->sc_pc = pa->pa_pc;
210 	sc->sc_pcitag = pa->pa_tag;
211 	sc->sc_memt = pa->pa_memt;
212 	sc->sc_iot = pa->pa_iot;
213 	sc->sc_dev = self;
214 
215 	pci_aprint_devinfo(pa, NULL);
216 
217 	/* fill in parameters from properties */
218 	dict = device_properties(self);
219 	if (!prop_dictionary_get_uint32(dict, "width", &sc->sc_width)) {
220 		aprint_error("%s: no width property\n", device_xname(self));
221 		return;
222 	}
223 	if (!prop_dictionary_get_uint32(dict, "height", &sc->sc_height)) {
224 		aprint_error("%s: no height property\n", device_xname(self));
225 		return;
226 	}
227 
228 #ifdef GLYPHCACHE_DEBUG
229 	/* leave some visible VRAM unused so we can see the glyph cache */
230 	sc->sc_height -= 200;
231 #endif
232 
233 	if (!prop_dictionary_get_uint32(dict, "depth", &sc->sc_depth)) {
234 		aprint_error("%s: no depth property\n", device_xname(self));
235 		return;
236 	}
237 	if (!prop_dictionary_get_uint32(dict, "linebytes", &sc->sc_stride)) {
238 		aprint_error("%s: no linebytes property\n",
239 		    device_xname(self));
240 		return;
241 	}
242 
243 	prop_dictionary_get_bool(dict, "is_console", &is_console);
244 
245 	if (pci_mapreg_info(pa->pa_pc, pa->pa_tag, 0x10, PCI_MAPREG_TYPE_MEM,
246 	    &sc->sc_fb, &sc->sc_fbsize, &flags)) {
247 		aprint_error("%s: failed to map the frame buffer.\n",
248 		    device_xname(sc->sc_dev));
249 	}
250 
251 	if (pci_mapreg_map(pa, 0x18, PCI_MAPREG_TYPE_MEM, 0,
252 	    &tag, &sc->sc_regh, &sc->sc_reg, &sc->sc_regsize)) {
253 		aprint_error("%s: failed to map registers.\n",
254 		    device_xname(sc->sc_dev));
255 	}
256 
257 	aprint_normal("%s: %d MB aperture at 0x%08x\n", device_xname(self),
258 	    (int)(sc->sc_fbsize >> 20), (uint32_t)sc->sc_fb);
259 
260 	sc->sc_defaultscreen_descr = (struct wsscreen_descr){
261 		"default",
262 		0, 0,
263 		NULL,
264 		8, 16,
265 		WSSCREEN_WSCOLORS | WSSCREEN_HILIT,
266 		NULL
267 	};
268 	sc->sc_screens[0] = &sc->sc_defaultscreen_descr;
269 	sc->sc_screenlist = (struct wsscreen_list){1, sc->sc_screens};
270 	sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
271 	sc->sc_locked = 0;
272 
273 	vcons_init(&sc->vd, sc, &sc->sc_defaultscreen_descr,
274 	    &r128fb_accessops);
275 	sc->vd.init_screen = r128fb_init_screen;
276 
277 	/* init engine here */
278 	r128fb_init(sc);
279 
280 	ri = &sc->sc_console_screen.scr_ri;
281 
282 	sc->sc_gc.gc_bitblt = r128fb_bitblt;
283 	sc->sc_gc.gc_blitcookie = sc;
284 	sc->sc_gc.gc_rop = R128_ROP3_S;
285 	if (is_console) {
286 		vcons_init_screen(&sc->vd, &sc->sc_console_screen, 1,
287 		    &defattr);
288 		sc->sc_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
289 
290 		r128fb_rectfill(sc, 0, 0, sc->sc_width, sc->sc_height,
291 		    ri->ri_devcmap[(defattr >> 16) & 0xff]);
292 		sc->sc_defaultscreen_descr.textops = &ri->ri_ops;
293 		sc->sc_defaultscreen_descr.capabilities = ri->ri_caps;
294 		sc->sc_defaultscreen_descr.nrows = ri->ri_rows;
295 		sc->sc_defaultscreen_descr.ncols = ri->ri_cols;
296 		glyphcache_init(&sc->sc_gc, sc->sc_height + 5,
297 				(0x800000 / sc->sc_stride) - sc->sc_height - 5,
298 				sc->sc_width,
299 				ri->ri_font->fontwidth,
300 				ri->ri_font->fontheight,
301 				defattr);
302 		wsdisplay_cnattach(&sc->sc_defaultscreen_descr, ri, 0, 0,
303 		    defattr);
304 		vcons_replay_msgbuf(&sc->sc_console_screen);
305 	} else {
306 		/*
307 		 * since we're not the console we can postpone the rest
308 		 * until someone actually allocates a screen for us
309 		 */
310 		if (sc->sc_console_screen.scr_ri.ri_rows == 0) {
311 			/* do some minimal setup to avoid weirdnesses later */
312 			vcons_init_screen(&sc->vd, &sc->sc_console_screen, 1,
313 			    &defattr);
314 		}
315 		glyphcache_init(&sc->sc_gc, sc->sc_height + 5,
316 				(0x800000 / sc->sc_stride) - sc->sc_height - 5,
317 				sc->sc_width,
318 				ri->ri_font->fontwidth,
319 				ri->ri_font->fontheight,
320 				defattr);
321 	}
322 
323 	j = 0;
324 	rasops_get_cmap(ri, cmap, sizeof(cmap));
325 	for (i = 0; i < 256; i++) {
326 		sc->sc_cmap_red[i] = cmap[j];
327 		sc->sc_cmap_green[i] = cmap[j + 1];
328 		sc->sc_cmap_blue[i] = cmap[j + 2];
329 		r128fb_putpalreg(sc, i, cmap[j], cmap[j + 1], cmap[j + 2]);
330 		j += 3;
331 	}
332 
333 	/* no suspend/resume support yet */
334 	pmf_device_register(sc->sc_dev, NULL, NULL);
335 
336 	reg = bus_space_read_4(sc->sc_memt, sc->sc_regh, R128_LVDS_GEN_CNTL);
337 	DPRINTF("R128_LVDS_GEN_CNTL: %08x\n", reg);
338 	if (reg & R128_LVDS_ON) {
339 		sc->sc_have_backlight = 1;
340 		sc->sc_bl_on = 1;
341 		sc->sc_bl_level = 255 -
342 		    ((reg & R128_LEVEL_MASK) >> R128_LEVEL_SHIFT);
343 		pmf_event_register(sc->sc_dev, PMFE_DISPLAY_BRIGHTNESS_UP,
344 		    r128fb_brightness_up, TRUE);
345 		pmf_event_register(sc->sc_dev, PMFE_DISPLAY_BRIGHTNESS_DOWN,
346 		    r128fb_brightness_down, TRUE);
347 		aprint_verbose("%s: LVDS output is active, enabling backlight"
348 			       " control\n", device_xname(self));
349 	} else
350 		sc->sc_have_backlight = 0;
351 
352 	aa.console = is_console;
353 	aa.scrdata = &sc->sc_screenlist;
354 	aa.accessops = &r128fb_accessops;
355 	aa.accesscookie = &sc->vd;
356 
357 	config_found(sc->sc_dev, &aa, wsemuldisplaydevprint);
358 }
359 
360 static int
361 r128fb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
362 	struct lwp *l)
363 {
364 	struct vcons_data *vd = v;
365 	struct r128fb_softc *sc = vd->cookie;
366 	struct wsdisplay_fbinfo *wdf;
367 	struct vcons_screen *ms = vd->active;
368 	struct wsdisplay_param  *param;
369 
370 	switch (cmd) {
371 	case WSDISPLAYIO_GTYPE:
372 		*(u_int *)data = WSDISPLAY_TYPE_PCIMISC;
373 		return 0;
374 
375 	/* PCI config read/write passthrough. */
376 	case PCI_IOC_CFGREAD:
377 	case PCI_IOC_CFGWRITE:
378 		return pci_devioctl(sc->sc_pc, sc->sc_pcitag,
379 		    cmd, data, flag, l);
380 
381 	case WSDISPLAYIO_GET_BUSID:
382 		return wsdisplayio_busid_pci(sc->sc_dev, sc->sc_pc,
383 		    sc->sc_pcitag, data);
384 
385 	case WSDISPLAYIO_GINFO:
386 		if (ms == NULL)
387 			return ENODEV;
388 		wdf = (void *)data;
389 		wdf->height = ms->scr_ri.ri_height;
390 		wdf->width = ms->scr_ri.ri_width;
391 		wdf->depth = ms->scr_ri.ri_depth;
392 		wdf->cmsize = 256;
393 		return 0;
394 
395 	case WSDISPLAYIO_GETCMAP:
396 		return r128fb_getcmap(sc,
397 		    (struct wsdisplay_cmap *)data);
398 
399 	case WSDISPLAYIO_PUTCMAP:
400 		return r128fb_putcmap(sc,
401 		    (struct wsdisplay_cmap *)data);
402 
403 	case WSDISPLAYIO_LINEBYTES:
404 		*(u_int *)data = sc->sc_stride;
405 		return 0;
406 
407 	case WSDISPLAYIO_SMODE: {
408 		int new_mode = *(int*)data;
409 		if (new_mode != sc->sc_mode) {
410 			sc->sc_mode = new_mode;
411 			if(new_mode == WSDISPLAYIO_MODE_EMUL) {
412 				r128fb_init(sc);
413 				r128fb_restore_palette(sc);
414 				glyphcache_wipe(&sc->sc_gc);
415 				r128fb_rectfill(sc, 0, 0, sc->sc_width,
416 				    sc->sc_height, ms->scr_ri.ri_devcmap[
417 				    (ms->scr_defattr >> 16) & 0xff]);
418 				vcons_redraw_screen(ms);
419 			}
420 		}
421 		}
422 		return 0;
423 
424 	case WSDISPLAYIO_GETPARAM:
425 		param = (struct wsdisplay_param *)data;
426 		if (sc->sc_have_backlight == 0)
427 			return EPASSTHROUGH;
428 		switch (param->param) {
429 		case WSDISPLAYIO_PARAM_BRIGHTNESS:
430 			param->min = 0;
431 			param->max = 255;
432 			param->curval = sc->sc_bl_level;
433 			return 0;
434 		case WSDISPLAYIO_PARAM_BACKLIGHT:
435 			param->min = 0;
436 			param->max = 1;
437 			param->curval = sc->sc_bl_on;
438 			return 0;
439 		}
440 		return EPASSTHROUGH;
441 
442 	case WSDISPLAYIO_SETPARAM:
443 		param = (struct wsdisplay_param *)data;
444 		if (sc->sc_have_backlight == 0)
445 			return EPASSTHROUGH;
446 		switch (param->param) {
447 		case WSDISPLAYIO_PARAM_BRIGHTNESS:
448 			r128fb_set_backlight(sc, param->curval);
449 			return 0;
450 		case WSDISPLAYIO_PARAM_BACKLIGHT:
451 			r128fb_switch_backlight(sc,  param->curval);
452 			return 0;
453 		}
454 		return EPASSTHROUGH;
455 	case WSDISPLAYIO_GET_EDID: {
456 		struct wsdisplayio_edid_info *d = data;
457 		return wsdisplayio_get_edid(sc->sc_dev, d);
458 	}
459 	}
460 	return EPASSTHROUGH;
461 }
462 
463 static paddr_t
464 r128fb_mmap(void *v, void *vs, off_t offset, int prot)
465 {
466 	struct vcons_data *vd = v;
467 	struct r128fb_softc *sc = vd->cookie;
468 	paddr_t pa;
469 
470 	/* 'regular' framebuffer mmap()ing */
471 	if (offset < sc->sc_fbsize) {
472 		pa = bus_space_mmap(sc->sc_memt, sc->sc_fb + offset, 0, prot,
473 		    BUS_SPACE_MAP_LINEAR);
474 		return pa;
475 	}
476 
477 	/*
478 	 * restrict all other mappings to processes with superuser privileges
479 	 * or the kernel itself
480 	 */
481 	if (kauth_authorize_machdep(kauth_cred_get(), KAUTH_MACHDEP_UNMANAGEDMEM,
482 	    NULL, NULL, NULL, NULL) != 0) {
483 		aprint_normal("%s: mmap() rejected.\n",
484 		    device_xname(sc->sc_dev));
485 		return -1;
486 	}
487 
488 	if ((offset >= sc->sc_fb) && (offset < (sc->sc_fb + sc->sc_fbsize))) {
489 		pa = bus_space_mmap(sc->sc_memt, offset, 0, prot,
490 		    BUS_SPACE_MAP_LINEAR);
491 		return pa;
492 	}
493 
494 	if ((offset >= sc->sc_reg) &&
495 	    (offset < (sc->sc_reg + sc->sc_regsize))) {
496 		pa = bus_space_mmap(sc->sc_memt, offset, 0, prot,
497 		    BUS_SPACE_MAP_LINEAR);
498 		return pa;
499 	}
500 
501 #ifdef PCI_MAGIC_IO_RANGE
502 	/* allow mapping of IO space */
503 	if ((offset >= PCI_MAGIC_IO_RANGE) &&
504 	    (offset < PCI_MAGIC_IO_RANGE + 0x10000)) {
505 		pa = bus_space_mmap(sc->sc_iot, offset - PCI_MAGIC_IO_RANGE,
506 		    0, prot, BUS_SPACE_MAP_LINEAR);
507 		return pa;
508 	}
509 #endif
510 
511 #ifdef OFB_ALLOW_OTHERS
512 	if (offset >= 0x80000000) {
513 		pa = bus_space_mmap(sc->sc_memt, offset, 0, prot,
514 		    BUS_SPACE_MAP_LINEAR);
515 		return pa;
516 	}
517 #endif
518 	return -1;
519 }
520 
521 static void
522 r128fb_init_screen(void *cookie, struct vcons_screen *scr,
523     int existing, long *defattr)
524 {
525 	struct r128fb_softc *sc = cookie;
526 	struct rasops_info *ri = &scr->scr_ri;
527 
528 	ri->ri_depth = sc->sc_depth;
529 	ri->ri_width = sc->sc_width;
530 	ri->ri_height = sc->sc_height;
531 	ri->ri_stride = sc->sc_stride;
532 	ri->ri_flg = RI_CENTER;
533 	if (sc->sc_depth == 8)
534 		ri->ri_flg |= RI_8BIT_IS_RGB | RI_ENABLE_ALPHA;
535 
536 	rasops_init(ri, 0, 0);
537 	ri->ri_caps = WSSCREEN_WSCOLORS;
538 #ifdef VCONS_DRAW_INTR
539 	scr->scr_flags |= VCONS_DONT_READ;
540 #endif
541 
542 	rasops_reconfig(ri, sc->sc_height / ri->ri_font->fontheight,
543 		    sc->sc_width / ri->ri_font->fontwidth);
544 
545 	ri->ri_hw = scr;
546 	ri->ri_ops.copyrows = r128fb_copyrows;
547 	ri->ri_ops.copycols = r128fb_copycols;
548 	ri->ri_ops.eraserows = r128fb_eraserows;
549 	ri->ri_ops.erasecols = r128fb_erasecols;
550 	ri->ri_ops.cursor = r128fb_cursor;
551 	if (FONT_IS_ALPHA(ri->ri_font)) {
552 		ri->ri_ops.putchar = r128fb_putchar_aa;
553 	} else
554 		ri->ri_ops.putchar = r128fb_putchar;
555 }
556 
557 static int
558 r128fb_putcmap(struct r128fb_softc *sc, struct wsdisplay_cmap *cm)
559 {
560 	u_char *r, *g, *b;
561 	u_int index = cm->index;
562 	u_int count = cm->count;
563 	int i, error;
564 	u_char rbuf[256], gbuf[256], bbuf[256];
565 
566 #ifdef R128FB_DEBUG
567 	aprint_debug("putcmap: %d %d\n",index, count);
568 #endif
569 	if (cm->index >= 256 || cm->count > 256 ||
570 	    (cm->index + cm->count) > 256)
571 		return EINVAL;
572 	error = copyin(cm->red, &rbuf[index], count);
573 	if (error)
574 		return error;
575 	error = copyin(cm->green, &gbuf[index], count);
576 	if (error)
577 		return error;
578 	error = copyin(cm->blue, &bbuf[index], count);
579 	if (error)
580 		return error;
581 
582 	memcpy(&sc->sc_cmap_red[index], &rbuf[index], count);
583 	memcpy(&sc->sc_cmap_green[index], &gbuf[index], count);
584 	memcpy(&sc->sc_cmap_blue[index], &bbuf[index], count);
585 
586 	r = &sc->sc_cmap_red[index];
587 	g = &sc->sc_cmap_green[index];
588 	b = &sc->sc_cmap_blue[index];
589 
590 	for (i = 0; i < count; i++) {
591 		r128fb_putpalreg(sc, index, *r, *g, *b);
592 		index++;
593 		r++, g++, b++;
594 	}
595 	return 0;
596 }
597 
598 static int
599 r128fb_getcmap(struct r128fb_softc *sc, struct wsdisplay_cmap *cm)
600 {
601 	u_int index = cm->index;
602 	u_int count = cm->count;
603 	int error;
604 
605 	if (index >= 255 || count > 256 || index + count > 256)
606 		return EINVAL;
607 
608 	error = copyout(&sc->sc_cmap_red[index],   cm->red,   count);
609 	if (error)
610 		return error;
611 	error = copyout(&sc->sc_cmap_green[index], cm->green, count);
612 	if (error)
613 		return error;
614 	error = copyout(&sc->sc_cmap_blue[index],  cm->blue,  count);
615 	if (error)
616 		return error;
617 
618 	return 0;
619 }
620 
621 static void
622 r128fb_restore_palette(struct r128fb_softc *sc)
623 {
624 	int i;
625 
626 	for (i = 0; i < (1 << sc->sc_depth); i++) {
627 		r128fb_putpalreg(sc, i, sc->sc_cmap_red[i],
628 		    sc->sc_cmap_green[i], sc->sc_cmap_blue[i]);
629 	}
630 }
631 
632 static int
633 r128fb_putpalreg(struct r128fb_softc *sc, uint8_t idx, uint8_t r, uint8_t g,
634     uint8_t b)
635 {
636 	uint32_t reg;
637 
638 	/* whack the DAC */
639 	reg = (r << 16) | (g << 8) | b;
640 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_PALETTE_INDEX, idx);
641 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_PALETTE_DATA, reg);
642 	return 0;
643 }
644 
645 static void
646 r128fb_init(struct r128fb_softc *sc)
647 {
648 	uint32_t datatype, d, reg;
649 
650 	r128fb_flush_engine(sc);
651 
652 	r128fb_wait(sc, 9);
653 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_CRTC_OFFSET, 0);
654 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DEFAULT_OFFSET, 0);
655 	/* pitch is in units of 8 pixels */
656 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DEFAULT_PITCH,
657 	    sc->sc_width >> 3);
658 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_AUX_SC_CNTL, 0);
659 	bus_space_write_4(sc->sc_memt, sc->sc_regh,
660 	    R128_DEFAULT_SC_BOTTOM_RIGHT,
661 	    R128_DEFAULT_SC_RIGHT_MAX | R128_DEFAULT_SC_BOTTOM_MAX);
662 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_SC_TOP_LEFT, 0);
663 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_SC_BOTTOM_RIGHT,
664 	    R128_DEFAULT_SC_RIGHT_MAX | R128_DEFAULT_SC_BOTTOM_MAX);
665 	bus_space_write_4(sc->sc_memt, sc->sc_regh,
666 	    R128_DEFAULT_SC_BOTTOM_RIGHT,
667 	    R128_DEFAULT_SC_RIGHT_MAX | R128_DEFAULT_SC_BOTTOM_MAX);
668 
669 #if 0
670 #if BYTE_ORDER == BIG_ENDIAN
671 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DP_DATATYPE,
672 	    R128_HOST_BIG_ENDIAN_EN);
673 #else
674 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DP_DATATYPE, 0);
675 #endif
676 #endif
677 	r128fb_wait(sc, 7);
678 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_SRC_PITCH,
679 	    sc->sc_width >> 3);
680 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DST_PITCH,
681 	    sc->sc_width >> 3);
682 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_SRC_OFFSET, 0);
683 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DST_OFFSET, 0);
684 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DP_WRITE_MASK,
685 	    0xffffffff);
686 
687 	switch (sc->sc_depth) {
688 		case 8:
689 			datatype = R128_GMC_DST_8BPP_CI;
690 			d = R128_CRTC_COLOR_8BIT;
691 			break;
692 		case 15:
693 			datatype = R128_GMC_DST_15BPP;
694 			d = R128_CRTC_COLOR_15BIT;
695 			break;
696 		case 16:
697 			datatype = R128_GMC_DST_16BPP;
698 			d = R128_CRTC_COLOR_16BIT;
699 			break;
700 		case 24:
701 			datatype = R128_GMC_DST_24BPP;
702 			d = R128_CRTC_COLOR_24BIT;
703 			break;
704 		case 32:
705 			datatype = R128_GMC_DST_32BPP;
706 			d = R128_CRTC_COLOR_32BIT;
707 			break;
708 		default:
709 			aprint_error("%s: unsupported depth %d\n",
710 			    device_xname(sc->sc_dev), sc->sc_depth);
711 			return;
712 	}
713 	sc->sc_master_cntl = R128_GMC_CLR_CMP_CNTL_DIS |
714 	    R128_GMC_AUX_CLIP_DIS | datatype;
715 	reg = bus_space_read_4(sc->sc_memt, sc->sc_regh, R128_CRTC_GEN_CNTL);
716 	DPRINTF("depth: %d\n", reg & R128_CRTC_PIX_WIDTH);
717 	reg &= ~R128_CRTC_PIX_WIDTH;
718 	reg |= d;
719 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_CRTC_GEN_CNTL, reg);
720 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_CRTC_PITCH, sc->sc_width >> 3);
721 	r128fb_flush_engine(sc);
722 }
723 
724 static void
725 r128fb_rectfill(struct r128fb_softc *sc, int x, int y, int wi, int he,
726      uint32_t colour)
727 {
728 
729 	r128fb_wait(sc, 5);
730 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DP_GUI_MASTER_CNTL,
731 	    R128_GMC_BRUSH_SOLID_COLOR |
732 	    R128_GMC_SRC_DATATYPE_COLOR |
733 	    R128_ROP3_P |
734 	    sc->sc_master_cntl);
735 
736 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DP_BRUSH_FRGD_CLR,
737 	    colour);
738 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DP_CNTL,
739 	    R128_DST_X_LEFT_TO_RIGHT | R128_DST_Y_TOP_TO_BOTTOM);
740 	/* now feed it coordinates */
741 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DST_X_Y,
742 	    (x << 16) | y);
743 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DST_WIDTH_HEIGHT,
744 	    (wi << 16) | he);
745 }
746 
747 static void
748 r128fb_bitblt(void *cookie, int xs, int ys, int xd, int yd,
749     int wi, int he, int rop)
750 {
751 	struct r128fb_softc *sc = cookie;
752 	uint32_t dp_cntl = 0;
753 
754 	r128fb_wait(sc, 5);
755 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DP_GUI_MASTER_CNTL,
756 	    R128_GMC_BRUSH_SOLID_COLOR |
757 	    R128_GMC_SRC_DATATYPE_COLOR |
758 	    rop |
759 	    R128_DP_SRC_SOURCE_MEMORY |
760 	    sc->sc_master_cntl);
761 
762 	if (yd <= ys) {
763 		dp_cntl = R128_DST_Y_TOP_TO_BOTTOM;
764 	} else {
765 		ys += he - 1;
766 		yd += he - 1;
767 	}
768 	if (xd <= xs) {
769 		dp_cntl |= R128_DST_X_LEFT_TO_RIGHT;
770 	} else {
771 		xs += wi - 1;
772 		xd += wi - 1;
773 	}
774 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DP_CNTL, dp_cntl);
775 
776 	/* now feed it coordinates */
777 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_SRC_X_Y,
778 	    (xs << 16) | ys);
779 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DST_X_Y,
780 	    (xd << 16) | yd);
781 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DST_WIDTH_HEIGHT,
782 	    (wi << 16) | he);
783 }
784 
785 static void
786 r128fb_cursor(void *cookie, int on, int row, int col)
787 {
788 	struct rasops_info *ri = cookie;
789 	struct vcons_screen *scr = ri->ri_hw;
790 	struct r128fb_softc *sc = scr->scr_cookie;
791 	int x, y, wi, he;
792 
793 	wi = ri->ri_font->fontwidth;
794 	he = ri->ri_font->fontheight;
795 
796 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
797 		x = ri->ri_ccol * wi + ri->ri_xorigin;
798 		y = ri->ri_crow * he + ri->ri_yorigin;
799 		if (ri->ri_flg & RI_CURSOR) {
800 			r128fb_bitblt(sc, x, y, x, y, wi, he, R128_ROP3_Dn);
801 			ri->ri_flg &= ~RI_CURSOR;
802 		}
803 		ri->ri_crow = row;
804 		ri->ri_ccol = col;
805 		if (on) {
806 			x = ri->ri_ccol * wi + ri->ri_xorigin;
807 			y = ri->ri_crow * he + ri->ri_yorigin;
808 			r128fb_bitblt(sc, x, y, x, y, wi, he, R128_ROP3_Dn);
809 			ri->ri_flg |= RI_CURSOR;
810 		}
811 	} else {
812 		scr->scr_ri.ri_crow = row;
813 		scr->scr_ri.ri_ccol = col;
814 		scr->scr_ri.ri_flg &= ~RI_CURSOR;
815 	}
816 
817 }
818 
819 static void
820 r128fb_putchar(void *cookie, int row, int col, u_int c, long attr)
821 {
822 	struct rasops_info *ri = cookie;
823 	struct wsdisplay_font *font = PICK_FONT(ri, c);
824 	struct vcons_screen *scr = ri->ri_hw;
825 	struct r128fb_softc *sc = scr->scr_cookie;
826 	void *data;
827 	uint32_t fg, bg;
828 	int i, x, y, wi, he, offset;
829 
830 	if (sc->sc_mode != WSDISPLAYIO_MODE_EMUL)
831 		return;
832 
833 	if (!CHAR_IN_FONT(c, font))
834 		return;
835 
836 	wi = font->fontwidth;
837 	he = font->fontheight;
838 
839 	bg = ri->ri_devcmap[(attr >> 16) & 0xf];
840 	fg = ri->ri_devcmap[(attr >> 24) & 0xf];
841 	x = ri->ri_xorigin + col * wi;
842 	y = ri->ri_yorigin + row * he;
843 
844 	if (c == 0x20) {
845 		r128fb_rectfill(sc, x, y, wi, he, bg);
846 		return;
847 	}
848 
849 	data = WSFONT_GLYPH(c, font);
850 
851 	r128fb_wait(sc, 8);
852 
853 	bus_space_write_4(sc->sc_memt, sc->sc_regh,
854 	    R128_DP_GUI_MASTER_CNTL,
855 	    R128_GMC_BRUSH_SOLID_COLOR |
856 	    R128_GMC_SRC_DATATYPE_MONO_FG_BG |
857 	    R128_ROP3_S |
858 	    R128_DP_SRC_SOURCE_HOST_DATA |
859 	    R128_GMC_DST_CLIPPING |
860 	    sc->sc_master_cntl);
861 
862 	bus_space_write_4(sc->sc_memt, sc->sc_regh,
863 	    R128_DP_CNTL,
864 	    R128_DST_Y_TOP_TO_BOTTOM |
865 	    R128_DST_X_LEFT_TO_RIGHT);
866 
867 	bus_space_write_4(sc->sc_memt, sc->sc_regh,
868 	    R128_DP_SRC_FRGD_CLR, fg);
869 	bus_space_write_4(sc->sc_memt, sc->sc_regh,
870 	    R128_DP_SRC_BKGD_CLR, bg);
871 
872 	/*
873 	 * The Rage 128 doesn't have anything to skip pixels
874 	 * when colour expanding but all coordinates
875 	 * are signed so we just clip the leading bytes and
876 	 * trailing bits away
877 	 */
878 	bus_space_write_4(sc->sc_memt, sc->sc_regh,
879 	    R128_SC_RIGHT, x + wi - 1);
880 	bus_space_write_4(sc->sc_memt, sc->sc_regh,
881 	    R128_SC_LEFT, x);
882 
883 	/* needed? */
884 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_SRC_X_Y, 0);
885 
886 	offset = 32 - (font->stride << 3);
887 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DST_X_Y,
888 	    ((x - offset) << 16) | y);
889 	bus_space_write_4(sc->sc_memt, sc->sc_regh,
890 	    R128_DST_WIDTH_HEIGHT, (32 << 16) | he);
891 
892 	r128fb_wait(sc, he);
893 	switch (font->stride) {
894 		case 1: {
895 			uint8_t *data8 = data;
896 			uint32_t reg;
897 			for (i = 0; i < he; i++) {
898 				reg = *data8;
899 				bus_space_write_stream_4(sc->sc_memt,
900 				    sc->sc_regh, R128_HOST_DATA0, reg);
901 				data8++;
902 			}
903 			break;
904 		}
905 		case 2: {
906 			uint16_t *data16 = data;
907 			uint32_t reg;
908 			for (i = 0; i < he; i++) {
909 				reg = *data16;
910 				bus_space_write_stream_4(sc->sc_memt,
911 				    sc->sc_regh, R128_HOST_DATA0, reg);
912 				data16++;
913 			}
914 			break;
915 		}
916 	}
917 }
918 
919 static void
920 r128fb_putchar_aa(void *cookie, int row, int col, u_int c, long attr)
921 {
922 	struct rasops_info *ri = cookie;
923 	struct wsdisplay_font *font = PICK_FONT(ri, c);
924 	struct vcons_screen *scr = ri->ri_hw;
925 	struct r128fb_softc *sc = scr->scr_cookie;
926 	uint32_t bg, latch = 0, bg8, fg8, pixel;
927 	int i, x, y, wi, he, r, g, b, aval;
928 	int r1, g1, b1, r0, g0, b0, fgo, bgo;
929 	uint8_t *data8;
930 	int rv, cnt = 0;
931 
932 	if (sc->sc_mode != WSDISPLAYIO_MODE_EMUL)
933 		return;
934 
935 	if (!CHAR_IN_FONT(c, font))
936 		return;
937 
938 	wi = font->fontwidth;
939 	he = font->fontheight;
940 
941 	bg = ri->ri_devcmap[(attr >> 16) & 0xf];
942 	x = ri->ri_xorigin + col * wi;
943 	y = ri->ri_yorigin + row * he;
944 	if (c == 0x20) {
945 		r128fb_rectfill(sc, x, y, wi, he, bg);
946 		return;
947 	}
948 
949 	rv = glyphcache_try(&sc->sc_gc, c, x, y, attr);
950 	if (rv == GC_OK)
951 		return;
952 
953 	data8 = WSFONT_GLYPH(c, font);
954 
955 	r128fb_wait(sc, 5);
956 	bus_space_write_4(sc->sc_memt, sc->sc_regh,
957 	    R128_DP_GUI_MASTER_CNTL,
958 	    R128_GMC_BRUSH_SOLID_COLOR |
959 	    R128_GMC_SRC_DATATYPE_COLOR |
960 	    R128_ROP3_S |
961 	    R128_DP_SRC_SOURCE_HOST_DATA |
962 	    sc->sc_master_cntl);
963 
964 	bus_space_write_4(sc->sc_memt, sc->sc_regh,
965 	    R128_DP_CNTL,
966 	    R128_DST_Y_TOP_TO_BOTTOM |
967 	    R128_DST_X_LEFT_TO_RIGHT);
968 
969 	/* needed? */
970 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_SRC_X_Y, 0);
971 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DST_X_Y,
972 	    (x << 16) | y);
973 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DST_WIDTH_HEIGHT,
974 	    (wi << 16) | he);
975 
976 	/*
977 	 * we need the RGB colours here, so get offsets into rasops_cmap
978 	 */
979 	fgo = ((attr >> 24) & 0xf) * 3;
980 	bgo = ((attr >> 16) & 0xf) * 3;
981 
982 	r0 = rasops_cmap[bgo];
983 	r1 = rasops_cmap[fgo];
984 	g0 = rasops_cmap[bgo + 1];
985 	g1 = rasops_cmap[fgo + 1];
986 	b0 = rasops_cmap[bgo + 2];
987 	b1 = rasops_cmap[fgo + 2];
988 #define R3G3B2(r, g, b) ((r & 0xe0) | ((g >> 3) & 0x1c) | (b >> 6))
989 	bg8 = R3G3B2(r0, g0, b0);
990 	fg8 = R3G3B2(r1, g1, b1);
991 
992 	r128fb_wait(sc, 16);
993 
994 	for (i = 0; i < ri->ri_fontscale; i++) {
995 		aval = *data8;
996 		if (aval == 0) {
997 			pixel = bg8;
998 		} else if (aval == 255) {
999 			pixel = fg8;
1000 		} else {
1001 			r = aval * r1 + (255 - aval) * r0;
1002 			g = aval * g1 + (255 - aval) * g0;
1003 			b = aval * b1 + (255 - aval) * b0;
1004 			pixel = ((r & 0xe000) >> 8) |
1005 				((g & 0xe000) >> 11) |
1006 				((b & 0xc000) >> 14);
1007 		}
1008 		latch = (latch << 8) | pixel;
1009 		/* write in 32bit chunks */
1010 		if ((i & 3) == 3) {
1011 			bus_space_write_stream_4(sc->sc_memt, sc->sc_regh,
1012 			    R128_HOST_DATA0, latch);
1013 			/*
1014 			 * not strictly necessary, old data should be shifted
1015 			 * out
1016 			 */
1017 			latch = 0;
1018 			cnt++;
1019 			if (cnt > 15) {
1020 				r128fb_wait(sc, 16);
1021 				cnt = 0;
1022 			}
1023 		}
1024 		data8++;
1025 	}
1026 	/* if we have pixels left in latch write them out */
1027 	if ((i & 3) != 0) {
1028 		latch = latch << ((4 - (i & 3)) << 3);
1029 		bus_space_write_stream_4(sc->sc_memt, sc->sc_regh,
1030 				    R128_HOST_DATA0, latch);
1031 	}
1032 	if (rv == GC_ADD) {
1033 		glyphcache_add(&sc->sc_gc, c, x, y);
1034 	}
1035 }
1036 
1037 static void
1038 r128fb_copycols(void *cookie, int row, int srccol, int dstcol, int ncols)
1039 {
1040 	struct rasops_info *ri = cookie;
1041 	struct vcons_screen *scr = ri->ri_hw;
1042 	struct r128fb_softc *sc = scr->scr_cookie;
1043 	int32_t xs, xd, y, width, height;
1044 
1045 	if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
1046 		xs = ri->ri_xorigin + ri->ri_font->fontwidth * srccol;
1047 		xd = ri->ri_xorigin + ri->ri_font->fontwidth * dstcol;
1048 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
1049 		width = ri->ri_font->fontwidth * ncols;
1050 		height = ri->ri_font->fontheight;
1051 		r128fb_bitblt(sc, xs, y, xd, y, width, height, R128_ROP3_S);
1052 	}
1053 }
1054 
1055 static void
1056 r128fb_erasecols(void *cookie, int row, int startcol, int ncols, long fillattr)
1057 {
1058 	struct rasops_info *ri = cookie;
1059 	struct vcons_screen *scr = ri->ri_hw;
1060 	struct r128fb_softc *sc = scr->scr_cookie;
1061 	int32_t x, y, width, height, fg, bg, ul;
1062 
1063 	if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
1064 		x = ri->ri_xorigin + ri->ri_font->fontwidth * startcol;
1065 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
1066 		width = ri->ri_font->fontwidth * ncols;
1067 		height = ri->ri_font->fontheight;
1068 		rasops_unpack_attr(fillattr, &fg, &bg, &ul);
1069 
1070 		r128fb_rectfill(sc, x, y, width, height, ri->ri_devcmap[bg]);
1071 	}
1072 }
1073 
1074 static void
1075 r128fb_copyrows(void *cookie, int srcrow, int dstrow, int nrows)
1076 {
1077 	struct rasops_info *ri = cookie;
1078 	struct vcons_screen *scr = ri->ri_hw;
1079 	struct r128fb_softc *sc = scr->scr_cookie;
1080 	int32_t x, ys, yd, width, height;
1081 
1082 	if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
1083 		x = ri->ri_xorigin;
1084 		ys = ri->ri_yorigin + ri->ri_font->fontheight * srcrow;
1085 		yd = ri->ri_yorigin + ri->ri_font->fontheight * dstrow;
1086 		width = ri->ri_emuwidth;
1087 		height = ri->ri_font->fontheight * nrows;
1088 		r128fb_bitblt(sc, x, ys, x, yd, width, height, R128_ROP3_S);
1089 	}
1090 }
1091 
1092 static void
1093 r128fb_eraserows(void *cookie, int row, int nrows, long fillattr)
1094 {
1095 	struct rasops_info *ri = cookie;
1096 	struct vcons_screen *scr = ri->ri_hw;
1097 	struct r128fb_softc *sc = scr->scr_cookie;
1098 	int32_t x, y, width, height, fg, bg, ul;
1099 
1100 	if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
1101 		x = ri->ri_xorigin;
1102 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
1103 		width = ri->ri_emuwidth;
1104 		height = ri->ri_font->fontheight * nrows;
1105 		rasops_unpack_attr(fillattr, &fg, &bg, &ul);
1106 
1107 		r128fb_rectfill(sc, x, y, width, height, ri->ri_devcmap[bg]);
1108 	}
1109 }
1110 
1111 static void
1112 r128fb_set_backlight(struct r128fb_softc *sc, int level)
1113 {
1114 	uint32_t reg;
1115 
1116 	/*
1117 	 * should we do nothing when backlight is off, should we just store the
1118 	 * level and use it when turning back on or should we just flip sc_bl_on
1119 	 * and turn the backlight on?
1120 	 * For now turn it on so a crashed screensaver can't get the user stuck
1121 	 * with a dark screen as long as hotkeys work
1122 	 */
1123 	if (level > 255) level = 255;
1124 	if (level < 0) level = 0;
1125 	if (level == sc->sc_bl_level)
1126 		return;
1127 	sc->sc_bl_level = level;
1128 	if (sc->sc_bl_on == 0)
1129 		sc->sc_bl_on = 1;
1130 	level = 255 - level;
1131 	reg = bus_space_read_4(sc->sc_memt, sc->sc_regh, R128_LVDS_GEN_CNTL);
1132 	reg &= ~R128_LEVEL_MASK;
1133 	reg |= (level << R128_LEVEL_SHIFT);
1134 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_LVDS_GEN_CNTL, reg);
1135 	DPRINTF("backlight level: %d reg %08x\n", level, reg);
1136 }
1137 
1138 static void
1139 r128fb_switch_backlight(struct r128fb_softc *sc, int on)
1140 {
1141 	uint32_t reg;
1142 	int level;
1143 
1144 	if (on == sc->sc_bl_on)
1145 		return;
1146 	sc->sc_bl_on = on;
1147 	reg = bus_space_read_4(sc->sc_memt, sc->sc_regh, R128_LVDS_GEN_CNTL);
1148 	reg &= ~R128_LEVEL_MASK;
1149 	level = on ? 255 - sc->sc_bl_level : 255;
1150 	reg |= level << R128_LEVEL_SHIFT;
1151 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_LVDS_GEN_CNTL, reg);
1152 	DPRINTF("backlight state: %d reg %08x\n", on, reg);
1153 }
1154 
1155 
1156 static void
1157 r128fb_brightness_up(device_t dev)
1158 {
1159 	struct r128fb_softc *sc = device_private(dev);
1160 
1161 	r128fb_set_backlight(sc, sc->sc_bl_level + 8);
1162 }
1163 
1164 static void
1165 r128fb_brightness_down(device_t dev)
1166 {
1167 	struct r128fb_softc *sc = device_private(dev);
1168 
1169 	r128fb_set_backlight(sc, sc->sc_bl_level - 8);
1170 }
1171