Stanislav Samsonov | 794d15b | 2008-06-22 22:45:10 +0200 | [diff] [blame] | 1 | /* |
| 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 Petazzoni | 95b80e0 | 2013-03-21 17:59:19 +0100 | [diff] [blame] | 13 | #include <linux/mbus.h> |
Rob Herring | cc22b4c | 2011-06-28 21:22:40 -0500 | [diff] [blame] | 14 | #include <video/vga.h> |
Nicolas Pitre | ba0cda6 | 2009-01-07 04:58:23 +0100 | [diff] [blame] | 15 | #include <asm/irq.h> |
Stanislav Samsonov | 794d15b | 2008-06-22 22:45:10 +0200 | [diff] [blame] | 16 | #include <asm/mach/pci.h> |
Lennert Buytenhek | 6f088f1 | 2008-08-09 13:44:58 +0200 | [diff] [blame] | 17 | #include <plat/pcie.h> |
Rob Herring | 0b9b18e | 2012-07-09 23:26:58 -0500 | [diff] [blame] | 18 | #include <mach/mv78xx0.h> |
Stanislav Samsonov | 794d15b | 2008-06-22 22:45:10 +0200 | [diff] [blame] | 19 | #include "common.h" |
| 20 | |
Thomas Petazzoni | c5d0ecc | 2013-07-26 10:17:41 -0300 | [diff] [blame] | 21 | #define MV78XX0_MBUS_PCIE_MEM_TARGET(port, lane) ((port) ? 8 : 4) |
| 22 | #define MV78XX0_MBUS_PCIE_MEM_ATTR(port, lane) (0xf8 & ~(0x10 << (lane))) |
| 23 | #define MV78XX0_MBUS_PCIE_IO_TARGET(port, lane) ((port) ? 8 : 4) |
| 24 | #define MV78XX0_MBUS_PCIE_IO_ATTR(port, lane) (0xf0 & ~(0x10 << (lane))) |
| 25 | |
Stanislav Samsonov | 794d15b | 2008-06-22 22:45:10 +0200 | [diff] [blame] | 26 | struct pcie_port { |
| 27 | u8 maj; |
| 28 | u8 min; |
| 29 | u8 root_bus_nr; |
| 30 | void __iomem *base; |
| 31 | spinlock_t conf_lock; |
Stanislav Samsonov | 794d15b | 2008-06-22 22:45:10 +0200 | [diff] [blame] | 32 | char mem_space_name[16]; |
Rob Herring | 0b9b18e | 2012-07-09 23:26:58 -0500 | [diff] [blame] | 33 | struct resource res; |
Stanislav Samsonov | 794d15b | 2008-06-22 22:45:10 +0200 | [diff] [blame] | 34 | }; |
| 35 | |
| 36 | static struct pcie_port pcie_port[8]; |
| 37 | static int num_pcie_ports; |
| 38 | static struct resource pcie_io_space; |
Stanislav Samsonov | 794d15b | 2008-06-22 22:45:10 +0200 | [diff] [blame] | 39 | |
Lennert Buytenhek | cfdeb63 | 2009-02-20 02:31:35 +0100 | [diff] [blame] | 40 | void __init mv78xx0_pcie_id(u32 *dev, u32 *rev) |
| 41 | { |
Thomas Petazzoni | 383b996 | 2012-09-11 14:27:20 +0200 | [diff] [blame] | 42 | *dev = orion_pcie_dev_id(PCIE00_VIRT_BASE); |
| 43 | *rev = orion_pcie_rev(PCIE00_VIRT_BASE); |
Lennert Buytenhek | cfdeb63 | 2009-02-20 02:31:35 +0100 | [diff] [blame] | 44 | } |
| 45 | |
Rob Herring | 0b9b18e | 2012-07-09 23:26:58 -0500 | [diff] [blame] | 46 | u32 pcie_port_size[8] = { |
| 47 | 0, |
| 48 | 0x30000000, |
| 49 | 0x10000000, |
| 50 | 0x10000000, |
| 51 | 0x08000000, |
| 52 | 0x08000000, |
| 53 | 0x08000000, |
| 54 | 0x04000000, |
| 55 | }; |
| 56 | |
Stanislav Samsonov | 794d15b | 2008-06-22 22:45:10 +0200 | [diff] [blame] | 57 | static void __init mv78xx0_pcie_preinit(void) |
| 58 | { |
| 59 | int i; |
| 60 | u32 size_each; |
| 61 | u32 start; |
Stanislav Samsonov | 794d15b | 2008-06-22 22:45:10 +0200 | [diff] [blame] | 62 | |
| 63 | pcie_io_space.name = "PCIe I/O Space"; |
| 64 | pcie_io_space.start = MV78XX0_PCIE_IO_PHYS_BASE(0); |
| 65 | pcie_io_space.end = |
| 66 | MV78XX0_PCIE_IO_PHYS_BASE(0) + MV78XX0_PCIE_IO_SIZE * 8 - 1; |
Rob Herring | 0b9b18e | 2012-07-09 23:26:58 -0500 | [diff] [blame] | 67 | pcie_io_space.flags = IORESOURCE_MEM; |
Stanislav Samsonov | 794d15b | 2008-06-22 22:45:10 +0200 | [diff] [blame] | 68 | if (request_resource(&iomem_resource, &pcie_io_space)) |
| 69 | panic("can't allocate PCIe I/O space"); |
| 70 | |
Rob Herring | 0b9b18e | 2012-07-09 23:26:58 -0500 | [diff] [blame] | 71 | if (num_pcie_ports > 7) |
Stanislav Samsonov | 794d15b | 2008-06-22 22:45:10 +0200 | [diff] [blame] | 72 | panic("invalid number of PCIe ports"); |
Rob Herring | 0b9b18e | 2012-07-09 23:26:58 -0500 | [diff] [blame] | 73 | |
| 74 | size_each = pcie_port_size[num_pcie_ports]; |
Stanislav Samsonov | 794d15b | 2008-06-22 22:45:10 +0200 | [diff] [blame] | 75 | |
| 76 | start = MV78XX0_PCIE_MEM_PHYS_BASE; |
| 77 | for (i = 0; i < num_pcie_ports; i++) { |
| 78 | struct pcie_port *pp = pcie_port + i; |
| 79 | |
Rob Herring | 0b9b18e | 2012-07-09 23:26:58 -0500 | [diff] [blame] | 80 | snprintf(pp->mem_space_name, sizeof(pp->mem_space_name), |
| 81 | "PCIe %d.%d MEM", pp->maj, pp->min); |
| 82 | pp->mem_space_name[sizeof(pp->mem_space_name) - 1] = 0; |
| 83 | pp->res.name = pp->mem_space_name; |
| 84 | pp->res.flags = IORESOURCE_MEM; |
| 85 | pp->res.start = start; |
| 86 | pp->res.end = start + size_each - 1; |
Stanislav Samsonov | 794d15b | 2008-06-22 22:45:10 +0200 | [diff] [blame] | 87 | start += size_each; |
Stanislav Samsonov | 794d15b | 2008-06-22 22:45:10 +0200 | [diff] [blame] | 88 | |
Rob Herring | 0b9b18e | 2012-07-09 23:26:58 -0500 | [diff] [blame] | 89 | if (request_resource(&iomem_resource, &pp->res)) |
Stanislav Samsonov | 794d15b | 2008-06-22 22:45:10 +0200 | [diff] [blame] | 90 | panic("can't allocate PCIe MEM sub-space"); |
Stanislav Samsonov | 794d15b | 2008-06-22 22:45:10 +0200 | [diff] [blame] | 91 | |
Thomas Petazzoni | c5d0ecc | 2013-07-26 10:17:41 -0300 | [diff] [blame] | 92 | mvebu_mbus_add_window_by_id(MV78XX0_MBUS_PCIE_MEM_TARGET(pp->maj, pp->min), |
| 93 | MV78XX0_MBUS_PCIE_MEM_ATTR(pp->maj, pp->min), |
| 94 | pp->res.start, resource_size(&pp->res)); |
| 95 | mvebu_mbus_add_window_remap_by_id(MV78XX0_MBUS_PCIE_IO_TARGET(pp->maj, pp->min), |
| 96 | MV78XX0_MBUS_PCIE_IO_ATTR(pp->maj, pp->min), |
| 97 | i * SZ_64K, SZ_64K, 0); |
Stanislav Samsonov | 794d15b | 2008-06-22 22:45:10 +0200 | [diff] [blame] | 98 | } |
| 99 | } |
| 100 | |
| 101 | static int __init mv78xx0_pcie_setup(int nr, struct pci_sys_data *sys) |
| 102 | { |
| 103 | struct pcie_port *pp; |
| 104 | |
| 105 | if (nr >= num_pcie_ports) |
| 106 | return 0; |
| 107 | |
| 108 | pp = &pcie_port[nr]; |
Russell King | 43ba990 | 2012-03-10 13:31:34 +0000 | [diff] [blame] | 109 | sys->private_data = pp; |
Stanislav Samsonov | 794d15b | 2008-06-22 22:45:10 +0200 | [diff] [blame] | 110 | pp->root_bus_nr = sys->busnr; |
| 111 | |
| 112 | /* |
| 113 | * Generic PCIe unit setup. |
| 114 | */ |
| 115 | orion_pcie_set_local_bus_nr(pp->base, sys->busnr); |
Andrew Lunn | 63a9332 | 2011-12-07 21:48:07 +0100 | [diff] [blame] | 116 | orion_pcie_setup(pp->base); |
Stanislav Samsonov | 794d15b | 2008-06-22 22:45:10 +0200 | [diff] [blame] | 117 | |
Rob Herring | 0b9b18e | 2012-07-09 23:26:58 -0500 | [diff] [blame] | 118 | pci_ioremap_io(nr * SZ_64K, MV78XX0_PCIE_IO_PHYS_BASE(nr)); |
| 119 | |
| 120 | pci_add_resource_offset(&sys->resources, &pp->res, sys->mem_offset); |
Stanislav Samsonov | 794d15b | 2008-06-22 22:45:10 +0200 | [diff] [blame] | 121 | |
| 122 | return 1; |
| 123 | } |
| 124 | |
Stanislav Samsonov | 794d15b | 2008-06-22 22:45:10 +0200 | [diff] [blame] | 125 | static int pcie_valid_config(struct pcie_port *pp, int bus, int dev) |
| 126 | { |
| 127 | /* |
| 128 | * Don't go out when trying to access nonexisting devices |
| 129 | * on the local bus. |
| 130 | */ |
| 131 | if (bus == pp->root_bus_nr && dev > 1) |
| 132 | return 0; |
| 133 | |
| 134 | return 1; |
| 135 | } |
| 136 | |
| 137 | static int pcie_rd_conf(struct pci_bus *bus, u32 devfn, int where, |
| 138 | int size, u32 *val) |
| 139 | { |
Russell King | 43ba990 | 2012-03-10 13:31:34 +0000 | [diff] [blame] | 140 | struct pci_sys_data *sys = bus->sysdata; |
| 141 | struct pcie_port *pp = sys->private_data; |
Stanislav Samsonov | 794d15b | 2008-06-22 22:45:10 +0200 | [diff] [blame] | 142 | unsigned long flags; |
| 143 | int ret; |
| 144 | |
| 145 | if (pcie_valid_config(pp, bus->number, PCI_SLOT(devfn)) == 0) { |
| 146 | *val = 0xffffffff; |
| 147 | return PCIBIOS_DEVICE_NOT_FOUND; |
| 148 | } |
| 149 | |
| 150 | spin_lock_irqsave(&pp->conf_lock, flags); |
| 151 | ret = orion_pcie_rd_conf(pp->base, bus, devfn, where, size, val); |
| 152 | spin_unlock_irqrestore(&pp->conf_lock, flags); |
| 153 | |
| 154 | return ret; |
| 155 | } |
| 156 | |
| 157 | static int pcie_wr_conf(struct pci_bus *bus, u32 devfn, |
| 158 | int where, int size, u32 val) |
| 159 | { |
Russell King | 43ba990 | 2012-03-10 13:31:34 +0000 | [diff] [blame] | 160 | struct pci_sys_data *sys = bus->sysdata; |
| 161 | struct pcie_port *pp = sys->private_data; |
Stanislav Samsonov | 794d15b | 2008-06-22 22:45:10 +0200 | [diff] [blame] | 162 | unsigned long flags; |
| 163 | int ret; |
| 164 | |
| 165 | if (pcie_valid_config(pp, bus->number, PCI_SLOT(devfn)) == 0) |
| 166 | return PCIBIOS_DEVICE_NOT_FOUND; |
| 167 | |
| 168 | spin_lock_irqsave(&pp->conf_lock, flags); |
| 169 | ret = orion_pcie_wr_conf(pp->base, bus, devfn, where, size, val); |
| 170 | spin_unlock_irqrestore(&pp->conf_lock, flags); |
| 171 | |
| 172 | return ret; |
| 173 | } |
| 174 | |
| 175 | static struct pci_ops pcie_ops = { |
| 176 | .read = pcie_rd_conf, |
| 177 | .write = pcie_wr_conf, |
| 178 | }; |
| 179 | |
Greg Kroah-Hartman | 351a102 | 2012-12-21 14:02:24 -0800 | [diff] [blame] | 180 | static void rc_pci_fixup(struct pci_dev *dev) |
Stanislav Samsonov | 794d15b | 2008-06-22 22:45:10 +0200 | [diff] [blame] | 181 | { |
| 182 | /* |
| 183 | * Prevent enumeration of root complex. |
| 184 | */ |
| 185 | if (dev->bus->parent == NULL && dev->devfn == 0) { |
| 186 | int i; |
| 187 | |
| 188 | for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) { |
| 189 | dev->resource[i].start = 0; |
| 190 | dev->resource[i].end = 0; |
| 191 | dev->resource[i].flags = 0; |
| 192 | } |
| 193 | } |
| 194 | } |
| 195 | DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_MARVELL, PCI_ANY_ID, rc_pci_fixup); |
| 196 | |
| 197 | static struct pci_bus __init * |
| 198 | mv78xx0_pcie_scan_bus(int nr, struct pci_sys_data *sys) |
| 199 | { |
| 200 | struct pci_bus *bus; |
| 201 | |
| 202 | if (nr < num_pcie_ports) { |
Bjorn Helgaas | 37d1590 | 2011-10-28 16:26:16 -0600 | [diff] [blame] | 203 | bus = pci_scan_root_bus(NULL, sys->busnr, &pcie_ops, sys, |
| 204 | &sys->resources); |
Stanislav Samsonov | 794d15b | 2008-06-22 22:45:10 +0200 | [diff] [blame] | 205 | } else { |
| 206 | bus = NULL; |
| 207 | BUG(); |
| 208 | } |
| 209 | |
| 210 | return bus; |
| 211 | } |
| 212 | |
Ralf Baechle | d534194 | 2011-06-10 15:30:21 +0100 | [diff] [blame] | 213 | static int __init mv78xx0_pcie_map_irq(const struct pci_dev *dev, u8 slot, |
| 214 | u8 pin) |
Stanislav Samsonov | 794d15b | 2008-06-22 22:45:10 +0200 | [diff] [blame] | 215 | { |
Russell King | 43ba990 | 2012-03-10 13:31:34 +0000 | [diff] [blame] | 216 | struct pci_sys_data *sys = dev->bus->sysdata; |
| 217 | struct pcie_port *pp = sys->private_data; |
Stanislav Samsonov | 794d15b | 2008-06-22 22:45:10 +0200 | [diff] [blame] | 218 | |
| 219 | return IRQ_MV78XX0_PCIE_00 + (pp->maj << 2) + pp->min; |
| 220 | } |
| 221 | |
| 222 | static struct hw_pci mv78xx0_pci __initdata = { |
| 223 | .nr_controllers = 8, |
| 224 | .preinit = mv78xx0_pcie_preinit, |
Stanislav Samsonov | 794d15b | 2008-06-22 22:45:10 +0200 | [diff] [blame] | 225 | .setup = mv78xx0_pcie_setup, |
| 226 | .scan = mv78xx0_pcie_scan_bus, |
| 227 | .map_irq = mv78xx0_pcie_map_irq, |
| 228 | }; |
| 229 | |
Thomas Petazzoni | 383b996 | 2012-09-11 14:27:20 +0200 | [diff] [blame] | 230 | static void __init add_pcie_port(int maj, int min, void __iomem *base) |
Stanislav Samsonov | 794d15b | 2008-06-22 22:45:10 +0200 | [diff] [blame] | 231 | { |
| 232 | printk(KERN_INFO "MV78xx0 PCIe port %d.%d: ", maj, min); |
| 233 | |
Thomas Petazzoni | 383b996 | 2012-09-11 14:27:20 +0200 | [diff] [blame] | 234 | if (orion_pcie_link_up(base)) { |
Stanislav Samsonov | 794d15b | 2008-06-22 22:45:10 +0200 | [diff] [blame] | 235 | struct pcie_port *pp = &pcie_port[num_pcie_ports++]; |
| 236 | |
| 237 | printk("link up\n"); |
| 238 | |
| 239 | pp->maj = maj; |
| 240 | pp->min = min; |
| 241 | pp->root_bus_nr = -1; |
Thomas Petazzoni | 383b996 | 2012-09-11 14:27:20 +0200 | [diff] [blame] | 242 | pp->base = base; |
Stanislav Samsonov | 794d15b | 2008-06-22 22:45:10 +0200 | [diff] [blame] | 243 | spin_lock_init(&pp->conf_lock); |
Rob Herring | 0b9b18e | 2012-07-09 23:26:58 -0500 | [diff] [blame] | 244 | memset(&pp->res, 0, sizeof(pp->res)); |
Stanislav Samsonov | 794d15b | 2008-06-22 22:45:10 +0200 | [diff] [blame] | 245 | } else { |
| 246 | printk("link down, ignoring\n"); |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | void __init mv78xx0_pcie_init(int init_port0, int init_port1) |
| 251 | { |
Rob Herring | cc22b4c | 2011-06-28 21:22:40 -0500 | [diff] [blame] | 252 | vga_base = MV78XX0_PCIE_MEM_PHYS_BASE; |
| 253 | |
Stanislav Samsonov | 794d15b | 2008-06-22 22:45:10 +0200 | [diff] [blame] | 254 | if (init_port0) { |
| 255 | add_pcie_port(0, 0, PCIE00_VIRT_BASE); |
Thomas Petazzoni | 383b996 | 2012-09-11 14:27:20 +0200 | [diff] [blame] | 256 | if (!orion_pcie_x4_mode(PCIE00_VIRT_BASE)) { |
Stanislav Samsonov | 794d15b | 2008-06-22 22:45:10 +0200 | [diff] [blame] | 257 | add_pcie_port(0, 1, PCIE01_VIRT_BASE); |
| 258 | add_pcie_port(0, 2, PCIE02_VIRT_BASE); |
| 259 | add_pcie_port(0, 3, PCIE03_VIRT_BASE); |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | if (init_port1) { |
| 264 | add_pcie_port(1, 0, PCIE10_VIRT_BASE); |
| 265 | if (!orion_pcie_x4_mode((void __iomem *)PCIE10_VIRT_BASE)) { |
| 266 | add_pcie_port(1, 1, PCIE11_VIRT_BASE); |
| 267 | add_pcie_port(1, 2, PCIE12_VIRT_BASE); |
| 268 | add_pcie_port(1, 3, PCIE13_VIRT_BASE); |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | pci_common_init(&mv78xx0_pci); |
| 273 | } |