octeon_intc.c revision 1.2
11.2Sjmcneill/* $NetBSD: octeon_intc.c,v 1.2 2021/01/15 00:38:23 jmcneill Exp $ */
21.1Sjmcneill
31.1Sjmcneill/*-
41.1Sjmcneill * Copyright (c) 2020 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.2Sjmcneill__KERNEL_RCSID(0, "$NetBSD: octeon_intc.c,v 1.2 2021/01/15 00:38:23 jmcneill 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 <dev/fdt/fdtvar.h>
411.1Sjmcneill
421.1Sjmcneill#include <arch/mips/cavium/octeonvar.h>
431.1Sjmcneill
441.1Sjmcneillstatic int	octeon_intc_match(device_t, cfdata_t, void *);
451.1Sjmcneillstatic void	octeon_intc_attach(device_t, device_t, void *);
461.1Sjmcneill
471.1Sjmcneillstatic void *	octeon_intc_establish(device_t, u_int *, int, int,
481.2Sjmcneill		    int (*)(void *), void *, const char *);
491.1Sjmcneillstatic void	octeon_intc_disestablish(device_t, void *);
501.1Sjmcneillstatic bool	octeon_intc_intrstr(device_t, u_int *, char *, size_t);
511.1Sjmcneill
521.1Sjmcneillstruct fdtbus_interrupt_controller_func octeon_intc_funcs = {
531.1Sjmcneill	.establish = octeon_intc_establish,
541.1Sjmcneill	.disestablish = octeon_intc_disestablish,
551.1Sjmcneill	.intrstr = octeon_intc_intrstr
561.1Sjmcneill};
571.1Sjmcneill
581.1Sjmcneillenum octeon_intc_type {
591.1Sjmcneill	OCTEON_INTC_CIU,
601.1Sjmcneill};
611.1Sjmcneill
621.1Sjmcneillstruct octeon_intc_softc {
631.1Sjmcneill	device_t		sc_dev;
641.1Sjmcneill	int			sc_phandle;
651.1Sjmcneill	enum octeon_intc_type	sc_type;
661.1Sjmcneill	const char		*sc_descr;
671.1Sjmcneill};
681.1Sjmcneill
691.1SjmcneillCFATTACH_DECL_NEW(octintc, sizeof(struct octeon_intc_softc),
701.1Sjmcneill	octeon_intc_match, octeon_intc_attach, NULL, NULL);
711.1Sjmcneill
721.1Sjmcneillstatic const struct of_compat_data compat_data[] = {
731.1Sjmcneill	{ "cavium,octeon-3860-ciu",		OCTEON_INTC_CIU },
741.1Sjmcneill	{ NULL }
751.1Sjmcneill};
761.1Sjmcneill
771.1Sjmcneillstatic int
781.1Sjmcneillocteon_intc_match(device_t parent, cfdata_t cf, void *aux)
791.1Sjmcneill{
801.1Sjmcneill	struct fdt_attach_args * const faa = aux;
811.1Sjmcneill
821.1Sjmcneill	return of_match_compat_data(faa->faa_phandle, compat_data);
831.1Sjmcneill}
841.1Sjmcneill
851.1Sjmcneillstatic void
861.1Sjmcneillocteon_intc_attach(device_t parent, device_t self, void *aux)
871.1Sjmcneill{
881.1Sjmcneill	struct octeon_intc_softc * const sc = device_private(self);
891.1Sjmcneill	struct fdt_attach_args * const faa = aux;
901.1Sjmcneill	const int phandle = faa->faa_phandle;
911.1Sjmcneill	int error;
921.1Sjmcneill
931.1Sjmcneill	sc->sc_dev = self;
941.1Sjmcneill	sc->sc_phandle = phandle;
951.1Sjmcneill	sc->sc_type = of_search_compatible(phandle, compat_data)->data;
961.1Sjmcneill
971.1Sjmcneill	switch (sc->sc_type) {
981.1Sjmcneill	case OCTEON_INTC_CIU:
991.1Sjmcneill		sc->sc_descr = "CIU";
1001.1Sjmcneill		break;
1011.1Sjmcneill	}
1021.1Sjmcneill
1031.1Sjmcneill	error = fdtbus_register_interrupt_controller(self, phandle,
1041.1Sjmcneill	    &octeon_intc_funcs);
1051.1Sjmcneill	if (error != 0) {
1061.1Sjmcneill		aprint_error(": couldn't register with fdtbus: %d\n", error);
1071.1Sjmcneill		return;
1081.1Sjmcneill	}
1091.1Sjmcneill
1101.1Sjmcneill	aprint_naive("\n");
1111.1Sjmcneill	aprint_normal(": %s\n", sc->sc_descr);
1121.1Sjmcneill}
1131.1Sjmcneill
1141.1Sjmcneillstatic void *
1151.1Sjmcneillocteon_intc_establish(device_t dev, u_int *specifier, int ipl, int flags,
1161.2Sjmcneill    int (*func)(void *), void *arg, const char *xname)
1171.1Sjmcneill{
1181.1Sjmcneill	struct octeon_intc_softc * const sc = device_private(dev);
1191.1Sjmcneill
1201.1Sjmcneill	/* 1st cell is the controller register (0 or 1) */
1211.1Sjmcneill	/* 2nd cell is the bit within the register (0..63) */
1221.1Sjmcneill
1231.1Sjmcneill	const u_int reg = be32toh(specifier[0]);
1241.1Sjmcneill	const u_int bit = be32toh(specifier[1]);
1251.1Sjmcneill	const u_int irq = (reg * 64) + bit;
1261.1Sjmcneill
1271.1Sjmcneill	if (irq >= NIRQS) {
1281.1Sjmcneill		aprint_error_dev(dev, "%s irq %d (%d, %d) out of range\n",
1291.1Sjmcneill		    sc->sc_descr, irq, reg, bit);
1301.1Sjmcneill		return NULL;
1311.1Sjmcneill	}
1321.1Sjmcneill
1331.1Sjmcneill	return octeon_intr_establish(irq, ipl, func, arg);
1341.1Sjmcneill}
1351.1Sjmcneill
1361.1Sjmcneillstatic void
1371.1Sjmcneillocteon_intc_disestablish(device_t dev, void *ih)
1381.1Sjmcneill{
1391.1Sjmcneill	octeon_intr_disestablish(ih);
1401.1Sjmcneill}
1411.1Sjmcneill
1421.1Sjmcneillstatic bool
1431.1Sjmcneillocteon_intc_intrstr(device_t dev, u_int *specifier, char *buf,
1441.1Sjmcneill    size_t buflen)
1451.1Sjmcneill{
1461.1Sjmcneill	struct octeon_intc_softc * const sc = device_private(dev);
1471.1Sjmcneill
1481.1Sjmcneill	/* 1st cell is the controller register (0 or 1) */
1491.1Sjmcneill	/* 2nd cell is the bit within the register (0..63) */
1501.1Sjmcneill
1511.1Sjmcneill	const u_int reg = be32toh(specifier[0]);
1521.1Sjmcneill	const u_int bit = be32toh(specifier[1]);
1531.1Sjmcneill	const u_int irq = (reg * 64) + bit;
1541.1Sjmcneill
1551.1Sjmcneill	snprintf(buf, buflen, "%s irq %d", sc->sc_descr, irq);
1561.1Sjmcneill
1571.1Sjmcneill	return true;
1581.1Sjmcneill}
159