Home | History | Annotate | Line # | Download | only in dev
wiifb.c revision 1.5.2.3
      1 /* $NetBSD: wiifb.c,v 1.5.2.3 2024/02/06 12:33:17 martin Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2024 Jared McNeill <jmcneill (at) invisible.ca>
      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,
     21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  * SUCH DAMAGE.
     27  */
     28 
     29 #include <sys/cdefs.h>
     30 __KERNEL_RCSID(0, "$NetBSD: wiifb.c,v 1.5.2.3 2024/02/06 12:33:17 martin Exp $");
     31 
     32 #include <sys/param.h>
     33 #include <sys/bus.h>
     34 #include <sys/device.h>
     35 #include <sys/systm.h>
     36 
     37 #include <machine/wii.h>
     38 
     39 #include <dev/videomode/videomode.h>
     40 #include <dev/wsfb/genfbvar.h>
     41 
     42 #include "mainbus.h"
     43 #include "vireg.h"
     44 #include "viio.h"
     45 
     46 #define WIIFB_ERROR_BLINK_INTERVAL	1000000
     47 
     48 #define WIIFB_TOP_BOTTOM_BORDER		16
     49 #define WIIFB_EFFECTIVE_START(p, w)	\
     50 	((uintptr_t)(p) + WIIFB_TOP_BOTTOM_BORDER * (w) * 2)
     51 #define WIIFB_EFFECTIVE_HEIGHT(h)	\
     52 	((h) - WIIFB_TOP_BOTTOM_BORDER * 2)
     53 
     54 
     55 struct wiifb_mode {
     56 	const char *		name;
     57 	u_int			width;
     58 	u_int			height;
     59 	u_int			lines;
     60 };
     61 
     62 static uint32_t wiifb_devcmap[16] = {
     63 	0x00800080,	/* Black */
     64 	0x1dff1d6b,	/* Blue */
     65 	0x4b554b4a,	/* Green */
     66 	0x80808080,	/* Cyan */
     67 	0x4c544cff,	/* Red */
     68 	0x3aaa34b5,	/* Magenta */
     69 	0x7140718a,	/* Brown */
     70 	0xff80ff80,	/* White */
     71 	0x80808080,	/* Gray */
     72 	0xc399c36a,	/* Bright Blue */
     73 	0xd076d074,	/* Bright Green */
     74 	0x80808080,	/* Bright Cyan */
     75 	0x4c544cff,	/* Bright Red */
     76 	0x3aaa34b5,	/* Bright Magenta */
     77 	0xe100e194,	/* Bright Yellow */
     78 	0xff80ff80	/* Bright White */
     79 };
     80 
     81 #define WIIFB_MODE_INDEX(fmt, interlaced)	((fmt << 1) | interlaced)
     82 
     83 static const struct wiifb_mode wiifb_modes[] = {
     84 	[WIIFB_MODE_INDEX(VI_DCR_FMT_NTSC, 0)] = {
     85 		.name = "NTSC 480p",
     86 		.width = 640,
     87 		.height = 480,
     88 		.lines = 525,
     89 	},
     90 	[WIIFB_MODE_INDEX(VI_DCR_FMT_NTSC, 1)] = {
     91 		.name = "NTSC 480i",
     92 		.width = 640,
     93 		.height = 480,
     94 		.lines = 525,
     95 	},
     96 	[WIIFB_MODE_INDEX(VI_DCR_FMT_PAL, 1)] = {
     97 		.name = "PAL 576i",
     98 		.width = 640,
     99 		.height = 574,
    100 		.lines = 625,
    101 	},
    102 
    103 };
    104 #define WIIFB_NMODES	__arraycount(wiifb_modes)
    105 
    106 struct wiifb_softc {
    107 	struct genfb_softc	sc_gen;
    108 
    109 	bus_space_tag_t		sc_bst;
    110 	bus_space_handle_t	sc_bsh;
    111 
    112 	void			*sc_bits;
    113 
    114 	uint8_t			sc_format;
    115 	bool			sc_interlaced;
    116 
    117 	const struct wiifb_mode	*sc_curmode;
    118 };
    119 
    120 #define	RD2(sc, reg)		\
    121 	bus_space_read_2((sc)->sc_bst, (sc)->sc_bsh, (reg))
    122 #define	RD4(sc, reg)		\
    123 	bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
    124 #define	WR2(sc, reg, val)	\
    125 	bus_space_write_2((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
    126 #define	WR4(sc, reg, val)	\
    127 	bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
    128 
    129 static int	wiifb_match(device_t, cfdata_t, void *);
    130 static void	wiifb_attach(device_t, device_t, void *);
    131 
    132 static void	wiifb_init(struct wiifb_softc *);
    133 static void	wiifb_set_mode(struct wiifb_softc *, uint8_t, bool);
    134 static void	wiifb_set_fb(struct wiifb_softc *);
    135 
    136 static int	wiifb_ioctl(void *, void *, u_long, void *, int, lwp_t *);
    137 static paddr_t	wiifb_mmap(void *, void *, off_t, int);
    138 
    139 static struct genfb_ops wiifb_ops = {
    140 	.genfb_ioctl = wiifb_ioctl,
    141 	.genfb_mmap = wiifb_mmap,
    142 };
    143 
    144 CFATTACH_DECL_NEW(wiifb, sizeof(struct wiifb_softc),
    145 	wiifb_match, wiifb_attach, NULL, NULL);
    146 
    147 static int
    148 wiifb_match(device_t parent, cfdata_t cf, void *aux)
    149 {
    150 	struct mainbus_attach_args *maa = aux;
    151 
    152 	return strcmp(maa->maa_name, "genfb") == 0;
    153 }
    154 
    155 static void
    156 wiifb_attach(device_t parent, device_t self, void *aux)
    157 {
    158 	struct wiifb_softc *sc = device_private(self);
    159 	prop_dictionary_t dict = device_properties(self);
    160 	struct mainbus_attach_args *maa = aux;
    161 	u_int offset;
    162 	uint32_t *p;
    163 	int error;
    164 
    165 	sc->sc_gen.sc_dev = self;
    166 	sc->sc_bst = maa->maa_bst;
    167 	error = bus_space_map(sc->sc_bst, maa->maa_addr, VI_SIZE, 0,
    168 	    &sc->sc_bsh);
    169 	if (error != 0) {
    170 		panic("couldn't map registers");
    171 	}
    172 	sc->sc_bits = mapiodev(XFB_START, XFB_SIZE, true);
    173 
    174 	/*
    175 	 * Paint the entire FB black. Use 4-byte accesses as the Wii will
    176 	 * ignore 1- and 2- byte writes to uncached memory.
    177 	 */
    178 	for (p = sc->sc_bits, offset = 0;
    179 	     offset < XFB_SIZE;
    180 	     offset += 4, p++) {
    181 		*p = 0x00800080;
    182 	}
    183 
    184 	wiifb_init(sc);
    185 	wiifb_set_mode(sc, sc->sc_format, sc->sc_interlaced);
    186 
    187 	prop_dictionary_set_uint32(dict, "width", sc->sc_curmode->width);
    188 	prop_dictionary_set_uint32(dict, "height",
    189 	    WIIFB_EFFECTIVE_HEIGHT(sc->sc_curmode->height));
    190 	prop_dictionary_set_uint8(dict, "depth", 16);
    191 	prop_dictionary_set_uint32(dict, "address", XFB_START);
    192 	prop_dictionary_set_uint32(dict, "virtual_address",
    193 	    WIIFB_EFFECTIVE_START(sc->sc_bits, sc->sc_curmode->width));
    194 	prop_dictionary_set_uint64(dict, "devcmap", (uintptr_t)wiifb_devcmap);
    195 
    196 	genfb_init(&sc->sc_gen);
    197 
    198 	aprint_naive("\n");
    199 	aprint_normal(": %s\n", sc->sc_curmode->name);
    200 
    201 	genfb_cnattach();
    202 	prop_dictionary_set_bool(dict, "is_console", true);
    203 	genfb_attach(&sc->sc_gen, &wiifb_ops);
    204 }
    205 
    206 static void
    207 wiifb_init(struct wiifb_softc *sc)
    208 {
    209 	uint16_t dcr;
    210 	uint16_t visel;
    211 
    212 	/* Read current display format and interlaced settings. */
    213 	dcr = RD2(sc, VI_DCR);
    214 	if ((dcr & VI_DCR_ENB) != 0) {
    215 		sc->sc_format = __SHIFTOUT(dcr, VI_DCR_FMT);
    216 		sc->sc_interlaced = (dcr & VI_DCR_NIN) == 0;
    217 	} else {
    218 		visel = RD2(sc, VI_VISEL);
    219 		sc->sc_format = VI_DCR_FMT_NTSC;
    220 		sc->sc_interlaced = (visel & VI_VISEL_COMPONENT_CABLE) == 0;
    221 	}
    222 
    223 	/* Reset video interface. */
    224 	WR2(sc, VI_DCR, dcr | VI_DCR_RST);
    225 	WR2(sc, VI_DCR, dcr & ~VI_DCR_RST);
    226 }
    227 
    228 static void
    229 wiifb_set_mode(struct wiifb_softc *sc, uint8_t format, bool interlaced)
    230 {
    231 	u_int modeidx;
    232 	u_int strides, reads;
    233 
    234 	modeidx = WIIFB_MODE_INDEX(format, interlaced);
    235 	if (modeidx == WIIFB_MODE_INDEX(VI_DCR_FMT_NTSC, 1)) {
    236 		/* NTSC 480i Magic numbers from YAGCD. */
    237 		WR2(sc, VI_VTR, 0x0f06);
    238 		WR4(sc, VI_HTR0, 0x476901AD);
    239 		WR4(sc, VI_HTR1, 0x02EA5140);
    240 		WR4(sc, VI_VTO, 0x00030018);
    241 		WR4(sc, VI_VTE, 0x00020019);
    242 		WR4(sc, VI_BBOI, 0x410C410C);
    243 		WR4(sc, VI_BBEI, 0x40ED40ED);
    244 	} else if (modeidx == WIIFB_MODE_INDEX(VI_DCR_FMT_NTSC, 0)) {
    245 		/* NTSC 480p */
    246 		WR2(sc, VI_VTR, 0x1e0c);
    247 		WR4(sc, VI_HTR0, 0x476901ad);
    248 		WR4(sc, VI_HTR1, 0x030a4940);
    249 		WR4(sc, VI_VTO, 0x00060030);
    250 		WR4(sc, VI_VTE, 0x00060030);
    251 		WR4(sc, VI_BBOI, 0x81d881d8);
    252 		WR4(sc, VI_BBEI, 0x81d881d8);
    253 	} else if (modeidx == WIIFB_MODE_INDEX(VI_DCR_FMT_PAL, 1)) {
    254 		/* PAL 576i */
    255 		WR2(sc, VI_VTR, 0x11f5);
    256 		WR4(sc, VI_HTR0, 0x4b6a01b0);
    257 		WR4(sc, VI_HTR1, 0x02f85640);
    258 		WR4(sc, VI_VTO, 0x00010023);
    259 		WR4(sc, VI_VTE, 0x00000024);
    260 		WR4(sc, VI_BBOI, 0x4d2b4d6d);
    261 		WR4(sc, VI_BBEI, 0x4d8a4d4c);
    262 	} else {
    263 		/*
    264 		 * Display mode is not supported. Blink the slot LED to
    265 		 * indicate failure.
    266 		 */
    267 		wii_slot_led_blink(WIIFB_ERROR_BLINK_INTERVAL);
    268 	}
    269 
    270 	if (modeidx >= WIIFB_NMODES || wiifb_modes[modeidx].name == NULL) {
    271 		panic("Unsupported format (0x%x) / interlaced (%d) settings",
    272 		    sc->sc_format, sc->sc_interlaced);
    273 	}
    274 	sc->sc_curmode = &wiifb_modes[modeidx];
    275 
    276 	/* Picture configuration */
    277 	strides = (sc->sc_curmode->width * 2) / (interlaced ? 16 : 32);
    278 	reads = (sc->sc_curmode->width * 2) / 32;
    279 	WR2(sc, VI_PICCONF,
    280 	    __SHIFTIN(strides, VI_PICCONF_STRIDES) |
    281 	    __SHIFTIN(reads, VI_PICCONF_READS));
    282 
    283 	/* Horizontal scaler configuration */
    284 	if (interlaced) {
    285 		WR2(sc, VI_HSR, __SHIFTIN(256, VI_HSR_STP));
    286 	} else {
    287 		WR2(sc, VI_HSR, __SHIFTIN(244, VI_HSR_STP) | VI_HSR_HS_EN);
    288 	}
    289 
    290 	/* Video clock configuration */
    291 	WR2(sc, VI_VICLK,
    292 	    interlaced ? VI_VICLK_SEL_27MHZ : VI_VICLK_SEL_54MHZ);
    293 
    294 	/* Horizontal scaling width */
    295 	WR2(sc, VI_HSCALINGW, sc->sc_curmode->width);
    296 
    297 	/* Set framebuffer address */
    298 	wiifb_set_fb(sc);
    299 }
    300 
    301 static void
    302 wiifb_set_fb(struct wiifb_softc *sc)
    303 {
    304 	uint32_t taddr = XFB_START;
    305 	uint32_t baddr = taddr + (sc->sc_interlaced ?
    306 				  sc->sc_curmode->width * 2 : 0);
    307 
    308 	WR4(sc, VI_TFBL,
    309 	    VI_TFBL_PGOFF |
    310 	    __SHIFTIN((taddr >> 5), VI_TFBL_FBB) |
    311 	    __SHIFTIN((taddr / 2) & 0xf, VI_TFBL_XOF));
    312 	WR4(sc, VI_TFBR, 0);
    313 
    314 	WR4(sc, VI_BFBL,
    315 	    VI_BFBL_PGOFF |
    316 	    __SHIFTIN((baddr >> 5), VI_BFBL_FBB) |
    317 	    __SHIFTIN((baddr / 2) & 0xf, VI_BFBL_XOF));
    318 	WR4(sc, VI_BFBR, 0);
    319 }
    320 
    321 static int
    322 wiifb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag, lwp_t *l)
    323 {
    324 	struct wiifb_softc *sc = v;
    325 	struct wsdisplayio_bus_id *busid;
    326 	struct wsdisplayio_fbinfo *fbi;
    327 	struct vi_regs *vr;
    328 	u_int video;
    329 
    330 	switch (cmd) {
    331 	case WSDISPLAYIO_GTYPE:
    332 		*(u_int *)data = WSDISPLAY_TYPE_HOLLYWOOD;
    333 		return 0;
    334 	case WSDISPLAYIO_GET_BUSID:
    335 		busid = data;
    336 		busid->bus_type = WSDISPLAYIO_BUS_SOC;
    337 		return 0;
    338 	case WSDISPLAYIO_GET_FBINFO:
    339 		fbi = data;
    340 		/*
    341 		 * rasops info does not match the pixel encoding due to our
    342 		 * devcmap, so fill out fbinfo manually instead of relying
    343 		 * on wsdisplayio_get_fbinfo.
    344 		 */
    345 		fbi->fbi_fboffset = 0;
    346 		fbi->fbi_width = sc->sc_curmode->width;
    347 		fbi->fbi_height =
    348 		    WIIFB_EFFECTIVE_HEIGHT(sc->sc_curmode->height);
    349 		fbi->fbi_stride = fbi->fbi_width * 2;
    350 		fbi->fbi_fbsize = fbi->fbi_height * fbi->fbi_stride;
    351 		fbi->fbi_bitsperpixel = 16;
    352 		fbi->fbi_pixeltype = WSFB_YUY2;
    353 		fbi->fbi_flags = WSFB_VRAM_IS_RAM;
    354 		return 0;
    355 
    356 	case WSDISPLAYIO_SVIDEO:
    357 		video = *(u_int *)data;
    358 		switch (video) {
    359 		case WSDISPLAYIO_VIDEO_OFF:
    360 			out32(HW_VIDIM, __SHIFTIN(7, VIDIM_Y) |
    361 					__SHIFTIN(7, VIDIM_C) |
    362 					VIDIM_E);
    363 			return 0;
    364 		case WSDISPLAYIO_VIDEO_ON:
    365 			out32(HW_VIDIM, 0);
    366 			return 0;
    367 		default:
    368 			return EINVAL;
    369 		}
    370 
    371 	case VIIO_GETREGS:
    372 	case VIIO_SETREGS:
    373 		vr = data;
    374 		switch (vr->bits) {
    375 		case 16:
    376 			if ((vr->reg & 1) != 0) {
    377 				return EINVAL;
    378 			}
    379 			if (cmd == VIIO_GETREGS) {
    380 				vr->val16 = RD2(sc, vr->reg);
    381 			} else {
    382 				WR2(sc, vr->reg, vr->val16);
    383 			}
    384 			return 0;
    385 		case 32:
    386 			if ((vr->reg & 3) != 0) {
    387 				return EINVAL;
    388 			}
    389 			if (cmd == VIIO_GETREGS) {
    390 				vr->val32 = RD4(sc, vr->reg);
    391 			} else {
    392 				WR4(sc, vr->reg, vr->val32);
    393 			}
    394 			return 0;
    395 		default:
    396 			return EINVAL;
    397 		}
    398 		return 0;
    399 	}
    400 
    401 	return EPASSTHROUGH;
    402 }
    403 
    404 static paddr_t
    405 wiifb_mmap(void *v, void *vs, off_t off, int prot)
    406 {
    407 	struct wiifb_softc *sc = v;
    408 	bus_addr_t start;
    409 	bus_size_t size;
    410 
    411 	start = WIIFB_EFFECTIVE_START(XFB_START, sc->sc_curmode->width);
    412 	size = WIIFB_EFFECTIVE_HEIGHT(sc->sc_curmode->height) *
    413 	       sc->sc_curmode->width * 2;
    414 
    415 	if (off < 0 || off >= size) {
    416 		return -1;
    417 	}
    418 
    419 	return bus_space_mmap(sc->sc_bst, start, off, prot,
    420 	    BUS_SPACE_MAP_LINEAR | BUS_SPACE_MAP_PREFETCHABLE);
    421 }
    422