xref: /netbsd-src/sys/arch/macppc/dev/platinumfb.c (revision b83ebeba7f767758d2778bb0f9d7a76534253621)
1 /*
2  * Redistribution and use in source and binary forms, with or without
3  * modification, are permitted provided that the following conditions
4  * are met:
5  * 1. Redistributions of source code must retain the above copyright
6  *    notice, this list of conditions and the following disclaimer.
7  * 2. Redistributions in binary form must reproduce the above copyright
8  *    notice, this list of conditions and the following disclaimer in the
9  *    documentation and/or other materials provided with the distribution.
10  *
11  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
12  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
13  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
14  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
15  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
16  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
17  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
18  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
19  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
20  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
21  */
22 
23 /* A console driver for Apple's Platinum onboard video controller,
24  * found in (all?) Catalyst logic boards including the Powermac 7200.
25  *
26  * Used valkyriefb.c from NetBSD, and platinumfb.c/platinumfb.h from
27  * Linux sources as templates.
28  *
29  * Platinum is broken regarding openfirmware video variables.  In OF,
30  * for a powermac 7200, doing "dev /platinum .properties" results in:
31  *
32  *	name                    platinum
33  *	device_type             display
34  *	model                   AAPL,343S1184
35  *	AAPL,connector          monitor
36  *	reg                     F8000000  00000800
37  *	                        F1000000  01000000
38  *	AAPL,interrupts         0000001E
39  *
40  * The first reg is the register set, and the second is for the
41  * framebuffer.  There is also a set of colormap registers hardcoded
42  * in platinumfbreg.h that (I think) aren't in openfirmware.
43  *
44  * powermac 7200 VRAM min and max limits are 1 and 4 Mb respectively.
45  * OF claims 16M so we don't use that value.  If other machines can
46  * can have more or less VRAM this code will need to be modified
47  */
48 
49 #include <sys/cdefs.h>
50 __KERNEL_RCSID(0, "$NetBSD: platinumfb.c,v 1.2 2016/07/06 13:28:34 macallan Exp $");
51 
52 #include <sys/param.h>
53 #include <sys/systm.h>
54 #include <sys/kernel.h>
55 #include <sys/device.h>
56 
57 #include <uvm/uvm_param.h>
58 
59 #include <dev/ofw/openfirm.h>
60 
61 #include <machine/autoconf.h>
62 #include <machine/pio.h>
63 
64 #include <dev/wscons/wsdisplayvar.h>
65 #include <dev/wscons/wsconsio.h>
66 #include <dev/wsfont/wsfont.h>
67 #include <dev/rasops/rasops.h>
68 #include <dev/wscons/wsdisplay_vconsvar.h>
69 
70 #include <dev/videomode/videomode.h>
71 
72 #include <arch/macppc/dev/platinumfbreg.h>
73 
74 #include <sys/sysctl.h>
75 
76 #include "opt_wsemul.h"
77 
78 /*
79  * here is a link of supported modes and resolutions:
80  *  https://support.apple.com/kb/SP343?locale=en_US
81  *
82  * default console and X bpp/depth for built-in X config file,
83  * select 8 or 16 or 32.
84  *
85  */
86 #define PLATINUM_CONSOLE_DEPTH 8
87 #define PLATINUM_FB_DEPTH      16
88 
89 /*
90  * resolution, from one of platinumfb_setting vmode_name's.
91  */
92 #define PLATINUM_FB_VMODE "1024x768x60"
93 
94 struct platinumfb_setting {
95 	char vmode_name[24];
96 	int32_t width;
97 	int32_t height;
98     	uint8_t freq;
99 	uint8_t macmode;
100 
101 	int32_t	pitch[3];
102 	uint32_t regs[26];
103 	uint8_t offset[3];
104 	uint8_t mode[3];
105 	uint8_t dacula_ctrl[3];
106 	uint8_t clock_params[2][2];
107 };
108 
109 struct platinumfb_softc {
110 	device_t sc_dev;
111 	int sc_node;
112 
113         uint8_t *sc_reg;
114 	uint32_t sc_reg_size;
115 
116     	uint8_t *sc_cmap;
117 	uint32_t sc_cmap_size;
118 
119 	uint8_t *sc_fb;
120 	uint32_t sc_fb_size;
121 
122  	int sc_depth;
123 	int sc_width, sc_height, sc_linebytes;
124 	const struct videomode *sc_videomode;
125 	uint8_t sc_modereg;
126 
127         int sc_mode;
128 
129 	u_char sc_cmap_red[256];
130 	u_char sc_cmap_green[256];
131 	u_char sc_cmap_blue[256];
132 
133 	struct vcons_data vd;
134 
135         uint8_t sc_cmode;
136         uint8_t sc_dac_type;
137         uint32_t sc_vram;
138         int sc_on;
139         struct platinumfb_setting *sc_pfs;
140 };
141 
142 #define DIV2	0x20
143 #define DIV4	0x40
144 #define DIV8	0x60
145 #define DIV16	0x80
146 
147 static struct platinumfb_setting platinum_5 = {
148     "640x480x60",
149     640, 480, 60, 5,
150     { 672, 1312, 2592 },
151     { 0xff0, 4, 0, 0, 0, 0, 0x320, 0,
152       0, 0x15e, 0xc8, 0x18, 0x18f, 0x2f, 0x35, 0x3e,
153       0x42, 0x182, 0x18e, 0x41a, 0x418, 2, 7, 0x44,
154       0x404, 0x408 }, { 0x34, 0x3c, 0x41 },
155     { 2, 0, 0xff }, { 0x11, 0x15, 0x19 },
156     {{ 26, 0 + DIV8 }, { 14, 2 + DIV4 }}
157 };
158 
159 static struct platinumfb_setting platinum_12 = {
160     "800x600x75",
161     800, 600, 75, 12,
162     { 832, 1632, 3232 },
163     { 0xff0, 4, 0, 0, 0, 0, 0x320, 0,
164       0, 0x1ce, 0x108, 0x14, 0x20f, 0x27, 0x30, 0x39,
165       0x72, 0x202, 0x20e, 0x4e2, 0x4e0, 4, 9, 0x2e,
166       0x4de, 0x4df }, { 0x64, 0x6c, 0x71 },
167     { 2, 0, 0xff }, { 0x11, 0x15, 0x19 },
168     {{ 122, 7 + DIV4 }, { 62, 9 + DIV2 }}
169 };
170 
171 static struct platinumfb_setting platinum_14 = {
172     "1024x768x60",
173     1024, 768, 60, 14,
174     { 1056, 2080, 4128 },
175     { 0xff0, 4, 0, 0, 0, 0, 0x320, 0,
176       0, 0x25a, 0x14f, 0x22, 0x29f, 0x43, 0x49, 0x5b,
177       0x8e, 0x28e, 0x29e, 0x64c, 0x64a, 0xa, 0xf, 0x44,
178       0x644, 0x646 }, { 0x80, 0x88, 0x8d },
179     { 2, 0, 0xff }, { 0x11, 0x15, 0x19 },
180     {{ 71, 6 + DIV2 }, { 118, 13 + DIV2 }}
181 };
182 
183 static struct platinumfb_setting platinum_20 = {
184     "1280x1024x75",
185     1280, 1024, 75, 20,
186     { 1312, 2592, 2592 },
187     { 0xffc, 4, 0, 0, 0, 0, 0x428, 0,
188       0, 0xb3, 0xd3, 0x12, 0x1a5, 0x23, 0x28, 0x2d,
189       0x5e, 0x19e, 0x1a4, 0x854, 0x852, 4, 9, 0x50,
190       0x850, 0x851 }, { 0x58, 0x5d, 0x5d },
191     { 0, 0xff, 0xff }, { 0x51, 0x55, 0x55 },
192     {{ 45, 3 }, { 66, 7 }}
193 };
194 
195 static struct platinumfb_setting *pfb_setting[] = {
196     &platinum_5,
197     &platinum_12,
198     &platinum_14,
199     &platinum_20
200 };
201 
202 static struct vcons_screen platinumfb_console_screen;
203 
204 static int	platinumfb_match(device_t, cfdata_t, void *);
205 static void	platinumfb_attach(device_t, device_t, void *);
206 
207 CFATTACH_DECL_NEW(platinumfb, sizeof(struct platinumfb_softc),
208     platinumfb_match, platinumfb_attach, NULL, NULL);
209 
210 static int	platinumfb_init(device_t);
211 static int 	platinumfb_set_mode(struct platinumfb_softc *,
212 				    const struct videomode *, int);
213 static void     platinumfb_set_rasops(struct platinumfb_softc *,
214 				      struct rasops_info *, int);
215 static int	platinumfb_ioctl(void *, void *, u_long, void *, int,
216 				 struct lwp *);
217 static paddr_t	platinumfb_mmap(void *, void *, off_t, int);
218 static void	platinumfb_init_screen(void *, struct vcons_screen *, int,
219 				       long *);
220 static int	platinumfb_putcmap(struct platinumfb_softc *,
221 				   struct wsdisplay_cmap *);
222 static int 	platinumfb_getcmap(struct platinumfb_softc *,
223 				   struct wsdisplay_cmap *);
224 static void	platinumfb_init_cmap(struct platinumfb_softc *);
225 static void	platinumfb_restore_palette(struct platinumfb_softc *);
226 static void	platinumfb_putpalreg(struct platinumfb_softc *,
227 				     uint8_t, uint8_t, uint8_t, uint8_t);
228 static uint32_t platinumfb_line_tweak(struct platinumfb_softc *);
229 static paddr_t	platinumfb_page_align_up(struct platinumfb_softc *);
230 static void 	platinumfb_set_clock(struct platinumfb_softc *);
231 static void	platinumfb_dac_type(struct platinumfb_softc *);
232 static void	platinumfb_memory_size(struct platinumfb_softc *);
233 static void	platinumfb_set_hardware(struct platinumfb_softc *);
234 
235 static inline void platinumfb_write_reg(struct platinumfb_softc *,
236  					int, uint32_t);
237 
238 #ifdef notyet
239 static inline uint32_t platinumfb_read_reg(struct platinumfb_softc *, int);
240 #endif
241 static inline void platinumfb_write_cmap_reg(struct platinumfb_softc *,
242  					     int, uint8_t);
243 static inline uint8_t platinumfb_read_cmap_reg(struct platinumfb_softc *, int);
244 static inline void platinumfb_store_d2(struct platinumfb_softc *,
245 				       uint8_t, uint8_t);
246 
247 struct wsscreen_descr platinumfb_defaultscreen = {
248 	"default",
249 	0, 0,
250 	NULL,
251 	8, 16,
252 	WSSCREEN_WSCOLORS | WSSCREEN_HILIT,
253 	NULL,
254 };
255 
256 const struct wsscreen_descr *_platinumfb_scrlist[] = {
257 	&platinumfb_defaultscreen,
258 	/* XXX other formats, graphics screen? */
259 };
260 
261 struct wsscreen_list platinumfb_screenlist = {
262 	sizeof(_platinumfb_scrlist) / sizeof(struct wsscreen_descr *),
263 	_platinumfb_scrlist
264 };
265 
266 struct wsdisplay_accessops platinumfb_accessops = {
267 	platinumfb_ioctl,
268 	platinumfb_mmap,
269 	NULL,
270 	NULL,
271 	NULL,
272 	NULL,	/* load_font */
273 	NULL,	/* polls */
274 	NULL,	/* scroll */
275 };
276 
277 static inline void
278 platinumfb_write_reg(struct platinumfb_softc *sc, int reg, uint32_t val)
279 {
280 	out32(sc->sc_reg + PLATINUM_REG_OFFSET_ADDR(reg), val);
281 }
282 
283 #ifdef notyet
284 static inline uint32_t
285 platinumfb_read_reg(struct platinumfb_softc *sc, int reg)
286 {
287 	return in32(sc->sc_reg + PLATINUM_REG_OFFSET_ADDR(reg));
288 }
289 #endif
290 
291 static inline void
292 platinumfb_write_cmap_reg(struct platinumfb_softc *sc,
293 			  int reg_offset, uint8_t val)
294 {
295 	out8(sc->sc_cmap + reg_offset, val);
296 }
297 
298 static inline uint8_t
299 platinumfb_read_cmap_reg(struct platinumfb_softc *sc, int reg_offset)
300 {
301 	return in8(sc->sc_cmap + reg_offset);
302 }
303 
304 static inline void
305 platinumfb_store_d2(struct platinumfb_softc *sc,
306 			  uint8_t a, uint8_t d)
307 {
308 	platinumfb_write_cmap_reg(sc, PLATINUM_CMAP_ADDR_OFFSET, a + 32);
309 	platinumfb_write_cmap_reg(sc, PLATINUM_CMAP_D2_OFFSET, d);
310 }
311 
312 static void
313 platinumfb_putpalreg(struct platinumfb_softc *sc,
314     uint8_t reg, uint8_t r, uint8_t g, uint8_t b)
315 {
316 	platinumfb_write_cmap_reg(sc, PLATINUM_CMAP_ADDR_OFFSET, reg);
317 	platinumfb_write_cmap_reg(sc, PLATINUM_CMAP_LUT_OFFSET, r);
318 	platinumfb_write_cmap_reg(sc, PLATINUM_CMAP_LUT_OFFSET, g);
319 	platinumfb_write_cmap_reg(sc, PLATINUM_CMAP_LUT_OFFSET, b);
320 }
321 
322 static uint32_t
323 platinumfb_line_tweak(struct platinumfb_softc *sc)
324 {
325 	/* bytes per line adjustment depending on resolution and depth */
326 	if (sc->sc_cmode > PLATINUM_CMODE_8 &&
327 	    strcmp(sc->sc_pfs->vmode_name, "832x624x75") == 0)
328 		return 0x10;
329 	else
330 		return 0x20;
331 }
332 
333 static paddr_t
334 platinumfb_page_align_up(struct platinumfb_softc *sc)
335 {
336 	/* round up framebuffer address to the next highest page */
337 	paddr_t addr = (paddr_t)sc->sc_fb;
338 	paddr_t ret = round_page(addr);
339 
340 	if (ret == addr)
341 		ret = round_page(addr + 1);
342 
343 	return ret;
344 }
345 
346 /* 2 versions of platinum clock, older one uses clock[1] and
347  *     freq = 14.3Mhz * c0 / (c1 & 0x1f) / (1 << (c1 >> 5))
348  * newer one uses clock[0] and
349  *     freq = 15Mhz * c0 / ((c1 & 0x1f) + 2) / (1 << (c1 >> 5))
350  */
351 static void
352 platinumfb_set_clock(struct platinumfb_softc *sc)
353 {
354 	uint8_t clk_idx = sc->sc_dac_type == PLATINUM_DAC_1 ? 1 : 0;
355 	uint8_t d2;
356 
357 	platinumfb_store_d2(sc, 6, 0xc6);
358 
359 	platinumfb_write_cmap_reg(sc, PLATINUM_CMAP_ADDR_OFFSET, 3+32);
360 	d2 = platinumfb_read_cmap_reg(sc, PLATINUM_CMAP_D2_OFFSET);
361 
362 	if (d2 == 2) {
363 		platinumfb_store_d2(sc, 7, sc->sc_pfs->clock_params[clk_idx][0]);
364 		platinumfb_store_d2(sc, 8, sc->sc_pfs->clock_params[clk_idx][1]);
365 		platinumfb_store_d2(sc, 3, 3);
366 	} else {
367 		platinumfb_store_d2(sc, 4, sc->sc_pfs->clock_params[clk_idx][0]);
368 		platinumfb_store_d2(sc, 5, sc->sc_pfs->clock_params[clk_idx][1]);
369 		platinumfb_store_d2(sc, 3, 2);
370 	}
371 
372 	delay(5000);
373 	platinumfb_store_d2(sc, 9, 0xa6);
374 }
375 
376 static void
377 platinumfb_dac_type(struct platinumfb_softc *sc)
378 {
379 	uint8_t dtype = 0;
380 
381 	platinumfb_write_cmap_reg(sc, PLATINUM_CMAP_ADDR_OFFSET, 0x40);
382 	dtype = platinumfb_read_cmap_reg(sc, PLATINUM_CMAP_D2_OFFSET);
383 
384 	switch (dtype) {
385 		case PLATINUM_DAC_0:
386 		case PLATINUM_DAC_1:
387 	 	/* do nothing */
388 	 	break;
389 	default:
390 		aprint_error_dev(sc->sc_dev, "unknown dac 0x%x, using 0x%x\n",
391 				 dtype, PLATINUM_DAC_0);
392 		dtype = PLATINUM_DAC_0;
393 		break;
394 	}
395 
396 	/* save type */
397 	sc->sc_dac_type = dtype;
398 }
399 
400 static void
401 platinumfb_memory_size(struct platinumfb_softc *sc)
402 {
403 	int i;
404 	off_t offset = PLATINUM_FB_BANK_SIZE;
405 	paddr_t total_vram = PLATINUM_FB_MIN_SIZE;
406 
407 	uint8_t test_val[] = {0x34, 0x56, 0x78};
408 	uint8_t bank[] = {0, 0, 0};
409 	uint8_t num_elems = sizeof(test_val)/sizeof(test_val[0]);
410 
411 	volatile uint8_t *fbuffer = mapiodev((paddr_t)sc->sc_fb, sc->sc_fb_size, false);
412 
413 	if (fbuffer == NULL)
414 		panic("platinumfb could not mapiodev");
415 
416 	/* turn on all banks of RAM */
417 	platinumfb_write_reg(sc, 16, (paddr_t)sc->sc_fb);
418 	platinumfb_write_reg(sc, 20, 0x1011);
419 	platinumfb_write_reg(sc, 24, 0);
420 
421 	/*
422 	 * write "unique" value to each bank of memory and read value
423 	 * back. if match assumes VRAM bank exists.  On the powermac 7200,
424 	 * bank0 is always there and soldered to motherboard, don't know
425 	 * if that is the case for others
426 	 */
427 	for (i = 0 ; i < num_elems; i++) {
428 		out8(fbuffer + offset, test_val[i]);
429 		out8(fbuffer + offset + 0x8, 0x0);
430 
431 		__asm volatile ("eieio; dcbf 0,%0"::"r"(&fbuffer[offset]):"memory");
432 
433 		bank[i] = fbuffer[offset] == test_val[i];
434 		total_vram += bank[i] * PLATINUM_FB_BANK_SIZE;
435 		offset += PLATINUM_FB_BANK_SIZE;
436 	}
437 
438 	/* save total vram or minimum */
439 	if (total_vram >= PLATINUM_FB_MIN_SIZE && total_vram <= PLATINUM_FB_MAX_SIZE) {
440 		sc->sc_vram = total_vram;
441 	} else {
442 		aprint_error_dev(sc->sc_dev,
443 				 "invalid VRAM size 0x%lx, using min 0x%x\n",
444 				 total_vram, PLATINUM_FB_MIN_SIZE);
445 		sc->sc_vram = PLATINUM_FB_MIN_SIZE;
446 	}
447 
448 	unmapiodev((paddr_t)fbuffer, sc->sc_fb_size);
449 }
450 
451 static int
452 platinumfb_match(device_t parent, cfdata_t cf, void *aux)
453 {
454 	struct confargs *ca = aux;
455 
456 	return (strcmp(ca->ca_name, "platinum") == 0);
457 }
458 
459 static void
460 platinumfb_attach(device_t parent, device_t self, void *aux)
461 {
462 	struct platinumfb_softc *sc = device_private(self);
463 	struct confargs *ca = aux;
464 	u_int *reg = ca->ca_reg;
465 
466 	sc->sc_dev = self;
467 	sc->sc_node = ca->ca_node;
468 
469 	sc->sc_reg = (uint8_t *)reg[0];
470 	sc->sc_reg_size = reg[1];
471 
472 	sc->sc_fb = (uint8_t *)reg[2];
473 	sc->sc_fb_size = PLATINUM_FB_MAX_SIZE;
474 
475 	sc->sc_cmap = (uint8_t *)PLATINUM_CMAP_BASE_ADDR;
476 	sc->sc_cmap_size = PLATINUM_CMAP_SIZE;
477 
478 	aprint_normal(" reg-addr 0x%08lx fb-addr 0x%08lx cmap-addr 0x%08lx\n",
479 		      (paddr_t)sc->sc_reg,
480 		      (paddr_t)sc->sc_fb,
481 		      (paddr_t)sc->sc_cmap);
482 
483 	config_finalize_register(sc->sc_dev, platinumfb_init);
484 }
485 
486 static void
487 platinumfb_init_cmap(struct platinumfb_softc *sc)
488 {
489 	int i;
490 	uint8_t tmp;
491 
492 	switch (sc->sc_cmode) {
493 	case PLATINUM_CMODE_8:
494 	default:
495 		/* R3G3B2 colormap */
496 		for (i = 0; i < 256; i++) {
497 			tmp = i & 0xe0;
498 			/*
499 			 * replicate bits so 0xe0 maps to a red value of 0xff
500 			 * in order to make white look actually white
501 			 */
502 			tmp |= (tmp >> 3) | (tmp >> 6);
503 			sc->sc_cmap_red[i] = tmp;
504 
505 			tmp = (i & 0x1c) << 3;
506 			tmp |= (tmp >> 3) | (tmp >> 6);
507 			sc->sc_cmap_green[i] = tmp;
508 
509 			tmp = (i & 0x03) << 6;
510 			tmp |= tmp >> 2;
511 			tmp |= tmp >> 4;
512 			sc->sc_cmap_blue[i] = tmp;
513 		}
514 		break;
515 
516 	case PLATINUM_CMODE_16:
517 		for (i = 0; i < 32; i++) {
518 			tmp = 255 * i / 32;
519 			sc->sc_cmap_red[i] = tmp;
520 			sc->sc_cmap_green[i] = tmp;
521 			sc->sc_cmap_blue[i] = tmp;
522 		}
523 		for (i = 32; i < 256; i++) {
524 			sc->sc_cmap_red[i] =  0;
525 			sc->sc_cmap_blue[i] = 0;
526 			sc->sc_cmap_green[i] = 0;
527 		}
528 		break;
529 
530 	case PLATINUM_CMODE_32:
531 		for (i = 0; i < 256; i++) {
532 			sc->sc_cmap_red[i] = i;
533 			sc->sc_cmap_green[i] = i;
534 			sc->sc_cmap_blue[i] = i;
535 		}
536 		break;
537 	}
538 }
539 
540 static void
541 platinumfb_restore_palette(struct platinumfb_softc *sc)
542 {
543 	int i;
544 
545 	for (i = 0; i < 256; i++) {
546 		platinumfb_putpalreg(sc, i, sc->sc_cmap_red[i],
547  			     sc->sc_cmap_green[i], sc->sc_cmap_blue[i]);
548 	}
549 }
550 
551 static int
552 platinumfb_init(device_t self)
553 {
554 	struct platinumfb_softc *sc = device_private(self);
555 	const struct videomode *mode = NULL;
556 	struct rasops_info *ri;
557 
558 	struct wsemuldisplaydev_attach_args aa;
559 	bool is_console = FALSE;
560 	long defattr;
561 
562 	int i;
563 
564 	/*
565 	 * become console if OF variable "output-device" is "screen" or
566 	 * contains "platinum", since normal OF video variables are unavailable
567 	 */
568 	int options;
569 	char output_device[128];
570 	options = OF_finddevice("/options");
571 	if (options == 0 ||
572 	    options == -1 ||
573 	    OF_getprop(options, "output-device", output_device,
574 		 sizeof(output_device)) == 0 ) {
575 		aprint_error_dev(sc->sc_dev,
576 		    "could not get output-device prop, assuming not console\n");
577 	} else {
578 		if (strstr(output_device,"platinum") ||
579 		    strcmp(output_device,"screen") == 0 ) {
580 			is_console = TRUE;
581 		}
582 	}
583 
584 	sc->sc_pfs = NULL;
585 	sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
586 	sc->sc_on = WSDISPLAYIO_VIDEO_ON;
587 
588 	/* determine vram memory and dac clock type */
589 	platinumfb_memory_size(sc);
590 	platinumfb_dac_type(sc);
591 
592 	aprint_normal_dev(sc->sc_dev,"is_console %d dac 0x%x vram 0x%x\n",
593 			  is_console, sc->sc_dac_type, sc->sc_vram);
594 
595 	for (i=0; i < sizeof(pfb_setting)/sizeof(pfb_setting[0]); i++) {
596 		if (strcmp(PLATINUM_FB_VMODE, pfb_setting[i]->vmode_name)==0) {
597 			mode = pick_mode_by_ref(pfb_setting[i]->width,
598 						pfb_setting[i]->height,
599  						pfb_setting[i]->freq);
600 			break;
601 		}
602 	}
603 
604 	if (!mode) {
605 		aprint_error_dev(sc->sc_dev,
606 		    "pick_mode_by_ref failed, using default\n");
607 		mode = pick_mode_by_ref(800, 600, 75);
608 	}
609 
610 	if (platinumfb_set_mode(sc, mode, PLATINUM_CONSOLE_DEPTH) != 0) {
611 		aprint_error_dev(sc->sc_dev, "platinumfb_set_mode failed\n");
612 		return 0;
613 	}
614 
615 	vcons_init(&sc->vd, sc, &platinumfb_defaultscreen,
616 		   &platinumfb_accessops);
617 	sc->vd.init_screen = platinumfb_init_screen;
618 
619 	ri = &platinumfb_console_screen.scr_ri;
620 	vcons_init_screen(&sc->vd, &platinumfb_console_screen, 1, &defattr);
621 
622 	platinumfb_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
623 
624 	platinumfb_defaultscreen.textops = &ri->ri_ops;
625 	platinumfb_defaultscreen.capabilities = ri->ri_caps;
626 	platinumfb_defaultscreen.nrows = ri->ri_rows;
627 	platinumfb_defaultscreen.ncols = ri->ri_cols;
628 
629 	if (is_console) {
630 		wsdisplay_cnattach(&platinumfb_defaultscreen, ri, 0, 0,
631 		    defattr);
632 		vcons_replay_msgbuf(&platinumfb_console_screen);
633 	}
634 
635 	aa.console = is_console;
636 	aa.scrdata = &platinumfb_screenlist;
637 	aa.accessops = &platinumfb_accessops;
638 	aa.accesscookie = &sc->vd;
639 
640 	config_found(self, &aa, wsemuldisplaydevprint);
641 
642 	return 0;
643 }
644 
645 static void
646 platinumfb_set_hardware(struct platinumfb_softc *sc)
647 {
648 	int i;
649 	bool one_bank = sc->sc_vram == PLATINUM_FB_BANK_SIZE;
650 
651 	/* now start programming the chip */
652 	platinumfb_write_reg(sc, 24, 7);    /* turn off display */
653 
654 	for (i = 0; i < 26; ++i)
655 		platinumfb_write_reg(sc, i+32, sc->sc_pfs->regs[i]);
656 
657 	platinumfb_write_reg(sc, 26+32, one_bank ?
658 			 sc->sc_pfs->offset[sc->sc_cmode] + 4 - sc->sc_cmode :
659 			 sc->sc_pfs->offset[sc->sc_cmode]);
660 
661 	/*
662 	 * reg 16 apparently needs an address 0x10 less the where frame
663 	 * buffer/ri_bits start for console text to be aligned.  In
664 	 * addition, X memory maps (mmap) the frame buffer starting on
665 	 * page boundaries.  So to get both X and console text aligned we
666 	 * start at the first page up from sc_fb[0]. Starting at sc_fb[0]
667 	 * did work on my machine but not sure if this negative offset
668 	 * would be problematic elsewhere.
669 	 *
670 	 * Not sure why linux used different fb offsets for each mode, as
671 	 * any addresses seemed to work as long as relative difference was
672 	 * 0x10.
673 	 */
674 	platinumfb_write_reg(sc, 16, platinumfb_page_align_up(sc) - 0x10);
675 
676 	platinumfb_write_reg(sc, 18, sc->sc_pfs->pitch[sc->sc_cmode]);
677 
678 	/*
679 	 * XXX register 19 setting looks wrong for 1 bank & 32 bpp.
680 	 * 512x384 is only resolution that would use such a setting, but
681 	 * that is not currently in videomodes.c
682 	 */
683 	if (sc->sc_cmode == PLATINUM_CMODE_32 &&
684 	   (sc->sc_pfs->macmode == 1 || sc->sc_pfs->macmode == 2))
685 		aprint_error_dev(sc->sc_dev,
686 		    "platinumfb reg19 array out-of-bounds");
687 
688 	platinumfb_write_reg(sc, 19, one_bank ?
689 	    sc->sc_pfs->mode[sc->sc_cmode+1] : /* XXX fix this for 32 bpp */
690 	    sc->sc_pfs->mode[sc->sc_cmode]);
691 
692 	platinumfb_write_reg(sc, 20, one_bank ? 0x11 : 0x1011);
693 	platinumfb_write_reg(sc, 21, 0x100);
694 	platinumfb_write_reg(sc, 22, 1);
695 	platinumfb_write_reg(sc, 23, 1);
696 	platinumfb_write_reg(sc, 26, 0xc00);
697 	platinumfb_write_reg(sc, 27, 0x235);
698 	/* platinumfb_write_reg(sc, 27, 0x2aa); */
699 
700 	platinumfb_store_d2(sc, 0,
701 	    one_bank ? sc->sc_pfs->dacula_ctrl[sc->sc_cmode] & 0xf :
702 	               sc->sc_pfs->dacula_ctrl[sc->sc_cmode]);
703 	platinumfb_store_d2(sc, 1, 4);
704 	platinumfb_store_d2(sc, 2, 0);
705 
706 	platinumfb_set_clock(sc);
707 
708 	platinumfb_write_reg(sc, 24, 0);  /* turn display on */
709 }
710 
711 static int
712 platinumfb_set_mode(struct platinumfb_softc *sc,
713     const struct videomode *mode, int depth)
714 {
715 	int i;
716 
717 	/* first find the parameter for the mode register */
718 	i = 0;
719 	while((i < __arraycount(pfb_setting)) &&
720 	      (strcmp(mode->name, pfb_setting[i]->vmode_name) != 0))
721 	    i++;
722 
723 	if (i >= __arraycount(pfb_setting)) {
724 		aprint_error_dev(sc->sc_dev,
725 		    "Can't find a mode register value for %s\n",
726 				 mode->name);
727 		return EINVAL;
728 	}
729 
730 	/* found a mode */
731 	sc->sc_pfs = pfb_setting[i];
732 
733 	/* determine depth settings */
734 	switch (depth) {
735 	case 8:
736 	default:
737 		sc->sc_depth = 8;
738 		sc->sc_cmode = PLATINUM_CMODE_8;
739 		break;
740 	case 15:
741 	case 16:
742 		/* 15 bpp but use 16 so X/wsfb works */
743 		sc->sc_depth = 16;
744 		sc->sc_cmode = PLATINUM_CMODE_16;
745 		break;
746 	case 24:
747 	case 32:
748 		/* 24 bpp but use 32 so X/wsfb works */
749 		sc->sc_depth = 32;
750 		sc->sc_cmode = PLATINUM_CMODE_32;
751 		break;
752 	}
753 
754 	sc->sc_modereg = sc->sc_pfs->macmode;
755 	sc->sc_videomode = mode;
756 	sc->sc_height = mode->vdisplay;
757 	sc->sc_width = mode->hdisplay;
758 	sc->sc_linebytes = sc->sc_width * (1 << sc->sc_cmode) + platinumfb_line_tweak(sc);
759 
760 	/* check if we have enough video memory */
761 	if (sc->sc_height * sc->sc_linebytes > sc->sc_vram - PAGE_SIZE) {
762 		aprint_error_dev(sc->sc_dev, "Not enough video RAM for %s\n",
763  			     mode->name);
764 		return EINVAL;
765 	}
766 
767 	/* set up and write colormap */
768 	platinumfb_init_cmap(sc);
769 	platinumfb_restore_palette(sc);
770 
771 	/* set hardware registers */
772 	platinumfb_set_hardware(sc);
773 
774 	aprint_normal_dev(sc->sc_dev, "switched to %s in %d bit color\n",
775 			  sc->sc_pfs->vmode_name, sc->sc_depth);
776 	return 0;
777 }
778 
779 static int
780 platinumfb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
781 	struct lwp *l)
782 {
783 	struct vcons_data *vd = v;
784 	struct platinumfb_softc *sc = vd->cookie;
785 	struct wsdisplay_fbinfo *wdf;
786 	struct vcons_screen *ms = vd->active;
787 	int i;
788 
789 	switch (cmd) {
790 	case WSDISPLAYIO_GTYPE:
791 		*(u_int *)data = WSDISPLAY_TYPE_PLATINUM;
792 		return 0;
793 
794 	case WSDISPLAYIO_GINFO:
795 		wdf = (void *)data;
796 		wdf->height = ms->scr_ri.ri_height;
797 		wdf->width = ms->scr_ri.ri_width;
798 		wdf->depth = ms->scr_ri.ri_depth;
799 		wdf->cmsize = 256;
800 		return 0;
801 
802 	case WSDISPLAYIO_GVIDEO:
803 		*(int *)data = sc->sc_on;
804 		return 0;
805 
806 	case WSDISPLAYIO_SVIDEO:
807 		/*
808 		 * poor man's screen blanking, just write zeros to colormap
809 		 * registers but don't save in softc.
810 		 */
811 		if (*(int *)data != sc->sc_on) {
812 			sc->sc_on = (sc->sc_on == WSDISPLAYIO_VIDEO_ON ?
813 				 WSDISPLAYIO_VIDEO_OFF : WSDISPLAYIO_VIDEO_ON);
814 
815 			/* XXX need to lock colormap? */
816 			if (sc->sc_on == WSDISPLAYIO_VIDEO_OFF) {
817 				for (i=0; i < 256; i++)
818 					platinumfb_putpalreg(sc, i, 0, 0, 0);
819 			} else {
820 				platinumfb_restore_palette(sc);
821 			}
822 		}
823 		return 0;
824 
825 	case WSDISPLAYIO_GETCMAP:
826 		if (sc->sc_cmode == PLATINUM_CMODE_8) {
827 			return platinumfb_getcmap(sc,
828 			     (struct wsdisplay_cmap *)data);
829 		} else
830 			return 0;
831 
832 	case WSDISPLAYIO_PUTCMAP:
833 		if (sc->sc_cmode == PLATINUM_CMODE_8) {
834 			return platinumfb_putcmap(sc,
835 			    (struct wsdisplay_cmap *)data);
836 		} else
837 			return 0;
838 
839 	case WSDISPLAYIO_SMODE: {
840 		int new_mode = *(int*)data;
841 
842 		if (new_mode != sc->sc_mode) {
843 			int new_depth = new_mode == WSDISPLAYIO_MODE_EMUL ?
844 			    PLATINUM_CONSOLE_DEPTH : PLATINUM_FB_DEPTH;
845 
846 			switch(new_mode) {
847 			/*
848 			 * XXX - not sure how this is supposed to work for
849 			 * switching bpp between console and X, but cases with
850 			 * (EMUL MAPPED) or (EMUL MAPPED DUMBFB) work, but
851 			 * (EMUL DUMBFB) garbles screen for some reason.
852 			 */
853 			case WSDISPLAYIO_MODE_EMUL:
854 			case WSDISPLAYIO_MODE_MAPPED:
855 			/* case WSDISPLAYIO_MODE_DUMBFB: XXX */
856 
857 				/* in case screen is "blanked" */
858 				platinumfb_restore_palette(sc);
859 
860 				sc->sc_mode = new_mode;
861 
862 				platinumfb_set_mode(sc, sc->sc_videomode, new_depth);
863 				platinumfb_set_rasops(sc, &ms->scr_ri, true);
864 
865 				if (new_mode == WSDISPLAYIO_MODE_EMUL)
866 					vcons_redraw_screen(ms);
867 			}
868 		}
869 
870 		return 0;
871 	}
872 
873 	case WSDISPLAYIO_LINEBYTES:
874 		*(u_int *)data = sc->sc_linebytes;
875 		return 0;
876 
877 	case WSDISPLAYIO_GET_FBINFO: {
878 		struct wsdisplayio_fbinfo *fbi = data;
879 		return wsdisplayio_get_fbinfo(&ms->scr_ri, fbi);
880 	}
881 
882 	}
883 	return EPASSTHROUGH;
884 }
885 
886 static paddr_t
887 platinumfb_mmap(void *v, void *vs, off_t offset, int prot)
888 {
889 	struct vcons_data *vd = v;
890 	struct platinumfb_softc *sc = vd->cookie;
891 	paddr_t pa = -1;
892 	paddr_t fb_aligned = platinumfb_page_align_up(sc);
893 	paddr_t fb_vram = sc->sc_vram - (fb_aligned - (paddr_t)sc->sc_fb);
894 
895 	/* XXX need to worry about superuser or mapping other registers? */
896 
897 	if (offset >= 0 && offset < fb_vram)
898 		pa = fb_aligned + offset;
899 
900 	return pa;
901 }
902 
903 static void platinumfb_set_rasops(struct platinumfb_softc *sc,
904 				  struct rasops_info *ri,
905 				  int existing)
906 {
907 	memset(ri, 0, sizeof(struct rasops_info));
908 
909 	ri->ri_depth = sc->sc_depth;
910 	ri->ri_width = sc->sc_width;
911 	ri->ri_height = sc->sc_height;
912 	ri->ri_stride = sc->sc_linebytes;
913 	ri->ri_bits = (u_char*)platinumfb_page_align_up(sc);
914 	ri->ri_flg = RI_FULLCLEAR;
915 
916 	if (existing)
917 		ri->ri_flg |= RI_CLEAR;
918 
919 	switch (sc->sc_cmode) {
920 	case PLATINUM_CMODE_8:
921 	default:
922 		ri->ri_flg |= RI_ENABLE_ALPHA | RI_8BIT_IS_RGB;
923 
924 		break;
925 	case PLATINUM_CMODE_16:
926 		if (strcmp(sc->sc_pfs->vmode_name, "640x480x60") == 0 ||
927 		    strcmp(sc->sc_pfs->vmode_name, "800x600x75") == 0 )
928 			ri->ri_flg |= RI_ENABLE_ALPHA;
929 
930 		ri->ri_rnum = 5;
931 		ri->ri_rpos = 10;
932 		ri->ri_gnum = 5;
933 		ri->ri_gpos = 5;
934 		ri->ri_bnum = 5;
935 		ri->ri_bpos = 0;
936 
937 		break;
938 	case PLATINUM_CMODE_32:
939 		if (strcmp(sc->sc_pfs->vmode_name, "640x480x60") == 0 ||
940 		    strcmp(sc->sc_pfs->vmode_name, "800x600x75") == 0 )
941 			ri->ri_flg |= RI_ENABLE_ALPHA;
942 
943 		ri->ri_rnum = 8;
944 		ri->ri_rpos = 16;
945 		ri->ri_gnum = 8;
946 		ri->ri_gpos = 8;
947 		ri->ri_bnum = 8;
948 		ri->ri_bpos = 0;
949 
950 		break;
951 	}
952 
953 	rasops_init(ri, 0, 0);
954 	ri->ri_caps = WSSCREEN_WSCOLORS;
955 
956 	rasops_reconfig(ri, sc->sc_height / ri->ri_font->fontheight,
957 		    sc->sc_width / ri->ri_font->fontwidth);
958 
959 }
960 
961 static void
962 platinumfb_init_screen(void *cookie, struct vcons_screen *scr,
963     int existing, long *defattr)
964 {
965 	struct platinumfb_softc *sc = cookie;
966 	struct rasops_info *ri = &scr->scr_ri;
967 
968 	scr->scr_flags |= VCONS_DONT_READ;
969 
970 	platinumfb_set_rasops(sc, ri, existing);
971 
972 	ri->ri_hw = scr;
973 }
974 
975 static int
976 platinumfb_putcmap(struct platinumfb_softc *sc, struct wsdisplay_cmap *cm)
977 {
978 	u_char *r, *g, *b;
979 	u_int index = cm->index;
980 	u_int count = cm->count;
981 	int i, error;
982 	u_char rbuf[256], gbuf[256], bbuf[256];
983 
984 	if (cm->index >= 256 || cm->count > 256 ||
985 	    (cm->index + cm->count) > 256)
986 		return EINVAL;
987 	error = copyin(cm->red, &rbuf[index], count);
988 	if (error)
989 		return error;
990 	error = copyin(cm->green, &gbuf[index], count);
991 	if (error)
992 		return error;
993 	error = copyin(cm->blue, &bbuf[index], count);
994 	if (error)
995 		return error;
996 
997 	memcpy(&sc->sc_cmap_red[index], &rbuf[index], count);
998 	memcpy(&sc->sc_cmap_green[index], &gbuf[index], count);
999 	memcpy(&sc->sc_cmap_blue[index], &bbuf[index], count);
1000 
1001 	/* write colormap registers if not currently blanked */
1002 	if (sc->sc_on == WSDISPLAYIO_VIDEO_ON) {
1003 		r = &sc->sc_cmap_red[index];
1004 		g = &sc->sc_cmap_green[index];
1005 		b = &sc->sc_cmap_blue[index];
1006 
1007 		for (i = 0; i < count; i++) {
1008 			platinumfb_putpalreg(sc, index, *r, *g, *b);
1009 			index++;
1010 			r++, g++, b++;
1011 		}
1012 	}
1013 
1014 	return 0;
1015 }
1016 
1017 static int
1018 platinumfb_getcmap(struct platinumfb_softc *sc, struct wsdisplay_cmap *cm)
1019 {
1020 	u_int index = cm->index;
1021 	u_int count = cm->count;
1022 	int error;
1023 
1024 	if (index >= 255 || count > 256 || index + count > 256)
1025 		return EINVAL;
1026 
1027 	error = copyout(&sc->sc_cmap_red[index],   cm->red,   count);
1028 	if (error)
1029 		return error;
1030 	error = copyout(&sc->sc_cmap_green[index], cm->green, count);
1031 	if (error)
1032 		return error;
1033 	error = copyout(&sc->sc_cmap_blue[index],  cm->blue,  count);
1034 	if (error)
1035 		return error;
1036 
1037  	return 0;
1038 }
1039