summaryrefslogtreecommitdiff
path: root/sys/dev/fdt/simplefb.c
blob: e8f1e807f9230c312fde33f0ebafe9f96fa3a727 (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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/* $NetBSD: simplefb.c,v 1.15 2021/08/30 22:47:24 jmcneill Exp $ */

/*-
 * Copyright (c) 2017 Jared 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 AUTHOR ``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 AUTHOR 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.
 */

#include "opt_wsdisplay_compat.h"

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: simplefb.c,v 1.15 2021/08/30 22:47:24 jmcneill Exp $");

#include <sys/param.h>
#include <sys/bus.h>
#include <sys/device.h>
#include <sys/systm.h>

#include <dev/fdt/fdtvar.h>

#include <dev/wsfb/genfbvar.h>

static const struct device_compatible_entry compat_data[] = {
	{ .compat = "simple-framebuffer" },
	DEVICE_COMPAT_EOL
};

struct simplefb_softc {
	struct genfb_softc sc_gen;
	bus_space_tag_t sc_bst;
	bus_space_handle_t sc_bsh;
	int sc_phandle;

	bus_addr_t sc_paddr;
};

static int simplefb_console_phandle = -1;

static bool
simplefb_shutdown(device_t self, int flags)
{
	genfb_enable_polling(self);
	return true;
}

static int
simplefb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag, lwp_t *l)
{
	struct simplefb_softc * const sc = v;
	struct wsdisplayio_bus_id *busid;
	struct wsdisplayio_fbinfo *fbi;
	struct rasops_info *ri;
	u_int video;
	int error;

	switch (cmd) {
	case WSDISPLAYIO_GTYPE:
		*(u_int *)data = WSDISPLAY_TYPE_GENFB;
		return 0;
	case WSDISPLAYIO_GET_BUSID:
		busid = data;
		busid->bus_type = WSDISPLAYIO_BUS_SOC;
		return 0;
	case WSDISPLAYIO_GET_FBINFO:
		fbi = data;
		ri = &sc->sc_gen.vd.active->scr_ri;
		error = wsdisplayio_get_fbinfo(ri, fbi);
		if (error == 0) {
	                /*
	                 * XXX
	                 * if the fb isn't page aligned, tell wsfb to skip the
			 * unaligned part
			 */
			fbi->fbi_fboffset = sc->sc_paddr & PAGE_MASK;
			fbi->fbi_flags |= WSFB_VRAM_IS_RAM;
		}
		return error;
	case WSDISPLAYIO_SVIDEO:
		video = *(u_int *)data;
		if (video == WSDISPLAYIO_VIDEO_OFF)
			pmf_event_inject(NULL, PMFE_DISPLAY_OFF);
		else if (video == WSDISPLAYIO_VIDEO_ON)
			pmf_event_inject(NULL, PMFE_DISPLAY_ON);
		else
			return EINVAL;
		return 0;
	default:
		return EPASSTHROUGH;
	}
}

static paddr_t
simplefb_mmap(void *v, void *vs, off_t off, int prot)
{
	struct simplefb_softc * const sc = v;

	if (off < 0 || off >= (sc->sc_paddr & PAGE_MASK) +
	    sc->sc_gen.sc_fbsize)
		return -1;

	return bus_space_mmap(sc->sc_bst, trunc_page(sc->sc_paddr), off, prot,
	    BUS_SPACE_MAP_LINEAR | BUS_SPACE_MAP_PREFETCHABLE);
}

static int
simplefb_attach_genfb(struct simplefb_softc *sc)
{
	device_t dev = sc->sc_gen.sc_dev;
	prop_dictionary_t dict = device_properties(dev);
	const int phandle = sc->sc_phandle;
	struct genfb_ops ops;
	uint32_t width, height, stride;
	uint16_t depth;
	const char *format;
	uint64_t sfb_addr;
	bus_addr_t addr;
	bus_size_t size;

	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
		aprint_error(": couldn't get registers\n");
		return ENXIO;
	}

	if (size == 0) {
		aprint_naive("\n");
		aprint_normal(": disabled\n");
		return ENXIO;
	}

	if (of_getprop_uint32(phandle, "width", &width) != 0 ||
	    of_getprop_uint32(phandle, "height", &height) != 0 ||
	    of_getprop_uint32(phandle, "stride", &stride) != 0 ||
	    (format = fdtbus_get_string(phandle, "format")) == NULL) {
		aprint_error(": missing property in DT\n");
		return ENXIO;
	}

	if (strcmp(format, "a8b8g8r8") == 0 ||
	    strcmp(format, "x8r8g8b8") == 0) {
		depth = 32;
	} else if (strcmp(format, "r8g8b8a8") == 0 ||
		   strcmp(format, "b8g8r8x8") == 0) {
		depth = 32;
		prop_dictionary_set_bool(dict, "is_swapped", true);
	} else if (strcmp(format, "x2r10g10b10") == 0) {
		depth = 32;
		prop_dictionary_set_bool(dict, "is_10bit", true);
	} else if (strcmp(format, "r5g6b5") == 0) {
		depth = 16;
	} else {
		aprint_error(": unsupported format '%s'\n", format);
		return ENXIO;
	}

	if (size < stride * height) {
		aprint_error(": incorrect size\n");
		return ENXIO;
	}

	/* Device may have been reconfigured. MD code will tell us. */
	if (prop_dictionary_get_uint64(dict, "simplefb-physaddr", &sfb_addr) &&
	    sfb_addr != 0) {
		addr = (bus_addr_t)sfb_addr;
	}

	if (bus_space_map(sc->sc_bst, addr, size,
	    BUS_SPACE_MAP_LINEAR | BUS_SPACE_MAP_PREFETCHABLE,
	    &sc->sc_bsh) != 0) {
		aprint_error(": failed to map fb\n");
		return ENXIO;
	}

	sc->sc_paddr = addr;

	prop_dictionary_set_uint32(dict, "width", width);
	prop_dictionary_set_uint32(dict, "height", height);
	prop_dictionary_set_uint8(dict, "depth", depth);
	prop_dictionary_set_uint16(dict, "linebytes", stride);
	prop_dictionary_set_uint32(dict, "address", addr);
	prop_dictionary_set_uint64(dict, "virtual_address",
	    (uintptr_t)bus_space_vaddr(sc->sc_bst, sc->sc_bsh));

	genfb_init(&sc->sc_gen);

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

	aprint_naive("\n");
	aprint_normal(": Simple Framebuffer (%ux%u %u-bpp @ 0x%" PRIxBUSADDR ")\n",
	    width, height, depth, addr);

	pmf_device_register1(dev, NULL, NULL, simplefb_shutdown);

	memset(&ops, 0, sizeof(ops));
	ops.genfb_ioctl = simplefb_ioctl;
	ops.genfb_mmap = simplefb_mmap;

#ifdef WSDISPLAY_MULTICONS
	const bool is_console = true;
	genfb_cnattach();
#else
	const bool is_console = phandle == simplefb_console_phandle;
	if (is_console)
		aprint_normal_dev(sc->sc_gen.sc_dev,
		    "switching to framebuffer console\n");
#endif

	prop_dictionary_set_bool(dict, "is_console", is_console);

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

	return 0;
}

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

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

static void
simplefb_attach(device_t parent, device_t self, void *aux)
{
	struct simplefb_softc * const sc = device_private(self);
	struct fdt_attach_args * const faa = aux;
	const int phandle = faa->faa_phandle;

	sc->sc_gen.sc_dev = self;
	sc->sc_phandle = phandle;
	sc->sc_bst = faa->faa_bst;

	if (simplefb_attach_genfb(sc) != 0)
		return;
}

CFATTACH_DECL_NEW(simplefb, sizeof(struct simplefb_softc),
	simplefb_match, simplefb_attach, NULL, NULL);

static int
simplefb_console_match(int phandle)
{
	return of_compatible_match(phandle, compat_data);
}

static void
simplefb_console_consinit(struct fdt_attach_args *faa, u_int uart_freq)
{
	simplefb_console_phandle = faa->faa_phandle;
	genfb_cnattach();
}

static const struct fdt_console simplefb_fdt_console = {
	.match = simplefb_console_match,
	.consinit = simplefb_console_consinit
};

FDT_CONSOLE(simplefb, &simplefb_fdt_console);