2 * This file is part of the coreboot project.
4 * Copyright (C) 2004 Tyan Computer
5 * Written by Yinghai Lu <yhlu@tyan.com> for Tyan Computer.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; version 2 of the License.
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.
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, MA 02110-1301 USA
21 #include <device/smbus_def.h>
23 #define SMBHSTSTAT 0x1
24 #define SMBHSTPRTCL 0x0
26 #define SMBXMITADD 0x2
27 #define SMBHSTDAT0 0x4
28 #define SMBHSTDAT1 0x5
31 * Between 1-10 seconds, We should never timeout normally.
32 * Longer than this is just painful when a timeout condition occurs.
34 #define SMBUS_TIMEOUT (100 * 1000 * 10)
36 static inline void smbus_delay(void)
42 /* Not needed, upon write to PRTCL, the status will be auto-cleared. */
43 static int smbus_wait_until_ready(unsigned smbus_io_base)
46 loops = SMBUS_TIMEOUT;
50 val = inb(smbus_io_base + SMBHSTSTAT);
54 outb(val, smbus_io_base + SMBHSTSTAT);
60 static int smbus_wait_until_done(unsigned smbus_io_base)
63 loops = SMBUS_TIMEOUT;
67 val = inb(smbus_io_base + SMBHSTSTAT);
68 if ((val & 0xff) != 0)
75 static int do_smbus_recv_byte(unsigned smbus_io_base, unsigned device)
77 unsigned char global_status_register, byte;
80 /* Not needed, upon write to PRTCL, the status will be auto-cleared. */
81 if (smbus_wait_until_ready(smbus_io_base) < 0)
85 /* Set the device I'm talking to. */
86 outb(((device & 0x7f) << 1) | 1, smbus_io_base + SMBXMITADD);
89 /* Set the command/address. */
90 outb(0, smbus_io_base + SMBHSTCMD);
94 outb(0x05, smbus_io_base + SMBHSTPRTCL);
97 /* Poll for transaction completion. */
98 if (smbus_wait_until_done(smbus_io_base) < 0)
102 global_status_register = inb(smbus_io_base + SMBHSTSTAT) & 0x80;
104 /* Read results of transaction. */
105 byte = inb(smbus_io_base + SMBHSTDAT0);
107 /* Lose check, otherwise it should be 0. */
108 if (global_status_register != 0x80)
114 static int do_smbus_send_byte(unsigned smbus_io_base, unsigned device,
117 unsigned global_status_register;
120 /* Not needed, upon write to PRTCL, the status will be auto-cleared. */
121 if (smbus_wait_until_ready(smbus_io_base) < 0)
125 outb(val, smbus_io_base + SMBHSTDAT0);
128 /* Set the device I'm talking to. */
129 outb(((device & 0x7f) << 1) | 0, smbus_io_base + SMBXMITADD);
132 outb(0, smbus_io_base + SMBHSTCMD);
135 /* Set up for a byte data write. */
136 outb(0x04, smbus_io_base + SMBHSTPRTCL);
139 /* Poll for transaction completion. */
140 if (smbus_wait_until_done(smbus_io_base) < 0)
144 global_status_register = inb(smbus_io_base + SMBHSTSTAT) & 0x80;
146 if (global_status_register != 0x80)
153 static int do_smbus_read_byte(unsigned smbus_io_base, unsigned device,
156 unsigned char global_status_register, byte;
159 /* Not needed, upon write to PRTCL, the status will be auto-cleared. */
160 if (smbus_wait_until_ready(smbus_io_base) < 0)
164 /* Set the device I'm talking to. */
165 outb(((device & 0x7f) << 1) | 1, smbus_io_base + SMBXMITADD);
168 /* Set the command/address. */
169 outb(address & 0xff, smbus_io_base + SMBHSTCMD);
173 outb(0x07, smbus_io_base + SMBHSTPRTCL);
176 /* Poll for transaction completion. */
177 if (smbus_wait_until_done(smbus_io_base) < 0)
181 global_status_register = inb(smbus_io_base + SMBHSTSTAT) & 0x80;
183 /* Read results of transaction. */
184 byte = inb(smbus_io_base + SMBHSTDAT0);
186 /* Lose check, otherwise it should be 0. */
187 if (global_status_register != 0x80)
193 static int do_smbus_write_byte(unsigned smbus_io_base, unsigned device,
194 unsigned address, unsigned char val)
196 unsigned global_status_register;
199 /* Not needed, upon write to PRTCL, the status will be auto-cleared. */
200 if (smbus_wait_until_ready(smbus_io_base) < 0)
204 outb(val, smbus_io_base + SMBHSTDAT0);
207 /* Set the device I'm talking to. */
208 outb(((device & 0x7f) << 1) | 0, smbus_io_base + SMBXMITADD);
211 outb(address & 0xff, smbus_io_base + SMBHSTCMD);
214 /* Set up for a byte data write. */
215 outb(0x06, smbus_io_base + SMBHSTPRTCL);
218 /* Poll for transaction completion. */
219 if (smbus_wait_until_done(smbus_io_base) < 0)
223 global_status_register = inb(smbus_io_base + SMBHSTSTAT) & 0x80;
225 if (global_status_register != 0x80)