usb: typec: tipd: add patch update support for tps6598x
authorJavier Carrasco <javier.carrasco@wolfvision.net>
Thu, 14 Dec 2023 16:29:12 +0000 (17:29 +0100)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Tue, 2 Jan 2024 13:41:30 +0000 (14:41 +0100)
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>
Link: https://lore.kernel.org/r/20231207-tps6598x_update-v2-4-f3cfcde6d890@wolfvision.net
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
drivers/usb/typec/tipd/core.c
drivers/usb/typec/tipd/tps6598x.h

index 7f4bbc0629b064b48c77f44d8dca2f6a6d4ec90b..a956eb976906a52a4c19b20d0cebe8f1b60468c0 100644 (file)
@@ -1125,6 +1125,71 @@ wait_for_app:
        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,
 };
index 01609bf509e4be7e393ac7eddf651c05ec0cc23d..89b24519463a19bb608b23e620242479dacea5f0 100644 (file)
 /* 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__ */