Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

usb: typec: ucsi: ccg: add firmware flashing support

CCGx has two copies of the firmware in addition to the bootloader.
If the device is running FW1, FW2 can be updated with the new version.
Dual firmware mode allows the CCG device to stay in a PD contract and
support USB PD and Type-C functionality while a firmware update is in
progress.

First we read the currently flashed firmware version of both
primary and secondary firmware and then compare it with
version of firmware file to determine if flashing is required.

Command framework is added to support sending commands to CCGx
controller. We wait for response after sending the command and then
read the response from RAB_RESPONSE register.

Below commands are supported,
- ENTER_FLASHING
- RESET
- PDPORT_ENABLE
- JUMP_TO_BOOT
- FLASH_ROW_RW
- VALIDATE_FW

Command specific mutex lock is also added to sync between driver
and user threads.

PD port number information is added which is required while sending
PD_PORT_ENABLE command

Signed-off-by: Ajay Gupta <ajayg@nvidia.com>
[ heikki: Added ABI documentation. ]
Signed-off-by: Heikki Krogerus <heikki.krogerus@linux.intel.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

authored by

Ajay Gupta and committed by
Greg Kroah-Hartman
5c9ae5a8 5fd958a4

+820 -13
+6
Documentation/ABI/testing/sysfs-driver-ucsi-ccg
··· 1 + What: /sys/bus/i2c/drivers/ucsi_ccg/.../do_flash 2 + Date: May 2019 3 + Contact: Ajay Gupta <ajayg@nvidia.com> 4 + Description: 5 + Tell the driver for Cypress CCGx Type-C controller to attempt 6 + firmware upgrade by writing [Yy1] to the file.
+814 -13
drivers/usb/typec/ucsi/ucsi_ccg.c
··· 9 9 */ 10 10 #include <linux/acpi.h> 11 11 #include <linux/delay.h> 12 + #include <linux/firmware.h> 12 13 #include <linux/i2c.h> 13 14 #include <linux/module.h> 14 15 #include <linux/pci.h> ··· 23 22 FW1, /* FW partition-1 (contains secondary fw) */ 24 23 FW2, /* FW partition-2 (contains primary fw) */ 25 24 FW_INVALID, 25 + }; 26 + 27 + #define CCGX_RAB_DEVICE_MODE 0x0000 28 + #define CCGX_RAB_INTR_REG 0x0006 29 + #define DEV_INT BIT(0) 30 + #define PORT0_INT BIT(1) 31 + #define PORT1_INT BIT(2) 32 + #define UCSI_READ_INT BIT(7) 33 + #define CCGX_RAB_JUMP_TO_BOOT 0x0007 34 + #define TO_BOOT 'J' 35 + #define TO_ALT_FW 'A' 36 + #define CCGX_RAB_RESET_REQ 0x0008 37 + #define RESET_SIG 'R' 38 + #define CMD_RESET_I2C 0x0 39 + #define CMD_RESET_DEV 0x1 40 + #define CCGX_RAB_ENTER_FLASHING 0x000A 41 + #define FLASH_ENTER_SIG 'P' 42 + #define CCGX_RAB_VALIDATE_FW 0x000B 43 + #define CCGX_RAB_FLASH_ROW_RW 0x000C 44 + #define FLASH_SIG 'F' 45 + #define FLASH_RD_CMD 0x0 46 + #define FLASH_WR_CMD 0x1 47 + #define FLASH_FWCT1_WR_CMD 0x2 48 + #define FLASH_FWCT2_WR_CMD 0x3 49 + #define FLASH_FWCT_SIG_WR_CMD 0x4 50 + #define CCGX_RAB_READ_ALL_VER 0x0010 51 + #define CCGX_RAB_READ_FW2_VER 0x0020 52 + #define CCGX_RAB_UCSI_CONTROL 0x0039 53 + #define CCGX_RAB_UCSI_CONTROL_START BIT(0) 54 + #define CCGX_RAB_UCSI_CONTROL_STOP BIT(1) 55 + #define CCGX_RAB_UCSI_DATA_BLOCK(offset) (0xf000 | ((offset) & 0xff)) 56 + #define REG_FLASH_RW_MEM 0x0200 57 + #define DEV_REG_IDX CCGX_RAB_DEVICE_MODE 58 + #define CCGX_RAB_PDPORT_ENABLE 0x002C 59 + #define PDPORT_1 BIT(0) 60 + #define PDPORT_2 BIT(1) 61 + #define CCGX_RAB_RESPONSE 0x007E 62 + #define ASYNC_EVENT BIT(7) 63 + 64 + /* CCGx events & async msg codes */ 65 + #define RESET_COMPLETE 0x80 66 + #define EVENT_INDEX RESET_COMPLETE 67 + #define PORT_CONNECT_DET 0x84 68 + #define PORT_DISCONNECT_DET 0x85 69 + #define ROLE_SWAP_COMPELETE 0x87 70 + 71 + /* ccg firmware */ 72 + #define CYACD_LINE_SIZE 527 73 + #define CCG4_ROW_SIZE 256 74 + #define FW1_METADATA_ROW 0x1FF 75 + #define FW2_METADATA_ROW 0x1FE 76 + #define FW_CFG_TABLE_SIG_SIZE 256 77 + 78 + static int secondary_fw_min_ver = 41; 79 + 80 + enum enum_flash_mode { 81 + SECONDARY_BL, /* update secondary using bootloader */ 82 + PRIMARY, /* update primary using secondary */ 83 + SECONDARY, /* update secondary using primary */ 84 + FLASH_NOT_NEEDED, /* update not required */ 85 + FLASH_INVALID, 86 + }; 87 + 88 + static const char * const ccg_fw_names[] = { 89 + "ccg_boot.cyacd", 90 + "ccg_primary.cyacd", 91 + "ccg_secondary.cyacd" 26 92 }; 27 93 28 94 struct ccg_dev_info { ··· 118 50 struct version_format app; 119 51 }; 120 52 53 + struct fw_config_table { 54 + u32 identity; 55 + u16 table_size; 56 + u8 fwct_version; 57 + u8 is_key_change; 58 + u8 guid[16]; 59 + struct version_format base; 60 + struct version_format app; 61 + u8 primary_fw_digest[32]; 62 + u32 key_exp_length; 63 + u8 key_modulus[256]; 64 + u8 key_exp[4]; 65 + }; 66 + 67 + /* CCGx response codes */ 68 + enum ccg_resp_code { 69 + CMD_NO_RESP = 0x00, 70 + CMD_SUCCESS = 0x02, 71 + FLASH_DATA_AVAILABLE = 0x03, 72 + CMD_INVALID = 0x05, 73 + FLASH_UPDATE_FAIL = 0x07, 74 + INVALID_FW = 0x08, 75 + INVALID_ARG = 0x09, 76 + CMD_NOT_SUPPORT = 0x0A, 77 + TRANSACTION_FAIL = 0x0C, 78 + PD_CMD_FAIL = 0x0D, 79 + UNDEF_ERROR = 0x0F, 80 + INVALID_RESP = 0x10, 81 + }; 82 + 83 + #define CCG_EVENT_MAX (EVENT_INDEX + 43) 84 + 85 + struct ccg_cmd { 86 + u16 reg; 87 + u32 data; 88 + int len; 89 + u32 delay; /* ms delay for cmd timeout */ 90 + }; 91 + 92 + struct ccg_resp { 93 + u8 code; 94 + u8 length; 95 + }; 96 + 121 97 struct ucsi_ccg { 122 98 struct device *dev; 123 99 struct ucsi *ucsi; ··· 170 58 struct ccg_dev_info info; 171 59 /* version info for boot, primary and secondary */ 172 60 struct version_info version[FW2 + 1]; 173 - }; 61 + /* CCG HPI communication flags */ 62 + unsigned long flags; 63 + #define RESET_PENDING 0 64 + #define DEV_CMD_PENDING 1 65 + struct ccg_resp dev_resp; 66 + u8 cmd_resp; 67 + int port_num; 68 + int irq; 69 + struct work_struct work; 70 + struct mutex lock; /* to sync between user and driver thread */ 174 71 175 - #define CCGX_RAB_DEVICE_MODE 0x0000 176 - #define CCGX_RAB_INTR_REG 0x0006 177 - #define CCGX_RAB_READ_ALL_VER 0x0010 178 - #define CCGX_RAB_READ_FW2_VER 0x0020 179 - #define CCGX_RAB_UCSI_CONTROL 0x0039 180 - #define CCGX_RAB_UCSI_CONTROL_START BIT(0) 181 - #define CCGX_RAB_UCSI_CONTROL_STOP BIT(1) 182 - #define CCGX_RAB_UCSI_DATA_BLOCK(offset) (0xf000 | ((offset) & 0xff)) 72 + /* fw build with vendor information */ 73 + u16 fw_build; 74 + }; 183 75 184 76 static int ccg_read(struct ucsi_ccg *uc, u16 rab, u8 *data, u32 len) 185 77 { ··· 392 276 return 0; 393 277 } 394 278 279 + static inline bool invalid_async_evt(int code) 280 + { 281 + return (code >= CCG_EVENT_MAX) || (code < EVENT_INDEX); 282 + } 283 + 284 + static void ccg_process_response(struct ucsi_ccg *uc) 285 + { 286 + struct device *dev = uc->dev; 287 + 288 + if (uc->dev_resp.code & ASYNC_EVENT) { 289 + if (uc->dev_resp.code == RESET_COMPLETE) { 290 + if (test_bit(RESET_PENDING, &uc->flags)) 291 + uc->cmd_resp = uc->dev_resp.code; 292 + get_fw_info(uc); 293 + } 294 + if (invalid_async_evt(uc->dev_resp.code)) 295 + dev_err(dev, "invalid async evt %d\n", 296 + uc->dev_resp.code); 297 + } else { 298 + if (test_bit(DEV_CMD_PENDING, &uc->flags)) { 299 + uc->cmd_resp = uc->dev_resp.code; 300 + clear_bit(DEV_CMD_PENDING, &uc->flags); 301 + } else { 302 + dev_err(dev, "dev resp 0x%04x but no cmd pending\n", 303 + uc->dev_resp.code); 304 + } 305 + } 306 + } 307 + 308 + static int ccg_read_response(struct ucsi_ccg *uc) 309 + { 310 + unsigned long target = jiffies + msecs_to_jiffies(1000); 311 + struct device *dev = uc->dev; 312 + u8 intval; 313 + int status; 314 + 315 + /* wait for interrupt status to get updated */ 316 + do { 317 + status = ccg_read(uc, CCGX_RAB_INTR_REG, &intval, 318 + sizeof(intval)); 319 + if (status < 0) 320 + return status; 321 + 322 + if (intval & DEV_INT) 323 + break; 324 + usleep_range(500, 600); 325 + } while (time_is_after_jiffies(target)); 326 + 327 + if (time_is_before_jiffies(target)) { 328 + dev_err(dev, "response timeout error\n"); 329 + return -ETIME; 330 + } 331 + 332 + status = ccg_read(uc, CCGX_RAB_RESPONSE, (u8 *)&uc->dev_resp, 333 + sizeof(uc->dev_resp)); 334 + if (status < 0) 335 + return status; 336 + 337 + status = ccg_write(uc, CCGX_RAB_INTR_REG, &intval, sizeof(intval)); 338 + if (status < 0) 339 + return status; 340 + 341 + return 0; 342 + } 343 + 344 + /* Caller must hold uc->lock */ 345 + static int ccg_send_command(struct ucsi_ccg *uc, struct ccg_cmd *cmd) 346 + { 347 + struct device *dev = uc->dev; 348 + int ret; 349 + 350 + switch (cmd->reg & 0xF000) { 351 + case DEV_REG_IDX: 352 + set_bit(DEV_CMD_PENDING, &uc->flags); 353 + break; 354 + default: 355 + dev_err(dev, "invalid cmd register\n"); 356 + break; 357 + } 358 + 359 + ret = ccg_write(uc, cmd->reg, (u8 *)&cmd->data, cmd->len); 360 + if (ret < 0) 361 + return ret; 362 + 363 + msleep(cmd->delay); 364 + 365 + ret = ccg_read_response(uc); 366 + if (ret < 0) { 367 + dev_err(dev, "response read error\n"); 368 + switch (cmd->reg & 0xF000) { 369 + case DEV_REG_IDX: 370 + clear_bit(DEV_CMD_PENDING, &uc->flags); 371 + break; 372 + default: 373 + dev_err(dev, "invalid cmd register\n"); 374 + break; 375 + } 376 + return -EIO; 377 + } 378 + ccg_process_response(uc); 379 + 380 + return uc->cmd_resp; 381 + } 382 + 383 + static int ccg_cmd_enter_flashing(struct ucsi_ccg *uc) 384 + { 385 + struct ccg_cmd cmd; 386 + int ret; 387 + 388 + cmd.reg = CCGX_RAB_ENTER_FLASHING; 389 + cmd.data = FLASH_ENTER_SIG; 390 + cmd.len = 1; 391 + cmd.delay = 50; 392 + 393 + mutex_lock(&uc->lock); 394 + 395 + ret = ccg_send_command(uc, &cmd); 396 + 397 + mutex_unlock(&uc->lock); 398 + 399 + if (ret != CMD_SUCCESS) { 400 + dev_err(uc->dev, "enter flashing failed ret=%d\n", ret); 401 + return ret; 402 + } 403 + 404 + return 0; 405 + } 406 + 407 + static int ccg_cmd_reset(struct ucsi_ccg *uc) 408 + { 409 + struct ccg_cmd cmd; 410 + u8 *p; 411 + int ret; 412 + 413 + p = (u8 *)&cmd.data; 414 + cmd.reg = CCGX_RAB_RESET_REQ; 415 + p[0] = RESET_SIG; 416 + p[1] = CMD_RESET_DEV; 417 + cmd.len = 2; 418 + cmd.delay = 5000; 419 + 420 + mutex_lock(&uc->lock); 421 + 422 + set_bit(RESET_PENDING, &uc->flags); 423 + 424 + ret = ccg_send_command(uc, &cmd); 425 + if (ret != RESET_COMPLETE) 426 + goto err_clear_flag; 427 + 428 + ret = 0; 429 + 430 + err_clear_flag: 431 + clear_bit(RESET_PENDING, &uc->flags); 432 + 433 + mutex_unlock(&uc->lock); 434 + 435 + return ret; 436 + } 437 + 438 + static int ccg_cmd_port_control(struct ucsi_ccg *uc, bool enable) 439 + { 440 + struct ccg_cmd cmd; 441 + int ret; 442 + 443 + cmd.reg = CCGX_RAB_PDPORT_ENABLE; 444 + if (enable) 445 + cmd.data = (uc->port_num == 1) ? 446 + PDPORT_1 : (PDPORT_1 | PDPORT_2); 447 + else 448 + cmd.data = 0x0; 449 + cmd.len = 1; 450 + cmd.delay = 10; 451 + 452 + mutex_lock(&uc->lock); 453 + 454 + ret = ccg_send_command(uc, &cmd); 455 + 456 + mutex_unlock(&uc->lock); 457 + 458 + if (ret != CMD_SUCCESS) { 459 + dev_err(uc->dev, "port control failed ret=%d\n", ret); 460 + return ret; 461 + } 462 + return 0; 463 + } 464 + 465 + static int ccg_cmd_jump_boot_mode(struct ucsi_ccg *uc, int bl_mode) 466 + { 467 + struct ccg_cmd cmd; 468 + int ret; 469 + 470 + cmd.reg = CCGX_RAB_JUMP_TO_BOOT; 471 + 472 + if (bl_mode) 473 + cmd.data = TO_BOOT; 474 + else 475 + cmd.data = TO_ALT_FW; 476 + 477 + cmd.len = 1; 478 + cmd.delay = 100; 479 + 480 + mutex_lock(&uc->lock); 481 + 482 + set_bit(RESET_PENDING, &uc->flags); 483 + 484 + ret = ccg_send_command(uc, &cmd); 485 + if (ret != RESET_COMPLETE) 486 + goto err_clear_flag; 487 + 488 + ret = 0; 489 + 490 + err_clear_flag: 491 + clear_bit(RESET_PENDING, &uc->flags); 492 + 493 + mutex_unlock(&uc->lock); 494 + 495 + return ret; 496 + } 497 + 498 + static int 499 + ccg_cmd_write_flash_row(struct ucsi_ccg *uc, u16 row, 500 + const void *data, u8 fcmd) 501 + { 502 + struct i2c_client *client = uc->client; 503 + struct ccg_cmd cmd; 504 + u8 buf[CCG4_ROW_SIZE + 2]; 505 + u8 *p; 506 + int ret; 507 + 508 + /* Copy the data into the flash read/write memory. */ 509 + put_unaligned_le16(REG_FLASH_RW_MEM, buf); 510 + 511 + memcpy(buf + 2, data, CCG4_ROW_SIZE); 512 + 513 + mutex_lock(&uc->lock); 514 + 515 + ret = i2c_master_send(client, buf, CCG4_ROW_SIZE + 2); 516 + if (ret != CCG4_ROW_SIZE + 2) { 517 + dev_err(uc->dev, "REG_FLASH_RW_MEM write fail %d\n", ret); 518 + return ret < 0 ? ret : -EIO; 519 + } 520 + 521 + /* Use the FLASH_ROW_READ_WRITE register to trigger */ 522 + /* writing of data to the desired flash row */ 523 + p = (u8 *)&cmd.data; 524 + cmd.reg = CCGX_RAB_FLASH_ROW_RW; 525 + p[0] = FLASH_SIG; 526 + p[1] = fcmd; 527 + put_unaligned_le16(row, &p[2]); 528 + cmd.len = 4; 529 + cmd.delay = 50; 530 + if (fcmd == FLASH_FWCT_SIG_WR_CMD) 531 + cmd.delay += 400; 532 + if (row == 510) 533 + cmd.delay += 220; 534 + ret = ccg_send_command(uc, &cmd); 535 + 536 + mutex_unlock(&uc->lock); 537 + 538 + if (ret != CMD_SUCCESS) { 539 + dev_err(uc->dev, "write flash row failed ret=%d\n", ret); 540 + return ret; 541 + } 542 + 543 + return 0; 544 + } 545 + 546 + static int ccg_cmd_validate_fw(struct ucsi_ccg *uc, unsigned int fwid) 547 + { 548 + struct ccg_cmd cmd; 549 + int ret; 550 + 551 + cmd.reg = CCGX_RAB_VALIDATE_FW; 552 + cmd.data = fwid; 553 + cmd.len = 1; 554 + cmd.delay = 500; 555 + 556 + mutex_lock(&uc->lock); 557 + 558 + ret = ccg_send_command(uc, &cmd); 559 + 560 + mutex_unlock(&uc->lock); 561 + 562 + if (ret != CMD_SUCCESS) 563 + return ret; 564 + 565 + return 0; 566 + } 567 + 568 + static bool ccg_check_vendor_version(struct ucsi_ccg *uc, 569 + struct version_format *app, 570 + struct fw_config_table *fw_cfg) 571 + { 572 + struct device *dev = uc->dev; 573 + 574 + /* Check if the fw build is for supported vendors */ 575 + if (le16_to_cpu(app->build) != uc->fw_build) { 576 + dev_info(dev, "current fw is not from supported vendor\n"); 577 + return false; 578 + } 579 + 580 + /* Check if the new fw build is for supported vendors */ 581 + if (le16_to_cpu(fw_cfg->app.build) != uc->fw_build) { 582 + dev_info(dev, "new fw is not from supported vendor\n"); 583 + return false; 584 + } 585 + return true; 586 + } 587 + 588 + static bool ccg_check_fw_version(struct ucsi_ccg *uc, const char *fw_name, 589 + struct version_format *app) 590 + { 591 + const struct firmware *fw = NULL; 592 + struct device *dev = uc->dev; 593 + struct fw_config_table fw_cfg; 594 + u32 cur_version, new_version; 595 + bool is_later = false; 596 + 597 + if (request_firmware(&fw, fw_name, dev) != 0) { 598 + dev_err(dev, "error: Failed to open cyacd file %s\n", fw_name); 599 + return false; 600 + } 601 + 602 + /* 603 + * check if signed fw 604 + * last part of fw image is fw cfg table and signature 605 + */ 606 + if (fw->size < sizeof(fw_cfg) + FW_CFG_TABLE_SIG_SIZE) 607 + goto out_release_firmware; 608 + 609 + memcpy((uint8_t *)&fw_cfg, fw->data + fw->size - 610 + sizeof(fw_cfg) - FW_CFG_TABLE_SIG_SIZE, sizeof(fw_cfg)); 611 + 612 + if (fw_cfg.identity != ('F' | 'W' << 8 | 'C' << 16 | 'T' << 24)) { 613 + dev_info(dev, "not a signed image\n"); 614 + goto out_release_firmware; 615 + } 616 + 617 + /* compare input version with FWCT version */ 618 + cur_version = le16_to_cpu(app->build) | app->patch << 16 | 619 + app->ver << 24; 620 + 621 + new_version = le16_to_cpu(fw_cfg.app.build) | fw_cfg.app.patch << 16 | 622 + fw_cfg.app.ver << 24; 623 + 624 + if (!ccg_check_vendor_version(uc, app, &fw_cfg)) 625 + goto out_release_firmware; 626 + 627 + if (new_version > cur_version) 628 + is_later = true; 629 + 630 + out_release_firmware: 631 + release_firmware(fw); 632 + return is_later; 633 + } 634 + 635 + static int ccg_fw_update_needed(struct ucsi_ccg *uc, 636 + enum enum_flash_mode *mode) 637 + { 638 + struct device *dev = uc->dev; 639 + int err; 640 + struct version_info version[3]; 641 + 642 + err = ccg_read(uc, CCGX_RAB_DEVICE_MODE, (u8 *)(&uc->info), 643 + sizeof(uc->info)); 644 + if (err) { 645 + dev_err(dev, "read device mode failed\n"); 646 + return err; 647 + } 648 + 649 + err = ccg_read(uc, CCGX_RAB_READ_ALL_VER, (u8 *)version, 650 + sizeof(version)); 651 + if (err) { 652 + dev_err(dev, "read device mode failed\n"); 653 + return err; 654 + } 655 + 656 + if (memcmp(&version[FW1], "\0\0\0\0\0\0\0\0", 657 + sizeof(struct version_info)) == 0) { 658 + dev_info(dev, "secondary fw is not flashed\n"); 659 + *mode = SECONDARY_BL; 660 + } else if (le16_to_cpu(version[FW1].base.build) < 661 + secondary_fw_min_ver) { 662 + dev_info(dev, "secondary fw version is too low (< %d)\n", 663 + secondary_fw_min_ver); 664 + *mode = SECONDARY; 665 + } else if (memcmp(&version[FW2], "\0\0\0\0\0\0\0\0", 666 + sizeof(struct version_info)) == 0) { 667 + dev_info(dev, "primary fw is not flashed\n"); 668 + *mode = PRIMARY; 669 + } else if (ccg_check_fw_version(uc, ccg_fw_names[PRIMARY], 670 + &version[FW2].app)) { 671 + dev_info(dev, "found primary fw with later version\n"); 672 + *mode = PRIMARY; 673 + } else { 674 + dev_info(dev, "secondary and primary fw are the latest\n"); 675 + *mode = FLASH_NOT_NEEDED; 676 + } 677 + return 0; 678 + } 679 + 680 + static int do_flash(struct ucsi_ccg *uc, enum enum_flash_mode mode) 681 + { 682 + struct device *dev = uc->dev; 683 + const struct firmware *fw = NULL; 684 + const char *p, *s; 685 + const char *eof; 686 + int err, row, len, line_sz, line_cnt = 0; 687 + unsigned long start_time = jiffies; 688 + struct fw_config_table fw_cfg; 689 + u8 fw_cfg_sig[FW_CFG_TABLE_SIG_SIZE]; 690 + u8 *wr_buf; 691 + 692 + err = request_firmware(&fw, ccg_fw_names[mode], dev); 693 + if (err) { 694 + dev_err(dev, "request %s failed err=%d\n", 695 + ccg_fw_names[mode], err); 696 + return err; 697 + } 698 + 699 + if (((uc->info.mode & CCG_DEVINFO_FWMODE_MASK) >> 700 + CCG_DEVINFO_FWMODE_SHIFT) == FW2) { 701 + err = ccg_cmd_port_control(uc, false); 702 + if (err < 0) 703 + goto release_fw; 704 + err = ccg_cmd_jump_boot_mode(uc, 0); 705 + if (err < 0) 706 + goto release_fw; 707 + } 708 + 709 + eof = fw->data + fw->size; 710 + 711 + /* 712 + * check if signed fw 713 + * last part of fw image is fw cfg table and signature 714 + */ 715 + if (fw->size < sizeof(fw_cfg) + sizeof(fw_cfg_sig)) 716 + goto not_signed_fw; 717 + 718 + memcpy((uint8_t *)&fw_cfg, fw->data + fw->size - 719 + sizeof(fw_cfg) - sizeof(fw_cfg_sig), sizeof(fw_cfg)); 720 + 721 + if (fw_cfg.identity != ('F' | ('W' << 8) | ('C' << 16) | ('T' << 24))) { 722 + dev_info(dev, "not a signed image\n"); 723 + goto not_signed_fw; 724 + } 725 + eof = fw->data + fw->size - sizeof(fw_cfg) - sizeof(fw_cfg_sig); 726 + 727 + memcpy((uint8_t *)&fw_cfg_sig, 728 + fw->data + fw->size - sizeof(fw_cfg_sig), sizeof(fw_cfg_sig)); 729 + 730 + /* flash fw config table and signature first */ 731 + err = ccg_cmd_write_flash_row(uc, 0, (u8 *)&fw_cfg, 732 + FLASH_FWCT1_WR_CMD); 733 + if (err) 734 + goto release_fw; 735 + 736 + err = ccg_cmd_write_flash_row(uc, 0, (u8 *)&fw_cfg + CCG4_ROW_SIZE, 737 + FLASH_FWCT2_WR_CMD); 738 + if (err) 739 + goto release_fw; 740 + 741 + err = ccg_cmd_write_flash_row(uc, 0, &fw_cfg_sig, 742 + FLASH_FWCT_SIG_WR_CMD); 743 + if (err) 744 + goto release_fw; 745 + 746 + not_signed_fw: 747 + wr_buf = kzalloc(CCG4_ROW_SIZE + 4, GFP_KERNEL); 748 + if (!wr_buf) 749 + return -ENOMEM; 750 + 751 + err = ccg_cmd_enter_flashing(uc); 752 + if (err) 753 + goto release_mem; 754 + 755 + /***************************************************************** 756 + * CCG firmware image (.cyacd) file line format 757 + * 758 + * :00rrrrllll[dd....]cc/r/n 759 + * 760 + * :00 header 761 + * rrrr is row number to flash (4 char) 762 + * llll is data len to flash (4 char) 763 + * dd is a data field represents one byte of data (512 char) 764 + * cc is checksum (2 char) 765 + * \r\n newline 766 + * 767 + * Total length: 3 + 4 + 4 + 512 + 2 + 2 = 527 768 + * 769 + *****************************************************************/ 770 + 771 + p = strnchr(fw->data, fw->size, ':'); 772 + while (p < eof) { 773 + s = strnchr(p + 1, eof - p - 1, ':'); 774 + 775 + if (!s) 776 + s = eof; 777 + 778 + line_sz = s - p; 779 + 780 + if (line_sz != CYACD_LINE_SIZE) { 781 + dev_err(dev, "Bad FW format line_sz=%d\n", line_sz); 782 + err = -EINVAL; 783 + goto release_mem; 784 + } 785 + 786 + if (hex2bin(wr_buf, p + 3, CCG4_ROW_SIZE + 4)) { 787 + err = -EINVAL; 788 + goto release_mem; 789 + } 790 + 791 + row = get_unaligned_be16(wr_buf); 792 + len = get_unaligned_be16(&wr_buf[2]); 793 + 794 + if (len != CCG4_ROW_SIZE) { 795 + err = -EINVAL; 796 + goto release_mem; 797 + } 798 + 799 + err = ccg_cmd_write_flash_row(uc, row, wr_buf + 4, 800 + FLASH_WR_CMD); 801 + if (err) 802 + goto release_mem; 803 + 804 + line_cnt++; 805 + p = s; 806 + } 807 + 808 + dev_info(dev, "total %d row flashed. time: %dms\n", 809 + line_cnt, jiffies_to_msecs(jiffies - start_time)); 810 + 811 + err = ccg_cmd_validate_fw(uc, (mode == PRIMARY) ? FW2 : FW1); 812 + if (err) 813 + dev_err(dev, "%s validation failed err=%d\n", 814 + (mode == PRIMARY) ? "FW2" : "FW1", err); 815 + else 816 + dev_info(dev, "%s validated\n", 817 + (mode == PRIMARY) ? "FW2" : "FW1"); 818 + 819 + err = ccg_cmd_port_control(uc, false); 820 + if (err < 0) 821 + goto release_mem; 822 + 823 + err = ccg_cmd_reset(uc); 824 + if (err < 0) 825 + goto release_mem; 826 + 827 + err = ccg_cmd_port_control(uc, true); 828 + if (err < 0) 829 + goto release_mem; 830 + 831 + release_mem: 832 + kfree(wr_buf); 833 + 834 + release_fw: 835 + release_firmware(fw); 836 + return err; 837 + } 838 + 839 + /******************************************************************************* 840 + * CCG4 has two copies of the firmware in addition to the bootloader. 841 + * If the device is running FW1, FW2 can be updated with the new version. 842 + * Dual firmware mode allows the CCG device to stay in a PD contract and support 843 + * USB PD and Type-C functionality while a firmware update is in progress. 844 + ******************************************************************************/ 845 + static int ccg_fw_update(struct ucsi_ccg *uc, enum enum_flash_mode flash_mode) 846 + { 847 + int err; 848 + 849 + while (flash_mode != FLASH_NOT_NEEDED) { 850 + err = do_flash(uc, flash_mode); 851 + if (err < 0) 852 + return err; 853 + err = ccg_fw_update_needed(uc, &flash_mode); 854 + if (err < 0) 855 + return err; 856 + } 857 + dev_info(uc->dev, "CCG FW update successful\n"); 858 + 859 + return err; 860 + } 861 + 862 + static int ccg_restart(struct ucsi_ccg *uc) 863 + { 864 + struct device *dev = uc->dev; 865 + int status; 866 + 867 + status = ucsi_ccg_init(uc); 868 + if (status < 0) { 869 + dev_err(dev, "ucsi_ccg_start fail, err=%d\n", status); 870 + return status; 871 + } 872 + 873 + status = request_threaded_irq(uc->irq, NULL, ccg_irq_handler, 874 + IRQF_ONESHOT | IRQF_TRIGGER_HIGH, 875 + dev_name(dev), uc); 876 + if (status < 0) { 877 + dev_err(dev, "request_threaded_irq failed - %d\n", status); 878 + return status; 879 + } 880 + 881 + uc->ucsi = ucsi_register_ppm(dev, &uc->ppm); 882 + if (IS_ERR(uc->ucsi)) { 883 + dev_err(uc->dev, "ucsi_register_ppm failed\n"); 884 + return PTR_ERR(uc->ucsi); 885 + } 886 + 887 + return 0; 888 + } 889 + 890 + static void ccg_update_firmware(struct work_struct *work) 891 + { 892 + struct ucsi_ccg *uc = container_of(work, struct ucsi_ccg, work); 893 + enum enum_flash_mode flash_mode; 894 + int status; 895 + 896 + status = ccg_fw_update_needed(uc, &flash_mode); 897 + if (status < 0) 898 + return; 899 + 900 + if (flash_mode != FLASH_NOT_NEEDED) { 901 + ucsi_unregister_ppm(uc->ucsi); 902 + free_irq(uc->irq, uc); 903 + 904 + ccg_fw_update(uc, flash_mode); 905 + ccg_restart(uc); 906 + } 907 + } 908 + 909 + static ssize_t do_flash_store(struct device *dev, 910 + struct device_attribute *attr, 911 + const char *buf, size_t n) 912 + { 913 + struct ucsi_ccg *uc = i2c_get_clientdata(to_i2c_client(dev)); 914 + bool flash; 915 + 916 + if (kstrtobool(buf, &flash)) 917 + return -EINVAL; 918 + 919 + if (!flash) 920 + return n; 921 + 922 + if (uc->fw_build == 0x0) { 923 + dev_err(dev, "fail to flash FW due to missing FW build info\n"); 924 + return -EINVAL; 925 + } 926 + 927 + schedule_work(&uc->work); 928 + return n; 929 + } 930 + 931 + static DEVICE_ATTR_WO(do_flash); 932 + 933 + static struct attribute *ucsi_ccg_sysfs_attrs[] = { 934 + &dev_attr_do_flash.attr, 935 + NULL, 936 + }; 937 + 938 + static struct attribute_group ucsi_ccg_attr_group = { 939 + .attrs = ucsi_ccg_sysfs_attrs, 940 + }; 941 + 395 942 static int ucsi_ccg_probe(struct i2c_client *client, 396 943 const struct i2c_device_id *id) 397 944 { ··· 1075 296 uc->ppm.sync = ucsi_ccg_sync; 1076 297 uc->dev = dev; 1077 298 uc->client = client; 299 + mutex_init(&uc->lock); 300 + INIT_WORK(&uc->work, ccg_update_firmware); 301 + 302 + /* Only fail FW flashing when FW build information is not provided */ 303 + status = device_property_read_u16(dev, "ccgx,firmware-build", 304 + &uc->fw_build); 305 + if (status) 306 + dev_err(uc->dev, "failed to get FW build information\n"); 1078 307 1079 308 /* reset ccg device and initialize ucsi */ 1080 309 status = ucsi_ccg_init(uc); ··· 1097 310 return status; 1098 311 } 1099 312 1100 - status = devm_request_threaded_irq(dev, client->irq, NULL, 1101 - ccg_irq_handler, 1102 - IRQF_ONESHOT | IRQF_TRIGGER_HIGH, 1103 - dev_name(dev), uc); 313 + uc->port_num = 1; 314 + 315 + if (uc->info.mode & CCG_DEVINFO_PDPORTS_MASK) 316 + uc->port_num++; 317 + 318 + status = request_threaded_irq(client->irq, NULL, ccg_irq_handler, 319 + IRQF_ONESHOT | IRQF_TRIGGER_HIGH, 320 + dev_name(dev), uc); 1104 321 if (status < 0) { 1105 322 dev_err(uc->dev, "request_threaded_irq failed - %d\n", status); 1106 323 return status; 1107 324 } 325 + 326 + uc->irq = client->irq; 1108 327 1109 328 uc->ucsi = ucsi_register_ppm(dev, &uc->ppm); 1110 329 if (IS_ERR(uc->ucsi)) { ··· 1128 335 } 1129 336 1130 337 i2c_set_clientdata(client, uc); 338 + 339 + status = sysfs_create_group(&uc->dev->kobj, &ucsi_ccg_attr_group); 340 + if (status) 341 + dev_err(uc->dev, "cannot create sysfs group: %d\n", status); 342 + 1131 343 return 0; 1132 344 } 1133 345 ··· 1140 342 { 1141 343 struct ucsi_ccg *uc = i2c_get_clientdata(client); 1142 344 345 + cancel_work_sync(&uc->work); 1143 346 ucsi_unregister_ppm(uc->ucsi); 347 + free_irq(uc->irq, uc); 348 + sysfs_remove_group(&uc->dev->kobj, &ucsi_ccg_attr_group); 1144 349 1145 350 return 0; 1146 351 }