5e2d9851303f169dec4c32d4f300c350e98af840
[coreboot.git] / src / southbridge / amd / rs780 / 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 "rs780.h"
27
28 /*------------------------------------------------
29 * Global variable
30 ------------------------------------------------*/
31 PCIE_CFG AtiPcieCfg = {
32         PCIE_ENABLE_STATIC_DEV_REMAP,   /* Config */
33         0,                      /* ResetReleaseDelay */
34         0,                      /* Gfx0Width */
35         0,                      /* Gfx1Width */
36         0,                      /* GfxPayload */
37         0,                      /* GppPayload */
38         0,                      /* PortDetect, filled by GppSbInit */
39         0,                      /* PortHp */
40         0,                      /* DbgConfig */
41         0,                      /* DbgConfig2 */
42         0,                      /* GfxLx */
43         0,                      /* GppLx */
44         0,                      /* NBSBLx */
45         0,                      /* PortSlotInit */
46         0,                      /* Gfx0Pwr */
47         0,                      /* Gfx1Pwr */
48         0                       /* GppPwr */
49 };
50
51 static void PciePowerOffGppPorts(device_t nb_dev, device_t dev, u32 port);
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_rs780_config *cfg =
67             (struct southbridge_amd_rs780_config *)nb_dev->chip_info;
68         u8 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_gppsb_configurations(device_t nb_dev, device_t sb_dev)
107 {
108         u32 reg;
109         struct southbridge_amd_rs780_config *cfg =
110             (struct southbridge_amd_rs780_config *)nb_dev->chip_info;
111
112         /* 5.5.7.1-3 enables GPP reconfiguration */
113         reg = nbmisc_read_index(nb_dev, PCIE_NBCFG_REG7);
114         reg |=
115             (RECONFIG_GPPSB_EN + RECONFIG_GPPSB_LINK_CONFIG +
116              RECONFIG_GPPSB_ATOMIC_RESET);
117         nbmisc_write_index(nb_dev, PCIE_NBCFG_REG7, reg);
118
119         /* 5.5.7.4a. De-asserts STRAP_BIF_all_valid for PCIE-GPPSB core */
120         reg = nbmisc_read_index(nb_dev, 0x66);
121         reg |= 1 << 31;
122         nbmisc_write_index(nb_dev, 0x66, reg);
123         /* 5.5.7.4b. sets desired GPPSB configurations, bit4-7 */
124         reg = nbmisc_read_index(nb_dev, 0x67);
125         reg &= 0xFFFFff0f;              /* clean */
126         reg |= cfg->gppsb_configuration << 4;
127         nbmisc_write_index(nb_dev, 0x67, reg);
128
129 #if 1
130         /* NOTE:
131          * In CIMx 4.5.0 and RPR, 4c is done before 5 & 6. But in this way,
132          * a x4 device in port B (dev 4) of Configuration B can only be detected
133          * as x1, instead of x4. When the port B is being trained, the
134          * LC_CURRENT_STATE is 6 and the LC_LINK_WIDTH_RD is 1. We have
135          * to set the PCIEIND:0x65 as 0xE0E0 and reset the slot. Then the card
136          * seems to work in x1 mode.
137          * In the 2nd way below, we do the 5 & 6 before 4c. it conforms the
138          * CIMx 4.3.0. It conflicts with RPR. But based on the test result I've
139          * made so far, I haven't found any mistake.
140          */
141         /* 5.5.7.4c. Asserts STRAP_BIF_all_valid for PCIE-GPPSB core */
142         reg = nbmisc_read_index(nb_dev, 0x66);
143         reg &= ~(1 << 31);
144         nbmisc_write_index(nb_dev, 0x66, reg);
145
146         /* 5.5.7.5-6. read bit14 and write back its inverst value */
147         reg = nbmisc_read_index(nb_dev, PCIE_NBCFG_REG7);
148         reg ^= RECONFIG_GPPSB_GPPSB;
149         nbmisc_write_index(nb_dev, PCIE_NBCFG_REG7, reg);
150 #else
151         /* 5.5.7.5-6. read bit14 and write back its inverst value */
152         reg = nbmisc_read_index(nb_dev, PCIE_NBCFG_REG7);
153         reg ^= RECONFIG_GPPSB_GPPSB;
154         nbmisc_write_index(nb_dev, PCIE_NBCFG_REG7, reg);
155
156         /* 5.5.7.4c. Asserts STRAP_BIF_all_valid for PCIE-GPPSB core */
157         reg = nbmisc_read_index(nb_dev, 0x66);
158         reg &= ~(1 << 31);
159         nbmisc_write_index(nb_dev, 0x66, reg);
160 #endif
161         /* 5.5.7.7. delay 1ms */
162         mdelay(1);
163
164         /* 5.5.7.8. waits until SB has trained to L0, poll for bit0-5 = 0x10 */
165         do {
166                 reg = nbpcie_p_read_index(sb_dev, PCIE_LC_STATE0);
167                 reg &= 0x3f;    /* remain LSB [5:0] bits */
168         } while (LC_STATE_RECONFIG_GPPSB != reg);
169
170         /* 5.5.7.9.ensures that virtual channel negotiation is completed. poll for bit1 = 0 */
171         do {
172                 reg =
173                     pci_ext_read_config32(nb_dev, sb_dev,
174                                           PCIE_VC0_RESOURCE_STATUS);
175         } while (reg & VC_NEGOTIATION_PENDING);
176 }
177
178 static void switching_gpp_configurations(device_t nb_dev, device_t sb_dev)
179 {
180         u32 reg;
181         struct southbridge_amd_rs780_config *cfg =
182             (struct southbridge_amd_rs780_config *)nb_dev->chip_info;
183
184         /* 5.6.2.1. De-asserts STRAP_BIF_all_valid for PCIE-GPP core */
185         reg = nbmisc_read_index(nb_dev, 0x22);
186         reg |= 1 << 14;
187         nbmisc_write_index(nb_dev, 0x22, reg);
188         /* 5.6.2.2. sets desired GPPSB configurations, bit4-7 */
189         reg = nbmisc_read_index(nb_dev, 0x2D);
190         reg &= ~(0xF << 7);             /* clean */
191         reg |= cfg->gpp_configuration << 7;
192         nbmisc_write_index(nb_dev, 0x2D, reg);
193         /* 5.6.2.3. Asserts STRAP_BIF_all_valid for PCIE-GPP core */
194         reg = nbmisc_read_index(nb_dev, 0x22);
195         reg &= ~(1 << 14);
196         nbmisc_write_index(nb_dev, 0x22, reg);
197 }
198
199 /*****************************************************************
200 * The rs780 uses NBCONFIG:0x1c (BAR3) to map the PCIE Extended Configuration
201 * Space to a 256MB range within the first 4GB of addressable memory.
202 *****************************************************************/
203 void enable_pcie_bar3(device_t nb_dev)
204 {
205         printk(BIOS_DEBUG, "enable_pcie_bar3()\n");
206         set_nbcfg_enable_bits(nb_dev, 0x7C, 1 << 30, 1 << 30);  /* Enables writes to the BAR3 register. */
207         set_nbcfg_enable_bits(nb_dev, 0x84, 7 << 16, 0 << 16);
208
209         pci_write_config32(nb_dev, 0x1C, EXT_CONF_BASE_ADDRESS);        /* PCIEMiscInit */
210         pci_write_config32(nb_dev, 0x20, 0x00000000);
211         set_htiu_enable_bits(nb_dev, 0x32, 1 << 28, 1 << 28);   /* PCIEMiscInit */
212         ProgK8TempMmioBase(1, EXT_CONF_BASE_ADDRESS, TEMP_MMIO_BASE_ADDRESS);
213 }
214
215 /*****************************************************************
216 * We should disable bar3 when we want to exit rs780_enable, because bar3 will be
217 * remapped in set_resource later.
218 *****************************************************************/
219 void disable_pcie_bar3(device_t nb_dev)
220 {
221         printk(BIOS_DEBUG, "disable_pcie_bar3()\n");
222         pci_write_config32(nb_dev, 0x1C, 0);    /* clear BAR3 address */
223         set_nbcfg_enable_bits(nb_dev, 0x7C, 1 << 30, 0 << 30);  /* Disable writes to the BAR3. */
224         set_htiu_enable_bits(nb_dev, 0x32, 1 << 28, 0); /* disable bar3 decode */
225         ProgK8TempMmioBase(0, EXT_CONF_BASE_ADDRESS, TEMP_MMIO_BASE_ADDRESS);
226 }
227
228 /*****************************************
229 * Compliant with CIM_33's PCIEGPPInit
230 * nb_dev:
231 *       root bridge struct
232 * dev:
233 *       p2p bridge struct
234 * port:
235 *       p2p bridge number, 4-10
236 *****************************************/
237 void rs780_gpp_sb_init(device_t nb_dev, device_t dev, u32 port)
238 {
239         u32 gfx_gpp_sb_sel;
240         struct southbridge_amd_rs780_config *cfg =
241             (struct southbridge_amd_rs780_config *)nb_dev->chip_info;
242         printk(BIOS_DEBUG, "gpp_sb_init nb_dev=0x%x, dev=0x%x, port=0x%x\n", nb_dev->path.pci.devfn, dev->path.pci.devfn, port);
243
244         gfx_gpp_sb_sel = port >= 4 && port <= 8 ?
245                                 PCIE_CORE_INDEX_GPPSB :         /* 4,5,6,7,8 */
246                                 PCIE_CORE_INDEX_GPP;            /* 9,10 */
247         /* init GPP core */
248         /* 5.10.8.3. Disable slave ordering logic */
249         set_pcie_enable_bits(nb_dev, 0x20 | gfx_gpp_sb_sel, 1 << 8,
250                              1 << 8);
251         /* 5.10.8.7. PCIE initialization 5.10.2: rpr 2.12*/
252         set_pcie_enable_bits(nb_dev, 0x02 | gfx_gpp_sb_sel, 1 << 0, 1 << 0);    /* no description in datasheet. */
253
254         /* init GPPSB port. rpr 5.10.8 */
255         /* 5.10.8.1-5.10.8.2. Sets RCB timeout to be 100ms/4=25ms by setting bits[18:16] to 3 h4
256          * and shortens the enumeration timer by setting bit[19] to 1
257          */
258         set_pcie_enable_bits(dev, 0x70, 0xF << 16, 0x4 << 16 | 1 << 19);
259         /* 5.10.8.4. Sets DMA payload size to 64 bytes. */
260         set_pcie_enable_bits(nb_dev, 0x10 | gfx_gpp_sb_sel, 7 << 10, 4 << 10);
261         /* 5.10.8.6. Disable RC ordering logic */
262         set_pcie_enable_bits(nb_dev, 0x20 | gfx_gpp_sb_sel, 1 << 9, 1 << 9);
263         /* 5.10.8.7. Ignores DLLs druing L1 */
264         set_pcie_enable_bits(nb_dev, 0x02 | gfx_gpp_sb_sel, 1 << 0, 1 << 0);
265         /* 5.10.8.8. Prevents LCto go from L0 to Rcv_L0s if L1 is armed. */
266         set_pcie_enable_bits(dev, 0xA1, 1 << 11, 1 << 11);
267         /* 5.10.8.9. Sets timer in Config state from 20us to 1us.
268          * 5.10.8.10. De-asserts RX_EN in L0s
269          * 5.10.8.11. Enables de-assertion of PG2RX_CR_EN to lock clock recovery parameter when .. */
270         set_pcie_enable_bits(dev, 0xB1, 1 << 23 | 1 << 19 | 1 << 28, 1 <<23 | 1 << 19 | 1 << 28);
271         /* 5.10.8.12. Turns off offset calibration */
272         /* 5.10.8.13. Enables Rx  Clock gating in CDR */
273         if (gfx_gpp_sb_sel == PCIE_CORE_INDEX_GPPSB)
274                 set_nbmisc_enable_bits(nb_dev, 0x67, 1 << 14 | 1 << 26, 1 << 14 | 1 << 26); /* 4,5,6,7 */
275         else
276                 set_nbmisc_enable_bits(nb_dev, 0x24, 1 << 29 | 1 << 28, 1 << 29 | 1 << 28); /* 9,10 */
277         /* 5.10.8.14. Sets number of TX Clocks to drain TX Pipe to 3 */
278         set_pcie_enable_bits(dev, 0xA0, 0xF << 4, 0x3 << 4);
279         /* 5.10.8.15. empty */
280         /* 5.10.8.16. P_ELEC_IDLE_MODE */
281         set_pcie_enable_bits(nb_dev, 0x40 | gfx_gpp_sb_sel, 0x3 << 14, 0x2 << 14);
282         /* 5.10.8.17. LC_BLOCK_EL_IDLE_IN_L0 */
283         set_pcie_enable_bits(dev, 0xB1, 1 << 20, 1 << 20);
284         /* 5.10.8.18. LC_DONT_GO_TO_L0S_IFL1_ARMED */
285         set_pcie_enable_bits(dev, 0xA1, 1 << 11, 1 << 11);
286         /* 5.10.8.19. RXP_REALIGN_ON_EACH_TSX_OR_SKP */
287         set_pcie_enable_bits(nb_dev, 0x40 | gfx_gpp_sb_sel, 1 << 28, 0 << 28);
288         /* 5.10.8.20. Bypass lane de-skew logic if in x1 */
289         set_pcie_enable_bits(nb_dev, 0xC2 | gfx_gpp_sb_sel, 1 << 14, 1 << 14);
290         /* 5.10.8.21. sets electrical idle threshold. */
291         if (gfx_gpp_sb_sel == PCIE_CORE_INDEX_GPPSB)
292                 set_nbmisc_enable_bits(nb_dev, 0x6A, 3 << 22, 2 << 22);
293         else
294                 set_nbmisc_enable_bits(nb_dev, 0x24, 3 << 16, 2 << 16);
295
296         /* 5.10.8.22. Disable GEN2 */
297         /* TODO: should be 2 seperated cases. */
298         set_nbmisc_enable_bits(nb_dev, 0x39, 1 << 31, 0 << 31);
299         set_nbmisc_enable_bits(nb_dev, 0x22, 1 << 5, 0 << 5);
300         set_nbmisc_enable_bits(nb_dev, 0x34, 1 << 31, 0 << 31);
301         set_nbmisc_enable_bits(nb_dev, 0x37, 7 << 5, 0 << 5);
302         /* 5.10.8.23. Disables GEN2 capability of the device. RPR says enable? No! */
303         set_pcie_enable_bits(dev, 0xA4, 1 << 0, 0 << 0);
304         /* 5.10.8.24. Disable advertising upconfigure support. */
305         set_pcie_enable_bits(dev, 0xA2, 1 << 13, 1 << 13);
306         /* 5.10.8.25-26. STRAP_BIF_DSN_EN */
307         if (gfx_gpp_sb_sel == PCIE_CORE_INDEX_GPPSB)
308                 set_nbmisc_enable_bits(nb_dev, 0x68, 1 << 19, 0 << 19);
309         else
310                 set_nbmisc_enable_bits(nb_dev, 0x22, 1 << 3, 0 << 3);
311         /* 5.10.8.27-28. */
312         set_pcie_enable_bits(nb_dev, 0xC1 | gfx_gpp_sb_sel, 1 << 0 | 1 << 2, 1 << 0 | 0 << 2);
313         /* 5.10.8.29. Uses the bif_core de-emphasis strength by default. */
314         if (gfx_gpp_sb_sel == PCIE_CORE_INDEX_GPPSB) {
315                 set_nbmisc_enable_bits(nb_dev, 0x67, 1 << 10, 1 << 10);
316                 set_nbmisc_enable_bits(nb_dev, 0x36, 1 << 29, 1 << 29);
317         }
318         else {
319                 set_nbmisc_enable_bits(nb_dev, 0x39, 1 << 30, 1 << 30);
320         }
321         /* 5.10.8.30. Set TX arbitration algorithm to round robin. */
322         set_pcie_enable_bits(nb_dev, 0x1C | gfx_gpp_sb_sel,
323                              1 << 0 | 0x1F << 1 | 0x1F << 6,
324                              1 << 0 | 0x04 << 1 | 0x04 << 6);
325
326         /* check compliance rpr step 2.1*/
327         if (AtiPcieCfg.Config & PCIE_GPP_COMPLIANCE) {
328                 u32 tmp;
329                 tmp = nbmisc_read_index(nb_dev, 0x67);
330                 tmp |= 1 << 3;
331                 nbmisc_write_index(nb_dev, 0x67, tmp);
332         }
333
334         /* step 5: dynamic slave CPL buffer allocation. Disable it, otherwise linux hangs. Why? */
335         /* set_pcie_enable_bits(nb_dev, 0x20 | gfx_gpp_sb_sel, 1 << 11, 1 << 11); */
336
337         /* step 5a: Training for GPP devices */
338         /* init GPP */
339         switch (port) {
340         case 4:         /* GPP */
341         case 5:
342         case 6:
343         case 7:
344         case 9:
345         case 10:
346                 /* 5.10.8.5. Blocks DMA traffic during C3 state */
347                 set_pcie_enable_bits(dev, 0x10, 1 << 0, 0 << 0);
348                 /* Enabels TLP flushing */
349                 set_pcie_enable_bits(dev, 0x20, 1 << 19, 0 << 19);
350
351                 /* check port enable */
352                 if (cfg->port_enable & (1 << port)) {
353                         PcieReleasePortTraining(nb_dev, dev, port);
354                         if (!(AtiPcieCfg.Config & PCIE_GPP_COMPLIANCE)) {
355                                 u8 res = PcieTrainPort(nb_dev, dev, port);
356                                 printk(BIOS_DEBUG, "PcieTrainPort port=0x%x result=%d\n", port, res);
357                                 if (res) {
358                                         AtiPcieCfg.PortDetect |= 1 << port;
359                                 }
360                         }
361                 }
362                 break;
363         case 8:         /* SB */
364                 break;
365         }
366         PciePowerOffGppPorts(nb_dev, dev, port);
367 }
368
369 /*****************************************
370 * Compliant with CIM_33's PCIEConfigureGPPCore
371 *****************************************/
372 void config_gpp_core(device_t nb_dev, device_t sb_dev)
373 {
374         u32 reg;
375         struct southbridge_amd_rs780_config *cfg =
376             (struct southbridge_amd_rs780_config *)nb_dev->chip_info;
377
378         reg = nbmisc_read_index(nb_dev, 0x20);
379         if (AtiPcieCfg.Config & PCIE_ENABLE_STATIC_DEV_REMAP)
380                 reg &= 0xfffffffd;      /* set bit1 = 0 */
381         else
382                 reg |= 0x2;     /* set bit1 = 1 */
383         nbmisc_write_index(nb_dev, 0x20, reg);
384
385         reg = nbmisc_read_index(nb_dev, 0x67);  /* get STRAP_BIF_LINK_CONFIG_GPPSB at bit 4-7 */
386         if (cfg->gppsb_configuration != ((reg >> 4) & 0xf))
387                 switching_gppsb_configurations(nb_dev, sb_dev);
388         reg = nbmisc_read_index(nb_dev, 0x2D);  /* get STRAP_BIF_LINK_CONFIG_GPP at bit 7-10 */
389         if (cfg->gpp_configuration != ((reg >> 7) & 0xf))
390                 switching_gpp_configurations(nb_dev, sb_dev);
391         ValidatePortEn(nb_dev);
392 }
393
394 /**
395  * Hide unused Gpp port
396  */
397 void pcie_hide_unused_ports(device_t nb_dev)
398 {
399         u16 hide = 0x6FC; /* skip port 0, 1, 8 */
400
401         hide &= ~(AtiPcieCfg.PortDetect | AtiPcieCfg.PortHp);
402         printk(BIOS_INFO, "rs780 unused GPP ports bitmap=0x%03x, force disabled\n", hide);
403         set_nbmisc_enable_bits(nb_dev, 0x0C, 0xFC, (hide & 0xFC)); /* bridge 2-7 */
404         set_nbmisc_enable_bits(nb_dev, 0x0C, 0x30000, ((hide >> 9) & 0x3) << 16); /* bridge 9-a */
405 }