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.
20 #if !(defined __NetBSD__ || defined __OpenBSD__)
25 #if defined __NetBSD__ || defined __OpenBSD__
26 #include <machine/sysarch.h>
27 static uint8_t inb(unsigned port
)
30 __asm
volatile("inb %w1,%0" : "=a" (data
) : "d" (port
));
33 static __inline
void outb(uint8_t data
, unsigned port
)
35 __asm
volatile("outb %0,%w1" : : "a" (data
), "d" (port
));
41 #define debug(x...) if (verbose) printf(x)
43 int send_ec_command(uint8_t command
)
48 while ((inb(EC_SC
) & EC_IBF
) && --timeout
) {
50 if ((timeout
& 0xff) == 0)
54 debug("Timeout while sending command 0x%02x to EC!\n",
63 int send_ec_data(uint8_t data
)
68 while ((inb(EC_SC
) & EC_IBF
) && --timeout
) { // wait for IBF = 0
70 if ((timeout
& 0xff) == 0)
74 debug("Timeout while sending data 0x%02x to EC!\n", data
);
83 int send_ec_data_nowait(uint8_t data
)
90 uint8_t recv_ec_data(void)
96 while (--timeout
) { // Wait for OBF = 1
97 if (inb(EC_SC
) & EC_OBF
) {
101 if ((timeout
& 0xff) == 0)
105 debug("\nTimeout while receiving data from EC!\n");
110 debug("recv_ec_data: 0x%02x\n", data
);
115 uint8_t ec_read(uint8_t addr
)
117 send_ec_command(RD_EC
);
120 return recv_ec_data();
123 uint8_t ec_ext_read(uint16_t addr
)
125 send_ec_command(WR_EC
);
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
);
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
);
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();