02420ef75be36c0d75127f5a571d1cad57e24077
[coreboot.git] / src / southbridge / intel / i82801cx / i82801cx_early_smbus.c
1 #include <device/pci_ids.h>
2 #include "i82801cx.h"
3
4 static void enable_smbus(void)
5 {
6         device_t dev = PCI_DEV(0x0, 0x1f, 0x3);
7
8         print_debug("SMBus controller enabled\n");
9         /* set smbus iobase */
10         pci_write_config32(dev, SMB_BASE, SMBUS_IO_BASE | PCI_BASE_ADDRESS_SPACE_IO);
11         /* Set smbus enable */
12         pci_write_config8(dev, HOSTC, HST_EN);
13         /* Set smbus iospace enable */ 
14         pci_write_config16(dev, PCI_COMMAND, PCI_COMMAND_IO);
15         /* Disable interrupt generation */ 
16         outb(0, SMBUS_IO_BASE + SMBHSTCTL);
17         /* clear any lingering errors, so the transaction will run */
18         outb(inb(SMBUS_IO_BASE + SMBHSTSTAT), SMBUS_IO_BASE + SMBHSTSTAT);
19 }
20
21
22 static inline void smbus_delay(void)
23 {
24         outb(0x80, 0x80);
25 }
26
27 // See http://www.coreboot.org/pipermail/linuxbios/2004-September/009077.html
28 // for a description of this function.
29 static int smbus_wait_until_active(void)
30 {
31         unsigned long loops;
32         loops = SMBUS_TIMEOUT;
33         do {
34                 unsigned char val;
35                 smbus_delay();
36                 val = inb(SMBUS_IO_BASE + SMBHSTSTAT);
37                 if ((val & 1)) {
38                         break;
39                 }
40         } while (--loops);
41         return loops ? 0 : -4;
42 }
43
44 static int smbus_wait_until_ready(void)
45 {
46         unsigned long loops;
47         loops = SMBUS_TIMEOUT;
48         do {
49                 unsigned char val;
50                 smbus_delay();
51                 val = inb(SMBUS_IO_BASE + SMBHSTSTAT);
52                 // !HOST_BUSY?
53                 if ((val & 1) == 0) {
54                         break;
55                 }
56                 if(loops == (SMBUS_TIMEOUT / 2)) {
57                         // Clear status flags
58                         outb(inb(SMBUS_IO_BASE + SMBHSTSTAT), 
59                                 SMBUS_IO_BASE + SMBHSTSTAT);
60                 }
61         } while(--loops);
62         return loops?0:-2;
63 }
64
65 static int smbus_wait_until_done(void)
66 {
67         unsigned long loops;
68         loops = SMBUS_TIMEOUT;
69         do {
70                 unsigned char val;
71                 smbus_delay();
72                 
73                 val = inb(SMBUS_IO_BASE + SMBHSTSTAT);
74                 // !HOST_BUSY?
75                 if ( (val & 1) == 0) {
76                         break;
77                 }
78                 // BYTE_DONE or SUCCESS or error?
79                 if ((val & ~((1<<6)|(1<<0)) ) != 0 ) {
80                         break;
81                 }
82         } while(--loops);
83         return loops?0:-3;
84 }
85
86 static int smbus_read_byte(unsigned device, unsigned address)
87 {
88         unsigned char global_control_register;
89         unsigned char global_status_register;
90         unsigned char byte;
91
92         if (smbus_wait_until_ready() < 0) {
93                 return -2;
94         }
95         
96         /* setup transaction */
97         /* disable interrupts */
98         outb(inb(SMBUS_IO_BASE + SMBHSTCTL) & 0xfe, SMBUS_IO_BASE + SMBHSTCTL);
99         /* set to read from the specified device  */
100         outb(((device & 0x7f) << 1) | 1, SMBUS_IO_BASE + SMBXMITADD);
101         /* set the command/address... */
102         outb(address & 0xFF, SMBUS_IO_BASE + SMBHSTCMD);
103         /* set up for a byte data read */
104         outb((inb(SMBUS_IO_BASE + SMBHSTCTL) & 0xe3) | (0x2<<2), SMBUS_IO_BASE + SMBHSTCTL);
105
106         /* clear any lingering errors, so the transaction will run */
107         outb(inb(SMBUS_IO_BASE + SMBHSTSTAT), SMBUS_IO_BASE + SMBHSTSTAT);
108
109         /* clear the data byte...*/
110         outb(0, SMBUS_IO_BASE + SMBHSTDAT0);
111
112         /* start a byte read, with interrupts disabled */
113         outb((inb(SMBUS_IO_BASE + SMBHSTCTL) | 0x40), SMBUS_IO_BASE + SMBHSTCTL);
114         /* poll for it to start */
115         if (smbus_wait_until_active() < 0) {
116                 return -4;
117         }
118
119         /* poll for transaction completion */
120         if (smbus_wait_until_done() < 0) {
121                 return -3;
122         }
123
124         global_status_register = inb(SMBUS_IO_BASE + SMBHSTSTAT) & ~(1<<6); /* Ignore the In Use Status... */
125
126         /* read results of transaction */
127         byte = inb(SMBUS_IO_BASE + SMBHSTDAT0);
128
129         // SUCCESS?
130         if (global_status_register != 2) {
131                 return -1;
132         }
133         return byte;
134 }