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