Commit | Line | Data |
---|---|---|
a17d94f0 SG |
1 | /* |
2 | * ChromeOS EC multi-function device (SPI) | |
3 | * | |
4 | * Copyright (C) 2012 Google, Inc | |
5 | * | |
6 | * This software is licensed under the terms of the GNU General Public | |
7 | * License version 2, as published by the Free Software Foundation, and | |
8 | * may be copied, distributed, and modified under those terms. | |
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. | |
14 | */ | |
15 | ||
16 | #include <linux/delay.h> | |
17 | #include <linux/kernel.h> | |
18 | #include <linux/module.h> | |
19 | #include <linux/mfd/cros_ec.h> | |
20 | #include <linux/mfd/cros_ec_commands.h> | |
01e73c89 | 21 | #include <linux/of.h> |
a17d94f0 SG |
22 | #include <linux/platform_device.h> |
23 | #include <linux/slab.h> | |
24 | #include <linux/spi/spi.h> | |
25 | ||
26 | ||
27 | /* The header byte, which follows the preamble */ | |
28 | #define EC_MSG_HEADER 0xec | |
29 | ||
30 | /* | |
31 | * Number of EC preamble bytes we read at a time. Since it takes | |
32 | * about 400-500us for the EC to respond there is not a lot of | |
33 | * point in tuning this. If the EC could respond faster then | |
34 | * we could increase this so that might expect the preamble and | |
35 | * message to occur in a single transaction. However, the maximum | |
36 | * SPI transfer size is 256 bytes, so at 5MHz we need a response | |
37 | * time of perhaps <320us (200 bytes / 1600 bits). | |
38 | */ | |
39 | #define EC_MSG_PREAMBLE_COUNT 32 | |
40 | ||
41 | /* | |
9c0b54a1 DA |
42 | * Allow for a long time for the EC to respond. We support i2c |
43 | * tunneling and support fairly long messages for the tunnel (249 | |
44 | * bytes long at the moment). If we're talking to a 100 kHz device | |
45 | * on the other end and need to transfer ~256 bytes, then we need: | |
46 | * 10 us/bit * ~10 bits/byte * ~256 bytes = ~25ms | |
47 | * | |
48 | * We'll wait 4 times that to handle clock stretching and other | |
49 | * paranoia. | |
50 | * | |
51 | * It's pretty unlikely that we'll really see a 249 byte tunnel in | |
52 | * anything other than testing. If this was more common we might | |
53 | * consider having slow commands like this require a GET_STATUS | |
54 | * wait loop. The 'flash write' command would be another candidate | |
55 | * for this, clocking in at 2-3ms. | |
56 | */ | |
57 | #define EC_MSG_DEADLINE_MS 100 | |
a17d94f0 SG |
58 | |
59 | /* | |
60 | * Time between raising the SPI chip select (for the end of a | |
61 | * transaction) and dropping it again (for the next transaction). | |
49f91ac3 DB |
62 | * If we go too fast, the EC will miss the transaction. We know that we |
63 | * need at least 70 us with the 16 MHz STM32 EC, so go with 200 us to be | |
64 | * safe. | |
a17d94f0 | 65 | */ |
49f91ac3 | 66 | #define EC_SPI_RECOVERY_TIME_NS (200 * 1000) |
a17d94f0 SG |
67 | |
68 | /** | |
69 | * struct cros_ec_spi - information about a SPI-connected EC | |
70 | * | |
71 | * @spi: SPI device we are connected to | |
72 | * @last_transfer_ns: time that we last finished a transfer, or 0 if there | |
73 | * if no record | |
ff4378f4 AS |
74 | * @start_of_msg_delay: used to set the delay_usecs on the spi_transfer that |
75 | * is sent when we want to turn on CS at the start of a transaction. | |
01e73c89 RK |
76 | * @end_of_msg_delay: used to set the delay_usecs on the spi_transfer that |
77 | * is sent when we want to turn off CS at the end of a transaction. | |
a17d94f0 SG |
78 | */ |
79 | struct cros_ec_spi { | |
80 | struct spi_device *spi; | |
81 | s64 last_transfer_ns; | |
ff4378f4 | 82 | unsigned int start_of_msg_delay; |
01e73c89 | 83 | unsigned int end_of_msg_delay; |
a17d94f0 SG |
84 | }; |
85 | ||
86 | static void debug_packet(struct device *dev, const char *name, u8 *ptr, | |
d3654070 | 87 | int len) |
a17d94f0 SG |
88 | { |
89 | #ifdef DEBUG | |
90 | int i; | |
91 | ||
92 | dev_dbg(dev, "%s: ", name); | |
93 | for (i = 0; i < len; i++) | |
9e146f43 TR |
94 | pr_cont(" %02x", ptr[i]); |
95 | ||
96 | pr_cont("\n"); | |
a17d94f0 SG |
97 | #endif |
98 | } | |
99 | ||
d3654070 SB |
100 | static int terminate_request(struct cros_ec_device *ec_dev) |
101 | { | |
102 | struct cros_ec_spi *ec_spi = ec_dev->priv; | |
103 | struct spi_message msg; | |
104 | struct spi_transfer trans; | |
105 | int ret; | |
106 | ||
107 | /* | |
108 | * Turn off CS, possibly adding a delay to ensure the rising edge | |
109 | * doesn't come too soon after the end of the data. | |
110 | */ | |
111 | spi_message_init(&msg); | |
112 | memset(&trans, 0, sizeof(trans)); | |
113 | trans.delay_usecs = ec_spi->end_of_msg_delay; | |
114 | spi_message_add_tail(&trans, &msg); | |
115 | ||
116 | ret = spi_sync(ec_spi->spi, &msg); | |
117 | ||
118 | /* Reset end-of-response timer */ | |
119 | ec_spi->last_transfer_ns = ktime_get_ns(); | |
120 | if (ret < 0) { | |
121 | dev_err(ec_dev->dev, | |
122 | "cs-deassert spi transfer failed: %d\n", | |
123 | ret); | |
124 | } | |
125 | ||
126 | return ret; | |
127 | } | |
128 | ||
129 | /** | |
130 | * receive_n_bytes - receive n bytes from the EC. | |
131 | * | |
132 | * Assumes buf is a pointer into the ec_dev->din buffer | |
133 | */ | |
134 | static int receive_n_bytes(struct cros_ec_device *ec_dev, u8 *buf, int n) | |
135 | { | |
136 | struct cros_ec_spi *ec_spi = ec_dev->priv; | |
137 | struct spi_transfer trans; | |
138 | struct spi_message msg; | |
139 | int ret; | |
140 | ||
141 | BUG_ON(buf - ec_dev->din + n > ec_dev->din_size); | |
142 | ||
143 | memset(&trans, 0, sizeof(trans)); | |
144 | trans.cs_change = 1; | |
145 | trans.rx_buf = buf; | |
146 | trans.len = n; | |
147 | ||
148 | spi_message_init(&msg); | |
149 | spi_message_add_tail(&trans, &msg); | |
150 | ret = spi_sync(ec_spi->spi, &msg); | |
151 | if (ret < 0) | |
152 | dev_err(ec_dev->dev, "spi transfer failed: %d\n", ret); | |
153 | ||
154 | return ret; | |
155 | } | |
156 | ||
157 | /** | |
158 | * cros_ec_spi_receive_packet - Receive a packet from the EC. | |
159 | * | |
160 | * This function has two phases: reading the preamble bytes (since if we read | |
161 | * data from the EC before it is ready to send, we just get preamble) and | |
162 | * reading the actual message. | |
163 | * | |
164 | * The received data is placed into ec_dev->din. | |
165 | * | |
166 | * @ec_dev: ChromeOS EC device | |
167 | * @need_len: Number of message bytes we need to read | |
168 | */ | |
169 | static int cros_ec_spi_receive_packet(struct cros_ec_device *ec_dev, | |
170 | int need_len) | |
171 | { | |
172 | struct ec_host_response *response; | |
173 | u8 *ptr, *end; | |
174 | int ret; | |
175 | unsigned long deadline; | |
176 | int todo; | |
177 | ||
178 | BUG_ON(EC_MSG_PREAMBLE_COUNT > ec_dev->din_size); | |
179 | ||
180 | /* Receive data until we see the header byte */ | |
181 | deadline = jiffies + msecs_to_jiffies(EC_MSG_DEADLINE_MS); | |
182 | while (true) { | |
183 | unsigned long start_jiffies = jiffies; | |
184 | ||
185 | ret = receive_n_bytes(ec_dev, | |
186 | ec_dev->din, | |
187 | EC_MSG_PREAMBLE_COUNT); | |
188 | if (ret < 0) | |
189 | return ret; | |
190 | ||
191 | ptr = ec_dev->din; | |
192 | for (end = ptr + EC_MSG_PREAMBLE_COUNT; ptr != end; ptr++) { | |
193 | if (*ptr == EC_SPI_FRAME_START) { | |
194 | dev_dbg(ec_dev->dev, "msg found at %zd\n", | |
195 | ptr - ec_dev->din); | |
196 | break; | |
197 | } | |
198 | } | |
199 | if (ptr != end) | |
200 | break; | |
201 | ||
202 | /* | |
203 | * Use the time at the start of the loop as a timeout. This | |
204 | * gives us one last shot at getting the transfer and is useful | |
205 | * in case we got context switched out for a while. | |
206 | */ | |
207 | if (time_after(start_jiffies, deadline)) { | |
208 | dev_warn(ec_dev->dev, "EC failed to respond in time\n"); | |
209 | return -ETIMEDOUT; | |
210 | } | |
211 | } | |
212 | ||
213 | /* | |
214 | * ptr now points to the header byte. Copy any valid data to the | |
215 | * start of our buffer | |
216 | */ | |
217 | todo = end - ++ptr; | |
218 | BUG_ON(todo < 0 || todo > ec_dev->din_size); | |
219 | todo = min(todo, need_len); | |
220 | memmove(ec_dev->din, ptr, todo); | |
221 | ptr = ec_dev->din + todo; | |
222 | dev_dbg(ec_dev->dev, "need %d, got %d bytes from preamble\n", | |
223 | need_len, todo); | |
224 | need_len -= todo; | |
225 | ||
226 | /* If the entire response struct wasn't read, get the rest of it. */ | |
227 | if (todo < sizeof(*response)) { | |
228 | ret = receive_n_bytes(ec_dev, ptr, sizeof(*response) - todo); | |
229 | if (ret < 0) | |
230 | return -EBADMSG; | |
231 | ptr += (sizeof(*response) - todo); | |
232 | todo = sizeof(*response); | |
233 | } | |
234 | ||
235 | response = (struct ec_host_response *)ec_dev->din; | |
236 | ||
237 | /* Abort if data_len is too large. */ | |
238 | if (response->data_len > ec_dev->din_size) | |
239 | return -EMSGSIZE; | |
240 | ||
241 | /* Receive data until we have it all */ | |
242 | while (need_len > 0) { | |
243 | /* | |
244 | * We can't support transfers larger than the SPI FIFO size | |
245 | * unless we have DMA. We don't have DMA on the ISP SPI ports | |
246 | * for Exynos. We need a way of asking SPI driver for | |
247 | * maximum-supported transfer size. | |
248 | */ | |
249 | todo = min(need_len, 256); | |
250 | dev_dbg(ec_dev->dev, "loop, todo=%d, need_len=%d, ptr=%zd\n", | |
251 | todo, need_len, ptr - ec_dev->din); | |
252 | ||
253 | ret = receive_n_bytes(ec_dev, ptr, todo); | |
254 | if (ret < 0) | |
255 | return ret; | |
256 | ||
257 | ptr += todo; | |
258 | need_len -= todo; | |
259 | } | |
260 | ||
261 | dev_dbg(ec_dev->dev, "loop done, ptr=%zd\n", ptr - ec_dev->din); | |
262 | ||
263 | return 0; | |
264 | } | |
265 | ||
a17d94f0 SG |
266 | /** |
267 | * cros_ec_spi_receive_response - Receive a response from the EC. | |
268 | * | |
269 | * This function has two phases: reading the preamble bytes (since if we read | |
270 | * data from the EC before it is ready to send, we just get preamble) and | |
271 | * reading the actual message. | |
272 | * | |
273 | * The received data is placed into ec_dev->din. | |
274 | * | |
275 | * @ec_dev: ChromeOS EC device | |
276 | * @need_len: Number of message bytes we need to read | |
277 | */ | |
278 | static int cros_ec_spi_receive_response(struct cros_ec_device *ec_dev, | |
279 | int need_len) | |
280 | { | |
a17d94f0 SG |
281 | u8 *ptr, *end; |
282 | int ret; | |
283 | unsigned long deadline; | |
284 | int todo; | |
285 | ||
d3654070 SB |
286 | BUG_ON(EC_MSG_PREAMBLE_COUNT > ec_dev->din_size); |
287 | ||
a17d94f0 SG |
288 | /* Receive data until we see the header byte */ |
289 | deadline = jiffies + msecs_to_jiffies(EC_MSG_DEADLINE_MS); | |
c9a81d67 DA |
290 | while (true) { |
291 | unsigned long start_jiffies = jiffies; | |
292 | ||
d3654070 SB |
293 | ret = receive_n_bytes(ec_dev, |
294 | ec_dev->din, | |
295 | EC_MSG_PREAMBLE_COUNT); | |
296 | if (ret < 0) | |
a17d94f0 | 297 | return ret; |
a17d94f0 | 298 | |
d3654070 | 299 | ptr = ec_dev->din; |
a17d94f0 | 300 | for (end = ptr + EC_MSG_PREAMBLE_COUNT; ptr != end; ptr++) { |
d3654070 | 301 | if (*ptr == EC_SPI_FRAME_START) { |
c34924b9 | 302 | dev_dbg(ec_dev->dev, "msg found at %zd\n", |
a17d94f0 SG |
303 | ptr - ec_dev->din); |
304 | break; | |
305 | } | |
306 | } | |
c9a81d67 DA |
307 | if (ptr != end) |
308 | break; | |
a17d94f0 | 309 | |
c9a81d67 DA |
310 | /* |
311 | * Use the time at the start of the loop as a timeout. This | |
312 | * gives us one last shot at getting the transfer and is useful | |
313 | * in case we got context switched out for a while. | |
314 | */ | |
315 | if (time_after(start_jiffies, deadline)) { | |
a17d94f0 SG |
316 | dev_warn(ec_dev->dev, "EC failed to respond in time\n"); |
317 | return -ETIMEDOUT; | |
318 | } | |
c9a81d67 | 319 | } |
a17d94f0 SG |
320 | |
321 | /* | |
322 | * ptr now points to the header byte. Copy any valid data to the | |
323 | * start of our buffer | |
324 | */ | |
325 | todo = end - ++ptr; | |
326 | BUG_ON(todo < 0 || todo > ec_dev->din_size); | |
327 | todo = min(todo, need_len); | |
328 | memmove(ec_dev->din, ptr, todo); | |
329 | ptr = ec_dev->din + todo; | |
330 | dev_dbg(ec_dev->dev, "need %d, got %d bytes from preamble\n", | |
331 | need_len, todo); | |
332 | need_len -= todo; | |
333 | ||
334 | /* Receive data until we have it all */ | |
335 | while (need_len > 0) { | |
336 | /* | |
337 | * We can't support transfers larger than the SPI FIFO size | |
338 | * unless we have DMA. We don't have DMA on the ISP SPI ports | |
339 | * for Exynos. We need a way of asking SPI driver for | |
340 | * maximum-supported transfer size. | |
341 | */ | |
342 | todo = min(need_len, 256); | |
c34924b9 | 343 | dev_dbg(ec_dev->dev, "loop, todo=%d, need_len=%d, ptr=%zd\n", |
a17d94f0 SG |
344 | todo, need_len, ptr - ec_dev->din); |
345 | ||
d3654070 SB |
346 | ret = receive_n_bytes(ec_dev, ptr, todo); |
347 | if (ret < 0) | |
a17d94f0 | 348 | return ret; |
a17d94f0 SG |
349 | |
350 | debug_packet(ec_dev->dev, "interim", ptr, todo); | |
351 | ptr += todo; | |
352 | need_len -= todo; | |
353 | } | |
354 | ||
c34924b9 | 355 | dev_dbg(ec_dev->dev, "loop done, ptr=%zd\n", ptr - ec_dev->din); |
a17d94f0 SG |
356 | |
357 | return 0; | |
358 | } | |
359 | ||
d3654070 SB |
360 | /** |
361 | * cros_ec_pkt_xfer_spi - Transfer a packet over SPI and receive the reply | |
362 | * | |
363 | * @ec_dev: ChromeOS EC device | |
364 | * @ec_msg: Message to transfer | |
365 | */ | |
366 | static int cros_ec_pkt_xfer_spi(struct cros_ec_device *ec_dev, | |
367 | struct cros_ec_command *ec_msg) | |
368 | { | |
369 | struct ec_host_request *request; | |
370 | struct ec_host_response *response; | |
371 | struct cros_ec_spi *ec_spi = ec_dev->priv; | |
ff4378f4 | 372 | struct spi_transfer trans, trans_delay; |
d3654070 SB |
373 | struct spi_message msg; |
374 | int i, len; | |
375 | u8 *ptr; | |
376 | u8 *rx_buf; | |
377 | u8 sum; | |
378 | int ret = 0, final_ret; | |
379 | ||
380 | len = cros_ec_prepare_tx(ec_dev, ec_msg); | |
381 | request = (struct ec_host_request *)ec_dev->dout; | |
382 | dev_dbg(ec_dev->dev, "prepared, len=%d\n", len); | |
383 | ||
384 | /* If it's too soon to do another transaction, wait */ | |
385 | if (ec_spi->last_transfer_ns) { | |
386 | unsigned long delay; /* The delay completed so far */ | |
387 | ||
388 | delay = ktime_get_ns() - ec_spi->last_transfer_ns; | |
389 | if (delay < EC_SPI_RECOVERY_TIME_NS) | |
390 | ndelay(EC_SPI_RECOVERY_TIME_NS - delay); | |
391 | } | |
392 | ||
393 | rx_buf = kzalloc(len, GFP_KERNEL); | |
394 | if (!rx_buf) { | |
395 | ret = -ENOMEM; | |
396 | goto exit; | |
397 | } | |
398 | ||
ff4378f4 AS |
399 | /* |
400 | * Leave a gap between CS assertion and clocking of data to allow the | |
401 | * EC time to wakeup. | |
402 | */ | |
403 | spi_message_init(&msg); | |
404 | if (ec_spi->start_of_msg_delay) { | |
405 | memset(&trans_delay, 0, sizeof(trans_delay)); | |
406 | trans_delay.delay_usecs = ec_spi->start_of_msg_delay; | |
407 | spi_message_add_tail(&trans_delay, &msg); | |
408 | } | |
409 | ||
d3654070 SB |
410 | /* Transmit phase - send our message */ |
411 | memset(&trans, 0, sizeof(trans)); | |
412 | trans.tx_buf = ec_dev->dout; | |
413 | trans.rx_buf = rx_buf; | |
414 | trans.len = len; | |
415 | trans.cs_change = 1; | |
d3654070 SB |
416 | spi_message_add_tail(&trans, &msg); |
417 | ret = spi_sync(ec_spi->spi, &msg); | |
418 | ||
419 | /* Get the response */ | |
420 | if (!ret) { | |
421 | /* Verify that EC can process command */ | |
422 | for (i = 0; i < len; i++) { | |
423 | switch (rx_buf[i]) { | |
424 | case EC_SPI_PAST_END: | |
425 | case EC_SPI_RX_BAD_DATA: | |
426 | case EC_SPI_NOT_READY: | |
427 | ret = -EAGAIN; | |
428 | ec_msg->result = EC_RES_IN_PROGRESS; | |
429 | default: | |
430 | break; | |
431 | } | |
432 | if (ret) | |
433 | break; | |
434 | } | |
435 | if (!ret) | |
436 | ret = cros_ec_spi_receive_packet(ec_dev, | |
437 | ec_msg->insize + sizeof(*response)); | |
438 | } else { | |
439 | dev_err(ec_dev->dev, "spi transfer failed: %d\n", ret); | |
440 | } | |
441 | ||
442 | final_ret = terminate_request(ec_dev); | |
443 | if (!ret) | |
444 | ret = final_ret; | |
445 | if (ret < 0) | |
446 | goto exit; | |
447 | ||
448 | ptr = ec_dev->din; | |
449 | ||
450 | /* check response error code */ | |
451 | response = (struct ec_host_response *)ptr; | |
452 | ec_msg->result = response->result; | |
453 | ||
454 | ret = cros_ec_check_result(ec_dev, ec_msg); | |
455 | if (ret) | |
456 | goto exit; | |
457 | ||
458 | len = response->data_len; | |
459 | sum = 0; | |
460 | if (len > ec_msg->insize) { | |
461 | dev_err(ec_dev->dev, "packet too long (%d bytes, expected %d)", | |
462 | len, ec_msg->insize); | |
463 | ret = -EMSGSIZE; | |
464 | goto exit; | |
465 | } | |
466 | ||
467 | for (i = 0; i < sizeof(*response); i++) | |
468 | sum += ptr[i]; | |
469 | ||
470 | /* copy response packet payload and compute checksum */ | |
471 | memcpy(ec_msg->data, ptr + sizeof(*response), len); | |
472 | for (i = 0; i < len; i++) | |
473 | sum += ec_msg->data[i]; | |
474 | ||
475 | if (sum) { | |
476 | dev_err(ec_dev->dev, | |
477 | "bad packet checksum, calculated %x\n", | |
478 | sum); | |
479 | ret = -EBADMSG; | |
480 | goto exit; | |
481 | } | |
482 | ||
483 | ret = len; | |
484 | exit: | |
485 | kfree(rx_buf); | |
486 | if (ec_msg->command == EC_CMD_REBOOT_EC) | |
487 | msleep(EC_REBOOT_DELAY_MS); | |
488 | ||
489 | return ret; | |
490 | } | |
491 | ||
a17d94f0 | 492 | /** |
7e6cb5b4 | 493 | * cros_ec_cmd_xfer_spi - Transfer a message over SPI and receive the reply |
a17d94f0 SG |
494 | * |
495 | * @ec_dev: ChromeOS EC device | |
496 | * @ec_msg: Message to transfer | |
497 | */ | |
7e6cb5b4 | 498 | static int cros_ec_cmd_xfer_spi(struct cros_ec_device *ec_dev, |
5d4773e2 | 499 | struct cros_ec_command *ec_msg) |
a17d94f0 SG |
500 | { |
501 | struct cros_ec_spi *ec_spi = ec_dev->priv; | |
502 | struct spi_transfer trans; | |
503 | struct spi_message msg; | |
504 | int i, len; | |
505 | u8 *ptr; | |
d3654070 | 506 | u8 *rx_buf; |
a17d94f0 SG |
507 | int sum; |
508 | int ret = 0, final_ret; | |
a17d94f0 SG |
509 | |
510 | len = cros_ec_prepare_tx(ec_dev, ec_msg); | |
511 | dev_dbg(ec_dev->dev, "prepared, len=%d\n", len); | |
512 | ||
513 | /* If it's too soon to do another transaction, wait */ | |
514 | if (ec_spi->last_transfer_ns) { | |
a17d94f0 SG |
515 | unsigned long delay; /* The delay completed so far */ |
516 | ||
154adc14 | 517 | delay = ktime_get_ns() - ec_spi->last_transfer_ns; |
a17d94f0 | 518 | if (delay < EC_SPI_RECOVERY_TIME_NS) |
1fe36866 | 519 | ndelay(EC_SPI_RECOVERY_TIME_NS - delay); |
a17d94f0 SG |
520 | } |
521 | ||
d3654070 SB |
522 | rx_buf = kzalloc(len, GFP_KERNEL); |
523 | if (!rx_buf) { | |
524 | ret = -ENOMEM; | |
525 | goto exit; | |
526 | } | |
527 | ||
a17d94f0 SG |
528 | /* Transmit phase - send our message */ |
529 | debug_packet(ec_dev->dev, "out", ec_dev->dout, len); | |
daf93d22 | 530 | memset(&trans, 0, sizeof(trans)); |
a17d94f0 | 531 | trans.tx_buf = ec_dev->dout; |
d3654070 | 532 | trans.rx_buf = rx_buf; |
a17d94f0 SG |
533 | trans.len = len; |
534 | trans.cs_change = 1; | |
535 | spi_message_init(&msg); | |
536 | spi_message_add_tail(&trans, &msg); | |
537 | ret = spi_sync(ec_spi->spi, &msg); | |
538 | ||
539 | /* Get the response */ | |
540 | if (!ret) { | |
d3654070 SB |
541 | /* Verify that EC can process command */ |
542 | for (i = 0; i < len; i++) { | |
543 | switch (rx_buf[i]) { | |
544 | case EC_SPI_PAST_END: | |
545 | case EC_SPI_RX_BAD_DATA: | |
546 | case EC_SPI_NOT_READY: | |
547 | ret = -EAGAIN; | |
548 | ec_msg->result = EC_RES_IN_PROGRESS; | |
549 | default: | |
550 | break; | |
551 | } | |
552 | if (ret) | |
553 | break; | |
554 | } | |
555 | if (!ret) | |
556 | ret = cros_ec_spi_receive_response(ec_dev, | |
557 | ec_msg->insize + EC_MSG_TX_PROTO_BYTES); | |
a17d94f0 SG |
558 | } else { |
559 | dev_err(ec_dev->dev, "spi transfer failed: %d\n", ret); | |
560 | } | |
561 | ||
d3654070 | 562 | final_ret = terminate_request(ec_dev); |
a17d94f0 SG |
563 | if (!ret) |
564 | ret = final_ret; | |
d3654070 | 565 | if (ret < 0) |
362196e5 | 566 | goto exit; |
a17d94f0 | 567 | |
a17d94f0 | 568 | ptr = ec_dev->din; |
6db07b63 BR |
569 | |
570 | /* check response error code */ | |
571 | ec_msg->result = ptr[0]; | |
572 | ret = cros_ec_check_result(ec_dev, ec_msg); | |
573 | if (ret) | |
362196e5 | 574 | goto exit; |
6db07b63 | 575 | |
a17d94f0 SG |
576 | len = ptr[1]; |
577 | sum = ptr[0] + ptr[1]; | |
5d4773e2 | 578 | if (len > ec_msg->insize) { |
a17d94f0 | 579 | dev_err(ec_dev->dev, "packet too long (%d bytes, expected %d)", |
5d4773e2 | 580 | len, ec_msg->insize); |
362196e5 DA |
581 | ret = -ENOSPC; |
582 | goto exit; | |
a17d94f0 SG |
583 | } |
584 | ||
585 | /* copy response packet payload and compute checksum */ | |
586 | for (i = 0; i < len; i++) { | |
587 | sum += ptr[i + 2]; | |
5d4773e2 | 588 | if (ec_msg->insize) |
a8411784 | 589 | ec_msg->data[i] = ptr[i + 2]; |
a17d94f0 SG |
590 | } |
591 | sum &= 0xff; | |
592 | ||
593 | debug_packet(ec_dev->dev, "in", ptr, len + 3); | |
594 | ||
595 | if (sum != ptr[len + 2]) { | |
596 | dev_err(ec_dev->dev, | |
597 | "bad packet checksum, expected %02x, got %02x\n", | |
598 | sum, ptr[len + 2]); | |
362196e5 DA |
599 | ret = -EBADMSG; |
600 | goto exit; | |
a17d94f0 SG |
601 | } |
602 | ||
12ebc8a5 | 603 | ret = len; |
362196e5 | 604 | exit: |
d3654070 | 605 | kfree(rx_buf); |
659e142b DA |
606 | if (ec_msg->command == EC_CMD_REBOOT_EC) |
607 | msleep(EC_REBOOT_DELAY_MS); | |
608 | ||
362196e5 | 609 | return ret; |
a17d94f0 SG |
610 | } |
611 | ||
01e73c89 RK |
612 | static void cros_ec_spi_dt_probe(struct cros_ec_spi *ec_spi, struct device *dev) |
613 | { | |
614 | struct device_node *np = dev->of_node; | |
615 | u32 val; | |
616 | int ret; | |
617 | ||
ff4378f4 AS |
618 | ret = of_property_read_u32(np, "google,cros-ec-spi-pre-delay", &val); |
619 | if (!ret) | |
620 | ec_spi->start_of_msg_delay = val; | |
621 | ||
01e73c89 RK |
622 | ret = of_property_read_u32(np, "google,cros-ec-spi-msg-delay", &val); |
623 | if (!ret) | |
624 | ec_spi->end_of_msg_delay = val; | |
625 | } | |
626 | ||
9922b412 | 627 | static int cros_ec_spi_probe(struct spi_device *spi) |
a17d94f0 SG |
628 | { |
629 | struct device *dev = &spi->dev; | |
630 | struct cros_ec_device *ec_dev; | |
631 | struct cros_ec_spi *ec_spi; | |
632 | int err; | |
633 | ||
634 | spi->bits_per_word = 8; | |
635 | spi->mode = SPI_MODE_0; | |
636 | err = spi_setup(spi); | |
637 | if (err < 0) | |
638 | return err; | |
639 | ||
640 | ec_spi = devm_kzalloc(dev, sizeof(*ec_spi), GFP_KERNEL); | |
641 | if (ec_spi == NULL) | |
642 | return -ENOMEM; | |
643 | ec_spi->spi = spi; | |
644 | ec_dev = devm_kzalloc(dev, sizeof(*ec_dev), GFP_KERNEL); | |
645 | if (!ec_dev) | |
646 | return -ENOMEM; | |
647 | ||
01e73c89 RK |
648 | /* Check for any DT properties */ |
649 | cros_ec_spi_dt_probe(ec_spi, dev); | |
650 | ||
a17d94f0 | 651 | spi_set_drvdata(spi, ec_dev); |
a17d94f0 SG |
652 | ec_dev->dev = dev; |
653 | ec_dev->priv = ec_spi; | |
654 | ec_dev->irq = spi->irq; | |
7e6cb5b4 | 655 | ec_dev->cmd_xfer = cros_ec_cmd_xfer_spi; |
d3654070 | 656 | ec_dev->pkt_xfer = cros_ec_pkt_xfer_spi; |
a17d94f0 | 657 | ec_dev->phys_name = dev_name(&ec_spi->spi->dev); |
2c7589af SB |
658 | ec_dev->din_size = EC_MSG_PREAMBLE_COUNT + |
659 | sizeof(struct ec_host_response) + | |
660 | sizeof(struct ec_response_get_protocol_info); | |
661 | ec_dev->dout_size = sizeof(struct ec_host_request); | |
a17d94f0 | 662 | |
d3654070 | 663 | |
a17d94f0 SG |
664 | err = cros_ec_register(ec_dev); |
665 | if (err) { | |
666 | dev_err(dev, "cannot register EC\n"); | |
667 | return err; | |
668 | } | |
669 | ||
fb50497c P |
670 | device_init_wakeup(&spi->dev, true); |
671 | ||
a17d94f0 SG |
672 | return 0; |
673 | } | |
674 | ||
9922b412 | 675 | static int cros_ec_spi_remove(struct spi_device *spi) |
a17d94f0 SG |
676 | { |
677 | struct cros_ec_device *ec_dev; | |
678 | ||
679 | ec_dev = spi_get_drvdata(spi); | |
680 | cros_ec_remove(ec_dev); | |
681 | ||
682 | return 0; | |
683 | } | |
684 | ||
685 | #ifdef CONFIG_PM_SLEEP | |
686 | static int cros_ec_spi_suspend(struct device *dev) | |
687 | { | |
688 | struct cros_ec_device *ec_dev = dev_get_drvdata(dev); | |
689 | ||
690 | return cros_ec_suspend(ec_dev); | |
691 | } | |
692 | ||
693 | static int cros_ec_spi_resume(struct device *dev) | |
694 | { | |
695 | struct cros_ec_device *ec_dev = dev_get_drvdata(dev); | |
696 | ||
697 | return cros_ec_resume(ec_dev); | |
698 | } | |
699 | #endif | |
700 | ||
701 | static SIMPLE_DEV_PM_OPS(cros_ec_spi_pm_ops, cros_ec_spi_suspend, | |
702 | cros_ec_spi_resume); | |
703 | ||
704 | static const struct spi_device_id cros_ec_spi_id[] = { | |
705 | { "cros-ec-spi", 0 }, | |
706 | { } | |
707 | }; | |
708 | MODULE_DEVICE_TABLE(spi, cros_ec_spi_id); | |
709 | ||
710 | static struct spi_driver cros_ec_driver_spi = { | |
711 | .driver = { | |
712 | .name = "cros-ec-spi", | |
713 | .owner = THIS_MODULE, | |
714 | .pm = &cros_ec_spi_pm_ops, | |
715 | }, | |
9922b412 TR |
716 | .probe = cros_ec_spi_probe, |
717 | .remove = cros_ec_spi_remove, | |
a17d94f0 SG |
718 | .id_table = cros_ec_spi_id, |
719 | }; | |
720 | ||
721 | module_spi_driver(cros_ec_driver_spi); | |
722 | ||
ea0f8b0b | 723 | MODULE_LICENSE("GPL v2"); |
a17d94f0 | 724 | MODULE_DESCRIPTION("ChromeOS EC multi function device (SPI)"); |