remove trailing whitespace
[coreboot.git] / src / northbridge / via / 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
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
25
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
40
41 /* Define register settings */
42 #define HOST_RESET      0xff
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 #if CONFIG_DEBUG_SMBUS
53 #define PRINT_DEBUG(x)          print_debug(x)
54 #define PRINT_DEBUG_HEX16(x)    print_debug_hex16(x)
55 #else
56 #define PRINT_DEBUG(x)
57 #define PRINT_DEBUG_HEX16(x)
58 #endif
59
60 /* Internal functions */
61 static void smbus_print_error(unsigned char host_status_register, int loops)
62 {
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)
67                 return;
68         print_err("smbus_error: ");
69         print_err_hex8(host_status_register);
70         print_err("\n");
71         if (loops >= SMBUS_TIMEOUT) {
72                 print_err("SMBus Timout\n");
73         }
74         if (host_status_register & (1 << 4)) {
75                 print_err("Interrup/SMI# was Failed Bus Transaction\n");
76         }
77         if (host_status_register & (1 << 3)) {
78                 print_err("Bus Error\n");
79         }
80         if (host_status_register & (1 << 2)) {
81                 print_err("Device Error\n");
82         }
83         if (host_status_register & (1 << 1)) {
84                 /* This isn't a real error... */
85                 print_debug("Interrupt/SMI# was Successful Completion\n");
86         }
87         if (host_status_register & (1 << 0)) {
88                 print_err("Host Busy\n");
89         }
90 }
91
92 static void smbus_wait_until_ready(void)
93 {
94         int loops;
95
96         loops = 0;
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         smbus_print_error(inb(SMBHSTSTAT), loops);
103 }
104
105 static void smbus_reset(void)
106 {
107         outb(HOST_RESET, SMBHSTSTAT);
108 }
109
110 /* Public functions */
111
112 static unsigned int get_spd_data(unsigned int dimm, unsigned int offset)
113 {
114         unsigned int val;
115
116         smbus_reset();
117         /* clear host data port */
118         outb(0x00, SMBHSTDAT0);
119         SMBUS_DELAY();
120         smbus_wait_until_ready();
121
122         /* Do some mathmatic magic */
123         dimm = (DIMM0 + dimm) << 1;
124
125         outb(dimm | 0x1, SMBXMITADD);
126         outb(offset, SMBHSTCMD);
127         outb(0x48, SMBHSTCTL);
128
129         SMBUS_DELAY();
130
131         smbus_wait_until_ready();
132
133         val = inb(SMBHSTDAT0);
134         smbus_reset();
135         return val;
136 }
137
138 void enable_smbus(void)
139 {
140         device_t dev;
141
142         dev = pci_locate_device(PCI_ID(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_VX855_LPC), 0);
143
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");
147         }
148
149         /* Set clock source */
150         pci_write_config8(dev, 0x94, 0x20);
151
152         /* Write SMBus IO base to 0xd0, and enable SMBus */
153         pci_write_config16(dev, 0xd0, SMBUS_IO_BASE | 1);
154
155         /* Set to Award value */
156         pci_write_config8(dev, 0xd2, 0x05);
157
158         /* Make it work for I/O ... */
159         pci_write_config16(dev, 0x04, 0x0003);
160
161         smbus_reset();
162         /* clear host data port */
163         outb(0x00, SMBHSTDAT0);
164         SMBUS_DELAY();
165         smbus_wait_until_ready();
166 }
167
168 /**
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.
174  *
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.
178  *
179  * @param mem_ctrl The memory controller and SMBus addresses.
180  */
181 void smbus_fixup(const struct mem_controller *mem_ctrl)
182 {
183         int i, ram_slots, current_slot = 0;
184         u8 result = 0;
185
186         ram_slots = ARRAY_SIZE(mem_ctrl->channel0);
187         if (!ram_slots) {
188                 print_err("smbus_fixup() thinks there are no RAM slots!\n");
189                 return;
190         }
191
192         PRINT_DEBUG("Waiting for SMBus to warm up");
193
194         /*
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.
198          */
199         for (i = 0; (i < SMBUS_TIMEOUT && ((result < SPD_MEMORY_TYPE_SDRAM) ||
200                                            (result >
201                                             SPD_MEMORY_TYPE_SDRAM_DDR3)));
202              i++) {
203
204                 if (current_slot > ram_slots)
205                         current_slot = 0;
206
207                 result = get_spd_data(mem_ctrl->channel0[current_slot],
208                                       SPD_MEMORY_TYPE);
209                 current_slot++;
210                 PRINT_DEBUG(".");
211         }
212
213         if (i >= SMBUS_TIMEOUT)
214                 print_err("SMBus timed out while warming up\n");
215         else
216                 PRINT_DEBUG("Done\n");
217 }
218
219 /* Debugging Function */
220 #if CONFIG_DEBUG_SMBUS
221 static void dump_spd_data(void)
222 {
223         int dimm, offset, regs;
224         unsigned int val;
225
226         for (dimm = 0; dimm < 8; dimm++) {
227                 print_debug("SPD Data for DIMM ");
228                 print_debug_hex8(dimm);
229                 print_debug("\n");
230
231                 val = get_spd_data(dimm, 0);
232                 if (val == 0xff) {
233                         regs = 256;
234                 } else if (val == 0x80) {
235                         regs = 128;
236                 } else {
237                         print_debug("No DIMM present\n");
238                         regs = 0;
239                 }
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));
245                         print_debug("\n");
246                 }
247         }
248 }
249 #else
250 #define dump_spd_data()
251 #endif