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/types.h> 34 #include <sys/param.h> 35 #include <sys/bus.h> 36 #include <sys/systm.h> 37 38 #include <machine/bus.h> 39 #include <sys/rman.h> 40 #include <machine/resource.h> 41 42 #include <dev/bhnd/cores/chipc/chipcreg.h> 43 44 #include "bhndreg.h" 45 #include "bhndvar.h" 46 47 /* BHND core device description table. */ 48 static const struct bhnd_core_desc { 49 uint16_t vendor; 50 uint16_t device; 51 bhnd_devclass_t class; 52 const char *desc; 53 } bhnd_core_descs[] = { 54 #define BHND_CDESC(_mfg, _cid, _cls, _desc) \ 55 { BHND_MFGID_ ## _mfg, BHND_COREID_ ## _cid, \ 56 BHND_DEVCLASS_ ## _cls, _desc } 57 58 BHND_CDESC(BCM, CC, CC, "ChipCommon I/O Controller"), 59 BHND_CDESC(BCM, ILINE20, OTHER, "iLine20 HPNA"), 60 BHND_CDESC(BCM, SRAM, RAM, "SRAM"), 61 BHND_CDESC(BCM, SDRAM, RAM, "SDRAM"), 62 BHND_CDESC(BCM, PCI, PCI, "PCI Bridge"), 63 BHND_CDESC(BCM, MIPS, CPU, "MIPS Core"), 64 BHND_CDESC(BCM, ENET, ENET_MAC, "Fast Ethernet MAC"), 65 BHND_CDESC(BCM, CODEC, OTHER, "V.90 Modem Codec"), 66 BHND_CDESC(BCM, USB, OTHER, "USB 1.1 Device/Host Controller"), 67 BHND_CDESC(BCM, ADSL, OTHER, "ADSL Core"), 68 BHND_CDESC(BCM, ILINE100, OTHER, "iLine100 HPNA"), 69 BHND_CDESC(BCM, IPSEC, OTHER, "IPsec Accelerator"), 70 BHND_CDESC(BCM, UTOPIA, OTHER, "UTOPIA ATM Core"), 71 BHND_CDESC(BCM, PCMCIA, PCCARD, "PCMCIA Bridge"), 72 BHND_CDESC(BCM, SOCRAM, RAM, "Internal Memory"), 73 BHND_CDESC(BCM, MEMC, MEMC, "MEMC SDRAM Controller"), 74 BHND_CDESC(BCM, OFDM, OTHER, "OFDM PHY"), 75 BHND_CDESC(BCM, EXTIF, OTHER, "External Interface"), 76 BHND_CDESC(BCM, D11, WLAN, "802.11 MAC/PHY/Radio"), 77 BHND_CDESC(BCM, APHY, WLAN_PHY, "802.11a PHY"), 78 BHND_CDESC(BCM, BPHY, WLAN_PHY, "802.11b PHY"), 79 BHND_CDESC(BCM, GPHY, WLAN_PHY, "802.11g PHY"), 80 BHND_CDESC(BCM, MIPS33, CPU, "MIPS 3302 Core"), 81 BHND_CDESC(BCM, USB11H, OTHER, "USB 1.1 Host Controller"), 82 BHND_CDESC(BCM, USB11D, OTHER, "USB 1.1 Device Core"), 83 BHND_CDESC(BCM, USB20H, OTHER, "USB 2.0 Host Controller"), 84 BHND_CDESC(BCM, USB20D, OTHER, "USB 2.0 Device Core"), 85 BHND_CDESC(BCM, SDIOH, OTHER, "SDIO Host Controller"), 86 BHND_CDESC(BCM, ROBO, OTHER, "RoboSwitch"), 87 BHND_CDESC(BCM, ATA100, OTHER, "Parallel ATA Controller"), 88 BHND_CDESC(BCM, SATAXOR, OTHER, "SATA DMA/XOR Controller"), 89 BHND_CDESC(BCM, GIGETH, ENET_MAC, "Gigabit Ethernet MAC"), 90 BHND_CDESC(BCM, PCIE, PCIE, "PCIe Bridge"), 91 BHND_CDESC(BCM, NPHY, WLAN_PHY, "802.11n 2x2 PHY"), 92 BHND_CDESC(BCM, SRAMC, MEMC, "SRAM Controller"), 93 BHND_CDESC(BCM, MINIMAC, OTHER, "MINI MAC/PHY"), 94 BHND_CDESC(BCM, ARM11, CPU, "ARM1176 CPU"), 95 BHND_CDESC(BCM, ARM7S, CPU, "ARM7TDMI-S CPU"), 96 BHND_CDESC(BCM, LPPHY, WLAN_PHY, "802.11a/b/g PHY"), 97 BHND_CDESC(BCM, PMU, PMU, "PMU"), 98 BHND_CDESC(BCM, SSNPHY, WLAN_PHY, "802.11n Single-Stream PHY"), 99 BHND_CDESC(BCM, SDIOD, OTHER, "SDIO Device Core"), 100 BHND_CDESC(BCM, ARMCM3, CPU, "ARM Cortex-M3 CPU"), 101 BHND_CDESC(BCM, HTPHY, WLAN_PHY, "802.11n 4x4 PHY"), 102 BHND_CDESC(BCM, MIPS74K, CPU, "MIPS74k CPU"), 103 BHND_CDESC(BCM, GMAC, ENET_MAC, "Gigabit MAC core"), 104 BHND_CDESC(BCM, DMEMC, MEMC, "DDR1/DDR2 Memory Controller"), 105 BHND_CDESC(BCM, PCIERC, OTHER, "PCIe Root Complex"), 106 BHND_CDESC(BCM, OCP, SOC_BRIDGE, "OCP to OCP Bridge"), 107 BHND_CDESC(BCM, SC, OTHER, "Shared Common Core"), 108 BHND_CDESC(BCM, AHB, SOC_BRIDGE, "OCP to AHB Bridge"), 109 BHND_CDESC(BCM, SPIH, OTHER, "SPI Host Controller"), 110 BHND_CDESC(BCM, I2S, OTHER, "I2S Digital Audio Interface"), 111 BHND_CDESC(BCM, DMEMS, MEMC, "SDR/DDR1 Memory Controller"), 112 BHND_CDESC(BCM, UBUS_SHIM, OTHER, "BCM6362/UBUS WLAN SHIM"), 113 BHND_CDESC(BCM, PCIE2, PCIE, "PCIe Bridge (Gen2)"), 114 115 BHND_CDESC(ARM, APB_BRIDGE, SOC_BRIDGE, "BP135 AMBA3 AXI to APB Bridge"), 116 BHND_CDESC(ARM, PL301, SOC_ROUTER, "PL301 AMBA3 Interconnect"), 117 BHND_CDESC(ARM, EROM, EROM, "PL366 Device Enumeration ROM"), 118 BHND_CDESC(ARM, OOB_ROUTER, OTHER, "PL367 OOB Interrupt Router"), 119 BHND_CDESC(ARM, AXI_UNMAPPED, OTHER, "Unmapped Address Ranges"), 120 121 BHND_CDESC(BCM, 4706_CC, CC, "ChipCommon I/O Controller"), 122 BHND_CDESC(BCM, NS_PCIE2, PCIE, "PCIe Bridge (Gen2)"), 123 BHND_CDESC(BCM, NS_DMA, OTHER, "DMA engine"), 124 BHND_CDESC(BCM, NS_SDIO, OTHER, "SDIO 3.0 Host Controller"), 125 BHND_CDESC(BCM, NS_USB20H, OTHER, "USB 2.0 Host Controller"), 126 BHND_CDESC(BCM, NS_USB30H, OTHER, "USB 3.0 Host Controller"), 127 BHND_CDESC(BCM, NS_A9JTAG, OTHER, "ARM Cortex A9 JTAG Interface"), 128 BHND_CDESC(BCM, NS_DDR23_MEMC, MEMC, "Denali DDR2/DD3 Memory Controller"), 129 BHND_CDESC(BCM, NS_ROM, NVRAM, "System ROM"), 130 BHND_CDESC(BCM, NS_NAND, NVRAM, "NAND Flash Controller"), 131 BHND_CDESC(BCM, NS_QSPI, NVRAM, "QSPI Flash Controller"), 132 BHND_CDESC(BCM, NS_CC_B, CC_B, "ChipCommon B Auxiliary I/O Controller"), 133 BHND_CDESC(BCM, 4706_SOCRAM, RAM, "Internal Memory"), 134 BHND_CDESC(BCM, IHOST_ARMCA9, CPU, "ARM Cortex A9 CPU"), 135 BHND_CDESC(BCM, 4706_GMAC_CMN, ENET, "Gigabit MAC (Common)"), 136 BHND_CDESC(BCM, 4706_GMAC, ENET_MAC, "Gigabit MAC"), 137 BHND_CDESC(BCM, AMEMC, MEMC, "Denali DDR1/DDR2 Memory Controller"), 138 #undef BHND_CDESC 139 140 /* Derived from inspection of the BCM4331 cores that provide PrimeCell 141 * IDs. Due to lack of documentation, the surmised device name/purpose 142 * provided here may be incorrect. */ 143 { BHND_MFGID_ARM, BHND_PRIMEID_EROM, BHND_DEVCLASS_OTHER, 144 "PL364 Device Enumeration ROM" }, 145 { BHND_MFGID_ARM, BHND_PRIMEID_SWRAP, BHND_DEVCLASS_OTHER, 146 "PL368 Device Management Interface" }, 147 { BHND_MFGID_ARM, BHND_PRIMEID_MWRAP, BHND_DEVCLASS_OTHER, 148 "PL369 Device Management Interface" }, 149 150 { 0, 0, 0, NULL } 151 }; 152 153 /** 154 * Return the name for a given JEP106 manufacturer ID. 155 * 156 * @param vendor A JEP106 Manufacturer ID, including the non-standard ARM 4-bit 157 * JEP106 continuation code. 158 */ 159 const char * 160 bhnd_vendor_name(uint16_t vendor) 161 { 162 switch (vendor) { 163 case BHND_MFGID_ARM: 164 return "ARM"; 165 case BHND_MFGID_BCM: 166 return "Broadcom"; 167 case BHND_MFGID_MIPS: 168 return "MIPS"; 169 default: 170 return "unknown"; 171 } 172 } 173 174 /** 175 * Return the name of a port type. 176 */ 177 const char * 178 bhnd_port_type_name(bhnd_port_type port_type) 179 { 180 switch (port_type) { 181 case BHND_PORT_DEVICE: 182 return ("device"); 183 case BHND_PORT_BRIDGE: 184 return ("bridge"); 185 case BHND_PORT_AGENT: 186 return ("agent"); 187 default: 188 return "unknown"; 189 } 190 } 191 192 193 static const struct bhnd_core_desc * 194 bhnd_find_core_desc(uint16_t vendor, uint16_t device) 195 { 196 for (u_int i = 0; bhnd_core_descs[i].desc != NULL; i++) { 197 if (bhnd_core_descs[i].vendor != vendor) 198 continue; 199 200 if (bhnd_core_descs[i].device != device) 201 continue; 202 203 return (&bhnd_core_descs[i]); 204 } 205 206 return (NULL); 207 } 208 209 /** 210 * Return a human-readable name for a BHND core. 211 * 212 * @param vendor The core designer's JEDEC-106 Manufacturer ID 213 * @param device The core identifier. 214 */ 215 const char * 216 bhnd_find_core_name(uint16_t vendor, uint16_t device) 217 { 218 const struct bhnd_core_desc *desc; 219 220 if ((desc = bhnd_find_core_desc(vendor, device)) == NULL) 221 return ("unknown"); 222 223 return desc->desc; 224 } 225 226 /** 227 * Return the device class for a BHND core. 228 * 229 * @param vendor The core designer's JEDEC-106 Manufacturer ID 230 * @param device The core identifier. 231 */ 232 bhnd_devclass_t 233 bhnd_find_core_class(uint16_t vendor, uint16_t device) 234 { 235 const struct bhnd_core_desc *desc; 236 237 if ((desc = bhnd_find_core_desc(vendor, device)) == NULL) 238 return (BHND_DEVCLASS_OTHER); 239 240 return desc->class; 241 } 242 243 /** 244 * Return a human-readable name for a BHND core. 245 * 246 * @param ci The core's info record. 247 */ 248 const char * 249 bhnd_core_name(const struct bhnd_core_info *ci) 250 { 251 return bhnd_find_core_name(ci->vendor, ci->device); 252 } 253 254 /** 255 * Return the device class for a BHND core. 256 * 257 * @param ci The core's info record. 258 */ 259 bhnd_devclass_t 260 bhnd_core_class(const struct bhnd_core_info *ci) 261 { 262 return bhnd_find_core_class(ci->vendor, ci->device); 263 } 264 265 /** 266 * Initialize a core info record with data from from a bhnd-attached @p dev. 267 * 268 * @param dev A bhnd device. 269 * @param core The record to be initialized. 270 */ 271 struct bhnd_core_info 272 bhnd_get_core_info(device_t dev) { 273 return (struct bhnd_core_info) { 274 .vendor = bhnd_get_vendor(dev), 275 .device = bhnd_get_device(dev), 276 .hwrev = bhnd_get_hwrev(dev), 277 .core_idx = bhnd_get_core_index(dev), 278 .unit = bhnd_get_core_unit(dev) 279 }; 280 } 281 282 /** 283 * Find a @p class child device with @p unit on @p dev. 284 * 285 * @param parent The bhnd-compatible bus to be searched. 286 * @param class The device class to match on. 287 * @param unit The device unit number; specify -1 to return the first match 288 * regardless of unit number. 289 * 290 * @retval device_t if a matching child device is found. 291 * @retval NULL if no matching child device is found. 292 */ 293 device_t 294 bhnd_find_child(device_t dev, bhnd_devclass_t class, int unit) 295 { 296 struct bhnd_core_match md = { 297 .vendor = BHND_MFGID_INVALID, 298 .device = BHND_COREID_INVALID, 299 .hwrev.start = BHND_HWREV_INVALID, 300 .hwrev.end = BHND_HWREV_INVALID, 301 .class = class, 302 .unit = unit 303 }; 304 305 return bhnd_match_child(dev, &md); 306 } 307 308 /** 309 * Find the first child device on @p dev that matches @p desc. 310 * 311 * @param parent The bhnd-compatible bus to be searched. 312 * @param desc A match descriptor. 313 * 314 * @retval device_t if a matching child device is found. 315 * @retval NULL if no matching child device is found. 316 */ 317 device_t 318 bhnd_match_child(device_t dev, const struct bhnd_core_match *desc) 319 { 320 device_t *devlistp; 321 device_t match; 322 int devcnt; 323 int error; 324 325 error = device_get_children(dev, &devlistp, &devcnt); 326 if (error != 0) 327 return (NULL); 328 329 match = NULL; 330 for (int i = 0; i < devcnt; i++) { 331 device_t dev = devlistp[i]; 332 if (bhnd_device_matches(dev, desc)) { 333 match = dev; 334 goto done; 335 } 336 } 337 338 done: 339 free(devlistp, M_TEMP); 340 return match; 341 } 342 343 /** 344 * Find the first core in @p cores that matches @p desc. 345 * 346 * @param cores The table to search. 347 * @param num_cores The length of @p cores. 348 * @param desc A match descriptor. 349 * 350 * @retval bhnd_core_info if a matching core is found. 351 * @retval NULL if no matching core is found. 352 */ 353 const struct bhnd_core_info * 354 bhnd_match_core(const struct bhnd_core_info *cores, u_int num_cores, 355 const struct bhnd_core_match *desc) 356 { 357 for (u_int i = 0; i < num_cores; i++) { 358 if (bhnd_core_matches(&cores[i], desc)) 359 return &cores[i]; 360 } 361 362 return (NULL); 363 } 364 365 366 /** 367 * Find the first core in @p cores with the given @p class. 368 * 369 * @param cores The table to search. 370 * @param num_cores The length of @p cores. 371 * @param desc A match descriptor. 372 * 373 * @retval bhnd_core_info if a matching core is found. 374 * @retval NULL if no matching core is found. 375 */ 376 const struct bhnd_core_info * 377 bhnd_find_core(const struct bhnd_core_info *cores, u_int num_cores, 378 bhnd_devclass_t class) 379 { 380 struct bhnd_core_match md = { 381 .vendor = BHND_MFGID_INVALID, 382 .device = BHND_COREID_INVALID, 383 .hwrev.start = BHND_HWREV_INVALID, 384 .hwrev.end = BHND_HWREV_INVALID, 385 .class = class, 386 .unit = -1 387 }; 388 389 return bhnd_match_core(cores, num_cores, &md); 390 } 391 392 /** 393 * Return true if the @p core matches @p desc. 394 * 395 * @param core A bhnd core descriptor. 396 * @param desc A match descriptor to compare against @p core. 397 * 398 * @retval true if @p core matches @p match 399 * @retval false if @p core does not match @p match. 400 */ 401 bool 402 bhnd_core_matches(const struct bhnd_core_info *core, 403 const struct bhnd_core_match *desc) 404 { 405 if (desc->vendor != BHND_MFGID_INVALID && 406 desc->vendor != core->vendor) 407 return (false); 408 409 if (desc->device != BHND_COREID_INVALID && 410 desc->device != core->device) 411 return (false); 412 413 if (desc->unit != -1 && desc->unit != core->unit) 414 return (false); 415 416 if (!bhnd_hwrev_matches(core->hwrev, &desc->hwrev)) 417 return (false); 418 419 if (desc->class != BHND_DEVCLASS_INVALID && 420 desc->class != bhnd_core_class(core)) 421 return (false); 422 423 return true; 424 } 425 426 /** 427 * Return true if the @p chip matches @p desc. 428 * 429 * @param chip A bhnd chip identifier. 430 * @param desc A match descriptor to compare against @p chip. 431 * 432 * @retval true if @p chip matches @p match 433 * @retval false if @p chip does not match @p match. 434 */ 435 bool 436 bhnd_chip_matches(const struct bhnd_chipid *chip, 437 const struct bhnd_chip_match *desc) 438 { 439 if (desc->match_id && chip->chip_id != desc->chip_id) 440 return (false); 441 442 if (desc->match_pkg && chip->chip_pkg != desc->chip_pkg) 443 return (false); 444 445 if (desc->match_rev && 446 !bhnd_hwrev_matches(chip->chip_rev, &desc->chip_rev)) 447 return (false); 448 449 return (true); 450 } 451 452 /** 453 * Return true if the @p hwrev matches @p desc. 454 * 455 * @param hwrev A bhnd hardware revision. 456 * @param desc A match descriptor to compare against @p core. 457 * 458 * @retval true if @p hwrev matches @p match 459 * @retval false if @p hwrev does not match @p match. 460 */ 461 bool 462 bhnd_hwrev_matches(uint16_t hwrev, const struct bhnd_hwrev_match *desc) 463 { 464 if (desc->start != BHND_HWREV_INVALID && 465 desc->start > hwrev) 466 return false; 467 468 if (desc->end != BHND_HWREV_INVALID && 469 desc->end < hwrev) 470 return false; 471 472 return true; 473 } 474 475 /** 476 * Return true if the @p dev matches @p desc. 477 * 478 * @param dev A bhnd device. 479 * @param desc A match descriptor to compare against @p dev. 480 * 481 * @retval true if @p dev matches @p match 482 * @retval false if @p dev does not match @p match. 483 */ 484 bool 485 bhnd_device_matches(device_t dev, const struct bhnd_core_match *desc) 486 { 487 struct bhnd_core_info ci = { 488 .vendor = bhnd_get_vendor(dev), 489 .device = bhnd_get_device(dev), 490 .unit = bhnd_get_core_unit(dev), 491 .hwrev = bhnd_get_hwrev(dev) 492 }; 493 494 return bhnd_core_matches(&ci, desc); 495 } 496 497 /** 498 * Search @p table for an entry matching @p dev. 499 * 500 * @param dev A bhnd device to match against @p table. 501 * @param table The device table to search. 502 * @param entry_size The @p table entry size, in bytes. 503 * 504 * @retval bhnd_device the first matching device, if any. 505 * @retval NULL if no matching device is found in @p table. 506 */ 507 const struct bhnd_device * 508 bhnd_device_lookup(device_t dev, const struct bhnd_device *table, 509 size_t entry_size) 510 { 511 const struct bhnd_device *entry; 512 device_t hostb, parent; 513 514 parent = device_get_parent(dev); 515 hostb = bhnd_find_hostb_device(parent); 516 517 for (entry = table; entry->desc != NULL; entry = 518 (const struct bhnd_device *) ((const char *) entry + entry_size)) 519 { 520 /* match core info */ 521 if (!bhnd_device_matches(dev, &entry->core)) 522 continue; 523 524 /* match device flags */ 525 if (entry->device_flags & BHND_DF_HOSTB) { 526 if (dev != hostb) 527 continue; 528 } 529 530 /* device found */ 531 return (entry); 532 } 533 534 /* not found */ 535 return (NULL); 536 } 537 538 /** 539 * Scan @p table for all quirk flags applicable to @p dev's chip identifier 540 * (as returned by bhnd_get_chipid). 541 * 542 * @param dev A bhnd device. 543 * @param table The chip quirk table to search. 544 * 545 * @return returns all matching quirk flags. 546 */ 547 uint32_t 548 bhnd_chip_quirks(device_t dev, const struct bhnd_chip_quirk *table) 549 { 550 const struct bhnd_chipid *cid; 551 const struct bhnd_chip_quirk *qent; 552 uint32_t quirks; 553 554 cid = bhnd_get_chipid(dev); 555 quirks = 0; 556 557 for (qent = table; !BHND_CHIP_QUIRK_IS_END(qent); qent++) { 558 if (bhnd_chip_matches(cid, &qent->chip)) 559 quirks |= qent->quirks; 560 } 561 562 return (quirks); 563 } 564 565 /** 566 * Scan @p table for all quirk flags applicable to @p dev. 567 * 568 * @param dev A bhnd device to match against @p table. 569 * @param table The device table to search. 570 * @param entry_size The @p table entry size, in bytes. 571 * 572 * @return returns all matching quirk flags. 573 */ 574 uint32_t 575 bhnd_device_quirks(device_t dev, const struct bhnd_device *table, 576 size_t entry_size) 577 { 578 const struct bhnd_device *dent; 579 const struct bhnd_device_quirk *qtable, *qent; 580 uint32_t quirks; 581 uint16_t hwrev; 582 583 hwrev = bhnd_get_hwrev(dev); 584 quirks = 0; 585 586 /* Find the quirk table */ 587 if ((dent = bhnd_device_lookup(dev, table, entry_size)) == NULL) { 588 /* This is almost certainly a (caller) implementation bug */ 589 device_printf(dev, "quirk lookup did not match any device\n"); 590 return (0); 591 } 592 593 /* Quirks aren't a mandatory field */ 594 if ((qtable = dent->quirks_table) == NULL) 595 return (0); 596 597 /* Collect matching quirk entries */ 598 for (qent = qtable; !BHND_DEVICE_QUIRK_IS_END(qent); qent++) { 599 if (bhnd_hwrev_matches(hwrev, &qent->hwrev)) 600 quirks |= qent->quirks; 601 } 602 603 return (quirks); 604 } 605 606 607 /** 608 * Allocate bhnd(4) resources defined in @p rs from a parent bus. 609 * 610 * @param dev The device requesting ownership of the resources. 611 * @param rs A standard bus resource specification. This will be updated 612 * with the allocated resource's RIDs. 613 * @param res On success, the allocated bhnd resources. 614 * 615 * @retval 0 success 616 * @retval non-zero if allocation of any non-RF_OPTIONAL resource fails, 617 * all allocated resources will be released and a regular 618 * unix error code will be returned. 619 */ 620 int 621 bhnd_alloc_resources(device_t dev, struct resource_spec *rs, 622 struct bhnd_resource **res) 623 { 624 /* Initialize output array */ 625 for (u_int i = 0; rs[i].type != -1; i++) 626 res[i] = NULL; 627 628 for (u_int i = 0; rs[i].type != -1; i++) { 629 res[i] = bhnd_alloc_resource_any(dev, rs[i].type, &rs[i].rid, 630 rs[i].flags); 631 632 /* Clean up all allocations on failure */ 633 if (res[i] == NULL && !(rs[i].flags & RF_OPTIONAL)) { 634 bhnd_release_resources(dev, rs, res); 635 return (ENXIO); 636 } 637 } 638 639 return (0); 640 }; 641 642 /** 643 * Release bhnd(4) resources defined in @p rs from a parent bus. 644 * 645 * @param dev The device that owns the resources. 646 * @param rs A standard bus resource specification previously initialized 647 * by @p bhnd_alloc_resources. 648 * @param res The bhnd resources to be released. 649 */ 650 void 651 bhnd_release_resources(device_t dev, const struct resource_spec *rs, 652 struct bhnd_resource **res) 653 { 654 for (u_int i = 0; rs[i].type != -1; i++) { 655 if (res[i] == NULL) 656 continue; 657 658 bhnd_release_resource(dev, rs[i].type, rs[i].rid, res[i]); 659 res[i] = NULL; 660 } 661 } 662 663 /** 664 * Parse the CHIPC_ID_* fields from the ChipCommon CHIPC_ID 665 * register, returning its bhnd_chipid representation. 666 * 667 * @param idreg The CHIPC_ID register value. 668 * @param enum_addr The enumeration address to include in the result. 669 * 670 * @warning 671 * On early siba(4) devices, the ChipCommon core does not provide 672 * a valid CHIPC_ID_NUMCORE field. On these ChipCommon revisions 673 * (see CHIPC_NCORES_MIN_HWREV()), this function will parse and return 674 * an invalid `ncores` value. 675 */ 676 struct bhnd_chipid 677 bhnd_parse_chipid(uint32_t idreg, bhnd_addr_t enum_addr) 678 { 679 struct bhnd_chipid result; 680 681 /* Fetch the basic chip info */ 682 result.chip_id = CHIPC_GET_ATTR(idreg, ID_CHIP); 683 result.chip_pkg = CHIPC_GET_ATTR(idreg, ID_PKG); 684 result.chip_rev = CHIPC_GET_ATTR(idreg, ID_REV); 685 result.chip_type = CHIPC_GET_ATTR(idreg, ID_BUS); 686 result.ncores = CHIPC_GET_ATTR(idreg, ID_NUMCORE); 687 688 result.enum_addr = enum_addr; 689 690 return (result); 691 } 692 693 /** 694 * Allocate the resource defined by @p rs via @p dev, use it 695 * to read the ChipCommon ID register relative to @p chipc_offset, 696 * then release the resource. 697 * 698 * @param dev The device owning @p rs. 699 * @param rs A resource spec that encompasses the ChipCommon register block. 700 * @param chipc_offset The offset of the ChipCommon registers within @p rs. 701 * @param[out] result the chip identification data. 702 * 703 * @retval 0 success 704 * @retval non-zero if the ChipCommon identification data could not be read. 705 */ 706 int 707 bhnd_read_chipid(device_t dev, struct resource_spec *rs, 708 bus_size_t chipc_offset, struct bhnd_chipid *result) 709 { 710 struct resource *res; 711 uint32_t reg; 712 int error, rid, rtype; 713 714 /* Allocate the ChipCommon window resource and fetch the chipid data */ 715 rid = rs->rid; 716 rtype = rs->type; 717 res = bus_alloc_resource_any(dev, rtype, &rid, RF_ACTIVE); 718 if (res == NULL) { 719 device_printf(dev, 720 "failed to allocate bhnd chipc resource\n"); 721 return (ENXIO); 722 } 723 724 /* Fetch the basic chip info */ 725 reg = bus_read_4(res, chipc_offset + CHIPC_ID); 726 *result = bhnd_parse_chipid(reg, 0x0); 727 728 /* Fetch the enum base address */ 729 error = 0; 730 switch (result->chip_type) { 731 case BHND_CHIPTYPE_SIBA: 732 result->enum_addr = BHND_DEFAULT_CHIPC_ADDR; 733 break; 734 case BHND_CHIPTYPE_BCMA: 735 case BHND_CHIPTYPE_BCMA_ALT: 736 result->enum_addr = bus_read_4(res, chipc_offset + 737 CHIPC_EROMPTR); 738 break; 739 case BHND_CHIPTYPE_UBUS: 740 device_printf(dev, "unsupported ubus/bcm63xx chip type"); 741 error = ENODEV; 742 goto cleanup; 743 default: 744 device_printf(dev, "unknown chip type %hhu\n", 745 result->chip_type); 746 error = ENODEV; 747 goto cleanup; 748 } 749 750 cleanup: 751 /* Clean up */ 752 bus_release_resource(dev, rtype, rid, res); 753 return (error); 754 } 755 756 /** 757 * Using the bhnd(4) bus-level core information and a custom core name, 758 * populate @p dev's device description. 759 * 760 * @param dev A bhnd-bus attached device. 761 * @param dev_name The core's name (e.g. "SDIO Device Core") 762 */ 763 void 764 bhnd_set_custom_core_desc(device_t dev, const char *dev_name) 765 { 766 const char *vendor_name; 767 char *desc; 768 769 vendor_name = bhnd_get_vendor_name(dev); 770 asprintf(&desc, M_BHND, "%s %s, rev %hhu", vendor_name, dev_name, 771 bhnd_get_hwrev(dev)); 772 773 if (desc != NULL) { 774 device_set_desc_copy(dev, desc); 775 free(desc, M_BHND); 776 } else { 777 device_set_desc(dev, dev_name); 778 } 779 } 780 781 /** 782 * Using the bhnd(4) bus-level core information, populate @p dev's device 783 * description. 784 * 785 * @param dev A bhnd-bus attached device. 786 */ 787 void 788 bhnd_set_default_core_desc(device_t dev) 789 { 790 bhnd_set_custom_core_desc(dev, bhnd_get_device_name(dev)); 791 } 792 793 /** 794 * Helper function for implementing BHND_BUS_IS_HW_DISABLED(). 795 * 796 * If a parent device is available, this implementation delegates the 797 * request to the BHND_BUS_IS_HW_DISABLED() method on the parent of @p dev. 798 * 799 * If no parent device is available (i.e. on a the bus root), the hardware 800 * is assumed to be usable and false is returned. 801 */ 802 bool 803 bhnd_bus_generic_is_hw_disabled(device_t dev, device_t child) 804 { 805 if (device_get_parent(dev) != NULL) 806 return (BHND_BUS_IS_HW_DISABLED(device_get_parent(dev), child)); 807 808 return (false); 809 } 810 811 /** 812 * Helper function for implementing BHND_BUS_GET_CHIPID(). 813 * 814 * This implementation delegates the request to the BHND_BUS_GET_CHIPID() 815 * method on the parent of @p dev. If no parent exists, the implementation 816 * will panic. 817 */ 818 const struct bhnd_chipid * 819 bhnd_bus_generic_get_chipid(device_t dev, device_t child) 820 { 821 if (device_get_parent(dev) != NULL) 822 return (BHND_BUS_GET_CHIPID(device_get_parent(dev), child)); 823 824 panic("missing BHND_BUS_GET_CHIPID()"); 825 } 826 827 /** 828 * Helper function for implementing BHND_BUS_ALLOC_RESOURCE(). 829 * 830 * This implementation of BHND_BUS_ALLOC_RESOURCE() delegates allocation 831 * of the underlying resource to BUS_ALLOC_RESOURCE(), and activation 832 * to @p dev's BHND_BUS_ACTIVATE_RESOURCE(). 833 */ 834 struct bhnd_resource * 835 bhnd_bus_generic_alloc_resource(device_t dev, device_t child, int type, 836 int *rid, rman_res_t start, rman_res_t end, rman_res_t count, 837 u_int flags) 838 { 839 struct bhnd_resource *br; 840 struct resource *res; 841 int error; 842 843 br = NULL; 844 res = NULL; 845 846 /* Allocate the real bus resource (without activating it) */ 847 res = BUS_ALLOC_RESOURCE(dev, child, type, rid, start, end, count, 848 (flags & ~RF_ACTIVE)); 849 if (res == NULL) 850 return (NULL); 851 852 /* Allocate our bhnd resource wrapper. */ 853 br = malloc(sizeof(struct bhnd_resource), M_BHND, M_NOWAIT); 854 if (br == NULL) 855 goto failed; 856 857 br->direct = false; 858 br->res = res; 859 860 /* Attempt activation */ 861 if (flags & RF_ACTIVE) { 862 error = BHND_BUS_ACTIVATE_RESOURCE(dev, child, type, *rid, br); 863 if (error) 864 goto failed; 865 } 866 867 return (br); 868 869 failed: 870 if (res != NULL) 871 BUS_RELEASE_RESOURCE(dev, child, type, *rid, res); 872 873 free(br, M_BHND); 874 return (NULL); 875 } 876 877 /** 878 * Helper function for implementing BHND_BUS_RELEASE_RESOURCE(). 879 * 880 * This implementation of BHND_BUS_RELEASE_RESOURCE() delegates release of 881 * the backing resource to BUS_RELEASE_RESOURCE(). 882 */ 883 int 884 bhnd_bus_generic_release_resource(device_t dev, device_t child, int type, 885 int rid, struct bhnd_resource *r) 886 { 887 int error; 888 889 if ((error = BUS_RELEASE_RESOURCE(dev, child, type, rid, r->res))) 890 return (error); 891 892 free(r, M_BHND); 893 return (0); 894 } 895 896 897 /** 898 * Helper function for implementing BHND_BUS_ACTIVATE_RESOURCE(). 899 * 900 * This implementation of BHND_BUS_ACTIVATE_RESOURCE() simply calls the 901 * BHND_BUS_ACTIVATE_RESOURCE() method of the parent of @p dev. 902 */ 903 int 904 bhnd_bus_generic_activate_resource(device_t dev, device_t child, int type, 905 int rid, struct bhnd_resource *r) 906 { 907 /* Try to delegate to the parent */ 908 if (device_get_parent(dev) != NULL) 909 return (BHND_BUS_ACTIVATE_RESOURCE(device_get_parent(dev), 910 child, type, rid, r)); 911 912 return (EINVAL); 913 }; 914 915 /** 916 * Helper function for implementing BHND_BUS_DEACTIVATE_RESOURCE(). 917 * 918 * This implementation of BHND_BUS_ACTIVATE_RESOURCE() simply calls the 919 * BHND_BUS_ACTIVATE_RESOURCE() method of the parent of @p dev. 920 */ 921 int 922 bhnd_bus_generic_deactivate_resource(device_t dev, device_t child, 923 int type, int rid, struct bhnd_resource *r) 924 { 925 if (device_get_parent(dev) != NULL) 926 return (BHND_BUS_DEACTIVATE_RESOURCE(device_get_parent(dev), 927 child, type, rid, r)); 928 929 return (EINVAL); 930 }; 931