2 * Copyright 2004 Tyan Computer
6 #include <device/smbus_def.h>
9 #define SMBHSTPRTCL 0x0
11 #define SMBXMITADD 0x2
12 #define SMBHSTDAT0 0x4
13 #define SMBHSTDAT1 0x5
16 * Between 1-10 seconds, We should never timeout normally.
17 * Longer than this is just painful when a timeout condition occurs.
19 #define SMBUS_TIMEOUT (100 * 1000 * 10)
21 static inline void smbus_delay(void)
27 /* Not needed, upon write to PRTCL, the status will be auto-cleared. */
28 static int smbus_wait_until_ready(unsigned smbus_io_base)
31 loops = SMBUS_TIMEOUT;
35 val = inb(smbus_io_base + SMBHSTSTAT);
39 outb(val, smbus_io_base + SMBHSTSTAT);
45 static int smbus_wait_until_done(unsigned smbus_io_base)
48 loops = SMBUS_TIMEOUT;
52 val = inb(smbus_io_base + SMBHSTSTAT);
53 if ((val & 0xff) != 0)
60 static int do_smbus_recv_byte(unsigned smbus_io_base, unsigned device)
62 unsigned char global_status_register, byte;
65 /* Not needed, upon write to PRTCL, the status will be auto-cleared. */
66 if (smbus_wait_until_ready(smbus_io_base) < 0)
70 /* Set the device I'm talking to. */
71 outb(((device & 0x7f) << 1) | 1, smbus_io_base + SMBXMITADD);
74 /* Set the command/address. */
75 outb(0, smbus_io_base + SMBHSTCMD);
79 outb(0x05, smbus_io_base + SMBHSTPRTCL);
82 /* Poll for transaction completion. */
83 if (smbus_wait_until_done(smbus_io_base) < 0)
87 global_status_register = inb(smbus_io_base + SMBHSTSTAT) & 0x80;
89 /* Read results of transaction. */
90 byte = inb(smbus_io_base + SMBHSTDAT0);
92 /* Lose check, otherwise it should be 0. */
93 if (global_status_register != 0x80)
99 static int do_smbus_send_byte(unsigned smbus_io_base, unsigned device,
102 unsigned global_status_register;
105 /* Not needed, upon write to PRTCL, the status will be auto-cleared. */
106 if (smbus_wait_until_ready(smbus_io_base) < 0)
110 outb(val, smbus_io_base + SMBHSTDAT0);
113 /* Set the device I'm talking to. */
114 outb(((device & 0x7f) << 1) | 0, smbus_io_base + SMBXMITADD);
117 outb(0, smbus_io_base + SMBHSTCMD);
120 /* Set up for a byte data write. */
121 outb(0x04, smbus_io_base + SMBHSTPRTCL);
124 /* Poll for transaction completion. */
125 if (smbus_wait_until_done(smbus_io_base) < 0)
129 global_status_register = inb(smbus_io_base + SMBHSTSTAT) & 0x80;
131 if (global_status_register != 0x80)
138 static int do_smbus_read_byte(unsigned smbus_io_base, unsigned device,
141 unsigned char global_status_register, byte;
144 /* Not needed, upon write to PRTCL, the status will be auto-cleared. */
145 if (smbus_wait_until_ready(smbus_io_base) < 0)
149 /* Set the device I'm talking to. */
150 outb(((device & 0x7f) << 1) | 1, smbus_io_base + SMBXMITADD);
153 /* Set the command/address. */
154 outb(address & 0xff, smbus_io_base + SMBHSTCMD);
158 outb(0x07, smbus_io_base + SMBHSTPRTCL);
161 /* Poll for transaction completion. */
162 if (smbus_wait_until_done(smbus_io_base) < 0)
166 global_status_register = inb(smbus_io_base + SMBHSTSTAT) & 0x80;
168 /* Read results of transaction. */
169 byte = inb(smbus_io_base + SMBHSTDAT0);
171 /* Lose check, otherwise it should be 0. */
172 if (global_status_register != 0x80)
178 static int do_smbus_write_byte(unsigned smbus_io_base, unsigned device,
179 unsigned address, unsigned char val)
181 unsigned global_status_register;
184 /* Not needed, upon write to PRTCL, the status will be auto-cleared. */
185 if (smbus_wait_until_ready(smbus_io_base) < 0)
189 outb(val, smbus_io_base + SMBHSTDAT0);
192 /* Set the device I'm talking to. */
193 outb(((device & 0x7f) << 1) | 0, smbus_io_base + SMBXMITADD);
196 outb(address & 0xff, smbus_io_base + SMBHSTCMD);
199 /* Set up for a byte data write. */
200 outb(0x06, smbus_io_base + SMBHSTPRTCL);
203 /* Poll for transaction completion. */
204 if (smbus_wait_until_done(smbus_io_base) < 0)
208 global_status_register = inb(smbus_io_base + SMBHSTSTAT) & 0x80;
210 if (global_status_register != 0x80)