VIA southbridge K8T890: Apply un-written naming rules
[coreboot.git] / src / southbridge / via / k8t890 / early_car.c
1 /*
2  * This file is part of the coreboot project.
3  *
4  * Copyright (C) 2007 Rudolf Marek <r.marek@assembler.cz>
5  * Copyright (C) 2011 Alexandru Gagniuc <mr.nuke.me@gmail.com>
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; version 2 of the License.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
19  */
20
21 /*
22  * Seems the link and width of HT link needs to be setup too, you need to
23  * generate PCI reset or LDTSTOP to apply.
24  */
25
26 #include <stdlib.h>
27 #include <cbmem.h>
28 #include <arch/io.h>
29 #include "k8x8xx.h"
30
31 /* The 256 bytes of NVRAM for S3 storage, 256B aligned */
32 #define K8T890_NVRAM_IO_BASE    0xf00
33 #define K8T890_MULTIPLE_FN_EN   0x4f
34
35 /* AMD K8 LDT0, LDT1, LDT2 Link Control Registers */
36 static u8 ldtreg[3] = {0x86, 0xa6, 0xc6};
37
38 #if CONFIG_SOUTHBRIDGE_VIA_SUBTYPE_K8T800_OLD
39 #define K8X8XX_HT_CFG_BASE 0xc0
40 #else
41 #define K8X8XX_HT_CFG_BASE 0x60
42 #endif
43
44 /* This functions sets KT890 link frequency and width to same values as
45  * it has been setup on K8 side, by AMD NB init.
46  * This will not work for K8T800_OLD, which has a slightly different
47  * register arrangement (device 3188)
48  */
49
50 u8 k8t890_early_setup_ht(void)
51 {
52         u8 awidth, afreq, cldtfreq;
53         u8 cldtwidth_in, cldtwidth_out, vldtwidth_in, vldtwidth_out, ldtnr, width;
54         u16 vldtcaps;
55
56 #if !CONFIG_SOUTHBRIDGE_VIA_SUBTYPE_K8T800_OLD
57         u8 reg;
58
59         /* hack, enable NVRAM in chipset */
60         pci_write_config8(PCI_DEV(0, 0x0, 0), K8T890_MULTIPLE_FN_EN, 0x01);
61
62         /*
63          * NVRAM I/O base at K8T890_NVRAM_IO_BASE
64          */
65
66         pci_write_config8(PCI_DEV(0, 0x0, 2), 0xa2, (K8T890_NVRAM_IO_BASE >> 8));
67         reg = pci_read_config8(PCI_DEV(0, 0x0, 2), 0xa1);
68         reg |= 0x1;
69         pci_write_config8(PCI_DEV(0, 0x0, 2), 0xa1, reg);
70 #endif
71
72         /* check if connected non coherent, initcomplete (find the SB on K8 side) */
73         ldtnr = 0;
74         if (0x7 == pci_read_config8(PCI_DEV(0, 0x18, 0), 0x98)) {
75                 ldtnr = 0;
76         } else if (0x7 == pci_read_config8(PCI_DEV(0, 0x18, 0), 0xb8)) {
77                 ldtnr = 1;
78         } else if (0x7 == pci_read_config8(PCI_DEV(0, 0x18, 0), 0xd8)) {
79                 ldtnr = 2;
80         }
81
82 #if CONFIG_SOUTHBRIDGE_VIA_SUBTYPE_K8M800
83         print_debug("K8M800 found at LDT ");
84 #elif CONFIG_SOUTHBRIDGE_VIA_SUBTYPE_K8T800
85         print_debug("K8T800 found at LDT ");
86 #elif CONFIG_SOUTHBRIDGE_VIA_SUBTYPE_K8T800_OLD
87         print_debug("K8T800_OLD found at LDT ");
88         pci_write_config8(PCI_DEV(0, 0x0, 0), 0x64, 0x00);
89         pci_write_config8(PCI_DEV(0, 0x0, 0), 0xdd, 0x50);
90 #elif CONFIG_SOUTHBRIDGE_VIA_SUBTYPE_K8T800PRO
91         print_debug("K8T800 Pro found at LDT ");
92 #elif CONFIG_SOUTHBRIDGE_VIA_SUBTYPE_K8M890
93         print_debug("K8M890 found at LDT ");
94         /* K8M890 fix HT delay */
95         pci_write_config8(PCI_DEV(0, 0x0, 2), 0xab, 0x22);
96 #elif CONFIG_SOUTHBRIDGE_VIA_SUBTYPE_K8T890
97         print_debug("K8T890 found at LDT ");
98 #endif
99         print_debug_hex8(ldtnr);
100
101         /* get the maximum widths for both sides */
102         cldtwidth_in = pci_read_config8(PCI_DEV(0, 0x18, 0), ldtreg[ldtnr]) & 0x7;
103         cldtwidth_out = (pci_read_config8(PCI_DEV(0, 0x18, 0), ldtreg[ldtnr]) >> 4) & 0x7;
104         vldtwidth_in = pci_read_config8(PCI_DEV(0, 0x0, 0), K8X8XX_HT_CFG_BASE + 0x6) & 0x7;
105         vldtwidth_out = (pci_read_config8(PCI_DEV(0, 0x0, 0), K8X8XX_HT_CFG_BASE + 0x6) >> 4) & 0x7;
106
107         width = MIN(MIN(MIN(cldtwidth_out, cldtwidth_in), vldtwidth_out), vldtwidth_in);
108         print_debug(" Agreed on width: ");
109         print_debug_hex8(width);
110
111         awidth = pci_read_config8(PCI_DEV(0, 0x0, 0), K8X8XX_HT_CFG_BASE + 0x7);
112
113         /* Update the desired HT LNK to match AMD NB max from VIA NB is 0x1 */
114         width = (width == 0x01) ? 0x11 : 0x00;
115
116         pci_write_config8(PCI_DEV(0, 0x0, 0), K8X8XX_HT_CFG_BASE + 0x7, width);
117
118         /* Get programmed HT freq at base 0x89 */
119         cldtfreq = pci_read_config8(PCI_DEV(0, 0x18, 0), ldtreg[ldtnr] + 3) & 0xf;
120         print_debug(" CPU programmed to HT freq: ");
121         print_debug_hex8(cldtfreq);
122
123         print_debug(" VIA HT caps: ");
124         vldtcaps = pci_read_config16(PCI_DEV(0, 0, 0), K8X8XX_HT_CFG_BASE + 0xe);
125         print_debug_hex16(vldtcaps);
126
127         if (!(vldtcaps & (1 << cldtfreq ))) {
128                 die("Chipset does not support desired HT frequency\n");
129         }
130
131         afreq = pci_read_config8(PCI_DEV(0, 0x0, 0), K8X8XX_HT_CFG_BASE + 0xd);
132         pci_write_config8(PCI_DEV(0, 0x0, 0), K8X8XX_HT_CFG_BASE + 0xd, cldtfreq);
133         print_debug("\n");
134
135         /* no reset needed */
136         if ((width == awidth) && (afreq == cldtfreq)) {
137                 return 0;
138         }
139
140         return 1;
141 }
142
143 static inline int s3_save_nvram_early(u32 dword, int size, int  nvram_pos)
144 {
145
146         printk(BIOS_DEBUG, "Writing %x of size %d to nvram pos: %d\n", dword, size, nvram_pos);
147         switch (size) {
148         case 1:
149                 outb((dword & 0xff), K8T890_NVRAM_IO_BASE+nvram_pos);
150                 nvram_pos +=1;
151                 break;
152         case 2:
153                 outw((dword & 0xffff), K8T890_NVRAM_IO_BASE+nvram_pos);
154                 nvram_pos +=2;
155                 break;
156         default:
157                 outl(dword, K8T890_NVRAM_IO_BASE+nvram_pos);
158                 nvram_pos +=4;
159                 break;
160         }
161         return nvram_pos;
162 }
163
164 static inline int s3_load_nvram_early(int size, u32 *old_dword, int nvram_pos)
165 {
166         switch (size) {
167         case 1:
168                 *old_dword &= ~0xff;
169                 *old_dword |= inb(K8T890_NVRAM_IO_BASE+nvram_pos);
170                 nvram_pos +=1;
171                 break;
172         case 2:
173                 *old_dword &= ~0xffff;
174                 *old_dword |= inw(K8T890_NVRAM_IO_BASE+nvram_pos);
175                 nvram_pos +=2;
176                 break;
177         default:
178                 *old_dword = inl(K8T890_NVRAM_IO_BASE+nvram_pos);
179                 nvram_pos +=4;
180                 break;
181         }
182         printk(BIOS_DEBUG, "Loading %x of size %d to nvram pos:%d\n", * old_dword, size, nvram_pos-size);
183         return nvram_pos;
184 }
185
186 struct cbmem_entry *get_cbmem_toc(void) {
187         return (struct cbmem_entry *) inl(K8T890_NVRAM_IO_BASE+K8T890_NVRAM_CBMEM_TOC);
188 }