1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2015, 2016 Cavium, Inc.
6 #include <linux/kernel.h>
7 #include <linux/init.h>
8 #include <linux/ioport.h>
9 #include <linux/of_pci.h>
11 #include <linux/pci-ecam.h>
12 #include <linux/platform_device.h>
14 #if defined(CONFIG_PCI_HOST_THUNDER_ECAM) || (defined(CONFIG_ACPI) && defined(CONFIG_PCI_QUIRKS))
16 static void set_val(u32 v, int where, int size, u32 *val)
18 int shift = (where & 3) * 8;
20 pr_debug("set_val %04x: %08x\n", (unsigned int)(where & ~3), v);
29 static int handle_ea_bar(u32 e0, int bar, struct pci_bus *bus,
30 unsigned int devfn, int where, int size, u32 *val)
35 /* Entries are 16-byte aligned; bits[2,3] select word in entry */
36 int where_a = where & 0xc;
39 set_val(e0, where, size, val);
40 return PCIBIOS_SUCCESSFUL;
43 addr = bus->ops->map_bus(bus, devfn, bar); /* BAR 0 */
45 return PCIBIOS_DEVICE_NOT_FOUND;
49 v |= 2; /* EA entry-1. Base-L */
50 set_val(v, where, size, val);
51 return PCIBIOS_SUCCESSFUL;
57 addr = bus->ops->map_bus(bus, devfn, bar); /* BAR 0 */
59 return PCIBIOS_DEVICE_NOT_FOUND;
61 barl_orig = readl(addr + 0);
62 writel(0xffffffff, addr + 0);
63 barl_rb = readl(addr + 0);
64 writel(barl_orig, addr + 0);
65 /* zeros in unsettable bits */
67 v |= 0xc; /* EA entry-2. Offset-L */
68 set_val(v, where, size, val);
69 return PCIBIOS_SUCCESSFUL;
72 addr = bus->ops->map_bus(bus, devfn, bar + 4); /* BAR 1 */
74 return PCIBIOS_DEVICE_NOT_FOUND;
76 v = readl(addr); /* EA entry-3. Base-H */
77 set_val(v, where, size, val);
78 return PCIBIOS_SUCCESSFUL;
80 return PCIBIOS_DEVICE_NOT_FOUND;
83 static int thunder_ecam_p2_config_read(struct pci_bus *bus, unsigned int devfn,
84 int where, int size, u32 *val)
86 struct pci_config_window *cfg = bus->sysdata;
87 int where_a = where & ~3;
92 /* EA Base[63:32] may be missing some bits ... */
100 return pci_generic_config_read(bus, devfn, where, size, val);
103 addr = bus->ops->map_bus(bus, devfn, where_a);
105 return PCIBIOS_DEVICE_NOT_FOUND;
110 * Bit 44 of the 64-bit Base must match the same bit in
111 * the config space access window. Since we are working with
112 * the high-order 32 bits, shift everything down by 32 bits.
114 node_bits = upper_32_bits(cfg->res.start) & (1 << 12);
117 set_val(v, where, size, val);
119 return PCIBIOS_SUCCESSFUL;
122 static int thunder_ecam_config_read(struct pci_bus *bus, unsigned int devfn,
123 int where, int size, u32 *val)
130 int where_a = where & ~3;
132 addr = bus->ops->map_bus(bus, devfn, 0xc);
134 return PCIBIOS_DEVICE_NOT_FOUND;
138 /* Check for non type-00 header */
139 cfg_type = (v >> 16) & 0x7f;
141 addr = bus->ops->map_bus(bus, devfn, 8);
143 return PCIBIOS_DEVICE_NOT_FOUND;
145 class_rev = readl(addr);
146 if (class_rev == 0xffffffff)
149 if ((class_rev & 0xff) >= 8) {
150 /* Pass-2 handling */
153 return thunder_ecam_p2_config_read(bus, devfn, where,
158 * All BARs have fixed addresses specified by the EA
159 * capability; they must return zero on read.
162 ((where >= 0x10 && where < 0x2c) ||
163 (where >= 0x1a4 && where < 0x1bc))) {
164 /* BAR or SR-IOV BAR */
166 return PCIBIOS_SUCCESSFUL;
169 addr = bus->ops->map_bus(bus, devfn, 0);
171 return PCIBIOS_DEVICE_NOT_FOUND;
173 vendor_device = readl(addr);
174 if (vendor_device == 0xffffffff)
177 pr_debug("%04x:%04x - Fix pass#: %08x, where: %03x, devfn: %03x\n",
178 vendor_device & 0xffff, vendor_device >> 16, class_rev,
179 (unsigned int)where, devfn);
181 /* Check for non type-00 header */
184 bool is_nic = (vendor_device == 0xa01e177d);
185 bool is_tns = (vendor_device == 0xa01f177d);
187 addr = bus->ops->map_bus(bus, devfn, 0x70);
189 return PCIBIOS_DEVICE_NOT_FOUND;
193 has_msix = (v & 0xff00) != 0;
195 if (!has_msix && where_a == 0x70) {
196 v |= 0xbc00; /* next capability is EA at 0xbc */
197 set_val(v, where, size, val);
198 return PCIBIOS_SUCCESSFUL;
200 if (where_a == 0xb0) {
201 addr = bus->ops->map_bus(bus, devfn, where_a);
203 return PCIBIOS_DEVICE_NOT_FOUND;
207 pr_err("Bad MSIX cap header: %08x\n", v);
208 v |= 0xbc00; /* next capability is EA at 0xbc */
209 set_val(v, where, size, val);
210 return PCIBIOS_SUCCESSFUL;
212 if (where_a == 0xbc) {
214 v = 0x40014; /* EA last in chain, 4 entries */
216 v = 0x30014; /* EA last in chain, 3 entries */
218 v = 0x20014; /* EA last in chain, 2 entries */
220 v = 0x10014; /* EA last in chain, 1 entry */
221 set_val(v, where, size, val);
222 return PCIBIOS_SUCCESSFUL;
224 if (where_a >= 0xc0 && where_a < 0xd0)
225 /* EA entry-0. PP=0, BAR0 Size:3 */
226 return handle_ea_bar(0x80ff0003,
227 0x10, bus, devfn, where,
229 if (where_a >= 0xd0 && where_a < 0xe0 && has_msix)
230 /* EA entry-1. PP=0, BAR4 Size:3 */
231 return handle_ea_bar(0x80ff0043,
232 0x20, bus, devfn, where,
234 if (where_a >= 0xe0 && where_a < 0xf0 && is_tns)
235 /* EA entry-2. PP=0, BAR2, Size:3 */
236 return handle_ea_bar(0x80ff0023,
237 0x18, bus, devfn, where,
239 if (where_a >= 0xe0 && where_a < 0xf0 && is_nic)
240 /* EA entry-2. PP=4, VF_BAR0 (9), Size:3 */
241 return handle_ea_bar(0x80ff0493,
242 0x1a4, bus, devfn, where,
244 if (where_a >= 0xf0 && where_a < 0x100 && is_nic)
245 /* EA entry-3. PP=4, VF_BAR4 (d), Size:3 */
246 return handle_ea_bar(0x80ff04d3,
247 0x1b4, bus, devfn, where,
249 } else if (cfg_type == 1) {
250 bool is_rsl_bridge = devfn == 0x08;
251 bool is_rad_bridge = devfn == 0xa0;
252 bool is_zip_bridge = devfn == 0xa8;
253 bool is_dfa_bridge = devfn == 0xb0;
254 bool is_nic_bridge = devfn == 0x10;
256 if (where_a == 0x70) {
257 addr = bus->ops->map_bus(bus, devfn, where_a);
259 return PCIBIOS_DEVICE_NOT_FOUND;
263 pr_err("Bad PCIe cap header: %08x\n", v);
264 v |= 0xbc00; /* next capability is EA at 0xbc */
265 set_val(v, where, size, val);
266 return PCIBIOS_SUCCESSFUL;
268 if (where_a == 0xbc) {
270 v = 0x10014; /* EA last in chain, 1 entry */
272 v = 0x00014; /* EA last in chain, no entries */
273 set_val(v, where, size, val);
274 return PCIBIOS_SUCCESSFUL;
276 if (where_a == 0xc0) {
277 if (is_rsl_bridge || is_nic_bridge)
278 v = 0x0101; /* subordinate:secondary = 1:1 */
279 else if (is_rad_bridge)
280 v = 0x0202; /* subordinate:secondary = 2:2 */
281 else if (is_zip_bridge)
282 v = 0x0303; /* subordinate:secondary = 3:3 */
283 else if (is_dfa_bridge)
284 v = 0x0404; /* subordinate:secondary = 4:4 */
285 set_val(v, where, size, val);
286 return PCIBIOS_SUCCESSFUL;
288 if (where_a == 0xc4 && is_nic_bridge) {
289 /* Enabled, not-Write, SP=ff, PP=05, BEI=6, ES=4 */
291 set_val(v, where, size, val);
292 return PCIBIOS_SUCCESSFUL;
294 if (where_a == 0xc8 && is_nic_bridge) {
295 v = 0x00000002; /* Base-L 64-bit */
296 set_val(v, where, size, val);
297 return PCIBIOS_SUCCESSFUL;
299 if (where_a == 0xcc && is_nic_bridge) {
300 v = 0xfffffffe; /* MaxOffset-L 64-bit */
301 set_val(v, where, size, val);
302 return PCIBIOS_SUCCESSFUL;
304 if (where_a == 0xd0 && is_nic_bridge) {
305 v = 0x00008430; /* NIC Base-H */
306 set_val(v, where, size, val);
307 return PCIBIOS_SUCCESSFUL;
309 if (where_a == 0xd4 && is_nic_bridge) {
310 v = 0x0000000f; /* MaxOffset-H */
311 set_val(v, where, size, val);
312 return PCIBIOS_SUCCESSFUL;
316 return pci_generic_config_read(bus, devfn, where, size, val);
319 static int thunder_ecam_config_write(struct pci_bus *bus, unsigned int devfn,
320 int where, int size, u32 val)
323 * All BARs have fixed addresses; ignore BAR writes so they
324 * don't get corrupted.
326 if ((where >= 0x10 && where < 0x2c) ||
327 (where >= 0x1a4 && where < 0x1bc))
328 /* BAR or SR-IOV BAR */
329 return PCIBIOS_SUCCESSFUL;
331 return pci_generic_config_write(bus, devfn, where, size, val);
334 const struct pci_ecam_ops pci_thunder_ecam_ops = {
336 .map_bus = pci_ecam_map_bus,
337 .read = thunder_ecam_config_read,
338 .write = thunder_ecam_config_write,
342 #ifdef CONFIG_PCI_HOST_THUNDER_ECAM
344 static const struct of_device_id thunder_ecam_of_match[] = {
346 .compatible = "cavium,pci-host-thunder-ecam",
347 .data = &pci_thunder_ecam_ops,
352 static struct platform_driver thunder_ecam_driver = {
354 .name = KBUILD_MODNAME,
355 .of_match_table = thunder_ecam_of_match,
356 .suppress_bind_attrs = true,
358 .probe = pci_host_common_probe,
360 builtin_platform_driver(thunder_ecam_driver);