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