blob: d4a7797614725d153776fc1c7ea079b6deeacbac [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
23#include "pci-host-common.h"
24
25#define PEM_CFG_WR 0x28
26#define PEM_CFG_RD 0x30
27
28struct thunder_pem_pci {
29 struct gen_pci gen_pci;
30 u32 ea_entry[3];
31 void __iomem *pem_reg_base;
32};
33
34static void __iomem *thunder_pem_map_bus(struct pci_bus *bus,
35 unsigned int devfn, int where)
36{
37 struct gen_pci *pci = bus->sysdata;
38 resource_size_t idx = bus->number - pci->cfg.bus_range->start;
39
40 return pci->cfg.win[idx] + ((devfn << 16) | where);
41}
42
43static int thunder_pem_bridge_read(struct pci_bus *bus, unsigned int devfn,
44 int where, int size, u32 *val)
45{
46 u64 read_val;
47 struct thunder_pem_pci *pem_pci;
48 struct gen_pci *pci = bus->sysdata;
49
50 pem_pci = container_of(pci, struct thunder_pem_pci, gen_pci);
51
52 if (devfn != 0 || where >= 2048) {
53 *val = ~0;
54 return PCIBIOS_DEVICE_NOT_FOUND;
55 }
56
57 /*
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.
61 */
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);
65 read_val >>= 32;
66
67 /*
68 * The config space contains some garbage, fix it up. Also
69 * synthesize an EA capability for the BAR used by MSI-X.
70 */
71 switch (where & ~3) {
72 case 0x40:
73 read_val &= 0xffff00ff;
74 read_val |= 0x00007000; /* Skip MSI CAP */
75 break;
76 case 0x70: /* Express Cap */
77 /* PME interrupt on vector 2*/
78 read_val |= (2u << 25);
79 break;
80 case 0xb0: /* MSI-X Cap */
81 /* TableSize=4, Next Cap is EA */
82 read_val &= 0xc00000ff;
83 read_val |= 0x0003bc00;
84 break;
85 case 0xb4:
86 /* Table offset=0, BIR=0 */
87 read_val = 0x00000000;
88 break;
89 case 0xb8:
90 /* BPA offset=0xf0000, BIR=0 */
91 read_val = 0x000f0000;
92 break;
93 case 0xbc:
94 /* EA, 1 entry, no next Cap */
95 read_val = 0x00010014;
96 break;
97 case 0xc0:
98 /* DW2 for type-1 */
99 read_val = 0x00000000;
100 break;
101 case 0xc4:
102 /* Entry BEI=0, PP=0x00, SP=0xff, ES=3 */
103 read_val = 0x80ff0003;
104 break;
105 case 0xc8:
106 read_val = pem_pci->ea_entry[0];
107 break;
108 case 0xcc:
109 read_val = pem_pci->ea_entry[1];
110 break;
111 case 0xd0:
112 read_val = pem_pci->ea_entry[2];
113 break;
114 default:
115 break;
116 }
117 read_val >>= (8 * (where & 3));
118 switch (size) {
119 case 1:
120 read_val &= 0xff;
121 break;
122 case 2:
123 read_val &= 0xffff;
124 break;
125 default:
126 break;
127 }
128 *val = read_val;
129 return PCIBIOS_SUCCESSFUL;
130}
131
132static int thunder_pem_config_read(struct pci_bus *bus, unsigned int devfn,
133 int where, int size, u32 *val)
134{
135 struct gen_pci *pci = bus->sysdata;
136
137 if (bus->number < pci->cfg.bus_range->start ||
138 bus->number > pci->cfg.bus_range->end)
139 return PCIBIOS_DEVICE_NOT_FOUND;
140
141 /*
142 * The first device on the bus is the PEM PCIe bridge.
143 * Special case its config access.
144 */
145 if (bus->number == pci->cfg.bus_range->start)
146 return thunder_pem_bridge_read(bus, devfn, where, size, val);
147
148 return pci_generic_config_read(bus, devfn, where, size, val);
149}
150
151/*
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.
155 */
David Daney93bf9072016-04-11 16:29:32 -0700156static u32 thunder_pem_bridge_w1c_bits(u64 where_aligned)
David Daneyf12b76e2016-03-04 14:31:47 -0800157{
158 u32 w1c_bits = 0;
159
David Daney93bf9072016-04-11 16:29:32 -0700160 switch (where_aligned) {
David Daneyf12b76e2016-03-04 14:31:47 -0800161 case 0x04: /* Command/Status */
162 case 0x1c: /* Base and I/O Limit/Secondary Status */
163 w1c_bits = 0xff000000;
164 break;
165 case 0x44: /* Power Management Control and Status */
166 w1c_bits = 0xfffffe00;
167 break;
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;
174 break;
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;
180 break;
181 default:
182 break;
183 }
184 return w1c_bits;
185}
186
David Daney93bf9072016-04-11 16:29:32 -0700187/* Some bits must be written to one so they appear to be read-only. */
188static u32 thunder_pem_bridge_w1_bits(u64 where_aligned)
189{
190 u32 w1_bits;
191
192 switch (where_aligned) {
193 case 0x1c: /* I/O Base / I/O Limit, Secondary Status */
194 /* Force 32-bit I/O addressing. */
195 w1_bits = 0x0101;
196 break;
197 case 0x24: /* Prefetchable Memory Base / Prefetchable Memory Limit */
198 /* Force 64-bit addressing */
199 w1_bits = 0x00010001;
200 break;
201 default:
202 w1_bits = 0;
203 break;
204 }
205 return w1_bits;
206}
207
David Daneyf12b76e2016-03-04 14:31:47 -0800208static int thunder_pem_bridge_write(struct pci_bus *bus, unsigned int devfn,
209 int where, int size, u32 val)
210{
211 struct gen_pci *pci = bus->sysdata;
212 struct thunder_pem_pci *pem_pci;
213 u64 write_val, read_val;
David Daney93bf9072016-04-11 16:29:32 -0700214 u64 where_aligned = where & ~3ull;
David Daneyf12b76e2016-03-04 14:31:47 -0800215 u32 mask = 0;
216
217 pem_pci = container_of(pci, struct thunder_pem_pci, gen_pci);
218
219 if (devfn != 0 || where >= 2048)
220 return PCIBIOS_DEVICE_NOT_FOUND;
221
222 /*
223 * 32-bit accesses only. If the write is for a size smaller
224 * than 32-bits, we must first read the 32-bit value and merge
225 * in the desired bits and then write the whole 32-bits back
226 * out.
227 */
228 switch (size) {
229 case 1:
David Daney93bf9072016-04-11 16:29:32 -0700230 writeq(where_aligned, pem_pci->pem_reg_base + PEM_CFG_RD);
David Daneyf12b76e2016-03-04 14:31:47 -0800231 read_val = readq(pem_pci->pem_reg_base + PEM_CFG_RD);
232 read_val >>= 32;
233 mask = ~(0xff << (8 * (where & 3)));
234 read_val &= mask;
235 val = (val & 0xff) << (8 * (where & 3));
236 val |= (u32)read_val;
237 break;
238 case 2:
David Daney93bf9072016-04-11 16:29:32 -0700239 writeq(where_aligned, pem_pci->pem_reg_base + PEM_CFG_RD);
David Daneyf12b76e2016-03-04 14:31:47 -0800240 read_val = readq(pem_pci->pem_reg_base + PEM_CFG_RD);
241 read_val >>= 32;
242 mask = ~(0xffff << (8 * (where & 3)));
243 read_val &= mask;
244 val = (val & 0xffff) << (8 * (where & 3));
245 val |= (u32)read_val;
246 break;
247 default:
248 break;
249 }
250
251 /*
252 * By expanding the write width to 32 bits, we may
253 * inadvertently hit some W1C bits that were not intended to
254 * be written. Calculate the mask that must be applied to the
255 * data to be written to avoid these cases.
256 */
257 if (mask) {
258 u32 w1c_bits = thunder_pem_bridge_w1c_bits(where);
259
260 if (w1c_bits) {
261 mask &= w1c_bits;
262 val &= ~mask;
263 }
264 }
265
266 /*
David Daney93bf9072016-04-11 16:29:32 -0700267 * Some bits must be read-only with value of one. Since the
268 * access method allows these to be cleared if a zero is
269 * written, force them to one before writing.
270 */
271 val |= thunder_pem_bridge_w1_bits(where_aligned);
272
273 /*
David Daneyf12b76e2016-03-04 14:31:47 -0800274 * Low order bits are the config address, the high order 32
275 * bits are the data to be written.
276 */
David Daney93bf9072016-04-11 16:29:32 -0700277 write_val = (((u64)val) << 32) | where_aligned;
David Daneyf12b76e2016-03-04 14:31:47 -0800278 writeq(write_val, pem_pci->pem_reg_base + PEM_CFG_WR);
279 return PCIBIOS_SUCCESSFUL;
280}
281
282static int thunder_pem_config_write(struct pci_bus *bus, unsigned int devfn,
283 int where, int size, u32 val)
284{
285 struct gen_pci *pci = bus->sysdata;
286
287 if (bus->number < pci->cfg.bus_range->start ||
288 bus->number > pci->cfg.bus_range->end)
289 return PCIBIOS_DEVICE_NOT_FOUND;
290 /*
291 * The first device on the bus is the PEM PCIe bridge.
292 * Special case its config access.
293 */
294 if (bus->number == pci->cfg.bus_range->start)
295 return thunder_pem_bridge_write(bus, devfn, where, size, val);
296
297
298 return pci_generic_config_write(bus, devfn, where, size, val);
299}
300
301static struct gen_pci_cfg_bus_ops thunder_pem_bus_ops = {
302 .bus_shift = 24,
303 .ops = {
304 .map_bus = thunder_pem_map_bus,
305 .read = thunder_pem_config_read,
306 .write = thunder_pem_config_write,
307 }
308};
309
310static const struct of_device_id thunder_pem_of_match[] = {
311 { .compatible = "cavium,pci-host-thunder-pem",
312 .data = &thunder_pem_bus_ops },
313
314 { },
315};
316MODULE_DEVICE_TABLE(of, thunder_pem_of_match);
317
318static int thunder_pem_probe(struct platform_device *pdev)
319{
320 struct device *dev = &pdev->dev;
321 const struct of_device_id *of_id;
322 resource_size_t bar4_start;
323 struct resource *res_pem;
324 struct thunder_pem_pci *pem_pci;
325
326 pem_pci = devm_kzalloc(dev, sizeof(*pem_pci), GFP_KERNEL);
327 if (!pem_pci)
328 return -ENOMEM;
329
330 of_id = of_match_node(thunder_pem_of_match, dev->of_node);
331 pem_pci->gen_pci.cfg.ops = (struct gen_pci_cfg_bus_ops *)of_id->data;
332
333 /*
334 * The second register range is the PEM bridge to the PCIe
335 * bus. It has a different config access method than those
336 * devices behind the bridge.
337 */
338 res_pem = platform_get_resource(pdev, IORESOURCE_MEM, 1);
339 if (!res_pem) {
340 dev_err(dev, "missing \"reg[1]\"property\n");
341 return -EINVAL;
342 }
343
344 pem_pci->pem_reg_base = devm_ioremap(dev, res_pem->start, 0x10000);
345 if (!pem_pci->pem_reg_base)
346 return -ENOMEM;
347
348 /*
349 * The MSI-X BAR for the PEM and AER interrupts is located at
350 * a fixed offset from the PEM register base. Generate a
351 * fragment of the synthesized Enhanced Allocation capability
352 * structure here for the BAR.
353 */
354 bar4_start = res_pem->start + 0xf00000;
355 pem_pci->ea_entry[0] = (u32)bar4_start | 2;
356 pem_pci->ea_entry[1] = (u32)(res_pem->end - bar4_start) & ~3u;
357 pem_pci->ea_entry[2] = (u32)(bar4_start >> 32);
358
359 return pci_host_common_probe(pdev, &pem_pci->gen_pci);
360}
361
362static struct platform_driver thunder_pem_driver = {
363 .driver = {
364 .name = KBUILD_MODNAME,
365 .of_match_table = thunder_pem_of_match,
366 },
367 .probe = thunder_pem_probe,
368};
369module_platform_driver(thunder_pem_driver);
370
371MODULE_DESCRIPTION("Thunder PEM PCIe host driver");
372MODULE_LICENSE("GPL v2");