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