This patch drops arch/i386/lib/console.c and arch/i386/lib/console_print.c and
[coreboot.git] / src / northbridge / amd / amdk8 / util.c
1 /*
2  * This file is part of the coreboot project.
3  *
4  * Copyright (C) 2008 Vincent Legoll <vincent.legoll@gmail.com>
5  * Copyright (C) 2008 Ronald G. Minnich <rminnich@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  * K8 northbridge utilities (dump routing registers).
23  * Designed to be called at any time.
24  * It can be called before RAM is set up by including this file.
25  * It can be called after RAM is set up by including amdk8.h and enabling the
26  * compilation of this file in src/northbridge/amd/amdk8/Makefile.inc.
27  */
28 #ifndef __PRE_RAM__
29 #include <console/console.h>
30 #include <string.h>
31 #include <device/pci.h>
32 #include <cpu/amd/amdk8_sysconf.h>
33 #include <device/pci_ops.h>
34 #include "amdk8.h"
35 #endif
36
37 /* Function 1 */
38 /* the DRAM, MMIO,and PCIIO routing are 64-bit registers, hence the ending at
39  * 0x78, 0xb8, and 0xd8
40  */
41 #define DRAM_ROUTE_START        0x40
42 #define DRAM_ROUTE_END          0x78
43 #define MMIO_ROUTE_START        0x80
44 #define MMIO_ROUTE_END          0xb8
45 #define PCIIO_ROUTE_START       0xc0
46 #define PCIIO_ROUTE_END         0xd8
47 #define CONFIG_ROUTE_START      0xe0
48 #define CONFIG_ROUTE_END        0xec
49
50 #define PCI_IO_BASE0            0xc0
51 #define PCI_IO_BASE1            0xc8
52 #define PCI_IO_BASE2            0xd0
53 #define PCI_IO_BASE3            0xd8
54 #define PCI_IO_BASE_VGA_EN      (1 << 4)
55 #define PCI_IO_BASE_NO_ISA      (1 << 5)
56
57 #define BITS(r, shift, mask) (((r>>shift)&mask))
58
59 /**
60  * Return "R" if the register has read-enable bit set.
61  */
62 static const char *re(u32 i)
63 {
64         return ((i & 1) ? "R" : "");
65 }
66
67 /**
68  * Return "W" if the register has write-enable bit set.
69  */
70 static const char *we(u32 i)
71 {
72         return ((i & 1) ? "W" : "");
73 }
74
75 /**
76  * Return a string containing the interleave settings.
77  */
78 static const char *ileave(u32 base)
79 {
80         switch ((base >> 8) & 7) {
81         case 0:
82                 return "No interleave";
83         case 1:
84                 return "2 nodes";
85         case 3:
86                 return "4 nodes";
87         case 7:
88                 return "8 nodes";
89         default:
90                 return "Reserved";
91         }
92 }
93
94 /**
95  * Return the node number.
96  * For one case (config registers) these are not the right bit fields.
97  */
98 static int r_node(u32 reg)
99 {
100         return BITS(reg, 0, 0x7);
101 }
102
103 /**
104  * Return the link number.
105  * For one case (config registers) these are not the right bit fields.
106  */
107 static int r_link(u32 reg)
108 {
109         return BITS(reg, 4, 0x3);
110 }
111
112 /**
113  * Print the DRAM routing info for one base/limit pair.
114  *
115  * Show base, limit, dest node, dest link on that node, read and write
116  * enable, and interleave information.
117  *
118  * @param level Printing level
119  * @param which Register number
120  * @param base Base register
121  * @param lim Limit register
122  */
123 static void showdram(int level, u8 which, u32 base, u32 lim)
124 {
125         printk(level, "DRAM(%02x)%010llx-%010llx, ->(%d), %s, %s, %s, %d\n",
126                which, (((u64) base & 0xffff0000) << 8),
127                (((u64) lim & 0xffff0000) << 8) + 0xffffff,
128                r_node(lim), re(base), we(base), ileave(base), (lim >> 8) & 3);
129 }
130
131 /**
132  * Print the config routing info for a config register.
133  *
134  * Show base, limit, dest node, dest link on that node, read and write
135  * enable, and device number compare enable
136  *
137  * @param level Printing level
138  * @param which Register number
139  * @param reg Config register
140  */
141 static void showconfig(int level, u8 which, u32 reg)
142 {
143         /* Don't use r_node() and r_link() here. */
144         printk(level, "CONFIG(%02x)%02x-%02x ->(%d,%d),%s %s (%s numbers)\n",
145                which, BITS(reg, 16, 0xff), BITS(reg, 24, 0xff),
146                BITS(reg, 4, 0x7), BITS(reg, 8, 0x3),
147                re(reg), we(reg),
148                BITS(reg, 2, 0x1)?"dev":"bus");
149 }
150
151 /**
152  * Print the PCIIO routing info for one base/limit pair.
153  *
154  * Show base, limit, dest node, dest link on that node, read and write
155  * enable, and VGA and ISA Enable.
156  *
157  * @param level Printing level
158  * @param which Register number
159  * @param base Base register
160  * @param lim Limit register
161  */
162 static void showpciio(int level, u8 which, u32 base, u32 lim)
163 {
164         printk(level, "PCIIO(%02x)%07x-%07x, ->(%d,%d), %s, %s,VGA %d ISA %d\n",
165                which, BITS(base, 12, 0x3fff) << 12,
166                (BITS(lim, 12, 0x3fff) << 12) + 0xfff, r_node(lim), r_link(lim),
167                re(base), we(base), BITS(base, 4, 0x1), BITS(base, 5, 0x1));
168 }
169
170 /**
171  * Print the MMIO routing info for one base/limit pair.
172  *
173  * Show base, limit, dest node, dest link on that node, read and write
174  * enable, and CPU Disable, Lock, and Non-posted.
175  *
176  * @param level Printing level
177  * @param which Register number
178  * @param base Base register
179  * @param lim Limit register
180  */
181 static void showmmio(int level, u8 which, u32 base, u32 lim)
182 {
183         printk(level, "MMIO(%02x)%010llx-%010llx, ->(%d,%d), %s, %s, "
184                "CPU disable %d, Lock %d, Non posted %d\n",
185                which, ((u64) BITS(base, 0, 0xffffff00)) << 8,
186                (((u64) BITS(lim, 0, 0xffffff00)) << 8) + 0xffff, r_node(lim),
187                r_link(lim), re(base), we(base), BITS(base, 4, 0x1),
188                BITS(base, 7, 0x1), BITS(lim, 7, 0x1));
189 }
190
191 /**
192  * Show all DRAM routing registers. This function is callable at any time.
193  *
194  * @param level The debug level.
195  * @param dev A 32-bit number in the standard bus/dev/fn format which is used
196  *            raw config space.
197  */
198 static void showalldram(int level, device_t dev)
199 {
200         u8 reg;
201         for (reg = DRAM_ROUTE_START; reg <= DRAM_ROUTE_END; reg += 8) {
202                 u32 base = pci_read_config32(dev, reg);
203                 u32 lim = pci_read_config32(dev, reg + 4);
204                 if (base || lim!=(reg-DRAM_ROUTE_START)/8)
205                         showdram(level, reg, base, lim);
206         }
207 }
208
209 /**
210  * Show all MMIO routing registers. This function is callable at any time.
211  *
212  * @param level The debug level.
213  * @param dev A 32-bit number in the standard bus/dev/fn format which is used
214  *            raw config space.
215  */
216 static void showallmmio(int level, device_t dev)
217 {
218         u8 reg;
219         for (reg = MMIO_ROUTE_START; reg <= MMIO_ROUTE_END; reg += 8) {
220                 u32 base = pci_read_config32(dev, reg);
221                 u32 lim = pci_read_config32(dev, reg + 4);
222                 if (base || lim)
223                         showmmio(level, reg, base, lim);
224         }
225 }
226
227 /**
228  * Show all PCIIO routing registers. This function is callable at any time.
229  *
230  * @param level The debug level.
231  * @param dev A 32-bit number in the standard bus/dev/fn format which is used
232  *            raw config space.
233  */
234 static void showallpciio(int level, device_t dev)
235 {
236         u8 reg;
237         for (reg = PCIIO_ROUTE_START; reg <= PCIIO_ROUTE_END; reg += 8) {
238                 u32 base = pci_read_config32(dev, reg);
239                 u32 lim = pci_read_config32(dev, reg + 4);
240                 if (base || lim)
241                         showpciio(level, reg, base, lim);
242         }
243 }
244
245 /**
246  * Show all config routing registers. This function is callable at any time.
247  *
248  * @param level The debug level.
249  * @param dev A 32-bit number in the standard bus/dev/fn format which is used
250  *            raw config space.
251  */
252 static void showallconfig(int level, device_t dev)
253 {
254         u8 reg;
255         for (reg = CONFIG_ROUTE_START; reg <= CONFIG_ROUTE_END; reg += 4) {
256                 u32 val = pci_read_config32(dev, reg);
257                 if (val)
258                         showconfig(level, reg, val);
259         }
260 }
261
262 /**
263  * Show all routing registers. This function is callable at any time.
264  *
265  * @param level The debug level.
266  * @param dev A 32-bit number in the standard bus/dev/fn format which is used
267  *            raw config space.
268  */
269 void showallroutes(int level, device_t dev)
270 {
271         showalldram(level, dev);
272         showallmmio(level, dev);
273         showallpciio(level, dev);
274         showallconfig(level, dev);
275 }