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.
35 #include <sys/types.h>
38 #include <machine/bus.h>
41 #include "bhnd_types.h"
42 #include "bhnd_erom_types.h"
43 #include "bhnd_debug.h"
44 #include "bhnd_bus_if.h"
45 #include "bhnd_match.h"
47 #include "nvram/bhnd_nvram.h"
49 extern devclass_t bhnd_devclass;
50 extern devclass_t bhnd_hostb_devclass;
51 extern devclass_t bhnd_nvram_devclass;
53 #define BHND_CHIPID_MAX_NAMELEN 32 /**< maximum buffer required for a
54 bhnd_format_chip_id() */
57 * bhnd child instance variables
59 enum bhnd_device_vars {
60 BHND_IVAR_VENDOR, /**< Designer's JEP-106 manufacturer ID. */
61 BHND_IVAR_DEVICE, /**< Part number */
62 BHND_IVAR_HWREV, /**< Core revision */
63 BHND_IVAR_DEVICE_CLASS, /**< Core class (@sa bhnd_devclass_t) */
64 BHND_IVAR_VENDOR_NAME, /**< Core vendor name */
65 BHND_IVAR_DEVICE_NAME, /**< Core name */
66 BHND_IVAR_CORE_INDEX, /**< Bus-assigned core number */
67 BHND_IVAR_CORE_UNIT, /**< Bus-assigned core unit number,
68 assigned sequentially (starting at 0) for
69 each vendor/device pair. */
73 * bhnd device probe priority bands.
76 BHND_PROBE_ROOT = 0, /**< Nexus or host bridge */
77 BHND_PROBE_BUS = 1000, /**< Busses and bridges */
78 BHND_PROBE_CPU = 2000, /**< CPU devices */
79 BHND_PROBE_INTERRUPT = 3000, /**< Interrupt controllers. */
80 BHND_PROBE_TIMER = 4000, /**< Timers and clocks. */
81 BHND_PROBE_RESOURCE = 5000, /**< Resource discovery (including NVRAM/SPROM) */
82 BHND_PROBE_DEFAULT = 6000, /**< Default device priority */
86 * Constants defining fine grained ordering within a BHND_PROBE_* priority band.
90 * BHND_PROBE_BUS + BHND_PROBE_ORDER_FIRST
94 BHND_PROBE_ORDER_FIRST = 0,
95 BHND_PROBE_ORDER_EARLY = 25,
96 BHND_PROBE_ORDER_MIDDLE = 50,
97 BHND_PROBE_ORDER_LATE = 75,
98 BHND_PROBE_ORDER_LAST = 100
103 * Simplified accessors for bhnd device ivars
105 #define BHND_ACCESSOR(var, ivar, type) \
106 __BUS_ACCESSOR(bhnd, var, BHND, ivar, type)
108 BHND_ACCESSOR(vendor, VENDOR, uint16_t);
109 BHND_ACCESSOR(device, DEVICE, uint16_t);
110 BHND_ACCESSOR(hwrev, HWREV, uint8_t);
111 BHND_ACCESSOR(class, DEVICE_CLASS, bhnd_devclass_t);
112 BHND_ACCESSOR(vendor_name, VENDOR_NAME, const char *);
113 BHND_ACCESSOR(device_name, DEVICE_NAME, const char *);
114 BHND_ACCESSOR(core_index, CORE_INDEX, u_int);
115 BHND_ACCESSOR(core_unit, CORE_UNIT, int);
120 * A bhnd(4) board descriptor.
122 struct bhnd_board_info {
123 uint16_t board_vendor; /**< PCI-SIG vendor ID (even on non-PCI
126 * On PCI devices, this will generally
127 * be the subsystem vendor ID, but the
128 * value may be overridden in device
131 uint16_t board_type; /**< Board type (See BHND_BOARD_*)
133 * On PCI devices, this will generally
134 * be the subsystem device ID, but the
135 * value may be overridden in device
138 uint16_t board_rev; /**< Board revision. */
139 uint8_t board_srom_rev; /**< Board SROM format revision */
141 uint32_t board_flags; /**< Board flags (see BHND_BFL_*) */
142 uint32_t board_flags2; /**< Board flags 2 (see BHND_BFL2_*) */
143 uint32_t board_flags3; /**< Board flags 3 (see BHND_BFL3_*) */
148 * Chip Identification
150 * This is read from the ChipCommon ID register; on earlier bhnd(4) devices
151 * where ChipCommon is unavailable, known values must be supplied.
154 uint16_t chip_id; /**< chip id (BHND_CHIPID_*) */
155 uint8_t chip_rev; /**< chip revision */
156 uint8_t chip_pkg; /**< chip package (BHND_PKGID_*) */
157 uint8_t chip_type; /**< chip type (BHND_CHIPTYPE_*) */
159 bhnd_addr_t enum_addr; /**< chip_type-specific enumeration
160 * address; either the siba(4) base
161 * core register block, or the bcma(4)
162 * EROM core address. */
164 uint8_t ncores; /**< number of cores, if known. 0 if
169 * A bhnd(4) core descriptor.
171 struct bhnd_core_info {
172 uint16_t vendor; /**< JEP-106 vendor (BHND_MFGID_*) */
173 uint16_t device; /**< device */
174 uint16_t hwrev; /**< hardware revision */
175 u_int core_idx; /**< bus-assigned core index */
176 int unit; /**< bus-assigned core unit */
180 * A bhnd(4) bus resource.
182 * This provides an abstract interface to per-core resources that may require
183 * bus-level remapping of address windows prior to access.
185 struct bhnd_resource {
186 struct resource *res; /**< the system resource. */
187 bool direct; /**< false if the resource requires
188 * bus window remapping before it
189 * is MMIO accessible. */
192 /** Wrap the active resource @p _r in a bhnd_resource structure */
193 #define BHND_DIRECT_RESOURCE(_r) ((struct bhnd_resource) { \
199 * Device quirk table descriptor.
201 struct bhnd_device_quirk {
202 struct bhnd_device_match desc; /**< device match descriptor */
203 uint32_t quirks; /**< quirk flags */
206 #define BHND_CORE_QUIRK(_rev, _flags) \
207 {{ BHND_MATCH_CORE_REV(_rev) }, (_flags) }
209 #define BHND_CHIP_QUIRK(_chip, _rev, _flags) \
210 {{ BHND_CHIP_IR(BCM ## _chip, _rev) }, (_flags) }
212 #define BHND_PKG_QUIRK(_chip, _pkg, _flags) \
213 {{ BHND_CHIP_IP(BCM ## _chip, BCM ## _chip ## _pkg) }, (_flags) }
215 #define BHND_BOARD_QUIRK(_board, _flags) \
216 {{ BHND_MATCH_BOARD_TYPE(_board) }, \
219 #define BHND_DEVICE_QUIRK_END { { BHND_MATCH_ANY }, 0 }
220 #define BHND_DEVICE_QUIRK_IS_END(_q) \
221 (((_q)->desc.m.match_flags == 0) && (_q)->quirks == 0)
225 BHND_DF_HOSTB = (1<<0), /**< core is serving as the bus' host
226 * bridge. implies BHND_DF_ADAPTER */
227 BHND_DF_SOC = (1<<1), /**< core is attached to a native
228 bus (BHND_ATTACH_NATIVE) */
229 BHND_DF_ADAPTER = (1<<2), /**< core is attached to a bridged
230 * adapter (BHND_ATTACH_ADAPTER) */
233 /** Device probe table descriptor */
235 const struct bhnd_device_match core; /**< core match descriptor */
236 const char *desc; /**< device description, or NULL. */
237 const struct bhnd_device_quirk *quirks_table; /**< quirks table for this device, or NULL */
238 uint32_t device_flags; /**< required BHND_DF_* flags */
241 #define _BHND_DEVICE(_vendor, _device, _desc, _quirks, \
243 { { BHND_MATCH_CORE(BHND_MFGID_ ## _vendor, \
244 BHND_COREID_ ## _device) }, _desc, _quirks, \
247 #define BHND_DEVICE(_vendor, _device, _desc, _quirks, ...) \
248 _BHND_DEVICE(_vendor, _device, _desc, _quirks, \
251 #define BHND_DEVICE_END { { BHND_MATCH_ANY }, NULL, NULL, 0 }
252 #define BHND_DEVICE_IS_END(_d) \
253 (BHND_MATCH_IS_ANY(&(_d)->core) && (_d)->desc == NULL)
255 const char *bhnd_vendor_name(uint16_t vendor);
256 const char *bhnd_port_type_name(bhnd_port_type port_type);
257 const char *bhnd_nvram_src_name(bhnd_nvram_src nvram_src);
259 const char *bhnd_find_core_name(uint16_t vendor,
261 bhnd_devclass_t bhnd_find_core_class(uint16_t vendor,
264 const char *bhnd_core_name(const struct bhnd_core_info *ci);
265 bhnd_devclass_t bhnd_core_class(const struct bhnd_core_info *ci);
267 int bhnd_format_chip_id(char *buffer, size_t size,
270 device_t bhnd_match_child(device_t dev,
271 const struct bhnd_core_match *desc);
273 device_t bhnd_find_child(device_t dev,
274 bhnd_devclass_t class, int unit);
276 device_t bhnd_find_bridge_root(device_t dev,
277 devclass_t bus_class);
279 const struct bhnd_core_info *bhnd_match_core(
280 const struct bhnd_core_info *cores,
282 const struct bhnd_core_match *desc);
284 const struct bhnd_core_info *bhnd_find_core(
285 const struct bhnd_core_info *cores,
286 u_int num_cores, bhnd_devclass_t class);
288 struct bhnd_core_match bhnd_core_get_match_desc(
289 const struct bhnd_core_info *core);
291 bool bhnd_cores_equal(
292 const struct bhnd_core_info *lhs,
293 const struct bhnd_core_info *rhs);
295 bool bhnd_core_matches(
296 const struct bhnd_core_info *core,
297 const struct bhnd_core_match *desc);
299 bool bhnd_chip_matches(
300 const struct bhnd_chipid *chipid,
301 const struct bhnd_chip_match *desc);
303 bool bhnd_board_matches(
304 const struct bhnd_board_info *info,
305 const struct bhnd_board_match *desc);
307 bool bhnd_hwrev_matches(uint16_t hwrev,
308 const struct bhnd_hwrev_match *desc);
310 bool bhnd_device_matches(device_t dev,
311 const struct bhnd_device_match *desc);
313 const struct bhnd_device *bhnd_device_lookup(device_t dev,
314 const struct bhnd_device *table,
317 uint32_t bhnd_device_quirks(device_t dev,
318 const struct bhnd_device *table,
321 struct bhnd_core_info bhnd_get_core_info(device_t dev);
323 int bhnd_alloc_resources(device_t dev,
324 struct resource_spec *rs,
325 struct bhnd_resource **res);
327 void bhnd_release_resources(device_t dev,
328 const struct resource_spec *rs,
329 struct bhnd_resource **res);
331 struct bhnd_chipid bhnd_parse_chipid(uint32_t idreg,
332 bhnd_addr_t enum_addr);
334 int bhnd_chipid_fixed_ncores(
335 const struct bhnd_chipid *cid,
336 uint16_t chipc_hwrev, uint8_t *ncores);
338 int bhnd_read_chipid(device_t dev,
339 struct resource_spec *rs,
340 bus_size_t chipc_offset,
341 struct bhnd_chipid *result);
343 void bhnd_set_custom_core_desc(device_t dev,
345 void bhnd_set_default_core_desc(device_t dev);
347 void bhnd_set_default_bus_desc(device_t dev,
348 const struct bhnd_chipid *chip_id);
350 int bhnd_nvram_getvar_str(device_t dev,
351 const char *name, char *buf, size_t len,
354 int bhnd_nvram_getvar_uint(device_t dev,
355 const char *name, void *value, int width);
356 int bhnd_nvram_getvar_uint8(device_t dev,
357 const char *name, uint8_t *value);
358 int bhnd_nvram_getvar_uint16(device_t dev,
359 const char *name, uint16_t *value);
360 int bhnd_nvram_getvar_uint32(device_t dev,
361 const char *name, uint32_t *value);
363 int bhnd_nvram_getvar_int(device_t dev,
364 const char *name, void *value, int width);
365 int bhnd_nvram_getvar_int8(device_t dev,
366 const char *name, int8_t *value);
367 int bhnd_nvram_getvar_int16(device_t dev,
368 const char *name, int16_t *value);
369 int bhnd_nvram_getvar_int32(device_t dev,
370 const char *name, int32_t *value);
372 int bhnd_nvram_getvar_array(device_t dev,
373 const char *name, void *buf, size_t count,
374 bhnd_nvram_type type);
376 bool bhnd_bus_generic_is_hw_disabled(device_t dev,
378 bool bhnd_bus_generic_is_region_valid(device_t dev,
379 device_t child, bhnd_port_type type,
380 u_int port, u_int region);
381 int bhnd_bus_generic_get_nvram_var(device_t dev,
382 device_t child, const char *name,
383 void *buf, size_t *size,
384 bhnd_nvram_type type);
385 const struct bhnd_chipid *bhnd_bus_generic_get_chipid(device_t dev,
387 int bhnd_bus_generic_read_board_info(device_t dev,
389 struct bhnd_board_info *info);
390 struct bhnd_resource *bhnd_bus_generic_alloc_resource (device_t dev,
391 device_t child, int type, int *rid,
392 rman_res_t start, rman_res_t end,
393 rman_res_t count, u_int flags);
394 int bhnd_bus_generic_release_resource (device_t dev,
395 device_t child, int type, int rid,
396 struct bhnd_resource *r);
397 int bhnd_bus_generic_activate_resource (device_t dev,
398 device_t child, int type, int rid,
399 struct bhnd_resource *r);
400 int bhnd_bus_generic_deactivate_resource (device_t dev,
401 device_t child, int type, int rid,
402 struct bhnd_resource *r);
403 bhnd_attach_type bhnd_bus_generic_get_attach_type(device_t dev,
407 * Return the bhnd(4) bus driver's device enumeration parser class
409 * @param driver A bhnd bus driver instance.
411 static inline bhnd_erom_class_t *
412 bhnd_driver_get_erom_class(driver_t *driver)
414 return (BHND_BUS_GET_EROM_CLASS(driver));
418 * Return the active host bridge core for the bhnd bus, if any, or NULL if
421 * @param dev A bhnd bus device.
423 static inline device_t
424 bhnd_find_hostb_device(device_t dev) {
425 return (BHND_BUS_FIND_HOSTB_DEVICE(dev));
429 * Return true if the hardware components required by @p dev are known to be
430 * unpopulated or otherwise unusable.
432 * In some cases, enumerated devices may have pins that are left floating, or
433 * the hardware may otherwise be non-functional; this method allows a parent
434 * device to explicitly specify if a successfully enumerated @p dev should
437 * @param dev A bhnd bus child device.
440 bhnd_is_hw_disabled(device_t dev) {
441 return (BHND_BUS_IS_HW_DISABLED(device_get_parent(dev), dev));
445 * Return the BHND chip identification info for the bhnd bus.
447 * @param dev A bhnd bus child device.
449 static inline const struct bhnd_chipid *
450 bhnd_get_chipid(device_t dev) {
451 return (BHND_BUS_GET_CHIPID(device_get_parent(dev), dev));
455 * If supported by the chipset, return the clock source for the given clock.
457 * This function is only supported on early PWRCTL-equipped chipsets
458 * that expose clock management via their host bridge interface. Currently,
459 * this includes PCI (not PCIe) devices, with ChipCommon core revisions 0-9.
461 * @param dev A bhnd bus child device.
462 * @param clock The clock for which a clock source will be returned.
464 * @retval bhnd_clksrc The clock source for @p clock.
465 * @retval BHND_CLKSRC_UNKNOWN If @p clock is unsupported, or its
466 * clock source is not known to the bus.
468 static inline bhnd_clksrc
469 bhnd_pwrctl_get_clksrc(device_t dev, bhnd_clock clock)
471 return (BHND_BUS_PWRCTL_GET_CLKSRC(device_get_parent(dev), dev, clock));
475 * If supported by the chipset, gate @p clock
477 * This function is only supported on early PWRCTL-equipped chipsets
478 * that expose clock management via their host bridge interface. Currently,
479 * this includes PCI (not PCIe) devices, with ChipCommon core revisions 0-9.
481 * @param dev A bhnd bus child device.
482 * @param clock The clock to be disabled.
485 * @retval ENODEV If bus-level clock source management is not supported.
486 * @retval ENXIO If bus-level management of @p clock is not supported.
489 bhnd_pwrctl_gate_clock(device_t dev, bhnd_clock clock)
491 return (BHND_BUS_PWRCTL_GATE_CLOCK(device_get_parent(dev), dev, clock));
495 * If supported by the chipset, ungate @p clock
497 * This function is only supported on early PWRCTL-equipped chipsets
498 * that expose clock management via their host bridge interface. Currently,
499 * this includes PCI (not PCIe) devices, with ChipCommon core revisions 0-9.
501 * @param dev A bhnd bus child device.
502 * @param clock The clock to be enabled.
505 * @retval ENODEV If bus-level clock source management is not supported.
506 * @retval ENXIO If bus-level management of @p clock is not supported.
509 bhnd_pwrctl_ungate_clock(device_t dev, bhnd_clock clock)
511 return (BHND_BUS_PWRCTL_UNGATE_CLOCK(device_get_parent(dev), dev,
516 * Return the BHND attachment type of the parent bhnd bus.
518 * @param dev A bhnd bus child device.
520 * @retval BHND_ATTACH_ADAPTER if the bus is resident on a bridged adapter,
521 * such as a WiFi chipset.
522 * @retval BHND_ATTACH_NATIVE if the bus provides hardware services (clock,
523 * CPU, etc) to a directly attached native host.
525 static inline bhnd_attach_type
526 bhnd_get_attach_type (device_t dev) {
527 return (BHND_BUS_GET_ATTACH_TYPE(device_get_parent(dev), dev));
531 * Attempt to read the BHND board identification from the bhnd bus.
533 * This relies on NVRAM access, and will fail if a valid NVRAM device cannot
534 * be found, or is not yet attached.
536 * @param dev The parent of @p child.
537 * @param child The bhnd device requesting board info.
538 * @param[out] info On success, will be populated with the bhnd(4) device's
542 * @retval ENODEV No valid NVRAM source could be found.
543 * @retval non-zero If reading @p name otherwise fails, a regular unix
544 * error code will be returned.
547 bhnd_read_board_info(device_t dev, struct bhnd_board_info *info)
549 return (BHND_BUS_READ_BOARD_INFO(device_get_parent(dev), dev, info));
553 * Return the number of interrupts to be assigned to @p child via
554 * BHND_BUS_ASSIGN_INTR().
556 * @param dev A bhnd bus child device.
559 bhnd_get_intr_count(device_t dev)
561 return (BHND_BUS_GET_INTR_COUNT(device_get_parent(dev), dev));
565 * Return the backplane interrupt vector corresponding to @p dev's given
568 * @param dev A bhnd bus child device.
569 * @param intr The interrupt number being queried. This is equivalent to the
570 * bus resource ID for the interrupt.
571 * @param[out] ivec On success, the assigned hardware interrupt vector be
572 * written to this pointer.
574 * On bcma(4) devices, this returns the OOB bus line assigned to the
577 * On siba(4) devices, this returns the target OCP slave flag number assigned
581 * @retval ENXIO If @p intr exceeds the number of interrupts available
585 bhnd_get_core_ivec(device_t dev, u_int intr, uint32_t *ivec)
587 return (BHND_BUS_GET_CORE_IVEC(device_get_parent(dev), dev, intr,
592 * Allocate and enable per-core PMU request handling for @p child.
594 * The region containing the core's PMU register block (if any) must be
595 * allocated via bus_alloc_resource(9) (or bhnd_alloc_resource) before
596 * calling bhnd_alloc_pmu(), and must not be released until after
597 * calling bhnd_release_pmu().
599 * @param dev The parent of @p child.
600 * @param child The requesting bhnd device.
603 * @retval non-zero If allocating PMU request state otherwise fails, a
604 * regular unix error code will be returned.
607 bhnd_alloc_pmu(device_t dev)
609 return (BHND_BUS_ALLOC_PMU(device_get_parent(dev), dev));
613 * Release any per-core PMU resources allocated for @p child. Any outstanding
614 * PMU requests are are discarded.
616 * @param dev The parent of @p child.
617 * @param child The requesting bhnd device.
620 * @retval non-zero If releasing PMU request state otherwise fails, a
621 * regular unix error code will be returned, and
622 * the core state will be left unmodified.
625 bhnd_release_pmu(device_t dev)
627 return (BHND_BUS_RELEASE_PMU(device_get_parent(dev), dev));
631 * Request that @p clock (or faster) be routed to @p dev.
633 * A driver must ask the bhnd bus to allocate clock request state
634 * via bhnd_alloc_pmu() before it can request clock resources.
636 * Request multiplexing is managed by the bus.
638 * @param dev The bhnd(4) device to which @p clock should be routed.
639 * @param clock The requested clock source.
642 * @retval ENODEV If an unsupported clock was requested.
643 * @retval ENXIO If the PMU has not been initialized or is otherwise unvailable.
646 bhnd_request_clock(device_t dev, bhnd_clock clock)
648 return (BHND_BUS_REQUEST_CLOCK(device_get_parent(dev), dev, clock));
652 * Request that @p clocks be powered on behalf of @p dev.
654 * This will power any clock sources (e.g. XTAL, PLL, etc) required for
655 * @p clocks and wait until they are ready, discarding any previous
656 * requests by @p dev.
658 * Request multiplexing is managed by the bus.
660 * A driver must ask the bhnd bus to allocate clock request state
661 * via bhnd_alloc_pmu() before it can request clock resources.
663 * @param dev The requesting bhnd(4) device.
664 * @param clocks The clock(s) to be enabled.
667 * @retval ENODEV If an unsupported clock was requested.
668 * @retval ENXIO If the PMU has not been initialized or is otherwise unvailable.
671 bhnd_enable_clocks(device_t dev, uint32_t clocks)
673 return (BHND_BUS_ENABLE_CLOCKS(device_get_parent(dev), dev, clocks));
677 * Power up an external PMU-managed resource assigned to @p dev.
679 * A driver must ask the bhnd bus to allocate PMU request state
680 * via bhnd_alloc_pmu() before it can request PMU resources.
682 * @param dev The requesting bhnd(4) device.
683 * @param rsrc The core-specific external resource identifier.
686 * @retval ENODEV If the PMU does not support @p rsrc.
687 * @retval ENXIO If the PMU has not been initialized or is otherwise unvailable.
690 bhnd_request_ext_rsrc(device_t dev, u_int rsrc)
692 return (BHND_BUS_REQUEST_EXT_RSRC(device_get_parent(dev), dev, rsrc));
696 * Power down an external PMU-managed resource assigned to @p dev.
698 * A driver must ask the bhnd bus to allocate PMU request state
699 * via bhnd_alloc_pmu() before it can request PMU resources.
701 * @param dev The requesting bhnd(4) device.
702 * @param rsrc The core-specific external resource identifier.
705 * @retval ENODEV If the PMU does not support @p rsrc.
706 * @retval ENXIO If the PMU has not been initialized or is otherwise unvailable.
709 bhnd_release_ext_rsrc(device_t dev, u_int rsrc)
711 return (BHND_BUS_RELEASE_EXT_RSRC(device_get_parent(dev), dev, rsrc));
716 * Read @p width bytes at @p offset from the bus-specific agent/config
719 * @param dev The bhnd device for which @p offset should be read.
720 * @param offset The offset to be read.
721 * @param width The size of the access. Must be 1, 2 or 4 bytes.
723 * The exact behavior of this method is bus-specific. In the case of
724 * bcma(4), this method provides access to the first agent port of @p child.
726 * @note Device drivers should only use this API for functionality
727 * that is not available via another bhnd(4) function.
729 static inline uint32_t
730 bhnd_read_config(device_t dev, bus_size_t offset, u_int width)
732 return (BHND_BUS_READ_CONFIG(device_get_parent(dev), dev, offset,
737 * Read @p width bytes at @p offset from the bus-specific agent/config
740 * @param dev The bhnd device for which @p offset should be read.
741 * @param offset The offset to be written.
742 * @param width The size of the access. Must be 1, 2 or 4 bytes.
744 * The exact behavior of this method is bus-specific. In the case of
745 * bcma(4), this method provides access to the first agent port of @p child.
747 * @note Device drivers should only use this API for functionality
748 * that is not available via another bhnd(4) function.
751 bhnd_write_config(device_t dev, bus_size_t offset, uint32_t val, u_int width)
753 BHND_BUS_WRITE_CONFIG(device_get_parent(dev), dev, offset, val, width);
757 * Read an NVRAM variable, coerced to the requested @p type.
759 * @param dev A bhnd bus child device.
760 * @param name The NVRAM variable name.
761 * @param[out] buf A buffer large enough to hold @p len bytes. On
762 * success, the requested value will be written to
763 * this buffer. This argment may be NULL if
764 * the value is not desired.
765 * @param[in,out] len The maximum capacity of @p buf. On success,
766 * will be set to the actual size of the requested
768 * @param type The desired data representation to be written
772 * @retval ENOENT The requested variable was not found.
773 * @retval ENODEV No valid NVRAM source could be found.
774 * @retval ENOMEM If a buffer of @p size is too small to hold the
776 * @retval EOPNOTSUPP If the value cannot be coerced to @p type.
777 * @retval ERANGE If value coercion would overflow @p type.
778 * @retval non-zero If reading @p name otherwise fails, a regular unix
779 * error code will be returned.
782 bhnd_nvram_getvar(device_t dev, const char *name, void *buf, size_t *len,
783 bhnd_nvram_type type)
785 return (BHND_BUS_GET_NVRAM_VAR(device_get_parent(dev), dev, name, buf,
790 * Allocate a resource from a device's parent bhnd(4) bus.
792 * @param dev The device requesting resource ownership.
793 * @param type The type of resource to allocate. This may be any type supported
794 * by the standard bus APIs.
795 * @param rid The bus-specific handle identifying the resource being allocated.
796 * @param start The start address of the resource.
797 * @param end The end address of the resource.
798 * @param count The size of the resource.
799 * @param flags The flags for the resource to be allocated. These may be any
800 * values supported by the standard bus APIs.
802 * To request the resource's default addresses, pass @p start and
803 * @p end values of @c 0 and @c ~0, respectively, and
804 * a @p count of @c 1.
806 * @retval NULL The resource could not be allocated.
807 * @retval resource The allocated resource.
809 static inline struct bhnd_resource *
810 bhnd_alloc_resource(device_t dev, int type, int *rid, rman_res_t start,
811 rman_res_t end, rman_res_t count, u_int flags)
813 return BHND_BUS_ALLOC_RESOURCE(device_get_parent(dev), dev, type, rid,
814 start, end, count, flags);
819 * Allocate a resource from a device's parent bhnd(4) bus, using the
820 * resource's default start, end, and count values.
822 * @param dev The device requesting resource ownership.
823 * @param type The type of resource to allocate. This may be any type supported
824 * by the standard bus APIs.
825 * @param rid The bus-specific handle identifying the resource being allocated.
826 * @param flags The flags for the resource to be allocated. These may be any
827 * values supported by the standard bus APIs.
829 * @retval NULL The resource could not be allocated.
830 * @retval resource The allocated resource.
832 static inline struct bhnd_resource *
833 bhnd_alloc_resource_any(device_t dev, int type, int *rid, u_int flags)
835 return bhnd_alloc_resource(dev, type, rid, 0, ~0, 1, flags);
839 * Activate a previously allocated bhnd resource.
841 * @param dev The device holding ownership of the allocated resource.
842 * @param type The type of the resource.
843 * @param rid The bus-specific handle identifying the resource.
844 * @param r A pointer to the resource returned by bhnd_alloc_resource or
845 * BHND_BUS_ALLOC_RESOURCE.
848 * @retval non-zero an error occurred while activating the resource.
851 bhnd_activate_resource(device_t dev, int type, int rid,
852 struct bhnd_resource *r)
854 return BHND_BUS_ACTIVATE_RESOURCE(device_get_parent(dev), dev, type,
859 * Deactivate a previously activated bhnd resource.
861 * @param dev The device holding ownership of the activated resource.
862 * @param type The type of the resource.
863 * @param rid The bus-specific handle identifying the resource.
864 * @param r A pointer to the resource returned by bhnd_alloc_resource or
865 * BHND_BUS_ALLOC_RESOURCE.
868 * @retval non-zero an error occurred while activating the resource.
871 bhnd_deactivate_resource(device_t dev, int type, int rid,
872 struct bhnd_resource *r)
874 return BHND_BUS_DEACTIVATE_RESOURCE(device_get_parent(dev), dev, type,
879 * Free a resource allocated by bhnd_alloc_resource().
881 * @param dev The device holding ownership of the resource.
882 * @param type The type of the resource.
883 * @param rid The bus-specific handle identifying the resource.
884 * @param r A pointer to the resource returned by bhnd_alloc_resource or
885 * BHND_ALLOC_RESOURCE.
888 * @retval non-zero an error occurred while activating the resource.
891 bhnd_release_resource(device_t dev, int type, int rid,
892 struct bhnd_resource *r)
894 return BHND_BUS_RELEASE_RESOURCE(device_get_parent(dev), dev, type,
899 * Return true if @p region_num is a valid region on @p port_num of
900 * @p type attached to @p dev.
902 * @param dev A bhnd bus child device.
903 * @param type The port type being queried.
904 * @param port_num The port number being queried.
905 * @param region_num The region number being queried.
908 bhnd_is_region_valid(device_t dev, bhnd_port_type type, u_int port_num,
911 return (BHND_BUS_IS_REGION_VALID(device_get_parent(dev), dev, type,
912 port_num, region_num));
916 * Return the number of ports of type @p type attached to @p def.
918 * @param dev A bhnd bus child device.
919 * @param type The port type being queried.
922 bhnd_get_port_count(device_t dev, bhnd_port_type type) {
923 return (BHND_BUS_GET_PORT_COUNT(device_get_parent(dev), dev, type));
927 * Return the number of memory regions mapped to @p child @p port of
930 * @param dev A bhnd bus child device.
931 * @param port The port number being queried.
932 * @param type The port type being queried.
935 bhnd_get_region_count(device_t dev, bhnd_port_type type, u_int port) {
936 return (BHND_BUS_GET_REGION_COUNT(device_get_parent(dev), dev, type,
941 * Return the resource-ID for a memory region on the given device port.
943 * @param dev A bhnd bus child device.
944 * @param type The port type.
945 * @param port The port identifier.
946 * @param region The identifier of the memory region on @p port.
948 * @retval int The RID for the given @p port and @p region on @p device.
949 * @retval -1 No such port/region found.
952 bhnd_get_port_rid(device_t dev, bhnd_port_type type, u_int port, u_int region)
954 return BHND_BUS_GET_PORT_RID(device_get_parent(dev), dev, type, port,
959 * Decode a port / region pair on @p dev defined by @p rid.
961 * @param dev A bhnd bus child device.
962 * @param type The resource type.
963 * @param rid The resource identifier.
964 * @param[out] port_type The decoded port type.
965 * @param[out] port The decoded port identifier.
966 * @param[out] region The decoded region identifier.
969 * @retval non-zero No matching port/region found.
972 bhnd_decode_port_rid(device_t dev, int type, int rid, bhnd_port_type *port_type,
973 u_int *port, u_int *region)
975 return BHND_BUS_DECODE_PORT_RID(device_get_parent(dev), dev, type, rid,
976 port_type, port, region);
980 * Get the address and size of @p region on @p port.
982 * @param dev A bhnd bus child device.
983 * @param port_type The port type.
984 * @param port The port identifier.
985 * @param region The identifier of the memory region on @p port.
986 * @param[out] region_addr The region's base address.
987 * @param[out] region_size The region's size.
990 * @retval non-zero No matching port/region found.
993 bhnd_get_region_addr(device_t dev, bhnd_port_type port_type, u_int port,
994 u_int region, bhnd_addr_t *region_addr, bhnd_size_t *region_size)
996 return BHND_BUS_GET_REGION_ADDR(device_get_parent(dev), dev, port_type,
997 port, region, region_addr, region_size);
1001 * bhnd bus-level equivalents of the bus_(read|write|set|barrier|...)
1002 * macros (compatible with bhnd_resource).
1004 * Generated with bhnd/tools/bus_macro.sh
1006 #define bhnd_bus_barrier(r, o, l, f) \
1008 bus_barrier((r)->res, (o), (l), (f)) : \
1010 device_get_parent(rman_get_device((r)->res)), \
1011 rman_get_device((r)->res), (r), (o), (l), (f))
1012 #define bhnd_bus_read_1(r, o) \
1014 bus_read_1((r)->res, (o)) : \
1016 device_get_parent(rman_get_device((r)->res)), \
1017 rman_get_device((r)->res), (r), (o))
1018 #define bhnd_bus_read_multi_1(r, o, d, c) \
1020 bus_read_multi_1((r)->res, (o), (d), (c)) : \
1021 BHND_BUS_READ_MULTI_1( \
1022 device_get_parent(rman_get_device((r)->res)), \
1023 rman_get_device((r)->res), (r), (o), (d), (c))
1024 #define bhnd_bus_read_region_1(r, o, d, c) \
1026 bus_read_region_1((r)->res, (o), (d), (c)) : \
1027 BHND_BUS_READ_REGION_1( \
1028 device_get_parent(rman_get_device((r)->res)), \
1029 rman_get_device((r)->res), (r), (o), (d), (c))
1030 #define bhnd_bus_write_1(r, o, v) \
1032 bus_write_1((r)->res, (o), (v)) : \
1034 device_get_parent(rman_get_device((r)->res)), \
1035 rman_get_device((r)->res), (r), (o), (v))
1036 #define bhnd_bus_write_multi_1(r, o, d, c) \
1038 bus_write_multi_1((r)->res, (o), (d), (c)) : \
1039 BHND_BUS_WRITE_MULTI_1( \
1040 device_get_parent(rman_get_device((r)->res)), \
1041 rman_get_device((r)->res), (r), (o), (d), (c))
1042 #define bhnd_bus_write_region_1(r, o, d, c) \
1044 bus_write_region_1((r)->res, (o), (d), (c)) : \
1045 BHND_BUS_WRITE_REGION_1( \
1046 device_get_parent(rman_get_device((r)->res)), \
1047 rman_get_device((r)->res), (r), (o), (d), (c))
1048 #define bhnd_bus_read_stream_1(r, o) \
1050 bus_read_stream_1((r)->res, (o)) : \
1051 BHND_BUS_READ_STREAM_1( \
1052 device_get_parent(rman_get_device((r)->res)), \
1053 rman_get_device((r)->res), (r), (o))
1054 #define bhnd_bus_read_multi_stream_1(r, o, d, c) \
1056 bus_read_multi_stream_1((r)->res, (o), (d), (c)) : \
1057 BHND_BUS_READ_MULTI_STREAM_1( \
1058 device_get_parent(rman_get_device((r)->res)), \
1059 rman_get_device((r)->res), (r), (o), (d), (c))
1060 #define bhnd_bus_read_region_stream_1(r, o, d, c) \
1062 bus_read_region_stream_1((r)->res, (o), (d), (c)) : \
1063 BHND_BUS_READ_REGION_STREAM_1( \
1064 device_get_parent(rman_get_device((r)->res)), \
1065 rman_get_device((r)->res), (r), (o), (d), (c))
1066 #define bhnd_bus_write_stream_1(r, o, v) \
1068 bus_write_stream_1((r)->res, (o), (v)) : \
1069 BHND_BUS_WRITE_STREAM_1( \
1070 device_get_parent(rman_get_device((r)->res)), \
1071 rman_get_device((r)->res), (r), (o), (v))
1072 #define bhnd_bus_write_multi_stream_1(r, o, d, c) \
1074 bus_write_multi_stream_1((r)->res, (o), (d), (c)) : \
1075 BHND_BUS_WRITE_MULTI_STREAM_1( \
1076 device_get_parent(rman_get_device((r)->res)), \
1077 rman_get_device((r)->res), (r), (o), (d), (c))
1078 #define bhnd_bus_write_region_stream_1(r, o, d, c) \
1080 bus_write_region_stream_1((r)->res, (o), (d), (c)) : \
1081 BHND_BUS_WRITE_REGION_STREAM_1( \
1082 device_get_parent(rman_get_device((r)->res)), \
1083 rman_get_device((r)->res), (r), (o), (d), (c))
1084 #define bhnd_bus_set_multi_1(r, o, v, c) \
1086 bus_set_multi_1((r)->res, (o), (v), (c)) : \
1087 BHND_BUS_SET_MULTI_1( \
1088 device_get_parent(rman_get_device((r)->res)), \
1089 rman_get_device((r)->res), (r), (o), (v), (c))
1090 #define bhnd_bus_set_region_1(r, o, v, c) \
1092 bus_set_region_1((r)->res, (o), (v), (c)) : \
1093 BHND_BUS_SET_REGION_1( \
1094 device_get_parent(rman_get_device((r)->res)), \
1095 rman_get_device((r)->res), (r), (o), (v), (c))
1096 #define bhnd_bus_read_2(r, o) \
1098 bus_read_2((r)->res, (o)) : \
1100 device_get_parent(rman_get_device((r)->res)), \
1101 rman_get_device((r)->res), (r), (o))
1102 #define bhnd_bus_read_multi_2(r, o, d, c) \
1104 bus_read_multi_2((r)->res, (o), (d), (c)) : \
1105 BHND_BUS_READ_MULTI_2( \
1106 device_get_parent(rman_get_device((r)->res)), \
1107 rman_get_device((r)->res), (r), (o), (d), (c))
1108 #define bhnd_bus_read_region_2(r, o, d, c) \
1110 bus_read_region_2((r)->res, (o), (d), (c)) : \
1111 BHND_BUS_READ_REGION_2( \
1112 device_get_parent(rman_get_device((r)->res)), \
1113 rman_get_device((r)->res), (r), (o), (d), (c))
1114 #define bhnd_bus_write_2(r, o, v) \
1116 bus_write_2((r)->res, (o), (v)) : \
1118 device_get_parent(rman_get_device((r)->res)), \
1119 rman_get_device((r)->res), (r), (o), (v))
1120 #define bhnd_bus_write_multi_2(r, o, d, c) \
1122 bus_write_multi_2((r)->res, (o), (d), (c)) : \
1123 BHND_BUS_WRITE_MULTI_2( \
1124 device_get_parent(rman_get_device((r)->res)), \
1125 rman_get_device((r)->res), (r), (o), (d), (c))
1126 #define bhnd_bus_write_region_2(r, o, d, c) \
1128 bus_write_region_2((r)->res, (o), (d), (c)) : \
1129 BHND_BUS_WRITE_REGION_2( \
1130 device_get_parent(rman_get_device((r)->res)), \
1131 rman_get_device((r)->res), (r), (o), (d), (c))
1132 #define bhnd_bus_read_stream_2(r, o) \
1134 bus_read_stream_2((r)->res, (o)) : \
1135 BHND_BUS_READ_STREAM_2( \
1136 device_get_parent(rman_get_device((r)->res)), \
1137 rman_get_device((r)->res), (r), (o))
1138 #define bhnd_bus_read_multi_stream_2(r, o, d, c) \
1140 bus_read_multi_stream_2((r)->res, (o), (d), (c)) : \
1141 BHND_BUS_READ_MULTI_STREAM_2( \
1142 device_get_parent(rman_get_device((r)->res)), \
1143 rman_get_device((r)->res), (r), (o), (d), (c))
1144 #define bhnd_bus_read_region_stream_2(r, o, d, c) \
1146 bus_read_region_stream_2((r)->res, (o), (d), (c)) : \
1147 BHND_BUS_READ_REGION_STREAM_2( \
1148 device_get_parent(rman_get_device((r)->res)), \
1149 rman_get_device((r)->res), (r), (o), (d), (c))
1150 #define bhnd_bus_write_stream_2(r, o, v) \
1152 bus_write_stream_2((r)->res, (o), (v)) : \
1153 BHND_BUS_WRITE_STREAM_2( \
1154 device_get_parent(rman_get_device((r)->res)), \
1155 rman_get_device((r)->res), (r), (o), (v))
1156 #define bhnd_bus_write_multi_stream_2(r, o, d, c) \
1158 bus_write_multi_stream_2((r)->res, (o), (d), (c)) : \
1159 BHND_BUS_WRITE_MULTI_STREAM_2( \
1160 device_get_parent(rman_get_device((r)->res)), \
1161 rman_get_device((r)->res), (r), (o), (d), (c))
1162 #define bhnd_bus_write_region_stream_2(r, o, d, c) \
1164 bus_write_region_stream_2((r)->res, (o), (d), (c)) : \
1165 BHND_BUS_WRITE_REGION_STREAM_2( \
1166 device_get_parent(rman_get_device((r)->res)), \
1167 rman_get_device((r)->res), (r), (o), (d), (c))
1168 #define bhnd_bus_set_multi_2(r, o, v, c) \
1170 bus_set_multi_2((r)->res, (o), (v), (c)) : \
1171 BHND_BUS_SET_MULTI_2( \
1172 device_get_parent(rman_get_device((r)->res)), \
1173 rman_get_device((r)->res), (r), (o), (v), (c))
1174 #define bhnd_bus_set_region_2(r, o, v, c) \
1176 bus_set_region_2((r)->res, (o), (v), (c)) : \
1177 BHND_BUS_SET_REGION_2( \
1178 device_get_parent(rman_get_device((r)->res)), \
1179 rman_get_device((r)->res), (r), (o), (v), (c))
1180 #define bhnd_bus_read_4(r, o) \
1182 bus_read_4((r)->res, (o)) : \
1184 device_get_parent(rman_get_device((r)->res)), \
1185 rman_get_device((r)->res), (r), (o))
1186 #define bhnd_bus_read_multi_4(r, o, d, c) \
1188 bus_read_multi_4((r)->res, (o), (d), (c)) : \
1189 BHND_BUS_READ_MULTI_4( \
1190 device_get_parent(rman_get_device((r)->res)), \
1191 rman_get_device((r)->res), (r), (o), (d), (c))
1192 #define bhnd_bus_read_region_4(r, o, d, c) \
1194 bus_read_region_4((r)->res, (o), (d), (c)) : \
1195 BHND_BUS_READ_REGION_4( \
1196 device_get_parent(rman_get_device((r)->res)), \
1197 rman_get_device((r)->res), (r), (o), (d), (c))
1198 #define bhnd_bus_write_4(r, o, v) \
1200 bus_write_4((r)->res, (o), (v)) : \
1202 device_get_parent(rman_get_device((r)->res)), \
1203 rman_get_device((r)->res), (r), (o), (v))
1204 #define bhnd_bus_write_multi_4(r, o, d, c) \
1206 bus_write_multi_4((r)->res, (o), (d), (c)) : \
1207 BHND_BUS_WRITE_MULTI_4( \
1208 device_get_parent(rman_get_device((r)->res)), \
1209 rman_get_device((r)->res), (r), (o), (d), (c))
1210 #define bhnd_bus_write_region_4(r, o, d, c) \
1212 bus_write_region_4((r)->res, (o), (d), (c)) : \
1213 BHND_BUS_WRITE_REGION_4( \
1214 device_get_parent(rman_get_device((r)->res)), \
1215 rman_get_device((r)->res), (r), (o), (d), (c))
1216 #define bhnd_bus_read_stream_4(r, o) \
1218 bus_read_stream_4((r)->res, (o)) : \
1219 BHND_BUS_READ_STREAM_4( \
1220 device_get_parent(rman_get_device((r)->res)), \
1221 rman_get_device((r)->res), (r), (o))
1222 #define bhnd_bus_read_multi_stream_4(r, o, d, c) \
1224 bus_read_multi_stream_4((r)->res, (o), (d), (c)) : \
1225 BHND_BUS_READ_MULTI_STREAM_4( \
1226 device_get_parent(rman_get_device((r)->res)), \
1227 rman_get_device((r)->res), (r), (o), (d), (c))
1228 #define bhnd_bus_read_region_stream_4(r, o, d, c) \
1230 bus_read_region_stream_4((r)->res, (o), (d), (c)) : \
1231 BHND_BUS_READ_REGION_STREAM_4( \
1232 device_get_parent(rman_get_device((r)->res)), \
1233 rman_get_device((r)->res), (r), (o), (d), (c))
1234 #define bhnd_bus_write_stream_4(r, o, v) \
1236 bus_write_stream_4((r)->res, (o), (v)) : \
1237 BHND_BUS_WRITE_STREAM_4( \
1238 device_get_parent(rman_get_device((r)->res)), \
1239 rman_get_device((r)->res), (r), (o), (v))
1240 #define bhnd_bus_write_multi_stream_4(r, o, d, c) \
1242 bus_write_multi_stream_4((r)->res, (o), (d), (c)) : \
1243 BHND_BUS_WRITE_MULTI_STREAM_4( \
1244 device_get_parent(rman_get_device((r)->res)), \
1245 rman_get_device((r)->res), (r), (o), (d), (c))
1246 #define bhnd_bus_write_region_stream_4(r, o, d, c) \
1248 bus_write_region_stream_4((r)->res, (o), (d), (c)) : \
1249 BHND_BUS_WRITE_REGION_STREAM_4( \
1250 device_get_parent(rman_get_device((r)->res)), \
1251 rman_get_device((r)->res), (r), (o), (d), (c))
1252 #define bhnd_bus_set_multi_4(r, o, v, c) \
1254 bus_set_multi_4((r)->res, (o), (v), (c)) : \
1255 BHND_BUS_SET_MULTI_4( \
1256 device_get_parent(rman_get_device((r)->res)), \
1257 rman_get_device((r)->res), (r), (o), (v), (c))
1258 #define bhnd_bus_set_region_4(r, o, v, c) \
1260 bus_set_region_4((r)->res, (o), (v), (c)) : \
1261 BHND_BUS_SET_REGION_4( \
1262 device_get_parent(rman_get_device((r)->res)), \
1263 rman_get_device((r)->res), (r), (o), (v), (c))
1265 #endif /* _BHND_BHND_H_ */