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