blob: 8def894f32968f1936386bc1f3b2b7b0e142508f [file] [log] [blame]
Saeed Bishara651c74c2008-06-22 22:45:06 +02001/*
2 * arch/arm/mach-kirkwood/pcie.c
3 *
4 * PCIe functions for Marvell Kirkwood SoCs
5 *
6 * This file is licensed under the terms of the GNU General Public
7 * License version 2. This program is licensed "as is" without any
8 * warranty of any kind, whether express or implied.
9 */
10
11#include <linux/kernel.h>
12#include <linux/pci.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090013#include <linux/slab.h>
Saeed Bishara651c74c2008-06-22 22:45:06 +020014#include <linux/mbus.h>
Rob Herringcc22b4c2011-06-28 21:22:40 -050015#include <video/vga.h>
Nicolas Pitre6e5c11a2009-01-07 04:47:02 +010016#include <asm/irq.h>
Saeed Bishara651c74c2008-06-22 22:45:06 +020017#include <asm/mach/pci.h>
Lennert Buytenhek6f088f12008-08-09 13:44:58 +020018#include <plat/pcie.h>
Rabeeh Khourye8b2b7b2009-03-22 17:30:32 +020019#include <mach/bridge-regs.h>
Andrew Lunn45173d52011-12-07 21:48:06 +010020#include <plat/addr-map.h>
Saeed Bishara651c74c2008-06-22 22:45:06 +020021#include "common.h"
22
Eric Cooper0e0cdd32011-02-02 17:16:10 -050023void kirkwood_enable_pcie(void)
24{
25 u32 curr = readl(CLOCK_GATING_CTRL);
26 if (!(curr & CGC_PEX0))
27 writel(curr | CGC_PEX0, CLOCK_GATING_CTRL);
28}
29
Ronen Shitritb2b3dc22008-09-15 10:40:35 +030030void __init kirkwood_pcie_id(u32 *dev, u32 *rev)
31{
Eric Cooper0e0cdd32011-02-02 17:16:10 -050032 kirkwood_enable_pcie();
Saeed Bisharaffd58bd2010-06-08 14:21:34 +030033 *dev = orion_pcie_dev_id((void __iomem *)PCIE_VIRT_BASE);
34 *rev = orion_pcie_rev((void __iomem *)PCIE_VIRT_BASE);
Ronen Shitritb2b3dc22008-09-15 10:40:35 +030035}
36
Saeed Bisharaffd58bd2010-06-08 14:21:34 +030037struct pcie_port {
38 u8 root_bus_nr;
39 void __iomem *base;
40 spinlock_t conf_lock;
41 int irq;
42 struct resource res[2];
43};
44
45static int pcie_port_map[2];
46static int num_pcie_ports;
47
48static inline struct pcie_port *bus_to_port(struct pci_bus *bus)
49{
50 struct pci_sys_data *sys = bus->sysdata;
51 return sys->private_data;
52}
53
54static int pcie_valid_config(struct pcie_port *pp, int bus, int dev)
Saeed Bishara651c74c2008-06-22 22:45:06 +020055{
56 /*
57 * Don't go out when trying to access --
58 * 1. nonexisting device on local bus
59 * 2. where there's no device connected (no link)
60 */
Saeed Bisharaffd58bd2010-06-08 14:21:34 +030061 if (bus == pp->root_bus_nr && dev == 0)
Saeed Bishara651c74c2008-06-22 22:45:06 +020062 return 1;
63
Saeed Bisharaffd58bd2010-06-08 14:21:34 +030064 if (!orion_pcie_link_up(pp->base))
Saeed Bishara651c74c2008-06-22 22:45:06 +020065 return 0;
66
Saeed Bisharaffd58bd2010-06-08 14:21:34 +030067 if (bus == pp->root_bus_nr && dev != 1)
Saeed Bishara651c74c2008-06-22 22:45:06 +020068 return 0;
69
70 return 1;
71}
72
73
74/*
75 * PCIe config cycles are done by programming the PCIE_CONF_ADDR register
76 * and then reading the PCIE_CONF_DATA register. Need to make sure these
77 * transactions are atomic.
78 */
Saeed Bishara651c74c2008-06-22 22:45:06 +020079
80static int pcie_rd_conf(struct pci_bus *bus, u32 devfn, int where,
81 int size, u32 *val)
82{
Saeed Bisharaffd58bd2010-06-08 14:21:34 +030083 struct pcie_port *pp = bus_to_port(bus);
Saeed Bishara651c74c2008-06-22 22:45:06 +020084 unsigned long flags;
85 int ret;
86
Saeed Bisharaffd58bd2010-06-08 14:21:34 +030087 if (pcie_valid_config(pp, bus->number, PCI_SLOT(devfn)) == 0) {
Saeed Bishara651c74c2008-06-22 22:45:06 +020088 *val = 0xffffffff;
89 return PCIBIOS_DEVICE_NOT_FOUND;
90 }
91
Saeed Bisharaffd58bd2010-06-08 14:21:34 +030092 spin_lock_irqsave(&pp->conf_lock, flags);
93 ret = orion_pcie_rd_conf(pp->base, bus, devfn, where, size, val);
94 spin_unlock_irqrestore(&pp->conf_lock, flags);
Saeed Bishara651c74c2008-06-22 22:45:06 +020095
96 return ret;
97}
98
99static int pcie_wr_conf(struct pci_bus *bus, u32 devfn,
100 int where, int size, u32 val)
101{
Saeed Bisharaffd58bd2010-06-08 14:21:34 +0300102 struct pcie_port *pp = bus_to_port(bus);
Saeed Bishara651c74c2008-06-22 22:45:06 +0200103 unsigned long flags;
104 int ret;
105
Saeed Bisharaffd58bd2010-06-08 14:21:34 +0300106 if (pcie_valid_config(pp, bus->number, PCI_SLOT(devfn)) == 0)
Saeed Bishara651c74c2008-06-22 22:45:06 +0200107 return PCIBIOS_DEVICE_NOT_FOUND;
108
Saeed Bisharaffd58bd2010-06-08 14:21:34 +0300109 spin_lock_irqsave(&pp->conf_lock, flags);
110 ret = orion_pcie_wr_conf(pp->base, bus, devfn, where, size, val);
111 spin_unlock_irqrestore(&pp->conf_lock, flags);
Saeed Bishara651c74c2008-06-22 22:45:06 +0200112
113 return ret;
114}
115
116static struct pci_ops pcie_ops = {
117 .read = pcie_rd_conf,
118 .write = pcie_wr_conf,
119};
120
Nicolas Pitrea87182b2010-07-05 13:59:56 -0400121static void __init pcie0_ioresources_init(struct pcie_port *pp)
Saeed Bishara651c74c2008-06-22 22:45:06 +0200122{
Nicolas Pitrea87182b2010-07-05 13:59:56 -0400123 pp->base = (void __iomem *)PCIE_VIRT_BASE;
124 pp->irq = IRQ_KIRKWOOD_PCIE;
Saeed Bishara651c74c2008-06-22 22:45:06 +0200125
126 /*
127 * IORESOURCE_IO
128 */
Saeed Bisharaffd58bd2010-06-08 14:21:34 +0300129 pp->res[0].name = "PCIe 0 I/O Space";
Arnaud Patarde4ff1c32010-08-22 22:49:46 +0200130 pp->res[0].start = KIRKWOOD_PCIE_IO_BUS_BASE;
Saeed Bisharaffd58bd2010-06-08 14:21:34 +0300131 pp->res[0].end = pp->res[0].start + KIRKWOOD_PCIE_IO_SIZE - 1;
132 pp->res[0].flags = IORESOURCE_IO;
Saeed Bishara651c74c2008-06-22 22:45:06 +0200133
134 /*
135 * IORESOURCE_MEM
136 */
Saeed Bisharaffd58bd2010-06-08 14:21:34 +0300137 pp->res[1].name = "PCIe 0 MEM";
138 pp->res[1].start = KIRKWOOD_PCIE_MEM_PHYS_BASE;
139 pp->res[1].end = pp->res[1].start + KIRKWOOD_PCIE_MEM_SIZE - 1;
140 pp->res[1].flags = IORESOURCE_MEM;
Saeed Bisharaffd58bd2010-06-08 14:21:34 +0300141}
142
Nicolas Pitrea87182b2010-07-05 13:59:56 -0400143static void __init pcie1_ioresources_init(struct pcie_port *pp)
Saeed Bisharaffd58bd2010-06-08 14:21:34 +0300144{
Nicolas Pitrea87182b2010-07-05 13:59:56 -0400145 pp->base = (void __iomem *)PCIE1_VIRT_BASE;
146 pp->irq = IRQ_KIRKWOOD_PCIE1;
Saeed Bisharaffd58bd2010-06-08 14:21:34 +0300147
148 /*
149 * IORESOURCE_IO
150 */
151 pp->res[0].name = "PCIe 1 I/O Space";
Arnaud Patarde4ff1c32010-08-22 22:49:46 +0200152 pp->res[0].start = KIRKWOOD_PCIE1_IO_BUS_BASE;
Saeed Bisharaffd58bd2010-06-08 14:21:34 +0300153 pp->res[0].end = pp->res[0].start + KIRKWOOD_PCIE1_IO_SIZE - 1;
154 pp->res[0].flags = IORESOURCE_IO;
Saeed Bisharaffd58bd2010-06-08 14:21:34 +0300155
156 /*
157 * IORESOURCE_MEM
158 */
159 pp->res[1].name = "PCIe 1 MEM";
160 pp->res[1].start = KIRKWOOD_PCIE1_MEM_PHYS_BASE;
161 pp->res[1].end = pp->res[1].start + KIRKWOOD_PCIE1_MEM_SIZE - 1;
162 pp->res[1].flags = IORESOURCE_MEM;
Saeed Bisharaffd58bd2010-06-08 14:21:34 +0300163}
164
165static int __init kirkwood_pcie_setup(int nr, struct pci_sys_data *sys)
166{
167 extern unsigned int kirkwood_clk_ctrl;
168 struct pcie_port *pp;
169 int index;
170
171 if (nr >= num_pcie_ports)
172 return 0;
173
174 index = pcie_port_map[nr];
175 printk(KERN_INFO "PCI: bus%d uses PCIe port %d\n", sys->busnr, index);
176
177 pp = kzalloc(sizeof(*pp), GFP_KERNEL);
178 if (!pp)
179 panic("PCIe: failed to allocate pcie_port data");
180 sys->private_data = pp;
181 pp->root_bus_nr = sys->busnr;
182 spin_lock_init(&pp->conf_lock);
183
184 switch (index) {
185 case 0:
Saeed Bisharaffd58bd2010-06-08 14:21:34 +0300186 kirkwood_clk_ctrl |= CGC_PEX0;
Nicolas Pitrea87182b2010-07-05 13:59:56 -0400187 pcie0_ioresources_init(pp);
Saeed Bisharaffd58bd2010-06-08 14:21:34 +0300188 break;
189 case 1:
Saeed Bisharaffd58bd2010-06-08 14:21:34 +0300190 kirkwood_clk_ctrl |= CGC_PEX1;
Nicolas Pitrea87182b2010-07-05 13:59:56 -0400191 pcie1_ioresources_init(pp);
Saeed Bisharaffd58bd2010-06-08 14:21:34 +0300192 break;
193 default:
Nicolas Pitrea87182b2010-07-05 13:59:56 -0400194 panic("PCIe setup: invalid controller %d", index);
Saeed Bisharaffd58bd2010-06-08 14:21:34 +0300195 }
196
Nicolas Pitrea87182b2010-07-05 13:59:56 -0400197 if (request_resource(&ioport_resource, &pp->res[0]))
198 panic("Request PCIe%d IO resource failed\n", index);
199 if (request_resource(&iomem_resource, &pp->res[1]))
200 panic("Request PCIe%d Memory resource failed\n", index);
201
202 sys->resource[0] = &pp->res[0];
203 sys->resource[1] = &pp->res[1];
204 sys->resource[2] = NULL;
205 sys->io_offset = 0;
206
Saeed Bisharaffd58bd2010-06-08 14:21:34 +0300207 /*
208 * Generic PCIe unit setup.
209 */
210 orion_pcie_set_local_bus_nr(pp->base, sys->busnr);
211
Andrew Lunn45173d52011-12-07 21:48:06 +0100212 orion_pcie_setup(pp->base, &orion_mbus_dram_info);
Rabeeh Khourye8b2b7b2009-03-22 17:30:32 +0200213
Saeed Bishara651c74c2008-06-22 22:45:06 +0200214 return 1;
215}
216
217static void __devinit rc_pci_fixup(struct pci_dev *dev)
218{
219 /*
220 * Prevent enumeration of root complex.
221 */
222 if (dev->bus->parent == NULL && dev->devfn == 0) {
223 int i;
224
225 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
226 dev->resource[i].start = 0;
227 dev->resource[i].end = 0;
228 dev->resource[i].flags = 0;
229 }
230 }
231}
232DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_MARVELL, PCI_ANY_ID, rc_pci_fixup);
233
234static struct pci_bus __init *
235kirkwood_pcie_scan_bus(int nr, struct pci_sys_data *sys)
236{
237 struct pci_bus *bus;
238
Saeed Bisharaffd58bd2010-06-08 14:21:34 +0300239 if (nr < num_pcie_ports) {
Saeed Bishara651c74c2008-06-22 22:45:06 +0200240 bus = pci_scan_bus(sys->busnr, &pcie_ops, sys);
241 } else {
242 bus = NULL;
243 BUG();
244 }
245
246 return bus;
247}
248
Ralf Baechled5341942011-06-10 15:30:21 +0100249static int __init kirkwood_pcie_map_irq(const struct pci_dev *dev, u8 slot,
250 u8 pin)
Saeed Bishara651c74c2008-06-22 22:45:06 +0200251{
Saeed Bisharaffd58bd2010-06-08 14:21:34 +0300252 struct pcie_port *pp = bus_to_port(dev->bus);
253
254 return pp->irq;
Saeed Bishara651c74c2008-06-22 22:45:06 +0200255}
256
257static struct hw_pci kirkwood_pci __initdata = {
Saeed Bishara651c74c2008-06-22 22:45:06 +0200258 .swizzle = pci_std_swizzle,
259 .setup = kirkwood_pcie_setup,
260 .scan = kirkwood_pcie_scan_bus,
261 .map_irq = kirkwood_pcie_map_irq,
262};
263
Saeed Bisharaffd58bd2010-06-08 14:21:34 +0300264static void __init add_pcie_port(int index, unsigned long base)
Saeed Bishara651c74c2008-06-22 22:45:06 +0200265{
Saeed Bisharaffd58bd2010-06-08 14:21:34 +0300266 printk(KERN_INFO "Kirkwood PCIe port %d: ", index);
267
268 if (orion_pcie_link_up((void __iomem *)base)) {
269 printk(KERN_INFO "link up\n");
270 pcie_port_map[num_pcie_ports++] = index;
271 } else
272 printk(KERN_INFO "link down, ignoring\n");
273}
274
275void __init kirkwood_pcie_init(unsigned int portmask)
276{
Rob Herringcc22b4c2011-06-28 21:22:40 -0500277 vga_base = KIRKWOOD_PCIE_MEM_PHYS_BASE;
278
Saeed Bisharaffd58bd2010-06-08 14:21:34 +0300279 if (portmask & KW_PCIE0)
280 add_pcie_port(0, PCIE_VIRT_BASE);
281
282 if (portmask & KW_PCIE1)
283 add_pcie_port(1, PCIE1_VIRT_BASE);
284
285 kirkwood_pci.nr_controllers = num_pcie_ports;
Saeed Bishara651c74c2008-06-22 22:45:06 +0200286 pci_common_init(&kirkwood_pci);
287}