| 1 |
|
| 2 |
|
| 3 |
#include <arch/io.h> |
| 4 |
#include <console/console.h> |
| 5 |
#include <delay.h> |
| 6 |
#include <ec/google/common/mec.h> |
| 7 |
#include <string.h> |
| 8 |
#include <timer.h> |
| 9 |
#include <types.h> |
| 10 |
|
| 11 |
#include "ec.h" |
| 12 |
|
| 13 |
|
| 14 |
#define EC_MAILBOX_ID 0x00f0 |
| 15 |
|
| 16 |
|
| 17 |
#define EC_MAILBOX_VERSION 0 |
| 18 |
|
| 19 |
|
| 20 |
#define EC_MAILBOX_START_COMMAND 0xda |
| 21 |
|
| 22 |
|
| 23 |
#define EC_MAILBOX_PROTO_VERSION 3 |
| 24 |
|
| 25 |
|
| 26 |
#define EC_MAILBOX_DATA_SIZE 32 |
| 27 |
|
| 28 |
|
| 29 |
#define EC_MAILBOX_DATA_EXTRA 2 |
| 30 |
|
| 31 |
|
| 32 |
#define EC_MAILBOX_TIMEOUT_MS MSECS_PER_SEC |
| 33 |
|
| 34 |
|
| 35 |
#define EC_CMDR_DATA BIT(0) |
| 36 |
#define EC_CMDR_PENDING BIT(1) |
| 37 |
#define EC_CMDR_BUSY BIT(2) |
| 38 |
#define EC_CMDR_CMD BIT(3) |
| 39 |
|
| 40 |
|
| 41 |
struct wilco_ec_request { |
| 42 |
uint8_t struct_version; |
| 43 |
uint8_t checksum; |
| 44 |
uint16_t mailbox_id; |
| 45 |
uint8_t mailbox_version; |
| 46 |
uint8_t reserved1; |
| 47 |
uint16_t data_size; |
| 48 |
uint8_t command; |
| 49 |
uint8_t reserved2; |
| 50 |
} __packed; |
| 51 |
|
| 52 |
|
| 53 |
struct wilco_ec_response { |
| 54 |
uint8_t struct_version; |
| 55 |
uint8_t checksum; |
| 56 |
uint16_t result; |
| 57 |
uint16_t data_size; |
| 58 |
uint8_t reserved[3]; |
| 59 |
uint8_t data[EC_MAILBOX_DATA_SIZE]; |
| 60 |
} __packed; |
| 61 |
|
| 62 |
struct wilco_ec_message { |
| 63 |
uint8_t command; |
| 64 |
uint8_t result; |
| 65 |
size_t request_size; |
| 66 |
size_t response_size; |
| 67 |
enum wilco_ec_msg_type type; |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
uint8_t data[EC_MAILBOX_DATA_SIZE]; |
| 73 |
}; |
| 74 |
|
| 75 |
static bool wilco_ec_response_timed_out(void) |
| 76 |
{ |
| 77 |
uint8_t mask = EC_CMDR_PENDING | EC_CMDR_BUSY; |
| 78 |
struct stopwatch sw; |
| 79 |
|
| 80 |
stopwatch_init_msecs_expire(&sw, EC_MAILBOX_TIMEOUT_MS); |
| 81 |
|
| 82 |
while (inb(CONFIG_EC_BASE_HOST_COMMAND) & mask) { |
| 83 |
if (stopwatch_expired(&sw)) { |
| 84 |
printk(BIOS_ERR, "%s: Command timeout\n", __func__); |
| 85 |
return true; |
| 86 |
} |
| 87 |
mdelay(1); |
| 88 |
} |
| 89 |
|
| 90 |
return false; |
| 91 |
} |
| 92 |
|
| 93 |
static uint8_t wilco_ec_checksum(void *data, size_t size) |
| 94 |
{ |
| 95 |
uint8_t *data_bytes = (uint8_t *)data; |
| 96 |
uint8_t checksum = 0; |
| 97 |
size_t i; |
| 98 |
|
| 99 |
for (i = 0; i < size; i++) |
| 100 |
checksum += data_bytes[i]; |
| 101 |
|
| 102 |
return checksum; |
| 103 |
} |
| 104 |
|
| 105 |
static void wilco_ec_prepare(struct wilco_ec_message *msg, |
| 106 |
struct wilco_ec_request *rq) |
| 107 |
{ |
| 108 |
memset(rq, 0, sizeof(*rq)); |
| 109 |
|
| 110 |
|
| 111 |
rq->struct_version = EC_MAILBOX_PROTO_VERSION; |
| 112 |
rq->mailbox_id = EC_MAILBOX_ID; |
| 113 |
rq->mailbox_version = EC_MAILBOX_VERSION; |
| 114 |
rq->data_size = msg->request_size + EC_MAILBOX_DATA_EXTRA; |
| 115 |
rq->command = msg->command; |
| 116 |
|
| 117 |
|
| 118 |
rq->checksum = wilco_ec_checksum(rq, sizeof(*rq)); |
| 119 |
rq->checksum += wilco_ec_checksum(msg->data, msg->request_size); |
| 120 |
rq->checksum = -rq->checksum; |
| 121 |
} |
| 122 |
|
| 123 |
static int wilco_ec_transfer(struct wilco_ec_message *msg) |
| 124 |
{ |
| 125 |
struct wilco_ec_request rq; |
| 126 |
struct wilco_ec_response rs; |
| 127 |
uint8_t checksum; |
| 128 |
size_t skip_size; |
| 129 |
|
| 130 |
|
| 131 |
wilco_ec_prepare(msg, &rq); |
| 132 |
|
| 133 |
|
| 134 |
mec_io_bytes(MEC_IO_WRITE, CONFIG_EC_BASE_PACKET, 0, &rq, sizeof(rq)); |
| 135 |
|
| 136 |
|
| 137 |
mec_io_bytes(MEC_IO_WRITE, CONFIG_EC_BASE_PACKET, sizeof(rq), |
| 138 |
msg->data, msg->request_size); |
| 139 |
|
| 140 |
|
| 141 |
outb(EC_MAILBOX_START_COMMAND, CONFIG_EC_BASE_HOST_COMMAND); |
| 142 |
|
| 143 |
|
| 144 |
if (msg->type == WILCO_EC_MSG_NO_RESPONSE) { |
| 145 |
printk(BIOS_DEBUG, "%s: EC does not respond to this command\n", |
| 146 |
__func__); |
| 147 |
return 0; |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
if (wilco_ec_response_timed_out()) { |
| 152 |
printk(BIOS_ERR, "%s: response timed out\n", __func__); |
| 153 |
return -1; |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
msg->result = inb(CONFIG_EC_BASE_HOST_DATA); |
| 158 |
if (msg->result != 0) { |
| 159 |
printk(BIOS_ERR, "%s: bad response: 0x%02x\n", |
| 160 |
__func__, msg->result); |
| 161 |
return -1; |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
checksum = mec_io_bytes(MEC_IO_READ, CONFIG_EC_BASE_PACKET, 0, |
| 166 |
&rs, sizeof(rs)); |
| 167 |
if (checksum) { |
| 168 |
printk(BIOS_ERR, "%s: bad checksum %02x\n", __func__, checksum); |
| 169 |
return -1; |
| 170 |
} |
| 171 |
msg->result = rs.result; |
| 172 |
|
| 173 |
|
| 174 |
if (rs.data_size > EC_MAILBOX_DATA_SIZE) { |
| 175 |
printk(BIOS_ERR, "%s: packet too long (%d bytes, expected %d)", |
| 176 |
__func__, rs.data_size, EC_MAILBOX_DATA_SIZE); |
| 177 |
return -1; |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
skip_size = (msg->type == WILCO_EC_MSG_DEFAULT) ? 1 : 0; |
| 182 |
|
| 183 |
if (msg->response_size > rs.data_size - skip_size) { |
| 184 |
printk(BIOS_ERR, "%s: data too short (%zu bytes, expected %zu)", |
| 185 |
__func__, rs.data_size - skip_size, msg->response_size); |
| 186 |
return -1; |
| 187 |
} |
| 188 |
|
| 189 |
memcpy(msg->data, rs.data + skip_size, msg->response_size); |
| 190 |
|
| 191 |
|
| 192 |
return msg->response_size; |
| 193 |
} |
| 194 |
|
| 195 |
int wilco_ec_mailbox(enum wilco_ec_msg_type type, uint8_t command, |
| 196 |
const void *request_data, size_t request_size, |
| 197 |
void *response_data, size_t response_size) |
| 198 |
{ |
| 199 |
struct wilco_ec_message msg = { |
| 200 |
.command = command, |
| 201 |
.request_size = request_size, |
| 202 |
.response_size = response_size, |
| 203 |
.type = type, |
| 204 |
}; |
| 205 |
int ret; |
| 206 |
|
| 207 |
if (request_size > EC_MAILBOX_DATA_SIZE) { |
| 208 |
printk(BIOS_ERR, "%s: provided request data too large: %zu\n", |
| 209 |
__func__, request_size); |
| 210 |
return -1; |
| 211 |
} |
| 212 |
if (response_size > EC_MAILBOX_DATA_SIZE) { |
| 213 |
printk(BIOS_ERR, "%s: expected response data too large: %zu\n", |
| 214 |
__func__, response_size); |
| 215 |
return -1; |
| 216 |
} |
| 217 |
if (request_size && !request_data) { |
| 218 |
printk(BIOS_ERR, "%s: request data missing\n", __func__); |
| 219 |
return -1; |
| 220 |
} |
| 221 |
if (response_size && !response_data) { |
| 222 |
printk(BIOS_ERR, "%s: request data missing\n", __func__); |
| 223 |
return -1; |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
if (request_size) |
| 228 |
memcpy(msg.data, request_data, request_size); |
| 229 |
|
| 230 |
|
| 231 |
ret = wilco_ec_transfer(&msg); |
| 232 |
|
| 233 |
|
| 234 |
if (ret > 0 && response_size) |
| 235 |
memcpy(response_data, msg.data, response_size); |
| 236 |
|
| 237 |
|
| 238 |
if (ret >= 0 && msg.result) |
| 239 |
ret = -1; |
| 240 |
|
| 241 |
return ret; |
| 242 |
} |