Home | History | Annotate | Line # | Download | only in samsung
exynos_combiner.c revision 1.9
      1 /*	$NetBSD: exynos_combiner.c,v 1.9 2018/10/18 07:35:15 skrll 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.9 2018/10/18 07:35:15 skrll 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_GROUP_BASE(group)	(((group) / 4) * 0x10)
     54 #define	COMBINER_GROUP_MASK(group)	(0xff << (((group) % 4) * 8))
     55 
     56 #define	COMBINER_IESR_REG(group)	(COMBINER_GROUP_BASE(group) + 0x00)
     57 #define	COMBINER_IECR_REG(group)	(COMBINER_GROUP_BASE(group) + 0x04)
     58 #define	COMBINER_ISTR_REG(group)	(COMBINER_GROUP_BASE(group) + 0x08)
     59 #define	COMBINER_IMSR_REG(group)	(COMBINER_GROUP_BASE(group) + 0x0c)
     60 
     61 struct exynos_combiner_softc;
     62 
     63 struct exynos_combiner_irq_entry {
     64 	int				irq_no;
     65 	int (*irq_handler)(void *);
     66 	void *				irq_arg;
     67 	struct exynos_combiner_irq_entry *irq_next;
     68 	bool				irq_mpsafe;
     69 };
     70 
     71 struct exynos_combiner_irq_group {
     72 	int irq_group_no;
     73 	struct exynos_combiner_softc	*irq_sc;
     74 	struct exynos_combiner_irq_entry *irq_entries;
     75 	struct exynos_combiner_irq_group *irq_group_next;
     76 	void *irq_ih;
     77 	int irq_ipl;
     78 };
     79 
     80 struct exynos_combiner_softc {
     81 	device_t		sc_dev;
     82 	bus_space_tag_t		sc_bst;
     83 	bus_space_handle_t	sc_bsh;
     84 	int			sc_phandle;
     85 	struct exynos_combiner_irq_group *irq_groups;
     86 };
     87 
     88 static int exynos_combiner_match(device_t, cfdata_t, void *);
     89 static void exynos_combiner_attach(device_t, device_t, void *);
     90 
     91 static void *	exynos_combiner_establish(device_t, u_int *, int, int,
     92 		    int (*)(void *), void *);
     93 static void	exynos_combiner_disestablish(device_t, void *);
     94 static bool	exynos_combiner_intrstr(device_t, u_int  *, char *,
     95 					size_t);
     96 
     97 struct fdtbus_interrupt_controller_func exynos_combiner_funcs = {
     98 	.establish = exynos_combiner_establish,
     99 	.disestablish = exynos_combiner_disestablish,
    100 	.intrstr = exynos_combiner_intrstr
    101 };
    102 
    103 CFATTACH_DECL_NEW(exynos_intr, sizeof(struct exynos_combiner_softc),
    104 	exynos_combiner_match, exynos_combiner_attach, NULL, NULL);
    105 
    106 static int
    107 exynos_combiner_match(device_t parent, cfdata_t cf, void *aux)
    108 {
    109 	const char * const compatible[] = { "samsung,exynos4210-combiner",
    110 					    NULL };
    111 	struct fdt_attach_args * const faa = aux;
    112 	return of_match_compatible(faa->faa_phandle, compatible);
    113 }
    114 
    115 static void
    116 exynos_combiner_attach(device_t parent, device_t self, void *aux)
    117 {
    118 	struct exynos_combiner_softc * const sc = device_private(self);
    119 	struct fdt_attach_args * const faa = aux;
    120 	bus_addr_t addr;
    121 	bus_size_t size;
    122 	int error;
    123 
    124 	if (fdtbus_get_reg(faa->faa_phandle, 0, &addr, &size) != 0) {
    125 		aprint_error(": couldn't get registers\n");
    126 		return;
    127 	}
    128 
    129 	sc->sc_dev = self;
    130 	sc->sc_phandle = faa->faa_phandle;
    131 	sc->sc_bst = faa->faa_bst;
    132 
    133 	error = bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh);
    134 	if (error) {
    135 		aprint_error(": couldn't map %#llx: %d",
    136 			     (uint64_t)addr, error);
    137 		return;
    138 	}
    139 
    140 	error = fdtbus_register_interrupt_controller(self, faa->faa_phandle,
    141 	    &exynos_combiner_funcs);
    142 	if (error) {
    143 		aprint_error(": couldn't register with fdtbus: %d\n", error);
    144 		return;
    145 	}
    146 
    147 	aprint_naive("\n");
    148 	aprint_normal(" @ 0x%08x: interrupt combiner\n", (uint)addr);
    149 
    150 }
    151 
    152 static struct exynos_combiner_irq_group *
    153 exynos_combiner_new_group(struct exynos_combiner_softc *sc, int group_no)
    154 {
    155 	struct exynos_combiner_irq_group *n = kmem_zalloc(sizeof(*n),
    156 							  KM_SLEEP);
    157 	n->irq_group_no = group_no;
    158 	n->irq_group_next = sc->irq_groups;
    159 	n->irq_sc = sc;
    160 	sc->irq_groups = n;
    161 	return n;
    162 }
    163 
    164 static struct exynos_combiner_irq_group *
    165 exynos_combiner_get_group(struct exynos_combiner_softc *sc, int group_no)
    166 {
    167 	for (struct exynos_combiner_irq_group *g = sc->irq_groups;
    168 	     g; g = g->irq_group_next) {
    169 		if (g->irq_group_no == group_no)
    170 			return g;
    171 	}
    172 	return NULL;
    173 }
    174 
    175 static struct exynos_combiner_irq_entry *
    176 exynos_combiner_new_irq(struct exynos_combiner_irq_group *group,
    177 			int irq, bool mpsafe, int (*func)(void *), void *arg)
    178 {
    179 	struct exynos_combiner_irq_entry * n = kmem_zalloc(sizeof(*n),
    180 							   KM_SLEEP);
    181 	n->irq_no = irq;
    182 	n->irq_handler = func;
    183 	n->irq_next = group->irq_entries;
    184 	n->irq_arg = arg;
    185 	n->irq_mpsafe = mpsafe;
    186 	group->irq_entries = n;
    187 	return n;
    188 }
    189 
    190 static struct exynos_combiner_irq_entry *
    191 exynos_combiner_get_irq(struct exynos_combiner_irq_group *g, int irq)
    192 {
    193 	for (struct exynos_combiner_irq_entry *p = g->irq_entries; p;
    194 	     p = p->irq_next) {
    195 		if (p->irq_no == irq)
    196 			return p;
    197 	}
    198 	return NULL;
    199 }
    200 
    201 static int
    202 exynos_combiner_irq(void *cookie)
    203 {
    204 	struct exynos_combiner_irq_group *groupp = cookie;
    205 	struct exynos_combiner_softc *sc = groupp->irq_sc;
    206 	int rv = 0;
    207 
    208 	const int group_no = groupp->irq_group_no;
    209 
    210 	const uint32_t istr =
    211 	    bus_space_read_4(sc->sc_bst, sc->sc_bsh, COMBINER_ISTR_REG(group_no));
    212 	const uint32_t istatus = __SHIFTOUT(istr, COMBINER_GROUP_MASK(group_no));
    213 
    214 	for (int irq = 0; irq < 8; irq++) {
    215 		if (istatus & __BIT(irq)) {
    216 			struct exynos_combiner_irq_entry *e =
    217 				exynos_combiner_get_irq(groupp, irq);
    218 			if (e) {
    219 				if (!e->irq_mpsafe)
    220 					KERNEL_LOCK(1, curlwp);
    221 				rv += e->irq_handler(e->irq_arg);
    222 				if (!e->irq_mpsafe)
    223 					KERNEL_UNLOCK_ONE(curlwp);
    224 			} else
    225 				printf("%s: Unexpected irq (%d) on group %d\n", __func__,
    226 				       irq, group_no);
    227 		}
    228 	}
    229 
    230 	return !!rv;
    231 }
    232 
    233 static void *
    234 exynos_combiner_establish(device_t dev, u_int *specifier,
    235 			  int ipl, int flags,
    236 			  int (*func)(void *), void *arg)
    237 {
    238 	struct exynos_combiner_softc * const sc = device_private(dev);
    239 	struct exynos_combiner_irq_group *groupp;
    240 	struct exynos_combiner_irq_entry *entryp;
    241 	const bool mpsafe = (flags & FDT_INTR_MPSAFE) != 0;
    242 	uint32_t iesr;
    243 
    244 	const u_int group = be32toh(specifier[0]);
    245 	const u_int intr = be32toh(specifier[1]);
    246 
    247 	groupp = exynos_combiner_get_group(sc, group);
    248 	if (!groupp) {
    249 		groupp = exynos_combiner_new_group(sc, group);
    250 		if (arg == NULL) {
    251 			groupp->irq_ih = fdtbus_intr_establish(sc->sc_phandle, group,
    252 			    ipl /* XXX */, flags, func, NULL);
    253 		} else {
    254 			groupp->irq_ih = fdtbus_intr_establish(sc->sc_phandle, group,
    255 			    ipl /* XXX */, FDT_INTR_MPSAFE, exynos_combiner_irq,
    256 			    groupp);
    257 		}
    258 		KASSERT(groupp->irq_ih != NULL);
    259 		groupp->irq_ipl = ipl;
    260 	} else if (groupp->irq_ipl != ipl) {
    261 		aprint_error_dev(dev,
    262 		    "interrupt combiner cannot share interrupts with different ipl\n");
    263 		return NULL;
    264 	}
    265 
    266 	if (exynos_combiner_get_irq(groupp, intr) != NULL)
    267 		return NULL;
    268 
    269 	entryp = exynos_combiner_new_irq(groupp, intr, mpsafe, func, arg);
    270 
    271 	iesr = bus_space_read_4(sc->sc_bst, sc->sc_bsh, COMBINER_IESR_REG(group));
    272 	iesr |= __SHIFTIN((1 << intr), COMBINER_GROUP_MASK(group));
    273 	bus_space_write_4(sc->sc_bst, sc->sc_bsh, COMBINER_IESR_REG(group), iesr);
    274 
    275 	return entryp;
    276 }
    277 
    278 static void
    279 exynos_combiner_disestablish(device_t dev, void *ih)
    280 {
    281 	/* MJF: Find the ih and disable the handler. */
    282 	panic("exynos_combiner_disestablish not implemented");
    283 }
    284 
    285 static bool
    286 exynos_combiner_intrstr(device_t dev, u_int *specifier, char *buf,
    287 			size_t buflen)
    288 {
    289 
    290 	/* 1st cell is the combiner group number */
    291 	/* 2nd cell is the interrupt number within the group */
    292 
    293 	const u_int group = be32toh(specifier[0]);
    294 	const u_int intr = be32toh(specifier[1]);
    295 
    296 	snprintf(buf, buflen, "interrupt combiner group %d intr %d", group, intr);
    297 
    298 	return true;
    299 }
    300