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