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 "nvram/bhnd_nvram.h" 45 46 #include "bhnd_chipc_if.h" 47 48 #include "bhnd_nvram_if.h" 49 #include "bhnd_nvram_map.h" 50 51 #include "bhndreg.h" 52 #include "bhndvar.h" 53 54 /* BHND core device description table. */ 55 static const struct bhnd_core_desc { 56 uint16_t vendor; 57 uint16_t device; 58 bhnd_devclass_t class; 59 const char *desc; 60 } bhnd_core_descs[] = { 61 #define BHND_CDESC(_mfg, _cid, _cls, _desc) \ 62 { BHND_MFGID_ ## _mfg, BHND_COREID_ ## _cid, \ 63 BHND_DEVCLASS_ ## _cls, _desc } 64 65 BHND_CDESC(BCM, CC, CC, "ChipCommon I/O Controller"), 66 BHND_CDESC(BCM, ILINE20, OTHER, "iLine20 HPNA"), 67 BHND_CDESC(BCM, SRAM, RAM, "SRAM"), 68 BHND_CDESC(BCM, SDRAM, RAM, "SDRAM"), 69 BHND_CDESC(BCM, PCI, PCI, "PCI Bridge"), 70 BHND_CDESC(BCM, MIPS, CPU, "MIPS Core"), 71 BHND_CDESC(BCM, ENET, ENET_MAC, "Fast Ethernet MAC"), 72 BHND_CDESC(BCM, CODEC, OTHER, "V.90 Modem Codec"), 73 BHND_CDESC(BCM, USB, OTHER, "USB 1.1 Device/Host Controller"), 74 BHND_CDESC(BCM, ADSL, OTHER, "ADSL Core"), 75 BHND_CDESC(BCM, ILINE100, OTHER, "iLine100 HPNA"), 76 BHND_CDESC(BCM, IPSEC, OTHER, "IPsec Accelerator"), 77 BHND_CDESC(BCM, UTOPIA, OTHER, "UTOPIA ATM Core"), 78 BHND_CDESC(BCM, PCMCIA, PCCARD, "PCMCIA Bridge"), 79 BHND_CDESC(BCM, SOCRAM, RAM, "Internal Memory"), 80 BHND_CDESC(BCM, MEMC, MEMC, "MEMC SDRAM Controller"), 81 BHND_CDESC(BCM, OFDM, OTHER, "OFDM PHY"), 82 BHND_CDESC(BCM, EXTIF, OTHER, "External Interface"), 83 BHND_CDESC(BCM, D11, WLAN, "802.11 MAC/PHY/Radio"), 84 BHND_CDESC(BCM, APHY, WLAN_PHY, "802.11a PHY"), 85 BHND_CDESC(BCM, BPHY, WLAN_PHY, "802.11b PHY"), 86 BHND_CDESC(BCM, GPHY, WLAN_PHY, "802.11g PHY"), 87 BHND_CDESC(BCM, MIPS33, CPU, "MIPS3302 Core"), 88 BHND_CDESC(BCM, USB11H, OTHER, "USB 1.1 Host Controller"), 89 BHND_CDESC(BCM, USB11D, OTHER, "USB 1.1 Device Core"), 90 BHND_CDESC(BCM, USB20H, OTHER, "USB 2.0 Host Controller"), 91 BHND_CDESC(BCM, USB20D, OTHER, "USB 2.0 Device Core"), 92 BHND_CDESC(BCM, SDIOH, OTHER, "SDIO Host Controller"), 93 BHND_CDESC(BCM, ROBO, OTHER, "RoboSwitch"), 94 BHND_CDESC(BCM, ATA100, OTHER, "Parallel ATA Controller"), 95 BHND_CDESC(BCM, SATAXOR, OTHER, "SATA DMA/XOR Controller"), 96 BHND_CDESC(BCM, GIGETH, ENET_MAC, "Gigabit Ethernet MAC"), 97 BHND_CDESC(BCM, PCIE, PCIE, "PCIe Bridge"), 98 BHND_CDESC(BCM, NPHY, WLAN_PHY, "802.11n 2x2 PHY"), 99 BHND_CDESC(BCM, SRAMC, MEMC, "SRAM Controller"), 100 BHND_CDESC(BCM, MINIMAC, OTHER, "MINI MAC/PHY"), 101 BHND_CDESC(BCM, ARM11, CPU, "ARM1176 CPU"), 102 BHND_CDESC(BCM, ARM7S, CPU, "ARM7TDMI-S CPU"), 103 BHND_CDESC(BCM, LPPHY, WLAN_PHY, "802.11a/b/g PHY"), 104 BHND_CDESC(BCM, PMU, PMU, "PMU"), 105 BHND_CDESC(BCM, SSNPHY, WLAN_PHY, "802.11n Single-Stream PHY"), 106 BHND_CDESC(BCM, SDIOD, OTHER, "SDIO Device Core"), 107 BHND_CDESC(BCM, ARMCM3, CPU, "ARM Cortex-M3 CPU"), 108 BHND_CDESC(BCM, HTPHY, WLAN_PHY, "802.11n 4x4 PHY"), 109 BHND_CDESC(MIPS,MIPS74K, CPU, "MIPS74k CPU"), 110 BHND_CDESC(BCM, GMAC, ENET_MAC, "Gigabit MAC core"), 111 BHND_CDESC(BCM, DMEMC, MEMC, "DDR1/DDR2 Memory Controller"), 112 BHND_CDESC(BCM, PCIERC, OTHER, "PCIe Root Complex"), 113 BHND_CDESC(BCM, OCP, SOC_BRIDGE, "OCP to OCP Bridge"), 114 BHND_CDESC(BCM, SC, OTHER, "Shared Common Core"), 115 BHND_CDESC(BCM, AHB, SOC_BRIDGE, "OCP to AHB Bridge"), 116 BHND_CDESC(BCM, SPIH, OTHER, "SPI Host Controller"), 117 BHND_CDESC(BCM, I2S, OTHER, "I2S Digital Audio Interface"), 118 BHND_CDESC(BCM, DMEMS, MEMC, "SDR/DDR1 Memory Controller"), 119 BHND_CDESC(BCM, UBUS_SHIM, OTHER, "BCM6362/UBUS WLAN SHIM"), 120 BHND_CDESC(BCM, PCIE2, PCIE, "PCIe Bridge (Gen2)"), 121 122 BHND_CDESC(ARM, APB_BRIDGE, SOC_BRIDGE, "BP135 AMBA3 AXI to APB Bridge"), 123 BHND_CDESC(ARM, PL301, SOC_ROUTER, "PL301 AMBA3 Interconnect"), 124 BHND_CDESC(ARM, EROM, EROM, "PL366 Device Enumeration ROM"), 125 BHND_CDESC(ARM, OOB_ROUTER, OTHER, "PL367 OOB Interrupt Router"), 126 BHND_CDESC(ARM, AXI_UNMAPPED, OTHER, "Unmapped Address Ranges"), 127 128 BHND_CDESC(BCM, 4706_CC, CC, "ChipCommon I/O Controller"), 129 BHND_CDESC(BCM, NS_PCIE2, PCIE, "PCIe Bridge (Gen2)"), 130 BHND_CDESC(BCM, NS_DMA, OTHER, "DMA engine"), 131 BHND_CDESC(BCM, NS_SDIO, OTHER, "SDIO 3.0 Host Controller"), 132 BHND_CDESC(BCM, NS_USB20H, OTHER, "USB 2.0 Host Controller"), 133 BHND_CDESC(BCM, NS_USB30H, OTHER, "USB 3.0 Host Controller"), 134 BHND_CDESC(BCM, NS_A9JTAG, OTHER, "ARM Cortex A9 JTAG Interface"), 135 BHND_CDESC(BCM, NS_DDR23_MEMC, MEMC, "Denali DDR2/DD3 Memory Controller"), 136 BHND_CDESC(BCM, NS_ROM, NVRAM, "System ROM"), 137 BHND_CDESC(BCM, NS_NAND, NVRAM, "NAND Flash Controller"), 138 BHND_CDESC(BCM, NS_QSPI, NVRAM, "QSPI Flash Controller"), 139 BHND_CDESC(BCM, NS_CC_B, CC_B, "ChipCommon B Auxiliary I/O Controller"), 140 BHND_CDESC(BCM, 4706_SOCRAM, RAM, "Internal Memory"), 141 BHND_CDESC(BCM, IHOST_ARMCA9, CPU, "ARM Cortex A9 CPU"), 142 BHND_CDESC(BCM, 4706_GMAC_CMN, ENET, "Gigabit MAC (Common)"), 143 BHND_CDESC(BCM, 4706_GMAC, ENET_MAC, "Gigabit MAC"), 144 BHND_CDESC(BCM, AMEMC, MEMC, "Denali DDR1/DDR2 Memory Controller"), 145 #undef BHND_CDESC 146 147 /* Derived from inspection of the BCM4331 cores that provide PrimeCell 148 * IDs. Due to lack of documentation, the surmised device name/purpose 149 * provided here may be incorrect. */ 150 { BHND_MFGID_ARM, BHND_PRIMEID_EROM, BHND_DEVCLASS_OTHER, 151 "PL364 Device Enumeration ROM" }, 152 { BHND_MFGID_ARM, BHND_PRIMEID_SWRAP, BHND_DEVCLASS_OTHER, 153 "PL368 Device Management Interface" }, 154 { BHND_MFGID_ARM, BHND_PRIMEID_MWRAP, BHND_DEVCLASS_OTHER, 155 "PL369 Device Management Interface" }, 156 157 { 0, 0, 0, NULL } 158 }; 159 160 /** 161 * Return the name for a given JEP106 manufacturer ID. 162 * 163 * @param vendor A JEP106 Manufacturer ID, including the non-standard ARM 4-bit 164 * JEP106 continuation code. 165 */ 166 const char * 167 bhnd_vendor_name(uint16_t vendor) 168 { 169 switch (vendor) { 170 case BHND_MFGID_ARM: 171 return "ARM"; 172 case BHND_MFGID_BCM: 173 return "Broadcom"; 174 case BHND_MFGID_MIPS: 175 return "MIPS"; 176 default: 177 return "unknown"; 178 } 179 } 180 181 /** 182 * Return the name of a port type. 183 */ 184 const char * 185 bhnd_port_type_name(bhnd_port_type port_type) 186 { 187 switch (port_type) { 188 case BHND_PORT_DEVICE: 189 return ("device"); 190 case BHND_PORT_BRIDGE: 191 return ("bridge"); 192 case BHND_PORT_AGENT: 193 return ("agent"); 194 default: 195 return "unknown"; 196 } 197 } 198 199 /** 200 * Return the name of an NVRAM source. 201 */ 202 const char * 203 bhnd_nvram_src_name(bhnd_nvram_src nvram_src) 204 { 205 switch (nvram_src) { 206 case BHND_NVRAM_SRC_FLASH: 207 return ("flash"); 208 case BHND_NVRAM_SRC_OTP: 209 return ("OTP"); 210 case BHND_NVRAM_SRC_SPROM: 211 return ("SPROM"); 212 case BHND_NVRAM_SRC_UNKNOWN: 213 return ("none"); 214 default: 215 return ("unknown"); 216 } 217 } 218 219 static const struct bhnd_core_desc * 220 bhnd_find_core_desc(uint16_t vendor, uint16_t device) 221 { 222 for (u_int i = 0; bhnd_core_descs[i].desc != NULL; i++) { 223 if (bhnd_core_descs[i].vendor != vendor) 224 continue; 225 226 if (bhnd_core_descs[i].device != device) 227 continue; 228 229 return (&bhnd_core_descs[i]); 230 } 231 232 return (NULL); 233 } 234 235 /** 236 * Return a human-readable name for a BHND core. 237 * 238 * @param vendor The core designer's JEDEC-106 Manufacturer ID 239 * @param device The core identifier. 240 */ 241 const char * 242 bhnd_find_core_name(uint16_t vendor, uint16_t device) 243 { 244 const struct bhnd_core_desc *desc; 245 246 if ((desc = bhnd_find_core_desc(vendor, device)) == NULL) 247 return ("unknown"); 248 249 return desc->desc; 250 } 251 252 /** 253 * Return the device class for a BHND core. 254 * 255 * @param vendor The core designer's JEDEC-106 Manufacturer ID 256 * @param device The core identifier. 257 */ 258 bhnd_devclass_t 259 bhnd_find_core_class(uint16_t vendor, uint16_t device) 260 { 261 const struct bhnd_core_desc *desc; 262 263 if ((desc = bhnd_find_core_desc(vendor, device)) == NULL) 264 return (BHND_DEVCLASS_OTHER); 265 266 return desc->class; 267 } 268 269 /** 270 * Return a human-readable name for a BHND core. 271 * 272 * @param ci The core's info record. 273 */ 274 const char * 275 bhnd_core_name(const struct bhnd_core_info *ci) 276 { 277 return bhnd_find_core_name(ci->vendor, ci->device); 278 } 279 280 /** 281 * Return the device class for a BHND core. 282 * 283 * @param ci The core's info record. 284 */ 285 bhnd_devclass_t 286 bhnd_core_class(const struct bhnd_core_info *ci) 287 { 288 return bhnd_find_core_class(ci->vendor, ci->device); 289 } 290 291 /** 292 * Initialize a core info record with data from from a bhnd-attached @p dev. 293 * 294 * @param dev A bhnd device. 295 * @param core The record to be initialized. 296 */ 297 struct bhnd_core_info 298 bhnd_get_core_info(device_t dev) { 299 return (struct bhnd_core_info) { 300 .vendor = bhnd_get_vendor(dev), 301 .device = bhnd_get_device(dev), 302 .hwrev = bhnd_get_hwrev(dev), 303 .core_idx = bhnd_get_core_index(dev), 304 .unit = bhnd_get_core_unit(dev) 305 }; 306 } 307 308 /** 309 * Find a @p class child device with @p unit on @p dev. 310 * 311 * @param parent The bhnd-compatible bus to be searched. 312 * @param class The device class to match on. 313 * @param unit The core unit number; specify -1 to return the first match 314 * regardless of unit number. 315 * 316 * @retval device_t if a matching child device is found. 317 * @retval NULL if no matching child device is found. 318 */ 319 device_t 320 bhnd_find_child(device_t dev, bhnd_devclass_t class, int unit) 321 { 322 struct bhnd_core_match md = { 323 BHND_MATCH_CORE_CLASS(class), 324 BHND_MATCH_CORE_UNIT(unit) 325 }; 326 327 if (unit == -1) 328 md.m.match.core_unit = 0; 329 330 return bhnd_match_child(dev, &md); 331 } 332 333 /** 334 * Find the first child device on @p dev that matches @p desc. 335 * 336 * @param parent The bhnd-compatible bus to be searched. 337 * @param desc A match descriptor. 338 * 339 * @retval device_t if a matching child device is found. 340 * @retval NULL if no matching child device is found. 341 */ 342 device_t 343 bhnd_match_child(device_t dev, const struct bhnd_core_match *desc) 344 { 345 device_t *devlistp; 346 device_t match; 347 int devcnt; 348 int error; 349 350 error = device_get_children(dev, &devlistp, &devcnt); 351 if (error != 0) 352 return (NULL); 353 354 match = NULL; 355 for (int i = 0; i < devcnt; i++) { 356 struct bhnd_core_info ci = bhnd_get_core_info(devlistp[i]); 357 358 if (bhnd_core_matches(&ci, desc)) { 359 match = devlistp[i]; 360 goto done; 361 } 362 } 363 364 done: 365 free(devlistp, M_TEMP); 366 return match; 367 } 368 369 /** 370 * Walk up the bhnd device hierarchy to locate the root device 371 * to which the bhndb bridge is attached. 372 * 373 * This can be used from within bhnd host bridge drivers to locate the 374 * actual upstream host device. 375 * 376 * @param dev A bhnd device. 377 * @param bus_class The expected bus (e.g. "pci") to which the bridge root 378 * should be attached. 379 * 380 * @retval device_t if a matching parent device is found. 381 * @retval NULL @p dev is not attached via a bhndb bus 382 * @retval NULL no parent device is attached via @p bus_class. 383 */ 384 device_t 385 bhnd_find_bridge_root(device_t dev, devclass_t bus_class) 386 { 387 devclass_t bhndb_class; 388 device_t parent; 389 390 KASSERT(device_get_devclass(device_get_parent(dev)) == bhnd_devclass, 391 ("%s not a bhnd device", device_get_nameunit(dev))); 392 393 bhndb_class = devclass_find("bhndb"); 394 395 /* Walk the device tree until we hit a bridge */ 396 parent = dev; 397 while ((parent = device_get_parent(parent)) != NULL) { 398 if (device_get_devclass(parent) == bhndb_class) 399 break; 400 } 401 402 /* No bridge? */ 403 if (parent == NULL) 404 return (NULL); 405 406 /* Search for a parent attached to the expected bus class */ 407 while ((parent = device_get_parent(parent)) != NULL) { 408 device_t bus; 409 410 bus = device_get_parent(parent); 411 if (bus != NULL && device_get_devclass(bus) == bus_class) 412 return (parent); 413 } 414 415 /* Not found */ 416 return (NULL); 417 } 418 419 /** 420 * Find the first core in @p cores that matches @p desc. 421 * 422 * @param cores The table to search. 423 * @param num_cores The length of @p cores. 424 * @param desc A match descriptor. 425 * 426 * @retval bhnd_core_info if a matching core is found. 427 * @retval NULL if no matching core is found. 428 */ 429 const struct bhnd_core_info * 430 bhnd_match_core(const struct bhnd_core_info *cores, u_int num_cores, 431 const struct bhnd_core_match *desc) 432 { 433 for (u_int i = 0; i < num_cores; i++) { 434 if (bhnd_core_matches(&cores[i], desc)) 435 return &cores[i]; 436 } 437 438 return (NULL); 439 } 440 441 442 /** 443 * Find the first core in @p cores with the given @p class. 444 * 445 * @param cores The table to search. 446 * @param num_cores The length of @p cores. 447 * @param desc A match descriptor. 448 * 449 * @retval bhnd_core_info if a matching core is found. 450 * @retval NULL if no matching core is found. 451 */ 452 const struct bhnd_core_info * 453 bhnd_find_core(const struct bhnd_core_info *cores, u_int num_cores, 454 bhnd_devclass_t class) 455 { 456 struct bhnd_core_match md = { 457 BHND_MATCH_CORE_CLASS(class) 458 }; 459 460 return bhnd_match_core(cores, num_cores, &md); 461 } 462 463 /** 464 * Return true if the @p core matches @p desc. 465 * 466 * @param core A bhnd core descriptor. 467 * @param desc A match descriptor to compare against @p core. 468 * 469 * @retval true if @p core matches @p match 470 * @retval false if @p core does not match @p match. 471 */ 472 bool 473 bhnd_core_matches(const struct bhnd_core_info *core, 474 const struct bhnd_core_match *desc) 475 { 476 if (desc->m.match.core_vendor && desc->core_vendor != core->vendor) 477 return (false); 478 479 if (desc->m.match.core_id && desc->core_id != core->device) 480 return (false); 481 482 if (desc->m.match.core_unit && desc->core_unit != core->unit) 483 return (false); 484 485 if (desc->m.match.core_rev && 486 !bhnd_hwrev_matches(core->hwrev, &desc->core_rev)) 487 return (false); 488 489 if (desc->m.match.core_class && 490 desc->core_class != bhnd_core_class(core)) 491 return (false); 492 493 return true; 494 } 495 496 /** 497 * Return true if the @p chip matches @p desc. 498 * 499 * @param chip A bhnd chip identifier. 500 * @param desc A match descriptor to compare against @p chip. 501 * 502 * @retval true if @p chip matches @p match 503 * @retval false if @p chip does not match @p match. 504 */ 505 bool 506 bhnd_chip_matches(const struct bhnd_chipid *chip, 507 const struct bhnd_chip_match *desc) 508 { 509 if (desc->m.match.chip_id && chip->chip_id != desc->chip_id) 510 return (false); 511 512 if (desc->m.match.chip_pkg && chip->chip_pkg != desc->chip_pkg) 513 return (false); 514 515 if (desc->m.match.chip_rev && 516 !bhnd_hwrev_matches(chip->chip_rev, &desc->chip_rev)) 517 return (false); 518 519 return (true); 520 } 521 522 /** 523 * Return true if the @p board matches @p desc. 524 * 525 * @param board The bhnd board info. 526 * @param desc A match descriptor to compare against @p board. 527 * 528 * @retval true if @p chip matches @p match 529 * @retval false if @p chip does not match @p match. 530 */ 531 bool 532 bhnd_board_matches(const struct bhnd_board_info *board, 533 const struct bhnd_board_match *desc) 534 { 535 if (desc->m.match.board_srom_rev && 536 !bhnd_hwrev_matches(board->board_srom_rev, &desc->board_srom_rev)) 537 return (false); 538 539 if (desc->m.match.board_vendor && 540 board->board_vendor != desc->board_vendor) 541 return (false); 542 543 if (desc->m.match.board_type && board->board_type != desc->board_type) 544 return (false); 545 546 if (desc->m.match.board_rev && 547 !bhnd_hwrev_matches(board->board_rev, &desc->board_rev)) 548 return (false); 549 550 return (true); 551 } 552 553 /** 554 * Return true if the @p hwrev matches @p desc. 555 * 556 * @param hwrev A bhnd hardware revision. 557 * @param desc A match descriptor to compare against @p core. 558 * 559 * @retval true if @p hwrev matches @p match 560 * @retval false if @p hwrev does not match @p match. 561 */ 562 bool 563 bhnd_hwrev_matches(uint16_t hwrev, const struct bhnd_hwrev_match *desc) 564 { 565 if (desc->start != BHND_HWREV_INVALID && 566 desc->start > hwrev) 567 return false; 568 569 if (desc->end != BHND_HWREV_INVALID && 570 desc->end < hwrev) 571 return false; 572 573 return true; 574 } 575 576 /** 577 * Return true if the @p dev matches @p desc. 578 * 579 * @param dev A bhnd device. 580 * @param desc A match descriptor to compare against @p dev. 581 * 582 * @retval true if @p dev matches @p match 583 * @retval false if @p dev does not match @p match. 584 */ 585 bool 586 bhnd_device_matches(device_t dev, const struct bhnd_device_match *desc) 587 { 588 struct bhnd_core_info core; 589 const struct bhnd_chipid *chip; 590 struct bhnd_board_info board; 591 device_t parent; 592 int error; 593 594 /* Construct individual match descriptors */ 595 struct bhnd_core_match m_core = { _BHND_CORE_MATCH_COPY(desc) }; 596 struct bhnd_chip_match m_chip = { _BHND_CHIP_MATCH_COPY(desc) }; 597 struct bhnd_board_match m_board = { _BHND_BOARD_MATCH_COPY(desc) }; 598 599 /* Fetch and match core info */ 600 if (m_core.m.match_flags) { 601 /* Only applicable to bhnd-attached cores */ 602 parent = device_get_parent(dev); 603 if (device_get_devclass(parent) != bhnd_devclass) { 604 device_printf(dev, "attempting to match core " 605 "attributes against non-core device\n"); 606 return (false); 607 } 608 609 core = bhnd_get_core_info(dev); 610 if (!bhnd_core_matches(&core, &m_core)) 611 return (false); 612 } 613 614 /* Fetch and match chip info */ 615 if (m_chip.m.match_flags) { 616 chip = bhnd_get_chipid(dev); 617 618 if (!bhnd_chip_matches(chip, &m_chip)) 619 return (false); 620 } 621 622 /* Fetch and match board info. 623 * 624 * This is not available until after NVRAM is up; earlier device 625 * matches should not include board requirements */ 626 if (m_board.m.match_flags) { 627 if ((error = bhnd_read_board_info(dev, &board))) { 628 device_printf(dev, "failed to read required board info " 629 "during device matching: %d\n", error); 630 return (false); 631 } 632 633 if (!bhnd_board_matches(&board, &m_board)) 634 return (false); 635 } 636 637 /* All matched */ 638 return (true); 639 } 640 641 /** 642 * Search @p table for an entry matching @p dev. 643 * 644 * @param dev A bhnd device to match against @p table. 645 * @param table The device table to search. 646 * @param entry_size The @p table entry size, in bytes. 647 * 648 * @retval bhnd_device the first matching device, if any. 649 * @retval NULL if no matching device is found in @p table. 650 */ 651 const struct bhnd_device * 652 bhnd_device_lookup(device_t dev, const struct bhnd_device *table, 653 size_t entry_size) 654 { 655 const struct bhnd_device *entry; 656 device_t hostb, parent; 657 bhnd_attach_type attach_type; 658 uint32_t dflags; 659 660 parent = device_get_parent(dev); 661 hostb = bhnd_find_hostb_device(parent); 662 attach_type = bhnd_get_attach_type(dev); 663 664 for (entry = table; !BHND_DEVICE_IS_END(entry); entry = 665 (const struct bhnd_device *) ((const char *) entry + entry_size)) 666 { 667 /* match core info */ 668 if (!bhnd_device_matches(dev, &entry->core)) 669 continue; 670 671 /* match device flags */ 672 dflags = entry->device_flags; 673 674 /* hostb implies BHND_ATTACH_ADAPTER requirement */ 675 if (dflags & BHND_DF_HOSTB) 676 dflags |= BHND_DF_ADAPTER; 677 678 if (dflags & BHND_DF_ADAPTER) 679 if (attach_type != BHND_ATTACH_ADAPTER) 680 continue; 681 682 if (dflags & BHND_DF_HOSTB) 683 if (dev != hostb) 684 continue; 685 686 if (dflags & BHND_DF_SOC) 687 if (attach_type != BHND_ATTACH_NATIVE) 688 continue; 689 690 /* device found */ 691 return (entry); 692 } 693 694 /* not found */ 695 return (NULL); 696 } 697 698 /** 699 * Scan the device @p table for all quirk flags applicable to @p dev. 700 * 701 * @param dev A bhnd device to match against @p table. 702 * @param table The device table to search. 703 * 704 * @return returns all matching quirk flags. 705 */ 706 uint32_t 707 bhnd_device_quirks(device_t dev, const struct bhnd_device *table, 708 size_t entry_size) 709 { 710 const struct bhnd_device *dent; 711 const struct bhnd_device_quirk *qent, *qtable; 712 uint32_t quirks; 713 714 /* Locate the device entry */ 715 if ((dent = bhnd_device_lookup(dev, table, entry_size)) == NULL) 716 return (0); 717 718 /* Quirks table is optional */ 719 qtable = dent->quirks_table; 720 if (qtable == NULL) 721 return (0); 722 723 /* Collect matching device quirk entries */ 724 quirks = 0; 725 for (qent = qtable; !BHND_DEVICE_QUIRK_IS_END(qent); qent++) { 726 if (bhnd_device_matches(dev, &qent->desc)) 727 quirks |= qent->quirks; 728 } 729 730 return (quirks); 731 } 732 733 734 /** 735 * Allocate bhnd(4) resources defined in @p rs from a parent bus. 736 * 737 * @param dev The device requesting ownership of the resources. 738 * @param rs A standard bus resource specification. This will be updated 739 * with the allocated resource's RIDs. 740 * @param res On success, the allocated bhnd resources. 741 * 742 * @retval 0 success 743 * @retval non-zero if allocation of any non-RF_OPTIONAL resource fails, 744 * all allocated resources will be released and a regular 745 * unix error code will be returned. 746 */ 747 int 748 bhnd_alloc_resources(device_t dev, struct resource_spec *rs, 749 struct bhnd_resource **res) 750 { 751 /* Initialize output array */ 752 for (u_int i = 0; rs[i].type != -1; i++) 753 res[i] = NULL; 754 755 for (u_int i = 0; rs[i].type != -1; i++) { 756 res[i] = bhnd_alloc_resource_any(dev, rs[i].type, &rs[i].rid, 757 rs[i].flags); 758 759 /* Clean up all allocations on failure */ 760 if (res[i] == NULL && !(rs[i].flags & RF_OPTIONAL)) { 761 bhnd_release_resources(dev, rs, res); 762 return (ENXIO); 763 } 764 } 765 766 return (0); 767 }; 768 769 /** 770 * Release bhnd(4) resources defined in @p rs from a parent bus. 771 * 772 * @param dev The device that owns the resources. 773 * @param rs A standard bus resource specification previously initialized 774 * by @p bhnd_alloc_resources. 775 * @param res The bhnd resources to be released. 776 */ 777 void 778 bhnd_release_resources(device_t dev, const struct resource_spec *rs, 779 struct bhnd_resource **res) 780 { 781 for (u_int i = 0; rs[i].type != -1; i++) { 782 if (res[i] == NULL) 783 continue; 784 785 bhnd_release_resource(dev, rs[i].type, rs[i].rid, res[i]); 786 res[i] = NULL; 787 } 788 } 789 790 /** 791 * Parse the CHIPC_ID_* fields from the ChipCommon CHIPC_ID 792 * register, returning its bhnd_chipid representation. 793 * 794 * @param idreg The CHIPC_ID register value. 795 * @param enum_addr The enumeration address to include in the result. 796 * 797 * @warning 798 * On early siba(4) devices, the ChipCommon core does not provide 799 * a valid CHIPC_ID_NUMCORE field. On these ChipCommon revisions 800 * (see CHIPC_NCORES_MIN_HWREV()), this function will parse and return 801 * an invalid `ncores` value. 802 */ 803 struct bhnd_chipid 804 bhnd_parse_chipid(uint32_t idreg, bhnd_addr_t enum_addr) 805 { 806 struct bhnd_chipid result; 807 808 /* Fetch the basic chip info */ 809 result.chip_id = CHIPC_GET_BITS(idreg, CHIPC_ID_CHIP); 810 result.chip_pkg = CHIPC_GET_BITS(idreg, CHIPC_ID_PKG); 811 result.chip_rev = CHIPC_GET_BITS(idreg, CHIPC_ID_REV); 812 result.chip_type = CHIPC_GET_BITS(idreg, CHIPC_ID_BUS); 813 result.ncores = CHIPC_GET_BITS(idreg, CHIPC_ID_NUMCORE); 814 815 result.enum_addr = enum_addr; 816 817 return (result); 818 } 819 820 /** 821 * Allocate the resource defined by @p rs via @p dev, use it 822 * to read the ChipCommon ID register relative to @p chipc_offset, 823 * then release the resource. 824 * 825 * @param dev The device owning @p rs. 826 * @param rs A resource spec that encompasses the ChipCommon register block. 827 * @param chipc_offset The offset of the ChipCommon registers within @p rs. 828 * @param[out] result the chip identification data. 829 * 830 * @retval 0 success 831 * @retval non-zero if the ChipCommon identification data could not be read. 832 */ 833 int 834 bhnd_read_chipid(device_t dev, struct resource_spec *rs, 835 bus_size_t chipc_offset, struct bhnd_chipid *result) 836 { 837 struct resource *res; 838 uint32_t reg; 839 int error, rid, rtype; 840 841 /* Allocate the ChipCommon window resource and fetch the chipid data */ 842 rid = rs->rid; 843 rtype = rs->type; 844 res = bus_alloc_resource_any(dev, rtype, &rid, RF_ACTIVE); 845 if (res == NULL) { 846 device_printf(dev, 847 "failed to allocate bhnd chipc resource\n"); 848 return (ENXIO); 849 } 850 851 /* Fetch the basic chip info */ 852 reg = bus_read_4(res, chipc_offset + CHIPC_ID); 853 *result = bhnd_parse_chipid(reg, 0x0); 854 855 /* Fetch the enum base address */ 856 error = 0; 857 switch (result->chip_type) { 858 case BHND_CHIPTYPE_SIBA: 859 result->enum_addr = BHND_DEFAULT_CHIPC_ADDR; 860 break; 861 case BHND_CHIPTYPE_BCMA: 862 case BHND_CHIPTYPE_BCMA_ALT: 863 result->enum_addr = bus_read_4(res, chipc_offset + 864 CHIPC_EROMPTR); 865 break; 866 case BHND_CHIPTYPE_UBUS: 867 device_printf(dev, "unsupported ubus/bcm63xx chip type"); 868 error = ENODEV; 869 goto cleanup; 870 default: 871 device_printf(dev, "unknown chip type %hhu\n", 872 result->chip_type); 873 error = ENODEV; 874 goto cleanup; 875 } 876 877 cleanup: 878 /* Clean up */ 879 bus_release_resource(dev, rtype, rid, res); 880 return (error); 881 } 882 883 /** 884 * Using the bhnd(4) bus-level core information and a custom core name, 885 * populate @p dev's device description. 886 * 887 * @param dev A bhnd-bus attached device. 888 * @param dev_name The core's name (e.g. "SDIO Device Core") 889 */ 890 void 891 bhnd_set_custom_core_desc(device_t dev, const char *dev_name) 892 { 893 const char *vendor_name; 894 char *desc; 895 896 vendor_name = bhnd_get_vendor_name(dev); 897 asprintf(&desc, M_BHND, "%s %s, rev %hhu", vendor_name, dev_name, 898 bhnd_get_hwrev(dev)); 899 900 if (desc != NULL) { 901 device_set_desc_copy(dev, desc); 902 free(desc, M_BHND); 903 } else { 904 device_set_desc(dev, dev_name); 905 } 906 } 907 908 /** 909 * Using the bhnd(4) bus-level core information, populate @p dev's device 910 * description. 911 * 912 * @param dev A bhnd-bus attached device. 913 */ 914 void 915 bhnd_set_default_core_desc(device_t dev) 916 { 917 bhnd_set_custom_core_desc(dev, bhnd_get_device_name(dev)); 918 } 919 920 /** 921 * Helper function for implementing BHND_BUS_IS_HW_DISABLED(). 922 * 923 * If a parent device is available, this implementation delegates the 924 * request to the BHND_BUS_IS_HW_DISABLED() method on the parent of @p dev. 925 * 926 * If no parent device is available (i.e. on a the bus root), the hardware 927 * is assumed to be usable and false is returned. 928 */ 929 bool 930 bhnd_bus_generic_is_hw_disabled(device_t dev, device_t child) 931 { 932 if (device_get_parent(dev) != NULL) 933 return (BHND_BUS_IS_HW_DISABLED(device_get_parent(dev), child)); 934 935 return (false); 936 } 937 938 /** 939 * Helper function for implementing BHND_BUS_GET_CHIPID(). 940 * 941 * This implementation delegates the request to the BHND_BUS_GET_CHIPID() 942 * method on the parent of @p dev. If no parent exists, the implementation 943 * will panic. 944 */ 945 const struct bhnd_chipid * 946 bhnd_bus_generic_get_chipid(device_t dev, device_t child) 947 { 948 if (device_get_parent(dev) != NULL) 949 return (BHND_BUS_GET_CHIPID(device_get_parent(dev), child)); 950 951 panic("missing BHND_BUS_GET_CHIPID()"); 952 } 953 954 /* nvram board_info population macros for bhnd_bus_generic_read_board_info() */ 955 #define BHND_GV(_dest, _name) \ 956 bhnd_nvram_getvar(child, BHND_NVAR_ ## _name, &_dest, sizeof(_dest)) 957 958 #define REQ_BHND_GV(_dest, _name) do { \ 959 if ((error = BHND_GV(_dest, _name))) { \ 960 device_printf(dev, \ 961 "error reading " __STRING(_name) ": %d\n", error); \ 962 return (error); \ 963 } \ 964 } while(0) 965 966 #define OPT_BHND_GV(_dest, _name, _default) do { \ 967 if ((error = BHND_GV(_dest, _name))) { \ 968 if (error != ENOENT) { \ 969 device_printf(dev, \ 970 "error reading " \ 971 __STRING(_name) ": %d\n", error); \ 972 return (error); \ 973 } \ 974 _dest = _default; \ 975 } \ 976 } while(0) 977 978 /** 979 * Helper function for implementing BHND_BUS_READ_BOARDINFO(). 980 * 981 * This implementation populates @p info with information from NVRAM, 982 * defaulting board_vendor and board_type fields to 0 if the 983 * requested variables cannot be found. 984 * 985 * This behavior is correct for most SoCs, but must be overridden on 986 * bridged (PCI, PCMCIA, etc) devices to produce a complete bhnd_board_info 987 * result. 988 */ 989 int 990 bhnd_bus_generic_read_board_info(device_t dev, device_t child, 991 struct bhnd_board_info *info) 992 { 993 int error; 994 995 OPT_BHND_GV(info->board_vendor, BOARDVENDOR, 0); 996 OPT_BHND_GV(info->board_type, BOARDTYPE, 0); /* srom >= 2 */ 997 REQ_BHND_GV(info->board_rev, BOARDREV); 998 REQ_BHND_GV(info->board_srom_rev,SROMREV); 999 REQ_BHND_GV(info->board_flags, BOARDFLAGS); 1000 OPT_BHND_GV(info->board_flags2, BOARDFLAGS2, 0); /* srom >= 4 */ 1001 OPT_BHND_GV(info->board_flags3, BOARDFLAGS3, 0); /* srom >= 11 */ 1002 1003 return (0); 1004 } 1005 1006 #undef BHND_GV 1007 #undef BHND_GV_REQ 1008 #undef BHND_GV_OPT 1009 1010 /** 1011 * Helper function for implementing BHND_BUS_GET_NVRAM_VAR(). 1012 * 1013 * This implementation searches @p dev for a usable NVRAM child device. 1014 * 1015 * If no usable child device is found on @p dev, the request is delegated to 1016 * the BHND_BUS_GET_NVRAM_VAR() method on the parent of @p dev. 1017 */ 1018 int 1019 bhnd_bus_generic_get_nvram_var(device_t dev, device_t child, const char *name, 1020 void *buf, size_t *size) 1021 { 1022 device_t nvram; 1023 device_t parent; 1024 1025 /* Make sure we're holding Giant for newbus */ 1026 GIANT_REQUIRED; 1027 1028 /* Look for a directly-attached NVRAM child */ 1029 if ((nvram = device_find_child(dev, "bhnd_nvram", -1)) != NULL) 1030 return BHND_NVRAM_GETVAR(nvram, name, buf, size); 1031 1032 /* Try to delegate to parent */ 1033 if ((parent = device_get_parent(dev)) == NULL) 1034 return (ENODEV); 1035 1036 return (BHND_BUS_GET_NVRAM_VAR(device_get_parent(dev), child, 1037 name, buf, size)); 1038 } 1039 1040 /** 1041 * Helper function for implementing BHND_BUS_ALLOC_RESOURCE(). 1042 * 1043 * This implementation of BHND_BUS_ALLOC_RESOURCE() delegates allocation 1044 * of the underlying resource to BUS_ALLOC_RESOURCE(), and activation 1045 * to @p dev's BHND_BUS_ACTIVATE_RESOURCE(). 1046 */ 1047 struct bhnd_resource * 1048 bhnd_bus_generic_alloc_resource(device_t dev, device_t child, int type, 1049 int *rid, rman_res_t start, rman_res_t end, rman_res_t count, 1050 u_int flags) 1051 { 1052 struct bhnd_resource *br; 1053 struct resource *res; 1054 int error; 1055 1056 br = NULL; 1057 res = NULL; 1058 1059 /* Allocate the real bus resource (without activating it) */ 1060 res = BUS_ALLOC_RESOURCE(dev, child, type, rid, start, end, count, 1061 (flags & ~RF_ACTIVE)); 1062 if (res == NULL) 1063 return (NULL); 1064 1065 /* Allocate our bhnd resource wrapper. */ 1066 br = malloc(sizeof(struct bhnd_resource), M_BHND, M_NOWAIT); 1067 if (br == NULL) 1068 goto failed; 1069 1070 br->direct = false; 1071 br->res = res; 1072 1073 /* Attempt activation */ 1074 if (flags & RF_ACTIVE) { 1075 error = BHND_BUS_ACTIVATE_RESOURCE(dev, child, type, *rid, br); 1076 if (error) 1077 goto failed; 1078 } 1079 1080 return (br); 1081 1082 failed: 1083 if (res != NULL) 1084 BUS_RELEASE_RESOURCE(dev, child, type, *rid, res); 1085 1086 free(br, M_BHND); 1087 return (NULL); 1088 } 1089 1090 /** 1091 * Helper function for implementing BHND_BUS_RELEASE_RESOURCE(). 1092 * 1093 * This implementation of BHND_BUS_RELEASE_RESOURCE() delegates release of 1094 * the backing resource to BUS_RELEASE_RESOURCE(). 1095 */ 1096 int 1097 bhnd_bus_generic_release_resource(device_t dev, device_t child, int type, 1098 int rid, struct bhnd_resource *r) 1099 { 1100 int error; 1101 1102 if ((error = BUS_RELEASE_RESOURCE(dev, child, type, rid, r->res))) 1103 return (error); 1104 1105 free(r, M_BHND); 1106 return (0); 1107 } 1108 1109 1110 /** 1111 * Helper function for implementing BHND_BUS_ACTIVATE_RESOURCE(). 1112 * 1113 * This implementation of BHND_BUS_ACTIVATE_RESOURCE() simply calls the 1114 * BHND_BUS_ACTIVATE_RESOURCE() method of the parent of @p dev. 1115 */ 1116 int 1117 bhnd_bus_generic_activate_resource(device_t dev, device_t child, int type, 1118 int rid, struct bhnd_resource *r) 1119 { 1120 /* Try to delegate to the parent */ 1121 if (device_get_parent(dev) != NULL) 1122 return (BHND_BUS_ACTIVATE_RESOURCE(device_get_parent(dev), 1123 child, type, rid, r)); 1124 1125 return (EINVAL); 1126 }; 1127 1128 /** 1129 * Helper function for implementing BHND_BUS_DEACTIVATE_RESOURCE(). 1130 * 1131 * This implementation of BHND_BUS_ACTIVATE_RESOURCE() simply calls the 1132 * BHND_BUS_ACTIVATE_RESOURCE() method of the parent of @p dev. 1133 */ 1134 int 1135 bhnd_bus_generic_deactivate_resource(device_t dev, device_t child, 1136 int type, int rid, struct bhnd_resource *r) 1137 { 1138 if (device_get_parent(dev) != NULL) 1139 return (BHND_BUS_DEACTIVATE_RESOURCE(device_get_parent(dev), 1140 child, type, rid, r)); 1141 1142 return (EINVAL); 1143 }; 1144 1145