fixes for EPIA.
[coreboot.git] / src / southbridge / via / vt8231 / vt8231_early_smbus.c
1 #define SMBUS_IO_BASE 0x5000
2
3 #define SMBHSTSTAT 0x0
4 #define SMBSLVSTAT 0x1
5 #define SMBHSTCTL  0x2
6 #define SMBHSTCMD  0x3
7 #define SMBXMITADD 0x4
8 #define SMBHSTDAT0 0x5
9 #define SMBHSTDAT1 0x6
10 #define SMBBLKDAT  0x7
11 #define SMBSLVCTL  0x8
12 #define SMBTRNSADD 0x9
13 #define SMBSLVDATA 0xa
14 #define SMLINK_PIN_CTL 0xe
15 #define SMBUS_PIN_CTL  0xf 
16
17 /* Define register settings */
18 #define HOST_RESET 0xff
19 #define DIMM_BASE 0xa0        // 1010000 is base for DIMM in SMBus
20 #define READ_CMD  0x01        // 1 in the 0 bit of SMBHSTADD states to READ
21
22
23 #define SMBUS_TIMEOUT (100*1000*10)
24
25 static void enable_smbus(void)
26 {
27         device_t dev;
28         unsigned char c;
29         /* Power management controller */
30         dev = pci_locate_device(PCI_ID(0x1106,0x8235), 0);
31         
32         if (dev == PCI_DEV_INVALID) {
33                 die("SMBUS controller not found\r\n");
34         }
35         
36         // set IO base address to SMBUS_IO_BASE
37         pci_write_config32(dev, 0x90, SMBUS_IO_BASE|1);
38         
39         // Enable SMBus 
40         c = pci_read_config8(dev, 0xd2);
41         c |= 5;
42         pci_write_config8(dev, 0xd2, c);
43         
44         /* make it work for I/O ...
45          */
46         dev = pci_locate_device(PCI_ID(0x1106,0x8231), 0);
47         c = pci_read_config8(dev, 4);
48         c |= 1;
49         pci_write_config8(dev, 4, c);
50         print_debug_hex8(c);
51         print_debug(" is the comm register\r\n");
52         
53         print_debug("SMBus controller enabled\r\n");
54 }
55
56
57 static inline void smbus_delay(void)
58 {
59         outb(0x80, 0x80);
60 }
61
62 static int smbus_wait_until_ready(void)
63 {
64         unsigned char c;
65         unsigned long loops;
66         loops = SMBUS_TIMEOUT;
67         do {
68                 unsigned char val;
69                 smbus_delay();
70                 c = inb(SMBUS_IO_BASE + SMBHSTSTAT);
71                 while((c & 1) == 1) {
72                         print_debug("c is ");
73                         print_debug_hex8(c);
74                         print_debug("\r\n");
75                         c = inb(SMBUS_IO_BASE + SMBHSTSTAT);
76                         /* nop */ 
77                 }
78
79         } while(--loops);
80         return loops?0:-1;
81 }
82
83 void smbus_reset(void)
84 {
85         outb(HOST_RESET, SMBUS_IO_BASE + SMBHSTSTAT);
86         outb(HOST_RESET, SMBUS_IO_BASE + SMBHSTSTAT);
87         outb(HOST_RESET, SMBUS_IO_BASE + SMBHSTSTAT);
88         outb(HOST_RESET, SMBUS_IO_BASE + SMBHSTSTAT);
89         
90         smbus_wait_until_ready();
91         print_debug("After reset status ");
92         print_debug_hex8( inb(SMBUS_IO_BASE + SMBHSTSTAT));
93         print_debug("\r\n");
94 }
95   
96
97
98 static int smbus_wait_until_done(void)
99 {
100         unsigned long loops;
101         unsigned char byte;
102         loops = SMBUS_TIMEOUT;
103         do {
104                 unsigned char val;
105                 smbus_delay();
106                 
107                 byte = inb(SMBUS_IO_BASE + SMBHSTSTAT);
108                 if (byte & 1)
109                         break;
110                 
111         } while(--loops);
112         return loops?0:-1;
113 }
114
115 static void smbus_print_error(unsigned char host_status_register)
116 {
117
118         print_err("smbus_error: ");
119         print_err_hex8(host_status_register);
120         print_err("\r\n");
121         if (host_status_register & (1 << 4)) {
122                 print_err("Interrup/SMI# was Failed Bus Transaction\r\n");
123         }
124         if (host_status_register & (1 << 3)) {
125                 print_err("Bus Error\r\n");
126         }
127         if (host_status_register & (1 << 2)) {
128                 print_err("Device Error\r\n");
129         }
130         if (host_status_register & (1 << 1)) {
131                 print_err("Interrupt/SMI# was Successful Completion\r\n");
132         }
133         if (host_status_register & (1 << 0)) {
134                 print_err("Host Busy\r\n");
135         }
136 }
137
138
139 /* SMBus routines borrowed from VIA's Trident Driver */
140 /* this works, so I am not going to touch it for now -- rgm */
141 static unsigned char smbus_read_byte(unsigned char devAdr, 
142                                 unsigned char bIndex) 
143 {
144         unsigned short i;
145         unsigned char  bData;
146         unsigned char  sts = 0;
147         
148         /* clear host status */
149         outb(0xff, SMBUS_IO_BASE);
150         
151         /* check SMBUS ready */
152         for ( i = 0; i < 0xFFFF; i++ )
153                 if ( (inb(SMBUS_IO_BASE) & 0x01) == 0 )
154                         break;
155         
156         /* set host command */
157         outb(bIndex, SMBUS_IO_BASE+3);
158         
159         /* set slave address */
160         outb(devAdr | 0x01, SMBUS_IO_BASE+4);
161         
162         /* start */
163         outb(0x48, SMBUS_IO_BASE+2);
164         
165         /* SMBUS Wait Ready */
166         for ( i = 0; i < 0xFFFF; i++ )
167                 if ( ((sts = inb(SMBUS_IO_BASE)) & 0x01) == 0 )
168                         break;
169         if ((sts & ~3) != 0) {
170                 smbus_print_error(sts);
171                 return 0;
172         }
173         bData=inb(SMBUS_IO_BASE+5);
174         
175         return bData;
176         
177 }
178
179 /* for reference, here is the fancier version which we will use at some 
180  * point
181  */
182 # if 0
183 int smbus_read_byte(unsigned device, unsigned address, unsigned char *result)
184 {
185         unsigned char host_status_register;
186         unsigned char byte;
187         
188         reset();
189         
190         smbus_wait_until_ready();
191         
192         /* setup transaction */
193         /* disable interrupts */
194         outb(inb(SMBUS_IO_BASE + SMBHSTCTL) & (~1), SMBUS_IO_BASE + SMBHSTCTL);
195         /* set the device I'm talking too */
196         outb(((device & 0x7f) << 1) | 1, SMBUS_IO_BASE + SMBXMITADD);
197         /* set the command/address... */
198         outb(address & 0xFF, SMBUS_IO_BASE + SMBHSTCMD);
199         /* set up for a byte data read */
200         outb((inb(SMBUS_IO_BASE + SMBHSTCTL) & 0xE3) | (0x2 << 2),
201                 SMBUS_IO_BASE + SMBHSTCTL);
202         
203         /* clear any lingering errors, so the transaction will run */
204         outb(inb(SMBUS_IO_BASE + SMBHSTSTAT), SMBUS_IO_BASE + SMBHSTSTAT);
205         
206         /* clear the data byte...*/
207         outb(0, SMBUS_IO_BASE + SMBHSTDAT0);
208         
209         /* start the command */
210         outb((inb(SMBUS_IO_BASE + SMBHSTCTL) | 0x40),
211                 SMBUS_IO_BASE + SMBHSTCTL);
212         
213         /* poll for transaction completion */
214         smbus_wait_until_done();
215         
216         host_status_register = inb(SMBUS_IO_BASE + SMBHSTSTAT);
217         
218         /* Ignore the In Use Status... */
219         host_status_register &= ~(1 << 6);
220         
221         /* read results of transaction */
222         byte = inb(SMBUS_IO_BASE + SMBHSTDAT0);
223         smbus_print_error(byte);
224         
225         *result = byte;
226         return host_status_register != 0x02;
227 }
228
229
230 #endif
231