I just went on a bugfix frenzy and fixed all printk format warnings
[coreboot.git] / src / southbridge / amd / rs690 / rs690_pcie.c
1 /*
2  * This file is part of the coreboot project.
3  *
4  * Copyright (C) 2008 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 "rs690.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 /*****************************************************************
60 * Compliant with CIM_33's PCIEPowerOffGppPorts
61 * Power off unused GPP lines
62 *****************************************************************/
63 static void PciePowerOffGppPorts(device_t nb_dev, device_t dev, u32 port)
64 {
65         u32 reg;
66         u16 state_save;
67         struct southbridge_amd_rs690_config *cfg =
68             (struct southbridge_amd_rs690_config *)nb_dev->chip_info;
69         u8 state = cfg->port_enable;
70
71         if (!(AtiPcieCfg.Config & PCIE_DISABLE_HIDE_UNUSED_PORTS))
72                 state &= AtiPcieCfg.PortDetect;
73         state = ~state;
74         state &= (1 << 4) + (1 << 5) + (1 << 6) + (1 << 7);
75         state_save = state << 17;
76         state &= !(AtiPcieCfg.PortHp);
77         reg = nbmisc_read_index(nb_dev, 0x0c);
78         reg |= state;
79         nbmisc_write_index(nb_dev, 0x0c, reg);
80
81         reg = nbmisc_read_index(nb_dev, 0x08);
82         reg |= state_save;
83         nbmisc_write_index(nb_dev, 0x08, reg);
84
85         if ((AtiPcieCfg.Config & PCIE_OFF_UNUSED_GPP_LANES)
86             && !(AtiPcieCfg.
87                  Config & (PCIE_DISABLE_HIDE_UNUSED_PORTS +
88                            PCIE_GFX_COMPLIANCE))) {
89         }
90         /* step 3 Power Down Control for Southbridge */
91         reg = nbpcie_p_read_index(dev, 0xa2);
92
93         switch ((reg >> 4) & 0x7) {     /* get bit 4-6, LC_LINK_WIDTH_RD */
94         case 1:
95                 nbpcie_ind_write_index(nb_dev, 0x65, 0x0e0e);
96                 break;
97         case 2:
98                 nbpcie_ind_write_index(nb_dev, 0x65, 0x0c0c);
99                 break;
100         default:
101                 break;
102         }
103 }
104
105 static void pcie_init(struct device *dev)
106 {
107         /* Enable pci error detecting */
108         u32 dword;
109
110         printk_debug("pcie_init in rs690_pcie.c\n");
111
112         /* System error enable */
113         dword = pci_read_config32(dev, 0x04);
114         dword |= (1 << 8);      /* System error enable */
115         dword |= (1 << 30);     /* Clear possible errors */
116         pci_write_config32(dev, 0x04, dword);
117 }
118
119 /**********************************************************************
120 **********************************************************************/
121 static void switching_gpp_configurations(device_t nb_dev, device_t sb_dev)
122 {
123         u32 reg;
124         struct southbridge_amd_rs690_config *cfg =
125             (struct southbridge_amd_rs690_config *)nb_dev->chip_info;
126
127         /* enables GPP reconfiguration */
128         reg = nbmisc_read_index(nb_dev, PCIE_NBCFG_REG7);
129         reg |=
130             (RECONFIG_GPPSB_EN + RECONFIG_GPPSB_LINK_CONFIG +
131              RECONFIG_GPPSB_ATOMIC_RESET);
132         nbmisc_write_index(nb_dev, PCIE_NBCFG_REG7, reg);
133
134         /* sets desired GPPSB configurations, bit4-7 */
135         reg = nbmisc_read_index(nb_dev, 0x67);
136         reg &= 0xffffff0f;              /* clean */
137         reg |= cfg->gpp_configuration << 4;
138         nbmisc_write_index(nb_dev, 0x67, reg);
139
140         /* read bit14 and write back its inverst value */
141         reg = nbmisc_read_index(nb_dev, PCIE_NBCFG_REG7);
142         reg ^= RECONFIG_GPPSB_GPPSB;
143         nbmisc_write_index(nb_dev, PCIE_NBCFG_REG7, reg);
144
145         /* delay 1ms */
146         mdelay(1);
147
148         /* waits until SB has trained to L0, poll for bit0-5 = 0x10 */
149         do {
150                 reg = nbpcie_p_read_index(sb_dev, PCIE_LC_STATE0);
151                 reg &= 0x1f;    /* remain LSB 5 bits */
152         } while (LC_STATE_RECONFIG_GPPSB != reg);
153
154         /* ensures that virtual channel negotiation is completed. poll for bit1 = 0 */
155         do {
156                 reg =
157                     pci_ext_read_config32(nb_dev, sb_dev,
158                                           PCIE_VC0_RESOURCE_STATUS);
159         } while (reg & VC_NEGOTIATION_PENDING);
160 }
161
162 /*****************************************************************
163 * The rs690 uses NBCONFIG:0x1c (BAR3) to map the PCIE Extended Configuration
164 * Space to a 256MB range within the first 4GB of addressable memory.
165 *****************************************************************/
166 void enable_pcie_bar3(device_t nb_dev)
167 {
168         printk_debug("enable_pcie_bar3()\n");
169         set_nbcfg_enable_bits(nb_dev, 0x7C, 1 << 30, 1 << 30);  /* Enables writes to the BAR3 register. */
170         set_nbcfg_enable_bits(nb_dev, 0x84, 7 << 16, 0 << 16);
171
172         pci_write_config32(nb_dev, 0x1C, EXT_CONF_BASE_ADDRESS);        /* PCIEMiscInit */
173         pci_write_config32(nb_dev, 0x20, 0x00000000);
174         set_htiu_enable_bits(nb_dev, 0x32, 1 << 28, 1 << 28);   /* PCIEMiscInit */
175         ProgK8TempMmioBase(1, EXT_CONF_BASE_ADDRESS, TEMP_MMIO_BASE_ADDRESS);
176 }
177
178 /*****************************************************************
179 * We should disable bar3 when we want to exit rs690_enable, because bar3 will be
180 * remapped in set_resource later.
181 *****************************************************************/
182 void disable_pcie_bar3(device_t nb_dev)
183 {
184         printk_debug("disable_pcie_bar3()\n");
185         set_nbcfg_enable_bits(nb_dev, 0x7C, 1 << 30, 0 << 30);  /* Disable writes to the BAR3. */
186         pci_write_config32(nb_dev, 0x1C, 0);    /* clear BAR3 address */
187         ProgK8TempMmioBase(0, EXT_CONF_BASE_ADDRESS, TEMP_MMIO_BASE_ADDRESS);
188 }
189
190 /*****************************************
191 * Compliant with CIM_33's PCIEGPPInit
192 * nb_dev:
193 *       root bridge struct
194 * dev:
195 *       p2p bridge struct
196 * port:
197 *       p2p bridge number, 4-8
198 *****************************************/
199 void rs690_gpp_sb_init(device_t nb_dev, device_t dev, u32 port)
200 {
201         u8 reg8;
202         u16 reg16;
203         device_t sb_dev;
204         struct southbridge_amd_rs690_config *cfg =
205             (struct southbridge_amd_rs690_config *)nb_dev->chip_info;
206         printk_debug("gpp_sb_init nb_dev=0x%p, dev=0x%p, port=0x%x\n", nb_dev, dev, port);
207
208         /* init GPP core */
209         set_pcie_enable_bits(nb_dev, 0x20 | PCIE_CORE_INDEX_GPPSB, 1 << 8,
210                              1 << 8);
211         /* PCIE initialization 5.10.2: rpr 2.12*/
212         set_pcie_enable_bits(nb_dev, 0x02 | PCIE_CORE_INDEX_GPPSB, 1 << 0, 1 << 0);     /* no description in datasheet. */
213
214         /* init GPPSB port */
215         /* Sets RCB timeout to be 100ms by setting bits[18:16] to 3 b101 and shortens the enumeration timer by setting bit[19] to 0*/
216         set_pcie_enable_bits(dev, 0x70, 7 << 16, 0xd << 16);
217         /* PCIE initialization 5.10.2: rpr 2.4 */
218         set_pcie_enable_bits(dev, 0x02, ~0xffffffff, 1 << 14);
219         /* Do not gate the electrical idle from the PHY and enables the escape from L1L23 */
220         set_pcie_enable_bits(dev, 0xA0, ~0xffffffbf, (3 << 30) | (3 << 12) | (3 << 4));
221         /* PCIE initialization 5.10.2: rpr 2.13 */
222         set_pcie_enable_bits(dev, 0x02, ~0xffffffff, 1 << 6);
223
224         /* SLOT_IMPLEMENTED in pcieConfig space */
225         reg8 = pci_read_config8(dev, 0x5b);
226         reg8 |= 1 << 0;
227         pci_write_config8(dev, 0x5b, reg8);
228
229         reg16 = pci_read_config16(dev, 0x5a);
230         reg16 |= 0x100;
231         pci_write_config16(dev, 0x5a, reg16);
232         nbmisc_write_index(nb_dev, 0x34, 0);
233
234         /* check compliance rpr step 2.1*/
235         if (AtiPcieCfg.Config & PCIE_GPP_COMPLIANCE) {
236                 u32 tmp;
237                 tmp = nbmisc_read_index(nb_dev, 0x67);
238                 tmp |= 1 << 3;
239                 nbmisc_write_index(nb_dev, 0x67, tmp);
240         }
241
242         /* step 5: dynamic slave CPL buffer allocation */
243         set_pcie_enable_bits(dev, 0x20, 1 << 11, 1 << 11);
244
245         /* step 5a: Training for GPP devices */
246         /* init GPP */
247         switch (port) {
248         case 4:         /* GPP */
249         case 5:
250         case 6:
251         case 7:
252                 /* Blocks DMA traffic during C3 state */
253                 set_pcie_enable_bits(dev, 0x10, 1 << 0, 0 << 0);
254                 /* Enabels TLP flushing */
255                 set_pcie_enable_bits(dev, 0x20, 1 << 19, 0 << 19);
256
257                 /* check port enable */
258                 if (cfg->port_enable & (1 << port)) {
259                         PcieReleasePortTraining(nb_dev, dev, port);
260                         if (!(AtiPcieCfg.Config & PCIE_GPP_COMPLIANCE)) {
261                                 u8 res = PcieTrainPort(nb_dev, dev, port);
262                                 printk_debug("PcieTrainPort port=0x%x result=%d\n", port, res);
263                                 if (res) {
264                                         AtiPcieCfg.PortDetect |= 1 << port;
265                                 }
266                         }
267                 }
268                 break;
269         case 8:         /* SB */
270                 break;
271         }
272         PciePowerOffGppPorts(nb_dev, dev, port);
273
274         /* step 5b: GFX devices in a GPP slot */
275
276         /* step 6a: VCI */
277         sb_dev = dev_find_slot(0, PCI_DEVFN(8, 0));
278         if (port == 8) {
279                 /* Clear bits 7:1 */
280                 pci_ext_write_config32(nb_dev, sb_dev, 0x114, 0x3f << 1, 0 << 1);
281                 /* Maps Traffic Class 1-7 to VC1 */
282                 pci_ext_write_config32(nb_dev, sb_dev, 0x120, 0x7f << 1, 0x7f << 1);
283                 /* Assigns VC ID to 1 */
284                 pci_ext_write_config32(nb_dev, sb_dev, 0x120, 7 << 24, 1 << 24);
285                 /* Enables VC1 */
286                 pci_ext_write_config32(nb_dev, sb_dev, 0x120, 1 << 31, 1 << 31);
287 #if 0
288                 do {
289                         reg16 = pci_ext_read_config32(nb_dev, sb_dev, 0x124);
290                         reg16 &= 0x2;
291                 } while (reg16); /*bit[1] = 0 means VC1 flow control initialization is successful */
292 #endif
293         }
294
295         /* step 6b: L0s for the southbridge link */
296         /* To enalbe L0s in the southbridage*/
297
298         /* step 6c: L0s for the GPP link(s) */
299         /* To eable L0s in the RS690 for the GPP port(s) */
300         set_pcie_enable_bits(nb_dev, 0xf9, 3 << 13, 2 << 13);
301         set_pcie_enable_bits(dev, 0xa0, 0xf << 8, 0x9 << 8);
302         reg16 = pci_read_config16(dev, 0x68);
303         reg16 |= 1 << 0;
304         pci_write_config16(dev, 0x68, reg16);
305
306         /* step 6d: ASPM L1 for the southbridge link */
307         /* To enalbe L1s in the southbridage*/
308
309         /* step 6e: ASPM L1 for GPP link(s) */;
310         set_pcie_enable_bits(nb_dev, 0xf9, 3 << 13, 2 << 13);
311         set_pcie_enable_bits(dev, 0xa0, 3 << 12, 3 << 12);
312         set_pcie_enable_bits(dev, 0xa0, 0xf << 4, 3 << 4);
313         reg16 = pci_read_config16(dev, 0x68);
314         reg16 &= ~0xff;
315         reg16 |= 1 << 1;
316         pci_write_config16(dev, 0x68, reg16);
317
318         /* step 6f: Turning off PLL during L1/L23 */
319         set_pcie_enable_bits(nb_dev, 0x40, 1 << 3, 1 << 3);
320         set_pcie_enable_bits(nb_dev, 0x40, 1 << 9, 1 << 9);
321
322         /* step 6g: TXCLK clock gating */
323         set_nbmisc_enable_bits(nb_dev, 0x7, 3 << 4, 3 << 4);
324         set_nbmisc_enable_bits(nb_dev, 0x7, 1 << 22, 1 << 22);
325         set_pcie_enable_bits(nb_dev, 0x11, 0xf << 4, 0xc << 4);
326
327         /* step 6h: LCLK clock gating, done in rs690_config_misc_clk() */
328 }
329
330 /*****************************************
331 * Compliant with CIM_33's PCIEConfigureGPPCore
332 *****************************************/
333 void config_gpp_core(device_t nb_dev, device_t sb_dev)
334 {
335         u32 reg;
336         struct southbridge_amd_rs690_config *cfg =
337             (struct southbridge_amd_rs690_config *)nb_dev->chip_info;
338
339         reg = nbmisc_read_index(nb_dev, 0x20);
340         if (AtiPcieCfg.Config & PCIE_ENABLE_STATIC_DEV_REMAP)
341                 reg &= 0xfffffffd;      /* set bit1 = 0 */
342         else
343                 reg |= 0x2;     /* set bit1 = 1 */
344         nbmisc_write_index(nb_dev, 0x20, reg);
345
346         reg = nbmisc_read_index(nb_dev, 0x67);  /* get STRAP_BIF_LINK_CONFIG_GPPSB at bit 4-7 */
347         if (cfg->gpp_configuration != ((reg >> 4) & 0xf))
348                 switching_gpp_configurations(nb_dev, sb_dev);
349         ValidatePortEn(nb_dev);
350 }
351
352 /*****************************************
353 * Compliant with CIM_33's PCIEMiscClkProg
354 *****************************************/
355 void pcie_config_misc_clk(device_t nb_dev)
356 {
357         u32 reg;
358         struct bus pbus; /* fake bus for dev0 fun1 */
359
360         reg = pci_read_config32(nb_dev, 0x4c);
361         reg |= 1 << 0;
362         pci_write_config32(nb_dev, 0x4c, reg);
363
364         if (AtiPcieCfg.Config & PCIE_GFX_CLK_GATING) {
365                 /* TXCLK Clock Gating */
366                 set_nbmisc_enable_bits(nb_dev, 0x07, 3 << 0, 3 << 0);
367                 set_nbmisc_enable_bits(nb_dev, 0x07, 1 << 22, 1 << 22);
368                 set_pcie_enable_bits(nb_dev, 0x11 | PCIE_CORE_INDEX_GFX, (3 << 6) | (~0xf), 3 << 6);
369
370                 /* LCLK Clock Gating */
371                 reg =  pci_cf8_conf1.read32(&pbus, 0, 1, 0x94);
372                 reg &= ~(1 << 16);
373                 pci_cf8_conf1.write32(&pbus, 0, 1, 0x94, reg);
374         }
375
376         if (AtiPcieCfg.Config & PCIE_GPP_CLK_GATING) {
377                 /* TXCLK Clock Gating */
378                 set_nbmisc_enable_bits(nb_dev, 0x07, 3 << 4, 3 << 4);
379                 set_nbmisc_enable_bits(nb_dev, 0x07, 1 << 22, 1 << 22);
380                 set_pcie_enable_bits(nb_dev, 0x11 | PCIE_CORE_INDEX_GPPSB, (3 << 6) | (~0xf), 3 << 6);
381
382                 /* LCLK Clock Gating */
383                 reg =  pci_cf8_conf1.read32(&pbus, 0, 1, 0x94);
384                 reg &= ~(1 << 24);
385                 pci_cf8_conf1.write32(&pbus, 0, 1, 0x94, reg);
386         }
387
388         reg = pci_read_config32(nb_dev, 0x4c);
389         reg &= ~(1 << 0);
390         pci_write_config32(nb_dev, 0x4c, reg);
391 }