3 * by yinghai.lu@amd.com
5 #include <device/smbus_def.h>
11 #define SMBHSTADDR 0x4
12 #define SMBHSTDAT0 0x5
13 #define SMBHSTDAT1 0x6
14 #define SMBHSTBLKDAT 0x7
16 #define SMBSLVCTRL 0x8
17 #define SMBSLVCMD_SHADOW 0x9
22 /* Between 1-10 seconds, We should never timeout normally
23 * Longer than this is just painful when a timeout condition occurs.
25 #define SMBUS_TIMEOUT (100*1000*10)
27 static inline void smbus_delay(void)
32 static int smbus_wait_until_ready(unsigned smbus_io_base)
35 loops = SMBUS_TIMEOUT;
38 val = inb(smbus_io_base + SMBHSTSTAT);
40 if (val == 0) { // ready now
43 outb(val, smbus_io_base + SMBHSTSTAT);
45 return -2; // time out
48 static int smbus_wait_until_done(unsigned smbus_io_base)
51 loops = SMBUS_TIMEOUT;
55 val = inb(smbus_io_base + SMBHSTSTAT);
56 val &= 0x1f; // mask off reserved bits
61 outb(val, smbus_io_base + SMBHSTSTAT); // clear status
68 static int do_smbus_recv_byte(unsigned smbus_io_base, unsigned device)
72 if (smbus_wait_until_ready(smbus_io_base) < 0) {
73 return -2; // not ready
76 /* set the device I'm talking too */
77 outb(((device & 0x7f) << 1)|1 , smbus_io_base + SMBHSTADDR);
79 byte = inb(smbus_io_base + SMBHSTCTRL);
80 byte &= 0xe3; // Clear [4:2]
81 byte |= (1<<2) | (1<<6); // Byte data read/write command, start the command
82 outb(byte, smbus_io_base + SMBHSTCTRL);
84 /* poll for transaction completion */
85 if (smbus_wait_until_done(smbus_io_base) < 0) {
86 return -3; // timeout or error
89 /* read results of transaction */
90 byte = inb(smbus_io_base + SMBHSTCMD);
95 static int do_smbus_send_byte(unsigned smbus_io_base, unsigned device, unsigned char val)
99 if (smbus_wait_until_ready(smbus_io_base) < 0) {
100 return -2; // not ready
103 /* set the command... */
104 outb(val, smbus_io_base + SMBHSTCMD);
106 /* set the device I'm talking too */
107 outb(((device & 0x7f) << 1)|0 , smbus_io_base + SMBHSTADDR);
109 byte = inb(smbus_io_base + SMBHSTCTRL);
110 byte &= 0xe3; // Clear [4:2]
111 byte |= (1<<2) | (1<<6); // Byte data read/write command, start the command
112 outb(byte, smbus_io_base + SMBHSTCTRL);
114 /* poll for transaction completion */
115 if (smbus_wait_until_done(smbus_io_base) < 0) {
116 return -3; // timeout or error
124 static int do_smbus_read_byte(unsigned smbus_io_base, unsigned device, unsigned address)
128 if (smbus_wait_until_ready(smbus_io_base) < 0) {
129 return -2; // not ready
132 /* set the command/address... */
133 outb(address & 0xff, smbus_io_base + SMBHSTCMD);
135 /* set the device I'm talking too */
136 outb(((device & 0x7f) << 1)|1 , smbus_io_base + SMBHSTADDR);
138 byte = inb(smbus_io_base + SMBHSTCTRL);
139 byte &= 0xe3; // Clear [4:2]
140 byte |= (1<<3) | (1<<6); // Byte data read/write command, start the command
141 outb(byte, smbus_io_base + SMBHSTCTRL);
143 /* poll for transaction completion */
144 if (smbus_wait_until_done(smbus_io_base) < 0) {
145 return -3; // timeout or error
148 /* read results of transaction */
149 byte = inb(smbus_io_base + SMBHSTDAT0);
153 static int do_smbus_write_byte(unsigned smbus_io_base, unsigned device, unsigned address, unsigned char val)
157 if (smbus_wait_until_ready(smbus_io_base) < 0) {
158 return -2; // not ready
161 /* set the command/address... */
162 outb(address & 0xff, smbus_io_base + SMBHSTCMD);
164 /* set the device I'm talking too */
165 outb(((device & 0x7f) << 1)|0 , smbus_io_base + SMBHSTADDR);
168 outb(val, smbus_io_base + SMBHSTDAT0);
170 byte = inb(smbus_io_base + SMBHSTCTRL);
171 byte &= 0xe3; // Clear [4:2]
172 byte |= (1<<3) | (1<<6); // Byte data read/write command, start the command
173 outb(byte, smbus_io_base + SMBHSTCTRL);
175 /* poll for transaction completion */
176 if (smbus_wait_until_done(smbus_io_base) < 0) {
177 return -3; // timeout or error