#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: valkyriefb.c,v 1.10 2025/10/04 03:26:41 thorpej Exp $");
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/device.h>
#include <dev/ofw/openfirm.h>
#include <machine/autoconf.h>
#include <uvm/uvm_extern.h>
#include <powerpc/oea/pmap.h>
#include <dev/wscons/wsdisplayvar.h>
#include <dev/wscons/wsconsio.h>
#include <dev/wsfont/wsfont.h>
#include <dev/rasops/rasops.h>
#include <dev/wscons/wsdisplay_vconsvar.h>
#include <dev/videomode/videomode.h>
#include <arch/macppc/dev/valkyriefbreg.h>
#include <arch/macppc/dev/videopllvar.h>
#include "opt_wsemul.h"
#include "opt_valkyriefb.h"
#include "videopll.h"
#if NVIDEOPLL < 1
#error "valkyriefb requires videopll at iic"
#endif
struct valkyriefb_softc {
device_t sc_dev;
int sc_node;
uint8_t *sc_base;
uint8_t *sc_fbaddr;
int sc_depth;
int sc_width, sc_height, sc_linebytes;
const struct videomode *sc_videomode;
uint8_t sc_modereg;
int sc_mode;
uint32_t sc_bg;
u_char sc_cmap_red[256];
u_char sc_cmap_green[256];
u_char sc_cmap_blue[256];
struct vcons_data vd;
};
struct valkyriefb_mode {
int width;
int height;
uint8_t mode;
};
static struct valkyriefb_mode modetab[] = {
{ 640, 480, 6 },
{ 800, 600, 12 },
{ 832, 624, 9 },
{ 1024, 768, 14},
};
static struct vcons_screen valkyriefb_console_screen;
static int valkyriefb_match(device_t, cfdata_t, void *);
static void valkyriefb_attach(device_t, device_t, void *);
static int valkyriefb_init(device_t);
static int valkyriefb_set_mode(struct valkyriefb_softc *,
const struct videomode *, int);
CFATTACH_DECL_NEW(valkyriefb, sizeof(struct valkyriefb_softc),
valkyriefb_match, valkyriefb_attach, NULL, NULL);
struct wsscreen_descr valkyriefb_defaultscreen = {
"default",
0, 0,
NULL,
8, 16,
WSSCREEN_WSCOLORS | WSSCREEN_HILIT,
NULL,
};
const struct wsscreen_descr *_valkyriefb_scrlist[] = {
&valkyriefb_defaultscreen,
};
struct wsscreen_list valkyriefb_screenlist = {
sizeof(_valkyriefb_scrlist) / sizeof(struct wsscreen_descr *),
_valkyriefb_scrlist
};
static int valkyriefb_ioctl(void *, void *, u_long, void *, int,
struct lwp *);
static paddr_t valkyriefb_mmap(void *, void *, off_t, int);
static void valkyriefb_init_screen(void *, struct vcons_screen *, int,
long *);
struct wsdisplay_accessops valkyriefb_accessops = {
valkyriefb_ioctl,
valkyriefb_mmap,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
};
static inline void
valkyriefb_write_reg(struct valkyriefb_softc *sc, int reg, uint8_t val)
{
*(sc->sc_base + VAL_REGS_OFFSET + reg) = val;
__asm volatile("eieio; sync;" ::: "memory");
}
#ifdef VALKYRIEFB_DEBUG
static inline uint8_t
valkyriefb_read_reg(struct valkyriefb_softc *sc, int reg)
{
return *(sc->sc_base + VAL_REGS_OFFSET + reg);
}
#endif
static void
valkyriefb_write_cmap(struct valkyriefb_softc *sc,
int reg, uint8_t r, uint8_t g, uint8_t b)
{
*(sc->sc_base + VAL_CMAP_OFFSET + VAL_CMAP_ADDR) = reg;
__asm volatile("eieio; sync;" ::: "memory");
*(sc->sc_base + VAL_CMAP_OFFSET + VAL_CMAP_LUT) = r;
__asm volatile("eieio; sync;" ::: "memory");
*(sc->sc_base + VAL_CMAP_OFFSET + VAL_CMAP_LUT) = g;
__asm volatile("eieio; sync;" ::: "memory");
*(sc->sc_base + VAL_CMAP_OFFSET + VAL_CMAP_LUT) = b;
__asm volatile("eieio; sync;" ::: "memory");
}
static int
valkyriefb_match(device_t parent, cfdata_t cf, void *aux)
{
struct confargs *ca = aux;
if (strcmp(ca->ca_name, "valkyrie") != 0)
return 0;
return 1;
}
static void
valkyriefb_attach(device_t parent, device_t self, void *aux)
{
struct valkyriefb_softc *sc = device_private(self);
struct confargs *ca = aux;
sc->sc_dev = self;
sc->sc_node = ca->ca_node;
aprint_normal(" address 0x%08x\n", ca->ca_reg[0]);
aprint_verbose_dev(sc->sc_dev, "waiting for videopll...\n");
sc->sc_base = (uint8_t *)ca->ca_reg[0];
#ifdef VALKYRIEFB_DEBUG
for (int i = 0; i < 0x40; i += 8) {
aprint_error_dev(sc->sc_dev, "%02x: %02x\n", i,
valkyriefb_read_reg(sc, i));
}
#endif
config_finalize_register(sc->sc_dev, valkyriefb_init);
sc->sc_fbaddr = (uint8_t *)(sc->sc_base + 0x1000);
}
static int
valkyriefb_init(device_t self)
{
struct valkyriefb_softc *sc = device_private(self);
const struct videomode *mode;
struct rasops_info *ri;
struct wsemuldisplaydev_attach_args aa;
bool console;
long defattr;
mode = pick_mode_by_ref(800, 600, 60);
if (mode == NULL)
return 0;
valkyriefb_set_mode(sc, mode, 8);
vcons_init(&sc->vd, sc, &valkyriefb_defaultscreen,
&valkyriefb_accessops);
sc->vd.init_screen = valkyriefb_init_screen;
console = device_getprop_bool(self, "is_console");
ri = &valkyriefb_console_screen.scr_ri;
vcons_init_screen(&sc->vd, &valkyriefb_console_screen, 1, &defattr);
memset(sc->sc_base + 0x1000, ri->ri_devcmap[(defattr >> 16) & 0xf],
sc->sc_width * sc->sc_linebytes);
valkyriefb_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
valkyriefb_defaultscreen.textops = &ri->ri_ops;
valkyriefb_defaultscreen.capabilities = ri->ri_caps;
valkyriefb_defaultscreen.nrows = ri->ri_rows;
valkyriefb_defaultscreen.ncols = ri->ri_cols;
if (console) {
wsdisplay_cnattach(&valkyriefb_defaultscreen, ri, 0, 0,
defattr);
vcons_replay_msgbuf(&valkyriefb_console_screen);
}
aa.console = console;
aa.scrdata = &valkyriefb_screenlist;
aa.accessops = &valkyriefb_accessops;
aa.accesscookie = &sc->vd;
config_found(self, &aa, wsemuldisplaydevprint, CFARGS_NONE);
return 0;
}
static int
valkyriefb_set_mode(struct valkyriefb_softc *sc,
const struct videomode *mode, int depth)
{
int i;
uint8_t modereg, tmp;
i = 0;
while ((i < __arraycount(modetab)) &&
(modetab[i].width != mode->hdisplay) &&
(modetab[i].height != mode->vdisplay))
i++;
if (i >= __arraycount(modetab)) {
aprint_error_dev(sc->sc_dev,
"Can't find a mode register value for %s\n", mode->name);
return EINVAL;
} else
modereg = modetab[i].mode;
if ((mode->hdisplay * mode->vdisplay * (depth >> 3)) > 0x100000) {
aprint_error_dev(sc->sc_dev, "Not enough video RAM for %s\n",
mode->name);
return EINVAL;
}
if ((depth != 8) && (depth != 16)) {
aprint_error_dev(sc->sc_dev,
"Depth [%d] is unsupported for %s\n", depth, mode->name);
return EINVAL;
}
valkyriefb_write_reg(sc, VAL_STATUS, 0);
delay(100);
valkyriefb_write_reg(sc, VAL_MODE, modereg | VAL_MODE_STOP);
if (depth == 8) {
sc->sc_depth = 8;
valkyriefb_write_reg(sc, VAL_DEPTH, VAL_DEPTH_8);
for (i = 0; i < 256; i++) {
tmp = i & 0xe0;
tmp |= (tmp >> 3) | (tmp >> 6);
sc->sc_cmap_red[i] = tmp;
tmp = (i & 0x1c) << 3;
tmp |= (tmp >> 3) | (tmp >> 6);
sc->sc_cmap_green[i] = tmp;
tmp = (i & 0x03) << 6;
tmp |= tmp >> 2;
tmp |= tmp >> 4;
sc->sc_cmap_blue[i] = tmp;
valkyriefb_write_cmap(sc, i, sc->sc_cmap_red[i],
sc->sc_cmap_green[i], sc->sc_cmap_blue[i]);
}
} else if (depth == 16) {
sc->sc_depth = 16;
valkyriefb_write_reg(sc, VAL_DEPTH, VAL_DEPTH_16);
}
videopll_set_freq(mode->dot_clock);
delay(100);
valkyriefb_write_reg(sc, VAL_MODE, modereg);
sc->sc_modereg = modereg;
sc->sc_videomode = mode;
sc->sc_width = mode->hdisplay;
sc->sc_height = mode->vdisplay;
sc->sc_linebytes = mode->hdisplay * (sc->sc_depth >> 3);
aprint_normal_dev(sc->sc_dev, "switched to %d x %d in %d bit colour\n",
sc->sc_width, sc->sc_height, sc->sc_depth);
return 0;
}
static int
valkyriefb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
struct lwp *l)
{
struct vcons_data *vd = v;
struct valkyriefb_softc *sc = vd->cookie;
struct wsdisplay_fbinfo *wdf;
struct vcons_screen *ms = vd->active;
switch (cmd) {
case WSDISPLAYIO_GTYPE:
*(u_int *)data = WSDISPLAY_TYPE_VALKYRIE;
return 0;
case WSDISPLAYIO_GINFO:
wdf = (void *)data;
wdf->height = ms->scr_ri.ri_height;
wdf->width = ms->scr_ri.ri_width;
wdf->depth = ms->scr_ri.ri_depth;
wdf->cmsize = 256;
return 0;
#if 0
case WSDISPLAYIO_GETCMAP:
return valkyriefb_getcmap(sc,
(struct wsdisplay_cmap *)data);
case WSDISPLAYIO_PUTCMAP:
return valkyriefb_putcmap(sc,
(struct wsdisplay_cmap *)data);
#endif
case WSDISPLAYIO_SMODE: {
int new_mode = *(int*)data;
if (new_mode != sc->sc_mode) {
sc->sc_mode = new_mode;
if (new_mode == WSDISPLAYIO_MODE_EMUL) {
vcons_redraw_screen(ms);
}
}
}
return 0;
}
return EPASSTHROUGH;
}
static paddr_t
valkyriefb_mmap(void *v, void *vs, off_t offset, int prot)
{
struct vcons_data *vd = v;
struct valkyriefb_softc *sc = vd->cookie;
paddr_t pa;
if (offset < 0x100000) {
pa = (paddr_t)(sc->sc_base + 0x1000 + offset);
pa |= POWERPC_MMAP_FLAG_PREFETCHABLE;
return pa;
}
return -1;
}
static void
valkyriefb_init_screen(void *cookie, struct vcons_screen *scr,
int existing, long *defattr)
{
struct valkyriefb_softc *sc = cookie;
struct rasops_info *ri = &scr->scr_ri;
memset(ri, 0, sizeof(struct rasops_info));
ri->ri_depth = sc->sc_depth;
ri->ri_width = sc->sc_width;
ri->ri_height = sc->sc_height;
ri->ri_stride = sc->sc_linebytes;
ri->ri_flg = RI_CENTER | RI_8BIT_IS_RGB | RI_ENABLE_ALPHA;
ri->ri_bits = sc->sc_fbaddr;
scr->scr_flags |= VCONS_DONT_READ;
rasops_init(ri, 0, 0);
ri->ri_caps = WSSCREEN_WSCOLORS;
rasops_reconfig(ri, sc->sc_height / ri->ri_font->fontheight,
sc->sc_width / ri->ri_font->fontwidth);
ri->ri_hw = scr;
}