2 * Copyright (c) 2015 Landon Fuller <landon@landonf.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$");
33 #include <sys/types.h>
34 #include <sys/param.h>
36 #include <sys/systm.h>
38 #include <machine/bus.h>
40 #include <machine/resource.h>
42 #include <dev/bhnd/cores/chipc/chipcreg.h>
44 #include "nvram/bhnd_nvram.h"
46 #include "bhnd_chipc_if.h"
48 #include "bhnd_nvram_if.h"
49 #include "bhnd_nvram_map.h"
54 static device_t find_nvram_child(device_t dev);
56 /* BHND core device description table. */
57 static const struct bhnd_core_desc {
60 bhnd_devclass_t class;
62 } bhnd_core_descs[] = {
63 #define BHND_CDESC(_mfg, _cid, _cls, _desc) \
64 { BHND_MFGID_ ## _mfg, BHND_COREID_ ## _cid, \
65 BHND_DEVCLASS_ ## _cls, _desc }
67 BHND_CDESC(BCM, CC, CC, "ChipCommon I/O Controller"),
68 BHND_CDESC(BCM, ILINE20, OTHER, "iLine20 HPNA"),
69 BHND_CDESC(BCM, SRAM, RAM, "SRAM"),
70 BHND_CDESC(BCM, SDRAM, RAM, "SDRAM"),
71 BHND_CDESC(BCM, PCI, PCI, "PCI Bridge"),
72 BHND_CDESC(BCM, MIPS, CPU, "MIPS Core"),
73 BHND_CDESC(BCM, ENET, ENET_MAC, "Fast Ethernet MAC"),
74 BHND_CDESC(BCM, CODEC, OTHER, "V.90 Modem Codec"),
75 BHND_CDESC(BCM, USB, OTHER, "USB 1.1 Device/Host Controller"),
76 BHND_CDESC(BCM, ADSL, OTHER, "ADSL Core"),
77 BHND_CDESC(BCM, ILINE100, OTHER, "iLine100 HPNA"),
78 BHND_CDESC(BCM, IPSEC, OTHER, "IPsec Accelerator"),
79 BHND_CDESC(BCM, UTOPIA, OTHER, "UTOPIA ATM Core"),
80 BHND_CDESC(BCM, PCMCIA, PCCARD, "PCMCIA Bridge"),
81 BHND_CDESC(BCM, SOCRAM, RAM, "Internal Memory"),
82 BHND_CDESC(BCM, MEMC, MEMC, "MEMC SDRAM Controller"),
83 BHND_CDESC(BCM, OFDM, OTHER, "OFDM PHY"),
84 BHND_CDESC(BCM, EXTIF, OTHER, "External Interface"),
85 BHND_CDESC(BCM, D11, WLAN, "802.11 MAC/PHY/Radio"),
86 BHND_CDESC(BCM, APHY, WLAN_PHY, "802.11a PHY"),
87 BHND_CDESC(BCM, BPHY, WLAN_PHY, "802.11b PHY"),
88 BHND_CDESC(BCM, GPHY, WLAN_PHY, "802.11g PHY"),
89 BHND_CDESC(BCM, MIPS33, CPU, "MIPS3302 Core"),
90 BHND_CDESC(BCM, USB11H, OTHER, "USB 1.1 Host Controller"),
91 BHND_CDESC(BCM, USB11D, OTHER, "USB 1.1 Device Core"),
92 BHND_CDESC(BCM, USB20H, OTHER, "USB 2.0 Host Controller"),
93 BHND_CDESC(BCM, USB20D, OTHER, "USB 2.0 Device Core"),
94 BHND_CDESC(BCM, SDIOH, OTHER, "SDIO Host Controller"),
95 BHND_CDESC(BCM, ROBO, OTHER, "RoboSwitch"),
96 BHND_CDESC(BCM, ATA100, OTHER, "Parallel ATA Controller"),
97 BHND_CDESC(BCM, SATAXOR, OTHER, "SATA DMA/XOR Controller"),
98 BHND_CDESC(BCM, GIGETH, ENET_MAC, "Gigabit Ethernet MAC"),
99 BHND_CDESC(BCM, PCIE, PCIE, "PCIe Bridge"),
100 BHND_CDESC(BCM, NPHY, WLAN_PHY, "802.11n 2x2 PHY"),
101 BHND_CDESC(BCM, SRAMC, MEMC, "SRAM Controller"),
102 BHND_CDESC(BCM, MINIMAC, OTHER, "MINI MAC/PHY"),
103 BHND_CDESC(BCM, ARM11, CPU, "ARM1176 CPU"),
104 BHND_CDESC(BCM, ARM7S, CPU, "ARM7TDMI-S CPU"),
105 BHND_CDESC(BCM, LPPHY, WLAN_PHY, "802.11a/b/g PHY"),
106 BHND_CDESC(BCM, PMU, PMU, "PMU"),
107 BHND_CDESC(BCM, SSNPHY, WLAN_PHY, "802.11n Single-Stream PHY"),
108 BHND_CDESC(BCM, SDIOD, OTHER, "SDIO Device Core"),
109 BHND_CDESC(BCM, ARMCM3, CPU, "ARM Cortex-M3 CPU"),
110 BHND_CDESC(BCM, HTPHY, WLAN_PHY, "802.11n 4x4 PHY"),
111 BHND_CDESC(MIPS,MIPS74K, CPU, "MIPS74k CPU"),
112 BHND_CDESC(BCM, GMAC, ENET_MAC, "Gigabit MAC core"),
113 BHND_CDESC(BCM, DMEMC, MEMC, "DDR1/DDR2 Memory Controller"),
114 BHND_CDESC(BCM, PCIERC, OTHER, "PCIe Root Complex"),
115 BHND_CDESC(BCM, OCP, SOC_BRIDGE, "OCP to OCP Bridge"),
116 BHND_CDESC(BCM, SC, OTHER, "Shared Common Core"),
117 BHND_CDESC(BCM, AHB, SOC_BRIDGE, "OCP to AHB Bridge"),
118 BHND_CDESC(BCM, SPIH, OTHER, "SPI Host Controller"),
119 BHND_CDESC(BCM, I2S, OTHER, "I2S Digital Audio Interface"),
120 BHND_CDESC(BCM, DMEMS, MEMC, "SDR/DDR1 Memory Controller"),
121 BHND_CDESC(BCM, UBUS_SHIM, OTHER, "BCM6362/UBUS WLAN SHIM"),
122 BHND_CDESC(BCM, PCIE2, PCIE, "PCIe Bridge (Gen2)"),
124 BHND_CDESC(ARM, APB_BRIDGE, SOC_BRIDGE, "BP135 AMBA3 AXI to APB Bridge"),
125 BHND_CDESC(ARM, PL301, SOC_ROUTER, "PL301 AMBA3 Interconnect"),
126 BHND_CDESC(ARM, EROM, EROM, "PL366 Device Enumeration ROM"),
127 BHND_CDESC(ARM, OOB_ROUTER, OTHER, "PL367 OOB Interrupt Router"),
128 BHND_CDESC(ARM, AXI_UNMAPPED, OTHER, "Unmapped Address Ranges"),
130 BHND_CDESC(BCM, 4706_CC, CC, "ChipCommon I/O Controller"),
131 BHND_CDESC(BCM, NS_PCIE2, PCIE, "PCIe Bridge (Gen2)"),
132 BHND_CDESC(BCM, NS_DMA, OTHER, "DMA engine"),
133 BHND_CDESC(BCM, NS_SDIO, OTHER, "SDIO 3.0 Host Controller"),
134 BHND_CDESC(BCM, NS_USB20H, OTHER, "USB 2.0 Host Controller"),
135 BHND_CDESC(BCM, NS_USB30H, OTHER, "USB 3.0 Host Controller"),
136 BHND_CDESC(BCM, NS_A9JTAG, OTHER, "ARM Cortex A9 JTAG Interface"),
137 BHND_CDESC(BCM, NS_DDR23_MEMC, MEMC, "Denali DDR2/DD3 Memory Controller"),
138 BHND_CDESC(BCM, NS_ROM, NVRAM, "System ROM"),
139 BHND_CDESC(BCM, NS_NAND, NVRAM, "NAND Flash Controller"),
140 BHND_CDESC(BCM, NS_QSPI, NVRAM, "QSPI Flash Controller"),
141 BHND_CDESC(BCM, NS_CC_B, CC_B, "ChipCommon B Auxiliary I/O Controller"),
142 BHND_CDESC(BCM, 4706_SOCRAM, RAM, "Internal Memory"),
143 BHND_CDESC(BCM, IHOST_ARMCA9, CPU, "ARM Cortex A9 CPU"),
144 BHND_CDESC(BCM, 4706_GMAC_CMN, ENET, "Gigabit MAC (Common)"),
145 BHND_CDESC(BCM, 4706_GMAC, ENET_MAC, "Gigabit MAC"),
146 BHND_CDESC(BCM, AMEMC, MEMC, "Denali DDR1/DDR2 Memory Controller"),
149 /* Derived from inspection of the BCM4331 cores that provide PrimeCell
150 * IDs. Due to lack of documentation, the surmised device name/purpose
151 * provided here may be incorrect. */
152 { BHND_MFGID_ARM, BHND_PRIMEID_EROM, BHND_DEVCLASS_OTHER,
153 "PL364 Device Enumeration ROM" },
154 { BHND_MFGID_ARM, BHND_PRIMEID_SWRAP, BHND_DEVCLASS_OTHER,
155 "PL368 Device Management Interface" },
156 { BHND_MFGID_ARM, BHND_PRIMEID_MWRAP, BHND_DEVCLASS_OTHER,
157 "PL369 Device Management Interface" },
163 * Return the name for a given JEP106 manufacturer ID.
165 * @param vendor A JEP106 Manufacturer ID, including the non-standard ARM 4-bit
166 * JEP106 continuation code.
169 bhnd_vendor_name(uint16_t vendor)
176 case BHND_MFGID_MIPS:
184 * Return the name of a port type.
187 bhnd_port_type_name(bhnd_port_type port_type)
190 case BHND_PORT_DEVICE:
192 case BHND_PORT_BRIDGE:
194 case BHND_PORT_AGENT:
202 static const struct bhnd_core_desc *
203 bhnd_find_core_desc(uint16_t vendor, uint16_t device)
205 for (u_int i = 0; bhnd_core_descs[i].desc != NULL; i++) {
206 if (bhnd_core_descs[i].vendor != vendor)
209 if (bhnd_core_descs[i].device != device)
212 return (&bhnd_core_descs[i]);
219 * Return a human-readable name for a BHND core.
221 * @param vendor The core designer's JEDEC-106 Manufacturer ID
222 * @param device The core identifier.
225 bhnd_find_core_name(uint16_t vendor, uint16_t device)
227 const struct bhnd_core_desc *desc;
229 if ((desc = bhnd_find_core_desc(vendor, device)) == NULL)
236 * Return the device class for a BHND core.
238 * @param vendor The core designer's JEDEC-106 Manufacturer ID
239 * @param device The core identifier.
242 bhnd_find_core_class(uint16_t vendor, uint16_t device)
244 const struct bhnd_core_desc *desc;
246 if ((desc = bhnd_find_core_desc(vendor, device)) == NULL)
247 return (BHND_DEVCLASS_OTHER);
253 * Return a human-readable name for a BHND core.
255 * @param ci The core's info record.
258 bhnd_core_name(const struct bhnd_core_info *ci)
260 return bhnd_find_core_name(ci->vendor, ci->device);
264 * Return the device class for a BHND core.
266 * @param ci The core's info record.
269 bhnd_core_class(const struct bhnd_core_info *ci)
271 return bhnd_find_core_class(ci->vendor, ci->device);
275 * Initialize a core info record with data from from a bhnd-attached @p dev.
277 * @param dev A bhnd device.
278 * @param core The record to be initialized.
280 struct bhnd_core_info
281 bhnd_get_core_info(device_t dev) {
282 return (struct bhnd_core_info) {
283 .vendor = bhnd_get_vendor(dev),
284 .device = bhnd_get_device(dev),
285 .hwrev = bhnd_get_hwrev(dev),
286 .core_idx = bhnd_get_core_index(dev),
287 .unit = bhnd_get_core_unit(dev)
292 * Find a @p class child device with @p unit on @p dev.
294 * @param parent The bhnd-compatible bus to be searched.
295 * @param class The device class to match on.
296 * @param unit The device unit number; specify -1 to return the first match
297 * regardless of unit number.
299 * @retval device_t if a matching child device is found.
300 * @retval NULL if no matching child device is found.
303 bhnd_find_child(device_t dev, bhnd_devclass_t class, int unit)
305 struct bhnd_core_match md = {
306 BHND_MATCH_CORE_CLASS(class),
307 BHND_MATCH_CORE_UNIT(unit)
311 md.m.match.core_unit = 0;
313 return bhnd_match_child(dev, &md);
317 * Find the first child device on @p dev that matches @p desc.
319 * @param parent The bhnd-compatible bus to be searched.
320 * @param desc A match descriptor.
322 * @retval device_t if a matching child device is found.
323 * @retval NULL if no matching child device is found.
326 bhnd_match_child(device_t dev, const struct bhnd_core_match *desc)
333 error = device_get_children(dev, &devlistp, &devcnt);
338 for (int i = 0; i < devcnt; i++) {
339 struct bhnd_core_info ci = bhnd_get_core_info(devlistp[i]);
341 if (bhnd_core_matches(&ci, desc)) {
348 free(devlistp, M_TEMP);
353 * Walk up the bhnd device hierarchy to locate the root device
354 * to which the bhndb bridge is attached.
356 * This can be used from within bhnd host bridge drivers to locate the
357 * actual upstream host device.
359 * @param dev A bhnd device.
360 * @param bus_class The expected bus (e.g. "pci") to which the bridge root
361 * should be attached.
363 * @retval device_t if a matching parent device is found.
364 * @retval NULL @p dev is not attached via a bhndb bus
365 * @retval NULL no parent device is attached via @p bus_class.
368 bhnd_find_bridge_root(device_t dev, devclass_t bus_class)
370 devclass_t bhndb_class;
373 KASSERT(device_get_devclass(device_get_parent(dev)) == bhnd_devclass,
374 ("%s not a bhnd device", device_get_nameunit(dev)));
376 bhndb_class = devclass_find("bhndb");
378 /* Walk the device tree until we hit a bridge */
380 while ((parent = device_get_parent(parent)) != NULL) {
381 if (device_get_devclass(parent) == bhndb_class)
389 /* Search for a parent attached to the expected bus class */
390 while ((parent = device_get_parent(parent)) != NULL) {
393 bus = device_get_parent(parent);
394 if (bus != NULL && device_get_devclass(bus) == bus_class)
403 * Find the first core in @p cores that matches @p desc.
405 * @param cores The table to search.
406 * @param num_cores The length of @p cores.
407 * @param desc A match descriptor.
409 * @retval bhnd_core_info if a matching core is found.
410 * @retval NULL if no matching core is found.
412 const struct bhnd_core_info *
413 bhnd_match_core(const struct bhnd_core_info *cores, u_int num_cores,
414 const struct bhnd_core_match *desc)
416 for (u_int i = 0; i < num_cores; i++) {
417 if (bhnd_core_matches(&cores[i], desc))
426 * Find the first core in @p cores with the given @p class.
428 * @param cores The table to search.
429 * @param num_cores The length of @p cores.
430 * @param desc A match descriptor.
432 * @retval bhnd_core_info if a matching core is found.
433 * @retval NULL if no matching core is found.
435 const struct bhnd_core_info *
436 bhnd_find_core(const struct bhnd_core_info *cores, u_int num_cores,
437 bhnd_devclass_t class)
439 struct bhnd_core_match md = {
440 BHND_MATCH_CORE_CLASS(class)
443 return bhnd_match_core(cores, num_cores, &md);
447 * Return true if the @p core matches @p desc.
449 * @param core A bhnd core descriptor.
450 * @param desc A match descriptor to compare against @p core.
452 * @retval true if @p core matches @p match
453 * @retval false if @p core does not match @p match.
456 bhnd_core_matches(const struct bhnd_core_info *core,
457 const struct bhnd_core_match *desc)
459 if (desc->m.match.core_vendor && desc->core_vendor != core->vendor)
462 if (desc->m.match.core_id && desc->core_id != core->device)
465 if (desc->m.match.core_unit && desc->core_unit != core->unit)
468 if (desc->m.match.core_rev &&
469 !bhnd_hwrev_matches(core->hwrev, &desc->core_rev))
472 if (desc->m.match.core_class &&
473 desc->core_class != bhnd_core_class(core))
480 * Return true if the @p chip matches @p desc.
482 * @param chip A bhnd chip identifier.
483 * @param desc A match descriptor to compare against @p chip.
485 * @retval true if @p chip matches @p match
486 * @retval false if @p chip does not match @p match.
489 bhnd_chip_matches(const struct bhnd_chipid *chip,
490 const struct bhnd_chip_match *desc)
492 if (desc->m.match.chip_id && chip->chip_id != desc->chip_id)
495 if (desc->m.match.chip_pkg && chip->chip_pkg != desc->chip_pkg)
498 if (desc->m.match.chip_rev &&
499 !bhnd_hwrev_matches(chip->chip_rev, &desc->chip_rev))
506 * Return true if the @p board matches @p desc.
508 * @param board The bhnd board info.
509 * @param desc A match descriptor to compare against @p board.
511 * @retval true if @p chip matches @p match
512 * @retval false if @p chip does not match @p match.
515 bhnd_board_matches(const struct bhnd_board_info *board,
516 const struct bhnd_board_match *desc)
518 if (desc->m.match.board_srom_rev &&
519 !bhnd_hwrev_matches(board->board_srom_rev, &desc->board_srom_rev))
522 if (desc->m.match.board_vendor &&
523 board->board_vendor != desc->board_vendor)
526 if (desc->m.match.board_type && board->board_type != desc->board_type)
529 if (desc->m.match.board_rev &&
530 !bhnd_hwrev_matches(board->board_rev, &desc->board_rev))
537 * Return true if the @p hwrev matches @p desc.
539 * @param hwrev A bhnd hardware revision.
540 * @param desc A match descriptor to compare against @p core.
542 * @retval true if @p hwrev matches @p match
543 * @retval false if @p hwrev does not match @p match.
546 bhnd_hwrev_matches(uint16_t hwrev, const struct bhnd_hwrev_match *desc)
548 if (desc->start != BHND_HWREV_INVALID &&
552 if (desc->end != BHND_HWREV_INVALID &&
560 * Return true if the @p dev matches @p desc.
562 * @param dev A bhnd device.
563 * @param desc A match descriptor to compare against @p dev.
565 * @retval true if @p dev matches @p match
566 * @retval false if @p dev does not match @p match.
569 bhnd_device_matches(device_t dev, const struct bhnd_device_match *desc)
571 struct bhnd_core_info core;
572 const struct bhnd_chipid *chip;
573 struct bhnd_board_info board;
577 /* Construct individual match descriptors */
578 struct bhnd_core_match m_core = { _BHND_CORE_MATCH_COPY(desc) };
579 struct bhnd_chip_match m_chip = { _BHND_CHIP_MATCH_COPY(desc) };
580 struct bhnd_board_match m_board = { _BHND_BOARD_MATCH_COPY(desc) };
582 /* Fetch and match core info */
583 if (m_core.m.match_flags) {
584 /* Only applicable to bhnd-attached cores */
585 parent = device_get_parent(dev);
586 if (device_get_devclass(parent) != bhnd_devclass) {
587 device_printf(dev, "attempting to match core "
588 "attributes against non-core device\n");
592 core = bhnd_get_core_info(dev);
593 if (!bhnd_core_matches(&core, &m_core))
597 /* Fetch and match chip info */
598 if (m_chip.m.match_flags) {
599 chip = bhnd_get_chipid(dev);
601 if (!bhnd_chip_matches(chip, &m_chip))
605 /* Fetch and match board info.
607 * This is not available until after NVRAM is up; earlier device
608 * matches should not include board requirements */
609 if (m_board.m.match_flags) {
610 if ((error = bhnd_read_board_info(dev, &board))) {
611 device_printf(dev, "failed to read required board info "
612 "during device matching: %d\n", error);
616 if (!bhnd_board_matches(&board, &m_board))
625 * Search @p table for an entry matching @p dev.
627 * @param dev A bhnd device to match against @p table.
628 * @param table The device table to search.
629 * @param entry_size The @p table entry size, in bytes.
631 * @retval bhnd_device the first matching device, if any.
632 * @retval NULL if no matching device is found in @p table.
634 const struct bhnd_device *
635 bhnd_device_lookup(device_t dev, const struct bhnd_device *table,
638 const struct bhnd_device *entry;
639 device_t hostb, parent;
640 bhnd_attach_type attach_type;
643 parent = device_get_parent(dev);
644 hostb = bhnd_find_hostb_device(parent);
645 attach_type = bhnd_get_attach_type(dev);
647 for (entry = table; !BHND_DEVICE_IS_END(entry); entry =
648 (const struct bhnd_device *) ((const char *) entry + entry_size))
650 /* match core info */
651 if (!bhnd_device_matches(dev, &entry->core))
654 /* match device flags */
655 dflags = entry->device_flags;
657 /* hostb implies BHND_ATTACH_ADAPTER requirement */
658 if (dflags & BHND_DF_HOSTB)
659 dflags |= BHND_DF_ADAPTER;
661 if (dflags & BHND_DF_ADAPTER)
662 if (attach_type != BHND_ATTACH_ADAPTER)
665 if (dflags & BHND_DF_HOSTB)
669 if (dflags & BHND_DF_SOC)
670 if (attach_type != BHND_ATTACH_NATIVE)
682 * Scan the device @p table for all quirk flags applicable to @p dev.
684 * @param dev A bhnd device to match against @p table.
685 * @param table The device table to search.
687 * @return returns all matching quirk flags.
690 bhnd_device_quirks(device_t dev, const struct bhnd_device *table,
693 const struct bhnd_device *dent;
694 const struct bhnd_device_quirk *qent, *qtable;
697 /* Locate the device entry */
698 if ((dent = bhnd_device_lookup(dev, table, entry_size)) == NULL)
701 /* Quirks table is optional */
702 qtable = dent->quirks_table;
706 /* Collect matching device quirk entries */
708 for (qent = qtable; !BHND_DEVICE_QUIRK_IS_END(qent); qent++) {
709 if (bhnd_device_matches(dev, &qent->desc))
710 quirks |= qent->quirks;
718 * Allocate bhnd(4) resources defined in @p rs from a parent bus.
720 * @param dev The device requesting ownership of the resources.
721 * @param rs A standard bus resource specification. This will be updated
722 * with the allocated resource's RIDs.
723 * @param res On success, the allocated bhnd resources.
726 * @retval non-zero if allocation of any non-RF_OPTIONAL resource fails,
727 * all allocated resources will be released and a regular
728 * unix error code will be returned.
731 bhnd_alloc_resources(device_t dev, struct resource_spec *rs,
732 struct bhnd_resource **res)
734 /* Initialize output array */
735 for (u_int i = 0; rs[i].type != -1; i++)
738 for (u_int i = 0; rs[i].type != -1; i++) {
739 res[i] = bhnd_alloc_resource_any(dev, rs[i].type, &rs[i].rid,
742 /* Clean up all allocations on failure */
743 if (res[i] == NULL && !(rs[i].flags & RF_OPTIONAL)) {
744 bhnd_release_resources(dev, rs, res);
753 * Release bhnd(4) resources defined in @p rs from a parent bus.
755 * @param dev The device that owns the resources.
756 * @param rs A standard bus resource specification previously initialized
757 * by @p bhnd_alloc_resources.
758 * @param res The bhnd resources to be released.
761 bhnd_release_resources(device_t dev, const struct resource_spec *rs,
762 struct bhnd_resource **res)
764 for (u_int i = 0; rs[i].type != -1; i++) {
768 bhnd_release_resource(dev, rs[i].type, rs[i].rid, res[i]);
774 * Parse the CHIPC_ID_* fields from the ChipCommon CHIPC_ID
775 * register, returning its bhnd_chipid representation.
777 * @param idreg The CHIPC_ID register value.
778 * @param enum_addr The enumeration address to include in the result.
781 * On early siba(4) devices, the ChipCommon core does not provide
782 * a valid CHIPC_ID_NUMCORE field. On these ChipCommon revisions
783 * (see CHIPC_NCORES_MIN_HWREV()), this function will parse and return
784 * an invalid `ncores` value.
787 bhnd_parse_chipid(uint32_t idreg, bhnd_addr_t enum_addr)
789 struct bhnd_chipid result;
791 /* Fetch the basic chip info */
792 result.chip_id = CHIPC_GET_BITS(idreg, CHIPC_ID_CHIP);
793 result.chip_pkg = CHIPC_GET_BITS(idreg, CHIPC_ID_PKG);
794 result.chip_rev = CHIPC_GET_BITS(idreg, CHIPC_ID_REV);
795 result.chip_type = CHIPC_GET_BITS(idreg, CHIPC_ID_BUS);
796 result.ncores = CHIPC_GET_BITS(idreg, CHIPC_ID_NUMCORE);
798 result.enum_addr = enum_addr;
804 * Allocate the resource defined by @p rs via @p dev, use it
805 * to read the ChipCommon ID register relative to @p chipc_offset,
806 * then release the resource.
808 * @param dev The device owning @p rs.
809 * @param rs A resource spec that encompasses the ChipCommon register block.
810 * @param chipc_offset The offset of the ChipCommon registers within @p rs.
811 * @param[out] result the chip identification data.
814 * @retval non-zero if the ChipCommon identification data could not be read.
817 bhnd_read_chipid(device_t dev, struct resource_spec *rs,
818 bus_size_t chipc_offset, struct bhnd_chipid *result)
820 struct resource *res;
822 int error, rid, rtype;
824 /* Allocate the ChipCommon window resource and fetch the chipid data */
827 res = bus_alloc_resource_any(dev, rtype, &rid, RF_ACTIVE);
830 "failed to allocate bhnd chipc resource\n");
834 /* Fetch the basic chip info */
835 reg = bus_read_4(res, chipc_offset + CHIPC_ID);
836 *result = bhnd_parse_chipid(reg, 0x0);
838 /* Fetch the enum base address */
840 switch (result->chip_type) {
841 case BHND_CHIPTYPE_SIBA:
842 result->enum_addr = BHND_DEFAULT_CHIPC_ADDR;
844 case BHND_CHIPTYPE_BCMA:
845 case BHND_CHIPTYPE_BCMA_ALT:
846 result->enum_addr = bus_read_4(res, chipc_offset +
849 case BHND_CHIPTYPE_UBUS:
850 device_printf(dev, "unsupported ubus/bcm63xx chip type");
854 device_printf(dev, "unknown chip type %hhu\n",
862 bus_release_resource(dev, rtype, rid, res);
867 * Using the bhnd(4) bus-level core information and a custom core name,
868 * populate @p dev's device description.
870 * @param dev A bhnd-bus attached device.
871 * @param dev_name The core's name (e.g. "SDIO Device Core")
874 bhnd_set_custom_core_desc(device_t dev, const char *dev_name)
876 const char *vendor_name;
879 vendor_name = bhnd_get_vendor_name(dev);
880 asprintf(&desc, M_BHND, "%s %s, rev %hhu", vendor_name, dev_name,
881 bhnd_get_hwrev(dev));
884 device_set_desc_copy(dev, desc);
887 device_set_desc(dev, dev_name);
892 * Using the bhnd(4) bus-level core information, populate @p dev's device
895 * @param dev A bhnd-bus attached device.
898 bhnd_set_default_core_desc(device_t dev)
900 bhnd_set_custom_core_desc(dev, bhnd_get_device_name(dev));
904 * Helper function for implementing BHND_BUS_IS_HW_DISABLED().
906 * If a parent device is available, this implementation delegates the
907 * request to the BHND_BUS_IS_HW_DISABLED() method on the parent of @p dev.
909 * If no parent device is available (i.e. on a the bus root), the hardware
910 * is assumed to be usable and false is returned.
913 bhnd_bus_generic_is_hw_disabled(device_t dev, device_t child)
915 if (device_get_parent(dev) != NULL)
916 return (BHND_BUS_IS_HW_DISABLED(device_get_parent(dev), child));
922 * Helper function for implementing BHND_BUS_GET_CHIPID().
924 * This implementation delegates the request to the BHND_BUS_GET_CHIPID()
925 * method on the parent of @p dev. If no parent exists, the implementation
928 const struct bhnd_chipid *
929 bhnd_bus_generic_get_chipid(device_t dev, device_t child)
931 if (device_get_parent(dev) != NULL)
932 return (BHND_BUS_GET_CHIPID(device_get_parent(dev), child));
934 panic("missing BHND_BUS_GET_CHIPID()");
937 /* nvram board_info population macros for bhnd_bus_generic_read_board_info() */
938 #define BHND_GV(_dest, _name) \
939 bhnd_nvram_getvar(child, BHND_NVAR_ ## _name, &_dest, sizeof(_dest))
941 #define REQ_BHND_GV(_dest, _name) do { \
942 if ((error = BHND_GV(_dest, _name))) { \
944 "error reading " __STRING(_name) ": %d\n", error); \
949 #define OPT_BHND_GV(_dest, _name, _default) do { \
950 if ((error = BHND_GV(_dest, _name))) { \
951 if (error != ENOENT) { \
954 __STRING(_name) ": %d\n", error); \
962 * Helper function for implementing BHND_BUS_READ_BOARDINFO().
964 * This implementation populates @p info with information from NVRAM,
965 * defaulting board_vendor and board_type fields to 0 if the
966 * requested variables cannot be found.
968 * This behavior is correct for most SoCs, but must be overridden on
969 * bridged (PCI, PCMCIA, etc) devices to produce a complete bhnd_board_info
973 bhnd_bus_generic_read_board_info(device_t dev, device_t child,
974 struct bhnd_board_info *info)
978 OPT_BHND_GV(info->board_vendor, BOARDVENDOR, 0);
979 OPT_BHND_GV(info->board_type, BOARDTYPE, 0); /* srom >= 2 */
980 REQ_BHND_GV(info->board_rev, BOARDREV);
981 REQ_BHND_GV(info->board_srom_rev,SROMREV);
982 REQ_BHND_GV(info->board_flags, BOARDFLAGS);
983 OPT_BHND_GV(info->board_flags2, BOARDFLAGS2, 0); /* srom >= 4 */
984 OPT_BHND_GV(info->board_flags3, BOARDFLAGS3, 0); /* srom >= 11 */
995 * Find an NVRAM child device on @p dev, if any.
997 * @retval device_t An NVRAM device.
998 * @retval NULL If no NVRAM device is found.
1001 find_nvram_child(device_t dev)
1003 device_t chipc, nvram;
1005 /* Look for a directly-attached NVRAM child */
1006 nvram = device_find_child(dev, "bhnd_nvram", 0);
1010 /* Remaining checks are only applicable when searching a bhnd(4)
1012 if (device_get_devclass(dev) != bhnd_devclass)
1015 /* Look for a ChipCommon-attached NVRAM device */
1016 if ((chipc = bhnd_find_child(dev, BHND_DEVCLASS_CC, -1)) != NULL) {
1017 nvram = device_find_child(chipc, "bhnd_nvram", 0);
1027 * Helper function for implementing BHND_BUS_GET_NVRAM_VAR().
1029 * This implementation searches @p dev for a usable NVRAM child device:
1030 * - The first child device implementing the bhnd_nvram devclass is
1031 * returned, otherwise
1032 * - If @p dev is a bhnd(4) bus, a ChipCommon core that advertises an
1033 * attached NVRAM source.
1035 * If no usable child device is found on @p dev, the request is delegated to
1036 * the BHND_BUS_GET_NVRAM_VAR() method on the parent of @p dev.
1039 bhnd_bus_generic_get_nvram_var(device_t dev, device_t child, const char *name,
1040 void *buf, size_t *size)
1045 /* Try to find an NVRAM device applicable to @p child */
1046 if ((nvram = find_nvram_child(dev)) != NULL)
1047 return BHND_NVRAM_GETVAR(nvram, name, buf, size);
1049 /* Try to delegate to parent */
1050 if ((parent = device_get_parent(dev)) == NULL)
1053 return (BHND_BUS_GET_NVRAM_VAR(device_get_parent(dev), child,
1058 * Helper function for implementing BHND_BUS_ALLOC_RESOURCE().
1060 * This implementation of BHND_BUS_ALLOC_RESOURCE() delegates allocation
1061 * of the underlying resource to BUS_ALLOC_RESOURCE(), and activation
1062 * to @p dev's BHND_BUS_ACTIVATE_RESOURCE().
1064 struct bhnd_resource *
1065 bhnd_bus_generic_alloc_resource(device_t dev, device_t child, int type,
1066 int *rid, rman_res_t start, rman_res_t end, rman_res_t count,
1069 struct bhnd_resource *br;
1070 struct resource *res;
1076 /* Allocate the real bus resource (without activating it) */
1077 res = BUS_ALLOC_RESOURCE(dev, child, type, rid, start, end, count,
1078 (flags & ~RF_ACTIVE));
1082 /* Allocate our bhnd resource wrapper. */
1083 br = malloc(sizeof(struct bhnd_resource), M_BHND, M_NOWAIT);
1090 /* Attempt activation */
1091 if (flags & RF_ACTIVE) {
1092 error = BHND_BUS_ACTIVATE_RESOURCE(dev, child, type, *rid, br);
1101 BUS_RELEASE_RESOURCE(dev, child, type, *rid, res);
1108 * Helper function for implementing BHND_BUS_RELEASE_RESOURCE().
1110 * This implementation of BHND_BUS_RELEASE_RESOURCE() delegates release of
1111 * the backing resource to BUS_RELEASE_RESOURCE().
1114 bhnd_bus_generic_release_resource(device_t dev, device_t child, int type,
1115 int rid, struct bhnd_resource *r)
1119 if ((error = BUS_RELEASE_RESOURCE(dev, child, type, rid, r->res)))
1128 * Helper function for implementing BHND_BUS_ACTIVATE_RESOURCE().
1130 * This implementation of BHND_BUS_ACTIVATE_RESOURCE() simply calls the
1131 * BHND_BUS_ACTIVATE_RESOURCE() method of the parent of @p dev.
1134 bhnd_bus_generic_activate_resource(device_t dev, device_t child, int type,
1135 int rid, struct bhnd_resource *r)
1137 /* Try to delegate to the parent */
1138 if (device_get_parent(dev) != NULL)
1139 return (BHND_BUS_ACTIVATE_RESOURCE(device_get_parent(dev),
1140 child, type, rid, r));
1146 * Helper function for implementing BHND_BUS_DEACTIVATE_RESOURCE().
1148 * This implementation of BHND_BUS_ACTIVATE_RESOURCE() simply calls the
1149 * BHND_BUS_ACTIVATE_RESOURCE() method of the parent of @p dev.
1152 bhnd_bus_generic_deactivate_resource(device_t dev, device_t child,
1153 int type, int rid, struct bhnd_resource *r)
1155 if (device_get_parent(dev) != NULL)
1156 return (BHND_BUS_DEACTIVATE_RESOURCE(device_get_parent(dev),
1157 child, type, rid, r));