Home | History | Annotate | Line # | Download | only in s3c2xx0
s3c2410_extint.c revision 1.13.52.5
      1  1.13.52.5   thorpej /* $NetBSD: s3c2410_extint.c,v 1.13.52.5 2021/04/05 00:48:46 thorpej Exp $ */
      2        1.1       bsh 
      3        1.1       bsh /*
      4        1.1       bsh  * Copyright (c) 2003  Genetec corporation.  All rights reserved.
      5        1.1       bsh  * Written by Hiroyuki Bessho for Genetec corporation.
      6        1.1       bsh  *
      7        1.1       bsh  * Redistribution and use in source and binary forms, with or without
      8        1.1       bsh  * modification, are permitted provided that the following conditions
      9        1.1       bsh  * are met:
     10        1.1       bsh  * 1. Redistributions of source code must retain the above copyright
     11        1.1       bsh  *    notice, this list of conditions and the following disclaimer.
     12        1.1       bsh  * 2. Redistributions in binary form must reproduce the above copyright
     13        1.1       bsh  *    notice, this list of conditions and the following disclaimer in the
     14        1.1       bsh  *    documentation and/or other materials provided with the distribution.
     15        1.1       bsh  * 3. The name of Genetec corporation may not be used to endorse
     16        1.1       bsh  *    or promote products derived from this software without specific prior
     17        1.1       bsh  *    written permission.
     18        1.1       bsh  *
     19        1.1       bsh  * THIS SOFTWARE IS PROVIDED BY GENETEC CORP. ``AS IS'' AND
     20        1.1       bsh  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21        1.1       bsh  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22        1.1       bsh  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL GENETEC CORP.
     23        1.1       bsh  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24        1.1       bsh  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25        1.1       bsh  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26        1.1       bsh  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27        1.1       bsh  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28        1.1       bsh  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29        1.1       bsh  * POSSIBILITY OF SUCH DAMAGE.
     30        1.1       bsh  */
     31        1.1       bsh 
     32        1.1       bsh /*
     33        1.1       bsh  * device driver to handle cascaded external interrupts of S3C2410.
     34        1.1       bsh  *
     35        1.1       bsh  * EXTINT [8..23] are cascaded to IRQ #5
     36        1.1       bsh  * EXTINT [4..7] are cascaded to IRQ #4
     37        1.1       bsh  * EXTINT [0..3] are not cascaded and connected directly as IRQ #[0..3].
     38        1.1       bsh  * EXTINT [0..3] are handled by main interrupt handler in s3c2410_intr.c.
     39        1.1       bsh  */
     40        1.1       bsh 
     41        1.1       bsh #include <sys/cdefs.h>
     42  1.13.52.5   thorpej __KERNEL_RCSID(0, "$NetBSD: s3c2410_extint.c,v 1.13.52.5 2021/04/05 00:48:46 thorpej Exp $");
     43        1.1       bsh 
     44        1.1       bsh #include <sys/param.h>
     45        1.1       bsh #include <sys/systm.h>
     46        1.1       bsh #include <sys/malloc.h>
     47        1.1       bsh #include <uvm/uvm_extern.h>
     48       1.11    dyoung #include <sys/bus.h>
     49        1.1       bsh #include <machine/intr.h>
     50        1.1       bsh #include <arm/cpufunc.h>
     51        1.1       bsh 
     52        1.1       bsh #include <arm/s3c2xx0/s3c2410reg.h>
     53        1.1       bsh #include <arm/s3c2xx0/s3c2410var.h>
     54        1.1       bsh 
     55        1.1       bsh #include "locators.h"
     56        1.1       bsh #include "opt_s3c2410.h"	/* for S3C2410_EXTINT_MAX */
     57        1.1       bsh 
     58        1.1       bsh #ifndef	S3C2410_EXTINT_MAX
     59        1.1       bsh #define S3C2410_EXTINT_MAX  23
     60        1.1       bsh #endif
     61        1.1       bsh 
     62        1.1       bsh #define	EXTINT_CASCADE_MIN	4
     63        1.1       bsh 
     64        1.1       bsh #if S3C2410_EXTINT_MAX < EXTINT_CASCADE_MIN
     65        1.1       bsh #error "Don't enable ssextio if you don't use extint[4..23]."
     66        1.1       bsh #endif
     67        1.1       bsh 
     68        1.1       bsh #define	N_EXTINT	(S3C2410_EXTINT_MAX - EXTINT_CASCADE_MIN +1)
     69        1.1       bsh 
     70        1.1       bsh struct ssextio_softc {
     71        1.1       bsh 	bus_space_tag_t sc_iot;
     72        1.1       bsh 	bus_space_handle_t sc_ioh;
     73        1.1       bsh 
     74        1.1       bsh 	uint32_t   sc_pending;
     75        1.1       bsh 	uint32_t   sc_mask;
     76        1.1       bsh 
     77        1.1       bsh 	struct extint_handler {
     78        1.1       bsh 		int (* func)(void *);
     79        1.1       bsh 		void *arg;
     80        1.1       bsh 		void *sh;		/* softintr handler */
     81        1.1       bsh 		int level;		/* IPL */
     82        1.1       bsh 	} sc_handler[N_EXTINT];
     83        1.1       bsh 
     84        1.1       bsh };
     85        1.1       bsh 
     86        1.1       bsh static struct	ssextio_softc *ssextio_softc = NULL;
     87        1.1       bsh 
     88        1.1       bsh /* cookies */
     89        1.1       bsh #define	EXTINT_4_7	1
     90        1.1       bsh #define	EXTINT_8_23	2
     91        1.1       bsh 
     92        1.1       bsh /* prototypes */
     93       1.13       chs static int	ssextio_match(device_t, cfdata_t, void *);
     94       1.13       chs static void	ssextio_attach(device_t, device_t, void *);
     95       1.13       chs static int 	ssextio_search(device_t, cfdata_t, const int *, void *);
     96        1.1       bsh static int	ssextio_print(void *, const char *);
     97        1.1       bsh 
     98        1.1       bsh static int	ssextio_cascaded_intr(void *);
     99        1.1       bsh static void	ssextio_softintr(void *);
    100        1.1       bsh 
    101        1.6     perry static inline void
    102        1.1       bsh update_hw_mask(void)
    103        1.1       bsh {
    104        1.1       bsh 	bus_space_write_4(ssextio_softc->sc_iot, ssextio_softc->sc_ioh,
    105        1.1       bsh 	    GPIO_EINTMASK, ssextio_softc->sc_mask | ssextio_softc->sc_pending);
    106        1.1       bsh }
    107        1.1       bsh 
    108        1.1       bsh 
    109        1.1       bsh /* attach structures */
    110       1.13       chs CFATTACH_DECL_NEW(ssextio, sizeof(struct ssextio_softc), ssextio_match, ssextio_attach,
    111        1.1       bsh     NULL, NULL);
    112        1.1       bsh 
    113        1.1       bsh static int
    114        1.1       bsh ssextio_print(void *aux, const char *name)
    115        1.1       bsh {
    116       1.13       chs 	struct s3c2xx0_attach_args *sa = aux;
    117        1.1       bsh 
    118        1.1       bsh 	if (sa->sa_addr != SSEXTIOCF_ADDR_DEFAULT)
    119        1.1       bsh                 aprint_normal(" addr 0x%lx", sa->sa_addr);
    120        1.1       bsh         if (sa->sa_intr > 0)
    121        1.1       bsh                 aprint_normal(" intr %d", sa->sa_intr);
    122        1.1       bsh         return (UNCONF);
    123        1.1       bsh }
    124        1.1       bsh 
    125        1.1       bsh int
    126       1.13       chs ssextio_match(device_t parent, cfdata_t match, void *aux)
    127        1.1       bsh {
    128        1.1       bsh #if S3C2410_EXTINT_MAX < 4
    129        1.1       bsh 	/* better not configure this driver */
    130        1.1       bsh 	return 0;
    131        1.1       bsh #else
    132        1.1       bsh 	if (ssextio_softc != NULL)
    133        1.1       bsh 		return 0;
    134        1.1       bsh 
    135        1.1       bsh 	return 1;
    136        1.1       bsh #endif
    137        1.1       bsh }
    138        1.1       bsh 
    139        1.1       bsh void
    140       1.13       chs ssextio_attach(device_t parent, device_t self, void *aux)
    141        1.1       bsh {
    142       1.13       chs 	struct ssextio_softc *sc = device_private(self);
    143       1.12  nisimura 	struct s3c24x0_softc *cpuc = ((struct s3c2xx0_attach_args *)aux)->sa_sc;
    144        1.1       bsh 
    145        1.1       bsh 	aprint_normal("\n");
    146        1.1       bsh 
    147        1.1       bsh 	ssextio_softc = sc;
    148        1.1       bsh 
    149        1.1       bsh 	sc->sc_iot = cpuc->sc_sx.sc_iot;
    150        1.1       bsh 	sc->sc_ioh = cpuc->sc_sx.sc_gpio_ioh;
    151        1.1       bsh 
    152        1.1       bsh 	sc->sc_pending = 0;
    153        1.1       bsh 	sc->sc_mask = ~0;
    154        1.1       bsh 
    155        1.1       bsh 	s3c24x0_intr_establish(S3C2410_INT_4_7, IPL_HIGH, IST_NONE,
    156        1.1       bsh 	    ssextio_cascaded_intr, (void *)EXTINT_4_7);
    157        1.1       bsh #if S3C2410_EXTINT_MAX >= 8
    158        1.1       bsh 	s3c24x0_intr_establish(S3C2410_INT_8_23, IPL_HIGH, IST_NONE,
    159        1.1       bsh 	    ssextio_cascaded_intr, (void *)EXTINT_8_23);
    160        1.1       bsh #endif
    161        1.1       bsh 	/*
    162        1.1       bsh 	 *  Attach each devices
    163        1.1       bsh 	 */
    164  1.13.52.1   thorpej 	config_search(self, NULL,
    165  1.13.52.4   thorpej 	    CFARG_SEARCH, ssextio_search,
    166  1.13.52.1   thorpej 	    CFARG_EOL);
    167        1.1       bsh }
    168        1.1       bsh 
    169        1.1       bsh static int
    170       1.13       chs ssextio_search(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
    171        1.1       bsh {
    172       1.12  nisimura 	struct ssextio_softc *sc = device_private(parent);
    173       1.13       chs 	struct s3c24x0_softc *cpuc = device_private(device_parent(parent));
    174        1.1       bsh 	struct s3c2xx0_attach_args sa;
    175        1.1       bsh 
    176        1.1       bsh 	sa.sa_sc = sc;
    177        1.1       bsh         sa.sa_iot = sc->sc_iot;
    178        1.1       bsh         sa.sa_addr = cf->cf_loc[SSEXTIOCF_ADDR];
    179        1.1       bsh         sa.sa_size = cf->cf_loc[SSEXTIOCF_SIZE];
    180        1.1       bsh         sa.sa_intr = cf->cf_loc[SSEXTIOCF_INTR];
    181        1.1       bsh 	sa.sa_dmat = cpuc->sc_sx.sc_dmat;
    182        1.1       bsh 
    183  1.13.52.5   thorpej         if (config_probe(parent, cf, &sa))
    184  1.13.52.3   thorpej                 config_attach(parent, cf, &sa, ssextio_print, CFARG_EOL);
    185        1.1       bsh 
    186        1.1       bsh         return 0;
    187        1.1       bsh }
    188        1.1       bsh 
    189        1.1       bsh void *
    190        1.1       bsh s3c2410_extint_establish(int extint, int ipl, int type,
    191        1.1       bsh     int (*func)(void *), void *arg)
    192        1.1       bsh {
    193        1.1       bsh 	int save;
    194        1.1       bsh 	int idx;
    195        1.1       bsh 	int soft_level;
    196        1.1       bsh 
    197        1.1       bsh 	if (extint < 0 || N_EXTINT <= extint)
    198        1.1       bsh 		panic("Bad interrupt no for extio");
    199        1.1       bsh 
    200        1.1       bsh 	if (extint < EXTINT_CASCADE_MIN) {
    201        1.1       bsh 		/*
    202        1.1       bsh 		 * EXINT[0..3] are not cascaded. they are handled by
    203        1.1       bsh 		 * the main interrupt controller.
    204        1.1       bsh 		 */
    205        1.1       bsh 		return s3c24x0_intr_establish(extint, ipl, type, func, arg);
    206        1.1       bsh 	}
    207        1.1       bsh 
    208        1.9      matt 	soft_level = SOFTINT_SERIAL;
    209        1.1       bsh 
    210        1.1       bsh 	idx = extint - EXTINT_CASCADE_MIN;
    211        1.1       bsh 
    212        1.1       bsh 	save = disable_interrupts(I32_bit);
    213        1.1       bsh 
    214        1.1       bsh 	ssextio_softc->sc_handler[idx].func = func;
    215        1.1       bsh 	ssextio_softc->sc_handler[idx].arg = arg;
    216        1.1       bsh 	ssextio_softc->sc_handler[idx].level = ipl;
    217        1.1       bsh 
    218        1.9      matt 	ssextio_softc->sc_handler[idx].sh = softint_establish(soft_level,
    219        1.1       bsh 	    ssextio_softintr, &ssextio_softc->sc_handler[idx]);
    220        1.1       bsh 
    221        1.1       bsh 	s3c2410_setup_extint(extint, type);
    222        1.1       bsh 
    223        1.1       bsh 	bus_space_write_4(ssextio_softc->sc_iot, ssextio_softc->sc_ioh,
    224        1.1       bsh 			  GPIO_EINTPEND, 1U << extint);
    225        1.1       bsh 	ssextio_softc->sc_mask &= ~(1U << extint);
    226        1.1       bsh 	update_hw_mask();
    227        1.1       bsh 
    228        1.1       bsh 	restore_interrupts(save);
    229        1.1       bsh 	return &ssextio_softc->sc_handler[idx];
    230        1.1       bsh }
    231        1.1       bsh 
    232        1.1       bsh 
    233        1.1       bsh static int
    234        1.1       bsh ssextio_cascaded_intr(void *cookie)
    235        1.1       bsh {
    236        1.1       bsh 	uint32_t pending_mask, pending;
    237        1.1       bsh 	int int_min;
    238        1.1       bsh 	bus_space_tag_t iot = ssextio_softc->sc_iot;
    239        1.1       bsh 	bus_space_handle_t ioh = ssextio_softc->sc_ioh;
    240        1.1       bsh 	int save, i;
    241        1.1       bsh 
    242        1.1       bsh 	switch((int)cookie) {
    243        1.1       bsh 	case EXTINT_4_7:
    244        1.1       bsh 		pending_mask = 0x000000f0;
    245        1.1       bsh 		int_min = 4;
    246        1.1       bsh 		break;
    247        1.1       bsh 
    248        1.1       bsh 	case EXTINT_8_23:
    249        1.1       bsh 		pending_mask = 0x00ffff00;
    250        1.1       bsh 		int_min = 8;
    251        1.1       bsh 		break;
    252        1.1       bsh 
    253        1.1       bsh 	default:
    254        1.8     perry 		panic("Bad cookie for %s", __func__);
    255        1.1       bsh 	}
    256        1.1       bsh 
    257        1.1       bsh 
    258       1.10   mbalmer 	save = disable_interrupts(I32_bit);
    259        1.1       bsh 	pending = pending_mask & bus_space_read_4(iot, ioh, GPIO_EINTPEND);
    260        1.1       bsh 	pending &= ~ssextio_softc->sc_mask;
    261        1.1       bsh 	ssextio_softc->sc_pending |= pending;
    262        1.1       bsh 	/* disable the extint until the handler is called. */
    263        1.1       bsh 	update_hw_mask();
    264        1.1       bsh 	restore_interrupts(save);
    265        1.1       bsh 
    266        1.1       bsh 	for (i=int_min; pending; ++i) {
    267        1.1       bsh 		if (pending & (1<<i)) {
    268        1.1       bsh 			assert(ssextio_softc->sc_handler[i-EXTINT_CASCADE_MIN].sh != NULL);
    269        1.1       bsh 
    270        1.9      matt 			softint_schedule(
    271        1.1       bsh 			    ssextio_softc->sc_handler[i-EXTINT_CASCADE_MIN].sh);
    272        1.1       bsh 			pending &= ~ (1<<i);
    273        1.1       bsh 		}
    274        1.1       bsh 	}
    275        1.1       bsh 
    276        1.1       bsh 	return 1;
    277        1.1       bsh }
    278        1.1       bsh 
    279        1.1       bsh static void
    280        1.1       bsh ssextio_softintr(void *cookie)
    281        1.1       bsh {
    282        1.1       bsh 	struct extint_handler *h = cookie;
    283        1.1       bsh 	int extint = EXTINT_CASCADE_MIN + h - ssextio_softc->sc_handler;
    284        1.1       bsh 	int s, save;
    285        1.1       bsh 
    286        1.1       bsh 	save = disable_interrupts(I32_bit);
    287        1.1       bsh 	/* clear hardware pending bits */
    288        1.1       bsh 	bus_space_write_4(ssextio_softc->sc_iot, ssextio_softc->sc_ioh,
    289        1.1       bsh 	    GPIO_EINTPEND, 1<<extint);
    290        1.1       bsh 	ssextio_softc->sc_pending &= ~(1<<extint);
    291        1.1       bsh 	update_hw_mask();
    292        1.1       bsh 	restore_interrupts(save);
    293        1.1       bsh 
    294        1.1       bsh 	s = _splraise(h->level);
    295        1.1       bsh 	h->func(h->arg);
    296        1.1       bsh 	splx(s);
    297        1.1       bsh }
    298