1 /* $NetBSD: vga_common.c,v 1.11 2009/02/19 00:39:26 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.11 2009/02/19 00:39:26 jmcneill Exp $");
32 
33 #include <sys/param.h>
34 #include <sys/device.h>
35 #include <sys/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 /*
43  * The following functions implement back-end configuration grabbing
44  * and attachment.
45  */
46 int
vga_common_probe(bus_space_tag_t iot,bus_space_tag_t memt)47 vga_common_probe(bus_space_tag_t iot, bus_space_tag_t memt)
48 {
49           bus_space_handle_t ioh_vga, ioh_6845, memh;
50           u_int8_t regval;
51           u_int16_t vgadata;
52           int gotio_vga, gotio_6845, gotmem, mono, rv;
53           int dispoffset;
54 
55           gotio_vga = gotio_6845 = gotmem = rv = 0;
56 
57           if (bus_space_map(iot, 0x3c0, 0x10, 0, &ioh_vga))
58                     goto bad;
59           gotio_vga = 1;
60 
61           /* read "misc output register" */
62           regval = bus_space_read_1(iot, ioh_vga, VGA_MISC_DATAR);
63           mono = !(regval & 1);
64 
65           if (bus_space_map(iot, (mono ? 0x3b0 : 0x3d0), 0x10, 0, &ioh_6845))
66                     goto bad;
67           gotio_6845 = 1;
68 
69           if (bus_space_map(memt, 0xa0000, 0x20000, 0, &memh))
70                     goto bad;
71           gotmem = 1;
72 
73           dispoffset = (mono ? 0x10000 : 0x18000);
74 
75           vgadata = bus_space_read_2(memt, memh, dispoffset);
76           bus_space_write_2(memt, memh, dispoffset, 0xa55a);
77           if (bus_space_read_2(memt, memh, dispoffset) != 0xa55a)
78                     goto bad;
79           bus_space_write_2(memt, memh, dispoffset, vgadata);
80 
81           /*
82            * check if this is really a VGA
83            * (try to write "Color Select" register as XFree86 does)
84            * XXX check before if at least EGA?
85            */
86           /* reset state */
87           (void) bus_space_read_1(iot, ioh_6845, 10);
88           bus_space_write_1(iot, ioh_vga, VGA_ATC_INDEX,
89               20 | 0x20); /* colselect | enable */
90           regval = bus_space_read_1(iot, ioh_vga, VGA_ATC_DATAR);
91           /* toggle the implemented bits */
92           bus_space_write_1(iot, ioh_vga, VGA_ATC_DATAW, regval ^ 0x0f);
93           bus_space_write_1(iot, ioh_vga, VGA_ATC_INDEX, 20 | 0x20);
94           /* read back */
95           if (bus_space_read_1(iot, ioh_vga, VGA_ATC_DATAR) != (regval ^ 0x0f))
96                     goto bad;
97           /* restore contents */
98           bus_space_write_1(iot, ioh_vga, VGA_ATC_DATAW, regval);
99 
100           rv = 1;
101 bad:
102           if (gotio_vga)
103                     bus_space_unmap(iot, ioh_vga, 0x10);
104           if (gotio_6845)
105                     bus_space_unmap(iot, ioh_6845, 0x10);
106           if (gotmem)
107                     bus_space_unmap(memt, memh, 0x20000);
108 
109           return (rv);
110 }
111