Add VIA CX700 support, plus VIA vt8454c reference board support.
[coreboot.git] / src / northbridge / via / cx700 / cx700_early_smbus.c
1 /*
2  * This file is part of the coreboot project.
3  *
4  * Copyright (C) 2007-2009 coresystems GmbH
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; version 2 of the License.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
18  */
19
20 // other bioses use this, too:
21 #define SMBUS_IO_BASE           0x0500
22
23 #define SMBHSTSTAT              SMBUS_IO_BASE + 0x0
24 #define SMBSLVSTAT              SMBUS_IO_BASE + 0x1
25 #define SMBHSTCTL               SMBUS_IO_BASE + 0x2
26 #define SMBHSTCMD               SMBUS_IO_BASE + 0x3
27 #define SMBXMITADD              SMBUS_IO_BASE + 0x4
28 #define SMBHSTDAT0              SMBUS_IO_BASE + 0x5
29 #define SMBHSTDAT1              SMBUS_IO_BASE + 0x6
30
31 #define SMBBLKDAT               SMBUS_IO_BASE + 0x7
32 #define SMBSLVCTL               SMBUS_IO_BASE + 0x8
33 #define SMBTRNSADD              SMBUS_IO_BASE + 0x9
34 #define SMBSLVDATA              SMBUS_IO_BASE + 0xa
35 #define SMLINK_PIN_CTL          SMBUS_IO_BASE + 0xe
36 #define SMBUS_PIN_CTL           SMBUS_IO_BASE + 0xf
37
38 /* Define register settings */
39 #define HOST_RESET              0xff
40 #define DIMM_BASE               0xa0    // 1010000 is base for DIMM in SMBus
41 #define READ_CMD                0x01    // 1 in the 0 bit of SMBHSTADD states to READ
42
43 #define SMBUS_TIMEOUT           (100*1000*10)
44
45 #define I2C_TRANS_CMD           0x40
46 #define CLOCK_SLAVE_ADDRESS     0x69
47
48 #define SMBUS_DELAY()           outb(0x80, 0x80)
49
50 /* Debugging macros. */
51
52 // #define DEBUG_SMBUS 1
53
54 #ifdef DEBUG_SMBUS
55 #define PRINT_DEBUG(x)          print_debug(x)
56 #define PRINT_DEBUG_HEX16(x)    print_debug_hex16(x)
57 #else
58 #define PRINT_DEBUG(x)
59 #define PRINT_DEBUG_HEX16(x)
60 #endif
61
62 /* Internal functions */
63 static void smbus_print_error(unsigned char host_status_register, int loops)
64 {
65         /* Check if there actually was an error */
66         if (host_status_register == 0x00 || host_status_register == 0x40 ||
67             host_status_register == 0x42)
68                 return;
69         print_err("SMBus Error: ");
70         print_err_hex8(host_status_register);
71
72         print_err("\r\n");
73         if (loops >= SMBUS_TIMEOUT) {
74                 print_err("SMBus Timout\r\n");
75         }
76         if (host_status_register & (1 << 4)) {
77                 print_err("Interrup/SMI# was Failed Bus Transaction\r\n");
78         }
79         if (host_status_register & (1 << 3)) {
80                 print_err("Bus Error\r\n");
81         }
82         if (host_status_register & (1 << 2)) {
83                 print_err("Device Error\r\n");
84         }
85         if (host_status_register & (1 << 1)) {
86                 /* This isn't a real error... */
87                 print_debug("Interrupt/SMI# was Successful Completion\r\n");
88         }
89         if (host_status_register & (1 << 0)) {
90                 print_err("Host Busy\r\n");
91         }
92 }
93
94 static void smbus_wait_until_ready(void)
95 {
96         int loops;
97
98         loops = 0;
99
100         /* Yes, this is a mess, but it's the easiest way to do it */
101         while (((inb(SMBHSTSTAT) & 1) == 1) && (loops <= SMBUS_TIMEOUT)) {
102                 SMBUS_DELAY();
103                 ++loops;
104         }
105 #ifdef DEBUG_SMBUS
106         /* Some systems seem to have a flakey SMBus. No need to spew a lot of
107          * errors on those, once we know that SMBus access is principally
108          * working.
109          */
110         smbus_print_error(inb(SMBHSTSTAT), loops);
111 #endif
112 }
113
114 static void smbus_reset(void)
115 {
116         outb(HOST_RESET, SMBHSTSTAT);
117 }
118
119 /* Public functions */
120 static void set_ics_data(unsigned char dev, int data, char len)
121 {
122         //int i;
123         smbus_reset();
124         /* clear host data port */
125         outb(0x00, SMBHSTDAT0);
126         SMBUS_DELAY();
127         smbus_wait_until_ready();
128
129         /* read to reset block transfer counter */
130         inb(SMBHSTCTL);
131
132         /* fill blocktransfer array */
133         if (dev = 0xd2) {
134                 //char d2_data[] = {0x0d,0x00,0x3f,0xcd,0x7f,0xbf,0x1a,0x2a,0x01,0x0f,0x0b,0x00,0x8d,0x9b};
135                 outb(0x0d, SMBBLKDAT);
136                 outb(0x00, SMBBLKDAT);
137                 outb(0x3f, SMBBLKDAT);
138                 outb(0xcd, SMBBLKDAT);
139                 outb(0x7f, SMBBLKDAT);
140                 outb(0xbf, SMBBLKDAT);
141                 outb(0x1a, SMBBLKDAT);
142                 outb(0x2a, SMBBLKDAT);
143                 outb(0x01, SMBBLKDAT);
144                 outb(0x0f, SMBBLKDAT);
145                 outb(0x0b, SMBBLKDAT);
146                 outb(0x80, SMBBLKDAT);
147                 outb(0x8d, SMBBLKDAT);
148                 outb(0x9b, SMBBLKDAT);
149         } else {
150                 //char d4_data[] = {0x08,0xff,0x3f,0x00,0x00,0xff,0xff,0xff,0xff};
151                 outb(0x08, SMBBLKDAT);
152                 outb(0xff, SMBBLKDAT);
153                 outb(0x3f, SMBBLKDAT);
154                 outb(0x00, SMBBLKDAT);
155                 outb(0x00, SMBBLKDAT);
156                 outb(0xff, SMBBLKDAT);
157                 outb(0xff, SMBBLKDAT);
158                 outb(0xff, SMBBLKDAT);
159                 outb(0xff, SMBBLKDAT);
160         }
161
162         //for (i=0; i < len; i++)
163         //      outb(data[i],SMBBLKDAT);
164
165         outb(dev, SMBXMITADD);
166         outb(0, SMBHSTCMD);
167         outb(len, SMBHSTDAT0);
168         outb(0x74, SMBHSTCTL);
169
170         SMBUS_DELAY();
171
172         smbus_wait_until_ready();
173
174         smbus_reset();
175
176 }
177
178 static unsigned int get_spd_data(const struct mem_controller *ctrl, unsigned int dimm,
179                                  unsigned int offset)
180 {
181         unsigned int val, addr;
182
183         smbus_reset();
184
185         /* clear host data port */
186         outb(0x00, SMBHSTDAT0);
187         SMBUS_DELAY();
188         smbus_wait_until_ready();
189
190         /* Fetch the SMBus address of the SPD ROM from
191          * the ctrl struct in auto.c in case they are at
192          * non-standard positions.
193          * SMBus Address shifted by 1
194          */
195         addr = (ctrl->channel0[dimm]) << 1;
196
197         outb(addr | 0x1, SMBXMITADD);
198         outb(offset, SMBHSTCMD);
199         outb(0x48, SMBHSTCTL);
200
201         SMBUS_DELAY();
202
203         smbus_wait_until_ready();
204
205         val = inb(SMBHSTDAT0);
206         smbus_reset();
207         return val;
208 }
209
210 static void enable_smbus(void)
211 {
212         device_t dev;
213
214         /* The CX700 ISA Bridge (0x1106, 0x8324) is hardcoded to this location,
215          * no need to probe.
216          */
217         dev = PCI_DEV(0, 17, 0);
218
219         /* SMBus Clock Select: Divider fof 14.318MHz */
220         pci_write_config8(dev, 0x94, 0x20);
221
222         /* SMBus I/O Base, enable SMBus */
223         pci_write_config16(dev, 0xd0, SMBUS_IO_BASE | 1);
224
225         /* SMBus Clock from 128K Source, Enable SMBus Host Controller */
226         pci_write_config8(dev, 0xd2, 0x05);
227
228         /* Enable I/O decoding */
229         pci_write_config16(dev, 0x04, 0x0003);
230
231         /* Setup clock chips */
232         set_ics_data(0xd2, 0, 14);
233         set_ics_data(0xd4, 0, 9);
234 }
235
236 /* Debugging Function */
237 #ifdef DEBUG_SMBUS
238 static void dump_spd_data(const struct mem_controller *ctrl)
239 {
240         int dimm, offset, regs;
241         unsigned int val;
242
243         for (dimm = 0; dimm < DIMM_SOCKETS; dimm++) {
244                 print_debug("SPD Data for DIMM ");
245                 print_debug_hex8(dimm);
246                 print_debug("\r\n");
247
248                 val = get_spd_data(ctrl, dimm, 0);
249                 if (val == 0xff) {
250                         regs = 256;
251                 } else if (val == 0x80) {
252                         regs = 128;
253                 } else {
254                         print_debug("No DIMM present\r\n");
255                         regs = 0;
256                 }
257                 for (offset = 0; offset < regs; offset++) {
258                         print_debug("  Offset ");
259                         print_debug_hex8(offset);
260                         print_debug(" = 0x");
261                         print_debug_hex8(get_spd_data(ctrl, dimm, offset));
262                         print_debug("\r\n");
263                 }
264         }
265 }
266 #else
267 #define dump_spd_data(ctrl)
268 #endif