Angel Pons | 210a008 | 2020-04-02 23:48:24 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Stefan Reinauer | 838c5a5 | 2010-01-17 14:08:17 +0000 | [diff] [blame] | 2 | |
| 3 | #include <console/console.h> |
| 4 | #include <arch/io.h> |
| 5 | #include <delay.h> |
Abel Briggs | c3bfbaf | 2022-04-14 22:08:41 -0500 | [diff] [blame] | 6 | #include <types.h> |
Stefan Reinauer | 838c5a5 | 2010-01-17 14:08:17 +0000 | [diff] [blame] | 7 | #include "ec.h" |
| 8 | |
Abel Briggs | c3bfbaf | 2022-04-14 22:08:41 -0500 | [diff] [blame] | 9 | #define EC_POLL_DELAY_US 10 |
| 10 | #define EC_SEND_TIMEOUT_US 20000 // 20ms |
| 11 | #define EC_RECV_TIMEOUT_US 320000 // 320ms |
| 12 | |
Kyösti Mälkki | 4a63780 | 2019-09-13 11:30:45 +0300 | [diff] [blame] | 13 | static u16 ec_cmd_reg = EC_SC; |
| 14 | static u16 ec_data_reg = EC_DATA; |
Vladimir Serbinenko | fe50d0b | 2013-06-07 01:55:57 +0200 | [diff] [blame] | 15 | |
Abel Briggs | c3bfbaf | 2022-04-14 22:08:41 -0500 | [diff] [blame] | 16 | /* |
| 17 | * Poll the EC status/command register for a specified |
| 18 | * state until the given timeout elapses. |
| 19 | */ |
| 20 | static int wait_ec_sc(int timeout_us, u8 mask, u8 state) |
| 21 | { |
| 22 | while (timeout_us > 0 && (inb(ec_cmd_reg) & mask) != state) { |
| 23 | udelay(EC_POLL_DELAY_US); |
| 24 | timeout_us -= EC_POLL_DELAY_US; |
| 25 | } |
| 26 | |
| 27 | return timeout_us > 0 ? 0 : -1; |
| 28 | } |
| 29 | |
| 30 | bool ec_ready_send(int timeout_us) |
| 31 | { |
| 32 | return wait_ec_sc(timeout_us, EC_IBF, 0) == 0; |
| 33 | } |
| 34 | |
| 35 | bool ec_ready_recv(int timeout_us) |
| 36 | { |
| 37 | return wait_ec_sc(timeout_us, EC_OBF, EC_OBF) == 0; |
| 38 | } |
| 39 | |
Stefan Reinauer | 838c5a5 | 2010-01-17 14:08:17 +0000 | [diff] [blame] | 40 | int send_ec_command(u8 command) |
| 41 | { |
Abel Briggs | c3bfbaf | 2022-04-14 22:08:41 -0500 | [diff] [blame] | 42 | return send_ec_command_timeout(command, EC_SEND_TIMEOUT_US); |
| 43 | } |
Stefan Reinauer | 838c5a5 | 2010-01-17 14:08:17 +0000 | [diff] [blame] | 44 | |
Abel Briggs | c3bfbaf | 2022-04-14 22:08:41 -0500 | [diff] [blame] | 45 | int send_ec_command_timeout(u8 command, int timeout_us) |
| 46 | { |
| 47 | if (!ec_ready_send(timeout_us)) { |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 48 | printk(BIOS_DEBUG, "Timeout while sending command 0x%02x to EC!\n", |
Stefan Reinauer | 838c5a5 | 2010-01-17 14:08:17 +0000 | [diff] [blame] | 49 | command); |
Abel Briggs | cb3c368 | 2022-05-14 10:49:23 -0500 | [diff] [blame] | 50 | return -1; |
Stefan Reinauer | 838c5a5 | 2010-01-17 14:08:17 +0000 | [diff] [blame] | 51 | } |
| 52 | |
Sven Schnelle | 0238a9c | 2011-03-14 08:18:27 +0000 | [diff] [blame] | 53 | outb(command, ec_cmd_reg); |
Abel Briggs | c3bfbaf | 2022-04-14 22:08:41 -0500 | [diff] [blame] | 54 | |
Stefan Reinauer | 838c5a5 | 2010-01-17 14:08:17 +0000 | [diff] [blame] | 55 | return 0; |
| 56 | } |
| 57 | |
| 58 | int send_ec_data(u8 data) |
| 59 | { |
Abel Briggs | c3bfbaf | 2022-04-14 22:08:41 -0500 | [diff] [blame] | 60 | return send_ec_data_timeout(data, EC_SEND_TIMEOUT_US); |
| 61 | } |
Stefan Reinauer | 838c5a5 | 2010-01-17 14:08:17 +0000 | [diff] [blame] | 62 | |
Abel Briggs | c3bfbaf | 2022-04-14 22:08:41 -0500 | [diff] [blame] | 63 | int send_ec_data_timeout(u8 data, int timeout_us) |
| 64 | { |
| 65 | if (!ec_ready_send(timeout_us)) { |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 66 | printk(BIOS_DEBUG, "Timeout while sending data 0x%02x to EC!\n", |
Stefan Reinauer | 838c5a5 | 2010-01-17 14:08:17 +0000 | [diff] [blame] | 67 | data); |
Abel Briggs | cb3c368 | 2022-05-14 10:49:23 -0500 | [diff] [blame] | 68 | return -1; |
Stefan Reinauer | 838c5a5 | 2010-01-17 14:08:17 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Sven Schnelle | 0238a9c | 2011-03-14 08:18:27 +0000 | [diff] [blame] | 71 | outb(data, ec_data_reg); |
Stefan Reinauer | 838c5a5 | 2010-01-17 14:08:17 +0000 | [diff] [blame] | 72 | |
| 73 | return 0; |
| 74 | } |
| 75 | |
Abel Briggs | cb3c368 | 2022-05-14 10:49:23 -0500 | [diff] [blame] | 76 | int recv_ec_data(void) |
Stefan Reinauer | 838c5a5 | 2010-01-17 14:08:17 +0000 | [diff] [blame] | 77 | { |
Abel Briggs | c3bfbaf | 2022-04-14 22:08:41 -0500 | [diff] [blame] | 78 | return recv_ec_data_timeout(EC_RECV_TIMEOUT_US); |
| 79 | } |
| 80 | |
Abel Briggs | cb3c368 | 2022-05-14 10:49:23 -0500 | [diff] [blame] | 81 | int recv_ec_data_timeout(int timeout_us) |
Abel Briggs | c3bfbaf | 2022-04-14 22:08:41 -0500 | [diff] [blame] | 82 | { |
Stefan Reinauer | 838c5a5 | 2010-01-17 14:08:17 +0000 | [diff] [blame] | 83 | u8 data; |
| 84 | |
Abel Briggs | c3bfbaf | 2022-04-14 22:08:41 -0500 | [diff] [blame] | 85 | if (!ec_ready_recv(timeout_us)) { |
| 86 | printk(BIOS_DEBUG, "Timeout while receiving data from EC!\n"); |
Abel Briggs | cb3c368 | 2022-05-14 10:49:23 -0500 | [diff] [blame] | 87 | return -1; |
Stefan Reinauer | 838c5a5 | 2010-01-17 14:08:17 +0000 | [diff] [blame] | 88 | } |
| 89 | |
Sven Schnelle | 0238a9c | 2011-03-14 08:18:27 +0000 | [diff] [blame] | 90 | data = inb(ec_data_reg); |
Felix Held | 0854f67 | 2023-04-13 20:06:22 +0200 | [diff] [blame^] | 91 | printk(BIOS_SPEW, "Data from EC: 0x%02x\n", data); |
Stefan Reinauer | 838c5a5 | 2010-01-17 14:08:17 +0000 | [diff] [blame] | 92 | |
| 93 | return data; |
| 94 | } |
| 95 | |
Bill XIE | eb4ded6 | 2017-10-26 11:45:49 +0800 | [diff] [blame] | 96 | void ec_clear_out_queue(void) |
| 97 | { |
Abel Briggs | c3bfbaf | 2022-04-14 22:08:41 -0500 | [diff] [blame] | 98 | int timeout = EC_RECV_TIMEOUT_US; |
Bill XIE | eb4ded6 | 2017-10-26 11:45:49 +0800 | [diff] [blame] | 99 | printk(BIOS_SPEW, "Clearing EC output queue...\n"); |
Abel Briggs | c3bfbaf | 2022-04-14 22:08:41 -0500 | [diff] [blame] | 100 | while (timeout > 0 && inb(ec_cmd_reg) & EC_OBF) { |
Bill XIE | eb4ded6 | 2017-10-26 11:45:49 +0800 | [diff] [blame] | 101 | u8 data = inb(ec_data_reg); |
| 102 | printk(BIOS_SPEW, "Discarding a garbage byte: 0x%02x\n", data); |
Abel Briggs | c3bfbaf | 2022-04-14 22:08:41 -0500 | [diff] [blame] | 103 | udelay(EC_POLL_DELAY_US); |
| 104 | timeout -= EC_POLL_DELAY_US; |
Bill XIE | eb4ded6 | 2017-10-26 11:45:49 +0800 | [diff] [blame] | 105 | } |
Abel Briggs | c3bfbaf | 2022-04-14 22:08:41 -0500 | [diff] [blame] | 106 | if (timeout <= 0) |
Bill XIE | eb4ded6 | 2017-10-26 11:45:49 +0800 | [diff] [blame] | 107 | printk(BIOS_ERR, "Timeout while clearing EC output queue!\n"); |
| 108 | else |
| 109 | printk(BIOS_SPEW, "EC output queue has been cleared.\n"); |
| 110 | } |
| 111 | |
Stefan Reinauer | 838c5a5 | 2010-01-17 14:08:17 +0000 | [diff] [blame] | 112 | u8 ec_read(u8 addr) |
| 113 | { |
Abel Briggs | c3bfbaf | 2022-04-14 22:08:41 -0500 | [diff] [blame] | 114 | send_ec_command(RD_EC); |
Stefan Reinauer | 838c5a5 | 2010-01-17 14:08:17 +0000 | [diff] [blame] | 115 | send_ec_data(addr); |
| 116 | |
| 117 | return recv_ec_data(); |
| 118 | } |
| 119 | |
| 120 | int ec_write(u8 addr, u8 data) |
| 121 | { |
Abel Briggs | c3bfbaf | 2022-04-14 22:08:41 -0500 | [diff] [blame] | 122 | send_ec_command(WR_EC); |
Stefan Reinauer | 838c5a5 | 2010-01-17 14:08:17 +0000 | [diff] [blame] | 123 | send_ec_data(addr); |
| 124 | return send_ec_data(data); |
| 125 | } |
| 126 | |
Nico Huber | 105d8e8 | 2017-01-23 13:54:41 +0100 | [diff] [blame] | 127 | u8 ec_status(void) |
| 128 | { |
| 129 | return inb(ec_cmd_reg); |
| 130 | } |
| 131 | |
Sven Schnelle | f9a0b92 | 2011-03-15 09:52:07 +0000 | [diff] [blame] | 132 | u8 ec_query(void) |
| 133 | { |
Abel Briggs | c3bfbaf | 2022-04-14 22:08:41 -0500 | [diff] [blame] | 134 | send_ec_command(QR_EC); |
Sven Schnelle | f9a0b92 | 2011-03-15 09:52:07 +0000 | [diff] [blame] | 135 | return recv_ec_data(); |
| 136 | } |
| 137 | |
Sven Schnelle | 8d94d7c | 2011-03-14 08:18:17 +0000 | [diff] [blame] | 138 | void ec_set_bit(u8 addr, u8 bit) |
| 139 | { |
| 140 | ec_write(addr, ec_read(addr) | (1 << bit)); |
| 141 | } |
| 142 | |
| 143 | void ec_clr_bit(u8 addr, u8 bit) |
| 144 | { |
| 145 | ec_write(addr, ec_read(addr) & ~(1 << bit)); |
| 146 | } |
| 147 | |
Sven Schnelle | 0238a9c | 2011-03-14 08:18:27 +0000 | [diff] [blame] | 148 | void ec_set_ports(u16 cmd_reg, u16 data_reg) |
| 149 | { |
Arthur Heymans | 6acc05e | 2022-05-12 18:01:13 +0200 | [diff] [blame] | 150 | if (!ENV_HAS_DATA_SECTION) |
Kyösti Mälkki | 4a63780 | 2019-09-13 11:30:45 +0300 | [diff] [blame] | 151 | return; |
| 152 | |
Sven Schnelle | 0238a9c | 2011-03-14 08:18:27 +0000 | [diff] [blame] | 153 | ec_cmd_reg = cmd_reg; |
| 154 | ec_data_reg = data_reg; |
| 155 | } |