Saeed Bishara | 651c74c | 2008-06-22 22:45:06 +0200 | [diff] [blame] | 1 | /* |
| 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 Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 13 | #include <linux/slab.h> |
Andrew Lunn | 27e53cf | 2012-03-08 21:45:59 +0100 | [diff] [blame] | 14 | #include <linux/clk.h> |
Rob Herring | cc22b4c | 2011-06-28 21:22:40 -0500 | [diff] [blame] | 15 | #include <video/vga.h> |
Nicolas Pitre | 6e5c11a | 2009-01-07 04:47:02 +0100 | [diff] [blame] | 16 | #include <asm/irq.h> |
Saeed Bishara | 651c74c | 2008-06-22 22:45:06 +0200 | [diff] [blame] | 17 | #include <asm/mach/pci.h> |
Lennert Buytenhek | 6f088f1 | 2008-08-09 13:44:58 +0200 | [diff] [blame] | 18 | #include <plat/pcie.h> |
Rabeeh Khoury | e8b2b7b | 2009-03-22 17:30:32 +0200 | [diff] [blame] | 19 | #include <mach/bridge-regs.h> |
Andrew Lunn | 45173d5 | 2011-12-07 21:48:06 +0100 | [diff] [blame] | 20 | #include <plat/addr-map.h> |
Saeed Bishara | 651c74c | 2008-06-22 22:45:06 +0200 | [diff] [blame] | 21 | #include "common.h" |
| 22 | |
Andrew Lunn | 27e53cf | 2012-03-08 21:45:59 +0100 | [diff] [blame] | 23 | static void kirkwood_enable_pcie_clk(const char *port) |
| 24 | { |
| 25 | struct clk *clk; |
| 26 | |
| 27 | clk = clk_get_sys("pcie", port); |
| 28 | if (IS_ERR(clk)) { |
| 29 | printk(KERN_ERR "PCIE clock %s missing\n", port); |
| 30 | return; |
| 31 | } |
| 32 | clk_prepare_enable(clk); |
| 33 | clk_put(clk); |
| 34 | } |
| 35 | |
| 36 | /* This function is called very early in the boot when probing the |
| 37 | hardware to determine what we actually are, and what rate tclk is |
| 38 | ticking at. Hence calling kirkwood_enable_pcie_clk() is not |
| 39 | possible since the clk tree has not been created yet. */ |
Eric Cooper | 0e0cdd3 | 2011-02-02 17:16:10 -0500 | [diff] [blame] | 40 | void kirkwood_enable_pcie(void) |
| 41 | { |
| 42 | u32 curr = readl(CLOCK_GATING_CTRL); |
| 43 | if (!(curr & CGC_PEX0)) |
| 44 | writel(curr | CGC_PEX0, CLOCK_GATING_CTRL); |
| 45 | } |
| 46 | |
Andrew Lunn | 98d9986 | 2012-04-11 21:07:45 +0200 | [diff] [blame] | 47 | void kirkwood_pcie_id(u32 *dev, u32 *rev) |
Ronen Shitrit | b2b3dc2f | 2008-09-15 10:40:35 +0300 | [diff] [blame] | 48 | { |
Eric Cooper | 0e0cdd3 | 2011-02-02 17:16:10 -0500 | [diff] [blame] | 49 | kirkwood_enable_pcie(); |
Thomas Petazzoni | 060f3d1 | 2012-09-11 14:27:19 +0200 | [diff] [blame^] | 50 | *dev = orion_pcie_dev_id(PCIE_VIRT_BASE); |
| 51 | *rev = orion_pcie_rev(PCIE_VIRT_BASE); |
Ronen Shitrit | b2b3dc2f | 2008-09-15 10:40:35 +0300 | [diff] [blame] | 52 | } |
| 53 | |
Saeed Bishara | ffd58bd | 2010-06-08 14:21:34 +0300 | [diff] [blame] | 54 | struct pcie_port { |
| 55 | u8 root_bus_nr; |
| 56 | void __iomem *base; |
| 57 | spinlock_t conf_lock; |
| 58 | int irq; |
| 59 | struct resource res[2]; |
| 60 | }; |
| 61 | |
| 62 | static int pcie_port_map[2]; |
| 63 | static int num_pcie_ports; |
| 64 | |
Saeed Bishara | ffd58bd | 2010-06-08 14:21:34 +0300 | [diff] [blame] | 65 | static int pcie_valid_config(struct pcie_port *pp, int bus, int dev) |
Saeed Bishara | 651c74c | 2008-06-22 22:45:06 +0200 | [diff] [blame] | 66 | { |
| 67 | /* |
| 68 | * Don't go out when trying to access -- |
| 69 | * 1. nonexisting device on local bus |
| 70 | * 2. where there's no device connected (no link) |
| 71 | */ |
Saeed Bishara | ffd58bd | 2010-06-08 14:21:34 +0300 | [diff] [blame] | 72 | if (bus == pp->root_bus_nr && dev == 0) |
Saeed Bishara | 651c74c | 2008-06-22 22:45:06 +0200 | [diff] [blame] | 73 | return 1; |
| 74 | |
Saeed Bishara | ffd58bd | 2010-06-08 14:21:34 +0300 | [diff] [blame] | 75 | if (!orion_pcie_link_up(pp->base)) |
Saeed Bishara | 651c74c | 2008-06-22 22:45:06 +0200 | [diff] [blame] | 76 | return 0; |
| 77 | |
Saeed Bishara | ffd58bd | 2010-06-08 14:21:34 +0300 | [diff] [blame] | 78 | if (bus == pp->root_bus_nr && dev != 1) |
Saeed Bishara | 651c74c | 2008-06-22 22:45:06 +0200 | [diff] [blame] | 79 | return 0; |
| 80 | |
| 81 | return 1; |
| 82 | } |
| 83 | |
| 84 | |
| 85 | /* |
| 86 | * PCIe config cycles are done by programming the PCIE_CONF_ADDR register |
| 87 | * and then reading the PCIE_CONF_DATA register. Need to make sure these |
| 88 | * transactions are atomic. |
| 89 | */ |
Saeed Bishara | 651c74c | 2008-06-22 22:45:06 +0200 | [diff] [blame] | 90 | |
| 91 | static int pcie_rd_conf(struct pci_bus *bus, u32 devfn, int where, |
| 92 | int size, u32 *val) |
| 93 | { |
Russell King | 43ba990 | 2012-03-10 13:31:34 +0000 | [diff] [blame] | 94 | struct pci_sys_data *sys = bus->sysdata; |
| 95 | struct pcie_port *pp = sys->private_data; |
Saeed Bishara | 651c74c | 2008-06-22 22:45:06 +0200 | [diff] [blame] | 96 | unsigned long flags; |
| 97 | int ret; |
| 98 | |
Saeed Bishara | ffd58bd | 2010-06-08 14:21:34 +0300 | [diff] [blame] | 99 | if (pcie_valid_config(pp, bus->number, PCI_SLOT(devfn)) == 0) { |
Saeed Bishara | 651c74c | 2008-06-22 22:45:06 +0200 | [diff] [blame] | 100 | *val = 0xffffffff; |
| 101 | return PCIBIOS_DEVICE_NOT_FOUND; |
| 102 | } |
| 103 | |
Saeed Bishara | ffd58bd | 2010-06-08 14:21:34 +0300 | [diff] [blame] | 104 | spin_lock_irqsave(&pp->conf_lock, flags); |
| 105 | ret = orion_pcie_rd_conf(pp->base, bus, devfn, where, size, val); |
| 106 | spin_unlock_irqrestore(&pp->conf_lock, flags); |
Saeed Bishara | 651c74c | 2008-06-22 22:45:06 +0200 | [diff] [blame] | 107 | |
| 108 | return ret; |
| 109 | } |
| 110 | |
| 111 | static int pcie_wr_conf(struct pci_bus *bus, u32 devfn, |
| 112 | int where, int size, u32 val) |
| 113 | { |
Russell King | 43ba990 | 2012-03-10 13:31:34 +0000 | [diff] [blame] | 114 | struct pci_sys_data *sys = bus->sysdata; |
| 115 | struct pcie_port *pp = sys->private_data; |
Saeed Bishara | 651c74c | 2008-06-22 22:45:06 +0200 | [diff] [blame] | 116 | unsigned long flags; |
| 117 | int ret; |
| 118 | |
Saeed Bishara | ffd58bd | 2010-06-08 14:21:34 +0300 | [diff] [blame] | 119 | if (pcie_valid_config(pp, bus->number, PCI_SLOT(devfn)) == 0) |
Saeed Bishara | 651c74c | 2008-06-22 22:45:06 +0200 | [diff] [blame] | 120 | return PCIBIOS_DEVICE_NOT_FOUND; |
| 121 | |
Saeed Bishara | ffd58bd | 2010-06-08 14:21:34 +0300 | [diff] [blame] | 122 | spin_lock_irqsave(&pp->conf_lock, flags); |
| 123 | ret = orion_pcie_wr_conf(pp->base, bus, devfn, where, size, val); |
| 124 | spin_unlock_irqrestore(&pp->conf_lock, flags); |
Saeed Bishara | 651c74c | 2008-06-22 22:45:06 +0200 | [diff] [blame] | 125 | |
| 126 | return ret; |
| 127 | } |
| 128 | |
| 129 | static struct pci_ops pcie_ops = { |
| 130 | .read = pcie_rd_conf, |
| 131 | .write = pcie_wr_conf, |
| 132 | }; |
| 133 | |
Nicolas Pitre | a87182b | 2010-07-05 13:59:56 -0400 | [diff] [blame] | 134 | static void __init pcie0_ioresources_init(struct pcie_port *pp) |
Saeed Bishara | 651c74c | 2008-06-22 22:45:06 +0200 | [diff] [blame] | 135 | { |
Thomas Petazzoni | 060f3d1 | 2012-09-11 14:27:19 +0200 | [diff] [blame^] | 136 | pp->base = PCIE_VIRT_BASE; |
Nicolas Pitre | a87182b | 2010-07-05 13:59:56 -0400 | [diff] [blame] | 137 | pp->irq = IRQ_KIRKWOOD_PCIE; |
Saeed Bishara | 651c74c | 2008-06-22 22:45:06 +0200 | [diff] [blame] | 138 | |
| 139 | /* |
| 140 | * IORESOURCE_IO |
| 141 | */ |
Saeed Bishara | ffd58bd | 2010-06-08 14:21:34 +0300 | [diff] [blame] | 142 | pp->res[0].name = "PCIe 0 I/O Space"; |
Arnaud Patard | e4ff1c3 | 2010-08-22 22:49:46 +0200 | [diff] [blame] | 143 | pp->res[0].start = KIRKWOOD_PCIE_IO_BUS_BASE; |
Saeed Bishara | ffd58bd | 2010-06-08 14:21:34 +0300 | [diff] [blame] | 144 | pp->res[0].end = pp->res[0].start + KIRKWOOD_PCIE_IO_SIZE - 1; |
| 145 | pp->res[0].flags = IORESOURCE_IO; |
Saeed Bishara | 651c74c | 2008-06-22 22:45:06 +0200 | [diff] [blame] | 146 | |
| 147 | /* |
| 148 | * IORESOURCE_MEM |
| 149 | */ |
Saeed Bishara | ffd58bd | 2010-06-08 14:21:34 +0300 | [diff] [blame] | 150 | pp->res[1].name = "PCIe 0 MEM"; |
| 151 | pp->res[1].start = KIRKWOOD_PCIE_MEM_PHYS_BASE; |
| 152 | pp->res[1].end = pp->res[1].start + KIRKWOOD_PCIE_MEM_SIZE - 1; |
| 153 | pp->res[1].flags = IORESOURCE_MEM; |
Saeed Bishara | ffd58bd | 2010-06-08 14:21:34 +0300 | [diff] [blame] | 154 | } |
| 155 | |
Nicolas Pitre | a87182b | 2010-07-05 13:59:56 -0400 | [diff] [blame] | 156 | static void __init pcie1_ioresources_init(struct pcie_port *pp) |
Saeed Bishara | ffd58bd | 2010-06-08 14:21:34 +0300 | [diff] [blame] | 157 | { |
Thomas Petazzoni | 060f3d1 | 2012-09-11 14:27:19 +0200 | [diff] [blame^] | 158 | pp->base = PCIE1_VIRT_BASE; |
Nicolas Pitre | a87182b | 2010-07-05 13:59:56 -0400 | [diff] [blame] | 159 | pp->irq = IRQ_KIRKWOOD_PCIE1; |
Saeed Bishara | ffd58bd | 2010-06-08 14:21:34 +0300 | [diff] [blame] | 160 | |
| 161 | /* |
| 162 | * IORESOURCE_IO |
| 163 | */ |
| 164 | pp->res[0].name = "PCIe 1 I/O Space"; |
Arnaud Patard | e4ff1c3 | 2010-08-22 22:49:46 +0200 | [diff] [blame] | 165 | pp->res[0].start = KIRKWOOD_PCIE1_IO_BUS_BASE; |
Saeed Bishara | ffd58bd | 2010-06-08 14:21:34 +0300 | [diff] [blame] | 166 | pp->res[0].end = pp->res[0].start + KIRKWOOD_PCIE1_IO_SIZE - 1; |
| 167 | pp->res[0].flags = IORESOURCE_IO; |
Saeed Bishara | ffd58bd | 2010-06-08 14:21:34 +0300 | [diff] [blame] | 168 | |
| 169 | /* |
| 170 | * IORESOURCE_MEM |
| 171 | */ |
| 172 | pp->res[1].name = "PCIe 1 MEM"; |
| 173 | pp->res[1].start = KIRKWOOD_PCIE1_MEM_PHYS_BASE; |
| 174 | pp->res[1].end = pp->res[1].start + KIRKWOOD_PCIE1_MEM_SIZE - 1; |
| 175 | pp->res[1].flags = IORESOURCE_MEM; |
Saeed Bishara | ffd58bd | 2010-06-08 14:21:34 +0300 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | static int __init kirkwood_pcie_setup(int nr, struct pci_sys_data *sys) |
| 179 | { |
Saeed Bishara | ffd58bd | 2010-06-08 14:21:34 +0300 | [diff] [blame] | 180 | struct pcie_port *pp; |
| 181 | int index; |
| 182 | |
| 183 | if (nr >= num_pcie_ports) |
| 184 | return 0; |
| 185 | |
| 186 | index = pcie_port_map[nr]; |
| 187 | printk(KERN_INFO "PCI: bus%d uses PCIe port %d\n", sys->busnr, index); |
| 188 | |
| 189 | pp = kzalloc(sizeof(*pp), GFP_KERNEL); |
| 190 | if (!pp) |
| 191 | panic("PCIe: failed to allocate pcie_port data"); |
| 192 | sys->private_data = pp; |
| 193 | pp->root_bus_nr = sys->busnr; |
| 194 | spin_lock_init(&pp->conf_lock); |
| 195 | |
| 196 | switch (index) { |
| 197 | case 0: |
Andrew Lunn | 27e53cf | 2012-03-08 21:45:59 +0100 | [diff] [blame] | 198 | kirkwood_enable_pcie_clk("0"); |
Nicolas Pitre | a87182b | 2010-07-05 13:59:56 -0400 | [diff] [blame] | 199 | pcie0_ioresources_init(pp); |
Saeed Bishara | ffd58bd | 2010-06-08 14:21:34 +0300 | [diff] [blame] | 200 | break; |
| 201 | case 1: |
Andrew Lunn | 27e53cf | 2012-03-08 21:45:59 +0100 | [diff] [blame] | 202 | kirkwood_enable_pcie_clk("1"); |
Nicolas Pitre | a87182b | 2010-07-05 13:59:56 -0400 | [diff] [blame] | 203 | pcie1_ioresources_init(pp); |
Saeed Bishara | ffd58bd | 2010-06-08 14:21:34 +0300 | [diff] [blame] | 204 | break; |
| 205 | default: |
Nicolas Pitre | a87182b | 2010-07-05 13:59:56 -0400 | [diff] [blame] | 206 | panic("PCIe setup: invalid controller %d", index); |
Saeed Bishara | ffd58bd | 2010-06-08 14:21:34 +0300 | [diff] [blame] | 207 | } |
| 208 | |
Nicolas Pitre | a87182b | 2010-07-05 13:59:56 -0400 | [diff] [blame] | 209 | if (request_resource(&ioport_resource, &pp->res[0])) |
| 210 | panic("Request PCIe%d IO resource failed\n", index); |
| 211 | if (request_resource(&iomem_resource, &pp->res[1])) |
| 212 | panic("Request PCIe%d Memory resource failed\n", index); |
| 213 | |
Nicolas Pitre | a87182b | 2010-07-05 13:59:56 -0400 | [diff] [blame] | 214 | sys->io_offset = 0; |
Bjorn Helgaas | 9f786d0 | 2012-02-23 20:19:01 -0700 | [diff] [blame] | 215 | pci_add_resource_offset(&sys->resources, &pp->res[0], sys->io_offset); |
| 216 | pci_add_resource_offset(&sys->resources, &pp->res[1], sys->mem_offset); |
Nicolas Pitre | a87182b | 2010-07-05 13:59:56 -0400 | [diff] [blame] | 217 | |
Saeed Bishara | ffd58bd | 2010-06-08 14:21:34 +0300 | [diff] [blame] | 218 | /* |
| 219 | * Generic PCIe unit setup. |
| 220 | */ |
| 221 | orion_pcie_set_local_bus_nr(pp->base, sys->busnr); |
| 222 | |
Andrew Lunn | 63a9332 | 2011-12-07 21:48:07 +0100 | [diff] [blame] | 223 | orion_pcie_setup(pp->base); |
Rabeeh Khoury | e8b2b7b | 2009-03-22 17:30:32 +0200 | [diff] [blame] | 224 | |
Saeed Bishara | 651c74c | 2008-06-22 22:45:06 +0200 | [diff] [blame] | 225 | return 1; |
| 226 | } |
| 227 | |
| 228 | static void __devinit rc_pci_fixup(struct pci_dev *dev) |
| 229 | { |
| 230 | /* |
| 231 | * Prevent enumeration of root complex. |
| 232 | */ |
| 233 | if (dev->bus->parent == NULL && dev->devfn == 0) { |
| 234 | int i; |
| 235 | |
| 236 | for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) { |
| 237 | dev->resource[i].start = 0; |
| 238 | dev->resource[i].end = 0; |
| 239 | dev->resource[i].flags = 0; |
| 240 | } |
| 241 | } |
| 242 | } |
| 243 | DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_MARVELL, PCI_ANY_ID, rc_pci_fixup); |
| 244 | |
| 245 | static struct pci_bus __init * |
| 246 | kirkwood_pcie_scan_bus(int nr, struct pci_sys_data *sys) |
| 247 | { |
| 248 | struct pci_bus *bus; |
| 249 | |
Saeed Bishara | ffd58bd | 2010-06-08 14:21:34 +0300 | [diff] [blame] | 250 | if (nr < num_pcie_ports) { |
Bjorn Helgaas | 37d1590 | 2011-10-28 16:26:16 -0600 | [diff] [blame] | 251 | bus = pci_scan_root_bus(NULL, sys->busnr, &pcie_ops, sys, |
| 252 | &sys->resources); |
Saeed Bishara | 651c74c | 2008-06-22 22:45:06 +0200 | [diff] [blame] | 253 | } else { |
| 254 | bus = NULL; |
| 255 | BUG(); |
| 256 | } |
| 257 | |
| 258 | return bus; |
| 259 | } |
| 260 | |
Ralf Baechle | d534194 | 2011-06-10 15:30:21 +0100 | [diff] [blame] | 261 | static int __init kirkwood_pcie_map_irq(const struct pci_dev *dev, u8 slot, |
| 262 | u8 pin) |
Saeed Bishara | 651c74c | 2008-06-22 22:45:06 +0200 | [diff] [blame] | 263 | { |
Russell King | 43ba990 | 2012-03-10 13:31:34 +0000 | [diff] [blame] | 264 | struct pci_sys_data *sys = dev->sysdata; |
| 265 | struct pcie_port *pp = sys->private_data; |
Saeed Bishara | ffd58bd | 2010-06-08 14:21:34 +0300 | [diff] [blame] | 266 | |
| 267 | return pp->irq; |
Saeed Bishara | 651c74c | 2008-06-22 22:45:06 +0200 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | static struct hw_pci kirkwood_pci __initdata = { |
Saeed Bishara | 651c74c | 2008-06-22 22:45:06 +0200 | [diff] [blame] | 271 | .setup = kirkwood_pcie_setup, |
| 272 | .scan = kirkwood_pcie_scan_bus, |
| 273 | .map_irq = kirkwood_pcie_map_irq, |
| 274 | }; |
| 275 | |
Thomas Petazzoni | 060f3d1 | 2012-09-11 14:27:19 +0200 | [diff] [blame^] | 276 | static void __init add_pcie_port(int index, void __iomem *base) |
Saeed Bishara | 651c74c | 2008-06-22 22:45:06 +0200 | [diff] [blame] | 277 | { |
Saeed Bishara | ffd58bd | 2010-06-08 14:21:34 +0300 | [diff] [blame] | 278 | printk(KERN_INFO "Kirkwood PCIe port %d: ", index); |
| 279 | |
Thomas Petazzoni | 060f3d1 | 2012-09-11 14:27:19 +0200 | [diff] [blame^] | 280 | if (orion_pcie_link_up(base)) { |
Saeed Bishara | ffd58bd | 2010-06-08 14:21:34 +0300 | [diff] [blame] | 281 | printk(KERN_INFO "link up\n"); |
| 282 | pcie_port_map[num_pcie_ports++] = index; |
| 283 | } else |
| 284 | printk(KERN_INFO "link down, ignoring\n"); |
| 285 | } |
| 286 | |
| 287 | void __init kirkwood_pcie_init(unsigned int portmask) |
| 288 | { |
Rob Herring | cc22b4c | 2011-06-28 21:22:40 -0500 | [diff] [blame] | 289 | vga_base = KIRKWOOD_PCIE_MEM_PHYS_BASE; |
| 290 | |
Saeed Bishara | ffd58bd | 2010-06-08 14:21:34 +0300 | [diff] [blame] | 291 | if (portmask & KW_PCIE0) |
| 292 | add_pcie_port(0, PCIE_VIRT_BASE); |
| 293 | |
| 294 | if (portmask & KW_PCIE1) |
| 295 | add_pcie_port(1, PCIE1_VIRT_BASE); |
| 296 | |
| 297 | kirkwood_pci.nr_controllers = num_pcie_ports; |
Saeed Bishara | 651c74c | 2008-06-22 22:45:06 +0200 | [diff] [blame] | 298 | pci_common_init(&kirkwood_pci); |
| 299 | } |