blob: ab287d6afddc1ea7792f8b8fa0ed25240b64425b [file] [log] [blame]
Stefan Reinauer838c5a52010-01-17 14:08:17 +00001/*
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>
Sven Schnelle7592e8b2011-01-27 11:43:03 +000023#include <device/device.h>
Stefan Reinauer838c5a52010-01-17 14:08:17 +000024#include <arch/io.h>
25#include <delay.h>
26#include "ec.h"
27
Vladimir Serbinenkofe50d0b2013-06-07 01:55:57 +020028#ifdef __PRE_RAM__
29
30static const int ec_cmd_reg = EC_SC;
31static const int ec_data_reg = EC_DATA;
32
33#else
34
Sven Schnelle0238a9c2011-03-14 08:18:27 +000035static int ec_cmd_reg = EC_SC;
36static int ec_data_reg = EC_DATA;
37
Vladimir Serbinenkofe50d0b2013-06-07 01:55:57 +020038#endif
39
Stefan Reinauer838c5a52010-01-17 14:08:17 +000040int send_ec_command(u8 command)
41{
42 int timeout;
43
44 timeout = 0x7ff;
Sven Schnelle0238a9c2011-03-14 08:18:27 +000045 while ((inb(ec_cmd_reg) & EC_IBF) && --timeout) {
Stefan Reinauer838c5a52010-01-17 14:08:17 +000046 udelay(10);
47 if ((timeout & 0xff) == 0)
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +000048 printk(BIOS_SPEW, ".");
Stefan Reinauer838c5a52010-01-17 14:08:17 +000049 }
50 if (!timeout) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000051 printk(BIOS_DEBUG, "Timeout while sending command 0x%02x to EC!\n",
Stefan Reinauer838c5a52010-01-17 14:08:17 +000052 command);
53 // return -1;
54 }
55
Sven Schnelle0238a9c2011-03-14 08:18:27 +000056 outb(command, ec_cmd_reg);
Stefan Reinauer838c5a52010-01-17 14:08:17 +000057 return 0;
58}
59
60int send_ec_data(u8 data)
61{
62 int timeout;
63
64 timeout = 0x7ff;
Sven Schnelle0238a9c2011-03-14 08:18:27 +000065 while ((inb(ec_cmd_reg) & EC_IBF) && --timeout) { // wait for IBF = 0
Stefan Reinauer838c5a52010-01-17 14:08:17 +000066 udelay(10);
67 if ((timeout & 0xff) == 0)
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +000068 printk(BIOS_SPEW, ".");
Stefan Reinauer838c5a52010-01-17 14:08:17 +000069 }
70 if (!timeout) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +000071 printk(BIOS_DEBUG, "Timeout while sending data 0x%02x to EC!\n",
Stefan Reinauer838c5a52010-01-17 14:08:17 +000072 data);
73 // return -1;
74 }
75
Sven Schnelle0238a9c2011-03-14 08:18:27 +000076 outb(data, ec_data_reg);
Stefan Reinauer838c5a52010-01-17 14:08:17 +000077
78 return 0;
79}
80
81int send_ec_data_nowait(u8 data)
82{
Sven Schnelle0238a9c2011-03-14 08:18:27 +000083 outb(data, ec_data_reg);
Stefan Reinauer838c5a52010-01-17 14:08:17 +000084
85 return 0;
86}
87
88u8 recv_ec_data(void)
89{
90 int timeout;
91 u8 data;
92
93 timeout = 0x7fff;
94 while (--timeout) { // Wait for OBF = 1
Sven Schnelle0238a9c2011-03-14 08:18:27 +000095 if (inb(ec_cmd_reg) & EC_OBF) {
Stefan Reinauer838c5a52010-01-17 14:08:17 +000096 break;
97 }
98 udelay(10);
99 if ((timeout & 0xff) == 0)
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000100 printk(BIOS_SPEW, ".");
Stefan Reinauer838c5a52010-01-17 14:08:17 +0000101 }
102 if (!timeout) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000103 printk(BIOS_DEBUG, "\nTimeout while receiving data from EC!\n");
Stefan Reinauer838c5a52010-01-17 14:08:17 +0000104 // return -1;
105 }
106
Sven Schnelle0238a9c2011-03-14 08:18:27 +0000107 data = inb(ec_data_reg);
Sven Schnelle5d9a83c2011-06-12 16:53:25 +0200108 printk(BIOS_SPEW, "recv_ec_data: 0x%02x\n", data);
Stefan Reinauer838c5a52010-01-17 14:08:17 +0000109
110 return data;
111}
112
113u8 ec_read(u8 addr)
114{
115 send_ec_command(0x80);
116 send_ec_data(addr);
117
118 return recv_ec_data();
119}
120
121int ec_write(u8 addr, u8 data)
122{
123 send_ec_command(0x81);
124 send_ec_data(addr);
125 return send_ec_data(data);
126}
127
Sven Schnellef9a0b922011-03-15 09:52:07 +0000128u8 ec_query(void)
129{
130 send_ec_command(0x84);
131 return recv_ec_data();
132}
133
Sven Schnelle8d94d7c2011-03-14 08:18:17 +0000134void ec_set_bit(u8 addr, u8 bit)
135{
136 ec_write(addr, ec_read(addr) | (1 << bit));
137}
138
139void ec_clr_bit(u8 addr, u8 bit)
140{
141 ec_write(addr, ec_read(addr) & ~(1 << bit));
142}
143
Vladimir Serbinenkofe50d0b2013-06-07 01:55:57 +0200144#ifndef __PRE_RAM__
145
Sven Schnelle0238a9c2011-03-14 08:18:27 +0000146void ec_set_ports(u16 cmd_reg, u16 data_reg)
147{
148 ec_cmd_reg = cmd_reg;
149 ec_data_reg = data_reg;
150}
151
Vladimir Serbinenkofe50d0b2013-06-07 01:55:57 +0200152#endif
153
Stefan Reinauer24d1d4b2013-03-21 11:51:41 -0700154#if !defined(__SMM__) && !defined(__PRE_RAM__)
Sven Schnelle7592e8b2011-01-27 11:43:03 +0000155struct chip_operations ec_acpi_ops = {
156 CHIP_NAME("ACPI Embedded Controller")
157};
Stefan Reinauer24d1d4b2013-03-21 11:51:41 -0700158#endif