1 #include <device/smbus_def.h>
3 #define SMBHST_STATUS 0x0
6 #define SMBHST_ADDR 0x4
9 #define SMBUS_TIMEOUT (100*1000*10)
10 #define SMBUS_STATUS_MASK 0x1e
11 #define SMBUS_ERROR_FLAG (1<<2)
13 void enable_smbus(void);
14 int do_smbus_read_byte(unsigned smbus_io_base, unsigned device, unsigned address);
16 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 + SMBHST_STATUS);
34 if ((val & 0x1) == 0) {
38 if(loops == (SMBUS_TIMEOUT / 2)) {
39 outw(inw(smbus_io_base + SMBHST_STATUS),
40 smbus_io_base + SMBHST_STATUS);
44 return loops?0:SMBUS_WAIT_UNTIL_READY_TIMEOUT;
47 static int smbus_wait_until_done(unsigned smbus_io_base)
50 loops = SMBUS_TIMEOUT;
55 val = inb(smbus_io_base + SMBHST_STATUS);
56 // Make sure the command is done
57 if ((val & 0x1) != 0) {
60 // Don't break out until one of the interrupt
66 return loops?0:SMBUS_WAIT_UNTIL_DONE_TIMEOUT;
69 int do_smbus_read_byte(unsigned smbus_io_base, unsigned device, unsigned address)
71 unsigned status_register;
74 if (smbus_wait_until_ready(smbus_io_base) < 0) {
75 return SMBUS_WAIT_UNTIL_READY_TIMEOUT;
78 /* setup transaction */
80 /* clear any lingering errors, so the transaction will run */
81 outb(0x1e, smbus_io_base + SMBHST_STATUS);
83 /* set the device I'm talking too */
84 outb(((device & 0x7f) << 1) | 1, smbus_io_base + SMBHST_ADDR);
86 /* set the command/address... */
87 outb(address & 0xff, smbus_io_base + SMBHST_CMD);
89 /* clear the data word...*/
90 outb(0, smbus_io_base + SMBHST_DAT);
92 /* start a byte read with interrupts disabled */
93 outb( (0x02 << 2)|(1<<6), smbus_io_base + SMBHST_CTL);
95 /* poll for transaction completion */
96 if (smbus_wait_until_done(smbus_io_base) < 0) {
97 return SMBUS_WAIT_UNTIL_DONE_TIMEOUT;
100 status_register = inw(smbus_io_base + SMBHST_STATUS);
102 /* read results of transaction */
103 byte = inw(smbus_io_base + SMBHST_DAT) & 0xff;
105 if (status_register & 0x04) {
107 print_debug("Read fail ");
108 print_debug_hex16(status_register);