xref: /netbsd-src/sys/arch/arm/nxp/imx6_gpc.c (revision a33a6c43803dc8b884b646dc14983a964a1581b3)
1*a33a6c43Sbouyer /*	$NetBSD: imx6_gpc.c,v 1.4 2023/05/04 13:29:33 bouyer Exp $	*/
28644267aSskrll 
38644267aSskrll /*-
48644267aSskrll  * Copyright (c) 2019 Genetec Corporation.  All rights reserved.
58644267aSskrll  * Written by Hashimoto Kenichi for Genetec Corporation.
68644267aSskrll  *
78644267aSskrll  * Redistribution and use in source and binary forms, with or without
88644267aSskrll  * modification, are permitted provided that the following conditions
98644267aSskrll  * are met:
108644267aSskrll  * 1. Redistributions of source code must retain the above copyright
118644267aSskrll  *    notice, this list of conditions and the following disclaimer.
128644267aSskrll  * 2. Redistributions in binary form must reproduce the above copyright
138644267aSskrll  *    notice, this list of conditions and the following disclaimer in the
148644267aSskrll  *    documentation and/or other materials provided with the distribution.
158644267aSskrll  *
168644267aSskrll  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
178644267aSskrll  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
188644267aSskrll  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
198644267aSskrll  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
208644267aSskrll  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
218644267aSskrll  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
228644267aSskrll  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
238644267aSskrll  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
248644267aSskrll  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
258644267aSskrll  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
268644267aSskrll  * SUCH DAMAGE.
278644267aSskrll  */
288644267aSskrll 
298644267aSskrll #include <sys/cdefs.h>
30*a33a6c43Sbouyer __KERNEL_RCSID(0, "$NetBSD: imx6_gpc.c,v 1.4 2023/05/04 13:29:33 bouyer Exp $");
318644267aSskrll 
328644267aSskrll #include "opt_fdt.h"
338644267aSskrll 
348644267aSskrll #include <sys/param.h>
358644267aSskrll #include <sys/bus.h>
368644267aSskrll #include <sys/device.h>
378644267aSskrll 
388644267aSskrll #include <dev/fdt/fdtvar.h>
398644267aSskrll 
408644267aSskrll #include <arm/cortex/gic_intr.h>
418644267aSskrll #include <arm/nxp/imx6_gpcreg.h>
428644267aSskrll 
438644267aSskrll struct imxgpc_softc {
448644267aSskrll 	device_t sc_dev;
458644267aSskrll 
468644267aSskrll 	bus_space_tag_t sc_iot;
478644267aSskrll 	bus_space_handle_t sc_ioh;
488644267aSskrll };
498644267aSskrll 
508644267aSskrll static int imxgpc_match(device_t, struct cfdata *, void *);
518644267aSskrll static void imxgpc_attach(device_t, device_t, void *);
528644267aSskrll 
538644267aSskrll static void *imxgpc_establish(device_t, u_int *, int, int,
5459ad346dSjmcneill     int (*)(void *), void *, const char *);
558644267aSskrll static void imxgpc_disestablish(device_t, void *);
568644267aSskrll static bool imxgpc_intrstr(device_t, u_int *, char *, size_t);
578644267aSskrll 
588644267aSskrll struct fdtbus_interrupt_controller_func imxgpc_funcs = {
598644267aSskrll 	.establish = imxgpc_establish,
608644267aSskrll 	.disestablish = imxgpc_disestablish,
618644267aSskrll 	.intrstr = imxgpc_intrstr
628644267aSskrll };
638644267aSskrll 
648644267aSskrll CFATTACH_DECL_NEW(imxgpc, sizeof(struct imxgpc_softc),
658644267aSskrll     imxgpc_match, imxgpc_attach, NULL, NULL);
668644267aSskrll 
676e54367aSthorpej static const struct device_compatible_entry compat_data[] = {
686e54367aSthorpej 	{ .compat = "fsl,imx6q-gpc" },
69*a33a6c43Sbouyer 	{ .compat = "fsl,imx6sx-gpc" },
706e54367aSthorpej 	DEVICE_COMPAT_EOL
716e54367aSthorpej };
726e54367aSthorpej 
738644267aSskrll static int
imxgpc_match(device_t parent,cfdata_t cf,void * aux)748644267aSskrll imxgpc_match(device_t parent, cfdata_t cf, void *aux)
758644267aSskrll {
768644267aSskrll 	struct fdt_attach_args * const faa = aux;
778644267aSskrll 
786e54367aSthorpej 	return of_compatible_match(faa->faa_phandle, compat_data);
798644267aSskrll }
808644267aSskrll 
818644267aSskrll static void
imxgpc_attach(device_t parent,device_t self,void * aux)828644267aSskrll imxgpc_attach(device_t parent, device_t self, void *aux)
838644267aSskrll {
848644267aSskrll 	struct imxgpc_softc * const sc = device_private(self);
858644267aSskrll 	struct fdt_attach_args * const faa = aux;
868644267aSskrll 	const int phandle = faa->faa_phandle;
878644267aSskrll 	bus_addr_t gpc_addr;
888644267aSskrll 	bus_size_t gpc_size;
898644267aSskrll 	int error;
908644267aSskrll 
918644267aSskrll 	if (fdtbus_get_reg(phandle, 0, &gpc_addr, &gpc_size) != 0) {
928644267aSskrll 		aprint_error(": couldn't get gpc registers\n");
938644267aSskrll 		return;
948644267aSskrll 	}
958644267aSskrll 
968644267aSskrll 	sc->sc_dev = self;
978644267aSskrll 	sc->sc_iot = faa->faa_bst;
988644267aSskrll 
998644267aSskrll 	error = bus_space_map(sc->sc_iot, gpc_addr, gpc_size, 0,
1008644267aSskrll 	    &sc->sc_ioh);
1018644267aSskrll 	if (error) {
1028644267aSskrll 		aprint_error(": couldn't map gpc registers: %d\n", error);
1038644267aSskrll 		return;
1048644267aSskrll 	}
1058644267aSskrll 
1068644267aSskrll 	error = fdtbus_register_interrupt_controller(self, faa->faa_phandle,
1078644267aSskrll 	    &imxgpc_funcs);
1088644267aSskrll 	if (error) {
1098644267aSskrll 		aprint_error(": couldn't register with fdtbus: %d\n", error);
1108644267aSskrll 		return;
1118644267aSskrll 	}
1128644267aSskrll 
1138644267aSskrll 	aprint_naive("\n");
1148644267aSskrll 	aprint_normal(": General Power Controller\n");
1158644267aSskrll 
1168644267aSskrll 	return;
1178644267aSskrll }
1188644267aSskrll 
1198644267aSskrll static void *
imxgpc_establish(device_t dev,u_int * specifier,int ipl,int flags,int (* func)(void *),void * arg,const char * xname)1208644267aSskrll imxgpc_establish(device_t dev, u_int *specifier, int ipl, int flags,
12159ad346dSjmcneill     int (*func)(void *), void *arg, const char *xname)
1228644267aSskrll {
1238644267aSskrll 	/* 1st cell is the interrupt type; 0 is SPI, 1 is PPI */
1248644267aSskrll 	/* 2nd cell is the interrupt number */
1258644267aSskrll 	/* 3rd cell is flags */
1268644267aSskrll 
1278644267aSskrll 	const u_int type = be32toh(specifier[0]);
1288644267aSskrll 	const u_int intr = be32toh(specifier[1]);
1298644267aSskrll 	const u_int irq = type == 0 ? IRQ_SPI(intr) : IRQ_PPI(intr);
1308644267aSskrll 	const u_int trig = be32toh(specifier[2]) & 0xf;
1318644267aSskrll 	const u_int level = (trig & 0x3) ? IST_EDGE : IST_LEVEL;
1328644267aSskrll 
1338644267aSskrll 	const u_int mpsafe = (flags & FDT_INTR_MPSAFE) ? IST_MPSAFE : 0;
1348644267aSskrll 
1358644267aSskrll 	aprint_debug_dev(dev, "intr establish irq %d, level %d\n", irq, level);
13659ad346dSjmcneill 	return intr_establish_xname(irq, ipl, level | mpsafe, func, arg,
13759ad346dSjmcneill 	  xname);
1388644267aSskrll }
1398644267aSskrll 
1408644267aSskrll static void
imxgpc_disestablish(device_t dev,void * ih)1418644267aSskrll imxgpc_disestablish(device_t dev, void *ih)
1428644267aSskrll {
1438644267aSskrll 	intr_disestablish(ih);
1448644267aSskrll }
1458644267aSskrll 
1468644267aSskrll static bool
imxgpc_intrstr(device_t dev,u_int * specifier,char * buf,size_t buflen)1478644267aSskrll imxgpc_intrstr(device_t dev, u_int *specifier, char *buf, size_t buflen)
1488644267aSskrll {
1498644267aSskrll 	/* 1st cell is the interrupt type; 0 is SPI, 1 is PPI */
1508644267aSskrll 	/* 2nd cell is the interrupt number */
1518644267aSskrll 	/* 3rd cell is flags */
1528644267aSskrll 
1538644267aSskrll 	if (!specifier)
1548644267aSskrll 		return false;
1558644267aSskrll 
1568644267aSskrll 	const u_int type = be32toh(specifier[0]);
1578644267aSskrll 	const u_int intr = be32toh(specifier[1]);
1588644267aSskrll 	const u_int irq = type == 0 ? IRQ_SPI(intr) : IRQ_PPI(intr);
1598644267aSskrll 
1608644267aSskrll 	snprintf(buf, buflen, "irq %d", irq);
1618644267aSskrll 
1628644267aSskrll 	return true;
1638644267aSskrll }
164