Home | History | Annotate | Line # | Download | only in ofw
vga_ofbus.c revision 1.17
      1 /* $NetBSD: vga_ofbus.c,v 1.17 2021/01/27 03:10:21 thorpej 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 (at) 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_ofbus.c,v 1.17 2021/01/27 03:10:21 thorpej Exp $");
     32 
     33 #include <sys/param.h>
     34 #include <sys/systm.h>
     35 #include <sys/kernel.h>
     36 #include <sys/device.h>
     37 #include <sys/malloc.h>
     38 #include <sys/proc.h>
     39 #include <sys/kauth.h>
     40 
     41 #include <dev/isa/isavar.h>
     42 
     43 #include <dev/ic/mc6845reg.h>
     44 #include <dev/ic/pcdisplayvar.h>
     45 #include <dev/ic/vgareg.h>
     46 #include <dev/ic/vgavar.h>
     47 
     48 #include <dev/wscons/wsconsio.h>
     49 #include <dev/wscons/wsdisplayvar.h>
     50 
     51 #include <dev/ofw/openfirm.h>
     52 
     53 #include <shark/ofw/vga_ofbusvar.h>
     54 
     55 struct vga_ofbus_softc {
     56 	struct vga_softc sc_vga;
     57 
     58 	int sc_phandle;
     59 };
     60 
     61 #if (NIGSFB_OFBUS > 0) || (NVGA_OFBUS > 0)
     62 extern int console_ihandle;
     63 #endif
     64 
     65 int	vga_ofbus_match (device_t, cfdata_t, void *);
     66 void	vga_ofbus_attach (device_t, device_t, void *);
     67 
     68 CFATTACH_DECL_NEW(vga_ofbus, sizeof(struct vga_ofbus_softc),
     69     vga_ofbus_match, vga_ofbus_attach, NULL, NULL);
     70 
     71 static const struct device_compatible_entry compat_data[] = {
     72 	{ .compat = "pnpPNP,900" },
     73 	DEVICE_COMPAT_EOL
     74 };
     75 
     76 static	int vga_ofbus_ioctl(void *, u_long, void *, int, struct lwp *);
     77 static	paddr_t vga_ofbus_mmap(void *, off_t, int);
     78 
     79 static const struct vga_funcs vga_ofbus_funcs = {
     80 	vga_ofbus_ioctl,
     81 	vga_ofbus_mmap
     82 };
     83 static uint32_t vga_reg[12];
     84 extern paddr_t isa_io_physaddr, isa_mem_physaddr;
     85 
     86 int
     87 vga_ofbus_match(device_t parent, cfdata_t match, void *aux)
     88 {
     89 	struct ofbus_attach_args *oba = aux;
     90 
     91 	if (!of_compatible_match(oba->oba_phandle, compat_data))
     92 		return (0);
     93 
     94 	if (!vga_is_console(&isa_io_bs_tag, WSDISPLAY_TYPE_ISAVGA) &&
     95 	    !vga_common_probe(&isa_io_bs_tag, &isa_mem_bs_tag))
     96 		return (0);
     97 
     98 	return (2);	/* more than generic pcdisplay */
     99 }
    100 
    101 void
    102 vga_ofbus_attach(device_t parent, device_t self, void *aux)
    103 {
    104 	struct vga_ofbus_softc *osc = device_private(self);
    105 	struct vga_softc *sc = &osc->sc_vga;
    106 	struct ofbus_attach_args *oba = aux;
    107 	int vga_handle, i;
    108 
    109 	sc->sc_dev = self;
    110 	aprint_normal("\n");
    111 	osc->sc_phandle = oba->oba_phandle;
    112 
    113 	vga_handle = OF_finddevice("/vlbus/display");
    114 	OF_getprop(vga_handle, "reg", vga_reg, sizeof(vga_reg));
    115 
    116 	/* for some idiotic reason we get this in the wrong byte order */
    117 	for (i = 0; i < 12; i++) {
    118 		vga_reg[i] = be32toh(vga_reg[i]);
    119 		aprint_debug_dev(self, "vga_reg[%2d] = 0x%08x\n",
    120 		    i, vga_reg[i]);
    121 	}
    122 
    123 	vga_common_attach(sc, &isa_io_bs_tag, &isa_mem_bs_tag,
    124 	    WSDISPLAY_TYPE_ISAVGA, 0, &vga_ofbus_funcs);
    125 	if (vga_reg[10] > 0) {
    126 		aprint_normal_dev(self, "aperture at 0x%08x\n", vga_reg[10]);
    127 	}
    128 }
    129 
    130 int
    131 vga_ofbus_cnattach(bus_space_tag_t iot, bus_space_tag_t memt)
    132 {
    133 	int chosen_phandle;
    134 	int stdout_ihandle, stdout_phandle, ret;
    135 	char buf[128];
    136 
    137 	stdout_phandle = 0;
    138 
    139 	/*
    140 	 * Find out whether the firmware's chosen stdout is
    141 	 * a display.  If so, use the existing ihandle so the firmware
    142 	 * doesn't become Unhappy.  If not, just open it.
    143 	 */
    144 	if ((chosen_phandle = OF_finddevice("/chosen")) == -1 ||
    145 	    OF_getprop(chosen_phandle, "stdout", &stdout_ihandle,
    146 	    sizeof(stdout_ihandle)) != sizeof(stdout_ihandle)) {
    147 		return ENXIO;
    148 	}
    149 	stdout_ihandle = of_decode_int((unsigned char *)&stdout_ihandle);
    150 	if ((stdout_phandle = OF_instance_to_package(stdout_ihandle)) == -1 ||
    151 	    OF_getprop(stdout_phandle, "device_type", buf, sizeof(buf)) <= 0) {
    152 		return ENXIO;
    153 	}
    154 
    155 	if (strcmp(buf, "display") != 0) {
    156 		/* The display is not stdout device. */
    157 		return ENXIO;
    158 	}
    159 
    160 	if (OF_call_method("text-mode3", stdout_ihandle, 0, 0) != 0) {
    161 		printf("vga_ofbus_match: text-mode3 method invocation on VGA "
    162 		       "screen device failed\n");
    163 	}
    164 
    165 	ret = vga_cnattach(iot, memt, WSDISPLAY_TYPE_ISAVGA, 1);
    166 #if (NIGSFB_OFBUS > 0) || (NVGA_OFBUS > 0)
    167 	if (ret == 0)
    168 		console_ihandle = stdout_ihandle;
    169 #endif
    170 
    171 	return ret;
    172 }
    173 
    174 static int
    175 vga_ofbus_ioctl(void *cookie, u_long cmd, void *data, int flag, struct lwp *l)
    176 {
    177 	/* we should catch WSDISPLAYIO_SMODE here */
    178 	return 0;
    179 }
    180 
    181 static paddr_t
    182 vga_ofbus_mmap(void *cookie, off_t offset, int prot)
    183 {
    184 
    185 	/* only the superuser may mmap IO and aperture */
    186 	if (curlwp != NULL) {
    187 		if (kauth_authorize_machdep(kauth_cred_get(),
    188 		    KAUTH_MACHDEP_UNMANAGEDMEM, NULL, NULL, NULL, NULL) != 0) {
    189 			return -1;
    190 		}
    191 	}
    192 
    193 	/*
    194 	 * XXX
    195 	 * we should really use bus_space_mmap here but for some reason
    196 	 * the ISA tags contain kernel virtual addresses and translating them
    197 	 * back to physical addresses doesn't really work
    198 	 */
    199 	/* access to IO ports */
    200 	if ((offset >= PCI_MAGIC_IO_RANGE) &&
    201 	    (offset < (PCI_MAGIC_IO_RANGE + 0x10000))) {
    202 		paddr_t pa;
    203 
    204 		pa = isa_io_physaddr + offset - PCI_MAGIC_IO_RANGE;
    205 		return arm_btop(pa);
    206 	}
    207 
    208 	/* legacy VGA aperture */
    209 	if ((offset >= 0xa0000) && (offset < 0xc0000)) {
    210 
    211 		return arm_btop(isa_mem_physaddr + offset);
    212 	}
    213 
    214 	/* SVGA aperture, we get the address from OpenFirmware */
    215 	if (vga_reg[10] == 0)
    216 		return -1;
    217 
    218 	if ((offset >= vga_reg[10]) &&
    219 	    (offset < (vga_reg[10] + vga_reg[11]))) {
    220 
    221 		return arm_btop(isa_mem_physaddr + offset);
    222 	}
    223 
    224 	return -1;
    225 }
    226