2 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
6 * Copyright (C) 2015, 2016 Cavium, Inc.
9 #include <linux/kernel.h>
10 #include <linux/init.h>
11 #include <linux/ioport.h>
12 #include <linux/of_pci.h>
14 #include <linux/pci-ecam.h>
15 #include <linux/platform_device.h>
17 #if defined(CONFIG_PCI_HOST_THUNDER_ECAM) || (defined(CONFIG_ACPI) && defined(CONFIG_PCI_QUIRKS))
19 static void set_val(u32 v, int where, int size, u32 *val)
21 int shift = (where & 3) * 8;
23 pr_debug("set_val %04x: %08x\n", (unsigned)(where & ~3), v);
32 static int handle_ea_bar(u32 e0, int bar, struct pci_bus *bus,
33 unsigned int devfn, int where, int size, u32 *val)
38 /* Entries are 16-byte aligned; bits[2,3] select word in entry */
39 int where_a = where & 0xc;
42 set_val(e0, where, size, val);
43 return PCIBIOS_SUCCESSFUL;
46 addr = bus->ops->map_bus(bus, devfn, bar); /* BAR 0 */
49 return PCIBIOS_DEVICE_NOT_FOUND;
53 v |= 2; /* EA entry-1. Base-L */
54 set_val(v, where, size, val);
55 return PCIBIOS_SUCCESSFUL;
61 addr = bus->ops->map_bus(bus, devfn, bar); /* BAR 0 */
64 return PCIBIOS_DEVICE_NOT_FOUND;
66 barl_orig = readl(addr + 0);
67 writel(0xffffffff, addr + 0);
68 barl_rb = readl(addr + 0);
69 writel(barl_orig, addr + 0);
70 /* zeros in unsettable bits */
72 v |= 0xc; /* EA entry-2. Offset-L */
73 set_val(v, where, size, val);
74 return PCIBIOS_SUCCESSFUL;
77 addr = bus->ops->map_bus(bus, devfn, bar + 4); /* BAR 1 */
80 return PCIBIOS_DEVICE_NOT_FOUND;
82 v = readl(addr); /* EA entry-3. Base-H */
83 set_val(v, where, size, val);
84 return PCIBIOS_SUCCESSFUL;
86 return PCIBIOS_DEVICE_NOT_FOUND;
89 static int thunder_ecam_p2_config_read(struct pci_bus *bus, unsigned int devfn,
90 int where, int size, u32 *val)
92 struct pci_config_window *cfg = bus->sysdata;
93 int where_a = where & ~3;
98 /* EA Base[63:32] may be missing some bits ... */
106 return pci_generic_config_read(bus, devfn, where, size, val);
109 addr = bus->ops->map_bus(bus, devfn, where_a);
112 return PCIBIOS_DEVICE_NOT_FOUND;
118 * Bit 44 of the 64-bit Base must match the same bit in
119 * the config space access window. Since we are working with
120 * the high-order 32 bits, shift everything down by 32 bits.
122 node_bits = upper_32_bits(cfg->res.start) & (1 << 12);
125 set_val(v, where, size, val);
127 return PCIBIOS_SUCCESSFUL;
130 static int thunder_ecam_config_read(struct pci_bus *bus, unsigned int devfn,
131 int where, int size, u32 *val)
138 int where_a = where & ~3;
140 addr = bus->ops->map_bus(bus, devfn, 0xc);
143 return PCIBIOS_DEVICE_NOT_FOUND;
148 /* Check for non type-00 header */
149 cfg_type = (v >> 16) & 0x7f;
151 addr = bus->ops->map_bus(bus, devfn, 8);
154 return PCIBIOS_DEVICE_NOT_FOUND;
157 class_rev = readl(addr);
158 if (class_rev == 0xffffffff)
161 if ((class_rev & 0xff) >= 8) {
162 /* Pass-2 handling */
165 return thunder_ecam_p2_config_read(bus, devfn, where,
170 * All BARs have fixed addresses specified by the EA
171 * capability; they must return zero on read.
174 ((where >= 0x10 && where < 0x2c) ||
175 (where >= 0x1a4 && where < 0x1bc))) {
176 /* BAR or SR-IOV BAR */
178 return PCIBIOS_SUCCESSFUL;
181 addr = bus->ops->map_bus(bus, devfn, 0);
184 return PCIBIOS_DEVICE_NOT_FOUND;
187 vendor_device = readl(addr);
188 if (vendor_device == 0xffffffff)
191 pr_debug("%04x:%04x - Fix pass#: %08x, where: %03x, devfn: %03x\n",
192 vendor_device & 0xffff, vendor_device >> 16, class_rev,
193 (unsigned) where, devfn);
195 /* Check for non type-00 header */
198 bool is_nic = (vendor_device == 0xa01e177d);
199 bool is_tns = (vendor_device == 0xa01f177d);
201 addr = bus->ops->map_bus(bus, devfn, 0x70);
204 return PCIBIOS_DEVICE_NOT_FOUND;
208 has_msix = (v & 0xff00) != 0;
210 if (!has_msix && where_a == 0x70) {
211 v |= 0xbc00; /* next capability is EA at 0xbc */
212 set_val(v, where, size, val);
213 return PCIBIOS_SUCCESSFUL;
215 if (where_a == 0xb0) {
216 addr = bus->ops->map_bus(bus, devfn, where_a);
219 return PCIBIOS_DEVICE_NOT_FOUND;
223 pr_err("Bad MSIX cap header: %08x\n", v);
224 v |= 0xbc00; /* next capability is EA at 0xbc */
225 set_val(v, where, size, val);
226 return PCIBIOS_SUCCESSFUL;
228 if (where_a == 0xbc) {
230 v = 0x40014; /* EA last in chain, 4 entries */
232 v = 0x30014; /* EA last in chain, 3 entries */
234 v = 0x20014; /* EA last in chain, 2 entries */
236 v = 0x10014; /* EA last in chain, 1 entry */
237 set_val(v, where, size, val);
238 return PCIBIOS_SUCCESSFUL;
240 if (where_a >= 0xc0 && where_a < 0xd0)
241 /* EA entry-0. PP=0, BAR0 Size:3 */
242 return handle_ea_bar(0x80ff0003,
243 0x10, bus, devfn, where,
245 if (where_a >= 0xd0 && where_a < 0xe0 && has_msix)
246 /* EA entry-1. PP=0, BAR4 Size:3 */
247 return handle_ea_bar(0x80ff0043,
248 0x20, bus, devfn, where,
250 if (where_a >= 0xe0 && where_a < 0xf0 && is_tns)
251 /* EA entry-2. PP=0, BAR2, Size:3 */
252 return handle_ea_bar(0x80ff0023,
253 0x18, bus, devfn, where,
255 if (where_a >= 0xe0 && where_a < 0xf0 && is_nic)
256 /* EA entry-2. PP=4, VF_BAR0 (9), Size:3 */
257 return handle_ea_bar(0x80ff0493,
258 0x1a4, bus, devfn, where,
260 if (where_a >= 0xf0 && where_a < 0x100 && is_nic)
261 /* EA entry-3. PP=4, VF_BAR4 (d), Size:3 */
262 return handle_ea_bar(0x80ff04d3,
263 0x1b4, bus, devfn, where,
265 } else if (cfg_type == 1) {
266 bool is_rsl_bridge = devfn == 0x08;
267 bool is_rad_bridge = devfn == 0xa0;
268 bool is_zip_bridge = devfn == 0xa8;
269 bool is_dfa_bridge = devfn == 0xb0;
270 bool is_nic_bridge = devfn == 0x10;
272 if (where_a == 0x70) {
273 addr = bus->ops->map_bus(bus, devfn, where_a);
276 return PCIBIOS_DEVICE_NOT_FOUND;
280 pr_err("Bad PCIe cap header: %08x\n", v);
281 v |= 0xbc00; /* next capability is EA at 0xbc */
282 set_val(v, where, size, val);
283 return PCIBIOS_SUCCESSFUL;
285 if (where_a == 0xbc) {
287 v = 0x10014; /* EA last in chain, 1 entry */
289 v = 0x00014; /* EA last in chain, no entries */
290 set_val(v, where, size, val);
291 return PCIBIOS_SUCCESSFUL;
293 if (where_a == 0xc0) {
294 if (is_rsl_bridge || is_nic_bridge)
295 v = 0x0101; /* subordinate:secondary = 1:1 */
296 else if (is_rad_bridge)
297 v = 0x0202; /* subordinate:secondary = 2:2 */
298 else if (is_zip_bridge)
299 v = 0x0303; /* subordinate:secondary = 3:3 */
300 else if (is_dfa_bridge)
301 v = 0x0404; /* subordinate:secondary = 4:4 */
302 set_val(v, where, size, val);
303 return PCIBIOS_SUCCESSFUL;
305 if (where_a == 0xc4 && is_nic_bridge) {
306 /* Enabled, not-Write, SP=ff, PP=05, BEI=6, ES=4 */
308 set_val(v, where, size, val);
309 return PCIBIOS_SUCCESSFUL;
311 if (where_a == 0xc8 && is_nic_bridge) {
312 v = 0x00000002; /* Base-L 64-bit */
313 set_val(v, where, size, val);
314 return PCIBIOS_SUCCESSFUL;
316 if (where_a == 0xcc && is_nic_bridge) {
317 v = 0xfffffffe; /* MaxOffset-L 64-bit */
318 set_val(v, where, size, val);
319 return PCIBIOS_SUCCESSFUL;
321 if (where_a == 0xd0 && is_nic_bridge) {
322 v = 0x00008430; /* NIC Base-H */
323 set_val(v, where, size, val);
324 return PCIBIOS_SUCCESSFUL;
326 if (where_a == 0xd4 && is_nic_bridge) {
327 v = 0x0000000f; /* MaxOffset-H */
328 set_val(v, where, size, val);
329 return PCIBIOS_SUCCESSFUL;
333 return pci_generic_config_read(bus, devfn, where, size, val);
336 static int thunder_ecam_config_write(struct pci_bus *bus, unsigned int devfn,
337 int where, int size, u32 val)
340 * All BARs have fixed addresses; ignore BAR writes so they
341 * don't get corrupted.
343 if ((where >= 0x10 && where < 0x2c) ||
344 (where >= 0x1a4 && where < 0x1bc))
345 /* BAR or SR-IOV BAR */
346 return PCIBIOS_SUCCESSFUL;
348 return pci_generic_config_write(bus, devfn, where, size, val);
351 struct pci_ecam_ops pci_thunder_ecam_ops = {
354 .map_bus = pci_ecam_map_bus,
355 .read = thunder_ecam_config_read,
356 .write = thunder_ecam_config_write,
360 #ifdef CONFIG_PCI_HOST_THUNDER_ECAM
362 static const struct of_device_id thunder_ecam_of_match[] = {
363 { .compatible = "cavium,pci-host-thunder-ecam" },
367 static int thunder_ecam_probe(struct platform_device *pdev)
369 return pci_host_common_probe(pdev, &pci_thunder_ecam_ops);
372 static struct platform_driver thunder_ecam_driver = {
374 .name = KBUILD_MODNAME,
375 .of_match_table = thunder_ecam_of_match,
376 .suppress_bind_attrs = true,
378 .probe = thunder_ecam_probe,
380 builtin_platform_driver(thunder_ecam_driver);