vgafb.c revision 1.27
1/*	$OpenBSD: vgafb.c,v 1.27 2006/01/01 11:59:39 miod Exp $	*/
2/*	$NetBSD: vga.c,v 1.3 1996/12/02 22:24:54 cgd Exp $	*/
3
4/*
5 * Copyright (c) 1995, 1996 Carnegie-Mellon University.
6 * All rights reserved.
7 *
8 * Author: Chris G. Demetriou
9 *
10 * Permission to use, copy, modify and distribute this software and
11 * its documentation is hereby granted, provided that both the copyright
12 * notice and this permission notice appear in all copies of the
13 * software, derivative works or modified versions, and any portions
14 * thereof, and that both notices appear in supporting documentation.
15 *
16 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
17 * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
18 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
19 *
20 * Carnegie Mellon requests users of this software to return to
21 *
22 *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
23 *  School of Computer Science
24 *  Carnegie Mellon University
25 *  Pittsburgh PA 15213-3890
26 *
27 * any improvements or extensions that they make and grant Carnegie the
28 * rights to redistribute these changes.
29 */
30
31#include <sys/param.h>
32#include <sys/systm.h>
33#include <sys/kernel.h>
34#include <sys/device.h>
35#include <sys/buf.h>
36
37#include <uvm/uvm_extern.h>
38
39#include <machine/bus.h>
40
41#include <dev/cons.h>
42#include <dev/ofw/openfirm.h>
43#include <macppc/macppc/ofw_machdep.h>
44
45#include <dev/wscons/wsconsio.h>
46#include <dev/wscons/wsdisplayvar.h>
47#include <dev/wscons/wscons_raster.h>
48#include <dev/rasops/rasops.h>
49#include <dev/wsfont/wsfont.h>
50
51#include <macppc/pci/vgafbvar.h>
52
53struct cfdriver vgafb_cd = {
54	NULL, "vgafb", DV_DULL,
55};
56
57void vgafb_setcolor(struct vgafb_config *vc, unsigned int index,
58		    u_int8_t r, u_int8_t g, u_int8_t b);
59void vgafb_restore_default_colors(struct vgafb_config *vc);
60
61struct vgafb_devconfig {
62	struct rasops_info dc_rinfo;    /* raster display data */
63	int dc_blanked;			/* currently had video disabled */
64};
65
66struct vgafb_devconfig vgafb_console_dc;
67
68struct wsscreen_descr vgafb_stdscreen = {
69	"std",
70	0, 0,   /* will be filled in -- XXX shouldn't, it's global */
71	0,
72	0, 0,
73	WSSCREEN_UNDERLINE | WSSCREEN_HILIT |
74	WSSCREEN_REVERSE | WSSCREEN_WSCOLORS
75};
76const struct wsscreen_descr *vgafb_scrlist[] = {
77	&vgafb_stdscreen,
78	/* XXX other formats, graphics screen? */
79};
80
81struct wsscreen_list vgafb_screenlist = {
82	sizeof(vgafb_scrlist) / sizeof(struct wsscreen_descr *), vgafb_scrlist
83};
84
85struct wsdisplay_accessops vgafb_accessops = {
86	vgafb_ioctl,
87	vgafb_mmap,
88	vgafb_alloc_screen,
89	vgafb_free_screen,
90	vgafb_show_screen,
91	NULL,		/* load_font */
92	NULL,		/* scrollback */
93	NULL,		/* getchar */
94	vgafb_burn,	/* burner */
95};
96
97int	vgafb_getcmap(struct vgafb_config *vc, struct wsdisplay_cmap *cm);
98int	vgafb_putcmap(struct vgafb_config *vc, struct wsdisplay_cmap *cm);
99
100#define FONT_WIDTH 8
101#define FONT_HEIGHT 16
102
103/*
104 * The following functions implement back-end configuration grabbing
105 * and attachment.
106 */
107int
108vgafb_common_probe(bus_space_tag_t iot, bus_space_tag_t memt, u_int32_t iobase,
109    size_t iosize, u_int32_t membase, size_t memsize, u_int32_t mmiobase,
110    size_t mmiosize)
111{
112	bus_space_handle_t ioh_b, ioh_c, ioh_d, memh, mmioh;
113	int gotio_b, gotio_c, gotio_d, gotmem, gotmmio, rv;
114
115	gotio_b = gotio_c = gotio_d = gotmem = gotmmio = rv = 0;
116
117	if (iosize != 0) {
118		if (bus_space_map(iot, iobase+0x3b0, 0xc, 0, &ioh_b))
119			goto bad;
120		gotio_b = 1;
121		if (bus_space_map(iot, iobase+0x3c0, 0x10, 0, &ioh_c))
122			goto bad;
123		gotio_c = 1;
124		if (bus_space_map(iot, iobase+0x3d0, 0x10, 0, &ioh_d))
125			goto bad;
126		gotio_d = 1;
127	}
128	if (mmiosize != 0) {
129		if (bus_space_map(iot, mmiobase, mmiosize, 0, &mmioh))
130			goto bad;
131		gotmmio = 1;
132	}
133
134	rv = 1;
135
136bad:
137	if (gotio_b)
138		bus_space_unmap(iot, ioh_b, 0xc);
139	if (gotio_c)
140		bus_space_unmap(iot, ioh_c, 0x10);
141	if (gotio_d)
142		bus_space_unmap(iot, ioh_d, 0x10);
143	if (gotmmio)
144		bus_space_unmap(memt, mmioh, mmiosize);
145	if (gotmem)
146		bus_space_unmap(memt, memh, memsize);
147
148	return (rv);
149}
150
151void
152vgafb_common_setup(bus_space_tag_t iot, bus_space_tag_t  memt,
153    struct vgafb_config *vc, u_int32_t iobase, size_t iosize,
154    u_int32_t  membase, size_t memsize, u_int32_t mmiobase, size_t mmiosize)
155{
156        vc->vc_iot = iot;
157        vc->vc_memt = memt;
158	vc->vc_paddr = membase;
159
160	if (iosize != 0) {
161           if (bus_space_map(vc->vc_iot, iobase+0x3b0, 0xc, 0, &vc->vc_ioh_b))
162		panic("vgafb_common_setup: couldn't map io b");
163           if (bus_space_map(vc->vc_iot, iobase+0x3c0, 0x10, 0, &vc->vc_ioh_c))
164		panic("vgafb_common_setup: couldn't map io c");
165           if (bus_space_map(vc->vc_iot, iobase+0x3d0, 0x10, 0, &vc->vc_ioh_d))
166		panic("vgafb_common_setup: couldn't map io d");
167	}
168	if (mmiosize != 0)
169	       if (bus_space_map(vc->vc_memt, mmiobase, mmiosize, 0,
170		   &vc->vc_mmioh))
171			panic("vgafb_common_setup: couldn't map mmio");
172
173	/* memsize should only be visible region for console */
174	memsize = cons_height * cons_linebytes;
175        if (bus_space_map(vc->vc_memt, membase, memsize,
176	    /* XXX */ppc_proc_is_64b ? 0 : 1, &vc->vc_memh))
177		panic("vgafb_common_setup: couldn't map memory");
178	cons_display_mem_h = vc->vc_memh;
179	vc->vc_ofh = cons_display_ofh;
180
181
182	vc->vc_crow = vc->vc_ccol = 0; /* Has to be some onscreen value */
183	vc->vc_so = 0;
184
185	/* clear screen, frob cursor, etc.? */
186	/*
187	*/
188
189	vc->vc_at = 0x00 | 0xf;			/* black bg|white fg */
190	vc->vc_so_at = 0x00 | 0xf | 0x80;	/* black bg|white fg|blink */
191
192	if (cons_depth == 8) {
193		vgafb_restore_default_colors(vc);
194	}
195}
196
197void
198vgafb_restore_default_colors(struct vgafb_config *vc)
199{
200	int i;
201
202	for (i = 0; i < 256; i++) {
203		const u_char *color;
204
205		color = &rasops_cmap[i * 3];
206		vgafb_setcolor(vc, i, color[0], color[1], color[2]);
207	}
208}
209
210void
211vgafb_wsdisplay_attach(struct device *parent, struct vgafb_config *vc,
212    int console)
213{
214	struct wsemuldisplaydev_attach_args aa;
215
216        aa.console = console;
217	aa.scrdata = &vgafb_screenlist;
218	aa.accessops = &vgafb_accessops;
219	aa.accesscookie = vc;
220
221	/* no need to keep the burner function if no hw support */
222	if (cons_backlight_available == 0)
223		vgafb_accessops.burn_screen = NULL;
224	else {
225		vc->vc_backlight_on = WSDISPLAYIO_VIDEO_OFF;
226		vgafb_burn(vc, WSDISPLAYIO_VIDEO_ON, 0);	/* paranoia */
227	}
228
229        config_found(parent, &aa, wsemuldisplaydevprint);
230}
231
232int
233vgafb_ioctl(void *v, u_long cmd, caddr_t data, int flag, struct proc *p)
234{
235	struct vgafb_config *vc = v;
236	struct wsdisplay_fbinfo *wdf;
237
238	switch (cmd) {
239	case WSDISPLAYIO_GTYPE:
240		*(u_int *)data = WSDISPLAY_TYPE_PCIVGA;
241		return 0;
242	case WSDISPLAYIO_GINFO:
243		wdf = (void *)data;
244		wdf->height = cons_height;
245		wdf->width  = cons_width;
246		wdf->depth  = cons_depth;
247		wdf->cmsize = 256;
248		return 0;
249
250	case WSDISPLAYIO_LINEBYTES:
251		*(u_int *)data = cons_linebytes;
252		return 0;
253
254	case WSDISPLAYIO_GETCMAP:
255		return vgafb_getcmap(vc, (struct wsdisplay_cmap *)data);
256
257	case WSDISPLAYIO_PUTCMAP:
258		return vgafb_putcmap(vc, (struct wsdisplay_cmap *)data);
259
260	case WSDISPLAYIO_SMODE:
261		/* track the state of the display,
262		 * if returning to WSDISPLAYIO_MODE_EMUL
263		 * restore the last palette, workaround for
264		 * bad accellerated X servers that does not restore
265		 * the correct palette.
266		 */
267		if (cons_depth == 8)
268			vgafb_restore_default_colors(vc);
269		break;
270
271	case WSDISPLAYIO_GETPARAM:
272	{
273		struct wsdisplay_param *dp = (struct wsdisplay_param *)data;
274
275		switch (dp->param) {
276		case WSDISPLAYIO_PARAM_BRIGHTNESS:
277			if (cons_backlight_available != 0) {
278				dp->min = MIN_BRIGHTNESS;
279				dp->max = MAX_BRIGHTNESS;
280				dp->curval = cons_brightness;
281				return 0;
282			}
283			return -1;
284		case WSDISPLAYIO_PARAM_BACKLIGHT:
285			if (cons_backlight_available != 0) {
286				dp->min = 0;
287				dp->max = 1;
288				dp->curval = vc->vc_backlight_on;
289				return 0;
290			} else
291				return -1;
292		}
293	}
294		return -1;
295
296	case WSDISPLAYIO_SETPARAM:
297	{
298		struct wsdisplay_param *dp = (struct wsdisplay_param *)data;
299
300		switch (dp->param) {
301		case WSDISPLAYIO_PARAM_BRIGHTNESS:
302			if (cons_backlight_available == 1) {
303				of_setbrightness(dp->curval);
304				return 0;
305			} else
306				return -1;
307		case WSDISPLAYIO_PARAM_BACKLIGHT:
308			if (cons_backlight_available != 0) {
309				vgafb_burn(vc,
310				    dp->curval ? WSDISPLAYIO_VIDEO_ON :
311				      WSDISPLAYIO_VIDEO_OFF, 0);
312				return 0;
313			} else
314				return -1;
315		}
316	}
317		return -1;
318
319	case WSDISPLAYIO_SVIDEO:
320	case WSDISPLAYIO_GVIDEO:
321		break;
322
323	case WSDISPLAYIO_GCURPOS:
324	case WSDISPLAYIO_SCURPOS:
325	case WSDISPLAYIO_GCURMAX:
326	case WSDISPLAYIO_GCURSOR:
327	case WSDISPLAYIO_SCURSOR:
328	default:
329		return -1; /* not supported yet */
330	}
331
332        return (0);
333}
334
335paddr_t
336vgafb_mmap(void *v, off_t offset, int prot)
337{
338	struct vgafb_config *vc = v;
339	bus_space_handle_t h;
340
341	/* memsize... */
342	if (offset >= 0x00000 && offset < vc->memsize)
343		h = vc->vc_paddr + offset;
344	/* XXX the following are probably wrong. we want physical addresses
345	   here, not virtual ones */
346	else if (offset >= 0x10000000 && offset < 0x10040000 )
347		/* 256KB of iohb */
348		h = vc->vc_ioh_b;
349	else if (offset >= 0x10040000 && offset < 0x10080000)
350		/* 256KB of iohc */
351		h = vc->vc_ioh_c;
352	else if (offset >= 0x18880000 && offset < 0x100c0000)
353		/* 256KB of iohd */
354		h = vc->vc_ioh_d;
355	else if (offset >= 0x20000000 && offset < 0x20000000+vc->mmiosize)
356		/* mmiosize... */
357		h = vc->vc_mmioh + (offset - 0x20000000);
358	else if (offset >= vc->membase && (offset < vc->membase+vc->memsize)) {
359		/* allow mmapping of memory */
360		h = offset;
361	} else if (offset >= vc->mmiobase &&
362	    (offset < vc->mmiobase+vc->mmiosize)) {
363		/* allow mmapping of mmio space */
364		h = offset;
365	} else {
366		h = -1;
367	}
368
369	return h;
370}
371
372
373void
374vgafb_cnprobe(struct consdev *cp)
375{
376	if (cons_displaytype != 1)
377		return;
378
379	cp->cn_pri = CN_INTERNAL;
380}
381
382void
383vgafb_cnattach(bus_space_tag_t iot, bus_space_tag_t  memt, void *pc, int bus,
384    int  device, int function)
385{
386        long defattr;
387
388	struct vgafb_devconfig *dc = &vgafb_console_dc;
389        struct rasops_info *ri = &dc->dc_rinfo;
390
391	ri->ri_flg = RI_CENTER;
392	ri->ri_depth = cons_depth;
393	ri->ri_bits = (void *)cons_display_mem_h;
394	ri->ri_width = cons_width;
395	ri->ri_height = cons_height;
396	ri->ri_stride = cons_linebytes;
397	ri->ri_hw = dc;
398
399	rasops_init(ri, 160, 160);	/* XXX */
400
401	vgafb_stdscreen.nrows = ri->ri_rows;
402	vgafb_stdscreen.ncols = ri->ri_cols;
403	vgafb_stdscreen.textops = &ri->ri_ops;
404	ri->ri_ops.alloc_attr(ri, 0, 0, 0, &defattr);
405
406	wsdisplay_cnattach(&vgafb_stdscreen, ri, 0, 0, defattr);
407}
408
409struct {
410	u_int8_t r;
411	u_int8_t g;
412	u_int8_t b;
413} vgafb_color[256];
414
415void
416vgafb_setcolor(struct vgafb_config *vc, unsigned int index, u_int8_t r,
417    u_int8_t g, u_int8_t b)
418{
419	vc->vc_cmap_red[index] = r;
420	vc->vc_cmap_green[index] = g;
421	vc->vc_cmap_blue[index] = b;
422
423	vgafb_color[index].r = r;
424	vgafb_color[index].g = g;
425	vgafb_color[index].b = b;
426	OF_call_method_1("set-colors", cons_display_ofh, 3,
427	    &vgafb_color[index], index, 1);
428}
429
430int
431vgafb_getcmap(struct vgafb_config *vc, struct wsdisplay_cmap *cm)
432{
433	u_int index = cm->index;
434	u_int count = cm->count;
435	int error;
436
437	if (index >= 256 || count > 256 - index)
438		return EINVAL;
439
440	error = copyout(&vc->vc_cmap_red[index],   cm->red,   count);
441	if (error)
442		return error;
443	error = copyout(&vc->vc_cmap_green[index], cm->green, count);
444	if (error)
445		return error;
446	error = copyout(&vc->vc_cmap_blue[index],  cm->blue,  count);
447	if (error)
448		return error;
449
450	return 0;
451}
452
453int
454vgafb_putcmap(struct vgafb_config *vc, struct wsdisplay_cmap *cm)
455{
456	u_int index = cm->index;
457	u_int count = cm->count;
458	u_int i;
459	int error;
460	u_int8_t *r, *g, *b;
461
462	if (index >= 256 || count > 256 - index)
463		return EINVAL;
464
465	if ((error = copyin(cm->red, &vc->vc_cmap_red[index], count)) != 0)
466		return (error);
467	if ((error = copyin(cm->green, &vc->vc_cmap_green[index], count)) != 0)
468		return (error);
469	if ((error = copyin(cm->blue, &vc->vc_cmap_blue[index], count)) != 0)
470		return (error);
471
472	r = &(vc->vc_cmap_red[index]);
473	g = &(vc->vc_cmap_green[index]);
474	b = &(vc->vc_cmap_blue[index]);
475
476	for (i = 0; i < count; i++) {
477		vgafb_color[i].r = *r;
478		vgafb_color[i].g = *g;
479		vgafb_color[i].b = *b;
480		r++, g++, b++;
481	}
482	OF_call_method_1("set-colors", cons_display_ofh, 3,
483	    &vgafb_color, index, count);
484	return 0;
485}
486
487void
488vgafb_burn(void *v, u_int on, u_int flags)
489{
490	struct vgafb_config *vc = v;
491
492	if (cons_backlight_available == 1 &&
493	    vc->vc_backlight_on != on) {
494		if (on == WSDISPLAYIO_VIDEO_ON) {
495			OF_call_method_1("backlight-on", cons_display_ofh, 0);
496		} else {
497			OF_call_method_1("backlight-off", cons_display_ofh, 0);
498		}
499		vc->vc_backlight_on = on;
500	}
501}
502