Add EC component for SMSC MEC1308/1310
[coreboot.git] / src / ec / smsc / mec1308 / ec.c
1 /*
2  * This file is part of the coreboot project.
3  *
4  * Copyright (C) 2011 The Chromium OS Authors. All rights reserved.
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 #include "chip.h"
28
29 static u16 ec_cmd_reg = 0;
30 static u16 ec_data_reg = 0;
31
32 static inline u8 __ec_read(u8 addr)
33 {
34         outb(addr, ec_cmd_reg);
35         return inb(ec_data_reg);
36 }
37
38 static inline void __ec_write(u8 addr, u8 data)
39 {
40         outb(addr, ec_cmd_reg);
41         outb(data, ec_data_reg);
42 }
43
44 static int ec_ready(void)
45 {
46         u16 timeout = EC_TIMEOUT;
47
48         if (!ec_cmd_reg || !ec_data_reg) {
49                 printk(BIOS_DEBUG, "Invalid ports: cmd=0x%x data=0x%x\n",
50                        ec_cmd_reg, ec_data_reg);
51                 return -1;
52         }
53
54         while (__ec_read(EC_MAILBOX_COMMAND) != 0 && --timeout) {
55                 udelay(10);
56                 if ((timeout & 0xff) == 0)
57                         printk(BIOS_SPEW, ".");
58         }
59         if (!timeout) {
60                 printk(BIOS_DEBUG, "Timeout waiting for EC to be ready.\n");
61                 return -1;
62         }
63         return 0;
64 }
65
66 int send_ec_command(u8 command)
67 {
68         if (ec_ready() < 0)
69                 return -1;
70         __ec_write(EC_MAILBOX_COMMAND, command);
71         return ec_ready();
72 }
73
74 int send_ec_command_data(u8 command, u8 data)
75 {
76         if (ec_ready() < 0)
77                 return -1;
78         __ec_write(EC_MAILBOX_DATA, data);
79         __ec_write(EC_MAILBOX_COMMAND, command);
80         return ec_ready();
81 }
82
83 u8 read_ec_command_byte(u8 command)
84 {
85         send_ec_command(command);
86         return __ec_read(EC_MAILBOX_DATA);
87 }
88
89 u8 ec_read(u8 addr)
90 {
91         if (send_ec_command_data(EC_RAM_READ, addr) < 0)
92                 return 0;
93         return __ec_read(EC_MAILBOX_DATA);
94 }
95
96 int ec_write(u8 addr, u8 data)
97 {
98         if (ec_ready() < 0)
99                 return -1;
100         __ec_write(EC_MAILBOX_DATA, addr);
101         __ec_write(EC_MAILBOX_DATA_H, data);
102         __ec_write(EC_MAILBOX_COMMAND, EC_RAM_WRITE);
103         return ec_ready();
104 }
105
106 void ec_set_bit(u8 addr, u8 bit)
107 {
108         ec_write(addr, ec_read(addr) | (1 << bit));
109 }
110
111 void ec_clr_bit(u8 addr, u8 bit)
112 {
113         ec_write(addr, ec_read(addr) &  ~(1 << bit));
114 }
115
116 void ec_set_ports(u16 cmd_reg, u16 data_reg)
117 {
118         ec_cmd_reg = cmd_reg;
119         ec_data_reg = data_reg;
120 }
121
122 static void mec1308_enable(device_t dev)
123 {
124         struct ec_smsc_mec1308_config *conf = dev->chip_info;
125
126         if (conf->mailbox_port) {
127                 ec_cmd_reg = conf->mailbox_port;
128                 ec_data_reg = conf->mailbox_port + 1;
129         }
130 }
131
132 struct chip_operations ec_smsc_mec1308_ops = {
133         CHIP_NAME("SMSC MEC1308 EC Mailbox Interface")
134         .enable_dev = mec1308_enable
135 };