2 * This file is part of the coreboot project.
4 * Copyright (C) 2007 Corey Osgood <corey.osgood@gmail.com>
5 * Copyright (C) 2007 Rudolf Marek <r.marek@assembler.cz>
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.
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.
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
22 #include <device/pci_ids.h>
28 * Print an error, should it occur. If no error, just exit.
30 * @param host_status The data returned on the host status register after
31 * a transaction is processed.
32 * @param loops The number of times a transaction was attempted.
34 static void smbus_print_error(u8 host_status, int loops)
36 /* Check if there actually was an error. */
37 if ((host_status == 0x00 || host_status == 0x40 ||
38 host_status == 0x42) && (loops < SMBUS_TIMEOUT))
41 if (loops >= SMBUS_TIMEOUT)
42 print_err("SMBus timeout\n");
43 if (host_status & (1 << 4))
44 print_err("Interrupt/SMI# was Failed Bus Transaction\n");
45 if (host_status & (1 << 3))
46 print_err("Bus error\n");
47 if (host_status & (1 << 2))
48 print_err("Device error\n");
49 if (host_status & (1 << 1))
50 print_debug("Interrupt/SMI# completed successfully\n");
51 if (host_status & (1 << 0))
52 print_err("Host busy\n");
56 * Wait for the SMBus to become ready to process the next transaction.
58 static void smbus_wait_until_ready(void)
62 PRINT_DEBUG("Waiting until SMBus ready\n");
64 /* Loop up to SMBUS_TIMEOUT times, waiting for bit 0 of the
65 * SMBus Host Status register to go to 0, indicating the operation
66 * was completed successfully. I don't remember why I did it this way,
67 * but I think it was because ROMCC was running low on registers */
69 while ((inb(SMBHSTSTAT) & 1) == 1 && loops < SMBUS_TIMEOUT)
72 smbus_print_error(inb(SMBHSTSTAT), loops);
76 * Reset and take ownership of the SMBus.
78 static void smbus_reset(void)
80 outb(HOST_RESET, SMBHSTSTAT);
82 /* Datasheet says we have to read it to take ownership of SMBus. */
85 PRINT_DEBUG("After reset status: ");
86 PRINT_DEBUG_HEX16(inb(SMBHSTSTAT));
91 * Read a byte from the SMBus.
93 * @param dimm The address location of the DIMM on the SMBus.
94 * @param offset The offset the data is located at.
96 u8 smbus_read_byte(u8 dimm, u8 offset)
100 PRINT_DEBUG("DIMM ");
101 PRINT_DEBUG_HEX16(dimm);
102 PRINT_DEBUG(" OFFSET ");
103 PRINT_DEBUG_HEX16(offset);
108 /* Clear host data port. */
109 outb(0x00, SMBHSTDAT0);
111 smbus_wait_until_ready();
113 /* Actual addr to reg format. */
116 outb(dimm, SMBXMITADD);
117 outb(offset, SMBHSTCMD);
119 /* Start transaction, byte data read. */
120 outb(0x48, SMBHSTCTL);
122 smbus_wait_until_ready();
124 val = inb(SMBHSTDAT0);
125 PRINT_DEBUG("Read: ");
126 PRINT_DEBUG_HEX16(val);
129 /* Probably don't have to do this, but it can't hurt. */
135 #define PSONREADY_TIMEOUT 0x7fffffff
137 static device_t get_vt8237_lpc(void)
141 /* Power management controller */
142 dev = pci_locate_device(PCI_ID(PCI_VENDOR_ID_VIA,
143 PCI_DEVICE_ID_VIA_VT8237R_LPC), 0);
144 if (dev != PCI_DEV_INVALID)
147 dev = pci_locate_device(PCI_ID(PCI_VENDOR_ID_VIA,
148 PCI_DEVICE_ID_VIA_VT8237S_LPC), 0);
149 if (dev != PCI_DEV_INVALID)
152 dev = pci_locate_device(PCI_ID(PCI_VENDOR_ID_VIA,
153 PCI_DEVICE_ID_VIA_VT8237A_LPC), 0);
158 * Enable the SMBus on VT8237R-based systems.
160 void enable_smbus(void)
165 /* Power management controller */
166 dev = get_vt8237_lpc();
167 if (dev == PCI_DEV_INVALID)
168 die("Power management controller not found\n");
170 /* Make sure the RTC power well is up before touching smbus. */
172 while (!(pci_read_config8(dev, VT8237R_PSON) & (1<<6))
173 && loops < PSONREADY_TIMEOUT)
177 * 7 = SMBus Clock from RTC 32.768KHz
178 * 5 = Internal PLL reset from susp
180 pci_write_config8(dev, VT8237R_POWER_WELL, 0xa0);
183 pci_write_config16(dev, VT8237R_SMBUS_IO_BASE_REG,
184 VT8237R_SMBUS_IO_BASE | 0x1);
186 /* SMBus Host Configuration, enable. */
187 pci_write_config8(dev, VT8237R_SMBUS_HOST_CONF, 0x01);
189 /* Make it work for I/O. */
190 pci_write_config16(dev, PCI_COMMAND, PCI_COMMAND_IO);
194 /* Reset the internal pointer. */
199 * A fixup for some systems that need time for the SMBus to "warm up". This is
200 * needed on some VT823x based systems, where the SMBus spurts out bad data for
201 * a short time after power on. This has been seen on the VIA Epia series and
202 * Jetway J7F2-series. It reads the ID byte from SMBus, looking for
203 * known-good data from a slot/address. Exits on either good data or a timeout.
205 * TODO: This should probably go into some global file, but one would need to
206 * be created just for it. If some other chip needs/wants it, we can
207 * worry about it then.
209 * @param ctrl The memory controller and SMBus addresses.
211 void smbus_fixup(const struct mem_controller *ctrl)
213 int i, ram_slots, current_slot = 0;
216 ram_slots = ARRAY_SIZE(ctrl->channel0);
218 print_err("smbus_fixup() thinks there are no RAM slots!\n");
222 PRINT_DEBUG("Waiting for SMBus to warm up");
225 * Bad SPD data should be either 0 or 0xff, but YMMV. So we look for
226 * the ID bytes of SDRAM, DDR, DDR2, and DDR3 (and anything in between).
227 * VT8237R has only been seen on DDR and DDR2 based systems, so far.
229 for (i = 0; (i < SMBUS_TIMEOUT && ((result < SPD_MEMORY_TYPE_SDRAM) ||
230 (result > SPD_MEMORY_TYPE_SDRAM_DDR3))); i++) {
232 if (current_slot > ram_slots)
235 result = smbus_read_byte(ctrl->channel0[current_slot],
241 if (i >= SMBUS_TIMEOUT)
242 print_err("SMBus timed out while warming up\n");
244 PRINT_DEBUG("Done\n");
247 /* FIXME: Better separate the NB and SB, will be done once it works. */
249 void vt8237_sb_enable_fid_vid(void)
251 device_t dev, devctl;
254 /* Power management controller */
255 dev = get_vt8237_lpc();
256 if (dev == PCI_DEV_INVALID)
259 devid = pci_read_config16(dev, PCI_DEVICE_ID);
263 /* Set ACPI base address to I/O VT8237R_ACPI_IO_BASE. */
264 pci_write_config16(dev, 0x88, VT8237R_ACPI_IO_BASE | 0x1);
266 /* Enable ACPI accessm RTC signal gated with PSON. */
267 pci_write_config8(dev, 0x81, 0x84);
269 /* chipset-specific parts */
271 /* VLINK: FIXME: can we drop the devid check and just look for the VLINK device? */
272 if (devid == PCI_DEVICE_ID_VIA_VT8237S_LPC ||
273 devid == PCI_DEVICE_ID_VIA_VT8237A_LPC) {
274 devctl = pci_locate_device(PCI_ID(PCI_VENDOR_ID_VIA,
275 PCI_DEVICE_ID_VIA_VT8237_VLINK), 0);
277 if (devctl != PCI_DEV_INVALID) {
278 /* So the chip knows we are on AMD. */
279 pci_write_config8(devctl, 0x7c, 0x7f);
283 if (devid == PCI_DEVICE_ID_VIA_VT8237S_LPC) {
285 * Allow SLP# signal to assert LDTSTOP_L.
286 * Will work for C3 and for FID/VID change.
289 outb(0xff, VT8237R_ACPI_IO_BASE + 0x50);
291 /* Reduce further the STPCLK/LDTSTP signal to 5us. */
292 pci_write_config8(dev, 0xec, 0x4);
297 /* VT8237R and VT8237A */
300 * Allow SLP# signal to assert LDTSTOP_L.
301 * Will work for C3 and for FID/VID change.
303 outb(0x1, VT8237R_ACPI_IO_BASE + 0x11);
306 void enable_rom_decode(void)
310 /* Power management controller */
311 dev = get_vt8237_lpc();
312 if (dev == PCI_DEV_INVALID)
315 /* ROM decode last 1MB FFC00000 - FFFFFFFF. */
316 pci_write_config8(dev, 0x41, 0x7f);
319 #if CONFIG_HAVE_ACPI_RESUME == 1
320 static int acpi_is_wakeup_early(void) {
324 print_debug("IN TEST WAKEUP\n");
326 /* Power management controller */
327 dev = get_vt8237_lpc();
328 if (dev == PCI_DEV_INVALID)
329 die("Power management controller not found\n");
331 /* Set ACPI base address to I/O VT8237R_ACPI_IO_BASE. */
332 pci_write_config16(dev, 0x88, VT8237R_ACPI_IO_BASE | 0x1);
334 /* Enable ACPI accessm RTC signal gated with PSON. */
335 pci_write_config8(dev, 0x81, 0x84);
337 tmp = inw(VT8237R_ACPI_IO_BASE + 0x04);
339 print_debug_hex8(tmp);
340 return ((tmp & (7 << 10)) >> 10) == 1 ? 3 : 0 ;
344 #if defined(__GNUC__)
345 void vt8237_early_spi_init(void)
348 volatile u16 *spireg;
351 /* Bus Control and Power Management */
352 dev = pci_locate_device(PCI_ID(PCI_VENDOR_ID_VIA,
353 PCI_DEVICE_ID_VIA_VT8237S_LPC), 0);
355 if (dev == PCI_DEV_INVALID)
356 die("SB not found\n");
358 /* Put SPI base 20 d0 fe. */
359 tmp = pci_read_config32(dev, 0xbc);
360 pci_write_config32(dev, 0xbc,
361 (VT8237S_SPI_MEM_BASE >> 8) | (tmp & 0xFF000000));
363 /* Set SPI clock to 33MHz. */
364 spireg = (u16 *) (VT8237S_SPI_MEM_BASE + 0x6c);
369 /* This #if is special. ROMCC chokes on the (rom == NULL) comparison.
370 * Since the whole function is only called for one target and that target
371 * is compiled with GCC, hide the function from ROMCC and be happy.
373 #if defined(__GNUC__)
377 * 19:16 4 bit position in shadow EEPROM
382 * 27 ERDBG - enable read from 0x5c
385 * 24 SEEPR - write 1 when done updating, wait until SEELD is
387 * cleared by reset, if it is 1 writing is disabled
388 * 19:16 4 bit position in shadow EEPROM
389 * 15:0 data from shadow EEPROM
391 * After PCIRESET SEELD and SEEPR must be 1 and 1.
394 /* 1 = needs PCI reset, 0 don't reset, network initialized. */
396 /* FIXME: Maybe close the debug register after use? */
398 #define LAN_TIMEOUT 0x7FFFFFFF
400 int vt8237_early_network_init(struct vt8237_network_rom *rom)
402 struct vt8237_network_rom n;
408 unsigned int checksum;
410 /* Network adapter */
411 dev = pci_locate_device(PCI_ID(PCI_VENDOR_ID_VIA,
412 PCI_DEVICE_ID_VIA_8233_7), 0);
413 if (dev == PCI_DEV_INVALID) {
414 print_err("Network is disabled, please enable\n");
418 tmp = pci_read_config32(dev, 0x5c);
419 tmp |= 0x08000000; /* Enable ERDBG. */
420 pci_write_config32(dev, 0x5c, tmp);
422 status = ((pci_read_config32(dev, 0x5c) >> 24) & 0x3);
424 /* Network controller OK, EEPROM loaded. */
429 print_err("No config data specified, using default MAC!\n");
430 n.mac_address[0] = 0x0;
431 n.mac_address[1] = 0x0;
432 n.mac_address[2] = 0xde;
433 n.mac_address[3] = 0xad;
434 n.mac_address[4] = 0xbe;
435 n.mac_address[5] = 0xef;
444 n.pmu_data_reg = 0x0;
459 rom_write = (u16 *) rom;
461 /* Write all data except checksum and second to last byte. */
462 tmp &= 0xff000000; /* Leave reserved bits in. */
463 for (i = 0; i < 15; i++) {
464 pci_write_config32(dev, 0x58, tmp | (i << 16) | rom_write[i]);
465 /* Lame code FIXME */
466 checksum += rom_write[i] & 0xff;
467 /* checksum %= 256; */
468 checksum += (rom_write[i] >> 8) & 0xff;
469 /* checksum %= 256; */
472 checksum += (rom_write[15] & 0xff);
473 checksum = ~(checksum & 0xff);
474 tmp |= (((checksum & 0xff) << 8) | rom_write[15]);
476 /* Write last byte and checksum. */
477 pci_write_config32(dev, 0x58, (15 << 16) | tmp);
479 tmp = pci_read_config32(dev, 0x5c);
480 pci_write_config32(dev, 0x5c, tmp | 0x01000000); /* Toggle SEEPR. */
482 /* Yes, this is a mess, but it's the easiest way to do it. */
483 /* XXX not so messy, but an explanation of the hack would have been better */
485 while ((((pci_read_config32(dev, 0x5c) >> 25) & 1) == 0)
486 && (loops < LAN_TIMEOUT)) {
490 if (loops >= LAN_TIMEOUT) {
491 print_err("Timeout - LAN controller didn't accept config\n");
495 /* We are done, config will be used after PCIRST#. */