1 /* SPDX-License-Identifier: GPL-2.0-only */
3 #include <console/console.h>
6 #include "ec_commands.h"
7 #include <spi-generic.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",
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
;
39 /* Wait minimum delay between CS assertions. */
40 stopwatch_wait_until_expired(&cs_cooldown_sw
);
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__
);
56 // Wait 1s for a framing byte.
57 stopwatch_init_usecs_expire(&sw
, USECS_PER_SEC
);
59 if (spi_xfer(slave
, NULL
, 0, &byte
, sizeof(byte
))) {
60 printk(BIOS_ERR
, "%s: Failed to receive byte.\n",
65 if (byte
== EcFramingByte
)
68 if (stopwatch_expired(&sw
)) {
70 "%s: Timeout waiting for framing byte.\n",
77 if (spi_xfer(slave
, NULL
, 0, resp_buf
, resp_size
)) {
78 printk(BIOS_ERR
, "%s: Failed to receive response.\n", __func__
);
83 spi_release_bus(slave
);
84 stopwatch_init_usecs_expire(&cs_cooldown_sw
, cs_cooldown_us
);
88 int google_chromeec_command(struct chromeec_command
*cec_command
)
91 static struct spi_slave slave
;
94 if (spi_setup_slave(CONFIG_EC_GOOGLE_CHROMEEC_SPI_BUS
,
95 CONFIG_EC_GOOGLE_CHROMEEC_SPI_CHIP
, &slave
))
97 stopwatch_init(&cs_cooldown_sw
);
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
;