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