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