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