tegra_lic.c revision 1.6
11.6Sthorpej/* $NetBSD: tegra_lic.c,v 1.6 2019/01/26 14:38:29 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.6Sthorpej__KERNEL_RCSID(0, "$NetBSD: tegra_lic.c,v 1.6 2019/01/26 14:38:29 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.1Sjmcneill int (*)(void *), void *); 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.1Sjmcneillstatic int 731.1Sjmcneilltegra_lic_match(device_t parent, cfdata_t cf, void *aux) 741.1Sjmcneill{ 751.5Sjmcneill const char * const compatible[] = { 761.5Sjmcneill "nvidia,tegra210-ictlr", 771.5Sjmcneill "nvidia,tegra124-ictlr", 781.5Sjmcneill NULL 791.5Sjmcneill }; 801.1Sjmcneill struct fdt_attach_args * const faa = aux; 811.1Sjmcneill 821.1Sjmcneill return of_match_compatible(faa->faa_phandle, compatible); 831.1Sjmcneill} 841.1Sjmcneill 851.1Sjmcneillstatic void 861.1Sjmcneilltegra_lic_attach(device_t parent, device_t self, void *aux) 871.1Sjmcneill{ 881.1Sjmcneill struct tegra_lic_softc * const sc = device_private(self); 891.1Sjmcneill struct fdt_attach_args * const faa = aux; 901.4Sjmcneill bus_space_tag_t bst; 911.4Sjmcneill bus_space_handle_t bsh; 921.4Sjmcneill bus_addr_t addr; 931.4Sjmcneill bus_size_t size; 941.4Sjmcneill int error, index; 951.1Sjmcneill 961.1Sjmcneill sc->sc_dev = self; 971.1Sjmcneill sc->sc_phandle = faa->faa_phandle; 981.1Sjmcneill 991.1Sjmcneill error = fdtbus_register_interrupt_controller(self, faa->faa_phandle, 1001.1Sjmcneill &tegra_lic_funcs); 1011.1Sjmcneill if (error) { 1021.1Sjmcneill aprint_error(": couldn't register with fdtbus: %d\n", error); 1031.1Sjmcneill return; 1041.1Sjmcneill } 1051.1Sjmcneill 1061.1Sjmcneill aprint_naive("\n"); 1071.1Sjmcneill aprint_normal(": LIC\n"); 1081.4Sjmcneill 1091.4Sjmcneill bst = faa->faa_bst; 1101.4Sjmcneill for (index = 0; ; index++) { 1111.4Sjmcneill error = fdtbus_get_reg(faa->faa_phandle, index, &addr, &size); 1121.4Sjmcneill if (error != 0) 1131.4Sjmcneill break; 1141.4Sjmcneill error = bus_space_map(bst, addr, size, 0, &bsh); 1151.4Sjmcneill if (error) { 1161.4Sjmcneill aprint_error_dev(self, "can't map IC#%d: %d\n", 1171.4Sjmcneill index, error); 1181.4Sjmcneill continue; 1191.4Sjmcneill } 1201.4Sjmcneill 1211.4Sjmcneill /* Clear interrupt enable for CPU */ 1221.4Sjmcneill bus_space_write_4(bst, bsh, LIC_CPU_IER_CLR_REG, 0xffffffff); 1231.4Sjmcneill 1241.4Sjmcneill /* Route to IRQ */ 1251.4Sjmcneill bus_space_write_4(bst, bsh, LIC_CPU_IEP_CLASS_REG, 0); 1261.4Sjmcneill 1271.4Sjmcneill bus_space_unmap(bst, bsh, size); 1281.4Sjmcneill } 1291.1Sjmcneill} 1301.1Sjmcneill 1311.1Sjmcneillstatic void * 1321.3Smartytegra_lic_establish(device_t dev, u_int *specifier, int ipl, int flags, 1331.1Sjmcneill int (*func)(void *), void *arg) 1341.1Sjmcneill{ 1351.1Sjmcneill int iflags = (flags & FDT_INTR_MPSAFE) ? IST_MPSAFE : 0; 1361.1Sjmcneill 1371.1Sjmcneill /* 1st cell is the interrupt type; 0 is SPI, 1 is PPI */ 1381.1Sjmcneill /* 2nd cell is the interrupt number */ 1391.1Sjmcneill /* 3rd cell is flags */ 1401.1Sjmcneill 1411.3Smarty const u_int type = be32toh(specifier[0]); 1421.3Smarty const u_int intr = be32toh(specifier[1]); 1431.1Sjmcneill const u_int irq = type == 0 ? IRQ_SPI(intr) : IRQ_PPI(intr); 1441.3Smarty const u_int trig = be32toh(specifier[2]) & 0xf; 1451.6Sthorpej const u_int level = (trig & FDT_INTR_TYPE_DOUBLE_EDGE) 1461.6Sthorpej ? IST_EDGE : IST_LEVEL; 1471.1Sjmcneill 1481.1Sjmcneill return intr_establish(irq, ipl, level | iflags, func, arg); 1491.1Sjmcneill} 1501.1Sjmcneill 1511.1Sjmcneillstatic void 1521.1Sjmcneilltegra_lic_disestablish(device_t dev, void *ih) 1531.1Sjmcneill{ 1541.1Sjmcneill intr_disestablish(ih); 1551.1Sjmcneill} 1561.1Sjmcneill 1571.1Sjmcneillstatic bool 1581.3Smartytegra_lic_intrstr(device_t dev, u_int *specifier, char *buf, 1591.1Sjmcneill size_t buflen) 1601.1Sjmcneill{ 1611.1Sjmcneill /* 1st cell is the interrupt type; 0 is SPI, 1 is PPI */ 1621.1Sjmcneill /* 2nd cell is the interrupt number */ 1631.1Sjmcneill /* 3rd cell is flags */ 1641.1Sjmcneill 1651.3Smarty const u_int type = be32toh(specifier[0]); 1661.3Smarty const u_int intr = be32toh(specifier[1]); 1671.1Sjmcneill const u_int irq = type == 0 ? IRQ_SPI(intr) : IRQ_PPI(intr); 1681.1Sjmcneill 1691.4Sjmcneill snprintf(buf, buflen, "irq %d", irq); 1701.1Sjmcneill 1711.1Sjmcneill return true; 1721.1Sjmcneill} 173