blob: befbf3beb72e39bba77c828227b942d3caa4208b [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älkkif1b58b72019-03-01 13:43:02 +02006#include <device/pci_ops.h>
Stefan Reinauerdebb11f2008-10-29 04:46:52 +00007#include <device/pci_ids.h>
Arthur Heymans742df5a2019-06-03 16:24:41 +02008#include "chip.h"
Arthur Heymanse6e5ecb2018-12-20 01:44:50 +01009#include "i82801gx.h"
10
11/* Low Power variant has 6 root ports. */
12#define NUM_ROOT_PORTS 6
13
14struct root_port_config {
15 /* RPFN is a write-once register so keep a copy until it is written */
16 u32 orig_rpfn;
17 u32 new_rpfn;
18 int num_ports;
19 struct device *ports[NUM_ROOT_PORTS];
20};
21
22static struct root_port_config rpc;
23
24static inline int root_port_is_first(struct device *dev)
25{
26 return PCI_FUNC(dev->path.pci.devfn) == 0;
27}
28
29static inline int root_port_is_last(struct device *dev)
30{
31 return PCI_FUNC(dev->path.pci.devfn) == (rpc.num_ports - 1);
32}
33
34/* Root ports are numbered 1..N in the documentation. */
35static inline int root_port_number(struct device *dev)
36{
37 return PCI_FUNC(dev->path.pci.devfn) + 1;
38}
Stefan Reinauerdebb11f2008-10-29 04:46:52 +000039
40static void pci_init(struct device *dev)
41{
42 u16 reg16;
Stefan Reinauerdebb11f2008-10-29 04:46:52 +000043
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +000044 printk(BIOS_DEBUG, "Initializing ICH7 PCIe bridge.\n");
Stefan Reinauer109ab312009-08-12 16:08:05 +000045
Stefan Reinauera8e11682009-03-11 14:54:18 +000046 /* Enable Bus Master */
Elyes HAOUAS12349252020-04-27 05:08:26 +020047 pci_or_config16(dev, PCI_COMMAND, PCI_COMMAND_MASTER);
Stefan Reinauerdebb11f2008-10-29 04:46:52 +000048
Stefan Reinauera8e11682009-03-11 14:54:18 +000049 /* Set Cache Line Size to 0x10 */
50 // This has no effect but the OS might expect it
Elyes HAOUASae22fe22020-05-21 09:04:16 +020051 pci_write_config8(dev, PCI_CACHE_LINE_SIZE, 0x10);
Stefan Reinauerdebb11f2008-10-29 04:46:52 +000052
Angel Ponsd19332c2020-06-08 12:32:54 +020053 pci_and_config16(dev, PCI_BRIDGE_CONTROL, ~PCI_BRIDGE_CTL_PARITY);
Stefan Reinauerdebb11f2008-10-29 04:46:52 +000054
Stefan Reinauera8e11682009-03-11 14:54:18 +000055 /* Enable IO xAPIC on this PCIe port */
Angel Ponsd19332c2020-06-08 12:32:54 +020056 pci_or_config32(dev, 0xd8, 1 << 7);
Stefan Reinauerdebb11f2008-10-29 04:46:52 +000057
Stefan Reinauera8e11682009-03-11 14:54:18 +000058 /* Enable Backbone Clock Gating */
Angel Ponsd19332c2020-06-08 12:32:54 +020059 pci_or_config32(dev, 0xe1, (1 << 3) | (1 << 2) | (1 << 1) | (1 << 0));
Stefan Reinauerdebb11f2008-10-29 04:46:52 +000060
Stefan Reinauera8e11682009-03-11 14:54:18 +000061 /* Set VC0 transaction class */
Angel Ponsd19332c2020-06-08 12:32:54 +020062 pci_update_config32(dev, 0x114, ~0x000000ff, 1);
Stefan Reinauera8e11682009-03-11 14:54:18 +000063
64 /* Mask completion timeouts */
Angel Ponsd19332c2020-06-08 12:32:54 +020065 pci_or_config32(dev, 0x148, 1 << 14);
Kyösti Mälkki8aa7e832013-07-26 08:52:10 +030066
Stefan Reinauera8e11682009-03-11 14:54:18 +000067 /* Enable common clock configuration */
68 // Are there cases when we don't want that?
Angel Ponsd19332c2020-06-08 12:32:54 +020069 pci_or_config16(dev, 0x50, 1 << 6);
Stefan Reinauera8e11682009-03-11 14:54:18 +000070
Angel Ponsd19332c2020-06-08 12:32:54 +020071 /* Clear errors in status registers. FIXME: Do something? */
Stefan Reinauera8e11682009-03-11 14:54:18 +000072 reg16 = pci_read_config16(dev, 0x06);
73 //reg16 |= 0xf900;
74 pci_write_config16(dev, 0x06, reg16);
75
76 reg16 = pci_read_config16(dev, 0x1e);
77 //reg16 |= 0xf900;
78 pci_write_config16(dev, 0x1e, reg16);
Stefan Reinauerdebb11f2008-10-29 04:46:52 +000079}
80
Arthur Heymanse6e5ecb2018-12-20 01:44:50 +010081static int get_num_ports(void)
82{
83 struct device *dev = pcidev_on_root(31, 0);
84 if (pci_read_config32(dev, FDVCT) & PCIE_4_PORTS_MAX)
85 return 4;
86 else
87 return 6;
88}
89
90static void root_port_init_config(struct device *dev)
91{
92 int rp;
93
94 if (root_port_is_first(dev)) {
95 rpc.orig_rpfn = RCBA32(RPFN);
96 rpc.new_rpfn = rpc.orig_rpfn;
97 rpc.num_ports = get_num_ports();
98 }
99
100 rp = root_port_number(dev);
101 if (rp > rpc.num_ports) {
Elyes HAOUAS92646ea2020-04-04 13:43:03 +0200102 printk(BIOS_ERR, "Found Root Port %d, expecting %d\n", rp, rpc.num_ports);
Arthur Heymanse6e5ecb2018-12-20 01:44:50 +0100103 return;
104 }
105
106 /* Cache pci device. */
107 rpc.ports[rp - 1] = dev;
108}
109
110/* Update devicetree with new Root Port function number assignment */
111static void ich_pcie_device_set_func(int index, int pci_func)
112{
113 struct device *dev;
114 unsigned int new_devfn;
115
116 dev = rpc.ports[index];
117
118 /* Set the new PCI function field for this Root Port. */
119 rpc.new_rpfn &= ~RPFN_FNMASK(index);
120 rpc.new_rpfn |= RPFN_FNSET(index, pci_func);
121
122 /* Determine the new devfn for this port */
123 new_devfn = PCI_DEVFN(ICH_PCIE_DEV_SLOT, pci_func);
124
125 if (dev->path.pci.devfn != new_devfn) {
126 printk(BIOS_DEBUG,
127 "ICH: PCIe map %02x.%1x -> %02x.%1x\n",
128 PCI_SLOT(dev->path.pci.devfn),
129 PCI_FUNC(dev->path.pci.devfn),
130 PCI_SLOT(new_devfn), PCI_FUNC(new_devfn));
131
132 dev->path.pci.devfn = new_devfn;
133 }
134}
135
136static void root_port_commit_config(struct device *dev)
137{
138 int i;
Angel Ponsaf4bd562021-12-28 13:05:56 +0100139 bool coalesce = false;
Arthur Heymanse6e5ecb2018-12-20 01:44:50 +0100140
141 if (dev->chip_info != NULL) {
Elyes HAOUAS8d9a6f12020-04-28 04:57:27 +0200142 const struct southbridge_intel_i82801gx_config *config = dev->chip_info;
Arthur Heymanse6e5ecb2018-12-20 01:44:50 +0100143 coalesce = config->pcie_port_coalesce;
144 }
145
146 if (!rpc.ports[0]->enabled)
Angel Ponsaf4bd562021-12-28 13:05:56 +0100147 coalesce = true;
Arthur Heymanse6e5ecb2018-12-20 01:44:50 +0100148
149 for (i = 0; i < rpc.num_ports; i++) {
150 struct device *pcie_dev;
151
152 pcie_dev = rpc.ports[i];
153
Jacob Garber14e826f2019-03-12 22:27:52 -0600154 if (pcie_dev == NULL) {
Elyes HAOUAS92646ea2020-04-04 13:43:03 +0200155 printk(BIOS_ERR, "Root Port %d device is NULL?\n", i + 1);
Arthur Heymanse6e5ecb2018-12-20 01:44:50 +0100156 continue;
157 }
158
159 if (pcie_dev->enabled)
160 continue;
161
Elyes HAOUAS92646ea2020-04-04 13:43:03 +0200162 printk(BIOS_DEBUG, "%s: Disabling device\n", dev_path(pcie_dev));
Arthur Heymanse6e5ecb2018-12-20 01:44:50 +0100163
164 /* Disable this device if possible */
165 i82801gx_enable(pcie_dev);
166 }
167
168 if (coalesce) {
169 int current_func;
170
171 /* For all Root Ports N enabled ports get assigned the lower
172 * PCI function number. The disabled ones get upper PCI
173 * function numbers. */
174 current_func = 0;
175 for (i = 0; i < rpc.num_ports; i++) {
176 if (!rpc.ports[i]->enabled)
177 continue;
178 ich_pcie_device_set_func(i, current_func);
179 current_func++;
180 }
181
182 /* Allocate the disabled devices' PCI function number. */
183 for (i = 0; i < rpc.num_ports; i++) {
184 if (rpc.ports[i]->enabled)
185 continue;
186 ich_pcie_device_set_func(i, current_func);
187 current_func++;
188 }
189 }
190
Elyes HAOUAS92646ea2020-04-04 13:43:03 +0200191 printk(BIOS_SPEW, "ICH: RPFN 0x%08x -> 0x%08x\n", rpc.orig_rpfn, rpc.new_rpfn);
Arthur Heymanse6e5ecb2018-12-20 01:44:50 +0100192 RCBA32(RPFN) = rpc.new_rpfn;
193}
194
195static void ich_pcie_enable(struct device *dev)
196{
197 /* Add this device to the root port config structure. */
198 root_port_init_config(dev);
199
200 /*
201 * When processing the last PCIe root port we can now
202 * update the Root Port Function Number and Hide register.
203 */
204 if (root_port_is_last(dev))
205 root_port_commit_config(dev);
206}
207
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000208static struct device_operations device_ops = {
209 .read_resources = pci_bus_read_resources,
210 .set_resources = pci_dev_set_resources,
211 .enable_resources = pci_bus_enable_resources,
212 .init = pci_init,
Arthur Heymanse6e5ecb2018-12-20 01:44:50 +0100213 .enable = ich_pcie_enable,
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000214 .scan_bus = pci_scan_bridge,
Angel Pons1fc0edd2020-05-31 00:03:28 +0200215 .ops_pci = &pci_dev_ops_pci,
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000216};
217
Patrick Georgiefff7332012-07-26 19:48:23 +0200218static const unsigned short i82801gx_pcie_ids[] = {
219 0x27d0, /* 82801GB/GR/GDH/GBM/GHM (ICH7/ICH7R/ICH7DH/ICH7-M/ICH7-M DH) */
220 0x27d2, /* 82801GB/GR/GDH/GBM/GHM (ICH7/ICH7R/ICH7DH/ICH7-M/ICH7-M DH) */
221 0x27d4, /* 82801GB/GR/GDH/GBM/GHM (ICH7/ICH7R/ICH7DH/ICH7-M/ICH7-M DH) */
222 0x27d6, /* 82801GB/GR/GDH/GBM/GHM (ICH7/ICH7R/ICH7DH/ICH7-M/ICH7-M DH) */
223 0x27e0, /* 82801GR/GDH/GHM (ICH7R/ICH7DH/ICH7-M DH) */
224 0x27e2, /* 82801GR/GDH/GHM (ICH7R/ICH7DH/ICH7-M DH) */
225 0
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000226};
227
Patrick Georgiefff7332012-07-26 19:48:23 +0200228static const struct pci_driver i82801gx_pcie __pci_driver = {
Arthur Heymans3f111b02017-03-09 12:02:52 +0100229 .ops = &device_ops,
Felix Singer43b7f412022-03-07 04:34:52 +0100230 .vendor = PCI_VID_INTEL,
Arthur Heymans3f111b02017-03-09 12:02:52 +0100231 .devices = i82801gx_pcie_ids,
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000232};