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