xref: /netbsd-src/sys/arch/arm/fdt/arm_simplefb.c (revision 1099f047a0006cc19f46e1118dd280c2811d7696)
1*1099f047Sjmcneill /* $NetBSD: arm_simplefb.c,v 1.13 2022/10/14 22:10:15 jmcneill Exp $ */
22824038dSjmcneill 
32824038dSjmcneill /*-
42824038dSjmcneill  * Copyright (c) 2019 The NetBSD Foundation, Inc.
52824038dSjmcneill  * All rights reserved.
62824038dSjmcneill  *
72824038dSjmcneill  * This code is derived from software contributed to The NetBSD Foundation
82824038dSjmcneill  * by Jared McNeill <jmcneill@invisible.ca>.
92824038dSjmcneill  *
102824038dSjmcneill  * Redistribution and use in source and binary forms, with or without
112824038dSjmcneill  * modification, are permitted provided that the following conditions
122824038dSjmcneill  * are met:
132824038dSjmcneill  * 1. Redistributions of source code must retain the above copyright
142824038dSjmcneill  *    notice, this list of conditions and the following disclaimer.
152824038dSjmcneill  * 2. Redistributions in binary form must reproduce the above copyright
162824038dSjmcneill  *    notice, this list of conditions and the following disclaimer in the
172824038dSjmcneill  *    documentation and/or other materials provided with the distribution.
182824038dSjmcneill  *
192824038dSjmcneill  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
202824038dSjmcneill  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
212824038dSjmcneill  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
222824038dSjmcneill  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
232824038dSjmcneill  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
242824038dSjmcneill  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
252824038dSjmcneill  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
262824038dSjmcneill  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
272824038dSjmcneill  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
282824038dSjmcneill  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
292824038dSjmcneill  * POSSIBILITY OF SUCH DAMAGE.
302824038dSjmcneill  */
312824038dSjmcneill 
322824038dSjmcneill #include "pci.h"
332824038dSjmcneill #include "opt_pci.h"
34a1329c24Sjmcneill #include "opt_vcons.h"
352824038dSjmcneill 
362824038dSjmcneill #include <sys/cdefs.h>
37*1099f047Sjmcneill __KERNEL_RCSID(0, "$NetBSD: arm_simplefb.c,v 1.13 2022/10/14 22:10:15 jmcneill Exp $");
382824038dSjmcneill 
392824038dSjmcneill #include <sys/param.h>
402824038dSjmcneill #include <sys/bus.h>
412824038dSjmcneill #include <sys/cpu.h>
422824038dSjmcneill #include <sys/device.h>
432824038dSjmcneill 
442824038dSjmcneill #include <dev/fdt/fdtvar.h>
452824038dSjmcneill #include <arm/fdt/arm_fdtvar.h>
462824038dSjmcneill 
472824038dSjmcneill #include <dev/wscons/wsconsio.h>
482824038dSjmcneill #include <dev/wscons/wsdisplayvar.h>
492824038dSjmcneill #include <dev/rasops/rasops.h>
502824038dSjmcneill #include <dev/wsfont/wsfont.h>
512824038dSjmcneill #include <dev/wscons/wsdisplay_vconsvar.h>
522824038dSjmcneill 
532824038dSjmcneill #if NPCI > 0 && defined(PCI_NETBSD_CONFIGURE)
542824038dSjmcneill #include <dev/pci/pcireg.h>
552824038dSjmcneill #include <dev/pci/pcivar.h>
562824038dSjmcneill #include <dev/pci/pciconf.h>
572824038dSjmcneill #endif
582824038dSjmcneill 
592824038dSjmcneill #include <arm/fdt/arm_simplefb.h>
602824038dSjmcneill 
612824038dSjmcneill #include <libfdt.h>
622824038dSjmcneill 
632824038dSjmcneill extern struct bus_space arm_generic_bs_tag;
642824038dSjmcneill 
652824038dSjmcneill static struct arm_simplefb_softc {
662824038dSjmcneill 	uint32_t	sc_width;
672824038dSjmcneill 	uint32_t	sc_height;
682824038dSjmcneill 	uint32_t	sc_stride;
692824038dSjmcneill 	uint16_t	sc_depth;
709047c5eaSrin 	bool		sc_swapped;
71e69e27c5Sjmcneill 	bool		sc_10bit;
722824038dSjmcneill 	void		*sc_bits;
732824038dSjmcneill } arm_simplefb_softc;
742824038dSjmcneill 
752824038dSjmcneill static struct wsscreen_descr arm_simplefb_stdscreen = {
762824038dSjmcneill 	.name = "std",
772824038dSjmcneill 	.ncols = 0,
782824038dSjmcneill 	.nrows = 0,
792824038dSjmcneill 	.textops = NULL,
802824038dSjmcneill 	.fontwidth = 0,
812824038dSjmcneill 	.fontheight = 0,
822824038dSjmcneill 	.capabilities = 0,
832824038dSjmcneill 	.modecookie = NULL
842824038dSjmcneill };
852824038dSjmcneill 
862824038dSjmcneill static struct wsdisplay_accessops arm_simplefb_accessops;
872824038dSjmcneill static struct vcons_data arm_simplefb_vcons_data;
882824038dSjmcneill static struct vcons_screen arm_simplefb_screen;
892824038dSjmcneill 
9036ef29e8Sjmcneill static bus_addr_t arm_simplefb_addr;
9136ef29e8Sjmcneill static bus_size_t arm_simplefb_size;
9236ef29e8Sjmcneill static bus_space_handle_t arm_simplefb_bsh;
9336ef29e8Sjmcneill 
946e54367aSthorpej static const struct device_compatible_entry compat_data[] = {
956e54367aSthorpej 	{ .compat = "simple-framebuffer" },
966e54367aSthorpej 	DEVICE_COMPAT_EOL
976e54367aSthorpej };
986e54367aSthorpej 
992824038dSjmcneill static int
arm_simplefb_find_node(void)1002824038dSjmcneill arm_simplefb_find_node(void)
1012824038dSjmcneill {
1022824038dSjmcneill 	int chosen_phandle, child;
1032824038dSjmcneill 
1042824038dSjmcneill 	chosen_phandle = OF_finddevice("/chosen");
1052824038dSjmcneill 	if (chosen_phandle == -1)
1062824038dSjmcneill 		return -1;
1072824038dSjmcneill 
1082824038dSjmcneill 	for (child = OF_child(chosen_phandle); child; child = OF_peer(child)) {
1092824038dSjmcneill 		if (!fdtbus_status_okay(child))
1102824038dSjmcneill 			continue;
1116e54367aSthorpej 		if (!of_compatible_match(child, compat_data))
1122824038dSjmcneill 			continue;
1132824038dSjmcneill 
1142824038dSjmcneill 		return child;
1152824038dSjmcneill 	}
1162824038dSjmcneill 
1172824038dSjmcneill 	return -1;
1182824038dSjmcneill }
1192824038dSjmcneill 
1202824038dSjmcneill static void
arm_simplefb_init_screen(void * cookie,struct vcons_screen * scr,int existing,long * defattr)1212824038dSjmcneill arm_simplefb_init_screen(void *cookie, struct vcons_screen *scr,
1222824038dSjmcneill     int existing, long *defattr)
1232824038dSjmcneill {
1242824038dSjmcneill 	struct arm_simplefb_softc * const sc = cookie;
1252824038dSjmcneill 	struct rasops_info *ri = &scr->scr_ri;
1262824038dSjmcneill 
1272824038dSjmcneill 	ri->ri_width = sc->sc_width;
1282824038dSjmcneill 	ri->ri_height = sc->sc_height;
1292824038dSjmcneill 	ri->ri_depth = sc->sc_depth;
1302824038dSjmcneill 	ri->ri_stride = sc->sc_stride;
1312824038dSjmcneill 	ri->ri_bits = sc->sc_bits;
1322824038dSjmcneill 	ri->ri_flg = RI_CENTER | RI_FULLCLEAR | RI_CLEAR;
1332824038dSjmcneill 
1349047c5eaSrin 	if (sc->sc_swapped) {
1359047c5eaSrin 		KASSERT(ri->ri_depth == 32);
1369047c5eaSrin 		ri->ri_rnum = ri->ri_gnum = ri->ri_bnum = 8;
1379047c5eaSrin 		ri->ri_rpos =  8;
1389047c5eaSrin 		ri->ri_gpos = 16;
1399047c5eaSrin 		ri->ri_bpos = 24;
140e69e27c5Sjmcneill 	} else if (sc->sc_10bit) {
141e69e27c5Sjmcneill 		KASSERT(ri->ri_depth == 32);
142e69e27c5Sjmcneill 		ri->ri_rnum = ri->ri_gnum = ri->ri_bnum = 10;
143e69e27c5Sjmcneill 		ri->ri_rpos = 20;
144e69e27c5Sjmcneill 		ri->ri_gpos = 10;
145e69e27c5Sjmcneill 		ri->ri_bpos =  0;
1469047c5eaSrin 	}
1479047c5eaSrin 
1482824038dSjmcneill 	scr->scr_flags |= VCONS_LOADFONT;
1492824038dSjmcneill 	scr->scr_flags |= VCONS_DONT_READ;
1502824038dSjmcneill 
1512824038dSjmcneill 	rasops_init(ri, ri->ri_height / 8, ri->ri_width / 8);
1522824038dSjmcneill 	ri->ri_caps = WSSCREEN_WSCOLORS | WSSCREEN_HILIT | WSSCREEN_UNDERLINE;
1532824038dSjmcneill 	rasops_reconfig(ri, sc->sc_height / ri->ri_font->fontheight,
1542824038dSjmcneill 	    sc->sc_width / ri->ri_font->fontwidth);
1552824038dSjmcneill 
1562824038dSjmcneill 	ri->ri_hw = scr;
1572824038dSjmcneill }
1582824038dSjmcneill 
1592824038dSjmcneill static int
arm_simplefb_ioctl(void * v,void * vs,u_long cmd,void * data,int flag,lwp_t * l)1607472dc0dSjmcneill arm_simplefb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
1617472dc0dSjmcneill     lwp_t *l)
1622824038dSjmcneill {
1632824038dSjmcneill 	return EPASSTHROUGH;
1642824038dSjmcneill }
1652824038dSjmcneill 
1662824038dSjmcneill static paddr_t
arm_simplefb_mmap(void * v,void * vs,off_t offset,int prot)1672824038dSjmcneill arm_simplefb_mmap(void *v, void *vs, off_t offset, int prot)
1682824038dSjmcneill {
1692824038dSjmcneill 	return -1;
1702824038dSjmcneill }
1712824038dSjmcneill 
1722824038dSjmcneill static void
arm_simplefb_pollc(void * v,int on)1732824038dSjmcneill arm_simplefb_pollc(void *v, int on)
1742824038dSjmcneill {
1752824038dSjmcneill }
1762824038dSjmcneill 
17736ef29e8Sjmcneill uint64_t
arm_simplefb_physaddr(void)17836ef29e8Sjmcneill arm_simplefb_physaddr(void)
17936ef29e8Sjmcneill {
18036ef29e8Sjmcneill 	return arm_simplefb_addr;
18136ef29e8Sjmcneill }
18236ef29e8Sjmcneill 
1832824038dSjmcneill void
arm_simplefb_preattach(void)1842824038dSjmcneill arm_simplefb_preattach(void)
1852824038dSjmcneill {
1862824038dSjmcneill 	struct arm_simplefb_softc * const sc = &arm_simplefb_softc;
1872824038dSjmcneill 	struct rasops_info *ri = &arm_simplefb_screen.scr_ri;
1882824038dSjmcneill 	bus_space_tag_t bst = &arm_generic_bs_tag;
1892824038dSjmcneill 	bus_space_handle_t bsh;
1902824038dSjmcneill 	uint32_t width, height, stride;
1912824038dSjmcneill 	const char *format;
1922824038dSjmcneill 	bus_addr_t addr;
1932824038dSjmcneill 	bus_size_t size;
1942824038dSjmcneill 	uint16_t depth;
1952824038dSjmcneill 	long defattr;
1969047c5eaSrin 	bool swapped = false;
197e69e27c5Sjmcneill 	bool is_10bit = false;
1982824038dSjmcneill 
1992824038dSjmcneill 	const int phandle = arm_simplefb_find_node();
2002824038dSjmcneill 	if (phandle == -1)
2012824038dSjmcneill 		return;
2022824038dSjmcneill 
2032824038dSjmcneill 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0 || size == 0)
2042824038dSjmcneill 		return;
2052824038dSjmcneill 
2062824038dSjmcneill 	if (of_getprop_uint32(phandle, "width", &width) != 0 ||
2072824038dSjmcneill 	    of_getprop_uint32(phandle, "height", &height) != 0 ||
2082824038dSjmcneill 	    of_getprop_uint32(phandle, "stride", &stride) != 0 ||
2092824038dSjmcneill 	    (format = fdtbus_get_string(phandle, "format")) == NULL)
2102824038dSjmcneill 		return;
2112824038dSjmcneill 
2122824038dSjmcneill 	if (width == 0 || height == 0)
2132824038dSjmcneill 		return;
2142824038dSjmcneill 
2152824038dSjmcneill 	if (strcmp(format, "a8b8g8r8") == 0 ||
2162824038dSjmcneill 	    strcmp(format, "x8r8g8b8") == 0) {
2172824038dSjmcneill 		depth = 32;
2189047c5eaSrin 	} else if (strcmp(format, "r8g8b8a8") == 0 ||
2199047c5eaSrin 		   strcmp(format, "b8g8r8x8") == 0) {
2209047c5eaSrin 		depth = 32;
2219047c5eaSrin 		swapped = true;
222e69e27c5Sjmcneill 	} else if (strcmp(format, "x2r10g10b10") == 0) {
223e69e27c5Sjmcneill 		depth = 32;
224e69e27c5Sjmcneill 		is_10bit = true;
2252824038dSjmcneill 	} else if (strcmp(format, "r5g6b5") == 0) {
2262824038dSjmcneill 		depth = 16;
2272824038dSjmcneill 	} else {
2282824038dSjmcneill 		return;
2292824038dSjmcneill 	}
2302824038dSjmcneill 
23102b4ea34Sjmcneill 	/*
23202b4ea34Sjmcneill 	 * Make sure that the size of the linear FB mapping is big enough
23302b4ea34Sjmcneill 	 * to fit the requested screen dimensions.
23402b4ea34Sjmcneill 	 */
23502b4ea34Sjmcneill 	if (size < stride * height) {
236abed5772Sskrll 		return;
23702b4ea34Sjmcneill 	}
238abed5772Sskrll 
2392824038dSjmcneill 	if (bus_space_map(bst, addr, size,
2402824038dSjmcneill 	    BUS_SPACE_MAP_LINEAR | BUS_SPACE_MAP_PREFETCHABLE, &bsh) != 0)
2412824038dSjmcneill 		return;
2422824038dSjmcneill 
24336ef29e8Sjmcneill 	arm_simplefb_addr = addr;
24436ef29e8Sjmcneill 	arm_simplefb_size = size;
24536ef29e8Sjmcneill 	arm_simplefb_bsh = bsh;
24636ef29e8Sjmcneill 
2472824038dSjmcneill 	sc->sc_width = width;
2482824038dSjmcneill 	sc->sc_height = height;
2492824038dSjmcneill 	sc->sc_depth = depth;
2502824038dSjmcneill 	sc->sc_stride = stride;
2512824038dSjmcneill 	sc->sc_bits = bus_space_vaddr(bst, bsh);
2529047c5eaSrin 	sc->sc_swapped = swapped;
253e69e27c5Sjmcneill 	sc->sc_10bit = is_10bit;
2542824038dSjmcneill 
2552824038dSjmcneill 	wsfont_init();
2562824038dSjmcneill 
2572824038dSjmcneill 	arm_simplefb_accessops.ioctl = arm_simplefb_ioctl;
2582824038dSjmcneill 	arm_simplefb_accessops.mmap = arm_simplefb_mmap;
2592824038dSjmcneill 	arm_simplefb_accessops.pollc = arm_simplefb_pollc;
2602824038dSjmcneill 
261e79a9d7bSjmcneill 	vcons_earlyinit(&arm_simplefb_vcons_data, sc, &arm_simplefb_stdscreen,
2622824038dSjmcneill 	    &arm_simplefb_accessops);
2632824038dSjmcneill 	arm_simplefb_vcons_data.init_screen = arm_simplefb_init_screen;
2647472dc0dSjmcneill 	vcons_init_screen(&arm_simplefb_vcons_data, &arm_simplefb_screen, 1,
2657472dc0dSjmcneill 	    &defattr);
2662824038dSjmcneill 	arm_simplefb_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
2672824038dSjmcneill 
2682824038dSjmcneill 	if (ri->ri_rows < 1 || ri->ri_cols < 1)
2692824038dSjmcneill 		return;
2702824038dSjmcneill 
2712824038dSjmcneill 	arm_simplefb_stdscreen.nrows = ri->ri_rows;
2722824038dSjmcneill 	arm_simplefb_stdscreen.ncols = ri->ri_cols;
2732824038dSjmcneill 	arm_simplefb_stdscreen.textops = &ri->ri_ops;
2742824038dSjmcneill 	arm_simplefb_stdscreen.capabilities = ri->ri_caps;
2752824038dSjmcneill 
2762824038dSjmcneill 	wsdisplay_preattach(&arm_simplefb_stdscreen, ri, 0, 0, defattr);
2772824038dSjmcneill 
2782824038dSjmcneill 	vcons_replay_msgbuf(&arm_simplefb_screen);
2792824038dSjmcneill }
280