log ec data with DEBUG_SPEW
[coreboot.git] / src / ec / acpi / ec.c
1 /*
2  * This file is part of the coreboot 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
8  * published by the Free Software Foundation; version 2 of
9  * the License.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
19  * MA 02110-1301 USA
20  */
21
22 #include <console/console.h>
23 #include <device/device.h>
24 #include <arch/io.h>
25 #include <delay.h>
26 #include "ec.h"
27
28 static int ec_cmd_reg = EC_SC;
29 static int ec_data_reg = EC_DATA;
30
31 int send_ec_command(u8 command)
32 {
33         int timeout;
34
35         timeout = 0x7ff;
36         while ((inb(ec_cmd_reg) & EC_IBF) && --timeout) {
37                 udelay(10);
38                 if ((timeout & 0xff) == 0)
39                         printk(BIOS_SPEW, ".");
40         }
41         if (!timeout) {
42                 printk(BIOS_DEBUG, "Timeout while sending command 0x%02x to EC!\n",
43                                 command);
44                 // return -1;
45         }
46
47         outb(command, ec_cmd_reg);
48         return 0;
49 }
50
51 int send_ec_data(u8 data)
52 {
53         int timeout;
54
55         timeout = 0x7ff;
56         while ((inb(ec_cmd_reg) & EC_IBF) && --timeout) { // wait for IBF = 0
57                 udelay(10);
58                 if ((timeout & 0xff) == 0)
59                         printk(BIOS_SPEW, ".");
60         }
61         if (!timeout) {
62                 printk(BIOS_DEBUG, "Timeout while sending data 0x%02x to EC!\n",
63                                 data);
64                 // return -1;
65         }
66
67         outb(data, ec_data_reg);
68
69         return 0;
70 }
71
72 int send_ec_data_nowait(u8 data)
73 {
74         outb(data, ec_data_reg);
75
76         return 0;
77 }
78
79 u8 recv_ec_data(void)
80 {
81         int timeout;
82         u8 data;
83
84         timeout = 0x7fff;
85         while (--timeout) { // Wait for OBF = 1
86                 if (inb(ec_cmd_reg) & EC_OBF) {
87                         break;
88                 }
89                 udelay(10);
90                 if ((timeout & 0xff) == 0)
91                         printk(BIOS_SPEW, ".");
92         }
93         if (!timeout) {
94                 printk(BIOS_DEBUG, "\nTimeout while receiving data from EC!\n");
95                 // return -1;
96         }
97
98         data = inb(ec_data_reg);
99         printk(BIOS_SPEW, "recv_ec_data: 0x%02x\n", data);
100
101         return data;
102 }
103
104 u8 ec_read(u8 addr)
105 {
106         send_ec_command(0x80);
107         send_ec_data(addr);
108
109         return recv_ec_data();
110 }
111
112 int ec_write(u8 addr, u8 data)
113 {
114         send_ec_command(0x81);
115         send_ec_data(addr);
116         return send_ec_data(data);
117 }
118
119 u8 ec_query(void)
120 {
121         send_ec_command(0x84);
122         return recv_ec_data();
123 }
124
125 void ec_set_bit(u8 addr, u8 bit)
126 {
127         ec_write(addr, ec_read(addr) | (1 << bit));
128 }
129
130 void ec_clr_bit(u8 addr, u8 bit)
131 {
132         ec_write(addr, ec_read(addr) &  ~(1 << bit));
133 }
134
135 void ec_set_ports(u16 cmd_reg, u16 data_reg)
136 {
137         ec_cmd_reg = cmd_reg;
138         ec_data_reg = data_reg;
139 }
140
141 struct chip_operations ec_acpi_ops = {
142         CHIP_NAME("ACPI Embedded Controller")
143 };