valkyriefb.c revision 1.4 1 /* $NetBSD: valkyriefb.c,v 1.4 2016/07/06 13:30:42 macallan Exp $ */
2
3 /*
4 * Copyright (c) 2012 Michael Lorenz
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 /*
29 * A console driver for Apple's Valkyrie onboard video controller, found in
30 * for example the Performa 6360
31 * This should be easy enough to adapt to mac68k but I don't have the hardware
32 */
33
34 #include <sys/cdefs.h>
35 __KERNEL_RCSID(0, "$NetBSD: valkyriefb.c,v 1.4 2016/07/06 13:30:42 macallan Exp $");
36
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/device.h>
41
42 #include <dev/ofw/openfirm.h>
43
44 #include <machine/autoconf.h>
45
46 #include <dev/wscons/wsdisplayvar.h>
47 #include <dev/wscons/wsconsio.h>
48 #include <dev/wsfont/wsfont.h>
49 #include <dev/rasops/rasops.h>
50 #include <dev/wscons/wsdisplay_vconsvar.h>
51
52 #include <dev/videomode/videomode.h>
53
54 #include <arch/macppc/dev/valkyriefbreg.h>
55 #include <arch/macppc/dev/videopllvar.h>
56
57 #include "opt_wsemul.h"
58 #include "opt_valkyriefb.h"
59
60 #include "videopll.h"
61 #if NVIDEOPLL < 1
62 #error "valkyriefb requires videopll at iic"
63 #endif
64
65 struct valkyriefb_softc {
66 device_t sc_dev;
67 int sc_node;
68 uint8_t *sc_base;
69 uint8_t *sc_fbaddr;
70
71 int sc_depth;
72 int sc_width, sc_height, sc_linebytes;
73 const struct videomode *sc_videomode;
74 uint8_t sc_modereg;
75
76 int sc_mode;
77 uint32_t sc_bg;
78
79 u_char sc_cmap_red[256];
80 u_char sc_cmap_green[256];
81 u_char sc_cmap_blue[256];
82
83 struct vcons_data vd;
84 };
85
86 struct valkyriefb_mode {
87 int width;
88 int height;
89 uint8_t mode;
90 };
91
92 /*
93 * Translate screen resolutions to values for Valkyrie's mode register.
94 * These numbers seem to roughly correspond to MacOS video mode numbers
95 * there are many numbers that result in the same resolution which in MacOS
96 * only differ by the display refresh rate, which isn't set by Valkyrie at
97 * all, instead there's a PLL chip hooked to cuda's i2c bus. It doesn't seem
98 * to matter which number we use as long as the resolution is right and we
99 * program the PLL for the right pixel clock
100 */
101 static struct valkyriefb_mode modetab[] = {
102 { 640, 480, 6 },
103 { 800, 600, 12 },
104 { 832, 624, 9 },
105 { 1024, 768, 14},
106 };
107
108 static struct vcons_screen valkyriefb_console_screen;
109
110 static int valkyriefb_match(device_t, cfdata_t, void *);
111 static void valkyriefb_attach(device_t, device_t, void *);
112 static int valkyriefb_init(device_t);
113 static int valkyriefb_set_mode(struct valkyriefb_softc *,
114 const struct videomode *, int);
115
116 CFATTACH_DECL_NEW(valkyriefb, sizeof(struct valkyriefb_softc),
117 valkyriefb_match, valkyriefb_attach, NULL, NULL);
118
119 struct wsscreen_descr valkyriefb_defaultscreen = {
120 "default",
121 0, 0,
122 NULL,
123 8, 16,
124 WSSCREEN_WSCOLORS | WSSCREEN_HILIT,
125 NULL,
126 };
127
128 const struct wsscreen_descr *_valkyriefb_scrlist[] = {
129 &valkyriefb_defaultscreen,
130 /* XXX other formats, graphics screen? */
131 };
132
133 struct wsscreen_list valkyriefb_screenlist = {
134 sizeof(_valkyriefb_scrlist) / sizeof(struct wsscreen_descr *),
135 _valkyriefb_scrlist
136 };
137
138 static int valkyriefb_ioctl(void *, void *, u_long, void *, int,
139 struct lwp *);
140 static paddr_t valkyriefb_mmap(void *, void *, off_t, int);
141
142 static void valkyriefb_init_screen(void *, struct vcons_screen *, int,
143 long *);
144
145
146 struct wsdisplay_accessops valkyriefb_accessops = {
147 valkyriefb_ioctl,
148 valkyriefb_mmap,
149 NULL,
150 NULL,
151 NULL,
152 NULL, /* load_font */
153 NULL, /* polls */
154 NULL, /* scroll */
155 };
156
157 static inline void
158 valkyriefb_write_reg(struct valkyriefb_softc *sc, int reg, uint8_t val)
159 {
160 *(sc->sc_base + VAL_REGS_OFFSET + reg) = val;
161 __asm("eieio; sync;");
162 }
163
164 #ifdef VALKYRIEFB_DEBUG
165 static inline uint8_t
166 valkyriefb_read_reg(struct valkyriefb_softc *sc, int reg)
167 {
168 return *(sc->sc_base + VAL_REGS_OFFSET + reg);
169 }
170 #endif
171
172 static void
173 valkyriefb_write_cmap(struct valkyriefb_softc *sc,
174 int reg, uint8_t r, uint8_t g, uint8_t b)
175 {
176 *(sc->sc_base + VAL_CMAP_OFFSET + VAL_CMAP_ADDR) = reg;
177 __asm("eieio; sync;");
178 *(sc->sc_base + VAL_CMAP_OFFSET + VAL_CMAP_LUT) = r;
179 __asm("eieio; sync;");
180 *(sc->sc_base + VAL_CMAP_OFFSET + VAL_CMAP_LUT) = g;
181 __asm("eieio; sync;");
182 *(sc->sc_base + VAL_CMAP_OFFSET + VAL_CMAP_LUT) = b;
183 __asm("eieio; sync;");
184 }
185
186 static int
187 valkyriefb_match(device_t parent, cfdata_t cf, void *aux)
188 {
189 struct confargs *ca = aux;
190
191 if (strcmp(ca->ca_name, "valkyrie") != 0)
192 return 0;
193 return 1;
194 }
195
196 static void
197 valkyriefb_attach(device_t parent, device_t self, void *aux)
198 {
199 struct valkyriefb_softc *sc = device_private(self);
200 struct confargs *ca = aux;
201
202 sc->sc_dev = self;
203 sc->sc_node = ca->ca_node;
204
205 aprint_normal(" address 0x%08x\n", ca->ca_reg[0]);
206 aprint_verbose_dev(sc->sc_dev, "waiting for videopll...\n");
207 sc->sc_base = (uint8_t *)ca->ca_reg[0];
208 #ifdef VALKYRIEFB_DEBUG
209 for (i = 0; i < 0x40; i += 8) {
210 aprint_error_dev(sc->sc_dev, "%02x: %02x\n", i,
211 valkyriefb_read_reg(sc, i));
212 }
213 #endif
214 config_finalize_register(sc->sc_dev, valkyriefb_init);
215 sc->sc_fbaddr = (uint8_t *)(sc->sc_base + 0x1000);
216 }
217
218 static int
219 valkyriefb_init(device_t self)
220 {
221 struct valkyriefb_softc *sc = device_private(self);
222 const struct videomode *mode;
223 struct rasops_info *ri;
224 prop_dictionary_t dict;
225 struct wsemuldisplaydev_attach_args aa;
226 bool console = FALSE;
227 long defattr;
228
229 mode = pick_mode_by_ref(800, 600, 60);
230 if (mode == NULL)
231 return 0;
232
233 valkyriefb_set_mode(sc, mode, 8);
234
235 vcons_init(&sc->vd, sc, &valkyriefb_defaultscreen,
236 &valkyriefb_accessops);
237 sc->vd.init_screen = valkyriefb_init_screen;
238
239 dict = device_properties(sc->sc_dev);
240 prop_dictionary_get_bool(dict, "is_console", &console);
241
242 ri = &valkyriefb_console_screen.scr_ri;
243 vcons_init_screen(&sc->vd, &valkyriefb_console_screen, 1, &defattr);
244 memset(sc->sc_base + 0x1000, ri->ri_devcmap[(defattr >> 16) & 0xf],
245 sc->sc_width * sc->sc_linebytes);
246 valkyriefb_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
247
248 valkyriefb_defaultscreen.textops = &ri->ri_ops;
249 valkyriefb_defaultscreen.capabilities = ri->ri_caps;
250 valkyriefb_defaultscreen.nrows = ri->ri_rows;
251 valkyriefb_defaultscreen.ncols = ri->ri_cols;
252 if (console) {
253 wsdisplay_cnattach(&valkyriefb_defaultscreen, ri, 0, 0,
254 defattr);
255 vcons_replay_msgbuf(&valkyriefb_console_screen);
256 }
257 aa.console = console;
258 aa.scrdata = &valkyriefb_screenlist;
259 aa.accessops = &valkyriefb_accessops;
260 aa.accesscookie = &sc->vd;
261
262 config_found(self, &aa, wsemuldisplaydevprint);
263
264 return 0;
265 }
266
267 static int
268 valkyriefb_set_mode(struct valkyriefb_softc *sc,
269 const struct videomode *mode, int depth)
270 {
271 int i;
272 uint8_t modereg, tmp;
273
274 /* first find the parameter for the mode register */
275 i = 0;
276 while ((i < __arraycount(modetab)) &&
277 (modetab[i].width != mode->hdisplay) &&
278 (modetab[i].height != mode->vdisplay))
279 i++;
280 if (i >= __arraycount(modetab)) {
281 aprint_error_dev(sc->sc_dev,
282 "Can't find a mode register value for %s\n", mode->name);
283 return EINVAL;
284 } else
285 modereg = modetab[i].mode;
286
287 /* check if we have enough video memory */
288 if ((mode->hdisplay * mode->vdisplay * (depth >> 3)) > 0x100000) {
289 aprint_error_dev(sc->sc_dev, "Not enough video RAM for %s\n",
290 mode->name);
291 return EINVAL;
292 }
293
294 /* reject depths other than 8 or 16 */
295 if ((depth != 8) && (depth != 16)) {
296 aprint_error_dev(sc->sc_dev,
297 "Depth [%d] is unsupported for %s\n", depth, mode->name);
298 return EINVAL;
299 }
300
301 /* now start programming the chip */
302 valkyriefb_write_reg(sc, VAL_STATUS, 0);
303 delay(100);
304 valkyriefb_write_reg(sc, VAL_MODE, modereg | VAL_MODE_STOP);
305
306 if (depth == 8) {
307 sc->sc_depth = 8;
308 valkyriefb_write_reg(sc, VAL_DEPTH, VAL_DEPTH_8);
309 for (i = 0; i < 256; i++) {
310 tmp = i & 0xe0;
311 /*
312 * replicate bits so 0xe0 maps to a red value of 0xff
313 * in order to make white look actually white
314 */
315 tmp |= (tmp >> 3) | (tmp >> 6);
316 sc->sc_cmap_red[i] = tmp;
317
318 tmp = (i & 0x1c) << 3;
319 tmp |= (tmp >> 3) | (tmp >> 6);
320 sc->sc_cmap_green[i] = tmp;
321
322 tmp = (i & 0x03) << 6;
323 tmp |= tmp >> 2;
324 tmp |= tmp >> 4;
325 sc->sc_cmap_blue[i] = tmp;
326
327 valkyriefb_write_cmap(sc, i, sc->sc_cmap_red[i],
328 sc->sc_cmap_green[i], sc->sc_cmap_blue[i]);
329 }
330 } else if (depth == 16) {
331 sc->sc_depth = 16;
332 valkyriefb_write_reg(sc, VAL_DEPTH, VAL_DEPTH_16);
333 /* does the palette have any effect in 16bit? */
334 }
335
336 videopll_set_freq(mode->dot_clock);
337 delay(100);
338 valkyriefb_write_reg(sc, VAL_MODE, modereg);
339
340 sc->sc_modereg = modereg;
341 sc->sc_videomode = mode;
342 sc->sc_width = mode->hdisplay;
343 sc->sc_height = mode->vdisplay;
344 sc->sc_linebytes = mode->hdisplay * (sc->sc_depth >> 3);
345 aprint_normal_dev(sc->sc_dev, "switched to %d x %d in %d bit colour\n",
346 sc->sc_width, sc->sc_height, sc->sc_depth);
347 return 0;
348 }
349
350 static int
351 valkyriefb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
352 struct lwp *l)
353 {
354 struct vcons_data *vd = v;
355 struct valkyriefb_softc *sc = vd->cookie;
356 struct wsdisplay_fbinfo *wdf;
357 struct vcons_screen *ms = vd->active;
358
359 switch (cmd) {
360 case WSDISPLAYIO_GTYPE:
361 *(u_int *)data = WSDISPLAY_TYPE_VALKYRIE;
362 return 0;
363
364 case WSDISPLAYIO_GINFO:
365 wdf = (void *)data;
366 wdf->height = ms->scr_ri.ri_height;
367 wdf->width = ms->scr_ri.ri_width;
368 wdf->depth = ms->scr_ri.ri_depth;
369 wdf->cmsize = 256;
370 return 0;
371 #if 0
372 case WSDISPLAYIO_GETCMAP:
373 return valkyriefb_getcmap(sc,
374 (struct wsdisplay_cmap *)data);
375
376 case WSDISPLAYIO_PUTCMAP:
377 return valkyriefb_putcmap(sc,
378 (struct wsdisplay_cmap *)data);
379 #endif
380
381 case WSDISPLAYIO_SMODE: {
382 int new_mode = *(int*)data;
383 if (new_mode != sc->sc_mode) {
384 sc->sc_mode = new_mode;
385 if (new_mode == WSDISPLAYIO_MODE_EMUL) {
386 vcons_redraw_screen(ms);
387 }
388 }
389 }
390 return 0;
391 }
392 return EPASSTHROUGH;
393 }
394
395 static paddr_t
396 valkyriefb_mmap(void *v, void *vs, off_t offset, int prot)
397 {
398 struct vcons_data *vd = v;
399 struct valkyriefb_softc *sc = vd->cookie;
400 paddr_t pa;
401
402 /* 'regular' framebuffer mmap()ing */
403 if (offset < 0x100000) {
404 pa = (paddr_t)(sc->sc_base + 0x1000 + offset);
405 return pa;
406 }
407 return -1;
408 }
409
410 static void
411 valkyriefb_init_screen(void *cookie, struct vcons_screen *scr,
412 int existing, long *defattr)
413 {
414 struct valkyriefb_softc *sc = cookie;
415 struct rasops_info *ri = &scr->scr_ri;
416
417 memset(ri, 0, sizeof(struct rasops_info));
418 ri->ri_depth = sc->sc_depth;
419 ri->ri_width = sc->sc_width;
420 ri->ri_height = sc->sc_height;
421 ri->ri_stride = sc->sc_linebytes;
422 ri->ri_flg = RI_CENTER | RI_8BIT_IS_RGB | RI_ENABLE_ALPHA;
423 ri->ri_bits = sc->sc_fbaddr;
424
425 scr->scr_flags |= VCONS_DONT_READ;
426
427 rasops_init(ri, 0, 0);
428 ri->ri_caps = WSSCREEN_WSCOLORS;
429
430 rasops_reconfig(ri, sc->sc_height / ri->ri_font->fontheight,
431 sc->sc_width / ri->ri_font->fontwidth);
432
433 ri->ri_hw = scr;
434 }
435