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> |
| 19 | #include <unistd.h> |
Steven Dee | 3236f7b | 2017-01-29 14:52:56 -0500 | [diff] [blame] | 20 | #if !(defined __NetBSD__ || defined __OpenBSD__) |
Stefan Reinauer | b5ab323 | 2009-04-22 07:23:00 +0000 | [diff] [blame] | 21 | #include <sys/io.h> |
Andrey Korolyov | 393d932 | 2016-01-05 14:27:59 +0300 | [diff] [blame] | 22 | #endif |
Stefan Reinauer | b5ab323 | 2009-04-22 07:23:00 +0000 | [diff] [blame] | 23 | #include "ec.h" |
| 24 | |
Steven Dee | 3236f7b | 2017-01-29 14:52:56 -0500 | [diff] [blame] | 25 | #if defined __NetBSD__ || defined __OpenBSD__ |
Andrey Korolyov | 393d932 | 2016-01-05 14:27:59 +0300 | [diff] [blame] | 26 | #include <machine/sysarch.h> |
| 27 | static uint8_t inb(unsigned port) |
| 28 | { |
| 29 | uint8_t data; |
| 30 | __asm volatile("inb %w1,%0" : "=a" (data) : "d" (port)); |
| 31 | return data; |
| 32 | } |
| 33 | static __inline void outb(uint8_t data, unsigned port) |
| 34 | { |
| 35 | __asm volatile("outb %0,%w1" : : "a" (data), "d" (port)); |
| 36 | } |
| 37 | #endif |
| 38 | |
Stefan Reinauer | b5ab323 | 2009-04-22 07:23:00 +0000 | [diff] [blame] | 39 | extern int verbose; |
| 40 | |
| 41 | #define debug(x...) if (verbose) printf(x) |
| 42 | |
| 43 | int send_ec_command(uint8_t command) |
| 44 | { |
| 45 | int timeout; |
| 46 | |
| 47 | timeout = 0x7ff; |
| 48 | while ((inb(EC_SC) & EC_IBF) && --timeout) { |
| 49 | usleep(10); |
| 50 | if ((timeout & 0xff) == 0) |
| 51 | debug("."); |
| 52 | } |
| 53 | if (!timeout) { |
Anton Kochkov | 7e59f769 | 2010-06-29 21:13:20 +0000 | [diff] [blame] | 54 | debug("Timeout while sending command 0x%02x to EC!\n", |
Uwe Hermann | 257ae3f | 2009-04-22 12:28:14 +0000 | [diff] [blame] | 55 | command); |
Stefan Reinauer | b5ab323 | 2009-04-22 07:23:00 +0000 | [diff] [blame] | 56 | // return -1; |
| 57 | } |
| 58 | |
| 59 | outb(command, EC_SC); |
| 60 | return 0; |
| 61 | } |
| 62 | |
| 63 | int send_ec_data(uint8_t data) |
| 64 | { |
| 65 | int timeout; |
| 66 | |
| 67 | timeout = 0x7ff; |
Uwe Hermann | 257ae3f | 2009-04-22 12:28:14 +0000 | [diff] [blame] | 68 | while ((inb(EC_SC) & EC_IBF) && --timeout) { // wait for IBF = 0 |
Stefan Reinauer | b5ab323 | 2009-04-22 07:23:00 +0000 | [diff] [blame] | 69 | usleep(10); |
| 70 | if ((timeout & 0xff) == 0) |
| 71 | debug("."); |
| 72 | } |
Anton Kochkov | 7e59f769 | 2010-06-29 21:13:20 +0000 | [diff] [blame] | 73 | if (timeout) { |
| 74 | debug("Timeout while sending data 0x%02x to EC!\n", data); |
Stefan Reinauer | b5ab323 | 2009-04-22 07:23:00 +0000 | [diff] [blame] | 75 | // return -1; |
| 76 | } |
| 77 | |
| 78 | outb(data, EC_DATA); |
| 79 | |
| 80 | return 0; |
| 81 | } |
| 82 | |
| 83 | int send_ec_data_nowait(uint8_t data) |
| 84 | { |
| 85 | outb(data, EC_DATA); |
| 86 | |
| 87 | return 0; |
| 88 | } |
| 89 | |
| 90 | uint8_t recv_ec_data(void) |
| 91 | { |
| 92 | int timeout; |
| 93 | uint8_t data; |
| 94 | |
| 95 | timeout = 0x7fff; |
Uwe Hermann | 257ae3f | 2009-04-22 12:28:14 +0000 | [diff] [blame] | 96 | while (--timeout) { // Wait for OBF = 1 |
Stefan Reinauer | b5ab323 | 2009-04-22 07:23:00 +0000 | [diff] [blame] | 97 | if (inb(EC_SC) & EC_OBF) { |
| 98 | break; |
| 99 | } |
| 100 | usleep(10); |
| 101 | if ((timeout & 0xff) == 0) |
| 102 | debug("."); |
| 103 | } |
| 104 | if (!timeout) { |
Anton Kochkov | 7e59f769 | 2010-06-29 21:13:20 +0000 | [diff] [blame] | 105 | debug("\nTimeout while receiving data from EC!\n"); |
Stefan Reinauer | b5ab323 | 2009-04-22 07:23:00 +0000 | [diff] [blame] | 106 | // return -1; |
| 107 | } |
| 108 | |
| 109 | data = inb(EC_DATA); |
| 110 | debug("recv_ec_data: 0x%02x\n", data); |
| 111 | |
| 112 | return data; |
| 113 | } |
| 114 | |
| 115 | uint8_t ec_read(uint8_t addr) |
| 116 | { |
Anton Kochkov | 7e59f769 | 2010-06-29 21:13:20 +0000 | [diff] [blame] | 117 | send_ec_command(RD_EC); |
Stefan Reinauer | b5ab323 | 2009-04-22 07:23:00 +0000 | [diff] [blame] | 118 | send_ec_data(addr); |
| 119 | |
| 120 | return recv_ec_data(); |
| 121 | } |
| 122 | |
Anton Kochkov | 7e59f769 | 2010-06-29 21:13:20 +0000 | [diff] [blame] | 123 | uint8_t ec_ext_read(uint16_t addr) |
| 124 | { |
| 125 | send_ec_command(WR_EC); |
| 126 | send_ec_data(0x02); |
| 127 | send_ec_data(addr & 0xff); |
| 128 | send_ec_command(RX_EC); |
| 129 | send_ec_data(addr >> 8); |
| 130 | |
| 131 | return recv_ec_data(); |
| 132 | } |
| 133 | |
| 134 | int ec_ext_write(uint16_t addr, uint8_t data) |
| 135 | { |
| 136 | send_ec_command(WR_EC); |
| 137 | send_ec_data(0x02); |
| 138 | send_ec_data(addr & 0xff); |
| 139 | send_ec_command(WX_EC); |
| 140 | send_ec_data(addr >> 8); |
Stefan Reinauer | 5ff7c13 | 2011-10-31 12:56:45 -0700 | [diff] [blame] | 141 | |
Anton Kochkov | 7e59f769 | 2010-06-29 21:13:20 +0000 | [diff] [blame] | 142 | return send_ec_data(data); |
| 143 | } |
| 144 | |
Stefan Reinauer | b5ab323 | 2009-04-22 07:23:00 +0000 | [diff] [blame] | 145 | int ec_write(uint8_t addr, uint8_t data) |
| 146 | { |
Anton Kochkov | 7e59f769 | 2010-06-29 21:13:20 +0000 | [diff] [blame] | 147 | send_ec_command(WR_EC); |
Stefan Reinauer | b5ab323 | 2009-04-22 07:23:00 +0000 | [diff] [blame] | 148 | send_ec_data(addr); |
Uwe Hermann | 257ae3f | 2009-04-22 12:28:14 +0000 | [diff] [blame] | 149 | |
Stefan Reinauer | b5ab323 | 2009-04-22 07:23:00 +0000 | [diff] [blame] | 150 | return send_ec_data(data); |
| 151 | } |
Stefan Reinauer | 984e0f3 | 2010-01-16 17:50:55 +0000 | [diff] [blame] | 152 | |
| 153 | uint8_t ec_idx_read(uint16_t addr) |
| 154 | { |
| 155 | uint16_t lpc_idx = 0x380; |
| 156 | |
| 157 | outb(addr & 0xff, lpc_idx + 2); |
| 158 | outb(addr >> 8, lpc_idx + 1); |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 159 | |
Stefan Reinauer | 984e0f3 | 2010-01-16 17:50:55 +0000 | [diff] [blame] | 160 | return inb(lpc_idx + 3); |
| 161 | } |
Alexander Couzens | 0edf419 | 2015-02-06 22:27:33 +0100 | [diff] [blame] | 162 | |
| 163 | uint8_t ec_query(void) |
| 164 | { |
| 165 | send_ec_command(QR_EC); |
| 166 | return recv_ec_data(); |
| 167 | } |