Home | History | Annotate | Line # | Download | only in dev
      1 /*	$NetBSD: valkyriefb.c,v 1.10 2025/10/04 03:26:41 thorpej 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.10 2025/10/04 03:26:41 thorpej 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 <uvm/uvm_extern.h>
     47 #include <powerpc/oea/pmap.h>
     48 
     49 #include <dev/wscons/wsdisplayvar.h>
     50 #include <dev/wscons/wsconsio.h>
     51 #include <dev/wsfont/wsfont.h>
     52 #include <dev/rasops/rasops.h>
     53 #include <dev/wscons/wsdisplay_vconsvar.h>
     54 
     55 #include <dev/videomode/videomode.h>
     56 
     57 #include <arch/macppc/dev/valkyriefbreg.h>
     58 #include <arch/macppc/dev/videopllvar.h>
     59 
     60 #include "opt_wsemul.h"
     61 #include "opt_valkyriefb.h"
     62 
     63 #include "videopll.h"
     64 #if NVIDEOPLL < 1
     65 #error "valkyriefb requires videopll at iic"
     66 #endif
     67 
     68 struct valkyriefb_softc {
     69 	device_t sc_dev;
     70 	int sc_node;
     71 	uint8_t *sc_base;
     72 	uint8_t *sc_fbaddr;
     73 
     74 	int sc_depth;
     75 	int sc_width, sc_height, sc_linebytes;
     76 	const struct videomode *sc_videomode;
     77 	uint8_t sc_modereg;
     78 
     79 	int sc_mode;
     80 	uint32_t sc_bg;
     81 
     82 	u_char sc_cmap_red[256];
     83 	u_char sc_cmap_green[256];
     84 	u_char sc_cmap_blue[256];
     85 
     86 	struct vcons_data vd;
     87 };
     88 
     89 struct valkyriefb_mode {
     90 	int width;
     91 	int height;
     92 	uint8_t mode;
     93 };
     94 
     95 /*
     96  * Translate screen resolutions to values for Valkyrie's mode register.
     97  * These numbers seem to roughly correspond to MacOS video mode numbers
     98  * there are many numbers that result in the same resolution which in MacOS
     99  * only differ by the display refresh rate, which isn't set by Valkyrie at
    100  * all, instead there's a PLL chip hooked to cuda's i2c bus. It doesn't seem
    101  * to matter which number we use as long as the resolution is right and we
    102  * program the PLL for the right pixel clock
    103  */
    104 static struct valkyriefb_mode modetab[] = {
    105 	{  640, 480, 6 },
    106 	{  800, 600, 12 },
    107 	{  832, 624, 9 },
    108 	{ 1024, 768, 14},
    109 };
    110 
    111 static struct vcons_screen valkyriefb_console_screen;
    112 
    113 static int	valkyriefb_match(device_t, cfdata_t, void *);
    114 static void	valkyriefb_attach(device_t, device_t, void *);
    115 static int	valkyriefb_init(device_t);
    116 static int 	valkyriefb_set_mode(struct valkyriefb_softc *,
    117 		    const struct videomode *, int);
    118 
    119 CFATTACH_DECL_NEW(valkyriefb, sizeof(struct valkyriefb_softc),
    120     valkyriefb_match, valkyriefb_attach, NULL, NULL);
    121 
    122 struct wsscreen_descr valkyriefb_defaultscreen = {
    123 	"default",
    124 	0, 0,
    125 	NULL,
    126 	8, 16,
    127 	WSSCREEN_WSCOLORS | WSSCREEN_HILIT,
    128 	NULL,
    129 };
    130 
    131 const struct wsscreen_descr *_valkyriefb_scrlist[] = {
    132 	&valkyriefb_defaultscreen,
    133 	/* XXX other formats, graphics screen? */
    134 };
    135 
    136 struct wsscreen_list valkyriefb_screenlist = {
    137 	sizeof(_valkyriefb_scrlist) / sizeof(struct wsscreen_descr *),
    138 	_valkyriefb_scrlist
    139 };
    140 
    141 static int	valkyriefb_ioctl(void *, void *, u_long, void *, int,
    142 		    struct lwp *);
    143 static paddr_t	valkyriefb_mmap(void *, void *, off_t, int);
    144 
    145 static void	valkyriefb_init_screen(void *, struct vcons_screen *, int,
    146 			    long *);
    147 
    148 
    149 struct wsdisplay_accessops valkyriefb_accessops = {
    150 	valkyriefb_ioctl,
    151 	valkyriefb_mmap,
    152 	NULL,
    153 	NULL,
    154 	NULL,
    155 	NULL,	/* load_font */
    156 	NULL,	/* polls */
    157 	NULL,	/* scroll */
    158 };
    159 
    160 static inline void
    161 valkyriefb_write_reg(struct valkyriefb_softc *sc, int reg, uint8_t val)
    162 {
    163 	*(sc->sc_base + VAL_REGS_OFFSET + reg) = val;
    164 	__asm volatile("eieio; sync;" ::: "memory");
    165 }
    166 
    167 #ifdef VALKYRIEFB_DEBUG
    168 static inline uint8_t
    169 valkyriefb_read_reg(struct valkyriefb_softc *sc, int reg)
    170 {
    171 	return *(sc->sc_base + VAL_REGS_OFFSET + reg);
    172 }
    173 #endif
    174 
    175 static void
    176 valkyriefb_write_cmap(struct valkyriefb_softc *sc,
    177     int reg, uint8_t r, uint8_t g, uint8_t b)
    178 {
    179 	*(sc->sc_base + VAL_CMAP_OFFSET + VAL_CMAP_ADDR) = reg;
    180 	__asm volatile("eieio; sync;" ::: "memory");
    181 	*(sc->sc_base + VAL_CMAP_OFFSET + VAL_CMAP_LUT) = r;
    182 	__asm volatile("eieio; sync;" ::: "memory");
    183 	*(sc->sc_base + VAL_CMAP_OFFSET + VAL_CMAP_LUT) = g;
    184 	__asm volatile("eieio; sync;" ::: "memory");
    185 	*(sc->sc_base + VAL_CMAP_OFFSET + VAL_CMAP_LUT) = b;
    186 	__asm volatile("eieio; sync;" ::: "memory");
    187 }
    188 
    189 static int
    190 valkyriefb_match(device_t parent, cfdata_t cf, void *aux)
    191 {
    192 	struct confargs *ca = aux;
    193 
    194 	if (strcmp(ca->ca_name, "valkyrie") != 0)
    195 		return 0;
    196 	return 1;
    197 }
    198 
    199 static void
    200 valkyriefb_attach(device_t parent, device_t self, void *aux)
    201 {
    202 	struct valkyriefb_softc *sc = device_private(self);
    203 	struct confargs *ca = aux;
    204 
    205 	sc->sc_dev = self;
    206 	sc->sc_node = ca->ca_node;
    207 
    208 	aprint_normal(" address 0x%08x\n", ca->ca_reg[0]);
    209 	aprint_verbose_dev(sc->sc_dev, "waiting for videopll...\n");
    210 	sc->sc_base = (uint8_t *)ca->ca_reg[0];
    211 #ifdef VALKYRIEFB_DEBUG
    212 	for (int i = 0; i < 0x40; i += 8) {
    213 		aprint_error_dev(sc->sc_dev, "%02x: %02x\n", i,
    214 		    valkyriefb_read_reg(sc, i));
    215 	}
    216 #endif
    217 	config_finalize_register(sc->sc_dev, valkyriefb_init);
    218 	sc->sc_fbaddr = (uint8_t *)(sc->sc_base + 0x1000);
    219 }
    220 
    221 static int
    222 valkyriefb_init(device_t self)
    223 {
    224 	struct valkyriefb_softc *sc = device_private(self);
    225 	const struct videomode *mode;
    226 	struct rasops_info *ri;
    227 	struct wsemuldisplaydev_attach_args aa;
    228 	bool console;
    229 	long defattr;
    230 
    231 	mode = pick_mode_by_ref(800, 600, 60);
    232 	if (mode == NULL)
    233 		return 0;
    234 
    235 	valkyriefb_set_mode(sc, mode, 8);
    236 
    237 	vcons_init(&sc->vd, sc, &valkyriefb_defaultscreen,
    238 	    &valkyriefb_accessops);
    239 	sc->vd.init_screen = valkyriefb_init_screen;
    240 
    241 	console = device_getprop_bool(self, "is_console");
    242 
    243 	ri = &valkyriefb_console_screen.scr_ri;
    244 	vcons_init_screen(&sc->vd, &valkyriefb_console_screen, 1, &defattr);
    245 	memset(sc->sc_base + 0x1000, ri->ri_devcmap[(defattr >> 16) & 0xf],
    246 	    sc->sc_width * sc->sc_linebytes);
    247 	valkyriefb_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
    248 
    249 	valkyriefb_defaultscreen.textops = &ri->ri_ops;
    250 	valkyriefb_defaultscreen.capabilities = ri->ri_caps;
    251 	valkyriefb_defaultscreen.nrows = ri->ri_rows;
    252 	valkyriefb_defaultscreen.ncols = ri->ri_cols;
    253 	if (console) {
    254 		wsdisplay_cnattach(&valkyriefb_defaultscreen, ri, 0, 0,
    255 		    defattr);
    256 		vcons_replay_msgbuf(&valkyriefb_console_screen);
    257 	}
    258 	aa.console = console;
    259 	aa.scrdata = &valkyriefb_screenlist;
    260 	aa.accessops = &valkyriefb_accessops;
    261 	aa.accesscookie = &sc->vd;
    262 
    263 	config_found(self, &aa, wsemuldisplaydevprint, CFARGS_NONE);
    264 
    265 	return 0;
    266 }
    267 
    268 static int
    269 valkyriefb_set_mode(struct valkyriefb_softc *sc,
    270     const struct videomode *mode, int depth)
    271 {
    272 	int i;
    273 	uint8_t modereg, tmp;
    274 
    275 	/* first find the parameter for the mode register */
    276 	i = 0;
    277 	while ((i < __arraycount(modetab)) &&
    278 	       (modetab[i].width != mode->hdisplay) &&
    279 	       (modetab[i].height != mode->vdisplay))
    280 		i++;
    281 	if (i >= __arraycount(modetab)) {
    282 		aprint_error_dev(sc->sc_dev,
    283 		    "Can't find a mode register value for %s\n", mode->name);
    284 		return EINVAL;
    285 	} else
    286 		modereg = modetab[i].mode;
    287 
    288 	/* check if we have enough video memory */
    289 	if ((mode->hdisplay * mode->vdisplay * (depth >> 3)) > 0x100000) {
    290 		aprint_error_dev(sc->sc_dev, "Not enough video RAM for %s\n",
    291 		    mode->name);
    292 		return EINVAL;
    293 	}
    294 
    295 	/* reject depths other than 8 or 16 */
    296 	if ((depth != 8) && (depth != 16)) {
    297 		aprint_error_dev(sc->sc_dev,
    298 		    "Depth [%d] is unsupported for %s\n", depth, mode->name);
    299 		return EINVAL;
    300 	}
    301 
    302 	/* now start programming the chip */
    303 	valkyriefb_write_reg(sc, VAL_STATUS, 0);
    304 	delay(100);
    305 	valkyriefb_write_reg(sc, VAL_MODE, modereg | VAL_MODE_STOP);
    306 
    307 	if (depth == 8) {
    308 		sc->sc_depth = 8;
    309 		valkyriefb_write_reg(sc, VAL_DEPTH, VAL_DEPTH_8);
    310 		for (i = 0; i < 256; i++) {
    311 			tmp = i & 0xe0;
    312 			/*
    313 			 * replicate bits so 0xe0 maps to a red value of 0xff
    314 			 * in order to make white look actually white
    315 			 */
    316 			tmp |= (tmp >> 3) | (tmp >> 6);
    317 			sc->sc_cmap_red[i] = tmp;
    318 
    319 			tmp = (i & 0x1c) << 3;
    320 			tmp |= (tmp >> 3) | (tmp >> 6);
    321 			sc->sc_cmap_green[i] = tmp;
    322 
    323 			tmp = (i & 0x03) << 6;
    324 			tmp |= tmp >> 2;
    325 			tmp |= tmp >> 4;
    326 			sc->sc_cmap_blue[i] = tmp;
    327 
    328 			valkyriefb_write_cmap(sc, i, sc->sc_cmap_red[i],
    329 			    sc->sc_cmap_green[i], sc->sc_cmap_blue[i]);
    330 		}
    331 	} else if (depth == 16) {
    332 		sc->sc_depth = 16;
    333 		valkyriefb_write_reg(sc, VAL_DEPTH, VAL_DEPTH_16);
    334 		/* does the palette have any effect in 16bit? */
    335 	}
    336 
    337 	videopll_set_freq(mode->dot_clock);
    338 	delay(100);
    339 	valkyriefb_write_reg(sc, VAL_MODE, modereg);
    340 
    341 	sc->sc_modereg = modereg;
    342 	sc->sc_videomode = mode;
    343 	sc->sc_width = mode->hdisplay;
    344 	sc->sc_height = mode->vdisplay;
    345 	sc->sc_linebytes = mode->hdisplay * (sc->sc_depth >> 3);
    346 	aprint_normal_dev(sc->sc_dev, "switched to %d x %d in %d bit colour\n",
    347 	    sc->sc_width, sc->sc_height, sc->sc_depth);
    348 	return 0;
    349 }
    350 
    351 static int
    352 valkyriefb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
    353 	struct lwp *l)
    354 {
    355 	struct vcons_data *vd = v;
    356 	struct valkyriefb_softc *sc = vd->cookie;
    357 	struct wsdisplay_fbinfo *wdf;
    358 	struct vcons_screen *ms = vd->active;
    359 
    360 	switch (cmd) {
    361 	case WSDISPLAYIO_GTYPE:
    362 		*(u_int *)data = WSDISPLAY_TYPE_VALKYRIE;
    363 		return 0;
    364 
    365 	case WSDISPLAYIO_GINFO:
    366 		wdf = (void *)data;
    367 		wdf->height = ms->scr_ri.ri_height;
    368 		wdf->width = ms->scr_ri.ri_width;
    369 		wdf->depth = ms->scr_ri.ri_depth;
    370 		wdf->cmsize = 256;
    371 		return 0;
    372 #if 0
    373 	case WSDISPLAYIO_GETCMAP:
    374 		return valkyriefb_getcmap(sc,
    375 		    (struct wsdisplay_cmap *)data);
    376 
    377 	case WSDISPLAYIO_PUTCMAP:
    378 		return valkyriefb_putcmap(sc,
    379 		    (struct wsdisplay_cmap *)data);
    380 #endif
    381 
    382 	case WSDISPLAYIO_SMODE: {
    383 		int new_mode = *(int*)data;
    384 		if (new_mode != sc->sc_mode) {
    385 			sc->sc_mode = new_mode;
    386 			if (new_mode == WSDISPLAYIO_MODE_EMUL) {
    387 				vcons_redraw_screen(ms);
    388 			}
    389 		}
    390 		}
    391 		return 0;
    392 	}
    393 	return EPASSTHROUGH;
    394 }
    395 
    396 static paddr_t
    397 valkyriefb_mmap(void *v, void *vs, off_t offset, int prot)
    398 {
    399 	struct vcons_data *vd = v;
    400 	struct valkyriefb_softc *sc = vd->cookie;
    401 	paddr_t pa;
    402 
    403 	/* 'regular' framebuffer mmap()ing */
    404 	if (offset < 0x100000) {
    405 		pa = (paddr_t)(sc->sc_base + 0x1000 + offset);
    406 		pa |= POWERPC_MMAP_FLAG_PREFETCHABLE;
    407 		return pa;
    408 	}
    409 	return -1;
    410 }
    411 
    412 static void
    413 valkyriefb_init_screen(void *cookie, struct vcons_screen *scr,
    414     int existing, long *defattr)
    415 {
    416 	struct valkyriefb_softc *sc = cookie;
    417 	struct rasops_info *ri = &scr->scr_ri;
    418 
    419 	memset(ri, 0, sizeof(struct rasops_info));
    420 	ri->ri_depth = sc->sc_depth;
    421 	ri->ri_width = sc->sc_width;
    422 	ri->ri_height = sc->sc_height;
    423 	ri->ri_stride = sc->sc_linebytes;
    424 	ri->ri_flg = RI_CENTER | RI_8BIT_IS_RGB | RI_ENABLE_ALPHA;
    425 	ri->ri_bits = sc->sc_fbaddr;
    426 
    427 	scr->scr_flags |= VCONS_DONT_READ;
    428 
    429 	rasops_init(ri, 0, 0);
    430 	ri->ri_caps = WSSCREEN_WSCOLORS;
    431 
    432 	rasops_reconfig(ri, sc->sc_height / ri->ri_font->fontheight,
    433 		    sc->sc_width / ri->ri_font->fontwidth);
    434 
    435 	ri->ri_hw = scr;
    436 }
    437