2 * Copyright (c) 2007 Bruce M. Simpson.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/systm.h>
33 #include <sys/kernel.h>
34 #include <sys/module.h>
36 #include <sys/malloc.h>
38 #include <machine/bus.h>
40 #include <dev/siba/siba_ids.h>
41 #include <dev/siba/sibareg.h>
42 #include <dev/siba/sibavar.h>
45 * TODO: De-mipsify this code.
46 * TODO: cpu clock calculation. -> move to siba_cc instance
47 * TODO: Hardwire IRQs for attached cores on siba at probe time.
48 * TODO: Support detach.
49 * TODO: Power management.
51 * TODO: Support deployments of siba other than as a system bus.
55 #define MIPS_MEM_RID 0x20
58 extern int rman_debug;
60 static struct rman mem_rman; /* XXX move to softc */
62 static int siba_debug = 1;
63 static const char descfmt[] = "Sonics SiliconBackplane rev %s";
64 #define SIBA_DEVDESCLEN sizeof(descfmt) + 8
67 * Device identifiers and descriptions.
69 static struct siba_devid siba_devids[] = {
70 { SIBA_VID_BROADCOM, SIBA_DEVID_CHIPCOMMON, SIBA_REV_ANY,
72 { SIBA_VID_BROADCOM, SIBA_DEVID_SDRAM, SIBA_REV_ANY,
74 { SIBA_VID_BROADCOM, SIBA_DEVID_PCI, SIBA_REV_ANY,
75 "PCI host interface" },
76 { SIBA_VID_BROADCOM, SIBA_DEVID_MIPS, SIBA_REV_ANY,
78 { SIBA_VID_BROADCOM, SIBA_DEVID_ETHERNET, SIBA_REV_ANY,
80 { SIBA_VID_BROADCOM, SIBA_DEVID_USB11_HOSTDEV, SIBA_REV_ANY,
81 "USB host controller" },
82 { SIBA_VID_BROADCOM, SIBA_DEVID_IPSEC, SIBA_REV_ANY,
83 "IPSEC accelerator" },
84 { SIBA_VID_BROADCOM, SIBA_DEVID_SDRAMDDR, SIBA_REV_ANY,
85 "SDRAM/DDR controller" },
86 { SIBA_VID_BROADCOM, SIBA_DEVID_MIPS_3302, SIBA_REV_ANY,
91 static int siba_activate_resource(device_t, device_t, int, int,
93 static device_t siba_add_child(device_t, u_int, const char *, int);
94 static struct resource *
95 siba_alloc_resource(device_t, device_t, int, int *, u_long,
96 u_long, u_long, u_int);
97 static int siba_attach(device_t);
99 static void siba_destroy_devinfo(struct siba_devinfo *);
101 static struct siba_devid *
102 siba_dev_match(uint16_t, uint16_t, uint8_t);
103 static struct resource_list *
104 siba_get_reslist(device_t, device_t);
105 static uint8_t siba_getirq(uint16_t);
106 static int siba_print_all_resources(device_t dev);
107 static int siba_print_child(device_t, device_t);
108 static int siba_probe(device_t);
109 static void siba_probe_nomatch(device_t, device_t);
110 int siba_read_ivar(device_t, device_t, int, uintptr_t *);
111 static struct siba_devinfo *
112 siba_setup_devinfo(device_t, uint8_t);
113 int siba_write_ivar(device_t, device_t, int, uintptr_t);
114 uint8_t siba_getncores(device_t, uint16_t);
117 * On the Sentry5, the system bus IRQs are the same as the
118 * MIPS IRQs. Particular cores are hardwired to certain IRQ lines.
121 siba_getirq(uint16_t devid)
126 case SIBA_DEVID_CHIPCOMMON:
129 case SIBA_DEVID_ETHERNET:
132 case SIBA_DEVID_IPSEC:
135 case SIBA_DEVID_USB11_HOSTDEV:
143 * 5 is reserved for the MIPS on-chip timer interrupt;
144 * it is hard-wired by the tick driver.
146 case SIBA_DEVID_MIPS:
147 case SIBA_DEVID_MIPS_3302:
152 irq = 0xFF; /* this core does not need an irq */
160 siba_probe(device_t dev)
162 struct siba_softc *sc = device_get_softc(dev);
169 //rman_debug = 1; /* XXX */
172 * Map the ChipCommon register set using the hints the kernel
176 sc->siba_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
178 if (sc->siba_mem_res == NULL) {
179 device_printf(dev, "unable to allocate probe aperture\n");
182 sc->siba_mem_bt = rman_get_bustag(sc->siba_mem_res);
183 sc->siba_mem_bh = rman_get_bushandle(sc->siba_mem_res);
184 sc->siba_maddr = rman_get_start(sc->siba_mem_res);
185 sc->siba_msize = rman_get_size(sc->siba_mem_res);
188 device_printf(dev, "start %08x len %08x\n",
189 sc->siba_maddr, sc->siba_msize);
192 idlo = siba_mips_read_4(sc, 0, SIBA_IDLOW);
193 idhi = siba_mips_read_4(sc, 0, SIBA_IDHIGH);
194 ccid = ((idhi & 0x8ff0) >> 4);
196 device_printf(dev, "idlo = %08x\n", idlo);
197 device_printf(dev, "idhi = %08x\n", idhi);
198 device_printf(dev, " chipcore id = %08x\n", ccid);
202 * For now, check that the first core is the ChipCommon core.
204 if (ccid != SIBA_DEVID_CHIPCOMMON) {
206 device_printf(dev, "first core is not ChipCommon\n");
211 * Determine backplane revision and set description string.
215 char descbuf[SIBA_DEVDESCLEN];
217 rev = idlo & 0xF0000000;
219 if (rev == 0x00000000)
221 else if (rev == 0x10000000)
224 (void)snprintf(descbuf, sizeof(descbuf), descfmt, revp);
225 device_set_desc_copy(dev, descbuf);
228 * Determine how many cores are present on this siba bus, so
229 * that we may map them all.
235 ccidreg = siba_mips_read_4(sc, 0, SIBA_CC_CHIPID);
236 cc_id = (ccidreg & SIBA_CC_IDMASK);
237 cc_rev = (ccidreg & SIBA_CC_REVMASK) >> SIBA_CC_REVSHIFT;
239 device_printf(dev, "ccid = %08x, cc_id = %04x, cc_rev = %04x\n",
240 ccidreg, cc_id, cc_rev);
243 sc->siba_ncores = siba_getncores(dev, cc_id);
245 device_printf(dev, "%d cores detected.\n", sc->siba_ncores);
249 * Now we know how many cores are on this siba, release the
250 * mapping and allocate a new mapping spanning all cores on the bus.
254 result = bus_release_resource(dev, SYS_RES_MEMORY, rid,
257 device_printf(dev, "error %d releasing resource\n", result);
262 total = sc->siba_ncores * SIBA_CORE_LEN;
264 /* XXX Don't allocate the entire window until we
265 * enumerate the bus. Once the bus has been enumerated,
266 * and instance variables/children instantiated + populated,
267 * release the resource so children may attach.
269 sc->siba_mem_res = bus_alloc_resource(dev, SYS_RES_MEMORY, &rid,
270 sc->siba_maddr, sc->siba_maddr + total - 1, total, RF_ACTIVE);
271 if (sc->siba_mem_res == NULL) {
272 device_printf(dev, "unable to allocate entire aperture\n");
275 sc->siba_mem_bt = rman_get_bustag(sc->siba_mem_res);
276 sc->siba_mem_bh = rman_get_bushandle(sc->siba_mem_res);
277 sc->siba_maddr = rman_get_start(sc->siba_mem_res);
278 sc->siba_msize = rman_get_size(sc->siba_mem_res);
281 device_printf(dev, "after remapping: start %08x len %08x\n",
282 sc->siba_maddr, sc->siba_msize);
284 bus_set_resource(dev, SYS_RES_MEMORY, rid, sc->siba_maddr,
288 * We need a manager for the space we claim on nexus to
289 * satisfy requests from children.
290 * We need to keep the source reservation we took because
291 * otherwise it may be claimed elsewhere.
294 mem_rman.rm_start = sc->siba_maddr;
295 mem_rman.rm_end = sc->siba_maddr + sc->siba_msize - 1;
296 mem_rman.rm_type = RMAN_ARRAY;
297 mem_rman.rm_descr = "SiBa I/O memory addresses";
298 if (rman_init(&mem_rman) != 0 ||
299 rman_manage_region(&mem_rman, mem_rman.rm_start,
300 mem_rman.rm_end) != 0) {
301 panic("%s: mem_rman", __func__);
308 siba_attach(device_t dev)
310 struct siba_softc *sc = device_get_softc(dev);
311 struct siba_devinfo *sdi;
316 printf("%s: entry\n", __func__);
318 bus_generic_probe(dev);
321 * Now that all bus space is mapped and visible to the CPU,
322 * enumerate its children.
323 * NB: only one core may be mapped at any time if the siba bus
324 * is the child of a PCI or PCMCIA bus.
326 for (idx = 0; idx < sc->siba_ncores; idx++) {
327 sdi = siba_setup_devinfo(dev, idx);
328 child = device_add_child(dev, NULL, -1);
330 panic("%s: device_add_child() failed\n", __func__);
331 device_set_ivars(child, sdi);
334 return (bus_generic_attach(dev));
337 static struct siba_devid *
338 siba_dev_match(uint16_t vid, uint16_t devid, uint8_t rev)
341 struct siba_devid *sd;
343 bound = sizeof(siba_devids) / sizeof(struct siba_devid);
344 sd = &siba_devids[0];
345 for (i = 0; i < bound; i++, sd++) {
346 if (((vid == SIBA_VID_ANY) || (vid == sd->sd_vendor)) &&
347 ((devid == SIBA_DEVID_ANY) || (devid == sd->sd_device)) &&
348 ((rev == SIBA_REV_ANY) || (rev == sd->sd_rev) ||
349 (sd->sd_rev == SIBA_REV_ANY)))
359 siba_print_child(device_t bus, device_t child)
363 retval += bus_print_child_header(bus, child);
364 retval += siba_print_all_resources(child);
365 if (device_get_flags(child))
366 retval += printf(" flags %#x", device_get_flags(child));
367 retval += printf(" on %s\n", device_get_nameunit(bus));
372 static struct resource *
373 siba_alloc_resource(device_t bus, device_t child, int type, int *rid,
374 u_long start, u_long end, u_long count, u_int flags)
377 struct resource_list *rl;
378 struct resource_list_entry *rle;
379 int isdefault, needactivate;
383 printf("%s: entry\n", __func__);
386 isdefault = (start == 0UL && end == ~0UL && count == 1);
387 needactivate = flags & RF_ACTIVE;
388 rl = BUS_GET_RESOURCE_LIST(bus, child);
392 rle = resource_list_find(rl, type, *rid);
395 if (rle->res != NULL)
396 panic("%s: resource entry is busy", __func__);
403 * If the request is for a resource which we manage,
404 * attempt to satisfy the allocation ourselves.
406 if (type == SYS_RES_MEMORY &&
407 start >= mem_rman.rm_start && end <= mem_rman.rm_end) {
409 rv = rman_reserve_resource(&mem_rman, start, end, count,
412 printf("%s: could not reserve resource\n", __func__);
416 rman_set_rid(rv, *rid);
419 if (bus_activate_resource(child, type, *rid, rv)) {
420 printf("%s: could not activate resource\n",
422 rman_release_resource(rv);
431 * Pass the request to the parent, usually MIPS nexus.
434 printf("%s: proxying request to parent\n", __func__);
435 return (resource_list_alloc(rl, bus, child, type, rid,
436 start, end, count, flags));
440 * The parent bus is responsible for resource activation; in the
441 * case of MIPS, this boils down to setting the virtual address and
442 * bus handle by mapping the physical address into KSEG1.
445 siba_activate_resource(device_t bus, device_t child, int type, int rid,
449 return (BUS_ACTIVATE_RESOURCE(device_get_parent(bus), child, type,
453 static struct siba_devinfo *
454 siba_setup_devinfo(device_t dev, uint8_t idx)
456 struct siba_softc *sc = device_get_softc(dev);
457 struct siba_devinfo *sdi;
458 uint32_t idlo, idhi, rev;
459 uint16_t vendorid, devid;
462 sdi = malloc(sizeof(*sdi), M_DEVBUF, M_WAITOK | M_ZERO);
463 resource_list_init(&sdi->sdi_rl);
465 idlo = siba_mips_read_4(sc, idx, SIBA_IDLOW);
466 idhi = siba_mips_read_4(sc, idx, SIBA_IDHIGH);
468 vendorid = (idhi & SIBA_IDHIGH_VENDORMASK) >>
469 SIBA_IDHIGH_VENDOR_SHIFT;
470 devid = ((idhi & 0x8ff0) >> 4);
471 rev = (idhi & SIBA_IDHIGH_REVLO);
472 rev |= (idhi & SIBA_IDHIGH_REVHI) >> SIBA_IDHIGH_REVHI_SHIFT;
474 sdi->sdi_vid = vendorid;
475 sdi->sdi_devid = devid;
478 sdi->sdi_irq = siba_getirq(devid);
481 * Determine memory window on bus and irq if one is needed.
483 baseaddr = sc->siba_maddr + (idx * SIBA_CORE_LEN);
484 resource_list_add(&sdi->sdi_rl, SYS_RES_MEMORY,
485 MIPS_MEM_RID, /* XXX */
486 baseaddr, baseaddr + SIBA_CORE_LEN - 1, SIBA_CORE_LEN);
488 if (sdi->sdi_irq != 0xff) {
489 resource_list_add(&sdi->sdi_rl, SYS_RES_IRQ,
490 0, sdi->sdi_irq, sdi->sdi_irq, 1);
498 siba_destroy_devinfo(struct siba_devinfo *sdi)
501 resource_list_free(&sdi->sdi_rl);
506 /* XXX is this needed? */
508 siba_add_child(device_t dev, u_int order, const char *name, int unit)
512 device_printf(dev, "%s: entry\n", __func__);
516 struct siba_devinfo *sdi;
518 child = device_add_child_ordered(dev, order, name, unit);
522 sdi = malloc(sizeof(struct siba_devinfo), M_DEVBUF, M_NOWAIT|M_ZERO);
526 device_set_ivars(child, sdi);
532 siba_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
534 struct siba_devinfo *sdi;
536 sdi = device_get_ivars(child);
539 case SIBA_IVAR_VENDOR:
540 *result = sdi->sdi_vid;
542 case SIBA_IVAR_DEVICE:
543 *result = sdi->sdi_devid;
545 case SIBA_IVAR_REVID:
546 *result = sdi->sdi_rev;
548 case SIBA_IVAR_CORE_INDEX:
549 *result = sdi->sdi_idx;
559 siba_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
566 siba_probe_nomatch(device_t dev, device_t child)
570 * Announce devices which weren't attached after we probed the bus.
573 struct siba_devid *sd;
575 sd = siba_dev_match(siba_get_vendor(child),
576 siba_get_device(child), SIBA_REV_ANY);
577 if (sd != NULL && sd->sd_desc != NULL) {
578 device_printf(dev, "<%s> "
579 "at device %d (no driver attached)\n",
580 sd->sd_desc, siba_get_core_index(child));
582 device_printf(dev, "<0x%04x, 0x%04x> "
583 "at device %d (no driver attached)\n",
584 siba_get_vendor(child), siba_get_device(child),
585 siba_get_core_index(child));
591 siba_print_all_resources(device_t dev)
593 struct siba_devinfo *sdi = device_get_ivars(dev);
594 struct resource_list *rl = &sdi->sdi_rl;
597 if (STAILQ_FIRST(rl))
598 retval += printf(" at");
600 retval += resource_list_print_type(rl, "mem", SYS_RES_MEMORY, "%#lx");
601 retval += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%ld");
606 static struct resource_list *
607 siba_get_reslist(device_t dev, device_t child)
609 struct siba_devinfo *sdi = device_get_ivars(child);
611 return (&sdi->sdi_rl);
614 static device_method_t siba_methods[] = {
615 /* Device interface */
616 DEVMETHOD(device_attach, siba_attach),
617 DEVMETHOD(device_detach, bus_generic_detach),
618 DEVMETHOD(device_probe, siba_probe),
619 DEVMETHOD(device_resume, bus_generic_resume),
620 DEVMETHOD(device_shutdown, bus_generic_shutdown),
621 DEVMETHOD(device_suspend, bus_generic_suspend),
624 DEVMETHOD(bus_activate_resource,siba_activate_resource),
625 DEVMETHOD(bus_add_child, siba_add_child),
626 DEVMETHOD(bus_alloc_resource, siba_alloc_resource),
627 DEVMETHOD(bus_get_resource_list,siba_get_reslist),
628 DEVMETHOD(bus_print_child, siba_print_child),
629 DEVMETHOD(bus_probe_nomatch, siba_probe_nomatch),
630 DEVMETHOD(bus_read_ivar, siba_read_ivar),
631 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
632 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
633 DEVMETHOD(bus_write_ivar, siba_write_ivar),
638 static driver_t siba_driver = {
641 sizeof(struct siba_softc),
643 static devclass_t siba_devclass;
645 DRIVER_MODULE(siba, nexus, siba_driver, siba_devclass, 0, 0);