bcm2835_genfb.c revision 1.11 1 /* $NetBSD: bcm2835_genfb.c,v 1.11 2020/12/01 04:17:10 rin Exp $ */
2
3 /*-
4 * Copyright (c) 2013 Jared D. McNeill <jmcneill (at) invisible.ca>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 /*
30 * Generic framebuffer console driver
31 */
32
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: bcm2835_genfb.c,v 1.11 2020/12/01 04:17:10 rin Exp $");
35
36 #include <sys/param.h>
37 #include <sys/types.h>
38 #include <sys/bus.h>
39 #include <sys/conf.h>
40 #include <sys/device.h>
41 #include <sys/endian.h>
42 #include <sys/kmem.h>
43 #include <sys/systm.h>
44
45 #include <dev/fdt/fdtvar.h>
46
47 #include <dev/wsfb/genfbvar.h>
48
49 struct bcmgenfb_softc {
50 struct genfb_softc sc_gen;
51 bus_space_tag_t sc_iot;
52 bus_space_handle_t sc_ioh;
53
54 uint32_t sc_wstype;
55 };
56
57 static int bcmgenfb_match(device_t, cfdata_t, void *);
58 static void bcmgenfb_attach(device_t, device_t, void *);
59
60 static int bcmgenfb_ioctl(void *, void *, u_long, void *, int, lwp_t *);
61 static paddr_t bcmgenfb_mmap(void *, void *, off_t, int);
62 static bool bcmgenfb_shutdown(device_t, int);
63
64 void bcmgenfb_set_console_dev(device_t);
65 void bcmgenfb_set_ioctl(int(*)(void *, void *, u_long, void *, int, struct lwp *));
66 void bcmgenfb_ddb_trap_callback(int);
67
68 static device_t bcmgenfb_console_dev = NULL;
69 int (*bcmgenfb_ioctl_handler)(void *, void *, u_long, void *, int, struct lwp *) = NULL;
70
71 CFATTACH_DECL_NEW(bcmgenfb, sizeof(struct bcmgenfb_softc),
72 bcmgenfb_match, bcmgenfb_attach, NULL, NULL);
73
74 static int
75 bcmgenfb_match(device_t parent, cfdata_t match, void *aux)
76 {
77 const char * const compatible[] = { "brcm,bcm2835-fb", NULL };
78 struct fdt_attach_args * const faa = aux;
79
80 return of_match_compatible(faa->faa_phandle, compatible);
81 }
82
83 static void
84 bcmgenfb_attach(device_t parent, device_t self, void *aux)
85 {
86 struct bcmgenfb_softc *sc = device_private(self);
87 struct fdt_attach_args * const faa = aux;
88 prop_dictionary_t dict = device_properties(self);
89 static const struct genfb_ops zero_ops;
90 struct genfb_ops ops = zero_ops;
91 bool is_console = false;
92 int error;
93
94 sc->sc_gen.sc_dev = self;
95 sc->sc_iot = faa->faa_bst;
96
97 sc->sc_wstype = WSDISPLAY_TYPE_VC4;
98 prop_dictionary_get_uint32(dict, "wsdisplay_type", &sc->sc_wstype);
99 prop_dictionary_get_bool(dict, "is_console", &is_console);
100 #if BYTE_ORDER == BIG_ENDIAN
101 prop_dictionary_set_bool(dict, "is_swapped", true);
102 #endif
103
104 genfb_init(&sc->sc_gen);
105
106 if (sc->sc_gen.sc_width == 0 ||
107 sc->sc_gen.sc_fbsize == 0) {
108 aprint_normal(": disabled\n");
109 return;
110 }
111
112 pmf_device_register1(self, NULL, NULL, bcmgenfb_shutdown);
113
114 error = bus_space_map(sc->sc_iot, sc->sc_gen.sc_fboffset,
115 sc->sc_gen.sc_fbsize,
116 BUS_SPACE_MAP_LINEAR|BUS_SPACE_MAP_PREFETCHABLE, &sc->sc_ioh);
117 if (error) {
118 aprint_error_dev(self, "couldn't map framebuffer (%d)\n",
119 error);
120 return;
121 }
122 sc->sc_gen.sc_fbaddr = bus_space_vaddr(sc->sc_iot, sc->sc_ioh);
123
124 ops.genfb_ioctl = bcmgenfb_ioctl;
125 ops.genfb_mmap = bcmgenfb_mmap;
126
127 aprint_naive("\n");
128
129 if (is_console)
130 aprint_normal(": switching to framebuffer console\n");
131 else
132 aprint_normal("\n");
133
134 genfb_attach(&sc->sc_gen, &ops);
135 }
136
137 static int
138 bcmgenfb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag, lwp_t *l)
139 {
140 struct bcmgenfb_softc *sc = v;
141 struct wsdisplayio_bus_id *busid;
142
143 switch (cmd) {
144 case WSDISPLAYIO_GTYPE:
145 *(u_int *)data = sc->sc_wstype;
146 return 0;
147 case WSDISPLAYIO_GET_BUSID:
148 busid = data;
149 busid->bus_type = WSDISPLAYIO_BUS_SOC;
150 return 0;
151 case WSDISPLAYIO_GET_FBINFO:
152 {
153 struct wsdisplayio_fbinfo *fbi = data;
154 struct rasops_info *ri = &sc->sc_gen.vd.active->scr_ri;
155 int ret;
156
157 ret = wsdisplayio_get_fbinfo(ri, fbi);
158 fbi->fbi_flags |= WSFB_VRAM_IS_RAM;
159 return ret;
160 }
161 default:
162 if (bcmgenfb_ioctl_handler != NULL)
163 return bcmgenfb_ioctl_handler(v, vs, cmd, data, flag, l);
164 return EPASSTHROUGH;
165 }
166 }
167
168 static paddr_t
169 bcmgenfb_mmap(void *v, void *vs, off_t offset, int prot)
170 {
171 struct bcmgenfb_softc *sc = v;
172
173 if (offset < 0 || offset >= sc->sc_gen.sc_fbsize)
174 return -1;
175
176 return bus_space_mmap(sc->sc_iot, sc->sc_gen.sc_fboffset, offset,
177 prot, BUS_SPACE_MAP_LINEAR|BUS_SPACE_MAP_PREFETCHABLE);
178 }
179
180 static bool
181 bcmgenfb_shutdown(device_t self, int flags)
182 {
183 genfb_enable_polling(self);
184 return true;
185 }
186 void
187 bcmgenfb_set_console_dev(device_t dev)
188 {
189 /* skip if already set. called from each genfb0,genfb1,... */
190 if (bcmgenfb_console_dev != NULL)
191 return;
192
193 bcmgenfb_console_dev = dev;
194 }
195
196 void
197 bcmgenfb_set_ioctl(int(*boo)(void *, void *, u_long, void *, int, struct lwp *))
198 {
199 bcmgenfb_ioctl_handler = boo;
200 }
201
202 void
203 bcmgenfb_ddb_trap_callback(int where)
204 {
205 if (bcmgenfb_console_dev == NULL)
206 return;
207
208 if (where) {
209 genfb_enable_polling(bcmgenfb_console_dev);
210 } else {
211 genfb_disable_polling(bcmgenfb_console_dev);
212 }
213 }
214