2 * This file is part of the coreboot project.
4 * Copyright (C) 2009 One Laptop per Child, Association, Inc.
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.
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.
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
20 #include <device/pci_ids.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
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
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
45 #define SMBUS_TIMEOUT (100*1000*10)
47 #define I2C_TRANS_CMD 0x40
48 #define CLOCK_SLAVE_ADDRESS 0x69
50 #define SMBUS_DELAY() outb(0x80, 0x80)
52 /* Debugging macros. Only necessary if something isn't working right */
57 #define PRINT_DEBUG(x) print_debug(x)
58 #define PRINT_DEBUG_HEX16(x) print_debug_hex16(x)
60 #define PRINT_DEBUG(x)
61 #define PRINT_DEBUG_HEX16(x)
64 /* Internal functions */
65 static void smbus_print_error(unsigned char host_status_register, int loops)
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)
72 print_err("smbus_error: ");
73 print_err_hex8(host_status_register);
75 if (loops >= SMBUS_TIMEOUT) {
76 print_err("SMBus Timout\r\n");
78 if (host_status_register & (1 << 4)) {
79 print_err("Interrup/SMI# was Failed Bus Transaction\r\n");
81 if (host_status_register & (1 << 3)) {
82 print_err("Bus Error\r\n");
84 if (host_status_register & (1 << 2)) {
85 print_err("Device Error\r\n");
87 if (host_status_register & (1 << 1)) {
88 /* This isn't a real error... */
89 print_debug("Interrupt/SMI# was Successful Completion\r\n");
91 if (host_status_register & (1 << 0)) {
92 print_err("Host Busy\r\n");
96 static void smbus_wait_until_ready(void)
101 /* Yes, this is a mess, but it's the easiest way to do it */
102 while (((inb(SMBHSTSTAT) & 1) == 1) && (loops <= SMBUS_TIMEOUT)) {
106 smbus_print_error(inb(SMBHSTSTAT), loops);
109 static void smbus_reset(void)
111 outb(HOST_RESET, SMBHSTSTAT);
114 /* Public functions */
115 static unsigned int set_ics_data(unsigned char dev, int data, char len)
118 /* clear host data port */
119 outb(0x00, SMBHSTDAT0);
121 smbus_wait_until_ready();
123 /* read to reset block transfer counter */
126 /* fill blocktransfer array */
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);
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);
156 //for (i=0; i < len; i++)
157 // outb(data[i],SMBBLKDAT);
159 outb(dev, SMBXMITADD);
161 outb(len, SMBHSTDAT0);
162 outb(0x74, SMBHSTCTL);
166 smbus_wait_until_ready();
172 static unsigned int get_spd_data(unsigned int dimm, unsigned int offset)
177 /* clear host data port */
178 outb(0x00, SMBHSTDAT0);
180 smbus_wait_until_ready();
182 /* Do some mathmatic magic */
187 outb(dimm | 0x1, SMBXMITADD);
188 outb(offset, SMBHSTCMD);
189 outb(0x48, SMBHSTCTL);
193 smbus_wait_until_ready();
195 val = inb(SMBHSTDAT0);
200 static void enable_smbus(void)
205 pci_locate_device(PCI_ID
206 (PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_VX855_LPC),
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");
214 /* Set clock source */
215 pci_write_config8(dev, 0x94, 0x20);
217 /* Write SMBus IO base to 0xd0, and enable SMBus */
218 pci_write_config16(dev, 0xd0, SMBUS_IO_BASE | 1);
220 /* Set to Award value */
221 pci_write_config8(dev, 0xd2, 0x05);
223 /* Make it work for I/O ... */
224 pci_write_config16(dev, 0x04, 0x0003);
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);
234 /* clear host data port */
235 outb(0x00, SMBHSTDAT0);
237 smbus_wait_until_ready();
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.
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.
251 * @param ctrl The memory controller and SMBus addresses.
253 void smbus_fixup(const struct mem_controller *ctrl)
255 int i, ram_slots, current_slot = 0;
258 ram_slots = ARRAY_SIZE(ctrl->channel0);
260 print_err("smbus_fixup() thinks there are no RAM slots!\r\n");
264 PRINT_DEBUG("Waiting for SMBus to warm up");
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.
271 for (i = 0; (i < SMBUS_TIMEOUT && ((result < SPD_MEMORY_TYPE_SDRAM) ||
273 SPD_MEMORY_TYPE_SDRAM_DDR3)));
276 if (current_slot > ram_slots)
279 result = get_spd_data(ctrl->channel0[current_slot],
285 if (i >= SMBUS_TIMEOUT)
286 print_err("SMBus timed out while warming up\r\n");
288 PRINT_DEBUG("Done\r\n");
291 /* Debugging Function */
293 static void dump_spd_data(void)
295 int dimm, offset, regs;
298 for (dimm = 0; dimm < 8; dimm++) {
299 print_debug("SPD Data for DIMM ");
300 print_debug_hex8(dimm);
303 val = get_spd_data(dimm, 0);
306 } else if (val == 0x80) {
309 print_debug("No DIMM present\r\n");
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));
322 #define dump_spd_data()