blob: 9446527c7bb3bee4871c866dee172fff058c2d49 [file] [log] [blame]
Stefan Reinauerdebb11f2008-10-29 04:46:52 +00001/*
2 * This file is part of the coreboot project.
3 *
Stefan Reinauer54309d62009-01-20 22:53:10 +00004 * Copyright (C) 2008-2009 coresystems GmbH
Stefan Reinauerdebb11f2008-10-29 04:46:52 +00005 *
Stefan Reinauera8e11682009-03-11 14:54:18 +00006 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; version 2 of
9 * the License.
Stefan Reinauerdebb11f2008-10-29 04:46:52 +000010 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
Stefan Reinauerdebb11f2008-10-29 04:46:52 +000015 */
16
17#include <console/console.h>
18#include <device/device.h>
19#include <device/pci.h>
Kyösti Mälkkif1b58b72019-03-01 13:43:02 +020020#include <device/pci_ops.h>
Stefan Reinauerdebb11f2008-10-29 04:46:52 +000021#include <device/pci_ids.h>
Arthur Heymanse6e5ecb2018-12-20 01:44:50 +010022#include "i82801gx.h"
23
24/* Low Power variant has 6 root ports. */
25#define NUM_ROOT_PORTS 6
26
27struct root_port_config {
28 /* RPFN is a write-once register so keep a copy until it is written */
29 u32 orig_rpfn;
30 u32 new_rpfn;
31 int num_ports;
32 struct device *ports[NUM_ROOT_PORTS];
33};
34
35static struct root_port_config rpc;
36
37static inline int root_port_is_first(struct device *dev)
38{
39 return PCI_FUNC(dev->path.pci.devfn) == 0;
40}
41
42static inline int root_port_is_last(struct device *dev)
43{
44 return PCI_FUNC(dev->path.pci.devfn) == (rpc.num_ports - 1);
45}
46
47/* Root ports are numbered 1..N in the documentation. */
48static inline int root_port_number(struct device *dev)
49{
50 return PCI_FUNC(dev->path.pci.devfn) + 1;
51}
Stefan Reinauerdebb11f2008-10-29 04:46:52 +000052
53static void pci_init(struct device *dev)
54{
55 u16 reg16;
56 u32 reg32;
57
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +000058 printk(BIOS_DEBUG, "Initializing ICH7 PCIe bridge.\n");
Stefan Reinauer109ab312009-08-12 16:08:05 +000059
Stefan Reinauera8e11682009-03-11 14:54:18 +000060 /* Enable Bus Master */
61 reg32 = pci_read_config32(dev, PCI_COMMAND);
62 reg32 |= PCI_COMMAND_MASTER;
63 pci_write_config32(dev, PCI_COMMAND, reg32);
Stefan Reinauerdebb11f2008-10-29 04:46:52 +000064
Stefan Reinauera8e11682009-03-11 14:54:18 +000065 /* Set Cache Line Size to 0x10 */
66 // This has no effect but the OS might expect it
67 pci_write_config8(dev, 0x0c, 0x10);
Stefan Reinauerdebb11f2008-10-29 04:46:52 +000068
Stefan Reinauerdebb11f2008-10-29 04:46:52 +000069 reg16 = pci_read_config16(dev, 0x3e);
Stefan Reinauera8e11682009-03-11 14:54:18 +000070 reg16 &= ~(1 << 0); /* disable parity error response */
71 // reg16 &= ~(1 << 1); /* disable SERR */
72 reg16 |= (1 << 2); /* ISA enable */
Stefan Reinauerdebb11f2008-10-29 04:46:52 +000073 pci_write_config16(dev, 0x3e, reg16);
74
Stefan Reinauera8e11682009-03-11 14:54:18 +000075 /* Enable IO xAPIC on this PCIe port */
76 reg32 = pci_read_config32(dev, 0xd8);
77 reg32 |= (1 << 7);
78 pci_write_config32(dev, 0xd8, reg32);
Stefan Reinauerdebb11f2008-10-29 04:46:52 +000079
Stefan Reinauera8e11682009-03-11 14:54:18 +000080 /* Enable Backbone Clock Gating */
81 reg32 = pci_read_config32(dev, 0xe1);
82 reg32 |= (1 << 3) | (1 << 2) | (1 << 1) | (1 << 0);
83 pci_write_config32(dev, 0xe1, reg32);
Stefan Reinauerdebb11f2008-10-29 04:46:52 +000084
Stefan Reinauera8e11682009-03-11 14:54:18 +000085 /* Set VC0 transaction class */
Kyösti Mälkki8aa7e832013-07-26 08:52:10 +030086 reg32 = pci_read_config32(dev, 0x114);
Stefan Reinauera8e11682009-03-11 14:54:18 +000087 reg32 &= 0xffffff00;
88 reg32 |= 1;
Kyösti Mälkki8aa7e832013-07-26 08:52:10 +030089 pci_write_config32(dev, 0x114, reg32);
Stefan Reinauera8e11682009-03-11 14:54:18 +000090
91 /* Mask completion timeouts */
Kyösti Mälkki8aa7e832013-07-26 08:52:10 +030092 reg32 = pci_read_config32(dev, 0x148);
Stefan Reinauera8e11682009-03-11 14:54:18 +000093 reg32 |= (1 << 14);
Kyösti Mälkki8aa7e832013-07-26 08:52:10 +030094 pci_write_config32(dev, 0x148, reg32);
95
Stefan Reinauera8e11682009-03-11 14:54:18 +000096 /* Enable common clock configuration */
97 // Are there cases when we don't want that?
98 reg16 = pci_read_config16(dev, 0x50);
99 reg16 |= (1 << 6);
100 pci_write_config16(dev, 0x50, reg16);
101
Stefan Reinauerde3206a2010-02-22 06:09:43 +0000102#ifdef EVEN_MORE_DEBUG
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000103 reg32 = pci_read_config32(dev, 0x20);
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000104 printk(BIOS_SPEW, " MBL = 0x%08x\n", reg32);
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000105 reg32 = pci_read_config32(dev, 0x24);
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000106 printk(BIOS_SPEW, " PMBL = 0x%08x\n", reg32);
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000107 reg32 = pci_read_config32(dev, 0x28);
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000108 printk(BIOS_SPEW, " PMBU32 = 0x%08x\n", reg32);
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000109 reg32 = pci_read_config32(dev, 0x2c);
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000110 printk(BIOS_SPEW, " PMLU32 = 0x%08x\n", reg32);
Stefan Reinauera8e11682009-03-11 14:54:18 +0000111#endif
112
113 /* Clear errors in status registers */
114 reg16 = pci_read_config16(dev, 0x06);
115 //reg16 |= 0xf900;
116 pci_write_config16(dev, 0x06, reg16);
117
118 reg16 = pci_read_config16(dev, 0x1e);
119 //reg16 |= 0xf900;
120 pci_write_config16(dev, 0x1e, reg16);
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000121}
122
Arthur Heymanse6e5ecb2018-12-20 01:44:50 +0100123static int get_num_ports(void)
124{
125 struct device *dev = pcidev_on_root(31, 0);
126 if (pci_read_config32(dev, FDVCT) & PCIE_4_PORTS_MAX)
127 return 4;
128 else
129 return 6;
130}
131
132static void root_port_init_config(struct device *dev)
133{
134 int rp;
135
136 if (root_port_is_first(dev)) {
137 rpc.orig_rpfn = RCBA32(RPFN);
138 rpc.new_rpfn = rpc.orig_rpfn;
139 rpc.num_ports = get_num_ports();
140 }
141
142 rp = root_port_number(dev);
143 if (rp > rpc.num_ports) {
144 printk(BIOS_ERR, "Found Root Port %d, expecting %d\n",
145 rp, rpc.num_ports);
146 return;
147 }
148
149 /* Cache pci device. */
150 rpc.ports[rp - 1] = dev;
151}
152
153/* Update devicetree with new Root Port function number assignment */
154static void ich_pcie_device_set_func(int index, int pci_func)
155{
156 struct device *dev;
157 unsigned int new_devfn;
158
159 dev = rpc.ports[index];
160
161 /* Set the new PCI function field for this Root Port. */
162 rpc.new_rpfn &= ~RPFN_FNMASK(index);
163 rpc.new_rpfn |= RPFN_FNSET(index, pci_func);
164
165 /* Determine the new devfn for this port */
166 new_devfn = PCI_DEVFN(ICH_PCIE_DEV_SLOT, pci_func);
167
168 if (dev->path.pci.devfn != new_devfn) {
169 printk(BIOS_DEBUG,
170 "ICH: PCIe map %02x.%1x -> %02x.%1x\n",
171 PCI_SLOT(dev->path.pci.devfn),
172 PCI_FUNC(dev->path.pci.devfn),
173 PCI_SLOT(new_devfn), PCI_FUNC(new_devfn));
174
175 dev->path.pci.devfn = new_devfn;
176 }
177}
178
179static void root_port_commit_config(struct device *dev)
180{
181 int i;
182 int coalesce = 0;
183
184 if (dev->chip_info != NULL) {
185 struct southbridge_intel_i82801gx_config *config
186 = dev->chip_info;
187 coalesce = config->pcie_port_coalesce;
188 }
189
190 if (!rpc.ports[0]->enabled)
191 coalesce = 1;
192
193 for (i = 0; i < rpc.num_ports; i++) {
194 struct device *pcie_dev;
195
196 pcie_dev = rpc.ports[i];
197
Jacob Garber14e826f2019-03-12 22:27:52 -0600198 if (pcie_dev == NULL) {
Arthur Heymanse6e5ecb2018-12-20 01:44:50 +0100199 printk(BIOS_ERR, "Root Port %d device is NULL?\n",
200 i + 1);
201 continue;
202 }
203
204 if (pcie_dev->enabled)
205 continue;
206
207 printk(BIOS_DEBUG, "%s: Disabling device\n",
208 dev_path(pcie_dev));
209
210 /* Disable this device if possible */
211 i82801gx_enable(pcie_dev);
212 }
213
214 if (coalesce) {
215 int current_func;
216
217 /* For all Root Ports N enabled ports get assigned the lower
218 * PCI function number. The disabled ones get upper PCI
219 * function numbers. */
220 current_func = 0;
221 for (i = 0; i < rpc.num_ports; i++) {
222 if (!rpc.ports[i]->enabled)
223 continue;
224 ich_pcie_device_set_func(i, current_func);
225 current_func++;
226 }
227
228 /* Allocate the disabled devices' PCI function number. */
229 for (i = 0; i < rpc.num_ports; i++) {
230 if (rpc.ports[i]->enabled)
231 continue;
232 ich_pcie_device_set_func(i, current_func);
233 current_func++;
234 }
235 }
236
237 printk(BIOS_SPEW, "ICH: RPFN 0x%08x -> 0x%08x\n",
238 rpc.orig_rpfn, rpc.new_rpfn);
239 RCBA32(RPFN) = rpc.new_rpfn;
240}
241
242static void ich_pcie_enable(struct device *dev)
243{
244 /* Add this device to the root port config structure. */
245 root_port_init_config(dev);
246
247 /*
248 * When processing the last PCIe root port we can now
249 * update the Root Port Function Number and Hide register.
250 */
251 if (root_port_is_last(dev))
252 root_port_commit_config(dev);
253}
254
255
Elyes HAOUAS99667032018-05-13 12:47:28 +0200256static void pcie_set_subsystem(struct device *dev, unsigned int vendor,
257 unsigned int device)
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000258{
Stefan Reinauera8e11682009-03-11 14:54:18 +0000259 /* NOTE: This is not the default position! */
260 if (!vendor || !device) {
261 pci_write_config32(dev, 0x94,
262 pci_read_config32(dev, 0));
263 } else {
264 pci_write_config32(dev, 0x94,
265 ((device & 0xffff) << 16) | (vendor & 0xffff));
266 }
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000267}
268
269static struct pci_operations pci_ops = {
Stefan Reinauera8e11682009-03-11 14:54:18 +0000270 .set_subsystem = pcie_set_subsystem,
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000271};
272
273static struct device_operations device_ops = {
274 .read_resources = pci_bus_read_resources,
275 .set_resources = pci_dev_set_resources,
276 .enable_resources = pci_bus_enable_resources,
277 .init = pci_init,
Arthur Heymanse6e5ecb2018-12-20 01:44:50 +0100278 .enable = ich_pcie_enable,
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000279 .scan_bus = pci_scan_bridge,
280 .ops_pci = &pci_ops,
281};
282
Patrick Georgiefff7332012-07-26 19:48:23 +0200283static const unsigned short i82801gx_pcie_ids[] = {
284 0x27d0, /* 82801GB/GR/GDH/GBM/GHM (ICH7/ICH7R/ICH7DH/ICH7-M/ICH7-M DH) */
285 0x27d2, /* 82801GB/GR/GDH/GBM/GHM (ICH7/ICH7R/ICH7DH/ICH7-M/ICH7-M DH) */
286 0x27d4, /* 82801GB/GR/GDH/GBM/GHM (ICH7/ICH7R/ICH7DH/ICH7-M/ICH7-M DH) */
287 0x27d6, /* 82801GB/GR/GDH/GBM/GHM (ICH7/ICH7R/ICH7DH/ICH7-M/ICH7-M DH) */
288 0x27e0, /* 82801GR/GDH/GHM (ICH7R/ICH7DH/ICH7-M DH) */
289 0x27e2, /* 82801GR/GDH/GHM (ICH7R/ICH7DH/ICH7-M DH) */
290 0
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000291};
292
Patrick Georgiefff7332012-07-26 19:48:23 +0200293static const struct pci_driver i82801gx_pcie __pci_driver = {
Arthur Heymans3f111b02017-03-09 12:02:52 +0100294 .ops = &device_ops,
295 .vendor = PCI_VENDOR_ID_INTEL,
296 .devices = i82801gx_pcie_ids,
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000297};