Home | History | Annotate | Line # | Download | only in ofw
vga_ofbus.c revision 1.12.16.2
      1  1.12.16.1       mjf /* $NetBSD: vga_ofbus.c,v 1.12.16.2 2008/06/02 13:22:39 mjf Exp $ */
      2        1.1   thorpej 
      3        1.1   thorpej /*
      4        1.1   thorpej  * Copyright (c) 1995, 1996 Carnegie-Mellon University.
      5        1.1   thorpej  * All rights reserved.
      6        1.1   thorpej  *
      7        1.1   thorpej  * Author: Chris G. Demetriou
      8        1.1   thorpej  *
      9        1.1   thorpej  * Permission to use, copy, modify and distribute this software and
     10        1.1   thorpej  * its documentation is hereby granted, provided that both the copyright
     11        1.1   thorpej  * notice and this permission notice appear in all copies of the
     12        1.1   thorpej  * software, derivative works or modified versions, and any portions
     13        1.1   thorpej  * thereof, and that both notices appear in supporting documentation.
     14        1.1   thorpej  *
     15        1.1   thorpej  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
     16        1.1   thorpej  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
     17        1.1   thorpej  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
     18        1.1   thorpej  *
     19        1.1   thorpej  * Carnegie Mellon requests users of this software to return to
     20        1.1   thorpej  *
     21        1.1   thorpej  *  Software Distribution Coordinator  or  Software.Distribution (at) CS.CMU.EDU
     22        1.1   thorpej  *  School of Computer Science
     23        1.1   thorpej  *  Carnegie Mellon University
     24        1.1   thorpej  *  Pittsburgh PA 15213-3890
     25        1.1   thorpej  *
     26        1.1   thorpej  * any improvements or extensions that they make and grant Carnegie the
     27        1.1   thorpej  * rights to redistribute these changes.
     28        1.1   thorpej  */
     29        1.5     lukem 
     30        1.5     lukem #include <sys/cdefs.h>
     31  1.12.16.1       mjf __KERNEL_RCSID(0, "$NetBSD: vga_ofbus.c,v 1.12.16.2 2008/06/02 13:22:39 mjf Exp $");
     32        1.1   thorpej 
     33        1.1   thorpej #include <sys/param.h>
     34        1.1   thorpej #include <sys/systm.h>
     35        1.1   thorpej #include <sys/kernel.h>
     36        1.1   thorpej #include <sys/device.h>
     37        1.1   thorpej #include <sys/malloc.h>
     38  1.12.16.2       mjf #include <sys/proc.h>
     39  1.12.16.2       mjf #include <sys/kauth.h>
     40        1.1   thorpej 
     41        1.1   thorpej #include <dev/isa/isavar.h>
     42        1.1   thorpej 
     43        1.1   thorpej #include <dev/ic/mc6845reg.h>
     44        1.1   thorpej #include <dev/ic/pcdisplayvar.h>
     45        1.1   thorpej #include <dev/ic/vgareg.h>
     46        1.1   thorpej #include <dev/ic/vgavar.h>
     47        1.1   thorpej 
     48        1.1   thorpej #include <dev/wscons/wsconsio.h>
     49        1.1   thorpej #include <dev/wscons/wsdisplayvar.h>
     50        1.1   thorpej 
     51        1.1   thorpej #include <dev/ofw/openfirm.h>
     52        1.6   tsutsui 
     53        1.6   tsutsui #include <shark/ofw/vga_ofbusvar.h>
     54        1.1   thorpej 
     55        1.1   thorpej struct vga_ofbus_softc {
     56        1.1   thorpej 	struct vga_softc sc_vga;
     57        1.1   thorpej 
     58        1.1   thorpej 	int sc_phandle;
     59        1.1   thorpej };
     60        1.1   thorpej 
     61       1.12      jmmv #if (NIGSFB_OFBUS > 0) || (NVGA_OFBUS > 0)
     62        1.9  macallan extern int console_ihandle;
     63       1.10      jmmv #endif
     64        1.9  macallan 
     65  1.12.16.1       mjf int	vga_ofbus_match (device_t, cfdata_t, void *);
     66  1.12.16.1       mjf void	vga_ofbus_attach (device_t, device_t, void *);
     67        1.1   thorpej 
     68  1.12.16.1       mjf CFATTACH_DECL_NEW(vga_ofbus, sizeof(struct vga_ofbus_softc),
     69        1.4   thorpej     vga_ofbus_match, vga_ofbus_attach, NULL, NULL);
     70        1.1   thorpej 
     71        1.1   thorpej static const char *compat_strings[] = { "pnpPNP,900", 0 };
     72        1.1   thorpej 
     73  1.12.16.2       mjf static	int vga_ofbus_ioctl(void *, u_long, void *, int, struct lwp *);
     74  1.12.16.2       mjf static	paddr_t vga_ofbus_mmap(void *, off_t, int);
     75  1.12.16.2       mjf 
     76  1.12.16.2       mjf static const struct vga_funcs vga_ofbus_funcs = {
     77  1.12.16.2       mjf 	vga_ofbus_ioctl,
     78  1.12.16.2       mjf 	vga_ofbus_mmap
     79  1.12.16.2       mjf };
     80  1.12.16.2       mjf static uint32_t vga_reg[12];
     81  1.12.16.2       mjf extern paddr_t isa_io_physaddr, isa_mem_physaddr;
     82  1.12.16.2       mjf 
     83        1.1   thorpej int
     84  1.12.16.1       mjf vga_ofbus_match(device_t parent, cfdata_t match, void *aux)
     85        1.1   thorpej {
     86        1.1   thorpej 	struct ofbus_attach_args *oba = aux;
     87        1.1   thorpej 
     88        1.1   thorpej 	if (of_compatible(oba->oba_phandle, compat_strings) == -1)
     89        1.1   thorpej 		return (0);
     90        1.1   thorpej 
     91        1.1   thorpej 	if (!vga_is_console(&isa_io_bs_tag, WSDISPLAY_TYPE_ISAVGA) &&
     92        1.1   thorpej 	    !vga_common_probe(&isa_io_bs_tag, &isa_mem_bs_tag))
     93        1.1   thorpej 		return (0);
     94        1.1   thorpej 
     95        1.1   thorpej 	return (2);	/* more than generic pcdisplay */
     96        1.1   thorpej }
     97        1.1   thorpej 
     98        1.1   thorpej void
     99  1.12.16.1       mjf vga_ofbus_attach(device_t parent, device_t self, void *aux)
    100        1.1   thorpej {
    101  1.12.16.1       mjf 	struct vga_ofbus_softc *osc = device_private(self);
    102        1.1   thorpej 	struct vga_softc *sc = &osc->sc_vga;
    103        1.1   thorpej 	struct ofbus_attach_args *oba = aux;
    104  1.12.16.2       mjf 	int vga_handle, i;
    105        1.1   thorpej 
    106  1.12.16.1       mjf 	sc->sc_dev = self;
    107  1.12.16.1       mjf 	aprint_normal("\n");
    108        1.1   thorpej 	osc->sc_phandle = oba->oba_phandle;
    109        1.1   thorpej 
    110  1.12.16.2       mjf 	vga_handle = OF_finddevice("/vlbus/display");
    111  1.12.16.2       mjf 	OF_getprop(vga_handle, "reg", vga_reg, sizeof(vga_reg));
    112  1.12.16.2       mjf 
    113  1.12.16.2       mjf 	/* for some idiotic reason we get this in the wrong byte order */
    114  1.12.16.2       mjf 	for (i = 0; i < 12; i++) {
    115  1.12.16.2       mjf 		vga_reg[i] = be32toh(vga_reg[i]);
    116  1.12.16.2       mjf 		printf("%08x\n", vga_reg[i]);
    117  1.12.16.2       mjf 	}
    118  1.12.16.2       mjf 
    119        1.1   thorpej 	vga_common_attach(sc, &isa_io_bs_tag, &isa_mem_bs_tag,
    120  1.12.16.2       mjf 	    WSDISPLAY_TYPE_ISAVGA, 0, &vga_ofbus_funcs);
    121  1.12.16.2       mjf 	if (vga_reg[10] > 0) {
    122  1.12.16.2       mjf 		aprint_normal("%s: aperture at 0x%08x\n", device_xname(self),
    123  1.12.16.2       mjf 		    vga_reg[10]);
    124  1.12.16.2       mjf 	}
    125        1.1   thorpej }
    126        1.1   thorpej 
    127        1.1   thorpej int
    128        1.6   tsutsui vga_ofbus_cnattach(bus_space_tag_t iot, bus_space_tag_t memt)
    129        1.1   thorpej {
    130        1.6   tsutsui 	int chosen_phandle;
    131        1.9  macallan 	int stdout_ihandle, stdout_phandle, ret;
    132        1.6   tsutsui 	char buf[128];
    133        1.6   tsutsui 
    134        1.6   tsutsui 	stdout_phandle = 0;
    135        1.6   tsutsui 
    136        1.6   tsutsui 	/*
    137        1.6   tsutsui 	 * Find out whether the firmware's chosen stdout is
    138        1.6   tsutsui 	 * a display.  If so, use the existing ihandle so the firmware
    139        1.6   tsutsui 	 * doesn't become Unhappy.  If not, just open it.
    140        1.6   tsutsui 	 */
    141        1.6   tsutsui 	if ((chosen_phandle = OF_finddevice("/chosen")) == -1 ||
    142        1.6   tsutsui 	    OF_getprop(chosen_phandle, "stdout", &stdout_ihandle,
    143        1.6   tsutsui 	    sizeof(stdout_ihandle)) != sizeof(stdout_ihandle)) {
    144        1.7   tsutsui 		return ENXIO;
    145        1.6   tsutsui 	}
    146        1.6   tsutsui 	stdout_ihandle = of_decode_int((unsigned char *)&stdout_ihandle);
    147        1.6   tsutsui 	if ((stdout_phandle = OF_instance_to_package(stdout_ihandle)) == -1 ||
    148        1.6   tsutsui 	    OF_getprop(stdout_phandle, "device_type", buf, sizeof(buf)) <= 0) {
    149        1.7   tsutsui 		return ENXIO;
    150        1.6   tsutsui 	}
    151        1.6   tsutsui 
    152        1.6   tsutsui 	if (strcmp(buf, "display") != 0) {
    153        1.6   tsutsui 		/* The display is not stdout device. */
    154        1.7   tsutsui 		return ENXIO;
    155        1.6   tsutsui 	}
    156        1.6   tsutsui 
    157        1.6   tsutsui 	if (OF_call_method("text-mode3", stdout_ihandle, 0, 0) != 0) {
    158        1.1   thorpej 		printf("vga_ofbus_match: text-mode3 method invocation on VGA "
    159        1.1   thorpej 		       "screen device failed\n");
    160        1.1   thorpej 	}
    161        1.1   thorpej 
    162        1.9  macallan 	ret = vga_cnattach(iot, memt, WSDISPLAY_TYPE_ISAVGA, 1);
    163       1.12      jmmv #if (NIGSFB_OFBUS > 0) || (NVGA_OFBUS > 0)
    164        1.9  macallan 	if (ret == 0)
    165        1.9  macallan 		console_ihandle = stdout_ihandle;
    166       1.10      jmmv #endif
    167        1.9  macallan 
    168        1.9  macallan 	return ret;
    169        1.1   thorpej }
    170  1.12.16.2       mjf 
    171  1.12.16.2       mjf static int
    172  1.12.16.2       mjf vga_ofbus_ioctl(void *cookie, u_long cmd, void *data, int flag, struct lwp *l)
    173  1.12.16.2       mjf {
    174  1.12.16.2       mjf 	/* we should catch WSDISPLAYIO_SMODE here */
    175  1.12.16.2       mjf 	return 0;
    176  1.12.16.2       mjf }
    177  1.12.16.2       mjf 
    178  1.12.16.2       mjf static paddr_t
    179  1.12.16.2       mjf vga_ofbus_mmap(void *cookie, off_t offset, int prot)
    180  1.12.16.2       mjf {
    181  1.12.16.2       mjf 
    182  1.12.16.2       mjf 	/* only the superuser may mmap IO and aperture */
    183  1.12.16.2       mjf 	if (curlwp != NULL) {
    184  1.12.16.2       mjf 		if (kauth_authorize_generic(kauth_cred_get(),
    185  1.12.16.2       mjf 		    KAUTH_GENERIC_ISSUSER, NULL) != 0) {
    186  1.12.16.2       mjf 			return -1;
    187  1.12.16.2       mjf 		}
    188  1.12.16.2       mjf 	}
    189  1.12.16.2       mjf 
    190  1.12.16.2       mjf 	/*
    191  1.12.16.2       mjf 	 * XXX
    192  1.12.16.2       mjf 	 * we should really use bus_space_mmap here but for some reason
    193  1.12.16.2       mjf 	 * the ISA tags contain kernel virtual addresses and translating them
    194  1.12.16.2       mjf 	 * back to physical addresses doesn't really work
    195  1.12.16.2       mjf 	 */
    196  1.12.16.2       mjf 	/* access to IO ports */
    197  1.12.16.2       mjf 	if ((offset >= PCI_MAGIC_IO_RANGE) &&
    198  1.12.16.2       mjf 	    (offset < (PCI_MAGIC_IO_RANGE + 0x10000))) {
    199  1.12.16.2       mjf 		paddr_t pa;
    200  1.12.16.2       mjf 
    201  1.12.16.2       mjf 		pa = isa_io_physaddr + offset - PCI_MAGIC_IO_RANGE;
    202  1.12.16.2       mjf 		return arm_btop(pa);
    203  1.12.16.2       mjf 	}
    204  1.12.16.2       mjf 
    205  1.12.16.2       mjf 	/* legacy VGA aperture */
    206  1.12.16.2       mjf 	if ((offset >= 0xa0000) && (offset < 0xc0000)) {
    207  1.12.16.2       mjf 
    208  1.12.16.2       mjf 		return arm_btop(isa_mem_physaddr + offset);
    209  1.12.16.2       mjf 	}
    210  1.12.16.2       mjf 
    211  1.12.16.2       mjf 	/* SVGA aperture, we get the address from OpenFirmware */
    212  1.12.16.2       mjf 	if (vga_reg[10] == 0)
    213  1.12.16.2       mjf 		return -1;
    214  1.12.16.2       mjf 
    215  1.12.16.2       mjf 	if ((offset >= vga_reg[10]) &&
    216  1.12.16.2       mjf 	    (offset < (vga_reg[10] + vga_reg[11]))) {
    217  1.12.16.2       mjf 
    218  1.12.16.2       mjf 		return arm_btop(isa_mem_physaddr + offset);
    219  1.12.16.2       mjf 	}
    220  1.12.16.2       mjf 
    221  1.12.16.2       mjf 	return -1;
    222  1.12.16.2       mjf }
    223