7b9d55dced65e182927cf531161871cc5c919160
[coreboot.git] / src / southbridge / via / vt8235 / vt8235_early_smbus.c
1 #define SMBUS_IO_BASE 0xf00
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 READ_CMD  0x01        // 1 in the 0 bit of SMBHSTADD states to READ
20
21
22 #define SMBUS_TIMEOUT (100*1000*10)
23
24 #define  I2C_TRANS_CMD          0x40
25 #define  CLOCK_SLAVE_ADDRESS    0x69
26
27 static void enable_smbus(void)
28 {
29         device_t dev;
30         unsigned char c;
31         int i;
32
33         /* Power management controller */
34         dev = pci_locate_device(PCI_ID(PCI_VENDOR_ID_VIA,
35                                 PCI_DEVICE_ID_VIA_8235), 0);
36
37         if (dev == PCI_DEV_INVALID) {
38                 die("SMBUS controller not found\n");
39         }
40
41         // set IO base address to SMBUS_IO_BASE
42         pci_write_config16(dev, 0xd0, SMBUS_IO_BASE | 1);
43
44         // Enable SMBus
45         pci_write_config8(dev, 0xd2, (0x4 << 1) | 1);
46
47         /* make it work for I/O ...
48          */
49         pci_write_config16(dev, 4, 1);
50
51         /* FIX for half baud rate problem */
52         /* let clocks and the like settle */
53         /* as yet arbitrary count - 1000 is too little 5000 works */
54         for(i = 0 ; i < 5000 ; i++)
55                 outb(0x80,0x80);
56
57         /*
58          * The VT1211 serial port needs 48 mhz clock, on power up it is getting
59          *  only 24 mhz, there is some mysterious device on the smbus that can
60          *  fix this...this code below does it.
61          *  */
62         outb(0xff, SMBUS_IO_BASE+SMBHSTSTAT);
63         outb(0x7f, SMBUS_IO_BASE+SMBHSTDAT0);
64         outb(0x83, SMBUS_IO_BASE+SMBHSTCMD);
65         outb(CLOCK_SLAVE_ADDRESS<<1 , SMBUS_IO_BASE+SMBXMITADD);
66         outb(8 | I2C_TRANS_CMD, SMBUS_IO_BASE+SMBHSTCTL);
67
68         for (;;) {
69                 c = inb(SMBUS_IO_BASE+SMBHSTSTAT);
70                 if ((c & 1) == 0)
71                         break;
72         }
73 }
74
75
76 static inline void smbus_delay(void)
77 {
78         outb(0x80, 0x80);
79 }
80
81 static int smbus_wait_until_ready(void)
82 {
83         unsigned char c;
84         unsigned long loops;
85         loops = SMBUS_TIMEOUT;
86         do {
87                 smbus_delay();
88                 c = inb(SMBUS_IO_BASE + SMBHSTSTAT);
89                 while((c & 1) == 1) {
90                         print_debug("c is ");
91                         print_debug_hex8(c);
92                         print_debug("\n");
93                         c = inb(SMBUS_IO_BASE + SMBHSTSTAT);
94                         /* nop */
95                 }
96
97         } while(--loops);
98         return loops?0:-1;
99 }
100
101 void smbus_reset(void)
102 {
103         outb(HOST_RESET, SMBUS_IO_BASE + SMBHSTSTAT);
104         outb(HOST_RESET, SMBUS_IO_BASE + SMBHSTSTAT);
105         outb(HOST_RESET, SMBUS_IO_BASE + SMBHSTSTAT);
106         outb(HOST_RESET, SMBUS_IO_BASE + SMBHSTSTAT);
107
108         smbus_wait_until_ready();
109         print_debug("After reset status ");
110         print_debug_hex8( inb(SMBUS_IO_BASE + SMBHSTSTAT));
111         print_debug("\n");
112 }
113
114
115
116 static int smbus_wait_until_done(void)
117 {
118         unsigned long loops;
119         unsigned char byte;
120         loops = SMBUS_TIMEOUT;
121         do {
122                 smbus_delay();
123
124                 byte = inb(SMBUS_IO_BASE + SMBHSTSTAT);
125                 if (byte & 1)
126                         break;
127
128         } while(--loops);
129         return loops?0:-1;
130 }
131
132 static void smbus_print_error(unsigned char host_status_register)
133 {
134
135         print_err("smbus_error: ");
136         print_err_hex8(host_status_register);
137         print_err("\n");
138         if (host_status_register & (1 << 4)) {
139                 print_err("Interrup/SMI# was Failed Bus Transaction\n");
140         }
141         if (host_status_register & (1 << 3)) {
142                 print_err("Bus Error\n");
143         }
144         if (host_status_register & (1 << 2)) {
145                 print_err("Device Error\n");
146         }
147         if (host_status_register & (1 << 1)) {
148                 print_err("Interrupt/SMI# was Successful Completion\n");
149         }
150         if (host_status_register & (1 << 0)) {
151                 print_err("Host Busy\n");
152         }
153 }
154
155
156 /* SMBus routines borrowed from VIA's Trident Driver */
157 /* this works, so I am not going to touch it for now -- rgm */
158 static unsigned char smbus_read_byte(unsigned char devAdr,
159                                 unsigned char bIndex)
160 {
161         unsigned short i;
162         unsigned char  bData;
163         unsigned char  sts = 0;
164
165         /* clear host status */
166         outb(0xff, SMBUS_IO_BASE);
167
168         /* check SMBUS ready */
169         for ( i = 0; i < 0xFFFF; i++ )
170                 if ( (inb(SMBUS_IO_BASE) & 0x01) == 0 )
171                         break;
172
173         /* set host command */
174         outb(bIndex, SMBUS_IO_BASE+3);
175
176         /* set slave address */
177         outb(devAdr | 0x01, SMBUS_IO_BASE+4);
178
179         /* start */
180         outb(0x48, SMBUS_IO_BASE+2);
181
182         /* SMBUS Wait Ready */
183         for ( i = 0; i < 0xFFFF; i++ )
184                 if ( ((sts = (inb(SMBUS_IO_BASE) & 0x1f)) & 0x01) == 0 )
185                         break;
186
187         if ((sts & ~3) != 0) {
188                 smbus_print_error(sts);
189                 return 0;
190         }
191         bData=inb(SMBUS_IO_BASE+5);
192
193         return bData;
194
195 }
196
197 /* for reference, here is the fancier version which we will use at some
198  * point
199  */
200 # if 0
201 int smbus_read_byte(unsigned device, unsigned address, unsigned char *result)
202 {
203         unsigned char host_status_register;
204         unsigned char byte;
205
206         reset();
207
208         smbus_wait_until_ready();
209
210         /* setup transaction */
211         /* disable interrupts */
212         outb(inb(SMBUS_IO_BASE + SMBHSTCTL) & (~1), SMBUS_IO_BASE + SMBHSTCTL);
213         /* set the device I'm talking too */
214         outb(((device & 0x7f) << 1) | 1, SMBUS_IO_BASE + SMBXMITADD);
215         /* set the command/address... */
216         outb(address & 0xFF, SMBUS_IO_BASE + SMBHSTCMD);
217         /* set up for a byte data read */
218         outb((inb(SMBUS_IO_BASE + SMBHSTCTL) & 0xE3) | (0x2 << 2),
219                 SMBUS_IO_BASE + SMBHSTCTL);
220
221         /* clear any lingering errors, so the transaction will run */
222         outb(inb(SMBUS_IO_BASE + SMBHSTSTAT), SMBUS_IO_BASE + SMBHSTSTAT);
223
224         /* clear the data byte...*/
225         outb(0, SMBUS_IO_BASE + SMBHSTDAT0);
226
227         /* start the command */
228         outb((inb(SMBUS_IO_BASE + SMBHSTCTL) | 0x40),
229                 SMBUS_IO_BASE + SMBHSTCTL);
230
231         /* poll for transaction completion */
232         smbus_wait_until_done();
233
234         host_status_register = inb(SMBUS_IO_BASE + SMBHSTSTAT);
235
236         /* Ignore the In Use Status... */
237         host_status_register &= ~(1 << 6);
238
239         /* read results of transaction */
240         byte = inb(SMBUS_IO_BASE + SMBHSTDAT0);
241         smbus_print_error(byte);
242
243         *result = byte;
244         return host_status_register != 0x02;
245 }
246
247
248 #endif
249