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