summaryrefslogtreecommitdiff
path: root/sys/arch/arm/broadcom/bcm2835_genfb.c
blob: b38b70ecbab0a8033e93108820d695a200683dea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/* $NetBSD: bcm2835_genfb.c,v 1.12 2021/01/27 03:10:19 thorpej Exp $ */

/*-
 * Copyright (c) 2013 Jared D. McNeill <jmcneill@invisible.ca>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

/*
 * Generic framebuffer console driver
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: bcm2835_genfb.c,v 1.12 2021/01/27 03:10:19 thorpej Exp $");

#include <sys/param.h>
#include <sys/types.h>
#include <sys/bus.h>
#include <sys/conf.h>
#include <sys/device.h>
#include <sys/endian.h>
#include <sys/kmem.h>
#include <sys/systm.h>

#include <dev/fdt/fdtvar.h>

#include <dev/wsfb/genfbvar.h>

struct bcmgenfb_softc {
	struct genfb_softc	sc_gen;
	bus_space_tag_t		sc_iot;
	bus_space_handle_t	sc_ioh;

	uint32_t		sc_wstype;
};

static int	bcmgenfb_match(device_t, cfdata_t, void *);
static void	bcmgenfb_attach(device_t, device_t, void *);

static int	bcmgenfb_ioctl(void *, void *, u_long, void *, int, lwp_t *);
static paddr_t	bcmgenfb_mmap(void *, void *, off_t, int);
static bool	bcmgenfb_shutdown(device_t, int);

void		bcmgenfb_set_console_dev(device_t);
void		bcmgenfb_set_ioctl(int(*)(void *, void *, u_long, void *, int, struct lwp *));
void		bcmgenfb_ddb_trap_callback(int);

static device_t bcmgenfb_console_dev = NULL;
int (*bcmgenfb_ioctl_handler)(void *, void *, u_long, void *, int, struct lwp *) = NULL;

CFATTACH_DECL_NEW(bcmgenfb, sizeof(struct bcmgenfb_softc),
    bcmgenfb_match, bcmgenfb_attach, NULL, NULL);

static const struct device_compatible_entry compat_data[] = {
	{ .compat = "brcm,bcm2835-fb" },
	DEVICE_COMPAT_EOL
};

static int
bcmgenfb_match(device_t parent, cfdata_t match, void *aux)
{
	struct fdt_attach_args * const faa = aux;

	return of_compatible_match(faa->faa_phandle, compat_data);
}

static void
bcmgenfb_attach(device_t parent, device_t self, void *aux)
{
	struct bcmgenfb_softc *sc = device_private(self);
	struct fdt_attach_args * const faa = aux;
	prop_dictionary_t dict = device_properties(self);
	static const struct genfb_ops zero_ops;
	struct genfb_ops ops = zero_ops;
	bool is_console = false;
	int error;

	sc->sc_gen.sc_dev = self;
	sc->sc_iot = faa->faa_bst;

	sc->sc_wstype = WSDISPLAY_TYPE_VC4;
	prop_dictionary_get_uint32(dict, "wsdisplay_type", &sc->sc_wstype);
	prop_dictionary_get_bool(dict, "is_console", &is_console);
#if BYTE_ORDER == BIG_ENDIAN
	prop_dictionary_set_bool(dict, "is_swapped", true);
#endif

	genfb_init(&sc->sc_gen);

	if (sc->sc_gen.sc_width == 0 ||
	    sc->sc_gen.sc_fbsize == 0) {
		aprint_normal(": disabled\n");
		return;
	}

	pmf_device_register1(self, NULL, NULL, bcmgenfb_shutdown);

	error = bus_space_map(sc->sc_iot, sc->sc_gen.sc_fboffset,
	    sc->sc_gen.sc_fbsize,
	    BUS_SPACE_MAP_LINEAR|BUS_SPACE_MAP_PREFETCHABLE, &sc->sc_ioh);
	if (error) {
		aprint_error_dev(self, "couldn't map framebuffer (%d)\n",
		    error);
		return;
	}
	sc->sc_gen.sc_fbaddr = bus_space_vaddr(sc->sc_iot, sc->sc_ioh);

	ops.genfb_ioctl = bcmgenfb_ioctl;
	ops.genfb_mmap = bcmgenfb_mmap;

	aprint_naive("\n");

	if (is_console)
		aprint_normal(": switching to framebuffer console\n");
	else
		aprint_normal("\n");

	genfb_attach(&sc->sc_gen, &ops);
}

static int
bcmgenfb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag, lwp_t *l)
{
	struct bcmgenfb_softc *sc = v;
	struct wsdisplayio_bus_id *busid;

	switch (cmd) {
	case WSDISPLAYIO_GTYPE:
		*(u_int *)data = sc->sc_wstype;
		return 0;
	case WSDISPLAYIO_GET_BUSID:
		busid = data;
		busid->bus_type = WSDISPLAYIO_BUS_SOC;
		return 0;
	case WSDISPLAYIO_GET_FBINFO:
		{
			struct wsdisplayio_fbinfo *fbi = data;
			struct rasops_info *ri = &sc->sc_gen.vd.active->scr_ri;
			int ret;

			ret = wsdisplayio_get_fbinfo(ri, fbi);
			fbi->fbi_flags |= WSFB_VRAM_IS_RAM;
			return ret;
		}
	default:
		if (bcmgenfb_ioctl_handler != NULL)
			return bcmgenfb_ioctl_handler(v, vs, cmd, data, flag, l);
		return EPASSTHROUGH;
	}
}

static paddr_t
bcmgenfb_mmap(void *v, void *vs, off_t offset, int prot)
{
	struct bcmgenfb_softc *sc = v;

	if (offset < 0 || offset >= sc->sc_gen.sc_fbsize)
		return -1;

	return bus_space_mmap(sc->sc_iot, sc->sc_gen.sc_fboffset, offset,
	    prot, BUS_SPACE_MAP_LINEAR|BUS_SPACE_MAP_PREFETCHABLE);
}

static bool
bcmgenfb_shutdown(device_t self, int flags)
{
	genfb_enable_polling(self);
	return true;
}
void
bcmgenfb_set_console_dev(device_t dev)
{
	/* skip if already set. called from each genfb0,genfb1,... */
	if (bcmgenfb_console_dev != NULL)
		return;

	bcmgenfb_console_dev = dev;
}

void
bcmgenfb_set_ioctl(int(*boo)(void *, void *, u_long, void *, int, struct lwp *))
{
	bcmgenfb_ioctl_handler = boo;
}

void
bcmgenfb_ddb_trap_callback(int where)
{
	if (bcmgenfb_console_dev == NULL)
		return;

	if (where) {
		genfb_enable_polling(bcmgenfb_console_dev);
	} else {
		genfb_disable_polling(bcmgenfb_console_dev);
	}
}