Home | History | Annotate | Line # | Download | only in fdt
arm_simplefb.c revision 1.8
      1  1.8   thorpej /* $NetBSD: arm_simplefb.c,v 1.8 2021/01/27 03:10:19 thorpej Exp $ */
      2  1.1  jmcneill 
      3  1.1  jmcneill /*-
      4  1.1  jmcneill  * Copyright (c) 2019 The NetBSD Foundation, Inc.
      5  1.1  jmcneill  * All rights reserved.
      6  1.1  jmcneill  *
      7  1.1  jmcneill  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1  jmcneill  * by Jared McNeill <jmcneill (at) invisible.ca>.
      9  1.1  jmcneill  *
     10  1.1  jmcneill  * Redistribution and use in source and binary forms, with or without
     11  1.1  jmcneill  * modification, are permitted provided that the following conditions
     12  1.1  jmcneill  * are met:
     13  1.1  jmcneill  * 1. Redistributions of source code must retain the above copyright
     14  1.1  jmcneill  *    notice, this list of conditions and the following disclaimer.
     15  1.1  jmcneill  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  jmcneill  *    notice, this list of conditions and the following disclaimer in the
     17  1.1  jmcneill  *    documentation and/or other materials provided with the distribution.
     18  1.1  jmcneill  *
     19  1.1  jmcneill  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.1  jmcneill  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.1  jmcneill  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.1  jmcneill  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.1  jmcneill  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.1  jmcneill  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.1  jmcneill  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.1  jmcneill  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.1  jmcneill  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.1  jmcneill  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.1  jmcneill  * POSSIBILITY OF SUCH DAMAGE.
     30  1.1  jmcneill  */
     31  1.1  jmcneill 
     32  1.1  jmcneill #include "pci.h"
     33  1.1  jmcneill #include "opt_pci.h"
     34  1.5  jmcneill #include "opt_vcons.h"
     35  1.1  jmcneill 
     36  1.1  jmcneill #include <sys/cdefs.h>
     37  1.8   thorpej __KERNEL_RCSID(0, "$NetBSD: arm_simplefb.c,v 1.8 2021/01/27 03:10:19 thorpej Exp $");
     38  1.1  jmcneill 
     39  1.1  jmcneill #include <sys/param.h>
     40  1.1  jmcneill #include <sys/bus.h>
     41  1.1  jmcneill #include <sys/cpu.h>
     42  1.1  jmcneill #include <sys/device.h>
     43  1.1  jmcneill 
     44  1.1  jmcneill #include <dev/fdt/fdtvar.h>
     45  1.1  jmcneill #include <arm/fdt/arm_fdtvar.h>
     46  1.1  jmcneill 
     47  1.1  jmcneill #include <dev/wscons/wsconsio.h>
     48  1.1  jmcneill #include <dev/wscons/wsdisplayvar.h>
     49  1.1  jmcneill #include <dev/rasops/rasops.h>
     50  1.1  jmcneill #include <dev/wsfont/wsfont.h>
     51  1.1  jmcneill #include <dev/wscons/wsdisplay_vconsvar.h>
     52  1.1  jmcneill 
     53  1.1  jmcneill #if NPCI > 0 && defined(PCI_NETBSD_CONFIGURE)
     54  1.1  jmcneill #include <dev/pci/pcireg.h>
     55  1.1  jmcneill #include <dev/pci/pcivar.h>
     56  1.1  jmcneill #include <dev/pci/pciconf.h>
     57  1.1  jmcneill #endif
     58  1.1  jmcneill 
     59  1.1  jmcneill #include <arm/fdt/arm_simplefb.h>
     60  1.1  jmcneill 
     61  1.1  jmcneill #include <libfdt.h>
     62  1.1  jmcneill 
     63  1.1  jmcneill extern struct bus_space arm_generic_bs_tag;
     64  1.1  jmcneill 
     65  1.1  jmcneill static struct arm_simplefb_softc {
     66  1.1  jmcneill 	uint32_t	sc_width;
     67  1.1  jmcneill 	uint32_t	sc_height;
     68  1.1  jmcneill 	uint32_t	sc_stride;
     69  1.1  jmcneill 	uint16_t	sc_depth;
     70  1.2       rin 	bool		sc_swapped;
     71  1.1  jmcneill 	void		*sc_bits;
     72  1.1  jmcneill } arm_simplefb_softc;
     73  1.1  jmcneill 
     74  1.1  jmcneill static struct wsscreen_descr arm_simplefb_stdscreen = {
     75  1.1  jmcneill 	.name = "std",
     76  1.1  jmcneill 	.ncols = 0,
     77  1.1  jmcneill 	.nrows = 0,
     78  1.1  jmcneill 	.textops = NULL,
     79  1.1  jmcneill 	.fontwidth = 0,
     80  1.1  jmcneill 	.fontheight = 0,
     81  1.1  jmcneill 	.capabilities = 0,
     82  1.1  jmcneill 	.modecookie = NULL
     83  1.1  jmcneill };
     84  1.1  jmcneill 
     85  1.1  jmcneill static struct wsdisplay_accessops arm_simplefb_accessops;
     86  1.1  jmcneill static struct vcons_data arm_simplefb_vcons_data;
     87  1.1  jmcneill static struct vcons_screen arm_simplefb_screen;
     88  1.1  jmcneill 
     89  1.3  jmcneill static bus_addr_t arm_simplefb_addr;
     90  1.3  jmcneill static bus_size_t arm_simplefb_size;
     91  1.3  jmcneill static bus_space_handle_t arm_simplefb_bsh;
     92  1.3  jmcneill 
     93  1.8   thorpej static const struct device_compatible_entry compat_data[] = {
     94  1.8   thorpej 	{ .compat = "simple-framebuffer" },
     95  1.8   thorpej 	DEVICE_COMPAT_EOL
     96  1.8   thorpej };
     97  1.8   thorpej 
     98  1.1  jmcneill static int
     99  1.1  jmcneill arm_simplefb_find_node(void)
    100  1.1  jmcneill {
    101  1.1  jmcneill 	int chosen_phandle, child;
    102  1.1  jmcneill 
    103  1.1  jmcneill 	chosen_phandle = OF_finddevice("/chosen");
    104  1.1  jmcneill 	if (chosen_phandle == -1)
    105  1.1  jmcneill 		return -1;
    106  1.1  jmcneill 
    107  1.1  jmcneill 	for (child = OF_child(chosen_phandle); child; child = OF_peer(child)) {
    108  1.1  jmcneill 		if (!fdtbus_status_okay(child))
    109  1.1  jmcneill 			continue;
    110  1.8   thorpej 		if (!of_compatible_match(child, compat_data))
    111  1.1  jmcneill 			continue;
    112  1.1  jmcneill 
    113  1.1  jmcneill 		return child;
    114  1.1  jmcneill 	}
    115  1.1  jmcneill 
    116  1.1  jmcneill 	return -1;
    117  1.1  jmcneill }
    118  1.1  jmcneill 
    119  1.1  jmcneill static void
    120  1.1  jmcneill arm_simplefb_init_screen(void *cookie, struct vcons_screen *scr,
    121  1.1  jmcneill     int existing, long *defattr)
    122  1.1  jmcneill {
    123  1.1  jmcneill 	struct arm_simplefb_softc * const sc = cookie;
    124  1.1  jmcneill 	struct rasops_info *ri = &scr->scr_ri;
    125  1.1  jmcneill 
    126  1.1  jmcneill 	ri->ri_width = sc->sc_width;
    127  1.1  jmcneill 	ri->ri_height = sc->sc_height;
    128  1.1  jmcneill 	ri->ri_depth = sc->sc_depth;
    129  1.1  jmcneill 	ri->ri_stride = sc->sc_stride;
    130  1.1  jmcneill 	ri->ri_bits = sc->sc_bits;
    131  1.1  jmcneill 	ri->ri_flg = RI_CENTER | RI_FULLCLEAR | RI_CLEAR;
    132  1.1  jmcneill 
    133  1.2       rin 	if (sc->sc_swapped) {
    134  1.2       rin 		KASSERT(ri->ri_depth == 32);
    135  1.2       rin 		ri->ri_rnum = ri->ri_gnum = ri->ri_bnum = 8;
    136  1.2       rin 		ri->ri_rpos =  8;
    137  1.2       rin 		ri->ri_gpos = 16;
    138  1.2       rin 		ri->ri_bpos = 24;
    139  1.2       rin 	}
    140  1.2       rin 
    141  1.1  jmcneill 	scr->scr_flags |= VCONS_LOADFONT;
    142  1.1  jmcneill 	scr->scr_flags |= VCONS_DONT_READ;
    143  1.1  jmcneill 
    144  1.1  jmcneill 	rasops_init(ri, ri->ri_height / 8, ri->ri_width / 8);
    145  1.1  jmcneill 	ri->ri_caps = WSSCREEN_WSCOLORS | WSSCREEN_HILIT | WSSCREEN_UNDERLINE;
    146  1.1  jmcneill 	rasops_reconfig(ri, sc->sc_height / ri->ri_font->fontheight,
    147  1.1  jmcneill 	    sc->sc_width / ri->ri_font->fontwidth);
    148  1.1  jmcneill 
    149  1.1  jmcneill 	ri->ri_hw = scr;
    150  1.1  jmcneill }
    151  1.1  jmcneill 
    152  1.1  jmcneill static int
    153  1.7  jmcneill arm_simplefb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
    154  1.7  jmcneill     lwp_t *l)
    155  1.1  jmcneill {
    156  1.1  jmcneill 	return EPASSTHROUGH;
    157  1.1  jmcneill }
    158  1.1  jmcneill 
    159  1.1  jmcneill static paddr_t
    160  1.1  jmcneill arm_simplefb_mmap(void *v, void *vs, off_t offset, int prot)
    161  1.1  jmcneill {
    162  1.1  jmcneill 	return -1;
    163  1.1  jmcneill }
    164  1.1  jmcneill 
    165  1.1  jmcneill static void
    166  1.1  jmcneill arm_simplefb_pollc(void *v, int on)
    167  1.1  jmcneill {
    168  1.1  jmcneill }
    169  1.1  jmcneill 
    170  1.4       rin #if NPCI > 0 && defined(PCI_NETBSD_CONFIGURE)
    171  1.3  jmcneill static void
    172  1.3  jmcneill arm_simplefb_reconfig(void *arg, uint64_t new_addr)
    173  1.3  jmcneill {
    174  1.3  jmcneill 	struct arm_simplefb_softc * const sc = &arm_simplefb_softc;
    175  1.3  jmcneill 	struct rasops_info *ri = &arm_simplefb_screen.scr_ri;
    176  1.3  jmcneill 	bus_space_tag_t bst = &arm_generic_bs_tag;
    177  1.3  jmcneill 
    178  1.3  jmcneill 	bus_space_unmap(bst, arm_simplefb_bsh, arm_simplefb_size);
    179  1.3  jmcneill 	bus_space_map(bst, new_addr, arm_simplefb_size,
    180  1.7  jmcneill 	    BUS_SPACE_MAP_LINEAR | BUS_SPACE_MAP_PREFETCHABLE,
    181  1.7  jmcneill 	    &arm_simplefb_bsh);
    182  1.3  jmcneill 
    183  1.3  jmcneill 	sc->sc_bits = bus_space_vaddr(bst, arm_simplefb_bsh);
    184  1.3  jmcneill 	ri->ri_bits = sc->sc_bits;
    185  1.3  jmcneill 
    186  1.3  jmcneill 	arm_simplefb_addr = (bus_addr_t)new_addr;
    187  1.3  jmcneill }
    188  1.4       rin #endif
    189  1.3  jmcneill 
    190  1.3  jmcneill uint64_t
    191  1.3  jmcneill arm_simplefb_physaddr(void)
    192  1.3  jmcneill {
    193  1.3  jmcneill 	return arm_simplefb_addr;
    194  1.3  jmcneill }
    195  1.3  jmcneill 
    196  1.1  jmcneill void
    197  1.1  jmcneill arm_simplefb_preattach(void)
    198  1.1  jmcneill {
    199  1.1  jmcneill 	struct arm_simplefb_softc * const sc = &arm_simplefb_softc;
    200  1.1  jmcneill 	struct rasops_info *ri = &arm_simplefb_screen.scr_ri;
    201  1.1  jmcneill 	bus_space_tag_t bst = &arm_generic_bs_tag;
    202  1.1  jmcneill 	bus_space_handle_t bsh;
    203  1.1  jmcneill 	uint32_t width, height, stride;
    204  1.1  jmcneill 	const char *format;
    205  1.1  jmcneill 	bus_addr_t addr;
    206  1.1  jmcneill 	bus_size_t size;
    207  1.1  jmcneill 	uint16_t depth;
    208  1.1  jmcneill 	long defattr;
    209  1.2       rin 	bool swapped = false;
    210  1.1  jmcneill 
    211  1.1  jmcneill 	const int phandle = arm_simplefb_find_node();
    212  1.1  jmcneill 	if (phandle == -1)
    213  1.1  jmcneill 		return;
    214  1.1  jmcneill 
    215  1.1  jmcneill 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0 || size == 0)
    216  1.1  jmcneill 		return;
    217  1.1  jmcneill 
    218  1.1  jmcneill 	if (of_getprop_uint32(phandle, "width", &width) != 0 ||
    219  1.1  jmcneill 	    of_getprop_uint32(phandle, "height", &height) != 0 ||
    220  1.1  jmcneill 	    of_getprop_uint32(phandle, "stride", &stride) != 0 ||
    221  1.1  jmcneill 	    (format = fdtbus_get_string(phandle, "format")) == NULL)
    222  1.1  jmcneill 		return;
    223  1.1  jmcneill 
    224  1.1  jmcneill 	if (width == 0 || height == 0)
    225  1.1  jmcneill 		return;
    226  1.1  jmcneill 
    227  1.1  jmcneill 	if (strcmp(format, "a8b8g8r8") == 0 ||
    228  1.1  jmcneill 	    strcmp(format, "x8r8g8b8") == 0) {
    229  1.1  jmcneill 		depth = 32;
    230  1.2       rin 	} else if (strcmp(format, "r8g8b8a8") == 0 ||
    231  1.2       rin 		   strcmp(format, "b8g8r8x8") == 0) {
    232  1.2       rin 		depth = 32;
    233  1.2       rin 		swapped = true;
    234  1.1  jmcneill 	} else if (strcmp(format, "r5g6b5") == 0) {
    235  1.1  jmcneill 		depth = 16;
    236  1.1  jmcneill 	} else {
    237  1.1  jmcneill 		return;
    238  1.1  jmcneill 	}
    239  1.1  jmcneill 
    240  1.1  jmcneill 	if (bus_space_map(bst, addr, size,
    241  1.1  jmcneill 	    BUS_SPACE_MAP_LINEAR | BUS_SPACE_MAP_PREFETCHABLE, &bsh) != 0)
    242  1.1  jmcneill 		return;
    243  1.1  jmcneill 
    244  1.3  jmcneill 	arm_simplefb_addr = addr;
    245  1.3  jmcneill 	arm_simplefb_size = size;
    246  1.3  jmcneill 	arm_simplefb_bsh = bsh;
    247  1.3  jmcneill 
    248  1.1  jmcneill 	sc->sc_width = width;
    249  1.1  jmcneill 	sc->sc_height = height;
    250  1.1  jmcneill 	sc->sc_depth = depth;
    251  1.1  jmcneill 	sc->sc_stride = stride;
    252  1.1  jmcneill 	sc->sc_bits = bus_space_vaddr(bst, bsh);
    253  1.2       rin 	sc->sc_swapped = swapped;
    254  1.1  jmcneill 
    255  1.1  jmcneill 	wsfont_init();
    256  1.1  jmcneill 
    257  1.1  jmcneill 	arm_simplefb_accessops.ioctl = arm_simplefb_ioctl;
    258  1.1  jmcneill 	arm_simplefb_accessops.mmap = arm_simplefb_mmap;
    259  1.1  jmcneill 	arm_simplefb_accessops.pollc = arm_simplefb_pollc;
    260  1.1  jmcneill 
    261  1.6  jmcneill 	vcons_earlyinit(&arm_simplefb_vcons_data, sc, &arm_simplefb_stdscreen,
    262  1.6  jmcneill 	    &arm_simplefb_accessops);
    263  1.1  jmcneill 	arm_simplefb_vcons_data.init_screen = arm_simplefb_init_screen;
    264  1.5  jmcneill #ifdef VCONS_DRAW_INTR
    265  1.1  jmcneill 	arm_simplefb_vcons_data.use_intr = 0;
    266  1.5  jmcneill #endif
    267  1.7  jmcneill 	vcons_init_screen(&arm_simplefb_vcons_data, &arm_simplefb_screen, 1,
    268  1.7  jmcneill 	    &defattr);
    269  1.5  jmcneill 	arm_simplefb_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
    270  1.1  jmcneill 
    271  1.1  jmcneill 	if (ri->ri_rows < 1 || ri->ri_cols < 1)
    272  1.1  jmcneill 		return;
    273  1.1  jmcneill 
    274  1.1  jmcneill 	arm_simplefb_stdscreen.nrows = ri->ri_rows;
    275  1.1  jmcneill 	arm_simplefb_stdscreen.ncols = ri->ri_cols;
    276  1.1  jmcneill 	arm_simplefb_stdscreen.textops = &ri->ri_ops;
    277  1.1  jmcneill 	arm_simplefb_stdscreen.capabilities = ri->ri_caps;
    278  1.1  jmcneill 
    279  1.1  jmcneill 	wsdisplay_preattach(&arm_simplefb_stdscreen, ri, 0, 0, defattr);
    280  1.1  jmcneill 
    281  1.1  jmcneill 	vcons_replay_msgbuf(&arm_simplefb_screen);
    282  1.1  jmcneill 
    283  1.1  jmcneill #if NPCI > 0 && defined(PCI_NETBSD_CONFIGURE)
    284  1.1  jmcneill 	/*
    285  1.1  jmcneill 	 * Let the PCI resource allocator know about our framebuffer. This
    286  1.3  jmcneill 	 * lets us know if the FB base address changes so we can remap the
    287  1.3  jmcneill 	 * framebuffer if necessary.
    288  1.1  jmcneill 	 */
    289  1.3  jmcneill 	pciconf_resource_reserve(PCI_CONF_MAP_MEM, addr, size,
    290  1.3  jmcneill 	    arm_simplefb_reconfig, NULL);
    291  1.1  jmcneill #endif
    292  1.1  jmcneill }
    293