platform/chrome: cros_ec_lpc: MEC access can return error code
cros_ec_lpc_io_bytes_mec was returning a u8 checksum of all bytes read/written, which didn't leave room to indicate errors. Change this u8 to an int where negative values indicate an error, and non-negative values are the checksum as before. Tested-by: Dustin L. Howett <dustin@howett.net> Signed-off-by: Ben Walsh <ben@jubnut.com> Link: https://lore.kernel.org/r/20240605063351.14836-2-ben@jubnut.com Signed-off-by: Tzung-Bi Shih <tzungbi@kernel.org>
This commit is contained in:
parent
106d673982
commit
68dbac0a58
4 changed files with 112 additions and 56 deletions
|
@ -62,14 +62,16 @@ struct cros_ec_lpc {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* struct lpc_driver_ops - LPC driver operations
|
* struct lpc_driver_ops - LPC driver operations
|
||||||
* @read: Copy length bytes from EC address offset into buffer dest. Returns
|
* @read: Copy length bytes from EC address offset into buffer dest.
|
||||||
* the 8-bit checksum of all bytes read.
|
* Returns a negative error code on error, or the 8-bit checksum
|
||||||
* @write: Copy length bytes from buffer msg into EC address offset. Returns
|
* of all bytes read.
|
||||||
* the 8-bit checksum of all bytes written.
|
* @write: Copy length bytes from buffer msg into EC address offset.
|
||||||
|
* Returns a negative error code on error, or the 8-bit checksum
|
||||||
|
* of all bytes written.
|
||||||
*/
|
*/
|
||||||
struct lpc_driver_ops {
|
struct lpc_driver_ops {
|
||||||
u8 (*read)(unsigned int offset, unsigned int length, u8 *dest);
|
int (*read)(unsigned int offset, unsigned int length, u8 *dest);
|
||||||
u8 (*write)(unsigned int offset, unsigned int length, const u8 *msg);
|
int (*write)(unsigned int offset, unsigned int length, const u8 *msg);
|
||||||
};
|
};
|
||||||
|
|
||||||
static struct lpc_driver_ops cros_ec_lpc_ops = { };
|
static struct lpc_driver_ops cros_ec_lpc_ops = { };
|
||||||
|
@ -78,10 +80,10 @@ static struct lpc_driver_ops cros_ec_lpc_ops = { };
|
||||||
* A generic instance of the read function of struct lpc_driver_ops, used for
|
* A generic instance of the read function of struct lpc_driver_ops, used for
|
||||||
* the LPC EC.
|
* the LPC EC.
|
||||||
*/
|
*/
|
||||||
static u8 cros_ec_lpc_read_bytes(unsigned int offset, unsigned int length,
|
static int cros_ec_lpc_read_bytes(unsigned int offset, unsigned int length,
|
||||||
u8 *dest)
|
u8 *dest)
|
||||||
{
|
{
|
||||||
int sum = 0;
|
u8 sum = 0;
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
for (i = 0; i < length; ++i) {
|
for (i = 0; i < length; ++i) {
|
||||||
|
@ -97,10 +99,10 @@ static u8 cros_ec_lpc_read_bytes(unsigned int offset, unsigned int length,
|
||||||
* A generic instance of the write function of struct lpc_driver_ops, used for
|
* A generic instance of the write function of struct lpc_driver_ops, used for
|
||||||
* the LPC EC.
|
* the LPC EC.
|
||||||
*/
|
*/
|
||||||
static u8 cros_ec_lpc_write_bytes(unsigned int offset, unsigned int length,
|
static int cros_ec_lpc_write_bytes(unsigned int offset, unsigned int length,
|
||||||
const u8 *msg)
|
const u8 *msg)
|
||||||
{
|
{
|
||||||
int sum = 0;
|
u8 sum = 0;
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
for (i = 0; i < length; ++i) {
|
for (i = 0; i < length; ++i) {
|
||||||
|
@ -116,8 +118,8 @@ static u8 cros_ec_lpc_write_bytes(unsigned int offset, unsigned int length,
|
||||||
* An instance of the read function of struct lpc_driver_ops, used for the
|
* An instance of the read function of struct lpc_driver_ops, used for the
|
||||||
* MEC variant of LPC EC.
|
* MEC variant of LPC EC.
|
||||||
*/
|
*/
|
||||||
static u8 cros_ec_lpc_mec_read_bytes(unsigned int offset, unsigned int length,
|
static int cros_ec_lpc_mec_read_bytes(unsigned int offset, unsigned int length,
|
||||||
u8 *dest)
|
u8 *dest)
|
||||||
{
|
{
|
||||||
int in_range = cros_ec_lpc_mec_in_range(offset, length);
|
int in_range = cros_ec_lpc_mec_in_range(offset, length);
|
||||||
|
|
||||||
|
@ -135,8 +137,8 @@ static u8 cros_ec_lpc_mec_read_bytes(unsigned int offset, unsigned int length,
|
||||||
* An instance of the write function of struct lpc_driver_ops, used for the
|
* An instance of the write function of struct lpc_driver_ops, used for the
|
||||||
* MEC variant of LPC EC.
|
* MEC variant of LPC EC.
|
||||||
*/
|
*/
|
||||||
static u8 cros_ec_lpc_mec_write_bytes(unsigned int offset, unsigned int length,
|
static int cros_ec_lpc_mec_write_bytes(unsigned int offset, unsigned int length,
|
||||||
const u8 *msg)
|
const u8 *msg)
|
||||||
{
|
{
|
||||||
int in_range = cros_ec_lpc_mec_in_range(offset, length);
|
int in_range = cros_ec_lpc_mec_in_range(offset, length);
|
||||||
|
|
||||||
|
@ -154,11 +156,14 @@ static int ec_response_timed_out(void)
|
||||||
{
|
{
|
||||||
unsigned long one_second = jiffies + HZ;
|
unsigned long one_second = jiffies + HZ;
|
||||||
u8 data;
|
u8 data;
|
||||||
|
int ret;
|
||||||
|
|
||||||
usleep_range(200, 300);
|
usleep_range(200, 300);
|
||||||
do {
|
do {
|
||||||
if (!(cros_ec_lpc_ops.read(EC_LPC_ADDR_HOST_CMD, 1, &data) &
|
ret = cros_ec_lpc_ops.read(EC_LPC_ADDR_HOST_CMD, 1, &data);
|
||||||
EC_LPC_STATUS_BUSY_MASK))
|
if (ret < 0)
|
||||||
|
return ret;
|
||||||
|
if (!(data & EC_LPC_STATUS_BUSY_MASK))
|
||||||
return 0;
|
return 0;
|
||||||
usleep_range(100, 200);
|
usleep_range(100, 200);
|
||||||
} while (time_before(jiffies, one_second));
|
} while (time_before(jiffies, one_second));
|
||||||
|
@ -179,28 +184,41 @@ static int cros_ec_pkt_xfer_lpc(struct cros_ec_device *ec,
|
||||||
goto done;
|
goto done;
|
||||||
|
|
||||||
/* Write buffer */
|
/* Write buffer */
|
||||||
cros_ec_lpc_ops.write(EC_LPC_ADDR_HOST_PACKET, ret, ec->dout);
|
ret = cros_ec_lpc_ops.write(EC_LPC_ADDR_HOST_PACKET, ret, ec->dout);
|
||||||
|
if (ret < 0)
|
||||||
|
goto done;
|
||||||
|
|
||||||
/* Here we go */
|
/* Here we go */
|
||||||
sum = EC_COMMAND_PROTOCOL_3;
|
sum = EC_COMMAND_PROTOCOL_3;
|
||||||
cros_ec_lpc_ops.write(EC_LPC_ADDR_HOST_CMD, 1, &sum);
|
ret = cros_ec_lpc_ops.write(EC_LPC_ADDR_HOST_CMD, 1, &sum);
|
||||||
|
if (ret < 0)
|
||||||
|
goto done;
|
||||||
|
|
||||||
if (ec_response_timed_out()) {
|
ret = ec_response_timed_out();
|
||||||
|
if (ret < 0)
|
||||||
|
goto done;
|
||||||
|
if (ret) {
|
||||||
dev_warn(ec->dev, "EC response timed out\n");
|
dev_warn(ec->dev, "EC response timed out\n");
|
||||||
ret = -EIO;
|
ret = -EIO;
|
||||||
goto done;
|
goto done;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Check result */
|
/* Check result */
|
||||||
msg->result = cros_ec_lpc_ops.read(EC_LPC_ADDR_HOST_DATA, 1, &sum);
|
ret = cros_ec_lpc_ops.read(EC_LPC_ADDR_HOST_DATA, 1, &sum);
|
||||||
|
if (ret < 0)
|
||||||
|
goto done;
|
||||||
|
msg->result = ret;
|
||||||
ret = cros_ec_check_result(ec, msg);
|
ret = cros_ec_check_result(ec, msg);
|
||||||
if (ret)
|
if (ret)
|
||||||
goto done;
|
goto done;
|
||||||
|
|
||||||
/* Read back response */
|
/* Read back response */
|
||||||
dout = (u8 *)&response;
|
dout = (u8 *)&response;
|
||||||
sum = cros_ec_lpc_ops.read(EC_LPC_ADDR_HOST_PACKET, sizeof(response),
|
ret = cros_ec_lpc_ops.read(EC_LPC_ADDR_HOST_PACKET, sizeof(response),
|
||||||
dout);
|
dout);
|
||||||
|
if (ret < 0)
|
||||||
|
goto done;
|
||||||
|
sum = ret;
|
||||||
|
|
||||||
msg->result = response.result;
|
msg->result = response.result;
|
||||||
|
|
||||||
|
@ -213,9 +231,12 @@ static int cros_ec_pkt_xfer_lpc(struct cros_ec_device *ec,
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Read response and process checksum */
|
/* Read response and process checksum */
|
||||||
sum += cros_ec_lpc_ops.read(EC_LPC_ADDR_HOST_PACKET +
|
ret = cros_ec_lpc_ops.read(EC_LPC_ADDR_HOST_PACKET +
|
||||||
sizeof(response), response.data_len,
|
sizeof(response), response.data_len,
|
||||||
msg->data);
|
msg->data);
|
||||||
|
if (ret < 0)
|
||||||
|
goto done;
|
||||||
|
sum += ret;
|
||||||
|
|
||||||
if (sum) {
|
if (sum) {
|
||||||
dev_err(ec->dev,
|
dev_err(ec->dev,
|
||||||
|
@ -255,32 +276,47 @@ static int cros_ec_cmd_xfer_lpc(struct cros_ec_device *ec,
|
||||||
sum = msg->command + args.flags + args.command_version + args.data_size;
|
sum = msg->command + args.flags + args.command_version + args.data_size;
|
||||||
|
|
||||||
/* Copy data and update checksum */
|
/* Copy data and update checksum */
|
||||||
sum += cros_ec_lpc_ops.write(EC_LPC_ADDR_HOST_PARAM, msg->outsize,
|
ret = cros_ec_lpc_ops.write(EC_LPC_ADDR_HOST_PARAM, msg->outsize,
|
||||||
msg->data);
|
msg->data);
|
||||||
|
if (ret < 0)
|
||||||
|
goto done;
|
||||||
|
sum += ret;
|
||||||
|
|
||||||
/* Finalize checksum and write args */
|
/* Finalize checksum and write args */
|
||||||
args.checksum = sum;
|
args.checksum = sum;
|
||||||
cros_ec_lpc_ops.write(EC_LPC_ADDR_HOST_ARGS, sizeof(args),
|
ret = cros_ec_lpc_ops.write(EC_LPC_ADDR_HOST_ARGS, sizeof(args),
|
||||||
(u8 *)&args);
|
(u8 *)&args);
|
||||||
|
if (ret < 0)
|
||||||
|
goto done;
|
||||||
|
|
||||||
/* Here we go */
|
/* Here we go */
|
||||||
sum = msg->command;
|
sum = msg->command;
|
||||||
cros_ec_lpc_ops.write(EC_LPC_ADDR_HOST_CMD, 1, &sum);
|
ret = cros_ec_lpc_ops.write(EC_LPC_ADDR_HOST_CMD, 1, &sum);
|
||||||
|
if (ret < 0)
|
||||||
|
goto done;
|
||||||
|
|
||||||
if (ec_response_timed_out()) {
|
ret = ec_response_timed_out();
|
||||||
|
if (ret < 0)
|
||||||
|
goto done;
|
||||||
|
if (ret) {
|
||||||
dev_warn(ec->dev, "EC response timed out\n");
|
dev_warn(ec->dev, "EC response timed out\n");
|
||||||
ret = -EIO;
|
ret = -EIO;
|
||||||
goto done;
|
goto done;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Check result */
|
/* Check result */
|
||||||
msg->result = cros_ec_lpc_ops.read(EC_LPC_ADDR_HOST_DATA, 1, &sum);
|
ret = cros_ec_lpc_ops.read(EC_LPC_ADDR_HOST_DATA, 1, &sum);
|
||||||
|
if (ret < 0)
|
||||||
|
goto done;
|
||||||
|
msg->result = ret;
|
||||||
ret = cros_ec_check_result(ec, msg);
|
ret = cros_ec_check_result(ec, msg);
|
||||||
if (ret)
|
if (ret)
|
||||||
goto done;
|
goto done;
|
||||||
|
|
||||||
/* Read back args */
|
/* Read back args */
|
||||||
cros_ec_lpc_ops.read(EC_LPC_ADDR_HOST_ARGS, sizeof(args), (u8 *)&args);
|
ret = cros_ec_lpc_ops.read(EC_LPC_ADDR_HOST_ARGS, sizeof(args), (u8 *)&args);
|
||||||
|
if (ret < 0)
|
||||||
|
goto done;
|
||||||
|
|
||||||
if (args.data_size > msg->insize) {
|
if (args.data_size > msg->insize) {
|
||||||
dev_err(ec->dev,
|
dev_err(ec->dev,
|
||||||
|
@ -294,8 +330,11 @@ static int cros_ec_cmd_xfer_lpc(struct cros_ec_device *ec,
|
||||||
sum = msg->command + args.flags + args.command_version + args.data_size;
|
sum = msg->command + args.flags + args.command_version + args.data_size;
|
||||||
|
|
||||||
/* Read response and update checksum */
|
/* Read response and update checksum */
|
||||||
sum += cros_ec_lpc_ops.read(EC_LPC_ADDR_HOST_PARAM, args.data_size,
|
ret = cros_ec_lpc_ops.read(EC_LPC_ADDR_HOST_PARAM, args.data_size,
|
||||||
msg->data);
|
msg->data);
|
||||||
|
if (ret < 0)
|
||||||
|
goto done;
|
||||||
|
sum += ret;
|
||||||
|
|
||||||
/* Verify checksum */
|
/* Verify checksum */
|
||||||
if (args.checksum != sum) {
|
if (args.checksum != sum) {
|
||||||
|
@ -320,19 +359,24 @@ static int cros_ec_lpc_readmem(struct cros_ec_device *ec, unsigned int offset,
|
||||||
int i = offset;
|
int i = offset;
|
||||||
char *s = dest;
|
char *s = dest;
|
||||||
int cnt = 0;
|
int cnt = 0;
|
||||||
|
int ret;
|
||||||
|
|
||||||
if (offset >= EC_MEMMAP_SIZE - bytes)
|
if (offset >= EC_MEMMAP_SIZE - bytes)
|
||||||
return -EINVAL;
|
return -EINVAL;
|
||||||
|
|
||||||
/* fixed length */
|
/* fixed length */
|
||||||
if (bytes) {
|
if (bytes) {
|
||||||
cros_ec_lpc_ops.read(ec_lpc->mmio_memory_base + offset, bytes, s);
|
ret = cros_ec_lpc_ops.read(ec_lpc->mmio_memory_base + offset, bytes, s);
|
||||||
|
if (ret < 0)
|
||||||
|
return ret;
|
||||||
return bytes;
|
return bytes;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* string */
|
/* string */
|
||||||
for (; i < EC_MEMMAP_SIZE; i++, s++) {
|
for (; i < EC_MEMMAP_SIZE; i++, s++) {
|
||||||
cros_ec_lpc_ops.read(ec_lpc->mmio_memory_base + i, 1, s);
|
ret = cros_ec_lpc_ops.read(ec_lpc->mmio_memory_base + i, 1, s);
|
||||||
|
if (ret < 0)
|
||||||
|
return ret;
|
||||||
cnt++;
|
cnt++;
|
||||||
if (!*s)
|
if (!*s)
|
||||||
break;
|
break;
|
||||||
|
@ -425,7 +469,9 @@ static int cros_ec_lpc_probe(struct platform_device *pdev)
|
||||||
*/
|
*/
|
||||||
cros_ec_lpc_ops.read = cros_ec_lpc_mec_read_bytes;
|
cros_ec_lpc_ops.read = cros_ec_lpc_mec_read_bytes;
|
||||||
cros_ec_lpc_ops.write = cros_ec_lpc_mec_write_bytes;
|
cros_ec_lpc_ops.write = cros_ec_lpc_mec_write_bytes;
|
||||||
cros_ec_lpc_ops.read(EC_LPC_ADDR_MEMMAP + EC_MEMMAP_ID, 2, buf);
|
ret = cros_ec_lpc_ops.read(EC_LPC_ADDR_MEMMAP + EC_MEMMAP_ID, 2, buf);
|
||||||
|
if (ret < 0)
|
||||||
|
return ret;
|
||||||
if (buf[0] != 'E' || buf[1] != 'C') {
|
if (buf[0] != 'E' || buf[1] != 'C') {
|
||||||
if (!devm_request_region(dev, ec_lpc->mmio_memory_base, EC_MEMMAP_SIZE,
|
if (!devm_request_region(dev, ec_lpc->mmio_memory_base, EC_MEMMAP_SIZE,
|
||||||
dev_name(dev))) {
|
dev_name(dev))) {
|
||||||
|
@ -436,8 +482,10 @@ static int cros_ec_lpc_probe(struct platform_device *pdev)
|
||||||
/* Re-assign read/write operations for the non MEC variant */
|
/* Re-assign read/write operations for the non MEC variant */
|
||||||
cros_ec_lpc_ops.read = cros_ec_lpc_read_bytes;
|
cros_ec_lpc_ops.read = cros_ec_lpc_read_bytes;
|
||||||
cros_ec_lpc_ops.write = cros_ec_lpc_write_bytes;
|
cros_ec_lpc_ops.write = cros_ec_lpc_write_bytes;
|
||||||
cros_ec_lpc_ops.read(ec_lpc->mmio_memory_base + EC_MEMMAP_ID, 2,
|
ret = cros_ec_lpc_ops.read(ec_lpc->mmio_memory_base + EC_MEMMAP_ID, 2,
|
||||||
buf);
|
buf);
|
||||||
|
if (ret < 0)
|
||||||
|
return ret;
|
||||||
if (buf[0] != 'E' || buf[1] != 'C') {
|
if (buf[0] != 'E' || buf[1] != 'C') {
|
||||||
dev_err(dev, "EC ID not detected\n");
|
dev_err(dev, "EC ID not detected\n");
|
||||||
return -ENODEV;
|
return -ENODEV;
|
||||||
|
|
|
@ -67,11 +67,12 @@ int cros_ec_lpc_mec_in_range(unsigned int offset, unsigned int length)
|
||||||
* @length: Number of bytes to read / write
|
* @length: Number of bytes to read / write
|
||||||
* @buf: Destination / source buffer
|
* @buf: Destination / source buffer
|
||||||
*
|
*
|
||||||
* Return: 8-bit checksum of all bytes read / written
|
* @return: A negative error code on error, or 8-bit checksum of all
|
||||||
|
* bytes read / written
|
||||||
*/
|
*/
|
||||||
u8 cros_ec_lpc_io_bytes_mec(enum cros_ec_lpc_mec_io_type io_type,
|
int cros_ec_lpc_io_bytes_mec(enum cros_ec_lpc_mec_io_type io_type,
|
||||||
unsigned int offset, unsigned int length,
|
unsigned int offset, unsigned int length,
|
||||||
u8 *buf)
|
u8 *buf)
|
||||||
{
|
{
|
||||||
int i = 0;
|
int i = 0;
|
||||||
int io_addr;
|
int io_addr;
|
||||||
|
|
|
@ -64,9 +64,10 @@ int cros_ec_lpc_mec_in_range(unsigned int offset, unsigned int length);
|
||||||
* @length: Number of bytes to read / write
|
* @length: Number of bytes to read / write
|
||||||
* @buf: Destination / source buffer
|
* @buf: Destination / source buffer
|
||||||
*
|
*
|
||||||
* @return 8-bit checksum of all bytes read / written
|
* @return: A negative error code on error, or 8-bit checksum of all
|
||||||
|
* bytes read / written
|
||||||
*/
|
*/
|
||||||
u8 cros_ec_lpc_io_bytes_mec(enum cros_ec_lpc_mec_io_type io_type,
|
int cros_ec_lpc_io_bytes_mec(enum cros_ec_lpc_mec_io_type io_type,
|
||||||
unsigned int offset, unsigned int length, u8 *buf);
|
unsigned int offset, unsigned int length, u8 *buf);
|
||||||
|
|
||||||
#endif /* __CROS_EC_LPC_MEC_H */
|
#endif /* __CROS_EC_LPC_MEC_H */
|
||||||
|
|
|
@ -117,13 +117,17 @@ static int wilco_ec_transfer(struct wilco_ec_device *ec,
|
||||||
struct wilco_ec_request *rq)
|
struct wilco_ec_request *rq)
|
||||||
{
|
{
|
||||||
struct wilco_ec_response *rs;
|
struct wilco_ec_response *rs;
|
||||||
u8 checksum;
|
int ret;
|
||||||
u8 flag;
|
u8 flag;
|
||||||
|
|
||||||
/* Write request header, then data */
|
/* Write request header, then data */
|
||||||
cros_ec_lpc_io_bytes_mec(MEC_IO_WRITE, 0, sizeof(*rq), (u8 *)rq);
|
ret = cros_ec_lpc_io_bytes_mec(MEC_IO_WRITE, 0, sizeof(*rq), (u8 *)rq);
|
||||||
cros_ec_lpc_io_bytes_mec(MEC_IO_WRITE, sizeof(*rq), msg->request_size,
|
if (ret < 0)
|
||||||
msg->request_data);
|
return ret;
|
||||||
|
ret = cros_ec_lpc_io_bytes_mec(MEC_IO_WRITE, sizeof(*rq), msg->request_size,
|
||||||
|
msg->request_data);
|
||||||
|
if (ret < 0)
|
||||||
|
return ret;
|
||||||
|
|
||||||
/* Start the command */
|
/* Start the command */
|
||||||
outb(EC_MAILBOX_START_COMMAND, ec->io_command->start);
|
outb(EC_MAILBOX_START_COMMAND, ec->io_command->start);
|
||||||
|
@ -149,10 +153,12 @@ static int wilco_ec_transfer(struct wilco_ec_device *ec,
|
||||||
|
|
||||||
/* Read back response */
|
/* Read back response */
|
||||||
rs = ec->data_buffer;
|
rs = ec->data_buffer;
|
||||||
checksum = cros_ec_lpc_io_bytes_mec(MEC_IO_READ, 0,
|
ret = cros_ec_lpc_io_bytes_mec(MEC_IO_READ, 0,
|
||||||
sizeof(*rs) + EC_MAILBOX_DATA_SIZE,
|
sizeof(*rs) + EC_MAILBOX_DATA_SIZE,
|
||||||
(u8 *)rs);
|
(u8 *)rs);
|
||||||
if (checksum) {
|
if (ret < 0)
|
||||||
|
return ret;
|
||||||
|
if (ret) {
|
||||||
dev_dbg(ec->dev, "bad packet checksum 0x%02x\n", rs->checksum);
|
dev_dbg(ec->dev, "bad packet checksum 0x%02x\n", rs->checksum);
|
||||||
return -EBADMSG;
|
return -EBADMSG;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue