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