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