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