imx31_icu.c revision 1.1.2.4 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.4 matt __KERNEL_RCSID(0, "$NetBSD: imx31_icu.c,v 1.1.2.4 2007/09/12 06:19:45 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.3 matt #include "locators.h"
42 1.1.2.3 matt
43 1.1.2.1 matt #include <sys/param.h>
44 1.1.2.1 matt #include <sys/evcnt.h>
45 1.1.2.3 matt #include <sys/device.h>
46 1.1.2.1 matt
47 1.1.2.1 matt #include <uvm/uvm_extern.h>
48 1.1.2.1 matt
49 1.1.2.1 matt #include <machine/intr.h>
50 1.1.2.1 matt
51 1.1.2.1 matt #include <arm/cpu.h>
52 1.1.2.1 matt #include <arm/armreg.h>
53 1.1.2.1 matt #include <arm/cpufunc.h>
54 1.1.2.1 matt
55 1.1.2.3 matt #include <machine/autoconf.h>
56 1.1.2.1 matt #include <machine/atomic.h>
57 1.1.2.1 matt #include <machine/bus.h>
58 1.1.2.1 matt
59 1.1.2.3 matt #include <arm/imx/imx31reg.h>
60 1.1.2.3 matt #include <arm/imx/imx31var.h>
61 1.1.2.1 matt
62 1.1.2.2 matt static void avic_unblock_irqs(struct pic_softc *, size_t, uint32_t);
63 1.1.2.2 matt static void avic_block_irqs(struct pic_softc *, size_t, uint32_t);
64 1.1.2.1 matt static void avic_establish_irq(struct pic_softc *, int, int, int);
65 1.1.2.2 matt static void avic_source_name(struct pic_softc *, int, char *, size_t);
66 1.1.2.1 matt
67 1.1.2.1 matt const struct pic_ops avic_pic_ops = {
68 1.1.2.2 matt .pic_unblock_irqs = avic_unblock_irqs,
69 1.1.2.2 matt .pic_block_irqs = avic_block_irqs,
70 1.1.2.1 matt .pic_establish_irq = avic_establish_irq,
71 1.1.2.1 matt .pic_source_name = avic_source_name
72 1.1.2.1 matt };
73 1.1.2.1 matt
74 1.1.2.1 matt struct avic_softc {
75 1.1.2.3 matt struct device avic_dv;
76 1.1.2.1 matt struct pic_softc avic_pic;
77 1.1.2.1 matt bus_space_tag_t avic_memt;
78 1.1.2.1 matt bus_space_handle_t avic_memh;
79 1.1.2.1 matt };
80 1.1.2.1 matt
81 1.1.2.3 matt extern struct cfdriver avic_cd;
82 1.1.2.3 matt
83 1.1.2.2 matt #define INTC_READ(avic, reg) \
84 1.1.2.2 matt bus_space_read_4((avic)->avic_memt, (avic)->avic_memh, (reg))
85 1.1.2.2 matt #define INTC_WRITE(avic, reg, val) \
86 1.1.2.2 matt bus_space_write_4((avic)->avic_memt, (avic)->avic_memh, (reg), (val))
87 1.1.2.2 matt #define HW_TO_SW_IPL(ipl) ((ipl) + 1)
88 1.1.2.2 matt #define SW_TO_HW_IPL(ipl) ((ipl) - 1)
89 1.1.2.2 matt
90 1.1.2.1 matt void
91 1.1.2.2 matt avic_unblock_irqs(struct pic_softc *pic, size_t irq_base, uint32_t irq_mask)
92 1.1.2.1 matt {
93 1.1.2.1 matt struct avic_softc * const avic = (void *) pic;
94 1.1.2.2 matt #if 0
95 1.1.2.2 matt if (irq_base == 0)
96 1.1.2.2 matt INTC_WRITE(avic, IMX31_INTENABLEL, irq_mask);
97 1.1.2.2 matt else
98 1.1.2.2 matt INTC_WRITE(avic, IMX31_INTENABLEH, irq_mask);
99 1.1.2.2 matt #else
100 1.1.2.2 matt uint32_t irq;
101 1.1.2.2 matt while ((irq = ffs(irq_mask)) != 0) {
102 1.1.2.2 matt irq--;
103 1.1.2.2 matt irq_base += irq;
104 1.1.2.2 matt irq_mask >>= irq;
105 1.1.2.2 matt INTC_WRITE(avic, IMX31_INTENNUM, irq_base);
106 1.1.2.2 matt }
107 1.1.2.2 matt #endif
108 1.1.2.1 matt }
109 1.1.2.1 matt
110 1.1.2.1 matt void
111 1.1.2.2 matt avic_block_irqs(struct pic_softc *pic, size_t irq_base, uint32_t irq_mask)
112 1.1.2.1 matt {
113 1.1.2.1 matt struct avic_softc * const avic = (void *) pic;
114 1.1.2.2 matt #if 0
115 1.1.2.2 matt if (irq_base == 0)
116 1.1.2.2 matt INTC_WRITE(avic, IMX31_INTDISABLEL, irq_mask);
117 1.1.2.2 matt else
118 1.1.2.2 matt INTC_WRITE(avic, IMX31_INTDISABLEH, irq_mask);
119 1.1.2.2 matt #else
120 1.1.2.2 matt uint32_t irq;
121 1.1.2.2 matt while ((irq = ffs(irq_mask)) != 0) {
122 1.1.2.2 matt irq--;
123 1.1.2.2 matt irq_base += irq;
124 1.1.2.2 matt irq_mask >>= irq;
125 1.1.2.2 matt INTC_WRITE(avic, IMX31_INTDISNUM, irq_base);
126 1.1.2.2 matt }
127 1.1.2.2 matt #endif
128 1.1.2.1 matt }
129 1.1.2.1 matt
130 1.1.2.1 matt void
131 1.1.2.2 matt avic_establish_irq(struct pic_softc *pic, int irq, int ipl, int type)
132 1.1.2.1 matt {
133 1.1.2.1 matt struct avic_softc * const avic = (void *) pic;
134 1.1.2.1 matt bus_addr_t priority_reg;
135 1.1.2.1 matt int priority_shift;
136 1.1.2.1 matt uint32_t v;
137 1.1.2.1 matt
138 1.1.2.1 matt KASSERT(irq < 64);
139 1.1.2.1 matt KASSERT(ipl < 16);
140 1.1.2.1 matt
141 1.1.2.2 matt priority_reg = IMX31_NIPRIORITY0 - (irq >> 3);
142 1.1.2.1 matt priority_shift = (irq & 7) * 4;
143 1.1.2.1 matt v = INTC_READ(avic, priority_reg);
144 1.1.2.1 matt v &= ~(0x0f << priority_shift);
145 1.1.2.1 matt v |= SW_TO_HW_IPL(ipl) << priority_shift;
146 1.1.2.1 matt INTC_WRITE(avic, priority_reg, v);
147 1.1.2.1 matt
148 1.1.2.1 matt KASSERT(type == IST_LEVEL);
149 1.1.2.1 matt }
150 1.1.2.1 matt
151 1.1.2.1 matt static const char * const avic_intr_source_names[] = AVIC_INTR_SOURCE_NAMES;
152 1.1.2.1 matt
153 1.1.2.1 matt void
154 1.1.2.1 matt avic_source_name(struct pic_softc *pic, int irq, char *buf, size_t len)
155 1.1.2.1 matt {
156 1.1.2.1 matt strlcpy(buf, avic_intr_source_names[irq], len);
157 1.1.2.1 matt }
158 1.1.2.1 matt
159 1.1.2.1 matt void
160 1.1.2.1 matt imx31_irq_handler(void *frame)
161 1.1.2.1 matt {
162 1.1.2.3 matt struct avic_softc * const avic = avic_cd.cd_devs[0];
163 1.1.2.1 matt struct pic_softc * const pic = &avic->avic_pic;
164 1.1.2.1 matt int32_t saved_nimask;
165 1.1.2.2 matt int32_t irq;
166 1.1.2.2 matt int ipl, newipl, oldipl;
167 1.1.2.1 matt
168 1.1.2.2 matt saved_nimask = INTC_READ(avic, IMX31_NIMASK);
169 1.1.2.1 matt for (;;) {
170 1.1.2.2 matt irq = INTC_READ(avic, IMX31_NIVECSR);
171 1.1.2.1 matt if (irq < 0)
172 1.1.2.1 matt break;
173 1.1.2.1 matt ipl = (int16_t) irq;
174 1.1.2.1 matt KASSERT(ipl >= 0);
175 1.1.2.1 matt irq >>= 16;
176 1.1.2.1 matt KASSERT(irq < 64);
177 1.1.2.1 matt KASSERT(pic->pic_sources[irq] != NULL);
178 1.1.2.1 matt
179 1.1.2.1 matt /*
180 1.1.2.1 matt * If this interrupt is not above the current spl,
181 1.1.2.1 matt * mark it as pending and try again.
182 1.1.2.1 matt */
183 1.1.2.1 matt newipl = HW_TO_SW_IPL(ipl);
184 1.1.2.2 matt if (newipl <= curcpu()->ci_cpl) {
185 1.1.2.1 matt pic_mark_pending(pic, irq);
186 1.1.2.1 matt continue;
187 1.1.2.1 matt }
188 1.1.2.1 matt
189 1.1.2.1 matt /*
190 1.1.2.1 matt * Before enabling interrupts, mask out lower priority
191 1.1.2.1 matt * interrupts and raise SPL to its equivalent.
192 1.1.2.1 matt */
193 1.1.2.1 matt
194 1.1.2.2 matt INTC_WRITE(avic, IMX31_NIMASK, ipl);
195 1.1.2.1 matt oldipl = _splraise(newipl);
196 1.1.2.1 matt cpsie(I32_bit);
197 1.1.2.1 matt
198 1.1.2.1 matt pic_dispatch(pic->pic_sources[irq], frame);
199 1.1.2.1 matt
200 1.1.2.1 matt /*
201 1.1.2.1 matt * Disable interrupts again. Drop SPL. Restore saved
202 1.1.2.1 matt * HW interrupt level.
203 1.1.2.1 matt */
204 1.1.2.1 matt cpsid(I32_bit);
205 1.1.2.1 matt splx(oldipl);
206 1.1.2.2 matt INTC_WRITE(avic, IMX31_NIMASK, saved_nimask);
207 1.1.2.1 matt }
208 1.1.2.1 matt }
209 1.1.2.1 matt
210 1.1.2.3 matt static int avic_match(device_t, cfdata_t, void *);
211 1.1.2.3 matt static void avic_attach(device_t, device_t, void *);
212 1.1.2.3 matt
213 1.1.2.3 matt CFATTACH_DECL(avic,
214 1.1.2.3 matt sizeof(struct avic_softc),
215 1.1.2.3 matt avic_match, avic_attach,
216 1.1.2.3 matt NULL, NULL);
217 1.1.2.3 matt
218 1.1.2.3 matt int
219 1.1.2.3 matt avic_match(device_t parent, cfdata_t self, void *aux)
220 1.1.2.3 matt {
221 1.1.2.3 matt struct ahb_attach_args * const ahba = aux;
222 1.1.2.3 matt
223 1.1.2.3 matt if (ahba->ahba_addr != INTC_BASE)
224 1.1.2.3 matt return 0;
225 1.1.2.3 matt
226 1.1.2.3 matt return 1;
227 1.1.2.3 matt }
228 1.1.2.3 matt
229 1.1.2.2 matt void
230 1.1.2.3 matt avic_attach(device_t parent, device_t self, void *aux)
231 1.1.2.1 matt {
232 1.1.2.3 matt struct avic_softc * const avic = (void *) self;
233 1.1.2.3 matt struct ahb_attach_args * const ahba = aux;
234 1.1.2.3 matt int error;
235 1.1.2.3 matt
236 1.1.2.3 matt KASSERT(ahba->ahba_irqbase != AHBCF_IRQBASE_DEFAULT);
237 1.1.2.4 matt KASSERT(self->dv_unit == 0);
238 1.1.2.4 matt
239 1.1.2.4 matt if (ahba->ahba_size == AHBCF_SIZE_DEFAULT)
240 1.1.2.4 matt ahba->ahba_size = INTC_SIZE;
241 1.1.2.3 matt
242 1.1.2.3 matt avic->avic_memt = ahba->ahba_memt;
243 1.1.2.3 matt error = bus_space_map(avic->avic_memt, ahba->ahba_addr, ahba->ahba_size,
244 1.1.2.3 matt 0, &avic->avic_memh);
245 1.1.2.3 matt if (error)
246 1.1.2.3 matt panic("avic_attach: failed to map register %#lx-%#lx: %d",
247 1.1.2.3 matt ahba->ahba_addr, ahba->ahba_addr + ahba->ahba_size - 1,
248 1.1.2.3 matt error);
249 1.1.2.3 matt
250 1.1.2.3 matt avic->avic_pic.pic_ops = &avic_pic_ops;
251 1.1.2.3 matt avic->avic_pic.pic_maxsources = 64;
252 1.1.2.3 matt strlcpy(avic->avic_pic.pic_name, self->dv_xname,
253 1.1.2.3 matt sizeof(avic->avic_pic.pic_name));
254 1.1.2.3 matt
255 1.1.2.4 matt pic_add(&avic->avic_pic, ahba->ahba_irqbase);
256 1.1.2.4 matt aprint_normal(": interrupts %d..%d\n",
257 1.1.2.4 matt ahba->ahba_irqbase, ahba->ahba_irqbase + 63);
258 1.1.2.4 matt #if 0
259 1.1.2.2 matt softintr_init();
260 1.1.2.4 matt #endif
261 1.1.2.1 matt }
262