2 * Copyright (c) 2005, 2006 Rink Springer <rink@il.fontys.nl>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission
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.
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
32 * This is the syscon(4)-ized version of the Xbox Frame Buffer driver. It
33 * supports about all features required, such as mouse support.
35 * A lot of functions that are not useful to us have not been implemented.
36 * It appears that some functions are never called, but these implementations
37 * are here nevertheless.
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <vm/vm_param.h>
42 #include <sys/kernel.h>
45 #include <sys/module.h>
47 #include <sys/consio.h>
48 #include <sys/limits.h>
52 #include <dev/kbd/kbdreg.h>
55 #include <machine/bus.h>
56 #include <machine/xbox.h>
57 #include <machine/legacyvar.h>
58 #include <dev/fb/fbreg.h>
59 #include <dev/fb/gfb.h>
60 #include <dev/syscons/syscons.h>
63 video_adapter_t sc_va;
65 /* screen height (pixels) */
68 /* screen width (pixels) */
71 /* pointer to the actual XBOX video memory */
74 /* pointer to the font used */
75 const struct gfb_font* sc_font;
78 #define SCREEN_WIDTH 640
79 #define SCREEN_HEIGHT 480
81 #define XBOXFB_DRIVER_NAME "xboxsc"
83 extern const struct gfb_font bold8x16;
85 static vi_probe_t xboxfb_probe;
86 static vi_init_t xboxfb_init;
87 static vi_get_info_t xboxfb_get_info;
88 static vi_query_mode_t xboxfb_query_mode;
89 static vi_set_mode_t xboxfb_set_mode;
90 static vi_save_font_t xboxfb_save_font;
91 static vi_load_font_t xboxfb_load_font;
92 static vi_show_font_t xboxfb_show_font;
93 static vi_save_palette_t xboxfb_save_palette;
94 static vi_load_palette_t xboxfb_load_palette;
95 static vi_set_border_t xboxfb_set_border;
96 static vi_save_state_t xboxfb_save_state;
97 static vi_load_state_t xboxfb_load_state;
98 static vi_set_win_org_t xboxfb_set_win_org;
99 static vi_read_hw_cursor_t xboxfb_read_hw_cursor;
100 static vi_set_hw_cursor_t xboxfb_set_hw_cursor;
101 static vi_set_hw_cursor_shape_t xboxfb_set_hw_cursor_shape;
102 static vi_blank_display_t xboxfb_blank_display;
103 static vi_mmap_t xboxfb_mmap;
104 static vi_ioctl_t xboxfb_ioctl;
105 static vi_clear_t xboxfb_clear;
106 static vi_fill_rect_t xboxfb_fill_rect;
107 static vi_bitblt_t xboxfb_bitblt;
108 static vi_diag_t xboxfb_diag;
109 static vi_save_cursor_palette_t xboxfb_save_cursor_palette;
110 static vi_load_cursor_palette_t xboxfb_load_cursor_palette;
111 static vi_copy_t xboxfb_copy;
112 static vi_putp_t xboxfb_putp;
113 static vi_putc_t xboxfb_putc;
114 static vi_puts_t xboxfb_puts;
115 static vi_putm_t xboxfb_putm;
117 static video_switch_t xboxvidsw = {
118 .probe = xboxfb_probe,
120 .get_info = xboxfb_get_info,
121 .query_mode = xboxfb_query_mode,
122 .set_mode = xboxfb_set_mode,
123 .save_font = xboxfb_save_font,
124 .load_font = xboxfb_load_font,
125 .show_font = xboxfb_show_font,
126 .save_palette = xboxfb_save_palette,
127 .load_palette = xboxfb_load_palette,
128 .set_border = xboxfb_set_border,
129 .save_state = xboxfb_save_state,
130 .load_state = xboxfb_load_state,
131 .set_win_org = xboxfb_set_win_org,
132 .read_hw_cursor = xboxfb_read_hw_cursor,
133 .set_hw_cursor = xboxfb_set_hw_cursor,
134 .set_hw_cursor_shape = xboxfb_set_hw_cursor_shape,
135 .blank_display = xboxfb_blank_display,
137 .ioctl = xboxfb_ioctl,
138 .clear = xboxfb_clear,
139 .fill_rect = xboxfb_fill_rect,
140 .bitblt = xboxfb_bitblt,
144 .save_cursor_palette = xboxfb_save_cursor_palette,
145 .load_cursor_palette = xboxfb_load_cursor_palette,
153 static int xboxfb_configure(int flags);
154 VIDEO_DRIVER(xboxsc, xboxvidsw, xboxfb_configure);
156 static vr_init_t xbr_init;
157 static vr_clear_t xbr_clear;
158 static vr_draw_border_t xbr_draw_border;
159 static vr_draw_t xbr_draw;
160 static vr_set_cursor_t xbr_set_cursor;
161 static vr_draw_cursor_t xbr_draw_cursor;
162 static vr_blink_cursor_t xbr_blink_cursor;
163 static vr_set_mouse_t xbr_set_mouse;
164 static vr_draw_mouse_t xbr_draw_mouse;
167 * We use our own renderer; this is because we must emulate a hardware
170 static sc_rndr_sw_t xboxrend = {
181 RENDERER(xboxsc, 0, xboxrend, gfb_set);
183 static struct xboxfb_softc xboxfb_sc;
185 /* color mappings, from dev/fb/creator.c */
186 static const uint32_t cmap[] = {
187 0x00000000, /* black */
188 0x000000ff, /* blue */
189 0x0000ff00, /* green */
190 0x0000c0c0, /* cyan */
191 0x00ff0000, /* red */
192 0x00c000c0, /* magenta */
193 0x00c0c000, /* brown */
194 0x00c0c0c0, /* light grey */
195 0x00808080, /* dark grey */
196 0x008080ff, /* light blue */
197 0x0080ff80, /* light green */
198 0x0080ffff, /* light cyan */
199 0x00ff8080, /* light red */
200 0x00ff80ff, /* light magenta */
201 0x00ffff80, /* yellow */
202 0x00ffffff /* white */
205 /* mouse pointer from dev/syscons/scgfbrndr.c */
206 static u_char mouse_pointer[16] = {
207 0x00, 0x40, 0x60, 0x70, 0x78, 0x7c, 0x7e, 0x68,
208 0x0c, 0x0c, 0x06, 0x06, 0x00, 0x00, 0x00, 0x00
212 xboxfb_init(int unit, video_adapter_t* adp, int flags)
214 struct xboxfb_softc* sc = &xboxfb_sc;
221 vid_init_struct (adp, XBOXFB_DRIVER_NAME, -1, unit);
222 sc->sc_height = SCREEN_HEIGHT;
223 sc->sc_width = SCREEN_WIDTH;
224 sc->sc_font = &bold8x16;
225 if (!(adp->va_flags & V_ADP_INITIALIZED)) {
227 * We must make a mapping from video framebuffer memory
228 * to real. This is very crude: we map the entire
229 * videomemory to PAGE_SIZE! Since our kernel lives at
230 * it's relocated address range (0xc0xxxxxx), it won't
233 * We use address PAGE_SIZE and up so we can still trap
234 * NULL pointers. Once the real init is called, the
235 * mapping will be done via the OS and stored in a more
236 * sensible location ... but since we're not fully
237 * initialized, this is our only way to go :-(
239 for (i = 0; i < (XBOX_FB_SIZE / PAGE_SIZE); i++) {
240 pmap_kenter (((i + 1) * PAGE_SIZE), XBOX_FB_START + (i * PAGE_SIZE));
242 pmap_kenter ((i + 1) * PAGE_SIZE, XBOX_FB_START_PTR - XBOX_FB_START_PTR % PAGE_SIZE);
243 sc->sc_framebuffer = (char*)PAGE_SIZE;
245 /* ensure the framebuffer is where we want it to be */
246 *(uint32_t*)((i + 1) * PAGE_SIZE + XBOX_FB_START_PTR % PAGE_SIZE) = XBOX_FB_START;
248 /* clear the screen */
249 iptr = (uint32_t*)sc->sc_framebuffer;
250 for (i = 0; i < sc->sc_height * sc->sc_width; i++)
253 /* don't ever do this again! */
254 adp->va_flags |= V_ADP_INITIALIZED;
257 vi->vi_mode = M_TEXT_80x25;
258 vi->vi_cwidth = sc->sc_font->width;
259 vi->vi_cheight = sc->sc_font->height;
260 vi->vi_height = (sc->sc_height / vi->vi_cheight);
261 vi->vi_width = (sc->sc_width / vi->vi_cwidth);
262 vi->vi_flags = V_INFO_COLOR | V_INFO_LINEAR;
263 vi->vi_mem_model = V_INFO_MM_DIRECT;
265 adp->va_flags |= V_ADP_COLOR;
267 if (vid_register(adp) < 0)
270 adp->va_flags |= V_ADP_REGISTERED;
276 xboxfb_probe(int unit, video_adapter_t** adp, void* arg, int flags)
282 xboxfb_configure(int flags)
284 struct xboxfb_softc* sc = &xboxfb_sc;
286 /* Don't init the framebuffer on non-XBOX-es */
287 if (!arch_i386_is_xbox)
291 * If we do only a probe, we are in such an early boot stadium
292 * that we cannot yet do a 'clean' initialization.
294 if (flags & VIO_PROBE_ONLY) {
295 xboxfb_init(0, &sc->sc_va, 0);
299 /* Do a clean mapping of the framebuffer memory */
300 sc->sc_framebuffer = pmap_mapdev (XBOX_FB_START, XBOX_FB_SIZE);
305 sc_identify(driver_t* driver, device_t parent)
307 BUS_ADD_CHILD(parent, INT_MAX, SC_DRIVER_NAME, 0);
311 sc_probe(device_t dev)
313 device_set_desc(dev, "XBox System console");
314 return (sc_probe_unit(device_get_unit(dev), device_get_flags(dev) | SC_AUTODETECT_KBD));
317 static int sc_attach(device_t dev)
319 return (sc_attach_unit(device_get_unit(dev), device_get_flags(dev) | SC_AUTODETECT_KBD));
322 static device_method_t sc_methods[] = {
323 /* Device interface */
324 DEVMETHOD(device_identify, sc_identify),
325 DEVMETHOD(device_probe, sc_probe),
326 DEVMETHOD(device_attach, sc_attach),
330 static driver_t xboxfb_sc_driver = {
336 static devclass_t sc_devclass;
338 DRIVER_MODULE(sc, legacy, xboxfb_sc_driver, sc_devclass, 0, 0);
341 xbr_init(scr_stat* scp)
346 xbr_clear(scr_stat* scp, int c, int attr)
351 xbr_draw_border(scr_stat* scp, int color)
356 xbr_draw(scr_stat* scp, int from, int count, int flip)
358 video_adapter_t* adp = scp->sc->adp;
362 /* Normal printing */
363 vidd_puts(adp, from, (uint16_t*)sc_vtb_pointer(&scp->vtb, from), count);
365 /* This is for selections and such: invert the color attribute */
366 for (i = count; i-- > 0; ++from) {
367 c = sc_vtb_getc(&scp->vtb, from);
368 a = sc_vtb_geta(&scp->vtb, from) >> 8;
369 vidd_putc(adp, from, c, (a >> 4) | ((a & 0xf) << 4));
375 xbr_set_cursor(scr_stat* scp, int base, int height, int blink)
380 xbr_draw_cursor(scr_stat* scp, int at, int blink, int on, int flip)
382 struct xboxfb_softc* sc = &xboxfb_sc;
383 video_adapter_t* adp = scp->sc->adp;
384 uint32_t* ptri = (uint32_t*)sc->sc_framebuffer;
387 if (scp->curs_attr.height <= 0)
390 /* calculate the coordinates in the video buffer */
391 row = (at / adp->va_info.vi_width) * adp->va_info.vi_cheight;
392 col = (at % adp->va_info.vi_width) * adp->va_info.vi_cwidth;
393 ptri += (row * sc->sc_width) + col;
395 /* our cursor consists of simply inverting the char under it */
396 for (i = 0; i < adp->va_info.vi_cheight; i++) {
397 for (j = 0; j < adp->va_info.vi_cwidth; j++) {
398 *ptri++ ^= 0x00FFFFFF;
400 ptri += (sc->sc_width - adp->va_info.vi_cwidth);
405 xbr_blink_cursor(scr_stat* scp, int at, int flip)
410 xbr_set_mouse(scr_stat* scp)
415 xbr_draw_mouse(scr_stat* scp, int x, int y, int on)
417 vidd_putm(scp->sc->adp, x, y, mouse_pointer, 0xffffffff, 16, 8);
422 xboxfb_get_info(video_adapter_t *adp, int mode, video_info_t *info)
424 bcopy(&adp->va_info, info, sizeof(*info));
429 xboxfb_query_mode(video_adapter_t *adp, video_info_t *info)
435 xboxfb_set_mode(video_adapter_t *adp, int mode)
441 xboxfb_save_font(video_adapter_t *adp, int page, int size, int width,
442 u_char *data, int c, int count)
448 xboxfb_load_font(video_adapter_t *adp, int page, int size, int width,
449 u_char *data, int c, int count)
455 xboxfb_show_font(video_adapter_t *adp, int page)
461 xboxfb_save_palette(video_adapter_t *adp, u_char *palette)
467 xboxfb_load_palette(video_adapter_t *adp, u_char *palette)
473 xboxfb_set_border(video_adapter_t *adp, int border)
479 xboxfb_save_state(video_adapter_t *adp, void *p, size_t size)
485 xboxfb_load_state(video_adapter_t *adp, void *p)
491 xboxfb_set_win_org(video_adapter_t *adp, off_t offset)
497 xboxfb_read_hw_cursor(video_adapter_t *adp, int *col, int *row)
505 xboxfb_set_hw_cursor(video_adapter_t *adp, int col, int row)
511 xboxfb_set_hw_cursor_shape(video_adapter_t *adp, int base, int height,
512 int celsize, int blink)
518 xboxfb_blank_display(video_adapter_t *adp, int mode)
524 xboxfb_mmap(video_adapter_t *adp, vm_ooffset_t offset, vm_paddr_t *paddr,
525 int prot, vm_memattr_t *memattr)
531 xboxfb_ioctl(video_adapter_t *adp, u_long cmd, caddr_t data)
533 return (fb_commonioctl(adp, cmd, data));
537 xboxfb_clear(video_adapter_t *adp)
543 xboxfb_fill_rect(video_adapter_t *adp, int val, int x, int y, int cx, int cy)
549 xboxfb_bitblt(video_adapter_t *adp, ...)
555 xboxfb_diag(video_adapter_t *adp, int level)
559 fb_dump_adp_info(adp->va_name, adp, level);
560 xboxfb_get_info(adp, 0, &info);
561 fb_dump_mode_info(adp->va_name, adp, &info, level);
566 xboxfb_save_cursor_palette(video_adapter_t *adp, u_char *palette)
572 xboxfb_load_cursor_palette(video_adapter_t *adp, u_char *palette)
578 xboxfb_copy(video_adapter_t *adp, vm_offset_t src, vm_offset_t dst, int n)
584 xboxfb_putp(video_adapter_t *adp, vm_offset_t off, u_int32_t p, u_int32_t a,
585 int size, int bpp, int bit_ltor, int byte_ltor)
591 xboxfb_putc(video_adapter_t *adp, vm_offset_t off, u_int8_t c, u_int8_t a)
595 struct xboxfb_softc* sc = &xboxfb_sc;
596 uint32_t* ptri = (uint32_t*)sc->sc_framebuffer;
597 const uint8_t* fontdata;
601 /* calculate the position in the frame buffer */
602 row = (off / adp->va_info.vi_width) * adp->va_info.vi_cheight;
603 col = (off % adp->va_info.vi_width) * adp->va_info.vi_cwidth;
604 fontdata = &sc->sc_font->data[c * adp->va_info.vi_cheight];
605 ptri += (row * sc->sc_width) + col;
607 /* Place the character on the screen, pixel by pixel */
608 for (j = 0; j < adp->va_info.vi_cheight; j++) {
610 for (i = 0; i < adp->va_info.vi_cwidth; i++) {
611 clr = (*fontdata & mask) ? cmap[a & 0xf] : cmap[(a >> 4) & 0xf];
615 ptri += (sc->sc_width - adp->va_info.vi_cwidth);
622 xboxfb_puts(video_adapter_t *adp, vm_offset_t off, u_int16_t *s, int len)
626 for (i = 0; i < len; i++) {
627 vidd_putc(adp, off + i, s[i] & 0xff, (s[i] & 0xff00) >> 8);
633 xboxfb_putm(video_adapter_t *adp, int x, int y, u_int8_t *pixel_image,
634 u_int32_t pixel_mask, int size, int width)
636 struct xboxfb_softc* sc = &xboxfb_sc;
637 uint32_t* ptri = (uint32_t*)sc->sc_framebuffer;
640 if (x < 0 || y < 0 || x + width > sc->sc_width || y + (2 * size) > sc->sc_height)
643 ptri += (y * sc->sc_width) + x;
645 /* plot the mousecursor wherever the user wants it */
646 for (j = 0; j < size; j++) {
647 for (i = width; i > 0; i--) {
648 if (pixel_image[j] & (1 << i))
652 ptri += (sc->sc_width - width);