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_debug.h"
43 #include "bhnd_bus_if.h"
44 #include "bhnd_match.h"
46 #include "nvram/bhnd_nvram.h"
48 extern devclass_t bhnd_devclass;
49 extern devclass_t bhnd_hostb_devclass;
50 extern devclass_t bhnd_nvram_devclass;
52 #define BHND_CHIPID_MAX_NAMELEN 32 /**< maximum buffer required for a
53 bhnd_format_chip_id() */
56 * bhnd child instance variables
58 enum bhnd_device_vars {
59 BHND_IVAR_VENDOR, /**< Designer's JEP-106 manufacturer ID. */
60 BHND_IVAR_DEVICE, /**< Part number */
61 BHND_IVAR_HWREV, /**< Core revision */
62 BHND_IVAR_DEVICE_CLASS, /**< Core class (@sa bhnd_devclass_t) */
63 BHND_IVAR_VENDOR_NAME, /**< Core vendor name */
64 BHND_IVAR_DEVICE_NAME, /**< Core name */
65 BHND_IVAR_CORE_INDEX, /**< Bus-assigned core number */
66 BHND_IVAR_CORE_UNIT, /**< Bus-assigned core unit number,
67 assigned sequentially (starting at 0) for
68 each vendor/device pair. */
72 * bhnd device probe priority bands.
75 BHND_PROBE_ROOT = 0, /**< Nexus or host bridge */
76 BHND_PROBE_BUS = 1000, /**< Busses and bridges */
77 BHND_PROBE_CPU = 2000, /**< CPU devices */
78 BHND_PROBE_INTERRUPT = 3000, /**< Interrupt controllers. */
79 BHND_PROBE_TIMER = 4000, /**< Timers and clocks. */
80 BHND_PROBE_RESOURCE = 5000, /**< Resource discovery (including NVRAM/SPROM) */
81 BHND_PROBE_DEFAULT = 6000, /**< Default device priority */
85 * Constants defining fine grained ordering within a BHND_PROBE_* priority band.
89 * BHND_PROBE_BUS + BHND_PROBE_ORDER_FIRST
93 BHND_PROBE_ORDER_FIRST = 0,
94 BHND_PROBE_ORDER_EARLY = 25,
95 BHND_PROBE_ORDER_MIDDLE = 50,
96 BHND_PROBE_ORDER_LATE = 75,
97 BHND_PROBE_ORDER_LAST = 100
102 * Simplified accessors for bhnd device ivars
104 #define BHND_ACCESSOR(var, ivar, type) \
105 __BUS_ACCESSOR(bhnd, var, BHND, ivar, type)
107 BHND_ACCESSOR(vendor, VENDOR, uint16_t);
108 BHND_ACCESSOR(device, DEVICE, uint16_t);
109 BHND_ACCESSOR(hwrev, HWREV, uint8_t);
110 BHND_ACCESSOR(class, DEVICE_CLASS, bhnd_devclass_t);
111 BHND_ACCESSOR(vendor_name, VENDOR_NAME, const char *);
112 BHND_ACCESSOR(device_name, DEVICE_NAME, const char *);
113 BHND_ACCESSOR(core_index, CORE_INDEX, u_int);
114 BHND_ACCESSOR(core_unit, CORE_UNIT, int);
119 * A bhnd(4) board descriptor.
121 struct bhnd_board_info {
122 uint16_t board_vendor; /**< PCI-SIG vendor ID (even on non-PCI
125 * On PCI devices, this will generally
126 * be the subsystem vendor ID, but the
127 * value may be overridden in device
130 uint16_t board_type; /**< Board type (See BHND_BOARD_*)
132 * On PCI devices, this will generally
133 * be the subsystem device ID, but the
134 * value may be overridden in device
137 uint16_t board_rev; /**< Board revision. */
138 uint8_t board_srom_rev; /**< Board SROM format revision */
140 uint32_t board_flags; /**< Board flags (see BHND_BFL_*) */
141 uint32_t board_flags2; /**< Board flags 2 (see BHND_BFL2_*) */
142 uint32_t board_flags3; /**< Board flags 3 (see BHND_BFL3_*) */
147 * Chip Identification
149 * This is read from the ChipCommon ID register; on earlier bhnd(4) devices
150 * where ChipCommon is unavailable, known values must be supplied.
153 uint16_t chip_id; /**< chip id (BHND_CHIPID_*) */
154 uint8_t chip_rev; /**< chip revision */
155 uint8_t chip_pkg; /**< chip package (BHND_PKGID_*) */
156 uint8_t chip_type; /**< chip type (BHND_CHIPTYPE_*) */
158 bhnd_addr_t enum_addr; /**< chip_type-specific enumeration
159 * address; either the siba(4) base
160 * core register block, or the bcma(4)
161 * EROM core address. */
163 uint8_t ncores; /**< number of cores, if known. 0 if
168 * A bhnd(4) core descriptor.
170 struct bhnd_core_info {
171 uint16_t vendor; /**< JEP-106 vendor (BHND_MFGID_*) */
172 uint16_t device; /**< device */
173 uint16_t hwrev; /**< hardware revision */
174 u_int core_idx; /**< bus-assigned core index */
175 int unit; /**< bus-assigned core unit */
179 * A bhnd(4) bus resource.
181 * This provides an abstract interface to per-core resources that may require
182 * bus-level remapping of address windows prior to access.
184 struct bhnd_resource {
185 struct resource *res; /**< the system resource. */
186 bool direct; /**< false if the resource requires
187 * bus window remapping before it
188 * is MMIO accessible. */
192 * Device quirk table descriptor.
194 struct bhnd_device_quirk {
195 struct bhnd_device_match desc; /**< device match descriptor */
196 uint32_t quirks; /**< quirk flags */
199 #define BHND_CORE_QUIRK(_rev, _flags) \
200 {{ BHND_MATCH_CORE_REV(_rev) }, (_flags) }
202 #define BHND_CHIP_QUIRK(_chip, _rev, _flags) \
203 {{ BHND_CHIP_IR(BCM ## _chip, _rev) }, (_flags) }
205 #define BHND_PKG_QUIRK(_chip, _pkg, _flags) \
206 {{ BHND_CHIP_IP(BCM ## _chip, BCM ## _chip ## _pkg) }, (_flags) }
208 #define BHND_BOARD_QUIRK(_board, _flags) \
209 {{ BHND_MATCH_BOARD_TYPE(_board) }, \
212 #define BHND_DEVICE_QUIRK_END { { BHND_MATCH_ANY }, 0 }
213 #define BHND_DEVICE_QUIRK_IS_END(_q) \
214 (((_q)->desc.m.match_flags == 0) && (_q)->quirks == 0)
218 BHND_DF_HOSTB = (1<<0), /**< core is serving as the bus' host
219 * bridge. implies BHND_DF_ADAPTER */
220 BHND_DF_SOC = (1<<1), /**< core is attached to a native
221 bus (BHND_ATTACH_NATIVE) */
222 BHND_DF_ADAPTER = (1<<2), /**< core is attached to a bridged
223 * adapter (BHND_ATTACH_ADAPTER) */
226 /** Device probe table descriptor */
228 const struct bhnd_device_match core; /**< core match descriptor */
229 const char *desc; /**< device description, or NULL. */
230 const struct bhnd_device_quirk *quirks_table; /**< quirks table for this device, or NULL */
231 uint32_t device_flags; /**< required BHND_DF_* flags */
234 #define _BHND_DEVICE(_vendor, _device, _desc, _quirks, \
236 { { BHND_MATCH_CORE(BHND_MFGID_ ## _vendor, \
237 BHND_COREID_ ## _device) }, _desc, _quirks, \
240 #define BHND_DEVICE(_vendor, _device, _desc, _quirks, ...) \
241 _BHND_DEVICE(_vendor, _device, _desc, _quirks, \
244 #define BHND_DEVICE_END { { BHND_MATCH_ANY }, NULL, NULL, 0 }
245 #define BHND_DEVICE_IS_END(_d) \
246 (BHND_MATCH_IS_ANY(&(_d)->core) && (_d)->desc == NULL)
248 const char *bhnd_vendor_name(uint16_t vendor);
249 const char *bhnd_port_type_name(bhnd_port_type port_type);
250 const char *bhnd_nvram_src_name(bhnd_nvram_src nvram_src);
252 const char *bhnd_find_core_name(uint16_t vendor,
254 bhnd_devclass_t bhnd_find_core_class(uint16_t vendor,
257 const char *bhnd_core_name(const struct bhnd_core_info *ci);
258 bhnd_devclass_t bhnd_core_class(const struct bhnd_core_info *ci);
260 int bhnd_format_chip_id(char *buffer, size_t size,
263 device_t bhnd_match_child(device_t dev,
264 const struct bhnd_core_match *desc);
266 device_t bhnd_find_child(device_t dev,
267 bhnd_devclass_t class, int unit);
269 device_t bhnd_find_bridge_root(device_t dev,
270 devclass_t bus_class);
272 const struct bhnd_core_info *bhnd_match_core(
273 const struct bhnd_core_info *cores,
275 const struct bhnd_core_match *desc);
277 const struct bhnd_core_info *bhnd_find_core(
278 const struct bhnd_core_info *cores,
279 u_int num_cores, bhnd_devclass_t class);
281 bool bhnd_core_matches(
282 const struct bhnd_core_info *core,
283 const struct bhnd_core_match *desc);
285 bool bhnd_chip_matches(
286 const struct bhnd_chipid *chipid,
287 const struct bhnd_chip_match *desc);
289 bool bhnd_board_matches(
290 const struct bhnd_board_info *info,
291 const struct bhnd_board_match *desc);
293 bool bhnd_hwrev_matches(uint16_t hwrev,
294 const struct bhnd_hwrev_match *desc);
296 bool bhnd_device_matches(device_t dev,
297 const struct bhnd_device_match *desc);
299 const struct bhnd_device *bhnd_device_lookup(device_t dev,
300 const struct bhnd_device *table,
303 uint32_t bhnd_device_quirks(device_t dev,
304 const struct bhnd_device *table,
307 struct bhnd_core_info bhnd_get_core_info(device_t dev);
309 int bhnd_alloc_resources(device_t dev,
310 struct resource_spec *rs,
311 struct bhnd_resource **res);
313 void bhnd_release_resources(device_t dev,
314 const struct resource_spec *rs,
315 struct bhnd_resource **res);
317 struct bhnd_chipid bhnd_parse_chipid(uint32_t idreg,
318 bhnd_addr_t enum_addr);
320 int bhnd_read_chipid(device_t dev,
321 struct resource_spec *rs,
322 bus_size_t chipc_offset,
323 struct bhnd_chipid *result);
325 void bhnd_set_custom_core_desc(device_t dev,
327 void bhnd_set_default_core_desc(device_t dev);
329 void bhnd_set_default_bus_desc(device_t dev,
330 const struct bhnd_chipid *chip_id);
332 int bhnd_nvram_getvar_str(device_t dev,
333 const char *name, char *buf, size_t len,
336 int bhnd_nvram_getvar_uint(device_t dev,
337 const char *name, void *value, int width);
338 int bhnd_nvram_getvar_uint8(device_t dev,
339 const char *name, uint8_t *value);
340 int bhnd_nvram_getvar_uint16(device_t dev,
341 const char *name, uint16_t *value);
342 int bhnd_nvram_getvar_uint32(device_t dev,
343 const char *name, uint32_t *value);
345 int bhnd_nvram_getvar_int(device_t dev,
346 const char *name, void *value, int width);
347 int bhnd_nvram_getvar_int8(device_t dev,
348 const char *name, int8_t *value);
349 int bhnd_nvram_getvar_int16(device_t dev,
350 const char *name, int16_t *value);
351 int bhnd_nvram_getvar_int32(device_t dev,
352 const char *name, int32_t *value);
354 int bhnd_nvram_getvar_array(device_t dev,
355 const char *name, void *buf, size_t count,
356 bhnd_nvram_type type);
358 bool bhnd_bus_generic_is_hw_disabled(device_t dev,
360 bool bhnd_bus_generic_is_region_valid(device_t dev,
361 device_t child, bhnd_port_type type,
362 u_int port, u_int region);
363 int bhnd_bus_generic_get_nvram_var(device_t dev,
364 device_t child, const char *name,
365 void *buf, size_t *size,
366 bhnd_nvram_type type);
367 const struct bhnd_chipid *bhnd_bus_generic_get_chipid(device_t dev,
369 int bhnd_bus_generic_read_board_info(device_t dev,
371 struct bhnd_board_info *info);
372 struct bhnd_resource *bhnd_bus_generic_alloc_resource (device_t dev,
373 device_t child, int type, int *rid,
374 rman_res_t start, rman_res_t end,
375 rman_res_t count, u_int flags);
376 int bhnd_bus_generic_release_resource (device_t dev,
377 device_t child, int type, int rid,
378 struct bhnd_resource *r);
379 int bhnd_bus_generic_activate_resource (device_t dev,
380 device_t child, int type, int rid,
381 struct bhnd_resource *r);
382 int bhnd_bus_generic_deactivate_resource (device_t dev,
383 device_t child, int type, int rid,
384 struct bhnd_resource *r);
385 bhnd_attach_type bhnd_bus_generic_get_attach_type(device_t dev,
391 * Return the active host bridge core for the bhnd bus, if any, or NULL if
394 * @param dev A bhnd bus device.
396 static inline device_t
397 bhnd_find_hostb_device(device_t dev) {
398 return (BHND_BUS_FIND_HOSTB_DEVICE(dev));
402 * Return true if the hardware components required by @p dev are known to be
403 * unpopulated or otherwise unusable.
405 * In some cases, enumerated devices may have pins that are left floating, or
406 * the hardware may otherwise be non-functional; this method allows a parent
407 * device to explicitly specify if a successfully enumerated @p dev should
410 * @param dev A bhnd bus child device.
413 bhnd_is_hw_disabled(device_t dev) {
414 return (BHND_BUS_IS_HW_DISABLED(device_get_parent(dev), dev));
418 * Return the BHND chip identification info for the bhnd bus.
420 * @param dev A bhnd bus child device.
422 static inline const struct bhnd_chipid *
423 bhnd_get_chipid(device_t dev) {
424 return (BHND_BUS_GET_CHIPID(device_get_parent(dev), dev));
428 * If supported by the chipset, return the clock source for the given clock.
430 * This function is only supported on early PWRCTL-equipped chipsets
431 * that expose clock management via their host bridge interface. Currently,
432 * this includes PCI (not PCIe) devices, with ChipCommon core revisions 0-9.
434 * @param dev A bhnd bus child device.
435 * @param clock The clock for which a clock source will be returned.
437 * @retval bhnd_clksrc The clock source for @p clock.
438 * @retval BHND_CLKSRC_UNKNOWN If @p clock is unsupported, or its
439 * clock source is not known to the bus.
441 static inline bhnd_clksrc
442 bhnd_pwrctl_get_clksrc(device_t dev, bhnd_clock clock)
444 return (BHND_BUS_PWRCTL_GET_CLKSRC(device_get_parent(dev), dev, clock));
448 * If supported by the chipset, gate @p clock
450 * This function is only supported on early PWRCTL-equipped chipsets
451 * that expose clock management via their host bridge interface. Currently,
452 * this includes PCI (not PCIe) devices, with ChipCommon core revisions 0-9.
454 * @param dev A bhnd bus child device.
455 * @param clock The clock to be disabled.
458 * @retval ENODEV If bus-level clock source management is not supported.
459 * @retval ENXIO If bus-level management of @p clock is not supported.
462 bhnd_pwrctl_gate_clock(device_t dev, bhnd_clock clock)
464 return (BHND_BUS_PWRCTL_GATE_CLOCK(device_get_parent(dev), dev, clock));
468 * If supported by the chipset, ungate @p clock
470 * This function is only supported on early PWRCTL-equipped chipsets
471 * that expose clock management via their host bridge interface. Currently,
472 * this includes PCI (not PCIe) devices, with ChipCommon core revisions 0-9.
474 * @param dev A bhnd bus child device.
475 * @param clock The clock to be enabled.
478 * @retval ENODEV If bus-level clock source management is not supported.
479 * @retval ENXIO If bus-level management of @p clock is not supported.
482 bhnd_pwrctl_ungate_clock(device_t dev, bhnd_clock clock)
484 return (BHND_BUS_PWRCTL_UNGATE_CLOCK(device_get_parent(dev), dev,
489 * Return the BHND attachment type of the parent bhnd bus.
491 * @param dev A bhnd bus child device.
493 * @retval BHND_ATTACH_ADAPTER if the bus is resident on a bridged adapter,
494 * such as a WiFi chipset.
495 * @retval BHND_ATTACH_NATIVE if the bus provides hardware services (clock,
496 * CPU, etc) to a directly attached native host.
498 static inline bhnd_attach_type
499 bhnd_get_attach_type (device_t dev) {
500 return (BHND_BUS_GET_ATTACH_TYPE(device_get_parent(dev), dev));
504 * Attempt to read the BHND board identification from the bhnd bus.
506 * This relies on NVRAM access, and will fail if a valid NVRAM device cannot
507 * be found, or is not yet attached.
509 * @param dev The parent of @p child.
510 * @param child The bhnd device requesting board info.
511 * @param[out] info On success, will be populated with the bhnd(4) device's
515 * @retval ENODEV No valid NVRAM source could be found.
516 * @retval non-zero If reading @p name otherwise fails, a regular unix
517 * error code will be returned.
520 bhnd_read_board_info(device_t dev, struct bhnd_board_info *info)
522 return (BHND_BUS_READ_BOARD_INFO(device_get_parent(dev), dev, info));
526 * Allocate and enable per-core PMU request handling for @p child.
528 * The region containing the core's PMU register block (if any) must be
529 * allocated via bus_alloc_resource(9) (or bhnd_alloc_resource) before
530 * calling bhnd_alloc_pmu(), and must not be released until after
531 * calling bhnd_release_pmu().
533 * @param dev The parent of @p child.
534 * @param child The requesting bhnd device.
537 * @retval non-zero If allocating PMU request state otherwise fails, a
538 * regular unix error code will be returned.
541 bhnd_alloc_pmu(device_t dev)
543 return (BHND_BUS_ALLOC_PMU(device_get_parent(dev), dev));
547 * Release any per-core PMU resources allocated for @p child. Any outstanding
548 * PMU requests are are discarded.
550 * @param dev The parent of @p child.
551 * @param child The requesting bhnd device.
554 * @retval non-zero If releasing PMU request state otherwise fails, a
555 * regular unix error code will be returned, and
556 * the core state will be left unmodified.
559 bhnd_release_pmu(device_t dev)
561 return (BHND_BUS_RELEASE_PMU(device_get_parent(dev), dev));
565 * Request that @p clock (or faster) be routed to @p dev.
567 * A driver must ask the bhnd bus to allocate clock request state
568 * via bhnd_alloc_pmu() before it can request clock resources.
570 * Request multiplexing is managed by the bus.
572 * @param dev The bhnd(4) device to which @p clock should be routed.
573 * @param clock The requested clock source.
576 * @retval ENODEV If an unsupported clock was requested.
577 * @retval ENXIO If the PMU has not been initialized or is otherwise unvailable.
580 bhnd_request_clock(device_t dev, bhnd_clock clock)
582 return (BHND_BUS_REQUEST_CLOCK(device_get_parent(dev), dev, clock));
586 * Request that @p clocks be powered on behalf of @p dev.
588 * This will power any clock sources (e.g. XTAL, PLL, etc) required for
589 * @p clocks and wait until they are ready, discarding any previous
590 * requests by @p dev.
592 * Request multiplexing is managed by the bus.
594 * A driver must ask the bhnd bus to allocate clock request state
595 * via bhnd_alloc_pmu() before it can request clock resources.
597 * @param dev The requesting bhnd(4) device.
598 * @param clocks The clock(s) to be enabled.
601 * @retval ENODEV If an unsupported clock was requested.
602 * @retval ENXIO If the PMU has not been initialized or is otherwise unvailable.
605 bhnd_enable_clocks(device_t dev, uint32_t clocks)
607 return (BHND_BUS_ENABLE_CLOCKS(device_get_parent(dev), dev, clocks));
611 * Power up an external PMU-managed resource assigned to @p dev.
613 * A driver must ask the bhnd bus to allocate PMU request state
614 * via bhnd_alloc_pmu() before it can request PMU resources.
616 * @param dev The requesting bhnd(4) device.
617 * @param rsrc The core-specific external resource identifier.
620 * @retval ENODEV If the PMU does not support @p rsrc.
621 * @retval ENXIO If the PMU has not been initialized or is otherwise unvailable.
624 bhnd_request_ext_rsrc(device_t dev, u_int rsrc)
626 return (BHND_BUS_REQUEST_EXT_RSRC(device_get_parent(dev), dev, rsrc));
630 * Power down an external PMU-managed resource assigned to @p dev.
632 * A driver must ask the bhnd bus to allocate PMU request state
633 * via bhnd_alloc_pmu() before it can request PMU resources.
635 * @param dev The requesting bhnd(4) device.
636 * @param rsrc The core-specific external resource identifier.
639 * @retval ENODEV If the PMU does not support @p rsrc.
640 * @retval ENXIO If the PMU has not been initialized or is otherwise unvailable.
643 bhnd_release_ext_rsrc(device_t dev, u_int rsrc)
645 return (BHND_BUS_RELEASE_EXT_RSRC(device_get_parent(dev), dev, rsrc));
650 * Read @p width bytes at @p offset from the bus-specific agent/config
653 * @param dev The bhnd device for which @p offset should be read.
654 * @param offset The offset to be read.
655 * @param width The size of the access. Must be 1, 2 or 4 bytes.
657 * The exact behavior of this method is bus-specific. In the case of
658 * bcma(4), this method provides access to the first agent port of @p child.
660 * @note Device drivers should only use this API for functionality
661 * that is not available via another bhnd(4) function.
663 static inline uint32_t
664 bhnd_read_config(device_t dev, bus_size_t offset, u_int width)
666 return (BHND_BUS_READ_CONFIG(device_get_parent(dev), dev, offset,
671 * Read @p width bytes at @p offset from the bus-specific agent/config
674 * @param dev The bhnd device for which @p offset should be read.
675 * @param offset The offset to be written.
676 * @param width The size of the access. Must be 1, 2 or 4 bytes.
678 * The exact behavior of this method is bus-specific. In the case of
679 * bcma(4), this method provides access to the first agent port of @p child.
681 * @note Device drivers should only use this API for functionality
682 * that is not available via another bhnd(4) function.
685 bhnd_write_config(device_t dev, bus_size_t offset, uint32_t val, u_int width)
687 BHND_BUS_WRITE_CONFIG(device_get_parent(dev), dev, offset, val, width);
691 * Read an NVRAM variable, coerced to the requested @p type.
693 * @param dev A bhnd bus child device.
694 * @param name The NVRAM variable name.
695 * @param[out] buf A buffer large enough to hold @p len bytes. On
696 * success, the requested value will be written to
697 * this buffer. This argment may be NULL if
698 * the value is not desired.
699 * @param[in,out] len The maximum capacity of @p buf. On success,
700 * will be set to the actual size of the requested
702 * @param type The desired data representation to be written
706 * @retval ENOENT The requested variable was not found.
707 * @retval ENODEV No valid NVRAM source could be found.
708 * @retval ENOMEM If a buffer of @p size is too small to hold the
710 * @retval EOPNOTSUPP If the value cannot be coerced to @p type.
711 * @retval ERANGE If value coercion would overflow @p type.
712 * @retval non-zero If reading @p name otherwise fails, a regular unix
713 * error code will be returned.
716 bhnd_nvram_getvar(device_t dev, const char *name, void *buf, size_t *len,
717 bhnd_nvram_type type)
719 return (BHND_BUS_GET_NVRAM_VAR(device_get_parent(dev), dev, name, buf,
724 * Allocate a resource from a device's parent bhnd(4) bus.
726 * @param dev The device requesting resource ownership.
727 * @param type The type of resource to allocate. This may be any type supported
728 * by the standard bus APIs.
729 * @param rid The bus-specific handle identifying the resource being allocated.
730 * @param start The start address of the resource.
731 * @param end The end address of the resource.
732 * @param count The size of the resource.
733 * @param flags The flags for the resource to be allocated. These may be any
734 * values supported by the standard bus APIs.
736 * To request the resource's default addresses, pass @p start and
737 * @p end values of @c 0 and @c ~0, respectively, and
738 * a @p count of @c 1.
740 * @retval NULL The resource could not be allocated.
741 * @retval resource The allocated resource.
743 static inline struct bhnd_resource *
744 bhnd_alloc_resource(device_t dev, int type, int *rid, rman_res_t start,
745 rman_res_t end, rman_res_t count, u_int flags)
747 return BHND_BUS_ALLOC_RESOURCE(device_get_parent(dev), dev, type, rid,
748 start, end, count, flags);
753 * Allocate a resource from a device's parent bhnd(4) bus, using the
754 * resource's default start, end, and count values.
756 * @param dev The device requesting resource ownership.
757 * @param type The type of resource to allocate. This may be any type supported
758 * by the standard bus APIs.
759 * @param rid The bus-specific handle identifying the resource being allocated.
760 * @param flags The flags for the resource to be allocated. These may be any
761 * values supported by the standard bus APIs.
763 * @retval NULL The resource could not be allocated.
764 * @retval resource The allocated resource.
766 static inline struct bhnd_resource *
767 bhnd_alloc_resource_any(device_t dev, int type, int *rid, u_int flags)
769 return bhnd_alloc_resource(dev, type, rid, 0, ~0, 1, flags);
773 * Activate a previously allocated bhnd resource.
775 * @param dev The device holding ownership of the allocated resource.
776 * @param type The type of the resource.
777 * @param rid The bus-specific handle identifying the resource.
778 * @param r A pointer to the resource returned by bhnd_alloc_resource or
779 * BHND_BUS_ALLOC_RESOURCE.
782 * @retval non-zero an error occurred while activating the resource.
785 bhnd_activate_resource(device_t dev, int type, int rid,
786 struct bhnd_resource *r)
788 return BHND_BUS_ACTIVATE_RESOURCE(device_get_parent(dev), dev, type,
793 * Deactivate a previously activated bhnd resource.
795 * @param dev The device holding ownership of the activated resource.
796 * @param type The type of the resource.
797 * @param rid The bus-specific handle identifying the resource.
798 * @param r A pointer to the resource returned by bhnd_alloc_resource or
799 * BHND_BUS_ALLOC_RESOURCE.
802 * @retval non-zero an error occurred while activating the resource.
805 bhnd_deactivate_resource(device_t dev, int type, int rid,
806 struct bhnd_resource *r)
808 return BHND_BUS_DEACTIVATE_RESOURCE(device_get_parent(dev), dev, type,
813 * Free a resource allocated by bhnd_alloc_resource().
815 * @param dev The device holding ownership of the resource.
816 * @param type The type of the resource.
817 * @param rid The bus-specific handle identifying the resource.
818 * @param r A pointer to the resource returned by bhnd_alloc_resource or
819 * BHND_ALLOC_RESOURCE.
822 * @retval non-zero an error occurred while activating the resource.
825 bhnd_release_resource(device_t dev, int type, int rid,
826 struct bhnd_resource *r)
828 return BHND_BUS_RELEASE_RESOURCE(device_get_parent(dev), dev, type,
833 * Return true if @p region_num is a valid region on @p port_num of
834 * @p type attached to @p dev.
836 * @param dev A bhnd bus child device.
837 * @param type The port type being queried.
838 * @param port_num The port number being queried.
839 * @param region_num The region number being queried.
842 bhnd_is_region_valid(device_t dev, bhnd_port_type type, u_int port_num,
845 return (BHND_BUS_IS_REGION_VALID(device_get_parent(dev), dev, type,
846 port_num, region_num));
850 * Return the number of ports of type @p type attached to @p def.
852 * @param dev A bhnd bus child device.
853 * @param type The port type being queried.
856 bhnd_get_port_count(device_t dev, bhnd_port_type type) {
857 return (BHND_BUS_GET_PORT_COUNT(device_get_parent(dev), dev, type));
861 * Return the number of memory regions mapped to @p child @p port of
864 * @param dev A bhnd bus child device.
865 * @param port The port number being queried.
866 * @param type The port type being queried.
869 bhnd_get_region_count(device_t dev, bhnd_port_type type, u_int port) {
870 return (BHND_BUS_GET_REGION_COUNT(device_get_parent(dev), dev, type,
875 * Return the resource-ID for a memory region on the given device port.
877 * @param dev A bhnd bus child device.
878 * @param type The port type.
879 * @param port The port identifier.
880 * @param region The identifier of the memory region on @p port.
882 * @retval int The RID for the given @p port and @p region on @p device.
883 * @retval -1 No such port/region found.
886 bhnd_get_port_rid(device_t dev, bhnd_port_type type, u_int port, u_int region)
888 return BHND_BUS_GET_PORT_RID(device_get_parent(dev), dev, type, port,
893 * Decode a port / region pair on @p dev defined by @p rid.
895 * @param dev A bhnd bus child device.
896 * @param type The resource type.
897 * @param rid The resource identifier.
898 * @param[out] port_type The decoded port type.
899 * @param[out] port The decoded port identifier.
900 * @param[out] region The decoded region identifier.
903 * @retval non-zero No matching port/region found.
906 bhnd_decode_port_rid(device_t dev, int type, int rid, bhnd_port_type *port_type,
907 u_int *port, u_int *region)
909 return BHND_BUS_DECODE_PORT_RID(device_get_parent(dev), dev, type, rid,
910 port_type, port, region);
914 * Get the address and size of @p region on @p port.
916 * @param dev A bhnd bus child device.
917 * @param port_type The port type.
918 * @param port The port identifier.
919 * @param region The identifier of the memory region on @p port.
920 * @param[out] region_addr The region's base address.
921 * @param[out] region_size The region's size.
924 * @retval non-zero No matching port/region found.
927 bhnd_get_region_addr(device_t dev, bhnd_port_type port_type, u_int port,
928 u_int region, bhnd_addr_t *region_addr, bhnd_size_t *region_size)
930 return BHND_BUS_GET_REGION_ADDR(device_get_parent(dev), dev, port_type,
931 port, region, region_addr, region_size);
935 * bhnd bus-level equivalents of the bus_(read|write|set|barrier|...)
936 * macros (compatible with bhnd_resource).
938 * Generated with bhnd/tools/bus_macro.sh
940 #define bhnd_bus_barrier(r, o, l, f) \
942 bus_barrier((r)->res, (o), (l), (f)) : \
944 device_get_parent(rman_get_device((r)->res)), \
945 rman_get_device((r)->res), (r), (o), (l), (f))
946 #define bhnd_bus_read_1(r, o) \
948 bus_read_1((r)->res, (o)) : \
950 device_get_parent(rman_get_device((r)->res)), \
951 rman_get_device((r)->res), (r), (o))
952 #define bhnd_bus_read_multi_1(r, o, d, c) \
954 bus_read_multi_1((r)->res, (o), (d), (c)) : \
955 BHND_BUS_READ_MULTI_1( \
956 device_get_parent(rman_get_device((r)->res)), \
957 rman_get_device((r)->res), (r), (o), (d), (c))
958 #define bhnd_bus_read_region_1(r, o, d, c) \
960 bus_read_region_1((r)->res, (o), (d), (c)) : \
961 BHND_BUS_READ_REGION_1( \
962 device_get_parent(rman_get_device((r)->res)), \
963 rman_get_device((r)->res), (r), (o), (d), (c))
964 #define bhnd_bus_write_1(r, o, v) \
966 bus_write_1((r)->res, (o), (v)) : \
968 device_get_parent(rman_get_device((r)->res)), \
969 rman_get_device((r)->res), (r), (o), (v))
970 #define bhnd_bus_write_multi_1(r, o, d, c) \
972 bus_write_multi_1((r)->res, (o), (d), (c)) : \
973 BHND_BUS_WRITE_MULTI_1( \
974 device_get_parent(rman_get_device((r)->res)), \
975 rman_get_device((r)->res), (r), (o), (d), (c))
976 #define bhnd_bus_write_region_1(r, o, d, c) \
978 bus_write_region_1((r)->res, (o), (d), (c)) : \
979 BHND_BUS_WRITE_REGION_1( \
980 device_get_parent(rman_get_device((r)->res)), \
981 rman_get_device((r)->res), (r), (o), (d), (c))
982 #define bhnd_bus_read_stream_1(r, o) \
984 bus_read_stream_1((r)->res, (o)) : \
985 BHND_BUS_READ_STREAM_1( \
986 device_get_parent(rman_get_device((r)->res)), \
987 rman_get_device((r)->res), (r), (o))
988 #define bhnd_bus_read_multi_stream_1(r, o, d, c) \
990 bus_read_multi_stream_1((r)->res, (o), (d), (c)) : \
991 BHND_BUS_READ_MULTI_STREAM_1( \
992 device_get_parent(rman_get_device((r)->res)), \
993 rman_get_device((r)->res), (r), (o), (d), (c))
994 #define bhnd_bus_read_region_stream_1(r, o, d, c) \
996 bus_read_region_stream_1((r)->res, (o), (d), (c)) : \
997 BHND_BUS_READ_REGION_STREAM_1( \
998 device_get_parent(rman_get_device((r)->res)), \
999 rman_get_device((r)->res), (r), (o), (d), (c))
1000 #define bhnd_bus_write_stream_1(r, o, v) \
1002 bus_write_stream_1((r)->res, (o), (v)) : \
1003 BHND_BUS_WRITE_STREAM_1( \
1004 device_get_parent(rman_get_device((r)->res)), \
1005 rman_get_device((r)->res), (r), (o), (v))
1006 #define bhnd_bus_write_multi_stream_1(r, o, d, c) \
1008 bus_write_multi_stream_1((r)->res, (o), (d), (c)) : \
1009 BHND_BUS_WRITE_MULTI_STREAM_1( \
1010 device_get_parent(rman_get_device((r)->res)), \
1011 rman_get_device((r)->res), (r), (o), (d), (c))
1012 #define bhnd_bus_write_region_stream_1(r, o, d, c) \
1014 bus_write_region_stream_1((r)->res, (o), (d), (c)) : \
1015 BHND_BUS_WRITE_REGION_STREAM_1( \
1016 device_get_parent(rman_get_device((r)->res)), \
1017 rman_get_device((r)->res), (r), (o), (d), (c))
1018 #define bhnd_bus_set_multi_1(r, o, v, c) \
1020 bus_set_multi_1((r)->res, (o), (v), (c)) : \
1021 BHND_BUS_SET_MULTI_1( \
1022 device_get_parent(rman_get_device((r)->res)), \
1023 rman_get_device((r)->res), (r), (o), (v), (c))
1024 #define bhnd_bus_set_region_1(r, o, v, c) \
1026 bus_set_region_1((r)->res, (o), (v), (c)) : \
1027 BHND_BUS_SET_REGION_1( \
1028 device_get_parent(rman_get_device((r)->res)), \
1029 rman_get_device((r)->res), (r), (o), (v), (c))
1030 #define bhnd_bus_read_2(r, o) \
1032 bus_read_2((r)->res, (o)) : \
1034 device_get_parent(rman_get_device((r)->res)), \
1035 rman_get_device((r)->res), (r), (o))
1036 #define bhnd_bus_read_multi_2(r, o, d, c) \
1038 bus_read_multi_2((r)->res, (o), (d), (c)) : \
1039 BHND_BUS_READ_MULTI_2( \
1040 device_get_parent(rman_get_device((r)->res)), \
1041 rman_get_device((r)->res), (r), (o), (d), (c))
1042 #define bhnd_bus_read_region_2(r, o, d, c) \
1044 bus_read_region_2((r)->res, (o), (d), (c)) : \
1045 BHND_BUS_READ_REGION_2( \
1046 device_get_parent(rman_get_device((r)->res)), \
1047 rman_get_device((r)->res), (r), (o), (d), (c))
1048 #define bhnd_bus_write_2(r, o, v) \
1050 bus_write_2((r)->res, (o), (v)) : \
1052 device_get_parent(rman_get_device((r)->res)), \
1053 rman_get_device((r)->res), (r), (o), (v))
1054 #define bhnd_bus_write_multi_2(r, o, d, c) \
1056 bus_write_multi_2((r)->res, (o), (d), (c)) : \
1057 BHND_BUS_WRITE_MULTI_2( \
1058 device_get_parent(rman_get_device((r)->res)), \
1059 rman_get_device((r)->res), (r), (o), (d), (c))
1060 #define bhnd_bus_write_region_2(r, o, d, c) \
1062 bus_write_region_2((r)->res, (o), (d), (c)) : \
1063 BHND_BUS_WRITE_REGION_2( \
1064 device_get_parent(rman_get_device((r)->res)), \
1065 rman_get_device((r)->res), (r), (o), (d), (c))
1066 #define bhnd_bus_read_stream_2(r, o) \
1068 bus_read_stream_2((r)->res, (o)) : \
1069 BHND_BUS_READ_STREAM_2( \
1070 device_get_parent(rman_get_device((r)->res)), \
1071 rman_get_device((r)->res), (r), (o))
1072 #define bhnd_bus_read_multi_stream_2(r, o, d, c) \
1074 bus_read_multi_stream_2((r)->res, (o), (d), (c)) : \
1075 BHND_BUS_READ_MULTI_STREAM_2( \
1076 device_get_parent(rman_get_device((r)->res)), \
1077 rman_get_device((r)->res), (r), (o), (d), (c))
1078 #define bhnd_bus_read_region_stream_2(r, o, d, c) \
1080 bus_read_region_stream_2((r)->res, (o), (d), (c)) : \
1081 BHND_BUS_READ_REGION_STREAM_2( \
1082 device_get_parent(rman_get_device((r)->res)), \
1083 rman_get_device((r)->res), (r), (o), (d), (c))
1084 #define bhnd_bus_write_stream_2(r, o, v) \
1086 bus_write_stream_2((r)->res, (o), (v)) : \
1087 BHND_BUS_WRITE_STREAM_2( \
1088 device_get_parent(rman_get_device((r)->res)), \
1089 rman_get_device((r)->res), (r), (o), (v))
1090 #define bhnd_bus_write_multi_stream_2(r, o, d, c) \
1092 bus_write_multi_stream_2((r)->res, (o), (d), (c)) : \
1093 BHND_BUS_WRITE_MULTI_STREAM_2( \
1094 device_get_parent(rman_get_device((r)->res)), \
1095 rman_get_device((r)->res), (r), (o), (d), (c))
1096 #define bhnd_bus_write_region_stream_2(r, o, d, c) \
1098 bus_write_region_stream_2((r)->res, (o), (d), (c)) : \
1099 BHND_BUS_WRITE_REGION_STREAM_2( \
1100 device_get_parent(rman_get_device((r)->res)), \
1101 rman_get_device((r)->res), (r), (o), (d), (c))
1102 #define bhnd_bus_set_multi_2(r, o, v, c) \
1104 bus_set_multi_2((r)->res, (o), (v), (c)) : \
1105 BHND_BUS_SET_MULTI_2( \
1106 device_get_parent(rman_get_device((r)->res)), \
1107 rman_get_device((r)->res), (r), (o), (v), (c))
1108 #define bhnd_bus_set_region_2(r, o, v, c) \
1110 bus_set_region_2((r)->res, (o), (v), (c)) : \
1111 BHND_BUS_SET_REGION_2( \
1112 device_get_parent(rman_get_device((r)->res)), \
1113 rman_get_device((r)->res), (r), (o), (v), (c))
1114 #define bhnd_bus_read_4(r, o) \
1116 bus_read_4((r)->res, (o)) : \
1118 device_get_parent(rman_get_device((r)->res)), \
1119 rman_get_device((r)->res), (r), (o))
1120 #define bhnd_bus_read_multi_4(r, o, d, c) \
1122 bus_read_multi_4((r)->res, (o), (d), (c)) : \
1123 BHND_BUS_READ_MULTI_4( \
1124 device_get_parent(rman_get_device((r)->res)), \
1125 rman_get_device((r)->res), (r), (o), (d), (c))
1126 #define bhnd_bus_read_region_4(r, o, d, c) \
1128 bus_read_region_4((r)->res, (o), (d), (c)) : \
1129 BHND_BUS_READ_REGION_4( \
1130 device_get_parent(rman_get_device((r)->res)), \
1131 rman_get_device((r)->res), (r), (o), (d), (c))
1132 #define bhnd_bus_write_4(r, o, v) \
1134 bus_write_4((r)->res, (o), (v)) : \
1136 device_get_parent(rman_get_device((r)->res)), \
1137 rman_get_device((r)->res), (r), (o), (v))
1138 #define bhnd_bus_write_multi_4(r, o, d, c) \
1140 bus_write_multi_4((r)->res, (o), (d), (c)) : \
1141 BHND_BUS_WRITE_MULTI_4( \
1142 device_get_parent(rman_get_device((r)->res)), \
1143 rman_get_device((r)->res), (r), (o), (d), (c))
1144 #define bhnd_bus_write_region_4(r, o, d, c) \
1146 bus_write_region_4((r)->res, (o), (d), (c)) : \
1147 BHND_BUS_WRITE_REGION_4( \
1148 device_get_parent(rman_get_device((r)->res)), \
1149 rman_get_device((r)->res), (r), (o), (d), (c))
1150 #define bhnd_bus_read_stream_4(r, o) \
1152 bus_read_stream_4((r)->res, (o)) : \
1153 BHND_BUS_READ_STREAM_4( \
1154 device_get_parent(rman_get_device((r)->res)), \
1155 rman_get_device((r)->res), (r), (o))
1156 #define bhnd_bus_read_multi_stream_4(r, o, d, c) \
1158 bus_read_multi_stream_4((r)->res, (o), (d), (c)) : \
1159 BHND_BUS_READ_MULTI_STREAM_4( \
1160 device_get_parent(rman_get_device((r)->res)), \
1161 rman_get_device((r)->res), (r), (o), (d), (c))
1162 #define bhnd_bus_read_region_stream_4(r, o, d, c) \
1164 bus_read_region_stream_4((r)->res, (o), (d), (c)) : \
1165 BHND_BUS_READ_REGION_STREAM_4( \
1166 device_get_parent(rman_get_device((r)->res)), \
1167 rman_get_device((r)->res), (r), (o), (d), (c))
1168 #define bhnd_bus_write_stream_4(r, o, v) \
1170 bus_write_stream_4((r)->res, (o), (v)) : \
1171 BHND_BUS_WRITE_STREAM_4( \
1172 device_get_parent(rman_get_device((r)->res)), \
1173 rman_get_device((r)->res), (r), (o), (v))
1174 #define bhnd_bus_write_multi_stream_4(r, o, d, c) \
1176 bus_write_multi_stream_4((r)->res, (o), (d), (c)) : \
1177 BHND_BUS_WRITE_MULTI_STREAM_4( \
1178 device_get_parent(rman_get_device((r)->res)), \
1179 rman_get_device((r)->res), (r), (o), (d), (c))
1180 #define bhnd_bus_write_region_stream_4(r, o, d, c) \
1182 bus_write_region_stream_4((r)->res, (o), (d), (c)) : \
1183 BHND_BUS_WRITE_REGION_STREAM_4( \
1184 device_get_parent(rman_get_device((r)->res)), \
1185 rman_get_device((r)->res), (r), (o), (d), (c))
1186 #define bhnd_bus_set_multi_4(r, o, v, c) \
1188 bus_set_multi_4((r)->res, (o), (v), (c)) : \
1189 BHND_BUS_SET_MULTI_4( \
1190 device_get_parent(rman_get_device((r)->res)), \
1191 rman_get_device((r)->res), (r), (o), (v), (c))
1192 #define bhnd_bus_set_region_4(r, o, v, c) \
1194 bus_set_region_4((r)->res, (o), (v), (c)) : \
1195 BHND_BUS_SET_REGION_4( \
1196 device_get_parent(rman_get_device((r)->res)), \
1197 rman_get_device((r)->res), (r), (o), (v), (c))
1199 #endif /* _BHND_BHND_H_ */