MCP55: Cosmetic fixes, switch to u8 et al.
[coreboot.git] / src / southbridge / nvidia / mcp55 / early_setup_car.c
1 /*
2  * This file is part of the coreboot project.
3  *
4  * Copyright (C) 2006 AMD
5  * Written by Yinghai Lu <yinghai.lu@amd.com> for AMD.
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 #ifdef UNUSED_CODE
23 int set_ht_link_buffer_counts_chain(u8 ht_c_num, unsigned vendorid, unsigned val);
24
25 static int set_ht_link_mcp55(u8 ht_c_num)
26 {
27         unsigned vendorid = 0x10de;
28         unsigned val = 0x01610109;
29         /* NVIDIA MCP55 hardcode, hardware can not set it automatically. */
30         return set_ht_link_buffer_counts_chain(ht_c_num, vendorid, val);
31 }
32
33 static void setup_ss_table(unsigned index, unsigned where, unsigned control,
34                            const unsigned int *register_values, int max)
35 {
36         int i;
37         unsigned val;
38
39         val = inl(control);
40         val &= 0xfffffffe;
41         outl(val, control);
42
43         outl(0, index); /* Index */
44         for (i = 0; i < max; i++) {
45                 unsigned long reg;
46                 reg = register_values[i];
47                 outl(reg, where);
48         }
49
50         val = inl(control);
51         val |= 1;
52         outl(val, control);
53 }
54 #endif
55
56 /* SIZE 0x100 */
57 #define ANACTRL_IO_BASE 0x2800
58 #define ANACTRL_REG_POS 0x68
59
60 /* SIZE 0x100 */
61 #define SYSCTRL_IO_BASE 0x2400
62 #define SYSCTRL_REG_POS 0x64
63
64 /* SIZE 0x100 */
65 #define ACPICTRL_IO_BASE        0x2000
66 #define ACPICTRL_REG_POS        0x60
67
68 /*
69  * 16 1 1 1 1 8 :0
70  * 16 0 4 0 0 8 :1
71  * 16 0 4 2 2 4 :2
72  *  4 4 4 4 4 8 :3
73  *  8 8 4 0 0 8 :4
74  *  8 0 4 4 4 8 :5
75 */
76
77 #define MCP55_CHIP_REV 3
78
79 static void mcp55_early_set_port(unsigned mcp55_num, unsigned *busn,
80                                  unsigned *devn, unsigned *io_base)
81 {
82
83         static const unsigned int ctrl_devport_conf[] = {
84                 PCI_ADDR(0, 1, 1, ANACTRL_REG_POS), ~(0x0000ff00), ANACTRL_IO_BASE,
85                 PCI_ADDR(0, 1, 1, SYSCTRL_REG_POS), ~(0x0000ff00), SYSCTRL_IO_BASE,
86                 PCI_ADDR(0, 1, 1, ACPICTRL_REG_POS), ~(0x0000ff00), ACPICTRL_IO_BASE,
87         };
88
89         int j;
90         for (j = 0; j < mcp55_num; j++ ) {
91                 setup_resource_map_offset(ctrl_devport_conf,
92                         ARRAY_SIZE(ctrl_devport_conf),
93                         PCI_DEV(busn[j], devn[j], 0) , io_base[j]);
94         }
95 }
96
97 static void mcp55_early_clear_port(unsigned mcp55_num, unsigned *busn,
98                                    unsigned *devn, unsigned *io_base)
99 {
100         static const unsigned int ctrl_devport_conf_clear[] = {
101                 PCI_ADDR(0, 1, 1, ANACTRL_REG_POS), ~(0x0000ff00), 0,
102                 PCI_ADDR(0, 1, 1, SYSCTRL_REG_POS), ~(0x0000ff00), 0,
103                 PCI_ADDR(0, 1, 1, ACPICTRL_REG_POS), ~(0x0000ff00), 0,
104         };
105
106         int j;
107         for (j = 0; j < mcp55_num; j++ ) {
108                 setup_resource_map_offset(ctrl_devport_conf_clear,
109                         ARRAY_SIZE(ctrl_devport_conf_clear),
110                         PCI_DEV(busn[j], devn[j], 0) , io_base[j]);
111         }
112 }
113
114 static void mcp55_early_pcie_setup(unsigned busnx, unsigned devnx,
115                                    unsigned anactrl_io_base, unsigned pci_e_x)
116 {
117         u32 tgio_ctrl, pll_ctrl, dword;
118         int i;
119         device_t dev;
120
121         dev = PCI_DEV(busnx, devnx + 1, 1);
122
123         dword = pci_read_config32(dev, 0xe4);
124         dword |= 0x3f0; /* Disable it at first. */
125         pci_write_config32(dev, 0xe4, dword);
126
127         for (i = 0; i < 3; i++) {
128                 tgio_ctrl = inl(anactrl_io_base + 0xcc);
129                 tgio_ctrl &= ~(3 << 9);
130                 tgio_ctrl |= (i << 9);
131                 outl(tgio_ctrl, anactrl_io_base + 0xcc);
132                 pll_ctrl = inl(anactrl_io_base + 0x30);
133                 pll_ctrl |= (1 << 31);
134                 outl(pll_ctrl, anactrl_io_base + 0x30);
135                 do {
136                         pll_ctrl = inl(anactrl_io_base + 0x30);
137                 } while (!(pll_ctrl & 1));
138         }
139         tgio_ctrl = inl(anactrl_io_base + 0xcc);
140         tgio_ctrl &= ~((7 << 4) | (1 << 8));
141         tgio_ctrl |= (pci_e_x << 4) | (1 << 8);
142         outl(tgio_ctrl, anactrl_io_base + 0xcc);
143
144         udelay(100); /* Wait 100us. */
145
146         dword = pci_read_config32(dev, 0xe4);
147         dword &= ~(0x3f0); /* Enable. */
148         pci_write_config32(dev, 0xe4, dword);
149
150         mdelay(100); /* Need to wait 100ms. */
151 }
152
153 static void mcp55_early_setup(unsigned mcp55_num, unsigned *busn,
154                               unsigned *devn, unsigned *io_base,
155                               unsigned *pci_e_x)
156 {
157     static const unsigned int ctrl_conf_1[] = {
158         RES_PORT_IO_32, ACPICTRL_IO_BASE + 0x10, 0x0007ffff, 0xff78000,
159         RES_PORT_IO_32, ACPICTRL_IO_BASE + 0xa4, 0xffedffff, 0x0012000,
160         RES_PORT_IO_32, ACPICTRL_IO_BASE + 0xac, 0xfffffdff, 0x0000200,
161         RES_PORT_IO_32, ACPICTRL_IO_BASE + 0xb4, 0xfffffffd, 0x0000002,
162
163         RES_PORT_IO_32, ANACTRL_IO_BASE + 0x24, 0xc0f0f08f, 0x26020230,
164         RES_PORT_IO_32, ANACTRL_IO_BASE + 0x34, 0x00000000, 0x22222222,
165         RES_PORT_IO_32, ANACTRL_IO_BASE + 0x08, 0x7FFFFFFF, 0x00000000,
166         RES_PORT_IO_32, ANACTRL_IO_BASE + 0x2C, 0x7FFFFFFF, 0x80000000,
167         RES_PORT_IO_32, ANACTRL_IO_BASE + 0xCC, 0xFFFFF9FF, 0x00000000,
168         RES_PORT_IO_32, ANACTRL_IO_BASE + 0x30, 0x8FFFFFFF, 0x40000000,
169         RES_PORT_IO_32, ANACTRL_IO_BASE + 0xCC, 0xFFFFF9FF, 0x00000200,
170         RES_PORT_IO_32, ANACTRL_IO_BASE + 0x30, 0x8FFFFFFF, 0x40000000,
171         RES_PORT_IO_32, ANACTRL_IO_BASE + 0xCC, 0xFFFFF9FF, 0x00000400,
172         RES_PORT_IO_32, ANACTRL_IO_BASE + 0x30, 0x8FFFFFFF, 0x40000000,
173         RES_PORT_IO_32, ANACTRL_IO_BASE + 0x74, 0xFFFF0FF5, 0x0000F000,
174         RES_PORT_IO_32, ANACTRL_IO_BASE + 0x78, 0xFF00FF00, 0x00100010,
175         RES_PORT_IO_32, ANACTRL_IO_BASE + 0x7C, 0xFF0FF0FF, 0x00500500,
176         RES_PORT_IO_32, ANACTRL_IO_BASE + 0x80, 0xFFFFFFE7, 0x00000000,
177         RES_PORT_IO_32, ANACTRL_IO_BASE + 0x60, 0xFFCFFFFF, 0x00300000,
178         RES_PORT_IO_32, ANACTRL_IO_BASE + 0x90, 0xFFFF00FF, 0x0000FF00,
179         RES_PORT_IO_32, ANACTRL_IO_BASE + 0x9C, 0xFF00FFFF, 0x00070000,
180
181         RES_PCI_IO, PCI_ADDR(0, 0, 0, 0x40), 0x00000000, 0xCB8410DE,
182         RES_PCI_IO, PCI_ADDR(0, 0, 0, 0x48), 0xFFFFDCED, 0x00002002,
183         RES_PCI_IO, PCI_ADDR(0, 0, 0, 0x78), 0xFFFFFF8E, 0x00000011,
184         RES_PCI_IO, PCI_ADDR(0, 0, 0, 0x80), 0xFFFF0000, 0x00009923,
185         RES_PCI_IO, PCI_ADDR(0, 0, 0, 0x88), 0xFFFFFFFE, 0x00000000,
186         RES_PCI_IO, PCI_ADDR(0, 0, 0, 0x8C), 0xFFFF0000, 0x0000007F,
187         RES_PCI_IO, PCI_ADDR(0, 0, 0, 0xDC), 0xFFFEFFFF, 0x00010000,
188
189         RES_PCI_IO, PCI_ADDR(0, 1, 0, 0x40), 0x00000000, 0xCB8410DE,
190         RES_PCI_IO, PCI_ADDR(0, 1, 0, 0x74), 0xFFFFFF7B, 0x00000084,
191         RES_PCI_IO, PCI_ADDR(0, 1, 0, 0xF8), 0xFFFFFFCF, 0x00000010,
192
193         RES_PCI_IO, PCI_ADDR(0, 1, 1, 0xC4), 0xFFFFFFFE, 0x00000001,
194         RES_PCI_IO, PCI_ADDR(0, 1, 1, 0xF0), 0x7FFFFFFD, 0x00000002,
195         RES_PCI_IO, PCI_ADDR(0, 1, 1, 0xF8), 0xFFFFFFCF, 0x00000010,
196
197         RES_PCI_IO, PCI_ADDR(0, 8, 0, 0x40), 0x00000000, 0xCB8410DE,
198         RES_PCI_IO, PCI_ADDR(0, 8, 0, 0x68), 0xFFFFFF00, 0x000000FF,
199         RES_PCI_IO, PCI_ADDR(0, 8, 0, 0xF8), 0xFFFFFFBF, 0x00000040, /* Enable bridge mode. */
200
201         RES_PCI_IO, PCI_ADDR(0, 9, 0, 0x40), 0x00000000, 0xCB8410DE,
202         RES_PCI_IO, PCI_ADDR(0, 9, 0, 0x68), 0xFFFFFF00, 0x000000FF,
203         RES_PCI_IO, PCI_ADDR(0, 9, 0, 0xF8), 0xFFFFFFBF, 0x00000040, /* Enable bridge mode. */
204     };
205
206     static const unsigned int ctrl_conf_1_1[] = {
207         RES_PCI_IO, PCI_ADDR(0, 5, 0, 0x40), 0x00000000, 0xCB8410DE,
208         RES_PCI_IO, PCI_ADDR(0, 5, 0, 0x50), 0xFFFFFFFC, 0x00000003,
209         RES_PCI_IO, PCI_ADDR(0, 5, 0, 0x64), 0xFFFFFFFE, 0x00000001,
210         RES_PCI_IO, PCI_ADDR(0, 5, 0, 0x70), 0xFFF0FFFF, 0x00040000,
211         RES_PCI_IO, PCI_ADDR(0, 5, 0, 0xAC), 0xFFFFF0FF, 0x00000100,
212         RES_PCI_IO, PCI_ADDR(0, 5, 0, 0x7C), 0xFFFFFFEF, 0x00000000,
213         RES_PCI_IO, PCI_ADDR(0, 5, 0, 0xC8), 0xFF00FF00, 0x000A000A,
214         RES_PCI_IO, PCI_ADDR(0, 5, 0, 0xD0), 0xF0FFFFFF, 0x03000000,
215         RES_PCI_IO, PCI_ADDR(0, 5, 0, 0xE0), 0xF0FFFFFF, 0x03000000,
216     };
217
218     static const unsigned int ctrl_conf_mcp55_only[] = {
219         RES_PCI_IO, PCI_ADDR(0, 1, 1, 0x40), 0x00000000, 0xCB8410DE,
220         RES_PCI_IO, PCI_ADDR(0, 1, 1, 0xE0), 0xFFFFFEFF, 0x00000000,
221         RES_PCI_IO, PCI_ADDR(0, 1, 1, 0xE4), 0xFFFFFFFB, 0x00000000,
222         RES_PCI_IO, PCI_ADDR(0, 1, 1, 0xE8), 0xFFA9C8FF, 0x00003000,
223
224         RES_PCI_IO, PCI_ADDR(0, 4, 0, 0x40), 0x00000000, 0xCB8410DE,
225         RES_PCI_IO, PCI_ADDR(0, 4, 0, 0xF8), 0xFFFFFFCF, 0x00000010,
226
227         RES_PCI_IO, PCI_ADDR(0, 2, 0, 0x40), 0x00000000, 0xCB8410DE,
228
229         RES_PCI_IO, PCI_ADDR(0, 2, 1, 0x40), 0x00000000, 0xCB8410DE,
230         RES_PCI_IO, PCI_ADDR(0, 2, 1, 0x64), 0xF87FFFFF, 0x05000000,
231         RES_PCI_IO, PCI_ADDR(0, 2, 1, 0x78), 0xFFC07FFF, 0x00360000,
232         RES_PCI_IO, PCI_ADDR(0, 2, 1, 0x68), 0xFE00D03F, 0x013F2C00,
233         RES_PCI_IO, PCI_ADDR(0, 2, 1, 0x70), 0xFFF7FFFF, 0x00080000,
234         RES_PCI_IO, PCI_ADDR(0, 2, 1, 0x7C), 0xFFFFF00F, 0x00000570,
235         RES_PCI_IO, PCI_ADDR(0, 2, 1, 0xF8), 0xFFFFFFCF, 0x00000010,
236
237         RES_PCI_IO, PCI_ADDR(0, 6, 0, 0x04), 0xFFFFFEFB, 0x00000104,
238         RES_PCI_IO, PCI_ADDR(0, 6, 0, 0x3C), 0xF5FFFFFF, 0x0A000000,
239         RES_PCI_IO, PCI_ADDR(0, 6, 0, 0x40), 0x00C8FFFF, 0x07330000,
240         RES_PCI_IO, PCI_ADDR(0, 6, 0, 0x48), 0xFFFFFFF8, 0x00000005,
241         RES_PCI_IO, PCI_ADDR(0, 6, 0, 0x4C), 0xFE02FFFF, 0x004C0000,
242         RES_PCI_IO, PCI_ADDR(0, 6, 0, 0x74), 0xFFFFFFC0, 0x00000000,
243         RES_PCI_IO, PCI_ADDR(0, 6, 0, 0xC0), 0x00000000, 0xCB8410DE,
244         RES_PCI_IO, PCI_ADDR(0, 6, 0, 0xC4), 0xFFFFFFF8, 0x00000007,
245         RES_PCI_IO, PCI_ADDR(0, 1, 0, 0x78), 0xC0FFFFFF, 0x19000000,
246
247 #if CONFIG_MCP55_USE_AZA
248         RES_PCI_IO, PCI_ADDR(0, 6, 1, 0x40), 0x00000000, 0xCB8410DE,
249
250         // RES_PCI_IO, PCI_ADDR(0, 1, 1, 0xE4), ~(1 << 14), (1 << 14),
251 #endif
252
253 #ifdef MCP55_MB_SETUP
254         /* Play a while with GPIO in MCP55. */
255         MCP55_MB_SETUP
256 #endif
257
258 #if CONFIG_MCP55_USE_AZA
259         RES_PORT_IO_8, SYSCTRL_IO_BASE + 0xc0+ 21, ~(3 << 2), (2 << 2),
260         RES_PORT_IO_8, SYSCTRL_IO_BASE + 0xc0+ 22, ~(3 << 2), (2 << 2),
261         RES_PORT_IO_8, SYSCTRL_IO_BASE + 0xc0+ 46, ~(3 << 2), (2 << 2),
262 #endif
263     };
264
265     static const unsigned int ctrl_conf_master_only[] = {
266         RES_PORT_IO_32, ACPICTRL_IO_BASE + 0x80, 0xEFFFFFF, 0x01000000,
267
268         /* Master MCP55???? YHLU */
269         RES_PORT_IO_8, SYSCTRL_IO_BASE + 0xc0+ 0, ~(3 << 2), (0 << 2),
270     };
271
272     static const unsigned int ctrl_conf_2[] = {
273         /* I didn't put PCI-E related stuff here. */
274
275         RES_PCI_IO, PCI_ADDR(0, 0, 0, 0x74), 0xFFFFF00F, 0x000009D0,
276         RES_PCI_IO, PCI_ADDR(0, 1, 0, 0x74), 0xFFFF7FFF, 0x00008000,
277
278         RES_PORT_IO_32, SYSCTRL_IO_BASE + 0x48, 0xFFFEFFFF, 0x00010000,
279
280         RES_PORT_IO_32, ANACTRL_IO_BASE + 0x60, 0xFFFFFF00, 0x00000012,
281
282 #if CONFIG_MCP55_USE_NIC
283         RES_PCI_IO, PCI_ADDR(0, 1, 1, 0xe4), ~((1 << 22) | (1 << 20)), (1 << 22) | (1 << 20),
284
285         RES_PORT_IO_8, SYSCTRL_IO_BASE + 0xc0+ 4, ~(0xff), ((0 << 4) | (1 << 2) | (0 << 0)),
286         RES_PORT_IO_8, SYSCTRL_IO_BASE + 0xc0+ 4, ~(0xff), ((0 << 4) | (1 << 2) | (1 << 0)),
287 #endif
288     };
289
290         int j, i;
291
292         for (j = 0; j < mcp55_num; j++) {
293                 mcp55_early_pcie_setup(busn[j], devn[j],
294                                 io_base[j] + ANACTRL_IO_BASE, pci_e_x[j]);
295
296                 setup_resource_map_x_offset(ctrl_conf_1,
297                                 ARRAY_SIZE(ctrl_conf_1),
298                                 PCI_DEV(busn[j], devn[j], 0), io_base[j]);
299
300                 for (i = 0; i < 3; i++) { /* Three SATA */
301                         setup_resource_map_x_offset(ctrl_conf_1_1,
302                                 ARRAY_SIZE(ctrl_conf_1_1),
303                                 PCI_DEV(busn[j], devn[j], i), io_base[j]);
304                 }
305
306                 if (busn[j] == 0) {
307                         setup_resource_map_x_offset(ctrl_conf_mcp55_only,
308                                 ARRAY_SIZE(ctrl_conf_mcp55_only),
309                                 PCI_DEV(busn[j], devn[j], 0), io_base[j]);
310                 }
311
312                 if ((busn[j] == 0) && (mcp55_num>1)) {
313                         setup_resource_map_x_offset(ctrl_conf_master_only,
314                                 ARRAY_SIZE(ctrl_conf_master_only),
315                                 PCI_DEV(busn[j], devn[j], 0), io_base[j]);
316                 }
317
318                 setup_resource_map_x_offset(ctrl_conf_2,
319                                 ARRAY_SIZE(ctrl_conf_2),
320                                 PCI_DEV(busn[j], devn[j], 0), io_base[j]);
321         }
322
323 #if 0
324         for (j = 0; j < mcp55_num; j++) {
325                 // PCI-E (XSPLL) SS table 0x40, x044, 0x48
326                 // SATA  (SPPLL) SS table 0xb0, 0xb4, 0xb8
327                 // CPU   (PPLL)  SS table 0xc0, 0xc4, 0xc8
328                 setup_ss_table(io_base[j] + ANACTRL_IO_BASE + 0x40,
329                         io_base[j] + ANACTRL_IO_BASE + 0x44,
330                         io_base[j] + ANACTRL_IO_BASE + 0x48, pcie_ss_tbl, 64);
331                 setup_ss_table(io_base[j] + ANACTRL_IO_BASE + 0xb0,
332                         io_base[j] + ANACTRL_IO_BASE + 0xb4,
333                         io_base[j] + ANACTRL_IO_BASE + 0xb8, sata_ss_tbl, 64);
334                 setup_ss_table(io_base[j] + ANACTRL_IO_BASE + 0xc0,
335                         io_base[j] + ANACTRL_IO_BASE + 0xc4,
336                         io_base[j] + ANACTRL_IO_BASE + 0xc8, cpu_ss_tbl, 64);
337         }
338 #endif
339 }
340
341 #ifndef HT_CHAIN_NUM_MAX
342
343 #define HT_CHAIN_NUM_MAX        4
344 #define HT_CHAIN_BUSN_D         0x40
345 #define HT_CHAIN_IOBASE_D       0x4000
346
347 #endif
348
349 static int mcp55_early_setup_x(void)
350 {
351         /* Find out how many MCP55 we have. */
352         unsigned busn[HT_CHAIN_NUM_MAX] = {0};
353         unsigned devn[HT_CHAIN_NUM_MAX] = {0};
354         unsigned io_base[HT_CHAIN_NUM_MAX] = {0};
355
356         /*
357          * FIXME: May have problem if there is different MCP55 HTX card with
358          * different PCI_E lane allocation. Need to use same trick about
359          * pci1234 to verify node/link connection.
360          */
361         unsigned pci_e_x[HT_CHAIN_NUM_MAX] = {
362                 CONFIG_MCP55_PCI_E_X_0, CONFIG_MCP55_PCI_E_X_1,
363                 CONFIG_MCP55_PCI_E_X_2, CONFIG_MCP55_PCI_E_X_3,
364         };
365         int mcp55_num = 0, ht_c_index;
366         unsigned busnx, devnx;
367
368         /* FIXME: Multi PCI segment handling. */
369
370         /* Any system that only have IO55 without MCP55? */
371         for (ht_c_index = 0; ht_c_index < HT_CHAIN_NUM_MAX; ht_c_index++) {
372                 busnx = ht_c_index * HT_CHAIN_BUSN_D;
373                 for (devnx = 0; devnx < 0x20; devnx++) {
374                         u32 id;
375                         device_t dev;
376                         dev = PCI_DEV(busnx, devnx, 0);
377                         id = pci_read_config32(dev, PCI_VENDOR_ID);
378                         if(id == 0x036910de) {
379                                 busn[mcp55_num] = busnx;
380                                 devn[mcp55_num] = devnx;
381
382                                 /* We may have HT chain other than MCP55. */
383                                 io_base[mcp55_num]
384                                         = ht_c_index * HT_CHAIN_IOBASE_D;
385
386                                 mcp55_num++;
387                                 if (mcp55_num == CONFIG_MCP55_NUM)
388                                         goto out;
389                                 break; /* Only one MCP55 on one chain. */
390                         }
391                 }
392         }
393
394 out:
395         print_debug("mcp55_num:");
396         print_debug_hex8(mcp55_num);
397         print_debug("\n");
398
399         mcp55_early_set_port(mcp55_num, busn, devn, io_base);
400         mcp55_early_setup(mcp55_num, busn, devn, io_base, pci_e_x);
401
402         mcp55_early_clear_port(mcp55_num, busn, devn, io_base);
403
404         // set_ht_link_mcp55(HT_CHAIN_NUM_MAX);
405
406         return 0;
407 }