Home | History | Annotate | Line # | Download | only in imx
imx31_icu.c revision 1.1.2.1
      1  1.1.2.1  matt /*-
      2  1.1.2.1  matt  * Copyright (c) 2007 The NetBSD Foundation, Inc.
      3  1.1.2.1  matt  * All rights reserved.
      4  1.1.2.1  matt  *
      5  1.1.2.1  matt  * This code is derived from software contributed to The NetBSD Foundation
      6  1.1.2.1  matt  * by Matt Thomas.
      7  1.1.2.1  matt  *
      8  1.1.2.1  matt  * Redistribution and use in source and binary forms, with or without
      9  1.1.2.1  matt  * modification, are permitted provided that the following conditions
     10  1.1.2.1  matt  * are met:
     11  1.1.2.1  matt  * 1. Redistributions of source code must retain the above copyright
     12  1.1.2.1  matt  *    notice, this list of conditions and the following disclaimer.
     13  1.1.2.1  matt  * 2. Redistributions in binary form must reproduce the above copyright
     14  1.1.2.1  matt  *    notice, this list of conditions and the following disclaimer in the
     15  1.1.2.1  matt  *    documentation and/or other materials provided with the distribution.
     16  1.1.2.1  matt  * 3. All advertising materials mentioning features or use of this software
     17  1.1.2.1  matt  *    must display the following acknowledgement:
     18  1.1.2.1  matt  *        This product includes software developed by the NetBSD
     19  1.1.2.1  matt  *        Foundation, Inc. and its contributors.
     20  1.1.2.1  matt  * 4. Neither the name of The NetBSD Foundation nor the names of its
     21  1.1.2.1  matt  *    contributors may be used to endorse or promote products derived
     22  1.1.2.1  matt  *    from this software without specific prior written permission.
     23  1.1.2.1  matt  *
     24  1.1.2.1  matt  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     25  1.1.2.1  matt  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     26  1.1.2.1  matt  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     27  1.1.2.1  matt  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     28  1.1.2.1  matt  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     29  1.1.2.1  matt  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     30  1.1.2.1  matt  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     31  1.1.2.1  matt  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     32  1.1.2.1  matt  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     33  1.1.2.1  matt  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     34  1.1.2.1  matt  * POSSIBILITY OF SUCH DAMAGE.
     35  1.1.2.1  matt  */
     36  1.1.2.1  matt #include <sys/cdefs.h>
     37  1.1.2.1  matt __KERNEL_RCSID(0, "$NetBSD: imx31_icu.c,v 1.1.2.1 2007/08/29 05:24:23 matt Exp $");
     38  1.1.2.1  matt 
     39  1.1.2.1  matt #define _INTR_PRIVATE
     40  1.1.2.1  matt 
     41  1.1.2.1  matt #include <sys/param.h>
     42  1.1.2.1  matt #include <sys/evcnt.h>
     43  1.1.2.1  matt 
     44  1.1.2.1  matt #include <uvm/uvm_extern.h>
     45  1.1.2.1  matt 
     46  1.1.2.1  matt #include <machine/intr.h>
     47  1.1.2.1  matt 
     48  1.1.2.1  matt #include <arm/cpu.h>
     49  1.1.2.1  matt #include <arm/armreg.h>
     50  1.1.2.1  matt #include <arm/cpufunc.h>
     51  1.1.2.1  matt 
     52  1.1.2.1  matt #include <machine/atomic.h>
     53  1.1.2.1  matt #include <machine/bus.h>
     54  1.1.2.1  matt 
     55  1.1.2.1  matt #include <arm/imx/imx31_intrreg.h>
     56  1.1.2.1  matt 
     57  1.1.2.1  matt static void avic_enable_irq(struct pic_softc *, irq);
     58  1.1.2.1  matt static void avic_disable_irq(struct pic_softc *, irq);
     59  1.1.2.1  matt static int avic_get_irq(struct pic_softc *);
     60  1.1.2.1  matt static void avic_establish_irq(struct pic_softc *, int, int, int);
     61  1.1.2.1  matt static void avic_source_name(struct pic_softc *, int);
     62  1.1.2.1  matt 
     63  1.1.2.1  matt const struct pic_ops avic_pic_ops = {
     64  1.1.2.1  matt 	.pic_enable_irq = avic_enable_irq,
     65  1.1.2.1  matt 	.pic_reeable_irq = avic_enable_irq,
     66  1.1.2.1  matt 	.pic_disable_irq = avic_disable_irq,
     67  1.1.2.1  matt 	.pic_establish_irq = avic_establish_irq,
     68  1.1.2.1  matt 	.pic_source_name = avic_source_name
     69  1.1.2.1  matt };
     70  1.1.2.1  matt 
     71  1.1.2.1  matt struct avic_softc {
     72  1.1.2.1  matt 	struct pic_softc avic_pic;
     73  1.1.2.1  matt 	bus_space_tag_t avic_memt;
     74  1.1.2.1  matt 	bus_space_handle_t avic_memh;
     75  1.1.2.1  matt } avic_pic = {
     76  1.1.2.1  matt 	.avic_pic = {
     77  1.1.2.1  matt 		.pic_ops = &avic_pic_ops,
     78  1.1.2.1  matt 		.pic_numintrs = 64,
     79  1.1.2.1  matt 		.pic_name = "avic",
     80  1.1.2.1  matt 	},
     81  1.1.2.1  matt };
     82  1.1.2.1  matt 
     83  1.1.2.1  matt void
     84  1.1.2.1  matt avic_enable_irq(struct pic_softc *pic, int irq)
     85  1.1.2.1  matt {
     86  1.1.2.1  matt 	struct avic_softc * const avic = (void *) pic;
     87  1.1.2.1  matt 	INTC_WRITE(&avic_softc, INTR_ENNUM, irq);
     88  1.1.2.1  matt }
     89  1.1.2.1  matt 
     90  1.1.2.1  matt void
     91  1.1.2.1  matt avic_disable_irq(struct pic_softc *pic, int irq)
     92  1.1.2.1  matt {
     93  1.1.2.1  matt 	struct avic_softc * const avic = (void *) pic;
     94  1.1.2.1  matt 	INTC_WRITE(&avic_softc, INTR_DISNUM, irq);
     95  1.1.2.1  matt }
     96  1.1.2.1  matt 
     97  1.1.2.1  matt void
     98  1.1.2.1  matt avic_establish(struct pic_softc *pic, int irq, int ipl, int type)
     99  1.1.2.1  matt {
    100  1.1.2.1  matt 	struct avic_softc * const avic = (void *) pic;
    101  1.1.2.1  matt 	bus_addr_t priority_reg;
    102  1.1.2.1  matt 	int priority_shift;
    103  1.1.2.1  matt 	uint32_t v;
    104  1.1.2.1  matt 
    105  1.1.2.1  matt 	KASSERT(irq < 64);
    106  1.1.2.1  matt 	KASSERT(ipl < 16);
    107  1.1.2.1  matt 
    108  1.1.2.1  matt 	priority_reg = INTR_NIPRIORITY0 - (irq >> 3);
    109  1.1.2.1  matt 	priority_shift = (irq & 7) * 4;
    110  1.1.2.1  matt 	v = INTC_READ(avic, priority_reg);
    111  1.1.2.1  matt 	v &= ~(0x0f << priority_shift);
    112  1.1.2.1  matt 	v |= SW_TO_HW_IPL(ipl) << priority_shift;
    113  1.1.2.1  matt 	INTC_WRITE(avic, priority_reg, v);
    114  1.1.2.1  matt 
    115  1.1.2.1  matt 	KASSERT(type == IST_LEVEL);
    116  1.1.2.1  matt }
    117  1.1.2.1  matt 
    118  1.1.2.1  matt static const char * const avic_intr_source_names[] = AVIC_INTR_SOURCE_NAMES;
    119  1.1.2.1  matt 
    120  1.1.2.1  matt void
    121  1.1.2.1  matt avic_source_name(struct pic_softc *pic, int irq, char *buf, size_t len)
    122  1.1.2.1  matt {
    123  1.1.2.1  matt 	strlcpy(buf, avic_intr_source_names[irq], len);
    124  1.1.2.1  matt }
    125  1.1.2.1  matt 
    126  1.1.2.1  matt void
    127  1.1.2.1  matt imx31_irq_handler(void *frame)
    128  1.1.2.1  matt {
    129  1.1.2.1  matt 	struct avic_softc * const avic = &avic_softc;
    130  1.1.2.1  matt 	struct pic_softc * const pic = &avic->avic_pic;
    131  1.1.2.1  matt 	int32_t saved_nimask;
    132  1.1.2.1  matt 	int32_t vec;
    133  1.1.2.1  matt 	int ipl;
    134  1.1.2.1  matt 
    135  1.1.2.1  matt 	saved_nimask = INTC_READ(avic, INTR_NIMASK);
    136  1.1.2.1  matt 	for (;;) {
    137  1.1.2.1  matt 		irq = INTC_READ(avic, INTR_NIVECSR);
    138  1.1.2.1  matt 		if (irq < 0)
    139  1.1.2.1  matt 			break;
    140  1.1.2.1  matt 		ipl = (int16_t) irq;
    141  1.1.2.1  matt 		KASSERT(ipl >= 0);
    142  1.1.2.1  matt 		irq >>= 16;
    143  1.1.2.1  matt 		KASSERT(irq < 64);
    144  1.1.2.1  matt 		KASSERT(pic->pic_sources[irq] != NULL);
    145  1.1.2.1  matt 
    146  1.1.2.1  matt 		/*
    147  1.1.2.1  matt 		 * If this interrupt is not above the current spl,
    148  1.1.2.1  matt 		 * mark it as pending and try again.
    149  1.1.2.1  matt 		 */
    150  1.1.2.1  matt 		newipl = HW_TO_SW_IPL(ipl);
    151  1.1.2.1  matt 		if (newipl <= current_spl_level) {
    152  1.1.2.1  matt 			pic_mark_pending(pic, irq);
    153  1.1.2.1  matt 			continue;
    154  1.1.2.1  matt 		}
    155  1.1.2.1  matt 
    156  1.1.2.1  matt 		/*
    157  1.1.2.1  matt 		 * Before enabling interrupts, mask out lower priority
    158  1.1.2.1  matt 		 * interrupts and raise SPL to its equivalent.
    159  1.1.2.1  matt 		 */
    160  1.1.2.1  matt 
    161  1.1.2.1  matt 		INTC_WRITE(avic, INTR_NIMASK, ipl);
    162  1.1.2.1  matt 		oldipl = _splraise(newipl);
    163  1.1.2.1  matt 		cpsie(I32_bit);
    164  1.1.2.1  matt 
    165  1.1.2.1  matt 		pic_dispatch(pic->pic_sources[irq], frame);
    166  1.1.2.1  matt 
    167  1.1.2.1  matt 		/*
    168  1.1.2.1  matt 		 * Disable interrupts again.  Drop SPL.  Restore saved
    169  1.1.2.1  matt 		 * HW interrupt level.
    170  1.1.2.1  matt 		 */
    171  1.1.2.1  matt 		cpsid(I32_bit);
    172  1.1.2.1  matt 		splx(oldipl);
    173  1.1.2.1  matt 		INTC_WRITE(avic, INTR_NIMASK, saved_nimask);
    174  1.1.2.1  matt 	}
    175  1.1.2.1  matt }
    176  1.1.2.1  matt 
    177  1.1.2.1  matt avic_init(void)
    178  1.1.2.1  matt {
    179  1.1.2.1  matt 	pic_add(&avic_softc.pic);
    180  1.1.2.1  matt }
    181