imx23_icoll.c revision 1.2.6.2 1 1.2.6.2 tls /* $Id: imx23_icoll.c,v 1.2.6.2 2013/02/25 00:28:27 tls Exp $ */
2 1.2.6.2 tls
3 1.2.6.2 tls /*
4 1.2.6.2 tls * Copyright (c) 2012 The NetBSD Foundation, Inc.
5 1.2.6.2 tls * All rights reserved.
6 1.2.6.2 tls *
7 1.2.6.2 tls * This code is derived from software contributed to The NetBSD Foundation
8 1.2.6.2 tls * by Petri Laakso.
9 1.2.6.2 tls *
10 1.2.6.2 tls * Redistribution and use in source and binary forms, with or without
11 1.2.6.2 tls * modification, are permitted provided that the following conditions
12 1.2.6.2 tls * are met:
13 1.2.6.2 tls * 1. Redistributions of source code must retain the above copyright
14 1.2.6.2 tls * notice, this list of conditions and the following disclaimer.
15 1.2.6.2 tls * 2. Redistributions in binary form must reproduce the above copyright
16 1.2.6.2 tls * notice, this list of conditions and the following disclaimer in the
17 1.2.6.2 tls * documentation and/or other materials provided with the distribution.
18 1.2.6.2 tls *
19 1.2.6.2 tls * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.2.6.2 tls * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.2.6.2 tls * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.2.6.2 tls * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.2.6.2 tls * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.2.6.2 tls * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.2.6.2 tls * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.2.6.2 tls * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.2.6.2 tls * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.2.6.2 tls * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.2.6.2 tls * POSSIBILITY OF SUCH DAMAGE.
30 1.2.6.2 tls */
31 1.2.6.2 tls
32 1.2.6.2 tls #include <sys/param.h>
33 1.2.6.2 tls #include <sys/bus.h>
34 1.2.6.2 tls #include <sys/cpu.h>
35 1.2.6.2 tls #include <sys/device.h>
36 1.2.6.2 tls #include <sys/errno.h>
37 1.2.6.2 tls #include <sys/systm.h>
38 1.2.6.2 tls
39 1.2.6.2 tls #define _INTR_PRIVATE
40 1.2.6.2 tls #include <arm/pic/picvar.h>
41 1.2.6.2 tls
42 1.2.6.2 tls #include <arm/imx/imx23_icollreg.h>
43 1.2.6.2 tls #include <arm/imx/imx23var.h>
44 1.2.6.2 tls
45 1.2.6.2 tls #define ICOLL_SOFT_RST_LOOP 455 /* At least 1 us ... */
46 1.2.6.2 tls #define ICOLL_READ(sc, reg) \
47 1.2.6.2 tls bus_space_read_4(sc->sc_iot, sc->sc_hdl, (reg))
48 1.2.6.2 tls #define ICOLL_WRITE(sc, reg, val) \
49 1.2.6.2 tls bus_space_write_4(sc->sc_iot, sc->sc_hdl, (reg), (val))
50 1.2.6.2 tls
51 1.2.6.2 tls #define ICOLL_IRQ_REG_SIZE 0x10
52 1.2.6.2 tls #define ICOLL_CLR_IRQ(sc, irq) \
53 1.2.6.2 tls ICOLL_WRITE(sc, HW_ICOLL_INTERRUPT0_CLR + \
54 1.2.6.2 tls (irq) * ICOLL_IRQ_REG_SIZE, \
55 1.2.6.2 tls HW_ICOLL_INTERRUPT_ENABLE)
56 1.2.6.2 tls #define ICOLL_SET_IRQ(sc, irq) \
57 1.2.6.2 tls ICOLL_WRITE(sc, HW_ICOLL_INTERRUPT0_SET + \
58 1.2.6.2 tls (irq) * ICOLL_IRQ_REG_SIZE, \
59 1.2.6.2 tls HW_ICOLL_INTERRUPT_ENABLE)
60 1.2.6.2 tls #define ICOLL_GET_PRIO(sc, irq) \
61 1.2.6.2 tls __SHIFTOUT(ICOLL_READ(sc, HW_ICOLL_INTERRUPT0 + \
62 1.2.6.2 tls (irq) * ICOLL_IRQ_REG_SIZE), \
63 1.2.6.2 tls HW_ICOLL_INTERRUPT_PRIORITY)
64 1.2.6.2 tls
65 1.2.6.2 tls #define PICTOSOFTC(pic) \
66 1.2.6.2 tls ((struct icoll_softc *)((char *)(pic) - \
67 1.2.6.2 tls offsetof(struct icoll_softc, sc_pic)))
68 1.2.6.2 tls
69 1.2.6.2 tls /*
70 1.2.6.2 tls * pic callbacks.
71 1.2.6.2 tls */
72 1.2.6.2 tls static void icoll_unblock_irqs(struct pic_softc *, size_t, uint32_t);
73 1.2.6.2 tls static void icoll_block_irqs(struct pic_softc *, size_t, uint32_t);
74 1.2.6.2 tls static int icoll_find_pending_irqs(struct pic_softc *);
75 1.2.6.2 tls static void icoll_establish_irq(struct pic_softc *, struct intrsource *);
76 1.2.6.2 tls static void icoll_source_name(struct pic_softc *, int, char *, size_t);
77 1.2.6.2 tls static void icoll_set_priority(struct pic_softc *, int);
78 1.2.6.2 tls
79 1.2.6.2 tls /*
80 1.2.6.2 tls * autoconf(9) callbacks.
81 1.2.6.2 tls */
82 1.2.6.2 tls static int icoll_match(device_t, cfdata_t, void *);
83 1.2.6.2 tls static void icoll_attach(device_t, device_t, void *);
84 1.2.6.2 tls static int icoll_activate(device_t, enum devact);
85 1.2.6.2 tls
86 1.2.6.2 tls /*
87 1.2.6.2 tls * ARM interrupt handler.
88 1.2.6.2 tls */
89 1.2.6.2 tls void imx23_intr_dispatch(struct clockframe *);
90 1.2.6.2 tls
91 1.2.6.2 tls const static struct pic_ops icoll_pic_ops = {
92 1.2.6.2 tls .pic_unblock_irqs = icoll_unblock_irqs,
93 1.2.6.2 tls .pic_block_irqs = icoll_block_irqs,
94 1.2.6.2 tls .pic_find_pending_irqs = icoll_find_pending_irqs,
95 1.2.6.2 tls .pic_establish_irq = icoll_establish_irq,
96 1.2.6.2 tls .pic_source_name = icoll_source_name,
97 1.2.6.2 tls .pic_set_priority = icoll_set_priority
98 1.2.6.2 tls };
99 1.2.6.2 tls
100 1.2.6.2 tls struct icoll_softc {
101 1.2.6.2 tls device_t sc_dev;
102 1.2.6.2 tls struct pic_softc sc_pic;
103 1.2.6.2 tls bus_space_tag_t sc_iot;
104 1.2.6.2 tls bus_space_handle_t sc_hdl;
105 1.2.6.2 tls };
106 1.2.6.2 tls
107 1.2.6.2 tls /* For IRQ handler. */
108 1.2.6.2 tls static struct icoll_softc *icoll_sc;
109 1.2.6.2 tls
110 1.2.6.2 tls /*
111 1.2.6.2 tls * Private to driver.
112 1.2.6.2 tls */
113 1.2.6.2 tls static void icoll_reset(struct icoll_softc *);
114 1.2.6.2 tls
115 1.2.6.2 tls CFATTACH_DECL3_NEW(icoll,
116 1.2.6.2 tls sizeof(struct icoll_softc),
117 1.2.6.2 tls icoll_match,
118 1.2.6.2 tls icoll_attach,
119 1.2.6.2 tls NULL,
120 1.2.6.2 tls icoll_activate,
121 1.2.6.2 tls NULL,
122 1.2.6.2 tls NULL,
123 1.2.6.2 tls 0);
124 1.2.6.2 tls
125 1.2.6.2 tls /*
126 1.2.6.2 tls * ARM interrupt handler.
127 1.2.6.2 tls */
128 1.2.6.2 tls void
129 1.2.6.2 tls imx23_intr_dispatch(struct clockframe *frame)
130 1.2.6.2 tls {
131 1.2.6.2 tls struct cpu_info * const ci = curcpu();
132 1.2.6.2 tls struct pic_softc *pic_sc;
133 1.2.6.2 tls int saved_spl;
134 1.2.6.2 tls uint8_t irq;
135 1.2.6.2 tls uint8_t prio;
136 1.2.6.2 tls
137 1.2.6.2 tls pic_sc = &icoll_sc->sc_pic;
138 1.2.6.2 tls
139 1.2.6.2 tls ci->ci_data.cpu_nintr++;
140 1.2.6.2 tls
141 1.2.6.2 tls /* Save current spl. */
142 1.2.6.2 tls saved_spl = curcpl();
143 1.2.6.2 tls
144 1.2.6.2 tls /* IRQ to be handled. */
145 1.2.6.2 tls irq = __SHIFTOUT(ICOLL_READ(icoll_sc, HW_ICOLL_STAT),
146 1.2.6.2 tls HW_ICOLL_STAT_VECTOR_NUMBER);
147 1.2.6.2 tls
148 1.2.6.2 tls /* Save IRQ's priority. Acknowledge it later. */
149 1.2.6.2 tls prio = ICOLL_GET_PRIO(icoll_sc, irq);
150 1.2.6.2 tls
151 1.2.6.2 tls /*
152 1.2.6.2 tls * Notify ICOLL to deassert IRQ before re-enabling the IRQ's.
153 1.2.6.2 tls * This is done by writing anything to HW_ICOLL_VECTOR.
154 1.2.6.2 tls */
155 1.2.6.2 tls ICOLL_WRITE(icoll_sc, HW_ICOLL_VECTOR,
156 1.2.6.2 tls __SHIFTIN(0x3fffffff, HW_ICOLL_VECTOR_IRQVECTOR));
157 1.2.6.2 tls
158 1.2.6.2 tls /* Bogus IRQ. */
159 1.2.6.2 tls if (irq == 0x7f) {
160 1.2.6.2 tls cpsie(I32_bit);
161 1.2.6.2 tls ICOLL_WRITE(icoll_sc, HW_ICOLL_LEVELACK, (1<<prio));
162 1.2.6.2 tls cpsid(I32_bit);
163 1.2.6.2 tls return;
164 1.2.6.2 tls }
165 1.2.6.2 tls
166 1.2.6.2 tls /* Raise the spl to the level of the IRQ. */
167 1.2.6.2 tls if (pic_sc->pic_sources[irq]->is_ipl > ci->ci_cpl)
168 1.2.6.2 tls saved_spl = _splraise(pic_sc->pic_sources[irq]->is_ipl);
169 1.2.6.2 tls
170 1.2.6.2 tls /* Call the handler registered for the IRQ. */
171 1.2.6.2 tls cpsie(I32_bit);
172 1.2.6.2 tls pic_dispatch(pic_sc->pic_sources[irq], frame);
173 1.2.6.2 tls
174 1.2.6.2 tls /*
175 1.2.6.2 tls * Acknowledge the IRQ by writing its priority to HW_ICOLL_LEVELACK.
176 1.2.6.2 tls * Interrupts should be enabled.
177 1.2.6.2 tls */
178 1.2.6.2 tls ICOLL_WRITE(icoll_sc, HW_ICOLL_LEVELACK, (1<<prio));
179 1.2.6.2 tls cpsid(I32_bit);
180 1.2.6.2 tls
181 1.2.6.2 tls /* Restore the saved spl. */
182 1.2.6.2 tls splx(saved_spl);
183 1.2.6.2 tls
184 1.2.6.2 tls return;
185 1.2.6.2 tls }
186 1.2.6.2 tls
187 1.2.6.2 tls /*
188 1.2.6.2 tls * pic callbacks.
189 1.2.6.2 tls */
190 1.2.6.2 tls static void
191 1.2.6.2 tls icoll_unblock_irqs(struct pic_softc *pic, size_t irq_base, uint32_t irq_mask)
192 1.2.6.2 tls {
193 1.2.6.2 tls struct icoll_softc *sc = PICTOSOFTC(pic);
194 1.2.6.2 tls uint8_t b;
195 1.2.6.2 tls
196 1.2.6.2 tls for (;;) {
197 1.2.6.2 tls b = ffs(irq_mask);
198 1.2.6.2 tls if (b == 0) break;
199 1.2.6.2 tls b--; /* Zero based index. */
200 1.2.6.2 tls ICOLL_SET_IRQ(sc, irq_base + b);
201 1.2.6.2 tls irq_mask &= ~(1<<b);
202 1.2.6.2 tls }
203 1.2.6.2 tls
204 1.2.6.2 tls return;
205 1.2.6.2 tls }
206 1.2.6.2 tls
207 1.2.6.2 tls static void
208 1.2.6.2 tls icoll_block_irqs(struct pic_softc *pic, size_t irq_base, uint32_t irq_mask)
209 1.2.6.2 tls {
210 1.2.6.2 tls struct icoll_softc *sc = PICTOSOFTC(pic);
211 1.2.6.2 tls uint8_t b;
212 1.2.6.2 tls
213 1.2.6.2 tls for (;;) {
214 1.2.6.2 tls b = ffs(irq_mask);
215 1.2.6.2 tls if (b == 0) break;
216 1.2.6.2 tls b--; /* Zero based index. */
217 1.2.6.2 tls ICOLL_CLR_IRQ(sc, irq_base + b);
218 1.2.6.2 tls irq_mask &= ~(1<<b);
219 1.2.6.2 tls }
220 1.2.6.2 tls
221 1.2.6.2 tls return;
222 1.2.6.2 tls }
223 1.2.6.2 tls
224 1.2.6.2 tls static int
225 1.2.6.2 tls icoll_find_pending_irqs(struct pic_softc *pic)
226 1.2.6.2 tls {
227 1.2.6.2 tls return 0; /* ICOLL HW doesn't provide list of pending interrupts. */
228 1.2.6.2 tls }
229 1.2.6.2 tls
230 1.2.6.2 tls static void
231 1.2.6.2 tls icoll_establish_irq(struct pic_softc *pic, struct intrsource *is)
232 1.2.6.2 tls {
233 1.2.6.2 tls return; /* Nothing to establish. */
234 1.2.6.2 tls }
235 1.2.6.2 tls
236 1.2.6.2 tls static void
237 1.2.6.2 tls icoll_source_name(struct pic_softc *pic, int irq, char *is_source, size_t size)
238 1.2.6.2 tls {
239 1.2.6.2 tls snprintf(is_source, size, "irq %d", irq);
240 1.2.6.2 tls }
241 1.2.6.2 tls
242 1.2.6.2 tls /*
243 1.2.6.2 tls * Set new interrupt priority level by enabling or disabling IRQ's.
244 1.2.6.2 tls */
245 1.2.6.2 tls static void
246 1.2.6.2 tls icoll_set_priority(struct pic_softc *pic, int newipl)
247 1.2.6.2 tls {
248 1.2.6.2 tls struct icoll_softc *sc = PICTOSOFTC(pic);
249 1.2.6.2 tls struct intrsource *is;
250 1.2.6.2 tls int i;
251 1.2.6.2 tls
252 1.2.6.2 tls for (i = 0; i < pic->pic_maxsources; i++) {
253 1.2.6.2 tls is = pic->pic_sources[i];
254 1.2.6.2 tls if (is == NULL)
255 1.2.6.2 tls continue;
256 1.2.6.2 tls if (is->is_ipl > newipl)
257 1.2.6.2 tls ICOLL_SET_IRQ(sc, pic->pic_irqbase + is->is_irq);
258 1.2.6.2 tls else
259 1.2.6.2 tls ICOLL_CLR_IRQ(sc, pic->pic_irqbase + is->is_irq);
260 1.2.6.2 tls }
261 1.2.6.2 tls }
262 1.2.6.2 tls
263 1.2.6.2 tls /*
264 1.2.6.2 tls * autoconf(9) callbacks.
265 1.2.6.2 tls */
266 1.2.6.2 tls static int
267 1.2.6.2 tls icoll_match(device_t parent, cfdata_t match, void *aux)
268 1.2.6.2 tls {
269 1.2.6.2 tls struct apb_attach_args *aa = aux;
270 1.2.6.2 tls
271 1.2.6.2 tls if ((aa->aa_addr == HW_ICOLL_BASE) && (aa->aa_size == HW_ICOLL_SIZE))
272 1.2.6.2 tls return 1;
273 1.2.6.2 tls
274 1.2.6.2 tls return 0;
275 1.2.6.2 tls }
276 1.2.6.2 tls
277 1.2.6.2 tls static void
278 1.2.6.2 tls icoll_attach(device_t parent, device_t self, void *aux)
279 1.2.6.2 tls {
280 1.2.6.2 tls static int icoll_attached = 0;
281 1.2.6.2 tls struct icoll_softc *sc = device_private(self);
282 1.2.6.2 tls struct apb_attach_args *aa = aux;
283 1.2.6.2 tls
284 1.2.6.2 tls if (icoll_attached)
285 1.2.6.2 tls return;
286 1.2.6.2 tls
287 1.2.6.2 tls icoll_sc = sc;
288 1.2.6.2 tls
289 1.2.6.2 tls sc->sc_dev = self;
290 1.2.6.2 tls sc->sc_iot = aa->aa_iot;
291 1.2.6.2 tls
292 1.2.6.2 tls sc->sc_pic.pic_maxsources = IRQ_LAST + 1;
293 1.2.6.2 tls sc->sc_pic.pic_ops = &icoll_pic_ops;
294 1.2.6.2 tls strlcpy(sc->sc_pic.pic_name, device_xname(self),
295 1.2.6.2 tls sizeof(sc->sc_pic.pic_name));
296 1.2.6.2 tls
297 1.2.6.2 tls if (bus_space_map(sc->sc_iot,
298 1.2.6.2 tls aa->aa_addr, aa->aa_size, 0, &(sc->sc_hdl))) {
299 1.2.6.2 tls aprint_error_dev(sc->sc_dev, "unable to map bus space\n");
300 1.2.6.2 tls return;
301 1.2.6.2 tls }
302 1.2.6.2 tls
303 1.2.6.2 tls icoll_reset(sc);
304 1.2.6.2 tls pic_add(&sc->sc_pic, 0);
305 1.2.6.2 tls aprint_normal("\n");
306 1.2.6.2 tls icoll_attached = 1;
307 1.2.6.2 tls
308 1.2.6.2 tls return;
309 1.2.6.2 tls }
310 1.2.6.2 tls
311 1.2.6.2 tls static int
312 1.2.6.2 tls icoll_activate(device_t self, enum devact act)
313 1.2.6.2 tls {
314 1.2.6.2 tls return EOPNOTSUPP;
315 1.2.6.2 tls }
316 1.2.6.2 tls
317 1.2.6.2 tls /*
318 1.2.6.2 tls * Reset the ICOLL block.
319 1.2.6.2 tls *
320 1.2.6.2 tls * Inspired by i.MX23 RM "39.3.10 Correct Way to Soft Reset a Block"
321 1.2.6.2 tls */
322 1.2.6.2 tls static void
323 1.2.6.2 tls icoll_reset(struct icoll_softc *sc)
324 1.2.6.2 tls {
325 1.2.6.2 tls unsigned int loop;
326 1.2.6.2 tls
327 1.2.6.2 tls /*
328 1.2.6.2 tls * Prepare for soft-reset by making sure that SFTRST is not currently
329 1.2.6.2 tls * asserted. Also clear CLKGATE so we can wait for its assertion below.
330 1.2.6.2 tls */
331 1.2.6.2 tls ICOLL_WRITE(sc, HW_ICOLL_CTRL_CLR, HW_ICOLL_CTRL_SFTRST);
332 1.2.6.2 tls
333 1.2.6.2 tls /* Wait at least a microsecond for SFTRST to deassert. */
334 1.2.6.2 tls loop = 0;
335 1.2.6.2 tls while ((ICOLL_READ(sc, HW_ICOLL_CTRL) & HW_ICOLL_CTRL_SFTRST) ||
336 1.2.6.2 tls (loop < ICOLL_SOFT_RST_LOOP)) {
337 1.2.6.2 tls loop++;
338 1.2.6.2 tls }
339 1.2.6.2 tls
340 1.2.6.2 tls /* Clear CLKGATE so we can wait for its assertion below. */
341 1.2.6.2 tls ICOLL_WRITE(sc, HW_ICOLL_CTRL_CLR, HW_ICOLL_CTRL_CLKGATE);
342 1.2.6.2 tls
343 1.2.6.2 tls /* Soft-reset the block. */
344 1.2.6.2 tls ICOLL_WRITE(sc, HW_ICOLL_CTRL_SET, HW_ICOLL_CTRL_SFTRST);
345 1.2.6.2 tls
346 1.2.6.2 tls /* Wait until clock is in the gated state. */
347 1.2.6.2 tls while (!(ICOLL_READ(sc, HW_ICOLL_CTRL) & HW_ICOLL_CTRL_CLKGATE));
348 1.2.6.2 tls
349 1.2.6.2 tls /* Bring block out of reset. */
350 1.2.6.2 tls ICOLL_WRITE(sc, HW_ICOLL_CTRL_CLR, HW_ICOLL_CTRL_SFTRST);
351 1.2.6.2 tls
352 1.2.6.2 tls loop = 0;
353 1.2.6.2 tls while ((ICOLL_READ(sc, HW_ICOLL_CTRL) & HW_ICOLL_CTRL_SFTRST) ||
354 1.2.6.2 tls (loop < ICOLL_SOFT_RST_LOOP)) {
355 1.2.6.2 tls loop++;
356 1.2.6.2 tls }
357 1.2.6.2 tls
358 1.2.6.2 tls ICOLL_WRITE(sc, HW_ICOLL_CTRL_CLR, HW_ICOLL_CTRL_CLKGATE);
359 1.2.6.2 tls
360 1.2.6.2 tls /* Wait until clock is in the NON-gated state. */
361 1.2.6.2 tls while (ICOLL_READ(sc, HW_ICOLL_CTRL) & HW_ICOLL_CTRL_CLKGATE);
362 1.2.6.2 tls
363 1.2.6.2 tls return;
364 1.2.6.2 tls }
365