Home | History | Annotate | Line # | Download | only in s3c2xx0
s3c2410_extint.c revision 1.9
      1  1.9      matt /* $NetBSD: s3c2410_extint.c,v 1.9 2008/01/06 01:37:55 matt 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.9      matt __KERNEL_RCSID(0, "$NetBSD: s3c2410_extint.c,v 1.9 2008/01/06 01:37:55 matt 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.1       bsh #include <machine/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 	struct device	sc_dev;
     72  1.1       bsh 
     73  1.1       bsh 	bus_space_tag_t sc_iot;
     74  1.1       bsh 	bus_space_handle_t sc_ioh;
     75  1.1       bsh 
     76  1.1       bsh 	uint32_t   sc_pending;
     77  1.1       bsh 	uint32_t   sc_mask;
     78  1.1       bsh 
     79  1.1       bsh 	struct extint_handler {
     80  1.1       bsh 		int (* func)(void *);
     81  1.1       bsh 		void *arg;
     82  1.1       bsh 		void *sh;		/* softintr handler */
     83  1.1       bsh 		int level;		/* IPL */
     84  1.1       bsh 	} sc_handler[N_EXTINT];
     85  1.1       bsh 
     86  1.1       bsh };
     87  1.1       bsh 
     88  1.1       bsh static struct	ssextio_softc *ssextio_softc = NULL;
     89  1.1       bsh 
     90  1.1       bsh /* cookies */
     91  1.1       bsh #define	EXTINT_4_7	1
     92  1.1       bsh #define	EXTINT_8_23	2
     93  1.1       bsh 
     94  1.1       bsh /* prototypes */
     95  1.1       bsh static int	ssextio_match(struct device *, struct cfdata *, void *);
     96  1.1       bsh static void	ssextio_attach(struct device *, struct device *, void *);
     97  1.3  drochner static int 	ssextio_search(struct device *, struct cfdata *,
     98  1.4  drochner 			       const int *, void *);
     99  1.1       bsh static int	ssextio_print(void *, const char *);
    100  1.1       bsh 
    101  1.1       bsh static int	ssextio_cascaded_intr(void *);
    102  1.1       bsh static void	ssextio_softintr(void *);
    103  1.1       bsh 
    104  1.6     perry static inline void
    105  1.1       bsh update_hw_mask(void)
    106  1.1       bsh {
    107  1.1       bsh 	bus_space_write_4(ssextio_softc->sc_iot, ssextio_softc->sc_ioh,
    108  1.1       bsh 	    GPIO_EINTMASK, ssextio_softc->sc_mask | ssextio_softc->sc_pending);
    109  1.1       bsh }
    110  1.1       bsh 
    111  1.1       bsh 
    112  1.1       bsh /* attach structures */
    113  1.1       bsh CFATTACH_DECL(ssextio, sizeof(struct ssextio_softc), ssextio_match, ssextio_attach,
    114  1.1       bsh     NULL, NULL);
    115  1.1       bsh 
    116  1.1       bsh static int
    117  1.1       bsh ssextio_print(void *aux, const char *name)
    118  1.1       bsh {
    119  1.1       bsh 	struct s3c2xx0_attach_args *sa = (struct s3c2xx0_attach_args*)aux;
    120  1.1       bsh 
    121  1.1       bsh 	if (sa->sa_addr != SSEXTIOCF_ADDR_DEFAULT)
    122  1.1       bsh                 aprint_normal(" addr 0x%lx", sa->sa_addr);
    123  1.1       bsh         if (sa->sa_intr > 0)
    124  1.1       bsh                 aprint_normal(" intr %d", sa->sa_intr);
    125  1.1       bsh         return (UNCONF);
    126  1.1       bsh }
    127  1.1       bsh 
    128  1.1       bsh int
    129  1.1       bsh ssextio_match(struct device *parent, struct cfdata *match, void *aux)
    130  1.1       bsh {
    131  1.1       bsh #if S3C2410_EXTINT_MAX < 4
    132  1.1       bsh 	/* better not configure this driver */
    133  1.1       bsh 	return 0;
    134  1.1       bsh #else
    135  1.1       bsh 	if (ssextio_softc != NULL)
    136  1.1       bsh 		return 0;
    137  1.1       bsh 
    138  1.1       bsh 	return 1;
    139  1.1       bsh #endif
    140  1.1       bsh }
    141  1.1       bsh 
    142  1.1       bsh void
    143  1.1       bsh ssextio_attach(struct device *parent, struct device *self, void *aux)
    144  1.1       bsh {
    145  1.1       bsh 	struct ssextio_softc *sc = (struct ssextio_softc*)self;
    146  1.1       bsh 	struct s3c24x0_softc *cpuc = (struct s3c24x0_softc *)parent;
    147  1.1       bsh 
    148  1.1       bsh 	aprint_normal("\n");
    149  1.1       bsh 
    150  1.1       bsh 	ssextio_softc = sc;
    151  1.1       bsh 
    152  1.1       bsh 	sc->sc_iot = cpuc->sc_sx.sc_iot;
    153  1.1       bsh 	sc->sc_ioh = cpuc->sc_sx.sc_gpio_ioh;
    154  1.1       bsh 
    155  1.1       bsh 	sc->sc_pending = 0;
    156  1.1       bsh 	sc->sc_mask = ~0;
    157  1.1       bsh 
    158  1.1       bsh 	s3c24x0_intr_establish(S3C2410_INT_4_7, IPL_HIGH, IST_NONE,
    159  1.1       bsh 	    ssextio_cascaded_intr, (void *)EXTINT_4_7);
    160  1.1       bsh #if S3C2410_EXTINT_MAX >= 8
    161  1.1       bsh 	s3c24x0_intr_establish(S3C2410_INT_8_23, IPL_HIGH, IST_NONE,
    162  1.1       bsh 	    ssextio_cascaded_intr, (void *)EXTINT_8_23);
    163  1.1       bsh #endif
    164  1.1       bsh 	/*
    165  1.1       bsh 	 *  Attach each devices
    166  1.1       bsh 	 */
    167  1.3  drochner 	config_search_ia(ssextio_search, self, "ssextio", NULL);
    168  1.1       bsh }
    169  1.1       bsh 
    170  1.1       bsh static int
    171  1.3  drochner ssextio_search(struct device *parent, struct cfdata *cf,
    172  1.4  drochner 	       const int *ldesc, void *aux)
    173  1.1       bsh {
    174  1.1       bsh 	struct ssextio_softc *sc = (struct ssextio_softc *)parent;
    175  1.7   thorpej 	struct s3c24x0_softc *cpuc =(struct s3c24x0_softc *) device_parent(&sc->sc_dev);
    176  1.1       bsh 	struct s3c2xx0_attach_args sa;
    177  1.1       bsh 
    178  1.1       bsh 	sa.sa_sc = sc;
    179  1.1       bsh         sa.sa_iot = sc->sc_iot;
    180  1.1       bsh         sa.sa_addr = cf->cf_loc[SSEXTIOCF_ADDR];
    181  1.1       bsh         sa.sa_size = cf->cf_loc[SSEXTIOCF_SIZE];
    182  1.1       bsh         sa.sa_intr = cf->cf_loc[SSEXTIOCF_INTR];
    183  1.1       bsh 	sa.sa_dmat = cpuc->sc_sx.sc_dmat;
    184  1.1       bsh 
    185  1.1       bsh         if (config_match(parent, cf, &sa))
    186  1.1       bsh                 config_attach(parent, cf, &sa, ssextio_print);
    187  1.1       bsh 
    188  1.1       bsh         return 0;
    189  1.1       bsh }
    190  1.1       bsh 
    191  1.1       bsh void *
    192  1.1       bsh s3c2410_extint_establish(int extint, int ipl, int type,
    193  1.1       bsh     int (*func)(void *), void *arg)
    194  1.1       bsh {
    195  1.1       bsh 	int save;
    196  1.1       bsh 	int idx;
    197  1.1       bsh 	int soft_level;
    198  1.1       bsh 
    199  1.1       bsh 	if (extint < 0 || N_EXTINT <= extint)
    200  1.1       bsh 		panic("Bad interrupt no for extio");
    201  1.1       bsh 
    202  1.1       bsh 	if (extint < EXTINT_CASCADE_MIN) {
    203  1.1       bsh 		/*
    204  1.1       bsh 		 * EXINT[0..3] are not cascaded. they are handled by
    205  1.1       bsh 		 * the main interrupt controller.
    206  1.1       bsh 		 */
    207  1.1       bsh 		return s3c24x0_intr_establish(extint, ipl, type, func, arg);
    208  1.1       bsh 	}
    209  1.1       bsh 
    210  1.9      matt 	soft_level = SOFTINT_SERIAL;
    211  1.1       bsh 
    212  1.1       bsh 	idx = extint - EXTINT_CASCADE_MIN;
    213  1.1       bsh 
    214  1.1       bsh 	save = disable_interrupts(I32_bit);
    215  1.1       bsh 
    216  1.1       bsh 	ssextio_softc->sc_handler[idx].func = func;
    217  1.1       bsh 	ssextio_softc->sc_handler[idx].arg = arg;
    218  1.1       bsh 	ssextio_softc->sc_handler[idx].level = ipl;
    219  1.1       bsh 
    220  1.9      matt 	ssextio_softc->sc_handler[idx].sh = softint_establish(soft_level,
    221  1.1       bsh 	    ssextio_softintr, &ssextio_softc->sc_handler[idx]);
    222  1.1       bsh 
    223  1.1       bsh 	s3c2410_setup_extint(extint, type);
    224  1.1       bsh 
    225  1.1       bsh 	bus_space_write_4(ssextio_softc->sc_iot, ssextio_softc->sc_ioh,
    226  1.1       bsh 			  GPIO_EINTPEND, 1U << extint);
    227  1.1       bsh 	ssextio_softc->sc_mask &= ~(1U << extint);
    228  1.1       bsh 	update_hw_mask();
    229  1.1       bsh 
    230  1.1       bsh 	restore_interrupts(save);
    231  1.1       bsh 	return &ssextio_softc->sc_handler[idx];
    232  1.1       bsh }
    233  1.1       bsh 
    234  1.1       bsh 
    235  1.1       bsh static int
    236  1.1       bsh ssextio_cascaded_intr(void *cookie)
    237  1.1       bsh {
    238  1.1       bsh 	uint32_t pending_mask, pending;
    239  1.1       bsh 	int int_min;
    240  1.1       bsh 	bus_space_tag_t iot = ssextio_softc->sc_iot;
    241  1.1       bsh 	bus_space_handle_t ioh = ssextio_softc->sc_ioh;
    242  1.1       bsh 	int save, i;
    243  1.1       bsh 
    244  1.1       bsh 	switch((int)cookie) {
    245  1.1       bsh 	case EXTINT_4_7:
    246  1.1       bsh 		pending_mask = 0x000000f0;
    247  1.1       bsh 		int_min = 4;
    248  1.1       bsh 		break;
    249  1.1       bsh 
    250  1.1       bsh 	case EXTINT_8_23:
    251  1.1       bsh 		pending_mask = 0x00ffff00;
    252  1.1       bsh 		int_min = 8;
    253  1.1       bsh 		break;
    254  1.1       bsh 
    255  1.1       bsh 	default:
    256  1.8     perry 		panic("Bad cookie for %s", __func__);
    257  1.1       bsh 	}
    258  1.1       bsh 
    259  1.1       bsh 
    260  1.1       bsh 	save = disable_interrupts(I32_bit);;
    261  1.1       bsh 	pending = pending_mask & bus_space_read_4(iot, ioh, GPIO_EINTPEND);
    262  1.1       bsh 	pending &= ~ssextio_softc->sc_mask;
    263  1.1       bsh 	ssextio_softc->sc_pending |= pending;
    264  1.1       bsh 	/* disable the extint until the handler is called. */
    265  1.1       bsh 	update_hw_mask();
    266  1.1       bsh 	restore_interrupts(save);
    267  1.1       bsh 
    268  1.1       bsh 	for (i=int_min; pending; ++i) {
    269  1.1       bsh 		if (pending & (1<<i)) {
    270  1.1       bsh 			assert(ssextio_softc->sc_handler[i-EXTINT_CASCADE_MIN].sh != NULL);
    271  1.1       bsh 
    272  1.9      matt 			softint_schedule(
    273  1.1       bsh 			    ssextio_softc->sc_handler[i-EXTINT_CASCADE_MIN].sh);
    274  1.1       bsh 			pending &= ~ (1<<i);
    275  1.1       bsh 		}
    276  1.1       bsh 	}
    277  1.1       bsh 
    278  1.1       bsh 	return 1;
    279  1.1       bsh }
    280  1.1       bsh 
    281  1.1       bsh static void
    282  1.1       bsh ssextio_softintr(void *cookie)
    283  1.1       bsh {
    284  1.1       bsh 	struct extint_handler *h = cookie;
    285  1.1       bsh 	int extint = EXTINT_CASCADE_MIN + h - ssextio_softc->sc_handler;
    286  1.1       bsh 	int s, save;
    287  1.1       bsh 
    288  1.1       bsh 	save = disable_interrupts(I32_bit);
    289  1.1       bsh 	/* clear hardware pending bits */
    290  1.1       bsh 	bus_space_write_4(ssextio_softc->sc_iot, ssextio_softc->sc_ioh,
    291  1.1       bsh 	    GPIO_EINTPEND, 1<<extint);
    292  1.1       bsh 	ssextio_softc->sc_pending &= ~(1<<extint);
    293  1.1       bsh 	update_hw_mask();
    294  1.1       bsh 	restore_interrupts(save);
    295  1.1       bsh 
    296  1.1       bsh 	s = _splraise(h->level);
    297  1.1       bsh 	h->func(h->arg);
    298  1.1       bsh 	splx(s);
    299  1.1       bsh }
    300