2 * This file is part of the LinuxBIOS project.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 #include <device/smbus_def.h>
21 #define SMBHSTSTAT 0x0
24 #define SMBXMITADD 0x4
25 #define SMBHSTDAT0 0x5
26 #define SMBHSTDAT1 0x6
28 #define SMBTRNSADD 0x9
29 #define SMBSLVDATA 0xa
30 #define SMLINK_PIN_CTL 0xe
31 #define SMBUS_PIN_CTL 0xf
33 #define SMBUS_TIMEOUT (100*1000*10)
36 static void smbus_delay(void)
41 static int smbus_wait_until_ready(unsigned smbus_io_base)
43 unsigned loops = SMBUS_TIMEOUT;
49 byte = inb(smbus_io_base + SMBHSTSTAT);
54 static int smbus_wait_until_done(unsigned smbus_io_base)
56 unsigned loops = SMBUS_TIMEOUT;
62 byte = inb(smbus_io_base + SMBHSTSTAT);
63 } while((byte & 1) || (byte & ~((1<<6)|(1<<0))) == 0);
67 static int smbus_wait_until_blk_done(unsigned smbus_io_base)
69 unsigned loops = SMBUS_TIMEOUT;
75 byte = inb(smbus_io_base + SMBHSTSTAT);
76 } while((byte&(1<<7)) == 0);
80 static int do_smbus_read_byte(unsigned smbus_io_base, unsigned device, unsigned address)
82 unsigned char global_status_register;
85 if (smbus_wait_until_ready(smbus_io_base) < 0) {
86 return SMBUS_WAIT_UNTIL_READY_TIMEOUT;
88 /* setup transaction */
89 /* disable interrupts */
90 outb(inb(smbus_io_base + SMBHSTCTL) & (~1), smbus_io_base + SMBHSTCTL);
91 /* set the device I'm talking too */
92 outb(((device & 0x7f) << 1) | 1, smbus_io_base + SMBXMITADD);
93 /* set the command/address... */
94 outb(address & 0xFF, smbus_io_base + SMBHSTCMD);
95 /* set up for a byte data read */
96 outb((inb(smbus_io_base + SMBHSTCTL) & 0xE3) | (0x2 << 2), smbus_io_base + SMBHSTCTL);
97 /* clear any lingering errors, so the transaction will run */
98 outb(inb(smbus_io_base + SMBHSTSTAT), smbus_io_base + SMBHSTSTAT);
100 /* clear the data byte...*/
101 outb(0, smbus_io_base + SMBHSTDAT0);
103 /* start the command */
104 outb((inb(smbus_io_base + SMBHSTCTL) | 0x40), smbus_io_base + SMBHSTCTL);
106 /* poll for transaction completion */
107 if (smbus_wait_until_done(smbus_io_base) < 0) {
108 return SMBUS_WAIT_UNTIL_DONE_TIMEOUT;
111 global_status_register = inb(smbus_io_base + SMBHSTSTAT);
113 /* Ignore the In Use Status... */
114 global_status_register &= ~(3 << 5);
116 /* read results of transaction */
117 byte = inb(smbus_io_base + SMBHSTDAT0);
118 if (global_status_register != (1 << 1)) {