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, OTHER, "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, OTHER, "USB 1.1 Host Controller"), 90 BHND_CDESC(BCM, USB11D, OTHER, "USB 1.1 Device Core"), 91 BHND_CDESC(BCM, USB20H, OTHER, "USB 2.0 Host Controller"), 92 BHND_CDESC(BCM, USB20D, OTHER, "USB 2.0 Device Core"), 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, OTHER, "USB 2.0 Host Controller"), 134 BHND_CDESC(BCM, NS_USB30H, OTHER, "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 * Return true if the @p core matches @p desc. 490 * 491 * @param core A bhnd core descriptor. 492 * @param desc A match descriptor to compare against @p core. 493 * 494 * @retval true if @p core matches @p match 495 * @retval false if @p core does not match @p match. 496 */ 497 bool 498 bhnd_core_matches(const struct bhnd_core_info *core, 499 const struct bhnd_core_match *desc) 500 { 501 if (desc->m.match.core_vendor && desc->core_vendor != core->vendor) 502 return (false); 503 504 if (desc->m.match.core_id && desc->core_id != core->device) 505 return (false); 506 507 if (desc->m.match.core_unit && desc->core_unit != core->unit) 508 return (false); 509 510 if (desc->m.match.core_rev && 511 !bhnd_hwrev_matches(core->hwrev, &desc->core_rev)) 512 return (false); 513 514 if (desc->m.match.core_class && 515 desc->core_class != bhnd_core_class(core)) 516 return (false); 517 518 return true; 519 } 520 521 /** 522 * Return true if the @p chip matches @p desc. 523 * 524 * @param chip A bhnd chip identifier. 525 * @param desc A match descriptor to compare against @p chip. 526 * 527 * @retval true if @p chip matches @p match 528 * @retval false if @p chip does not match @p match. 529 */ 530 bool 531 bhnd_chip_matches(const struct bhnd_chipid *chip, 532 const struct bhnd_chip_match *desc) 533 { 534 if (desc->m.match.chip_id && chip->chip_id != desc->chip_id) 535 return (false); 536 537 if (desc->m.match.chip_pkg && chip->chip_pkg != desc->chip_pkg) 538 return (false); 539 540 if (desc->m.match.chip_rev && 541 !bhnd_hwrev_matches(chip->chip_rev, &desc->chip_rev)) 542 return (false); 543 544 return (true); 545 } 546 547 /** 548 * Return true if the @p board matches @p desc. 549 * 550 * @param board The bhnd board info. 551 * @param desc A match descriptor to compare against @p board. 552 * 553 * @retval true if @p chip matches @p match 554 * @retval false if @p chip does not match @p match. 555 */ 556 bool 557 bhnd_board_matches(const struct bhnd_board_info *board, 558 const struct bhnd_board_match *desc) 559 { 560 if (desc->m.match.board_srom_rev && 561 !bhnd_hwrev_matches(board->board_srom_rev, &desc->board_srom_rev)) 562 return (false); 563 564 if (desc->m.match.board_vendor && 565 board->board_vendor != desc->board_vendor) 566 return (false); 567 568 if (desc->m.match.board_type && board->board_type != desc->board_type) 569 return (false); 570 571 if (desc->m.match.board_rev && 572 !bhnd_hwrev_matches(board->board_rev, &desc->board_rev)) 573 return (false); 574 575 return (true); 576 } 577 578 /** 579 * Return true if the @p hwrev matches @p desc. 580 * 581 * @param hwrev A bhnd hardware revision. 582 * @param desc A match descriptor to compare against @p core. 583 * 584 * @retval true if @p hwrev matches @p match 585 * @retval false if @p hwrev does not match @p match. 586 */ 587 bool 588 bhnd_hwrev_matches(uint16_t hwrev, const struct bhnd_hwrev_match *desc) 589 { 590 if (desc->start != BHND_HWREV_INVALID && 591 desc->start > hwrev) 592 return false; 593 594 if (desc->end != BHND_HWREV_INVALID && 595 desc->end < hwrev) 596 return false; 597 598 return true; 599 } 600 601 /** 602 * Return true if the @p dev matches @p desc. 603 * 604 * @param dev A bhnd device. 605 * @param desc A match descriptor to compare against @p dev. 606 * 607 * @retval true if @p dev matches @p match 608 * @retval false if @p dev does not match @p match. 609 */ 610 bool 611 bhnd_device_matches(device_t dev, const struct bhnd_device_match *desc) 612 { 613 struct bhnd_core_info core; 614 const struct bhnd_chipid *chip; 615 struct bhnd_board_info board; 616 device_t parent; 617 int error; 618 619 /* Construct individual match descriptors */ 620 struct bhnd_core_match m_core = { _BHND_CORE_MATCH_COPY(desc) }; 621 struct bhnd_chip_match m_chip = { _BHND_CHIP_MATCH_COPY(desc) }; 622 struct bhnd_board_match m_board = { _BHND_BOARD_MATCH_COPY(desc) }; 623 624 /* Fetch and match core info */ 625 if (m_core.m.match_flags) { 626 /* Only applicable to bhnd-attached cores */ 627 parent = device_get_parent(dev); 628 if (device_get_devclass(parent) != bhnd_devclass) { 629 device_printf(dev, "attempting to match core " 630 "attributes against non-core device\n"); 631 return (false); 632 } 633 634 core = bhnd_get_core_info(dev); 635 if (!bhnd_core_matches(&core, &m_core)) 636 return (false); 637 } 638 639 /* Fetch and match chip info */ 640 if (m_chip.m.match_flags) { 641 chip = bhnd_get_chipid(dev); 642 643 if (!bhnd_chip_matches(chip, &m_chip)) 644 return (false); 645 } 646 647 /* Fetch and match board info. 648 * 649 * This is not available until after NVRAM is up; earlier device 650 * matches should not include board requirements */ 651 if (m_board.m.match_flags) { 652 if ((error = bhnd_read_board_info(dev, &board))) { 653 device_printf(dev, "failed to read required board info " 654 "during device matching: %d\n", error); 655 return (false); 656 } 657 658 if (!bhnd_board_matches(&board, &m_board)) 659 return (false); 660 } 661 662 /* All matched */ 663 return (true); 664 } 665 666 /** 667 * Search @p table for an entry matching @p dev. 668 * 669 * @param dev A bhnd device to match against @p table. 670 * @param table The device table to search. 671 * @param entry_size The @p table entry size, in bytes. 672 * 673 * @retval bhnd_device the first matching device, if any. 674 * @retval NULL if no matching device is found in @p table. 675 */ 676 const struct bhnd_device * 677 bhnd_device_lookup(device_t dev, const struct bhnd_device *table, 678 size_t entry_size) 679 { 680 const struct bhnd_device *entry; 681 device_t hostb, parent; 682 bhnd_attach_type attach_type; 683 uint32_t dflags; 684 685 parent = device_get_parent(dev); 686 hostb = bhnd_find_hostb_device(parent); 687 attach_type = bhnd_get_attach_type(dev); 688 689 for (entry = table; !BHND_DEVICE_IS_END(entry); entry = 690 (const struct bhnd_device *) ((const char *) entry + entry_size)) 691 { 692 /* match core info */ 693 if (!bhnd_device_matches(dev, &entry->core)) 694 continue; 695 696 /* match device flags */ 697 dflags = entry->device_flags; 698 699 /* hostb implies BHND_ATTACH_ADAPTER requirement */ 700 if (dflags & BHND_DF_HOSTB) 701 dflags |= BHND_DF_ADAPTER; 702 703 if (dflags & BHND_DF_ADAPTER) 704 if (attach_type != BHND_ATTACH_ADAPTER) 705 continue; 706 707 if (dflags & BHND_DF_HOSTB) 708 if (dev != hostb) 709 continue; 710 711 if (dflags & BHND_DF_SOC) 712 if (attach_type != BHND_ATTACH_NATIVE) 713 continue; 714 715 /* device found */ 716 return (entry); 717 } 718 719 /* not found */ 720 return (NULL); 721 } 722 723 /** 724 * Scan the device @p table for all quirk flags applicable to @p dev. 725 * 726 * @param dev A bhnd device to match against @p table. 727 * @param table The device table to search. 728 * 729 * @return returns all matching quirk flags. 730 */ 731 uint32_t 732 bhnd_device_quirks(device_t dev, const struct bhnd_device *table, 733 size_t entry_size) 734 { 735 const struct bhnd_device *dent; 736 const struct bhnd_device_quirk *qent, *qtable; 737 uint32_t quirks; 738 739 /* Locate the device entry */ 740 if ((dent = bhnd_device_lookup(dev, table, entry_size)) == NULL) 741 return (0); 742 743 /* Quirks table is optional */ 744 qtable = dent->quirks_table; 745 if (qtable == NULL) 746 return (0); 747 748 /* Collect matching device quirk entries */ 749 quirks = 0; 750 for (qent = qtable; !BHND_DEVICE_QUIRK_IS_END(qent); qent++) { 751 if (bhnd_device_matches(dev, &qent->desc)) 752 quirks |= qent->quirks; 753 } 754 755 return (quirks); 756 } 757 758 759 /** 760 * Allocate bhnd(4) resources defined in @p rs from a parent bus. 761 * 762 * @param dev The device requesting ownership of the resources. 763 * @param rs A standard bus resource specification. This will be updated 764 * with the allocated resource's RIDs. 765 * @param res On success, the allocated bhnd resources. 766 * 767 * @retval 0 success 768 * @retval non-zero if allocation of any non-RF_OPTIONAL resource fails, 769 * all allocated resources will be released and a regular 770 * unix error code will be returned. 771 */ 772 int 773 bhnd_alloc_resources(device_t dev, struct resource_spec *rs, 774 struct bhnd_resource **res) 775 { 776 /* Initialize output array */ 777 for (u_int i = 0; rs[i].type != -1; i++) 778 res[i] = NULL; 779 780 for (u_int i = 0; rs[i].type != -1; i++) { 781 res[i] = bhnd_alloc_resource_any(dev, rs[i].type, &rs[i].rid, 782 rs[i].flags); 783 784 /* Clean up all allocations on failure */ 785 if (res[i] == NULL && !(rs[i].flags & RF_OPTIONAL)) { 786 bhnd_release_resources(dev, rs, res); 787 return (ENXIO); 788 } 789 } 790 791 return (0); 792 }; 793 794 /** 795 * Release bhnd(4) resources defined in @p rs from a parent bus. 796 * 797 * @param dev The device that owns the resources. 798 * @param rs A standard bus resource specification previously initialized 799 * by @p bhnd_alloc_resources. 800 * @param res The bhnd resources to be released. 801 */ 802 void 803 bhnd_release_resources(device_t dev, const struct resource_spec *rs, 804 struct bhnd_resource **res) 805 { 806 for (u_int i = 0; rs[i].type != -1; i++) { 807 if (res[i] == NULL) 808 continue; 809 810 bhnd_release_resource(dev, rs[i].type, rs[i].rid, res[i]); 811 res[i] = NULL; 812 } 813 } 814 815 /** 816 * Parse the CHIPC_ID_* fields from the ChipCommon CHIPC_ID 817 * register, returning its bhnd_chipid representation. 818 * 819 * @param idreg The CHIPC_ID register value. 820 * @param enum_addr The enumeration address to include in the result. 821 * 822 * @warning 823 * On early siba(4) devices, the ChipCommon core does not provide 824 * a valid CHIPC_ID_NUMCORE field. On these ChipCommon revisions 825 * (see CHIPC_NCORES_MIN_HWREV()), this function will parse and return 826 * an invalid `ncores` value. 827 */ 828 struct bhnd_chipid 829 bhnd_parse_chipid(uint32_t idreg, bhnd_addr_t enum_addr) 830 { 831 struct bhnd_chipid result; 832 833 /* Fetch the basic chip info */ 834 result.chip_id = CHIPC_GET_BITS(idreg, CHIPC_ID_CHIP); 835 result.chip_pkg = CHIPC_GET_BITS(idreg, CHIPC_ID_PKG); 836 result.chip_rev = CHIPC_GET_BITS(idreg, CHIPC_ID_REV); 837 result.chip_type = CHIPC_GET_BITS(idreg, CHIPC_ID_BUS); 838 result.ncores = CHIPC_GET_BITS(idreg, CHIPC_ID_NUMCORE); 839 840 result.enum_addr = enum_addr; 841 842 return (result); 843 } 844 845 846 /** 847 * Determine the correct core count for a chip identification value that 848 * may contain an invalid core count. 849 * 850 * On some early siba(4) devices (see CHIPC_NCORES_MIN_HWREV()), the ChipCommon 851 * core does not provide a valid CHIPC_ID_NUMCORE field. 852 * 853 * @param cid The chip identification to be queried. 854 * @param chipc_hwrev The hardware revision of the ChipCommon core from which 855 * @p cid was parsed. 856 * @param[out] ncores On success, will be set to the correct core count. 857 * 858 * @retval 0 If the core count is already correct, or was mapped to a 859 * a correct value. 860 * @retval EINVAL If the core count is incorrect, but the chip was not 861 * recognized. 862 */ 863 int 864 bhnd_chipid_fixed_ncores(const struct bhnd_chipid *cid, uint16_t chipc_hwrev, 865 uint8_t *ncores) 866 { 867 /* bcma(4), and most siba(4) devices */ 868 if (CHIPC_NCORES_MIN_HWREV(chipc_hwrev)) { 869 *ncores = cid->ncores; 870 return (0); 871 } 872 873 /* broken siba(4) chipsets */ 874 switch (cid->chip_id) { 875 case BHND_CHIPID_BCM4306: 876 *ncores = 6; 877 break; 878 case BHND_CHIPID_BCM4704: 879 *ncores = 9; 880 break; 881 case BHND_CHIPID_BCM5365: 882 /* 883 * BCM5365 does support ID_NUMCORE in at least 884 * some of its revisions, but for unknown 885 * reasons, Broadcom's drivers always exclude 886 * the ChipCommon revision (0x5) used by BCM5365 887 * from the set of revisions supporting 888 * ID_NUMCORE, and instead supply a fixed value. 889 * 890 * Presumably, at least some of these devices 891 * shipped with a broken ID_NUMCORE value. 892 */ 893 *ncores = 7; 894 break; 895 default: 896 return (EINVAL); 897 } 898 899 return (0); 900 } 901 902 /** 903 * Allocate the resource defined by @p rs via @p dev, use it 904 * to read the ChipCommon ID register relative to @p chipc_offset, 905 * then release the resource. 906 * 907 * @param dev The device owning @p rs. 908 * @param rs A resource spec that encompasses the ChipCommon register block. 909 * @param chipc_offset The offset of the ChipCommon registers within @p rs. 910 * @param[out] result the chip identification data. 911 * 912 * @retval 0 success 913 * @retval non-zero if the ChipCommon identification data could not be read. 914 */ 915 int 916 bhnd_read_chipid(device_t dev, struct resource_spec *rs, 917 bus_size_t chipc_offset, struct bhnd_chipid *result) 918 { 919 struct resource *res; 920 bhnd_addr_t enum_addr; 921 uint32_t reg; 922 uint8_t chip_type; 923 int error, rid, rtype; 924 925 rid = rs->rid; 926 rtype = rs->type; 927 error = 0; 928 929 /* Allocate the ChipCommon window resource and fetch the chipid data */ 930 res = bus_alloc_resource_any(dev, rtype, &rid, RF_ACTIVE); 931 if (res == NULL) { 932 device_printf(dev, 933 "failed to allocate bhnd chipc resource\n"); 934 return (ENXIO); 935 } 936 937 /* Fetch the basic chip info */ 938 reg = bus_read_4(res, chipc_offset + CHIPC_ID); 939 chip_type = CHIPC_GET_BITS(reg, CHIPC_ID_BUS); 940 941 /* Fetch the EROMPTR */ 942 if (BHND_CHIPTYPE_HAS_EROM(chip_type)) { 943 enum_addr = bus_read_4(res, chipc_offset + CHIPC_EROMPTR); 944 } else if (chip_type == BHND_CHIPTYPE_SIBA) { 945 /* siba(4) uses the ChipCommon base address as the enumeration 946 * address */ 947 enum_addr = rman_get_start(res) + chipc_offset; 948 } else { 949 device_printf(dev, "unknown chip type %hhu\n", chip_type); 950 error = ENODEV; 951 goto cleanup; 952 } 953 954 *result = bhnd_parse_chipid(reg, enum_addr); 955 956 /* Fix the core count on early siba(4) devices */ 957 if (chip_type == BHND_CHIPTYPE_SIBA) { 958 uint32_t idh; 959 uint16_t chipc_hwrev; 960 961 /* 962 * We need the ChipCommon revision to determine whether 963 * the ncore field is valid. 964 * 965 * We can safely assume the siba IDHIGH register is mapped 966 * within the chipc register block. 967 */ 968 idh = bus_read_4(res, SB0_REG_ABS(SIBA_CFG0_IDHIGH)); 969 chipc_hwrev = SIBA_IDH_CORE_REV(idh); 970 971 error = bhnd_chipid_fixed_ncores(result, chipc_hwrev, 972 &result->ncores); 973 if (error) 974 goto cleanup; 975 } 976 977 cleanup: 978 /* Clean up */ 979 bus_release_resource(dev, rtype, rid, res); 980 return (error); 981 } 982 983 /** 984 * Read an NVRAM variable's NUL-terminated string value. 985 * 986 * @param dev A bhnd bus child device. 987 * @param name The NVRAM variable name. 988 * @param[out] buf A buffer large enough to hold @p len bytes. On 989 * success, the NUL-terminated string value will be 990 * written to this buffer. This argment may be NULL if 991 * the value is not desired. 992 * @param len The maximum capacity of @p buf. 993 * @param[out] rlen On success, will be set to the actual size of 994 * the requested value (including NUL termination). This 995 * argment may be NULL if the size is not desired. 996 * 997 * @retval 0 success 998 * @retval ENOENT The requested variable was not found. 999 * @retval ENODEV No valid NVRAM source could be found. 1000 * @retval ENOMEM If @p buf is non-NULL and a buffer of @p len is too 1001 * small to hold the requested value. 1002 * @retval EFTYPE If the variable data cannot be coerced to a valid 1003 * string representation. 1004 * @retval ERANGE If value coercion would overflow @p type. 1005 * @retval non-zero If reading @p name otherwise fails, a regular unix 1006 * error code will be returned. 1007 */ 1008 int 1009 bhnd_nvram_getvar_str(device_t dev, const char *name, char *buf, size_t len, 1010 size_t *rlen) 1011 { 1012 size_t larg; 1013 int error; 1014 1015 larg = len; 1016 error = bhnd_nvram_getvar(dev, name, buf, &larg, BHND_NVRAM_TYPE_CSTR); 1017 if (rlen != NULL) 1018 *rlen = larg; 1019 1020 return (error); 1021 } 1022 1023 /** 1024 * Read an NVRAM variable's unsigned integer value. 1025 * 1026 * @param dev A bhnd bus child device. 1027 * @param name The NVRAM variable name. 1028 * @param[out] value On success, the requested value will be written 1029 * to this pointer. 1030 * @param width The output integer type width (1, 2, or 1031 * 4 bytes). 1032 * 1033 * @retval 0 success 1034 * @retval ENOENT The requested variable was not found. 1035 * @retval ENODEV No valid NVRAM source could be found. 1036 * @retval EFTYPE If the variable data cannot be coerced to a 1037 * a valid unsigned integer representation. 1038 * @retval ERANGE If value coercion would overflow (or underflow) an 1039 * unsigned representation of the given @p width. 1040 * @retval non-zero If reading @p name otherwise fails, a regular unix 1041 * error code will be returned. 1042 */ 1043 int 1044 bhnd_nvram_getvar_uint(device_t dev, const char *name, void *value, int width) 1045 { 1046 bhnd_nvram_type type; 1047 size_t len; 1048 1049 switch (width) { 1050 case 1: 1051 type = BHND_NVRAM_TYPE_UINT8; 1052 break; 1053 case 2: 1054 type = BHND_NVRAM_TYPE_UINT16; 1055 break; 1056 case 4: 1057 type = BHND_NVRAM_TYPE_UINT32; 1058 break; 1059 default: 1060 device_printf(dev, "unsupported NVRAM integer width: %d\n", 1061 width); 1062 return (EINVAL); 1063 } 1064 1065 len = width; 1066 return (bhnd_nvram_getvar(dev, name, value, &len, type)); 1067 } 1068 1069 /** 1070 * Read an NVRAM variable's unsigned 8-bit integer value. 1071 * 1072 * @param dev A bhnd bus child device. 1073 * @param name The NVRAM variable name. 1074 * @param[out] value On success, the requested value will be written 1075 * to this pointer. 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) uint8_t. 1083 * @retval non-zero If reading @p name otherwise fails, a regular unix 1084 * error code will be returned. 1085 */ 1086 int 1087 bhnd_nvram_getvar_uint8(device_t dev, const char *name, uint8_t *value) 1088 { 1089 return (bhnd_nvram_getvar_uint(dev, name, value, sizeof(*value))); 1090 } 1091 1092 /** 1093 * Read an NVRAM variable's unsigned 16-bit integer value. 1094 * 1095 * @param dev A bhnd bus child device. 1096 * @param name The NVRAM variable name. 1097 * @param[out] value On success, the requested value will be written 1098 * to this pointer. 1099 * 1100 * @retval 0 success 1101 * @retval ENOENT The requested variable was not found. 1102 * @retval ENODEV No valid NVRAM source could be found. 1103 * @retval EFTYPE If the variable data cannot be coerced to a 1104 * a valid unsigned integer representation. 1105 * @retval ERANGE If value coercion would overflow (or underflow) 1106 * uint16_t. 1107 * @retval non-zero If reading @p name otherwise fails, a regular unix 1108 * error code will be returned. 1109 */ 1110 int 1111 bhnd_nvram_getvar_uint16(device_t dev, const char *name, uint16_t *value) 1112 { 1113 return (bhnd_nvram_getvar_uint(dev, name, value, sizeof(*value))); 1114 } 1115 1116 /** 1117 * Read an NVRAM variable's unsigned 32-bit integer value. 1118 * 1119 * @param dev A bhnd bus child device. 1120 * @param name The NVRAM variable name. 1121 * @param[out] value On success, the requested value will be written 1122 * to this pointer. 1123 * 1124 * @retval 0 success 1125 * @retval ENOENT The requested variable was not found. 1126 * @retval ENODEV No valid NVRAM source could be found. 1127 * @retval EFTYPE If the variable data cannot be coerced to a 1128 * a valid unsigned integer representation. 1129 * @retval ERANGE If value coercion would overflow (or underflow) 1130 * uint32_t. 1131 * @retval non-zero If reading @p name otherwise fails, a regular unix 1132 * error code will be returned. 1133 */ 1134 int 1135 bhnd_nvram_getvar_uint32(device_t dev, const char *name, uint32_t *value) 1136 { 1137 return (bhnd_nvram_getvar_uint(dev, name, value, sizeof(*value))); 1138 } 1139 1140 /** 1141 * Read an NVRAM variable's signed integer value. 1142 * 1143 * @param dev A bhnd bus child device. 1144 * @param name The NVRAM variable name. 1145 * @param[out] value On success, the requested value will be written 1146 * to this pointer. 1147 * @param width The output integer type width (1, 2, or 1148 * 4 bytes). 1149 * 1150 * @retval 0 success 1151 * @retval ENOENT The requested variable was not found. 1152 * @retval ENODEV No valid NVRAM source could be found. 1153 * @retval EFTYPE If the variable data cannot be coerced to a 1154 * a valid integer representation. 1155 * @retval ERANGE If value coercion would overflow (or underflow) an 1156 * signed representation of the given @p width. 1157 * @retval non-zero If reading @p name otherwise fails, a regular unix 1158 * error code will be returned. 1159 */ 1160 int 1161 bhnd_nvram_getvar_int(device_t dev, const char *name, void *value, int width) 1162 { 1163 bhnd_nvram_type type; 1164 size_t len; 1165 1166 switch (width) { 1167 case 1: 1168 type = BHND_NVRAM_TYPE_INT8; 1169 break; 1170 case 2: 1171 type = BHND_NVRAM_TYPE_INT16; 1172 break; 1173 case 4: 1174 type = BHND_NVRAM_TYPE_INT32; 1175 break; 1176 default: 1177 device_printf(dev, "unsupported NVRAM integer width: %d\n", 1178 width); 1179 return (EINVAL); 1180 } 1181 1182 len = width; 1183 return (bhnd_nvram_getvar(dev, name, value, &len, type)); 1184 } 1185 1186 /** 1187 * Read an NVRAM variable's signed 8-bit integer value. 1188 * 1189 * @param dev A bhnd bus child device. 1190 * @param name The NVRAM variable name. 1191 * @param[out] value On success, the requested value will be written 1192 * to this pointer. 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) int8_t. 1200 * @retval non-zero If reading @p name otherwise fails, a regular unix 1201 * error code will be returned. 1202 */ 1203 int 1204 bhnd_nvram_getvar_int8(device_t dev, const char *name, int8_t *value) 1205 { 1206 return (bhnd_nvram_getvar_int(dev, name, value, sizeof(*value))); 1207 } 1208 1209 /** 1210 * Read an NVRAM variable's signed 16-bit integer value. 1211 * 1212 * @param dev A bhnd bus child device. 1213 * @param name The NVRAM variable name. 1214 * @param[out] value On success, the requested value will be written 1215 * to this pointer. 1216 * 1217 * @retval 0 success 1218 * @retval ENOENT The requested variable was not found. 1219 * @retval ENODEV No valid NVRAM source could be found. 1220 * @retval EFTYPE If the variable data cannot be coerced to a 1221 * a valid integer representation. 1222 * @retval ERANGE If value coercion would overflow (or underflow) 1223 * int16_t. 1224 * @retval non-zero If reading @p name otherwise fails, a regular unix 1225 * error code will be returned. 1226 */ 1227 int 1228 bhnd_nvram_getvar_int16(device_t dev, const char *name, int16_t *value) 1229 { 1230 return (bhnd_nvram_getvar_int(dev, name, value, sizeof(*value))); 1231 } 1232 1233 /** 1234 * Read an NVRAM variable's signed 32-bit integer value. 1235 * 1236 * @param dev A bhnd bus child device. 1237 * @param name The NVRAM variable name. 1238 * @param[out] value On success, the requested value will be written 1239 * to this pointer. 1240 * 1241 * @retval 0 success 1242 * @retval ENOENT The requested variable was not found. 1243 * @retval ENODEV No valid NVRAM source could be found. 1244 * @retval EFTYPE If the variable data cannot be coerced to a 1245 * a valid integer representation. 1246 * @retval ERANGE If value coercion would overflow (or underflow) 1247 * int32_t. 1248 * @retval non-zero If reading @p name otherwise fails, a regular unix 1249 * error code will be returned. 1250 */ 1251 int 1252 bhnd_nvram_getvar_int32(device_t dev, const char *name, int32_t *value) 1253 { 1254 return (bhnd_nvram_getvar_int(dev, name, value, sizeof(*value))); 1255 } 1256 1257 1258 /** 1259 * Read an NVRAM variable's array value. 1260 * 1261 * @param dev A bhnd bus child device. 1262 * @param name The NVRAM variable name. 1263 * @param[out] buf A buffer large enough to hold @p size bytes. 1264 * On success, the requested value will be written 1265 * to this buffer. 1266 * @param[in,out] size The required number of bytes to write to 1267 * @p buf. 1268 * @param type The desired array element data representation. 1269 * 1270 * @retval 0 success 1271 * @retval ENOENT The requested variable was not found. 1272 * @retval ENODEV No valid NVRAM source could be found. 1273 * @retval ENXIO If less than @p size bytes are available. 1274 * @retval ENOMEM If a buffer of @p size is too small to hold the 1275 * requested value. 1276 * @retval EFTYPE If the variable data cannot be coerced to a 1277 * a valid instance of @p type. 1278 * @retval ERANGE If value coercion would overflow (or underflow) a 1279 * representation of @p type. 1280 * @retval non-zero If reading @p name otherwise fails, a regular unix 1281 * error code will be returned. 1282 */ 1283 int 1284 bhnd_nvram_getvar_array(device_t dev, const char *name, void *buf, size_t size, 1285 bhnd_nvram_type type) 1286 { 1287 size_t nbytes; 1288 int error; 1289 1290 /* Attempt read */ 1291 nbytes = size; 1292 if ((error = bhnd_nvram_getvar(dev, name, buf, &nbytes, type))) 1293 return (error); 1294 1295 /* Verify that the expected number of bytes were fetched */ 1296 if (nbytes < size) 1297 return (ENXIO); 1298 1299 return (0); 1300 } 1301 1302 /** 1303 * Using the bhnd(4) bus-level core information and a custom core name, 1304 * populate @p dev's device description. 1305 * 1306 * @param dev A bhnd-bus attached device. 1307 * @param dev_name The core's name (e.g. "SDIO Device Core") 1308 */ 1309 void 1310 bhnd_set_custom_core_desc(device_t dev, const char *dev_name) 1311 { 1312 const char *vendor_name; 1313 char *desc; 1314 1315 vendor_name = bhnd_get_vendor_name(dev); 1316 asprintf(&desc, M_BHND, "%s %s, rev %hhu", vendor_name, dev_name, 1317 bhnd_get_hwrev(dev)); 1318 1319 if (desc != NULL) { 1320 device_set_desc_copy(dev, desc); 1321 free(desc, M_BHND); 1322 } else { 1323 device_set_desc(dev, dev_name); 1324 } 1325 } 1326 1327 /** 1328 * Using the bhnd(4) bus-level core information, populate @p dev's device 1329 * description. 1330 * 1331 * @param dev A bhnd-bus attached device. 1332 */ 1333 void 1334 bhnd_set_default_core_desc(device_t dev) 1335 { 1336 bhnd_set_custom_core_desc(dev, bhnd_get_device_name(dev)); 1337 } 1338 1339 1340 /** 1341 * Using the bhnd @p chip_id, populate the bhnd(4) bus @p dev's device 1342 * description. 1343 * 1344 * @param dev A bhnd-bus attached device. 1345 */ 1346 void 1347 bhnd_set_default_bus_desc(device_t dev, const struct bhnd_chipid *chip_id) 1348 { 1349 const char *bus_name; 1350 char *desc; 1351 char chip_name[BHND_CHIPID_MAX_NAMELEN]; 1352 1353 /* Determine chip type's bus name */ 1354 switch (chip_id->chip_type) { 1355 case BHND_CHIPTYPE_SIBA: 1356 bus_name = "SIBA bus"; 1357 break; 1358 case BHND_CHIPTYPE_BCMA: 1359 case BHND_CHIPTYPE_BCMA_ALT: 1360 bus_name = "BCMA bus"; 1361 break; 1362 case BHND_CHIPTYPE_UBUS: 1363 bus_name = "UBUS bus"; 1364 break; 1365 default: 1366 bus_name = "Unknown Type"; 1367 break; 1368 } 1369 1370 /* Format chip name */ 1371 bhnd_format_chip_id(chip_name, sizeof(chip_name), 1372 chip_id->chip_id); 1373 1374 /* Format and set device description */ 1375 asprintf(&desc, M_BHND, "%s %s", chip_name, bus_name); 1376 if (desc != NULL) { 1377 device_set_desc_copy(dev, desc); 1378 free(desc, M_BHND); 1379 } else { 1380 device_set_desc(dev, bus_name); 1381 } 1382 1383 } 1384 1385 /** 1386 * Helper function for implementing BHND_BUS_IS_HW_DISABLED(). 1387 * 1388 * If a parent device is available, this implementation delegates the 1389 * request to the BHND_BUS_IS_HW_DISABLED() method on the parent of @p dev. 1390 * 1391 * If no parent device is available (i.e. on a the bus root), the hardware 1392 * is assumed to be usable and false is returned. 1393 */ 1394 bool 1395 bhnd_bus_generic_is_hw_disabled(device_t dev, device_t child) 1396 { 1397 if (device_get_parent(dev) != NULL) 1398 return (BHND_BUS_IS_HW_DISABLED(device_get_parent(dev), child)); 1399 1400 return (false); 1401 } 1402 1403 /** 1404 * Helper function for implementing BHND_BUS_GET_CHIPID(). 1405 * 1406 * This implementation delegates the request to the BHND_BUS_GET_CHIPID() 1407 * method on the parent of @p dev. If no parent exists, the implementation 1408 * will panic. 1409 */ 1410 const struct bhnd_chipid * 1411 bhnd_bus_generic_get_chipid(device_t dev, device_t child) 1412 { 1413 if (device_get_parent(dev) != NULL) 1414 return (BHND_BUS_GET_CHIPID(device_get_parent(dev), child)); 1415 1416 panic("missing BHND_BUS_GET_CHIPID()"); 1417 } 1418 1419 /* nvram board_info population macros for bhnd_bus_generic_read_board_info() */ 1420 #define BHND_GV(_dest, _name) \ 1421 bhnd_nvram_getvar_uint(child, BHND_NVAR_ ## _name, &_dest, \ 1422 sizeof(_dest)) 1423 1424 #define REQ_BHND_GV(_dest, _name) do { \ 1425 if ((error = BHND_GV(_dest, _name))) { \ 1426 device_printf(dev, \ 1427 "error reading " __STRING(_name) ": %d\n", error); \ 1428 return (error); \ 1429 } \ 1430 } while(0) 1431 1432 #define OPT_BHND_GV(_dest, _name, _default) do { \ 1433 if ((error = BHND_GV(_dest, _name))) { \ 1434 if (error != ENOENT) { \ 1435 device_printf(dev, \ 1436 "error reading " \ 1437 __STRING(_name) ": %d\n", error); \ 1438 return (error); \ 1439 } \ 1440 _dest = _default; \ 1441 } \ 1442 } while(0) 1443 1444 /** 1445 * Helper function for implementing BHND_BUS_READ_BOARDINFO(). 1446 * 1447 * This implementation populates @p info with information from NVRAM, 1448 * defaulting board_vendor and board_type fields to 0 if the 1449 * requested variables cannot be found. 1450 * 1451 * This behavior is correct for most SoCs, but must be overridden on 1452 * bridged (PCI, PCMCIA, etc) devices to produce a complete bhnd_board_info 1453 * result. 1454 */ 1455 int 1456 bhnd_bus_generic_read_board_info(device_t dev, device_t child, 1457 struct bhnd_board_info *info) 1458 { 1459 int error; 1460 1461 OPT_BHND_GV(info->board_vendor, BOARDVENDOR, 0); 1462 OPT_BHND_GV(info->board_type, BOARDTYPE, 0); /* srom >= 2 */ 1463 REQ_BHND_GV(info->board_rev, BOARDREV); 1464 OPT_BHND_GV(info->board_srom_rev,SROMREV, 0); /* missing in 1465 some SoC 1466 NVRAM */ 1467 REQ_BHND_GV(info->board_flags, BOARDFLAGS); 1468 OPT_BHND_GV(info->board_flags2, BOARDFLAGS2, 0); /* srom >= 4 */ 1469 OPT_BHND_GV(info->board_flags3, BOARDFLAGS3, 0); /* srom >= 11 */ 1470 1471 return (0); 1472 } 1473 1474 #undef BHND_GV 1475 #undef BHND_GV_REQ 1476 #undef BHND_GV_OPT 1477 1478 /** 1479 * Helper function for implementing BHND_BUS_GET_NVRAM_VAR(). 1480 * 1481 * This implementation searches @p dev for a usable NVRAM child device. 1482 * 1483 * If no usable child device is found on @p dev, the request is delegated to 1484 * the BHND_BUS_GET_NVRAM_VAR() method on the parent of @p dev. 1485 */ 1486 int 1487 bhnd_bus_generic_get_nvram_var(device_t dev, device_t child, const char *name, 1488 void *buf, size_t *size, bhnd_nvram_type type) 1489 { 1490 device_t nvram; 1491 device_t parent; 1492 1493 /* Make sure we're holding Giant for newbus */ 1494 GIANT_REQUIRED; 1495 1496 /* Look for a directly-attached NVRAM child */ 1497 if ((nvram = device_find_child(dev, "bhnd_nvram", -1)) != NULL) 1498 return BHND_NVRAM_GETVAR(nvram, name, buf, size, type); 1499 1500 /* Try to delegate to parent */ 1501 if ((parent = device_get_parent(dev)) == NULL) 1502 return (ENODEV); 1503 1504 return (BHND_BUS_GET_NVRAM_VAR(device_get_parent(dev), child, 1505 name, buf, size, type)); 1506 } 1507 1508 /** 1509 * Helper function for implementing BHND_BUS_ALLOC_RESOURCE(). 1510 * 1511 * This implementation of BHND_BUS_ALLOC_RESOURCE() delegates allocation 1512 * of the underlying resource to BUS_ALLOC_RESOURCE(), and activation 1513 * to @p dev's BHND_BUS_ACTIVATE_RESOURCE(). 1514 */ 1515 struct bhnd_resource * 1516 bhnd_bus_generic_alloc_resource(device_t dev, device_t child, int type, 1517 int *rid, rman_res_t start, rman_res_t end, rman_res_t count, 1518 u_int flags) 1519 { 1520 struct bhnd_resource *br; 1521 struct resource *res; 1522 int error; 1523 1524 br = NULL; 1525 res = NULL; 1526 1527 /* Allocate the real bus resource (without activating it) */ 1528 res = BUS_ALLOC_RESOURCE(dev, child, type, rid, start, end, count, 1529 (flags & ~RF_ACTIVE)); 1530 if (res == NULL) 1531 return (NULL); 1532 1533 /* Allocate our bhnd resource wrapper. */ 1534 br = malloc(sizeof(struct bhnd_resource), M_BHND, M_NOWAIT); 1535 if (br == NULL) 1536 goto failed; 1537 1538 br->direct = false; 1539 br->res = res; 1540 1541 /* Attempt activation */ 1542 if (flags & RF_ACTIVE) { 1543 error = BHND_BUS_ACTIVATE_RESOURCE(dev, child, type, *rid, br); 1544 if (error) 1545 goto failed; 1546 } 1547 1548 return (br); 1549 1550 failed: 1551 if (res != NULL) 1552 BUS_RELEASE_RESOURCE(dev, child, type, *rid, res); 1553 1554 free(br, M_BHND); 1555 return (NULL); 1556 } 1557 1558 /** 1559 * Helper function for implementing BHND_BUS_RELEASE_RESOURCE(). 1560 * 1561 * This implementation of BHND_BUS_RELEASE_RESOURCE() delegates release of 1562 * the backing resource to BUS_RELEASE_RESOURCE(). 1563 */ 1564 int 1565 bhnd_bus_generic_release_resource(device_t dev, device_t child, int type, 1566 int rid, struct bhnd_resource *r) 1567 { 1568 int error; 1569 1570 if ((error = BUS_RELEASE_RESOURCE(dev, child, type, rid, r->res))) 1571 return (error); 1572 1573 free(r, M_BHND); 1574 return (0); 1575 } 1576 1577 1578 /** 1579 * Helper function for implementing BHND_BUS_ACTIVATE_RESOURCE(). 1580 * 1581 * This implementation of BHND_BUS_ACTIVATE_RESOURCE() simply calls the 1582 * BHND_BUS_ACTIVATE_RESOURCE() method of the parent of @p dev. 1583 */ 1584 int 1585 bhnd_bus_generic_activate_resource(device_t dev, device_t child, int type, 1586 int rid, struct bhnd_resource *r) 1587 { 1588 /* Try to delegate to the parent */ 1589 if (device_get_parent(dev) != NULL) 1590 return (BHND_BUS_ACTIVATE_RESOURCE(device_get_parent(dev), 1591 child, type, rid, r)); 1592 1593 return (EINVAL); 1594 }; 1595 1596 /** 1597 * Helper function for implementing BHND_BUS_DEACTIVATE_RESOURCE(). 1598 * 1599 * This implementation of BHND_BUS_ACTIVATE_RESOURCE() simply calls the 1600 * BHND_BUS_ACTIVATE_RESOURCE() method of the parent of @p dev. 1601 */ 1602 int 1603 bhnd_bus_generic_deactivate_resource(device_t dev, device_t child, 1604 int type, int rid, struct bhnd_resource *r) 1605 { 1606 if (device_get_parent(dev) != NULL) 1607 return (BHND_BUS_DEACTIVATE_RESOURCE(device_get_parent(dev), 1608 child, type, rid, r)); 1609 1610 return (EINVAL); 1611 }; 1612 1613