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