eebe711cdba7f1d094ad2d39a64bd98118445c93
[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  * GEN2 Software Compliance
271  */
272 void init_gen2(device_t nb_dev, device_t dev, u8 port)
273 {
274         u32 reg, val;
275
276         /* for A11 (0x89 == 0) */
277         reg = 0x34;
278         if (port <= 3) {
279                 val = 1<<5;
280         } else {
281                 val = 1<<31;
282                 if (port >= 9)
283                         reg = 0x39;
284         }
285
286         /* TODO: check for rev > a11 */
287         switch (port) {
288                 case 2:
289                         reg = 0x34;
290                         val = 1<<5;
291                         break;
292                 case 3:
293                         reg = 0x22;
294                         val = 1<<6;
295                         break;
296                 case 4:
297                         reg = 0x34;
298                         val = 1<<31;
299                         break;
300                 case 5:
301                 case 6:
302                         reg = 0x39;
303                         val = 1<<31;
304                         break;
305                 case 7:
306                 case 8:
307                 case 9:
308                         reg = 0x37;
309                         val = 1<<port;
310                         break;
311                 case 10:
312                         reg = 0x22;
313                         val = 1<<5;
314                         break;
315                 default:
316                         reg = 0;
317                         break;
318         }
319
320         /* Enables GEN2 capability of the device */
321         set_pcie_enable_bits(dev, 0xA4, 0x1, 0x1);
322         /* Advertise the link speed to be Gen2 */
323         pci_ext_write_config32(nb_dev, dev, 0x88, 0xF0, 1<<2); /* LINK_CRTL2 */
324         set_nbmisc_enable_bits(nb_dev, reg, val, val);
325 }
326
327
328 /* Alternative to default CPL buffer count */
329 const u8 pGpp420000[] = {0x38, 0x1C};
330 const u8 pGpp411000[] = {0x38, 0x0E, 0x0E};
331 const u8 pGpp222000[] = {0x1C, 0x1C, 0x1C};
332 const u8 pGpp221100[] = {0x1C, 0x1C, 0x0E, 0x0E};
333 const u8 pGpp211110[] = {0x1C, 0x0E, 0x0E, 0x0E, 0, 0x0E, 0x0E};
334 const u8 pGpp111111[] = {0x0E, 0x0E, 0x0E, 0x0E, 0, 0x0E, 0x0E};
335
336 /*
337  * Enabling Dynamic Slave CPL Buffer Allocation Feature for PCIE-GPP3a Ports
338  * PcieLibCplBufferAllocation
339  */
340 static void gpp3a_cpl_buf_alloc(device_t nb_dev, device_t dev)
341 {
342         u8 dev_index;
343         u8 *slave_cpl;
344         u8 value;
345         struct southbridge_amd_sr5650_config *cfg =
346             (struct southbridge_amd_sr5650_config *)nb_dev->chip_info;
347
348         dev_index = dev->path.pci.devfn >> 3;
349         if (dev_index < 4 || dev_index > 0xa) {
350                 return;
351         }
352
353         switch (cfg->gpp3a_configuration) {
354         case 0x1: /* 4:2:0:0:0:0 */
355                 slave_cpl = (u8 *)&pGpp420000;
356                 break;
357         case 0x2: /* 4:1:1:0:0:0 */
358                 slave_cpl = (u8 *)&pGpp411000;
359                 break;
360         case 0xC: /* 2:2:2:0:0:0 */
361                 slave_cpl = (u8 *)&pGpp222000;
362                 break;
363         case 0xA: /* 2:2:1:1:0:0 */
364                 slave_cpl = (u8 *)&pGpp221100;
365                 break;
366         case 0x4: /* 2:1:1:1:1:0 */
367                 slave_cpl = (u8 *)&pGpp211110;
368                 break;
369         case 0xB: /* 1:1:1:1:1:1 */
370                 slave_cpl = (u8 *)&pGpp111111;
371                 break;
372         default:  /* shouldn't be here. */
373                 printk(BIOS_WARNING, "buggy gpp3a_configuration\n");
374                 return;
375         }
376
377         value = slave_cpl[dev_index - 4];
378         if (value != 0) {
379                 set_pcie_enable_bits(dev, 0x10, 0x3f << 8, value << 8);
380                 set_pcie_enable_bits(dev, 0x20, 1 << 11, 1 << 11);
381         }
382 }
383
384 /*
385  * Enabling Dynamic Slave CPL Buffer Allocation Feature for PCIE-GPP1/PCIE-GPP2 Ports
386  * PcieLibCplBufferAllocation
387  */
388 static void gpp12_cpl_buf_alloc(device_t nb_dev, device_t dev)
389 {
390         u8 gpp_cfg;
391         u8 value;
392         u8 dev_index;
393
394         dev_index = dev->path.pci.devfn >> 3;
395         struct southbridge_amd_sr5650_config *cfg =
396             (struct southbridge_amd_sr5650_config *)nb_dev->chip_info;
397
398         if (dev_index < 4) {
399                 gpp_cfg = cfg->gpp1_configuration;
400         } else if (dev_index > 0xa) {
401                 gpp_cfg = cfg->gpp2_configuration;
402         } else {
403                 return;
404         }
405
406         if (gpp_cfg == 0) {
407                 /* Configuration 16:0,  leave the default value */
408         } else if (gpp_cfg == 1) {
409                 /* Configuration 8:8 */
410                 value = 0x60;
411                 set_pcie_enable_bits(dev, 0x10, 0x3f << 8, value << 8);
412                 set_pcie_enable_bits(dev, 0x20, 1 << 11, 1 << 11);
413         } else {
414                 printk(BIOS_DEBUG, "buggy gpp configuration\n");
415         }
416 }
417
418 #if 1                           /* BTS report error without this function. But some board
419                                  * fail to boot. Leave it here for future debug. */
420
421 /*
422  * Enable LCLK clock gating
423  */
424 static void EnableLclkGating(device_t dev)
425 {
426         u8 port;
427         u32 reg = 0;
428         u32 mask = 0;
429         u32 value = 0;
430         device_t nb_dev = dev_find_slot(0, 0);
431         device_t clk_f1= dev_find_slot(0, 1);
432
433         reg = 0xE8;
434         port = dev->path.pci.devfn >> 3;
435         switch (port) {
436                 //PCIE_CORE_INDEX_GPP1
437                 case 2:
438                 case 3:
439                         reg = 0x94;
440                         mask = 1 << 16;
441                         break;
442
443                 //PCIE_CORE_INDEX_GPP2
444                 case 11:
445                 case 12:
446                         value = 1 << 28;
447                         break;
448
449                 //PCIE_CORE_INDEX_GPP3a
450                 case 4 ... 7:
451                 case 9:
452                 case 10:
453                         value = 1 << 31;
454                         break;
455
456                 //PCIE_CORE_INDEX_GPP3b;
457                 case 13:
458                         value = 1 << 25;
459                         break;
460
461                 //PCIE_CORE_INDEX_SB;
462                 case 8:
463                         reg = 0x94;
464                         mask = 1 << 24;
465                         break;
466                 default:
467                         break;
468         }
469         /* enable access func1 */
470         set_nbcfg_enable_bits(nb_dev, 0x4C, 1 << 0, 1 << 0);
471         set_nbcfg_enable_bits(clk_f1, reg, mask, value);
472 }
473 #endif
474
475 /*****************************************
476 * Compliant with CIM_33's PCIEGPPInit
477 * nb_dev:
478 *       root bridge struct
479 * dev:
480 *       p2p bridge struct
481 * port:
482 *       p2p bridge number, 4-10
483 *****************************************/
484 void sr5650_gpp_sb_init(device_t nb_dev, device_t dev, u32 port)
485 {
486         u32 gpp_sb_sel = 0;
487         struct southbridge_amd_sr5650_config *cfg =
488             (struct southbridge_amd_sr5650_config *)nb_dev->chip_info;
489
490         printk(BIOS_DEBUG, "gpp_sb_init nb_dev=0x%p, dev=0x%p, port=0x%x\n", nb_dev, dev, port);
491         switch (port) {
492         case 2:
493         case 3:
494                 gpp_sb_sel = PCIE_CORE_INDEX_GPP1;
495                 break;
496         case 4 ... 7:
497         case 9:
498         case 10:
499                 gpp_sb_sel = PCIE_CORE_INDEX_GPP3a;
500                 break;
501         case 8:
502                 gpp_sb_sel = PCIE_CORE_INDEX_SB;
503                 break;
504         case 11:
505         case 12:
506                 gpp_sb_sel = PCIE_CORE_INDEX_GPP2;
507                 break;
508         case 13:
509                 gpp_sb_sel = PCIE_CORE_INDEX_GPP3b;
510                 break;
511         }
512
513         /* Init common Core registers */
514         set_pcie_enable_bits(dev, 0xB1, 1 << 28 | 1 << 23 | 1 << 20 | 1 << 19,
515                 1 << 28 | 1 << 23 | 1 << 20 | 1 << 19);
516         if (gpp_sb_sel == PCIE_CORE_INDEX_GPP3a) {
517                 set_pcie_enable_bits(dev, 0xB1, 1 << 22, 1 << 22);
518                 /* 4.3.3.2.3 Step 10: Dynamic Slave CPL Buffer Allocation */
519                 gpp3a_cpl_buf_alloc(nb_dev, dev);
520         }
521         if (gpp_sb_sel == PCIE_CORE_INDEX_GPP1 || gpp_sb_sel == PCIE_CORE_INDEX_GPP2) {
522                 gpp12_cpl_buf_alloc(nb_dev, dev);
523         }
524         set_pcie_enable_bits(dev, 0xA1, (1 << 26) | (1 << 24) | (1 << 11), 1 << 11);
525         set_pcie_enable_bits(dev, 0xA0, 0x0000FFF0, 0x6830);
526         // PCIE should not ignore malformed packet error or ATS request
527         set_pcie_enable_bits(dev, 0x70, 1 << 12, 0);
528         //Step 14.1: Advertising Hot Plug Capabilities
529         set_pcie_enable_bits(dev, 0x10, 1 << 4, 1 << 4); //Enable power fault
530
531         set_pcie_enable_bits(nb_dev, 0xC1 | gpp_sb_sel, 1 << 0, 1 << 0);
532
533         /* init GPP core */
534         /* 4.4.2.step13.1. Sets RCB completion timeout to be 200ms */
535         pci_ext_write_config32(nb_dev, dev, 0x80, 0xF << 0, 0x6 << 0);
536         /* 4.4.2.step13.2. RCB completion timeout on link down to shorten enumeration time. */
537         set_pcie_enable_bits(dev, 0x70, 1 << 19, 1 << 19);
538         /* 4.4.2.step13.3. Enable slave ordering rules */
539         set_pcie_enable_bits(nb_dev, 0x20 | gpp_sb_sel, 1 << 8, 0 << 8);
540         /* 4.4.2.step13.4. Sets DMA payload size to 64 bytes. */
541         set_pcie_enable_bits(nb_dev, 0x10 | gpp_sb_sel, 7 << 10, 4 << 10);
542         /* 4.4.2.step13.5. Set REGS_DLP_IGNORE_IN_L1_EN to ignore DLLPs
543            during L1 so that Tx Clk can be turned off. */
544         set_pcie_enable_bits(nb_dev, 0x02 | gpp_sb_sel, 1 << 0 | 1 << 8, 1 << 0 | 1 << 8); // add bit 8 from CIMx
545         /* 4.4.2.step13.6. Set REGS_LC_ALLOW_TX_L1_CONTROL to allow TX to
546            prevent LC from going to L1 when there are outstanding completions.*/
547         set_pcie_enable_bits(dev, 0x02, 1 << 15, 1 << 15);
548
549         /* Enables the PLL power down when all lanes are inactive.
550          * It should be on in GPP.
551          */
552         if (gpp_sb_sel == PCIE_CORE_INDEX_GPP3a || gpp_sb_sel == PCIE_CORE_INDEX_GPP3b || gpp_sb_sel == PCIE_CORE_INDEX_SB) {
553                 set_pcie_enable_bits(nb_dev, 0x02 | gpp_sb_sel, 1 << 3, 1 << 3);
554         }
555
556         /* 4.4.2.step13.7. Set REGS_LC_DONT_GO_TO_L0S_IF_L1_ARMED to prevent
557            lc to go to from L0 to Rcv_L0s if L1 is armed. */
558         set_pcie_enable_bits(dev, 0xA1, 1 << 11, 1 << 11);
559         /* 4.4.2.step13.8. CMGOOD_OVERRIDE for all five PCIe cores. */
560         set_nbmisc_enable_bits(nb_dev, 0x22, 1 << 27, 1 << 27);
561         /* 4.4.2.step13.9. Prevents Electrical Idle from causing a
562            transition from Rcv_L0 to Rcv_L0s. */
563         set_pcie_enable_bits(dev, 0xB1, 1 << 20, 1 << 20);
564         /* 4.4.2.step13.10. Prevents the LTSSM from going to Rcv_L0s if
565            it has already acknowledged a request to go
566            to L1 but it has not transitioned there yet. */
567         /* seems the same as step13.7 */
568         set_pcie_enable_bits(dev, 0xA1, 1 << 11, 1 << 11);
569         /* 4.4.2.step13.11. Transmits FTS before Recovery. */
570         set_pcie_enable_bits(dev, 0xA3, 1 << 9, 1 << 9);
571         /* 4.4.2.step13.12. Sets TX arbitration algorithm to round robin
572            for PCIE-GPP1, PCIE-GPP2, PCIE-GPP3a and PCIE-GPP3b cores only. */
573         //if (gpp_sb_sel != PCIE_CORE_INDEX_SB) /* RPR NOT set SB_CORE, BTS set SB_CORE, we comply with BTS */
574                 set_pcie_enable_bits(nb_dev, 0x1C | gpp_sb_sel, 0x7FF, 0x109);
575         /* 4.4.2.step13.13. Sets number of TX Clocks to drain TX Pipe to 0x3.*/
576         set_pcie_enable_bits(dev, 0xA0, 0xF << 4, 0x3 << 4);
577         /* 4.4.2.step13.14. Lets PI use Electrical Idle from PHY when
578            turning off PLL in L1 at Gen 2 speed instead of Inferred Electrical
579            Idle.
580            NOTE: LC still uses Inferred Electrical Idle. */
581         set_pcie_enable_bits(nb_dev, 0x40 | gpp_sb_sel, 3 << 14, 2 << 14);
582         /* 4.4.2.step13.15. Turn on rx_fronten_en for all active lanes upon
583            exit from Electrical Idle, rather than being tied to PLL_PDNB. */
584         set_pcie_enable_bits(nb_dev, 0xC2 | gpp_sb_sel, 1 << 25, 1 << 25);
585
586         /* 4.4.2.step13.16. Advertises TX L0s and L1 exit latency.
587            TX L0s exit latency to be 100b: 512ns to less than 1us;
588            L1 exit latency to be 011b: 4us to less than 8us.
589            For Hot-Plug Slots: Advertise TX L0s and L1 exit latency.
590            TX L0s exit latency to be 110b: 2us to 4us.
591            L1 exit latency to be 111b: more than 64us.*/
592         //set_pcie_enable_bits(dev, 0xC1, 0xF << 0, 0xC << 0); /* 0xF for htplg. */
593         set_pcie_enable_bits(dev, 0xC1, 0xF << 0, 0xF << 0); /* 0xF for htplg. */
594         /* 4.4.2.step13.17. Always ACK an ASPM L1 entry DLLP to
595            workaround credit control issue on PM_NAK
596            message of SB700 and SB800. */
597         /* 4.4.4.step13.18. To allow advertising Gen 2 capabilities to Southbridge. */
598         if (port == 8) {
599                 set_pcie_enable_bits(dev, 0xA0, 1 << 23, 1 << 23);
600                 set_pcie_enable_bits(nb_dev, 0xC1 | gpp_sb_sel, 1 << 1, 1 << 1);
601         }
602         /* 4.4.2.step13.19. CMOS Option (Gen 2 AUTO-Part 1 - Enabled by Default) */
603         /* 4.4.2.step13.20. CMOS Option (RC Advertised Gen 2-Part1 - Disabled by Default)*/
604         set_nbcfg_enable_bits(dev, 0x88, 0xF << 0, 0x2 << 0);
605         /* Disables GEN2 capability of the device.
606          * RPR typo- it says enable but the bit setting says disable.
607          * Disable it here and we enable it later. */
608         set_pcie_enable_bits(dev, 0xA4, 1 << 0, 1 << 0);
609
610         /* 4.4.2.step13.21. Legacy Hot Plug  -CMOS Option */
611         /* NOTE: This feature can be enabled only for Hot-Plug slots implemented on SR5690 platform. */
612
613         /* 4.4.2.step13.22. Native PCIe Mode -CMOS Option */
614         /* Enable native PME. */
615         set_pcie_enable_bits(dev, 0x10, 1 << 3, 1 < 3);
616         /* This bit when set indicates that the PCIe Link associated with this port
617            is connected to a slot. */
618         pci_ext_write_config32(nb_dev, dev, 0x5a, 1 << 8, 1 << 8);
619         /* This bit when set indicates that this slot is capable of supporting
620            Hot-Plug operations. */
621         set_nbcfg_enable_bits(dev, 0x6C, 1 << 6, 1 << 6);
622         /* Enables flushing of TLPs when Data Link is down. */
623         set_pcie_enable_bits(dev, 0x20, 1 << 19, 0 << 19);
624
625         /* 4.4.2.step14. Server Class Hot Plug Feature. NOTE: This feature is not supported on SR5670 and SR5650 */
626         /* 4.4.2 step14.1: Advertising Hot Plug Capabilities */
627         /* 4.4.2.step14.2: Firmware Upload */
628         /* 4.4.2.Step14.3: SBIOS Acknowledgment to Firmware of Successful Firmware Upload */
629         /* step14.4 */
630         /* step14.5 */
631         /* skip */
632
633         /* CIMx LPC Deadlock workaround - Enable Memory Write Map*/
634         if (gpp_sb_sel == PCIE_CORE_INDEX_SB) {
635                 set_pcie_enable_bits(nb_dev, 0x10 | gpp_sb_sel, 1 << 9, 1 << 9);
636                 set_htiu_enable_bits(nb_dev, 0x06, 1 << 26, 1 << 26);
637         }
638
639         /* This CPL setup requires more than this one register and should be done in gpp_core.
640          * The additional setup is for the different revisions. */
641
642         /* CIMx CommonPortInit settings that are not set above. */
643         pci_ext_write_config32(nb_dev, dev, 0x88, 0xF0, 1 << 0); /* LINK_CRTL2 */
644
645         if ( port == 8 )
646                 set_pcie_enable_bits(dev, 0xA0, 0, 1 << 23);
647
648 #if 0 //SR56x0 pcie Gen2 code is not tested yet, we should enable it again when test finished.
649         /* set automatic Gen2 support, needs mainboard config option as Gen2 can cause issues on some platforms. */
650         init_gen2(nb_dev, dev, port);
651         set_pcie_enable_bits(dev, 0xA4, 1 << 29, 1 << 29);
652         set_pcie_enable_bits(dev, 0xC0, 1 << 15, 0);
653         set_pcie_enable_bits(dev, 0xA2, 1 << 13, 0);
654 #endif
655
656         /* Hotplug Support - bit5 + bit6  capable and surprise */
657         pci_ext_write_config32(nb_dev, dev, 0x6c, 0x60, 0x60);
658
659         /* Set interrupt pin info 0x3d */
660         pci_ext_write_config32(nb_dev, dev, 0x3c, 1 << 8, 1 << 8);
661
662         /* 5.12.9.3 Hotplug step 1 - NB_PCIE_ROOT_CTRL - enable pm irq
663         The RPR is wrong - this is not a PCIEND_P register */
664         pci_ext_write_config32(nb_dev, dev, 0x74, 1 << 3, 1 << 3);
665
666         /* 5.12.9.3 step 2 - PCIEP_PORT_CNTL - enable hotplug messages */
667         if ( port != 8)
668                 set_pcie_enable_bits(dev, 0x10, 1 << 2, 1 << 2);
669
670         /* Not sure about this PME setup */
671         /* Native PME */
672         set_pcie_enable_bits(dev, 0x10, 1 << 3, 1 << 3); /* Not set in CIMx */
673
674         /* PME Enable */
675         pci_ext_write_config32(nb_dev, dev, 0x54, 1 << 8, 1 << 8); /* Not in CIMx */
676
677         /* 4.4.3 Training for GPP devices */
678         /* init GPP */
679         switch (port) {
680         case 2:
681         case 3:
682         case 4: /* GPP_SB */
683         case 5:
684         case 6:
685         case 7:
686         case 9: /*GPP*/
687         case 10:
688         case 11:
689         case 12:
690         case 13:
691                 /* 4.4.2.step13.5. Blocks DMA traffic during C3 state */
692                 set_pcie_enable_bits(dev, 0x10, 1 << 0, 0 << 0);
693                 /* Enabels TLP flushing */
694                 set_pcie_enable_bits(dev, 0x20, 1 << 19, 0 << 19);
695
696                 /* check port enable */
697                 if (cfg->port_enable & (1 << port)) {
698                         PcieReleasePortTraining(nb_dev, dev, port);
699                         if (!(AtiPcieCfg.Config & PCIE_GPP_COMPLIANCE)) {
700                                 u8 res = PcieTrainPort(nb_dev, dev, port);
701                                 printk(BIOS_DEBUG, "PcieTrainPort port=0x%x result=%d\n", port, res);
702                                 if (res) {
703                                         AtiPcieCfg.PortDetect |= 1 << port;
704                                 }
705                         }
706                 }
707                 break;
708         case 8:         /* SB */
709                 break;
710         default:
711                 break;
712         }
713
714         /* Re-enable RC ordering logic after training (from CIMx)*/
715         set_pcie_enable_bits(nb_dev, 0x20 | gpp_sb_sel, 1 << 9, 0);
716
717         /* Advertising Hot Plug Capabilities */
718         pci_ext_write_config32(nb_dev, dev, 0x6c, 0x04001B, 0x00001B);
719
720         /* PCIE Late Init (CIMx late init - Maybe move somewhere else? Later in the coreboot PCI device enum?) */
721         /* Set Slot Number */
722         pci_ext_write_config32(nb_dev, dev, 0x6c, 0x1FFF << 19, port << 19);
723
724         /* Set Slot present 0x5A*/
725         pci_ext_write_config32(nb_dev, dev, 0x58, 1 << 24, 1 << 24);
726
727         //PCIE-GPP1 TXCLK Clock Gating In L1  Late Core sttting - Maybe move somewhere else? */
728         set_pcie_enable_bits(nb_dev, 0x11 | gpp_sb_sel, 0xF << 0, 0x0C << 0);
729         /* Enable powering down PLLs in L1 or L23 Ready states.
730          * Turns off PHY`s RX FRONTEND during L1 when PLL power down is enabled */
731         set_pcie_enable_bits(nb_dev, 0x40 | gpp_sb_sel, 0x1219, 0x1009);
732         /* 4.4..7.1 TXCLK Gating in L1, Enables powering down TXCLK clock pads on the receive side. */
733         set_pcie_enable_bits(nb_dev, 0x40 | gpp_sb_sel, 1 << 6, 1 << 6);
734
735         /* Step 20: Disables immediate RCB timeout on link down */
736         if (!((pci_read_config32(dev, 0x6C ) >> 6) & 0x01)) {
737                 set_pcie_enable_bits(dev, 0x70, 1 << 19, 0 << 19);
738         }
739
740         /* Step 27: LCLK Gating */
741         EnableLclkGating(dev);
742
743         /* Set Common Clock */
744         /* If dev present, set PcieCapPtr+0x10, BIT6);
745          * set dev 0x68,bit 6
746          * retrain link, set dev, 0x68 bit 5;
747          * wait dev 0x6B bit3 clear
748          */
749
750         if (port == 8){
751                 PciePowerOffGppPorts(nb_dev, dev, port); /* , This should be run for all ports that are not hotplug and don't detect devices */
752         }
753 }
754
755 /**
756  * Step 21: Register Locking
757  * Lock HWInit Register of each pcie core
758  */
759 static void lock_hwinitreg(device_t nb_dev)
760 {
761         /* Step 21: Register Locking, Lock HWInit Register */
762         set_pcie_enable_bits(nb_dev, 0x10 | PCIE_CORE_INDEX_GPP1, 1 << 0, 1 << 0);
763         set_pcie_enable_bits(nb_dev, 0x10 | PCIE_CORE_INDEX_SB, 1 << 0, 1 << 0);
764         set_pcie_enable_bits(nb_dev, 0x10 | PCIE_CORE_INDEX_GPP2, 1 << 0, 1 << 0);
765         set_pcie_enable_bits(nb_dev, 0x10 | PCIE_CORE_INDEX_GPP3a, 1 << 0, 1 << 0);
766         set_pcie_enable_bits(nb_dev, 0x10 | PCIE_CORE_INDEX_GPP3b, 1 << 0, 1 << 0);
767 }
768
769 /**
770  * Lock HWInit Register
771  */
772 void sr56x0_lock_hwinitreg(void)
773 {
774         device_t nb_dev = dev_find_slot(0, PCI_DEVFN(0, 0));
775
776         /* Lock HWInit Register */
777         lock_hwinitreg(nb_dev);
778
779         /* Lock HWInit Register NBMISCIND:0x0 NBCNTL[7] HWINIT_WR_LOCK */
780         set_nbmisc_enable_bits(nb_dev, 0x00, 1 << 7, 1 << 7);
781 }
782
783 /*****************************************
784 * Compliant with CIM_33's PCIEConfigureGPPCore
785 *****************************************/
786 void config_gpp_core(device_t nb_dev, device_t sb_dev)
787 {
788         u32 reg;
789         struct southbridge_amd_sr5650_config *cfg =
790                 (struct southbridge_amd_sr5650_config *)nb_dev->chip_info;
791
792         reg = nbmisc_read_index(nb_dev, 0x20);
793         if (AtiPcieCfg.Config & PCIE_ENABLE_STATIC_DEV_REMAP)
794                 reg &= 0xfffffffd;      /* set bit1 = 0 */
795         else
796                 reg |= 0x2;     /* set bit1 = 1 */
797         nbmisc_write_index(nb_dev, 0x20, reg);
798
799         /* Must perform PCIE-GPP1, GPP2, GPP3a global reset anyway */
800         reg = nbmisc_read_index(nb_dev, 0x8);
801         reg |= (1 << 31) | (1 << 15) | (1 << 13);       //asserts
802         nbmisc_write_index(nb_dev, 0x8, reg);
803         reg &= ~((1 << 31) | (1 << 15) | (1 << 13));    //De-aserts
804         nbmisc_write_index(nb_dev, 0x8, reg);
805
806         reg = nbmisc_read_index(nb_dev, 0x67); /* get STRAP_BIF_LINK_CONFIG at bit 0-4 */
807         if (cfg->gpp3a_configuration != (reg & 0x1F))
808                 switching_gpp3a_configurations(nb_dev, sb_dev);
809         reg = nbmisc_read_index(nb_dev, 0x8);  /* get MULTIPORT_CONFIG_GPP1 MULTIPORT_CONFIG_CONFIG_GPP2 at bit 8,9 */
810         if ((cfg->gpp1_configuration << 8) != (reg & (1 << 8)))
811                 switching_gpp1_configurations(nb_dev, sb_dev);
812         if ((cfg->gpp2_configuration << 9) != (reg & (1 << 9)))
813                 switching_gpp2_configurations(nb_dev, sb_dev);
814         ValidatePortEn(nb_dev);
815 }
816
817 /*****************************************
818 * Compliant with CIM_33's PCIEMiscClkProg
819 *****************************************/
820 void pcie_config_misc_clk(device_t nb_dev)
821 {
822         u32 reg;
823         //struct bus pbus; /* fake bus for dev0 fun1 */
824
825         reg = pci_read_config32(nb_dev, 0x4c);
826         reg |= 1 << 0;
827         pci_write_config32(nb_dev, 0x4c, reg);
828
829 #if 0                           /* TODO: Check the mics clock later. */
830         if (AtiPcieCfg.Config & PCIE_GFX_CLK_GATING) {
831                 /* TXCLK Clock Gating */
832                 set_nbmisc_enable_bits(nb_dev, 0x07, 3 << 0, 3 << 0);
833                 set_nbmisc_enable_bits(nb_dev, 0x07, 1 << 22, 1 << 22);
834                 set_pcie_enable_bits(nb_dev, 0x11 | PCIE_CORE_INDEX_GFX, (3 << 6) | (~0xf), 3 << 6);
835
836                 /* LCLK Clock Gating */
837                 reg =  pci_cf8_conf1.read32(&pbus, 0, 1, 0x94);
838                 reg &= ~(1 << 16);
839                 pci_cf8_conf1.write32(&pbus, 0, 1, 0x94, reg);
840         }
841
842         if (AtiPcieCfg.Config & PCIE_GPP_CLK_GATING) {
843                 /* TXCLK Clock Gating */
844                 set_nbmisc_enable_bits(nb_dev, 0x07, 3 << 4, 3 << 4);
845                 set_nbmisc_enable_bits(nb_dev, 0x07, 1 << 22, 1 << 22);
846                 set_pcie_enable_bits(nb_dev, 0x11 | PCIE_CORE_INDEX_SB, (3 << 6) | (~0xf), 3 << 6);
847
848                 /* LCLK Clock Gating */
849                 reg =  pci_cf8_conf1.read32(&pbus, 0, 1, 0x94);
850                 reg &= ~(1 << 24);
851                 pci_cf8_conf1.write32(&pbus, 0, 1, 0x94, reg);
852         }
853 #endif
854
855         reg = pci_read_config32(nb_dev, 0x4c);
856         reg &= ~(1 << 0);
857         pci_write_config32(nb_dev, 0x4c, reg);
858 }