summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorli feng <li1.feng@intel.com>2021-04-29 21:26:17 -0700
committerCommit Bot <commit-bot@chromium.org>2021-05-01 15:30:47 +0000
commitfb0108e6173072149c43f8ddbfd9a8488a4604dd (patch)
tree381b9f788e9679cf9a36760f8bfddab43de16d3e
parent633d179e08a8deab525ed9ae2aa1d1cd9f68544d (diff)
downloadchrome-ec-fb0108e6173072149c43f8ddbfd9a8488a4604dd.tar.gz
tcpc: Add common routine to check power
This comment is from CL:1123314: We do not want to allow depthcharge to suspend the PD port for a firmware upgrade if there is not enough battery power to last during the full firmware upgrade. A common routine is created to check power: pd_firmware_upgrade_check_power_readiness() in usb_common.c. Firmware upgrade which needs to suspend PD port should check this function to make sure there is enough power. BUG=none BRANCH=none TEST=make -j buildall Signed-off-by: li feng <li1.feng@intel.com> Change-Id: I47910a4d82da9b569c297ad2cd399a2faf4cf98a Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/2862556 Reviewed-by: Keith Short <keithshort@chromium.org> Commit-Queue: Keith Short <keithshort@chromium.org> (cherry picked from commit 01ef01d06be623269cf286c580d39e1f0e3a78d4) Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/2864741 Tested-by: Keith Short <keithshort@chromium.org>
-rw-r--r--common/usb_common.c41
-rw-r--r--common/usb_pd_host_cmd.c44
-rw-r--r--include/usb_common.h11
3 files changed, 54 insertions, 42 deletions
diff --git a/common/usb_common.c b/common/usb_common.c
index fbeb48c8d1..a24a063d18 100644
--- a/common/usb_common.c
+++ b/common/usb_common.c
@@ -38,6 +38,14 @@
#define CPRINTF(format, args...)
#endif
+/*
+ * If we are trying to upgrade PD firmwares (TCPC chips, retimer, etc), we
+ * need to ensure the battery has enough charge for this process. 100mAh
+ * is about 5% of most batteries, and it should be enough charge to get us
+ * through the EC jump to RW and PD upgrade.
+ */
+#define MIN_BATTERY_FOR_PD_UPGRADE_MAH 100 /* mAH */
+
__overridable void board_vbus_present_change(void)
{
}
@@ -122,6 +130,39 @@ int remote_flashing(int argc, char **argv)
}
#endif /* defined(CONFIG_CMD_PD) && defined(CONFIG_CMD_PD_FLASH) */
+bool pd_firmware_upgrade_check_power_readiness(int port)
+{
+ if (IS_ENABLED(HAS_TASK_CHARGER)) {
+ struct batt_params batt = { 0 };
+ /*
+ * Cannot rely on the EC's active charger data as the
+ * EC may just rebooted into RW and has not necessarily
+ * picked the active charger yet. Charger task may not
+ * initialized, so check battery directly.
+ * Prevent the upgrade if the battery doesn't have enough
+ * charge to finish the upgrade.
+ */
+ battery_get_params(&batt);
+ if (batt.flags & BATT_FLAG_BAD_REMAINING_CAPACITY ||
+ batt.remaining_capacity <
+ MIN_BATTERY_FOR_PD_UPGRADE_MAH) {
+ CPRINTS("C%d: Cannot suspend for upgrade, not "
+ "enough battery (%dmAh)!",
+ port, batt.remaining_capacity);
+ return false;
+ }
+ } else {
+ /* VBUS is present on the port (it is either a
+ * source or sink) to provide power, so don't allow
+ * PD firmware upgrade on the port.
+ */
+ if (pd_is_vbus_present(port))
+ return false;
+ }
+
+ return true;
+}
+
int usb_get_battery_soc(void)
{
#if defined(CONFIG_CHARGER)
diff --git a/common/usb_pd_host_cmd.c b/common/usb_pd_host_cmd.c
index e35f6241f8..c800c20478 100644
--- a/common/usb_pd_host_cmd.c
+++ b/common/usb_pd_host_cmd.c
@@ -15,19 +15,11 @@
#include "host_command.h"
#include "mkbp_event.h"
#include "tcpm/tcpm.h"
+#include "usb_common.h"
#include "usb_mux.h"
#include "usb_pd_tcpm.h"
#include "usb_pd.h"
-
#ifdef CONFIG_COMMON_RUNTIME
-/*
- * If we are trying to upgrade the TCPC port that is supplying power, then we
- * need to ensure that the battery has enough charge for the upgrade. 100mAh
- * is about 5% of most batteries, and it should be enough charge to get us
- * through the EC jump to RW and PD upgrade.
- */
-#define MIN_BATTERY_FOR_TCPC_UPGRADE_MAH 100 /* mAH */
-
struct ec_params_usb_pd_rw_hash_entry rw_hash_table[RW_HASH_ENTRIES];
#define CPRINTF(format, args...) cprintf(CC_USBPD, format, ## args)
@@ -534,40 +526,8 @@ static enum ec_status pd_control(struct host_cmd_handler_args *args)
return EC_RES_ACCESS_DENIED;
if (cmd->subcmd == PD_SUSPEND) {
- /*
- * The AP is requesting to suspend PD traffic on the EC so it
- * can perform a firmware upgrade. If Vbus is present on the
- * connector (it is either a source or sink), then we will
- * prevent the upgrade if there is not enough battery to finish
- * the upgrade. We cannot rely on the EC's active charger data
- * as the EC just rebooted into RW and has not necessarily
- * picked the active charger yet.
- */
-#ifdef HAS_TASK_CHARGER
- if (pd_is_vbus_present(cmd->chip)) {
- struct batt_params batt = { 0 };
- /*
- * The charger task has not re-initialized, so we need
- * to ask the battery directly.
- */
- battery_get_params(&batt);
- if (batt.remaining_capacity <
- MIN_BATTERY_FOR_TCPC_UPGRADE_MAH ||
- batt.flags & BATT_FLAG_BAD_REMAINING_CAPACITY) {
- CPRINTS("C%d: Cannot suspend for upgrade, not "
- "enough battery (%dmAh)!",
- cmd->chip, batt.remaining_capacity);
- return EC_RES_BUSY;
- }
- }
-#else
- if (pd_is_vbus_present(cmd->chip)) {
- CPRINTS("C%d: Cannot suspend for upgrade, Vbus "
- "present!",
- cmd->chip);
+ if (!pd_firmware_upgrade_check_power_readiness(cmd->chip))
return EC_RES_BUSY;
- }
-#endif
enable = 0;
} else if (cmd->subcmd == PD_RESUME) {
enable = 1;
diff --git a/include/usb_common.h b/include/usb_common.h
index 00440390df..01ef2c036e 100644
--- a/include/usb_common.h
+++ b/include/usb_common.h
@@ -81,6 +81,17 @@ int hex8tou32(char *str, uint32_t *val);
*/
int remote_flashing(int argc, char **argv);
+/*
+ * When AP requests to suspend PD traffic on the EC so it can do
+ * firmware upgrade (retimer firmware, or TCPC chips firmware),
+ * it calls this function to check if power is ready for performing
+ * the upgrade.
+ * @param port USB-C port number
+ * @dreturn true - power is ready
+ * false - power is not ready
+ */
+bool pd_firmware_upgrade_check_power_readiness(int port);
+
/* Returns the battery percentage [0-100] of the system. */
int usb_get_battery_soc(void);