blob: a63cfada5d5c257375db0ec00f5b4ebf94afd01f [file] [log] [blame]
Hung-Te Lin23fb9972013-06-21 20:11:47 +08001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2013 Google Inc. All rights reserved.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
Hung-Te Lin23fb9972013-06-21 20:11:47 +080014 */
15
16#include <arch/io.h>
17#include <console/console.h>
18#include <delay.h>
19#include <stdint.h>
20#include <string.h>
21#include "ec.h"
22#include "ec_commands.h"
23#include "ec_message.h"
24
Hung-Te Line946f982013-06-22 11:18:39 +080025/* Common utilities */
Aaron Durbin82827272014-08-06 14:34:57 -050026void * __attribute__((weak)) crosec_get_buffer(size_t size, int req)
27{
28 printk(BIOS_DEBUG, "crosec_get_buffer() implementation required.\n");
29 return NULL;
30}
Hung-Te Line946f982013-06-22 11:18:39 +080031
32/* Dumps EC command / response data into debug output.
33 *
34 * @param name Message prefix name.
35 * @param cmd Command code, or -1 to ignore cmd message.
36 * @param data Data buffer to print.
37 * @param len Length of data.
38 */
39static void cros_ec_dump_data(const char *name, int cmd, const uint8_t *data,
40 int len)
41{
42 int i;
43
44 printk(BIOS_DEBUG, "%s: ", name);
45 if (cmd != -1)
46 printk(BIOS_DEBUG, "cmd=%#x: ", cmd);
47 for (i = 0; i < len; i++)
48 printk(BIOS_DEBUG, "%02x ", data[i]);
49 printk(BIOS_DEBUG, "\n");
50}
51
52/* Calculate a simple 8-bit checksum of a data block
53 *
54 * @param data Data block to checksum
55 * @param size Size of data block in bytes
56 * @return checksum value (0 to 255)
57 */
58static int cros_ec_calc_checksum(const uint8_t *data, int size)
59{
60 int csum, i;
61
62 for (i = csum = 0; i < size; i++)
63 csum += data[i];
64 return csum & 0xff;
65}
66
67/* Standard Chrome EC protocol, version 3 */
68
69struct ec_command_v3 {
70 struct ec_host_request header;
71 uint8_t data[MSG_BYTES];
72};
73
74struct ec_response_v3 {
75 struct ec_host_response header;
76 uint8_t data[MSG_BYTES];
77};
78
79/**
80 * Create a request packet for protocol version 3.
81 *
82 * @param cec_command Command description.
83 * @param cmd Packed command bit stream.
84 * @return packet size in bytes, or <0 if error.
85 */
86static int create_proto3_request(const struct chromeec_command *cec_command,
87 struct ec_command_v3 *cmd)
88{
89 struct ec_host_request *rq = &cmd->header;
90 int out_bytes = cec_command->cmd_size_in + sizeof(*rq);
91
92 /* Fail if output size is too big */
93 if (out_bytes > sizeof(*cmd)) {
94 printk(BIOS_ERR, "%s: Cannot send %d bytes\n", __func__,
95 cec_command->cmd_size_in);
96 return -EC_RES_REQUEST_TRUNCATED;
97 }
98
99 /* Fill in request packet */
100 rq->struct_version = EC_HOST_REQUEST_VERSION;
101 rq->checksum = 0;
102 rq->command = cec_command->cmd_code;
103 rq->command_version = cec_command->cmd_version;
104 rq->reserved = 0;
105 rq->data_len = cec_command->cmd_size_in;
106
107 /* Copy data after header */
108 memcpy(cmd->data, cec_command->cmd_data_in, cec_command->cmd_size_in);
109
110 /* Write checksum field so the entire packet sums to 0 */
111 rq->checksum = (uint8_t)(-cros_ec_calc_checksum(
112 (const uint8_t*)cmd, out_bytes));
113
114 cros_ec_dump_data("out", rq->command, (const uint8_t *)cmd, out_bytes);
115
116 /* Return size of request packet */
117 return out_bytes;
118}
119
120/**
121 * Prepare the device to receive a protocol version 3 response.
122 *
123 * @param cec_command Command description.
124 * @param resp Response buffer.
125 * @return maximum expected number of bytes in response, or <0 if error.
126 */
127static int prepare_proto3_response_buffer(
128 const struct chromeec_command *cec_command,
129 struct ec_response_v3 *resp)
130{
131 int in_bytes = cec_command->cmd_size_out + sizeof(resp->header);
132
133 /* Fail if input size is too big */
134 if (in_bytes > sizeof(*resp)) {
135 printk(BIOS_ERR, "%s: Cannot receive %d bytes\n", __func__,
136 cec_command->cmd_size_out);
137 return -EC_RES_RESPONSE_TOO_BIG;
138 }
139
140 /* Return expected size of response packet */
141 return in_bytes;
142}
143
144/**
145 * Handle a protocol version 3 response packet.
146 *
147 * The packet must already be stored in the response buffer.
148 *
149 * @param resp Response buffer.
150 * @param cec_command Command structure to receive valid response.
151 * @return number of bytes of response data, or <0 if error
152 */
153static int handle_proto3_response(struct ec_response_v3 *resp,
154 struct chromeec_command *cec_command)
155{
156 struct ec_host_response *rs = &resp->header;
157 int in_bytes;
158 int csum;
159
160 cros_ec_dump_data("in-header", -1, (const uint8_t*)rs, sizeof(*rs));
161
162 /* Check input data */
163 if (rs->struct_version != EC_HOST_RESPONSE_VERSION) {
164 printk(BIOS_ERR, "%s: EC response version mismatch\n", __func__);
165 return -EC_RES_INVALID_RESPONSE;
166 }
167
168 if (rs->reserved) {
169 printk(BIOS_ERR, "%s: EC response reserved != 0\n", __func__);
170 return -EC_RES_INVALID_RESPONSE;
171 }
172
173 if (rs->data_len > sizeof(resp->data) ||
174 rs->data_len > cec_command->cmd_size_out) {
175 printk(BIOS_ERR, "%s: EC returned too much data\n", __func__);
176 return -EC_RES_RESPONSE_TOO_BIG;
177 }
178
179 cros_ec_dump_data("in-data", -1, resp->data, rs->data_len);
180
181 /* Update in_bytes to actual data size */
182 in_bytes = sizeof(*rs) + rs->data_len;
183
184 /* Verify checksum */
185 csum = cros_ec_calc_checksum((const uint8_t *)resp, in_bytes);
186 if (csum) {
187 printk(BIOS_ERR, "%s: EC response checksum invalid: 0x%02x\n",
188 __func__, csum);
189 return -EC_RES_INVALID_CHECKSUM;
190 }
191
192 /* Return raw response. */
193 cec_command->cmd_code = rs->result;
194 cec_command->cmd_size_out = rs->data_len;
195 memcpy(cec_command->cmd_data_out, resp->data, rs->data_len);
196
197 /* Return error result, if any */
198 if (rs->result) {
199 printk(BIOS_ERR, "%s: EC response with error code: %d\n",
200 __func__, rs->result);
201 return -(int)rs->result;
202 }
203
204 return rs->data_len;
205}
206
207static int send_command_proto3(struct chromeec_command *cec_command,
208 crosec_io_t crosec_io, void *context)
209{
210 int out_bytes, in_bytes;
211 int rv;
Aaron Durbin82827272014-08-06 14:34:57 -0500212 struct ec_command_v3 *cmd;
213 struct ec_response_v3 *resp;
214
215 if ((cmd = crosec_get_buffer(sizeof(*cmd), 1)) == NULL)
216 return -EC_RES_ERROR;
217 if ((resp = crosec_get_buffer(sizeof(*resp), 0)) == NULL)
218 return -EC_RES_ERROR;
Hung-Te Line946f982013-06-22 11:18:39 +0800219
220 /* Create request packet */
Aaron Durbin82827272014-08-06 14:34:57 -0500221 out_bytes = create_proto3_request(cec_command, cmd);
Hung-Te Line946f982013-06-22 11:18:39 +0800222 if (out_bytes < 0) {
223 return out_bytes;
224 }
225
226 /* Prepare response buffer */
Aaron Durbin82827272014-08-06 14:34:57 -0500227 in_bytes = prepare_proto3_response_buffer(cec_command, resp);
Hung-Te Line946f982013-06-22 11:18:39 +0800228 if (in_bytes < 0) {
229 return in_bytes;
230 }
231
Aaron Durbin82827272014-08-06 14:34:57 -0500232 rv = crosec_io(out_bytes, in_bytes, context);
Hung-Te Line946f982013-06-22 11:18:39 +0800233 if (rv != 0) {
Julius Werner02e847b2015-02-20 13:36:11 -0800234 printk(BIOS_ERR, "%s: failed to complete I/O: Err = %#x.\n",
Hung-Te Line946f982013-06-22 11:18:39 +0800235 __func__, rv >= 0 ? rv : -rv);
236 return -EC_RES_ERROR;
237 }
238
239 /* Process the response */
Aaron Durbin82827272014-08-06 14:34:57 -0500240 return handle_proto3_response(resp, cec_command);
Hung-Te Line946f982013-06-22 11:18:39 +0800241}
242
243static int crosec_command_proto_v3(struct chromeec_command *cec_command,
244 crosec_io_t crosec_io, void *context)
245{
246 int rv = send_command_proto3(cec_command, crosec_io, context);
247 if (rv < 0) {
248 cec_command->cmd_code = rv;
249 return 1;
250 }
251 return 0;
252}
253
Hung-Te Lin23fb9972013-06-21 20:11:47 +0800254int crosec_command_proto(struct chromeec_command *cec_command,
255 crosec_io_t crosec_io, void *context)
256{
Hung-Te Line946f982013-06-22 11:18:39 +0800257 // TODO(hungte) Detect and fallback to v2 if we need.
258 return crosec_command_proto_v3(cec_command, crosec_io, context);
Hung-Te Lin23fb9972013-06-21 20:11:47 +0800259}