xref: /netbsd/sys/dev/fdt/simplefb.c (revision 32909d15)
1*32909d15Sjmcneill /* $NetBSD: simplefb.c,v 1.15 2021/08/30 22:47:24 jmcneill Exp $ */
26bc85a93Sjmcneill 
36bc85a93Sjmcneill /*-
46bc85a93Sjmcneill  * Copyright (c) 2017 Jared McNeill <jmcneill@invisible.ca>
56bc85a93Sjmcneill  * All rights reserved.
66bc85a93Sjmcneill  *
76bc85a93Sjmcneill  * Redistribution and use in source and binary forms, with or without
86bc85a93Sjmcneill  * modification, are permitted provided that the following conditions
96bc85a93Sjmcneill  * are met:
106bc85a93Sjmcneill  * 1. Redistributions of source code must retain the above copyright
116bc85a93Sjmcneill  *    notice, this list of conditions and the following disclaimer.
126bc85a93Sjmcneill  * 2. Redistributions in binary form must reproduce the above copyright
136bc85a93Sjmcneill  *    notice, this list of conditions and the following disclaimer in the
146bc85a93Sjmcneill  *    documentation and/or other materials provided with the distribution.
156bc85a93Sjmcneill  *
166bc85a93Sjmcneill  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
176bc85a93Sjmcneill  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
186bc85a93Sjmcneill  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
196bc85a93Sjmcneill  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
206bc85a93Sjmcneill  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
216bc85a93Sjmcneill  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
226bc85a93Sjmcneill  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
236bc85a93Sjmcneill  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
246bc85a93Sjmcneill  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
256bc85a93Sjmcneill  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
266bc85a93Sjmcneill  * SUCH DAMAGE.
276bc85a93Sjmcneill  */
286bc85a93Sjmcneill 
299e659622Sjmcneill #include "opt_wsdisplay_compat.h"
309e659622Sjmcneill 
316bc85a93Sjmcneill #include <sys/cdefs.h>
32*32909d15Sjmcneill __KERNEL_RCSID(0, "$NetBSD: simplefb.c,v 1.15 2021/08/30 22:47:24 jmcneill Exp $");
336bc85a93Sjmcneill 
346bc85a93Sjmcneill #include <sys/param.h>
356bc85a93Sjmcneill #include <sys/bus.h>
366bc85a93Sjmcneill #include <sys/device.h>
376bc85a93Sjmcneill #include <sys/systm.h>
386bc85a93Sjmcneill 
396bc85a93Sjmcneill #include <dev/fdt/fdtvar.h>
406bc85a93Sjmcneill 
416bc85a93Sjmcneill #include <dev/wsfb/genfbvar.h>
426bc85a93Sjmcneill 
438e90f9edSthorpej static const struct device_compatible_entry compat_data[] = {
448e90f9edSthorpej 	{ .compat = "simple-framebuffer" },
458e90f9edSthorpej 	DEVICE_COMPAT_EOL
466bc85a93Sjmcneill };
476bc85a93Sjmcneill 
486bc85a93Sjmcneill struct simplefb_softc {
496bc85a93Sjmcneill 	struct genfb_softc sc_gen;
506bc85a93Sjmcneill 	bus_space_tag_t sc_bst;
516bc85a93Sjmcneill 	bus_space_handle_t sc_bsh;
526bc85a93Sjmcneill 	int sc_phandle;
536bc85a93Sjmcneill 
546bc85a93Sjmcneill 	bus_addr_t sc_paddr;
556bc85a93Sjmcneill };
566bc85a93Sjmcneill 
57886c97bdSjmcneill static int simplefb_console_phandle = -1;
58886c97bdSjmcneill 
596bc85a93Sjmcneill static bool
simplefb_shutdown(device_t self,int flags)606bc85a93Sjmcneill simplefb_shutdown(device_t self, int flags)
616bc85a93Sjmcneill {
626bc85a93Sjmcneill 	genfb_enable_polling(self);
636bc85a93Sjmcneill 	return true;
646bc85a93Sjmcneill }
656bc85a93Sjmcneill 
666bc85a93Sjmcneill static int
simplefb_ioctl(void * v,void * vs,u_long cmd,void * data,int flag,lwp_t * l)676bc85a93Sjmcneill simplefb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag, lwp_t *l)
686bc85a93Sjmcneill {
696bc85a93Sjmcneill 	struct simplefb_softc * const sc = v;
706bc85a93Sjmcneill 	struct wsdisplayio_bus_id *busid;
716bc85a93Sjmcneill 	struct wsdisplayio_fbinfo *fbi;
726bc85a93Sjmcneill 	struct rasops_info *ri;
7337ec32d2Sjmcneill 	u_int video;
746bc85a93Sjmcneill 	int error;
756bc85a93Sjmcneill 
766bc85a93Sjmcneill 	switch (cmd) {
776bc85a93Sjmcneill 	case WSDISPLAYIO_GTYPE:
786bc85a93Sjmcneill 		*(u_int *)data = WSDISPLAY_TYPE_GENFB;
796bc85a93Sjmcneill 		return 0;
806bc85a93Sjmcneill 	case WSDISPLAYIO_GET_BUSID:
816bc85a93Sjmcneill 		busid = data;
826bc85a93Sjmcneill 		busid->bus_type = WSDISPLAYIO_BUS_SOC;
836bc85a93Sjmcneill 		return 0;
846bc85a93Sjmcneill 	case WSDISPLAYIO_GET_FBINFO:
856bc85a93Sjmcneill 		fbi = data;
866bc85a93Sjmcneill 		ri = &sc->sc_gen.vd.active->scr_ri;
876bc85a93Sjmcneill 		error = wsdisplayio_get_fbinfo(ri, fbi);
881834b3d6Smacallan 		if (error == 0) {
891834b3d6Smacallan 	                /*
901834b3d6Smacallan 	                 * XXX
911834b3d6Smacallan 	                 * if the fb isn't page aligned, tell wsfb to skip the
921834b3d6Smacallan 			 * unaligned part
931834b3d6Smacallan 			 */
941834b3d6Smacallan 			fbi->fbi_fboffset = sc->sc_paddr & PAGE_MASK;
956bc85a93Sjmcneill 			fbi->fbi_flags |= WSFB_VRAM_IS_RAM;
961834b3d6Smacallan 		}
976bc85a93Sjmcneill 		return error;
9837ec32d2Sjmcneill 	case WSDISPLAYIO_SVIDEO:
9937ec32d2Sjmcneill 		video = *(u_int *)data;
10037ec32d2Sjmcneill 		if (video == WSDISPLAYIO_VIDEO_OFF)
10137ec32d2Sjmcneill 			pmf_event_inject(NULL, PMFE_DISPLAY_OFF);
10237ec32d2Sjmcneill 		else if (video == WSDISPLAYIO_VIDEO_ON)
10337ec32d2Sjmcneill 			pmf_event_inject(NULL, PMFE_DISPLAY_ON);
10437ec32d2Sjmcneill 		else
10537ec32d2Sjmcneill 			return EINVAL;
10637ec32d2Sjmcneill 		return 0;
1076bc85a93Sjmcneill 	default:
1086bc85a93Sjmcneill 		return EPASSTHROUGH;
1096bc85a93Sjmcneill 	}
1106bc85a93Sjmcneill }
1116bc85a93Sjmcneill 
1126bc85a93Sjmcneill static paddr_t
simplefb_mmap(void * v,void * vs,off_t off,int prot)1136bc85a93Sjmcneill simplefb_mmap(void *v, void *vs, off_t off, int prot)
1146bc85a93Sjmcneill {
1156bc85a93Sjmcneill 	struct simplefb_softc * const sc = v;
1166bc85a93Sjmcneill 
1175bf455f7Srin 	if (off < 0 || off >= (sc->sc_paddr & PAGE_MASK) +
1185bf455f7Srin 	    sc->sc_gen.sc_fbsize)
1196bc85a93Sjmcneill 		return -1;
1206bc85a93Sjmcneill 
1215bf455f7Srin 	return bus_space_mmap(sc->sc_bst, trunc_page(sc->sc_paddr), off, prot,
1226bc85a93Sjmcneill 	    BUS_SPACE_MAP_LINEAR | BUS_SPACE_MAP_PREFETCHABLE);
1236bc85a93Sjmcneill }
1246bc85a93Sjmcneill 
1256bc85a93Sjmcneill static int
simplefb_attach_genfb(struct simplefb_softc * sc)1266bc85a93Sjmcneill simplefb_attach_genfb(struct simplefb_softc *sc)
1276bc85a93Sjmcneill {
1286bc85a93Sjmcneill 	device_t dev = sc->sc_gen.sc_dev;
1296bc85a93Sjmcneill 	prop_dictionary_t dict = device_properties(dev);
1306bc85a93Sjmcneill 	const int phandle = sc->sc_phandle;
1316bc85a93Sjmcneill 	struct genfb_ops ops;
1326bc85a93Sjmcneill 	uint32_t width, height, stride;
1336bc85a93Sjmcneill 	uint16_t depth;
1346bc85a93Sjmcneill 	const char *format;
135c4d72cceSrin 	uint64_t sfb_addr;
136c4d72cceSrin 	bus_addr_t addr;
1376bc85a93Sjmcneill 	bus_size_t size;
1386bc85a93Sjmcneill 
1396bc85a93Sjmcneill 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
1406bc85a93Sjmcneill 		aprint_error(": couldn't get registers\n");
1416bc85a93Sjmcneill 		return ENXIO;
1426bc85a93Sjmcneill 	}
1436bc85a93Sjmcneill 
144886c97bdSjmcneill 	if (size == 0) {
145886c97bdSjmcneill 		aprint_naive("\n");
146886c97bdSjmcneill 		aprint_normal(": disabled\n");
147886c97bdSjmcneill 		return ENXIO;
148886c97bdSjmcneill 	}
149886c97bdSjmcneill 
1506bc85a93Sjmcneill 	if (of_getprop_uint32(phandle, "width", &width) != 0 ||
1516bc85a93Sjmcneill 	    of_getprop_uint32(phandle, "height", &height) != 0 ||
1526bc85a93Sjmcneill 	    of_getprop_uint32(phandle, "stride", &stride) != 0 ||
1536bc85a93Sjmcneill 	    (format = fdtbus_get_string(phandle, "format")) == NULL) {
1546bc85a93Sjmcneill 		aprint_error(": missing property in DT\n");
1556bc85a93Sjmcneill 		return ENXIO;
1566bc85a93Sjmcneill 	}
1576bc85a93Sjmcneill 
1586bc85a93Sjmcneill 	if (strcmp(format, "a8b8g8r8") == 0 ||
1596bc85a93Sjmcneill 	    strcmp(format, "x8r8g8b8") == 0) {
1606bc85a93Sjmcneill 		depth = 32;
1616deedafdSrin 	} else if (strcmp(format, "r8g8b8a8") == 0 ||
1626deedafdSrin 		   strcmp(format, "b8g8r8x8") == 0) {
1636deedafdSrin 		depth = 32;
1646deedafdSrin 		prop_dictionary_set_bool(dict, "is_swapped", true);
165*32909d15Sjmcneill 	} else if (strcmp(format, "x2r10g10b10") == 0) {
166*32909d15Sjmcneill 		depth = 32;
167*32909d15Sjmcneill 		prop_dictionary_set_bool(dict, "is_10bit", true);
1686bc85a93Sjmcneill 	} else if (strcmp(format, "r5g6b5") == 0) {
1696bc85a93Sjmcneill 		depth = 16;
1706bc85a93Sjmcneill 	} else {
1716bc85a93Sjmcneill 		aprint_error(": unsupported format '%s'\n", format);
1726bc85a93Sjmcneill 		return ENXIO;
1736bc85a93Sjmcneill 	}
1746bc85a93Sjmcneill 
17587cd84c7Sjmcneill 	if (size < stride * height) {
176134ffaa4Sskrll 		aprint_error(": incorrect size\n");
177134ffaa4Sskrll 		return ENXIO;
178134ffaa4Sskrll 	}
179134ffaa4Sskrll 
180a7c55392Sjmcneill 	/* Device may have been reconfigured. MD code will tell us. */
181a7c55392Sjmcneill 	if (prop_dictionary_get_uint64(dict, "simplefb-physaddr", &sfb_addr) &&
182a7c55392Sjmcneill 	    sfb_addr != 0) {
183a7c55392Sjmcneill 		addr = (bus_addr_t)sfb_addr;
184a7c55392Sjmcneill 	}
185a7c55392Sjmcneill 
1868694f07cSryo 	if (bus_space_map(sc->sc_bst, addr, size,
1878694f07cSryo 	    BUS_SPACE_MAP_LINEAR | BUS_SPACE_MAP_PREFETCHABLE,
1886bc85a93Sjmcneill 	    &sc->sc_bsh) != 0) {
1896bc85a93Sjmcneill 		aprint_error(": failed to map fb\n");
1906bc85a93Sjmcneill 		return ENXIO;
1916bc85a93Sjmcneill 	}
1926bc85a93Sjmcneill 
1936bc85a93Sjmcneill 	sc->sc_paddr = addr;
1946bc85a93Sjmcneill 
1956bc85a93Sjmcneill 	prop_dictionary_set_uint32(dict, "width", width);
1966bc85a93Sjmcneill 	prop_dictionary_set_uint32(dict, "height", height);
1976bc85a93Sjmcneill 	prop_dictionary_set_uint8(dict, "depth", depth);
1986bc85a93Sjmcneill 	prop_dictionary_set_uint16(dict, "linebytes", stride);
1996bc85a93Sjmcneill 	prop_dictionary_set_uint32(dict, "address", addr);
2008694f07cSryo 	prop_dictionary_set_uint64(dict, "virtual_address",
2016bc85a93Sjmcneill 	    (uintptr_t)bus_space_vaddr(sc->sc_bst, sc->sc_bsh));
2026bc85a93Sjmcneill 
2036bc85a93Sjmcneill 	genfb_init(&sc->sc_gen);
2046bc85a93Sjmcneill 
2056bc85a93Sjmcneill 	if (sc->sc_gen.sc_width == 0 || sc->sc_gen.sc_fbsize == 0) {
2066bc85a93Sjmcneill 		aprint_normal(": disabled\n");
2076bc85a93Sjmcneill 		return ENXIO;
2086bc85a93Sjmcneill 	}
2096bc85a93Sjmcneill 
2106bc85a93Sjmcneill 	aprint_naive("\n");
2116bc85a93Sjmcneill 	aprint_normal(": Simple Framebuffer (%ux%u %u-bpp @ 0x%" PRIxBUSADDR ")\n",
2126bc85a93Sjmcneill 	    width, height, depth, addr);
2136bc85a93Sjmcneill 
2146bc85a93Sjmcneill 	pmf_device_register1(dev, NULL, NULL, simplefb_shutdown);
2156bc85a93Sjmcneill 
2166bc85a93Sjmcneill 	memset(&ops, 0, sizeof(ops));
2176bc85a93Sjmcneill 	ops.genfb_ioctl = simplefb_ioctl;
2186bc85a93Sjmcneill 	ops.genfb_mmap = simplefb_mmap;
2196bc85a93Sjmcneill 
2209e659622Sjmcneill #ifdef WSDISPLAY_MULTICONS
2219e659622Sjmcneill 	const bool is_console = true;
222e1cc6694Sjmcneill 	genfb_cnattach();
2239e659622Sjmcneill #else
224886c97bdSjmcneill 	const bool is_console = phandle == simplefb_console_phandle;
2256bc85a93Sjmcneill 	if (is_console)
2266bc85a93Sjmcneill 		aprint_normal_dev(sc->sc_gen.sc_dev,
2276bc85a93Sjmcneill 		    "switching to framebuffer console\n");
2289e659622Sjmcneill #endif
2299e659622Sjmcneill 
2309e659622Sjmcneill 	prop_dictionary_set_bool(dict, "is_console", is_console);
2316bc85a93Sjmcneill 
2326bc85a93Sjmcneill 	genfb_attach(&sc->sc_gen, &ops);
2336bc85a93Sjmcneill 
2346bc85a93Sjmcneill 	return 0;
2356bc85a93Sjmcneill }
2366bc85a93Sjmcneill 
2376bc85a93Sjmcneill static int
simplefb_match(device_t parent,cfdata_t cf,void * aux)2386bc85a93Sjmcneill simplefb_match(device_t parent, cfdata_t cf, void *aux)
2396bc85a93Sjmcneill {
2406bc85a93Sjmcneill 	struct fdt_attach_args * const faa = aux;
2416bc85a93Sjmcneill 
2428e90f9edSthorpej 	return of_compatible_match(faa->faa_phandle, compat_data);
2436bc85a93Sjmcneill }
2446bc85a93Sjmcneill 
2456bc85a93Sjmcneill static void
simplefb_attach(device_t parent,device_t self,void * aux)2466bc85a93Sjmcneill simplefb_attach(device_t parent, device_t self, void *aux)
2476bc85a93Sjmcneill {
2486bc85a93Sjmcneill 	struct simplefb_softc * const sc = device_private(self);
2496bc85a93Sjmcneill 	struct fdt_attach_args * const faa = aux;
2506bc85a93Sjmcneill 	const int phandle = faa->faa_phandle;
2516bc85a93Sjmcneill 
2526bc85a93Sjmcneill 	sc->sc_gen.sc_dev = self;
2536bc85a93Sjmcneill 	sc->sc_phandle = phandle;
2546bc85a93Sjmcneill 	sc->sc_bst = faa->faa_bst;
2556bc85a93Sjmcneill 
2566bc85a93Sjmcneill 	if (simplefb_attach_genfb(sc) != 0)
2576bc85a93Sjmcneill 		return;
2586bc85a93Sjmcneill }
2596bc85a93Sjmcneill 
2606bc85a93Sjmcneill CFATTACH_DECL_NEW(simplefb, sizeof(struct simplefb_softc),
2616bc85a93Sjmcneill 	simplefb_match, simplefb_attach, NULL, NULL);
262886c97bdSjmcneill 
263886c97bdSjmcneill static int
simplefb_console_match(int phandle)264886c97bdSjmcneill simplefb_console_match(int phandle)
265886c97bdSjmcneill {
2668e90f9edSthorpej 	return of_compatible_match(phandle, compat_data);
267886c97bdSjmcneill }
268886c97bdSjmcneill 
269886c97bdSjmcneill static void
simplefb_console_consinit(struct fdt_attach_args * faa,u_int uart_freq)270886c97bdSjmcneill simplefb_console_consinit(struct fdt_attach_args *faa, u_int uart_freq)
271886c97bdSjmcneill {
272886c97bdSjmcneill 	simplefb_console_phandle = faa->faa_phandle;
273886c97bdSjmcneill 	genfb_cnattach();
274886c97bdSjmcneill }
275886c97bdSjmcneill 
276886c97bdSjmcneill static const struct fdt_console simplefb_fdt_console = {
277886c97bdSjmcneill 	.match = simplefb_console_match,
278886c97bdSjmcneill 	.consinit = simplefb_console_consinit
279886c97bdSjmcneill };
280886c97bdSjmcneill 
281886c97bdSjmcneill FDT_CONSOLE(simplefb, &simplefb_fdt_console);
282