Home | History | Annotate | Line # | Download | only in fdt
plfb_fdt.c revision 1.1
      1  1.1  jmcneill /* $NetBSD: plfb_fdt.c,v 1.1 2017/06/03 14:50:39 jmcneill 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.1  jmcneill #include <sys/cdefs.h>
     34  1.1  jmcneill __KERNEL_RCSID(0, "$NetBSD: plfb_fdt.c,v 1.1 2017/06/03 14:50:39 jmcneill Exp $");
     35  1.1  jmcneill 
     36  1.1  jmcneill #include <sys/param.h>
     37  1.1  jmcneill #include <sys/types.h>
     38  1.1  jmcneill #include <sys/systm.h>
     39  1.1  jmcneill #include <sys/device.h>
     40  1.1  jmcneill #include <sys/conf.h>
     41  1.1  jmcneill #include <sys/bus.h>
     42  1.1  jmcneill #include <sys/kmem.h>
     43  1.1  jmcneill #include <sys/sysctl.h>
     44  1.1  jmcneill 
     45  1.1  jmcneill #include <dev/wsfb/genfbvar.h>
     46  1.1  jmcneill 
     47  1.1  jmcneill #include <dev/fdt/fdtvar.h>
     48  1.1  jmcneill #include <dev/fdt/display_timing.h>
     49  1.1  jmcneill 
     50  1.1  jmcneill #define	LCDTIMING0		0x000
     51  1.1  jmcneill #define	 LCDTIMING0_HBP		__BITS(31,24)
     52  1.1  jmcneill #define	 LCDTIMING0_HFP		__BITS(23,16)
     53  1.1  jmcneill #define	 LCDTIMING0_HSW		__BITS(15,8)
     54  1.1  jmcneill #define	 LCDTIMING0_PPL		__BITS(7,2)
     55  1.1  jmcneill #define	LCDTIMING1		0x004
     56  1.1  jmcneill #define	 LCDTIMING1_VBP		__BITS(31,24)
     57  1.1  jmcneill #define	 LCDTIMING1_VFP		__BITS(23,16)
     58  1.1  jmcneill #define	 LCDTIMING1_VSW		__BITS(15,10)
     59  1.1  jmcneill #define	 LCDTIMING1_LPP		__BITS(9,0)
     60  1.1  jmcneill #define	LCDUPBASE		0x010
     61  1.1  jmcneill #define	LCDLPBASE		0x014
     62  1.1  jmcneill #define	LCDCONTROL		0x018
     63  1.1  jmcneill #define	 LCDCONTROL_PWR		__BIT(11)
     64  1.1  jmcneill #define	 LCDCONTROL_BGR		__BIT(8)
     65  1.1  jmcneill #define	 LCDCONTROL_BPP		__BITS(3,1)
     66  1.1  jmcneill #define	  LCDCONTROL_BPP_24	__SHIFTIN(5, LCDCONTROL_BPP)
     67  1.1  jmcneill #define	 LCDCONTROL_EN		__BIT(0)
     68  1.1  jmcneill 
     69  1.1  jmcneill #define	PLFB_BPP		32
     70  1.1  jmcneill 
     71  1.1  jmcneill struct plfb_softc {
     72  1.1  jmcneill 	struct genfb_softc	sc_gen;
     73  1.1  jmcneill 	bus_space_tag_t		sc_bst;
     74  1.1  jmcneill 	bus_space_handle_t	sc_bsh;
     75  1.1  jmcneill 	int			sc_phandle;
     76  1.1  jmcneill 
     77  1.1  jmcneill 	bus_space_handle_t	sc_vram_bsh;
     78  1.1  jmcneill 	bus_addr_t		sc_vram_addr;
     79  1.1  jmcneill 	bus_size_t		sc_vram_size;
     80  1.1  jmcneill 	uintptr_t		sc_vram;
     81  1.1  jmcneill 
     82  1.1  jmcneill 	uint32_t		sc_wstype;
     83  1.1  jmcneill };
     84  1.1  jmcneill 
     85  1.1  jmcneill static int	plfb_match(device_t, cfdata_t, void *);
     86  1.1  jmcneill static void	plfb_attach(device_t, device_t, void *);
     87  1.1  jmcneill 
     88  1.1  jmcneill static int	plfb_ioctl(void *, void *, u_long, void *, int, lwp_t *);
     89  1.1  jmcneill static paddr_t	plfb_mmap(void *, void *, off_t, int);
     90  1.1  jmcneill static bool	plfb_shutdown(device_t, int);
     91  1.1  jmcneill 
     92  1.1  jmcneill static void	plfb_init(struct plfb_softc *);
     93  1.1  jmcneill 
     94  1.1  jmcneill static const char * const compatible[] = {
     95  1.1  jmcneill 	"arm,pl111",
     96  1.1  jmcneill 	NULL
     97  1.1  jmcneill };
     98  1.1  jmcneill 
     99  1.1  jmcneill CFATTACH_DECL_NEW(plfb_fdt, sizeof(struct plfb_softc),
    100  1.1  jmcneill     plfb_match, plfb_attach, NULL, NULL);
    101  1.1  jmcneill 
    102  1.1  jmcneill #define	FB_READ(sc, reg)	\
    103  1.1  jmcneill 	bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
    104  1.1  jmcneill #define	FB_WRITE(sc, reg, val)	\
    105  1.1  jmcneill 	bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
    106  1.1  jmcneill 
    107  1.1  jmcneill static int
    108  1.1  jmcneill plfb_match(device_t parent, cfdata_t match, void *aux)
    109  1.1  jmcneill {
    110  1.1  jmcneill 	struct fdt_attach_args * const faa = aux;
    111  1.1  jmcneill 
    112  1.1  jmcneill 	return of_match_compatible(faa->faa_phandle, compatible);
    113  1.1  jmcneill }
    114  1.1  jmcneill 
    115  1.1  jmcneill static void
    116  1.1  jmcneill plfb_attach(device_t parent, device_t self, void *aux)
    117  1.1  jmcneill {
    118  1.1  jmcneill 	struct plfb_softc *sc = device_private(self);
    119  1.1  jmcneill 	prop_dictionary_t dict = device_properties(self);
    120  1.1  jmcneill 	struct fdt_attach_args * const faa = aux;
    121  1.1  jmcneill 	const int phandle = faa->faa_phandle;
    122  1.1  jmcneill 	struct genfb_ops ops;
    123  1.1  jmcneill 	struct clk *clk;
    124  1.1  jmcneill 	bus_addr_t addr;
    125  1.1  jmcneill 	bus_size_t size;
    126  1.1  jmcneill 
    127  1.1  jmcneill 	sc->sc_gen.sc_dev = self;
    128  1.1  jmcneill 	sc->sc_phandle = phandle;
    129  1.1  jmcneill 	sc->sc_bst = faa->faa_bst;
    130  1.1  jmcneill 
    131  1.1  jmcneill 	/* Enable clocks */
    132  1.1  jmcneill 	for (int i = 0; (clk = fdtbus_clock_get_index(phandle, i)); i++)
    133  1.1  jmcneill 		if (clk_enable(clk) != 0) {
    134  1.1  jmcneill 			aprint_error(": couldn't enable clock #%d\n", i);
    135  1.1  jmcneill 			return;
    136  1.1  jmcneill 		}
    137  1.1  jmcneill 
    138  1.1  jmcneill 	/* Map CLCD registers */
    139  1.1  jmcneill 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
    140  1.1  jmcneill 		aprint_error(": missing 'reg' property\n");
    141  1.1  jmcneill 		return;
    142  1.1  jmcneill 	}
    143  1.1  jmcneill 	if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh)) {
    144  1.1  jmcneill 		aprint_error(": couldn't map device\n");
    145  1.1  jmcneill 		return;
    146  1.1  jmcneill 	}
    147  1.1  jmcneill 
    148  1.1  jmcneill 	/* Map VRAM */
    149  1.1  jmcneill 	const int vram_phandle = fdtbus_get_phandle(phandle, "memory-region");
    150  1.1  jmcneill 	if (vram_phandle == -1) {
    151  1.1  jmcneill 		/*
    152  1.1  jmcneill 		 * The 'memory-region' property is optional. If
    153  1.1  jmcneill 		 * absent, we can allocate FB from main RAM. (TODO)
    154  1.1  jmcneill 		 */
    155  1.1  jmcneill 		aprint_error(": missing 'memory-region' property\n");
    156  1.1  jmcneill 		return;
    157  1.1  jmcneill 	}
    158  1.1  jmcneill 	if (fdtbus_get_reg(vram_phandle, 0, &sc->sc_vram_addr,
    159  1.1  jmcneill 	    &sc->sc_vram_size) != 0) {
    160  1.1  jmcneill 		aprint_error(": missing 'reg' property on memory-region\n");
    161  1.1  jmcneill 		return;
    162  1.1  jmcneill 	}
    163  1.1  jmcneill 	if (bus_space_map(sc->sc_bst, sc->sc_vram_addr, sc->sc_vram_size,
    164  1.1  jmcneill 	    BUS_SPACE_MAP_LINEAR, &sc->sc_vram_bsh)) {
    165  1.1  jmcneill 		aprint_error(": couldn't map vram\n");
    166  1.1  jmcneill 		return;
    167  1.1  jmcneill 	}
    168  1.1  jmcneill 	sc->sc_vram = (uintptr_t)bus_space_vaddr(sc->sc_bst, sc->sc_vram_bsh);
    169  1.1  jmcneill 
    170  1.1  jmcneill 	plfb_init(sc);
    171  1.1  jmcneill 
    172  1.1  jmcneill 	sc->sc_wstype = WSDISPLAY_TYPE_PLFB;
    173  1.1  jmcneill 	prop_dictionary_set_bool(dict, "is_console", false);
    174  1.1  jmcneill 
    175  1.1  jmcneill 	genfb_init(&sc->sc_gen);
    176  1.1  jmcneill 
    177  1.1  jmcneill 	if (sc->sc_gen.sc_width == 0 ||
    178  1.1  jmcneill 	    sc->sc_gen.sc_fbsize == 0) {
    179  1.1  jmcneill 		aprint_normal(": disabled\n");
    180  1.1  jmcneill 		return;
    181  1.1  jmcneill 	}
    182  1.1  jmcneill 
    183  1.1  jmcneill 	pmf_device_register1(self, NULL, NULL, plfb_shutdown);
    184  1.1  jmcneill 
    185  1.1  jmcneill 	memset(&ops, 0, sizeof(ops));
    186  1.1  jmcneill 	ops.genfb_ioctl = plfb_ioctl;
    187  1.1  jmcneill 	ops.genfb_mmap = plfb_mmap;
    188  1.1  jmcneill 
    189  1.1  jmcneill 	aprint_naive("\n");
    190  1.1  jmcneill 	aprint_normal("\n");
    191  1.1  jmcneill 
    192  1.1  jmcneill 	genfb_attach(&sc->sc_gen, &ops);
    193  1.1  jmcneill }
    194  1.1  jmcneill 
    195  1.1  jmcneill static int
    196  1.1  jmcneill plfb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag, lwp_t *l)
    197  1.1  jmcneill {
    198  1.1  jmcneill 	struct plfb_softc *sc = v;
    199  1.1  jmcneill 	struct wsdisplayio_bus_id *busid;
    200  1.1  jmcneill 
    201  1.1  jmcneill 	switch (cmd) {
    202  1.1  jmcneill 	case WSDISPLAYIO_GTYPE:
    203  1.1  jmcneill 		*(u_int *)data = sc->sc_wstype;
    204  1.1  jmcneill 		return 0;
    205  1.1  jmcneill 	case WSDISPLAYIO_GET_BUSID:
    206  1.1  jmcneill 		busid = data;
    207  1.1  jmcneill 		busid->bus_type = WSDISPLAYIO_BUS_SOC;
    208  1.1  jmcneill 		return 0;
    209  1.1  jmcneill 	case WSDISPLAYIO_GET_FBINFO:
    210  1.1  jmcneill 		{
    211  1.1  jmcneill 			struct wsdisplayio_fbinfo *fbi = data;
    212  1.1  jmcneill 			struct rasops_info *ri = &sc->sc_gen.vd.active->scr_ri;
    213  1.1  jmcneill 
    214  1.1  jmcneill 			return wsdisplayio_get_fbinfo(ri, fbi);
    215  1.1  jmcneill 		}
    216  1.1  jmcneill 	default:
    217  1.1  jmcneill 		return EPASSTHROUGH;
    218  1.1  jmcneill 	}
    219  1.1  jmcneill }
    220  1.1  jmcneill 
    221  1.1  jmcneill static paddr_t
    222  1.1  jmcneill plfb_mmap(void *v, void *vs, off_t offset, int prot)
    223  1.1  jmcneill {
    224  1.1  jmcneill 	struct plfb_softc *sc = v;
    225  1.1  jmcneill 
    226  1.1  jmcneill 	if (offset < 0 || offset >= sc->sc_vram_size)
    227  1.1  jmcneill 		return -1;
    228  1.1  jmcneill 
    229  1.1  jmcneill 	return bus_space_mmap(sc->sc_bst, sc->sc_vram_addr, offset, prot,
    230  1.1  jmcneill 	    BUS_SPACE_MAP_LINEAR | BUS_SPACE_MAP_PREFETCHABLE);
    231  1.1  jmcneill }
    232  1.1  jmcneill 
    233  1.1  jmcneill static bool
    234  1.1  jmcneill plfb_shutdown(device_t self, int flags)
    235  1.1  jmcneill {
    236  1.1  jmcneill 	genfb_enable_polling(self);
    237  1.1  jmcneill 	return true;
    238  1.1  jmcneill }
    239  1.1  jmcneill 
    240  1.1  jmcneill static int
    241  1.1  jmcneill plfb_get_panel_timing(struct plfb_softc *sc, struct display_timing *timing)
    242  1.1  jmcneill {
    243  1.1  jmcneill 	int panel, panel_timing;
    244  1.1  jmcneill 
    245  1.1  jmcneill 	panel = of_find_firstchild_byname(sc->sc_phandle, "panel");
    246  1.1  jmcneill 	if (panel <= 0)
    247  1.1  jmcneill 		return ENOENT;
    248  1.1  jmcneill 	panel_timing = of_find_firstchild_byname(panel, "panel-timing");
    249  1.1  jmcneill 	if (panel_timing <= 0)
    250  1.1  jmcneill 		return ENOENT;
    251  1.1  jmcneill 
    252  1.1  jmcneill 	return display_timing_parse(panel_timing, timing);
    253  1.1  jmcneill }
    254  1.1  jmcneill 
    255  1.1  jmcneill static void
    256  1.1  jmcneill plfb_init(struct plfb_softc *sc)
    257  1.1  jmcneill {
    258  1.1  jmcneill 	prop_dictionary_t dict = device_properties(sc->sc_gen.sc_dev);
    259  1.1  jmcneill 	struct display_timing timing;
    260  1.1  jmcneill 
    261  1.1  jmcneill 	if (plfb_get_panel_timing(sc, &timing) != 0) {
    262  1.1  jmcneill 		aprint_error_dev(sc->sc_gen.sc_dev,
    263  1.1  jmcneill 		    "couldn't get panel timings\n");
    264  1.1  jmcneill 		return;
    265  1.1  jmcneill 	}
    266  1.1  jmcneill 
    267  1.1  jmcneill 	prop_dictionary_set_uint32(dict, "width", timing.hactive);
    268  1.1  jmcneill 	prop_dictionary_set_uint32(dict, "height", timing.vactive);
    269  1.1  jmcneill 	prop_dictionary_set_uint8(dict, "depth", PLFB_BPP);
    270  1.1  jmcneill 	prop_dictionary_set_bool(dict, "dblscan", 0);
    271  1.1  jmcneill 	prop_dictionary_set_bool(dict, "interlace", 0);
    272  1.1  jmcneill 	prop_dictionary_set_uint16(dict, "linebytes", timing.hactive * (PLFB_BPP / 8));
    273  1.1  jmcneill 	prop_dictionary_set_uint32(dict, "address", sc->sc_vram_addr);
    274  1.1  jmcneill 	prop_dictionary_set_uint32(dict, "virtual_address", sc->sc_vram);
    275  1.1  jmcneill 
    276  1.1  jmcneill 	/* FB base address */
    277  1.1  jmcneill 	FB_WRITE(sc, LCDUPBASE, sc->sc_vram_addr);
    278  1.1  jmcneill 	FB_WRITE(sc, LCDLPBASE, 0);
    279  1.1  jmcneill 
    280  1.1  jmcneill 	/* CRTC timings */
    281  1.1  jmcneill 	FB_WRITE(sc, LCDTIMING0,
    282  1.1  jmcneill 	    __SHIFTIN(timing.hback_porch - 1, LCDTIMING0_HBP) |
    283  1.1  jmcneill 	    __SHIFTIN(timing.hfront_porch - 1, LCDTIMING0_HFP) |
    284  1.1  jmcneill 	    __SHIFTIN(timing.hsync_len - 1, LCDTIMING0_HSW) |
    285  1.1  jmcneill 	    __SHIFTIN((timing.hactive / 16) - 1, LCDTIMING0_PPL));
    286  1.1  jmcneill 	FB_WRITE(sc, LCDTIMING1,
    287  1.1  jmcneill 	    __SHIFTIN(timing.vback_porch - 1, LCDTIMING1_VBP) |
    288  1.1  jmcneill 	    __SHIFTIN(timing.vfront_porch - 1, LCDTIMING1_VFP) |
    289  1.1  jmcneill 	    __SHIFTIN(timing.vsync_len - 1, LCDTIMING1_VSW) |
    290  1.1  jmcneill 	    __SHIFTIN(timing.vactive - 1, LCDTIMING1_LPP));
    291  1.1  jmcneill 
    292  1.1  jmcneill 	/* Configure and enable CLCD */
    293  1.1  jmcneill 	FB_WRITE(sc, LCDCONTROL,
    294  1.1  jmcneill 	    LCDCONTROL_PWR | LCDCONTROL_EN | LCDCONTROL_BPP_24 |
    295  1.1  jmcneill 	    LCDCONTROL_BGR);
    296  1.1  jmcneill }
    297