- A new test case for romcc
[coreboot.git] / src / southbridge / amd / amd8111 / amd8111_early_smbus.c
1 #define SMBUS_IO_BASE 0x1000
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_debug("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 }
25
26
27 static inline void smbus_delay(void)
28 {
29         outb(0x80, 0x80);
30 }
31
32 static int smbus_wait_until_ready(void)
33 {
34         unsigned long loops;
35         loops = SMBUS_TIMEOUT;
36         do {
37                 unsigned short val;
38                 smbus_delay();
39                 val = inw(SMBUS_IO_BASE + SMBGSTATUS);
40                 if ((val & 0x800) == 0) {
41                         break;
42                 }
43         } while(--loops);
44         return loops?0:-1;
45 }
46
47 static int smbus_wait_until_done(void)
48 {
49         unsigned long loops;
50         loops = SMBUS_TIMEOUT;
51         do {
52                 unsigned short val;
53                 smbus_delay();
54                 
55                 val = inw(SMBUS_IO_BASE + SMBGSTATUS);
56                 if (((val & 0x8) == 0) | ((val & 0x437) != 0)) {
57                         break;
58                 }
59         } while(--loops);
60         return loops?0:-1;
61 }
62
63 static int smbus_read_byte(unsigned device, unsigned address)
64 {
65         unsigned char global_control_register;
66         unsigned char global_status_register;
67         unsigned char byte;
68
69         if (smbus_wait_until_ready() < 0) {
70                 return -1;
71         }
72         
73         /* setup transaction */
74         /* disable interrupts */
75         outw(inw(SMBUS_IO_BASE + SMBGCTL) & ~((1<<10)|(1<<9)|(1<<8)|(1<<4)), SMBUS_IO_BASE + SMBGCTL);
76         /* set the device I'm talking too */
77         outw(((device & 0x7f) << 1) | 1, SMBUS_IO_BASE + SMBHSTADDR);
78         /* set the command/address... */
79         outb(address & 0xFF, SMBUS_IO_BASE + SMBHSTCMD);
80         /* set up for a byte data read */
81         outw((inw(SMBUS_IO_BASE + SMBGCTL) & ~7) | (0x2), SMBUS_IO_BASE + SMBGCTL);
82
83         /* clear any lingering errors, so the transaction will run */
84         /* Do I need to write the bits to a 1 to clear an error? */
85         outw(inw(SMBUS_IO_BASE + SMBGSTATUS), SMBUS_IO_BASE + SMBGSTATUS);
86
87         /* clear the data word...*/
88         outw(0, SMBUS_IO_BASE + SMBHSTDAT);
89
90         /* start the command */
91         outw((inw(SMBUS_IO_BASE + SMBGCTL) | (1 << 3)), SMBUS_IO_BASE + SMBGCTL);
92
93
94         /* poll for transaction completion */
95         if (smbus_wait_until_done() < 0) {
96                 return -1;
97         }
98
99         global_status_register = inw(SMBUS_IO_BASE + SMBGSTATUS);
100
101         /* read results of transaction */
102         byte = inw(SMBUS_IO_BASE + SMBHSTDAT) & 0xff;
103
104         if (global_status_register != (1 << 4)) {
105                 return -1;
106         }
107         return byte;
108 }