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
20 int cros_ec_spi_packet(struct cros_ec_dev *dev, int out_bytes, int in_bytes)
25 if (spi_claim_bus(dev->spi)) {
26 debug("%s: Cannot claim SPI bus\n", __func__);
30 rv = spi_xfer(dev->spi, max(out_bytes, in_bytes) * 8,
32 SPI_XFER_BEGIN | SPI_XFER_END);
34 spi_release_bus(dev->spi);
37 debug("%s: Cannot complete SPI transfer\n", __func__);
45 * Send a command to a LPC CROS_EC device and return the reply.
47 * The device's internal input/output buffers are used.
49 * @param dev CROS_EC device
50 * @param cmd Command to send (EC_CMD_...)
51 * @param cmd_version Version of command to send (EC_VER_...)
52 * @param dout Output data (may be NULL If dout_len=0)
53 * @param dout_len Size of output data in bytes
54 * @param dinp Returns pointer to response data. This will be
55 * untouched unless we return a value > 0.
56 * @param din_len Maximum size of response in bytes
57 * @return number of bytes in response, or -1 on error
59 int cros_ec_spi_command(struct cros_ec_dev *dev, uint8_t cmd, int cmd_version,
60 const uint8_t *dout, int dout_len,
61 uint8_t **dinp, int din_len)
63 int in_bytes = din_len + 4; /* status, length, checksum, trailer */
69 if (dev->protocol_version != 2) {
70 debug("%s: Unsupported EC protcol version %d\n",
71 __func__, dev->protocol_version);
76 * Sanity-check input size to make sure it plus transaction overhead
77 * fits in the internal device buffer.
79 if (in_bytes > sizeof(dev->din)) {
80 debug("%s: Cannot receive %d bytes\n", __func__, din_len);
84 /* We represent message length as a byte */
85 if (dout_len > 0xff) {
86 debug("%s: Cannot send %d bytes\n", __func__, dout_len);
91 * Clear input buffer so we don't get false hits for MSG_HEADER
93 memset(dev->din, '\0', in_bytes);
95 if (spi_claim_bus(dev->spi)) {
96 debug("%s: Cannot claim SPI bus\n", __func__);
101 out[0] = EC_CMD_VERSION0 + cmd_version;
103 out[2] = (uint8_t)dout_len;
104 memcpy(out + 3, dout, dout_len);
105 csum = cros_ec_calc_checksum(out, 3)
106 + cros_ec_calc_checksum(dout, dout_len);
107 out[3 + dout_len] = (uint8_t)csum;
110 * Send output data and receive input data starting such that the
111 * message body will be dword aligned.
113 p = dev->din + sizeof(int64_t) - 2;
115 cros_ec_dump_data("out", cmd, out, len);
116 rv = spi_xfer(dev->spi, max(len, in_bytes) * 8, out, p,
117 SPI_XFER_BEGIN | SPI_XFER_END);
119 spi_release_bus(dev->spi);
122 debug("%s: Cannot complete SPI transfer\n", __func__);
126 len = min(p[1], din_len);
127 cros_ec_dump_data("in", -1, p, len + 3);
129 /* Response code is first byte of message */
130 if (p[0] != EC_RES_SUCCESS) {
131 printf("%s: Returned status %d\n", __func__, p[0]);
136 csum = cros_ec_calc_checksum(p, len + 2);
137 if (csum != p[len + 2]) {
138 debug("%s: Invalid checksum rx %#02x, calced %#02x\n", __func__,
143 /* Anything else is the response data */
149 int cros_ec_spi_decode_fdt(struct cros_ec_dev *dev, const void *blob)
151 /* Decode interface-specific FDT params */
152 dev->max_frequency = fdtdec_get_int(blob, dev->node,
153 "spi-max-frequency", 500000);
154 dev->cs = fdtdec_get_int(blob, dev->node, "reg", 0);
160 * Initialize SPI protocol.
162 * @param dev CROS_EC device
163 * @param blob Device tree blob
164 * @return 0 if ok, -1 on error
166 int cros_ec_spi_init(struct cros_ec_dev *dev, const void *blob)
168 dev->spi = spi_setup_slave_fdt(blob, dev->node, dev->parent_node);
170 debug("%s: Could not setup SPI slave\n", __func__);