1 /* SPDX-License-Identifier: GPL-2.0-only */
2
3 #include "system76_ec.h"
4 #include <arch/io.h>
5 #include <console/system76_ec.h>
6 #include <console/console.h>
7 #include <timer.h>
8
9 // This is the command region for System76 EC firmware. It must be
10 // enabled for LPC in the mainboard.
11 #define SYSTEM76_EC_BASE 0x0E00
12 #define SYSTEM76_EC_SIZE 256
13
14 #define REG_CMD 0
15 #define REG_RESULT 1
16 #define REG_DATA 2 // Start of command data
17
18 // When command register is 0, command is complete
19 #define CMD_FINISHED 0
20
21 #define RESULT_OK 0
22
23 // Print command. Registers are unique for each command
24 #define CMD_PRINT 4
25 #define CMD_PRINT_REG_FLAGS 2
26 #define CMD_PRINT_REG_LEN 3
27 #define CMD_PRINT_REG_DATA 4
28
system76_ec_read(uint8_t addr)29 static inline uint8_t system76_ec_read(uint8_t addr)
30 {
31 return inb(SYSTEM76_EC_BASE + (uint16_t)addr);
32 }
33
system76_ec_write(uint8_t addr,uint8_t data)34 static inline void system76_ec_write(uint8_t addr, uint8_t data)
35 {
36 outb(data, SYSTEM76_EC_BASE + (uint16_t)addr);
37 }
38
system76_ec_init(void)39 void system76_ec_init(void)
40 {
41 // Clear entire command region
42 for (int i = 0; i < SYSTEM76_EC_SIZE; i++)
43 system76_ec_write((uint8_t)i, 0);
44 }
45
system76_ec_flush(void)46 void system76_ec_flush(void)
47 {
48 system76_ec_write(REG_CMD, CMD_PRINT);
49
50 // Wait for command completion, for up to 10 milliseconds, with a
51 // test period of 1 microsecond
52 wait_us(10000, system76_ec_read(REG_CMD) == CMD_FINISHED);
53
54 system76_ec_write(CMD_PRINT_REG_LEN, 0);
55 }
56
system76_ec_print(uint8_t byte)57 void system76_ec_print(uint8_t byte)
58 {
59 uint8_t len = system76_ec_read(CMD_PRINT_REG_LEN);
60 system76_ec_write(CMD_PRINT_REG_DATA + len, byte);
61 system76_ec_write(CMD_PRINT_REG_LEN, len + 1);
62
63 // If we hit the end of the buffer, or were given a newline, flush
64 if (byte == '\n' || len >= (SYSTEM76_EC_SIZE - CMD_PRINT_REG_DATA))
65 system76_ec_flush();
66 }
67
system76_ec_cmd(uint8_t cmd,const uint8_t * request_data,uint8_t request_size,uint8_t * reply_data,uint8_t reply_size)68 bool system76_ec_cmd(uint8_t cmd, const uint8_t *request_data,
69 uint8_t request_size, uint8_t *reply_data, uint8_t reply_size)
70 {
71 if (request_size > SYSTEM76_EC_SIZE - REG_DATA ||
72 reply_size > SYSTEM76_EC_SIZE - REG_DATA) {
73 printk(BIOS_ERR, "EC command %d too long - request size %u, reply size %u\n",
74 cmd, request_size, reply_size);
75 return false;
76 }
77
78 /* If any data were buffered by system76_ec_print(), flush it first */
79 uint8_t buffered_len = system76_ec_read(CMD_PRINT_REG_LEN);
80 if (buffered_len > 0)
81 system76_ec_flush();
82
83 /* Write the data */
84 uint8_t i;
85 for (i = 0; i < request_size; ++i)
86 system76_ec_write(REG_DATA + i, request_data[i]);
87
88 /* Write the command */
89 system76_ec_write(REG_CMD, cmd);
90
91 /* Wait for the command to complete */
92 bool ret = true;
93 int elapsed = wait_ms(1000, system76_ec_read(REG_CMD) == CMD_FINISHED);
94 if (elapsed == 0) {
95 /* Timed out: fail the command, don't attempt to read a reply. */
96 printk(BIOS_WARNING, "EC command %d timed out - request size %d, reply size %d\n",
97 cmd, request_size, reply_size);
98 ret = false;
99 } else {
100 /* Read the reply */
101 for (i = 0; i < reply_size; ++i)
102 reply_data[i] = system76_ec_read(REG_DATA+i);
103 /* Check the reply status */
104 ret = (system76_ec_read(REG_RESULT) == RESULT_OK);
105 }
106
107 /* Reset the flags and length so we can buffer console prints again */
108 system76_ec_write(CMD_PRINT_REG_FLAGS, 0);
109 system76_ec_write(CMD_PRINT_REG_LEN, 0);
110
111 return ret;
112 }
113