public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Heikki Krogerus <heikki.krogerus@linux.intel.com>
To: Javier Carrasco <javier.carrasco@wolfvision.net>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH v2 4/4] usb: typec: tipd: add patch update support for tps6598x
Date: Tue, 19 Dec 2023 15:24:18 +0200	[thread overview]
Message-ID: <ZYGGC4sraG6akqPd@kuha.fi.intel.com> (raw)
In-Reply-To: <20231207-tps6598x_update-v2-4-f3cfcde6d890@wolfvision.net>

On Thu, Dec 14, 2023 at 05:29:12PM +0100, Javier Carrasco wrote:
> The TPS6598x PD controller supports firmware updates that can be loaded
> either from an external flash memory or a host using the device's I2C
> host interface. This patch implements the second approach, which is
> especially relevant if no flash memory is available.
> 
> In order to make patch bundle updates, a series of tasks (special
> commands) must be sent to the device as it is documented in the
> TPS65987DDH and TPS65988DH Host Interface Technical Reference Manual[1],
> section 4.11 (Patch Bundle Update Tasks).
> 
> The update sequence is as follows:
> 1. PTCs - Start Patch Load Sequence: the proposed approach includes
>    device and application configuration data.
> 2. PTCd - Patch Download: 64-byte data chunks must be sent until the end
>    of the firmware file is reached (the last chunk may be shorter).
> 3. PTCc - Patch Data Transfer Complete: ends the patch loading sequence.
> 
> After this sequence and if no errors occurred, the device will change
> its mode to 'APP' after SETUP_MS milliseconds, and then it will be ready
> for normal operation.
> 
> [1] https://www.ti.com/lit/ug/slvubh2b/slvubh2b.pdf?ts=1697623299919&ref_url=https%253A%252F%252Fwww.ti.com%252Fproduct%252FTPS65987D
> 
> Signed-off-by: Javier Carrasco <javier.carrasco@wolfvision.net>

Reviewed-by: Heikki Krogerus <heikki.krogerus@linux.intel.com>

> ---
>  drivers/usb/typec/tipd/core.c     | 68 ++++++++++++++++++++++++++++++++++++++-
>  drivers/usb/typec/tipd/tps6598x.h | 18 +++++++++++
>  2 files changed, 85 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/usb/typec/tipd/core.c b/drivers/usb/typec/tipd/core.c
> index 7f4bbc0629b0..a956eb976906 100644
> --- a/drivers/usb/typec/tipd/core.c
> +++ b/drivers/usb/typec/tipd/core.c
> @@ -1125,6 +1125,71 @@ static int tps25750_apply_patch(struct tps6598x *tps)
>  	return 0;
>  };
>  
> +static int tps6598x_apply_patch(struct tps6598x *tps)
> +{
> +	u8 in = TPS_PTCS_CONTENT_DEV | TPS_PTCS_CONTENT_APP;
> +	u8 out[TPS_MAX_LEN] = {0};
> +	size_t in_len = sizeof(in);
> +	size_t copied_bytes = 0;
> +	size_t bytes_left;
> +	const struct firmware *fw;
> +	const char *firmware_name;
> +	int ret;
> +
> +	ret = device_property_read_string(tps->dev, "firmware-name",
> +					  &firmware_name);
> +	if (ret)
> +		return ret;
> +
> +	ret = tps_request_firmware(tps, &fw);
> +	if (ret)
> +		return ret;
> +
> +	ret = tps6598x_exec_cmd(tps, "PTCs", in_len, &in,
> +				TPS_PTCS_OUT_BYTES, out);
> +	if (ret || out[TPS_PTCS_STATUS] == TPS_PTCS_STATUS_FAIL) {
> +		if (!ret)
> +			ret = -EBUSY;
> +		dev_err(tps->dev, "Update start failed (%d)\n", ret);
> +		goto release_fw;
> +	}
> +
> +	bytes_left = fw->size;
> +	while (bytes_left) {
> +		if (bytes_left < TPS_MAX_LEN)
> +			in_len = bytes_left;
> +		else
> +			in_len = TPS_MAX_LEN;
> +		ret = tps6598x_exec_cmd(tps, "PTCd", in_len,
> +					fw->data + copied_bytes,
> +					TPS_PTCD_OUT_BYTES, out);
> +		if (ret || out[TPS_PTCD_TRANSFER_STATUS] ||
> +		    out[TPS_PTCD_LOADING_STATE] == TPS_PTCD_LOAD_ERR) {
> +			if (!ret)
> +				ret = -EBUSY;
> +			dev_err(tps->dev, "Patch download failed (%d)\n", ret);
> +			goto release_fw;
> +		}
> +		copied_bytes += in_len;
> +		bytes_left -= in_len;
> +	}
> +
> +	ret = tps6598x_exec_cmd(tps, "PTCc", 0, NULL, TPS_PTCC_OUT_BYTES, out);
> +	if (ret || out[TPS_PTCC_DEV] || out[TPS_PTCC_APP]) {
> +		if (!ret)
> +			ret = -EBUSY;
> +		dev_err(tps->dev, "Update completion failed (%d)\n", ret);
> +		goto release_fw;
> +	}
> +	msleep(TPS_SETUP_MS);
> +	dev_info(tps->dev, "Firmware update succeeded\n");
> +
> +release_fw:
> +	release_firmware(fw);
> +
> +	return ret;
> +};
> +
>  static int cd321x_init(struct tps6598x *tps)
>  {
>  	return 0;
> @@ -1150,7 +1215,7 @@ static int tps25750_init(struct tps6598x *tps)
>  
>  static int tps6598x_init(struct tps6598x *tps)
>  {
> -	return 0;
> +	return tps->data->apply_patch(tps);
>  }
>  
>  static int cd321x_reset(struct tps6598x *tps)
> @@ -1468,6 +1533,7 @@ static const struct tipd_data tps6598x_data = {
>  	.register_port = tps6598x_register_port,
>  	.trace_power_status = trace_tps6598x_power_status,
>  	.trace_status = trace_tps6598x_status,
> +	.apply_patch = tps6598x_apply_patch,
>  	.init = tps6598x_init,
>  	.reset = tps6598x_reset,
>  };
> diff --git a/drivers/usb/typec/tipd/tps6598x.h b/drivers/usb/typec/tipd/tps6598x.h
> index 01609bf509e4..89b24519463a 100644
> --- a/drivers/usb/typec/tipd/tps6598x.h
> +++ b/drivers/usb/typec/tipd/tps6598x.h
> @@ -235,4 +235,22 @@
>  /* SLEEP CONF REG */
>  #define TPS_SLEEP_CONF_SLEEP_MODE_ALLOWED	BIT(0)
>  
> +/* Start Patch Download Sequence */
> +#define TPS_PTCS_CONTENT_APP			BIT(0)
> +#define TPS_PTCS_CONTENT_DEV			BIT(1)
> +#define TPS_PTCS_OUT_BYTES			4
> +#define TPS_PTCS_STATUS				1
> +
> +#define TPS_PTCS_STATUS_FAIL			0x80
> +/* Patch Download */
> +#define TPS_PTCD_OUT_BYTES			10
> +#define TPS_PTCD_TRANSFER_STATUS		1
> +#define TPS_PTCD_LOADING_STATE			2
> +
> +#define TPS_PTCD_LOAD_ERR			0x09
> +/* Patch Download Complete */
> +#define TPS_PTCC_OUT_BYTES			4
> +#define TPS_PTCC_DEV				2
> +#define TPS_PTCC_APP				3
> +
>  #endif /* __TPS6598X_H__ */
> 
> -- 
> 2.39.2

-- 
heikki

  reply	other threads:[~2023-12-19 13:24 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-12-14 16:29 [PATCH v2 0/4] usb: typec: tipd: add patch update support for tps6598x Javier Carrasco
2023-12-14 16:29 ` [PATCH v2 1/4] usb: typec: tipd: add init and reset functions to tipd_data Javier Carrasco
2023-12-19 11:46   ` Heikki Krogerus
2024-01-04 16:14   ` Roger Quadros
2024-01-04 16:39     ` Javier Carrasco
2023-12-14 16:29 ` [PATCH v2 2/4] usb: typec: tipd: add function to request firmware Javier Carrasco
2023-12-19 11:50   ` Heikki Krogerus
2023-12-14 16:29 ` [PATCH v2 3/4] usb: typec: tipd: declare in_data in as const in exec_cmd functions Javier Carrasco
2023-12-19 11:51   ` Heikki Krogerus
2023-12-14 16:29 ` [PATCH v2 4/4] usb: typec: tipd: add patch update support for tps6598x Javier Carrasco
2023-12-19 13:24   ` Heikki Krogerus [this message]
2024-01-04 14:20 ` [PATCH v2 0/4] " Jai Luthra
2024-01-04 15:47   ` Jai Luthra
2024-01-04 16:15     ` Roger Quadros
2024-01-04 16:36       ` Javier Carrasco
2024-01-04 17:08         ` Roger Quadros
2024-01-04 17:15           ` Javier Carrasco
2024-01-04 18:52       ` Dhruva Gole
2024-01-04 20:15         ` Javier Carrasco
2024-01-05  7:57           ` Roger Quadros
2024-01-04 16:25     ` Javier Carrasco
2024-01-05  8:12       ` Jai Luthra
2024-01-05  9:49         ` Javier Carrasco
2024-01-05 10:10           ` Jai Luthra
2024-01-05 16:40             ` Abdel Alkuor

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ZYGGC4sraG6akqPd@kuha.fi.intel.com \
    --to=heikki.krogerus@linux.intel.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=javier.carrasco@wolfvision.net \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-usb@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox