From 2c0b14edd4ad56b16ba05fc34a7a3b4ed307c211 Mon Sep 17 00:00:00 2001 From: "Ben V. Brown" Date: Fri, 12 Jun 2020 22:13:40 +1000 Subject: [PATCH] Culling until fits in code Does _not_ negotiate correctly :( --- workspace/TS100/Core/BSP/Miniware/Power.cpp | 28 +- .../Inc/stm32f1xx_hal_rcc.h | 2 +- .../TS100/Core/BSP/Miniware/postRTOS.cpp | 9 +- .../TS100/Core/Drivers/FUSB302/FUSB302.cpp | 318 ++- .../Core/Drivers/FUSB302/USBC_PD/usb_pd.h | 192 +- .../{usb_pd_policy.c => usb_pd_policy.cpp} | 347 +--- .../FUSB302/USBC_PD/usb_pd_protocol.cpp | 1789 ++++++++--------- .../Core/Drivers/FUSB302/USBC_TCPM/tcpm.h | 134 +- .../Drivers/FUSB302/USBC_TCPM/usb_pd_tcpm.h | 66 +- .../Core/Drivers/FUSB302/tcpm_driver.cpp | 28 +- .../Core/Drivers/FUSB302/usb_pd_driver.cpp | 236 +-- .../Core/Drivers/FUSB302/usb_pd_driver.h | 4 +- workspace/TS100/Core/Src/gui.cpp | 3 +- 13 files changed, 1377 insertions(+), 1779 deletions(-) rename workspace/TS100/Core/Drivers/FUSB302/USBC_PD/{usb_pd_policy.c => usb_pd_policy.cpp} (66%) diff --git a/workspace/TS100/Core/BSP/Miniware/Power.cpp b/workspace/TS100/Core/BSP/Miniware/Power.cpp index c72e6207..cd552479 100644 --- a/workspace/TS100/Core/BSP/Miniware/Power.cpp +++ b/workspace/TS100/Core/BSP/Miniware/Power.cpp @@ -9,23 +9,37 @@ void power_probe() { // If TS100 - noop #ifdef MODEL_TS80 + startQC(systemSettings.voltageDiv); - startQC(systemSettings.voltageDiv); - - seekQC((systemSettings.cutoutSetting) ? 120 : 90, - systemSettings.voltageDiv); // this will move the QC output to the preferred voltage to start with + seekQC((systemSettings.cutoutSetting) ? 120 : 90, + systemSettings.voltageDiv); // this will move the QC output to the preferred voltage to start with #endif } void power_check() { #ifdef MODEL_TS80 - QC_resync(); + QC_resync(); + if (FUSB302_present) { + pd_run_state_machine(); + if (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_9) == GPIO_PIN_RESET) { + tcpc_alert(); + } + } + #endif } -uint8_t usb_pd_detect(){ +uint8_t usb_pd_detect() { #ifdef MODEL_TS80 - FUSB302_present=fusb302_detect(); + FUSB302_present = fusb302_detect(); + if (FUSB302_present) { + GPIO_InitTypeDef GPIO_InitStruct; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_MEDIUM; + GPIO_InitStruct.Pin = GPIO_PIN_9; + GPIO_InitStruct.Mode = GPIO_MODE_INPUT; + GPIO_InitStruct.Pull = GPIO_PULLUP; + HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); + } return FUSB302_present; #endif return false; diff --git a/workspace/TS100/Core/BSP/Miniware/Vendor/STM32F1xx_HAL_Driver/Inc/stm32f1xx_hal_rcc.h b/workspace/TS100/Core/BSP/Miniware/Vendor/STM32F1xx_HAL_Driver/Inc/stm32f1xx_hal_rcc.h index 0e3bdfc5..10c507ba 100644 --- a/workspace/TS100/Core/BSP/Miniware/Vendor/STM32F1xx_HAL_Driver/Inc/stm32f1xx_hal_rcc.h +++ b/workspace/TS100/Core/BSP/Miniware/Vendor/STM32F1xx_HAL_Driver/Inc/stm32f1xx_hal_rcc.h @@ -291,7 +291,7 @@ typedef struct /** @defgroup RCC_Flag Flags * Elements values convention: XXXYYYYYb * - YYYYY : Flag position in the register - * - XXX : Register index + * - X XX : Register index * - 001: CR register * - 010: BDCR register * - 011: CSR register diff --git a/workspace/TS100/Core/BSP/Miniware/postRTOS.cpp b/workspace/TS100/Core/BSP/Miniware/postRTOS.cpp index deb78e5d..7b596758 100644 --- a/workspace/TS100/Core/BSP/Miniware/postRTOS.cpp +++ b/workspace/TS100/Core/BSP/Miniware/postRTOS.cpp @@ -8,7 +8,14 @@ #include "stdlib.h" #include "task.h" #include "I2C_Wrapper.hpp" +#include "USBC_TCPM/tcpm.h" void postRToSInit() { - // Any after RTos setup + // Any after RTos setup FRToSI2C::FRToSInit(); +#ifdef MODEL_TS80 + tcpm_init(); + osDelay(50); + pd_init(); + osDelay(50); +#endif } diff --git a/workspace/TS100/Core/Drivers/FUSB302/FUSB302.cpp b/workspace/TS100/Core/Drivers/FUSB302/FUSB302.cpp index 44739e8d..f4f657bb 100644 --- a/workspace/TS100/Core/Drivers/FUSB302/FUSB302.cpp +++ b/workspace/TS100/Core/Drivers/FUSB302/FUSB302.cpp @@ -14,8 +14,8 @@ #include "I2C_Wrapper.hpp" #define PACKET_IS_GOOD_CRC(head) (PD_HEADER_TYPE(head) == PD_CTRL_GOOD_CRC && \ PD_HEADER_CNT(head) == 0) -const struct tcpc_config_t tcpc_config[CONFIG_USB_PD_PORT_COUNT] = { { 0, - fusb302_I2C_SLAVE_ADDR, &fusb302_tcpm_drv, TCPC_ALERT_ACTIVE_LOW }, }; +const struct tcpc_config_t tcpc_config = { +fusb302_I2C_SLAVE_ADDR, &fusb302_tcpm_drv }; static struct fusb302_chip_state { int cc_polarity; int vconn_enabled; @@ -24,15 +24,15 @@ static struct fusb302_chip_state { int rx_enable; uint8_t mdac_vnc; uint8_t mdac_rd; -} state[CONFIG_USB_PD_PORT_COUNT]; +} state; /* * Bring the FUSB302 out of reset after Hard Reset signaling. This will * automatically flush both the Rx and Tx FIFOs. */ -static void fusb302_pd_reset(int port) { +static void fusb302_pd_reset() { - tcpc_write(port, TCPC_REG_RESET, TCPC_REG_RESET_PD_RESET); + tcpc_write( TCPC_REG_RESET, TCPC_REG_RESET_PD_RESET); } @@ -40,7 +40,7 @@ static void fusb302_pd_reset(int port) { * Flush our Rx FIFO. To prevent packet framing issues, this function should * only be called when Rx is disabled. */ -static void fusb302_flush_rx_fifo(int port) { +static void fusb302_flush_rx_fifo() { /* * other bits in the register _should_ be 0 * until the day we support other SOP* types... @@ -48,39 +48,39 @@ static void fusb302_flush_rx_fifo(int port) { * value should be so we don't clobber it here! */ - tcpc_write(port, TCPC_REG_CONTROL1, TCPC_REG_CONTROL1_RX_FLUSH); + tcpc_write( TCPC_REG_CONTROL1, TCPC_REG_CONTROL1_RX_FLUSH); } -static void fusb302_flush_tx_fifo(int port) { +static void fusb302_flush_tx_fifo() { int reg; - tcpc_read(port, TCPC_REG_CONTROL0, ®); + tcpc_read( TCPC_REG_CONTROL0, ®); reg |= TCPC_REG_CONTROL0_TX_FLUSH; - tcpc_write(port, TCPC_REG_CONTROL0, reg); + tcpc_write( TCPC_REG_CONTROL0, reg); } -static void fusb302_auto_goodcrc_enable(int port, int enable) { +static void fusb302_auto_goodcrc_enable(int enable) { int reg; - tcpc_read(port, TCPC_REG_SWITCHES1, ®); + tcpc_read( TCPC_REG_SWITCHES1, ®); if (enable) reg |= TCPC_REG_SWITCHES1_AUTO_GCRC; else reg &= ~TCPC_REG_SWITCHES1_AUTO_GCRC; - tcpc_write(port, TCPC_REG_SWITCHES1, reg); + tcpc_write( TCPC_REG_SWITCHES1, reg); } /* Convert BC LVL values (in FUSB302) to Type-C CC Voltage Status */ -static int convert_bc_lvl(int port, int bc_lvl) { +static int convert_bc_lvl(int bc_lvl) { /* assume OPEN unless one of the following conditions is true... */ int ret = TYPEC_CC_VOLT_OPEN; - if (state[port].pulling_up) { + if (state.pulling_up) { if (bc_lvl == 0x00) ret = TYPEC_CC_VOLT_RA; else if (bc_lvl < 0x3) @@ -97,13 +97,13 @@ static int convert_bc_lvl(int port, int bc_lvl) { return ret; } -static int measure_cc_pin_source(int port, int cc_measure) { +static int measure_cc_pin_source(int cc_measure) { int switches0_reg; int reg; int cc_lvl; /* Read status register */ - tcpc_read(port, TCPC_REG_SWITCHES0, ®); + tcpc_read( TCPC_REG_SWITCHES0, ®); /* Save current value */ switches0_reg = reg; /* Clear pull-up register settings and measure bits */ @@ -117,16 +117,16 @@ static int measure_cc_pin_source(int port, int cc_measure) { reg |= cc_measure; /* Set measurement switch */ - tcpc_write(port, TCPC_REG_SWITCHES0, reg); + tcpc_write( TCPC_REG_SWITCHES0, reg); /* Set MDAC for Open vs Rd/Ra comparison */ - tcpc_write(port, TCPC_REG_MEASURE, state[port].mdac_vnc); + tcpc_write( TCPC_REG_MEASURE, state.mdac_vnc); /* Wait on measurement */ usleep(250); /* Read status register */ - tcpc_read(port, TCPC_REG_STATUS0, ®); + tcpc_read( TCPC_REG_STATUS0, ®); /* Assume open */ cc_lvl = TYPEC_CC_VOLT_OPEN; @@ -134,13 +134,13 @@ static int measure_cc_pin_source(int port, int cc_measure) { /* CC level is below the 'no connect' threshold (vOpen) */ if ((reg & TCPC_REG_STATUS0_COMP) == 0) { /* Set MDAC for Rd vs Ra comparison */ - tcpc_write(port, TCPC_REG_MEASURE, state[port].mdac_rd); + tcpc_write( TCPC_REG_MEASURE, state.mdac_rd); /* Wait on measurement */ usleep(250); /* Read status register */ - tcpc_read(port, TCPC_REG_STATUS0, ®); + tcpc_read( TCPC_REG_STATUS0, ®); cc_lvl = (reg & TCPC_REG_STATUS0_COMP) ? @@ -148,32 +148,32 @@ static int measure_cc_pin_source(int port, int cc_measure) { } /* Restore SWITCHES0 register to its value prior */ - tcpc_write(port, TCPC_REG_SWITCHES0, switches0_reg); + tcpc_write( TCPC_REG_SWITCHES0, switches0_reg); return cc_lvl; } /* Determine cc pin state for source when in manual detect mode */ -static void detect_cc_pin_source_manual(int port, int *cc1_lvl, int *cc2_lvl) { +static void detect_cc_pin_source_manual(int *cc1_lvl, int *cc2_lvl) { int cc1_measure = TCPC_REG_SWITCHES0_MEAS_CC1; int cc2_measure = TCPC_REG_SWITCHES0_MEAS_CC2; - if (state[port].vconn_enabled) { + if (state.vconn_enabled) { /* If VCONN enabled, measure cc_pin that matches polarity */ - if (state[port].cc_polarity) - *cc2_lvl = measure_cc_pin_source(port, cc2_measure); + if (state.cc_polarity) + *cc2_lvl = measure_cc_pin_source(cc2_measure); else - *cc1_lvl = measure_cc_pin_source(port, cc1_measure); + *cc1_lvl = measure_cc_pin_source(cc1_measure); } else { /* If VCONN not enabled, measure both cc1 and cc2 */ - *cc1_lvl = measure_cc_pin_source(port, cc1_measure); - *cc2_lvl = measure_cc_pin_source(port, cc2_measure); + *cc1_lvl = measure_cc_pin_source(cc1_measure); + *cc2_lvl = measure_cc_pin_source(cc2_measure); } } /* Determine cc pin state for sink */ -static void detect_cc_pin_sink(int port, int *cc1, int *cc2) { +static void detect_cc_pin_sink(int *cc1, int *cc2) { int reg; int orig_meas_cc1; int orig_meas_cc2; @@ -183,7 +183,7 @@ static void detect_cc_pin_sink(int port, int *cc1, int *cc2) { /* * Measure CC1 first. */ - tcpc_read(port, TCPC_REG_SWITCHES0, ®); + tcpc_read( TCPC_REG_SWITCHES0, ®); /* save original state to be returned to later... */ if (reg & TCPC_REG_SWITCHES0_MEAS_CC1) @@ -200,14 +200,14 @@ static void detect_cc_pin_sink(int port, int *cc1, int *cc2) { reg &= ~TCPC_REG_SWITCHES0_MEAS_CC2; reg |= TCPC_REG_SWITCHES0_MEAS_CC1; - tcpc_write(port, TCPC_REG_SWITCHES0, reg); + tcpc_write( TCPC_REG_SWITCHES0, reg); /* CC1 is now being measured by FUSB302. */ /* Wait on measurement */ usleep(250); - tcpc_read(port, TCPC_REG_STATUS0, &bc_lvl_cc1); + tcpc_read( TCPC_REG_STATUS0, &bc_lvl_cc1); /* mask away unwanted bits */ bc_lvl_cc1 &= (TCPC_REG_STATUS0_BC_LVL0 | TCPC_REG_STATUS0_BC_LVL1); @@ -216,29 +216,29 @@ static void detect_cc_pin_sink(int port, int *cc1, int *cc2) { * Measure CC2 next. */ - tcpc_read(port, TCPC_REG_SWITCHES0, ®); + tcpc_read( TCPC_REG_SWITCHES0, ®); /* Disable CC1 measurement switch, enable CC2 measurement switch */ reg &= ~TCPC_REG_SWITCHES0_MEAS_CC1; reg |= TCPC_REG_SWITCHES0_MEAS_CC2; - tcpc_write(port, TCPC_REG_SWITCHES0, reg); + tcpc_write( TCPC_REG_SWITCHES0, reg); /* CC2 is now being measured by FUSB302. */ /* Wait on measurement */ usleep(250); - tcpc_read(port, TCPC_REG_STATUS0, &bc_lvl_cc2); + tcpc_read( TCPC_REG_STATUS0, &bc_lvl_cc2); /* mask away unwanted bits */ bc_lvl_cc2 &= (TCPC_REG_STATUS0_BC_LVL0 | TCPC_REG_STATUS0_BC_LVL1); - *cc1 = convert_bc_lvl(port, bc_lvl_cc1); - *cc2 = convert_bc_lvl(port, bc_lvl_cc2); + *cc1 = convert_bc_lvl(bc_lvl_cc1); + *cc2 = convert_bc_lvl(bc_lvl_cc2); /* return MEAS_CC1/2 switches to original state */ - tcpc_read(port, TCPC_REG_SWITCHES0, ®); + tcpc_read( TCPC_REG_SWITCHES0, ®); if (orig_meas_cc1) reg |= TCPC_REG_SWITCHES0_MEAS_CC1; else @@ -248,7 +248,7 @@ static void detect_cc_pin_sink(int port, int *cc1, int *cc2) { else reg &= ~TCPC_REG_SWITCHES0_MEAS_CC2; - tcpc_write(port, TCPC_REG_SWITCHES0, reg); + tcpc_write( TCPC_REG_SWITCHES0, reg); } @@ -268,7 +268,7 @@ static int get_num_bytes(uint16_t header) { return rv; } -static int fusb302_send_message(int port, uint16_t header, const uint32_t *data, +static int fusb302_send_message(uint16_t header, const uint32_t *data, uint8_t *buf, int buf_pos) { int rv; int reg; @@ -315,17 +315,17 @@ static int fusb302_send_message(int port, uint16_t header, const uint32_t *data, /* burst write for speed! */ - rv = tcpc_xfer(port, buf, buf_pos, 0, 0, I2C_XFER_SINGLE); + rv = tcpc_xfer(buf, buf_pos, 0, 0, I2C_XFER_SINGLE); return rv; } -static int fusb302_tcpm_select_rp_value(int port, int rp) { +static int fusb302_tcpm_select_rp_value(int rp) { int reg; int rv; uint8_t vnc, rd; - rv = tcpc_read(port, TCPC_REG_CONTROL0, ®); + rv = tcpc_read( TCPC_REG_CONTROL0, ®); if (rv) return rv; @@ -348,35 +348,35 @@ static int fusb302_tcpm_select_rp_value(int port, int rp) { vnc = TCPC_REG_MEASURE_MDAC_MV(PD_SRC_DEF_VNC_MV); rd = TCPC_REG_MEASURE_MDAC_MV(PD_SRC_DEF_RD_THRESH_MV); } - state[port].mdac_vnc = vnc; - state[port].mdac_rd = rd; - rv = tcpc_write(port, TCPC_REG_CONTROL0, reg); + state.mdac_vnc = vnc; + state.mdac_rd = rd; + rv = tcpc_write( TCPC_REG_CONTROL0, reg); return rv; } -static int fusb302_tcpm_init(int port) { +static int fusb302_tcpm_init() { int reg; /* set default */ - state[port].cc_polarity = -1; + state.cc_polarity = -1; /* set the voltage threshold for no connect detection (vOpen) */ - state[port].mdac_vnc = TCPC_REG_MEASURE_MDAC_MV(PD_SRC_DEF_VNC_MV); + state.mdac_vnc = TCPC_REG_MEASURE_MDAC_MV(PD_SRC_DEF_VNC_MV); /* set the voltage threshold for Rd vs Ra detection */ - state[port].mdac_rd = TCPC_REG_MEASURE_MDAC_MV(PD_SRC_DEF_RD_THRESH_MV); + state.mdac_rd = TCPC_REG_MEASURE_MDAC_MV(PD_SRC_DEF_RD_THRESH_MV); /* all other variables assumed to default to 0 */ /* Restore default settings */ - tcpc_write(port, TCPC_REG_RESET, TCPC_REG_RESET_SW_RESET); + tcpc_write( TCPC_REG_RESET, TCPC_REG_RESET_SW_RESET); /* Turn on retries and set number of retries */ - tcpc_read(port, TCPC_REG_CONTROL3, ®); + tcpc_read( TCPC_REG_CONTROL3, ®); reg |= TCPC_REG_CONTROL3_AUTO_RETRY; reg |= (PD_RETRY_COUNT & 0x3) << TCPC_REG_CONTROL3_N_RETRIES_POS; - tcpc_write(port, TCPC_REG_CONTROL3, reg); + tcpc_write( TCPC_REG_CONTROL3, reg); /* Create interrupt masks */ reg = 0xFF; @@ -388,7 +388,7 @@ static int fusb302_tcpm_init(int port) { reg &= ~TCPC_REG_MASK_ALERT; /* packet received with correct CRC */ reg &= ~TCPC_REG_MASK_CRC_CHK; - tcpc_write(port, TCPC_REG_MASK, reg); + tcpc_write( TCPC_REG_MASK, reg); reg = 0xFF; /* when all pd message retries fail... */ @@ -399,46 +399,46 @@ static int fusb302_tcpm_init(int port) { reg &= ~TCPC_REG_MASKA_TX_SUCCESS; /* when fusb302 receives a hard reset */ reg &= ~TCPC_REG_MASKA_HARDRESET; - tcpc_write(port, TCPC_REG_MASKA, reg); + tcpc_write( TCPC_REG_MASKA, reg); reg = 0xFF; /* when fusb302 sends GoodCRC to ack a pd message */ reg &= ~TCPC_REG_MASKB_GCRCSENT; - tcpc_write(port, TCPC_REG_MASKB, reg); + tcpc_write( TCPC_REG_MASKB, reg); /* Interrupt Enable */ - tcpc_read(port, TCPC_REG_CONTROL0, ®); + tcpc_read( TCPC_REG_CONTROL0, ®); reg &= ~TCPC_REG_CONTROL0_INT_MASK; - tcpc_write(port, TCPC_REG_CONTROL0, reg); + tcpc_write( TCPC_REG_CONTROL0, reg); /* Set VCONN switch defaults */ - tcpm_set_polarity(port, 0); - tcpm_set_vconn(port, 0); + tcpm_set_polarity(0); + tcpm_set_vconn(0); /* Turn on the power! */ /* TODO: Reduce power consumption */ - tcpc_write(port, TCPC_REG_POWER, TCPC_REG_POWER_PWR_ALL); + tcpc_write( TCPC_REG_POWER, TCPC_REG_POWER_PWR_ALL); return 0; } -static int fusb302_tcpm_release(int port) { +static int fusb302_tcpm_release() { return EC_ERROR_UNIMPLEMENTED; } -static int fusb302_tcpm_get_cc(int port, int *cc1, int *cc2) { - if (state[port].pulling_up) { +static int fusb302_tcpm_get_cc(int *cc1, int *cc2) { + if (state.pulling_up) { /* Source mode? */ - detect_cc_pin_source_manual(port, cc1, cc2); + detect_cc_pin_source_manual(cc1, cc2); } else { /* Sink mode? */ - detect_cc_pin_sink(port, cc1, cc2); + detect_cc_pin_sink(cc1, cc2); } return 0; } -static int fusb302_tcpm_set_cc(int port, int pull) { +static int fusb302_tcpm_set_cc(int pull) { int reg; /* NOTE: FUSB302 toggles a single pull-up between CC1 and CC2 */ @@ -446,7 +446,7 @@ static int fusb302_tcpm_set_cc(int port, int pull) { switch (pull) { case TYPEC_CC_RP: /* enable the pull-up we know to be necessary */ - tcpc_read(port, TCPC_REG_SWITCHES0, ®); + tcpc_read( TCPC_REG_SWITCHES0, ®); reg &= ~(TCPC_REG_SWITCHES0_CC2_PU_EN | TCPC_REG_SWITCHES0_CC1_PU_EN | @@ -458,49 +458,49 @@ static int fusb302_tcpm_set_cc(int port, int pull) { reg |= TCPC_REG_SWITCHES0_CC1_PU_EN | TCPC_REG_SWITCHES0_CC2_PU_EN; - if (state[port].vconn_enabled) - reg |= state[port].cc_polarity ? + if (state.vconn_enabled) + reg |= state.cc_polarity ? TCPC_REG_SWITCHES0_VCONN_CC1 : - TCPC_REG_SWITCHES0_VCONN_CC2; + TCPC_REG_SWITCHES0_VCONN_CC2; - tcpc_write(port, TCPC_REG_SWITCHES0, reg); + tcpc_write( TCPC_REG_SWITCHES0, reg); - state[port].pulling_up = 1; + state.pulling_up = 1; break; case TYPEC_CC_RD: /* Enable UFP Mode */ /* turn off toggle */ - tcpc_read(port, TCPC_REG_CONTROL2, ®); + tcpc_read( TCPC_REG_CONTROL2, ®); reg &= ~TCPC_REG_CONTROL2_TOGGLE; - tcpc_write(port, TCPC_REG_CONTROL2, reg); + tcpc_write( TCPC_REG_CONTROL2, reg); /* enable pull-downs, disable pullups */ - tcpc_read(port, TCPC_REG_SWITCHES0, ®); + tcpc_read( TCPC_REG_SWITCHES0, ®); reg &= ~(TCPC_REG_SWITCHES0_CC2_PU_EN); reg &= ~(TCPC_REG_SWITCHES0_CC1_PU_EN); reg |= (TCPC_REG_SWITCHES0_CC1_PD_EN); reg |= (TCPC_REG_SWITCHES0_CC2_PD_EN); - tcpc_write(port, TCPC_REG_SWITCHES0, reg); + tcpc_write( TCPC_REG_SWITCHES0, reg); - state[port].pulling_up = 0; + state.pulling_up = 0; break; case TYPEC_CC_OPEN: /* Disable toggling */ - tcpc_read(port, TCPC_REG_CONTROL2, ®); + tcpc_read( TCPC_REG_CONTROL2, ®); reg &= ~TCPC_REG_CONTROL2_TOGGLE; - tcpc_write(port, TCPC_REG_CONTROL2, reg); + tcpc_write( TCPC_REG_CONTROL2, reg); /* Ensure manual switches are opened */ - tcpc_read(port, TCPC_REG_SWITCHES0, ®); + tcpc_read( TCPC_REG_SWITCHES0, ®); reg &= ~TCPC_REG_SWITCHES0_CC1_PU_EN; reg &= ~TCPC_REG_SWITCHES0_CC2_PU_EN; reg &= ~TCPC_REG_SWITCHES0_CC1_PD_EN; reg &= ~TCPC_REG_SWITCHES0_CC2_PD_EN; - tcpc_write(port, TCPC_REG_SWITCHES0, reg); + tcpc_write( TCPC_REG_SWITCHES0, reg); - state[port].pulling_up = 0; + state.pulling_up = 0; break; default: /* Unsupported... */ @@ -510,17 +510,17 @@ static int fusb302_tcpm_set_cc(int port, int pull) { return 0; } -static int fusb302_tcpm_set_polarity(int port, int polarity) { +static int fusb302_tcpm_set_polarity(int polarity) { /* Port polarity : 0 => CC1 is CC line, 1 => CC2 is CC line */ int reg; - tcpc_read(port, TCPC_REG_SWITCHES0, ®); + tcpc_read( TCPC_REG_SWITCHES0, ®); /* clear VCONN switch bits */ reg &= ~TCPC_REG_SWITCHES0_VCONN_CC1; reg &= ~TCPC_REG_SWITCHES0_VCONN_CC2; - if (state[port].vconn_enabled) { + if (state.vconn_enabled) { /* set VCONN switch to be non-CC line */ if (polarity) reg |= TCPC_REG_SWITCHES0_VCONN_CC1; @@ -538,9 +538,9 @@ static int fusb302_tcpm_set_polarity(int port, int polarity) { else reg |= TCPC_REG_SWITCHES0_MEAS_CC1; - tcpc_write(port, TCPC_REG_SWITCHES0, reg); + tcpc_write( TCPC_REG_SWITCHES0, reg); - tcpc_read(port, TCPC_REG_SWITCHES1, ®); + tcpc_read( TCPC_REG_SWITCHES1, ®); /* clear tx_cc bits */ reg &= ~TCPC_REG_SWITCHES1_TXCC1_EN; @@ -552,15 +552,15 @@ static int fusb302_tcpm_set_polarity(int port, int polarity) { else reg |= TCPC_REG_SWITCHES1_TXCC1_EN; - tcpc_write(port, TCPC_REG_SWITCHES1, reg); + tcpc_write( TCPC_REG_SWITCHES1, reg); /* Save the polarity for later */ - state[port].cc_polarity = polarity; + state.cc_polarity = polarity; return 0; } -static int fusb302_tcpm_set_vconn(int port, int enable) { +static int fusb302_tcpm_set_vconn(int enable) { /* * FUSB302 does not have dedicated VCONN Enable switch. * We'll get through this by disabling both of the @@ -572,31 +572,30 @@ static int fusb302_tcpm_set_vconn(int port, int enable) { int reg; /* save enable state for later use */ - state[port].vconn_enabled = enable; + state.vconn_enabled = enable; if (enable) { /* set to saved polarity */ - tcpm_set_polarity(port, state[port].cc_polarity); + tcpm_set_polarity(state.cc_polarity); } else { - tcpc_read(port, TCPC_REG_SWITCHES0, ®); + tcpc_read( TCPC_REG_SWITCHES0, ®); /* clear VCONN switch bits */ reg &= ~TCPC_REG_SWITCHES0_VCONN_CC1; reg &= ~TCPC_REG_SWITCHES0_VCONN_CC2; - tcpc_write(port, TCPC_REG_SWITCHES0, reg); + tcpc_write( TCPC_REG_SWITCHES0, reg); } return 0; } -static int fusb302_tcpm_set_msg_header(int port, int power_role, - int data_role) { +static int fusb302_tcpm_set_msg_header(int power_role, int data_role) { int reg; - tcpc_read(port, TCPC_REG_SWITCHES1, ®); + tcpc_read( TCPC_REG_SWITCHES1, ®); reg &= ~TCPC_REG_SWITCHES1_POWERROLE; reg &= ~TCPC_REG_SWITCHES1_DATAROLE; @@ -606,25 +605,25 @@ static int fusb302_tcpm_set_msg_header(int port, int power_role, if (data_role) reg |= TCPC_REG_SWITCHES1_DATAROLE; - tcpc_write(port, TCPC_REG_SWITCHES1, reg); + tcpc_write( TCPC_REG_SWITCHES1, reg); return 0; } -static int fusb302_tcpm_set_rx_enable(int port, int enable) { +static int fusb302_tcpm_set_rx_enable(int enable) { int reg; - state[port].rx_enable = enable; + state.rx_enable = enable; /* Get current switch state */ - tcpc_read(port, TCPC_REG_SWITCHES0, ®); + tcpc_read( TCPC_REG_SWITCHES0, ®); /* Clear CC1/CC2 measure bits */ reg &= ~TCPC_REG_SWITCHES0_MEAS_CC1; reg &= ~TCPC_REG_SWITCHES0_MEAS_CC2; if (enable) { - switch (state[port].cc_polarity) { + switch (state.cc_polarity) { /* if CC polarity hasnt been determined, can't enable */ case -1: return EC_ERROR_UNKNOWN; @@ -638,39 +637,39 @@ static int fusb302_tcpm_set_rx_enable(int port, int enable) { /* "shouldn't get here" */ return EC_ERROR_UNKNOWN; } - tcpc_write(port, TCPC_REG_SWITCHES0, reg); + tcpc_write( TCPC_REG_SWITCHES0, reg); /* Disable BC_LVL interrupt when enabling PD comm */ - if (!tcpc_read(port, TCPC_REG_MASK, ®)) - tcpc_write(port, TCPC_REG_MASK, reg | TCPC_REG_MASK_BC_LVL); + if (!tcpc_read( TCPC_REG_MASK, ®)) + tcpc_write( TCPC_REG_MASK, reg | TCPC_REG_MASK_BC_LVL); /* flush rx fifo in case messages have been coming our way */ - fusb302_flush_rx_fifo(port); + fusb302_flush_rx_fifo(); } else { - tcpc_write(port, TCPC_REG_SWITCHES0, reg); + tcpc_write( TCPC_REG_SWITCHES0, reg); /* Enable BC_LVL interrupt when disabling PD comm */ - if (!tcpc_read(port, TCPC_REG_MASK, ®)) - tcpc_write(port, TCPC_REG_MASK, reg & ~TCPC_REG_MASK_BC_LVL); + if (!tcpc_read( TCPC_REG_MASK, ®)) + tcpc_write( TCPC_REG_MASK, reg & ~TCPC_REG_MASK_BC_LVL); } - fusb302_auto_goodcrc_enable(port, enable); + fusb302_auto_goodcrc_enable(enable); return 0; } /* Return true if our Rx FIFO is empty */ -static int fusb302_rx_fifo_is_empty(int port) { +static int fusb302_rx_fifo_is_empty() { int reg, ret; - ret = (!tcpc_read(port, TCPC_REG_STATUS1, ®)) + ret = (!tcpc_read( TCPC_REG_STATUS1, ®)) && (reg & TCPC_REG_STATUS1_RX_EMPTY); return ret; } -static int fusb302_tcpm_get_message(int port, uint32_t *payload, int *head) { +static int fusb302_tcpm_get_message(uint32_t *payload, int *head) { /* * This is the buffer that will get the burst-read data * from the fusb302. @@ -683,7 +682,7 @@ static int fusb302_tcpm_get_message(int port, uint32_t *payload, int *head) { int rv, len; /* If our FIFO is empty then we have no packet */ - if (fusb302_rx_fifo_is_empty(port)) + if (fusb302_rx_fifo_is_empty()) return EC_ERROR_UNKNOWN; /* Read until we have a non-GoodCRC packet or an empty FIFO */ @@ -694,7 +693,7 @@ static int fusb302_tcpm_get_message(int port, uint32_t *payload, int *head) { * PART 1 OF BURST READ: Write in register address. * Issue a START, no STOP. */ - rv = tcpc_xfer(port, buf, 1, 0, 0, I2C_XFER_START); + rv = tcpc_xfer(buf, 1, 0, 0, I2C_XFER_START); /* * PART 2 OF BURST READ: Read up to the header. @@ -703,7 +702,7 @@ static int fusb302_tcpm_get_message(int port, uint32_t *payload, int *head) { * and determine how many more bytes we need to read. * TODO: Check token to ensure valid packet. */ - rv |= tcpc_xfer(port, 0, 0, buf, 3, I2C_XFER_START); + rv |= tcpc_xfer(0, 0, buf, 3, I2C_XFER_START); /* Grab the header */ *head = (buf[1] & 0xFF); @@ -717,9 +716,9 @@ static int fusb302_tcpm_get_message(int port, uint32_t *payload, int *head) { * No START, but do issue a STOP at the end. * add 4 to len to read CRC out */ - rv |= tcpc_xfer(port, 0, 0, buf, len + 4, I2C_XFER_STOP); + rv |= tcpc_xfer(0, 0, buf, len + 4, I2C_XFER_STOP); - } while (!rv && PACKET_IS_GOOD_CRC(*head) && !fusb302_rx_fifo_is_empty(port)); + } while (!rv && PACKET_IS_GOOD_CRC(*head) && !fusb302_rx_fifo_is_empty()); if (!rv) { /* Discard GoodCRC packets */ @@ -738,8 +737,8 @@ static int fusb302_tcpm_get_message(int port, uint32_t *payload, int *head) { return rv; } -static int fusb302_tcpm_transmit(int port, enum tcpm_transmit_type type, - uint16_t header, const uint32_t *data) { +static int fusb302_tcpm_transmit(enum tcpm_transmit_type type, uint16_t header, + const uint32_t *data) { /* * this is the buffer that will be burst-written into the fusb302 * maximum size necessary = @@ -760,7 +759,7 @@ static int fusb302_tcpm_transmit(int port, enum tcpm_transmit_type type, int reg; /* Flush the TXFIFO */ - fusb302_flush_tx_fifo(port); + fusb302_flush_tx_fifo (); switch (type) { case TCPC_TX_SOP: @@ -774,37 +773,37 @@ static int fusb302_tcpm_transmit(int port, enum tcpm_transmit_type type, buf[buf_pos++] = fusb302_TKN_SYNC1; buf[buf_pos++] = fusb302_TKN_SYNC2; - fusb302_send_message(port, header, data, buf, buf_pos); - // wait for the GoodCRC to come back before we let the rest - // of the code do stuff like change polarity and miss it + fusb302_send_message(header, data, buf, buf_pos); +// wait for the GoodCRC to come back before we let the rest +// of the code do stuff like change polarity and miss it // delay_us(600); osDelay(1); break; case TCPC_TX_HARD_RESET: /* Simply hit the SEND_HARD_RESET bit */ - tcpc_read(port, TCPC_REG_CONTROL3, ®); + tcpc_read( TCPC_REG_CONTROL3, ®); reg |= TCPC_REG_CONTROL3_SEND_HARDRESET; - tcpc_write(port, TCPC_REG_CONTROL3, reg); + tcpc_write( TCPC_REG_CONTROL3, reg); break; case TCPC_TX_BIST_MODE_2: /* Hit the BIST_MODE2 bit and start TX */ - tcpc_read(port, TCPC_REG_CONTROL1, ®); + tcpc_read( TCPC_REG_CONTROL1, ®); reg |= TCPC_REG_CONTROL1_BIST_MODE2; - tcpc_write(port, TCPC_REG_CONTROL1, reg); + tcpc_write( TCPC_REG_CONTROL1, reg); - tcpc_read(port, TCPC_REG_CONTROL0, ®); + tcpc_read( TCPC_REG_CONTROL0, ®); reg |= TCPC_REG_CONTROL0_TX_START; - tcpc_write(port, TCPC_REG_CONTROL0, reg); + tcpc_write( TCPC_REG_CONTROL0, reg); - //task_wait_event(PD_T_BIST_TRANSMIT); +//task_wait_event(PD_T_BIST_TRANSMIT); /* Clear BIST mode bit, TX_START is self-clearing */ - tcpc_read(port, TCPC_REG_CONTROL1, ®); + tcpc_read( TCPC_REG_CONTROL1, ®); reg &= ~TCPC_REG_CONTROL1_BIST_MODE2; - tcpc_write(port, TCPC_REG_CONTROL1, reg); + tcpc_write( TCPC_REG_CONTROL1, reg); break; default: @@ -815,20 +814,19 @@ static int fusb302_tcpm_transmit(int port, enum tcpm_transmit_type type, } #ifdef CONFIG_USB_PD_VBUS_DETECT_TCPC -static int fusb302_tcpm_get_vbus_level(int port) +static int fusb302_tcpm_get_vbus_level() { int reg; /* Read status register */ - tcpc_read(port, TCPC_REG_STATUS0, ®); - + tcpc_read( TCPC_REG_STATUS0, ®); return (reg & TCPC_REG_STATUS0_VBUSOK) ? 1 : 0; } #endif -void fusb302_tcpc_alert(int port) { +void fusb302_tcpc_alert() { /* interrupt has been received */ int interrupt; int interrupta; @@ -836,85 +834,85 @@ void fusb302_tcpc_alert(int port) { /* reading interrupt registers clears them */ - tcpc_read(port, TCPC_REG_INTERRUPT, &interrupt); - tcpc_read(port, TCPC_REG_INTERRUPTA, &interrupta); - tcpc_read(port, TCPC_REG_INTERRUPTB, &interruptb); + tcpc_read( TCPC_REG_INTERRUPT, &interrupt); + tcpc_read( TCPC_REG_INTERRUPTA, &interrupta); + tcpc_read( TCPC_REG_INTERRUPTB, &interruptb); /* * Ignore BC_LVL changes when transmitting / receiving PD, * since CC level will constantly change. */ - if (state[port].rx_enable) + if (state.rx_enable) interrupt &= ~TCPC_REG_INTERRUPT_BC_LVL; if (interrupt & TCPC_REG_INTERRUPT_BC_LVL) { /* CC Status change */ - //task_set_event(PD_PORT_TO_TASK_ID(port), PD_EVENT_CC, 0); +//task_set_event(PD_PORT_TO_TASK_ID(port), PD_EVENT_CC, 0); } if (interrupt & TCPC_REG_INTERRUPT_COLLISION) { /* packet sending collided */ - pd_transmit_complete(port, TCPC_TX_COMPLETE_FAILED); + pd_transmit_complete(TCPC_TX_COMPLETE_FAILED); } /* GoodCRC was received, our FIFO is now non-empty */ if (interrupta & TCPC_REG_INTERRUPTA_TX_SUCCESS) { - //task_set_event(PD_PORT_TO_TASK_ID(port), - // PD_EVENT_RX, 0); +//task_set_event(PD_PORT_TO_TASK_ID(port), +// PD_EVENT_RX, 0); - pd_transmit_complete(port, TCPC_TX_COMPLETE_SUCCESS); + pd_transmit_complete(TCPC_TX_COMPLETE_SUCCESS); } if (interrupta & TCPC_REG_INTERRUPTA_RETRYFAIL) { /* all retries have failed to get a GoodCRC */ - pd_transmit_complete(port, TCPC_TX_COMPLETE_FAILED); + pd_transmit_complete(TCPC_TX_COMPLETE_FAILED); } if (interrupta & TCPC_REG_INTERRUPTA_HARDSENT) { /* hard reset has been sent */ /* bring FUSB302 out of reset */ - fusb302_pd_reset(port); + fusb302_pd_reset(); - pd_transmit_complete(port, TCPC_TX_COMPLETE_SUCCESS); + pd_transmit_complete(TCPC_TX_COMPLETE_SUCCESS); } if (interrupta & TCPC_REG_INTERRUPTA_HARDRESET) { /* hard reset has been received */ /* bring FUSB302 out of reset */ - fusb302_pd_reset(port); + fusb302_pd_reset(); - pd_execute_hard_reset(port); + pd_execute_hard_reset (); - //task_wake(PD_PORT_TO_TASK_ID(port)); +//task_wake(PD_PORT_TO_TASK_ID(port)); } if (interruptb & TCPC_REG_INTERRUPTB_GCRCSENT) { /* Packet received and GoodCRC sent */ /* (this interrupt fires after the GoodCRC finishes) */ - if (state[port].rx_enable) { + if (state.rx_enable) { //task_set_event(PD_PORT_TO_TASK_ID(port), // PD_EVENT_RX, 0); } else { /* flush rx fifo if rx isn't enabled */ - fusb302_flush_rx_fifo(port); + fusb302_flush_rx_fifo(); } } } /* For BIST receiving */ -void tcpm_set_bist_test_data(int port) { +void tcpm_set_bist_test_data() { int reg; /* Read control3 register */ - tcpc_read(port, TCPC_REG_CONTROL3, ®); + tcpc_read( TCPC_REG_CONTROL3, ®); /* Set the BIST_TMODE bit (Clears on Hard Reset) */ reg |= TCPC_REG_CONTROL3_BIST_TMODE; /* Write the updated value */ - tcpc_write(port, TCPC_REG_CONTROL3, reg); + tcpc_write( TCPC_REG_CONTROL3, reg); } diff --git a/workspace/TS100/Core/Drivers/FUSB302/USBC_PD/usb_pd.h b/workspace/TS100/Core/Drivers/FUSB302/USBC_PD/usb_pd.h index cd520d3e..e863e5ff 100644 --- a/workspace/TS100/Core/Drivers/FUSB302/USBC_PD/usb_pd.h +++ b/workspace/TS100/Core/Drivers/FUSB302/USBC_PD/usb_pd.h @@ -192,17 +192,17 @@ enum pd_rx_errors { /* function table for entered mode */ struct amode_fx { - int (*status)(int port, uint32_t *payload); - int (*config)(int port, uint32_t *payload); + int (*status)( uint32_t *payload); + int (*config)( uint32_t *payload); }; /* function table for alternate mode capable responders */ struct svdm_response { - int (*identity)(int port, uint32_t *payload); - int (*svids)(int port, uint32_t *payload); - int (*modes)(int port, uint32_t *payload); - int (*enter_mode)(int port, uint32_t *payload); - int (*exit_mode)(int port, uint32_t *payload); + int (*identity)( uint32_t *payload); + int (*svids)( uint32_t *payload); + int (*modes)( uint32_t *payload); + int (*enter_mode)( uint32_t *payload); + int (*exit_mode)( uint32_t *payload); struct amode_fx *amode; }; @@ -214,12 +214,12 @@ struct svdm_svid_data { struct svdm_amode_fx { uint16_t svid; - int (*enter)(int port, uint32_t mode_caps); - int (*status)(int port, uint32_t *payload); - int (*config)(int port, uint32_t *payload); - void (*post_config)(int port); - int (*attention)(int port, uint32_t *payload); - void (*exit)(int port); + int (*enter)( uint32_t mode_caps); + int (*status)( uint32_t *payload); + int (*config)( uint32_t *payload); + void (*post_config)(); + int (*attention)( uint32_t *payload); + void (*exit)(); }; /* defined in /usb_pd_policy.c */ @@ -251,8 +251,6 @@ enum hpd_event { /* supported alternate modes */ enum pd_alternate_modes { - PD_AMODE_GOOGLE, - PD_AMODE_DISPLAYPORT, /* not a real mode */ PD_AMODE_COUNT, }; @@ -787,7 +785,7 @@ void pd_set_dual_role(enum pd_dual_role_states state); * * @param port Port number from which to get role */ -int pd_get_role(int port); +int pd_get_role(); #endif @@ -973,7 +971,7 @@ enum pd_request_type { * @param port USB-C port number * @return 0 for PD_REV1.0, 1 for PD_REV2.0, 2 for PD_REV3.0 */ -int pd_get_rev(int port); +int pd_get_rev(); /** * Get current PD VDO Version @@ -981,7 +979,7 @@ int pd_get_rev(int port); * @param port USB-C port number * @return 0 for PD_REV1.0, 1 for PD_REV2.0 */ -int pd_get_vdo_ver(int port); +int pd_get_vdo_ver(); #else #define pd_get_rev(n) PD_REV20 #define pd_get_vdo_ver(n) VDM_VER10 @@ -996,7 +994,7 @@ int pd_get_vdo_ver(int port); * @param req_type request type * @return <0 if invalid, else EC_SUCCESS */ -int pd_build_request(int port, uint32_t *rdo, uint32_t *ma, uint32_t *mv, +int pd_build_request( uint32_t *rdo, uint32_t *ma, uint32_t *mv, enum pd_request_type req_type); /** @@ -1014,7 +1012,7 @@ int pd_is_max_request_allowed(void); * @param cnt the number of Power Data Objects. * @param src_caps Power Data Objects representing the source capabilities. */ -void pd_process_source_cap_callback(int port, int cnt, uint32_t *src_caps); +void pd_process_source_cap_callback( int cnt, uint32_t *src_caps); /** * Process source capabilities packet @@ -1023,7 +1021,7 @@ void pd_process_source_cap_callback(int port, int cnt, uint32_t *src_caps); * @param cnt the number of Power Data Objects. * @param src_caps Power Data Objects representing the source capabilities. */ -void pd_process_source_cap(int port, int cnt, uint32_t *src_caps); +void pd_process_source_cap( int cnt, uint32_t *src_caps); /** * Find PDO index that offers the most amount of power and stays within @@ -1034,7 +1032,7 @@ void pd_process_source_cap(int port, int cnt, uint32_t *src_caps); * @param pdo raw pdo corresponding to index, or index 0 on error (output) * @return index of PDO within source cap packet */ -int pd_find_pdo_index(int port, int max_mv, uint32_t *pdo); +int pd_find_pdo_index( int max_mv, uint32_t *pdo); /** * Extract power information out of a Power Data Object (PDO) @@ -1052,7 +1050,7 @@ void pd_extract_pdo_power(uint32_t pdo, uint32_t *ma, uint32_t *mv); * @param ma reduce current to minimum value. * @param mv reduce voltage to minimum value. */ -void pd_snk_give_back(int port, uint32_t * const ma, uint32_t * const mv); +void pd_snk_give_back( uint32_t * const ma, uint32_t * const mv); /** * Put a cap on the max voltage requested as a sink. @@ -1104,7 +1102,7 @@ void pd_transition_voltage(int idx); * * @param port USB-C port number */ -void pd_power_supply_reset(int port); +void pd_power_supply_reset(); /** * Enable or disable VBUS discharge for a given port. @@ -1112,7 +1110,7 @@ void pd_power_supply_reset(int port); * @param port USB-C port number * @enable 1 if enabling discharge, 0 if disabling */ -void pd_set_vbus_discharge(int port, int enable); +void pd_set_vbus_discharge( int enable); /** * Enable the power supply output after the ready delay. @@ -1120,7 +1118,7 @@ void pd_set_vbus_discharge(int port, int enable); * @param port USB-C port number * @return EC_SUCCESS if the power supply is ready, <0 else. */ -int pd_set_power_supply_ready(int port); +int pd_set_power_supply_ready(); /** * Ask the specified voltage from the PD source. @@ -1129,7 +1127,7 @@ int pd_set_power_supply_ready(int port); * @param port USB-C port number * @param mv request voltage in millivolts. */ -void pd_request_source_voltage(int port, int mv); +void pd_request_source_voltage( int mv); /** * Set a voltage limit from the PD source. @@ -1138,7 +1136,7 @@ void pd_request_source_voltage(int port, int mv); * @param port USB-C port number * @param mv limit voltage in millivolts. */ -void pd_set_external_voltage_limit(int port, int mv); +void pd_set_external_voltage_limit( int mv); /** * Set the PD input current limit. @@ -1147,7 +1145,7 @@ void pd_set_external_voltage_limit(int port, int mv); * @param max_ma Maximum current limit * @param supply_voltage Voltage at which current limit is applied */ -void pd_set_input_current_limit(int port, uint32_t max_ma, +void pd_set_input_current_limit( uint32_t max_ma, uint32_t supply_voltage); @@ -1156,7 +1154,7 @@ void pd_set_input_current_limit(int port, uint32_t max_ma, * * @param port USB-C port number. */ -void pd_update_contract(int port); +void pd_update_contract(); /* Encode DTS status of port partner in current limit parameter */ typedef uint32_t typec_current_t; @@ -1170,7 +1168,7 @@ typedef uint32_t typec_current_t; * @param max_ma Maximum current limit * @param supply_voltage Voltage at which current limit is applied */ -void typec_set_input_current_limit(int port, typec_current_t max_ma, +void typec_set_input_current_limit( typec_current_t max_ma, uint32_t supply_voltage); /** @@ -1179,7 +1177,7 @@ void typec_set_input_current_limit(int port, typec_current_t max_ma, * @param port USB-C port number * @param rp One of enum tcpc_rp_value (eg TYPEC_RP_3A0) defining the limit. */ -void typec_set_source_current_limit(int port, int rp); +void typec_set_source_current_limit( int rp); /** * Verify board specific health status : current, voltages... @@ -1194,14 +1192,14 @@ int pd_board_checks(void); * @param port USB-C port number * @return VBUS is detected */ -int pd_snk_is_vbus_provided(int port); +int pd_snk_is_vbus_provided(); /** * Notify PD protocol that VBUS has gone low * * @param port USB-C port number */ -void pd_vbus_low(int port); +void pd_vbus_low(); /** * Check if power swap is allowed. @@ -1209,7 +1207,7 @@ void pd_vbus_low(int port); * @param port USB-C port number * @return True if power swap is allowed, False otherwise */ -int pd_check_power_swap(int port); +int pd_check_power_swap(); /** * Check if data swap is allowed. @@ -1218,7 +1216,7 @@ int pd_check_power_swap(int port); * @param data_role current data role * @return True if data swap is allowed, False otherwise */ -int pd_check_data_swap(int port, int data_role); +int pd_check_data_swap( int data_role); /** * Check if vconn swap is allowed. @@ -1227,7 +1225,7 @@ int pd_check_data_swap(int port, int data_role); * @return True if vconn swap is allowed, False otherwise */ -int pd_check_vconn_swap(int port); +int pd_check_vconn_swap(); /** * Check current power role for potential power swap @@ -1236,7 +1234,7 @@ int pd_check_vconn_swap(int port); * @param pr_role Our power role * @param flags PD flags */ -void pd_check_pr_role(int port, int pr_role, int flags); +void pd_check_pr_role( int pr_role, int flags); /** * Check current data role for potential data swap @@ -1245,7 +1243,7 @@ void pd_check_pr_role(int port, int pr_role, int flags); * @param dr_role Our data role * @param flags PD flags */ -void pd_check_dr_role(int port, int dr_role, int flags); +void pd_check_dr_role( int dr_role, int flags); /** * Check if we should charge from this device. This is @@ -1264,7 +1262,7 @@ int pd_charge_from_device(uint16_t vid, uint16_t pid); * @param port USB-C port number * @param data_role new data role */ -void pd_execute_data_swap(int port, int data_role); +void pd_execute_data_swap( int data_role); /** * Get PD device info used for VDO_CMD_SEND_INFO / VDO_CMD_READ_INFO @@ -1282,7 +1280,7 @@ void pd_get_info(uint32_t *info_data); * @param rpayload pointer to the data to send back. * @return if >0, number of VDOs to send back. */ -int pd_custom_vdm(int port, int cnt, uint32_t *payload, uint32_t **rpayload); +int pd_custom_vdm( int cnt, uint32_t *payload, uint32_t **rpayload); /** * Handle Structured Vendor Defined Messages @@ -1293,7 +1291,7 @@ int pd_custom_vdm(int port, int cnt, uint32_t *payload, uint32_t **rpayload); * @param rpayload pointer to the data to send back. * @return if >0, number of VDOs to send back. */ -int pd_svdm(int port, int cnt, uint32_t *payload, uint32_t **rpayload); +int pd_svdm( int cnt, uint32_t *payload, uint32_t **rpayload); /** * Handle Custom VDMs for flashing. @@ -1303,7 +1301,7 @@ int pd_svdm(int port, int cnt, uint32_t *payload, uint32_t **rpayload); * @param payload payload data. * @return if >0, number of VDOs to send back. */ -int pd_custom_flash_vdm(int port, int cnt, uint32_t *payload); +int pd_custom_flash_vdm( int cnt, uint32_t *payload); /** * Enter alternate mode on DFP @@ -1313,7 +1311,7 @@ int pd_custom_flash_vdm(int port, int cnt, uint32_t *payload); * @param opos object position of mode to exit. * @return vdm for UFP to be sent to enter mode or zero if not. */ -uint32_t pd_dfp_enter_mode(int port, uint16_t svid, int opos); +uint32_t pd_dfp_enter_mode( uint16_t svid, int opos); /** * Get DisplayPort pin mode for DFP to request from UFP's capabilities. @@ -1322,7 +1320,7 @@ uint32_t pd_dfp_enter_mode(int port, uint16_t svid, int opos); * @param status DisplayPort Status VDO. * @return one-hot PIN config to request. */ -int pd_dfp_dp_get_pin_mode(int port, uint32_t status); +int pd_dfp_dp_get_pin_mode( uint32_t status); /** * Exit alternate mode on DFP @@ -1332,14 +1330,14 @@ int pd_dfp_dp_get_pin_mode(int port, uint32_t status); * @param opos object position of mode to exit. * @return 1 if UFP should be sent exit mode VDM. */ -int pd_dfp_exit_mode(int port, uint16_t svid, int opos); +int pd_dfp_exit_mode( uint16_t svid, int opos); /** * Initialize policy engine for DFP * * @param port USB-C port number */ -void pd_dfp_pe_init(int port); +void pd_dfp_pe_init(); /** * Return the VID of the USB PD accessory connected to a specified port @@ -1347,7 +1345,7 @@ void pd_dfp_pe_init(int port); * @param port USB-C port number * @return the USB Vendor Identifier or 0 if it doesn't exist */ -uint16_t pd_get_identity_vid(int port); +uint16_t pd_get_identity_vid(); /** * Return the PID of the USB PD accessory connected to a specified port @@ -1355,7 +1353,7 @@ uint16_t pd_get_identity_vid(int port); * @param port USB-C port number * @return the USB Product Identifier or 0 if it doesn't exist */ -uint16_t pd_get_identity_pid(int port); +uint16_t pd_get_identity_pid(); /** * Store Device ID & RW hash of device @@ -1367,7 +1365,7 @@ uint16_t pd_get_identity_pid(int port); * @return true if the dev / hash match an existing hash * in our table, false otherwise */ -int pd_dev_store_rw_hash(int port, uint16_t dev_id, uint32_t *rw_hash, +int pd_dev_store_rw_hash( uint16_t dev_id, uint32_t *rw_hash, uint32_t ec_current_image); /** @@ -1376,7 +1374,7 @@ int pd_dev_store_rw_hash(int port, uint16_t dev_id, uint32_t *rw_hash, * @param port USB-C accessory port number * @return EC_RES_SUCCESS if the VDM was sent properly else error code */ -int pd_fetch_acc_log_entry(int port); +int pd_fetch_acc_log_entry(); /** * Analyze the log entry received as the VDO_CMD_GET_LOG payload. @@ -1385,7 +1383,7 @@ int pd_fetch_acc_log_entry(int port); * @param cnt number of data objects in payload * @param payload payload data */ -void pd_log_recv_vdm(int port, int cnt, uint32_t *payload); +void pd_log_recv_vdm( int cnt, uint32_t *payload); /** * Send Vendor Defined Message @@ -1396,7 +1394,7 @@ void pd_log_recv_vdm(int port, int cnt, uint32_t *payload); * @param data Pointer to payload to send * @param count number of data objects in payload */ -void pd_send_vdm(int port, uint32_t vid, int cmd, const uint32_t *data, +void pd_send_vdm( uint32_t vid, int cmd, const uint32_t *data, int count); /* Power Data Objects for the source and the sink */ @@ -1425,7 +1423,7 @@ static inline void pd_send_host_event(int mask) { } * @param svid USB standard or vendor id * @return object position of mode chosen in alternate mode otherwise zero. */ -int pd_alt_mode(int port, uint16_t svid); +int pd_alt_mode( uint16_t svid); /** * Send hpd over USB PD. @@ -1433,7 +1431,7 @@ int pd_alt_mode(int port, uint16_t svid); * @param port port number. * @param hpd hotplug detect type. */ -void pd_send_hpd(int port, enum hpd_event hpd); +void pd_send_hpd( enum hpd_event hpd); /** * Enable USB Billboard Device. @@ -1448,7 +1446,7 @@ extern const struct deferred_data pd_usb_billboard_deferred_data; * * @param port USB-C port number */ -void pd_init_dequeue(int port); +void pd_init_dequeue(); /** * Prepare packet reading state machine. @@ -1459,7 +1457,7 @@ void pd_init_dequeue(int port); * @param val the read bits. * @return new position in the packet buffer. */ -int pd_dequeue_bits(int port, int off, int len, uint32_t *val); +int pd_dequeue_bits( int off, int len, uint32_t *val); /** * Advance until the end of the preamble. @@ -1467,7 +1465,7 @@ int pd_dequeue_bits(int port, int off, int len, uint32_t *val); * @param port USB-C port number * @return new position in the packet buffer. */ -int pd_find_preamble(int port); +int pd_find_preamble(); /** * Write the preamble in the TX buffer. @@ -1475,7 +1473,7 @@ int pd_find_preamble(int port); * @param port USB-C port number * @return new position in the packet buffer. */ -int pd_write_preamble(int port); +int pd_write_preamble(); /** * Write one 10-period symbol in the TX packet. @@ -1487,7 +1485,7 @@ int pd_write_preamble(int port); * @param val10 the 10-bit integer. * @return new position in the packet buffer. */ -int pd_write_sym(int port, int bit_off, uint32_t val10); +int pd_write_sym( int bit_off, uint32_t val10); /** @@ -1498,7 +1496,7 @@ int pd_write_sym(int port, int bit_off, uint32_t val10); * @param bit_off current position in the packet buffer. * @return new position in the packet buffer. */ -int pd_write_last_edge(int port, int bit_off); +int pd_write_last_edge( int bit_off); /** * Do 4B5B encoding on a 32-bit word. @@ -1508,7 +1506,7 @@ int pd_write_last_edge(int port, int bit_off); * @param val32 32-bit word value to encode * @return new offset in the message in bits. */ -int encode_word(int port, int off, uint32_t val32); +int encode_word( int off, uint32_t val32); /** * Ensure that we have an edge after EOP and we end up at level 0, @@ -1520,7 +1518,7 @@ int encode_word(int port, int off, uint32_t val32); * @param data payload content * @return length of the message in bits. */ -int prepare_message(int port, uint16_t header, uint8_t cnt, +int prepare_message( uint16_t header, uint8_t cnt, const uint32_t *data); /** @@ -1529,7 +1527,7 @@ int prepare_message(int port, uint16_t header, uint8_t cnt, * @param port USB-C port number * @param msg context string. */ -void pd_dump_packet(int port, const char *msg); +void pd_dump_packet( const char *msg); /** * Change the TX data clock frequency. @@ -1537,7 +1535,7 @@ void pd_dump_packet(int port, const char *msg); * @param port USB-C port number * @param freq frequency in hertz. */ -void pd_set_clock(int port, int freq); +void pd_set_clock( int freq); /* TX/RX callbacks */ @@ -1549,7 +1547,7 @@ void pd_set_clock(int port, int freq); * @param bit_len size of the packet in bits. * @return length transmitted or negative if error */ -int pd_start_tx(int port, int polarity, int bit_len); +int pd_start_tx( int polarity, int bit_len); /** * Set PD TX DMA to use circular mode. Call this before pd_start_tx() to @@ -1557,14 +1555,14 @@ int pd_start_tx(int port, int polarity, int bit_len); * * @param port USB-C port number */ -void pd_tx_set_circular_mode(int port); +void pd_tx_set_circular_mode(); /** * Stop PD TX DMA circular mode transaction already in progress. * * @param port USB-C port number */ -void pd_tx_clear_circular_mode(int port); +void pd_tx_clear_circular_mode(); /** * Call when we are done sending a packet. @@ -1572,7 +1570,7 @@ void pd_tx_clear_circular_mode(int port); * @param port USB-C port number * @param polarity plug polarity (0=CC1, 1=CC2). */ -void pd_tx_done(int port, int polarity); +void pd_tx_done( int polarity); /** * Check whether the PD reception is started. @@ -1580,14 +1578,14 @@ void pd_tx_done(int port, int polarity); * @param port USB-C port number * @return true if the reception is on-going. */ -int pd_rx_started(int port); +int pd_rx_started(); /** * Suspend the PD task. * @param port USB-C port number * @param enable pass 0 to resume, anything else to suspend */ -void pd_set_suspend(int port, int enable); +void pd_set_suspend( int enable); /** * Check if the port has been initialized and PD task has not been @@ -1596,29 +1594,29 @@ void pd_set_suspend(int port, int enable); * @param port USB-C port number * @return true if the PD task is not suspended. */ -int pd_is_port_enabled(int port); +int pd_is_port_enabled(); /* Callback when the hardware has detected an incoming packet */ -void pd_rx_event(int port); +void pd_rx_event(); /* Start sampling the CC line for reception */ -void pd_rx_start(int port); +void pd_rx_start(); /* Call when we are done reading a packet */ -void pd_rx_complete(int port); +void pd_rx_complete(); /* restart listening to the CC wire */ -void pd_rx_enable_monitoring(int port); +void pd_rx_enable_monitoring(); /* stop listening to the CC wire during transmissions */ -void pd_rx_disable_monitoring(int port); +void pd_rx_disable_monitoring(); /* get time since last RX edge interrupt */ -uint64_t get_time_since_last_edge(int port); +uint64_t get_time_since_last_edge(); /** * Deinitialize the hardware used for PD. * * @param port USB-C port number */ -void pd_hw_release(int port); +void pd_hw_release(); /** * Initialize the hardware used for PD RX/TX. @@ -1626,7 +1624,7 @@ void pd_hw_release(int port); * @param port USB-C port number * @param role Role to initialize pins in */ -void pd_hw_init(int port, int role); +void pd_hw_init( int role); /** * Initialize the reception side of hardware used for PD. @@ -1636,19 +1634,19 @@ void pd_hw_init(int port, int role); * * @param port USB-C port number */ -void pd_hw_init_rx(int port); +void pd_hw_init_rx(); /** * Initialize the Power Delivery state machine */ -void pd_init(int port); +void pd_init(); /** * Run the state machine. This function must be called regularly * to iterate through the state machine. It uses get_time() to * determine what actions to take each call. */ -void pd_run_state_machine(int port); +void pd_run_state_machine(); /* --- Protocol layer functions --- */ @@ -1659,14 +1657,14 @@ void pd_run_state_machine(int port); * @param payload buffer to store the packet payload (must be 7x 32-bit) * @return the packet header or <0 in case of error */ -int pd_analyze_rx(int port, uint32_t *payload); +int pd_analyze_rx( uint32_t *payload); /** * Check if PD communication is enabled * * @return true if it's enabled or false otherwise */ -int pd_comm_is_enabled(int port); +int pd_comm_is_enabled(); /** * Get connected state @@ -1674,14 +1672,14 @@ int pd_comm_is_enabled(int port); * @param port USB-C port number * @return True if port is in connected state */ -int pd_is_connected(int port); +int pd_is_connected(); /** * Execute a hard reset * * @param port USB-C port number */ -void pd_execute_hard_reset(int port); +void pd_execute_hard_reset(); /** * Signal to protocol layer that PD transmit is complete @@ -1689,28 +1687,28 @@ void pd_execute_hard_reset(int port); * @param port USB-C port number * @param status status of the transmission */ -void pd_transmit_complete(int port, int status); +void pd_transmit_complete( int status); /** * Get port polarity. * * @param port USB-C port number */ -int pd_get_polarity(int port); +int pd_get_polarity(); /** * Get port partner data swap capable status * * @param port USB-C port number */ -int pd_get_partner_data_swap_capable(int port); +int pd_get_partner_data_swap_capable(); /** * Request power swap command to be issued * * @param port USB-C port number */ -void pd_request_power_swap(int port); +void pd_request_power_swap(); /** * Try to become the VCONN source, if we are not already the source and the @@ -1718,14 +1716,14 @@ void pd_request_power_swap(int port); * * @param port USB-C port number */ -void pd_try_vconn_src(int port); +void pd_try_vconn_src(); /** * Request data swap command to be issued * * @param port USB-C port number */ -void pd_request_data_swap(int port); +void pd_request_data_swap(); /** * Set the PD communication enabled flag. When communication is disabled, @@ -1735,7 +1733,7 @@ void pd_request_data_swap(int port); * @param port USB-C port number * @param enable Enable flag to set */ -void pd_comm_enable(int port, int enable); +void pd_comm_enable( int enable); /** * Set the PD pings enabled flag. When source has negotiated power over @@ -1745,7 +1743,7 @@ void pd_comm_enable(int port, int enable); * @param port USB-C port number * @param enable Enable flag to set */ -void pd_ping_enable(int port, int enable); +void pd_ping_enable( int enable); /* Issue PD soft reset */ void pd_soft_reset(void); @@ -1758,7 +1756,7 @@ void pd_prepare_reset(void); * * @param port USB-C port number */ -void pd_set_new_power_request(int port); +void pd_set_new_power_request(); /** * Return true if partner port is a DTS or TS capable of entering debug @@ -1766,7 +1764,7 @@ void pd_set_new_power_request(int port); * * @param port USB-C port number */ -int pd_ts_dts_plugged(int port); +int pd_ts_dts_plugged(); /* ----- Logging ----- */ #ifdef CONFIG_USB_PD_LOGGING diff --git a/workspace/TS100/Core/Drivers/FUSB302/USBC_PD/usb_pd_policy.c b/workspace/TS100/Core/Drivers/FUSB302/USBC_PD/usb_pd_policy.cpp similarity index 66% rename from workspace/TS100/Core/Drivers/FUSB302/USBC_PD/usb_pd_policy.c rename to workspace/TS100/Core/Drivers/FUSB302/USBC_PD/usb_pd_policy.cpp index 49b9cdad..d70f7c3c 100644 --- a/workspace/TS100/Core/Drivers/FUSB302/USBC_PD/usb_pd_policy.c +++ b/workspace/TS100/Core/Drivers/FUSB302/USBC_PD/usb_pd_policy.cpp @@ -17,69 +17,31 @@ static int rw_flash_changed = 1; int pd_check_requested_voltage(uint32_t rdo, const int port) { - int max_ma = rdo & 0x3FF; - int op_ma = (rdo >> 10) & 0x3FF; - int idx = RDO_POS(rdo); - uint32_t pdo; - uint32_t pdo_ma; -#if defined(CONFIG_USB_PD_DYNAMIC_SRC_CAP) || \ - defined(CONFIG_USB_PD_MAX_SINGLE_SOURCE_CURRENT) - const uint32_t *src_pdo; - const int pdo_cnt = charge_manager_get_source_pdo(&src_pdo, port); -#else - const uint32_t *src_pdo = pd_src_pdo; - const int pdo_cnt = pd_src_pdo_cnt; -#endif - - /* Board specific check for this request */ - if (pd_board_check_request(rdo, pdo_cnt)) - return EC_ERROR_INVAL; - - /* check current ... */ - pdo = src_pdo[idx - 1]; - pdo_ma = (pdo & 0x3ff); - if (op_ma > pdo_ma) - return EC_ERROR_INVAL; /* too much op current */ - if (max_ma > pdo_ma && !(rdo & RDO_CAP_MISMATCH)) - return EC_ERROR_INVAL; /* too much max current */ - - CPRINTF("Requested %d V %d mA (for %d/%d mA)\n", - ((pdo >> 10) & 0x3ff) * 50, (pdo & 0x3ff) * 10, - op_ma * 10, max_ma * 10); - - /* Accept the requested voltage */ - return EC_SUCCESS; + //No source + return EC_ERROR_INVAL; } -static int stub_pd_board_check_request(uint32_t rdo, int pdo_cnt) { - int idx = RDO_POS(rdo); - - /* Check for invalid index */ - return (!idx || idx > pdo_cnt) ? EC_ERROR_INVAL : EC_SUCCESS; -} -int pd_board_check_request(uint32_t, int) __attribute__((weak, alias("stub_pd_board_check_request"))); - #ifdef CONFIG_USB_PD_DUAL_ROLE /* Last received source cap */ -static uint32_t pd_src_caps[CONFIG_USB_PD_PORT_COUNT][PDO_MAX_OBJECTS]; -static uint8_t pd_src_cap_cnt[CONFIG_USB_PD_PORT_COUNT]; +static uint32_t pd_src_caps[PDO_MAX_OBJECTS]; +static uint8_t pd_src_cap_cnt; /* Cap on the max voltage requested as a sink (in millivolts) */ static unsigned max_request_mv = PD_MAX_VOLTAGE_MV; /* no cap */ -int pd_find_pdo_index(int port, int max_mv, uint32_t *selected_pdo) { +int pd_find_pdo_index(int max_mv, uint32_t *selected_pdo) { int i, uw, mv, ma; int ret = 0; int __attribute__((unused)) cur_mv = 0; int cur_uw = 0; int prefer_cur; - const uint32_t *src_caps = pd_src_caps[port]; + const uint32_t *src_caps = pd_src_caps; /* max voltage is always limited by this boards max request */ max_mv = MIN(max_mv, PD_MAX_VOLTAGE_MV); /* Get max power that is under our max voltage input */ - for (i = 0; i < pd_src_cap_cnt[port]; i++) { + for (i = 0; i < pd_src_cap_cnt; i++) { /* its an unsupported Augmented PDO (PD3.0) */ if ((src_caps[i] & PDO_TYPE_MASK) == PDO_TYPE_AUGMENTED) continue; @@ -149,7 +111,7 @@ void pd_extract_pdo_power(uint32_t pdo, uint32_t *ma, uint32_t *mv) { *ma = MIN(max_ma, PD_MAX_CURRENT_MA); } -int pd_build_request(int port, uint32_t *rdo, uint32_t *ma, uint32_t *mv, +int pd_build_request(uint32_t *rdo, uint32_t *ma, uint32_t *mv, enum pd_request_type req_type) { uint32_t pdo; int pdo_index, flags = 0; @@ -160,10 +122,10 @@ int pd_build_request(int port, uint32_t *rdo, uint32_t *ma, uint32_t *mv, if (req_type == PD_REQUEST_VSAFE5V) { /* src cap 0 should be vSafe5V */ pdo_index = 0; - pdo = pd_src_caps[port][0]; + pdo = pd_src_caps[0]; } else { /* find pdo index for max voltage we can request */ - pdo_index = pd_find_pdo_index(port, max_request_mv, &pdo); + pdo_index = pd_find_pdo_index(max_request_mv, &pdo); } pd_extract_pdo_power(pdo, ma, mv); @@ -205,29 +167,29 @@ int pd_build_request(int port, uint32_t *rdo, uint32_t *ma, uint32_t *mv, return EC_SUCCESS; } -void pd_process_source_cap(int port, int cnt, uint32_t *src_caps) { +void pd_process_source_cap(int cnt, uint32_t *src_caps) { #ifdef CONFIG_CHARGE_MANAGER uint32_t ma, mv, pdo; #endif int i; - pd_src_cap_cnt[port] = cnt; + pd_src_cap_cnt = cnt; for (i = 0; i < cnt; i++) - pd_src_caps[port][i] = *src_caps++; + pd_src_caps[i] = *src_caps++; #ifdef CONFIG_CHARGE_MANAGER /* Get max power info that we could request */ - pd_find_pdo_index(port, PD_MAX_VOLTAGE_MV, &pdo); + pd_find_pdo_index( PD_MAX_VOLTAGE_MV, &pdo); pd_extract_pdo_power(pdo, &ma, &mv); /* Set max. limit, but apply 500mA ceiling */ - //charge_manager_set_ceil(port, CEIL_REQUESTOR_PD, PD_MIN_MA); - pd_set_input_current_limit(port, ma, mv); + //charge_manager_set_ceil( CEIL_REQUESTOR_PD, PD_MIN_MA); + pd_set_input_current_limit(ma, mv); #endif } #pragma weak pd_process_source_cap_callback -void pd_process_source_cap_callback(int port, int cnt, uint32_t *src_caps) { +void pd_process_source_cap_callback(int cnt, uint32_t *src_caps) { } void pd_set_max_voltage(unsigned mv) { @@ -257,16 +219,16 @@ static struct pd_policy pe[CONFIG_USB_PD_PORT_COUNT]; void pd_dfp_pe_init(int port) { - memset(&pe[port], 0, sizeof(struct pd_policy)); + memset(&pe, 0, sizeof(struct pd_policy)); } -static void dfp_consume_identity(int port, int cnt, uint32_t *payload) +static void dfp_consume_identity( int cnt, uint32_t *payload) { int ptype = PD_IDH_PTYPE(payload[VDO_I(IDH)]); - size_t identity_size = MIN(sizeof(pe[port].identity), + size_t identity_size = MIN(sizeof(pe.identity), (cnt - 1) * sizeof(uint32_t)); pd_dfp_pe_init(port); - memcpy(&pe[port].identity, payload + 1, identity_size); + memcpy(&pe.identity, payload + 1, identity_size); switch (ptype) { case IDH_PTYPE_AMA: /* TODO(tbroch) do I disable VBUS here if power contract @@ -286,19 +248,19 @@ static void dfp_consume_identity(int port, int cnt, uint32_t *payload) } } -static int dfp_discover_svids(int port, uint32_t *payload) +static int dfp_discover_svids( uint32_t *payload) { payload[0] = VDO(USB_SID_PD, 1, CMD_DISCOVER_SVID); return 1; } -static void dfp_consume_svids(int port, uint32_t *payload) +static void dfp_consume_svids( uint32_t *payload) { int i; uint32_t *ptr = payload + 1; uint16_t svid0, svid1; - for (i = pe[port].svid_cnt; i < pe[port].svid_cnt + 12; i += 2) { + for (i = pe.svid_cnt; i < pe.svid_cnt + 12; i += 2) { if (i == SVID_DISCOVERY_MAX) { CPRINTF("ERR:SVIDCNT\n"); break; @@ -307,14 +269,14 @@ static void dfp_consume_svids(int port, uint32_t *payload) svid0 = PD_VDO_SVID_SVID0(*ptr); if (!svid0) break; - pe[port].svids[i].svid = svid0; - pe[port].svid_cnt++; + pe.svids[i].svid = svid0; + pe.svid_cnt++; svid1 = PD_VDO_SVID_SVID1(*ptr); if (!svid1) break; - pe[port].svids[i + 1].svid = svid1; - pe[port].svid_cnt++; + pe.svids[i + 1].svid = svid1; + pe.svid_cnt++; ptr++; } /* TODO(tbroch) need to re-issue discover svids if > 12 */ @@ -322,65 +284,65 @@ static void dfp_consume_svids(int port, uint32_t *payload) CPRINTF("ERR:SVID+12\n"); } -static int dfp_discover_modes(int port, uint32_t *payload) +static int dfp_discover_modes( uint32_t *payload) { - uint16_t svid = pe[port].svids[pe[port].svid_idx].svid; - if (pe[port].svid_idx >= pe[port].svid_cnt) + uint16_t svid = pe.svids[pe.svid_idx].svid; + if (pe.svid_idx >= pe.svid_cnt) return 0; payload[0] = VDO(svid, 1, CMD_DISCOVER_MODES); return 1; } -static void dfp_consume_modes(int port, int cnt, uint32_t *payload) +static void dfp_consume_modes( int cnt, uint32_t *payload) { - int idx = pe[port].svid_idx; - pe[port].svids[idx].mode_cnt = cnt - 1; - if (pe[port].svids[idx].mode_cnt < 0) { + int idx = pe.svid_idx; + pe.svids[idx].mode_cnt = cnt - 1; + if (pe.svids[idx].mode_cnt < 0) { CPRINTF("ERR:NOMODE\n"); } else { - memcpy(pe[port].svids[pe[port].svid_idx].mode_vdo, &payload[1], - sizeof(uint32_t) * pe[port].svids[idx].mode_cnt); + memcpy(pe.svids[pe.svid_idx].mode_vdo, &payload[1], + sizeof(uint32_t) * pe.svids[idx].mode_cnt); } - pe[port].svid_idx++; + pe.svid_idx++; } -static int get_mode_idx(int port, uint16_t svid) +static int get_mode_idx( uint16_t svid) { int i; for (i = 0; i < PD_AMODE_COUNT; i++) { - if (pe[port].amodes[i].fx->svid == svid) + if (pe.amodes[i].fx->svid == svid) return i; } return -1; } -static struct svdm_amode_data *get_modep(int port, uint16_t svid) +static struct svdm_amode_data *get_modep( uint16_t svid) { - int idx = get_mode_idx(port, svid); + int idx = get_mode_idx( svid); - return (idx == -1) ? NULL : &pe[port].amodes[idx]; + return (idx == -1) ? NULL : &pe.amodes[idx]; } -int pd_alt_mode(int port, uint16_t svid) +int pd_alt_mode( uint16_t svid) { - struct svdm_amode_data *modep = get_modep(port, svid); + struct svdm_amode_data *modep = get_modep( svid); return (modep) ? modep->opos : -1; } -int allocate_mode(int port, uint16_t svid) +int allocate_mode( uint16_t svid) { int i, j; struct svdm_amode_data *modep; - int mode_idx = get_mode_idx(port, svid); + int mode_idx = get_mode_idx( svid); if (mode_idx != -1) return mode_idx; /* There's no space to enter another mode */ - if (pe[port].amode_idx == PD_AMODE_COUNT) { + if (pe.amode_idx == PD_AMODE_COUNT) { CPRINTF("ERR:NO AMODE SPACE\n"); return -1; } @@ -390,17 +352,17 @@ int allocate_mode(int port, uint16_t svid) if (!&supported_modes[i]) continue; - for (j = 0; j < pe[port].svid_cnt; j++) { - struct svdm_svid_data *svidp = &pe[port].svids[j]; + for (j = 0; j < pe.svid_cnt; j++) { + struct svdm_svid_data *svidp = &pe.svids[j]; if ((svidp->svid != supported_modes[i].svid) || (svid && (svidp->svid != svid))) continue; - modep = &pe[port].amodes[pe[port].amode_idx]; + modep = &pe.amodes[pe.amode_idx]; modep->fx = &supported_modes[i]; - modep->data = &pe[port].svids[j]; - pe[port].amode_idx++; - return pe[port].amode_idx - 1; + modep->data = &pe.svids[j]; + pe.amode_idx++; + return pe.amode_idx - 1; } } return -1; @@ -410,15 +372,15 @@ int allocate_mode(int port, uint16_t svid) * Enter default mode ( payload[0] == 0 ) or attempt to enter mode via svid & * opos */ -uint32_t pd_dfp_enter_mode(int port, uint16_t svid, int opos) +uint32_t pd_dfp_enter_mode( uint16_t svid, int opos) { - int mode_idx = allocate_mode(port, svid); + int mode_idx = allocate_mode( svid); struct svdm_amode_data *modep; uint32_t mode_caps; if (mode_idx == -1) return 0; - modep = &pe[port].amodes[mode_idx]; + modep = &pe.amodes[mode_idx]; if (!opos) { /* choose the lowest as default */ @@ -431,7 +393,7 @@ uint32_t pd_dfp_enter_mode(int port, uint16_t svid, int opos) } mode_caps = modep->data->mode_vdo[modep->opos - 1]; - if (modep->fx->enter(port, mode_caps) == -1) + if (modep->fx->enter( mode_caps) == -1) return 0; /* SVDM to send to UFP for mode entry */ @@ -459,17 +421,17 @@ static int validate_mode_request(struct svdm_amode_data *modep, return 1; } -static void dfp_consume_attention(int port, uint32_t *payload) +static void dfp_consume_attention( uint32_t *payload) { uint16_t svid = PD_VDO_VID(payload[0]); int opos = PD_VDO_OPOS(payload[0]); - struct svdm_amode_data *modep = get_modep(port, svid); + struct svdm_amode_data *modep = get_modep( svid); if (!modep || !validate_mode_request(modep, svid, opos)) return; if (modep->fx->attention) - modep->fx->attention(port, payload); + modep->fx->attention( payload); } /* @@ -491,9 +453,9 @@ static void dfp_consume_attention(int port, uint32_t *payload) * output. If UFP is a USB-C receptacle it may assert C/D/E/F. The DFP USB-C * receptacle must always choose C/D in those cases. */ -int pd_dfp_dp_get_pin_mode(int port, uint32_t status) +int pd_dfp_dp_get_pin_mode( uint32_t status) { - struct svdm_amode_data *modep = get_modep(port, USB_SID_DISPLAYPORT); + struct svdm_amode_data *modep = get_modep( USB_SID_DISPLAYPORT); uint32_t mode_caps; uint32_t pin_caps; if (!modep) @@ -522,7 +484,7 @@ int pd_dfp_dp_get_pin_mode(int port, uint32_t status) return 1 << get_next_bit(&pin_caps); } -int pd_dfp_exit_mode(int port, uint16_t svid, int opos) +int pd_dfp_exit_mode( uint16_t svid, int opos) { struct svdm_amode_data *modep; int idx; @@ -534,8 +496,8 @@ int pd_dfp_exit_mode(int port, uint16_t svid, int opos) */ if (!svid) { for (idx = 0; idx < PD_AMODE_COUNT; idx++) - if (pe[port].amodes[idx].fx) - pe[port].amodes[idx].fx->exit(port); + if (pe.amodes[idx].fx) + pe.amodes[idx].fx->exit(port); pd_dfp_pe_init(port); return 0; @@ -547,7 +509,7 @@ int pd_dfp_exit_mode(int port, uint16_t svid, int opos) * to exit all modes. We currently don't have any UFPs that support * multiple modes on one SVID. */ - modep = get_modep(port, svid); + modep = get_modep( svid); if (!modep || !validate_mode_request(modep, svid, opos)) return 0; @@ -560,12 +522,12 @@ int pd_dfp_exit_mode(int port, uint16_t svid, int opos) uint16_t pd_get_identity_vid(int port) { - return PD_IDH_VID(pe[port].identity[0]); + return PD_IDH_VID(pe.identity[0]); } uint16_t pd_get_identity_pid(int port) { - return PD_PRODUCT_PID(pe[port].identity[2]); + return PD_PRODUCT_PID(pe.identity[2]); } #ifdef CONFIG_CMD_USB_PD_PE @@ -579,34 +541,34 @@ static void dump_pe(int port) struct svdm_amode_data *modep; uint32_t mode_caps; - if (pe[port].identity[0] == 0) { + if (pe.identity[0] == 0) { ccprintf("No identity discovered yet.\n"); return; } - idh_ptype = PD_IDH_PTYPE(pe[port].identity[0]); + idh_ptype = PD_IDH_PTYPE(pe.identity[0]); ccprintf("IDENT:\n"); - ccprintf("\t[ID Header] %08x :: %s, VID:%04x\n", pe[port].identity[0], + ccprintf("\t[ID Header] %08x :: %s, VID:%04x\n", pe.identity[0], idh_ptype_names[idh_ptype], pd_get_identity_vid(port)); - ccprintf("\t[Cert Stat] %08x\n", pe[port].identity[1]); - for (i = 2; i < ARRAY_SIZE(pe[port].identity); i++) { + ccprintf("\t[Cert Stat] %08x\n", pe.identity[1]); + for (i = 2; i < ARRAY_SIZE(pe.identity); i++) { ccprintf("\t"); - if (pe[port].identity[i]) - ccprintf("[%d] %08x ", i, pe[port].identity[i]); + if (pe.identity[i]) + ccprintf("[%d] %08x ", i, pe.identity[i]); } ccprintf("\n"); - if (pe[port].svid_cnt < 1) { + if (pe.svid_cnt < 1) { ccprintf("No SVIDS discovered yet.\n"); return; } - for (i = 0; i < pe[port].svid_cnt; i++) { - ccprintf("SVID[%d]: %04x MODES:", i, pe[port].svids[i].svid); - for (j = 0; j < pe[port].svids[j].mode_cnt; j++) + for (i = 0; i < pe.svid_cnt; i++) { + ccprintf("SVID[%d]: %04x MODES:", i, pe.svids[i].svid); + for (j = 0; j < pe.svids[j].mode_cnt; j++) ccprintf(" [%d] %08x", j + 1, - pe[port].svids[i].mode_vdo[j]); + pe.svids[i].mode_vdo[j]); ccprintf("\n"); - modep = get_modep(port, pe[port].svids[i].svid); + modep = get_modep( pe.svids[i].svid); if (modep) { mode_caps = modep->data->mode_vdo[modep->opos - 1]; ccprintf("MODE[%d]: svid:%04x caps:%08x\n", modep->opos, @@ -638,10 +600,10 @@ DECLARE_CONSOLE_COMMAND(pe, command_pe, #endif /* CONFIG_USB_PD_ALT_MODE_DFP */ -int pd_svdm(int port, int cnt, uint32_t *payload, uint32_t **rpayload) { +int pd_svdm(int cnt, uint32_t *payload, uint32_t **rpayload) { int cmd = PD_VDO_CMD(payload[0]); int cmd_type = PD_VDO_CMDT(payload[0]); - int (*func)(int port, uint32_t *payload) = NULL; + int (*func)(uint32_t *payload) = NULL; int rsize = 1; /* VDM header at a minimum */ @@ -677,7 +639,7 @@ int pd_svdm(int port, int cnt, uint32_t *payload, uint32_t **rpayload) { * attention is only SVDM with no response * (just goodCRC) return zero here. */ - dfp_consume_attention(port, payload); + dfp_consume_attention( payload); return 0; #endif default: @@ -685,7 +647,7 @@ int pd_svdm(int port, int cnt, uint32_t *payload, uint32_t **rpayload) { rsize = 0; } if (func) - rsize = func(port, payload); + rsize = func(payload); else /* not supported : NACK it */ rsize = 0; @@ -703,30 +665,30 @@ int pd_svdm(int port, int cnt, uint32_t *payload, uint32_t **rpayload) { #ifdef CONFIG_USB_PD_ALT_MODE_DFP struct svdm_amode_data *modep; - modep = get_modep(port, PD_VDO_VID(payload[0])); + modep = get_modep( PD_VDO_VID(payload[0])); #endif switch (cmd) { #ifdef CONFIG_USB_PD_ALT_MODE_DFP case CMD_DISCOVER_IDENT: - dfp_consume_identity(port, cnt, payload); - rsize = dfp_discover_svids(port, payload); + dfp_consume_identity( cnt, payload); + rsize = dfp_discover_svids( payload); #ifdef CONFIG_CHARGE_MANAGER if (pd_charge_from_device(pd_get_identity_vid(port), pd_get_identity_pid(port))) - charge_manager_update_dualrole(port, + charge_manager_update_dualrole( CAP_DEDICATED); #endif break; case CMD_DISCOVER_SVID: - dfp_consume_svids(port, payload); - rsize = dfp_discover_modes(port, payload); + dfp_consume_svids( payload); + rsize = dfp_discover_modes( payload); break; case CMD_DISCOVER_MODES: - dfp_consume_modes(port, cnt, payload); - rsize = dfp_discover_modes(port, payload); + dfp_consume_modes( cnt, payload); + rsize = dfp_discover_modes( payload); /* enter the default mode for DFP */ if (!rsize) { - payload[0] = pd_dfp_enter_mode(port, 0, 0); + payload[0] = pd_dfp_enter_mode( 0, 0); if (payload[0]) rsize = 1; } @@ -736,10 +698,10 @@ int pd_svdm(int port, int cnt, uint32_t *payload, uint32_t **rpayload) { rsize = 0; } else { if (!modep->opos) - pd_dfp_enter_mode(port, 0, 0); + pd_dfp_enter_mode( 0, 0); if (modep->opos) { - rsize = modep->fx->status(port, + rsize = modep->fx->status( payload); payload[0] |= PD_VDO_OPOS(modep->opos); } @@ -748,9 +710,9 @@ int pd_svdm(int port, int cnt, uint32_t *payload, uint32_t **rpayload) { case CMD_DP_STATUS: /* DP status response & UFP's DP attention have same payload */ - dfp_consume_attention(port, payload); + dfp_consume_attention( payload); if (modep && modep->opos) - rsize = modep->fx->config(port, payload); + rsize = modep->fx->config( payload); else rsize = 0; break; @@ -810,7 +772,7 @@ int pd_svdm(int port, int cnt, uint32_t *payload, uint32_t **rpayload) { #else -int pd_svdm(int port, int cnt, uint32_t *payload, uint32_t **rpayload) +int pd_svdm( int cnt, uint32_t *payload, uint32_t **rpayload) { return 0; } @@ -818,7 +780,7 @@ int pd_svdm(int port, int cnt, uint32_t *payload, uint32_t **rpayload) #endif /* CONFIG_USB_PD_ALT_MODE */ #ifndef CONFIG_USB_PD_CUSTOM_VDM -int pd_vdm(int port, int cnt, uint32_t *payload, uint32_t **rpayload) { +int pd_vdm(int cnt, uint32_t *payload, uint32_t **rpayload) { return 0; } #endif /* !CONFIG_USB_PD_CUSTOM_VDM */ @@ -837,7 +799,6 @@ static void pd_usb_billboard_deferred(void) { #endif } -DECLARE_DEFERRED( pd_usb_billboard_deferred); #ifdef CONFIG_USB_PD_ALT_MODE_DFP static int hc_remote_pd_discovery(struct host_cmd_handler_args *args) @@ -897,110 +858,12 @@ DECLARE_HOST_COMMAND(EC_CMD_USB_PD_GET_AMODE, #define FW_RW_END (CONFIG_EC_WRITABLE_STORAGE_OFF + \ CONFIG_RW_STORAGE_OFF + CONFIG_RW_SIZE) -/* - uint8_t *flash_hash_rw(void) - { - static struct sha256_ctx ctx; - - // re-calculate RW hash when changed as its time consuming - if (rw_flash_changed) { - rw_flash_changed = 0; - SHA256_init(&ctx); - SHA256_update(&ctx, (void *)CONFIG_PROGRAM_MEMORY_BASE + - CONFIG_RW_MEM_OFF, - CONFIG_RW_SIZE - RSANUMBYTES); - return SHA256_final(&ctx); - } else { - return ctx.buf; - } - } - - - void pd_get_info(uint32_t *info_data) - { - void *rw_hash = flash_hash_rw(); - - // copy first 20 bytes of RW hash - memcpy(info_data, rw_hash, 5 * sizeof(uint32_t)); - // copy other info into data msg - #if defined(CONFIG_USB_PD_HW_DEV_ID_BOARD_MAJOR) && \ - defined(CONFIG_USB_PD_HW_DEV_ID_BOARD_MINOR) - info_data[5] = VDO_INFO(CONFIG_USB_PD_HW_DEV_ID_BOARD_MAJOR, - CONFIG_USB_PD_HW_DEV_ID_BOARD_MINOR, - ver_get_numcommits(), - (system_get_image_copy() != SYSTEM_IMAGE_RO)); - #else - info_data[5] = 0; - #endif - } - - int pd_custom_flash_vdm(int port, int cnt, uint32_t *payload) - { - static int flash_offset; - int rsize = 1; // default is just VDM header returned - - switch (PD_VDO_CMD(payload[0])) { - case VDO_CMD_VERSION: - memcpy(payload + 1, ¤t_image_data.version, 24); - rsize = 7; - break; - case VDO_CMD_REBOOT: - // ensure the power supply is in a safe state - pd_power_supply_reset(0); - system_reset(0); - break; - case VDO_CMD_READ_INFO: - // copy info into response - pd_get_info(payload + 1); - rsize = 7; - break; - case VDO_CMD_FLASH_ERASE: - // do not kill the code under our feet - if (system_get_image_copy() != SYSTEM_IMAGE_RO) - break; - pd_log_event(PD_EVENT_ACC_RW_ERASE, 0, 0, NULL); - flash_offset = CONFIG_EC_WRITABLE_STORAGE_OFF + - CONFIG_RW_STORAGE_OFF; - flash_physical_erase(CONFIG_EC_WRITABLE_STORAGE_OFF + - CONFIG_RW_STORAGE_OFF, CONFIG_RW_SIZE); - rw_flash_changed = 1; - break; - case VDO_CMD_FLASH_WRITE: - // do not kill the code under our feet - if ((system_get_image_copy() != SYSTEM_IMAGE_RO) || - (flash_offset < CONFIG_EC_WRITABLE_STORAGE_OFF + - CONFIG_RW_STORAGE_OFF)) - break; - flash_physical_write(flash_offset, 4*(cnt - 1), - (const char *)(payload+1)); - flash_offset += 4*(cnt - 1); - rw_flash_changed = 1; - break; - case VDO_CMD_ERASE_SIG: - // this is not touching the code area - { - uint32_t zero = 0; - int offset; - // zeroes the area containing the RSA signature - for (offset = FW_RW_END - RSANUMBYTES; - offset < FW_RW_END; offset += 4) - flash_physical_write(offset, 4, - (const char *)&zero); - } - break; - default: - // Unknown : do not answer - return 0; - } - return rsize; - } - */ #ifdef CONFIG_USB_PD_DISCHARGE -void pd_set_vbus_discharge(int port, int enable) +void pd_set_vbus_discharge( int enable) { static struct mutex discharge_lock[CONFIG_USB_PD_PORT_COUNT]; - mutex_lock(&discharge_lock[port]); + mutex_lock(&discharge_lock); enable &= !board_vbus_source_enabled(port); #ifdef CONFIG_USB_PD_DISCHARGE_GPIO if (!port) @@ -1010,10 +873,10 @@ void pd_set_vbus_discharge(int port, int enable) gpio_set_level(GPIO_USB_C1_DISCHARGE, enable); #endif /* CONFIG_USB_PD_PORT_COUNT */ #elif defined(CONFIG_USB_PD_DISCHARGE_TCPC) - tcpc_discharge_vbus(port, enable); + tcpc_discharge_vbus( enable); #else #error "PD discharge implementation not defined" #endif - mutex_unlock(&discharge_lock[port]); + mutex_unlock(&discharge_lock); } #endif /* CONFIG_USB_PD_DISCHARGE */ diff --git a/workspace/TS100/Core/Drivers/FUSB302/USBC_PD/usb_pd_protocol.cpp b/workspace/TS100/Core/Drivers/FUSB302/USBC_PD/usb_pd_protocol.cpp index 1b1cc383..1ef96807 100644 --- a/workspace/TS100/Core/Drivers/FUSB302/USBC_PD/usb_pd_protocol.cpp +++ b/workspace/TS100/Core/Drivers/FUSB302/USBC_PD/usb_pd_protocol.cpp @@ -42,17 +42,17 @@ static uint8_t pd_comm_enabled[CONFIG_USB_PD_PORT_COUNT]; #else /* CONFIG_COMMON_RUNTIME */ #define CPRINTF(format, args...) #define CPRINTS(format, args...) -static const int debug_level=0; +static const int debug_level = 0; #endif #ifdef CONFIG_USB_PD_DUAL_ROLE -#define DUAL_ROLE_IF_ELSE(port, sink_clause, src_clause) \ - (pd[port].power_role == PD_ROLE_SINK ? (sink_clause) : (src_clause)) +#define DUAL_ROLE_IF_ELSE( sink_clause, src_clause) \ + (pd.power_role == PD_ROLE_SINK ? (sink_clause) : (src_clause)) #else -#define DUAL_ROLE_IF_ELSE(port, sink_clause, src_clause) (src_clause) +#define DUAL_ROLE_IF_ELSE( sink_clause, src_clause) (src_clause) #endif -#define READY_RETURN_STATE(port) DUAL_ROLE_IF_ELSE(port, PD_STATE_SNK_READY, \ +#define READY_RETURN_STATE() DUAL_ROLE_IF_ELSE( PD_STATE_SNK_READY, \ PD_STATE_SRC_READY) /* Type C supply voltage (mV) */ @@ -126,7 +126,7 @@ static uint64_t next_role_swap = PD_T_DRP_SNK; static int snk_hard_reset_vbus_off = 0; #endif #ifdef CONFIG_USB_PD_DUAL_ROLE_AUTO_TOGGLE -static const int auto_toggle_supported = tcpm_auto_toggle_supported(port); +static const int auto_toggle_supported = tcpm_auto_toggle_supported(); #endif #if defined(CONFIG_CHARGE_MANAGER) static typec_current_t typec_curr = 0, typec_curr_change = 0; @@ -208,7 +208,7 @@ static struct pd_protocol { /* protocol revision */ uint8_t rev; #endif -} pd[CONFIG_USB_PD_PORT_COUNT]; +} pd; #ifdef CONFIG_COMMON_RUNTIME static const char * const pd_state_names[] = { @@ -248,7 +248,7 @@ BUILD_ASSERT(ARRAY_SIZE(pd_state_names) == PD_STATE_COUNT); static struct ec_params_usb_pd_rw_hash_entry rw_hash_table[RW_HASH_ENTRIES]; #endif -int pd_comm_is_enabled(int port) { +int pd_comm_is_enabled() { #ifdef CONFIG_COMMON_RUNTIME return pd_comm_enabled[port]; #else @@ -256,71 +256,71 @@ int pd_comm_is_enabled(int port) { #endif } -static inline void set_state_timeout(int port, uint64_t timeout, +static inline void set_state_timeout(uint64_t timeout, enum pd_states timeout_state) { - pd[port].timeout = timeout; - pd[port].timeout_state = timeout_state; + pd.timeout = timeout; + pd.timeout_state = timeout_state; } #ifdef CONFIG_USB_PD_REV30 -int pd_get_rev(int port) +int pd_get_rev() { - return pd[port].rev; + return pd.rev; } -int pd_get_vdo_ver(int port) +int pd_get_vdo_ver() { - return vdo_ver[pd[port].rev]; + return vdo_ver[pd.rev]; } #endif /* Return flag for pd state is connected */ -int pd_is_connected(int port) { - if (pd[port].task_state == PD_STATE_DISABLED) +int pd_is_connected() { + if (pd.task_state == PD_STATE_DISABLED) return 0; #ifdef CONFIG_USB_PD_DUAL_ROLE_AUTO_TOGGLE - if (pd[port].task_state == PD_STATE_DRP_AUTO_TOGGLE) + if (pd.task_state == PD_STATE_DRP_AUTO_TOGGLE) return 0; #endif - return DUAL_ROLE_IF_ELSE(port, + return DUAL_ROLE_IF_ELSE( /* sink */ - pd[port].task_state != PD_STATE_SNK_DISCONNECTED && pd[port].task_state != PD_STATE_SNK_DISCONNECTED_DEBOUNCE, + pd.task_state != PD_STATE_SNK_DISCONNECTED && pd.task_state != PD_STATE_SNK_DISCONNECTED_DEBOUNCE, /* source */ - pd[port].task_state != PD_STATE_SRC_DISCONNECTED && pd[port].task_state != PD_STATE_SRC_DISCONNECTED_DEBOUNCE); + pd.task_state != PD_STATE_SRC_DISCONNECTED && pd.task_state != PD_STATE_SRC_DISCONNECTED_DEBOUNCE); } /* * Return true if partner port is a DTS or TS capable of entering debug * mode (eg. is presenting Rp/Rp or Rd/Rd). */ -int pd_ts_dts_plugged(int port) { - return pd[port].flags & PD_FLAGS_TS_DTS_PARTNER; +int pd_ts_dts_plugged() { + return pd.flags & PD_FLAGS_TS_DTS_PARTNER; } #ifdef CONFIG_USB_PD_DUAL_ROLE -void pd_vbus_low(int port) { - pd[port].flags &= ~PD_FLAGS_VBUS_NEVER_LOW; +void pd_vbus_low() { + pd.flags &= ~PD_FLAGS_VBUS_NEVER_LOW; } -static inline int pd_is_vbus_present(int port) { +static inline int pd_is_vbus_present() { #ifdef CONFIG_USB_PD_VBUS_DETECT_TCPC - return tcpm_get_vbus_level(port); + return tcpm_get_vbus_level(); #else - return pd_snk_is_vbus_provided(port); + return pd_snk_is_vbus_provided(); #endif } #endif -static inline void set_state(int port, enum pd_states next_state) { - enum pd_states last_state = pd[port].task_state; +static inline void set_state(enum pd_states next_state) { + enum pd_states last_state = pd.task_state; #ifdef CONFIG_LOW_POWER_IDLE int i; #endif - set_state_timeout(port, 0, PD_STATE_DISABLED); - pd[port].task_state = next_state; + set_state_timeout(0, PD_STATE_DISABLED); + pd.task_state = next_state; if (last_state == next_state) return; @@ -329,7 +329,7 @@ static inline void set_state(int port, enum pd_states next_state) { #ifdef CONFIG_USB_PD_DUAL_ROLE_AUTO_TOGGLE /* Clear flag to allow DRP auto toggle when possible */ if (last_state != PD_STATE_DRP_AUTO_TOGGLE) - pd[port].flags &= ~PD_FLAGS_TCPC_DRP_TOGGLE; + pd.flags &= ~PD_FLAGS_TCPC_DRP_TOGGLE; #endif /* Ignore dual-role toggling between sink and source */ @@ -342,56 +342,40 @@ static inline void set_state(int port, enum pd_states next_state) { if (next_state == PD_STATE_SRC_DISCONNECTED || next_state == PD_STATE_SNK_DISCONNECTED) { /* Clear the input current limit */ - pd_set_input_current_limit(port, 0, 0); + pd_set_input_current_limit(0, 0); #ifdef CONFIG_CHARGE_MANAGER - //typec_set_input_current_limit(port, 0, 0); - //charge_manager_set_ceil(port, + //typec_set_input_current_limit( 0, 0); + //charge_manager_set_ceil( // CEIL_REQUESTOR_PD, // CHARGE_CEIL_NONE); #endif #ifdef CONFIG_USBC_VCONN - tcpm_set_vconn(port, 0); + tcpm_set_vconn( 0); #endif #else /* CONFIG_USB_PD_DUAL_ROLE */ if (next_state == PD_STATE_SRC_DISCONNECTED) { #endif - /* - * If we are source, make sure VBUS is off and - * if PD REV3.0, restore RP. - */ - if (pd[port].power_role == PD_ROLE_SOURCE) { - /* - * Rp is restored by pd_power_supply_reset if - * CONFIG_USB_PD_MAX_SINGLE_SOURCE_CURRENT is defined. - */ - pd_power_supply_reset(port); -#if !defined(CONFIG_USB_PD_MAX_SINGLE_SOURCE_CURRENT) && \ - defined(CONFIG_USB_PD_REV30) - /* Restore Rp */ - tcpm_select_rp_value(port, CONFIG_USB_PD_PULLUP); - tcpm_set_cc(port, TYPEC_CC_RP); -#endif - } + #ifdef CONFIG_USB_PD_REV30 /* Adjust rev to highest level*/ - pd[port].rev = PD_REV30; + pd.rev = PD_REV30; #endif #ifdef CONFIG_USB_PD_CHROMEOS - pd[port].dev_id = 0; - pd[port].flags &= ~PD_FLAGS_RESET_ON_DISCONNECT_MASK; + pd.dev_id = 0; + pd.flags &= ~PD_FLAGS_RESET_ON_DISCONNECT_MASK; #endif #ifdef CONFIG_CHARGE_MANAGER - //charge_manager_update_dualrole(port, CAP_UNKNOWN); + //charge_manager_update_dualrole( CAP_UNKNOWN); #endif #ifdef CONFIG_USB_PD_ALT_MODE_DFP - pd_dfp_exit_mode(port, 0, 0); + pd_dfp_exit_mode( 0, 0); #endif #ifdef CONFIG_USBC_SS_MUX - usb_mux_set(port, TYPEC_MUX_NONE, USB_SWITCH_DISCONNECT, - pd[port].polarity); + usb_mux_set( TYPEC_MUX_NONE, USB_SWITCH_DISCONNECT, + pd.polarity); #endif /* Disable TCPC RX */ - tcpm_set_rx_enable(port, 0); + tcpm_set_rx_enable(0); } #ifdef CONFIG_LOW_POWER_IDLE @@ -414,38 +398,38 @@ static inline void set_state(int port, enum pd_states next_state) { } /* increment message ID counter */ -static void inc_id(int port) { - pd[port].msg_id = (pd[port].msg_id + 1) & PD_MESSAGE_ID_COUNT; +static void inc_id() { + pd.msg_id = (pd.msg_id + 1) & PD_MESSAGE_ID_COUNT; } #ifdef CONFIG_USB_PD_REV30 -static void sink_can_xmit(int port, int rp) +static void sink_can_xmit( int rp) { - tcpm_select_rp_value(port, rp); - tcpm_set_cc(port, TYPEC_CC_RP); + tcpm_select_rp_value( rp); + tcpm_set_cc( TYPEC_CC_RP); } -static inline void pd_ca_reset(int port) +static inline void pd_ca_reset() { - pd[port].ca_buffered = 0; + pd.ca_buffered = 0; } #endif -void pd_transmit_complete(int port, int status) { +void pd_transmit_complete(int status) { if (status == TCPC_TX_COMPLETE_SUCCESS) - inc_id(port); + inc_id(); - pd[port].tx_status = status; - //task_set_event(PD_PORT_TO_TASK_ID(port), PD_EVENT_TX, 0); + pd.tx_status = status; + //task_set_event(PD_PORT_TO_TASK_ID(), PD_EVENT_TX, 0); pd_task_set_event(PD_EVENT_TX, 0); } -static int pd_transmit(int port, enum tcpm_transmit_type type, uint16_t header, +static int pd_transmit(enum tcpm_transmit_type type, uint16_t header, const uint32_t *data) { int evt; /* If comms are disabled, do not transmit, return error */ - if (!pd_comm_is_enabled(port)) + if (!pd_comm_is_enabled()) return -1; #ifdef CONFIG_USB_PD_REV30 /* Source-coordinated collision avoidance */ @@ -465,9 +449,9 @@ static int pd_transmit(int port, enum tcpm_transmit_type type, uint16_t header, * * Note: a Sink can still send Hard Reset signaling at any time. */ - if ((pd[port].rev == PD_REV30) && - (pd[port].flags & PD_FLAGS_EXPLICIT_CONTRACT)) { - if (pd[port].power_role == PD_ROLE_SOURCE) { + if ((pd.rev == PD_REV30) && + (pd.flags & PD_FLAGS_EXPLICIT_CONTRACT)) { + if (pd.power_role == PD_ROLE_SOURCE) { /* * Inform Sink that it can't transmit. If a sink * transmition is in progress and a collsion occurs, @@ -475,32 +459,32 @@ static int pd_transmit(int port, enum tcpm_transmit_type type, uint16_t header, * all extended messages are chunked. This effectively * defaults to PD REV 2.0 collision avoidance. */ - sink_can_xmit(port, SINK_TX_NG); + sink_can_xmit( SINK_TX_NG); } else if (type != TCPC_TX_HARD_RESET) { int cc1; int cc2; - tcpm_get_cc(port, &cc1, &cc2); + tcpm_get_cc( &cc1, &cc2); if (cc1 == TYPEC_CC_VOLT_SNK_1_5 || cc2 == TYPEC_CC_VOLT_SNK_1_5) { /* Sink can't transmit now. */ /* Check if message is already buffered. */ - if (pd[port].ca_buffered) + if (pd.ca_buffered) return -1; /* Buffer message and send later. */ - pd[port].ca_type = type; - pd[port].ca_header = header; - memcpy(pd[port].ca_buffer, + pd.ca_type = type; + pd.ca_header = header; + memcpy(pd.ca_buffer, data, sizeof(uint32_t) * PD_HEADER_CNT(header)); - pd[port].ca_buffered = 1; + pd.ca_buffered = 1; return 1; } } } #endif - tcpm_transmit(port, type, header, data); + tcpm_transmit(type, header, data); /* Wait until TX is complete */ // Would wait, except that we're making tcpm_transmit blocking @@ -510,10 +494,10 @@ static int pd_transmit(int port, enum tcpm_transmit_type type, uint16_t header, * If the source just completed a transmit, tell * the sink it can transmit if it wants to. */ - if ((pd[port].rev == PD_REV30) && - (pd[port].power_role == PD_ROLE_SOURCE) && - (pd[port].flags & PD_FLAGS_EXPLICIT_CONTRACT)) { - sink_can_xmit(port, SINK_TX_OK); + if ((pd.rev == PD_REV30) && + (pd.power_role == PD_ROLE_SOURCE) && + (pd.flags & PD_FLAGS_EXPLICIT_CONTRACT)) { + sink_can_xmit( SINK_TX_OK); } #endif @@ -522,50 +506,50 @@ static int pd_transmit(int port, enum tcpm_transmit_type type, uint16_t header, // return -1; /* TODO: give different error condition for failed vs discarded */ - return pd[port].tx_status == TCPC_TX_COMPLETE_SUCCESS ? 1 : -1; + return pd.tx_status == TCPC_TX_COMPLETE_SUCCESS ? 1 : -1; } #ifdef CONFIG_USB_PD_REV30 -static void pd_ca_send_pending(int port) +static void pd_ca_send_pending() { int cc1; int cc2; /* Check if a message has been buffered. */ - if (!pd[port].ca_buffered) + if (!pd.ca_buffered) return; - tcpm_get_cc(port, &cc1, &cc2); + tcpm_get_cc( &cc1, &cc2); if ((cc1 != TYPEC_CC_VOLT_SNK_1_5) && (cc2 != TYPEC_CC_VOLT_SNK_1_5)) - if (pd_transmit(port, pd[port].ca_type, - pd[port].ca_header, - pd[port].ca_buffer) < 0) + if (pd_transmit( pd.ca_type, + pd.ca_header, + pd.ca_buffer) < 0) return; /* Message was sent, so free up the buffer. */ - pd[port].ca_buffered = 0; + pd.ca_buffered = 0; } #endif -static void pd_update_roles(int port) { +static void pd_update_roles() { /* Notify TCPC of role update */ - tcpm_set_msg_header(port, pd[port].power_role, pd[port].data_role); + tcpm_set_msg_header(pd.power_role, pd.data_role); } -static int send_control(int port, int type) { +static int send_control(int type) { int bit_len; - uint16_t header = PD_HEADER(type, pd[port].power_role, pd[port].data_role, - pd[port].msg_id, 0, pd_get_rev(port), 0); + uint16_t header = PD_HEADER(type, pd.power_role, pd.data_role, pd.msg_id, 0, + pd_get_rev(), 0); - bit_len = pd_transmit(port, TCPC_TX_SOP, header, NULL); + bit_len = pd_transmit(TCPC_TX_SOP, header, NULL); if (debug_level >= 2) CPRINTF("CTRL[%d]>%d\n", type, bit_len); return bit_len; } -static int send_source_cap(int port) { +static int send_source_cap() { int bit_len; #if defined(CONFIG_USB_PD_DYNAMIC_SRC_CAP) || \ defined(CONFIG_USB_PD_MAX_SINGLE_SOURCE_CURRENT) @@ -579,14 +563,13 @@ static int send_source_cap(int port) { if (src_pdo_cnt == 0) /* No source capabilities defined, sink only */ - header = PD_HEADER(PD_CTRL_REJECT, pd[port].power_role, - pd[port].data_role, pd[port].msg_id, 0, pd_get_rev(port), 0); + header = PD_HEADER(PD_CTRL_REJECT, pd.power_role, pd.data_role, + pd.msg_id, 0, pd_get_rev(), 0); else - header = PD_HEADER(PD_DATA_SOURCE_CAP, pd[port].power_role, - pd[port].data_role, pd[port].msg_id, src_pdo_cnt, - pd_get_rev(port), 0); + header = PD_HEADER(PD_DATA_SOURCE_CAP, pd.power_role, pd.data_role, + pd.msg_id, src_pdo_cnt, pd_get_rev(), 0); - bit_len = pd_transmit(port, TCPC_TX_SOP, header, src_pdo); + bit_len = pd_transmit(TCPC_TX_SOP, header, src_pdo); if (debug_level >= 2) CPRINTF("srcCAP>%d\n", bit_len); @@ -594,16 +577,16 @@ static int send_source_cap(int port) { } #ifdef CONFIG_USB_PD_REV30 -static int send_battery_cap(int port, uint32_t *payload) +static int send_battery_cap( uint32_t *payload) { int bit_len; uint16_t msg[6] = {0, 0, 0, 0, 0, 0}; uint16_t header = PD_HEADER(PD_EXT_BATTERY_CAP, - pd[port].power_role, - pd[port].data_role, - pd[port].msg_id, + pd.power_role, + pd.data_role, + pd.msg_id, 3, /* Number of Data Objects */ - pd[port].rev, + pd.rev, 1 /* This is an exteded message */ ); @@ -673,22 +656,22 @@ static int send_battery_cap(int port, uint32_t *payload) } } - bit_len = pd_transmit(port, TCPC_TX_SOP, header, (uint32_t *)msg); + bit_len = pd_transmit( TCPC_TX_SOP, header, (uint32_t *)msg); if (debug_level >= 2) CPRINTF("batCap>%d\n", bit_len); return bit_len; } -static int send_battery_status(int port, uint32_t *payload) +static int send_battery_status( uint32_t *payload) { int bit_len; uint32_t msg = 0; uint16_t header = PD_HEADER(PD_DATA_BATTERY_STATUS, - pd[port].power_role, - pd[port].data_role, - pd[port].msg_id, + pd.power_role, + pd.data_role, + pd.msg_id, 1, /* Number of Data Objects */ - pd[port].rev, + pd.rev, 0 /* This is NOT an extended message */ ); @@ -740,7 +723,7 @@ static int send_battery_status(int port, uint32_t *payload) msg = BSDO_CAP(BSDO_CAP_UNKNOWN); } - bit_len = pd_transmit(port, TCPC_TX_SOP, header, &msg); + bit_len = pd_transmit( TCPC_TX_SOP, header, &msg); if (debug_level >= 2) CPRINTF("batStat>%d\n", bit_len); @@ -749,30 +732,30 @@ static int send_battery_status(int port, uint32_t *payload) #endif #ifdef CONFIG_USB_PD_DUAL_ROLE -static void send_sink_cap(int port) { +static void send_sink_cap() { int bit_len; - uint16_t header = PD_HEADER(PD_DATA_SINK_CAP, pd[port].power_role, - pd[port].data_role, pd[port].msg_id, pd_snk_pdo_cnt, - pd_get_rev(port), 0); + uint16_t header = PD_HEADER(PD_DATA_SINK_CAP, pd.power_role, + pd.data_role, pd.msg_id, pd_snk_pdo_cnt, pd_get_rev(), + 0); - bit_len = pd_transmit(port, TCPC_TX_SOP, header, pd_snk_pdo); + bit_len = pd_transmit(TCPC_TX_SOP, header, pd_snk_pdo); if (debug_level >= 2) CPRINTF("snkCAP>%d\n", bit_len); } -static int send_request(int port, uint32_t rdo) { +static int send_request(uint32_t rdo) { int bit_len; - uint16_t header = PD_HEADER(PD_DATA_REQUEST, pd[port].power_role, - pd[port].data_role, pd[port].msg_id, 1, pd_get_rev(port), 0); + uint16_t header = PD_HEADER(PD_DATA_REQUEST, pd.power_role, + pd.data_role, pd.msg_id, 1, pd_get_rev(), 0); - bit_len = pd_transmit(port, TCPC_TX_SOP, header, &rdo); + bit_len = pd_transmit(TCPC_TX_SOP, header, &rdo); if (debug_level >= 2) CPRINTF("REQ%d>\n", bit_len); return bit_len; } #ifdef CONFIG_BBRAM -static int pd_get_saved_active(int port) +static int pd_get_saved_active() { uint8_t val; @@ -784,7 +767,7 @@ static int pd_get_saved_active(int port) return !!val; } -static void pd_set_saved_active(int port, int val) +static void pd_set_saved_active( int val) { if (system_set_bbram(port ? SYSTEM_BBRAM_IDX_PD1 : SYSTEM_BBRAM_IDX_PD0, val)) @@ -794,56 +777,53 @@ static void pd_set_saved_active(int port, int val) #endif /* CONFIG_USB_PD_DUAL_ROLE */ #ifdef CONFIG_COMMON_RUNTIME -static int send_bist_cmd(int port) +static int send_bist_cmd() { /* currently only support sending bist carrier 2 */ uint32_t bdo = BDO(BDO_MODE_CARRIER2, 0); int bit_len; - uint16_t header = PD_HEADER(PD_DATA_BIST, pd[port].power_role, - pd[port].data_role, pd[port].msg_id, 1, - pd_get_rev(port), 0); + uint16_t header = PD_HEADER(PD_DATA_BIST, pd.power_role, + pd.data_role, pd.msg_id, 1, + pd_get_rev(), 0); - bit_len = pd_transmit(port, TCPC_TX_SOP, header, &bdo); + bit_len = pd_transmit( TCPC_TX_SOP, header, &bdo); CPRINTF("BIST>%d\n", bit_len); return bit_len; } #endif -static void queue_vdm(int port, uint32_t *header, const uint32_t *data, - int data_cnt) { - pd[port].vdo_count = data_cnt + 1; - pd[port].vdo_data[0] = header[0]; - memcpy(&pd[port].vdo_data[1], data, sizeof(uint32_t) * data_cnt); +static void queue_vdm(uint32_t *header, const uint32_t *data, int data_cnt) { + pd.vdo_count = data_cnt + 1; + pd.vdo_data[0] = header[0]; + memcpy(&pd.vdo_data[1], data, sizeof(uint32_t) * data_cnt); /* Set ready, pd task will actually send */ - pd[port].vdm_state = VDM_STATE_READY; + pd.vdm_state = VDM_STATE_READY; } -static void handle_vdm_request(int port, int cnt, uint32_t *payload) { +static void handle_vdm_request(int cnt, uint32_t *payload) { int rlen = 0; uint32_t *rdata; - if (pd[port].vdm_state == VDM_STATE_BUSY) { + if (pd.vdm_state == VDM_STATE_BUSY) { /* If UFP responded busy retry after timeout */ if (PD_VDO_CMDT(payload[0]) == CMDT_RSP_BUSY) { - pd[port].vdm_timeout.val = get_time().val + + pd.vdm_timeout.val = get_time().val + PD_T_VDM_BUSY; - pd[port].vdm_state = VDM_STATE_WAIT_RSP_BUSY; - pd[port].vdo_retry = (payload[0] & ~VDO_CMDT_MASK) | + pd.vdm_state = VDM_STATE_WAIT_RSP_BUSY; + pd.vdo_retry = (payload[0] & ~VDO_CMDT_MASK) | CMDT_INIT; return; } else { - pd[port].vdm_state = VDM_STATE_DONE; + pd.vdm_state = VDM_STATE_DONE; } } if (PD_VDO_SVDM(payload[0])) - rlen = pd_svdm(port, cnt, payload, &rdata); - else - rlen = pd_custom_vdm(port, cnt, payload, &rdata); + rlen = pd_svdm(cnt, payload, &rdata); if (rlen > 0) { - queue_vdm(port, rdata, &rdata[1], rlen - 1); + queue_vdm(rdata, &rdata[1], rlen - 1); return; } if (debug_level >= 2) @@ -851,75 +831,73 @@ static void handle_vdm_request(int port, int cnt, uint32_t *payload) { PD_VDO_VID(payload[0]), payload[0] & 0xFFFF); } -void pd_execute_hard_reset(int port) { - if (pd[port].last_state == PD_STATE_HARD_RESET_SEND) +void pd_execute_hard_reset() { + if (pd.last_state == PD_STATE_HARD_RESET_SEND) CPRINTF("C%d HARD RST TX\n", port); else CPRINTF("C%d HARD RST RX\n", port); - pd[port].msg_id = 0; + pd.msg_id = 0; #ifdef CONFIG_USB_PD_ALT_MODE_DFP - pd_dfp_exit_mode(port, 0, 0); + pd_dfp_exit_mode( 0, 0); #endif #ifdef CONFIG_USB_PD_REV30 - pd[port].rev = PD_REV30; - pd_ca_reset(port); + pd.rev = PD_REV30; + pd_ca_reset(); #endif /* * Fake set last state to hard reset to make sure that the next * state to run knows that we just did a hard reset. */ - pd[port].last_state = PD_STATE_HARD_RESET_EXECUTE; + pd.last_state = PD_STATE_HARD_RESET_EXECUTE; #ifdef CONFIG_USB_PD_DUAL_ROLE /* * If we are swapping to a source and have changed to Rp, restore back * to Rd and turn off vbus to match our power_role. */ - if (pd[port].task_state == PD_STATE_SNK_SWAP_STANDBY - || pd[port].task_state == PD_STATE_SNK_SWAP_COMPLETE) { - tcpm_set_cc(port, TYPEC_CC_RD); - pd_power_supply_reset(port); + if (pd.task_state == PD_STATE_SNK_SWAP_STANDBY + || pd.task_state == PD_STATE_SNK_SWAP_COMPLETE) { + tcpm_set_cc(TYPEC_CC_RD); + pd_power_supply_reset(); } - if (pd[port].power_role == PD_ROLE_SINK) { + if (pd.power_role == PD_ROLE_SINK) { /* Clear the input current limit */ - pd_set_input_current_limit(port, 0, 0); + pd_set_input_current_limit(0, 0); #ifdef CONFIG_CHARGE_MANAGER - //charge_manager_set_ceil(port, + //charge_manager_set_ceil( // CEIL_REQUESTOR_PD, // CHARGE_CEIL_NONE); #endif /* CONFIG_CHARGE_MANAGER */ - set_state(port, PD_STATE_SNK_HARD_RESET_RECOVER); + set_state (PD_STATE_SNK_HARD_RESET_RECOVER); return; } #endif /* CONFIG_USB_PD_DUAL_ROLE */ /* We are a source, cut power */ - pd_power_supply_reset(port); - pd[port].src_recover = get_time().val + PD_T_SRC_RECOVER; - set_state(port, PD_STATE_SRC_HARD_RESET_RECOVER); + pd_power_supply_reset(); + pd.src_recover = get_time().val + PD_T_SRC_RECOVER; + set_state(PD_STATE_SRC_HARD_RESET_RECOVER); } -static void execute_soft_reset(int port) { - pd[port].msg_id = 0; - set_state(port, - DUAL_ROLE_IF_ELSE(port, PD_STATE_SNK_DISCOVERY, - PD_STATE_SRC_DISCOVERY)); +static void execute_soft_reset() { + pd.msg_id = 0; + set_state( + DUAL_ROLE_IF_ELSE(PD_STATE_SNK_DISCOVERY, PD_STATE_SRC_DISCOVERY)); CPRINTF("C%d Soft Rst\n", port); } void pd_soft_reset(void) { int i; - for (i = 0; i < CONFIG_USB_PD_PORT_COUNT; ++i) - if (pd_is_connected(i)) { - set_state(i, PD_STATE_SOFT_RESET); - // getting rid of task stuff - //task_wake(PD_PORT_TO_TASK_ID(i)); - } + if (pd_is_connected()) { + set_state(PD_STATE_SOFT_RESET); + // getting rid of task stuff + //task_wake(PD_PORT_TO_TASK_ID(i)); + } } #ifdef CONFIG_USB_PD_DUAL_ROLE @@ -927,7 +905,7 @@ void pd_soft_reset(void) { * Request desired charge voltage from source. * Returns EC_SUCCESS on success or non-zero on failure. */ -static int pd_send_request_msg(int port, int always_send_request) { +static int pd_send_request_msg(int always_send_request) { uint32_t rdo, curr_limit, supply_voltage; int res; @@ -945,14 +923,14 @@ static int pd_send_request_msg(int port, int always_send_request) { #endif /* Clear new power request */ - pd[port].new_power_request = 0; + pd.new_power_request = 0; /* Build and send request RDO */ /* * If this port is not actively charging or we are not allowed to * request the max voltage, then select vSafe5V */ - res = pd_build_request(port, &rdo, &curr_limit, &supply_voltage, + res = pd_build_request(&rdo, &curr_limit, &supply_voltage, charging && max_request_allowed ? PD_REQUEST_MAX : PD_REQUEST_VSAFE5V); @@ -965,38 +943,38 @@ static int pd_send_request_msg(int port, int always_send_request) { if (!always_send_request) { /* Don't re-request the same voltage */ - if (pd[port].prev_request_mv == supply_voltage) + if (pd.prev_request_mv == supply_voltage) return EC_SUCCESS; #ifdef CONFIG_CHARGE_MANAGER /* Limit current to PD_MIN_MA during transition */ //else - // charge_manager_force_ceil(port, PD_MIN_MA); + // charge_manager_force_ceil( PD_MIN_MA); #endif } CPRINTF("Req C%d [%d] %dmV %dmA", port, RDO_POS(rdo), supply_voltage, curr_limit); if (rdo & RDO_CAP_MISMATCH) - CPRINTF(" Mismatch"); CPRINTF("\n"); + CPRINTF(" Mismatch");CPRINTF("\n"); - pd[port].curr_limit = curr_limit; - pd[port].supply_voltage = supply_voltage; - pd[port].prev_request_mv = supply_voltage; - res = send_request(port, rdo); + pd.curr_limit = curr_limit; + pd.supply_voltage = supply_voltage; + pd.prev_request_mv = supply_voltage; + res = send_request(rdo); if (res < 0) return res; - set_state(port, PD_STATE_SNK_REQUESTED); + set_state (PD_STATE_SNK_REQUESTED); return EC_SUCCESS; } #endif -static void pd_update_pdo_flags(int port, uint32_t pdo) { +static void pd_update_pdo_flags(uint32_t pdo) { #ifdef CONFIG_CHARGE_MANAGER #ifdef CONFIG_USB_PD_ALT_MODE_DFP int charge_whitelisted = - (pd[port].power_role == PD_ROLE_SINK && - pd_charge_from_device(pd_get_identity_vid(port), - pd_get_identity_pid(port))); + (pd.power_role == PD_ROLE_SINK && + pd_charge_from_device(pd_get_identity_vid(), + pd_get_identity_pid())); #else const int charge_whitelisted = 0; #endif @@ -1008,25 +986,25 @@ static void pd_update_pdo_flags(int port, uint32_t pdo) { #ifdef CONFIG_USB_PD_DUAL_ROLE if (pdo & PDO_FIXED_DUAL_ROLE) - pd[port].flags |= PD_FLAGS_PARTNER_DR_POWER; + pd.flags |= PD_FLAGS_PARTNER_DR_POWER; else - pd[port].flags &= ~PD_FLAGS_PARTNER_DR_POWER; + pd.flags &= ~PD_FLAGS_PARTNER_DR_POWER; if (pdo & PDO_FIXED_EXTERNAL) - pd[port].flags |= PD_FLAGS_PARTNER_EXTPOWER; + pd.flags |= PD_FLAGS_PARTNER_EXTPOWER; else - pd[port].flags &= ~PD_FLAGS_PARTNER_EXTPOWER; + pd.flags &= ~PD_FLAGS_PARTNER_EXTPOWER; if (pdo & PDO_FIXED_COMM_CAP) - pd[port].flags |= PD_FLAGS_PARTNER_USB_COMM; + pd.flags |= PD_FLAGS_PARTNER_USB_COMM; else - pd[port].flags &= ~PD_FLAGS_PARTNER_USB_COMM; + pd.flags &= ~PD_FLAGS_PARTNER_USB_COMM; #endif if (pdo & PDO_FIXED_DATA_SWAP) - pd[port].flags |= PD_FLAGS_PARTNER_DR_DATA; + pd.flags |= PD_FLAGS_PARTNER_DR_DATA; else - pd[port].flags &= ~PD_FLAGS_PARTNER_DR_DATA; + pd.flags &= ~PD_FLAGS_PARTNER_DR_DATA; #ifdef CONFIG_CHARGE_MANAGER /* @@ -1036,128 +1014,91 @@ static void pd_update_pdo_flags(int port, uint32_t pdo) { * charging white-list. */ /* - if (!(pd[port].flags & PD_FLAGS_PARTNER_DR_POWER) || - (pd[port].flags & PD_FLAGS_PARTNER_EXTPOWER) || + if (!(pd.flags & PD_FLAGS_PARTNER_DR_POWER) || + (pd.flags & PD_FLAGS_PARTNER_EXTPOWER) || charge_whitelisted) - charge_manager_update_dualrole(port, CAP_DEDICATED); + charge_manager_update_dualrole( CAP_DEDICATED); else - charge_manager_update_dualrole(port, CAP_DUALROLE); + charge_manager_update_dualrole( CAP_DUALROLE); */ #endif } -static void handle_data_request(int port, uint16_t head, uint32_t *payload) { +static void handle_data_request(uint16_t head, uint32_t *payload) { int type = PD_HEADER_TYPE(head); int cnt = PD_HEADER_CNT(head); switch (type) { #ifdef CONFIG_USB_PD_DUAL_ROLE case PD_DATA_SOURCE_CAP: - if ((pd[port].task_state == PD_STATE_SNK_DISCOVERY) - || (pd[port].task_state == PD_STATE_SNK_TRANSITION) + if ((pd.task_state == PD_STATE_SNK_DISCOVERY) + || (pd.task_state == PD_STATE_SNK_TRANSITION) #ifdef CONFIG_USB_PD_VBUS_DETECT_NONE - || (pd[port].task_state == + || (pd.task_state == PD_STATE_SNK_HARD_RESET_RECOVER) #endif - || (pd[port].task_state == PD_STATE_SNK_READY)) { + || (pd.task_state == PD_STATE_SNK_READY)) { #ifdef CONFIG_USB_PD_REV30 /* * Only adjust sink rev if source rev is higher. */ - if (PD_HEADER_REV(head) < pd[port].rev) - pd[port].rev = PD_HEADER_REV(head); + if (PD_HEADER_REV(head) < pd.rev) + pd.rev = PD_HEADER_REV(head); #endif /* Port partner is now known to be PD capable */ - pd[port].flags |= PD_FLAGS_PREVIOUS_PD_CONN; + pd.flags |= PD_FLAGS_PREVIOUS_PD_CONN; /* src cap 0 should be fixed PDO */ - pd_update_pdo_flags(port, payload[0]); + pd_update_pdo_flags(payload[0]); - pd_process_source_cap(port, cnt, payload); + pd_process_source_cap(cnt, payload); /* Source will resend source cap on failure */ - pd_send_request_msg(port, 1); + pd_send_request_msg(1); // We call the callback after we send the request // because the timing on Request seems to be sensitive // User code can take the time until PS_RDY to do stuff - pd_process_source_cap_callback(port, cnt, payload); + pd_process_source_cap_callback(cnt, payload); } break; #endif /* CONFIG_USB_PD_DUAL_ROLE */ case PD_DATA_REQUEST: - if ((pd[port].power_role == PD_ROLE_SOURCE) && (cnt == 1)) { -#ifdef CONFIG_USB_PD_REV30 - /* - * Adjust the rev level to what the sink supports. If - * they're equal, no harm done. - */ - pd[port].rev = PD_HEADER_REV(head); -#endif - if (!pd_check_requested_voltage(payload[0], port)) { - if (send_control(port, PD_CTRL_ACCEPT) < 0) - /* - * if we fail to send accept, do - * nothing and let sink timeout and - * send hard reset - */ - return; - /* explicit contract is now in place */ - pd[port].flags |= PD_FLAGS_EXPLICIT_CONTRACT; -#ifdef CONFIG_USB_PD_REV30 - /* - * Start Source-coordinated collision - * avoidance - */ - if (pd[port].rev == PD_REV30 && - pd[port].power_role == PD_ROLE_SOURCE) - sink_can_xmit(port, SINK_TX_OK); -#endif -#ifdef CONFIG_USB_PD_DUAL_ROLE -#ifdef CONFIG_BBRAM - pd_set_saved_active(port, 1); -#endif -#endif - pd[port].requested_idx = RDO_POS(payload[0]); - set_state(port, PD_STATE_SRC_ACCEPTED); - return; - } - } /* the message was incorrect or cannot be satisfied */ - send_control(port, PD_CTRL_REJECT); + send_control(PD_CTRL_REJECT); /* keep last contract in place (whether implicit or explicit) */ - set_state(port, PD_STATE_SRC_READY); + set_state(PD_STATE_SRC_READY); break; case PD_DATA_BIST: /* If not in READY state, then don't start BIST */ - if (DUAL_ROLE_IF_ELSE(port, pd[port].task_state == PD_STATE_SNK_READY, - pd[port].task_state == PD_STATE_SRC_READY)) { + if (DUAL_ROLE_IF_ELSE(pd.task_state == PD_STATE_SNK_READY, + pd.task_state == PD_STATE_SRC_READY)) { /* currently only support sending bist carrier mode 2 */ if ((payload[0] >> 28) == 5) { /* bist data object mode is 2 */ - pd_transmit(port, TCPC_TX_BIST_MODE_2, 0, + pd_transmit(TCPC_TX_BIST_MODE_2, 0, NULL); /* Set to appropriate port disconnected state */ - set_state(port, - DUAL_ROLE_IF_ELSE(port, PD_STATE_SNK_DISCONNECTED, + set_state( + DUAL_ROLE_IF_ELSE(PD_STATE_SNK_DISCONNECTED, PD_STATE_SRC_DISCONNECTED)); } } break; case PD_DATA_SINK_CAP: - pd[port].flags |= PD_FLAGS_SNK_CAP_RECVD; + pd.flags |= PD_FLAGS_SNK_CAP_RECVD; /* snk cap 0 should be fixed PDO */ - pd_update_pdo_flags(port, payload[0]); - if (pd[port].task_state == PD_STATE_SRC_GET_SINK_CAP) - set_state(port, PD_STATE_SRC_READY); + pd_update_pdo_flags(payload[0]); + if (pd.task_state == PD_STATE_SRC_GET_SINK_CAP) + set_state(PD_STATE_SRC_READY); break; #ifdef CONFIG_USB_PD_REV30 case PD_DATA_BATTERY_STATUS: break; #endif case PD_DATA_VENDOR_DEF: - handle_vdm_request(port, cnt, payload); + handle_vdm_request(cnt, payload); break; default: CPRINTF("Unhandled data message type %d\n", type); @@ -1165,50 +1106,50 @@ static void handle_data_request(int port, uint16_t head, uint32_t *payload) { } #ifdef CONFIG_USB_PD_DUAL_ROLE -void pd_request_power_swap(int port) { - if (pd[port].task_state == PD_STATE_SRC_READY) - set_state(port, PD_STATE_SRC_SWAP_INIT); - else if (pd[port].task_state == PD_STATE_SNK_READY) - set_state(port, PD_STATE_SNK_SWAP_INIT); +void pd_request_power_swap() { + if (pd.task_state == PD_STATE_SRC_READY) + set_state (PD_STATE_SRC_SWAP_INIT); + else if (pd.task_state == PD_STATE_SNK_READY) + set_state (PD_STATE_SNK_SWAP_INIT); // getting rid of task stuff - //task_wake(PD_PORT_TO_TASK_ID(port)); + //task_wake(PD_PORT_TO_TASK_ID()); } #ifdef CONFIG_USBC_VCONN_SWAP -static void pd_request_vconn_swap(int port) +static void pd_request_vconn_swap() { - if (pd[port].task_state == PD_STATE_SRC_READY || - pd[port].task_state == PD_STATE_SNK_READY) - set_state(port, PD_STATE_VCONN_SWAP_SEND); + if (pd.task_state == PD_STATE_SRC_READY || + pd.task_state == PD_STATE_SNK_READY) + set_state( PD_STATE_VCONN_SWAP_SEND); // getting rid of task stuff - //task_wake(PD_PORT_TO_TASK_ID(port)); + //task_wake(PD_PORT_TO_TASK_ID()); } -void pd_try_vconn_src(int port) +void pd_try_vconn_src() { /* * If we don't currently provide vconn, and we can supply it, send * a vconn swap request. */ - if (!(pd[port].flags & PD_FLAGS_VCONN_ON)) { - if (pd_check_vconn_swap(port)) - pd_request_vconn_swap(port); + if (!(pd.flags & PD_FLAGS_VCONN_ON)) { + if (pd_check_vconn_swap()) + pd_request_vconn_swap(); } } #endif #endif /* CONFIG_USB_PD_DUAL_ROLE */ -void pd_request_data_swap(int port) { - if (DUAL_ROLE_IF_ELSE(port, pd[port].task_state == PD_STATE_SNK_READY, - pd[port].task_state == PD_STATE_SRC_READY)) - set_state(port, PD_STATE_DR_SWAP); +void pd_request_data_swap() { + if (DUAL_ROLE_IF_ELSE(pd.task_state == PD_STATE_SNK_READY, + pd.task_state == PD_STATE_SRC_READY)) + set_state(PD_STATE_DR_SWAP); // getting rid of task stuff - //task_wake(PD_PORT_TO_TASK_ID(port)); + //task_wake(PD_PORT_TO_TASK_ID()); } -static void pd_set_data_role(int port, int role) { - pd[port].data_role = role; - pd_execute_data_swap(port, role); +static void pd_set_data_role(int role) { + pd.data_role = role; + pd_execute_data_swap(role); #ifdef CONFIG_USBC_SS_MUX #ifdef CONFIG_USBC_SS_MUX_DFP_ONLY @@ -1217,25 +1158,25 @@ static void pd_set_data_role(int port, int role) { * If new data role is UFP, then disconnect the SS mux. */ if (role == PD_ROLE_DFP) - usb_mux_set(port, TYPEC_MUX_USB, USB_SWITCH_CONNECT, - pd[port].polarity); + usb_mux_set( TYPEC_MUX_USB, USB_SWITCH_CONNECT, + pd.polarity); else - usb_mux_set(port, TYPEC_MUX_NONE, USB_SWITCH_DISCONNECT, - pd[port].polarity); + usb_mux_set( TYPEC_MUX_NONE, USB_SWITCH_DISCONNECT, + pd.polarity); #else - usb_mux_set(port, TYPEC_MUX_USB, USB_SWITCH_CONNECT, - pd[port].polarity); + usb_mux_set( TYPEC_MUX_USB, USB_SWITCH_CONNECT, + pd.polarity); #endif #endif - pd_update_roles(port); + pd_update_roles(); } -static void pd_dr_swap(int port) { - pd_set_data_role(port, !pd[port].data_role); - pd[port].flags |= PD_FLAGS_CHECK_IDENTITY; +static void pd_dr_swap() { + pd_set_data_role(!pd.data_role); + pd.flags |= PD_FLAGS_CHECK_IDENTITY; } -static void handle_ctrl_request(int port, uint16_t head, uint32_t *payload) { +static void handle_ctrl_request(uint16_t head, uint32_t *payload) { int type = PD_HEADER_TYPE(head); int res; @@ -1247,21 +1188,21 @@ static void handle_ctrl_request(int port, uint16_t head, uint32_t *payload) { /* Nothing else to do */ break; case PD_CTRL_GET_SOURCE_CAP: - res = send_source_cap(port); - if ((res >= 0) && (pd[port].task_state == PD_STATE_SRC_DISCOVERY)) - set_state(port, PD_STATE_SRC_NEGOCIATE); + res = send_source_cap(); + if ((res >= 0) && (pd.task_state == PD_STATE_SRC_DISCOVERY)) + set_state(PD_STATE_SRC_NEGOCIATE); break; case PD_CTRL_GET_SINK_CAP: #ifdef CONFIG_USB_PD_DUAL_ROLE - send_sink_cap(port); + send_sink_cap(); #else - send_control(port, REFUSE(pd[port].rev)); + send_control(REFUSE(pd.rev)); #endif break; #ifdef CONFIG_USB_PD_DUAL_ROLE case PD_CTRL_GOTO_MIN: #ifdef CONFIG_USB_PD_GIVE_BACK - if (pd[port].task_state == PD_STATE_SNK_READY) { + if (pd.task_state == PD_STATE_SNK_READY) { /* * Reduce power consumption now! * @@ -1269,63 +1210,63 @@ static void handle_ctrl_request(int port, uint16_t head, uint32_t *payload) { * by sending a new source cap message at a * later time. */ - pd_snk_give_back(port, &pd[port].curr_limit, - &pd[port].supply_voltage); - set_state(port, PD_STATE_SNK_TRANSITION); + pd_snk_give_back( &pd.curr_limit, + &pd.supply_voltage); + set_state( PD_STATE_SNK_TRANSITION); } #endif break; case PD_CTRL_PS_RDY: - if (pd[port].task_state == PD_STATE_SNK_SWAP_SRC_DISABLE) { - set_state(port, PD_STATE_SNK_SWAP_STANDBY); - } else if (pd[port].task_state == PD_STATE_SRC_SWAP_STANDBY) { + if (pd.task_state == PD_STATE_SNK_SWAP_SRC_DISABLE) { + set_state (PD_STATE_SNK_SWAP_STANDBY); + } else if (pd.task_state == PD_STATE_SRC_SWAP_STANDBY) { /* reset message ID and swap roles */ - pd[port].msg_id = 0; - pd[port].power_role = PD_ROLE_SINK; - pd_update_roles(port); - set_state(port, PD_STATE_SNK_DISCOVERY); + pd.msg_id = 0; + pd.power_role = PD_ROLE_SINK; + pd_update_roles(); + set_state (PD_STATE_SNK_DISCOVERY); #ifdef CONFIG_USBC_VCONN_SWAP - } else if (pd[port].task_state == PD_STATE_VCONN_SWAP_INIT) { + } else if (pd.task_state == PD_STATE_VCONN_SWAP_INIT) { /* * If VCONN is on, then this PS_RDY tells us it's * ok to turn VCONN off */ - if (pd[port].flags & PD_FLAGS_VCONN_ON) - set_state(port, PD_STATE_VCONN_SWAP_READY); + if (pd.flags & PD_FLAGS_VCONN_ON) + set_state( PD_STATE_VCONN_SWAP_READY); #endif - } else if (pd[port].task_state == PD_STATE_SNK_DISCOVERY) { + } else if (pd.task_state == PD_STATE_SNK_DISCOVERY) { /* Don't know what power source is ready. Reset. */ - set_state(port, PD_STATE_HARD_RESET_SEND); - } else if (pd[port].task_state == PD_STATE_SNK_SWAP_STANDBY) { + set_state(PD_STATE_HARD_RESET_SEND); + } else if (pd.task_state == PD_STATE_SNK_SWAP_STANDBY) { /* Do nothing, assume this is a redundant PD_RDY */ - } else if (pd[port].power_role == PD_ROLE_SINK) { - set_state(port, PD_STATE_SNK_READY); - pd_set_input_current_limit(port, pd[port].curr_limit, - pd[port].supply_voltage); + } else if (pd.power_role == PD_ROLE_SINK) { + set_state (PD_STATE_SNK_READY); + pd_set_input_current_limit(pd.curr_limit, + pd.supply_voltage); #ifdef CONFIG_CHARGE_MANAGER /* Set ceiling based on what's negotiated */ - //charge_manager_set_ceil(port, + //charge_manager_set_ceil( // CEIL_REQUESTOR_PD, - // pd[port].curr_limit); + // pd.curr_limit); #endif } break; #endif case PD_CTRL_REJECT: case PD_CTRL_WAIT: - if (pd[port].task_state == PD_STATE_DR_SWAP) - set_state(port, READY_RETURN_STATE(port)); + if (pd.task_state == PD_STATE_DR_SWAP) + set_state(READY_RETURN_STATE()); #ifdef CONFIG_USBC_VCONN_SWAP - else if (pd[port].task_state == PD_STATE_VCONN_SWAP_SEND) - set_state(port, READY_RETURN_STATE(port)); + else if (pd.task_state == PD_STATE_VCONN_SWAP_SEND) + set_state( READY_RETURN_STATE()); #endif #ifdef CONFIG_USB_PD_DUAL_ROLE - else if (pd[port].task_state == PD_STATE_SRC_SWAP_INIT) - set_state(port, PD_STATE_SRC_READY); - else if (pd[port].task_state == PD_STATE_SNK_SWAP_INIT) - set_state(port, PD_STATE_SNK_READY); - else if (pd[port].task_state == PD_STATE_SNK_REQUESTED) { + else if (pd.task_state == PD_STATE_SRC_SWAP_INIT) + set_state(PD_STATE_SRC_READY); + else if (pd.task_state == PD_STATE_SNK_SWAP_INIT) + set_state (PD_STATE_SNK_READY); + else if (pd.task_state == PD_STATE_SNK_REQUESTED) { /* * Explicit Contract in place * @@ -1341,158 +1282,158 @@ static void handle_ctrl_request(int port, uint16_t head, uint32_t *payload) { * On reception of a WAIT or REJECT message, * transition to PD_STATE_SNK_DISCOVERY */ - if (pd[port].flags & PD_FLAGS_EXPLICIT_CONTRACT) { + if (pd.flags & PD_FLAGS_EXPLICIT_CONTRACT) { /* We have an explicit contract */ if (type == PD_CTRL_WAIT) { /* * Trigger a new power request when * we enter PD_STATE_SNK_READY */ - pd[port].new_power_request = 1; + pd.new_power_request = 1; /* * After the request is triggered, * make sure the request is sent. */ - pd[port].prev_request_mv = 0; + pd.prev_request_mv = 0; /* * Transition to PD_STATE_SNK_READY * after PD_T_SINK_REQUEST ms. */ - set_state_timeout(port, get_time().val + + set_state_timeout(get_time().val + PD_T_SINK_REQUEST, PD_STATE_SNK_READY); } else { /* The request was rejected */ - set_state(port, PD_STATE_SNK_READY); + set_state(PD_STATE_SNK_READY); } } else { /* No explicit contract */ - set_state(port, PD_STATE_SNK_DISCOVERY); + set_state (PD_STATE_SNK_DISCOVERY); } } #endif break; case PD_CTRL_ACCEPT: - if (pd[port].task_state == PD_STATE_SOFT_RESET) { + if (pd.task_state == PD_STATE_SOFT_RESET) { /* * For the case that we sent soft reset in SNK_DISCOVERY * on startup due to VBUS never low, clear the flag. */ - pd[port].flags &= ~PD_FLAGS_VBUS_NEVER_LOW; - execute_soft_reset(port); - } else if (pd[port].task_state == PD_STATE_DR_SWAP) { + pd.flags &= ~PD_FLAGS_VBUS_NEVER_LOW; + execute_soft_reset(); + } else if (pd.task_state == PD_STATE_DR_SWAP) { /* switch data role */ - pd_dr_swap(port); - set_state(port, READY_RETURN_STATE(port)); + pd_dr_swap(); + set_state(READY_RETURN_STATE()); #ifdef CONFIG_USB_PD_DUAL_ROLE #ifdef CONFIG_USBC_VCONN_SWAP - } else if (pd[port].task_state == PD_STATE_VCONN_SWAP_SEND) { + } else if (pd.task_state == PD_STATE_VCONN_SWAP_SEND) { /* switch vconn */ - set_state(port, PD_STATE_VCONN_SWAP_INIT); + set_state( PD_STATE_VCONN_SWAP_INIT); #endif - } else if (pd[port].task_state == PD_STATE_SRC_SWAP_INIT) { + } else if (pd.task_state == PD_STATE_SRC_SWAP_INIT) { /* explicit contract goes away for power swap */ - pd[port].flags &= ~PD_FLAGS_EXPLICIT_CONTRACT; - set_state(port, PD_STATE_SRC_SWAP_SNK_DISABLE); - } else if (pd[port].task_state == PD_STATE_SNK_SWAP_INIT) { + pd.flags &= ~PD_FLAGS_EXPLICIT_CONTRACT; + set_state (PD_STATE_SRC_SWAP_SNK_DISABLE); + } else if (pd.task_state == PD_STATE_SNK_SWAP_INIT) { /* explicit contract goes away for power swap */ - pd[port].flags &= ~PD_FLAGS_EXPLICIT_CONTRACT; - set_state(port, PD_STATE_SNK_SWAP_SNK_DISABLE); - } else if (pd[port].task_state == PD_STATE_SNK_REQUESTED) { + pd.flags &= ~PD_FLAGS_EXPLICIT_CONTRACT; + set_state (PD_STATE_SNK_SWAP_SNK_DISABLE); + } else if (pd.task_state == PD_STATE_SNK_REQUESTED) { /* explicit contract is now in place */ - pd[port].flags |= PD_FLAGS_EXPLICIT_CONTRACT; + pd.flags |= PD_FLAGS_EXPLICIT_CONTRACT; #ifdef CONFIG_BBRAM - pd_set_saved_active(port, 1); + pd_set_saved_active( 1); #endif - set_state(port, PD_STATE_SNK_TRANSITION); + set_state (PD_STATE_SNK_TRANSITION); #endif } break; case PD_CTRL_SOFT_RESET: - execute_soft_reset(port); + execute_soft_reset(); /* We are done, acknowledge with an Accept packet */ - send_control(port, PD_CTRL_ACCEPT); + send_control(PD_CTRL_ACCEPT); break; case PD_CTRL_PR_SWAP: #ifdef CONFIG_USB_PD_DUAL_ROLE - if (pd_check_power_swap(port)) { - send_control(port, PD_CTRL_ACCEPT); + if (pd_check_power_swap()) { + send_control(PD_CTRL_ACCEPT); /* * Clear flag for checking power role to avoid * immediately requesting another swap. */ - pd[port].flags &= ~PD_FLAGS_CHECK_PR_ROLE; - set_state(port, - DUAL_ROLE_IF_ELSE(port, PD_STATE_SNK_SWAP_SNK_DISABLE, + pd.flags &= ~PD_FLAGS_CHECK_PR_ROLE; + set_state( + DUAL_ROLE_IF_ELSE(PD_STATE_SNK_SWAP_SNK_DISABLE, PD_STATE_SRC_SWAP_SNK_DISABLE)); } else { - send_control(port, REFUSE(pd[port].rev)); + send_control(REFUSE(pd.rev)); } #else - send_control(port, REFUSE(pd[port].rev)); + send_control(REFUSE(pd.rev)); #endif break; case PD_CTRL_DR_SWAP: - if (pd_check_data_swap(port, pd[port].data_role)) { + if (pd_check_data_swap(pd.data_role)) { /* * Accept switch and perform data swap. Clear * flag for checking data role to avoid * immediately requesting another swap. */ - pd[port].flags &= ~PD_FLAGS_CHECK_DR_ROLE; - if (send_control(port, PD_CTRL_ACCEPT) >= 0) - pd_dr_swap(port); + pd.flags &= ~PD_FLAGS_CHECK_DR_ROLE; + if (send_control(PD_CTRL_ACCEPT) >= 0) + pd_dr_swap(); } else { - send_control(port, REFUSE(pd[port].rev)); + send_control(REFUSE(pd.rev)); } break; case PD_CTRL_VCONN_SWAP: #ifdef CONFIG_USBC_VCONN_SWAP - if (pd[port].task_state == PD_STATE_SRC_READY || - pd[port].task_state == PD_STATE_SNK_READY) { - if (pd_check_vconn_swap(port)) { - if (send_control(port, PD_CTRL_ACCEPT) > 0) - set_state(port, + if (pd.task_state == PD_STATE_SRC_READY || + pd.task_state == PD_STATE_SNK_READY) { + if (pd_check_vconn_swap()) { + if (send_control( PD_CTRL_ACCEPT) > 0) + set_state( PD_STATE_VCONN_SWAP_INIT); } else { - send_control(port, REFUSE(pd[port].rev)); + send_control( REFUSE(pd.rev)); } } #else - send_control(port, REFUSE(pd[port].rev)); + send_control(REFUSE(pd.rev)); #endif break; default: #ifdef CONFIG_USB_PD_REV30 - send_control(port, PD_CTRL_NOT_SUPPORTED); + send_control( PD_CTRL_NOT_SUPPORTED); #endif CPRINTF("Unhandled ctrl message type %d\n", type); } } #ifdef CONFIG_USB_PD_REV30 -static void handle_ext_request(int port, uint16_t head, uint32_t *payload) +static void handle_ext_request( uint16_t head, uint32_t *payload) { int type = PD_HEADER_TYPE(head); switch (type) { case PD_EXT_GET_BATTERY_CAP: - send_battery_cap(port, payload); + send_battery_cap( payload); break; case PD_EXT_GET_BATTERY_STATUS: - send_battery_status(port, payload); + send_battery_status( payload); break; case PD_EXT_BATTERY_CAP: break; default: - send_control(port, PD_CTRL_NOT_SUPPORTED); + send_control( PD_CTRL_NOT_SUPPORTED); } } #endif -static void handle_request(int port, uint16_t head, uint32_t *payload) { +static void handle_request(uint16_t head, uint32_t *payload) { int cnt = PD_HEADER_CNT(head); int p; @@ -1501,59 +1442,58 @@ static void handle_request(int port, uint16_t head, uint32_t *payload) { || debug_level >= 3) { CPRINTF("RECV %04x/%d ", head, cnt); for (p = 0; p < cnt; p++) - CPRINTF("[%d]%08x ", p, payload[p]); CPRINTF("\n"); + CPRINTF("[%d]%08x ", p, payload[p]);CPRINTF("\n"); } /* * If we are in disconnected state, we shouldn't get a request. * Ignore it if we get one. */ - if (!pd_is_connected(port)) + if (!pd_is_connected()) return; #ifdef CONFIG_USB_PD_REV30 /* Check if this is an extended chunked data message. */ - if (pd[port].rev == PD_REV30 && PD_HEADER_EXT(head)) { - handle_ext_request(port, head, payload); + if (pd.rev == PD_REV30 && PD_HEADER_EXT(head)) { + handle_ext_request( head, payload); return; } #endif if (cnt) - handle_data_request(port, head, payload); + handle_data_request(head, payload); else - handle_ctrl_request(port, head, payload); + handle_ctrl_request(head, payload); } -void pd_send_vdm(int port, uint32_t vid, int cmd, const uint32_t *data, - int count) { +void pd_send_vdm(uint32_t vid, int cmd, const uint32_t *data, int count) { if (count > VDO_MAX_SIZE - 1) { CPRINTF("VDM over max size\n"); return; } /* set VDM header with VID & CMD */ - pd[port].vdo_data[0] = + pd.vdo_data[0] = VDO(vid, ((vid & USB_SID_PD) == USB_SID_PD) ? 1 : (PD_VDO_CMD(cmd) <= CMD_ATTENTION), cmd); #ifdef CONFIG_USB_PD_REV30 - pd[port].vdo_data[0] |= VDO_SVDM_VERS(vdo_ver[pd[port].rev]); + pd.vdo_data[0] |= VDO_SVDM_VERS(vdo_ver[pd.rev]); #endif - queue_vdm(port, pd[port].vdo_data, data, count); + queue_vdm(pd.vdo_data, data, count); // getting rid of task stuff - //task_wake(PD_PORT_TO_TASK_ID(port)); + //task_wake(PD_PORT_TO_TASK_ID()); } -static inline int pdo_busy(int port) { +static inline int pdo_busy() { /* * Note, main PDO state machine (pd_task) uses READY state exclusively * to denote port partners have successfully negociated a contract. All * other protocol actions force state transitions. */ - int rv = (pd[port].task_state != PD_STATE_SRC_READY); + int rv = (pd.task_state != PD_STATE_SRC_READY); #ifdef CONFIG_USB_PD_DUAL_ROLE - rv &= (pd[port].task_state != PD_STATE_SNK_READY); + rv &= (pd.task_state != PD_STATE_SNK_READY); #endif return rv; } @@ -1583,15 +1523,15 @@ static uint64_t vdm_get_ready_timeout(uint32_t vdm_hdr) { return timeout; } -static void pd_vdm_send_state_machine(int port) { +static void pd_vdm_send_state_machine() { int res; uint16_t header; - switch (pd[port].vdm_state) { + switch (pd.vdm_state) { case VDM_STATE_READY: /* Only transmit VDM if connected. */ - if (!pd_is_connected(port)) { - pd[port].vdm_state = VDM_STATE_ERR_BUSY; + if (!pd_is_connected()) { + pd.vdm_state = VDM_STATE_ERR_BUSY; break; } @@ -1599,35 +1539,33 @@ static void pd_vdm_send_state_machine(int port) { * if there's traffic or we're not in PDO ready state don't send * a VDM. */ - if (pdo_busy(port)) + if (pdo_busy()) break; /* Prepare and send VDM */ - header = PD_HEADER(PD_DATA_VENDOR_DEF, pd[port].power_role, - pd[port].data_role, pd[port].msg_id, (int )pd[port].vdo_count, - pd_get_rev(port), 0); - res = pd_transmit(port, TCPC_TX_SOP, header, pd[port].vdo_data); + header = PD_HEADER(PD_DATA_VENDOR_DEF, pd.power_role, pd.data_role, + pd.msg_id, (int )pd.vdo_count, pd_get_rev(), 0); + res = pd_transmit(TCPC_TX_SOP, header, pd.vdo_data); if (res < 0) { - pd[port].vdm_state = VDM_STATE_ERR_SEND; + pd.vdm_state = VDM_STATE_ERR_SEND; } else { - pd[port].vdm_state = VDM_STATE_BUSY; - pd[port].vdm_timeout.val = get_time().val - + vdm_get_ready_timeout(pd[port].vdo_data[0]); + pd.vdm_state = VDM_STATE_BUSY; + pd.vdm_timeout.val = get_time().val + + vdm_get_ready_timeout(pd.vdo_data[0]); } break; case VDM_STATE_WAIT_RSP_BUSY: /* wait and then initiate request again */ - if (get_time().val > pd[port].vdm_timeout.val) { - pd[port].vdo_data[0] = pd[port].vdo_retry; - pd[port].vdo_count = 1; - pd[port].vdm_state = VDM_STATE_READY; + if (get_time().val > pd.vdm_timeout.val) { + pd.vdo_data[0] = pd.vdo_retry; + pd.vdo_count = 1; + pd.vdm_state = VDM_STATE_READY; } break; case VDM_STATE_BUSY: /* Wait for VDM response or timeout */ - if (pd[port].vdm_timeout.val - && (get_time().val > pd[port].vdm_timeout.val)) { - pd[port].vdm_state = VDM_STATE_ERR_TMOUT; + if (pd.vdm_timeout.val && (get_time().val > pd.vdm_timeout.val)) { + pd.vdm_state = VDM_STATE_ERR_TMOUT; } break; default: @@ -1649,22 +1587,22 @@ static inline void pd_dev_dump_info(uint16_t dev_id, uint8_t *hash) } #endif /* CONFIG_CMD_PD_DEV_DUMP_INFO */ -int pd_dev_store_rw_hash(int port, uint16_t dev_id, uint32_t *rw_hash, +int pd_dev_store_rw_hash(uint16_t dev_id, uint32_t *rw_hash, uint32_t current_image) { #ifdef CONFIG_COMMON_RUNTIME int i; #endif #ifdef CONFIG_USB_PD_CHROMEOS - pd[port].dev_id = dev_id; - memcpy(pd[port].dev_rw_hash, rw_hash, PD_RW_HASH_SIZE); + pd.dev_id = dev_id; + memcpy(pd.dev_rw_hash, rw_hash, PD_RW_HASH_SIZE); #endif #ifdef CONFIG_CMD_PD_DEV_DUMP_INFO if (debug_level >= 2) pd_dev_dump_info(dev_id, (uint8_t *)rw_hash); #endif #ifdef CONFIG_USB_PD_CHROMEOS - pd[port].current_image = current_image; + pd.current_image = current_image; #endif #ifdef CONFIG_COMMON_RUNTIME @@ -1738,62 +1676,62 @@ void pd_set_dual_role(enum pd_dual_role_states state) { ; } -void pd_update_dual_role_config(int port) { +void pd_update_dual_role_config() { /* * Change to sink if port is currently a source AND (new DRP * state is force sink OR new DRP state is either toggle off * or debug accessory toggle only and we are in the source * disconnected state). */ - if (pd[port].power_role == PD_ROLE_SOURCE - && ((drp_state == PD_DRP_FORCE_SINK && !pd_ts_dts_plugged(port)) + if (pd.power_role == PD_ROLE_SOURCE + && ((drp_state == PD_DRP_FORCE_SINK && !pd_ts_dts_plugged()) || (drp_state == PD_DRP_TOGGLE_OFF - && pd[port].task_state == PD_STATE_SRC_DISCONNECTED))) { - pd[port].power_role = PD_ROLE_SINK; - set_state(port, PD_STATE_SNK_DISCONNECTED); - tcpm_set_cc(port, TYPEC_CC_RD); + && pd.task_state == PD_STATE_SRC_DISCONNECTED))) { + pd.power_role = PD_ROLE_SINK; + set_state (PD_STATE_SNK_DISCONNECTED); + tcpm_set_cc(TYPEC_CC_RD); /* Make sure we're not sourcing VBUS. */ - pd_power_supply_reset(port); + pd_power_supply_reset(); } /* * Change to source if port is currently a sink and the * new DRP state is force source. */ - if (pd[port].power_role == PD_ROLE_SINK + if (pd.power_role == PD_ROLE_SINK && drp_state == PD_DRP_FORCE_SOURCE) { - pd[port].power_role = PD_ROLE_SOURCE; - set_state(port, PD_STATE_SRC_DISCONNECTED); - tcpm_set_cc(port, TYPEC_CC_RP); + pd.power_role = PD_ROLE_SOURCE; + set_state(PD_STATE_SRC_DISCONNECTED); + tcpm_set_cc(TYPEC_CC_RP); } #if defined(CONFIG_USB_PD_DUAL_ROLE_AUTO_TOGGLE) && \ defined(CONFIG_USB_PD_TCPC_LOW_POWER) /* When switching drp mode, make sure tcpc is out of standby mode */ - tcpm_set_drp_toggle(port, 0); + tcpm_set_drp_toggle( 0); #endif } -int pd_get_role(int port) { - return pd[port].power_role; +int pd_get_role() { + return pd.power_role; } -static int pd_is_power_swapping(int port) { +static int pd_is_power_swapping() { /* return true if in the act of swapping power roles */ - return pd[port].task_state == PD_STATE_SNK_SWAP_SNK_DISABLE - || pd[port].task_state == PD_STATE_SNK_SWAP_SRC_DISABLE - || pd[port].task_state == PD_STATE_SNK_SWAP_STANDBY - || pd[port].task_state == PD_STATE_SNK_SWAP_COMPLETE - || pd[port].task_state == PD_STATE_SRC_SWAP_SNK_DISABLE - || pd[port].task_state == PD_STATE_SRC_SWAP_SRC_DISABLE - || pd[port].task_state == PD_STATE_SRC_SWAP_STANDBY; + return pd.task_state == PD_STATE_SNK_SWAP_SNK_DISABLE + || pd.task_state == PD_STATE_SNK_SWAP_SRC_DISABLE + || pd.task_state == PD_STATE_SNK_SWAP_STANDBY + || pd.task_state == PD_STATE_SNK_SWAP_COMPLETE + || pd.task_state == PD_STATE_SRC_SWAP_SNK_DISABLE + || pd.task_state == PD_STATE_SRC_SWAP_SRC_DISABLE + || pd.task_state == PD_STATE_SRC_SWAP_STANDBY; } /* * Provide Rp to ensure the partner port is in a known state (eg. not * PD negotiated, not sourcing 20V). */ -static void pd_partner_port_reset(int port) { +static void pd_partner_port_reset() { uint64_t timeout; #ifdef CONFIG_BBRAM @@ -1802,61 +1740,61 @@ static void pd_partner_port_reset(int port) { * active, and we didn't just lose power, make sure we * don't boot into RO with a pre-existing power contract. */ - if (!pd_get_saved_active(port) || + if (!pd_get_saved_active() || system_get_image_copy() != SYSTEM_IMAGE_RO || system_get_reset_flags() & (RESET_FLAG_BROWNOUT | RESET_FLAG_POWER_ON)) return; #endif // CONFIG_BBRAM /* Provide Rp for 100 msec. or until we no longer have VBUS. */ - tcpm_set_cc(port, TYPEC_CC_RP); + tcpm_set_cc(TYPEC_CC_RP); timeout = get_time().val + 100 * MSEC; - while (get_time().val < timeout && pd_is_vbus_present(port)) + while (get_time().val < timeout && pd_is_vbus_present()) msleep(10); #ifdef CONFIG_BBRAM - pd_set_saved_active(port, 0); + pd_set_saved_active( 0); #endif } #endif /* CONFIG_USB_PD_DUAL_ROLE */ -int pd_get_polarity(int port) { - return pd[port].polarity; +int pd_get_polarity() { + return pd.polarity; } -int pd_get_partner_data_swap_capable(int port) { +int pd_get_partner_data_swap_capable() { /* return data swap capable status of port partner */ - return pd[port].flags & PD_FLAGS_PARTNER_DR_DATA; + return pd.flags & PD_FLAGS_PARTNER_DR_DATA; } #ifdef CONFIG_COMMON_RUNTIME -void pd_comm_enable(int port, int enable) +void pd_comm_enable( int enable) { /* We don't check port >= CONFIG_USB_PD_PORT_COUNT deliberately */ pd_comm_enabled[port] = enable; /* If type-C connection, then update the TCPC RX enable */ - if (pd_is_connected(port)) - tcpm_set_rx_enable(port, enable); + if (pd_is_connected()) + tcpm_set_rx_enable( enable); #ifdef CONFIG_USB_PD_DUAL_ROLE /* * If communications are enabled, start hard reset timer for * any port in PD_SNK_DISCOVERY. */ - if (enable && pd[port].task_state == PD_STATE_SNK_DISCOVERY) - set_state_timeout(port, + if (enable && pd.task_state == PD_STATE_SNK_DISCOVERY) + set_state_timeout( get_time().val + PD_T_SINK_WAIT_CAP, PD_STATE_HARD_RESET_SEND); #endif } #endif -void pd_ping_enable(int port, int enable) { +void pd_ping_enable(int enable) { if (enable) - pd[port].flags |= PD_FLAGS_PING_ENABLED; + pd.flags |= PD_FLAGS_PING_ENABLED; else - pd[port].flags &= ~PD_FLAGS_PING_ENABLED; + pd.flags &= ~PD_FLAGS_PING_ENABLED; } /** @@ -1917,10 +1855,10 @@ static typec_current_t get_typec_current_limit(int polarity, int cc1, int cc2) { /** * Signal power request to indicate a charger update that affects the port. */ -void pd_set_new_power_request(int port) { - pd[port].new_power_request = 1; +void pd_set_new_power_request() { +// pd.new_power_request = 1; // getting rid of task stuff - //task_wake(PD_PORT_TO_TASK_ID(port)); + //task_wake(PD_PORT_TO_TASK_ID()); } #endif /* CONFIG_CHARGE_MANAGER */ @@ -1972,22 +1910,22 @@ static void pd_init_tasks(void) #endif /* CONFIG_COMMON_RUNTIME */ #ifndef CONFIG_USB_PD_TCPC -static int pd_restart_tcpc(int port) { +static int pd_restart_tcpc() { if (board_set_tcpc_power_mode) { /* force chip reset */ - board_set_tcpc_power_mode(port, 0); + board_set_tcpc_power_mode(0); } - return tcpm_init(port); + return tcpm_init(); } #endif -void pd_init(int port) { +void pd_init() { #ifdef CONFIG_COMMON_RUNTIME pd_init_tasks(); #endif /* Ensure the power supply is in the default state */ - pd_power_supply_reset(port); + pd_power_supply_reset(); #ifdef CONFIG_USB_PD_TCPC_BOARD_INIT /* Board specific TCPC init */ @@ -1995,18 +1933,18 @@ void pd_init(int port) { #endif /* Initialize TCPM driver and wait for TCPC to be ready */ - res = tcpm_init(port); + res = tcpm_init(); #ifdef CONFIG_USB_PD_DUAL_ROLE - //pd_partner_port_reset(port); + //pd_partner_port_reset(); #endif CPRINTS("TCPC p%d init %s", port, res ? "failed" : "ready"); - this_state = res ? PD_STATE_SUSPENDED : PD_DEFAULT_STATE(port); + this_state = res ? PD_STATE_SUSPENDED : PD_DEFAULT_STATE(); #ifndef CONFIG_USB_PD_TCPC if (!res) { struct ec_response_pd_chip_info *info; - tcpm_get_chip_info(port, 0, &info); + tcpm_get_chip_info(0, &info); CPRINTS("TCPC p%d VID:0x%x PID:0x%x DID:0x%x FWV:0x%lx", port, info->vendor_id, info->product_id, info->device_id, info->fw_version_number); @@ -2015,8 +1953,8 @@ void pd_init(int port) { #ifdef CONFIG_USB_PD_REV30 /* Set Revision to highest */ - pd[port].rev = PD_REV30; - pd_ca_reset(port); + pd.rev = PD_REV30; + pd_ca_reset(); #endif #ifdef CONFIG_USB_PD_DUAL_ROLE @@ -2025,60 +1963,59 @@ void pd_init(int port) { * present. This flag is used to maintain a PD connection after a * reset by sending a soft reset. */ - //pd[port].flags = pd_is_vbus_present(port) ? PD_FLAGS_VBUS_NEVER_LOW : 0; - pd[port].flags = 0; + //pd.flags = pd_is_vbus_present() ? PD_FLAGS_VBUS_NEVER_LOW : 0; + pd.flags = 0; #endif /* Disable TCPC RX until connection is established */ - tcpm_set_rx_enable(port, 0); + tcpm_set_rx_enable(0); #ifdef CONFIG_USBC_SS_MUX /* Initialize USB mux to its default state */ - usb_mux_init(port); + usb_mux_init(); #endif /* Initialize PD protocol state variables for each port. */ - pd[port].power_role = PD_ROLE_DEFAULT(port); - pd[port].vdm_state = VDM_STATE_DONE; - set_state(port, this_state); + pd.power_role = PD_ROLE_DEFAULT(); + pd.vdm_state = VDM_STATE_DONE; + set_state(this_state); #ifdef CONFIG_USB_PD_MAX_SINGLE_SOURCE_CURRENT - ASSERT(PD_ROLE_DEFAULT(port) == PD_ROLE_SINK); - tcpm_select_rp_value(port, CONFIG_USB_PD_MAX_SINGLE_SOURCE_CURRENT); + ASSERT(PD_ROLE_DEFAULT() == PD_ROLE_SINK); + tcpm_select_rp_value( CONFIG_USB_PD_MAX_SINGLE_SOURCE_CURRENT); #else - tcpm_select_rp_value(port, CONFIG_USB_PD_PULLUP); + tcpm_select_rp_value( CONFIG_USB_PD_PULLUP); #endif - tcpm_set_cc(port, - PD_ROLE_DEFAULT(port) == PD_ROLE_SOURCE ? - TYPEC_CC_RP : TYPEC_CC_RD); + tcpm_set_cc( + PD_ROLE_DEFAULT() == PD_ROLE_SOURCE ? TYPEC_CC_RP : TYPEC_CC_RD); #ifdef CONFIG_USB_PD_ALT_MODE_DFP /* Initialize PD Policy engine */ - pd_dfp_pe_init(port); + pd_dfp_pe_init(); #endif #ifdef CONFIG_CHARGE_MANAGER /* Initialize PD and type-C supplier current limits to 0 */ - pd_set_input_current_limit(port, 0, 0); - //typec_set_input_current_limit(port, 0, 0); - //charge_manager_update_dualrole(port, CAP_UNKNOWN); + pd_set_input_current_limit(0, 0); + //typec_set_input_current_limit( 0, 0); + //charge_manager_update_dualrole( CAP_UNKNOWN); #endif } -void pd_run_state_machine(int port) { +void pd_run_state_machine() { #ifdef CONFIG_USB_PD_REV30 /* send any pending messages */ - pd_ca_send_pending(port); + pd_ca_send_pending(); #endif /* process VDM messages last */ - pd_vdm_send_state_machine(port); + pd_vdm_send_state_machine(); /* Verify board specific health status : current, voltages... */ res = pd_board_checks(); if (res != EC_SUCCESS) { /* cut the power */ - pd_execute_hard_reset(port); + pd_execute_hard_reset(); /* notify the other side of the issue */ - pd_transmit(port, TCPC_TX_HARD_RESET, 0, NULL); + pd_transmit(TCPC_TX_HARD_RESET, 0, NULL); } /* wait for next event/packet or timeout expiration */ @@ -2086,7 +2023,7 @@ void pd_run_state_machine(int port) { //evt = task_wait_event(timeout); #ifdef CONFIG_USB_PD_DUAL_ROLE if (evt & PD_EVENT_UPDATE_DUAL_ROLE) - pd_update_dual_role_config(port); + pd_update_dual_role_config(); #endif #ifdef CONFIG_USB_PD_TCPC @@ -2094,21 +2031,21 @@ void pd_run_state_machine(int port) { * run port controller task to check CC and/or read incoming * messages */ - tcpc_run(port, evt); + tcpc_run( evt); #else /* if TCPC has reset, then need to initialize it again */ if (evt & PD_EVENT_TCPC_RESET) { CPRINTS("TCPC p%d reset!", port); - if (tcpm_init(port) != EC_SUCCESS) + if (tcpm_init() != EC_SUCCESS) CPRINTS("TCPC p%d init failed", port); #ifdef CONFIG_USB_PD_DUAL_ROLE_AUTO_TOGGLE } if ((evt & PD_EVENT_TCPC_RESET) && - (pd[port].task_state != PD_STATE_DRP_AUTO_TOGGLE)) { + (pd.task_state != PD_STATE_DRP_AUTO_TOGGLE)) { #endif /* Ensure CC termination is default */ - tcpm_set_cc(port, PD_ROLE_DEFAULT(port) == + tcpm_set_cc( PD_ROLE_DEFAULT() == PD_ROLE_SOURCE ? TYPEC_CC_RP : TYPEC_CC_RD); /* @@ -2118,23 +2055,23 @@ void pd_run_state_machine(int port) { * Otherwise, go to the default disconnected state * and force renegotiation. */ - if (pd[port].vdm_state == VDM_STATE_DONE + if (pd.vdm_state == VDM_STATE_DONE && ( #ifdef CONFIG_USB_PD_DUAL_ROLE - (PD_ROLE_DEFAULT(port) == PD_ROLE_SINK - && pd[port].task_state == PD_STATE_SNK_READY) + (PD_ROLE_DEFAULT() == PD_ROLE_SINK + && pd.task_state == PD_STATE_SNK_READY) || #endif - (PD_ROLE_DEFAULT(port) == PD_ROLE_SOURCE - && pd[port].task_state == PD_STATE_SRC_READY))) { - tcpm_set_polarity(port, pd[port].polarity); - tcpm_set_msg_header(port, pd[port].power_role, pd[port].data_role); - tcpm_set_rx_enable(port, 1); + (PD_ROLE_DEFAULT() == PD_ROLE_SOURCE + && pd.task_state == PD_STATE_SRC_READY))) { + tcpm_set_polarity(pd.polarity); + tcpm_set_msg_header(pd.power_role, pd.data_role); + tcpm_set_rx_enable(1); } else { /* Ensure state variables are at default */ - pd[port].power_role = PD_ROLE_DEFAULT(port); - pd[port].vdm_state = VDM_STATE_DONE; - set_state(port, PD_DEFAULT_STATE(port)); + pd.power_role = PD_ROLE_DEFAULT(); + pd.vdm_state = VDM_STATE_DONE; + set_state(PD_DEFAULT_STATE()); } } #endif @@ -2142,15 +2079,15 @@ void pd_run_state_machine(int port) { /* process any potential incoming message */ incoming_packet = evt & PD_EVENT_RX; //if (incoming_packet) { - if (!tcpm_get_message(port, payload, &head)) - handle_request(port, head, payload); + if (!tcpm_get_message(payload, &head)) + handle_request(head, payload); //} - if (pd[port].req_suspend_state) - set_state(port, PD_STATE_SUSPENDED); + if (pd.req_suspend_state) + set_state(PD_STATE_SUSPENDED); /* if nothing to do, verify the state of the world in 500ms */ - this_state = pd[port].task_state; + this_state = pd.task_state; timeout = 500 * MSEC; switch (this_state) { case PD_STATE_DISABLED: @@ -2158,18 +2095,18 @@ void pd_run_state_machine(int port) { break; case PD_STATE_SRC_DISCONNECTED: timeout = 10 * MSEC; - tcpm_get_cc(port, &cc1, &cc2); + tcpm_get_cc(&cc1, &cc2); #ifdef CONFIG_USB_PD_DUAL_ROLE_AUTO_TOGGLE /* * Attempt TCPC auto DRP toggle if it is * not already auto toggling and not try.src */ if (auto_toggle_supported && - !(pd[port].flags & PD_FLAGS_TCPC_DRP_TOGGLE) && - !(pd[port].flags & PD_FLAGS_TRY_SRC) && + !(pd.flags & PD_FLAGS_TCPC_DRP_TOGGLE) && + !(pd.flags & PD_FLAGS_TRY_SRC) && (cc1 == TYPEC_CC_VOLT_OPEN && cc2 == TYPEC_CC_VOLT_OPEN)) { - set_state(port, PD_STATE_DRP_AUTO_TOGGLE); + set_state( PD_STATE_DRP_AUTO_TOGGLE); timeout = 2*MSEC; break; } @@ -2180,11 +2117,11 @@ void pd_run_state_machine(int port) { || (cc1 == TYPEC_CC_VOLT_RA && cc2 == TYPEC_CC_VOLT_RA)) { #ifdef CONFIG_USBC_BACKWARDS_COMPATIBLE_DFP /* Enable VBUS */ - if (pd_set_power_supply_ready(port)) + if (pd_set_power_supply_ready()) break; #endif - pd[port].cc_state = PD_CC_NONE; - set_state(port, PD_STATE_SRC_DISCONNECTED_DEBOUNCE); + pd.cc_state = PD_CC_NONE; + set_state(PD_STATE_SRC_DISCONNECTED_DEBOUNCE); } #if defined(CONFIG_USB_PD_DUAL_ROLE) /* @@ -2195,17 +2132,17 @@ void pd_run_state_machine(int port) { * If Try.SRC state is not active, then this block * handles the normal DRP toggle from SRC->SNK */ - else if ((pd[port].flags & PD_FLAGS_TRY_SRC - && get_time().val >= pd[port].try_src_marker) - || (!(pd[port].flags & PD_FLAGS_TRY_SRC) + else if ((pd.flags & PD_FLAGS_TRY_SRC + && get_time().val >= pd.try_src_marker) + || (!(pd.flags & PD_FLAGS_TRY_SRC) && drp_state != PD_DRP_FORCE_SOURCE && drp_state != PD_DRP_FREEZE && get_time().val >= next_role_swap)) { - pd[port].power_role = PD_ROLE_SINK; - set_state(port, PD_STATE_SNK_DISCONNECTED); - tcpm_set_cc(port, TYPEC_CC_RD); + pd.power_role = PD_ROLE_SINK; + set_state (PD_STATE_SNK_DISCONNECTED); + tcpm_set_cc(TYPEC_CC_RD); next_role_swap = get_time().val + PD_T_DRP_SNK; - pd[port].try_src_marker = get_time().val + PD_T_TRY_WAIT; + pd.try_src_marker = get_time().val + PD_T_TRY_WAIT; /* Swap states quickly */ timeout = 2 * MSEC; @@ -2214,7 +2151,7 @@ void pd_run_state_machine(int port) { break; case PD_STATE_SRC_DISCONNECTED_DEBOUNCE: timeout = 20 * MSEC; - tcpm_get_cc(port, &cc1, &cc2); + tcpm_get_cc(&cc1, &cc2); if (cc1 == TYPEC_CC_VOLT_RD && cc2 == TYPEC_CC_VOLT_RD) { /* Debug accessory */ @@ -2227,19 +2164,19 @@ void pd_run_state_machine(int port) { new_cc_state = PD_CC_AUDIO_ACC; } else { /* No UFP */ - set_state(port, PD_STATE_SRC_DISCONNECTED); + set_state(PD_STATE_SRC_DISCONNECTED); timeout = 5 * MSEC; break; } /* If in Try.SRC state, then don't need to debounce */ - if (!(pd[port].flags & PD_FLAGS_TRY_SRC)) { + if (!(pd.flags & PD_FLAGS_TRY_SRC)) { /* Debounce the cc state */ - if (new_cc_state != pd[port].cc_state) { - pd[port].cc_debounce = get_time().val + + if (new_cc_state != pd.cc_state) { + pd.cc_debounce = get_time().val + PD_T_CC_DEBOUNCE; - pd[port].cc_state = new_cc_state; + pd.cc_state = new_cc_state; break; - } else if (get_time().val < pd[port].cc_debounce) { + } else if (get_time().val < pd.cc_debounce) { break; } } @@ -2248,41 +2185,41 @@ void pd_run_state_machine(int port) { /* UFP is attached */ if (new_cc_state == PD_CC_UFP_ATTACHED || new_cc_state == PD_CC_DEBUG_ACC) { - pd[port].polarity = (cc1 != TYPEC_CC_VOLT_RD); - tcpm_set_polarity(port, pd[port].polarity); + pd.polarity = (cc1 != TYPEC_CC_VOLT_RD); + tcpm_set_polarity(pd.polarity); /* initial data role for source is DFP */ - pd_set_data_role(port, PD_ROLE_DFP); + pd_set_data_role( PD_ROLE_DFP); if (new_cc_state == PD_CC_DEBUG_ACC) - pd[port].flags |= + pd.flags |= PD_FLAGS_TS_DTS_PARTNER; #ifndef CONFIG_USBC_BACKWARDS_COMPATIBLE_DFP /* Enable VBUS */ - if (pd_set_power_supply_ready(port)) { + if (pd_set_power_supply_ready()) { #ifdef CONFIG_USBC_SS_MUX - usb_mux_set(port, TYPEC_MUX_NONE, + usb_mux_set( TYPEC_MUX_NONE, USB_SWITCH_DISCONNECT, - pd[port].polarity); + pd.polarity); #endif break; } #endif /* If PD comm is enabled, enable TCPC RX */ - if (pd_comm_is_enabled(port)) - tcpm_set_rx_enable(port, 1); + if (pd_comm_is_enabled()) + tcpm_set_rx_enable(1); #ifdef CONFIG_USBC_VCONN - tcpm_set_vconn(port, 1); - pd[port].flags |= PD_FLAGS_VCONN_ON; + tcpm_set_vconn( 1); + pd.flags |= PD_FLAGS_VCONN_ON; #endif - pd[port].flags |= PD_FLAGS_CHECK_PR_ROLE | + pd.flags |= PD_FLAGS_CHECK_PR_ROLE | PD_FLAGS_CHECK_DR_ROLE; hard_reset_count = 0; timeout = 5 * MSEC; - set_state(port, PD_STATE_SRC_STARTUP); + set_state(PD_STATE_SRC_STARTUP); } /* * AUDIO_ACC will remain in this state indefinitely @@ -2291,15 +2228,15 @@ void pd_run_state_machine(int port) { break; case PD_STATE_SRC_HARD_RESET_RECOVER: /* Do not continue until hard reset recovery time */ - if (get_time().val < pd[port].src_recover) { + if (get_time().val < pd.src_recover) { timeout = 50 * MSEC; break; } /* Enable VBUS */ timeout = 10 * MSEC; - if (pd_set_power_supply_ready(port)) { - set_state(port, PD_STATE_SRC_DISCONNECTED); + if (pd_set_power_supply_ready()) { + set_state(PD_STATE_SRC_DISCONNECTED); break; } #ifdef CONFIG_USB_PD_TCPM_TCPCI @@ -2308,21 +2245,21 @@ void pd_run_state_machine(int port) { * to RECEIVE_DETECT register to enable * PD message passing. */ - if (pd_comm_is_enabled(port)) - tcpm_set_rx_enable(port, 1); + if (pd_comm_is_enabled()) + tcpm_set_rx_enable( 1); #endif /* CONFIG_USB_PD_TCPM_TCPCI */ - set_state(port, PD_STATE_SRC_STARTUP); + set_state(PD_STATE_SRC_STARTUP); break; case PD_STATE_SRC_STARTUP: /* Wait for power source to enable */ - if (pd[port].last_state != pd[port].task_state) { - pd[port].flags |= PD_FLAGS_CHECK_IDENTITY; + if (pd.last_state != pd.task_state) { + pd.flags |= PD_FLAGS_CHECK_IDENTITY; /* reset various counters */ caps_count = 0; - pd[port].msg_id = 0; + pd.msg_id = 0; snk_cap_count = 0; - set_state_timeout(port, + set_state_timeout( #ifdef CONFIG_USBC_BACKWARDS_COMPATIBLE_DFP /* * delay for power supply to start up. @@ -2332,7 +2269,7 @@ void pd_run_state_machine(int port) { */ get_time().val + PD_POWER_SUPPLY_TURN_ON_DELAY - - (pd[port].last_state == + (pd.last_state == PD_STATE_SRC_DISCONNECTED_DEBOUNCE ? PD_T_CC_DEBOUNCE : 0), #else @@ -2343,13 +2280,13 @@ void pd_run_state_machine(int port) { } break; case PD_STATE_SRC_DISCOVERY: - if (pd[port].last_state != pd[port].task_state) { + if (pd.last_state != pd.task_state) { /* * If we have had PD connection with this port * partner, then start NoResponseTimer. */ - if (pd[port].flags & PD_FLAGS_PREVIOUS_PD_CONN) - set_state_timeout(port, get_time().val + + if (pd.flags & PD_FLAGS_PREVIOUS_PD_CONN) + set_state_timeout(get_time().val + PD_T_NO_RESPONSE, hard_reset_count < PD_HARD_RESET_COUNT ? @@ -2360,15 +2297,15 @@ void pd_run_state_machine(int port) { /* Send source cap some minimum number of times */ if (caps_count < PD_CAPS_COUNT) { /* Query capabilities of the other side */ - res = send_source_cap(port); + res = send_source_cap(); /* packet was acked => PD capable device) */ if (res >= 0) { - set_state(port, PD_STATE_SRC_NEGOCIATE); + set_state(PD_STATE_SRC_NEGOCIATE); timeout = 10 * MSEC; hard_reset_count = 0; caps_count = 0; /* Port partner is PD capable */ - pd[port].flags |= + pd.flags |= PD_FLAGS_PREVIOUS_PD_CONN; } else { /* failed, retry later */ timeout = PD_T_SEND_SOURCE_CAP; @@ -2378,34 +2315,34 @@ void pd_run_state_machine(int port) { break; case PD_STATE_SRC_NEGOCIATE: /* wait for a "Request" message */ - if (pd[port].last_state != pd[port].task_state) - set_state_timeout(port, get_time().val + + if (pd.last_state != pd.task_state) + set_state_timeout(get_time().val + PD_T_SENDER_RESPONSE, PD_STATE_HARD_RESET_SEND); break; case PD_STATE_SRC_ACCEPTED: /* Accept sent, wait for enabling the new voltage */ - if (pd[port].last_state != pd[port].task_state) - set_state_timeout(port, get_time().val + + if (pd.last_state != pd.task_state) + set_state_timeout(get_time().val + PD_T_SINK_TRANSITION, PD_STATE_SRC_POWERED); break; case PD_STATE_SRC_POWERED: /* Switch to the new requested voltage */ - if (pd[port].last_state != pd[port].task_state) { - pd_transition_voltage(pd[port].requested_idx); - set_state_timeout(port, get_time().val + + if (pd.last_state != pd.task_state) { + pd_transition_voltage(pd.requested_idx); + set_state_timeout(get_time().val + PD_POWER_SUPPLY_TURN_ON_DELAY, PD_STATE_SRC_TRANSITION); } break; case PD_STATE_SRC_TRANSITION: /* the voltage output is good, notify the source */ - res = send_control(port, PD_CTRL_PS_RDY); + res = send_control(PD_CTRL_PS_RDY); if (res >= 0) { timeout = 10 * MSEC; /* it'a time to ping regularly the sink */ - set_state(port, PD_STATE_SRC_READY); + set_state(PD_STATE_SRC_READY); } else { /* The sink did not ack, cut the power... */ - set_state(port, PD_STATE_SRC_DISCONNECTED); + set_state(PD_STATE_SRC_DISCONNECTED); } break; case PD_STATE_SRC_READY: @@ -2416,25 +2353,25 @@ void pd_run_state_machine(int port) { * incoming packet or if VDO response pending to avoid * collisions. */ - if (incoming_packet || (pd[port].vdm_state == VDM_STATE_BUSY)) + if (incoming_packet || (pd.vdm_state == VDM_STATE_BUSY)) break; /* Send updated source capabilities to our partner */ - if (pd[port].flags & PD_FLAGS_UPDATE_SRC_CAPS) { - res = send_source_cap(port); + if (pd.flags & PD_FLAGS_UPDATE_SRC_CAPS) { + res = send_source_cap(); if (res >= 0) { - set_state(port, PD_STATE_SRC_NEGOCIATE); - pd[port].flags &= ~PD_FLAGS_UPDATE_SRC_CAPS; + set_state(PD_STATE_SRC_NEGOCIATE); + pd.flags &= ~PD_FLAGS_UPDATE_SRC_CAPS; } break; } /* Send get sink cap if haven't received it yet */ - if (!(pd[port].flags & PD_FLAGS_SNK_CAP_RECVD)) { + if (!(pd.flags & PD_FLAGS_SNK_CAP_RECVD)) { if (++snk_cap_count <= PD_SNK_CAP_RETRIES) { /* Get sink cap to know if dual-role device */ - send_control(port, PD_CTRL_GET_SINK_CAP); - set_state(port, PD_STATE_SRC_GET_SINK_CAP); + send_control(PD_CTRL_GET_SINK_CAP); + set_state(PD_STATE_SRC_GET_SINK_CAP); break; } else if (debug_level >= 2 && snk_cap_count == PD_SNK_CAP_RETRIES + 1) { @@ -2443,50 +2380,50 @@ void pd_run_state_machine(int port) { } /* Check power role policy, which may trigger a swap */ - if (pd[port].flags & PD_FLAGS_CHECK_PR_ROLE) { - pd_check_pr_role(port, PD_ROLE_SOURCE, pd[port].flags); - pd[port].flags &= ~PD_FLAGS_CHECK_PR_ROLE; + if (pd.flags & PD_FLAGS_CHECK_PR_ROLE) { + pd_check_pr_role( PD_ROLE_SOURCE, pd.flags); + pd.flags &= ~PD_FLAGS_CHECK_PR_ROLE; break; } /* Check data role policy, which may trigger a swap */ - if (pd[port].flags & PD_FLAGS_CHECK_DR_ROLE) { - pd_check_dr_role(port, pd[port].data_role, pd[port].flags); - pd[port].flags &= ~PD_FLAGS_CHECK_DR_ROLE; + if (pd.flags & PD_FLAGS_CHECK_DR_ROLE) { + pd_check_dr_role(pd.data_role, pd.flags); + pd.flags &= ~PD_FLAGS_CHECK_DR_ROLE; break; } /* Send discovery SVDMs last */ - if (pd[port].data_role == PD_ROLE_DFP - && (pd[port].flags & PD_FLAGS_CHECK_IDENTITY)) { + if (pd.data_role == PD_ROLE_DFP + && (pd.flags & PD_FLAGS_CHECK_IDENTITY)) { #ifndef CONFIG_USB_PD_SIMPLE_DFP - pd_send_vdm(port, USB_SID_PD, + pd_send_vdm( USB_SID_PD, CMD_DISCOVER_IDENT, NULL, 0); #endif - pd[port].flags &= ~PD_FLAGS_CHECK_IDENTITY; + pd.flags &= ~PD_FLAGS_CHECK_IDENTITY; break; } - if (!(pd[port].flags & PD_FLAGS_PING_ENABLED)) + if (!(pd.flags & PD_FLAGS_PING_ENABLED)) break; /* Verify that the sink is alive */ - res = send_control(port, PD_CTRL_PING); + res = send_control(PD_CTRL_PING); if (res >= 0) break; /* Ping dropped. Try soft reset. */ - set_state(port, PD_STATE_SOFT_RESET); + set_state(PD_STATE_SOFT_RESET); timeout = 10 * MSEC; break; case PD_STATE_SRC_GET_SINK_CAP: - if (pd[port].last_state != pd[port].task_state) - set_state_timeout(port, get_time().val + + if (pd.last_state != pd.task_state) + set_state_timeout(get_time().val + PD_T_SENDER_RESPONSE, PD_STATE_SRC_READY); break; case PD_STATE_DR_SWAP: - if (pd[port].last_state != pd[port].task_state) { - res = send_control(port, PD_CTRL_DR_SWAP); + if (pd.last_state != pd.task_state) { + res = send_control(PD_CTRL_DR_SWAP); if (res < 0) { timeout = 10 * MSEC; /* @@ -2494,20 +2431,19 @@ void pd_run_state_machine(int port) { * soft reset, otherwise ignore * failure. */ - set_state(port, - res == -1 ? - PD_STATE_SOFT_RESET : READY_RETURN_STATE(port)); + set_state( + res == -1 ? PD_STATE_SOFT_RESET : READY_RETURN_STATE()); break; } /* Wait for accept or reject */ - set_state_timeout(port, get_time().val + - PD_T_SENDER_RESPONSE, READY_RETURN_STATE(port)); + set_state_timeout(get_time().val + + PD_T_SENDER_RESPONSE, READY_RETURN_STATE()); } break; #ifdef CONFIG_USB_PD_DUAL_ROLE case PD_STATE_SRC_SWAP_INIT: - if (pd[port].last_state != pd[port].task_state) { - res = send_control(port, PD_CTRL_PR_SWAP); + if (pd.last_state != pd.task_state) { + res = send_control(PD_CTRL_PR_SWAP); if (res < 0) { timeout = 10 * MSEC; /* @@ -2515,44 +2451,43 @@ void pd_run_state_machine(int port) { * soft reset, otherwise ignore * failure. */ - set_state(port, - res == -1 ? PD_STATE_SOFT_RESET : PD_STATE_SRC_READY); + set_state(res == -1 ? PD_STATE_SOFT_RESET : PD_STATE_SRC_READY); break; } /* Wait for accept or reject */ - set_state_timeout(port, get_time().val + + set_state_timeout(get_time().val + PD_T_SENDER_RESPONSE, PD_STATE_SRC_READY); } break; case PD_STATE_SRC_SWAP_SNK_DISABLE: /* Give time for sink to stop drawing current */ - if (pd[port].last_state != pd[port].task_state) - set_state_timeout(port, get_time().val + + if (pd.last_state != pd.task_state) + set_state_timeout(get_time().val + PD_T_SINK_TRANSITION, PD_STATE_SRC_SWAP_SRC_DISABLE); break; case PD_STATE_SRC_SWAP_SRC_DISABLE: /* Turn power off */ - if (pd[port].last_state != pd[port].task_state) { - pd_power_supply_reset(port); - set_state_timeout(port, get_time().val + + if (pd.last_state != pd.task_state) { + pd_power_supply_reset(); + set_state_timeout(get_time().val + PD_POWER_SUPPLY_TURN_OFF_DELAY, PD_STATE_SRC_SWAP_STANDBY); } break; case PD_STATE_SRC_SWAP_STANDBY: /* Send PS_RDY to let sink know our power is off */ - if (pd[port].last_state != pd[port].task_state) { + if (pd.last_state != pd.task_state) { /* Send PS_RDY */ - res = send_control(port, PD_CTRL_PS_RDY); + res = send_control(PD_CTRL_PS_RDY); if (res < 0) { timeout = 10 * MSEC; - set_state(port, PD_STATE_SRC_DISCONNECTED); + set_state(PD_STATE_SRC_DISCONNECTED); break; } /* Switch to Rd and swap roles to sink */ - tcpm_set_cc(port, TYPEC_CC_RD); - pd[port].power_role = PD_ROLE_SINK; + tcpm_set_cc(TYPEC_CC_RD); + pd.power_role = PD_ROLE_SINK; /* Wait for PS_RDY from new source */ - set_state_timeout(port, get_time().val + + set_state_timeout(get_time().val + PD_T_PS_SOURCE_ON, PD_STATE_SNK_DISCONNECTED); } break; @@ -2561,30 +2496,30 @@ void pd_run_state_machine(int port) { int rstatus; #endif CPRINTS("TCPC p%d suspended!", port); - pd[port].req_suspend_state = 0; + pd.req_suspend_state = 0; #ifdef CONFIG_USB_PD_TCPC - pd_rx_disable_monitoring(port); - pd_hw_release(port); - pd_power_supply_reset(port); + pd_rx_disable_monitoring(); + pd_hw_release(); + pd_power_supply_reset(); #else - rstatus = tcpm_release(port); + rstatus = tcpm_release(); if (rstatus != 0 && rstatus != EC_ERROR_UNIMPLEMENTED) CPRINTS("TCPC p%d release failed!", port); #endif /* Wait for resume */ // getting rid of task stuff - //while (pd[port].task_state == PD_STATE_SUSPENDED) + //while (pd.task_state == PD_STATE_SUSPENDED) // task_wait_event(-1); #ifdef CONFIG_USB_PD_TCPC - pd_hw_init(port, PD_ROLE_DEFAULT(port)); + pd_hw_init( PD_ROLE_DEFAULT()); CPRINTS("TCPC p%d resumed!", port); #else - if (rstatus != EC_ERROR_UNIMPLEMENTED && pd_restart_tcpc(port) != 0) { + if (rstatus != EC_ERROR_UNIMPLEMENTED && pd_restart_tcpc() != 0) { /* stay in PD_STATE_SUSPENDED */ CPRINTS("TCPC p%d restart failed!", port); break; } - set_state(port, PD_DEFAULT_STATE(port)); + set_state(PD_DEFAULT_STATE()); CPRINTS("TCPC p%d resumed!", port); #endif break; @@ -2596,7 +2531,7 @@ void pd_run_state_machine(int port) { #else timeout = 10 * MSEC; #endif - tcpm_get_cc(port, &cc1, &cc2); + tcpm_get_cc(&cc1, &cc2); #ifdef CONFIG_USB_PD_DUAL_ROLE_AUTO_TOGGLE /* @@ -2604,11 +2539,11 @@ void pd_run_state_machine(int port) { * not already auto toggling and not try.src */ if (auto_toggle_supported && - !(pd[port].flags & PD_FLAGS_TCPC_DRP_TOGGLE) && - !(pd[port].flags & PD_FLAGS_TRY_SRC) && + !(pd.flags & PD_FLAGS_TCPC_DRP_TOGGLE) && + !(pd.flags & PD_FLAGS_TRY_SRC) && (cc1 == TYPEC_CC_VOLT_OPEN && cc2 == TYPEC_CC_VOLT_OPEN)) { - set_state(port, PD_STATE_DRP_AUTO_TOGGLE); + set_state( PD_STATE_DRP_AUTO_TOGGLE); timeout = 2*MSEC; break; } @@ -2616,12 +2551,12 @@ void pd_run_state_machine(int port) { /* Source connection monitoring */ if (cc1 != TYPEC_CC_VOLT_OPEN || cc2 != TYPEC_CC_VOLT_OPEN) { - pd[port].cc_state = PD_CC_NONE; + pd.cc_state = PD_CC_NONE; hard_reset_count = 0; new_cc_state = PD_CC_NONE; - pd[port].cc_debounce = get_time().val + + pd.cc_debounce = get_time().val + PD_T_CC_DEBOUNCE; - set_state(port, PD_STATE_SNK_DISCONNECTED_DEBOUNCE); + set_state (PD_STATE_SNK_DISCONNECTED_DEBOUNCE); timeout = 10 * MSEC; break; } @@ -2632,18 +2567,18 @@ void pd_run_state_machine(int port) { * normal dual role toggle until tDRPTryWait timer * expires. */ - if (pd[port].flags & PD_FLAGS_TRY_SRC) { - if (get_time().val > pd[port].try_src_marker) - pd[port].flags &= ~PD_FLAGS_TRY_SRC; + if (pd.flags & PD_FLAGS_TRY_SRC) { + if (get_time().val > pd.try_src_marker) + pd.flags &= ~PD_FLAGS_TRY_SRC; break; } /* If no source detected, check for role toggle. */ if (drp_state == PD_DRP_TOGGLE_ON && get_time().val >= next_role_swap) { /* Swap roles to source */ - pd[port].power_role = PD_ROLE_SOURCE; - set_state(port, PD_STATE_SRC_DISCONNECTED); - tcpm_set_cc(port, TYPEC_CC_RP); + pd.power_role = PD_ROLE_SOURCE; + set_state(PD_STATE_SRC_DISCONNECTED); + tcpm_set_cc(TYPEC_CC_RP); next_role_swap = get_time().val + PD_T_DRP_SRC; /* Swap states quickly */ @@ -2651,7 +2586,7 @@ void pd_run_state_machine(int port) { } break; case PD_STATE_SNK_DISCONNECTED_DEBOUNCE: - tcpm_get_cc(port, &cc1, &cc2); + tcpm_get_cc(&cc1, &cc2); if (cc_is_rp(cc1) && cc_is_rp(cc2)) { /* Debug accessory */ @@ -2660,7 +2595,7 @@ void pd_run_state_machine(int port) { new_cc_state = PD_CC_DFP_ATTACHED; } else { /* No connection any more */ - set_state(port, PD_STATE_SNK_DISCONNECTED); + set_state (PD_STATE_SNK_DISCONNECTED); timeout = 5 * MSEC; break; } @@ -2668,59 +2603,59 @@ void pd_run_state_machine(int port) { timeout = 20 * MSEC; /* Debounce the cc state */ - if (new_cc_state != pd[port].cc_state) { - pd[port].cc_debounce = get_time().val + + if (new_cc_state != pd.cc_state) { + pd.cc_debounce = get_time().val + PD_T_CC_DEBOUNCE; - pd[port].cc_state = new_cc_state; + pd.cc_state = new_cc_state; break; } /* Wait for CC debounce and VBUS present */ - if (get_time().val < pd[port].cc_debounce || !pd_is_vbus_present(port)) + if (get_time().val < pd.cc_debounce || !pd_is_vbus_present()) break; - if (pd_try_src_enable && !(pd[port].flags & PD_FLAGS_TRY_SRC)) { + if (pd_try_src_enable && !(pd.flags & PD_FLAGS_TRY_SRC)) { /* * If TRY_SRC is enabled, but not active, * then force attempt to connect as source. */ - pd[port].try_src_marker = get_time().val + PD_T_TRY_SRC; + pd.try_src_marker = get_time().val + PD_T_TRY_SRC; /* Swap roles to source */ - pd[port].power_role = PD_ROLE_SOURCE; - tcpm_set_cc(port, TYPEC_CC_RP); + pd.power_role = PD_ROLE_SOURCE; + tcpm_set_cc(TYPEC_CC_RP); timeout = 2 * MSEC; - set_state(port, PD_STATE_SRC_DISCONNECTED); + set_state(PD_STATE_SRC_DISCONNECTED); /* Set flag after the state change */ - pd[port].flags |= PD_FLAGS_TRY_SRC; + pd.flags |= PD_FLAGS_TRY_SRC; break; } /* We are attached */ - pd[port].polarity = get_snk_polarity(cc1, cc2); - tcpm_set_polarity(port, pd[port].polarity); + pd.polarity = get_snk_polarity(cc1, cc2); + tcpm_set_polarity(pd.polarity); /* reset message ID on connection */ - pd[port].msg_id = 0; + pd.msg_id = 0; /* initial data role for sink is UFP */ - pd_set_data_role(port, PD_ROLE_UFP); + pd_set_data_role( PD_ROLE_UFP); #if defined(CONFIG_CHARGE_MANAGER) - typec_curr = get_typec_current_limit(pd[port].polarity, cc1, cc2); + typec_curr = get_typec_current_limit(pd.polarity, cc1, cc2); //typec_set_input_current_limit( // port, typec_curr, TYPE_C_VOLTAGE); #endif /* If PD comm is enabled, enable TCPC RX */ - if (pd_comm_is_enabled(port)) - tcpm_set_rx_enable(port, 1); + if (pd_comm_is_enabled()) + tcpm_set_rx_enable(1); /* DFP is attached */ if (new_cc_state == PD_CC_DFP_ATTACHED || new_cc_state == PD_CC_DEBUG_ACC) { - pd[port].flags |= PD_FLAGS_CHECK_PR_ROLE | + pd.flags |= PD_FLAGS_CHECK_PR_ROLE | PD_FLAGS_CHECK_DR_ROLE | PD_FLAGS_CHECK_IDENTITY; if (new_cc_state == PD_CC_DEBUG_ACC) - pd[port].flags |= + pd.flags |= PD_FLAGS_TS_DTS_PARTNER; - send_control(port, PD_CTRL_GET_SOURCE_CAP); - set_state(port, PD_STATE_SNK_DISCOVERY); + send_control(PD_CTRL_GET_SOURCE_CAP); + set_state (PD_STATE_SNK_DISCOVERY); timeout = 10 * MSEC; //hook_call_deferred( // &pd_usb_billboard_deferred_data, @@ -2728,50 +2663,50 @@ void pd_run_state_machine(int port) { } break; case PD_STATE_SNK_HARD_RESET_RECOVER: - if (pd[port].last_state != pd[port].task_state) - pd[port].flags |= PD_FLAGS_CHECK_IDENTITY; + if (pd.last_state != pd.task_state) + pd.flags |= PD_FLAGS_CHECK_IDENTITY; #ifdef CONFIG_USB_PD_VBUS_DETECT_NONE /* * Can't measure vbus state so this is the maximum * recovery time for the source. */ - if (pd[port].last_state != pd[port].task_state) - set_state_timeout(port, get_time().val + + if (pd.last_state != pd.task_state) + set_state_timeout( get_time().val + PD_T_SAFE_0V + PD_T_SRC_RECOVER_MAX + PD_T_SRC_TURN_ON, PD_STATE_SNK_DISCONNECTED); #else /* Wait for VBUS to go low and then high*/ - if (pd[port].last_state != pd[port].task_state) { + if (pd.last_state != pd.task_state) { snk_hard_reset_vbus_off = 0; - set_state_timeout(port, get_time().val + + set_state_timeout(get_time().val + PD_T_SAFE_0V, hard_reset_count < PD_HARD_RESET_COUNT ? PD_STATE_HARD_RESET_SEND : PD_STATE_SNK_DISCOVERY); } - if (!pd_is_vbus_present(port) && !snk_hard_reset_vbus_off) { + if (!pd_is_vbus_present() && !snk_hard_reset_vbus_off) { /* VBUS has gone low, reset timeout */ snk_hard_reset_vbus_off = 1; - set_state_timeout(port, get_time().val + + set_state_timeout(get_time().val + PD_T_SRC_RECOVER_MAX + PD_T_SRC_TURN_ON, PD_STATE_SNK_DISCONNECTED); } - if (pd_is_vbus_present(port) && snk_hard_reset_vbus_off) { + if (pd_is_vbus_present() && snk_hard_reset_vbus_off) { #ifdef CONFIG_USB_PD_TCPM_TCPCI /* * After transmitting hard reset, TCPM writes * to RECEIVE_MESSAGE register to enable * PD message passing. */ - if (pd_comm_is_enabled(port)) - tcpm_set_rx_enable(port, 1); + if (pd_comm_is_enabled()) + tcpm_set_rx_enable( 1); #endif /* CONFIG_USB_PD_TCPM_TCPCI */ /* VBUS went high again */ - set_state(port, PD_STATE_SNK_DISCOVERY); + set_state (PD_STATE_SNK_DISCOVERY); timeout = 10 * MSEC; } @@ -2783,16 +2718,16 @@ void pd_run_state_machine(int port) { break; case PD_STATE_SNK_DISCOVERY: /* Wait for source cap expired only if we are enabled */ - if ((pd[port].last_state != pd[port].task_state) - && pd_comm_is_enabled(port)) { + if ((pd.last_state != pd.task_state) + && pd_comm_is_enabled()) { /* * If VBUS has never been low, and we timeout * waiting for source cap, try a soft reset * first, in case we were already in a stable * contract before this boot. */ - if (pd[port].flags & PD_FLAGS_VBUS_NEVER_LOW) - set_state_timeout(port, get_time().val + + if (pd.flags & PD_FLAGS_VBUS_NEVER_LOW) + set_state_timeout(get_time().val + PD_T_SINK_WAIT_CAP, PD_STATE_SOFT_RESET); /* * If we haven't passed hard reset counter, @@ -2800,12 +2735,12 @@ void pd_run_state_machine(int port) { * NoResponseTimer. */ else if (hard_reset_count < PD_HARD_RESET_COUNT) - set_state_timeout(port, get_time().val + + set_state_timeout(get_time().val + PD_T_SINK_WAIT_CAP, PD_STATE_HARD_RESET_SEND); - else if (pd[port].flags & + else if (pd.flags & PD_FLAGS_PREVIOUS_PD_CONN) /* ErrorRecovery */ - set_state_timeout(port, get_time().val + + set_state_timeout(get_time().val + PD_T_NO_RESPONSE, PD_STATE_SNK_DISCONNECTED); #if defined(CONFIG_CHARGE_MANAGER) /* @@ -2814,7 +2749,7 @@ void pd_run_state_machine(int port) { * typec current limit. So, set to 0 so that * we guarantee this is revised below. */ - if (pd[port].last_state != PD_STATE_SNK_DISCONNECTED_DEBOUNCE) + if (pd.last_state != PD_STATE_SNK_DISCONNECTED_DEBOUNCE) typec_curr = 0; #endif } @@ -2823,13 +2758,13 @@ void pd_run_state_machine(int port) { timeout = PD_T_SINK_ADJ - PD_T_DEBOUNCE; /* Check if CC pull-up has changed */ - tcpm_get_cc(port, &cc1, &cc2); + tcpm_get_cc(&cc1, &cc2); if (typec_curr - != get_typec_current_limit(pd[port].polarity, cc1, cc2)) { + != get_typec_current_limit(pd.polarity, cc1, cc2)) { /* debounce signal by requiring two reads */ if (typec_curr_change) { /* set new input current limit */ - typec_curr = get_typec_current_limit(pd[port].polarity, cc1, + typec_curr = get_typec_current_limit(pd.polarity, cc1, cc2); //typec_set_input_current_limit( // port, typec_curr, TYPE_C_VOLTAGE); @@ -2845,16 +2780,16 @@ void pd_run_state_machine(int port) { break; case PD_STATE_SNK_REQUESTED: /* Wait for ACCEPT or REJECT */ - if (pd[port].last_state != pd[port].task_state) { + if (pd.last_state != pd.task_state) { hard_reset_count = 0; - set_state_timeout(port, get_time().val + + set_state_timeout(get_time().val + PD_T_SENDER_RESPONSE, PD_STATE_HARD_RESET_SEND); } break; case PD_STATE_SNK_TRANSITION: /* Wait for PS_RDY */ - if (pd[port].last_state != pd[port].task_state) - set_state_timeout(port, get_time().val + + if (pd.last_state != pd.task_state) + set_state_timeout(get_time().val + PD_T_PS_TRANSITION, PD_STATE_HARD_RESET_SEND); break; case PD_STATE_SNK_READY: @@ -2865,36 +2800,36 @@ void pd_run_state_machine(int port) { * incoming packet or if VDO response pending to avoid * collisions. */ - if (incoming_packet || (pd[port].vdm_state == VDM_STATE_BUSY)) + if (incoming_packet || (pd.vdm_state == VDM_STATE_BUSY)) break; /* Check for new power to request */ - if (pd[port].new_power_request) { - if (pd_send_request_msg(port, 0) != EC_SUCCESS) - set_state(port, PD_STATE_SOFT_RESET); + if (pd.new_power_request) { + if (pd_send_request_msg(0) != EC_SUCCESS) + set_state(PD_STATE_SOFT_RESET); break; } /* Check power role policy, which may trigger a swap */ - if (pd[port].flags & PD_FLAGS_CHECK_PR_ROLE) { - pd_check_pr_role(port, PD_ROLE_SINK, pd[port].flags); - pd[port].flags &= ~PD_FLAGS_CHECK_PR_ROLE; + if (pd.flags & PD_FLAGS_CHECK_PR_ROLE) { + pd_check_pr_role( PD_ROLE_SINK, pd.flags); + pd.flags &= ~PD_FLAGS_CHECK_PR_ROLE; break; } /* Check data role policy, which may trigger a swap */ - if (pd[port].flags & PD_FLAGS_CHECK_DR_ROLE) { - pd_check_dr_role(port, pd[port].data_role, pd[port].flags); - pd[port].flags &= ~PD_FLAGS_CHECK_DR_ROLE; + if (pd.flags & PD_FLAGS_CHECK_DR_ROLE) { + pd_check_dr_role(pd.data_role, pd.flags); + pd.flags &= ~PD_FLAGS_CHECK_DR_ROLE; break; } /* If DFP, send discovery SVDMs */ - if (pd[port].data_role == PD_ROLE_DFP - && (pd[port].flags & PD_FLAGS_CHECK_IDENTITY)) { - pd_send_vdm(port, USB_SID_PD, + if (pd.data_role == PD_ROLE_DFP + && (pd.flags & PD_FLAGS_CHECK_IDENTITY)) { + pd_send_vdm( USB_SID_PD, CMD_DISCOVER_IDENT, NULL, 0); - pd[port].flags &= ~PD_FLAGS_CHECK_IDENTITY; + pd.flags &= ~PD_FLAGS_CHECK_IDENTITY; break; } @@ -2902,8 +2837,8 @@ void pd_run_state_machine(int port) { timeout = 200 * MSEC; break; case PD_STATE_SNK_SWAP_INIT: - if (pd[port].last_state != pd[port].task_state) { - res = send_control(port, PD_CTRL_PR_SWAP); + if (pd.last_state != pd.task_state) { + res = send_control(PD_CTRL_PR_SWAP); if (res < 0) { timeout = 10 * MSEC; /* @@ -2911,74 +2846,73 @@ void pd_run_state_machine(int port) { * soft reset, otherwise ignore * failure. */ - set_state(port, - res == -1 ? PD_STATE_SOFT_RESET : PD_STATE_SNK_READY); + set_state(res == -1 ? PD_STATE_SOFT_RESET : PD_STATE_SNK_READY); break; } /* Wait for accept or reject */ - set_state_timeout(port, get_time().val + + set_state_timeout(get_time().val + PD_T_SENDER_RESPONSE, PD_STATE_SNK_READY); } break; case PD_STATE_SNK_SWAP_SNK_DISABLE: /* Stop drawing power */ - pd_set_input_current_limit(port, 0, 0); + pd_set_input_current_limit(0, 0); #ifdef CONFIG_CHARGE_MANAGER - //typec_set_input_current_limit(port, 0, 0); - //charge_manager_set_ceil(port, + //typec_set_input_current_limit( 0, 0); + //charge_manager_set_ceil( // CEIL_REQUESTOR_PD, // CHARGE_CEIL_NONE); #endif - set_state(port, PD_STATE_SNK_SWAP_SRC_DISABLE); + set_state (PD_STATE_SNK_SWAP_SRC_DISABLE); timeout = 10 * MSEC; break; case PD_STATE_SNK_SWAP_SRC_DISABLE: /* Wait for PS_RDY */ - if (pd[port].last_state != pd[port].task_state) - set_state_timeout(port, get_time().val + + if (pd.last_state != pd.task_state) + set_state_timeout(get_time().val + PD_T_PS_SOURCE_OFF, PD_STATE_HARD_RESET_SEND); break; case PD_STATE_SNK_SWAP_STANDBY: - if (pd[port].last_state != pd[port].task_state) { + if (pd.last_state != pd.task_state) { /* Switch to Rp and enable power supply */ - tcpm_set_cc(port, TYPEC_CC_RP); - if (pd_set_power_supply_ready(port)) { + tcpm_set_cc(TYPEC_CC_RP); + if (pd_set_power_supply_ready()) { /* Restore Rd */ - tcpm_set_cc(port, TYPEC_CC_RD); + tcpm_set_cc(TYPEC_CC_RD); timeout = 10 * MSEC; - set_state(port, PD_STATE_SNK_DISCONNECTED); + set_state (PD_STATE_SNK_DISCONNECTED); break; } /* Wait for power supply to turn on */ - set_state_timeout(port, get_time().val + + set_state_timeout(get_time().val + PD_POWER_SUPPLY_TURN_ON_DELAY, PD_STATE_SNK_SWAP_COMPLETE); } break; case PD_STATE_SNK_SWAP_COMPLETE: /* Send PS_RDY and change to source role */ - res = send_control(port, PD_CTRL_PS_RDY); + res = send_control(PD_CTRL_PS_RDY); if (res < 0) { /* Restore Rd */ - tcpm_set_cc(port, TYPEC_CC_RD); - pd_power_supply_reset(port); + tcpm_set_cc(TYPEC_CC_RD); + pd_power_supply_reset(); timeout = 10 * MSEC; - set_state(port, PD_STATE_SNK_DISCONNECTED); + set_state (PD_STATE_SNK_DISCONNECTED); break; } /* Don't send GET_SINK_CAP on swap */ snk_cap_count = PD_SNK_CAP_RETRIES + 1; caps_count = 0; - pd[port].msg_id = 0; - pd[port].power_role = PD_ROLE_SOURCE; - pd_update_roles(port); - set_state(port, PD_STATE_SRC_DISCOVERY); + pd.msg_id = 0; + pd.power_role = PD_ROLE_SOURCE; + pd_update_roles(); + set_state(PD_STATE_SRC_DISCOVERY); timeout = 10 * MSEC; break; #ifdef CONFIG_USBC_VCONN_SWAP case PD_STATE_VCONN_SWAP_SEND: - if (pd[port].last_state != pd[port].task_state) { - res = send_control(port, PD_CTRL_VCONN_SWAP); + if (pd.last_state != pd.task_state) { + res = send_control( PD_CTRL_VCONN_SWAP); if (res < 0) { timeout = 10*MSEC; /* @@ -2986,39 +2920,39 @@ void pd_run_state_machine(int port) { * soft reset, otherwise ignore * failure. */ - set_state(port, res == -1 ? + set_state( res == -1 ? PD_STATE_SOFT_RESET : - READY_RETURN_STATE(port)); + READY_RETURN_STATE()); break; } /* Wait for accept or reject */ - set_state_timeout(port, + set_state_timeout( get_time().val + PD_T_SENDER_RESPONSE, - READY_RETURN_STATE(port)); + READY_RETURN_STATE()); } break; case PD_STATE_VCONN_SWAP_INIT: - if (pd[port].last_state != pd[port].task_state) { - if (!(pd[port].flags & PD_FLAGS_VCONN_ON)) { + if (pd.last_state != pd.task_state) { + if (!(pd.flags & PD_FLAGS_VCONN_ON)) { /* Turn VCONN on and wait for it */ - tcpm_set_vconn(port, 1); - set_state_timeout(port, + tcpm_set_vconn( 1); + set_state_timeout( get_time().val + PD_VCONN_SWAP_DELAY, PD_STATE_VCONN_SWAP_READY); } else { - set_state_timeout(port, + set_state_timeout( get_time().val + PD_T_VCONN_SOURCE_ON, - READY_RETURN_STATE(port)); + READY_RETURN_STATE()); } } break; case PD_STATE_VCONN_SWAP_READY: - if (pd[port].last_state != pd[port].task_state) { - if (!(pd[port].flags & PD_FLAGS_VCONN_ON)) { + if (pd.last_state != pd.task_state) { + if (!(pd.flags & PD_FLAGS_VCONN_ON)) { /* VCONN is now on, send PS_RDY */ - pd[port].flags |= PD_FLAGS_VCONN_ON; - res = send_control(port, + pd.flags |= PD_FLAGS_VCONN_ON; + res = send_control( PD_CTRL_PS_RDY); if (res == -1) { timeout = 10*MSEC; @@ -3026,67 +2960,67 @@ void pd_run_state_machine(int port) { * If failed to get goodCRC, * send soft reset */ - set_state(port, + set_state( PD_STATE_SOFT_RESET); break; } - set_state(port, - READY_RETURN_STATE(port)); + set_state( + READY_RETURN_STATE()); } else { /* Turn VCONN off and wait for it */ - tcpm_set_vconn(port, 0); - pd[port].flags &= ~PD_FLAGS_VCONN_ON; - set_state_timeout(port, + tcpm_set_vconn( 0); + pd.flags &= ~PD_FLAGS_VCONN_ON; + set_state_timeout( get_time().val + PD_VCONN_SWAP_DELAY, - READY_RETURN_STATE(port)); + READY_RETURN_STATE()); } } break; #endif /* CONFIG_USBC_VCONN_SWAP */ #endif /* CONFIG_USB_PD_DUAL_ROLE */ case PD_STATE_SOFT_RESET: - if (pd[port].last_state != pd[port].task_state) { + if (pd.last_state != pd.task_state) { /* Message ID of soft reset is always 0 */ - pd[port].msg_id = 0; - res = send_control(port, PD_CTRL_SOFT_RESET); + pd.msg_id = 0; + res = send_control(PD_CTRL_SOFT_RESET); /* if soft reset failed, try hard reset. */ if (res < 0) { - set_state(port, PD_STATE_HARD_RESET_SEND); + set_state(PD_STATE_HARD_RESET_SEND); timeout = 5 * MSEC; break; } - set_state_timeout(port, get_time().val + PD_T_SENDER_RESPONSE, + set_state_timeout(get_time().val + PD_T_SENDER_RESPONSE, PD_STATE_HARD_RESET_SEND); } break; case PD_STATE_HARD_RESET_SEND: hard_reset_count++; - if (pd[port].last_state != pd[port].task_state) + if (pd.last_state != pd.task_state) hard_reset_sent = 0; #ifdef CONFIG_CHARGE_MANAGER - if (pd[port].last_state == PD_STATE_SNK_DISCOVERY - || (pd[port].last_state == PD_STATE_SOFT_RESET - && (pd[port].flags & PD_FLAGS_VBUS_NEVER_LOW))) { - pd[port].flags &= ~PD_FLAGS_VBUS_NEVER_LOW; - /* - * If discovery timed out, assume that we - * have a dedicated charger attached. This - * may not be a correct assumption according - * to the specification, but it generally - * works in practice and the harmful - * effects of a wrong assumption here - * are minimal. - */ - //charge_manager_update_dualrole(port, - // CAP_DEDICATED); - } +// if (pd.last_state == PD_STATE_SNK_DISCOVERY +// || (pd.last_state == PD_STATE_SOFT_RESET +// && (pd.flags & PD_FLAGS_VBUS_NEVER_LOW))) { +// pd.flags &= ~PD_FLAGS_VBUS_NEVER_LOW; +// /* +// * If discovery timed out, assume that we +// * have a dedicated charger attached. This +// * may not be a correct assumption according +// * to the specification, but it generally +// * works in practice and the harmful +// * effects of a wrong assumption here +// * are minimal. +// */ +// //charge_manager_update_dualrole( +// // CAP_DEDICATED); +// } #endif /* try sending hard reset until it succeeds */ if (!hard_reset_sent) { - if (pd_transmit(port, TCPC_TX_HARD_RESET, 0, NULL) < 0) { + if (pd_transmit(TCPC_TX_HARD_RESET, 0, NULL) < 0) { timeout = 10 * MSEC; break; } @@ -3097,13 +3031,10 @@ void pd_run_state_machine(int port) { * If we are source, delay before cutting power * to allow sink time to get hard reset. */ - if (pd[port].power_role == PD_ROLE_SOURCE) { - set_state_timeout(port, get_time().val + PD_T_PS_HARD_RESET, - PD_STATE_HARD_RESET_EXECUTE); - } else { - set_state(port, PD_STATE_HARD_RESET_EXECUTE); - timeout = 10 * MSEC; - } + + set_state(PD_STATE_HARD_RESET_EXECUTE); + timeout = 10 * MSEC; + } break; case PD_STATE_HARD_RESET_EXECUTE: @@ -3112,30 +3043,30 @@ void pd_run_state_machine(int port) { * If hard reset while in the last stages of power * swap, then we need to restore our CC resistor. */ - if (pd[port].last_state == PD_STATE_SNK_SWAP_STANDBY) - tcpm_set_cc(port, TYPEC_CC_RD); + if (pd.last_state == PD_STATE_SNK_SWAP_STANDBY) + tcpm_set_cc(TYPEC_CC_RD); #endif /* reset our own state machine */ - pd_execute_hard_reset(port); + pd_execute_hard_reset(); timeout = 10 * MSEC; break; #ifdef CONFIG_COMMON_RUNTIME case PD_STATE_BIST_RX: - send_bist_cmd(port); + send_bist_cmd(); /* Delay at least enough for partner to finish BIST */ timeout = PD_T_BIST_RECEIVE + 20*MSEC; /* Set to appropriate port disconnected state */ - set_state(port, DUAL_ROLE_IF_ELSE(port, + set_state( DUAL_ROLE_IF_ELSE( PD_STATE_SNK_DISCONNECTED, PD_STATE_SRC_DISCONNECTED)); break; case PD_STATE_BIST_TX: - pd_transmit(port, TCPC_TX_BIST_MODE_2, 0, NULL); + pd_transmit( TCPC_TX_BIST_MODE_2, 0, NULL); /* Delay at least enough to finish sending BIST */ timeout = PD_T_BIST_TRANSMIT + 20*MSEC; /* Set to appropriate port disconnected state */ - set_state(port, DUAL_ROLE_IF_ELSE(port, + set_state( DUAL_ROLE_IF_ELSE( PD_STATE_SNK_DISCONNECTED, PD_STATE_SRC_DISCONNECTED)); break; @@ -3148,7 +3079,7 @@ void pd_run_state_machine(int port) { assert(auto_toggle_supported); /* Check for connection */ - tcpm_get_cc(port, &cc1, &cc2); + tcpm_get_cc( &cc1, &cc2); /* Set to appropriate port state */ if (cc1 == TYPEC_CC_VOLT_OPEN && @@ -3172,29 +3103,29 @@ void pd_run_state_machine(int port) { next_state = PD_STATE_DRP_AUTO_TOGGLE; if (next_state != PD_STATE_DRP_AUTO_TOGGLE) { - tcpm_set_drp_toggle(port, 0); + tcpm_set_drp_toggle( 0); #ifdef CONFIG_USB_PD_TCPC_LOW_POWER CPRINTS("TCPC p%d Exit Low Power Mode", port); #endif } if (next_state == PD_STATE_SNK_DISCONNECTED) { - tcpm_set_cc(port, TYPEC_CC_RD); - pd[port].power_role = PD_ROLE_SINK; + tcpm_set_cc( TYPEC_CC_RD); + pd.power_role = PD_ROLE_SINK; timeout = 2*MSEC; } else if (next_state == PD_STATE_SRC_DISCONNECTED) { - tcpm_set_cc(port, TYPEC_CC_RP); - pd[port].power_role = PD_ROLE_SOURCE; + tcpm_set_cc( TYPEC_CC_RP); + pd.power_role = PD_ROLE_SOURCE; timeout = 2*MSEC; } else { - tcpm_set_drp_toggle(port, 1); - pd[port].flags |= PD_FLAGS_TCPC_DRP_TOGGLE; + tcpm_set_drp_toggle( 1); + pd.flags |= PD_FLAGS_TCPC_DRP_TOGGLE; timeout = -1; #ifdef CONFIG_USB_PD_TCPC_LOW_POWER CPRINTS("TCPC p%d Low Power Mode", port); #endif } - set_state(port, next_state); + set_state( next_state); break; } @@ -3203,69 +3134,41 @@ void pd_run_state_machine(int port) { break; } - pd[port].last_state = this_state; + pd.last_state = this_state; /* * Check for state timeout, and if not check if need to adjust * timeout value to wake up on the next state timeout. */ now = get_time(); - if (pd[port].timeout) { - if (now.val >= pd[port].timeout) { - set_state(port, pd[port].timeout_state); + if (pd.timeout) { + if (now.val >= pd.timeout) { + set_state(pd.timeout_state); /* On a state timeout, run next state soon */ timeout = timeout < 10 * MSEC ? timeout : 10 * MSEC; - } else if (pd[port].timeout - now.val < timeout) { - timeout = pd[port].timeout - now.val; + } else if (pd.timeout - now.val < timeout) { + timeout = pd.timeout - now.val; } } /* Check for disconnection if we're connected */ - if (!pd_is_connected(port)) + if (!pd_is_connected()) return; #ifdef CONFIG_USB_PD_DUAL_ROLE - if (pd_is_power_swapping(port)) + if (pd_is_power_swapping()) return; #endif - if (pd[port].power_role == PD_ROLE_SOURCE) { - /* Source: detect disconnect by monitoring CC */ - tcpm_get_cc(port, &cc1, &cc2); - if (pd[port].polarity) - cc1 = cc2; - if (cc1 == TYPEC_CC_VOLT_OPEN) { - set_state(port, PD_STATE_SRC_DISCONNECTED); - /* Debouncing */ - timeout = 10 * MSEC; -#ifdef CONFIG_USB_PD_DUAL_ROLE - /* - * If Try.SRC is configured, then ATTACHED_SRC - * needs to transition to TryWait.SNK. Change - * power role to SNK and start state timer. - */ - if (pd_try_src_enable) { - /* Swap roles to sink */ - pd[port].power_role = PD_ROLE_SINK; - tcpm_set_cc(port, TYPEC_CC_RD); - /* Set timer for TryWait.SNK state */ - pd[port].try_src_marker = get_time().val + PD_T_TRY_WAIT; - /* Advance to TryWait.SNK state */ - set_state(port, PD_STATE_SNK_DISCONNECTED); - /* Mark state as TryWait.SNK */ - pd[port].flags |= PD_FLAGS_TRY_SRC; - } -#endif - } - } + #ifdef CONFIG_USB_PD_DUAL_ROLE /* * Sink disconnect if VBUS is low and we are not recovering * a hard reset. */ - if (pd[port].power_role == PD_ROLE_SINK && !pd_is_vbus_present(port) - && pd[port].task_state != PD_STATE_SNK_HARD_RESET_RECOVER - && pd[port].task_state != PD_STATE_HARD_RESET_EXECUTE) { + if (pd.power_role == PD_ROLE_SINK && !pd_is_vbus_present() + && pd.task_state != PD_STATE_SNK_HARD_RESET_RECOVER + && pd.task_state != PD_STATE_HARD_RESET_EXECUTE) { /* Sink: detect disconnect by monitoring VBUS */ - set_state(port, PD_STATE_SNK_DISCONNECTED); + set_state (PD_STATE_SNK_DISCONNECTED); /* set timeout small to reconnect fast */ timeout = 5 * MSEC; } @@ -3320,34 +3223,34 @@ void pd_run_state_machine(int port) { * port's default state. */ -void pd_set_suspend(int port, int enable) +void pd_set_suspend( int enable) { int tries = 300; if (enable) { - pd[port].req_suspend_state = 1; + pd.req_suspend_state = 1; do { // getting rid of task stuff - //task_wake(PD_PORT_TO_TASK_ID(port)); - if (pd[port].task_state == PD_STATE_SUSPENDED) + //task_wake(PD_PORT_TO_TASK_ID()); + if (pd.task_state == PD_STATE_SUSPENDED) break; msleep(1); } while (--tries != 0); if (!tries) CPRINTS("TCPC p%d set_suspend failed!", port); } else { - if (pd[port].task_state != PD_STATE_SUSPENDED) + if (pd.task_state != PD_STATE_SUSPENDED) CPRINTS("TCPC p%d suspend disable request " "while not suspended!", port); - set_state(port, PD_DEFAULT_STATE(port)); + set_state( PD_DEFAULT_STATE()); // getting rid of task stuff - //task_wake(PD_PORT_TO_TASK_ID(port)); + //task_wake(PD_PORT_TO_TASK_ID()); } } -int pd_is_port_enabled(int port) +int pd_is_port_enabled() { - switch (pd[port].task_state) { + switch (pd.task_state) { case PD_STATE_DISABLED: case PD_STATE_SUSPENDED: return 0; @@ -3423,22 +3326,22 @@ static int remote_flashing(int argc, char **argv) flash_offset[port] += argc * 4; } - pd_send_vdm(port, USB_VID_GOOGLE, cmd, data, cnt); + pd_send_vdm( USB_VID_GOOGLE, cmd, data, cnt); /* Wait until VDM is done */ - while (pd[port].vdm_state > 0) + while (pd.vdm_state > 0) task_wait_event(100*MSEC); - ccprintf("DONE %d\n", pd[port].vdm_state); + ccprintf("DONE %d\n", pd.vdm_state); return EC_SUCCESS; } #endif /* defined(CONFIG_CMD_PD) && defined(CONFIG_CMD_PD_FLASH) */ #if defined(CONFIG_USB_PD_ALT_MODE) && !defined(CONFIG_USB_PD_ALT_MODE_DFP) -void pd_send_hpd(int port, enum hpd_event hpd) +void pd_send_hpd( enum hpd_event hpd) { uint32_t data[1]; - int opos = pd_alt_mode(port, USB_SID_DISPLAYPORT); + int opos = pd_alt_mode( USB_SID_DISPLAYPORT); if (!opos) return; @@ -3450,7 +3353,7 @@ void pd_send_hpd(int port, enum hpd_event hpd) 1, /* enabled */ 0, /* power low */ 0x2); - pd_send_vdm(port, USB_SID_DISPLAYPORT, + pd_send_vdm( USB_SID_DISPLAYPORT, VDO_OPOS(opos) | CMD_ATTENTION, data, 1); /* Wait until VDM is done. */ while (pd[0].vdm_state > 0) @@ -3458,70 +3361,70 @@ void pd_send_hpd(int port, enum hpd_event hpd) } #endif -int pd_fetch_acc_log_entry(int port) +int pd_fetch_acc_log_entry() { timestamp_t timeout; /* Cannot send a VDM now, the host should retry */ - if (pd[port].vdm_state > 0) - return pd[port].vdm_state == VDM_STATE_BUSY ? + if (pd.vdm_state > 0) + return pd.vdm_state == VDM_STATE_BUSY ? EC_RES_BUSY : EC_RES_UNAVAILABLE; - pd_send_vdm(port, USB_VID_GOOGLE, VDO_CMD_GET_LOG, NULL, 0); + pd_send_vdm( USB_VID_GOOGLE, VDO_CMD_GET_LOG, NULL, 0); timeout.val = get_time().val + 75*MSEC; /* Wait until VDM is done */ - while ((pd[port].vdm_state > 0) && + while ((pd.vdm_state > 0) && (get_time().val < timeout.val)) task_wait_event(10*MSEC); - if (pd[port].vdm_state > 0) + if (pd.vdm_state > 0) return EC_RES_TIMEOUT; - else if (pd[port].vdm_state < 0) + else if (pd.vdm_state < 0) return EC_RES_ERROR; return EC_RES_SUCCESS; } #ifdef CONFIG_USB_PD_DUAL_ROLE -void pd_request_source_voltage(int port, int mv) +void pd_request_source_voltage( int mv) { pd_set_max_voltage(mv); - if (pd[port].task_state == PD_STATE_SNK_READY || - pd[port].task_state == PD_STATE_SNK_TRANSITION) { + if (pd.task_state == PD_STATE_SNK_READY || + pd.task_state == PD_STATE_SNK_TRANSITION) { /* Set flag to send new power request in pd_task */ - pd[port].new_power_request = 1; + pd.new_power_request = 1; } else { - pd[port].power_role = PD_ROLE_SINK; - tcpm_set_cc(port, TYPEC_CC_RD); - set_state(port, PD_STATE_SNK_DISCONNECTED); + pd.power_role = PD_ROLE_SINK; + tcpm_set_cc( TYPEC_CC_RD); + set_state( PD_STATE_SNK_DISCONNECTED); } // getting rid of task stuff - //task_wake(PD_PORT_TO_TASK_ID(port)); + //task_wake(PD_PORT_TO_TASK_ID()); } -void pd_set_external_voltage_limit(int port, int mv) +void pd_set_external_voltage_limit( int mv) { pd_set_max_voltage(mv); - if (pd[port].task_state == PD_STATE_SNK_READY || - pd[port].task_state == PD_STATE_SNK_TRANSITION) { + if (pd.task_state == PD_STATE_SNK_READY || + pd.task_state == PD_STATE_SNK_TRANSITION) { /* Set flag to send new power request in pd_task */ - pd[port].new_power_request = 1; + pd.new_power_request = 1; // getting rid of task stuff - //task_wake(PD_PORT_TO_TASK_ID(port)); + //task_wake(PD_PORT_TO_TASK_ID()); } } -void pd_update_contract(int port) +void pd_update_contract() { - if ((pd[port].task_state >= PD_STATE_SRC_NEGOCIATE) && - (pd[port].task_state <= PD_STATE_SRC_GET_SINK_CAP)) { - pd[port].flags |= PD_FLAGS_UPDATE_SRC_CAPS; + if ((pd.task_state >= PD_STATE_SRC_NEGOCIATE) && + (pd.task_state <= PD_STATE_SRC_GET_SINK_CAP)) { + pd.flags |= PD_FLAGS_UPDATE_SRC_CAPS; // getting rid of task stuff - //task_wake(PD_PORT_TO_TASK_ID(port)); + //task_wake(PD_PORT_TO_TASK_ID()); } } @@ -3628,25 +3531,25 @@ static int command_pd(int argc, char **argv) #if defined(CONFIG_CMD_PD) && defined(CONFIG_USB_PD_DUAL_ROLE) if (!strcasecmp(argv[2], "tx")) { - set_state(port, PD_STATE_SNK_DISCOVERY); + set_state( PD_STATE_SNK_DISCOVERY); // getting rid of task stuff - //task_wake(PD_PORT_TO_TASK_ID(port)); + //task_wake(PD_PORT_TO_TASK_ID()); } else if (!strcasecmp(argv[2], "bist_rx")) { - set_state(port, PD_STATE_BIST_RX); + set_state( PD_STATE_BIST_RX); // getting rid of task stuff - //task_wake(PD_PORT_TO_TASK_ID(port)); + //task_wake(PD_PORT_TO_TASK_ID()); } else if (!strcasecmp(argv[2], "bist_tx")) { if (*e) return EC_ERROR_PARAM3; - set_state(port, PD_STATE_BIST_TX); + set_state( PD_STATE_BIST_TX); // getting rid of task stuff - //task_wake(PD_PORT_TO_TASK_ID(port)); + //task_wake(PD_PORT_TO_TASK_ID()); } else if (!strcasecmp(argv[2], "charger")) { - pd[port].power_role = PD_ROLE_SOURCE; - tcpm_set_cc(port, TYPEC_CC_RP); - set_state(port, PD_STATE_SRC_DISCONNECTED); + pd.power_role = PD_ROLE_SOURCE; + tcpm_set_cc( TYPEC_CC_RP); + set_state( PD_STATE_SRC_DISCONNECTED); // getting rid of task stuff - //task_wake(PD_PORT_TO_TASK_ID(port)); + //task_wake(PD_PORT_TO_TASK_ID()); } else if (!strncasecmp(argv[2], "dev", 3)) { int max_volt; if (argc >= 4) @@ -3654,42 +3557,42 @@ static int command_pd(int argc, char **argv) else max_volt = pd_get_max_voltage(); - pd_request_source_voltage(port, max_volt); + pd_request_source_voltage( max_volt); ccprintf("max req: %dmV\n", max_volt); } else if (!strcasecmp(argv[2], "disable")) { - pd_comm_enable(port, 0); + pd_comm_enable( 0); ccprintf("Port C%d disable\n", port); return EC_SUCCESS; } else if (!strcasecmp(argv[2], "enable")) { - pd_comm_enable(port, 1); + pd_comm_enable( 1); ccprintf("Port C%d enabled\n", port); return EC_SUCCESS; } else if (!strncasecmp(argv[2], "hard", 4)) { - set_state(port, PD_STATE_HARD_RESET_SEND); + set_state( PD_STATE_HARD_RESET_SEND); // getting rid of task stuff - //task_wake(PD_PORT_TO_TASK_ID(port)); + //task_wake(PD_PORT_TO_TASK_ID()); } else if (!strncasecmp(argv[2], "info", 4)) { int i; ccprintf("Hash "); for (i = 0; i < PD_RW_HASH_SIZE / 4; i++) - ccprintf("%08x ", pd[port].dev_rw_hash[i]); + ccprintf("%08x ", pd.dev_rw_hash[i]); ccprintf("\nImage %s\n", system_image_copy_t_to_string( - pd[port].current_image)); + pd.current_image)); } else if (!strncasecmp(argv[2], "soft", 4)) { - set_state(port, PD_STATE_SOFT_RESET); + set_state( PD_STATE_SOFT_RESET); // getting rid of task stuff - //task_wake(PD_PORT_TO_TASK_ID(port)); + //task_wake(PD_PORT_TO_TASK_ID()); } else if (!strncasecmp(argv[2], "swap", 4)) { if (argc < 4) return EC_ERROR_PARAM_COUNT; if (!strncasecmp(argv[3], "power", 5)) - pd_request_power_swap(port); + pd_request_power_swap(); else if (!strncasecmp(argv[3], "data", 4)) - pd_request_data_swap(port); + pd_request_data_swap(); #ifdef CONFIG_USBC_VCONN_SWAP else if (!strncasecmp(argv[3], "vconn", 5)) - pd_request_vconn_swap(port); + pd_request_vconn_swap(); #endif else return EC_ERROR_PARAM3; @@ -3700,11 +3603,11 @@ static int command_pd(int argc, char **argv) enable = strtoi(argv[3], &e, 10); if (*e) return EC_ERROR_PARAM3; - pd_ping_enable(port, enable); + pd_ping_enable( enable); } ccprintf("Pings %s\n", - (pd[port].flags & PD_FLAGS_PING_ENABLED) ? + (pd.flags & PD_FLAGS_PING_ENABLED) ? "on" : "off"); } else if (!strncasecmp(argv[2], "vdm", 3)) { if (argc < 4) @@ -3717,13 +3620,13 @@ static int command_pd(int argc, char **argv) enable = strtoi(argv[4], &e, 10); if (*e) return EC_ERROR_PARAM4; - pd_send_vdm(port, USB_VID_GOOGLE, VDO_CMD_PING_ENABLE, + pd_send_vdm( USB_VID_GOOGLE, VDO_CMD_PING_ENABLE, &enable, 1); } else if (!strncasecmp(argv[3], "curr", 4)) { - pd_send_vdm(port, USB_VID_GOOGLE, VDO_CMD_CURRENT, + pd_send_vdm( USB_VID_GOOGLE, VDO_CMD_CURRENT, NULL, 0); } else if (!strncasecmp(argv[3], "vers", 4)) { - pd_send_vdm(port, USB_VID_GOOGLE, VDO_CMD_VERSION, + pd_send_vdm( USB_VID_GOOGLE, VDO_CMD_VERSION, NULL, 0); } else { return EC_ERROR_PARAM_COUNT; @@ -3737,13 +3640,13 @@ static int command_pd(int argc, char **argv) if (!strncasecmp(argv[2], "state", 5)) { ccprintf("Port C%d CC%d, %s - Role: %s-%s%s " "State: %s, Flags: 0x%04x\n", - port, pd[port].polarity + 1, - pd_comm_is_enabled(port) ? "Ena" : "Dis", - pd[port].power_role == PD_ROLE_SOURCE ? "SRC" : "SNK", - pd[port].data_role == PD_ROLE_DFP ? "DFP" : "UFP", - (pd[port].flags & PD_FLAGS_VCONN_ON) ? "-VC" : "", - pd_state_names[pd[port].task_state], - pd[port].flags); + port, pd.polarity + 1, + pd_comm_is_enabled() ? "Ena" : "Dis", + pd.power_role == PD_ROLE_SOURCE ? "SRC" : "SNK", + pd.data_role == PD_ROLE_DFP ? "DFP" : "UFP", + (pd.flags & PD_FLAGS_VCONN_ON) ? "-VC" : "", + pd_state_names[pd.task_state], + pd.flags); } else { return EC_ERROR_PARAM1; } @@ -3894,12 +3797,12 @@ static int hc_remote_flash(struct host_cmd_handler_args *args) * Busy still with a VDM that host likely generated. 1 deep VDM queue * so just return for retry logic on host side to deal with. */ - if (pd[port].vdm_state > 0) + if (pd.vdm_state > 0) return EC_RES_BUSY; switch (p->cmd) { case USB_PD_FW_REBOOT: - pd_send_vdm(port, USB_VID_GOOGLE, VDO_CMD_REBOOT, NULL, 0); + pd_send_vdm( USB_VID_GOOGLE, VDO_CMD_REBOOT, NULL, 0); /* * Return immediately to free pending i2c bus. Host needs to @@ -3908,7 +3811,7 @@ static int hc_remote_flash(struct host_cmd_handler_args *args) return EC_RES_SUCCESS; case USB_PD_FW_FLASH_ERASE: - pd_send_vdm(port, USB_VID_GOOGLE, VDO_CMD_FLASH_ERASE, NULL, 0); + pd_send_vdm( USB_VID_GOOGLE, VDO_CMD_FLASH_ERASE, NULL, 0); /* * Return immediately. Host needs to manage delays here which @@ -3917,7 +3820,7 @@ static int hc_remote_flash(struct host_cmd_handler_args *args) return EC_RES_SUCCESS; case USB_PD_FW_ERASE_SIG: - pd_send_vdm(port, USB_VID_GOOGLE, VDO_CMD_ERASE_SIG, NULL, 0); + pd_send_vdm( USB_VID_GOOGLE, VDO_CMD_ERASE_SIG, NULL, 0); timeout.val = get_time().val + 500*MSEC; break; @@ -3928,16 +3831,16 @@ static int hc_remote_flash(struct host_cmd_handler_args *args) size = p->size / 4; for (i = 0; i < size; i += VDO_MAX_SIZE - 1) { - pd_send_vdm(port, USB_VID_GOOGLE, VDO_CMD_FLASH_WRITE, + pd_send_vdm( USB_VID_GOOGLE, VDO_CMD_FLASH_WRITE, data + i, MIN(size - i, VDO_MAX_SIZE - 1)); timeout.val = get_time().val + 500*MSEC; /* Wait until VDM is done */ - while ((pd[port].vdm_state > 0) && + while ((pd.vdm_state > 0) && (get_time().val < timeout.val)) task_wait_event(10*MSEC); - if (pd[port].vdm_state > 0) + if (pd.vdm_state > 0) return EC_RES_TIMEOUT; } return EC_RES_SUCCESS; @@ -3948,13 +3851,13 @@ static int hc_remote_flash(struct host_cmd_handler_args *args) } /* Wait until VDM is done or timeout */ - while ((pd[port].vdm_state > 0) && (get_time().val < timeout.val)) + while ((pd.vdm_state > 0) && (get_time().val < timeout.val)) task_wait_event(50*MSEC); - if ((pd[port].vdm_state > 0) || - (pd[port].vdm_state == VDM_STATE_ERR_TMOUT)) + if ((pd.vdm_state > 0) || + (pd.vdm_state == VDM_STATE_ERR_TMOUT)) rv = EC_RES_TIMEOUT; - else if (pd[port].vdm_state < 0) + else if (pd.vdm_state < 0) rv = EC_RES_ERROR; return rv; diff --git a/workspace/TS100/Core/Drivers/FUSB302/USBC_TCPM/tcpm.h b/workspace/TS100/Core/Drivers/FUSB302/USBC_TCPM/tcpm.h index 982dcf37..e44504fb 100644 --- a/workspace/TS100/Core/Drivers/FUSB302/USBC_TCPM/tcpm.h +++ b/workspace/TS100/Core/Drivers/FUSB302/USBC_TCPM/tcpm.h @@ -18,110 +18,93 @@ #endif #ifndef CONFIG_USB_PD_TCPC -extern const struct tcpc_config_t tcpc_config[]; +extern const struct tcpc_config_t tcpc_config; /* I2C wrapper functions - get I2C port / slave addr from config struct. */ -int tcpc_write(int port, int reg, int val); -int tcpc_write16(int port, int reg, int val); -int tcpc_read(int port, int reg, int *val); -int tcpc_read16(int port, int reg, int *val); -int tcpc_xfer(int port, - const uint8_t *out, int out_size, - uint8_t *in, int in_size, +int tcpc_write(int reg, int val); +int tcpc_write16(int reg, int val); +int tcpc_read(int reg, int *val); +int tcpc_read16(int reg, int *val); +int tcpc_xfer(const uint8_t *out, int out_size, uint8_t *in, int in_size, int flags); /* TCPM driver wrapper function */ -static inline int tcpm_init(int port) -{ +static inline int tcpm_init() { int rv; - rv = tcpc_config[port].drv->init(port); + rv = tcpc_config.drv->init(); if (rv) return rv; /* Board specific post TCPC init */ if (board_tcpc_post_init) - rv = board_tcpc_post_init(port); + rv = board_tcpc_post_init(); return rv; } -static inline int tcpm_release(int port) -{ - return tcpc_config[port].drv->release(port); +static inline int tcpm_release() { + return tcpc_config.drv->release(); } -static inline int tcpm_get_cc(int port, int *cc1, int *cc2) -{ - return tcpc_config[port].drv->get_cc(port, cc1, cc2); +static inline int tcpm_get_cc(int *cc1, int *cc2) { + return tcpc_config.drv->get_cc(cc1, cc2); } -static inline int tcpm_get_vbus_level(int port) -{ - return tcpc_config[port].drv->get_vbus_level(port); +static inline int tcpm_get_vbus_level() { + return tcpc_config.drv->get_vbus_level(); } -static inline int tcpm_select_rp_value(int port, int rp) -{ - return tcpc_config[port].drv->select_rp_value(port, rp); +static inline int tcpm_select_rp_value(int rp) { + return tcpc_config.drv->select_rp_value(rp); } -static inline int tcpm_set_cc(int port, int pull) -{ - return tcpc_config[port].drv->set_cc(port, pull); +static inline int tcpm_set_cc(int pull) { + return tcpc_config.drv->set_cc(pull); } -static inline int tcpm_set_polarity(int port, int polarity) -{ - return tcpc_config[port].drv->set_polarity(port, polarity); +static inline int tcpm_set_polarity(int polarity) { + return tcpc_config.drv->set_polarity(polarity); } -static inline int tcpm_set_vconn(int port, int enable) -{ - return tcpc_config[port].drv->set_vconn(port, enable); +static inline int tcpm_set_vconn(int enable) { + return tcpc_config.drv->set_vconn(enable); } -static inline int tcpm_set_msg_header(int port, int power_role, int data_role) -{ - return tcpc_config[port].drv->set_msg_header(port, power_role, - data_role); +static inline int tcpm_set_msg_header(int power_role, int data_role) { + return tcpc_config.drv->set_msg_header(power_role, data_role); } -static inline int tcpm_set_rx_enable(int port, int enable) -{ - return tcpc_config[port].drv->set_rx_enable(port, enable); +static inline int tcpm_set_rx_enable(int enable) { + return tcpc_config.drv->set_rx_enable(enable); } -static inline int tcpm_get_message(int port, uint32_t *payload, int *head) -{ - return tcpc_config[port].drv->get_message(port, payload, head); +static inline int tcpm_get_message(uint32_t *payload, int *head) { + return tcpc_config.drv->get_message(payload, head); } -static inline int tcpm_transmit(int port, enum tcpm_transmit_type type, - uint16_t header, const uint32_t *data) -{ - return tcpc_config[port].drv->transmit(port, type, header, data); +static inline int tcpm_transmit(enum tcpm_transmit_type type, uint16_t header, + const uint32_t *data) { + return tcpc_config.drv->transmit(type, header, data); } -static inline void tcpc_alert(int port) -{ - tcpc_config[port].drv->tcpc_alert(port); +static inline void tcpc_alert() { + tcpc_config.drv->tcpc_alert(); } -static inline void tcpc_discharge_vbus(int port, int enable) -{ - tcpc_config[port].drv->tcpc_discharge_vbus(port, enable); +static inline void tcpc_discharge_vbus(int enable) { + tcpc_config.drv->tcpc_discharge_vbus(enable); } #ifdef CONFIG_USB_PD_DUAL_ROLE_AUTO_TOGGLE -static inline int tcpm_auto_toggle_supported(int port) +static inline int tcpm_auto_toggle_supported() { - return !!tcpc_config[port].drv->drp_toggle; + return !!tcpc_config.drv->drp_toggle; } -static inline int tcpm_set_drp_toggle(int port, int enable) +static inline int tcpm_set_drp_toggle( int enable) { - return tcpc_config[port].drv->drp_toggle(port, enable); + return tcpc_config.drv->drp_toggle( enable); } #endif @@ -129,21 +112,20 @@ static inline int tcpm_set_drp_toggle(int port, int enable) static inline int tcpc_i2c_read(const int port, const int addr, const int reg, int *data) { - return tcpc_read(port, reg, data); + return tcpc_read( reg, data); } static inline int tcpc_i2c_write(const int port, const int addr, const int reg, int data) { - return tcpc_write(port, reg, data); + return tcpc_write( reg, data); } #endif -static inline int tcpm_get_chip_info(int port, int renew, - struct ec_response_pd_chip_info **info) -{ - if (tcpc_config[port].drv->get_chip_info) - return tcpc_config[port].drv->get_chip_info(port, renew, info); +static inline int tcpm_get_chip_info(int renew, + struct ec_response_pd_chip_info **info) { + if (tcpc_config.drv->get_chip_info) + return tcpc_config.drv->get_chip_info(renew, info); return EC_ERROR_UNIMPLEMENTED; } @@ -156,7 +138,7 @@ static inline int tcpm_get_chip_info(int port, int renew, * * @return EC_SUCCESS or error */ -int tcpm_init(int port); +int tcpm_init(); /** * Read the CC line status. @@ -167,7 +149,7 @@ int tcpm_init(int port); * * @return EC_SUCCESS or error */ -int tcpm_get_cc(int port, int *cc1, int *cc2); +int tcpm_get_cc( int *cc1, int *cc2); /** * Read VBUS @@ -176,7 +158,7 @@ int tcpm_get_cc(int port, int *cc1, int *cc2); * * @return 0 => VBUS not detected, 1 => VBUS detected */ -int tcpm_get_vbus_level(int port); +int tcpm_get_vbus_level(); /** * Set the value of the CC pull-up used when we are a source. @@ -186,7 +168,7 @@ int tcpm_get_vbus_level(int port); * * @return EC_SUCCESS or error */ -int tcpm_select_rp_value(int port, int rp); +int tcpm_select_rp_value( int rp); /** * Set the CC pull resistor. This sets our role as either source or sink. @@ -196,7 +178,7 @@ int tcpm_select_rp_value(int port, int rp); * * @return EC_SUCCESS or error */ -int tcpm_set_cc(int port, int pull); +int tcpm_set_cc( int pull); /** * Set polarity @@ -206,7 +188,7 @@ int tcpm_set_cc(int port, int pull); * * @return EC_SUCCESS or error */ -int tcpm_set_polarity(int port, int polarity); +int tcpm_set_polarity( int polarity); /** * Set Vconn. @@ -216,7 +198,7 @@ int tcpm_set_polarity(int port, int polarity); * * @return EC_SUCCESS or error */ -int tcpm_set_vconn(int port, int enable); +int tcpm_set_vconn( int enable); /** * Set PD message header to use for goodCRC @@ -227,7 +209,7 @@ int tcpm_set_vconn(int port, int enable); * * @return EC_SUCCESS or error */ -int tcpm_set_msg_header(int port, int power_role, int data_role); +int tcpm_set_msg_header( int power_role, int data_role); /** * Set RX enable flag @@ -237,7 +219,7 @@ int tcpm_set_msg_header(int port, int power_role, int data_role); * * @return EC_SUCCESS or error */ -int tcpm_set_rx_enable(int port, int enable); +int tcpm_set_rx_enable( int enable); /** * Read last received PD message. @@ -248,7 +230,7 @@ int tcpm_set_rx_enable(int port, int enable); * * @return EC_SUCCESS or error */ -int tcpm_get_message(int port, uint32_t *payload, int *head); +int tcpm_get_message( uint32_t *payload, int *head); /** * Transmit PD message @@ -261,7 +243,7 @@ int tcpm_get_message(int port, uint32_t *payload, int *head); * * @return EC_SUCCESS or error */ -int tcpm_transmit(int port, enum tcpm_transmit_type type, uint16_t header, +int tcpm_transmit( enum tcpm_transmit_type type, uint16_t header, const uint32_t *data); /** @@ -269,7 +251,7 @@ int tcpm_transmit(int port, enum tcpm_transmit_type type, uint16_t header, * * @param port Type-C port number */ -void tcpc_alert(int port); +void tcpc_alert(); #endif diff --git a/workspace/TS100/Core/Drivers/FUSB302/USBC_TCPM/usb_pd_tcpm.h b/workspace/TS100/Core/Drivers/FUSB302/USBC_TCPM/usb_pd_tcpm.h index 947a88c9..86a4fbb3 100644 --- a/workspace/TS100/Core/Drivers/FUSB302/USBC_TCPM/usb_pd_tcpm.h +++ b/workspace/TS100/Core/Drivers/FUSB302/USBC_TCPM/usb_pd_tcpm.h @@ -71,7 +71,7 @@ enum ec_error_list { /* Module-internal error codes may use this range. */ EC_ERROR_INTERNAL_FIRST = 0x10000, - EC_ERROR_INTERNAL_LAST = 0x1FFFF + EC_ERROR_INTERNAL_LAST = 0x1FFFF }; /* Flags for i2c_xfer() */ @@ -95,17 +95,11 @@ enum tcpc_cc_voltage_status { }; enum tcpc_cc_pull { - TYPEC_CC_RA = 0, - TYPEC_CC_RP = 1, - TYPEC_CC_RD = 2, - TYPEC_CC_OPEN = 3, + TYPEC_CC_RA = 0, TYPEC_CC_RP = 1, TYPEC_CC_RD = 2, TYPEC_CC_OPEN = 3, }; enum tcpc_rp_value { - TYPEC_RP_USB = 0, - TYPEC_RP_1A5 = 1, - TYPEC_RP_3A0 = 2, - TYPEC_RP_RESERVED = 3, + TYPEC_RP_USB = 0, TYPEC_RP_1A5 = 1, TYPEC_RP_3A0 = 2, TYPEC_RP_RESERVED = 3, }; enum tcpm_transmit_type { @@ -120,9 +114,9 @@ enum tcpm_transmit_type { }; enum tcpc_transmit_complete { - TCPC_TX_COMPLETE_SUCCESS = 0, + TCPC_TX_COMPLETE_SUCCESS = 0, TCPC_TX_COMPLETE_DISCARDED = 1, - TCPC_TX_COMPLETE_FAILED = 2, + TCPC_TX_COMPLETE_FAILED = 2, }; struct tcpm_drv { @@ -133,7 +127,7 @@ struct tcpm_drv { * * @return EC_SUCCESS or error */ - int (*init)(int port); + int (*init)(); /** * Release the TCPM hardware and disconnect the driver. @@ -143,7 +137,7 @@ struct tcpm_drv { * * @return EC_SUCCESS or error */ - int (*release)(int port); + int (*release)(); /** * Read the CC line status. @@ -154,7 +148,7 @@ struct tcpm_drv { * * @return EC_SUCCESS or error */ - int (*get_cc)(int port, int *cc1, int *cc2); + int (*get_cc)(int *cc1, int *cc2); /** * Read VBUS @@ -163,7 +157,7 @@ struct tcpm_drv { * * @return 0 => VBUS not detected, 1 => VBUS detected */ - int (*get_vbus_level)(int port); + int (*get_vbus_level)(); /** * Set the value of the CC pull-up used when we are a source. @@ -173,7 +167,7 @@ struct tcpm_drv { * * @return EC_SUCCESS or error */ - int (*select_rp_value)(int port, int rp); + int (*select_rp_value)(int rp); /** * Set the CC pull resistor. This sets our role as either source or sink. @@ -183,7 +177,7 @@ struct tcpm_drv { * * @return EC_SUCCESS or error */ - int (*set_cc)(int port, int pull); + int (*set_cc)(int pull); /** * Set polarity @@ -193,7 +187,7 @@ struct tcpm_drv { * * @return EC_SUCCESS or error */ - int (*set_polarity)(int port, int polarity); + int (*set_polarity)(int polarity); /** * Set Vconn. @@ -203,7 +197,7 @@ struct tcpm_drv { * * @return EC_SUCCESS or error */ - int (*set_vconn)(int port, int enable); + int (*set_vconn)(int enable); /** * Set PD message header to use for goodCRC @@ -214,7 +208,7 @@ struct tcpm_drv { * * @return EC_SUCCESS or error */ - int (*set_msg_header)(int port, int power_role, int data_role); + int (*set_msg_header)(int power_role, int data_role); /** * Set RX enable flag @@ -224,7 +218,7 @@ struct tcpm_drv { * * @return EC_SUCCESS or error */ - int (*set_rx_enable)(int port, int enable); + int (*set_rx_enable)(int enable); /** * Read last received PD message. @@ -235,7 +229,7 @@ struct tcpm_drv { * * @return EC_SUCCESS or error */ - int (*get_message)(int port, uint32_t *payload, int *head); + int (*get_message)(uint32_t *payload, int *head); /** * Transmit PD message @@ -248,15 +242,15 @@ struct tcpm_drv { * * @return EC_SUCCESS or error */ - int (*transmit)(int port, enum tcpm_transmit_type type, uint16_t header, - const uint32_t *data); + int (*transmit)(enum tcpm_transmit_type type, uint16_t header, + const uint32_t *data); /** * TCPC is asserting alert * * @param port Type-C port number */ - void (*tcpc_alert)(int port); + void (*tcpc_alert)(); /** * Discharge PD VBUS on src/sink disconnect & power role swap @@ -264,7 +258,7 @@ struct tcpm_drv { * @param port Type-C port number * @param enable Discharge enable or disable */ - void (*tcpc_discharge_vbus)(int port, int enable); + void (*tcpc_discharge_vbus)(int enable); #ifdef CONFIG_USB_PD_DUAL_ROLE_AUTO_TOGGLE /** @@ -275,7 +269,7 @@ struct tcpm_drv { * * @return EC_SUCCESS or error */ - int (*drp_toggle)(int port, int enable); + int (*drp_toggle)( int enable); #endif /** @@ -287,20 +281,16 @@ struct tcpm_drv { * * @return EC_SUCCESS or error */ - int (*get_chip_info)(int port, int renew, - struct ec_response_pd_chip_info **info); + int (*get_chip_info)(int renew, struct ec_response_pd_chip_info **info); }; enum tcpc_alert_polarity { - TCPC_ALERT_ACTIVE_LOW, - TCPC_ALERT_ACTIVE_HIGH, + TCPC_ALERT_ACTIVE_LOW, TCPC_ALERT_ACTIVE_HIGH, }; struct tcpc_config_t { - int i2c_host_port; int i2c_slave_addr; const struct tcpm_drv *drv; - enum tcpc_alert_polarity pol; }; /** @@ -317,21 +307,21 @@ uint16_t tcpc_get_alert_status(void); * @param port Type-C port number * @param mode 0: off/sleep, 1: on/awake */ -void board_set_tcpc_power_mode(int port, int mode) __attribute__((weak)); +void board_set_tcpc_power_mode(int mode) __attribute__((weak)); /** * Initialize TCPC. * * @param port Type-C port number */ -void tcpc_init(int port); +void tcpc_init(); /** * TCPC is asserting alert * * @param port Type-C port number */ -void tcpc_alert_clear(int port); +void tcpc_alert_clear(); /** * Run TCPC task once. This checks for incoming messages, processes @@ -340,7 +330,7 @@ void tcpc_alert_clear(int port); * @param port Type-C port number * @param evt Event type that woke up this task */ -int tcpc_run(int port, int evt); +int tcpc_run(int evt); /** * Initialize board specific TCPC functions post TCPC initialization. @@ -349,6 +339,6 @@ int tcpc_run(int port, int evt); * * @return EC_SUCCESS or error */ -int board_tcpc_post_init(int port) __attribute__((weak)); +int board_tcpc_post_init() __attribute__((weak)); #endif /* __CROS_EC_USB_PD_TCPM_H */ diff --git a/workspace/TS100/Core/Drivers/FUSB302/tcpm_driver.cpp b/workspace/TS100/Core/Drivers/FUSB302/tcpm_driver.cpp index d133ebb4..bf8b60fb 100644 --- a/workspace/TS100/Core/Drivers/FUSB302/tcpm_driver.cpp +++ b/workspace/TS100/Core/Drivers/FUSB302/tcpm_driver.cpp @@ -8,36 +8,36 @@ #include "tcpm_driver.h" #include "I2C_Wrapper.hpp" #include "I2CBB.hpp" -extern const struct tcpc_config_t tcpc_config[CONFIG_USB_PD_PORT_COUNT]; +extern const struct tcpc_config_t tcpc_config; #define STATUS_OK 0 /* I2C wrapper functions - get I2C port / slave addr from config struct. */ -int tcpc_write(int port, int reg, int val) { +int tcpc_write(int reg, int val) { - I2CBB::Mem_Write(tcpc_config[port].i2c_slave_addr, reg, (uint8_t*) &val, 1); + I2CBB::Mem_Write(tcpc_config.i2c_slave_addr, reg, (uint8_t*) &val, 1); return STATUS_OK; } -int tcpc_write16(int port, int reg, int val) { +int tcpc_write16(int reg, int val) { uint8_t data[2]; data[0] = (0xFF) & val; data[1] = (0xFF) & (val >> 8); - I2CBB::Mem_Write(tcpc_config[port].i2c_slave_addr, reg, (uint8_t*) data, 2); + I2CBB::Mem_Write(tcpc_config.i2c_slave_addr, reg, (uint8_t*) data, 2); return STATUS_OK; } -int tcpc_read(int port, int reg, int *val) { +int tcpc_read(int reg, int *val) { uint8_t data[1]; - I2CBB::Mem_Read(tcpc_config[port].i2c_slave_addr, reg, (uint8_t*) data, 1); + I2CBB::Mem_Read(tcpc_config.i2c_slave_addr, reg, (uint8_t*) data, 1); *val = data[0]; return STATUS_OK; } -int tcpc_read16(int port, int reg, int *val) { +int tcpc_read16(int reg, int *val) { uint8_t data[2]; - I2CBB::Mem_Write(tcpc_config[port].i2c_slave_addr, reg, (uint8_t*) data, 2); + I2CBB::Mem_Write(tcpc_config.i2c_slave_addr, reg, (uint8_t*) data, 2); *val = data[0]; *val |= (data[1] << 8); @@ -45,8 +45,8 @@ int tcpc_read16(int port, int reg, int *val) { return STATUS_OK; } -int tcpc_xfer(int port, const uint8_t *out, int out_size, uint8_t *in, - int in_size, int flags) { +int tcpc_xfer(const uint8_t *out, int out_size, uint8_t *in, int in_size, + int flags) { // Write out the I2C port to the given slave address // Write out the out byte array to the device (sending a stop if the flag is set) // Then issue a read from the device @@ -54,12 +54,12 @@ int tcpc_xfer(int port, const uint8_t *out, int out_size, uint8_t *in, if (flags & I2C_XFER_STOP) { //Issuing a stop between the requests //Send as a Tx followed by a Rx - I2CBB::Transmit(tcpc_config[port].i2c_slave_addr, (uint8_t*) out, + I2CBB::Transmit(tcpc_config.i2c_slave_addr, (uint8_t*) out, out_size); - I2CBB::Receive(tcpc_config[port].i2c_slave_addr, in, in_size); + I2CBB::Receive(tcpc_config.i2c_slave_addr, in, in_size); } else { //issue as a continious transmit & recieve - I2CBB::TransmitReceive(tcpc_config[port].i2c_slave_addr, (uint8_t*) out, + I2CBB::TransmitReceive(tcpc_config.i2c_slave_addr, (uint8_t*) out, out_size, in, in_size); } diff --git a/workspace/TS100/Core/Drivers/FUSB302/usb_pd_driver.cpp b/workspace/TS100/Core/Drivers/FUSB302/usb_pd_driver.cpp index 772bc52a..7036633d 100644 --- a/workspace/TS100/Core/Drivers/FUSB302/usb_pd_driver.cpp +++ b/workspace/TS100/Core/Drivers/FUSB302/usb_pd_driver.cpp @@ -26,15 +26,15 @@ uint32_t pd_task_set_event(uint32_t event, int wait_for_reply) { } const uint32_t pd_src_pdo[] = { PDO_FIXED(5000, 1500, PDO_FIXED_FLAGS), }; + const int pd_src_pdo_cnt = ARRAY_SIZE(pd_src_pdo); -const uint32_t pd_snk_pdo[] = { PDO_FIXED(5000, 500, PDO_FIXED_FLAGS), - PDO_FIXED(9000, 500, PDO_FIXED_FLAGS), PDO_FIXED(20000, 500, +const uint32_t pd_snk_pdo[] = { PDO_FIXED(5000, 1000, PDO_FIXED_FLAGS), + PDO_FIXED(9000, 1500, PDO_FIXED_FLAGS), PDO_FIXED(12000, 3500, PDO_FIXED_FLAGS), }; const int pd_snk_pdo_cnt = ARRAY_SIZE(pd_snk_pdo); -void pd_set_input_current_limit(int port, uint32_t max_ma, - uint32_t supply_voltage) { +void pd_set_input_current_limit(uint32_t max_ma, uint32_t supply_voltage) { } @@ -42,7 +42,7 @@ int pd_is_valid_input_voltage(int mv) { return 1; } -int pd_snk_is_vbus_provided(int port) { +int pd_snk_is_vbus_provided() { return 1; } @@ -58,20 +58,20 @@ timestamp_t get_time(void) { return t; } -void pd_power_supply_reset(int port) { +void pd_power_supply_reset() { return; } -void pd_execute_data_swap(int port, int data_role) { +void pd_execute_data_swap(int data_role) { /* Do nothing */ } -int pd_check_data_swap(int port, int data_role) { +int pd_check_data_swap(int data_role) { // Never allow data swap return 0; } -int pd_check_power_swap(int port) { +int pd_check_power_swap() { /* Always refuse power swap */ return 0; } @@ -80,17 +80,8 @@ int pd_board_checks(void) { return EC_SUCCESS; } -int pd_set_power_supply_ready(int port) { -#if 0 - /* Disable charging */ - gpio_set_level(GPIO_USB_C0_CHARGE_L, 1); - - /* Enable VBUS source */ - gpio_set_level(GPIO_USB_C0_5V_EN, 1); - - /* notify host of power info change */ - pd_send_host_event(PD_EVENT_POWER_CHANGE); -#endif // if 0 +int pd_set_power_supply_ready() { +//Tells other device if we can supply power return EC_SUCCESS; /* we are ready */ } @@ -132,7 +123,7 @@ void pd_transition_voltage(int idx) { } -void pd_check_dr_role(int port, int dr_role, int flags) { +void pd_check_dr_role(int dr_role, int flags) { #if 0 /* If UFP, try to switch to DFP */ if ((flags & PD_FLAGS_PARTNER_DR_DATA) && dr_role == PD_ROLE_UFP) @@ -140,7 +131,7 @@ void pd_check_dr_role(int port, int dr_role, int flags) { #endif } -void pd_check_pr_role(int port, int pr_role, int flags) { +void pd_check_pr_role(int pr_role, int flags) { #if 0 /* * If partner is dual-role power and dualrole toggling is on, consider @@ -162,40 +153,28 @@ void pd_check_pr_role(int port, int pr_role, int flags) { #endif // if 0 } -void pd_process_source_cap_callback(int port, int cnt, uint32_t *src_caps) { -// char str[256]; -// int i; -// uint32_t ma, mv, pdo; -// uint8_t old_display; -// -// old_display = display_screen; -// display_screen = SCREEN_POWER; -// memset(display_buffer[SCREEN_POWER], 0x00, DISP_MEM_SIZE); -// -// sprintf(str, "Has Power Delivery"); -// UG_PutString(0, 8, str); -// -// for (i = 0; i < cnt; i++) -// { -// pd_extract_pdo_power(src_caps[i], &ma, &mv); -// sprintf(str, "%5.2f V, %5.2f A", (float)mv/1000, (float)ma/1000); -// UG_PutString(0, 8*(i+2), str); -// } -// -// display_screen = old_display; -// display_needs_update = 1; +void pd_process_source_cap_callback(int cnt, uint32_t *src_caps) { + char str[256]; + int i; + uint32_t ma, mv, pdo; + + for (i = 0; i < cnt; i++) { + pd_extract_pdo_power(src_caps[i], &ma, &mv); + //Charger can supply power at mv & mA + //TODO we want to ask for the charger to select the closest to our ideal (12V) + //And fall back to 9V + } //TODO Handle information on supported voltages? } /* ----------------- Vendor Defined Messages ------------------ */ /* Holds valid object position (opos) for entered mode */ -static int alt_mode[PD_AMODE_COUNT]; const uint32_t vdo_idh = VDO_IDH(0, /* data caps as USB host */ 1, /* data caps as USB device */ -IDH_PTYPE_AMA, /* Alternate mode */ -1, /* supports alt modes */ +IDH_PTYPE_PERIPH, /* Alternate mode */ +0, /* Does not support alt modes */ USB_VID_GOOGLE); const uint32_t vdo_product = VDO_PRODUCT(CONFIG_USB_PID, CONFIG_USB_BCD_DEV); @@ -205,21 +184,19 @@ const uint32_t vdo_ama = VDO_AMA(CONFIG_USB_PD_IDENTITY_HW_VERS, 0, /* Vconn power */ 0, /* Vconn power required */ 1, /* Vbus power required */ - AMA_USBSS_BBONLY /* USB SS support */); + AMA_USBSS_U2_ONLY /* USB 2.0 support */); -static int svdm_response_identity(int port, uint32_t *payload) { +static int svdm_response_identity(uint32_t *payload) { payload[VDO_I(IDH)] = vdo_idh; - /* TODO(tbroch): Do we plan to obtain TID (test ID) */ payload[VDO_I(CSTAT)] = VDO_CSTAT(0); payload[VDO_I(PRODUCT)] = vdo_product; payload[VDO_I(AMA)] = vdo_ama; return VDO_I(AMA) + 1; } - -static int svdm_response_svids(int port, uint32_t *payload) { - payload[1] = VDO_SVID(USB_SID_DISPLAYPORT, USB_VID_GOOGLE); - payload[2] = 0; - return 3; +//No custom svdm +static int svdm_response_svids(uint32_t *payload) { + payload[1] = 0; + return 2; } #define OPOS_DP 1 @@ -233,21 +210,11 @@ MODE_DP_V13, /* DPv1.3 Support, no Gen2 */ MODE_DP_SNK) /* Its a sink only */ }; -const uint32_t vdo_goog_modes[1] = { VDO_MODE_GOOGLE(MODE_GOOGLE_FU) }; - -static int svdm_response_modes(int port, uint32_t *payload) { - if (PD_VDO_VID(payload[0]) == USB_SID_DISPLAYPORT) { - memcpy(payload + 1, vdo_dp_modes, sizeof(vdo_dp_modes)); - return ARRAY_SIZE(vdo_dp_modes) + 1; - } else if (PD_VDO_VID(payload[0]) == USB_VID_GOOGLE) { - memcpy(payload + 1, vdo_goog_modes, sizeof(vdo_goog_modes)); - return ARRAY_SIZE(vdo_goog_modes) + 1; - } else { - return 0; /* nak */ - } +static int svdm_response_modes(uint32_t *payload) { + return 0; /* nak */ } -static int dp_status(int port, uint32_t *payload) { +static int dp_status(uint32_t *payload) { int opos = PD_VDO_OPOS(payload[0]); int hpd = 0; // gpio_get_level(GPIO_DP_HPD); if (opos != OPOS_DP) @@ -264,57 +231,22 @@ static int dp_status(int port, uint32_t *payload) { return 2; } -static int dp_config(int port, uint32_t *payload) { - if (PD_DP_CFG_DPON(payload[1])) - 0; //gpio_set_level(GPIO_PD_SBU_ENABLE, 1); +static int dp_config(uint32_t *payload) { return 1; } -static int svdm_enter_mode(int port, uint32_t *payload) { +static int svdm_enter_mode(uint32_t *payload) { int rv = 0; /* will generate a NAK */ - char str[256]; - uint8_t old_display; - /* SID & mode request is valid */ - if ((PD_VDO_VID(payload[0]) == USB_SID_DISPLAYPORT) - && (PD_VDO_OPOS(payload[0]) == OPOS_DP)) { - alt_mode[PD_AMODE_DISPLAYPORT] = OPOS_DP; - rv = 1; - //pd_log_event(PD_EVENT_VIDEO_DP_MODE, 0, 1, NULL); - } else if ((PD_VDO_VID(payload[0]) == USB_VID_GOOGLE) - && (PD_VDO_OPOS(payload[0]) == OPOS_GFU)) { - alt_mode[PD_AMODE_GOOGLE] = OPOS_GFU; - rv = 1; - } - -// if (rv) - /* - * If we failed initial mode entry we'll have enumerated the USB - * Billboard class. If so we should disconnect. - */ - //usb_disconnect(); -// old_display = display_screen; -// display_screen = SCREEN_ALTMODE; -// memset(display_buffer[SCREEN_ALTMODE], 0x00, DISP_MEM_SIZE); -// -// sprintf(str, "Requested Alt Mode"); -// UG_PutString(0, 8, str); -// -// display_screen = old_display; -// display_needs_update = 1; -//TODO handle alt mode ? return rv; } -int pd_alt_mode(int port, uint16_t svid) { - if (svid == USB_SID_DISPLAYPORT) - return alt_mode[PD_AMODE_DISPLAYPORT]; - else if (svid == USB_VID_GOOGLE) - return alt_mode[PD_AMODE_GOOGLE]; +int pd_alt_mode(uint16_t svid) { + return 0; } -static int svdm_exit_mode(int port, uint32_t *payload) { +static int svdm_exit_mode(uint32_t *payload) { return 1; /* Must return ACK */ } @@ -324,91 +256,3 @@ const struct svdm_response svdm_rsp = { &svdm_response_identity, &svdm_response_svids, &svdm_response_modes, &svdm_enter_mode, &svdm_exit_mode, &dp_fx, }; -int pd_custom_vdm(int port, int cnt, uint32_t *payload, uint32_t **rpayload) { - int rsize; - - if (PD_VDO_VID(payload[0]) != USB_VID_GOOGLE || !alt_mode[PD_AMODE_GOOGLE]) - return 0; - - *rpayload = payload; - - rsize = pd_custom_flash_vdm(port, cnt, payload); - if (!rsize) { - int cmd = PD_VDO_CMD(payload[0]); - switch (cmd) { - case VDO_CMD_GET_LOG: - rsize = pd_vdm_get_log_entry(payload); - break; - default: - /* Unknown : do not answer */ - return 0; - } - } - - /* respond (positively) to the request */ - payload[0] |= VDO_SRC_RESPONDER; - - return rsize; -} - -int pd_custom_flash_vdm(int port, int cnt, uint32_t *payload) { - static int flash_offset; - int rsize = 1; /* default is just VDM header returned */ - - switch (PD_VDO_CMD(payload[0])) { -#if 0 - case VDO_CMD_VERSION: - memcpy(payload + 1, ¤t_image_data.version, 24); - rsize = 7; - break; - case VDO_CMD_REBOOT: - /* ensure the power supply is in a safe state */ - pd_power_supply_reset(0); - system_reset(0); - break; - case VDO_CMD_READ_INFO: - /* copy info into response */ - pd_get_info(payload + 1); - rsize = 7; - break; - case VDO_CMD_FLASH_ERASE: - /* do not kill the code under our feet */ - if (system_get_image_copy() != SYSTEM_IMAGE_RO) - break; - pd_log_event(PD_EVENT_ACC_RW_ERASE, 0, 0, NULL); - flash_offset = CONFIG_EC_WRITABLE_STORAGE_OFF + - CONFIG_RW_STORAGE_OFF; - flash_physical_erase(CONFIG_EC_WRITABLE_STORAGE_OFF + - CONFIG_RW_STORAGE_OFF, CONFIG_RW_SIZE); - rw_flash_changed = 1; - break; - case VDO_CMD_FLASH_WRITE: - /* do not kill the code under our feet */ - if ((system_get_image_copy() != SYSTEM_IMAGE_RO) || - (flash_offset < CONFIG_EC_WRITABLE_STORAGE_OFF + - CONFIG_RW_STORAGE_OFF)) - break; - flash_physical_write(flash_offset, 4*(cnt - 1), - (const char *)(payload+1)); - flash_offset += 4*(cnt - 1); - rw_flash_changed = 1; - break; - case VDO_CMD_ERASE_SIG: - /* this is not touching the code area */ - { - uint32_t zero = 0; - int offset; - /* zeroes the area containing the RSA signature */ - for (offset = FW_RW_END - RSANUMBYTES; - offset < FW_RW_END; offset += 4) - flash_physical_write(offset, 4, - (const char *)&zero); - } - break; -#endif // 0 - default: - /* Unknown : do not answer */ - return 0; - } - return rsize; -} diff --git a/workspace/TS100/Core/Drivers/FUSB302/usb_pd_driver.h b/workspace/TS100/Core/Drivers/FUSB302/usb_pd_driver.h index 1a560b1a..18100253 100644 --- a/workspace/TS100/Core/Drivers/FUSB302/usb_pd_driver.h +++ b/workspace/TS100/Core/Drivers/FUSB302/usb_pd_driver.h @@ -17,9 +17,9 @@ #define CONFIG_CHARGE_MANAGER //#define CONFIG_USBC_BACKWARDS_COMPATIBLE_DFP //#define CONFIG_USBC_VCONN_SWAP -#define CONFIG_USB_PD_ALT_MODE +//#define CONFIG_USB_PD_ALT_MODE //#define CONFIG_USB_PD_CHROMEOS -#define CONFIG_USB_PD_DUAL_ROLE +//#define CONFIG_USB_PD_DUAL_ROLE //#define CONFIG_USB_PD_GIVE_BACK //#define CONFIG_USB_PD_SIMPLE_DFP //#define CONFIG_USB_PD_TCPM_TCPCI diff --git a/workspace/TS100/Core/Src/gui.cpp b/workspace/TS100/Core/Src/gui.cpp index 2fe91a24..16730c9e 100644 --- a/workspace/TS100/Core/Src/gui.cpp +++ b/workspace/TS100/Core/Src/gui.cpp @@ -353,8 +353,7 @@ static void settings_setInputPRange(void) { static void settings_displayInputPRange(void) { printShortDescription(0, 5); //0 = 9V, 1=12V (Fixed Voltages, these imply 1.5A limits) - //2 = 18W, 2=24W (Auto Adjusting V, estimated from the tip resistance???) # TODO - // Need to come back and look at these ^ as there were issues with voltage hunting + /// TODO TS80P switch (systemSettings.cutoutSetting) { case 0: OLED::printNumber(9, 2);