1 /*	$NetBSD: pm2fb.c,v 1.28 2015/09/16 16:52:54 macallan Exp $	*/
2 
3 /*
4  * Copyright (c) 2009, 2012 Michael Lorenz
5  * 		 2014 Naruaki Etomi
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 /*
30  * A console driver for Permedia 2 graphics controllers
31  */
32 
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: pm2fb.c,v 1.28 2015/09/16 16:52:54 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 #include <sys/atomic.h>
44 
45 #include <dev/videomode/videomode.h>
46 
47 #include <dev/pci/pcivar.h>
48 #include <dev/pci/pcireg.h>
49 #include <dev/pci/pcidevs.h>
50 #include <dev/pci/pciio.h>
51 #include <dev/pci/pm2reg.h>
52 
53 #include <dev/wscons/wsdisplayvar.h>
54 #include <dev/wscons/wsconsio.h>
55 #include <dev/wsfont/wsfont.h>
56 #include <dev/rasops/rasops.h>
57 #include <dev/wscons/wsdisplay_vconsvar.h>
58 #include <dev/wscons/wsdisplay_glyphcachevar.h>
59 #include <dev/pci/wsdisplay_pci.h>
60 
61 #include <dev/i2c/i2cvar.h>
62 #include <dev/i2c/i2c_bitbang.h>
63 #include <dev/i2c/ddcvar.h>
64 #include <dev/videomode/videomode.h>
65 #include <dev/videomode/edidvar.h>
66 #include <dev/videomode/edidreg.h>
67 
68 #include "opt_pm2fb.h"
69 
70 #ifdef PM2FB_DEBUG
71 #define DPRINTF aprint_error
72 #else
73 #define DPRINTF while (0) printf
74 #endif
75 
76 #if BYTE_ORDER == LITTLE_ENDIAN
77 /*
78  * XXX
79  * A temporary workaround for unaligned blits on little endian hardware.
80  * This makes pm2fb_bitblt() work well on little endian hardware to get
81  * scrolling right, but not much more. Unaligned blits ( as in, where the lower
82  * 2 bits of the source and destination X coordinates don't match ) are still
83  * wrong so the glyph cache is also disabled.
84  */
85 #define BITBLT_LE_WORKAROUND
86 #endif
87 
88 struct pm2fb_softc {
89 	device_t sc_dev;
90 
91 	pci_chipset_tag_t sc_pc;
92 	pcitag_t sc_pcitag;
93 
94 	bus_space_tag_t sc_memt;
95 	bus_space_tag_t sc_iot;
96 
97 	bus_space_handle_t sc_regh;
98 	bus_addr_t sc_fb, sc_reg;
99 	bus_size_t sc_fbsize, sc_regsize;
100 
101 	int sc_width, sc_height, sc_depth, sc_stride;
102 	int sc_locked;
103 	struct vcons_screen sc_console_screen;
104 	struct wsscreen_descr sc_defaultscreen_descr;
105 	const struct wsscreen_descr *sc_screens[1];
106 	struct wsscreen_list sc_screenlist;
107 	struct vcons_data vd;
108 	int sc_mode;
109 	u_char sc_cmap_red[256];
110 	u_char sc_cmap_green[256];
111 	u_char sc_cmap_blue[256];
112 	/* engine stuff */
113 	uint32_t sc_pprod;
114 	int sc_is_pm2;
115 	/* i2c stuff */
116 	struct i2c_controller sc_i2c;
117 	uint8_t sc_edid_data[128];
118 	struct edid_info sc_ei;
119 	const struct videomode *sc_videomode;
120 	glyphcache sc_gc;
121 };
122 
123 static int	pm2fb_match(device_t, cfdata_t, void *);
124 static void	pm2fb_attach(device_t, device_t, void *);
125 
126 CFATTACH_DECL_NEW(pm2fb, sizeof(struct pm2fb_softc),
127     pm2fb_match, pm2fb_attach, NULL, NULL);
128 
129 extern const u_char rasops_cmap[768];
130 
131 static int	pm2fb_ioctl(void *, void *, u_long, void *, int,
132 			     struct lwp *);
133 static paddr_t	pm2fb_mmap(void *, void *, off_t, int);
134 static void	pm2fb_init_screen(void *, struct vcons_screen *, int, long *);
135 
136 static int	pm2fb_putcmap(struct pm2fb_softc *, struct wsdisplay_cmap *);
137 static int 	pm2fb_getcmap(struct pm2fb_softc *, struct wsdisplay_cmap *);
138 static void	pm2fb_init_palette(struct pm2fb_softc *);
139 static int 	pm2fb_putpalreg(struct pm2fb_softc *, uint8_t, uint8_t,
140 			    uint8_t, uint8_t);
141 
142 static void	pm2fb_init(struct pm2fb_softc *);
143 static inline void pm2fb_wait(struct pm2fb_softc *, int);
144 static void	pm2fb_flush_engine(struct pm2fb_softc *);
145 static void	pm2fb_rectfill(struct pm2fb_softc *, int, int, int, int,
146 			    uint32_t);
147 static void	pm2fb_rectfill_a(void *, int, int, int, int, long);
148 static void	pm2fb_bitblt(void *, int, int, int, int, int, int, int);
149 
150 static void	pm2fb_cursor(void *, int, int, int);
151 static void	pm2fb_putchar(void *, int, int, u_int, long);
152 static void	pm2fb_putchar_aa(void *, int, int, u_int, long);
153 static void	pm2fb_copycols(void *, int, int, int, int);
154 static void	pm2fb_erasecols(void *, int, int, int, long);
155 static void	pm2fb_copyrows(void *, int, int, int);
156 static void	pm2fb_eraserows(void *, int, int, long);
157 
158 struct wsdisplay_accessops pm2fb_accessops = {
159 	pm2fb_ioctl,
160 	pm2fb_mmap,
161 	NULL,	/* alloc_screen */
162 	NULL,	/* free_screen */
163 	NULL,	/* show_screen */
164 	NULL, 	/* load_font */
165 	NULL,	/* pollc */
166 	NULL	/* scroll */
167 };
168 
169 /* I2C glue */
170 static int pm2fb_i2c_acquire_bus(void *, int);
171 static void pm2fb_i2c_release_bus(void *, int);
172 static int pm2fb_i2c_send_start(void *, int);
173 static int pm2fb_i2c_send_stop(void *, int);
174 static int pm2fb_i2c_initiate_xfer(void *, i2c_addr_t, int);
175 static int pm2fb_i2c_read_byte(void *, uint8_t *, int);
176 static int pm2fb_i2c_write_byte(void *, uint8_t, int);
177 
178 /* I2C bitbang glue */
179 static void pm2fb_i2cbb_set_bits(void *, uint32_t);
180 static void pm2fb_i2cbb_set_dir(void *, uint32_t);
181 static uint32_t pm2fb_i2cbb_read(void *);
182 
183 static void pm2_setup_i2c(struct pm2fb_softc *);
184 
185 static const struct i2c_bitbang_ops pm2fb_i2cbb_ops = {
186 	pm2fb_i2cbb_set_bits,
187 	pm2fb_i2cbb_set_dir,
188 	pm2fb_i2cbb_read,
189 	{
190 		PM2_DD_SDA_IN,
191 		PM2_DD_SCL_IN,
192 		0,
193 		0
194 	}
195 };
196 
197 /* mode setting stuff */
198 static int pm2fb_set_pll(struct pm2fb_softc *, int);
199 static int pm2vfb_set_pll(struct pm2fb_softc *, int);
200 static uint8_t pm2fb_read_dac(struct pm2fb_softc *, int);
201 static void pm2fb_write_dac(struct pm2fb_softc *, int, uint8_t);
202 static void pm2fb_set_mode(struct pm2fb_softc *, const struct videomode *);
203 
204 const struct {
205 	int vendor;
206 	int product;
207 	int flags;
208 } pm2fb_pci_devices[] = {
209 	{
210 		PCI_VENDOR_3DLABS,
211 		PCI_PRODUCT_3DLABS_PERMEDIA2V,
212 		0
213 	},
214 	{
215 		PCI_VENDOR_TI,
216 		PCI_PRODUCT_TI_TVP4020,
217 		1
218 	},
219 	{
220 		0,
221 		0,
222 		0
223 	}
224 };
225 
226 /* this table is from xf86-video-glint */
227 #define PARTPROD(a,b,c) (((a)<<6) | ((b)<<3) | (c))
228 int partprodPermedia[] = {
229 	-1,
230 	PARTPROD(0,0,1), PARTPROD(0,1,1), PARTPROD(1,1,1), PARTPROD(1,1,2),
231 	PARTPROD(1,2,2), PARTPROD(2,2,2), PARTPROD(1,2,3), PARTPROD(2,2,3),
232 	PARTPROD(1,3,3), PARTPROD(2,3,3), PARTPROD(1,2,4), PARTPROD(3,3,3),
233 	PARTPROD(1,3,4), PARTPROD(2,3,4),              -1, PARTPROD(3,3,4),
234 	PARTPROD(1,4,4), PARTPROD(2,4,4),              -1, PARTPROD(3,4,4),
235 	             -1, PARTPROD(2,3,5),              -1, PARTPROD(4,4,4),
236 	PARTPROD(1,4,5), PARTPROD(2,4,5), PARTPROD(3,4,5),              -1,
237 	             -1,              -1,              -1, PARTPROD(4,4,5),
238 	PARTPROD(1,5,5), PARTPROD(2,5,5),              -1, PARTPROD(3,5,5),
239 	             -1,              -1,              -1, PARTPROD(4,5,5),
240 	             -1,              -1,              -1, PARTPROD(3,4,6),
241 	             -1,              -1,              -1, PARTPROD(5,5,5),
242 	PARTPROD(1,5,6), PARTPROD(2,5,6),              -1, PARTPROD(3,5,6),
243 	             -1,              -1,              -1, PARTPROD(4,5,6),
244 	             -1,              -1,              -1,              -1,
245 	             -1,              -1,              -1, PARTPROD(5,5,6),
246 	             -1,              -1,              -1,              -1,
247 	             -1,              -1,              -1,              -1,
248 	             -1,              -1,              -1,              -1,
249 	             -1,              -1,              -1,              -1,
250 	             -1,              -1,              -1,              -1,
251 	             -1,              -1,              -1,              -1,
252 	             -1,              -1,              -1,              -1,
253 	             -1,              -1,              -1,              -1,
254 	             -1,              -1,              -1,              -1,
255 	             -1,              -1,              -1,              -1,
256 	             -1,              -1,              -1,              -1,
257 	             -1,              -1,              -1,              -1,
258 	             -1,              -1,              -1,              -1,
259 	             -1,              -1,              -1,              -1,
260 	             -1,              -1,              -1,              -1,
261 	             -1,              -1,              -1,              -1,
262 		     0};
263 
264 static inline void
pm2fb_wait(struct pm2fb_softc * sc,int slots)265 pm2fb_wait(struct pm2fb_softc *sc, int slots)
266 {
267 	uint32_t reg;
268 
269 	do {
270 		reg = bus_space_read_4(sc->sc_memt, sc->sc_regh,
271 			PM2_INPUT_FIFO_SPACE);
272 	} while (reg <= slots);
273 }
274 
275 static void
pm2fb_flush_engine(struct pm2fb_softc * sc)276 pm2fb_flush_engine(struct pm2fb_softc *sc)
277 {
278 
279 	pm2fb_wait(sc, 2);
280 
281 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_FILTER_MODE,
282 	    PM2FLT_PASS_SYNC);
283 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_SYNC, 0);
284 	do {
285 		while (bus_space_read_4(sc->sc_memt, sc->sc_regh,
286 			PM2_OUTPUT_FIFO_WORDS) == 0);
287 	} while (bus_space_read_4(sc->sc_memt, sc->sc_regh, PM2_OUTPUT_FIFO) !=
288 	    PM2_SYNC_TAG);
289 }
290 
291 static int
pm2fb_match(device_t parent,cfdata_t match,void * aux)292 pm2fb_match(device_t parent, cfdata_t match, void *aux)
293 {
294 	struct pci_attach_args *pa = (struct pci_attach_args *)aux;
295 	int i;
296 
297 	if (PCI_CLASS(pa->pa_class) != PCI_CLASS_DISPLAY)
298 		return 0;
299 
300 	for (i = 0; pm2fb_pci_devices[i].vendor; i++) {
301 		if ((PCI_VENDOR(pa->pa_id) == pm2fb_pci_devices[i].vendor &&
302 		     PCI_PRODUCT(pa->pa_id) == pm2fb_pci_devices[i].product))
303 			return 100;
304 	}
305 
306 	return (0);
307 }
308 
309 static void
pm2fb_attach(device_t parent,device_t self,void * aux)310 pm2fb_attach(device_t parent, device_t self, void *aux)
311 {
312 	struct pm2fb_softc	*sc = device_private(self);
313 	struct pci_attach_args	*pa = aux;
314 	struct rasops_info	*ri;
315 	struct wsemuldisplaydev_attach_args aa;
316 	prop_dictionary_t	dict;
317 	unsigned long		defattr;
318 	bool			is_console = FALSE;
319 	uint32_t		flags;
320 	int			i;
321 
322 	sc->sc_pc = pa->pa_pc;
323 	sc->sc_pcitag = pa->pa_tag;
324 	sc->sc_memt = pa->pa_memt;
325 	sc->sc_iot = pa->pa_iot;
326 	sc->sc_dev = self;
327 
328 	for (i = 0; pm2fb_pci_devices[i].vendor; i++) {
329 		if (PCI_PRODUCT(pa->pa_id) == pm2fb_pci_devices[i].product) {
330 			sc->sc_is_pm2 = pm2fb_pci_devices[i].flags ;
331 			break;
332 		}
333 	}
334 
335 	pci_aprint_devinfo(pa, NULL);
336 
337 	/*
338 	 * fill in parameters from properties
339 	 * if we can't get a usable mode via DDC2 we'll use this to pick one,
340 	 * which is why we fill them in with some conservative values that
341 	 * hopefully work as a last resort
342 	 */
343 	dict = device_properties(self);
344 	if (!prop_dictionary_get_uint32(dict, "width", &sc->sc_width)) {
345 		aprint_error("%s: no width property\n", device_xname(self));
346 		sc->sc_width = 1024;
347 	}
348 	if (!prop_dictionary_get_uint32(dict, "height", &sc->sc_height)) {
349 		aprint_error("%s: no height property\n", device_xname(self));
350 		sc->sc_height = 768;
351 	}
352 	if (!prop_dictionary_get_uint32(dict, "depth", &sc->sc_depth)) {
353 		aprint_error("%s: no depth property\n", device_xname(self));
354 		sc->sc_depth = 8;
355 	}
356 
357 	/*
358 	 * don't look at the linebytes property - The Raptor firmware lies
359 	 * about it. Get it from width * depth >> 3 instead.
360 	 */
361 
362 	sc->sc_stride = sc->sc_width * (sc->sc_depth >> 3);
363 
364 	prop_dictionary_get_bool(dict, "is_console", &is_console);
365 
366 	pci_mapreg_info(pa->pa_pc, pa->pa_tag, 0x14, PCI_MAPREG_TYPE_MEM,
367 	    &sc->sc_fb, &sc->sc_fbsize, &flags);
368 
369 	if (pci_mapreg_map(pa, 0x10, PCI_MAPREG_TYPE_MEM, 0,
370 	    &sc->sc_memt, &sc->sc_regh, &sc->sc_reg, &sc->sc_regsize)) {
371 		aprint_error("%s: failed to map registers.\n",
372 		    device_xname(sc->sc_dev));
373 	}
374 
375 	/*
376 	 * XXX yeah, casting the fb address to uint32_t is formally wrong
377 	 * but as far as I know there are no PM2 with 64bit BARs
378 	 */
379 	aprint_normal("%s: %d MB aperture at 0x%08x\n", device_xname(self),
380 	    (int)(sc->sc_fbsize >> 20), (uint32_t)sc->sc_fb);
381 
382 	sc->sc_defaultscreen_descr = (struct wsscreen_descr){
383 		"default",
384 		0, 0,
385 		NULL,
386 		8, 16,
387 		WSSCREEN_WSCOLORS | WSSCREEN_HILIT,
388 		NULL
389 	};
390 	sc->sc_screens[0] = &sc->sc_defaultscreen_descr;
391 	sc->sc_screenlist = (struct wsscreen_list){1, sc->sc_screens};
392 	sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
393 	sc->sc_locked = 0;
394 
395 	pm2_setup_i2c(sc);
396 
397 	vcons_init(&sc->vd, sc, &sc->sc_defaultscreen_descr,
398 	    &pm2fb_accessops);
399 	sc->vd.init_screen = pm2fb_init_screen;
400 
401 	/* init engine here */
402 	pm2fb_init(sc);
403 
404 	ri = &sc->sc_console_screen.scr_ri;
405 
406 	sc->sc_gc.gc_bitblt = pm2fb_bitblt;
407 	sc->sc_gc.gc_rectfill = pm2fb_rectfill_a;
408 	sc->sc_gc.gc_blitcookie = sc;
409 	sc->sc_gc.gc_rop = 3;
410 
411 #ifdef PM2FB_DEBUG
412 	/*
413 	 * leave some room at the bottom of the screen for various blitter
414 	 * tests and in order to make the glyph cache visible
415 	 */
416 	sc->sc_height -= 200;
417 #endif
418 
419 	if (is_console) {
420 		vcons_init_screen(&sc->vd, &sc->sc_console_screen, 1,
421 		    &defattr);
422 		sc->sc_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
423 
424 		pm2fb_rectfill(sc, 0, 0, sc->sc_width, sc->sc_height,
425 		    ri->ri_devcmap[(defattr >> 16) & 0xff]);
426 		sc->sc_defaultscreen_descr.textops = &ri->ri_ops;
427 		sc->sc_defaultscreen_descr.capabilities = ri->ri_caps;
428 		sc->sc_defaultscreen_descr.nrows = ri->ri_rows;
429 		sc->sc_defaultscreen_descr.ncols = ri->ri_cols;
430 
431 		glyphcache_init(&sc->sc_gc, sc->sc_height + 5,
432 			min(2047, (sc->sc_fbsize / sc->sc_stride))
433 			 - sc->sc_height - 5,
434 			sc->sc_width,
435 			ri->ri_font->fontwidth,
436 			ri->ri_font->fontheight,
437 			defattr);
438 		wsdisplay_cnattach(&sc->sc_defaultscreen_descr, ri, 0, 0,
439 		    defattr);
440 		vcons_replay_msgbuf(&sc->sc_console_screen);
441 	} else {
442 		if (sc->sc_console_screen.scr_ri.ri_rows == 0) {
443 			/* do some minimal setup to avoid weirdnesses later */
444 			vcons_init_screen(&sc->vd, &sc->sc_console_screen, 1,
445 			   &defattr);
446 		} else
447 			(*ri->ri_ops.allocattr)(ri, 0, 0, 0, &defattr);
448 		glyphcache_init(&sc->sc_gc, sc->sc_height + 5,
449 			   min(2047, (sc->sc_fbsize / sc->sc_stride))
450 			    - sc->sc_height - 5,
451 			   sc->sc_width,
452 			   ri->ri_font->fontwidth,
453 			   ri->ri_font->fontheight,
454 			   defattr);
455 	}
456 
457 	pm2fb_init_palette(sc);
458 
459 	aa.console = is_console;
460 	aa.scrdata = &sc->sc_screenlist;
461 	aa.accessops = &pm2fb_accessops;
462 	aa.accesscookie = &sc->vd;
463 
464 	config_found(sc->sc_dev, &aa, wsemuldisplaydevprint);
465 
466 #ifdef PM2FB_DEBUG
467 	/*
468 	 * draw a pattern to check if pm2fb_bitblt() gets the alignment stuff
469 	 * right
470 	 */
471 	pm2fb_rectfill(sc, 0, sc->sc_height, sc->sc_width, 200, 0xffffffff);
472 	pm2fb_rectfill(sc, 0, sc->sc_height, 300, 10, 0);
473 	pm2fb_rectfill(sc, 10, sc->sc_height, 200, 10, 0xe0e0e0e0);
474 	for (i = 1; i < 20; i++) {
475 		pm2fb_bitblt(sc, 0, sc->sc_height,
476 			i, sc->sc_height + 10 * i,
477 			300, 10, 3);
478 		pm2fb_bitblt(sc, i, sc->sc_height,
479 			400, sc->sc_height + 10 * i,
480 			300, 10, 3);
481 	}
482 #endif
483 }
484 
485 static int
pm2fb_ioctl(void * v,void * vs,u_long cmd,void * data,int flag,struct lwp * l)486 pm2fb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
487 	struct lwp *l)
488 {
489 	struct vcons_data *vd = v;
490 	struct pm2fb_softc *sc = vd->cookie;
491 	struct wsdisplay_fbinfo *wdf;
492 	struct vcons_screen *ms = vd->active;
493 
494 	switch (cmd) {
495 	case WSDISPLAYIO_GTYPE:
496 		*(u_int *)data = WSDISPLAY_TYPE_PCIMISC;
497 		return 0;
498 
499 	/* PCI config read/write passthrough. */
500 	case PCI_IOC_CFGREAD:
501 	case PCI_IOC_CFGWRITE:
502 		return pci_devioctl(sc->sc_pc, sc->sc_pcitag,
503 		    cmd, data, flag, l);
504 
505 	case WSDISPLAYIO_GET_BUSID:
506 		return wsdisplayio_busid_pci(sc->sc_dev, sc->sc_pc,
507 		    sc->sc_pcitag, data);
508 
509 	case WSDISPLAYIO_GINFO:
510 		if (ms == NULL)
511 			return ENODEV;
512 		wdf = (void *)data;
513 		wdf->height = ms->scr_ri.ri_height;
514 		wdf->width = ms->scr_ri.ri_width;
515 		wdf->depth = ms->scr_ri.ri_depth;
516 		wdf->cmsize = 256;
517 		return 0;
518 
519 	case WSDISPLAYIO_GETCMAP:
520 		return pm2fb_getcmap(sc,
521 		    (struct wsdisplay_cmap *)data);
522 
523 	case WSDISPLAYIO_PUTCMAP:
524 		return pm2fb_putcmap(sc,
525 		    (struct wsdisplay_cmap *)data);
526 
527 	case WSDISPLAYIO_LINEBYTES:
528 		*(u_int *)data = sc->sc_stride;
529 		return 0;
530 
531 	case WSDISPLAYIO_SMODE: {
532 		int new_mode = *(int*)data;
533 		if (new_mode != sc->sc_mode) {
534 			sc->sc_mode = new_mode;
535 			if(new_mode == WSDISPLAYIO_MODE_EMUL) {
536 				/* first set the video mode */
537 				if (sc->sc_videomode != NULL) {
538 					pm2fb_set_mode(sc, sc->sc_videomode);
539 				}
540 				/* then initialize the drawing engine */
541 				pm2fb_init(sc);
542 				pm2fb_init_palette(sc);
543 				/* clean out the glyph cache */
544 				glyphcache_wipe(&sc->sc_gc);
545 				/* and redraw everything */
546 				vcons_redraw_screen(ms);
547 			} else
548 				pm2fb_flush_engine(sc);
549 		}
550 		}
551 		return 0;
552 	case WSDISPLAYIO_GET_EDID: {
553 		struct wsdisplayio_edid_info *d = data;
554 		d->data_size = 128;
555 		if (d->buffer_size < 128)
556 			return EAGAIN;
557 		return copyout(sc->sc_edid_data, d->edid_data, 128);
558 	}
559 
560 	case WSDISPLAYIO_GET_FBINFO: {
561 		struct wsdisplayio_fbinfo *fbi = data;
562 		return wsdisplayio_get_fbinfo(&ms->scr_ri, fbi);
563 	}
564 	}
565 	return EPASSTHROUGH;
566 }
567 
568 static paddr_t
pm2fb_mmap(void * v,void * vs,off_t offset,int prot)569 pm2fb_mmap(void *v, void *vs, off_t offset, int prot)
570 {
571 	struct vcons_data *vd = v;
572 	struct pm2fb_softc *sc = vd->cookie;
573 	paddr_t pa;
574 
575 	/* 'regular' framebuffer mmap()ing */
576 	if (offset < sc->sc_fbsize) {
577 		pa = bus_space_mmap(sc->sc_memt, sc->sc_fb + offset, 0, prot,
578 		    BUS_SPACE_MAP_LINEAR);
579 		return pa;
580 	}
581 
582 	/*
583 	 * restrict all other mappings to processes with superuser privileges
584 	 * or the kernel itself
585 	 */
586 	if (kauth_authorize_machdep(kauth_cred_get(),
587 	    KAUTH_MACHDEP_UNMANAGEDMEM,
588 	    NULL, NULL, NULL, NULL) != 0) {
589 		aprint_normal("%s: mmap() rejected.\n",
590 		    device_xname(sc->sc_dev));
591 		return -1;
592 	}
593 
594 	if ((offset >= sc->sc_fb) && (offset < (sc->sc_fb + sc->sc_fbsize))) {
595 		pa = bus_space_mmap(sc->sc_memt, offset, 0, prot,
596 		    BUS_SPACE_MAP_LINEAR);
597 		return pa;
598 	}
599 
600 	if ((offset >= sc->sc_reg) &&
601 	    (offset < (sc->sc_reg + sc->sc_regsize))) {
602 		pa = bus_space_mmap(sc->sc_memt, offset, 0, prot,
603 		    BUS_SPACE_MAP_LINEAR);
604 		return pa;
605 	}
606 
607 #ifdef PCI_MAGIC_IO_RANGE
608 	/* allow mapping of IO space */
609 	if ((offset >= PCI_MAGIC_IO_RANGE) &&
610 	    (offset < PCI_MAGIC_IO_RANGE + 0x10000)) {
611 		pa = bus_space_mmap(sc->sc_iot, offset - PCI_MAGIC_IO_RANGE,
612 		    0, prot, BUS_SPACE_MAP_LINEAR);
613 		return pa;
614 	}
615 #endif
616 
617 	return -1;
618 }
619 
620 static void
pm2fb_init_screen(void * cookie,struct vcons_screen * scr,int existing,long * defattr)621 pm2fb_init_screen(void *cookie, struct vcons_screen *scr,
622     int existing, long *defattr)
623 {
624 	struct pm2fb_softc *sc = cookie;
625 	struct rasops_info *ri = &scr->scr_ri;
626 
627 	ri->ri_depth = sc->sc_depth;
628 	ri->ri_width = sc->sc_width;
629 	ri->ri_height = sc->sc_height;
630 	ri->ri_stride = sc->sc_stride;
631 	ri->ri_flg = RI_CENTER;
632 	if (sc->sc_depth == 8)
633 		ri->ri_flg |= RI_8BIT_IS_RGB | RI_ENABLE_ALPHA;
634 
635 	rasops_init(ri, 0, 0);
636 	ri->ri_caps = WSSCREEN_WSCOLORS | WSSCREEN_UNDERLINE;
637 
638 	rasops_reconfig(ri, sc->sc_height / ri->ri_font->fontheight,
639 		    sc->sc_width / ri->ri_font->fontwidth);
640 
641 	ri->ri_hw = scr;
642 	ri->ri_ops.copyrows = pm2fb_copyrows;
643 	ri->ri_ops.copycols = pm2fb_copycols;
644 	ri->ri_ops.cursor = pm2fb_cursor;
645 	ri->ri_ops.eraserows = pm2fb_eraserows;
646 	ri->ri_ops.erasecols = pm2fb_erasecols;
647 	if (FONT_IS_ALPHA(ri->ri_font)) {
648 		ri->ri_ops.putchar = pm2fb_putchar_aa;
649 	} else
650 		ri->ri_ops.putchar = pm2fb_putchar;
651 }
652 
653 static int
pm2fb_putcmap(struct pm2fb_softc * sc,struct wsdisplay_cmap * cm)654 pm2fb_putcmap(struct pm2fb_softc *sc, struct wsdisplay_cmap *cm)
655 {
656 	u_char *r, *g, *b;
657 	u_int index = cm->index;
658 	u_int count = cm->count;
659 	int i, error;
660 	u_char rbuf[256], gbuf[256], bbuf[256];
661 
662 #ifdef PM2FB_DEBUG
663 	aprint_debug("putcmap: %d %d\n",index, count);
664 #endif
665 	if (cm->index >= 256 || cm->count > 256 ||
666 	    (cm->index + cm->count) > 256)
667 		return EINVAL;
668 	error = copyin(cm->red, &rbuf[index], count);
669 	if (error)
670 		return error;
671 	error = copyin(cm->green, &gbuf[index], count);
672 	if (error)
673 		return error;
674 	error = copyin(cm->blue, &bbuf[index], count);
675 	if (error)
676 		return error;
677 
678 	memcpy(&sc->sc_cmap_red[index], &rbuf[index], count);
679 	memcpy(&sc->sc_cmap_green[index], &gbuf[index], count);
680 	memcpy(&sc->sc_cmap_blue[index], &bbuf[index], count);
681 
682 	r = &sc->sc_cmap_red[index];
683 	g = &sc->sc_cmap_green[index];
684 	b = &sc->sc_cmap_blue[index];
685 
686 	for (i = 0; i < count; i++) {
687 		pm2fb_putpalreg(sc, index, *r, *g, *b);
688 		index++;
689 		r++, g++, b++;
690 	}
691 	return 0;
692 }
693 
694 static int
pm2fb_getcmap(struct pm2fb_softc * sc,struct wsdisplay_cmap * cm)695 pm2fb_getcmap(struct pm2fb_softc *sc, struct wsdisplay_cmap *cm)
696 {
697 	u_int index = cm->index;
698 	u_int count = cm->count;
699 	int error;
700 
701 	if (index >= 255 || count > 256 || index + count > 256)
702 		return EINVAL;
703 
704 	error = copyout(&sc->sc_cmap_red[index],   cm->red,   count);
705 	if (error)
706 		return error;
707 	error = copyout(&sc->sc_cmap_green[index], cm->green, count);
708 	if (error)
709 		return error;
710 	error = copyout(&sc->sc_cmap_blue[index],  cm->blue,  count);
711 	if (error)
712 		return error;
713 
714 	return 0;
715 }
716 
717 static void
pm2fb_init_palette(struct pm2fb_softc * sc)718 pm2fb_init_palette(struct pm2fb_softc *sc)
719 {
720 	struct rasops_info *ri = &sc->sc_console_screen.scr_ri;
721 	int i, j = 0;
722 	uint8_t cmap[768];
723 
724 	rasops_get_cmap(ri, cmap, sizeof(cmap));
725 	for (i = 0; i < 256; i++) {
726 		sc->sc_cmap_red[i] = cmap[j];
727 		sc->sc_cmap_green[i] = cmap[j + 1];
728 		sc->sc_cmap_blue[i] = cmap[j + 2];
729 		pm2fb_putpalreg(sc, i, cmap[j], cmap[j + 1], cmap[j + 2]);
730 		j += 3;
731 	}
732 }
733 
734 static int
pm2fb_putpalreg(struct pm2fb_softc * sc,uint8_t idx,uint8_t r,uint8_t g,uint8_t b)735 pm2fb_putpalreg(struct pm2fb_softc *sc, uint8_t idx, uint8_t r, uint8_t g,
736     uint8_t b)
737 {
738 	bus_space_write_1(sc->sc_memt, sc->sc_regh, PM2_DAC_PAL_WRITE_IDX, idx);
739 	bus_space_write_1(sc->sc_memt, sc->sc_regh, PM2_DAC_DATA, r);
740 	bus_space_write_1(sc->sc_memt, sc->sc_regh, PM2_DAC_DATA, g);
741 	bus_space_write_1(sc->sc_memt, sc->sc_regh, PM2_DAC_DATA, b);
742 	return 0;
743 }
744 
745 static uint8_t
pm2fb_read_dac(struct pm2fb_softc * sc,int reg)746 pm2fb_read_dac(struct pm2fb_softc *sc, int reg)
747 {
748 	if (sc->sc_is_pm2) {
749 		bus_space_write_1(sc->sc_memt, sc->sc_regh,
750 		    PM2_DAC_PAL_WRITE_IDX, reg);
751 		return bus_space_read_1(sc->sc_memt, sc->sc_regh,
752 		    PM2_DAC_INDEX_DATA);
753 	} else {
754 		bus_space_write_1(sc->sc_memt, sc->sc_regh,
755 		    PM2V_DAC_INDEX_LOW, reg & 0xff);
756 		bus_space_write_1(sc->sc_memt, sc->sc_regh,
757 		    PM2V_DAC_INDEX_HIGH, (reg >> 8) & 0xff);
758 		return bus_space_read_1(sc->sc_memt, sc->sc_regh,
759 		    PM2V_DAC_INDEX_DATA);
760 	}
761 }
762 
763 static void
pm2fb_write_dac(struct pm2fb_softc * sc,int reg,uint8_t data)764 pm2fb_write_dac(struct pm2fb_softc *sc, int reg, uint8_t data)
765 {
766 	pm2fb_wait(sc, 3);
767 	if (sc->sc_is_pm2) {
768 		pm2fb_wait(sc, 2);
769 		bus_space_write_1(sc->sc_memt, sc->sc_regh,
770 		    PM2_DAC_PAL_WRITE_IDX, reg);
771 		bus_space_write_1(sc->sc_memt, sc->sc_regh,
772 		    PM2_DAC_INDEX_DATA, data);
773 	} else {
774 		pm2fb_wait(sc, 3);
775 		bus_space_write_1(sc->sc_memt, sc->sc_regh,
776 		    PM2V_DAC_INDEX_LOW, reg & 0xff);
777 		bus_space_write_1(sc->sc_memt, sc->sc_regh,
778 		    PM2V_DAC_INDEX_HIGH, (reg >> 8) & 0xff);
779 		bus_space_write_1(sc->sc_memt, sc->sc_regh,
780 		    PM2V_DAC_INDEX_DATA, data);
781 	}
782 }
783 
784 static void
pm2fb_init(struct pm2fb_softc * sc)785 pm2fb_init(struct pm2fb_softc *sc)
786 {
787 	pm2fb_flush_engine(sc);
788 
789 	pm2fb_wait(sc, 9);
790 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_SCREEN_BASE, 0);
791 	/* set aperture endianness */
792 #if BYTE_ORDER == BIG_ENDIAN
793 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_APERTURE1_CONTROL,
794 		PM2_AP_BYTESWAP | PM2_AP_HALFWORDSWAP);
795 #else
796 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_APERTURE1_CONTROL, 0);
797 #endif
798 #if 0
799 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_BYPASS_MASK,
800 		0xffffffff);
801 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_FB_WRITE_MASK,
802 		0xffffffff);
803 #endif
804 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_HW_WRITEMASK,
805 		0xffffffff);
806 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_SW_WRITEMASK,
807 		0xffffffff);
808 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_WRITE_MODE,
809 		PM2WM_WRITE_EN);
810 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_SCREENSIZE,
811 	    (sc->sc_height << 16) | sc->sc_width);
812 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_SCISSOR_MODE,
813 	    PM2SC_SCREEN_EN);
814 	pm2fb_wait(sc, 8);
815 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_DITHER_MODE, 0);
816 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_ALPHA_MODE, 0);
817 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_DDA_MODE, 0);
818 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_TEX_COLOUR_MODE, 0);
819 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_TEX_ADDRESS_MODE, 0);
820 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_TEX_READ_MODE, 0);
821 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_TEX_LUT_MODE, 0);
822 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_YUV_MODE, 0);
823 	pm2fb_wait(sc, 8);
824 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_DEPTH_MODE, 0);
825 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_DEPTH, 0);
826 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_STENCIL_MODE, 0);
827 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_STIPPLE_MODE, 0);
828 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_ROP_MODE, 0);
829 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_WINDOW_ORIGIN, 0);
830 #if 0
831 	sc->sc_pprod = bus_space_read_4(sc->sc_memt, sc->sc_regh,
832 	    PM2_FB_READMODE) &
833 	    (PM2FB_PP0_MASK | PM2FB_PP1_MASK | PM2FB_PP2_MASK);
834 #endif
835 	sc->sc_pprod = partprodPermedia[sc->sc_stride >> 5];
836 
837 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_FB_READMODE,
838 	    sc->sc_pprod);
839 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_TEXMAP_FORMAT,
840 	    sc->sc_pprod);
841 	pm2fb_wait(sc, 9);
842 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_DY, 1 << 16);
843 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_DXDOM, 0);
844 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_STARTXDOM, 0);
845 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_STARTXSUB, 0);
846 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_STARTY, 0);
847 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_COUNT, 0);
848 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_SCISSOR_MINYX, 0);
849 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_SCISSOR_MAXYX,
850 	    0x0fff0fff);
851 	/*
852 	 * another scissor we need to disable in order to blit into off-screen
853 	 * memory
854 	 */
855 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_SCREENSIZE,
856 	    0x0fff0fff);
857 
858 	switch(sc->sc_depth) {
859 		case 8:
860 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
861 			    PM2_RE_PIXEL_SIZE, PM2PS_8BIT);
862 			break;
863 		case 16:
864 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
865 			    PM2_RE_PIXEL_SIZE, PM2PS_16BIT);
866 			break;
867 		case 32:
868 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
869 			    PM2_RE_PIXEL_SIZE, PM2PS_32BIT);
870 			break;
871 	}
872 	pm2fb_flush_engine(sc);
873 	DPRINTF("pixel size: %08x\n",
874 	    bus_space_read_4(sc->sc_memt, sc->sc_regh, PM2_RE_PIXEL_SIZE));
875 }
876 
877 static void
pm2fb_rectfill(struct pm2fb_softc * sc,int x,int y,int wi,int he,uint32_t colour)878 pm2fb_rectfill(struct pm2fb_softc *sc, int x, int y, int wi, int he,
879      uint32_t colour)
880 {
881 
882 	pm2fb_wait(sc, 7);
883 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_DDA_MODE, 0);
884 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_MODE, 0);
885 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_CONFIG,
886 	    PM2RECFG_WRITE_EN);
887 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_BLOCK_COLOUR,
888 	    colour);
889 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_RECT_START,
890 	    (y << 16) | x);
891 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_RECT_SIZE,
892 	    (he << 16) | wi);
893 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_RENDER,
894 	    PM2RE_RECTANGLE | PM2RE_INC_X | PM2RE_INC_Y | PM2RE_FASTFILL);
895 }
896 
897 static void
pm2fb_rectfill_a(void * cookie,int x,int y,int wi,int he,long attr)898 pm2fb_rectfill_a(void *cookie, int x, int y, int wi, int he, long attr)
899 {
900 	struct pm2fb_softc *sc = cookie;
901 
902 	pm2fb_rectfill(sc, x, y, wi, he,
903 	    sc->vd.active->scr_ri.ri_devcmap[(attr >> 24 & 0xf)]);
904 }
905 
906 static void
pm2fb_bitblt(void * cookie,int xs,int ys,int xd,int yd,int wi,int he,int rop)907 pm2fb_bitblt(void *cookie, int xs, int ys, int xd, int yd,
908     int wi, int he, int rop)
909 {
910 	struct pm2fb_softc *sc = cookie;
911 	uint32_t dir = 0;
912 	int rxd, rwi, rxdelta;
913 
914 	if (yd <= ys) {
915 		dir |= PM2RE_INC_Y;
916 	}
917 	if (xd <= xs) {
918 		dir |= PM2RE_INC_X;
919 	}
920 	pm2fb_wait(sc, 8);
921 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_DDA_MODE, 0);
922 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_MODE, 0);
923 	if (sc->sc_depth == 8) {
924 		int adjust;
925 		/*
926 		 * use packed mode for some extra speed
927 		 * this copies 32bit quantities even in 8 bit mode, so we need
928 		 * to adjust for cases where the lower two bits in source and
929 		 * destination X don't align, and/or where the width isn't a
930 		 * multiple of 4
931 		 */
932 		if (rop == 3) {
933 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
934 			    PM2_RE_CONFIG,
935 			    PM2RECFG_READ_SRC | PM2RECFG_WRITE_EN |
936 			    PM2RECFG_ROP_EN | PM2RECFG_PACKED | (rop << 6));
937 		} else {
938 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
939 			    PM2_RE_CONFIG,
940 			    PM2RECFG_READ_SRC | PM2RECFG_READ_DST |
941 			    PM2RECFG_WRITE_EN | PM2RECFG_PACKED |
942 			    PM2RECFG_ROP_EN | (rop << 6));
943 		}
944 		rxd = xd >> 2;
945 		rwi = (wi + 7) >> 2;
946 		rxdelta = (xs & 0xffc) - (xd & 0xffc);
947 		/* adjust for non-aligned x */
948 #ifdef BITBLT_LE_WORKAROUND
949 		/* I have no idea why this seems to work */
950 		adjust = 1;
951 #else
952 		adjust = ((xd & 3) - (xs & 3));
953 #endif
954 		bus_space_write_4(sc->sc_memt, sc->sc_regh,
955 		    PM2_RE_PACKEDDATA_LIMIT,
956 		    (xd << 16) | (xd + wi) | (adjust << 29));
957 
958 	} else {
959 		/* we're in 16 or 32bit mode */
960 		if (rop == 3) {
961 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
962 			    PM2_RE_CONFIG,
963 			    PM2RECFG_READ_SRC | PM2RECFG_WRITE_EN |
964 			    PM2RECFG_ROP_EN | PM2RECFG_PACKED | (rop << 6));
965 		} else {
966 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
967 			    PM2_RE_CONFIG,
968 			    PM2RECFG_READ_SRC | PM2RECFG_READ_DST |
969 			    PM2RECFG_WRITE_EN | PM2RECFG_PACKED |
970 			    PM2RECFG_ROP_EN | (rop << 6));
971 		}
972 		rxd = xd;
973 		rwi = wi;
974 		rxdelta = xs - xd;
975 	}
976 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_RECT_START,
977 	    (yd << 16) | rxd);
978 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_RECT_SIZE,
979 	    (he << 16) | rwi);
980 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_SOURCE_DELTA,
981 	    (((ys - yd) & 0xfff) << 16) | (rxdelta & 0xfff));
982 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_RENDER,
983 	    PM2RE_RECTANGLE | dir);
984 }
985 
986 static void
pm2fb_cursor(void * cookie,int on,int row,int col)987 pm2fb_cursor(void *cookie, int on, int row, int col)
988 {
989 	struct rasops_info *ri = cookie;
990 	struct vcons_screen *scr = ri->ri_hw;
991 	struct pm2fb_softc *sc = scr->scr_cookie;
992 	int x, y, wi, he;
993 
994 	wi = ri->ri_font->fontwidth;
995 	he = ri->ri_font->fontheight;
996 
997 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
998 		x = ri->ri_ccol * wi + ri->ri_xorigin;
999 		y = ri->ri_crow * he + ri->ri_yorigin;
1000 		if (ri->ri_flg & RI_CURSOR) {
1001 			pm2fb_bitblt(sc, x, y, x, y, wi, he, 12);
1002 			ri->ri_flg &= ~RI_CURSOR;
1003 		}
1004 		ri->ri_crow = row;
1005 		ri->ri_ccol = col;
1006 		if (on) {
1007 			x = ri->ri_ccol * wi + ri->ri_xorigin;
1008 			y = ri->ri_crow * he + ri->ri_yorigin;
1009 			pm2fb_bitblt(sc, x, y, x, y, wi, he, 12);
1010 			ri->ri_flg |= RI_CURSOR;
1011 		}
1012 	} else {
1013 		scr->scr_ri.ri_crow = row;
1014 		scr->scr_ri.ri_ccol = col;
1015 		scr->scr_ri.ri_flg &= ~RI_CURSOR;
1016 	}
1017 
1018 }
1019 
1020 static void
pm2fb_putchar(void * cookie,int row,int col,u_int c,long attr)1021 pm2fb_putchar(void *cookie, int row, int col, u_int c, long attr)
1022 {
1023 	struct rasops_info *ri = cookie;
1024 	struct wsdisplay_font *font = PICK_FONT(ri, c);
1025 	struct vcons_screen *scr = ri->ri_hw;
1026 	struct pm2fb_softc *sc = scr->scr_cookie;
1027 	uint32_t mode;
1028 
1029 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
1030 		void *data;
1031 		uint32_t fg, bg;
1032 		int uc, i;
1033 		int x, y, wi, he;
1034 
1035 		wi = font->fontwidth;
1036 		he = font->fontheight;
1037 
1038 		if (!CHAR_IN_FONT(c, font))
1039 			return;
1040 		bg = ri->ri_devcmap[(attr >> 16) & 0xf];
1041 		fg = ri->ri_devcmap[(attr >> 24) & 0xf];
1042 		x = ri->ri_xorigin + col * wi;
1043 		y = ri->ri_yorigin + row * he;
1044 		if (c == 0x20) {
1045 			pm2fb_rectfill(sc, x, y, wi, he, bg);
1046 		} else {
1047 			uc = c - font->firstchar;
1048 			data = (uint8_t *)font->data + uc * ri->ri_fontscale;
1049 
1050 			mode = PM2RM_MASK_MIRROR;
1051 #if BYTE_ORDER == LITTLE_ENDIAN
1052 			switch (ri->ri_font->stride) {
1053 				case 1:
1054 					mode |= 4 << 7;
1055 					break;
1056 				case 2:
1057 					mode |= 3 << 7;
1058 					break;
1059 			}
1060 #else
1061 			switch (ri->ri_font->stride) {
1062 				case 1:
1063 					mode |= 3 << 7;
1064 					break;
1065 				case 2:
1066 					mode |= 2 << 7;
1067 					break;
1068 			}
1069 #endif
1070 			pm2fb_wait(sc, 8);
1071 
1072 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
1073 			    PM2_RE_MODE, mode);
1074 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
1075 			    PM2_RE_CONFIG, PM2RECFG_WRITE_EN);
1076 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
1077 			    PM2_RE_BLOCK_COLOUR, bg);
1078 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
1079 			    PM2_RE_RECT_START, (y << 16) | x);
1080 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
1081 			    PM2_RE_RECT_SIZE, (he << 16) | wi);
1082 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
1083 			    PM2_RE_RENDER,
1084 			    PM2RE_RECTANGLE |
1085 			    PM2RE_INC_X | PM2RE_INC_Y | PM2RE_FASTFILL);
1086 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
1087 			    PM2_RE_BLOCK_COLOUR, fg);
1088 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
1089 			    PM2_RE_RENDER,
1090 			    PM2RE_RECTANGLE | PM2RE_SYNC_ON_MASK |
1091 			    PM2RE_INC_X | PM2RE_INC_Y | PM2RE_FASTFILL);
1092 
1093 			pm2fb_wait(sc, he);
1094 			switch (ri->ri_font->stride) {
1095 			case 1: {
1096 				uint8_t *data8 = data;
1097 				uint32_t reg;
1098 				for (i = 0; i < he; i++) {
1099 					reg = *data8;
1100 					bus_space_write_4(sc->sc_memt,
1101 					    sc->sc_regh,
1102 					    PM2_RE_BITMASK, reg);
1103 					data8++;
1104 				}
1105 				break;
1106 				}
1107 			case 2: {
1108 				uint16_t *data16 = data;
1109 				uint32_t reg;
1110 				for (i = 0; i < he; i++) {
1111 					reg = *data16;
1112 					bus_space_write_4(sc->sc_memt,
1113 					    sc->sc_regh,
1114 					    PM2_RE_BITMASK, reg);
1115 					data16++;
1116 				}
1117 				break;
1118 			}
1119 			}
1120 		}
1121 		if (attr & 1)
1122 			pm2fb_rectfill(sc, x, y + he - 2, wi, 1, fg);
1123 	}
1124 }
1125 
1126 static void
pm2fb_putchar_aa(void * cookie,int row,int col,u_int c,long attr)1127 pm2fb_putchar_aa(void *cookie, int row, int col, u_int c, long attr)
1128 {
1129 	struct rasops_info *ri = cookie;
1130 	struct wsdisplay_font *font = PICK_FONT(ri, c);
1131 	struct vcons_screen *scr = ri->ri_hw;
1132 	struct pm2fb_softc *sc = scr->scr_cookie;
1133 	uint32_t bg, fg, /*latch = 0,*/ bg8, fg8, pixel;
1134 	int i, x, y, wi, he, r, g, b, aval;
1135 	int r1, g1, b1, r0, g0, b0, fgo, bgo;
1136 	uint8_t *data8;
1137 	int rv = GC_NOPE, cnt = 0;
1138 
1139 	if (sc->sc_mode != WSDISPLAYIO_MODE_EMUL)
1140 		return;
1141 
1142 	if (!CHAR_IN_FONT(c, font))
1143 		return;
1144 
1145 	wi = font->fontwidth;
1146 	he = font->fontheight;
1147 
1148 	bg = ri->ri_devcmap[(attr >> 16) & 0xf];
1149 	fg = ri->ri_devcmap[(attr >> 24) & 0xf];
1150 	x = ri->ri_xorigin + col * wi;
1151 	y = ri->ri_yorigin + row * he;
1152 	if (c == 0x20) {
1153 		pm2fb_rectfill(sc, x, y, wi, he, bg);
1154 		if (attr & 1)
1155 			pm2fb_rectfill(sc, x, y + he - 2, wi, 1, fg);
1156 		return;
1157 	}
1158 
1159 #ifdef BITBLT_LE_WORKAROUND
1160 	rv = GC_NOPE;
1161 #else
1162 	rv = glyphcache_try(&sc->sc_gc, c, x, y, attr);
1163 	if (rv == GC_OK)
1164 		return;
1165 #endif
1166 
1167 	data8 = WSFONT_GLYPH(c, font);
1168 
1169 	pm2fb_wait(sc, 5);
1170 #if 0
1171 	/*
1172 	 * TODO:
1173 	 * - use packed mode here as well, instead of writing each pixel separately
1174 	 * - see if we can trick the chip into doing the alpha blending for us
1175 	 */
1176 	x = x >> 2;
1177 	wi = (wi + 3) >> 2;
1178 #endif
1179 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_MODE, 0);
1180 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_CONFIG,
1181 			    PM2RECFG_WRITE_EN /*| PM2RECFG_PACKED*/);
1182 	bus_space_write_4(sc->sc_memt, sc->sc_regh,
1183 			    PM2_RE_RECT_START, (y << 16) | x);
1184 	bus_space_write_4(sc->sc_memt, sc->sc_regh,
1185 			    PM2_RE_RECT_SIZE, (he << 16) | wi);
1186 	bus_space_write_4(sc->sc_memt, sc->sc_regh,
1187 			    PM2_RE_RENDER,
1188 			    PM2RE_RECTANGLE | PM2RE_SYNC_ON_HOST |
1189 			    PM2RE_INC_X | PM2RE_INC_Y);
1190 	/*
1191 	 * we need the RGB colours here, so get offsets into rasops_cmap
1192 	 */
1193 	fgo = ((attr >> 24) & 0xf) * 3;
1194 	bgo = ((attr >> 16) & 0xf) * 3;
1195 
1196 	r0 = rasops_cmap[bgo];
1197 	r1 = rasops_cmap[fgo];
1198 	g0 = rasops_cmap[bgo + 1];
1199 	g1 = rasops_cmap[fgo + 1];
1200 	b0 = rasops_cmap[bgo + 2];
1201 	b1 = rasops_cmap[fgo + 2];
1202 #define R3G3B2(r, g, b) ((r & 0xe0) | ((g >> 3) & 0x1c) | (b >> 6))
1203 	bg8 = R3G3B2(r0, g0, b0);
1204 	fg8 = R3G3B2(r1, g1, b1);
1205 
1206 	pm2fb_wait(sc, 200);
1207 
1208 	for (i = 0; i < ri->ri_fontscale; i++) {
1209 		aval = *data8;
1210 		if (aval == 0) {
1211 			pixel = bg8;
1212 		} else if (aval == 255) {
1213 			pixel = fg8;
1214 		} else {
1215 			r = aval * r1 + (255 - aval) * r0;
1216 			g = aval * g1 + (255 - aval) * g0;
1217 			b = aval * b1 + (255 - aval) * b0;
1218 			pixel = ((r & 0xe000) >> 8) |
1219 				((g & 0xe000) >> 11) |
1220 				((b & 0xc000) >> 14);
1221 		}
1222 #if 0
1223 		latch = (latch << 8) | pixel;
1224 		/* write in 32bit chunks */
1225 		if ((i & 3) == 3) {
1226 			bus_space_write_stream_4(sc->sc_memt, sc->sc_regh,
1227 			    PM2_RE_DATA, latch);
1228 			/*
1229 			 * not strictly necessary, old data should be shifted
1230 			 * out
1231 			 */
1232 			latch = 0;
1233 			cnt++;
1234 			if (cnt > 190) {
1235 				pm2fb_wait(sc, 200);
1236 				cnt = 0;
1237 			}
1238 		}
1239 #else
1240 		bus_space_write_4(sc->sc_memt, sc->sc_regh,
1241 			    PM2_RE_COLOUR, pixel);
1242 
1243 		if (cnt > 190) {
1244 			pm2fb_wait(sc, 200);
1245 			cnt = 0;
1246 		}
1247 #endif
1248 		data8++;
1249 	}
1250 #if 0
1251 	/* if we have pixels left in latch write them out */
1252 	if ((i & 3) != 0) {
1253 		latch = latch << ((4 - (i & 3)) << 3);
1254 		bus_space_write_stream_4(sc->sc_memt, sc->sc_regh,
1255 				    PM2_RE_DATA, latch);
1256 	}
1257 #endif
1258 	/*
1259 	 * XXX
1260 	 * occasionally characters end up in the cache only partially drawn
1261 	 * apparently the blitter might end up grabbing them before they're
1262 	 * completely flushed out into video memory
1263 	 * so we let the pipeline drain a little bit before continuing
1264 	 */
1265 	pm2fb_wait(sc, 20);
1266 
1267 	if (rv == GC_ADD) {
1268 		glyphcache_add(&sc->sc_gc, c, x, y);
1269 	} else if (attr & 1)
1270 		pm2fb_rectfill(sc, x, y + he - 2, wi, 1, fg);
1271 }
1272 
1273 static void
pm2fb_copycols(void * cookie,int row,int srccol,int dstcol,int ncols)1274 pm2fb_copycols(void *cookie, int row, int srccol, int dstcol, int ncols)
1275 {
1276 	struct rasops_info *ri = cookie;
1277 	struct vcons_screen *scr = ri->ri_hw;
1278 	struct pm2fb_softc *sc = scr->scr_cookie;
1279 	int32_t xs, xd, y, width, height;
1280 
1281 	if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
1282 		xs = ri->ri_xorigin + ri->ri_font->fontwidth * srccol;
1283 		xd = ri->ri_xorigin + ri->ri_font->fontwidth * dstcol;
1284 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
1285 		width = ri->ri_font->fontwidth * ncols;
1286 		height = ri->ri_font->fontheight;
1287 		pm2fb_bitblt(sc, xs, y, xd, y, width, height, 3);
1288 	}
1289 }
1290 
1291 static void
pm2fb_erasecols(void * cookie,int row,int startcol,int ncols,long fillattr)1292 pm2fb_erasecols(void *cookie, int row, int startcol, int ncols, long fillattr)
1293 {
1294 	struct rasops_info *ri = cookie;
1295 	struct vcons_screen *scr = ri->ri_hw;
1296 	struct pm2fb_softc *sc = scr->scr_cookie;
1297 	int32_t x, y, width, height, fg, bg, ul;
1298 
1299 	if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
1300 		x = ri->ri_xorigin + ri->ri_font->fontwidth * startcol;
1301 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
1302 		width = ri->ri_font->fontwidth * ncols;
1303 		height = ri->ri_font->fontheight;
1304 		rasops_unpack_attr(fillattr, &fg, &bg, &ul);
1305 
1306 		pm2fb_rectfill(sc, x, y, width, height, ri->ri_devcmap[bg]);
1307 	}
1308 }
1309 
1310 static void
pm2fb_copyrows(void * cookie,int srcrow,int dstrow,int nrows)1311 pm2fb_copyrows(void *cookie, int srcrow, int dstrow, int nrows)
1312 {
1313 	struct rasops_info *ri = cookie;
1314 	struct vcons_screen *scr = ri->ri_hw;
1315 	struct pm2fb_softc *sc = scr->scr_cookie;
1316 	int32_t x, ys, yd, width, height;
1317 
1318 	if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
1319 		x = ri->ri_xorigin;
1320 		ys = ri->ri_yorigin + ri->ri_font->fontheight * srcrow;
1321 		yd = ri->ri_yorigin + ri->ri_font->fontheight * dstrow;
1322 		width = ri->ri_emuwidth;
1323 		height = ri->ri_font->fontheight*nrows;
1324 		pm2fb_bitblt(sc, x, ys, x, yd, width, height, 3);
1325 	}
1326 }
1327 
1328 static void
pm2fb_eraserows(void * cookie,int row,int nrows,long fillattr)1329 pm2fb_eraserows(void *cookie, int row, int nrows, long fillattr)
1330 {
1331 	struct rasops_info *ri = cookie;
1332 	struct vcons_screen *scr = ri->ri_hw;
1333 	struct pm2fb_softc *sc = scr->scr_cookie;
1334 	int32_t x, y, width, height, fg, bg, ul;
1335 
1336 	if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
1337 		x = ri->ri_xorigin;
1338 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
1339 		width = ri->ri_emuwidth;
1340 		height = ri->ri_font->fontheight * nrows;
1341 		rasops_unpack_attr(fillattr, &fg, &bg, &ul);
1342 
1343 		pm2fb_rectfill(sc, x, y, width, height, ri->ri_devcmap[bg]);
1344 	}
1345 }
1346 
1347 /*
1348  * Permedia2 can't blit outside of 2048x2048, so reject anything higher
1349  * max. dot clock is probably too high
1350  */
1351 
1352 #define MODE_IS_VALID(m) (((m)->hdisplay < 2048) && \
1353 	((m)->dot_clock < 230000))
1354 
1355 static void
pm2_setup_i2c(struct pm2fb_softc * sc)1356 pm2_setup_i2c(struct pm2fb_softc *sc)
1357 {
1358 	int i;
1359 #ifdef PM2FB_DEBUG
1360 	int j;
1361 #endif
1362 
1363 	/* Fill in the i2c tag */
1364 	sc->sc_i2c.ic_cookie = sc;
1365 	sc->sc_i2c.ic_acquire_bus = pm2fb_i2c_acquire_bus;
1366 	sc->sc_i2c.ic_release_bus = pm2fb_i2c_release_bus;
1367 	sc->sc_i2c.ic_send_start = pm2fb_i2c_send_start;
1368 	sc->sc_i2c.ic_send_stop = pm2fb_i2c_send_stop;
1369 	sc->sc_i2c.ic_initiate_xfer = pm2fb_i2c_initiate_xfer;
1370 	sc->sc_i2c.ic_read_byte = pm2fb_i2c_read_byte;
1371 	sc->sc_i2c.ic_write_byte = pm2fb_i2c_write_byte;
1372 	sc->sc_i2c.ic_exec = NULL;
1373 
1374 	DPRINTF("data: %08x\n", bus_space_read_4(sc->sc_memt, sc->sc_regh,
1375 		PM2_DISPLAY_DATA));
1376 
1377 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_DISPLAY_DATA, 0);
1378 
1379 	/* zero out the EDID buffer */
1380 	memset(sc->sc_edid_data, 0, 128);
1381 
1382 	/* Some monitors don't respond first time */
1383 	i = 0;
1384 	while (sc->sc_edid_data[1] == 0 && i < 10) {
1385 		ddc_read_edid(&sc->sc_i2c, sc->sc_edid_data, 128);
1386 		i++;
1387 	}
1388 #ifdef PM2FB_DEBUG
1389 	printf("i = %d\n", i);
1390 	for (i = 0; i < 128; i += 16) {
1391 		printf("%02x:", i);
1392 		for (j = 0; j < 16; j++)
1393 			printf(" %02x", sc->sc_edid_data[i + j]);
1394 		printf("\n");
1395 	}
1396 #endif
1397 
1398 	if (edid_parse(&sc->sc_edid_data[0], &sc->sc_ei) != -1) {
1399 #ifdef PM2FB_DEBUG
1400 		edid_print(&sc->sc_ei);
1401 #endif
1402 
1403 		/*
1404 		 * Now pick a mode.
1405 		 */
1406 		if ((sc->sc_ei.edid_preferred_mode != NULL)) {
1407 			struct videomode *m = sc->sc_ei.edid_preferred_mode;
1408 			if (MODE_IS_VALID(m)) {
1409 				sc->sc_videomode = m;
1410 			} else {
1411 				aprint_error_dev(sc->sc_dev,
1412 				    "unable to use preferred mode\n");
1413 			}
1414 		}
1415 		/*
1416 		 * if we can't use the preferred mode go look for the
1417 		 * best one we can support
1418 		 */
1419 		if (sc->sc_videomode == NULL) {
1420 			struct videomode *m = sc->sc_ei.edid_modes;
1421 
1422 			sort_modes(sc->sc_ei.edid_modes,
1423 			    &sc->sc_ei.edid_preferred_mode,
1424 			    sc->sc_ei.edid_nmodes);
1425 			if (sc->sc_videomode == NULL)
1426 				for (int n = 0; n < sc->sc_ei.edid_nmodes; n++)
1427 					if (MODE_IS_VALID(&m[n])) {
1428 						sc->sc_videomode = &m[n];
1429 						break;
1430 					}
1431 		}
1432 	}
1433 	if (sc->sc_videomode == NULL) {
1434 		/* no EDID data? */
1435 		sc->sc_videomode = pick_mode_by_ref(sc->sc_width,
1436 		    sc->sc_height, 60);
1437 	}
1438 	if (sc->sc_videomode != NULL) {
1439 		pm2fb_set_mode(sc, sc->sc_videomode);
1440 	}
1441 }
1442 
1443 /* I2C bitbanging */
pm2fb_i2cbb_set_bits(void * cookie,uint32_t bits)1444 static void pm2fb_i2cbb_set_bits(void *cookie, uint32_t bits)
1445 {
1446 	struct pm2fb_softc *sc = cookie;
1447 	uint32_t out;
1448 
1449 	out = bits << 2;	/* bitmasks match the IN bits */
1450 
1451 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_DISPLAY_DATA, out);
1452 	delay(100);
1453 }
1454 
pm2fb_i2cbb_set_dir(void * cookie,uint32_t dir)1455 static void pm2fb_i2cbb_set_dir(void *cookie, uint32_t dir)
1456 {
1457 	/* Nothing to do */
1458 }
1459 
pm2fb_i2cbb_read(void * cookie)1460 static uint32_t pm2fb_i2cbb_read(void *cookie)
1461 {
1462 	struct pm2fb_softc *sc = cookie;
1463 	uint32_t bits;
1464 
1465 	bits = bus_space_read_4(sc->sc_memt, sc->sc_regh, PM2_DISPLAY_DATA);
1466 	return bits;
1467 }
1468 
1469 /* higher level I2C stuff */
1470 static int
pm2fb_i2c_acquire_bus(void * cookie,int flags)1471 pm2fb_i2c_acquire_bus(void *cookie, int flags)
1472 {
1473 	/* private bus */
1474 	return (0);
1475 }
1476 
1477 static void
pm2fb_i2c_release_bus(void * cookie,int flags)1478 pm2fb_i2c_release_bus(void *cookie, int flags)
1479 {
1480 	/* private bus */
1481 }
1482 
1483 static int
pm2fb_i2c_send_start(void * cookie,int flags)1484 pm2fb_i2c_send_start(void *cookie, int flags)
1485 {
1486 	return (i2c_bitbang_send_start(cookie, flags, &pm2fb_i2cbb_ops));
1487 }
1488 
1489 static int
pm2fb_i2c_send_stop(void * cookie,int flags)1490 pm2fb_i2c_send_stop(void *cookie, int flags)
1491 {
1492 
1493 	return (i2c_bitbang_send_stop(cookie, flags, &pm2fb_i2cbb_ops));
1494 }
1495 
1496 static int
pm2fb_i2c_initiate_xfer(void * cookie,i2c_addr_t addr,int flags)1497 pm2fb_i2c_initiate_xfer(void *cookie, i2c_addr_t addr, int flags)
1498 {
1499 
1500 	return (i2c_bitbang_initiate_xfer(cookie, addr, flags,
1501 	    &pm2fb_i2cbb_ops));
1502 }
1503 
1504 static int
pm2fb_i2c_read_byte(void * cookie,uint8_t * valp,int flags)1505 pm2fb_i2c_read_byte(void *cookie, uint8_t *valp, int flags)
1506 {
1507 	return (i2c_bitbang_read_byte(cookie, valp, flags, &pm2fb_i2cbb_ops));
1508 }
1509 
1510 static int
pm2fb_i2c_write_byte(void * cookie,uint8_t val,int flags)1511 pm2fb_i2c_write_byte(void *cookie, uint8_t val, int flags)
1512 {
1513 	return (i2c_bitbang_write_byte(cookie, val, flags, &pm2fb_i2cbb_ops));
1514 }
1515 
1516 static int
pm2vfb_set_pll(struct pm2fb_softc * sc,int freq)1517 pm2vfb_set_pll(struct pm2fb_softc *sc, int freq)
1518 {
1519 	int m, n, p, diff, out_freq, bm = 1, bn = 3, bp = 0,
1520 	    bdiff = 1000000 /* , bfreq */;
1521 	int fi;
1522 	uint8_t temp;
1523 
1524 	for (m = 1; m < 128; m++) {
1525 		for (n = 2 * m + 1; n < 256; n++) {
1526 			fi = PM2_EXT_CLOCK_FREQ * n / m;
1527 			for (p = 0; p < 2; p++) {
1528 				out_freq = fi >> (p + 1);
1529 				diff = abs(out_freq - freq);
1530 				if (diff < bdiff) {
1531 					bdiff = diff;
1532 					/* bfreq = out_freq; */
1533 					bm = m;
1534 					bn = n;
1535 					bp = p;
1536 				}
1537 			}
1538 		}
1539 	}
1540 #if 0
1541 	/*
1542 	 * XXX
1543 	 * output between switching modes and attaching a wsdisplay will
1544 	 * go through firmware calls on sparc64 and potentially mess up
1545 	 * our drawing engine state
1546 	 */
1547 	DPRINTF("best: %d kHz ( %d off ), %d %d %d\n", bfreq, bdiff, bm, bn, bp);
1548 #endif
1549 	temp = pm2fb_read_dac(sc, PM2V_DAC_CLOCK_CONTROL) & 0xfc;
1550 	pm2fb_write_dac(sc, PM2V_DAC_CONTROL, 0);
1551 	pm2fb_write_dac(sc, PM2V_DAC_CLOCK_A_M, bm);
1552 	pm2fb_write_dac(sc, PM2V_DAC_CLOCK_A_N, bn);
1553 	pm2fb_write_dac(sc, PM2V_DAC_CLOCK_A_P, bp);
1554 	pm2fb_write_dac(sc, PM2V_DAC_CLOCK_CONTROL, temp | 3);
1555 	return 0;
1556 }
1557 
1558 static int
pm2fb_set_pll(struct pm2fb_softc * sc,int freq)1559 pm2fb_set_pll(struct pm2fb_softc *sc, int freq)
1560 {
1561 	uint8_t  reg, bm = 0, bn = 0, bp = 0;
1562 	unsigned int  m, n, p, fi, diff, out_freq, bdiff = 1000000;
1563 
1564 	for (n = 2; n < 15; n++) {
1565 		for (m = 2 ; m < 256; m++) {
1566 			fi = PM2_EXT_CLOCK_FREQ * m / n;
1567 			if (fi >= PM2_PLL_FREQ_MIN && fi <= PM2_PLL_FREQ_MAX) {
1568 				for (p = 0; p < 5; p++) {
1569 					out_freq = fi >> p;
1570 					diff = abs(out_freq - freq);
1571 					if (diff < bdiff) {
1572 						bm = m;
1573 						bn = n;
1574 						bp = p;
1575 						bdiff = diff;
1576 					}
1577 				}
1578 			}
1579 		}
1580 	}
1581 
1582 	pm2fb_write_dac(sc, PM2_DAC_PIXELCLKA_M, bm);
1583 	pm2fb_write_dac(sc, PM2_DAC_PIXELCLKA_N, bn);
1584 	pm2fb_write_dac(sc, PM2_DAC_PIXELCLKA_P, (bp | 0x08));
1585 
1586 	do {
1587 		reg = bus_space_read_1(sc->sc_memt, sc->sc_regh,
1588 			PM2_DAC_INDEX_DATA);
1589 	} while (reg == PCLK_LOCKED);
1590 
1591 	return 0;
1592 }
1593 
1594 /*
1595  * most of the following was adapted from the xf86-video-glint driver's
1596  * pm2_dac.c (8bpp only)
1597  */
1598 static void
pm2fb_set_dac(struct pm2fb_softc * sc,const struct videomode * mode)1599 pm2fb_set_dac(struct pm2fb_softc *sc, const struct videomode *mode)
1600 {
1601 	int t1, t2, t3, t4, stride;
1602 	uint32_t vclk, tmp;
1603 	uint8_t sync = 0;
1604 
1605 	t1 = mode->hsync_start - mode->hdisplay;
1606 	t2 = mode->vsync_start - mode->vdisplay;
1607 	t3 = mode->hsync_end - mode->hsync_start;
1608 	t4 = mode->vsync_end - mode->vsync_start;
1609 
1610 	/* first round up to the next multiple of 32 */
1611 	stride = (mode->hdisplay + 31) & ~31;
1612 	/* then find the next bigger one that we have partial products for */
1613 	while ((partprodPermedia[stride >> 5] == -1) && (stride < 2048)) {
1614 		stride += 32;
1615 	}
1616 
1617 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_HTOTAL,
1618 	    ((mode->htotal) >> 2) - 1);
1619 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_HSYNC_END,
1620 	    (t1 + t3) >> 2);
1621 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_HSYNC_START,
1622 	    (t1 >> 2));
1623 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_HBLANK_END,
1624 	    (mode->htotal - mode->hdisplay) >> 2);
1625 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_HGATE_END,
1626 	    (mode->htotal - mode->hdisplay) >> 2);
1627 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_SCREEN_STRIDE,
1628 	    stride >> 3);
1629 
1630 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_VTOTAL,
1631 	    mode->vtotal - 2);
1632 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_VSYNC_END,
1633 	    t2 + t4 - 1);
1634 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_VSYNC_START,
1635 	    t2 - 1);
1636 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_VBLANK_END,
1637 	    mode->vtotal - mode->vdisplay);
1638 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_VIDEO_CONTROL,
1639 	    PM2_VC_VIDEO_ENABLE |
1640 	    PM2_VC_HSYNC_ACT_HIGH | PM2_VC_VSYNC_ACT_HIGH);
1641 
1642 	vclk = bus_space_read_4(sc->sc_memt, sc->sc_regh, PM2_VCLKCTL);
1643 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_VCLKCTL,
1644 	    vclk & 0xfffffffc);
1645 
1646 	tmp = bus_space_read_4(sc->sc_memt, sc->sc_regh, PM2_CHIP_CONFIG);
1647 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_CHIP_CONFIG,
1648 	    tmp & 0xffffffdd);
1649 
1650 	pm2fb_write_dac(sc, PM2_DAC_MODE_CONTROL, MOC_BUFFERFRONT);
1651 	pm2fb_set_pll(sc, mode->dot_clock);
1652 
1653 	sync = MC_PALETTE_8BIT;
1654 
1655 	if (!(mode->flags & VID_PHSYNC))
1656 	    sync |= MC_HSYNC_INV;
1657 	if (!(mode->flags & VID_PVSYNC))
1658 	    sync |= MC_VSYNC_INV;
1659 
1660 	pm2fb_write_dac(sc, PM2_DAC_MISC_CONTROL, sync);
1661 	pm2fb_write_dac(sc, PM2_DAC_COLOR_MODE,
1662 	    CM_PALETTE | CM_GUI_ENABLE | CM_RGB);
1663 
1664 	sc->sc_width = mode->hdisplay;
1665 	sc->sc_height = mode->vdisplay;
1666 	sc->sc_depth = 8;
1667 	sc->sc_stride = stride;
1668 	aprint_normal_dev(sc->sc_dev, "pm2 using %d x %d in 8 bit, stride %d\n",
1669 	    sc->sc_width, sc->sc_height, stride);
1670 }
1671 
1672 /*
1673  * most of the following was adapted from the xf86-video-glint driver's
1674  * pm2v_dac.c
1675  */
1676 static void
pm2vfb_set_dac(struct pm2fb_softc * sc,const struct videomode * mode)1677 pm2vfb_set_dac(struct pm2fb_softc *sc, const struct videomode *mode)
1678 {
1679 	int t1, t2, t3, t4, stride;
1680 	uint32_t vclk;
1681 	uint8_t sync = 0;
1682 
1683 	t1 = mode->hsync_start - mode->hdisplay;
1684 	t2 = mode->vsync_start - mode->vdisplay;
1685 	t3 = mode->hsync_end - mode->hsync_start;
1686 	t4 = mode->vsync_end - mode->vsync_start;
1687 
1688 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_HTOTAL,
1689 	    ((mode->htotal) >> 3) - 1);
1690 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_HSYNC_END,
1691 	    (t1 + t3) >> 3);
1692 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_HSYNC_START,
1693 	    (t1 >> 3) - 1);
1694 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_HBLANK_END,
1695 	    (mode->htotal - mode->hdisplay) >> 3);
1696 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_HGATE_END,
1697 	    (mode->htotal - mode->hdisplay) >> 3);
1698 
1699 	/* first round up to the next multiple of 32 */
1700 	stride = (mode->hdisplay + 31) & ~31;
1701 	/* then find the next bigger one that we have partial products for */
1702 	while ((partprodPermedia[stride >> 5] == -1) && (stride < 2048)) {
1703 		stride += 32;
1704 	}
1705 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_SCREEN_STRIDE,
1706 	    stride >> 3);
1707 
1708 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_VTOTAL,
1709 	    mode->vtotal - 1);
1710 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_VSYNC_END,
1711 	    t2 + t4);
1712 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_VSYNC_START,
1713 	    t2);
1714 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_VBLANK_END,
1715 	    mode->vtotal - mode->vdisplay);
1716 
1717 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_VIDEO_CONTROL,
1718 	    PM2_VC_VIDEO_ENABLE | PM2_VC_RAMDAC_64BIT |
1719 	    PM2_VC_HSYNC_ACT_HIGH | PM2_VC_VSYNC_ACT_HIGH);
1720 
1721 	vclk = bus_space_read_4(sc->sc_memt, sc->sc_regh, PM2_VCLKCTL);
1722 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_VCLKCTL,
1723 	    vclk & 0xfffffffc);
1724 
1725 	pm2vfb_set_pll(sc, mode->dot_clock / 2);
1726 	pm2fb_write_dac(sc, PM2V_DAC_MISC_CONTROL, PM2V_DAC_8BIT);
1727 
1728 	if (mode->flags & VID_PHSYNC)
1729 		sync |= PM2V_DAC_HSYNC_INV;
1730 	if (mode->flags & VID_PVSYNC)
1731 		sync |= PM2V_DAC_VSYNC_INV;
1732 	pm2fb_write_dac(sc, PM2V_DAC_SYNC_CONTROL, sync);
1733 
1734 	pm2fb_write_dac(sc, PM2V_DAC_COLOR_FORMAT, PM2V_DAC_PALETTE);
1735 	pm2fb_write_dac(sc, PM2V_DAC_PIXEL_SIZE, PM2V_PS_8BIT);
1736 	sc->sc_width = mode->hdisplay;
1737 	sc->sc_height = mode->vdisplay;
1738 	sc->sc_depth = 8;
1739 	sc->sc_stride = stride;
1740 	aprint_normal_dev(sc->sc_dev, "pm2v using %d x %d in 8 bit, stride %d\n",
1741 	    sc->sc_width, sc->sc_height, stride);
1742 }
1743 
1744 static void
pm2fb_set_mode(struct pm2fb_softc * sc,const struct videomode * mode)1745 pm2fb_set_mode(struct pm2fb_softc *sc, const struct videomode *mode)
1746 {
1747 	if (sc->sc_is_pm2) {
1748 		pm2fb_set_dac(sc, mode);
1749 	} else {
1750 		pm2vfb_set_dac(sc, mode);
1751 	}
1752 }
1753