1 /*- 2 * Copyright (c) 2015 Landon Fuller <landon@landonf.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 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. 15 * 16 * NO WARRANTY 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. 28 */ 29 30 #include <sys/cdefs.h> 31 __FBSDID("$FreeBSD$"); 32 33 #include <sys/param.h> 34 #include <sys/bus.h> 35 #include <sys/systm.h> 36 37 #include <machine/bus.h> 38 #include <sys/rman.h> 39 #include <machine/resource.h> 40 41 #include <dev/bhnd/siba/sibareg.h> 42 43 #include <dev/bhnd/cores/chipc/chipcreg.h> 44 45 #include "nvram/bhnd_nvram.h" 46 47 #include "bhnd_chipc_if.h" 48 49 #include "bhnd_nvram_if.h" 50 #include "bhnd_nvram_map.h" 51 52 #include "bhndreg.h" 53 #include "bhndvar.h" 54 55 /* BHND core device description table. */ 56 static const struct bhnd_core_desc { 57 uint16_t vendor; 58 uint16_t device; 59 bhnd_devclass_t class; 60 const char *desc; 61 } bhnd_core_descs[] = { 62 #define BHND_CDESC(_mfg, _cid, _cls, _desc) \ 63 { BHND_MFGID_ ## _mfg, BHND_COREID_ ## _cid, \ 64 BHND_DEVCLASS_ ## _cls, _desc } 65 66 BHND_CDESC(BCM, CC, CC, "ChipCommon I/O Controller"), 67 BHND_CDESC(BCM, ILINE20, OTHER, "iLine20 HPNA"), 68 BHND_CDESC(BCM, SRAM, RAM, "SRAM"), 69 BHND_CDESC(BCM, SDRAM, RAM, "SDRAM"), 70 BHND_CDESC(BCM, PCI, PCI, "PCI Bridge"), 71 BHND_CDESC(BCM, MIPS, CPU, "MIPS Core"), 72 BHND_CDESC(BCM, ENET, ENET_MAC, "Fast Ethernet MAC"), 73 BHND_CDESC(BCM, CODEC, OTHER, "V.90 Modem Codec"), 74 BHND_CDESC(BCM, USB, USB_DUAL, "USB 1.1 Device/Host Controller"), 75 BHND_CDESC(BCM, ADSL, OTHER, "ADSL Core"), 76 BHND_CDESC(BCM, ILINE100, OTHER, "iLine100 HPNA"), 77 BHND_CDESC(BCM, IPSEC, OTHER, "IPsec Accelerator"), 78 BHND_CDESC(BCM, UTOPIA, OTHER, "UTOPIA ATM Core"), 79 BHND_CDESC(BCM, PCMCIA, PCCARD, "PCMCIA Bridge"), 80 BHND_CDESC(BCM, SOCRAM, RAM, "Internal Memory"), 81 BHND_CDESC(BCM, MEMC, MEMC, "MEMC SDRAM Controller"), 82 BHND_CDESC(BCM, OFDM, OTHER, "OFDM PHY"), 83 BHND_CDESC(BCM, EXTIF, OTHER, "External Interface"), 84 BHND_CDESC(BCM, D11, WLAN, "802.11 MAC/PHY/Radio"), 85 BHND_CDESC(BCM, APHY, WLAN_PHY, "802.11a PHY"), 86 BHND_CDESC(BCM, BPHY, WLAN_PHY, "802.11b PHY"), 87 BHND_CDESC(BCM, GPHY, WLAN_PHY, "802.11g PHY"), 88 BHND_CDESC(BCM, MIPS33, CPU, "MIPS3302 Core"), 89 BHND_CDESC(BCM, USB11H, USB_HOST, "USB 1.1 Host Controller"), 90 BHND_CDESC(BCM, USB11D, USB_DEV, "USB 1.1 Device Controller"), 91 BHND_CDESC(BCM, USB20H, USB_HOST, "USB 2.0 Host Controller"), 92 BHND_CDESC(BCM, USB20D, USB_DEV, "USB 2.0 Device Controller"), 93 BHND_CDESC(BCM, SDIOH, OTHER, "SDIO Host Controller"), 94 BHND_CDESC(BCM, ROBO, OTHER, "RoboSwitch"), 95 BHND_CDESC(BCM, ATA100, OTHER, "Parallel ATA Controller"), 96 BHND_CDESC(BCM, SATAXOR, OTHER, "SATA DMA/XOR Controller"), 97 BHND_CDESC(BCM, GIGETH, ENET_MAC, "Gigabit Ethernet MAC"), 98 BHND_CDESC(BCM, PCIE, PCIE, "PCIe Bridge"), 99 BHND_CDESC(BCM, NPHY, WLAN_PHY, "802.11n 2x2 PHY"), 100 BHND_CDESC(BCM, SRAMC, MEMC, "SRAM Controller"), 101 BHND_CDESC(BCM, MINIMAC, OTHER, "MINI MAC/PHY"), 102 BHND_CDESC(BCM, ARM11, CPU, "ARM1176 CPU"), 103 BHND_CDESC(BCM, ARM7S, CPU, "ARM7TDMI-S CPU"), 104 BHND_CDESC(BCM, LPPHY, WLAN_PHY, "802.11a/b/g PHY"), 105 BHND_CDESC(BCM, PMU, PMU, "PMU"), 106 BHND_CDESC(BCM, SSNPHY, WLAN_PHY, "802.11n Single-Stream PHY"), 107 BHND_CDESC(BCM, SDIOD, OTHER, "SDIO Device Core"), 108 BHND_CDESC(BCM, ARMCM3, CPU, "ARM Cortex-M3 CPU"), 109 BHND_CDESC(BCM, HTPHY, WLAN_PHY, "802.11n 4x4 PHY"), 110 BHND_CDESC(MIPS,MIPS74K, CPU, "MIPS74k CPU"), 111 BHND_CDESC(BCM, GMAC, ENET_MAC, "Gigabit MAC core"), 112 BHND_CDESC(BCM, DMEMC, MEMC, "DDR1/DDR2 Memory Controller"), 113 BHND_CDESC(BCM, PCIERC, OTHER, "PCIe Root Complex"), 114 BHND_CDESC(BCM, OCP, SOC_BRIDGE, "OCP to OCP Bridge"), 115 BHND_CDESC(BCM, SC, OTHER, "Shared Common Core"), 116 BHND_CDESC(BCM, AHB, SOC_BRIDGE, "OCP to AHB Bridge"), 117 BHND_CDESC(BCM, SPIH, OTHER, "SPI Host Controller"), 118 BHND_CDESC(BCM, I2S, OTHER, "I2S Digital Audio Interface"), 119 BHND_CDESC(BCM, DMEMS, MEMC, "SDR/DDR1 Memory Controller"), 120 BHND_CDESC(BCM, UBUS_SHIM, OTHER, "BCM6362/UBUS WLAN SHIM"), 121 BHND_CDESC(BCM, PCIE2, PCIE, "PCIe Bridge (Gen2)"), 122 123 BHND_CDESC(ARM, APB_BRIDGE, SOC_BRIDGE, "BP135 AMBA3 AXI to APB Bridge"), 124 BHND_CDESC(ARM, PL301, SOC_ROUTER, "PL301 AMBA3 Interconnect"), 125 BHND_CDESC(ARM, EROM, EROM, "PL366 Device Enumeration ROM"), 126 BHND_CDESC(ARM, OOB_ROUTER, OTHER, "PL367 OOB Interrupt Router"), 127 BHND_CDESC(ARM, AXI_UNMAPPED, OTHER, "Unmapped Address Ranges"), 128 129 BHND_CDESC(BCM, 4706_CC, CC, "ChipCommon I/O Controller"), 130 BHND_CDESC(BCM, NS_PCIE2, PCIE, "PCIe Bridge (Gen2)"), 131 BHND_CDESC(BCM, NS_DMA, OTHER, "DMA engine"), 132 BHND_CDESC(BCM, NS_SDIO, OTHER, "SDIO 3.0 Host Controller"), 133 BHND_CDESC(BCM, NS_USB20H, USB_HOST, "USB 2.0 Host Controller"), 134 BHND_CDESC(BCM, NS_USB30H, USB_HOST, "USB 3.0 Host Controller"), 135 BHND_CDESC(BCM, NS_A9JTAG, OTHER, "ARM Cortex A9 JTAG Interface"), 136 BHND_CDESC(BCM, NS_DDR23_MEMC, MEMC, "Denali DDR2/DD3 Memory Controller"), 137 BHND_CDESC(BCM, NS_ROM, NVRAM, "System ROM"), 138 BHND_CDESC(BCM, NS_NAND, NVRAM, "NAND Flash Controller"), 139 BHND_CDESC(BCM, NS_QSPI, NVRAM, "QSPI Flash Controller"), 140 BHND_CDESC(BCM, NS_CC_B, CC_B, "ChipCommon B Auxiliary I/O Controller"), 141 BHND_CDESC(BCM, 4706_SOCRAM, RAM, "Internal Memory"), 142 BHND_CDESC(BCM, IHOST_ARMCA9, CPU, "ARM Cortex A9 CPU"), 143 BHND_CDESC(BCM, 4706_GMAC_CMN, ENET, "Gigabit MAC (Common)"), 144 BHND_CDESC(BCM, 4706_GMAC, ENET_MAC, "Gigabit MAC"), 145 BHND_CDESC(BCM, AMEMC, MEMC, "Denali DDR1/DDR2 Memory Controller"), 146 #undef BHND_CDESC 147 148 /* Derived from inspection of the BCM4331 cores that provide PrimeCell 149 * IDs. Due to lack of documentation, the surmised device name/purpose 150 * provided here may be incorrect. */ 151 { BHND_MFGID_ARM, BHND_PRIMEID_EROM, BHND_DEVCLASS_OTHER, 152 "PL364 Device Enumeration ROM" }, 153 { BHND_MFGID_ARM, BHND_PRIMEID_SWRAP, BHND_DEVCLASS_OTHER, 154 "PL368 Device Management Interface" }, 155 { BHND_MFGID_ARM, BHND_PRIMEID_MWRAP, BHND_DEVCLASS_OTHER, 156 "PL369 Device Management Interface" }, 157 158 { 0, 0, 0, NULL } 159 }; 160 161 /** 162 * Return the name for a given JEP106 manufacturer ID. 163 * 164 * @param vendor A JEP106 Manufacturer ID, including the non-standard ARM 4-bit 165 * JEP106 continuation code. 166 */ 167 const char * 168 bhnd_vendor_name(uint16_t vendor) 169 { 170 switch (vendor) { 171 case BHND_MFGID_ARM: 172 return "ARM"; 173 case BHND_MFGID_BCM: 174 return "Broadcom"; 175 case BHND_MFGID_MIPS: 176 return "MIPS"; 177 default: 178 return "unknown"; 179 } 180 } 181 182 /** 183 * Return the name of a port type. 184 */ 185 const char * 186 bhnd_port_type_name(bhnd_port_type port_type) 187 { 188 switch (port_type) { 189 case BHND_PORT_DEVICE: 190 return ("device"); 191 case BHND_PORT_BRIDGE: 192 return ("bridge"); 193 case BHND_PORT_AGENT: 194 return ("agent"); 195 default: 196 return "unknown"; 197 } 198 } 199 200 /** 201 * Return the name of an NVRAM source. 202 */ 203 const char * 204 bhnd_nvram_src_name(bhnd_nvram_src nvram_src) 205 { 206 switch (nvram_src) { 207 case BHND_NVRAM_SRC_FLASH: 208 return ("flash"); 209 case BHND_NVRAM_SRC_OTP: 210 return ("OTP"); 211 case BHND_NVRAM_SRC_SPROM: 212 return ("SPROM"); 213 case BHND_NVRAM_SRC_UNKNOWN: 214 return ("none"); 215 default: 216 return ("unknown"); 217 } 218 } 219 220 static const struct bhnd_core_desc * 221 bhnd_find_core_desc(uint16_t vendor, uint16_t device) 222 { 223 for (u_int i = 0; bhnd_core_descs[i].desc != NULL; i++) { 224 if (bhnd_core_descs[i].vendor != vendor) 225 continue; 226 227 if (bhnd_core_descs[i].device != device) 228 continue; 229 230 return (&bhnd_core_descs[i]); 231 } 232 233 return (NULL); 234 } 235 236 /** 237 * Return a human-readable name for a BHND core. 238 * 239 * @param vendor The core designer's JEDEC-106 Manufacturer ID 240 * @param device The core identifier. 241 */ 242 const char * 243 bhnd_find_core_name(uint16_t vendor, uint16_t device) 244 { 245 const struct bhnd_core_desc *desc; 246 247 if ((desc = bhnd_find_core_desc(vendor, device)) == NULL) 248 return ("unknown"); 249 250 return desc->desc; 251 } 252 253 /** 254 * Return the device class for a BHND core. 255 * 256 * @param vendor The core designer's JEDEC-106 Manufacturer ID 257 * @param device The core identifier. 258 */ 259 bhnd_devclass_t 260 bhnd_find_core_class(uint16_t vendor, uint16_t device) 261 { 262 const struct bhnd_core_desc *desc; 263 264 if ((desc = bhnd_find_core_desc(vendor, device)) == NULL) 265 return (BHND_DEVCLASS_OTHER); 266 267 return desc->class; 268 } 269 270 /** 271 * Return a human-readable name for a BHND core. 272 * 273 * @param ci The core's info record. 274 */ 275 const char * 276 bhnd_core_name(const struct bhnd_core_info *ci) 277 { 278 return bhnd_find_core_name(ci->vendor, ci->device); 279 } 280 281 /** 282 * Return the device class for a BHND core. 283 * 284 * @param ci The core's info record. 285 */ 286 bhnd_devclass_t 287 bhnd_core_class(const struct bhnd_core_info *ci) 288 { 289 return bhnd_find_core_class(ci->vendor, ci->device); 290 } 291 292 /** 293 * Write a human readable name representation of the given 294 * BHND_CHIPID_* constant to @p buffer. 295 * 296 * @param buffer Output buffer, or NULL to compute the required size. 297 * @param size Capacity of @p buffer, in bytes. 298 * @param chip_id Chip ID to be formatted. 299 * 300 * @return Returns the required number of bytes on success, or a negative 301 * integer on failure. No more than @p size-1 characters be written, with 302 * the @p size'th set to '\0'. 303 * 304 * @sa BHND_CHIPID_MAX_NAMELEN 305 */ 306 int 307 bhnd_format_chip_id(char *buffer, size_t size, uint16_t chip_id) 308 { 309 /* All hex formatted IDs are within the range of 0x4000-0x9C3F (40000-1) */ 310 if (chip_id >= 0x4000 && chip_id <= 0x9C3F) 311 return (snprintf(buffer, size, "BCM%hX", chip_id)); 312 else 313 return (snprintf(buffer, size, "BCM%hu", chip_id)); 314 } 315 316 /** 317 * Initialize a core info record with data from from a bhnd-attached @p dev. 318 * 319 * @param dev A bhnd device. 320 * @param core The record to be initialized. 321 */ 322 struct bhnd_core_info 323 bhnd_get_core_info(device_t dev) { 324 return (struct bhnd_core_info) { 325 .vendor = bhnd_get_vendor(dev), 326 .device = bhnd_get_device(dev), 327 .hwrev = bhnd_get_hwrev(dev), 328 .core_idx = bhnd_get_core_index(dev), 329 .unit = bhnd_get_core_unit(dev) 330 }; 331 } 332 333 /** 334 * Find a @p class child device with @p unit on @p dev. 335 * 336 * @param parent The bhnd-compatible bus to be searched. 337 * @param class The device class to match on. 338 * @param unit The core unit number; specify -1 to return the first match 339 * regardless of unit number. 340 * 341 * @retval device_t if a matching child device is found. 342 * @retval NULL if no matching child device is found. 343 */ 344 device_t 345 bhnd_find_child(device_t dev, bhnd_devclass_t class, int unit) 346 { 347 struct bhnd_core_match md = { 348 BHND_MATCH_CORE_CLASS(class), 349 BHND_MATCH_CORE_UNIT(unit) 350 }; 351 352 if (unit == -1) 353 md.m.match.core_unit = 0; 354 355 return bhnd_match_child(dev, &md); 356 } 357 358 /** 359 * Find the first child device on @p dev that matches @p desc. 360 * 361 * @param parent The bhnd-compatible bus to be searched. 362 * @param desc A match descriptor. 363 * 364 * @retval device_t if a matching child device is found. 365 * @retval NULL if no matching child device is found. 366 */ 367 device_t 368 bhnd_match_child(device_t dev, const struct bhnd_core_match *desc) 369 { 370 device_t *devlistp; 371 device_t match; 372 int devcnt; 373 int error; 374 375 error = device_get_children(dev, &devlistp, &devcnt); 376 if (error != 0) 377 return (NULL); 378 379 match = NULL; 380 for (int i = 0; i < devcnt; i++) { 381 struct bhnd_core_info ci = bhnd_get_core_info(devlistp[i]); 382 383 if (bhnd_core_matches(&ci, desc)) { 384 match = devlistp[i]; 385 goto done; 386 } 387 } 388 389 done: 390 free(devlistp, M_TEMP); 391 return match; 392 } 393 394 /** 395 * Walk up the bhnd device hierarchy to locate the root device 396 * to which the bhndb bridge is attached. 397 * 398 * This can be used from within bhnd host bridge drivers to locate the 399 * actual upstream host device. 400 * 401 * @param dev A bhnd device. 402 * @param bus_class The expected bus (e.g. "pci") to which the bridge root 403 * should be attached. 404 * 405 * @retval device_t if a matching parent device is found. 406 * @retval NULL @p dev is not attached via a bhndb bus 407 * @retval NULL no parent device is attached via @p bus_class. 408 */ 409 device_t 410 bhnd_find_bridge_root(device_t dev, devclass_t bus_class) 411 { 412 devclass_t bhndb_class; 413 device_t parent; 414 415 KASSERT(device_get_devclass(device_get_parent(dev)) == bhnd_devclass, 416 ("%s not a bhnd device", device_get_nameunit(dev))); 417 418 bhndb_class = devclass_find("bhndb"); 419 420 /* Walk the device tree until we hit a bridge */ 421 parent = dev; 422 while ((parent = device_get_parent(parent)) != NULL) { 423 if (device_get_devclass(parent) == bhndb_class) 424 break; 425 } 426 427 /* No bridge? */ 428 if (parent == NULL) 429 return (NULL); 430 431 /* Search for a parent attached to the expected bus class */ 432 while ((parent = device_get_parent(parent)) != NULL) { 433 device_t bus; 434 435 bus = device_get_parent(parent); 436 if (bus != NULL && device_get_devclass(bus) == bus_class) 437 return (parent); 438 } 439 440 /* Not found */ 441 return (NULL); 442 } 443 444 /** 445 * Find the first core in @p cores that matches @p desc. 446 * 447 * @param cores The table to search. 448 * @param num_cores The length of @p cores. 449 * @param desc A match descriptor. 450 * 451 * @retval bhnd_core_info if a matching core is found. 452 * @retval NULL if no matching core is found. 453 */ 454 const struct bhnd_core_info * 455 bhnd_match_core(const struct bhnd_core_info *cores, u_int num_cores, 456 const struct bhnd_core_match *desc) 457 { 458 for (u_int i = 0; i < num_cores; i++) { 459 if (bhnd_core_matches(&cores[i], desc)) 460 return &cores[i]; 461 } 462 463 return (NULL); 464 } 465 466 467 /** 468 * Find the first core in @p cores with the given @p class. 469 * 470 * @param cores The table to search. 471 * @param num_cores The length of @p cores. 472 * @param desc A match descriptor. 473 * 474 * @retval bhnd_core_info if a matching core is found. 475 * @retval NULL if no matching core is found. 476 */ 477 const struct bhnd_core_info * 478 bhnd_find_core(const struct bhnd_core_info *cores, u_int num_cores, 479 bhnd_devclass_t class) 480 { 481 struct bhnd_core_match md = { 482 BHND_MATCH_CORE_CLASS(class) 483 }; 484 485 return bhnd_match_core(cores, num_cores, &md); 486 } 487 488 489 /** 490 * Create an equality match descriptor for @p core. 491 * 492 * @param core The core info to be matched on. 493 * @param desc On return, will be populated with a match descriptor for @p core. 494 */ 495 struct bhnd_core_match 496 bhnd_core_get_match_desc(const struct bhnd_core_info *core) 497 { 498 return ((struct bhnd_core_match) { 499 BHND_MATCH_CORE_VENDOR(core->vendor), 500 BHND_MATCH_CORE_ID(core->device), 501 BHND_MATCH_CORE_REV(HWREV_EQ(core->hwrev)), 502 BHND_MATCH_CORE_CLASS(bhnd_core_class(core)), 503 BHND_MATCH_CORE_IDX(core->core_idx), 504 BHND_MATCH_CORE_UNIT(core->unit) 505 }); 506 } 507 508 509 /** 510 * Return true if the @p lhs is equal to @p rhs 511 * 512 * @param lhs The first bhnd core descriptor to compare. 513 * @param rhs The second bhnd core descriptor to compare. 514 * 515 * @retval true if @p lhs is equal to @p rhs 516 * @retval false if @p lhs is not equal to @p rhs 517 */ 518 bool 519 bhnd_cores_equal(const struct bhnd_core_info *lhs, 520 const struct bhnd_core_info *rhs) 521 { 522 struct bhnd_core_match md; 523 524 /* Use an equality match descriptor to perform the comparison */ 525 md = bhnd_core_get_match_desc(rhs); 526 return (bhnd_core_matches(lhs, &md)); 527 } 528 529 /** 530 * Return true if the @p core matches @p desc. 531 * 532 * @param core A bhnd core descriptor. 533 * @param desc A match descriptor to compare against @p core. 534 * 535 * @retval true if @p core matches @p match 536 * @retval false if @p core does not match @p match. 537 */ 538 bool 539 bhnd_core_matches(const struct bhnd_core_info *core, 540 const struct bhnd_core_match *desc) 541 { 542 if (desc->m.match.core_vendor && desc->core_vendor != core->vendor) 543 return (false); 544 545 if (desc->m.match.core_id && desc->core_id != core->device) 546 return (false); 547 548 if (desc->m.match.core_unit && desc->core_unit != core->unit) 549 return (false); 550 551 if (desc->m.match.core_rev && 552 !bhnd_hwrev_matches(core->hwrev, &desc->core_rev)) 553 return (false); 554 555 if (desc->m.match.core_idx && desc->core_idx != core->core_idx) 556 return (false); 557 558 if (desc->m.match.core_class && 559 desc->core_class != bhnd_core_class(core)) 560 return (false); 561 562 return true; 563 } 564 565 /** 566 * Return true if the @p chip matches @p desc. 567 * 568 * @param chip A bhnd chip identifier. 569 * @param desc A match descriptor to compare against @p chip. 570 * 571 * @retval true if @p chip matches @p match 572 * @retval false if @p chip does not match @p match. 573 */ 574 bool 575 bhnd_chip_matches(const struct bhnd_chipid *chip, 576 const struct bhnd_chip_match *desc) 577 { 578 if (desc->m.match.chip_id && chip->chip_id != desc->chip_id) 579 return (false); 580 581 if (desc->m.match.chip_pkg && chip->chip_pkg != desc->chip_pkg) 582 return (false); 583 584 if (desc->m.match.chip_rev && 585 !bhnd_hwrev_matches(chip->chip_rev, &desc->chip_rev)) 586 return (false); 587 588 return (true); 589 } 590 591 /** 592 * Return true if the @p board matches @p desc. 593 * 594 * @param board The bhnd board info. 595 * @param desc A match descriptor to compare against @p board. 596 * 597 * @retval true if @p chip matches @p match 598 * @retval false if @p chip does not match @p match. 599 */ 600 bool 601 bhnd_board_matches(const struct bhnd_board_info *board, 602 const struct bhnd_board_match *desc) 603 { 604 if (desc->m.match.board_srom_rev && 605 !bhnd_hwrev_matches(board->board_srom_rev, &desc->board_srom_rev)) 606 return (false); 607 608 if (desc->m.match.board_vendor && 609 board->board_vendor != desc->board_vendor) 610 return (false); 611 612 if (desc->m.match.board_type && board->board_type != desc->board_type) 613 return (false); 614 615 if (desc->m.match.board_rev && 616 !bhnd_hwrev_matches(board->board_rev, &desc->board_rev)) 617 return (false); 618 619 return (true); 620 } 621 622 /** 623 * Return true if the @p hwrev matches @p desc. 624 * 625 * @param hwrev A bhnd hardware revision. 626 * @param desc A match descriptor to compare against @p core. 627 * 628 * @retval true if @p hwrev matches @p match 629 * @retval false if @p hwrev does not match @p match. 630 */ 631 bool 632 bhnd_hwrev_matches(uint16_t hwrev, const struct bhnd_hwrev_match *desc) 633 { 634 if (desc->start != BHND_HWREV_INVALID && 635 desc->start > hwrev) 636 return false; 637 638 if (desc->end != BHND_HWREV_INVALID && 639 desc->end < hwrev) 640 return false; 641 642 return true; 643 } 644 645 /** 646 * Return true if the @p dev matches @p desc. 647 * 648 * @param dev A bhnd device. 649 * @param desc A match descriptor to compare against @p dev. 650 * 651 * @retval true if @p dev matches @p match 652 * @retval false if @p dev does not match @p match. 653 */ 654 bool 655 bhnd_device_matches(device_t dev, const struct bhnd_device_match *desc) 656 { 657 struct bhnd_core_info core; 658 const struct bhnd_chipid *chip; 659 struct bhnd_board_info board; 660 device_t parent; 661 int error; 662 663 /* Construct individual match descriptors */ 664 struct bhnd_core_match m_core = { _BHND_CORE_MATCH_COPY(desc) }; 665 struct bhnd_chip_match m_chip = { _BHND_CHIP_MATCH_COPY(desc) }; 666 struct bhnd_board_match m_board = { _BHND_BOARD_MATCH_COPY(desc) }; 667 668 /* Fetch and match core info */ 669 if (m_core.m.match_flags) { 670 /* Only applicable to bhnd-attached cores */ 671 parent = device_get_parent(dev); 672 if (device_get_devclass(parent) != bhnd_devclass) { 673 device_printf(dev, "attempting to match core " 674 "attributes against non-core device\n"); 675 return (false); 676 } 677 678 core = bhnd_get_core_info(dev); 679 if (!bhnd_core_matches(&core, &m_core)) 680 return (false); 681 } 682 683 /* Fetch and match chip info */ 684 if (m_chip.m.match_flags) { 685 chip = bhnd_get_chipid(dev); 686 687 if (!bhnd_chip_matches(chip, &m_chip)) 688 return (false); 689 } 690 691 /* Fetch and match board info. 692 * 693 * This is not available until after NVRAM is up; earlier device 694 * matches should not include board requirements */ 695 if (m_board.m.match_flags) { 696 if ((error = bhnd_read_board_info(dev, &board))) { 697 device_printf(dev, "failed to read required board info " 698 "during device matching: %d\n", error); 699 return (false); 700 } 701 702 if (!bhnd_board_matches(&board, &m_board)) 703 return (false); 704 } 705 706 /* All matched */ 707 return (true); 708 } 709 710 /** 711 * Search @p table for an entry matching @p dev. 712 * 713 * @param dev A bhnd device to match against @p table. 714 * @param table The device table to search. 715 * @param entry_size The @p table entry size, in bytes. 716 * 717 * @retval bhnd_device the first matching device, if any. 718 * @retval NULL if no matching device is found in @p table. 719 */ 720 const struct bhnd_device * 721 bhnd_device_lookup(device_t dev, const struct bhnd_device *table, 722 size_t entry_size) 723 { 724 const struct bhnd_device *entry; 725 device_t hostb, parent; 726 bhnd_attach_type attach_type; 727 uint32_t dflags; 728 729 parent = device_get_parent(dev); 730 hostb = bhnd_find_hostb_device(parent); 731 attach_type = bhnd_get_attach_type(dev); 732 733 for (entry = table; !BHND_DEVICE_IS_END(entry); entry = 734 (const struct bhnd_device *) ((const char *) entry + entry_size)) 735 { 736 /* match core info */ 737 if (!bhnd_device_matches(dev, &entry->core)) 738 continue; 739 740 /* match device flags */ 741 dflags = entry->device_flags; 742 743 /* hostb implies BHND_ATTACH_ADAPTER requirement */ 744 if (dflags & BHND_DF_HOSTB) 745 dflags |= BHND_DF_ADAPTER; 746 747 if (dflags & BHND_DF_ADAPTER) 748 if (attach_type != BHND_ATTACH_ADAPTER) 749 continue; 750 751 if (dflags & BHND_DF_HOSTB) 752 if (dev != hostb) 753 continue; 754 755 if (dflags & BHND_DF_SOC) 756 if (attach_type != BHND_ATTACH_NATIVE) 757 continue; 758 759 /* device found */ 760 return (entry); 761 } 762 763 /* not found */ 764 return (NULL); 765 } 766 767 /** 768 * Scan the device @p table for all quirk flags applicable to @p dev. 769 * 770 * @param dev A bhnd device to match against @p table. 771 * @param table The device table to search. 772 * 773 * @return returns all matching quirk flags. 774 */ 775 uint32_t 776 bhnd_device_quirks(device_t dev, const struct bhnd_device *table, 777 size_t entry_size) 778 { 779 const struct bhnd_device *dent; 780 const struct bhnd_device_quirk *qent, *qtable; 781 uint32_t quirks; 782 783 /* Locate the device entry */ 784 if ((dent = bhnd_device_lookup(dev, table, entry_size)) == NULL) 785 return (0); 786 787 /* Quirks table is optional */ 788 qtable = dent->quirks_table; 789 if (qtable == NULL) 790 return (0); 791 792 /* Collect matching device quirk entries */ 793 quirks = 0; 794 for (qent = qtable; !BHND_DEVICE_QUIRK_IS_END(qent); qent++) { 795 if (bhnd_device_matches(dev, &qent->desc)) 796 quirks |= qent->quirks; 797 } 798 799 return (quirks); 800 } 801 802 803 /** 804 * Allocate bhnd(4) resources defined in @p rs from a parent bus. 805 * 806 * @param dev The device requesting ownership of the resources. 807 * @param rs A standard bus resource specification. This will be updated 808 * with the allocated resource's RIDs. 809 * @param res On success, the allocated bhnd resources. 810 * 811 * @retval 0 success 812 * @retval non-zero if allocation of any non-RF_OPTIONAL resource fails, 813 * all allocated resources will be released and a regular 814 * unix error code will be returned. 815 */ 816 int 817 bhnd_alloc_resources(device_t dev, struct resource_spec *rs, 818 struct bhnd_resource **res) 819 { 820 /* Initialize output array */ 821 for (u_int i = 0; rs[i].type != -1; i++) 822 res[i] = NULL; 823 824 for (u_int i = 0; rs[i].type != -1; i++) { 825 res[i] = bhnd_alloc_resource_any(dev, rs[i].type, &rs[i].rid, 826 rs[i].flags); 827 828 /* Clean up all allocations on failure */ 829 if (res[i] == NULL && !(rs[i].flags & RF_OPTIONAL)) { 830 bhnd_release_resources(dev, rs, res); 831 return (ENXIO); 832 } 833 } 834 835 return (0); 836 } 837 838 /** 839 * Release bhnd(4) resources defined in @p rs from a parent bus. 840 * 841 * @param dev The device that owns the resources. 842 * @param rs A standard bus resource specification previously initialized 843 * by @p bhnd_alloc_resources. 844 * @param res The bhnd resources to be released. 845 */ 846 void 847 bhnd_release_resources(device_t dev, const struct resource_spec *rs, 848 struct bhnd_resource **res) 849 { 850 for (u_int i = 0; rs[i].type != -1; i++) { 851 if (res[i] == NULL) 852 continue; 853 854 bhnd_release_resource(dev, rs[i].type, rs[i].rid, res[i]); 855 res[i] = NULL; 856 } 857 } 858 859 /** 860 * Parse the CHIPC_ID_* fields from the ChipCommon CHIPC_ID 861 * register, returning its bhnd_chipid representation. 862 * 863 * @param idreg The CHIPC_ID register value. 864 * @param enum_addr The enumeration address to include in the result. 865 * 866 * @warning 867 * On early siba(4) devices, the ChipCommon core does not provide 868 * a valid CHIPC_ID_NUMCORE field. On these ChipCommon revisions 869 * (see CHIPC_NCORES_MIN_HWREV()), this function will parse and return 870 * an invalid `ncores` value. 871 */ 872 struct bhnd_chipid 873 bhnd_parse_chipid(uint32_t idreg, bhnd_addr_t enum_addr) 874 { 875 struct bhnd_chipid result; 876 877 /* Fetch the basic chip info */ 878 result.chip_id = CHIPC_GET_BITS(idreg, CHIPC_ID_CHIP); 879 result.chip_pkg = CHIPC_GET_BITS(idreg, CHIPC_ID_PKG); 880 result.chip_rev = CHIPC_GET_BITS(idreg, CHIPC_ID_REV); 881 result.chip_type = CHIPC_GET_BITS(idreg, CHIPC_ID_BUS); 882 result.ncores = CHIPC_GET_BITS(idreg, CHIPC_ID_NUMCORE); 883 884 result.enum_addr = enum_addr; 885 886 return (result); 887 } 888 889 890 /** 891 * Determine the correct core count for a chip identification value that 892 * may contain an invalid core count. 893 * 894 * On some early siba(4) devices (see CHIPC_NCORES_MIN_HWREV()), the ChipCommon 895 * core does not provide a valid CHIPC_ID_NUMCORE field. 896 * 897 * @param cid The chip identification to be queried. 898 * @param chipc_hwrev The hardware revision of the ChipCommon core from which 899 * @p cid was parsed. 900 * @param[out] ncores On success, will be set to the correct core count. 901 * 902 * @retval 0 If the core count is already correct, or was mapped to a 903 * a correct value. 904 * @retval EINVAL If the core count is incorrect, but the chip was not 905 * recognized. 906 */ 907 int 908 bhnd_chipid_fixed_ncores(const struct bhnd_chipid *cid, uint16_t chipc_hwrev, 909 uint8_t *ncores) 910 { 911 /* bcma(4), and most siba(4) devices */ 912 if (CHIPC_NCORES_MIN_HWREV(chipc_hwrev)) { 913 *ncores = cid->ncores; 914 return (0); 915 } 916 917 /* broken siba(4) chipsets */ 918 switch (cid->chip_id) { 919 case BHND_CHIPID_BCM4306: 920 *ncores = 6; 921 break; 922 case BHND_CHIPID_BCM4704: 923 *ncores = 9; 924 break; 925 case BHND_CHIPID_BCM5365: 926 /* 927 * BCM5365 does support ID_NUMCORE in at least 928 * some of its revisions, but for unknown 929 * reasons, Broadcom's drivers always exclude 930 * the ChipCommon revision (0x5) used by BCM5365 931 * from the set of revisions supporting 932 * ID_NUMCORE, and instead supply a fixed value. 933 * 934 * Presumably, at least some of these devices 935 * shipped with a broken ID_NUMCORE value. 936 */ 937 *ncores = 7; 938 break; 939 default: 940 return (EINVAL); 941 } 942 943 return (0); 944 } 945 946 /** 947 * Allocate the resource defined by @p rs via @p dev, use it 948 * to read the ChipCommon ID register relative to @p chipc_offset, 949 * then release the resource. 950 * 951 * @param dev The device owning @p rs. 952 * @param rs A resource spec that encompasses the ChipCommon register block. 953 * @param chipc_offset The offset of the ChipCommon registers within @p rs. 954 * @param[out] result the chip identification data. 955 * 956 * @retval 0 success 957 * @retval non-zero if the ChipCommon identification data could not be read. 958 */ 959 int 960 bhnd_read_chipid(device_t dev, struct resource_spec *rs, 961 bus_size_t chipc_offset, struct bhnd_chipid *result) 962 { 963 struct resource *res; 964 bhnd_addr_t enum_addr; 965 uint32_t reg; 966 uint8_t chip_type; 967 int error, rid, rtype; 968 969 rid = rs->rid; 970 rtype = rs->type; 971 error = 0; 972 973 /* Allocate the ChipCommon window resource and fetch the chipid data */ 974 res = bus_alloc_resource_any(dev, rtype, &rid, RF_ACTIVE); 975 if (res == NULL) { 976 device_printf(dev, 977 "failed to allocate bhnd chipc resource\n"); 978 return (ENXIO); 979 } 980 981 /* Fetch the basic chip info */ 982 reg = bus_read_4(res, chipc_offset + CHIPC_ID); 983 chip_type = CHIPC_GET_BITS(reg, CHIPC_ID_BUS); 984 985 /* Fetch the EROMPTR */ 986 if (BHND_CHIPTYPE_HAS_EROM(chip_type)) { 987 enum_addr = bus_read_4(res, chipc_offset + CHIPC_EROMPTR); 988 } else if (chip_type == BHND_CHIPTYPE_SIBA) { 989 /* siba(4) uses the ChipCommon base address as the enumeration 990 * address */ 991 enum_addr = BHND_DEFAULT_CHIPC_ADDR; 992 } else { 993 device_printf(dev, "unknown chip type %hhu\n", chip_type); 994 error = ENODEV; 995 goto cleanup; 996 } 997 998 *result = bhnd_parse_chipid(reg, enum_addr); 999 1000 /* Fix the core count on early siba(4) devices */ 1001 if (chip_type == BHND_CHIPTYPE_SIBA) { 1002 uint32_t idh; 1003 uint16_t chipc_hwrev; 1004 1005 /* 1006 * We need the ChipCommon revision to determine whether 1007 * the ncore field is valid. 1008 * 1009 * We can safely assume the siba IDHIGH register is mapped 1010 * within the chipc register block. 1011 */ 1012 idh = bus_read_4(res, SB0_REG_ABS(SIBA_CFG0_IDHIGH)); 1013 chipc_hwrev = SIBA_IDH_CORE_REV(idh); 1014 1015 error = bhnd_chipid_fixed_ncores(result, chipc_hwrev, 1016 &result->ncores); 1017 if (error) 1018 goto cleanup; 1019 } 1020 1021 cleanup: 1022 /* Clean up */ 1023 bus_release_resource(dev, rtype, rid, res); 1024 return (error); 1025 } 1026 1027 /** 1028 * Read an NVRAM variable's NUL-terminated string value. 1029 * 1030 * @param dev A bhnd bus child device. 1031 * @param name The NVRAM variable name. 1032 * @param[out] buf A buffer large enough to hold @p len bytes. On 1033 * success, the NUL-terminated string value will be 1034 * written to this buffer. This argment may be NULL if 1035 * the value is not desired. 1036 * @param len The maximum capacity of @p buf. 1037 * @param[out] rlen On success, will be set to the actual size of 1038 * the requested value (including NUL termination). This 1039 * argment may be NULL if the size is not desired. 1040 * 1041 * @retval 0 success 1042 * @retval ENOENT The requested variable was not found. 1043 * @retval ENODEV No valid NVRAM source could be found. 1044 * @retval ENOMEM If @p buf is non-NULL and a buffer of @p len is too 1045 * small to hold the requested value. 1046 * @retval EFTYPE If the variable data cannot be coerced to a valid 1047 * string representation. 1048 * @retval ERANGE If value coercion would overflow @p type. 1049 * @retval non-zero If reading @p name otherwise fails, a regular unix 1050 * error code will be returned. 1051 */ 1052 int 1053 bhnd_nvram_getvar_str(device_t dev, const char *name, char *buf, size_t len, 1054 size_t *rlen) 1055 { 1056 size_t larg; 1057 int error; 1058 1059 larg = len; 1060 error = bhnd_nvram_getvar(dev, name, buf, &larg, BHND_NVRAM_TYPE_CSTR); 1061 if (rlen != NULL) 1062 *rlen = larg; 1063 1064 return (error); 1065 } 1066 1067 /** 1068 * Read an NVRAM variable's unsigned integer value. 1069 * 1070 * @param dev A bhnd bus child device. 1071 * @param name The NVRAM variable name. 1072 * @param[out] value On success, the requested value will be written 1073 * to this pointer. 1074 * @param width The output integer type width (1, 2, or 1075 * 4 bytes). 1076 * 1077 * @retval 0 success 1078 * @retval ENOENT The requested variable was not found. 1079 * @retval ENODEV No valid NVRAM source could be found. 1080 * @retval EFTYPE If the variable data cannot be coerced to a 1081 * a valid unsigned integer representation. 1082 * @retval ERANGE If value coercion would overflow (or underflow) an 1083 * unsigned representation of the given @p width. 1084 * @retval non-zero If reading @p name otherwise fails, a regular unix 1085 * error code will be returned. 1086 */ 1087 int 1088 bhnd_nvram_getvar_uint(device_t dev, const char *name, void *value, int width) 1089 { 1090 bhnd_nvram_type type; 1091 size_t len; 1092 1093 switch (width) { 1094 case 1: 1095 type = BHND_NVRAM_TYPE_UINT8; 1096 break; 1097 case 2: 1098 type = BHND_NVRAM_TYPE_UINT16; 1099 break; 1100 case 4: 1101 type = BHND_NVRAM_TYPE_UINT32; 1102 break; 1103 default: 1104 device_printf(dev, "unsupported NVRAM integer width: %d\n", 1105 width); 1106 return (EINVAL); 1107 } 1108 1109 len = width; 1110 return (bhnd_nvram_getvar(dev, name, value, &len, type)); 1111 } 1112 1113 /** 1114 * Read an NVRAM variable's unsigned 8-bit integer value. 1115 * 1116 * @param dev A bhnd bus child device. 1117 * @param name The NVRAM variable name. 1118 * @param[out] value On success, the requested value will be written 1119 * to this pointer. 1120 * 1121 * @retval 0 success 1122 * @retval ENOENT The requested variable was not found. 1123 * @retval ENODEV No valid NVRAM source could be found. 1124 * @retval EFTYPE If the variable data cannot be coerced to a 1125 * a valid unsigned integer representation. 1126 * @retval ERANGE If value coercion would overflow (or underflow) uint8_t. 1127 * @retval non-zero If reading @p name otherwise fails, a regular unix 1128 * error code will be returned. 1129 */ 1130 int 1131 bhnd_nvram_getvar_uint8(device_t dev, const char *name, uint8_t *value) 1132 { 1133 return (bhnd_nvram_getvar_uint(dev, name, value, sizeof(*value))); 1134 } 1135 1136 /** 1137 * Read an NVRAM variable's unsigned 16-bit integer value. 1138 * 1139 * @param dev A bhnd bus child device. 1140 * @param name The NVRAM variable name. 1141 * @param[out] value On success, the requested value will be written 1142 * to this pointer. 1143 * 1144 * @retval 0 success 1145 * @retval ENOENT The requested variable was not found. 1146 * @retval ENODEV No valid NVRAM source could be found. 1147 * @retval EFTYPE If the variable data cannot be coerced to a 1148 * a valid unsigned integer representation. 1149 * @retval ERANGE If value coercion would overflow (or underflow) 1150 * uint16_t. 1151 * @retval non-zero If reading @p name otherwise fails, a regular unix 1152 * error code will be returned. 1153 */ 1154 int 1155 bhnd_nvram_getvar_uint16(device_t dev, const char *name, uint16_t *value) 1156 { 1157 return (bhnd_nvram_getvar_uint(dev, name, value, sizeof(*value))); 1158 } 1159 1160 /** 1161 * Read an NVRAM variable's unsigned 32-bit integer value. 1162 * 1163 * @param dev A bhnd bus child device. 1164 * @param name The NVRAM variable name. 1165 * @param[out] value On success, the requested value will be written 1166 * to this pointer. 1167 * 1168 * @retval 0 success 1169 * @retval ENOENT The requested variable was not found. 1170 * @retval ENODEV No valid NVRAM source could be found. 1171 * @retval EFTYPE If the variable data cannot be coerced to a 1172 * a valid unsigned integer representation. 1173 * @retval ERANGE If value coercion would overflow (or underflow) 1174 * uint32_t. 1175 * @retval non-zero If reading @p name otherwise fails, a regular unix 1176 * error code will be returned. 1177 */ 1178 int 1179 bhnd_nvram_getvar_uint32(device_t dev, const char *name, uint32_t *value) 1180 { 1181 return (bhnd_nvram_getvar_uint(dev, name, value, sizeof(*value))); 1182 } 1183 1184 /** 1185 * Read an NVRAM variable's signed integer value. 1186 * 1187 * @param dev A bhnd bus child device. 1188 * @param name The NVRAM variable name. 1189 * @param[out] value On success, the requested value will be written 1190 * to this pointer. 1191 * @param width The output integer type width (1, 2, or 1192 * 4 bytes). 1193 * 1194 * @retval 0 success 1195 * @retval ENOENT The requested variable was not found. 1196 * @retval ENODEV No valid NVRAM source could be found. 1197 * @retval EFTYPE If the variable data cannot be coerced to a 1198 * a valid integer representation. 1199 * @retval ERANGE If value coercion would overflow (or underflow) an 1200 * signed representation of the given @p width. 1201 * @retval non-zero If reading @p name otherwise fails, a regular unix 1202 * error code will be returned. 1203 */ 1204 int 1205 bhnd_nvram_getvar_int(device_t dev, const char *name, void *value, int width) 1206 { 1207 bhnd_nvram_type type; 1208 size_t len; 1209 1210 switch (width) { 1211 case 1: 1212 type = BHND_NVRAM_TYPE_INT8; 1213 break; 1214 case 2: 1215 type = BHND_NVRAM_TYPE_INT16; 1216 break; 1217 case 4: 1218 type = BHND_NVRAM_TYPE_INT32; 1219 break; 1220 default: 1221 device_printf(dev, "unsupported NVRAM integer width: %d\n", 1222 width); 1223 return (EINVAL); 1224 } 1225 1226 len = width; 1227 return (bhnd_nvram_getvar(dev, name, value, &len, type)); 1228 } 1229 1230 /** 1231 * Read an NVRAM variable's signed 8-bit integer value. 1232 * 1233 * @param dev A bhnd bus child device. 1234 * @param name The NVRAM variable name. 1235 * @param[out] value On success, the requested value will be written 1236 * to this pointer. 1237 * 1238 * @retval 0 success 1239 * @retval ENOENT The requested variable was not found. 1240 * @retval ENODEV No valid NVRAM source could be found. 1241 * @retval EFTYPE If the variable data cannot be coerced to a 1242 * a valid integer representation. 1243 * @retval ERANGE If value coercion would overflow (or underflow) int8_t. 1244 * @retval non-zero If reading @p name otherwise fails, a regular unix 1245 * error code will be returned. 1246 */ 1247 int 1248 bhnd_nvram_getvar_int8(device_t dev, const char *name, int8_t *value) 1249 { 1250 return (bhnd_nvram_getvar_int(dev, name, value, sizeof(*value))); 1251 } 1252 1253 /** 1254 * Read an NVRAM variable's signed 16-bit integer value. 1255 * 1256 * @param dev A bhnd bus child device. 1257 * @param name The NVRAM variable name. 1258 * @param[out] value On success, the requested value will be written 1259 * to this pointer. 1260 * 1261 * @retval 0 success 1262 * @retval ENOENT The requested variable was not found. 1263 * @retval ENODEV No valid NVRAM source could be found. 1264 * @retval EFTYPE If the variable data cannot be coerced to a 1265 * a valid integer representation. 1266 * @retval ERANGE If value coercion would overflow (or underflow) 1267 * int16_t. 1268 * @retval non-zero If reading @p name otherwise fails, a regular unix 1269 * error code will be returned. 1270 */ 1271 int 1272 bhnd_nvram_getvar_int16(device_t dev, const char *name, int16_t *value) 1273 { 1274 return (bhnd_nvram_getvar_int(dev, name, value, sizeof(*value))); 1275 } 1276 1277 /** 1278 * Read an NVRAM variable's signed 32-bit integer value. 1279 * 1280 * @param dev A bhnd bus child device. 1281 * @param name The NVRAM variable name. 1282 * @param[out] value On success, the requested value will be written 1283 * to this pointer. 1284 * 1285 * @retval 0 success 1286 * @retval ENOENT The requested variable was not found. 1287 * @retval ENODEV No valid NVRAM source could be found. 1288 * @retval EFTYPE If the variable data cannot be coerced to a 1289 * a valid integer representation. 1290 * @retval ERANGE If value coercion would overflow (or underflow) 1291 * int32_t. 1292 * @retval non-zero If reading @p name otherwise fails, a regular unix 1293 * error code will be returned. 1294 */ 1295 int 1296 bhnd_nvram_getvar_int32(device_t dev, const char *name, int32_t *value) 1297 { 1298 return (bhnd_nvram_getvar_int(dev, name, value, sizeof(*value))); 1299 } 1300 1301 1302 /** 1303 * Read an NVRAM variable's array value. 1304 * 1305 * @param dev A bhnd bus child device. 1306 * @param name The NVRAM variable name. 1307 * @param[out] buf A buffer large enough to hold @p size bytes. 1308 * On success, the requested value will be written 1309 * to this buffer. 1310 * @param[in,out] size The required number of bytes to write to 1311 * @p buf. 1312 * @param type The desired array element data representation. 1313 * 1314 * @retval 0 success 1315 * @retval ENOENT The requested variable was not found. 1316 * @retval ENODEV No valid NVRAM source could be found. 1317 * @retval ENXIO If less than @p size bytes are available. 1318 * @retval ENOMEM If a buffer of @p size is too small to hold the 1319 * requested value. 1320 * @retval EFTYPE If the variable data cannot be coerced to a 1321 * a valid instance of @p type. 1322 * @retval ERANGE If value coercion would overflow (or underflow) a 1323 * representation of @p type. 1324 * @retval non-zero If reading @p name otherwise fails, a regular unix 1325 * error code will be returned. 1326 */ 1327 int 1328 bhnd_nvram_getvar_array(device_t dev, const char *name, void *buf, size_t size, 1329 bhnd_nvram_type type) 1330 { 1331 size_t nbytes; 1332 int error; 1333 1334 /* Attempt read */ 1335 nbytes = size; 1336 if ((error = bhnd_nvram_getvar(dev, name, buf, &nbytes, type))) 1337 return (error); 1338 1339 /* Verify that the expected number of bytes were fetched */ 1340 if (nbytes < size) 1341 return (ENXIO); 1342 1343 return (0); 1344 } 1345 1346 /** 1347 * Using the bhnd(4) bus-level core information and a custom core name, 1348 * populate @p dev's device description. 1349 * 1350 * @param dev A bhnd-bus attached device. 1351 * @param dev_name The core's name (e.g. "SDIO Device Core") 1352 */ 1353 void 1354 bhnd_set_custom_core_desc(device_t dev, const char *dev_name) 1355 { 1356 const char *vendor_name; 1357 char *desc; 1358 1359 vendor_name = bhnd_get_vendor_name(dev); 1360 asprintf(&desc, M_BHND, "%s %s, rev %hhu", vendor_name, dev_name, 1361 bhnd_get_hwrev(dev)); 1362 1363 if (desc != NULL) { 1364 device_set_desc_copy(dev, desc); 1365 free(desc, M_BHND); 1366 } else { 1367 device_set_desc(dev, dev_name); 1368 } 1369 } 1370 1371 /** 1372 * Using the bhnd(4) bus-level core information, populate @p dev's device 1373 * description. 1374 * 1375 * @param dev A bhnd-bus attached device. 1376 */ 1377 void 1378 bhnd_set_default_core_desc(device_t dev) 1379 { 1380 bhnd_set_custom_core_desc(dev, bhnd_get_device_name(dev)); 1381 } 1382 1383 1384 /** 1385 * Using the bhnd @p chip_id, populate the bhnd(4) bus @p dev's device 1386 * description. 1387 * 1388 * @param dev A bhnd-bus attached device. 1389 */ 1390 void 1391 bhnd_set_default_bus_desc(device_t dev, const struct bhnd_chipid *chip_id) 1392 { 1393 const char *bus_name; 1394 char *desc; 1395 char chip_name[BHND_CHIPID_MAX_NAMELEN]; 1396 1397 /* Determine chip type's bus name */ 1398 switch (chip_id->chip_type) { 1399 case BHND_CHIPTYPE_SIBA: 1400 bus_name = "SIBA bus"; 1401 break; 1402 case BHND_CHIPTYPE_BCMA: 1403 case BHND_CHIPTYPE_BCMA_ALT: 1404 bus_name = "BCMA bus"; 1405 break; 1406 case BHND_CHIPTYPE_UBUS: 1407 bus_name = "UBUS bus"; 1408 break; 1409 default: 1410 bus_name = "Unknown Type"; 1411 break; 1412 } 1413 1414 /* Format chip name */ 1415 bhnd_format_chip_id(chip_name, sizeof(chip_name), 1416 chip_id->chip_id); 1417 1418 /* Format and set device description */ 1419 asprintf(&desc, M_BHND, "%s %s", chip_name, bus_name); 1420 if (desc != NULL) { 1421 device_set_desc_copy(dev, desc); 1422 free(desc, M_BHND); 1423 } else { 1424 device_set_desc(dev, bus_name); 1425 } 1426 1427 } 1428 1429 /** 1430 * Helper function for implementing BHND_BUS_IS_HW_DISABLED(). 1431 * 1432 * If a parent device is available, this implementation delegates the 1433 * request to the BHND_BUS_IS_HW_DISABLED() method on the parent of @p dev. 1434 * 1435 * If no parent device is available (i.e. on a the bus root), the hardware 1436 * is assumed to be usable and false is returned. 1437 */ 1438 bool 1439 bhnd_bus_generic_is_hw_disabled(device_t dev, device_t child) 1440 { 1441 if (device_get_parent(dev) != NULL) 1442 return (BHND_BUS_IS_HW_DISABLED(device_get_parent(dev), child)); 1443 1444 return (false); 1445 } 1446 1447 /** 1448 * Helper function for implementing BHND_BUS_GET_CHIPID(). 1449 * 1450 * This implementation delegates the request to the BHND_BUS_GET_CHIPID() 1451 * method on the parent of @p dev. If no parent exists, the implementation 1452 * will panic. 1453 */ 1454 const struct bhnd_chipid * 1455 bhnd_bus_generic_get_chipid(device_t dev, device_t child) 1456 { 1457 if (device_get_parent(dev) != NULL) 1458 return (BHND_BUS_GET_CHIPID(device_get_parent(dev), child)); 1459 1460 panic("missing BHND_BUS_GET_CHIPID()"); 1461 } 1462 1463 /* nvram board_info population macros for bhnd_bus_generic_read_board_info() */ 1464 #define BHND_GV(_dest, _name) \ 1465 bhnd_nvram_getvar_uint(child, BHND_NVAR_ ## _name, &_dest, \ 1466 sizeof(_dest)) 1467 1468 #define REQ_BHND_GV(_dest, _name) do { \ 1469 if ((error = BHND_GV(_dest, _name))) { \ 1470 device_printf(dev, \ 1471 "error reading " __STRING(_name) ": %d\n", error); \ 1472 return (error); \ 1473 } \ 1474 } while(0) 1475 1476 #define OPT_BHND_GV(_dest, _name, _default) do { \ 1477 if ((error = BHND_GV(_dest, _name))) { \ 1478 if (error != ENOENT) { \ 1479 device_printf(dev, \ 1480 "error reading " \ 1481 __STRING(_name) ": %d\n", error); \ 1482 return (error); \ 1483 } \ 1484 _dest = _default; \ 1485 } \ 1486 } while(0) 1487 1488 /** 1489 * Helper function for implementing BHND_BUS_READ_BOARDINFO(). 1490 * 1491 * This implementation populates @p info with information from NVRAM, 1492 * defaulting board_vendor and board_type fields to 0 if the 1493 * requested variables cannot be found. 1494 * 1495 * This behavior is correct for most SoCs, but must be overridden on 1496 * bridged (PCI, PCMCIA, etc) devices to produce a complete bhnd_board_info 1497 * result. 1498 */ 1499 int 1500 bhnd_bus_generic_read_board_info(device_t dev, device_t child, 1501 struct bhnd_board_info *info) 1502 { 1503 int error; 1504 1505 OPT_BHND_GV(info->board_vendor, BOARDVENDOR, 0); 1506 OPT_BHND_GV(info->board_type, BOARDTYPE, 0); /* srom >= 2 */ 1507 REQ_BHND_GV(info->board_rev, BOARDREV); 1508 OPT_BHND_GV(info->board_srom_rev,SROMREV, 0); /* missing in 1509 some SoC 1510 NVRAM */ 1511 REQ_BHND_GV(info->board_flags, BOARDFLAGS); 1512 OPT_BHND_GV(info->board_flags2, BOARDFLAGS2, 0); /* srom >= 4 */ 1513 OPT_BHND_GV(info->board_flags3, BOARDFLAGS3, 0); /* srom >= 11 */ 1514 1515 return (0); 1516 } 1517 1518 #undef BHND_GV 1519 #undef BHND_GV_REQ 1520 #undef BHND_GV_OPT 1521 1522 /** 1523 * Helper function for implementing BHND_BUS_GET_NVRAM_VAR(). 1524 * 1525 * This implementation searches @p dev for a usable NVRAM child device. 1526 * 1527 * If no usable child device is found on @p dev, the request is delegated to 1528 * the BHND_BUS_GET_NVRAM_VAR() method on the parent of @p dev. 1529 */ 1530 int 1531 bhnd_bus_generic_get_nvram_var(device_t dev, device_t child, const char *name, 1532 void *buf, size_t *size, bhnd_nvram_type type) 1533 { 1534 device_t nvram; 1535 device_t parent; 1536 1537 /* Make sure we're holding Giant for newbus */ 1538 GIANT_REQUIRED; 1539 1540 /* Look for a directly-attached NVRAM child */ 1541 if ((nvram = device_find_child(dev, "bhnd_nvram", -1)) != NULL) 1542 return BHND_NVRAM_GETVAR(nvram, name, buf, size, type); 1543 1544 /* Try to delegate to parent */ 1545 if ((parent = device_get_parent(dev)) == NULL) 1546 return (ENODEV); 1547 1548 return (BHND_BUS_GET_NVRAM_VAR(device_get_parent(dev), child, 1549 name, buf, size, type)); 1550 } 1551 1552 /** 1553 * Helper function for implementing BHND_BUS_ALLOC_RESOURCE(). 1554 * 1555 * This implementation of BHND_BUS_ALLOC_RESOURCE() delegates allocation 1556 * of the underlying resource to BUS_ALLOC_RESOURCE(), and activation 1557 * to @p dev's BHND_BUS_ACTIVATE_RESOURCE(). 1558 */ 1559 struct bhnd_resource * 1560 bhnd_bus_generic_alloc_resource(device_t dev, device_t child, int type, 1561 int *rid, rman_res_t start, rman_res_t end, rman_res_t count, 1562 u_int flags) 1563 { 1564 struct bhnd_resource *br; 1565 struct resource *res; 1566 int error; 1567 1568 br = NULL; 1569 res = NULL; 1570 1571 /* Allocate the real bus resource (without activating it) */ 1572 res = BUS_ALLOC_RESOURCE(dev, child, type, rid, start, end, count, 1573 (flags & ~RF_ACTIVE)); 1574 if (res == NULL) 1575 return (NULL); 1576 1577 /* Allocate our bhnd resource wrapper. */ 1578 br = malloc(sizeof(struct bhnd_resource), M_BHND, M_NOWAIT); 1579 if (br == NULL) 1580 goto failed; 1581 1582 br->direct = false; 1583 br->res = res; 1584 1585 /* Attempt activation */ 1586 if (flags & RF_ACTIVE) { 1587 error = BHND_BUS_ACTIVATE_RESOURCE(dev, child, type, *rid, br); 1588 if (error) 1589 goto failed; 1590 } 1591 1592 return (br); 1593 1594 failed: 1595 if (res != NULL) 1596 BUS_RELEASE_RESOURCE(dev, child, type, *rid, res); 1597 1598 free(br, M_BHND); 1599 return (NULL); 1600 } 1601 1602 /** 1603 * Helper function for implementing BHND_BUS_RELEASE_RESOURCE(). 1604 * 1605 * This implementation of BHND_BUS_RELEASE_RESOURCE() delegates release of 1606 * the backing resource to BUS_RELEASE_RESOURCE(). 1607 */ 1608 int 1609 bhnd_bus_generic_release_resource(device_t dev, device_t child, int type, 1610 int rid, struct bhnd_resource *r) 1611 { 1612 int error; 1613 1614 if ((error = BUS_RELEASE_RESOURCE(dev, child, type, rid, r->res))) 1615 return (error); 1616 1617 free(r, M_BHND); 1618 return (0); 1619 } 1620 1621 1622 /** 1623 * Helper function for implementing BHND_BUS_ACTIVATE_RESOURCE(). 1624 * 1625 * This implementation of BHND_BUS_ACTIVATE_RESOURCE() first calls the 1626 * BHND_BUS_ACTIVATE_RESOURCE() method of the parent of @p dev. 1627 * 1628 * If this fails, and if @p dev is the direct parent of @p child, standard 1629 * resource activation is attempted via bus_activate_resource(). This enables 1630 * direct use of the bhnd(4) resource APIs on devices that may not be attached 1631 * to a parent bhnd bus or bridge. 1632 */ 1633 int 1634 bhnd_bus_generic_activate_resource(device_t dev, device_t child, int type, 1635 int rid, struct bhnd_resource *r) 1636 { 1637 int error; 1638 bool passthrough; 1639 1640 passthrough = (device_get_parent(child) != dev); 1641 1642 /* Try to delegate to the parent */ 1643 if (device_get_parent(dev) != NULL) { 1644 error = BHND_BUS_ACTIVATE_RESOURCE(device_get_parent(dev), 1645 child, type, rid, r); 1646 } else { 1647 error = ENODEV; 1648 } 1649 1650 /* If bhnd(4) activation has failed and we're the child's direct 1651 * parent, try falling back on standard resource activation. 1652 */ 1653 if (error && !passthrough) { 1654 error = bus_activate_resource(child, type, rid, r->res); 1655 if (!error) 1656 r->direct = true; 1657 } 1658 1659 return (error); 1660 } 1661 1662 /** 1663 * Helper function for implementing BHND_BUS_DEACTIVATE_RESOURCE(). 1664 * 1665 * This implementation of BHND_BUS_ACTIVATE_RESOURCE() simply calls the 1666 * BHND_BUS_ACTIVATE_RESOURCE() method of the parent of @p dev. 1667 */ 1668 int 1669 bhnd_bus_generic_deactivate_resource(device_t dev, device_t child, 1670 int type, int rid, struct bhnd_resource *r) 1671 { 1672 if (device_get_parent(dev) != NULL) 1673 return (BHND_BUS_DEACTIVATE_RESOURCE(device_get_parent(dev), 1674 child, type, rid, r)); 1675 1676 return (EINVAL); 1677 } 1678 1679