]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - sys/mips/idt/obio.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / sys / mips / idt / obio.c
1 /*-
2  * Copyright (c) 2007, Oleksandr Tymoshenko <gonzo@freebsd.org>
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  * 3. All advertising materials mentioning features or use of this software
13  *    must display the following acknowledgement:
14  *      This product includes software developed for the NetBSD Project by
15  *      Wasabi Systems, Inc.
16  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
17  *    or promote products derived from this software without specific prior
18  *    written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/bus.h>
39 #include <sys/interrupt.h>
40 #include <sys/kernel.h>
41 #include <sys/module.h>
42 #include <sys/rman.h>
43 #include <sys/malloc.h>
44
45 #include <machine/bus.h>
46
47 #include <mips/idt/idtreg.h>
48 #include <mips/idt/obiovar.h>
49
50 #define ICU_REG_READ(o) \
51     *((volatile uint32_t *)MIPS_PHYS_TO_KSEG1(IDT_BASE_ICU + (o)))
52 #define ICU_REG_WRITE(o,v) (ICU_REG_READ(o)) = (v)
53
54 #define GPIO_REG_READ(o) \
55     *((volatile uint32_t *)MIPS_PHYS_TO_KSEG1(IDT_BASE_GPIO + (o)))
56 #define GPIO_REG_WRITE(o,v) (GPIO_REG_READ(o)) = (v)
57
58 static int      obio_activate_resource(device_t, device_t, int, int,
59                     struct resource *);
60 static device_t obio_add_child(device_t, int, const char *, int);
61 static struct resource *
62                 obio_alloc_resource(device_t, device_t, int, int *, u_long,
63                     u_long, u_long, u_int);
64 static int      obio_attach(device_t);
65 static int      obio_deactivate_resource(device_t, device_t, int, int,
66                     struct resource *);
67 static struct resource_list *
68                 obio_get_resource_list(device_t, device_t);
69 static void     obio_hinted_child(device_t, const char *, int);
70 static int      obio_intr(void *);
71 static int      obio_probe(device_t);
72 static int      obio_release_resource(device_t, device_t, int, int,
73                     struct resource *);
74 static int      obio_setup_intr(device_t, device_t, struct resource *, int,
75                     driver_filter_t *, driver_intr_t *, void *, void **);
76 static int      obio_teardown_intr(device_t, device_t, struct resource *,
77                     void *);
78
79 static void obio_mask_irq(unsigned int irq)
80 {
81         int ip_bit, mask, mask_register;
82
83         /* mask IRQ */
84         mask_register = ICU_IRQ_MASK_REG(irq);
85         ip_bit = ICU_IP_BIT(irq);
86
87         mask = ICU_REG_READ(mask_register);
88         ICU_REG_WRITE(mask_register, mask | ip_bit);
89 }
90
91 static void obio_unmask_irq(unsigned int irq)
92 {
93         int ip_bit, mask, mask_register;
94
95         /* unmask IRQ */
96         mask_register = ICU_IRQ_MASK_REG(irq);
97         ip_bit = ICU_IP_BIT(irq);
98
99         mask = ICU_REG_READ(mask_register);
100         ICU_REG_WRITE(mask_register, mask & ~ip_bit);
101 }
102
103 static int
104 obio_probe(device_t dev)
105 {
106
107         return (0);
108 }
109
110 static int
111 obio_attach(device_t dev)
112 {
113         struct obio_softc *sc = device_get_softc(dev);
114         int rid, irq;
115
116         sc->oba_mem_rman.rm_type = RMAN_ARRAY;
117         sc->oba_mem_rman.rm_descr = "OBIO memeory";
118         if (rman_init(&sc->oba_mem_rman) != 0 ||
119             rman_manage_region(&sc->oba_mem_rman, OBIO_MEM_START,
120                 OBIO_MEM_START + OBIO_MEM_SIZE) != 0)
121                 panic("obio_attach: failed to set up I/O rman");
122
123         sc->oba_irq_rman.rm_type = RMAN_ARRAY;
124         sc->oba_irq_rman.rm_descr = "OBIO IRQ";
125
126         if (rman_init(&sc->oba_irq_rman) != 0 ||
127             rman_manage_region(&sc->oba_irq_rman, IRQ_BASE, IRQ_END) != 0)
128                 panic("obio_attach: failed to set up IRQ rman");
129
130         /* Hook up our interrupt handlers. We should handle IRQ0..IRQ4*/
131         for(irq = 0; irq < 5; irq++) {
132                 if ((sc->sc_irq[irq] = bus_alloc_resource(dev, SYS_RES_IRQ, 
133                     &rid, irq, irq, 1, RF_SHAREABLE | RF_ACTIVE)) == NULL) {
134                         device_printf(dev, "unable to allocate IRQ resource\n");
135                         return (ENXIO);
136                 }
137
138                 if ((bus_setup_intr(dev, sc->sc_irq[irq], INTR_TYPE_MISC, 
139                     obio_intr, NULL, sc, &sc->sc_ih[irq]))) {
140                         device_printf(dev,
141                             "WARNING: unable to register interrupt handler\n");
142                         return (ENXIO);
143                 }
144         }
145
146         bus_generic_probe(dev);
147         bus_enumerate_hinted_children(dev);
148         bus_generic_attach(dev);
149
150         return (0);
151 }
152
153 static struct resource *
154 obio_alloc_resource(device_t bus, device_t child, int type, int *rid,
155     u_long start, u_long end, u_long count, u_int flags)
156 {
157         struct obio_softc               *sc = device_get_softc(bus);
158         struct obio_ivar                *ivar = device_get_ivars(child);
159         struct resource                 *rv;
160         struct resource_list_entry      *rle;
161         struct rman                     *rm;
162         int                              isdefault, needactivate, passthrough;
163
164         isdefault = (start == 0UL && end == ~0UL);
165         needactivate = flags & RF_ACTIVE;
166         passthrough = (device_get_parent(child) != bus);
167         rle = NULL;
168
169         if (passthrough)
170                 return (BUS_ALLOC_RESOURCE(device_get_parent(bus), child, type,
171                     rid, start, end, count, flags));
172
173         /*
174          * If this is an allocation of the "default" range for a given RID,
175          * and we know what the resources for this device are (ie. they aren't
176          * maintained by a child bus), then work out the start/end values.
177          */
178         if (isdefault) {
179                 rle = resource_list_find(&ivar->resources, type, *rid);
180                 if (rle == NULL)
181                         return (NULL);
182                 if (rle->res != NULL) {
183                         panic("%s: resource entry is busy", __func__);
184                 }
185                 start = rle->start;
186                 end = rle->end;
187                 count = rle->count;
188         }
189
190         switch (type) {
191         case SYS_RES_IRQ:
192                 rm = &sc->oba_irq_rman;
193                 break;
194         case SYS_RES_MEMORY:
195                 rm = &sc->oba_mem_rman;
196                 break;
197         default:
198                 printf("%s: unknown resource type %d\n", __func__, type);
199                 return (0);
200         }
201
202         rv = rman_reserve_resource(rm, start, end, count, flags, child);
203         if (rv == 0) {
204                 printf("%s: could not reserve resource\n", __func__);
205                 return (0);
206         }
207
208         rman_set_rid(rv, *rid);
209
210         if (needactivate) {
211                 if (bus_activate_resource(child, type, *rid, rv)) {
212                         printf("%s: could not activate resource\n", __func__);
213                         rman_release_resource(rv);
214                         return (0);
215                 }
216         }
217
218         return (rv);
219 }
220
221 static int
222 obio_activate_resource(device_t bus, device_t child, int type, int rid,
223     struct resource *r)
224 {
225
226         /* XXX: should we mask/unmask IRQ here? */
227         return (BUS_ACTIVATE_RESOURCE(device_get_parent(bus), child,
228                 type, rid, r));
229 }
230
231 static int
232 obio_deactivate_resource(device_t bus, device_t child, int type, int rid,
233     struct resource *r)
234 {
235
236         /* XXX: should we mask/unmask IRQ here? */
237         return (BUS_DEACTIVATE_RESOURCE(device_get_parent(bus), child,
238                 type, rid, r));
239 }
240
241 static int
242 obio_release_resource(device_t dev, device_t child, int type,
243     int rid, struct resource *r)
244 {
245         struct resource_list *rl;
246         struct resource_list_entry *rle;
247
248         rl = obio_get_resource_list(dev, child);
249         if (rl == NULL)
250                 return (EINVAL);
251         rle = resource_list_find(rl, type, rid);
252         if (rle == NULL)
253                 return (EINVAL);
254         rman_release_resource(r);
255         rle->res = NULL;
256
257         return (0);
258 }
259
260 static int
261 obio_setup_intr(device_t dev, device_t child, struct resource *ires,
262                 int flags, driver_filter_t *filt, driver_intr_t *handler,
263                 void *arg, void **cookiep)
264 {
265         struct obio_softc *sc = device_get_softc(dev);
266         struct intr_event *event;
267         int irq, ip_bit, error, mask, mask_register;
268
269         irq = rman_get_start(ires);
270
271         if (irq >= NIRQS)
272                 panic("%s: bad irq %d", __func__, irq);
273
274         event = sc->sc_eventstab[irq];
275         if (event == NULL) {
276                 error = intr_event_create(&event, (void *)irq, 0, irq, 
277                     (mask_fn)obio_mask_irq, (mask_fn)obio_unmask_irq,
278                     NULL, NULL,
279                     "obio intr%d:", irq);
280
281                 sc->sc_eventstab[irq] = event;
282         }
283
284         intr_event_add_handler(event, device_get_nameunit(child), filt,
285             handler, arg, intr_priority(flags), flags, cookiep);
286
287         /* unmask IRQ */
288         mask_register = ICU_IRQ_MASK_REG(irq);
289         ip_bit = ICU_IP_BIT(irq);
290
291         mask = ICU_REG_READ(mask_register);
292         ICU_REG_WRITE(mask_register, mask & ~ip_bit);
293
294         return (0);
295 }
296
297 static int
298 obio_teardown_intr(device_t dev, device_t child, struct resource *ires,
299     void *cookie)
300 {
301         struct obio_softc *sc = device_get_softc(dev);
302         int irq, result;
303         uint32_t mask_register, mask, ip_bit;
304
305         irq = rman_get_start(ires);
306         if (irq >= NIRQS)
307                 panic("%s: bad irq %d", __func__, irq);
308
309         if (sc->sc_eventstab[irq] == NULL)
310                 panic("Trying to teardown unoccupied IRQ");
311
312         /* mask IRQ */
313         mask_register = ICU_IRQ_MASK_REG(irq);
314         ip_bit = ICU_IP_BIT(irq);
315
316         mask = ICU_REG_READ(mask_register);
317         ICU_REG_WRITE(mask_register, mask | ip_bit);
318
319         result = intr_event_remove_handler(cookie);
320         if (!result)
321                 sc->sc_eventstab[irq] = NULL;
322
323         return (result);
324 }
325
326 static int
327 obio_intr(void *arg)
328 {
329         struct obio_softc *sc = arg;
330         struct intr_event *event;
331         uint32_t irqstat, ipend, imask, xpend;
332         int irq, thread, group, i;
333
334         irqstat = 0;
335         irq = 0;
336         for (group = 2; group <= 6; group++) {
337                 ipend = ICU_REG_READ(ICU_GROUP_IPEND_REG(group));
338                 imask = ICU_REG_READ(ICU_GROUP_MASK_REG(group));
339                 xpend = ipend;
340                 ipend &= ~imask;
341
342                 while ((i = fls(xpend)) != 0) {
343                         xpend &= ~(1 << (i - 1));
344                         irq = IP_IRQ(group, i - 1);
345                 }
346         
347                 while ((i = fls(ipend)) != 0) {
348                         ipend &= ~(1 << (i - 1));
349                         irq = IP_IRQ(group, i - 1);
350                         event = sc->sc_eventstab[irq];
351                         thread = 0;
352                         if (!event || TAILQ_EMPTY(&event->ie_handlers)) {
353                                 /* TODO: Log stray IRQs */
354                                 continue;
355                         }
356
357                         /* TODO: frame instead of NULL? */
358                         intr_event_handle(event, NULL);
359                         /* XXX: Log stray IRQs */
360                 }
361         }
362 #if 0
363         ipend = ICU_REG_READ(ICU_IPEND2);
364         printf("ipend2 = %08x!\n", ipend);
365
366         ipend = ICU_REG_READ(ICU_IPEND3);
367         printf("ipend3 = %08x!\n", ipend);
368
369         ipend = ICU_REG_READ(ICU_IPEND4);
370         printf("ipend4 = %08x!\n", ipend);
371         ipend = ICU_REG_READ(ICU_IPEND5);
372         printf("ipend5 = %08x!\n", ipend);
373
374         ipend = ICU_REG_READ(ICU_IPEND6);
375         printf("ipend6 = %08x!\n", ipend);
376 #endif
377         while (irqstat != 0) {
378                 if ((irqstat & 1) == 1) {
379                 }
380
381                 irq++;
382                 irqstat >>= 1;
383         }
384
385         return (FILTER_HANDLED);
386 }
387
388 static void
389 obio_hinted_child(device_t bus, const char *dname, int dunit)
390 {
391         device_t                child;
392         long                    maddr;
393         int                     msize;
394         int                     irq;
395         int                     result;
396
397         child = BUS_ADD_CHILD(bus, 0, dname, dunit);
398
399         /*
400          * Set hard-wired resources for hinted child using
401          * specific RIDs.
402          */
403         resource_long_value(dname, dunit, "maddr", &maddr);
404         resource_int_value(dname, dunit, "msize", &msize);
405
406
407         result = bus_set_resource(child, SYS_RES_MEMORY, 0,
408             maddr, msize);
409         if (result != 0)
410                 device_printf(bus, "warning: bus_set_resource() failed\n");
411
412         if (resource_int_value(dname, dunit, "irq", &irq) == 0) {
413                 result = bus_set_resource(child, SYS_RES_IRQ, 0, irq, 1);
414                 if (result != 0)
415                         device_printf(bus,
416                             "warning: bus_set_resource() failed\n");
417         }
418 }
419
420 static device_t
421 obio_add_child(device_t bus, int order, const char *name, int unit)
422 {
423         device_t                child;
424         struct obio_ivar        *ivar;
425
426         ivar = malloc(sizeof(struct obio_ivar), M_DEVBUF, M_WAITOK | M_ZERO);
427         if (ivar == NULL) {
428                 printf("Failed to allocate ivar\n");
429                 return (0);
430         }
431         resource_list_init(&ivar->resources);
432
433         child = device_add_child_ordered(bus, order, name, unit);
434         if (child == NULL) {
435                 printf("Can't add child %s%d ordered\n", name, unit);
436                 return (0);
437         }
438
439         device_set_ivars(child, ivar);
440
441         return (child);
442 }
443
444 /*
445  * Helper routine for bus_generic_rl_get_resource/bus_generic_rl_set_resource
446  * Provides pointer to resource_list for these routines
447  */
448 static struct resource_list *
449 obio_get_resource_list(device_t dev, device_t child)
450 {
451         struct obio_ivar *ivar;
452
453         ivar = device_get_ivars(child);
454         return (&(ivar->resources));
455 }
456
457 static device_method_t obio_methods[] = {
458         DEVMETHOD(bus_activate_resource,        obio_activate_resource),
459         DEVMETHOD(bus_add_child,                obio_add_child),
460         DEVMETHOD(bus_alloc_resource,           obio_alloc_resource),
461         DEVMETHOD(bus_deactivate_resource,      obio_deactivate_resource),
462         DEVMETHOD(bus_get_resource_list,        obio_get_resource_list),
463         DEVMETHOD(bus_hinted_child,             obio_hinted_child),
464         DEVMETHOD(bus_release_resource,         obio_release_resource),
465         DEVMETHOD(bus_setup_intr,               obio_setup_intr),
466         DEVMETHOD(bus_teardown_intr,            obio_teardown_intr),
467         DEVMETHOD(device_attach,                obio_attach),
468         DEVMETHOD(device_probe,                 obio_probe),
469         DEVMETHOD(bus_get_resource,             bus_generic_rl_get_resource),
470         DEVMETHOD(bus_set_resource,             bus_generic_rl_set_resource),
471
472         {0, 0},
473 };
474
475 static driver_t obio_driver = {
476         "obio",
477         obio_methods,
478         sizeof(struct obio_softc),
479 };
480 static devclass_t obio_devclass;
481
482 DRIVER_MODULE(obio, nexus, obio_driver, obio_devclass, 0, 0);