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> |
Saeed Bishara | 651c74c | 2008-06-22 22:45:06 +0200 | [diff] [blame] | 20 | #include "common.h" |
| 21 | |
Andrew Lunn | 27e53cf | 2012-03-08 21:45:59 +0100 | [diff] [blame] | 22 | static void kirkwood_enable_pcie_clk(const char *port) |
| 23 | { |
| 24 | struct clk *clk; |
| 25 | |
| 26 | clk = clk_get_sys("pcie", port); |
| 27 | if (IS_ERR(clk)) { |
Andrew Lunn | 98adf93 | 2012-10-20 13:23:16 +0200 | [diff] [blame] | 28 | pr_err("PCIE clock %s missing\n", port); |
Andrew Lunn | 27e53cf | 2012-03-08 21:45:59 +0100 | [diff] [blame] | 29 | return; |
| 30 | } |
| 31 | clk_prepare_enable(clk); |
| 32 | clk_put(clk); |
| 33 | } |
| 34 | |
| 35 | /* This function is called very early in the boot when probing the |
| 36 | hardware to determine what we actually are, and what rate tclk is |
| 37 | ticking at. Hence calling kirkwood_enable_pcie_clk() is not |
| 38 | possible since the clk tree has not been created yet. */ |
Eric Cooper | 0e0cdd3 | 2011-02-02 17:16:10 -0500 | [diff] [blame] | 39 | void kirkwood_enable_pcie(void) |
| 40 | { |
| 41 | u32 curr = readl(CLOCK_GATING_CTRL); |
| 42 | if (!(curr & CGC_PEX0)) |
| 43 | writel(curr | CGC_PEX0, CLOCK_GATING_CTRL); |
| 44 | } |
| 45 | |
Andrew Lunn | 98d9986 | 2012-04-11 21:07:45 +0200 | [diff] [blame] | 46 | void kirkwood_pcie_id(u32 *dev, u32 *rev) |
Ronen Shitrit | b2b3dc2 | 2008-09-15 10:40:35 +0300 | [diff] [blame] | 47 | { |
Eric Cooper | 0e0cdd3 | 2011-02-02 17:16:10 -0500 | [diff] [blame] | 48 | kirkwood_enable_pcie(); |
Thomas Petazzoni | 060f3d1 | 2012-09-11 14:27:19 +0200 | [diff] [blame] | 49 | *dev = orion_pcie_dev_id(PCIE_VIRT_BASE); |
| 50 | *rev = orion_pcie_rev(PCIE_VIRT_BASE); |
Ronen Shitrit | b2b3dc2 | 2008-09-15 10:40:35 +0300 | [diff] [blame] | 51 | } |
| 52 | |
Saeed Bishara | ffd58bd | 2010-06-08 14:21:34 +0300 | [diff] [blame] | 53 | struct pcie_port { |
| 54 | u8 root_bus_nr; |
| 55 | void __iomem *base; |
| 56 | spinlock_t conf_lock; |
| 57 | int irq; |
Rob Herring | 2bb0808 | 2012-07-09 22:43:33 -0500 | [diff] [blame] | 58 | struct resource res; |
Saeed Bishara | ffd58bd | 2010-06-08 14:21:34 +0300 | [diff] [blame] | 59 | }; |
| 60 | |
| 61 | static int pcie_port_map[2]; |
| 62 | static int num_pcie_ports; |
| 63 | |
Saeed Bishara | ffd58bd | 2010-06-08 14:21:34 +0300 | [diff] [blame] | 64 | 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] | 65 | { |
| 66 | /* |
| 67 | * Don't go out when trying to access -- |
| 68 | * 1. nonexisting device on local bus |
| 69 | * 2. where there's no device connected (no link) |
| 70 | */ |
Saeed Bishara | ffd58bd | 2010-06-08 14:21:34 +0300 | [diff] [blame] | 71 | if (bus == pp->root_bus_nr && dev == 0) |
Saeed Bishara | 651c74c | 2008-06-22 22:45:06 +0200 | [diff] [blame] | 72 | return 1; |
| 73 | |
Saeed Bishara | ffd58bd | 2010-06-08 14:21:34 +0300 | [diff] [blame] | 74 | if (!orion_pcie_link_up(pp->base)) |
Saeed Bishara | 651c74c | 2008-06-22 22:45:06 +0200 | [diff] [blame] | 75 | return 0; |
| 76 | |
Saeed Bishara | ffd58bd | 2010-06-08 14:21:34 +0300 | [diff] [blame] | 77 | if (bus == pp->root_bus_nr && dev != 1) |
Saeed Bishara | 651c74c | 2008-06-22 22:45:06 +0200 | [diff] [blame] | 78 | return 0; |
| 79 | |
| 80 | return 1; |
| 81 | } |
| 82 | |
| 83 | |
| 84 | /* |
| 85 | * PCIe config cycles are done by programming the PCIE_CONF_ADDR register |
| 86 | * and then reading the PCIE_CONF_DATA register. Need to make sure these |
| 87 | * transactions are atomic. |
| 88 | */ |
Saeed Bishara | 651c74c | 2008-06-22 22:45:06 +0200 | [diff] [blame] | 89 | |
| 90 | static int pcie_rd_conf(struct pci_bus *bus, u32 devfn, int where, |
| 91 | int size, u32 *val) |
| 92 | { |
Russell King | 43ba990 | 2012-03-10 13:31:34 +0000 | [diff] [blame] | 93 | struct pci_sys_data *sys = bus->sysdata; |
| 94 | struct pcie_port *pp = sys->private_data; |
Saeed Bishara | 651c74c | 2008-06-22 22:45:06 +0200 | [diff] [blame] | 95 | unsigned long flags; |
| 96 | int ret; |
| 97 | |
Saeed Bishara | ffd58bd | 2010-06-08 14:21:34 +0300 | [diff] [blame] | 98 | if (pcie_valid_config(pp, bus->number, PCI_SLOT(devfn)) == 0) { |
Saeed Bishara | 651c74c | 2008-06-22 22:45:06 +0200 | [diff] [blame] | 99 | *val = 0xffffffff; |
| 100 | return PCIBIOS_DEVICE_NOT_FOUND; |
| 101 | } |
| 102 | |
Saeed Bishara | ffd58bd | 2010-06-08 14:21:34 +0300 | [diff] [blame] | 103 | spin_lock_irqsave(&pp->conf_lock, flags); |
| 104 | ret = orion_pcie_rd_conf(pp->base, bus, devfn, where, size, val); |
| 105 | spin_unlock_irqrestore(&pp->conf_lock, flags); |
Saeed Bishara | 651c74c | 2008-06-22 22:45:06 +0200 | [diff] [blame] | 106 | |
| 107 | return ret; |
| 108 | } |
| 109 | |
| 110 | static int pcie_wr_conf(struct pci_bus *bus, u32 devfn, |
| 111 | int where, int size, u32 val) |
| 112 | { |
Russell King | 43ba990 | 2012-03-10 13:31:34 +0000 | [diff] [blame] | 113 | struct pci_sys_data *sys = bus->sysdata; |
| 114 | struct pcie_port *pp = sys->private_data; |
Saeed Bishara | 651c74c | 2008-06-22 22:45:06 +0200 | [diff] [blame] | 115 | unsigned long flags; |
| 116 | int ret; |
| 117 | |
Saeed Bishara | ffd58bd | 2010-06-08 14:21:34 +0300 | [diff] [blame] | 118 | if (pcie_valid_config(pp, bus->number, PCI_SLOT(devfn)) == 0) |
Saeed Bishara | 651c74c | 2008-06-22 22:45:06 +0200 | [diff] [blame] | 119 | return PCIBIOS_DEVICE_NOT_FOUND; |
| 120 | |
Saeed Bishara | ffd58bd | 2010-06-08 14:21:34 +0300 | [diff] [blame] | 121 | spin_lock_irqsave(&pp->conf_lock, flags); |
| 122 | ret = orion_pcie_wr_conf(pp->base, bus, devfn, where, size, val); |
| 123 | spin_unlock_irqrestore(&pp->conf_lock, flags); |
Saeed Bishara | 651c74c | 2008-06-22 22:45:06 +0200 | [diff] [blame] | 124 | |
| 125 | return ret; |
| 126 | } |
| 127 | |
| 128 | static struct pci_ops pcie_ops = { |
| 129 | .read = pcie_rd_conf, |
| 130 | .write = pcie_wr_conf, |
| 131 | }; |
| 132 | |
Nicolas Pitre | a87182b | 2010-07-05 13:59:56 -0400 | [diff] [blame] | 133 | static void __init pcie0_ioresources_init(struct pcie_port *pp) |
Saeed Bishara | 651c74c | 2008-06-22 22:45:06 +0200 | [diff] [blame] | 134 | { |
Thomas Petazzoni | 060f3d1 | 2012-09-11 14:27:19 +0200 | [diff] [blame] | 135 | pp->base = PCIE_VIRT_BASE; |
Nicolas Pitre | a87182b | 2010-07-05 13:59:56 -0400 | [diff] [blame] | 136 | pp->irq = IRQ_KIRKWOOD_PCIE; |
Saeed Bishara | 651c74c | 2008-06-22 22:45:06 +0200 | [diff] [blame] | 137 | |
| 138 | /* |
Saeed Bishara | 651c74c | 2008-06-22 22:45:06 +0200 | [diff] [blame] | 139 | * IORESOURCE_MEM |
| 140 | */ |
Rob Herring | 2bb0808 | 2012-07-09 22:43:33 -0500 | [diff] [blame] | 141 | pp->res.name = "PCIe 0 MEM"; |
| 142 | pp->res.start = KIRKWOOD_PCIE_MEM_PHYS_BASE; |
| 143 | pp->res.end = pp->res.start + KIRKWOOD_PCIE_MEM_SIZE - 1; |
| 144 | pp->res.flags = IORESOURCE_MEM; |
Saeed Bishara | ffd58bd | 2010-06-08 14:21:34 +0300 | [diff] [blame] | 145 | } |
| 146 | |
Nicolas Pitre | a87182b | 2010-07-05 13:59:56 -0400 | [diff] [blame] | 147 | static void __init pcie1_ioresources_init(struct pcie_port *pp) |
Saeed Bishara | ffd58bd | 2010-06-08 14:21:34 +0300 | [diff] [blame] | 148 | { |
Thomas Petazzoni | 060f3d1 | 2012-09-11 14:27:19 +0200 | [diff] [blame] | 149 | pp->base = PCIE1_VIRT_BASE; |
Nicolas Pitre | a87182b | 2010-07-05 13:59:56 -0400 | [diff] [blame] | 150 | pp->irq = IRQ_KIRKWOOD_PCIE1; |
Saeed Bishara | ffd58bd | 2010-06-08 14:21:34 +0300 | [diff] [blame] | 151 | |
| 152 | /* |
Saeed Bishara | ffd58bd | 2010-06-08 14:21:34 +0300 | [diff] [blame] | 153 | * IORESOURCE_MEM |
| 154 | */ |
Rob Herring | 2bb0808 | 2012-07-09 22:43:33 -0500 | [diff] [blame] | 155 | pp->res.name = "PCIe 1 MEM"; |
| 156 | pp->res.start = KIRKWOOD_PCIE1_MEM_PHYS_BASE; |
| 157 | pp->res.end = pp->res.start + KIRKWOOD_PCIE1_MEM_SIZE - 1; |
| 158 | pp->res.flags = IORESOURCE_MEM; |
Saeed Bishara | ffd58bd | 2010-06-08 14:21:34 +0300 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | static int __init kirkwood_pcie_setup(int nr, struct pci_sys_data *sys) |
| 162 | { |
Saeed Bishara | ffd58bd | 2010-06-08 14:21:34 +0300 | [diff] [blame] | 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]; |
Andrew Lunn | 98adf93 | 2012-10-20 13:23:16 +0200 | [diff] [blame] | 170 | pr_info("PCI: bus%d uses PCIe port %d\n", sys->busnr, index); |
Saeed Bishara | ffd58bd | 2010-06-08 14:21:34 +0300 | [diff] [blame] | 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: |
Andrew Lunn | 27e53cf | 2012-03-08 21:45:59 +0100 | [diff] [blame] | 181 | kirkwood_enable_pcie_clk("0"); |
Nicolas Pitre | a87182b | 2010-07-05 13:59:56 -0400 | [diff] [blame] | 182 | pcie0_ioresources_init(pp); |
Rob Herring | 2bb0808 | 2012-07-09 22:43:33 -0500 | [diff] [blame] | 183 | pci_ioremap_io(SZ_64K * sys->busnr, KIRKWOOD_PCIE_IO_PHYS_BASE); |
Saeed Bishara | ffd58bd | 2010-06-08 14:21:34 +0300 | [diff] [blame] | 184 | break; |
| 185 | case 1: |
Andrew Lunn | 27e53cf | 2012-03-08 21:45:59 +0100 | [diff] [blame] | 186 | kirkwood_enable_pcie_clk("1"); |
Nicolas Pitre | a87182b | 2010-07-05 13:59:56 -0400 | [diff] [blame] | 187 | pcie1_ioresources_init(pp); |
Andrew Lunn | 98adf93 | 2012-10-20 13:23:16 +0200 | [diff] [blame] | 188 | pci_ioremap_io(SZ_64K * sys->busnr, |
| 189 | KIRKWOOD_PCIE1_IO_PHYS_BASE); |
Saeed Bishara | ffd58bd | 2010-06-08 14:21:34 +0300 | [diff] [blame] | 190 | break; |
| 191 | default: |
Nicolas Pitre | a87182b | 2010-07-05 13:59:56 -0400 | [diff] [blame] | 192 | panic("PCIe setup: invalid controller %d", index); |
Saeed Bishara | ffd58bd | 2010-06-08 14:21:34 +0300 | [diff] [blame] | 193 | } |
| 194 | |
Rob Herring | 2bb0808 | 2012-07-09 22:43:33 -0500 | [diff] [blame] | 195 | if (request_resource(&iomem_resource, &pp->res)) |
Nicolas Pitre | a87182b | 2010-07-05 13:59:56 -0400 | [diff] [blame] | 196 | panic("Request PCIe%d Memory resource failed\n", index); |
| 197 | |
Rob Herring | 2bb0808 | 2012-07-09 22:43:33 -0500 | [diff] [blame] | 198 | pci_add_resource_offset(&sys->resources, &pp->res, sys->mem_offset); |
Nicolas Pitre | a87182b | 2010-07-05 13:59:56 -0400 | [diff] [blame] | 199 | |
Saeed Bishara | ffd58bd | 2010-06-08 14:21:34 +0300 | [diff] [blame] | 200 | /* |
| 201 | * Generic PCIe unit setup. |
| 202 | */ |
| 203 | orion_pcie_set_local_bus_nr(pp->base, sys->busnr); |
| 204 | |
Andrew Lunn | 63a9332 | 2011-12-07 21:48:07 +0100 | [diff] [blame] | 205 | orion_pcie_setup(pp->base); |
Rabeeh Khoury | e8b2b7b | 2009-03-22 17:30:32 +0200 | [diff] [blame] | 206 | |
Saeed Bishara | 651c74c | 2008-06-22 22:45:06 +0200 | [diff] [blame] | 207 | return 1; |
| 208 | } |
| 209 | |
Jason Gunthorpe | 1dc831b | 2012-11-21 00:19:06 -0700 | [diff] [blame] | 210 | /* |
| 211 | * The root complex has a hardwired class of PCI_CLASS_MEMORY_OTHER, when it |
| 212 | * is operating as a root complex this needs to be switched to |
| 213 | * PCI_CLASS_BRIDGE_HOST or Linux will errantly try to process the BAR's on |
| 214 | * the device. Decoding setup is handled by the orion code. |
| 215 | */ |
Greg Kroah-Hartman | 351a102 | 2012-12-21 14:02:24 -0800 | [diff] [blame] | 216 | static void rc_pci_fixup(struct pci_dev *dev) |
Saeed Bishara | 651c74c | 2008-06-22 22:45:06 +0200 | [diff] [blame] | 217 | { |
Saeed Bishara | 651c74c | 2008-06-22 22:45:06 +0200 | [diff] [blame] | 218 | if (dev->bus->parent == NULL && dev->devfn == 0) { |
| 219 | int i; |
| 220 | |
Jason Gunthorpe | 1dc831b | 2012-11-21 00:19:06 -0700 | [diff] [blame] | 221 | dev->class &= 0xff; |
| 222 | dev->class |= PCI_CLASS_BRIDGE_HOST << 8; |
Saeed Bishara | 651c74c | 2008-06-22 22:45:06 +0200 | [diff] [blame] | 223 | for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) { |
| 224 | dev->resource[i].start = 0; |
| 225 | dev->resource[i].end = 0; |
| 226 | dev->resource[i].flags = 0; |
| 227 | } |
| 228 | } |
| 229 | } |
| 230 | DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_MARVELL, PCI_ANY_ID, rc_pci_fixup); |
| 231 | |
Ralf Baechle | d534194 | 2011-06-10 15:30:21 +0100 | [diff] [blame] | 232 | static int __init kirkwood_pcie_map_irq(const struct pci_dev *dev, u8 slot, |
| 233 | u8 pin) |
Saeed Bishara | 651c74c | 2008-06-22 22:45:06 +0200 | [diff] [blame] | 234 | { |
Russell King | 43ba990 | 2012-03-10 13:31:34 +0000 | [diff] [blame] | 235 | struct pci_sys_data *sys = dev->sysdata; |
| 236 | struct pcie_port *pp = sys->private_data; |
Saeed Bishara | ffd58bd | 2010-06-08 14:21:34 +0300 | [diff] [blame] | 237 | |
| 238 | return pp->irq; |
Saeed Bishara | 651c74c | 2008-06-22 22:45:06 +0200 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | static struct hw_pci kirkwood_pci __initdata = { |
Saeed Bishara | 651c74c | 2008-06-22 22:45:06 +0200 | [diff] [blame] | 242 | .setup = kirkwood_pcie_setup, |
Saeed Bishara | 651c74c | 2008-06-22 22:45:06 +0200 | [diff] [blame] | 243 | .map_irq = kirkwood_pcie_map_irq, |
Jason Gunthorpe | 4a9329a | 2012-11-21 00:15:11 -0700 | [diff] [blame] | 244 | .ops = &pcie_ops, |
Saeed Bishara | 651c74c | 2008-06-22 22:45:06 +0200 | [diff] [blame] | 245 | }; |
| 246 | |
Thomas Petazzoni | 060f3d1 | 2012-09-11 14:27:19 +0200 | [diff] [blame] | 247 | static void __init add_pcie_port(int index, void __iomem *base) |
Saeed Bishara | 651c74c | 2008-06-22 22:45:06 +0200 | [diff] [blame] | 248 | { |
Jason Gunthorpe | b73690c | 2012-11-21 11:25:28 -0700 | [diff] [blame] | 249 | pcie_port_map[num_pcie_ports++] = index; |
| 250 | pr_info("Kirkwood PCIe port %d: link %s\n", index, |
| 251 | orion_pcie_link_up(base) ? "up" : "down"); |
Saeed Bishara | ffd58bd | 2010-06-08 14:21:34 +0300 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | void __init kirkwood_pcie_init(unsigned int portmask) |
| 255 | { |
Rob Herring | cc22b4c | 2011-06-28 21:22:40 -0500 | [diff] [blame] | 256 | vga_base = KIRKWOOD_PCIE_MEM_PHYS_BASE; |
| 257 | |
Saeed Bishara | ffd58bd | 2010-06-08 14:21:34 +0300 | [diff] [blame] | 258 | if (portmask & KW_PCIE0) |
| 259 | add_pcie_port(0, PCIE_VIRT_BASE); |
| 260 | |
| 261 | if (portmask & KW_PCIE1) |
| 262 | add_pcie_port(1, PCIE1_VIRT_BASE); |
| 263 | |
| 264 | kirkwood_pci.nr_controllers = num_pcie_ports; |
Saeed Bishara | 651c74c | 2008-06-22 22:45:06 +0200 | [diff] [blame] | 265 | pci_common_init(&kirkwood_pci); |
| 266 | } |