10fdebc5eSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2edabd38eSSaeed Bishara /*
3edabd38eSSaeed Bishara * arch/arm/mach-dove/pcie.c
4edabd38eSSaeed Bishara *
5edabd38eSSaeed Bishara * PCIe functions for Marvell Dove 88AP510 SoC
6edabd38eSSaeed Bishara */
7edabd38eSSaeed Bishara
8edabd38eSSaeed Bishara #include <linux/kernel.h>
9edabd38eSSaeed Bishara #include <linux/pci.h>
10529b89efSSebastian Hesselbarth #include <linux/clk.h>
11cc22b4c1SRob Herring #include <video/vga.h>
12edabd38eSSaeed Bishara #include <asm/mach/pci.h>
13edabd38eSSaeed Bishara #include <asm/mach/arch.h>
14edabd38eSSaeed Bishara #include <asm/setup.h>
15edabd38eSSaeed Bishara #include <asm/delay.h>
16edabd38eSSaeed Bishara #include <plat/pcie.h>
1745173d5eSAndrew Lunn #include <plat/addr-map.h>
18ce78179eSArnd Bergmann #include "irqs.h"
19ce78179eSArnd Bergmann #include "bridge-regs.h"
20edabd38eSSaeed Bishara #include "common.h"
21edabd38eSSaeed Bishara
22edabd38eSSaeed Bishara struct pcie_port {
23edabd38eSSaeed Bishara u8 index;
24edabd38eSSaeed Bishara u8 root_bus_nr;
25edabd38eSSaeed Bishara void __iomem *base;
26edabd38eSSaeed Bishara spinlock_t conf_lock;
27edabd38eSSaeed Bishara char mem_space_name[16];
28d191bb69SRob Herring struct resource res;
29edabd38eSSaeed Bishara };
30edabd38eSSaeed Bishara
31edabd38eSSaeed Bishara static struct pcie_port pcie_port[2];
32edabd38eSSaeed Bishara static int num_pcie_ports;
33edabd38eSSaeed Bishara
34edabd38eSSaeed Bishara
dove_pcie_setup(int nr,struct pci_sys_data * sys)35edabd38eSSaeed Bishara static int __init dove_pcie_setup(int nr, struct pci_sys_data *sys)
36edabd38eSSaeed Bishara {
37edabd38eSSaeed Bishara struct pcie_port *pp;
386198461eSPali Rohár struct resource realio;
39edabd38eSSaeed Bishara
40edabd38eSSaeed Bishara if (nr >= num_pcie_ports)
41edabd38eSSaeed Bishara return 0;
42edabd38eSSaeed Bishara
43edabd38eSSaeed Bishara pp = &pcie_port[nr];
4443ba990bSRussell King sys->private_data = pp;
45edabd38eSSaeed Bishara pp->root_bus_nr = sys->busnr;
46edabd38eSSaeed Bishara
47edabd38eSSaeed Bishara /*
48edabd38eSSaeed Bishara * Generic PCIe unit setup.
49edabd38eSSaeed Bishara */
50edabd38eSSaeed Bishara orion_pcie_set_local_bus_nr(pp->base, sys->busnr);
51edabd38eSSaeed Bishara
5263a9332bSAndrew Lunn orion_pcie_setup(pp->base);
53edabd38eSSaeed Bishara
546198461eSPali Rohár realio.start = sys->busnr * SZ_64K;
556198461eSPali Rohár realio.end = realio.start + SZ_64K - 1;
566198461eSPali Rohár pci_remap_iospace(&realio, pp->index == 0 ? DOVE_PCIE0_IO_PHYS_BASE :
576198461eSPali Rohár DOVE_PCIE1_IO_PHYS_BASE);
58edabd38eSSaeed Bishara
59edabd38eSSaeed Bishara /*
60edabd38eSSaeed Bishara * IORESOURCE_MEM
61edabd38eSSaeed Bishara */
62edabd38eSSaeed Bishara snprintf(pp->mem_space_name, sizeof(pp->mem_space_name),
63edabd38eSSaeed Bishara "PCIe %d MEM", pp->index);
64edabd38eSSaeed Bishara pp->mem_space_name[sizeof(pp->mem_space_name) - 1] = 0;
65d191bb69SRob Herring pp->res.name = pp->mem_space_name;
66edabd38eSSaeed Bishara if (pp->index == 0) {
67d191bb69SRob Herring pp->res.start = DOVE_PCIE0_MEM_PHYS_BASE;
68d191bb69SRob Herring pp->res.end = pp->res.start + DOVE_PCIE0_MEM_SIZE - 1;
69edabd38eSSaeed Bishara } else {
70d191bb69SRob Herring pp->res.start = DOVE_PCIE1_MEM_PHYS_BASE;
71d191bb69SRob Herring pp->res.end = pp->res.start + DOVE_PCIE1_MEM_SIZE - 1;
72edabd38eSSaeed Bishara }
73d191bb69SRob Herring pp->res.flags = IORESOURCE_MEM;
74d191bb69SRob Herring if (request_resource(&iomem_resource, &pp->res))
75edabd38eSSaeed Bishara panic("Request PCIe Memory resource failed\n");
76d191bb69SRob Herring pci_add_resource_offset(&sys->resources, &pp->res, sys->mem_offset);
77edabd38eSSaeed Bishara
78edabd38eSSaeed Bishara return 1;
79edabd38eSSaeed Bishara }
80edabd38eSSaeed Bishara
pcie_valid_config(struct pcie_port * pp,int bus,int dev)81edabd38eSSaeed Bishara static int pcie_valid_config(struct pcie_port *pp, int bus, int dev)
82edabd38eSSaeed Bishara {
83edabd38eSSaeed Bishara /*
84edabd38eSSaeed Bishara * Don't go out when trying to access nonexisting devices
85edabd38eSSaeed Bishara * on the local bus.
86edabd38eSSaeed Bishara */
87edabd38eSSaeed Bishara if (bus == pp->root_bus_nr && dev > 1)
88edabd38eSSaeed Bishara return 0;
89edabd38eSSaeed Bishara
90edabd38eSSaeed Bishara return 1;
91edabd38eSSaeed Bishara }
92edabd38eSSaeed Bishara
pcie_rd_conf(struct pci_bus * bus,u32 devfn,int where,int size,u32 * val)93edabd38eSSaeed Bishara static int pcie_rd_conf(struct pci_bus *bus, u32 devfn, int where,
94edabd38eSSaeed Bishara int size, u32 *val)
95edabd38eSSaeed Bishara {
9643ba990bSRussell King struct pci_sys_data *sys = bus->sysdata;
9743ba990bSRussell King struct pcie_port *pp = sys->private_data;
98edabd38eSSaeed Bishara unsigned long flags;
99edabd38eSSaeed Bishara int ret;
100edabd38eSSaeed Bishara
101edabd38eSSaeed Bishara if (pcie_valid_config(pp, bus->number, PCI_SLOT(devfn)) == 0) {
102edabd38eSSaeed Bishara *val = 0xffffffff;
103edabd38eSSaeed Bishara return PCIBIOS_DEVICE_NOT_FOUND;
104edabd38eSSaeed Bishara }
105edabd38eSSaeed Bishara
106edabd38eSSaeed Bishara spin_lock_irqsave(&pp->conf_lock, flags);
107edabd38eSSaeed Bishara ret = orion_pcie_rd_conf(pp->base, bus, devfn, where, size, val);
108edabd38eSSaeed Bishara spin_unlock_irqrestore(&pp->conf_lock, flags);
109edabd38eSSaeed Bishara
110edabd38eSSaeed Bishara return ret;
111edabd38eSSaeed Bishara }
112edabd38eSSaeed Bishara
pcie_wr_conf(struct pci_bus * bus,u32 devfn,int where,int size,u32 val)113edabd38eSSaeed Bishara static int pcie_wr_conf(struct pci_bus *bus, u32 devfn,
114edabd38eSSaeed Bishara int where, int size, u32 val)
115edabd38eSSaeed Bishara {
11643ba990bSRussell King struct pci_sys_data *sys = bus->sysdata;
11743ba990bSRussell King struct pcie_port *pp = sys->private_data;
118edabd38eSSaeed Bishara unsigned long flags;
119edabd38eSSaeed Bishara int ret;
120edabd38eSSaeed Bishara
121edabd38eSSaeed Bishara if (pcie_valid_config(pp, bus->number, PCI_SLOT(devfn)) == 0)
122edabd38eSSaeed Bishara return PCIBIOS_DEVICE_NOT_FOUND;
123edabd38eSSaeed Bishara
124edabd38eSSaeed Bishara spin_lock_irqsave(&pp->conf_lock, flags);
125edabd38eSSaeed Bishara ret = orion_pcie_wr_conf(pp->base, bus, devfn, where, size, val);
126edabd38eSSaeed Bishara spin_unlock_irqrestore(&pp->conf_lock, flags);
127edabd38eSSaeed Bishara
128edabd38eSSaeed Bishara return ret;
129edabd38eSSaeed Bishara }
130edabd38eSSaeed Bishara
131edabd38eSSaeed Bishara static struct pci_ops pcie_ops = {
132edabd38eSSaeed Bishara .read = pcie_rd_conf,
133edabd38eSSaeed Bishara .write = pcie_wr_conf,
134edabd38eSSaeed Bishara };
135edabd38eSSaeed Bishara
136fdaa3725SPali Rohár /*
137fdaa3725SPali Rohár * The root complex has a hardwired class of PCI_CLASS_MEMORY_OTHER, when it
138fdaa3725SPali Rohár * is operating as a root complex this needs to be switched to
139fdaa3725SPali Rohár * PCI_CLASS_BRIDGE_HOST or Linux will errantly try to process the BAR's on
140fdaa3725SPali Rohár * the device. Decoding setup is handled by the orion code.
141fdaa3725SPali Rohár */
rc_pci_fixup(struct pci_dev * dev)142351a102dSGreg Kroah-Hartman static void rc_pci_fixup(struct pci_dev *dev)
143edabd38eSSaeed Bishara {
144edabd38eSSaeed Bishara if (dev->bus->parent == NULL && dev->devfn == 0) {
145*09cc9006SMika Westerberg struct resource *r;
146edabd38eSSaeed Bishara
147fdaa3725SPali Rohár dev->class &= 0xff;
148fdaa3725SPali Rohár dev->class |= PCI_CLASS_BRIDGE_HOST << 8;
149*09cc9006SMika Westerberg pci_dev_for_each_resource(dev, r) {
150*09cc9006SMika Westerberg r->start = 0;
151*09cc9006SMika Westerberg r->end = 0;
152*09cc9006SMika Westerberg r->flags = 0;
153edabd38eSSaeed Bishara }
154edabd38eSSaeed Bishara }
155edabd38eSSaeed Bishara }
156edabd38eSSaeed Bishara DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_MARVELL, PCI_ANY_ID, rc_pci_fixup);
157edabd38eSSaeed Bishara
15897ad2bdcSLorenzo Pieralisi static int __init
dove_pcie_scan_bus(int nr,struct pci_host_bridge * bridge)15997ad2bdcSLorenzo Pieralisi dove_pcie_scan_bus(int nr, struct pci_host_bridge *bridge)
160edabd38eSSaeed Bishara {
16197ad2bdcSLorenzo Pieralisi struct pci_sys_data *sys = pci_host_bridge_priv(bridge);
16297ad2bdcSLorenzo Pieralisi
1639e808eb6SBjorn Helgaas if (nr >= num_pcie_ports) {
164edabd38eSSaeed Bishara BUG();
16597ad2bdcSLorenzo Pieralisi return -EINVAL;
166edabd38eSSaeed Bishara }
167edabd38eSSaeed Bishara
16897ad2bdcSLorenzo Pieralisi list_splice_init(&sys->resources, &bridge->windows);
16997ad2bdcSLorenzo Pieralisi bridge->dev.parent = NULL;
17097ad2bdcSLorenzo Pieralisi bridge->sysdata = sys;
17197ad2bdcSLorenzo Pieralisi bridge->busnr = sys->busnr;
17297ad2bdcSLorenzo Pieralisi bridge->ops = &pcie_ops;
17397ad2bdcSLorenzo Pieralisi
17497ad2bdcSLorenzo Pieralisi return pci_scan_root_bus_bridge(bridge);
175edabd38eSSaeed Bishara }
176edabd38eSSaeed Bishara
dove_pcie_map_irq(const struct pci_dev * dev,u8 slot,u8 pin)177d5341942SRalf Baechle static int __init dove_pcie_map_irq(const struct pci_dev *dev, u8 slot, u8 pin)
178edabd38eSSaeed Bishara {
17943ba990bSRussell King struct pci_sys_data *sys = dev->sysdata;
18043ba990bSRussell King struct pcie_port *pp = sys->private_data;
181edabd38eSSaeed Bishara
182edabd38eSSaeed Bishara return pp->index ? IRQ_DOVE_PCIE1 : IRQ_DOVE_PCIE0;
183edabd38eSSaeed Bishara }
184edabd38eSSaeed Bishara
185edabd38eSSaeed Bishara static struct hw_pci dove_pci __initdata = {
186edabd38eSSaeed Bishara .nr_controllers = 2,
187edabd38eSSaeed Bishara .setup = dove_pcie_setup,
188edabd38eSSaeed Bishara .scan = dove_pcie_scan_bus,
189edabd38eSSaeed Bishara .map_irq = dove_pcie_map_irq,
190edabd38eSSaeed Bishara };
191edabd38eSSaeed Bishara
add_pcie_port(int index,void __iomem * base)192c3c5a281SThomas Petazzoni static void __init add_pcie_port(int index, void __iomem *base)
193edabd38eSSaeed Bishara {
194edabd38eSSaeed Bishara printk(KERN_INFO "Dove PCIe port %d: ", index);
195edabd38eSSaeed Bishara
196c3c5a281SThomas Petazzoni if (orion_pcie_link_up(base)) {
197edabd38eSSaeed Bishara struct pcie_port *pp = &pcie_port[num_pcie_ports++];
198529b89efSSebastian Hesselbarth struct clk *clk = clk_get_sys("pcie", (index ? "1" : "0"));
199529b89efSSebastian Hesselbarth
200529b89efSSebastian Hesselbarth if (!IS_ERR(clk))
201529b89efSSebastian Hesselbarth clk_prepare_enable(clk);
202edabd38eSSaeed Bishara
203edabd38eSSaeed Bishara printk(KERN_INFO "link up\n");
204edabd38eSSaeed Bishara
205edabd38eSSaeed Bishara pp->index = index;
206edabd38eSSaeed Bishara pp->root_bus_nr = -1;
207c3c5a281SThomas Petazzoni pp->base = base;
208edabd38eSSaeed Bishara spin_lock_init(&pp->conf_lock);
209d191bb69SRob Herring memset(&pp->res, 0, sizeof(pp->res));
210edabd38eSSaeed Bishara } else {
211edabd38eSSaeed Bishara printk(KERN_INFO "link down, ignoring\n");
212edabd38eSSaeed Bishara }
213edabd38eSSaeed Bishara }
214edabd38eSSaeed Bishara
dove_pcie_init(int init_port0,int init_port1)215edabd38eSSaeed Bishara void __init dove_pcie_init(int init_port0, int init_port1)
216edabd38eSSaeed Bishara {
217cc22b4c1SRob Herring vga_base = DOVE_PCIE0_MEM_PHYS_BASE;
218cc22b4c1SRob Herring
219edabd38eSSaeed Bishara if (init_port0)
220edabd38eSSaeed Bishara add_pcie_port(0, DOVE_PCIE0_VIRT_BASE);
221edabd38eSSaeed Bishara
222edabd38eSSaeed Bishara if (init_port1)
223edabd38eSSaeed Bishara add_pcie_port(1, DOVE_PCIE1_VIRT_BASE);
224edabd38eSSaeed Bishara
225edabd38eSSaeed Bishara pci_common_init(&dove_pci);
226edabd38eSSaeed Bishara }
227