blob: 0f5b891d54efa77fbb7d34c274f8f13f37e0f0f0 [file] [log] [blame]
Stefan Reinauerb5ab3232009-04-22 07:23:00 +00001/*
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 Hermann257ae3f2009-04-22 12:28:14 +00008 * published by the Free Software Foundation; version 2 of the License.
Stefan Reinauerb5ab3232009-04-22 07:23:00 +00009 *
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.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
Uwe Hermann257ae3f2009-04-22 12:28:14 +000017 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Stefan Reinauerb5ab3232009-04-22 07:23:00 +000018 */
19
20#include <stdio.h>
Stefan Reinauer2dd1ded2010-09-27 18:48:15 +000021#include <stdint.h>
Stefan Reinauerb5ab3232009-04-22 07:23:00 +000022#include <stdlib.h>
23#include <unistd.h>
24#include <sys/io.h>
25#include "ec.h"
26
27extern int verbose;
28
29#define debug(x...) if (verbose) printf(x)
30
31int send_ec_command(uint8_t command)
32{
33 int timeout;
34
35 timeout = 0x7ff;
36 while ((inb(EC_SC) & EC_IBF) && --timeout) {
37 usleep(10);
38 if ((timeout & 0xff) == 0)
39 debug(".");
40 }
41 if (!timeout) {
Anton Kochkov7e59f7692010-06-29 21:13:20 +000042 debug("Timeout while sending command 0x%02x to EC!\n",
Uwe Hermann257ae3f2009-04-22 12:28:14 +000043 command);
Stefan Reinauerb5ab3232009-04-22 07:23:00 +000044 // return -1;
45 }
46
47 outb(command, EC_SC);
48 return 0;
49}
50
51int send_ec_data(uint8_t data)
52{
53 int timeout;
54
55 timeout = 0x7ff;
Uwe Hermann257ae3f2009-04-22 12:28:14 +000056 while ((inb(EC_SC) & EC_IBF) && --timeout) { // wait for IBF = 0
Stefan Reinauerb5ab3232009-04-22 07:23:00 +000057 usleep(10);
58 if ((timeout & 0xff) == 0)
59 debug(".");
60 }
Anton Kochkov7e59f7692010-06-29 21:13:20 +000061 if (timeout) {
62 debug("Timeout while sending data 0x%02x to EC!\n", data);
Stefan Reinauerb5ab3232009-04-22 07:23:00 +000063 // return -1;
64 }
65
66 outb(data, EC_DATA);
67
68 return 0;
69}
70
71int send_ec_data_nowait(uint8_t data)
72{
73 outb(data, EC_DATA);
74
75 return 0;
76}
77
78uint8_t recv_ec_data(void)
79{
80 int timeout;
81 uint8_t data;
82
83 timeout = 0x7fff;
Uwe Hermann257ae3f2009-04-22 12:28:14 +000084 while (--timeout) { // Wait for OBF = 1
Stefan Reinauerb5ab3232009-04-22 07:23:00 +000085 if (inb(EC_SC) & EC_OBF) {
86 break;
87 }
88 usleep(10);
89 if ((timeout & 0xff) == 0)
90 debug(".");
91 }
92 if (!timeout) {
Anton Kochkov7e59f7692010-06-29 21:13:20 +000093 debug("\nTimeout while receiving data from EC!\n");
Stefan Reinauerb5ab3232009-04-22 07:23:00 +000094 // return -1;
95 }
96
97 data = inb(EC_DATA);
98 debug("recv_ec_data: 0x%02x\n", data);
99
100 return data;
101}
102
103uint8_t ec_read(uint8_t addr)
104{
Anton Kochkov7e59f7692010-06-29 21:13:20 +0000105 send_ec_command(RD_EC);
Stefan Reinauerb5ab3232009-04-22 07:23:00 +0000106 send_ec_data(addr);
107
108 return recv_ec_data();
109}
110
Anton Kochkov7e59f7692010-06-29 21:13:20 +0000111uint8_t ec_ext_read(uint16_t addr)
112{
113 send_ec_command(WR_EC);
114 send_ec_data(0x02);
115 send_ec_data(addr & 0xff);
116 send_ec_command(RX_EC);
117 send_ec_data(addr >> 8);
118
119 return recv_ec_data();
120}
121
122int ec_ext_write(uint16_t addr, uint8_t data)
123{
124 send_ec_command(WR_EC);
125 send_ec_data(0x02);
126 send_ec_data(addr & 0xff);
127 send_ec_command(WX_EC);
128 send_ec_data(addr >> 8);
Stefan Reinauer5ff7c132011-10-31 12:56:45 -0700129
Anton Kochkov7e59f7692010-06-29 21:13:20 +0000130 return send_ec_data(data);
131}
132
Stefan Reinauerb5ab3232009-04-22 07:23:00 +0000133int ec_write(uint8_t addr, uint8_t data)
134{
Anton Kochkov7e59f7692010-06-29 21:13:20 +0000135 send_ec_command(WR_EC);
Stefan Reinauerb5ab3232009-04-22 07:23:00 +0000136 send_ec_data(addr);
Uwe Hermann257ae3f2009-04-22 12:28:14 +0000137
Stefan Reinauerb5ab3232009-04-22 07:23:00 +0000138 return send_ec_data(data);
139}
Stefan Reinauer984e0f32010-01-16 17:50:55 +0000140
141uint8_t ec_idx_read(uint16_t addr)
142{
143 uint16_t lpc_idx = 0x380;
144
145 outb(addr & 0xff, lpc_idx + 2);
146 outb(addr >> 8, lpc_idx + 1);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000147
Stefan Reinauer984e0f32010-01-16 17:50:55 +0000148 return inb(lpc_idx + 3);
149}