ofw_syscons.c revision 256281
1/*-
2 * Copyright (c) 2003 Peter Grehan
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 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD: stable/10/sys/powerpc/ofw/ofw_syscons.c 255904 2013-09-26 22:47:02Z nwhitehorn $");
29
30#include <sys/param.h>
31#include <sys/systm.h>
32#include <sys/module.h>
33#include <sys/bus.h>
34#include <sys/kernel.h>
35#include <sys/sysctl.h>
36#include <sys/limits.h>
37#include <sys/conf.h>
38#include <sys/cons.h>
39#include <sys/proc.h>
40#include <sys/fcntl.h>
41#include <sys/malloc.h>
42#include <sys/fbio.h>
43#include <sys/consio.h>
44
45#include <machine/bus.h>
46#include <machine/sc_machdep.h>
47#include <machine/vm.h>
48
49#include <sys/rman.h>
50
51#include <dev/fb/fbreg.h>
52#include <dev/syscons/syscons.h>
53
54#include <dev/ofw/openfirm.h>
55#include <dev/ofw/ofw_bus.h>
56#include <dev/ofw/ofw_pci.h>
57#include <powerpc/ofw/ofw_syscons.h>
58
59static int ofwfb_ignore_mmap_checks = 1;
60static SYSCTL_NODE(_hw, OID_AUTO, ofwfb, CTLFLAG_RD, 0, "ofwfb");
61SYSCTL_INT(_hw_ofwfb, OID_AUTO, relax_mmap, CTLFLAG_RW,
62    &ofwfb_ignore_mmap_checks, 0, "relaxed mmap bounds checking");
63
64extern u_char dflt_font_16[];
65extern u_char dflt_font_14[];
66extern u_char dflt_font_8[];
67
68static int ofwfb_configure(int flags);
69
70static vi_probe_t ofwfb_probe;
71static vi_init_t ofwfb_init;
72static vi_get_info_t ofwfb_get_info;
73static vi_query_mode_t ofwfb_query_mode;
74static vi_set_mode_t ofwfb_set_mode;
75static vi_save_font_t ofwfb_save_font;
76static vi_load_font_t ofwfb_load_font;
77static vi_show_font_t ofwfb_show_font;
78static vi_save_palette_t ofwfb_save_palette;
79static vi_load_palette_t ofwfb_load_palette;
80static vi_set_border_t ofwfb_set_border;
81static vi_save_state_t ofwfb_save_state;
82static vi_load_state_t ofwfb_load_state;
83static vi_set_win_org_t ofwfb_set_win_org;
84static vi_read_hw_cursor_t ofwfb_read_hw_cursor;
85static vi_set_hw_cursor_t ofwfb_set_hw_cursor;
86static vi_set_hw_cursor_shape_t ofwfb_set_hw_cursor_shape;
87static vi_blank_display_t ofwfb_blank_display;
88static vi_mmap_t ofwfb_mmap;
89static vi_ioctl_t ofwfb_ioctl;
90static vi_clear_t ofwfb_clear;
91static vi_fill_rect_t ofwfb_fill_rect;
92static vi_bitblt_t ofwfb_bitblt;
93static vi_diag_t ofwfb_diag;
94static vi_save_cursor_palette_t ofwfb_save_cursor_palette;
95static vi_load_cursor_palette_t ofwfb_load_cursor_palette;
96static vi_copy_t ofwfb_copy;
97static vi_putp_t ofwfb_putp;
98static vi_putc_t ofwfb_putc;
99static vi_puts_t ofwfb_puts;
100static vi_putm_t ofwfb_putm;
101
102static video_switch_t ofwfbvidsw = {
103	.probe			= ofwfb_probe,
104	.init			= ofwfb_init,
105	.get_info		= ofwfb_get_info,
106	.query_mode		= ofwfb_query_mode,
107	.set_mode		= ofwfb_set_mode,
108	.save_font		= ofwfb_save_font,
109	.load_font		= ofwfb_load_font,
110	.show_font		= ofwfb_show_font,
111	.save_palette		= ofwfb_save_palette,
112	.load_palette		= ofwfb_load_palette,
113	.set_border		= ofwfb_set_border,
114	.save_state		= ofwfb_save_state,
115	.load_state		= ofwfb_load_state,
116	.set_win_org		= ofwfb_set_win_org,
117	.read_hw_cursor		= ofwfb_read_hw_cursor,
118	.set_hw_cursor		= ofwfb_set_hw_cursor,
119	.set_hw_cursor_shape	= ofwfb_set_hw_cursor_shape,
120	.blank_display		= ofwfb_blank_display,
121	.mmap			= ofwfb_mmap,
122	.ioctl			= ofwfb_ioctl,
123	.clear			= ofwfb_clear,
124	.fill_rect		= ofwfb_fill_rect,
125	.bitblt			= ofwfb_bitblt,
126	.diag			= ofwfb_diag,
127	.save_cursor_palette	= ofwfb_save_cursor_palette,
128	.load_cursor_palette	= ofwfb_load_cursor_palette,
129	.copy			= ofwfb_copy,
130	.putp			= ofwfb_putp,
131	.putc			= ofwfb_putc,
132	.puts			= ofwfb_puts,
133	.putm			= ofwfb_putm,
134};
135
136/*
137 * bitmap depth-specific routines
138 */
139static vi_blank_display_t ofwfb_blank_display8;
140static vi_putc_t ofwfb_putc8;
141static vi_putm_t ofwfb_putm8;
142static vi_set_border_t ofwfb_set_border8;
143
144static vi_blank_display_t ofwfb_blank_display32;
145static vi_putc_t ofwfb_putc32;
146static vi_putm_t ofwfb_putm32;
147static vi_set_border_t ofwfb_set_border32;
148
149VIDEO_DRIVER(ofwfb, ofwfbvidsw, ofwfb_configure);
150
151extern sc_rndr_sw_t txtrndrsw;
152RENDERER(ofwfb, 0, txtrndrsw, gfb_set);
153
154RENDERER_MODULE(ofwfb, gfb_set);
155
156/*
157 * Define the iso6429-1983 colormap
158 */
159static struct {
160	uint8_t	red;
161	uint8_t	green;
162	uint8_t	blue;
163} ofwfb_cmap[16] = {		/*  #     R    G    B   Color */
164				/*  -     -    -    -   ----- */
165	{ 0x00, 0x00, 0x00 },	/*  0     0    0    0   Black */
166	{ 0x00, 0x00, 0xaa },	/*  1     0    0  2/3   Blue  */
167	{ 0x00, 0xaa, 0x00 },	/*  2     0  2/3    0   Green */
168	{ 0x00, 0xaa, 0xaa },	/*  3     0  2/3  2/3   Cyan  */
169	{ 0xaa, 0x00, 0x00 },	/*  4   2/3    0    0   Red   */
170	{ 0xaa, 0x00, 0xaa },	/*  5   2/3    0  2/3   Magenta */
171	{ 0xaa, 0x55, 0x00 },	/*  6   2/3  1/3    0   Brown */
172	{ 0xaa, 0xaa, 0xaa },	/*  7   2/3  2/3  2/3   White */
173        { 0x55, 0x55, 0x55 },	/*  8   1/3  1/3  1/3   Gray  */
174	{ 0x55, 0x55, 0xff },	/*  9   1/3  1/3    1   Bright Blue  */
175	{ 0x55, 0xff, 0x55 },	/* 10   1/3    1  1/3   Bright Green */
176	{ 0x55, 0xff, 0xff },	/* 11   1/3    1    1   Bright Cyan  */
177	{ 0xff, 0x55, 0x55 },	/* 12     1  1/3  1/3   Bright Red   */
178	{ 0xff, 0x55, 0xff },	/* 13     1  1/3    1   Bright Magenta */
179	{ 0xff, 0xff, 0x80 },	/* 14     1    1  1/3   Bright Yellow */
180	{ 0xff, 0xff, 0xff }	/* 15     1    1    1   Bright White */
181};
182
183#define	TODO	printf("%s: unimplemented\n", __func__)
184
185static u_int16_t ofwfb_static_window[ROW*COL];
186
187static struct ofwfb_softc ofwfb_softc;
188
189static __inline int
190ofwfb_background(uint8_t attr)
191{
192	return (attr >> 4);
193}
194
195static __inline int
196ofwfb_foreground(uint8_t attr)
197{
198	return (attr & 0x0f);
199}
200
201static u_int
202ofwfb_pix32(struct ofwfb_softc *sc, int attr)
203{
204	u_int retval;
205
206	if (sc->sc_tag == &bs_le_tag)
207		retval = (ofwfb_cmap[attr].red << 16) |
208			(ofwfb_cmap[attr].green << 8) |
209			ofwfb_cmap[attr].blue;
210	else
211		retval = (ofwfb_cmap[attr].blue  << 16) |
212			(ofwfb_cmap[attr].green << 8) |
213			ofwfb_cmap[attr].red;
214
215	return (retval);
216}
217
218static int
219ofwfb_configure(int flags)
220{
221	struct ofwfb_softc *sc;
222        phandle_t chosen;
223        ihandle_t stdout;
224	phandle_t node;
225	uint32_t fb_phys;
226	int depth;
227	int disable;
228	int len;
229	int i;
230	char type[16];
231	static int done = 0;
232
233	disable = 0;
234	TUNABLE_INT_FETCH("hw.syscons.disable", &disable);
235	if (disable != 0)
236		return (0);
237
238	if (done != 0)
239		return (0);
240	done = 1;
241
242	sc = &ofwfb_softc;
243
244	chosen = OF_finddevice("/chosen");
245	OF_getprop(chosen, "stdout", &stdout, sizeof(stdout));
246        node = OF_instance_to_package(stdout);
247	if (node == -1) {
248		/*
249		 * The "/chosen/stdout" does not exist try
250		 * using "screen" directly.
251		 */
252		node = OF_finddevice("screen");
253	}
254	OF_getprop(node, "device_type", type, sizeof(type));
255	if (strcmp(type, "display") != 0)
256		return (0);
257
258	/* Only support 8 and 32-bit framebuffers */
259	OF_getprop(node, "depth", &depth, sizeof(depth));
260	if (depth == 8) {
261		sc->sc_blank = ofwfb_blank_display8;
262		sc->sc_putc = ofwfb_putc8;
263		sc->sc_putm = ofwfb_putm8;
264		sc->sc_set_border = ofwfb_set_border8;
265	} else if (depth == 32) {
266		sc->sc_blank = ofwfb_blank_display32;
267		sc->sc_putc = ofwfb_putc32;
268		sc->sc_putm = ofwfb_putm32;
269		sc->sc_set_border = ofwfb_set_border32;
270	} else
271		return (0);
272
273	if (OF_getproplen(node, "height") != sizeof(sc->sc_height) ||
274	    OF_getproplen(node, "width") != sizeof(sc->sc_width) ||
275	    OF_getproplen(node, "linebytes") != sizeof(sc->sc_stride))
276		return (0);
277
278	sc->sc_depth = depth;
279	sc->sc_node = node;
280	sc->sc_console = 1;
281	OF_getprop(node, "height", &sc->sc_height, sizeof(sc->sc_height));
282	OF_getprop(node, "width", &sc->sc_width, sizeof(sc->sc_width));
283	OF_getprop(node, "linebytes", &sc->sc_stride, sizeof(sc->sc_stride));
284
285	/*
286	 * Get the PCI addresses of the adapter. The node may be the
287	 * child of the PCI device: in that case, try the parent for
288	 * the assigned-addresses property.
289	 */
290	len = OF_getprop(node, "assigned-addresses", sc->sc_pciaddrs,
291	          sizeof(sc->sc_pciaddrs));
292	if (len == -1) {
293		len = OF_getprop(OF_parent(node), "assigned-addresses",
294		    sc->sc_pciaddrs, sizeof(sc->sc_pciaddrs));
295	}
296	if (len == -1)
297		len = 0;
298	sc->sc_num_pciaddrs = len / sizeof(struct ofw_pci_register);
299
300	/*
301	 * Grab the physical address of the framebuffer, and then map it
302	 * into our memory space. If the MMU is not yet up, it will be
303	 * remapped for us when relocation turns on.
304	 *
305	 * XXX We assume #address-cells is 1 at this point.
306	 */
307	if (OF_getproplen(node, "address") == sizeof(fb_phys)) {
308		OF_getprop(node, "address", &fb_phys, sizeof(fb_phys));
309		sc->sc_tag = &bs_be_tag;
310		bus_space_map(sc->sc_tag, fb_phys, sc->sc_height *
311		    sc->sc_stride, BUS_SPACE_MAP_PREFETCHABLE, &sc->sc_addr);
312	} else {
313		/*
314		 * Some IBM systems don't have an address property. Try to
315		 * guess the framebuffer region from the assigned addresses.
316		 * This is ugly, but there doesn't seem to be an alternative.
317		 * Linux does the same thing.
318		 */
319
320		fb_phys = sc->sc_num_pciaddrs;
321		for (i = 0; i < sc->sc_num_pciaddrs; i++) {
322			/* If it is too small, not the framebuffer */
323			if (sc->sc_pciaddrs[i].size_lo <
324			    sc->sc_stride*sc->sc_height)
325				continue;
326			/* If it is not memory, it isn't either */
327			if (!(sc->sc_pciaddrs[i].phys_hi &
328			    OFW_PCI_PHYS_HI_SPACE_MEM32))
329				continue;
330
331			/* This could be the framebuffer */
332			fb_phys = i;
333
334			/* If it is prefetchable, it certainly is */
335			if (sc->sc_pciaddrs[i].phys_hi &
336			    OFW_PCI_PHYS_HI_PREFETCHABLE)
337				break;
338		}
339		if (fb_phys == sc->sc_num_pciaddrs)
340			return (0);
341
342		OF_decode_addr(node, fb_phys, &sc->sc_tag, &sc->sc_addr);
343	}
344
345	ofwfb_init(0, &sc->sc_va, 0);
346
347	return (0);
348}
349
350static int
351ofwfb_probe(int unit, video_adapter_t **adp, void *arg, int flags)
352{
353	TODO;
354	return (0);
355}
356
357static int
358ofwfb_init(int unit, video_adapter_t *adp, int flags)
359{
360	struct ofwfb_softc *sc;
361	video_info_t *vi;
362	int cborder;
363	int font_height;
364
365	sc = (struct ofwfb_softc *)adp;
366	vi = &adp->va_info;
367
368	vid_init_struct(adp, "ofwfb", -1, unit);
369
370	/* The default font size can be overridden by loader */
371	font_height = 16;
372	TUNABLE_INT_FETCH("hw.syscons.fsize", &font_height);
373	if (font_height == 8) {
374		sc->sc_font = dflt_font_8;
375		sc->sc_font_height = 8;
376	} else if (font_height == 14) {
377		sc->sc_font = dflt_font_14;
378		sc->sc_font_height = 14;
379	} else {
380		/* default is 8x16 */
381		sc->sc_font = dflt_font_16;
382		sc->sc_font_height = 16;
383	}
384
385	/* The user can set a border in chars - default is 1 char width */
386	cborder = 1;
387	TUNABLE_INT_FETCH("hw.syscons.border", &cborder);
388
389	vi->vi_cheight = sc->sc_font_height;
390	vi->vi_width = sc->sc_width/8 - 2*cborder;
391	vi->vi_height = sc->sc_height/sc->sc_font_height - 2*cborder;
392	vi->vi_cwidth = 8;
393
394	/*
395	 * Clamp width/height to syscons maximums
396	 */
397	if (vi->vi_width > COL)
398		vi->vi_width = COL;
399	if (vi->vi_height > ROW)
400		vi->vi_height = ROW;
401
402	sc->sc_xmargin = (sc->sc_width - (vi->vi_width * vi->vi_cwidth)) / 2;
403	sc->sc_ymargin = (sc->sc_height - (vi->vi_height * vi->vi_cheight))/2;
404
405	/*
406	 * Avoid huge amounts of conditional code in syscons by
407	 * defining a dummy h/w text display buffer.
408	 */
409	adp->va_window = (vm_offset_t) ofwfb_static_window;
410
411	/*
412	 * Enable future font-loading and flag color support, as well as
413	 * adding V_ADP_MODECHANGE so that we ofwfb_set_mode() gets called
414	 * when the X server shuts down. This enables us to get the console
415	 * back when X disappears.
416	 */
417	adp->va_flags |= V_ADP_FONT | V_ADP_COLOR | V_ADP_MODECHANGE;
418
419	ofwfb_set_mode(&sc->sc_va, 0);
420
421	vid_register(&sc->sc_va);
422
423	return (0);
424}
425
426static int
427ofwfb_get_info(video_adapter_t *adp, int mode, video_info_t *info)
428{
429	bcopy(&adp->va_info, info, sizeof(*info));
430	return (0);
431}
432
433static int
434ofwfb_query_mode(video_adapter_t *adp, video_info_t *info)
435{
436	TODO;
437	return (0);
438}
439
440static int
441ofwfb_set_mode(video_adapter_t *adp, int mode)
442{
443	struct ofwfb_softc *sc;
444	char name[64];
445	ihandle_t ih;
446	int i, retval;
447
448	sc = (struct ofwfb_softc *)adp;
449
450	/*
451	 * Open the display device, which will initialize it.
452	 */
453
454	memset(name, 0, sizeof(name));
455	OF_package_to_path(sc->sc_node, name, sizeof(name));
456	ih = OF_open(name);
457
458	if (sc->sc_depth == 8) {
459		/*
460		 * Install the ISO6429 colormap - older OFW systems
461		 * don't do this by default
462		 */
463		for (i = 0; i < 16; i++) {
464			OF_call_method("color!", ih, 4, 1,
465				       ofwfb_cmap[i].red,
466				       ofwfb_cmap[i].green,
467				       ofwfb_cmap[i].blue,
468				       i,
469				       &retval);
470		}
471	}
472
473	ofwfb_blank_display(&sc->sc_va, V_DISPLAY_ON);
474
475	return (0);
476}
477
478static int
479ofwfb_save_font(video_adapter_t *adp, int page, int size, int width,
480    u_char *data, int c, int count)
481{
482	TODO;
483	return (0);
484}
485
486static int
487ofwfb_load_font(video_adapter_t *adp, int page, int size, int width,
488    u_char *data, int c, int count)
489{
490	struct ofwfb_softc *sc;
491
492	sc = (struct ofwfb_softc *)adp;
493
494	/*
495	 * syscons code has already determined that current width/height
496	 * are unchanged for this new font
497	 */
498	sc->sc_font = data;
499	return (0);
500}
501
502static int
503ofwfb_show_font(video_adapter_t *adp, int page)
504{
505
506	return (0);
507}
508
509static int
510ofwfb_save_palette(video_adapter_t *adp, u_char *palette)
511{
512	/* TODO; */
513	return (0);
514}
515
516static int
517ofwfb_load_palette(video_adapter_t *adp, u_char *palette)
518{
519	/* TODO; */
520	return (0);
521}
522
523static int
524ofwfb_set_border8(video_adapter_t *adp, int border)
525{
526	struct ofwfb_softc *sc;
527	int i, j;
528	uint8_t *addr;
529	uint8_t bground;
530
531	sc = (struct ofwfb_softc *)adp;
532
533	bground = ofwfb_background(border);
534
535	/* Set top margin */
536	addr = (uint8_t *) sc->sc_addr;
537	for (i = 0; i < sc->sc_ymargin; i++) {
538		for (j = 0; j < sc->sc_width; j++) {
539			*(addr + j) = bground;
540		}
541		addr += sc->sc_stride;
542	}
543
544	/* bottom margin */
545	addr = (uint8_t *) sc->sc_addr + (sc->sc_height - sc->sc_ymargin)*sc->sc_stride;
546	for (i = 0; i < sc->sc_ymargin; i++) {
547		for (j = 0; j < sc->sc_width; j++) {
548			*(addr + j) = bground;
549		}
550		addr += sc->sc_stride;
551	}
552
553	/* remaining left and right borders */
554	addr = (uint8_t *) sc->sc_addr + sc->sc_ymargin*sc->sc_stride;
555	for (i = 0; i < sc->sc_height - 2*sc->sc_xmargin; i++) {
556		for (j = 0; j < sc->sc_xmargin; j++) {
557			*(addr + j) = bground;
558			*(addr + j + sc->sc_width - sc->sc_xmargin) = bground;
559		}
560		addr += sc->sc_stride;
561	}
562
563	return (0);
564}
565
566static int
567ofwfb_set_border32(video_adapter_t *adp, int border)
568{
569	/* XXX Be lazy for now and blank entire screen */
570	return (ofwfb_blank_display32(adp, border));
571}
572
573static int
574ofwfb_set_border(video_adapter_t *adp, int border)
575{
576	struct ofwfb_softc *sc;
577
578	sc = (struct ofwfb_softc *)adp;
579
580	return ((*sc->sc_set_border)(adp, border));
581}
582
583static int
584ofwfb_save_state(video_adapter_t *adp, void *p, size_t size)
585{
586	TODO;
587	return (0);
588}
589
590static int
591ofwfb_load_state(video_adapter_t *adp, void *p)
592{
593	TODO;
594	return (0);
595}
596
597static int
598ofwfb_set_win_org(video_adapter_t *adp, off_t offset)
599{
600	TODO;
601	return (0);
602}
603
604static int
605ofwfb_read_hw_cursor(video_adapter_t *adp, int *col, int *row)
606{
607	*col = 0;
608	*row = 0;
609
610	return (0);
611}
612
613static int
614ofwfb_set_hw_cursor(video_adapter_t *adp, int col, int row)
615{
616
617	return (0);
618}
619
620static int
621ofwfb_set_hw_cursor_shape(video_adapter_t *adp, int base, int height,
622    int celsize, int blink)
623{
624	return (0);
625}
626
627static int
628ofwfb_blank_display8(video_adapter_t *adp, int mode)
629{
630	struct ofwfb_softc *sc;
631	int i;
632	uint32_t *addr;
633	uint32_t color;
634	uint32_t end;
635
636	sc = (struct ofwfb_softc *)adp;
637	addr = (uint32_t *) sc->sc_addr;
638	end = (sc->sc_stride/4) * sc->sc_height;
639
640	/* Splat 4 pixels at once. */
641	color = (ofwfb_background(SC_NORM_ATTR) << 24) |
642	    (ofwfb_background(SC_NORM_ATTR) << 16) |
643	    (ofwfb_background(SC_NORM_ATTR) << 8) |
644	    (ofwfb_background(SC_NORM_ATTR));
645
646	for (i = 0; i < end; i++)
647		*(addr + i) = color;
648
649	return (0);
650}
651
652static int
653ofwfb_blank_display32(video_adapter_t *adp, int mode)
654{
655	struct ofwfb_softc *sc;
656	int i;
657	uint32_t *addr, blank;
658
659	sc = (struct ofwfb_softc *)adp;
660	addr = (uint32_t *) sc->sc_addr;
661	blank = ofwfb_pix32(sc, ofwfb_background(SC_NORM_ATTR));
662
663	for (i = 0; i < (sc->sc_stride/4)*sc->sc_height; i++)
664		*(addr + i) = blank;
665
666	return (0);
667}
668
669static int
670ofwfb_blank_display(video_adapter_t *adp, int mode)
671{
672	struct ofwfb_softc *sc;
673
674	sc = (struct ofwfb_softc *)adp;
675
676	return ((*sc->sc_blank)(adp, mode));
677}
678
679static int
680ofwfb_mmap(video_adapter_t *adp, vm_ooffset_t offset, vm_paddr_t *paddr,
681    int prot, vm_memattr_t *memattr)
682{
683	struct ofwfb_softc *sc;
684	int i;
685
686	sc = (struct ofwfb_softc *)adp;
687
688	/*
689	 * Make sure the requested address lies within the PCI device's
690	 * assigned addrs
691	 */
692	for (i = 0; i < sc->sc_num_pciaddrs; i++)
693	  if (offset >= sc->sc_pciaddrs[i].phys_lo &&
694	    offset < (sc->sc_pciaddrs[i].phys_lo + sc->sc_pciaddrs[i].size_lo))
695		{
696			/*
697			 * If this is a prefetchable BAR, we can (and should)
698			 * enable write-combining.
699			 */
700			if (sc->sc_pciaddrs[i].phys_hi &
701			    OFW_PCI_PHYS_HI_PREFETCHABLE)
702				*memattr = VM_MEMATTR_WRITE_COMBINING;
703
704			*paddr = offset;
705			return (0);
706		}
707
708	/*
709	 * Hack for Radeon...
710	 */
711	if (ofwfb_ignore_mmap_checks) {
712		*paddr = offset;
713		return (0);
714	}
715
716	/*
717	 * This might be a legacy VGA mem request: if so, just point it at the
718	 * framebuffer, since it shouldn't be touched
719	 */
720	if (offset < sc->sc_stride*sc->sc_height) {
721		*paddr = sc->sc_addr + offset;
722		return (0);
723	}
724
725	/*
726	 * Error if we didn't have a better idea.
727	 */
728	if (sc->sc_num_pciaddrs == 0)
729		return (ENOMEM);
730
731	return (EINVAL);
732}
733
734static int
735ofwfb_ioctl(video_adapter_t *adp, u_long cmd, caddr_t data)
736{
737
738	return (0);
739}
740
741static int
742ofwfb_clear(video_adapter_t *adp)
743{
744	TODO;
745	return (0);
746}
747
748static int
749ofwfb_fill_rect(video_adapter_t *adp, int val, int x, int y, int cx, int cy)
750{
751	TODO;
752	return (0);
753}
754
755static int
756ofwfb_bitblt(video_adapter_t *adp, ...)
757{
758	TODO;
759	return (0);
760}
761
762static int
763ofwfb_diag(video_adapter_t *adp, int level)
764{
765	TODO;
766	return (0);
767}
768
769static int
770ofwfb_save_cursor_palette(video_adapter_t *adp, u_char *palette)
771{
772	TODO;
773	return (0);
774}
775
776static int
777ofwfb_load_cursor_palette(video_adapter_t *adp, u_char *palette)
778{
779	TODO;
780	return (0);
781}
782
783static int
784ofwfb_copy(video_adapter_t *adp, vm_offset_t src, vm_offset_t dst, int n)
785{
786	TODO;
787	return (0);
788}
789
790static int
791ofwfb_putp(video_adapter_t *adp, vm_offset_t off, uint32_t p, uint32_t a,
792    int size, int bpp, int bit_ltor, int byte_ltor)
793{
794	TODO;
795	return (0);
796}
797
798static int
799ofwfb_putc8(video_adapter_t *adp, vm_offset_t off, uint8_t c, uint8_t a)
800{
801	struct ofwfb_softc *sc;
802	int row;
803	int col;
804	int i;
805	uint32_t *addr;
806	u_char *p, fg, bg;
807	union {
808		uint32_t l;
809		uint8_t  c[4];
810	} ch1, ch2;
811
812
813	sc = (struct ofwfb_softc *)adp;
814        row = (off / adp->va_info.vi_width) * adp->va_info.vi_cheight;
815        col = (off % adp->va_info.vi_width) * adp->va_info.vi_cwidth;
816	p = sc->sc_font + c*sc->sc_font_height;
817	addr = (u_int32_t *)((uintptr_t)sc->sc_addr
818		+ (row + sc->sc_ymargin)*sc->sc_stride
819		+ col + sc->sc_xmargin);
820
821	fg = ofwfb_foreground(a);
822	bg = ofwfb_background(a);
823
824	for (i = 0; i < sc->sc_font_height; i++) {
825		u_char fline = p[i];
826
827		/*
828		 * Assume that there is more background than foreground
829		 * in characters and init accordingly
830		 */
831		ch1.l = ch2.l = (bg << 24) | (bg << 16) | (bg << 8) | bg;
832
833		/*
834		 * Calculate 2 x 4-chars at a time, and then
835		 * write these out.
836		 */
837		if (fline & 0x80) ch1.c[0] = fg;
838		if (fline & 0x40) ch1.c[1] = fg;
839		if (fline & 0x20) ch1.c[2] = fg;
840		if (fline & 0x10) ch1.c[3] = fg;
841
842		if (fline & 0x08) ch2.c[0] = fg;
843		if (fline & 0x04) ch2.c[1] = fg;
844		if (fline & 0x02) ch2.c[2] = fg;
845		if (fline & 0x01) ch2.c[3] = fg;
846
847		addr[0] = ch1.l;
848		addr[1] = ch2.l;
849		addr += (sc->sc_stride / sizeof(u_int32_t));
850	}
851
852	return (0);
853}
854
855static int
856ofwfb_putc32(video_adapter_t *adp, vm_offset_t off, uint8_t c, uint8_t a)
857{
858	struct ofwfb_softc *sc;
859	int row;
860	int col;
861	int i, j, k;
862	uint32_t *addr, fg, bg;
863	u_char *p;
864
865	sc = (struct ofwfb_softc *)adp;
866        row = (off / adp->va_info.vi_width) * adp->va_info.vi_cheight;
867        col = (off % adp->va_info.vi_width) * adp->va_info.vi_cwidth;
868	p = sc->sc_font + c*sc->sc_font_height;
869	addr = (uint32_t *)sc->sc_addr
870		+ (row + sc->sc_ymargin)*(sc->sc_stride/4)
871		+ col + sc->sc_xmargin;
872
873	fg = ofwfb_pix32(sc, ofwfb_foreground(a));
874	bg = ofwfb_pix32(sc, ofwfb_background(a));
875
876	for (i = 0; i < sc->sc_font_height; i++) {
877		for (j = 0, k = 7; j < 8; j++, k--) {
878			if ((p[i] & (1 << k)) == 0)
879				*(addr + j) = bg;
880			else
881				*(addr + j) = fg;
882		}
883		addr += (sc->sc_stride/4);
884	}
885
886	return (0);
887}
888
889static int
890ofwfb_putc(video_adapter_t *adp, vm_offset_t off, uint8_t c, uint8_t a)
891{
892	struct ofwfb_softc *sc;
893
894	sc = (struct ofwfb_softc *)adp;
895
896	return ((*sc->sc_putc)(adp, off, c, a));
897}
898
899static int
900ofwfb_puts(video_adapter_t *adp, vm_offset_t off, u_int16_t *s, int len)
901{
902	int i;
903
904	for (i = 0; i < len; i++) {
905		ofwfb_putc(adp, off + i, s[i] & 0xff, (s[i] & 0xff00) >> 8);
906	}
907	return (0);
908}
909
910static int
911ofwfb_putm(video_adapter_t *adp, int x, int y, uint8_t *pixel_image,
912    uint32_t pixel_mask, int size, int width)
913{
914	struct ofwfb_softc *sc;
915
916	sc = (struct ofwfb_softc *)adp;
917
918	return ((*sc->sc_putm)(adp, x, y, pixel_image, pixel_mask, size,
919	    width));
920}
921
922static int
923ofwfb_putm8(video_adapter_t *adp, int x, int y, uint8_t *pixel_image,
924    uint32_t pixel_mask, int size, int width)
925{
926	struct ofwfb_softc *sc;
927	int i, j, k;
928	uint8_t *addr;
929	u_char fg, bg;
930
931	sc = (struct ofwfb_softc *)adp;
932	addr = (u_int8_t *)((uintptr_t)sc->sc_addr
933		+ (y + sc->sc_ymargin)*sc->sc_stride
934		+ x + sc->sc_xmargin);
935
936	fg = ofwfb_foreground(SC_NORM_ATTR);
937	bg = ofwfb_background(SC_NORM_ATTR);
938
939	for (i = 0; i < size && i+y < sc->sc_height - 2*sc->sc_ymargin; i++) {
940		/*
941		 * Calculate 2 x 4-chars at a time, and then
942		 * write these out.
943		 */
944		for (j = 0, k = width; j < 8; j++, k--) {
945			if (x + j >= sc->sc_width - 2*sc->sc_xmargin)
946				continue;
947
948			if (pixel_image[i] & (1 << k))
949				addr[j] = (addr[j] == fg) ? bg : fg;
950		}
951
952		addr += (sc->sc_stride / sizeof(u_int8_t));
953	}
954
955	return (0);
956}
957
958static int
959ofwfb_putm32(video_adapter_t *adp, int x, int y, uint8_t *pixel_image,
960    uint32_t pixel_mask, int size, int width)
961{
962	struct ofwfb_softc *sc;
963	int i, j, k;
964	uint32_t fg, bg;
965	uint32_t *addr;
966
967	sc = (struct ofwfb_softc *)adp;
968	addr = (uint32_t *)sc->sc_addr
969		+ (y + sc->sc_ymargin)*(sc->sc_stride/4)
970		+ x + sc->sc_xmargin;
971
972	fg = ofwfb_pix32(sc, ofwfb_foreground(SC_NORM_ATTR));
973	bg = ofwfb_pix32(sc, ofwfb_background(SC_NORM_ATTR));
974
975	for (i = 0; i < size && i+y < sc->sc_height - 2*sc->sc_ymargin; i++) {
976		for (j = 0, k = width; j < 8; j++, k--) {
977			if (x + j >= sc->sc_width - 2*sc->sc_xmargin)
978				continue;
979
980			if (pixel_image[i] & (1 << k))
981				*(addr + j) = (*(addr + j) == fg) ? bg : fg;
982		}
983		addr += (sc->sc_stride/4);
984	}
985
986	return (0);
987}
988
989/*
990 * Define the syscons nexus device attachment
991 */
992static void
993ofwfb_scidentify(driver_t *driver, device_t parent)
994{
995	device_t child;
996
997	/*
998	 * The Nintendo Wii doesn't have open firmware, so don't probe ofwfb
999	 * because otherwise we will crash.
1000	 */
1001	if (strcmp(installed_platform(), "wii") == 0)
1002		return;
1003	/*
1004	 * Add with a priority guaranteed to make it last on
1005	 * the device list
1006	 */
1007	child = BUS_ADD_CHILD(parent, INT_MAX, SC_DRIVER_NAME, 0);
1008}
1009
1010static int
1011ofwfb_scprobe(device_t dev)
1012{
1013	int error;
1014
1015	device_set_desc(dev, "System console");
1016
1017	error = sc_probe_unit(device_get_unit(dev),
1018	    device_get_flags(dev) | SC_AUTODETECT_KBD);
1019	if (error != 0)
1020		return (error);
1021
1022	/* This is a fake device, so make sure we added it ourselves */
1023	return (BUS_PROBE_NOWILDCARD);
1024}
1025
1026static int
1027ofwfb_scattach(device_t dev)
1028{
1029	return (sc_attach_unit(device_get_unit(dev),
1030	    device_get_flags(dev) | SC_AUTODETECT_KBD));
1031}
1032
1033static device_method_t ofwfb_sc_methods[] = {
1034  	DEVMETHOD(device_identify,	ofwfb_scidentify),
1035	DEVMETHOD(device_probe,		ofwfb_scprobe),
1036	DEVMETHOD(device_attach,	ofwfb_scattach),
1037	{ 0, 0 }
1038};
1039
1040static driver_t ofwfb_sc_driver = {
1041	SC_DRIVER_NAME,
1042	ofwfb_sc_methods,
1043	sizeof(sc_softc_t),
1044};
1045
1046static devclass_t	sc_devclass;
1047
1048DRIVER_MODULE(ofwfb, nexus, ofwfb_sc_driver, sc_devclass, 0, 0);
1049
1050/*
1051 * Define a stub keyboard driver in case one hasn't been
1052 * compiled into the kernel
1053 */
1054#include <sys/kbio.h>
1055#include <dev/kbd/kbdreg.h>
1056
1057static int dummy_kbd_configure(int flags);
1058
1059keyboard_switch_t dummysw;
1060
1061static int
1062dummy_kbd_configure(int flags)
1063{
1064
1065	return (0);
1066}
1067KEYBOARD_DRIVER(dummy, dummysw, dummy_kbd_configure);
1068
1069/*
1070 * Utility routines from <dev/fb/fbreg.h>
1071 */
1072void
1073ofwfb_bcopy(const void *s, void *d, size_t c)
1074{
1075	bcopy(s, d, c);
1076}
1077
1078void
1079ofwfb_bzero(void *d, size_t c)
1080{
1081	bzero(d, c);
1082}
1083
1084void
1085ofwfb_fillw(int pat, void *base, size_t cnt)
1086{
1087	u_int16_t *bptr = base;
1088
1089	while (cnt--)
1090		*bptr++ = pat;
1091}
1092
1093u_int16_t
1094ofwfb_readw(u_int16_t *addr)
1095{
1096	return (*addr);
1097}
1098
1099void
1100ofwfb_writew(u_int16_t *addr, u_int16_t val)
1101{
1102	*addr = val;
1103}
1104