bcm2835_genfb.c revision 1.6 1 /* $NetBSD: bcm2835_genfb.c,v 1.6 2014/07/24 21:35:12 riastradh 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.6 2014/07/24 21:35:12 riastradh 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_ddb_trap_callback(int);
65
66 static device_t bcmgenfb_console_dev = NULL;
67
68 CFATTACH_DECL_NEW(bcmgenfb, sizeof(struct bcmgenfb_softc),
69 bcmgenfb_match, bcmgenfb_attach, NULL, NULL);
70
71 static int
72 bcmgenfb_match(device_t parent, cfdata_t match, void *aux)
73 {
74 struct amba_attach_args *aaa = aux;
75
76 return !strcmp(aaa->aaa_name, "fb");
77 }
78
79 static void
80 bcmgenfb_attach(device_t parent, device_t self, void *aux)
81 {
82 struct bcmgenfb_softc *sc = device_private(self);
83 struct amba_attach_args *aaa = aux;
84 prop_dictionary_t dict = device_properties(self);
85 static const struct genfb_ops zero_ops;
86 struct genfb_ops ops = zero_ops;
87 bool is_console = false;
88 int error;
89
90 sc->sc_gen.sc_dev = self;
91 sc->sc_iot = aaa->aaa_iot;
92
93 sc->sc_wstype = WSDISPLAY_TYPE_VC4;
94 prop_dictionary_get_uint32(dict, "wsdisplay_type", &sc->sc_wstype);
95 prop_dictionary_get_bool(dict, "is_console", &is_console);
96
97 genfb_init(&sc->sc_gen);
98
99 if (sc->sc_gen.sc_width == 0 ||
100 sc->sc_gen.sc_fbsize == 0) {
101 aprint_normal(": disabled\n");
102 return;
103 }
104
105 pmf_device_register1(self, NULL, NULL, bcmgenfb_shutdown);
106
107 error = bus_space_map(sc->sc_iot, sc->sc_gen.sc_fboffset,
108 sc->sc_gen.sc_fbsize,
109 BUS_SPACE_MAP_LINEAR|BUS_SPACE_MAP_PREFETCHABLE, &sc->sc_ioh);
110 if (error) {
111 aprint_error_dev(self, "couldn't map framebuffer (%d)\n",
112 error);
113 return;
114 }
115 sc->sc_gen.sc_fbaddr = bus_space_vaddr(sc->sc_iot, sc->sc_ioh);
116
117 ops.genfb_ioctl = bcmgenfb_ioctl;
118 ops.genfb_mmap = bcmgenfb_mmap;
119
120 aprint_naive("\n");
121
122 if (is_console)
123 aprint_normal(": switching to framebuffer console\n");
124 else
125 aprint_normal("\n");
126
127 genfb_attach(&sc->sc_gen, &ops);
128 }
129
130 static int
131 bcmgenfb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag, lwp_t *l)
132 {
133 struct bcmgenfb_softc *sc = v;
134 struct wsdisplayio_bus_id *busid;
135
136 switch (cmd) {
137 case WSDISPLAYIO_GTYPE:
138 *(u_int *)data = sc->sc_wstype;
139 return 0;
140 case WSDISPLAYIO_GET_BUSID:
141 busid = data;
142 busid->bus_type = WSDISPLAYIO_BUS_SOC;
143 return 0;
144 case WSDISPLAYIO_GET_FBINFO:
145 {
146 struct wsdisplayio_fbinfo *fbi = data;
147 struct rasops_info *ri = &sc->sc_gen.vd.active->scr_ri;
148 int ret;
149
150 ret = wsdisplayio_get_fbinfo(ri, fbi);
151 fbi->fbi_flags |= WSFB_VRAM_IS_RAM;
152 return ret;
153 }
154 default:
155 return EPASSTHROUGH;
156 }
157 }
158
159 static paddr_t
160 bcmgenfb_mmap(void *v, void *vs, off_t offset, int prot)
161 {
162 struct bcmgenfb_softc *sc = v;
163
164 if (offset < 0 || offset >= sc->sc_gen.sc_fbsize)
165 return -1;
166
167 return bus_space_mmap(sc->sc_iot, sc->sc_gen.sc_fboffset, offset,
168 prot, BUS_SPACE_MAP_LINEAR|BUS_SPACE_MAP_PREFETCHABLE);
169 }
170
171 static bool
172 bcmgenfb_shutdown(device_t self, int flags)
173 {
174 genfb_enable_polling(self);
175 return true;
176 }
177 void
178 bcmgenfb_set_console_dev(device_t dev)
179 {
180 KASSERT(bcmgenfb_console_dev == NULL);
181 bcmgenfb_console_dev = dev;
182 }
183
184 void
185 bcmgenfb_ddb_trap_callback(int where)
186 {
187 if (bcmgenfb_console_dev == NULL)
188 return;
189
190 if (where) {
191 genfb_enable_polling(bcmgenfb_console_dev);
192 } else {
193 genfb_disable_polling(bcmgenfb_console_dev);
194 }
195 }
196