Home | History | Annotate | Line # | Download | only in samsung
exynos_combiner.c revision 1.3
      1 /*	$NetBSD: exynos_combiner.c,v 1.3 2015/12/24 21:20:17 marty Exp $ */
      2 
      3 /*-
      4 * Copyright (c) 2015 The NetBSD Foundation, Inc.
      5 * All rights reserved.
      6 *
      7 * This code is derived from software contributed to The NetBSD Foundation
      8 * by Marty Fouts
      9 *
     10 * Redistribution and use in source and binary forms, with or without
     11 * modification, are permitted provided that the following conditions
     12 * are met:
     13 * 1. Redistributions of source code must retain the above copyright
     14 *    notice, this list of conditions and the following disclaimer.
     15 * 2. Redistributions in binary form must reproduce the above copyright
     16 *    notice, this list of conditions and the following disclaimer in the
     17 *    documentation and/or other materials provided with the distribution.
     18 *
     19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29 * POSSIBILITY OF SUCH DAMAGE.
     30 */
     31 
     32 #include "opt_exynos.h"
     33 #include "opt_arm_debug.h"
     34 #include "gpio.h"
     35 
     36 #include <sys/cdefs.h>
     37 __KERNEL_RCSID(1, "$NetBSD: exynos_combiner.c,v 1.3 2015/12/24 21:20:17 marty Exp $");
     38 
     39 #include <sys/param.h>
     40 #include <sys/bus.h>
     41 #include <sys/device.h>
     42 #include <sys/intr.h>
     43 #include <sys/systm.h>
     44 #include <sys/kmem.h>
     45 
     46 #include <arm/cortex/gic_intr.h>
     47 
     48 #include <arm/samsung/exynos_reg.h>
     49 #include <arm/samsung/exynos_intr.h>
     50 
     51 #include <dev/fdt/fdtvar.h>
     52 
     53 #define COMBINER_IESR_OFFSET  0x00
     54 #define COMBINER_IECR_OFFSET  0x04
     55 #define COMBINER_ISTR_OFFSET  0x08
     56 #define COMBINER_IMSR_OFFSET  0x0C
     57 #define COMBINER_BLOCK_SIZE   0x10
     58 
     59 struct exynos_combiner_softc {
     60 	device_t		sc_dev;
     61 	bus_space_tag_t		sc_bst;
     62 	bus_space_handle_t	sc_bsh;
     63 	int			sc_phandle;
     64 
     65 };
     66 
     67 static int exynos_combiner_match(device_t, cfdata_t, void *);
     68 static void exynos_combiner_attach(device_t, device_t, void *);
     69 
     70 static void *	exynos_combiner_establish(device_t, int, u_int, int, int,
     71 		    int (*)(void *), void *);
     72 static void	exynos_combiner_disestablish(device_t, void *);
     73 static bool	exynos_combiner_intrstr(device_t, int, u_int, char *, size_t);
     74 
     75 struct fdtbus_interrupt_controller_func exynos_combiner_funcs = {
     76 	.establish = exynos_combiner_establish,
     77 	.disestablish = exynos_combiner_disestablish,
     78 	.intrstr = exynos_combiner_intrstr
     79 };
     80 
     81 CFATTACH_DECL_NEW(exynos_intr, sizeof(struct exynos_combiner_softc),
     82 	exynos_combiner_match, exynos_combiner_attach, NULL, NULL);
     83 
     84 static int
     85 exynos_combiner_match(device_t parent, cfdata_t cf, void *aux)
     86 {
     87 	const char * const compatible[] = { "samsung,exynos4210-combiner",
     88 					    NULL };
     89 	struct fdt_attach_args * const faa = aux;
     90 	return of_match_compatible(faa->faa_phandle, compatible);
     91 }
     92 
     93 static void
     94 exynos_combiner_attach(device_t parent, device_t self, void *aux)
     95 {
     96 	struct exynos_combiner_softc * const sc
     97 		= kmem_zalloc(sizeof(*sc), KM_SLEEP);
     98 	struct fdt_attach_args * const faa = aux;
     99 	bus_addr_t addr;
    100 	bus_size_t size;
    101 	int error;
    102 
    103 	if (fdtbus_get_reg(faa->faa_phandle, 0, &addr, &size) != 0) {
    104 		aprint_error(": couldn't get registers\n");
    105 		return;
    106 	}
    107 
    108 	sc->sc_dev = self;
    109 	sc->sc_phandle = faa->faa_phandle;
    110 	sc->sc_bst = faa->faa_bst;
    111 
    112 	error = bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh);
    113 	if (error) {
    114 		aprint_error(": couldn't map %#llx: %d",
    115 			     (uint64_t)addr, error);
    116 		return;
    117 	}
    118 
    119 	error = fdtbus_register_interrupt_controller(self, faa->faa_phandle,
    120 	    &exynos_combiner_funcs);
    121 	if (error) {
    122 		aprint_error(": couldn't register with fdtbus: %d\n", error);
    123 		return;
    124 	}
    125 
    126 	aprint_normal(" @ 0x%08x: interrupt combiner", (uint)addr);
    127 	aprint_naive("\n");
    128 	aprint_normal("\n");
    129 
    130 }
    131 
    132 static void *
    133 exynos_combiner_establish(device_t dev, int phandle, u_int index, int ipl,
    134 			  int flags,
    135 			  int (*func)(void *), void *arg)
    136 {
    137 	struct exynos_combiner_softc * const sc = device_private(dev);
    138 	int iflags = (flags & FDT_INTR_MPSAFE) ? IST_MPSAFE : 0;
    139 	int iblock = index >> 3;
    140 	int ioffset = index & 0x07;
    141 	int block_offset =
    142 		iblock * COMBINER_BLOCK_SIZE + COMBINER_IESR_OFFSET;
    143 	int istatus =
    144 		bus_space_read_4(sc->sc_bst, sc->sc_bsh, block_offset);
    145 	printf("Establishing irq %d (0x%x) @ iblock = %d, ioffset = %d\n",
    146 	       index, index, iblock, ioffset);
    147 	istatus |= 1 << ioffset;
    148 	bus_space_write_4(sc->sc_bst, sc->sc_bsh, block_offset, istatus);
    149 	return intr_establish(index, ipl, iflags, func, arg);
    150 }
    151 
    152 static void
    153 exynos_combiner_disestablish(device_t dev, void *ih)
    154 {
    155 	intr_disestablish(ih);
    156 }
    157 
    158 static bool
    159 exynos_combiner_intrstr(device_t dev, int phandle, u_int index, char *buf,
    160     size_t buflen)
    161 {
    162 	struct exynos_combiner_softc * const sc = device_private(dev);
    163 	u_int *interrupts;
    164 	int interrupt_cells, len;
    165 
    166 	if (of_getprop_uint32(sc->sc_phandle, "#interrupt-cells",
    167 	    &interrupt_cells)) {
    168 		return false;
    169 	}
    170 
    171 	len = OF_getproplen(phandle, "interrupts");
    172 	if (len <= 0) {
    173 		return false;
    174 	}
    175 
    176 	const u_int clen = interrupt_cells * 4;
    177 	const u_int nintr = len / interrupt_cells;
    178 
    179 	if (index >= nintr) {
    180 		return false;
    181 	}
    182 
    183 	interrupts = kmem_alloc(len, KM_SLEEP);
    184 
    185 	if (OF_getprop(phandle, "interrupts", interrupts, len) != len) {
    186 		kmem_free(interrupts, len);
    187 		return false;
    188 	}
    189 
    190 	/* 1st cell is the interrupt type; */
    191 	/* 2nd cell is the interrupt number */
    192 	/* 3rd cell is flags */
    193 
    194 	const u_int type = be32toh(interrupts[index * clen + 0]);
    195 	const u_int intr = be32toh(interrupts[index * clen + 1]);
    196 	const u_int irq = type == 0 ? IRQ_SPI(intr) : IRQ_PPI(intr);
    197 
    198 	kmem_free(interrupts, len);
    199 
    200 	snprintf(buf, buflen, "LIC irq %d", irq);
    201 
    202 	return true;
    203 }
    204