2 * Copyright (c) 2015-2016 Landon Fuller <landonf@FreeBSD.org>
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 * without modification.
11 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12 * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
13 * redistribution must be conditioned upon including a substantially
14 * similar Disclaimer requirement for further binary redistribution.
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
20 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21 * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
22 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
27 * THE POSSIBILITY OF SUCH DAMAGES.
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
34 * Broadcom Home Networking Division (HND) Bus Driver.
36 * The Broadcom HND family of devices consists of both SoCs and host-connected
37 * networking chipsets containing a common family of Broadcom IP cores,
38 * including an integrated MIPS and/or ARM cores.
40 * HND devices expose a nearly identical interface whether accessible over a
41 * native SoC interconnect, or when connected via a host interface such as
42 * PCIe. As a result, the majority of hardware support code should be re-usable
43 * across host drivers for HND networking chipsets, as well as FreeBSD support
44 * for Broadcom MIPS/ARM HND SoCs.
46 * Earlier HND models used the siba(4) on-chip interconnect, while later models
47 * use bcma(4); the programming model is almost entirely independent
48 * of the actual underlying interconect.
51 #include <sys/param.h>
52 #include <sys/kernel.h>
54 #include <sys/module.h>
55 #include <sys/systm.h>
57 #include <machine/bus.h>
59 #include <machine/resource.h>
61 #include <dev/bhnd/cores/chipc/chipcvar.h>
63 #include <dev/bhnd/cores/pmu/bhnd_pmu.h>
64 #include <dev/bhnd/cores/pmu/bhnd_pmureg.h>
66 #include "bhnd_chipc_if.h"
67 #include "bhnd_nvram_if.h"
72 MALLOC_DEFINE(M_BHND, "bhnd", "bhnd bus data structures");
74 /* Bus pass at which all bus-required children must be available, and
75 * attachment may be finalized. */
76 #define BHND_FINISH_ATTACH_PASS BUS_PASS_DEFAULT
79 * bhnd_generic_probe_nomatch() reporting configuration.
81 static const struct bhnd_nomatch {
82 uint16_t vendor; /**< core designer */
83 uint16_t device; /**< core id */
84 bool if_verbose; /**< print when bootverbose is set. */
85 } bhnd_nomatch_table[] = {
86 { BHND_MFGID_ARM, BHND_COREID_OOB_ROUTER, true },
87 { BHND_MFGID_ARM, BHND_COREID_EROM, true },
88 { BHND_MFGID_ARM, BHND_COREID_PL301, true },
89 { BHND_MFGID_ARM, BHND_COREID_APB_BRIDGE, true },
90 { BHND_MFGID_ARM, BHND_COREID_AXI_UNMAPPED, false },
92 { BHND_MFGID_INVALID, BHND_COREID_INVALID, false }
96 static int bhnd_delete_children(struct bhnd_softc *sc);
98 static int bhnd_finish_attach(struct bhnd_softc *sc);
100 static device_t bhnd_find_chipc(struct bhnd_softc *sc);
101 static struct chipc_caps *bhnd_find_chipc_caps(struct bhnd_softc *sc);
102 static device_t bhnd_find_platform_dev(struct bhnd_softc *sc,
103 const char *classname);
104 static device_t bhnd_find_pmu(struct bhnd_softc *sc);
105 static device_t bhnd_find_nvram(struct bhnd_softc *sc);
107 static int compare_ascending_probe_order(const void *lhs,
109 static int compare_descending_probe_order(const void *lhs,
113 * Default bhnd(4) bus driver implementation of DEVICE_ATTACH().
115 * This implementation calls device_probe_and_attach() for each of the device's
116 * children, in bhnd probe order.
119 bhnd_generic_attach(device_t dev)
121 struct bhnd_softc *sc;
126 if (device_is_attached(dev))
129 sc = device_get_softc(dev);
132 if ((error = device_get_children(dev, &devs, &ndevs)))
135 /* Probe and attach all children */
136 qsort(devs, ndevs, sizeof(*devs), compare_ascending_probe_order);
137 for (int i = 0; i < ndevs; i++) {
138 device_t child = devs[i];
139 device_probe_and_attach(child);
142 /* Try to finalize attachment */
143 if (bus_current_pass >= BHND_FINISH_ATTACH_PASS) {
144 if ((error = bhnd_finish_attach(sc)))
152 bhnd_delete_children(sc);
158 * Detach and delete all children, in reverse of their attach order.
161 bhnd_delete_children(struct bhnd_softc *sc)
167 if ((error = device_get_children(sc->dev, &devs, &ndevs)))
170 /* Detach in the reverse of attach order */
171 qsort(devs, ndevs, sizeof(*devs), compare_descending_probe_order);
172 for (int i = 0; i < ndevs; i++) {
173 device_t child = devs[i];
175 /* Terminate on first error */
176 if ((error = device_delete_child(sc->dev, child)))
186 * Default bhnd(4) bus driver implementation of DEVICE_DETACH().
188 * This implementation calls device_detach() for each of the device's
189 * children, in reverse bhnd probe order, terminating if any call to
190 * device_detach() fails.
193 bhnd_generic_detach(device_t dev)
195 struct bhnd_softc *sc;
197 if (!device_is_attached(dev))
200 sc = device_get_softc(dev);
201 return (bhnd_delete_children(sc));
205 * Default bhnd(4) bus driver implementation of DEVICE_SHUTDOWN().
207 * This implementation calls device_shutdown() for each of the device's
208 * children, in reverse bhnd probe order, terminating if any call to
209 * device_shutdown() fails.
212 bhnd_generic_shutdown(device_t dev)
218 if (!device_is_attached(dev))
221 if ((error = device_get_children(dev, &devs, &ndevs)))
224 /* Shutdown in the reverse of attach order */
225 qsort(devs, ndevs, sizeof(*devs), compare_descending_probe_order);
226 for (int i = 0; i < ndevs; i++) {
227 device_t child = devs[i];
229 /* Terminate on first error */
230 if ((error = device_shutdown(child)))
240 * Default bhnd(4) bus driver implementation of DEVICE_RESUME().
242 * This implementation calls BUS_RESUME_CHILD() for each of the device's
243 * children in bhnd probe order, terminating if any call to BUS_RESUME_CHILD()
247 bhnd_generic_resume(device_t dev)
253 if (!device_is_attached(dev))
256 if ((error = device_get_children(dev, &devs, &ndevs)))
259 qsort(devs, ndevs, sizeof(*devs), compare_ascending_probe_order);
260 for (int i = 0; i < ndevs; i++) {
261 device_t child = devs[i];
263 /* Terminate on first error */
264 if ((error = BUS_RESUME_CHILD(device_get_parent(child), child)))
274 * Default bhnd(4) bus driver implementation of DEVICE_SUSPEND().
276 * This implementation calls BUS_SUSPEND_CHILD() for each of the device's
277 * children in reverse bhnd probe order. If any call to BUS_SUSPEND_CHILD()
278 * fails, the suspend operation is terminated and any devices that were
279 * suspended are resumed immediately by calling their BUS_RESUME_CHILD()
283 bhnd_generic_suspend(device_t dev)
289 if (!device_is_attached(dev))
292 if ((error = device_get_children(dev, &devs, &ndevs)))
295 /* Suspend in the reverse of attach order */
296 qsort(devs, ndevs, sizeof(*devs), compare_descending_probe_order);
297 for (int i = 0; i < ndevs; i++) {
298 device_t child = devs[i];
299 error = BUS_SUSPEND_CHILD(device_get_parent(child), child);
301 /* On error, resume suspended devices and then terminate */
303 for (int j = 0; j < i; j++) {
304 BUS_RESUME_CHILD(device_get_parent(devs[j]),
318 bhnd_new_pass(device_t dev)
320 struct bhnd_softc *sc;
323 sc = device_get_softc(dev);
325 /* Attach any permissible children */
326 bus_generic_new_pass(dev);
328 /* Finalize attachment */
329 if (!sc->attach_done && bus_current_pass >= BHND_FINISH_ATTACH_PASS) {
330 if ((error = bhnd_finish_attach(sc))) {
331 panic("bhnd_finish_attach() failed: %d", error);
337 * Finish any pending bus attachment operations.
339 * When attached as a SoC bus (as opposed to a bridged WiFi device), our
340 * platform devices may not be attached until later bus passes, necessitating
341 * delayed initialization on our part.
344 bhnd_finish_attach(struct bhnd_softc *sc)
346 struct chipc_caps *ccaps;
348 GIANT_REQUIRED; /* for newbus */
350 KASSERT(bus_current_pass >= BHND_FINISH_ATTACH_PASS,
351 ("bhnd_finish_attach() called in pass %d", bus_current_pass));
353 KASSERT(!sc->attach_done, ("duplicate call to bhnd_finish_attach()"));
355 /* Locate chipc device */
356 if ((sc->chipc_dev = bhnd_find_chipc(sc)) == NULL) {
357 device_printf(sc->dev, "error: ChipCommon device not found\n");
361 ccaps = BHND_CHIPC_GET_CAPS(sc->chipc_dev);
363 /* Look for NVRAM device */
364 if (ccaps->nvram_src != BHND_NVRAM_SRC_UNKNOWN) {
365 if ((sc->nvram_dev = bhnd_find_nvram(sc)) == NULL) {
366 device_printf(sc->dev,
367 "warning: NVRAM %s device not found\n",
368 bhnd_nvram_src_name(ccaps->nvram_src));
373 if (ccaps->pmu || ccaps->pwr_ctrl) {
374 if ((sc->pmu_dev = bhnd_find_pmu(sc)) == NULL) {
375 device_printf(sc->dev,
376 "attach failed: supported PMU not found\n");
381 /* Mark attach as completed */
382 sc->attach_done = true;
387 /* Locate the ChipCommon core. */
389 bhnd_find_chipc(struct bhnd_softc *sc)
393 /* Make sure we're holding Giant for newbus */
396 /* chipc_dev is initialized during attachment */
397 if (sc->attach_done) {
398 if ((chipc = sc->chipc_dev) == NULL)
404 /* Locate chipc core with a core unit of 0 */
405 chipc = bhnd_find_child(sc->dev, BHND_DEVCLASS_CC, 0);
410 if (device_get_state(chipc) < DS_ATTACHING) {
411 device_printf(sc->dev, "chipc found, but did not attach\n");
418 /* Locate the ChipCommon core and return the device capabilities */
419 static struct chipc_caps *
420 bhnd_find_chipc_caps(struct bhnd_softc *sc)
424 if ((chipc = bhnd_find_chipc(sc)) == NULL) {
425 device_printf(sc->dev,
426 "chipc unavailable; cannot fetch capabilities\n");
430 return (BHND_CHIPC_GET_CAPS(chipc));
434 * Find an attached platform device on @p dev, searching first for cores
435 * matching @p classname, and if not found, searching the children of the first
436 * bhnd_chipc device on the bus.
438 * @param sc Driver state.
439 * @param chipc Attached ChipCommon device.
440 * @param classname Device class to search for.
442 * @retval device_t A matching device.
443 * @retval NULL If no matching device is found.
446 bhnd_find_platform_dev(struct bhnd_softc *sc, const char *classname)
448 device_t chipc, child;
450 /* Make sure we're holding Giant for newbus */
453 /* Look for a directly-attached child */
454 child = device_find_child(sc->dev, classname, -1);
458 /* Look for the first matching ChipCommon child */
459 if ((chipc = bhnd_find_chipc(sc)) == NULL) {
460 device_printf(sc->dev,
461 "chipc unavailable; cannot locate %s\n", classname);
465 child = device_find_child(chipc, classname, -1);
469 /* Look for a parent-attached device (e.g. nexus0 -> bhnd_nvram) */
470 child = device_find_child(device_get_parent(sc->dev), classname, -1);
475 if (device_get_state(child) < DS_ATTACHING)
481 /* Locate the PMU device, if any */
483 bhnd_find_pmu(struct bhnd_softc *sc)
485 /* Make sure we're holding Giant for newbus */
488 /* pmu_dev is initialized during attachment */
489 if (sc->attach_done) {
490 if (sc->pmu_dev == NULL)
493 if (device_get_state(sc->pmu_dev) < DS_ATTACHING)
496 return (sc->pmu_dev);
500 return (bhnd_find_platform_dev(sc, "bhnd_pmu"));
503 /* Locate the NVRAM device, if any */
505 bhnd_find_nvram(struct bhnd_softc *sc)
507 struct chipc_caps *ccaps;
509 /* Make sure we're holding Giant for newbus */
513 /* nvram_dev is initialized during attachment */
514 if (sc->attach_done) {
515 if (sc->nvram_dev == NULL)
518 if (device_get_state(sc->nvram_dev) < DS_ATTACHING)
521 return (sc->nvram_dev);
524 if ((ccaps = bhnd_find_chipc_caps(sc)) == NULL)
527 if (ccaps->nvram_src == BHND_NVRAM_SRC_UNKNOWN)
530 return (bhnd_find_platform_dev(sc, "bhnd_nvram"));
534 * Ascending comparison of bhnd device's probe order.
537 compare_ascending_probe_order(const void *lhs, const void *rhs)
542 ldev = (*(const device_t *) lhs);
543 rdev = (*(const device_t *) rhs);
545 lorder = BHND_BUS_GET_PROBE_ORDER(device_get_parent(ldev), ldev);
546 rorder = BHND_BUS_GET_PROBE_ORDER(device_get_parent(rdev), rdev);
548 if (lorder < rorder) {
550 } else if (lorder > rorder) {
558 * Descending comparison of bhnd device's probe order.
561 compare_descending_probe_order(const void *lhs, const void *rhs)
563 return (compare_ascending_probe_order(rhs, lhs));
567 * Default bhnd(4) bus driver implementation of BHND_BUS_GET_PROBE_ORDER().
569 * This implementation determines probe ordering based on the device's class
570 * and other properties, including whether the device is serving as a host
574 bhnd_generic_get_probe_order(device_t dev, device_t child)
576 switch (bhnd_get_class(child)) {
577 case BHND_DEVCLASS_CC:
578 /* Must be early enough to provide NVRAM access to the
580 return (BHND_PROBE_ROOT + BHND_PROBE_ORDER_FIRST);
582 case BHND_DEVCLASS_CC_B:
584 case BHND_DEVCLASS_PMU:
585 return (BHND_PROBE_BUS + BHND_PROBE_ORDER_EARLY);
587 case BHND_DEVCLASS_SOC_ROUTER:
588 return (BHND_PROBE_BUS + BHND_PROBE_ORDER_LATE);
590 case BHND_DEVCLASS_SOC_BRIDGE:
591 return (BHND_PROBE_BUS + BHND_PROBE_ORDER_LAST);
593 case BHND_DEVCLASS_CPU:
594 return (BHND_PROBE_CPU + BHND_PROBE_ORDER_FIRST);
596 case BHND_DEVCLASS_RAM:
598 case BHND_DEVCLASS_MEMC:
599 return (BHND_PROBE_CPU + BHND_PROBE_ORDER_EARLY);
601 case BHND_DEVCLASS_NVRAM:
602 return (BHND_PROBE_RESOURCE + BHND_PROBE_ORDER_EARLY);
604 case BHND_DEVCLASS_PCI:
605 case BHND_DEVCLASS_PCIE:
606 case BHND_DEVCLASS_PCCARD:
607 case BHND_DEVCLASS_ENET:
608 case BHND_DEVCLASS_ENET_MAC:
609 case BHND_DEVCLASS_ENET_PHY:
610 case BHND_DEVCLASS_WLAN:
611 case BHND_DEVCLASS_WLAN_MAC:
612 case BHND_DEVCLASS_WLAN_PHY:
613 case BHND_DEVCLASS_EROM:
614 case BHND_DEVCLASS_OTHER:
615 case BHND_DEVCLASS_INVALID:
616 if (bhnd_find_hostb_device(dev) == child)
617 return (BHND_PROBE_ROOT + BHND_PROBE_ORDER_EARLY);
619 return (BHND_PROBE_DEFAULT);
621 return (BHND_PROBE_DEFAULT);
626 * Default bhnd(4) bus driver implementation of BHND_BUS_ALLOC_PMU().
629 bhnd_generic_alloc_pmu(device_t dev, device_t child)
631 struct bhnd_softc *sc;
632 struct bhnd_resource *br;
633 struct chipc_caps *ccaps;
634 struct bhnd_core_pmu_info *pm;
635 struct resource_list *rl;
636 struct resource_list_entry *rle;
643 GIANT_REQUIRED; /* for newbus */
645 sc = device_get_softc(dev);
646 pm = bhnd_get_pmu_info(child);
647 pmu_regs = BHND_CLK_CTL_ST;
649 if ((ccaps = bhnd_find_chipc_caps(sc)) == NULL) {
650 device_printf(sc->dev, "alloc_pmu failed: chipc "
651 "capabilities unavailable\n");
655 if ((pmu_dev = bhnd_find_pmu(sc)) == NULL) {
656 device_printf(sc->dev,
657 "pmu unavailable; cannot allocate request state\n");
661 /* already allocated? */
663 panic("duplicate PMU allocation for %s",
664 device_get_nameunit(child));
667 /* Determine address+size of the core's PMU register block */
668 error = bhnd_get_region_addr(child, BHND_PORT_DEVICE, 0, 0, &r_addr,
671 device_printf(sc->dev, "error fetching register block info for "
672 "%s: %d\n", device_get_nameunit(child), error);
676 if (r_size < (pmu_regs + sizeof(uint32_t))) {
677 device_printf(sc->dev, "pmu offset %#jx would overrun %s "
678 "register block\n", (uintmax_t)pmu_regs,
679 device_get_nameunit(child));
683 /* Locate actual resource containing the core's register block */
684 if ((rl = BUS_GET_RESOURCE_LIST(dev, child)) == NULL) {
685 device_printf(dev, "NULL resource list returned for %s\n",
686 device_get_nameunit(child));
690 if ((rle = resource_list_find(rl, SYS_RES_MEMORY, 0)) == NULL) {
691 device_printf(dev, "cannot locate core register resource "
692 "for %s\n", device_get_nameunit(child));
696 if (rle->res == NULL) {
697 device_printf(dev, "core register resource unallocated for "
698 "%s\n", device_get_nameunit(child));
702 if (r_addr+pmu_regs < rman_get_start(rle->res) ||
703 r_addr+pmu_regs >= rman_get_end(rle->res))
705 device_printf(dev, "core register resource does not map PMU "
706 "registers at %#jx\n for %s\n", r_addr+pmu_regs,
707 device_get_nameunit(child));
711 /* Adjust PMU register offset relative to the actual start address
712 * of the core's register block allocation.
714 * XXX: The saved offset will be invalid if bus_adjust_resource is
715 * used to modify the resource's start address.
717 if (rman_get_start(rle->res) > r_addr)
718 pmu_regs -= rman_get_start(rle->res) - r_addr;
720 pmu_regs -= r_addr - rman_get_start(rle->res);
722 /* Allocate and initialize PMU info */
723 br = malloc(sizeof(struct bhnd_resource), M_BHND, M_NOWAIT);
728 br->direct = ((rman_get_flags(rle->res) & RF_ACTIVE) != 0);
730 pm = malloc(sizeof(*pm), M_BHND, M_NOWAIT);
736 pm->pm_pmu = pmu_dev;
738 pm->pm_regs = pmu_regs;
740 bhnd_set_pmu_info(child, pm);
745 * Default bhnd(4) bus driver implementation of BHND_BUS_RELEASE_PMU().
748 bhnd_generic_release_pmu(device_t dev, device_t child)
750 struct bhnd_softc *sc;
751 struct bhnd_core_pmu_info *pm;
755 GIANT_REQUIRED; /* for newbus */
757 sc = device_get_softc(dev);
759 if ((pmu = bhnd_find_pmu(sc)) == NULL) {
760 device_printf(sc->dev,
761 "pmu unavailable; cannot release request state\n");
765 /* dispatch release request */
766 pm = bhnd_get_pmu_info(child);
768 panic("pmu over-release for %s", device_get_nameunit(child));
770 if ((error = BHND_PMU_CORE_RELEASE(pmu, pm)))
774 bhnd_set_pmu_info(child, NULL);
775 free(pm->pm_res, M_BHND);
782 * Default bhnd(4) bus driver implementation of BHND_BUS_REQUEST_CLOCK().
785 bhnd_generic_request_clock(device_t dev, device_t child, bhnd_clock clock)
787 struct bhnd_softc *sc;
788 struct bhnd_core_pmu_info *pm;
790 sc = device_get_softc(dev);
792 if ((pm = bhnd_get_pmu_info(child)) == NULL)
793 panic("no active PMU request state");
795 /* dispatch request to PMU */
796 return (BHND_PMU_CORE_REQ_CLOCK(pm->pm_pmu, pm, clock));
800 * Default bhnd(4) bus driver implementation of BHND_BUS_ENABLE_CLOCKS().
803 bhnd_generic_enable_clocks(device_t dev, device_t child, uint32_t clocks)
805 struct bhnd_softc *sc;
806 struct bhnd_core_pmu_info *pm;
808 sc = device_get_softc(dev);
810 if ((pm = bhnd_get_pmu_info(child)) == NULL)
811 panic("no active PMU request state");
813 /* dispatch request to PMU */
814 return (BHND_PMU_CORE_EN_CLOCKS(pm->pm_pmu, pm, clocks));
818 * Default bhnd(4) bus driver implementation of BHND_BUS_REQUEST_EXT_RSRC().
821 bhnd_generic_request_ext_rsrc(device_t dev, device_t child, u_int rsrc)
823 struct bhnd_softc *sc;
824 struct bhnd_core_pmu_info *pm;
826 sc = device_get_softc(dev);
828 if ((pm = bhnd_get_pmu_info(child)) == NULL)
829 panic("no active PMU request state");
831 /* dispatch request to PMU */
832 return (BHND_PMU_CORE_REQ_EXT_RSRC(pm->pm_pmu, pm, rsrc));
836 * Default bhnd(4) bus driver implementation of BHND_BUS_RELEASE_EXT_RSRC().
839 bhnd_generic_release_ext_rsrc(device_t dev, device_t child, u_int rsrc)
841 struct bhnd_softc *sc;
842 struct bhnd_core_pmu_info *pm;
844 sc = device_get_softc(dev);
846 if ((pm = bhnd_get_pmu_info(child)) == NULL)
847 panic("no active PMU request state");
849 /* dispatch request to PMU */
850 return (BHND_PMU_CORE_RELEASE_EXT_RSRC(pm->pm_pmu, pm, rsrc));
855 * Default bhnd(4) bus driver implementation of BHND_BUS_IS_REGION_VALID().
857 * This implementation assumes that port and region numbers are 0-indexed and
858 * are allocated non-sparsely, using BHND_BUS_GET_PORT_COUNT() and
859 * BHND_BUS_GET_REGION_COUNT() to determine if @p port and @p region fall
860 * within the defined range.
863 bhnd_generic_is_region_valid(device_t dev, device_t child,
864 bhnd_port_type type, u_int port, u_int region)
866 if (port >= bhnd_get_port_count(child, type))
869 if (region >= bhnd_get_region_count(child, type, port))
876 * Default bhnd(4) bus driver implementation of BHND_BUS_GET_NVRAM_VAR().
878 * This implementation searches @p dev for a usable NVRAM child device.
880 * If no usable child device is found on @p dev, the request is delegated to
881 * the BHND_BUS_GET_NVRAM_VAR() method on the parent of @p dev.
884 bhnd_generic_get_nvram_var(device_t dev, device_t child, const char *name,
885 void *buf, size_t *size, bhnd_nvram_type type)
887 struct bhnd_softc *sc;
888 device_t nvram, parent;
890 sc = device_get_softc(dev);
892 /* If a NVRAM device is available, consult it first */
893 if ((nvram = bhnd_find_nvram(sc)) != NULL)
894 return BHND_NVRAM_GETVAR(nvram, name, buf, size, type);
896 /* Otherwise, try to delegate to parent */
897 if ((parent = device_get_parent(dev)) == NULL)
900 return (BHND_BUS_GET_NVRAM_VAR(device_get_parent(dev), child,
901 name, buf, size, type));
905 * Default bhnd(4) bus driver implementation of BUS_PRINT_CHILD().
907 * This implementation requests the device's struct resource_list via
908 * BUS_GET_RESOURCE_LIST.
911 bhnd_generic_print_child(device_t dev, device_t child)
913 struct resource_list *rl;
916 retval += bus_print_child_header(dev, child);
918 rl = BUS_GET_RESOURCE_LIST(dev, child);
922 retval += resource_list_print_type(rl, "mem", SYS_RES_MEMORY,
925 retval += resource_list_print_type(rl, "irq", SYS_RES_IRQ,
929 retval += printf(" at core %u", bhnd_get_core_index(child));
931 retval += bus_print_child_domain(dev, child);
932 retval += bus_print_child_footer(dev, child);
938 * Default bhnd(4) bus driver implementation of BUS_PROBE_NOMATCH().
940 * This implementation requests the device's struct resource_list via
941 * BUS_GET_RESOURCE_LIST.
944 bhnd_generic_probe_nomatch(device_t dev, device_t child)
946 struct resource_list *rl;
947 const struct bhnd_nomatch *nm;
950 /* Fetch reporting configuration for this device */
952 for (nm = bhnd_nomatch_table; nm->device != BHND_COREID_INVALID; nm++) {
953 if (nm->vendor != bhnd_get_vendor(child))
956 if (nm->device != bhnd_get_device(child))
960 if (bootverbose && nm->if_verbose)
968 /* Print the non-matched device info */
969 device_printf(dev, "<%s %s>", bhnd_get_vendor_name(child),
970 bhnd_get_device_name(child));
972 rl = BUS_GET_RESOURCE_LIST(dev, child);
974 resource_list_print_type(rl, "mem", SYS_RES_MEMORY, "%#jx");
975 resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%#jd");
978 printf(" at core %u (no driver attached)\n",
979 bhnd_get_core_index(child));
983 * Default implementation of BUS_CHILD_PNPINFO_STR().
986 bhnd_child_pnpinfo_str(device_t dev, device_t child, char *buf,
989 if (device_get_parent(child) != dev) {
990 return (BUS_CHILD_PNPINFO_STR(device_get_parent(dev), child,
994 snprintf(buf, buflen, "vendor=0x%hx device=0x%hx rev=0x%hhx",
995 bhnd_get_vendor(child), bhnd_get_device(child),
996 bhnd_get_hwrev(child));
1002 * Default implementation of BUS_CHILD_LOCATION_STR().
1005 bhnd_child_location_str(device_t dev, device_t child, char *buf,
1011 if (device_get_parent(child) != dev) {
1012 return (BUS_CHILD_LOCATION_STR(device_get_parent(dev), child,
1017 if (bhnd_get_region_addr(child, BHND_PORT_DEVICE, 0, 0, &addr, &size)) {
1018 /* No device default port/region */
1024 snprintf(buf, buflen, "port0.0=0x%llx", (unsigned long long) addr);
1029 * Default bhnd(4) bus driver implementation of BUS_CHILD_DELETED().
1031 * This implementation manages internal bhnd(4) state, and must be called
1032 * by subclassing drivers.
1035 bhnd_generic_child_deleted(device_t dev, device_t child)
1037 struct bhnd_softc *sc;
1039 sc = device_get_softc(dev);
1041 /* Free device info */
1042 if (bhnd_get_pmu_info(child) != NULL) {
1043 /* Releasing PMU requests automatically would be nice,
1044 * but we can't reference per-core PMU register
1045 * resource after driver detach */
1046 panic("%s leaked device pmu state\n",
1047 device_get_nameunit(child));
1050 /* Clean up platform device references */
1051 if (sc->chipc_dev == child) {
1052 sc->chipc_dev = NULL;
1053 } else if (sc->nvram_dev == child) {
1054 sc->nvram_dev = NULL;
1055 } else if (sc->pmu_dev == child) {
1061 * Helper function for implementing BUS_SUSPEND_CHILD().
1063 * TODO: Power management
1065 * If @p child is not a direct child of @p dev, suspension is delegated to
1066 * the @p dev parent.
1069 bhnd_generic_suspend_child(device_t dev, device_t child)
1071 if (device_get_parent(child) != dev)
1072 BUS_SUSPEND_CHILD(device_get_parent(dev), child);
1074 return bus_generic_suspend_child(dev, child);
1078 * Helper function for implementing BUS_RESUME_CHILD().
1080 * TODO: Power management
1082 * If @p child is not a direct child of @p dev, suspension is delegated to
1083 * the @p dev parent.
1086 bhnd_generic_resume_child(device_t dev, device_t child)
1088 if (device_get_parent(child) != dev)
1089 BUS_RESUME_CHILD(device_get_parent(dev), child);
1091 return bus_generic_resume_child(dev, child);
1095 * Delegate all indirect I/O to the parent device. When inherited by
1096 * non-bridged bus implementations, resources will never be marked as
1097 * indirect, and these methods will never be called.
1099 #define BHND_IO_READ(_type, _name, _method) \
1101 bhnd_read_ ## _name (device_t dev, device_t child, \
1102 struct bhnd_resource *r, bus_size_t offset) \
1104 return (BHND_BUS_READ_ ## _method( \
1105 device_get_parent(dev), child, r, offset)); \
1108 #define BHND_IO_WRITE(_type, _name, _method) \
1110 bhnd_write_ ## _name (device_t dev, device_t child, \
1111 struct bhnd_resource *r, bus_size_t offset, _type value) \
1113 return (BHND_BUS_WRITE_ ## _method( \
1114 device_get_parent(dev), child, r, offset, \
1118 #define BHND_IO_MISC(_type, _op, _method) \
1120 bhnd_ ## _op (device_t dev, device_t child, \
1121 struct bhnd_resource *r, bus_size_t offset, _type datap, \
1124 BHND_BUS_ ## _method(device_get_parent(dev), child, r, \
1125 offset, datap, count); \
1128 #define BHND_IO_METHODS(_type, _size) \
1129 BHND_IO_READ(_type, _size, _size) \
1130 BHND_IO_WRITE(_type, _size, _size) \
1132 BHND_IO_READ(_type, stream_ ## _size, STREAM_ ## _size) \
1133 BHND_IO_WRITE(_type, stream_ ## _size, STREAM_ ## _size) \
1135 BHND_IO_MISC(_type*, read_multi_ ## _size, \
1136 READ_MULTI_ ## _size) \
1137 BHND_IO_MISC(_type*, write_multi_ ## _size, \
1138 WRITE_MULTI_ ## _size) \
1140 BHND_IO_MISC(_type*, read_multi_stream_ ## _size, \
1141 READ_MULTI_STREAM_ ## _size) \
1142 BHND_IO_MISC(_type*, write_multi_stream_ ## _size, \
1143 WRITE_MULTI_STREAM_ ## _size) \
1145 BHND_IO_MISC(_type, set_multi_ ## _size, SET_MULTI_ ## _size) \
1146 BHND_IO_MISC(_type, set_region_ ## _size, SET_REGION_ ## _size) \
1148 BHND_IO_MISC(_type*, read_region_ ## _size, \
1149 READ_REGION_ ## _size) \
1150 BHND_IO_MISC(_type*, write_region_ ## _size, \
1151 WRITE_REGION_ ## _size) \
1153 BHND_IO_MISC(_type*, read_region_stream_ ## _size, \
1154 READ_REGION_STREAM_ ## _size) \
1155 BHND_IO_MISC(_type*, write_region_stream_ ## _size, \
1156 WRITE_REGION_STREAM_ ## _size) \
1158 BHND_IO_METHODS(uint8_t, 1);
1159 BHND_IO_METHODS(uint16_t, 2);
1160 BHND_IO_METHODS(uint32_t, 4);
1163 bhnd_barrier(device_t dev, device_t child, struct bhnd_resource *r,
1164 bus_size_t offset, bus_size_t length, int flags)
1166 BHND_BUS_BARRIER(device_get_parent(dev), child, r, offset, length,
1170 static device_method_t bhnd_methods[] = {
1171 /* Device interface */ \
1172 DEVMETHOD(device_attach, bhnd_generic_attach),
1173 DEVMETHOD(device_detach, bhnd_generic_detach),
1174 DEVMETHOD(device_shutdown, bhnd_generic_shutdown),
1175 DEVMETHOD(device_suspend, bhnd_generic_suspend),
1176 DEVMETHOD(device_resume, bhnd_generic_resume),
1179 DEVMETHOD(bus_new_pass, bhnd_new_pass),
1180 DEVMETHOD(bus_child_deleted, bhnd_generic_child_deleted),
1181 DEVMETHOD(bus_probe_nomatch, bhnd_generic_probe_nomatch),
1182 DEVMETHOD(bus_print_child, bhnd_generic_print_child),
1183 DEVMETHOD(bus_child_pnpinfo_str, bhnd_child_pnpinfo_str),
1184 DEVMETHOD(bus_child_location_str, bhnd_child_location_str),
1186 DEVMETHOD(bus_suspend_child, bhnd_generic_suspend_child),
1187 DEVMETHOD(bus_resume_child, bhnd_generic_resume_child),
1189 DEVMETHOD(bus_set_resource, bus_generic_rl_set_resource),
1190 DEVMETHOD(bus_get_resource, bus_generic_rl_get_resource),
1191 DEVMETHOD(bus_delete_resource, bus_generic_rl_delete_resource),
1192 DEVMETHOD(bus_alloc_resource, bus_generic_rl_alloc_resource),
1193 DEVMETHOD(bus_adjust_resource, bus_generic_adjust_resource),
1194 DEVMETHOD(bus_release_resource, bus_generic_rl_release_resource),
1195 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
1196 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
1198 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
1199 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
1200 DEVMETHOD(bus_config_intr, bus_generic_config_intr),
1201 DEVMETHOD(bus_bind_intr, bus_generic_bind_intr),
1202 DEVMETHOD(bus_describe_intr, bus_generic_describe_intr),
1204 DEVMETHOD(bus_get_dma_tag, bus_generic_get_dma_tag),
1206 /* BHND interface */
1207 DEVMETHOD(bhnd_bus_get_chipid, bhnd_bus_generic_get_chipid),
1208 DEVMETHOD(bhnd_bus_is_hw_disabled, bhnd_bus_generic_is_hw_disabled),
1209 DEVMETHOD(bhnd_bus_read_board_info, bhnd_bus_generic_read_board_info),
1211 DEVMETHOD(bhnd_bus_get_probe_order, bhnd_generic_get_probe_order),
1213 DEVMETHOD(bhnd_bus_alloc_pmu, bhnd_generic_alloc_pmu),
1214 DEVMETHOD(bhnd_bus_release_pmu, bhnd_generic_release_pmu),
1215 DEVMETHOD(bhnd_bus_request_clock, bhnd_generic_request_clock),
1216 DEVMETHOD(bhnd_bus_enable_clocks, bhnd_generic_enable_clocks),
1217 DEVMETHOD(bhnd_bus_request_ext_rsrc, bhnd_generic_request_ext_rsrc),
1218 DEVMETHOD(bhnd_bus_release_ext_rsrc, bhnd_generic_release_ext_rsrc),
1220 DEVMETHOD(bhnd_bus_is_region_valid, bhnd_generic_is_region_valid),
1221 DEVMETHOD(bhnd_bus_get_nvram_var, bhnd_generic_get_nvram_var),
1223 /* BHND interface (bus I/O) */
1224 DEVMETHOD(bhnd_bus_read_1, bhnd_read_1),
1225 DEVMETHOD(bhnd_bus_read_2, bhnd_read_2),
1226 DEVMETHOD(bhnd_bus_read_4, bhnd_read_4),
1227 DEVMETHOD(bhnd_bus_write_1, bhnd_write_1),
1228 DEVMETHOD(bhnd_bus_write_2, bhnd_write_2),
1229 DEVMETHOD(bhnd_bus_write_4, bhnd_write_4),
1231 DEVMETHOD(bhnd_bus_read_stream_1, bhnd_read_stream_1),
1232 DEVMETHOD(bhnd_bus_read_stream_2, bhnd_read_stream_2),
1233 DEVMETHOD(bhnd_bus_read_stream_4, bhnd_read_stream_4),
1234 DEVMETHOD(bhnd_bus_write_stream_1, bhnd_write_stream_1),
1235 DEVMETHOD(bhnd_bus_write_stream_2, bhnd_write_stream_2),
1236 DEVMETHOD(bhnd_bus_write_stream_4, bhnd_write_stream_4),
1238 DEVMETHOD(bhnd_bus_read_multi_1, bhnd_read_multi_1),
1239 DEVMETHOD(bhnd_bus_read_multi_2, bhnd_read_multi_2),
1240 DEVMETHOD(bhnd_bus_read_multi_4, bhnd_read_multi_4),
1241 DEVMETHOD(bhnd_bus_write_multi_1, bhnd_write_multi_1),
1242 DEVMETHOD(bhnd_bus_write_multi_2, bhnd_write_multi_2),
1243 DEVMETHOD(bhnd_bus_write_multi_4, bhnd_write_multi_4),
1245 DEVMETHOD(bhnd_bus_read_multi_stream_1, bhnd_read_multi_stream_1),
1246 DEVMETHOD(bhnd_bus_read_multi_stream_2, bhnd_read_multi_stream_2),
1247 DEVMETHOD(bhnd_bus_read_multi_stream_4, bhnd_read_multi_stream_4),
1248 DEVMETHOD(bhnd_bus_write_multi_stream_1,bhnd_write_multi_stream_1),
1249 DEVMETHOD(bhnd_bus_write_multi_stream_2,bhnd_write_multi_stream_2),
1250 DEVMETHOD(bhnd_bus_write_multi_stream_4,bhnd_write_multi_stream_4),
1252 DEVMETHOD(bhnd_bus_set_multi_1, bhnd_set_multi_1),
1253 DEVMETHOD(bhnd_bus_set_multi_2, bhnd_set_multi_2),
1254 DEVMETHOD(bhnd_bus_set_multi_4, bhnd_set_multi_4),
1256 DEVMETHOD(bhnd_bus_set_region_1, bhnd_set_region_1),
1257 DEVMETHOD(bhnd_bus_set_region_2, bhnd_set_region_2),
1258 DEVMETHOD(bhnd_bus_set_region_4, bhnd_set_region_4),
1260 DEVMETHOD(bhnd_bus_read_region_1, bhnd_read_region_1),
1261 DEVMETHOD(bhnd_bus_read_region_2, bhnd_read_region_2),
1262 DEVMETHOD(bhnd_bus_read_region_4, bhnd_read_region_4),
1263 DEVMETHOD(bhnd_bus_write_region_1, bhnd_write_region_1),
1264 DEVMETHOD(bhnd_bus_write_region_2, bhnd_write_region_2),
1265 DEVMETHOD(bhnd_bus_write_region_4, bhnd_write_region_4),
1267 DEVMETHOD(bhnd_bus_read_region_stream_1,bhnd_read_region_stream_1),
1268 DEVMETHOD(bhnd_bus_read_region_stream_2,bhnd_read_region_stream_2),
1269 DEVMETHOD(bhnd_bus_read_region_stream_4,bhnd_read_region_stream_4),
1270 DEVMETHOD(bhnd_bus_write_region_stream_1, bhnd_write_region_stream_1),
1271 DEVMETHOD(bhnd_bus_write_region_stream_2, bhnd_write_region_stream_2),
1272 DEVMETHOD(bhnd_bus_write_region_stream_4, bhnd_write_region_stream_4),
1274 DEVMETHOD(bhnd_bus_barrier, bhnd_barrier),
1279 devclass_t bhnd_devclass; /**< bhnd bus. */
1280 devclass_t bhnd_hostb_devclass; /**< bhnd bus host bridge. */
1281 devclass_t bhnd_nvram_devclass; /**< bhnd NVRAM device */
1283 DEFINE_CLASS_0(bhnd, bhnd_driver, bhnd_methods, sizeof(struct bhnd_softc));
1284 MODULE_VERSION(bhnd, 1);