6fda8b0e0a08efc658e01d9328bf52e2685b990f
[coreboot.git] / src / northbridge / via / vx800 / vx800_early_smbus.c
1 /*
2  * This file is part of the coreboot project.
3  *
4  * Copyright (C) 2009 One Laptop per Child, Association, Inc.
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 #include <device/pci_ids.h>
21 #include "vx800.h"
22 #define SMBUS_IO_BASE           0x0500  //from award bios
23 #define PMIO_BASE               VX800_ACPI_IO_BASE      //might as well set this while we're here
24
25 #define SMBHSTSTAT              SMBUS_IO_BASE + 0x0
26 #define SMBSLVSTAT              SMBUS_IO_BASE + 0x1
27 #define SMBHSTCTL               SMBUS_IO_BASE + 0x2
28 #define SMBHSTCMD               SMBUS_IO_BASE + 0x3
29 #define SMBXMITADD              SMBUS_IO_BASE + 0x4
30 #define SMBHSTDAT0              SMBUS_IO_BASE + 0x5
31 #define SMBHSTDAT1              SMBUS_IO_BASE + 0x6
32 /* Rest of these aren't currently used... */
33 #define SMBBLKDAT               SMBUS_IO_BASE + 0x7
34 #define SMBSLVCTL               SMBUS_IO_BASE + 0x8
35 #define SMBTRNSADD              SMBUS_IO_BASE + 0x9
36 #define SMBSLVDATA              SMBUS_IO_BASE + 0xa
37 #define SMLINK_PIN_CTL          SMBUS_IO_BASE + 0xe
38 #define SMBUS_PIN_CTL           SMBUS_IO_BASE + 0xf
39
40 /* Define register settings */
41 #define HOST_RESET      0xff
42 #define DIMM_BASE               0xa0    // 1010000 is base for DIMM in SMBus
43 #define READ_CMD                0x01    // 1 in the 0 bit of SMBHSTADD states to READ
44
45 #define SMBUS_TIMEOUT           (100*1000*10)
46
47 #define I2C_TRANS_CMD           0x40
48 #define CLOCK_SLAVE_ADDRESS     0x69
49
50 #define SMBUS_DELAY()           outb(0x80, 0x80)
51
52 /* Debugging macros. Only necessary if something isn't working right */
53
54 #define DEBUG_SMBUS 1
55
56 #ifdef DEBUG_SMBUS
57 #define PRINT_DEBUG(x)          print_debug(x)
58 #define PRINT_DEBUG_HEX16(x)    print_debug_hex16(x)
59 #else
60 #define PRINT_DEBUG(x)
61 #define PRINT_DEBUG_HEX16(x)
62 #endif
63
64 /* Internal functions */
65 static void smbus_print_error(unsigned char host_status_register, int loops)
66 {
67 //              print_err("some i2c error\r\n");
68         /* Check if there actually was an error */
69         if (host_status_register == 0x00 || host_status_register == 0x40 ||
70             host_status_register == 0x42)
71                 return;
72         print_err("smbus_error: ");
73         print_err_hex8(host_status_register);
74         print_err("\r\n");
75         if (loops >= SMBUS_TIMEOUT) {
76                 print_err("SMBus Timout\r\n");
77         }
78         if (host_status_register & (1 << 4)) {
79                 print_err("Interrup/SMI# was Failed Bus Transaction\r\n");
80         }
81         if (host_status_register & (1 << 3)) {
82                 print_err("Bus Error\r\n");
83         }
84         if (host_status_register & (1 << 2)) {
85                 print_err("Device Error\r\n");
86         }
87         if (host_status_register & (1 << 1)) {
88                 /* This isn't a real error... */
89                 print_debug("Interrupt/SMI# was Successful Completion\r\n");
90         }
91         if (host_status_register & (1 << 0)) {
92                 print_err("Host Busy\r\n");
93         }
94 }
95
96 static void smbus_wait_until_ready(void)
97 {
98         int loops;
99
100         loops = 0;
101         /* Yes, this is a mess, but it's the easiest way to do it */
102         while (((inb(SMBHSTSTAT) & 1) == 1) && (loops <= SMBUS_TIMEOUT)) {
103                 SMBUS_DELAY();
104                 ++loops;
105         }
106         smbus_print_error(inb(SMBHSTSTAT), loops);
107 }
108
109 static void smbus_reset(void)
110 {
111         outb(HOST_RESET, SMBHSTSTAT);
112 }
113
114 /* Public functions */
115 static unsigned int set_ics_data(unsigned char dev, int data, char len)
116 {
117         smbus_reset();
118         /* clear host data port */
119         outb(0x00, SMBHSTDAT0);
120         SMBUS_DELAY();
121         smbus_wait_until_ready();
122
123         /* read to reset block transfer counter */
124         inb(SMBHSTCTL);
125
126         /* fill blocktransfer array */
127         if (dev == 0xd2) {
128                 //char d2_data[] = {0x0d,0x00,0x3f,0xcd,0x7f,0xbf,0x1a,0x2a,0x01,0x0f,0x0b,0x00,0x8d,0x9b};
129                 outb(0x0d, SMBBLKDAT);
130                 outb(0x00, SMBBLKDAT);
131                 outb(0x3f, SMBBLKDAT);
132                 outb(0xcd, SMBBLKDAT);
133                 outb(0x7f, SMBBLKDAT);
134                 outb(0xbf, SMBBLKDAT);
135                 outb(0x1a, SMBBLKDAT);
136                 outb(0x2a, SMBBLKDAT);
137                 outb(0x01, SMBBLKDAT);
138                 outb(0x0f, SMBBLKDAT);
139                 outb(0x0b, SMBBLKDAT);
140                 outb(0x80, SMBBLKDAT);
141                 outb(0x8d, SMBBLKDAT);
142                 outb(0x9b, SMBBLKDAT);
143         } else {
144                 //char d4_data[] = {0x08,0xff,0x3f,0x00,0x00,0xff,0xff,0xff,0xff};
145                 outb(0x08, SMBBLKDAT);
146                 outb(0xff, SMBBLKDAT);
147                 outb(0x3f, SMBBLKDAT);
148                 outb(0x00, SMBBLKDAT);
149                 outb(0x00, SMBBLKDAT);
150                 outb(0xff, SMBBLKDAT);
151                 outb(0xff, SMBBLKDAT);
152                 outb(0xff, SMBBLKDAT);
153                 outb(0xff, SMBBLKDAT);
154         }
155
156         //for (i=0; i < len; i++)
157         //      outb(data[i],SMBBLKDAT);
158
159         outb(dev, SMBXMITADD);
160         outb(0, SMBHSTCMD);
161         outb(len, SMBHSTDAT0);
162         outb(0x74, SMBHSTCTL);
163
164         SMBUS_DELAY();
165
166         smbus_wait_until_ready();
167
168         smbus_reset();
169         return 0;
170 }
171
172 static unsigned int get_spd_data(unsigned int dimm, unsigned int offset)
173 {
174         unsigned int val;
175
176         smbus_reset();
177         /* clear host data port */
178         outb(0x00, SMBHSTDAT0);
179         SMBUS_DELAY();
180         smbus_wait_until_ready();
181
182         /* Do some mathmatic magic */
183         dimm = (dimm << 1);
184         dimm &= 0x0E;
185         dimm |= 0xA0;
186
187         outb(dimm | 0x1, SMBXMITADD);
188         outb(offset, SMBHSTCMD);
189         outb(0x48, SMBHSTCTL);
190
191         SMBUS_DELAY();
192
193         smbus_wait_until_ready();
194
195         val = inb(SMBHSTDAT0);
196         smbus_reset();
197         return val;
198 }
199
200 static void enable_smbus(void)
201 {
202         device_t dev;
203
204         dev =
205             pci_locate_device(PCI_ID
206                               (PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_VX855_LPC),
207                               0);
208
209         if (dev == PCI_DEV_INVALID) {
210                 /* This won't display text if enable_smbus() is before serial init */
211                 die("Power Managment Controller not found\r\n");
212         }
213
214         /* Set clock source */
215         pci_write_config8(dev, 0x94, 0x20);
216
217         /* Write SMBus IO base to 0xd0, and enable SMBus */
218         pci_write_config16(dev, 0xd0, SMBUS_IO_BASE | 1);
219
220         /* Set to Award value */
221         pci_write_config8(dev, 0xd2, 0x05);
222
223         /* Make it work for I/O ... */
224         pci_write_config16(dev, 0x04, 0x0003);
225
226         /*
227            coreboot hangs at this two lines after os reboot(this even happen after I change os 
228            reboot to cold reboot, this also interfere S3 wakeup) */
229         /* Setup clock chips */
230         //set_ics_data(0xd2, 0, 14);
231         //set_ics_data(0xd4, 0, 9);
232
233         smbus_reset();
234         /* clear host data port */
235         outb(0x00, SMBHSTDAT0);
236         SMBUS_DELAY();
237         smbus_wait_until_ready();
238 }
239
240 /**
241  * A fixup for some systems that need time for the SMBus to "warm up". This is 
242  * needed on some VT823x based systems, where the SMBus spurts out bad data for 
243  * a short time after power on. This has been seen on the VIA Epia series and 
244  * Jetway J7F2-series. It reads the ID byte from SMBus, looking for 
245  * known-good data from a slot/address. Exits on either good data or a timeout.
246  *
247  * TODO: This should probably go into some global file, but one would need to
248  *       be created just for it. If some other chip needs/wants it, we can
249  *       worry about it then.
250  *
251  * @param ctrl The memory controller and SMBus addresses.
252  */
253 void smbus_fixup(const struct mem_controller *ctrl)
254 {
255         int i, ram_slots, current_slot = 0;
256         u8 result = 0;
257
258         ram_slots = ARRAY_SIZE(ctrl->channel0);
259         if (!ram_slots) {
260                 print_err("smbus_fixup() thinks there are no RAM slots!\r\n");
261                 return;
262         }
263
264         PRINT_DEBUG("Waiting for SMBus to warm up");
265
266         /*
267          * Bad SPD data should be either 0 or 0xff, but YMMV. So we look for
268          * the ID bytes of SDRAM, DDR, DDR2, and DDR3 (and anything in between).
269          * VT8237R has only been seen on DDR and DDR2 based systems, so far.
270          */
271         for (i = 0; (i < SMBUS_TIMEOUT && ((result < SPD_MEMORY_TYPE_SDRAM) ||
272                                            (result >
273                                             SPD_MEMORY_TYPE_SDRAM_DDR3)));
274              i++) {
275
276                 if (current_slot > ram_slots)
277                         current_slot = 0;
278
279                 result = get_spd_data(ctrl->channel0[current_slot],
280                                       SPD_MEMORY_TYPE);
281                 current_slot++;
282                 PRINT_DEBUG(".");
283         }
284
285         if (i >= SMBUS_TIMEOUT)
286                 print_err("SMBus timed out while warming up\r\n");
287         else
288                 PRINT_DEBUG("Done\r\n");
289 }
290
291 /* Debugging Function */
292 #ifdef DEBUG_SMBUS
293 static void dump_spd_data(void)
294 {
295         int dimm, offset, regs;
296         unsigned int val;
297
298         for (dimm = 0; dimm < 8; dimm++) {
299                 print_debug("SPD Data for DIMM ");
300                 print_debug_hex8(dimm);
301                 print_debug("\r\n");
302
303                 val = get_spd_data(dimm, 0);
304                 if (val == 0xff) {
305                         regs = 256;
306                 } else if (val == 0x80) {
307                         regs = 128;
308                 } else {
309                         print_debug("No DIMM present\r\n");
310                         regs = 0;
311                 }
312                 for (offset = 0; offset < regs; offset++) {
313                         print_debug("  Offset ");
314                         print_debug_hex8(offset);
315                         print_debug(" = 0x");
316                         print_debug_hex8(get_spd_data(dimm, offset));
317                         print_debug("\r\n");
318                 }
319         }
320 }
321 #else
322 #define dump_spd_data()
323 #endif