blob: 7efaca91301aa5fe68e0e681eba7588d711a937a [file] [log] [blame]
Angel Pons182dbde2020-04-02 23:49:05 +02001/* SPDX-License-Identifier: GPL-2.0-only */
2/* This file is part of the coreboot project. */
Stefan Reinauerdebb11f2008-10-29 04:46:52 +00003
4#include <console/console.h>
5#include <device/device.h>
6#include <device/pci.h>
Kyösti Mälkkidf128a52019-09-21 18:35:37 +03007#include <device/pci_def.h>
Kyösti Mälkkif1b58b72019-03-01 13:43:02 +02008#include <device/pci_ops.h>
Stefan Reinauerdebb11f2008-10-29 04:46:52 +00009#include <device/pci_ids.h>
Arthur Heymans742df5a2019-06-03 16:24:41 +020010#include "chip.h"
Arthur Heymanse6e5ecb2018-12-20 01:44:50 +010011#include "i82801gx.h"
12
13/* Low Power variant has 6 root ports. */
14#define NUM_ROOT_PORTS 6
15
16struct root_port_config {
17 /* RPFN is a write-once register so keep a copy until it is written */
18 u32 orig_rpfn;
19 u32 new_rpfn;
20 int num_ports;
21 struct device *ports[NUM_ROOT_PORTS];
22};
23
24static struct root_port_config rpc;
25
26static inline int root_port_is_first(struct device *dev)
27{
28 return PCI_FUNC(dev->path.pci.devfn) == 0;
29}
30
31static inline int root_port_is_last(struct device *dev)
32{
33 return PCI_FUNC(dev->path.pci.devfn) == (rpc.num_ports - 1);
34}
35
36/* Root ports are numbered 1..N in the documentation. */
37static inline int root_port_number(struct device *dev)
38{
39 return PCI_FUNC(dev->path.pci.devfn) + 1;
40}
Stefan Reinauerdebb11f2008-10-29 04:46:52 +000041
42static void pci_init(struct device *dev)
43{
44 u16 reg16;
45 u32 reg32;
46
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +000047 printk(BIOS_DEBUG, "Initializing ICH7 PCIe bridge.\n");
Stefan Reinauer109ab312009-08-12 16:08:05 +000048
Stefan Reinauera8e11682009-03-11 14:54:18 +000049 /* Enable Bus Master */
50 reg32 = pci_read_config32(dev, PCI_COMMAND);
51 reg32 |= PCI_COMMAND_MASTER;
52 pci_write_config32(dev, PCI_COMMAND, reg32);
Stefan Reinauerdebb11f2008-10-29 04:46:52 +000053
Stefan Reinauera8e11682009-03-11 14:54:18 +000054 /* Set Cache Line Size to 0x10 */
55 // This has no effect but the OS might expect it
56 pci_write_config8(dev, 0x0c, 0x10);
Stefan Reinauerdebb11f2008-10-29 04:46:52 +000057
Kyösti Mälkkidf128a52019-09-21 18:35:37 +030058 reg16 = pci_read_config16(dev, PCI_BRIDGE_CONTROL);
59 reg16 &= ~PCI_BRIDGE_CTL_PARITY;
60 reg16 |= PCI_BRIDGE_CTL_NO_ISA;
61 pci_write_config16(dev, PCI_BRIDGE_CONTROL, reg16);
Stefan Reinauerdebb11f2008-10-29 04:46:52 +000062
Stefan Reinauera8e11682009-03-11 14:54:18 +000063 /* Enable IO xAPIC on this PCIe port */
64 reg32 = pci_read_config32(dev, 0xd8);
65 reg32 |= (1 << 7);
66 pci_write_config32(dev, 0xd8, reg32);
Stefan Reinauerdebb11f2008-10-29 04:46:52 +000067
Stefan Reinauera8e11682009-03-11 14:54:18 +000068 /* Enable Backbone Clock Gating */
69 reg32 = pci_read_config32(dev, 0xe1);
70 reg32 |= (1 << 3) | (1 << 2) | (1 << 1) | (1 << 0);
71 pci_write_config32(dev, 0xe1, reg32);
Stefan Reinauerdebb11f2008-10-29 04:46:52 +000072
Stefan Reinauera8e11682009-03-11 14:54:18 +000073 /* Set VC0 transaction class */
Kyösti Mälkki8aa7e832013-07-26 08:52:10 +030074 reg32 = pci_read_config32(dev, 0x114);
Stefan Reinauera8e11682009-03-11 14:54:18 +000075 reg32 &= 0xffffff00;
76 reg32 |= 1;
Kyösti Mälkki8aa7e832013-07-26 08:52:10 +030077 pci_write_config32(dev, 0x114, reg32);
Stefan Reinauera8e11682009-03-11 14:54:18 +000078
79 /* Mask completion timeouts */
Kyösti Mälkki8aa7e832013-07-26 08:52:10 +030080 reg32 = pci_read_config32(dev, 0x148);
Stefan Reinauera8e11682009-03-11 14:54:18 +000081 reg32 |= (1 << 14);
Kyösti Mälkki8aa7e832013-07-26 08:52:10 +030082 pci_write_config32(dev, 0x148, reg32);
83
Stefan Reinauera8e11682009-03-11 14:54:18 +000084 /* Enable common clock configuration */
85 // Are there cases when we don't want that?
86 reg16 = pci_read_config16(dev, 0x50);
87 reg16 |= (1 << 6);
88 pci_write_config16(dev, 0x50, reg16);
89
Stefan Reinauerde3206a2010-02-22 06:09:43 +000090#ifdef EVEN_MORE_DEBUG
Stefan Reinauerdebb11f2008-10-29 04:46:52 +000091 reg32 = pci_read_config32(dev, 0x20);
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +000092 printk(BIOS_SPEW, " MBL = 0x%08x\n", reg32);
Stefan Reinauerdebb11f2008-10-29 04:46:52 +000093 reg32 = pci_read_config32(dev, 0x24);
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +000094 printk(BIOS_SPEW, " PMBL = 0x%08x\n", reg32);
Stefan Reinauerdebb11f2008-10-29 04:46:52 +000095 reg32 = pci_read_config32(dev, 0x28);
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +000096 printk(BIOS_SPEW, " PMBU32 = 0x%08x\n", reg32);
Stefan Reinauerdebb11f2008-10-29 04:46:52 +000097 reg32 = pci_read_config32(dev, 0x2c);
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +000098 printk(BIOS_SPEW, " PMLU32 = 0x%08x\n", reg32);
Stefan Reinauera8e11682009-03-11 14:54:18 +000099#endif
100
101 /* Clear errors in status registers */
102 reg16 = pci_read_config16(dev, 0x06);
103 //reg16 |= 0xf900;
104 pci_write_config16(dev, 0x06, reg16);
105
106 reg16 = pci_read_config16(dev, 0x1e);
107 //reg16 |= 0xf900;
108 pci_write_config16(dev, 0x1e, reg16);
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000109}
110
Arthur Heymanse6e5ecb2018-12-20 01:44:50 +0100111static int get_num_ports(void)
112{
113 struct device *dev = pcidev_on_root(31, 0);
114 if (pci_read_config32(dev, FDVCT) & PCIE_4_PORTS_MAX)
115 return 4;
116 else
117 return 6;
118}
119
120static void root_port_init_config(struct device *dev)
121{
122 int rp;
123
124 if (root_port_is_first(dev)) {
125 rpc.orig_rpfn = RCBA32(RPFN);
126 rpc.new_rpfn = rpc.orig_rpfn;
127 rpc.num_ports = get_num_ports();
128 }
129
130 rp = root_port_number(dev);
131 if (rp > rpc.num_ports) {
132 printk(BIOS_ERR, "Found Root Port %d, expecting %d\n",
133 rp, rpc.num_ports);
134 return;
135 }
136
137 /* Cache pci device. */
138 rpc.ports[rp - 1] = dev;
139}
140
141/* Update devicetree with new Root Port function number assignment */
142static void ich_pcie_device_set_func(int index, int pci_func)
143{
144 struct device *dev;
145 unsigned int new_devfn;
146
147 dev = rpc.ports[index];
148
149 /* Set the new PCI function field for this Root Port. */
150 rpc.new_rpfn &= ~RPFN_FNMASK(index);
151 rpc.new_rpfn |= RPFN_FNSET(index, pci_func);
152
153 /* Determine the new devfn for this port */
154 new_devfn = PCI_DEVFN(ICH_PCIE_DEV_SLOT, pci_func);
155
156 if (dev->path.pci.devfn != new_devfn) {
157 printk(BIOS_DEBUG,
158 "ICH: PCIe map %02x.%1x -> %02x.%1x\n",
159 PCI_SLOT(dev->path.pci.devfn),
160 PCI_FUNC(dev->path.pci.devfn),
161 PCI_SLOT(new_devfn), PCI_FUNC(new_devfn));
162
163 dev->path.pci.devfn = new_devfn;
164 }
165}
166
167static void root_port_commit_config(struct device *dev)
168{
169 int i;
170 int coalesce = 0;
171
172 if (dev->chip_info != NULL) {
173 struct southbridge_intel_i82801gx_config *config
174 = dev->chip_info;
175 coalesce = config->pcie_port_coalesce;
176 }
177
178 if (!rpc.ports[0]->enabled)
179 coalesce = 1;
180
181 for (i = 0; i < rpc.num_ports; i++) {
182 struct device *pcie_dev;
183
184 pcie_dev = rpc.ports[i];
185
Jacob Garber14e826f2019-03-12 22:27:52 -0600186 if (pcie_dev == NULL) {
Arthur Heymanse6e5ecb2018-12-20 01:44:50 +0100187 printk(BIOS_ERR, "Root Port %d device is NULL?\n",
188 i + 1);
189 continue;
190 }
191
192 if (pcie_dev->enabled)
193 continue;
194
195 printk(BIOS_DEBUG, "%s: Disabling device\n",
196 dev_path(pcie_dev));
197
198 /* Disable this device if possible */
199 i82801gx_enable(pcie_dev);
200 }
201
202 if (coalesce) {
203 int current_func;
204
205 /* For all Root Ports N enabled ports get assigned the lower
206 * PCI function number. The disabled ones get upper PCI
207 * function numbers. */
208 current_func = 0;
209 for (i = 0; i < rpc.num_ports; i++) {
210 if (!rpc.ports[i]->enabled)
211 continue;
212 ich_pcie_device_set_func(i, current_func);
213 current_func++;
214 }
215
216 /* Allocate the disabled devices' PCI function number. */
217 for (i = 0; i < rpc.num_ports; i++) {
218 if (rpc.ports[i]->enabled)
219 continue;
220 ich_pcie_device_set_func(i, current_func);
221 current_func++;
222 }
223 }
224
225 printk(BIOS_SPEW, "ICH: RPFN 0x%08x -> 0x%08x\n",
226 rpc.orig_rpfn, rpc.new_rpfn);
227 RCBA32(RPFN) = rpc.new_rpfn;
228}
229
230static void ich_pcie_enable(struct device *dev)
231{
232 /* Add this device to the root port config structure. */
233 root_port_init_config(dev);
234
235 /*
236 * When processing the last PCIe root port we can now
237 * update the Root Port Function Number and Hide register.
238 */
239 if (root_port_is_last(dev))
240 root_port_commit_config(dev);
241}
242
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000243static struct pci_operations pci_ops = {
Subrata Banik15ccbf02019-03-20 15:09:44 +0530244 .set_subsystem = pci_dev_set_subsystem,
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000245};
246
247static struct device_operations device_ops = {
248 .read_resources = pci_bus_read_resources,
249 .set_resources = pci_dev_set_resources,
250 .enable_resources = pci_bus_enable_resources,
251 .init = pci_init,
Arthur Heymanse6e5ecb2018-12-20 01:44:50 +0100252 .enable = ich_pcie_enable,
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000253 .scan_bus = pci_scan_bridge,
254 .ops_pci = &pci_ops,
255};
256
Patrick Georgiefff7332012-07-26 19:48:23 +0200257static const unsigned short i82801gx_pcie_ids[] = {
258 0x27d0, /* 82801GB/GR/GDH/GBM/GHM (ICH7/ICH7R/ICH7DH/ICH7-M/ICH7-M DH) */
259 0x27d2, /* 82801GB/GR/GDH/GBM/GHM (ICH7/ICH7R/ICH7DH/ICH7-M/ICH7-M DH) */
260 0x27d4, /* 82801GB/GR/GDH/GBM/GHM (ICH7/ICH7R/ICH7DH/ICH7-M/ICH7-M DH) */
261 0x27d6, /* 82801GB/GR/GDH/GBM/GHM (ICH7/ICH7R/ICH7DH/ICH7-M/ICH7-M DH) */
262 0x27e0, /* 82801GR/GDH/GHM (ICH7R/ICH7DH/ICH7-M DH) */
263 0x27e2, /* 82801GR/GDH/GHM (ICH7R/ICH7DH/ICH7-M DH) */
264 0
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000265};
266
Patrick Georgiefff7332012-07-26 19:48:23 +0200267static const struct pci_driver i82801gx_pcie __pci_driver = {
Arthur Heymans3f111b02017-03-09 12:02:52 +0100268 .ops = &device_ops,
269 .vendor = PCI_VENDOR_ID_INTEL,
270 .devices = i82801gx_pcie_ids,
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000271};