blob: 3f5cd9ed1b651948fd8c9d80262f84cebbd7f94f [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.
Stefan Reinauer838c5a52010-01-17 14:08:17 +000015 */
16
17#include <console/console.h>
Sven Schnelle7592e8b2011-01-27 11:43:03 +000018#include <device/device.h>
Stefan Reinauer838c5a52010-01-17 14:08:17 +000019#include <arch/io.h>
20#include <delay.h>
21#include "ec.h"
22
Vladimir Serbinenkofe50d0b2013-06-07 01:55:57 +020023#ifdef __PRE_RAM__
24
25static const int ec_cmd_reg = EC_SC;
26static const int ec_data_reg = EC_DATA;
27
28#else
29
Sven Schnelle0238a9c2011-03-14 08:18:27 +000030static int ec_cmd_reg = EC_SC;
31static int ec_data_reg = EC_DATA;
32
Vladimir Serbinenkofe50d0b2013-06-07 01:55:57 +020033#endif
34
Stefan Reinauer838c5a52010-01-17 14:08:17 +000035int send_ec_command(u8 command)
36{
37 int timeout;
38
39 timeout = 0x7ff;
Sven Schnelle0238a9c2011-03-14 08:18:27 +000040 while ((inb(ec_cmd_reg) & EC_IBF) && --timeout) {
Stefan Reinauer838c5a52010-01-17 14:08:17 +000041 udelay(10);
42 if ((timeout & 0xff) == 0)
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +000043 printk(BIOS_SPEW, ".");
Stefan Reinauer838c5a52010-01-17 14:08:17 +000044 }
45 if (!timeout) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000046 printk(BIOS_DEBUG, "Timeout while sending command 0x%02x to EC!\n",
Stefan Reinauer838c5a52010-01-17 14:08:17 +000047 command);
48 // return -1;
49 }
50
Vladimir Serbinenkoea9181c2014-01-05 06:50:10 +010051 udelay(10);
52
Sven Schnelle0238a9c2011-03-14 08:18:27 +000053 outb(command, ec_cmd_reg);
Stefan Reinauer838c5a52010-01-17 14:08:17 +000054 return 0;
55}
56
57int send_ec_data(u8 data)
58{
59 int timeout;
60
61 timeout = 0x7ff;
Sven Schnelle0238a9c2011-03-14 08:18:27 +000062 while ((inb(ec_cmd_reg) & EC_IBF) && --timeout) { // wait for IBF = 0
Stefan Reinauer838c5a52010-01-17 14:08:17 +000063 udelay(10);
64 if ((timeout & 0xff) == 0)
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +000065 printk(BIOS_SPEW, ".");
Stefan Reinauer838c5a52010-01-17 14:08:17 +000066 }
67 if (!timeout) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +000068 printk(BIOS_DEBUG, "Timeout while sending data 0x%02x to EC!\n",
Stefan Reinauer838c5a52010-01-17 14:08:17 +000069 data);
70 // return -1;
71 }
72
Vladimir Serbinenkoea9181c2014-01-05 06:50:10 +010073 udelay(10);
74
Sven Schnelle0238a9c2011-03-14 08:18:27 +000075 outb(data, ec_data_reg);
Stefan Reinauer838c5a52010-01-17 14:08:17 +000076
77 return 0;
78}
79
80int send_ec_data_nowait(u8 data)
81{
Sven Schnelle0238a9c2011-03-14 08:18:27 +000082 outb(data, ec_data_reg);
Stefan Reinauer838c5a52010-01-17 14:08:17 +000083
84 return 0;
85}
86
87u8 recv_ec_data(void)
88{
89 int timeout;
90 u8 data;
91
92 timeout = 0x7fff;
93 while (--timeout) { // Wait for OBF = 1
Sven Schnelle0238a9c2011-03-14 08:18:27 +000094 if (inb(ec_cmd_reg) & EC_OBF) {
Stefan Reinauer838c5a52010-01-17 14:08:17 +000095 break;
96 }
97 udelay(10);
98 if ((timeout & 0xff) == 0)
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +000099 printk(BIOS_SPEW, ".");
Stefan Reinauer838c5a52010-01-17 14:08:17 +0000100 }
101 if (!timeout) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000102 printk(BIOS_DEBUG, "\nTimeout while receiving data from EC!\n");
Stefan Reinauer838c5a52010-01-17 14:08:17 +0000103 // return -1;
104 }
105
Vladimir Serbinenkoea9181c2014-01-05 06:50:10 +0100106 udelay(10);
107
Sven Schnelle0238a9c2011-03-14 08:18:27 +0000108 data = inb(ec_data_reg);
Sven Schnelle5d9a83c2011-06-12 16:53:25 +0200109 printk(BIOS_SPEW, "recv_ec_data: 0x%02x\n", data);
Stefan Reinauer838c5a52010-01-17 14:08:17 +0000110
111 return data;
112}
113
114u8 ec_read(u8 addr)
115{
116 send_ec_command(0x80);
117 send_ec_data(addr);
118
119 return recv_ec_data();
120}
121
122int ec_write(u8 addr, u8 data)
123{
124 send_ec_command(0x81);
125 send_ec_data(addr);
126 return send_ec_data(data);
127}
128
Sven Schnellef9a0b922011-03-15 09:52:07 +0000129u8 ec_query(void)
130{
131 send_ec_command(0x84);
132 return recv_ec_data();
133}
134
Sven Schnelle8d94d7c2011-03-14 08:18:17 +0000135void ec_set_bit(u8 addr, u8 bit)
136{
137 ec_write(addr, ec_read(addr) | (1 << bit));
138}
139
140void ec_clr_bit(u8 addr, u8 bit)
141{
142 ec_write(addr, ec_read(addr) & ~(1 << bit));
143}
144
Vladimir Serbinenkofe50d0b2013-06-07 01:55:57 +0200145#ifndef __PRE_RAM__
146
Sven Schnelle0238a9c2011-03-14 08:18:27 +0000147void ec_set_ports(u16 cmd_reg, u16 data_reg)
148{
149 ec_cmd_reg = cmd_reg;
150 ec_data_reg = data_reg;
151}
152
Vladimir Serbinenkofe50d0b2013-06-07 01:55:57 +0200153#endif
154
Stefan Reinauer24d1d4b2013-03-21 11:51:41 -0700155#if !defined(__SMM__) && !defined(__PRE_RAM__)
Sven Schnelle7592e8b2011-01-27 11:43:03 +0000156struct chip_operations ec_acpi_ops = {
157 CHIP_NAME("ACPI Embedded Controller")
158};
Stefan Reinauer24d1d4b2013-03-21 11:51:41 -0700159#endif