]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/sparc64/sparc64/upa.c
This commit was generated by cvs2svn to compensate for changes in r170242,
[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
67 struct upa_regs {
68         uint64_t        phys;
69         uint64_t        size;
70 };
71
72 struct upa_ranges {
73         uint64_t        child;
74         uint64_t        parent;
75         uint64_t        size;
76 };
77
78 struct upa_devinfo {
79         struct ofw_bus_devinfo  udi_obdinfo;
80         struct resource_list    udi_rl;
81 };
82
83 struct upa_softc {
84         struct resource         *sc_res[UPA_NREG];
85         bus_space_tag_t         sc_bt[UPA_NREG];
86         bus_space_handle_t      sc_bh[UPA_NREG];
87
88         uint32_t                sc_ign;
89
90         int                     sc_nrange;
91         struct upa_ranges       *sc_ranges;
92 };
93
94 #define UPA_READ(sc, reg, off) \
95         bus_space_read_8((sc)->sc_bt[(reg)], (sc)->sc_bh[(reg)], (off))
96 #define UPA_WRITE(sc, reg, off, val) \
97         bus_space_write_8((sc)->sc_bt[(reg)], (sc)->sc_bh[(reg)], (off), (val))
98
99 static device_probe_t upa_probe;
100 static device_attach_t upa_attach;
101 static bus_alloc_resource_t upa_alloc_resource;
102 static bus_setup_intr_t upa_setup_intr;
103 static bus_print_child_t upa_print_child;
104 static bus_probe_nomatch_t upa_probe_nomatch;
105 static bus_get_resource_list_t upa_get_resource_list;
106 static ofw_bus_get_devinfo_t upa_get_devinfo;
107
108 static struct upa_devinfo *upa_setup_dinfo(device_t, struct upa_softc *,
109     phandle_t, uint32_t);
110 static void upa_destroy_dinfo(struct upa_devinfo *);
111 static int upa_print_res(struct upa_devinfo *);
112
113 static device_method_t upa_methods[] = {
114         /* Device interface */
115         DEVMETHOD(device_probe,         upa_probe),
116         DEVMETHOD(device_attach,        upa_attach),
117         DEVMETHOD(device_shutdown,      bus_generic_shutdown),
118         DEVMETHOD(device_suspend,       bus_generic_suspend),
119         DEVMETHOD(device_resume,        bus_generic_resume),
120
121         /* Bus interface */
122         DEVMETHOD(bus_print_child,      upa_print_child),
123         DEVMETHOD(bus_probe_nomatch,    upa_probe_nomatch),
124         DEVMETHOD(bus_read_ivar,        bus_generic_read_ivar),
125         DEVMETHOD(bus_write_ivar,       bus_generic_write_ivar),
126         DEVMETHOD(bus_alloc_resource,   upa_alloc_resource),
127         DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
128         DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
129         DEVMETHOD(bus_release_resource, bus_generic_rl_release_resource),
130         DEVMETHOD(bus_setup_intr,       upa_setup_intr),
131         DEVMETHOD(bus_teardown_intr,    bus_generic_teardown_intr),
132         DEVMETHOD(bus_get_resource,     bus_generic_rl_get_resource),
133         DEVMETHOD(bus_get_resource_list, upa_get_resource_list),
134
135         /* ofw_bus interface */
136         DEVMETHOD(ofw_bus_get_devinfo,  upa_get_devinfo),
137         DEVMETHOD(ofw_bus_get_compat,   ofw_bus_gen_get_compat),
138         DEVMETHOD(ofw_bus_get_model,    ofw_bus_gen_get_model),
139         DEVMETHOD(ofw_bus_get_name,     ofw_bus_gen_get_name),
140         DEVMETHOD(ofw_bus_get_node,     ofw_bus_gen_get_node),
141         DEVMETHOD(ofw_bus_get_type,     ofw_bus_gen_get_type),
142
143         { NULL, NULL }
144 };
145
146 static devclass_t upa_devclass;
147
148 DEFINE_CLASS_0(upa, upa_driver, upa_methods, sizeof(struct upa_softc));
149 DRIVER_MODULE(upa, nexus, upa_driver, upa_devclass, 0, 0);
150
151 static int
152 upa_probe(device_t dev)
153 {
154         const char* compat;
155
156         compat = ofw_bus_get_compat(dev);
157         if (compat != NULL && strcmp(ofw_bus_get_name(dev), "upa") == 0 &&
158             strcmp(compat, "upa64s") == 0) {
159                 device_set_desc(dev, "UPA bridge");
160                 return (BUS_PROBE_DEFAULT);
161         }
162         return (ENXIO);
163 }
164
165 static int
166 upa_attach(device_t dev)
167 {
168         struct upa_devinfo *udi;
169         struct upa_softc *sc;
170         phandle_t child, node;
171         device_t cdev;
172         uint32_t portid;
173         int i, rid;
174 #if 1
175         device_t *children, schizo;
176         u_long scount, sstart, ucount, ustart;
177         int j, nchildren;
178 #endif
179
180         sc = device_get_softc(dev);
181         node = ofw_bus_get_node(dev);
182         for (i = UPA_CFG; i <= UPA_IMR2; i++) {
183                 rid = i;
184                 /*
185                  * The UPA_IMR{1,2} resources are shared with that of the
186                  * Schizo PCI bus B CSR bank.
187                  */
188 #if 0
189                 sc->sc_res[i] = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
190                     &rid, ((i == UPA_IMR1 || i == UPA_IMR2) ? RF_SHAREABLE :
191                     0) | RF_ACTIVE);
192                 if (sc->sc_res[i] == NULL) {
193                         device_printf(dev,
194                             "could not allocate resource %d\n", i);
195                         goto fail;
196                 }
197                 sc->sc_bt[i] = rman_get_bustag(sc->sc_res[i]);
198                 sc->sc_bh[i] = rman_get_bushandle(sc->sc_res[i]);
199 #else
200                 /*
201                  * Workaround for the fact that rman(9) only allows to
202                  * share resources of the same size.
203                  */
204                 if (i == UPA_IMR1 || i == UPA_IMR2) {
205                         if (bus_get_resource(dev, SYS_RES_MEMORY, i, &ustart,
206                             &ucount) != 0) {
207                                 device_printf(dev,
208                                     "could not determine UPA resource\n");
209                                 goto fail;
210                         }
211                         if (device_get_children(device_get_parent(dev),
212                             &children, &nchildren) != 0) {
213                                 device_printf(dev, "could not get children\n");
214                                 goto fail;
215                         }
216                         schizo = NULL;
217                         for (j = 0; j < nchildren; j++) {
218                                 if (ofw_bus_get_type(children[j]) != NULL &&
219                                     strcmp(ofw_bus_get_type(children[j]),
220                                     "pci") == 0 &&
221                                     ofw_bus_get_compat(children[j]) != NULL &&
222                                     strcmp(ofw_bus_get_compat(children[j]),
223                                     "pci108e,8001") == 0 &&
224                                     ((bus_get_resource_start(children[j],
225                                     SYS_RES_MEMORY, 0) >> 20) & 1) == 1) {
226                                         schizo = children[j];
227                                         break;
228                                 }
229                         }
230                         free(children, M_TEMP);
231                         if (schizo == NULL) {
232                                 device_printf(dev, "could not find Schizo\n");
233                                 goto fail;
234                         }
235                         if (bus_get_resource(schizo, SYS_RES_MEMORY, 0,
236                             &sstart, &scount) != 0) {
237                                 device_printf(dev,
238                                     "could not determine Schizo resource\n");
239                                 goto fail;
240                         }
241                         sc->sc_res[i] = bus_alloc_resource(dev, SYS_RES_MEMORY,
242                             &rid, sstart, sstart + scount - 1, scount,
243                             RF_SHAREABLE | RF_ACTIVE);
244                 } else
245                         sc->sc_res[i] = bus_alloc_resource_any(dev,
246                             SYS_RES_MEMORY, &rid, RF_ACTIVE);
247                 if (sc->sc_res[i] == NULL) {
248                         device_printf(dev,
249                             "could not allocate resource %d\n", i);
250                         goto fail;
251                 }
252                 sc->sc_bt[i] = rman_get_bustag(sc->sc_res[i]);
253                 sc->sc_bh[i] = rman_get_bushandle(sc->sc_res[i]);
254                 if (i == UPA_IMR1 || i == UPA_IMR2)
255                         bus_space_subregion(sc->sc_bt[i], sc->sc_bh[i],
256                             ustart - sstart, ucount, &sc->sc_bh[i]);
257 #endif
258         }
259
260         if (OF_getprop(node, "portid", &portid, sizeof(portid)) == -1) {
261                 device_printf(dev, "could not determine portid\n");
262                 goto fail;
263         }
264         sc->sc_ign = (portid << INTMAP_IGN_SHIFT) & INTMAP_IGN_MASK;
265
266         sc->sc_nrange = OF_getprop_alloc(node, "ranges", sizeof(*sc->sc_ranges),
267             (void **)&sc->sc_ranges);
268         if (sc->sc_nrange == -1) {
269                 device_printf(dev, "could not determine ranges\n");
270                 goto fail;
271         }
272
273         /* Make sure the power level is appropriate for normal operation. */
274         if (UPA_READ(sc, UPA_CFG, UPA_CFG_IF) != UPA_CFG_IF_POK) {
275                 if (bootverbose)
276                         device_printf(dev, "applying power\n");
277                 UPA_WRITE(sc, UPA_CFG, UPA_CFG_ESTAR, UPA_CFG_ESTAR_SPEED_1_2);
278                 UPA_WRITE(sc, UPA_CFG, UPA_CFG_ESTAR, UPA_CFG_ESTAR_SPEED_FULL);
279                 (void)UPA_READ(sc, UPA_CFG, UPA_CFG_ESTAR);
280                 UPA_WRITE(sc, UPA_CFG, UPA_CFG_IF, UPA_CFG_IF_POK_RST);
281                 (void)UPA_READ(sc, UPA_CFG, UPA_CFG_IF);
282                 DELAY(20000);
283                 UPA_WRITE(sc, UPA_CFG, UPA_CFG_IF, UPA_CFG_IF_POK);
284                 (void)UPA_READ(sc, UPA_CFG, UPA_CFG_IF);
285         }
286
287         for (child = OF_child(node); child != 0; child = OF_peer(child)) {
288                 /*
289                  * The `upa-portid' properties of the children are used as
290                  * index for the interrupt mapping registers. Thus we also
291                  * use it as device unit number in order to save on a unit
292                  * member in the devinfo struct.
293                  * The `upa-portid' properties are also used to make up the
294                  * INOs of the children as the values contained in their
295                  * `interrupts' properties are bogus.
296                  */
297                 if (OF_getprop(child, "upa-portid", &portid,
298                     sizeof(portid)) == -1) {
299                         device_printf(dev,
300                             "could not determine upa-portid of child 0x%lx\n",
301                             (unsigned long)child);
302                         continue;
303                 }
304                 if (portid > 1) {
305                         device_printf(dev,
306                             "upa-portid %d of child 0x%lx invalid\n", portid,
307                             (unsigned long)child);
308                         continue;
309                 }
310                 if ((udi = upa_setup_dinfo(dev, sc, child, portid)) == NULL)
311                         continue;
312                 if ((cdev = device_add_child(dev, NULL, portid)) == NULL) {
313                         device_printf(dev, "<%s>: device_add_child failed\n",
314                             udi->udi_obdinfo.obd_name);
315                         upa_destroy_dinfo(udi);
316                         continue;
317                 }
318                 device_set_ivars(cdev, udi);
319         }
320
321         return (bus_generic_attach(dev));
322
323  fail:
324         for (i = UPA_CFG; i <= UPA_IMR2 && sc->sc_res[i] != NULL; i++)
325                 bus_release_resource(dev, SYS_RES_MEMORY,
326                     rman_get_rid(sc->sc_res[i]), sc->sc_res[i]);
327         return (ENXIO);
328 }
329
330 static int
331 upa_print_child(device_t dev, device_t child)
332 {
333         int rv;
334
335         rv = bus_print_child_header(dev, child);
336         rv += upa_print_res(device_get_ivars(child));
337         rv += bus_print_child_footer(dev, child);
338         return (rv);
339 }
340
341 static void
342 upa_probe_nomatch(device_t dev, device_t child)
343 {
344         const char *type;
345
346         device_printf(dev, "<%s>", ofw_bus_get_name(child));
347         upa_print_res(device_get_ivars(child));
348         type = ofw_bus_get_type(child);
349         printf(" type %s (no driver attached)\n",
350             type != NULL ? type : "unknown");
351 }
352
353 static struct resource *
354 upa_alloc_resource(device_t dev, device_t child, int type, int *rid,
355     u_long start, u_long end, u_long count, u_int flags)
356 {
357         struct resource_list *rl;
358         struct resource_list_entry *rle;
359         struct upa_softc *sc;
360         struct resource *rv;
361         bus_addr_t cend, cstart;
362         int i, isdefault, passthrough;
363
364         isdefault = (start == 0UL && end == ~0UL);
365         passthrough = (device_get_parent(child) != dev);
366         sc = device_get_softc(dev);
367         rl = BUS_GET_RESOURCE_LIST(dev, child);
368         rle = NULL;
369         switch (type) {
370         case SYS_RES_IRQ:
371                 return (resource_list_alloc(rl, dev, child, type, rid, start,
372                     end, count, flags));
373         case SYS_RES_MEMORY:
374                 if (!passthrough) {
375                         rle = resource_list_find(rl, type, *rid);
376                         if (rle == NULL)
377                                 return (NULL);
378                         if (rle->res != NULL)
379                                 panic("%s: resource entry is busy", __func__);
380                         if (isdefault) {
381                                 start = rle->start;
382                                 count = ulmax(count, rle->count);
383                                 end = ulmax(rle->end, start + count - 1);
384                         }
385                 }
386                 for (i = 0; i < sc->sc_nrange; i++) {
387                         cstart = sc->sc_ranges[i].child;
388                         cend = cstart + sc->sc_ranges[i].size - 1;
389                         if (start < cstart || start > cend)
390                                 continue;
391                         if (end < cstart || end > cend)
392                                 return (NULL);
393                         start += sc->sc_ranges[i].parent - cstart;
394                         end += sc->sc_ranges[i].parent - cstart;
395                         rv = bus_generic_alloc_resource(dev, child, type, rid,
396                             start, end, count, flags);
397                         if (!passthrough)
398                                 rle->res = rv;
399                         return (rv);
400                 }
401                 /* FALLTHROUGH */
402         default:
403                 return (NULL);
404         }
405 }
406
407 static int
408 upa_setup_intr(device_t dev, device_t child, struct resource *ires, int flags,
409     driver_filter_t *filt, driver_intr_t *func, void *arg, void **cookiep)
410 {
411         struct upa_softc *sc;
412         uint64_t intrmap;
413         u_int imr;
414         int error;
415
416         sc = device_get_softc(dev);
417         imr = device_get_unit(child) == 0 ? UPA_IMR1 : UPA_IMR2;
418         intrmap = UPA_READ(sc, imr, 0x0);
419         if (INTVEC(intrmap) != INTVEC(rman_get_start(ires))) {
420                 device_printf(dev,
421                     "invalid interrupt vector (0x%x != 0x%x)\n",
422                     (unsigned int)INTVEC(intrmap),
423                     (unsigned int)INTVEC(rman_get_start(ires)));
424                 return (EINVAL);
425         }
426
427         /*
428          * The interrupts are pulse type and therefore automatically
429          * cleared. Thus we don't need to use a stub/wrapper like we
430          * do with the other sun4u host-to-<foo> bridges.
431          */
432
433         UPA_WRITE(sc, imr, 0x0, intrmap & ~INTMAP_V);
434         (void)UPA_READ(sc, imr, 0x0);
435         error = bus_generic_setup_intr(dev, child, ires, flags, filt,
436             func, arg, cookiep);
437         if (error != 0)
438                 return (error);
439         UPA_WRITE(sc, imr, 0x0, INTMAP_ENABLE(intrmap, PCPU_GET(mid)));
440         (void)UPA_READ(sc, imr, 0x0);
441
442         return (0);
443 }
444
445 static struct resource_list *
446 upa_get_resource_list(device_t dev, device_t child)
447 {
448         struct upa_devinfo *udi;
449
450         udi = device_get_ivars(child);
451         return (&udi->udi_rl);
452 }
453
454 static const struct ofw_bus_devinfo *
455 upa_get_devinfo(device_t dev, device_t child)
456 {
457         struct upa_devinfo *udi;
458
459         udi = device_get_ivars(child);
460         return (&udi->udi_obdinfo);
461 }
462
463 static struct upa_devinfo *
464 upa_setup_dinfo(device_t dev, struct upa_softc *sc, phandle_t node,
465     uint32_t portid)
466 {
467         struct upa_devinfo *udi;
468         struct upa_regs *reg;
469         uint32_t intr;
470         int i, nreg;
471
472         udi = malloc(sizeof(*udi), M_DEVBUF, M_WAITOK | M_ZERO);
473         if (ofw_bus_gen_setup_devinfo(&udi->udi_obdinfo, node) != 0) {
474                 free(udi, M_DEVBUF);
475                 return (NULL);
476         }
477         resource_list_init(&udi->udi_rl);
478
479         nreg = OF_getprop_alloc(node, "reg", sizeof(*reg), (void **)&reg);
480         if (nreg == -1) {
481                 device_printf(dev, "<%s>: incomplete\n",
482                     udi->udi_obdinfo.obd_name);
483                 goto fail;
484         }
485         for (i = 0; i < nreg; i++)
486                 resource_list_add(&udi->udi_rl, SYS_RES_MEMORY, i, reg[i].phys,
487                     reg[i].phys + reg[i].size - 1, reg[i].size);
488         free(reg, M_OFWPROP);
489
490         intr = (UPA_INO_BASE + portid) | sc->sc_ign;
491         resource_list_add(&udi->udi_rl, SYS_RES_IRQ, 0, intr, intr, 1);
492
493         return (udi);
494
495  fail:
496         upa_destroy_dinfo(udi);
497         return (NULL);
498 }
499
500 static void
501 upa_destroy_dinfo(struct upa_devinfo *dinfo)
502 {
503
504         resource_list_free(&dinfo->udi_rl);
505         ofw_bus_gen_destroy_devinfo(&dinfo->udi_obdinfo);
506         free(dinfo, M_DEVBUF);
507 }
508
509 static int
510 upa_print_res(struct upa_devinfo *udi)
511 {
512         int rv;
513
514         rv = 0;
515         rv += resource_list_print_type(&udi->udi_rl, "mem", SYS_RES_MEMORY,
516             "%#lx");
517         rv += resource_list_print_type(&udi->udi_rl, "irq", SYS_RES_IRQ,
518             "%ld");
519         return (rv);
520 }