| 1 |
|
| 2 |
|
| 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> |
| 9 |
|
| 10 |
static struct stopwatch cs_cooldown_sw; |
| 11 |
static const long cs_cooldown_us = 200; |
| 12 |
|
| 13 |
static const uint8_t EcFramingByte = 0xec; |
| 14 |
|
| 15 |
#define PROTO3_MAX_PACKET_SIZE 268 |
| 16 |
|
| 17 |
static uint8_t req_buf[PROTO3_MAX_PACKET_SIZE]; |
| 18 |
static uint8_t resp_buf[PROTO3_MAX_PACKET_SIZE]; |
| 19 |
|
| 20 |
void *crosec_get_buffer(size_t size, int req) |
| 21 |
{ |
| 22 |
if (size > PROTO3_MAX_PACKET_SIZE) { |
| 23 |
printk(BIOS_DEBUG, "Proto v3 buffer request too large: %zu!\n", |
| 24 |
size); |
| 25 |
return NULL; |
| 26 |
} |
| 27 |
|
| 28 |
if (req) |
| 29 |
return req_buf; |
| 30 |
else |
| 31 |
return resp_buf; |
| 32 |
} |
| 33 |
|
| 34 |
static int crosec_spi_io(size_t req_size, size_t resp_size, void *context) |
| 35 |
{ |
| 36 |
struct spi_slave *slave = (struct spi_slave *)context; |
| 37 |
int ret = 0; |
| 38 |
|
| 39 |
|
| 40 |
stopwatch_wait_until_expired(&cs_cooldown_sw); |
| 41 |
|
| 42 |
spi_claim_bus(slave); |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
udelay(CONFIG_EC_GOOGLE_CHROMEEC_SPI_WAKEUP_DELAY_US); |
| 47 |
|
| 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; |
| 52 |
} |
| 53 |
|
| 54 |
uint8_t byte; |
| 55 |
struct stopwatch sw; |
| 56 |
|
| 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; |
| 64 |
} |
| 65 |
if (byte == EcFramingByte) |
| 66 |
break; |
| 67 |
|
| 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; |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 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; |
| 80 |
} |
| 81 |
|
| 82 |
out: |
| 83 |
spi_release_bus(slave); |
| 84 |
stopwatch_init_usecs_expire(&cs_cooldown_sw, cs_cooldown_us); |
| 85 |
return ret; |
| 86 |
} |
| 87 |
|
| 88 |
int google_chromeec_command(struct chromeec_command *cec_command) |
| 89 |
{ |
| 90 |
static int done = 0; |
| 91 |
static struct spi_slave slave; |
| 92 |
|
| 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; |
| 99 |
} |
| 100 |
return crosec_command_proto(cec_command, crosec_spi_io, &slave); |
| 101 |
} |
| 102 |
|
| 103 |
enum host_event_code google_chromeec_get_event(void) |
| 104 |
{ |
| 105 |
printk(BIOS_ERR, "%s: Not supported.\n", __func__); |
| 106 |
return EC_HOST_EVENT_NONE; |
| 107 |
} |