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