Various cosmetics, coding style fixes, constifications (trivial).
[coreboot.git] / src / southbridge / via / vt8237r / vt8237r_early_smbus.c
1 /*
2  * This file is part of the LinuxBIOS project.
3  *
4  * Copyright (C) 2007 Corey Osgood <corey_osgood@verizon.net>
5  * Copyright (C) 2007 Rudolf Marek <r.marek@assembler.cz>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
20  */
21
22 #include <device/pci_ids.h>
23 #include <spd.h>
24 #include "vt8237r.h"
25
26 /**
27  * Print an error, should it occur. If no error, just exit.
28  *
29  * @param host_status The data returned on the host status register after
30  *                    a transaction is processed.
31  * @param loops The number of times a transaction was attempted.
32  */
33 static void smbus_print_error(u8 host_status, int loops)
34 {
35         /* Check if there actually was an error. */
36         if ((host_status == 0x00 || host_status == 0x40 ||
37              host_status == 0x42) && (loops < SMBUS_TIMEOUT))
38                 return;
39
40         if (loops >= SMBUS_TIMEOUT)
41                 print_err("SMBus timeout\r\n");
42         if (host_status & (1 << 4))
43                 print_err("Interrupt/SMI# was Failed Bus Transaction\r\n");
44         if (host_status & (1 << 3))
45                 print_err("Bus error\r\n");
46         if (host_status & (1 << 2))
47                 print_err("Device error\r\n");
48         if (host_status & (1 << 1))
49                 print_debug("Interrupt/SMI# completed successfully\r\n");
50         if (host_status & (1 << 0))
51                 print_err("Host busy\r\n");
52 }
53
54 /**
55  * Wait for the SMBus to become ready to process the next transaction.
56  */
57 static void smbus_wait_until_ready(void)
58 {
59         int loops;
60
61         PRINT_DEBUG("Waiting until SMBus ready\r\n");
62
63         loops = 0;
64         /* Yes, this is a mess, but it's the easiest way to do it. */
65         while ((inb(SMBHSTSTAT) & 1) == 1 && loops < SMBUS_TIMEOUT)
66                 ++loops;
67
68         smbus_print_error(inb(SMBHSTSTAT), loops);
69 }
70
71 /**
72  * Reset and take ownership of the SMBus.
73  */
74 static void smbus_reset(void)
75 {
76         outb(HOST_RESET, SMBHSTSTAT);
77
78         /* Datasheet says we have to read it to take ownership of SMBus. */
79         inb(SMBHSTSTAT);
80
81         PRINT_DEBUG("After reset status: ");
82         PRINT_DEBUG_HEX16(inb(SMBHSTSTAT));
83         PRINT_DEBUG("\r\n");
84 }
85
86 /**
87  * Read a byte from the SMBus.
88  *
89  * @param dimm The address location of the DIMM on the SMBus.
90  * @param offset The offset the data is located at.
91  */
92 u8 smbus_read_byte(u8 dimm, u8 offset)
93 {
94         u8 val;
95
96         PRINT_DEBUG("DIMM ");
97         PRINT_DEBUG_HEX16(dimm);
98         PRINT_DEBUG(" OFFSET ");
99         PRINT_DEBUG_HEX16(offset);
100         PRINT_DEBUG("\r\n");
101
102         smbus_reset();
103
104         /* Clear host data port. */
105         outb(0x00, SMBHSTDAT0);
106         SMBUS_DELAY();
107         smbus_wait_until_ready();
108
109         /* Actual addr to reg format. */
110         dimm = (dimm << 1);
111         dimm |= 1;
112         outb(dimm, SMBXMITADD);
113         outb(offset, SMBHSTCMD);
114
115         /* Start transaction, byte data read. */
116         outb(0x48, SMBHSTCTL);
117         SMBUS_DELAY();
118         smbus_wait_until_ready();
119
120         val = inb(SMBHSTDAT0);
121         PRINT_DEBUG("Read: ");
122         PRINT_DEBUG_HEX16(val);
123         PRINT_DEBUG("\r\n");
124
125         /* Probably don't have to do this, but it can't hurt. */
126         smbus_reset();
127
128         return val;
129 }
130
131 /**
132  * Enable the smbus on vt8237r-based systems
133  */
134 void enable_smbus(void)
135 {
136         device_t dev;
137
138         /* Power management controller */
139         dev = pci_locate_device(PCI_ID(PCI_VENDOR_ID_VIA,
140                                        PCI_DEVICE_ID_VIA_VT8237R_LPC), 0);
141
142         if (dev == PCI_DEV_INVALID)
143                 die("Power management controller not found\r\n");
144
145         /* 7 = SMBus Clock from RTC 32.768KHz
146          * 5 = Internal PLL reset from susp
147          */
148         pci_write_config8(dev, VT8237R_POWER_WELL, 0xa0);
149
150         /* Enable SMBus. */
151         pci_write_config16(dev, VT8237R_SMBUS_IO_BASE_REG,
152                            VT8237R_SMBUS_IO_BASE | 0x1);
153
154         /* SMBus Host Configuration, enable. */
155         pci_write_config8(dev, VT8237R_SMBUS_HOST_CONF, 0x01);
156
157         /* Make it work for I/O. */
158         pci_write_config16(dev, PCI_COMMAND, PCI_COMMAND_IO);
159
160         smbus_reset();
161
162         /* Reset the internal pointer. */
163         inb(SMBHSTCTL);
164 }
165
166 /**
167  * A fixup for some systems that need time for the SMBus to "warm up". This is 
168  * needed on some VT823x based systems, where the SMBus spurts out bad data for 
169  * a short time after power on. This has been seen on the VIA Epia series and 
170  * Jetway J7F2-series. It reads the ID byte from SMBus, looking for 
171  * known-good data from a slot/address. Exits on either good data or a timeout.
172  *
173  * TODO: This should probably go into some global file, but one would need to
174  *       be created just for it. If some other chip needs/wants it, we can
175  *       worry about it then.
176  *
177  * @param ctrl The memory controller and SMBus addresses.
178  */
179 void smbus_fixup(const struct mem_controller *ctrl)
180 {
181         int i, ram_slots, current_slot = 0;
182         u8 result = 0;
183
184         ram_slots = ARRAY_SIZE(ctrl->channel0);
185         if (!ram_slots) {
186                 print_err("smbus_fixup() thinks there are no RAM slots!\r\n");
187                 return;
188         }
189
190         PRINT_DEBUG("Waiting for SMBus to warm up");
191
192         /*
193          * Bad SPD data should be either 0 or 0xff, but YMMV. So we look for
194          * the ID bytes of SDRAM, DDR, DDR2, and DDR3 (and anything in between).
195          * VT8237R has only been seen on DDR and DDR2 based systems, so far.
196          */
197         for (i = 0; (i < SMBUS_TIMEOUT && ((result < SPD_MEMORY_TYPE_SDRAM) ||
198                                 (result > SPD_MEMORY_TYPE_SDRAM_DDR3))); i++) {
199
200                 if (current_slot > ram_slots)
201                         current_slot = 0;
202
203                 result = smbus_read_byte(ctrl->channel0[current_slot],
204                                          SPD_MEMORY_TYPE);
205                 current_slot++;
206                 PRINT_DEBUG(".");
207         }
208
209         if (i >= SMBUS_TIMEOUT)
210                 print_err("SMBus timed out while warming up\r\n");
211         else
212                 PRINT_DEBUG("Done\r\n");
213 }