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