2 * Copyright (C) 2012 Margarida Gouveia
3 * Copyright (c) 2003 Peter Grehan
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer,
11 * without modification, immediately at the beginning of the file.
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.
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, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/module.h>
34 #include <sys/kernel.h>
35 #include <sys/sysctl.h>
36 #include <sys/limits.h>
40 #include <sys/fcntl.h>
41 #include <sys/malloc.h>
43 #include <sys/consio.h>
48 #include <machine/bus.h>
49 #include <machine/sc_machdep.h>
50 #include <machine/platform.h>
51 #include <machine/pmap.h>
55 #include <dev/fb/fbreg.h>
56 #include <dev/syscons/syscons.h>
58 #include <powerpc/wii/wii_fbreg.h>
59 #include <powerpc/wii/wii_fbvar.h>
62 * Driver for the Nintendo Wii's framebuffer. Based on Linux's gcnfb.c.
68 static int wiifb_scprobe(device_t);
69 static int wiifb_scattach(device_t);
71 static device_method_t wiifb_sc_methods[] = {
72 DEVMETHOD(device_probe, wiifb_scprobe),
73 DEVMETHOD(device_attach, wiifb_scattach),
78 static driver_t wiifb_sc_driver = {
84 static devclass_t sc_devclass;
86 DRIVER_MODULE(sc, wiibus, wiifb_sc_driver, sc_devclass, 0, 0);
89 wiifb_scprobe(device_t dev)
93 device_set_desc(dev, "Nintendo Wii frambuffer");
95 error = sc_probe_unit(device_get_unit(dev),
96 device_get_flags(dev) | SC_AUTODETECT_KBD);
100 /* This is a fake device, so make sure we added it ourselves */
101 return (BUS_PROBE_NOWILDCARD);
105 wiifb_scattach(device_t dev)
108 return (sc_attach_unit(device_get_unit(dev),
109 device_get_flags(dev) | SC_AUTODETECT_KBD));
113 * Video driver routines and glue.
115 static void wiifb_reset_video(struct wiifb_softc *);
116 static void wiifb_enable_interrupts(struct wiifb_softc *);
117 static void wiifb_configure_tv_mode(struct wiifb_softc *);
118 static void wiifb_setup_framebuffer(struct wiifb_softc *);
119 static int wiifb_configure(int);
120 static vi_probe_t wiifb_probe;
121 static vi_init_t wiifb_init;
122 static vi_get_info_t wiifb_get_info;
123 static vi_query_mode_t wiifb_query_mode;
124 static vi_set_mode_t wiifb_set_mode;
125 static vi_save_font_t wiifb_save_font;
126 static vi_load_font_t wiifb_load_font;
127 static vi_show_font_t wiifb_show_font;
128 static vi_save_palette_t wiifb_save_palette;
129 static vi_load_palette_t wiifb_load_palette;
130 static vi_set_border_t wiifb_set_border;
131 static vi_save_state_t wiifb_save_state;
132 static vi_load_state_t wiifb_load_state;
133 static vi_set_win_org_t wiifb_set_win_org;
134 static vi_read_hw_cursor_t wiifb_read_hw_cursor;
135 static vi_set_hw_cursor_t wiifb_set_hw_cursor;
136 static vi_set_hw_cursor_shape_t wiifb_set_hw_cursor_shape;
137 static vi_blank_display_t wiifb_blank_display;
138 static vi_mmap_t wiifb_mmap;
139 static vi_ioctl_t wiifb_ioctl;
140 static vi_clear_t wiifb_clear;
141 static vi_fill_rect_t wiifb_fill_rect;
142 static vi_bitblt_t wiifb_bitblt;
143 static vi_diag_t wiifb_diag;
144 static vi_save_cursor_palette_t wiifb_save_cursor_palette;
145 static vi_load_cursor_palette_t wiifb_load_cursor_palette;
146 static vi_copy_t wiifb_copy;
147 static vi_putp_t wiifb_putp;
148 static vi_putc_t wiifb_putc;
149 static vi_puts_t wiifb_puts;
150 static vi_putm_t wiifb_putm;
152 static video_switch_t wiifbvidsw = {
153 .probe = wiifb_probe,
155 .get_info = wiifb_get_info,
156 .query_mode = wiifb_query_mode,
157 .set_mode = wiifb_set_mode,
158 .save_font = wiifb_save_font,
159 .load_font = wiifb_load_font,
160 .show_font = wiifb_show_font,
161 .save_palette = wiifb_save_palette,
162 .load_palette = wiifb_load_palette,
163 .set_border = wiifb_set_border,
164 .save_state = wiifb_save_state,
165 .load_state = wiifb_load_state,
166 .set_win_org = wiifb_set_win_org,
167 .read_hw_cursor = wiifb_read_hw_cursor,
168 .set_hw_cursor = wiifb_set_hw_cursor,
169 .set_hw_cursor_shape = wiifb_set_hw_cursor_shape,
170 .blank_display = wiifb_blank_display,
172 .ioctl = wiifb_ioctl,
173 .clear = wiifb_clear,
174 .fill_rect = wiifb_fill_rect,
175 .bitblt = wiifb_bitblt,
177 .save_cursor_palette = wiifb_save_cursor_palette,
178 .load_cursor_palette = wiifb_load_cursor_palette,
186 VIDEO_DRIVER(wiifb, wiifbvidsw, wiifb_configure);
188 extern sc_rndr_sw_t txtrndrsw;
189 RENDERER(wiifb, 0, txtrndrsw, gfb_set);
190 RENDERER_MODULE(wiifb, gfb_set);
192 static struct wiifb_softc wiifb_softc;
193 static uint16_t wiifb_static_window[ROW*COL];
194 extern u_char dflt_font_8[];
197 * Map the syscons colors to YUY2 (Y'UV422).
198 * Some colours are an approximation.
200 * The Wii has a 16 bit pixel, so each 32 bit DWORD encodes
201 * two pixels. The upper 16 bits is for pixel 0 (left hand pixel
202 * in a pair), the lower 16 bits is for pixel 1.
204 * For now, we're going to ignore that entirely and just use the
205 * lower 16 bits for each pixel. We'll take the upper value into
208 static uint32_t wiifb_cmap[16] = {
209 0x00800080, /* Black */
210 0x1dff1d6b, /* Blue */
211 0x4b554b4a, /* Green */
212 0x80808080, /* Cyan */
213 0x4c544cff, /* Red */
214 0x3aaa34b5, /* Magenta */
215 0x7140718a, /* Brown */
216 0xff80ff80, /* White */
217 0x80808080, /* Gray */
218 0xc399c36a, /* Bright Blue */
219 0xd076d074, /* Bright Green */
220 0x80808080, /* Bright Cyan */
221 0x4c544cff, /* Bright Red */
222 0x3aaa34b5, /* Bright Magenta */
223 0xe100e194, /* Bright Yellow */
224 0xff80ff80 /* Bright White */
227 static struct wiifb_mode_desc wiifb_modes[] = {
228 [WIIFB_MODE_NTSC_480i] = {
232 WIIFB_MODE_FLAG_INTERLACED,
234 [WIIFB_MODE_NTSC_480p] = {
238 WIIFB_MODE_FLAG_PROGRESSIVE,
240 [WIIFB_MODE_PAL_576i] = {
244 WIIFB_MODE_FLAG_INTERLACED,
246 [WIIFB_MODE_PAL_480i] = {
250 WIIFB_MODE_FLAG_INTERLACED,
252 [WIIFB_MODE_PAL_480p] = {
256 WIIFB_MODE_FLAG_PROGRESSIVE,
260 static const uint32_t wiifb_filter_coeft[] = {
261 0x1ae771f0, 0x0db4a574, 0x00c1188e, 0xc4c0cbe2, 0xfcecdecf,
262 0x13130f08, 0x00080c0f
266 wiifb_background(uint8_t attr)
273 wiifb_foreground(uint8_t attr)
276 return (attr & 0x0f);
280 wiifb_reset_video(struct wiifb_softc *sc)
282 struct wiifb_dispcfg dc;
284 wiifb_dispcfg_read(sc, &dc);
286 wiifb_dispcfg_write(sc, &dc);
288 wiifb_dispcfg_write(sc, &dc);
292 wiifb_enable_interrupts(struct wiifb_softc *sc)
294 struct wiifb_dispint di;
298 * Display Interrupt 0
304 wiifb_dispint_write(sc, 0, &di);
307 * Display Interrupt 1
309 di.di_htiming = sc->sc_format == WIIFB_FORMAT_PAL ? 433 : 430;
310 di.di_vtiming = sc->sc_mode->fd_lines;
313 if (sc->sc_mode->fd_flags & WIIFB_MODE_FLAG_INTERLACED)
315 wiifb_dispint_write(sc, 1, &di);
318 * Display Interrupts 2 and 3 are not used.
320 memset(&di, 0, sizeof(di));
321 wiifb_dispint_write(sc, 2, &di);
322 wiifb_dispint_write(sc, 3, &di);
324 memset(&di, 0, sizeof(di));
325 wiifb_dispint_write(sc, 0, &di);
326 wiifb_dispint_write(sc, 1, &di);
327 wiifb_dispint_write(sc, 2, &di);
328 wiifb_dispint_write(sc, 3, &di);
333 * Reference gcnfb.c for an in depth explanation.
334 * XXX only works with NTSC.
337 wiifb_configure_tv_mode(struct wiifb_softc *sc)
339 struct wiifb_vtiming vt;
340 struct wiifb_hscaling hs;
341 struct wiifb_htiming0 ht0;
342 struct wiifb_htiming1 ht1;
343 struct wiifb_vtimingodd vto;
344 struct wiifb_vtimingeven vte;
345 struct wiifb_burstblankodd bbo;
346 struct wiifb_burstblankeven bbe;
347 struct wiifb_picconf pc;
348 struct wiifb_mode_desc *mode = sc->sc_mode;
349 unsigned int height = mode->fd_height;
350 unsigned int width = mode->fd_width;
351 unsigned int eqpulse, interlacebias, shift;
352 const unsigned int maxwidth = 714;
353 unsigned int hblanking = maxwidth - width;
354 unsigned int hmargin = hblanking / 2;
355 unsigned int A = 20 + hmargin, C = 60 + hblanking - hmargin;
356 unsigned int maxheight = 484;
357 unsigned int P = 2 * (20 - 10 + 1);
359 unsigned int vblanking = maxheight - height;
360 unsigned int vmargin = vblanking / 2;
361 unsigned int prb = vmargin;
362 unsigned int psb = vblanking - vmargin;
368 if (mode->fd_flags & WIIFB_MODE_FLAG_INTERLACED) {
369 vt.vt_actvideo = height / 2;
373 vt.vt_actvideo = height;
377 /* Lines of equalization */
378 if (mode->fd_lines == 625)
382 vt.vt_eqpulse = eqpulse << shift;
383 wiifb_vtiming_write(sc, &vt);
386 * Horizontal timings.
388 ht0.ht0_hlinew = 858 / 2;
390 ht0.ht0_hcolourstart = 71;
391 ht0.ht0_hcolourend = 71 + 34;
392 ht1.ht1_hblankstart = (858 / 2) - A;
393 ht1.ht1_hblankend = 64 + C;
394 wiifb_htiming0_write(sc, &ht0);
395 wiifb_htiming1_write(sc, &ht1);
398 * Vertical timing odd/even.
401 vto.vto_preb = (P + interlacebias + prb) << shift;
402 vto.vto_postb = (Q - interlacebias + psb) << shift;
403 vte.vte_preb = (P + prb) << shift;
404 vte.vte_postb = (Q - psb) << shift;
406 /* XXX if this isn't 0, it doesn't work? */
409 vte.vte_preb = (P + interlacebias + prb) << shift;
410 vte.vte_postb = (Q - interlacebias + psb) << shift;
411 vto.vto_preb = (P + prb) << shift;
412 vto.vto_postb = (Q - psb) << shift;
414 wiifb_vtimingodd_write(sc, &vto);
415 wiifb_vtimingeven_write(sc, &vte);
418 * Burst blanking odd/even interval.
420 bbo.bbo_bs1 = 2 * (18 - 7 + 1);
421 bbe.bbe_bs2 = bbo.bbo_bs3 = bbe.bbe_bs4 = bbo.bbo_bs1;
422 bbo.bbo_be1 = 2 * (525 - 7 + 1);
423 bbe.bbe_be2 = bbo.bbo_be3 = bbe.bbe_be4 = bbo.bbo_be1;
424 wiifb_burstblankodd_write(sc, &bbo);
425 wiifb_burstblankeven_write(sc, &bbe);
428 * Picture configuration.
430 pc.pc_strides = (mode->fd_width * 2) / 32;
431 if (mode->fd_flags & WIIFB_MODE_FLAG_INTERLACED)
433 pc.pc_reads = (mode->fd_width * 2) / 32;
434 wiifb_picconf_write(sc, &pc);
437 * Horizontal scaling disabled.
441 wiifb_hscaling_write(sc, &hs);
444 * Filter coeficient table.
446 for (i = 0; i < 7; i++)
447 wiifb_filtcoeft_write(sc, i, wiifb_filter_coeft[i]);
452 wiifb_antialias_write(sc, 0x00ff0000);
457 wiifb_videoclk_write(sc,
458 mode->fd_flags & WIIFB_MODE_FLAG_INTERLACED ? 0 : 1);
461 * Disable horizontal scaling width.
463 wiifb_hscalingw_write(sc, mode->fd_width);
466 * DEBUG mode borders. Not used.
468 wiifb_hborderend_write(sc, 0);
469 wiifb_hborderstart_write(sc, 0);
472 * XXX unknown registers.
474 wiifb_unknown1_write(sc, 0x00ff);
475 wiifb_unknown2_write(sc, 0x00ff00ff);
476 wiifb_unknown3_write(sc, 0x00ff00ff);
480 wiifb_setup_framebuffer(struct wiifb_softc *sc)
482 intptr_t addr = sc->sc_fb_addr;
483 struct wiifb_topfieldbasel tfbl;
484 struct wiifb_bottomfieldbasel bfbl;
485 struct wiifb_topfieldbaser tfbr;
486 struct wiifb_bottomfieldbaser bfbr;
488 tfbl.tfbl_fbaddr = addr >> 5;
489 tfbl.tfbl_xoffset = (addr / 2) & 0xf;
490 tfbl.tfbl_pageoffbit = 1;
491 wiifb_topfieldbasel_write(sc, &tfbl);
493 if (sc->sc_mode->fd_flags & WIIFB_MODE_FLAG_INTERLACED)
494 addr += sc->sc_mode->fd_width * 2;
495 bfbl.bfbl_fbaddr = addr >> 5;
496 bfbl.bfbl_xoffset = (addr / 2) & 0xf;
497 bfbl.bfbl_pageoffbit = 1;
498 wiifb_bottomfieldbasel_write(sc, &bfbl);
501 * Only used used for 3D.
503 memset(&tfbr, 0, sizeof(tfbr));
504 memset(&bfbr, 0, sizeof(bfbr));
505 wiifb_topfieldbaser_write(sc, &tfbr);
506 wiifb_bottomfieldbaser_write(sc, &bfbr);
510 wiifb_configure(int flags)
512 struct wiifb_softc *sc;
513 struct wiifb_dispcfg dc;
517 if (sc->sc_initialized) {
518 /* XXX We should instead use bus_space */
519 sc->sc_fb_addr = (intptr_t)pmap_mapdev(WIIFB_FB_ADDR, WIIFB_FB_LEN);
520 sc->sc_reg_addr = (intptr_t)pmap_mapdev(WIIFB_REG_ADDR, WIIFB_REG_LEN);
526 sc->sc_fb_addr = WIIFB_FB_ADDR;
527 sc->sc_fb_size = WIIFB_FB_LEN;
529 sc->sc_reg_addr = WIIFB_REG_ADDR;
530 sc->sc_reg_size = WIIFB_REG_LEN;
532 wiifb_reset_video(sc);
533 wiifb_dispcfg_read(sc, &dc);
534 sc->sc_format = dc.dc_format;
535 sc->sc_component = wiifb_component_enabled(sc);
536 progressive = dc.dc_noninterlaced;
537 switch (sc->sc_format) {
538 case WIIFB_FORMAT_MPAL:
539 case WIIFB_FORMAT_DEBUG:
540 case WIIFB_FORMAT_NTSC:
541 sc->sc_mode = progressive ?
542 &wiifb_modes[WIIFB_MODE_NTSC_480p] :
543 &wiifb_modes[WIIFB_MODE_NTSC_480i];
545 case WIIFB_FORMAT_PAL:
546 sc->sc_mode = progressive ?
547 &wiifb_modes[WIIFB_MODE_PAL_480p] :
548 &wiifb_modes[WIIFB_MODE_PAL_480i];
551 sc->sc_height = sc->sc_mode->fd_height;
552 sc->sc_width = sc->sc_mode->fd_width;
553 /* Usually we multiply by 4, but I think this looks better. */
554 sc->sc_stride = sc->sc_width * 2;
556 wiifb_init(0, &sc->sc_va, 0);
558 sc->sc_initialized = 1;
564 wiifb_probe(int unit, video_adapter_t **adp, void *arg, int flags)
571 wiifb_init(int unit, video_adapter_t *adp, int flags)
573 struct wiifb_softc *sc;
576 sc = (struct wiifb_softc *)adp;
579 vid_init_struct(adp, "wiifb", -1, unit);
581 sc->sc_font = dflt_font_8;
582 vi->vi_cheight = WIIFB_FONT_HEIGHT;
583 vi->vi_width = sc->sc_width/8;
584 vi->vi_height = sc->sc_height/vi->vi_cheight;
588 * Clamp width/height to syscons maximums
590 if (vi->vi_width > COL)
592 if (vi->vi_height > ROW)
595 sc->sc_xmargin = (sc->sc_width - (vi->vi_width * vi->vi_cwidth)) / 2;
596 sc->sc_ymargin = (sc->sc_height - (vi->vi_height * vi->vi_cheight))/2;
598 adp->va_window = (vm_offset_t) wiifb_static_window;
599 /* XXX no colour support */
600 adp->va_flags |= V_ADP_FONT | /*V_ADP_COLOR |*/ V_ADP_MODECHANGE;
602 vid_register(&sc->sc_va);
604 wiifb_configure_tv_mode(sc);
605 wiifb_setup_framebuffer(sc);
606 wiifb_enable_interrupts(sc);
613 wiifb_get_info(video_adapter_t *adp, int mode, video_info_t *info)
616 bcopy(&adp->va_info, info, sizeof(*info));
621 wiifb_query_mode(video_adapter_t *adp, video_info_t *info)
628 wiifb_set_mode(video_adapter_t *adp, int mode)
635 wiifb_save_font(video_adapter_t *adp, int page, int size, int width,
636 u_char *data, int c, int count)
643 wiifb_load_font(video_adapter_t *adp, int page, int size, int width,
644 u_char *data, int c, int count)
646 struct wiifb_softc *sc = (struct wiifb_softc *)adp;
654 wiifb_show_font(video_adapter_t *adp, int page)
661 wiifb_save_palette(video_adapter_t *adp, u_char *palette)
668 wiifb_load_palette(video_adapter_t *adp, u_char *palette)
675 wiifb_set_border(video_adapter_t *adp, int border)
678 return (wiifb_blank_display(adp, border));
682 wiifb_save_state(video_adapter_t *adp, void *p, size_t size)
689 wiifb_load_state(video_adapter_t *adp, void *p)
696 wiifb_set_win_org(video_adapter_t *adp, off_t offset)
703 wiifb_read_hw_cursor(video_adapter_t *adp, int *col, int *row)
712 wiifb_set_hw_cursor(video_adapter_t *adp, int col, int row)
719 wiifb_set_hw_cursor_shape(video_adapter_t *adp, int base, int height,
720 int celsize, int blink)
727 wiifb_blank_display(video_adapter_t *adp, int mode)
729 struct wiifb_softc *sc = (struct wiifb_softc *)adp;
732 for (p = (uint32_t *)sc->sc_fb_addr;
733 p < (uint32_t *)(sc->sc_fb_addr + sc->sc_fb_size);
735 *p = wiifb_cmap[wiifb_background(SC_NORM_ATTR)];
741 wiifb_mmap(video_adapter_t *adp, vm_ooffset_t offset, vm_paddr_t *paddr,
742 int prot, vm_memattr_t *memattr)
744 struct wiifb_softc *sc;
746 sc = (struct wiifb_softc *)adp;
749 * This might be a legacy VGA mem request: if so, just point it at the
750 * framebuffer, since it shouldn't be touched
752 if (offset < sc->sc_stride*sc->sc_height) {
753 *paddr = sc->sc_fb_addr + offset;
761 wiifb_ioctl(video_adapter_t *adp, u_long cmd, caddr_t data)
768 wiifb_clear(video_adapter_t *adp)
771 return (wiifb_blank_display(adp, 0));
775 wiifb_fill_rect(video_adapter_t *adp, int val, int x, int y, int cx, int cy)
782 wiifb_bitblt(video_adapter_t *adp, ...)
789 wiifb_diag(video_adapter_t *adp, int level)
796 wiifb_save_cursor_palette(video_adapter_t *adp, u_char *palette)
803 wiifb_load_cursor_palette(video_adapter_t *adp, u_char *palette)
810 wiifb_copy(video_adapter_t *adp, vm_offset_t src, vm_offset_t dst, int n)
817 wiifb_putp(video_adapter_t *adp, vm_offset_t off, uint32_t p, uint32_t a,
818 int size, int bpp, int bit_ltor, int byte_ltor)
825 wiifb_putc(video_adapter_t *adp, vm_offset_t off, uint8_t c, uint8_t a)
827 struct wiifb_softc *sc;
834 unsigned long pixel[2];
836 sc = (struct wiifb_softc *)adp;
837 row = (off / adp->va_info.vi_width) * adp->va_info.vi_cheight;
838 col = (off % adp->va_info.vi_width) * adp->va_info.vi_cwidth / 2;
839 p = sc->sc_font + c*WIIFB_FONT_HEIGHT;
840 addr = (uint32_t *)sc->sc_fb_addr
841 + (row + sc->sc_ymargin)*(sc->sc_stride/4)
842 + col + sc->sc_xmargin;
844 bg = wiifb_cmap[wiifb_background(a)];
845 fg = wiifb_cmap[wiifb_foreground(a)];
847 for (i = 0; i < WIIFB_FONT_HEIGHT; i++) {
848 for (j = 0, k = 7; j < 4; j++, k--) {
849 if ((p[i] & (1 << k)) == 0)
854 if ((p[i] & (1 << k)) == 0)
859 addr[j] = (pixel[0] & 0xffff00ff) |
860 (pixel[1] & 0x0000ff00);
862 addr += (sc->sc_stride/4);
869 wiifb_puts(video_adapter_t *adp, vm_offset_t off, u_int16_t *s, int len)
873 for (i = 0; i < len; i++)
874 wiifb_putc(adp, off + i, s[i] & 0xff, (s[i] & 0xff00) >> 8);
880 wiifb_putm(video_adapter_t *adp, int x, int y, uint8_t *pixel_image,
881 uint32_t pixel_mask, int size, int width)