Home | History | Annotate | Line # | Download | only in dev
wiifb.c revision 1.1
      1  1.1  jmcneill /* $NetBSD: wiifb.c,v 1.1 2024/01/20 21:36:00 jmcneill Exp $ */
      2  1.1  jmcneill 
      3  1.1  jmcneill /*-
      4  1.1  jmcneill  * Copyright (c) 2024 Jared McNeill <jmcneill (at) invisible.ca>
      5  1.1  jmcneill  * All rights reserved.
      6  1.1  jmcneill  *
      7  1.1  jmcneill  * Redistribution and use in source and binary forms, with or without
      8  1.1  jmcneill  * modification, are permitted provided that the following conditions
      9  1.1  jmcneill  * are met:
     10  1.1  jmcneill  * 1. Redistributions of source code must retain the above copyright
     11  1.1  jmcneill  *    notice, this list of conditions and the following disclaimer.
     12  1.1  jmcneill  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  jmcneill  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  jmcneill  *    documentation and/or other materials provided with the distribution.
     15  1.1  jmcneill  *
     16  1.1  jmcneill  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  1.1  jmcneill  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  1.1  jmcneill  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  1.1  jmcneill  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  1.1  jmcneill  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     21  1.1  jmcneill  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     22  1.1  jmcneill  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     23  1.1  jmcneill  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     24  1.1  jmcneill  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  1.1  jmcneill  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  1.1  jmcneill  * SUCH DAMAGE.
     27  1.1  jmcneill  */
     28  1.1  jmcneill 
     29  1.1  jmcneill #include <sys/cdefs.h>
     30  1.1  jmcneill __KERNEL_RCSID(0, "$NetBSD: wiifb.c,v 1.1 2024/01/20 21:36:00 jmcneill Exp $");
     31  1.1  jmcneill 
     32  1.1  jmcneill #include <sys/param.h>
     33  1.1  jmcneill #include <sys/bus.h>
     34  1.1  jmcneill #include <sys/device.h>
     35  1.1  jmcneill #include <sys/systm.h>
     36  1.1  jmcneill 
     37  1.1  jmcneill #include <machine/wii.h>
     38  1.1  jmcneill 
     39  1.1  jmcneill #include <dev/videomode/videomode.h>
     40  1.1  jmcneill #include <dev/wsfb/genfbvar.h>
     41  1.1  jmcneill 
     42  1.1  jmcneill #include "mainbus.h"
     43  1.1  jmcneill #include "vireg.h"
     44  1.1  jmcneill 
     45  1.1  jmcneill #define	WIIFB_ERROR_BLINK_INTERVAL	1000000
     46  1.1  jmcneill 
     47  1.1  jmcneill struct wiifb_mode {
     48  1.1  jmcneill 	const char *		name;
     49  1.1  jmcneill 	u_int			width;
     50  1.1  jmcneill 	u_int			height;
     51  1.1  jmcneill 	u_int			lines;
     52  1.1  jmcneill };
     53  1.1  jmcneill 
     54  1.1  jmcneill static uint32_t wiifb_devcmap[16] = {
     55  1.1  jmcneill 	0x00800080,	/* Black */
     56  1.1  jmcneill 	0x1dff1d6b,	/* Blue */
     57  1.1  jmcneill 	0x4b554b4a,	/* Green */
     58  1.1  jmcneill 	0x80808080,	/* Cyan */
     59  1.1  jmcneill 	0x4c544cff,	/* Red */
     60  1.1  jmcneill 	0x3aaa34b5,	/* Magenta */
     61  1.1  jmcneill 	0x7140718a,	/* Brown */
     62  1.1  jmcneill 	0xff80ff80,	/* White */
     63  1.1  jmcneill 	0x80808080,	/* Gray */
     64  1.1  jmcneill 	0xc399c36a,	/* Bright Blue */
     65  1.1  jmcneill 	0xd076d074,	/* Bright Green */
     66  1.1  jmcneill 	0x80808080,	/* Bright Cyan */
     67  1.1  jmcneill 	0x4c544cff,	/* Bright Red */
     68  1.1  jmcneill 	0x3aaa34b5,	/* Bright Magenta */
     69  1.1  jmcneill 	0xe100e194,	/* Bright Yellow */
     70  1.1  jmcneill 	0xff80ff80	/* Bright White */
     71  1.1  jmcneill };
     72  1.1  jmcneill 
     73  1.1  jmcneill #define WIIFB_MODE_INDEX(fmt, interlaced)	((fmt << 1) | interlaced)
     74  1.1  jmcneill 
     75  1.1  jmcneill static const struct wiifb_mode wiifb_modes[] = {
     76  1.1  jmcneill 	[WIIFB_MODE_INDEX(VI_DCR_FMT_NTSC, 0)] = {
     77  1.1  jmcneill 		.name = "NTSC 480p",
     78  1.1  jmcneill 		.width = 640,
     79  1.1  jmcneill 		.height = 480,
     80  1.1  jmcneill 		.lines = 525,
     81  1.1  jmcneill 	},
     82  1.1  jmcneill 	[WIIFB_MODE_INDEX(VI_DCR_FMT_NTSC, 1)] = {
     83  1.1  jmcneill 		.name = "NTSC 480i",
     84  1.1  jmcneill 		.width = 640,
     85  1.1  jmcneill 		.height = 480,
     86  1.1  jmcneill 		.lines = 525,
     87  1.1  jmcneill 	},
     88  1.1  jmcneill };
     89  1.1  jmcneill #define WIIFB_NMODES	__arraycount(wiifb_modes)
     90  1.1  jmcneill 
     91  1.1  jmcneill struct wiifb_softc {
     92  1.1  jmcneill 	struct genfb_softc	sc_gen;
     93  1.1  jmcneill 
     94  1.1  jmcneill 	bus_space_tag_t		sc_bst;
     95  1.1  jmcneill 	bus_space_handle_t	sc_bsh;
     96  1.1  jmcneill 
     97  1.1  jmcneill 	void			*sc_bits;
     98  1.1  jmcneill 
     99  1.1  jmcneill 	uint8_t			sc_format;
    100  1.1  jmcneill 	bool			sc_interlaced;
    101  1.1  jmcneill 
    102  1.1  jmcneill 	const struct wiifb_mode	*sc_curmode;
    103  1.1  jmcneill };
    104  1.1  jmcneill 
    105  1.1  jmcneill #define	RD2(sc, reg)		\
    106  1.1  jmcneill 	bus_space_read_2((sc)->sc_bst, (sc)->sc_bsh, (reg))
    107  1.1  jmcneill #define	RD4(sc, reg)		\
    108  1.1  jmcneill 	bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
    109  1.1  jmcneill #define	WR2(sc, reg, val)	\
    110  1.1  jmcneill 	bus_space_write_2((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
    111  1.1  jmcneill #define	WR4(sc, reg, val)	\
    112  1.1  jmcneill 	bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
    113  1.1  jmcneill 
    114  1.1  jmcneill static int	wiifb_match(device_t, cfdata_t, void *);
    115  1.1  jmcneill static void	wiifb_attach(device_t, device_t, void *);
    116  1.1  jmcneill 
    117  1.1  jmcneill static void	wiifb_init(struct wiifb_softc *);
    118  1.1  jmcneill static void	wiifb_set_mode(struct wiifb_softc *, uint8_t, bool);
    119  1.1  jmcneill static void	wiifb_set_fb(struct wiifb_softc *);
    120  1.1  jmcneill 
    121  1.1  jmcneill static int	wiifb_ioctl(void *, void *, u_long, void *, int, lwp_t *);
    122  1.1  jmcneill static paddr_t	wiifb_mmap(void *, void *, off_t, int);
    123  1.1  jmcneill 
    124  1.1  jmcneill static struct genfb_ops wiifb_ops = {
    125  1.1  jmcneill 	.genfb_ioctl = wiifb_ioctl,
    126  1.1  jmcneill 	.genfb_mmap = wiifb_mmap,
    127  1.1  jmcneill };
    128  1.1  jmcneill 
    129  1.1  jmcneill CFATTACH_DECL_NEW(wiifb, sizeof(struct wiifb_softc),
    130  1.1  jmcneill 	wiifb_match, wiifb_attach, NULL, NULL);
    131  1.1  jmcneill 
    132  1.1  jmcneill static int
    133  1.1  jmcneill wiifb_match(device_t parent, cfdata_t cf, void *aux)
    134  1.1  jmcneill {
    135  1.1  jmcneill 	struct mainbus_attach_args *maa = aux;
    136  1.1  jmcneill 
    137  1.1  jmcneill 	return strcmp(maa->maa_name, "genfb") == 0;
    138  1.1  jmcneill }
    139  1.1  jmcneill 
    140  1.1  jmcneill static void
    141  1.1  jmcneill wiifb_attach(device_t parent, device_t self, void *aux)
    142  1.1  jmcneill {
    143  1.1  jmcneill 	struct wiifb_softc *sc = device_private(self);
    144  1.1  jmcneill 	prop_dictionary_t dict = device_properties(self);
    145  1.1  jmcneill 	struct mainbus_attach_args *maa = aux;
    146  1.1  jmcneill 	int error;
    147  1.1  jmcneill 
    148  1.1  jmcneill 	sc->sc_gen.sc_dev = self;
    149  1.1  jmcneill 	sc->sc_bst = maa->maa_bst;
    150  1.1  jmcneill 	error = bus_space_map(sc->sc_bst, maa->maa_addr, VI_SIZE, 0,
    151  1.1  jmcneill 	    &sc->sc_bsh);
    152  1.1  jmcneill 	if (error != 0) {
    153  1.1  jmcneill 		panic("couldn't map registers");
    154  1.1  jmcneill 	}
    155  1.1  jmcneill 	sc->sc_bits = mapiodev(XFB_START, XFB_SIZE, true);
    156  1.1  jmcneill 
    157  1.1  jmcneill 	wiifb_init(sc);
    158  1.1  jmcneill 	wiifb_set_mode(sc, sc->sc_format, sc->sc_interlaced);
    159  1.1  jmcneill 
    160  1.1  jmcneill 	prop_dictionary_set_uint32(dict, "width", sc->sc_curmode->width);
    161  1.1  jmcneill 	prop_dictionary_set_uint32(dict, "height", sc->sc_curmode->height);
    162  1.1  jmcneill 	prop_dictionary_set_uint8(dict, "depth", 16);
    163  1.1  jmcneill 	prop_dictionary_set_uint32(dict, "address", XFB_START);
    164  1.1  jmcneill 	prop_dictionary_set_uint32(dict, "virtual_address",
    165  1.1  jmcneill 	    (uintptr_t)sc->sc_bits);
    166  1.1  jmcneill 	prop_dictionary_set_uint64(dict, "devcmap", (uintptr_t)wiifb_devcmap);
    167  1.1  jmcneill 
    168  1.1  jmcneill 	genfb_init(&sc->sc_gen);
    169  1.1  jmcneill 
    170  1.1  jmcneill 	aprint_naive("\n");
    171  1.1  jmcneill 	aprint_normal(": %s\n", sc->sc_curmode->name);
    172  1.1  jmcneill 
    173  1.1  jmcneill 	genfb_cnattach();
    174  1.1  jmcneill 	prop_dictionary_set_bool(dict, "is_console", true);
    175  1.1  jmcneill 	genfb_attach(&sc->sc_gen, &wiifb_ops);
    176  1.1  jmcneill }
    177  1.1  jmcneill 
    178  1.1  jmcneill static void
    179  1.1  jmcneill wiifb_init(struct wiifb_softc *sc)
    180  1.1  jmcneill {
    181  1.1  jmcneill 	uint16_t dcr;
    182  1.1  jmcneill 
    183  1.1  jmcneill #if notyet
    184  1.1  jmcneill 	/* Read current display format and interlaced settings. */
    185  1.1  jmcneill 	dcr = RD2(sc, VI_DCR);
    186  1.1  jmcneill 	sc->sc_format = __SHIFTOUT(dcr, VI_DCR_FMT);
    187  1.1  jmcneill 	sc->sc_interlaced = (dcr & VI_DCR_NIN) == 0;
    188  1.1  jmcneill 
    189  1.1  jmcneill 	/* Reset video interface. */
    190  1.1  jmcneill 	WR2(sc, VI_DCR, dcr | VI_DCR_RST);
    191  1.1  jmcneill 	WR2(sc, VI_DCR, dcr & ~VI_DCR_RST);
    192  1.1  jmcneill #else
    193  1.1  jmcneill 	/* Force NTSC 480i and reset video interface. */
    194  1.1  jmcneill 	dcr = RD2(sc, VI_DCR);
    195  1.1  jmcneill 	dcr |= VI_DCR_RST;
    196  1.1  jmcneill 	WR2(sc, VI_DCR, dcr);
    197  1.1  jmcneill 	dcr &= ~VI_DCR_RST;
    198  1.1  jmcneill 	dcr &= ~VI_DCR_FMT;
    199  1.1  jmcneill 	dcr |= __SHIFTIN(VI_DCR_FMT_NTSC, VI_DCR_FMT);
    200  1.1  jmcneill 	dcr &= ~VI_DCR_NIN;
    201  1.1  jmcneill 	WR2(sc, VI_DCR, dcr);
    202  1.1  jmcneill 
    203  1.1  jmcneill 	sc->sc_format = VI_DCR_FMT_NTSC;
    204  1.1  jmcneill 	sc->sc_interlaced = 1;
    205  1.1  jmcneill #endif
    206  1.1  jmcneill }
    207  1.1  jmcneill 
    208  1.1  jmcneill static void
    209  1.1  jmcneill wiifb_set_mode(struct wiifb_softc *sc, uint8_t format, bool interlaced)
    210  1.1  jmcneill {
    211  1.1  jmcneill 	u_int modeidx;
    212  1.1  jmcneill 	u_int strides, reads;
    213  1.1  jmcneill 
    214  1.1  jmcneill 	modeidx = WIIFB_MODE_INDEX(format, interlaced);
    215  1.1  jmcneill 	if (modeidx >= WIIFB_NMODES || wiifb_modes[modeidx].name == NULL) {
    216  1.1  jmcneill 		panic("Unsupported format (0x%x) / interlaced (%d) settings",
    217  1.1  jmcneill 		    sc->sc_format, sc->sc_interlaced);
    218  1.1  jmcneill 	}
    219  1.1  jmcneill 	sc->sc_curmode = &wiifb_modes[modeidx];
    220  1.1  jmcneill 
    221  1.1  jmcneill 	if (modeidx == WIIFB_MODE_INDEX(VI_DCR_FMT_NTSC, 1)) {
    222  1.1  jmcneill 		/* Magic numbers from YAGCD. */
    223  1.1  jmcneill 		WR2(sc, VI_VTR, 0x0f06);
    224  1.1  jmcneill 		WR4(sc, VI_HTR0, 0x476901AD);
    225  1.1  jmcneill 		WR4(sc, VI_HTR1, 0x02EA5140);
    226  1.1  jmcneill 		WR4(sc, VI_VTO, 0x00030018);
    227  1.1  jmcneill 		WR4(sc, VI_VTE, 0x00020019);
    228  1.1  jmcneill 		WR4(sc, VI_BBOI, 0x410C410C);
    229  1.1  jmcneill 		WR4(sc, VI_BBEI, 0x40ED40ED);
    230  1.1  jmcneill 	} else {
    231  1.1  jmcneill 		/*
    232  1.1  jmcneill 		 * Display mode is not supported. Blink the slot LED to
    233  1.1  jmcneill 		 * indicate failure.
    234  1.1  jmcneill 		 */
    235  1.1  jmcneill 		wii_slot_led_blink(WIIFB_ERROR_BLINK_INTERVAL);
    236  1.1  jmcneill 	}
    237  1.1  jmcneill 
    238  1.1  jmcneill 	/* Picture configuration */
    239  1.1  jmcneill 	strides = (sc->sc_curmode->width * 2) / (interlaced ? 16 : 32);
    240  1.1  jmcneill 	reads = (sc->sc_curmode->width * 2) / 32;
    241  1.1  jmcneill 	WR2(sc, VI_PICCONF,
    242  1.1  jmcneill 	    __SHIFTIN(strides, VI_PICCONF_STRIDES) |
    243  1.1  jmcneill 	    __SHIFTIN(reads, VI_PICCONF_READS));
    244  1.1  jmcneill 
    245  1.1  jmcneill 	WR2(sc, VI_HSR, __SHIFTIN(256, VI_HSR_STP));
    246  1.1  jmcneill 
    247  1.1  jmcneill 	/* Video clock configuration */
    248  1.1  jmcneill 	WR2(sc, VI_VICLK,
    249  1.1  jmcneill 	    interlaced ? VI_VICLK_SEL_27MHZ : VI_VICLK_SEL_54MHZ);
    250  1.1  jmcneill 
    251  1.1  jmcneill 	/* Horizontal scaling width */
    252  1.1  jmcneill 	WR2(sc, VI_HSCALINGW, sc->sc_curmode->width);
    253  1.1  jmcneill 
    254  1.1  jmcneill 	/* Set framebuffer address */
    255  1.1  jmcneill 	wiifb_set_fb(sc);
    256  1.1  jmcneill }
    257  1.1  jmcneill 
    258  1.1  jmcneill static void
    259  1.1  jmcneill wiifb_set_fb(struct wiifb_softc *sc)
    260  1.1  jmcneill {
    261  1.1  jmcneill 	uint32_t taddr = XFB_START;
    262  1.1  jmcneill 	uint32_t baddr = taddr + (sc->sc_interlaced ?
    263  1.1  jmcneill 				  sc->sc_curmode->width * 2 : 0);
    264  1.1  jmcneill 
    265  1.1  jmcneill 	WR4(sc, VI_TFBL,
    266  1.1  jmcneill 	    VI_TFBL_PGOFF |
    267  1.1  jmcneill 	    __SHIFTIN((taddr >> 5), VI_TFBL_FBB) |
    268  1.1  jmcneill 	    __SHIFTIN((taddr / 2) & 0xf, VI_TFBL_XOF));
    269  1.1  jmcneill 	WR4(sc, VI_TFBR, 0);
    270  1.1  jmcneill 
    271  1.1  jmcneill 	WR4(sc, VI_BFBL,
    272  1.1  jmcneill 	    VI_BFBL_PGOFF |
    273  1.1  jmcneill 	    __SHIFTIN((baddr >> 5), VI_BFBL_FBB) |
    274  1.1  jmcneill 	    __SHIFTIN((baddr / 2) & 0xf, VI_BFBL_XOF));
    275  1.1  jmcneill 	WR4(sc, VI_BFBR, 0);
    276  1.1  jmcneill }
    277  1.1  jmcneill 
    278  1.1  jmcneill static int
    279  1.1  jmcneill wiifb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag, lwp_t *l)
    280  1.1  jmcneill {
    281  1.1  jmcneill 	struct wiifb_softc *sc = v;
    282  1.1  jmcneill 	struct wsdisplayio_bus_id *busid;
    283  1.1  jmcneill 	struct wsdisplayio_fbinfo *fbi;
    284  1.1  jmcneill 
    285  1.1  jmcneill 	switch (cmd) {
    286  1.1  jmcneill 	case WSDISPLAYIO_GTYPE:
    287  1.1  jmcneill 		*(u_int *)data = WSDISPLAY_TYPE_HOLLYWOOD;
    288  1.1  jmcneill 		return 0;
    289  1.1  jmcneill 	case WSDISPLAYIO_GET_BUSID:
    290  1.1  jmcneill 		busid = data;
    291  1.1  jmcneill 		busid->bus_type = WSDISPLAYIO_BUS_SOC;
    292  1.1  jmcneill 		return 0;
    293  1.1  jmcneill 	case WSDISPLAYIO_GET_FBINFO:
    294  1.1  jmcneill 		fbi = data;
    295  1.1  jmcneill 		/*
    296  1.1  jmcneill 		 * rasops info does not match the pixel encoding due to our
    297  1.1  jmcneill 		 * devcmap, so fill out fbinfo manually instead of relying
    298  1.1  jmcneill 		 * on wsdisplayio_get_fbinfo.
    299  1.1  jmcneill 		 */
    300  1.1  jmcneill 		fbi->fbi_fbsize = XFB_SIZE;
    301  1.1  jmcneill 		fbi->fbi_fboffset = 0;
    302  1.1  jmcneill 		fbi->fbi_width = sc->sc_curmode->width;
    303  1.1  jmcneill 		fbi->fbi_height = sc->sc_curmode->height;
    304  1.1  jmcneill 		fbi->fbi_stride = fbi->fbi_width * 2;
    305  1.1  jmcneill 		fbi->fbi_bitsperpixel = 16;
    306  1.1  jmcneill 		fbi->fbi_pixeltype = WSFB_YUY2;
    307  1.1  jmcneill 		fbi->fbi_flags = WSFB_VRAM_IS_RAM;
    308  1.1  jmcneill 		return 0;
    309  1.1  jmcneill 	}
    310  1.1  jmcneill 
    311  1.1  jmcneill 	return EPASSTHROUGH;
    312  1.1  jmcneill }
    313  1.1  jmcneill 
    314  1.1  jmcneill static paddr_t
    315  1.1  jmcneill wiifb_mmap(void *v, void *vs, off_t off, int prot)
    316  1.1  jmcneill {
    317  1.1  jmcneill 	struct wiifb_softc *sc = v;
    318  1.1  jmcneill 
    319  1.1  jmcneill 	if (off < 0 || off >= XFB_SIZE) {
    320  1.1  jmcneill 		return -1;
    321  1.1  jmcneill 	}
    322  1.1  jmcneill 
    323  1.1  jmcneill 	return bus_space_mmap(sc->sc_bst, XFB_START, off, prot,
    324  1.1  jmcneill 	    BUS_SPACE_MAP_LINEAR | BUS_SPACE_MAP_PREFETCHABLE);
    325  1.1  jmcneill }
    326