2 * Copyright 2004 Tyan Computer
5 #include <device/smbus_def.h>
8 #define SMBHSTPRTCL 0x0
10 #define SMBXMITADD 0x2
11 #define SMBHSTDAT0 0x4
12 #define SMBHSTDAT1 0x5
14 /* Between 1-10 seconds, We should never timeout normally
15 * Longer than this is just painful when a timeout condition occurs.
17 #define SMBUS_TIMEOUT (100*1000*10)
19 static inline void smbus_delay(void)
24 static int smbus_wait_until_ready(unsigned smbus_io_base)
27 loops = SMBUS_TIMEOUT;
31 val = inb(smbus_io_base + SMBHSTSTAT);
36 outb(val,smbus_io_base + SMBHSTSTAT);
41 static int smbus_wait_until_done(unsigned smbus_io_base)
44 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;
61 // Don't need, when you write to PRTCL, the status will be cleared automatically
62 if (smbus_wait_until_ready(smbus_io_base) < 0) {
67 /* set the device I'm talking too */
68 outb(((device & 0x7f) << 1)|1 , smbus_io_base + SMBXMITADD);
70 /* set the command/address... */
71 outb(0, smbus_io_base + SMBHSTCMD);
74 outb(0x05, smbus_io_base + SMBHSTPRTCL);
77 /* poll for transaction completion */
78 if (smbus_wait_until_done(smbus_io_base) < 0) {
82 global_status_register = inb(smbus_io_base + SMBHSTSTAT) & 0x80; /* lose check */
84 /* read results of transaction */
85 byte = inb(smbus_io_base + SMBHSTDAT0);
87 if (global_status_register != 0x80) { // lose check, otherwise it should be 0
92 static int do_smbus_send_byte(unsigned smbus_io_base, unsigned device, unsigned char val)
94 unsigned global_status_register;
97 // Don't need, when you write to PRTCL, the status will be cleared automatically
98 if (smbus_wait_until_ready(smbus_io_base) < 0) {
103 outb(val, smbus_io_base + SMBHSTDAT0);
106 /* set the device I'm talking too */
107 outb(((device & 0x7f) << 1) | 0, smbus_io_base + SMBXMITADD);
110 outb(0, smbus_io_base + SMBHSTCMD);
113 /* set up for a byte data write */
114 outb(0x04, smbus_io_base + SMBHSTPRTCL);
117 /* poll for transaction completion */
118 if (smbus_wait_until_done(smbus_io_base) < 0) {
121 global_status_register = inb(smbus_io_base + SMBHSTSTAT) & 0x80; /* lose check */;
123 if (global_status_register != 0x80) {
128 static int do_smbus_read_byte(unsigned smbus_io_base, unsigned device, unsigned address)
130 unsigned char global_status_register;
133 // Don't need, when you write to PRTCL, the status will be cleared automatically
134 if (smbus_wait_until_ready(smbus_io_base) < 0) {
139 /* set the device I'm talking too */
140 outb(((device & 0x7f) << 1)|1 , smbus_io_base + SMBXMITADD);
142 /* set the command/address... */
143 outb(address & 0xff, smbus_io_base + SMBHSTCMD);
146 outb(0x07, smbus_io_base + SMBHSTPRTCL);
149 /* poll for transaction completion */
150 if (smbus_wait_until_done(smbus_io_base) < 0) {
154 global_status_register = inb(smbus_io_base + SMBHSTSTAT) & 0x80; /* lose check */
156 /* read results of transaction */
157 byte = inb(smbus_io_base + SMBHSTDAT0);
159 if (global_status_register != 0x80) { // lose check, otherwise it should be 0
166 static int do_smbus_write_byte(unsigned smbus_io_base, unsigned device, unsigned address, unsigned char val)
168 unsigned global_status_register;
171 // Don't need, when you write to PRTCL, the status will be cleared automatically
172 if (smbus_wait_until_ready(smbus_io_base) < 0) {
177 outb(val, smbus_io_base + SMBHSTDAT0);
180 /* set the device I'm talking too */
181 outb(((device & 0x7f) << 1) | 0, smbus_io_base + SMBXMITADD);
184 outb(address & 0xff, smbus_io_base + SMBHSTCMD);
187 /* set up for a byte data write */
188 outb(0x06, smbus_io_base + SMBHSTPRTCL);
191 /* poll for transaction completion */
192 if (smbus_wait_until_done(smbus_io_base) < 0) {
195 global_status_register = inb(smbus_io_base + SMBHSTSTAT) & 0x80; /* lose check */;
197 if (global_status_register != 0x80) {