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