blob: dc26a654c496d1d06797af927531f6030a24e146 [file] [log] [blame]
Stanislav Samsonov794d15b2008-06-22 22:45:10 +02001/*
2 * arch/arm/mach-mv78xx0/pcie.c
3 *
4 * PCIe functions for Marvell MV78xx0 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>
Thomas Petazzoni95b80e02013-03-21 17:59:19 +010013#include <linux/mbus.h>
Rob Herringcc22b4c2011-06-28 21:22:40 -050014#include <video/vga.h>
Nicolas Pitreba0cda62009-01-07 04:58:23 +010015#include <asm/irq.h>
Stanislav Samsonov794d15b2008-06-22 22:45:10 +020016#include <asm/mach/pci.h>
Lennert Buytenhek6f088f12008-08-09 13:44:58 +020017#include <plat/pcie.h>
Rob Herring0b9b18e2012-07-09 23:26:58 -050018#include <mach/mv78xx0.h>
Stanislav Samsonov794d15b2008-06-22 22:45:10 +020019#include "common.h"
20
21struct pcie_port {
22 u8 maj;
23 u8 min;
24 u8 root_bus_nr;
25 void __iomem *base;
26 spinlock_t conf_lock;
Stanislav Samsonov794d15b2008-06-22 22:45:10 +020027 char mem_space_name[16];
Rob Herring0b9b18e2012-07-09 23:26:58 -050028 struct resource res;
Stanislav Samsonov794d15b2008-06-22 22:45:10 +020029};
30
31static struct pcie_port pcie_port[8];
32static int num_pcie_ports;
33static struct resource pcie_io_space;
Stanislav Samsonov794d15b2008-06-22 22:45:10 +020034
Lennert Buytenhekcfdeb632009-02-20 02:31:35 +010035void __init mv78xx0_pcie_id(u32 *dev, u32 *rev)
36{
Thomas Petazzoni383b9962012-09-11 14:27:20 +020037 *dev = orion_pcie_dev_id(PCIE00_VIRT_BASE);
38 *rev = orion_pcie_rev(PCIE00_VIRT_BASE);
Lennert Buytenhekcfdeb632009-02-20 02:31:35 +010039}
40
Rob Herring0b9b18e2012-07-09 23:26:58 -050041u32 pcie_port_size[8] = {
42 0,
43 0x30000000,
44 0x10000000,
45 0x10000000,
46 0x08000000,
47 0x08000000,
48 0x08000000,
49 0x04000000,
50};
51
Stanislav Samsonov794d15b2008-06-22 22:45:10 +020052static void __init mv78xx0_pcie_preinit(void)
53{
54 int i;
55 u32 size_each;
56 u32 start;
Stanislav Samsonov794d15b2008-06-22 22:45:10 +020057
58 pcie_io_space.name = "PCIe I/O Space";
59 pcie_io_space.start = MV78XX0_PCIE_IO_PHYS_BASE(0);
60 pcie_io_space.end =
61 MV78XX0_PCIE_IO_PHYS_BASE(0) + MV78XX0_PCIE_IO_SIZE * 8 - 1;
Rob Herring0b9b18e2012-07-09 23:26:58 -050062 pcie_io_space.flags = IORESOURCE_MEM;
Stanislav Samsonov794d15b2008-06-22 22:45:10 +020063 if (request_resource(&iomem_resource, &pcie_io_space))
64 panic("can't allocate PCIe I/O space");
65
Rob Herring0b9b18e2012-07-09 23:26:58 -050066 if (num_pcie_ports > 7)
Stanislav Samsonov794d15b2008-06-22 22:45:10 +020067 panic("invalid number of PCIe ports");
Rob Herring0b9b18e2012-07-09 23:26:58 -050068
69 size_each = pcie_port_size[num_pcie_ports];
Stanislav Samsonov794d15b2008-06-22 22:45:10 +020070
71 start = MV78XX0_PCIE_MEM_PHYS_BASE;
72 for (i = 0; i < num_pcie_ports; i++) {
73 struct pcie_port *pp = pcie_port + i;
Thomas Petazzoni95b80e02013-03-21 17:59:19 +010074 char winname[MVEBU_MBUS_MAX_WINNAME_SZ];
Stanislav Samsonov794d15b2008-06-22 22:45:10 +020075
Rob Herring0b9b18e2012-07-09 23:26:58 -050076 snprintf(pp->mem_space_name, sizeof(pp->mem_space_name),
77 "PCIe %d.%d MEM", pp->maj, pp->min);
78 pp->mem_space_name[sizeof(pp->mem_space_name) - 1] = 0;
79 pp->res.name = pp->mem_space_name;
80 pp->res.flags = IORESOURCE_MEM;
81 pp->res.start = start;
82 pp->res.end = start + size_each - 1;
Stanislav Samsonov794d15b2008-06-22 22:45:10 +020083 start += size_each;
Stanislav Samsonov794d15b2008-06-22 22:45:10 +020084
Rob Herring0b9b18e2012-07-09 23:26:58 -050085 if (request_resource(&iomem_resource, &pp->res))
Stanislav Samsonov794d15b2008-06-22 22:45:10 +020086 panic("can't allocate PCIe MEM sub-space");
Stanislav Samsonov794d15b2008-06-22 22:45:10 +020087
Thomas Petazzoni95b80e02013-03-21 17:59:19 +010088 snprintf(winname, sizeof(winname), "pcie%d.%d",
89 pp->maj, pp->min);
Rob Herring0b9b18e2012-07-09 23:26:58 -050090
Thomas Petazzoni95b80e02013-03-21 17:59:19 +010091 mvebu_mbus_add_window_remap_flags(winname,
92 pp->res.start,
93 resource_size(&pp->res),
94 MVEBU_MBUS_NO_REMAP,
95 MVEBU_MBUS_PCI_MEM);
96 mvebu_mbus_add_window_remap_flags(winname,
97 i * SZ_64K, SZ_64K,
98 0, MVEBU_MBUS_PCI_IO);
Stanislav Samsonov794d15b2008-06-22 22:45:10 +020099 }
100}
101
102static int __init mv78xx0_pcie_setup(int nr, struct pci_sys_data *sys)
103{
104 struct pcie_port *pp;
105
106 if (nr >= num_pcie_ports)
107 return 0;
108
109 pp = &pcie_port[nr];
Russell King43ba9902012-03-10 13:31:34 +0000110 sys->private_data = pp;
Stanislav Samsonov794d15b2008-06-22 22:45:10 +0200111 pp->root_bus_nr = sys->busnr;
112
113 /*
114 * Generic PCIe unit setup.
115 */
116 orion_pcie_set_local_bus_nr(pp->base, sys->busnr);
Andrew Lunn63a93322011-12-07 21:48:07 +0100117 orion_pcie_setup(pp->base);
Stanislav Samsonov794d15b2008-06-22 22:45:10 +0200118
Rob Herring0b9b18e2012-07-09 23:26:58 -0500119 pci_ioremap_io(nr * SZ_64K, MV78XX0_PCIE_IO_PHYS_BASE(nr));
120
121 pci_add_resource_offset(&sys->resources, &pp->res, sys->mem_offset);
Stanislav Samsonov794d15b2008-06-22 22:45:10 +0200122
123 return 1;
124}
125
Stanislav Samsonov794d15b2008-06-22 22:45:10 +0200126static int pcie_valid_config(struct pcie_port *pp, int bus, int dev)
127{
128 /*
129 * Don't go out when trying to access nonexisting devices
130 * on the local bus.
131 */
132 if (bus == pp->root_bus_nr && dev > 1)
133 return 0;
134
135 return 1;
136}
137
138static int pcie_rd_conf(struct pci_bus *bus, u32 devfn, int where,
139 int size, u32 *val)
140{
Russell King43ba9902012-03-10 13:31:34 +0000141 struct pci_sys_data *sys = bus->sysdata;
142 struct pcie_port *pp = sys->private_data;
Stanislav Samsonov794d15b2008-06-22 22:45:10 +0200143 unsigned long flags;
144 int ret;
145
146 if (pcie_valid_config(pp, bus->number, PCI_SLOT(devfn)) == 0) {
147 *val = 0xffffffff;
148 return PCIBIOS_DEVICE_NOT_FOUND;
149 }
150
151 spin_lock_irqsave(&pp->conf_lock, flags);
152 ret = orion_pcie_rd_conf(pp->base, bus, devfn, where, size, val);
153 spin_unlock_irqrestore(&pp->conf_lock, flags);
154
155 return ret;
156}
157
158static int pcie_wr_conf(struct pci_bus *bus, u32 devfn,
159 int where, int size, u32 val)
160{
Russell King43ba9902012-03-10 13:31:34 +0000161 struct pci_sys_data *sys = bus->sysdata;
162 struct pcie_port *pp = sys->private_data;
Stanislav Samsonov794d15b2008-06-22 22:45:10 +0200163 unsigned long flags;
164 int ret;
165
166 if (pcie_valid_config(pp, bus->number, PCI_SLOT(devfn)) == 0)
167 return PCIBIOS_DEVICE_NOT_FOUND;
168
169 spin_lock_irqsave(&pp->conf_lock, flags);
170 ret = orion_pcie_wr_conf(pp->base, bus, devfn, where, size, val);
171 spin_unlock_irqrestore(&pp->conf_lock, flags);
172
173 return ret;
174}
175
176static struct pci_ops pcie_ops = {
177 .read = pcie_rd_conf,
178 .write = pcie_wr_conf,
179};
180
Greg Kroah-Hartman351a1022012-12-21 14:02:24 -0800181static void rc_pci_fixup(struct pci_dev *dev)
Stanislav Samsonov794d15b2008-06-22 22:45:10 +0200182{
183 /*
184 * Prevent enumeration of root complex.
185 */
186 if (dev->bus->parent == NULL && dev->devfn == 0) {
187 int i;
188
189 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
190 dev->resource[i].start = 0;
191 dev->resource[i].end = 0;
192 dev->resource[i].flags = 0;
193 }
194 }
195}
196DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_MARVELL, PCI_ANY_ID, rc_pci_fixup);
197
198static struct pci_bus __init *
199mv78xx0_pcie_scan_bus(int nr, struct pci_sys_data *sys)
200{
201 struct pci_bus *bus;
202
203 if (nr < num_pcie_ports) {
Bjorn Helgaas37d15902011-10-28 16:26:16 -0600204 bus = pci_scan_root_bus(NULL, sys->busnr, &pcie_ops, sys,
205 &sys->resources);
Stanislav Samsonov794d15b2008-06-22 22:45:10 +0200206 } else {
207 bus = NULL;
208 BUG();
209 }
210
211 return bus;
212}
213
Ralf Baechled5341942011-06-10 15:30:21 +0100214static int __init mv78xx0_pcie_map_irq(const struct pci_dev *dev, u8 slot,
215 u8 pin)
Stanislav Samsonov794d15b2008-06-22 22:45:10 +0200216{
Russell King43ba9902012-03-10 13:31:34 +0000217 struct pci_sys_data *sys = dev->bus->sysdata;
218 struct pcie_port *pp = sys->private_data;
Stanislav Samsonov794d15b2008-06-22 22:45:10 +0200219
220 return IRQ_MV78XX0_PCIE_00 + (pp->maj << 2) + pp->min;
221}
222
223static struct hw_pci mv78xx0_pci __initdata = {
224 .nr_controllers = 8,
225 .preinit = mv78xx0_pcie_preinit,
Stanislav Samsonov794d15b2008-06-22 22:45:10 +0200226 .setup = mv78xx0_pcie_setup,
227 .scan = mv78xx0_pcie_scan_bus,
228 .map_irq = mv78xx0_pcie_map_irq,
229};
230
Thomas Petazzoni383b9962012-09-11 14:27:20 +0200231static void __init add_pcie_port(int maj, int min, void __iomem *base)
Stanislav Samsonov794d15b2008-06-22 22:45:10 +0200232{
233 printk(KERN_INFO "MV78xx0 PCIe port %d.%d: ", maj, min);
234
Thomas Petazzoni383b9962012-09-11 14:27:20 +0200235 if (orion_pcie_link_up(base)) {
Stanislav Samsonov794d15b2008-06-22 22:45:10 +0200236 struct pcie_port *pp = &pcie_port[num_pcie_ports++];
237
238 printk("link up\n");
239
240 pp->maj = maj;
241 pp->min = min;
242 pp->root_bus_nr = -1;
Thomas Petazzoni383b9962012-09-11 14:27:20 +0200243 pp->base = base;
Stanislav Samsonov794d15b2008-06-22 22:45:10 +0200244 spin_lock_init(&pp->conf_lock);
Rob Herring0b9b18e2012-07-09 23:26:58 -0500245 memset(&pp->res, 0, sizeof(pp->res));
Stanislav Samsonov794d15b2008-06-22 22:45:10 +0200246 } else {
247 printk("link down, ignoring\n");
248 }
249}
250
251void __init mv78xx0_pcie_init(int init_port0, int init_port1)
252{
Rob Herringcc22b4c2011-06-28 21:22:40 -0500253 vga_base = MV78XX0_PCIE_MEM_PHYS_BASE;
254
Stanislav Samsonov794d15b2008-06-22 22:45:10 +0200255 if (init_port0) {
256 add_pcie_port(0, 0, PCIE00_VIRT_BASE);
Thomas Petazzoni383b9962012-09-11 14:27:20 +0200257 if (!orion_pcie_x4_mode(PCIE00_VIRT_BASE)) {
Stanislav Samsonov794d15b2008-06-22 22:45:10 +0200258 add_pcie_port(0, 1, PCIE01_VIRT_BASE);
259 add_pcie_port(0, 2, PCIE02_VIRT_BASE);
260 add_pcie_port(0, 3, PCIE03_VIRT_BASE);
261 }
262 }
263
264 if (init_port1) {
265 add_pcie_port(1, 0, PCIE10_VIRT_BASE);
266 if (!orion_pcie_x4_mode((void __iomem *)PCIE10_VIRT_BASE)) {
267 add_pcie_port(1, 1, PCIE11_VIRT_BASE);
268 add_pcie_port(1, 2, PCIE12_VIRT_BASE);
269 add_pcie_port(1, 3, PCIE13_VIRT_BASE);
270 }
271 }
272
273 pci_common_init(&mv78xx0_pci);
274}