2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License version 2 as
4 * published by the Free Software Foundation.
6 * This program is distributed in the hope that it will be useful,
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 * GNU General Public License for more details.
11 * You should have received a copy of the GNU General Public License
12 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14 * Copyright (C) 2015 - 2016 Cavium, Inc.
17 #include <linux/kernel.h>
18 #include <linux/module.h>
19 #include <linux/of_address.h>
20 #include <linux/of_pci.h>
21 #include <linux/platform_device.h>
23 #include "pci-host-common.h"
25 #define PEM_CFG_WR 0x28
26 #define PEM_CFG_RD 0x30
28 struct thunder_pem_pci
{
29 struct gen_pci gen_pci
;
31 void __iomem
*pem_reg_base
;
34 static void __iomem
*thunder_pem_map_bus(struct pci_bus
*bus
,
35 unsigned int devfn
, int where
)
37 struct gen_pci
*pci
= bus
->sysdata
;
38 resource_size_t idx
= bus
->number
- pci
->cfg
.bus_range
->start
;
40 return pci
->cfg
.win
[idx
] + ((devfn
<< 16) | where
);
43 static int thunder_pem_bridge_read(struct pci_bus
*bus
, unsigned int devfn
,
44 int where
, int size
, u32
*val
)
47 struct thunder_pem_pci
*pem_pci
;
48 struct gen_pci
*pci
= bus
->sysdata
;
50 pem_pci
= container_of(pci
, struct thunder_pem_pci
, gen_pci
);
52 if (devfn
!= 0 || where
>= 2048) {
54 return PCIBIOS_DEVICE_NOT_FOUND
;
58 * 32-bit accesses only. Write the address to the low order
59 * bits of PEM_CFG_RD, then trigger the read by reading back.
60 * The config data lands in the upper 32-bits of PEM_CFG_RD.
62 read_val
= where
& ~3ull;
63 writeq(read_val
, pem_pci
->pem_reg_base
+ PEM_CFG_RD
);
64 read_val
= readq(pem_pci
->pem_reg_base
+ PEM_CFG_RD
);
68 * The config space contains some garbage, fix it up. Also
69 * synthesize an EA capability for the BAR used by MSI-X.
73 read_val
&= 0xffff00ff;
74 read_val
|= 0x00007000; /* Skip MSI CAP */
76 case 0x70: /* Express Cap */
77 /* PME interrupt on vector 2*/
78 read_val
|= (2u << 25);
80 case 0xb0: /* MSI-X Cap */
81 /* TableSize=4, Next Cap is EA */
82 read_val
&= 0xc00000ff;
83 read_val
|= 0x0003bc00;
86 /* Table offset=0, BIR=0 */
87 read_val
= 0x00000000;
90 /* BPA offset=0xf0000, BIR=0 */
91 read_val
= 0x000f0000;
94 /* EA, 1 entry, no next Cap */
95 read_val
= 0x00010014;
99 read_val
= 0x00000000;
102 /* Entry BEI=0, PP=0x00, SP=0xff, ES=3 */
103 read_val
= 0x80ff0003;
106 read_val
= pem_pci
->ea_entry
[0];
109 read_val
= pem_pci
->ea_entry
[1];
112 read_val
= pem_pci
->ea_entry
[2];
117 read_val
>>= (8 * (where
& 3));
129 return PCIBIOS_SUCCESSFUL
;
132 static int thunder_pem_config_read(struct pci_bus
*bus
, unsigned int devfn
,
133 int where
, int size
, u32
*val
)
135 struct gen_pci
*pci
= bus
->sysdata
;
137 if (bus
->number
< pci
->cfg
.bus_range
->start
||
138 bus
->number
> pci
->cfg
.bus_range
->end
)
139 return PCIBIOS_DEVICE_NOT_FOUND
;
142 * The first device on the bus is the PEM PCIe bridge.
143 * Special case its config access.
145 if (bus
->number
== pci
->cfg
.bus_range
->start
)
146 return thunder_pem_bridge_read(bus
, devfn
, where
, size
, val
);
148 return pci_generic_config_read(bus
, devfn
, where
, size
, val
);
152 * Some of the w1c_bits below also include read-only or non-writable
153 * reserved bits, this makes the code simpler and is OK as the bits
154 * are not affected by writing zeros to them.
156 static u32
thunder_pem_bridge_w1c_bits(int where
)
160 switch (where
& ~3) {
161 case 0x04: /* Command/Status */
162 case 0x1c: /* Base and I/O Limit/Secondary Status */
163 w1c_bits
= 0xff000000;
165 case 0x44: /* Power Management Control and Status */
166 w1c_bits
= 0xfffffe00;
168 case 0x78: /* Device Control/Device Status */
169 case 0x80: /* Link Control/Link Status */
170 case 0x88: /* Slot Control/Slot Status */
171 case 0x90: /* Root Status */
172 case 0xa0: /* Link Control 2 Registers/Link Status 2 */
173 w1c_bits
= 0xffff0000;
175 case 0x104: /* Uncorrectable Error Status */
176 case 0x110: /* Correctable Error Status */
177 case 0x130: /* Error Status */
178 case 0x160: /* Link Control 4 */
179 w1c_bits
= 0xffffffff;
187 static int thunder_pem_bridge_write(struct pci_bus
*bus
, unsigned int devfn
,
188 int where
, int size
, u32 val
)
190 struct gen_pci
*pci
= bus
->sysdata
;
191 struct thunder_pem_pci
*pem_pci
;
192 u64 write_val
, read_val
;
195 pem_pci
= container_of(pci
, struct thunder_pem_pci
, gen_pci
);
197 if (devfn
!= 0 || where
>= 2048)
198 return PCIBIOS_DEVICE_NOT_FOUND
;
201 * 32-bit accesses only. If the write is for a size smaller
202 * than 32-bits, we must first read the 32-bit value and merge
203 * in the desired bits and then write the whole 32-bits back
208 read_val
= where
& ~3ull;
209 writeq(read_val
, pem_pci
->pem_reg_base
+ PEM_CFG_RD
);
210 read_val
= readq(pem_pci
->pem_reg_base
+ PEM_CFG_RD
);
212 mask
= ~(0xff << (8 * (where
& 3)));
214 val
= (val
& 0xff) << (8 * (where
& 3));
215 val
|= (u32
)read_val
;
218 read_val
= where
& ~3ull;
219 writeq(read_val
, pem_pci
->pem_reg_base
+ PEM_CFG_RD
);
220 read_val
= readq(pem_pci
->pem_reg_base
+ PEM_CFG_RD
);
222 mask
= ~(0xffff << (8 * (where
& 3)));
224 val
= (val
& 0xffff) << (8 * (where
& 3));
225 val
|= (u32
)read_val
;
232 * By expanding the write width to 32 bits, we may
233 * inadvertently hit some W1C bits that were not intended to
234 * be written. Calculate the mask that must be applied to the
235 * data to be written to avoid these cases.
238 u32 w1c_bits
= thunder_pem_bridge_w1c_bits(where
);
247 * Low order bits are the config address, the high order 32
248 * bits are the data to be written.
250 write_val
= where
& ~3ull;
251 write_val
|= (((u64
)val
) << 32);
252 writeq(write_val
, pem_pci
->pem_reg_base
+ PEM_CFG_WR
);
253 return PCIBIOS_SUCCESSFUL
;
256 static int thunder_pem_config_write(struct pci_bus
*bus
, unsigned int devfn
,
257 int where
, int size
, u32 val
)
259 struct gen_pci
*pci
= bus
->sysdata
;
261 if (bus
->number
< pci
->cfg
.bus_range
->start
||
262 bus
->number
> pci
->cfg
.bus_range
->end
)
263 return PCIBIOS_DEVICE_NOT_FOUND
;
265 * The first device on the bus is the PEM PCIe bridge.
266 * Special case its config access.
268 if (bus
->number
== pci
->cfg
.bus_range
->start
)
269 return thunder_pem_bridge_write(bus
, devfn
, where
, size
, val
);
272 return pci_generic_config_write(bus
, devfn
, where
, size
, val
);
275 static struct gen_pci_cfg_bus_ops thunder_pem_bus_ops
= {
278 .map_bus
= thunder_pem_map_bus
,
279 .read
= thunder_pem_config_read
,
280 .write
= thunder_pem_config_write
,
284 static const struct of_device_id thunder_pem_of_match
[] = {
285 { .compatible
= "cavium,pci-host-thunder-pem",
286 .data
= &thunder_pem_bus_ops
},
290 MODULE_DEVICE_TABLE(of
, thunder_pem_of_match
);
292 static int thunder_pem_probe(struct platform_device
*pdev
)
294 struct device
*dev
= &pdev
->dev
;
295 const struct of_device_id
*of_id
;
296 resource_size_t bar4_start
;
297 struct resource
*res_pem
;
298 struct thunder_pem_pci
*pem_pci
;
300 pem_pci
= devm_kzalloc(dev
, sizeof(*pem_pci
), GFP_KERNEL
);
304 of_id
= of_match_node(thunder_pem_of_match
, dev
->of_node
);
305 pem_pci
->gen_pci
.cfg
.ops
= (struct gen_pci_cfg_bus_ops
*)of_id
->data
;
308 * The second register range is the PEM bridge to the PCIe
309 * bus. It has a different config access method than those
310 * devices behind the bridge.
312 res_pem
= platform_get_resource(pdev
, IORESOURCE_MEM
, 1);
314 dev_err(dev
, "missing \"reg[1]\"property\n");
318 pem_pci
->pem_reg_base
= devm_ioremap(dev
, res_pem
->start
, 0x10000);
319 if (!pem_pci
->pem_reg_base
)
323 * The MSI-X BAR for the PEM and AER interrupts is located at
324 * a fixed offset from the PEM register base. Generate a
325 * fragment of the synthesized Enhanced Allocation capability
326 * structure here for the BAR.
328 bar4_start
= res_pem
->start
+ 0xf00000;
329 pem_pci
->ea_entry
[0] = (u32
)bar4_start
| 2;
330 pem_pci
->ea_entry
[1] = (u32
)(res_pem
->end
- bar4_start
) & ~3u;
331 pem_pci
->ea_entry
[2] = (u32
)(bar4_start
>> 32);
333 return pci_host_common_probe(pdev
, &pem_pci
->gen_pci
);
336 static struct platform_driver thunder_pem_driver
= {
338 .name
= KBUILD_MODNAME
,
339 .of_match_table
= thunder_pem_of_match
,
341 .probe
= thunder_pem_probe
,
343 module_platform_driver(thunder_pem_driver
);
345 MODULE_DESCRIPTION("Thunder PEM PCIe host driver");
346 MODULE_LICENSE("GPL v2");