xref: /netbsd-src/sys/dev/pci/r128fb.c (revision e89934bbf778a6d6d6894877c4da59d0c7835b0f)
1 /*	$NetBSD: r128fb.c,v 1.40 2017/01/20 12:25:07 maya 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.40 2017/01/20 12:25:07 maya 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 = FALSE;
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 		} else
315 			(*ri->ri_ops.allocattr)(ri, 0, 0, 0, &defattr);
316 		glyphcache_init(&sc->sc_gc, sc->sc_height + 5,
317 				(0x800000 / sc->sc_stride) - sc->sc_height - 5,
318 				sc->sc_width,
319 				ri->ri_font->fontwidth,
320 				ri->ri_font->fontheight,
321 				defattr);
322 	}
323 
324 	j = 0;
325 	rasops_get_cmap(ri, cmap, sizeof(cmap));
326 	for (i = 0; i < 256; i++) {
327 		sc->sc_cmap_red[i] = cmap[j];
328 		sc->sc_cmap_green[i] = cmap[j + 1];
329 		sc->sc_cmap_blue[i] = cmap[j + 2];
330 		r128fb_putpalreg(sc, i, cmap[j], cmap[j + 1], cmap[j + 2]);
331 		j += 3;
332 	}
333 
334 	/* no suspend/resume support yet */
335 	if (!pmf_device_register(sc->sc_dev, NULL, NULL))
336 		aprint_error_dev(sc->sc_dev,
337 		    "couldn't establish power handler\n");
338 
339 	reg = bus_space_read_4(sc->sc_memt, sc->sc_regh, R128_LVDS_GEN_CNTL);
340 	DPRINTF("R128_LVDS_GEN_CNTL: %08x\n", reg);
341 	if (reg & R128_LVDS_ON) {
342 		sc->sc_have_backlight = 1;
343 		sc->sc_bl_on = 1;
344 		sc->sc_bl_level = 255 -
345 		    ((reg & R128_LEVEL_MASK) >> R128_LEVEL_SHIFT);
346 		pmf_event_register(sc->sc_dev, PMFE_DISPLAY_BRIGHTNESS_UP,
347 		    r128fb_brightness_up, TRUE);
348 		pmf_event_register(sc->sc_dev, PMFE_DISPLAY_BRIGHTNESS_DOWN,
349 		    r128fb_brightness_down, TRUE);
350 		aprint_verbose("%s: LVDS output is active, enabling backlight"
351 			       " control\n", device_xname(self));
352 	} else
353 		sc->sc_have_backlight = 0;
354 
355 	aa.console = is_console;
356 	aa.scrdata = &sc->sc_screenlist;
357 	aa.accessops = &r128fb_accessops;
358 	aa.accesscookie = &sc->vd;
359 
360 	config_found(sc->sc_dev, &aa, wsemuldisplaydevprint);
361 }
362 
363 static int
364 r128fb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
365 	struct lwp *l)
366 {
367 	struct vcons_data *vd = v;
368 	struct r128fb_softc *sc = vd->cookie;
369 	struct wsdisplay_fbinfo *wdf;
370 	struct vcons_screen *ms = vd->active;
371 	struct wsdisplay_param  *param;
372 
373 	switch (cmd) {
374 	case WSDISPLAYIO_GTYPE:
375 		*(u_int *)data = WSDISPLAY_TYPE_PCIMISC;
376 		return 0;
377 
378 	/* PCI config read/write passthrough. */
379 	case PCI_IOC_CFGREAD:
380 	case PCI_IOC_CFGWRITE:
381 		return pci_devioctl(sc->sc_pc, sc->sc_pcitag,
382 		    cmd, data, flag, l);
383 
384 	case WSDISPLAYIO_GET_BUSID:
385 		return wsdisplayio_busid_pci(sc->sc_dev, sc->sc_pc,
386 		    sc->sc_pcitag, data);
387 
388 	case WSDISPLAYIO_GINFO:
389 		if (ms == NULL)
390 			return ENODEV;
391 		wdf = (void *)data;
392 		wdf->height = ms->scr_ri.ri_height;
393 		wdf->width = ms->scr_ri.ri_width;
394 		wdf->depth = ms->scr_ri.ri_depth;
395 		wdf->cmsize = 256;
396 		return 0;
397 
398 	case WSDISPLAYIO_GETCMAP:
399 		return r128fb_getcmap(sc,
400 		    (struct wsdisplay_cmap *)data);
401 
402 	case WSDISPLAYIO_PUTCMAP:
403 		return r128fb_putcmap(sc,
404 		    (struct wsdisplay_cmap *)data);
405 
406 	case WSDISPLAYIO_LINEBYTES:
407 		*(u_int *)data = sc->sc_stride;
408 		return 0;
409 
410 	case WSDISPLAYIO_SMODE: {
411 		int new_mode = *(int*)data;
412 		if (new_mode != sc->sc_mode) {
413 			sc->sc_mode = new_mode;
414 			if(new_mode == WSDISPLAYIO_MODE_EMUL) {
415 				r128fb_init(sc);
416 				r128fb_restore_palette(sc);
417 				glyphcache_wipe(&sc->sc_gc);
418 				r128fb_rectfill(sc, 0, 0, sc->sc_width,
419 				    sc->sc_height, ms->scr_ri.ri_devcmap[
420 				    (ms->scr_defattr >> 16) & 0xff]);
421 				vcons_redraw_screen(ms);
422 			}
423 		}
424 		}
425 		return 0;
426 
427 	case WSDISPLAYIO_GETPARAM:
428 		param = (struct wsdisplay_param *)data;
429 		if (sc->sc_have_backlight == 0)
430 			return EPASSTHROUGH;
431 		switch (param->param) {
432 		case WSDISPLAYIO_PARAM_BRIGHTNESS:
433 			param->min = 0;
434 			param->max = 255;
435 			param->curval = sc->sc_bl_level;
436 			return 0;
437 		case WSDISPLAYIO_PARAM_BACKLIGHT:
438 			param->min = 0;
439 			param->max = 1;
440 			param->curval = sc->sc_bl_on;
441 			return 0;
442 		}
443 		return EPASSTHROUGH;
444 
445 	case WSDISPLAYIO_SETPARAM:
446 		param = (struct wsdisplay_param *)data;
447 		if (sc->sc_have_backlight == 0)
448 			return EPASSTHROUGH;
449 		switch (param->param) {
450 		case WSDISPLAYIO_PARAM_BRIGHTNESS:
451 			r128fb_set_backlight(sc, param->curval);
452 			return 0;
453 		case WSDISPLAYIO_PARAM_BACKLIGHT:
454 			r128fb_switch_backlight(sc,  param->curval);
455 			return 0;
456 		}
457 		return EPASSTHROUGH;
458 
459 	case WSDISPLAYIO_GET_EDID: {
460 		struct wsdisplayio_edid_info *d = data;
461 		return wsdisplayio_get_edid(sc->sc_dev, d);
462 	}
463 
464 	case WSDISPLAYIO_GET_FBINFO: {
465 		struct wsdisplayio_fbinfo *fbi = data;
466 		return wsdisplayio_get_fbinfo(&ms->scr_ri, fbi);
467 	}
468 	}
469 	return EPASSTHROUGH;
470 }
471 
472 static paddr_t
473 r128fb_mmap(void *v, void *vs, off_t offset, int prot)
474 {
475 	struct vcons_data *vd = v;
476 	struct r128fb_softc *sc = vd->cookie;
477 	paddr_t pa;
478 
479 	/* 'regular' framebuffer mmap()ing */
480 	if (offset < sc->sc_fbsize) {
481 		pa = bus_space_mmap(sc->sc_memt, sc->sc_fb + offset, 0, prot,
482 		    BUS_SPACE_MAP_LINEAR);
483 		return pa;
484 	}
485 
486 	/*
487 	 * restrict all other mappings to processes with superuser privileges
488 	 * or the kernel itself
489 	 */
490 	if (kauth_authorize_machdep(kauth_cred_get(), KAUTH_MACHDEP_UNMANAGEDMEM,
491 	    NULL, NULL, NULL, NULL) != 0) {
492 		aprint_normal("%s: mmap() rejected.\n",
493 		    device_xname(sc->sc_dev));
494 		return -1;
495 	}
496 
497 	if ((offset >= sc->sc_fb) && (offset < (sc->sc_fb + sc->sc_fbsize))) {
498 		pa = bus_space_mmap(sc->sc_memt, offset, 0, prot,
499 		    BUS_SPACE_MAP_LINEAR);
500 		return pa;
501 	}
502 
503 	if ((offset >= sc->sc_reg) &&
504 	    (offset < (sc->sc_reg + sc->sc_regsize))) {
505 		pa = bus_space_mmap(sc->sc_memt, offset, 0, prot,
506 		    BUS_SPACE_MAP_LINEAR);
507 		return pa;
508 	}
509 
510 #ifdef PCI_MAGIC_IO_RANGE
511 	/* allow mapping of IO space */
512 	if ((offset >= PCI_MAGIC_IO_RANGE) &&
513 	    (offset < PCI_MAGIC_IO_RANGE + 0x10000)) {
514 		pa = bus_space_mmap(sc->sc_iot, offset - PCI_MAGIC_IO_RANGE,
515 		    0, prot, BUS_SPACE_MAP_LINEAR);
516 		return pa;
517 	}
518 #endif
519 
520 #ifdef OFB_ALLOW_OTHERS
521 	if (offset >= 0x80000000) {
522 		pa = bus_space_mmap(sc->sc_memt, offset, 0, prot,
523 		    BUS_SPACE_MAP_LINEAR);
524 		return pa;
525 	}
526 #endif
527 	return -1;
528 }
529 
530 static void
531 r128fb_init_screen(void *cookie, struct vcons_screen *scr,
532     int existing, long *defattr)
533 {
534 	struct r128fb_softc *sc = cookie;
535 	struct rasops_info *ri = &scr->scr_ri;
536 
537 	ri->ri_depth = sc->sc_depth;
538 	ri->ri_width = sc->sc_width;
539 	ri->ri_height = sc->sc_height;
540 	ri->ri_stride = sc->sc_stride;
541 	ri->ri_flg = RI_CENTER;
542 	if (sc->sc_depth == 8)
543 		ri->ri_flg |= RI_8BIT_IS_RGB | RI_ENABLE_ALPHA;
544 
545 	rasops_init(ri, 0, 0);
546 	ri->ri_caps = WSSCREEN_WSCOLORS;
547 #ifdef VCONS_DRAW_INTR
548 	scr->scr_flags |= VCONS_DONT_READ;
549 #endif
550 
551 	rasops_reconfig(ri, sc->sc_height / ri->ri_font->fontheight,
552 		    sc->sc_width / ri->ri_font->fontwidth);
553 
554 	ri->ri_hw = scr;
555 	ri->ri_ops.copyrows = r128fb_copyrows;
556 	ri->ri_ops.copycols = r128fb_copycols;
557 	ri->ri_ops.eraserows = r128fb_eraserows;
558 	ri->ri_ops.erasecols = r128fb_erasecols;
559 	ri->ri_ops.cursor = r128fb_cursor;
560 	if (FONT_IS_ALPHA(ri->ri_font)) {
561 		ri->ri_ops.putchar = r128fb_putchar_aa;
562 	} else
563 		ri->ri_ops.putchar = r128fb_putchar;
564 }
565 
566 static int
567 r128fb_putcmap(struct r128fb_softc *sc, struct wsdisplay_cmap *cm)
568 {
569 	u_char *r, *g, *b;
570 	u_int index = cm->index;
571 	u_int count = cm->count;
572 	int i, error;
573 	u_char rbuf[256], gbuf[256], bbuf[256];
574 
575 #ifdef R128FB_DEBUG
576 	aprint_debug("putcmap: %d %d\n",index, count);
577 #endif
578 	if (cm->index >= 256 || cm->count > 256 ||
579 	    (cm->index + cm->count) > 256)
580 		return EINVAL;
581 	error = copyin(cm->red, &rbuf[index], count);
582 	if (error)
583 		return error;
584 	error = copyin(cm->green, &gbuf[index], count);
585 	if (error)
586 		return error;
587 	error = copyin(cm->blue, &bbuf[index], count);
588 	if (error)
589 		return error;
590 
591 	memcpy(&sc->sc_cmap_red[index], &rbuf[index], count);
592 	memcpy(&sc->sc_cmap_green[index], &gbuf[index], count);
593 	memcpy(&sc->sc_cmap_blue[index], &bbuf[index], count);
594 
595 	r = &sc->sc_cmap_red[index];
596 	g = &sc->sc_cmap_green[index];
597 	b = &sc->sc_cmap_blue[index];
598 
599 	for (i = 0; i < count; i++) {
600 		r128fb_putpalreg(sc, index, *r, *g, *b);
601 		index++;
602 		r++, g++, b++;
603 	}
604 	return 0;
605 }
606 
607 static int
608 r128fb_getcmap(struct r128fb_softc *sc, struct wsdisplay_cmap *cm)
609 {
610 	u_int index = cm->index;
611 	u_int count = cm->count;
612 	int error;
613 
614 	if (index >= 255 || count > 256 || index + count > 256)
615 		return EINVAL;
616 
617 	error = copyout(&sc->sc_cmap_red[index],   cm->red,   count);
618 	if (error)
619 		return error;
620 	error = copyout(&sc->sc_cmap_green[index], cm->green, count);
621 	if (error)
622 		return error;
623 	error = copyout(&sc->sc_cmap_blue[index],  cm->blue,  count);
624 	if (error)
625 		return error;
626 
627 	return 0;
628 }
629 
630 static void
631 r128fb_restore_palette(struct r128fb_softc *sc)
632 {
633 	int i;
634 
635 	for (i = 0; i < (1 << sc->sc_depth); i++) {
636 		r128fb_putpalreg(sc, i, sc->sc_cmap_red[i],
637 		    sc->sc_cmap_green[i], sc->sc_cmap_blue[i]);
638 	}
639 }
640 
641 static int
642 r128fb_putpalreg(struct r128fb_softc *sc, uint8_t idx, uint8_t r, uint8_t g,
643     uint8_t b)
644 {
645 	uint32_t reg;
646 
647 	/* whack the DAC */
648 	reg = (r << 16) | (g << 8) | b;
649 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_PALETTE_INDEX, idx);
650 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_PALETTE_DATA, reg);
651 	return 0;
652 }
653 
654 static void
655 r128fb_init(struct r128fb_softc *sc)
656 {
657 	uint32_t datatype, d, reg;
658 
659 	r128fb_flush_engine(sc);
660 
661 	r128fb_wait(sc, 9);
662 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_CRTC_OFFSET, 0);
663 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DEFAULT_OFFSET, 0);
664 	/* pitch is in units of 8 pixels */
665 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DEFAULT_PITCH,
666 	    sc->sc_width >> 3);
667 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_AUX_SC_CNTL, 0);
668 	bus_space_write_4(sc->sc_memt, sc->sc_regh,
669 	    R128_DEFAULT_SC_BOTTOM_RIGHT,
670 	    R128_DEFAULT_SC_RIGHT_MAX | R128_DEFAULT_SC_BOTTOM_MAX);
671 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_SC_TOP_LEFT, 0);
672 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_SC_BOTTOM_RIGHT,
673 	    R128_DEFAULT_SC_RIGHT_MAX | R128_DEFAULT_SC_BOTTOM_MAX);
674 	bus_space_write_4(sc->sc_memt, sc->sc_regh,
675 	    R128_DEFAULT_SC_BOTTOM_RIGHT,
676 	    R128_DEFAULT_SC_RIGHT_MAX | R128_DEFAULT_SC_BOTTOM_MAX);
677 
678 #if 0
679 #if BYTE_ORDER == BIG_ENDIAN
680 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DP_DATATYPE,
681 	    R128_HOST_BIG_ENDIAN_EN);
682 #else
683 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DP_DATATYPE, 0);
684 #endif
685 #endif
686 	r128fb_wait(sc, 7);
687 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_SRC_PITCH,
688 	    sc->sc_width >> 3);
689 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DST_PITCH,
690 	    sc->sc_width >> 3);
691 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_SRC_OFFSET, 0);
692 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DST_OFFSET, 0);
693 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DP_WRITE_MASK,
694 	    0xffffffff);
695 
696 	switch (sc->sc_depth) {
697 		case 8:
698 			datatype = R128_GMC_DST_8BPP_CI;
699 			d = R128_CRTC_COLOR_8BIT;
700 			break;
701 		case 15:
702 			datatype = R128_GMC_DST_15BPP;
703 			d = R128_CRTC_COLOR_15BIT;
704 			break;
705 		case 16:
706 			datatype = R128_GMC_DST_16BPP;
707 			d = R128_CRTC_COLOR_16BIT;
708 			break;
709 		case 24:
710 			datatype = R128_GMC_DST_24BPP;
711 			d = R128_CRTC_COLOR_24BIT;
712 			break;
713 		case 32:
714 			datatype = R128_GMC_DST_32BPP;
715 			d = R128_CRTC_COLOR_32BIT;
716 			break;
717 		default:
718 			aprint_error("%s: unsupported depth %d\n",
719 			    device_xname(sc->sc_dev), sc->sc_depth);
720 			return;
721 	}
722 	sc->sc_master_cntl = R128_GMC_CLR_CMP_CNTL_DIS |
723 	    R128_GMC_AUX_CLIP_DIS | datatype;
724 	reg = bus_space_read_4(sc->sc_memt, sc->sc_regh, R128_CRTC_GEN_CNTL);
725 	DPRINTF("depth: %d\n", reg & R128_CRTC_PIX_WIDTH);
726 	reg &= ~R128_CRTC_PIX_WIDTH;
727 	reg |= d;
728 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_CRTC_GEN_CNTL, reg);
729 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_CRTC_PITCH, sc->sc_width >> 3);
730 	r128fb_flush_engine(sc);
731 }
732 
733 static void
734 r128fb_rectfill(struct r128fb_softc *sc, int x, int y, int wi, int he,
735      uint32_t colour)
736 {
737 
738 	r128fb_wait(sc, 5);
739 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DP_GUI_MASTER_CNTL,
740 	    R128_GMC_BRUSH_SOLID_COLOR |
741 	    R128_GMC_SRC_DATATYPE_COLOR |
742 	    R128_ROP3_P |
743 	    sc->sc_master_cntl);
744 
745 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DP_BRUSH_FRGD_CLR,
746 	    colour);
747 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DP_CNTL,
748 	    R128_DST_X_LEFT_TO_RIGHT | R128_DST_Y_TOP_TO_BOTTOM);
749 	/* now feed it coordinates */
750 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DST_X_Y,
751 	    (x << 16) | y);
752 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DST_WIDTH_HEIGHT,
753 	    (wi << 16) | he);
754 }
755 
756 static void
757 r128fb_bitblt(void *cookie, int xs, int ys, int xd, int yd,
758     int wi, int he, int rop)
759 {
760 	struct r128fb_softc *sc = cookie;
761 	uint32_t dp_cntl = 0;
762 
763 	r128fb_wait(sc, 5);
764 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DP_GUI_MASTER_CNTL,
765 	    R128_GMC_BRUSH_SOLID_COLOR |
766 	    R128_GMC_SRC_DATATYPE_COLOR |
767 	    rop |
768 	    R128_DP_SRC_SOURCE_MEMORY |
769 	    sc->sc_master_cntl);
770 
771 	if (yd <= ys) {
772 		dp_cntl = R128_DST_Y_TOP_TO_BOTTOM;
773 	} else {
774 		ys += he - 1;
775 		yd += he - 1;
776 	}
777 	if (xd <= xs) {
778 		dp_cntl |= R128_DST_X_LEFT_TO_RIGHT;
779 	} else {
780 		xs += wi - 1;
781 		xd += wi - 1;
782 	}
783 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DP_CNTL, dp_cntl);
784 
785 	/* now feed it coordinates */
786 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_SRC_X_Y,
787 	    (xs << 16) | ys);
788 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DST_X_Y,
789 	    (xd << 16) | yd);
790 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DST_WIDTH_HEIGHT,
791 	    (wi << 16) | he);
792 }
793 
794 static void
795 r128fb_cursor(void *cookie, int on, int row, int col)
796 {
797 	struct rasops_info *ri = cookie;
798 	struct vcons_screen *scr = ri->ri_hw;
799 	struct r128fb_softc *sc = scr->scr_cookie;
800 	int x, y, wi, he;
801 
802 	wi = ri->ri_font->fontwidth;
803 	he = ri->ri_font->fontheight;
804 
805 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
806 		x = ri->ri_ccol * wi + ri->ri_xorigin;
807 		y = ri->ri_crow * he + ri->ri_yorigin;
808 		if (ri->ri_flg & RI_CURSOR) {
809 			r128fb_bitblt(sc, x, y, x, y, wi, he, R128_ROP3_Dn);
810 			ri->ri_flg &= ~RI_CURSOR;
811 		}
812 		ri->ri_crow = row;
813 		ri->ri_ccol = col;
814 		if (on) {
815 			x = ri->ri_ccol * wi + ri->ri_xorigin;
816 			y = ri->ri_crow * he + ri->ri_yorigin;
817 			r128fb_bitblt(sc, x, y, x, y, wi, he, R128_ROP3_Dn);
818 			ri->ri_flg |= RI_CURSOR;
819 		}
820 	} else {
821 		scr->scr_ri.ri_crow = row;
822 		scr->scr_ri.ri_ccol = col;
823 		scr->scr_ri.ri_flg &= ~RI_CURSOR;
824 	}
825 
826 }
827 
828 static void
829 r128fb_putchar(void *cookie, int row, int col, u_int c, long attr)
830 {
831 	struct rasops_info *ri = cookie;
832 	struct wsdisplay_font *font = PICK_FONT(ri, c);
833 	struct vcons_screen *scr = ri->ri_hw;
834 	struct r128fb_softc *sc = scr->scr_cookie;
835 	void *data;
836 	uint32_t fg, bg;
837 	int i, x, y, wi, he, offset;
838 
839 	if (sc->sc_mode != WSDISPLAYIO_MODE_EMUL)
840 		return;
841 
842 	if (!CHAR_IN_FONT(c, font))
843 		return;
844 
845 	wi = font->fontwidth;
846 	he = font->fontheight;
847 
848 	bg = ri->ri_devcmap[(attr >> 16) & 0xf];
849 	fg = ri->ri_devcmap[(attr >> 24) & 0xf];
850 	x = ri->ri_xorigin + col * wi;
851 	y = ri->ri_yorigin + row * he;
852 
853 	if (c == 0x20) {
854 		r128fb_rectfill(sc, x, y, wi, he, bg);
855 		return;
856 	}
857 
858 	data = WSFONT_GLYPH(c, font);
859 
860 	r128fb_wait(sc, 8);
861 
862 	bus_space_write_4(sc->sc_memt, sc->sc_regh,
863 	    R128_DP_GUI_MASTER_CNTL,
864 	    R128_GMC_BRUSH_SOLID_COLOR |
865 	    R128_GMC_SRC_DATATYPE_MONO_FG_BG |
866 	    R128_ROP3_S |
867 	    R128_DP_SRC_SOURCE_HOST_DATA |
868 	    R128_GMC_DST_CLIPPING |
869 	    sc->sc_master_cntl);
870 
871 	bus_space_write_4(sc->sc_memt, sc->sc_regh,
872 	    R128_DP_CNTL,
873 	    R128_DST_Y_TOP_TO_BOTTOM |
874 	    R128_DST_X_LEFT_TO_RIGHT);
875 
876 	bus_space_write_4(sc->sc_memt, sc->sc_regh,
877 	    R128_DP_SRC_FRGD_CLR, fg);
878 	bus_space_write_4(sc->sc_memt, sc->sc_regh,
879 	    R128_DP_SRC_BKGD_CLR, bg);
880 
881 	/*
882 	 * The Rage 128 doesn't have anything to skip pixels
883 	 * when colour expanding but all coordinates
884 	 * are signed so we just clip the leading bytes and
885 	 * trailing bits away
886 	 */
887 	bus_space_write_4(sc->sc_memt, sc->sc_regh,
888 	    R128_SC_RIGHT, x + wi - 1);
889 	bus_space_write_4(sc->sc_memt, sc->sc_regh,
890 	    R128_SC_LEFT, x);
891 
892 	/* needed? */
893 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_SRC_X_Y, 0);
894 
895 	offset = 32 - (font->stride << 3);
896 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DST_X_Y,
897 	    ((x - offset) << 16) | y);
898 	bus_space_write_4(sc->sc_memt, sc->sc_regh,
899 	    R128_DST_WIDTH_HEIGHT, (32 << 16) | he);
900 
901 	r128fb_wait(sc, he);
902 	switch (font->stride) {
903 		case 1: {
904 			uint8_t *data8 = data;
905 			uint32_t reg;
906 			for (i = 0; i < he; i++) {
907 				reg = *data8;
908 				bus_space_write_stream_4(sc->sc_memt,
909 				    sc->sc_regh, R128_HOST_DATA0, reg);
910 				data8++;
911 			}
912 			break;
913 		}
914 		case 2: {
915 			uint16_t *data16 = data;
916 			uint32_t reg;
917 			for (i = 0; i < he; i++) {
918 				reg = *data16;
919 				bus_space_write_stream_4(sc->sc_memt,
920 				    sc->sc_regh, R128_HOST_DATA0, reg);
921 				data16++;
922 			}
923 			break;
924 		}
925 	}
926 }
927 
928 static void
929 r128fb_putchar_aa(void *cookie, int row, int col, u_int c, long attr)
930 {
931 	struct rasops_info *ri = cookie;
932 	struct wsdisplay_font *font = PICK_FONT(ri, c);
933 	struct vcons_screen *scr = ri->ri_hw;
934 	struct r128fb_softc *sc = scr->scr_cookie;
935 	uint32_t bg, latch = 0, bg8, fg8, pixel;
936 	int i, x, y, wi, he, r, g, b, aval;
937 	int r1, g1, b1, r0, g0, b0, fgo, bgo;
938 	uint8_t *data8;
939 	int rv, cnt = 0;
940 
941 	if (sc->sc_mode != WSDISPLAYIO_MODE_EMUL)
942 		return;
943 
944 	if (!CHAR_IN_FONT(c, font))
945 		return;
946 
947 	wi = font->fontwidth;
948 	he = font->fontheight;
949 
950 	bg = ri->ri_devcmap[(attr >> 16) & 0xf];
951 	x = ri->ri_xorigin + col * wi;
952 	y = ri->ri_yorigin + row * he;
953 	if (c == 0x20) {
954 		r128fb_rectfill(sc, x, y, wi, he, bg);
955 		return;
956 	}
957 
958 	rv = glyphcache_try(&sc->sc_gc, c, x, y, attr);
959 	if (rv == GC_OK)
960 		return;
961 
962 	data8 = WSFONT_GLYPH(c, font);
963 
964 	r128fb_wait(sc, 5);
965 	bus_space_write_4(sc->sc_memt, sc->sc_regh,
966 	    R128_DP_GUI_MASTER_CNTL,
967 	    R128_GMC_BRUSH_SOLID_COLOR |
968 	    R128_GMC_SRC_DATATYPE_COLOR |
969 	    R128_ROP3_S |
970 	    R128_DP_SRC_SOURCE_HOST_DATA |
971 	    sc->sc_master_cntl);
972 
973 	bus_space_write_4(sc->sc_memt, sc->sc_regh,
974 	    R128_DP_CNTL,
975 	    R128_DST_Y_TOP_TO_BOTTOM |
976 	    R128_DST_X_LEFT_TO_RIGHT);
977 
978 	/* needed? */
979 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_SRC_X_Y, 0);
980 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DST_X_Y,
981 	    (x << 16) | y);
982 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DST_WIDTH_HEIGHT,
983 	    (wi << 16) | he);
984 
985 	/*
986 	 * we need the RGB colours here, so get offsets into rasops_cmap
987 	 */
988 	fgo = ((attr >> 24) & 0xf) * 3;
989 	bgo = ((attr >> 16) & 0xf) * 3;
990 
991 	r0 = rasops_cmap[bgo];
992 	r1 = rasops_cmap[fgo];
993 	g0 = rasops_cmap[bgo + 1];
994 	g1 = rasops_cmap[fgo + 1];
995 	b0 = rasops_cmap[bgo + 2];
996 	b1 = rasops_cmap[fgo + 2];
997 #define R3G3B2(r, g, b) ((r & 0xe0) | ((g >> 3) & 0x1c) | (b >> 6))
998 	bg8 = R3G3B2(r0, g0, b0);
999 	fg8 = R3G3B2(r1, g1, b1);
1000 
1001 	r128fb_wait(sc, 16);
1002 
1003 	for (i = 0; i < ri->ri_fontscale; i++) {
1004 		aval = *data8;
1005 		if (aval == 0) {
1006 			pixel = bg8;
1007 		} else if (aval == 255) {
1008 			pixel = fg8;
1009 		} else {
1010 			r = aval * r1 + (255 - aval) * r0;
1011 			g = aval * g1 + (255 - aval) * g0;
1012 			b = aval * b1 + (255 - aval) * b0;
1013 			pixel = ((r & 0xe000) >> 8) |
1014 				((g & 0xe000) >> 11) |
1015 				((b & 0xc000) >> 14);
1016 		}
1017 		latch = (latch << 8) | pixel;
1018 		/* write in 32bit chunks */
1019 		if ((i & 3) == 3) {
1020 			bus_space_write_stream_4(sc->sc_memt, sc->sc_regh,
1021 			    R128_HOST_DATA0, latch);
1022 			/*
1023 			 * not strictly necessary, old data should be shifted
1024 			 * out
1025 			 */
1026 			latch = 0;
1027 			cnt++;
1028 			if (cnt > 15) {
1029 				r128fb_wait(sc, 16);
1030 				cnt = 0;
1031 			}
1032 		}
1033 		data8++;
1034 	}
1035 	/* if we have pixels left in latch write them out */
1036 	if ((i & 3) != 0) {
1037 		latch = latch << ((4 - (i & 3)) << 3);
1038 		bus_space_write_stream_4(sc->sc_memt, sc->sc_regh,
1039 				    R128_HOST_DATA0, latch);
1040 	}
1041 	if (rv == GC_ADD) {
1042 		glyphcache_add(&sc->sc_gc, c, x, y);
1043 	}
1044 }
1045 
1046 static void
1047 r128fb_copycols(void *cookie, int row, int srccol, int dstcol, int ncols)
1048 {
1049 	struct rasops_info *ri = cookie;
1050 	struct vcons_screen *scr = ri->ri_hw;
1051 	struct r128fb_softc *sc = scr->scr_cookie;
1052 	int32_t xs, xd, y, width, height;
1053 
1054 	if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
1055 		xs = ri->ri_xorigin + ri->ri_font->fontwidth * srccol;
1056 		xd = ri->ri_xorigin + ri->ri_font->fontwidth * dstcol;
1057 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
1058 		width = ri->ri_font->fontwidth * ncols;
1059 		height = ri->ri_font->fontheight;
1060 		r128fb_bitblt(sc, xs, y, xd, y, width, height, R128_ROP3_S);
1061 	}
1062 }
1063 
1064 static void
1065 r128fb_erasecols(void *cookie, int row, int startcol, int ncols, long fillattr)
1066 {
1067 	struct rasops_info *ri = cookie;
1068 	struct vcons_screen *scr = ri->ri_hw;
1069 	struct r128fb_softc *sc = scr->scr_cookie;
1070 	int32_t x, y, width, height, fg, bg, ul;
1071 
1072 	if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
1073 		x = ri->ri_xorigin + ri->ri_font->fontwidth * startcol;
1074 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
1075 		width = ri->ri_font->fontwidth * ncols;
1076 		height = ri->ri_font->fontheight;
1077 		rasops_unpack_attr(fillattr, &fg, &bg, &ul);
1078 
1079 		r128fb_rectfill(sc, x, y, width, height, ri->ri_devcmap[bg]);
1080 	}
1081 }
1082 
1083 static void
1084 r128fb_copyrows(void *cookie, int srcrow, int dstrow, int nrows)
1085 {
1086 	struct rasops_info *ri = cookie;
1087 	struct vcons_screen *scr = ri->ri_hw;
1088 	struct r128fb_softc *sc = scr->scr_cookie;
1089 	int32_t x, ys, yd, width, height;
1090 
1091 	if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
1092 		x = ri->ri_xorigin;
1093 		ys = ri->ri_yorigin + ri->ri_font->fontheight * srcrow;
1094 		yd = ri->ri_yorigin + ri->ri_font->fontheight * dstrow;
1095 		width = ri->ri_emuwidth;
1096 		height = ri->ri_font->fontheight * nrows;
1097 		r128fb_bitblt(sc, x, ys, x, yd, width, height, R128_ROP3_S);
1098 	}
1099 }
1100 
1101 static void
1102 r128fb_eraserows(void *cookie, int row, int nrows, long fillattr)
1103 {
1104 	struct rasops_info *ri = cookie;
1105 	struct vcons_screen *scr = ri->ri_hw;
1106 	struct r128fb_softc *sc = scr->scr_cookie;
1107 	int32_t x, y, width, height, fg, bg, ul;
1108 
1109 	if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
1110 		x = ri->ri_xorigin;
1111 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
1112 		width = ri->ri_emuwidth;
1113 		height = ri->ri_font->fontheight * nrows;
1114 		rasops_unpack_attr(fillattr, &fg, &bg, &ul);
1115 
1116 		r128fb_rectfill(sc, x, y, width, height, ri->ri_devcmap[bg]);
1117 	}
1118 }
1119 
1120 static void
1121 r128fb_set_backlight(struct r128fb_softc *sc, int level)
1122 {
1123 	uint32_t reg;
1124 
1125 	/*
1126 	 * should we do nothing when backlight is off, should we just store the
1127 	 * level and use it when turning back on or should we just flip sc_bl_on
1128 	 * and turn the backlight on?
1129 	 * For now turn it on so a crashed screensaver can't get the user stuck
1130 	 * with a dark screen as long as hotkeys work
1131 	 */
1132 	if (level > 255) level = 255;
1133 	if (level < 0) level = 0;
1134 	if (level == sc->sc_bl_level)
1135 		return;
1136 	sc->sc_bl_level = level;
1137 	if (sc->sc_bl_on == 0)
1138 		sc->sc_bl_on = 1;
1139 	level = 255 - level;
1140 	reg = bus_space_read_4(sc->sc_memt, sc->sc_regh, R128_LVDS_GEN_CNTL);
1141 	reg &= ~R128_LEVEL_MASK;
1142 	reg |= (level << R128_LEVEL_SHIFT);
1143 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_LVDS_GEN_CNTL, reg);
1144 	DPRINTF("backlight level: %d reg %08x\n", level, reg);
1145 }
1146 
1147 static void
1148 r128fb_switch_backlight(struct r128fb_softc *sc, int on)
1149 {
1150 	uint32_t reg;
1151 	int level;
1152 
1153 	if (on == sc->sc_bl_on)
1154 		return;
1155 	sc->sc_bl_on = on;
1156 	reg = bus_space_read_4(sc->sc_memt, sc->sc_regh, R128_LVDS_GEN_CNTL);
1157 	reg &= ~R128_LEVEL_MASK;
1158 	level = on ? 255 - sc->sc_bl_level : 255;
1159 	reg |= level << R128_LEVEL_SHIFT;
1160 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_LVDS_GEN_CNTL, reg);
1161 	DPRINTF("backlight state: %d reg %08x\n", on, reg);
1162 }
1163 
1164 
1165 static void
1166 r128fb_brightness_up(device_t dev)
1167 {
1168 	struct r128fb_softc *sc = device_private(dev);
1169 
1170 	r128fb_set_backlight(sc, sc->sc_bl_level + 8);
1171 }
1172 
1173 static void
1174 r128fb_brightness_down(device_t dev)
1175 {
1176 	struct r128fb_softc *sc = device_private(dev);
1177 
1178 	r128fb_set_backlight(sc, sc->sc_bl_level - 8);
1179 }
1180