platinumfb.c revision 1.1 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.1 2016/06/10 21:32:46 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 static inline uint32_t platinumfb_read_reg(struct platinumfb_softc *, int);
238 static inline void platinumfb_write_cmap_reg(struct platinumfb_softc *,
239 int, uint8_t);
240 static inline uint8_t platinumfb_read_cmap_reg(struct platinumfb_softc *, int);
241 static inline void platinumfb_store_d2(struct platinumfb_softc *,
242 uint8_t, uint8_t);
243
244 struct wsscreen_descr platinumfb_defaultscreen = {
245 "default",
246 0, 0,
247 NULL,
248 8, 16,
249 WSSCREEN_WSCOLORS | WSSCREEN_HILIT,
250 NULL,
251 };
252
253 const struct wsscreen_descr *_platinumfb_scrlist[] = {
254 &platinumfb_defaultscreen,
255 /* XXX other formats, graphics screen? */
256 };
257
258 struct wsscreen_list platinumfb_screenlist = {
259 sizeof(_platinumfb_scrlist) / sizeof(struct wsscreen_descr *),
260 _platinumfb_scrlist
261 };
262
263 struct wsdisplay_accessops platinumfb_accessops = {
264 platinumfb_ioctl,
265 platinumfb_mmap,
266 NULL,
267 NULL,
268 NULL,
269 NULL, /* load_font */
270 NULL, /* polls */
271 NULL, /* scroll */
272 };
273
274 static inline void
275 platinumfb_write_reg(struct platinumfb_softc *sc, int reg, uint32_t val)
276 {
277 out32(sc->sc_reg + PLATINUM_REG_OFFSET_ADDR(reg), val);
278 }
279
280 static inline uint32_t
281 platinumfb_read_reg(struct platinumfb_softc *sc, int reg)
282 {
283 return in32(sc->sc_reg + PLATINUM_REG_OFFSET_ADDR(reg));
284 }
285
286 static inline void
287 platinumfb_write_cmap_reg(struct platinumfb_softc *sc,
288 int reg_offset, uint8_t val)
289 {
290 out8(sc->sc_cmap + reg_offset, val);
291 }
292
293 static inline uint8_t
294 platinumfb_read_cmap_reg(struct platinumfb_softc *sc, int reg_offset)
295 {
296 return in8(sc->sc_cmap + reg_offset);
297 }
298
299 static inline void
300 platinumfb_store_d2(struct platinumfb_softc *sc,
301 uint8_t a, uint8_t d)
302 {
303 platinumfb_write_cmap_reg(sc, PLATINUM_CMAP_ADDR_OFFSET, a + 32);
304 platinumfb_write_cmap_reg(sc, PLATINUM_CMAP_D2_OFFSET, d);
305 }
306
307 static void
308 platinumfb_putpalreg(struct platinumfb_softc *sc,
309 uint8_t reg, uint8_t r, uint8_t g, uint8_t b)
310 {
311 platinumfb_write_cmap_reg(sc, PLATINUM_CMAP_ADDR_OFFSET, reg);
312 platinumfb_write_cmap_reg(sc, PLATINUM_CMAP_LUT_OFFSET, r);
313 platinumfb_write_cmap_reg(sc, PLATINUM_CMAP_LUT_OFFSET, g);
314 platinumfb_write_cmap_reg(sc, PLATINUM_CMAP_LUT_OFFSET, b);
315 }
316
317 static uint32_t
318 platinumfb_line_tweak(struct platinumfb_softc *sc)
319 {
320 /* bytes per line adjustment depending on resolution and depth */
321 if (sc->sc_cmode > PLATINUM_CMODE_8 &&
322 strcmp(sc->sc_pfs->vmode_name, "832x624x75") == 0)
323 return 0x10;
324 else
325 return 0x20;
326 }
327
328 static paddr_t
329 platinumfb_page_align_up(struct platinumfb_softc *sc)
330 {
331 /* round up framebuffer address to the next highest page */
332 paddr_t addr = (paddr_t)sc->sc_fb;
333 paddr_t ret = round_page(addr);
334
335 if (ret == addr)
336 ret = round_page(addr + 1);
337
338 return ret;
339 }
340
341 /* 2 versions of platinum clock, older one uses clock[1] and
342 * freq = 14.3Mhz * c0 / (c1 & 0x1f) / (1 << (c1 >> 5))
343 * newer one uses clock[0] and
344 * freq = 15Mhz * c0 / ((c1 & 0x1f) + 2) / (1 << (c1 >> 5))
345 */
346 static void
347 platinumfb_set_clock(struct platinumfb_softc *sc)
348 {
349 uint8_t clk_idx = sc->sc_dac_type == PLATINUM_DAC_1 ? 1 : 0;
350 uint8_t d2;
351
352 platinumfb_store_d2(sc, 6, 0xc6);
353
354 platinumfb_write_cmap_reg(sc, PLATINUM_CMAP_ADDR_OFFSET, 3+32);
355 d2 = platinumfb_read_cmap_reg(sc, PLATINUM_CMAP_D2_OFFSET);
356
357 if (d2 == 2) {
358 platinumfb_store_d2(sc, 7, sc->sc_pfs->clock_params[clk_idx][0]);
359 platinumfb_store_d2(sc, 8, sc->sc_pfs->clock_params[clk_idx][1]);
360 platinumfb_store_d2(sc, 3, 3);
361 } else {
362 platinumfb_store_d2(sc, 4, sc->sc_pfs->clock_params[clk_idx][0]);
363 platinumfb_store_d2(sc, 5, sc->sc_pfs->clock_params[clk_idx][1]);
364 platinumfb_store_d2(sc, 3, 2);
365 }
366
367 delay(5000);
368 platinumfb_store_d2(sc, 9, 0xa6);
369 }
370
371 static void
372 platinumfb_dac_type(struct platinumfb_softc *sc)
373 {
374 uint8_t dtype = 0;
375
376 platinumfb_write_cmap_reg(sc, PLATINUM_CMAP_ADDR_OFFSET, 0x40);
377 dtype = platinumfb_read_cmap_reg(sc, PLATINUM_CMAP_D2_OFFSET);
378
379 switch (dtype) {
380 case PLATINUM_DAC_0:
381 case PLATINUM_DAC_1:
382 /* do nothing */
383 break;
384 default:
385 aprint_error_dev(sc->sc_dev, "unknown dac 0x%x, using 0x%x\n",
386 dtype, PLATINUM_DAC_0);
387 dtype = PLATINUM_DAC_0;
388 break;
389 }
390
391 /* save type */
392 sc->sc_dac_type = dtype;
393 }
394
395 static void
396 platinumfb_memory_size(struct platinumfb_softc *sc)
397 {
398 int i;
399 off_t offset = PLATINUM_FB_BANK_SIZE;
400 paddr_t total_vram = PLATINUM_FB_MIN_SIZE;
401
402 uint8_t test_val[] = {0x34, 0x56, 0x78};
403 uint8_t bank[] = {0, 0, 0};
404 uint8_t num_elems = sizeof(test_val)/sizeof(test_val[0]);
405
406 volatile uint8_t *fbuffer = mapiodev((paddr_t)sc->sc_fb, sc->sc_fb_size, false);
407
408 if (fbuffer == NULL)
409 panic("platinumfb could not mapiodev");
410
411 /* turn on all banks of RAM */
412 platinumfb_write_reg(sc, 16, (paddr_t)sc->sc_fb);
413 platinumfb_write_reg(sc, 20, 0x1011);
414 platinumfb_write_reg(sc, 24, 0);
415
416 /*
417 * write "unique" value to each bank of memory and read value
418 * back. if match assumes VRAM bank exists. On the powermac 7200,
419 * bank0 is always there and soldered to motherboard, don't know
420 * if that is the case for others
421 */
422 for (i = 0 ; i < num_elems; i++) {
423 out8(fbuffer + offset, test_val[i]);
424 out8(fbuffer + offset + 0x8, 0x0);
425
426 __asm volatile ("eieio; dcbf 0,%0"::"r"(&fbuffer[offset]):"memory");
427
428 bank[i] = fbuffer[offset] == test_val[i];
429 total_vram += bank[i] * PLATINUM_FB_BANK_SIZE;
430 offset += PLATINUM_FB_BANK_SIZE;
431 }
432
433 /* save total vram or minimum */
434 if (total_vram >= PLATINUM_FB_MIN_SIZE && total_vram <= PLATINUM_FB_MAX_SIZE) {
435 sc->sc_vram = total_vram;
436 } else {
437 aprint_error_dev(sc->sc_dev,
438 "invalid VRAM size 0x%lx, using min 0x%x\n",
439 total_vram, PLATINUM_FB_MIN_SIZE);
440 sc->sc_vram = PLATINUM_FB_MIN_SIZE;
441 }
442
443 unmapiodev((paddr_t)fbuffer, sc->sc_fb_size);
444 }
445
446 static int
447 platinumfb_match(device_t parent, cfdata_t cf, void *aux)
448 {
449 struct confargs *ca = aux;
450
451 return (strcmp(ca->ca_name, "platinum") == 0);
452 }
453
454 static void
455 platinumfb_attach(device_t parent, device_t self, void *aux)
456 {
457 struct platinumfb_softc *sc = device_private(self);
458 struct confargs *ca = aux;
459 u_int *reg = ca->ca_reg;
460
461 sc->sc_dev = self;
462 sc->sc_node = ca->ca_node;
463
464 sc->sc_reg = (uint8_t *)reg[0];
465 sc->sc_reg_size = reg[1];
466
467 sc->sc_fb = (uint8_t *)reg[2];
468 sc->sc_fb_size = PLATINUM_FB_MAX_SIZE;
469
470 sc->sc_cmap = (uint8_t *)PLATINUM_CMAP_BASE_ADDR;
471 sc->sc_cmap_size = PLATINUM_CMAP_SIZE;
472
473 aprint_normal(" reg-addr 0x%08lx fb-addr 0x%08lx cmap-addr 0x%08lx\n",
474 (paddr_t)sc->sc_reg,
475 (paddr_t)sc->sc_fb,
476 (paddr_t)sc->sc_cmap);
477
478 config_finalize_register(sc->sc_dev, platinumfb_init);
479 }
480
481 static void
482 platinumfb_init_cmap(struct platinumfb_softc *sc)
483 {
484 int i;
485 uint8_t tmp;
486
487 switch (sc->sc_cmode) {
488 case PLATINUM_CMODE_8:
489 default:
490 /* R3G3B2 colormap */
491 for (i = 0; i < 256; i++) {
492 tmp = i & 0xe0;
493 /*
494 * replicate bits so 0xe0 maps to a red value of 0xff
495 * in order to make white look actually white
496 */
497 tmp |= (tmp >> 3) | (tmp >> 6);
498 sc->sc_cmap_red[i] = tmp;
499
500 tmp = (i & 0x1c) << 3;
501 tmp |= (tmp >> 3) | (tmp >> 6);
502 sc->sc_cmap_green[i] = tmp;
503
504 tmp = (i & 0x03) << 6;
505 tmp |= tmp >> 2;
506 tmp |= tmp >> 4;
507 sc->sc_cmap_blue[i] = tmp;
508 }
509 break;
510
511 case PLATINUM_CMODE_16:
512 for (i = 0; i < 32; i++) {
513 tmp = 255 * i / 32;
514 sc->sc_cmap_red[i] = tmp;
515 sc->sc_cmap_green[i] = tmp;
516 sc->sc_cmap_blue[i] = tmp;
517 }
518 for (i = 32; i < 256; i++) {
519 sc->sc_cmap_red[i] = 0;
520 sc->sc_cmap_blue[i] = 0;
521 sc->sc_cmap_green[i] = 0;
522 }
523 break;
524
525 case PLATINUM_CMODE_32:
526 for (i = 0; i < 256; i++) {
527 sc->sc_cmap_red[i] = i;
528 sc->sc_cmap_green[i] = i;
529 sc->sc_cmap_blue[i] = i;
530 }
531 break;
532 }
533 }
534
535 static void
536 platinumfb_restore_palette(struct platinumfb_softc *sc)
537 {
538 int i;
539
540 for (i = 0; i < 256; i++) {
541 platinumfb_putpalreg(sc, i, sc->sc_cmap_red[i],
542 sc->sc_cmap_green[i], sc->sc_cmap_blue[i]);
543 }
544 }
545
546 static int
547 platinumfb_init(device_t self)
548 {
549 struct platinumfb_softc *sc = device_private(self);
550 const struct videomode *mode = NULL;
551 struct rasops_info *ri;
552
553 struct wsemuldisplaydev_attach_args aa;
554 bool is_console = FALSE;
555 long defattr;
556
557 int i;
558
559 /*
560 * become console if OF variable "output-device" is "screen" or
561 * contains "platinum", since normal OF video variables are unavailable
562 */
563 int options;
564 char output_device[128];
565 options = OF_finddevice("/options");
566 if (options == 0 ||
567 options == -1 ||
568 OF_getprop(options, "output-device", output_device,
569 sizeof(output_device)) == 0 ) {
570 aprint_error_dev(sc->sc_dev,
571 "could not get output-device prop, assuming not console\n");
572 } else {
573 if (strstr(output_device,"platinum") ||
574 strcmp(output_device,"screen") == 0 ) {
575 is_console = TRUE;
576 }
577 }
578
579 sc->sc_pfs = NULL;
580 sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
581 sc->sc_on = WSDISPLAYIO_VIDEO_ON;
582
583 /* determine vram memory and dac clock type */
584 platinumfb_memory_size(sc);
585 platinumfb_dac_type(sc);
586
587 aprint_normal_dev(sc->sc_dev,"is_console %d dac 0x%x vram 0x%x\n",
588 is_console, sc->sc_dac_type, sc->sc_vram);
589
590 for (i=0; i < sizeof(pfb_setting)/sizeof(pfb_setting[0]); i++) {
591 if (strcmp(PLATINUM_FB_VMODE, pfb_setting[i]->vmode_name)==0) {
592 mode = pick_mode_by_ref(pfb_setting[i]->width,
593 pfb_setting[i]->height,
594 pfb_setting[i]->freq);
595 break;
596 }
597 }
598
599 if (!mode) {
600 aprint_error_dev(sc->sc_dev,
601 "pick_mode_by_ref failed, using default\n");
602 mode = pick_mode_by_ref(800, 600, 75);
603 }
604
605 if (platinumfb_set_mode(sc, mode, PLATINUM_CONSOLE_DEPTH) != 0) {
606 aprint_error_dev(sc->sc_dev, "platinumfb_set_mode failed\n");
607 return 0;
608 }
609
610 vcons_init(&sc->vd, sc, &platinumfb_defaultscreen,
611 &platinumfb_accessops);
612 sc->vd.init_screen = platinumfb_init_screen;
613
614 ri = &platinumfb_console_screen.scr_ri;
615 vcons_init_screen(&sc->vd, &platinumfb_console_screen, 1, &defattr);
616
617 platinumfb_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
618
619 platinumfb_defaultscreen.textops = &ri->ri_ops;
620 platinumfb_defaultscreen.capabilities = ri->ri_caps;
621 platinumfb_defaultscreen.nrows = ri->ri_rows;
622 platinumfb_defaultscreen.ncols = ri->ri_cols;
623
624 if (is_console) {
625 wsdisplay_cnattach(&platinumfb_defaultscreen, ri, 0, 0,
626 defattr);
627 vcons_replay_msgbuf(&platinumfb_console_screen);
628 }
629
630 aa.console = is_console;
631 aa.scrdata = &platinumfb_screenlist;
632 aa.accessops = &platinumfb_accessops;
633 aa.accesscookie = &sc->vd;
634
635 config_found(self, &aa, wsemuldisplaydevprint);
636
637 return 0;
638 }
639
640 static void
641 platinumfb_set_hardware(struct platinumfb_softc *sc)
642 {
643 int i;
644 bool one_bank = sc->sc_vram == PLATINUM_FB_BANK_SIZE;
645
646 /* now start programming the chip */
647 platinumfb_write_reg(sc, 24, 7); /* turn off display */
648
649 for (i = 0; i < 26; ++i)
650 platinumfb_write_reg(sc, i+32, sc->sc_pfs->regs[i]);
651
652 platinumfb_write_reg(sc, 26+32, one_bank ?
653 sc->sc_pfs->offset[sc->sc_cmode] + 4 - sc->sc_cmode :
654 sc->sc_pfs->offset[sc->sc_cmode]);
655
656 /*
657 * reg 16 apparently needs an address 0x10 less the where frame
658 * buffer/ri_bits start for console text to be aligned. In
659 * addition, X memory maps (mmap) the frame buffer starting on
660 * page boundaries. So to get both X and console text aligned we
661 * start at the first page up from sc_fb[0]. Starting at sc_fb[0]
662 * did work on my machine but not sure if this negative offset
663 * would be problematic elsewhere.
664 *
665 * Not sure why linux used different fb offsets for each mode, as
666 * any addresses seemed to work as long as relative difference was
667 * 0x10.
668 */
669 platinumfb_write_reg(sc, 16, platinumfb_page_align_up(sc) - 0x10);
670
671 platinumfb_write_reg(sc, 18, sc->sc_pfs->pitch[sc->sc_cmode]);
672
673 /*
674 * XXX register 19 setting looks wrong for 1 bank & 32 bpp.
675 * 512x384 is only resolution that would use such a setting, but
676 * that is not currently in videomodes.c
677 */
678 if (sc->sc_cmode == PLATINUM_CMODE_32 &&
679 (sc->sc_pfs->macmode == 1 || sc->sc_pfs->macmode == 2))
680 aprint_error_dev(sc->sc_dev,
681 "platinumfb reg19 array out-of-bounds");
682
683 platinumfb_write_reg(sc, 19, one_bank ?
684 sc->sc_pfs->mode[sc->sc_cmode+1] : /* XXX fix this for 32 bpp */
685 sc->sc_pfs->mode[sc->sc_cmode]);
686
687 platinumfb_write_reg(sc, 20, one_bank ? 0x11 : 0x1011);
688 platinumfb_write_reg(sc, 21, 0x100);
689 platinumfb_write_reg(sc, 22, 1);
690 platinumfb_write_reg(sc, 23, 1);
691 platinumfb_write_reg(sc, 26, 0xc00);
692 platinumfb_write_reg(sc, 27, 0x235);
693 /* platinumfb_write_reg(sc, 27, 0x2aa); */
694
695 platinumfb_store_d2(sc, 0,
696 one_bank ? sc->sc_pfs->dacula_ctrl[sc->sc_cmode] & 0xf :
697 sc->sc_pfs->dacula_ctrl[sc->sc_cmode]);
698 platinumfb_store_d2(sc, 1, 4);
699 platinumfb_store_d2(sc, 2, 0);
700
701 platinumfb_set_clock(sc);
702
703 platinumfb_write_reg(sc, 24, 0); /* turn display on */
704 }
705
706 static int
707 platinumfb_set_mode(struct platinumfb_softc *sc,
708 const struct videomode *mode, int depth)
709 {
710 int i;
711
712 /* first find the parameter for the mode register */
713 i = 0;
714 while((i < __arraycount(pfb_setting)) &&
715 (strcmp(mode->name, pfb_setting[i]->vmode_name) != 0))
716 i++;
717
718 if (i >= __arraycount(pfb_setting)) {
719 aprint_error_dev(sc->sc_dev,
720 "Can't find a mode register value for %s\n",
721 mode->name);
722 return EINVAL;
723 }
724
725 /* found a mode */
726 sc->sc_pfs = pfb_setting[i];
727
728 /* determine depth settings */
729 switch (depth) {
730 case 8:
731 default:
732 sc->sc_depth = 8;
733 sc->sc_cmode = PLATINUM_CMODE_8;
734 break;
735 case 15:
736 case 16:
737 /* 15 bpp but use 16 so X/wsfb works */
738 sc->sc_depth = 16;
739 sc->sc_cmode = PLATINUM_CMODE_16;
740 break;
741 case 24:
742 case 32:
743 /* 24 bpp but use 32 so X/wsfb works */
744 sc->sc_depth = 32;
745 sc->sc_cmode = PLATINUM_CMODE_32;
746 break;
747 }
748
749 sc->sc_modereg = sc->sc_pfs->macmode;
750 sc->sc_videomode = mode;
751 sc->sc_height = mode->vdisplay;
752 sc->sc_width = mode->hdisplay;
753 sc->sc_linebytes = sc->sc_width * (1 << sc->sc_cmode) + platinumfb_line_tweak(sc);
754
755 /* check if we have enough video memory */
756 if (sc->sc_height * sc->sc_linebytes > sc->sc_vram - PAGE_SIZE) {
757 aprint_error_dev(sc->sc_dev, "Not enough video RAM for %s\n",
758 mode->name);
759 return EINVAL;
760 }
761
762 /* set up and write colormap */
763 platinumfb_init_cmap(sc);
764 platinumfb_restore_palette(sc);
765
766 /* set hardware registers */
767 platinumfb_set_hardware(sc);
768
769 aprint_normal_dev(sc->sc_dev, "switched to %s in %d bit color\n",
770 sc->sc_pfs->vmode_name, sc->sc_depth);
771 return 0;
772 }
773
774 static int
775 platinumfb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
776 struct lwp *l)
777 {
778 struct vcons_data *vd = v;
779 struct platinumfb_softc *sc = vd->cookie;
780 struct wsdisplay_fbinfo *wdf;
781 struct vcons_screen *ms = vd->active;
782 int i;
783
784 switch (cmd) {
785 case WSDISPLAYIO_GTYPE:
786 *(u_int *)data = WSDISPLAY_TYPE_PLATINUM;
787 return 0;
788
789 case WSDISPLAYIO_GINFO:
790 wdf = (void *)data;
791 wdf->height = ms->scr_ri.ri_height;
792 wdf->width = ms->scr_ri.ri_width;
793 wdf->depth = ms->scr_ri.ri_depth;
794 wdf->cmsize = 256;
795 return 0;
796
797 case WSDISPLAYIO_GVIDEO:
798 *(int *)data = sc->sc_on;
799 return 0;
800
801 case WSDISPLAYIO_SVIDEO:
802 /*
803 * poor man's screen blanking, just write zeros to colormap
804 * registers but don't save in softc.
805 */
806 if (*(int *)data != sc->sc_on) {
807 sc->sc_on = (sc->sc_on == WSDISPLAYIO_VIDEO_ON ?
808 WSDISPLAYIO_VIDEO_OFF : WSDISPLAYIO_VIDEO_ON);
809
810 /* XXX need to lock colormap? */
811 if (sc->sc_on == WSDISPLAYIO_VIDEO_OFF) {
812 for (i=0; i < 256; i++)
813 platinumfb_putpalreg(sc, i, 0, 0, 0);
814 } else {
815 platinumfb_restore_palette(sc);
816 }
817 }
818 return 0;
819
820 case WSDISPLAYIO_GETCMAP:
821 if (sc->sc_cmode == PLATINUM_CMODE_8) {
822 return platinumfb_getcmap(sc,
823 (struct wsdisplay_cmap *)data);
824 } else
825 return 0;
826
827 case WSDISPLAYIO_PUTCMAP:
828 if (sc->sc_cmode == PLATINUM_CMODE_8) {
829 return platinumfb_putcmap(sc,
830 (struct wsdisplay_cmap *)data);
831 } else
832 return 0;
833
834 case WSDISPLAYIO_SMODE: {
835 int new_mode = *(int*)data;
836
837 if (new_mode != sc->sc_mode) {
838 int new_depth = new_mode == WSDISPLAYIO_MODE_EMUL ?
839 PLATINUM_CONSOLE_DEPTH : PLATINUM_FB_DEPTH;
840
841 switch(new_mode) {
842 /*
843 * XXX - not sure how this is supposed to work for
844 * switching bpp between console and X, but cases with
845 * (EMUL MAPPED) or (EMUL MAPPED DUMBFB) work, but
846 * (EMUL DUMBFB) garbles screen for some reason.
847 */
848 case WSDISPLAYIO_MODE_EMUL:
849 case WSDISPLAYIO_MODE_MAPPED:
850 /* case WSDISPLAYIO_MODE_DUMBFB: XXX */
851
852 /* in case screen is "blanked" */
853 platinumfb_restore_palette(sc);
854
855 sc->sc_mode = new_mode;
856
857 platinumfb_set_mode(sc, sc->sc_videomode, new_depth);
858 platinumfb_set_rasops(sc, &ms->scr_ri, true);
859
860 if (new_mode == WSDISPLAYIO_MODE_EMUL)
861 vcons_redraw_screen(ms);
862 }
863 }
864
865 return 0;
866 }
867
868 case WSDISPLAYIO_LINEBYTES:
869 *(u_int *)data = sc->sc_linebytes;
870 return 0;
871
872 case WSDISPLAYIO_GET_FBINFO: {
873 struct wsdisplayio_fbinfo *fbi = data;
874 return wsdisplayio_get_fbinfo(&ms->scr_ri, fbi);
875 }
876
877 }
878 return EPASSTHROUGH;
879 }
880
881 static paddr_t
882 platinumfb_mmap(void *v, void *vs, off_t offset, int prot)
883 {
884 struct vcons_data *vd = v;
885 struct platinumfb_softc *sc = vd->cookie;
886 paddr_t pa = -1;
887 paddr_t fb_aligned = platinumfb_page_align_up(sc);
888 paddr_t fb_vram = sc->sc_vram - (fb_aligned - (paddr_t)sc->sc_fb);
889
890 /* XXX need to worry about superuser or mapping other registers? */
891
892 if (offset >= 0 && offset < fb_vram)
893 pa = fb_aligned + offset;
894
895 return pa;
896 }
897
898 static void platinumfb_set_rasops(struct platinumfb_softc *sc,
899 struct rasops_info *ri,
900 int existing)
901 {
902 memset(ri, 0, sizeof(struct rasops_info));
903
904 ri->ri_depth = sc->sc_depth;
905 ri->ri_width = sc->sc_width;
906 ri->ri_height = sc->sc_height;
907 ri->ri_stride = sc->sc_linebytes;
908 ri->ri_bits = (u_char*)platinumfb_page_align_up(sc);
909 ri->ri_flg = RI_FULLCLEAR;
910
911 if (existing)
912 ri->ri_flg |= RI_CLEAR;
913
914 switch (sc->sc_cmode) {
915 case PLATINUM_CMODE_8:
916 default:
917 ri->ri_flg |= RI_ENABLE_ALPHA | RI_8BIT_IS_RGB;
918
919 break;
920 case PLATINUM_CMODE_16:
921 if (strcmp(sc->sc_pfs->vmode_name, "640x480x60") == 0 ||
922 strcmp(sc->sc_pfs->vmode_name, "800x600x75") == 0 )
923 ri->ri_flg |= RI_ENABLE_ALPHA;
924
925 ri->ri_rnum = 5;
926 ri->ri_rpos = 10;
927 ri->ri_gnum = 5;
928 ri->ri_gpos = 5;
929 ri->ri_bnum = 5;
930 ri->ri_bpos = 0;
931
932 break;
933 case PLATINUM_CMODE_32:
934 if (strcmp(sc->sc_pfs->vmode_name, "640x480x60") == 0 ||
935 strcmp(sc->sc_pfs->vmode_name, "800x600x75") == 0 )
936 ri->ri_flg |= RI_ENABLE_ALPHA;
937
938 ri->ri_rnum = 8;
939 ri->ri_rpos = 16;
940 ri->ri_gnum = 8;
941 ri->ri_gpos = 8;
942 ri->ri_bnum = 8;
943 ri->ri_bpos = 0;
944
945 break;
946 }
947
948 rasops_init(ri, 0, 0);
949 ri->ri_caps = WSSCREEN_WSCOLORS;
950
951 rasops_reconfig(ri, sc->sc_height / ri->ri_font->fontheight,
952 sc->sc_width / ri->ri_font->fontwidth);
953
954 }
955
956 static void
957 platinumfb_init_screen(void *cookie, struct vcons_screen *scr,
958 int existing, long *defattr)
959 {
960 struct platinumfb_softc *sc = cookie;
961 struct rasops_info *ri = &scr->scr_ri;
962
963 scr->scr_flags |= VCONS_DONT_READ;
964
965 platinumfb_set_rasops(sc, ri, existing);
966
967 ri->ri_hw = scr;
968 }
969
970 static int
971 platinumfb_putcmap(struct platinumfb_softc *sc, struct wsdisplay_cmap *cm)
972 {
973 u_char *r, *g, *b;
974 u_int index = cm->index;
975 u_int count = cm->count;
976 int i, error;
977 u_char rbuf[256], gbuf[256], bbuf[256];
978
979 if (cm->index >= 256 || cm->count > 256 ||
980 (cm->index + cm->count) > 256)
981 return EINVAL;
982 error = copyin(cm->red, &rbuf[index], count);
983 if (error)
984 return error;
985 error = copyin(cm->green, &gbuf[index], count);
986 if (error)
987 return error;
988 error = copyin(cm->blue, &bbuf[index], count);
989 if (error)
990 return error;
991
992 memcpy(&sc->sc_cmap_red[index], &rbuf[index], count);
993 memcpy(&sc->sc_cmap_green[index], &gbuf[index], count);
994 memcpy(&sc->sc_cmap_blue[index], &bbuf[index], count);
995
996 /* write colormap registers if not currently blanked */
997 if (sc->sc_on == WSDISPLAYIO_VIDEO_ON) {
998 r = &sc->sc_cmap_red[index];
999 g = &sc->sc_cmap_green[index];
1000 b = &sc->sc_cmap_blue[index];
1001
1002 for (i = 0; i < count; i++) {
1003 platinumfb_putpalreg(sc, index, *r, *g, *b);
1004 index++;
1005 r++, g++, b++;
1006 }
1007 }
1008
1009 return 0;
1010 }
1011
1012 static int
1013 platinumfb_getcmap(struct platinumfb_softc *sc, struct wsdisplay_cmap *cm)
1014 {
1015 u_int index = cm->index;
1016 u_int count = cm->count;
1017 int error;
1018
1019 if (index >= 255 || count > 256 || index + count > 256)
1020 return EINVAL;
1021
1022 error = copyout(&sc->sc_cmap_red[index], cm->red, count);
1023 if (error)
1024 return error;
1025 error = copyout(&sc->sc_cmap_green[index], cm->green, count);
1026 if (error)
1027 return error;
1028 error = copyout(&sc->sc_cmap_blue[index], cm->blue, count);
1029 if (error)
1030 return error;
1031
1032 return 0;
1033 }
1034