wiifb.c revision 1.2 1 1.2 jmcneill /* $NetBSD: wiifb.c,v 1.2 2024/01/21 13:05:29 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.2 jmcneill __KERNEL_RCSID(0, "$NetBSD: wiifb.c,v 1.2 2024/01/21 13:05:29 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.2 jmcneill #include "viio.h"
45 1.1 jmcneill
46 1.1 jmcneill #define WIIFB_ERROR_BLINK_INTERVAL 1000000
47 1.1 jmcneill
48 1.1 jmcneill struct wiifb_mode {
49 1.1 jmcneill const char * name;
50 1.1 jmcneill u_int width;
51 1.1 jmcneill u_int height;
52 1.1 jmcneill u_int lines;
53 1.1 jmcneill };
54 1.1 jmcneill
55 1.1 jmcneill static uint32_t wiifb_devcmap[16] = {
56 1.1 jmcneill 0x00800080, /* Black */
57 1.1 jmcneill 0x1dff1d6b, /* Blue */
58 1.1 jmcneill 0x4b554b4a, /* Green */
59 1.1 jmcneill 0x80808080, /* Cyan */
60 1.1 jmcneill 0x4c544cff, /* Red */
61 1.1 jmcneill 0x3aaa34b5, /* Magenta */
62 1.1 jmcneill 0x7140718a, /* Brown */
63 1.1 jmcneill 0xff80ff80, /* White */
64 1.1 jmcneill 0x80808080, /* Gray */
65 1.1 jmcneill 0xc399c36a, /* Bright Blue */
66 1.1 jmcneill 0xd076d074, /* Bright Green */
67 1.1 jmcneill 0x80808080, /* Bright Cyan */
68 1.1 jmcneill 0x4c544cff, /* Bright Red */
69 1.1 jmcneill 0x3aaa34b5, /* Bright Magenta */
70 1.1 jmcneill 0xe100e194, /* Bright Yellow */
71 1.1 jmcneill 0xff80ff80 /* Bright White */
72 1.1 jmcneill };
73 1.1 jmcneill
74 1.1 jmcneill #define WIIFB_MODE_INDEX(fmt, interlaced) ((fmt << 1) | interlaced)
75 1.1 jmcneill
76 1.1 jmcneill static const struct wiifb_mode wiifb_modes[] = {
77 1.1 jmcneill [WIIFB_MODE_INDEX(VI_DCR_FMT_NTSC, 0)] = {
78 1.1 jmcneill .name = "NTSC 480p",
79 1.1 jmcneill .width = 640,
80 1.1 jmcneill .height = 480,
81 1.1 jmcneill .lines = 525,
82 1.1 jmcneill },
83 1.1 jmcneill [WIIFB_MODE_INDEX(VI_DCR_FMT_NTSC, 1)] = {
84 1.1 jmcneill .name = "NTSC 480i",
85 1.1 jmcneill .width = 640,
86 1.1 jmcneill .height = 480,
87 1.1 jmcneill .lines = 525,
88 1.1 jmcneill },
89 1.1 jmcneill };
90 1.1 jmcneill #define WIIFB_NMODES __arraycount(wiifb_modes)
91 1.1 jmcneill
92 1.1 jmcneill struct wiifb_softc {
93 1.1 jmcneill struct genfb_softc sc_gen;
94 1.1 jmcneill
95 1.1 jmcneill bus_space_tag_t sc_bst;
96 1.1 jmcneill bus_space_handle_t sc_bsh;
97 1.1 jmcneill
98 1.1 jmcneill void *sc_bits;
99 1.1 jmcneill
100 1.1 jmcneill uint8_t sc_format;
101 1.1 jmcneill bool sc_interlaced;
102 1.1 jmcneill
103 1.1 jmcneill const struct wiifb_mode *sc_curmode;
104 1.1 jmcneill };
105 1.1 jmcneill
106 1.1 jmcneill #define RD2(sc, reg) \
107 1.1 jmcneill bus_space_read_2((sc)->sc_bst, (sc)->sc_bsh, (reg))
108 1.1 jmcneill #define RD4(sc, reg) \
109 1.1 jmcneill bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
110 1.1 jmcneill #define WR2(sc, reg, val) \
111 1.1 jmcneill bus_space_write_2((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
112 1.1 jmcneill #define WR4(sc, reg, val) \
113 1.1 jmcneill bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
114 1.1 jmcneill
115 1.1 jmcneill static int wiifb_match(device_t, cfdata_t, void *);
116 1.1 jmcneill static void wiifb_attach(device_t, device_t, void *);
117 1.1 jmcneill
118 1.1 jmcneill static void wiifb_init(struct wiifb_softc *);
119 1.1 jmcneill static void wiifb_set_mode(struct wiifb_softc *, uint8_t, bool);
120 1.1 jmcneill static void wiifb_set_fb(struct wiifb_softc *);
121 1.1 jmcneill
122 1.1 jmcneill static int wiifb_ioctl(void *, void *, u_long, void *, int, lwp_t *);
123 1.1 jmcneill static paddr_t wiifb_mmap(void *, void *, off_t, int);
124 1.1 jmcneill
125 1.1 jmcneill static struct genfb_ops wiifb_ops = {
126 1.1 jmcneill .genfb_ioctl = wiifb_ioctl,
127 1.1 jmcneill .genfb_mmap = wiifb_mmap,
128 1.1 jmcneill };
129 1.1 jmcneill
130 1.1 jmcneill CFATTACH_DECL_NEW(wiifb, sizeof(struct wiifb_softc),
131 1.1 jmcneill wiifb_match, wiifb_attach, NULL, NULL);
132 1.1 jmcneill
133 1.1 jmcneill static int
134 1.1 jmcneill wiifb_match(device_t parent, cfdata_t cf, void *aux)
135 1.1 jmcneill {
136 1.1 jmcneill struct mainbus_attach_args *maa = aux;
137 1.1 jmcneill
138 1.1 jmcneill return strcmp(maa->maa_name, "genfb") == 0;
139 1.1 jmcneill }
140 1.1 jmcneill
141 1.1 jmcneill static void
142 1.1 jmcneill wiifb_attach(device_t parent, device_t self, void *aux)
143 1.1 jmcneill {
144 1.1 jmcneill struct wiifb_softc *sc = device_private(self);
145 1.1 jmcneill prop_dictionary_t dict = device_properties(self);
146 1.1 jmcneill struct mainbus_attach_args *maa = aux;
147 1.1 jmcneill int error;
148 1.1 jmcneill
149 1.1 jmcneill sc->sc_gen.sc_dev = self;
150 1.1 jmcneill sc->sc_bst = maa->maa_bst;
151 1.1 jmcneill error = bus_space_map(sc->sc_bst, maa->maa_addr, VI_SIZE, 0,
152 1.1 jmcneill &sc->sc_bsh);
153 1.1 jmcneill if (error != 0) {
154 1.1 jmcneill panic("couldn't map registers");
155 1.1 jmcneill }
156 1.1 jmcneill sc->sc_bits = mapiodev(XFB_START, XFB_SIZE, true);
157 1.1 jmcneill
158 1.1 jmcneill wiifb_init(sc);
159 1.1 jmcneill wiifb_set_mode(sc, sc->sc_format, sc->sc_interlaced);
160 1.1 jmcneill
161 1.1 jmcneill prop_dictionary_set_uint32(dict, "width", sc->sc_curmode->width);
162 1.1 jmcneill prop_dictionary_set_uint32(dict, "height", sc->sc_curmode->height);
163 1.1 jmcneill prop_dictionary_set_uint8(dict, "depth", 16);
164 1.1 jmcneill prop_dictionary_set_uint32(dict, "address", XFB_START);
165 1.1 jmcneill prop_dictionary_set_uint32(dict, "virtual_address",
166 1.1 jmcneill (uintptr_t)sc->sc_bits);
167 1.1 jmcneill prop_dictionary_set_uint64(dict, "devcmap", (uintptr_t)wiifb_devcmap);
168 1.1 jmcneill
169 1.1 jmcneill genfb_init(&sc->sc_gen);
170 1.1 jmcneill
171 1.1 jmcneill aprint_naive("\n");
172 1.1 jmcneill aprint_normal(": %s\n", sc->sc_curmode->name);
173 1.1 jmcneill
174 1.1 jmcneill genfb_cnattach();
175 1.1 jmcneill prop_dictionary_set_bool(dict, "is_console", true);
176 1.1 jmcneill genfb_attach(&sc->sc_gen, &wiifb_ops);
177 1.1 jmcneill }
178 1.1 jmcneill
179 1.1 jmcneill static void
180 1.1 jmcneill wiifb_init(struct wiifb_softc *sc)
181 1.1 jmcneill {
182 1.1 jmcneill uint16_t dcr;
183 1.2 jmcneill uint16_t visel;
184 1.1 jmcneill
185 1.1 jmcneill /* Read current display format and interlaced settings. */
186 1.1 jmcneill dcr = RD2(sc, VI_DCR);
187 1.2 jmcneill if ((dcr & VI_DCR_ENB) != 0) {
188 1.2 jmcneill sc->sc_format = __SHIFTOUT(dcr, VI_DCR_FMT);
189 1.2 jmcneill sc->sc_interlaced = (dcr & VI_DCR_NIN) == 0;
190 1.2 jmcneill } else {
191 1.2 jmcneill visel = RD2(sc, VI_VISEL);
192 1.2 jmcneill sc->sc_format = VI_DCR_FMT_NTSC;
193 1.2 jmcneill sc->sc_interlaced = (visel & VI_VISEL_COMPONENT_CABLE) == 0;
194 1.2 jmcneill }
195 1.1 jmcneill
196 1.1 jmcneill /* Reset video interface. */
197 1.1 jmcneill WR2(sc, VI_DCR, dcr | VI_DCR_RST);
198 1.1 jmcneill WR2(sc, VI_DCR, dcr & ~VI_DCR_RST);
199 1.1 jmcneill }
200 1.1 jmcneill
201 1.1 jmcneill static void
202 1.1 jmcneill wiifb_set_mode(struct wiifb_softc *sc, uint8_t format, bool interlaced)
203 1.1 jmcneill {
204 1.1 jmcneill u_int modeidx;
205 1.1 jmcneill u_int strides, reads;
206 1.1 jmcneill
207 1.1 jmcneill modeidx = WIIFB_MODE_INDEX(format, interlaced);
208 1.1 jmcneill if (modeidx >= WIIFB_NMODES || wiifb_modes[modeidx].name == NULL) {
209 1.1 jmcneill panic("Unsupported format (0x%x) / interlaced (%d) settings",
210 1.1 jmcneill sc->sc_format, sc->sc_interlaced);
211 1.1 jmcneill }
212 1.1 jmcneill sc->sc_curmode = &wiifb_modes[modeidx];
213 1.1 jmcneill
214 1.1 jmcneill if (modeidx == WIIFB_MODE_INDEX(VI_DCR_FMT_NTSC, 1)) {
215 1.2 jmcneill /* NTSC 480i Magic numbers from YAGCD. */
216 1.1 jmcneill WR2(sc, VI_VTR, 0x0f06);
217 1.1 jmcneill WR4(sc, VI_HTR0, 0x476901AD);
218 1.1 jmcneill WR4(sc, VI_HTR1, 0x02EA5140);
219 1.1 jmcneill WR4(sc, VI_VTO, 0x00030018);
220 1.1 jmcneill WR4(sc, VI_VTE, 0x00020019);
221 1.1 jmcneill WR4(sc, VI_BBOI, 0x410C410C);
222 1.1 jmcneill WR4(sc, VI_BBEI, 0x40ED40ED);
223 1.2 jmcneill } else if (modeidx == WIIFB_MODE_INDEX(VI_DCR_FMT_NTSC, 0)) {
224 1.2 jmcneill /* NTSC 480p */
225 1.2 jmcneill WR2(sc, VI_VTR, 0x1e0c);
226 1.2 jmcneill WR4(sc, VI_HTR0, 0x476901ad);
227 1.2 jmcneill WR4(sc, VI_HTR1, 0x030a4940);
228 1.2 jmcneill WR4(sc, VI_VTO, 0x00060030);
229 1.2 jmcneill WR4(sc, VI_VTE, 0x00060030);
230 1.2 jmcneill WR4(sc, VI_BBOI, 0x81d881d8);
231 1.2 jmcneill WR4(sc, VI_BBEI, 0x81d881d8);
232 1.1 jmcneill } else {
233 1.1 jmcneill /*
234 1.1 jmcneill * Display mode is not supported. Blink the slot LED to
235 1.1 jmcneill * indicate failure.
236 1.1 jmcneill */
237 1.1 jmcneill wii_slot_led_blink(WIIFB_ERROR_BLINK_INTERVAL);
238 1.1 jmcneill }
239 1.1 jmcneill
240 1.1 jmcneill /* Picture configuration */
241 1.1 jmcneill strides = (sc->sc_curmode->width * 2) / (interlaced ? 16 : 32);
242 1.1 jmcneill reads = (sc->sc_curmode->width * 2) / 32;
243 1.1 jmcneill WR2(sc, VI_PICCONF,
244 1.1 jmcneill __SHIFTIN(strides, VI_PICCONF_STRIDES) |
245 1.1 jmcneill __SHIFTIN(reads, VI_PICCONF_READS));
246 1.1 jmcneill
247 1.2 jmcneill /* Horizontal scaler configuration */
248 1.2 jmcneill if (interlaced) {
249 1.2 jmcneill WR2(sc, VI_HSR, __SHIFTIN(256, VI_HSR_STP));
250 1.2 jmcneill } else {
251 1.2 jmcneill WR2(sc, VI_HSR, __SHIFTIN(244, VI_HSR_STP) | VI_HSR_HS_EN);
252 1.2 jmcneill }
253 1.1 jmcneill
254 1.1 jmcneill /* Video clock configuration */
255 1.1 jmcneill WR2(sc, VI_VICLK,
256 1.1 jmcneill interlaced ? VI_VICLK_SEL_27MHZ : VI_VICLK_SEL_54MHZ);
257 1.1 jmcneill
258 1.1 jmcneill /* Horizontal scaling width */
259 1.1 jmcneill WR2(sc, VI_HSCALINGW, sc->sc_curmode->width);
260 1.1 jmcneill
261 1.1 jmcneill /* Set framebuffer address */
262 1.1 jmcneill wiifb_set_fb(sc);
263 1.1 jmcneill }
264 1.1 jmcneill
265 1.1 jmcneill static void
266 1.1 jmcneill wiifb_set_fb(struct wiifb_softc *sc)
267 1.1 jmcneill {
268 1.1 jmcneill uint32_t taddr = XFB_START;
269 1.1 jmcneill uint32_t baddr = taddr + (sc->sc_interlaced ?
270 1.1 jmcneill sc->sc_curmode->width * 2 : 0);
271 1.1 jmcneill
272 1.1 jmcneill WR4(sc, VI_TFBL,
273 1.1 jmcneill VI_TFBL_PGOFF |
274 1.1 jmcneill __SHIFTIN((taddr >> 5), VI_TFBL_FBB) |
275 1.1 jmcneill __SHIFTIN((taddr / 2) & 0xf, VI_TFBL_XOF));
276 1.1 jmcneill WR4(sc, VI_TFBR, 0);
277 1.1 jmcneill
278 1.1 jmcneill WR4(sc, VI_BFBL,
279 1.1 jmcneill VI_BFBL_PGOFF |
280 1.1 jmcneill __SHIFTIN((baddr >> 5), VI_BFBL_FBB) |
281 1.1 jmcneill __SHIFTIN((baddr / 2) & 0xf, VI_BFBL_XOF));
282 1.1 jmcneill WR4(sc, VI_BFBR, 0);
283 1.1 jmcneill }
284 1.1 jmcneill
285 1.1 jmcneill static int
286 1.1 jmcneill wiifb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag, lwp_t *l)
287 1.1 jmcneill {
288 1.1 jmcneill struct wiifb_softc *sc = v;
289 1.1 jmcneill struct wsdisplayio_bus_id *busid;
290 1.1 jmcneill struct wsdisplayio_fbinfo *fbi;
291 1.2 jmcneill struct vi_regs *vr;
292 1.1 jmcneill
293 1.1 jmcneill switch (cmd) {
294 1.1 jmcneill case WSDISPLAYIO_GTYPE:
295 1.1 jmcneill *(u_int *)data = WSDISPLAY_TYPE_HOLLYWOOD;
296 1.1 jmcneill return 0;
297 1.1 jmcneill case WSDISPLAYIO_GET_BUSID:
298 1.1 jmcneill busid = data;
299 1.1 jmcneill busid->bus_type = WSDISPLAYIO_BUS_SOC;
300 1.1 jmcneill return 0;
301 1.1 jmcneill case WSDISPLAYIO_GET_FBINFO:
302 1.1 jmcneill fbi = data;
303 1.1 jmcneill /*
304 1.1 jmcneill * rasops info does not match the pixel encoding due to our
305 1.1 jmcneill * devcmap, so fill out fbinfo manually instead of relying
306 1.1 jmcneill * on wsdisplayio_get_fbinfo.
307 1.1 jmcneill */
308 1.1 jmcneill fbi->fbi_fbsize = XFB_SIZE;
309 1.1 jmcneill fbi->fbi_fboffset = 0;
310 1.1 jmcneill fbi->fbi_width = sc->sc_curmode->width;
311 1.1 jmcneill fbi->fbi_height = sc->sc_curmode->height;
312 1.1 jmcneill fbi->fbi_stride = fbi->fbi_width * 2;
313 1.1 jmcneill fbi->fbi_bitsperpixel = 16;
314 1.1 jmcneill fbi->fbi_pixeltype = WSFB_YUY2;
315 1.1 jmcneill fbi->fbi_flags = WSFB_VRAM_IS_RAM;
316 1.1 jmcneill return 0;
317 1.2 jmcneill
318 1.2 jmcneill case VIIO_GETREGS:
319 1.2 jmcneill case VIIO_SETREGS:
320 1.2 jmcneill vr = data;
321 1.2 jmcneill switch (vr->bits) {
322 1.2 jmcneill case 16:
323 1.2 jmcneill if ((vr->reg & 1) != 0) {
324 1.2 jmcneill return EINVAL;
325 1.2 jmcneill }
326 1.2 jmcneill if (cmd == VIIO_GETREGS) {
327 1.2 jmcneill vr->val16 = RD2(sc, vr->reg);
328 1.2 jmcneill } else {
329 1.2 jmcneill WR2(sc, vr->reg, vr->val16);
330 1.2 jmcneill }
331 1.2 jmcneill return 0;
332 1.2 jmcneill case 32:
333 1.2 jmcneill if ((vr->reg & 3) != 0) {
334 1.2 jmcneill return EINVAL;
335 1.2 jmcneill }
336 1.2 jmcneill if (cmd == VIIO_GETREGS) {
337 1.2 jmcneill vr->val32 = RD4(sc, vr->reg);
338 1.2 jmcneill } else {
339 1.2 jmcneill WR4(sc, vr->reg, vr->val32);
340 1.2 jmcneill }
341 1.2 jmcneill return 0;
342 1.2 jmcneill default:
343 1.2 jmcneill return EINVAL;
344 1.2 jmcneill }
345 1.2 jmcneill return 0;
346 1.1 jmcneill }
347 1.1 jmcneill
348 1.1 jmcneill return EPASSTHROUGH;
349 1.1 jmcneill }
350 1.1 jmcneill
351 1.1 jmcneill static paddr_t
352 1.1 jmcneill wiifb_mmap(void *v, void *vs, off_t off, int prot)
353 1.1 jmcneill {
354 1.1 jmcneill struct wiifb_softc *sc = v;
355 1.1 jmcneill
356 1.1 jmcneill if (off < 0 || off >= XFB_SIZE) {
357 1.1 jmcneill return -1;
358 1.1 jmcneill }
359 1.1 jmcneill
360 1.1 jmcneill return bus_space_mmap(sc->sc_bst, XFB_START, off, prot,
361 1.1 jmcneill BUS_SPACE_MAP_LINEAR | BUS_SPACE_MAP_PREFETCHABLE);
362 1.1 jmcneill }
363