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/module.h>
11 #include <linux/ioport.h>
12 #include <linux/of_pci.h>
14 #include <linux/platform_device.h>
16 #include "pci-host-common.h"
18 /* Mapping is standard ECAM */
19 static void __iomem
*thunder_ecam_map_bus(struct pci_bus
*bus
,
23 struct gen_pci
*pci
= bus
->sysdata
;
24 resource_size_t idx
= bus
->number
- pci
->cfg
.bus_range
->start
;
26 return pci
->cfg
.win
[idx
] + ((devfn
<< 12) | where
);
29 static void set_val(u32 v
, int where
, int size
, u32
*val
)
31 int shift
= (where
& 3) * 8;
33 pr_debug("set_val %04x: %08x\n", (unsigned)(where
& ~3), v
);
42 static int handle_ea_bar(u32 e0
, int bar
, struct pci_bus
*bus
,
43 unsigned int devfn
, int where
, int size
, u32
*val
)
48 /* Entries are 16-byte aligned; bits[2,3] select word in entry */
49 int where_a
= where
& 0xc;
52 set_val(e0
, where
, size
, val
);
53 return PCIBIOS_SUCCESSFUL
;
56 addr
= bus
->ops
->map_bus(bus
, devfn
, bar
); /* BAR 0 */
59 return PCIBIOS_DEVICE_NOT_FOUND
;
63 v
|= 2; /* EA entry-1. Base-L */
64 set_val(v
, where
, size
, val
);
65 return PCIBIOS_SUCCESSFUL
;
71 addr
= bus
->ops
->map_bus(bus
, devfn
, bar
); /* BAR 0 */
74 return PCIBIOS_DEVICE_NOT_FOUND
;
76 barl_orig
= readl(addr
+ 0);
77 writel(0xffffffff, addr
+ 0);
78 barl_rb
= readl(addr
+ 0);
79 writel(barl_orig
, addr
+ 0);
80 /* zeros in unsettable bits */
82 v
|= 0xc; /* EA entry-2. Offset-L */
83 set_val(v
, where
, size
, val
);
84 return PCIBIOS_SUCCESSFUL
;
87 addr
= bus
->ops
->map_bus(bus
, devfn
, bar
+ 4); /* BAR 1 */
90 return PCIBIOS_DEVICE_NOT_FOUND
;
92 v
= readl(addr
); /* EA entry-3. Base-H */
93 set_val(v
, where
, size
, val
);
94 return PCIBIOS_SUCCESSFUL
;
96 return PCIBIOS_DEVICE_NOT_FOUND
;
99 static int thunder_ecam_p2_config_read(struct pci_bus
*bus
, unsigned int devfn
,
100 int where
, int size
, u32
*val
)
102 struct gen_pci
*pci
= bus
->sysdata
;
103 int where_a
= where
& ~3;
108 /* EA Base[63:32] may be missing some bits ... */
116 return pci_generic_config_read(bus
, devfn
, where
, size
, val
);
119 addr
= bus
->ops
->map_bus(bus
, devfn
, where_a
);
122 return PCIBIOS_DEVICE_NOT_FOUND
;
128 * Bit 44 of the 64-bit Base must match the same bit in
129 * the config space access window. Since we are working with
130 * the high-order 32 bits, shift everything down by 32 bits.
132 node_bits
= (pci
->cfg
.res
.start
>> 32) & (1 << 12);
135 set_val(v
, where
, size
, val
);
137 return PCIBIOS_SUCCESSFUL
;
140 static int thunder_ecam_config_read(struct pci_bus
*bus
, unsigned int devfn
,
141 int where
, int size
, u32
*val
)
148 int where_a
= where
& ~3;
150 addr
= bus
->ops
->map_bus(bus
, devfn
, 0xc);
153 return PCIBIOS_DEVICE_NOT_FOUND
;
158 /* Check for non type-00 header */
159 cfg_type
= (v
>> 16) & 0x7f;
161 addr
= bus
->ops
->map_bus(bus
, devfn
, 8);
164 return PCIBIOS_DEVICE_NOT_FOUND
;
167 class_rev
= readl(addr
);
168 if (class_rev
== 0xffffffff)
171 if ((class_rev
& 0xff) >= 8) {
172 /* Pass-2 handling */
175 return thunder_ecam_p2_config_read(bus
, devfn
, where
,
180 * All BARs have fixed addresses specified by the EA
181 * capability; they must return zero on read.
184 ((where
>= 0x10 && where
< 0x2c) ||
185 (where
>= 0x1a4 && where
< 0x1bc))) {
186 /* BAR or SR-IOV BAR */
188 return PCIBIOS_SUCCESSFUL
;
191 addr
= bus
->ops
->map_bus(bus
, devfn
, 0);
194 return PCIBIOS_DEVICE_NOT_FOUND
;
197 vendor_device
= readl(addr
);
198 if (vendor_device
== 0xffffffff)
201 pr_debug("%04x:%04x - Fix pass#: %08x, where: %03x, devfn: %03x\n",
202 vendor_device
& 0xffff, vendor_device
>> 16, class_rev
,
203 (unsigned) where
, devfn
);
205 /* Check for non type-00 header */
208 bool is_nic
= (vendor_device
== 0xa01e177d);
209 bool is_tns
= (vendor_device
== 0xa01f177d);
211 addr
= bus
->ops
->map_bus(bus
, devfn
, 0x70);
214 return PCIBIOS_DEVICE_NOT_FOUND
;
218 has_msix
= (v
& 0xff00) != 0;
220 if (!has_msix
&& where_a
== 0x70) {
221 v
|= 0xbc00; /* next capability is EA at 0xbc */
222 set_val(v
, where
, size
, val
);
223 return PCIBIOS_SUCCESSFUL
;
225 if (where_a
== 0xb0) {
226 addr
= bus
->ops
->map_bus(bus
, devfn
, where_a
);
229 return PCIBIOS_DEVICE_NOT_FOUND
;
233 pr_err("Bad MSIX cap header: %08x\n", v
);
234 v
|= 0xbc00; /* next capability is EA at 0xbc */
235 set_val(v
, where
, size
, val
);
236 return PCIBIOS_SUCCESSFUL
;
238 if (where_a
== 0xbc) {
240 v
= 0x40014; /* EA last in chain, 4 entries */
242 v
= 0x30014; /* EA last in chain, 3 entries */
244 v
= 0x20014; /* EA last in chain, 2 entries */
246 v
= 0x10014; /* EA last in chain, 1 entry */
247 set_val(v
, where
, size
, val
);
248 return PCIBIOS_SUCCESSFUL
;
250 if (where_a
>= 0xc0 && where_a
< 0xd0)
251 /* EA entry-0. PP=0, BAR0 Size:3 */
252 return handle_ea_bar(0x80ff0003,
253 0x10, bus
, devfn
, where
,
255 if (where_a
>= 0xd0 && where_a
< 0xe0 && has_msix
)
256 /* EA entry-1. PP=0, BAR4 Size:3 */
257 return handle_ea_bar(0x80ff0043,
258 0x20, bus
, devfn
, where
,
260 if (where_a
>= 0xe0 && where_a
< 0xf0 && is_tns
)
261 /* EA entry-2. PP=0, BAR2, Size:3 */
262 return handle_ea_bar(0x80ff0023,
263 0x18, bus
, devfn
, where
,
265 if (where_a
>= 0xe0 && where_a
< 0xf0 && is_nic
)
266 /* EA entry-2. PP=4, VF_BAR0 (9), Size:3 */
267 return handle_ea_bar(0x80ff0493,
268 0x1a4, bus
, devfn
, where
,
270 if (where_a
>= 0xf0 && where_a
< 0x100 && is_nic
)
271 /* EA entry-3. PP=4, VF_BAR4 (d), Size:3 */
272 return handle_ea_bar(0x80ff04d3,
273 0x1b4, bus
, devfn
, where
,
275 } else if (cfg_type
== 1) {
276 bool is_rsl_bridge
= devfn
== 0x08;
277 bool is_rad_bridge
= devfn
== 0xa0;
278 bool is_zip_bridge
= devfn
== 0xa8;
279 bool is_dfa_bridge
= devfn
== 0xb0;
280 bool is_nic_bridge
= devfn
== 0x10;
282 if (where_a
== 0x70) {
283 addr
= bus
->ops
->map_bus(bus
, devfn
, where_a
);
286 return PCIBIOS_DEVICE_NOT_FOUND
;
290 pr_err("Bad PCIe cap header: %08x\n", v
);
291 v
|= 0xbc00; /* next capability is EA at 0xbc */
292 set_val(v
, where
, size
, val
);
293 return PCIBIOS_SUCCESSFUL
;
295 if (where_a
== 0xbc) {
297 v
= 0x10014; /* EA last in chain, 1 entry */
299 v
= 0x00014; /* EA last in chain, no entries */
300 set_val(v
, where
, size
, val
);
301 return PCIBIOS_SUCCESSFUL
;
303 if (where_a
== 0xc0) {
304 if (is_rsl_bridge
|| is_nic_bridge
)
305 v
= 0x0101; /* subordinate:secondary = 1:1 */
306 else if (is_rad_bridge
)
307 v
= 0x0202; /* subordinate:secondary = 2:2 */
308 else if (is_zip_bridge
)
309 v
= 0x0303; /* subordinate:secondary = 3:3 */
310 else if (is_dfa_bridge
)
311 v
= 0x0404; /* subordinate:secondary = 4:4 */
312 set_val(v
, where
, size
, val
);
313 return PCIBIOS_SUCCESSFUL
;
315 if (where_a
== 0xc4 && is_nic_bridge
) {
316 /* Enabled, not-Write, SP=ff, PP=05, BEI=6, ES=4 */
318 set_val(v
, where
, size
, val
);
319 return PCIBIOS_SUCCESSFUL
;
321 if (where_a
== 0xc8 && is_nic_bridge
) {
322 v
= 0x00000002; /* Base-L 64-bit */
323 set_val(v
, where
, size
, val
);
324 return PCIBIOS_SUCCESSFUL
;
326 if (where_a
== 0xcc && is_nic_bridge
) {
327 v
= 0xfffffffe; /* MaxOffset-L 64-bit */
328 set_val(v
, where
, size
, val
);
329 return PCIBIOS_SUCCESSFUL
;
331 if (where_a
== 0xd0 && is_nic_bridge
) {
332 v
= 0x00008430; /* NIC Base-H */
333 set_val(v
, where
, size
, val
);
334 return PCIBIOS_SUCCESSFUL
;
336 if (where_a
== 0xd4 && is_nic_bridge
) {
337 v
= 0x0000000f; /* MaxOffset-H */
338 set_val(v
, where
, size
, val
);
339 return PCIBIOS_SUCCESSFUL
;
343 return pci_generic_config_read(bus
, devfn
, where
, size
, val
);
346 static int thunder_ecam_config_write(struct pci_bus
*bus
, unsigned int devfn
,
347 int where
, int size
, u32 val
)
350 * All BARs have fixed addresses; ignore BAR writes so they
351 * don't get corrupted.
353 if ((where
>= 0x10 && where
< 0x2c) ||
354 (where
>= 0x1a4 && where
< 0x1bc))
355 /* BAR or SR-IOV BAR */
356 return PCIBIOS_SUCCESSFUL
;
358 return pci_generic_config_write(bus
, devfn
, where
, size
, val
);
361 static struct gen_pci_cfg_bus_ops thunder_ecam_bus_ops
= {
364 .map_bus
= thunder_ecam_map_bus
,
365 .read
= thunder_ecam_config_read
,
366 .write
= thunder_ecam_config_write
,
370 static const struct of_device_id thunder_ecam_of_match
[] = {
371 { .compatible
= "cavium,pci-host-thunder-ecam",
372 .data
= &thunder_ecam_bus_ops
},
376 MODULE_DEVICE_TABLE(of
, thunder_ecam_of_match
);
378 static int thunder_ecam_probe(struct platform_device
*pdev
)
380 struct device
*dev
= &pdev
->dev
;
381 const struct of_device_id
*of_id
;
382 struct gen_pci
*pci
= devm_kzalloc(dev
, sizeof(*pci
), GFP_KERNEL
);
387 of_id
= of_match_node(thunder_ecam_of_match
, dev
->of_node
);
388 pci
->cfg
.ops
= (struct gen_pci_cfg_bus_ops
*)of_id
->data
;
390 return pci_host_common_probe(pdev
, pci
);
393 static struct platform_driver thunder_ecam_driver
= {
395 .name
= KBUILD_MODNAME
,
396 .of_match_table
= thunder_ecam_of_match
,
398 .probe
= thunder_ecam_probe
,
400 module_platform_driver(thunder_ecam_driver
);
402 MODULE_DESCRIPTION("Thunder ECAM PCI host driver");
403 MODULE_LICENSE("GPL v2");