blob: c6e282a015756e811071a7cc2fa053faa06c9db3 [file] [log] [blame]
Stefan Reinauer020b22a2012-03-30 17:06:43 -07001/*
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.
Stefan Reinauer020b22a2012-03-30 17:06:43 -070015 */
16
Edward O'Callaghanb57fef92014-06-17 20:13:08 +100017#include <arch/io.h>
Stefan Reinauer020b22a2012-03-30 17:06:43 -070018#include <console/console.h>
19#include <device/device.h>
Stefan Reinauer020b22a2012-03-30 17:06:43 -070020#include <delay.h>
Edward O'Callaghanb57fef92014-06-17 20:13:08 +100021
Stefan Reinauer020b22a2012-03-30 17:06:43 -070022#include "ec.h"
23#include "chip.h"
24
25static u16 ec_cmd_reg = 0;
26static u16 ec_data_reg = 0;
27
28static inline u8 __ec_read(u8 addr)
29{
30 outb(addr, ec_cmd_reg);
31 return inb(ec_data_reg);
32}
33
34static inline void __ec_write(u8 addr, u8 data)
35{
36 outb(addr, ec_cmd_reg);
37 outb(data, ec_data_reg);
38}
39
40static int ec_ready(void)
41{
42 u16 timeout = EC_TIMEOUT;
43
44 if (!ec_cmd_reg || !ec_data_reg) {
45 printk(BIOS_DEBUG, "Invalid ports: cmd=0x%x data=0x%x\n",
46 ec_cmd_reg, ec_data_reg);
47 return -1;
48 }
49
50 while (__ec_read(EC_MAILBOX_COMMAND) != 0 && --timeout) {
51 udelay(10);
52 if ((timeout & 0xff) == 0)
53 printk(BIOS_SPEW, ".");
54 }
55 if (!timeout) {
56 printk(BIOS_DEBUG, "Timeout waiting for EC to be ready.\n");
57 return -1;
58 }
59 return 0;
60}
61
62int send_ec_command(u8 command)
63{
64 if (ec_ready() < 0)
65 return -1;
66 __ec_write(EC_MAILBOX_COMMAND, command);
67 return ec_ready();
68}
69
70int send_ec_command_data(u8 command, u8 data)
71{
72 if (ec_ready() < 0)
73 return -1;
74 __ec_write(EC_MAILBOX_DATA, data);
75 __ec_write(EC_MAILBOX_COMMAND, command);
76 return ec_ready();
77}
78
79u8 read_ec_command_byte(u8 command)
80{
81 send_ec_command(command);
82 return __ec_read(EC_MAILBOX_DATA);
83}
84
85u8 ec_read(u8 addr)
86{
87 if (send_ec_command_data(EC_RAM_READ, addr) < 0)
88 return 0;
89 return __ec_read(EC_MAILBOX_DATA);
90}
91
92int ec_write(u8 addr, u8 data)
93{
94 if (ec_ready() < 0)
95 return -1;
96 __ec_write(EC_MAILBOX_DATA, addr);
97 __ec_write(EC_MAILBOX_DATA_H, data);
98 __ec_write(EC_MAILBOX_COMMAND, EC_RAM_WRITE);
99 return ec_ready();
100}
101
102void ec_set_bit(u8 addr, u8 bit)
103{
104 ec_write(addr, ec_read(addr) | (1 << bit));
105}
106
107void ec_clr_bit(u8 addr, u8 bit)
108{
109 ec_write(addr, ec_read(addr) & ~(1 << bit));
110}
111
112void ec_set_ports(u16 cmd_reg, u16 data_reg)
113{
114 ec_cmd_reg = cmd_reg;
115 ec_data_reg = data_reg;
116}
117
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +1100118static void mec1308_enable(struct device *dev)
Stefan Reinauer020b22a2012-03-30 17:06:43 -0700119{
Kyösti Mälkki07841c22019-08-17 15:14:38 +0300120 DEVTREE_CONST struct ec_smsc_mec1308_config *conf = dev->chip_info;
Stefan Reinauer020b22a2012-03-30 17:06:43 -0700121
122 if (conf->mailbox_port) {
123 ec_cmd_reg = conf->mailbox_port;
124 ec_data_reg = conf->mailbox_port + 1;
125 }
126}
127
128struct chip_operations ec_smsc_mec1308_ops = {
129 CHIP_NAME("SMSC MEC1308 EC Mailbox Interface")
130 .enable_dev = mec1308_enable
131};