Home | History | Annotate | Line # | Download | only in nxp
      1  1.4    bouyer /*	$NetBSD: imx6_gpc.c,v 1.4 2023/05/04 13:29:33 bouyer Exp $	*/
      2  1.1     skrll 
      3  1.1     skrll /*-
      4  1.1     skrll  * Copyright (c) 2019 Genetec Corporation.  All rights reserved.
      5  1.1     skrll  * Written by Hashimoto Kenichi for Genetec Corporation.
      6  1.1     skrll  *
      7  1.1     skrll  * Redistribution and use in source and binary forms, with or without
      8  1.1     skrll  * modification, are permitted provided that the following conditions
      9  1.1     skrll  * are met:
     10  1.1     skrll  * 1. Redistributions of source code must retain the above copyright
     11  1.1     skrll  *    notice, this list of conditions and the following disclaimer.
     12  1.1     skrll  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1     skrll  *    notice, this list of conditions and the following disclaimer in the
     14  1.1     skrll  *    documentation and/or other materials provided with the distribution.
     15  1.1     skrll  *
     16  1.1     skrll  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  1.1     skrll  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  1.1     skrll  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  1.1     skrll  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  1.1     skrll  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     21  1.1     skrll  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     22  1.1     skrll  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     23  1.1     skrll  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     24  1.1     skrll  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  1.1     skrll  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  1.1     skrll  * SUCH DAMAGE.
     27  1.1     skrll  */
     28  1.1     skrll 
     29  1.1     skrll #include <sys/cdefs.h>
     30  1.4    bouyer __KERNEL_RCSID(0, "$NetBSD: imx6_gpc.c,v 1.4 2023/05/04 13:29:33 bouyer Exp $");
     31  1.1     skrll 
     32  1.1     skrll #include "opt_fdt.h"
     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 <dev/fdt/fdtvar.h>
     39  1.1     skrll 
     40  1.1     skrll #include <arm/cortex/gic_intr.h>
     41  1.1     skrll #include <arm/nxp/imx6_gpcreg.h>
     42  1.1     skrll 
     43  1.1     skrll struct imxgpc_softc {
     44  1.1     skrll 	device_t sc_dev;
     45  1.1     skrll 
     46  1.1     skrll 	bus_space_tag_t sc_iot;
     47  1.1     skrll 	bus_space_handle_t sc_ioh;
     48  1.1     skrll };
     49  1.1     skrll 
     50  1.1     skrll static int imxgpc_match(device_t, struct cfdata *, void *);
     51  1.1     skrll static void imxgpc_attach(device_t, device_t, void *);
     52  1.1     skrll 
     53  1.1     skrll static void *imxgpc_establish(device_t, u_int *, int, int,
     54  1.2  jmcneill     int (*)(void *), void *, const char *);
     55  1.1     skrll static void imxgpc_disestablish(device_t, void *);
     56  1.1     skrll static bool imxgpc_intrstr(device_t, u_int *, char *, size_t);
     57  1.1     skrll 
     58  1.1     skrll struct fdtbus_interrupt_controller_func imxgpc_funcs = {
     59  1.1     skrll 	.establish = imxgpc_establish,
     60  1.1     skrll 	.disestablish = imxgpc_disestablish,
     61  1.1     skrll 	.intrstr = imxgpc_intrstr
     62  1.1     skrll };
     63  1.1     skrll 
     64  1.1     skrll CFATTACH_DECL_NEW(imxgpc, sizeof(struct imxgpc_softc),
     65  1.1     skrll     imxgpc_match, imxgpc_attach, NULL, NULL);
     66  1.1     skrll 
     67  1.3   thorpej static const struct device_compatible_entry compat_data[] = {
     68  1.3   thorpej 	{ .compat = "fsl,imx6q-gpc" },
     69  1.4    bouyer 	{ .compat = "fsl,imx6sx-gpc" },
     70  1.3   thorpej 	DEVICE_COMPAT_EOL
     71  1.3   thorpej };
     72  1.3   thorpej 
     73  1.1     skrll static int
     74  1.1     skrll imxgpc_match(device_t parent, cfdata_t cf, void *aux)
     75  1.1     skrll {
     76  1.1     skrll 	struct fdt_attach_args * const faa = aux;
     77  1.1     skrll 
     78  1.3   thorpej 	return of_compatible_match(faa->faa_phandle, compat_data);
     79  1.1     skrll }
     80  1.1     skrll 
     81  1.1     skrll static void
     82  1.1     skrll imxgpc_attach(device_t parent, device_t self, void *aux)
     83  1.1     skrll {
     84  1.1     skrll 	struct imxgpc_softc * const sc = device_private(self);
     85  1.1     skrll 	struct fdt_attach_args * const faa = aux;
     86  1.1     skrll 	const int phandle = faa->faa_phandle;
     87  1.1     skrll 	bus_addr_t gpc_addr;
     88  1.1     skrll 	bus_size_t gpc_size;
     89  1.1     skrll 	int error;
     90  1.1     skrll 
     91  1.1     skrll 	if (fdtbus_get_reg(phandle, 0, &gpc_addr, &gpc_size) != 0) {
     92  1.1     skrll 		aprint_error(": couldn't get gpc registers\n");
     93  1.1     skrll 		return;
     94  1.1     skrll 	}
     95  1.1     skrll 
     96  1.1     skrll 	sc->sc_dev = self;
     97  1.1     skrll 	sc->sc_iot = faa->faa_bst;
     98  1.1     skrll 
     99  1.1     skrll 	error = bus_space_map(sc->sc_iot, gpc_addr, gpc_size, 0,
    100  1.1     skrll 	    &sc->sc_ioh);
    101  1.1     skrll 	if (error) {
    102  1.1     skrll 		aprint_error(": couldn't map gpc registers: %d\n", error);
    103  1.1     skrll 		return;
    104  1.1     skrll 	}
    105  1.1     skrll 
    106  1.1     skrll 	error = fdtbus_register_interrupt_controller(self, faa->faa_phandle,
    107  1.1     skrll 	    &imxgpc_funcs);
    108  1.1     skrll 	if (error) {
    109  1.1     skrll 		aprint_error(": couldn't register with fdtbus: %d\n", error);
    110  1.1     skrll 		return;
    111  1.1     skrll 	}
    112  1.1     skrll 
    113  1.1     skrll 	aprint_naive("\n");
    114  1.1     skrll 	aprint_normal(": General Power Controller\n");
    115  1.1     skrll 
    116  1.1     skrll 	return;
    117  1.1     skrll }
    118  1.1     skrll 
    119  1.1     skrll static void *
    120  1.1     skrll imxgpc_establish(device_t dev, u_int *specifier, int ipl, int flags,
    121  1.2  jmcneill     int (*func)(void *), void *arg, const char *xname)
    122  1.1     skrll {
    123  1.1     skrll 	/* 1st cell is the interrupt type; 0 is SPI, 1 is PPI */
    124  1.1     skrll 	/* 2nd cell is the interrupt number */
    125  1.1     skrll 	/* 3rd cell is flags */
    126  1.1     skrll 
    127  1.1     skrll 	const u_int type = be32toh(specifier[0]);
    128  1.1     skrll 	const u_int intr = be32toh(specifier[1]);
    129  1.1     skrll 	const u_int irq = type == 0 ? IRQ_SPI(intr) : IRQ_PPI(intr);
    130  1.1     skrll 	const u_int trig = be32toh(specifier[2]) & 0xf;
    131  1.1     skrll 	const u_int level = (trig & 0x3) ? IST_EDGE : IST_LEVEL;
    132  1.1     skrll 
    133  1.1     skrll 	const u_int mpsafe = (flags & FDT_INTR_MPSAFE) ? IST_MPSAFE : 0;
    134  1.1     skrll 
    135  1.1     skrll 	aprint_debug_dev(dev, "intr establish irq %d, level %d\n", irq, level);
    136  1.2  jmcneill 	return intr_establish_xname(irq, ipl, level | mpsafe, func, arg,
    137  1.2  jmcneill 	  xname);
    138  1.1     skrll }
    139  1.1     skrll 
    140  1.1     skrll static void
    141  1.1     skrll imxgpc_disestablish(device_t dev, void *ih)
    142  1.1     skrll {
    143  1.1     skrll 	intr_disestablish(ih);
    144  1.1     skrll }
    145  1.1     skrll 
    146  1.1     skrll static bool
    147  1.1     skrll imxgpc_intrstr(device_t dev, u_int *specifier, char *buf, size_t buflen)
    148  1.1     skrll {
    149  1.1     skrll 	/* 1st cell is the interrupt type; 0 is SPI, 1 is PPI */
    150  1.1     skrll 	/* 2nd cell is the interrupt number */
    151  1.1     skrll 	/* 3rd cell is flags */
    152  1.1     skrll 
    153  1.1     skrll 	if (!specifier)
    154  1.1     skrll 		return false;
    155  1.1     skrll 
    156  1.1     skrll 	const u_int type = be32toh(specifier[0]);
    157  1.1     skrll 	const u_int intr = be32toh(specifier[1]);
    158  1.1     skrll 	const u_int irq = type == 0 ? IRQ_SPI(intr) : IRQ_PPI(intr);
    159  1.1     skrll 
    160  1.1     skrll 	snprintf(buf, buflen, "irq %d", irq);
    161  1.1     skrll 
    162  1.1     skrll 	return true;
    163  1.1     skrll }
    164