blob: 015333f139a209fd686a4ac173aba02baeeb1b33 [file] [log] [blame]
Hung-ying Tyan00391232013-05-15 18:27:30 +08001/*
2 * Chromium OS cros_ec driver - SPI interface
3 *
4 * Copyright (c) 2012 The Chromium OS Authors.
Hung-ying Tyan00391232013-05-15 18:27:30 +08005 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02006 * SPDX-License-Identifier: GPL-2.0+
Hung-ying Tyan00391232013-05-15 18:27:30 +08007 */
8
9/*
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
13 * KBC.
14 */
15
16#include <common.h>
17#include <cros_ec.h>
18#include <spi.h>
19
Randall Spanglerce3ebb92014-02-27 13:26:10 -070020int cros_ec_spi_packet(struct cros_ec_dev *dev, int out_bytes, int in_bytes)
21{
22 int rv;
23
24 /* Do the transfer */
25 if (spi_claim_bus(dev->spi)) {
26 debug("%s: Cannot claim SPI bus\n", __func__);
27 return -1;
28 }
29
30 rv = spi_xfer(dev->spi, max(out_bytes, in_bytes) * 8,
31 dev->dout, dev->din,
32 SPI_XFER_BEGIN | SPI_XFER_END);
33
34 spi_release_bus(dev->spi);
35
36 if (rv) {
37 debug("%s: Cannot complete SPI transfer\n", __func__);
38 return -1;
39 }
40
41 return in_bytes;
42}
43
Hung-ying Tyan00391232013-05-15 18:27:30 +080044/**
45 * Send a command to a LPC CROS_EC device and return the reply.
46 *
47 * The device's internal input/output buffers are used.
48 *
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
58 */
59int 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)
62{
63 int in_bytes = din_len + 4; /* status, length, checksum, trailer */
64 uint8_t *out;
65 uint8_t *p;
66 int csum, len;
67 int rv;
68
Randall Spanglerf03e9702014-02-27 13:26:08 -070069 if (dev->protocol_version != 2) {
70 debug("%s: Unsupported EC protcol version %d\n",
71 __func__, dev->protocol_version);
72 return -1;
73 }
74
Hung-ying Tyan00391232013-05-15 18:27:30 +080075 /*
76 * Sanity-check input size to make sure it plus transaction overhead
77 * fits in the internal device buffer.
78 */
79 if (in_bytes > sizeof(dev->din)) {
80 debug("%s: Cannot receive %d bytes\n", __func__, din_len);
81 return -1;
82 }
83
84 /* We represent message length as a byte */
85 if (dout_len > 0xff) {
86 debug("%s: Cannot send %d bytes\n", __func__, dout_len);
87 return -1;
88 }
89
90 /*
91 * Clear input buffer so we don't get false hits for MSG_HEADER
92 */
93 memset(dev->din, '\0', in_bytes);
94
95 if (spi_claim_bus(dev->spi)) {
96 debug("%s: Cannot claim SPI bus\n", __func__);
97 return -1;
98 }
99
100 out = dev->dout;
Simon Glassc2a700f2014-07-07 10:16:37 -0600101 out[0] = EC_CMD_VERSION0 + cmd_version;
Hung-ying Tyan00391232013-05-15 18:27:30 +0800102 out[1] = cmd;
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;
108
109 /*
110 * Send output data and receive input data starting such that the
111 * message body will be dword aligned.
112 */
113 p = dev->din + sizeof(int64_t) - 2;
114 len = dout_len + 4;
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);
118
119 spi_release_bus(dev->spi);
120
121 if (rv) {
122 debug("%s: Cannot complete SPI transfer\n", __func__);
123 return -1;
124 }
125
126 len = min(p[1], din_len);
127 cros_ec_dump_data("in", -1, p, len + 3);
128
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]);
132 return -(int)(p[0]);
133 }
134
135 /* Check checksum */
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__,
139 p[2 + len], csum);
140 return -1;
141 }
142
143 /* Anything else is the response data */
144 *dinp = p + 2;
145
146 return len;
147}
148
149int cros_ec_spi_decode_fdt(struct cros_ec_dev *dev, const void *blob)
150{
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);
155
156 return 0;
157}
158
159/**
160 * Initialize SPI protocol.
161 *
162 * @param dev CROS_EC device
163 * @param blob Device tree blob
164 * @return 0 if ok, -1 on error
165 */
166int cros_ec_spi_init(struct cros_ec_dev *dev, const void *blob)
167{
Simon Glassc2a700f2014-07-07 10:16:37 -0600168 dev->spi = spi_setup_slave_fdt(blob, dev->node, dev->parent_node);
Hung-ying Tyan00391232013-05-15 18:27:30 +0800169 if (!dev->spi) {
170 debug("%s: Could not setup SPI slave\n", __func__);
171 return -1;
172 }
173
174 return 0;
175}