Home | History | Annotate | Line # | Download | only in fdt
plfb_fdt.c revision 1.5
      1  1.5   thorpej /* $NetBSD: plfb_fdt.c,v 1.5 2021/01/27 03:10:19 thorpej Exp $ */
      2  1.1  jmcneill 
      3  1.1  jmcneill /*-
      4  1.1  jmcneill  * Copyright (c) 2017 Jared McNeill <jmcneill (at) invisible.ca>
      5  1.1  jmcneill  * All rights reserved.
      6  1.1  jmcneill  *
      7  1.1  jmcneill  * Redistribution and use in source and binary forms, with or without
      8  1.1  jmcneill  * modification, are permitted provided that the following conditions
      9  1.1  jmcneill  * are met:
     10  1.1  jmcneill  * 1. Redistributions of source code must retain the above copyright
     11  1.1  jmcneill  *    notice, this list of conditions and the following disclaimer.
     12  1.1  jmcneill  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  jmcneill  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  jmcneill  *    documentation and/or other materials provided with the distribution.
     15  1.1  jmcneill  *
     16  1.1  jmcneill  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  1.1  jmcneill  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  1.1  jmcneill  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  1.1  jmcneill  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  1.1  jmcneill  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  1.1  jmcneill  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  1.1  jmcneill  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  1.1  jmcneill  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  1.1  jmcneill  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  1.1  jmcneill  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  1.1  jmcneill  * POSSIBILITY OF SUCH DAMAGE.
     27  1.1  jmcneill  */
     28  1.1  jmcneill 
     29  1.1  jmcneill /*
     30  1.1  jmcneill  * ARM PrimeCell PL111 framebuffer console driver
     31  1.1  jmcneill  */
     32  1.1  jmcneill 
     33  1.4  jmcneill #include "opt_wsdisplay_compat.h"
     34  1.4  jmcneill 
     35  1.1  jmcneill #include <sys/cdefs.h>
     36  1.5   thorpej __KERNEL_RCSID(0, "$NetBSD: plfb_fdt.c,v 1.5 2021/01/27 03:10:19 thorpej Exp $");
     37  1.1  jmcneill 
     38  1.1  jmcneill #include <sys/param.h>
     39  1.1  jmcneill #include <sys/types.h>
     40  1.1  jmcneill #include <sys/systm.h>
     41  1.1  jmcneill #include <sys/device.h>
     42  1.1  jmcneill #include <sys/conf.h>
     43  1.1  jmcneill #include <sys/bus.h>
     44  1.1  jmcneill #include <sys/kmem.h>
     45  1.1  jmcneill #include <sys/sysctl.h>
     46  1.1  jmcneill 
     47  1.1  jmcneill #include <dev/wsfb/genfbvar.h>
     48  1.1  jmcneill 
     49  1.1  jmcneill #include <dev/fdt/fdtvar.h>
     50  1.1  jmcneill #include <dev/fdt/display_timing.h>
     51  1.1  jmcneill 
     52  1.1  jmcneill #define	LCDTIMING0		0x000
     53  1.1  jmcneill #define	 LCDTIMING0_HBP		__BITS(31,24)
     54  1.1  jmcneill #define	 LCDTIMING0_HFP		__BITS(23,16)
     55  1.1  jmcneill #define	 LCDTIMING0_HSW		__BITS(15,8)
     56  1.1  jmcneill #define	 LCDTIMING0_PPL		__BITS(7,2)
     57  1.1  jmcneill #define	LCDTIMING1		0x004
     58  1.1  jmcneill #define	 LCDTIMING1_VBP		__BITS(31,24)
     59  1.1  jmcneill #define	 LCDTIMING1_VFP		__BITS(23,16)
     60  1.1  jmcneill #define	 LCDTIMING1_VSW		__BITS(15,10)
     61  1.1  jmcneill #define	 LCDTIMING1_LPP		__BITS(9,0)
     62  1.1  jmcneill #define	LCDUPBASE		0x010
     63  1.1  jmcneill #define	LCDLPBASE		0x014
     64  1.1  jmcneill #define	LCDCONTROL		0x018
     65  1.1  jmcneill #define	 LCDCONTROL_PWR		__BIT(11)
     66  1.1  jmcneill #define	 LCDCONTROL_BGR		__BIT(8)
     67  1.1  jmcneill #define	 LCDCONTROL_BPP		__BITS(3,1)
     68  1.1  jmcneill #define	  LCDCONTROL_BPP_24	__SHIFTIN(5, LCDCONTROL_BPP)
     69  1.1  jmcneill #define	 LCDCONTROL_EN		__BIT(0)
     70  1.1  jmcneill 
     71  1.1  jmcneill #define	PLFB_BPP		32
     72  1.1  jmcneill 
     73  1.2  jmcneill static int plfb_console_phandle = -1;
     74  1.2  jmcneill 
     75  1.1  jmcneill struct plfb_softc {
     76  1.1  jmcneill 	struct genfb_softc	sc_gen;
     77  1.1  jmcneill 	bus_space_tag_t		sc_bst;
     78  1.1  jmcneill 	bus_space_handle_t	sc_bsh;
     79  1.1  jmcneill 	int			sc_phandle;
     80  1.1  jmcneill 
     81  1.1  jmcneill 	bus_space_handle_t	sc_vram_bsh;
     82  1.1  jmcneill 	bus_addr_t		sc_vram_addr;
     83  1.1  jmcneill 	bus_size_t		sc_vram_size;
     84  1.1  jmcneill 	uintptr_t		sc_vram;
     85  1.1  jmcneill 
     86  1.1  jmcneill 	uint32_t		sc_wstype;
     87  1.1  jmcneill };
     88  1.1  jmcneill 
     89  1.1  jmcneill static int	plfb_match(device_t, cfdata_t, void *);
     90  1.1  jmcneill static void	plfb_attach(device_t, device_t, void *);
     91  1.1  jmcneill 
     92  1.1  jmcneill static int	plfb_ioctl(void *, void *, u_long, void *, int, lwp_t *);
     93  1.1  jmcneill static paddr_t	plfb_mmap(void *, void *, off_t, int);
     94  1.1  jmcneill static bool	plfb_shutdown(device_t, int);
     95  1.1  jmcneill 
     96  1.1  jmcneill static void	plfb_init(struct plfb_softc *);
     97  1.1  jmcneill 
     98  1.5   thorpej static const struct device_compatible_entry compat_data[] = {
     99  1.5   thorpej 	{ .compat = "arm,pl111" },
    100  1.5   thorpej 	DEVICE_COMPAT_EOL
    101  1.1  jmcneill };
    102  1.1  jmcneill 
    103  1.1  jmcneill CFATTACH_DECL_NEW(plfb_fdt, sizeof(struct plfb_softc),
    104  1.1  jmcneill     plfb_match, plfb_attach, NULL, NULL);
    105  1.1  jmcneill 
    106  1.1  jmcneill #define	FB_READ(sc, reg)	\
    107  1.1  jmcneill 	bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
    108  1.1  jmcneill #define	FB_WRITE(sc, reg, val)	\
    109  1.1  jmcneill 	bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
    110  1.1  jmcneill 
    111  1.1  jmcneill static int
    112  1.1  jmcneill plfb_match(device_t parent, cfdata_t match, void *aux)
    113  1.1  jmcneill {
    114  1.1  jmcneill 	struct fdt_attach_args * const faa = aux;
    115  1.1  jmcneill 
    116  1.5   thorpej 	return of_compatible_match(faa->faa_phandle, compat_data);
    117  1.1  jmcneill }
    118  1.1  jmcneill 
    119  1.1  jmcneill static void
    120  1.1  jmcneill plfb_attach(device_t parent, device_t self, void *aux)
    121  1.1  jmcneill {
    122  1.1  jmcneill 	struct plfb_softc *sc = device_private(self);
    123  1.1  jmcneill 	prop_dictionary_t dict = device_properties(self);
    124  1.1  jmcneill 	struct fdt_attach_args * const faa = aux;
    125  1.1  jmcneill 	const int phandle = faa->faa_phandle;
    126  1.1  jmcneill 	struct genfb_ops ops;
    127  1.1  jmcneill 	struct clk *clk;
    128  1.1  jmcneill 	bus_addr_t addr;
    129  1.1  jmcneill 	bus_size_t size;
    130  1.1  jmcneill 
    131  1.1  jmcneill 	sc->sc_gen.sc_dev = self;
    132  1.1  jmcneill 	sc->sc_phandle = phandle;
    133  1.1  jmcneill 	sc->sc_bst = faa->faa_bst;
    134  1.1  jmcneill 
    135  1.1  jmcneill 	/* Enable clocks */
    136  1.1  jmcneill 	for (int i = 0; (clk = fdtbus_clock_get_index(phandle, i)); i++)
    137  1.1  jmcneill 		if (clk_enable(clk) != 0) {
    138  1.1  jmcneill 			aprint_error(": couldn't enable clock #%d\n", i);
    139  1.1  jmcneill 			return;
    140  1.1  jmcneill 		}
    141  1.1  jmcneill 
    142  1.1  jmcneill 	/* Map CLCD registers */
    143  1.1  jmcneill 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
    144  1.1  jmcneill 		aprint_error(": missing 'reg' property\n");
    145  1.1  jmcneill 		return;
    146  1.1  jmcneill 	}
    147  1.1  jmcneill 	if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh)) {
    148  1.1  jmcneill 		aprint_error(": couldn't map device\n");
    149  1.1  jmcneill 		return;
    150  1.1  jmcneill 	}
    151  1.1  jmcneill 
    152  1.1  jmcneill 	/* Map VRAM */
    153  1.1  jmcneill 	const int vram_phandle = fdtbus_get_phandle(phandle, "memory-region");
    154  1.1  jmcneill 	if (vram_phandle == -1) {
    155  1.1  jmcneill 		/*
    156  1.1  jmcneill 		 * The 'memory-region' property is optional. If
    157  1.1  jmcneill 		 * absent, we can allocate FB from main RAM. (TODO)
    158  1.1  jmcneill 		 */
    159  1.1  jmcneill 		aprint_error(": missing 'memory-region' property\n");
    160  1.1  jmcneill 		return;
    161  1.1  jmcneill 	}
    162  1.1  jmcneill 	if (fdtbus_get_reg(vram_phandle, 0, &sc->sc_vram_addr,
    163  1.1  jmcneill 	    &sc->sc_vram_size) != 0) {
    164  1.1  jmcneill 		aprint_error(": missing 'reg' property on memory-region\n");
    165  1.1  jmcneill 		return;
    166  1.1  jmcneill 	}
    167  1.1  jmcneill 	if (bus_space_map(sc->sc_bst, sc->sc_vram_addr, sc->sc_vram_size,
    168  1.1  jmcneill 	    BUS_SPACE_MAP_LINEAR, &sc->sc_vram_bsh)) {
    169  1.1  jmcneill 		aprint_error(": couldn't map vram\n");
    170  1.1  jmcneill 		return;
    171  1.1  jmcneill 	}
    172  1.1  jmcneill 	sc->sc_vram = (uintptr_t)bus_space_vaddr(sc->sc_bst, sc->sc_vram_bsh);
    173  1.1  jmcneill 
    174  1.1  jmcneill 	plfb_init(sc);
    175  1.1  jmcneill 
    176  1.4  jmcneill 	aprint_naive("\n");
    177  1.4  jmcneill 	aprint_normal("\n");
    178  1.4  jmcneill 
    179  1.1  jmcneill 	sc->sc_wstype = WSDISPLAY_TYPE_PLFB;
    180  1.4  jmcneill 
    181  1.4  jmcneill #ifdef WSDISPLAY_MULTICONS
    182  1.4  jmcneill 	const bool is_console = true;
    183  1.4  jmcneill 	genfb_cnattach();
    184  1.4  jmcneill #else
    185  1.4  jmcneill 	const bool is_console = phandle == plfb_console_phandle;
    186  1.4  jmcneill 	if (is_console)
    187  1.4  jmcneill 		aprint_normal_dev(self, "switching to framebuffer console\n");
    188  1.4  jmcneill #endif
    189  1.4  jmcneill 
    190  1.4  jmcneill 	prop_dictionary_set_bool(dict, "is_console", is_console);
    191  1.1  jmcneill 
    192  1.1  jmcneill 	genfb_init(&sc->sc_gen);
    193  1.1  jmcneill 
    194  1.1  jmcneill 	if (sc->sc_gen.sc_width == 0 ||
    195  1.1  jmcneill 	    sc->sc_gen.sc_fbsize == 0) {
    196  1.4  jmcneill 		aprint_normal_dev(self, "disabled\n");
    197  1.1  jmcneill 		return;
    198  1.1  jmcneill 	}
    199  1.1  jmcneill 
    200  1.1  jmcneill 	pmf_device_register1(self, NULL, NULL, plfb_shutdown);
    201  1.1  jmcneill 
    202  1.1  jmcneill 	memset(&ops, 0, sizeof(ops));
    203  1.1  jmcneill 	ops.genfb_ioctl = plfb_ioctl;
    204  1.1  jmcneill 	ops.genfb_mmap = plfb_mmap;
    205  1.1  jmcneill 
    206  1.1  jmcneill 	genfb_attach(&sc->sc_gen, &ops);
    207  1.1  jmcneill }
    208  1.1  jmcneill 
    209  1.1  jmcneill static int
    210  1.1  jmcneill plfb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag, lwp_t *l)
    211  1.1  jmcneill {
    212  1.1  jmcneill 	struct plfb_softc *sc = v;
    213  1.1  jmcneill 	struct wsdisplayio_bus_id *busid;
    214  1.1  jmcneill 
    215  1.1  jmcneill 	switch (cmd) {
    216  1.1  jmcneill 	case WSDISPLAYIO_GTYPE:
    217  1.1  jmcneill 		*(u_int *)data = sc->sc_wstype;
    218  1.1  jmcneill 		return 0;
    219  1.1  jmcneill 	case WSDISPLAYIO_GET_BUSID:
    220  1.1  jmcneill 		busid = data;
    221  1.1  jmcneill 		busid->bus_type = WSDISPLAYIO_BUS_SOC;
    222  1.1  jmcneill 		return 0;
    223  1.1  jmcneill 	case WSDISPLAYIO_GET_FBINFO:
    224  1.1  jmcneill 		{
    225  1.1  jmcneill 			struct wsdisplayio_fbinfo *fbi = data;
    226  1.1  jmcneill 			struct rasops_info *ri = &sc->sc_gen.vd.active->scr_ri;
    227  1.1  jmcneill 
    228  1.1  jmcneill 			return wsdisplayio_get_fbinfo(ri, fbi);
    229  1.1  jmcneill 		}
    230  1.1  jmcneill 	default:
    231  1.1  jmcneill 		return EPASSTHROUGH;
    232  1.1  jmcneill 	}
    233  1.1  jmcneill }
    234  1.1  jmcneill 
    235  1.1  jmcneill static paddr_t
    236  1.1  jmcneill plfb_mmap(void *v, void *vs, off_t offset, int prot)
    237  1.1  jmcneill {
    238  1.1  jmcneill 	struct plfb_softc *sc = v;
    239  1.1  jmcneill 
    240  1.1  jmcneill 	if (offset < 0 || offset >= sc->sc_vram_size)
    241  1.1  jmcneill 		return -1;
    242  1.1  jmcneill 
    243  1.1  jmcneill 	return bus_space_mmap(sc->sc_bst, sc->sc_vram_addr, offset, prot,
    244  1.1  jmcneill 	    BUS_SPACE_MAP_LINEAR | BUS_SPACE_MAP_PREFETCHABLE);
    245  1.1  jmcneill }
    246  1.1  jmcneill 
    247  1.1  jmcneill static bool
    248  1.1  jmcneill plfb_shutdown(device_t self, int flags)
    249  1.1  jmcneill {
    250  1.1  jmcneill 	genfb_enable_polling(self);
    251  1.1  jmcneill 	return true;
    252  1.1  jmcneill }
    253  1.1  jmcneill 
    254  1.1  jmcneill static int
    255  1.1  jmcneill plfb_get_panel_timing(struct plfb_softc *sc, struct display_timing *timing)
    256  1.1  jmcneill {
    257  1.1  jmcneill 	int panel, panel_timing;
    258  1.1  jmcneill 
    259  1.1  jmcneill 	panel = of_find_firstchild_byname(sc->sc_phandle, "panel");
    260  1.1  jmcneill 	if (panel <= 0)
    261  1.1  jmcneill 		return ENOENT;
    262  1.1  jmcneill 	panel_timing = of_find_firstchild_byname(panel, "panel-timing");
    263  1.1  jmcneill 	if (panel_timing <= 0)
    264  1.1  jmcneill 		return ENOENT;
    265  1.1  jmcneill 
    266  1.1  jmcneill 	return display_timing_parse(panel_timing, timing);
    267  1.1  jmcneill }
    268  1.1  jmcneill 
    269  1.1  jmcneill static void
    270  1.1  jmcneill plfb_init(struct plfb_softc *sc)
    271  1.1  jmcneill {
    272  1.1  jmcneill 	prop_dictionary_t dict = device_properties(sc->sc_gen.sc_dev);
    273  1.1  jmcneill 	struct display_timing timing;
    274  1.1  jmcneill 
    275  1.1  jmcneill 	if (plfb_get_panel_timing(sc, &timing) != 0) {
    276  1.3  jmcneill 		/* No timings specified in DT, assume 800x600 */
    277  1.3  jmcneill 		timing.hactive = 800;
    278  1.3  jmcneill 		timing.hback_porch = 128;
    279  1.3  jmcneill 		timing.hfront_porch = 24;
    280  1.3  jmcneill 		timing.hsync_len = 72;
    281  1.3  jmcneill 		timing.vactive = 600;
    282  1.3  jmcneill 		timing.vback_porch = 22;
    283  1.3  jmcneill 		timing.vfront_porch = 1;
    284  1.3  jmcneill 		timing.vsync_len = 2;
    285  1.1  jmcneill 	}
    286  1.1  jmcneill 
    287  1.1  jmcneill 	prop_dictionary_set_uint32(dict, "width", timing.hactive);
    288  1.1  jmcneill 	prop_dictionary_set_uint32(dict, "height", timing.vactive);
    289  1.1  jmcneill 	prop_dictionary_set_uint8(dict, "depth", PLFB_BPP);
    290  1.1  jmcneill 	prop_dictionary_set_bool(dict, "dblscan", 0);
    291  1.1  jmcneill 	prop_dictionary_set_bool(dict, "interlace", 0);
    292  1.1  jmcneill 	prop_dictionary_set_uint16(dict, "linebytes", timing.hactive * (PLFB_BPP / 8));
    293  1.1  jmcneill 	prop_dictionary_set_uint32(dict, "address", sc->sc_vram_addr);
    294  1.1  jmcneill 	prop_dictionary_set_uint32(dict, "virtual_address", sc->sc_vram);
    295  1.1  jmcneill 
    296  1.1  jmcneill 	/* FB base address */
    297  1.1  jmcneill 	FB_WRITE(sc, LCDUPBASE, sc->sc_vram_addr);
    298  1.1  jmcneill 	FB_WRITE(sc, LCDLPBASE, 0);
    299  1.1  jmcneill 
    300  1.1  jmcneill 	/* CRTC timings */
    301  1.1  jmcneill 	FB_WRITE(sc, LCDTIMING0,
    302  1.1  jmcneill 	    __SHIFTIN(timing.hback_porch - 1, LCDTIMING0_HBP) |
    303  1.1  jmcneill 	    __SHIFTIN(timing.hfront_porch - 1, LCDTIMING0_HFP) |
    304  1.1  jmcneill 	    __SHIFTIN(timing.hsync_len - 1, LCDTIMING0_HSW) |
    305  1.1  jmcneill 	    __SHIFTIN((timing.hactive / 16) - 1, LCDTIMING0_PPL));
    306  1.1  jmcneill 	FB_WRITE(sc, LCDTIMING1,
    307  1.1  jmcneill 	    __SHIFTIN(timing.vback_porch - 1, LCDTIMING1_VBP) |
    308  1.1  jmcneill 	    __SHIFTIN(timing.vfront_porch - 1, LCDTIMING1_VFP) |
    309  1.1  jmcneill 	    __SHIFTIN(timing.vsync_len - 1, LCDTIMING1_VSW) |
    310  1.1  jmcneill 	    __SHIFTIN(timing.vactive - 1, LCDTIMING1_LPP));
    311  1.1  jmcneill 
    312  1.1  jmcneill 	/* Configure and enable CLCD */
    313  1.1  jmcneill 	FB_WRITE(sc, LCDCONTROL,
    314  1.1  jmcneill 	    LCDCONTROL_PWR | LCDCONTROL_EN | LCDCONTROL_BPP_24 |
    315  1.1  jmcneill 	    LCDCONTROL_BGR);
    316  1.1  jmcneill }
    317  1.2  jmcneill 
    318  1.2  jmcneill static int
    319  1.2  jmcneill plfb_console_match(int phandle)
    320  1.2  jmcneill {
    321  1.5   thorpej 	return of_compatible_match(phandle, compat_data);
    322  1.2  jmcneill }
    323  1.2  jmcneill 
    324  1.2  jmcneill static void
    325  1.2  jmcneill plfb_console_consinit(struct fdt_attach_args *faa, u_int uart_freq)
    326  1.2  jmcneill {
    327  1.2  jmcneill 	plfb_console_phandle = faa->faa_phandle;
    328  1.2  jmcneill 	genfb_cnattach();
    329  1.2  jmcneill }
    330  1.2  jmcneill 
    331  1.2  jmcneill static const struct fdt_console plfb_fdt_console = {
    332  1.2  jmcneill 	.match = plfb_console_match,
    333  1.2  jmcneill 	.consinit = plfb_console_consinit
    334  1.2  jmcneill };
    335  1.2  jmcneill 
    336  1.2  jmcneill FDT_CONSOLE(plfb, &plfb_fdt_console);
    337