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   c = pci_read_config8(dev, 0x54);
45   c &= ~0x80;
46   pci_write_config8(dev, 0xd2, c);
47
48
49   print_debug("SMBus controller enabled\r\n");
50 }
51
52
53 static inline void smbus_delay(void)
54 {
55   outb(0x80, 0x80);
56 }
57
58 static int smbus_wait_until_ready(void)
59 {
60         unsigned char c;
61   unsigned long loops;
62   loops = SMBUS_TIMEOUT;
63   do {
64     unsigned char val;
65     smbus_delay();
66     c = inb(SMBUS_IO_BASE + SMBHSTSTAT);
67     while((c & 1) == 1) {
68       print_err("c is ");
69       print_err_hex8(c);
70       print_err("\n");
71       c = inb(SMBUS_IO_BASE + SMBHSTSTAT);
72       /* nop */ 
73     }
74
75   } while(--loops);
76   return loops?0:-1;
77 }
78
79 void smbus_reset(void)
80 {
81   outb(HOST_RESET, SMBUS_IO_BASE + SMBHSTSTAT);
82   outb(HOST_RESET, SMBUS_IO_BASE + SMBHSTSTAT);
83   outb(HOST_RESET, SMBUS_IO_BASE + SMBHSTSTAT);
84   outb(HOST_RESET, SMBUS_IO_BASE + SMBHSTSTAT);
85
86   smbus_wait_until_ready();
87   print_err("After reset status ");
88   print_err_hex8( inb(SMBUS_IO_BASE + SMBHSTSTAT));
89   print_err("\n");
90 }
91   
92
93
94 static int smbus_wait_until_done(void)
95 {
96   unsigned long loops;
97   unsigned char byte;
98   loops = SMBUS_TIMEOUT;
99   do {
100     unsigned char val;
101     smbus_delay();
102                 
103     byte = inb(SMBUS_IO_BASE + SMBHSTSTAT);
104     if (byte & 1)
105       break;
106
107   } while(--loops);
108   return loops?0:-1;
109 }
110
111 static void smbus_print_error(unsigned char host_status_register)
112 {
113
114   print_err("smbus_error: ");
115   print_err_hex8(host_status_register);
116   print_err("\n");
117   if (host_status_register & (1 << 4)) {
118     print_err("Interrup/SMI# was Failed Bus Transaction\n");
119   }
120   if (host_status_register & (1 << 3)) {
121     print_err("Bus Error\n");
122   }
123   if (host_status_register & (1 << 2)) {
124     print_err("Device Error\n");
125   }
126   if (host_status_register & (1 << 1)) {
127     print_err("Interrupt/SMI# was Successful Completion\n");
128   }
129   if (host_status_register & (1 << 0)) {
130     print_err("Host Busy\n");
131   }
132 }
133
134
135 /* SMBus routines borrowed from VIA's Trident Driver */
136 /* this works, so I am not going to touch it for now -- rgm */
137 static unsigned char smbus_read_byte(unsigned char devAdr, 
138                                 unsigned char bIndex) 
139 {
140   unsigned short i;
141   unsigned char  bData;
142   unsigned char  sts;
143                         
144   /* clear host status */
145   outb(0xff, SMBUS_IO_BASE);
146                             
147   /* check SMBUS ready */
148   for ( i = 0; i < 0xFFFF; i++ )
149     if ( (inb(SMBUS_IO_BASE) & 0x01) == 0 )
150       break;
151
152   /* set host command */
153   outb(bIndex, SMBUS_IO_BASE+3);
154
155   /* set slave address */
156   outb(devAdr | 0x01, SMBUS_IO_BASE+4);
157
158   /* start */
159   outb(0x48, SMBUS_IO_BASE+2);
160
161   /* SMBUS Wait Ready */
162   for ( i = 0; i < 0xFFFF; i++ )
163     if ( ((sts = inb(SMBUS_IO_BASE)) & 0x01) == 0 )
164       break;
165   if ((sts & ~3) != 0) {
166     smbus_print_error(sts);
167     return 0;
168   }
169   bData=inb(SMBUS_IO_BASE+5);
170
171   return bData;
172
173 }
174
175 /* for reference, here is the fancier version which we will use at some 
176  * point
177  */
178 # if 0
179 int smbus_read_byte(unsigned device, unsigned address, unsigned char *result)
180 {
181   unsigned char host_status_register;
182   unsigned char byte;
183
184   reset();
185
186   smbus_wait_until_ready();
187
188   /* setup transaction */
189   /* disable interrupts */
190   outb(inb(SMBUS_IO_BASE + SMBHSTCTL) & (~1), SMBUS_IO_BASE + SMBHSTCTL);
191   /* set the device I'm talking too */
192   outb(((device & 0x7f) << 1) | 1, SMBUS_IO_BASE + SMBXMITADD);
193   /* set the command/address... */
194   outb(address & 0xFF, SMBUS_IO_BASE + SMBHSTCMD);
195   /* set up for a byte data read */
196   outb((inb(SMBUS_IO_BASE + SMBHSTCTL) & 0xE3) | (0x2 << 2),
197           SMBUS_IO_BASE + SMBHSTCTL);
198
199   /* clear any lingering errors, so the transaction will run */
200   outb(inb(SMBUS_IO_BASE + SMBHSTSTAT), SMBUS_IO_BASE + SMBHSTSTAT);
201
202   /* clear the data byte...*/
203   outb(0, SMBUS_IO_BASE + SMBHSTDAT0);
204
205   /* start the command */
206   outb((inb(SMBUS_IO_BASE + SMBHSTCTL) | 0x40),
207           SMBUS_IO_BASE + SMBHSTCTL);
208
209   /* poll for transaction completion */
210   smbus_wait_until_done();
211
212   host_status_register = inb(SMBUS_IO_BASE + SMBHSTSTAT);
213
214   /* Ignore the In Use Status... */
215   host_status_register &= ~(1 << 6);
216
217   /* read results of transaction */
218   byte = inb(SMBUS_IO_BASE + SMBHSTDAT0);
219   smbus_print_error(byte);
220
221   *result = byte;
222   return host_status_register != 0x02;
223 }
224
225
226 #endif
227