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