]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/mips/rt305x/obio.c
Update clang to trunk r290819 and resolve conflicts.
[FreeBSD/FreeBSD.git] / sys / mips / rt305x / 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  *
18  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
22  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/bus.h>
37 #include <sys/interrupt.h>
38 #include <sys/kernel.h>
39 #include <sys/module.h>
40 #include <sys/rman.h>
41 #include <sys/malloc.h>
42
43 #include <machine/bus.h>
44
45 #include <mips/rt305x/rt305xreg.h>
46 #include <mips/rt305x/obiovar.h>
47 #include <mips/rt305x/rt305x_icvar.h>
48
49 /* MIPS HW interrupts of IRQ/FIQ respectively */
50 #define RT305X_INTR             0
51 #define RT305X_FAST_INTR        1
52
53 /* Interrupt levels */
54 #define INTR_IRQ 0
55 #define INTR_FIQ 1
56
57
58 int irq_priorities[NIRQS] = {
59         INTR_IRQ,       /* SYSCTL */
60         INTR_FIQ,       /* TIMER0 */
61         INTR_FIQ,       /* WDTIMER */
62         INTR_IRQ,       /* Illegal Access */
63         INTR_IRQ,       /* PCM */
64         INTR_IRQ,       /* UART */
65         INTR_IRQ,       /* GPIO */
66         INTR_FIQ,       /* GDMA */
67         INTR_IRQ,       /* NAND */
68         INTR_IRQ,       /* Perfomance Counter */
69         INTR_IRQ,       /* I2S */
70         INTR_IRQ,       /* unknown */
71         INTR_IRQ,       /* UARTLITE */
72         INTR_IRQ,       /* unknown */
73         INTR_IRQ,       /* unknown */
74         INTR_IRQ,       /* unknown */
75         INTR_IRQ,       /* unknown */
76         INTR_IRQ,       /* EtherNet Switch */
77         INTR_FIQ,       /* OTG */
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 };
92
93
94 #define REG_READ(o) *((volatile uint32_t *)MIPS_PHYS_TO_KSEG1(INTCTL_BASE + (o)))
95 #define REG_WRITE(o,v) (REG_READ(o)) = (v)
96
97 static int      obio_activate_resource(device_t, device_t, int, int,
98                     struct resource *);
99 static device_t obio_add_child(device_t, u_int, const char *, int);
100 static struct resource *
101                 obio_alloc_resource(device_t, device_t, int, int *, rman_res_t,
102                     rman_res_t, rman_res_t, u_int);
103 static int      obio_attach(device_t);
104 static int      obio_deactivate_resource(device_t, device_t, int, int,
105                     struct resource *);
106 static struct resource_list *
107                 obio_get_resource_list(device_t, device_t);
108 static void     obio_add_res_child(device_t, const char *, int, long, int, int);
109 static void     obio_hinted_child(device_t, const char *, int);
110 static int      obio_intr(void *);
111 static int      obio_probe(device_t);
112 static int      obio_release_resource(device_t, device_t, int, int,
113                     struct resource *);
114 static int      obio_setup_intr(device_t, device_t, struct resource *, int,
115                     driver_filter_t *, driver_intr_t *, void *, void **);
116 static int      obio_teardown_intr(device_t, device_t, struct resource *,
117                     void *);
118
119 static void 
120 obio_mask_irq(void *source)
121 {
122         int irq;
123         uint32_t irqmask;
124
125         irq = (int)source;
126         irqmask = 1 << irq;
127
128         /* disable IRQ */
129         rt305x_ic_set(IC_INT_DIS, irqmask);
130 }
131
132 static void 
133 obio_unmask_irq(void *source)
134 {
135         int irq;
136         uint32_t irqmask;
137
138         irq = (int)source;
139         irqmask = 1 << irq;
140
141         /* enable IRQ */
142         rt305x_ic_set(IC_INT_ENA, irqmask);
143
144 }
145
146
147 static int
148 obio_probe(device_t dev)
149 {
150
151         return (BUS_PROBE_NOWILDCARD);
152 }
153
154 static int
155 obio_attach(device_t dev)
156 {
157         struct obio_softc *sc = device_get_softc(dev);
158         int rid;
159
160         sc->oba_mem_rman.rm_type = RMAN_ARRAY;
161         sc->oba_mem_rman.rm_descr = "OBIO memory";
162         if (rman_init(&sc->oba_mem_rman) != 0 ||
163             rman_manage_region(&sc->oba_mem_rman, OBIO_MEM_START,
164                 OBIO_MEM_END) != 0)
165                 panic("obio_attach: failed to set up I/O rman");
166
167         sc->oba_irq_rman.rm_type = RMAN_ARRAY;
168         sc->oba_irq_rman.rm_descr = "OBIO IRQ";
169         if (rman_init(&sc->oba_irq_rman) != 0 ||
170             rman_manage_region(&sc->oba_irq_rman, 0, NIRQS-1) != 0)
171                 panic("obio_attach: failed to set up IRQ rman");
172
173         /* Hook up our interrupt handler. */
174         if ((sc->sc_irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid,
175             RT305X_INTR, RT305X_INTR, 1,
176             RF_SHAREABLE | RF_ACTIVE)) == NULL) {
177                 device_printf(dev, "unable to allocate IRQ resource\n");
178                 return (ENXIO);
179         }
180
181         if ((bus_setup_intr(dev, sc->sc_irq, INTR_TYPE_MISC, obio_intr, NULL,
182             sc, &sc->sc_ih))) {
183                 device_printf(dev,
184                     "WARNING: unable to register interrupt handler\n");
185                 return (ENXIO);
186         }
187
188         /* Hook up our FAST interrupt handler. */
189         if ((sc->sc_fast_irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid,
190             RT305X_FAST_INTR, RT305X_FAST_INTR, 1,
191             RF_SHAREABLE | RF_ACTIVE)) == NULL) {
192                 device_printf(dev, "unable to allocate IRQ resource\n");
193                 return (ENXIO);
194         }
195
196         if ((bus_setup_intr(dev, sc->sc_fast_irq, INTR_TYPE_MISC, obio_intr,
197             NULL, sc, &sc->sc_fast_ih))) {
198                 device_printf(dev,
199                     "WARNING: unable to register interrupt handler\n");
200                 return (ENXIO);
201         }
202
203         /* disable all interrupts */
204         rt305x_ic_set(IC_INT_DIS, IC_INT_MASK|IC_LINE_GLOBAL);
205
206         bus_generic_probe(dev);
207
208         obio_add_res_child(dev, "rt305x_sysctl", 0, 
209             SYSCTL_BASE, (SYSCTL_END - SYSCTL_BASE + 1),
210             IC_SYSCTL);
211         obio_add_res_child(dev, "rt305x_ic", 0, 
212             INTCTL_BASE, (INTCTL_END - INTCTL_BASE + 1),
213             -1);
214 #ifdef notyet
215         obio_add_res_child(dev, "timer",0, 
216             TIMER_BASE, (TIMER_END - TIMER_BASE  + 1),
217             IC_TIMER0);
218         obio_add_res_child(dev, "rt305x_memc", 0,
219             MEMCTRL_BASE, (MEMCTRL_END - MEMCTRL_BASE + 1),
220             -1);
221         obio_add_res_child(dev, "pcm",  0, 
222             PCM_BASE, (PCM_END - PCM_BASE  + 1),
223             IC_PCM);
224 #endif
225         obio_add_res_child(dev, "uart", 0, 
226             UART_BASE, (UART_END - UART_BASE + 1),
227             IC_UART);
228         obio_add_res_child(dev, "gpio", 0, 
229             PIO_BASE, (PIO_END - PIO_BASE  + 1),
230             IC_PIO);
231 #ifdef notyet
232         obio_add_res_child(dev, "rt305x_dma", 0,
233             GDMA_BASE, (GDMA_END - GDMA_BASE + 1),
234             IC_DMA);
235         obio_add_res_child(dev, "rt305x_nandc", 0,
236             NANDFC_BASE, (NANDFC_END - NANDFC_BASE  + 1),
237             IC_NAND);
238         obio_add_res_child(dev, "i2c",  0,
239             I2C_BASE, (I2C_END - I2C_BASE  + 1),
240             -1);
241         obio_add_res_child(dev, "i2s", 0,
242             I2S_BASE, (I2S_END - I2S_BASE  + 1),
243             IC_I2S);
244 #endif
245         obio_add_res_child(dev, "spi", 0, 
246             SPI_BASE, (SPI_END - SPI_BASE  + 1),
247             -1);
248         obio_add_res_child(dev, "uart", 1,
249             UARTLITE_BASE, (UARTLITE_END - UARTLITE_BASE + 1),
250             IC_UARTLITE);
251 #if !defined(RT5350) && !defined(MT7620)
252         obio_add_res_child(dev, "cfi",  0,
253             FLASH_BASE, (FLASH_END - FLASH_BASE  + 1),
254             -1);
255         obio_add_res_child(dev, "dwcotg", 0,
256             USB_OTG_BASE, (USB_OTG_END - USB_OTG_BASE  + 1),
257             IC_OTG);
258 #else
259         obio_add_res_child(dev, "ehci", 0,
260             USB_OTG_BASE, (USB_OTG_END - USB_OTG_BASE  + 1),
261             IC_OTG);
262         obio_add_res_child(dev, "ohci", 0,
263             USB_OHCI_BASE, (USB_OHCI_END - USB_OHCI_BASE + 1),
264             IC_OTG);
265 #endif
266         obio_add_res_child(dev, "switch", 0,
267             ETHSW_BASE, (ETHSW_END - ETHSW_BASE  + 1),
268             IC_ETHSW);
269
270         bus_enumerate_hinted_children(dev);
271         bus_generic_attach(dev);
272
273         /* enable IC */
274         rt305x_ic_set(IC_INT_ENA, IC_LINE_GLOBAL);
275
276         return (0);
277 }
278
279 static struct resource *
280 obio_alloc_resource(device_t bus, device_t child, int type, int *rid,
281     rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
282 {
283         struct obio_softc               *sc = device_get_softc(bus);
284         struct obio_ivar                *ivar = device_get_ivars(child);
285         struct resource                 *rv;
286         struct resource_list_entry      *rle;
287         struct rman                     *rm;
288         int                              isdefault, needactivate, passthrough;
289
290         isdefault = (RMAN_IS_DEFAULT_RANGE(start, end) && count == 1);
291         needactivate = flags & RF_ACTIVE;
292         passthrough = (device_get_parent(child) != bus);
293         rle = NULL;
294
295         if (passthrough)
296                 return (BUS_ALLOC_RESOURCE(device_get_parent(bus), child, type,
297                     rid, start, end, count, flags));
298
299         /*
300          * If this is an allocation of the "default" range for a given RID,
301          * and we know what the resources for this device are (ie. they aren't
302          * maintained by a child bus), then work out the start/end values.
303          */
304         if (isdefault) {
305                 rle = resource_list_find(&ivar->resources, type, *rid);
306                 if (rle == NULL)
307                         return (NULL);
308                 if (rle->res != NULL) {
309                         panic("%s: resource entry is busy", __func__);
310                 }
311                 start = rle->start;
312                 end = rle->end;
313                 count = rle->count;
314         }
315
316         switch (type) {
317         case SYS_RES_IRQ:
318                 rm = &sc->oba_irq_rman;
319                 break;
320         case SYS_RES_MEMORY:
321                 rm = &sc->oba_mem_rman;
322                 break;
323         default:
324                 printf("%s: unknown resource type %d\n", __func__, type);
325                 return (0);
326         }
327
328         rv = rman_reserve_resource(rm, start, end, count, flags, child);
329         if (rv == NULL) {
330                 printf("%s: could not reserve resource\n", __func__);
331                 return (0);
332         }
333
334         rman_set_rid(rv, *rid);
335
336         if (needactivate) {
337                 if (bus_activate_resource(child, type, *rid, rv)) {
338                         printf("%s: could not activate resource\n", __func__);
339                         rman_release_resource(rv);
340                         return (0);
341                 }
342         }
343
344         return (rv);
345 }
346
347 static int
348 obio_activate_resource(device_t bus, device_t child, int type, int rid,
349     struct resource *r)
350 {
351
352         /*
353          * If this is a memory resource, track the direct mapping
354          * in the uncached MIPS KSEG1 segment.
355          */
356         if (type == SYS_RES_MEMORY) {
357                 void *vaddr;
358
359                 vaddr = (void *)MIPS_PHYS_TO_KSEG1((intptr_t)rman_get_start(r));
360                 rman_set_virtual(r, vaddr);
361                 rman_set_bustag(r, mips_bus_space_generic);
362                 rman_set_bushandle(r, (bus_space_handle_t)vaddr);
363         }
364
365         return (rman_activate_resource(r));
366 }
367
368 static int
369 obio_deactivate_resource(device_t bus, device_t child, int type, int rid,
370     struct resource *r)
371 {
372
373         return (rman_deactivate_resource(r));
374 }
375
376 static int
377 obio_release_resource(device_t dev, device_t child, int type,
378     int rid, struct resource *r)
379 {
380         struct resource_list *rl;
381         struct resource_list_entry *rle;
382
383         rl = obio_get_resource_list(dev, child);
384         if (rl == NULL)
385                 return (EINVAL);
386         rle = resource_list_find(rl, type, rid);
387         if (rle == NULL)
388                 return (EINVAL);
389         rman_release_resource(r);
390         rle->res = NULL;
391
392         return (0);
393 }
394
395 static int
396 obio_setup_intr(device_t dev, device_t child, struct resource *ires,
397                 int flags, driver_filter_t *filt, driver_intr_t *handler,
398                 void *arg, void **cookiep)
399 {
400         struct obio_softc *sc = device_get_softc(dev);
401         struct intr_event *event;
402         int irq, error, priority;
403         uint32_t irqmask;
404
405         irq = rman_get_start(ires);
406
407         if (irq >= NIRQS)
408                 panic("%s: bad irq %d", __func__, irq);
409
410         event = sc->sc_eventstab[irq];
411         if (event == NULL) {
412                 error = intr_event_create(&event, (void *)irq, 0, irq,
413                     obio_mask_irq, obio_unmask_irq,
414                     NULL, NULL, "obio intr%d:", irq);
415
416                 sc->sc_eventstab[irq] = event;
417         }
418         else
419                 panic("obio: Can't share IRQs");
420
421         intr_event_add_handler(event, device_get_nameunit(child), filt,
422             handler, arg, intr_priority(flags), flags, cookiep);
423
424         irqmask = 1 << irq;
425         priority = irq_priorities[irq];
426
427         if (priority == INTR_FIQ)
428                 rt305x_ic_set(IC_INTTYPE, rt305x_ic_get(IC_INTTYPE) | irqmask);
429         else
430                 rt305x_ic_set(IC_INTTYPE, rt305x_ic_get(IC_INTTYPE) & ~irqmask);
431
432         /* enable */
433         obio_unmask_irq((void*)irq);
434
435         return (0);
436 }
437
438 static int
439 obio_teardown_intr(device_t dev, device_t child, struct resource *ires,
440     void *cookie)
441 {
442         struct obio_softc *sc = device_get_softc(dev);
443         int irq, result, priority;
444         uint32_t irqmask;
445
446         irq = rman_get_start(ires);
447         if (irq >= NIRQS)
448                 panic("%s: bad irq %d", __func__, irq);
449
450         if (sc->sc_eventstab[irq] == NULL)
451                 panic("Trying to teardown unoccupied IRQ");
452
453         irqmask = (1 << irq);
454         priority = irq_priorities[irq];
455
456         if (priority == INTR_FIQ)
457                 rt305x_ic_set(IC_INTTYPE, rt305x_ic_get(IC_INTTYPE) & ~irqmask);
458         else
459                 rt305x_ic_set(IC_INTTYPE, rt305x_ic_get(IC_INTTYPE) | irqmask);
460
461         /* disable */
462         obio_mask_irq((void*)irq);
463
464         result = intr_event_remove_handler(cookie);
465         if (!result) {
466                 sc->sc_eventstab[irq] = NULL;
467         }
468
469         return (result);
470 }
471
472 static int
473 obio_intr(void *arg)
474 {
475         struct obio_softc *sc = arg;
476         struct intr_event *event;
477         uint32_t irqstat;
478         int irq;
479
480         irqstat = rt305x_ic_get(IC_IRQ0STAT);
481         irqstat |= rt305x_ic_get(IC_IRQ1STAT);
482
483         irq = 0;
484         while (irqstat != 0) {
485                 if ((irqstat & 1) == 1) {
486                         event = sc->sc_eventstab[irq];
487                         if (!event || TAILQ_EMPTY(&event->ie_handlers))
488                                 continue;
489
490                         /* TODO: pass frame as an argument*/
491                         /* TODO: log stray interrupt */
492                         intr_event_handle(event, NULL);
493                 }
494                 irq++;
495                 irqstat >>= 1;
496         }
497
498         return (FILTER_HANDLED);
499 }
500
501 static void
502 obio_add_res_child(device_t bus, const char *dname, int dunit, 
503     long maddr, int msize, int irq)
504 {
505         device_t                child;
506         int                     result;
507
508         child = BUS_ADD_CHILD(bus, 0, dname, dunit);
509
510         result = bus_set_resource(child, SYS_RES_MEMORY, 0,
511             maddr, msize);
512         if (result != 0)
513                 device_printf(bus, "warning: bus_set_resource() failed\n");
514
515         if (irq != -1) {
516                 result = bus_set_resource(child, SYS_RES_IRQ, 0, irq, 1);
517                 if (result != 0)
518                         device_printf(bus,
519                             "warning: bus_set_resource() failed\n");
520         }
521 }
522
523 static void
524 obio_hinted_child(device_t bus, const char *dname, int dunit)
525 {
526         long                    maddr;
527         int                     msize;
528         int                     irq;
529
530         /*
531          * Set hard-wired resources for hinted child using
532          * specific RIDs.
533          */
534         resource_long_value(dname, dunit, "maddr", &maddr);
535         resource_int_value(dname, dunit, "msize", &msize);
536
537
538         if (resource_int_value(dname, dunit, "irq", &irq) == 0) irq = -1;
539
540         obio_add_res_child(bus, dname, dunit, maddr, msize, irq);
541 }
542
543 static device_t
544 obio_add_child(device_t bus, u_int order, const char *name, int unit)
545 {
546         device_t                child;
547         struct obio_ivar        *ivar;
548
549         ivar = malloc(sizeof(struct obio_ivar), M_DEVBUF, M_WAITOK | M_ZERO);
550         resource_list_init(&ivar->resources);
551
552         child = device_add_child_ordered(bus, order, name, unit);
553         if (child == NULL) {
554                 printf("Can't add child %s%d ordered\n", name, unit);
555                 return (0);
556         }
557
558         device_set_ivars(child, ivar);
559
560         return (child);
561 }
562
563 /*
564  * Helper routine for bus_generic_rl_get_resource/bus_generic_rl_set_resource
565  * Provides pointer to resource_list for these routines
566  */
567 static struct resource_list *
568 obio_get_resource_list(device_t dev, device_t child)
569 {
570         struct obio_ivar *ivar;
571
572         ivar = device_get_ivars(child);
573         return (&(ivar->resources));
574 }
575
576 static int
577 obio_print_all_resources(device_t dev)
578 {
579         struct obio_ivar *ivar = device_get_ivars(dev);
580         struct resource_list *rl = &ivar->resources;
581         int retval = 0;
582
583         if (STAILQ_FIRST(rl))
584                 retval += printf(" at");
585
586         retval += resource_list_print_type(rl, "mem", SYS_RES_MEMORY, "%#jx");
587         retval += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%jd");
588
589         return (retval);
590 }
591
592 static int
593 obio_print_child(device_t bus, device_t child)
594 {
595         int retval = 0;
596
597         retval += bus_print_child_header(bus, child);
598         retval += obio_print_all_resources(child);
599         if (device_get_flags(child))
600                 retval += printf(" flags %#x", device_get_flags(child));
601         retval += printf(" on %s\n", device_get_nameunit(bus));
602
603         return (retval);
604 }
605
606 static device_method_t obio_methods[] = {
607         DEVMETHOD(bus_activate_resource,        obio_activate_resource),
608         DEVMETHOD(bus_add_child,                obio_add_child),
609         DEVMETHOD(bus_alloc_resource,           obio_alloc_resource),
610         DEVMETHOD(bus_deactivate_resource,      obio_deactivate_resource),
611         DEVMETHOD(bus_get_resource_list,        obio_get_resource_list),
612         DEVMETHOD(bus_hinted_child,             obio_hinted_child),
613         DEVMETHOD(bus_print_child,              obio_print_child),
614         DEVMETHOD(bus_release_resource,         obio_release_resource),
615         DEVMETHOD(bus_setup_intr,               obio_setup_intr),
616         DEVMETHOD(bus_teardown_intr,            obio_teardown_intr),
617         DEVMETHOD(device_attach,                obio_attach),
618         DEVMETHOD(device_probe,                 obio_probe),
619         DEVMETHOD(bus_get_resource,             bus_generic_rl_get_resource),
620         DEVMETHOD(bus_set_resource,             bus_generic_rl_set_resource),
621
622         {0, 0},
623 };
624
625 static driver_t obio_driver = {
626         "obio",
627         obio_methods,
628         sizeof(struct obio_softc),
629 };
630 static devclass_t obio_devclass;
631
632 DRIVER_MODULE(obio, nexus, obio_driver, obio_devclass, 0, 0);