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