From cbdbbd39df55ecd582fbcd4f566015bcc2c72770 Mon Sep 17 00:00:00 2001 From: Edward Hill Date: Thu, 5 Dec 2019 13:27:51 -0700 Subject: usbc: Move PPC overcurrent functions to usb_common.c Share single copy of PPC overcurrent functions between TCPMv1 and TCPMv2. BUG=none BRANCH=none TEST=build Change-Id: I70e25e8580f6bbfebe6269552cd186f3bb981ede Signed-off-by: Edward Hill Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/1954305 Reviewed-by: Aseda Aboagye --- common/usb_common.c | 64 +++++++++++++++++++++++++++++++++- common/usb_pd_protocol.c | 51 --------------------------- common/usbc/usb_tc_ctvpd_sm.c | 4 +-- common/usbc/usb_tc_drp_acc_trysrc_sm.c | 56 ++--------------------------- 4 files changed, 67 insertions(+), 108 deletions(-) diff --git a/common/usb_common.c b/common/usb_common.c index a068142ae5..af77e65ebc 100644 --- a/common/usb_common.c +++ b/common/usb_common.c @@ -8,14 +8,22 @@ * and the new (i.e. usb_sm_*) USB-C PD stacks. */ -#include "common.h" +#include "atomic.h" #include "charge_state.h" +#include "common.h" +#include "hooks.h" #include "task.h" #include "usb_common.h" #include "usb_pd.h" #include "usb_pd_tcpm.h" +#include "usbc_ppc.h" #include "util.h" +#ifdef CONFIG_COMMON_RUNTIME +#define CPRINTF(format, args...) cprintf(CC_USBPD, format, ## args) +#define CPRINTS(format, args...) cprints(CC_USBPD, format, ## args) +#endif + int usb_get_battery_soc(void) { #if defined(CONFIG_CHARGER) @@ -379,3 +387,57 @@ enum pd_drp_next_states drp_auto_toggle_next_state( return DRP_TC_DRP_AUTO_TOGGLE; } } + +#ifdef CONFIG_USBC_PPC + +static void pd_send_hard_reset(int port) +{ + task_set_event(PD_PORT_TO_TASK_ID(port), PD_EVENT_SEND_HARD_RESET, 0); +} + +static uint32_t port_oc_reset_req; + +static void re_enable_ports(void) +{ + uint32_t ports = atomic_read_clear(&port_oc_reset_req); + + while (ports) { + int port = __fls(ports); + + ports &= ~BIT(port); + + /* + * Let the board know that the overcurrent is + * over since we're going to attempt re-enabling + * the port. + */ + board_overcurrent_event(port, 0); + + pd_send_hard_reset(port); + /* + * TODO(b/117854867): PD3.0 to send an alert message + * indicating OCP after explicit contract. + */ + } +} +DECLARE_DEFERRED(re_enable_ports); + +void pd_handle_overcurrent(int port) +{ + /* Keep track of the overcurrent events. */ + CPRINTS("C%d: overcurrent!", port); + + if (IS_ENABLED(CONFIG_USB_PD_LOGGING)) + pd_log_event(PD_EVENT_PS_FAULT, PD_LOG_PORT_SIZE(port, 0), + PS_FAULT_OCP, NULL); + + ppc_add_oc_event(port); + /* Let the board specific code know about the OC event. */ + board_overcurrent_event(port, 1); + + /* Wait 1s before trying to re-enable the port. */ + atomic_or(&port_oc_reset_req, BIT(port)); + hook_call_deferred(&re_enable_ports_data, SECOND); +} + +#endif /* CONFIG_USBC_PPC */ diff --git a/common/usb_pd_protocol.c b/common/usb_pd_protocol.c index cee2c5ca0f..ff7d6eb045 100644 --- a/common/usb_pd_protocol.c +++ b/common/usb_pd_protocol.c @@ -5035,57 +5035,6 @@ void pd_update_contract(int port) #endif /* CONFIG_USB_PD_DUAL_ROLE */ -#ifdef CONFIG_USBC_PPC -static void pd_send_hard_reset(int port) -{ - task_set_event(PD_PORT_TO_TASK_ID(port), PD_EVENT_SEND_HARD_RESET, 0); -} - -static uint32_t port_oc_reset_req; - -static void re_enable_ports(void) -{ - uint32_t ports = atomic_read_clear(&port_oc_reset_req); - - while (ports) { - int port = __fls(ports); - - ports &= ~BIT(port); - - /* - * Let the board know that the overcurrent is - * over since we're going to attempt re-enabling - * the port. - */ - board_overcurrent_event(port, 0); - - pd_send_hard_reset(port); - /* - * TODO(b/117854867): PD3.0 to send an alert message - * indicating OCP after explicit contract. - */ - } -} -DECLARE_DEFERRED(re_enable_ports); - -void pd_handle_overcurrent(int port) -{ - /* Keep track of the overcurrent events. */ - CPRINTS("C%d: overcurrent!", port); -#ifdef CONFIG_USB_PD_LOGGING - pd_log_event(PD_EVENT_PS_FAULT, PD_LOG_PORT_SIZE(port, 0), PS_FAULT_OCP, - NULL); -#endif /* defined(CONFIG_USB_PD_LOGGING) */ - ppc_add_oc_event(port); - /* Let the board specific code know about the OC event. */ - board_overcurrent_event(port, 1); - - /* Wait 1s before trying to re-enable the port. */ - atomic_or(&port_oc_reset_req, BIT(port)); - hook_call_deferred(&re_enable_ports_data, SECOND); -} -#endif /* defined(CONFIG_USBC_PPC) */ - static int command_pd(int argc, char **argv) { int port; diff --git a/common/usbc/usb_tc_ctvpd_sm.c b/common/usbc/usb_tc_ctvpd_sm.c index 1747a8c0ab..4d0b6664f0 100644 --- a/common/usbc/usb_tc_ctvpd_sm.c +++ b/common/usbc/usb_tc_ctvpd_sm.c @@ -15,8 +15,8 @@ /* USB Type-C CTVPD module */ #ifdef CONFIG_COMMON_RUNTIME -#define CPRINTF(format, args...) cprintf(CC_HOOK, format, ## args) -#define CPRINTS(format, args...) cprints(CC_HOOK, format, ## args) +#define CPRINTF(format, args...) cprintf(CC_USBPD, format, ## args) +#define CPRINTS(format, args...) cprints(CC_USBPD, format, ## args) #else /* CONFIG_COMMON_RUNTIME */ #define CPRINTF(format, args...) #define CPRINTS(format, args...) diff --git a/common/usbc/usb_tc_drp_acc_trysrc_sm.c b/common/usbc/usb_tc_drp_acc_trysrc_sm.c index 6aa39b520b..a025aad2fb 100644 --- a/common/usbc/usb_tc_drp_acc_trysrc_sm.c +++ b/common/usbc/usb_tc_drp_acc_trysrc_sm.c @@ -26,8 +26,8 @@ */ #ifdef CONFIG_COMMON_RUNTIME -#define CPRINTF(format, args...) cprintf(CC_HOOK, format, ## args) -#define CPRINTS(format, args...) cprints(CC_HOOK, format, ## args) +#define CPRINTF(format, args...) cprintf(CC_USBPD, format, ## args) +#define CPRINTS(format, args...) cprints(CC_USBPD, format, ## args) #else /* CONFIG_COMMON_RUNTIME */ #define CPRINTF(format, args...) #define CPRINTS(format, args...) @@ -1541,58 +1541,6 @@ int tc_is_vconn_src(int port) } #endif -#ifdef CONFIG_USBC_PPC -static void pd_send_hard_reset(int port) -{ - task_set_event(PD_PORT_TO_TASK_ID(port), PD_EVENT_SEND_HARD_RESET, 0); -} - -static uint32_t port_oc_reset_req; - -static void re_enable_ports(void) -{ - uint32_t ports = atomic_read_clear(&port_oc_reset_req); - - while (ports) { - int port = __fls(ports); - - ports &= ~BIT(port); - - /* - * Let the board know that the overcurrent is - * over since we're going to attempt re-enabling - * the port. - */ - board_overcurrent_event(port, 0); - - pd_send_hard_reset(port); - /* - * TODO(b/117854867): PD3.0 to send an alert message - * indicating OCP after explicit contract. - */ - } -} -DECLARE_DEFERRED(re_enable_ports); - -void pd_handle_overcurrent(int port) -{ - /* Keep track of the overcurrent events. */ - CPRINTS("C%d: overcurrent!", port); - - if (IS_ENABLED(CONFIG_USB_PD_LOGGING)) - pd_log_event(PD_EVENT_PS_FAULT, PD_LOG_PORT_SIZE(port, 0), - PS_FAULT_OCP, NULL); - - ppc_add_oc_event(port); - /* Let the board specific code know about the OC event. */ - board_overcurrent_event(port, 1); - - /* Wait 1s before trying to re-enable the port. */ - atomic_or(&port_oc_reset_req, BIT(port)); - hook_call_deferred(&re_enable_ports_data, SECOND); -} -#endif /* defined(CONFIG_USBC_PPC) */ - #ifdef CONFIG_USB_PD_TCPC_LOW_POWER static int reset_device_and_notify(int port) { -- cgit v1.2.1