fix quartet and S4880 spd initialization.
[coreboot.git] / src / mainboard / amd / quartet / auto.c
1 #define ASSEMBLY 1
2 // #define MAXIMUM_CONSOLE_LOGLEVEL 9
3 // #define DEFAULT_CONSOLE_LOGLEVEL 9
4
5 #include <stdint.h>
6 #include <device/pci_def.h>
7 #include <cpu/p6/apic.h>
8 #include <arch/io.h>
9 #include <device/pnp.h>
10 #include <arch/romcc_io.h>
11 #include "pc80/serial.c"
12 #include "arch/i386/lib/console.c"
13 #include "ram/ramtest.c"
14 #include "northbridge/amd/amdk8/early_ht.c"
15 #include "southbridge/amd/amd8111/amd8111_early_smbus.c"
16 #include "northbridge/amd/amdk8/raminit.h"
17 #include "cpu/k8/apic_timer.c"
18 #include "lib/delay.c"
19 #include "cpu/p6/boot_cpu.c"
20 #include "northbridge/amd/amdk8/reset_test.c"
21 #include "debug.c"
22 #include "northbridge/amd/amdk8/cpu_rev.c"
23
24 #define SIO_BASE 0x2e
25
26 static void memreset_setup(void)
27 {
28         if (is_cpu_pre_c0()) {
29                 /* Set the memreset low */
30                 outb((0 << 7)|(0 << 6)|(0<<5)|(0<<4)|(1<<2)|(0<<0), SMBUS_IO_BASE + 0xc0 + 28);
31                 /* Ensure the BIOS has control of the memory lines */
32                 outb((0 << 7)|(0 << 6)|(0<<5)|(0<<4)|(1<<2)|(0<<0), SMBUS_IO_BASE + 0xc0 + 29);
33         }
34         else {
35                 /* Ensure the CPU has controll of the memory lines */
36                 outb((0 << 7)|(0 << 6)|(0<<5)|(0<<4)|(1<<2)|(1<<0), SMBUS_IO_BASE + 0xc0 + 29);
37         }
38 }
39
40 static void memreset(int controllers, const struct mem_controller *ctrl)
41 {
42         if (is_cpu_pre_c0()) {
43                 udelay(800);
44                 /* Set memreset_high */
45                 outb((0<<7)|(0<<6)|(0<<5)|(0<<4)|(1<<2)|(1<<0), SMBUS_IO_BASE + 0xc0 + 28);
46                 udelay(90);
47         }
48 }
49
50 /*
51  * generate_row is specific to board implementation
52  *
53  */
54
55 static unsigned int generate_row(uint8_t node, uint8_t row, uint8_t maxnodes)
56 {
57         /* Routing Table Node i 
58          *
59          * F0: 0x40, 0x44, 0x48, 0x4c, 0x50, 0x54, 0x58, 0x5c 
60          *  i:    0,    1,    2,    3,    4,    5,    6,    7
61          *
62          * [ 0: 3] Request Route
63          *     [0] Route to this node
64          *     [1] Route to Link 0
65          *     [2] Route to Link 1
66          *     [3] Route to Link 2
67          * [11: 8] Response Route
68          *     [0] Route to this node
69          *     [1] Route to Link 0
70          *     [2] Route to Link 1
71          *     [3] Route to Link 2
72          * [19:16] Broadcast route
73          *     [0] Route to this node
74          *     [1] Route to Link 0
75          *     [2] Route to Link 1
76          *     [3] Route to Link 2
77          */
78
79         uint32_t ret=0x00010101; /* default row entry */
80
81         static const unsigned int rows_2p[2][2] = {
82                 { 0x00030101, 0x00010202 },
83                 { 0x00010202, 0x00030101 }
84         };
85
86         static const unsigned int rows_4p[4][4] = {
87                 { 0x00070101, 0x00010202, 0x00030404, 0x00010204 },
88                 { 0x00010202, 0x000b0101, 0x00010208, 0x00030808 },
89                 { 0x00030808, 0x00010208, 0x000b0101, 0x00010202 },
90                 { 0x00010204, 0x00030404, 0x00010202, 0x00070101 }
91         };
92
93         if (!(node>=maxnodes || row>=maxnodes)) {
94                 if (maxnodes==2)
95                         ret=rows_2p[node][row];
96                 if (maxnodes==4)
97                         ret=rows_4p[node][row];
98         }
99
100         return ret;
101 }
102
103
104 #if ( FAKE_SPDROM != 1 )
105 static inline void activate_spd_rom(const struct mem_controller *ctrl)
106 {
107 #define SMBUS_HUB 0x18
108         unsigned device=(ctrl->channel0[0])>>8;
109         smbus_write_byte(SMBUS_HUB , 0x01, device);
110         smbus_write_byte(SMBUS_HUB , 0x03, 0);
111 }
112
113 static inline int spd_read_byte(unsigned device, unsigned address)
114 {
115         return smbus_read_byte(device & 0xff, address);
116 }
117 #else
118 #include "fakespd.c"
119 #endif
120
121 /* no specific code here. this should go away completely */
122 static void coherent_ht_mainboard(unsigned cpus)
123 {
124 }
125
126 #include "northbridge/amd/amdk8/raminit.c"
127
128 #define CONNECTION_0_1 UP
129 #define CONNECTION_0_2 ACROSS
130 #define CONNECTION_1_3 DOWN
131
132 #include "northbridge/amd/amdk8/coherent_ht.c"
133 #include "sdram/generic_sdram.c"
134
135 #include "resourcemap.c" /* quartet does not want the default */
136
137 static void enable_lapic(void)
138 {
139
140         msr_t msr;
141         msr = rdmsr(0x1b);
142         msr.hi &= 0xffffff00;
143         msr.lo &= 0x000007ff;
144         msr.lo |= APIC_DEFAULT_BASE | (1 << 11);
145         wrmsr(0x1b, msr);
146 }
147
148 static void stop_this_cpu(void)
149 {
150         unsigned apicid;
151         apicid = apic_read(APIC_ID) >> 24;
152
153         /* Send an APIC INIT to myself */
154         apic_write(APIC_ICR2, SET_APIC_DEST_FIELD(apicid));
155         apic_write(APIC_ICR, APIC_INT_LEVELTRIG | APIC_INT_ASSERT | APIC_DM_INIT);
156         /* Wait for the ipi send to finish */
157         apic_wait_icr_idle();
158
159         /* Deassert the APIC INIT */
160         apic_write(APIC_ICR2, SET_APIC_DEST_FIELD(apicid));
161         apic_write(APIC_ICR,  APIC_INT_LEVELTRIG | APIC_DM_INIT);
162         /* Wait for the ipi send to finish */
163         apic_wait_icr_idle();
164
165         /* If I haven't halted spin forever */
166         for(;;) {
167                 hlt();
168         }
169 }
170
171 #define PC87360_FDC  0x00
172 #define PC87360_PP   0x01
173 #define PC87360_SP2  0x02
174 #define PC87360_SP1  0x03
175 #define PC87360_SWC  0x04
176 #define PC87360_KBCM 0x05
177 #define PC87360_KBCK 0x06
178 #define PC87360_GPIO 0x07
179 #define PC87360_ACB  0x08
180 #define PC87360_FSCM 0x09
181 #define PC87360_WDT  0x0A
182
183 static void pc87360_enable_serial(void)
184 {
185         pnp_set_logical_device(SIO_BASE, PC87360_SP1);
186         pnp_set_enable(SIO_BASE, 1);
187         pnp_set_iobase0(SIO_BASE, 0x3f8);
188 }
189
190 #define RC0 ((1<<1)<<8)
191 #define RC1 ((1<<2)<<8)
192 #define RC2 ((1<<3)<<8)
193 #define RC3 ((1<<4)<<8)
194
195 #define DIMM0 0x50
196 #define DIMM1 0x51
197 #define DIMM2 0x52
198 #define DIMM3 0x53
199
200 static void main(void)
201 {
202         static const struct mem_controller cpu[] = {
203                 {
204                         .node_id = 0,
205                         .f0 = PCI_DEV(0, 0x18, 0),
206                         .f1 = PCI_DEV(0, 0x18, 1),
207                         .f2 = PCI_DEV(0, 0x18, 2),
208                         .f3 = PCI_DEV(0, 0x18, 3),
209                         .channel0 = { RC0|DIMM0, RC0|DIMM2, 0, 0 },
210                         .channel1 = { RC0|DIMM1, RC0|DIMM3, 0, 0 },
211                 },
212                 {
213                         .node_id = 1,
214                         .f0 = PCI_DEV(0, 0x19, 0),
215                         .f1 = PCI_DEV(0, 0x19, 1),
216                         .f2 = PCI_DEV(0, 0x19, 2),
217                         .f3 = PCI_DEV(0, 0x19, 3),
218                         .channel0 = { RC1|DIMM0, RC1|DIMM2, 0, 0 },
219                         .channel1 = { RC1|DIMM1, RC1|DIMM3, 0, 0 },
220                 },
221                 {
222                         .node_id = 2,
223                         .f0 = PCI_DEV(0, 0x1a, 0),
224                         .f1 = PCI_DEV(0, 0x1a, 1),
225                         .f2 = PCI_DEV(0, 0x1a, 2),
226                         .f3 = PCI_DEV(0, 0x1a, 3),
227                         .channel0 = { RC2|DIMM0, RC2|DIMM2, 0, 0 },
228                         .channel1 = { RC2|DIMM1, RC2|DIMM3, 0, 0 },
229                 },
230                 {
231                         .node_id = 3,
232                         .f0 = PCI_DEV(0, 0x1b, 0),
233                         .f1 = PCI_DEV(0, 0x1b, 1),
234                         .f2 = PCI_DEV(0, 0x1b, 2),
235                         .f3 = PCI_DEV(0, 0x1b, 3),
236                         .channel0 = { RC3|DIMM0, RC3|DIMM2, 0, 0 },
237                         .channel1 = { RC3|DIMM1, RC3|DIMM3, 0, 0 },
238                 }
239         };
240         if (cpu_init_detected()) {
241                 asm("jmp __cpu_reset");
242         }
243         enable_lapic();
244         init_timer();
245         if (!boot_cpu()) {
246 //              notify_bsp_ap_is_stopped();
247                 stop_this_cpu();
248         }
249         pc87360_enable_serial();
250         uart_init();
251         console_init();
252         setup_quartet_resource_map();
253         setup_coherent_ht_domain();
254         enumerate_ht_chain(0);
255         distinguish_cpu_resets(0);
256         
257 #if 0
258         print_pci_devices();
259 #endif
260         enable_smbus();
261 #if 0
262         dump_spd_registers(&cpu[0]);
263 #endif
264         memreset_setup();
265         sdram_initialize(sizeof(cpu)/sizeof(cpu[0]), cpu);
266
267 #if 0
268         dump_pci_devices();
269 #endif
270 #if 0
271         dump_pci_device(PCI_DEV(0, 0x18, 2));
272 #endif
273
274         /* Check all of memory */
275 #if 0
276         msr_t msr;
277         msr = rdmsr(TOP_MEM);
278         print_debug("TOP_MEM: ");
279         print_debug_hex32(msr.hi);
280         print_debug_hex32(msr.lo);
281         print_debug("\r\n");
282 #endif
283 #if 0
284         ram_check(0x00000000, msr.lo);
285 #endif
286 #if 0
287         static const struct {
288                 unsigned long lo, hi;
289         } check_addrs[] = {
290                 /* Check 16MB of memory @ 0*/
291                 { 0x00000000, 0x01000000 },
292 #if 0
293                 /* Check 16MB of memory @ 2GB */
294                 { 0x80000000, 0x81000000 },
295 #endif
296         };
297         int i;
298         for(i = 0; i < sizeof(check_addrs)/sizeof(check_addrs[0]); i++) {
299                 ram_check(check_addrs[i].lo, check_addrs[i].hi);
300         }
301 #endif
302 }