Since some people disapprove of white space cleanups mixed in regular commits
[coreboot.git] / src / mainboard / roda / rk886ex / 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 <arch/io.h>
24 #include <delay.h>
25 #include "ec.h"
26
27 int send_ec_command(u8 command)
28 {
29         int timeout;
30
31         timeout = 0x7ff;
32         while ((inb(EC_SC) & EC_IBF) && --timeout) {
33                 udelay(10);
34                 if ((timeout & 0xff) == 0)
35                         printk(BIOS_SPEW, ".");
36         }
37         if (!timeout) {
38                 printk(BIOS_DEBUG, "Timeout while sending command 0x%02x to EC!\n",
39                                 command);
40                 // return -1;
41         }
42
43         outb(command, EC_SC);
44         return 0;
45 }
46
47 int send_ec_data(u8 data)
48 {
49         int timeout;
50
51         timeout = 0x7ff;
52         while ((inb(EC_SC) & EC_IBF) && --timeout) { // wait for IBF = 0
53                 udelay(10);
54                 if ((timeout & 0xff) == 0)
55                         printk(BIOS_SPEW, ".");
56         }
57         if (!timeout) {
58                 printk(BIOS_DEBUG, "Timeout while sending data 0x%02x to EC!\n",
59                                 data);
60                 // return -1;
61         }
62
63         outb(data, EC_DATA);
64
65         return 0;
66 }
67
68 int send_ec_data_nowait(u8 data)
69 {
70         outb(data, EC_DATA);
71
72         return 0;
73 }
74
75 u8 recv_ec_data(void)
76 {
77         int timeout;
78         u8 data;
79
80         timeout = 0x7fff;
81         while (--timeout) { // Wait for OBF = 1
82                 if (inb(EC_SC) & EC_OBF) {
83                         break;
84                 }
85                 udelay(10);
86                 if ((timeout & 0xff) == 0)
87                         printk(BIOS_SPEW, ".");
88         }
89         if (!timeout) {
90                 printk(BIOS_DEBUG, "\nTimeout while receiving data from EC!\n");
91                 // return -1;
92         }
93
94         data = inb(EC_DATA);
95         printk(BIOS_DEBUG, "recv_ec_data: 0x%02x\n", data);
96
97         return data;
98 }
99
100 u8 ec_read(u8 addr)
101 {
102         send_ec_command(0x80);
103         send_ec_data(addr);
104
105         return recv_ec_data();
106 }
107
108 int ec_write(u8 addr, u8 data)
109 {
110         send_ec_command(0x81);
111         send_ec_data(addr);
112         return send_ec_data(data);
113 }
114