5157609639b68bc8fd4139064f47b39d0546c4fc
[coreboot.git] / src / southbridge / amd / amd8111 / amd8111_early_smbus.c
1 #define SMBUS_IO_BASE 0x0f00
2
3 #define SMBGSTATUS 0xe0
4 #define SMBGCTL    0xe2
5 #define SMBHSTADDR 0xe4
6 #define SMBHSTDAT  0xe6
7 #define SMBHSTCMD  0xe8
8 #define SMBHSTFIFO 0xe9
9
10 #define SMBUS_TIMEOUT (100*1000*10)
11
12 static void enable_smbus(void)
13 {
14         device_t dev;
15         dev = pci_locate_device(PCI_ID(0x1022, 0x746b), 0);
16         if (dev == PCI_DEV_INVALID) {
17                 die("SMBUS controller not found\r\n");
18         }
19         uint8_t enable;
20         print_spew("SMBus controller enabled\r\n");
21         pci_write_config32(dev, 0x58, SMBUS_IO_BASE | 1);
22         enable = pci_read_config8(dev, 0x41);
23         pci_write_config8(dev, 0x41, enable | (1 << 7));
24         /* clear any lingering errors, so the transaction will run */
25         outw(inw(SMBUS_IO_BASE + SMBGSTATUS), SMBUS_IO_BASE + SMBGSTATUS);
26 }
27
28
29 static inline void smbus_delay(void)
30 {
31         outb(0x80, 0x80);
32 }
33
34 static int smbus_wait_until_ready(void)
35 {
36         unsigned long loops;
37         loops = SMBUS_TIMEOUT;
38         do {
39                 unsigned short val;
40                 smbus_delay();
41                 val = inw(SMBUS_IO_BASE + SMBGSTATUS);
42                 if ((val & 0x800) == 0) {
43                         break;
44                 }
45                 if(loops == (SMBUS_TIMEOUT / 2)) {
46                         outw(inw(SMBUS_IO_BASE + SMBGSTATUS), 
47                                 SMBUS_IO_BASE + SMBGSTATUS);
48                 }
49         } while(--loops);
50         return loops?0:-2;
51 }
52
53 static int smbus_wait_until_done(void)
54 {
55         unsigned long loops;
56         loops = SMBUS_TIMEOUT;
57         do {
58                 unsigned short val;
59                 smbus_delay();
60                 
61                 val = inw(SMBUS_IO_BASE + SMBGSTATUS);
62                 if (((val & 0x8) == 0) | ((val & 0x437) != 0)) {
63                         break;
64                 }
65         } while(--loops);
66         return loops?0:-3;
67 }
68
69 static int smbus_read_byte(unsigned device, unsigned address)
70 {
71         unsigned char global_control_register;
72         unsigned char global_status_register;
73         unsigned char byte;
74
75         if (smbus_wait_until_ready() < 0) {
76                 return -2;
77         }
78         
79         /* setup transaction */
80         /* disable interrupts */
81         outw(inw(SMBUS_IO_BASE + SMBGCTL) & ~((1<<10)|(1<<9)|(1<<8)|(1<<4)), SMBUS_IO_BASE + SMBGCTL);
82         /* set the device I'm talking too */
83         outw(((device & 0x7f) << 1) | 1, SMBUS_IO_BASE + SMBHSTADDR);
84         /* set the command/address... */
85         outb(address & 0xFF, SMBUS_IO_BASE + SMBHSTCMD);
86         /* set up for a byte data read */
87         outw((inw(SMBUS_IO_BASE + SMBGCTL) & ~7) | (0x2), SMBUS_IO_BASE + SMBGCTL);
88
89         /* clear any lingering errors, so the transaction will run */
90         /* Do I need to write the bits to a 1 to clear an error? */
91         outw(inw(SMBUS_IO_BASE + SMBGSTATUS), SMBUS_IO_BASE + SMBGSTATUS);
92
93         /* clear the data word...*/
94         outw(0, SMBUS_IO_BASE + SMBHSTDAT);
95
96         /* start the command */
97         outw((inw(SMBUS_IO_BASE + SMBGCTL) | (1 << 3)), SMBUS_IO_BASE + SMBGCTL);
98
99
100         /* poll for transaction completion */
101         if (smbus_wait_until_done() < 0) {
102                 return -3;
103         }
104
105         global_status_register = inw(SMBUS_IO_BASE + SMBGSTATUS);
106
107         /* read results of transaction */
108         byte = inw(SMBUS_IO_BASE + SMBHSTDAT) & 0xff;
109
110         if (global_status_register != (1 << 4)) {
111                 return -1;
112         }
113         return byte;
114 }
115
116 static void smbus_write_byte(unsigned device, unsigned address, unsigned char val)
117 {
118         if (smbus_wait_until_ready() < 0) {
119                 return;
120         }
121
122         /* by LYH */
123         outb(0x37,SMBUS_IO_BASE + SMBGSTATUS);
124         /* set the device I'm talking too */
125         outw(((device & 0x7f) << 1) | 0, SMBUS_IO_BASE + SMBHSTADDR);
126
127         /* data to send */
128         outb(val, SMBUS_IO_BASE + SMBHSTDAT);
129
130         outb(address & 0xFF, SMBUS_IO_BASE + SMBHSTCMD);
131
132         /* start the command */
133         outb(0xa, SMBUS_IO_BASE + SMBGCTL);
134
135         /* poll for transaction completion */
136         smbus_wait_until_done();
137         return;
138 }