]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/sparc64/sparc64/upa.c
o Rename ic_eoi to ic_clear to emphasize the functions it points
[FreeBSD/FreeBSD.git] / sys / sparc64 / sparc64 / upa.c
1 /*-
2  * Copyright (c) 2006 Marius Strobl <marius@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/bus.h>
33 #include <sys/kernel.h>
34 #include <sys/module.h>
35 #include <sys/pcpu.h>
36 #include <sys/resource.h>
37 #include <sys/rman.h>
38
39 #include <dev/ofw/ofw_bus.h>
40 #include <dev/ofw/ofw_bus_subr.h>
41 #include <dev/ofw/openfirm.h>
42
43 #include <machine/bus.h>
44 #include <machine/bus_common.h>
45 #include <machine/resource.h>
46
47 #define UPA_NREG        3
48
49 #define UPA_CFG         0
50 #define UPA_IMR1        1
51 #define UPA_IMR2        2
52
53 /* UPA_CFG bank */
54 #define UPA_CFG_UPA0                    0x00    /* UPA0 config register */
55 #define UPA_CFG_UPA1                    0x08    /* UPA1 config register */
56 #define UPA_CFG_IF                      0x10    /* interface config register */
57 #define  UPA_CFG_IF_RST                 0x00
58 #define  UPA_CFG_IF_POK_RST             0x02
59 #define  UPA_CFG_IF_POK                 0x03
60 #define UPA_CFG_ESTAR                   0x18    /* Estar config register */
61 #define  UPA_CFG_ESTAR_SPEED_FULL       0x01
62 #define  UPA_CFG_ESTAR_SPEED_1_2        0x02
63 #define  UPA_CFG_ESTAR_SPEED_1_64       0x40
64
65 #define UPA_INO_BASE                    0x2a
66 #define UPA_INO_MAX                     0x2b
67
68 struct upa_regs {
69         uint64_t        phys;
70         uint64_t        size;
71 };
72
73 struct upa_ranges {
74         uint64_t        child;
75         uint64_t        parent;
76         uint64_t        size;
77 };
78
79 struct upa_devinfo {
80         struct ofw_bus_devinfo  udi_obdinfo;
81         struct resource_list    udi_rl;
82 };
83
84 struct upa_softc {
85         struct resource         *sc_res[UPA_NREG];
86         bus_space_tag_t         sc_bt[UPA_NREG];
87         bus_space_handle_t      sc_bh[UPA_NREG];
88
89         uint32_t                sc_ign;
90
91         int                     sc_nrange;
92         struct upa_ranges       *sc_ranges;
93 };
94
95 #define UPA_READ(sc, reg, off) \
96         bus_space_read_8((sc)->sc_bt[(reg)], (sc)->sc_bh[(reg)], (off))
97 #define UPA_WRITE(sc, reg, off, val) \
98         bus_space_write_8((sc)->sc_bt[(reg)], (sc)->sc_bh[(reg)], (off), (val))
99
100 static device_probe_t upa_probe;
101 static device_attach_t upa_attach;
102 static bus_alloc_resource_t upa_alloc_resource;
103 static bus_setup_intr_t upa_setup_intr;
104 static bus_print_child_t upa_print_child;
105 static bus_probe_nomatch_t upa_probe_nomatch;
106 static bus_get_resource_list_t upa_get_resource_list;
107 static ofw_bus_get_devinfo_t upa_get_devinfo;
108
109 static void upa_intr_enable(void *);
110 static void upa_intr_disable(void *);
111 static void upa_intr_assign(void *);
112 static struct upa_devinfo *upa_setup_dinfo(device_t, struct upa_softc *,
113     phandle_t, uint32_t);
114 static void upa_destroy_dinfo(struct upa_devinfo *);
115 static int upa_print_res(struct upa_devinfo *);
116
117 static device_method_t upa_methods[] = {
118         /* Device interface */
119         DEVMETHOD(device_probe,         upa_probe),
120         DEVMETHOD(device_attach,        upa_attach),
121         DEVMETHOD(device_shutdown,      bus_generic_shutdown),
122         DEVMETHOD(device_suspend,       bus_generic_suspend),
123         DEVMETHOD(device_resume,        bus_generic_resume),
124
125         /* Bus interface */
126         DEVMETHOD(bus_print_child,      upa_print_child),
127         DEVMETHOD(bus_probe_nomatch,    upa_probe_nomatch),
128         DEVMETHOD(bus_read_ivar,        bus_generic_read_ivar),
129         DEVMETHOD(bus_write_ivar,       bus_generic_write_ivar),
130         DEVMETHOD(bus_alloc_resource,   upa_alloc_resource),
131         DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
132         DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
133         DEVMETHOD(bus_release_resource, bus_generic_rl_release_resource),
134         DEVMETHOD(bus_setup_intr,       upa_setup_intr),
135         DEVMETHOD(bus_teardown_intr,    bus_generic_teardown_intr),
136         DEVMETHOD(bus_get_resource,     bus_generic_rl_get_resource),
137         DEVMETHOD(bus_get_resource_list, upa_get_resource_list),
138
139         /* ofw_bus interface */
140         DEVMETHOD(ofw_bus_get_devinfo,  upa_get_devinfo),
141         DEVMETHOD(ofw_bus_get_compat,   ofw_bus_gen_get_compat),
142         DEVMETHOD(ofw_bus_get_model,    ofw_bus_gen_get_model),
143         DEVMETHOD(ofw_bus_get_name,     ofw_bus_gen_get_name),
144         DEVMETHOD(ofw_bus_get_node,     ofw_bus_gen_get_node),
145         DEVMETHOD(ofw_bus_get_type,     ofw_bus_gen_get_type),
146
147         { NULL, NULL }
148 };
149
150 static devclass_t upa_devclass;
151
152 DEFINE_CLASS_0(upa, upa_driver, upa_methods, sizeof(struct upa_softc));
153 DRIVER_MODULE(upa, nexus, upa_driver, upa_devclass, 0, 0);
154
155 static const struct intr_controller upa_ic = {
156         upa_intr_enable,
157         upa_intr_disable,
158         upa_intr_assign,
159         /* The interrupts are pulse type and thus automatically cleared. */
160         NULL
161 };
162
163 struct upa_icarg {
164         struct upa_softc        *uica_sc;
165         u_int                   uica_imr;
166 };
167
168 static int
169 upa_probe(device_t dev)
170 {
171         const char* compat;
172
173         compat = ofw_bus_get_compat(dev);
174         if (compat != NULL && strcmp(ofw_bus_get_name(dev), "upa") == 0 &&
175             strcmp(compat, "upa64s") == 0) {
176                 device_set_desc(dev, "UPA bridge");
177                 return (BUS_PROBE_DEFAULT);
178         }
179         return (ENXIO);
180 }
181
182 static int
183 upa_attach(device_t dev)
184 {
185         struct upa_devinfo *udi;
186         struct upa_icarg *uica;
187         struct upa_softc *sc;
188         phandle_t child, node;
189         device_t cdev;
190         uint32_t portid;
191         int i, imr, j, rid;
192 #if 1
193         device_t *children, schizo;
194         u_long scount, sstart, ucount, ustart;
195         int nchildren;
196 #endif
197
198         sc = device_get_softc(dev);
199         node = ofw_bus_get_node(dev);
200         for (i = UPA_CFG; i <= UPA_IMR2; i++) {
201                 rid = i;
202                 /*
203                  * The UPA_IMR{1,2} resources are shared with that of the
204                  * Schizo PCI bus B CSR bank.
205                  */
206 #if 0
207                 sc->sc_res[i] = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
208                     &rid, ((i == UPA_IMR1 || i == UPA_IMR2) ? RF_SHAREABLE :
209                     0) | RF_ACTIVE);
210                 if (sc->sc_res[i] == NULL) {
211                         device_printf(dev,
212                             "could not allocate resource %d\n", i);
213                         goto fail;
214                 }
215                 sc->sc_bt[i] = rman_get_bustag(sc->sc_res[i]);
216                 sc->sc_bh[i] = rman_get_bushandle(sc->sc_res[i]);
217 #else
218                 /*
219                  * Workaround for the fact that rman(9) only allows to
220                  * share resources of the same size.
221                  */
222                 if (i == UPA_IMR1 || i == UPA_IMR2) {
223                         if (bus_get_resource(dev, SYS_RES_MEMORY, i, &ustart,
224                             &ucount) != 0) {
225                                 device_printf(dev,
226                                     "could not determine UPA resource\n");
227                                 goto fail;
228                         }
229                         if (device_get_children(device_get_parent(dev),
230                             &children, &nchildren) != 0) {
231                                 device_printf(dev, "could not get children\n");
232                                 goto fail;
233                         }
234                         schizo = NULL;
235                         for (j = 0; j < nchildren; j++) {
236                                 if (ofw_bus_get_type(children[j]) != NULL &&
237                                     strcmp(ofw_bus_get_type(children[j]),
238                                     "pci") == 0 &&
239                                     ofw_bus_get_compat(children[j]) != NULL &&
240                                     strcmp(ofw_bus_get_compat(children[j]),
241                                     "pci108e,8001") == 0 &&
242                                     ((bus_get_resource_start(children[j],
243                                     SYS_RES_MEMORY, 0) >> 20) & 1) == 1) {
244                                         schizo = children[j];
245                                         break;
246                                 }
247                         }
248                         free(children, M_TEMP);
249                         if (schizo == NULL) {
250                                 device_printf(dev, "could not find Schizo\n");
251                                 goto fail;
252                         }
253                         if (bus_get_resource(schizo, SYS_RES_MEMORY, 0,
254                             &sstart, &scount) != 0) {
255                                 device_printf(dev,
256                                     "could not determine Schizo resource\n");
257                                 goto fail;
258                         }
259                         sc->sc_res[i] = bus_alloc_resource(dev, SYS_RES_MEMORY,
260                             &rid, sstart, sstart + scount - 1, scount,
261                             RF_SHAREABLE | RF_ACTIVE);
262                 } else
263                         sc->sc_res[i] = bus_alloc_resource_any(dev,
264                             SYS_RES_MEMORY, &rid, RF_ACTIVE);
265                 if (sc->sc_res[i] == NULL) {
266                         device_printf(dev,
267                             "could not allocate resource %d\n", i);
268                         goto fail;
269                 }
270                 sc->sc_bt[i] = rman_get_bustag(sc->sc_res[i]);
271                 sc->sc_bh[i] = rman_get_bushandle(sc->sc_res[i]);
272                 if (i == UPA_IMR1 || i == UPA_IMR2)
273                         bus_space_subregion(sc->sc_bt[i], sc->sc_bh[i],
274                             ustart - sstart, ucount, &sc->sc_bh[i]);
275 #endif
276         }
277
278         if (OF_getprop(node, "portid", &sc->sc_ign, sizeof(sc->sc_ign)) == -1) {
279                 device_printf(dev, "could not determine IGN\n");
280                 goto fail;
281         }
282
283         sc->sc_nrange = OF_getprop_alloc(node, "ranges", sizeof(*sc->sc_ranges),
284             (void **)&sc->sc_ranges);
285         if (sc->sc_nrange == -1) {
286                 device_printf(dev, "could not determine ranges\n");
287                 goto fail;
288         }
289
290         /*
291          * Hunt through all the interrupt mapping regs and register our
292          * interrupt controller for the corresponding interrupt vectors.
293          */
294         for (i = UPA_INO_BASE; i <= UPA_INO_MAX; i++) {
295                 imr = 0;
296                 for (j = UPA_IMR1; j <= UPA_IMR2; j++) {
297                         if (INTVEC(UPA_READ(sc, j, 0x0)) ==
298                             INTMAP_VEC(sc->sc_ign, i)) {
299                                 imr = j;
300                                 break;
301                         }
302                 }
303                 if (imr == 0)
304                         continue;
305                 uica = malloc(sizeof(*uica), M_DEVBUF, M_NOWAIT);
306                 if (uica == NULL)
307                         panic("%s: could not allocate interrupt controller "
308                             "argument", __func__);
309                 uica->uica_sc = sc;
310                 uica->uica_imr = imr;
311 #ifdef UPA_DEBUG
312                 device_printf(dev, "intr map (INO %d) IMR%d: %#lx\n",
313                     i, imr, (u_long)UPA_READ(sc, imr, 0x0));
314 #endif
315                 if (intr_controller_register(INTMAP_VEC(sc->sc_ign, i),
316                     &upa_ic, uica) != 0)
317                         panic("%s: could not register interrupt controller "
318                             "for INO %d", __func__, i);
319         }
320
321         /* Make sure the power level is appropriate for normal operation. */
322         if (UPA_READ(sc, UPA_CFG, UPA_CFG_IF) != UPA_CFG_IF_POK) {
323                 if (bootverbose)
324                         device_printf(dev, "applying power\n");
325                 UPA_WRITE(sc, UPA_CFG, UPA_CFG_ESTAR, UPA_CFG_ESTAR_SPEED_1_2);
326                 UPA_WRITE(sc, UPA_CFG, UPA_CFG_ESTAR, UPA_CFG_ESTAR_SPEED_FULL);
327                 (void)UPA_READ(sc, UPA_CFG, UPA_CFG_ESTAR);
328                 UPA_WRITE(sc, UPA_CFG, UPA_CFG_IF, UPA_CFG_IF_POK_RST);
329                 (void)UPA_READ(sc, UPA_CFG, UPA_CFG_IF);
330                 DELAY(20000);
331                 UPA_WRITE(sc, UPA_CFG, UPA_CFG_IF, UPA_CFG_IF_POK);
332                 (void)UPA_READ(sc, UPA_CFG, UPA_CFG_IF);
333         }
334
335         for (child = OF_child(node); child != 0; child = OF_peer(child)) {
336                 /*
337                  * The `upa-portid' properties of the children are used as
338                  * index for the interrupt mapping registers.
339                  * The `upa-portid' properties are also used to make up the
340                  * INOs of the children as the values contained in their
341                  * `interrupts' properties are bogus.
342                  */
343                 if (OF_getprop(child, "upa-portid", &portid,
344                     sizeof(portid)) == -1) {
345                         device_printf(dev,
346                             "could not determine upa-portid of child 0x%lx\n",
347                             (unsigned long)child);
348                         continue;
349                 }
350                 if (portid > 1) {
351                         device_printf(dev,
352                             "upa-portid %d of child 0x%lx invalid\n", portid,
353                             (unsigned long)child);
354                         continue;
355                 }
356                 if ((udi = upa_setup_dinfo(dev, sc, child, portid)) == NULL)
357                         continue;
358                 if ((cdev = device_add_child(dev, NULL, -1)) == NULL) {
359                         device_printf(dev, "<%s>: device_add_child failed\n",
360                             udi->udi_obdinfo.obd_name);
361                         upa_destroy_dinfo(udi);
362                         continue;
363                 }
364                 device_set_ivars(cdev, udi);
365         }
366
367         return (bus_generic_attach(dev));
368
369  fail:
370         for (i = UPA_CFG; i <= UPA_IMR2 && sc->sc_res[i] != NULL; i++)
371                 bus_release_resource(dev, SYS_RES_MEMORY,
372                     rman_get_rid(sc->sc_res[i]), sc->sc_res[i]);
373         return (ENXIO);
374 }
375
376 static int
377 upa_print_child(device_t dev, device_t child)
378 {
379         int rv;
380
381         rv = bus_print_child_header(dev, child);
382         rv += upa_print_res(device_get_ivars(child));
383         rv += bus_print_child_footer(dev, child);
384         return (rv);
385 }
386
387 static void
388 upa_probe_nomatch(device_t dev, device_t child)
389 {
390         const char *type;
391
392         device_printf(dev, "<%s>", ofw_bus_get_name(child));
393         upa_print_res(device_get_ivars(child));
394         type = ofw_bus_get_type(child);
395         printf(" type %s (no driver attached)\n",
396             type != NULL ? type : "unknown");
397 }
398
399 static struct resource *
400 upa_alloc_resource(device_t dev, device_t child, int type, int *rid,
401     u_long start, u_long end, u_long count, u_int flags)
402 {
403         struct resource_list *rl;
404         struct resource_list_entry *rle;
405         struct upa_softc *sc;
406         struct resource *rv;
407         bus_addr_t cend, cstart;
408         int i, isdefault, passthrough;
409
410         isdefault = (start == 0UL && end == ~0UL);
411         passthrough = (device_get_parent(child) != dev);
412         sc = device_get_softc(dev);
413         rl = BUS_GET_RESOURCE_LIST(dev, child);
414         rle = NULL;
415         switch (type) {
416         case SYS_RES_IRQ:
417                 return (resource_list_alloc(rl, dev, child, type, rid, start,
418                     end, count, flags));
419         case SYS_RES_MEMORY:
420                 if (!passthrough) {
421                         rle = resource_list_find(rl, type, *rid);
422                         if (rle == NULL)
423                                 return (NULL);
424                         if (rle->res != NULL)
425                                 panic("%s: resource entry is busy", __func__);
426                         if (isdefault) {
427                                 start = rle->start;
428                                 count = ulmax(count, rle->count);
429                                 end = ulmax(rle->end, start + count - 1);
430                         }
431                 }
432                 for (i = 0; i < sc->sc_nrange; i++) {
433                         cstart = sc->sc_ranges[i].child;
434                         cend = cstart + sc->sc_ranges[i].size - 1;
435                         if (start < cstart || start > cend)
436                                 continue;
437                         if (end < cstart || end > cend)
438                                 return (NULL);
439                         start += sc->sc_ranges[i].parent - cstart;
440                         end += sc->sc_ranges[i].parent - cstart;
441                         rv = bus_generic_alloc_resource(dev, child, type, rid,
442                             start, end, count, flags);
443                         if (!passthrough)
444                                 rle->res = rv;
445                         return (rv);
446                 }
447                 /* FALLTHROUGH */
448         default:
449                 return (NULL);
450         }
451 }
452
453 static void
454 upa_intr_enable(void *arg)
455 {
456         struct intr_vector *iv = arg;
457         struct upa_icarg *uica = iv->iv_icarg;
458
459         UPA_WRITE(uica->uica_sc, uica->uica_imr, 0x0,
460             INTMAP_ENABLE(iv->iv_vec, iv->iv_mid));
461         (void)UPA_READ(uica->uica_sc, uica->uica_imr, 0x0);
462 }
463
464 static void
465 upa_intr_disable(void *arg)
466 {
467         struct intr_vector *iv = arg;
468         struct upa_icarg *uica = iv->iv_icarg;
469
470         UPA_WRITE(uica->uica_sc, uica->uica_imr, 0x0, iv->iv_vec);
471         (void)UPA_READ(uica->uica_sc, uica->uica_imr, 0x0);
472 }
473
474 static void
475 upa_intr_assign(void *arg)
476 {
477         struct intr_vector *iv = arg;
478         struct upa_icarg *uica = iv->iv_icarg;
479
480         UPA_WRITE(uica->uica_sc, uica->uica_imr, 0x0, INTMAP_TID(
481             UPA_READ(uica->uica_sc, uica->uica_imr, 0x0), iv->iv_mid));
482         (void)UPA_READ(uica->uica_sc, uica->uica_imr, 0x0);
483 }
484
485 static int
486 upa_setup_intr(device_t dev, device_t child, struct resource *ires, int flags,
487     driver_filter_t *filt, driver_intr_t *func, void *arg, void **cookiep)
488 {
489         struct upa_softc *sc;
490         u_long vec;
491
492         sc = device_get_softc(dev);
493         /*
494          * Make sure the vector is fully specified and we registered
495          * our interrupt controller for it.
496          */
497         vec = rman_get_start(ires);
498         if (INTIGN(vec) != sc->sc_ign || intr_vectors[vec].iv_ic != &upa_ic) {
499                 device_printf(dev, "invalid interrupt vector 0x%lx\n", vec);
500                 return (EINVAL);
501         }
502         return (bus_generic_setup_intr(dev, child, ires, flags, filt, func,
503             arg, cookiep));
504 }
505
506 static struct resource_list *
507 upa_get_resource_list(device_t dev, device_t child)
508 {
509         struct upa_devinfo *udi;
510
511         udi = device_get_ivars(child);
512         return (&udi->udi_rl);
513 }
514
515 static const struct ofw_bus_devinfo *
516 upa_get_devinfo(device_t dev, device_t child)
517 {
518         struct upa_devinfo *udi;
519
520         udi = device_get_ivars(child);
521         return (&udi->udi_obdinfo);
522 }
523
524 static struct upa_devinfo *
525 upa_setup_dinfo(device_t dev, struct upa_softc *sc, phandle_t node,
526     uint32_t portid)
527 {
528         struct upa_devinfo *udi;
529         struct upa_regs *reg;
530         uint32_t intr;
531         int i, nreg;
532
533         udi = malloc(sizeof(*udi), M_DEVBUF, M_WAITOK | M_ZERO);
534         if (ofw_bus_gen_setup_devinfo(&udi->udi_obdinfo, node) != 0) {
535                 free(udi, M_DEVBUF);
536                 return (NULL);
537         }
538         resource_list_init(&udi->udi_rl);
539
540         nreg = OF_getprop_alloc(node, "reg", sizeof(*reg), (void **)&reg);
541         if (nreg == -1) {
542                 device_printf(dev, "<%s>: incomplete\n",
543                     udi->udi_obdinfo.obd_name);
544                 goto fail;
545         }
546         for (i = 0; i < nreg; i++)
547                 resource_list_add(&udi->udi_rl, SYS_RES_MEMORY, i, reg[i].phys,
548                     reg[i].phys + reg[i].size - 1, reg[i].size);
549         free(reg, M_OFWPROP);
550
551         intr = INTMAP_VEC(sc->sc_ign, (UPA_INO_BASE + portid));
552         resource_list_add(&udi->udi_rl, SYS_RES_IRQ, 0, intr, intr, 1);
553
554         return (udi);
555
556  fail:
557         upa_destroy_dinfo(udi);
558         return (NULL);
559 }
560
561 static void
562 upa_destroy_dinfo(struct upa_devinfo *dinfo)
563 {
564
565         resource_list_free(&dinfo->udi_rl);
566         ofw_bus_gen_destroy_devinfo(&dinfo->udi_obdinfo);
567         free(dinfo, M_DEVBUF);
568 }
569
570 static int
571 upa_print_res(struct upa_devinfo *udi)
572 {
573         int rv;
574
575         rv = 0;
576         rv += resource_list_print_type(&udi->udi_rl, "mem", SYS_RES_MEMORY,
577             "%#lx");
578         rv += resource_list_print_type(&udi->udi_rl, "irq", SYS_RES_IRQ,
579             "%ld");
580         return (rv);
581 }