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