blob: 09af97083ac090f60088f5f5bdab368a16c42844 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001// 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"
25#include "../pci-bridge-emul.h"
26
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
67struct 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;
102 struct pci_bridge_emul bridge;
103 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;
108 struct resource regs;
109};
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:
153 * BAR[0] -> internal registers (needed for MSI)
154 * BAR[1] -> covers all DRAM banks
155 * BAR[2] -> Disabled
156 * 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));
209
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));
215}
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 = {};
373 struct pci_bridge_emul_conf *conf = &port->bridge.conf;
374
375 /* Are the new iobase/iolimit values invalid? */
376 if (conf->iolimit < conf->iobase ||
377 conf->iolimitupper < conf->iobaseupper ||
378 !(conf->command & PCI_COMMAND_IO)) {
379 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 */
397 desired.remap = ((conf->iobase & 0xF0) << 8) |
398 (conf->iobaseupper << 16);
399 desired.base = port->pcie->io.start + desired.remap;
400 desired.size = ((0xFFF | ((conf->iolimit & 0xF0) << 8) |
401 (conf->iolimitupper << 16)) -
402 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};
412 struct pci_bridge_emul_conf *conf = &port->bridge.conf;
413
414 /* Are the new membase/memlimit values invalid? */
415 if (conf->memlimit < conf->membase ||
416 !(conf->command & PCI_COMMAND_MEMORY)) {
417 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 */
428 desired.base = ((conf->membase & 0xFFF0) << 16);
429 desired.size = (((conf->memlimit & 0xFFF0) << 16) | 0xFFFFF) -
430 desired.base + 1;
431
432 mvebu_pcie_set_window(port, port->mem_target, port->mem_attr, &desired,
433 &port->memwin);
434}
435
436static pci_bridge_emul_read_status_t
437mvebu_pci_bridge_emul_pcie_conf_read(struct pci_bridge_emul *bridge,
438 int reg, u32 *value)
439{
440 struct mvebu_pcie_port *port = bridge->data;
441
442 switch (reg) {
443 case PCI_EXP_DEVCAP:
444 *value = mvebu_readl(port, PCIE_CAP_PCIEXP + PCI_EXP_DEVCAP);
445 break;
446
447 case PCI_EXP_DEVCTL:
448 *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);
451 break;
452
453 case PCI_EXP_LNKCAP:
454 /*
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
462 case PCI_EXP_LNKCTL:
463 *value = mvebu_readl(port, PCIE_CAP_PCIEXP + PCI_EXP_LNKCTL);
464 break;
465
466 case PCI_EXP_SLTCTL:
467 *value = PCI_EXP_SLTSTA_PDS << 16;
468 break;
469
470 case PCI_EXP_RTSTA:
471 *value = mvebu_readl(port, PCIE_RC_RTSTA);
472 break;
473
474 default:
475 return PCI_BRIDGE_EMUL_NOT_HANDLED;
476 }
477
478 return PCI_BRIDGE_EMUL_HANDLED;
479}
480
481static void
482mvebu_pci_bridge_emul_base_conf_write(struct pci_bridge_emul *bridge,
483 int reg, u32 old, u32 new, u32 mask)
484{
485 struct mvebu_pcie_port *port = bridge->data;
486 struct pci_bridge_emul_conf *conf = &bridge->conf;
487
488 switch (reg) {
489 case PCI_COMMAND:
490 {
491 if (!mvebu_has_ioport(port))
492 conf->command &= ~PCI_COMMAND_IO;
493
494 if ((old ^ new) & PCI_COMMAND_IO)
495 mvebu_pcie_handle_iobase_change(port);
496 if ((old ^ new) & PCI_COMMAND_MEMORY)
497 mvebu_pcie_handle_membase_change(port);
498
499 break;
500 }
501
502 case PCI_IO_BASE:
503 /*
504 * We keep bit 1 set, it is a read-only bit that
505 * indicates we support 32 bits addressing for the
506 * I/O
507 */
508 conf->iobase |= PCI_IO_RANGE_TYPE_32;
509 conf->iolimit |= PCI_IO_RANGE_TYPE_32;
510 mvebu_pcie_handle_iobase_change(port);
511 break;
512
513 case PCI_MEMORY_BASE:
514 mvebu_pcie_handle_membase_change(port);
515 break;
516
517 case PCI_IO_BASE_UPPER16:
518 mvebu_pcie_handle_iobase_change(port);
519 break;
520
521 case PCI_PRIMARY_BUS:
522 mvebu_pcie_set_local_bus_nr(port, conf->secondary_bus);
523 break;
524
525 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:
538 /*
539 * Armada370 data says these bits must always
540 * be zero when in root complex mode.
541 */
542 new &= ~(PCI_EXP_DEVCTL_URRE | PCI_EXP_DEVCTL_FERE |
543 PCI_EXP_DEVCTL_NFERE | PCI_EXP_DEVCTL_CERE);
544
545 mvebu_writel(port, new, PCIE_CAP_PCIEXP + PCI_EXP_DEVCTL);
546 break;
547
548 case PCI_EXP_LNKCTL:
549 /*
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 */
555 new &= ~PCI_EXP_LNKCTL_CLKREQ_EN;
556
557 mvebu_writel(port, new, PCIE_CAP_PCIEXP + PCI_EXP_LNKCTL);
558 break;
559
560 case PCI_EXP_RTSTA:
561 mvebu_writel(port, new, PCIE_RC_RTSTA);
562 break;
563 }
564}
565
566struct 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 u32 pcie_cap = mvebu_readl(port, PCIE_CAP_PCIEXP);
580 u8 pcie_cap_ver = ((pcie_cap >> 16) & PCI_EXP_FLAGS_VERS);
581
582 bridge->conf.vendor = PCI_VENDOR_ID_MARVELL;
583 bridge->conf.device = mvebu_readl(port, PCIE_DEV_ID_OFF) >> 16;
584 bridge->conf.class_revision =
585 mvebu_readl(port, PCIE_DEV_REV_OFF) & 0xff;
586
587 if (mvebu_has_ioport(port)) {
588 /* We support 32 bits I/O addressing */
589 bridge->conf.iobase = PCI_IO_RANGE_TYPE_32;
590 bridge->conf.iolimit = PCI_IO_RANGE_TYPE_32;
591 }
592
593 /*
594 * Older mvebu hardware provides PCIe Capability structure only in
595 * version 1. New hardware provides it in version 2.
596 */
597 bridge->pcie_conf.cap = cpu_to_le16(pcie_cap_ver);
598
599 bridge->has_pcie = true;
600 bridge->data = port;
601 bridge->ops = &mvebu_pci_bridge_emul_ops;
602
603 pci_bridge_emul_init(bridge, PCI_BRIDGE_EMUL_NO_PREFETCHABLE_BAR);
604}
605
606static inline struct mvebu_pcie *sys_to_pcie(struct pci_sys_data *sys)
607{
608 return sys->private_data;
609}
610
611static struct mvebu_pcie_port *mvebu_pcie_find_port(struct mvebu_pcie *pcie,
612 struct pci_bus *bus,
613 int devfn)
614{
615 int i;
616
617 for (i = 0; i < pcie->nports; i++) {
618 struct mvebu_pcie_port *port = &pcie->ports[i];
619
620 if (bus->number == 0 && port->devfn == devfn)
621 return port;
622 if (bus->number != 0 &&
623 bus->number >= port->bridge.conf.secondary_bus &&
624 bus->number <= port->bridge.conf.subordinate_bus)
625 return port;
626 }
627
628 return NULL;
629}
630
631/* PCI configuration space write function */
632static int mvebu_pcie_wr_conf(struct pci_bus *bus, u32 devfn,
633 int where, int size, u32 val)
634{
635 struct mvebu_pcie *pcie = bus->sysdata;
636 struct mvebu_pcie_port *port;
637 int ret;
638
639 port = mvebu_pcie_find_port(pcie, bus, devfn);
640 if (!port)
641 return PCIBIOS_DEVICE_NOT_FOUND;
642
643 /* Access the emulated PCI-to-PCI bridge */
644 if (bus->number == 0)
645 return pci_bridge_emul_conf_write(&port->bridge, where,
646 size, val);
647
648 if (!mvebu_pcie_link_up(port))
649 return PCIBIOS_DEVICE_NOT_FOUND;
650
651 /* Access the real PCIe interface */
652 ret = mvebu_pcie_hw_wr_conf(port, bus, devfn,
653 where, size, val);
654
655 return ret;
656}
657
658/* PCI configuration space read function */
659static int mvebu_pcie_rd_conf(struct pci_bus *bus, u32 devfn, int where,
660 int size, u32 *val)
661{
662 struct mvebu_pcie *pcie = bus->sysdata;
663 struct mvebu_pcie_port *port;
664 int ret;
665
666 port = mvebu_pcie_find_port(pcie, bus, devfn);
667 if (!port) {
668 *val = 0xffffffff;
669 return PCIBIOS_DEVICE_NOT_FOUND;
670 }
671
672 /* Access the emulated PCI-to-PCI bridge */
673 if (bus->number == 0)
674 return pci_bridge_emul_conf_read(&port->bridge, where,
675 size, val);
676
677 if (!mvebu_pcie_link_up(port)) {
678 *val = 0xffffffff;
679 return PCIBIOS_DEVICE_NOT_FOUND;
680 }
681
682 /* Access the real PCIe interface */
683 ret = mvebu_pcie_hw_rd_conf(port, bus, devfn,
684 where, size, val);
685
686 return ret;
687}
688
689static struct pci_ops mvebu_pcie_ops = {
690 .read = mvebu_pcie_rd_conf,
691 .write = mvebu_pcie_wr_conf,
692};
693
694static resource_size_t mvebu_pcie_align_resource(struct pci_dev *dev,
695 const struct resource *res,
696 resource_size_t start,
697 resource_size_t size,
698 resource_size_t align)
699{
700 if (dev->bus->number != 0)
701 return start;
702
703 /*
704 * On the PCI-to-PCI bridge side, the I/O windows must have at
705 * least a 64 KB size and the memory windows must have at
706 * least a 1 MB size. Moreover, MBus windows need to have a
707 * base address aligned on their size, and their size must be
708 * a power of two. This means that if the BAR doesn't have a
709 * power of two size, several MBus windows will actually be
710 * created. We need to ensure that the biggest MBus window
711 * (which will be the first one) is aligned on its size, which
712 * explains the rounddown_pow_of_two() being done here.
713 */
714 if (res->flags & IORESOURCE_IO)
715 return round_up(start, max_t(resource_size_t, SZ_64K,
716 rounddown_pow_of_two(size)));
717 else if (res->flags & IORESOURCE_MEM)
718 return round_up(start, max_t(resource_size_t, SZ_1M,
719 rounddown_pow_of_two(size)));
720 else
721 return start;
722}
723
724static void __iomem *mvebu_pcie_map_registers(struct platform_device *pdev,
725 struct device_node *np,
726 struct mvebu_pcie_port *port)
727{
728 int ret = 0;
729
730 ret = of_address_to_resource(np, 0, &port->regs);
731 if (ret)
732 return ERR_PTR(ret);
733
734 return devm_ioremap_resource(&pdev->dev, &port->regs);
735}
736
737#define DT_FLAGS_TO_TYPE(flags) (((flags) >> 24) & 0x03)
738#define DT_TYPE_IO 0x1
739#define DT_TYPE_MEM32 0x2
740#define DT_CPUADDR_TO_TARGET(cpuaddr) (((cpuaddr) >> 56) & 0xFF)
741#define DT_CPUADDR_TO_ATTR(cpuaddr) (((cpuaddr) >> 48) & 0xFF)
742
743static int mvebu_get_tgt_attr(struct device_node *np, int devfn,
744 unsigned long type,
745 unsigned int *tgt,
746 unsigned int *attr)
747{
748 const int na = 3, ns = 2;
749 const __be32 *range;
750 int rlen, nranges, rangesz, pna, i;
751
752 *tgt = -1;
753 *attr = -1;
754
755 range = of_get_property(np, "ranges", &rlen);
756 if (!range)
757 return -EINVAL;
758
759 pna = of_n_addr_cells(np);
760 rangesz = pna + na + ns;
761 nranges = rlen / sizeof(__be32) / rangesz;
762
763 for (i = 0; i < nranges; i++, range += rangesz) {
764 u32 flags = of_read_number(range, 1);
765 u32 slot = of_read_number(range + 1, 1);
766 u64 cpuaddr = of_read_number(range + na, pna);
767 unsigned long rtype;
768
769 if (DT_FLAGS_TO_TYPE(flags) == DT_TYPE_IO)
770 rtype = IORESOURCE_IO;
771 else if (DT_FLAGS_TO_TYPE(flags) == DT_TYPE_MEM32)
772 rtype = IORESOURCE_MEM;
773 else
774 continue;
775
776 if (slot == PCI_SLOT(devfn) && type == rtype) {
777 *tgt = DT_CPUADDR_TO_TARGET(cpuaddr);
778 *attr = DT_CPUADDR_TO_ATTR(cpuaddr);
779 return 0;
780 }
781 }
782
783 return -ENOENT;
784}
785
786#ifdef CONFIG_PM_SLEEP
787static int mvebu_pcie_suspend(struct device *dev)
788{
789 struct mvebu_pcie *pcie;
790 int i;
791
792 pcie = dev_get_drvdata(dev);
793 for (i = 0; i < pcie->nports; i++) {
794 struct mvebu_pcie_port *port = pcie->ports + i;
795 port->saved_pcie_stat = mvebu_readl(port, PCIE_STAT_OFF);
796 }
797
798 return 0;
799}
800
801static int mvebu_pcie_resume(struct device *dev)
802{
803 struct mvebu_pcie *pcie;
804 int i;
805
806 pcie = dev_get_drvdata(dev);
807 for (i = 0; i < pcie->nports; i++) {
808 struct mvebu_pcie_port *port = pcie->ports + i;
809 mvebu_writel(port, port->saved_pcie_stat, PCIE_STAT_OFF);
810 mvebu_pcie_setup_hw(port);
811 }
812
813 return 0;
814}
815#endif
816
817static void mvebu_pcie_port_clk_put(void *data)
818{
819 struct mvebu_pcie_port *port = data;
820
821 clk_put(port->clk);
822}
823
824static int mvebu_pcie_parse_port(struct mvebu_pcie *pcie,
825 struct mvebu_pcie_port *port, struct device_node *child)
826{
827 struct device *dev = &pcie->pdev->dev;
828 enum of_gpio_flags flags;
829 int reset_gpio, ret;
830
831 port->pcie = pcie;
832
833 if (of_property_read_u32(child, "marvell,pcie-port", &port->port)) {
834 dev_warn(dev, "ignoring %pOF, missing pcie-port property\n",
835 child);
836 goto skip;
837 }
838
839 if (of_property_read_u32(child, "marvell,pcie-lane", &port->lane))
840 port->lane = 0;
841
842 port->name = devm_kasprintf(dev, GFP_KERNEL, "pcie%d.%d", port->port,
843 port->lane);
844 if (!port->name) {
845 ret = -ENOMEM;
846 goto err;
847 }
848
849 port->devfn = of_pci_get_devfn(child);
850 if (port->devfn < 0)
851 goto skip;
852
853 ret = mvebu_get_tgt_attr(dev->of_node, port->devfn, IORESOURCE_MEM,
854 &port->mem_target, &port->mem_attr);
855 if (ret < 0) {
856 dev_err(dev, "%s: cannot get tgt/attr for mem window\n",
857 port->name);
858 goto skip;
859 }
860
861 if (resource_size(&pcie->io) != 0) {
862 mvebu_get_tgt_attr(dev->of_node, port->devfn, IORESOURCE_IO,
863 &port->io_target, &port->io_attr);
864 } else {
865 port->io_target = -1;
866 port->io_attr = -1;
867 }
868
869 reset_gpio = of_get_named_gpio_flags(child, "reset-gpios", 0, &flags);
870 if (reset_gpio == -EPROBE_DEFER) {
871 ret = reset_gpio;
872 goto err;
873 }
874
875 if (gpio_is_valid(reset_gpio)) {
876 unsigned long gpio_flags;
877
878 port->reset_name = devm_kasprintf(dev, GFP_KERNEL, "%s-reset",
879 port->name);
880 if (!port->reset_name) {
881 ret = -ENOMEM;
882 goto err;
883 }
884
885 if (flags & OF_GPIO_ACTIVE_LOW) {
886 dev_info(dev, "%pOF: reset gpio is active low\n",
887 child);
888 gpio_flags = GPIOF_ACTIVE_LOW |
889 GPIOF_OUT_INIT_LOW;
890 } else {
891 gpio_flags = GPIOF_OUT_INIT_HIGH;
892 }
893
894 ret = devm_gpio_request_one(dev, reset_gpio, gpio_flags,
895 port->reset_name);
896 if (ret) {
897 if (ret == -EPROBE_DEFER)
898 goto err;
899 goto skip;
900 }
901
902 port->reset_gpio = gpio_to_desc(reset_gpio);
903 }
904
905 port->clk = of_clk_get_by_name(child, NULL);
906 if (IS_ERR(port->clk)) {
907 dev_err(dev, "%s: cannot get clock\n", port->name);
908 goto skip;
909 }
910
911 ret = devm_add_action(dev, mvebu_pcie_port_clk_put, port);
912 if (ret < 0) {
913 clk_put(port->clk);
914 goto err;
915 }
916
917 return 1;
918
919skip:
920 ret = 0;
921
922 /* In the case of skipping, we need to free these */
923 devm_kfree(dev, port->reset_name);
924 port->reset_name = NULL;
925 devm_kfree(dev, port->name);
926 port->name = NULL;
927
928err:
929 return ret;
930}
931
932/*
933 * Power up a PCIe port. PCIe requires the refclk to be stable for 100µs
934 * prior to releasing PERST. See table 2-4 in section 2.6.2 AC Specifications
935 * of the PCI Express Card Electromechanical Specification, 1.1.
936 */
937static int mvebu_pcie_powerup(struct mvebu_pcie_port *port)
938{
939 int ret;
940
941 ret = clk_prepare_enable(port->clk);
942 if (ret < 0)
943 return ret;
944
945 if (port->reset_gpio) {
946 u32 reset_udelay = PCI_PM_D3COLD_WAIT * 1000;
947
948 of_property_read_u32(port->dn, "reset-delay-us",
949 &reset_udelay);
950
951 udelay(100);
952
953 gpiod_set_value_cansleep(port->reset_gpio, 0);
954 msleep(reset_udelay / 1000);
955 }
956
957 return 0;
958}
959
960/*
961 * Power down a PCIe port. Strictly, PCIe requires us to place the card
962 * in D3hot state before asserting PERST#.
963 */
964static void mvebu_pcie_powerdown(struct mvebu_pcie_port *port)
965{
966 gpiod_set_value_cansleep(port->reset_gpio, 1);
967
968 clk_disable_unprepare(port->clk);
969}
970
971/*
972 * We can't use devm_of_pci_get_host_bridge_resources() because we
973 * need to parse our special DT properties encoding the MEM and IO
974 * apertures.
975 */
976static int mvebu_pcie_parse_request_resources(struct mvebu_pcie *pcie)
977{
978 struct device *dev = &pcie->pdev->dev;
979 struct device_node *np = dev->of_node;
980 int ret;
981
982 INIT_LIST_HEAD(&pcie->resources);
983
984 /* Get the bus range */
985 ret = of_pci_parse_bus_range(np, &pcie->busn);
986 if (ret) {
987 dev_err(dev, "failed to parse bus-range property: %d\n", ret);
988 return ret;
989 }
990 pci_add_resource(&pcie->resources, &pcie->busn);
991
992 /* Get the PCIe memory aperture */
993 mvebu_mbus_get_pcie_mem_aperture(&pcie->mem);
994 if (resource_size(&pcie->mem) == 0) {
995 dev_err(dev, "invalid memory aperture size\n");
996 return -EINVAL;
997 }
998
999 pcie->mem.name = "PCI MEM";
1000 pci_add_resource(&pcie->resources, &pcie->mem);
1001
1002 /* Get the PCIe IO aperture */
1003 mvebu_mbus_get_pcie_io_aperture(&pcie->io);
1004
1005 if (resource_size(&pcie->io) != 0) {
1006 pcie->realio.flags = pcie->io.flags;
1007 pcie->realio.start = PCIBIOS_MIN_IO;
1008 pcie->realio.end = min_t(resource_size_t,
1009 IO_SPACE_LIMIT - SZ_64K,
1010 resource_size(&pcie->io) - 1);
1011 pcie->realio.name = "PCI I/O";
1012
1013 pci_add_resource(&pcie->resources, &pcie->realio);
1014 }
1015
1016 return devm_request_pci_bus_resources(dev, &pcie->resources);
1017}
1018
1019/*
1020 * This is a copy of pci_host_probe(), except that it does the I/O
1021 * remap as the last step, once we are sure we won't fail.
1022 *
1023 * It should be removed once the I/O remap error handling issue has
1024 * been sorted out.
1025 */
1026static int mvebu_pci_host_probe(struct pci_host_bridge *bridge)
1027{
1028 struct mvebu_pcie *pcie;
1029 struct pci_bus *bus, *child;
1030 int ret;
1031
1032 ret = pci_scan_root_bus_bridge(bridge);
1033 if (ret < 0) {
1034 dev_err(bridge->dev.parent, "Scanning root bridge failed");
1035 return ret;
1036 }
1037
1038 pcie = pci_host_bridge_priv(bridge);
1039 if (resource_size(&pcie->io) != 0) {
1040 unsigned int i;
1041
1042 for (i = 0; i < resource_size(&pcie->realio); i += SZ_64K)
1043 pci_ioremap_io(i, pcie->io.start + i);
1044 }
1045
1046 bus = bridge->bus;
1047
1048 /*
1049 * We insert PCI resources into the iomem_resource and
1050 * ioport_resource trees in either pci_bus_claim_resources()
1051 * or pci_bus_assign_resources().
1052 */
1053 if (pci_has_flag(PCI_PROBE_ONLY)) {
1054 pci_bus_claim_resources(bus);
1055 } else {
1056 pci_bus_size_bridges(bus);
1057 pci_bus_assign_resources(bus);
1058
1059 list_for_each_entry(child, &bus->children, node)
1060 pcie_bus_configure_settings(child);
1061 }
1062
1063 pci_bus_add_devices(bus);
1064 return 0;
1065}
1066
1067static int mvebu_pcie_probe(struct platform_device *pdev)
1068{
1069 struct device *dev = &pdev->dev;
1070 struct mvebu_pcie *pcie;
1071 struct pci_host_bridge *bridge;
1072 struct device_node *np = dev->of_node;
1073 struct device_node *child;
1074 int num, i, ret;
1075
1076 bridge = devm_pci_alloc_host_bridge(dev, sizeof(struct mvebu_pcie));
1077 if (!bridge)
1078 return -ENOMEM;
1079
1080 pcie = pci_host_bridge_priv(bridge);
1081 pcie->pdev = pdev;
1082 platform_set_drvdata(pdev, pcie);
1083
1084 ret = mvebu_pcie_parse_request_resources(pcie);
1085 if (ret)
1086 return ret;
1087
1088 num = of_get_available_child_count(np);
1089
1090 pcie->ports = devm_kcalloc(dev, num, sizeof(*pcie->ports), GFP_KERNEL);
1091 if (!pcie->ports)
1092 return -ENOMEM;
1093
1094 i = 0;
1095 for_each_available_child_of_node(np, child) {
1096 struct mvebu_pcie_port *port = &pcie->ports[i];
1097
1098 ret = mvebu_pcie_parse_port(pcie, port, child);
1099 if (ret < 0) {
1100 of_node_put(child);
1101 return ret;
1102 } else if (ret == 0) {
1103 continue;
1104 }
1105
1106 port->dn = child;
1107 i++;
1108 }
1109 pcie->nports = i;
1110
1111 for (i = 0; i < pcie->nports; i++) {
1112 struct mvebu_pcie_port *port = &pcie->ports[i];
1113
1114 child = port->dn;
1115 if (!child)
1116 continue;
1117
1118 ret = mvebu_pcie_powerup(port);
1119 if (ret < 0)
1120 continue;
1121
1122 port->base = mvebu_pcie_map_registers(pdev, child, port);
1123 if (IS_ERR(port->base)) {
1124 dev_err(dev, "%s: cannot map registers\n", port->name);
1125 port->base = NULL;
1126 mvebu_pcie_powerdown(port);
1127 continue;
1128 }
1129
1130 mvebu_pcie_setup_hw(port);
1131 mvebu_pcie_set_local_dev_nr(port, 1);
1132 mvebu_pci_bridge_emul_init(port);
1133 }
1134
1135 pcie->nports = i;
1136
1137 list_splice_init(&pcie->resources, &bridge->windows);
1138 bridge->dev.parent = dev;
1139 bridge->sysdata = pcie;
1140 bridge->busnr = 0;
1141 bridge->ops = &mvebu_pcie_ops;
1142 bridge->map_irq = of_irq_parse_and_map_pci;
1143 bridge->swizzle_irq = pci_common_swizzle;
1144 bridge->align_resource = mvebu_pcie_align_resource;
1145 bridge->msi = pcie->msi;
1146
1147 return mvebu_pci_host_probe(bridge);
1148}
1149
1150static const struct of_device_id mvebu_pcie_of_match_table[] = {
1151 { .compatible = "marvell,armada-xp-pcie", },
1152 { .compatible = "marvell,armada-370-pcie", },
1153 { .compatible = "marvell,dove-pcie", },
1154 { .compatible = "marvell,kirkwood-pcie", },
1155 {},
1156};
1157
1158static const struct dev_pm_ops mvebu_pcie_pm_ops = {
1159 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(mvebu_pcie_suspend, mvebu_pcie_resume)
1160};
1161
1162static struct platform_driver mvebu_pcie_driver = {
1163 .driver = {
1164 .name = "mvebu-pcie",
1165 .of_match_table = mvebu_pcie_of_match_table,
1166 /* driver unloading/unbinding currently not supported */
1167 .suppress_bind_attrs = true,
1168 .pm = &mvebu_pcie_pm_ops,
1169 },
1170 .probe = mvebu_pcie_probe,
1171};
1172builtin_platform_driver(mvebu_pcie_driver);