]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - sys/sparc64/sbus/sbus.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / sys / sparc64 / sbus / sbus.c
1 /*-
2  * Copyright (c) 1998 The NetBSD Foundation, Inc.
3  * All rights reserved.
4  *
5  * This code is derived from software contributed to The NetBSD Foundation
6  * by Paul Kranenburg.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *        This product includes software developed by the NetBSD
19  *        Foundation, Inc. and its contributors.
20  * 4. Neither the name of The NetBSD Foundation nor the names of its
21  *    contributors may be used to endorse or promote products derived
22  *    from this software without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
25  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
26  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
27  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
28  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34  * POSSIBILITY OF SUCH DAMAGE.
35  */
36 /*-
37  * Copyright (c) 1992, 1993
38  *      The Regents of the University of California.  All rights reserved.
39  *
40  * This software was developed by the Computer Systems Engineering group
41  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
42  * contributed to Berkeley.
43  *
44  * All advertising materials mentioning features or use of this software
45  * must display the following acknowledgement:
46  *      This product includes software developed by the University of
47  *      California, Lawrence Berkeley Laboratory.
48  *
49  * Redistribution and use in source and binary forms, with or without
50  * modification, are permitted provided that the following conditions
51  * are met:
52  * 1. Redistributions of source code must retain the above copyright
53  *    notice, this list of conditions and the following disclaimer.
54  * 2. Redistributions in binary form must reproduce the above copyright
55  *    notice, this list of conditions and the following disclaimer in the
56  *    documentation and/or other materials provided with the distribution.
57  * 4. Neither the name of the University nor the names of its contributors
58  *    may be used to endorse or promote products derived from this software
59  *    without specific prior written permission.
60  *
61  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
62  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
63  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
64  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
65  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
66  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
67  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
68  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
69  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
70  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
71  * SUCH DAMAGE.
72  */
73 /*-
74  * Copyright (c) 1999 Eduardo Horvath
75  * Copyright (c) 2002 by Thomas Moestl <tmm@FreeBSD.org>.
76  * Copyright (c) 2005 Marius Strobl <marius@FreeBSD.org>
77  * All rights reserved.
78  *
79  * Redistribution and use in source and binary forms, with or without
80  * modification, are permitted provided that the following conditions
81  * are met:
82  * 1. Redistributions of source code must retain the above copyright
83  *    notice, this list of conditions and the following disclaimer.
84  *
85  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR  ``AS IS'' AND
86  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
87  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
88  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR  BE LIABLE
89  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
90  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
91  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
92  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
93  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
94  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
95  * SUCH DAMAGE.
96  *
97  *      from: @(#)sbus.c        8.1 (Berkeley) 6/11/93
98  *      from: NetBSD: sbus.c,v 1.46 2001/10/07 20:30:41 eeh Exp
99  */
100
101 #include <sys/cdefs.h>
102 __FBSDID("$FreeBSD$");
103
104 /*
105  * SBus support.
106  */
107
108 #include <sys/param.h>
109 #include <sys/systm.h>
110 #include <sys/bus.h>
111 #include <sys/kernel.h>
112 #include <sys/malloc.h>
113 #include <sys/module.h>
114 #include <sys/pcpu.h>
115 #include <sys/queue.h>
116 #include <sys/reboot.h>
117 #include <sys/rman.h>
118
119 #include <dev/ofw/ofw_bus.h>
120 #include <dev/ofw/ofw_bus_subr.h>
121 #include <dev/ofw/openfirm.h>
122
123 #include <machine/bus.h>
124 #include <machine/bus_common.h>
125 #include <machine/bus_private.h>
126 #include <machine/iommureg.h>
127 #include <machine/iommuvar.h>
128 #include <machine/resource.h>
129
130 #include <sparc64/sbus/ofw_sbus.h>
131 #include <sparc64/sbus/sbusreg.h>
132 #include <sparc64/sbus/sbusvar.h>
133
134 struct sbus_devinfo {
135         int                     sdi_burstsz;
136         int                     sdi_clockfreq;
137         int                     sdi_slot;
138
139         struct ofw_bus_devinfo  sdi_obdinfo;
140         struct resource_list    sdi_rl;
141 };
142
143 /* Range descriptor, allocated for each sc_range. */
144 struct sbus_rd {
145         bus_addr_t              rd_poffset;
146         bus_addr_t              rd_pend;
147         int                     rd_slot;
148         bus_addr_t              rd_coffset;
149         bus_addr_t              rd_cend;
150         struct rman             rd_rman;
151         bus_space_handle_t      rd_bushandle;
152         struct resource         *rd_res;
153 };
154
155 struct sbus_softc {
156         device_t                sc_dev;
157         bus_dma_tag_t           sc_cdmatag;
158         bus_space_tag_t         sc_cbustag;
159         int                     sc_clockfreq;   /* clock frequency (in Hz) */
160         int                     sc_nrange;
161         struct sbus_rd          *sc_rd;
162         int                     sc_burst;       /* burst transfer sizes supp. */
163
164         struct resource         *sc_sysio_res;
165         int                     sc_ign;         /* IGN for this sysio */
166         struct iommu_state      sc_is;          /* IOMMU state (iommuvar.h) */
167
168         struct resource         *sc_ot_ires;
169         void                    *sc_ot_ihand;
170         struct resource         *sc_pf_ires;
171         void                    *sc_pf_ihand;
172 };
173
174 #define SYSIO_READ8(sc, off) \
175         bus_read_8((sc)->sc_sysio_res, (off))
176 #define SYSIO_WRITE8(sc, off, v) \
177         bus_write_8((sc)->sc_sysio_res, (off), (v))
178
179 static device_probe_t sbus_probe;
180 static device_attach_t sbus_attach;
181 static bus_print_child_t sbus_print_child;
182 static bus_probe_nomatch_t sbus_probe_nomatch;
183 static bus_read_ivar_t sbus_read_ivar;
184 static bus_get_resource_list_t sbus_get_resource_list;
185 static bus_setup_intr_t sbus_setup_intr;
186 static bus_alloc_resource_t sbus_alloc_resource;
187 static bus_release_resource_t sbus_release_resource;
188 static bus_activate_resource_t sbus_activate_resource;
189 static bus_deactivate_resource_t sbus_deactivate_resource;
190 static bus_get_dma_tag_t sbus_get_dma_tag;
191 static ofw_bus_get_devinfo_t sbus_get_devinfo;
192
193 static int sbus_inlist(const char *, const char *const *);
194 static struct sbus_devinfo * sbus_setup_dinfo(device_t, struct sbus_softc *,
195     phandle_t);
196 static void sbus_destroy_dinfo(struct sbus_devinfo *);
197 static void sbus_intr_enable(void *);
198 static void sbus_intr_disable(void *);
199 static void sbus_intr_assign(void *);
200 static void sbus_intr_clear(void *);
201 static int sbus_find_intrmap(struct sbus_softc *, u_int, bus_addr_t *,
202     bus_addr_t *);
203 static bus_space_tag_t sbus_alloc_bustag(struct sbus_softc *);
204 static driver_intr_t sbus_overtemp;
205 static driver_intr_t sbus_pwrfail;
206 static int sbus_print_res(struct sbus_devinfo *);
207
208 static device_method_t sbus_methods[] = {
209         /* Device interface */
210         DEVMETHOD(device_probe,         sbus_probe),
211         DEVMETHOD(device_attach,        sbus_attach),
212         DEVMETHOD(device_shutdown,      bus_generic_shutdown),
213         DEVMETHOD(device_suspend,       bus_generic_suspend),
214         DEVMETHOD(device_resume,        bus_generic_resume),
215
216         /* Bus interface */
217         DEVMETHOD(bus_print_child,      sbus_print_child),
218         DEVMETHOD(bus_probe_nomatch,    sbus_probe_nomatch),
219         DEVMETHOD(bus_read_ivar,        sbus_read_ivar),
220         DEVMETHOD(bus_alloc_resource,   sbus_alloc_resource),
221         DEVMETHOD(bus_activate_resource,        sbus_activate_resource),
222         DEVMETHOD(bus_deactivate_resource,      sbus_deactivate_resource),
223         DEVMETHOD(bus_release_resource, sbus_release_resource),
224         DEVMETHOD(bus_setup_intr,       sbus_setup_intr),
225         DEVMETHOD(bus_teardown_intr,    bus_generic_teardown_intr),
226         DEVMETHOD(bus_get_resource,     bus_generic_rl_get_resource),
227         DEVMETHOD(bus_get_resource_list, sbus_get_resource_list),
228         DEVMETHOD(bus_get_dma_tag,      sbus_get_dma_tag),
229
230         /* ofw_bus interface */
231         DEVMETHOD(ofw_bus_get_devinfo,  sbus_get_devinfo),
232         DEVMETHOD(ofw_bus_get_compat,   ofw_bus_gen_get_compat),
233         DEVMETHOD(ofw_bus_get_model,    ofw_bus_gen_get_model),
234         DEVMETHOD(ofw_bus_get_name,     ofw_bus_gen_get_name),
235         DEVMETHOD(ofw_bus_get_node,     ofw_bus_gen_get_node),
236         DEVMETHOD(ofw_bus_get_type,     ofw_bus_gen_get_type),
237
238         KOBJMETHOD_END
239 };
240
241 static driver_t sbus_driver = {
242         "sbus",
243         sbus_methods,
244         sizeof(struct sbus_softc),
245 };
246
247 static devclass_t sbus_devclass;
248
249 DRIVER_MODULE(sbus, nexus, sbus_driver, sbus_devclass, 0, 0);
250 MODULE_VERSION(sbus, 1);
251
252 #define OFW_SBUS_TYPE   "sbus"
253 #define OFW_SBUS_NAME   "sbus"
254
255 static const struct intr_controller sbus_ic = {
256         sbus_intr_enable,
257         sbus_intr_disable,
258         sbus_intr_assign,
259         sbus_intr_clear
260 };
261
262 struct sbus_icarg {
263         struct sbus_softc       *sica_sc;
264         bus_addr_t              sica_map;
265         bus_addr_t              sica_clr;
266 };
267
268 static const char *const sbus_order_first[] = {
269         "auxio",
270         "dma",
271         NULL
272 };
273
274 static int
275 sbus_inlist(const char *name, const char *const *list)
276 {
277         int i;
278
279         if (name == NULL)
280                 return (0);
281         for (i = 0; list[i] != NULL; i++) {
282                 if (strcmp(name, list[i]) == 0)
283                         return (1);
284         }
285         return (0);
286 }
287
288 static int
289 sbus_probe(device_t dev)
290 {
291         const char *t;
292
293         t = ofw_bus_get_type(dev);
294         if (((t == NULL || strcmp(t, OFW_SBUS_TYPE) != 0)) &&
295             strcmp(ofw_bus_get_name(dev), OFW_SBUS_NAME) != 0)
296                 return (ENXIO);
297         device_set_desc(dev, "U2S UPA-SBus bridge");
298         return (0);
299 }
300
301 static int
302 sbus_attach(device_t dev)
303 {
304         struct sbus_softc *sc;
305         struct sbus_devinfo *sdi;
306         struct sbus_icarg *sica;
307         struct sbus_ranges *range;
308         struct resource *res;
309         struct resource_list *rl;
310         device_t cdev;
311         bus_addr_t intrclr, intrmap, phys;
312         bus_size_t size;
313         u_long vec;
314         phandle_t child, node;
315         uint32_t prop;
316         int i, j;
317
318         sc = device_get_softc(dev);
319         sc->sc_dev = dev;
320         node = ofw_bus_get_node(dev);
321
322         i = 0;
323         sc->sc_sysio_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &i,
324             RF_ACTIVE);
325         if (sc->sc_sysio_res == NULL)
326                 panic("%s: cannot allocate device memory", __func__);
327
328         if (OF_getprop(node, "interrupts", &prop, sizeof(prop)) == -1)
329                 panic("%s: cannot get IGN", __func__);
330         sc->sc_ign = INTIGN(prop);
331         sc->sc_cbustag = sbus_alloc_bustag(sc);
332
333         /*
334          * Record clock frequency for synchronous SCSI.
335          * IS THIS THE CORRECT DEFAULT??
336          */
337         if (OF_getprop(node, "clock-frequency", &prop, sizeof(prop)) == -1)
338                 prop = 25000000;
339         sc->sc_clockfreq = prop;
340         prop /= 1000;
341         device_printf(dev, "clock %d.%03d MHz\n", prop / 1000, prop % 1000);
342
343         /*
344          * Collect address translations from the OBP.
345          */
346         if ((sc->sc_nrange = OF_getprop_alloc(node, "ranges",
347             sizeof(*range), (void **)&range)) == -1) {
348                 panic("%s: error getting ranges property", __func__);
349         }
350         sc->sc_rd = (struct sbus_rd *)malloc(sizeof(*sc->sc_rd) * sc->sc_nrange,
351             M_DEVBUF, M_NOWAIT);
352         if (sc->sc_rd == NULL)
353                 panic("%s: cannot allocate rmans", __func__);
354         /*
355          * Preallocate all space that the SBus bridge decodes, so that nothing
356          * else gets in the way; set up rmans etc.
357          */
358         rl = BUS_GET_RESOURCE_LIST(device_get_parent(dev), dev);
359         for (i = 0; i < sc->sc_nrange; i++) {
360                 phys = range[i].poffset | ((bus_addr_t)range[i].pspace << 32);
361                 size = range[i].size;
362                 sc->sc_rd[i].rd_slot = range[i].cspace;
363                 sc->sc_rd[i].rd_coffset = range[i].coffset;
364                 sc->sc_rd[i].rd_cend = sc->sc_rd[i].rd_coffset + size;
365                 j = resource_list_add_next(rl, SYS_RES_MEMORY, phys,
366                     phys + size - 1, size);
367                 if ((res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &j,
368                     RF_ACTIVE)) == NULL)
369                         panic("%s: cannot allocate decoded range", __func__);
370                 sc->sc_rd[i].rd_bushandle = rman_get_bushandle(res);
371                 sc->sc_rd[i].rd_rman.rm_type = RMAN_ARRAY;
372                 sc->sc_rd[i].rd_rman.rm_descr = "SBus Device Memory";
373                 if (rman_init(&sc->sc_rd[i].rd_rman) != 0 ||
374                     rman_manage_region(&sc->sc_rd[i].rd_rman, 0, size) != 0)
375                         panic("%s: failed to set up memory rman", __func__);
376                 sc->sc_rd[i].rd_poffset = phys;
377                 sc->sc_rd[i].rd_pend = phys + size;
378                 sc->sc_rd[i].rd_res = res;
379         }
380         free(range, M_OFWPROP);
381
382         /*
383          * Get the SBus burst transfer size if burst transfers are supported.
384          */
385         if (OF_getprop(node, "up-burst-sizes", &sc->sc_burst,
386             sizeof(sc->sc_burst)) == -1 || sc->sc_burst == 0)
387                 sc->sc_burst =
388                     (SBUS_BURST64_DEF << SBUS_BURST64_SHIFT) | SBUS_BURST_DEF;
389
390
391         /* initalise the IOMMU */
392
393         /* punch in our copies */
394         sc->sc_is.is_pmaxaddr = IOMMU_MAXADDR(SBUS_IOMMU_BITS);
395         sc->sc_is.is_bustag = rman_get_bustag(sc->sc_sysio_res);
396         sc->sc_is.is_bushandle = rman_get_bushandle(sc->sc_sysio_res);
397         sc->sc_is.is_iommu = SBR_IOMMU;
398         sc->sc_is.is_dtag = SBR_IOMMU_TLB_TAG_DIAG;
399         sc->sc_is.is_ddram = SBR_IOMMU_TLB_DATA_DIAG;
400         sc->sc_is.is_dqueue = SBR_IOMMU_QUEUE_DIAG;
401         sc->sc_is.is_dva = SBR_IOMMU_SVADIAG;
402         sc->sc_is.is_dtcmp = 0;
403         sc->sc_is.is_sb[0] = SBR_STRBUF;
404         sc->sc_is.is_sb[1] = 0;
405
406         /*
407          * Note: the SBus IOMMU ignores the high bits of an address, so a NULL
408          * DMA pointer will be translated by the first page of the IOTSB.
409          * To detect bugs we'll allocate and ignore the first entry.
410          */
411         iommu_init(device_get_nameunit(dev), &sc->sc_is, 3, -1, 1);
412
413         /* Create the DMA tag. */
414         if (bus_dma_tag_create(bus_get_dma_tag(dev), 8, 0,
415             sc->sc_is.is_pmaxaddr, ~0, NULL, NULL, sc->sc_is.is_pmaxaddr,
416             0xff, 0xffffffff, 0, NULL, NULL, &sc->sc_cdmatag) != 0)
417                 panic("%s: bus_dma_tag_create failed", __func__);
418         /* Customize the tag. */
419         sc->sc_cdmatag->dt_cookie = &sc->sc_is;
420         sc->sc_cdmatag->dt_mt = &iommu_dma_methods;
421
422         /*
423          * Hunt through all the interrupt mapping regs and register our
424          * interrupt controller for the corresponding interrupt vectors.
425          * We do this early in order to be able to catch stray interrupts.
426          */
427         for (i = 0; i <= SBUS_MAX_INO; i++) {
428                 if (sbus_find_intrmap(sc, i, &intrmap, &intrclr) == 0)
429                         continue;
430                 sica = malloc(sizeof(*sica), M_DEVBUF, M_NOWAIT);
431                 if (sica == NULL)
432                         panic("%s: could not allocate interrupt controller "
433                             "argument", __func__);
434                 sica->sica_sc = sc;
435                 sica->sica_map = intrmap;
436                 sica->sica_clr = intrclr;
437 #ifdef SBUS_DEBUG
438                 device_printf(dev,
439                     "intr map (INO %d, %s) %#lx: %#lx, clr: %#lx\n",
440                     i, (i & INTMAP_OBIO_MASK) == 0 ? "SBus slot" : "OBIO",
441                     (u_long)intrmap, (u_long)SYSIO_READ8(sc, intrmap),
442                     (u_long)intrclr);
443 #endif
444                 j = intr_controller_register(INTMAP_VEC(sc->sc_ign, i),
445                     &sbus_ic, sica);
446                 if (j != 0)
447                         device_printf(dev, "could not register interrupt "
448                             "controller for INO %d (%d)\n", i, j);
449         }
450
451         /* Enable the over-temperature and power-fail interrupts. */
452         i = 4;
453         sc->sc_ot_ires = bus_alloc_resource_any(dev, SYS_RES_IRQ, &i,
454             RF_ACTIVE);
455         if (sc->sc_ot_ires == NULL ||
456             INTIGN(vec = rman_get_start(sc->sc_ot_ires)) != sc->sc_ign ||
457             INTVEC(SYSIO_READ8(sc, SBR_THERM_INT_MAP)) != vec ||
458             intr_vectors[vec].iv_ic != &sbus_ic ||
459             bus_setup_intr(dev, sc->sc_ot_ires, INTR_TYPE_MISC | INTR_FAST,
460             NULL, sbus_overtemp, sc, &sc->sc_ot_ihand) != 0)
461                 panic("%s: failed to set up temperature interrupt", __func__);
462         i = 3;
463         sc->sc_pf_ires = bus_alloc_resource_any(dev, SYS_RES_IRQ, &i,
464             RF_ACTIVE);
465         if (sc->sc_pf_ires == NULL ||
466             INTIGN(vec = rman_get_start(sc->sc_pf_ires)) != sc->sc_ign ||
467             INTVEC(SYSIO_READ8(sc, SBR_POWER_INT_MAP)) != vec ||
468             intr_vectors[vec].iv_ic != &sbus_ic ||
469             bus_setup_intr(dev, sc->sc_pf_ires, INTR_TYPE_MISC | INTR_FAST,
470             NULL, sbus_pwrfail, sc, &sc->sc_pf_ihand) != 0)
471                 panic("%s: failed to set up power fail interrupt", __func__);
472
473         /* Initialize the counter-timer. */
474         sparc64_counter_init(device_get_nameunit(dev),
475             rman_get_bustag(sc->sc_sysio_res),
476             rman_get_bushandle(sc->sc_sysio_res), SBR_TC0);
477
478         /*
479          * Loop through ROM children, fixing any relative addresses
480          * and then configuring each device.
481          */
482         for (child = OF_child(node); child != 0; child = OF_peer(child)) {
483                 if ((sdi = sbus_setup_dinfo(dev, sc, child)) == NULL)
484                         continue;
485                 /*
486                  * For devices where there are variants that are actually
487                  * split into two SBus devices (as opposed to the first
488                  * half of the device being a SBus device and the second
489                  * half hanging off of the first one) like 'auxio' and
490                  * 'SUNW,fdtwo' or 'dma' and 'esp' probe the SBus device
491                  * which is a prerequisite to the driver attaching to the
492                  * second one with a lower order. Saves us from dealing
493                  * with different probe orders in the respective device
494                  * drivers which generally is more hackish.
495                  */
496                 cdev = device_add_child_ordered(dev, (OF_child(child) == 0 &&
497                     sbus_inlist(sdi->sdi_obdinfo.obd_name, sbus_order_first)) ?
498                     SBUS_ORDER_FIRST : SBUS_ORDER_NORMAL, NULL, -1);
499                 if (cdev == NULL) {
500                         device_printf(dev,
501                             "<%s>: device_add_child_ordered failed\n",
502                             sdi->sdi_obdinfo.obd_name);
503                         sbus_destroy_dinfo(sdi);
504                         continue;
505                 }
506                 device_set_ivars(cdev, sdi);
507         }
508         return (bus_generic_attach(dev));
509 }
510
511 static struct sbus_devinfo *
512 sbus_setup_dinfo(device_t dev, struct sbus_softc *sc, phandle_t node)
513 {
514         struct sbus_devinfo *sdi;
515         struct sbus_regs *reg;
516         u_int32_t base, iv, *intr;
517         int i, nreg, nintr, slot, rslot;
518
519         sdi = malloc(sizeof(*sdi), M_DEVBUF, M_ZERO | M_WAITOK);
520         if (ofw_bus_gen_setup_devinfo(&sdi->sdi_obdinfo, node) != 0) {
521                 free(sdi, M_DEVBUF);
522                 return (NULL);
523         }
524         resource_list_init(&sdi->sdi_rl);
525         slot = -1;
526         nreg = OF_getprop_alloc(node, "reg", sizeof(*reg), (void **)&reg);
527         if (nreg == -1) {
528                 if (sdi->sdi_obdinfo.obd_type == NULL ||
529                     strcmp(sdi->sdi_obdinfo.obd_type, "hierarchical") != 0) {
530                         device_printf(dev, "<%s>: incomplete\n",
531                             sdi->sdi_obdinfo.obd_name);
532                         goto fail;
533                 }
534         } else {
535                 for (i = 0; i < nreg; i++) {
536                         base = reg[i].sbr_offset;
537                         if (SBUS_ABS(base)) {
538                                 rslot = SBUS_ABS_TO_SLOT(base);
539                                 base = SBUS_ABS_TO_OFFSET(base);
540                         } else
541                                 rslot = reg[i].sbr_slot;
542                         if (slot != -1 && slot != rslot) {
543                                 device_printf(dev, "<%s>: multiple slots\n",
544                                     sdi->sdi_obdinfo.obd_name);
545                                 free(reg, M_OFWPROP);
546                                 goto fail;
547                         }
548                         slot = rslot;
549
550                         resource_list_add(&sdi->sdi_rl, SYS_RES_MEMORY, i,
551                             base, base + reg[i].sbr_size, reg[i].sbr_size);
552                 }
553                 free(reg, M_OFWPROP);
554         }
555         sdi->sdi_slot = slot;
556
557         /*
558          * The `interrupts' property contains the SBus interrupt level.
559          */
560         nintr = OF_getprop_alloc(node, "interrupts", sizeof(*intr),
561             (void **)&intr);
562         if (nintr != -1) {
563                 for (i = 0; i < nintr; i++) {
564                         iv = intr[i];
565                         /*
566                          * SBus card devices need the slot number encoded into
567                          * the vector as this is generally not done.
568                          */
569                         if ((iv & INTMAP_OBIO_MASK) == 0)
570                                 iv |= slot << 3;
571                         iv = INTMAP_VEC(sc->sc_ign, iv);
572                         resource_list_add(&sdi->sdi_rl, SYS_RES_IRQ, i,
573                             iv, iv, 1);
574                 }
575                 free(intr, M_OFWPROP);
576         }
577         if (OF_getprop(node, "burst-sizes", &sdi->sdi_burstsz,
578             sizeof(sdi->sdi_burstsz)) == -1)
579                 sdi->sdi_burstsz = sc->sc_burst;
580         else
581                 sdi->sdi_burstsz &= sc->sc_burst;
582         if (OF_getprop(node, "clock-frequency", &sdi->sdi_clockfreq,
583             sizeof(sdi->sdi_clockfreq)) == -1)
584                 sdi->sdi_clockfreq = sc->sc_clockfreq;
585
586         return (sdi);
587
588 fail:
589         sbus_destroy_dinfo(sdi);
590         return (NULL);
591 }
592
593 static void
594 sbus_destroy_dinfo(struct sbus_devinfo *dinfo)
595 {
596
597         resource_list_free(&dinfo->sdi_rl);
598         ofw_bus_gen_destroy_devinfo(&dinfo->sdi_obdinfo);
599         free(dinfo, M_DEVBUF);
600 }
601
602 static int
603 sbus_print_child(device_t dev, device_t child)
604 {
605         int rv;
606
607         rv = bus_print_child_header(dev, child);
608         rv += sbus_print_res(device_get_ivars(child));
609         rv += bus_print_child_footer(dev, child);
610         return (rv);
611 }
612
613 static void
614 sbus_probe_nomatch(device_t dev, device_t child)
615 {
616         const char *type;
617
618         device_printf(dev, "<%s>", ofw_bus_get_name(child));
619         sbus_print_res(device_get_ivars(child));
620         type = ofw_bus_get_type(child);
621         printf(" type %s (no driver attached)\n",
622             type != NULL ? type : "unknown");
623 }
624
625 static int
626 sbus_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
627 {
628         struct sbus_softc *sc;
629         struct sbus_devinfo *dinfo;
630
631         sc = device_get_softc(dev);
632         if ((dinfo = device_get_ivars(child)) == NULL)
633                 return (ENOENT);
634         switch (which) {
635         case SBUS_IVAR_BURSTSZ:
636                 *result = dinfo->sdi_burstsz;
637                 break;
638         case SBUS_IVAR_CLOCKFREQ:
639                 *result = dinfo->sdi_clockfreq;
640                 break;
641         case SBUS_IVAR_IGN:
642                 *result = sc->sc_ign;
643                 break;
644         case SBUS_IVAR_SLOT:
645                 *result = dinfo->sdi_slot;
646                 break;
647         default:
648                 return (ENOENT);
649         }
650         return (0);
651 }
652
653 static struct resource_list *
654 sbus_get_resource_list(device_t dev, device_t child)
655 {
656         struct sbus_devinfo *sdi;
657
658         sdi = device_get_ivars(child);
659         return (&sdi->sdi_rl);
660 }
661
662 static void
663 sbus_intr_enable(void *arg)
664 {
665         struct intr_vector *iv = arg;
666         struct sbus_icarg *sica = iv->iv_icarg;
667
668         SYSIO_WRITE8(sica->sica_sc, sica->sica_map,
669             INTMAP_ENABLE(iv->iv_vec, iv->iv_mid));
670 }
671
672 static void
673 sbus_intr_disable(void *arg)
674 {
675         struct intr_vector *iv = arg;
676         struct sbus_icarg *sica = iv->iv_icarg;
677
678         SYSIO_WRITE8(sica->sica_sc, sica->sica_map, iv->iv_vec);
679 }
680
681 static void
682 sbus_intr_assign(void *arg)
683 {
684         struct intr_vector *iv = arg;
685         struct sbus_icarg *sica = iv->iv_icarg;
686
687         SYSIO_WRITE8(sica->sica_sc, sica->sica_map, INTMAP_TID(
688             SYSIO_READ8(sica->sica_sc, sica->sica_map), iv->iv_mid));
689 }
690
691 static void
692 sbus_intr_clear(void *arg)
693 {
694         struct intr_vector *iv = arg;
695         struct sbus_icarg *sica = iv->iv_icarg;
696
697         SYSIO_WRITE8(sica->sica_sc, sica->sica_clr, 0);
698 }
699
700 static int
701 sbus_find_intrmap(struct sbus_softc *sc, u_int ino, bus_addr_t *intrmapptr,
702     bus_addr_t *intrclrptr)
703 {
704         bus_addr_t intrclr, intrmap;
705         int i;
706
707         if (ino > SBUS_MAX_INO) {
708                 device_printf(sc->sc_dev, "out of range INO %d requested\n",
709                     ino);
710                 return (0);
711         }
712
713         if ((ino & INTMAP_OBIO_MASK) == 0) {
714                 intrmap = SBR_SLOT0_INT_MAP + INTSLOT(ino) * 8;
715                 intrclr = SBR_SLOT0_INT_CLR +
716                     (INTSLOT(ino) * 8 * 8) + (INTPRI(ino) * 8);
717         } else {
718                 intrclr = 0;
719                 for (i = 0, intrmap = SBR_SCSI_INT_MAP;
720                     intrmap <= SBR_RESERVED_INT_MAP; intrmap += 8, i++) {
721                         if (INTVEC(SYSIO_READ8(sc, intrmap)) ==
722                             INTMAP_VEC(sc->sc_ign, ino)) {
723                                 intrclr = SBR_SCSI_INT_CLR + i * 8;
724                                 break;
725                         }
726                 }
727                 if (intrclr == 0)
728                         return (0);
729         }
730         if (intrmapptr != NULL)
731                 *intrmapptr = intrmap;
732         if (intrclrptr != NULL)
733                 *intrclrptr = intrclr;
734         return (1);
735 }
736
737 static int
738 sbus_setup_intr(device_t dev, device_t child, struct resource *ires, int flags,
739     driver_filter_t *filt, driver_intr_t *intr, void *arg, void **cookiep)
740 {
741         struct sbus_softc *sc;
742         u_long vec;
743
744         sc = device_get_softc(dev);
745         /*
746          * Make sure the vector is fully specified and we registered
747          * our interrupt controller for it.
748          */
749         vec = rman_get_start(ires);
750         if (INTIGN(vec) != sc->sc_ign || intr_vectors[vec].iv_ic != &sbus_ic) {
751                 device_printf(dev, "invalid interrupt vector 0x%lx\n", vec);
752                 return (EINVAL);
753         }
754         return (bus_generic_setup_intr(dev, child, ires, flags, filt, intr,
755             arg, cookiep));
756 }
757
758 static struct resource *
759 sbus_alloc_resource(device_t bus, device_t child, int type, int *rid,
760     u_long start, u_long end, u_long count, u_int flags)
761 {
762         struct sbus_softc *sc;
763         struct rman *rm;
764         struct resource *rv;
765         struct resource_list *rl;
766         struct resource_list_entry *rle;
767         device_t schild;
768         bus_space_handle_t bh;
769         bus_addr_t toffs;
770         bus_size_t tend;
771         int i, slot;
772         int isdefault, needactivate, passthrough;
773
774         isdefault = (start == 0UL && end == ~0UL);
775         needactivate = flags & RF_ACTIVE;
776         passthrough = (device_get_parent(child) != bus);
777         rle = NULL;
778         sc = device_get_softc(bus);
779         rl = BUS_GET_RESOURCE_LIST(bus, child);
780         switch (type) {
781         case SYS_RES_IRQ:
782                 return (resource_list_alloc(rl, bus, child, type, rid, start,
783                     end, count, flags));
784         case SYS_RES_MEMORY:
785                 if (!passthrough) {
786                         rle = resource_list_find(rl, type, *rid);
787                         if (rle == NULL)
788                                 return (NULL);
789                         if (rle->res != NULL)
790                                 panic("%s: resource entry is busy", __func__);
791                         if (isdefault) {
792                                 start = rle->start;
793                                 count = ulmax(count, rle->count);
794                                 end = ulmax(rle->end, start + count - 1);
795                         }
796                 }
797                 rm = NULL;
798                 bh = toffs = tend = 0;
799                 schild = child;
800                 while (device_get_parent(schild) != bus)
801                         schild = device_get_parent(schild);
802                 slot = sbus_get_slot(schild);
803                 for (i = 0; i < sc->sc_nrange; i++) {
804                         if (sc->sc_rd[i].rd_slot != slot ||
805                             start < sc->sc_rd[i].rd_coffset ||
806                             start > sc->sc_rd[i].rd_cend)
807                                 continue;
808                         /* Disallow cross-range allocations. */
809                         if (end > sc->sc_rd[i].rd_cend)
810                                 return (NULL);
811                         /* We've found the connection to the parent bus */
812                         toffs = start - sc->sc_rd[i].rd_coffset;
813                         tend = end - sc->sc_rd[i].rd_coffset;
814                         rm = &sc->sc_rd[i].rd_rman;
815                         bh = sc->sc_rd[i].rd_bushandle;
816                         break;
817                 }
818                 if (rm == NULL)
819                         return (NULL);
820                 flags &= ~RF_ACTIVE;
821                 rv = rman_reserve_resource(rm, toffs, tend, count, flags,
822                     child);
823                 if (rv == NULL)
824                         return (NULL);
825                 rman_set_rid(rv, *rid);
826                 rman_set_bustag(rv, sc->sc_cbustag);
827                 rman_set_bushandle(rv, bh + rman_get_start(rv));
828                 if (needactivate) {
829                         if (bus_activate_resource(child, type, *rid, rv)) {
830                                 rman_release_resource(rv);
831                                 return (NULL);
832                         }
833                 }
834                 if (!passthrough)
835                         rle->res = rv;
836                 return (rv);
837         default:
838                 return (NULL);
839         }
840 }
841
842 static int
843 sbus_activate_resource(device_t bus, device_t child, int type, int rid,
844     struct resource *r)
845 {
846         void *p;
847         int error;
848
849         if (type == SYS_RES_IRQ) {
850                 return (BUS_ACTIVATE_RESOURCE(device_get_parent(bus),
851                     child, type, rid, r));
852         }
853         if (type == SYS_RES_MEMORY) {
854                 /*
855                  * Need to memory-map the device space, as some drivers
856                  * depend on the virtual address being set and usable.
857                  */
858                 error = sparc64_bus_mem_map(rman_get_bustag(r),
859                     rman_get_bushandle(r), rman_get_size(r), 0, 0, &p);
860                 if (error != 0)
861                         return (error);
862                 rman_set_virtual(r, p);
863         }
864         return (rman_activate_resource(r));
865 }
866
867 static int
868 sbus_deactivate_resource(device_t bus, device_t child, int type, int rid,
869     struct resource *r)
870 {
871
872         if (type == SYS_RES_IRQ) {
873                 return (BUS_DEACTIVATE_RESOURCE(device_get_parent(bus),
874                     child, type, rid, r));
875         }
876         if (type == SYS_RES_MEMORY) {
877                 sparc64_bus_mem_unmap(rman_get_virtual(r), rman_get_size(r));
878                 rman_set_virtual(r, NULL);
879         }
880         return (rman_deactivate_resource(r));
881 }
882
883 static int
884 sbus_release_resource(device_t bus, device_t child, int type, int rid,
885     struct resource *r)
886 {
887         struct resource_list *rl;
888         struct resource_list_entry *rle;
889         int error, passthrough;
890
891         passthrough = (device_get_parent(child) != bus);
892         rl = BUS_GET_RESOURCE_LIST(bus, child);
893         if (type == SYS_RES_IRQ)
894                 return (resource_list_release(rl, bus, child, type, rid, r));
895         if ((rman_get_flags(r) & RF_ACTIVE) != 0) {
896                 error = bus_deactivate_resource(child, type, rid, r);
897                 if (error != 0)
898                         return (error);
899         }
900         error = rman_release_resource(r);
901         if (error != 0 || passthrough)
902                 return (error);
903         rle = resource_list_find(rl, type, rid);
904         if (rle == NULL)
905                 panic("%s: cannot find resource", __func__);
906         if (rle->res == NULL)
907                 panic("%s: resource entry is not busy", __func__);
908         rle->res = NULL;
909         return (0);
910 }
911
912 static bus_dma_tag_t
913 sbus_get_dma_tag(device_t bus, device_t child)
914 {
915         struct sbus_softc *sc;
916
917         sc = device_get_softc(bus);
918         return (sc->sc_cdmatag);
919 }
920
921 static const struct ofw_bus_devinfo *
922 sbus_get_devinfo(device_t bus, device_t child)
923 {
924         struct sbus_devinfo *sdi;
925
926         sdi = device_get_ivars(child);
927         return (&sdi->sdi_obdinfo);
928 }
929
930 /*
931  * Handle an overtemp situation.
932  *
933  * SPARCs have temperature sensors which generate interrupts
934  * if the machine's temperature exceeds a certain threshold.
935  * This handles the interrupt and powers off the machine.
936  * The same needs to be done to PCI controller drivers.
937  */
938 static void
939 sbus_overtemp(void *arg)
940 {
941         static int shutdown;
942
943         /* As the interrupt is cleared we may be called multiple times. */
944         if (shutdown != 0)
945                 return;
946         shutdown++;
947         printf("DANGER: OVER TEMPERATURE detected\nShutting down NOW.\n");
948         shutdown_nice(RB_POWEROFF);
949 }
950
951 /* Try to shut down in time in case of power failure. */
952 static void
953 sbus_pwrfail(void *arg)
954 {
955         static int shutdown;
956
957         /* As the interrupt is cleared we may be called multiple times. */
958         if (shutdown != 0)
959                 return;
960         shutdown++;
961         printf("Power failure detected\nShutting down NOW.\n");
962         shutdown_nice(0);
963 }
964
965 static bus_space_tag_t
966 sbus_alloc_bustag(struct sbus_softc *sc)
967 {
968         bus_space_tag_t sbt;
969
970         sbt = (bus_space_tag_t)malloc(sizeof(struct bus_space_tag), M_DEVBUF,
971             M_NOWAIT | M_ZERO);
972         if (sbt == NULL)
973                 panic("%s: out of memory", __func__);
974
975         sbt->bst_cookie = sc;
976         sbt->bst_parent = rman_get_bustag(sc->sc_sysio_res);
977         sbt->bst_type = SBUS_BUS_SPACE;
978         return (sbt);
979 }
980
981 static int
982 sbus_print_res(struct sbus_devinfo *sdi)
983 {
984         int rv;
985
986         rv = 0;
987         rv += resource_list_print_type(&sdi->sdi_rl, "mem", SYS_RES_MEMORY,
988             "%#lx");
989         rv += resource_list_print_type(&sdi->sdi_rl, "irq", SYS_RES_IRQ,
990             "%ld");
991         return (rv);
992 }