Patrick Georgi | ac95903 | 2020-05-05 22:49:26 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Stefan Reinauer | b5ab323 | 2009-04-22 07:23:00 +0000 | [diff] [blame] | 2 | |
| 3 | #include <stdio.h> |
Stefan Reinauer | 2dd1ded | 2010-09-27 18:48:15 +0000 | [diff] [blame] | 4 | #include <stdint.h> |
Stefan Reinauer | b5ab323 | 2009-04-22 07:23:00 +0000 | [diff] [blame] | 5 | #include <stdlib.h> |
Iru Cai | 2e8f4cc | 2018-01-25 21:44:09 +0800 | [diff] [blame] | 6 | #include <string.h> |
Stefan Reinauer | b5ab323 | 2009-04-22 07:23:00 +0000 | [diff] [blame] | 7 | #include <unistd.h> |
Steven Dee | 3236f7b | 2017-01-29 14:52:56 -0500 | [diff] [blame] | 8 | #if !(defined __NetBSD__ || defined __OpenBSD__) |
Stefan Reinauer | b5ab323 | 2009-04-22 07:23:00 +0000 | [diff] [blame] | 9 | #include <sys/io.h> |
Andrey Korolyov | 393d932 | 2016-01-05 14:27:59 +0300 | [diff] [blame] | 10 | #endif |
Stefan Reinauer | b5ab323 | 2009-04-22 07:23:00 +0000 | [diff] [blame] | 11 | #include "ec.h" |
| 12 | |
Iru Cai | 2e8f4cc | 2018-01-25 21:44:09 +0800 | [diff] [blame] | 13 | static int ec_data = 0x62; |
| 14 | static int ec_sc = 0x66; |
| 15 | |
Steven Dee | 3236f7b | 2017-01-29 14:52:56 -0500 | [diff] [blame] | 16 | #if defined __NetBSD__ || defined __OpenBSD__ |
Andrey Korolyov | 393d932 | 2016-01-05 14:27:59 +0300 | [diff] [blame] | 17 | #include <machine/sysarch.h> |
| 18 | static uint8_t inb(unsigned port) |
| 19 | { |
| 20 | uint8_t data; |
| 21 | __asm volatile("inb %w1,%0" : "=a" (data) : "d" (port)); |
| 22 | return data; |
| 23 | } |
| 24 | static __inline void outb(uint8_t data, unsigned port) |
| 25 | { |
| 26 | __asm volatile("outb %0,%w1" : : "a" (data), "d" (port)); |
| 27 | } |
| 28 | #endif |
| 29 | |
Stefan Reinauer | b5ab323 | 2009-04-22 07:23:00 +0000 | [diff] [blame] | 30 | extern int verbose; |
| 31 | |
| 32 | #define debug(x...) if (verbose) printf(x) |
| 33 | |
| 34 | int send_ec_command(uint8_t command) |
| 35 | { |
| 36 | int timeout; |
| 37 | |
| 38 | timeout = 0x7ff; |
Iru Cai | 2e8f4cc | 2018-01-25 21:44:09 +0800 | [diff] [blame] | 39 | while ((inb(ec_sc) & EC_IBF) && --timeout) { |
Stefan Reinauer | b5ab323 | 2009-04-22 07:23:00 +0000 | [diff] [blame] | 40 | usleep(10); |
| 41 | if ((timeout & 0xff) == 0) |
| 42 | debug("."); |
| 43 | } |
| 44 | if (!timeout) { |
Anton Kochkov | 7e59f769 | 2010-06-29 21:13:20 +0000 | [diff] [blame] | 45 | debug("Timeout while sending command 0x%02x to EC!\n", |
Uwe Hermann | 257ae3f | 2009-04-22 12:28:14 +0000 | [diff] [blame] | 46 | command); |
Stefan Reinauer | b5ab323 | 2009-04-22 07:23:00 +0000 | [diff] [blame] | 47 | // return -1; |
| 48 | } |
| 49 | |
Iru Cai | 2e8f4cc | 2018-01-25 21:44:09 +0800 | [diff] [blame] | 50 | outb(command, ec_sc); |
Stefan Reinauer | b5ab323 | 2009-04-22 07:23:00 +0000 | [diff] [blame] | 51 | return 0; |
| 52 | } |
| 53 | |
| 54 | int send_ec_data(uint8_t data) |
| 55 | { |
| 56 | int timeout; |
| 57 | |
| 58 | timeout = 0x7ff; |
Iru Cai | 2e8f4cc | 2018-01-25 21:44:09 +0800 | [diff] [blame] | 59 | while ((inb(ec_sc) & EC_IBF) && --timeout) { // wait for IBF = 0 |
Stefan Reinauer | b5ab323 | 2009-04-22 07:23:00 +0000 | [diff] [blame] | 60 | usleep(10); |
| 61 | if ((timeout & 0xff) == 0) |
| 62 | debug("."); |
| 63 | } |
Arthur Heymans | 2873a4a | 2017-04-06 14:17:26 +0200 | [diff] [blame] | 64 | if (!timeout) { |
Anton Kochkov | 7e59f769 | 2010-06-29 21:13:20 +0000 | [diff] [blame] | 65 | debug("Timeout while sending data 0x%02x to EC!\n", data); |
Stefan Reinauer | b5ab323 | 2009-04-22 07:23:00 +0000 | [diff] [blame] | 66 | // return -1; |
| 67 | } |
| 68 | |
Iru Cai | 2e8f4cc | 2018-01-25 21:44:09 +0800 | [diff] [blame] | 69 | outb(data, ec_data); |
Stefan Reinauer | b5ab323 | 2009-04-22 07:23:00 +0000 | [diff] [blame] | 70 | |
| 71 | return 0; |
| 72 | } |
| 73 | |
| 74 | int send_ec_data_nowait(uint8_t data) |
| 75 | { |
Iru Cai | 2e8f4cc | 2018-01-25 21:44:09 +0800 | [diff] [blame] | 76 | outb(data, ec_data); |
Stefan Reinauer | b5ab323 | 2009-04-22 07:23:00 +0000 | [diff] [blame] | 77 | |
| 78 | return 0; |
| 79 | } |
| 80 | |
| 81 | uint8_t recv_ec_data(void) |
| 82 | { |
| 83 | int timeout; |
| 84 | uint8_t data; |
| 85 | |
| 86 | timeout = 0x7fff; |
Uwe Hermann | 257ae3f | 2009-04-22 12:28:14 +0000 | [diff] [blame] | 87 | while (--timeout) { // Wait for OBF = 1 |
Iru Cai | 2e8f4cc | 2018-01-25 21:44:09 +0800 | [diff] [blame] | 88 | if (inb(ec_sc) & EC_OBF) |
Stefan Reinauer | b5ab323 | 2009-04-22 07:23:00 +0000 | [diff] [blame] | 89 | break; |
Iru Cai | 2e8f4cc | 2018-01-25 21:44:09 +0800 | [diff] [blame] | 90 | |
Stefan Reinauer | b5ab323 | 2009-04-22 07:23:00 +0000 | [diff] [blame] | 91 | usleep(10); |
| 92 | if ((timeout & 0xff) == 0) |
| 93 | debug("."); |
| 94 | } |
| 95 | if (!timeout) { |
Anton Kochkov | 7e59f769 | 2010-06-29 21:13:20 +0000 | [diff] [blame] | 96 | debug("\nTimeout while receiving data from EC!\n"); |
Stefan Reinauer | b5ab323 | 2009-04-22 07:23:00 +0000 | [diff] [blame] | 97 | // return -1; |
| 98 | } |
| 99 | |
Iru Cai | 2e8f4cc | 2018-01-25 21:44:09 +0800 | [diff] [blame] | 100 | data = inb(ec_data); |
Stefan Reinauer | b5ab323 | 2009-04-22 07:23:00 +0000 | [diff] [blame] | 101 | debug("recv_ec_data: 0x%02x\n", data); |
| 102 | |
| 103 | return data; |
| 104 | } |
| 105 | |
| 106 | uint8_t ec_read(uint8_t addr) |
| 107 | { |
Anton Kochkov | 7e59f769 | 2010-06-29 21:13:20 +0000 | [diff] [blame] | 108 | send_ec_command(RD_EC); |
Stefan Reinauer | b5ab323 | 2009-04-22 07:23:00 +0000 | [diff] [blame] | 109 | send_ec_data(addr); |
| 110 | |
| 111 | return recv_ec_data(); |
| 112 | } |
| 113 | |
Anton Kochkov | 7e59f769 | 2010-06-29 21:13:20 +0000 | [diff] [blame] | 114 | uint8_t ec_ext_read(uint16_t addr) |
| 115 | { |
| 116 | send_ec_command(WR_EC); |
| 117 | send_ec_data(0x02); |
| 118 | send_ec_data(addr & 0xff); |
| 119 | send_ec_command(RX_EC); |
| 120 | send_ec_data(addr >> 8); |
| 121 | |
| 122 | return recv_ec_data(); |
| 123 | } |
| 124 | |
| 125 | int ec_ext_write(uint16_t addr, uint8_t data) |
| 126 | { |
| 127 | send_ec_command(WR_EC); |
| 128 | send_ec_data(0x02); |
| 129 | send_ec_data(addr & 0xff); |
| 130 | send_ec_command(WX_EC); |
| 131 | send_ec_data(addr >> 8); |
Stefan Reinauer | 5ff7c13 | 2011-10-31 12:56:45 -0700 | [diff] [blame] | 132 | |
Anton Kochkov | 7e59f769 | 2010-06-29 21:13:20 +0000 | [diff] [blame] | 133 | return send_ec_data(data); |
| 134 | } |
| 135 | |
Stefan Reinauer | b5ab323 | 2009-04-22 07:23:00 +0000 | [diff] [blame] | 136 | int ec_write(uint8_t addr, uint8_t data) |
| 137 | { |
Anton Kochkov | 7e59f769 | 2010-06-29 21:13:20 +0000 | [diff] [blame] | 138 | send_ec_command(WR_EC); |
Stefan Reinauer | b5ab323 | 2009-04-22 07:23:00 +0000 | [diff] [blame] | 139 | send_ec_data(addr); |
Uwe Hermann | 257ae3f | 2009-04-22 12:28:14 +0000 | [diff] [blame] | 140 | |
Stefan Reinauer | b5ab323 | 2009-04-22 07:23:00 +0000 | [diff] [blame] | 141 | return send_ec_data(data); |
| 142 | } |
Stefan Reinauer | 984e0f3 | 2010-01-16 17:50:55 +0000 | [diff] [blame] | 143 | |
| 144 | uint8_t ec_idx_read(uint16_t addr) |
| 145 | { |
| 146 | uint16_t lpc_idx = 0x380; |
| 147 | |
| 148 | outb(addr & 0xff, lpc_idx + 2); |
| 149 | outb(addr >> 8, lpc_idx + 1); |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 150 | |
Stefan Reinauer | 984e0f3 | 2010-01-16 17:50:55 +0000 | [diff] [blame] | 151 | return inb(lpc_idx + 3); |
| 152 | } |
Alexander Couzens | 0edf419 | 2015-02-06 22:27:33 +0100 | [diff] [blame] | 153 | |
| 154 | uint8_t ec_query(void) |
| 155 | { |
| 156 | send_ec_command(QR_EC); |
| 157 | return recv_ec_data(); |
| 158 | } |
Iru Cai | 2e8f4cc | 2018-01-25 21:44:09 +0800 | [diff] [blame] | 159 | |
| 160 | int get_ec_ports(void) |
| 161 | { |
| 162 | FILE *fp = fopen("/proc/ioports", "r"); |
| 163 | int data = 0, cmd = 0; |
| 164 | char line[100]; |
| 165 | |
| 166 | if (fp == NULL) |
| 167 | return -1; |
| 168 | |
| 169 | while (!feof(fp) && (data == 0 || cmd == 0)) { |
Stefan Reinauer | 3a78259 | 2020-10-22 22:16:20 +0000 | [diff] [blame] | 170 | if (fgets(line, sizeof(line), fp) == NULL) { |
| 171 | fprintf(stderr, "Can not read from /proc/ioports.\n"); |
| 172 | break; |
| 173 | } |
Iru Cai | 2e8f4cc | 2018-01-25 21:44:09 +0800 | [diff] [blame] | 174 | if (strstr(line, "EC data") != NULL) |
| 175 | data = strtol(line, NULL, 16); |
| 176 | |
| 177 | if (strstr(line, "EC cmd") != NULL) |
| 178 | cmd = strtol(line, NULL, 16); |
| 179 | } |
| 180 | |
| 181 | fclose(fp); |
| 182 | if (data != 0 && cmd != 0) { |
| 183 | debug("EC data = 0x%x, EC cmd = 0x%x\n", data, cmd); |
| 184 | ec_data = data; |
| 185 | ec_sc = cmd; |
| 186 | } else { |
| 187 | return -1; |
| 188 | } |
| 189 | return 0; |
| 190 | } |