Home | History | Annotate | Line # | Download | only in imx
imx31_icu.c revision 1.1.2.2
      1 /*-
      2  * Copyright (c) 2007 The NetBSD Foundation, Inc.
      3  * All rights reserved.
      4  *
      5  * This code is derived from software contributed to The NetBSD Foundation
      6  * by Matt Thomas.
      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  * 3. All advertising materials mentioning features or use of this software
     17  *    must display the following acknowledgement:
     18  *        This product includes software developed by the NetBSD
     19  *        Foundation, Inc. and its contributors.
     20  * 4. Neither the name of The NetBSD Foundation nor the names of its
     21  *    contributors may be used to endorse or promote products derived
     22  *    from this software without specific prior written permission.
     23  *
     24  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     25  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     26  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     27  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     28  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     29  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     30  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     31  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     32  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     33  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     34  * POSSIBILITY OF SUCH DAMAGE.
     35  */
     36 #include <sys/cdefs.h>
     37 __KERNEL_RCSID(0, "$NetBSD: imx31_icu.c,v 1.1.2.2 2007/08/30 07:08:15 matt Exp $");
     38 
     39 #define _INTR_PRIVATE
     40 
     41 #include <sys/param.h>
     42 #include <sys/evcnt.h>
     43 
     44 #include <uvm/uvm_extern.h>
     45 
     46 #include <machine/intr.h>
     47 
     48 #include <arm/cpu.h>
     49 #include <arm/armreg.h>
     50 #include <arm/cpufunc.h>
     51 
     52 #include <machine/atomic.h>
     53 #include <machine/bus.h>
     54 
     55 #include <arm/imx/imx31_intrreg.h>
     56 
     57 static void avic_unblock_irqs(struct pic_softc *, size_t, uint32_t);
     58 static void avic_block_irqs(struct pic_softc *, size_t, uint32_t);
     59 static void avic_establish_irq(struct pic_softc *, int, int, int);
     60 static void avic_source_name(struct pic_softc *, int, char *, size_t);
     61 
     62 const struct pic_ops avic_pic_ops = {
     63 	.pic_unblock_irqs = avic_unblock_irqs,
     64 	.pic_block_irqs = avic_block_irqs,
     65 	.pic_establish_irq = avic_establish_irq,
     66 	.pic_source_name = avic_source_name
     67 };
     68 
     69 struct avic_softc {
     70 	struct pic_softc avic_pic;
     71 	bus_space_tag_t avic_memt;
     72 	bus_space_handle_t avic_memh;
     73 } pic_avic = {
     74 	.avic_pic = {
     75 		.pic_ops = &avic_pic_ops,
     76 		.pic_maxsources = 64,
     77 		.pic_name = "avic",
     78 	},
     79 };
     80 
     81 #define	INTC_READ(avic, reg) \
     82 	bus_space_read_4((avic)->avic_memt, (avic)->avic_memh, (reg))
     83 #define	INTC_WRITE(avic, reg, val) \
     84 	bus_space_write_4((avic)->avic_memt, (avic)->avic_memh, (reg), (val))
     85 #define	HW_TO_SW_IPL(ipl)	((ipl) + 1)
     86 #define	SW_TO_HW_IPL(ipl)	((ipl) - 1)
     87 
     88 void
     89 avic_unblock_irqs(struct pic_softc *pic, size_t irq_base, uint32_t irq_mask)
     90 {
     91 	struct avic_softc * const avic = (void *) pic;
     92 #if 0
     93 	if (irq_base == 0)
     94 		INTC_WRITE(avic, IMX31_INTENABLEL, irq_mask);
     95 	else
     96 		INTC_WRITE(avic, IMX31_INTENABLEH, irq_mask);
     97 #else
     98 	uint32_t irq;
     99 	while ((irq = ffs(irq_mask)) != 0) {
    100 		irq--;
    101 		irq_base += irq;
    102 		irq_mask >>= irq;
    103 		INTC_WRITE(avic, IMX31_INTENNUM, irq_base);
    104 	}
    105 #endif
    106 }
    107 
    108 void
    109 avic_block_irqs(struct pic_softc *pic, size_t irq_base, uint32_t irq_mask)
    110 {
    111 	struct avic_softc * const avic = (void *) pic;
    112 #if 0
    113 	if (irq_base == 0)
    114 		INTC_WRITE(avic, IMX31_INTDISABLEL, irq_mask);
    115 	else
    116 		INTC_WRITE(avic, IMX31_INTDISABLEH, irq_mask);
    117 #else
    118 	uint32_t irq;
    119 	while ((irq = ffs(irq_mask)) != 0) {
    120 		irq--;
    121 		irq_base += irq;
    122 		irq_mask >>= irq;
    123 		INTC_WRITE(avic, IMX31_INTDISNUM, irq_base);
    124 	}
    125 #endif
    126 }
    127 
    128 void
    129 avic_establish_irq(struct pic_softc *pic, int irq, int ipl, int type)
    130 {
    131 	struct avic_softc * const avic = (void *) pic;
    132 	bus_addr_t priority_reg;
    133 	int priority_shift;
    134 	uint32_t v;
    135 
    136 	KASSERT(irq < 64);
    137 	KASSERT(ipl < 16);
    138 
    139 	priority_reg = IMX31_NIPRIORITY0 - (irq >> 3);
    140 	priority_shift = (irq & 7) * 4;
    141 	v = INTC_READ(avic, priority_reg);
    142 	v &= ~(0x0f << priority_shift);
    143 	v |= SW_TO_HW_IPL(ipl) << priority_shift;
    144 	INTC_WRITE(avic, priority_reg, v);
    145 
    146 	KASSERT(type == IST_LEVEL);
    147 }
    148 
    149 static const char * const avic_intr_source_names[] = AVIC_INTR_SOURCE_NAMES;
    150 
    151 void
    152 avic_source_name(struct pic_softc *pic, int irq, char *buf, size_t len)
    153 {
    154 	strlcpy(buf, avic_intr_source_names[irq], len);
    155 }
    156 
    157 void
    158 imx31_irq_handler(void *frame)
    159 {
    160 	struct avic_softc * const avic = &pic_avic;
    161 	struct pic_softc * const pic = &avic->avic_pic;
    162 	int32_t saved_nimask;
    163 	int32_t irq;
    164 	int ipl, newipl, oldipl;
    165 
    166 	saved_nimask = INTC_READ(avic, IMX31_NIMASK);
    167 	for (;;) {
    168 		irq = INTC_READ(avic, IMX31_NIVECSR);
    169 		if (irq < 0)
    170 			break;
    171 		ipl = (int16_t) irq;
    172 		KASSERT(ipl >= 0);
    173 		irq >>= 16;
    174 		KASSERT(irq < 64);
    175 		KASSERT(pic->pic_sources[irq] != NULL);
    176 
    177 		/*
    178 		 * If this interrupt is not above the current spl,
    179 		 * mark it as pending and try again.
    180 		 */
    181 		newipl = HW_TO_SW_IPL(ipl);
    182 		if (newipl <= curcpu()->ci_cpl) {
    183 			pic_mark_pending(pic, irq);
    184 			continue;
    185 		}
    186 
    187 		/*
    188 		 * Before enabling interrupts, mask out lower priority
    189 		 * interrupts and raise SPL to its equivalent.
    190 		 */
    191 
    192 		INTC_WRITE(avic, IMX31_NIMASK, ipl);
    193 		oldipl = _splraise(newipl);
    194 		cpsie(I32_bit);
    195 
    196 		pic_dispatch(pic->pic_sources[irq], frame);
    197 
    198 		/*
    199 		 * Disable interrupts again.  Drop SPL.  Restore saved
    200 		 * HW interrupt level.
    201 		 */
    202 		cpsid(I32_bit);
    203 		splx(oldipl);
    204 		INTC_WRITE(avic, IMX31_NIMASK, saved_nimask);
    205 	}
    206 }
    207 
    208 void
    209 imx31_icu_init(void)
    210 {
    211 	pic_add(&pic_avic.avic_pic, 0);
    212 	softintr_init();
    213 }
    214