xref: /netbsd/sys/dev/ic/vga_common.c (revision b74d978f)
1 /* $NetBSD: vga_common.c,v 1.7 2006/02/19 15:16:53 jmcneill Exp $ */
2 
3 /*
4  * Copyright (c) 1995, 1996 Carnegie-Mellon University.
5  * All rights reserved.
6  *
7  * Author: Chris G. Demetriou
8  *
9  * Permission to use, copy, modify and distribute this software and
10  * its documentation is hereby granted, provided that both the copyright
11  * notice and this permission notice appear in all copies of the
12  * software, derivative works or modified versions, and any portions
13  * thereof, and that both notices appear in supporting documentation.
14  *
15  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
16  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
17  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
18  *
19  * Carnegie Mellon requests users of this software to return to
20  *
21  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
22  *  School of Computer Science
23  *  Carnegie Mellon University
24  *  Pittsburgh PA 15213-3890
25  *
26  * any improvements or extensions that they make and grant Carnegie the
27  * rights to redistribute these changes.
28  */
29 
30 #include <sys/cdefs.h>
31 __KERNEL_RCSID(0, "$NetBSD: vga_common.c,v 1.7 2006/02/19 15:16:53 jmcneill Exp $");
32 
33 #include <sys/param.h>
34 #include <sys/device.h>
35 #include <machine/bus.h>
36 
37 #include <dev/ic/mc6845reg.h>
38 #include <dev/ic/pcdisplayvar.h>
39 #include <dev/ic/vgareg.h>
40 #include <dev/ic/vgavar.h>
41 
42 #ifdef __i386__
43 #include <arch/i386/bios/vesafbvar.h>
44 #endif
45 
46 /*
47  * The following functions implement back-end configuration grabbing
48  * and attachment.
49  */
50 int
51 vga_common_probe(bus_space_tag_t iot, bus_space_tag_t memt)
52 {
53 	bus_space_handle_t ioh_vga, ioh_6845, memh;
54 	u_int8_t regval;
55 	u_int16_t vgadata;
56 	int gotio_vga, gotio_6845, gotmem, mono, rv;
57 	int dispoffset;
58 #ifdef __i386__
59 	struct device *dv;
60 	struct vesafb_softc *vesafb;
61 #endif
62 
63 	gotio_vga = gotio_6845 = gotmem = rv = 0;
64 
65 #ifdef __i386__
66 	for (dv = alldevs.tqh_first; dv; dv=dv->dv_list.tqe_next)
67 		if (strncmp(dv->dv_xname, "vesafb", 6) == 0) {
68 			vesafb = (struct vesafb_softc *)dv;
69 			if (vesafb->sc_isconsole)
70 				goto bad;
71 		}
72 #endif
73 
74 	if (bus_space_map(iot, 0x3c0, 0x10, 0, &ioh_vga))
75 		goto bad;
76 	gotio_vga = 1;
77 
78 	/* read "misc output register" */
79 	regval = bus_space_read_1(iot, ioh_vga, VGA_MISC_DATAR);
80 	mono = !(regval & 1);
81 
82 	if (bus_space_map(iot, (mono ? 0x3b0 : 0x3d0), 0x10, 0, &ioh_6845))
83 		goto bad;
84 	gotio_6845 = 1;
85 
86 	if (bus_space_map(memt, 0xa0000, 0x20000, 0, &memh))
87 		goto bad;
88 	gotmem = 1;
89 
90 	dispoffset = (mono ? 0x10000 : 0x18000);
91 
92 	vgadata = bus_space_read_2(memt, memh, dispoffset);
93 	bus_space_write_2(memt, memh, dispoffset, 0xa55a);
94 	if (bus_space_read_2(memt, memh, dispoffset) != 0xa55a)
95 		goto bad;
96 	bus_space_write_2(memt, memh, dispoffset, vgadata);
97 
98 	/*
99 	 * check if this is really a VGA
100 	 * (try to write "Color Select" register as XFree86 does)
101 	 * XXX check before if at least EGA?
102 	 */
103 	/* reset state */
104 	(void) bus_space_read_1(iot, ioh_6845, 10);
105 	bus_space_write_1(iot, ioh_vga, VGA_ATC_INDEX,
106 	    20 | 0x20); /* colselect | enable */
107 	regval = bus_space_read_1(iot, ioh_vga, VGA_ATC_DATAR);
108 	/* toggle the implemented bits */
109 	bus_space_write_1(iot, ioh_vga, VGA_ATC_DATAW, regval ^ 0x0f);
110 	bus_space_write_1(iot, ioh_vga, VGA_ATC_INDEX, 20 | 0x20);
111 	/* read back */
112 	if (bus_space_read_1(iot, ioh_vga, VGA_ATC_DATAR) != (regval ^ 0x0f))
113 		goto bad;
114 	/* restore contents */
115 	bus_space_write_1(iot, ioh_vga, VGA_ATC_DATAW, regval);
116 
117 	rv = 1;
118 bad:
119 	if (gotio_vga)
120 		bus_space_unmap(iot, ioh_vga, 0x10);
121 	if (gotio_6845)
122 		bus_space_unmap(iot, ioh_6845, 0x10);
123 	if (gotmem)
124 		bus_space_unmap(memt, memh, 0x20000);
125 
126 	return (rv);
127 }
128