From patchwork Mon Jan 28 20:37:26 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ajay Gupta X-Patchwork-Id: 10784703 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 4BB7C139A for ; Mon, 28 Jan 2019 20:37:45 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 3732F2C048 for ; Mon, 28 Jan 2019 20:37:45 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 2B8BD2C2E0; Mon, 28 Jan 2019 20:37:45 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id B0F502C345 for ; Mon, 28 Jan 2019 20:37:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727469AbfA1Uhn (ORCPT ); Mon, 28 Jan 2019 15:37:43 -0500 Received: from mail-pf1-f193.google.com ([209.85.210.193]:34518 "EHLO mail-pf1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727115AbfA1Uhn (ORCPT ); Mon, 28 Jan 2019 15:37:43 -0500 Received: by mail-pf1-f193.google.com with SMTP id h3so8558087pfg.1 for ; Mon, 28 Jan 2019 12:37:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=mYzchNjikRNCbk6Vy9NeeLttSoN5tumBPhkzJQ0G9NQ=; b=BIiMkXx50rAe5HpKb9NQkrPwy2d1NUDjH/Yl1Tw2CGms3eR69mPJw8nKsxF3Tgko1J TqQ1uIIQgIFmP10ldN/h7H/Hyg2DbMj3l9z11abXn71RBKVb7nSJt8hFmNi7/lmV+KFy Z5RT3rnFi+VN2fPaAv3tjesXuP3aOkGvn4OyZ8hovQDMspHXjT+xfi54aWGB8DlNL42V cHM/qRdy5XV6ojYixRGgijA6tvV3np5M+Jd6kz6/K0OVf5n276hydLigGiFRfZ+bFdN1 WcfdLyRQrZrZw754C+u9hGhwX3+tpqBBlpTfbWhO6d3TKb9sncVUCD/hetmpNHILMfTY ZPSg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=mYzchNjikRNCbk6Vy9NeeLttSoN5tumBPhkzJQ0G9NQ=; b=CJv0RlZUuEHTYwm+dZOASlIjUZgcfx33y7U5+aLBIzwazLqHERkSnUqL+c4lAA4w93 l/4r84XtiAlp7km/7lRZjVa0XLJfnsQ7+Ev0nrfPuAksKKHx/Ot43D41Pk2BZvW66Omb pFcIk6Fh6fpJ2KHaeBwPuIc7OcMrSdmvxeAmlr6ATkmhh3P/A4ryWTAoohWwjaLW+C/A wVuBRGwE0npY1ID2PyZ36qESltbo4T0zfkSCcaatGYpDbufBq+Kg1NLpb+pUccklqwCs gA3IPcB+aq3+zjgAdkmlVtbcRss2S9zjtEIU21bwqhNCbWcByFjZCsDOd8ZkVKVoj9KT rtvQ== X-Gm-Message-State: AJcUukclYc2TF4YO8lvihF00g3SU5m109Whr8TeB2SWIQtW2gSVDQiMx AwEbo9kZXhFPgK1vb5S5pcM= X-Google-Smtp-Source: ALg8bN67SqGlYXIQW7kTrE1PTFoOdqCeuv8tzesD6IMMnR9DurSHXDuQcOpE3wS7keI0KVNcOoDaHQ== X-Received: by 2002:a63:fd53:: with SMTP id m19mr21385853pgj.340.1548707862049; Mon, 28 Jan 2019 12:37:42 -0800 (PST) Received: from ajayg.nvidia.com (searspoint.nvidia.com. [216.228.112.21]) by smtp.gmail.com with ESMTPSA id t3sm34628047pgv.31.2019.01.28.12.37.40 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 28 Jan 2019 12:37:41 -0800 (PST) From: Ajay Gupta X-Google-Original-From: Ajay Gupta To: heikki.krogerus@linux.intel.com Cc: linux-usb@vger.kernel.org, Ajay Gupta Subject: [PATCH v2 1/6] usb: typec: ucsi: add get_fw_info function Date: Mon, 28 Jan 2019 12:37:26 -0800 Message-Id: <20190128203731.12681-2-ajayg@nvidia.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190128203731.12681-1-ajayg@nvidia.com> References: <20190128203731.12681-1-ajayg@nvidia.com> X-NVConfidentiality: public Sender: linux-usb-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-usb@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Function is to get the details of ccg firmware and device version. It will be useful in debugging and also during firmware update. Signed-off-by: Ajay Gupta --- Changes from v1: - Updated commit message - Dropped uses of bitfield - Removed dev_dbg prints drivers/usb/typec/ucsi/ucsi_ccg.c | 58 +++++++++++++++++++++++++++++-- 1 file changed, 56 insertions(+), 2 deletions(-) diff --git a/drivers/usb/typec/ucsi/ucsi_ccg.c b/drivers/usb/typec/ucsi/ucsi_ccg.c index de8a43bdff68..76cf772872db 100644 --- a/drivers/usb/typec/ucsi/ucsi_ccg.c +++ b/drivers/usb/typec/ucsi/ucsi_ccg.c @@ -17,15 +17,46 @@ #include #include "ucsi.h" +struct ccg_dev_info { +#define CCG_DEVINFO_FWMODE_SHIFT (0) +#define CCG_DEVINFO_FWMODE_MASK (0x3 << CCG_DEVINFO_FWMODE_SHIFT) +#define CCG_DEVINFO_PDPORTS_SHIFT (2) +#define CCG_DEVINFO_PDPORTS_MASK (0x3 << CCG_DEVINFO_PDPORTS_SHIFT) + u8 mode; + u8 bl_mode; + u16 silicon_id; + u16 bl_last_row; +}; + +struct version_format { + u16 build; + u8 patch; + u8 ver; +#define CCG_VERSION_MIN_SHIFT (0) +#define CCG_VERSION_MIN_MASK (0xf << CCG_VERSION_MIN_SHIFT) +#define CCG_VERSION_MAJ_SHIFT (4) +#define CCG_VERSION_MAJ_MASK (0xf << CCG_VERSION_MAJ_SHIFT) +}; + +struct version_info { + struct version_format base; + struct version_format app; +}; + struct ucsi_ccg { struct device *dev; struct ucsi *ucsi; struct ucsi_ppm ppm; struct i2c_client *client; + struct ccg_dev_info info; + struct version_info version[3]; }; -#define CCGX_RAB_INTR_REG 0x06 -#define CCGX_RAB_UCSI_CONTROL 0x39 +#define CCGX_RAB_DEVICE_MODE 0x0000 +#define CCGX_RAB_INTR_REG 0x0006 +#define CCGX_RAB_READ_ALL_VER 0x0010 +#define CCGX_RAB_READ_FW2_VER 0x0020 +#define CCGX_RAB_UCSI_CONTROL 0x0039 #define CCGX_RAB_UCSI_CONTROL_START BIT(0) #define CCGX_RAB_UCSI_CONTROL_STOP BIT(1) #define CCGX_RAB_UCSI_DATA_BLOCK(offset) (0xf000 | ((offset) & 0xff)) @@ -220,6 +251,23 @@ static irqreturn_t ccg_irq_handler(int irq, void *data) return IRQ_HANDLED; } +static int get_fw_info(struct ucsi_ccg *uc) +{ + int err; + + err = ccg_read(uc, CCGX_RAB_READ_ALL_VER, (u8 *)(&uc->version), + sizeof(uc->version)); + if (err < 0) + return err; + + err = ccg_read(uc, CCGX_RAB_DEVICE_MODE, (u8 *)(&uc->info), + sizeof(uc->info)); + if (err < 0) + return err; + + return 0; +} + static int ucsi_ccg_probe(struct i2c_client *client, const struct i2c_device_id *id) { @@ -248,6 +296,12 @@ static int ucsi_ccg_probe(struct i2c_client *client, return status; } + status = get_fw_info(uc); + if (status < 0) { + dev_err(uc->dev, "get_fw_info failed - %d\n", status); + return status; + } + status = devm_request_threaded_irq(dev, client->irq, NULL, ccg_irq_handler, IRQF_ONESHOT | IRQF_TRIGGER_HIGH, From patchwork Mon Jan 28 20:37:27 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ajay Gupta X-Patchwork-Id: 10784705 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 31C3D139A for ; Mon, 28 Jan 2019 20:37:47 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 202122C331 for ; Mon, 28 Jan 2019 20:37:47 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 14D0A2C32D; Mon, 28 Jan 2019 20:37:47 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 4104A2C04C for ; Mon, 28 Jan 2019 20:37:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727616AbfA1Uhp (ORCPT ); Mon, 28 Jan 2019 15:37:45 -0500 Received: from mail-pg1-f196.google.com ([209.85.215.196]:46406 "EHLO mail-pg1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726719AbfA1Uho (ORCPT ); Mon, 28 Jan 2019 15:37:44 -0500 Received: by mail-pg1-f196.google.com with SMTP id w7so7693948pgp.13 for ; Mon, 28 Jan 2019 12:37:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=X7YJWwAzbkQTZVvk0v5qfpxGosEf+62mWyxOmlmyORI=; b=OwaUK85aucn/TaKOOxQlLyMkDOB4gEv0wYn5xA8LW1TKASeYmgiBeacLuljL2teAaV bNYe4InmDCh3zyxCLD7pq+R8y5BaWDPtoplV/TSd4c7W2p9DGc3Na7OC9xDbXq60jgFL /s5xBNfBha5LnGjEZP8p/m5SOiJzrG0HucnbB3aghoopktB5KTLlryX5hBnktbOro1PB d9l9rJOvWYhzCTtrsYxaLQGT4gECeadY6ksKCRhxQ8EK0ciUnKTEvFpBaIAXonc9TWh8 knBIoZtlLR3asA2qI4roh0GypldUASb6BWjUKeMfFGMW94JIQa4SDZLTLa7FLSmdcSQb 2mYw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=X7YJWwAzbkQTZVvk0v5qfpxGosEf+62mWyxOmlmyORI=; b=ZJsKzreaXpxsULfsQZ/HKf05KlgF1NWSWoO5dcCzkQzIL+Jt1z+28ekGL5cyYQdM59 j0FHVjW8pd/mIn7ATMzgC3avLOdhB4JeLFvpIoe4Qsj/+beCrRT8hxh9wEa7eAcEmDS/ LK2KMwlnOsC4DqcmhfPOPdflgzzDx7e3QC+DhJQrZQRnFsQAXL5mH/uDDtUCdQzbmcap f7vTnS4otTl4usCefHD1WJRV8yXshWtP5EzzO47wq7wsgmVN1pm8bdIpTuGQcV7Gpk8U Xf9vP2y978shIZZMp+QbPCYGX5KYdPD3KN1azc1eWMDtpJoLqgsi1CrFeSrxZtni8U0i S25A== X-Gm-Message-State: AJcUukdN67Cda17/GjA6thMyaGAGAkhSA36iekeXHlYbKs7JVDrLeA/U biNjFR7BcfIDLN04i90BB1g= X-Google-Smtp-Source: ALg8bN5pq+0lgg5nkeqCA5prI4+c2rEnIkByy+KgbT4LB/MLCRUcLAgh4MMY5NPQ/IpbtYc8cd86TA== X-Received: by 2002:a62:6ec8:: with SMTP id j191mr23397472pfc.198.1548707863165; Mon, 28 Jan 2019 12:37:43 -0800 (PST) Received: from ajayg.nvidia.com (searspoint.nvidia.com. [216.228.112.21]) by smtp.gmail.com with ESMTPSA id t3sm34628047pgv.31.2019.01.28.12.37.42 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 28 Jan 2019 12:37:42 -0800 (PST) From: Ajay Gupta X-Google-Original-From: Ajay Gupta To: heikki.krogerus@linux.intel.com Cc: linux-usb@vger.kernel.org, Ajay Gupta Subject: [PATCH v2 2/6] usb: typec: ucsi: add ccg command framework Date: Mon, 28 Jan 2019 12:37:27 -0800 Message-Id: <20190128203731.12681-3-ajayg@nvidia.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190128203731.12681-1-ajayg@nvidia.com> References: <20190128203731.12681-1-ajayg@nvidia.com> X-NVConfidentiality: public Sender: linux-usb-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-usb@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Used to send various commands to ccg controller. They are mainly used during firmware update process. We wait for response after sending the command and then read the response from RAB_RESPONSE register. Signed-off-by: Ajay Gupta --- Changes from v1: - Updated commit message and dropped dev_dbg prints drivers/usb/typec/ucsi/ucsi_ccg.c | 242 ++++++++++++++++++++++++++++-- 1 file changed, 233 insertions(+), 9 deletions(-) diff --git a/drivers/usb/typec/ucsi/ucsi_ccg.c b/drivers/usb/typec/ucsi/ucsi_ccg.c index 76cf772872db..3155ee6be357 100644 --- a/drivers/usb/typec/ucsi/ucsi_ccg.c +++ b/drivers/usb/typec/ucsi/ucsi_ccg.c @@ -17,6 +17,29 @@ #include #include "ucsi.h" +#define CCGX_RAB_DEVICE_MODE 0x0000 +#define CCGX_RAB_INTR_REG 0x0006 +#define DEV_INT BIT(0) +#define PORT0_INT BIT(1) +#define PORT1_INT BIT(2) +#define UCSI_READ_INT BIT(7) +#define CCGX_RAB_READ_ALL_VER 0x0010 +#define CCGX_RAB_READ_FW2_VER 0x0020 +#define CCGX_RAB_UCSI_CONTROL 0x0039 +#define CCGX_RAB_UCSI_CONTROL_START BIT(0) +#define CCGX_RAB_UCSI_CONTROL_STOP BIT(1) +#define CCGX_RAB_UCSI_DATA_BLOCK(offset) (0xf000 | ((offset) & 0xff)) +#define DEV_REG_IDX CCGX_RAB_DEVICE_MODE +#define CCGX_RAB_RESPONSE 0x007E +#define ASYNC_EVENT BIT(7) + +/* CCGx events & async msg codes */ +#define RESET_COMPLETE 0x80 +#define EVENT_INDEX RESET_COMPLETE +#define PORT_CONNECT_DET 0x84 +#define PORT_DISCONNECT_DET 0x85 +#define ROLE_SWAP_COMPELETE 0x87 + struct ccg_dev_info { #define CCG_DEVINFO_FWMODE_SHIFT (0) #define CCG_DEVINFO_FWMODE_MASK (0x3 << CCG_DEVINFO_FWMODE_SHIFT) @@ -43,6 +66,99 @@ struct version_info { struct version_format app; }; +/* CCGx response codes */ +enum ccg_resp_code { + CMD_NO_RESP = 0x00, + CMD_SUCCESS = 0x02, + FLASH_DATA_AVAILABLE = 0x03, + CMD_INVALID = 0x05, + FLASH_UPDATE_FAIL = 0x07, + INVALID_FW = 0x08, + INVALID_ARG = 0x09, + CMD_NOT_SUPPORT = 0x0A, + TRANSACTION_FAIL = 0x0C, + PD_CMD_FAIL = 0x0D, + UNDEF_ERROR = 0x0F, + INVALID_RESP = 0x10, +}; + +static const char * const ccg_resp_strs[] = { + /* 0x00 */ "No Response.", + /* 0x01 */ "0x01", + /* 0x02 */ "HPI Command Success.", + /* 0x03 */ "Flash Data Available in data memory.", + /* 0x04 */ "0x04", + /* 0x05 */ "Invalid Command.", + /* 0x06 */ "0x06", + /* 0x07 */ "Flash write operation failed.", + /* 0x08 */ "Firmware validity check failed.", + /* 0x09 */ "Command failed due to invalid arguments.", + /* 0x0A */ "Command not supported in the current mode.", + /* 0x0B */ "0x0B", + /* 0x0C */ "Transaction Failed. GOOD_CRC was not received.", + /* 0x0D */ "PD Command Failed.", + /* 0x0E */ "0x0E", + /* 0x0F */ "Undefined Error", +}; + +static const char * const ccg_evt_strs[] = { + /* 0x80 */ "Reset Complete.", + /* 0x81 */ "Message queue overflow detected.", + /* 0x82 */ "Overcurrent Detected", + /* 0x83 */ "Overvoltage Detected", + /* 0x84 */ "Type-C Port Connect Detected", + /* 0x85 */ "Type-C Port Disconnect Detected", + /* 0x86 */ "PD Contract Negotiation Complete", + /* 0x87 */ "SWAP Complete", + /* 0x88 */ "0x88", + /* 0x89 */ "0x89", + /* 0x8A */ "PS_RDY Message Received", + /* 0x8B */ "GotoMin Message Received.", + /* 0x8C */ "Accept Message Received", + /* 0x8D */ "Reject Message Received", + /* 0x8E */ "Wait Message Received", + /* 0x8F */ "Hard Reset Received", + /* 0x90 */ "VDM Received", + /* 0x91 */ "Source Capabilities Message Received", + /* 0x92 */ "Sink Capabilities Message Received", + /* 0x93 */ "Display Port Alternate Mode entered", + /* 0x94 */ "Display Port device connected at UFP_U", + /* 0x95 */ "Display port device not connected at UFP_U", + /* 0x96 */ "Display port SID not found in Discover SID process", + /* 0x97 */ "Multiple SVIDs discovered along with DisplayPort SID", + /* 0x98 */ "DP Functionality not supported by Cable", + /* 0x99 */ "Display Port Configuration not supported by UFP", + /* 0x9A */ "Hard Reset Sent to Port Partner", + /* 0x9B */ "Soft Reset Sent to Port Partner", + /* 0x9C */ "Cable Reset Sent to EMCA", + /* 0x9D */ "Source Disabled State Entered", + /* 0x9E */ "Sender Response Timer Timeout", + /* 0x9F */ "No VDM Response Received", + /* 0xA0 */ "Unexpected Voltage on Vbus", + /* 0xA1 */ "Type-C Error Recovery", + /* 0xA2 */ "0xA2", + /* 0xA3 */ "0xA3", + /* 0xA4 */ "0xA4", + /* 0xA5 */ "0xA5", + /* 0xA6 */ "EMCA Detected", + /* 0xA7 */ "0xA7", + /* 0xA8 */ "0xA8", + /* 0xA9 */ "0xA9", + /* 0xAA */ "Rp Change Detected", +}; + +struct ccg_cmd { + u16 reg; + u32 data; + int len; + int delay; /* ms delay for cmd timeout */ +}; + +struct ccg_resp { + u8 code; + u8 length; +}; + struct ucsi_ccg { struct device *dev; struct ucsi *ucsi; @@ -50,17 +166,14 @@ struct ucsi_ccg { struct i2c_client *client; struct ccg_dev_info info; struct version_info version[3]; + /* CCG HPI communication flags */ + unsigned long flags; +#define RESET_PENDING 0 +#define DEV_CMD_PENDING 1 + struct ccg_resp dev_resp; + u8 cmd_resp; }; -#define CCGX_RAB_DEVICE_MODE 0x0000 -#define CCGX_RAB_INTR_REG 0x0006 -#define CCGX_RAB_READ_ALL_VER 0x0010 -#define CCGX_RAB_READ_FW2_VER 0x0020 -#define CCGX_RAB_UCSI_CONTROL 0x0039 -#define CCGX_RAB_UCSI_CONTROL_START BIT(0) -#define CCGX_RAB_UCSI_CONTROL_STOP BIT(1) -#define CCGX_RAB_UCSI_DATA_BLOCK(offset) (0xf000 | ((offset) & 0xff)) - static int ccg_read(struct ucsi_ccg *uc, u16 rab, u8 *data, u32 len) { struct i2c_client *client = uc->client; @@ -268,6 +381,117 @@ static int get_fw_info(struct ucsi_ccg *uc) return 0; } +static inline bool invalid_resp(int code) +{ + return (code >= INVALID_RESP); +} + +static inline bool invalid_evt(int code) +{ + unsigned long num_of_events = ARRAY_SIZE(ccg_evt_strs); + + return (code >= (EVENT_INDEX + num_of_events)) || (code < EVENT_INDEX); +} + +static void ccg_process_response(struct ucsi_ccg *uc) +{ + struct device *dev = uc->dev; + + if (uc->dev_resp.code & ASYNC_EVENT) { + if (uc->dev_resp.code == RESET_COMPLETE) { + if (test_bit(RESET_PENDING, &uc->flags)) + uc->cmd_resp = uc->dev_resp.code; + get_fw_info(uc); + } + + if (invalid_evt(uc->dev_resp.code)) + dev_err(dev, "invalid evt %d\n", uc->dev_resp.code); + } else { + if (test_bit(DEV_CMD_PENDING, &uc->flags)) { + uc->cmd_resp = uc->dev_resp.code; + clear_bit(DEV_CMD_PENDING, &uc->flags); + } else { + dev_err(dev, "dev resp 0x%04x but no cmd pending\n", + uc->dev_resp.code); + } + } +} + +static int ccg_read_response(struct ucsi_ccg *uc) +{ + unsigned long target = jiffies + msecs_to_jiffies(1000); + struct device *dev = uc->dev; + u8 intval; + int status; + + /* wait for interrupt status to get updated */ + do { + status = ccg_read(uc, CCGX_RAB_INTR_REG, &intval, + sizeof(intval)); + if (status < 0) + return status; + + if (intval & DEV_INT) + break; + usleep_range(500, 600); + } while (time_is_after_jiffies(target)); + + if (time_is_before_jiffies(target)) { + dev_err(dev, "response timeout error\n"); + return -ETIME; + } + + status = ccg_read(uc, CCGX_RAB_RESPONSE, (u8 *)&uc->dev_resp, + sizeof(uc->dev_resp)); + if (status < 0) + return status; + + status = ccg_write(uc, CCGX_RAB_INTR_REG, &intval, sizeof(intval)); + if (status < 0) + return status; + + return 0; +} + +/* Caller must hold uc->lock */ +static int ccg_send_command(struct ucsi_ccg *uc, struct ccg_cmd *cmd) +{ + struct device *dev = uc->dev; + int ret; + + switch (cmd->reg & 0xF000) { + case DEV_REG_IDX: + set_bit(DEV_CMD_PENDING, &uc->flags); + break; + default: + dev_err(dev, "invalid cmd register\n"); + break; + } + + ret = ccg_write(uc, cmd->reg, (u8 *)&cmd->data, cmd->len); + if (ret < 0) + return ret; + + msleep(cmd->delay); + + ret = ccg_read_response(uc); + if (ret < 0) { + dev_err(dev, "response read error\n"); + switch (cmd->reg & 0xF000) { + case DEV_REG_IDX: + clear_bit(DEV_CMD_PENDING, &uc->flags); + break; + default: + dev_err(dev, "invalid cmd register\n"); + break; + } + return -EIO; + } + ccg_process_response(uc); + + return uc->cmd_resp; +} + static int ucsi_ccg_probe(struct i2c_client *client, const struct i2c_device_id *id) { From patchwork Mon Jan 28 20:37:28 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ajay Gupta X-Patchwork-Id: 10784707 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id A94B0139A for ; Mon, 28 Jan 2019 20:37:48 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 98CBA2C351 for ; Mon, 28 Jan 2019 20:37:48 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 85FA92C34F; Mon, 28 Jan 2019 20:37:48 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id BAA742C344 for ; Mon, 28 Jan 2019 20:37:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727735AbfA1Uhp (ORCPT ); Mon, 28 Jan 2019 15:37:45 -0500 Received: from mail-pf1-f196.google.com ([209.85.210.196]:34523 "EHLO mail-pf1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727115AbfA1Uhp (ORCPT ); Mon, 28 Jan 2019 15:37:45 -0500 Received: by mail-pf1-f196.google.com with SMTP id h3so8558153pfg.1 for ; Mon, 28 Jan 2019 12:37:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=I4HD6/r3kKX9iTxQFy5RVm1vsXvdqz7PwXuXe75axDI=; b=K5awJaVhYQ9Vo+cq7HHT8bMO80BNF8Cn7b1y8atKaMsQJr6cNM0Cyaheqm5sTjCrtf kXpnZqq5HKDpSTM5ZqQneA4dXu4K77X6HMXgZN1qrlFM8qhpaqciMLlxztBUWw2t+uHd AeLNhgZE08U5Fx4R6rmCKVx+gbfzee353RcbJbnn730QVQPiACyIDOrrUuZdIqmW8bdU GcvmtWWxfV77eAlZysioO9VHTJQalexQJgBfeqsva9uf10haCZsX3M5oKhQ9Kv1HZvKy O9O2LfZl131XeJ76OlfZWiCLcx+yhxHWHJrSl+Sz9V/g28rrnSwa8g9v/7puVrQpeeY2 r31A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=I4HD6/r3kKX9iTxQFy5RVm1vsXvdqz7PwXuXe75axDI=; b=ZccatXFOjafhQ4mGrUpRHu9NEVezZ2FzjEiAoDZY22XDGaCOQLw1Bm0Mwg3BoZ3hCw X/C4LNj4v/Jkb/5B/RvhXvoMRn5HCHvIWtF5L4KESwUQ+QSNRkiY6GNbm5xA+1sp06dH iFd27ExIkZU/cJYQzDxgYyOc7ZXxbNxsdwj9apoYv4IOv71OcVZQEhHR8Sb2HdFtLHNP RR+wzlKLSUGXldwvJOuC77fnrzGijDNd2s9vFhi073U+fzH5m7bYgy74aw7kw9b2BPcK UNKMSHzyDtw+qM/kawwQGCVNIK0ogyRXcvEPihvi3d3P4ACzf1BW0+UZfPQ6LgQZjP6x 23Kg== X-Gm-Message-State: AJcUukdTa2ezQ6puQe6AXdzp3U939hrCoP3eaLurKSXmPzfNBmELZBlc Cve3fzp8PEeoRH09+PBGoR8+bBDm X-Google-Smtp-Source: ALg8bN5N4LEotX8laHGTvzaDnZjs0wyOGTC3Oa58fhbK5Zo7gfzu29SUtQpv3sDVdzUrSml0w5UAJQ== X-Received: by 2002:a62:37c3:: with SMTP id e186mr23850948pfa.251.1548707864318; Mon, 28 Jan 2019 12:37:44 -0800 (PST) Received: from ajayg.nvidia.com (searspoint.nvidia.com. [216.228.112.21]) by smtp.gmail.com with ESMTPSA id t3sm34628047pgv.31.2019.01.28.12.37.43 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 28 Jan 2019 12:37:43 -0800 (PST) From: Ajay Gupta X-Google-Original-From: Ajay Gupta To: heikki.krogerus@linux.intel.com Cc: linux-usb@vger.kernel.org, Ajay Gupta Subject: [PATCH v2 3/6] usb: typec: ucsi: add port num info Date: Mon, 28 Jan 2019 12:37:28 -0800 Message-Id: <20190128203731.12681-4-ajayg@nvidia.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190128203731.12681-1-ajayg@nvidia.com> References: <20190128203731.12681-1-ajayg@nvidia.com> X-NVConfidentiality: public Sender: linux-usb-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-usb@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Read PD port number information and save. It will be required while sending PD_PORT_ENABLE command. Signed-off-by: Ajay Gupta --- Changes from v1: - Updated commit message - Used mask and shift instead of bitfield drivers/usb/typec/ucsi/ucsi_ccg.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/drivers/usb/typec/ucsi/ucsi_ccg.c b/drivers/usb/typec/ucsi/ucsi_ccg.c index 3155ee6be357..f71387a21693 100644 --- a/drivers/usb/typec/ucsi/ucsi_ccg.c +++ b/drivers/usb/typec/ucsi/ucsi_ccg.c @@ -172,6 +172,7 @@ struct ucsi_ccg { #define DEV_CMD_PENDING 1 struct ccg_resp dev_resp; u8 cmd_resp; + int port_num; }; static int ccg_read(struct ucsi_ccg *uc, u16 rab, u8 *data, u32 len) @@ -526,6 +527,12 @@ static int ucsi_ccg_probe(struct i2c_client *client, return status; } + if ((uc->info.mode & CCG_DEVINFO_PDPORTS_MASK) >> + CCG_DEVINFO_PDPORTS_SHIFT) + uc->port_num = 2; + else + uc->port_num = 1; + status = devm_request_threaded_irq(dev, client->irq, NULL, ccg_irq_handler, IRQF_ONESHOT | IRQF_TRIGGER_HIGH, From patchwork Mon Jan 28 20:37:29 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ajay Gupta X-Patchwork-Id: 10784709 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 1B647922 for ; Mon, 28 Jan 2019 20:37:49 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 0B2782C048 for ; Mon, 28 Jan 2019 20:37:49 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id F3A5B2C35B; Mon, 28 Jan 2019 20:37:48 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 541DB2C048 for ; Mon, 28 Jan 2019 20:37:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727766AbfA1Uhr (ORCPT ); Mon, 28 Jan 2019 15:37:47 -0500 Received: from mail-pg1-f194.google.com ([209.85.215.194]:34111 "EHLO mail-pg1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727115AbfA1Uhq (ORCPT ); Mon, 28 Jan 2019 15:37:46 -0500 Received: by mail-pg1-f194.google.com with SMTP id j10so7725018pga.1 for ; Mon, 28 Jan 2019 12:37:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=syDpRKg2EbZnqmLDx7nb4agewOZ8WED9GVHBglxsS1A=; b=j+J/+ZrqW6wpMGttXUbl7g9DLaEkrSBvzl2uu5SPFnOzvRXUacRHeY8Q78tNSg+xIV 3OYUed/yUTVRQjPUQlDChfAUBZ+7GIm0+kONVjHUyXbwqOCQgQgw24yjFLk7TLDBdL+z Ti3gyeEZBQwAwlPXLD1gFfW04R9Ry2QWSvV3CQ+evWyc+LslSQOsyPVVsG00KhTer+Nh SbPqfUWFpE/dXhklI5WhIwAbL9lbTj55ybk3rU8EBo9u90D6Xv0MnA6B3zJS0q1IgXU8 ERqWwEOU66G4Zfcb27rCIoUSQAM4sZQwOfDbIgJavqHXiYd7PwfCcpdEQdwf77mdHvlT GnuA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=syDpRKg2EbZnqmLDx7nb4agewOZ8WED9GVHBglxsS1A=; b=rydPOgvwpAjJ9OWCe9b0HFj520TG8Rvq3WjEWAghPaz9LqUYCiqV6Wsh7dH6wga6ie LIW4rh53NsST0o1C2rXW5Zj9wIYGNyboF9vu5ahN7VXk5x9ahGAQCti1k3DLrhVaHT8a fF1iiDY2Z+7b98lXfod45bD1NvxPozHWHdkfmN7NDGiuOVEchGKMwATsza3MfiSTQdTj 91YMqSywuW6Shy09Ui37LEC/jWSRlvuL8Rkji+XWLmxJLhkysN1Zzyo2bGKh8n4YBgZq +k6MfrlaEXkJi6kl4sh0Qh6JguHxZ81xps4UaZkrbC7HvCcyiOxPuFfFTRPg8d6Psc3i EzcA== X-Gm-Message-State: AJcUukdve+heETrppW3zE28xzTNzgXnbYRTrJF7Tm7lEZM4KKxMP44SQ xsJ0mukhVEwCGDW8xukOb3I= X-Google-Smtp-Source: ALg8bN6AO+8WgA5M0o+CL/u7A31e9zkeqsRkjT1mZ5yGWx3H3EP50KOM7UTSqRMEDDo2p6D8DsyEkw== X-Received: by 2002:a62:130c:: with SMTP id b12mr23425195pfj.247.1548707865424; Mon, 28 Jan 2019 12:37:45 -0800 (PST) Received: from ajayg.nvidia.com (searspoint.nvidia.com. [216.228.112.21]) by smtp.gmail.com with ESMTPSA id t3sm34628047pgv.31.2019.01.28.12.37.44 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 28 Jan 2019 12:37:44 -0800 (PST) From: Ajay Gupta X-Google-Original-From: Ajay Gupta To: heikki.krogerus@linux.intel.com Cc: linux-usb@vger.kernel.org, Ajay Gupta Subject: [PATCH v2 4/6] usb: typec: ucsi: add cmd used for fw flashing Date: Mon, 28 Jan 2019 12:37:29 -0800 Message-Id: <20190128203731.12681-5-ajayg@nvidia.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190128203731.12681-1-ajayg@nvidia.com> References: <20190128203731.12681-1-ajayg@nvidia.com> X-NVConfidentiality: public Sender: linux-usb-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-usb@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Adding support for below commands which will be used during firmware flashing. - ENTER_FLASHING - RESET - PDPORT_ENABLE - JUMP_TO_BOOT - FLASH_ROW_RW - VALIDATE_FW I command specific mutex lock is also added to sync between driver and user threads. Signed-off-by: Ajay Gupta --- Changes from v1: - Updated commit message - Used mask and shift instead of bit fields drivers/usb/typec/ucsi/ucsi_ccg.c | 216 ++++++++++++++++++++++++++++++ 1 file changed, 216 insertions(+) diff --git a/drivers/usb/typec/ucsi/ucsi_ccg.c b/drivers/usb/typec/ucsi/ucsi_ccg.c index f71387a21693..b1fadec9172b 100644 --- a/drivers/usb/typec/ucsi/ucsi_ccg.c +++ b/drivers/usb/typec/ucsi/ucsi_ccg.c @@ -23,13 +23,34 @@ #define PORT0_INT BIT(1) #define PORT1_INT BIT(2) #define UCSI_READ_INT BIT(7) +#define CCGX_RAB_JUMP_TO_BOOT 0x0007 +#define TO_BOOT 'J' +#define TO_ALT_FW 'A' +#define CCGX_RAB_RESET_REQ 0x0008 +#define RESET_SIG 'R' +#define CMD_RESET_I2C 0x0 +#define CMD_RESET_DEV 0x1 +#define CCGX_RAB_ENTER_FLASHING 0x000A +#define FLASH_ENTER_SIG 'P' +#define CCGX_RAB_VALIDATE_FW 0x000B +#define CCGX_RAB_FLASH_ROW_RW 0x000C +#define FLASH_SIG 'F' +#define FLASH_RD_CMD 0x0 +#define FLASH_WR_CMD 0x1 +#define FLASH_FWCT1_WR_CMD 0x2 +#define FLASH_FWCT2_WR_CMD 0x3 +#define FLASH_FWCT_SIG_WR_CMD 0x4 #define CCGX_RAB_READ_ALL_VER 0x0010 #define CCGX_RAB_READ_FW2_VER 0x0020 #define CCGX_RAB_UCSI_CONTROL 0x0039 #define CCGX_RAB_UCSI_CONTROL_START BIT(0) #define CCGX_RAB_UCSI_CONTROL_STOP BIT(1) #define CCGX_RAB_UCSI_DATA_BLOCK(offset) (0xf000 | ((offset) & 0xff)) +#define REG_FLASH_RW_MEM 0x0200 #define DEV_REG_IDX CCGX_RAB_DEVICE_MODE +#define CCGX_RAB_PDPORT_ENABLE 0x002C +#define PDPORT_1 BIT(0) +#define PDPORT_2 BIT(1) #define CCGX_RAB_RESPONSE 0x007E #define ASYNC_EVENT BIT(7) @@ -40,6 +61,13 @@ #define PORT_DISCONNECT_DET 0x85 #define ROLE_SWAP_COMPELETE 0x87 +/* ccg firmware */ +#define CYACD_LINE_SIZE 527 +#define CCG4_ROW_SIZE 256 +#define FW1_METADATA_ROW 0x1FF +#define FW2_METADATA_ROW 0x1FE +#define FW_CFG_TABLE_SIG_SIZE 256 + struct ccg_dev_info { #define CCG_DEVINFO_FWMODE_SHIFT (0) #define CCG_DEVINFO_FWMODE_MASK (0x3 << CCG_DEVINFO_FWMODE_SHIFT) @@ -173,6 +201,7 @@ struct ucsi_ccg { struct ccg_resp dev_resp; u8 cmd_resp; int port_num; + struct mutex lock; /* to sync between user and driver thread */ }; static int ccg_read(struct ucsi_ccg *uc, u16 rab, u8 *data, u32 len) @@ -493,6 +522,193 @@ static int ccg_send_command(struct ucsi_ccg *uc, struct ccg_cmd *cmd) return uc->cmd_resp; } +static int ccg_cmd_enter_flashing(struct ucsi_ccg *uc) +{ + struct ccg_cmd cmd; + int ret; + + cmd.reg = CCGX_RAB_ENTER_FLASHING; + cmd.data = FLASH_ENTER_SIG; + cmd.len = 1; + cmd.delay = 50; + + mutex_lock(&uc->lock); + + ret = ccg_send_command(uc, &cmd); + + mutex_unlock(&uc->lock); + + if (ret != CMD_SUCCESS) { + dev_err(uc->dev, "enter flashing failed ret=%d\n", ret); + return ret; + } + + return 0; +} + +static int ccg_cmd_reset(struct ucsi_ccg *uc, bool extra_delay) +{ + struct ccg_cmd cmd; + u8 *p; + int ret; + + p = (u8 *)&cmd.data; + cmd.reg = CCGX_RAB_RESET_REQ; + p[0] = RESET_SIG; + p[1] = CMD_RESET_DEV; + cmd.len = 2; + cmd.delay = 2000 + (extra_delay ? 3000 : 0); + + mutex_lock(&uc->lock); + + set_bit(RESET_PENDING, &uc->flags); + + ret = ccg_send_command(uc, &cmd); + if (ret != RESET_COMPLETE) + goto err_clear_flag; + + ret = 0; + +err_clear_flag: + clear_bit(RESET_PENDING, &uc->flags); + + mutex_unlock(&uc->lock); + + return ret; +} + +static int ccg_cmd_port_control(struct ucsi_ccg *uc, bool enable) +{ + struct ccg_cmd cmd; + int ret; + + cmd.reg = CCGX_RAB_PDPORT_ENABLE; + if (enable) + cmd.data = (uc->port_num == 1) ? + PDPORT_1 : (PDPORT_1 | PDPORT_2); + else + cmd.data = 0x0; + cmd.len = 1; + cmd.delay = 10; + + mutex_lock(&uc->lock); + + ret = ccg_send_command(uc, &cmd); + + mutex_unlock(&uc->lock); + + if (ret != CMD_SUCCESS) { + dev_err(uc->dev, "port control failed ret=%d\n", ret); + return ret; + } + return 0; +} + +static int ccg_cmd_jump_boot_mode(struct ucsi_ccg *uc, int bl_mode) +{ + struct ccg_cmd cmd; + int ret; + + cmd.reg = CCGX_RAB_JUMP_TO_BOOT; + + if (bl_mode) + cmd.data = TO_BOOT; + else + cmd.data = TO_ALT_FW; + + cmd.len = 1; + cmd.delay = 100; + + mutex_lock(&uc->lock); + + set_bit(RESET_PENDING, &uc->flags); + + ret = ccg_send_command(uc, &cmd); + if (ret != RESET_COMPLETE) + goto err_clear_flag; + + ret = 0; + +err_clear_flag: + clear_bit(RESET_PENDING, &uc->flags); + + mutex_unlock(&uc->lock); + + return ret; +} + +static int +ccg_cmd_write_flash_row(struct ucsi_ccg *uc, u16 row, + const void *data, u8 fcmd) +{ + struct i2c_client *client = uc->client; + struct ccg_cmd cmd; + u8 buf[CCG4_ROW_SIZE + 2]; + u8 *p; + int ret; + + /* Copy the data into the flash read/write memory. */ + buf[0] = REG_FLASH_RW_MEM & 0xFF; + buf[1] = REG_FLASH_RW_MEM >> 8; + + memcpy(buf + 2, data, CCG4_ROW_SIZE); + + mutex_lock(&uc->lock); + + ret = i2c_master_send(client, buf, CCG4_ROW_SIZE + 2); + if (ret != CCG4_ROW_SIZE + 2) { + dev_err(uc->dev, "REG_FLASH_RW_MEM write fail %d\n", ret); + return ret < 0 ? ret : -EIO; + } + + /* Use the FLASH_ROW_READ_WRITE register to trigger */ + /* writing of data to the desired flash row */ + p = (u8 *)&cmd.data; + cmd.reg = CCGX_RAB_FLASH_ROW_RW; + p[0] = FLASH_SIG; + p[1] = fcmd; + p[2] = row & 0xFF; + p[3] = row >> 8; + cmd.len = 4; + cmd.delay = 50; + if (fcmd == FLASH_FWCT_SIG_WR_CMD) + cmd.delay += 400; + if (row == 510) + cmd.delay += 220; + ret = ccg_send_command(uc, &cmd); + + mutex_unlock(&uc->lock); + + if (ret != CMD_SUCCESS) { + dev_err(uc->dev, "write flash row failed ret=%d\n", ret); + return ret; + } + + return 0; +} + +static int ccg_cmd_validate_fw(struct ucsi_ccg *uc, unsigned int fwid) +{ + struct ccg_cmd cmd; + int ret; + + cmd.reg = CCGX_RAB_VALIDATE_FW; + cmd.data = fwid; + cmd.len = 1; + cmd.delay = 500; + + mutex_lock(&uc->lock); + + ret = ccg_send_command(uc, &cmd); + + mutex_unlock(&uc->lock); + + if (ret != CMD_SUCCESS) + return ret; + + return 0; +} + static int ucsi_ccg_probe(struct i2c_client *client, const struct i2c_device_id *id) { From patchwork Mon Jan 28 20:37:30 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ajay Gupta X-Patchwork-Id: 10784711 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id B4347184E for ; Mon, 28 Jan 2019 20:37:49 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id A25CC2C338 for ; Mon, 28 Jan 2019 20:37:49 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 96B522C351; Mon, 28 Jan 2019 20:37:49 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 132102C34F for ; Mon, 28 Jan 2019 20:37:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727804AbfA1Uhr (ORCPT ); Mon, 28 Jan 2019 15:37:47 -0500 Received: from mail-pf1-f194.google.com ([209.85.210.194]:46076 "EHLO mail-pf1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727750AbfA1Uhr (ORCPT ); Mon, 28 Jan 2019 15:37:47 -0500 Received: by mail-pf1-f194.google.com with SMTP id g62so8520069pfd.12 for ; Mon, 28 Jan 2019 12:37:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=fGSIhGsuy8KE5eNh2GwuDKhLEjwZRhsrs2XbdkTh3xk=; b=LwCAcRFvzoGPgmomIpvgRyctu5guV424HBUSRPWXMk1ndy4BRz5PfFJMn3Icf2NiXG VVBGOjnz0M4TyShU5Gg2bd8pb65WA2mBMQBzEdswIqmWWUVG0p0l7oI3qu1QAec6TcCz vBIAZn0wFIJFBbGu+t8YRxN+xhmTvSIGGg/zJ2YDFrohJhVpWPbk0/QVW8JBm/ZV3VMj xfG2vgP1PjLsjgtUPqqwVrMoY3vQ6C5E4EamoA7jng18nsMzdkJp+S7iHzhxn70WZPUn 7AzXUkGtSkxlOrmAZazKbzUN16sUDdWdozUWo2tTLfYR6aSy8GTuuBaN4ElUtjCpriUq r9+A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=fGSIhGsuy8KE5eNh2GwuDKhLEjwZRhsrs2XbdkTh3xk=; b=U3FXagaq1DiR0bfextIDO9YsyMznw2u8BLOVdA9XvSVmmLuC9iklpozH8lNx3BDbpO D4hSysBAOJ4rdhmXr9Z/qy915YrheX0zqEsp214MShpr8uVp9KjmJX4GIhULUNSTQqU/ LeQ5nilwsU04BJLnDTmQCAbdzKRyd9LcLWYC3WrYJbBTadnA8+mTPytuqtD4S2zw2hX8 wgisqHc3Jl/6iZZn+YsHow4UlCW3uiM0wSLNN8uysYG6+C1Z+B1L5vSaxd8a4j6CK8hW atZCjzUXt3Ye9KhSEbZcbZswarsbfw4Yd7dkIl0cX9u7Ygghc13EipD37g50jZ58waBC aYGQ== X-Gm-Message-State: AJcUukcFHNdWRqdmhXEYIBLaj8NP5RrwwVBWP2mCXX1D35pHyGYIfhWJ 9zQvkJb95rYlOT97k5rAEM7LsSPu X-Google-Smtp-Source: ALg8bN58SpbQr6M125I7KGek/2W3USt6nJwuRYgNVgZMrjgMRWeMqT46jm+2gi/uLF7sCPp3ouMPoA== X-Received: by 2002:a62:7086:: with SMTP id l128mr23176254pfc.68.1548707866442; Mon, 28 Jan 2019 12:37:46 -0800 (PST) Received: from ajayg.nvidia.com (searspoint.nvidia.com. [216.228.112.21]) by smtp.gmail.com with ESMTPSA id t3sm34628047pgv.31.2019.01.28.12.37.45 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 28 Jan 2019 12:37:45 -0800 (PST) From: Ajay Gupta X-Google-Original-From: Ajay Gupta To: heikki.krogerus@linux.intel.com Cc: linux-usb@vger.kernel.org, Ajay Gupta Subject: [PATCH v2 5/6] usb: typec: ucsi: add fw update needed check Date: Mon, 28 Jan 2019 12:37:30 -0800 Message-Id: <20190128203731.12681-6-ajayg@nvidia.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190128203731.12681-1-ajayg@nvidia.com> References: <20190128203731.12681-1-ajayg@nvidia.com> X-NVConfidentiality: public Sender: linux-usb-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-usb@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Here 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. Signed-off-by: Ajay Gupta --- Changes from v1: - Updated commit message - Squashed patch 6/7 of v1 into it - Dropped dev_dbg prints - Dropped uses of bitfield min/maj drivers/usb/typec/ucsi/ucsi_ccg.c | 153 ++++++++++++++++++++++++++++++ 1 file changed, 153 insertions(+) diff --git a/drivers/usb/typec/ucsi/ucsi_ccg.c b/drivers/usb/typec/ucsi/ucsi_ccg.c index b1fadec9172b..9fbd7f2c04fb 100644 --- a/drivers/usb/typec/ucsi/ucsi_ccg.c +++ b/drivers/usb/typec/ucsi/ucsi_ccg.c @@ -9,6 +9,7 @@ */ #include #include +#include #include #include #include @@ -68,6 +69,29 @@ #define FW2_METADATA_ROW 0x1FE #define FW_CFG_TABLE_SIG_SIZE 256 +static int secondary_fw_min_ver = 41; + +enum enum_fw_mode { + BOOT, /* bootloader */ + FW1, /* FW partition-1 (contains secondary fw) */ + FW2, /* FW partition-2 (contains primary fw) */ + FW_INVALID, +}; + +enum enum_flash_mode { + SECONDARY_BL, /* update secondary using bootloader */ + PRIMARY, /* update primary using secondary */ + SECONDARY, /* update secondary using primary */ + FLASH_NOT_NEEDED, /* update not required */ + FLASH_INVALID, +}; + +static const char * const ccg_fw_names[] = { + /* 0 */ "ccg_boot.cyacd", + /* 1 */ "ccg_primary.cyacd", + /* 2 */ "ccg_secondary.cyacd" +}; + struct ccg_dev_info { #define CCG_DEVINFO_FWMODE_SHIFT (0) #define CCG_DEVINFO_FWMODE_MASK (0x3 << CCG_DEVINFO_FWMODE_SHIFT) @@ -94,6 +118,20 @@ struct version_info { struct version_format app; }; +struct fw_config_table { + u32 identity; + u16 table_size; + u8 fwct_version; + u8 is_key_change; + u8 guid[16]; + struct version_format base; + struct version_format app; + u8 primary_fw_digest[32]; + u32 key_exp_length; + u8 key_modulus[256]; + u8 key_exp[4]; +}; + /* CCGx response codes */ enum ccg_resp_code { CMD_NO_RESP = 0x00, @@ -709,6 +747,121 @@ static int ccg_cmd_validate_fw(struct ucsi_ccg *uc, unsigned int fwid) return 0; } +static bool ccg_check_vendor_version(struct ucsi_ccg *uc, + struct version_format *app, + struct fw_config_table *fw_cfg) +{ + struct device *dev = uc->dev; + + /* Check if the fw build is for supported vendors. + * Add all supported vendors here. + */ + if (app->build != (('n' << 8) | 'v')) { + dev_info(dev, "current fw is not from supported vendor\n"); + return false; + } + + /* Check if the new fw build is for supported vendors + * Add all supported vendors here. + */ + if (fw_cfg->app.build != (('n' << 8) | 'v')) { + dev_info(dev, "new fw is not from supported vendor\n"); + return false; + } + return true; +} + +static bool ccg_check_fw_version(struct ucsi_ccg *uc, const char *fw_name, + struct version_format *app) +{ + const struct firmware *fw = NULL; + struct device *dev = uc->dev; + struct fw_config_table fw_cfg; + u32 cur_version, new_version; + bool is_later = false; + + if (request_firmware(&fw, fw_name, dev) != 0) { + dev_err(dev, "error: Failed to open cyacd file %s\n", fw_name); + return false; + } + + /* + * check if signed fw + * last part of fw image is fw cfg table and signature + */ + if (fw->size < sizeof(fw_cfg) + FW_CFG_TABLE_SIG_SIZE) + goto not_signed_fw; + + memcpy((uint8_t *)&fw_cfg, fw->data + fw->size - + sizeof(fw_cfg) - FW_CFG_TABLE_SIG_SIZE, sizeof(fw_cfg)); + + if (fw_cfg.identity != ('F' | 'W' << 8 | 'C' << 16 | 'T' << 24)) { + dev_info(dev, "not a signed image\n"); + goto not_signed_fw; + } + + /* compare input version with FWCT version */ + cur_version = app->build | app->patch << 16 | app->ver << 24; + + new_version = fw_cfg.app.build | fw_cfg.app.patch << 16 | + fw_cfg.app.ver << 24; + + if (!ccg_check_vendor_version(uc, app, &fw_cfg)) + goto not_supported_version; + + if (new_version > cur_version) + is_later = true; + +not_supported_version: +not_signed_fw: + release_firmware(fw); + return is_later; +} + +static int ccg_fw_update_needed(struct ucsi_ccg *uc, + enum enum_flash_mode *mode) +{ + struct device *dev = uc->dev; + int err; + struct version_info version[3]; + + err = ccg_read(uc, CCGX_RAB_DEVICE_MODE, (u8 *)(&uc->info), + sizeof(uc->info)); + if (err) { + dev_err(dev, "read device mode failed\n"); + return err; + } + + err = ccg_read(uc, CCGX_RAB_READ_ALL_VER, (u8 *)version, + sizeof(version)); + if (err) { + dev_err(dev, "read device mode failed\n"); + return err; + } + + if (memcmp(&version[FW1], "\0\0\0\0\0\0\0\0", + sizeof(struct version_info)) == 0) { + dev_info(dev, "secondary fw is not flashed\n"); + *mode = SECONDARY_BL; + } else if (version[FW1].base.build < secondary_fw_min_ver) { + dev_info(dev, "secondary fw version is too low (< %d)\n", + secondary_fw_min_ver); + *mode = SECONDARY; + } else if (memcmp(&version[FW2], "\0\0\0\0\0\0\0\0", + sizeof(struct version_info)) == 0) { + dev_info(dev, "primary fw is not flashed\n"); + *mode = PRIMARY; + } else if (ccg_check_fw_version(uc, ccg_fw_names[PRIMARY], + &version[FW2].app)) { + dev_info(dev, "found primary fw with later version\n"); + *mode = PRIMARY; + } else { + dev_info(dev, "secondary and primary fw are the latest\n"); + *mode = FLASH_NOT_NEEDED; + } + return 0; +} + static int ucsi_ccg_probe(struct i2c_client *client, const struct i2c_device_id *id) { From patchwork Mon Jan 28 20:37:31 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ajay Gupta X-Patchwork-Id: 10784713 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 037E0922 for ; Mon, 28 Jan 2019 20:37:51 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id E46DA2C263 for ; Mon, 28 Jan 2019 20:37:50 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id D85742C34A; Mon, 28 Jan 2019 20:37:50 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 208F52C263 for ; Mon, 28 Jan 2019 20:37:50 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727750AbfA1Uht (ORCPT ); Mon, 28 Jan 2019 15:37:49 -0500 Received: from mail-pg1-f196.google.com ([209.85.215.196]:38466 "EHLO mail-pg1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727811AbfA1Uhs (ORCPT ); Mon, 28 Jan 2019 15:37:48 -0500 Received: by mail-pg1-f196.google.com with SMTP id g189so7715911pgc.5 for ; Mon, 28 Jan 2019 12:37:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=J112qjJIDZDT1+5/QECJ0TZvzPEJe/r3r4k5C4viEqg=; b=IkE9qA0pPzeA9wuvedTJLEb6iMOFyuCAi9Cx0YPcSZRtWvjkELtvOcQuheixiy7OFE agxTImOx0IE6IhhettbtGU6ywwSoapfw+PLCjb6gPd8VpXaHo29Fb3GoK2zqcZFDVYda yp3M9RSsd84FPKZBOYxaqBBYgg/bfRNDdan+QDl041VpCeXh1hkauKwXU9hiiU5eDJim pwyK551YRTVASM9b07RLhb9WzwK0Gknp3ZZGrrzg95wotI9lp+4GFAL9If1jK+lEwcUf HrGeDIuR7dZx1wVJvU1yg49wS3EJJtWR/72z1MJw0MmbuFFjbjpec+PheSDcgNiM+lYZ LO5w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=J112qjJIDZDT1+5/QECJ0TZvzPEJe/r3r4k5C4viEqg=; b=JclA1NZfXVnC6In4I4bDY/fL2pl/F5gTUdD/d9kYMCbDacIaZEOTCWGPQUilQcIxZB nlgbm76LuVxpsZtFO9OZrb80t2RHoIVQhEDhDp6AefmSJi20940nJNDFSjDTlJLBWmWl UUTmCagH6DE1cEU3kXVIgcml4MRJik4OBMAKJd47BDlvi3L3V7JUMjzufcYP8+9e+5WQ k6qGxACaHK+amdGA7QtRblhSl79TLAvapb7XtE3HXT8dK1w7rkk7byEM7G7Ox8aflXkN hhM5THj1/LKhsEggtgGus0iKo0lOiVKgQL85LffahZvg3eUCgItGp8zxbUtC2hZFLY8J apDg== X-Gm-Message-State: AJcUukcO2o3jBa211MNoOeI+9Nin7lBKqUyhZJULy2iskz2j4Ef2mwcA kwQ5L1RhpIIU67C7+T7eYl8= X-Google-Smtp-Source: ALg8bN64i9I48VFBsp2Os64fngKRozXJBvx4ZJ78XDixyp2m13cTN0HqqVbxfv8vdFEYoWomkSAglA== X-Received: by 2002:a62:4851:: with SMTP id v78mr23516416pfa.97.1548707867593; Mon, 28 Jan 2019 12:37:47 -0800 (PST) Received: from ajayg.nvidia.com (searspoint.nvidia.com. [216.228.112.21]) by smtp.gmail.com with ESMTPSA id t3sm34628047pgv.31.2019.01.28.12.37.46 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 28 Jan 2019 12:37:46 -0800 (PST) From: Ajay Gupta X-Google-Original-From: Ajay Gupta To: heikki.krogerus@linux.intel.com Cc: linux-usb@vger.kernel.org, Ajay Gupta Subject: [PATCH v2 6/6] usb: typec: ucsi: add firmware flashing support Date: Mon, 28 Jan 2019 12:37:31 -0800 Message-Id: <20190128203731.12681-7-ajayg@nvidia.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190128203731.12681-1-ajayg@nvidia.com> References: <20190128203731.12681-1-ajayg@nvidia.com> X-NVConfidentiality: public Sender: linux-usb-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-usb@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP 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. Signed-off-by: Ajay Gupta --- Changes from v1: - Updated commit message - Dropped a few dev_dbg prints drivers/usb/typec/ucsi/ucsi_ccg.c | 284 ++++++++++++++++++++++++++++++ 1 file changed, 284 insertions(+) diff --git a/drivers/usb/typec/ucsi/ucsi_ccg.c b/drivers/usb/typec/ucsi/ucsi_ccg.c index 9fbd7f2c04fb..619fbb42615f 100644 --- a/drivers/usb/typec/ucsi/ucsi_ccg.c +++ b/drivers/usb/typec/ucsi/ucsi_ccg.c @@ -239,6 +239,8 @@ struct ucsi_ccg { struct ccg_resp dev_resp; u8 cmd_resp; int port_num; + int irq; + struct work_struct work; struct mutex lock; /* to sync between user and driver thread */ }; @@ -862,6 +864,277 @@ static int ccg_fw_update_needed(struct ucsi_ccg *uc, return 0; } +static int do_flash(struct ucsi_ccg *uc, enum enum_flash_mode mode) +{ + struct device *dev = uc->dev; + const struct firmware *fw = NULL; + const char *p, *s; + const char *eof; + int err, row, len, line_sz, line_cnt = 0; + unsigned long start_time = jiffies; + struct fw_config_table fw_cfg; + u8 fw_cfg_sig[FW_CFG_TABLE_SIG_SIZE]; + u8 *wr_buf; + + err = request_firmware(&fw, ccg_fw_names[mode], dev); + if (err) { + dev_err(dev, "request %s failed err=%d\n", + ccg_fw_names[mode], err); + return err; + } + + if (((uc->info.mode & CCG_DEVINFO_FWMODE_MASK) >> + CCG_DEVINFO_FWMODE_SHIFT) == FW2) { + err = ccg_cmd_port_control(uc, false); + if (err < 0) + goto release_fw; + err = ccg_cmd_jump_boot_mode(uc, 0); + if (err < 0) + goto release_fw; + } + + eof = fw->data + fw->size; + + /* + * check if signed fw + * last part of fw image is fw cfg table and signature + */ + if (fw->size < sizeof(fw_cfg) + sizeof(fw_cfg_sig)) + goto not_signed_fw; + + memcpy((uint8_t *)&fw_cfg, fw->data + fw->size - + sizeof(fw_cfg) - sizeof(fw_cfg_sig), sizeof(fw_cfg)); + + if (fw_cfg.identity != ('F' | ('W' << 8) | ('C' << 16) | ('T' << 24))) { + dev_info(dev, "not a signed image\n"); + goto not_signed_fw; + } + eof = fw->data + fw->size - sizeof(fw_cfg) - sizeof(fw_cfg_sig); + + memcpy((uint8_t *)&fw_cfg_sig, + fw->data + fw->size - sizeof(fw_cfg_sig), sizeof(fw_cfg_sig)); + + /* flash fw config table and signature first */ + err = ccg_cmd_write_flash_row(uc, 0, (u8 *)&fw_cfg, + FLASH_FWCT1_WR_CMD); + if (err) + goto release_fw; + + err = ccg_cmd_write_flash_row(uc, 0, (u8 *)&fw_cfg + CCG4_ROW_SIZE, + FLASH_FWCT2_WR_CMD); + if (err) + goto release_fw; + + err = ccg_cmd_write_flash_row(uc, 0, &fw_cfg_sig, + FLASH_FWCT_SIG_WR_CMD); + if (err) + goto release_fw; + +not_signed_fw: + wr_buf = kzalloc(CCG4_ROW_SIZE + 4, GFP_KERNEL); + if (!wr_buf) + return -ENOMEM; + + err = ccg_cmd_enter_flashing(uc); + if (err) + goto release_mem; + + /***************************************************************** + * CCG firmware image (.cyacd) file line format + * + * :00rrrrllll[dd....]cc/r/n + * + * :00 header + * rrrr is row number to flash (4 char) + * llll is data len to flash (4 char) + * dd is a data field represents one byte of data (512 char) + * cc is checksum (2 char) + * \r\n newline + * + * Total length: 3 + 4 + 4 + 512 + 2 + 2 = 527 + * + *****************************************************************/ + + p = strnchr(fw->data, fw->size, ':'); + while (p < eof) { + s = strnchr(p + 1, eof - p - 1, ':'); + + if (!s) + s = eof; + + line_sz = s - p; + + if (line_sz != CYACD_LINE_SIZE) { + dev_err(dev, "Bad FW format line_sz=%d\n", line_sz); + err = -EINVAL; + goto release_mem; + } + + if (hex2bin(wr_buf, p + 3, CCG4_ROW_SIZE + 4)) { + err = -EINVAL; + goto release_mem; + } + + row = (wr_buf[0] << 8) + wr_buf[1]; + len = (wr_buf[2] << 8) + wr_buf[3]; + + if (len != CCG4_ROW_SIZE) { + err = -EINVAL; + goto release_mem; + } + + err = ccg_cmd_write_flash_row(uc, row, wr_buf + 4, + FLASH_WR_CMD); + if (err) + goto release_mem; + + line_cnt++; + p = s; + } + + dev_info(dev, "total %d row flashed. time: %dms\n", + line_cnt, jiffies_to_msecs(jiffies - start_time)); + + err = ccg_cmd_validate_fw(uc, (mode == PRIMARY) ? FW2 : FW1); + if (err) + dev_err(dev, "%s validation failed err=%d\n", + (mode == PRIMARY) ? "FW2" : "FW1", err); + else + dev_info(dev, "%s validated\n", + (mode == PRIMARY) ? "FW2" : "FW1"); + + err = ccg_cmd_port_control(uc, false); + if (err < 0) + goto release_mem; + + err = ccg_cmd_reset(uc, mode == SECONDARY); + if (err < 0) + goto release_mem; + + err = ccg_cmd_port_control(uc, true); + if (err < 0) + goto release_mem; + +release_mem: + kfree(wr_buf); + +release_fw: + release_firmware(fw); + return err; +} + +/******************************************************************************* + * CCG4 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. + ******************************************************************************/ +static int ccg_fw_update(struct ucsi_ccg *uc, enum enum_flash_mode flash_mode) +{ + int err; + + while (flash_mode != FLASH_NOT_NEEDED) { + err = do_flash(uc, flash_mode); + if (err < 0) + return err; + err = ccg_fw_update_needed(uc, &flash_mode); + if (err < 0) + return err; + } + dev_info(uc->dev, "CCG FW update successful\n"); + + return err; +} + +static int ccg_restart(struct ucsi_ccg *uc) +{ + struct device *dev = uc->dev; + int status; + + status = ucsi_ccg_init(uc); + if (status < 0) { + dev_err(dev, "ucsi_ccg_start fail, err=%d\n", status); + return status; + } + + status = devm_request_threaded_irq(dev, uc->irq, NULL, ccg_irq_handler, + IRQF_ONESHOT | IRQF_TRIGGER_HIGH, + dev_name(dev), uc); + if (status < 0) { + dev_err(dev, "request_threaded_irq failed - %d\n", status); + return status; + } + + uc->ucsi = ucsi_register_ppm(dev, &uc->ppm); + if (IS_ERR(uc->ucsi)) { + dev_err(uc->dev, "ucsi_register_ppm failed\n"); + return PTR_ERR(uc->ucsi); + } + + return 0; +} + +static void ccg_update_firmware(struct work_struct *work) +{ + struct ucsi_ccg *uc = container_of(work, struct ucsi_ccg, work); + enum enum_flash_mode flash_mode; + int status; + + status = ccg_fw_update_needed(uc, &flash_mode); + if (status < 0) + return; + + if (flash_mode != FLASH_NOT_NEEDED) { + ucsi_unregister_ppm(uc->ucsi); + if (uc->irq) + devm_free_irq(uc->dev, uc->irq, uc); + + ccg_fw_update(uc, flash_mode); + ccg_restart(uc); + } +} + +static ssize_t do_flash_store(struct device *dev, + struct device_attribute *attr, + const char *buf, size_t n) +{ + struct i2c_client *i2c_cl = to_i2c_client(dev); + struct ucsi_ccg *uc = i2c_get_clientdata(i2c_cl); + unsigned int mode; + + if (kstrtouint(buf, 10, &mode)) + return -EINVAL; + + if (mode) + schedule_work(&uc->work); + + return n; +} + +static ssize_t do_flash_show(struct device *dev, + struct device_attribute *attr, char *buf) +{ + return sprintf(buf, "[Usage]\n" + "1) copy ccg_boot.cyacd /lib/firmware/\n" + "2) copy ccg_1.cyacd /lib/firmware/\n" + "3) copy ccg_2.cyacd /lib/firmware/\n" + "4) echo 1 > do_flash\n" + "5) Remove all TypeC cable/adapter after flashing\n"); +} + +static DEVICE_ATTR_RW(do_flash); + +static struct attribute * +ucsi_ccg_sysfs_attrs[] = { + &dev_attr_do_flash.attr, + NULL, +}; + +static struct attribute_group +ucsi_ccg_attr_group = { + .attrs = ucsi_ccg_sysfs_attrs, +}; + static int ucsi_ccg_probe(struct i2c_client *client, const struct i2c_device_id *id) { @@ -882,6 +1155,8 @@ static int ucsi_ccg_probe(struct i2c_client *client, uc->ppm.sync = ucsi_ccg_sync; uc->dev = dev; uc->client = client; + mutex_init(&uc->lock); + INIT_WORK(&uc->work, ccg_update_firmware); /* reset ccg device and initialize ucsi */ status = ucsi_ccg_init(uc); @@ -911,6 +1186,8 @@ static int ucsi_ccg_probe(struct i2c_client *client, return status; } + uc->irq = client->irq; + uc->ucsi = ucsi_register_ppm(dev, &uc->ppm); if (IS_ERR(uc->ucsi)) { dev_err(uc->dev, "ucsi_register_ppm failed\n"); @@ -927,6 +1204,11 @@ static int ucsi_ccg_probe(struct i2c_client *client, } i2c_set_clientdata(client, uc); + + status = sysfs_create_group(&uc->dev->kobj, &ucsi_ccg_attr_group); + if (status) + dev_err(uc->dev, "cannot create sysfs group: %d\n", status); + return 0; } @@ -934,7 +1216,9 @@ static int ucsi_ccg_remove(struct i2c_client *client) { struct ucsi_ccg *uc = i2c_get_clientdata(client); + cancel_work_sync(&uc->work); ucsi_unregister_ppm(uc->ucsi); + sysfs_remove_group(&uc->dev->kobj, &ucsi_ccg_attr_group); return 0; }