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