bcm2835_genfb.c revision 1.7 1 /* $NetBSD: bcm2835_genfb.c,v 1.7 2014/09/05 21:15:42 macallan 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.7 2014/09/05 21:15:42 macallan Exp $");
35
36 #include <sys/param.h>
37 #include <sys/types.h>
38 #include <sys/systm.h>
39 #include <sys/device.h>
40 #include <sys/conf.h>
41 #include <sys/bus.h>
42 #include <sys/kmem.h>
43
44 #include <arm/broadcom/bcm_amba.h>
45
46 #include <dev/wsfb/genfbvar.h>
47
48 struct bcmgenfb_softc {
49 struct genfb_softc sc_gen;
50 bus_space_tag_t sc_iot;
51 bus_space_handle_t sc_ioh;
52
53 uint32_t sc_wstype;
54 };
55
56 static int bcmgenfb_match(device_t, cfdata_t, void *);
57 static void bcmgenfb_attach(device_t, device_t, void *);
58
59 static int bcmgenfb_ioctl(void *, void *, u_long, void *, int, lwp_t *);
60 static paddr_t bcmgenfb_mmap(void *, void *, off_t, int);
61 static bool bcmgenfb_shutdown(device_t, int);
62
63 void bcmgenfb_set_console_dev(device_t);
64 void bcmgenfb_set_ioctl(int(*)(void *, void *, u_long, void *, int, struct lwp *));
65 void bcmgenfb_ddb_trap_callback(int);
66
67 static device_t bcmgenfb_console_dev = NULL;
68 int (*bcmgenfb_ioctl_handler)(void *, void *, u_long, void *, int, struct lwp *) = NULL;
69
70 CFATTACH_DECL_NEW(bcmgenfb, sizeof(struct bcmgenfb_softc),
71 bcmgenfb_match, bcmgenfb_attach, NULL, NULL);
72
73 static int
74 bcmgenfb_match(device_t parent, cfdata_t match, void *aux)
75 {
76 struct amba_attach_args *aaa = aux;
77
78 return !strcmp(aaa->aaa_name, "fb");
79 }
80
81 static void
82 bcmgenfb_attach(device_t parent, device_t self, void *aux)
83 {
84 struct bcmgenfb_softc *sc = device_private(self);
85 struct amba_attach_args *aaa = aux;
86 prop_dictionary_t dict = device_properties(self);
87 static const struct genfb_ops zero_ops;
88 struct genfb_ops ops = zero_ops;
89 bool is_console = false;
90 int error;
91
92 sc->sc_gen.sc_dev = self;
93 sc->sc_iot = aaa->aaa_iot;
94
95 sc->sc_wstype = WSDISPLAY_TYPE_VC4;
96 prop_dictionary_get_uint32(dict, "wsdisplay_type", &sc->sc_wstype);
97 prop_dictionary_get_bool(dict, "is_console", &is_console);
98
99 genfb_init(&sc->sc_gen);
100
101 if (sc->sc_gen.sc_width == 0 ||
102 sc->sc_gen.sc_fbsize == 0) {
103 aprint_normal(": disabled\n");
104 return;
105 }
106
107 pmf_device_register1(self, NULL, NULL, bcmgenfb_shutdown);
108
109 error = bus_space_map(sc->sc_iot, sc->sc_gen.sc_fboffset,
110 sc->sc_gen.sc_fbsize,
111 BUS_SPACE_MAP_LINEAR|BUS_SPACE_MAP_PREFETCHABLE, &sc->sc_ioh);
112 if (error) {
113 aprint_error_dev(self, "couldn't map framebuffer (%d)\n",
114 error);
115 return;
116 }
117 sc->sc_gen.sc_fbaddr = bus_space_vaddr(sc->sc_iot, sc->sc_ioh);
118
119 ops.genfb_ioctl = bcmgenfb_ioctl;
120 ops.genfb_mmap = bcmgenfb_mmap;
121
122 aprint_naive("\n");
123
124 if (is_console)
125 aprint_normal(": switching to framebuffer console\n");
126 else
127 aprint_normal("\n");
128
129 genfb_attach(&sc->sc_gen, &ops);
130 }
131
132 static int
133 bcmgenfb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag, lwp_t *l)
134 {
135 struct bcmgenfb_softc *sc = v;
136 struct wsdisplayio_bus_id *busid;
137
138 switch (cmd) {
139 case WSDISPLAYIO_GTYPE:
140 *(u_int *)data = sc->sc_wstype;
141 return 0;
142 case WSDISPLAYIO_GET_BUSID:
143 busid = data;
144 busid->bus_type = WSDISPLAYIO_BUS_SOC;
145 return 0;
146 case WSDISPLAYIO_GET_FBINFO:
147 {
148 struct wsdisplayio_fbinfo *fbi = data;
149 struct rasops_info *ri = &sc->sc_gen.vd.active->scr_ri;
150 int ret;
151
152 ret = wsdisplayio_get_fbinfo(ri, fbi);
153 fbi->fbi_flags |= WSFB_VRAM_IS_RAM;
154 return ret;
155 }
156 default:
157 if (bcmgenfb_ioctl_handler != NULL)
158 return bcmgenfb_ioctl_handler(v, vs, cmd, data, flag, l);
159 return EPASSTHROUGH;
160 }
161 }
162
163 static paddr_t
164 bcmgenfb_mmap(void *v, void *vs, off_t offset, int prot)
165 {
166 struct bcmgenfb_softc *sc = v;
167
168 if (offset < 0 || offset >= sc->sc_gen.sc_fbsize)
169 return -1;
170
171 return bus_space_mmap(sc->sc_iot, sc->sc_gen.sc_fboffset, offset,
172 prot, BUS_SPACE_MAP_LINEAR|BUS_SPACE_MAP_PREFETCHABLE);
173 }
174
175 static bool
176 bcmgenfb_shutdown(device_t self, int flags)
177 {
178 genfb_enable_polling(self);
179 return true;
180 }
181 void
182 bcmgenfb_set_console_dev(device_t dev)
183 {
184 KASSERT(bcmgenfb_console_dev == NULL);
185 bcmgenfb_console_dev = dev;
186 }
187
188 void
189 bcmgenfb_set_ioctl(int(*boo)(void *, void *, u_long, void *, int, struct lwp *))
190 {
191 bcmgenfb_ioctl_handler = boo;
192 }
193
194 void
195 bcmgenfb_ddb_trap_callback(int where)
196 {
197 if (bcmgenfb_console_dev == NULL)
198 return;
199
200 if (where) {
201 genfb_enable_polling(bcmgenfb_console_dev);
202 } else {
203 genfb_disable_polling(bcmgenfb_console_dev);
204 }
205 }
206