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)(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 */
46 return PCIBIOS_DEVICE_NOT_FOUND;
50 v |= 2; /* EA entry-1. Base-L */
51 set_val(v, where, size, val);
52 return PCIBIOS_SUCCESSFUL;
58 addr = bus->ops->map_bus(bus, devfn, bar); /* BAR 0 */
61 return PCIBIOS_DEVICE_NOT_FOUND;
63 barl_orig = readl(addr + 0);
64 writel(0xffffffff, addr + 0);
65 barl_rb = readl(addr + 0);
66 writel(barl_orig, addr + 0);
67 /* zeros in unsettable bits */
69 v |= 0xc; /* EA entry-2. Offset-L */
70 set_val(v, where, size, val);
71 return PCIBIOS_SUCCESSFUL;
74 addr = bus->ops->map_bus(bus, devfn, bar + 4); /* BAR 1 */
77 return PCIBIOS_DEVICE_NOT_FOUND;
79 v = readl(addr); /* EA entry-3. Base-H */
80 set_val(v, where, size, val);
81 return PCIBIOS_SUCCESSFUL;
83 return PCIBIOS_DEVICE_NOT_FOUND;
86 static int thunder_ecam_p2_config_read(struct pci_bus *bus, unsigned int devfn,
87 int where, int size, u32 *val)
89 struct pci_config_window *cfg = bus->sysdata;
90 int where_a = where & ~3;
95 /* EA Base[63:32] may be missing some bits ... */
103 return pci_generic_config_read(bus, devfn, where, size, val);
106 addr = bus->ops->map_bus(bus, devfn, where_a);
109 return PCIBIOS_DEVICE_NOT_FOUND;
115 * Bit 44 of the 64-bit Base must match the same bit in
116 * the config space access window. Since we are working with
117 * the high-order 32 bits, shift everything down by 32 bits.
119 node_bits = (cfg->res.start >> 32) & (1 << 12);
122 set_val(v, where, size, val);
124 return PCIBIOS_SUCCESSFUL;
127 static int thunder_ecam_config_read(struct pci_bus *bus, unsigned int devfn,
128 int where, int size, u32 *val)
135 int where_a = where & ~3;
137 addr = bus->ops->map_bus(bus, devfn, 0xc);
140 return PCIBIOS_DEVICE_NOT_FOUND;
145 /* Check for non type-00 header */
146 cfg_type = (v >> 16) & 0x7f;
148 addr = bus->ops->map_bus(bus, devfn, 8);
151 return PCIBIOS_DEVICE_NOT_FOUND;
154 class_rev = readl(addr);
155 if (class_rev == 0xffffffff)
158 if ((class_rev & 0xff) >= 8) {
159 /* Pass-2 handling */
162 return thunder_ecam_p2_config_read(bus, devfn, where,
167 * All BARs have fixed addresses specified by the EA
168 * capability; they must return zero on read.
171 ((where >= 0x10 && where < 0x2c) ||
172 (where >= 0x1a4 && where < 0x1bc))) {
173 /* BAR or SR-IOV BAR */
175 return PCIBIOS_SUCCESSFUL;
178 addr = bus->ops->map_bus(bus, devfn, 0);
181 return PCIBIOS_DEVICE_NOT_FOUND;
184 vendor_device = readl(addr);
185 if (vendor_device == 0xffffffff)
188 pr_debug("%04x:%04x - Fix pass#: %08x, where: %03x, devfn: %03x\n",
189 vendor_device & 0xffff, vendor_device >> 16, class_rev,
190 (unsigned) where, devfn);
192 /* Check for non type-00 header */
195 bool is_nic = (vendor_device == 0xa01e177d);
196 bool is_tns = (vendor_device == 0xa01f177d);
198 addr = bus->ops->map_bus(bus, devfn, 0x70);
201 return PCIBIOS_DEVICE_NOT_FOUND;
205 has_msix = (v & 0xff00) != 0;
207 if (!has_msix && where_a == 0x70) {
208 v |= 0xbc00; /* next capability is EA at 0xbc */
209 set_val(v, where, size, val);
210 return PCIBIOS_SUCCESSFUL;
212 if (where_a == 0xb0) {
213 addr = bus->ops->map_bus(bus, devfn, where_a);
216 return PCIBIOS_DEVICE_NOT_FOUND;
220 pr_err("Bad MSIX cap header: %08x\n", v);
221 v |= 0xbc00; /* next capability is EA at 0xbc */
222 set_val(v, where, size, val);
223 return PCIBIOS_SUCCESSFUL;
225 if (where_a == 0xbc) {
227 v = 0x40014; /* EA last in chain, 4 entries */
229 v = 0x30014; /* EA last in chain, 3 entries */
231 v = 0x20014; /* EA last in chain, 2 entries */
233 v = 0x10014; /* EA last in chain, 1 entry */
234 set_val(v, where, size, val);
235 return PCIBIOS_SUCCESSFUL;
237 if (where_a >= 0xc0 && where_a < 0xd0)
238 /* EA entry-0. PP=0, BAR0 Size:3 */
239 return handle_ea_bar(0x80ff0003,
240 0x10, bus, devfn, where,
242 if (where_a >= 0xd0 && where_a < 0xe0 && has_msix)
243 /* EA entry-1. PP=0, BAR4 Size:3 */
244 return handle_ea_bar(0x80ff0043,
245 0x20, bus, devfn, where,
247 if (where_a >= 0xe0 && where_a < 0xf0 && is_tns)
248 /* EA entry-2. PP=0, BAR2, Size:3 */
249 return handle_ea_bar(0x80ff0023,
250 0x18, bus, devfn, where,
252 if (where_a >= 0xe0 && where_a < 0xf0 && is_nic)
253 /* EA entry-2. PP=4, VF_BAR0 (9), Size:3 */
254 return handle_ea_bar(0x80ff0493,
255 0x1a4, bus, devfn, where,
257 if (where_a >= 0xf0 && where_a < 0x100 && is_nic)
258 /* EA entry-3. PP=4, VF_BAR4 (d), Size:3 */
259 return handle_ea_bar(0x80ff04d3,
260 0x1b4, bus, devfn, where,
262 } else if (cfg_type == 1) {
263 bool is_rsl_bridge = devfn == 0x08;
264 bool is_rad_bridge = devfn == 0xa0;
265 bool is_zip_bridge = devfn == 0xa8;
266 bool is_dfa_bridge = devfn == 0xb0;
267 bool is_nic_bridge = devfn == 0x10;
269 if (where_a == 0x70) {
270 addr = bus->ops->map_bus(bus, devfn, where_a);
273 return PCIBIOS_DEVICE_NOT_FOUND;
277 pr_err("Bad PCIe cap header: %08x\n", v);
278 v |= 0xbc00; /* next capability is EA at 0xbc */
279 set_val(v, where, size, val);
280 return PCIBIOS_SUCCESSFUL;
282 if (where_a == 0xbc) {
284 v = 0x10014; /* EA last in chain, 1 entry */
286 v = 0x00014; /* EA last in chain, no entries */
287 set_val(v, where, size, val);
288 return PCIBIOS_SUCCESSFUL;
290 if (where_a == 0xc0) {
291 if (is_rsl_bridge || is_nic_bridge)
292 v = 0x0101; /* subordinate:secondary = 1:1 */
293 else if (is_rad_bridge)
294 v = 0x0202; /* subordinate:secondary = 2:2 */
295 else if (is_zip_bridge)
296 v = 0x0303; /* subordinate:secondary = 3:3 */
297 else if (is_dfa_bridge)
298 v = 0x0404; /* subordinate:secondary = 4:4 */
299 set_val(v, where, size, val);
300 return PCIBIOS_SUCCESSFUL;
302 if (where_a == 0xc4 && is_nic_bridge) {
303 /* Enabled, not-Write, SP=ff, PP=05, BEI=6, ES=4 */
305 set_val(v, where, size, val);
306 return PCIBIOS_SUCCESSFUL;
308 if (where_a == 0xc8 && is_nic_bridge) {
309 v = 0x00000002; /* Base-L 64-bit */
310 set_val(v, where, size, val);
311 return PCIBIOS_SUCCESSFUL;
313 if (where_a == 0xcc && is_nic_bridge) {
314 v = 0xfffffffe; /* MaxOffset-L 64-bit */
315 set_val(v, where, size, val);
316 return PCIBIOS_SUCCESSFUL;
318 if (where_a == 0xd0 && is_nic_bridge) {
319 v = 0x00008430; /* NIC Base-H */
320 set_val(v, where, size, val);
321 return PCIBIOS_SUCCESSFUL;
323 if (where_a == 0xd4 && is_nic_bridge) {
324 v = 0x0000000f; /* MaxOffset-H */
325 set_val(v, where, size, val);
326 return PCIBIOS_SUCCESSFUL;
330 return pci_generic_config_read(bus, devfn, where, size, val);
333 static int thunder_ecam_config_write(struct pci_bus *bus, unsigned int devfn,
334 int where, int size, u32 val)
337 * All BARs have fixed addresses; ignore BAR writes so they
338 * don't get corrupted.
340 if ((where >= 0x10 && where < 0x2c) ||
341 (where >= 0x1a4 && where < 0x1bc))
342 /* BAR or SR-IOV BAR */
343 return PCIBIOS_SUCCESSFUL;
345 return pci_generic_config_write(bus, devfn, where, size, val);
348 const struct pci_ecam_ops pci_thunder_ecam_ops = {
350 .map_bus = pci_ecam_map_bus,
351 .read = thunder_ecam_config_read,
352 .write = thunder_ecam_config_write,
356 #ifdef CONFIG_PCI_HOST_THUNDER_ECAM
358 static const struct of_device_id thunder_ecam_of_match[] = {
360 .compatible = "cavium,pci-host-thunder-ecam",
361 .data = &pci_thunder_ecam_ops,
366 static struct platform_driver thunder_ecam_driver = {
368 .name = KBUILD_MODNAME,
369 .of_match_table = thunder_ecam_of_match,
370 .suppress_bind_attrs = true,
372 .probe = pci_host_common_probe,
374 builtin_platform_driver(thunder_ecam_driver);