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