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