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>
18 static void set_val(u32 v
, int where
, int size
, u32
*val
)
20 int shift
= (where
& 3) * 8;
22 pr_debug("set_val %04x: %08x\n", (unsigned)(where
& ~3), v
);
31 static int handle_ea_bar(u32 e0
, int bar
, struct pci_bus
*bus
,
32 unsigned int devfn
, int where
, int size
, u32
*val
)
37 /* Entries are 16-byte aligned; bits[2,3] select word in entry */
38 int where_a
= where
& 0xc;
41 set_val(e0
, where
, size
, val
);
42 return PCIBIOS_SUCCESSFUL
;
45 addr
= bus
->ops
->map_bus(bus
, devfn
, bar
); /* BAR 0 */
48 return PCIBIOS_DEVICE_NOT_FOUND
;
52 v
|= 2; /* EA entry-1. Base-L */
53 set_val(v
, where
, size
, val
);
54 return PCIBIOS_SUCCESSFUL
;
60 addr
= bus
->ops
->map_bus(bus
, devfn
, bar
); /* BAR 0 */
63 return PCIBIOS_DEVICE_NOT_FOUND
;
65 barl_orig
= readl(addr
+ 0);
66 writel(0xffffffff, addr
+ 0);
67 barl_rb
= readl(addr
+ 0);
68 writel(barl_orig
, addr
+ 0);
69 /* zeros in unsettable bits */
71 v
|= 0xc; /* EA entry-2. Offset-L */
72 set_val(v
, where
, size
, val
);
73 return PCIBIOS_SUCCESSFUL
;
76 addr
= bus
->ops
->map_bus(bus
, devfn
, bar
+ 4); /* BAR 1 */
79 return PCIBIOS_DEVICE_NOT_FOUND
;
81 v
= readl(addr
); /* EA entry-3. Base-H */
82 set_val(v
, where
, size
, val
);
83 return PCIBIOS_SUCCESSFUL
;
85 return PCIBIOS_DEVICE_NOT_FOUND
;
88 static int thunder_ecam_p2_config_read(struct pci_bus
*bus
, unsigned int devfn
,
89 int where
, int size
, u32
*val
)
91 struct pci_config_window
*cfg
= bus
->sysdata
;
92 int where_a
= where
& ~3;
97 /* EA Base[63:32] may be missing some bits ... */
105 return pci_generic_config_read(bus
, devfn
, where
, size
, val
);
108 addr
= bus
->ops
->map_bus(bus
, devfn
, where_a
);
111 return PCIBIOS_DEVICE_NOT_FOUND
;
117 * Bit 44 of the 64-bit Base must match the same bit in
118 * the config space access window. Since we are working with
119 * the high-order 32 bits, shift everything down by 32 bits.
121 node_bits
= (cfg
->res
.start
>> 32) & (1 << 12);
124 set_val(v
, where
, size
, val
);
126 return PCIBIOS_SUCCESSFUL
;
129 static int thunder_ecam_config_read(struct pci_bus
*bus
, unsigned int devfn
,
130 int where
, int size
, u32
*val
)
137 int where_a
= where
& ~3;
139 addr
= bus
->ops
->map_bus(bus
, devfn
, 0xc);
142 return PCIBIOS_DEVICE_NOT_FOUND
;
147 /* Check for non type-00 header */
148 cfg_type
= (v
>> 16) & 0x7f;
150 addr
= bus
->ops
->map_bus(bus
, devfn
, 8);
153 return PCIBIOS_DEVICE_NOT_FOUND
;
156 class_rev
= readl(addr
);
157 if (class_rev
== 0xffffffff)
160 if ((class_rev
& 0xff) >= 8) {
161 /* Pass-2 handling */
164 return thunder_ecam_p2_config_read(bus
, devfn
, where
,
169 * All BARs have fixed addresses specified by the EA
170 * capability; they must return zero on read.
173 ((where
>= 0x10 && where
< 0x2c) ||
174 (where
>= 0x1a4 && where
< 0x1bc))) {
175 /* BAR or SR-IOV BAR */
177 return PCIBIOS_SUCCESSFUL
;
180 addr
= bus
->ops
->map_bus(bus
, devfn
, 0);
183 return PCIBIOS_DEVICE_NOT_FOUND
;
186 vendor_device
= readl(addr
);
187 if (vendor_device
== 0xffffffff)
190 pr_debug("%04x:%04x - Fix pass#: %08x, where: %03x, devfn: %03x\n",
191 vendor_device
& 0xffff, vendor_device
>> 16, class_rev
,
192 (unsigned) where
, devfn
);
194 /* Check for non type-00 header */
197 bool is_nic
= (vendor_device
== 0xa01e177d);
198 bool is_tns
= (vendor_device
== 0xa01f177d);
200 addr
= bus
->ops
->map_bus(bus
, devfn
, 0x70);
203 return PCIBIOS_DEVICE_NOT_FOUND
;
207 has_msix
= (v
& 0xff00) != 0;
209 if (!has_msix
&& where_a
== 0x70) {
210 v
|= 0xbc00; /* next capability is EA at 0xbc */
211 set_val(v
, where
, size
, val
);
212 return PCIBIOS_SUCCESSFUL
;
214 if (where_a
== 0xb0) {
215 addr
= bus
->ops
->map_bus(bus
, devfn
, where_a
);
218 return PCIBIOS_DEVICE_NOT_FOUND
;
222 pr_err("Bad MSIX cap header: %08x\n", v
);
223 v
|= 0xbc00; /* next capability is EA at 0xbc */
224 set_val(v
, where
, size
, val
);
225 return PCIBIOS_SUCCESSFUL
;
227 if (where_a
== 0xbc) {
229 v
= 0x40014; /* EA last in chain, 4 entries */
231 v
= 0x30014; /* EA last in chain, 3 entries */
233 v
= 0x20014; /* EA last in chain, 2 entries */
235 v
= 0x10014; /* EA last in chain, 1 entry */
236 set_val(v
, where
, size
, val
);
237 return PCIBIOS_SUCCESSFUL
;
239 if (where_a
>= 0xc0 && where_a
< 0xd0)
240 /* EA entry-0. PP=0, BAR0 Size:3 */
241 return handle_ea_bar(0x80ff0003,
242 0x10, bus
, devfn
, where
,
244 if (where_a
>= 0xd0 && where_a
< 0xe0 && has_msix
)
245 /* EA entry-1. PP=0, BAR4 Size:3 */
246 return handle_ea_bar(0x80ff0043,
247 0x20, bus
, devfn
, where
,
249 if (where_a
>= 0xe0 && where_a
< 0xf0 && is_tns
)
250 /* EA entry-2. PP=0, BAR2, Size:3 */
251 return handle_ea_bar(0x80ff0023,
252 0x18, bus
, devfn
, where
,
254 if (where_a
>= 0xe0 && where_a
< 0xf0 && is_nic
)
255 /* EA entry-2. PP=4, VF_BAR0 (9), Size:3 */
256 return handle_ea_bar(0x80ff0493,
257 0x1a4, bus
, devfn
, where
,
259 if (where_a
>= 0xf0 && where_a
< 0x100 && is_nic
)
260 /* EA entry-3. PP=4, VF_BAR4 (d), Size:3 */
261 return handle_ea_bar(0x80ff04d3,
262 0x1b4, bus
, devfn
, where
,
264 } else if (cfg_type
== 1) {
265 bool is_rsl_bridge
= devfn
== 0x08;
266 bool is_rad_bridge
= devfn
== 0xa0;
267 bool is_zip_bridge
= devfn
== 0xa8;
268 bool is_dfa_bridge
= devfn
== 0xb0;
269 bool is_nic_bridge
= devfn
== 0x10;
271 if (where_a
== 0x70) {
272 addr
= bus
->ops
->map_bus(bus
, devfn
, where_a
);
275 return PCIBIOS_DEVICE_NOT_FOUND
;
279 pr_err("Bad PCIe cap header: %08x\n", v
);
280 v
|= 0xbc00; /* next capability is EA at 0xbc */
281 set_val(v
, where
, size
, val
);
282 return PCIBIOS_SUCCESSFUL
;
284 if (where_a
== 0xbc) {
286 v
= 0x10014; /* EA last in chain, 1 entry */
288 v
= 0x00014; /* EA last in chain, no entries */
289 set_val(v
, where
, size
, val
);
290 return PCIBIOS_SUCCESSFUL
;
292 if (where_a
== 0xc0) {
293 if (is_rsl_bridge
|| is_nic_bridge
)
294 v
= 0x0101; /* subordinate:secondary = 1:1 */
295 else if (is_rad_bridge
)
296 v
= 0x0202; /* subordinate:secondary = 2:2 */
297 else if (is_zip_bridge
)
298 v
= 0x0303; /* subordinate:secondary = 3:3 */
299 else if (is_dfa_bridge
)
300 v
= 0x0404; /* subordinate:secondary = 4:4 */
301 set_val(v
, where
, size
, val
);
302 return PCIBIOS_SUCCESSFUL
;
304 if (where_a
== 0xc4 && is_nic_bridge
) {
305 /* Enabled, not-Write, SP=ff, PP=05, BEI=6, ES=4 */
307 set_val(v
, where
, size
, val
);
308 return PCIBIOS_SUCCESSFUL
;
310 if (where_a
== 0xc8 && is_nic_bridge
) {
311 v
= 0x00000002; /* Base-L 64-bit */
312 set_val(v
, where
, size
, val
);
313 return PCIBIOS_SUCCESSFUL
;
315 if (where_a
== 0xcc && is_nic_bridge
) {
316 v
= 0xfffffffe; /* MaxOffset-L 64-bit */
317 set_val(v
, where
, size
, val
);
318 return PCIBIOS_SUCCESSFUL
;
320 if (where_a
== 0xd0 && is_nic_bridge
) {
321 v
= 0x00008430; /* NIC Base-H */
322 set_val(v
, where
, size
, val
);
323 return PCIBIOS_SUCCESSFUL
;
325 if (where_a
== 0xd4 && is_nic_bridge
) {
326 v
= 0x0000000f; /* MaxOffset-H */
327 set_val(v
, where
, size
, val
);
328 return PCIBIOS_SUCCESSFUL
;
332 return pci_generic_config_read(bus
, devfn
, where
, size
, val
);
335 static int thunder_ecam_config_write(struct pci_bus
*bus
, unsigned int devfn
,
336 int where
, int size
, u32 val
)
339 * All BARs have fixed addresses; ignore BAR writes so they
340 * don't get corrupted.
342 if ((where
>= 0x10 && where
< 0x2c) ||
343 (where
>= 0x1a4 && where
< 0x1bc))
344 /* BAR or SR-IOV BAR */
345 return PCIBIOS_SUCCESSFUL
;
347 return pci_generic_config_write(bus
, devfn
, where
, size
, val
);
350 static struct pci_ecam_ops pci_thunder_ecam_ops
= {
353 .map_bus
= pci_ecam_map_bus
,
354 .read
= thunder_ecam_config_read
,
355 .write
= thunder_ecam_config_write
,
359 static const struct of_device_id thunder_ecam_of_match
[] = {
360 { .compatible
= "cavium,pci-host-thunder-ecam" },
363 MODULE_DEVICE_TABLE(of
, thunder_ecam_of_match
);
365 static int thunder_ecam_probe(struct platform_device
*pdev
)
367 return pci_host_common_probe(pdev
, &pci_thunder_ecam_ops
);
370 static struct platform_driver thunder_ecam_driver
= {
372 .name
= KBUILD_MODNAME
,
373 .of_match_table
= thunder_ecam_of_match
,
375 .probe
= thunder_ecam_probe
,
377 module_platform_driver(thunder_ecam_driver
);
379 MODULE_DESCRIPTION("Thunder ECAM PCI host driver");
380 MODULE_LICENSE("GPL v2");