imx23_icoll.c revision 1.2.6.3 1 1.2.6.2 tls /* $Id: imx23_icoll.c,v 1.2.6.3 2014/08/20 00:02:46 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.3 tls #include <arm/cpufunc.h>
40 1.2.6.3 tls
41 1.2.6.2 tls #define _INTR_PRIVATE
42 1.2.6.2 tls #include <arm/pic/picvar.h>
43 1.2.6.2 tls
44 1.2.6.2 tls #include <arm/imx/imx23_icollreg.h>
45 1.2.6.2 tls #include <arm/imx/imx23var.h>
46 1.2.6.2 tls
47 1.2.6.2 tls #define ICOLL_SOFT_RST_LOOP 455 /* At least 1 us ... */
48 1.2.6.2 tls #define ICOLL_READ(sc, reg) \
49 1.2.6.2 tls bus_space_read_4(sc->sc_iot, sc->sc_hdl, (reg))
50 1.2.6.2 tls #define ICOLL_WRITE(sc, reg, val) \
51 1.2.6.2 tls bus_space_write_4(sc->sc_iot, sc->sc_hdl, (reg), (val))
52 1.2.6.2 tls
53 1.2.6.2 tls #define ICOLL_IRQ_REG_SIZE 0x10
54 1.2.6.2 tls #define ICOLL_CLR_IRQ(sc, irq) \
55 1.2.6.2 tls ICOLL_WRITE(sc, HW_ICOLL_INTERRUPT0_CLR + \
56 1.2.6.2 tls (irq) * ICOLL_IRQ_REG_SIZE, \
57 1.2.6.2 tls HW_ICOLL_INTERRUPT_ENABLE)
58 1.2.6.2 tls #define ICOLL_SET_IRQ(sc, irq) \
59 1.2.6.2 tls ICOLL_WRITE(sc, HW_ICOLL_INTERRUPT0_SET + \
60 1.2.6.2 tls (irq) * ICOLL_IRQ_REG_SIZE, \
61 1.2.6.2 tls HW_ICOLL_INTERRUPT_ENABLE)
62 1.2.6.2 tls #define ICOLL_GET_PRIO(sc, irq) \
63 1.2.6.2 tls __SHIFTOUT(ICOLL_READ(sc, HW_ICOLL_INTERRUPT0 + \
64 1.2.6.2 tls (irq) * ICOLL_IRQ_REG_SIZE), \
65 1.2.6.2 tls HW_ICOLL_INTERRUPT_PRIORITY)
66 1.2.6.2 tls
67 1.2.6.2 tls #define PICTOSOFTC(pic) \
68 1.2.6.2 tls ((struct icoll_softc *)((char *)(pic) - \
69 1.2.6.2 tls offsetof(struct icoll_softc, sc_pic)))
70 1.2.6.2 tls
71 1.2.6.2 tls /*
72 1.2.6.2 tls * pic callbacks.
73 1.2.6.2 tls */
74 1.2.6.2 tls static void icoll_unblock_irqs(struct pic_softc *, size_t, uint32_t);
75 1.2.6.2 tls static void icoll_block_irqs(struct pic_softc *, size_t, uint32_t);
76 1.2.6.2 tls static int icoll_find_pending_irqs(struct pic_softc *);
77 1.2.6.2 tls static void icoll_establish_irq(struct pic_softc *, struct intrsource *);
78 1.2.6.2 tls static void icoll_source_name(struct pic_softc *, int, char *, size_t);
79 1.2.6.2 tls static void icoll_set_priority(struct pic_softc *, int);
80 1.2.6.2 tls
81 1.2.6.2 tls /*
82 1.2.6.2 tls * autoconf(9) callbacks.
83 1.2.6.2 tls */
84 1.2.6.2 tls static int icoll_match(device_t, cfdata_t, void *);
85 1.2.6.2 tls static void icoll_attach(device_t, device_t, void *);
86 1.2.6.2 tls static int icoll_activate(device_t, enum devact);
87 1.2.6.2 tls
88 1.2.6.2 tls /*
89 1.2.6.2 tls * ARM interrupt handler.
90 1.2.6.2 tls */
91 1.2.6.2 tls void imx23_intr_dispatch(struct clockframe *);
92 1.2.6.2 tls
93 1.2.6.2 tls const static struct pic_ops icoll_pic_ops = {
94 1.2.6.2 tls .pic_unblock_irqs = icoll_unblock_irqs,
95 1.2.6.2 tls .pic_block_irqs = icoll_block_irqs,
96 1.2.6.2 tls .pic_find_pending_irqs = icoll_find_pending_irqs,
97 1.2.6.2 tls .pic_establish_irq = icoll_establish_irq,
98 1.2.6.2 tls .pic_source_name = icoll_source_name,
99 1.2.6.2 tls .pic_set_priority = icoll_set_priority
100 1.2.6.2 tls };
101 1.2.6.2 tls
102 1.2.6.2 tls struct icoll_softc {
103 1.2.6.2 tls device_t sc_dev;
104 1.2.6.2 tls struct pic_softc sc_pic;
105 1.2.6.2 tls bus_space_tag_t sc_iot;
106 1.2.6.2 tls bus_space_handle_t sc_hdl;
107 1.2.6.2 tls };
108 1.2.6.2 tls
109 1.2.6.2 tls /* For IRQ handler. */
110 1.2.6.2 tls static struct icoll_softc *icoll_sc;
111 1.2.6.2 tls
112 1.2.6.2 tls /*
113 1.2.6.2 tls * Private to driver.
114 1.2.6.2 tls */
115 1.2.6.2 tls static void icoll_reset(struct icoll_softc *);
116 1.2.6.2 tls
117 1.2.6.2 tls CFATTACH_DECL3_NEW(icoll,
118 1.2.6.2 tls sizeof(struct icoll_softc),
119 1.2.6.2 tls icoll_match,
120 1.2.6.2 tls icoll_attach,
121 1.2.6.2 tls NULL,
122 1.2.6.2 tls icoll_activate,
123 1.2.6.2 tls NULL,
124 1.2.6.2 tls NULL,
125 1.2.6.2 tls 0);
126 1.2.6.2 tls
127 1.2.6.2 tls /*
128 1.2.6.2 tls * ARM interrupt handler.
129 1.2.6.2 tls */
130 1.2.6.2 tls void
131 1.2.6.2 tls imx23_intr_dispatch(struct clockframe *frame)
132 1.2.6.2 tls {
133 1.2.6.2 tls struct cpu_info * const ci = curcpu();
134 1.2.6.2 tls struct pic_softc *pic_sc;
135 1.2.6.2 tls int saved_spl;
136 1.2.6.2 tls uint8_t irq;
137 1.2.6.2 tls uint8_t prio;
138 1.2.6.2 tls
139 1.2.6.2 tls pic_sc = &icoll_sc->sc_pic;
140 1.2.6.2 tls
141 1.2.6.2 tls ci->ci_data.cpu_nintr++;
142 1.2.6.2 tls
143 1.2.6.2 tls /* Save current spl. */
144 1.2.6.2 tls saved_spl = curcpl();
145 1.2.6.2 tls
146 1.2.6.2 tls /* IRQ to be handled. */
147 1.2.6.2 tls irq = __SHIFTOUT(ICOLL_READ(icoll_sc, HW_ICOLL_STAT),
148 1.2.6.2 tls HW_ICOLL_STAT_VECTOR_NUMBER);
149 1.2.6.2 tls
150 1.2.6.2 tls /* Save IRQ's priority. Acknowledge it later. */
151 1.2.6.2 tls prio = ICOLL_GET_PRIO(icoll_sc, irq);
152 1.2.6.2 tls
153 1.2.6.2 tls /*
154 1.2.6.2 tls * Notify ICOLL to deassert IRQ before re-enabling the IRQ's.
155 1.2.6.2 tls * This is done by writing anything to HW_ICOLL_VECTOR.
156 1.2.6.2 tls */
157 1.2.6.2 tls ICOLL_WRITE(icoll_sc, HW_ICOLL_VECTOR,
158 1.2.6.2 tls __SHIFTIN(0x3fffffff, HW_ICOLL_VECTOR_IRQVECTOR));
159 1.2.6.2 tls
160 1.2.6.2 tls /* Bogus IRQ. */
161 1.2.6.2 tls if (irq == 0x7f) {
162 1.2.6.2 tls cpsie(I32_bit);
163 1.2.6.2 tls ICOLL_WRITE(icoll_sc, HW_ICOLL_LEVELACK, (1<<prio));
164 1.2.6.2 tls cpsid(I32_bit);
165 1.2.6.2 tls return;
166 1.2.6.2 tls }
167 1.2.6.2 tls
168 1.2.6.2 tls /* Raise the spl to the level of the IRQ. */
169 1.2.6.2 tls if (pic_sc->pic_sources[irq]->is_ipl > ci->ci_cpl)
170 1.2.6.2 tls saved_spl = _splraise(pic_sc->pic_sources[irq]->is_ipl);
171 1.2.6.2 tls
172 1.2.6.2 tls /* Call the handler registered for the IRQ. */
173 1.2.6.2 tls cpsie(I32_bit);
174 1.2.6.2 tls pic_dispatch(pic_sc->pic_sources[irq], frame);
175 1.2.6.2 tls
176 1.2.6.2 tls /*
177 1.2.6.2 tls * Acknowledge the IRQ by writing its priority to HW_ICOLL_LEVELACK.
178 1.2.6.2 tls * Interrupts should be enabled.
179 1.2.6.2 tls */
180 1.2.6.2 tls ICOLL_WRITE(icoll_sc, HW_ICOLL_LEVELACK, (1<<prio));
181 1.2.6.2 tls cpsid(I32_bit);
182 1.2.6.2 tls
183 1.2.6.2 tls /* Restore the saved spl. */
184 1.2.6.2 tls splx(saved_spl);
185 1.2.6.2 tls
186 1.2.6.2 tls return;
187 1.2.6.2 tls }
188 1.2.6.2 tls
189 1.2.6.2 tls /*
190 1.2.6.2 tls * pic callbacks.
191 1.2.6.2 tls */
192 1.2.6.2 tls static void
193 1.2.6.2 tls icoll_unblock_irqs(struct pic_softc *pic, size_t irq_base, uint32_t irq_mask)
194 1.2.6.2 tls {
195 1.2.6.2 tls struct icoll_softc *sc = PICTOSOFTC(pic);
196 1.2.6.2 tls uint8_t b;
197 1.2.6.2 tls
198 1.2.6.2 tls for (;;) {
199 1.2.6.2 tls b = ffs(irq_mask);
200 1.2.6.2 tls if (b == 0) break;
201 1.2.6.2 tls b--; /* Zero based index. */
202 1.2.6.2 tls ICOLL_SET_IRQ(sc, irq_base + b);
203 1.2.6.2 tls irq_mask &= ~(1<<b);
204 1.2.6.2 tls }
205 1.2.6.2 tls
206 1.2.6.2 tls return;
207 1.2.6.2 tls }
208 1.2.6.2 tls
209 1.2.6.2 tls static void
210 1.2.6.2 tls icoll_block_irqs(struct pic_softc *pic, size_t irq_base, uint32_t irq_mask)
211 1.2.6.2 tls {
212 1.2.6.2 tls struct icoll_softc *sc = PICTOSOFTC(pic);
213 1.2.6.2 tls uint8_t b;
214 1.2.6.2 tls
215 1.2.6.2 tls for (;;) {
216 1.2.6.2 tls b = ffs(irq_mask);
217 1.2.6.2 tls if (b == 0) break;
218 1.2.6.2 tls b--; /* Zero based index. */
219 1.2.6.2 tls ICOLL_CLR_IRQ(sc, irq_base + b);
220 1.2.6.2 tls irq_mask &= ~(1<<b);
221 1.2.6.2 tls }
222 1.2.6.2 tls
223 1.2.6.2 tls return;
224 1.2.6.2 tls }
225 1.2.6.2 tls
226 1.2.6.2 tls static int
227 1.2.6.2 tls icoll_find_pending_irqs(struct pic_softc *pic)
228 1.2.6.2 tls {
229 1.2.6.2 tls return 0; /* ICOLL HW doesn't provide list of pending interrupts. */
230 1.2.6.2 tls }
231 1.2.6.2 tls
232 1.2.6.2 tls static void
233 1.2.6.2 tls icoll_establish_irq(struct pic_softc *pic, struct intrsource *is)
234 1.2.6.2 tls {
235 1.2.6.2 tls return; /* Nothing to establish. */
236 1.2.6.2 tls }
237 1.2.6.2 tls
238 1.2.6.2 tls static void
239 1.2.6.2 tls icoll_source_name(struct pic_softc *pic, int irq, char *is_source, size_t size)
240 1.2.6.2 tls {
241 1.2.6.2 tls snprintf(is_source, size, "irq %d", irq);
242 1.2.6.2 tls }
243 1.2.6.2 tls
244 1.2.6.2 tls /*
245 1.2.6.2 tls * Set new interrupt priority level by enabling or disabling IRQ's.
246 1.2.6.2 tls */
247 1.2.6.2 tls static void
248 1.2.6.2 tls icoll_set_priority(struct pic_softc *pic, int newipl)
249 1.2.6.2 tls {
250 1.2.6.2 tls struct icoll_softc *sc = PICTOSOFTC(pic);
251 1.2.6.2 tls struct intrsource *is;
252 1.2.6.2 tls int i;
253 1.2.6.2 tls
254 1.2.6.2 tls for (i = 0; i < pic->pic_maxsources; i++) {
255 1.2.6.2 tls is = pic->pic_sources[i];
256 1.2.6.2 tls if (is == NULL)
257 1.2.6.2 tls continue;
258 1.2.6.2 tls if (is->is_ipl > newipl)
259 1.2.6.2 tls ICOLL_SET_IRQ(sc, pic->pic_irqbase + is->is_irq);
260 1.2.6.2 tls else
261 1.2.6.2 tls ICOLL_CLR_IRQ(sc, pic->pic_irqbase + is->is_irq);
262 1.2.6.2 tls }
263 1.2.6.2 tls }
264 1.2.6.2 tls
265 1.2.6.2 tls /*
266 1.2.6.2 tls * autoconf(9) callbacks.
267 1.2.6.2 tls */
268 1.2.6.2 tls static int
269 1.2.6.2 tls icoll_match(device_t parent, cfdata_t match, void *aux)
270 1.2.6.2 tls {
271 1.2.6.2 tls struct apb_attach_args *aa = aux;
272 1.2.6.2 tls
273 1.2.6.2 tls if ((aa->aa_addr == HW_ICOLL_BASE) && (aa->aa_size == HW_ICOLL_SIZE))
274 1.2.6.2 tls return 1;
275 1.2.6.2 tls
276 1.2.6.2 tls return 0;
277 1.2.6.2 tls }
278 1.2.6.2 tls
279 1.2.6.2 tls static void
280 1.2.6.2 tls icoll_attach(device_t parent, device_t self, void *aux)
281 1.2.6.2 tls {
282 1.2.6.2 tls static int icoll_attached = 0;
283 1.2.6.2 tls struct icoll_softc *sc = device_private(self);
284 1.2.6.2 tls struct apb_attach_args *aa = aux;
285 1.2.6.2 tls
286 1.2.6.2 tls if (icoll_attached)
287 1.2.6.2 tls return;
288 1.2.6.2 tls
289 1.2.6.2 tls icoll_sc = sc;
290 1.2.6.2 tls
291 1.2.6.2 tls sc->sc_dev = self;
292 1.2.6.2 tls sc->sc_iot = aa->aa_iot;
293 1.2.6.2 tls
294 1.2.6.2 tls sc->sc_pic.pic_maxsources = IRQ_LAST + 1;
295 1.2.6.2 tls sc->sc_pic.pic_ops = &icoll_pic_ops;
296 1.2.6.2 tls strlcpy(sc->sc_pic.pic_name, device_xname(self),
297 1.2.6.2 tls sizeof(sc->sc_pic.pic_name));
298 1.2.6.2 tls
299 1.2.6.2 tls if (bus_space_map(sc->sc_iot,
300 1.2.6.2 tls aa->aa_addr, aa->aa_size, 0, &(sc->sc_hdl))) {
301 1.2.6.2 tls aprint_error_dev(sc->sc_dev, "unable to map bus space\n");
302 1.2.6.2 tls return;
303 1.2.6.2 tls }
304 1.2.6.2 tls
305 1.2.6.2 tls icoll_reset(sc);
306 1.2.6.2 tls pic_add(&sc->sc_pic, 0);
307 1.2.6.2 tls aprint_normal("\n");
308 1.2.6.2 tls icoll_attached = 1;
309 1.2.6.2 tls
310 1.2.6.2 tls return;
311 1.2.6.2 tls }
312 1.2.6.2 tls
313 1.2.6.2 tls static int
314 1.2.6.2 tls icoll_activate(device_t self, enum devact act)
315 1.2.6.2 tls {
316 1.2.6.2 tls return EOPNOTSUPP;
317 1.2.6.2 tls }
318 1.2.6.2 tls
319 1.2.6.2 tls /*
320 1.2.6.2 tls * Reset the ICOLL block.
321 1.2.6.2 tls *
322 1.2.6.2 tls * Inspired by i.MX23 RM "39.3.10 Correct Way to Soft Reset a Block"
323 1.2.6.2 tls */
324 1.2.6.2 tls static void
325 1.2.6.2 tls icoll_reset(struct icoll_softc *sc)
326 1.2.6.2 tls {
327 1.2.6.2 tls unsigned int loop;
328 1.2.6.2 tls
329 1.2.6.2 tls /*
330 1.2.6.2 tls * Prepare for soft-reset by making sure that SFTRST is not currently
331 1.2.6.2 tls * asserted. Also clear CLKGATE so we can wait for its assertion below.
332 1.2.6.2 tls */
333 1.2.6.2 tls ICOLL_WRITE(sc, HW_ICOLL_CTRL_CLR, HW_ICOLL_CTRL_SFTRST);
334 1.2.6.2 tls
335 1.2.6.2 tls /* Wait at least a microsecond for SFTRST to deassert. */
336 1.2.6.2 tls loop = 0;
337 1.2.6.2 tls while ((ICOLL_READ(sc, HW_ICOLL_CTRL) & HW_ICOLL_CTRL_SFTRST) ||
338 1.2.6.2 tls (loop < ICOLL_SOFT_RST_LOOP)) {
339 1.2.6.2 tls loop++;
340 1.2.6.2 tls }
341 1.2.6.2 tls
342 1.2.6.2 tls /* Clear CLKGATE so we can wait for its assertion below. */
343 1.2.6.2 tls ICOLL_WRITE(sc, HW_ICOLL_CTRL_CLR, HW_ICOLL_CTRL_CLKGATE);
344 1.2.6.2 tls
345 1.2.6.2 tls /* Soft-reset the block. */
346 1.2.6.2 tls ICOLL_WRITE(sc, HW_ICOLL_CTRL_SET, HW_ICOLL_CTRL_SFTRST);
347 1.2.6.2 tls
348 1.2.6.2 tls /* Wait until clock is in the gated state. */
349 1.2.6.2 tls while (!(ICOLL_READ(sc, HW_ICOLL_CTRL) & HW_ICOLL_CTRL_CLKGATE));
350 1.2.6.2 tls
351 1.2.6.2 tls /* Bring block out of reset. */
352 1.2.6.2 tls ICOLL_WRITE(sc, HW_ICOLL_CTRL_CLR, HW_ICOLL_CTRL_SFTRST);
353 1.2.6.2 tls
354 1.2.6.2 tls loop = 0;
355 1.2.6.2 tls while ((ICOLL_READ(sc, HW_ICOLL_CTRL) & HW_ICOLL_CTRL_SFTRST) ||
356 1.2.6.2 tls (loop < ICOLL_SOFT_RST_LOOP)) {
357 1.2.6.2 tls loop++;
358 1.2.6.2 tls }
359 1.2.6.2 tls
360 1.2.6.2 tls ICOLL_WRITE(sc, HW_ICOLL_CTRL_CLR, HW_ICOLL_CTRL_CLKGATE);
361 1.2.6.2 tls
362 1.2.6.2 tls /* Wait until clock is in the NON-gated state. */
363 1.2.6.2 tls while (ICOLL_READ(sc, HW_ICOLL_CTRL) & HW_ICOLL_CTRL_CLKGATE);
364 1.2.6.2 tls
365 1.2.6.2 tls return;
366 1.2.6.2 tls }
367