diff options
Diffstat (limited to 'driver/retimer/bb_retimer.c')
-rw-r--r-- | driver/retimer/bb_retimer.c | 95 |
1 files changed, 17 insertions, 78 deletions
diff --git a/driver/retimer/bb_retimer.c b/driver/retimer/bb_retimer.c index cd18a7a6ef..5571b666d4 100644 --- a/driver/retimer/bb_retimer.c +++ b/driver/retimer/bb_retimer.c @@ -37,6 +37,7 @@ #define CPRINTF(format, args...) cprintf(CC_USBCHARGE, format, ##args) #define BB_RETIMER_I2C_RETRY 5 +#define BB_RETIMER_REG_OFFSET_MAX UINT8_MAX /* * Mutex for BB_RETIMER_REG_CONNECTION_STATE register, which can be @@ -51,12 +52,16 @@ static mux_state_t bb_mux_state[CONFIG_USB_PD_PORT_MAX_COUNT]; /** * Utility functions */ -static int bb_retimer_read(const struct usb_mux *me, const uint8_t offset, +static int bb_retimer_read(const struct usb_mux *me, const uint32_t offset, uint32_t *data) { int rv, retry = 0; uint8_t buf[BB_RETIMER_READ_SIZE]; + /* Validate the register address */ + if (offset > BB_RETIMER_REG_OFFSET_MAX) + return EC_ERROR_INVAL; + /* * This I2C message will trigger retimer's internal read sequence * if its a NAK, sleep and resend same I2C @@ -69,7 +74,8 @@ static int bb_retimer_read(const struct usb_mux *me, const uint8_t offset, * byte[1:4] : Data [LSB -> MSB] * Stop */ - rv = i2c_xfer(me->i2c_port, me->i2c_addr_flags, &offset, 1, buf, + rv = i2c_xfer(me->i2c_port, me->i2c_addr_flags, + (const uint8_t *)&offset, 1, buf, BB_RETIMER_READ_SIZE); if (rv == EC_SUCCESS) @@ -91,12 +97,16 @@ static int bb_retimer_read(const struct usb_mux *me, const uint8_t offset, return EC_SUCCESS; } -static int bb_retimer_write(const struct usb_mux *me, const uint8_t offset, +static int bb_retimer_write(const struct usb_mux *me, const uint32_t offset, uint32_t data) { int rv, retry = 0; uint8_t buf[BB_RETIMER_WRITE_SIZE]; + /* Validate the register address */ + if (offset > BB_RETIMER_REG_OFFSET_MAX) + return EC_ERROR_INVAL; + /* * Write sequence * Addr flags(w) @@ -105,7 +115,7 @@ static int bb_retimer_write(const struct usb_mux *me, const uint8_t offset, * byte[2:5] : Data [LSB -> MSB] * stop */ - buf[0] = offset; + buf[0] = (uint8_t)offset; buf[1] = BB_RETIMER_REG_SIZE; buf[2] = data & 0xFF; buf[3] = (data >> 8) & 0xFF; @@ -670,79 +680,8 @@ const struct usb_mux_driver bb_usb_retimer = { .set_idle_mode = bb_set_idle_mode, .enter_low_power_mode = retimer_low_power_mode, .is_retimer_fw_update_capable = is_retimer_fw_update_capable, -}; - #ifdef CONFIG_CMD_RETIMER -static int console_command_bb_retimer(int argc, const char **argv) -{ - char rw, *e; - int port; - uint8_t reg; - uint32_t data, val = 0; - int rv = EC_SUCCESS; - const struct usb_mux *mux; - const struct usb_mux_chain *mux_chain; - - if (argc < 4) - return EC_ERROR_PARAM_COUNT; - - /* Get port number */ - port = strtoi(argv[1], &e, 0); - if (*e || !board_is_usb_pd_port_present(port)) - return EC_ERROR_PARAM1; - - mux_chain = &usb_muxes[port]; - while (mux_chain) { - mux = mux_chain->mux; - if (mux->driver == &bb_usb_retimer) - break; - mux_chain = mux_chain->next; - } - - if (!mux_chain) - return EC_ERROR_PARAM1; - - /* Validate r/w selection */ - rw = argv[2][0]; - if (rw != 'w' && rw != 'r') - return EC_ERROR_PARAM2; - - /* Get register address */ - reg = (uint8_t)strtoull(argv[3], &e, 0); - if (*e) - return EC_ERROR_PARAM3; - - /* Get value to be written */ - if (rw == 'w') { - val = strtoull(argv[4], &e, 0); - if (*e) - return EC_ERROR_PARAM4; - } - - for (; mux_chain != NULL; mux_chain = mux_chain->next) { - mux = mux_chain->mux; - if (mux->driver == &bb_usb_retimer) { - if (rw == 'r') - rv = bb_retimer_read(mux, reg, &data); - else { - rv = bb_retimer_write(mux, reg, val); - if (rv == EC_SUCCESS) { - rv = bb_retimer_read(mux, reg, &data); - if (rv == EC_SUCCESS && data != val) - rv = EC_ERROR_UNKNOWN; - } - } - if (rv == EC_SUCCESS) - CPRINTS("Addr 0x%x register %d = 0x%x", - mux->i2c_addr_flags, reg, data); - } - } - - return rv; -} -/* TODO(b/278138274): Use common console command for all Retimers */ -DECLARE_CONSOLE_COMMAND(bb, console_command_bb_retimer, - "<port> r <reg>" - "\n<port> w <reg> <val>", - "Read or write to BB retimer register"); + .retimer_read = bb_retimer_read, + .retimer_write = bb_retimer_write, #endif /* CONFIG_CMD_RETIMER */ +}; |