Add AMD SR56x0 support.
[coreboot.git] / src / southbridge / amd / sr5650 / pcie.c
1 /*
2  * This file is part of the coreboot project.
3  *
4  * Copyright (C) 2010 Advanced Micro Devices, Inc.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; version 2 of the License.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
18  */
19
20 #include <console/console.h>
21 #include <device/device.h>
22 #include <device/pci.h>
23 #include <device/pci_ids.h>
24 #include <device/pci_ops.h>
25 #include <delay.h>
26 #include "sr5650.h"
27 #include "cmn.h"
28
29 /*------------------------------------------------
30 * Global variable
31 ------------------------------------------------*/
32 PCIE_CFG AtiPcieCfg = {
33         PCIE_ENABLE_STATIC_DEV_REMAP,   /* Config */
34         0,                      /* ResetReleaseDelay */
35         0,                      /* Gfx0Width */
36         0,                      /* Gfx1Width */
37         0,                      /* GfxPayload */
38         0,                      /* GppPayload */
39         0,                      /* PortDetect, filled by GppSbInit */
40         0,                      /* PortHp */
41         0,                      /* DbgConfig */
42         0,                      /* DbgConfig2 */
43         0,                      /* GfxLx */
44         0,                      /* GppLx */
45         0,                      /* NBSBLx */
46         0,                      /* PortSlotInit */
47         0,                      /* Gfx0Pwr */
48         0,                      /* Gfx1Pwr */
49         0                       /* GppPwr */
50 };
51
52 static void ValidatePortEn(device_t nb_dev);
53
54 static void ValidatePortEn(device_t nb_dev)
55 {
56 }
57
58 /*****************************************************************
59 * Compliant with CIM_33's PCIEPowerOffGppPorts
60 * Power off unused GPP lines
61 *****************************************************************/
62 static void PciePowerOffGppPorts(device_t nb_dev, device_t dev, u32 port)
63 {
64         u32 reg;
65         u16 state_save;
66         struct southbridge_amd_sr5650_config *cfg =
67                 (struct southbridge_amd_sr5650_config *)nb_dev->chip_info;
68         u16 state = cfg->port_enable;
69
70         if (!(AtiPcieCfg.Config & PCIE_DISABLE_HIDE_UNUSED_PORTS))
71                 state &= AtiPcieCfg.PortDetect;
72         state = ~state;
73         state &= (1 << 4) + (1 << 5) + (1 << 6) + (1 << 7);
74         state_save = state << 17;
75         state &= !(AtiPcieCfg.PortHp);
76         reg = nbmisc_read_index(nb_dev, 0x0c);
77         reg |= state;
78         nbmisc_write_index(nb_dev, 0x0c, reg);
79
80         reg = nbmisc_read_index(nb_dev, 0x08);
81         reg |= state_save;
82         nbmisc_write_index(nb_dev, 0x08, reg);
83
84         if ((AtiPcieCfg.Config & PCIE_OFF_UNUSED_GPP_LANES)
85             && !(AtiPcieCfg.
86                  Config & (PCIE_DISABLE_HIDE_UNUSED_PORTS +
87                            PCIE_GFX_COMPLIANCE))) {
88         }
89         /* step 3 Power Down Control for Southbridge */
90         reg = nbpcie_p_read_index(dev, 0xa2);
91
92         switch ((reg >> 4) & 0x7) {     /* get bit 4-6, LC_LINK_WIDTH_RD */
93         case 1:
94                 nbpcie_ind_write_index(nb_dev, 0x65, 0x0e0e);
95                 break;
96         case 2:
97                 nbpcie_ind_write_index(nb_dev, 0x65, 0x0c0c);
98                 break;
99         default:
100                 break;
101         }
102 }
103
104 /**********************************************************************
105 **********************************************************************/
106 static void switching_gpp1_configurations(device_t nb_dev, device_t sb_dev)
107 {
108         u32 reg;
109         struct southbridge_amd_sr5650_config *cfg =
110             (struct southbridge_amd_sr5650_config *)nb_dev->chip_info;
111
112         /* 4.3.3.1.1.1.step1. Asserts PCIE-GPP1 global reset */
113         reg = nbmisc_read_index(nb_dev, 0x8);
114         reg |= 1 << 15;
115         nbmisc_write_index(nb_dev, 0x8, reg);
116
117         /* 4.3.3.1.1.1.step2. De-asserts STRAP_BIF_all_valid for PCIE-GPP1 core */
118         reg = nbmisc_read_index(nb_dev, 0x26);
119         reg |= 1 << 28;
120         nbmisc_write_index(nb_dev, 0x26, reg);
121
122         /* 4.3.3.1.1.1.step3. Programs PCIE-GPP1 to be desired port configuration 8:8 or 16:0. */
123         reg = nbmisc_read_index(nb_dev, 0x8);
124         reg &= ~(1 << 8);               /* clean */
125         reg |= cfg->gpp1_configuration << 8;
126         nbmisc_write_index(nb_dev, 0x8, reg);
127
128         /* 4.3.3.1.1.1.step4. Wait for 2ms */
129         mdelay(1);
130
131         /* 4.3.3.1.1.1.step5. Asserts STRAP_BIF_all_valid for PCIE-GPP1 core */
132         reg = nbmisc_read_index(nb_dev, 0x26);
133         reg &= ~(1 << 28);
134         nbmisc_write_index(nb_dev, 0x26, reg);
135
136         /* 4.3.3.1.1.1.step6. De-asserts PCIE-GPP1 global reset */
137         reg = nbmisc_read_index(nb_dev, 0x8);
138         reg &= ~(1 << 15);
139         nbmisc_write_index(nb_dev, 0x8, reg);
140
141         /* Follow the procedure for PCIE-GPP1 common initialization and
142          * link training sequence. */
143 }
144
145 /**********************************************************************
146 **********************************************************************/
147 static void switching_gpp2_configurations(device_t nb_dev, device_t sb_dev)
148 {
149         u32 reg;
150         struct southbridge_amd_sr5650_config *cfg =
151             (struct southbridge_amd_sr5650_config *)nb_dev->chip_info;
152
153         /* 4.3.3.1.1.2.step1. Asserts PCIE-GPP2 global reset */
154         reg = nbmisc_read_index(nb_dev, 0x8);
155         reg |= 1 << 13;
156         nbmisc_write_index(nb_dev, 0x8, reg);
157
158         /* 4.3.3.1.1.2.step2. De-asserts STRAP_BIF_all_valid for PCIE-GPP2 core */
159         reg = nbmisc_read_index(nb_dev, 0x26);
160         reg |= 1 << 29;
161         nbmisc_write_index(nb_dev, 0x26, reg);
162
163         /* 4.3.3.1.1.2.step3. Programs PCIE-GPP2 to be desired port configuration 8:8 or 16:0. */
164         reg = nbmisc_read_index(nb_dev, 0x8);
165         reg &= ~(1 << 9);               /* clean */
166         reg |= (cfg->gpp2_configuration & 1) << 9;
167         nbmisc_write_index(nb_dev, 0x8, reg);
168
169         /* 4.3.3.1.1.2.step4. Wait for 2ms */
170         mdelay(2);
171
172         /* 4.3.3.1.1.2.step5. Asserts STRAP_BIF_all_valid for PCIE-GPP2 core */
173         reg = nbmisc_read_index(nb_dev, 0x26);
174         reg &= ~(1 << 29);
175         nbmisc_write_index(nb_dev, 0x26, reg);
176
177         /* 4.3.3.1.1.2.step6. De-asserts PCIE-GPP2 global reset */
178         reg = nbmisc_read_index(nb_dev, 0x8);
179         reg &= ~(1 << 13);
180         nbmisc_write_index(nb_dev, 0x8, reg);
181
182         /* Follow the procedure for PCIE-GPP2 common initialization and
183          * link training sequence. */
184 }
185 static void switching_gpp3a_configurations(device_t nb_dev, device_t sb_dev)
186 {
187         u32 reg;
188         struct southbridge_amd_sr5650_config *cfg =
189             (struct southbridge_amd_sr5650_config *)nb_dev->chip_info;
190
191         /* 4.3.3.2.3.2.step1. Asserts PCIE-GPP3a global reset. */
192         reg = nbmisc_read_index(nb_dev, 0x8);
193         reg |= 1 << 31;
194         nbmisc_write_index(nb_dev, 0x8, reg);
195         /* 4.3.3.2.3.2.step2. De-asserts STRAP_BIF_all_valid for PCIE-GPP3a core */
196         reg = nbmisc_read_index(nb_dev, 0x26);
197         reg |= 1 << 30;
198         nbmisc_write_index(nb_dev, 0x26, reg);
199         /* 4.3.3.2.3.2.step3. Programs the desired PCIE-GPP3a configuration. */
200         reg = nbmisc_read_index(nb_dev, 0x67);
201         reg &= ~0x1F;           /* clean */
202         reg |= cfg->gpp3a_configuration;
203         nbmisc_write_index(nb_dev, 0x67, reg);
204         /* 4.3.3.2.3.2.step4. Programs PCIE-GPP3a Line Director. */
205         reg = nbmisc_read_index(nb_dev, 0x26);
206         reg &= 0xF0000000;      /* TODO:Lane reversed. */
207         switch (cfg->gpp3a_configuration) {
208         case 0xB:               /* 1:1:1:1:1:1 */
209                 reg |= 0x2AA3554;
210                 break;
211         case 0x1:               /* 4:2:0:0:0:0 */
212                 reg |= 0x055B000;
213                 break;
214         case 0x2:               /* 4:1:1:0:0:0 */
215                 reg |= 0x215B400;
216                 break;
217         case 0xC:               /* 2:2:2:0:0:0 */
218                 reg |= 0xFF0BAA0;
219                 break;
220         case 0xA:               /* 2:2:1:1:0:0 */
221                 reg |= 0x215B400;
222                 break;
223         case 0x4:               /* 2:1:1:1:1:0 */
224                 reg |= 0xFF0BAA0;
225                 break;
226         default:        /* shouldn't be here. */
227                 printk(BIOS_DEBUG, "Warning:gpp3a_configuration is not correct. Check you devicetree.cb\n");
228                 break;
229         }
230         nbmisc_write_index(nb_dev, 0x26, reg);
231         /* 4.3.3.2.3.2.step5. De-asserts STRAP_BIF_all_valid for PCIE-GPP3a core */
232         reg = nbmisc_read_index(nb_dev, 0x26);
233         reg &= ~(1 << 30);
234         nbmisc_write_index(nb_dev, 0x26, reg);
235         /* 4.3.3.2.3.2.step6. De-asserts PCIE-GPP3a global reset. */
236         reg = nbmisc_read_index(nb_dev, 0x8);
237         reg &= ~(1 << 31);
238         nbmisc_write_index(nb_dev, 0x8, reg);
239 }
240
241 /*****************************************************************
242 * The sr5650 uses NBCONFIG:0x1c (BAR3) to map the PCIE Extended Configuration
243 * Space to a 256MB range within the first 4GB of addressable memory.
244 *****************************************************************/
245 void enable_pcie_bar3(device_t nb_dev)
246 {
247         printk(BIOS_DEBUG, "enable_pcie_bar3()\n");
248         set_nbcfg_enable_bits(nb_dev, 0x7C, 1 << 30, 1 << 30);  /* Enables writes to the BAR3 register. */
249         set_nbcfg_enable_bits(nb_dev, 0x84, 7 << 16, 0 << 16);
250
251         pci_write_config32(nb_dev, 0x1C, EXT_CONF_BASE_ADDRESS);        /* PCIEMiscInit */
252         pci_write_config32(nb_dev, 0x20, 0x00000000);
253         set_htiu_enable_bits(nb_dev, 0x32, 1 << 28, 1 << 28);   /* PCIEMiscInit */
254         ProgK8TempMmioBase(1, EXT_CONF_BASE_ADDRESS, TEMP_MMIO_BASE_ADDRESS);
255 }
256
257 /*****************************************************************
258 * We should disable bar3 when we want to exit sr5650_enable, because bar3 will be
259 * remapped in set_resource later.
260 *****************************************************************/
261 void disable_pcie_bar3(device_t nb_dev)
262 {
263         printk(BIOS_DEBUG, "disable_pcie_bar3()\n");
264         pci_write_config32(nb_dev, 0x1C, 0);    /* clear BAR3 address */
265         set_nbcfg_enable_bits(nb_dev, 0x7C, 1 << 30, 0 << 30);  /* Disable writes to the BAR3. */
266         ProgK8TempMmioBase(0, EXT_CONF_BASE_ADDRESS, TEMP_MMIO_BASE_ADDRESS);
267 }
268
269 /*
270 */
271 void init_gen2(device_t nb_dev, device_t dev, u8 port)
272 {
273         u32 reg, val;
274         /* for A11 (0x89 == 0) */
275         reg = 0x34;
276         if (port <= 3){
277                 val = 1<<5;
278         }else{
279                 val = 1<<31;
280                 if (port >= 9 )
281                         reg = 0x39;
282         }
283
284         /* todo: check for rev > a11
285         switch (port) {
286                 case 2;
287                         reg = 0x34;
288                         val = 1<<5;
289                         break;
290                 case 3:
291                         reg = 0x22;
292                         val = 1<<6;
293                         break;
294                 case 4:
295                         reg = 0x34;
296                         val = 1<<31;
297                         break;
298                 case 5:
299                 case 6:
300                         reg = 0x39;
301                         val = 1<<31;
302                         break;
303                 case 7..9:
304                         reg = 0x37;
305                         val = 1<<port;
306                         break;
307                 case 10:
308                         reg = 0x22;
309                         val = 1<<5;
310                         break;
311                 default:
312                         reg = 0;
313                         break;
314         }
315         */
316         set_pcie_enable_bits(dev, 0xA4, 0x1, 0x1);
317         pci_ext_write_config32(nb_dev, dev, 0x88, 0xF0, 1<<2); /* LINK_CRTL2*/
318         set_nbmisc_enable_bits(nb_dev, reg, val, val);
319 }
320
321
322 /* Alternative to default CPL buffer count */
323 const u8 pGpp420000[] = {0x38, 0x1C};
324 const u8 pGpp411000[] = {0x38, 0x0E, 0x0E};
325 const u8 pGpp222000[] = {0x1C, 0x1C, 0x1C};
326 const u8 pGpp221100[] = {0x1C, 0x1C, 0x0E, 0x0E};
327 const u8 pGpp211110[] = {0x1C, 0x0E, 0x0E, 0x0E, 0, 0x0E, 0x0E};
328 const u8 pGpp111111[] = {0x0E, 0x0E, 0x0E, 0x0E, 0, 0x0E, 0x0E};
329
330 /*
331  * Enabling Dynamic Slave CPL Buffer Allocation Feature for PCIE-GPP3a Ports
332  * PcieLibCplBufferAllocation
333  */
334 static void gpp3a_cpl_buf_alloc(device_t nb_dev, device_t dev)
335 {
336         u8 dev_index;
337         u8 *slave_cpl;
338         u8 value;
339         struct southbridge_amd_sr5650_config *cfg =
340             (struct southbridge_amd_sr5650_config *)nb_dev->chip_info;
341
342         dev_index = dev->path.pci.devfn >> 3;
343         if (dev_index < 4 || dev_index > 0xa) {
344                 return;
345         }
346
347         switch (cfg->gpp3a_configuration) {
348         case 0x1: /* 4:2:0:0:0:0 */
349                 slave_cpl = (u8 *)&pGpp420000;
350                 break;
351         case 0x2: /* 4:1:1:0:0:0 */
352                 slave_cpl = (u8 *)&pGpp411000;
353                 break;
354         case 0xC: /* 2:2:2:0:0:0 */
355                 slave_cpl = (u8 *)&pGpp222000;
356                 break;
357         case 0xA: /* 2:2:1:1:0:0 */
358                 slave_cpl = (u8 *)&pGpp221100;
359                 break;
360         case 0x4: /* 2:1:1:1:1:0 */
361                 slave_cpl = (u8 *)&pGpp211110;
362                 break;
363         case 0xB: /* 1:1:1:1:1:1 */
364                 slave_cpl = (u8 *)&pGpp111111;
365                 break;
366         default:  /* shouldn't be here. */
367                 printk(BIOS_DEBUG, "buggy gpp3a_configuration\n");
368                 break;
369         }
370
371         value = slave_cpl[dev_index - 4];
372         if (value != 0) {
373                 set_pcie_enable_bits(dev, 0x10, 0x3f << 8, value << 8);
374                 set_pcie_enable_bits(dev, 0x20, 1 << 11, 1 << 11);
375         }
376 }
377
378 /*
379  * Enabling Dynamic Slave CPL Buffer Allocation Feature for PCIE-GPP1/PCIE-GPP2 Ports
380  * PcieLibCplBufferAllocation
381  */
382 static void gpp12_cpl_buf_alloc(device_t nb_dev, device_t dev)
383 {
384         u8 gpp_cfg;
385         u8 value;
386         u8 dev_index;
387
388         dev_index = dev->path.pci.devfn >> 3;
389         struct southbridge_amd_sr5650_config *cfg =
390             (struct southbridge_amd_sr5650_config *)nb_dev->chip_info;
391
392         if (dev_index < 4) {
393                 gpp_cfg = cfg->gpp1_configuration;
394         } else if (dev_index > 0xa) {
395                 gpp_cfg = cfg->gpp2_configuration;
396         } else {
397                 return;
398         }
399
400         if (gpp_cfg == 0) {
401                 /* Configuration 16:0,  leave the default value */
402         } else if (gpp_cfg == 1) {
403                 /* Configuration 8:8 */
404                 value = 0x60;
405                 set_pcie_enable_bits(dev, 0x10, 0x3f << 8, value << 8);
406                 set_pcie_enable_bits(dev, 0x20, 1 << 11, 1 << 11);
407         } else {
408                 printk(BIOS_DEBUG, "buggy gpp configuration\n");
409         }
410 }
411
412 #if 0                           /* BTS report error without this function. But some board
413                                  * fail to boot. Leave it here for future debug. */
414
415 /*
416  * Enable LCLK clock gating
417  */
418 static void EnableLclkGating(device_t dev)
419 {
420         u8 port;
421         u32 reg = 0;
422         u32 mask = 0;
423         u32 value = 0;
424         device_t nb_dev = dev_find_slot(0, 0);
425         device_t clk_f1= dev_find_slot(0, 1);
426
427         port = dev->path.pci.devfn >> 3;
428         switch (port) {
429                 //PCIE_CORE_INDEX_GPP1
430                 case 2:
431                 case 3:
432                         reg = 0x94;
433                         mask = 1 << 16;
434                         break;
435
436                 //PCIE_CORE_INDEX_GPP2
437                 case 11:
438                 case 12:
439                         reg = 0xE8;
440                         value = 1 << 28;
441                         break;
442
443                 //PCIE_CORE_INDEX_GPP3a
444                 case 4 ... 7:
445                 case 9:
446                 case 10:
447                         reg = 0xE8;
448                         value = 1 << 31;
449                         break;
450
451                 //PCIE_CORE_INDEX_GPP3b;
452                 case 13:
453                         reg = 0xE8;
454                         value = 1 << 25;
455                         break;
456
457                 //PCIE_CORE_INDEX_SB;
458                 case 8:
459                         reg = 0x94;
460                         mask = 1 << 24;
461                         break;
462                 default:
463                         break;
464         }
465         /* enable access func1 */
466         set_nbcfg_enable_bits(nb_dev, 0x4C, 1 << 0, 1 << 0);
467         set_nbcfg_enable_bits(clk_f1, reg, mask, value);
468 }
469 #endif
470
471 /*****************************************
472 * Compliant with CIM_33's PCIEGPPInit
473 * nb_dev:
474 *       root bridge struct
475 * dev:
476 *       p2p bridge struct
477 * port:
478 *       p2p bridge number, 4-10
479 *****************************************/
480 void sr5650_gpp_sb_init(device_t nb_dev, device_t dev, u32 port)
481 {
482         u32 gpp_sb_sel = 0;
483         struct southbridge_amd_sr5650_config *cfg =
484             (struct southbridge_amd_sr5650_config *)nb_dev->chip_info;
485
486         printk(BIOS_DEBUG, "gpp_sb_init nb_dev=0x%p, dev=0x%p, port=0x%x\n", nb_dev, dev, port);
487         switch (port) {
488         case 2:
489         case 3:
490                 gpp_sb_sel = PCIE_CORE_INDEX_GPP1;
491                 break;
492         case 4 ... 7:
493         case 9:
494         case 10:
495                 gpp_sb_sel = PCIE_CORE_INDEX_GPP3a;
496                 break;
497         case 8:
498                 gpp_sb_sel = PCIE_CORE_INDEX_SB;
499                 break;
500         case 11:
501         case 12:
502                 gpp_sb_sel = PCIE_CORE_INDEX_GPP2;
503                 break;
504         case 13:
505                 gpp_sb_sel = PCIE_CORE_INDEX_GPP3b;
506                 break;
507         }
508
509         /* Init common Core registers */
510         set_pcie_enable_bits(dev, 0xB1, 1 << 28 | 1 << 23 | 1 << 20 | 1 << 19,
511                 1 << 28 | 1 << 23 | 1 << 20 | 1 << 19);
512         if (gpp_sb_sel == PCIE_CORE_INDEX_GPP3a) {
513                 set_pcie_enable_bits(dev, 0xB1, 1 << 22, 1 << 22);
514                 /* 4.3.3.2.3 Step 10: Dynamic Slave CPL Buffer Allocation */
515                 gpp3a_cpl_buf_alloc(nb_dev, dev);
516         }
517         if (gpp_sb_sel == PCIE_CORE_INDEX_GPP1 || gpp_sb_sel == PCIE_CORE_INDEX_GPP2) {
518                 gpp12_cpl_buf_alloc(nb_dev, dev);
519         }
520         set_pcie_enable_bits(dev, 0xA1, (1 << 26) | (1 << 24) | (1 << 11), 1 << 11);
521         set_pcie_enable_bits(dev, 0xA0, 0x0000FFF0, 0x6830);
522         // PCIE should not ignore malformed packet error or ATS request
523         set_pcie_enable_bits(dev, 0x70, 1 << 12, 0);
524         //Step 14.1: Advertising Hot Plug Capabilities
525         set_pcie_enable_bits(dev, 0x10, 1 << 4, 1 << 4); //Enable power fault
526
527         set_pcie_enable_bits(nb_dev, 0xC1 | gpp_sb_sel, 1 << 0, 1 << 0);
528
529         /* init GPP core */
530         /* 4.4.2.step13.1. Sets RCB completion timeout to be 200ms */
531         pci_ext_write_config32(nb_dev, dev, 0x80, 0xF << 0, 0x6 << 0);
532         /* 4.4.2.step13.2. RCB completion timeout on link down to shorten enumeration time. */
533         set_pcie_enable_bits(dev, 0x70, 1 << 19, 1 << 19);
534         /* 4.4.2.step13.3. Enable slave ordering rules */
535         set_pcie_enable_bits(nb_dev, 0x20 | gpp_sb_sel, 1 << 8, 0 << 8);
536         /* 4.4.2.step13.4. Sets DMA payload size to 64 bytes. */
537         set_pcie_enable_bits(nb_dev, 0x10 | gpp_sb_sel, 7 << 10, 4 << 10);
538         /* 4.4.2.step13.5. Set REGS_DLP_IGNORE_IN_L1_EN to ignore DLLPs
539            during L1 so that Tx Clk can be turned off. */
540         set_pcie_enable_bits(nb_dev, 0x02 | gpp_sb_sel, 1 << 0 | 1 << 8, 1 << 0 | 1 << 8); // add bit 8 from CIMx
541         /* 4.4.2.step13.6. Set REGS_LC_ALLOW_TX_L1_CONTROL to allow TX to
542            prevent LC from going to L1 when there are outstanding completions.*/
543         set_pcie_enable_bits(dev, 0x02, 1 << 15, 1 << 15);
544         /* 4.4.2.step13.7. Set REGS_LC_DONT_GO_TO_L0S_IF_L1_ARMED to prevent
545            lc to go to from L0 to Rcv_L0s if L1 is armed. */
546         set_pcie_enable_bits(dev, 0xA1, 1 << 11, 1 << 11);
547         /* 4.4.2.step13.8. CMGOOD_OVERRIDE for all five PCIe cores. */
548         set_nbmisc_enable_bits(nb_dev, 0x22, 1 << 27, 1 << 27);
549         /* 4.4.2.step13.9. Prevents Electrical Idle from causing a
550            transition from Rcv_L0 to Rcv_L0s. */
551         set_pcie_enable_bits(dev, 0xB1, 1 << 20, 1 << 20);
552         /* 4.4.2.step13.10. Prevents the LTSSM from going to Rcv_L0s if
553            it has already acknowledged a request to go
554            to L1 but it has not transitioned there yet. */
555         /* seems the same as step13.7 */
556         set_pcie_enable_bits(dev, 0xA1, 1 << 11, 1 << 11);
557         /* 4.4.2.step13.11. Transmits FTS before Recovery. */
558         set_pcie_enable_bits(dev, 0xA3, 1 << 9, 1 << 9);
559         /* 4.4.2.step13.12. Sets TX arbitration algorithm to round robin
560            for PCIE-GPP1, PCIE-GPP2, PCIE-GPP3a and PCIE-GPP3b cores only. */
561         //if (gpp_sb_sel != PCIE_CORE_INDEX_SB) /* RPR NOT set SB_CORE, BTS set SB_CORE, we comply with BTS */
562                 set_pcie_enable_bits(nb_dev, 0x1C | gpp_sb_sel, 0x7FF, 0x109);
563         /* 4.4.2.step13.13. Sets number of TX Clocks to drain TX Pipe to 0x3.*/
564         set_pcie_enable_bits(dev, 0xA0, 0xF << 4, 0x3 << 4);
565         /* 4.4.2.step13.14. Lets PI use Electrical Idle from PHY when
566            turning off PLL in L1 at Gen 2 speed instead of Inferred Electrical
567            Idle.
568            NOTE: LC still uses Inferred Electrical Idle. */
569         set_pcie_enable_bits(nb_dev, 0x40 | gpp_sb_sel, 3 << 14, 2 << 14);
570         /* 4.4.2.step13.15. Turn on rx_fronten_en for all active lanes upon
571            exit from Electrical Idle, rather than being tied to PLL_PDNB. */
572         set_pcie_enable_bits(nb_dev, 0xC2 | gpp_sb_sel, 1 << 25, 1 << 25);
573
574         /* 4.4.2.step13.16. Advertises TX L0s and L1 exit latency.
575            TX L0s exit latency to be 100b: 512ns to less than 1us;
576            L1 exit latency to be 011b: 4us to less than 8us.
577            For Hot-Plug Slots: Advertise TX L0s and L1 exit latency.
578            TX L0s exit latency to be 110b: 2us to 4us.
579            L1 exit latency to be 111b: more than 64us.*/
580         //set_pcie_enable_bits(dev, 0xC1, 0xF << 0, 0xC << 0); /* 0xF for htplg. */
581         set_pcie_enable_bits(dev, 0xC1, 0xF << 0, 0xF << 0); /* 0xF for htplg. */
582         /* 4.4.2.step13.17. Always ACK an ASPM L1 entry DLLP to
583            workaround credit control issue on PM_NAK
584            message of SB700 and SB800. */
585         /* 4.4.4.step13.18. To allow advertising Gen 2 capabilities to Southbridge. */
586         if (port == 8) {
587                 set_pcie_enable_bits(dev, 0xA0, 1 << 23, 1 << 23);
588                 set_pcie_enable_bits(nb_dev, 0xC1 | gpp_sb_sel, 1 << 1, 1 << 1);
589         }
590         /* 4.4.2.step13.19. CMOS Option (Gen 2 AUTO-Part 1 - Enabled by Default) */
591         /* 4.4.2.step13.20. CMOS Option (RC Advertised Gen 2-Part1 - Disabled by Default)*/
592         set_nbcfg_enable_bits(dev, 0x88, 0xF << 0, 0x2 << 0);
593         /* Disables GEN2 capability of the device.
594          * RPR typo- it says enable but the bit setting says disable.
595          * Disable it here and we enable it later. */
596         set_pcie_enable_bits(dev, 0xA4, 1 << 0, 1 << 0);
597         /* 4.4.2.step13.21. */
598         /* 4.4.2.step13.22 */
599         /* Enable native PME. */
600         set_pcie_enable_bits(dev, 0x10, 1 << 3, 1 < 3);
601         /* This bit when set indicates that the PCIe Link associated with this port
602            is connected to a slot. */
603         pci_ext_write_config32(nb_dev, dev, 0x5a, 1 << 8, 1 << 8);
604         /* This bit when set indicates that this slot is capable of supporting
605            Hot-Plug operations. */
606         set_nbcfg_enable_bits(dev, 0x6C, 1 << 6, 1 << 6);
607         /* Enables flushing of TLPs when Data Link is down. */
608         set_pcie_enable_bits(dev, 0x20, 1 << 19, 0 << 19);
609
610         /* 4.4.2.step14. Server Class Hot Plug Feature */
611         /* 4.4.2 step14.1: Advertising Hot Plug Capabilities */
612         /* 4.4.2.step14.2: Firmware Upload */
613         /* 4.4.2.Step14.3: SBIOS Acknowledgment to Firmware of Successful Firmware Upload */
614         /* step14.4 */
615         /* step14.5 */
616         /* skip */
617
618         /* CIMx LPC Deadlock workaround - Enable Memory Write Map*/
619         if (gpp_sb_sel == PCIE_CORE_INDEX_SB) {
620                 set_pcie_enable_bits(nb_dev, 0x10 | gpp_sb_sel, 1 << 9, 1 << 9);
621                 set_htiu_enable_bits(nb_dev, 0x06, 1 << 26, 1 << 26);
622         }
623
624         /* This CPL setup requires more than this one register and should be done in gpp_core.
625          * The additional setup is for the different revisions. */
626
627         /* CIMx CommonPortInit settings that are not set above. */
628         pci_ext_write_config32(nb_dev, dev, 0x88, 0xF0, 1 << 0); /* LINK_CRTL2 */
629
630         if ( port == 8 )
631                 set_pcie_enable_bits(dev, 0xA0, 0, 1 << 23);
632
633         /* set automatic Gen2 support, needs mainboard config option as Gen2 can cause issues on some platforms. */
634         init_gen2(nb_dev, dev, port);
635         set_pcie_enable_bits(dev, 0xA4, 1 << 29, 1 << 29);
636         set_pcie_enable_bits(dev, 0xC0, 1 << 15, 0);
637         set_pcie_enable_bits(dev, 0xA2, 1 << 13, 0);
638
639         /* Hotplug Support - bit5 + bit6  capable and surprise */
640         pci_ext_write_config32(nb_dev, dev, 0x6c, 0x60, 0x60);
641
642         /* Set interrupt pin info 0x3d */
643         pci_ext_write_config32(nb_dev, dev, 0x3c, 1 << 8, 1 << 8);
644
645         /* 5.12.9.3 Hotplug step 1 - NB_PCIE_ROOT_CTRL - enable pm irq
646         The RPR is wrong - this is not a PCIEND_P register */
647         pci_ext_write_config32(nb_dev, dev, 0x74, 1 << 3, 1 << 3);
648
649         /* 5.12.9.3 step 2 - PCIEP_PORT_CNTL - enable hotplug messages */
650         if ( port != 8)
651                 set_pcie_enable_bits(dev, 0x10, 1 << 2, 1 << 2);
652
653         /* Not sure about this PME setup */
654         /* Native PME */
655         set_pcie_enable_bits(dev, 0x10, 1 << 3, 1 << 3); /* Not set in CIMx */
656
657         /* PME Enable */
658         pci_ext_write_config32(nb_dev, dev, 0x54, 1 << 8, 1 << 8); /* Not in CIMx */
659
660         /* 4.4.3 Training for GPP devices */
661         /* init GPP */
662         switch (port) {
663         case 2:
664         case 3:
665         case 4: /* GPP_SB */
666         case 5:
667         case 6:
668         case 7:
669         case 9: /*GPP*/
670         case 10:
671         case 11:
672         case 12:
673         case 13:
674                 /* 4.4.2.step13.5. Blocks DMA traffic during C3 state */
675                 set_pcie_enable_bits(dev, 0x10, 1 << 0, 0 << 0);
676                 /* Enabels TLP flushing */
677                 set_pcie_enable_bits(dev, 0x20, 1 << 19, 0 << 19);
678
679                 /* check port enable */
680                 if (cfg->port_enable & (1 << port)) {
681                         PcieReleasePortTraining(nb_dev, dev, port);
682                         if (!(AtiPcieCfg.Config & PCIE_GPP_COMPLIANCE)) {
683                                 u8 res = PcieTrainPort(nb_dev, dev, port);
684                                 printk(BIOS_DEBUG, "PcieTrainPort port=0x%x result=%d\n", port, res);
685                                 if (res) {
686                                         AtiPcieCfg.PortDetect |= 1 << port;
687                                 }
688                         }
689                 }
690                 break;
691         case 8:         /* SB */
692                 break;
693         default:
694                 break;
695         }
696
697         /* Re-enable RC ordering logic after training (from CIMx)*/
698         set_pcie_enable_bits(nb_dev, 0x20 | gpp_sb_sel, 1 << 9, 0);
699
700         /* Advertising Hot Plug Capabilities */
701         pci_ext_write_config32(nb_dev, dev, 0x6c, 0x04001B, 0x00001B);
702
703         /* PCIE Late Init (CIMx late init - Maybe move somewhere else? Later in the coreboot PCI device enum?) */
704         /* Set Slot Number */
705         pci_ext_write_config32(nb_dev, dev, 0x6c, 0x1FFF << 19, port << 19);
706
707         /* Set Slot present 0x5A*/
708         pci_ext_write_config32(nb_dev, dev, 0x58, 1 << 24, 1 << 24);
709
710         //PCIE-GPP1 TXCLK Clock Gating In L1  Late Core sttting - Maybe move somewhere else? */
711         set_pcie_enable_bits(nb_dev, 0x11 | gpp_sb_sel, 0xF << 0, 0x0C << 0);
712         /* Enable powering down PLLs in L1 or L23 Ready states.
713          * Turns off PHY`s RX FRONTEND during L1 when PLL power down is enabled */
714         set_pcie_enable_bits(nb_dev, 0x40 | gpp_sb_sel, 0x1219, 0x1009);
715         /* 4.4..7.1 TXCLK Gating in L1, Enables powering down TXCLK clock pads on the receive side. */
716         set_pcie_enable_bits(nb_dev, 0x40 | gpp_sb_sel, 1 << 6, 1 << 6);
717
718         /* Step 21: Register Locking PCIE Misc. Late Core sttting - Must move somewhere do PciInitLate FIXME */
719         /* Lock HWInit Register */
720         //set_pcie_enable_bits(nb_dev, 0x10 | gpp_sb_sel, 1 << 0, 1 << 0);
721
722         /* Step 27: LCLK Gating */
723         //EnableLclkGating(dev);
724
725         /* Set Common Clock */
726         /* If dev present, set PcieCapPtr+0x10, BIT6);
727          * set dev 0x68,bit 6
728          * retrain link, set dev, 0x68 bit 5;
729          * wait dev 0x6B bit3 clear
730          */
731
732         if (port == 8){
733                 PciePowerOffGppPorts(nb_dev, dev, port); /* , This should be run for all ports that are not hotplug and don't detect devices */
734         }
735 }
736
737 /*****************************************
738 * Compliant with CIM_33's PCIEConfigureGPPCore
739 *****************************************/
740 void config_gpp_core(device_t nb_dev, device_t sb_dev)
741 {
742         u32 reg;
743         struct southbridge_amd_sr5650_config *cfg =
744                 (struct southbridge_amd_sr5650_config *)nb_dev->chip_info;
745
746         reg = nbmisc_read_index(nb_dev, 0x20);
747         if (AtiPcieCfg.Config & PCIE_ENABLE_STATIC_DEV_REMAP)
748                 reg &= 0xfffffffd;      /* set bit1 = 0 */
749         else
750                 reg |= 0x2;     /* set bit1 = 1 */
751         nbmisc_write_index(nb_dev, 0x20, reg);
752
753         /* Must perform PCIE-GPP1, GPP2, GPP3a global reset anyway */
754         reg = nbmisc_read_index(nb_dev, 0x8);
755         reg |= (1 << 31) | (1 << 15) | (1 << 13);       //asserts
756         nbmisc_write_index(nb_dev, 0x8, reg);
757         reg &= ~((1 << 31) | (1 << 15) | (1 << 13));    //De-aserts
758         nbmisc_write_index(nb_dev, 0x8, reg);
759
760         reg = nbmisc_read_index(nb_dev, 0x67); /* get STRAP_BIF_LINK_CONFIG at bit 0-4 */
761         if (cfg->gpp3a_configuration != (reg & 0x1F))
762                 switching_gpp3a_configurations(nb_dev, sb_dev);
763         reg = nbmisc_read_index(nb_dev, 0x8);  /* get MULTIPORT_CONFIG_GPP1 MULTIPORT_CONFIG_CONFIG_GPP2 at bit 8,9 */
764         if ((cfg->gpp1_configuration << 8) != (reg & (1 << 8)))
765                 switching_gpp1_configurations(nb_dev, sb_dev);
766         if ((cfg->gpp2_configuration << 9) != (reg & (1 << 9)))
767                 switching_gpp2_configurations(nb_dev, sb_dev);
768         ValidatePortEn(nb_dev);
769 }
770
771 /*****************************************
772 * Compliant with CIM_33's PCIEMiscClkProg
773 *****************************************/
774 void pcie_config_misc_clk(device_t nb_dev)
775 {
776         u32 reg;
777         //struct bus pbus; /* fake bus for dev0 fun1 */
778
779         reg = pci_read_config32(nb_dev, 0x4c);
780         reg |= 1 << 0;
781         pci_write_config32(nb_dev, 0x4c, reg);
782
783 #if 0                           /* TODO: Check the mics clock later. */
784         if (AtiPcieCfg.Config & PCIE_GFX_CLK_GATING) {
785                 /* TXCLK Clock Gating */
786                 set_nbmisc_enable_bits(nb_dev, 0x07, 3 << 0, 3 << 0);
787                 set_nbmisc_enable_bits(nb_dev, 0x07, 1 << 22, 1 << 22);
788                 set_pcie_enable_bits(nb_dev, 0x11 | PCIE_CORE_INDEX_GFX, (3 << 6) | (~0xf), 3 << 6);
789
790                 /* LCLK Clock Gating */
791                 reg =  pci_cf8_conf1.read32(&pbus, 0, 1, 0x94);
792                 reg &= ~(1 << 16);
793                 pci_cf8_conf1.write32(&pbus, 0, 1, 0x94, reg);
794         }
795
796         if (AtiPcieCfg.Config & PCIE_GPP_CLK_GATING) {
797                 /* TXCLK Clock Gating */
798                 set_nbmisc_enable_bits(nb_dev, 0x07, 3 << 4, 3 << 4);
799                 set_nbmisc_enable_bits(nb_dev, 0x07, 1 << 22, 1 << 22);
800                 set_pcie_enable_bits(nb_dev, 0x11 | PCIE_CORE_INDEX_SB, (3 << 6) | (~0xf), 3 << 6);
801
802                 /* LCLK Clock Gating */
803                 reg =  pci_cf8_conf1.read32(&pbus, 0, 1, 0x94);
804                 reg &= ~(1 << 24);
805                 pci_cf8_conf1.write32(&pbus, 0, 1, 0x94, reg);
806         }
807 #endif
808
809         reg = pci_read_config32(nb_dev, 0x4c);
810         reg &= ~(1 << 0);
811         pci_write_config32(nb_dev, 0x4c, reg);
812 }