1/*-
2 * Copyright (c) 2005, 2006 Rink Springer <rink@il.fontys.nl>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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
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, 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.
26 */
27
28#include <sys/cdefs.h>
29__FBSDID("$FreeBSD$");
30
31/*
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.
34 *
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.
38 */
39#include <sys/param.h>
40#include <sys/systm.h>
41#include <vm/vm_param.h>
42#include <sys/kernel.h>
43#include <sys/bus.h>
44#include <sys/cons.h>
45#include <sys/module.h>
46#include <sys/conf.h>
47#include <sys/consio.h>
48#include <sys/limits.h>
49#include <sys/tty.h>
50#include <sys/kbio.h>
51#include <sys/fbio.h>
52#include <dev/kbd/kbdreg.h>
53#include <vm/vm.h>
54#include <vm/pmap.h>
55#include <machine/bus.h>
56#include <machine/xbox.h>
57#include <x86/legacyvar.h>
58#include <dev/fb/fbreg.h>
59#include <dev/fb/gfb.h>
60#include <dev/syscons/syscons.h>
61
62struct xboxfb_softc {
63	video_adapter_t	sc_va;
64
65	/* screen height (pixels) */
66	uint32_t sc_height;
67
68	/* screen width (pixels) */
69	uint32_t sc_width;
70
71	/* pointer to the actual XBOX video memory */
72	char* sc_framebuffer;
73
74	/* pointer to the font used */
75	const struct gfb_font* sc_font;
76};
77
78#define SCREEN_WIDTH	640
79#define SCREEN_HEIGHT	480
80
81#define XBOXFB_DRIVER_NAME "xboxsc"
82
83extern const struct gfb_font bold8x16;
84
85static vi_probe_t xboxfb_probe;
86static vi_init_t xboxfb_init;
87static vi_get_info_t xboxfb_get_info;
88static vi_query_mode_t xboxfb_query_mode;
89static vi_set_mode_t xboxfb_set_mode;
90static vi_save_font_t xboxfb_save_font;
91static vi_load_font_t xboxfb_load_font;
92static vi_show_font_t xboxfb_show_font;
93static vi_save_palette_t xboxfb_save_palette;
94static vi_load_palette_t xboxfb_load_palette;
95static vi_set_border_t xboxfb_set_border;
96static vi_save_state_t xboxfb_save_state;
97static vi_load_state_t xboxfb_load_state;
98static vi_set_win_org_t xboxfb_set_win_org;
99static vi_read_hw_cursor_t xboxfb_read_hw_cursor;
100static vi_set_hw_cursor_t xboxfb_set_hw_cursor;
101static vi_set_hw_cursor_shape_t xboxfb_set_hw_cursor_shape;
102static vi_blank_display_t xboxfb_blank_display;
103static vi_mmap_t xboxfb_mmap;
104static vi_ioctl_t xboxfb_ioctl;
105static vi_clear_t xboxfb_clear;
106static vi_fill_rect_t xboxfb_fill_rect;
107static vi_bitblt_t xboxfb_bitblt;
108static vi_diag_t xboxfb_diag;
109static vi_save_cursor_palette_t xboxfb_save_cursor_palette;
110static vi_load_cursor_palette_t xboxfb_load_cursor_palette;
111static vi_copy_t xboxfb_copy;
112static vi_putp_t xboxfb_putp;
113static vi_putc_t xboxfb_putc;
114static vi_puts_t xboxfb_puts;
115static vi_putm_t xboxfb_putm;
116
117static video_switch_t xboxvidsw = {
118	.probe                = xboxfb_probe,
119	.init                 = xboxfb_init,
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,
136	.mmap                 = xboxfb_mmap,
137	.ioctl                = xboxfb_ioctl,
138	.clear                = xboxfb_clear,
139	.fill_rect            = xboxfb_fill_rect,
140	.bitblt               = xboxfb_bitblt,
141	NULL,
142	NULL,
143	.diag                 = xboxfb_diag,
144	.save_cursor_palette  = xboxfb_save_cursor_palette,
145	.load_cursor_palette  = xboxfb_load_cursor_palette,
146	.copy                 = xboxfb_copy,
147	.putp                 = xboxfb_putp,
148	.putc                 = xboxfb_putc,
149	.puts                 = xboxfb_puts,
150	.putm                 = xboxfb_putm
151};
152
153static int xboxfb_configure(int flags);
154VIDEO_DRIVER(xboxsc, xboxvidsw, xboxfb_configure);
155
156static vr_init_t xbr_init;
157static vr_clear_t xbr_clear;
158static vr_draw_border_t xbr_draw_border;
159static vr_draw_t xbr_draw;
160static vr_set_cursor_t xbr_set_cursor;
161static vr_draw_cursor_t xbr_draw_cursor;
162static vr_blink_cursor_t xbr_blink_cursor;
163static vr_set_mouse_t xbr_set_mouse;
164static vr_draw_mouse_t xbr_draw_mouse;
165
166/*
167 * We use our own renderer; this is because we must emulate a hardware
168 * cursor.
169 */
170static sc_rndr_sw_t xboxrend = {
171	xbr_init,
172	xbr_clear,
173	xbr_draw_border,
174	xbr_draw,
175	xbr_set_cursor,
176	xbr_draw_cursor,
177	xbr_blink_cursor,
178	xbr_set_mouse,
179	xbr_draw_mouse
180};
181RENDERER(xboxsc, 0, xboxrend, gfb_set);
182
183static struct xboxfb_softc xboxfb_sc;
184
185/* color mappings, from dev/fb/creator.c */
186static 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 */
203};
204
205/* mouse pointer from dev/syscons/scgfbrndr.c */
206static u_char mouse_pointer[16] = {
207        0x00, 0x40, 0x60, 0x70, 0x78, 0x7c, 0x7e, 0x68,
208        0x0c, 0x0c, 0x06, 0x06, 0x00, 0x00, 0x00, 0x00
209};
210
211static int
212xboxfb_init(int unit, video_adapter_t* adp, int flags)
213{
214	struct xboxfb_softc* sc = &xboxfb_sc;
215	video_info_t* vi;
216	int i;
217	int* iptr;
218
219	vi = &adp->va_info;
220
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)) {
226		/*
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
231		 * care.
232		 *
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 :-(
238		 */
239		for (i = 0; i < (XBOX_FB_SIZE / PAGE_SIZE); i++) {
240			pmap_kenter (((i + 1) * PAGE_SIZE), XBOX_FB_START + (i * PAGE_SIZE));
241		}
242		pmap_kenter ((i + 1) * PAGE_SIZE, XBOX_FB_START_PTR - XBOX_FB_START_PTR % PAGE_SIZE);
243		sc->sc_framebuffer = (char*)PAGE_SIZE;
244
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;
247
248		/* clear the screen */
249		iptr = (uint32_t*)sc->sc_framebuffer;
250		for (i = 0; i < sc->sc_height * sc->sc_width; i++)
251			*iptr++ = cmap[0];
252
253		/* don't ever do this again! */
254		adp->va_flags |= V_ADP_INITIALIZED;
255	}
256
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;
264
265	adp->va_flags |= V_ADP_COLOR;
266
267	if (vid_register(adp) < 0)
268		return (ENXIO);
269
270	adp->va_flags |= V_ADP_REGISTERED;
271
272	return 0;
273}
274
275static int
276xboxfb_probe(int unit, video_adapter_t** adp, void* arg, int flags)
277{
278	return 0;
279}
280
281static int
282xboxfb_configure(int flags)
283{
284	struct xboxfb_softc* sc = &xboxfb_sc;
285
286	/* Don't init the framebuffer on non-XBOX-es */
287	if (!arch_i386_is_xbox)
288		return 0;
289
290	/*
291	 * If we do only a probe, we are in such an early boot stadium
292	 * that we cannot yet do a 'clean' initialization.
293	 */
294	if (flags & VIO_PROBE_ONLY) {
295		xboxfb_init(0, &sc->sc_va, 0);
296		return 1;
297	}
298
299	/* Do a clean mapping of the framebuffer memory */
300	sc->sc_framebuffer = pmap_mapdev (XBOX_FB_START, XBOX_FB_SIZE);
301	return 1;
302}
303
304static void
305sc_identify(driver_t* driver, device_t parent)
306{
307	BUS_ADD_CHILD(parent, INT_MAX, SC_DRIVER_NAME, 0);
308}
309
310static int
311sc_probe(device_t dev)
312{
313	device_set_desc(dev, "XBox System console");
314	return (sc_probe_unit(device_get_unit(dev), device_get_flags(dev) | SC_AUTODETECT_KBD));
315}
316
317static int sc_attach(device_t dev)
318{
319	return (sc_attach_unit(device_get_unit(dev), device_get_flags(dev) | SC_AUTODETECT_KBD));
320}
321
322static 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),
327	{ 0, 0 }
328};
329
330static driver_t xboxfb_sc_driver = {
331	SC_DRIVER_NAME,
332	sc_methods,
333	sizeof(sc_softc_t)
334};
335
336static devclass_t sc_devclass;
337
338DRIVER_MODULE(sc, legacy, xboxfb_sc_driver, sc_devclass, 0, 0);
339
340static void
341xbr_init(scr_stat* scp)
342{
343}
344
345static void
346xbr_clear(scr_stat* scp, int c, int attr)
347{
348}
349
350static void
351xbr_draw_border(scr_stat* scp, int color)
352{
353}
354
355static void
356xbr_draw(scr_stat* scp, int from, int count, int flip)
357{
358	video_adapter_t* adp = scp->sc->adp;
359	int i, c, a;
360
361	if (!flip) {
362		/* Normal printing */
363		vidd_puts(adp, from, (uint16_t*)sc_vtb_pointer(&scp->vtb, from), count);
364	} else {
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));
370		}
371	}
372}
373
374static void
375xbr_set_cursor(scr_stat* scp, int base, int height, int blink)
376{
377}
378
379static void
380xbr_draw_cursor(scr_stat* scp, int at, int blink, int on, int flip)
381{
382	struct xboxfb_softc* sc = &xboxfb_sc;
383	video_adapter_t* adp = scp->sc->adp;
384	uint32_t* ptri = (uint32_t*)sc->sc_framebuffer;
385	int row, col, i, j;
386
387	if (scp->curs_attr.height <= 0)
388		return;
389
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;
394
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;
399		}
400		ptri += (sc->sc_width - adp->va_info.vi_cwidth);
401	}
402}
403
404static void
405xbr_blink_cursor(scr_stat* scp, int at, int flip)
406{
407}
408
409static void
410xbr_set_mouse(scr_stat* scp)
411{
412}
413
414static void
415xbr_draw_mouse(scr_stat* scp, int x, int y, int on)
416{
417	vidd_putm(scp->sc->adp, x, y, mouse_pointer, 0xffffffff, 16, 8);
418
419}
420
421static int
422xboxfb_get_info(video_adapter_t *adp, int mode, video_info_t *info)
423{
424	bcopy(&adp->va_info, info, sizeof(*info));
425	return (0);
426}
427
428static int
429xboxfb_query_mode(video_adapter_t *adp, video_info_t *info)
430{
431	return (ENODEV);
432}
433
434static int
435xboxfb_set_mode(video_adapter_t *adp, int mode)
436{
437	return (0);
438}
439
440static int
441xboxfb_save_font(video_adapter_t *adp, int page, int size, int width,
442    u_char *data, int c, int count)
443{
444	return (ENODEV);
445}
446
447static int
448xboxfb_load_font(video_adapter_t *adp, int page, int size, int width,
449    u_char *data, int c, int count)
450{
451	return (ENODEV);
452}
453
454static int
455xboxfb_show_font(video_adapter_t *adp, int page)
456{
457	return (ENODEV);
458}
459
460static int
461xboxfb_save_palette(video_adapter_t *adp, u_char *palette)
462{
463	return (ENODEV);
464}
465
466static int
467xboxfb_load_palette(video_adapter_t *adp, u_char *palette)
468{
469	return (ENODEV);
470}
471
472static int
473xboxfb_set_border(video_adapter_t *adp, int border)
474{
475	return (0);
476}
477
478static int
479xboxfb_save_state(video_adapter_t *adp, void *p, size_t size)
480{
481	return (ENODEV);
482}
483
484static int
485xboxfb_load_state(video_adapter_t *adp, void *p)
486{
487	return (ENODEV);
488}
489
490static int
491xboxfb_set_win_org(video_adapter_t *adp, off_t offset)
492{
493	return (ENODEV);
494}
495
496static int
497xboxfb_read_hw_cursor(video_adapter_t *adp, int *col, int *row)
498{
499	*col = 0;
500	*row = 0;
501	return (0);
502}
503
504static int
505xboxfb_set_hw_cursor(video_adapter_t *adp, int col, int row)
506{
507	return (ENODEV);
508}
509
510static int
511xboxfb_set_hw_cursor_shape(video_adapter_t *adp, int base, int height,
512    int celsize, int blink)
513{
514	return (ENODEV);
515}
516
517static int
518xboxfb_blank_display(video_adapter_t *adp, int mode)
519{
520	return (0);
521}
522
523static int
524xboxfb_mmap(video_adapter_t *adp, vm_ooffset_t offset, vm_paddr_t *paddr,
525    int prot, vm_memattr_t *memattr)
526{
527	return (EINVAL);
528}
529
530static int
531xboxfb_ioctl(video_adapter_t *adp, u_long cmd, caddr_t data)
532{
533	return (fb_commonioctl(adp, cmd, data));
534}
535
536static int
537xboxfb_clear(video_adapter_t *adp)
538{
539	return (0);
540}
541
542static int
543xboxfb_fill_rect(video_adapter_t *adp, int val, int x, int y, int cx, int cy)
544{
545	return (0);
546}
547
548static int
549xboxfb_bitblt(video_adapter_t *adp, ...)
550{
551	return (ENODEV);
552}
553
554static int
555xboxfb_diag(video_adapter_t *adp, int level)
556{
557	video_info_t info;
558
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);
562	return (0);
563}
564
565static int
566xboxfb_save_cursor_palette(video_adapter_t *adp, u_char *palette)
567{
568	return (ENODEV);
569}
570
571static int
572xboxfb_load_cursor_palette(video_adapter_t *adp, u_char *palette)
573{
574	return (ENODEV);
575}
576
577static int
578xboxfb_copy(video_adapter_t *adp, vm_offset_t src, vm_offset_t dst, int n)
579{
580	return (ENODEV);
581}
582
583static int
584xboxfb_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)
586{
587	return (ENODEV);
588}
589
590static int
591xboxfb_putc(video_adapter_t *adp, vm_offset_t off, u_int8_t c, u_int8_t a)
592{
593	int row, col;
594	int i, j;
595	struct xboxfb_softc* sc = &xboxfb_sc;
596	uint32_t* ptri = (uint32_t*)sc->sc_framebuffer;
597	const uint8_t* fontdata;
598	uint32_t clr;
599	uint8_t mask;
600
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;
606
607	/* Place the character on the screen, pixel by pixel */
608	for (j = 0; j < adp->va_info.vi_cheight; j++) {
609		mask = 0x80;
610		for (i = 0; i < adp->va_info.vi_cwidth; i++) {
611			clr = (*fontdata & mask) ? cmap[a & 0xf] : cmap[(a >> 4) & 0xf];
612			*ptri++ = clr;
613			mask >>= 1;
614		}
615		ptri += (sc->sc_width - adp->va_info.vi_cwidth);
616		fontdata++;
617	}
618	return (0);
619}
620
621static int
622xboxfb_puts(video_adapter_t *adp, vm_offset_t off, u_int16_t *s, int len)
623{
624	int i;
625
626	for (i = 0; i < len; i++) {
627		vidd_putc(adp, off + i, s[i] & 0xff, (s[i] & 0xff00) >> 8);
628	}
629	return (0);
630}
631
632static int
633xboxfb_putm(video_adapter_t *adp, int x, int y, u_int8_t *pixel_image,
634    u_int32_t pixel_mask, int size, int width)
635{
636	struct xboxfb_softc* sc = &xboxfb_sc;
637	uint32_t* ptri = (uint32_t*)sc->sc_framebuffer;
638	int i, j;
639
640	if (x < 0 || y < 0 || x + width > sc->sc_width || y + (2 * size) > sc->sc_height)
641		return 0;
642
643	ptri += (y * sc->sc_width) + x;
644
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))
649				*ptri = cmap[0xf];
650			ptri++;
651		}
652		ptri += (sc->sc_width - width);
653	}
654	return (0);
655}
656