blob: adf0e491b56ff25b705f0ec8dba72ebc053de4de [file] [log] [blame]
Angel Pons182dbde2020-04-02 23:49:05 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Stefan Reinauerdebb11f2008-10-29 04:46:52 +00002
3#include <console/console.h>
4#include <device/device.h>
5#include <device/pci.h>
Kyösti Mälkkidf128a52019-09-21 18:35:37 +03006#include <device/pci_def.h>
Kyösti Mälkkif1b58b72019-03-01 13:43:02 +02007#include <device/pci_ops.h>
Stefan Reinauerdebb11f2008-10-29 04:46:52 +00008#include <device/pci_ids.h>
Arthur Heymans742df5a2019-06-03 16:24:41 +02009#include "chip.h"
Arthur Heymanse6e5ecb2018-12-20 01:44:50 +010010#include "i82801gx.h"
11
12/* Low Power variant has 6 root ports. */
13#define NUM_ROOT_PORTS 6
14
15struct root_port_config {
16 /* RPFN is a write-once register so keep a copy until it is written */
17 u32 orig_rpfn;
18 u32 new_rpfn;
19 int num_ports;
20 struct device *ports[NUM_ROOT_PORTS];
21};
22
23static struct root_port_config rpc;
24
25static inline int root_port_is_first(struct device *dev)
26{
27 return PCI_FUNC(dev->path.pci.devfn) == 0;
28}
29
30static inline int root_port_is_last(struct device *dev)
31{
32 return PCI_FUNC(dev->path.pci.devfn) == (rpc.num_ports - 1);
33}
34
35/* Root ports are numbered 1..N in the documentation. */
36static inline int root_port_number(struct device *dev)
37{
38 return PCI_FUNC(dev->path.pci.devfn) + 1;
39}
Stefan Reinauerdebb11f2008-10-29 04:46:52 +000040
41static void pci_init(struct device *dev)
42{
43 u16 reg16;
44 u32 reg32;
45
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +000046 printk(BIOS_DEBUG, "Initializing ICH7 PCIe bridge.\n");
Stefan Reinauer109ab312009-08-12 16:08:05 +000047
Stefan Reinauera8e11682009-03-11 14:54:18 +000048 /* Enable Bus Master */
Elyes HAOUAS12349252020-04-27 05:08:26 +020049 pci_or_config16(dev, PCI_COMMAND, PCI_COMMAND_MASTER);
Stefan Reinauerdebb11f2008-10-29 04:46:52 +000050
Stefan Reinauera8e11682009-03-11 14:54:18 +000051 /* Set Cache Line Size to 0x10 */
52 // This has no effect but the OS might expect it
Elyes HAOUASae22fe22020-05-21 09:04:16 +020053 pci_write_config8(dev, PCI_CACHE_LINE_SIZE, 0x10);
Stefan Reinauerdebb11f2008-10-29 04:46:52 +000054
Kyösti Mälkkidf128a52019-09-21 18:35:37 +030055 reg16 = pci_read_config16(dev, PCI_BRIDGE_CONTROL);
56 reg16 &= ~PCI_BRIDGE_CTL_PARITY;
57 reg16 |= PCI_BRIDGE_CTL_NO_ISA;
58 pci_write_config16(dev, PCI_BRIDGE_CONTROL, reg16);
Stefan Reinauerdebb11f2008-10-29 04:46:52 +000059
Stefan Reinauera8e11682009-03-11 14:54:18 +000060 /* Enable IO xAPIC on this PCIe port */
61 reg32 = pci_read_config32(dev, 0xd8);
62 reg32 |= (1 << 7);
63 pci_write_config32(dev, 0xd8, reg32);
Stefan Reinauerdebb11f2008-10-29 04:46:52 +000064
Stefan Reinauera8e11682009-03-11 14:54:18 +000065 /* Enable Backbone Clock Gating */
66 reg32 = pci_read_config32(dev, 0xe1);
67 reg32 |= (1 << 3) | (1 << 2) | (1 << 1) | (1 << 0);
68 pci_write_config32(dev, 0xe1, reg32);
Stefan Reinauerdebb11f2008-10-29 04:46:52 +000069
Stefan Reinauera8e11682009-03-11 14:54:18 +000070 /* Set VC0 transaction class */
Kyösti Mälkki8aa7e832013-07-26 08:52:10 +030071 reg32 = pci_read_config32(dev, 0x114);
Stefan Reinauera8e11682009-03-11 14:54:18 +000072 reg32 &= 0xffffff00;
73 reg32 |= 1;
Kyösti Mälkki8aa7e832013-07-26 08:52:10 +030074 pci_write_config32(dev, 0x114, reg32);
Stefan Reinauera8e11682009-03-11 14:54:18 +000075
76 /* Mask completion timeouts */
Kyösti Mälkki8aa7e832013-07-26 08:52:10 +030077 reg32 = pci_read_config32(dev, 0x148);
Stefan Reinauera8e11682009-03-11 14:54:18 +000078 reg32 |= (1 << 14);
Kyösti Mälkki8aa7e832013-07-26 08:52:10 +030079 pci_write_config32(dev, 0x148, reg32);
80
Stefan Reinauera8e11682009-03-11 14:54:18 +000081 /* Enable common clock configuration */
82 // Are there cases when we don't want that?
83 reg16 = pci_read_config16(dev, 0x50);
84 reg16 |= (1 << 6);
85 pci_write_config16(dev, 0x50, reg16);
86
Stefan Reinauerde3206a2010-02-22 06:09:43 +000087#ifdef EVEN_MORE_DEBUG
Stefan Reinauerdebb11f2008-10-29 04:46:52 +000088 reg32 = pci_read_config32(dev, 0x20);
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +000089 printk(BIOS_SPEW, " MBL = 0x%08x\n", reg32);
Stefan Reinauerdebb11f2008-10-29 04:46:52 +000090 reg32 = pci_read_config32(dev, 0x24);
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +000091 printk(BIOS_SPEW, " PMBL = 0x%08x\n", reg32);
Stefan Reinauerdebb11f2008-10-29 04:46:52 +000092 reg32 = pci_read_config32(dev, 0x28);
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +000093 printk(BIOS_SPEW, " PMBU32 = 0x%08x\n", reg32);
Stefan Reinauerdebb11f2008-10-29 04:46:52 +000094 reg32 = pci_read_config32(dev, 0x2c);
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +000095 printk(BIOS_SPEW, " PMLU32 = 0x%08x\n", reg32);
Stefan Reinauera8e11682009-03-11 14:54:18 +000096#endif
97
98 /* Clear errors in status registers */
99 reg16 = pci_read_config16(dev, 0x06);
100 //reg16 |= 0xf900;
101 pci_write_config16(dev, 0x06, reg16);
102
103 reg16 = pci_read_config16(dev, 0x1e);
104 //reg16 |= 0xf900;
105 pci_write_config16(dev, 0x1e, reg16);
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000106}
107
Arthur Heymanse6e5ecb2018-12-20 01:44:50 +0100108static int get_num_ports(void)
109{
110 struct device *dev = pcidev_on_root(31, 0);
111 if (pci_read_config32(dev, FDVCT) & PCIE_4_PORTS_MAX)
112 return 4;
113 else
114 return 6;
115}
116
117static void root_port_init_config(struct device *dev)
118{
119 int rp;
120
121 if (root_port_is_first(dev)) {
122 rpc.orig_rpfn = RCBA32(RPFN);
123 rpc.new_rpfn = rpc.orig_rpfn;
124 rpc.num_ports = get_num_ports();
125 }
126
127 rp = root_port_number(dev);
128 if (rp > rpc.num_ports) {
Elyes HAOUAS92646ea2020-04-04 13:43:03 +0200129 printk(BIOS_ERR, "Found Root Port %d, expecting %d\n", rp, rpc.num_ports);
Arthur Heymanse6e5ecb2018-12-20 01:44:50 +0100130 return;
131 }
132
133 /* Cache pci device. */
134 rpc.ports[rp - 1] = dev;
135}
136
137/* Update devicetree with new Root Port function number assignment */
138static void ich_pcie_device_set_func(int index, int pci_func)
139{
140 struct device *dev;
141 unsigned int new_devfn;
142
143 dev = rpc.ports[index];
144
145 /* Set the new PCI function field for this Root Port. */
146 rpc.new_rpfn &= ~RPFN_FNMASK(index);
147 rpc.new_rpfn |= RPFN_FNSET(index, pci_func);
148
149 /* Determine the new devfn for this port */
150 new_devfn = PCI_DEVFN(ICH_PCIE_DEV_SLOT, pci_func);
151
152 if (dev->path.pci.devfn != new_devfn) {
153 printk(BIOS_DEBUG,
154 "ICH: PCIe map %02x.%1x -> %02x.%1x\n",
155 PCI_SLOT(dev->path.pci.devfn),
156 PCI_FUNC(dev->path.pci.devfn),
157 PCI_SLOT(new_devfn), PCI_FUNC(new_devfn));
158
159 dev->path.pci.devfn = new_devfn;
160 }
161}
162
163static void root_port_commit_config(struct device *dev)
164{
165 int i;
166 int coalesce = 0;
167
168 if (dev->chip_info != NULL) {
Elyes HAOUAS92646ea2020-04-04 13:43:03 +0200169 struct southbridge_intel_i82801gx_config *config = dev->chip_info;
Arthur Heymanse6e5ecb2018-12-20 01:44:50 +0100170 coalesce = config->pcie_port_coalesce;
171 }
172
173 if (!rpc.ports[0]->enabled)
174 coalesce = 1;
175
176 for (i = 0; i < rpc.num_ports; i++) {
177 struct device *pcie_dev;
178
179 pcie_dev = rpc.ports[i];
180
Jacob Garber14e826f2019-03-12 22:27:52 -0600181 if (pcie_dev == NULL) {
Elyes HAOUAS92646ea2020-04-04 13:43:03 +0200182 printk(BIOS_ERR, "Root Port %d device is NULL?\n", i + 1);
Arthur Heymanse6e5ecb2018-12-20 01:44:50 +0100183 continue;
184 }
185
186 if (pcie_dev->enabled)
187 continue;
188
Elyes HAOUAS92646ea2020-04-04 13:43:03 +0200189 printk(BIOS_DEBUG, "%s: Disabling device\n", dev_path(pcie_dev));
Arthur Heymanse6e5ecb2018-12-20 01:44:50 +0100190
191 /* Disable this device if possible */
192 i82801gx_enable(pcie_dev);
193 }
194
195 if (coalesce) {
196 int current_func;
197
198 /* For all Root Ports N enabled ports get assigned the lower
199 * PCI function number. The disabled ones get upper PCI
200 * function numbers. */
201 current_func = 0;
202 for (i = 0; i < rpc.num_ports; i++) {
203 if (!rpc.ports[i]->enabled)
204 continue;
205 ich_pcie_device_set_func(i, current_func);
206 current_func++;
207 }
208
209 /* Allocate the disabled devices' PCI function number. */
210 for (i = 0; i < rpc.num_ports; i++) {
211 if (rpc.ports[i]->enabled)
212 continue;
213 ich_pcie_device_set_func(i, current_func);
214 current_func++;
215 }
216 }
217
Elyes HAOUAS92646ea2020-04-04 13:43:03 +0200218 printk(BIOS_SPEW, "ICH: RPFN 0x%08x -> 0x%08x\n", rpc.orig_rpfn, rpc.new_rpfn);
Arthur Heymanse6e5ecb2018-12-20 01:44:50 +0100219 RCBA32(RPFN) = rpc.new_rpfn;
220}
221
222static void ich_pcie_enable(struct device *dev)
223{
224 /* Add this device to the root port config structure. */
225 root_port_init_config(dev);
226
227 /*
228 * When processing the last PCIe root port we can now
229 * update the Root Port Function Number and Hide register.
230 */
231 if (root_port_is_last(dev))
232 root_port_commit_config(dev);
233}
234
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000235static struct device_operations device_ops = {
236 .read_resources = pci_bus_read_resources,
237 .set_resources = pci_dev_set_resources,
238 .enable_resources = pci_bus_enable_resources,
239 .init = pci_init,
Arthur Heymanse6e5ecb2018-12-20 01:44:50 +0100240 .enable = ich_pcie_enable,
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000241 .scan_bus = pci_scan_bridge,
Angel Pons1fc0edd2020-05-31 00:03:28 +0200242 .ops_pci = &pci_dev_ops_pci,
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000243};
244
Patrick Georgiefff7332012-07-26 19:48:23 +0200245static const unsigned short i82801gx_pcie_ids[] = {
246 0x27d0, /* 82801GB/GR/GDH/GBM/GHM (ICH7/ICH7R/ICH7DH/ICH7-M/ICH7-M DH) */
247 0x27d2, /* 82801GB/GR/GDH/GBM/GHM (ICH7/ICH7R/ICH7DH/ICH7-M/ICH7-M DH) */
248 0x27d4, /* 82801GB/GR/GDH/GBM/GHM (ICH7/ICH7R/ICH7DH/ICH7-M/ICH7-M DH) */
249 0x27d6, /* 82801GB/GR/GDH/GBM/GHM (ICH7/ICH7R/ICH7DH/ICH7-M/ICH7-M DH) */
250 0x27e0, /* 82801GR/GDH/GHM (ICH7R/ICH7DH/ICH7-M DH) */
251 0x27e2, /* 82801GR/GDH/GHM (ICH7R/ICH7DH/ICH7-M DH) */
252 0
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000253};
254
Patrick Georgiefff7332012-07-26 19:48:23 +0200255static const struct pci_driver i82801gx_pcie __pci_driver = {
Arthur Heymans3f111b02017-03-09 12:02:52 +0100256 .ops = &device_ops,
257 .vendor = PCI_VENDOR_ID_INTEL,
258 .devices = i82801gx_pcie_ids,
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000259};