drivers/uart: Replace 'unsigned long int' by 'unsigned long'
[coreboot2.git] / src / ec / google / chromeec / ec_spi.c
blob0124191d3530ecb6cd60f615ab16029e9a8725d1
1 /* SPDX-License-Identifier: GPL-2.0-only */
3 #include <console/console.h>
4 #include <delay.h>
5 #include "ec.h"
6 #include "ec_commands.h"
7 #include <spi-generic.h>
8 #include <timer.h>
10 static struct stopwatch cs_cooldown_sw;
11 static const long cs_cooldown_us = 200;
13 static const uint8_t EcFramingByte = 0xec;
15 #define PROTO3_MAX_PACKET_SIZE 268
17 static uint8_t req_buf[PROTO3_MAX_PACKET_SIZE];
18 static uint8_t resp_buf[PROTO3_MAX_PACKET_SIZE];
20 void *crosec_get_buffer(size_t size, int req)
22 if (size > PROTO3_MAX_PACKET_SIZE) {
23 printk(BIOS_DEBUG, "Proto v3 buffer request too large: %zu!\n",
24 size);
25 return NULL;
28 if (req)
29 return req_buf;
30 else
31 return resp_buf;
34 static int crosec_spi_io(size_t req_size, size_t resp_size, void *context)
36 struct spi_slave *slave = (struct spi_slave *)context;
37 int ret = 0;
39 /* Wait minimum delay between CS assertions. */
40 stopwatch_wait_until_expired(&cs_cooldown_sw);
42 spi_claim_bus(slave);
44 /* Allow EC to ramp up clock after being awaken.
45 * See chrome-os-partner:32223 for more details. */
46 udelay(CONFIG_EC_GOOGLE_CHROMEEC_SPI_WAKEUP_DELAY_US);
48 if (spi_xfer(slave, req_buf, req_size, NULL, 0)) {
49 printk(BIOS_ERR, "%s: Failed to send request.\n", __func__);
50 ret = -1;
51 goto out;
54 uint8_t byte;
55 struct stopwatch sw;
56 // Wait 1s for a framing byte.
57 stopwatch_init_usecs_expire(&sw, USECS_PER_SEC);
58 while (1) {
59 if (spi_xfer(slave, NULL, 0, &byte, sizeof(byte))) {
60 printk(BIOS_ERR, "%s: Failed to receive byte.\n",
61 __func__);
62 ret = -1;
63 goto out;
65 if (byte == EcFramingByte)
66 break;
68 if (stopwatch_expired(&sw)) {
69 printk(BIOS_ERR,
70 "%s: Timeout waiting for framing byte.\n",
71 __func__);
72 ret = -1;
73 goto out;
77 if (spi_xfer(slave, NULL, 0, resp_buf, resp_size)) {
78 printk(BIOS_ERR, "%s: Failed to receive response.\n", __func__);
79 ret = -1;
82 out:
83 spi_release_bus(slave);
84 stopwatch_init_usecs_expire(&cs_cooldown_sw, cs_cooldown_us);
85 return ret;
88 int google_chromeec_command(struct chromeec_command *cec_command)
90 static int done = 0;
91 static struct spi_slave slave;
93 if (!done) {
94 if (spi_setup_slave(CONFIG_EC_GOOGLE_CHROMEEC_SPI_BUS,
95 CONFIG_EC_GOOGLE_CHROMEEC_SPI_CHIP, &slave))
96 return -1;
97 stopwatch_init(&cs_cooldown_sw);
98 done = 1;
100 return crosec_command_proto(cec_command, crosec_spi_io, &slave);
103 enum host_event_code google_chromeec_get_event(void)
105 printk(BIOS_ERR, "%s: Not supported.\n", __func__);
106 return EC_HOST_EVENT_NONE;