2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2015-2017 Landon Fuller <landonf@landonf.org>
5 * Copyright (c) 2017 The FreeBSD Foundation
8 * Portions of this software were developed by Landon Fuller
9 * under sponsorship from the FreeBSD Foundation.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer,
16 * without modification.
17 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18 * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
19 * redistribution must be conditioned upon including a substantially
20 * similar Disclaimer requirement for further binary redistribution.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
26 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
27 * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
28 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
31 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
33 * THE POSSIBILITY OF SUCH DAMAGES.
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
39 #include <sys/param.h>
41 #include <sys/kernel.h>
42 #include <sys/limits.h>
43 #include <sys/systm.h>
45 #include <machine/bus.h>
46 #include <machine/resource.h>
48 #include <dev/bhnd/bhnd_eromvar.h>
50 #include "bcma_eromreg.h"
51 #include "bcma_eromvar.h"
54 * BCMA Enumeration ROM (EROM) Table
56 * Provides auto-discovery of BCMA cores on Broadcom's HND SoC.
58 * The EROM core address can be found at BCMA_CC_EROM_ADDR within the
59 * ChipCommon registers. The table itself is comprised of 32-bit
60 * type-tagged entries, organized into an array of variable-length
61 * core descriptor records.
63 * The final core descriptor is followed by a 32-bit BCMA_EROM_TABLE_EOF (0xF)
67 static const char *bcma_erom_entry_type_name (uint8_t entry);
69 static int bcma_erom_read32(struct bcma_erom *erom,
71 static int bcma_erom_skip32(struct bcma_erom *erom);
73 static int bcma_erom_skip_core(struct bcma_erom *erom);
74 static int bcma_erom_skip_mport(struct bcma_erom *erom);
75 static int bcma_erom_skip_sport_region(struct bcma_erom *erom);
77 static int bcma_erom_seek_next(struct bcma_erom *erom,
79 static int bcma_erom_region_to_port_type(struct bcma_erom *erom,
80 uint8_t region_type, bhnd_port_type *port_type);
83 static int bcma_erom_peek32(struct bcma_erom *erom,
86 static bus_size_t bcma_erom_tell(struct bcma_erom *erom);
87 static void bcma_erom_seek(struct bcma_erom *erom,
89 static void bcma_erom_reset(struct bcma_erom *erom);
91 static int bcma_erom_seek_matching_core(struct bcma_erom *sc,
92 const struct bhnd_core_match *desc,
93 struct bhnd_core_info *core);
95 static int bcma_erom_parse_core(struct bcma_erom *erom,
96 struct bcma_erom_core *core);
98 static int bcma_erom_parse_mport(struct bcma_erom *erom,
99 struct bcma_erom_mport *mport);
101 static int bcma_erom_parse_sport_region(struct bcma_erom *erom,
102 struct bcma_erom_sport_region *region);
104 static void bcma_erom_to_core_info(const struct bcma_erom_core *core,
105 u_int core_idx, int core_unit,
106 struct bhnd_core_info *info);
109 * BCMA EROM per-instance state.
112 struct bhnd_erom obj;
113 device_t dev; /**< parent device, or NULL if none. */
114 struct bhnd_erom_io *eio; /**< bus I/O callbacks */
115 bhnd_size_t offset; /**< current read offset */
118 #define EROM_LOG(erom, fmt, ...) do { \
119 printf("%s erom[0x%llx]: " fmt, __FUNCTION__, \
120 (unsigned long long)(erom->offset), ##__VA_ARGS__); \
123 /** Return the type name for an EROM entry */
125 bcma_erom_entry_type_name (uint8_t entry)
127 switch (BCMA_EROM_GET_ATTR(entry, ENTRY_TYPE)) {
128 case BCMA_EROM_ENTRY_TYPE_CORE:
130 case BCMA_EROM_ENTRY_TYPE_MPORT:
132 case BCMA_EROM_ENTRY_TYPE_REGION:
139 /* BCMA implementation of BHND_EROM_INIT() */
141 bcma_erom_init(bhnd_erom_t *erom, const struct bhnd_chipid *cid,
142 struct bhnd_erom_io *eio)
144 struct bcma_erom *sc;
145 bhnd_addr_t table_addr;
148 sc = (struct bcma_erom *)erom;
152 /* Determine erom table address */
153 if (BHND_ADDR_MAX - BCMA_EROM_TABLE_START < cid->enum_addr)
154 return (ENXIO); /* would overflow */
156 table_addr = cid->enum_addr + BCMA_EROM_TABLE_START;
158 /* Try to map the erom table */
159 error = bhnd_erom_io_map(sc->eio, table_addr, BCMA_EROM_TABLE_SIZE);
166 /* BCMA implementation of BHND_EROM_PROBE() */
168 bcma_erom_probe(bhnd_erom_class_t *cls, struct bhnd_erom_io *eio,
169 const struct bhnd_chipid *hint, struct bhnd_chipid *cid)
173 /* Hints aren't supported; all BCMA devices have a ChipCommon
178 /* Read and parse chip identification */
179 if ((error = bhnd_erom_read_chipid(eio, cid)))
182 /* Verify chip type */
183 switch (cid->chip_type) {
184 case BHND_CHIPTYPE_BCMA:
185 return (BUS_PROBE_DEFAULT);
187 case BHND_CHIPTYPE_BCMA_ALT:
188 case BHND_CHIPTYPE_UBUS:
189 return (BUS_PROBE_GENERIC);
197 bcma_erom_fini(bhnd_erom_t *erom)
199 struct bcma_erom *sc = (struct bcma_erom *)erom;
201 bhnd_erom_io_fini(sc->eio);
205 bcma_erom_lookup_core(bhnd_erom_t *erom, const struct bhnd_core_match *desc,
206 struct bhnd_core_info *core)
208 struct bcma_erom *sc = (struct bcma_erom *)erom;
210 /* Search for the first matching core */
211 return (bcma_erom_seek_matching_core(sc, desc, core));
215 bcma_erom_lookup_core_addr(bhnd_erom_t *erom, const struct bhnd_core_match *desc,
216 bhnd_port_type port_type, u_int port_num, u_int region_num,
217 struct bhnd_core_info *core, bhnd_addr_t *addr, bhnd_size_t *size)
219 struct bcma_erom *sc;
220 struct bcma_erom_core ec;
222 uint8_t region_port, region_type;
226 sc = (struct bcma_erom *)erom;
228 /* Seek to the first matching core and provide the core info
230 if ((error = bcma_erom_seek_matching_core(sc, desc, core)))
233 if ((error = bcma_erom_parse_core(sc, &ec)))
236 /* Skip master ports */
237 for (u_long i = 0; i < ec.num_mport; i++) {
238 if ((error = bcma_erom_skip_mport(sc)))
242 /* Seek to the region block for the given port type */
245 bhnd_port_type p_type;
248 if ((error = bcma_erom_peek32(sc, &entry)))
251 if (!BCMA_EROM_ENTRY_IS(entry, REGION))
254 /* Expected region type? */
255 r_type = BCMA_EROM_GET_ATTR(entry, REGION_TYPE);
256 error = bcma_erom_region_to_port_type(sc, r_type, &p_type);
260 if (p_type == port_type) {
265 /* Skip to next entry */
266 if ((error = bcma_erom_skip_sport_region(sc)))
273 /* Found the appropriate port type block; now find the region records
274 * for the given port number */
276 for (u_int i = 0; i <= port_num; i++) {
277 bhnd_port_type p_type;
279 if ((error = bcma_erom_peek32(sc, &entry)))
282 if (!BCMA_EROM_ENTRY_IS(entry, REGION))
285 /* Fetch the type/port of the first region entry */
286 region_type = BCMA_EROM_GET_ATTR(entry, REGION_TYPE);
287 region_port = BCMA_EROM_GET_ATTR(entry, REGION_PORT);
289 /* Have we found the region entries for the desired port? */
291 error = bcma_erom_region_to_port_type(sc, region_type,
296 if (p_type == port_type)
302 /* Otherwise, seek to next block of region records */
304 uint8_t next_type, next_port;
306 if ((error = bcma_erom_skip_sport_region(sc)))
309 if ((error = bcma_erom_peek32(sc, &entry)))
312 if (!BCMA_EROM_ENTRY_IS(entry, REGION))
315 next_type = BCMA_EROM_GET_ATTR(entry, REGION_TYPE);
316 next_port = BCMA_EROM_GET_ATTR(entry, REGION_PORT);
318 if (next_type != region_type ||
319 next_port != region_port)
327 /* Finally, search for the requested region number */
328 for (u_int i = 0; i <= region_num; i++) {
329 struct bcma_erom_sport_region region;
330 uint8_t next_port, next_type;
332 if ((error = bcma_erom_peek32(sc, &entry)))
335 if (!BCMA_EROM_ENTRY_IS(entry, REGION))
338 /* Check for the end of the region block */
339 next_type = BCMA_EROM_GET_ATTR(entry, REGION_TYPE);
340 next_port = BCMA_EROM_GET_ATTR(entry, REGION_PORT);
342 if (next_type != region_type ||
343 next_port != region_port)
346 /* Parse the region */
347 if ((error = bcma_erom_parse_sport_region(sc, ®ion)))
350 /* Is this our target region_num? */
351 if (i == region_num) {
353 *addr = region.base_addr;
364 bcma_erom_get_core_table(bhnd_erom_t *erom, struct bhnd_core_info **cores,
367 struct bcma_erom *sc;
368 struct bhnd_core_info *buffer;
369 bus_size_t initial_offset;
373 sc = (struct bcma_erom *)erom;
376 initial_offset = bcma_erom_tell(sc);
378 /* Determine the core count */
380 for (count = 0, error = 0; !error; count++) {
381 struct bcma_erom_core core;
383 /* Seek to the first readable core entry */
384 error = bcma_erom_seek_next(sc, BCMA_EROM_ENTRY_TYPE_CORE);
390 /* Read past the core descriptor */
391 if ((error = bcma_erom_parse_core(sc, &core)))
395 /* Allocate our output buffer */
396 buffer = mallocarray(count, sizeof(struct bhnd_core_info), M_BHND,
398 if (buffer == NULL) {
403 /* Parse all core descriptors */
405 for (u_int i = 0; i < count; i++) {
406 struct bcma_erom_core core;
410 error = bcma_erom_seek_next(sc, BCMA_EROM_ENTRY_TYPE_CORE);
414 error = bcma_erom_parse_core(sc, &core);
418 /* Determine the unit number */
420 for (u_int j = 0; j < i; j++) {
421 if (buffer[i].vendor == buffer[j].vendor &&
422 buffer[i].device == buffer[j].device)
426 /* Convert to a bhnd info record */
427 bcma_erom_to_core_info(&core, i, unit, &buffer[i]);
436 free(buffer, M_BHND);
439 /* Restore the initial position */
440 bcma_erom_seek(sc, initial_offset);
445 bcma_erom_free_core_table(bhnd_erom_t *erom, struct bhnd_core_info *cores)
451 * Return the current read position.
454 bcma_erom_tell(struct bcma_erom *erom)
456 return (erom->offset);
460 * Seek to an absolute read position.
463 bcma_erom_seek(struct bcma_erom *erom, bus_size_t offset)
465 erom->offset = offset;
469 * Read a 32-bit entry value from the EROM table without advancing the
472 * @param erom EROM read state.
473 * @param entry Will contain the read result on success.
475 * @retval ENOENT The end of the EROM table was reached.
476 * @retval non-zero The read could not be completed.
479 bcma_erom_peek32(struct bcma_erom *erom, uint32_t *entry)
481 if (erom->offset >= (BCMA_EROM_TABLE_SIZE - sizeof(uint32_t))) {
482 EROM_LOG(erom, "BCMA EROM table missing terminating EOF\n");
486 *entry = bhnd_erom_io_read(erom->eio, erom->offset, 4);
491 * Read a 32-bit entry value from the EROM table.
493 * @param erom EROM read state.
494 * @param entry Will contain the read result on success.
496 * @retval ENOENT The end of the EROM table was reached.
497 * @retval non-zero The read could not be completed.
500 bcma_erom_read32(struct bcma_erom *erom, uint32_t *entry)
504 if ((error = bcma_erom_peek32(erom, entry)) == 0)
511 * Read and discard 32-bit entry value from the EROM table.
513 * @param erom EROM read state.
515 * @retval ENOENT The end of the EROM table was reached.
516 * @retval non-zero The read could not be completed.
519 bcma_erom_skip32(struct bcma_erom *erom)
523 return bcma_erom_read32(erom, &entry);
527 * Read and discard a core descriptor from the EROM table.
529 * @param erom EROM read state.
531 * @retval ENOENT The end of the EROM table was reached.
532 * @retval non-zero The read could not be completed.
535 bcma_erom_skip_core(struct bcma_erom *erom)
537 struct bcma_erom_core core;
538 return (bcma_erom_parse_core(erom, &core));
542 * Read and discard a master port descriptor from the EROM table.
544 * @param erom EROM read state.
546 * @retval ENOENT The end of the EROM table was reached.
547 * @retval non-zero The read could not be completed.
550 bcma_erom_skip_mport(struct bcma_erom *erom)
552 struct bcma_erom_mport mp;
553 return (bcma_erom_parse_mport(erom, &mp));
557 * Read and discard a port region descriptor from the EROM table.
559 * @param erom EROM read state.
561 * @retval ENOENT The end of the EROM table was reached.
562 * @retval non-zero The read could not be completed.
565 bcma_erom_skip_sport_region(struct bcma_erom *erom)
567 struct bcma_erom_sport_region r;
568 return (bcma_erom_parse_sport_region(erom, &r));
572 * Seek to the next entry matching the given EROM entry type.
574 * @param erom EROM read state.
575 * @param etype One of BCMA_EROM_ENTRY_TYPE_CORE,
576 * BCMA_EROM_ENTRY_TYPE_MPORT, or BCMA_EROM_ENTRY_TYPE_REGION.
578 * @retval ENOENT The end of the EROM table was reached.
579 * @retval non-zero Reading or parsing the descriptor failed.
582 bcma_erom_seek_next(struct bcma_erom *erom, uint8_t etype)
587 /* Iterate until we hit an entry matching the requested type. */
588 while (!(error = bcma_erom_peek32(erom, &entry))) {
590 if (entry == BCMA_EROM_TABLE_EOF)
594 if (!BCMA_EROM_GET_ATTR(entry, ENTRY_ISVALID))
597 /* Entry type matches? */
598 if (BCMA_EROM_GET_ATTR(entry, ENTRY_TYPE) == etype)
601 /* Skip non-matching entry types. */
602 switch (BCMA_EROM_GET_ATTR(entry, ENTRY_TYPE)) {
603 case BCMA_EROM_ENTRY_TYPE_CORE:
604 if ((error = bcma_erom_skip_core(erom)))
609 case BCMA_EROM_ENTRY_TYPE_MPORT:
610 if ((error = bcma_erom_skip_mport(erom)))
615 case BCMA_EROM_ENTRY_TYPE_REGION:
616 if ((error = bcma_erom_skip_sport_region(erom)))
621 /* Unknown entry type! */
630 * Return the read position to the start of the EROM table.
632 * @param erom EROM read state.
635 bcma_erom_reset(struct bcma_erom *erom)
641 * Seek to the first core entry matching @p desc.
643 * @param erom EROM read state.
644 * @param desc The core match descriptor.
645 * @param[out] core On success, the matching core info. If the core info
646 * is not desired, a NULL pointer may be provided.
648 * @retval ENOENT The end of the EROM table was reached before @p index was
650 * @retval non-zero Reading or parsing failed.
653 bcma_erom_seek_matching_core(struct bcma_erom *sc,
654 const struct bhnd_core_match *desc, struct bhnd_core_info *core)
656 struct bhnd_core_match imatch;
657 bus_size_t core_offset, next_offset;
660 /* Seek to table start. */
663 /* We can't determine a core's unit number during the initial scan. */
665 imatch.m.match.core_unit = 0;
667 /* Locate the first matching core */
668 for (u_int i = 0; i < UINT_MAX; i++) {
669 struct bcma_erom_core ec;
670 struct bhnd_core_info ci;
672 /* Seek to the next core */
673 error = bcma_erom_seek_next(sc, BCMA_EROM_ENTRY_TYPE_CORE);
677 /* Save the core offset */
678 core_offset = bcma_erom_tell(sc);
681 if ((error = bcma_erom_parse_core(sc, &ec)))
684 bcma_erom_to_core_info(&ec, i, 0, &ci);
686 /* Check for initial match */
687 if (!bhnd_core_matches(&ci, &imatch))
690 /* Re-scan preceding cores to determine the unit number. */
691 next_offset = bcma_erom_tell(sc);
693 for (u_int j = 0; j < i; j++) {
695 error = bcma_erom_seek_next(sc,
696 BCMA_EROM_ENTRY_TYPE_CORE);
700 if ((error = bcma_erom_parse_core(sc, &ec)))
703 /* Bump the unit number? */
704 if (ec.vendor == ci.vendor && ec.device == ci.device)
708 /* Check for full match against now-valid unit number */
709 if (!bhnd_core_matches(&ci, desc)) {
710 /* Reposition to allow reading the next core */
711 bcma_erom_seek(sc, next_offset);
715 /* Found; seek to the core's initial offset and provide
716 * the core info to the caller */
717 bcma_erom_seek(sc, core_offset);
724 /* Not found, or a parse error occured */
729 * Read the next core descriptor from the EROM table.
731 * @param erom EROM read state.
732 * @param[out] core On success, will be populated with the parsed core
735 * @retval ENOENT The end of the EROM table was reached.
736 * @retval non-zero Reading or parsing the core descriptor failed.
739 bcma_erom_parse_core(struct bcma_erom *erom, struct bcma_erom_core *core)
744 /* Parse CoreDescA */
745 if ((error = bcma_erom_read32(erom, &entry)))
749 if (entry == BCMA_EROM_TABLE_EOF)
752 if (!BCMA_EROM_ENTRY_IS(entry, CORE)) {
753 EROM_LOG(erom, "Unexpected EROM entry 0x%x (type=%s)\n",
754 entry, bcma_erom_entry_type_name(entry));
759 core->vendor = BCMA_EROM_GET_ATTR(entry, COREA_DESIGNER);
760 core->device = BCMA_EROM_GET_ATTR(entry, COREA_ID);
762 /* Parse CoreDescB */
763 if ((error = bcma_erom_read32(erom, &entry)))
766 if (!BCMA_EROM_ENTRY_IS(entry, CORE)) {
770 core->rev = BCMA_EROM_GET_ATTR(entry, COREB_REV);
771 core->num_mport = BCMA_EROM_GET_ATTR(entry, COREB_NUM_MP);
772 core->num_dport = BCMA_EROM_GET_ATTR(entry, COREB_NUM_DP);
773 core->num_mwrap = BCMA_EROM_GET_ATTR(entry, COREB_NUM_WMP);
774 core->num_swrap = BCMA_EROM_GET_ATTR(entry, COREB_NUM_WSP);
780 * Read the next master port descriptor from the EROM table.
782 * @param erom EROM read state.
783 * @param[out] mport On success, will be populated with the parsed
786 * @retval non-zero Reading or parsing the descriptor failed.
789 bcma_erom_parse_mport(struct bcma_erom *erom, struct bcma_erom_mport *mport)
794 /* Parse the master port descriptor */
795 if ((error = bcma_erom_read32(erom, &entry)))
798 if (!BCMA_EROM_ENTRY_IS(entry, MPORT))
801 mport->port_vid = BCMA_EROM_GET_ATTR(entry, MPORT_ID);
802 mport->port_num = BCMA_EROM_GET_ATTR(entry, MPORT_NUM);
808 * Read the next slave port region descriptor from the EROM table.
810 * @param erom EROM read state.
811 * @param[out] mport On success, will be populated with the parsed
814 * @retval ENOENT The end of the region descriptor table was reached.
815 * @retval non-zero Reading or parsing the descriptor failed.
818 bcma_erom_parse_sport_region(struct bcma_erom *erom,
819 struct bcma_erom_sport_region *region)
825 /* Peek at the region descriptor */
826 if (bcma_erom_peek32(erom, &entry))
829 /* A non-region entry signals the end of the region table */
830 if (!BCMA_EROM_ENTRY_IS(entry, REGION)) {
833 bcma_erom_skip32(erom);
836 region->base_addr = BCMA_EROM_GET_ATTR(entry, REGION_BASE);
837 region->region_type = BCMA_EROM_GET_ATTR(entry, REGION_TYPE);
838 region->region_port = BCMA_EROM_GET_ATTR(entry, REGION_PORT);
839 size_type = BCMA_EROM_GET_ATTR(entry, REGION_SIZE);
841 /* If region address is 64-bit, fetch the high bits. */
842 if (BCMA_EROM_GET_ATTR(entry, REGION_64BIT)) {
843 if ((error = bcma_erom_read32(erom, &entry)))
846 region->base_addr |= ((bhnd_addr_t) entry << 32);
849 /* Parse the region size; it's either encoded as the binary logarithm
850 * of the number of 4K pages (i.e. log2 n), or its encoded as a
851 * 32-bit/64-bit literal value directly following the current entry. */
852 if (size_type == BCMA_EROM_REGION_SIZE_OTHER) {
853 if ((error = bcma_erom_read32(erom, &entry)))
856 region->size = BCMA_EROM_GET_ATTR(entry, RSIZE_VAL);
858 if (BCMA_EROM_GET_ATTR(entry, RSIZE_64BIT)) {
859 if ((error = bcma_erom_read32(erom, &entry)))
861 region->size |= ((bhnd_size_t) entry << 32);
864 region->size = BCMA_EROM_REGION_SIZE_BASE << size_type;
867 /* Verify that addr+size does not overflow. */
868 if (region->size != 0 &&
869 BHND_ADDR_MAX - (region->size - 1) < region->base_addr)
871 EROM_LOG(erom, "%s%u: invalid address map %llx:%llx\n",
872 bcma_erom_entry_type_name(region->region_type),
874 (unsigned long long) region->base_addr,
875 (unsigned long long) region->size);
884 * Convert a bcma_erom_core record to its bhnd_core_info representation.
886 * @param core EROM core record to convert.
887 * @param core_idx The core index of @p core.
888 * @param core_unit The core unit of @p core.
889 * @param[out] info The populated bhnd_core_info representation.
892 bcma_erom_to_core_info(const struct bcma_erom_core *core, u_int core_idx,
893 int core_unit, struct bhnd_core_info *info)
895 info->vendor = core->vendor;
896 info->device = core->device;
897 info->hwrev = core->rev;
898 info->core_idx = core_idx;
899 info->unit = core_unit;
903 * Map an EROM region type to its corresponding port type.
905 * @param region_type Region type value.
906 * @param[out] port_type On success, the corresponding port type.
909 bcma_erom_region_to_port_type(struct bcma_erom *erom, uint8_t region_type,
910 bhnd_port_type *port_type)
912 switch (region_type) {
913 case BCMA_EROM_REGION_TYPE_DEVICE:
914 *port_type = BHND_PORT_DEVICE;
916 case BCMA_EROM_REGION_TYPE_BRIDGE:
917 *port_type = BHND_PORT_BRIDGE;
919 case BCMA_EROM_REGION_TYPE_MWRAP:
920 case BCMA_EROM_REGION_TYPE_SWRAP:
921 *port_type = BHND_PORT_AGENT;
924 EROM_LOG(erom, "unsupported region type %hhx\n",
931 * Register all MMIO region descriptors for the given slave port.
933 * @param erom EROM read state.
934 * @param corecfg Core info to be populated with the scanned port regions.
935 * @param port_num Port index for which regions will be parsed.
936 * @param region_type The region type to be parsed.
937 * @param[out] offset The offset at which to perform parsing. On success, this
938 * will be updated to point to the next EROM table entry.
941 bcma_erom_corecfg_fill_port_regions(struct bcma_erom *erom,
942 struct bcma_corecfg *corecfg, bcma_pid_t port_num,
945 struct bcma_sport *sport;
946 struct bcma_sport_list *sports;
947 bus_size_t entry_offset;
949 bhnd_port_type port_type;
953 /* Determine the port type for this region type. */
954 error = bcma_erom_region_to_port_type(erom, region_type, &port_type);
958 /* Fetch the list to be populated */
959 sports = bcma_corecfg_get_port_list(corecfg, port_type);
961 /* Allocate a new port descriptor */
962 sport = bcma_alloc_sport(port_num, port_type);
966 /* Read all address regions defined for this port */
967 for (bcma_rmid_t region_num = 0;; region_num++) {
968 struct bcma_map *map;
969 struct bcma_erom_sport_region spr;
971 /* No valid port definition should come anywhere near
973 if (region_num == BCMA_RMID_MAX) {
974 EROM_LOG(erom, "core%u %s%u: region count reached "
975 "upper limit of %u\n",
976 corecfg->core_info.core_idx,
977 bhnd_port_type_name(port_type),
978 port_num, BCMA_RMID_MAX);
984 /* Parse the next region entry. */
985 entry_offset = bcma_erom_tell(erom);
986 error = bcma_erom_parse_sport_region(erom, &spr);
987 if (error && error != ENOENT) {
988 EROM_LOG(erom, "core%u %s%u.%u: invalid slave port "
990 corecfg->core_info.core_idx,
991 bhnd_port_type_name(port_type),
992 port_num, region_num);
996 /* ENOENT signals no further region entries */
997 if (error == ENOENT) {
998 /* No further entries */
1003 /* A region or type mismatch also signals no further region
1005 if (spr.region_port != port_num ||
1006 spr.region_type != region_type)
1008 /* We don't want to consume this entry */
1009 bcma_erom_seek(erom, entry_offset);
1016 * Create the map entry.
1018 map = malloc(sizeof(struct bcma_map), M_BHND, M_NOWAIT);
1024 map->m_region_num = region_num;
1025 map->m_base = spr.base_addr;
1026 map->m_size = spr.size;
1029 /* Add the region map to the port */
1030 STAILQ_INSERT_TAIL(&sport->sp_maps, map, m_link);
1031 sport->sp_num_maps++;
1035 /* Append the new port descriptor on success, or deallocate the
1036 * partially parsed descriptor on failure. */
1038 STAILQ_INSERT_TAIL(sports, sport, sp_link);
1039 } else if (sport != NULL) {
1040 bcma_free_sport(sport);
1047 * Parse the next core entry from the EROM table and produce a bcma_corecfg
1048 * to be owned by the caller.
1050 * @param erom A bcma EROM instance.
1051 * @param[out] result On success, the core's device info. The caller inherits
1052 * ownership of this allocation.
1054 * @return If successful, returns 0. If the end of the EROM table is hit,
1055 * ENOENT will be returned. On error, returns a non-zero error value.
1058 bcma_erom_next_corecfg(struct bcma_erom *erom, struct bcma_corecfg **result)
1060 struct bcma_corecfg *cfg;
1061 struct bcma_erom_core core;
1062 uint8_t first_region_type;
1063 bus_size_t initial_offset;
1069 initial_offset = bcma_erom_tell(erom);
1071 /* Parse the next core entry */
1072 if ((error = bcma_erom_parse_core(erom, &core)))
1075 /* Determine the core's index and unit numbers */
1076 bcma_erom_reset(erom);
1079 for (; bcma_erom_tell(erom) != initial_offset; core_index++) {
1080 struct bcma_erom_core prev_core;
1082 /* Parse next core */
1083 error = bcma_erom_seek_next(erom, BCMA_EROM_ENTRY_TYPE_CORE);
1087 if ((error = bcma_erom_parse_core(erom, &prev_core)))
1090 /* Is earlier unit? */
1091 if (core.vendor == prev_core.vendor &&
1092 core.device == prev_core.device)
1097 /* Seek to next core */
1098 error = bcma_erom_seek_next(erom, BCMA_EROM_ENTRY_TYPE_CORE);
1103 /* We already parsed the core descriptor */
1104 if ((error = bcma_erom_skip_core(erom)))
1107 /* Allocate our corecfg */
1108 cfg = bcma_alloc_corecfg(core_index, core_unit, core.vendor,
1109 core.device, core.rev);
1113 /* These are 5-bit values in the EROM table, and should never be able
1114 * to overflow BCMA_PID_MAX. */
1115 KASSERT(core.num_mport <= BCMA_PID_MAX, ("unsupported mport count"));
1116 KASSERT(core.num_dport <= BCMA_PID_MAX, ("unsupported dport count"));
1117 KASSERT(core.num_mwrap + core.num_swrap <= BCMA_PID_MAX,
1118 ("unsupported wport count"));
1122 "core%u: %s %s (cid=%hx, rev=%hu, unit=%d)\n",
1124 bhnd_vendor_name(core.vendor),
1125 bhnd_find_core_name(core.vendor, core.device),
1126 core.device, core.rev, core_unit);
1129 cfg->num_master_ports = core.num_mport;
1130 cfg->num_dev_ports = 0; /* determined below */
1131 cfg->num_bridge_ports = 0; /* determined blow */
1132 cfg->num_wrapper_ports = core.num_mwrap + core.num_swrap;
1134 /* Parse Master Port Descriptors */
1135 for (uint8_t i = 0; i < core.num_mport; i++) {
1136 struct bcma_mport *mport;
1137 struct bcma_erom_mport mpd;
1139 /* Parse the master port descriptor */
1140 error = bcma_erom_parse_mport(erom, &mpd);
1144 /* Initialize a new bus mport structure */
1145 mport = malloc(sizeof(struct bcma_mport), M_BHND, M_NOWAIT);
1146 if (mport == NULL) {
1151 mport->mp_vid = mpd.port_vid;
1152 mport->mp_num = mpd.port_num;
1155 STAILQ_INSERT_TAIL(&cfg->master_ports, mport, mp_link);
1160 * Determine whether this is a bridge device; if so, we can
1161 * expect the first sequence of address region descriptors to
1162 * be of EROM_REGION_TYPE_BRIDGE instead of
1163 * BCMA_EROM_REGION_TYPE_DEVICE.
1165 * It's unclear whether this is the correct mechanism by which we
1166 * should detect/handle bridge devices, but this approach matches
1167 * that of (some of) Broadcom's published drivers.
1169 if (core.num_dport > 0) {
1172 if ((error = bcma_erom_peek32(erom, &entry)))
1175 if (BCMA_EROM_ENTRY_IS(entry, REGION) &&
1176 BCMA_EROM_GET_ATTR(entry, REGION_TYPE) == BCMA_EROM_REGION_TYPE_BRIDGE)
1178 first_region_type = BCMA_EROM_REGION_TYPE_BRIDGE;
1179 cfg->num_dev_ports = 0;
1180 cfg->num_bridge_ports = core.num_dport;
1182 first_region_type = BCMA_EROM_REGION_TYPE_DEVICE;
1183 cfg->num_dev_ports = core.num_dport;
1184 cfg->num_bridge_ports = 0;
1188 /* Device/bridge port descriptors */
1189 for (uint8_t sp_num = 0; sp_num < core.num_dport; sp_num++) {
1190 error = bcma_erom_corecfg_fill_port_regions(erom, cfg, sp_num,
1197 /* Wrapper (aka device management) descriptors (for master ports). */
1198 for (uint8_t sp_num = 0; sp_num < core.num_mwrap; sp_num++) {
1199 error = bcma_erom_corecfg_fill_port_regions(erom, cfg, sp_num,
1200 BCMA_EROM_REGION_TYPE_MWRAP);
1207 /* Wrapper (aka device management) descriptors (for slave ports). */
1208 for (uint8_t i = 0; i < core.num_swrap; i++) {
1209 /* Slave wrapper ports are not numbered distinctly from master
1213 * Broadcom DDR1/DDR2 Memory Controller
1214 * (cid=82e, rev=1, unit=0, d/mw/sw = 2/0/1 ) ->
1215 * bhnd0: erom[0xdc]: core6 agent0.0: mismatch got: 0x1 (0x2)
1217 * ARM BP135 AMBA3 AXI to APB Bridge
1218 * (cid=135, rev=0, unit=0, d/mw/sw = 1/0/1 ) ->
1219 * bhnd0: erom[0x124]: core9 agent1.0: mismatch got: 0x0 (0x2)
1223 * (core.num_mwrap > 0) ?
1225 * ((core.vendor == BHND_MFGID_BCM) ? 1 : 0)
1228 sp_num = (core.num_mwrap > 0) ?
1230 ((core.vendor == BHND_MFGID_BCM) ? 1 : 0) + i;
1231 error = bcma_erom_corecfg_fill_port_regions(erom, cfg, sp_num,
1232 BCMA_EROM_REGION_TYPE_SWRAP);
1239 * Seek to the next core entry (if any), skipping any dangling/invalid
1242 * On the BCM4706, the EROM entry for the memory controller core
1243 * (0x4bf/0x52E) contains a dangling/unused slave wrapper port region
1246 if ((error = bcma_erom_seek_next(erom, BCMA_EROM_ENTRY_TYPE_CORE))) {
1247 if (error != ENOENT)
1256 bcma_free_corecfg(cfg);
1262 bcma_erom_dump(bhnd_erom_t *erom)
1264 struct bcma_erom *sc;
1268 sc = (struct bcma_erom *)erom;
1270 bcma_erom_reset(sc);
1272 while (!(error = bcma_erom_read32(sc, &entry))) {
1274 if (entry == BCMA_EROM_TABLE_EOF) {
1275 EROM_LOG(sc, "EOF\n");
1280 if (!BCMA_EROM_GET_ATTR(entry, ENTRY_ISVALID)) {
1281 EROM_LOG(sc, "invalid EROM entry %#x\n", entry);
1285 switch (BCMA_EROM_GET_ATTR(entry, ENTRY_TYPE)) {
1286 case BCMA_EROM_ENTRY_TYPE_CORE: {
1288 EROM_LOG(sc, "coreA (0x%x)\n", entry);
1289 EROM_LOG(sc, "\tdesigner:\t0x%x\n",
1290 BCMA_EROM_GET_ATTR(entry, COREA_DESIGNER));
1291 EROM_LOG(sc, "\tid:\t\t0x%x\n",
1292 BCMA_EROM_GET_ATTR(entry, COREA_ID));
1293 EROM_LOG(sc, "\tclass:\t\t0x%x\n",
1294 BCMA_EROM_GET_ATTR(entry, COREA_CLASS));
1297 if ((error = bcma_erom_read32(sc, &entry))) {
1298 EROM_LOG(sc, "error reading CoreDescB: %d\n",
1303 if (!BCMA_EROM_ENTRY_IS(entry, CORE)) {
1304 EROM_LOG(sc, "invalid core descriptor; found "
1305 "unexpected entry %#x (type=%s)\n",
1306 entry, bcma_erom_entry_type_name(entry));
1310 EROM_LOG(sc, "coreB (0x%x)\n", entry);
1311 EROM_LOG(sc, "\trev:\t0x%x\n",
1312 BCMA_EROM_GET_ATTR(entry, COREB_REV));
1313 EROM_LOG(sc, "\tnummp:\t0x%x\n",
1314 BCMA_EROM_GET_ATTR(entry, COREB_NUM_MP));
1315 EROM_LOG(sc, "\tnumdp:\t0x%x\n",
1316 BCMA_EROM_GET_ATTR(entry, COREB_NUM_DP));
1317 EROM_LOG(sc, "\tnumwmp:\t0x%x\n",
1318 BCMA_EROM_GET_ATTR(entry, COREB_NUM_WMP));
1319 EROM_LOG(sc, "\tnumwsp:\t0x%x\n",
1320 BCMA_EROM_GET_ATTR(entry, COREB_NUM_WMP));
1324 case BCMA_EROM_ENTRY_TYPE_MPORT:
1325 EROM_LOG(sc, "\tmport 0x%x\n", entry);
1326 EROM_LOG(sc, "\t\tport:\t0x%x\n",
1327 BCMA_EROM_GET_ATTR(entry, MPORT_NUM));
1328 EROM_LOG(sc, "\t\tid:\t\t0x%x\n",
1329 BCMA_EROM_GET_ATTR(entry, MPORT_ID));
1332 case BCMA_EROM_ENTRY_TYPE_REGION: {
1336 addr64 = (BCMA_EROM_GET_ATTR(entry, REGION_64BIT) != 0);
1337 size_type = BCMA_EROM_GET_ATTR(entry, REGION_SIZE);
1339 EROM_LOG(sc, "\tregion 0x%x:\n", entry);
1340 EROM_LOG(sc, "\t\t%s:\t0x%x\n",
1341 addr64 ? "baselo" : "base",
1342 BCMA_EROM_GET_ATTR(entry, REGION_BASE));
1343 EROM_LOG(sc, "\t\tport:\t0x%x\n",
1344 BCMA_EROM_GET_ATTR(entry, REGION_PORT));
1345 EROM_LOG(sc, "\t\ttype:\t0x%x\n",
1346 BCMA_EROM_GET_ATTR(entry, REGION_TYPE));
1347 EROM_LOG(sc, "\t\tsztype:\t0x%hhx\n", size_type);
1349 /* Read the base address high bits */
1351 if ((error = bcma_erom_read32(sc, &entry))) {
1352 EROM_LOG(sc, "error reading region "
1353 "base address high bits %d\n",
1358 EROM_LOG(sc, "\t\tbasehi:\t0x%x\n", entry);
1361 /* Read extended size descriptor */
1362 if (size_type == BCMA_EROM_REGION_SIZE_OTHER) {
1365 if ((error = bcma_erom_read32(sc, &entry))) {
1366 EROM_LOG(sc, "error reading region "
1367 "size descriptor %d\n",
1372 if (BCMA_EROM_GET_ATTR(entry, RSIZE_64BIT))
1377 EROM_LOG(sc, "\t\t%s:\t0x%x\n",
1378 size64 ? "sizelo" : "size",
1379 BCMA_EROM_GET_ATTR(entry, RSIZE_VAL));
1382 error = bcma_erom_read32(sc, &entry);
1384 EROM_LOG(sc, "error reading "
1385 "region size high bits: "
1390 EROM_LOG(sc, "\t\tsizehi:\t0x%x\n",
1398 EROM_LOG(sc, "unknown EROM entry 0x%x (type=%s)\n",
1399 entry, bcma_erom_entry_type_name(entry));
1404 if (error == ENOENT)
1405 EROM_LOG(sc, "BCMA EROM table missing terminating EOF\n");
1407 EROM_LOG(sc, "EROM read failed: %d\n", error);
1412 static kobj_method_t bcma_erom_methods[] = {
1413 KOBJMETHOD(bhnd_erom_probe, bcma_erom_probe),
1414 KOBJMETHOD(bhnd_erom_init, bcma_erom_init),
1415 KOBJMETHOD(bhnd_erom_fini, bcma_erom_fini),
1416 KOBJMETHOD(bhnd_erom_get_core_table, bcma_erom_get_core_table),
1417 KOBJMETHOD(bhnd_erom_free_core_table, bcma_erom_free_core_table),
1418 KOBJMETHOD(bhnd_erom_lookup_core, bcma_erom_lookup_core),
1419 KOBJMETHOD(bhnd_erom_lookup_core_addr, bcma_erom_lookup_core_addr),
1420 KOBJMETHOD(bhnd_erom_dump, bcma_erom_dump),
1425 BHND_EROM_DEFINE_CLASS(bcma_erom, bcma_erom_parser, bcma_erom_methods, sizeof(struct bcma_erom));