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>
23 #define SMBUS_IO_BASE 0x0500 //from award bios
24 #define PMIO_BASE VX800_ACPI_IO_BASE //might as well set this while we're here
26 #define SMBHSTSTAT SMBUS_IO_BASE + 0x0
27 #define SMBSLVSTAT SMBUS_IO_BASE + 0x1
28 #define SMBHSTCTL SMBUS_IO_BASE + 0x2
29 #define SMBHSTCMD SMBUS_IO_BASE + 0x3
30 #define SMBXMITADD SMBUS_IO_BASE + 0x4
31 #define SMBHSTDAT0 SMBUS_IO_BASE + 0x5
32 #define SMBHSTDAT1 SMBUS_IO_BASE + 0x6
33 /* Rest of these aren't currently used... */
34 #define SMBBLKDAT SMBUS_IO_BASE + 0x7
35 #define SMBSLVCTL SMBUS_IO_BASE + 0x8
36 #define SMBTRNSADD SMBUS_IO_BASE + 0x9
37 #define SMBSLVDATA SMBUS_IO_BASE + 0xa
38 #define SMLINK_PIN_CTL SMBUS_IO_BASE + 0xe
39 #define SMBUS_PIN_CTL SMBUS_IO_BASE + 0xf
41 /* Define register settings */
42 #define HOST_RESET 0xff
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 #if CONFIG_DEBUG_SMBUS
53 #define PRINT_DEBUG(x) print_debug(x)
54 #define PRINT_DEBUG_HEX16(x) print_debug_hex16(x)
56 #define PRINT_DEBUG(x)
57 #define PRINT_DEBUG_HEX16(x)
60 /* Internal functions */
61 static void smbus_print_error(unsigned char host_status_register, int loops)
63 // print_err("some i2c error\n");
64 /* Check if there actually was an error */
65 if (host_status_register == 0x00 || host_status_register == 0x40 ||
66 host_status_register == 0x42)
68 print_err("smbus_error: ");
69 print_err_hex8(host_status_register);
71 if (loops >= SMBUS_TIMEOUT) {
72 print_err("SMBus Timout\n");
74 if (host_status_register & (1 << 4)) {
75 print_err("Interrup/SMI# was Failed Bus Transaction\n");
77 if (host_status_register & (1 << 3)) {
78 print_err("Bus Error\n");
80 if (host_status_register & (1 << 2)) {
81 print_err("Device Error\n");
83 if (host_status_register & (1 << 1)) {
84 /* This isn't a real error... */
85 print_debug("Interrupt/SMI# was Successful Completion\n");
87 if (host_status_register & (1 << 0)) {
88 print_err("Host Busy\n");
92 static void smbus_wait_until_ready(void)
97 /* Yes, this is a mess, but it's the easiest way to do it */
98 while (((inb(SMBHSTSTAT) & 1) == 1) && (loops <= SMBUS_TIMEOUT)) {
102 smbus_print_error(inb(SMBHSTSTAT), loops);
105 static void smbus_reset(void)
107 outb(HOST_RESET, SMBHSTSTAT);
110 /* Public functions */
112 static unsigned int get_spd_data(unsigned int dimm, unsigned int offset)
117 /* clear host data port */
118 outb(0x00, SMBHSTDAT0);
120 smbus_wait_until_ready();
122 /* Do some mathmatic magic */
123 dimm = (DIMM0 + dimm) << 1;
125 outb(dimm | 0x1, SMBXMITADD);
126 outb(offset, SMBHSTCMD);
127 outb(0x48, SMBHSTCTL);
131 smbus_wait_until_ready();
133 val = inb(SMBHSTDAT0);
138 void enable_smbus(void)
142 dev = pci_locate_device(PCI_ID(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_VX855_LPC), 0);
144 if (dev == PCI_DEV_INVALID) {
145 /* This won't display text if enable_smbus() is before serial init */
146 die("Power Managment Controller not found\n");
149 /* Set clock source */
150 pci_write_config8(dev, 0x94, 0x20);
152 /* Write SMBus IO base to 0xd0, and enable SMBus */
153 pci_write_config16(dev, 0xd0, SMBUS_IO_BASE | 1);
155 /* Set to Award value */
156 pci_write_config8(dev, 0xd2, 0x05);
158 /* Make it work for I/O ... */
159 pci_write_config16(dev, 0x04, 0x0003);
162 /* clear host data port */
163 outb(0x00, SMBHSTDAT0);
165 smbus_wait_until_ready();
169 * A fixup for some systems that need time for the SMBus to "warm up". This is
170 * needed on some VT823x based systems, where the SMBus spurts out bad data for
171 * a short time after power on. This has been seen on the VIA Epia series and
172 * Jetway J7F2-series. It reads the ID byte from SMBus, looking for
173 * known-good data from a slot/address. Exits on either good data or a timeout.
175 * TODO: This should probably go into some global file, but one would need to
176 * be created just for it. If some other chip needs/wants it, we can
177 * worry about it then.
179 * @param mem_ctrl The memory controller and SMBus addresses.
181 void smbus_fixup(const struct mem_controller *mem_ctrl)
183 int i, ram_slots, current_slot = 0;
186 ram_slots = ARRAY_SIZE(mem_ctrl->channel0);
188 print_err("smbus_fixup() thinks there are no RAM slots!\n");
192 PRINT_DEBUG("Waiting for SMBus to warm up");
195 * Bad SPD data should be either 0 or 0xff, but YMMV. So we look for
196 * the ID bytes of SDRAM, DDR, DDR2, and DDR3 (and anything in between).
197 * VT8237R has only been seen on DDR and DDR2 based systems, so far.
199 for (i = 0; (i < SMBUS_TIMEOUT && ((result < SPD_MEMORY_TYPE_SDRAM) ||
201 SPD_MEMORY_TYPE_SDRAM_DDR3)));
204 if (current_slot > ram_slots)
207 result = get_spd_data(mem_ctrl->channel0[current_slot],
213 if (i >= SMBUS_TIMEOUT)
214 print_err("SMBus timed out while warming up\n");
216 PRINT_DEBUG("Done\n");
219 /* Debugging Function */
220 #if CONFIG_DEBUG_SMBUS
221 static void dump_spd_data(void)
223 int dimm, offset, regs;
226 for (dimm = 0; dimm < 8; dimm++) {
227 print_debug("SPD Data for DIMM ");
228 print_debug_hex8(dimm);
231 val = get_spd_data(dimm, 0);
234 } else if (val == 0x80) {
237 print_debug("No DIMM present\n");
240 for (offset = 0; offset < regs; offset++) {
241 print_debug(" Offset ");
242 print_debug_hex8(offset);
243 print_debug(" = 0x");
244 print_debug_hex8(get_spd_data(dimm, offset));
250 #define dump_spd_data()