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)
26 static int smbus_wait_until_ready(unsigned smbus_io_base)
29 loops = SMBUS_TIMEOUT;
33 val = inb(smbus_io_base + SMBHSTSTAT);
37 outb(val, smbus_io_base + SMBHSTSTAT);
42 static int smbus_wait_until_done(unsigned smbus_io_base)
45 loops = SMBUS_TIMEOUT;
49 val = inb(smbus_io_base + SMBHSTSTAT);
50 if ((val & 0xff) != 0)
56 static int do_smbus_recv_byte(unsigned smbus_io_base, unsigned device)
58 unsigned char global_status_register, byte;
61 /* Not needed, upon write to PRTCL, the status will be auto-cleared. */
62 if (smbus_wait_until_ready(smbus_io_base) < 0)
66 /* Set the device I'm talking to. */
67 outb(((device & 0x7f) << 1) | 1, smbus_io_base + SMBXMITADD);
70 /* Set the command/address. */
71 outb(0, smbus_io_base + SMBHSTCMD);
75 outb(0x05, smbus_io_base + SMBHSTPRTCL);
78 /* Poll for transaction completion. */
79 if (smbus_wait_until_done(smbus_io_base) < 0)
83 global_status_register = inb(smbus_io_base + SMBHSTSTAT) & 0x80;
85 /* Read results of transaction. */
86 byte = inb(smbus_io_base + SMBHSTDAT0);
88 /* Lose check, otherwise it should be 0. */
89 if (global_status_register != 0x80)
95 static int do_smbus_send_byte(unsigned smbus_io_base, unsigned device,
98 unsigned global_status_register;
101 /* Not needed, upon write to PRTCL, the status will be auto-cleared. */
102 if (smbus_wait_until_ready(smbus_io_base) < 0)
106 outb(val, smbus_io_base + SMBHSTDAT0);
109 /* Set the device I'm talking to. */
110 outb(((device & 0x7f) << 1) | 0, smbus_io_base + SMBXMITADD);
113 outb(0, smbus_io_base + SMBHSTCMD);
116 /* Set up for a byte data write. */
117 outb(0x04, smbus_io_base + SMBHSTPRTCL);
120 /* Poll for transaction completion. */
121 if (smbus_wait_until_done(smbus_io_base) < 0)
125 global_status_register = inb(smbus_io_base + SMBHSTSTAT) & 0x80;
127 if (global_status_register != 0x80)
133 static int do_smbus_read_byte(unsigned smbus_io_base, unsigned device,
136 unsigned char global_status_register, byte;
139 /* Not needed, upon write to PRTCL, the status will be auto-cleared. */
140 if (smbus_wait_until_ready(smbus_io_base) < 0)
144 /* Set the device I'm talking to. */
145 outb(((device & 0x7f) << 1) | 1, smbus_io_base + SMBXMITADD);
148 /* Set the command/address. */
149 outb(address & 0xff, smbus_io_base + SMBHSTCMD);
153 outb(0x07, smbus_io_base + SMBHSTPRTCL);
156 /* Poll for transaction completion. */
157 if (smbus_wait_until_done(smbus_io_base) < 0)
161 global_status_register = inb(smbus_io_base + SMBHSTSTAT) & 0x80;
163 /* Read results of transaction. */
164 byte = inb(smbus_io_base + SMBHSTDAT0);
166 /* Lose check, otherwise it should be 0. */
167 if (global_status_register != 0x80)
173 static int do_smbus_write_byte(unsigned smbus_io_base, unsigned device,
174 unsigned address, unsigned char val)
176 unsigned global_status_register;
179 /* Not needed, upon write to PRTCL, the status will be auto-cleared. */
180 if (smbus_wait_until_ready(smbus_io_base) < 0)
184 outb(val, smbus_io_base + SMBHSTDAT0);
187 /* Set the device I'm talking to. */
188 outb(((device & 0x7f) << 1) | 0, smbus_io_base + SMBXMITADD);
191 outb(address & 0xff, smbus_io_base + SMBHSTCMD);
194 /* Set up for a byte data write. */
195 outb(0x06, smbus_io_base + SMBHSTPRTCL);
198 /* Poll for transaction completion. */
199 if (smbus_wait_until_done(smbus_io_base) < 0)
203 global_status_register = inb(smbus_io_base + SMBHSTSTAT) & 0x80;
205 if (global_status_register != 0x80)