imx31_icu.c revision 1.1.2.2 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.2 matt __KERNEL_RCSID(0, "$NetBSD: imx31_icu.c,v 1.1.2.2 2007/08/30 07:08:15 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.2 matt static void avic_unblock_irqs(struct pic_softc *, size_t, uint32_t);
58 1.1.2.2 matt static void avic_block_irqs(struct pic_softc *, size_t, uint32_t);
59 1.1.2.1 matt static void avic_establish_irq(struct pic_softc *, int, int, int);
60 1.1.2.2 matt static void avic_source_name(struct pic_softc *, int, char *, size_t);
61 1.1.2.1 matt
62 1.1.2.1 matt const struct pic_ops avic_pic_ops = {
63 1.1.2.2 matt .pic_unblock_irqs = avic_unblock_irqs,
64 1.1.2.2 matt .pic_block_irqs = avic_block_irqs,
65 1.1.2.1 matt .pic_establish_irq = avic_establish_irq,
66 1.1.2.1 matt .pic_source_name = avic_source_name
67 1.1.2.1 matt };
68 1.1.2.1 matt
69 1.1.2.1 matt struct avic_softc {
70 1.1.2.1 matt struct pic_softc avic_pic;
71 1.1.2.1 matt bus_space_tag_t avic_memt;
72 1.1.2.1 matt bus_space_handle_t avic_memh;
73 1.1.2.2 matt } pic_avic = {
74 1.1.2.1 matt .avic_pic = {
75 1.1.2.1 matt .pic_ops = &avic_pic_ops,
76 1.1.2.2 matt .pic_maxsources = 64,
77 1.1.2.1 matt .pic_name = "avic",
78 1.1.2.1 matt },
79 1.1.2.1 matt };
80 1.1.2.1 matt
81 1.1.2.2 matt #define INTC_READ(avic, reg) \
82 1.1.2.2 matt bus_space_read_4((avic)->avic_memt, (avic)->avic_memh, (reg))
83 1.1.2.2 matt #define INTC_WRITE(avic, reg, val) \
84 1.1.2.2 matt bus_space_write_4((avic)->avic_memt, (avic)->avic_memh, (reg), (val))
85 1.1.2.2 matt #define HW_TO_SW_IPL(ipl) ((ipl) + 1)
86 1.1.2.2 matt #define SW_TO_HW_IPL(ipl) ((ipl) - 1)
87 1.1.2.2 matt
88 1.1.2.1 matt void
89 1.1.2.2 matt avic_unblock_irqs(struct pic_softc *pic, size_t irq_base, uint32_t irq_mask)
90 1.1.2.1 matt {
91 1.1.2.1 matt struct avic_softc * const avic = (void *) pic;
92 1.1.2.2 matt #if 0
93 1.1.2.2 matt if (irq_base == 0)
94 1.1.2.2 matt INTC_WRITE(avic, IMX31_INTENABLEL, irq_mask);
95 1.1.2.2 matt else
96 1.1.2.2 matt INTC_WRITE(avic, IMX31_INTENABLEH, irq_mask);
97 1.1.2.2 matt #else
98 1.1.2.2 matt uint32_t irq;
99 1.1.2.2 matt while ((irq = ffs(irq_mask)) != 0) {
100 1.1.2.2 matt irq--;
101 1.1.2.2 matt irq_base += irq;
102 1.1.2.2 matt irq_mask >>= irq;
103 1.1.2.2 matt INTC_WRITE(avic, IMX31_INTENNUM, irq_base);
104 1.1.2.2 matt }
105 1.1.2.2 matt #endif
106 1.1.2.1 matt }
107 1.1.2.1 matt
108 1.1.2.1 matt void
109 1.1.2.2 matt avic_block_irqs(struct pic_softc *pic, size_t irq_base, uint32_t irq_mask)
110 1.1.2.1 matt {
111 1.1.2.1 matt struct avic_softc * const avic = (void *) pic;
112 1.1.2.2 matt #if 0
113 1.1.2.2 matt if (irq_base == 0)
114 1.1.2.2 matt INTC_WRITE(avic, IMX31_INTDISABLEL, irq_mask);
115 1.1.2.2 matt else
116 1.1.2.2 matt INTC_WRITE(avic, IMX31_INTDISABLEH, irq_mask);
117 1.1.2.2 matt #else
118 1.1.2.2 matt uint32_t irq;
119 1.1.2.2 matt while ((irq = ffs(irq_mask)) != 0) {
120 1.1.2.2 matt irq--;
121 1.1.2.2 matt irq_base += irq;
122 1.1.2.2 matt irq_mask >>= irq;
123 1.1.2.2 matt INTC_WRITE(avic, IMX31_INTDISNUM, irq_base);
124 1.1.2.2 matt }
125 1.1.2.2 matt #endif
126 1.1.2.1 matt }
127 1.1.2.1 matt
128 1.1.2.1 matt void
129 1.1.2.2 matt avic_establish_irq(struct pic_softc *pic, int irq, int ipl, int type)
130 1.1.2.1 matt {
131 1.1.2.1 matt struct avic_softc * const avic = (void *) pic;
132 1.1.2.1 matt bus_addr_t priority_reg;
133 1.1.2.1 matt int priority_shift;
134 1.1.2.1 matt uint32_t v;
135 1.1.2.1 matt
136 1.1.2.1 matt KASSERT(irq < 64);
137 1.1.2.1 matt KASSERT(ipl < 16);
138 1.1.2.1 matt
139 1.1.2.2 matt priority_reg = IMX31_NIPRIORITY0 - (irq >> 3);
140 1.1.2.1 matt priority_shift = (irq & 7) * 4;
141 1.1.2.1 matt v = INTC_READ(avic, priority_reg);
142 1.1.2.1 matt v &= ~(0x0f << priority_shift);
143 1.1.2.1 matt v |= SW_TO_HW_IPL(ipl) << priority_shift;
144 1.1.2.1 matt INTC_WRITE(avic, priority_reg, v);
145 1.1.2.1 matt
146 1.1.2.1 matt KASSERT(type == IST_LEVEL);
147 1.1.2.1 matt }
148 1.1.2.1 matt
149 1.1.2.1 matt static const char * const avic_intr_source_names[] = AVIC_INTR_SOURCE_NAMES;
150 1.1.2.1 matt
151 1.1.2.1 matt void
152 1.1.2.1 matt avic_source_name(struct pic_softc *pic, int irq, char *buf, size_t len)
153 1.1.2.1 matt {
154 1.1.2.1 matt strlcpy(buf, avic_intr_source_names[irq], len);
155 1.1.2.1 matt }
156 1.1.2.1 matt
157 1.1.2.1 matt void
158 1.1.2.1 matt imx31_irq_handler(void *frame)
159 1.1.2.1 matt {
160 1.1.2.2 matt struct avic_softc * const avic = &pic_avic;
161 1.1.2.1 matt struct pic_softc * const pic = &avic->avic_pic;
162 1.1.2.1 matt int32_t saved_nimask;
163 1.1.2.2 matt int32_t irq;
164 1.1.2.2 matt int ipl, newipl, oldipl;
165 1.1.2.1 matt
166 1.1.2.2 matt saved_nimask = INTC_READ(avic, IMX31_NIMASK);
167 1.1.2.1 matt for (;;) {
168 1.1.2.2 matt irq = INTC_READ(avic, IMX31_NIVECSR);
169 1.1.2.1 matt if (irq < 0)
170 1.1.2.1 matt break;
171 1.1.2.1 matt ipl = (int16_t) irq;
172 1.1.2.1 matt KASSERT(ipl >= 0);
173 1.1.2.1 matt irq >>= 16;
174 1.1.2.1 matt KASSERT(irq < 64);
175 1.1.2.1 matt KASSERT(pic->pic_sources[irq] != NULL);
176 1.1.2.1 matt
177 1.1.2.1 matt /*
178 1.1.2.1 matt * If this interrupt is not above the current spl,
179 1.1.2.1 matt * mark it as pending and try again.
180 1.1.2.1 matt */
181 1.1.2.1 matt newipl = HW_TO_SW_IPL(ipl);
182 1.1.2.2 matt if (newipl <= curcpu()->ci_cpl) {
183 1.1.2.1 matt pic_mark_pending(pic, irq);
184 1.1.2.1 matt continue;
185 1.1.2.1 matt }
186 1.1.2.1 matt
187 1.1.2.1 matt /*
188 1.1.2.1 matt * Before enabling interrupts, mask out lower priority
189 1.1.2.1 matt * interrupts and raise SPL to its equivalent.
190 1.1.2.1 matt */
191 1.1.2.1 matt
192 1.1.2.2 matt INTC_WRITE(avic, IMX31_NIMASK, ipl);
193 1.1.2.1 matt oldipl = _splraise(newipl);
194 1.1.2.1 matt cpsie(I32_bit);
195 1.1.2.1 matt
196 1.1.2.1 matt pic_dispatch(pic->pic_sources[irq], frame);
197 1.1.2.1 matt
198 1.1.2.1 matt /*
199 1.1.2.1 matt * Disable interrupts again. Drop SPL. Restore saved
200 1.1.2.1 matt * HW interrupt level.
201 1.1.2.1 matt */
202 1.1.2.1 matt cpsid(I32_bit);
203 1.1.2.1 matt splx(oldipl);
204 1.1.2.2 matt INTC_WRITE(avic, IMX31_NIMASK, saved_nimask);
205 1.1.2.1 matt }
206 1.1.2.1 matt }
207 1.1.2.1 matt
208 1.1.2.2 matt void
209 1.1.2.2 matt imx31_icu_init(void)
210 1.1.2.1 matt {
211 1.1.2.2 matt pic_add(&pic_avic.avic_pic, 0);
212 1.1.2.2 matt softintr_init();
213 1.1.2.1 matt }
214