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