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/init.h>
19 #include <linux/of_address.h>
20 #include <linux/of_pci.h>
21 #include <linux/pci-ecam.h>
22 #include <linux/platform_device.h>
24 #define PEM_CFG_WR 0x28
25 #define PEM_CFG_RD 0x30
27 struct thunder_pem_pci {
29 void __iomem *pem_reg_base;
32 static int thunder_pem_bridge_read(struct pci_bus *bus, unsigned int devfn,
33 int where, int size, u32 *val)
36 struct pci_config_window *cfg = bus->sysdata;
37 struct thunder_pem_pci *pem_pci = (struct thunder_pem_pci *)cfg->priv;
39 if (devfn != 0 || where >= 2048) {
41 return PCIBIOS_DEVICE_NOT_FOUND;
45 * 32-bit accesses only. Write the address to the low order
46 * bits of PEM_CFG_RD, then trigger the read by reading back.
47 * The config data lands in the upper 32-bits of PEM_CFG_RD.
49 read_val = where & ~3ull;
50 writeq(read_val, pem_pci->pem_reg_base + PEM_CFG_RD);
51 read_val = readq(pem_pci->pem_reg_base + PEM_CFG_RD);
55 * The config space contains some garbage, fix it up. Also
56 * synthesize an EA capability for the BAR used by MSI-X.
60 read_val &= 0xffff00ff;
61 read_val |= 0x00007000; /* Skip MSI CAP */
63 case 0x70: /* Express Cap */
64 /* PME interrupt on vector 2*/
65 read_val |= (2u << 25);
67 case 0xb0: /* MSI-X Cap */
68 /* TableSize=4, Next Cap is EA */
69 read_val &= 0xc00000ff;
70 read_val |= 0x0003bc00;
73 /* Table offset=0, BIR=0 */
74 read_val = 0x00000000;
77 /* BPA offset=0xf0000, BIR=0 */
78 read_val = 0x000f0000;
81 /* EA, 1 entry, no next Cap */
82 read_val = 0x00010014;
86 read_val = 0x00000000;
89 /* Entry BEI=0, PP=0x00, SP=0xff, ES=3 */
90 read_val = 0x80ff0003;
93 read_val = pem_pci->ea_entry[0];
96 read_val = pem_pci->ea_entry[1];
99 read_val = pem_pci->ea_entry[2];
104 read_val >>= (8 * (where & 3));
116 return PCIBIOS_SUCCESSFUL;
119 static int thunder_pem_config_read(struct pci_bus *bus, unsigned int devfn,
120 int where, int size, u32 *val)
122 struct pci_config_window *cfg = bus->sysdata;
124 if (bus->number < cfg->busr.start ||
125 bus->number > cfg->busr.end)
126 return PCIBIOS_DEVICE_NOT_FOUND;
129 * The first device on the bus is the PEM PCIe bridge.
130 * Special case its config access.
132 if (bus->number == cfg->busr.start)
133 return thunder_pem_bridge_read(bus, devfn, where, size, val);
135 return pci_generic_config_read(bus, devfn, where, size, val);
139 * Some of the w1c_bits below also include read-only or non-writable
140 * reserved bits, this makes the code simpler and is OK as the bits
141 * are not affected by writing zeros to them.
143 static u32 thunder_pem_bridge_w1c_bits(u64 where_aligned)
147 switch (where_aligned) {
148 case 0x04: /* Command/Status */
149 case 0x1c: /* Base and I/O Limit/Secondary Status */
150 w1c_bits = 0xff000000;
152 case 0x44: /* Power Management Control and Status */
153 w1c_bits = 0xfffffe00;
155 case 0x78: /* Device Control/Device Status */
156 case 0x80: /* Link Control/Link Status */
157 case 0x88: /* Slot Control/Slot Status */
158 case 0x90: /* Root Status */
159 case 0xa0: /* Link Control 2 Registers/Link Status 2 */
160 w1c_bits = 0xffff0000;
162 case 0x104: /* Uncorrectable Error Status */
163 case 0x110: /* Correctable Error Status */
164 case 0x130: /* Error Status */
165 case 0x160: /* Link Control 4 */
166 w1c_bits = 0xffffffff;
174 /* Some bits must be written to one so they appear to be read-only. */
175 static u32 thunder_pem_bridge_w1_bits(u64 where_aligned)
179 switch (where_aligned) {
180 case 0x1c: /* I/O Base / I/O Limit, Secondary Status */
181 /* Force 32-bit I/O addressing. */
184 case 0x24: /* Prefetchable Memory Base / Prefetchable Memory Limit */
185 /* Force 64-bit addressing */
186 w1_bits = 0x00010001;
195 static int thunder_pem_bridge_write(struct pci_bus *bus, unsigned int devfn,
196 int where, int size, u32 val)
198 struct pci_config_window *cfg = bus->sysdata;
199 struct thunder_pem_pci *pem_pci = (struct thunder_pem_pci *)cfg->priv;
200 u64 write_val, read_val;
201 u64 where_aligned = where & ~3ull;
205 if (devfn != 0 || where >= 2048)
206 return PCIBIOS_DEVICE_NOT_FOUND;
209 * 32-bit accesses only. If the write is for a size smaller
210 * than 32-bits, we must first read the 32-bit value and merge
211 * in the desired bits and then write the whole 32-bits back
216 writeq(where_aligned, pem_pci->pem_reg_base + PEM_CFG_RD);
217 read_val = readq(pem_pci->pem_reg_base + PEM_CFG_RD);
219 mask = ~(0xff << (8 * (where & 3)));
221 val = (val & 0xff) << (8 * (where & 3));
222 val |= (u32)read_val;
225 writeq(where_aligned, pem_pci->pem_reg_base + PEM_CFG_RD);
226 read_val = readq(pem_pci->pem_reg_base + PEM_CFG_RD);
228 mask = ~(0xffff << (8 * (where & 3)));
230 val = (val & 0xffff) << (8 * (where & 3));
231 val |= (u32)read_val;
238 * By expanding the write width to 32 bits, we may
239 * inadvertently hit some W1C bits that were not intended to
240 * be written. Calculate the mask that must be applied to the
241 * data to be written to avoid these cases.
244 u32 w1c_bits = thunder_pem_bridge_w1c_bits(where);
253 * Some bits must be read-only with value of one. Since the
254 * access method allows these to be cleared if a zero is
255 * written, force them to one before writing.
257 val |= thunder_pem_bridge_w1_bits(where_aligned);
260 * Low order bits are the config address, the high order 32
261 * bits are the data to be written.
263 write_val = (((u64)val) << 32) | where_aligned;
264 writeq(write_val, pem_pci->pem_reg_base + PEM_CFG_WR);
265 return PCIBIOS_SUCCESSFUL;
268 static int thunder_pem_config_write(struct pci_bus *bus, unsigned int devfn,
269 int where, int size, u32 val)
271 struct pci_config_window *cfg = bus->sysdata;
273 if (bus->number < cfg->busr.start ||
274 bus->number > cfg->busr.end)
275 return PCIBIOS_DEVICE_NOT_FOUND;
277 * The first device on the bus is the PEM PCIe bridge.
278 * Special case its config access.
280 if (bus->number == cfg->busr.start)
281 return thunder_pem_bridge_write(bus, devfn, where, size, val);
284 return pci_generic_config_write(bus, devfn, where, size, val);
287 static int thunder_pem_init(struct device *dev, struct pci_config_window *cfg,
288 struct resource *res_pem)
290 struct thunder_pem_pci *pem_pci;
291 resource_size_t bar4_start;
293 pem_pci = devm_kzalloc(dev, sizeof(*pem_pci), GFP_KERNEL);
297 pem_pci->pem_reg_base = devm_ioremap(dev, res_pem->start, 0x10000);
298 if (!pem_pci->pem_reg_base)
302 * The MSI-X BAR for the PEM and AER interrupts is located at
303 * a fixed offset from the PEM register base. Generate a
304 * fragment of the synthesized Enhanced Allocation capability
305 * structure here for the BAR.
307 bar4_start = res_pem->start + 0xf00000;
308 pem_pci->ea_entry[0] = (u32)bar4_start | 2;
309 pem_pci->ea_entry[1] = (u32)(res_pem->end - bar4_start) & ~3u;
310 pem_pci->ea_entry[2] = (u32)(bar4_start >> 32);
316 static int thunder_pem_platform_init(struct pci_config_window *cfg)
318 struct device *dev = cfg->parent;
319 struct platform_device *pdev = to_platform_device(dev);
320 struct resource *res_pem;
326 * The second register range is the PEM bridge to the PCIe
327 * bus. It has a different config access method than those
328 * devices behind the bridge.
330 res_pem = platform_get_resource(pdev, IORESOURCE_MEM, 1);
332 dev_err(dev, "missing \"reg[1]\"property\n");
336 return thunder_pem_init(dev, cfg, res_pem);
339 static struct pci_ecam_ops pci_thunder_pem_ops = {
341 .init = thunder_pem_platform_init,
343 .map_bus = pci_ecam_map_bus,
344 .read = thunder_pem_config_read,
345 .write = thunder_pem_config_write,
349 static const struct of_device_id thunder_pem_of_match[] = {
350 { .compatible = "cavium,pci-host-thunder-pem" },
354 static int thunder_pem_probe(struct platform_device *pdev)
356 return pci_host_common_probe(pdev, &pci_thunder_pem_ops);
359 static struct platform_driver thunder_pem_driver = {
361 .name = KBUILD_MODNAME,
362 .of_match_table = thunder_pem_of_match,
364 .probe = thunder_pem_probe,
366 builtin_platform_driver(thunder_pem_driver);