1 /* $NetBSD: imx31_icu.c,v 1.4 2008/06/13 08:49:15 cegger Exp $ */
3 * Copyright (c) 2007 The NetBSD Foundation, Inc.
6 * This code is derived from software contributed to The NetBSD Foundation
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
30 #include <sys/cdefs.h>
31 __KERNEL_RCSID(0, "$NetBSD: imx31_icu.c,v 1.4 2008/06/13 08:49:15 cegger Exp $");
37 #include <sys/param.h>
38 #include <sys/evcnt.h>
39 #include <sys/device.h>
40 #include <sys/atomic.h>
42 #include <uvm/uvm_extern.h>
44 #include <machine/intr.h>
47 #include <arm/armreg.h>
48 #include <arm/cpufunc.h>
50 #include <machine/autoconf.h>
51 #include <machine/bus.h>
53 #include <arm/imx/imx31reg.h>
54 #include <arm/imx/imx31var.h>
56 static void avic_unblock_irqs(struct pic_softc
*, size_t, uint32_t);
57 static void avic_block_irqs(struct pic_softc
*, size_t, uint32_t);
58 static void avic_establish_irq(struct pic_softc
*, struct intrsource
*);
59 static void avic_source_name(struct pic_softc
*, int, char *, size_t);
61 const struct pic_ops avic_pic_ops
= {
62 .pic_unblock_irqs
= avic_unblock_irqs
,
63 .pic_block_irqs
= avic_block_irqs
,
64 .pic_establish_irq
= avic_establish_irq
,
65 .pic_source_name
= avic_source_name
69 struct device avic_dv
;
70 struct pic_softc avic_pic
;
71 bus_space_tag_t avic_memt
;
72 bus_space_handle_t avic_memh
;
75 extern struct cfdriver avic_cd
;
77 #define INTC_READ(avic, reg) \
78 bus_space_read_4((avic)->avic_memt, (avic)->avic_memh, (reg))
79 #define INTC_WRITE(avic, reg, val) \
80 bus_space_write_4((avic)->avic_memt, (avic)->avic_memh, (reg), (val))
81 #define HW_TO_SW_IPL(ipl) ((ipl) + 1)
82 #define SW_TO_HW_IPL(ipl) ((ipl) - 1)
85 avic_unblock_irqs(struct pic_softc
*pic
, size_t irq_base
, uint32_t irq_mask
)
87 struct avic_softc
* const avic
= (void *) pic
;
90 INTC_WRITE(avic
, IMX31_INTENABLEL
, irq_mask
);
92 INTC_WRITE(avic
, IMX31_INTENABLEH
, irq_mask
);
95 while ((irq
= ffs(irq_mask
)) != 0) {
99 INTC_WRITE(avic
, IMX31_INTENNUM
, irq_base
);
105 avic_block_irqs(struct pic_softc
*pic
, size_t irq_base
, uint32_t irq_mask
)
107 struct avic_softc
* const avic
= (void *) pic
;
110 INTC_WRITE(avic
, IMX31_INTDISABLEL
, irq_mask
);
112 INTC_WRITE(avic
, IMX31_INTDISABLEH
, irq_mask
);
115 while ((irq
= ffs(irq_mask
)) != 0) {
119 INTC_WRITE(avic
, IMX31_INTDISNUM
, irq_base
);
125 avic_establish_irq(struct pic_softc
*pic
, struct intrsource
*is
)
127 struct avic_softc
* const avic
= (void *) pic
;
128 bus_addr_t priority_reg
;
132 KASSERT(is
->is_irq
< 64);
133 KASSERT(is
->is_ipl
< 16);
135 priority_reg
= IMX31_NIPRIORITY0
- (is
->is_irq
>> 3);
136 priority_shift
= (is
->is_irq
& 7) * 4;
137 v
= INTC_READ(avic
, priority_reg
);
138 v
&= ~(0x0f << priority_shift
);
139 v
|= SW_TO_HW_IPL(is
->is_ipl
) << priority_shift
;
140 INTC_WRITE(avic
, priority_reg
, v
);
142 KASSERT(is
->is_type
== IST_LEVEL
);
145 static const char * const avic_intr_source_names
[] = AVIC_INTR_SOURCE_NAMES
;
148 avic_source_name(struct pic_softc
*pic
, int irq
, char *buf
, size_t len
)
150 strlcpy(buf
, avic_intr_source_names
[irq
], len
);
154 imx31_irq_handler(void *frame
)
156 struct avic_softc
* const avic
= device_lookup_private(&avic_cd
, 0);
157 struct pic_softc
* const pic
= &avic
->avic_pic
;
158 int32_t saved_nimask
;
160 int ipl
, newipl
, oldipl
;
162 saved_nimask
= INTC_READ(avic
, IMX31_NIMASK
);
164 irq
= INTC_READ(avic
, IMX31_NIVECSR
);
171 KASSERT(pic
->pic_sources
[irq
] != NULL
);
174 * If this interrupt is not above the current spl,
175 * mark it as pending and try again.
177 newipl
= HW_TO_SW_IPL(ipl
);
178 if (newipl
<= curcpu()->ci_cpl
) {
179 pic_mark_pending(pic
, irq
);
184 * Before enabling interrupts, mask out lower priority
185 * interrupts and raise SPL to its equivalent.
188 INTC_WRITE(avic
, IMX31_NIMASK
, ipl
);
189 oldipl
= _splraise(newipl
);
192 pic_dispatch(pic
->pic_sources
[irq
], frame
);
195 * Disable interrupts again. Drop SPL. Restore saved
196 * HW interrupt level.
200 INTC_WRITE(avic
, IMX31_NIMASK
, saved_nimask
);
204 static int avic_match(device_t
, cfdata_t
, void *);
205 static void avic_attach(device_t
, device_t
, void *);
208 sizeof(struct avic_softc
),
209 avic_match
, avic_attach
,
213 avic_match(device_t parent
, cfdata_t self
, void *aux
)
215 struct ahb_attach_args
* const ahba
= aux
;
217 if (ahba
->ahba_addr
!= INTC_BASE
)
224 avic_attach(device_t parent
, device_t self
, void *aux
)
226 struct avic_softc
* const avic
= (void *) self
;
227 struct ahb_attach_args
* const ahba
= aux
;
230 KASSERT(ahba
->ahba_irqbase
!= AHBCF_IRQBASE_DEFAULT
);
231 KASSERT(self
->dv_unit
== 0);
233 if (ahba
->ahba_size
== AHBCF_SIZE_DEFAULT
)
234 ahba
->ahba_size
= INTC_SIZE
;
236 avic
->avic_memt
= ahba
->ahba_memt
;
237 error
= bus_space_map(avic
->avic_memt
, ahba
->ahba_addr
, ahba
->ahba_size
,
238 0, &avic
->avic_memh
);
240 panic("avic_attach: failed to map register %#lx-%#lx: %d",
241 ahba
->ahba_addr
, ahba
->ahba_addr
+ ahba
->ahba_size
- 1,
244 avic
->avic_pic
.pic_ops
= &avic_pic_ops
;
245 avic
->avic_pic
.pic_maxsources
= 64;
246 strlcpy(avic
->avic_pic
.pic_name
, self
->dv_xname
,
247 sizeof(avic
->avic_pic
.pic_name
));
249 pic_add(&avic
->avic_pic
, ahba
->ahba_irqbase
);
250 aprint_normal(": interrupts %d..%d\n",
251 ahba
->ahba_irqbase
, ahba
->ahba_irqbase
+ 63);