49f3d900204e4c93495b061965529191a35a9e3a
[coreboot.git] / src / southbridge / via / vt8237r / vt8237r_early_smbus.c
1 /*
2  * This file is part of the coreboot 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 <stdlib.h>
25 #include "vt8237r.h"
26
27 /**
28  * Print an error, should it occur. If no error, just exit.
29  *
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.
33  */
34 static void smbus_print_error(u8 host_status, int loops)
35 {
36         /* Check if there actually was an error. */
37         if ((host_status == 0x00 || host_status == 0x40 ||
38              host_status == 0x42) && (loops < SMBUS_TIMEOUT))
39                 return;
40
41         if (loops >= SMBUS_TIMEOUT)
42                 print_err("SMBus timeout\r\n");
43         if (host_status & (1 << 4))
44                 print_err("Interrupt/SMI# was Failed Bus Transaction\r\n");
45         if (host_status & (1 << 3))
46                 print_err("Bus error\r\n");
47         if (host_status & (1 << 2))
48                 print_err("Device error\r\n");
49         if (host_status & (1 << 1))
50                 print_debug("Interrupt/SMI# completed successfully\r\n");
51         if (host_status & (1 << 0))
52                 print_err("Host busy\r\n");
53 }
54
55 /**
56  * Wait for the SMBus to become ready to process the next transaction.
57  */
58 static void smbus_wait_until_ready(void)
59 {
60         int loops;
61
62         PRINT_DEBUG("Waiting until SMBus ready\r\n");
63
64         loops = 0;
65         /* Yes, this is a mess, but it's the easiest way to do it. */
66         while ((inb(SMBHSTSTAT) & 1) == 1 && loops < SMBUS_TIMEOUT)
67                 ++loops;
68
69         smbus_print_error(inb(SMBHSTSTAT), loops);
70 }
71
72 /**
73  * Reset and take ownership of the SMBus.
74  */
75 static void smbus_reset(void)
76 {
77         outb(HOST_RESET, SMBHSTSTAT);
78
79         /* Datasheet says we have to read it to take ownership of SMBus. */
80         inb(SMBHSTSTAT);
81
82         PRINT_DEBUG("After reset status: ");
83         PRINT_DEBUG_HEX16(inb(SMBHSTSTAT));
84         PRINT_DEBUG("\r\n");
85 }
86
87 /**
88  * Read a byte from the SMBus.
89  *
90  * @param dimm The address location of the DIMM on the SMBus.
91  * @param offset The offset the data is located at.
92  */
93 u8 smbus_read_byte(u8 dimm, u8 offset)
94 {
95         u8 val;
96
97         PRINT_DEBUG("DIMM ");
98         PRINT_DEBUG_HEX16(dimm);
99         PRINT_DEBUG(" OFFSET ");
100         PRINT_DEBUG_HEX16(offset);
101         PRINT_DEBUG("\r\n");
102
103         smbus_reset();
104
105         /* Clear host data port. */
106         outb(0x00, SMBHSTDAT0);
107         SMBUS_DELAY();
108         smbus_wait_until_ready();
109
110         /* Actual addr to reg format. */
111         dimm = (dimm << 1);
112         dimm |= 1;
113         outb(dimm, SMBXMITADD);
114         outb(offset, SMBHSTCMD);
115
116         /* Start transaction, byte data read. */
117         outb(0x48, SMBHSTCTL);
118         SMBUS_DELAY();
119         smbus_wait_until_ready();
120
121         val = inb(SMBHSTDAT0);
122         PRINT_DEBUG("Read: ");
123         PRINT_DEBUG_HEX16(val);
124         PRINT_DEBUG("\r\n");
125
126         /* Probably don't have to do this, but it can't hurt. */
127         smbus_reset();
128
129         return val;
130 }
131
132 /**
133  * Enable the SMBus on VT8237R-based systems.
134  */
135 void enable_smbus(void)
136 {
137         device_t dev;
138
139         /* Power management controller */
140         dev = pci_locate_device(PCI_ID(PCI_VENDOR_ID_VIA,
141                                        PCI_DEVICE_ID_VIA_VT8237R_LPC), 0);
142         if (dev == PCI_DEV_INVALID) {
143                 /* Power management controller */
144                 dev = pci_locate_device(PCI_ID(PCI_VENDOR_ID_VIA,
145                                         PCI_DEVICE_ID_VIA_VT8237S_LPC), 0);
146                 if (dev == PCI_DEV_INVALID)
147                         die("Power management controller not found\r\n");
148         }
149
150         /*
151          * 7 = SMBus Clock from RTC 32.768KHz
152          * 5 = Internal PLL reset from susp
153          */
154         pci_write_config8(dev, VT8237R_POWER_WELL, 0xa0);
155
156         /* Enable SMBus. */
157         pci_write_config16(dev, VT8237R_SMBUS_IO_BASE_REG,
158                            VT8237R_SMBUS_IO_BASE | 0x1);
159
160         /* SMBus Host Configuration, enable. */
161         pci_write_config8(dev, VT8237R_SMBUS_HOST_CONF, 0x01);
162
163         /* Make it work for I/O. */
164         pci_write_config16(dev, PCI_COMMAND, PCI_COMMAND_IO);
165
166         smbus_reset();
167
168         /* Reset the internal pointer. */
169         inb(SMBHSTCTL);
170 }
171
172 /**
173  * A fixup for some systems that need time for the SMBus to "warm up". This is 
174  * needed on some VT823x based systems, where the SMBus spurts out bad data for 
175  * a short time after power on. This has been seen on the VIA Epia series and 
176  * Jetway J7F2-series. It reads the ID byte from SMBus, looking for 
177  * known-good data from a slot/address. Exits on either good data or a timeout.
178  *
179  * TODO: This should probably go into some global file, but one would need to
180  *       be created just for it. If some other chip needs/wants it, we can
181  *       worry about it then.
182  *
183  * @param ctrl The memory controller and SMBus addresses.
184  */
185 void smbus_fixup(const struct mem_controller *ctrl)
186 {
187         int i, ram_slots, current_slot = 0;
188         u8 result = 0;
189
190         ram_slots = ARRAY_SIZE(ctrl->channel0);
191         if (!ram_slots) {
192                 print_err("smbus_fixup() thinks there are no RAM slots!\r\n");
193                 return;
194         }
195
196         PRINT_DEBUG("Waiting for SMBus to warm up");
197
198         /*
199          * Bad SPD data should be either 0 or 0xff, but YMMV. So we look for
200          * the ID bytes of SDRAM, DDR, DDR2, and DDR3 (and anything in between).
201          * VT8237R has only been seen on DDR and DDR2 based systems, so far.
202          */
203         for (i = 0; (i < SMBUS_TIMEOUT && ((result < SPD_MEMORY_TYPE_SDRAM) ||
204            (result > SPD_MEMORY_TYPE_SDRAM_DDR3))); i++) {
205
206                 if (current_slot > ram_slots)
207                         current_slot = 0;
208
209                 result = smbus_read_byte(ctrl->channel0[current_slot],
210                                          SPD_MEMORY_TYPE);
211                 current_slot++;
212                 PRINT_DEBUG(".");
213         }
214
215         if (i >= SMBUS_TIMEOUT)
216                 print_err("SMBus timed out while warming up\r\n");
217         else
218                 PRINT_DEBUG("Done\r\n");
219 }
220
221 /* FIXME: Better separate the NB and SB, will be done once it works. */
222
223 void vt8237_sb_enable_fid_vid(void)
224 {
225         device_t dev, devctl;
226
227         /* Power management controller */
228         dev = pci_locate_device(PCI_ID(PCI_VENDOR_ID_VIA,
229                                        PCI_DEVICE_ID_VIA_VT8237R_LPC), 0);
230         if (dev == PCI_DEV_INVALID) {
231                 /* Power management controller */
232                 dev = pci_locate_device(PCI_ID(PCI_VENDOR_ID_VIA,
233                                         PCI_DEVICE_ID_VIA_VT8237S_LPC), 0);
234                 if (dev == PCI_DEV_INVALID)
235                         return;
236
237                 devctl = pci_locate_device(PCI_ID(PCI_VENDOR_ID_VIA,
238                                            PCI_DEVICE_ID_VIA_VT8237_VLINK), 0);
239
240                 if (devctl == PCI_DEV_INVALID)
241                         return;
242
243                 /* Set ACPI base address to I/O VT8237R_ACPI_IO_BASE. */
244                 pci_write_config16(dev, 0x88, VT8237R_ACPI_IO_BASE | 0x1);
245
246                 /* Enable ACPI accessm RTC signal gated with PSON. */
247                 pci_write_config8(dev, 0x81, 0x84);
248
249                 /*
250                  * Allow SLP# signal to assert LDTSTOP_L.
251                  * Will work for C3 and for FID/VID change.
252                  */
253
254                 outb(0xff, VT8237R_ACPI_IO_BASE + 0x50);
255
256                 /* Reduce further the STPCLK/LDTSTP signal to 5us. */
257                 pci_write_config8(dev, 0xec, 0x4);
258
259                 /* So the chip knows we are on AMD. */
260                 pci_write_config8(devctl, 0x7c, 0x7f);
261
262                 return;
263         }
264
265         /* Set ACPI base address to I/O VT8237R_ACPI_IO_BASE. */
266         pci_write_config16(dev, 0x88, VT8237R_ACPI_IO_BASE | 0x1);
267
268         /* Enable ACPI accessm RTC signal gated with PSON. */
269         pci_write_config8(dev, 0x81, 0x84);
270
271         /*
272          * Allow SLP# signal to assert LDTSTOP_L.
273          * Will work for C3 and for FID/VID change.
274          */
275         outb(0x1, VT8237R_ACPI_IO_BASE + 0x11);
276 }
277
278 void enable_rom_decode(void)
279 {
280         device_t dev;
281
282         /* Power management controller */
283         dev = pci_locate_device(PCI_ID(PCI_VENDOR_ID_VIA,
284                                        PCI_DEVICE_ID_VIA_VT8237R_LPC), 0);
285         if (dev == PCI_DEV_INVALID) {
286                 /* Power management controller */
287                 dev = pci_locate_device(PCI_ID(PCI_VENDOR_ID_VIA,
288                                         PCI_DEVICE_ID_VIA_VT8237S_LPC), 0);
289                 if (dev == PCI_DEV_INVALID)
290                         return;
291         }
292
293         /* ROM decode last 1MB FFC00000 - FFFFFFFF. */
294         pci_write_config8(dev, 0x41, 0x7f);
295 }
296
297 #if defined(__GNUC__)
298 void vt8237_early_spi_init(void)
299 {
300         device_t dev;
301         volatile u16 *spireg;
302         u32 tmp;
303
304         /* Bus Control and Power Management */
305         dev = pci_locate_device(PCI_ID(PCI_VENDOR_ID_VIA,
306                                        PCI_DEVICE_ID_VIA_VT8237S_LPC), 0);
307
308         if (dev == PCI_DEV_INVALID)
309                 die("SB not found\r\n");
310
311         /* Put SPI base 20 d0 fe. */
312         tmp = pci_read_config32(dev, 0xbc);
313         pci_write_config32(dev, 0xbc,
314                            (VT8237S_SPI_MEM_BASE >> 8) | (tmp & 0xFF000000));
315
316         /* Set SPI clock to 33MHz. */
317         spireg = (u16 *) (VT8237S_SPI_MEM_BASE + 0x6c);
318         (*spireg) &= 0xff00;
319 }
320 #endif
321
322 /* This #if is special. ROMCC chokes on the (rom == NULL) comparison.
323  * Since the whole function is only called for one target and that target
324  * is compiled with GCC, hide the function from ROMCC and be happy.
325  */
326 #if defined(__GNUC__)
327 /*
328  * Offset 0x58:
329  * 31:20        reserved
330  * 19:16        4 bit position in shadow EEPROM
331  * 15:0         data to write
332  *
333  * Offset 0x5c:
334  * 31:28        reserved
335  * 27           ERDBG - enable read from 0x5c
336  * 26           reserved
337  * 25           SEELD
338  * 24           SEEPR - write 1 when done updating, wait until SEELD is
339  *                      set to 1, sticky
340  *              cleared by reset, if it is 1 writing is disabled
341  * 19:16        4 bit position in shadow EEPROM
342  * 15:0         data from shadow EEPROM
343  *
344  * After PCIRESET SEELD and SEEPR must be 1 and 1.
345  */
346
347 /* 1 = needs PCI reset, 0 don't reset, network initialized. */
348
349 /* FIXME: Maybe close the debug register after use? */
350
351 #define LAN_TIMEOUT 0x7FFFFFFF
352
353 int vt8237_early_network_init(struct vt8237_network_rom *rom)
354 {
355         struct vt8237_network_rom n;
356         int i, loops;
357         device_t dev;
358         u32 tmp;
359         u8 status;
360         u16 *rom_write;
361         unsigned int checksum;
362
363         /* Network adapter */
364         dev = pci_locate_device(PCI_ID(PCI_VENDOR_ID_VIA,
365                                        PCI_DEVICE_ID_VIA_8233_7), 0);
366         if (dev == PCI_DEV_INVALID) {
367                 print_err("Network is disabled, please enable\n");
368                 return 0;
369         }
370
371         tmp = pci_read_config32(dev, 0x5c);
372         tmp |= 0x08000000;      /* Enable ERDBG. */
373         pci_write_config32(dev, 0x5c, tmp);
374
375         status = ((pci_read_config32(dev, 0x5c) >> 24) & 0x3);
376
377         /* Network controller OK, EEPROM loaded. */
378         if (status == 3)
379                 return 0;
380
381         if (rom == NULL) {
382                 print_err("No config data specified, using default MAC!\n");
383                 n.mac_address[0] = 0x0;
384                 n.mac_address[1] = 0x0;
385                 n.mac_address[2] = 0xde;
386                 n.mac_address[3] = 0xad;
387                 n.mac_address[4] = 0xbe;
388                 n.mac_address[5] = 0xef;
389                 n.phy_addr = 0x1;
390                 n.res1 = 0x0;
391                 n.sub_sid = 0x102;
392                 n.sub_vid = 0x1106;
393                 n.pid = 0x3065;
394                 n.vid = 0x1106;
395                 n.pmcc = 0x1f;
396                 n.data_sel = 0x10;
397                 n.pmu_data_reg = 0x0;
398                 n.aux_curr = 0x0;
399                 n.reserved = 0x0;
400                 n.min_gnt = 0x3;
401                 n.max_lat = 0x8;
402                 n.bcr0 = 0x9;
403                 n.bcr1 = 0xe;
404                 n.cfg_a = 0x3;
405                 n.cfg_b = 0x0;
406                 n.cfg_c = 0x40;
407                 n.cfg_d = 0x82;
408                 n.checksum = 0x0;
409                 rom = &n;
410         }
411
412         rom_write = (u16 *) rom;
413         checksum = 0;
414         /* Write all data except checksum and second to last byte. */
415         tmp &= 0xff000000;      /* Leave reserved bits in. */
416         for (i = 0; i < 15; i++) {
417                 pci_write_config32(dev, 0x58, tmp | (i << 16) | rom_write[i]);
418                 /* Lame code FIXME */
419                 checksum += rom_write[i] & 0xff;
420                 /* checksum %= 256; */
421                 checksum += (rom_write[i] >> 8) & 0xff;
422                 /* checksum %= 256; */
423         }
424
425         checksum += (rom_write[15] & 0xff);
426         checksum = ~(checksum & 0xff);
427         tmp |= (((checksum & 0xff) << 8) | rom_write[15]);
428
429         /* Write last byte and checksum. */
430         pci_write_config32(dev, 0x58, (15 << 16) | tmp);
431
432         tmp = pci_read_config32(dev, 0x5c);
433         pci_write_config32(dev, 0x5c, tmp | 0x01000000); /* Toggle SEEPR. */
434
435         /* Yes, this is a mess, but it's the easiest way to do it. */
436         while ((((pci_read_config32(dev, 0x5c) >> 25) & 1) == 0)
437                && (loops < LAN_TIMEOUT)) {
438                 ++loops;
439         }
440
441         if (loops >= LAN_TIMEOUT) {
442                 print_err("Timeout - LAN controller didn't accept config\n");
443                 return 0;
444         }
445
446         /* We are done, config will be used after PCIRST#. */
447         return 1;
448 }
449 #endif