Home | History | Annotate | Line # | Download | only in samsung
exynos_combiner.c revision 1.1
      1  1.1  marty /*	$NetBSD: exynos_combiner.c,v 1.1 2015/12/21 00:52:50 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.1  marty __KERNEL_RCSID(1, "$NetBSD: exynos_combiner.c,v 1.1 2015/12/21 00:52:50 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_io.h>
     50  1.1  marty #include <arm/samsung/exynos_intr.h>
     51  1.1  marty 
     52  1.1  marty #include <dev/fdt/fdtvar.h>
     53  1.1  marty 
     54  1.1  marty struct exynos_combiner_softc {
     55  1.1  marty 	device_t		sc_dev;
     56  1.1  marty 	int			sc_phandle;
     57  1.1  marty 
     58  1.1  marty };
     59  1.1  marty 
     60  1.1  marty static int exynos_combiner_match(device_t, cfdata_t, void *);
     61  1.1  marty static void exynos_combiner_attach(device_t, device_t, void *);
     62  1.1  marty 
     63  1.1  marty static void *	exynos_combiner_establish(device_t, int, u_int, int, int,
     64  1.1  marty 		    int (*)(void *), void *);
     65  1.1  marty static void	exynos_combiner_disestablish(device_t, void *);
     66  1.1  marty static bool	exynos_combiner_intrstr(device_t, int, u_int, char *, size_t);
     67  1.1  marty 
     68  1.1  marty struct fdtbus_interrupt_controller_func exynos_combiner_funcs = {
     69  1.1  marty 	.establish = exynos_combiner_establish,
     70  1.1  marty 	.disestablish = exynos_combiner_disestablish,
     71  1.1  marty 	.intrstr = exynos_combiner_intrstr
     72  1.1  marty };
     73  1.1  marty 
     74  1.1  marty CFATTACH_DECL_NEW(exynos_intr, sizeof(struct exynos_combiner_softc),
     75  1.1  marty 	exynos_combiner_match, exynos_combiner_attach, NULL, NULL);
     76  1.1  marty 
     77  1.1  marty static int
     78  1.1  marty exynos_combiner_match(device_t parent, cfdata_t cf, void *aux)
     79  1.1  marty {
     80  1.1  marty 	const char * const compatible[] = { "samsung,exynos4210-combiner",
     81  1.1  marty 					    NULL };
     82  1.1  marty 	struct fdt_attach_args * const faa = aux;
     83  1.1  marty 	return of_match_compatible(faa->faa_phandle, compatible);
     84  1.1  marty }
     85  1.1  marty 
     86  1.1  marty static void
     87  1.1  marty exynos_combiner_attach(device_t parent, device_t self, void *aux)
     88  1.1  marty {
     89  1.1  marty 	struct exynos_combiner_softc * const sc
     90  1.1  marty 		= kmem_zalloc(sizeof(*sc), KM_SLEEP);
     91  1.1  marty 	struct fdt_attach_args * const faa = aux;
     92  1.1  marty 	bus_addr_t addr;
     93  1.1  marty 	bus_size_t size;
     94  1.1  marty 	int error;
     95  1.1  marty 
     96  1.1  marty 	if (fdtbus_get_reg(faa->faa_phandle, 0, &addr, &size) != 0) {
     97  1.1  marty 		aprint_error(": couldn't get registers\n");
     98  1.1  marty 		return;
     99  1.1  marty 	}
    100  1.1  marty 
    101  1.1  marty 	sc->sc_dev = self;
    102  1.1  marty 	sc->sc_phandle = faa->faa_phandle;
    103  1.1  marty 	error = fdtbus_register_interrupt_controller(self, faa->faa_phandle,
    104  1.1  marty 	    &exynos_combiner_funcs);
    105  1.1  marty 	if (error) {
    106  1.1  marty 		aprint_error(": couldn't register with fdtbus: %d\n", error);
    107  1.1  marty 		return;
    108  1.1  marty 	}
    109  1.1  marty 
    110  1.1  marty 
    111  1.1  marty 	aprint_normal(" @ 0x%08x: interrupt combiner,  NOT IMPLEMENTED",
    112  1.1  marty 		      (uint)addr);
    113  1.1  marty 	aprint_naive("\n");
    114  1.1  marty 	aprint_normal("\n");
    115  1.1  marty 
    116  1.1  marty }
    117  1.1  marty 
    118  1.1  marty 
    119  1.1  marty static void *
    120  1.1  marty exynos_combiner_establish(device_t dev, int phandle, u_int index, int ipl,
    121  1.1  marty 			  int flags,
    122  1.1  marty 			  int (*func)(void *), void *arg)
    123  1.1  marty {
    124  1.1  marty 	struct exynos_combiner_softc * const sc = device_private(dev);
    125  1.1  marty 	int iflags = (flags & FDT_INTR_MPSAFE) ? IST_MPSAFE : 0;
    126  1.1  marty 	u_int *interrupts;
    127  1.1  marty 	int interrupt_cells, len;
    128  1.1  marty 
    129  1.1  marty 	if (of_getprop_uint32(sc->sc_phandle, "#interrupt-cells",
    130  1.1  marty 	    &interrupt_cells)) {
    131  1.1  marty 		return NULL;
    132  1.1  marty 	}
    133  1.1  marty 
    134  1.1  marty 	len = OF_getproplen(phandle, "interrupts");
    135  1.1  marty 	if (len <= 0)
    136  1.1  marty 		return NULL;
    137  1.1  marty 
    138  1.1  marty 	const u_int clen = interrupt_cells * 4;
    139  1.1  marty 	const u_int nintr = len / interrupt_cells;
    140  1.1  marty 
    141  1.1  marty 	if (index >= nintr)
    142  1.1  marty 		return NULL;
    143  1.1  marty 
    144  1.1  marty 	interrupts = kmem_alloc(len, KM_SLEEP);
    145  1.1  marty 
    146  1.1  marty 	if (OF_getprop(phandle, "interrupts", interrupts, len) != len) {
    147  1.1  marty 		kmem_free(interrupts, len);
    148  1.1  marty 		return NULL;
    149  1.1  marty 	}
    150  1.1  marty 
    151  1.1  marty 	/* 1st cell is the interrupt type; */
    152  1.1  marty 	/* 2nd cell is the interrupt number */
    153  1.1  marty 	/* 3rd cell is flags */
    154  1.1  marty 
    155  1.1  marty 	const u_int type = be32toh(interrupts[index * clen + 0]);
    156  1.1  marty 	const u_int intr = be32toh(interrupts[index * clen + 1]);
    157  1.1  marty 	const u_int irq = type == 0 ? IRQ_SPI(intr) : IRQ_PPI(intr);
    158  1.1  marty 	const u_int trig = be32toh(interrupts[index * clen + 2]) & 0xf;
    159  1.1  marty 	const u_int level = (trig & 0x3) ? IST_EDGE : IST_LEVEL;
    160  1.1  marty 
    161  1.1  marty 	kmem_free(interrupts, len);
    162  1.1  marty 
    163  1.1  marty 	return intr_establish(irq, ipl, level | iflags, func, arg);
    164  1.1  marty }
    165  1.1  marty 
    166  1.1  marty static void
    167  1.1  marty exynos_combiner_disestablish(device_t dev, void *ih)
    168  1.1  marty {
    169  1.1  marty 	intr_disestablish(ih);
    170  1.1  marty }
    171  1.1  marty 
    172  1.1  marty static bool
    173  1.1  marty exynos_combiner_intrstr(device_t dev, int phandle, u_int index, char *buf,
    174  1.1  marty     size_t buflen)
    175  1.1  marty {
    176  1.1  marty 	struct exynos_combiner_softc * const sc = device_private(dev);
    177  1.1  marty 	u_int *interrupts;
    178  1.1  marty 	int interrupt_cells, len;
    179  1.1  marty 
    180  1.1  marty 	if (of_getprop_uint32(sc->sc_phandle, "#interrupt-cells",
    181  1.1  marty 	    &interrupt_cells)) {
    182  1.1  marty 		return false;
    183  1.1  marty 	}
    184  1.1  marty 
    185  1.1  marty 	len = OF_getproplen(phandle, "interrupts");
    186  1.1  marty 	if (len <= 0) {
    187  1.1  marty 		return false;
    188  1.1  marty 	}
    189  1.1  marty 
    190  1.1  marty 	const u_int clen = interrupt_cells * 4;
    191  1.1  marty 	const u_int nintr = len / interrupt_cells;
    192  1.1  marty 
    193  1.1  marty 	if (index >= nintr) {
    194  1.1  marty 		return false;
    195  1.1  marty 	}
    196  1.1  marty 
    197  1.1  marty 	interrupts = kmem_alloc(len, KM_SLEEP);
    198  1.1  marty 
    199  1.1  marty 	if (OF_getprop(phandle, "interrupts", interrupts, len) != len) {
    200  1.1  marty 		kmem_free(interrupts, len);
    201  1.1  marty 		return false;
    202  1.1  marty 	}
    203  1.1  marty 
    204  1.1  marty 	/* 1st cell is the interrupt type; */
    205  1.1  marty 	/* 2nd cell is the interrupt number */
    206  1.1  marty 	/* 3rd cell is flags */
    207  1.1  marty 
    208  1.1  marty 	const u_int type = be32toh(interrupts[index * clen + 0]);
    209  1.1  marty 	const u_int intr = be32toh(interrupts[index * clen + 1]);
    210  1.1  marty 	const u_int irq = type == 0 ? IRQ_SPI(intr) : IRQ_PPI(intr);
    211  1.1  marty 
    212  1.1  marty 	kmem_free(interrupts, len);
    213  1.1  marty 
    214  1.1  marty 	snprintf(buf, buflen, "LIC irq %d", irq);
    215  1.1  marty 
    216  1.1  marty 	return true;
    217  1.1  marty }
    218