2 * This file is part of the coreboot project.
4 * Copyright (C) 2005 Yinghai Lu <yinghailu@gmail.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
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 static void smbus_delay(void)
28 static int smbus_wait_until_ready(u16 smbus_io_base)
30 unsigned loops = SMBUS_TIMEOUT;
36 byte = inb(smbus_io_base + SMBHSTSTAT);
38 return loops ? 0 : -1;
41 static int smbus_wait_until_done(u16 smbus_io_base)
43 unsigned loops = SMBUS_TIMEOUT;
49 byte = inb(smbus_io_base + SMBHSTSTAT);
50 } while ((byte & 1) || (byte & ~((1 << 6) | (1 << 0))) == 0);
51 return loops ? 0 : -1;
54 static int do_smbus_read_byte(u16 smbus_io_base, u8 device, u8 address)
56 unsigned char global_status_register;
59 if (smbus_wait_until_ready(smbus_io_base) < 0) {
60 return SMBUS_WAIT_UNTIL_READY_TIMEOUT;
62 /* Setup transaction */
63 /* Disable interrupts */
64 outb(inb(smbus_io_base + SMBHSTCTL) & (~1), SMBUS_IO_BASE + SMBHSTCTL);
65 /* Set the device I'm talking too */
66 outb(((device & 0x7f) << 1) | 1, smbus_io_base + SMBXMITADD);
67 /* Set the command/address... */
68 outb(address & 0xff, smbus_io_base + SMBHSTCMD);
69 /* Set up for a byte data read */
70 outb((inb(smbus_io_base + SMBHSTCTL) & 0xe3) | (0x2 << 2),
71 (smbus_io_base + SMBHSTCTL));
72 /* Clear any lingering errors, so the transaction will run */
73 outb(inb(smbus_io_base + SMBHSTSTAT), SMBUS_IO_BASE + SMBHSTSTAT);
75 /* Clear the data byte... */
76 outb(0, smbus_io_base + SMBHSTDAT0);
78 /* Start the command */
79 outb((inb(smbus_io_base + SMBHSTCTL) | 0x40),
80 smbus_io_base + SMBHSTCTL);
82 /* Poll for transaction completion */
83 if (smbus_wait_until_done(smbus_io_base) < 0) {
84 return SMBUS_WAIT_UNTIL_DONE_TIMEOUT;
87 global_status_register = inb(smbus_io_base + SMBHSTSTAT);
89 /* Ignore the "In Use" status... */
90 global_status_register &= ~(3 << 5);
92 /* Read results of transaction */
93 byte = inb(smbus_io_base + SMBHSTDAT0);
94 if (global_status_register != (1 << 1)) {