blob: 5a2483e125a3fea92cf3782b14449e40ca2763ee [file] [log] [blame]
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001// SPDX-License-Identifier: GPL-2.0
2/*
3 * PCIe driver for Marvell Armada 370 and Armada XP SoCs
4 *
5 * Author: Thomas Petazzoni <thomas.petazzoni@free-electrons.com>
6 */
7
8#include <linux/kernel.h>
9#include <linux/pci.h>
10#include <linux/clk.h>
11#include <linux/delay.h>
12#include <linux/gpio.h>
13#include <linux/init.h>
14#include <linux/mbus.h>
15#include <linux/msi.h>
16#include <linux/slab.h>
17#include <linux/platform_device.h>
18#include <linux/of_address.h>
19#include <linux/of_irq.h>
20#include <linux/of_gpio.h>
21#include <linux/of_pci.h>
22#include <linux/of_platform.h>
23
24#include "../pci.h"
David Brazdil0f672f62019-12-10 10:32:29 +000025#include "../pci-bridge-emul.h"
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000026
27/*
28 * PCIe unit register offsets.
29 */
30#define PCIE_DEV_ID_OFF 0x0000
31#define PCIE_CMD_OFF 0x0004
32#define PCIE_DEV_REV_OFF 0x0008
33#define PCIE_BAR_LO_OFF(n) (0x0010 + ((n) << 3))
34#define PCIE_BAR_HI_OFF(n) (0x0014 + ((n) << 3))
35#define PCIE_CAP_PCIEXP 0x0060
36#define PCIE_HEADER_LOG_4_OFF 0x0128
37#define PCIE_BAR_CTRL_OFF(n) (0x1804 + (((n) - 1) * 4))
38#define PCIE_WIN04_CTRL_OFF(n) (0x1820 + ((n) << 4))
39#define PCIE_WIN04_BASE_OFF(n) (0x1824 + ((n) << 4))
40#define PCIE_WIN04_REMAP_OFF(n) (0x182c + ((n) << 4))
41#define PCIE_WIN5_CTRL_OFF 0x1880
42#define PCIE_WIN5_BASE_OFF 0x1884
43#define PCIE_WIN5_REMAP_OFF 0x188c
44#define PCIE_CONF_ADDR_OFF 0x18f8
45#define PCIE_CONF_ADDR_EN 0x80000000
46#define PCIE_CONF_REG(r) ((((r) & 0xf00) << 16) | ((r) & 0xfc))
47#define PCIE_CONF_BUS(b) (((b) & 0xff) << 16)
48#define PCIE_CONF_DEV(d) (((d) & 0x1f) << 11)
49#define PCIE_CONF_FUNC(f) (((f) & 0x7) << 8)
50#define PCIE_CONF_ADDR(bus, devfn, where) \
51 (PCIE_CONF_BUS(bus) | PCIE_CONF_DEV(PCI_SLOT(devfn)) | \
52 PCIE_CONF_FUNC(PCI_FUNC(devfn)) | PCIE_CONF_REG(where) | \
53 PCIE_CONF_ADDR_EN)
54#define PCIE_CONF_DATA_OFF 0x18fc
55#define PCIE_MASK_OFF 0x1910
56#define PCIE_MASK_ENABLE_INTS 0x0f000000
57#define PCIE_CTRL_OFF 0x1a00
58#define PCIE_CTRL_X1_MODE 0x0001
59#define PCIE_STAT_OFF 0x1a04
60#define PCIE_STAT_BUS 0xff00
61#define PCIE_STAT_DEV 0x1f0000
62#define PCIE_STAT_LINK_DOWN BIT(0)
63#define PCIE_RC_RTSTA 0x1a14
64#define PCIE_DEBUG_CTRL 0x1a60
65#define PCIE_DEBUG_SOFT_RESET BIT(20)
66
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000067struct mvebu_pcie_port;
68
69/* Structure representing all PCIe interfaces */
70struct mvebu_pcie {
71 struct platform_device *pdev;
72 struct mvebu_pcie_port *ports;
73 struct msi_controller *msi;
74 struct list_head resources;
75 struct resource io;
76 struct resource realio;
77 struct resource mem;
78 struct resource busn;
79 int nports;
80};
81
82struct mvebu_pcie_window {
83 phys_addr_t base;
84 phys_addr_t remap;
85 size_t size;
86};
87
88/* Structure representing one PCIe interface */
89struct mvebu_pcie_port {
90 char *name;
91 void __iomem *base;
92 u32 port;
93 u32 lane;
94 int devfn;
95 unsigned int mem_target;
96 unsigned int mem_attr;
97 unsigned int io_target;
98 unsigned int io_attr;
99 struct clk *clk;
100 struct gpio_desc *reset_gpio;
101 char *reset_name;
David Brazdil0f672f62019-12-10 10:32:29 +0000102 struct pci_bridge_emul bridge;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000103 struct device_node *dn;
104 struct mvebu_pcie *pcie;
105 struct mvebu_pcie_window memwin;
106 struct mvebu_pcie_window iowin;
107 u32 saved_pcie_stat;
Olivier Deprez0e641232021-09-23 10:07:05 +0200108 struct resource regs;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000109};
110
111static inline void mvebu_writel(struct mvebu_pcie_port *port, u32 val, u32 reg)
112{
113 writel(val, port->base + reg);
114}
115
116static inline u32 mvebu_readl(struct mvebu_pcie_port *port, u32 reg)
117{
118 return readl(port->base + reg);
119}
120
121static inline bool mvebu_has_ioport(struct mvebu_pcie_port *port)
122{
123 return port->io_target != -1 && port->io_attr != -1;
124}
125
126static bool mvebu_pcie_link_up(struct mvebu_pcie_port *port)
127{
128 return !(mvebu_readl(port, PCIE_STAT_OFF) & PCIE_STAT_LINK_DOWN);
129}
130
131static void mvebu_pcie_set_local_bus_nr(struct mvebu_pcie_port *port, int nr)
132{
133 u32 stat;
134
135 stat = mvebu_readl(port, PCIE_STAT_OFF);
136 stat &= ~PCIE_STAT_BUS;
137 stat |= nr << 8;
138 mvebu_writel(port, stat, PCIE_STAT_OFF);
139}
140
141static void mvebu_pcie_set_local_dev_nr(struct mvebu_pcie_port *port, int nr)
142{
143 u32 stat;
144
145 stat = mvebu_readl(port, PCIE_STAT_OFF);
146 stat &= ~PCIE_STAT_DEV;
147 stat |= nr << 16;
148 mvebu_writel(port, stat, PCIE_STAT_OFF);
149}
150
151/*
152 * Setup PCIE BARs and Address Decode Wins:
Olivier Deprez0e641232021-09-23 10:07:05 +0200153 * BAR[0] -> internal registers (needed for MSI)
154 * BAR[1] -> covers all DRAM banks
155 * BAR[2] -> Disabled
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000156 * WIN[0-3] -> DRAM bank[0-3]
157 */
158static void mvebu_pcie_setup_wins(struct mvebu_pcie_port *port)
159{
160 const struct mbus_dram_target_info *dram;
161 u32 size;
162 int i;
163
164 dram = mv_mbus_dram_info();
165
166 /* First, disable and clear BARs and windows. */
167 for (i = 1; i < 3; i++) {
168 mvebu_writel(port, 0, PCIE_BAR_CTRL_OFF(i));
169 mvebu_writel(port, 0, PCIE_BAR_LO_OFF(i));
170 mvebu_writel(port, 0, PCIE_BAR_HI_OFF(i));
171 }
172
173 for (i = 0; i < 5; i++) {
174 mvebu_writel(port, 0, PCIE_WIN04_CTRL_OFF(i));
175 mvebu_writel(port, 0, PCIE_WIN04_BASE_OFF(i));
176 mvebu_writel(port, 0, PCIE_WIN04_REMAP_OFF(i));
177 }
178
179 mvebu_writel(port, 0, PCIE_WIN5_CTRL_OFF);
180 mvebu_writel(port, 0, PCIE_WIN5_BASE_OFF);
181 mvebu_writel(port, 0, PCIE_WIN5_REMAP_OFF);
182
183 /* Setup windows for DDR banks. Count total DDR size on the fly. */
184 size = 0;
185 for (i = 0; i < dram->num_cs; i++) {
186 const struct mbus_dram_window *cs = dram->cs + i;
187
188 mvebu_writel(port, cs->base & 0xffff0000,
189 PCIE_WIN04_BASE_OFF(i));
190 mvebu_writel(port, 0, PCIE_WIN04_REMAP_OFF(i));
191 mvebu_writel(port,
192 ((cs->size - 1) & 0xffff0000) |
193 (cs->mbus_attr << 8) |
194 (dram->mbus_dram_target_id << 4) | 1,
195 PCIE_WIN04_CTRL_OFF(i));
196
197 size += cs->size;
198 }
199
200 /* Round up 'size' to the nearest power of two. */
201 if ((size & (size - 1)) != 0)
202 size = 1 << fls(size);
203
204 /* Setup BAR[1] to all DRAM banks. */
205 mvebu_writel(port, dram->cs[0].base, PCIE_BAR_LO_OFF(1));
206 mvebu_writel(port, 0, PCIE_BAR_HI_OFF(1));
207 mvebu_writel(port, ((size - 1) & 0xffff0000) | 1,
208 PCIE_BAR_CTRL_OFF(1));
Olivier Deprez0e641232021-09-23 10:07:05 +0200209
210 /*
211 * Point BAR[0] to the device's internal registers.
212 */
213 mvebu_writel(port, round_down(port->regs.start, SZ_1M), PCIE_BAR_LO_OFF(0));
214 mvebu_writel(port, 0, PCIE_BAR_HI_OFF(0));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000215}
216
217static void mvebu_pcie_setup_hw(struct mvebu_pcie_port *port)
218{
219 u32 cmd, mask;
220
221 /* Point PCIe unit MBUS decode windows to DRAM space. */
222 mvebu_pcie_setup_wins(port);
223
224 /* Master + slave enable. */
225 cmd = mvebu_readl(port, PCIE_CMD_OFF);
226 cmd |= PCI_COMMAND_IO;
227 cmd |= PCI_COMMAND_MEMORY;
228 cmd |= PCI_COMMAND_MASTER;
229 mvebu_writel(port, cmd, PCIE_CMD_OFF);
230
231 /* Enable interrupt lines A-D. */
232 mask = mvebu_readl(port, PCIE_MASK_OFF);
233 mask |= PCIE_MASK_ENABLE_INTS;
234 mvebu_writel(port, mask, PCIE_MASK_OFF);
235}
236
237static int mvebu_pcie_hw_rd_conf(struct mvebu_pcie_port *port,
238 struct pci_bus *bus,
239 u32 devfn, int where, int size, u32 *val)
240{
241 void __iomem *conf_data = port->base + PCIE_CONF_DATA_OFF;
242
243 mvebu_writel(port, PCIE_CONF_ADDR(bus->number, devfn, where),
244 PCIE_CONF_ADDR_OFF);
245
246 switch (size) {
247 case 1:
248 *val = readb_relaxed(conf_data + (where & 3));
249 break;
250 case 2:
251 *val = readw_relaxed(conf_data + (where & 2));
252 break;
253 case 4:
254 *val = readl_relaxed(conf_data);
255 break;
256 }
257
258 return PCIBIOS_SUCCESSFUL;
259}
260
261static int mvebu_pcie_hw_wr_conf(struct mvebu_pcie_port *port,
262 struct pci_bus *bus,
263 u32 devfn, int where, int size, u32 val)
264{
265 void __iomem *conf_data = port->base + PCIE_CONF_DATA_OFF;
266
267 mvebu_writel(port, PCIE_CONF_ADDR(bus->number, devfn, where),
268 PCIE_CONF_ADDR_OFF);
269
270 switch (size) {
271 case 1:
272 writeb(val, conf_data + (where & 3));
273 break;
274 case 2:
275 writew(val, conf_data + (where & 2));
276 break;
277 case 4:
278 writel(val, conf_data);
279 break;
280 default:
281 return PCIBIOS_BAD_REGISTER_NUMBER;
282 }
283
284 return PCIBIOS_SUCCESSFUL;
285}
286
287/*
288 * Remove windows, starting from the largest ones to the smallest
289 * ones.
290 */
291static void mvebu_pcie_del_windows(struct mvebu_pcie_port *port,
292 phys_addr_t base, size_t size)
293{
294 while (size) {
295 size_t sz = 1 << (fls(size) - 1);
296
297 mvebu_mbus_del_window(base, sz);
298 base += sz;
299 size -= sz;
300 }
301}
302
303/*
304 * MBus windows can only have a power of two size, but PCI BARs do not
305 * have this constraint. Therefore, we have to split the PCI BAR into
306 * areas each having a power of two size. We start from the largest
307 * one (i.e highest order bit set in the size).
308 */
309static void mvebu_pcie_add_windows(struct mvebu_pcie_port *port,
310 unsigned int target, unsigned int attribute,
311 phys_addr_t base, size_t size,
312 phys_addr_t remap)
313{
314 size_t size_mapped = 0;
315
316 while (size) {
317 size_t sz = 1 << (fls(size) - 1);
318 int ret;
319
320 ret = mvebu_mbus_add_window_remap_by_id(target, attribute, base,
321 sz, remap);
322 if (ret) {
323 phys_addr_t end = base + sz - 1;
324
325 dev_err(&port->pcie->pdev->dev,
326 "Could not create MBus window at [mem %pa-%pa]: %d\n",
327 &base, &end, ret);
328 mvebu_pcie_del_windows(port, base - size_mapped,
329 size_mapped);
330 return;
331 }
332
333 size -= sz;
334 size_mapped += sz;
335 base += sz;
336 if (remap != MVEBU_MBUS_NO_REMAP)
337 remap += sz;
338 }
339}
340
341static void mvebu_pcie_set_window(struct mvebu_pcie_port *port,
342 unsigned int target, unsigned int attribute,
343 const struct mvebu_pcie_window *desired,
344 struct mvebu_pcie_window *cur)
345{
346 if (desired->base == cur->base && desired->remap == cur->remap &&
347 desired->size == cur->size)
348 return;
349
350 if (cur->size != 0) {
351 mvebu_pcie_del_windows(port, cur->base, cur->size);
352 cur->size = 0;
353 cur->base = 0;
354
355 /*
356 * If something tries to change the window while it is enabled
357 * the change will not be done atomically. That would be
358 * difficult to do in the general case.
359 */
360 }
361
362 if (desired->size == 0)
363 return;
364
365 mvebu_pcie_add_windows(port, target, attribute, desired->base,
366 desired->size, desired->remap);
367 *cur = *desired;
368}
369
370static void mvebu_pcie_handle_iobase_change(struct mvebu_pcie_port *port)
371{
372 struct mvebu_pcie_window desired = {};
David Brazdil0f672f62019-12-10 10:32:29 +0000373 struct pci_bridge_emul_conf *conf = &port->bridge.conf;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000374
375 /* Are the new iobase/iolimit values invalid? */
David Brazdil0f672f62019-12-10 10:32:29 +0000376 if (conf->iolimit < conf->iobase ||
377 conf->iolimitupper < conf->iobaseupper ||
378 !(conf->command & PCI_COMMAND_IO)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000379 mvebu_pcie_set_window(port, port->io_target, port->io_attr,
380 &desired, &port->iowin);
381 return;
382 }
383
384 if (!mvebu_has_ioport(port)) {
385 dev_WARN(&port->pcie->pdev->dev,
386 "Attempt to set IO when IO is disabled\n");
387 return;
388 }
389
390 /*
391 * We read the PCI-to-PCI bridge emulated registers, and
392 * calculate the base address and size of the address decoding
393 * window to setup, according to the PCI-to-PCI bridge
394 * specifications. iobase is the bus address, port->iowin_base
395 * is the CPU address.
396 */
David Brazdil0f672f62019-12-10 10:32:29 +0000397 desired.remap = ((conf->iobase & 0xF0) << 8) |
398 (conf->iobaseupper << 16);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000399 desired.base = port->pcie->io.start + desired.remap;
David Brazdil0f672f62019-12-10 10:32:29 +0000400 desired.size = ((0xFFF | ((conf->iolimit & 0xF0) << 8) |
401 (conf->iolimitupper << 16)) -
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000402 desired.remap) +
403 1;
404
405 mvebu_pcie_set_window(port, port->io_target, port->io_attr, &desired,
406 &port->iowin);
407}
408
409static void mvebu_pcie_handle_membase_change(struct mvebu_pcie_port *port)
410{
411 struct mvebu_pcie_window desired = {.remap = MVEBU_MBUS_NO_REMAP};
David Brazdil0f672f62019-12-10 10:32:29 +0000412 struct pci_bridge_emul_conf *conf = &port->bridge.conf;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000413
414 /* Are the new membase/memlimit values invalid? */
David Brazdil0f672f62019-12-10 10:32:29 +0000415 if (conf->memlimit < conf->membase ||
416 !(conf->command & PCI_COMMAND_MEMORY)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000417 mvebu_pcie_set_window(port, port->mem_target, port->mem_attr,
418 &desired, &port->memwin);
419 return;
420 }
421
422 /*
423 * We read the PCI-to-PCI bridge emulated registers, and
424 * calculate the base address and size of the address decoding
425 * window to setup, according to the PCI-to-PCI bridge
426 * specifications.
427 */
David Brazdil0f672f62019-12-10 10:32:29 +0000428 desired.base = ((conf->membase & 0xFFF0) << 16);
429 desired.size = (((conf->memlimit & 0xFFF0) << 16) | 0xFFFFF) -
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000430 desired.base + 1;
431
432 mvebu_pcie_set_window(port, port->mem_target, port->mem_attr, &desired,
433 &port->memwin);
434}
435
David Brazdil0f672f62019-12-10 10:32:29 +0000436static pci_bridge_emul_read_status_t
437mvebu_pci_bridge_emul_pcie_conf_read(struct pci_bridge_emul *bridge,
438 int reg, u32 *value)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000439{
David Brazdil0f672f62019-12-10 10:32:29 +0000440 struct mvebu_pcie_port *port = bridge->data;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000441
David Brazdil0f672f62019-12-10 10:32:29 +0000442 switch (reg) {
443 case PCI_EXP_DEVCAP:
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000444 *value = mvebu_readl(port, PCIE_CAP_PCIEXP + PCI_EXP_DEVCAP);
445 break;
446
David Brazdil0f672f62019-12-10 10:32:29 +0000447 case PCI_EXP_DEVCTL:
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000448 *value = mvebu_readl(port, PCIE_CAP_PCIEXP + PCI_EXP_DEVCTL) &
449 ~(PCI_EXP_DEVCTL_URRE | PCI_EXP_DEVCTL_FERE |
450 PCI_EXP_DEVCTL_NFERE | PCI_EXP_DEVCTL_CERE);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000451 break;
452
David Brazdil0f672f62019-12-10 10:32:29 +0000453 case PCI_EXP_LNKCAP:
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000454 /*
455 * PCIe requires the clock power management capability to be
456 * hard-wired to zero for downstream ports
457 */
458 *value = mvebu_readl(port, PCIE_CAP_PCIEXP + PCI_EXP_LNKCAP) &
459 ~PCI_EXP_LNKCAP_CLKPM;
460 break;
461
David Brazdil0f672f62019-12-10 10:32:29 +0000462 case PCI_EXP_LNKCTL:
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000463 *value = mvebu_readl(port, PCIE_CAP_PCIEXP + PCI_EXP_LNKCTL);
464 break;
465
David Brazdil0f672f62019-12-10 10:32:29 +0000466 case PCI_EXP_SLTCTL:
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000467 *value = PCI_EXP_SLTSTA_PDS << 16;
468 break;
469
David Brazdil0f672f62019-12-10 10:32:29 +0000470 case PCI_EXP_RTSTA:
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000471 *value = mvebu_readl(port, PCIE_RC_RTSTA);
472 break;
473
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000474 default:
David Brazdil0f672f62019-12-10 10:32:29 +0000475 return PCI_BRIDGE_EMUL_NOT_HANDLED;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000476 }
477
David Brazdil0f672f62019-12-10 10:32:29 +0000478 return PCI_BRIDGE_EMUL_HANDLED;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000479}
480
David Brazdil0f672f62019-12-10 10:32:29 +0000481static void
482mvebu_pci_bridge_emul_base_conf_write(struct pci_bridge_emul *bridge,
483 int reg, u32 old, u32 new, u32 mask)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000484{
David Brazdil0f672f62019-12-10 10:32:29 +0000485 struct mvebu_pcie_port *port = bridge->data;
486 struct pci_bridge_emul_conf *conf = &bridge->conf;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000487
David Brazdil0f672f62019-12-10 10:32:29 +0000488 switch (reg) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000489 case PCI_COMMAND:
490 {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000491 if (!mvebu_has_ioport(port))
David Brazdil0f672f62019-12-10 10:32:29 +0000492 conf->command &= ~PCI_COMMAND_IO;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000493
David Brazdil0f672f62019-12-10 10:32:29 +0000494 if ((old ^ new) & PCI_COMMAND_IO)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000495 mvebu_pcie_handle_iobase_change(port);
David Brazdil0f672f62019-12-10 10:32:29 +0000496 if ((old ^ new) & PCI_COMMAND_MEMORY)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000497 mvebu_pcie_handle_membase_change(port);
David Brazdil0f672f62019-12-10 10:32:29 +0000498
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000499 break;
500 }
501
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000502 case PCI_IO_BASE:
503 /*
David Brazdil0f672f62019-12-10 10:32:29 +0000504 * We keep bit 1 set, it is a read-only bit that
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000505 * indicates we support 32 bits addressing for the
506 * I/O
507 */
David Brazdil0f672f62019-12-10 10:32:29 +0000508 conf->iobase |= PCI_IO_RANGE_TYPE_32;
509 conf->iolimit |= PCI_IO_RANGE_TYPE_32;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000510 mvebu_pcie_handle_iobase_change(port);
511 break;
512
513 case PCI_MEMORY_BASE:
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000514 mvebu_pcie_handle_membase_change(port);
515 break;
516
517 case PCI_IO_BASE_UPPER16:
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000518 mvebu_pcie_handle_iobase_change(port);
519 break;
520
521 case PCI_PRIMARY_BUS:
David Brazdil0f672f62019-12-10 10:32:29 +0000522 mvebu_pcie_set_local_bus_nr(port, conf->secondary_bus);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000523 break;
524
David Brazdil0f672f62019-12-10 10:32:29 +0000525 default:
526 break;
527 }
528}
529
530static void
531mvebu_pci_bridge_emul_pcie_conf_write(struct pci_bridge_emul *bridge,
532 int reg, u32 old, u32 new, u32 mask)
533{
534 struct mvebu_pcie_port *port = bridge->data;
535
536 switch (reg) {
537 case PCI_EXP_DEVCTL:
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000538 /*
539 * Armada370 data says these bits must always
540 * be zero when in root complex mode.
541 */
David Brazdil0f672f62019-12-10 10:32:29 +0000542 new &= ~(PCI_EXP_DEVCTL_URRE | PCI_EXP_DEVCTL_FERE |
543 PCI_EXP_DEVCTL_NFERE | PCI_EXP_DEVCTL_CERE);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000544
David Brazdil0f672f62019-12-10 10:32:29 +0000545 mvebu_writel(port, new, PCIE_CAP_PCIEXP + PCI_EXP_DEVCTL);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000546 break;
547
David Brazdil0f672f62019-12-10 10:32:29 +0000548 case PCI_EXP_LNKCTL:
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000549 /*
550 * If we don't support CLKREQ, we must ensure that the
551 * CLKREQ enable bit always reads zero. Since we haven't
552 * had this capability, and it's dependent on board wiring,
553 * disable it for the time being.
554 */
David Brazdil0f672f62019-12-10 10:32:29 +0000555 new &= ~PCI_EXP_LNKCTL_CLKREQ_EN;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000556
David Brazdil0f672f62019-12-10 10:32:29 +0000557 mvebu_writel(port, new, PCIE_CAP_PCIEXP + PCI_EXP_LNKCTL);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000558 break;
559
David Brazdil0f672f62019-12-10 10:32:29 +0000560 case PCI_EXP_RTSTA:
561 mvebu_writel(port, new, PCIE_RC_RTSTA);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000562 break;
563 }
David Brazdil0f672f62019-12-10 10:32:29 +0000564}
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000565
David Brazdil0f672f62019-12-10 10:32:29 +0000566struct pci_bridge_emul_ops mvebu_pci_bridge_emul_ops = {
567 .write_base = mvebu_pci_bridge_emul_base_conf_write,
568 .read_pcie = mvebu_pci_bridge_emul_pcie_conf_read,
569 .write_pcie = mvebu_pci_bridge_emul_pcie_conf_write,
570};
571
572/*
573 * Initialize the configuration space of the PCI-to-PCI bridge
574 * associated with the given PCIe interface.
575 */
576static void mvebu_pci_bridge_emul_init(struct mvebu_pcie_port *port)
577{
578 struct pci_bridge_emul *bridge = &port->bridge;
579
580 bridge->conf.vendor = PCI_VENDOR_ID_MARVELL;
581 bridge->conf.device = mvebu_readl(port, PCIE_DEV_ID_OFF) >> 16;
582 bridge->conf.class_revision =
583 mvebu_readl(port, PCIE_DEV_REV_OFF) & 0xff;
584
585 if (mvebu_has_ioport(port)) {
586 /* We support 32 bits I/O addressing */
587 bridge->conf.iobase = PCI_IO_RANGE_TYPE_32;
588 bridge->conf.iolimit = PCI_IO_RANGE_TYPE_32;
589 }
590
591 bridge->has_pcie = true;
592 bridge->data = port;
593 bridge->ops = &mvebu_pci_bridge_emul_ops;
594
595 pci_bridge_emul_init(bridge, PCI_BRIDGE_EMUL_NO_PREFETCHABLE_BAR);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000596}
597
598static inline struct mvebu_pcie *sys_to_pcie(struct pci_sys_data *sys)
599{
600 return sys->private_data;
601}
602
603static struct mvebu_pcie_port *mvebu_pcie_find_port(struct mvebu_pcie *pcie,
604 struct pci_bus *bus,
605 int devfn)
606{
607 int i;
608
609 for (i = 0; i < pcie->nports; i++) {
610 struct mvebu_pcie_port *port = &pcie->ports[i];
611
612 if (bus->number == 0 && port->devfn == devfn)
613 return port;
614 if (bus->number != 0 &&
David Brazdil0f672f62019-12-10 10:32:29 +0000615 bus->number >= port->bridge.conf.secondary_bus &&
616 bus->number <= port->bridge.conf.subordinate_bus)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000617 return port;
618 }
619
620 return NULL;
621}
622
623/* PCI configuration space write function */
624static int mvebu_pcie_wr_conf(struct pci_bus *bus, u32 devfn,
625 int where, int size, u32 val)
626{
627 struct mvebu_pcie *pcie = bus->sysdata;
628 struct mvebu_pcie_port *port;
629 int ret;
630
631 port = mvebu_pcie_find_port(pcie, bus, devfn);
632 if (!port)
633 return PCIBIOS_DEVICE_NOT_FOUND;
634
635 /* Access the emulated PCI-to-PCI bridge */
636 if (bus->number == 0)
David Brazdil0f672f62019-12-10 10:32:29 +0000637 return pci_bridge_emul_conf_write(&port->bridge, where,
638 size, val);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000639
640 if (!mvebu_pcie_link_up(port))
641 return PCIBIOS_DEVICE_NOT_FOUND;
642
643 /* Access the real PCIe interface */
644 ret = mvebu_pcie_hw_wr_conf(port, bus, devfn,
645 where, size, val);
646
647 return ret;
648}
649
650/* PCI configuration space read function */
651static int mvebu_pcie_rd_conf(struct pci_bus *bus, u32 devfn, int where,
652 int size, u32 *val)
653{
654 struct mvebu_pcie *pcie = bus->sysdata;
655 struct mvebu_pcie_port *port;
656 int ret;
657
658 port = mvebu_pcie_find_port(pcie, bus, devfn);
659 if (!port) {
660 *val = 0xffffffff;
661 return PCIBIOS_DEVICE_NOT_FOUND;
662 }
663
664 /* Access the emulated PCI-to-PCI bridge */
665 if (bus->number == 0)
David Brazdil0f672f62019-12-10 10:32:29 +0000666 return pci_bridge_emul_conf_read(&port->bridge, where,
667 size, val);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000668
669 if (!mvebu_pcie_link_up(port)) {
670 *val = 0xffffffff;
671 return PCIBIOS_DEVICE_NOT_FOUND;
672 }
673
674 /* Access the real PCIe interface */
675 ret = mvebu_pcie_hw_rd_conf(port, bus, devfn,
676 where, size, val);
677
678 return ret;
679}
680
681static struct pci_ops mvebu_pcie_ops = {
682 .read = mvebu_pcie_rd_conf,
683 .write = mvebu_pcie_wr_conf,
684};
685
686static resource_size_t mvebu_pcie_align_resource(struct pci_dev *dev,
687 const struct resource *res,
688 resource_size_t start,
689 resource_size_t size,
690 resource_size_t align)
691{
692 if (dev->bus->number != 0)
693 return start;
694
695 /*
696 * On the PCI-to-PCI bridge side, the I/O windows must have at
697 * least a 64 KB size and the memory windows must have at
698 * least a 1 MB size. Moreover, MBus windows need to have a
699 * base address aligned on their size, and their size must be
700 * a power of two. This means that if the BAR doesn't have a
701 * power of two size, several MBus windows will actually be
702 * created. We need to ensure that the biggest MBus window
703 * (which will be the first one) is aligned on its size, which
704 * explains the rounddown_pow_of_two() being done here.
705 */
706 if (res->flags & IORESOURCE_IO)
707 return round_up(start, max_t(resource_size_t, SZ_64K,
708 rounddown_pow_of_two(size)));
709 else if (res->flags & IORESOURCE_MEM)
710 return round_up(start, max_t(resource_size_t, SZ_1M,
711 rounddown_pow_of_two(size)));
712 else
713 return start;
714}
715
716static void __iomem *mvebu_pcie_map_registers(struct platform_device *pdev,
717 struct device_node *np,
718 struct mvebu_pcie_port *port)
719{
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000720 int ret = 0;
721
Olivier Deprez0e641232021-09-23 10:07:05 +0200722 ret = of_address_to_resource(np, 0, &port->regs);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000723 if (ret)
724 return ERR_PTR(ret);
725
Olivier Deprez0e641232021-09-23 10:07:05 +0200726 return devm_ioremap_resource(&pdev->dev, &port->regs);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000727}
728
729#define DT_FLAGS_TO_TYPE(flags) (((flags) >> 24) & 0x03)
730#define DT_TYPE_IO 0x1
731#define DT_TYPE_MEM32 0x2
732#define DT_CPUADDR_TO_TARGET(cpuaddr) (((cpuaddr) >> 56) & 0xFF)
733#define DT_CPUADDR_TO_ATTR(cpuaddr) (((cpuaddr) >> 48) & 0xFF)
734
735static int mvebu_get_tgt_attr(struct device_node *np, int devfn,
736 unsigned long type,
737 unsigned int *tgt,
738 unsigned int *attr)
739{
740 const int na = 3, ns = 2;
741 const __be32 *range;
742 int rlen, nranges, rangesz, pna, i;
743
744 *tgt = -1;
745 *attr = -1;
746
747 range = of_get_property(np, "ranges", &rlen);
748 if (!range)
749 return -EINVAL;
750
751 pna = of_n_addr_cells(np);
752 rangesz = pna + na + ns;
753 nranges = rlen / sizeof(__be32) / rangesz;
754
755 for (i = 0; i < nranges; i++, range += rangesz) {
756 u32 flags = of_read_number(range, 1);
757 u32 slot = of_read_number(range + 1, 1);
758 u64 cpuaddr = of_read_number(range + na, pna);
759 unsigned long rtype;
760
761 if (DT_FLAGS_TO_TYPE(flags) == DT_TYPE_IO)
762 rtype = IORESOURCE_IO;
763 else if (DT_FLAGS_TO_TYPE(flags) == DT_TYPE_MEM32)
764 rtype = IORESOURCE_MEM;
765 else
766 continue;
767
768 if (slot == PCI_SLOT(devfn) && type == rtype) {
769 *tgt = DT_CPUADDR_TO_TARGET(cpuaddr);
770 *attr = DT_CPUADDR_TO_ATTR(cpuaddr);
771 return 0;
772 }
773 }
774
775 return -ENOENT;
776}
777
778#ifdef CONFIG_PM_SLEEP
779static int mvebu_pcie_suspend(struct device *dev)
780{
781 struct mvebu_pcie *pcie;
782 int i;
783
784 pcie = dev_get_drvdata(dev);
785 for (i = 0; i < pcie->nports; i++) {
786 struct mvebu_pcie_port *port = pcie->ports + i;
787 port->saved_pcie_stat = mvebu_readl(port, PCIE_STAT_OFF);
788 }
789
790 return 0;
791}
792
793static int mvebu_pcie_resume(struct device *dev)
794{
795 struct mvebu_pcie *pcie;
796 int i;
797
798 pcie = dev_get_drvdata(dev);
799 for (i = 0; i < pcie->nports; i++) {
800 struct mvebu_pcie_port *port = pcie->ports + i;
801 mvebu_writel(port, port->saved_pcie_stat, PCIE_STAT_OFF);
802 mvebu_pcie_setup_hw(port);
803 }
804
805 return 0;
806}
807#endif
808
809static void mvebu_pcie_port_clk_put(void *data)
810{
811 struct mvebu_pcie_port *port = data;
812
813 clk_put(port->clk);
814}
815
816static int mvebu_pcie_parse_port(struct mvebu_pcie *pcie,
817 struct mvebu_pcie_port *port, struct device_node *child)
818{
819 struct device *dev = &pcie->pdev->dev;
820 enum of_gpio_flags flags;
821 int reset_gpio, ret;
822
823 port->pcie = pcie;
824
825 if (of_property_read_u32(child, "marvell,pcie-port", &port->port)) {
826 dev_warn(dev, "ignoring %pOF, missing pcie-port property\n",
827 child);
828 goto skip;
829 }
830
831 if (of_property_read_u32(child, "marvell,pcie-lane", &port->lane))
832 port->lane = 0;
833
834 port->name = devm_kasprintf(dev, GFP_KERNEL, "pcie%d.%d", port->port,
835 port->lane);
836 if (!port->name) {
837 ret = -ENOMEM;
838 goto err;
839 }
840
841 port->devfn = of_pci_get_devfn(child);
842 if (port->devfn < 0)
843 goto skip;
844
845 ret = mvebu_get_tgt_attr(dev->of_node, port->devfn, IORESOURCE_MEM,
846 &port->mem_target, &port->mem_attr);
847 if (ret < 0) {
848 dev_err(dev, "%s: cannot get tgt/attr for mem window\n",
849 port->name);
850 goto skip;
851 }
852
853 if (resource_size(&pcie->io) != 0) {
854 mvebu_get_tgt_attr(dev->of_node, port->devfn, IORESOURCE_IO,
855 &port->io_target, &port->io_attr);
856 } else {
857 port->io_target = -1;
858 port->io_attr = -1;
859 }
860
861 reset_gpio = of_get_named_gpio_flags(child, "reset-gpios", 0, &flags);
862 if (reset_gpio == -EPROBE_DEFER) {
863 ret = reset_gpio;
864 goto err;
865 }
866
867 if (gpio_is_valid(reset_gpio)) {
868 unsigned long gpio_flags;
869
870 port->reset_name = devm_kasprintf(dev, GFP_KERNEL, "%s-reset",
871 port->name);
872 if (!port->reset_name) {
873 ret = -ENOMEM;
874 goto err;
875 }
876
877 if (flags & OF_GPIO_ACTIVE_LOW) {
878 dev_info(dev, "%pOF: reset gpio is active low\n",
879 child);
880 gpio_flags = GPIOF_ACTIVE_LOW |
881 GPIOF_OUT_INIT_LOW;
882 } else {
883 gpio_flags = GPIOF_OUT_INIT_HIGH;
884 }
885
886 ret = devm_gpio_request_one(dev, reset_gpio, gpio_flags,
887 port->reset_name);
888 if (ret) {
889 if (ret == -EPROBE_DEFER)
890 goto err;
891 goto skip;
892 }
893
894 port->reset_gpio = gpio_to_desc(reset_gpio);
895 }
896
897 port->clk = of_clk_get_by_name(child, NULL);
898 if (IS_ERR(port->clk)) {
899 dev_err(dev, "%s: cannot get clock\n", port->name);
900 goto skip;
901 }
902
903 ret = devm_add_action(dev, mvebu_pcie_port_clk_put, port);
904 if (ret < 0) {
905 clk_put(port->clk);
906 goto err;
907 }
908
909 return 1;
910
911skip:
912 ret = 0;
913
914 /* In the case of skipping, we need to free these */
915 devm_kfree(dev, port->reset_name);
916 port->reset_name = NULL;
917 devm_kfree(dev, port->name);
918 port->name = NULL;
919
920err:
921 return ret;
922}
923
924/*
925 * Power up a PCIe port. PCIe requires the refclk to be stable for 100µs
926 * prior to releasing PERST. See table 2-4 in section 2.6.2 AC Specifications
927 * of the PCI Express Card Electromechanical Specification, 1.1.
928 */
929static int mvebu_pcie_powerup(struct mvebu_pcie_port *port)
930{
931 int ret;
932
933 ret = clk_prepare_enable(port->clk);
934 if (ret < 0)
935 return ret;
936
937 if (port->reset_gpio) {
938 u32 reset_udelay = PCI_PM_D3COLD_WAIT * 1000;
939
940 of_property_read_u32(port->dn, "reset-delay-us",
941 &reset_udelay);
942
943 udelay(100);
944
945 gpiod_set_value_cansleep(port->reset_gpio, 0);
946 msleep(reset_udelay / 1000);
947 }
948
949 return 0;
950}
951
952/*
953 * Power down a PCIe port. Strictly, PCIe requires us to place the card
954 * in D3hot state before asserting PERST#.
955 */
956static void mvebu_pcie_powerdown(struct mvebu_pcie_port *port)
957{
958 gpiod_set_value_cansleep(port->reset_gpio, 1);
959
960 clk_disable_unprepare(port->clk);
961}
962
963/*
964 * We can't use devm_of_pci_get_host_bridge_resources() because we
965 * need to parse our special DT properties encoding the MEM and IO
966 * apertures.
967 */
968static int mvebu_pcie_parse_request_resources(struct mvebu_pcie *pcie)
969{
970 struct device *dev = &pcie->pdev->dev;
971 struct device_node *np = dev->of_node;
972 int ret;
973
974 INIT_LIST_HEAD(&pcie->resources);
975
976 /* Get the bus range */
977 ret = of_pci_parse_bus_range(np, &pcie->busn);
978 if (ret) {
979 dev_err(dev, "failed to parse bus-range property: %d\n", ret);
980 return ret;
981 }
982 pci_add_resource(&pcie->resources, &pcie->busn);
983
984 /* Get the PCIe memory aperture */
985 mvebu_mbus_get_pcie_mem_aperture(&pcie->mem);
986 if (resource_size(&pcie->mem) == 0) {
987 dev_err(dev, "invalid memory aperture size\n");
988 return -EINVAL;
989 }
990
991 pcie->mem.name = "PCI MEM";
992 pci_add_resource(&pcie->resources, &pcie->mem);
993
994 /* Get the PCIe IO aperture */
995 mvebu_mbus_get_pcie_io_aperture(&pcie->io);
996
997 if (resource_size(&pcie->io) != 0) {
998 pcie->realio.flags = pcie->io.flags;
999 pcie->realio.start = PCIBIOS_MIN_IO;
1000 pcie->realio.end = min_t(resource_size_t,
1001 IO_SPACE_LIMIT - SZ_64K,
1002 resource_size(&pcie->io) - 1);
1003 pcie->realio.name = "PCI I/O";
1004
1005 pci_add_resource(&pcie->resources, &pcie->realio);
1006 }
1007
1008 return devm_request_pci_bus_resources(dev, &pcie->resources);
1009}
1010
1011/*
1012 * This is a copy of pci_host_probe(), except that it does the I/O
1013 * remap as the last step, once we are sure we won't fail.
1014 *
1015 * It should be removed once the I/O remap error handling issue has
1016 * been sorted out.
1017 */
1018static int mvebu_pci_host_probe(struct pci_host_bridge *bridge)
1019{
1020 struct mvebu_pcie *pcie;
1021 struct pci_bus *bus, *child;
1022 int ret;
1023
1024 ret = pci_scan_root_bus_bridge(bridge);
1025 if (ret < 0) {
1026 dev_err(bridge->dev.parent, "Scanning root bridge failed");
1027 return ret;
1028 }
1029
1030 pcie = pci_host_bridge_priv(bridge);
1031 if (resource_size(&pcie->io) != 0) {
1032 unsigned int i;
1033
1034 for (i = 0; i < resource_size(&pcie->realio); i += SZ_64K)
1035 pci_ioremap_io(i, pcie->io.start + i);
1036 }
1037
1038 bus = bridge->bus;
1039
1040 /*
1041 * We insert PCI resources into the iomem_resource and
1042 * ioport_resource trees in either pci_bus_claim_resources()
1043 * or pci_bus_assign_resources().
1044 */
1045 if (pci_has_flag(PCI_PROBE_ONLY)) {
1046 pci_bus_claim_resources(bus);
1047 } else {
1048 pci_bus_size_bridges(bus);
1049 pci_bus_assign_resources(bus);
1050
1051 list_for_each_entry(child, &bus->children, node)
1052 pcie_bus_configure_settings(child);
1053 }
1054
1055 pci_bus_add_devices(bus);
1056 return 0;
1057}
1058
1059static int mvebu_pcie_probe(struct platform_device *pdev)
1060{
1061 struct device *dev = &pdev->dev;
1062 struct mvebu_pcie *pcie;
1063 struct pci_host_bridge *bridge;
1064 struct device_node *np = dev->of_node;
1065 struct device_node *child;
1066 int num, i, ret;
1067
1068 bridge = devm_pci_alloc_host_bridge(dev, sizeof(struct mvebu_pcie));
1069 if (!bridge)
1070 return -ENOMEM;
1071
1072 pcie = pci_host_bridge_priv(bridge);
1073 pcie->pdev = pdev;
1074 platform_set_drvdata(pdev, pcie);
1075
1076 ret = mvebu_pcie_parse_request_resources(pcie);
1077 if (ret)
1078 return ret;
1079
1080 num = of_get_available_child_count(np);
1081
1082 pcie->ports = devm_kcalloc(dev, num, sizeof(*pcie->ports), GFP_KERNEL);
1083 if (!pcie->ports)
1084 return -ENOMEM;
1085
1086 i = 0;
1087 for_each_available_child_of_node(np, child) {
1088 struct mvebu_pcie_port *port = &pcie->ports[i];
1089
1090 ret = mvebu_pcie_parse_port(pcie, port, child);
1091 if (ret < 0) {
1092 of_node_put(child);
1093 return ret;
1094 } else if (ret == 0) {
1095 continue;
1096 }
1097
1098 port->dn = child;
1099 i++;
1100 }
1101 pcie->nports = i;
1102
1103 for (i = 0; i < pcie->nports; i++) {
1104 struct mvebu_pcie_port *port = &pcie->ports[i];
1105
1106 child = port->dn;
1107 if (!child)
1108 continue;
1109
1110 ret = mvebu_pcie_powerup(port);
1111 if (ret < 0)
1112 continue;
1113
1114 port->base = mvebu_pcie_map_registers(pdev, child, port);
1115 if (IS_ERR(port->base)) {
1116 dev_err(dev, "%s: cannot map registers\n", port->name);
1117 port->base = NULL;
1118 mvebu_pcie_powerdown(port);
1119 continue;
1120 }
1121
1122 mvebu_pcie_setup_hw(port);
1123 mvebu_pcie_set_local_dev_nr(port, 1);
David Brazdil0f672f62019-12-10 10:32:29 +00001124 mvebu_pci_bridge_emul_init(port);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001125 }
1126
1127 pcie->nports = i;
1128
1129 list_splice_init(&pcie->resources, &bridge->windows);
1130 bridge->dev.parent = dev;
1131 bridge->sysdata = pcie;
1132 bridge->busnr = 0;
1133 bridge->ops = &mvebu_pcie_ops;
1134 bridge->map_irq = of_irq_parse_and_map_pci;
1135 bridge->swizzle_irq = pci_common_swizzle;
1136 bridge->align_resource = mvebu_pcie_align_resource;
1137 bridge->msi = pcie->msi;
1138
1139 return mvebu_pci_host_probe(bridge);
1140}
1141
1142static const struct of_device_id mvebu_pcie_of_match_table[] = {
1143 { .compatible = "marvell,armada-xp-pcie", },
1144 { .compatible = "marvell,armada-370-pcie", },
1145 { .compatible = "marvell,dove-pcie", },
1146 { .compatible = "marvell,kirkwood-pcie", },
1147 {},
1148};
1149
1150static const struct dev_pm_ops mvebu_pcie_pm_ops = {
1151 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(mvebu_pcie_suspend, mvebu_pcie_resume)
1152};
1153
1154static struct platform_driver mvebu_pcie_driver = {
1155 .driver = {
1156 .name = "mvebu-pcie",
1157 .of_match_table = mvebu_pcie_of_match_table,
1158 /* driver unloading/unbinding currently not supported */
1159 .suppress_bind_attrs = true,
1160 .pm = &mvebu_pcie_pm_ops,
1161 },
1162 .probe = mvebu_pcie_probe,
1163};
1164builtin_platform_driver(mvebu_pcie_driver);