Home | History | Annotate | Line # | Download | only in samsung
exynos_combiner.c revision 1.6
      1 /*	$NetBSD: exynos_combiner.c,v 1.6 2016/01/05 21:53:48 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.6 2016/01/05 21:53:48 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_GROUP_SIZE    0x10
     58 #define COMBINER_IRQS_PER_BLOCK   8
     59 #define COMBINER_BLOCKS_PER_GROUP 4
     60 #define COMBINER_N_BLOCKS        32
     61 
     62 struct exynos_combiner_softc;
     63 
     64 struct exynos_combiner_irq_entry {
     65 	int				irq_no;
     66 	int (*irq_handler)(void *);
     67 	void *				irq_arg;
     68 	struct exynos_combiner_irq_entry *irq_next;
     69 };
     70 
     71 struct exynos_combiner_irq_block {
     72 	int irq_block_no;
     73 	struct exynos_combiner_softc	*irq_sc;
     74 	struct exynos_combiner_irq_entry *irq_entries;
     75 	struct exynos_combiner_irq_block *irq_block_next;
     76 };
     77 
     78 struct exynos_combiner_softc {
     79 	device_t		sc_dev;
     80 	bus_space_tag_t		sc_bst;
     81 	bus_space_handle_t	sc_bsh;
     82 	int			sc_phandle;
     83 	struct exynos_combiner_irq_block *irq_blocks;
     84 };
     85 
     86 static int exynos_combiner_match(device_t, cfdata_t, void *);
     87 static void exynos_combiner_attach(device_t, device_t, void *);
     88 
     89 static void *	exynos_combiner_establish(device_t, u_int *, int, int,
     90 		    int (*)(void *), void *);
     91 static void	exynos_combiner_disestablish(device_t, void *);
     92 static bool	exynos_combiner_intrstr(device_t, u_int  *, char *,
     93 					size_t);
     94 
     95 struct fdtbus_interrupt_controller_func exynos_combiner_funcs = {
     96 	.establish = exynos_combiner_establish,
     97 	.disestablish = exynos_combiner_disestablish,
     98 	.intrstr = exynos_combiner_intrstr
     99 };
    100 
    101 CFATTACH_DECL_NEW(exynos_intr, sizeof(struct exynos_combiner_softc),
    102 	exynos_combiner_match, exynos_combiner_attach, NULL, NULL);
    103 
    104 static int
    105 exynos_combiner_match(device_t parent, cfdata_t cf, void *aux)
    106 {
    107 	const char * const compatible[] = { "samsung,exynos4210-combiner",
    108 					    NULL };
    109 	struct fdt_attach_args * const faa = aux;
    110 	return of_match_compatible(faa->faa_phandle, compatible);
    111 }
    112 
    113 static void
    114 exynos_combiner_attach(device_t parent, device_t self, void *aux)
    115 {
    116 	struct exynos_combiner_softc * const sc = device_private(self);
    117 	struct fdt_attach_args * const faa = aux;
    118 	bus_addr_t addr;
    119 	bus_size_t size;
    120 	int error;
    121 
    122 	if (fdtbus_get_reg(faa->faa_phandle, 0, &addr, &size) != 0) {
    123 		aprint_error(": couldn't get registers\n");
    124 		return;
    125 	}
    126 
    127 	sc->sc_dev = self;
    128 	sc->sc_phandle = faa->faa_phandle;
    129 	sc->sc_bst = faa->faa_bst;
    130 	sc->irq_blocks = NULL;
    131 
    132 	error = bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh);
    133 	if (error) {
    134 		aprint_error(": couldn't map %#llx: %d",
    135 			     (uint64_t)addr, error);
    136 		return;
    137 	}
    138 
    139 	error = fdtbus_register_interrupt_controller(self, faa->faa_phandle,
    140 	    &exynos_combiner_funcs);
    141 	if (error) {
    142 		aprint_error(": couldn't register with fdtbus: %d\n", error);
    143 		return;
    144 	}
    145 
    146 	aprint_normal(" @ 0x%08x: interrupt combiner", (uint)addr);
    147 	aprint_naive("\n");
    148 	aprint_normal("\n");
    149 
    150 }
    151 
    152 static struct exynos_combiner_irq_block *
    153 exynos_combiner_new_block(struct exynos_combiner_softc *sc, int block_no)
    154 {
    155 	struct exynos_combiner_irq_block *n = kmem_zalloc(sizeof(*n),
    156 							  KM_SLEEP);
    157 	n->irq_block_no = block_no;
    158 	n->irq_block_next = sc->irq_blocks;
    159 	sc->irq_blocks = n;
    160 	return n;
    161 }
    162 
    163 static struct exynos_combiner_irq_block *
    164 exynos_combiner_get_block(struct exynos_combiner_softc *sc, int block)
    165 {
    166 	for (struct exynos_combiner_irq_block *b = sc->irq_blocks;
    167 	     b; b = b->irq_block_next) {
    168 		if (b->irq_block_no == block)
    169 			return b;
    170 	}
    171 	return NULL;
    172 }
    173 
    174 static struct exynos_combiner_irq_entry *
    175 exynos_combiner_new_irq(struct exynos_combiner_irq_block *block,
    176 			int irq, int (*func)(void *), void *arg)
    177 {
    178 	struct exynos_combiner_irq_entry * n = kmem_zalloc(sizeof(*n),
    179 							   KM_SLEEP);
    180 	n->irq_no = irq;
    181 	n->irq_handler = func;
    182 	n->irq_next = block->irq_entries;
    183 	n->irq_arg = arg;
    184 	block->irq_entries = n;
    185 	return n;
    186 }
    187 
    188 static struct exynos_combiner_irq_entry *
    189 exynos_combiner_get_irq(struct exynos_combiner_irq_block *b, int irq)
    190 {
    191 	for (struct exynos_combiner_irq_entry *p = b->irq_entries; p;
    192 	     p = p->irq_next) {
    193 		if (p->irq_no == irq)
    194 			return p;
    195 	}
    196 	return NULL;
    197 }
    198 
    199 static int exynos_combiner_irq(void *cookie)
    200 {
    201 	struct exynos_combiner_irq_block *blockp = cookie;
    202 	struct exynos_combiner_softc *sc = blockp->irq_sc;
    203 	int intr = blockp->irq_block_no;
    204 	int iblock =
    205 		intr / COMBINER_BLOCKS_PER_GROUP * COMBINER_GROUP_SIZE
    206 		+ COMBINER_IESR_OFFSET;
    207 	int istatus =
    208 		bus_space_read_4(sc->sc_bst, sc->sc_bsh, iblock);
    209 	istatus >>= (intr % 4) *8;
    210 	for (int irq = 0; irq < 8; irq++) {
    211 		if (istatus & 1 << irq) {
    212 			struct exynos_combiner_irq_entry *e =
    213 				exynos_combiner_get_irq(blockp, irq);
    214 			if (e)
    215 				e->irq_handler(e->irq_arg);
    216 			else
    217 				printf("%s: Unexpected irq %d, %d\n", __func__,
    218 				       intr, irq);
    219 		}
    220 	}
    221 	return 0;
    222 }
    223 
    224 static void *
    225 exynos_combiner_establish(device_t dev, u_int *specifier,
    226 			  int ipl, int flags,
    227 			  int (*func)(void *), void *arg)
    228 {
    229 	struct exynos_combiner_softc * const sc = device_private(dev);
    230 	struct exynos_combiner_irq_block *blockp;
    231 	struct exynos_combiner_irq_entry *entryp;
    232 
    233 	const u_int intr = be32toh(specifier[0]);
    234 	const u_int irq = be32toh(specifier[1]);
    235 
    236 	int iblock =
    237 		intr / COMBINER_BLOCKS_PER_GROUP * COMBINER_GROUP_SIZE
    238 		+ COMBINER_IESR_OFFSET;
    239 
    240 	blockp =  exynos_combiner_get_block(sc, intr);
    241 	if (!blockp) {
    242 		blockp = exynos_combiner_new_block(sc, intr);
    243 		KASSERT(blockp);
    244 		intr_establish(intr, ipl, IST_LEVEL, exynos_combiner_irq,
    245 			       blockp);
    246 	}
    247 
    248 	entryp = exynos_combiner_get_irq(blockp, irq);
    249 	if (entryp)
    250 		return NULL;
    251 	entryp = exynos_combiner_new_irq(blockp, irq, func, arg);
    252 	KASSERT(entryp);
    253 
    254 	int istatus =
    255 		bus_space_read_4(sc->sc_bst, sc->sc_bsh, iblock);
    256 	istatus |= 1 << (irq + ((intr % 4) * 8));
    257 	bus_space_write_4(sc->sc_bst, sc->sc_bsh, iblock, istatus);
    258 	return (void *)istatus;
    259 }
    260 
    261 static void
    262 exynos_combiner_disestablish(device_t dev, void *ih)
    263 {
    264 	/* MJF: Find the ih and disable the handler. */
    265 	intr_disestablish(ih);
    266 }
    267 
    268 static bool
    269 exynos_combiner_intrstr(device_t dev, u_int *specifier, char *buf,
    270 			size_t buflen)
    271 {
    272 
    273 	/* 1st cell is the interrupt block */
    274 	/* 2nd cell is the interrupt number */
    275 
    276 	const u_int intr = be32toh(specifier[0]);
    277 	const u_int irq = be32toh(specifier[1]);
    278 
    279 	snprintf(buf, buflen, "combiner intr %d irq %d", intr, irq);
    280 
    281 	return true;
    282 }
    283