blob: c0f9c1239c8cb4c23e5664a9bddd6917c3b80cd5 [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>
20#include <device/pci_ids.h>
Arthur Heymanse6e5ecb2018-12-20 01:44:50 +010021#include "i82801gx.h"
22
23/* Low Power variant has 6 root ports. */
24#define NUM_ROOT_PORTS 6
25
26struct root_port_config {
27 /* RPFN is a write-once register so keep a copy until it is written */
28 u32 orig_rpfn;
29 u32 new_rpfn;
30 int num_ports;
31 struct device *ports[NUM_ROOT_PORTS];
32};
33
34static struct root_port_config rpc;
35
36static inline int root_port_is_first(struct device *dev)
37{
38 return PCI_FUNC(dev->path.pci.devfn) == 0;
39}
40
41static inline int root_port_is_last(struct device *dev)
42{
43 return PCI_FUNC(dev->path.pci.devfn) == (rpc.num_ports - 1);
44}
45
46/* Root ports are numbered 1..N in the documentation. */
47static inline int root_port_number(struct device *dev)
48{
49 return PCI_FUNC(dev->path.pci.devfn) + 1;
50}
Stefan Reinauerdebb11f2008-10-29 04:46:52 +000051
52static void pci_init(struct device *dev)
53{
54 u16 reg16;
55 u32 reg32;
56
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +000057 printk(BIOS_DEBUG, "Initializing ICH7 PCIe bridge.\n");
Stefan Reinauer109ab312009-08-12 16:08:05 +000058
Stefan Reinauera8e11682009-03-11 14:54:18 +000059 /* Enable Bus Master */
60 reg32 = pci_read_config32(dev, PCI_COMMAND);
61 reg32 |= PCI_COMMAND_MASTER;
62 pci_write_config32(dev, PCI_COMMAND, reg32);
Stefan Reinauerdebb11f2008-10-29 04:46:52 +000063
Stefan Reinauera8e11682009-03-11 14:54:18 +000064 /* Set Cache Line Size to 0x10 */
65 // This has no effect but the OS might expect it
66 pci_write_config8(dev, 0x0c, 0x10);
Stefan Reinauerdebb11f2008-10-29 04:46:52 +000067
Stefan Reinauerdebb11f2008-10-29 04:46:52 +000068 reg16 = pci_read_config16(dev, 0x3e);
Stefan Reinauera8e11682009-03-11 14:54:18 +000069 reg16 &= ~(1 << 0); /* disable parity error response */
70 // reg16 &= ~(1 << 1); /* disable SERR */
71 reg16 |= (1 << 2); /* ISA enable */
Stefan Reinauerdebb11f2008-10-29 04:46:52 +000072 pci_write_config16(dev, 0x3e, reg16);
73
Stefan Reinauera8e11682009-03-11 14:54:18 +000074 /* Enable IO xAPIC on this PCIe port */
75 reg32 = pci_read_config32(dev, 0xd8);
76 reg32 |= (1 << 7);
77 pci_write_config32(dev, 0xd8, reg32);
Stefan Reinauerdebb11f2008-10-29 04:46:52 +000078
Stefan Reinauera8e11682009-03-11 14:54:18 +000079 /* Enable Backbone Clock Gating */
80 reg32 = pci_read_config32(dev, 0xe1);
81 reg32 |= (1 << 3) | (1 << 2) | (1 << 1) | (1 << 0);
82 pci_write_config32(dev, 0xe1, reg32);
Stefan Reinauerdebb11f2008-10-29 04:46:52 +000083
Stefan Reinauera8e11682009-03-11 14:54:18 +000084 /* Set VC0 transaction class */
Kyösti Mälkki8aa7e832013-07-26 08:52:10 +030085 reg32 = pci_read_config32(dev, 0x114);
Stefan Reinauera8e11682009-03-11 14:54:18 +000086 reg32 &= 0xffffff00;
87 reg32 |= 1;
Kyösti Mälkki8aa7e832013-07-26 08:52:10 +030088 pci_write_config32(dev, 0x114, reg32);
Stefan Reinauera8e11682009-03-11 14:54:18 +000089
90 /* Mask completion timeouts */
Kyösti Mälkki8aa7e832013-07-26 08:52:10 +030091 reg32 = pci_read_config32(dev, 0x148);
Stefan Reinauera8e11682009-03-11 14:54:18 +000092 reg32 |= (1 << 14);
Kyösti Mälkki8aa7e832013-07-26 08:52:10 +030093 pci_write_config32(dev, 0x148, reg32);
94
Stefan Reinauera8e11682009-03-11 14:54:18 +000095 /* Enable common clock configuration */
96 // Are there cases when we don't want that?
97 reg16 = pci_read_config16(dev, 0x50);
98 reg16 |= (1 << 6);
99 pci_write_config16(dev, 0x50, reg16);
100
Stefan Reinauerde3206a2010-02-22 06:09:43 +0000101#ifdef EVEN_MORE_DEBUG
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000102 reg32 = pci_read_config32(dev, 0x20);
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000103 printk(BIOS_SPEW, " MBL = 0x%08x\n", reg32);
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000104 reg32 = pci_read_config32(dev, 0x24);
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000105 printk(BIOS_SPEW, " PMBL = 0x%08x\n", reg32);
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000106 reg32 = pci_read_config32(dev, 0x28);
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000107 printk(BIOS_SPEW, " PMBU32 = 0x%08x\n", reg32);
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000108 reg32 = pci_read_config32(dev, 0x2c);
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000109 printk(BIOS_SPEW, " PMLU32 = 0x%08x\n", reg32);
Stefan Reinauera8e11682009-03-11 14:54:18 +0000110#endif
111
112 /* Clear errors in status registers */
113 reg16 = pci_read_config16(dev, 0x06);
114 //reg16 |= 0xf900;
115 pci_write_config16(dev, 0x06, reg16);
116
117 reg16 = pci_read_config16(dev, 0x1e);
118 //reg16 |= 0xf900;
119 pci_write_config16(dev, 0x1e, reg16);
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000120}
121
Arthur Heymanse6e5ecb2018-12-20 01:44:50 +0100122static int get_num_ports(void)
123{
124 struct device *dev = pcidev_on_root(31, 0);
125 if (pci_read_config32(dev, FDVCT) & PCIE_4_PORTS_MAX)
126 return 4;
127 else
128 return 6;
129}
130
131static void root_port_init_config(struct device *dev)
132{
133 int rp;
134
135 if (root_port_is_first(dev)) {
136 rpc.orig_rpfn = RCBA32(RPFN);
137 rpc.new_rpfn = rpc.orig_rpfn;
138 rpc.num_ports = get_num_ports();
139 }
140
141 rp = root_port_number(dev);
142 if (rp > rpc.num_ports) {
143 printk(BIOS_ERR, "Found Root Port %d, expecting %d\n",
144 rp, rpc.num_ports);
145 return;
146 }
147
148 /* Cache pci device. */
149 rpc.ports[rp - 1] = dev;
150}
151
152/* Update devicetree with new Root Port function number assignment */
153static void ich_pcie_device_set_func(int index, int pci_func)
154{
155 struct device *dev;
156 unsigned int new_devfn;
157
158 dev = rpc.ports[index];
159
160 /* Set the new PCI function field for this Root Port. */
161 rpc.new_rpfn &= ~RPFN_FNMASK(index);
162 rpc.new_rpfn |= RPFN_FNSET(index, pci_func);
163
164 /* Determine the new devfn for this port */
165 new_devfn = PCI_DEVFN(ICH_PCIE_DEV_SLOT, pci_func);
166
167 if (dev->path.pci.devfn != new_devfn) {
168 printk(BIOS_DEBUG,
169 "ICH: PCIe map %02x.%1x -> %02x.%1x\n",
170 PCI_SLOT(dev->path.pci.devfn),
171 PCI_FUNC(dev->path.pci.devfn),
172 PCI_SLOT(new_devfn), PCI_FUNC(new_devfn));
173
174 dev->path.pci.devfn = new_devfn;
175 }
176}
177
178static void root_port_commit_config(struct device *dev)
179{
180 int i;
181 int coalesce = 0;
182
183 if (dev->chip_info != NULL) {
184 struct southbridge_intel_i82801gx_config *config
185 = dev->chip_info;
186 coalesce = config->pcie_port_coalesce;
187 }
188
189 if (!rpc.ports[0]->enabled)
190 coalesce = 1;
191
192 for (i = 0; i < rpc.num_ports; i++) {
193 struct device *pcie_dev;
194
195 pcie_dev = rpc.ports[i];
196
197 if (dev == NULL) {
198 printk(BIOS_ERR, "Root Port %d device is NULL?\n",
199 i + 1);
200 continue;
201 }
202
203 if (pcie_dev->enabled)
204 continue;
205
206 printk(BIOS_DEBUG, "%s: Disabling device\n",
207 dev_path(pcie_dev));
208
209 /* Disable this device if possible */
210 i82801gx_enable(pcie_dev);
211 }
212
213 if (coalesce) {
214 int current_func;
215
216 /* For all Root Ports N enabled ports get assigned the lower
217 * PCI function number. The disabled ones get upper PCI
218 * function numbers. */
219 current_func = 0;
220 for (i = 0; i < rpc.num_ports; i++) {
221 if (!rpc.ports[i]->enabled)
222 continue;
223 ich_pcie_device_set_func(i, current_func);
224 current_func++;
225 }
226
227 /* Allocate the disabled devices' PCI function number. */
228 for (i = 0; i < rpc.num_ports; i++) {
229 if (rpc.ports[i]->enabled)
230 continue;
231 ich_pcie_device_set_func(i, current_func);
232 current_func++;
233 }
234 }
235
236 printk(BIOS_SPEW, "ICH: RPFN 0x%08x -> 0x%08x\n",
237 rpc.orig_rpfn, rpc.new_rpfn);
238 RCBA32(RPFN) = rpc.new_rpfn;
239}
240
241static void ich_pcie_enable(struct device *dev)
242{
243 /* Add this device to the root port config structure. */
244 root_port_init_config(dev);
245
246 /*
247 * When processing the last PCIe root port we can now
248 * update the Root Port Function Number and Hide register.
249 */
250 if (root_port_is_last(dev))
251 root_port_commit_config(dev);
252}
253
254
Elyes HAOUAS99667032018-05-13 12:47:28 +0200255static void pcie_set_subsystem(struct device *dev, unsigned int vendor,
256 unsigned int device)
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000257{
Stefan Reinauera8e11682009-03-11 14:54:18 +0000258 /* NOTE: This is not the default position! */
259 if (!vendor || !device) {
260 pci_write_config32(dev, 0x94,
261 pci_read_config32(dev, 0));
262 } else {
263 pci_write_config32(dev, 0x94,
264 ((device & 0xffff) << 16) | (vendor & 0xffff));
265 }
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000266}
267
268static struct pci_operations pci_ops = {
Stefan Reinauera8e11682009-03-11 14:54:18 +0000269 .set_subsystem = pcie_set_subsystem,
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000270};
271
272static struct device_operations device_ops = {
273 .read_resources = pci_bus_read_resources,
274 .set_resources = pci_dev_set_resources,
275 .enable_resources = pci_bus_enable_resources,
276 .init = pci_init,
Arthur Heymanse6e5ecb2018-12-20 01:44:50 +0100277 .enable = ich_pcie_enable,
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000278 .scan_bus = pci_scan_bridge,
279 .ops_pci = &pci_ops,
280};
281
Patrick Georgiefff7332012-07-26 19:48:23 +0200282static const unsigned short i82801gx_pcie_ids[] = {
283 0x27d0, /* 82801GB/GR/GDH/GBM/GHM (ICH7/ICH7R/ICH7DH/ICH7-M/ICH7-M DH) */
284 0x27d2, /* 82801GB/GR/GDH/GBM/GHM (ICH7/ICH7R/ICH7DH/ICH7-M/ICH7-M DH) */
285 0x27d4, /* 82801GB/GR/GDH/GBM/GHM (ICH7/ICH7R/ICH7DH/ICH7-M/ICH7-M DH) */
286 0x27d6, /* 82801GB/GR/GDH/GBM/GHM (ICH7/ICH7R/ICH7DH/ICH7-M/ICH7-M DH) */
287 0x27e0, /* 82801GR/GDH/GHM (ICH7R/ICH7DH/ICH7-M DH) */
288 0x27e2, /* 82801GR/GDH/GHM (ICH7R/ICH7DH/ICH7-M DH) */
289 0
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000290};
291
Patrick Georgiefff7332012-07-26 19:48:23 +0200292static const struct pci_driver i82801gx_pcie __pci_driver = {
Arthur Heymans3f111b02017-03-09 12:02:52 +0100293 .ops = &device_ops,
294 .vendor = PCI_VENDOR_ID_INTEL,
295 .devices = i82801gx_pcie_ids,
Stefan Reinauerdebb11f2008-10-29 04:46:52 +0000296};