blob: de373176ee670f7c435d9cd9849cb50b857f49f4 [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>
Rob Herringcc22b4c2011-06-28 21:22:40 -050014#include <video/vga.h>
Nicolas Pitre6e5c11a2009-01-07 04:47:02 +010015#include <asm/irq.h>
Saeed Bishara651c74c2008-06-22 22:45:06 +020016#include <asm/mach/pci.h>
Lennert Buytenhek6f088f12008-08-09 13:44:58 +020017#include <plat/pcie.h>
Rabeeh Khourye8b2b7b2009-03-22 17:30:32 +020018#include <mach/bridge-regs.h>
Andrew Lunn45173d52011-12-07 21:48:06 +010019#include <plat/addr-map.h>
Saeed Bishara651c74c2008-06-22 22:45:06 +020020#include "common.h"
21
Eric Cooper0e0cdd32011-02-02 17:16:10 -050022void kirkwood_enable_pcie(void)
23{
24 u32 curr = readl(CLOCK_GATING_CTRL);
25 if (!(curr & CGC_PEX0))
26 writel(curr | CGC_PEX0, CLOCK_GATING_CTRL);
27}
28
Ronen Shitritb2b3dc22008-09-15 10:40:35 +030029void __init kirkwood_pcie_id(u32 *dev, u32 *rev)
30{
Eric Cooper0e0cdd32011-02-02 17:16:10 -050031 kirkwood_enable_pcie();
Saeed Bisharaffd58bd2010-06-08 14:21:34 +030032 *dev = orion_pcie_dev_id((void __iomem *)PCIE_VIRT_BASE);
33 *rev = orion_pcie_rev((void __iomem *)PCIE_VIRT_BASE);
Ronen Shitritb2b3dc22008-09-15 10:40:35 +030034}
35
Saeed Bisharaffd58bd2010-06-08 14:21:34 +030036struct pcie_port {
37 u8 root_bus_nr;
38 void __iomem *base;
39 spinlock_t conf_lock;
40 int irq;
41 struct resource res[2];
42};
43
44static int pcie_port_map[2];
45static int num_pcie_ports;
46
Saeed Bisharaffd58bd2010-06-08 14:21:34 +030047static int pcie_valid_config(struct pcie_port *pp, int bus, int dev)
Saeed Bishara651c74c2008-06-22 22:45:06 +020048{
49 /*
50 * Don't go out when trying to access --
51 * 1. nonexisting device on local bus
52 * 2. where there's no device connected (no link)
53 */
Saeed Bisharaffd58bd2010-06-08 14:21:34 +030054 if (bus == pp->root_bus_nr && dev == 0)
Saeed Bishara651c74c2008-06-22 22:45:06 +020055 return 1;
56
Saeed Bisharaffd58bd2010-06-08 14:21:34 +030057 if (!orion_pcie_link_up(pp->base))
Saeed Bishara651c74c2008-06-22 22:45:06 +020058 return 0;
59
Saeed Bisharaffd58bd2010-06-08 14:21:34 +030060 if (bus == pp->root_bus_nr && dev != 1)
Saeed Bishara651c74c2008-06-22 22:45:06 +020061 return 0;
62
63 return 1;
64}
65
66
67/*
68 * PCIe config cycles are done by programming the PCIE_CONF_ADDR register
69 * and then reading the PCIE_CONF_DATA register. Need to make sure these
70 * transactions are atomic.
71 */
Saeed Bishara651c74c2008-06-22 22:45:06 +020072
73static int pcie_rd_conf(struct pci_bus *bus, u32 devfn, int where,
74 int size, u32 *val)
75{
Russell King43ba9902012-03-10 13:31:34 +000076 struct pci_sys_data *sys = bus->sysdata;
77 struct pcie_port *pp = sys->private_data;
Saeed Bishara651c74c2008-06-22 22:45:06 +020078 unsigned long flags;
79 int ret;
80
Saeed Bisharaffd58bd2010-06-08 14:21:34 +030081 if (pcie_valid_config(pp, bus->number, PCI_SLOT(devfn)) == 0) {
Saeed Bishara651c74c2008-06-22 22:45:06 +020082 *val = 0xffffffff;
83 return PCIBIOS_DEVICE_NOT_FOUND;
84 }
85
Saeed Bisharaffd58bd2010-06-08 14:21:34 +030086 spin_lock_irqsave(&pp->conf_lock, flags);
87 ret = orion_pcie_rd_conf(pp->base, bus, devfn, where, size, val);
88 spin_unlock_irqrestore(&pp->conf_lock, flags);
Saeed Bishara651c74c2008-06-22 22:45:06 +020089
90 return ret;
91}
92
93static int pcie_wr_conf(struct pci_bus *bus, u32 devfn,
94 int where, int size, u32 val)
95{
Russell King43ba9902012-03-10 13:31:34 +000096 struct pci_sys_data *sys = bus->sysdata;
97 struct pcie_port *pp = sys->private_data;
Saeed Bishara651c74c2008-06-22 22:45:06 +020098 unsigned long flags;
99 int ret;
100
Saeed Bisharaffd58bd2010-06-08 14:21:34 +0300101 if (pcie_valid_config(pp, bus->number, PCI_SLOT(devfn)) == 0)
Saeed Bishara651c74c2008-06-22 22:45:06 +0200102 return PCIBIOS_DEVICE_NOT_FOUND;
103
Saeed Bisharaffd58bd2010-06-08 14:21:34 +0300104 spin_lock_irqsave(&pp->conf_lock, flags);
105 ret = orion_pcie_wr_conf(pp->base, bus, devfn, where, size, val);
106 spin_unlock_irqrestore(&pp->conf_lock, flags);
Saeed Bishara651c74c2008-06-22 22:45:06 +0200107
108 return ret;
109}
110
111static struct pci_ops pcie_ops = {
112 .read = pcie_rd_conf,
113 .write = pcie_wr_conf,
114};
115
Nicolas Pitrea87182b2010-07-05 13:59:56 -0400116static void __init pcie0_ioresources_init(struct pcie_port *pp)
Saeed Bishara651c74c2008-06-22 22:45:06 +0200117{
Nicolas Pitrea87182b2010-07-05 13:59:56 -0400118 pp->base = (void __iomem *)PCIE_VIRT_BASE;
119 pp->irq = IRQ_KIRKWOOD_PCIE;
Saeed Bishara651c74c2008-06-22 22:45:06 +0200120
121 /*
122 * IORESOURCE_IO
123 */
Saeed Bisharaffd58bd2010-06-08 14:21:34 +0300124 pp->res[0].name = "PCIe 0 I/O Space";
Arnaud Patarde4ff1c32010-08-22 22:49:46 +0200125 pp->res[0].start = KIRKWOOD_PCIE_IO_BUS_BASE;
Saeed Bisharaffd58bd2010-06-08 14:21:34 +0300126 pp->res[0].end = pp->res[0].start + KIRKWOOD_PCIE_IO_SIZE - 1;
127 pp->res[0].flags = IORESOURCE_IO;
Saeed Bishara651c74c2008-06-22 22:45:06 +0200128
129 /*
130 * IORESOURCE_MEM
131 */
Saeed Bisharaffd58bd2010-06-08 14:21:34 +0300132 pp->res[1].name = "PCIe 0 MEM";
133 pp->res[1].start = KIRKWOOD_PCIE_MEM_PHYS_BASE;
134 pp->res[1].end = pp->res[1].start + KIRKWOOD_PCIE_MEM_SIZE - 1;
135 pp->res[1].flags = IORESOURCE_MEM;
Saeed Bisharaffd58bd2010-06-08 14:21:34 +0300136}
137
Nicolas Pitrea87182b2010-07-05 13:59:56 -0400138static void __init pcie1_ioresources_init(struct pcie_port *pp)
Saeed Bisharaffd58bd2010-06-08 14:21:34 +0300139{
Nicolas Pitrea87182b2010-07-05 13:59:56 -0400140 pp->base = (void __iomem *)PCIE1_VIRT_BASE;
141 pp->irq = IRQ_KIRKWOOD_PCIE1;
Saeed Bisharaffd58bd2010-06-08 14:21:34 +0300142
143 /*
144 * IORESOURCE_IO
145 */
146 pp->res[0].name = "PCIe 1 I/O Space";
Arnaud Patarde4ff1c32010-08-22 22:49:46 +0200147 pp->res[0].start = KIRKWOOD_PCIE1_IO_BUS_BASE;
Saeed Bisharaffd58bd2010-06-08 14:21:34 +0300148 pp->res[0].end = pp->res[0].start + KIRKWOOD_PCIE1_IO_SIZE - 1;
149 pp->res[0].flags = IORESOURCE_IO;
Saeed Bisharaffd58bd2010-06-08 14:21:34 +0300150
151 /*
152 * IORESOURCE_MEM
153 */
154 pp->res[1].name = "PCIe 1 MEM";
155 pp->res[1].start = KIRKWOOD_PCIE1_MEM_PHYS_BASE;
156 pp->res[1].end = pp->res[1].start + KIRKWOOD_PCIE1_MEM_SIZE - 1;
157 pp->res[1].flags = IORESOURCE_MEM;
Saeed Bisharaffd58bd2010-06-08 14:21:34 +0300158}
159
160static int __init kirkwood_pcie_setup(int nr, struct pci_sys_data *sys)
161{
162 extern unsigned int kirkwood_clk_ctrl;
163 struct pcie_port *pp;
164 int index;
165
166 if (nr >= num_pcie_ports)
167 return 0;
168
169 index = pcie_port_map[nr];
170 printk(KERN_INFO "PCI: bus%d uses PCIe port %d\n", sys->busnr, index);
171
172 pp = kzalloc(sizeof(*pp), GFP_KERNEL);
173 if (!pp)
174 panic("PCIe: failed to allocate pcie_port data");
175 sys->private_data = pp;
176 pp->root_bus_nr = sys->busnr;
177 spin_lock_init(&pp->conf_lock);
178
179 switch (index) {
180 case 0:
Saeed Bisharaffd58bd2010-06-08 14:21:34 +0300181 kirkwood_clk_ctrl |= CGC_PEX0;
Nicolas Pitrea87182b2010-07-05 13:59:56 -0400182 pcie0_ioresources_init(pp);
Saeed Bisharaffd58bd2010-06-08 14:21:34 +0300183 break;
184 case 1:
Saeed Bisharaffd58bd2010-06-08 14:21:34 +0300185 kirkwood_clk_ctrl |= CGC_PEX1;
Nicolas Pitrea87182b2010-07-05 13:59:56 -0400186 pcie1_ioresources_init(pp);
Saeed Bisharaffd58bd2010-06-08 14:21:34 +0300187 break;
188 default:
Nicolas Pitrea87182b2010-07-05 13:59:56 -0400189 panic("PCIe setup: invalid controller %d", index);
Saeed Bisharaffd58bd2010-06-08 14:21:34 +0300190 }
191
Nicolas Pitrea87182b2010-07-05 13:59:56 -0400192 if (request_resource(&ioport_resource, &pp->res[0]))
193 panic("Request PCIe%d IO resource failed\n", index);
194 if (request_resource(&iomem_resource, &pp->res[1]))
195 panic("Request PCIe%d Memory resource failed\n", index);
196
Nicolas Pitrea87182b2010-07-05 13:59:56 -0400197 sys->io_offset = 0;
Bjorn Helgaas9f786d032012-02-23 20:19:01 -0700198 pci_add_resource_offset(&sys->resources, &pp->res[0], sys->io_offset);
199 pci_add_resource_offset(&sys->resources, &pp->res[1], sys->mem_offset);
Nicolas Pitrea87182b2010-07-05 13:59:56 -0400200
Saeed Bisharaffd58bd2010-06-08 14:21:34 +0300201 /*
202 * Generic PCIe unit setup.
203 */
204 orion_pcie_set_local_bus_nr(pp->base, sys->busnr);
205
Andrew Lunn63a93322011-12-07 21:48:07 +0100206 orion_pcie_setup(pp->base);
Rabeeh Khourye8b2b7b2009-03-22 17:30:32 +0200207
Saeed Bishara651c74c2008-06-22 22:45:06 +0200208 return 1;
209}
210
211static void __devinit rc_pci_fixup(struct pci_dev *dev)
212{
213 /*
214 * Prevent enumeration of root complex.
215 */
216 if (dev->bus->parent == NULL && dev->devfn == 0) {
217 int i;
218
219 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
220 dev->resource[i].start = 0;
221 dev->resource[i].end = 0;
222 dev->resource[i].flags = 0;
223 }
224 }
225}
226DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_MARVELL, PCI_ANY_ID, rc_pci_fixup);
227
228static struct pci_bus __init *
229kirkwood_pcie_scan_bus(int nr, struct pci_sys_data *sys)
230{
231 struct pci_bus *bus;
232
Saeed Bisharaffd58bd2010-06-08 14:21:34 +0300233 if (nr < num_pcie_ports) {
Bjorn Helgaas37d15902011-10-28 16:26:16 -0600234 bus = pci_scan_root_bus(NULL, sys->busnr, &pcie_ops, sys,
235 &sys->resources);
Saeed Bishara651c74c2008-06-22 22:45:06 +0200236 } else {
237 bus = NULL;
238 BUG();
239 }
240
241 return bus;
242}
243
Ralf Baechled5341942011-06-10 15:30:21 +0100244static int __init kirkwood_pcie_map_irq(const struct pci_dev *dev, u8 slot,
245 u8 pin)
Saeed Bishara651c74c2008-06-22 22:45:06 +0200246{
Russell King43ba9902012-03-10 13:31:34 +0000247 struct pci_sys_data *sys = dev->sysdata;
248 struct pcie_port *pp = sys->private_data;
Saeed Bisharaffd58bd2010-06-08 14:21:34 +0300249
250 return pp->irq;
Saeed Bishara651c74c2008-06-22 22:45:06 +0200251}
252
253static struct hw_pci kirkwood_pci __initdata = {
Saeed Bishara651c74c2008-06-22 22:45:06 +0200254 .setup = kirkwood_pcie_setup,
255 .scan = kirkwood_pcie_scan_bus,
256 .map_irq = kirkwood_pcie_map_irq,
257};
258
Saeed Bisharaffd58bd2010-06-08 14:21:34 +0300259static void __init add_pcie_port(int index, unsigned long base)
Saeed Bishara651c74c2008-06-22 22:45:06 +0200260{
Saeed Bisharaffd58bd2010-06-08 14:21:34 +0300261 printk(KERN_INFO "Kirkwood PCIe port %d: ", index);
262
263 if (orion_pcie_link_up((void __iomem *)base)) {
264 printk(KERN_INFO "link up\n");
265 pcie_port_map[num_pcie_ports++] = index;
266 } else
267 printk(KERN_INFO "link down, ignoring\n");
268}
269
270void __init kirkwood_pcie_init(unsigned int portmask)
271{
Rob Herringcc22b4c2011-06-28 21:22:40 -0500272 vga_base = KIRKWOOD_PCIE_MEM_PHYS_BASE;
273
Saeed Bisharaffd58bd2010-06-08 14:21:34 +0300274 if (portmask & KW_PCIE0)
275 add_pcie_port(0, PCIE_VIRT_BASE);
276
277 if (portmask & KW_PCIE1)
278 add_pcie_port(1, PCIE1_VIRT_BASE);
279
280 kirkwood_pci.nr_controllers = num_pcie_ports;
Saeed Bishara651c74c2008-06-22 22:45:06 +0200281 pci_common_init(&kirkwood_pci);
282}