Paul Mundt | 5713e60 | 2009-06-17 18:20:48 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Low-Level PCI Express Support for the SH7786 |
| 3 | * |
Paul Mundt | 1da09c4 | 2011-01-18 19:56:04 +0900 | [diff] [blame] | 4 | * Copyright (C) 2009 - 2011 Paul Mundt |
Paul Mundt | 5713e60 | 2009-06-17 18:20:48 +0900 | [diff] [blame] | 5 | * |
| 6 | * This file is subject to the terms and conditions of the GNU General Public |
| 7 | * License. See the file "COPYING" in the main directory of this archive |
| 8 | * for more details. |
| 9 | */ |
Paul Mundt | 1da09c4 | 2011-01-18 19:56:04 +0900 | [diff] [blame] | 10 | #define pr_fmt(fmt) "PCI: " fmt |
| 11 | |
Paul Mundt | 5713e60 | 2009-06-17 18:20:48 +0900 | [diff] [blame] | 12 | #include <linux/pci.h> |
| 13 | #include <linux/init.h> |
| 14 | #include <linux/kernel.h> |
| 15 | #include <linux/io.h> |
Paul Mundt | 1da09c4 | 2011-01-18 19:56:04 +0900 | [diff] [blame] | 16 | #include <linux/async.h> |
Paul Mundt | 5713e60 | 2009-06-17 18:20:48 +0900 | [diff] [blame] | 17 | #include <linux/delay.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 18 | #include <linux/slab.h> |
Paul Mundt | c524ebf | 2010-09-20 18:45:11 +0900 | [diff] [blame] | 19 | #include <linux/clk.h> |
| 20 | #include <linux/sh_clk.h> |
Paul Mundt | 58796ce | 2012-05-18 17:42:29 +0900 | [diff] [blame] | 21 | #include <linux/sh_intc.h> |
Thomas Petazzoni | d62e9bf | 2017-12-04 16:09:04 +0100 | [diff] [blame] | 22 | #include <cpu/sh7786.h> |
Paul Mundt | 5713e60 | 2009-06-17 18:20:48 +0900 | [diff] [blame] | 23 | #include "pcie-sh7786.h" |
| 24 | #include <asm/sizes.h> |
| 25 | |
| 26 | struct sh7786_pcie_port { |
| 27 | struct pci_channel *hose; |
Paul Mundt | c524ebf | 2010-09-20 18:45:11 +0900 | [diff] [blame] | 28 | struct clk *fclk, phy_clk; |
Paul Mundt | 5713e60 | 2009-06-17 18:20:48 +0900 | [diff] [blame] | 29 | unsigned int index; |
| 30 | int endpoint; |
| 31 | int link; |
| 32 | }; |
| 33 | |
| 34 | static struct sh7786_pcie_port *sh7786_pcie_ports; |
| 35 | static unsigned int nr_ports; |
Thomas Petazzoni | bf9c7e3 | 2017-12-04 16:09:07 +0100 | [diff] [blame] | 36 | static unsigned long dma_pfn_offset; |
Paul Mundt | 5713e60 | 2009-06-17 18:20:48 +0900 | [diff] [blame] | 37 | |
| 38 | static struct sh7786_pcie_hwops { |
| 39 | int (*core_init)(void); |
Lai Jiangshan | 362f2b0 | 2013-03-12 13:59:14 -0700 | [diff] [blame] | 40 | async_func_t port_init_hw; |
Paul Mundt | 5713e60 | 2009-06-17 18:20:48 +0900 | [diff] [blame] | 41 | } *sh7786_pcie_hwops; |
| 42 | |
Paul Mundt | 7561f2d | 2010-02-08 16:36:56 +0900 | [diff] [blame] | 43 | static struct resource sh7786_pci0_resources[] = { |
Paul Mundt | 5713e60 | 2009-06-17 18:20:48 +0900 | [diff] [blame] | 44 | { |
Thomas Petazzoni | 5da1bb9 | 2017-12-04 16:09:05 +0100 | [diff] [blame] | 45 | .name = "PCIe0 MEM 0", |
Paul Mundt | 7561f2d | 2010-02-08 16:36:56 +0900 | [diff] [blame] | 46 | .start = 0xfd000000, |
| 47 | .end = 0xfd000000 + SZ_8M - 1, |
Thomas Petazzoni | 5da1bb9 | 2017-12-04 16:09:05 +0100 | [diff] [blame] | 48 | .flags = IORESOURCE_MEM, |
Paul Mundt | 7561f2d | 2010-02-08 16:36:56 +0900 | [diff] [blame] | 49 | }, { |
Thomas Petazzoni | 5da1bb9 | 2017-12-04 16:09:05 +0100 | [diff] [blame] | 50 | .name = "PCIe0 MEM 1", |
Paul Mundt | 7561f2d | 2010-02-08 16:36:56 +0900 | [diff] [blame] | 51 | .start = 0xc0000000, |
| 52 | .end = 0xc0000000 + SZ_512M - 1, |
| 53 | .flags = IORESOURCE_MEM | IORESOURCE_MEM_32BIT, |
| 54 | }, { |
Thomas Petazzoni | 5da1bb9 | 2017-12-04 16:09:05 +0100 | [diff] [blame] | 55 | .name = "PCIe0 MEM 2", |
Paul Mundt | 7561f2d | 2010-02-08 16:36:56 +0900 | [diff] [blame] | 56 | .start = 0x10000000, |
| 57 | .end = 0x10000000 + SZ_64M - 1, |
Paul Mundt | 5713e60 | 2009-06-17 18:20:48 +0900 | [diff] [blame] | 58 | .flags = IORESOURCE_MEM, |
| 59 | }, { |
Thomas Petazzoni | 5da1bb9 | 2017-12-04 16:09:05 +0100 | [diff] [blame] | 60 | .name = "PCIe0 IO", |
Paul Mundt | 7561f2d | 2010-02-08 16:36:56 +0900 | [diff] [blame] | 61 | .start = 0xfe100000, |
| 62 | .end = 0xfe100000 + SZ_1M - 1, |
Thomas Petazzoni | 5da1bb9 | 2017-12-04 16:09:05 +0100 | [diff] [blame] | 63 | .flags = IORESOURCE_IO, |
Paul Mundt | 5713e60 | 2009-06-17 18:20:48 +0900 | [diff] [blame] | 64 | }, |
| 65 | }; |
| 66 | |
Paul Mundt | 7561f2d | 2010-02-08 16:36:56 +0900 | [diff] [blame] | 67 | static struct resource sh7786_pci1_resources[] = { |
| 68 | { |
Thomas Petazzoni | 5da1bb9 | 2017-12-04 16:09:05 +0100 | [diff] [blame] | 69 | .name = "PCIe1 MEM 0", |
Paul Mundt | 7561f2d | 2010-02-08 16:36:56 +0900 | [diff] [blame] | 70 | .start = 0xfd800000, |
| 71 | .end = 0xfd800000 + SZ_8M - 1, |
Thomas Petazzoni | 5da1bb9 | 2017-12-04 16:09:05 +0100 | [diff] [blame] | 72 | .flags = IORESOURCE_MEM, |
Paul Mundt | 7561f2d | 2010-02-08 16:36:56 +0900 | [diff] [blame] | 73 | }, { |
Thomas Petazzoni | 5da1bb9 | 2017-12-04 16:09:05 +0100 | [diff] [blame] | 74 | .name = "PCIe1 MEM 1", |
Paul Mundt | 7561f2d | 2010-02-08 16:36:56 +0900 | [diff] [blame] | 75 | .start = 0xa0000000, |
| 76 | .end = 0xa0000000 + SZ_512M - 1, |
| 77 | .flags = IORESOURCE_MEM | IORESOURCE_MEM_32BIT, |
| 78 | }, { |
Thomas Petazzoni | 5da1bb9 | 2017-12-04 16:09:05 +0100 | [diff] [blame] | 79 | .name = "PCIe1 MEM 2", |
Paul Mundt | 7561f2d | 2010-02-08 16:36:56 +0900 | [diff] [blame] | 80 | .start = 0x30000000, |
| 81 | .end = 0x30000000 + SZ_256M - 1, |
| 82 | .flags = IORESOURCE_MEM | IORESOURCE_MEM_32BIT, |
| 83 | }, { |
Thomas Petazzoni | 5da1bb9 | 2017-12-04 16:09:05 +0100 | [diff] [blame] | 84 | .name = "PCIe1 IO", |
Paul Mundt | 7561f2d | 2010-02-08 16:36:56 +0900 | [diff] [blame] | 85 | .start = 0xfe300000, |
| 86 | .end = 0xfe300000 + SZ_1M - 1, |
Thomas Petazzoni | 5da1bb9 | 2017-12-04 16:09:05 +0100 | [diff] [blame] | 87 | .flags = IORESOURCE_IO, |
Paul Mundt | 7561f2d | 2010-02-08 16:36:56 +0900 | [diff] [blame] | 88 | }, |
Paul Mundt | 5713e60 | 2009-06-17 18:20:48 +0900 | [diff] [blame] | 89 | }; |
| 90 | |
Paul Mundt | 7561f2d | 2010-02-08 16:36:56 +0900 | [diff] [blame] | 91 | static struct resource sh7786_pci2_resources[] = { |
Paul Mundt | 5713e60 | 2009-06-17 18:20:48 +0900 | [diff] [blame] | 92 | { |
Thomas Petazzoni | 5da1bb9 | 2017-12-04 16:09:05 +0100 | [diff] [blame] | 93 | .name = "PCIe2 MEM 0", |
Paul Mundt | 7561f2d | 2010-02-08 16:36:56 +0900 | [diff] [blame] | 94 | .start = 0xfc800000, |
| 95 | .end = 0xfc800000 + SZ_4M - 1, |
Thomas Petazzoni | 5da1bb9 | 2017-12-04 16:09:05 +0100 | [diff] [blame] | 96 | .flags = IORESOURCE_MEM, |
Paul Mundt | 5713e60 | 2009-06-17 18:20:48 +0900 | [diff] [blame] | 97 | }, { |
Thomas Petazzoni | 5da1bb9 | 2017-12-04 16:09:05 +0100 | [diff] [blame] | 98 | .name = "PCIe2 MEM 1", |
Paul Mundt | 7561f2d | 2010-02-08 16:36:56 +0900 | [diff] [blame] | 99 | .start = 0x80000000, |
| 100 | .end = 0x80000000 + SZ_512M - 1, |
| 101 | .flags = IORESOURCE_MEM | IORESOURCE_MEM_32BIT, |
Paul Mundt | 5713e60 | 2009-06-17 18:20:48 +0900 | [diff] [blame] | 102 | }, { |
Thomas Petazzoni | 5da1bb9 | 2017-12-04 16:09:05 +0100 | [diff] [blame] | 103 | .name = "PCIe2 MEM 2", |
Paul Mundt | 7561f2d | 2010-02-08 16:36:56 +0900 | [diff] [blame] | 104 | .start = 0x20000000, |
| 105 | .end = 0x20000000 + SZ_256M - 1, |
| 106 | .flags = IORESOURCE_MEM | IORESOURCE_MEM_32BIT, |
| 107 | }, { |
Thomas Petazzoni | 5da1bb9 | 2017-12-04 16:09:05 +0100 | [diff] [blame] | 108 | .name = "PCIe2 IO", |
Paul Mundt | 7561f2d | 2010-02-08 16:36:56 +0900 | [diff] [blame] | 109 | .start = 0xfcd00000, |
| 110 | .end = 0xfcd00000 + SZ_1M - 1, |
Thomas Petazzoni | 5da1bb9 | 2017-12-04 16:09:05 +0100 | [diff] [blame] | 111 | .flags = IORESOURCE_IO, |
Paul Mundt | 5713e60 | 2009-06-17 18:20:48 +0900 | [diff] [blame] | 112 | }, |
| 113 | }; |
| 114 | |
| 115 | extern struct pci_ops sh7786_pci_ops; |
| 116 | |
Paul Mundt | 7561f2d | 2010-02-08 16:36:56 +0900 | [diff] [blame] | 117 | #define DEFINE_CONTROLLER(start, idx) \ |
| 118 | { \ |
| 119 | .pci_ops = &sh7786_pci_ops, \ |
| 120 | .resources = sh7786_pci##idx##_resources, \ |
| 121 | .nr_resources = ARRAY_SIZE(sh7786_pci##idx##_resources), \ |
| 122 | .reg_base = start, \ |
| 123 | .mem_offset = 0, \ |
| 124 | .io_offset = 0, \ |
Paul Mundt | 5713e60 | 2009-06-17 18:20:48 +0900 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | static struct pci_channel sh7786_pci_channels[] = { |
| 128 | DEFINE_CONTROLLER(0xfe000000, 0), |
| 129 | DEFINE_CONTROLLER(0xfe200000, 1), |
| 130 | DEFINE_CONTROLLER(0xfcc00000, 2), |
| 131 | }; |
| 132 | |
Paul Mundt | c524ebf | 2010-09-20 18:45:11 +0900 | [diff] [blame] | 133 | static struct clk fixed_pciexclkp = { |
| 134 | .rate = 100000000, /* 100 MHz reference clock */ |
| 135 | }; |
| 136 | |
Greg Kroah-Hartman | b881bc4 | 2012-12-21 14:06:37 -0800 | [diff] [blame] | 137 | static void sh7786_pci_fixup(struct pci_dev *dev) |
Paul Mundt | 2c65d75 | 2010-09-20 15:39:54 +0900 | [diff] [blame] | 138 | { |
| 139 | /* |
| 140 | * Prevent enumeration of root complex resources. |
| 141 | */ |
| 142 | if (pci_is_root_bus(dev->bus) && dev->devfn == 0) { |
| 143 | int i; |
| 144 | |
| 145 | for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) { |
| 146 | dev->resource[i].start = 0; |
| 147 | dev->resource[i].end = 0; |
| 148 | dev->resource[i].flags = 0; |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_RENESAS, PCI_DEVICE_ID_RENESAS_SH7786, |
| 153 | sh7786_pci_fixup); |
| 154 | |
Paul Mundt | c524ebf | 2010-09-20 18:45:11 +0900 | [diff] [blame] | 155 | static int __init phy_wait_for_ack(struct pci_channel *chan) |
Paul Mundt | 5713e60 | 2009-06-17 18:20:48 +0900 | [diff] [blame] | 156 | { |
| 157 | unsigned int timeout = 100; |
| 158 | |
| 159 | while (timeout--) { |
| 160 | if (pci_read_reg(chan, SH4A_PCIEPHYADRR) & (1 << BITS_ACK)) |
| 161 | return 0; |
| 162 | |
| 163 | udelay(100); |
| 164 | } |
| 165 | |
| 166 | return -ETIMEDOUT; |
| 167 | } |
| 168 | |
Paul Mundt | c524ebf | 2010-09-20 18:45:11 +0900 | [diff] [blame] | 169 | static int __init pci_wait_for_irq(struct pci_channel *chan, unsigned int mask) |
Paul Mundt | 5713e60 | 2009-06-17 18:20:48 +0900 | [diff] [blame] | 170 | { |
| 171 | unsigned int timeout = 100; |
| 172 | |
| 173 | while (timeout--) { |
| 174 | if ((pci_read_reg(chan, SH4A_PCIEINTR) & mask) == mask) |
| 175 | return 0; |
| 176 | |
| 177 | udelay(100); |
| 178 | } |
| 179 | |
| 180 | return -ETIMEDOUT; |
| 181 | } |
| 182 | |
Paul Mundt | c524ebf | 2010-09-20 18:45:11 +0900 | [diff] [blame] | 183 | static void __init phy_write_reg(struct pci_channel *chan, unsigned int addr, |
| 184 | unsigned int lane, unsigned int data) |
Paul Mundt | 5713e60 | 2009-06-17 18:20:48 +0900 | [diff] [blame] | 185 | { |
Paul Mundt | 53178d7 | 2010-08-20 16:04:59 +0900 | [diff] [blame] | 186 | unsigned long phyaddr; |
Paul Mundt | 5713e60 | 2009-06-17 18:20:48 +0900 | [diff] [blame] | 187 | |
| 188 | phyaddr = (1 << BITS_CMD) + ((lane & 0xf) << BITS_LANE) + |
| 189 | ((addr & 0xff) << BITS_ADR); |
| 190 | |
Paul Mundt | 5713e60 | 2009-06-17 18:20:48 +0900 | [diff] [blame] | 191 | /* Set write data */ |
| 192 | pci_write_reg(chan, data, SH4A_PCIEPHYDOUTR); |
| 193 | pci_write_reg(chan, phyaddr, SH4A_PCIEPHYADRR); |
| 194 | |
| 195 | phy_wait_for_ack(chan); |
| 196 | |
| 197 | /* Clear command */ |
Paul Mundt | 53178d7 | 2010-08-20 16:04:59 +0900 | [diff] [blame] | 198 | pci_write_reg(chan, 0, SH4A_PCIEPHYDOUTR); |
Paul Mundt | 5713e60 | 2009-06-17 18:20:48 +0900 | [diff] [blame] | 199 | pci_write_reg(chan, 0, SH4A_PCIEPHYADRR); |
| 200 | |
| 201 | phy_wait_for_ack(chan); |
Paul Mundt | 5713e60 | 2009-06-17 18:20:48 +0900 | [diff] [blame] | 202 | } |
| 203 | |
Paul Mundt | c524ebf | 2010-09-20 18:45:11 +0900 | [diff] [blame] | 204 | static int __init pcie_clk_init(struct sh7786_pcie_port *port) |
Paul Mundt | 5713e60 | 2009-06-17 18:20:48 +0900 | [diff] [blame] | 205 | { |
Paul Mundt | c524ebf | 2010-09-20 18:45:11 +0900 | [diff] [blame] | 206 | struct pci_channel *chan = port->hose; |
| 207 | struct clk *clk; |
| 208 | char fclk_name[16]; |
| 209 | int ret; |
| 210 | |
| 211 | /* |
| 212 | * First register the fixed clock |
| 213 | */ |
| 214 | ret = clk_register(&fixed_pciexclkp); |
| 215 | if (unlikely(ret != 0)) |
| 216 | return ret; |
| 217 | |
| 218 | /* |
| 219 | * Grab the port's function clock, which the PHY clock depends |
| 220 | * on. clock lookups don't help us much at this point, since no |
| 221 | * dev_id is available this early. Lame. |
| 222 | */ |
| 223 | snprintf(fclk_name, sizeof(fclk_name), "pcie%d_fck", port->index); |
| 224 | |
| 225 | port->fclk = clk_get(NULL, fclk_name); |
| 226 | if (IS_ERR(port->fclk)) { |
| 227 | ret = PTR_ERR(port->fclk); |
| 228 | goto err_fclk; |
| 229 | } |
| 230 | |
| 231 | clk_enable(port->fclk); |
| 232 | |
| 233 | /* |
| 234 | * And now, set up the PHY clock |
| 235 | */ |
| 236 | clk = &port->phy_clk; |
| 237 | |
| 238 | memset(clk, 0, sizeof(struct clk)); |
| 239 | |
| 240 | clk->parent = &fixed_pciexclkp; |
| 241 | clk->enable_reg = (void __iomem *)(chan->reg_base + SH4A_PCIEPHYCTLR); |
| 242 | clk->enable_bit = BITS_CKE; |
| 243 | |
Nobuhiro Iwamatsu | ad3337c | 2012-06-27 09:59:40 +0900 | [diff] [blame] | 244 | ret = sh_clk_mstp_register(clk, 1); |
Paul Mundt | c524ebf | 2010-09-20 18:45:11 +0900 | [diff] [blame] | 245 | if (unlikely(ret < 0)) |
| 246 | goto err_phy; |
| 247 | |
| 248 | return 0; |
| 249 | |
| 250 | err_phy: |
| 251 | clk_disable(port->fclk); |
| 252 | clk_put(port->fclk); |
| 253 | err_fclk: |
| 254 | clk_unregister(&fixed_pciexclkp); |
| 255 | |
| 256 | return ret; |
| 257 | } |
| 258 | |
| 259 | static int __init phy_init(struct sh7786_pcie_port *port) |
| 260 | { |
| 261 | struct pci_channel *chan = port->hose; |
Paul Mundt | 5713e60 | 2009-06-17 18:20:48 +0900 | [diff] [blame] | 262 | unsigned int timeout = 100; |
| 263 | |
Paul Mundt | c524ebf | 2010-09-20 18:45:11 +0900 | [diff] [blame] | 264 | clk_enable(&port->phy_clk); |
Paul Mundt | 53178d7 | 2010-08-20 16:04:59 +0900 | [diff] [blame] | 265 | |
Paul Mundt | 5713e60 | 2009-06-17 18:20:48 +0900 | [diff] [blame] | 266 | /* Initialize the phy */ |
| 267 | phy_write_reg(chan, 0x60, 0xf, 0x004b008b); |
| 268 | phy_write_reg(chan, 0x61, 0xf, 0x00007b41); |
| 269 | phy_write_reg(chan, 0x64, 0xf, 0x00ff4f00); |
| 270 | phy_write_reg(chan, 0x65, 0xf, 0x09070907); |
| 271 | phy_write_reg(chan, 0x66, 0xf, 0x00000010); |
| 272 | phy_write_reg(chan, 0x74, 0xf, 0x0007001c); |
| 273 | phy_write_reg(chan, 0x79, 0xf, 0x01fc000d); |
Paul Mundt | 53178d7 | 2010-08-20 16:04:59 +0900 | [diff] [blame] | 274 | phy_write_reg(chan, 0xb0, 0xf, 0x00000610); |
Paul Mundt | 5713e60 | 2009-06-17 18:20:48 +0900 | [diff] [blame] | 275 | |
| 276 | /* Deassert Standby */ |
Paul Mundt | 53178d7 | 2010-08-20 16:04:59 +0900 | [diff] [blame] | 277 | phy_write_reg(chan, 0x67, 0x1, 0x00000400); |
| 278 | |
| 279 | /* Disable clock */ |
Paul Mundt | c524ebf | 2010-09-20 18:45:11 +0900 | [diff] [blame] | 280 | clk_disable(&port->phy_clk); |
Paul Mundt | 5713e60 | 2009-06-17 18:20:48 +0900 | [diff] [blame] | 281 | |
| 282 | while (timeout--) { |
| 283 | if (pci_read_reg(chan, SH4A_PCIEPHYSR)) |
| 284 | return 0; |
| 285 | |
| 286 | udelay(100); |
| 287 | } |
| 288 | |
| 289 | return -ETIMEDOUT; |
| 290 | } |
| 291 | |
Paul Mundt | c524ebf | 2010-09-20 18:45:11 +0900 | [diff] [blame] | 292 | static void __init pcie_reset(struct sh7786_pcie_port *port) |
Paul Mundt | 2dbfa1e | 2010-09-07 16:11:04 +0900 | [diff] [blame] | 293 | { |
| 294 | struct pci_channel *chan = port->hose; |
| 295 | |
| 296 | pci_write_reg(chan, 1, SH4A_PCIESRSTR); |
| 297 | pci_write_reg(chan, 0, SH4A_PCIETCTLR); |
| 298 | pci_write_reg(chan, 0, SH4A_PCIESRSTR); |
| 299 | pci_write_reg(chan, 0, SH4A_PCIETXVC0SR); |
| 300 | } |
| 301 | |
Paul Mundt | c524ebf | 2010-09-20 18:45:11 +0900 | [diff] [blame] | 302 | static int __init pcie_init(struct sh7786_pcie_port *port) |
Paul Mundt | 5713e60 | 2009-06-17 18:20:48 +0900 | [diff] [blame] | 303 | { |
| 304 | struct pci_channel *chan = port->hose; |
| 305 | unsigned int data; |
Thomas Petazzoni | 79e1c5e | 2017-12-04 16:09:06 +0100 | [diff] [blame] | 306 | phys_addr_t memstart, memend; |
Paul Mundt | 7578a4c | 2010-02-10 16:00:58 +0900 | [diff] [blame] | 307 | size_t memsize; |
Paul Mundt | da03a63 | 2010-09-07 17:03:10 +0900 | [diff] [blame] | 308 | int ret, i, win; |
Paul Mundt | 5713e60 | 2009-06-17 18:20:48 +0900 | [diff] [blame] | 309 | |
| 310 | /* Begin initialization */ |
Paul Mundt | 2dbfa1e | 2010-09-07 16:11:04 +0900 | [diff] [blame] | 311 | pcie_reset(port); |
Paul Mundt | 5713e60 | 2009-06-17 18:20:48 +0900 | [diff] [blame] | 312 | |
Paul Mundt | 2c65d75 | 2010-09-20 15:39:54 +0900 | [diff] [blame] | 313 | /* |
| 314 | * Initial header for port config space is type 1, set the device |
| 315 | * class to match. Hardware takes care of propagating the IDSETR |
| 316 | * settings, so there is no need to bother with a quirk. |
| 317 | */ |
| 318 | pci_write_reg(chan, PCI_CLASS_BRIDGE_PCI << 16, SH4A_PCIEIDSETR1); |
Paul Mundt | 5713e60 | 2009-06-17 18:20:48 +0900 | [diff] [blame] | 319 | |
| 320 | /* Initialize default capabilities. */ |
| 321 | data = pci_read_reg(chan, SH4A_PCIEEXPCAP0); |
| 322 | data &= ~(PCI_EXP_FLAGS_TYPE << 16); |
| 323 | |
| 324 | if (port->endpoint) |
| 325 | data |= PCI_EXP_TYPE_ENDPOINT << 20; |
| 326 | else |
| 327 | data |= PCI_EXP_TYPE_ROOT_PORT << 20; |
| 328 | |
| 329 | data |= PCI_CAP_ID_EXP; |
| 330 | pci_write_reg(chan, data, SH4A_PCIEEXPCAP0); |
| 331 | |
Paul Mundt | 7578a4c | 2010-02-10 16:00:58 +0900 | [diff] [blame] | 332 | /* Enable data link layer active state reporting */ |
| 333 | pci_write_reg(chan, PCI_EXP_LNKCAP_DLLLARC, SH4A_PCIEEXPCAP3); |
| 334 | |
| 335 | /* Enable extended sync and ASPM L0s support */ |
Paul Mundt | 5713e60 | 2009-06-17 18:20:48 +0900 | [diff] [blame] | 336 | data = pci_read_reg(chan, SH4A_PCIEEXPCAP4); |
Paul Mundt | 7578a4c | 2010-02-10 16:00:58 +0900 | [diff] [blame] | 337 | data &= ~PCI_EXP_LNKCTL_ASPMC; |
| 338 | data |= PCI_EXP_LNKCTL_ES | 1; |
Paul Mundt | 5713e60 | 2009-06-17 18:20:48 +0900 | [diff] [blame] | 339 | pci_write_reg(chan, data, SH4A_PCIEEXPCAP4); |
| 340 | |
Paul Mundt | 7578a4c | 2010-02-10 16:00:58 +0900 | [diff] [blame] | 341 | /* Write out the physical slot number */ |
| 342 | data = pci_read_reg(chan, SH4A_PCIEEXPCAP5); |
| 343 | data &= ~PCI_EXP_SLTCAP_PSN; |
| 344 | data |= (port->index + 1) << 19; |
| 345 | pci_write_reg(chan, data, SH4A_PCIEEXPCAP5); |
| 346 | |
Paul Mundt | 5713e60 | 2009-06-17 18:20:48 +0900 | [diff] [blame] | 347 | /* Set the completion timer timeout to the maximum 32ms. */ |
| 348 | data = pci_read_reg(chan, SH4A_PCIETLCTLR); |
Paul Mundt | 7578a4c | 2010-02-10 16:00:58 +0900 | [diff] [blame] | 349 | data &= ~0x3f00; |
Paul Mundt | 5713e60 | 2009-06-17 18:20:48 +0900 | [diff] [blame] | 350 | data |= 0x32 << 8; |
| 351 | pci_write_reg(chan, data, SH4A_PCIETLCTLR); |
| 352 | |
| 353 | /* |
| 354 | * Set fast training sequences to the maximum 255, |
| 355 | * and enable MAC data scrambling. |
| 356 | */ |
| 357 | data = pci_read_reg(chan, SH4A_PCIEMACCTLR); |
| 358 | data &= ~PCIEMACCTLR_SCR_DIS; |
| 359 | data |= (0xff << 16); |
| 360 | pci_write_reg(chan, data, SH4A_PCIEMACCTLR); |
| 361 | |
Thomas Petazzoni | 79e1c5e | 2017-12-04 16:09:06 +0100 | [diff] [blame] | 362 | memstart = __pa(memory_start); |
| 363 | memend = __pa(memory_end); |
| 364 | memsize = roundup_pow_of_two(memend - memstart); |
| 365 | |
| 366 | /* |
| 367 | * The start address must be aligned on its size. So we round |
| 368 | * it down, and then recalculate the size so that it covers |
| 369 | * the entire memory. |
| 370 | */ |
| 371 | memstart = ALIGN_DOWN(memstart, memsize); |
| 372 | memsize = roundup_pow_of_two(memend - memstart); |
Paul Mundt | 7578a4c | 2010-02-10 16:00:58 +0900 | [diff] [blame] | 373 | |
Thomas Petazzoni | bf9c7e3 | 2017-12-04 16:09:07 +0100 | [diff] [blame] | 374 | dma_pfn_offset = memstart >> PAGE_SHIFT; |
| 375 | |
Paul Mundt | 7578a4c | 2010-02-10 16:00:58 +0900 | [diff] [blame] | 376 | /* |
| 377 | * If there's more than 512MB of memory, we need to roll over to |
| 378 | * LAR1/LAMR1. |
| 379 | */ |
| 380 | if (memsize > SZ_512M) { |
Thomas Petazzoni | 79e1c5e | 2017-12-04 16:09:06 +0100 | [diff] [blame] | 381 | pci_write_reg(chan, memstart + SZ_512M, SH4A_PCIELAR1); |
Paul Mundt | cecf48e | 2010-09-20 17:10:02 +0900 | [diff] [blame] | 382 | pci_write_reg(chan, ((memsize - SZ_512M) - SZ_256) | 1, |
| 383 | SH4A_PCIELAMR1); |
Paul Mundt | 7578a4c | 2010-02-10 16:00:58 +0900 | [diff] [blame] | 384 | memsize = SZ_512M; |
| 385 | } else { |
| 386 | /* |
| 387 | * Otherwise just zero it out and disable it. |
| 388 | */ |
Paul Mundt | cecf48e | 2010-09-20 17:10:02 +0900 | [diff] [blame] | 389 | pci_write_reg(chan, 0, SH4A_PCIELAR1); |
| 390 | pci_write_reg(chan, 0, SH4A_PCIELAMR1); |
Paul Mundt | 7578a4c | 2010-02-10 16:00:58 +0900 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | /* |
| 394 | * LAR0/LAMR0 covers up to the first 512MB, which is enough to |
| 395 | * cover all of lowmem on most platforms. |
| 396 | */ |
Thomas Petazzoni | 79e1c5e | 2017-12-04 16:09:06 +0100 | [diff] [blame] | 397 | pci_write_reg(chan, memstart, SH4A_PCIELAR0); |
Paul Mundt | cecf48e | 2010-09-20 17:10:02 +0900 | [diff] [blame] | 398 | pci_write_reg(chan, (memsize - SZ_256) | 1, SH4A_PCIELAMR0); |
Paul Mundt | 7578a4c | 2010-02-10 16:00:58 +0900 | [diff] [blame] | 399 | |
Paul Mundt | 5713e60 | 2009-06-17 18:20:48 +0900 | [diff] [blame] | 400 | /* Finish initialization */ |
| 401 | data = pci_read_reg(chan, SH4A_PCIETCTLR); |
| 402 | data |= 0x1; |
| 403 | pci_write_reg(chan, data, SH4A_PCIETCTLR); |
| 404 | |
Paul Mundt | 81df84f | 2010-09-19 13:57:51 +0900 | [diff] [blame] | 405 | /* Let things settle down a bit.. */ |
| 406 | mdelay(100); |
| 407 | |
Paul Mundt | 5713e60 | 2009-06-17 18:20:48 +0900 | [diff] [blame] | 408 | /* Enable DL_Active Interrupt generation */ |
| 409 | data = pci_read_reg(chan, SH4A_PCIEDLINTENR); |
| 410 | data |= PCIEDLINTENR_DLL_ACT_ENABLE; |
| 411 | pci_write_reg(chan, data, SH4A_PCIEDLINTENR); |
| 412 | |
| 413 | /* Disable MAC data scrambling. */ |
| 414 | data = pci_read_reg(chan, SH4A_PCIEMACCTLR); |
| 415 | data |= PCIEMACCTLR_SCR_DIS | (0xff << 16); |
| 416 | pci_write_reg(chan, data, SH4A_PCIEMACCTLR); |
| 417 | |
Paul Mundt | bd792ae | 2010-09-20 16:12:58 +0900 | [diff] [blame] | 418 | /* |
| 419 | * This will timeout if we don't have a link, but we permit the |
| 420 | * port to register anyways in order to support hotplug on future |
| 421 | * hardware. |
| 422 | */ |
Paul Mundt | 5713e60 | 2009-06-17 18:20:48 +0900 | [diff] [blame] | 423 | ret = pci_wait_for_irq(chan, MASK_INT_TX_CTRL); |
Paul Mundt | 5713e60 | 2009-06-17 18:20:48 +0900 | [diff] [blame] | 424 | |
Paul Mundt | 7578a4c | 2010-02-10 16:00:58 +0900 | [diff] [blame] | 425 | data = pci_read_reg(chan, SH4A_PCIEPCICONF1); |
| 426 | data &= ~(PCI_STATUS_DEVSEL_MASK << 16); |
| 427 | data |= PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER | |
| 428 | (PCI_STATUS_CAP_LIST | PCI_STATUS_DEVSEL_FAST) << 16; |
| 429 | pci_write_reg(chan, data, SH4A_PCIEPCICONF1); |
| 430 | |
Paul Mundt | 5713e60 | 2009-06-17 18:20:48 +0900 | [diff] [blame] | 431 | pci_write_reg(chan, 0x80888000, SH4A_PCIETXVC0DCTLR); |
| 432 | pci_write_reg(chan, 0x00222000, SH4A_PCIERXVC0DCTLR); |
Paul Mundt | 5713e60 | 2009-06-17 18:20:48 +0900 | [diff] [blame] | 433 | |
| 434 | wmb(); |
| 435 | |
Paul Mundt | bd792ae | 2010-09-20 16:12:58 +0900 | [diff] [blame] | 436 | if (ret == 0) { |
| 437 | data = pci_read_reg(chan, SH4A_PCIEMACSR); |
| 438 | printk(KERN_NOTICE "PCI: PCIe#%d x%d link detected\n", |
| 439 | port->index, (data >> 20) & 0x3f); |
| 440 | } else |
| 441 | printk(KERN_NOTICE "PCI: PCIe#%d link down\n", |
| 442 | port->index); |
Paul Mundt | 5713e60 | 2009-06-17 18:20:48 +0900 | [diff] [blame] | 443 | |
Paul Mundt | da03a63 | 2010-09-07 17:03:10 +0900 | [diff] [blame] | 444 | for (i = win = 0; i < chan->nr_resources; i++) { |
Paul Mundt | 7578a4c | 2010-02-10 16:00:58 +0900 | [diff] [blame] | 445 | struct resource *res = chan->resources + i; |
| 446 | resource_size_t size; |
Paul Mundt | cecf48e | 2010-09-20 17:10:02 +0900 | [diff] [blame] | 447 | u32 mask; |
Paul Mundt | 7578a4c | 2010-02-10 16:00:58 +0900 | [diff] [blame] | 448 | |
Paul Mundt | da03a63 | 2010-09-07 17:03:10 +0900 | [diff] [blame] | 449 | /* |
| 450 | * We can't use the 32-bit mode windows in legacy 29-bit |
| 451 | * mode, so just skip them entirely. |
| 452 | */ |
| 453 | if ((res->flags & IORESOURCE_MEM_32BIT) && __in_29bit_mode()) |
Thomas Petazzoni | 7dd7f69 | 2017-12-04 16:09:03 +0100 | [diff] [blame] | 454 | res->flags |= IORESOURCE_DISABLED; |
| 455 | |
| 456 | if (res->flags & IORESOURCE_DISABLED) |
Paul Mundt | da03a63 | 2010-09-07 17:03:10 +0900 | [diff] [blame] | 457 | continue; |
| 458 | |
| 459 | pci_write_reg(chan, 0x00000000, SH4A_PCIEPTCTLR(win)); |
Paul Mundt | 7578a4c | 2010-02-10 16:00:58 +0900 | [diff] [blame] | 460 | |
Paul Mundt | 7578a4c | 2010-02-10 16:00:58 +0900 | [diff] [blame] | 461 | /* |
| 462 | * The PAMR mask is calculated in units of 256kB, which |
| 463 | * keeps things pretty simple. |
| 464 | */ |
Paul Mundt | cecf48e | 2010-09-20 17:10:02 +0900 | [diff] [blame] | 465 | size = resource_size(res); |
| 466 | mask = (roundup_pow_of_two(size) / SZ_256K) - 1; |
| 467 | pci_write_reg(chan, mask << 18, SH4A_PCIEPAMR(win)); |
Paul Mundt | 7578a4c | 2010-02-10 16:00:58 +0900 | [diff] [blame] | 468 | |
Paul Mundt | a80be168 | 2010-10-15 06:15:56 +0900 | [diff] [blame] | 469 | pci_write_reg(chan, upper_32_bits(res->start), |
Paul Mundt | cecf48e | 2010-09-20 17:10:02 +0900 | [diff] [blame] | 470 | SH4A_PCIEPARH(win)); |
Paul Mundt | a80be168 | 2010-10-15 06:15:56 +0900 | [diff] [blame] | 471 | pci_write_reg(chan, lower_32_bits(res->start), |
Paul Mundt | cecf48e | 2010-09-20 17:10:02 +0900 | [diff] [blame] | 472 | SH4A_PCIEPARL(win)); |
Paul Mundt | 7578a4c | 2010-02-10 16:00:58 +0900 | [diff] [blame] | 473 | |
Paul Mundt | cecf48e | 2010-09-20 17:10:02 +0900 | [diff] [blame] | 474 | mask = MASK_PARE; |
Paul Mundt | 7578a4c | 2010-02-10 16:00:58 +0900 | [diff] [blame] | 475 | if (res->flags & IORESOURCE_IO) |
Paul Mundt | cecf48e | 2010-09-20 17:10:02 +0900 | [diff] [blame] | 476 | mask |= MASK_SPC; |
Paul Mundt | 7578a4c | 2010-02-10 16:00:58 +0900 | [diff] [blame] | 477 | |
Paul Mundt | cecf48e | 2010-09-20 17:10:02 +0900 | [diff] [blame] | 478 | pci_write_reg(chan, mask, SH4A_PCIEPTCTLR(win)); |
Paul Mundt | da03a63 | 2010-09-07 17:03:10 +0900 | [diff] [blame] | 479 | |
| 480 | win++; |
Paul Mundt | 7578a4c | 2010-02-10 16:00:58 +0900 | [diff] [blame] | 481 | } |
Paul Mundt | 5713e60 | 2009-06-17 18:20:48 +0900 | [diff] [blame] | 482 | |
| 483 | return 0; |
| 484 | } |
| 485 | |
Matthew Minter | 2b8ff9f | 2017-07-31 17:37:49 +0100 | [diff] [blame] | 486 | int pcibios_map_platform_irq(const struct pci_dev *pdev, u8 slot, u8 pin) |
Paul Mundt | 5713e60 | 2009-06-17 18:20:48 +0900 | [diff] [blame] | 487 | { |
Paul Mundt | 58796ce | 2012-05-18 17:42:29 +0900 | [diff] [blame] | 488 | return evt2irq(0xae0); |
Paul Mundt | 5713e60 | 2009-06-17 18:20:48 +0900 | [diff] [blame] | 489 | } |
| 490 | |
Thomas Petazzoni | bf9c7e3 | 2017-12-04 16:09:07 +0100 | [diff] [blame] | 491 | void pcibios_bus_add_device(struct pci_dev *pdev) |
| 492 | { |
| 493 | pdev->dev.dma_pfn_offset = dma_pfn_offset; |
| 494 | } |
| 495 | |
Paul Mundt | c524ebf | 2010-09-20 18:45:11 +0900 | [diff] [blame] | 496 | static int __init sh7786_pcie_core_init(void) |
Paul Mundt | 5713e60 | 2009-06-17 18:20:48 +0900 | [diff] [blame] | 497 | { |
| 498 | /* Return the number of ports */ |
| 499 | return test_mode_pin(MODE_PIN12) ? 3 : 2; |
| 500 | } |
| 501 | |
Paul Mundt | 1da09c4 | 2011-01-18 19:56:04 +0900 | [diff] [blame] | 502 | static void __init sh7786_pcie_init_hw(void *data, async_cookie_t cookie) |
Paul Mundt | 5713e60 | 2009-06-17 18:20:48 +0900 | [diff] [blame] | 503 | { |
Paul Mundt | 1da09c4 | 2011-01-18 19:56:04 +0900 | [diff] [blame] | 504 | struct sh7786_pcie_port *port = data; |
Paul Mundt | 5713e60 | 2009-06-17 18:20:48 +0900 | [diff] [blame] | 505 | int ret; |
| 506 | |
Paul Mundt | 5713e60 | 2009-06-17 18:20:48 +0900 | [diff] [blame] | 507 | /* |
| 508 | * Check if we are configured in endpoint or root complex mode, |
| 509 | * this is a fixed pin setting that applies to all PCIe ports. |
| 510 | */ |
| 511 | port->endpoint = test_mode_pin(MODE_PIN11); |
| 512 | |
Paul Mundt | c524ebf | 2010-09-20 18:45:11 +0900 | [diff] [blame] | 513 | /* |
| 514 | * Setup clocks, needed both for PHY and PCIe registers. |
| 515 | */ |
| 516 | ret = pcie_clk_init(port); |
Paul Mundt | 1da09c4 | 2011-01-18 19:56:04 +0900 | [diff] [blame] | 517 | if (unlikely(ret < 0)) { |
| 518 | pr_err("clock initialization failed for port#%d\n", |
| 519 | port->index); |
| 520 | return; |
| 521 | } |
Paul Mundt | c524ebf | 2010-09-20 18:45:11 +0900 | [diff] [blame] | 522 | |
| 523 | ret = phy_init(port); |
Paul Mundt | 1da09c4 | 2011-01-18 19:56:04 +0900 | [diff] [blame] | 524 | if (unlikely(ret < 0)) { |
| 525 | pr_err("phy initialization failed for port#%d\n", |
| 526 | port->index); |
| 527 | return; |
| 528 | } |
Paul Mundt | c524ebf | 2010-09-20 18:45:11 +0900 | [diff] [blame] | 529 | |
Paul Mundt | 5713e60 | 2009-06-17 18:20:48 +0900 | [diff] [blame] | 530 | ret = pcie_init(port); |
Paul Mundt | 1da09c4 | 2011-01-18 19:56:04 +0900 | [diff] [blame] | 531 | if (unlikely(ret < 0)) { |
| 532 | pr_err("core initialization failed for port#%d\n", |
| 533 | port->index); |
| 534 | return; |
| 535 | } |
Paul Mundt | 5713e60 | 2009-06-17 18:20:48 +0900 | [diff] [blame] | 536 | |
Paul Mundt | 1da09c4 | 2011-01-18 19:56:04 +0900 | [diff] [blame] | 537 | /* In the interest of preserving device ordering, synchronize */ |
| 538 | async_synchronize_cookie(cookie); |
| 539 | |
| 540 | register_pci_controller(port->hose); |
Paul Mundt | 5713e60 | 2009-06-17 18:20:48 +0900 | [diff] [blame] | 541 | } |
| 542 | |
| 543 | static struct sh7786_pcie_hwops sh7786_65nm_pcie_hwops __initdata = { |
| 544 | .core_init = sh7786_pcie_core_init, |
| 545 | .port_init_hw = sh7786_pcie_init_hw, |
| 546 | }; |
| 547 | |
| 548 | static int __init sh7786_pcie_init(void) |
| 549 | { |
Paul Mundt | b6b77b2 | 2010-10-14 08:44:55 +0900 | [diff] [blame] | 550 | struct clk *platclk; |
Thomas Petazzoni | d62e9bf | 2017-12-04 16:09:04 +0100 | [diff] [blame] | 551 | u32 mm_sel; |
Paul Mundt | 1da09c4 | 2011-01-18 19:56:04 +0900 | [diff] [blame] | 552 | int i; |
Paul Mundt | 5713e60 | 2009-06-17 18:20:48 +0900 | [diff] [blame] | 553 | |
Matt Fleming | 3b554c3 | 2010-06-19 00:01:03 +0100 | [diff] [blame] | 554 | printk(KERN_NOTICE "PCI: Starting initialization.\n"); |
Paul Mundt | 5713e60 | 2009-06-17 18:20:48 +0900 | [diff] [blame] | 555 | |
| 556 | sh7786_pcie_hwops = &sh7786_65nm_pcie_hwops; |
| 557 | |
| 558 | nr_ports = sh7786_pcie_hwops->core_init(); |
| 559 | BUG_ON(nr_ports > ARRAY_SIZE(sh7786_pci_channels)); |
| 560 | |
| 561 | if (unlikely(nr_ports == 0)) |
| 562 | return -ENODEV; |
| 563 | |
Kees Cook | 6396bb2 | 2018-06-12 14:03:40 -0700 | [diff] [blame^] | 564 | sh7786_pcie_ports = kcalloc(nr_ports, sizeof(struct sh7786_pcie_port), |
Paul Mundt | 5713e60 | 2009-06-17 18:20:48 +0900 | [diff] [blame] | 565 | GFP_KERNEL); |
| 566 | if (unlikely(!sh7786_pcie_ports)) |
| 567 | return -ENOMEM; |
| 568 | |
Paul Mundt | b6b77b2 | 2010-10-14 08:44:55 +0900 | [diff] [blame] | 569 | /* |
| 570 | * Fetch any optional platform clock associated with this block. |
| 571 | * |
| 572 | * This is a rather nasty hack for boards with spec-mocking FPGAs |
| 573 | * that have a secondary set of clocks outside of the on-chip |
| 574 | * ones that need to be accounted for before there is any chance |
| 575 | * of touching the existing MSTP bits or CPG clocks. |
| 576 | */ |
| 577 | platclk = clk_get(NULL, "pcie_plat_clk"); |
| 578 | if (IS_ERR(platclk)) { |
| 579 | /* Sane hardware should probably get a WARN_ON.. */ |
| 580 | platclk = NULL; |
| 581 | } |
| 582 | |
| 583 | clk_enable(platclk); |
| 584 | |
Thomas Petazzoni | d62e9bf | 2017-12-04 16:09:04 +0100 | [diff] [blame] | 585 | mm_sel = sh7786_mm_sel(); |
| 586 | |
| 587 | /* |
| 588 | * Depending on the MMSELR register value, the PCIe0 MEM 1 |
| 589 | * area may not be available. See Table 13.11 of the SH7786 |
| 590 | * datasheet. |
| 591 | */ |
| 592 | if (mm_sel != 1 && mm_sel != 2 && mm_sel != 5 && mm_sel != 6) |
| 593 | sh7786_pci0_resources[2].flags |= IORESOURCE_DISABLED; |
| 594 | |
Paul Mundt | 5713e60 | 2009-06-17 18:20:48 +0900 | [diff] [blame] | 595 | printk(KERN_NOTICE "PCI: probing %d ports.\n", nr_ports); |
| 596 | |
| 597 | for (i = 0; i < nr_ports; i++) { |
| 598 | struct sh7786_pcie_port *port = sh7786_pcie_ports + i; |
| 599 | |
| 600 | port->index = i; |
| 601 | port->hose = sh7786_pci_channels + i; |
Paul Mundt | 7561f2d | 2010-02-08 16:36:56 +0900 | [diff] [blame] | 602 | port->hose->io_map_base = port->hose->resources[0].start; |
Paul Mundt | 5713e60 | 2009-06-17 18:20:48 +0900 | [diff] [blame] | 603 | |
Paul Mundt | 1da09c4 | 2011-01-18 19:56:04 +0900 | [diff] [blame] | 604 | async_schedule(sh7786_pcie_hwops->port_init_hw, port); |
Paul Mundt | b6b77b2 | 2010-10-14 08:44:55 +0900 | [diff] [blame] | 605 | } |
Paul Mundt | 5713e60 | 2009-06-17 18:20:48 +0900 | [diff] [blame] | 606 | |
Paul Mundt | cd7bb53 | 2011-01-28 15:14:08 +0900 | [diff] [blame] | 607 | async_synchronize_full(); |
| 608 | |
Paul Mundt | 5713e60 | 2009-06-17 18:20:48 +0900 | [diff] [blame] | 609 | return 0; |
| 610 | } |
| 611 | arch_initcall(sh7786_pcie_init); |