2 * This file is part of the coreboot project.
4 * Copyright (C) 2007-2009 coresystems GmbH
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 // other bioses use this, too:
21 #define SMBUS_IO_BASE 0x0500
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
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
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
43 #define SMBUS_TIMEOUT (100*1000*10)
45 #define I2C_TRANS_CMD 0x40
46 #define CLOCK_SLAVE_ADDRESS 0x69
48 #define SMBUS_DELAY() outb(0x80, 0x80)
50 /* Debugging macros. */
52 // #define DEBUG_SMBUS 1
55 #define PRINT_DEBUG(x) print_debug(x)
56 #define PRINT_DEBUG_HEX16(x) print_debug_hex16(x)
58 #define PRINT_DEBUG(x)
59 #define PRINT_DEBUG_HEX16(x)
62 /* Internal functions */
63 static void smbus_print_error(unsigned char host_status_register, int loops)
65 /* Check if there actually was an error */
66 if (host_status_register == 0x00 || host_status_register == 0x40 ||
67 host_status_register == 0x42)
69 print_err("SMBus Error: ");
70 print_err_hex8(host_status_register);
73 if (loops >= SMBUS_TIMEOUT) {
74 print_err("SMBus Timout\r\n");
76 if (host_status_register & (1 << 4)) {
77 print_err("Interrup/SMI# was Failed Bus Transaction\r\n");
79 if (host_status_register & (1 << 3)) {
80 print_err("Bus Error\r\n");
82 if (host_status_register & (1 << 2)) {
83 print_err("Device Error\r\n");
85 if (host_status_register & (1 << 1)) {
86 /* This isn't a real error... */
87 print_debug("Interrupt/SMI# was Successful Completion\r\n");
89 if (host_status_register & (1 << 0)) {
90 print_err("Host Busy\r\n");
94 static void smbus_wait_until_ready(void)
100 /* Yes, this is a mess, but it's the easiest way to do it */
101 while (((inb(SMBHSTSTAT) & 1) == 1) && (loops <= SMBUS_TIMEOUT)) {
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
110 smbus_print_error(inb(SMBHSTSTAT), loops);
114 static void smbus_reset(void)
116 outb(HOST_RESET, SMBHSTSTAT);
119 /* Public functions */
120 static void set_ics_data(unsigned char dev, int data, char len)
124 /* clear host data port */
125 outb(0x00, SMBHSTDAT0);
127 smbus_wait_until_ready();
129 /* read to reset block transfer counter */
132 /* fill blocktransfer array */
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);
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);
162 //for (i=0; i < len; i++)
163 // outb(data[i],SMBBLKDAT);
165 outb(dev, SMBXMITADD);
167 outb(len, SMBHSTDAT0);
168 outb(0x74, SMBHSTCTL);
172 smbus_wait_until_ready();
178 static unsigned int get_spd_data(const struct mem_controller *ctrl, unsigned int dimm,
181 unsigned int val, addr;
185 /* clear host data port */
186 outb(0x00, SMBHSTDAT0);
188 smbus_wait_until_ready();
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
195 addr = (ctrl->channel0[dimm]) << 1;
197 outb(addr | 0x1, SMBXMITADD);
198 outb(offset, SMBHSTCMD);
199 outb(0x48, SMBHSTCTL);
203 smbus_wait_until_ready();
205 val = inb(SMBHSTDAT0);
210 static void enable_smbus(void)
214 /* The CX700 ISA Bridge (0x1106, 0x8324) is hardcoded to this location,
217 dev = PCI_DEV(0, 17, 0);
219 /* SMBus Clock Select: Divider fof 14.318MHz */
220 pci_write_config8(dev, 0x94, 0x20);
222 /* SMBus I/O Base, enable SMBus */
223 pci_write_config16(dev, 0xd0, SMBUS_IO_BASE | 1);
225 /* SMBus Clock from 128K Source, Enable SMBus Host Controller */
226 pci_write_config8(dev, 0xd2, 0x05);
228 /* Enable I/O decoding */
229 pci_write_config16(dev, 0x04, 0x0003);
231 /* Setup clock chips */
232 set_ics_data(0xd2, 0, 14);
233 set_ics_data(0xd4, 0, 9);
236 /* Debugging Function */
238 static void dump_spd_data(const struct mem_controller *ctrl)
240 int dimm, offset, regs;
243 for (dimm = 0; dimm < DIMM_SOCKETS; dimm++) {
244 print_debug("SPD Data for DIMM ");
245 print_debug_hex8(dimm);
248 val = get_spd_data(ctrl, dimm, 0);
251 } else if (val == 0x80) {
254 print_debug("No DIMM present\r\n");
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));
267 #define dump_spd_data(ctrl)