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 */
46 * Sanity-check input size to make sure it plus transaction overhead
47 * fits in the internal device buffer.
49 if (in_bytes > sizeof(dev->din)) {
50 debug("%s: Cannot receive %d bytes\n", __func__, din_len);
54 /* We represent message length as a byte */
55 if (dout_len > 0xff) {
56 debug("%s: Cannot send %d bytes\n", __func__, dout_len);
61 * Clear input buffer so we don't get false hits for MSG_HEADER
63 memset(dev->din, '\0', in_bytes);
65 if (spi_claim_bus(dev->spi)) {
66 debug("%s: Cannot claim SPI bus\n", __func__);
73 out[2] = (uint8_t)dout_len;
74 memcpy(out + 3, dout, dout_len);
75 csum = cros_ec_calc_checksum(out, 3)
76 + cros_ec_calc_checksum(dout, dout_len);
77 out[3 + dout_len] = (uint8_t)csum;
80 * Send output data and receive input data starting such that the
81 * message body will be dword aligned.
83 p = dev->din + sizeof(int64_t) - 2;
85 cros_ec_dump_data("out", cmd, out, len);
86 rv = spi_xfer(dev->spi, max(len, in_bytes) * 8, out, p,
87 SPI_XFER_BEGIN | SPI_XFER_END);
89 spi_release_bus(dev->spi);
92 debug("%s: Cannot complete SPI transfer\n", __func__);
96 len = min(p[1], din_len);
97 cros_ec_dump_data("in", -1, p, len + 3);
99 /* Response code is first byte of message */
100 if (p[0] != EC_RES_SUCCESS) {
101 printf("%s: Returned status %d\n", __func__, p[0]);
106 csum = cros_ec_calc_checksum(p, len + 2);
107 if (csum != p[len + 2]) {
108 debug("%s: Invalid checksum rx %#02x, calced %#02x\n", __func__,
113 /* Anything else is the response data */
119 int cros_ec_spi_decode_fdt(struct cros_ec_dev *dev, const void *blob)
121 /* Decode interface-specific FDT params */
122 dev->max_frequency = fdtdec_get_int(blob, dev->node,
123 "spi-max-frequency", 500000);
124 dev->cs = fdtdec_get_int(blob, dev->node, "reg", 0);
130 * Initialize SPI protocol.
132 * @param dev CROS_EC device
133 * @param blob Device tree blob
134 * @return 0 if ok, -1 on error
136 int cros_ec_spi_init(struct cros_ec_dev *dev, const void *blob)
138 dev->spi = spi_setup_slave_fdt(blob, dev->parent_node,
139 dev->cs, dev->max_frequency, 0);
141 debug("%s: Could not setup SPI slave\n", __func__);