diff options
Diffstat (limited to 'common')
-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 |
14 files changed, 120 insertions, 60 deletions
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; |