1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * UCSI driver for Cypress CCGx Type-C controller 4 * 5 * Copyright (C) 2017-2018 NVIDIA Corporation. All rights reserved. 6 * Author: Ajay Gupta <ajayg@nvidia.com> 7 * 8 * Some code borrowed from drivers/usb/typec/ucsi/ucsi_acpi.c 9 */ 10 #include <linux/acpi.h> 11 #include <linux/delay.h> 12 #include <linux/firmware.h> 13 #include <linux/hex.h> 14 #include <linux/i2c.h> 15 #include <linux/module.h> 16 #include <linux/pci.h> 17 #include <linux/platform_device.h> 18 #include <linux/pm.h> 19 #include <linux/pm_runtime.h> 20 #include <linux/usb/typec_dp.h> 21 22 #include <linux/unaligned.h> 23 #include "ucsi.h" 24 25 enum enum_fw_mode { 26 BOOT, /* bootloader */ 27 FW1, /* FW partition-1 (contains secondary fw) */ 28 FW2, /* FW partition-2 (contains primary fw) */ 29 FW_INVALID, 30 }; 31 32 #define CCGX_RAB_DEVICE_MODE 0x0000 33 #define CCGX_RAB_INTR_REG 0x0006 34 #define DEV_INT BIT(0) 35 #define PORT0_INT BIT(1) 36 #define PORT1_INT BIT(2) 37 #define UCSI_READ_INT BIT(7) 38 #define CCGX_RAB_JUMP_TO_BOOT 0x0007 39 #define TO_BOOT 'J' 40 #define TO_ALT_FW 'A' 41 #define CCGX_RAB_RESET_REQ 0x0008 42 #define RESET_SIG 'R' 43 #define CMD_RESET_I2C 0x0 44 #define CMD_RESET_DEV 0x1 45 #define CCGX_RAB_ENTER_FLASHING 0x000A 46 #define FLASH_ENTER_SIG 'P' 47 #define CCGX_RAB_VALIDATE_FW 0x000B 48 #define CCGX_RAB_FLASH_ROW_RW 0x000C 49 #define FLASH_SIG 'F' 50 #define FLASH_RD_CMD 0x0 51 #define FLASH_WR_CMD 0x1 52 #define FLASH_FWCT1_WR_CMD 0x2 53 #define FLASH_FWCT2_WR_CMD 0x3 54 #define FLASH_FWCT_SIG_WR_CMD 0x4 55 #define CCGX_RAB_READ_ALL_VER 0x0010 56 #define CCGX_RAB_READ_FW2_VER 0x0020 57 #define CCGX_RAB_UCSI_CONTROL 0x0039 58 #define CCGX_RAB_UCSI_CONTROL_START BIT(0) 59 #define CCGX_RAB_UCSI_CONTROL_STOP BIT(1) 60 #define CCGX_RAB_UCSI_DATA_BLOCK(offset) (0xf000 | ((offset) & 0xff)) 61 #define REG_FLASH_RW_MEM 0x0200 62 #define DEV_REG_IDX CCGX_RAB_DEVICE_MODE 63 #define CCGX_RAB_PDPORT_ENABLE 0x002C 64 #define PDPORT_1 BIT(0) 65 #define PDPORT_2 BIT(1) 66 #define CCGX_RAB_RESPONSE 0x007E 67 #define ASYNC_EVENT BIT(7) 68 69 /* CCGx events & async msg codes */ 70 #define RESET_COMPLETE 0x80 71 #define EVENT_INDEX RESET_COMPLETE 72 #define PORT_CONNECT_DET 0x84 73 #define PORT_DISCONNECT_DET 0x85 74 #define ROLE_SWAP_COMPELETE 0x87 75 76 /* ccg firmware */ 77 #define CYACD_LINE_SIZE 527 78 #define CCG4_ROW_SIZE 256 79 #define FW1_METADATA_ROW 0x1FF 80 #define FW2_METADATA_ROW 0x1FE 81 #define FW_CFG_TABLE_SIG_SIZE 256 82 83 static int secondary_fw_min_ver = 41; 84 85 enum enum_flash_mode { 86 SECONDARY_BL, /* update secondary using bootloader */ 87 PRIMARY, /* update primary using secondary */ 88 SECONDARY, /* update secondary using primary */ 89 FLASH_NOT_NEEDED, /* update not required */ 90 FLASH_INVALID, 91 }; 92 93 static const char * const ccg_fw_names[] = { 94 "ccg_boot.cyacd", 95 "ccg_primary.cyacd", 96 "ccg_secondary.cyacd" 97 }; 98 99 struct ccg_dev_info { 100 #define CCG_DEVINFO_FWMODE_SHIFT (0) 101 #define CCG_DEVINFO_FWMODE_MASK (0x3 << CCG_DEVINFO_FWMODE_SHIFT) 102 #define CCG_DEVINFO_PDPORTS_SHIFT (2) 103 #define CCG_DEVINFO_PDPORTS_MASK (0x3 << CCG_DEVINFO_PDPORTS_SHIFT) 104 u8 mode; 105 u8 bl_mode; 106 __le16 silicon_id; 107 __le16 bl_last_row; 108 } __packed; 109 110 struct version_format { 111 __le16 build; 112 u8 patch; 113 u8 ver; 114 #define CCG_VERSION_PATCH(x) ((x) << 16) 115 #define CCG_VERSION(x) ((x) << 24) 116 #define CCG_VERSION_MIN_SHIFT (0) 117 #define CCG_VERSION_MIN_MASK (0xf << CCG_VERSION_MIN_SHIFT) 118 #define CCG_VERSION_MAJ_SHIFT (4) 119 #define CCG_VERSION_MAJ_MASK (0xf << CCG_VERSION_MAJ_SHIFT) 120 } __packed; 121 122 /* 123 * Firmware version 3.1.10 or earlier, built for NVIDIA has known issue 124 * of missing interrupt when a device is connected for runtime resume 125 */ 126 #define CCG_FW_BUILD_NVIDIA (('n' << 8) | 'v') 127 #define CCG_OLD_FW_VERSION (CCG_VERSION(0x31) | CCG_VERSION_PATCH(10)) 128 129 /* Firmware for Tegra doesn't support UCSI ALT command, built 130 * for NVIDIA has known issue of reporting wrong capability info 131 */ 132 #define CCG_FW_BUILD_NVIDIA_TEGRA (('g' << 8) | 'n') 133 134 /* Altmode offset for NVIDIA Function Test Board (FTB) */ 135 #define NVIDIA_FTB_DP_OFFSET (2) 136 #define NVIDIA_FTB_DBG_OFFSET (3) 137 138 #define CCG_READ_MAX_RETRIES 4 139 140 struct version_info { 141 struct version_format base; 142 struct version_format app; 143 }; 144 145 struct fw_config_table { 146 u32 identity; 147 u16 table_size; 148 u8 fwct_version; 149 u8 is_key_change; 150 u8 guid[16]; 151 struct version_format base; 152 struct version_format app; 153 u8 primary_fw_digest[32]; 154 u32 key_exp_length; 155 u8 key_modulus[256]; 156 u8 key_exp[4]; 157 }; 158 159 /* CCGx response codes */ 160 enum ccg_resp_code { 161 CMD_NO_RESP = 0x00, 162 CMD_SUCCESS = 0x02, 163 FLASH_DATA_AVAILABLE = 0x03, 164 CMD_INVALID = 0x05, 165 FLASH_UPDATE_FAIL = 0x07, 166 INVALID_FW = 0x08, 167 INVALID_ARG = 0x09, 168 CMD_NOT_SUPPORT = 0x0A, 169 TRANSACTION_FAIL = 0x0C, 170 PD_CMD_FAIL = 0x0D, 171 UNDEF_ERROR = 0x0F, 172 INVALID_RESP = 0x10, 173 }; 174 175 #define CCG_EVENT_MAX (EVENT_INDEX + 43) 176 177 struct ccg_cmd { 178 u16 reg; 179 u32 data; 180 int len; 181 u32 delay; /* ms delay for cmd timeout */ 182 }; 183 184 struct ccg_resp { 185 u8 code; 186 u8 length; 187 }; 188 189 struct ucsi_ccg_altmode { 190 u16 svid; 191 u32 mid; 192 u8 linked_idx; 193 u8 active_idx; 194 #define UCSI_MULTI_DP_INDEX (0xff) 195 bool checked; 196 } __packed; 197 198 #define CCGX_MESSAGE_IN_MAX 4 199 struct op_region { 200 __le32 cci; 201 __le32 message_in[CCGX_MESSAGE_IN_MAX]; 202 }; 203 204 struct ucsi_ccg { 205 struct device *dev; 206 struct ucsi *ucsi; 207 struct i2c_client *client; 208 209 struct ccg_dev_info info; 210 /* version info for boot, primary and secondary */ 211 struct version_info version[FW2 + 1]; 212 u32 fw_version; 213 /* CCG HPI communication flags */ 214 unsigned long flags; 215 #define RESET_PENDING 0 216 #define DEV_CMD_PENDING 1 217 struct ccg_resp dev_resp; 218 u8 cmd_resp; 219 int port_num; 220 int irq; 221 struct work_struct work; 222 struct mutex lock; /* to sync between user and driver thread */ 223 224 /* fw build with vendor information */ 225 u16 fw_build; 226 struct work_struct pm_work; 227 228 bool has_multiple_dp; 229 struct ucsi_ccg_altmode orig[UCSI_MAX_ALTMODES]; 230 struct ucsi_ccg_altmode updated[UCSI_MAX_ALTMODES]; 231 232 /* 233 * This spinlock protects op_data which includes CCI and MESSAGE_IN that 234 * will be updated in ISR 235 */ 236 spinlock_t op_lock; 237 struct op_region op_data; 238 }; 239 240 static int ccg_read(struct ucsi_ccg *uc, u16 rab, u8 *data, u32 len) 241 { 242 struct i2c_client *client = uc->client; 243 const struct i2c_adapter_quirks *quirks = client->adapter->quirks; 244 unsigned char buf[2]; 245 struct i2c_msg msgs[] = { 246 { 247 .addr = client->addr, 248 .flags = 0x0, 249 .len = sizeof(buf), 250 .buf = buf, 251 }, 252 { 253 .addr = client->addr, 254 .flags = I2C_M_RD, 255 .buf = data, 256 }, 257 }; 258 u32 rlen, rem_len = len, max_read_len = len; 259 int status; 260 int retry_count; 261 262 /* check any max_read_len limitation on i2c adapter */ 263 if (quirks && quirks->max_read_len) 264 max_read_len = quirks->max_read_len; 265 266 pm_runtime_get_sync(uc->dev); 267 while (rem_len > 0) { 268 msgs[1].buf = &data[len - rem_len]; 269 rlen = min_t(u16, rem_len, max_read_len); 270 msgs[1].len = rlen; 271 put_unaligned_le16(rab, buf); 272 273 for (retry_count = 0; retry_count < CCG_READ_MAX_RETRIES; retry_count++) { 274 status = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs)); 275 276 /* Only retry on -EREMOTEIO, and not after the last attempt */ 277 if (status != -EREMOTEIO || retry_count == CCG_READ_MAX_RETRIES - 1) 278 break; 279 280 fsleep(1000); 281 } 282 283 if (status < 0) { 284 dev_err(uc->dev, "i2c_transfer failed %d\n", status); 285 pm_runtime_put_sync(uc->dev); 286 return status; 287 } 288 rab += rlen; 289 rem_len -= rlen; 290 } 291 292 pm_runtime_put_sync(uc->dev); 293 return 0; 294 } 295 296 static int ccg_write(struct ucsi_ccg *uc, u16 rab, const u8 *data, u32 len) 297 { 298 struct i2c_client *client = uc->client; 299 unsigned char *buf; 300 struct i2c_msg msgs[] = { 301 { 302 .addr = client->addr, 303 .flags = 0x0, 304 } 305 }; 306 int status; 307 308 buf = kzalloc(len + sizeof(rab), GFP_KERNEL); 309 if (!buf) 310 return -ENOMEM; 311 312 put_unaligned_le16(rab, buf); 313 memcpy(buf + sizeof(rab), data, len); 314 315 msgs[0].len = len + sizeof(rab); 316 msgs[0].buf = buf; 317 318 pm_runtime_get_sync(uc->dev); 319 status = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs)); 320 if (status < 0) { 321 dev_err(uc->dev, "i2c_transfer failed %d\n", status); 322 pm_runtime_put_sync(uc->dev); 323 kfree(buf); 324 return status; 325 } 326 327 pm_runtime_put_sync(uc->dev); 328 kfree(buf); 329 return 0; 330 } 331 332 static int ccg_op_region_update(struct ucsi_ccg *uc, u32 cci) 333 { 334 u16 reg = CCGX_RAB_UCSI_DATA_BLOCK(UCSI_MESSAGE_IN); 335 struct op_region *data = &uc->op_data; 336 unsigned char *buf; 337 size_t size = sizeof(data->message_in); 338 339 buf = kzalloc(size, GFP_ATOMIC); 340 if (!buf) 341 return -ENOMEM; 342 if (UCSI_CCI_LENGTH(cci)) { 343 int ret = ccg_read(uc, reg, (void *)buf, size); 344 345 if (ret) { 346 kfree(buf); 347 return ret; 348 } 349 } 350 351 spin_lock(&uc->op_lock); 352 data->cci = cpu_to_le32(cci); 353 if (UCSI_CCI_LENGTH(cci)) 354 memcpy(&data->message_in, buf, size); 355 spin_unlock(&uc->op_lock); 356 kfree(buf); 357 return 0; 358 } 359 360 static int ucsi_ccg_init(struct ucsi_ccg *uc) 361 { 362 unsigned int count = 10; 363 u8 data; 364 int status; 365 366 spin_lock_init(&uc->op_lock); 367 368 data = CCGX_RAB_UCSI_CONTROL_STOP; 369 status = ccg_write(uc, CCGX_RAB_UCSI_CONTROL, &data, sizeof(data)); 370 if (status < 0) 371 return status; 372 373 data = CCGX_RAB_UCSI_CONTROL_START; 374 status = ccg_write(uc, CCGX_RAB_UCSI_CONTROL, &data, sizeof(data)); 375 if (status < 0) 376 return status; 377 378 /* 379 * Flush CCGx RESPONSE queue by acking interrupts. Above ucsi control 380 * register write will push response which must be cleared. 381 */ 382 do { 383 status = ccg_read(uc, CCGX_RAB_INTR_REG, &data, sizeof(data)); 384 if (status < 0) 385 return status; 386 387 if (!(data & DEV_INT)) 388 return 0; 389 390 status = ccg_write(uc, CCGX_RAB_INTR_REG, &data, sizeof(data)); 391 if (status < 0) 392 return status; 393 394 usleep_range(10000, 11000); 395 } while (--count); 396 397 return -ETIMEDOUT; 398 } 399 400 static int ucsi_ccg_update_get_current_cam_cmd(struct ucsi_ccg *uc, u8 *data) 401 { 402 u8 cam, new_cam; 403 404 cam = data[0]; 405 if (cam >= UCSI_MAX_ALTMODES) { 406 dev_err(uc->dev, "PPM returned invalid alternate mode index %u\n", 407 cam); 408 return -EPROTO; 409 } 410 411 new_cam = uc->orig[cam].linked_idx; 412 if (new_cam >= UCSI_MAX_ALTMODES) { 413 dev_err(uc->dev, "invalid linked alternate mode index %u for %u\n", 414 new_cam, cam); 415 return -EPROTO; 416 } 417 418 uc->updated[new_cam].active_idx = cam; 419 data[0] = new_cam; 420 421 return 0; 422 } 423 424 static bool ucsi_ccg_update_altmodes(struct ucsi *ucsi, 425 u8 recipient, 426 struct ucsi_altmode *orig, 427 struct ucsi_altmode *updated) 428 { 429 struct ucsi_ccg *uc = ucsi_get_drvdata(ucsi); 430 struct ucsi_ccg_altmode *alt, *new_alt; 431 int i, j, k = 0; 432 bool found = false; 433 434 if (recipient != UCSI_RECIPIENT_CON) 435 return false; 436 437 alt = uc->orig; 438 new_alt = uc->updated; 439 memset(uc->updated, 0, sizeof(uc->updated)); 440 441 /* 442 * Copy original connector altmodes to new structure. 443 * We need this before second loop since second loop 444 * checks for duplicate altmodes. 445 */ 446 for (i = 0; i < UCSI_MAX_ALTMODES; i++) { 447 alt[i].svid = orig[i].svid; 448 alt[i].mid = orig[i].mid; 449 if (!alt[i].svid) 450 break; 451 } 452 453 for (i = 0; i < UCSI_MAX_ALTMODES; i++) { 454 if (!alt[i].svid) 455 break; 456 457 /* already checked and considered */ 458 if (alt[i].checked) 459 continue; 460 461 if (!DP_CONF_GET_PIN_ASSIGN(alt[i].mid)) { 462 /* Found Non DP altmode */ 463 new_alt[k].svid = alt[i].svid; 464 new_alt[k].mid |= alt[i].mid; 465 new_alt[k].linked_idx = i; 466 alt[i].linked_idx = k; 467 updated[k].svid = new_alt[k].svid; 468 updated[k].mid = new_alt[k].mid; 469 k++; 470 continue; 471 } 472 473 for (j = i + 1; j < UCSI_MAX_ALTMODES; j++) { 474 if (alt[i].svid != alt[j].svid || 475 !DP_CONF_GET_PIN_ASSIGN(alt[j].mid)) { 476 continue; 477 } else { 478 /* Found duplicate DP mode */ 479 new_alt[k].svid = alt[i].svid; 480 new_alt[k].mid |= alt[i].mid | alt[j].mid; 481 new_alt[k].linked_idx = UCSI_MULTI_DP_INDEX; 482 alt[i].linked_idx = k; 483 alt[j].linked_idx = k; 484 alt[j].checked = true; 485 found = true; 486 } 487 } 488 if (found) { 489 uc->has_multiple_dp = true; 490 } else { 491 /* Didn't find any duplicate DP altmode */ 492 new_alt[k].svid = alt[i].svid; 493 new_alt[k].mid |= alt[i].mid; 494 new_alt[k].linked_idx = i; 495 alt[i].linked_idx = k; 496 } 497 updated[k].svid = new_alt[k].svid; 498 updated[k].mid = new_alt[k].mid; 499 k++; 500 } 501 return found; 502 } 503 504 static void ucsi_ccg_update_set_new_cam_cmd(struct ucsi_ccg *uc, 505 struct ucsi_connector *con, 506 u64 *cmd) 507 { 508 struct ucsi_ccg_altmode *new_port, *port; 509 struct typec_altmode *alt = NULL; 510 u8 new_cam, cam, pin; 511 bool enter_new_mode; 512 int i, j, k = 0xff; 513 514 port = uc->orig; 515 new_cam = UCSI_SET_NEW_CAM_GET_AM(*cmd); 516 if (new_cam >= ARRAY_SIZE(uc->updated)) 517 return; 518 new_port = &uc->updated[new_cam]; 519 cam = new_port->linked_idx; 520 enter_new_mode = UCSI_SET_NEW_CAM_ENTER(*cmd); 521 522 /* 523 * If CAM is UCSI_MULTI_DP_INDEX then this is DP altmode 524 * with multiple DP mode. Find out CAM for best pin assignment 525 * among all DP mode. Priorite pin E->D->C after making sure 526 * the partner supports that pin. 527 */ 528 if (cam == UCSI_MULTI_DP_INDEX) { 529 if (enter_new_mode) { 530 for (i = 0; con->partner_altmode[i]; i++) { 531 alt = con->partner_altmode[i]; 532 if (alt->svid == new_port->svid) 533 break; 534 } 535 /* 536 * alt will always be non NULL since this is 537 * UCSI_SET_NEW_CAM command and so there will be 538 * at least one con->partner_altmode[i] with svid 539 * matching with new_port->svid. 540 */ 541 for (j = 0; port[j].svid; j++) { 542 pin = DP_CONF_GET_PIN_ASSIGN(port[j].mid); 543 if (alt && port[j].svid == alt->svid && 544 (pin & DP_CONF_GET_PIN_ASSIGN(alt->vdo))) { 545 /* prioritize pin E->D->C */ 546 if (k == 0xff || (k != 0xff && pin > 547 DP_CONF_GET_PIN_ASSIGN(port[k].mid)) 548 ) { 549 k = j; 550 } 551 } 552 } 553 cam = k; 554 new_port->active_idx = cam; 555 } else { 556 cam = new_port->active_idx; 557 } 558 } 559 *cmd &= ~UCSI_SET_NEW_CAM_AM_MASK; 560 *cmd |= UCSI_SET_NEW_CAM_SET_AM(cam); 561 } 562 563 /* 564 * Change the order of vdo values of NVIDIA test device FTB 565 * (Function Test Board) which reports altmode list with vdo=0x3 566 * first and then vdo=0x. Current logic to assign mode value is 567 * based on order in altmode list and it causes a mismatch of CON 568 * and SOP altmodes since NVIDIA GPU connector has order of vdo=0x1 569 * first and then vdo=0x3 570 */ 571 static void ucsi_ccg_nvidia_altmode(struct ucsi_ccg *uc, 572 struct ucsi_altmode *alt, 573 u64 command) 574 { 575 switch (UCSI_ALTMODE_OFFSET(command)) { 576 case NVIDIA_FTB_DP_OFFSET: 577 if (alt[0].mid == USB_TYPEC_NVIDIA_VLINK_DBG_VDO) 578 alt[0].mid = USB_TYPEC_NVIDIA_VLINK_DP_VDO | 579 DP_CAP_DP_SIGNALLING(0) | DP_CAP_USB | 580 DP_CONF_SET_PIN_ASSIGN(BIT(DP_PIN_ASSIGN_E)); 581 break; 582 case NVIDIA_FTB_DBG_OFFSET: 583 if (alt[0].mid == USB_TYPEC_NVIDIA_VLINK_DP_VDO) 584 alt[0].mid = USB_TYPEC_NVIDIA_VLINK_DBG_VDO; 585 break; 586 default: 587 break; 588 } 589 } 590 591 static int ucsi_ccg_read_version(struct ucsi *ucsi, u16 *version) 592 { 593 struct ucsi_ccg *uc = ucsi_get_drvdata(ucsi); 594 u16 reg = CCGX_RAB_UCSI_DATA_BLOCK(UCSI_VERSION); 595 596 return ccg_read(uc, reg, (u8 *)version, sizeof(*version)); 597 } 598 599 static int ucsi_ccg_read_cci(struct ucsi *ucsi, u32 *cci) 600 { 601 struct ucsi_ccg *uc = ucsi_get_drvdata(ucsi); 602 603 spin_lock(&uc->op_lock); 604 *cci = uc->op_data.cci; 605 spin_unlock(&uc->op_lock); 606 607 return 0; 608 } 609 610 static int ucsi_ccg_read_message_in(struct ucsi *ucsi, void *val, size_t val_len) 611 { 612 struct ucsi_ccg *uc = ucsi_get_drvdata(ucsi); 613 614 spin_lock(&uc->op_lock); 615 memcpy(val, uc->op_data.message_in, val_len); 616 spin_unlock(&uc->op_lock); 617 618 return 0; 619 } 620 621 static int ucsi_ccg_async_control(struct ucsi *ucsi, u64 command) 622 { 623 struct ucsi_ccg *uc = ucsi_get_drvdata(ucsi); 624 u16 reg = CCGX_RAB_UCSI_DATA_BLOCK(UCSI_CONTROL); 625 626 /* 627 * UCSI may read CCI instantly after async_control, 628 * clear CCI to avoid caller getting wrong data before we get CCI from ISR 629 */ 630 spin_lock(&uc->op_lock); 631 uc->op_data.cci = 0; 632 spin_unlock(&uc->op_lock); 633 634 return ccg_write(uc, reg, (u8 *)&command, sizeof(command)); 635 } 636 637 static int ucsi_ccg_sync_control(struct ucsi *ucsi, u64 command, u32 *cci, 638 void *data, size_t size, void *msg_out, 639 size_t msg_out_size) 640 { 641 struct ucsi_ccg *uc = ucsi_get_drvdata(ucsi); 642 struct ucsi_connector *con; 643 int con_index; 644 int ret; 645 646 mutex_lock(&uc->lock); 647 pm_runtime_get_sync(uc->dev); 648 649 if (UCSI_COMMAND(command) == UCSI_SET_NEW_CAM && 650 uc->has_multiple_dp) { 651 con_index = (command >> 16) & 652 UCSI_CMD_CONNECTOR_MASK; 653 if (con_index == 0) { 654 ret = -EINVAL; 655 goto err_put; 656 } 657 con = &uc->ucsi->connector[con_index - 1]; 658 ucsi_ccg_update_set_new_cam_cmd(uc, con, &command); 659 } 660 661 ret = ucsi_sync_control_common(ucsi, command, cci, data, size, 662 msg_out, msg_out_size); 663 664 switch (UCSI_COMMAND(command)) { 665 case UCSI_GET_CURRENT_CAM: 666 if (!ret && uc->has_multiple_dp && 667 (*cci & UCSI_CCI_COMMAND_COMPLETE) && 668 !(*cci & UCSI_CCI_ERROR)) 669 ret = ucsi_ccg_update_get_current_cam_cmd(uc, (u8 *)data); 670 break; 671 case UCSI_GET_ALTERNATE_MODES: 672 if (UCSI_ALTMODE_RECIPIENT(command) == UCSI_RECIPIENT_SOP) { 673 struct ucsi_altmode *alt = data; 674 675 if (alt[0].svid == USB_TYPEC_NVIDIA_VLINK_SID) 676 ucsi_ccg_nvidia_altmode(uc, alt, command); 677 } 678 break; 679 case UCSI_GET_CAPABILITY: 680 if (uc->fw_build == CCG_FW_BUILD_NVIDIA_TEGRA) { 681 struct ucsi_capability *cap = data; 682 683 cap->features &= ~UCSI_CAP_ALT_MODE_DETAILS; 684 } 685 break; 686 default: 687 break; 688 } 689 690 err_put: 691 pm_runtime_put_sync(uc->dev); 692 mutex_unlock(&uc->lock); 693 694 return ret; 695 } 696 697 static const struct ucsi_operations ucsi_ccg_ops = { 698 .read_version = ucsi_ccg_read_version, 699 .read_cci = ucsi_ccg_read_cci, 700 .poll_cci = ucsi_ccg_read_cci, 701 .read_message_in = ucsi_ccg_read_message_in, 702 .sync_control = ucsi_ccg_sync_control, 703 .async_control = ucsi_ccg_async_control, 704 .update_altmodes = ucsi_ccg_update_altmodes 705 }; 706 707 static irqreturn_t ccg_irq_handler(int irq, void *data) 708 { 709 u16 reg = CCGX_RAB_UCSI_DATA_BLOCK(UCSI_CCI); 710 struct ucsi_ccg *uc = data; 711 u8 intr_reg; 712 u32 cci = 0; 713 int ret = 0; 714 715 ret = ccg_read(uc, CCGX_RAB_INTR_REG, &intr_reg, sizeof(intr_reg)); 716 if (ret) 717 return ret; 718 719 if (!intr_reg) 720 return IRQ_HANDLED; 721 else if (!(intr_reg & UCSI_READ_INT)) 722 goto err_clear_irq; 723 724 ret = ccg_read(uc, reg, (void *)&cci, sizeof(cci)); 725 if (ret) 726 goto err_clear_irq; 727 728 /* 729 * As per CCGx UCSI interface guide, copy CCI and MESSAGE_IN 730 * to the OpRegion before clear the UCSI interrupt 731 */ 732 ret = ccg_op_region_update(uc, cci); 733 if (ret) 734 goto err_clear_irq; 735 736 err_clear_irq: 737 ccg_write(uc, CCGX_RAB_INTR_REG, &intr_reg, sizeof(intr_reg)); 738 739 if (!ret) 740 ucsi_notify_common(uc->ucsi, cci); 741 742 return IRQ_HANDLED; 743 } 744 745 static int ccg_request_irq(struct ucsi_ccg *uc) 746 { 747 unsigned long flags = IRQF_ONESHOT; 748 749 if (!dev_fwnode(uc->dev)) 750 flags |= IRQF_TRIGGER_HIGH; 751 752 return request_threaded_irq(uc->irq, NULL, ccg_irq_handler, flags, dev_name(uc->dev), uc); 753 } 754 755 static void ccg_pm_workaround_work(struct work_struct *pm_work) 756 { 757 ccg_irq_handler(0, container_of(pm_work, struct ucsi_ccg, pm_work)); 758 } 759 760 static int get_fw_info(struct ucsi_ccg *uc) 761 { 762 int err; 763 764 err = ccg_read(uc, CCGX_RAB_READ_ALL_VER, (u8 *)(&uc->version), 765 sizeof(uc->version)); 766 if (err < 0) 767 return err; 768 769 uc->fw_version = CCG_VERSION(uc->version[FW2].app.ver) | 770 CCG_VERSION_PATCH(uc->version[FW2].app.patch); 771 772 err = ccg_read(uc, CCGX_RAB_DEVICE_MODE, (u8 *)(&uc->info), 773 sizeof(uc->info)); 774 if (err < 0) 775 return err; 776 777 return 0; 778 } 779 780 static inline bool invalid_async_evt(int code) 781 { 782 return (code >= CCG_EVENT_MAX) || (code < EVENT_INDEX); 783 } 784 785 static void ccg_process_response(struct ucsi_ccg *uc) 786 { 787 struct device *dev = uc->dev; 788 789 if (uc->dev_resp.code & ASYNC_EVENT) { 790 if (uc->dev_resp.code == RESET_COMPLETE) { 791 if (test_bit(RESET_PENDING, &uc->flags)) 792 uc->cmd_resp = uc->dev_resp.code; 793 get_fw_info(uc); 794 } 795 if (invalid_async_evt(uc->dev_resp.code)) 796 dev_err(dev, "invalid async evt %d\n", 797 uc->dev_resp.code); 798 } else { 799 if (test_bit(DEV_CMD_PENDING, &uc->flags)) { 800 uc->cmd_resp = uc->dev_resp.code; 801 clear_bit(DEV_CMD_PENDING, &uc->flags); 802 } else { 803 dev_err(dev, "dev resp 0x%04x but no cmd pending\n", 804 uc->dev_resp.code); 805 } 806 } 807 } 808 809 static int ccg_read_response(struct ucsi_ccg *uc) 810 { 811 unsigned long target = jiffies + msecs_to_jiffies(1000); 812 struct device *dev = uc->dev; 813 u8 intval; 814 int status; 815 816 /* wait for interrupt status to get updated */ 817 do { 818 status = ccg_read(uc, CCGX_RAB_INTR_REG, &intval, 819 sizeof(intval)); 820 if (status < 0) 821 return status; 822 823 if (intval & DEV_INT) 824 break; 825 usleep_range(500, 600); 826 } while (time_is_after_jiffies(target)); 827 828 if (time_is_before_jiffies(target)) { 829 dev_err(dev, "response timeout error\n"); 830 return -ETIME; 831 } 832 833 status = ccg_read(uc, CCGX_RAB_RESPONSE, (u8 *)&uc->dev_resp, 834 sizeof(uc->dev_resp)); 835 if (status < 0) 836 return status; 837 838 status = ccg_write(uc, CCGX_RAB_INTR_REG, &intval, sizeof(intval)); 839 if (status < 0) 840 return status; 841 842 return 0; 843 } 844 845 /* Caller must hold uc->lock */ 846 static int ccg_send_command(struct ucsi_ccg *uc, struct ccg_cmd *cmd) 847 { 848 struct device *dev = uc->dev; 849 int ret; 850 851 switch (cmd->reg & 0xF000) { 852 case DEV_REG_IDX: 853 set_bit(DEV_CMD_PENDING, &uc->flags); 854 break; 855 default: 856 dev_err(dev, "invalid cmd register\n"); 857 break; 858 } 859 860 ret = ccg_write(uc, cmd->reg, (u8 *)&cmd->data, cmd->len); 861 if (ret < 0) 862 return ret; 863 864 msleep(cmd->delay); 865 866 ret = ccg_read_response(uc); 867 if (ret < 0) { 868 dev_err(dev, "response read error\n"); 869 switch (cmd->reg & 0xF000) { 870 case DEV_REG_IDX: 871 clear_bit(DEV_CMD_PENDING, &uc->flags); 872 break; 873 default: 874 dev_err(dev, "invalid cmd register\n"); 875 break; 876 } 877 return -EIO; 878 } 879 ccg_process_response(uc); 880 881 return uc->cmd_resp; 882 } 883 884 static int ccg_cmd_enter_flashing(struct ucsi_ccg *uc) 885 { 886 struct ccg_cmd cmd; 887 int ret; 888 889 cmd.reg = CCGX_RAB_ENTER_FLASHING; 890 cmd.data = FLASH_ENTER_SIG; 891 cmd.len = 1; 892 cmd.delay = 50; 893 894 mutex_lock(&uc->lock); 895 896 ret = ccg_send_command(uc, &cmd); 897 898 mutex_unlock(&uc->lock); 899 900 if (ret != CMD_SUCCESS) { 901 dev_err(uc->dev, "enter flashing failed ret=%d\n", ret); 902 return ret; 903 } 904 905 return 0; 906 } 907 908 static int ccg_cmd_reset(struct ucsi_ccg *uc) 909 { 910 struct ccg_cmd cmd; 911 u8 *p; 912 int ret; 913 914 p = (u8 *)&cmd.data; 915 cmd.reg = CCGX_RAB_RESET_REQ; 916 p[0] = RESET_SIG; 917 p[1] = CMD_RESET_DEV; 918 cmd.len = 2; 919 cmd.delay = 5000; 920 921 mutex_lock(&uc->lock); 922 923 set_bit(RESET_PENDING, &uc->flags); 924 925 ret = ccg_send_command(uc, &cmd); 926 if (ret != RESET_COMPLETE) 927 goto err_clear_flag; 928 929 ret = 0; 930 931 err_clear_flag: 932 clear_bit(RESET_PENDING, &uc->flags); 933 934 mutex_unlock(&uc->lock); 935 936 return ret; 937 } 938 939 static int ccg_cmd_port_control(struct ucsi_ccg *uc, bool enable) 940 { 941 struct ccg_cmd cmd; 942 int ret; 943 944 cmd.reg = CCGX_RAB_PDPORT_ENABLE; 945 if (enable) 946 cmd.data = (uc->port_num == 1) ? 947 PDPORT_1 : (PDPORT_1 | PDPORT_2); 948 else 949 cmd.data = 0x0; 950 cmd.len = 1; 951 cmd.delay = 10; 952 953 mutex_lock(&uc->lock); 954 955 ret = ccg_send_command(uc, &cmd); 956 957 mutex_unlock(&uc->lock); 958 959 if (ret != CMD_SUCCESS) { 960 dev_err(uc->dev, "port control failed ret=%d\n", ret); 961 return ret; 962 } 963 return 0; 964 } 965 966 static int ccg_cmd_jump_boot_mode(struct ucsi_ccg *uc, int bl_mode) 967 { 968 struct ccg_cmd cmd; 969 int ret; 970 971 cmd.reg = CCGX_RAB_JUMP_TO_BOOT; 972 973 if (bl_mode) 974 cmd.data = TO_BOOT; 975 else 976 cmd.data = TO_ALT_FW; 977 978 cmd.len = 1; 979 cmd.delay = 100; 980 981 mutex_lock(&uc->lock); 982 983 set_bit(RESET_PENDING, &uc->flags); 984 985 ret = ccg_send_command(uc, &cmd); 986 if (ret != RESET_COMPLETE) 987 goto err_clear_flag; 988 989 ret = 0; 990 991 err_clear_flag: 992 clear_bit(RESET_PENDING, &uc->flags); 993 994 mutex_unlock(&uc->lock); 995 996 return ret; 997 } 998 999 static int 1000 ccg_cmd_write_flash_row(struct ucsi_ccg *uc, u16 row, 1001 const void *data, u8 fcmd) 1002 { 1003 struct i2c_client *client = uc->client; 1004 struct ccg_cmd cmd; 1005 u8 buf[CCG4_ROW_SIZE + 2]; 1006 u8 *p; 1007 int ret; 1008 1009 /* Copy the data into the flash read/write memory. */ 1010 put_unaligned_le16(REG_FLASH_RW_MEM, buf); 1011 1012 memcpy(buf + 2, data, CCG4_ROW_SIZE); 1013 1014 mutex_lock(&uc->lock); 1015 1016 ret = i2c_master_send(client, buf, CCG4_ROW_SIZE + 2); 1017 if (ret != CCG4_ROW_SIZE + 2) { 1018 dev_err(uc->dev, "REG_FLASH_RW_MEM write fail %d\n", ret); 1019 mutex_unlock(&uc->lock); 1020 return ret < 0 ? ret : -EIO; 1021 } 1022 1023 /* Use the FLASH_ROW_READ_WRITE register to trigger */ 1024 /* writing of data to the desired flash row */ 1025 p = (u8 *)&cmd.data; 1026 cmd.reg = CCGX_RAB_FLASH_ROW_RW; 1027 p[0] = FLASH_SIG; 1028 p[1] = fcmd; 1029 put_unaligned_le16(row, &p[2]); 1030 cmd.len = 4; 1031 cmd.delay = 50; 1032 if (fcmd == FLASH_FWCT_SIG_WR_CMD) 1033 cmd.delay += 400; 1034 if (row == 510) 1035 cmd.delay += 220; 1036 ret = ccg_send_command(uc, &cmd); 1037 1038 mutex_unlock(&uc->lock); 1039 1040 if (ret != CMD_SUCCESS) { 1041 dev_err(uc->dev, "write flash row failed ret=%d\n", ret); 1042 return ret; 1043 } 1044 1045 return 0; 1046 } 1047 1048 static int ccg_cmd_validate_fw(struct ucsi_ccg *uc, unsigned int fwid) 1049 { 1050 struct ccg_cmd cmd; 1051 int ret; 1052 1053 cmd.reg = CCGX_RAB_VALIDATE_FW; 1054 cmd.data = fwid; 1055 cmd.len = 1; 1056 cmd.delay = 500; 1057 1058 mutex_lock(&uc->lock); 1059 1060 ret = ccg_send_command(uc, &cmd); 1061 1062 mutex_unlock(&uc->lock); 1063 1064 if (ret != CMD_SUCCESS) 1065 return ret; 1066 1067 return 0; 1068 } 1069 1070 static bool ccg_check_vendor_version(struct ucsi_ccg *uc, 1071 struct version_format *app, 1072 struct fw_config_table *fw_cfg) 1073 { 1074 struct device *dev = uc->dev; 1075 1076 /* Check if the fw build is for supported vendors */ 1077 if (le16_to_cpu(app->build) != uc->fw_build) { 1078 dev_info(dev, "current fw is not from supported vendor\n"); 1079 return false; 1080 } 1081 1082 /* Check if the new fw build is for supported vendors */ 1083 if (le16_to_cpu(fw_cfg->app.build) != uc->fw_build) { 1084 dev_info(dev, "new fw is not from supported vendor\n"); 1085 return false; 1086 } 1087 return true; 1088 } 1089 1090 static bool ccg_check_fw_version(struct ucsi_ccg *uc, const char *fw_name, 1091 struct version_format *app) 1092 { 1093 const struct firmware *fw = NULL; 1094 struct device *dev = uc->dev; 1095 struct fw_config_table fw_cfg; 1096 u32 cur_version, new_version; 1097 bool is_later = false; 1098 1099 if (request_firmware(&fw, fw_name, dev) != 0) { 1100 dev_err(dev, "error: Failed to open cyacd file %s\n", fw_name); 1101 return false; 1102 } 1103 1104 /* 1105 * check if signed fw 1106 * last part of fw image is fw cfg table and signature 1107 */ 1108 if (fw->size < sizeof(fw_cfg) + FW_CFG_TABLE_SIG_SIZE) 1109 goto out_release_firmware; 1110 1111 memcpy((uint8_t *)&fw_cfg, fw->data + fw->size - 1112 sizeof(fw_cfg) - FW_CFG_TABLE_SIG_SIZE, sizeof(fw_cfg)); 1113 1114 if (fw_cfg.identity != ('F' | 'W' << 8 | 'C' << 16 | 'T' << 24)) { 1115 dev_info(dev, "not a signed image\n"); 1116 goto out_release_firmware; 1117 } 1118 1119 /* compare input version with FWCT version */ 1120 cur_version = le16_to_cpu(app->build) | CCG_VERSION_PATCH(app->patch) | 1121 CCG_VERSION(app->ver); 1122 1123 new_version = le16_to_cpu(fw_cfg.app.build) | 1124 CCG_VERSION_PATCH(fw_cfg.app.patch) | 1125 CCG_VERSION(fw_cfg.app.ver); 1126 1127 if (!ccg_check_vendor_version(uc, app, &fw_cfg)) 1128 goto out_release_firmware; 1129 1130 if (new_version > cur_version) 1131 is_later = true; 1132 1133 out_release_firmware: 1134 release_firmware(fw); 1135 return is_later; 1136 } 1137 1138 static int ccg_fw_update_needed(struct ucsi_ccg *uc, 1139 enum enum_flash_mode *mode) 1140 { 1141 struct device *dev = uc->dev; 1142 int err; 1143 struct version_info version[3]; 1144 1145 err = ccg_read(uc, CCGX_RAB_DEVICE_MODE, (u8 *)(&uc->info), 1146 sizeof(uc->info)); 1147 if (err) { 1148 dev_err(dev, "read device mode failed\n"); 1149 return err; 1150 } 1151 1152 err = ccg_read(uc, CCGX_RAB_READ_ALL_VER, (u8 *)version, 1153 sizeof(version)); 1154 if (err) { 1155 dev_err(dev, "read device mode failed\n"); 1156 return err; 1157 } 1158 1159 if (memcmp(&version[FW1], "\0\0\0\0\0\0\0\0", 1160 sizeof(struct version_info)) == 0) { 1161 dev_info(dev, "secondary fw is not flashed\n"); 1162 *mode = SECONDARY_BL; 1163 } else if (le16_to_cpu(version[FW1].base.build) < 1164 secondary_fw_min_ver) { 1165 dev_info(dev, "secondary fw version is too low (< %d)\n", 1166 secondary_fw_min_ver); 1167 *mode = SECONDARY; 1168 } else if (memcmp(&version[FW2], "\0\0\0\0\0\0\0\0", 1169 sizeof(struct version_info)) == 0) { 1170 dev_info(dev, "primary fw is not flashed\n"); 1171 *mode = PRIMARY; 1172 } else if (ccg_check_fw_version(uc, ccg_fw_names[PRIMARY], 1173 &version[FW2].app)) { 1174 dev_info(dev, "found primary fw with later version\n"); 1175 *mode = PRIMARY; 1176 } else { 1177 dev_info(dev, "secondary and primary fw are the latest\n"); 1178 *mode = FLASH_NOT_NEEDED; 1179 } 1180 return 0; 1181 } 1182 1183 static int do_flash(struct ucsi_ccg *uc, enum enum_flash_mode mode) 1184 { 1185 struct device *dev = uc->dev; 1186 const struct firmware *fw = NULL; 1187 const char *p, *s; 1188 const char *eof; 1189 int err, row, len, line_sz, line_cnt = 0; 1190 unsigned long start_time = jiffies; 1191 struct fw_config_table fw_cfg; 1192 u8 fw_cfg_sig[FW_CFG_TABLE_SIG_SIZE]; 1193 u8 *wr_buf; 1194 1195 err = request_firmware(&fw, ccg_fw_names[mode], dev); 1196 if (err) { 1197 dev_err(dev, "request %s failed err=%d\n", 1198 ccg_fw_names[mode], err); 1199 return err; 1200 } 1201 1202 if (((uc->info.mode & CCG_DEVINFO_FWMODE_MASK) >> 1203 CCG_DEVINFO_FWMODE_SHIFT) == FW2) { 1204 err = ccg_cmd_port_control(uc, false); 1205 if (err < 0) 1206 goto release_fw; 1207 err = ccg_cmd_jump_boot_mode(uc, 0); 1208 if (err < 0) 1209 goto release_fw; 1210 } 1211 1212 eof = fw->data + fw->size; 1213 1214 /* 1215 * check if signed fw 1216 * last part of fw image is fw cfg table and signature 1217 */ 1218 if (fw->size < sizeof(fw_cfg) + sizeof(fw_cfg_sig)) 1219 goto not_signed_fw; 1220 1221 memcpy((uint8_t *)&fw_cfg, fw->data + fw->size - 1222 sizeof(fw_cfg) - sizeof(fw_cfg_sig), sizeof(fw_cfg)); 1223 1224 if (fw_cfg.identity != ('F' | ('W' << 8) | ('C' << 16) | ('T' << 24))) { 1225 dev_info(dev, "not a signed image\n"); 1226 goto not_signed_fw; 1227 } 1228 eof = fw->data + fw->size - sizeof(fw_cfg) - sizeof(fw_cfg_sig); 1229 1230 memcpy((uint8_t *)&fw_cfg_sig, 1231 fw->data + fw->size - sizeof(fw_cfg_sig), sizeof(fw_cfg_sig)); 1232 1233 /* flash fw config table and signature first */ 1234 err = ccg_cmd_write_flash_row(uc, 0, (u8 *)&fw_cfg, 1235 FLASH_FWCT1_WR_CMD); 1236 if (err) 1237 goto release_fw; 1238 1239 err = ccg_cmd_write_flash_row(uc, 0, (u8 *)&fw_cfg + CCG4_ROW_SIZE, 1240 FLASH_FWCT2_WR_CMD); 1241 if (err) 1242 goto release_fw; 1243 1244 err = ccg_cmd_write_flash_row(uc, 0, &fw_cfg_sig, 1245 FLASH_FWCT_SIG_WR_CMD); 1246 if (err) 1247 goto release_fw; 1248 1249 not_signed_fw: 1250 wr_buf = kzalloc(CCG4_ROW_SIZE + 4, GFP_KERNEL); 1251 if (!wr_buf) { 1252 err = -ENOMEM; 1253 goto release_fw; 1254 } 1255 1256 err = ccg_cmd_enter_flashing(uc); 1257 if (err) 1258 goto release_mem; 1259 1260 /***************************************************************** 1261 * CCG firmware image (.cyacd) file line format 1262 * 1263 * :00rrrrllll[dd....]cc/r/n 1264 * 1265 * :00 header 1266 * rrrr is row number to flash (4 char) 1267 * llll is data len to flash (4 char) 1268 * dd is a data field represents one byte of data (512 char) 1269 * cc is checksum (2 char) 1270 * \r\n newline 1271 * 1272 * Total length: 3 + 4 + 4 + 512 + 2 + 2 = 527 1273 * 1274 *****************************************************************/ 1275 1276 p = strnchr(fw->data, fw->size, ':'); 1277 if (!p) { 1278 dev_err(dev, "Bad FW format: no ':' record header found\n"); 1279 err = -EINVAL; 1280 goto release_mem; 1281 } 1282 while (p < eof) { 1283 s = strnchr(p + 1, eof - p - 1, ':'); 1284 1285 if (!s) 1286 s = eof; 1287 1288 line_sz = s - p; 1289 1290 if (line_sz != CYACD_LINE_SIZE) { 1291 dev_err(dev, "Bad FW format line_sz=%d\n", line_sz); 1292 err = -EINVAL; 1293 goto release_mem; 1294 } 1295 1296 if (hex2bin(wr_buf, p + 3, CCG4_ROW_SIZE + 4)) { 1297 err = -EINVAL; 1298 goto release_mem; 1299 } 1300 1301 row = get_unaligned_be16(wr_buf); 1302 len = get_unaligned_be16(&wr_buf[2]); 1303 1304 if (len != CCG4_ROW_SIZE) { 1305 err = -EINVAL; 1306 goto release_mem; 1307 } 1308 1309 err = ccg_cmd_write_flash_row(uc, row, wr_buf + 4, 1310 FLASH_WR_CMD); 1311 if (err) 1312 goto release_mem; 1313 1314 line_cnt++; 1315 p = s; 1316 } 1317 1318 dev_info(dev, "total %d row flashed. time: %dms\n", 1319 line_cnt, jiffies_to_msecs(jiffies - start_time)); 1320 1321 err = ccg_cmd_validate_fw(uc, (mode == PRIMARY) ? FW2 : FW1); 1322 if (err) 1323 dev_err(dev, "%s validation failed err=%d\n", 1324 (mode == PRIMARY) ? "FW2" : "FW1", err); 1325 else 1326 dev_info(dev, "%s validated\n", 1327 (mode == PRIMARY) ? "FW2" : "FW1"); 1328 1329 err = ccg_cmd_port_control(uc, false); 1330 if (err < 0) 1331 goto release_mem; 1332 1333 err = ccg_cmd_reset(uc); 1334 if (err < 0) 1335 goto release_mem; 1336 1337 err = ccg_cmd_port_control(uc, true); 1338 if (err < 0) 1339 goto release_mem; 1340 1341 release_mem: 1342 kfree(wr_buf); 1343 1344 release_fw: 1345 release_firmware(fw); 1346 return err; 1347 } 1348 1349 /******************************************************************************* 1350 * CCG4 has two copies of the firmware in addition to the bootloader. 1351 * If the device is running FW1, FW2 can be updated with the new version. 1352 * Dual firmware mode allows the CCG device to stay in a PD contract and support 1353 * USB PD and Type-C functionality while a firmware update is in progress. 1354 ******************************************************************************/ 1355 static int ccg_fw_update(struct ucsi_ccg *uc, enum enum_flash_mode flash_mode) 1356 { 1357 int err = 0; 1358 1359 while (flash_mode != FLASH_NOT_NEEDED) { 1360 err = do_flash(uc, flash_mode); 1361 if (err < 0) 1362 return err; 1363 err = ccg_fw_update_needed(uc, &flash_mode); 1364 if (err < 0) 1365 return err; 1366 } 1367 dev_info(uc->dev, "CCG FW update successful\n"); 1368 1369 return err; 1370 } 1371 1372 static int ccg_restart(struct ucsi_ccg *uc) 1373 { 1374 struct device *dev = uc->dev; 1375 int status; 1376 1377 status = ucsi_ccg_init(uc); 1378 if (status < 0) { 1379 dev_err(dev, "ucsi_ccg_start fail, err=%d\n", status); 1380 return status; 1381 } 1382 1383 status = ccg_request_irq(uc); 1384 if (status < 0) { 1385 dev_err(dev, "request_threaded_irq failed - %d\n", status); 1386 return status; 1387 } 1388 1389 status = ucsi_register(uc->ucsi); 1390 if (status) { 1391 dev_err(uc->dev, "failed to register the interface\n"); 1392 return status; 1393 } 1394 1395 pm_runtime_enable(uc->dev); 1396 return 0; 1397 } 1398 1399 static void ccg_update_firmware(struct work_struct *work) 1400 { 1401 struct ucsi_ccg *uc = container_of(work, struct ucsi_ccg, work); 1402 enum enum_flash_mode flash_mode; 1403 int status; 1404 1405 status = ccg_fw_update_needed(uc, &flash_mode); 1406 if (status < 0) 1407 return; 1408 1409 if (flash_mode != FLASH_NOT_NEEDED) { 1410 ucsi_unregister(uc->ucsi); 1411 pm_runtime_disable(uc->dev); 1412 free_irq(uc->irq, uc); 1413 1414 ccg_fw_update(uc, flash_mode); 1415 ccg_restart(uc); 1416 } 1417 } 1418 1419 static ssize_t do_flash_store(struct device *dev, 1420 struct device_attribute *attr, 1421 const char *buf, size_t n) 1422 { 1423 struct ucsi_ccg *uc = i2c_get_clientdata(to_i2c_client(dev)); 1424 bool flash; 1425 1426 if (kstrtobool(buf, &flash)) 1427 return -EINVAL; 1428 1429 if (!flash) 1430 return n; 1431 1432 schedule_work(&uc->work); 1433 return n; 1434 } 1435 1436 static umode_t ucsi_ccg_attrs_is_visible(struct kobject *kobj, struct attribute *attr, int idx) 1437 { 1438 struct device *dev = kobj_to_dev(kobj); 1439 struct ucsi_ccg *uc = i2c_get_clientdata(to_i2c_client(dev)); 1440 1441 if (!uc->fw_build) 1442 return 0; 1443 1444 return attr->mode; 1445 } 1446 1447 static DEVICE_ATTR_WO(do_flash); 1448 1449 static struct attribute *ucsi_ccg_attrs[] = { 1450 &dev_attr_do_flash.attr, 1451 NULL, 1452 }; 1453 static struct attribute_group ucsi_ccg_attr_group = { 1454 .attrs = ucsi_ccg_attrs, 1455 .is_visible = ucsi_ccg_attrs_is_visible, 1456 }; 1457 static const struct attribute_group *ucsi_ccg_groups[] = { 1458 &ucsi_ccg_attr_group, 1459 NULL, 1460 }; 1461 1462 static int ucsi_ccg_probe(struct i2c_client *client) 1463 { 1464 struct device *dev = &client->dev; 1465 struct ucsi_ccg *uc; 1466 const char *fw_name; 1467 int status; 1468 1469 uc = devm_kzalloc_obj(dev, *uc); 1470 if (!uc) 1471 return -ENOMEM; 1472 1473 uc->dev = dev; 1474 uc->client = client; 1475 uc->irq = client->irq; 1476 mutex_init(&uc->lock); 1477 INIT_WORK(&uc->work, ccg_update_firmware); 1478 INIT_WORK(&uc->pm_work, ccg_pm_workaround_work); 1479 1480 /* Only fail FW flashing when FW build information is not provided */ 1481 status = device_property_read_string(dev, "firmware-name", &fw_name); 1482 if (!status) { 1483 if (!strcmp(fw_name, "nvidia,jetson-agx-xavier")) 1484 uc->fw_build = CCG_FW_BUILD_NVIDIA_TEGRA; 1485 else if (!strcmp(fw_name, "nvidia,gpu")) 1486 uc->fw_build = CCG_FW_BUILD_NVIDIA; 1487 if (!uc->fw_build) 1488 dev_err(uc->dev, "failed to get FW build information\n"); 1489 } 1490 1491 /* reset ccg device and initialize ucsi */ 1492 status = ucsi_ccg_init(uc); 1493 if (status < 0) { 1494 dev_err(uc->dev, "ucsi_ccg_init failed - %d\n", status); 1495 return status; 1496 } 1497 1498 status = get_fw_info(uc); 1499 if (status < 0) { 1500 dev_err(uc->dev, "get_fw_info failed - %d\n", status); 1501 return status; 1502 } 1503 1504 uc->port_num = 1; 1505 1506 if (uc->info.mode & CCG_DEVINFO_PDPORTS_MASK) 1507 uc->port_num++; 1508 1509 uc->ucsi = ucsi_create(dev, &ucsi_ccg_ops); 1510 if (IS_ERR(uc->ucsi)) 1511 return PTR_ERR(uc->ucsi); 1512 1513 ucsi_set_drvdata(uc->ucsi, uc); 1514 1515 status = ccg_request_irq(uc); 1516 if (status < 0) { 1517 dev_err(uc->dev, "request_threaded_irq failed - %d\n", status); 1518 goto out_ucsi_destroy; 1519 } 1520 1521 status = ucsi_register(uc->ucsi); 1522 if (status) 1523 goto out_free_irq; 1524 1525 i2c_set_clientdata(client, uc); 1526 1527 device_disable_async_suspend(uc->dev); 1528 1529 pm_runtime_set_active(uc->dev); 1530 pm_runtime_enable(uc->dev); 1531 pm_runtime_use_autosuspend(uc->dev); 1532 pm_runtime_set_autosuspend_delay(uc->dev, 5000); 1533 pm_runtime_idle(uc->dev); 1534 1535 return 0; 1536 1537 out_free_irq: 1538 free_irq(uc->irq, uc); 1539 out_ucsi_destroy: 1540 ucsi_destroy(uc->ucsi); 1541 1542 return status; 1543 } 1544 1545 static void ucsi_ccg_remove(struct i2c_client *client) 1546 { 1547 struct ucsi_ccg *uc = i2c_get_clientdata(client); 1548 1549 cancel_work_sync(&uc->pm_work); 1550 cancel_work_sync(&uc->work); 1551 pm_runtime_disable(uc->dev); 1552 ucsi_unregister(uc->ucsi); 1553 free_irq(uc->irq, uc); 1554 ucsi_destroy(uc->ucsi); 1555 } 1556 1557 static const struct of_device_id ucsi_ccg_of_match_table[] = { 1558 { .compatible = "cypress,cypd4226", }, 1559 { /* sentinel */ } 1560 }; 1561 MODULE_DEVICE_TABLE(of, ucsi_ccg_of_match_table); 1562 1563 static const struct i2c_device_id ucsi_ccg_device_id[] = { 1564 { .name = "ccgx-ucsi" }, 1565 { } 1566 }; 1567 MODULE_DEVICE_TABLE(i2c, ucsi_ccg_device_id); 1568 1569 static const struct acpi_device_id amd_i2c_ucsi_match[] = { 1570 {"AMDI0042"}, 1571 {} 1572 }; 1573 MODULE_DEVICE_TABLE(acpi, amd_i2c_ucsi_match); 1574 1575 static int ucsi_ccg_resume(struct device *dev) 1576 { 1577 struct i2c_client *client = to_i2c_client(dev); 1578 struct ucsi_ccg *uc = i2c_get_clientdata(client); 1579 1580 return ucsi_resume(uc->ucsi); 1581 } 1582 1583 static int ucsi_ccg_runtime_suspend(struct device *dev) 1584 { 1585 return 0; 1586 } 1587 1588 static int ucsi_ccg_runtime_resume(struct device *dev) 1589 { 1590 struct i2c_client *client = to_i2c_client(dev); 1591 struct ucsi_ccg *uc = i2c_get_clientdata(client); 1592 1593 /* 1594 * Firmware version 3.1.10 or earlier, built for NVIDIA has known issue 1595 * of missing interrupt when a device is connected for runtime resume. 1596 * Schedule a work to call ISR as a workaround. 1597 */ 1598 if (uc->fw_build == CCG_FW_BUILD_NVIDIA && 1599 uc->fw_version <= CCG_OLD_FW_VERSION) 1600 schedule_work(&uc->pm_work); 1601 1602 return 0; 1603 } 1604 1605 static const struct dev_pm_ops ucsi_ccg_pm = { 1606 .resume = ucsi_ccg_resume, 1607 .runtime_suspend = ucsi_ccg_runtime_suspend, 1608 .runtime_resume = ucsi_ccg_runtime_resume, 1609 }; 1610 1611 static struct i2c_driver ucsi_ccg_driver = { 1612 .driver = { 1613 .name = "ucsi_ccg", 1614 .pm = &ucsi_ccg_pm, 1615 .dev_groups = ucsi_ccg_groups, 1616 .acpi_match_table = amd_i2c_ucsi_match, 1617 .of_match_table = ucsi_ccg_of_match_table, 1618 }, 1619 .probe = ucsi_ccg_probe, 1620 .remove = ucsi_ccg_remove, 1621 .id_table = ucsi_ccg_device_id, 1622 }; 1623 1624 module_i2c_driver(ucsi_ccg_driver); 1625 1626 MODULE_AUTHOR("Ajay Gupta <ajayg@nvidia.com>"); 1627 MODULE_DESCRIPTION("UCSI driver for Cypress CCGx Type-C controller"); 1628 MODULE_LICENSE("GPL v2"); 1629