xref: /netbsd-src/sys/dev/pci/r128fb.c (revision cac8e449158efc7261bebc8657cbb0125a2cfdde)
1 /*	$NetBSD: r128fb.c,v 1.5 2008/05/30 19:56:14 macallan Exp $	*/
2 
3 /*
4  * Copyright (c) 2007 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.5 2008/05/30 19:56:14 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 <uvm/uvm_extern.h>
45 
46 #include <dev/videomode/videomode.h>
47 
48 #include <dev/pci/pcivar.h>
49 #include <dev/pci/pcireg.h>
50 #include <dev/pci/pcidevs.h>
51 #include <dev/pci/pciio.h>
52 #include <dev/pci/r128fbreg.h>
53 
54 #include <dev/wscons/wsdisplayvar.h>
55 #include <dev/wscons/wsconsio.h>
56 #include <dev/wsfont/wsfont.h>
57 #include <dev/rasops/rasops.h>
58 #include <dev/wscons/wsdisplay_vconsvar.h>
59 
60 #include <dev/i2c/i2cvar.h>
61 
62 struct r128fb_softc {
63 	device_t sc_dev;
64 
65 	pci_chipset_tag_t sc_pc;
66 	pcitag_t sc_pcitag;
67 
68 	bus_space_tag_t sc_memt;
69 	bus_space_tag_t sc_iot;
70 
71 	bus_space_handle_t sc_fbh;
72 	bus_space_handle_t sc_regh;
73 	bus_addr_t sc_fb, sc_reg;
74 	bus_size_t sc_fbsize, sc_regsize;
75 
76 	int sc_width, sc_height, sc_depth, sc_stride;
77 	int sc_locked;
78 	void *sc_fbaddr;
79 	struct vcons_screen sc_console_screen;
80 	struct wsscreen_descr sc_defaultscreen_descr;
81 	const struct wsscreen_descr *sc_screens[1];
82 	struct wsscreen_list sc_screenlist;
83 	struct vcons_data vd;
84 	int sc_mode;
85 	u_char sc_cmap_red[256];
86 	u_char sc_cmap_green[256];
87 	u_char sc_cmap_blue[256];
88 	/* engine stuff */
89 	uint32_t sc_master_cntl;
90 };
91 
92 static int	r128fb_match(device_t, cfdata_t, void *);
93 static void	r128fb_attach(device_t, device_t, void *);
94 
95 CFATTACH_DECL_NEW(r128fb, sizeof(struct r128fb_softc),
96     r128fb_match, r128fb_attach, NULL, NULL);
97 
98 extern const u_char rasops_cmap[768];
99 
100 static int	r128fb_ioctl(void *, void *, u_long, void *, int,
101 			     struct lwp *);
102 static paddr_t	r128fb_mmap(void *, void *, off_t, int);
103 static void	r128fb_init_screen(void *, struct vcons_screen *, int, long *);
104 
105 static int	r128fb_putcmap(struct r128fb_softc *, struct wsdisplay_cmap *);
106 static int 	r128fb_getcmap(struct r128fb_softc *, struct wsdisplay_cmap *);
107 static void	r128fb_restore_palette(struct r128fb_softc *);
108 static int 	r128fb_putpalreg(struct r128fb_softc *, uint8_t, uint8_t,
109 			    uint8_t, uint8_t);
110 
111 static void	r128fb_init(struct r128fb_softc *);
112 static void	r128fb_flush_engine(struct r128fb_softc *);
113 static void	r128fb_rectfill(struct r128fb_softc *, int, int, int, int,
114 			    uint32_t);
115 static void	r128fb_bitblt(struct r128fb_softc *, int, int, int, int, int,
116 			    int, int);
117 
118 static void	r128fb_cursor(void *, int, int, int);
119 #if 0
120 static void	r128fb_putchar(void *, int, int, u_int, long);
121 #endif
122 static void	r128fb_copycols(void *, int, int, int, int);
123 static void	r128fb_erasecols(void *, int, int, int, long);
124 static void	r128fb_copyrows(void *, int, int, int);
125 static void	r128fb_eraserows(void *, int, int, long);
126 
127 struct wsdisplay_accessops r128fb_accessops = {
128 	r128fb_ioctl,
129 	r128fb_mmap,
130 	NULL,	/* alloc_screen */
131 	NULL,	/* free_screen */
132 	NULL,	/* show_screen */
133 	NULL, 	/* load_font */
134 	NULL,	/* pollc */
135 	NULL	/* scroll */
136 };
137 
138 static inline void
139 r128fb_wait(struct r128fb_softc *sc, int slots)
140 {
141 	uint32_t reg;
142 
143 	do {
144 		reg = (bus_space_read_4(sc->sc_memt, sc->sc_regh,
145 		    R128_GUI_STAT) & R128_GUI_FIFOCNT_MASK);
146 	} while (reg <= slots);
147 }
148 
149 static void
150 r128fb_flush_engine(struct r128fb_softc *sc)
151 {
152 	uint32_t reg;
153 
154 	reg = bus_space_read_4(sc->sc_memt, sc->sc_regh, R128_PC_NGUI_CTLSTAT);
155 	reg |= R128_PC_FLUSH_ALL;
156 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_PC_NGUI_CTLSTAT, reg);
157 	do {
158 		reg = bus_space_read_4(sc->sc_memt, sc->sc_regh,
159 		    R128_PC_NGUI_CTLSTAT);
160 	} while (reg & R128_PC_BUSY);
161 }
162 
163 static int
164 r128fb_match(device_t parent, cfdata_t match, void *aux)
165 {
166 	struct pci_attach_args *pa = (struct pci_attach_args *)aux;
167 
168 	if (PCI_CLASS(pa->pa_class) != PCI_CLASS_DISPLAY)
169 		return 0;
170 	if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_ATI)
171 		return 0;
172 
173 	/* only cards tested on so far - likely need a list */
174 	if ((PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_ATI_RAGE1AGP4XT) ||
175 	    (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_ATI_RAGE3AGP4XT))
176 		return 100;
177 	return (0);
178 }
179 
180 static void
181 r128fb_attach(device_t parent, device_t self, void *aux)
182 {
183 	struct r128fb_softc	*sc = device_private(self);
184 	struct pci_attach_args	*pa = aux;
185 	struct rasops_info	*ri;
186 	char devinfo[256];
187 	struct wsemuldisplaydev_attach_args aa;
188 	prop_dictionary_t	dict;
189 	unsigned long		defattr;
190 	bool			is_console;
191 	int i, j;
192 
193 	sc->sc_pc = pa->pa_pc;
194 	sc->sc_pcitag = pa->pa_tag;
195 	sc->sc_memt = pa->pa_memt;
196 	sc->sc_iot = pa->pa_iot;
197 	sc->sc_dev = self;
198 
199 	pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof(devinfo));
200 	aprint_normal(": %s\n", devinfo);
201 
202 	/* fill in parameters from properties */
203 	dict = device_properties(self);
204 	if (!prop_dictionary_get_uint32(dict, "width", &sc->sc_width)) {
205 		aprint_error("%s: no width property\n", device_xname(self));
206 		return;
207 	}
208 	if (!prop_dictionary_get_uint32(dict, "height", &sc->sc_height)) {
209 		aprint_error("%s: no height property\n", device_xname(self));
210 		return;
211 	}
212 	if (!prop_dictionary_get_uint32(dict, "depth", &sc->sc_depth)) {
213 		aprint_error("%s: no depth property\n", device_xname(self));
214 		return;
215 	}
216 	if (!prop_dictionary_get_uint32(dict, "linebytes", &sc->sc_stride)) {
217 		aprint_error("%s: no linebytes property\n",
218 		    device_xname(self));
219 		return;
220 	}
221 
222 	prop_dictionary_get_bool(dict, "is_console", &is_console);
223 
224 	if (pci_mapreg_map(pa, 0x10, PCI_MAPREG_TYPE_MEM,
225 	    BUS_SPACE_MAP_LINEAR,
226 	    &sc->sc_memt, &sc->sc_fbh, &sc->sc_fb, &sc->sc_fbsize)) {
227 		aprint_error("%s: failed to map the frame buffer.\n",
228 		    device_xname(sc->sc_dev));
229 	}
230 	sc->sc_fbaddr = bus_space_vaddr(sc->sc_memt, sc->sc_fbh);
231 
232 	if (pci_mapreg_map(pa, 0x18, PCI_MAPREG_TYPE_MEM, 0,
233 	    &sc->sc_memt, &sc->sc_regh, &sc->sc_reg, &sc->sc_regsize)) {
234 		aprint_error("%s: failed to map registers.\n",
235 		    device_xname(sc->sc_dev));
236 	}
237 
238 	/*
239 	 * XXX yeah, casting the fb address to uint32_t is formally wrong
240 	 * but as far as I know there are no mach64 with 64bit BARs
241 	 */
242 	aprint_normal("%s: %d MB aperture at 0x%08x\n", device_xname(self),
243 	    (int)(sc->sc_fbsize >> 20), (uint32_t)sc->sc_fb);
244 
245 	sc->sc_defaultscreen_descr = (struct wsscreen_descr){
246 		"default",
247 		0, 0,
248 		NULL,
249 		8, 16,
250 		WSSCREEN_WSCOLORS | WSSCREEN_HILIT,
251 		NULL
252 	};
253 	sc->sc_screens[0] = &sc->sc_defaultscreen_descr;
254 	sc->sc_screenlist = (struct wsscreen_list){1, sc->sc_screens};
255 	sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
256 	sc->sc_locked = 0;
257 
258 	vcons_init(&sc->vd, sc, &sc->sc_defaultscreen_descr,
259 	    &r128fb_accessops);
260 	sc->vd.init_screen = r128fb_init_screen;
261 
262 	/* init engine here */
263 	r128fb_init(sc);
264 
265 	ri = &sc->sc_console_screen.scr_ri;
266 
267 	if (is_console) {
268 		vcons_init_screen(&sc->vd, &sc->sc_console_screen, 1,
269 		    &defattr);
270 		sc->sc_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
271 
272 		sc->sc_defaultscreen_descr.textops = &ri->ri_ops;
273 		sc->sc_defaultscreen_descr.capabilities = ri->ri_caps;
274 		sc->sc_defaultscreen_descr.nrows = ri->ri_rows;
275 		sc->sc_defaultscreen_descr.ncols = ri->ri_cols;
276 		wsdisplay_cnattach(&sc->sc_defaultscreen_descr, ri, 0, 0,
277 		    defattr);
278 	} else {
279 		/*
280 		 * since we're not the console we can postpone the rest
281 		 * until someone actually allocates a screen for us
282 		 */
283 		(*ri->ri_ops.allocattr)(ri, 0, 0, 0, &defattr);
284 	}
285 
286 	j = 0;
287 	for (i = 0; i < (1 << sc->sc_depth); i++) {
288 
289 		sc->sc_cmap_red[i] = rasops_cmap[j];
290 		sc->sc_cmap_green[i] = rasops_cmap[j + 1];
291 		sc->sc_cmap_blue[i] = rasops_cmap[j + 2];
292 		r128fb_putpalreg(sc, i, rasops_cmap[j], rasops_cmap[j + 1],
293 		    rasops_cmap[j + 2]);
294 		j += 3;
295 	}
296 
297 	r128fb_rectfill(sc, 0, 0, sc->sc_width, sc->sc_height,
298 	    ri->ri_devcmap[(defattr >> 16) & 0xff]);
299 
300 	aa.console = is_console;
301 	aa.scrdata = &sc->sc_screenlist;
302 	aa.accessops = &r128fb_accessops;
303 	aa.accesscookie = &sc->vd;
304 
305 	config_found(sc->sc_dev, &aa, wsemuldisplaydevprint);
306 
307 }
308 
309 static int
310 r128fb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
311 	struct lwp *l)
312 {
313 	struct vcons_data *vd = v;
314 	struct r128fb_softc *sc = vd->cookie;
315 	struct wsdisplay_fbinfo *wdf;
316 	struct vcons_screen *ms = vd->active;
317 
318 	switch (cmd) {
319 
320 		case WSDISPLAYIO_GTYPE:
321 			*(u_int *)data = WSDISPLAY_TYPE_PCIMISC;
322 			return 0;
323 
324 		/* PCI config read/write passthrough. */
325 		case PCI_IOC_CFGREAD:
326 		case PCI_IOC_CFGWRITE:
327 			return (pci_devioctl(sc->sc_pc, sc->sc_pcitag,
328 			    cmd, data, flag, l));
329 
330 		case WSDISPLAYIO_GINFO:
331 			if (ms == NULL)
332 				return ENODEV;
333 			wdf = (void *)data;
334 			wdf->height = ms->scr_ri.ri_height;
335 			wdf->width = ms->scr_ri.ri_width;
336 			wdf->depth = ms->scr_ri.ri_depth;
337 			wdf->cmsize = 256;
338 			return 0;
339 
340 		case WSDISPLAYIO_GETCMAP:
341 			return r128fb_getcmap(sc,
342 			    (struct wsdisplay_cmap *)data);
343 
344 		case WSDISPLAYIO_PUTCMAP:
345 			return r128fb_putcmap(sc,
346 			    (struct wsdisplay_cmap *)data);
347 
348 		case WSDISPLAYIO_LINEBYTES:
349 			*(u_int *)data = sc->sc_stride;
350 			return 0;
351 
352 		case WSDISPLAYIO_SMODE:
353 			{
354 				int new_mode = *(int*)data;
355 
356 				/* notify the bus backend */
357 				if (new_mode != sc->sc_mode) {
358 					sc->sc_mode = new_mode;
359 					if(new_mode == WSDISPLAYIO_MODE_EMUL) {
360 						r128fb_restore_palette(sc);
361 						vcons_redraw_screen(ms);
362 					}
363 				}
364 			}
365 			return 0;
366 	}
367 	return EPASSTHROUGH;
368 }
369 
370 static paddr_t
371 r128fb_mmap(void *v, void *vs, off_t offset, int prot)
372 {
373 	struct vcons_data *vd = v;
374 	struct r128fb_softc *sc = vd->cookie;
375 	struct lwp *me;
376 	paddr_t pa;
377 
378 	/* 'regular' framebuffer mmap()ing */
379 	if (offset < sc->sc_fbsize) {
380 		pa = bus_space_mmap(sc->sc_memt, sc->sc_fb + offset, 0, prot,
381 		    BUS_SPACE_MAP_LINEAR);
382 		return pa;
383 	}
384 
385 	/*
386 	 * restrict all other mappings to processes with superuser privileges
387 	 * or the kernel itself
388 	 */
389 	me = curlwp;
390 	if (me != NULL) {
391 		if (kauth_authorize_generic(me->l_cred, KAUTH_GENERIC_ISSUSER,
392 		    NULL) != 0) {
393 			aprint_normal("%s: mmap() rejected.\n",
394 			    device_xname(sc->sc_dev));
395 			return -1;
396 		}
397 	}
398 
399 	if ((offset >= sc->sc_fb) && (offset < (sc->sc_fb + sc->sc_fbsize))) {
400 		pa = bus_space_mmap(sc->sc_memt, offset, 0, prot,
401 		    BUS_SPACE_MAP_LINEAR);
402 		return pa;
403 	}
404 
405 	if ((offset >= sc->sc_reg) &&
406 	    (offset < (sc->sc_reg + sc->sc_regsize))) {
407 		pa = bus_space_mmap(sc->sc_memt, offset, 0, prot,
408 		    BUS_SPACE_MAP_LINEAR);
409 		return pa;
410 	}
411 
412 #ifdef PCI_MAGIC_IO_RANGE
413 	/* allow mapping of IO space */
414 	if ((offset >= PCI_MAGIC_IO_RANGE) &&
415 	    (offset < PCI_MAGIC_IO_RANGE + 0x10000)) {
416 		pa = bus_space_mmap(sc->sc_iot, offset - PCI_MAGIC_IO_RANGE,
417 		    0, prot, BUS_SPACE_MAP_LINEAR);
418 		return pa;
419 	}
420 #endif
421 
422 #ifdef OFB_ALLOW_OTHERS
423 	if (offset >= 0x80000000) {
424 		pa = bus_space_mmap(sc->sc_memt, offset, 0, prot,
425 		    BUS_SPACE_MAP_LINEAR);
426 		return pa;
427 	}
428 #endif
429 	return -1;
430 }
431 
432 static void
433 r128fb_init_screen(void *cookie, struct vcons_screen *scr,
434     int existing, long *defattr)
435 {
436 	struct r128fb_softc *sc = cookie;
437 	struct rasops_info *ri = &scr->scr_ri;
438 
439 	ri->ri_depth = sc->sc_depth;
440 	ri->ri_width = sc->sc_width;
441 	ri->ri_height = sc->sc_height;
442 	ri->ri_stride = sc->sc_stride;
443 	ri->ri_flg = RI_CENTER | RI_FULLCLEAR;
444 
445 	ri->ri_bits = (char *)sc->sc_fbaddr;
446 
447 	if (existing) {
448 		ri->ri_flg |= RI_CLEAR;
449 	}
450 
451 	rasops_init(ri, sc->sc_height / 8, sc->sc_width / 8);
452 	ri->ri_caps = WSSCREEN_WSCOLORS;
453 
454 	rasops_reconfig(ri, sc->sc_height / ri->ri_font->fontheight,
455 		    sc->sc_width / ri->ri_font->fontwidth);
456 
457 	ri->ri_hw = scr;
458 	ri->ri_ops.copyrows = r128fb_copyrows;
459 	ri->ri_ops.copycols = r128fb_copycols;
460 	ri->ri_ops.eraserows = r128fb_eraserows;
461 	ri->ri_ops.erasecols = r128fb_erasecols;
462 	ri->ri_ops.cursor = r128fb_cursor;
463 #if 0
464 	ri->ri_ops.putchar = r128fb_putchar;
465 #endif
466 }
467 
468 static int
469 r128fb_putcmap(struct r128fb_softc *sc, struct wsdisplay_cmap *cm)
470 {
471 	u_char *r, *g, *b;
472 	u_int index = cm->index;
473 	u_int count = cm->count;
474 	int i, error;
475 	u_char rbuf[256], gbuf[256], bbuf[256];
476 
477 #ifdef R128FB_DEBUG
478 	aprint_debug("putcmap: %d %d\n",index, count);
479 #endif
480 	if (cm->index >= 256 || cm->count > 256 ||
481 	    (cm->index + cm->count) > 256)
482 		return EINVAL;
483 	error = copyin(cm->red, &rbuf[index], count);
484 	if (error)
485 		return error;
486 	error = copyin(cm->green, &gbuf[index], count);
487 	if (error)
488 		return error;
489 	error = copyin(cm->blue, &bbuf[index], count);
490 	if (error)
491 		return error;
492 
493 	memcpy(&sc->sc_cmap_red[index], &rbuf[index], count);
494 	memcpy(&sc->sc_cmap_green[index], &gbuf[index], count);
495 	memcpy(&sc->sc_cmap_blue[index], &bbuf[index], count);
496 
497 	r = &sc->sc_cmap_red[index];
498 	g = &sc->sc_cmap_green[index];
499 	b = &sc->sc_cmap_blue[index];
500 
501 	for (i = 0; i < count; i++) {
502 		r128fb_putpalreg(sc, index, *r, *g, *b);
503 		index++;
504 		r++, g++, b++;
505 	}
506 	return 0;
507 }
508 
509 static int
510 r128fb_getcmap(struct r128fb_softc *sc, struct wsdisplay_cmap *cm)
511 {
512 	u_int index = cm->index;
513 	u_int count = cm->count;
514 	int error;
515 
516 	if (index >= 255 || count > 256 || index + count > 256)
517 		return EINVAL;
518 
519 	error = copyout(&sc->sc_cmap_red[index],   cm->red,   count);
520 	if (error)
521 		return error;
522 	error = copyout(&sc->sc_cmap_green[index], cm->green, count);
523 	if (error)
524 		return error;
525 	error = copyout(&sc->sc_cmap_blue[index],  cm->blue,  count);
526 	if (error)
527 		return error;
528 
529 	return 0;
530 }
531 
532 static void
533 r128fb_restore_palette(struct r128fb_softc *sc)
534 {
535 	int i;
536 
537 	for (i = 0; i < (1 << sc->sc_depth); i++) {
538 		r128fb_putpalreg(sc, i, sc->sc_cmap_red[i],
539 		    sc->sc_cmap_green[i], sc->sc_cmap_blue[i]);
540 	}
541 }
542 
543 static int
544 r128fb_putpalreg(struct r128fb_softc *sc, uint8_t idx, uint8_t r, uint8_t g,
545     uint8_t b)
546 {
547 	uint32_t reg;
548 
549 	/* whack the DAC */
550 	reg = (r << 16) | (g << 8) | b;
551 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_PALETTE_INDEX, idx);
552 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_PALETTE_DATA, reg);
553 	return 0;
554 }
555 
556 static void
557 r128fb_init(struct r128fb_softc *sc)
558 {
559 	uint32_t datatype;
560 
561 	r128fb_flush_engine(sc);
562 
563 	r128fb_wait(sc, 8);
564 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_CRTC_OFFSET, 0);
565 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DEFAULT_OFFSET, 0);
566 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DEFAULT_PITCH,
567 	    sc->sc_stride >> 3);
568 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_AUX_SC_CNTL, 0);
569 	bus_space_write_4(sc->sc_memt, sc->sc_regh,
570 	    R128_DEFAULT_SC_BOTTOM_RIGHT,
571 	    R128_DEFAULT_SC_RIGHT_MAX | R128_DEFAULT_SC_BOTTOM_MAX);
572 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_SC_TOP_LEFT, 0);
573 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_SC_BOTTOM_RIGHT,
574 	    R128_DEFAULT_SC_RIGHT_MAX | R128_DEFAULT_SC_BOTTOM_MAX);
575 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DP_DATATYPE,
576 	    R128_HOST_BIG_ENDIAN_EN);
577 
578 	r128fb_wait(sc, 5);
579 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_SRC_PITCH,
580 	    sc->sc_stride >> 3);
581 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DST_PITCH,
582 	    sc->sc_stride >> 3);
583 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_SRC_OFFSET, 0);
584 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DST_OFFSET, 0);
585 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DP_WRITE_MASK,
586 	    0xffffffff);
587 
588 	switch (sc->sc_depth) {
589 		case 8:
590 			datatype = R128_GMC_DST_8BPP_CI;
591 			break;
592 		case 15:
593 			datatype = R128_GMC_DST_15BPP;
594 			break;
595 		case 16:
596 			datatype = R128_GMC_DST_16BPP;
597 			break;
598 		case 24:
599 			datatype = R128_GMC_DST_24BPP;
600 			break;
601 		case 32:
602 			datatype = R128_GMC_DST_32BPP;
603 			break;
604 		default:
605 			aprint_error("%s: unsupported depth %d\n",
606 			    device_xname(sc->sc_dev), sc->sc_depth);
607 			return;
608 	}
609 	sc->sc_master_cntl = R128_GMC_CLR_CMP_CNTL_DIS |
610 	    R128_GMC_AUX_CLIP_DIS | datatype;
611 
612 	r128fb_flush_engine(sc);
613 }
614 
615 static void
616 r128fb_rectfill(struct r128fb_softc *sc, int x, int y, int wi, int he,
617      uint32_t colour)
618 {
619 
620 	r128fb_wait(sc, 6);
621 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DP_GUI_MASTER_CNTL,
622 	    R128_GMC_BRUSH_SOLID_COLOR |
623 	    R128_GMC_SRC_DATATYPE_COLOR |
624 	    R128_ROP3_P |
625 	    sc->sc_master_cntl);
626 
627 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DP_BRUSH_FRGD_CLR,
628 	    colour);
629 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DP_CNTL,
630 	    R128_DST_X_LEFT_TO_RIGHT | R128_DST_Y_TOP_TO_BOTTOM);
631 	/* now feed it coordinates */
632 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DST_X_Y,
633 	    (x << 16) | y);
634 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DST_WIDTH_HEIGHT,
635 	    (wi << 16) | he);
636 	r128fb_flush_engine(sc);
637 }
638 
639 static void
640 r128fb_bitblt(struct r128fb_softc *sc, int xs, int ys, int xd, int yd,
641     int wi, int he, int rop)
642 {
643 	uint32_t dp_cntl = 0;
644 
645 	r128fb_wait(sc, 5);
646 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DP_GUI_MASTER_CNTL,
647 	    R128_GMC_BRUSH_SOLID_COLOR |
648 	    R128_GMC_SRC_DATATYPE_COLOR |
649 	    rop |
650 	    R128_DP_SRC_SOURCE_MEMORY |
651 	    sc->sc_master_cntl);
652 
653 	if (yd <= ys) {
654 		dp_cntl = R128_DST_Y_TOP_TO_BOTTOM;
655 	} else {
656 		ys += he - 1;
657 		yd += he - 1;
658 	}
659 	if (xd <= xs) {
660 		dp_cntl |= R128_DST_X_LEFT_TO_RIGHT;
661 	} else {
662 		xs += wi - 1;
663 		xd += wi - 1;
664 	}
665 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DP_CNTL, dp_cntl);
666 
667 	/* now feed it coordinates */
668 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_SRC_X_Y,
669 	    (xs << 16) | ys);
670 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DST_X_Y,
671 	    (xd << 16) | yd);
672 	bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DST_WIDTH_HEIGHT,
673 	    (wi << 16) | he);
674 	r128fb_flush_engine(sc);
675 }
676 
677 static void
678 r128fb_cursor(void *cookie, int on, int row, int col)
679 {
680 	struct rasops_info *ri = cookie;
681 	struct vcons_screen *scr = ri->ri_hw;
682 	struct r128fb_softc *sc = scr->scr_cookie;
683 	int x, y, wi, he;
684 
685 	wi = ri->ri_font->fontwidth;
686 	he = ri->ri_font->fontheight;
687 
688 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
689 		x = ri->ri_ccol * wi + ri->ri_xorigin;
690 		y = ri->ri_crow * he + ri->ri_yorigin;
691 		if (ri->ri_flg & RI_CURSOR) {
692 			r128fb_bitblt(sc, x, y, x, y, wi, he, R128_ROP3_Dn);
693 			ri->ri_flg &= ~RI_CURSOR;
694 		}
695 		ri->ri_crow = row;
696 		ri->ri_ccol = col;
697 		if (on) {
698 			x = ri->ri_ccol * wi + ri->ri_xorigin;
699 			y = ri->ri_crow * he + ri->ri_yorigin;
700 			r128fb_bitblt(sc, x, y, x, y, wi, he, R128_ROP3_Dn);
701 			ri->ri_flg |= RI_CURSOR;;
702 		}
703 	} else {
704 		scr->scr_ri.ri_crow = row;
705 		scr->scr_ri.ri_ccol = col;
706 		scr->scr_ri.ri_flg &= ~RI_CURSOR;
707 	}
708 
709 }
710 
711 #if 0
712 static void
713 r128fb_putchar(void *cookie, int row, int col, u_int c, long attr)
714 {
715 }
716 #endif
717 
718 static void
719 r128fb_copycols(void *cookie, int row, int srccol, int dstcol, int ncols)
720 {
721 	struct rasops_info *ri = cookie;
722 	struct vcons_screen *scr = ri->ri_hw;
723 	struct r128fb_softc *sc = scr->scr_cookie;
724 	int32_t xs, xd, y, width, height;
725 
726 	if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
727 		xs = ri->ri_xorigin + ri->ri_font->fontwidth * srccol;
728 		xd = ri->ri_xorigin + ri->ri_font->fontwidth * dstcol;
729 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
730 		width = ri->ri_font->fontwidth * ncols;
731 		height = ri->ri_font->fontheight;
732 		r128fb_bitblt(sc, xs, y, xd, y, width, height, R128_ROP3_S);
733 	}
734 }
735 
736 static void
737 r128fb_erasecols(void *cookie, int row, int startcol, int ncols, long fillattr)
738 {
739 	struct rasops_info *ri = cookie;
740 	struct vcons_screen *scr = ri->ri_hw;
741 	struct r128fb_softc *sc = scr->scr_cookie;
742 	int32_t x, y, width, height, fg, bg, ul;
743 
744 	if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
745 		x = ri->ri_xorigin + ri->ri_font->fontwidth * startcol;
746 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
747 		width = ri->ri_font->fontwidth * ncols;
748 		height = ri->ri_font->fontheight;
749 		rasops_unpack_attr(fillattr, &fg, &bg, &ul);
750 
751 		r128fb_rectfill(sc, x, y, width, height, ri->ri_devcmap[bg]);
752 	}
753 }
754 
755 static void
756 r128fb_copyrows(void *cookie, int srcrow, int dstrow, int nrows)
757 {
758 	struct rasops_info *ri = cookie;
759 	struct vcons_screen *scr = ri->ri_hw;
760 	struct r128fb_softc *sc = scr->scr_cookie;
761 	int32_t x, ys, yd, width, height;
762 
763 	if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
764 		x = ri->ri_xorigin;
765 		ys = ri->ri_yorigin + ri->ri_font->fontheight * srcrow;
766 		yd = ri->ri_yorigin + ri->ri_font->fontheight * dstrow;
767 		width = ri->ri_emuwidth;
768 		height = ri->ri_font->fontheight*nrows;
769 		r128fb_bitblt(sc, x, ys, x, yd, width, height, R128_ROP3_S);
770 	}
771 }
772 
773 static void
774 r128fb_eraserows(void *cookie, int row, int nrows, long fillattr)
775 {
776 	struct rasops_info *ri = cookie;
777 	struct vcons_screen *scr = ri->ri_hw;
778 	struct r128fb_softc *sc = scr->scr_cookie;
779 	int32_t x, y, width, height, fg, bg, ul;
780 
781 	if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
782 		x = ri->ri_xorigin;
783 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
784 		width = ri->ri_emuwidth;
785 		height = ri->ri_font->fontheight * nrows;
786 		rasops_unpack_attr(fillattr, &fg, &bg, &ul);
787 
788 		r128fb_rectfill(sc, x, y, width, height, ri->ri_devcmap[bg]);
789 	}
790 }
791 
792