2 * Chromium OS cros_ec driver - SPI interface
4 * Copyright (c) 2012 The Chromium OS Authors.
6 * SPDX-License-Identifier: GPL-2.0+
10 * The Matrix Keyboard Protocol driver handles talking to the keyboard
11 * controller chip. Mostly this is for keyboard functions, but some other
12 * things have slipped in, so we provide generic services to talk to the
21 * Send a command to a LPC CROS_EC device and return the reply.
23 * The device's internal input/output buffers are used.
25 * @param dev CROS_EC device
26 * @param cmd Command to send (EC_CMD_...)
27 * @param cmd_version Version of command to send (EC_VER_...)
28 * @param dout Output data (may be NULL If dout_len=0)
29 * @param dout_len Size of output data in bytes
30 * @param dinp Returns pointer to response data. This will be
31 * untouched unless we return a value > 0.
32 * @param din_len Maximum size of response in bytes
33 * @return number of bytes in response, or -1 on error
35 int cros_ec_spi_command(struct cros_ec_dev *dev, uint8_t cmd, int cmd_version,
36 const uint8_t *dout, int dout_len,
37 uint8_t **dinp, int din_len)
39 int in_bytes = din_len + 4; /* status, length, checksum, trailer */
45 if (dev->protocol_version != 2) {
46 debug("%s: Unsupported EC protcol version %d\n",
47 __func__, dev->protocol_version);
52 * Sanity-check input size to make sure it plus transaction overhead
53 * fits in the internal device buffer.
55 if (in_bytes > sizeof(dev->din)) {
56 debug("%s: Cannot receive %d bytes\n", __func__, din_len);
60 /* We represent message length as a byte */
61 if (dout_len > 0xff) {
62 debug("%s: Cannot send %d bytes\n", __func__, dout_len);
67 * Clear input buffer so we don't get false hits for MSG_HEADER
69 memset(dev->din, '\0', in_bytes);
71 if (spi_claim_bus(dev->spi)) {
72 debug("%s: Cannot claim SPI bus\n", __func__);
79 out[2] = (uint8_t)dout_len;
80 memcpy(out + 3, dout, dout_len);
81 csum = cros_ec_calc_checksum(out, 3)
82 + cros_ec_calc_checksum(dout, dout_len);
83 out[3 + dout_len] = (uint8_t)csum;
86 * Send output data and receive input data starting such that the
87 * message body will be dword aligned.
89 p = dev->din + sizeof(int64_t) - 2;
91 cros_ec_dump_data("out", cmd, out, len);
92 rv = spi_xfer(dev->spi, max(len, in_bytes) * 8, out, p,
93 SPI_XFER_BEGIN | SPI_XFER_END);
95 spi_release_bus(dev->spi);
98 debug("%s: Cannot complete SPI transfer\n", __func__);
102 len = min(p[1], din_len);
103 cros_ec_dump_data("in", -1, p, len + 3);
105 /* Response code is first byte of message */
106 if (p[0] != EC_RES_SUCCESS) {
107 printf("%s: Returned status %d\n", __func__, p[0]);
112 csum = cros_ec_calc_checksum(p, len + 2);
113 if (csum != p[len + 2]) {
114 debug("%s: Invalid checksum rx %#02x, calced %#02x\n", __func__,
119 /* Anything else is the response data */
125 int cros_ec_spi_decode_fdt(struct cros_ec_dev *dev, const void *blob)
127 /* Decode interface-specific FDT params */
128 dev->max_frequency = fdtdec_get_int(blob, dev->node,
129 "spi-max-frequency", 500000);
130 dev->cs = fdtdec_get_int(blob, dev->node, "reg", 0);
136 * Initialize SPI protocol.
138 * @param dev CROS_EC device
139 * @param blob Device tree blob
140 * @return 0 if ok, -1 on error
142 int cros_ec_spi_init(struct cros_ec_dev *dev, const void *blob)
144 dev->spi = spi_setup_slave_fdt(blob, dev->parent_node, dev->node);
146 debug("%s: Could not setup SPI slave\n", __func__);