blob: a7b61ce3511732af53cdd233509b92d3f25d01a4 [file] [log] [blame]
David Daneyf12b76e2016-03-04 14:31:47 -08001/*
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.
5 *
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.
10 *
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/>.
13 *
14 * Copyright (C) 2015 - 2016 Cavium, Inc.
15 */
16
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>
22
Jayachandran C1958e712016-05-11 17:34:46 -050023#include "../ecam.h"
David Daneyf12b76e2016-03-04 14:31:47 -080024
25#define PEM_CFG_WR 0x28
26#define PEM_CFG_RD 0x30
27
28struct thunder_pem_pci {
David Daneyf12b76e2016-03-04 14:31:47 -080029 u32 ea_entry[3];
30 void __iomem *pem_reg_base;
31};
32
David Daneyf12b76e2016-03-04 14:31:47 -080033static int thunder_pem_bridge_read(struct pci_bus *bus, unsigned int devfn,
34 int where, int size, u32 *val)
35{
36 u64 read_val;
Jayachandran C1958e712016-05-11 17:34:46 -050037 struct pci_config_window *cfg = bus->sysdata;
38 struct thunder_pem_pci *pem_pci = (struct thunder_pem_pci *)cfg->priv;
David Daneyf12b76e2016-03-04 14:31:47 -080039
40 if (devfn != 0 || where >= 2048) {
41 *val = ~0;
42 return PCIBIOS_DEVICE_NOT_FOUND;
43 }
44
45 /*
46 * 32-bit accesses only. Write the address to the low order
47 * bits of PEM_CFG_RD, then trigger the read by reading back.
48 * The config data lands in the upper 32-bits of PEM_CFG_RD.
49 */
50 read_val = where & ~3ull;
51 writeq(read_val, pem_pci->pem_reg_base + PEM_CFG_RD);
52 read_val = readq(pem_pci->pem_reg_base + PEM_CFG_RD);
53 read_val >>= 32;
54
55 /*
56 * The config space contains some garbage, fix it up. Also
57 * synthesize an EA capability for the BAR used by MSI-X.
58 */
59 switch (where & ~3) {
60 case 0x40:
61 read_val &= 0xffff00ff;
62 read_val |= 0x00007000; /* Skip MSI CAP */
63 break;
64 case 0x70: /* Express Cap */
65 /* PME interrupt on vector 2*/
66 read_val |= (2u << 25);
67 break;
68 case 0xb0: /* MSI-X Cap */
69 /* TableSize=4, Next Cap is EA */
70 read_val &= 0xc00000ff;
71 read_val |= 0x0003bc00;
72 break;
73 case 0xb4:
74 /* Table offset=0, BIR=0 */
75 read_val = 0x00000000;
76 break;
77 case 0xb8:
78 /* BPA offset=0xf0000, BIR=0 */
79 read_val = 0x000f0000;
80 break;
81 case 0xbc:
82 /* EA, 1 entry, no next Cap */
83 read_val = 0x00010014;
84 break;
85 case 0xc0:
86 /* DW2 for type-1 */
87 read_val = 0x00000000;
88 break;
89 case 0xc4:
90 /* Entry BEI=0, PP=0x00, SP=0xff, ES=3 */
91 read_val = 0x80ff0003;
92 break;
93 case 0xc8:
94 read_val = pem_pci->ea_entry[0];
95 break;
96 case 0xcc:
97 read_val = pem_pci->ea_entry[1];
98 break;
99 case 0xd0:
100 read_val = pem_pci->ea_entry[2];
101 break;
102 default:
103 break;
104 }
105 read_val >>= (8 * (where & 3));
106 switch (size) {
107 case 1:
108 read_val &= 0xff;
109 break;
110 case 2:
111 read_val &= 0xffff;
112 break;
113 default:
114 break;
115 }
116 *val = read_val;
117 return PCIBIOS_SUCCESSFUL;
118}
119
120static int thunder_pem_config_read(struct pci_bus *bus, unsigned int devfn,
121 int where, int size, u32 *val)
122{
Jayachandran C1958e712016-05-11 17:34:46 -0500123 struct pci_config_window *cfg = bus->sysdata;
David Daneyf12b76e2016-03-04 14:31:47 -0800124
Jayachandran C1958e712016-05-11 17:34:46 -0500125 if (bus->number < cfg->busr.start ||
126 bus->number > cfg->busr.end)
David Daneyf12b76e2016-03-04 14:31:47 -0800127 return PCIBIOS_DEVICE_NOT_FOUND;
128
129 /*
130 * The first device on the bus is the PEM PCIe bridge.
131 * Special case its config access.
132 */
Jayachandran C1958e712016-05-11 17:34:46 -0500133 if (bus->number == cfg->busr.start)
David Daneyf12b76e2016-03-04 14:31:47 -0800134 return thunder_pem_bridge_read(bus, devfn, where, size, val);
135
136 return pci_generic_config_read(bus, devfn, where, size, val);
137}
138
139/*
140 * Some of the w1c_bits below also include read-only or non-writable
141 * reserved bits, this makes the code simpler and is OK as the bits
142 * are not affected by writing zeros to them.
143 */
144static u32 thunder_pem_bridge_w1c_bits(int where)
145{
146 u32 w1c_bits = 0;
147
148 switch (where & ~3) {
149 case 0x04: /* Command/Status */
150 case 0x1c: /* Base and I/O Limit/Secondary Status */
151 w1c_bits = 0xff000000;
152 break;
153 case 0x44: /* Power Management Control and Status */
154 w1c_bits = 0xfffffe00;
155 break;
156 case 0x78: /* Device Control/Device Status */
157 case 0x80: /* Link Control/Link Status */
158 case 0x88: /* Slot Control/Slot Status */
159 case 0x90: /* Root Status */
160 case 0xa0: /* Link Control 2 Registers/Link Status 2 */
161 w1c_bits = 0xffff0000;
162 break;
163 case 0x104: /* Uncorrectable Error Status */
164 case 0x110: /* Correctable Error Status */
165 case 0x130: /* Error Status */
166 case 0x160: /* Link Control 4 */
167 w1c_bits = 0xffffffff;
168 break;
169 default:
170 break;
171 }
172 return w1c_bits;
173}
174
175static int thunder_pem_bridge_write(struct pci_bus *bus, unsigned int devfn,
176 int where, int size, u32 val)
177{
Jayachandran C1958e712016-05-11 17:34:46 -0500178 struct pci_config_window *cfg = bus->sysdata;
179 struct thunder_pem_pci *pem_pci = (struct thunder_pem_pci *)cfg->priv;
David Daneyf12b76e2016-03-04 14:31:47 -0800180 u64 write_val, read_val;
181 u32 mask = 0;
182
David Daneyf12b76e2016-03-04 14:31:47 -0800183
184 if (devfn != 0 || where >= 2048)
185 return PCIBIOS_DEVICE_NOT_FOUND;
186
187 /*
188 * 32-bit accesses only. If the write is for a size smaller
189 * than 32-bits, we must first read the 32-bit value and merge
190 * in the desired bits and then write the whole 32-bits back
191 * out.
192 */
193 switch (size) {
194 case 1:
195 read_val = where & ~3ull;
196 writeq(read_val, pem_pci->pem_reg_base + PEM_CFG_RD);
197 read_val = readq(pem_pci->pem_reg_base + PEM_CFG_RD);
198 read_val >>= 32;
199 mask = ~(0xff << (8 * (where & 3)));
200 read_val &= mask;
201 val = (val & 0xff) << (8 * (where & 3));
202 val |= (u32)read_val;
203 break;
204 case 2:
205 read_val = where & ~3ull;
206 writeq(read_val, pem_pci->pem_reg_base + PEM_CFG_RD);
207 read_val = readq(pem_pci->pem_reg_base + PEM_CFG_RD);
208 read_val >>= 32;
209 mask = ~(0xffff << (8 * (where & 3)));
210 read_val &= mask;
211 val = (val & 0xffff) << (8 * (where & 3));
212 val |= (u32)read_val;
213 break;
214 default:
215 break;
216 }
217
218 /*
219 * By expanding the write width to 32 bits, we may
220 * inadvertently hit some W1C bits that were not intended to
221 * be written. Calculate the mask that must be applied to the
222 * data to be written to avoid these cases.
223 */
224 if (mask) {
225 u32 w1c_bits = thunder_pem_bridge_w1c_bits(where);
226
227 if (w1c_bits) {
228 mask &= w1c_bits;
229 val &= ~mask;
230 }
231 }
232
233 /*
234 * Low order bits are the config address, the high order 32
235 * bits are the data to be written.
236 */
237 write_val = where & ~3ull;
238 write_val |= (((u64)val) << 32);
239 writeq(write_val, pem_pci->pem_reg_base + PEM_CFG_WR);
240 return PCIBIOS_SUCCESSFUL;
241}
242
243static int thunder_pem_config_write(struct pci_bus *bus, unsigned int devfn,
244 int where, int size, u32 val)
245{
Jayachandran C1958e712016-05-11 17:34:46 -0500246 struct pci_config_window *cfg = bus->sysdata;
David Daneyf12b76e2016-03-04 14:31:47 -0800247
Jayachandran C1958e712016-05-11 17:34:46 -0500248 if (bus->number < cfg->busr.start ||
249 bus->number > cfg->busr.end)
David Daneyf12b76e2016-03-04 14:31:47 -0800250 return PCIBIOS_DEVICE_NOT_FOUND;
251 /*
252 * The first device on the bus is the PEM PCIe bridge.
253 * Special case its config access.
254 */
Jayachandran C1958e712016-05-11 17:34:46 -0500255 if (bus->number == cfg->busr.start)
David Daneyf12b76e2016-03-04 14:31:47 -0800256 return thunder_pem_bridge_write(bus, devfn, where, size, val);
257
258
259 return pci_generic_config_write(bus, devfn, where, size, val);
260}
261
Jayachandran C1958e712016-05-11 17:34:46 -0500262static int thunder_pem_init(struct device *dev, struct pci_config_window *cfg)
David Daneyf12b76e2016-03-04 14:31:47 -0800263{
David Daneyf12b76e2016-03-04 14:31:47 -0800264 resource_size_t bar4_start;
265 struct resource *res_pem;
266 struct thunder_pem_pci *pem_pci;
Jayachandran C1958e712016-05-11 17:34:46 -0500267 struct platform_device *pdev;
268
269 /* Only OF support for now */
270 if (!dev->of_node)
271 return -EINVAL;
David Daneyf12b76e2016-03-04 14:31:47 -0800272
273 pem_pci = devm_kzalloc(dev, sizeof(*pem_pci), GFP_KERNEL);
274 if (!pem_pci)
275 return -ENOMEM;
276
Jayachandran C1958e712016-05-11 17:34:46 -0500277 pdev = to_platform_device(dev);
David Daneyf12b76e2016-03-04 14:31:47 -0800278
279 /*
280 * The second register range is the PEM bridge to the PCIe
281 * bus. It has a different config access method than those
282 * devices behind the bridge.
283 */
284 res_pem = platform_get_resource(pdev, IORESOURCE_MEM, 1);
285 if (!res_pem) {
286 dev_err(dev, "missing \"reg[1]\"property\n");
287 return -EINVAL;
288 }
289
290 pem_pci->pem_reg_base = devm_ioremap(dev, res_pem->start, 0x10000);
291 if (!pem_pci->pem_reg_base)
292 return -ENOMEM;
293
294 /*
295 * The MSI-X BAR for the PEM and AER interrupts is located at
296 * a fixed offset from the PEM register base. Generate a
297 * fragment of the synthesized Enhanced Allocation capability
298 * structure here for the BAR.
299 */
300 bar4_start = res_pem->start + 0xf00000;
301 pem_pci->ea_entry[0] = (u32)bar4_start | 2;
302 pem_pci->ea_entry[1] = (u32)(res_pem->end - bar4_start) & ~3u;
303 pem_pci->ea_entry[2] = (u32)(bar4_start >> 32);
304
Jayachandran C1958e712016-05-11 17:34:46 -0500305 cfg->priv = pem_pci;
306 return 0;
307}
308
309static struct pci_ecam_ops pci_thunder_pem_ops = {
310 .bus_shift = 24,
311 .init = thunder_pem_init,
312 .pci_ops = {
313 .map_bus = pci_ecam_map_bus,
314 .read = thunder_pem_config_read,
315 .write = thunder_pem_config_write,
316 }
317};
318
319static const struct of_device_id thunder_pem_of_match[] = {
320 { .compatible = "cavium,pci-host-thunder-pem" },
321 { },
322};
323MODULE_DEVICE_TABLE(of, thunder_pem_of_match);
324
325static int thunder_pem_probe(struct platform_device *pdev)
326{
327 return pci_host_common_probe(pdev, &pci_thunder_pem_ops);
David Daneyf12b76e2016-03-04 14:31:47 -0800328}
329
330static struct platform_driver thunder_pem_driver = {
331 .driver = {
332 .name = KBUILD_MODNAME,
333 .of_match_table = thunder_pem_of_match,
334 },
335 .probe = thunder_pem_probe,
336};
337module_platform_driver(thunder_pem_driver);
338
339MODULE_DESCRIPTION("Thunder PEM PCIe host driver");
340MODULE_LICENSE("GPL v2");