xref: /netbsd-src/sys/dev/pci/pm2fb.c (revision 4817a0b0b8fe9612e8ebe21a9bf2d97b95038a97)
1 /*	$NetBSD: pm2fb.c,v 1.6 2010/12/16 06:45:50 cegger Exp $	*/
2 
3 /*
4  * Copyright (c) 2009 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 Permedia 2 graphics controllers
30  * tested on sparc64 only so far
31  */
32 
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: pm2fb.c,v 1.6 2010/12/16 06:45:50 cegger 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/pm2reg.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 
58 #include <dev/i2c/i2cvar.h>
59 
60 struct pm2fb_softc {
61 	device_t sc_dev;
62 
63 	pci_chipset_tag_t sc_pc;
64 	pcitag_t sc_pcitag;
65 
66 	bus_space_tag_t sc_memt;
67 	bus_space_tag_t sc_iot;
68 
69 	bus_space_handle_t sc_regh;
70 	bus_addr_t sc_fb, sc_reg;
71 	bus_size_t sc_fbsize, sc_regsize;
72 
73 	int sc_width, sc_height, sc_depth, sc_stride;
74 	int sc_locked;
75 	struct vcons_screen sc_console_screen;
76 	struct wsscreen_descr sc_defaultscreen_descr;
77 	const struct wsscreen_descr *sc_screens[1];
78 	struct wsscreen_list sc_screenlist;
79 	struct vcons_data vd;
80 	int sc_mode;
81 	u_char sc_cmap_red[256];
82 	u_char sc_cmap_green[256];
83 	u_char sc_cmap_blue[256];
84 	/* engine stuff */
85 	uint32_t sc_pprod;
86 };
87 
88 static int	pm2fb_match(device_t, cfdata_t, void *);
89 static void	pm2fb_attach(device_t, device_t, void *);
90 
91 CFATTACH_DECL_NEW(pm2fb, sizeof(struct pm2fb_softc),
92     pm2fb_match, pm2fb_attach, NULL, NULL);
93 
94 extern const u_char rasops_cmap[768];
95 
96 static int	pm2fb_ioctl(void *, void *, u_long, void *, int,
97 			     struct lwp *);
98 static paddr_t	pm2fb_mmap(void *, void *, off_t, int);
99 static void	pm2fb_init_screen(void *, struct vcons_screen *, int, long *);
100 
101 static int	pm2fb_putcmap(struct pm2fb_softc *, struct wsdisplay_cmap *);
102 static int 	pm2fb_getcmap(struct pm2fb_softc *, struct wsdisplay_cmap *);
103 static void	pm2fb_restore_palette(struct pm2fb_softc *);
104 static int 	pm2fb_putpalreg(struct pm2fb_softc *, uint8_t, uint8_t,
105 			    uint8_t, uint8_t);
106 
107 static void	pm2fb_init(struct pm2fb_softc *);
108 static void	pm2fb_flush_engine(struct pm2fb_softc *);
109 static void	pm2fb_rectfill(struct pm2fb_softc *, int, int, int, int,
110 			    uint32_t);
111 static void	pm2fb_bitblt(struct pm2fb_softc *, int, int, int, int, int,
112 			    int, int);
113 
114 static void	pm2fb_cursor(void *, int, int, int);
115 static void	pm2fb_putchar(void *, int, int, u_int, long);
116 static void	pm2fb_copycols(void *, int, int, int, int);
117 static void	pm2fb_erasecols(void *, int, int, int, long);
118 static void	pm2fb_copyrows(void *, int, int, int);
119 static void	pm2fb_eraserows(void *, int, int, long);
120 
121 struct wsdisplay_accessops pm2fb_accessops = {
122 	pm2fb_ioctl,
123 	pm2fb_mmap,
124 	NULL,	/* alloc_screen */
125 	NULL,	/* free_screen */
126 	NULL,	/* show_screen */
127 	NULL, 	/* load_font */
128 	NULL,	/* pollc */
129 	NULL	/* scroll */
130 };
131 
132 static inline void
133 pm2fb_wait(struct pm2fb_softc *sc, int slots)
134 {
135 	uint32_t reg;
136 
137 	do {
138 		reg = bus_space_read_4(sc->sc_memt, sc->sc_regh,
139 			PM2_INPUT_FIFO_SPACE);
140 	} while (reg <= slots);
141 }
142 
143 static void
144 pm2fb_flush_engine(struct pm2fb_softc *sc)
145 {
146 
147 	pm2fb_wait(sc, 2);
148 
149 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_FILTER_MODE,
150 	    PM2FLT_PASS_SYNC);
151 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_SYNC, 0);
152 	do {
153 		while (bus_space_read_4(sc->sc_memt, sc->sc_regh,
154 			PM2_OUTPUT_FIFO_WORDS) == 0);
155 	} while (bus_space_read_4(sc->sc_memt, sc->sc_regh, PM2_OUTPUT_FIFO) !=
156 	    0x188);
157 }
158 
159 static int
160 pm2fb_match(device_t parent, cfdata_t match, void *aux)
161 {
162 	struct pci_attach_args *pa = (struct pci_attach_args *)aux;
163 
164 	if (PCI_CLASS(pa->pa_class) != PCI_CLASS_DISPLAY)
165 		return 0;
166 	if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_3DLABS)
167 		return 0;
168 
169 	/* only cards tested on so far - likely need a list */
170 	if ((PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_3DLABS_PERMEDIA2) ||
171 	    (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_3DLABS_PERMEDIA2V))
172 		return 100;
173 	return (0);
174 }
175 
176 static void
177 pm2fb_attach(device_t parent, device_t self, void *aux)
178 {
179 	struct pm2fb_softc	*sc = device_private(self);
180 	struct pci_attach_args	*pa = aux;
181 	struct rasops_info	*ri;
182 	char devinfo[256];
183 	struct wsemuldisplaydev_attach_args aa;
184 	prop_dictionary_t	dict;
185 	unsigned long		defattr;
186 	bool			is_console;
187 	int i, j;
188 	uint32_t flags;
189 
190 	sc->sc_pc = pa->pa_pc;
191 	sc->sc_pcitag = pa->pa_tag;
192 	sc->sc_memt = pa->pa_memt;
193 	sc->sc_iot = pa->pa_iot;
194 	sc->sc_dev = self;
195 
196 	pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof(devinfo));
197 	aprint_normal(": %s\n", devinfo);
198 
199 	/* fill in parameters from properties */
200 	dict = device_properties(self);
201 	if (!prop_dictionary_get_uint32(dict, "width", &sc->sc_width)) {
202 		aprint_error("%s: no width property\n", device_xname(self));
203 		return;
204 	}
205 	if (!prop_dictionary_get_uint32(dict, "height", &sc->sc_height)) {
206 		aprint_error("%s: no height property\n", device_xname(self));
207 		return;
208 	}
209 	if (!prop_dictionary_get_uint32(dict, "depth", &sc->sc_depth)) {
210 		aprint_error("%s: no depth property\n", device_xname(self));
211 		return;
212 	}
213 	/*
214 	 * don't look at the linebytes property - The Raptor firmware lies
215 	 * about it. Get it from width * depth >> 3 instead.
216 	 */
217 	sc->sc_stride = sc->sc_width * (sc->sc_depth >> 3);
218 
219 	prop_dictionary_get_bool(dict, "is_console", &is_console);
220 
221 	pci_mapreg_info(pa->pa_pc, pa->pa_tag, 0x14, PCI_MAPREG_TYPE_MEM,
222 	    &sc->sc_fb, &sc->sc_fbsize, &flags);
223 
224 	if (pci_mapreg_map(pa, 0x10, PCI_MAPREG_TYPE_MEM, 0,
225 	    &sc->sc_memt, &sc->sc_regh, &sc->sc_reg, &sc->sc_regsize)) {
226 		aprint_error("%s: failed to map registers.\n",
227 		    device_xname(sc->sc_dev));
228 	}
229 
230 	/*
231 	 * XXX yeah, casting the fb address to uint32_t is formally wrong
232 	 * but as far as I know there are no PM2 with 64bit BARs
233 	 */
234 	aprint_normal("%s: %d MB aperture at 0x%08x\n", device_xname(self),
235 	    (int)(sc->sc_fbsize >> 20), (uint32_t)sc->sc_fb);
236 
237 	sc->sc_defaultscreen_descr = (struct wsscreen_descr){
238 		"default",
239 		0, 0,
240 		NULL,
241 		8, 16,
242 		WSSCREEN_WSCOLORS | WSSCREEN_HILIT,
243 		NULL
244 	};
245 	sc->sc_screens[0] = &sc->sc_defaultscreen_descr;
246 	sc->sc_screenlist = (struct wsscreen_list){1, sc->sc_screens};
247 	sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
248 	sc->sc_locked = 0;
249 
250 	vcons_init(&sc->vd, sc, &sc->sc_defaultscreen_descr,
251 	    &pm2fb_accessops);
252 	sc->vd.init_screen = pm2fb_init_screen;
253 
254 	/* init engine here */
255 	pm2fb_init(sc);
256 
257 	ri = &sc->sc_console_screen.scr_ri;
258 
259 	j = 0;
260 	for (i = 0; i < (1 << sc->sc_depth); i++) {
261 
262 		sc->sc_cmap_red[i] = rasops_cmap[j];
263 		sc->sc_cmap_green[i] = rasops_cmap[j + 1];
264 		sc->sc_cmap_blue[i] = rasops_cmap[j + 2];
265 		pm2fb_putpalreg(sc, i, rasops_cmap[j], rasops_cmap[j + 1],
266 		    rasops_cmap[j + 2]);
267 		j += 3;
268 	}
269 
270 	if (is_console) {
271 		vcons_init_screen(&sc->vd, &sc->sc_console_screen, 1,
272 		    &defattr);
273 		sc->sc_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
274 
275 		pm2fb_rectfill(sc, 0, 0, sc->sc_width, sc->sc_height,
276 		    ri->ri_devcmap[(defattr >> 16) & 0xff]);
277 		sc->sc_defaultscreen_descr.textops = &ri->ri_ops;
278 		sc->sc_defaultscreen_descr.capabilities = ri->ri_caps;
279 		sc->sc_defaultscreen_descr.nrows = ri->ri_rows;
280 		sc->sc_defaultscreen_descr.ncols = ri->ri_cols;
281 		wsdisplay_cnattach(&sc->sc_defaultscreen_descr, ri, 0, 0,
282 		    defattr);
283 		vcons_replay_msgbuf(&sc->sc_console_screen);
284 	} else {
285 		/*
286 		 * since we're not the console we can postpone the rest
287 		 * until someone actually allocates a screen for us
288 		 */
289 		(*ri->ri_ops.allocattr)(ri, 0, 0, 0, &defattr);
290 	}
291 
292 	aa.console = is_console;
293 	aa.scrdata = &sc->sc_screenlist;
294 	aa.accessops = &pm2fb_accessops;
295 	aa.accesscookie = &sc->vd;
296 
297 	config_found(sc->sc_dev, &aa, wsemuldisplaydevprint);
298 }
299 
300 static int
301 pm2fb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
302 	struct lwp *l)
303 {
304 	struct vcons_data *vd = v;
305 	struct pm2fb_softc *sc = vd->cookie;
306 	struct wsdisplay_fbinfo *wdf;
307 	struct vcons_screen *ms = vd->active;
308 
309 	switch (cmd) {
310 	case WSDISPLAYIO_GTYPE:
311 		*(u_int *)data = WSDISPLAY_TYPE_PCIMISC;
312 		return 0;
313 
314 	/* PCI config read/write passthrough. */
315 	case PCI_IOC_CFGREAD:
316 	case PCI_IOC_CFGWRITE:
317 		return pci_devioctl(sc->sc_pc, sc->sc_pcitag,
318 		    cmd, data, flag, l);
319 
320 	case WSDISPLAYIO_GINFO:
321 		if (ms == NULL)
322 			return ENODEV;
323 		wdf = (void *)data;
324 		wdf->height = ms->scr_ri.ri_height;
325 		wdf->width = ms->scr_ri.ri_width;
326 		wdf->depth = ms->scr_ri.ri_depth;
327 		wdf->cmsize = 256;
328 		return 0;
329 
330 	case WSDISPLAYIO_GETCMAP:
331 		return pm2fb_getcmap(sc,
332 		    (struct wsdisplay_cmap *)data);
333 
334 	case WSDISPLAYIO_PUTCMAP:
335 		return pm2fb_putcmap(sc,
336 		    (struct wsdisplay_cmap *)data);
337 
338 	case WSDISPLAYIO_LINEBYTES:
339 		*(u_int *)data = sc->sc_stride;
340 		return 0;
341 
342 	case WSDISPLAYIO_SMODE: {
343 		int new_mode = *(int*)data;
344 		if (new_mode != sc->sc_mode) {
345 			sc->sc_mode = new_mode;
346 			if(new_mode == WSDISPLAYIO_MODE_EMUL) {
347 				pm2fb_restore_palette(sc);
348 				vcons_redraw_screen(ms);
349 			} else
350 				pm2fb_flush_engine(sc);
351 		}
352 		}
353 		return 0;
354 	}
355 	return EPASSTHROUGH;
356 }
357 
358 static paddr_t
359 pm2fb_mmap(void *v, void *vs, off_t offset, int prot)
360 {
361 	struct vcons_data *vd = v;
362 	struct pm2fb_softc *sc = vd->cookie;
363 	paddr_t pa;
364 
365 	/* 'regular' framebuffer mmap()ing */
366 	if (offset < sc->sc_fbsize) {
367 		pa = bus_space_mmap(sc->sc_memt, sc->sc_fb + offset, 0, prot,
368 		    BUS_SPACE_MAP_LINEAR);
369 		return pa;
370 	}
371 
372 	/*
373 	 * restrict all other mappings to processes with superuser privileges
374 	 * or the kernel itself
375 	 */
376 	if (kauth_authorize_generic(kauth_cred_get(), KAUTH_GENERIC_ISSUSER,
377 	    NULL) != 0) {
378 		aprint_normal("%s: mmap() rejected.\n",
379 		    device_xname(sc->sc_dev));
380 		return -1;
381 	}
382 
383 	if ((offset >= sc->sc_fb) && (offset < (sc->sc_fb + sc->sc_fbsize))) {
384 		pa = bus_space_mmap(sc->sc_memt, offset, 0, prot,
385 		    BUS_SPACE_MAP_LINEAR);
386 		return pa;
387 	}
388 
389 	if ((offset >= sc->sc_reg) &&
390 	    (offset < (sc->sc_reg + sc->sc_regsize))) {
391 		pa = bus_space_mmap(sc->sc_memt, offset, 0, prot,
392 		    BUS_SPACE_MAP_LINEAR);
393 		return pa;
394 	}
395 
396 #ifdef PCI_MAGIC_IO_RANGE
397 	/* allow mapping of IO space */
398 	if ((offset >= PCI_MAGIC_IO_RANGE) &&
399 	    (offset < PCI_MAGIC_IO_RANGE + 0x10000)) {
400 		pa = bus_space_mmap(sc->sc_iot, offset - PCI_MAGIC_IO_RANGE,
401 		    0, prot, BUS_SPACE_MAP_LINEAR);
402 		return pa;
403 	}
404 #endif
405 
406 	return -1;
407 }
408 
409 static void
410 pm2fb_init_screen(void *cookie, struct vcons_screen *scr,
411     int existing, long *defattr)
412 {
413 	struct pm2fb_softc *sc = cookie;
414 	struct rasops_info *ri = &scr->scr_ri;
415 
416 	ri->ri_depth = sc->sc_depth;
417 	ri->ri_width = sc->sc_width;
418 	ri->ri_height = sc->sc_height;
419 	ri->ri_stride = sc->sc_stride;
420 	ri->ri_flg = RI_CENTER;
421 
422 	rasops_init(ri, sc->sc_height / 8, sc->sc_width / 8);
423 	ri->ri_caps = WSSCREEN_WSCOLORS;
424 
425 	rasops_reconfig(ri, sc->sc_height / ri->ri_font->fontheight,
426 		    sc->sc_width / ri->ri_font->fontwidth);
427 
428 	ri->ri_hw = scr;
429 	ri->ri_ops.copyrows = pm2fb_copyrows;
430 	ri->ri_ops.copycols = pm2fb_copycols;
431 	ri->ri_ops.cursor = pm2fb_cursor;
432 	ri->ri_ops.eraserows = pm2fb_eraserows;
433 	ri->ri_ops.erasecols = pm2fb_erasecols;
434 	ri->ri_ops.putchar = pm2fb_putchar;
435 }
436 
437 static int
438 pm2fb_putcmap(struct pm2fb_softc *sc, struct wsdisplay_cmap *cm)
439 {
440 	u_char *r, *g, *b;
441 	u_int index = cm->index;
442 	u_int count = cm->count;
443 	int i, error;
444 	u_char rbuf[256], gbuf[256], bbuf[256];
445 
446 #ifdef PM2FB_DEBUG
447 	aprint_debug("putcmap: %d %d\n",index, count);
448 #endif
449 	if (cm->index >= 256 || cm->count > 256 ||
450 	    (cm->index + cm->count) > 256)
451 		return EINVAL;
452 	error = copyin(cm->red, &rbuf[index], count);
453 	if (error)
454 		return error;
455 	error = copyin(cm->green, &gbuf[index], count);
456 	if (error)
457 		return error;
458 	error = copyin(cm->blue, &bbuf[index], count);
459 	if (error)
460 		return error;
461 
462 	memcpy(&sc->sc_cmap_red[index], &rbuf[index], count);
463 	memcpy(&sc->sc_cmap_green[index], &gbuf[index], count);
464 	memcpy(&sc->sc_cmap_blue[index], &bbuf[index], count);
465 
466 	r = &sc->sc_cmap_red[index];
467 	g = &sc->sc_cmap_green[index];
468 	b = &sc->sc_cmap_blue[index];
469 
470 	for (i = 0; i < count; i++) {
471 		pm2fb_putpalreg(sc, index, *r, *g, *b);
472 		index++;
473 		r++, g++, b++;
474 	}
475 	return 0;
476 }
477 
478 static int
479 pm2fb_getcmap(struct pm2fb_softc *sc, struct wsdisplay_cmap *cm)
480 {
481 	u_int index = cm->index;
482 	u_int count = cm->count;
483 	int error;
484 
485 	if (index >= 255 || count > 256 || index + count > 256)
486 		return EINVAL;
487 
488 	error = copyout(&sc->sc_cmap_red[index],   cm->red,   count);
489 	if (error)
490 		return error;
491 	error = copyout(&sc->sc_cmap_green[index], cm->green, count);
492 	if (error)
493 		return error;
494 	error = copyout(&sc->sc_cmap_blue[index],  cm->blue,  count);
495 	if (error)
496 		return error;
497 
498 	return 0;
499 }
500 
501 static void
502 pm2fb_restore_palette(struct pm2fb_softc *sc)
503 {
504 	int i;
505 
506 	for (i = 0; i < (1 << sc->sc_depth); i++) {
507 		pm2fb_putpalreg(sc, i, sc->sc_cmap_red[i],
508 		    sc->sc_cmap_green[i], sc->sc_cmap_blue[i]);
509 	}
510 }
511 
512 static int
513 pm2fb_putpalreg(struct pm2fb_softc *sc, uint8_t idx, uint8_t r, uint8_t g,
514     uint8_t b)
515 {
516 	bus_space_write_1(sc->sc_memt, sc->sc_regh, PM2_DAC_PAL_WRITE_IDX, idx);
517 	bus_space_write_1(sc->sc_memt, sc->sc_regh, PM2_DAC_DATA, r);
518 	bus_space_write_1(sc->sc_memt, sc->sc_regh, PM2_DAC_DATA, g);
519 	bus_space_write_1(sc->sc_memt, sc->sc_regh, PM2_DAC_DATA, b);
520 	return 0;
521 }
522 
523 static void
524 pm2fb_init(struct pm2fb_softc *sc)
525 {
526 	pm2fb_flush_engine(sc);
527 
528 	pm2fb_wait(sc, 8);
529 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_SCREEN_BASE, 0);
530 #if 0
531 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_BYPASS_MASK,
532 		0xffffffff);
533 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_FB_WRITE_MASK,
534 		0xffffffff);
535 #endif
536 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_HW_WRITEMASK,
537 		0xffffffff);
538 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_SW_WRITEMASK,
539 		0xffffffff);
540 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_WRITE_MODE,
541 		PM2WM_WRITE_EN);
542 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_SCREENSIZE,
543 	    (sc->sc_height << 16) | sc->sc_width);
544 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_SCISSOR_MODE,
545 	    PM2SC_SCREEN_EN);
546 	pm2fb_wait(sc, 8);
547 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_DITHER_MODE, 0);
548 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_ALPHA_MODE, 0);
549 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_DDA_MODE, 0);
550 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_TEX_COLOUR_MODE, 0);
551 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_TEX_ADDRESS_MODE, 0);
552 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_TEX_READ_MODE, 0);
553 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_TEX_LUT_MODE, 0);
554 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_YUV_MODE, 0);
555 	pm2fb_wait(sc, 8);
556 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_DEPTH_MODE, 0);
557 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_DEPTH, 0);
558 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_STENCIL_MODE, 0);
559 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_STIPPLE_MODE, 0);
560 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_ROP_MODE, 0);
561 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_WINDOW_ORIGIN, 0);
562 	sc->sc_pprod = bus_space_read_4(sc->sc_memt, sc->sc_regh,
563 	    PM2_FB_READMODE) &
564 	    (PM2FB_PP0_MASK | PM2FB_PP1_MASK | PM2FB_PP2_MASK);
565 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_FB_READMODE,
566 	    sc->sc_pprod);
567 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_TEXMAP_FORMAT,
568 	    sc->sc_pprod);
569 	pm2fb_wait(sc, 8);
570 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_DY, 1 << 16);
571 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_DXDOM, 0);
572 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_STARTXDOM, 0);
573 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_STARTXSUB, 0);
574 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_STARTY, 0);
575 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_COUNT, 0);
576 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_SCISSOR_MINYX, 0);
577 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_SCISSOR_MAXYX,
578 	    0x0fff0fff);
579 	pm2fb_flush_engine(sc);
580 }
581 
582 static void
583 pm2fb_rectfill(struct pm2fb_softc *sc, int x, int y, int wi, int he,
584      uint32_t colour)
585 {
586 
587 	pm2fb_wait(sc, 7);
588 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_DDA_MODE, 0);
589 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_MODE, 0);
590 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_CONFIG,
591 	    PM2RECFG_WRITE_EN);
592 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_BLOCK_COLOUR,
593 	    colour);
594 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_RECT_START,
595 	    (y << 16) | x);
596 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_RECT_SIZE,
597 	    (he << 16) | wi);
598 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_RENDER,
599 	    PM2RE_RECTANGLE | PM2RE_INC_X | PM2RE_INC_Y | PM2RE_FASTFILL);
600 }
601 
602 static void
603 pm2fb_bitblt(struct pm2fb_softc *sc, int xs, int ys, int xd, int yd,
604     int wi, int he, int rop)
605 {
606 	uint32_t dir = 0;
607 
608 	if (yd <= ys) {
609 		dir |= PM2RE_INC_Y;
610 	}
611 	if (xd <= xs) {
612 		dir |= PM2RE_INC_X;
613 	}
614 	pm2fb_wait(sc, 7);
615 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_DDA_MODE, 0);
616 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_MODE, 0);
617 	if (rop == 3) {
618 		bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_CONFIG,
619 		    PM2RECFG_READ_SRC | PM2RECFG_WRITE_EN | PM2RECFG_ROP_EN |
620 		    PM2RECFG_PACKED | (rop << 6));
621 	} else {
622 		bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_CONFIG,
623 		    PM2RECFG_READ_SRC | PM2RECFG_READ_DST | PM2RECFG_WRITE_EN |
624 		    PM2RECFG_PACKED | PM2RECFG_ROP_EN | (rop << 6));
625 	}
626 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_RECT_START,
627 	    (yd << 16) | xd);
628 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_RECT_SIZE,
629 	    (he << 16) | wi);
630 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_SOURCE_DELTA,
631 	    (((ys - yd) & 0xfff) << 16) | ((xs - xd) & 0xfff));
632 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_RENDER,
633 	    PM2RE_RECTANGLE | dir);
634 }
635 
636 static void
637 pm2fb_cursor(void *cookie, int on, int row, int col)
638 {
639 	struct rasops_info *ri = cookie;
640 	struct vcons_screen *scr = ri->ri_hw;
641 	struct pm2fb_softc *sc = scr->scr_cookie;
642 	int x, y, wi, he;
643 
644 	wi = ri->ri_font->fontwidth;
645 	he = ri->ri_font->fontheight;
646 
647 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
648 		x = ri->ri_ccol * wi + ri->ri_xorigin;
649 		y = ri->ri_crow * he + ri->ri_yorigin;
650 		if (ri->ri_flg & RI_CURSOR) {
651 			pm2fb_bitblt(sc, x, y, x, y, wi, he, 12);
652 			ri->ri_flg &= ~RI_CURSOR;
653 		}
654 		ri->ri_crow = row;
655 		ri->ri_ccol = col;
656 		if (on) {
657 			x = ri->ri_ccol * wi + ri->ri_xorigin;
658 			y = ri->ri_crow * he + ri->ri_yorigin;
659 			pm2fb_bitblt(sc, x, y, x, y, wi, he, 12);
660 			ri->ri_flg |= RI_CURSOR;
661 		}
662 	} else {
663 		scr->scr_ri.ri_crow = row;
664 		scr->scr_ri.ri_ccol = col;
665 		scr->scr_ri.ri_flg &= ~RI_CURSOR;
666 	}
667 
668 }
669 
670 static void
671 pm2fb_putchar(void *cookie, int row, int col, u_int c, long attr)
672 {
673 	struct rasops_info *ri = cookie;
674 	struct wsdisplay_font *font = PICK_FONT(ri, c);
675 	struct vcons_screen *scr = ri->ri_hw;
676 	struct pm2fb_softc *sc = scr->scr_cookie;
677 	uint32_t mode;
678 
679 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
680 		void *data;
681 		uint32_t fg, bg;
682 		int uc, i;
683 		int x, y, wi, he;
684 
685 		wi = font->fontwidth;
686 		he = font->fontheight;
687 
688 		if (!CHAR_IN_FONT(c, font))
689 			return;
690 		bg = ri->ri_devcmap[(attr >> 16) & 0xf];
691 		fg = ri->ri_devcmap[(attr >> 24) & 0xf];
692 		x = ri->ri_xorigin + col * wi;
693 		y = ri->ri_yorigin + row * he;
694 		if (c == 0x20) {
695 			pm2fb_rectfill(sc, x, y, wi, he, bg);
696 		} else {
697 			uc = c - font->firstchar;
698 			data = (uint8_t *)font->data + uc * ri->ri_fontscale;
699 
700 			mode = PM2RM_MASK_MIRROR;
701 			switch (ri->ri_font->stride) {
702 				case 1:
703 					mode |= 3 << 7;
704 					break;
705 				case 2:
706 					mode |= 2 << 7;
707 					break;
708 			}
709 
710 			pm2fb_wait(sc, 8);
711 
712 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
713 			    PM2_RE_MODE, mode);
714 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
715 			    PM2_RE_CONFIG, PM2RECFG_WRITE_EN);
716 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
717 			    PM2_RE_BLOCK_COLOUR, bg);
718 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
719 			    PM2_RE_RECT_START, (y << 16) | x);
720 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
721 			    PM2_RE_RECT_SIZE, (he << 16) | wi);
722 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
723 			    PM2_RE_RENDER,
724 			    PM2RE_RECTANGLE |
725 			    PM2RE_INC_X | PM2RE_INC_Y | PM2RE_FASTFILL);
726 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
727 			    PM2_RE_BLOCK_COLOUR, fg);
728 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
729 			    PM2_RE_RENDER,
730 			    PM2RE_RECTANGLE | PM2RE_SYNC_ON_MASK |
731 			    PM2RE_INC_X | PM2RE_INC_Y | PM2RE_FASTFILL);
732 
733 			pm2fb_wait(sc, he);
734 			switch (ri->ri_font->stride) {
735 			case 1: {
736 				uint8_t *data8 = data;
737 				uint32_t reg;
738 				for (i = 0; i < he; i++) {
739 					reg = *data8;
740 					bus_space_write_4(sc->sc_memt,
741 					    sc->sc_regh,
742 					    PM2_RE_BITMASK, reg);
743 					data8++;
744 				}
745 				break;
746 				}
747 			case 2: {
748 				uint16_t *data16 = data;
749 				uint32_t reg;
750 				for (i = 0; i < he; i++) {
751 					reg = *data16;
752 					bus_space_write_4(sc->sc_memt,
753 					    sc->sc_regh,
754 					    PM2_RE_BITMASK, reg);
755 					data16++;
756 				}
757 				break;
758 			}
759 			}
760 		}
761 	}
762 }
763 
764 static void
765 pm2fb_copycols(void *cookie, int row, int srccol, int dstcol, int ncols)
766 {
767 	struct rasops_info *ri = cookie;
768 	struct vcons_screen *scr = ri->ri_hw;
769 	struct pm2fb_softc *sc = scr->scr_cookie;
770 	int32_t xs, xd, y, width, height;
771 
772 	if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
773 		xs = ri->ri_xorigin + ri->ri_font->fontwidth * srccol;
774 		xd = ri->ri_xorigin + ri->ri_font->fontwidth * dstcol;
775 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
776 		width = ri->ri_font->fontwidth * ncols;
777 		height = ri->ri_font->fontheight;
778 		pm2fb_bitblt(sc, xs, y, xd, y, width, height, 3);
779 	}
780 }
781 
782 static void
783 pm2fb_erasecols(void *cookie, int row, int startcol, int ncols, long fillattr)
784 {
785 	struct rasops_info *ri = cookie;
786 	struct vcons_screen *scr = ri->ri_hw;
787 	struct pm2fb_softc *sc = scr->scr_cookie;
788 	int32_t x, y, width, height, fg, bg, ul;
789 
790 	if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
791 		x = ri->ri_xorigin + ri->ri_font->fontwidth * startcol;
792 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
793 		width = ri->ri_font->fontwidth * ncols;
794 		height = ri->ri_font->fontheight;
795 		rasops_unpack_attr(fillattr, &fg, &bg, &ul);
796 
797 		pm2fb_rectfill(sc, x, y, width, height, ri->ri_devcmap[bg]);
798 	}
799 }
800 
801 static void
802 pm2fb_copyrows(void *cookie, int srcrow, int dstrow, int nrows)
803 {
804 	struct rasops_info *ri = cookie;
805 	struct vcons_screen *scr = ri->ri_hw;
806 	struct pm2fb_softc *sc = scr->scr_cookie;
807 	int32_t x, ys, yd, width, height;
808 
809 	if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
810 		x = ri->ri_xorigin;
811 		ys = ri->ri_yorigin + ri->ri_font->fontheight * srcrow;
812 		yd = ri->ri_yorigin + ri->ri_font->fontheight * dstrow;
813 		width = ri->ri_emuwidth;
814 		height = ri->ri_font->fontheight*nrows;
815 		pm2fb_bitblt(sc, x, ys, x, yd, width, height, 3);
816 	}
817 }
818 
819 static void
820 pm2fb_eraserows(void *cookie, int row, int nrows, long fillattr)
821 {
822 	struct rasops_info *ri = cookie;
823 	struct vcons_screen *scr = ri->ri_hw;
824 	struct pm2fb_softc *sc = scr->scr_cookie;
825 	int32_t x, y, width, height, fg, bg, ul;
826 
827 	if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
828 		x = ri->ri_xorigin;
829 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
830 		width = ri->ri_emuwidth;
831 		height = ri->ri_font->fontheight * nrows;
832 		rasops_unpack_attr(fillattr, &fg, &bg, &ul);
833 
834 		pm2fb_rectfill(sc, x, y, width, height, ri->ri_devcmap[bg]);
835 	}
836 }
837 
838