xref: /openbmc/linux/arch/arm/mach-dove/pcie.c (revision fdaa3725831972284ef2779ddba00491d9dbbfca)
1edabd38eSSaeed Bishara /*
2edabd38eSSaeed Bishara  * arch/arm/mach-dove/pcie.c
3edabd38eSSaeed Bishara  *
4edabd38eSSaeed Bishara  * PCIe functions for Marvell Dove 88AP510 SoC
5edabd38eSSaeed Bishara  *
6edabd38eSSaeed Bishara  * This file is licensed under the terms of the GNU General Public
7edabd38eSSaeed Bishara  * License version 2. This program is licensed "as is" without any
8edabd38eSSaeed Bishara  * warranty of any kind, whether express or implied.
9edabd38eSSaeed Bishara  */
10edabd38eSSaeed Bishara 
11edabd38eSSaeed Bishara #include <linux/kernel.h>
12edabd38eSSaeed Bishara #include <linux/pci.h>
13529b89efSSebastian Hesselbarth #include <linux/clk.h>
14cc22b4c1SRob Herring #include <video/vga.h>
15edabd38eSSaeed Bishara #include <asm/mach/pci.h>
16edabd38eSSaeed Bishara #include <asm/mach/arch.h>
17edabd38eSSaeed Bishara #include <asm/setup.h>
18edabd38eSSaeed Bishara #include <asm/delay.h>
19edabd38eSSaeed Bishara #include <plat/pcie.h>
2045173d5eSAndrew Lunn #include <plat/addr-map.h>
21ce78179eSArnd Bergmann #include "irqs.h"
22ce78179eSArnd Bergmann #include "bridge-regs.h"
23edabd38eSSaeed Bishara #include "common.h"
24edabd38eSSaeed Bishara 
25edabd38eSSaeed Bishara struct pcie_port {
26edabd38eSSaeed Bishara 	u8			index;
27edabd38eSSaeed Bishara 	u8			root_bus_nr;
28edabd38eSSaeed Bishara 	void __iomem		*base;
29edabd38eSSaeed Bishara 	spinlock_t		conf_lock;
30edabd38eSSaeed Bishara 	char			mem_space_name[16];
31d191bb69SRob Herring 	struct resource		res;
32edabd38eSSaeed Bishara };
33edabd38eSSaeed Bishara 
34edabd38eSSaeed Bishara static struct pcie_port pcie_port[2];
35edabd38eSSaeed Bishara static int num_pcie_ports;
36edabd38eSSaeed Bishara 
37edabd38eSSaeed Bishara 
38edabd38eSSaeed Bishara static int __init dove_pcie_setup(int nr, struct pci_sys_data *sys)
39edabd38eSSaeed Bishara {
40edabd38eSSaeed Bishara 	struct pcie_port *pp;
416198461eSPali Rohár 	struct resource realio;
42edabd38eSSaeed Bishara 
43edabd38eSSaeed Bishara 	if (nr >= num_pcie_ports)
44edabd38eSSaeed Bishara 		return 0;
45edabd38eSSaeed Bishara 
46edabd38eSSaeed Bishara 	pp = &pcie_port[nr];
4743ba990bSRussell King 	sys->private_data = pp;
48edabd38eSSaeed Bishara 	pp->root_bus_nr = sys->busnr;
49edabd38eSSaeed Bishara 
50edabd38eSSaeed Bishara 	/*
51edabd38eSSaeed Bishara 	 * Generic PCIe unit setup.
52edabd38eSSaeed Bishara 	 */
53edabd38eSSaeed Bishara 	orion_pcie_set_local_bus_nr(pp->base, sys->busnr);
54edabd38eSSaeed Bishara 
5563a9332bSAndrew Lunn 	orion_pcie_setup(pp->base);
56edabd38eSSaeed Bishara 
576198461eSPali Rohár 	realio.start = sys->busnr * SZ_64K;
586198461eSPali Rohár 	realio.end = realio.start + SZ_64K - 1;
596198461eSPali Rohár 	pci_remap_iospace(&realio, pp->index == 0 ? DOVE_PCIE0_IO_PHYS_BASE :
606198461eSPali Rohár 						    DOVE_PCIE1_IO_PHYS_BASE);
61edabd38eSSaeed Bishara 
62edabd38eSSaeed Bishara 	/*
63edabd38eSSaeed Bishara 	 * IORESOURCE_MEM
64edabd38eSSaeed Bishara 	 */
65edabd38eSSaeed Bishara 	snprintf(pp->mem_space_name, sizeof(pp->mem_space_name),
66edabd38eSSaeed Bishara 		 "PCIe %d MEM", pp->index);
67edabd38eSSaeed Bishara 	pp->mem_space_name[sizeof(pp->mem_space_name) - 1] = 0;
68d191bb69SRob Herring 	pp->res.name = pp->mem_space_name;
69edabd38eSSaeed Bishara 	if (pp->index == 0) {
70d191bb69SRob Herring 		pp->res.start = DOVE_PCIE0_MEM_PHYS_BASE;
71d191bb69SRob Herring 		pp->res.end = pp->res.start + DOVE_PCIE0_MEM_SIZE - 1;
72edabd38eSSaeed Bishara 	} else {
73d191bb69SRob Herring 		pp->res.start = DOVE_PCIE1_MEM_PHYS_BASE;
74d191bb69SRob Herring 		pp->res.end = pp->res.start + DOVE_PCIE1_MEM_SIZE - 1;
75edabd38eSSaeed Bishara 	}
76d191bb69SRob Herring 	pp->res.flags = IORESOURCE_MEM;
77d191bb69SRob Herring 	if (request_resource(&iomem_resource, &pp->res))
78edabd38eSSaeed Bishara 		panic("Request PCIe Memory resource failed\n");
79d191bb69SRob Herring 	pci_add_resource_offset(&sys->resources, &pp->res, sys->mem_offset);
80edabd38eSSaeed Bishara 
81edabd38eSSaeed Bishara 	return 1;
82edabd38eSSaeed Bishara }
83edabd38eSSaeed Bishara 
84edabd38eSSaeed Bishara static int pcie_valid_config(struct pcie_port *pp, int bus, int dev)
85edabd38eSSaeed Bishara {
86edabd38eSSaeed Bishara 	/*
87edabd38eSSaeed Bishara 	 * Don't go out when trying to access nonexisting devices
88edabd38eSSaeed Bishara 	 * on the local bus.
89edabd38eSSaeed Bishara 	 */
90edabd38eSSaeed Bishara 	if (bus == pp->root_bus_nr && dev > 1)
91edabd38eSSaeed Bishara 		return 0;
92edabd38eSSaeed Bishara 
93edabd38eSSaeed Bishara 	return 1;
94edabd38eSSaeed Bishara }
95edabd38eSSaeed Bishara 
96edabd38eSSaeed Bishara static int pcie_rd_conf(struct pci_bus *bus, u32 devfn, int where,
97edabd38eSSaeed Bishara 			int size, u32 *val)
98edabd38eSSaeed Bishara {
9943ba990bSRussell King 	struct pci_sys_data *sys = bus->sysdata;
10043ba990bSRussell King 	struct pcie_port *pp = sys->private_data;
101edabd38eSSaeed Bishara 	unsigned long flags;
102edabd38eSSaeed Bishara 	int ret;
103edabd38eSSaeed Bishara 
104edabd38eSSaeed Bishara 	if (pcie_valid_config(pp, bus->number, PCI_SLOT(devfn)) == 0) {
105edabd38eSSaeed Bishara 		*val = 0xffffffff;
106edabd38eSSaeed Bishara 		return PCIBIOS_DEVICE_NOT_FOUND;
107edabd38eSSaeed Bishara 	}
108edabd38eSSaeed Bishara 
109edabd38eSSaeed Bishara 	spin_lock_irqsave(&pp->conf_lock, flags);
110edabd38eSSaeed Bishara 	ret = orion_pcie_rd_conf(pp->base, bus, devfn, where, size, val);
111edabd38eSSaeed Bishara 	spin_unlock_irqrestore(&pp->conf_lock, flags);
112edabd38eSSaeed Bishara 
113edabd38eSSaeed Bishara 	return ret;
114edabd38eSSaeed Bishara }
115edabd38eSSaeed Bishara 
116edabd38eSSaeed Bishara static int pcie_wr_conf(struct pci_bus *bus, u32 devfn,
117edabd38eSSaeed Bishara 			int where, int size, u32 val)
118edabd38eSSaeed Bishara {
11943ba990bSRussell King 	struct pci_sys_data *sys = bus->sysdata;
12043ba990bSRussell King 	struct pcie_port *pp = sys->private_data;
121edabd38eSSaeed Bishara 	unsigned long flags;
122edabd38eSSaeed Bishara 	int ret;
123edabd38eSSaeed Bishara 
124edabd38eSSaeed Bishara 	if (pcie_valid_config(pp, bus->number, PCI_SLOT(devfn)) == 0)
125edabd38eSSaeed Bishara 		return PCIBIOS_DEVICE_NOT_FOUND;
126edabd38eSSaeed Bishara 
127edabd38eSSaeed Bishara 	spin_lock_irqsave(&pp->conf_lock, flags);
128edabd38eSSaeed Bishara 	ret = orion_pcie_wr_conf(pp->base, bus, devfn, where, size, val);
129edabd38eSSaeed Bishara 	spin_unlock_irqrestore(&pp->conf_lock, flags);
130edabd38eSSaeed Bishara 
131edabd38eSSaeed Bishara 	return ret;
132edabd38eSSaeed Bishara }
133edabd38eSSaeed Bishara 
134edabd38eSSaeed Bishara static struct pci_ops pcie_ops = {
135edabd38eSSaeed Bishara 	.read = pcie_rd_conf,
136edabd38eSSaeed Bishara 	.write = pcie_wr_conf,
137edabd38eSSaeed Bishara };
138edabd38eSSaeed Bishara 
139*fdaa3725SPali Rohár /*
140*fdaa3725SPali Rohár  * The root complex has a hardwired class of PCI_CLASS_MEMORY_OTHER, when it
141*fdaa3725SPali Rohár  * is operating as a root complex this needs to be switched to
142*fdaa3725SPali Rohár  * PCI_CLASS_BRIDGE_HOST or Linux will errantly try to process the BAR's on
143*fdaa3725SPali Rohár  * the device. Decoding setup is handled by the orion code.
144*fdaa3725SPali Rohár  */
145351a102dSGreg Kroah-Hartman static void rc_pci_fixup(struct pci_dev *dev)
146edabd38eSSaeed Bishara {
147edabd38eSSaeed Bishara 	if (dev->bus->parent == NULL && dev->devfn == 0) {
148edabd38eSSaeed Bishara 		int i;
149edabd38eSSaeed Bishara 
150*fdaa3725SPali Rohár 		dev->class &= 0xff;
151*fdaa3725SPali Rohár 		dev->class |= PCI_CLASS_BRIDGE_HOST << 8;
152edabd38eSSaeed Bishara 		for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
153edabd38eSSaeed Bishara 			dev->resource[i].start = 0;
154edabd38eSSaeed Bishara 			dev->resource[i].end   = 0;
155edabd38eSSaeed Bishara 			dev->resource[i].flags = 0;
156edabd38eSSaeed Bishara 		}
157edabd38eSSaeed Bishara 	}
158edabd38eSSaeed Bishara }
159edabd38eSSaeed Bishara DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_MARVELL, PCI_ANY_ID, rc_pci_fixup);
160edabd38eSSaeed Bishara 
16197ad2bdcSLorenzo Pieralisi static int __init
16297ad2bdcSLorenzo Pieralisi dove_pcie_scan_bus(int nr, struct pci_host_bridge *bridge)
163edabd38eSSaeed Bishara {
16497ad2bdcSLorenzo Pieralisi 	struct pci_sys_data *sys = pci_host_bridge_priv(bridge);
16597ad2bdcSLorenzo Pieralisi 
1669e808eb6SBjorn Helgaas 	if (nr >= num_pcie_ports) {
167edabd38eSSaeed Bishara 		BUG();
16897ad2bdcSLorenzo Pieralisi 		return -EINVAL;
169edabd38eSSaeed Bishara 	}
170edabd38eSSaeed Bishara 
17197ad2bdcSLorenzo Pieralisi 	list_splice_init(&sys->resources, &bridge->windows);
17297ad2bdcSLorenzo Pieralisi 	bridge->dev.parent = NULL;
17397ad2bdcSLorenzo Pieralisi 	bridge->sysdata = sys;
17497ad2bdcSLorenzo Pieralisi 	bridge->busnr = sys->busnr;
17597ad2bdcSLorenzo Pieralisi 	bridge->ops = &pcie_ops;
17697ad2bdcSLorenzo Pieralisi 
17797ad2bdcSLorenzo Pieralisi 	return pci_scan_root_bus_bridge(bridge);
178edabd38eSSaeed Bishara }
179edabd38eSSaeed Bishara 
180d5341942SRalf Baechle static int __init dove_pcie_map_irq(const struct pci_dev *dev, u8 slot, u8 pin)
181edabd38eSSaeed Bishara {
18243ba990bSRussell King 	struct pci_sys_data *sys = dev->sysdata;
18343ba990bSRussell King 	struct pcie_port *pp = sys->private_data;
184edabd38eSSaeed Bishara 
185edabd38eSSaeed Bishara 	return pp->index ? IRQ_DOVE_PCIE1 : IRQ_DOVE_PCIE0;
186edabd38eSSaeed Bishara }
187edabd38eSSaeed Bishara 
188edabd38eSSaeed Bishara static struct hw_pci dove_pci __initdata = {
189edabd38eSSaeed Bishara 	.nr_controllers	= 2,
190edabd38eSSaeed Bishara 	.setup		= dove_pcie_setup,
191edabd38eSSaeed Bishara 	.scan		= dove_pcie_scan_bus,
192edabd38eSSaeed Bishara 	.map_irq	= dove_pcie_map_irq,
193edabd38eSSaeed Bishara };
194edabd38eSSaeed Bishara 
195c3c5a281SThomas Petazzoni static void __init add_pcie_port(int index, void __iomem *base)
196edabd38eSSaeed Bishara {
197edabd38eSSaeed Bishara 	printk(KERN_INFO "Dove PCIe port %d: ", index);
198edabd38eSSaeed Bishara 
199c3c5a281SThomas Petazzoni 	if (orion_pcie_link_up(base)) {
200edabd38eSSaeed Bishara 		struct pcie_port *pp = &pcie_port[num_pcie_ports++];
201529b89efSSebastian Hesselbarth 		struct clk *clk = clk_get_sys("pcie", (index ? "1" : "0"));
202529b89efSSebastian Hesselbarth 
203529b89efSSebastian Hesselbarth 		if (!IS_ERR(clk))
204529b89efSSebastian Hesselbarth 			clk_prepare_enable(clk);
205edabd38eSSaeed Bishara 
206edabd38eSSaeed Bishara 		printk(KERN_INFO "link up\n");
207edabd38eSSaeed Bishara 
208edabd38eSSaeed Bishara 		pp->index = index;
209edabd38eSSaeed Bishara 		pp->root_bus_nr = -1;
210c3c5a281SThomas Petazzoni 		pp->base = base;
211edabd38eSSaeed Bishara 		spin_lock_init(&pp->conf_lock);
212d191bb69SRob Herring 		memset(&pp->res, 0, sizeof(pp->res));
213edabd38eSSaeed Bishara 	} else {
214edabd38eSSaeed Bishara 		printk(KERN_INFO "link down, ignoring\n");
215edabd38eSSaeed Bishara 	}
216edabd38eSSaeed Bishara }
217edabd38eSSaeed Bishara 
218edabd38eSSaeed Bishara void __init dove_pcie_init(int init_port0, int init_port1)
219edabd38eSSaeed Bishara {
220cc22b4c1SRob Herring 	vga_base = DOVE_PCIE0_MEM_PHYS_BASE;
221cc22b4c1SRob Herring 
222edabd38eSSaeed Bishara 	if (init_port0)
223edabd38eSSaeed Bishara 		add_pcie_port(0, DOVE_PCIE0_VIRT_BASE);
224edabd38eSSaeed Bishara 
225edabd38eSSaeed Bishara 	if (init_port1)
226edabd38eSSaeed Bishara 		add_pcie_port(1, DOVE_PCIE1_VIRT_BASE);
227edabd38eSSaeed Bishara 
228edabd38eSSaeed Bishara 	pci_common_init(&dove_pci);
229edabd38eSSaeed Bishara }
230