1 // SPDX-License-Identifier: GPL-2.0
3 * UCSI driver for Cypress CCGx Type-C controller
5 * Copyright (C) 2017-2018 NVIDIA Corporation. All rights reserved.
6 * Author: Ajay Gupta <ajayg@nvidia.com>
8 * Some code borrowed from drivers/usb/typec/ucsi/ucsi_acpi.c
10 #include <linux/acpi.h>
11 #include <linux/delay.h>
12 #include <linux/firmware.h>
13 #include <linux/i2c.h>
14 #include <linux/module.h>
15 #include <linux/pci.h>
16 #include <linux/platform_device.h>
18 #include <linux/pm_runtime.h>
19 #include <linux/usb/typec_dp.h>
21 #include <asm/unaligned.h>
25 BOOT, /* bootloader */
26 FW1, /* FW partition-1 (contains secondary fw) */
27 FW2, /* FW partition-2 (contains primary fw) */
31 #define CCGX_RAB_DEVICE_MODE 0x0000
32 #define CCGX_RAB_INTR_REG 0x0006
33 #define DEV_INT BIT(0)
34 #define PORT0_INT BIT(1)
35 #define PORT1_INT BIT(2)
36 #define UCSI_READ_INT BIT(7)
37 #define CCGX_RAB_JUMP_TO_BOOT 0x0007
40 #define CCGX_RAB_RESET_REQ 0x0008
42 #define CMD_RESET_I2C 0x0
43 #define CMD_RESET_DEV 0x1
44 #define CCGX_RAB_ENTER_FLASHING 0x000A
45 #define FLASH_ENTER_SIG 'P'
46 #define CCGX_RAB_VALIDATE_FW 0x000B
47 #define CCGX_RAB_FLASH_ROW_RW 0x000C
49 #define FLASH_RD_CMD 0x0
50 #define FLASH_WR_CMD 0x1
51 #define FLASH_FWCT1_WR_CMD 0x2
52 #define FLASH_FWCT2_WR_CMD 0x3
53 #define FLASH_FWCT_SIG_WR_CMD 0x4
54 #define CCGX_RAB_READ_ALL_VER 0x0010
55 #define CCGX_RAB_READ_FW2_VER 0x0020
56 #define CCGX_RAB_UCSI_CONTROL 0x0039
57 #define CCGX_RAB_UCSI_CONTROL_START BIT(0)
58 #define CCGX_RAB_UCSI_CONTROL_STOP BIT(1)
59 #define CCGX_RAB_UCSI_DATA_BLOCK(offset) (0xf000 | ((offset) & 0xff))
60 #define REG_FLASH_RW_MEM 0x0200
61 #define DEV_REG_IDX CCGX_RAB_DEVICE_MODE
62 #define CCGX_RAB_PDPORT_ENABLE 0x002C
63 #define PDPORT_1 BIT(0)
64 #define PDPORT_2 BIT(1)
65 #define CCGX_RAB_RESPONSE 0x007E
66 #define ASYNC_EVENT BIT(7)
68 /* CCGx events & async msg codes */
69 #define RESET_COMPLETE 0x80
70 #define EVENT_INDEX RESET_COMPLETE
71 #define PORT_CONNECT_DET 0x84
72 #define PORT_DISCONNECT_DET 0x85
73 #define ROLE_SWAP_COMPELETE 0x87
76 #define CYACD_LINE_SIZE 527
77 #define CCG4_ROW_SIZE 256
78 #define FW1_METADATA_ROW 0x1FF
79 #define FW2_METADATA_ROW 0x1FE
80 #define FW_CFG_TABLE_SIG_SIZE 256
82 static int secondary_fw_min_ver = 41;
84 enum enum_flash_mode {
85 SECONDARY_BL, /* update secondary using bootloader */
86 PRIMARY, /* update primary using secondary */
87 SECONDARY, /* update secondary using primary */
88 FLASH_NOT_NEEDED, /* update not required */
92 static const char * const ccg_fw_names[] = {
99 #define CCG_DEVINFO_FWMODE_SHIFT (0)
100 #define CCG_DEVINFO_FWMODE_MASK (0x3 << CCG_DEVINFO_FWMODE_SHIFT)
101 #define CCG_DEVINFO_PDPORTS_SHIFT (2)
102 #define CCG_DEVINFO_PDPORTS_MASK (0x3 << CCG_DEVINFO_PDPORTS_SHIFT)
109 struct version_format {
113 #define CCG_VERSION_PATCH(x) ((x) << 16)
114 #define CCG_VERSION(x) ((x) << 24)
115 #define CCG_VERSION_MIN_SHIFT (0)
116 #define CCG_VERSION_MIN_MASK (0xf << CCG_VERSION_MIN_SHIFT)
117 #define CCG_VERSION_MAJ_SHIFT (4)
118 #define CCG_VERSION_MAJ_MASK (0xf << CCG_VERSION_MAJ_SHIFT)
122 * Firmware version 3.1.10 or earlier, built for NVIDIA has known issue
123 * of missing interrupt when a device is connected for runtime resume
125 #define CCG_FW_BUILD_NVIDIA (('n' << 8) | 'v')
126 #define CCG_OLD_FW_VERSION (CCG_VERSION(0x31) | CCG_VERSION_PATCH(10))
128 struct version_info {
129 struct version_format base;
130 struct version_format app;
133 struct fw_config_table {
139 struct version_format base;
140 struct version_format app;
141 u8 primary_fw_digest[32];
147 /* CCGx response codes */
151 FLASH_DATA_AVAILABLE = 0x03,
153 FLASH_UPDATE_FAIL = 0x07,
156 CMD_NOT_SUPPORT = 0x0A,
157 TRANSACTION_FAIL = 0x0C,
163 #define CCG_EVENT_MAX (EVENT_INDEX + 43)
169 u32 delay; /* ms delay for cmd timeout */
177 struct ucsi_ccg_altmode {
182 #define UCSI_MULTI_DP_INDEX (0xff)
189 struct i2c_client *client;
191 struct ccg_dev_info info;
192 /* version info for boot, primary and secondary */
193 struct version_info version[FW2 + 1];
195 /* CCG HPI communication flags */
197 #define RESET_PENDING 0
198 #define DEV_CMD_PENDING 1
199 struct ccg_resp dev_resp;
203 struct work_struct work;
204 struct mutex lock; /* to sync between user and driver thread */
206 /* fw build with vendor information */
208 struct work_struct pm_work;
210 struct completion complete;
213 bool has_multiple_dp;
214 struct ucsi_ccg_altmode orig[UCSI_MAX_ALTMODES];
215 struct ucsi_ccg_altmode updated[UCSI_MAX_ALTMODES];
218 static int ccg_read(struct ucsi_ccg *uc, u16 rab, u8 *data, u32 len)
220 struct i2c_client *client = uc->client;
221 const struct i2c_adapter_quirks *quirks = client->adapter->quirks;
222 unsigned char buf[2];
223 struct i2c_msg msgs[] = {
225 .addr = client->addr,
231 .addr = client->addr,
236 u32 rlen, rem_len = len, max_read_len = len;
239 /* check any max_read_len limitation on i2c adapter */
240 if (quirks && quirks->max_read_len)
241 max_read_len = quirks->max_read_len;
243 pm_runtime_get_sync(uc->dev);
244 while (rem_len > 0) {
245 msgs[1].buf = &data[len - rem_len];
246 rlen = min_t(u16, rem_len, max_read_len);
248 put_unaligned_le16(rab, buf);
249 status = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
251 dev_err(uc->dev, "i2c_transfer failed %d\n", status);
252 pm_runtime_put_sync(uc->dev);
259 pm_runtime_put_sync(uc->dev);
263 static int ccg_write(struct ucsi_ccg *uc, u16 rab, const u8 *data, u32 len)
265 struct i2c_client *client = uc->client;
267 struct i2c_msg msgs[] = {
269 .addr = client->addr,
275 buf = kzalloc(len + sizeof(rab), GFP_KERNEL);
279 put_unaligned_le16(rab, buf);
280 memcpy(buf + sizeof(rab), data, len);
282 msgs[0].len = len + sizeof(rab);
285 pm_runtime_get_sync(uc->dev);
286 status = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
288 dev_err(uc->dev, "i2c_transfer failed %d\n", status);
289 pm_runtime_put_sync(uc->dev);
294 pm_runtime_put_sync(uc->dev);
299 static int ucsi_ccg_init(struct ucsi_ccg *uc)
301 unsigned int count = 10;
305 data = CCGX_RAB_UCSI_CONTROL_STOP;
306 status = ccg_write(uc, CCGX_RAB_UCSI_CONTROL, &data, sizeof(data));
310 data = CCGX_RAB_UCSI_CONTROL_START;
311 status = ccg_write(uc, CCGX_RAB_UCSI_CONTROL, &data, sizeof(data));
316 * Flush CCGx RESPONSE queue by acking interrupts. Above ucsi control
317 * register write will push response which must be cleared.
320 status = ccg_read(uc, CCGX_RAB_INTR_REG, &data, sizeof(data));
327 status = ccg_write(uc, CCGX_RAB_INTR_REG, &data, sizeof(data));
331 usleep_range(10000, 11000);
337 static void ucsi_ccg_update_get_current_cam_cmd(struct ucsi_ccg *uc, u8 *data)
342 new_cam = uc->orig[cam].linked_idx;
343 uc->updated[new_cam].active_idx = cam;
347 static bool ucsi_ccg_update_altmodes(struct ucsi *ucsi,
348 struct ucsi_altmode *orig,
349 struct ucsi_altmode *updated)
351 struct ucsi_ccg *uc = ucsi_get_drvdata(ucsi);
352 struct ucsi_ccg_altmode *alt, *new_alt;
357 new_alt = uc->updated;
358 memset(uc->updated, 0, sizeof(uc->updated));
361 * Copy original connector altmodes to new structure.
362 * We need this before second loop since second loop
363 * checks for duplicate altmodes.
365 for (i = 0; i < UCSI_MAX_ALTMODES; i++) {
366 alt[i].svid = orig[i].svid;
367 alt[i].mid = orig[i].mid;
372 for (i = 0; i < UCSI_MAX_ALTMODES; i++) {
376 /* already checked and considered */
380 if (!DP_CONF_GET_PIN_ASSIGN(alt[i].mid)) {
381 /* Found Non DP altmode */
382 new_alt[k].svid = alt[i].svid;
383 new_alt[k].mid |= alt[i].mid;
384 new_alt[k].linked_idx = i;
385 alt[i].linked_idx = k;
386 updated[k].svid = new_alt[k].svid;
387 updated[k].mid = new_alt[k].mid;
392 for (j = i + 1; j < UCSI_MAX_ALTMODES; j++) {
393 if (alt[i].svid != alt[j].svid ||
394 !DP_CONF_GET_PIN_ASSIGN(alt[j].mid)) {
397 /* Found duplicate DP mode */
398 new_alt[k].svid = alt[i].svid;
399 new_alt[k].mid |= alt[i].mid | alt[j].mid;
400 new_alt[k].linked_idx = UCSI_MULTI_DP_INDEX;
401 alt[i].linked_idx = k;
402 alt[j].linked_idx = k;
403 alt[j].checked = true;
408 uc->has_multiple_dp = true;
410 /* Didn't find any duplicate DP altmode */
411 new_alt[k].svid = alt[i].svid;
412 new_alt[k].mid |= alt[i].mid;
413 new_alt[k].linked_idx = i;
414 alt[i].linked_idx = k;
416 updated[k].svid = new_alt[k].svid;
417 updated[k].mid = new_alt[k].mid;
423 static void ucsi_ccg_update_set_new_cam_cmd(struct ucsi_ccg *uc,
424 struct ucsi_connector *con,
427 struct ucsi_ccg_altmode *new_port, *port;
428 struct typec_altmode *alt = NULL;
429 u8 new_cam, cam, pin;
434 new_cam = UCSI_SET_NEW_CAM_GET_AM(*cmd);
435 new_port = &uc->updated[new_cam];
436 cam = new_port->linked_idx;
437 enter_new_mode = UCSI_SET_NEW_CAM_ENTER(*cmd);
440 * If CAM is UCSI_MULTI_DP_INDEX then this is DP altmode
441 * with multiple DP mode. Find out CAM for best pin assignment
442 * among all DP mode. Priorite pin E->D->C after making sure
443 * the partner supports that pin.
445 if (cam == UCSI_MULTI_DP_INDEX) {
446 if (enter_new_mode) {
447 for (i = 0; con->partner_altmode[i]; i++) {
448 alt = con->partner_altmode[i];
449 if (alt->svid == new_port->svid)
453 * alt will always be non NULL since this is
454 * UCSI_SET_NEW_CAM command and so there will be
455 * at least one con->partner_altmode[i] with svid
456 * matching with new_port->svid.
458 for (j = 0; port[j].svid; j++) {
459 pin = DP_CONF_GET_PIN_ASSIGN(port[j].mid);
460 if (alt && port[j].svid == alt->svid &&
461 (pin & DP_CONF_GET_PIN_ASSIGN(alt->vdo))) {
462 /* prioritize pin E->D->C */
463 if (k == 0xff || (k != 0xff && pin >
464 DP_CONF_GET_PIN_ASSIGN(port[k].mid))
471 new_port->active_idx = cam;
473 cam = new_port->active_idx;
476 *cmd &= ~UCSI_SET_NEW_CAM_AM_MASK;
477 *cmd |= UCSI_SET_NEW_CAM_SET_AM(cam);
480 static int ucsi_ccg_read(struct ucsi *ucsi, unsigned int offset,
481 void *val, size_t val_len)
483 struct ucsi_ccg *uc = ucsi_get_drvdata(ucsi);
485 u16 reg = CCGX_RAB_UCSI_DATA_BLOCK(offset);
487 ret = ccg_read(uc, reg, val, val_len);
491 if (offset == UCSI_MESSAGE_IN) {
492 if (UCSI_COMMAND(uc->last_cmd_sent) == UCSI_GET_CURRENT_CAM &&
493 uc->has_multiple_dp) {
494 ucsi_ccg_update_get_current_cam_cmd(uc, (u8 *)val);
496 uc->last_cmd_sent = 0;
502 static int ucsi_ccg_async_write(struct ucsi *ucsi, unsigned int offset,
503 const void *val, size_t val_len)
505 u16 reg = CCGX_RAB_UCSI_DATA_BLOCK(offset);
507 return ccg_write(ucsi_get_drvdata(ucsi), reg, val, val_len);
510 static int ucsi_ccg_sync_write(struct ucsi *ucsi, unsigned int offset,
511 const void *val, size_t val_len)
513 struct ucsi_ccg *uc = ucsi_get_drvdata(ucsi);
514 struct ucsi_connector *con;
518 mutex_lock(&uc->lock);
519 pm_runtime_get_sync(uc->dev);
520 set_bit(DEV_CMD_PENDING, &uc->flags);
522 if (offset == UCSI_CONTROL && val_len == sizeof(uc->last_cmd_sent)) {
523 uc->last_cmd_sent = *(u64 *)val;
525 if (UCSI_COMMAND(uc->last_cmd_sent) == UCSI_SET_NEW_CAM &&
526 uc->has_multiple_dp) {
527 con_index = (uc->last_cmd_sent >> 16) &
528 UCSI_CMD_CONNECTOR_MASK;
529 con = &uc->ucsi->connector[con_index - 1];
530 ucsi_ccg_update_set_new_cam_cmd(uc, con, (u64 *)val);
534 ret = ucsi_ccg_async_write(ucsi, offset, val, val_len);
538 if (!wait_for_completion_timeout(&uc->complete, msecs_to_jiffies(5000)))
542 clear_bit(DEV_CMD_PENDING, &uc->flags);
543 pm_runtime_put_sync(uc->dev);
544 mutex_unlock(&uc->lock);
549 static const struct ucsi_operations ucsi_ccg_ops = {
550 .read = ucsi_ccg_read,
551 .sync_write = ucsi_ccg_sync_write,
552 .async_write = ucsi_ccg_async_write,
553 .update_altmodes = ucsi_ccg_update_altmodes
556 static irqreturn_t ccg_irq_handler(int irq, void *data)
558 u16 reg = CCGX_RAB_UCSI_DATA_BLOCK(UCSI_CCI);
559 struct ucsi_ccg *uc = data;
564 ret = ccg_read(uc, CCGX_RAB_INTR_REG, &intr_reg, sizeof(intr_reg));
568 ret = ccg_read(uc, reg, (void *)&cci, sizeof(cci));
572 if (UCSI_CCI_CONNECTOR(cci))
573 ucsi_connector_change(uc->ucsi, UCSI_CCI_CONNECTOR(cci));
575 if (test_bit(DEV_CMD_PENDING, &uc->flags) &&
576 cci & (UCSI_CCI_ACK_COMPLETE | UCSI_CCI_COMMAND_COMPLETE))
577 complete(&uc->complete);
580 ccg_write(uc, CCGX_RAB_INTR_REG, &intr_reg, sizeof(intr_reg));
585 static void ccg_pm_workaround_work(struct work_struct *pm_work)
587 ccg_irq_handler(0, container_of(pm_work, struct ucsi_ccg, pm_work));
590 static int get_fw_info(struct ucsi_ccg *uc)
594 err = ccg_read(uc, CCGX_RAB_READ_ALL_VER, (u8 *)(&uc->version),
595 sizeof(uc->version));
599 uc->fw_version = CCG_VERSION(uc->version[FW2].app.ver) |
600 CCG_VERSION_PATCH(uc->version[FW2].app.patch);
602 err = ccg_read(uc, CCGX_RAB_DEVICE_MODE, (u8 *)(&uc->info),
610 static inline bool invalid_async_evt(int code)
612 return (code >= CCG_EVENT_MAX) || (code < EVENT_INDEX);
615 static void ccg_process_response(struct ucsi_ccg *uc)
617 struct device *dev = uc->dev;
619 if (uc->dev_resp.code & ASYNC_EVENT) {
620 if (uc->dev_resp.code == RESET_COMPLETE) {
621 if (test_bit(RESET_PENDING, &uc->flags))
622 uc->cmd_resp = uc->dev_resp.code;
625 if (invalid_async_evt(uc->dev_resp.code))
626 dev_err(dev, "invalid async evt %d\n",
629 if (test_bit(DEV_CMD_PENDING, &uc->flags)) {
630 uc->cmd_resp = uc->dev_resp.code;
631 clear_bit(DEV_CMD_PENDING, &uc->flags);
633 dev_err(dev, "dev resp 0x%04x but no cmd pending\n",
639 static int ccg_read_response(struct ucsi_ccg *uc)
641 unsigned long target = jiffies + msecs_to_jiffies(1000);
642 struct device *dev = uc->dev;
646 /* wait for interrupt status to get updated */
648 status = ccg_read(uc, CCGX_RAB_INTR_REG, &intval,
653 if (intval & DEV_INT)
655 usleep_range(500, 600);
656 } while (time_is_after_jiffies(target));
658 if (time_is_before_jiffies(target)) {
659 dev_err(dev, "response timeout error\n");
663 status = ccg_read(uc, CCGX_RAB_RESPONSE, (u8 *)&uc->dev_resp,
664 sizeof(uc->dev_resp));
668 status = ccg_write(uc, CCGX_RAB_INTR_REG, &intval, sizeof(intval));
675 /* Caller must hold uc->lock */
676 static int ccg_send_command(struct ucsi_ccg *uc, struct ccg_cmd *cmd)
678 struct device *dev = uc->dev;
681 switch (cmd->reg & 0xF000) {
683 set_bit(DEV_CMD_PENDING, &uc->flags);
686 dev_err(dev, "invalid cmd register\n");
690 ret = ccg_write(uc, cmd->reg, (u8 *)&cmd->data, cmd->len);
696 ret = ccg_read_response(uc);
698 dev_err(dev, "response read error\n");
699 switch (cmd->reg & 0xF000) {
701 clear_bit(DEV_CMD_PENDING, &uc->flags);
704 dev_err(dev, "invalid cmd register\n");
709 ccg_process_response(uc);
714 static int ccg_cmd_enter_flashing(struct ucsi_ccg *uc)
719 cmd.reg = CCGX_RAB_ENTER_FLASHING;
720 cmd.data = FLASH_ENTER_SIG;
724 mutex_lock(&uc->lock);
726 ret = ccg_send_command(uc, &cmd);
728 mutex_unlock(&uc->lock);
730 if (ret != CMD_SUCCESS) {
731 dev_err(uc->dev, "enter flashing failed ret=%d\n", ret);
738 static int ccg_cmd_reset(struct ucsi_ccg *uc)
745 cmd.reg = CCGX_RAB_RESET_REQ;
747 p[1] = CMD_RESET_DEV;
751 mutex_lock(&uc->lock);
753 set_bit(RESET_PENDING, &uc->flags);
755 ret = ccg_send_command(uc, &cmd);
756 if (ret != RESET_COMPLETE)
762 clear_bit(RESET_PENDING, &uc->flags);
764 mutex_unlock(&uc->lock);
769 static int ccg_cmd_port_control(struct ucsi_ccg *uc, bool enable)
774 cmd.reg = CCGX_RAB_PDPORT_ENABLE;
776 cmd.data = (uc->port_num == 1) ?
777 PDPORT_1 : (PDPORT_1 | PDPORT_2);
783 mutex_lock(&uc->lock);
785 ret = ccg_send_command(uc, &cmd);
787 mutex_unlock(&uc->lock);
789 if (ret != CMD_SUCCESS) {
790 dev_err(uc->dev, "port control failed ret=%d\n", ret);
796 static int ccg_cmd_jump_boot_mode(struct ucsi_ccg *uc, int bl_mode)
801 cmd.reg = CCGX_RAB_JUMP_TO_BOOT;
806 cmd.data = TO_ALT_FW;
811 mutex_lock(&uc->lock);
813 set_bit(RESET_PENDING, &uc->flags);
815 ret = ccg_send_command(uc, &cmd);
816 if (ret != RESET_COMPLETE)
822 clear_bit(RESET_PENDING, &uc->flags);
824 mutex_unlock(&uc->lock);
830 ccg_cmd_write_flash_row(struct ucsi_ccg *uc, u16 row,
831 const void *data, u8 fcmd)
833 struct i2c_client *client = uc->client;
835 u8 buf[CCG4_ROW_SIZE + 2];
839 /* Copy the data into the flash read/write memory. */
840 put_unaligned_le16(REG_FLASH_RW_MEM, buf);
842 memcpy(buf + 2, data, CCG4_ROW_SIZE);
844 mutex_lock(&uc->lock);
846 ret = i2c_master_send(client, buf, CCG4_ROW_SIZE + 2);
847 if (ret != CCG4_ROW_SIZE + 2) {
848 dev_err(uc->dev, "REG_FLASH_RW_MEM write fail %d\n", ret);
849 mutex_unlock(&uc->lock);
850 return ret < 0 ? ret : -EIO;
853 /* Use the FLASH_ROW_READ_WRITE register to trigger */
854 /* writing of data to the desired flash row */
856 cmd.reg = CCGX_RAB_FLASH_ROW_RW;
859 put_unaligned_le16(row, &p[2]);
862 if (fcmd == FLASH_FWCT_SIG_WR_CMD)
866 ret = ccg_send_command(uc, &cmd);
868 mutex_unlock(&uc->lock);
870 if (ret != CMD_SUCCESS) {
871 dev_err(uc->dev, "write flash row failed ret=%d\n", ret);
878 static int ccg_cmd_validate_fw(struct ucsi_ccg *uc, unsigned int fwid)
883 cmd.reg = CCGX_RAB_VALIDATE_FW;
888 mutex_lock(&uc->lock);
890 ret = ccg_send_command(uc, &cmd);
892 mutex_unlock(&uc->lock);
894 if (ret != CMD_SUCCESS)
900 static bool ccg_check_vendor_version(struct ucsi_ccg *uc,
901 struct version_format *app,
902 struct fw_config_table *fw_cfg)
904 struct device *dev = uc->dev;
906 /* Check if the fw build is for supported vendors */
907 if (le16_to_cpu(app->build) != uc->fw_build) {
908 dev_info(dev, "current fw is not from supported vendor\n");
912 /* Check if the new fw build is for supported vendors */
913 if (le16_to_cpu(fw_cfg->app.build) != uc->fw_build) {
914 dev_info(dev, "new fw is not from supported vendor\n");
920 static bool ccg_check_fw_version(struct ucsi_ccg *uc, const char *fw_name,
921 struct version_format *app)
923 const struct firmware *fw = NULL;
924 struct device *dev = uc->dev;
925 struct fw_config_table fw_cfg;
926 u32 cur_version, new_version;
927 bool is_later = false;
929 if (request_firmware(&fw, fw_name, dev) != 0) {
930 dev_err(dev, "error: Failed to open cyacd file %s\n", fw_name);
936 * last part of fw image is fw cfg table and signature
938 if (fw->size < sizeof(fw_cfg) + FW_CFG_TABLE_SIG_SIZE)
939 goto out_release_firmware;
941 memcpy((uint8_t *)&fw_cfg, fw->data + fw->size -
942 sizeof(fw_cfg) - FW_CFG_TABLE_SIG_SIZE, sizeof(fw_cfg));
944 if (fw_cfg.identity != ('F' | 'W' << 8 | 'C' << 16 | 'T' << 24)) {
945 dev_info(dev, "not a signed image\n");
946 goto out_release_firmware;
949 /* compare input version with FWCT version */
950 cur_version = le16_to_cpu(app->build) | CCG_VERSION_PATCH(app->patch) |
951 CCG_VERSION(app->ver);
953 new_version = le16_to_cpu(fw_cfg.app.build) |
954 CCG_VERSION_PATCH(fw_cfg.app.patch) |
955 CCG_VERSION(fw_cfg.app.ver);
957 if (!ccg_check_vendor_version(uc, app, &fw_cfg))
958 goto out_release_firmware;
960 if (new_version > cur_version)
963 out_release_firmware:
964 release_firmware(fw);
968 static int ccg_fw_update_needed(struct ucsi_ccg *uc,
969 enum enum_flash_mode *mode)
971 struct device *dev = uc->dev;
973 struct version_info version[3];
975 err = ccg_read(uc, CCGX_RAB_DEVICE_MODE, (u8 *)(&uc->info),
978 dev_err(dev, "read device mode failed\n");
982 err = ccg_read(uc, CCGX_RAB_READ_ALL_VER, (u8 *)version,
985 dev_err(dev, "read device mode failed\n");
989 if (memcmp(&version[FW1], "\0\0\0\0\0\0\0\0",
990 sizeof(struct version_info)) == 0) {
991 dev_info(dev, "secondary fw is not flashed\n");
992 *mode = SECONDARY_BL;
993 } else if (le16_to_cpu(version[FW1].base.build) <
994 secondary_fw_min_ver) {
995 dev_info(dev, "secondary fw version is too low (< %d)\n",
996 secondary_fw_min_ver);
998 } else if (memcmp(&version[FW2], "\0\0\0\0\0\0\0\0",
999 sizeof(struct version_info)) == 0) {
1000 dev_info(dev, "primary fw is not flashed\n");
1002 } else if (ccg_check_fw_version(uc, ccg_fw_names[PRIMARY],
1003 &version[FW2].app)) {
1004 dev_info(dev, "found primary fw with later version\n");
1007 dev_info(dev, "secondary and primary fw are the latest\n");
1008 *mode = FLASH_NOT_NEEDED;
1013 static int do_flash(struct ucsi_ccg *uc, enum enum_flash_mode mode)
1015 struct device *dev = uc->dev;
1016 const struct firmware *fw = NULL;
1019 int err, row, len, line_sz, line_cnt = 0;
1020 unsigned long start_time = jiffies;
1021 struct fw_config_table fw_cfg;
1022 u8 fw_cfg_sig[FW_CFG_TABLE_SIG_SIZE];
1025 err = request_firmware(&fw, ccg_fw_names[mode], dev);
1027 dev_err(dev, "request %s failed err=%d\n",
1028 ccg_fw_names[mode], err);
1032 if (((uc->info.mode & CCG_DEVINFO_FWMODE_MASK) >>
1033 CCG_DEVINFO_FWMODE_SHIFT) == FW2) {
1034 err = ccg_cmd_port_control(uc, false);
1037 err = ccg_cmd_jump_boot_mode(uc, 0);
1042 eof = fw->data + fw->size;
1045 * check if signed fw
1046 * last part of fw image is fw cfg table and signature
1048 if (fw->size < sizeof(fw_cfg) + sizeof(fw_cfg_sig))
1051 memcpy((uint8_t *)&fw_cfg, fw->data + fw->size -
1052 sizeof(fw_cfg) - sizeof(fw_cfg_sig), sizeof(fw_cfg));
1054 if (fw_cfg.identity != ('F' | ('W' << 8) | ('C' << 16) | ('T' << 24))) {
1055 dev_info(dev, "not a signed image\n");
1058 eof = fw->data + fw->size - sizeof(fw_cfg) - sizeof(fw_cfg_sig);
1060 memcpy((uint8_t *)&fw_cfg_sig,
1061 fw->data + fw->size - sizeof(fw_cfg_sig), sizeof(fw_cfg_sig));
1063 /* flash fw config table and signature first */
1064 err = ccg_cmd_write_flash_row(uc, 0, (u8 *)&fw_cfg,
1065 FLASH_FWCT1_WR_CMD);
1069 err = ccg_cmd_write_flash_row(uc, 0, (u8 *)&fw_cfg + CCG4_ROW_SIZE,
1070 FLASH_FWCT2_WR_CMD);
1074 err = ccg_cmd_write_flash_row(uc, 0, &fw_cfg_sig,
1075 FLASH_FWCT_SIG_WR_CMD);
1080 wr_buf = kzalloc(CCG4_ROW_SIZE + 4, GFP_KERNEL);
1086 err = ccg_cmd_enter_flashing(uc);
1090 /*****************************************************************
1091 * CCG firmware image (.cyacd) file line format
1093 * :00rrrrllll[dd....]cc/r/n
1096 * rrrr is row number to flash (4 char)
1097 * llll is data len to flash (4 char)
1098 * dd is a data field represents one byte of data (512 char)
1099 * cc is checksum (2 char)
1102 * Total length: 3 + 4 + 4 + 512 + 2 + 2 = 527
1104 *****************************************************************/
1106 p = strnchr(fw->data, fw->size, ':');
1108 s = strnchr(p + 1, eof - p - 1, ':');
1115 if (line_sz != CYACD_LINE_SIZE) {
1116 dev_err(dev, "Bad FW format line_sz=%d\n", line_sz);
1121 if (hex2bin(wr_buf, p + 3, CCG4_ROW_SIZE + 4)) {
1126 row = get_unaligned_be16(wr_buf);
1127 len = get_unaligned_be16(&wr_buf[2]);
1129 if (len != CCG4_ROW_SIZE) {
1134 err = ccg_cmd_write_flash_row(uc, row, wr_buf + 4,
1143 dev_info(dev, "total %d row flashed. time: %dms\n",
1144 line_cnt, jiffies_to_msecs(jiffies - start_time));
1146 err = ccg_cmd_validate_fw(uc, (mode == PRIMARY) ? FW2 : FW1);
1148 dev_err(dev, "%s validation failed err=%d\n",
1149 (mode == PRIMARY) ? "FW2" : "FW1", err);
1151 dev_info(dev, "%s validated\n",
1152 (mode == PRIMARY) ? "FW2" : "FW1");
1154 err = ccg_cmd_port_control(uc, false);
1158 err = ccg_cmd_reset(uc);
1162 err = ccg_cmd_port_control(uc, true);
1170 release_firmware(fw);
1174 /*******************************************************************************
1175 * CCG4 has two copies of the firmware in addition to the bootloader.
1176 * If the device is running FW1, FW2 can be updated with the new version.
1177 * Dual firmware mode allows the CCG device to stay in a PD contract and support
1178 * USB PD and Type-C functionality while a firmware update is in progress.
1179 ******************************************************************************/
1180 static int ccg_fw_update(struct ucsi_ccg *uc, enum enum_flash_mode flash_mode)
1184 while (flash_mode != FLASH_NOT_NEEDED) {
1185 err = do_flash(uc, flash_mode);
1188 err = ccg_fw_update_needed(uc, &flash_mode);
1192 dev_info(uc->dev, "CCG FW update successful\n");
1197 static int ccg_restart(struct ucsi_ccg *uc)
1199 struct device *dev = uc->dev;
1202 status = ucsi_ccg_init(uc);
1204 dev_err(dev, "ucsi_ccg_start fail, err=%d\n", status);
1208 status = request_threaded_irq(uc->irq, NULL, ccg_irq_handler,
1209 IRQF_ONESHOT | IRQF_TRIGGER_HIGH,
1212 dev_err(dev, "request_threaded_irq failed - %d\n", status);
1216 status = ucsi_register(uc->ucsi);
1218 dev_err(uc->dev, "failed to register the interface\n");
1225 static void ccg_update_firmware(struct work_struct *work)
1227 struct ucsi_ccg *uc = container_of(work, struct ucsi_ccg, work);
1228 enum enum_flash_mode flash_mode;
1231 status = ccg_fw_update_needed(uc, &flash_mode);
1235 if (flash_mode != FLASH_NOT_NEEDED) {
1236 ucsi_unregister(uc->ucsi);
1237 free_irq(uc->irq, uc);
1239 ccg_fw_update(uc, flash_mode);
1244 static ssize_t do_flash_store(struct device *dev,
1245 struct device_attribute *attr,
1246 const char *buf, size_t n)
1248 struct ucsi_ccg *uc = i2c_get_clientdata(to_i2c_client(dev));
1251 if (kstrtobool(buf, &flash))
1257 if (uc->fw_build == 0x0) {
1258 dev_err(dev, "fail to flash FW due to missing FW build info\n");
1262 schedule_work(&uc->work);
1266 static DEVICE_ATTR_WO(do_flash);
1268 static struct attribute *ucsi_ccg_attrs[] = {
1269 &dev_attr_do_flash.attr,
1272 ATTRIBUTE_GROUPS(ucsi_ccg);
1274 static int ucsi_ccg_probe(struct i2c_client *client,
1275 const struct i2c_device_id *id)
1277 struct device *dev = &client->dev;
1278 struct ucsi_ccg *uc;
1281 uc = devm_kzalloc(dev, sizeof(*uc), GFP_KERNEL);
1286 uc->client = client;
1287 mutex_init(&uc->lock);
1288 init_completion(&uc->complete);
1289 INIT_WORK(&uc->work, ccg_update_firmware);
1290 INIT_WORK(&uc->pm_work, ccg_pm_workaround_work);
1292 /* Only fail FW flashing when FW build information is not provided */
1293 status = device_property_read_u16(dev, "ccgx,firmware-build",
1296 dev_err(uc->dev, "failed to get FW build information\n");
1298 /* reset ccg device and initialize ucsi */
1299 status = ucsi_ccg_init(uc);
1301 dev_err(uc->dev, "ucsi_ccg_init failed - %d\n", status);
1305 status = get_fw_info(uc);
1307 dev_err(uc->dev, "get_fw_info failed - %d\n", status);
1313 if (uc->info.mode & CCG_DEVINFO_PDPORTS_MASK)
1316 uc->ucsi = ucsi_create(dev, &ucsi_ccg_ops);
1317 if (IS_ERR(uc->ucsi))
1318 return PTR_ERR(uc->ucsi);
1320 ucsi_set_drvdata(uc->ucsi, uc);
1322 status = request_threaded_irq(client->irq, NULL, ccg_irq_handler,
1323 IRQF_ONESHOT | IRQF_TRIGGER_HIGH,
1326 dev_err(uc->dev, "request_threaded_irq failed - %d\n", status);
1327 goto out_ucsi_destroy;
1330 uc->irq = client->irq;
1332 status = ucsi_register(uc->ucsi);
1336 i2c_set_clientdata(client, uc);
1338 pm_runtime_set_active(uc->dev);
1339 pm_runtime_enable(uc->dev);
1340 pm_runtime_use_autosuspend(uc->dev);
1341 pm_runtime_set_autosuspend_delay(uc->dev, 5000);
1342 pm_runtime_idle(uc->dev);
1347 free_irq(uc->irq, uc);
1349 ucsi_destroy(uc->ucsi);
1354 static int ucsi_ccg_remove(struct i2c_client *client)
1356 struct ucsi_ccg *uc = i2c_get_clientdata(client);
1358 cancel_work_sync(&uc->pm_work);
1359 cancel_work_sync(&uc->work);
1360 pm_runtime_disable(uc->dev);
1361 ucsi_unregister(uc->ucsi);
1362 ucsi_destroy(uc->ucsi);
1363 free_irq(uc->irq, uc);
1368 static const struct i2c_device_id ucsi_ccg_device_id[] = {
1372 MODULE_DEVICE_TABLE(i2c, ucsi_ccg_device_id);
1374 static int ucsi_ccg_resume(struct device *dev)
1376 struct i2c_client *client = to_i2c_client(dev);
1377 struct ucsi_ccg *uc = i2c_get_clientdata(client);
1379 return ucsi_resume(uc->ucsi);
1382 static int ucsi_ccg_runtime_suspend(struct device *dev)
1387 static int ucsi_ccg_runtime_resume(struct device *dev)
1389 struct i2c_client *client = to_i2c_client(dev);
1390 struct ucsi_ccg *uc = i2c_get_clientdata(client);
1393 * Firmware version 3.1.10 or earlier, built for NVIDIA has known issue
1394 * of missing interrupt when a device is connected for runtime resume.
1395 * Schedule a work to call ISR as a workaround.
1397 if (uc->fw_build == CCG_FW_BUILD_NVIDIA &&
1398 uc->fw_version <= CCG_OLD_FW_VERSION)
1399 schedule_work(&uc->pm_work);
1404 static const struct dev_pm_ops ucsi_ccg_pm = {
1405 .resume = ucsi_ccg_resume,
1406 .runtime_suspend = ucsi_ccg_runtime_suspend,
1407 .runtime_resume = ucsi_ccg_runtime_resume,
1410 static struct i2c_driver ucsi_ccg_driver = {
1414 .dev_groups = ucsi_ccg_groups,
1416 .probe = ucsi_ccg_probe,
1417 .remove = ucsi_ccg_remove,
1418 .id_table = ucsi_ccg_device_id,
1421 module_i2c_driver(ucsi_ccg_driver);
1423 MODULE_AUTHOR("Ajay Gupta <ajayg@nvidia.com>");
1424 MODULE_DESCRIPTION("UCSI driver for Cypress CCGx Type-C controller");
1425 MODULE_LICENSE("GPL v2");