diff options
-rw-r--r-- | chip/mec1322/system.c | 26 | ||||
-rw-r--r-- | chip/stm32/usb_pd_phy.c | 2 | ||||
-rw-r--r-- | common/charge_manager.c | 60 | ||||
-rw-r--r-- | common/charge_ramp_sw.c | 4 | ||||
-rw-r--r-- | common/host_command_pd.c | 2 | ||||
-rw-r--r-- | common/i2c_master.c | 3 | ||||
-rw-r--r-- | common/pd_log.c | 4 | ||||
-rw-r--r-- | common/peripheral.c | 3 | ||||
-rw-r--r-- | common/system.c | 2 | ||||
-rw-r--r-- | common/usb_charger.c | 2 | ||||
-rw-r--r-- | common/usb_common.c | 5 | ||||
-rw-r--r-- | common/usb_pd_policy.c | 8 | ||||
-rw-r--r-- | common/usb_pd_protocol.c | 48 | ||||
-rw-r--r-- | common/usb_pd_tcpc.c | 14 | ||||
-rw-r--r-- | common/usbc/usb_pe_drp_sm.c | 9 | ||||
-rw-r--r-- | common/usbc/usb_tc_drp_acc_trysrc_sm.c | 16 | ||||
-rw-r--r-- | driver/bc12/pi3usb9201.c | 2 | ||||
-rw-r--r-- | driver/charger/bd9995x.c | 4 | ||||
-rw-r--r-- | driver/ppc/aoz1380.c | 2 | ||||
-rw-r--r-- | driver/ppc/nx20p348x.c | 2 | ||||
-rw-r--r-- | driver/ppc/sn5s330.c | 2 | ||||
-rw-r--r-- | driver/retimer/bb_retimer.c | 2 | ||||
-rw-r--r-- | driver/tcpm/it83xx.c | 4 | ||||
-rw-r--r-- | driver/tcpm/tcpci.c | 5 | ||||
-rw-r--r-- | driver/usb_mux/usb_mux.c | 4 | ||||
-rw-r--r-- | include/usb_pd.h | 17 | ||||
-rw-r--r-- | test/charge_manager.c | 10 | ||||
-rw-r--r-- | test/usb_pd.c | 2 |
28 files changed, 179 insertions, 85 deletions
diff --git a/chip/mec1322/system.c b/chip/mec1322/system.c index 6fa837f6f4..6dac6abfc6 100644 --- a/chip/mec1322/system.c +++ b/chip/mec1322/system.c @@ -17,6 +17,7 @@ #include "hooks.h" #include "task.h" #include "timer.h" +#include "usb_pd.h" #include "util.h" #include "spi.h" @@ -297,14 +298,27 @@ void system_hibernate(uint32_t seconds, uint32_t microseconds) * Leave USB-C charging enabled in hibernate, in order to * allow wake-on-plug. 5V enable must be pulled low. */ -#if CONFIG_USB_PD_PORT_MAX_COUNT > 0 - gpio_set_flags(GPIO_USB_C0_5V_EN, GPIO_PULL_DOWN | GPIO_INPUT); - gpio_set_level(GPIO_USB_C0_CHARGE_EN_L, 0); + switch (board_get_usb_pd_port_count()) { +#if CONFIG_USB_PD_PORT_MAX_COUNT >= 2 + case 2: + gpio_set_flags(GPIO_USB_C1_5V_EN, GPIO_PULL_DOWN | GPIO_INPUT); + gpio_set_level(GPIO_USB_C1_CHARGE_EN_L, 0); + /* Fall through */ #endif -#if CONFIG_USB_PD_PORT_MAX_COUNT > 1 - gpio_set_flags(GPIO_USB_C1_5V_EN, GPIO_PULL_DOWN | GPIO_INPUT); - gpio_set_level(GPIO_USB_C1_CHARGE_EN_L, 0); +#if CONFIG_USB_PD_PORT_MAX_COUNT >= 1 + case 1: + gpio_set_flags(GPIO_USB_C0_5V_EN, GPIO_PULL_DOWN | GPIO_INPUT); + gpio_set_level(GPIO_USB_C0_CHARGE_EN_L, 0); + /* Fall through */ #endif + case 0: + /* Nothing to do but break */ + break; + default: + /* More ports needs to be defined */ + ASSERT(false); + break; + } #endif /* CONFIG_USB_PD_PORT_MAX_COUNT */ if (hibernate_wake_pins_used > 0) { diff --git a/chip/stm32/usb_pd_phy.c b/chip/stm32/usb_pd_phy.c index 7296ae245f..426cac15d3 100644 --- a/chip/stm32/usb_pd_phy.c +++ b/chip/stm32/usb_pd_phy.c @@ -463,7 +463,7 @@ void pd_rx_handler(void) } #endif - for (i = 0; i < CONFIG_USB_PD_PORT_MAX_COUNT; i++) { + for (i = 0; i < board_get_usb_pd_port_count(); i++) { if (pending & EXTI_COMP_MASK(i)) { rx_edge_ts[i][rx_edge_ts_idx[i]].val = get_time().val; next_idx = (rx_edge_ts_idx[i] == diff --git a/common/charge_manager.c b/common/charge_manager.c index 6f8a796999..d95f5cb809 100644 --- a/common/charge_manager.c +++ b/common/charge_manager.c @@ -140,7 +140,7 @@ enum charge_manager_change_type { static int is_pd_port(int port) { - return port >= 0 && port < CONFIG_USB_PD_PORT_MAX_COUNT; + return port >= 0 && port < board_get_usb_pd_port_count(); } static int is_sink(int port) @@ -151,6 +151,25 @@ static int is_sink(int port) return pd_get_role(port) == PD_ROLE_SINK; } +/** + * Some of the SKUs in certain boards have less number of USB PD ports than + * defined in CONFIG_USB_PD_PORT_MAX_COUNT. With the charge port configuration + * for DEDICATED_PORT towards the end, this will lead to holes in the static + * configuration. The ports that fall in that hole are invalid and this function + * is used to check the validity of the ports. + */ +static int is_valid_port(int port) +{ + if (port < 0 || port >= CHARGE_PORT_COUNT) + return 0; + + /* Check if the port falls in the hole */ + if (port >= board_get_usb_pd_port_count() && + port < CONFIG_USB_PD_PORT_MAX_COUNT) + return 0; + return 1; +} + #ifndef TEST_BUILD static int is_connected(int port) { @@ -190,6 +209,8 @@ static void charge_manager_init(void) int i, j; for (i = 0; i < CHARGE_PORT_COUNT; ++i) { + if (!is_valid_port(i)) + continue; for (j = 0; j < CHARGE_SUPPLIER_COUNT; ++j) { available_charge[j][i].current = CHARGE_CURRENT_UNINITIALIZED; @@ -221,14 +242,17 @@ static int charge_manager_is_seeded(void) if (is_seeded) return 1; - for (i = 0; i < CHARGE_SUPPLIER_COUNT; ++i) - for (j = 0; j < CHARGE_PORT_COUNT; ++j) + for (i = 0; i < CHARGE_SUPPLIER_COUNT; ++i) { + for (j = 0; j < CHARGE_PORT_COUNT; ++j) { + if (!is_valid_port(j)) + continue; if (available_charge[i][j].current == CHARGE_CURRENT_UNINITIALIZED || available_charge[i][j].voltage == CHARGE_VOLTAGE_UNINITIALIZED) return 0; - + } + } is_seeded = 1; return 1; } @@ -506,6 +530,9 @@ static int charge_manager_get_ceil(int port) int ceil = CHARGE_CEIL_NONE; int val, i; + if (!is_valid_port(port)) + return ceil; + for (i = 0; i < CEIL_REQUESTOR_COUNT; ++i) { val = charge_ceil[port][i]; if (val != CHARGE_CEIL_NONE && @@ -544,6 +571,10 @@ static void charge_manager_get_best_charge_port(int *new_port, */ for (i = 0; i < CHARGE_SUPPLIER_COUNT; ++i) for (j = 0; j < CHARGE_PORT_COUNT; ++j) { + /* Skip this port if it is not valid. */ + if (!is_valid_port(j)) + continue; + /* * Skip this supplier if there is no * available charge. @@ -761,7 +792,7 @@ static void charge_manager_refresh(void) if (updated_old_port != CHARGE_PORT_NONE) save_log[updated_old_port] = 1; - for (i = 0; i < CONFIG_USB_PD_PORT_MAX_COUNT; ++i) + for (i = 0; i < board_get_usb_pd_port_count(); ++i) if (save_log[i]) charge_manager_save_log(i); #endif @@ -814,6 +845,11 @@ static void charge_manager_make_change(enum charge_manager_change_type change, int i; int clear_override = 0; + if (!is_valid_port(port)) { + CPRINTS("%s: p%d invalid", __func__, port); + return; + } + /* Determine if this is a change which can affect charge status */ switch (change) { case CHANGE_CHARGE: @@ -1008,6 +1044,9 @@ void charge_manager_leave_safe_mode(void) void charge_manager_set_ceil(int port, enum ceil_requestor requestor, int ceil) { + if (!is_valid_port(port)) + return; + if (charge_ceil[port][requestor] != ceil) { charge_ceil[port][requestor] = ceil; if (port == charge_port && charge_manager_is_seeded()) @@ -1138,7 +1177,7 @@ static int can_supply_max_current(int port) if (!is_active_source(port)) /* Non-active ports don't get 3A */ return 0; - for (p = 0; p < CONFIG_USB_PD_PORT_MAX_COUNT; p++) { + for (p = 0; p < board_get_usb_pd_port_count(); p++) { if (p == port) continue; if (source_port_rp[p] == @@ -1166,7 +1205,7 @@ void charge_manager_source_port(int port, int enable) return; /* Set port limit according to policy */ - for (p = 0; p < CONFIG_USB_PD_PORT_MAX_COUNT; p++) { + for (p = 0; p < board_get_usb_pd_port_count(); p++) { rp = can_supply_max_current(p) ? CONFIG_USB_PD_MAX_SINGLE_SOURCE_CURRENT : CONFIG_USB_PD_PULLUP; @@ -1206,6 +1245,11 @@ static enum ec_status hc_pd_power_info(struct host_cmd_handler_args *args) if (port == PD_POWER_CHARGING_PORT) port = charge_port; + /* + * Not checking for invalid port here, because it might break existing + * contract with ectool users. The invalid ports will have the response + * voltage, current and power parameters set to 0. + */ if (port >= CHARGE_PORT_COUNT) return EC_RES_INVALID_PARAM; @@ -1309,7 +1353,7 @@ static void charge_manager_set_external_power_limit(int current_lim, if (voltage_lim == EC_POWER_LIMIT_NONE) voltage_lim = PD_MAX_VOLTAGE_MV; - for (port = 0; port < CONFIG_USB_PD_PORT_MAX_COUNT; ++port) { + for (port = 0; port < board_get_usb_pd_port_count(); ++port) { charge_manager_set_ceil(port, CEIL_REQUESTOR_HOST, current_lim); pd_set_external_voltage_limit(port, voltage_lim); } diff --git a/common/charge_ramp_sw.c b/common/charge_ramp_sw.c index 33dbff656b..2916894e8c 100644 --- a/common/charge_ramp_sw.c +++ b/common/charge_ramp_sw.c @@ -157,7 +157,7 @@ void chg_ramp_task(void *u) int active_icl_new; /* Clear last OCP supplier to guarantee we ramp on first connect */ - for (i = 0; i < CONFIG_USB_PD_PORT_MAX_COUNT; i++) + for (i = 0; i < board_get_usb_pd_port_count(); i++) oc_info[i][0].sup = CHARGE_SUPPLIER_NONE; /* @@ -364,7 +364,7 @@ static int command_chgramp(int argc, char **argv) ccprintf("Chg Ramp:\nState: %d\nMin ICL: %d\nActive ICL: %d\n", ramp_st, min_icl, active_icl); - for (port = 0; port < CONFIG_USB_PD_PORT_MAX_COUNT; port++) { + for (port = 0; port < board_get_usb_pd_port_count(); port++) { ccprintf("Port %d:\n", port); ccprintf(" OC idx:%d\n", oc_info_idx[port]); for (i = 0; i < RAMP_COUNT; i++) { diff --git a/common/host_command_pd.c b/common/host_command_pd.c index 593ac335ec..565ae4cacf 100644 --- a/common/host_command_pd.c +++ b/common/host_command_pd.c @@ -139,7 +139,7 @@ static void pd_service_tcpc_ports(uint16_t port_status) { int i; - for (i = 0; i < CONFIG_USB_PD_PORT_MAX_COUNT; i++) { + for (i = 0; i < board_get_usb_pd_port_count(); i++) { if ((port_status & (PD_STATUS_TCPC_ALERT_0 << i)) && pd_is_port_enabled(i)) tcpc_alert(i); diff --git a/common/i2c_master.c b/common/i2c_master.c index 37090c0ad6..c84bf02f9e 100644 --- a/common/i2c_master.c +++ b/common/i2c_master.c @@ -14,6 +14,7 @@ #include "i2c.h" #include "system.h" #include "task.h" +#include "usb_pd.h" #include "usb_pd_tcpm.h" #include "util.h" #include "watchdog.h" @@ -924,7 +925,7 @@ static void i2c_passthru_protect_tcpc_ports(void) return; } - for (i = 0; i < CONFIG_USB_PD_PORT_MAX_COUNT; i++) { + for (i = 0; i < board_get_usb_pd_port_count(); i++) { /* TCPC tunnel not configured. No need to protect anything */ if (!I2C_GET_ADDR(tcpc_config[i].i2c_info.addr_flags)) continue; diff --git a/common/pd_log.c b/common/pd_log.c index 1eb5d3e3ab..3708aad72e 100644 --- a/common/pd_log.c +++ b/common/pd_log.c @@ -68,7 +68,7 @@ dequeue_retry: if (r->type == PD_EVENT_NO_ENTRY) { int i, res; incoming_logs = 0; - for (i = 0; i < CONFIG_USB_PD_PORT_MAX_COUNT; ++i) { + for (i = 0; i < board_get_usb_pd_port_count(); ++i) { /* only accessories who knows Google logging format */ if (pd_get_identity_vid(i) != USB_VID_GOOGLE) continue; @@ -96,7 +96,7 @@ static enum ec_status hc_pd_write_log_entry(struct host_cmd_handler_args *args) if (type < PD_EVENT_MCU_BASE || type >= PD_EVENT_ACC_BASE) return EC_RES_INVALID_PARAM; - if (port > 0 && port >= CONFIG_USB_PD_PORT_MAX_COUNT) + if (port > 0 && port >= board_get_usb_pd_port_count()) return EC_RES_INVALID_PARAM; switch (type) { diff --git a/common/peripheral.c b/common/peripheral.c index 7e567974ec..79a80b0ef9 100644 --- a/common/peripheral.c +++ b/common/peripheral.c @@ -7,6 +7,7 @@ #include "compile_time_macros.h" #include "ec_commands.h" #include "host_command.h" +#include "usb_pd.h" #include "usb_pd_tcpm.h" #ifdef CONFIG_HOSTCMD_LOCATE_CHIP @@ -30,7 +31,7 @@ static enum ec_status hc_locate_chip(struct host_cmd_handler_args *args) break; case EC_CHIP_TYPE_TCPC: #if defined(CONFIG_USB_PD_PORT_MAX_COUNT) && !defined(CONFIG_USB_PD_TCPC) - if (params->index >= CONFIG_USB_PD_PORT_MAX_COUNT) + if (params->index >= board_get_usb_pd_port_count()) return EC_RES_OVERFLOW; resp->bus_type = tcpc_config[params->index].bus_type; if (resp->bus_type == EC_BUS_TYPE_I2C) { diff --git a/common/system.c b/common/system.c index 90029d88b4..61ef4882fc 100644 --- a/common/system.c +++ b/common/system.c @@ -857,7 +857,7 @@ static int handle_pending_reboot(enum ec_reboot_cmd cmd) { int port; - for (port = 0; port < CONFIG_USB_PD_PORT_MAX_COUNT; + for (port = 0; port < board_get_usb_pd_port_count(); port++) pd_set_suspend(port, 1); } diff --git a/common/usb_charger.c b/common/usb_charger.c index c2dd062f6f..4a85dc6daf 100644 --- a/common/usb_charger.c +++ b/common/usb_charger.c @@ -111,7 +111,7 @@ void usb_charger_reset_charge(int port) static void usb_charger_init(void) { int i; - for (i = 0; i < CONFIG_USB_PD_PORT_MAX_COUNT; i++) { + for (i = 0; i < board_get_usb_pd_port_count(); i++) { usb_charger_reset_charge(i); /* Initialize VBUS supplier based on whether VBUS is present. */ update_vbus_supplier(i, pd_is_vbus_present(i)); diff --git a/common/usb_common.c b/common/usb_common.c index eab88e0e5e..f7c47fc787 100644 --- a/common/usb_common.c +++ b/common/usb_common.c @@ -305,3 +305,8 @@ void notify_sysjump_ready(volatile const task_id_t * const sysjump_task_waiting) TASK_EVENT_SYSJUMP_READY, 0); } #endif + +__attribute__((weak)) uint8_t board_get_usb_pd_port_count(void) +{ + return CONFIG_USB_PD_PORT_MAX_COUNT; +} diff --git a/common/usb_pd_policy.c b/common/usb_pd_policy.c index f36ec396ab..4b8fa85071 100644 --- a/common/usb_pd_policy.c +++ b/common/usb_pd_policy.c @@ -629,7 +629,7 @@ static int command_pe(int argc, char **argv) return EC_ERROR_PARAM_COUNT; /* command: pe <port> <subcmd> <args> */ port = strtoi(argv[1], &e, 10); - if (*e || port >= CONFIG_USB_PD_PORT_MAX_COUNT) + if (*e || port >= board_get_usb_pd_port_count()) return EC_ERROR_PARAM2; if (!strncasecmp(argv[2], "dump", 4)) dump_pe(port); @@ -884,7 +884,7 @@ static int command_cable(int argc, char **argv) if (argc < 2) return EC_ERROR_PARAM_COUNT; port = strtoi(argv[1], &e, 0); - if (*e || port >= CONFIG_USB_PD_PORT_MAX_COUNT) + if (*e || port >= board_get_usb_pd_port_count()) return EC_ERROR_PARAM2; if (!cable[port].is_identified) { @@ -984,7 +984,7 @@ static enum ec_status hc_remote_pd_discovery(struct host_cmd_handler_args *args) const uint8_t *port = args->params; struct ec_params_usb_pd_discovery_entry *r = args->response; - if (*port >= CONFIG_USB_PD_PORT_MAX_COUNT) + if (*port >= board_get_usb_pd_port_count()) return EC_RES_INVALID_PARAM; r->vid = pd_get_identity_vid(*port); @@ -1006,7 +1006,7 @@ static enum ec_status hc_remote_pd_get_amode(struct host_cmd_handler_args *args) const struct ec_params_usb_pd_get_mode_request *p = args->params; struct ec_params_usb_pd_get_mode_response *r = args->response; - if (p->port >= CONFIG_USB_PD_PORT_MAX_COUNT) + if (p->port >= board_get_usb_pd_port_count()) return EC_RES_INVALID_PARAM; /* no more to send */ diff --git a/common/usb_pd_protocol.c b/common/usb_pd_protocol.c index c751b02689..99ef76d62e 100644 --- a/common/usb_pd_protocol.c +++ b/common/usb_pd_protocol.c @@ -851,11 +851,11 @@ static inline void set_state(int port, enum pd_states next_state) #ifdef CONFIG_LOW_POWER_IDLE /* If a PD device is attached then disable deep sleep */ - for (i = 0; i < CONFIG_USB_PD_PORT_MAX_COUNT; i++) { + for (i = 0; i < board_get_usb_pd_port_count(); i++) { if (pd_capable(i)) break; } - if (i == CONFIG_USB_PD_PORT_MAX_COUNT) + if (i == board_get_usb_pd_port_count()) enable_sleep(SLEEP_MASK_USB_PD); else disable_sleep(SLEEP_MASK_USB_PD); @@ -1477,7 +1477,7 @@ void pd_soft_reset(void) { int i; - for (i = 0; i < CONFIG_USB_PD_PORT_MAX_COUNT; ++i) + for (i = 0; i < board_get_usb_pd_port_count(); ++i) if (pd_is_connected(i)) { set_state(i, PD_STATE_SOFT_RESET); task_wake(PD_PORT_TO_TASK_ID(i)); @@ -2378,7 +2378,7 @@ static void pd_update_try_source(void) int batt_soc = usb_get_battery_soc(); try_src = 0; - for (i = 0; i < CONFIG_USB_PD_PORT_MAX_COUNT; i++) + for (i = 0; i < board_get_usb_pd_port_count(); i++) try_src |= drp_state[i] == PD_DRP_TOGGLE_ON; /* @@ -2411,7 +2411,7 @@ static void pd_update_try_source(void) * mode went from enabled to disabled and trying_source * was active at that time. */ - for (i = 0; i < CONFIG_USB_PD_PORT_MAX_COUNT; i++) + for (i = 0; i < board_get_usb_pd_port_count(); i++) pd[i].flags &= ~PD_FLAGS_TRY_SRC; } #endif /* CONFIG_USB_PD_TRY_SRC */ @@ -2425,7 +2425,7 @@ static void pd_update_snk_reset(void) if (batt_soc < CONFIG_USB_PD_RESET_MIN_BATT_SOC) return; - for (i = 0; i < CONFIG_USB_PD_PORT_MAX_COUNT; i++) { + for (i = 0; i < board_get_usb_pd_port_count(); i++) { if (pd[i].flags & PD_FLAGS_SNK_WAITING_BATT) { /* * Battery has gained sufficient charge to kick off PD @@ -2744,13 +2744,13 @@ static void pd_init_tasks(void) #if defined(HAS_TASK_CHIPSET) && defined(CONFIG_USB_PD_DUAL_ROLE) /* Set dual-role state based on chipset power state */ if (chipset_in_state(CHIPSET_STATE_ANY_OFF)) - for (i = 0; i < CONFIG_USB_PD_PORT_MAX_COUNT; i++) + for (i = 0; i < board_get_usb_pd_port_count(); i++) drp_state[i] = PD_DRP_FORCE_SINK; else if (chipset_in_state(CHIPSET_STATE_ANY_SUSPEND)) - for (i = 0; i < CONFIG_USB_PD_PORT_MAX_COUNT; i++) + for (i = 0; i < board_get_usb_pd_port_count(); i++) drp_state[i] = PD_DRP_TOGGLE_OFF; else /* CHIPSET_STATE_ON */ - for (i = 0; i < CONFIG_USB_PD_PORT_MAX_COUNT; i++) + for (i = 0; i < board_get_usb_pd_port_count(); i++) drp_state[i] = PD_DRP_TOGGLE_ON; #endif @@ -2765,7 +2765,7 @@ static void pd_init_tasks(void) enable = 1; #endif #endif - for (i = 0; i < CONFIG_USB_PD_PORT_MAX_COUNT; i++) + for (i = 0; i < board_get_usb_pd_port_count(); i++) pd_comm_enabled[i] = enable; CPRINTS("PD comm %sabled", enable ? "en" : "dis"); @@ -4759,7 +4759,7 @@ static void pd_chipset_resume(void) { int i; - for (i = 0; i < CONFIG_USB_PD_PORT_MAX_COUNT; i++) { + for (i = 0; i < board_get_usb_pd_port_count(); i++) { #ifdef CONFIG_CHARGE_MANAGER if (charge_manager_get_active_charge_port() != i) #endif @@ -4776,7 +4776,7 @@ static void pd_chipset_suspend(void) { int i; - for (i = 0; i < CONFIG_USB_PD_PORT_MAX_COUNT; i++) + for (i = 0; i < board_get_usb_pd_port_count(); i++) pd_set_dual_role(i, PD_DRP_TOGGLE_OFF); CPRINTS("PD:S0->S3"); } @@ -4786,7 +4786,7 @@ static void pd_chipset_startup(void) { int i; - for (i = 0; i < CONFIG_USB_PD_PORT_MAX_COUNT; i++) { + for (i = 0; i < board_get_usb_pd_port_count(); i++) { pd_set_dual_role_no_wakeup(i, PD_DRP_TOGGLE_OFF); pd[i].flags |= PD_FLAGS_CHECK_IDENTITY; /* Reset cable attributes and flags */ @@ -4804,7 +4804,7 @@ static void pd_chipset_shutdown(void) { int i; - for (i = 0; i < CONFIG_USB_PD_PORT_MAX_COUNT; i++) { + for (i = 0; i < board_get_usb_pd_port_count(); i++) { pd_set_dual_role_no_wakeup(i, PD_DRP_FORCE_SINK); task_set_event(PD_PORT_TO_TASK_ID(i), PD_EVENT_POWER_STATE_CHANGE | @@ -4823,7 +4823,7 @@ void pd_prepare_sysjump(void) int i; /* Exit modes before sysjump so we can cleanly enter again later */ - for (i = 0; i < CONFIG_USB_PD_PORT_MAX_COUNT; i++) { + for (i = 0; i < board_get_usb_pd_port_count(); i++) { /* * We can't be in an alternate mode if PD comm is disabled or * the port is suspended, so no need to send the event @@ -4956,7 +4956,7 @@ static int remote_flashing(int argc, char **argv) return EC_ERROR_PARAM_COUNT; port = strtoi(argv[1], &e, 10); - if (*e || port >= CONFIG_USB_PD_PORT_MAX_COUNT) + if (*e || port >= board_get_usb_pd_port_count()) return EC_ERROR_PARAM2; cnt = 0; @@ -5197,7 +5197,7 @@ static int command_pd(int argc, char **argv) port = strtoi(argv[1], &e, 10); if (argc < 3) return EC_ERROR_PARAM_COUNT; - if (*e || port >= CONFIG_USB_PD_PORT_MAX_COUNT) + if (*e || port >= board_get_usb_pd_port_count()) return EC_ERROR_PARAM2; #if defined(CONFIG_CMD_PD) && defined(CONFIG_USB_PD_DUAL_ROLE) @@ -5386,7 +5386,7 @@ DECLARE_CONSOLE_COMMAND(pd, command_pd, static enum ec_status hc_pd_ports(struct host_cmd_handler_args *args) { struct ec_response_usb_pd_ports *r = args->response; - r->num_ports = CONFIG_USB_PD_PORT_MAX_COUNT; + r->num_ports = board_get_usb_pd_port_count(); args->response_size = sizeof(*r); return EC_RES_SUCCESS; @@ -5427,7 +5427,7 @@ static enum ec_status hc_usb_pd_control(struct host_cmd_handler_args *args) struct ec_response_usb_pd_control_v1 *r_v1 = args->response; struct ec_response_usb_pd_control *r = args->response; - if (p->port >= CONFIG_USB_PD_PORT_MAX_COUNT) + if (p->port >= board_get_usb_pd_port_count()) return EC_RES_INVALID_PARAM; if (p->role >= USB_PD_CTRL_ROLE_COUNT || @@ -5527,7 +5527,7 @@ static enum ec_status hc_remote_flash(struct host_cmd_handler_args *args) int i, size, rv = EC_RES_SUCCESS; timestamp_t timeout; - if (port >= CONFIG_USB_PD_PORT_MAX_COUNT) + if (port >= board_get_usb_pd_port_count()) return EC_RES_INVALID_PARAM; if (p->size + sizeof(*p) > args->params_size) @@ -5656,7 +5656,7 @@ static enum ec_status hc_remote_pd_dev_info(struct host_cmd_handler_args *args) const uint8_t *port = args->params; struct ec_params_usb_pd_rw_hash_entry *r = args->response; - if (*port >= CONFIG_USB_PD_PORT_MAX_COUNT) + if (*port >= board_get_usb_pd_port_count()) return EC_RES_INVALID_PARAM; r->dev_id = pd[*port].dev_id; @@ -5682,7 +5682,7 @@ static enum ec_status hc_remote_pd_chip_info(struct host_cmd_handler_args *args) const struct ec_params_pd_chip_info *p = args->params; struct ec_response_pd_chip_info_v1 *info; - if (p->port >= CONFIG_USB_PD_PORT_MAX_COUNT) + if (p->port >= board_get_usb_pd_port_count()) return EC_RES_INVALID_PARAM; if (tcpm_get_chip_info(p->port, p->live, &info)) @@ -5711,7 +5711,7 @@ static enum ec_status hc_remote_pd_set_amode(struct host_cmd_handler_args *args) { const struct ec_params_usb_pd_set_mode_request *p = args->params; - if ((p->port >= CONFIG_USB_PD_PORT_MAX_COUNT) || + if ((p->port >= board_get_usb_pd_port_count()) || (!p->svid) || (!p->opos)) return EC_RES_INVALID_PARAM; @@ -5750,7 +5750,7 @@ static enum ec_status pd_control(struct host_cmd_handler_args *args) const struct ec_params_pd_control *cmd = args->params; int enable = 0; - if (cmd->chip >= CONFIG_USB_PD_PORT_MAX_COUNT) + if (cmd->chip >= board_get_usb_pd_port_count()) return EC_RES_INVALID_PARAM; /* Always allow disable command */ diff --git a/common/usb_pd_tcpc.c b/common/usb_pd_tcpc.c index 44c6d58a96..2e1cbf73d7 100644 --- a/common/usb_pd_tcpc.c +++ b/common/usb_pd_tcpc.c @@ -1111,11 +1111,11 @@ int tcpc_set_rx_enable(int port, int enable) #if defined(CONFIG_LOW_POWER_IDLE) && !defined(CONFIG_USB_POWER_DELIVERY) /* If any PD port is connected, then disable deep sleep */ - for (i = 0; i < CONFIG_USB_PD_PORT_MAX_COUNT; ++i) + for (i = 0; i < board_get_usb_pd_port_count(); ++i) if (pd[i].rx_enabled) break; - if (i == CONFIG_USB_PD_PORT_MAX_COUNT) + if (i == board_get_usb_pd_port_count()) enable_sleep(SLEEP_MASK_USB_PD); else disable_sleep(SLEEP_MASK_USB_PD); @@ -1163,7 +1163,7 @@ void tcpc_pre_init(void) int i; /* Mark as uninitialized */ - for (i = 0; i < CONFIG_USB_PD_PORT_MAX_COUNT; i++) + for (i = 0; i < board_get_usb_pd_port_count(); i++) pd[i].power_status |= TCPC_REG_POWER_STATUS_UNINIT | TCPC_REG_POWER_STATUS_VBUS_DET; } @@ -1174,6 +1174,9 @@ void tcpc_init(int port) { int i; + if (port >= board_get_usb_pd_port_count()) + return; + /* Initialize physical layer */ pd_hw_init(port, PD_ROLE_DEFAULT(port)); pd[port].cc_pull = PD_ROLE_DEFAULT(port) == @@ -1222,6 +1225,9 @@ void pd_vbus_evt_p0(enum gpio_signal signal) #if CONFIG_USB_PD_PORT_MAX_COUNT >= 2 void pd_vbus_evt_p1(enum gpio_signal signal) { + if (board_get_usb_pd_port_count() == 1) + return; + tcpc_set_power_status(TASK_ID_TO_PD_PORT(TASK_ID_PD_C1), !gpio_get_level(GPIO_USB_C1_VBUS_WAKE_L)); task_wake(TASK_ID_PD_C1); @@ -1423,7 +1429,7 @@ static int command_tcpc(int argc, char **argv) port = strtoi(argv[1], &e, 10); if (argc < 3) return EC_ERROR_PARAM_COUNT; - if (*e || port >= CONFIG_USB_PD_PORT_MAX_COUNT) + if (*e || port >= board_get_usb_pd_port_count()) return EC_ERROR_PARAM2; if (!strcasecmp(argv[2], "clock")) { diff --git a/common/usbc/usb_pe_drp_sm.c b/common/usbc/usb_pe_drp_sm.c index 7390a5e78b..8e75e59ee4 100644 --- a/common/usbc/usb_pe_drp_sm.c +++ b/common/usbc/usb_pe_drp_sm.c @@ -4280,6 +4280,9 @@ void pd_set_vbus_discharge(int port, int enable) { static struct mutex discharge_lock[CONFIG_USB_PD_PORT_MAX_COUNT]; + if (port >= board_get_usb_pd_port_count()) + return; + mutex_lock(&discharge_lock[port]); enable &= !board_vbus_source_enabled(port); @@ -4731,7 +4734,7 @@ static int command_pe(int argc, char **argv) /* command: pe <port> <subcmd> <args> */ port = strtoi(argv[1], &e, 10); - if (*e || port >= CONFIG_USB_PD_PORT_MAX_COUNT) + if (*e || port >= board_get_usb_pd_port_count()) return EC_ERROR_PARAM2; if (!strncasecmp(argv[2], "dump", 4)) dump_pe(port); @@ -4749,7 +4752,7 @@ static enum ec_status hc_remote_pd_discovery(struct host_cmd_handler_args *args) const uint8_t *port = args->params; struct ec_params_usb_pd_discovery_entry *r = args->response; - if (*port >= CONFIG_USB_PD_PORT_MAX_COUNT) + if (*port >= board_get_usb_pd_port_count()) return EC_RES_INVALID_PARAM; r->vid = pd_get_identity_vid(*port); @@ -4772,7 +4775,7 @@ static enum ec_status hc_remote_pd_get_amode(struct host_cmd_handler_args *args) const struct ec_params_usb_pd_get_mode_request *p = args->params; struct ec_params_usb_pd_get_mode_response *r = args->response; - if (p->port >= CONFIG_USB_PD_PORT_MAX_COUNT) + if (p->port >= board_get_usb_pd_port_count()) return EC_RES_INVALID_PARAM; /* no more to send */ diff --git a/common/usbc/usb_tc_drp_acc_trysrc_sm.c b/common/usbc/usb_tc_drp_acc_trysrc_sm.c index 61208f0edb..de95682a64 100644 --- a/common/usbc/usb_tc_drp_acc_trysrc_sm.c +++ b/common/usbc/usb_tc_drp_acc_trysrc_sm.c @@ -649,7 +649,7 @@ void pd_prepare_sysjump(void) * Exit modes before sysjump so we can cleanly enter again * later */ - for (i = 0; i < CONFIG_USB_PD_PORT_MAX_COUNT; i++) { + for (i = 0; i < board_get_usb_pd_port_count(); i++) { /* * We can't be in an alternate mode if PD comm is * disabled, so no need to send the event @@ -879,7 +879,7 @@ static void pd_update_try_source(void) int batt_soc = usb_get_battery_soc(); try_src = 0; - for (i = 0; i < CONFIG_USB_PD_PORT_MAX_COUNT; i++) + for (i = 0; i < board_get_usb_pd_port_count(); i++) try_src |= drp_state[i] == PD_DRP_TOGGLE_ON; /* @@ -1027,7 +1027,7 @@ static enum ec_status hc_pd_ports(struct host_cmd_handler_args *args) { struct ec_response_usb_pd_ports *r = args->response; - r->num_ports = CONFIG_USB_PD_PORT_MAX_COUNT; + r->num_ports = board_get_usb_pd_port_count(); args->response_size = sizeof(*r); return EC_RES_SUCCESS; @@ -1068,7 +1068,7 @@ static enum ec_status hc_usb_pd_control(struct host_cmd_handler_args *args) struct ec_response_usb_pd_control_v2 *r_v2 = args->response; struct ec_response_usb_pd_control *r = args->response; - if (p->port >= CONFIG_USB_PD_PORT_MAX_COUNT) + if (p->port >= board_get_usb_pd_port_count()) return EC_RES_INVALID_PARAM; if (p->role >= USB_PD_CTRL_ROLE_COUNT || @@ -1163,7 +1163,7 @@ static enum ec_status hc_remote_flash(struct host_cmd_handler_args *args) const uint32_t *data = &(p->size) + 1; int i, size; - if (port >= CONFIG_USB_PD_PORT_MAX_COUNT) + if (port >= board_get_usb_pd_port_count()) return EC_RES_INVALID_PARAM; if (p->size + sizeof(*p) > args->params_size) @@ -1260,7 +1260,7 @@ static enum ec_status hc_remote_pd_dev_info(struct host_cmd_handler_args *args) const uint8_t *port = args->params; struct ec_params_usb_pd_rw_hash_entry *r = args->response; - if (*port >= CONFIG_USB_PD_PORT_MAX_COUNT) + if (*port >= board_get_usb_pd_port_count()) return EC_RES_INVALID_PARAM; r->dev_id = tc[*port].dev_id; @@ -1285,7 +1285,7 @@ static enum ec_status hc_remote_pd_chip_info(struct host_cmd_handler_args *args) const struct ec_params_pd_chip_info *p = args->params; struct ec_response_pd_chip_info_v1 *info; - if (p->port >= CONFIG_USB_PD_PORT_MAX_COUNT) + if (p->port >= board_get_usb_pd_port_count()) return EC_RES_INVALID_PARAM; if (tcpm_get_chip_info(p->port, p->live, &info)) @@ -1331,7 +1331,7 @@ static enum ec_status hc_remote_pd_set_amode(struct host_cmd_handler_args *args) { const struct ec_params_usb_pd_set_mode_request *p = args->params; - if ((p->port >= CONFIG_USB_PD_PORT_MAX_COUNT) || + if ((p->port >= board_get_usb_pd_port_count()) || (!p->svid) || (!p->opos)) return EC_RES_INVALID_PARAM; diff --git a/driver/bc12/pi3usb9201.c b/driver/bc12/pi3usb9201.c index be2844e3e3..7f11e1da8a 100644 --- a/driver/bc12/pi3usb9201.c +++ b/driver/bc12/pi3usb9201.c @@ -244,7 +244,7 @@ void usb_charger_task(void *u) * Set most recent bc1.2 detection supplier result to * CHARGE_SUPPLIER_NONE for all ports. */ - for (i = 0; i < CONFIG_USB_PD_PORT_MAX_COUNT; i++) + for (i = 0; i < board_get_usb_pd_port_count(); i++) bc12_supplier[port] = CHARGE_SUPPLIER_NONE; /* diff --git a/driver/charger/bd9995x.c b/driver/charger/bd9995x.c index dc3125928f..23303589b6 100644 --- a/driver/charger/bd9995x.c +++ b/driver/charger/bd9995x.c @@ -1273,7 +1273,7 @@ void usb_charger_task(void *u) vbus_voltage = 0; #endif - for (port = 0; port < CONFIG_USB_PD_PORT_MAX_COUNT; port++) { + for (port = 0; port < board_get_usb_pd_port_count(); port++) { bc12_detected_type[port] = CHARGE_SUPPLIER_NONE; bd9995x_enable_vbus_detect_interrupts(port, 1); bc12_det_mark[port] = 0; @@ -1282,7 +1282,7 @@ void usb_charger_task(void *u) while (1) { sleep_usec = -1; changed = 0; - for (port = 0; port < CONFIG_USB_PD_PORT_MAX_COUNT; port++) { + for (port = 0; port < board_get_usb_pd_port_count(); port++) { /* Get port interrupts */ interrupts = bd9995x_get_interrupts(port); if (interrupts & BD9995X_CMD_INT_VBUS_DET || diff --git a/driver/ppc/aoz1380.c b/driver/ppc/aoz1380.c index f077a5e07d..708d488e94 100644 --- a/driver/ppc/aoz1380.c +++ b/driver/ppc/aoz1380.c @@ -93,7 +93,7 @@ static void aoz1380_irq_deferred(void) int i; uint32_t pending = atomic_read_clear(&irq_pending); - for (i = 0; i < CONFIG_USB_PD_PORT_MAX_COUNT; i++) + for (i = 0; i < board_get_usb_pd_port_count(); i++) if (BIT(i) & pending) aoz1380_handle_interrupt(i); } diff --git a/driver/ppc/nx20p348x.c b/driver/ppc/nx20p348x.c index bdbd02d6b2..01c28f52eb 100644 --- a/driver/ppc/nx20p348x.c +++ b/driver/ppc/nx20p348x.c @@ -384,7 +384,7 @@ static void nx20p348x_irq_deferred(void) int i; uint32_t pending = atomic_read_clear(&irq_pending); - for (i = 0; i < CONFIG_USB_PD_PORT_MAX_COUNT; i++) + for (i = 0; i < board_get_usb_pd_port_count(); i++) if (BIT(i) & pending) nx20p348x_handle_interrupt(i); } diff --git a/driver/ppc/sn5s330.c b/driver/ppc/sn5s330.c index 6a877b0808..cdc355a8ae 100644 --- a/driver/ppc/sn5s330.c +++ b/driver/ppc/sn5s330.c @@ -750,7 +750,7 @@ static void sn5s330_irq_deferred(void) int i; uint32_t pending = atomic_read_clear(&irq_pending); - for (i = 0; i < CONFIG_USB_PD_PORT_MAX_COUNT; i++) + for (i = 0; i < board_get_usb_pd_port_count(); i++) if (BIT(i) & pending) sn5s330_handle_interrupt(i); } diff --git a/driver/retimer/bb_retimer.c b/driver/retimer/bb_retimer.c index e211c10d58..a6e7f87150 100644 --- a/driver/retimer/bb_retimer.c +++ b/driver/retimer/bb_retimer.c @@ -235,7 +235,7 @@ static int console_command_bb_retimer(int argc, char **argv) /* Get port number */ port = strtoi(argv[1], &e, 0); - if (*e || port < 0 || port > CONFIG_USB_PD_PORT_MAX_COUNT) + if (*e || port < 0 || port > board_get_usb_pd_port_count()) return EC_ERROR_PARAM1; /* Validate r/w selection */ diff --git a/driver/tcpm/it83xx.c b/driver/tcpm/it83xx.c index 4b2dc4fc6e..fd03426e33 100644 --- a/driver/tcpm/it83xx.c +++ b/driver/tcpm/it83xx.c @@ -544,11 +544,11 @@ static int it83xx_tcpm_set_rx_enable(int port, int enable) } /* If any PD port is connected, then disable deep sleep */ - for (i = 0; i < CONFIG_USB_PD_PORT_MAX_COUNT; ++i) + for (i = 0; i < board_get_usb_pd_port_count(); ++i) if (IT83XX_USBPD_GCR(i) | USBPD_REG_MASK_BMC_PHY) break; - if (i == CONFIG_USB_PD_PORT_MAX_COUNT) + if (i == board_get_usb_pd_port_count()) enable_sleep(SLEEP_MASK_USB_PD); else disable_sleep(SLEEP_MASK_USB_PD); diff --git a/driver/tcpm/tcpci.c b/driver/tcpm/tcpci.c index ba8e9f2f42..21aa13cd5e 100644 --- a/driver/tcpm/tcpci.c +++ b/driver/tcpm/tcpci.c @@ -744,7 +744,7 @@ int tcpci_get_chip_info(int port, int live, int error; int val; - if (port >= CONFIG_USB_PD_PORT_MAX_COUNT) + if (port >= board_get_usb_pd_port_count()) return EC_ERROR_INVAL; i = &info[port]; @@ -816,6 +816,9 @@ int tcpci_tcpm_init(int port) int power_status; int tries = TCPM_INIT_TRIES; + if (port >= board_get_usb_pd_port_count()) + return EC_ERROR_INVAL; + while (1) { error = tcpc_read(port, TCPC_REG_POWER_STATUS, &power_status); /* diff --git a/driver/usb_mux/usb_mux.c b/driver/usb_mux/usb_mux.c index 0d08fbc99c..122f71d773 100644 --- a/driver/usb_mux/usb_mux.c +++ b/driver/usb_mux/usb_mux.c @@ -195,7 +195,7 @@ static int command_typec(int argc, char **argv) return EC_ERROR_PARAM_COUNT; port = strtoi(argv[1], &e, 10); - if (*e || port >= CONFIG_USB_PD_PORT_MAX_COUNT) + if (*e || port >= board_get_usb_pd_port_count()) return EC_ERROR_PARAM1; if (argc < 3) { @@ -234,7 +234,7 @@ static enum ec_status hc_usb_pd_mux_info(struct host_cmd_handler_args *args) int port = p->port; const struct usb_mux *mux; - if (port >= CONFIG_USB_PD_PORT_MAX_COUNT) + if (port >= board_get_usb_pd_port_count()) return EC_RES_INVALID_PARAM; mux = &usb_muxes[port]; diff --git a/include/usb_pd.h b/include/usb_pd.h index 57249bc312..05e6fe0f02 100644 --- a/include/usb_pd.h +++ b/include/usb_pd.h @@ -2261,6 +2261,23 @@ int pd_is_vbus_present(int port); */ uint8_t board_get_dp_pin_mode(int port); +#ifdef CONFIG_USB_PD_PORT_MAX_COUNT +#ifdef CONFIG_USB_POWER_DELIVERY +/** + * Get board specific usb pd port count + * + * @return <= CONFIG_USB_PD_PORT_MAX_COUNT if configured in board file, + * else return CONFIG_USB_PD_PORT_MAX_COUNT + */ +uint8_t board_get_usb_pd_port_count(void); +#else +static inline uint8_t board_get_usb_pd_port_count(void) +{ + return CONFIG_USB_PD_PORT_MAX_COUNT; +} +#endif /* CONFIG_USB_POWER_DELIVERY */ +#endif /* CONFIG_USB_PD_PORT_MAX_COUNT */ + #ifdef CONFIG_USB_PD_RETIMER /** * Return true if specified PD port partner is UFP. diff --git a/test/charge_manager.c b/test/charge_manager.c index 30c9efc69b..7fb8685aef 100644 --- a/test/charge_manager.c +++ b/test/charge_manager.c @@ -78,7 +78,7 @@ enum battery_present battery_is_present(void) static void clear_new_power_requests(void) { int i; - for (i = 0; i < CONFIG_USB_PD_PORT_MAX_COUNT; ++i) + for (i = 0; i < board_get_usb_pd_port_count(); ++i) new_power_request[i] = 0; } @@ -115,7 +115,7 @@ static void initialize_charge_table(int current, int voltage, int ceil) charge.current = current; charge.voltage = voltage; - for (i = 0; i < CONFIG_USB_PD_PORT_MAX_COUNT; ++i) { + for (i = 0; i < board_get_usb_pd_port_count(); ++i) { for (j = 0; j < CEIL_REQUESTOR_COUNT; ++j) charge_manager_set_ceil(i, j, ceil); charge_manager_update_dualrole(i, CAP_DEDICATED); @@ -141,12 +141,12 @@ static int test_initialization(void) /* Initialize all supplier/port pairs, except for the last one */ for (i = 0; i < CHARGE_SUPPLIER_COUNT; ++i) - for (j = 0; j < CONFIG_USB_PD_PORT_MAX_COUNT; ++j) { + for (j = 0; j < board_get_usb_pd_port_count(); ++j) { if (i == 0) charge_manager_update_dualrole(j, CAP_DEDICATED); if (i == CHARGE_SUPPLIER_COUNT - 1 && - j == CONFIG_USB_PD_PORT_MAX_COUNT - 1) + j == board_get_usb_pd_port_count() - 1) break; charge_manager_update_charge(i, j, &charge); } @@ -157,7 +157,7 @@ static int test_initialization(void) /* Update last pair and verify a charge port has been selected */ charge_manager_update_charge(CHARGE_SUPPLIER_COUNT-1, - CONFIG_USB_PD_PORT_MAX_COUNT-1, + board_get_usb_pd_port_count()-1, &charge); wait_for_charge_manager_refresh(); TEST_ASSERT(active_charge_port != CHARGE_PORT_NONE); diff --git a/test/usb_pd.c b/test/usb_pd.c index 96d18beafd..6ec9b3ffda 100644 --- a/test/usb_pd.c +++ b/test/usb_pd.c @@ -147,7 +147,7 @@ static void init_ports(void) { int i; - for (i = 0; i < CONFIG_USB_PD_PORT_MAX_COUNT; ++i) { + for (i = 0; i < board_get_usb_pd_port_count(); ++i) { pd_port[i].host_mode = 0; pd_port[i].partner_role = -1; pd_port[i].has_vbus = 0; |