Home | History | Annotate | Line # | Download | only in nxp
      1  1.3   thorpej /*	$NetBSD: imx7_gpc.c,v 1.3 2021/01/27 03:10:20 thorpej Exp $	*/
      2  1.1     skrll /*-
      3  1.1     skrll  * Copyright (c) 2019 Genetec Corporation.  All rights reserved.
      4  1.1     skrll  * Written by Hashimoto Kenichi for Genetec Corporation.
      5  1.1     skrll  *
      6  1.1     skrll  * Redistribution and use in source and binary forms, with or without
      7  1.1     skrll  * modification, are permitted provided that the following conditions
      8  1.1     skrll  * are met:
      9  1.1     skrll  * 1. Redistributions of source code must retain the above copyright
     10  1.1     skrll  *    notice, this list of conditions and the following disclaimer.
     11  1.1     skrll  * 2. Redistributions in binary form must reproduce the above copyright
     12  1.1     skrll  *    notice, this list of conditions and the following disclaimer in the
     13  1.1     skrll  *    documentation and/or other materials provided with the distribution.
     14  1.1     skrll  *
     15  1.1     skrll  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     16  1.1     skrll  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     17  1.1     skrll  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     18  1.1     skrll  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     19  1.1     skrll  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     20  1.1     skrll  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     21  1.1     skrll  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     22  1.1     skrll  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     23  1.1     skrll  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     24  1.1     skrll  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     25  1.1     skrll  * SUCH DAMAGE.
     26  1.1     skrll  */
     27  1.1     skrll #include <sys/cdefs.h>
     28  1.3   thorpej __KERNEL_RCSID(0, "$NetBSD: imx7_gpc.c,v 1.3 2021/01/27 03:10:20 thorpej Exp $");
     29  1.1     skrll 
     30  1.1     skrll #include "opt_fdt.h"
     31  1.1     skrll 
     32  1.1     skrll #define	_INTR_PRIVATE
     33  1.1     skrll 
     34  1.1     skrll #include <sys/param.h>
     35  1.1     skrll #include <sys/bus.h>
     36  1.1     skrll #include <sys/device.h>
     37  1.1     skrll 
     38  1.1     skrll #include <arm/nxp/imx6var.h>
     39  1.1     skrll #include <arm/nxp/imx6_reg.h>
     40  1.1     skrll #include <arm/nxp/imx6_gpcreg.h>
     41  1.1     skrll 
     42  1.1     skrll #include <arm/cortex/gic_intr.h>
     43  1.1     skrll 
     44  1.1     skrll #include <dev/fdt/fdtvar.h>
     45  1.1     skrll 
     46  1.1     skrll #define	GPC_PCG_CPU_0_1_MAPPING		0xec
     47  1.1     skrll #define	 OTG2_A53_DOMAIN		__BIT(5)
     48  1.1     skrll #define	 OTG1_A53_DOMAIN		__BIT(4)
     49  1.1     skrll 
     50  1.1     skrll #define	GPC_PU_PGC_SW_PUP_REQ		0xf8
     51  1.1     skrll #define	 USB_OTG2_SW_PUP_REQ		__BIT(3)
     52  1.1     skrll #define	 USB_OTG1_SW_PUP_REQ		__BIT(2)
     53  1.1     skrll 
     54  1.1     skrll #define	IMXGPC_MAXCPUS	4
     55  1.1     skrll 
     56  1.1     skrll /* Mapping of CPU number to GPC_IMR1_COREx base offset */
     57  1.1     skrll static const bus_size_t imx7gpc_imr_base[IMXGPC_MAXCPUS] = {
     58  1.1     skrll 	0x30,
     59  1.1     skrll 	0x40,
     60  1.1     skrll 	0x1c0,
     61  1.1     skrll 	0x1d0,
     62  1.1     skrll };
     63  1.1     skrll 
     64  1.1     skrll #define	GPC_IMRn_COREx(n,x)	(imx7gpc_imr_base[(x)] + (n) * 0x4)
     65  1.1     skrll 
     66  1.1     skrll struct imx7gpc_softc {
     67  1.1     skrll 	device_t sc_dev;
     68  1.1     skrll 
     69  1.1     skrll 	bus_space_tag_t sc_iot;
     70  1.1     skrll 	bus_space_handle_t sc_ioh;
     71  1.1     skrll };
     72  1.1     skrll 
     73  1.1     skrll static int imx7gpc_match(device_t, struct cfdata *, void *);
     74  1.1     skrll static void imx7gpc_attach(device_t, device_t, void *);
     75  1.1     skrll 
     76  1.1     skrll static void imx7gpc_powerup(struct imx7gpc_softc *, uint32_t, uint32_t);
     77  1.1     skrll static void imx7gpc_mask(struct imx7gpc_softc *, u_int, bool);
     78  1.1     skrll static void imx7gpc_unmask(struct imx7gpc_softc *, u_int, bool);
     79  1.1     skrll 
     80  1.1     skrll static void *imx7gpc_establish(device_t, u_int *, int, int,
     81  1.2  jmcneill     int (*)(void *), void *, const char *);
     82  1.1     skrll static void imx7gpc_disestablish(device_t, void *);
     83  1.1     skrll static bool imx7gpc_intrstr(device_t, u_int *, char *, size_t);
     84  1.1     skrll 
     85  1.1     skrll struct fdtbus_interrupt_controller_func imx7gpc_funcs = {
     86  1.1     skrll 	.establish = imx7gpc_establish,
     87  1.1     skrll 	.disestablish = imx7gpc_disestablish,
     88  1.1     skrll 	.intrstr = imx7gpc_intrstr
     89  1.1     skrll };
     90  1.1     skrll 
     91  1.1     skrll CFATTACH_DECL_NEW(imx7gpc, sizeof(struct imx7gpc_softc),
     92  1.1     skrll     imx7gpc_match, imx7gpc_attach, NULL, NULL);
     93  1.1     skrll 
     94  1.3   thorpej static const struct device_compatible_entry compat_data[] = {
     95  1.3   thorpej 	{ .compat = "fsl,imx7d-gpc" },
     96  1.3   thorpej 	{ .compat = "fsl,imx8mq-gpc" },
     97  1.3   thorpej 	DEVICE_COMPAT_EOL
     98  1.3   thorpej };
     99  1.3   thorpej 
    100  1.1     skrll static int
    101  1.1     skrll imx7gpc_match(device_t parent, cfdata_t cf, void *aux)
    102  1.1     skrll {
    103  1.1     skrll 	struct fdt_attach_args * const faa = aux;
    104  1.1     skrll 
    105  1.3   thorpej 	return of_compatible_match(faa->faa_phandle, compat_data);
    106  1.1     skrll }
    107  1.1     skrll 
    108  1.1     skrll static void
    109  1.1     skrll imx7gpc_attach(device_t parent, device_t self, void *aux)
    110  1.1     skrll {
    111  1.1     skrll 	struct imx7gpc_softc * const sc = device_private(self);
    112  1.1     skrll 	struct fdt_attach_args * const faa = aux;
    113  1.1     skrll 	const int phandle = faa->faa_phandle;
    114  1.1     skrll 	bus_addr_t gpc_addr;
    115  1.1     skrll 	bus_size_t gpc_size;
    116  1.1     skrll 	int error;
    117  1.1     skrll 
    118  1.1     skrll 	KASSERT(ncpu <= IMXGPC_MAXCPUS);
    119  1.1     skrll 
    120  1.1     skrll 	if (fdtbus_get_reg(phandle, 0, &gpc_addr, &gpc_size) != 0) {
    121  1.1     skrll 		aprint_error(": couldn't get gpc registers\n");
    122  1.1     skrll 		return;
    123  1.1     skrll 	}
    124  1.1     skrll 
    125  1.1     skrll 	sc->sc_dev = self;
    126  1.1     skrll 	sc->sc_iot = faa->faa_bst;
    127  1.1     skrll 
    128  1.1     skrll 	error = bus_space_map(sc->sc_iot, gpc_addr, gpc_size, 0,
    129  1.1     skrll 	    &sc->sc_ioh);
    130  1.1     skrll 	if (error) {
    131  1.1     skrll 		aprint_error(": couldn't map gpc registers: %d\n", error);
    132  1.1     skrll 		return;
    133  1.1     skrll 	}
    134  1.1     skrll 
    135  1.1     skrll 	error = fdtbus_register_interrupt_controller(self, faa->faa_phandle,
    136  1.1     skrll 	    &imx7gpc_funcs);
    137  1.1     skrll 	if (error) {
    138  1.1     skrll 		aprint_error(": couldn't register with fdtbus: %d\n", error);
    139  1.1     skrll 		return;
    140  1.1     skrll 	}
    141  1.1     skrll 
    142  1.1     skrll 	aprint_naive("\n");
    143  1.1     skrll 	aprint_normal(": General Power Controller\n");
    144  1.1     skrll 
    145  1.1     skrll 	/* XXX enable OTG power domains */
    146  1.1     skrll 	imx7gpc_powerup(sc, USB_OTG2_SW_PUP_REQ | USB_OTG1_SW_PUP_REQ,
    147  1.1     skrll 	    OTG2_A53_DOMAIN | OTG1_A53_DOMAIN);
    148  1.1     skrll }
    149  1.1     skrll 
    150  1.1     skrll static void
    151  1.1     skrll imx7gpc_powerup(struct imx7gpc_softc *sc, uint32_t req, uint32_t map)
    152  1.1     skrll {
    153  1.1     skrll 	uint32_t val;
    154  1.1     skrll 
    155  1.1     skrll 	val = bus_space_read_4(sc->sc_iot, sc->sc_ioh, GPC_PCG_CPU_0_1_MAPPING);
    156  1.1     skrll 	val |= map;
    157  1.1     skrll 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, GPC_PCG_CPU_0_1_MAPPING, val);
    158  1.1     skrll 
    159  1.1     skrll 	val = bus_space_read_4(sc->sc_iot, sc->sc_ioh, GPC_PU_PGC_SW_PUP_REQ);
    160  1.1     skrll 	val |= req;
    161  1.1     skrll 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, GPC_PU_PGC_SW_PUP_REQ, val);
    162  1.1     skrll 
    163  1.1     skrll 	delay(5000);
    164  1.1     skrll 
    165  1.1     skrll 	val = bus_space_read_4(sc->sc_iot, sc->sc_ioh, GPC_PCG_CPU_0_1_MAPPING);
    166  1.1     skrll 	val &= ~map;
    167  1.1     skrll 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, GPC_PCG_CPU_0_1_MAPPING, val);
    168  1.1     skrll }
    169  1.1     skrll 
    170  1.1     skrll static void
    171  1.1     skrll imx7gpc_mask(struct imx7gpc_softc *sc, u_int irq, bool mpsafe)
    172  1.1     skrll {
    173  1.1     skrll 	const u_int reg = irq / 32;
    174  1.1     skrll 	const u_int bit = irq % 32;
    175  1.1     skrll 	uint32_t val;
    176  1.1     skrll 
    177  1.1     skrll 	for (u_int cpu = 0; cpu < (mpsafe ? ncpu : 1); cpu++) {
    178  1.1     skrll 		val = bus_space_read_4(sc->sc_iot, sc->sc_ioh,
    179  1.1     skrll 		    GPC_IMRn_COREx(reg, cpu));
    180  1.1     skrll 		val |= __BIT(bit);
    181  1.1     skrll 		bus_space_write_4(sc->sc_iot, sc->sc_ioh,
    182  1.1     skrll 		    GPC_IMRn_COREx(reg, cpu), val);
    183  1.1     skrll 	}
    184  1.1     skrll }
    185  1.1     skrll 
    186  1.1     skrll static void
    187  1.1     skrll imx7gpc_unmask(struct imx7gpc_softc *sc, u_int irq, bool mpsafe)
    188  1.1     skrll {
    189  1.1     skrll 	const u_int reg = irq / 32;
    190  1.1     skrll 	const u_int bit = irq % 32;
    191  1.1     skrll 	uint32_t val;
    192  1.1     skrll 
    193  1.1     skrll 	for (u_int cpu = 0; cpu < (mpsafe ? ncpu : 1); cpu++) {
    194  1.1     skrll 		val = bus_space_read_4(sc->sc_iot, sc->sc_ioh,
    195  1.1     skrll 		    GPC_IMRn_COREx(reg, cpu));
    196  1.1     skrll 		val &= ~__BIT(bit);
    197  1.1     skrll 		bus_space_write_4(sc->sc_iot, sc->sc_ioh,
    198  1.1     skrll 		    GPC_IMRn_COREx(reg, cpu), val);
    199  1.1     skrll 	}
    200  1.1     skrll }
    201  1.1     skrll 
    202  1.1     skrll 
    203  1.1     skrll static void *
    204  1.1     skrll imx7gpc_establish(device_t dev, u_int *specifier, int ipl, int flags,
    205  1.2  jmcneill     int (*func)(void *), void *arg, const char *xname)
    206  1.1     skrll {
    207  1.1     skrll 	struct imx7gpc_softc * const sc = device_private(dev);
    208  1.1     skrll 	void *ih;
    209  1.1     skrll 
    210  1.1     skrll 	/* 1st cell is the interrupt type; 0 is SPI, 1 is PPI */
    211  1.1     skrll 	/* 2nd cell is the interrupt number */
    212  1.1     skrll 	/* 3rd cell is flags */
    213  1.1     skrll 
    214  1.1     skrll 	const u_int type = be32toh(specifier[0]);
    215  1.1     skrll 	const u_int intr = be32toh(specifier[1]);
    216  1.1     skrll 	const u_int irq = type == 0 ? IRQ_SPI(intr) : IRQ_PPI(intr);
    217  1.1     skrll 	const u_int trig = be32toh(specifier[2]) & 0xf;
    218  1.1     skrll 	const u_int level = (trig & 0x3) ? IST_EDGE : IST_LEVEL;
    219  1.1     skrll 
    220  1.1     skrll 	const u_int mpsafe = (flags & FDT_INTR_MPSAFE) ? IST_MPSAFE : 0;
    221  1.1     skrll 
    222  1.1     skrll 	if (type != 0)
    223  1.1     skrll 		return NULL;	/* Only SPIs are supported */
    224  1.1     skrll 
    225  1.1     skrll 	KASSERT(irq >= 32);
    226  1.1     skrll 
    227  1.1     skrll 	aprint_debug_dev(dev, "intr establish irq %d, level %d\n", irq, level);
    228  1.1     skrll 
    229  1.2  jmcneill 	ih = intr_establish_xname(irq, ipl, level | mpsafe, func, arg, xname);
    230  1.1     skrll 	if (ih != NULL)
    231  1.1     skrll 		imx7gpc_unmask(sc, irq - 32, mpsafe == IST_MPSAFE);
    232  1.1     skrll 
    233  1.1     skrll 	return ih;
    234  1.1     skrll }
    235  1.1     skrll 
    236  1.1     skrll static void
    237  1.1     skrll imx7gpc_disestablish(device_t dev, void *ih)
    238  1.1     skrll {
    239  1.1     skrll 	struct imx7gpc_softc * const sc = device_private(dev);
    240  1.1     skrll 	struct intrsource *is = ih;
    241  1.1     skrll 	const u_int irq = is->is_irq;
    242  1.1     skrll 	const bool mpsafe = is->is_mpsafe;
    243  1.1     skrll 
    244  1.1     skrll 	intr_disestablish(ih);
    245  1.1     skrll 	imx7gpc_mask(sc, irq - 32, mpsafe);
    246  1.1     skrll }
    247  1.1     skrll 
    248  1.1     skrll static bool
    249  1.1     skrll imx7gpc_intrstr(device_t dev, u_int *specifier, char *buf, size_t buflen)
    250  1.1     skrll {
    251  1.1     skrll 	/* 1st cell is the interrupt type; 0 is SPI, 1 is PPI */
    252  1.1     skrll 	/* 2nd cell is the interrupt number */
    253  1.1     skrll 	/* 3rd cell is flags */
    254  1.1     skrll 
    255  1.1     skrll 	if (!specifier)
    256  1.1     skrll 		return false;
    257  1.1     skrll 
    258  1.1     skrll 	const u_int type = be32toh(specifier[0]);
    259  1.1     skrll 	const u_int intr = be32toh(specifier[1]);
    260  1.1     skrll 	const u_int irq = type == 0 ? IRQ_SPI(intr) : IRQ_PPI(intr);
    261  1.1     skrll 
    262  1.1     skrll 	snprintf(buf, buflen, "irq %d", irq);
    263  1.1     skrll 
    264  1.1     skrll 	return true;
    265  1.1     skrll }
    266