xref: /netbsd-src/sys/dev/ic/vga_common.c (revision dc02b079e40c6c2260c14c7efd59ca0c725a2295)
1*dc02b079Sjmcneill /* $NetBSD: vga_common.c,v 1.11 2009/02/19 00:39:26 jmcneill Exp $ */
20f061c7cSjunyoung 
30f061c7cSjunyoung /*
40f061c7cSjunyoung  * Copyright (c) 1995, 1996 Carnegie-Mellon University.
50f061c7cSjunyoung  * All rights reserved.
60f061c7cSjunyoung  *
70f061c7cSjunyoung  * Author: Chris G. Demetriou
80f061c7cSjunyoung  *
90f061c7cSjunyoung  * Permission to use, copy, modify and distribute this software and
100f061c7cSjunyoung  * its documentation is hereby granted, provided that both the copyright
110f061c7cSjunyoung  * notice and this permission notice appear in all copies of the
120f061c7cSjunyoung  * software, derivative works or modified versions, and any portions
130f061c7cSjunyoung  * thereof, and that both notices appear in supporting documentation.
140f061c7cSjunyoung  *
150f061c7cSjunyoung  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
160f061c7cSjunyoung  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
170f061c7cSjunyoung  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
180f061c7cSjunyoung  *
190f061c7cSjunyoung  * Carnegie Mellon requests users of this software to return to
200f061c7cSjunyoung  *
210f061c7cSjunyoung  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
220f061c7cSjunyoung  *  School of Computer Science
230f061c7cSjunyoung  *  Carnegie Mellon University
240f061c7cSjunyoung  *  Pittsburgh PA 15213-3890
250f061c7cSjunyoung  *
260f061c7cSjunyoung  * any improvements or extensions that they make and grant Carnegie the
270f061c7cSjunyoung  * rights to redistribute these changes.
280f061c7cSjunyoung  */
290f061c7cSjunyoung 
30365cbd94Slukem #include <sys/cdefs.h>
31*dc02b079Sjmcneill __KERNEL_RCSID(0, "$NetBSD: vga_common.c,v 1.11 2009/02/19 00:39:26 jmcneill Exp $");
32365cbd94Slukem 
330f061c7cSjunyoung #include <sys/param.h>
340f061c7cSjunyoung #include <sys/device.h>
35a2a38285Sad #include <sys/bus.h>
360f061c7cSjunyoung 
370f061c7cSjunyoung #include <dev/ic/mc6845reg.h>
380f061c7cSjunyoung #include <dev/ic/pcdisplayvar.h>
390f061c7cSjunyoung #include <dev/ic/vgareg.h>
400f061c7cSjunyoung #include <dev/ic/vgavar.h>
410f061c7cSjunyoung 
420f061c7cSjunyoung /*
430f061c7cSjunyoung  * The following functions implement back-end configuration grabbing
440f061c7cSjunyoung  * and attachment.
450f061c7cSjunyoung  */
460f061c7cSjunyoung int
vga_common_probe(bus_space_tag_t iot,bus_space_tag_t memt)470f061c7cSjunyoung vga_common_probe(bus_space_tag_t iot, bus_space_tag_t memt)
480f061c7cSjunyoung {
490f061c7cSjunyoung 	bus_space_handle_t ioh_vga, ioh_6845, memh;
500f061c7cSjunyoung 	u_int8_t regval;
510f061c7cSjunyoung 	u_int16_t vgadata;
520f061c7cSjunyoung 	int gotio_vga, gotio_6845, gotmem, mono, rv;
530f061c7cSjunyoung 	int dispoffset;
540f061c7cSjunyoung 
550f061c7cSjunyoung 	gotio_vga = gotio_6845 = gotmem = rv = 0;
560f061c7cSjunyoung 
570f061c7cSjunyoung 	if (bus_space_map(iot, 0x3c0, 0x10, 0, &ioh_vga))
580f061c7cSjunyoung 		goto bad;
590f061c7cSjunyoung 	gotio_vga = 1;
600f061c7cSjunyoung 
610f061c7cSjunyoung 	/* read "misc output register" */
6289b4d622Stsutsui 	regval = bus_space_read_1(iot, ioh_vga, VGA_MISC_DATAR);
630f061c7cSjunyoung 	mono = !(regval & 1);
640f061c7cSjunyoung 
650f061c7cSjunyoung 	if (bus_space_map(iot, (mono ? 0x3b0 : 0x3d0), 0x10, 0, &ioh_6845))
660f061c7cSjunyoung 		goto bad;
670f061c7cSjunyoung 	gotio_6845 = 1;
680f061c7cSjunyoung 
690f061c7cSjunyoung 	if (bus_space_map(memt, 0xa0000, 0x20000, 0, &memh))
700f061c7cSjunyoung 		goto bad;
710f061c7cSjunyoung 	gotmem = 1;
720f061c7cSjunyoung 
730f061c7cSjunyoung 	dispoffset = (mono ? 0x10000 : 0x18000);
740f061c7cSjunyoung 
750f061c7cSjunyoung 	vgadata = bus_space_read_2(memt, memh, dispoffset);
760f061c7cSjunyoung 	bus_space_write_2(memt, memh, dispoffset, 0xa55a);
770f061c7cSjunyoung 	if (bus_space_read_2(memt, memh, dispoffset) != 0xa55a)
780f061c7cSjunyoung 		goto bad;
790f061c7cSjunyoung 	bus_space_write_2(memt, memh, dispoffset, vgadata);
800f061c7cSjunyoung 
810f061c7cSjunyoung 	/*
820f061c7cSjunyoung 	 * check if this is really a VGA
830f061c7cSjunyoung 	 * (try to write "Color Select" register as XFree86 does)
840f061c7cSjunyoung 	 * XXX check before if at least EGA?
850f061c7cSjunyoung 	 */
860f061c7cSjunyoung 	/* reset state */
870f061c7cSjunyoung 	(void) bus_space_read_1(iot, ioh_6845, 10);
880f061c7cSjunyoung 	bus_space_write_1(iot, ioh_vga, VGA_ATC_INDEX,
890f061c7cSjunyoung 	    20 | 0x20); /* colselect | enable */
900f061c7cSjunyoung 	regval = bus_space_read_1(iot, ioh_vga, VGA_ATC_DATAR);
910f061c7cSjunyoung 	/* toggle the implemented bits */
920f061c7cSjunyoung 	bus_space_write_1(iot, ioh_vga, VGA_ATC_DATAW, regval ^ 0x0f);
93a8c7515fStsutsui 	bus_space_write_1(iot, ioh_vga, VGA_ATC_INDEX, 20 | 0x20);
940f061c7cSjunyoung 	/* read back */
950f061c7cSjunyoung 	if (bus_space_read_1(iot, ioh_vga, VGA_ATC_DATAR) != (regval ^ 0x0f))
960f061c7cSjunyoung 		goto bad;
970f061c7cSjunyoung 	/* restore contents */
980f061c7cSjunyoung 	bus_space_write_1(iot, ioh_vga, VGA_ATC_DATAW, regval);
990f061c7cSjunyoung 
1000f061c7cSjunyoung 	rv = 1;
1010f061c7cSjunyoung bad:
1020f061c7cSjunyoung 	if (gotio_vga)
1030f061c7cSjunyoung 		bus_space_unmap(iot, ioh_vga, 0x10);
1040f061c7cSjunyoung 	if (gotio_6845)
1050f061c7cSjunyoung 		bus_space_unmap(iot, ioh_6845, 0x10);
1060f061c7cSjunyoung 	if (gotmem)
1070f061c7cSjunyoung 		bus_space_unmap(memt, memh, 0x20000);
1080f061c7cSjunyoung 
1090f061c7cSjunyoung 	return (rv);
1100f061c7cSjunyoung }
111