| 1 |
|
| 2 |
|
| 3 |
#include <acpi/acpigen.h> |
| 4 |
#include <acpi/acpi_device.h> |
| 5 |
#include <acpi/acpi_pld.h> |
| 6 |
#include <console/console.h> |
| 7 |
#include <device/device.h> |
| 8 |
#include <drivers/usb/acpi/chip.h> |
| 9 |
#include <gpio.h> |
| 10 |
#include <stdio.h> |
| 11 |
|
| 12 |
#include "chip.h" |
| 13 |
#include "retimer.h" |
| 14 |
|
| 15 |
|
| 16 |
#define INTEL_USB4_RETIMER_DSM_UUID "E0053122-795B-4122-8A5E-57BE1D26ACB3" |
| 17 |
|
| 18 |
static const char *usb4_retimer_scope; |
| 19 |
static const char *usb4_retimer_path_arg(const char *arg) |
| 20 |
{ |
| 21 |
|
| 22 |
static char name[DEVICE_PATH_MAX]; |
| 23 |
snprintf(name, sizeof(name), "%s%c%s", usb4_retimer_scope, '.', arg); |
| 24 |
return name; |
| 25 |
} |
| 26 |
|
| 27 |
|
| 28 |
#define USB4_RETIMER_ITERATION_NUM 12 |
| 29 |
#define USB4_RETIMER_POLL_CYCLE_MS 25 |
| 30 |
static void usb4_retimer_execute_ec_cmd(uint8_t port, uint8_t cmd, uint8_t expected_value, |
| 31 |
struct acpi_gpio *power_gpio) |
| 32 |
{ |
| 33 |
const char *RFWU = ec_retimer_fw_update_path(); |
| 34 |
const uint8_t data = cmd << USB_RETIMER_FW_UPDATE_OP_SHIFT | port; |
| 35 |
|
| 36 |
|
| 37 |
ec_retimer_fw_update(data); |
| 38 |
|
| 39 |
if (RFWU) { |
| 40 |
acpigen_write_if_lequal_namestr_int(RFWU, USB_RETIMER_FW_UPDATE_ERROR); |
| 41 |
acpigen_disable_tx_gpio(power_gpio); |
| 42 |
acpigen_write_return_integer(-1); |
| 43 |
acpigen_pop_len(); |
| 44 |
} |
| 45 |
|
| 46 |
acpigen_write_store_int_to_op(USB4_RETIMER_ITERATION_NUM, LOCAL2_OP); |
| 47 |
acpigen_emit_byte(WHILE_OP); |
| 48 |
acpigen_write_len_f(); |
| 49 |
acpigen_emit_byte(LGREATER_OP); |
| 50 |
acpigen_emit_byte(LOCAL2_OP); |
| 51 |
acpigen_emit_byte(ZERO_OP); |
| 52 |
|
| 53 |
if (RFWU) { |
| 54 |
acpigen_write_if_lequal_namestr_int(RFWU, expected_value); |
| 55 |
acpigen_emit_byte(BREAK_OP); |
| 56 |
acpigen_pop_len(); |
| 57 |
|
| 58 |
if (cmd == USB_RETIMER_FW_UPDATE_GET_MUX) { |
| 59 |
acpigen_write_if_lequal_namestr_int(RFWU, USB_RETIMER_FW_UPDATE_INVALID_MUX); |
| 60 |
acpigen_write_sleep(USB4_RETIMER_POLL_CYCLE_MS); |
| 61 |
acpigen_emit_byte(DECREMENT_OP); |
| 62 |
acpigen_emit_byte(LOCAL2_OP); |
| 63 |
acpigen_emit_byte(CONTINUE_OP); |
| 64 |
acpigen_pop_len(); |
| 65 |
|
| 66 |
acpigen_emit_byte(AND_OP); |
| 67 |
acpigen_emit_namestring(RFWU); |
| 68 |
acpigen_write_integer(USB_RETIMER_FW_UPDATE_MUX_MASK); |
| 69 |
acpigen_emit_byte(LOCAL3_OP); |
| 70 |
acpigen_write_if(); |
| 71 |
acpigen_emit_byte(LNOT_OP); |
| 72 |
acpigen_emit_byte(LEQUAL_OP); |
| 73 |
acpigen_emit_byte(LOCAL3_OP); |
| 74 |
acpigen_emit_byte(0); |
| 75 |
acpigen_disable_tx_gpio(power_gpio); |
| 76 |
acpigen_write_return_integer(-1); |
| 77 |
acpigen_pop_len(); |
| 78 |
} else if (cmd == USB_RETIMER_FW_UPDATE_SET_TBT) { |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
acpigen_write_if_lequal_namestr_int(RFWU, USB_PD_MUX_TBT_COMPAT_ENABLED); |
| 85 |
acpigen_emit_byte(BREAK_OP); |
| 86 |
acpigen_pop_len(); |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
acpigen_write_sleep(USB4_RETIMER_POLL_CYCLE_MS); |
| 91 |
acpigen_emit_byte(DECREMENT_OP); |
| 92 |
acpigen_emit_byte(LOCAL2_OP); |
| 93 |
acpigen_pop_len(); |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
acpigen_write_if_lequal_op_int(LOCAL2_OP, 0); |
| 100 |
acpigen_disable_tx_gpio(power_gpio); |
| 101 |
acpigen_write_return_integer(-1); |
| 102 |
acpigen_pop_len(); |
| 103 |
} |
| 104 |
|
| 105 |
static void enable_retimer_online_state(uint8_t port, struct acpi_gpio *power_gpio) |
| 106 |
{ |
| 107 |
uint8_t expected_value; |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
acpigen_enable_tx_gpio(power_gpio); |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
expected_value = USB_PD_MUX_NONE; |
| 128 |
usb4_retimer_execute_ec_cmd(port, USB_RETIMER_FW_UPDATE_GET_MUX, expected_value, |
| 129 |
power_gpio); |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
expected_value = 0; |
| 137 |
usb4_retimer_execute_ec_cmd(port, USB_RETIMER_FW_UPDATE_SUSPEND_PD, expected_value, |
| 138 |
power_gpio); |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
expected_value = USB_PD_MUX_USB_ENABLED; |
| 146 |
usb4_retimer_execute_ec_cmd(port, USB_RETIMER_FW_UPDATE_SET_USB, expected_value, |
| 147 |
power_gpio); |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
expected_value = USB_PD_MUX_SAFE_MODE; |
| 155 |
usb4_retimer_execute_ec_cmd(port, USB_RETIMER_FW_UPDATE_SET_SAFE, expected_value, |
| 156 |
power_gpio); |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
expected_value = USB_PD_MUX_USB4_ENABLED; |
| 164 |
usb4_retimer_execute_ec_cmd(port, USB_RETIMER_FW_UPDATE_SET_TBT, expected_value, |
| 165 |
power_gpio); |
| 166 |
} |
| 167 |
|
| 168 |
static void disable_retimer_online_state(uint8_t port, struct acpi_gpio *power_gpio) |
| 169 |
{ |
| 170 |
uint8_t expected_value; |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
expected_value = 0; |
| 185 |
usb4_retimer_execute_ec_cmd(port, USB_RETIMER_FW_UPDATE_DISCONNECT, expected_value, |
| 186 |
power_gpio); |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
expected_value = 1; |
| 194 |
usb4_retimer_execute_ec_cmd(port, USB_RETIMER_FW_UPDATE_RESUME_PD, expected_value, |
| 195 |
power_gpio); |
| 196 |
|
| 197 |
|
| 198 |
acpigen_disable_tx_gpio(power_gpio); |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
static void usb4_retimer_cb_standard_query(uint8_t port, void *arg) |
| 212 |
{ |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
acpigen_write_to_integer(ARG1_OP, LOCAL1_OP); |
| 221 |
|
| 222 |
|
| 223 |
acpigen_write_if_lequal_op_int(LOCAL1_OP, 1); |
| 224 |
acpigen_write_return_singleton_buffer(0x7); |
| 225 |
acpigen_pop_len(); |
| 226 |
|
| 227 |
|
| 228 |
acpigen_write_return_singleton_buffer(0x1); |
| 229 |
} |
| 230 |
|
| 231 |
static void usb4_retimer_cb_get_power_state(uint8_t port, void *arg) |
| 232 |
{ |
| 233 |
const char *PWR; |
| 234 |
char pwr[DEVICE_PATH_MAX]; |
| 235 |
|
| 236 |
snprintf(pwr, sizeof(pwr), "HR.DFP%1d.PWR", port); |
| 237 |
PWR = usb4_retimer_path_arg(pwr); |
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
acpigen_write_if(); |
| 245 |
acpigen_emit_byte(LGREATER_OP); |
| 246 |
acpigen_emit_namestring(PWR); |
| 247 |
acpigen_emit_byte(0); |
| 248 |
acpigen_write_return_integer(1); |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
acpigen_write_else(); |
| 256 |
acpigen_write_return_integer(0); |
| 257 |
acpigen_pop_len(); |
| 258 |
} |
| 259 |
|
| 260 |
static void usb4_retimer_cb_set_power_state(uint8_t port, void *arg) |
| 261 |
{ |
| 262 |
struct acpi_gpio *power_gpio = arg; |
| 263 |
const char *PWR; |
| 264 |
char pwr[DEVICE_PATH_MAX]; |
| 265 |
|
| 266 |
snprintf(pwr, sizeof(pwr), "HR.DFP%1d.PWR", port); |
| 267 |
PWR = usb4_retimer_path_arg(pwr); |
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
acpigen_get_package_op_element(ARG3_OP, 0, LOCAL1_OP); |
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
acpigen_write_if(); |
| 284 |
acpigen_emit_byte(LAND_OP); |
| 285 |
acpigen_emit_byte(LEQUAL_OP); |
| 286 |
acpigen_emit_byte(LOCAL1_OP); |
| 287 |
acpigen_emit_byte(0); |
| 288 |
acpigen_emit_byte(LGREATER_OP); |
| 289 |
acpigen_emit_namestring(PWR); |
| 290 |
acpigen_emit_byte(0); |
| 291 |
|
| 292 |
acpigen_emit_byte(DECREMENT_OP); |
| 293 |
acpigen_emit_namestring(PWR); |
| 294 |
acpigen_write_if_lequal_namestr_int(PWR, 0); |
| 295 |
disable_retimer_online_state(port, power_gpio); |
| 296 |
acpigen_pop_len(); |
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
acpigen_write_else(); |
| 305 |
acpigen_write_if(); |
| 306 |
acpigen_emit_byte(LAND_OP); |
| 307 |
acpigen_emit_byte(LEQUAL_OP); |
| 308 |
acpigen_emit_byte(LOCAL1_OP); |
| 309 |
acpigen_emit_byte(1); |
| 310 |
acpigen_emit_byte(LEQUAL_OP); |
| 311 |
acpigen_emit_namestring(PWR); |
| 312 |
acpigen_emit_byte(0); |
| 313 |
enable_retimer_online_state(port, power_gpio); |
| 314 |
|
| 315 |
acpigen_emit_byte(INCREMENT_OP); |
| 316 |
acpigen_emit_namestring(PWR); |
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
acpigen_write_else(); |
| 324 |
acpigen_write_return_integer(0); |
| 325 |
acpigen_pop_len(); |
| 326 |
acpigen_pop_len(); |
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
acpigen_write_if_lequal_namestr_int(PWR, 1); |
| 334 |
acpigen_write_return_integer(1); |
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
acpigen_write_else(); |
| 342 |
acpigen_write_return_integer(0); |
| 343 |
acpigen_pop_len(); |
| 344 |
} |
| 345 |
|
| 346 |
static void (*usb4_retimer_callbacks[3])(uint8_t port, void *) = { |
| 347 |
usb4_retimer_cb_standard_query, |
| 348 |
usb4_retimer_cb_get_power_state, |
| 349 |
usb4_retimer_cb_set_power_state, |
| 350 |
}; |
| 351 |
|
| 352 |
static void usb4_retimer_write_dsm(uint8_t port, const char *uuid, |
| 353 |
void (**callbacks)(uint8_t port, void *), size_t count, void *arg) |
| 354 |
{ |
| 355 |
struct usb4_retimer_dsm_uuid id = DSM_UUID(uuid, callbacks, count, arg); |
| 356 |
size_t i; |
| 357 |
|
| 358 |
acpigen_write_to_integer(ARG2_OP, LOCAL0_OP); |
| 359 |
|
| 360 |
for (i = 0; i < id.count; i++) { |
| 361 |
|
| 362 |
acpigen_write_if_lequal_op_int(LOCAL0_OP, i); |
| 363 |
|
| 364 |
|
| 365 |
if (id.callbacks[i]) |
| 366 |
id.callbacks[i](port, id.arg); |
| 367 |
|
| 368 |
acpigen_pop_len(); |
| 369 |
} |
| 370 |
} |
| 371 |
|
| 372 |
static void usb4_retimer_fill_ssdt(const struct device *dev) |
| 373 |
{ |
| 374 |
struct drivers_intel_usb4_retimer_config *config = dev->chip_info; |
| 375 |
const struct device *usb_device; |
| 376 |
static char dfp[DEVICE_PATH_MAX]; |
| 377 |
struct acpi_pld pld; |
| 378 |
uint8_t dfp_port, usb_port; |
| 379 |
int ec_port = 0; |
| 380 |
|
| 381 |
usb4_retimer_scope = acpi_device_scope(dev); |
| 382 |
if (!usb4_retimer_scope || !config) |
| 383 |
return; |
| 384 |
|
| 385 |
|
| 386 |
acpigen_write_scope(usb4_retimer_scope); |
| 387 |
|
| 388 |
|
| 389 |
acpigen_write_device("HR"); |
| 390 |
acpigen_write_ADR(0); |
| 391 |
acpigen_write_STA(ACPI_STATUS_DEVICE_ALL_ON); |
| 392 |
|
| 393 |
for (dfp_port = 0; dfp_port < DFP_NUM_MAX; dfp_port++) { |
| 394 |
if (!config->dfp[dfp_port].power_gpio.pin_count) { |
| 395 |
printk(BIOS_WARNING, "%s: No DFP%1d power GPIO for %s\n", |
| 396 |
__func__, dfp_port, dev_path(dev)); |
| 397 |
continue; |
| 398 |
} |
| 399 |
|
| 400 |
usb_device = config->dfp[dfp_port].typec_port; |
| 401 |
usb_port = usb_device->path.usb.port_id; |
| 402 |
|
| 403 |
|
| 404 |
ec_port = (config->dfp[dfp_port].ec_port) ? |
| 405 |
config->dfp[dfp_port].ec_port - EC_TYPEC_PORT_0 : |
| 406 |
retimer_get_index_for_typec(usb_port); |
| 407 |
if (ec_port == -1) { |
| 408 |
printk(BIOS_ERR, "%s: No relative EC port found for TC port %d\n", |
| 409 |
__func__, usb_port); |
| 410 |
continue; |
| 411 |
} |
| 412 |
printk(BIOS_INFO, "USB Type-C %d mapped to EC port %d\n", usb_port, ec_port); |
| 413 |
|
| 414 |
snprintf(dfp, sizeof(dfp), "DFP%1d", ec_port); |
| 415 |
acpigen_write_device(dfp); |
| 416 |
|
| 417 |
acpigen_write_ADR(dfp_port*2 + 1); |
| 418 |
|
| 419 |
|
| 420 |
if (CONFIG(DRIVERS_USB_ACPI)) { |
| 421 |
if (usb_acpi_get_pld(usb_device, &pld)) |
| 422 |
acpigen_write_pld(&pld); |
| 423 |
else |
| 424 |
printk(BIOS_ERR, "Error retrieving PLD for USB Type-C %d\n", |
| 425 |
usb_port); |
| 426 |
} |
| 427 |
|
| 428 |
|
| 429 |
acpigen_write_name("PWR"); |
| 430 |
acpigen_write_zero(); |
| 431 |
|
| 432 |
|
| 433 |
acpigen_write_method_serialized("_DSM", 0x4); |
| 434 |
|
| 435 |
acpigen_write_to_buffer(ARG0_OP, LOCAL0_OP); |
| 436 |
acpigen_write_if(); |
| 437 |
acpigen_emit_byte(LNOT_OP); |
| 438 |
acpigen_emit_byte(LEQUAL_OP); |
| 439 |
acpigen_emit_byte(LOCAL0_OP); |
| 440 |
acpigen_write_uuid(INTEL_USB4_RETIMER_DSM_UUID); |
| 441 |
|
| 442 |
acpigen_write_return_singleton_buffer(0x0); |
| 443 |
acpigen_pop_len(); |
| 444 |
usb4_retimer_write_dsm(ec_port, INTEL_USB4_RETIMER_DSM_UUID, |
| 445 |
usb4_retimer_callbacks, ARRAY_SIZE(usb4_retimer_callbacks), |
| 446 |
(void *)&config->dfp[dfp_port].power_gpio); |
| 447 |
|
| 448 |
acpigen_write_return_singleton_buffer(0x0); |
| 449 |
|
| 450 |
acpigen_pop_len(); |
| 451 |
acpigen_pop_len(); |
| 452 |
} |
| 453 |
acpigen_pop_len(); |
| 454 |
acpigen_pop_len(); |
| 455 |
|
| 456 |
printk(BIOS_INFO, "%s.HR: %s at %s\n", usb4_retimer_scope, dev->chip_ops->name, |
| 457 |
dev_path(dev)); |
| 458 |
} |
| 459 |
|
| 460 |
static struct device_operations usb4_retimer_dev_ops = { |
| 461 |
.read_resources = noop_read_resources, |
| 462 |
.set_resources = noop_set_resources, |
| 463 |
.acpi_fill_ssdt = usb4_retimer_fill_ssdt, |
| 464 |
}; |
| 465 |
|
| 466 |
static void usb4_retimer_enable(struct device *dev) |
| 467 |
{ |
| 468 |
dev->ops = &usb4_retimer_dev_ops; |
| 469 |
} |
| 470 |
|
| 471 |
struct chip_operations drivers_intel_usb4_retimer_ops = { |
| 472 |
.name = "Intel USB4 Retimer", |
| 473 |
.enable_dev = usb4_retimer_enable |
| 474 |
}; |
| 475 |
|
| 476 |
__weak const char *ec_retimer_fw_update_path(void) |
| 477 |
{ |
| 478 |
return NULL; |
| 479 |
} |
| 480 |
|
| 481 |
__weak void ec_retimer_fw_update(uint8_t data) |
| 482 |
{ |
| 483 |
} |
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
__weak int retimer_get_index_for_typec(uint8_t typec_port) |
| 497 |
{ |
| 498 |
|
| 499 |
return (int)typec_port; |
| 500 |
} |