Skip to content

Commit 3307d16

Browse files
committed
fix response and timeout in pd wait_for_ack
Wait for ack was not correctly checking response code. Wait for ack was not checking if interrupt bit Fix wait code to use a timestamp instead of for loop Signed-off-by: Kieran Levin <ktl@frame.work>
1 parent 8567dce commit 3307d16

File tree

2 files changed

+46
-14
lines changed

2 files changed

+46
-14
lines changed

zephyr/program/lotus/include/cypress_pd_common.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@
9999
(0x1032 + ((x) * 0x1000))
100100
#define CCG_PORT_INTR_STATUS_REG(x) \
101101
(0x1034 + ((x) * 0x1000))
102+
#define CCG_PORT_CURRENT_REG(x) \
103+
(0x1058 + ((x) * 0x1000))
102104
#define CCG_PORT_HOST_CAP_REG(x) \
103105
(0x105C + ((x) * 0x1000))
104106
#define CCG_ALT_MODE_MASK_REG(x) \

zephyr/program/lotus/src/cypress_pd_common.c

Lines changed: 44 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -199,15 +199,17 @@ int cypd_clear_int(int controller, int mask)
199199

200200
int cypd_wait_for_ack(int controller, int timeout_ms)
201201
{
202-
int timeout;
203202
const struct gpio_dt_spec *intr = gpio_get_dt_spec(pd_chip_config[controller].gpio);
203+
timestamp_t start = get_time();
204204

205205
/* wait for interrupt ack to be asserted */
206-
for (timeout = 0; timeout < timeout_ms; timeout++) {
206+
do
207+
{
207208
if (gpio_pin_get_dt(intr) == 0)
208209
break;
209-
usleep(MSEC);
210-
}
210+
usleep(100);
211+
} while (time_since32(start) < (timeout_ms * MSEC));
212+
211213
/* make sure response is ok */
212214
if (gpio_pin_get_dt(intr) != 0) {
213215
CPRINTS("%s timeout on interrupt", __func__);
@@ -221,8 +223,20 @@ static int cypd_write_reg8_wait_ack(int controller, int reg, int data)
221223
int rv = EC_SUCCESS;
222224
int intr_status;
223225
int event;
224-
int cmd_port = reg & 0x2000 ? 1 : 0;
226+
int cmd_port = -1;
225227
int ack_mask = 0;
228+
int expected_ack_mask = 0;
229+
230+
if (reg < 0x1000) {
231+
expected_ack_mask = CCG_DEV_INTR;
232+
cmd_port = -1;
233+
} else if (reg < 0x2000) {
234+
expected_ack_mask = CCG_PORT0_INTR;
235+
cmd_port = 0;
236+
} else {
237+
expected_ack_mask = CCG_PORT1_INTR;
238+
cmd_port = 1;
239+
}
226240

227241
rv = cypd_write_reg8(controller, reg, data);
228242
if (rv != EC_SUCCESS)
@@ -236,27 +250,34 @@ static int cypd_write_reg8_wait_ack(int controller, int reg, int data)
236250
if (rv != EC_SUCCESS)
237251
CPRINTS("Get INT Fail");
238252

239-
if (intr_status & CCG_DEV_INTR) {
253+
if (intr_status & CCG_DEV_INTR && cmd_port == -1) {
240254
rv = cypd_read_reg16(controller, CCG_RESPONSE_REG, &event);
241255
if (rv != EC_SUCCESS)
242256
CPRINTS("fail to read DEV response");
243257
ack_mask = CCG_DEV_INTR;
244-
} else if (intr_status & CCG_PORT0_INTR && !cmd_port) {
258+
} else if (intr_status & CCG_PORT0_INTR && cmd_port == 0) {
245259
rv = cypd_read_reg16(controller, CCG_PORT_PD_RESPONSE_REG(0), &event);
246260
if (rv != EC_SUCCESS)
247261
CPRINTS("fail to read P0 response");
248262
ack_mask = CCG_PORT0_INTR;
249-
} else if (intr_status & CCG_PORT1_INTR && cmd_port) {
263+
} else if (intr_status & CCG_PORT1_INTR && cmd_port == 1) {
250264
rv = cypd_read_reg16(controller, CCG_PORT_PD_RESPONSE_REG(1), &event);
251265
if (rv != EC_SUCCESS)
252266
CPRINTS("fail to read P1 response");
253267
ack_mask = CCG_PORT1_INTR;
268+
} else {
269+
CPRINTS("%s C:%d Unexpected response 0x%x to reg 0x%x",
270+
__func__, controller, intr_status, reg);
254271
}
255272

256273
/* only clear response code let main task handle event code */
257274
if (event < 0x80) {
258275
cypd_clear_int(controller, ack_mask);
259-
rv = event & CCG_RESPONSE_SUCCESS ? EC_SUCCESS : EC_ERROR_INVAL;
276+
if (event != CCG_RESPONSE_SUCCESS) {
277+
CPRINTS("%s C:%d 0x%x response 0x%x",
278+
__func__, controller, reg, event);
279+
}
280+
rv = (event == CCG_RESPONSE_SUCCESS) ? EC_SUCCESS : EC_ERROR_INVAL;
260281
}
261282

262283
usleep(50);
@@ -1631,10 +1652,15 @@ void cypd_port_int(int controller, int port)
16311652
switch (data2[0]) {
16321653
case CCG_RESPONSE_PORT_DISCONNECT:
16331654
record_ucsi_connector_change_event(controller, port);
1655+
CPRINTS("PORT_DISCONNECT");
16341656
__fallthrough;
16351657
case CCG_RESPONSE_HARD_RESET_RX:
16361658
case CCG_RESPONSE_TYPE_C_ERROR_RECOVERY:
1637-
CPRINTS("TYPE_C_ERROR_RECOVERY");
1659+
if (data2[0] == CCG_RESPONSE_HARD_RESET_RX)
1660+
CPRINTS("HARD_RESET_RX");
1661+
if (data2[0] == CCG_RESPONSE_TYPE_C_ERROR_RECOVERY)
1662+
CPRINTS("TYPE_C_ERROR_RECOVERY");
1663+
16381664
cypd_update_port_state(controller, port);
16391665
cypd_release_port(controller, port);
16401666
/* make sure the type-c state is cleared */
@@ -2067,6 +2093,10 @@ static int cmd_cypd_get_status(int argc, const char **argv)
20672093
port_status[(data >> 2) & 0x7],
20682094
data & 0x20 ? "Ra" : "NoRa",
20692095
current_level[(data >> 6) & 0x03]);
2096+
cypd_read_reg8(i, CCG_PORT_VBUS_FET_CONTROL(p), &data);
2097+
CPRINTS(" VBUS_FET : %s %s",
2098+
data & 0x1 ? "EC" : "Auto",
2099+
data & 0x2 ? "On" : "Off");
20702100
cypd_read_reg_block(i, CCG_CURRENT_RDO_REG(p), data16, 4);
20712101
CPRINTS(" RDO : Current:%dmA MaxCurrent%dmA 0x%08x",
20722102
((data16[0] + (data16[1]<<8)) & 0x3FF)*10,
@@ -2080,10 +2110,10 @@ static int cmd_cypd_get_status(int argc, const char **argv)
20802110
*(uint32_t *)data16);
20812111
cypd_read_reg8(i, CCG_TYPE_C_VOLTAGE_REG(p), &data);
20822112
CPRINTS(" TYPE_C_VOLTAGE : %dmV", data*100);
2083-
cypd_read_reg16(i, CCG_PORT_INTR_STATUS_REG(p), &data);
2084-
CPRINTS(" INTR_STATUS_REG0: 0x%02x", data);
2085-
cypd_read_reg16(i, CCG_PORT_INTR_STATUS_REG(p)+2, &data);
2086-
CPRINTS(" INTR_STATUS_REG1: 0x%02x", data);
2113+
cypd_read_reg8(i, CCG_PORT_CURRENT_REG(p), &data);
2114+
CPRINTS(" TYPE_C_CURRENT : %dmA", data*50);
2115+
cypd_read_reg_block(i, CCG_PORT_INTR_STATUS_REG(p), data16, 4);
2116+
cypd_print_buff(" INTR_STATUS:", data16, 4);
20872117
cypd_read_reg16(i, SELECT_SINK_PDO_EPR_MASK(p), &data);
20882118
CPRINTS(" SINK PDO EPR MASK: 0x%02x", data);
20892119
/* Flush console to avoid truncating output */

0 commit comments

Comments
 (0)