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