tegra_lic.c revision 1.8
11.8Sthorpej/* $NetBSD: tegra_lic.c,v 1.8 2021/01/27 03:10:19 thorpej Exp $ */
21.1Sjmcneill
31.1Sjmcneill/*-
41.1Sjmcneill * Copyright (c) 2015 Jared D. McNeill <jmcneill@invisible.ca>
51.1Sjmcneill * All rights reserved.
61.1Sjmcneill *
71.1Sjmcneill * Redistribution and use in source and binary forms, with or without
81.1Sjmcneill * modification, are permitted provided that the following conditions
91.1Sjmcneill * are met:
101.1Sjmcneill * 1. Redistributions of source code must retain the above copyright
111.1Sjmcneill *    notice, this list of conditions and the following disclaimer.
121.1Sjmcneill * 2. Redistributions in binary form must reproduce the above copyright
131.1Sjmcneill *    notice, this list of conditions and the following disclaimer in the
141.1Sjmcneill *    documentation and/or other materials provided with the distribution.
151.1Sjmcneill *
161.1Sjmcneill * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
171.1Sjmcneill * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
181.1Sjmcneill * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
191.1Sjmcneill * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
201.1Sjmcneill * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
211.1Sjmcneill * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
221.1Sjmcneill * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
231.1Sjmcneill * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
241.1Sjmcneill * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
251.1Sjmcneill * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
261.1Sjmcneill * SUCH DAMAGE.
271.1Sjmcneill */
281.1Sjmcneill
291.1Sjmcneill#include <sys/cdefs.h>
301.8Sthorpej__KERNEL_RCSID(0, "$NetBSD: tegra_lic.c,v 1.8 2021/01/27 03:10:19 thorpej Exp $");
311.1Sjmcneill
321.1Sjmcneill#include <sys/param.h>
331.1Sjmcneill#include <sys/bus.h>
341.1Sjmcneill#include <sys/device.h>
351.1Sjmcneill#include <sys/intr.h>
361.1Sjmcneill#include <sys/systm.h>
371.1Sjmcneill#include <sys/kernel.h>
381.1Sjmcneill#include <sys/kmem.h>
391.1Sjmcneill
401.1Sjmcneill#include <arm/nvidia/tegra_reg.h>
411.1Sjmcneill#include <arm/nvidia/tegra_var.h>
421.1Sjmcneill
431.1Sjmcneill#include <arm/cortex/gic_intr.h>
441.1Sjmcneill
451.1Sjmcneill#include <dev/fdt/fdtvar.h>
461.1Sjmcneill
471.4Sjmcneill#define	LIC_CPU_IER_CLR_REG	0x28
481.4Sjmcneill#define	LIC_CPU_IEP_CLASS_REG	0x2c
491.4Sjmcneill
501.1Sjmcneillstatic int	tegra_lic_match(device_t, cfdata_t, void *);
511.1Sjmcneillstatic void	tegra_lic_attach(device_t, device_t, void *);
521.1Sjmcneill
531.3Smartystatic void *	tegra_lic_establish(device_t, u_int *, int, int,
541.7Sjmcneill		    int (*)(void *), void *, const char *);
551.1Sjmcneillstatic void	tegra_lic_disestablish(device_t, void *);
561.3Smartystatic bool	tegra_lic_intrstr(device_t, u_int *, char *, size_t);
571.1Sjmcneill
581.1Sjmcneillstruct fdtbus_interrupt_controller_func tegra_lic_funcs = {
591.1Sjmcneill	.establish = tegra_lic_establish,
601.1Sjmcneill	.disestablish = tegra_lic_disestablish,
611.1Sjmcneill	.intrstr = tegra_lic_intrstr
621.1Sjmcneill};
631.1Sjmcneill
641.1Sjmcneillstruct tegra_lic_softc {
651.1Sjmcneill	device_t		sc_dev;
661.1Sjmcneill	int			sc_phandle;
671.1Sjmcneill};
681.1Sjmcneill
691.1SjmcneillCFATTACH_DECL_NEW(tegra_lic, sizeof(struct tegra_lic_softc),
701.1Sjmcneill	tegra_lic_match, tegra_lic_attach, NULL, NULL);
711.1Sjmcneill
721.8Sthorpejstatic const struct device_compatible_entry compat_data[] = {
731.8Sthorpej	{ .compat = "nvidia,tegra210-ictlr" },
741.8Sthorpej	{ .compat = "nvidia,tegra124-ictlr" },
751.8Sthorpej	DEVICE_COMPAT_EOL
761.8Sthorpej};
771.8Sthorpej
781.1Sjmcneillstatic int
791.1Sjmcneilltegra_lic_match(device_t parent, cfdata_t cf, void *aux)
801.1Sjmcneill{
811.1Sjmcneill	struct fdt_attach_args * const faa = aux;
821.1Sjmcneill
831.8Sthorpej	return of_compatible_match(faa->faa_phandle, compat_data);
841.1Sjmcneill}
851.1Sjmcneill
861.1Sjmcneillstatic void
871.1Sjmcneilltegra_lic_attach(device_t parent, device_t self, void *aux)
881.1Sjmcneill{
891.1Sjmcneill	struct tegra_lic_softc * const sc = device_private(self);
901.1Sjmcneill	struct fdt_attach_args * const faa = aux;
911.4Sjmcneill	bus_space_tag_t bst;
921.4Sjmcneill	bus_space_handle_t bsh;
931.4Sjmcneill	bus_addr_t addr;
941.4Sjmcneill	bus_size_t size;
951.4Sjmcneill	int error, index;
961.1Sjmcneill
971.1Sjmcneill	sc->sc_dev = self;
981.1Sjmcneill	sc->sc_phandle = faa->faa_phandle;
991.1Sjmcneill
1001.1Sjmcneill	error = fdtbus_register_interrupt_controller(self, faa->faa_phandle,
1011.1Sjmcneill	    &tegra_lic_funcs);
1021.1Sjmcneill	if (error) {
1031.1Sjmcneill		aprint_error(": couldn't register with fdtbus: %d\n", error);
1041.1Sjmcneill		return;
1051.1Sjmcneill	}
1061.1Sjmcneill
1071.1Sjmcneill	aprint_naive("\n");
1081.1Sjmcneill	aprint_normal(": LIC\n");
1091.4Sjmcneill
1101.4Sjmcneill	bst = faa->faa_bst;
1111.4Sjmcneill	for (index = 0; ; index++) {
1121.4Sjmcneill		error = fdtbus_get_reg(faa->faa_phandle, index, &addr, &size);
1131.4Sjmcneill		if (error != 0)
1141.4Sjmcneill			break;
1151.4Sjmcneill		error = bus_space_map(bst, addr, size, 0, &bsh);
1161.4Sjmcneill		if (error) {
1171.4Sjmcneill			aprint_error_dev(self, "can't map IC#%d: %d\n",
1181.4Sjmcneill			    index, error);
1191.4Sjmcneill			continue;
1201.4Sjmcneill		}
1211.4Sjmcneill
1221.4Sjmcneill		/* Clear interrupt enable for CPU */
1231.4Sjmcneill		bus_space_write_4(bst, bsh, LIC_CPU_IER_CLR_REG, 0xffffffff);
1241.4Sjmcneill
1251.4Sjmcneill		/* Route to IRQ */
1261.4Sjmcneill		bus_space_write_4(bst, bsh, LIC_CPU_IEP_CLASS_REG, 0);
1271.4Sjmcneill
1281.4Sjmcneill		bus_space_unmap(bst, bsh, size);
1291.4Sjmcneill	}
1301.1Sjmcneill}
1311.1Sjmcneill
1321.1Sjmcneillstatic void *
1331.3Smartytegra_lic_establish(device_t dev, u_int *specifier, int ipl, int flags,
1341.7Sjmcneill    int (*func)(void *), void *arg, const char *xname)
1351.1Sjmcneill{
1361.1Sjmcneill	int iflags = (flags & FDT_INTR_MPSAFE) ? IST_MPSAFE : 0;
1371.1Sjmcneill
1381.1Sjmcneill	/* 1st cell is the interrupt type; 0 is SPI, 1 is PPI */
1391.1Sjmcneill	/* 2nd cell is the interrupt number */
1401.1Sjmcneill	/* 3rd cell is flags */
1411.1Sjmcneill
1421.3Smarty	const u_int type = be32toh(specifier[0]);
1431.3Smarty	const u_int intr = be32toh(specifier[1]);
1441.1Sjmcneill	const u_int irq = type == 0 ? IRQ_SPI(intr) : IRQ_PPI(intr);
1451.3Smarty	const u_int trig = be32toh(specifier[2]) & 0xf;
1461.6Sthorpej	const u_int level = (trig & FDT_INTR_TYPE_DOUBLE_EDGE)
1471.6Sthorpej	    ? IST_EDGE : IST_LEVEL;
1481.1Sjmcneill
1491.7Sjmcneill	return intr_establish_xname(irq, ipl, level | iflags, func, arg,
1501.7Sjmcneill	    xname);
1511.1Sjmcneill}
1521.1Sjmcneill
1531.1Sjmcneillstatic void
1541.1Sjmcneilltegra_lic_disestablish(device_t dev, void *ih)
1551.1Sjmcneill{
1561.1Sjmcneill	intr_disestablish(ih);
1571.1Sjmcneill}
1581.1Sjmcneill
1591.1Sjmcneillstatic bool
1601.3Smartytegra_lic_intrstr(device_t dev, u_int *specifier, char *buf,
1611.1Sjmcneill    size_t buflen)
1621.1Sjmcneill{
1631.1Sjmcneill	/* 1st cell is the interrupt type; 0 is SPI, 1 is PPI */
1641.1Sjmcneill	/* 2nd cell is the interrupt number */
1651.1Sjmcneill	/* 3rd cell is flags */
1661.1Sjmcneill
1671.3Smarty	const u_int type = be32toh(specifier[0]);
1681.3Smarty	const u_int intr = be32toh(specifier[1]);
1691.1Sjmcneill	const u_int irq = type == 0 ? IRQ_SPI(intr) : IRQ_PPI(intr);
1701.1Sjmcneill
1711.4Sjmcneill	snprintf(buf, buflen, "irq %d", irq);
1721.1Sjmcneill
1731.1Sjmcneill	return true;
1741.1Sjmcneill}
175