Home | History | Annotate | Line # | Download | only in imx
imx31_icu.c revision 1.4.4.1
      1  1.4.4.1   skrll /*	$NetBSD: imx31_icu.c,v 1.4.4.1 2009/01/19 13:15:58 skrll Exp $	*/
      2      1.2    matt /*-
      3      1.2    matt  * Copyright (c) 2007 The NetBSD Foundation, Inc.
      4      1.2    matt  * All rights reserved.
      5      1.2    matt  *
      6      1.2    matt  * This code is derived from software contributed to The NetBSD Foundation
      7      1.2    matt  * by Matt Thomas.
      8      1.2    matt  *
      9      1.2    matt  * Redistribution and use in source and binary forms, with or without
     10      1.2    matt  * modification, are permitted provided that the following conditions
     11      1.2    matt  * are met:
     12      1.2    matt  * 1. Redistributions of source code must retain the above copyright
     13      1.2    matt  *    notice, this list of conditions and the following disclaimer.
     14      1.2    matt  * 2. Redistributions in binary form must reproduce the above copyright
     15      1.2    matt  *    notice, this list of conditions and the following disclaimer in the
     16      1.2    matt  *    documentation and/or other materials provided with the distribution.
     17      1.2    matt  *
     18      1.2    matt  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     19      1.2    matt  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     20      1.2    matt  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     21      1.2    matt  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     22      1.2    matt  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     23      1.2    matt  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     24      1.2    matt  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     25      1.2    matt  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     26      1.2    matt  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     27      1.2    matt  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     28      1.2    matt  * POSSIBILITY OF SUCH DAMAGE.
     29      1.2    matt  */
     30      1.2    matt #include <sys/cdefs.h>
     31  1.4.4.1   skrll __KERNEL_RCSID(0, "$NetBSD: imx31_icu.c,v 1.4.4.1 2009/01/19 13:15:58 skrll Exp $");
     32      1.2    matt 
     33      1.2    matt #define _INTR_PRIVATE
     34      1.2    matt 
     35      1.2    matt #include "locators.h"
     36      1.2    matt 
     37      1.2    matt #include <sys/param.h>
     38      1.2    matt #include <sys/evcnt.h>
     39      1.2    matt #include <sys/device.h>
     40  1.4.4.1   skrll #include <sys/atomic.h>
     41      1.2    matt 
     42      1.2    matt #include <uvm/uvm_extern.h>
     43      1.2    matt 
     44      1.2    matt #include <machine/intr.h>
     45      1.2    matt 
     46      1.2    matt #include <arm/cpu.h>
     47      1.2    matt #include <arm/armreg.h>
     48      1.2    matt #include <arm/cpufunc.h>
     49      1.2    matt 
     50      1.2    matt #include <machine/autoconf.h>
     51      1.2    matt #include <machine/bus.h>
     52      1.2    matt 
     53      1.2    matt #include <arm/imx/imx31reg.h>
     54      1.2    matt #include <arm/imx/imx31var.h>
     55      1.2    matt 
     56      1.2    matt static void avic_unblock_irqs(struct pic_softc *, size_t, uint32_t);
     57      1.2    matt static void avic_block_irqs(struct pic_softc *, size_t, uint32_t);
     58      1.2    matt static void avic_establish_irq(struct pic_softc *, struct intrsource *);
     59      1.2    matt static void avic_source_name(struct pic_softc *, int, char *, size_t);
     60      1.2    matt 
     61      1.2    matt const struct pic_ops avic_pic_ops = {
     62      1.2    matt 	.pic_unblock_irqs = avic_unblock_irqs,
     63      1.2    matt 	.pic_block_irqs = avic_block_irqs,
     64      1.2    matt 	.pic_establish_irq = avic_establish_irq,
     65      1.2    matt 	.pic_source_name = avic_source_name
     66      1.2    matt };
     67      1.2    matt 
     68      1.2    matt struct avic_softc {
     69      1.2    matt 	struct device avic_dv;
     70      1.2    matt 	struct pic_softc avic_pic;
     71      1.2    matt 	bus_space_tag_t avic_memt;
     72      1.2    matt 	bus_space_handle_t avic_memh;
     73      1.2    matt };
     74      1.2    matt 
     75      1.2    matt extern struct cfdriver avic_cd;
     76      1.2    matt 
     77      1.2    matt #define	INTC_READ(avic, reg) \
     78      1.2    matt 	bus_space_read_4((avic)->avic_memt, (avic)->avic_memh, (reg))
     79      1.2    matt #define	INTC_WRITE(avic, reg, val) \
     80      1.2    matt 	bus_space_write_4((avic)->avic_memt, (avic)->avic_memh, (reg), (val))
     81      1.2    matt #define	HW_TO_SW_IPL(ipl)	((ipl) + 1)
     82      1.2    matt #define	SW_TO_HW_IPL(ipl)	((ipl) - 1)
     83      1.2    matt 
     84      1.2    matt void
     85      1.2    matt avic_unblock_irqs(struct pic_softc *pic, size_t irq_base, uint32_t irq_mask)
     86      1.2    matt {
     87      1.2    matt 	struct avic_softc * const avic = (void *) pic;
     88      1.2    matt #if 0
     89      1.2    matt 	if (irq_base == 0)
     90      1.2    matt 		INTC_WRITE(avic, IMX31_INTENABLEL, irq_mask);
     91      1.2    matt 	else
     92      1.2    matt 		INTC_WRITE(avic, IMX31_INTENABLEH, irq_mask);
     93      1.2    matt #else
     94      1.2    matt 	uint32_t irq;
     95      1.2    matt 	while ((irq = ffs(irq_mask)) != 0) {
     96      1.2    matt 		irq--;
     97      1.2    matt 		irq_base += irq;
     98      1.2    matt 		irq_mask >>= irq;
     99      1.2    matt 		INTC_WRITE(avic, IMX31_INTENNUM, irq_base);
    100      1.2    matt 	}
    101      1.2    matt #endif
    102      1.2    matt }
    103      1.2    matt 
    104      1.2    matt void
    105      1.2    matt avic_block_irqs(struct pic_softc *pic, size_t irq_base, uint32_t irq_mask)
    106      1.2    matt {
    107      1.2    matt 	struct avic_softc * const avic = (void *) pic;
    108      1.2    matt #if 0
    109      1.2    matt 	if (irq_base == 0)
    110      1.2    matt 		INTC_WRITE(avic, IMX31_INTDISABLEL, irq_mask);
    111      1.2    matt 	else
    112      1.2    matt 		INTC_WRITE(avic, IMX31_INTDISABLEH, irq_mask);
    113      1.2    matt #else
    114      1.2    matt 	uint32_t irq;
    115      1.2    matt 	while ((irq = ffs(irq_mask)) != 0) {
    116      1.2    matt 		irq--;
    117      1.2    matt 		irq_base += irq;
    118      1.2    matt 		irq_mask >>= irq;
    119      1.2    matt 		INTC_WRITE(avic, IMX31_INTDISNUM, irq_base);
    120      1.2    matt 	}
    121      1.2    matt #endif
    122      1.2    matt }
    123      1.2    matt 
    124      1.2    matt void
    125      1.2    matt avic_establish_irq(struct pic_softc *pic, struct intrsource *is)
    126      1.2    matt {
    127      1.2    matt 	struct avic_softc * const avic = (void *) pic;
    128      1.2    matt 	bus_addr_t priority_reg;
    129      1.2    matt 	int priority_shift;
    130      1.2    matt 	uint32_t v;
    131      1.2    matt 
    132      1.2    matt 	KASSERT(is->is_irq < 64);
    133      1.2    matt 	KASSERT(is->is_ipl < 16);
    134      1.2    matt 
    135      1.2    matt 	priority_reg = IMX31_NIPRIORITY0 - (is->is_irq >> 3);
    136      1.2    matt 	priority_shift = (is->is_irq & 7) * 4;
    137      1.2    matt 	v = INTC_READ(avic, priority_reg);
    138      1.2    matt 	v &= ~(0x0f << priority_shift);
    139      1.2    matt 	v |= SW_TO_HW_IPL(is->is_ipl) << priority_shift;
    140      1.2    matt 	INTC_WRITE(avic, priority_reg, v);
    141      1.2    matt 
    142      1.2    matt 	KASSERT(is->is_type == IST_LEVEL);
    143      1.2    matt }
    144      1.2    matt 
    145      1.2    matt static const char * const avic_intr_source_names[] = AVIC_INTR_SOURCE_NAMES;
    146      1.2    matt 
    147      1.2    matt void
    148      1.2    matt avic_source_name(struct pic_softc *pic, int irq, char *buf, size_t len)
    149      1.2    matt {
    150      1.2    matt 	strlcpy(buf, avic_intr_source_names[irq], len);
    151      1.2    matt }
    152      1.2    matt 
    153      1.2    matt void
    154      1.2    matt imx31_irq_handler(void *frame)
    155      1.2    matt {
    156      1.4  cegger 	struct avic_softc * const avic = device_lookup_private(&avic_cd, 0);
    157      1.2    matt 	struct pic_softc * const pic = &avic->avic_pic;
    158      1.2    matt 	int32_t saved_nimask;
    159      1.2    matt 	int32_t irq;
    160      1.2    matt 	int ipl, newipl, oldipl;
    161      1.2    matt 
    162      1.2    matt 	saved_nimask = INTC_READ(avic, IMX31_NIMASK);
    163      1.2    matt 	for (;;) {
    164      1.2    matt 		irq = INTC_READ(avic, IMX31_NIVECSR);
    165      1.2    matt 		if (irq < 0)
    166      1.2    matt 			break;
    167      1.2    matt 		ipl = (int16_t) irq;
    168      1.2    matt 		KASSERT(ipl >= 0);
    169      1.2    matt 		irq >>= 16;
    170      1.2    matt 		KASSERT(irq < 64);
    171      1.2    matt 		KASSERT(pic->pic_sources[irq] != NULL);
    172      1.2    matt 
    173      1.2    matt 		/*
    174      1.2    matt 		 * If this interrupt is not above the current spl,
    175      1.2    matt 		 * mark it as pending and try again.
    176      1.2    matt 		 */
    177      1.2    matt 		newipl = HW_TO_SW_IPL(ipl);
    178      1.2    matt 		if (newipl <= curcpu()->ci_cpl) {
    179      1.2    matt 			pic_mark_pending(pic, irq);
    180      1.2    matt 			continue;
    181      1.2    matt 		}
    182      1.2    matt 
    183      1.2    matt 		/*
    184      1.2    matt 		 * Before enabling interrupts, mask out lower priority
    185      1.2    matt 		 * interrupts and raise SPL to its equivalent.
    186      1.2    matt 		 */
    187      1.2    matt 
    188      1.2    matt 		INTC_WRITE(avic, IMX31_NIMASK, ipl);
    189      1.2    matt 		oldipl = _splraise(newipl);
    190      1.2    matt 		cpsie(I32_bit);
    191      1.2    matt 
    192      1.2    matt 		pic_dispatch(pic->pic_sources[irq], frame);
    193      1.2    matt 
    194      1.2    matt 		/*
    195      1.2    matt 		 * Disable interrupts again.  Drop SPL.  Restore saved
    196      1.2    matt 		 * HW interrupt level.
    197      1.2    matt 		 */
    198      1.2    matt 		cpsid(I32_bit);
    199      1.2    matt 		splx(oldipl);
    200      1.2    matt 		INTC_WRITE(avic, IMX31_NIMASK, saved_nimask);
    201      1.2    matt 	}
    202      1.2    matt }
    203      1.2    matt 
    204      1.2    matt static int avic_match(device_t, cfdata_t, void *);
    205      1.2    matt static void avic_attach(device_t, device_t, void *);
    206      1.2    matt 
    207      1.2    matt CFATTACH_DECL(avic,
    208      1.2    matt 	sizeof(struct avic_softc),
    209      1.2    matt 	avic_match, avic_attach,
    210      1.2    matt 	NULL, NULL);
    211      1.2    matt 
    212      1.2    matt int
    213      1.2    matt avic_match(device_t parent, cfdata_t self, void *aux)
    214      1.2    matt {
    215      1.2    matt 	struct ahb_attach_args * const ahba = aux;
    216      1.2    matt 
    217      1.2    matt 	if (ahba->ahba_addr != INTC_BASE)
    218      1.2    matt 		return 0;
    219      1.2    matt 
    220      1.2    matt 	return 1;
    221      1.2    matt }
    222      1.2    matt 
    223      1.2    matt void
    224      1.2    matt avic_attach(device_t parent, device_t self, void *aux)
    225      1.2    matt {
    226      1.2    matt 	struct avic_softc * const avic = (void *) self;
    227      1.2    matt 	struct ahb_attach_args * const ahba = aux;
    228      1.2    matt 	int error;
    229      1.2    matt 
    230      1.2    matt 	KASSERT(ahba->ahba_irqbase != AHBCF_IRQBASE_DEFAULT);
    231      1.2    matt 	KASSERT(self->dv_unit == 0);
    232      1.2    matt 
    233      1.2    matt 	if (ahba->ahba_size == AHBCF_SIZE_DEFAULT)
    234      1.2    matt 		ahba->ahba_size = INTC_SIZE;
    235      1.2    matt 
    236      1.2    matt 	avic->avic_memt = ahba->ahba_memt;
    237      1.2    matt 	error = bus_space_map(avic->avic_memt, ahba->ahba_addr, ahba->ahba_size,
    238      1.2    matt 	    0, &avic->avic_memh);
    239      1.2    matt 	if (error)
    240      1.2    matt 		panic("avic_attach: failed to map register %#lx-%#lx: %d",
    241      1.2    matt 		    ahba->ahba_addr, ahba->ahba_addr + ahba->ahba_size - 1,
    242      1.2    matt 		    error);
    243      1.2    matt 
    244      1.2    matt 	avic->avic_pic.pic_ops = &avic_pic_ops;
    245      1.2    matt 	avic->avic_pic.pic_maxsources = 64;
    246      1.2    matt 	strlcpy(avic->avic_pic.pic_name, self->dv_xname,
    247      1.2    matt 	    sizeof(avic->avic_pic.pic_name));
    248      1.2    matt 
    249      1.2    matt 	pic_add(&avic->avic_pic, ahba->ahba_irqbase);
    250      1.2    matt 	aprint_normal(": interrupts %d..%d\n",
    251      1.2    matt 	    ahba->ahba_irqbase, ahba->ahba_irqbase + 63);
    252      1.2    matt #if 0
    253      1.2    matt 	softintr_init();
    254      1.2    matt #endif
    255      1.2    matt }
    256