ccee3b4633c05a2a8ad08f1105c43de4e42ae073
[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 #if CONFIG_DEBUG_SMBUS
52 #define PRINT_DEBUG(x)          print_debug(x)
53 #define PRINT_DEBUG_HEX16(x)    print_debug_hex16(x)
54 #else
55 #define PRINT_DEBUG(x)
56 #define PRINT_DEBUG_HEX16(x)
57 #endif
58
59 /* Internal functions */
60 static void smbus_print_error(unsigned char host_status_register, int loops)
61 {
62         /* Check if there actually was an error */
63         if (host_status_register == 0x00 || host_status_register == 0x40 ||
64             host_status_register == 0x42)
65                 return;
66         print_err("SMBus Error: ");
67         print_err_hex8(host_status_register);
68
69         print_err("\r\n");
70         if (loops >= SMBUS_TIMEOUT) {
71                 print_err("SMBus Timout\r\n");
72         }
73         if (host_status_register & (1 << 4)) {
74                 print_err("Interrup/SMI# was Failed Bus Transaction\r\n");
75         }
76         if (host_status_register & (1 << 3)) {
77                 print_err("Bus Error\r\n");
78         }
79         if (host_status_register & (1 << 2)) {
80                 print_err("Device Error\r\n");
81         }
82         if (host_status_register & (1 << 1)) {
83                 /* This isn't a real error... */
84                 print_debug("Interrupt/SMI# was Successful Completion\r\n");
85         }
86         if (host_status_register & (1 << 0)) {
87                 print_err("Host Busy\r\n");
88         }
89 }
90
91 static void smbus_wait_until_ready(void)
92 {
93         int loops;
94
95         loops = 0;
96
97         /* Yes, this is a mess, but it's the easiest way to do it */
98         while (((inb(SMBHSTSTAT) & 1) == 1) && (loops <= SMBUS_TIMEOUT)) {
99                 SMBUS_DELAY();
100                 ++loops;
101         }
102 #if CONFIG_DEBUG_SMBUS
103         /* Some systems seem to have a flakey SMBus. No need to spew a lot of
104          * errors on those, once we know that SMBus access is principally
105          * working.
106          */
107         smbus_print_error(inb(SMBHSTSTAT), loops);
108 #endif
109 }
110
111 static void smbus_reset(void)
112 {
113         outb(HOST_RESET, SMBHSTSTAT);
114 }
115
116 /* Public functions */
117 static void set_ics_data(unsigned char dev, int data, char len)
118 {
119         //int i;
120         smbus_reset();
121         /* clear host data port */
122         outb(0x00, SMBHSTDAT0);
123         SMBUS_DELAY();
124         smbus_wait_until_ready();
125
126         /* read to reset block transfer counter */
127         inb(SMBHSTCTL);
128
129         /* fill blocktransfer array */
130         if (dev = 0xd2) {
131                 //char d2_data[] = {0x0d,0x00,0x3f,0xcd,0x7f,0xbf,0x1a,0x2a,0x01,0x0f,0x0b,0x00,0x8d,0x9b};
132                 outb(0x0d, SMBBLKDAT);
133                 outb(0x00, SMBBLKDAT);
134                 outb(0x3f, SMBBLKDAT);
135                 outb(0xcd, SMBBLKDAT);
136                 outb(0x7f, SMBBLKDAT);
137                 outb(0xbf, SMBBLKDAT);
138                 outb(0x1a, SMBBLKDAT);
139                 outb(0x2a, SMBBLKDAT);
140                 outb(0x01, SMBBLKDAT);
141                 outb(0x0f, SMBBLKDAT);
142                 outb(0x0b, SMBBLKDAT);
143                 outb(0x80, SMBBLKDAT);
144                 outb(0x8d, SMBBLKDAT);
145                 outb(0x9b, SMBBLKDAT);
146         } else {
147                 //char d4_data[] = {0x08,0xff,0x3f,0x00,0x00,0xff,0xff,0xff,0xff};
148                 outb(0x08, SMBBLKDAT);
149                 outb(0xff, SMBBLKDAT);
150                 outb(0x3f, SMBBLKDAT);
151                 outb(0x00, SMBBLKDAT);
152                 outb(0x00, SMBBLKDAT);
153                 outb(0xff, SMBBLKDAT);
154                 outb(0xff, SMBBLKDAT);
155                 outb(0xff, SMBBLKDAT);
156                 outb(0xff, SMBBLKDAT);
157         }
158
159         //for (i=0; i < len; i++)
160         //      outb(data[i],SMBBLKDAT);
161
162         outb(dev, SMBXMITADD);
163         outb(0, SMBHSTCMD);
164         outb(len, SMBHSTDAT0);
165         outb(0x74, SMBHSTCTL);
166
167         SMBUS_DELAY();
168
169         smbus_wait_until_ready();
170
171         smbus_reset();
172
173 }
174
175 static unsigned int get_spd_data(const struct mem_controller *ctrl, unsigned int dimm,
176                                  unsigned int offset)
177 {
178         unsigned int val, addr;
179
180         smbus_reset();
181
182         /* clear host data port */
183         outb(0x00, SMBHSTDAT0);
184         SMBUS_DELAY();
185         smbus_wait_until_ready();
186
187         /* Fetch the SMBus address of the SPD ROM from
188          * the ctrl struct in romstage.c in case they are at
189          * non-standard positions.
190          * SMBus Address shifted by 1
191          */
192         addr = (ctrl->channel0[dimm]) << 1;
193
194         outb(addr | 0x1, SMBXMITADD);
195         outb(offset, SMBHSTCMD);
196         outb(0x48, SMBHSTCTL);
197
198         SMBUS_DELAY();
199
200         smbus_wait_until_ready();
201
202         val = inb(SMBHSTDAT0);
203         smbus_reset();
204         return val;
205 }
206
207 static void enable_smbus(void)
208 {
209         device_t dev;
210
211         /* The CX700 ISA Bridge (0x1106, 0x8324) is hardcoded to this location,
212          * no need to probe.
213          */
214         dev = PCI_DEV(0, 17, 0);
215
216         /* SMBus Clock Select: Divider fof 14.318MHz */
217         pci_write_config8(dev, 0x94, 0x20);
218
219         /* SMBus I/O Base, enable SMBus */
220         pci_write_config16(dev, 0xd0, SMBUS_IO_BASE | 1);
221
222         /* SMBus Clock from 128K Source, Enable SMBus Host Controller */
223         pci_write_config8(dev, 0xd2, 0x05);
224
225         /* Enable I/O decoding */
226         pci_write_config16(dev, 0x04, 0x0003);
227
228         /* Setup clock chips */
229         set_ics_data(0xd2, 0, 14);
230         set_ics_data(0xd4, 0, 9);
231 }
232
233 /* Debugging Function */
234 #ifdef CONFIG_DEBUG_SMBUS
235 static void dump_spd_data(const struct mem_controller *ctrl)
236 {
237         int dimm, offset, regs;
238         unsigned int val;
239
240         for (dimm = 0; dimm < DIMM_SOCKETS; dimm++) {
241                 print_debug("SPD Data for DIMM ");
242                 print_debug_hex8(dimm);
243                 print_debug("\r\n");
244
245                 val = get_spd_data(ctrl, dimm, 0);
246                 if (val == 0xff) {
247                         regs = 256;
248                 } else if (val == 0x80) {
249                         regs = 128;
250                 } else {
251                         print_debug("No DIMM present\r\n");
252                         regs = 0;
253                 }
254                 for (offset = 0; offset < regs; offset++) {
255                         print_debug("  Offset ");
256                         print_debug_hex8(offset);
257                         print_debug(" = 0x");
258                         print_debug_hex8(get_spd_data(ctrl, dimm, offset));
259                         print_debug("\r\n");
260                 }
261         }
262 }
263 #else
264 #define dump_spd_data(ctrl)
265 #endif