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