AGESA boards: Clean up some includes
[coreboot.git] / util / ectool / ec.c
blob53a53605f607dbc510fe1083643c33fe82fe10a7
1 /*
2 * This file is part of the ectool project.
4 * Copyright (C) 2008-2009 coresystems GmbH
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
8 * published by the Free Software Foundation; version 2 of the License.
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.
16 #include <stdio.h>
17 #include <stdint.h>
18 #include <stdlib.h>
19 #include <unistd.h>
20 #if !(defined __NetBSD__ || defined __OpenBSD__)
21 #include <sys/io.h>
22 #endif
23 #include "ec.h"
25 #if defined __NetBSD__ || defined __OpenBSD__
26 #include <machine/sysarch.h>
27 static uint8_t inb(unsigned port)
29 uint8_t data;
30 __asm volatile("inb %w1,%0" : "=a" (data) : "d" (port));
31 return data;
33 static __inline void outb(uint8_t data, unsigned port)
35 __asm volatile("outb %0,%w1" : : "a" (data), "d" (port));
37 #endif
39 extern int verbose;
41 #define debug(x...) if (verbose) printf(x)
43 int send_ec_command(uint8_t command)
45 int timeout;
47 timeout = 0x7ff;
48 while ((inb(EC_SC) & EC_IBF) && --timeout) {
49 usleep(10);
50 if ((timeout & 0xff) == 0)
51 debug(".");
53 if (!timeout) {
54 debug("Timeout while sending command 0x%02x to EC!\n",
55 command);
56 // return -1;
59 outb(command, EC_SC);
60 return 0;
63 int send_ec_data(uint8_t data)
65 int timeout;
67 timeout = 0x7ff;
68 while ((inb(EC_SC) & EC_IBF) && --timeout) { // wait for IBF = 0
69 usleep(10);
70 if ((timeout & 0xff) == 0)
71 debug(".");
73 if (!timeout) {
74 debug("Timeout while sending data 0x%02x to EC!\n", data);
75 // return -1;
78 outb(data, EC_DATA);
80 return 0;
83 int send_ec_data_nowait(uint8_t data)
85 outb(data, EC_DATA);
87 return 0;
90 uint8_t recv_ec_data(void)
92 int timeout;
93 uint8_t data;
95 timeout = 0x7fff;
96 while (--timeout) { // Wait for OBF = 1
97 if (inb(EC_SC) & EC_OBF) {
98 break;
100 usleep(10);
101 if ((timeout & 0xff) == 0)
102 debug(".");
104 if (!timeout) {
105 debug("\nTimeout while receiving data from EC!\n");
106 // return -1;
109 data = inb(EC_DATA);
110 debug("recv_ec_data: 0x%02x\n", data);
112 return data;
115 uint8_t ec_read(uint8_t addr)
117 send_ec_command(RD_EC);
118 send_ec_data(addr);
120 return recv_ec_data();
123 uint8_t ec_ext_read(uint16_t addr)
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);
131 return recv_ec_data();
134 int ec_ext_write(uint16_t addr, uint8_t data)
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);
142 return send_ec_data(data);
145 int ec_write(uint8_t addr, uint8_t data)
147 send_ec_command(WR_EC);
148 send_ec_data(addr);
150 return send_ec_data(data);
153 uint8_t ec_idx_read(uint16_t addr)
155 uint16_t lpc_idx = 0x380;
157 outb(addr & 0xff, lpc_idx + 2);
158 outb(addr >> 8, lpc_idx + 1);
160 return inb(lpc_idx + 3);
163 uint8_t ec_query(void)
165 send_ec_command(QR_EC);
166 return recv_ec_data();