xref: /rk3399_rockchip-uboot/drivers/misc/cros_ec.c (revision 17c82fdc12e24b32e49a3b53f59b8b2f6b136f8b)
188364387SHung-ying Tyan /*
288364387SHung-ying Tyan  * Chromium OS cros_ec driver
388364387SHung-ying Tyan  *
488364387SHung-ying Tyan  * Copyright (c) 2012 The Chromium OS Authors.
588364387SHung-ying Tyan  *
61a459660SWolfgang Denk  * SPDX-License-Identifier:	GPL-2.0+
788364387SHung-ying Tyan  */
888364387SHung-ying Tyan 
988364387SHung-ying Tyan /*
10836bb6e8SSimon Glass  * This is the interface to the Chrome OS EC. It provides keyboard functions,
11836bb6e8SSimon Glass  * power control and battery management. Quite a few other functions are
12836bb6e8SSimon Glass  * provided to enable the EC software to be updated, talk to the EC's I2C bus
13836bb6e8SSimon Glass  * and store a small amount of data in a memory which persists while the EC
14836bb6e8SSimon Glass  * is not reset.
1588364387SHung-ying Tyan  */
1688364387SHung-ying Tyan 
1788364387SHung-ying Tyan #include <common.h>
1888364387SHung-ying Tyan #include <command.h>
1984d6cbd3SSimon Glass #include <dm.h>
2088364387SHung-ying Tyan #include <i2c.h>
2188364387SHung-ying Tyan #include <cros_ec.h>
2288364387SHung-ying Tyan #include <fdtdec.h>
2388364387SHung-ying Tyan #include <malloc.h>
2488364387SHung-ying Tyan #include <spi.h>
251221ce45SMasahiro Yamada #include <linux/errno.h>
2688364387SHung-ying Tyan #include <asm/io.h>
2788364387SHung-ying Tyan #include <asm-generic/gpio.h>
2884d6cbd3SSimon Glass #include <dm/device-internal.h>
292ec9d171SSimon Glass #include <dm/of_extra.h>
3084d6cbd3SSimon Glass #include <dm/uclass-internal.h>
3188364387SHung-ying Tyan 
3288364387SHung-ying Tyan #ifdef DEBUG_TRACE
3388364387SHung-ying Tyan #define debug_trace(fmt, b...)	debug(fmt, #b)
3488364387SHung-ying Tyan #else
3588364387SHung-ying Tyan #define debug_trace(fmt, b...)
3688364387SHung-ying Tyan #endif
3788364387SHung-ying Tyan 
3888364387SHung-ying Tyan enum {
3988364387SHung-ying Tyan 	/* Timeout waiting for a flash erase command to complete */
4088364387SHung-ying Tyan 	CROS_EC_CMD_TIMEOUT_MS	= 5000,
4188364387SHung-ying Tyan 	/* Timeout waiting for a synchronous hash to be recomputed */
4288364387SHung-ying Tyan 	CROS_EC_CMD_HASH_TIMEOUT_MS = 2000,
4388364387SHung-ying Tyan };
4488364387SHung-ying Tyan 
4588364387SHung-ying Tyan DECLARE_GLOBAL_DATA_PTR;
4688364387SHung-ying Tyan 
cros_ec_dump_data(const char * name,int cmd,const uint8_t * data,int len)4788364387SHung-ying Tyan void cros_ec_dump_data(const char *name, int cmd, const uint8_t *data, int len)
4888364387SHung-ying Tyan {
4988364387SHung-ying Tyan #ifdef DEBUG
5088364387SHung-ying Tyan 	int i;
5188364387SHung-ying Tyan 
5288364387SHung-ying Tyan 	printf("%s: ", name);
5388364387SHung-ying Tyan 	if (cmd != -1)
5488364387SHung-ying Tyan 		printf("cmd=%#x: ", cmd);
5588364387SHung-ying Tyan 	for (i = 0; i < len; i++)
5688364387SHung-ying Tyan 		printf("%02x ", data[i]);
5788364387SHung-ying Tyan 	printf("\n");
5888364387SHung-ying Tyan #endif
5988364387SHung-ying Tyan }
6088364387SHung-ying Tyan 
6188364387SHung-ying Tyan /*
6288364387SHung-ying Tyan  * Calculate a simple 8-bit checksum of a data block
6388364387SHung-ying Tyan  *
6488364387SHung-ying Tyan  * @param data	Data block to checksum
6588364387SHung-ying Tyan  * @param size	Size of data block in bytes
6688364387SHung-ying Tyan  * @return checksum value (0 to 255)
6788364387SHung-ying Tyan  */
cros_ec_calc_checksum(const uint8_t * data,int size)6888364387SHung-ying Tyan int cros_ec_calc_checksum(const uint8_t *data, int size)
6988364387SHung-ying Tyan {
7088364387SHung-ying Tyan 	int csum, i;
7188364387SHung-ying Tyan 
7288364387SHung-ying Tyan 	for (i = csum = 0; i < size; i++)
7388364387SHung-ying Tyan 		csum += data[i];
7488364387SHung-ying Tyan 	return csum & 0xff;
7588364387SHung-ying Tyan }
7688364387SHung-ying Tyan 
772d8ede58SSimon Glass /**
782d8ede58SSimon Glass  * Create a request packet for protocol version 3.
792d8ede58SSimon Glass  *
802d8ede58SSimon Glass  * The packet is stored in the device's internal output buffer.
812d8ede58SSimon Glass  *
822d8ede58SSimon Glass  * @param dev		CROS-EC device
832d8ede58SSimon Glass  * @param cmd		Command to send (EC_CMD_...)
842d8ede58SSimon Glass  * @param cmd_version	Version of command to send (EC_VER_...)
852d8ede58SSimon Glass  * @param dout          Output data (may be NULL If dout_len=0)
862d8ede58SSimon Glass  * @param dout_len      Size of output data in bytes
872d8ede58SSimon Glass  * @return packet size in bytes, or <0 if error.
882d8ede58SSimon Glass  */
create_proto3_request(struct cros_ec_dev * dev,int cmd,int cmd_version,const void * dout,int dout_len)892d8ede58SSimon Glass static int create_proto3_request(struct cros_ec_dev *dev,
902d8ede58SSimon Glass 				 int cmd, int cmd_version,
912d8ede58SSimon Glass 				 const void *dout, int dout_len)
922d8ede58SSimon Glass {
932d8ede58SSimon Glass 	struct ec_host_request *rq = (struct ec_host_request *)dev->dout;
942d8ede58SSimon Glass 	int out_bytes = dout_len + sizeof(*rq);
952d8ede58SSimon Glass 
962d8ede58SSimon Glass 	/* Fail if output size is too big */
972d8ede58SSimon Glass 	if (out_bytes > (int)sizeof(dev->dout)) {
982d8ede58SSimon Glass 		debug("%s: Cannot send %d bytes\n", __func__, dout_len);
992d8ede58SSimon Glass 		return -EC_RES_REQUEST_TRUNCATED;
1002d8ede58SSimon Glass 	}
1012d8ede58SSimon Glass 
1022d8ede58SSimon Glass 	/* Fill in request packet */
1032d8ede58SSimon Glass 	rq->struct_version = EC_HOST_REQUEST_VERSION;
1042d8ede58SSimon Glass 	rq->checksum = 0;
1052d8ede58SSimon Glass 	rq->command = cmd;
1062d8ede58SSimon Glass 	rq->command_version = cmd_version;
1072d8ede58SSimon Glass 	rq->reserved = 0;
1082d8ede58SSimon Glass 	rq->data_len = dout_len;
1092d8ede58SSimon Glass 
1102d8ede58SSimon Glass 	/* Copy data after header */
1112d8ede58SSimon Glass 	memcpy(rq + 1, dout, dout_len);
1122d8ede58SSimon Glass 
1132d8ede58SSimon Glass 	/* Write checksum field so the entire packet sums to 0 */
1142d8ede58SSimon Glass 	rq->checksum = (uint8_t)(-cros_ec_calc_checksum(dev->dout, out_bytes));
1152d8ede58SSimon Glass 
1162d8ede58SSimon Glass 	cros_ec_dump_data("out", cmd, dev->dout, out_bytes);
1172d8ede58SSimon Glass 
1182d8ede58SSimon Glass 	/* Return size of request packet */
1192d8ede58SSimon Glass 	return out_bytes;
1202d8ede58SSimon Glass }
1212d8ede58SSimon Glass 
1222d8ede58SSimon Glass /**
1232d8ede58SSimon Glass  * Prepare the device to receive a protocol version 3 response.
1242d8ede58SSimon Glass  *
1252d8ede58SSimon Glass  * @param dev		CROS-EC device
1262d8ede58SSimon Glass  * @param din_len       Maximum size of response in bytes
1272d8ede58SSimon Glass  * @return maximum expected number of bytes in response, or <0 if error.
1282d8ede58SSimon Glass  */
prepare_proto3_response_buffer(struct cros_ec_dev * dev,int din_len)1292d8ede58SSimon Glass static int prepare_proto3_response_buffer(struct cros_ec_dev *dev, int din_len)
1302d8ede58SSimon Glass {
1312d8ede58SSimon Glass 	int in_bytes = din_len + sizeof(struct ec_host_response);
1322d8ede58SSimon Glass 
1332d8ede58SSimon Glass 	/* Fail if input size is too big */
1342d8ede58SSimon Glass 	if (in_bytes > (int)sizeof(dev->din)) {
1352d8ede58SSimon Glass 		debug("%s: Cannot receive %d bytes\n", __func__, din_len);
1362d8ede58SSimon Glass 		return -EC_RES_RESPONSE_TOO_BIG;
1372d8ede58SSimon Glass 	}
1382d8ede58SSimon Glass 
1392d8ede58SSimon Glass 	/* Return expected size of response packet */
1402d8ede58SSimon Glass 	return in_bytes;
1412d8ede58SSimon Glass }
1422d8ede58SSimon Glass 
1432d8ede58SSimon Glass /**
1442d8ede58SSimon Glass  * Handle a protocol version 3 response packet.
1452d8ede58SSimon Glass  *
1462d8ede58SSimon Glass  * The packet must already be stored in the device's internal input buffer.
1472d8ede58SSimon Glass  *
1482d8ede58SSimon Glass  * @param dev		CROS-EC device
1492d8ede58SSimon Glass  * @param dinp          Returns pointer to response data
1502d8ede58SSimon Glass  * @param din_len       Maximum size of response in bytes
1518bbb38b1SSimon Glass  * @return number of bytes of response data, or <0 if error. Note that error
1528bbb38b1SSimon Glass  * codes can be from errno.h or -ve EC_RES_INVALID_CHECKSUM values (and they
1538bbb38b1SSimon Glass  * overlap!)
1542d8ede58SSimon Glass  */
handle_proto3_response(struct cros_ec_dev * dev,uint8_t ** dinp,int din_len)1552d8ede58SSimon Glass static int handle_proto3_response(struct cros_ec_dev *dev,
1562d8ede58SSimon Glass 				  uint8_t **dinp, int din_len)
1572d8ede58SSimon Glass {
1582d8ede58SSimon Glass 	struct ec_host_response *rs = (struct ec_host_response *)dev->din;
1592d8ede58SSimon Glass 	int in_bytes;
1602d8ede58SSimon Glass 	int csum;
1612d8ede58SSimon Glass 
1622d8ede58SSimon Glass 	cros_ec_dump_data("in-header", -1, dev->din, sizeof(*rs));
1632d8ede58SSimon Glass 
1642d8ede58SSimon Glass 	/* Check input data */
1652d8ede58SSimon Glass 	if (rs->struct_version != EC_HOST_RESPONSE_VERSION) {
1662d8ede58SSimon Glass 		debug("%s: EC response version mismatch\n", __func__);
1672d8ede58SSimon Glass 		return -EC_RES_INVALID_RESPONSE;
1682d8ede58SSimon Glass 	}
1692d8ede58SSimon Glass 
1702d8ede58SSimon Glass 	if (rs->reserved) {
1712d8ede58SSimon Glass 		debug("%s: EC response reserved != 0\n", __func__);
1722d8ede58SSimon Glass 		return -EC_RES_INVALID_RESPONSE;
1732d8ede58SSimon Glass 	}
1742d8ede58SSimon Glass 
1752d8ede58SSimon Glass 	if (rs->data_len > din_len) {
1762d8ede58SSimon Glass 		debug("%s: EC returned too much data\n", __func__);
1772d8ede58SSimon Glass 		return -EC_RES_RESPONSE_TOO_BIG;
1782d8ede58SSimon Glass 	}
1792d8ede58SSimon Glass 
1802d8ede58SSimon Glass 	cros_ec_dump_data("in-data", -1, dev->din + sizeof(*rs), rs->data_len);
1812d8ede58SSimon Glass 
1822d8ede58SSimon Glass 	/* Update in_bytes to actual data size */
1832d8ede58SSimon Glass 	in_bytes = sizeof(*rs) + rs->data_len;
1842d8ede58SSimon Glass 
1852d8ede58SSimon Glass 	/* Verify checksum */
1862d8ede58SSimon Glass 	csum = cros_ec_calc_checksum(dev->din, in_bytes);
1872d8ede58SSimon Glass 	if (csum) {
1882d8ede58SSimon Glass 		debug("%s: EC response checksum invalid: 0x%02x\n", __func__,
1892d8ede58SSimon Glass 		      csum);
1902d8ede58SSimon Glass 		return -EC_RES_INVALID_CHECKSUM;
1912d8ede58SSimon Glass 	}
1922d8ede58SSimon Glass 
1932d8ede58SSimon Glass 	/* Return error result, if any */
1942d8ede58SSimon Glass 	if (rs->result)
1952d8ede58SSimon Glass 		return -(int)rs->result;
1962d8ede58SSimon Glass 
1972d8ede58SSimon Glass 	/* If we're still here, set response data pointer and return length */
1982d8ede58SSimon Glass 	*dinp = (uint8_t *)(rs + 1);
1992d8ede58SSimon Glass 
2002d8ede58SSimon Glass 	return rs->data_len;
2012d8ede58SSimon Glass }
2022d8ede58SSimon Glass 
send_command_proto3(struct cros_ec_dev * dev,int cmd,int cmd_version,const void * dout,int dout_len,uint8_t ** dinp,int din_len)2032d8ede58SSimon Glass static int send_command_proto3(struct cros_ec_dev *dev,
2042d8ede58SSimon Glass 			       int cmd, int cmd_version,
2052d8ede58SSimon Glass 			       const void *dout, int dout_len,
2062d8ede58SSimon Glass 			       uint8_t **dinp, int din_len)
2072d8ede58SSimon Glass {
20884d6cbd3SSimon Glass 	struct dm_cros_ec_ops *ops;
2092d8ede58SSimon Glass 	int out_bytes, in_bytes;
2102d8ede58SSimon Glass 	int rv;
2112d8ede58SSimon Glass 
2122d8ede58SSimon Glass 	/* Create request packet */
2132d8ede58SSimon Glass 	out_bytes = create_proto3_request(dev, cmd, cmd_version,
2142d8ede58SSimon Glass 					  dout, dout_len);
2152d8ede58SSimon Glass 	if (out_bytes < 0)
2162d8ede58SSimon Glass 		return out_bytes;
2172d8ede58SSimon Glass 
2182d8ede58SSimon Glass 	/* Prepare response buffer */
2192d8ede58SSimon Glass 	in_bytes = prepare_proto3_response_buffer(dev, din_len);
2202d8ede58SSimon Glass 	if (in_bytes < 0)
2212d8ede58SSimon Glass 		return in_bytes;
2222d8ede58SSimon Glass 
22384d6cbd3SSimon Glass 	ops = dm_cros_ec_get_ops(dev->dev);
2248bbb38b1SSimon Glass 	rv = ops->packet ? ops->packet(dev->dev, out_bytes, in_bytes) : -ENOSYS;
2252d8ede58SSimon Glass 	if (rv < 0)
2262d8ede58SSimon Glass 		return rv;
2272d8ede58SSimon Glass 
2282d8ede58SSimon Glass 	/* Process the response */
2292d8ede58SSimon Glass 	return handle_proto3_response(dev, dinp, din_len);
2302d8ede58SSimon Glass }
2312d8ede58SSimon Glass 
send_command(struct cros_ec_dev * dev,uint8_t cmd,int cmd_version,const void * dout,int dout_len,uint8_t ** dinp,int din_len)23288364387SHung-ying Tyan static int send_command(struct cros_ec_dev *dev, uint8_t cmd, int cmd_version,
23388364387SHung-ying Tyan 			const void *dout, int dout_len,
23488364387SHung-ying Tyan 			uint8_t **dinp, int din_len)
23588364387SHung-ying Tyan {
23684d6cbd3SSimon Glass 	struct dm_cros_ec_ops *ops;
2372d8ede58SSimon Glass 	int ret = -1;
2382d8ede58SSimon Glass 
2392d8ede58SSimon Glass 	/* Handle protocol version 3 support */
2402d8ede58SSimon Glass 	if (dev->protocol_version == 3) {
2412d8ede58SSimon Glass 		return send_command_proto3(dev, cmd, cmd_version,
2422d8ede58SSimon Glass 					   dout, dout_len, dinp, din_len);
2432d8ede58SSimon Glass 	}
24488364387SHung-ying Tyan 
24584d6cbd3SSimon Glass 	ops = dm_cros_ec_get_ops(dev->dev);
24684d6cbd3SSimon Glass 	ret = ops->command(dev->dev, cmd, cmd_version,
24784d6cbd3SSimon Glass 			   (const uint8_t *)dout, dout_len, dinp, din_len);
24888364387SHung-ying Tyan 
24988364387SHung-ying Tyan 	return ret;
25088364387SHung-ying Tyan }
25188364387SHung-ying Tyan 
25288364387SHung-ying Tyan /**
25388364387SHung-ying Tyan  * Send a command to the CROS-EC device and return the reply.
25488364387SHung-ying Tyan  *
25588364387SHung-ying Tyan  * The device's internal input/output buffers are used.
25688364387SHung-ying Tyan  *
25788364387SHung-ying Tyan  * @param dev		CROS-EC device
25888364387SHung-ying Tyan  * @param cmd		Command to send (EC_CMD_...)
25988364387SHung-ying Tyan  * @param cmd_version	Version of command to send (EC_VER_...)
26088364387SHung-ying Tyan  * @param dout          Output data (may be NULL If dout_len=0)
26188364387SHung-ying Tyan  * @param dout_len      Size of output data in bytes
26288364387SHung-ying Tyan  * @param dinp          Response data (may be NULL If din_len=0).
26388364387SHung-ying Tyan  *			If not NULL, it will be updated to point to the data
26488364387SHung-ying Tyan  *			and will always be double word aligned (64-bits)
26588364387SHung-ying Tyan  * @param din_len       Maximum size of response in bytes
2668bbb38b1SSimon Glass  * @return number of bytes in response, or -ve on error
26788364387SHung-ying Tyan  */
ec_command_inptr(struct cros_ec_dev * dev,uint8_t cmd,int cmd_version,const void * dout,int dout_len,uint8_t ** dinp,int din_len)26888364387SHung-ying Tyan static int ec_command_inptr(struct cros_ec_dev *dev, uint8_t cmd,
26988364387SHung-ying Tyan 		int cmd_version, const void *dout, int dout_len, uint8_t **dinp,
27088364387SHung-ying Tyan 		int din_len)
27188364387SHung-ying Tyan {
2722ab83f0dSSimon Glass 	uint8_t *din = NULL;
27388364387SHung-ying Tyan 	int len;
27488364387SHung-ying Tyan 
27588364387SHung-ying Tyan 	len = send_command(dev, cmd, cmd_version, dout, dout_len,
27688364387SHung-ying Tyan 				&din, din_len);
27788364387SHung-ying Tyan 
27888364387SHung-ying Tyan 	/* If the command doesn't complete, wait a while */
27988364387SHung-ying Tyan 	if (len == -EC_RES_IN_PROGRESS) {
2802ab83f0dSSimon Glass 		struct ec_response_get_comms_status *resp = NULL;
28188364387SHung-ying Tyan 		ulong start;
28288364387SHung-ying Tyan 
28388364387SHung-ying Tyan 		/* Wait for command to complete */
28488364387SHung-ying Tyan 		start = get_timer(0);
28588364387SHung-ying Tyan 		do {
28688364387SHung-ying Tyan 			int ret;
28788364387SHung-ying Tyan 
28888364387SHung-ying Tyan 			mdelay(50);	/* Insert some reasonable delay */
28988364387SHung-ying Tyan 			ret = send_command(dev, EC_CMD_GET_COMMS_STATUS, 0,
29088364387SHung-ying Tyan 					NULL, 0,
29188364387SHung-ying Tyan 					(uint8_t **)&resp, sizeof(*resp));
29288364387SHung-ying Tyan 			if (ret < 0)
29388364387SHung-ying Tyan 				return ret;
29488364387SHung-ying Tyan 
29588364387SHung-ying Tyan 			if (get_timer(start) > CROS_EC_CMD_TIMEOUT_MS) {
29688364387SHung-ying Tyan 				debug("%s: Command %#02x timeout\n",
29788364387SHung-ying Tyan 				      __func__, cmd);
29888364387SHung-ying Tyan 				return -EC_RES_TIMEOUT;
29988364387SHung-ying Tyan 			}
30088364387SHung-ying Tyan 		} while (resp->flags & EC_COMMS_STATUS_PROCESSING);
30188364387SHung-ying Tyan 
30288364387SHung-ying Tyan 		/* OK it completed, so read the status response */
30388364387SHung-ying Tyan 		/* not sure why it was 0 for the last argument */
30488364387SHung-ying Tyan 		len = send_command(dev, EC_CMD_RESEND_RESPONSE, 0,
30588364387SHung-ying Tyan 				NULL, 0, &din, din_len);
30688364387SHung-ying Tyan 	}
30788364387SHung-ying Tyan 
308e907bf2dSSimon Glass 	debug("%s: len=%d, din=%p\n", __func__, len, din);
30988364387SHung-ying Tyan 	if (dinp) {
31088364387SHung-ying Tyan 		/* If we have any data to return, it must be 64bit-aligned */
31188364387SHung-ying Tyan 		assert(len <= 0 || !((uintptr_t)din & 7));
31288364387SHung-ying Tyan 		*dinp = din;
31388364387SHung-ying Tyan 	}
31488364387SHung-ying Tyan 
31588364387SHung-ying Tyan 	return len;
31688364387SHung-ying Tyan }
31788364387SHung-ying Tyan 
31888364387SHung-ying Tyan /**
31988364387SHung-ying Tyan  * Send a command to the CROS-EC device and return the reply.
32088364387SHung-ying Tyan  *
32188364387SHung-ying Tyan  * The device's internal input/output buffers are used.
32288364387SHung-ying Tyan  *
32388364387SHung-ying Tyan  * @param dev		CROS-EC device
32488364387SHung-ying Tyan  * @param cmd		Command to send (EC_CMD_...)
32588364387SHung-ying Tyan  * @param cmd_version	Version of command to send (EC_VER_...)
32688364387SHung-ying Tyan  * @param dout          Output data (may be NULL If dout_len=0)
32788364387SHung-ying Tyan  * @param dout_len      Size of output data in bytes
32888364387SHung-ying Tyan  * @param din           Response data (may be NULL If din_len=0).
32988364387SHung-ying Tyan  *			It not NULL, it is a place for ec_command() to copy the
33088364387SHung-ying Tyan  *      data to.
33188364387SHung-ying Tyan  * @param din_len       Maximum size of response in bytes
3328bbb38b1SSimon Glass  * @return number of bytes in response, or -ve on error
33388364387SHung-ying Tyan  */
ec_command(struct cros_ec_dev * dev,uint8_t cmd,int cmd_version,const void * dout,int dout_len,void * din,int din_len)33488364387SHung-ying Tyan static int ec_command(struct cros_ec_dev *dev, uint8_t cmd, int cmd_version,
33588364387SHung-ying Tyan 		      const void *dout, int dout_len,
33688364387SHung-ying Tyan 		      void *din, int din_len)
33788364387SHung-ying Tyan {
33888364387SHung-ying Tyan 	uint8_t *in_buffer;
33988364387SHung-ying Tyan 	int len;
34088364387SHung-ying Tyan 
34188364387SHung-ying Tyan 	assert((din_len == 0) || din);
34288364387SHung-ying Tyan 	len = ec_command_inptr(dev, cmd, cmd_version, dout, dout_len,
34388364387SHung-ying Tyan 			&in_buffer, din_len);
34488364387SHung-ying Tyan 	if (len > 0) {
34588364387SHung-ying Tyan 		/*
34688364387SHung-ying Tyan 		 * If we were asked to put it somewhere, do so, otherwise just
34788364387SHung-ying Tyan 		 * disregard the result.
34888364387SHung-ying Tyan 		 */
34988364387SHung-ying Tyan 		if (din && in_buffer) {
35088364387SHung-ying Tyan 			assert(len <= din_len);
35188364387SHung-ying Tyan 			memmove(din, in_buffer, len);
35288364387SHung-ying Tyan 		}
35388364387SHung-ying Tyan 	}
35488364387SHung-ying Tyan 	return len;
35588364387SHung-ying Tyan }
35688364387SHung-ying Tyan 
cros_ec_scan_keyboard(struct udevice * dev,struct mbkp_keyscan * scan)357745009c4SSimon Glass int cros_ec_scan_keyboard(struct udevice *dev, struct mbkp_keyscan *scan)
35888364387SHung-ying Tyan {
359745009c4SSimon Glass 	struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
360745009c4SSimon Glass 
361745009c4SSimon Glass 	if (ec_command(cdev, EC_CMD_MKBP_STATE, 0, NULL, 0, scan,
3622ab83f0dSSimon Glass 		       sizeof(scan->data)) != sizeof(scan->data))
36388364387SHung-ying Tyan 		return -1;
36488364387SHung-ying Tyan 
36588364387SHung-ying Tyan 	return 0;
36688364387SHung-ying Tyan }
36788364387SHung-ying Tyan 
cros_ec_read_id(struct cros_ec_dev * dev,char * id,int maxlen)36888364387SHung-ying Tyan int cros_ec_read_id(struct cros_ec_dev *dev, char *id, int maxlen)
36988364387SHung-ying Tyan {
37088364387SHung-ying Tyan 	struct ec_response_get_version *r;
37188364387SHung-ying Tyan 
37288364387SHung-ying Tyan 	if (ec_command_inptr(dev, EC_CMD_GET_VERSION, 0, NULL, 0,
3732ab83f0dSSimon Glass 			(uint8_t **)&r, sizeof(*r)) != sizeof(*r))
37488364387SHung-ying Tyan 		return -1;
37588364387SHung-ying Tyan 
3762ab83f0dSSimon Glass 	if (maxlen > (int)sizeof(r->version_string_ro))
37788364387SHung-ying Tyan 		maxlen = sizeof(r->version_string_ro);
37888364387SHung-ying Tyan 
37988364387SHung-ying Tyan 	switch (r->current_image) {
38088364387SHung-ying Tyan 	case EC_IMAGE_RO:
38188364387SHung-ying Tyan 		memcpy(id, r->version_string_ro, maxlen);
38288364387SHung-ying Tyan 		break;
38388364387SHung-ying Tyan 	case EC_IMAGE_RW:
38488364387SHung-ying Tyan 		memcpy(id, r->version_string_rw, maxlen);
38588364387SHung-ying Tyan 		break;
38688364387SHung-ying Tyan 	default:
38788364387SHung-ying Tyan 		return -1;
38888364387SHung-ying Tyan 	}
38988364387SHung-ying Tyan 
39088364387SHung-ying Tyan 	id[maxlen - 1] = '\0';
39188364387SHung-ying Tyan 	return 0;
39288364387SHung-ying Tyan }
39388364387SHung-ying Tyan 
cros_ec_read_version(struct cros_ec_dev * dev,struct ec_response_get_version ** versionp)39488364387SHung-ying Tyan int cros_ec_read_version(struct cros_ec_dev *dev,
39588364387SHung-ying Tyan 		       struct ec_response_get_version **versionp)
39688364387SHung-ying Tyan {
39788364387SHung-ying Tyan 	if (ec_command_inptr(dev, EC_CMD_GET_VERSION, 0, NULL, 0,
39888364387SHung-ying Tyan 			(uint8_t **)versionp, sizeof(**versionp))
3992ab83f0dSSimon Glass 			!= sizeof(**versionp))
40088364387SHung-ying Tyan 		return -1;
40188364387SHung-ying Tyan 
40288364387SHung-ying Tyan 	return 0;
40388364387SHung-ying Tyan }
40488364387SHung-ying Tyan 
cros_ec_read_build_info(struct cros_ec_dev * dev,char ** strp)40588364387SHung-ying Tyan int cros_ec_read_build_info(struct cros_ec_dev *dev, char **strp)
40688364387SHung-ying Tyan {
40788364387SHung-ying Tyan 	if (ec_command_inptr(dev, EC_CMD_GET_BUILD_INFO, 0, NULL, 0,
408836bb6e8SSimon Glass 			(uint8_t **)strp, EC_PROTO2_MAX_PARAM_SIZE) < 0)
40988364387SHung-ying Tyan 		return -1;
41088364387SHung-ying Tyan 
41188364387SHung-ying Tyan 	return 0;
41288364387SHung-ying Tyan }
41388364387SHung-ying Tyan 
cros_ec_read_current_image(struct cros_ec_dev * dev,enum ec_current_image * image)41488364387SHung-ying Tyan int cros_ec_read_current_image(struct cros_ec_dev *dev,
41588364387SHung-ying Tyan 		enum ec_current_image *image)
41688364387SHung-ying Tyan {
41788364387SHung-ying Tyan 	struct ec_response_get_version *r;
41888364387SHung-ying Tyan 
41988364387SHung-ying Tyan 	if (ec_command_inptr(dev, EC_CMD_GET_VERSION, 0, NULL, 0,
4202ab83f0dSSimon Glass 			(uint8_t **)&r, sizeof(*r)) != sizeof(*r))
42188364387SHung-ying Tyan 		return -1;
42288364387SHung-ying Tyan 
42388364387SHung-ying Tyan 	*image = r->current_image;
42488364387SHung-ying Tyan 	return 0;
42588364387SHung-ying Tyan }
42688364387SHung-ying Tyan 
cros_ec_wait_on_hash_done(struct cros_ec_dev * dev,struct ec_response_vboot_hash * hash)42788364387SHung-ying Tyan static int cros_ec_wait_on_hash_done(struct cros_ec_dev *dev,
42888364387SHung-ying Tyan 				  struct ec_response_vboot_hash *hash)
42988364387SHung-ying Tyan {
43088364387SHung-ying Tyan 	struct ec_params_vboot_hash p;
43188364387SHung-ying Tyan 	ulong start;
43288364387SHung-ying Tyan 
43388364387SHung-ying Tyan 	start = get_timer(0);
43488364387SHung-ying Tyan 	while (hash->status == EC_VBOOT_HASH_STATUS_BUSY) {
43588364387SHung-ying Tyan 		mdelay(50);	/* Insert some reasonable delay */
43688364387SHung-ying Tyan 
43788364387SHung-ying Tyan 		p.cmd = EC_VBOOT_HASH_GET;
43888364387SHung-ying Tyan 		if (ec_command(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
43988364387SHung-ying Tyan 		       hash, sizeof(*hash)) < 0)
44088364387SHung-ying Tyan 			return -1;
44188364387SHung-ying Tyan 
44288364387SHung-ying Tyan 		if (get_timer(start) > CROS_EC_CMD_HASH_TIMEOUT_MS) {
44388364387SHung-ying Tyan 			debug("%s: EC_VBOOT_HASH_GET timeout\n", __func__);
44488364387SHung-ying Tyan 			return -EC_RES_TIMEOUT;
44588364387SHung-ying Tyan 		}
44688364387SHung-ying Tyan 	}
44788364387SHung-ying Tyan 	return 0;
44888364387SHung-ying Tyan }
44988364387SHung-ying Tyan 
45088364387SHung-ying Tyan 
cros_ec_read_hash(struct cros_ec_dev * dev,struct ec_response_vboot_hash * hash)45188364387SHung-ying Tyan int cros_ec_read_hash(struct cros_ec_dev *dev,
45288364387SHung-ying Tyan 		struct ec_response_vboot_hash *hash)
45388364387SHung-ying Tyan {
45488364387SHung-ying Tyan 	struct ec_params_vboot_hash p;
45588364387SHung-ying Tyan 	int rv;
45688364387SHung-ying Tyan 
45788364387SHung-ying Tyan 	p.cmd = EC_VBOOT_HASH_GET;
45888364387SHung-ying Tyan 	if (ec_command(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
45988364387SHung-ying Tyan 		       hash, sizeof(*hash)) < 0)
46088364387SHung-ying Tyan 		return -1;
46188364387SHung-ying Tyan 
46288364387SHung-ying Tyan 	/* If the EC is busy calculating the hash, fidget until it's done. */
46388364387SHung-ying Tyan 	rv = cros_ec_wait_on_hash_done(dev, hash);
46488364387SHung-ying Tyan 	if (rv)
46588364387SHung-ying Tyan 		return rv;
46688364387SHung-ying Tyan 
46788364387SHung-ying Tyan 	/* If the hash is valid, we're done. Otherwise, we have to kick it off
46888364387SHung-ying Tyan 	 * again and wait for it to complete. Note that we explicitly assume
46988364387SHung-ying Tyan 	 * that hashing zero bytes is always wrong, even though that would
47088364387SHung-ying Tyan 	 * produce a valid hash value. */
47188364387SHung-ying Tyan 	if (hash->status == EC_VBOOT_HASH_STATUS_DONE && hash->size)
47288364387SHung-ying Tyan 		return 0;
47388364387SHung-ying Tyan 
47488364387SHung-ying Tyan 	debug("%s: No valid hash (status=%d size=%d). Compute one...\n",
47588364387SHung-ying Tyan 	      __func__, hash->status, hash->size);
47688364387SHung-ying Tyan 
477836bb6e8SSimon Glass 	p.cmd = EC_VBOOT_HASH_START;
47888364387SHung-ying Tyan 	p.hash_type = EC_VBOOT_HASH_TYPE_SHA256;
47988364387SHung-ying Tyan 	p.nonce_size = 0;
48088364387SHung-ying Tyan 	p.offset = EC_VBOOT_HASH_OFFSET_RW;
48188364387SHung-ying Tyan 
48288364387SHung-ying Tyan 	if (ec_command(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
48388364387SHung-ying Tyan 		       hash, sizeof(*hash)) < 0)
48488364387SHung-ying Tyan 		return -1;
48588364387SHung-ying Tyan 
48688364387SHung-ying Tyan 	rv = cros_ec_wait_on_hash_done(dev, hash);
48788364387SHung-ying Tyan 	if (rv)
48888364387SHung-ying Tyan 		return rv;
48988364387SHung-ying Tyan 
49088364387SHung-ying Tyan 	debug("%s: hash done\n", __func__);
49188364387SHung-ying Tyan 
49288364387SHung-ying Tyan 	return 0;
49388364387SHung-ying Tyan }
49488364387SHung-ying Tyan 
cros_ec_invalidate_hash(struct cros_ec_dev * dev)49588364387SHung-ying Tyan static int cros_ec_invalidate_hash(struct cros_ec_dev *dev)
49688364387SHung-ying Tyan {
49788364387SHung-ying Tyan 	struct ec_params_vboot_hash p;
49888364387SHung-ying Tyan 	struct ec_response_vboot_hash *hash;
49988364387SHung-ying Tyan 
50088364387SHung-ying Tyan 	/* We don't have an explict command for the EC to discard its current
50188364387SHung-ying Tyan 	 * hash value, so we'll just tell it to calculate one that we know is
50288364387SHung-ying Tyan 	 * wrong (we claim that hashing zero bytes is always invalid).
50388364387SHung-ying Tyan 	 */
50488364387SHung-ying Tyan 	p.cmd = EC_VBOOT_HASH_RECALC;
50588364387SHung-ying Tyan 	p.hash_type = EC_VBOOT_HASH_TYPE_SHA256;
50688364387SHung-ying Tyan 	p.nonce_size = 0;
50788364387SHung-ying Tyan 	p.offset = 0;
50888364387SHung-ying Tyan 	p.size = 0;
50988364387SHung-ying Tyan 
51088364387SHung-ying Tyan 	debug("%s:\n", __func__);
51188364387SHung-ying Tyan 
51288364387SHung-ying Tyan 	if (ec_command_inptr(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
51388364387SHung-ying Tyan 		       (uint8_t **)&hash, sizeof(*hash)) < 0)
51488364387SHung-ying Tyan 		return -1;
51588364387SHung-ying Tyan 
51688364387SHung-ying Tyan 	/* No need to wait for it to finish */
51788364387SHung-ying Tyan 	return 0;
51888364387SHung-ying Tyan }
51988364387SHung-ying Tyan 
cros_ec_reboot(struct cros_ec_dev * dev,enum ec_reboot_cmd cmd,uint8_t flags)52088364387SHung-ying Tyan int cros_ec_reboot(struct cros_ec_dev *dev, enum ec_reboot_cmd cmd,
52188364387SHung-ying Tyan 		uint8_t flags)
52288364387SHung-ying Tyan {
52388364387SHung-ying Tyan 	struct ec_params_reboot_ec p;
52488364387SHung-ying Tyan 
52588364387SHung-ying Tyan 	p.cmd = cmd;
52688364387SHung-ying Tyan 	p.flags = flags;
52788364387SHung-ying Tyan 
52888364387SHung-ying Tyan 	if (ec_command_inptr(dev, EC_CMD_REBOOT_EC, 0, &p, sizeof(p), NULL, 0)
52988364387SHung-ying Tyan 			< 0)
53088364387SHung-ying Tyan 		return -1;
53188364387SHung-ying Tyan 
53288364387SHung-ying Tyan 	if (!(flags & EC_REBOOT_FLAG_ON_AP_SHUTDOWN)) {
53388364387SHung-ying Tyan 		/*
53488364387SHung-ying Tyan 		 * EC reboot will take place immediately so delay to allow it
53588364387SHung-ying Tyan 		 * to complete.  Note that some reboot types (EC_REBOOT_COLD)
53688364387SHung-ying Tyan 		 * will reboot the AP as well, in which case we won't actually
53788364387SHung-ying Tyan 		 * get to this point.
53888364387SHung-ying Tyan 		 */
53988364387SHung-ying Tyan 		/*
54088364387SHung-ying Tyan 		 * TODO(rspangler@chromium.org): Would be nice if we had a
54188364387SHung-ying Tyan 		 * better way to determine when the reboot is complete.  Could
54288364387SHung-ying Tyan 		 * we poll a memory-mapped LPC value?
54388364387SHung-ying Tyan 		 */
54488364387SHung-ying Tyan 		udelay(50000);
54588364387SHung-ying Tyan 	}
54688364387SHung-ying Tyan 
54788364387SHung-ying Tyan 	return 0;
54888364387SHung-ying Tyan }
54988364387SHung-ying Tyan 
cros_ec_interrupt_pending(struct udevice * dev)550745009c4SSimon Glass int cros_ec_interrupt_pending(struct udevice *dev)
55188364387SHung-ying Tyan {
552745009c4SSimon Glass 	struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
553745009c4SSimon Glass 
55488364387SHung-ying Tyan 	/* no interrupt support : always poll */
555745009c4SSimon Glass 	if (!dm_gpio_is_valid(&cdev->ec_int))
5562ab83f0dSSimon Glass 		return -ENOENT;
55788364387SHung-ying Tyan 
558745009c4SSimon Glass 	return dm_gpio_get_value(&cdev->ec_int);
55988364387SHung-ying Tyan }
56088364387SHung-ying Tyan 
cros_ec_info(struct cros_ec_dev * dev,struct ec_response_mkbp_info * info)561836bb6e8SSimon Glass int cros_ec_info(struct cros_ec_dev *dev, struct ec_response_mkbp_info *info)
56288364387SHung-ying Tyan {
563836bb6e8SSimon Glass 	if (ec_command(dev, EC_CMD_MKBP_INFO, 0, NULL, 0, info,
5642ab83f0dSSimon Glass 		       sizeof(*info)) != sizeof(*info))
56588364387SHung-ying Tyan 		return -1;
56688364387SHung-ying Tyan 
56788364387SHung-ying Tyan 	return 0;
56888364387SHung-ying Tyan }
56988364387SHung-ying Tyan 
cros_ec_get_host_events(struct cros_ec_dev * dev,uint32_t * events_ptr)57088364387SHung-ying Tyan int cros_ec_get_host_events(struct cros_ec_dev *dev, uint32_t *events_ptr)
57188364387SHung-ying Tyan {
57288364387SHung-ying Tyan 	struct ec_response_host_event_mask *resp;
57388364387SHung-ying Tyan 
57488364387SHung-ying Tyan 	/*
57588364387SHung-ying Tyan 	 * Use the B copy of the event flags, because the main copy is already
57688364387SHung-ying Tyan 	 * used by ACPI/SMI.
57788364387SHung-ying Tyan 	 */
57888364387SHung-ying Tyan 	if (ec_command_inptr(dev, EC_CMD_HOST_EVENT_GET_B, 0, NULL, 0,
5792ab83f0dSSimon Glass 		       (uint8_t **)&resp, sizeof(*resp)) < (int)sizeof(*resp))
58088364387SHung-ying Tyan 		return -1;
58188364387SHung-ying Tyan 
58288364387SHung-ying Tyan 	if (resp->mask & EC_HOST_EVENT_MASK(EC_HOST_EVENT_INVALID))
58388364387SHung-ying Tyan 		return -1;
58488364387SHung-ying Tyan 
58588364387SHung-ying Tyan 	*events_ptr = resp->mask;
58688364387SHung-ying Tyan 	return 0;
58788364387SHung-ying Tyan }
58888364387SHung-ying Tyan 
cros_ec_clear_host_events(struct cros_ec_dev * dev,uint32_t events)58988364387SHung-ying Tyan int cros_ec_clear_host_events(struct cros_ec_dev *dev, uint32_t events)
59088364387SHung-ying Tyan {
59188364387SHung-ying Tyan 	struct ec_params_host_event_mask params;
59288364387SHung-ying Tyan 
59388364387SHung-ying Tyan 	params.mask = events;
59488364387SHung-ying Tyan 
59588364387SHung-ying Tyan 	/*
59688364387SHung-ying Tyan 	 * Use the B copy of the event flags, so it affects the data returned
59788364387SHung-ying Tyan 	 * by cros_ec_get_host_events().
59888364387SHung-ying Tyan 	 */
59988364387SHung-ying Tyan 	if (ec_command_inptr(dev, EC_CMD_HOST_EVENT_CLEAR_B, 0,
60088364387SHung-ying Tyan 		       &params, sizeof(params), NULL, 0) < 0)
60188364387SHung-ying Tyan 		return -1;
60288364387SHung-ying Tyan 
60388364387SHung-ying Tyan 	return 0;
60488364387SHung-ying Tyan }
60588364387SHung-ying Tyan 
cros_ec_flash_protect(struct cros_ec_dev * dev,uint32_t set_mask,uint32_t set_flags,struct ec_response_flash_protect * resp)60688364387SHung-ying Tyan int cros_ec_flash_protect(struct cros_ec_dev *dev,
60788364387SHung-ying Tyan 		       uint32_t set_mask, uint32_t set_flags,
60888364387SHung-ying Tyan 		       struct ec_response_flash_protect *resp)
60988364387SHung-ying Tyan {
61088364387SHung-ying Tyan 	struct ec_params_flash_protect params;
61188364387SHung-ying Tyan 
61288364387SHung-ying Tyan 	params.mask = set_mask;
61388364387SHung-ying Tyan 	params.flags = set_flags;
61488364387SHung-ying Tyan 
61588364387SHung-ying Tyan 	if (ec_command(dev, EC_CMD_FLASH_PROTECT, EC_VER_FLASH_PROTECT,
61688364387SHung-ying Tyan 		       &params, sizeof(params),
6172ab83f0dSSimon Glass 		       resp, sizeof(*resp)) != sizeof(*resp))
61888364387SHung-ying Tyan 		return -1;
61988364387SHung-ying Tyan 
62088364387SHung-ying Tyan 	return 0;
62188364387SHung-ying Tyan }
62288364387SHung-ying Tyan 
cros_ec_check_version(struct cros_ec_dev * dev)62388364387SHung-ying Tyan static int cros_ec_check_version(struct cros_ec_dev *dev)
62488364387SHung-ying Tyan {
62588364387SHung-ying Tyan 	struct ec_params_hello req;
62688364387SHung-ying Tyan 	struct ec_response_hello *resp;
62788364387SHung-ying Tyan 
62872a38e06SSimon Glass 	struct dm_cros_ec_ops *ops;
62972a38e06SSimon Glass 	int ret;
63072a38e06SSimon Glass 
63172a38e06SSimon Glass 	ops = dm_cros_ec_get_ops(dev->dev);
63272a38e06SSimon Glass 	if (ops->check_version) {
63372a38e06SSimon Glass 		ret = ops->check_version(dev->dev);
63472a38e06SSimon Glass 		if (ret)
63572a38e06SSimon Glass 			return ret;
63672a38e06SSimon Glass 	}
63788364387SHung-ying Tyan 
63888364387SHung-ying Tyan 	/*
63988364387SHung-ying Tyan 	 * TODO(sjg@chromium.org).
64088364387SHung-ying Tyan 	 * There is a strange oddity here with the EC. We could just ignore
64188364387SHung-ying Tyan 	 * the response, i.e. pass the last two parameters as NULL and 0.
64288364387SHung-ying Tyan 	 * In this case we won't read back very many bytes from the EC.
64388364387SHung-ying Tyan 	 * On the I2C bus the EC gets upset about this and will try to send
64488364387SHung-ying Tyan 	 * the bytes anyway. This means that we will have to wait for that
64588364387SHung-ying Tyan 	 * to complete before continuing with a new EC command.
64688364387SHung-ying Tyan 	 *
64788364387SHung-ying Tyan 	 * This problem is probably unique to the I2C bus.
64888364387SHung-ying Tyan 	 *
64988364387SHung-ying Tyan 	 * So for now, just read all the data anyway.
65088364387SHung-ying Tyan 	 */
651e8c12662SRandall Spangler 
652a6070283SRandall Spangler 	/* Try sending a version 3 packet */
653a6070283SRandall Spangler 	dev->protocol_version = 3;
654d11e8fd8SSimon Glass 	req.in_data = 0;
655a6070283SRandall Spangler 	if (ec_command_inptr(dev, EC_CMD_HELLO, 0, &req, sizeof(req),
656a6070283SRandall Spangler 			     (uint8_t **)&resp, sizeof(*resp)) > 0) {
657a6070283SRandall Spangler 		return 0;
658a6070283SRandall Spangler 	}
659a6070283SRandall Spangler 
660e8c12662SRandall Spangler 	/* Try sending a version 2 packet */
661e8c12662SRandall Spangler 	dev->protocol_version = 2;
66288364387SHung-ying Tyan 	if (ec_command_inptr(dev, EC_CMD_HELLO, 0, &req, sizeof(req),
66388364387SHung-ying Tyan 		       (uint8_t **)&resp, sizeof(*resp)) > 0) {
664e8c12662SRandall Spangler 		return 0;
66588364387SHung-ying Tyan 	}
66688364387SHung-ying Tyan 
667e8c12662SRandall Spangler 	/*
668e8c12662SRandall Spangler 	 * Fail if we're still here, since the EC doesn't understand any
669e8c12662SRandall Spangler 	 * protcol version we speak.  Version 1 interface without command
670e8c12662SRandall Spangler 	 * version is no longer supported, and we don't know about any new
671e8c12662SRandall Spangler 	 * protocol versions.
672e8c12662SRandall Spangler 	 */
673e8c12662SRandall Spangler 	dev->protocol_version = 0;
674e8c12662SRandall Spangler 	printf("%s: ERROR: old EC interface not supported\n", __func__);
675e8c12662SRandall Spangler 	return -1;
67688364387SHung-ying Tyan }
67788364387SHung-ying Tyan 
cros_ec_test(struct cros_ec_dev * dev)67888364387SHung-ying Tyan int cros_ec_test(struct cros_ec_dev *dev)
67988364387SHung-ying Tyan {
68088364387SHung-ying Tyan 	struct ec_params_hello req;
68188364387SHung-ying Tyan 	struct ec_response_hello *resp;
68288364387SHung-ying Tyan 
68388364387SHung-ying Tyan 	req.in_data = 0x12345678;
68488364387SHung-ying Tyan 	if (ec_command_inptr(dev, EC_CMD_HELLO, 0, &req, sizeof(req),
68588364387SHung-ying Tyan 		       (uint8_t **)&resp, sizeof(*resp)) < sizeof(*resp)) {
68688364387SHung-ying Tyan 		printf("ec_command_inptr() returned error\n");
68788364387SHung-ying Tyan 		return -1;
68888364387SHung-ying Tyan 	}
68988364387SHung-ying Tyan 	if (resp->out_data != req.in_data + 0x01020304) {
69088364387SHung-ying Tyan 		printf("Received invalid handshake %x\n", resp->out_data);
69188364387SHung-ying Tyan 		return -1;
69288364387SHung-ying Tyan 	}
69388364387SHung-ying Tyan 
69488364387SHung-ying Tyan 	return 0;
69588364387SHung-ying Tyan }
69688364387SHung-ying Tyan 
cros_ec_flash_offset(struct cros_ec_dev * dev,enum ec_flash_region region,uint32_t * offset,uint32_t * size)69788364387SHung-ying Tyan int cros_ec_flash_offset(struct cros_ec_dev *dev, enum ec_flash_region region,
69888364387SHung-ying Tyan 		      uint32_t *offset, uint32_t *size)
69988364387SHung-ying Tyan {
70088364387SHung-ying Tyan 	struct ec_params_flash_region_info p;
70188364387SHung-ying Tyan 	struct ec_response_flash_region_info *r;
70288364387SHung-ying Tyan 	int ret;
70388364387SHung-ying Tyan 
70488364387SHung-ying Tyan 	p.region = region;
70588364387SHung-ying Tyan 	ret = ec_command_inptr(dev, EC_CMD_FLASH_REGION_INFO,
70688364387SHung-ying Tyan 			 EC_VER_FLASH_REGION_INFO,
70788364387SHung-ying Tyan 			 &p, sizeof(p), (uint8_t **)&r, sizeof(*r));
70888364387SHung-ying Tyan 	if (ret != sizeof(*r))
70988364387SHung-ying Tyan 		return -1;
71088364387SHung-ying Tyan 
71188364387SHung-ying Tyan 	if (offset)
71288364387SHung-ying Tyan 		*offset = r->offset;
71388364387SHung-ying Tyan 	if (size)
71488364387SHung-ying Tyan 		*size = r->size;
71588364387SHung-ying Tyan 
71688364387SHung-ying Tyan 	return 0;
71788364387SHung-ying Tyan }
71888364387SHung-ying Tyan 
cros_ec_flash_erase(struct cros_ec_dev * dev,uint32_t offset,uint32_t size)71988364387SHung-ying Tyan int cros_ec_flash_erase(struct cros_ec_dev *dev, uint32_t offset, uint32_t size)
72088364387SHung-ying Tyan {
72188364387SHung-ying Tyan 	struct ec_params_flash_erase p;
72288364387SHung-ying Tyan 
72388364387SHung-ying Tyan 	p.offset = offset;
72488364387SHung-ying Tyan 	p.size = size;
72588364387SHung-ying Tyan 	return ec_command_inptr(dev, EC_CMD_FLASH_ERASE, 0, &p, sizeof(p),
72688364387SHung-ying Tyan 			NULL, 0);
72788364387SHung-ying Tyan }
72888364387SHung-ying Tyan 
72988364387SHung-ying Tyan /**
73088364387SHung-ying Tyan  * Write a single block to the flash
73188364387SHung-ying Tyan  *
73288364387SHung-ying Tyan  * Write a block of data to the EC flash. The size must not exceed the flash
73388364387SHung-ying Tyan  * write block size which you can obtain from cros_ec_flash_write_burst_size().
73488364387SHung-ying Tyan  *
73588364387SHung-ying Tyan  * The offset starts at 0. You can obtain the region information from
73688364387SHung-ying Tyan  * cros_ec_flash_offset() to find out where to write for a particular region.
73788364387SHung-ying Tyan  *
73888364387SHung-ying Tyan  * Attempting to write to the region where the EC is currently running from
73988364387SHung-ying Tyan  * will result in an error.
74088364387SHung-ying Tyan  *
74188364387SHung-ying Tyan  * @param dev		CROS-EC device
74288364387SHung-ying Tyan  * @param data		Pointer to data buffer to write
74388364387SHung-ying Tyan  * @param offset	Offset within flash to write to.
74488364387SHung-ying Tyan  * @param size		Number of bytes to write
74588364387SHung-ying Tyan  * @return 0 if ok, -1 on error
74688364387SHung-ying Tyan  */
cros_ec_flash_write_block(struct cros_ec_dev * dev,const uint8_t * data,uint32_t offset,uint32_t size)74788364387SHung-ying Tyan static int cros_ec_flash_write_block(struct cros_ec_dev *dev,
74888364387SHung-ying Tyan 		const uint8_t *data, uint32_t offset, uint32_t size)
74988364387SHung-ying Tyan {
750bae5b97eSMoritz Fischer 	struct ec_params_flash_write *p;
751bae5b97eSMoritz Fischer 	int ret;
75288364387SHung-ying Tyan 
753bae5b97eSMoritz Fischer 	p = malloc(sizeof(*p) + size);
754bae5b97eSMoritz Fischer 	if (!p)
755bae5b97eSMoritz Fischer 		return -ENOMEM;
75688364387SHung-ying Tyan 
757bae5b97eSMoritz Fischer 	p->offset = offset;
758bae5b97eSMoritz Fischer 	p->size = size;
759bae5b97eSMoritz Fischer 	assert(data && p->size <= EC_FLASH_WRITE_VER0_SIZE);
760bae5b97eSMoritz Fischer 	memcpy(p + 1, data, p->size);
761bae5b97eSMoritz Fischer 
762bae5b97eSMoritz Fischer 	ret = ec_command_inptr(dev, EC_CMD_FLASH_WRITE, 0,
763bae5b97eSMoritz Fischer 			  p, sizeof(*p) + size, NULL, 0) >= 0 ? 0 : -1;
764bae5b97eSMoritz Fischer 
765bae5b97eSMoritz Fischer 	free(p);
766bae5b97eSMoritz Fischer 
767bae5b97eSMoritz Fischer 	return ret;
76888364387SHung-ying Tyan }
76988364387SHung-ying Tyan 
77088364387SHung-ying Tyan /**
77188364387SHung-ying Tyan  * Return optimal flash write burst size
77288364387SHung-ying Tyan  */
cros_ec_flash_write_burst_size(struct cros_ec_dev * dev)77388364387SHung-ying Tyan static int cros_ec_flash_write_burst_size(struct cros_ec_dev *dev)
77488364387SHung-ying Tyan {
775836bb6e8SSimon Glass 	return EC_FLASH_WRITE_VER0_SIZE;
77688364387SHung-ying Tyan }
77788364387SHung-ying Tyan 
77888364387SHung-ying Tyan /**
77988364387SHung-ying Tyan  * Check if a block of data is erased (all 0xff)
78088364387SHung-ying Tyan  *
78188364387SHung-ying Tyan  * This function is useful when dealing with flash, for checking whether a
78288364387SHung-ying Tyan  * data block is erased and thus does not need to be programmed.
78388364387SHung-ying Tyan  *
78488364387SHung-ying Tyan  * @param data		Pointer to data to check (must be word-aligned)
78588364387SHung-ying Tyan  * @param size		Number of bytes to check (must be word-aligned)
78688364387SHung-ying Tyan  * @return 0 if erased, non-zero if any word is not erased
78788364387SHung-ying Tyan  */
cros_ec_data_is_erased(const uint32_t * data,int size)78888364387SHung-ying Tyan static int cros_ec_data_is_erased(const uint32_t *data, int size)
78988364387SHung-ying Tyan {
79088364387SHung-ying Tyan 	assert(!(size & 3));
79188364387SHung-ying Tyan 	size /= sizeof(uint32_t);
79288364387SHung-ying Tyan 	for (; size > 0; size -= 4, data++)
79388364387SHung-ying Tyan 		if (*data != -1U)
79488364387SHung-ying Tyan 			return 0;
79588364387SHung-ying Tyan 
79688364387SHung-ying Tyan 	return 1;
79788364387SHung-ying Tyan }
79888364387SHung-ying Tyan 
799281ca88fSMoritz Fischer /**
800281ca88fSMoritz Fischer  * Read back flash parameters
801281ca88fSMoritz Fischer  *
802281ca88fSMoritz Fischer  * This function reads back parameters of the flash as reported by the EC
803281ca88fSMoritz Fischer  *
804281ca88fSMoritz Fischer  * @param dev  Pointer to device
805281ca88fSMoritz Fischer  * @param info Pointer to output flash info struct
806281ca88fSMoritz Fischer  */
cros_ec_read_flashinfo(struct cros_ec_dev * dev,struct ec_response_flash_info * info)807281ca88fSMoritz Fischer int cros_ec_read_flashinfo(struct cros_ec_dev *dev,
808281ca88fSMoritz Fischer 			  struct ec_response_flash_info *info)
809281ca88fSMoritz Fischer {
810281ca88fSMoritz Fischer 	int ret;
811281ca88fSMoritz Fischer 
812281ca88fSMoritz Fischer 	ret = ec_command(dev, EC_CMD_FLASH_INFO, 0,
813281ca88fSMoritz Fischer 			 NULL, 0, info, sizeof(*info));
814281ca88fSMoritz Fischer 	if (ret < 0)
815281ca88fSMoritz Fischer 		return ret;
816281ca88fSMoritz Fischer 
817281ca88fSMoritz Fischer 	return ret < sizeof(*info) ? -1 : 0;
818281ca88fSMoritz Fischer }
819281ca88fSMoritz Fischer 
cros_ec_flash_write(struct cros_ec_dev * dev,const uint8_t * data,uint32_t offset,uint32_t size)82088364387SHung-ying Tyan int cros_ec_flash_write(struct cros_ec_dev *dev, const uint8_t *data,
82188364387SHung-ying Tyan 		     uint32_t offset, uint32_t size)
82288364387SHung-ying Tyan {
82388364387SHung-ying Tyan 	uint32_t burst = cros_ec_flash_write_burst_size(dev);
82488364387SHung-ying Tyan 	uint32_t end, off;
82588364387SHung-ying Tyan 	int ret;
82688364387SHung-ying Tyan 
82788364387SHung-ying Tyan 	/*
82888364387SHung-ying Tyan 	 * TODO: round up to the nearest multiple of write size.  Can get away
82988364387SHung-ying Tyan 	 * without that on link right now because its write size is 4 bytes.
83088364387SHung-ying Tyan 	 */
83188364387SHung-ying Tyan 	end = offset + size;
83288364387SHung-ying Tyan 	for (off = offset; off < end; off += burst, data += burst) {
83388364387SHung-ying Tyan 		uint32_t todo;
83488364387SHung-ying Tyan 
83588364387SHung-ying Tyan 		/* If the data is empty, there is no point in programming it */
83688364387SHung-ying Tyan 		todo = min(end - off, burst);
83788364387SHung-ying Tyan 		if (dev->optimise_flash_write &&
83888364387SHung-ying Tyan 				cros_ec_data_is_erased((uint32_t *)data, todo))
83988364387SHung-ying Tyan 			continue;
84088364387SHung-ying Tyan 
84188364387SHung-ying Tyan 		ret = cros_ec_flash_write_block(dev, data, off, todo);
84288364387SHung-ying Tyan 		if (ret)
84388364387SHung-ying Tyan 			return ret;
84488364387SHung-ying Tyan 	}
84588364387SHung-ying Tyan 
84688364387SHung-ying Tyan 	return 0;
84788364387SHung-ying Tyan }
84888364387SHung-ying Tyan 
84988364387SHung-ying Tyan /**
85088364387SHung-ying Tyan  * Read a single block from the flash
85188364387SHung-ying Tyan  *
85288364387SHung-ying Tyan  * Read a block of data from the EC flash. The size must not exceed the flash
85388364387SHung-ying Tyan  * write block size which you can obtain from cros_ec_flash_write_burst_size().
85488364387SHung-ying Tyan  *
85588364387SHung-ying Tyan  * The offset starts at 0. You can obtain the region information from
85688364387SHung-ying Tyan  * cros_ec_flash_offset() to find out where to read for a particular region.
85788364387SHung-ying Tyan  *
85888364387SHung-ying Tyan  * @param dev		CROS-EC device
85988364387SHung-ying Tyan  * @param data		Pointer to data buffer to read into
86088364387SHung-ying Tyan  * @param offset	Offset within flash to read from
86188364387SHung-ying Tyan  * @param size		Number of bytes to read
86288364387SHung-ying Tyan  * @return 0 if ok, -1 on error
86388364387SHung-ying Tyan  */
cros_ec_flash_read_block(struct cros_ec_dev * dev,uint8_t * data,uint32_t offset,uint32_t size)86488364387SHung-ying Tyan static int cros_ec_flash_read_block(struct cros_ec_dev *dev, uint8_t *data,
86588364387SHung-ying Tyan 				 uint32_t offset, uint32_t size)
86688364387SHung-ying Tyan {
86788364387SHung-ying Tyan 	struct ec_params_flash_read p;
86888364387SHung-ying Tyan 
86988364387SHung-ying Tyan 	p.offset = offset;
87088364387SHung-ying Tyan 	p.size = size;
87188364387SHung-ying Tyan 
87288364387SHung-ying Tyan 	return ec_command(dev, EC_CMD_FLASH_READ, 0,
87388364387SHung-ying Tyan 			  &p, sizeof(p), data, size) >= 0 ? 0 : -1;
87488364387SHung-ying Tyan }
87588364387SHung-ying Tyan 
cros_ec_flash_read(struct cros_ec_dev * dev,uint8_t * data,uint32_t offset,uint32_t size)87688364387SHung-ying Tyan int cros_ec_flash_read(struct cros_ec_dev *dev, uint8_t *data, uint32_t offset,
87788364387SHung-ying Tyan 		    uint32_t size)
87888364387SHung-ying Tyan {
87988364387SHung-ying Tyan 	uint32_t burst = cros_ec_flash_write_burst_size(dev);
88088364387SHung-ying Tyan 	uint32_t end, off;
88188364387SHung-ying Tyan 	int ret;
88288364387SHung-ying Tyan 
88388364387SHung-ying Tyan 	end = offset + size;
88488364387SHung-ying Tyan 	for (off = offset; off < end; off += burst, data += burst) {
88588364387SHung-ying Tyan 		ret = cros_ec_flash_read_block(dev, data, off,
88688364387SHung-ying Tyan 					    min(end - off, burst));
88788364387SHung-ying Tyan 		if (ret)
88888364387SHung-ying Tyan 			return ret;
88988364387SHung-ying Tyan 	}
89088364387SHung-ying Tyan 
89188364387SHung-ying Tyan 	return 0;
89288364387SHung-ying Tyan }
89388364387SHung-ying Tyan 
cros_ec_flash_update_rw(struct cros_ec_dev * dev,const uint8_t * image,int image_size)89488364387SHung-ying Tyan int cros_ec_flash_update_rw(struct cros_ec_dev *dev,
89588364387SHung-ying Tyan 			 const uint8_t *image, int image_size)
89688364387SHung-ying Tyan {
89788364387SHung-ying Tyan 	uint32_t rw_offset, rw_size;
89888364387SHung-ying Tyan 	int ret;
89988364387SHung-ying Tyan 
90088364387SHung-ying Tyan 	if (cros_ec_flash_offset(dev, EC_FLASH_REGION_RW, &rw_offset, &rw_size))
90188364387SHung-ying Tyan 		return -1;
9022ab83f0dSSimon Glass 	if (image_size > (int)rw_size)
90388364387SHung-ying Tyan 		return -1;
90488364387SHung-ying Tyan 
90588364387SHung-ying Tyan 	/* Invalidate the existing hash, just in case the AP reboots
90688364387SHung-ying Tyan 	 * unexpectedly during the update. If that happened, the EC RW firmware
90788364387SHung-ying Tyan 	 * would be invalid, but the EC would still have the original hash.
90888364387SHung-ying Tyan 	 */
90988364387SHung-ying Tyan 	ret = cros_ec_invalidate_hash(dev);
91088364387SHung-ying Tyan 	if (ret)
91188364387SHung-ying Tyan 		return ret;
91288364387SHung-ying Tyan 
91388364387SHung-ying Tyan 	/*
91488364387SHung-ying Tyan 	 * Erase the entire RW section, so that the EC doesn't see any garbage
91588364387SHung-ying Tyan 	 * past the new image if it's smaller than the current image.
91688364387SHung-ying Tyan 	 *
91788364387SHung-ying Tyan 	 * TODO: could optimize this to erase just the current image, since
91888364387SHung-ying Tyan 	 * presumably everything past that is 0xff's.  But would still need to
91988364387SHung-ying Tyan 	 * round up to the nearest multiple of erase size.
92088364387SHung-ying Tyan 	 */
92188364387SHung-ying Tyan 	ret = cros_ec_flash_erase(dev, rw_offset, rw_size);
92288364387SHung-ying Tyan 	if (ret)
92388364387SHung-ying Tyan 		return ret;
92488364387SHung-ying Tyan 
92588364387SHung-ying Tyan 	/* Write the image */
92688364387SHung-ying Tyan 	ret = cros_ec_flash_write(dev, image, rw_offset, image_size);
92788364387SHung-ying Tyan 	if (ret)
92888364387SHung-ying Tyan 		return ret;
92988364387SHung-ying Tyan 
93088364387SHung-ying Tyan 	return 0;
93188364387SHung-ying Tyan }
93288364387SHung-ying Tyan 
cros_ec_read_vbnvcontext(struct cros_ec_dev * dev,uint8_t * block)93388364387SHung-ying Tyan int cros_ec_read_vbnvcontext(struct cros_ec_dev *dev, uint8_t *block)
93488364387SHung-ying Tyan {
93588364387SHung-ying Tyan 	struct ec_params_vbnvcontext p;
93688364387SHung-ying Tyan 	int len;
93788364387SHung-ying Tyan 
93888364387SHung-ying Tyan 	p.op = EC_VBNV_CONTEXT_OP_READ;
93988364387SHung-ying Tyan 
94088364387SHung-ying Tyan 	len = ec_command(dev, EC_CMD_VBNV_CONTEXT, EC_VER_VBNV_CONTEXT,
94188364387SHung-ying Tyan 			&p, sizeof(p), block, EC_VBNV_BLOCK_SIZE);
94288364387SHung-ying Tyan 	if (len < EC_VBNV_BLOCK_SIZE)
94388364387SHung-ying Tyan 		return -1;
94488364387SHung-ying Tyan 
94588364387SHung-ying Tyan 	return 0;
94688364387SHung-ying Tyan }
94788364387SHung-ying Tyan 
cros_ec_write_vbnvcontext(struct cros_ec_dev * dev,const uint8_t * block)94888364387SHung-ying Tyan int cros_ec_write_vbnvcontext(struct cros_ec_dev *dev, const uint8_t *block)
94988364387SHung-ying Tyan {
95088364387SHung-ying Tyan 	struct ec_params_vbnvcontext p;
95188364387SHung-ying Tyan 	int len;
95288364387SHung-ying Tyan 
95388364387SHung-ying Tyan 	p.op = EC_VBNV_CONTEXT_OP_WRITE;
95488364387SHung-ying Tyan 	memcpy(p.block, block, sizeof(p.block));
95588364387SHung-ying Tyan 
95688364387SHung-ying Tyan 	len = ec_command_inptr(dev, EC_CMD_VBNV_CONTEXT, EC_VER_VBNV_CONTEXT,
95788364387SHung-ying Tyan 			&p, sizeof(p), NULL, 0);
95888364387SHung-ying Tyan 	if (len < 0)
95988364387SHung-ying Tyan 		return -1;
96088364387SHung-ying Tyan 
96188364387SHung-ying Tyan 	return 0;
96288364387SHung-ying Tyan }
96388364387SHung-ying Tyan 
cros_ec_set_ldo(struct udevice * dev,uint8_t index,uint8_t state)964f48eaf01SSimon Glass int cros_ec_set_ldo(struct udevice *dev, uint8_t index, uint8_t state)
96588364387SHung-ying Tyan {
966f48eaf01SSimon Glass 	struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
96788364387SHung-ying Tyan 	struct ec_params_ldo_set params;
96888364387SHung-ying Tyan 
96988364387SHung-ying Tyan 	params.index = index;
97088364387SHung-ying Tyan 	params.state = state;
97188364387SHung-ying Tyan 
972f48eaf01SSimon Glass 	if (ec_command_inptr(cdev, EC_CMD_LDO_SET, 0, &params, sizeof(params),
97388364387SHung-ying Tyan 			     NULL, 0))
97488364387SHung-ying Tyan 		return -1;
97588364387SHung-ying Tyan 
97688364387SHung-ying Tyan 	return 0;
97788364387SHung-ying Tyan }
97888364387SHung-ying Tyan 
cros_ec_get_ldo(struct udevice * dev,uint8_t index,uint8_t * state)979f48eaf01SSimon Glass int cros_ec_get_ldo(struct udevice *dev, uint8_t index, uint8_t *state)
98088364387SHung-ying Tyan {
981f48eaf01SSimon Glass 	struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
98288364387SHung-ying Tyan 	struct ec_params_ldo_get params;
98388364387SHung-ying Tyan 	struct ec_response_ldo_get *resp;
98488364387SHung-ying Tyan 
98588364387SHung-ying Tyan 	params.index = index;
98688364387SHung-ying Tyan 
987f48eaf01SSimon Glass 	if (ec_command_inptr(cdev, EC_CMD_LDO_GET, 0, &params, sizeof(params),
988f48eaf01SSimon Glass 			     (uint8_t **)&resp, sizeof(*resp)) !=
989f48eaf01SSimon Glass 			     sizeof(*resp))
99088364387SHung-ying Tyan 		return -1;
99188364387SHung-ying Tyan 
99288364387SHung-ying Tyan 	*state = resp->state;
99388364387SHung-ying Tyan 
99488364387SHung-ying Tyan 	return 0;
99588364387SHung-ying Tyan }
99688364387SHung-ying Tyan 
cros_ec_register(struct udevice * dev)99784d6cbd3SSimon Glass int cros_ec_register(struct udevice *dev)
99884d6cbd3SSimon Glass {
999e564f054SSimon Glass 	struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
100084d6cbd3SSimon Glass 	char id[MSG_BYTES];
100184d6cbd3SSimon Glass 
100284d6cbd3SSimon Glass 	cdev->dev = dev;
100332f8a19fSSimon Glass 	gpio_request_by_name(dev, "ec-interrupt", 0, &cdev->ec_int,
100432f8a19fSSimon Glass 			     GPIOD_IS_IN);
10052ec9d171SSimon Glass 	cdev->optimise_flash_write = dev_read_bool(dev, "optimise-flash-write");
100684d6cbd3SSimon Glass 
100784d6cbd3SSimon Glass 	if (cros_ec_check_version(cdev)) {
100884d6cbd3SSimon Glass 		debug("%s: Could not detect CROS-EC version\n", __func__);
100984d6cbd3SSimon Glass 		return -CROS_EC_ERR_CHECK_VERSION;
101084d6cbd3SSimon Glass 	}
101184d6cbd3SSimon Glass 
101284d6cbd3SSimon Glass 	if (cros_ec_read_id(cdev, id, sizeof(id))) {
101384d6cbd3SSimon Glass 		debug("%s: Could not read KBC ID\n", __func__);
101484d6cbd3SSimon Glass 		return -CROS_EC_ERR_READ_ID;
101584d6cbd3SSimon Glass 	}
101684d6cbd3SSimon Glass 
101784d6cbd3SSimon Glass 	/* Remember this device for use by the cros_ec command */
1018c4b206dfSSimon Glass 	debug("Google Chrome EC v%d CROS-EC driver ready, id '%s'\n",
1019c4b206dfSSimon Glass 	      cdev->protocol_version, id);
102084d6cbd3SSimon Glass 
102184d6cbd3SSimon Glass 	return 0;
102284d6cbd3SSimon Glass }
102388364387SHung-ying Tyan 
cros_ec_decode_ec_flash(struct udevice * dev,struct fdt_cros_ec * config)10242ec9d171SSimon Glass int cros_ec_decode_ec_flash(struct udevice *dev, struct fdt_cros_ec *config)
1025d7f25f35SSimon Glass {
10262ec9d171SSimon Glass 	ofnode flash_node, node;
1027d7f25f35SSimon Glass 
10282ec9d171SSimon Glass 	flash_node = dev_read_subnode(dev, "flash");
10292ec9d171SSimon Glass 	if (!ofnode_valid(flash_node)) {
1030d7f25f35SSimon Glass 		debug("Failed to find flash node\n");
1031d7f25f35SSimon Glass 		return -1;
1032d7f25f35SSimon Glass 	}
1033d7f25f35SSimon Glass 
10342ec9d171SSimon Glass 	if (of_read_fmap_entry(flash_node, "flash", &config->flash)) {
10352ec9d171SSimon Glass 		debug("Failed to decode flash node in chrome-ec\n");
1036d7f25f35SSimon Glass 		return -1;
1037d7f25f35SSimon Glass 	}
1038d7f25f35SSimon Glass 
10392ec9d171SSimon Glass 	config->flash_erase_value = ofnode_read_s32_default(flash_node,
1040d7f25f35SSimon Glass 							    "erase-value", -1);
1041*17c82fdcSSimon Glass 	ofnode_for_each_subnode(node, flash_node) {
10422ec9d171SSimon Glass 		const char *name = ofnode_get_name(node);
1043d7f25f35SSimon Glass 		enum ec_flash_region region;
1044d7f25f35SSimon Glass 
1045d7f25f35SSimon Glass 		if (0 == strcmp(name, "ro")) {
1046d7f25f35SSimon Glass 			region = EC_FLASH_REGION_RO;
1047d7f25f35SSimon Glass 		} else if (0 == strcmp(name, "rw")) {
1048d7f25f35SSimon Glass 			region = EC_FLASH_REGION_RW;
1049d7f25f35SSimon Glass 		} else if (0 == strcmp(name, "wp-ro")) {
1050d7f25f35SSimon Glass 			region = EC_FLASH_REGION_WP_RO;
1051d7f25f35SSimon Glass 		} else {
1052d7f25f35SSimon Glass 			debug("Unknown EC flash region name '%s'\n", name);
1053d7f25f35SSimon Glass 			return -1;
1054d7f25f35SSimon Glass 		}
1055d7f25f35SSimon Glass 
10562ec9d171SSimon Glass 		if (of_read_fmap_entry(node, "reg", &config->region[region])) {
1057d7f25f35SSimon Glass 			debug("Failed to decode flash region in chrome-ec'\n");
1058d7f25f35SSimon Glass 			return -1;
1059d7f25f35SSimon Glass 		}
1060d7f25f35SSimon Glass 	}
1061d7f25f35SSimon Glass 
1062d7f25f35SSimon Glass 	return 0;
1063d7f25f35SSimon Glass }
1064d7f25f35SSimon Glass 
cros_ec_i2c_tunnel(struct udevice * dev,int port,struct i2c_msg * in,int nmsgs)10656d1a718fSMoritz Fischer int cros_ec_i2c_tunnel(struct udevice *dev, int port, struct i2c_msg *in,
10666d1a718fSMoritz Fischer 		       int nmsgs)
1067cc456bd7SSimon Glass {
1068cc456bd7SSimon Glass 	struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
1069cc456bd7SSimon Glass 	union {
1070cc456bd7SSimon Glass 		struct ec_params_i2c_passthru p;
1071cc456bd7SSimon Glass 		uint8_t outbuf[EC_PROTO2_MAX_PARAM_SIZE];
1072cc456bd7SSimon Glass 	} params;
1073cc456bd7SSimon Glass 	union {
1074cc456bd7SSimon Glass 		struct ec_response_i2c_passthru r;
1075cc456bd7SSimon Glass 		uint8_t inbuf[EC_PROTO2_MAX_PARAM_SIZE];
1076cc456bd7SSimon Glass 	} response;
1077cc456bd7SSimon Glass 	struct ec_params_i2c_passthru *p = &params.p;
1078cc456bd7SSimon Glass 	struct ec_response_i2c_passthru *r = &response.r;
1079cc456bd7SSimon Glass 	struct ec_params_i2c_passthru_msg *msg;
1080cc456bd7SSimon Glass 	uint8_t *pdata, *read_ptr = NULL;
1081cc456bd7SSimon Glass 	int read_len;
1082cc456bd7SSimon Glass 	int size;
1083cc456bd7SSimon Glass 	int rv;
1084cc456bd7SSimon Glass 	int i;
1085cc456bd7SSimon Glass 
10866d1a718fSMoritz Fischer 	p->port = port;
1087cc456bd7SSimon Glass 
1088cc456bd7SSimon Glass 	p->num_msgs = nmsgs;
1089cc456bd7SSimon Glass 	size = sizeof(*p) + p->num_msgs * sizeof(*msg);
1090cc456bd7SSimon Glass 
1091cc456bd7SSimon Glass 	/* Create a message to write the register address and optional data */
1092cc456bd7SSimon Glass 	pdata = (uint8_t *)p + size;
1093cc456bd7SSimon Glass 
1094cc456bd7SSimon Glass 	read_len = 0;
1095cc456bd7SSimon Glass 	for (i = 0, msg = p->msg; i < nmsgs; i++, msg++, in++) {
1096cc456bd7SSimon Glass 		bool is_read = in->flags & I2C_M_RD;
1097cc456bd7SSimon Glass 
1098cc456bd7SSimon Glass 		msg->addr_flags = in->addr;
1099cc456bd7SSimon Glass 		msg->len = in->len;
1100cc456bd7SSimon Glass 		if (is_read) {
1101cc456bd7SSimon Glass 			msg->addr_flags |= EC_I2C_FLAG_READ;
1102cc456bd7SSimon Glass 			read_len += in->len;
1103cc456bd7SSimon Glass 			read_ptr = in->buf;
1104cc456bd7SSimon Glass 			if (sizeof(*r) + read_len > sizeof(response)) {
1105cc456bd7SSimon Glass 				puts("Read length too big for buffer\n");
1106cc456bd7SSimon Glass 				return -1;
1107cc456bd7SSimon Glass 			}
1108cc456bd7SSimon Glass 		} else {
1109cc456bd7SSimon Glass 			if (pdata - (uint8_t *)p + in->len > sizeof(params)) {
1110cc456bd7SSimon Glass 				puts("Params too large for buffer\n");
1111cc456bd7SSimon Glass 				return -1;
1112cc456bd7SSimon Glass 			}
1113cc456bd7SSimon Glass 			memcpy(pdata, in->buf, in->len);
1114cc456bd7SSimon Glass 			pdata += in->len;
1115cc456bd7SSimon Glass 		}
1116cc456bd7SSimon Glass 	}
1117cc456bd7SSimon Glass 
1118cc456bd7SSimon Glass 	rv = ec_command(cdev, EC_CMD_I2C_PASSTHRU, 0, p, pdata - (uint8_t *)p,
1119cc456bd7SSimon Glass 			r, sizeof(*r) + read_len);
1120cc456bd7SSimon Glass 	if (rv < 0)
1121cc456bd7SSimon Glass 		return rv;
1122cc456bd7SSimon Glass 
1123cc456bd7SSimon Glass 	/* Parse response */
1124cc456bd7SSimon Glass 	if (r->i2c_status & EC_I2C_STATUS_ERROR) {
1125cc456bd7SSimon Glass 		printf("Transfer failed with status=0x%x\n", r->i2c_status);
1126cc456bd7SSimon Glass 		return -1;
1127cc456bd7SSimon Glass 	}
1128cc456bd7SSimon Glass 
1129cc456bd7SSimon Glass 	if (rv < sizeof(*r) + read_len) {
1130cc456bd7SSimon Glass 		puts("Truncated read response\n");
1131cc456bd7SSimon Glass 		return -1;
1132cc456bd7SSimon Glass 	}
1133cc456bd7SSimon Glass 
1134cc456bd7SSimon Glass 	/* We only support a single read message for each transfer */
1135cc456bd7SSimon Glass 	if (read_len)
1136cc456bd7SSimon Glass 		memcpy(read_ptr, r->data, read_len);
1137cc456bd7SSimon Glass 
1138cc456bd7SSimon Glass 	return 0;
1139cc456bd7SSimon Glass }
1140cc456bd7SSimon Glass 
114184d6cbd3SSimon Glass UCLASS_DRIVER(cros_ec) = {
114284d6cbd3SSimon Glass 	.id		= UCLASS_CROS_EC,
114384d6cbd3SSimon Glass 	.name		= "cros_ec",
114484d6cbd3SSimon Glass 	.per_device_auto_alloc_size = sizeof(struct cros_ec_dev),
114591195485SSimon Glass 	.post_bind	= dm_scan_fdt_dev,
114684d6cbd3SSimon Glass };
1147