1*e5fbc36aSthorpej /* $NetBSD: vga_ofbus.c,v 1.18 2023/12/20 15:34:45 thorpej Exp $ */
218c88948Sthorpej
318c88948Sthorpej /*
418c88948Sthorpej * Copyright (c) 1995, 1996 Carnegie-Mellon University.
518c88948Sthorpej * All rights reserved.
618c88948Sthorpej *
718c88948Sthorpej * Author: Chris G. Demetriou
818c88948Sthorpej *
918c88948Sthorpej * Permission to use, copy, modify and distribute this software and
1018c88948Sthorpej * its documentation is hereby granted, provided that both the copyright
1118c88948Sthorpej * notice and this permission notice appear in all copies of the
1218c88948Sthorpej * software, derivative works or modified versions, and any portions
1318c88948Sthorpej * thereof, and that both notices appear in supporting documentation.
1418c88948Sthorpej *
1518c88948Sthorpej * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
1618c88948Sthorpej * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
1718c88948Sthorpej * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
1818c88948Sthorpej *
1918c88948Sthorpej * Carnegie Mellon requests users of this software to return to
2018c88948Sthorpej *
2118c88948Sthorpej * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
2218c88948Sthorpej * School of Computer Science
2318c88948Sthorpej * Carnegie Mellon University
2418c88948Sthorpej * Pittsburgh PA 15213-3890
2518c88948Sthorpej *
2618c88948Sthorpej * any improvements or extensions that they make and grant Carnegie the
2718c88948Sthorpej * rights to redistribute these changes.
2818c88948Sthorpej */
2918c88948Sthorpej
30ed517291Slukem #include <sys/cdefs.h>
31*e5fbc36aSthorpej __KERNEL_RCSID(0, "$NetBSD: vga_ofbus.c,v 1.18 2023/12/20 15:34:45 thorpej Exp $");
32ed517291Slukem
3318c88948Sthorpej #include <sys/param.h>
3418c88948Sthorpej #include <sys/systm.h>
3518c88948Sthorpej #include <sys/kernel.h>
3618c88948Sthorpej #include <sys/device.h>
3794959c08Smacallan #include <sys/proc.h>
3894959c08Smacallan #include <sys/kauth.h>
3918c88948Sthorpej
4018c88948Sthorpej #include <dev/isa/isavar.h>
4118c88948Sthorpej
4218c88948Sthorpej #include <dev/ic/mc6845reg.h>
4318c88948Sthorpej #include <dev/ic/pcdisplayvar.h>
4418c88948Sthorpej #include <dev/ic/vgareg.h>
4518c88948Sthorpej #include <dev/ic/vgavar.h>
4618c88948Sthorpej
4718c88948Sthorpej #include <dev/wscons/wsconsio.h>
4818c88948Sthorpej #include <dev/wscons/wsdisplayvar.h>
4918c88948Sthorpej
5018c88948Sthorpej #include <dev/ofw/openfirm.h>
517da0e843Stsutsui
527da0e843Stsutsui #include <shark/ofw/vga_ofbusvar.h>
5318c88948Sthorpej
5418c88948Sthorpej struct vga_ofbus_softc {
5518c88948Sthorpej struct vga_softc sc_vga;
5618c88948Sthorpej
5718c88948Sthorpej int sc_phandle;
5818c88948Sthorpej };
5918c88948Sthorpej
60d749362bSjmmv #if (NIGSFB_OFBUS > 0) || (NVGA_OFBUS > 0)
612b8bf5f8Smacallan extern int console_ihandle;
625575290bSjmmv #endif
632b8bf5f8Smacallan
64d293cfcaScube int vga_ofbus_match (device_t, cfdata_t, void *);
65d293cfcaScube void vga_ofbus_attach (device_t, device_t, void *);
6618c88948Sthorpej
67d293cfcaScube CFATTACH_DECL_NEW(vga_ofbus, sizeof(struct vga_ofbus_softc),
6889bf5a8fSthorpej vga_ofbus_match, vga_ofbus_attach, NULL, NULL);
6918c88948Sthorpej
706e54367aSthorpej static const struct device_compatible_entry compat_data[] = {
716e54367aSthorpej { .compat = "pnpPNP,900" },
726e54367aSthorpej DEVICE_COMPAT_EOL
736e54367aSthorpej };
7418c88948Sthorpej
7594959c08Smacallan static int vga_ofbus_ioctl(void *, u_long, void *, int, struct lwp *);
7694959c08Smacallan static paddr_t vga_ofbus_mmap(void *, off_t, int);
7794959c08Smacallan
7894959c08Smacallan static const struct vga_funcs vga_ofbus_funcs = {
7994959c08Smacallan vga_ofbus_ioctl,
8094959c08Smacallan vga_ofbus_mmap
8194959c08Smacallan };
8294959c08Smacallan static uint32_t vga_reg[12];
8394959c08Smacallan extern paddr_t isa_io_physaddr, isa_mem_physaddr;
8494959c08Smacallan
8518c88948Sthorpej int
vga_ofbus_match(device_t parent,cfdata_t match,void * aux)86d293cfcaScube vga_ofbus_match(device_t parent, cfdata_t match, void *aux)
8718c88948Sthorpej {
8818c88948Sthorpej struct ofbus_attach_args *oba = aux;
8918c88948Sthorpej
906e54367aSthorpej if (!of_compatible_match(oba->oba_phandle, compat_data))
9118c88948Sthorpej return (0);
9218c88948Sthorpej
9318c88948Sthorpej if (!vga_is_console(&isa_io_bs_tag, WSDISPLAY_TYPE_ISAVGA) &&
9418c88948Sthorpej !vga_common_probe(&isa_io_bs_tag, &isa_mem_bs_tag))
9518c88948Sthorpej return (0);
9618c88948Sthorpej
9718c88948Sthorpej return (2); /* more than generic pcdisplay */
9818c88948Sthorpej }
9918c88948Sthorpej
10018c88948Sthorpej void
vga_ofbus_attach(device_t parent,device_t self,void * aux)101d293cfcaScube vga_ofbus_attach(device_t parent, device_t self, void *aux)
10218c88948Sthorpej {
103d293cfcaScube struct vga_ofbus_softc *osc = device_private(self);
10418c88948Sthorpej struct vga_softc *sc = &osc->sc_vga;
10518c88948Sthorpej struct ofbus_attach_args *oba = aux;
10694959c08Smacallan int vga_handle, i;
10718c88948Sthorpej
108d293cfcaScube sc->sc_dev = self;
109d293cfcaScube aprint_normal("\n");
11018c88948Sthorpej osc->sc_phandle = oba->oba_phandle;
11118c88948Sthorpej
11294959c08Smacallan vga_handle = OF_finddevice("/vlbus/display");
11394959c08Smacallan OF_getprop(vga_handle, "reg", vga_reg, sizeof(vga_reg));
11494959c08Smacallan
11594959c08Smacallan /* for some idiotic reason we get this in the wrong byte order */
11694959c08Smacallan for (i = 0; i < 12; i++) {
11794959c08Smacallan vga_reg[i] = be32toh(vga_reg[i]);
118a371a943Stsutsui aprint_debug_dev(self, "vga_reg[%2d] = 0x%08x\n",
119a371a943Stsutsui i, vga_reg[i]);
12094959c08Smacallan }
12194959c08Smacallan
12218c88948Sthorpej vga_common_attach(sc, &isa_io_bs_tag, &isa_mem_bs_tag,
12394959c08Smacallan WSDISPLAY_TYPE_ISAVGA, 0, &vga_ofbus_funcs);
12494959c08Smacallan if (vga_reg[10] > 0) {
125a371a943Stsutsui aprint_normal_dev(self, "aperture at 0x%08x\n", vga_reg[10]);
12694959c08Smacallan }
12718c88948Sthorpej }
12818c88948Sthorpej
12918c88948Sthorpej int
vga_ofbus_cnattach(bus_space_tag_t iot,bus_space_tag_t memt)1307da0e843Stsutsui vga_ofbus_cnattach(bus_space_tag_t iot, bus_space_tag_t memt)
13118c88948Sthorpej {
1327da0e843Stsutsui int chosen_phandle;
1332b8bf5f8Smacallan int stdout_ihandle, stdout_phandle, ret;
1347da0e843Stsutsui char buf[128];
1357da0e843Stsutsui
1367da0e843Stsutsui stdout_phandle = 0;
1377da0e843Stsutsui
1387da0e843Stsutsui /*
1397da0e843Stsutsui * Find out whether the firmware's chosen stdout is
1407da0e843Stsutsui * a display. If so, use the existing ihandle so the firmware
1417da0e843Stsutsui * doesn't become Unhappy. If not, just open it.
1427da0e843Stsutsui */
1437da0e843Stsutsui if ((chosen_phandle = OF_finddevice("/chosen")) == -1 ||
1447da0e843Stsutsui OF_getprop(chosen_phandle, "stdout", &stdout_ihandle,
1457da0e843Stsutsui sizeof(stdout_ihandle)) != sizeof(stdout_ihandle)) {
146f0586216Stsutsui return ENXIO;
1477da0e843Stsutsui }
1487da0e843Stsutsui stdout_ihandle = of_decode_int((unsigned char *)&stdout_ihandle);
1497da0e843Stsutsui if ((stdout_phandle = OF_instance_to_package(stdout_ihandle)) == -1 ||
1507da0e843Stsutsui OF_getprop(stdout_phandle, "device_type", buf, sizeof(buf)) <= 0) {
151f0586216Stsutsui return ENXIO;
1527da0e843Stsutsui }
1537da0e843Stsutsui
1547da0e843Stsutsui if (strcmp(buf, "display") != 0) {
1557da0e843Stsutsui /* The display is not stdout device. */
156f0586216Stsutsui return ENXIO;
1577da0e843Stsutsui }
1587da0e843Stsutsui
1597da0e843Stsutsui if (OF_call_method("text-mode3", stdout_ihandle, 0, 0) != 0) {
16018c88948Sthorpej printf("vga_ofbus_match: text-mode3 method invocation on VGA "
16118c88948Sthorpej "screen device failed\n");
16218c88948Sthorpej }
16318c88948Sthorpej
1642b8bf5f8Smacallan ret = vga_cnattach(iot, memt, WSDISPLAY_TYPE_ISAVGA, 1);
165d749362bSjmmv #if (NIGSFB_OFBUS > 0) || (NVGA_OFBUS > 0)
1662b8bf5f8Smacallan if (ret == 0)
1672b8bf5f8Smacallan console_ihandle = stdout_ihandle;
1685575290bSjmmv #endif
1692b8bf5f8Smacallan
1702b8bf5f8Smacallan return ret;
17118c88948Sthorpej }
17294959c08Smacallan
17394959c08Smacallan static int
vga_ofbus_ioctl(void * cookie,u_long cmd,void * data,int flag,struct lwp * l)17494959c08Smacallan vga_ofbus_ioctl(void *cookie, u_long cmd, void *data, int flag, struct lwp *l)
17594959c08Smacallan {
17694959c08Smacallan /* we should catch WSDISPLAYIO_SMODE here */
17794959c08Smacallan return 0;
17894959c08Smacallan }
17994959c08Smacallan
18094959c08Smacallan static paddr_t
vga_ofbus_mmap(void * cookie,off_t offset,int prot)18194959c08Smacallan vga_ofbus_mmap(void *cookie, off_t offset, int prot)
18294959c08Smacallan {
18394959c08Smacallan
18494959c08Smacallan /* only the superuser may mmap IO and aperture */
18594959c08Smacallan if (curlwp != NULL) {
1860c9d8d15Selad if (kauth_authorize_machdep(kauth_cred_get(),
1870c9d8d15Selad KAUTH_MACHDEP_UNMANAGEDMEM, NULL, NULL, NULL, NULL) != 0) {
18894959c08Smacallan return -1;
18994959c08Smacallan }
19094959c08Smacallan }
19194959c08Smacallan
19294959c08Smacallan /*
19394959c08Smacallan * XXX
19494959c08Smacallan * we should really use bus_space_mmap here but for some reason
19594959c08Smacallan * the ISA tags contain kernel virtual addresses and translating them
19694959c08Smacallan * back to physical addresses doesn't really work
19794959c08Smacallan */
19894959c08Smacallan /* access to IO ports */
19994959c08Smacallan if ((offset >= PCI_MAGIC_IO_RANGE) &&
20094959c08Smacallan (offset < (PCI_MAGIC_IO_RANGE + 0x10000))) {
20194959c08Smacallan paddr_t pa;
20294959c08Smacallan
20394959c08Smacallan pa = isa_io_physaddr + offset - PCI_MAGIC_IO_RANGE;
20494959c08Smacallan return arm_btop(pa);
20594959c08Smacallan }
20694959c08Smacallan
20794959c08Smacallan /* legacy VGA aperture */
20894959c08Smacallan if ((offset >= 0xa0000) && (offset < 0xc0000)) {
20994959c08Smacallan
21094959c08Smacallan return arm_btop(isa_mem_physaddr + offset);
21194959c08Smacallan }
21294959c08Smacallan
21394959c08Smacallan /* SVGA aperture, we get the address from OpenFirmware */
21494959c08Smacallan if (vga_reg[10] == 0)
21594959c08Smacallan return -1;
21694959c08Smacallan
21794959c08Smacallan if ((offset >= vga_reg[10]) &&
21894959c08Smacallan (offset < (vga_reg[10] + vga_reg[11]))) {
21994959c08Smacallan
22094959c08Smacallan return arm_btop(isa_mem_physaddr + offset);
22194959c08Smacallan }
22294959c08Smacallan
22394959c08Smacallan return -1;
22494959c08Smacallan }
225