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_bus_if.h"
44 extern devclass_t bhnd_devclass;
45 extern devclass_t bhnd_hostb_devclass;
46 extern devclass_t bhnd_nvram_devclass;
49 * bhnd child instance variables
51 enum bhnd_device_vars {
52 BHND_IVAR_VENDOR, /**< Designer's JEP-106 manufacturer ID. */
53 BHND_IVAR_DEVICE, /**< Part number */
54 BHND_IVAR_HWREV, /**< Core revision */
55 BHND_IVAR_DEVICE_CLASS, /**< Core class (@sa bhnd_devclass_t) */
56 BHND_IVAR_VENDOR_NAME, /**< Core vendor name */
57 BHND_IVAR_DEVICE_NAME, /**< Core name */
58 BHND_IVAR_CORE_INDEX, /**< Bus-assigned core number */
59 BHND_IVAR_CORE_UNIT, /**< Bus-assigned core unit number,
60 assigned sequentially (starting at 0) for
61 each vendor/device pair. */
65 * bhnd device probe priority bands.
68 BHND_PROBE_ROOT = 0, /**< Nexus or host bridge */
69 BHND_PROBE_BUS = 1000, /**< Busses and bridges */
70 BHND_PROBE_CPU = 2000, /**< CPU devices */
71 BHND_PROBE_INTERRUPT = 3000, /**< Interrupt controllers. */
72 BHND_PROBE_TIMER = 4000, /**< Timers and clocks. */
73 BHND_PROBE_RESOURCE = 5000, /**< Resource discovery (including NVRAM/SPROM) */
74 BHND_PROBE_DEFAULT = 6000, /**< Default device priority */
78 * Constants defining fine grained ordering within a BHND_PROBE_* priority band.
82 * BHND_PROBE_BUS + BHND_PROBE_ORDER_FIRST
86 BHND_PROBE_ORDER_FIRST = 0,
87 BHND_PROBE_ORDER_EARLY = 25,
88 BHND_PROBE_ORDER_MIDDLE = 50,
89 BHND_PROBE_ORDER_LATE = 75,
90 BHND_PROBE_ORDER_LAST = 100
95 * Simplified accessors for bhnd device ivars
97 #define BHND_ACCESSOR(var, ivar, type) \
98 __BUS_ACCESSOR(bhnd, var, BHND, ivar, type)
100 BHND_ACCESSOR(vendor, VENDOR, uint16_t);
101 BHND_ACCESSOR(device, DEVICE, uint16_t);
102 BHND_ACCESSOR(hwrev, HWREV, uint8_t);
103 BHND_ACCESSOR(class, DEVICE_CLASS, bhnd_devclass_t);
104 BHND_ACCESSOR(vendor_name, VENDOR_NAME, const char *);
105 BHND_ACCESSOR(device_name, DEVICE_NAME, const char *);
106 BHND_ACCESSOR(core_index, CORE_INDEX, u_int);
107 BHND_ACCESSOR(core_unit, CORE_UNIT, int);
112 * Chip Identification
114 * This is read from the ChipCommon ID register; on earlier bhnd(4) devices
115 * where ChipCommon is unavailable, known values must be supplied.
118 uint16_t chip_id; /**< chip id (BHND_CHIPID_*) */
119 uint8_t chip_rev; /**< chip revision */
120 uint8_t chip_pkg; /**< chip package (BHND_PKGID_*) */
121 uint8_t chip_type; /**< chip type (BHND_CHIPTYPE_*) */
123 bhnd_addr_t enum_addr; /**< chip_type-specific enumeration
124 * address; either the siba(4) base
125 * core register block, or the bcma(4)
126 * EROM core address. */
128 uint8_t ncores; /**< number of cores, if known. 0 if
133 * A bhnd(4) bus resource.
135 * This provides an abstract interface to per-core resources that may require
136 * bus-level remapping of address windows prior to access.
138 struct bhnd_resource {
139 struct resource *res; /**< the system resource. */
140 bool direct; /**< false if the resource requires
141 * bus window remapping before it
142 * is MMIO accessible. */
146 * A bhnd(4) core descriptor.
148 struct bhnd_core_info {
149 uint16_t vendor; /**< vendor */
150 uint16_t device; /**< device */
151 uint16_t hwrev; /**< hardware revision */
152 u_int core_idx; /**< bus-assigned core index */
153 int unit; /**< bus-assigned core unit */
158 * A hardware revision match descriptor.
160 struct bhnd_hwrev_match {
161 uint16_t start; /**< first revision, or BHND_HWREV_INVALID
162 to match on any revision. */
163 uint16_t end; /**< last revision, or BHND_HWREV_INVALID
164 to match on any revision. */
168 * Wildcard hardware revision match descriptor.
170 #define BHND_HWREV_ANY { BHND_HWREV_INVALID, BHND_HWREV_INVALID }
171 #define BHND_HWREV_IS_ANY(_m) \
172 ((_m)->start == BHND_HWREV_INVALID && (_m)->end == BHND_HWREV_INVALID)
175 * Hardware revision match descriptor for an inclusive range.
177 * @param _start The first applicable hardware revision.
178 * @param _end The last applicable hardware revision, or BHND_HWREV_INVALID
179 * to match on any revision.
181 #define BHND_HWREV_RANGE(_start, _end) { _start, _end }
184 * Hardware revision match descriptor for a single revision.
186 * @param _hwrev The hardware revision to match on.
188 #define BHND_HWREV_EQ(_hwrev) BHND_HWREV_RANGE(_hwrev, _hwrev)
191 * Hardware revision match descriptor for any revision equal to or greater
194 * @param _start The first hardware revision to match on.
196 #define BHND_HWREV_GTE(_start) BHND_HWREV_RANGE(_start, BHND_HWREV_INVALID)
199 * Hardware revision match descriptor for any revision equal to or less
202 * @param _end The last hardware revision to match on.
204 #define BHND_HWREV_LTE(_end) BHND_HWREV_RANGE(0, _end)
207 /** A core match descriptor. */
208 struct bhnd_core_match {
209 uint16_t vendor; /**< required JEP106 device vendor or BHND_MFGID_INVALID. */
210 uint16_t device; /**< required core ID or BHND_COREID_INVALID */
211 struct bhnd_hwrev_match hwrev; /**< matching revisions. */
212 bhnd_devclass_t class; /**< required class or BHND_DEVCLASS_INVALID */
213 int unit; /**< required core unit, or -1 */
217 * Core match descriptor matching against the given @p _vendor, @p _device,
218 * and @p _hwrev match descriptors.
220 #define BHND_CORE_MATCH(_vendor, _device, _hwrev) \
221 { _vendor, _device, _hwrev, BHND_DEVCLASS_INVALID, -1 }
224 * Wildcard core match descriptor.
226 #define BHND_CORE_MATCH_ANY \
228 .vendor = BHND_MFGID_INVALID, \
229 .device = BHND_COREID_INVALID, \
230 .hwrev = BHND_HWREV_ANY, \
231 .class = BHND_DEVCLASS_INVALID, \
236 * Device quirk table descriptor.
238 struct bhnd_device_quirk {
239 struct bhnd_hwrev_match hwrev; /**< applicable hardware revisions */
240 uint32_t quirks; /**< quirk flags */
242 #define BHND_DEVICE_QUIRK_END { BHND_HWREV_ANY, 0 }
243 #define BHND_DEVICE_QUIRK_IS_END(_q) \
244 (BHND_HWREV_IS_ANY(&(_q)->hwrev) && (_q)->quirks == 0)
248 BHND_DF_HOSTB = (1<<0) /**< core is serving as the bus'
252 /** Device probe table descriptor */
254 const struct bhnd_core_match core; /**< core match descriptor */
255 const char *desc; /**< device description, or NULL. */
256 const struct bhnd_device_quirk *quirks_table; /**< quirks table for this device, or NULL */
257 uint32_t device_flags; /**< required BHND_DF_* flags */
260 #define _BHND_DEVICE(_device, _desc, _quirks, _flags, ...) \
261 { BHND_CORE_MATCH(BHND_MFGID_BCM, BHND_COREID_ ## _device, \
262 BHND_HWREV_ANY), _desc, _quirks, _flags }
264 #define BHND_DEVICE(_device, _desc, _quirks, ...) \
265 _BHND_DEVICE(_device, _desc, _quirks, ## __VA_ARGS__, 0)
267 #define BHND_DEVICE_END { BHND_CORE_MATCH_ANY, NULL, NULL, 0 }
269 const char *bhnd_vendor_name(uint16_t vendor);
270 const char *bhnd_port_type_name(bhnd_port_type port_type);
272 const char *bhnd_find_core_name(uint16_t vendor,
274 bhnd_devclass_t bhnd_find_core_class(uint16_t vendor,
277 const char *bhnd_core_name(const struct bhnd_core_info *ci);
278 bhnd_devclass_t bhnd_core_class(const struct bhnd_core_info *ci);
281 device_t bhnd_match_child(device_t dev,
282 const struct bhnd_core_match *desc);
284 device_t bhnd_find_child(device_t dev,
285 bhnd_devclass_t class, int unit);
287 const struct bhnd_core_info *bhnd_match_core(
288 const struct bhnd_core_info *cores,
290 const struct bhnd_core_match *desc);
292 const struct bhnd_core_info *bhnd_find_core(
293 const struct bhnd_core_info *cores,
294 u_int num_cores, bhnd_devclass_t class);
296 bool bhnd_core_matches(
297 const struct bhnd_core_info *core,
298 const struct bhnd_core_match *desc);
300 bool bhnd_hwrev_matches(uint16_t hwrev,
301 const struct bhnd_hwrev_match *desc);
303 bool bhnd_device_matches(device_t dev,
304 const struct bhnd_core_match *desc);
306 const struct bhnd_device *bhnd_device_lookup(device_t dev,
307 const struct bhnd_device *table,
310 uint32_t bhnd_device_quirks(device_t dev,
311 const struct bhnd_device *table,
314 struct bhnd_core_info bhnd_get_core_info(device_t dev);
317 int bhnd_alloc_resources(device_t dev,
318 struct resource_spec *rs,
319 struct bhnd_resource **res);
321 void bhnd_release_resources(device_t dev,
322 const struct resource_spec *rs,
323 struct bhnd_resource **res);
325 struct bhnd_chipid bhnd_parse_chipid(uint32_t idreg,
326 bhnd_addr_t enum_addr);
328 int bhnd_read_chipid(device_t dev,
329 struct resource_spec *rs,
330 bus_size_t chipc_offset,
331 struct bhnd_chipid *result);
333 void bhnd_set_custom_core_desc(device_t dev,
335 void bhnd_set_default_core_desc(device_t dev);
338 bool bhnd_bus_generic_is_hostb_device(device_t dev,
340 bool bhnd_bus_generic_is_hw_disabled(device_t dev,
342 bool bhnd_bus_generic_is_region_valid(device_t dev,
343 device_t child, bhnd_port_type type,
344 u_int port, u_int region);
345 int bhnd_bus_generic_read_nvram_var(device_t dev,
346 device_t child, const char *name,
347 void *buf, size_t *size);
348 const struct bhnd_chipid *bhnd_bus_generic_get_chipid(device_t dev,
350 struct bhnd_resource *bhnd_bus_generic_alloc_resource (device_t dev,
351 device_t child, int type, int *rid,
352 rman_res_t start, rman_res_t end,
353 rman_res_t count, u_int flags);
354 int bhnd_bus_generic_release_resource (device_t dev,
355 device_t child, int type, int rid,
356 struct bhnd_resource *r);
357 int bhnd_bus_generic_activate_resource (device_t dev,
358 device_t child, int type, int rid,
359 struct bhnd_resource *r);
360 int bhnd_bus_generic_deactivate_resource (device_t dev,
361 device_t child, int type, int rid,
362 struct bhnd_resource *r);
367 * Return true if @p dev is serving as a host bridge for its parent bhnd
370 * @param dev A bhnd bus child device.
373 bhnd_is_hostb_device(device_t dev) {
374 return (BHND_BUS_IS_HOSTB_DEVICE(device_get_parent(dev), dev));
378 * Return true if the hardware components required by @p dev are known to be
379 * unpopulated or otherwise unusable.
381 * In some cases, enumerated devices may have pins that are left floating, or
382 * the hardware may otherwise be non-functional; this method allows a parent
383 * device to explicitly specify if a successfully enumerated @p dev should
386 * @param dev A bhnd bus child device.
389 bhnd_is_hw_disabled(device_t dev) {
390 return (BHND_BUS_IS_HW_DISABLED(device_get_parent(dev), dev));
394 * Allocate a resource from a device's parent bhnd(4) bus.
396 * @param dev The device requesting resource ownership.
397 * @param type The type of resource to allocate. This may be any type supported
398 * by the standard bus APIs.
399 * @param rid The bus-specific handle identifying the resource being allocated.
400 * @param start The start address of the resource.
401 * @param end The end address of the resource.
402 * @param count The size of the resource.
403 * @param flags The flags for the resource to be allocated. These may be any
404 * values supported by the standard bus APIs.
406 * To request the resource's default addresses, pass @p start and
407 * @p end values of @c 0 and @c ~0, respectively, and
408 * a @p count of @c 1.
410 * @retval NULL The resource could not be allocated.
411 * @retval resource The allocated resource.
413 static inline struct bhnd_resource *
414 bhnd_alloc_resource(device_t dev, int type, int *rid, rman_res_t start,
415 rman_res_t end, rman_res_t count, u_int flags)
417 return BHND_BUS_ALLOC_RESOURCE(device_get_parent(dev), dev, type, rid,
418 start, end, count, flags);
423 * Allocate a resource from a device's parent bhnd(4) bus, using the
424 * resource's default start, end, and count values.
426 * @param dev The device requesting resource ownership.
427 * @param type The type of resource to allocate. This may be any type supported
428 * by the standard bus APIs.
429 * @param rid The bus-specific handle identifying the resource being allocated.
430 * @param flags The flags for the resource to be allocated. These may be any
431 * values supported by the standard bus APIs.
433 * @retval NULL The resource could not be allocated.
434 * @retval resource The allocated resource.
436 static inline struct bhnd_resource *
437 bhnd_alloc_resource_any(device_t dev, int type, int *rid, u_int flags)
439 return bhnd_alloc_resource(dev, type, rid, 0, ~0, 1, flags);
443 * Activate a previously allocated bhnd resource.
445 * @param dev The device holding ownership of the allocated resource.
446 * @param type The type of the resource.
447 * @param rid The bus-specific handle identifying the resource.
448 * @param r A pointer to the resource returned by bhnd_alloc_resource or
449 * BHND_BUS_ALLOC_RESOURCE.
452 * @retval non-zero an error occurred while activating the resource.
455 bhnd_activate_resource(device_t dev, int type, int rid,
456 struct bhnd_resource *r)
458 return BHND_BUS_ACTIVATE_RESOURCE(device_get_parent(dev), dev, type,
463 * Deactivate a previously activated bhnd resource.
465 * @param dev The device holding ownership of the activated resource.
466 * @param type The type of the resource.
467 * @param rid The bus-specific handle identifying the resource.
468 * @param r A pointer to the resource returned by bhnd_alloc_resource or
469 * BHND_BUS_ALLOC_RESOURCE.
472 * @retval non-zero an error occurred while activating the resource.
475 bhnd_deactivate_resource(device_t dev, int type, int rid,
476 struct bhnd_resource *r)
478 return BHND_BUS_DEACTIVATE_RESOURCE(device_get_parent(dev), dev, type,
483 * Free a resource allocated by bhnd_alloc_resource().
485 * @param dev The device holding ownership of the resource.
486 * @param type The type of the resource.
487 * @param rid The bus-specific handle identifying the resource.
488 * @param r A pointer to the resource returned by bhnd_alloc_resource or
489 * BHND_ALLOC_RESOURCE.
492 * @retval non-zero an error occurred while activating the resource.
495 bhnd_release_resource(device_t dev, int type, int rid,
496 struct bhnd_resource *r)
498 return BHND_BUS_RELEASE_RESOURCE(device_get_parent(dev), dev, type,
503 * Return true if @p region_num is a valid region on @p port_num of
504 * @p type attached to @p dev.
506 * @param dev A bhnd bus child device.
507 * @param type The port type being queried.
508 * @param port_num The port number being queried.
509 * @param region_num The region number being queried.
512 bhnd_is_region_valid(device_t dev, bhnd_port_type type, u_int port_num,
515 return (BHND_BUS_IS_REGION_VALID(device_get_parent(dev), dev, type,
516 port_num, region_num));
520 * Return the number of ports of type @p type attached to @p def.
522 * @param dev A bhnd bus child device.
523 * @param type The port type being queried.
526 bhnd_get_port_count(device_t dev, bhnd_port_type type) {
527 return (BHND_BUS_GET_PORT_COUNT(device_get_parent(dev), dev, type));
531 * Return the number of memory regions mapped to @p child @p port of
534 * @param dev A bhnd bus child device.
535 * @param port The port number being queried.
536 * @param type The port type being queried.
539 bhnd_get_region_count(device_t dev, bhnd_port_type type, u_int port) {
540 return (BHND_BUS_GET_REGION_COUNT(device_get_parent(dev), dev, type,
545 * Return the resource-ID for a memory region on the given device port.
547 * @param dev A bhnd bus child device.
548 * @param type The port type.
549 * @param port The port identifier.
550 * @param region The identifier of the memory region on @p port.
552 * @retval int The RID for the given @p port and @p region on @p device.
553 * @retval -1 No such port/region found.
556 bhnd_get_port_rid(device_t dev, bhnd_port_type type, u_int port, u_int region)
558 return BHND_BUS_GET_PORT_RID(device_get_parent(dev), dev, type, port,
563 * Decode a port / region pair on @p dev defined by @p rid.
565 * @param dev A bhnd bus child device.
566 * @param type The resource type.
567 * @param rid The resource identifier.
568 * @param[out] port_type The decoded port type.
569 * @param[out] port The decoded port identifier.
570 * @param[out] region The decoded region identifier.
573 * @retval non-zero No matching port/region found.
576 bhnd_decode_port_rid(device_t dev, int type, int rid, bhnd_port_type *port_type,
577 u_int *port, u_int *region)
579 return BHND_BUS_DECODE_PORT_RID(device_get_parent(dev), dev, type, rid,
580 port_type, port, region);
584 * Get the address and size of @p region on @p port.
586 * @param dev A bhnd bus child device.
587 * @param port_type The port type.
588 * @param port The port identifier.
589 * @param region The identifier of the memory region on @p port.
590 * @param[out] region_addr The region's base address.
591 * @param[out] region_size The region's size.
594 * @retval non-zero No matching port/region found.
597 bhnd_get_region_addr(device_t dev, bhnd_port_type port_type, u_int port,
598 u_int region, bhnd_addr_t *region_addr, bhnd_size_t *region_size)
600 return BHND_BUS_GET_REGION_ADDR(device_get_parent(dev), dev, port_type,
601 port, region, region_addr, region_size);
605 * bhnd bus-level equivalents of the bus_(read|write|set|barrier|...)
606 * macros (compatible with bhnd_resource).
608 * Generated with bhnd/tools/bus_macro.sh
610 #define bhnd_bus_barrier(r, o, l, f) \
612 bus_barrier((r)->res, (o), (l), (f)) : \
613 BHND_BUS_BARRIER(device_get_parent(rman_get_device((r)->res)), \
614 rman_get_device((r)->res), (r), (o), (l), (f))
615 #define bhnd_bus_read_1(r, o) \
617 bus_read_1((r)->res, (o)) : \
618 BHND_BUS_READ_1(device_get_parent(rman_get_device((r)->res)), \
619 rman_get_device((r)->res), (r), (o))
620 #define bhnd_bus_write_1(r, o, v) \
622 bus_write_1((r)->res, (o), (v)) : \
623 BHND_BUS_WRITE_1(device_get_parent(rman_get_device((r)->res)), \
624 rman_get_device((r)->res), (r), (o), (v))
625 #define bhnd_bus_read_2(r, o) \
627 bus_read_2((r)->res, (o)) : \
628 BHND_BUS_READ_2(device_get_parent(rman_get_device((r)->res)), \
629 rman_get_device((r)->res), (r), (o))
630 #define bhnd_bus_write_2(r, o, v) \
632 bus_write_2((r)->res, (o), (v)) : \
633 BHND_BUS_WRITE_2(device_get_parent(rman_get_device((r)->res)), \
634 rman_get_device((r)->res), (r), (o), (v))
635 #define bhnd_bus_read_4(r, o) \
637 bus_read_4((r)->res, (o)) : \
638 BHND_BUS_READ_4(device_get_parent(rman_get_device((r)->res)), \
639 rman_get_device((r)->res), (r), (o))
640 #define bhnd_bus_write_4(r, o, v) \
642 bus_write_4((r)->res, (o), (v)) : \
643 BHND_BUS_WRITE_4(device_get_parent(rman_get_device((r)->res)), \
644 rman_get_device((r)->res), (r), (o), (v))
646 #endif /* _BHND_BHND_H_ */