Thomas Petazzoni | 1c52a51 | 2016-04-26 10:31:46 +0200 | [diff] [blame] | 1 | /* |
| 2 | * PCIe host controller driver for Marvell Armada-8K SoCs |
| 3 | * |
| 4 | * Armada-8K PCIe Glue Layer Source Code |
| 5 | * |
| 6 | * Copyright (C) 2016 Marvell Technology Group Ltd. |
| 7 | * |
Paul Gortmaker | 0e6f98c | 2016-07-02 19:13:21 -0400 | [diff] [blame] | 8 | * Author: Yehuda Yitshak <yehuday@marvell.com> |
| 9 | * Author: Shadi Ammouri <shadi@marvell.com> |
| 10 | * |
Thomas Petazzoni | 1c52a51 | 2016-04-26 10:31:46 +0200 | [diff] [blame] | 11 | * This file is licensed under the terms of the GNU General Public |
| 12 | * License version 2. This program is licensed "as is" without any |
| 13 | * warranty of any kind, whether express or implied. |
| 14 | */ |
| 15 | |
| 16 | #include <linux/clk.h> |
| 17 | #include <linux/delay.h> |
| 18 | #include <linux/interrupt.h> |
| 19 | #include <linux/kernel.h> |
Paul Gortmaker | 0e6f98c | 2016-07-02 19:13:21 -0400 | [diff] [blame] | 20 | #include <linux/init.h> |
Thomas Petazzoni | 1c52a51 | 2016-04-26 10:31:46 +0200 | [diff] [blame] | 21 | #include <linux/of.h> |
| 22 | #include <linux/pci.h> |
| 23 | #include <linux/phy/phy.h> |
| 24 | #include <linux/platform_device.h> |
| 25 | #include <linux/resource.h> |
| 26 | #include <linux/of_pci.h> |
| 27 | #include <linux/of_irq.h> |
| 28 | |
| 29 | #include "pcie-designware.h" |
| 30 | |
| 31 | struct armada8k_pcie { |
Bjorn Helgaas | a477815 | 2016-10-06 13:30:00 -0500 | [diff] [blame] | 32 | struct pcie_port pp; /* pp.dbi_base is DT ctrl */ |
Thomas Petazzoni | 1c52a51 | 2016-04-26 10:31:46 +0200 | [diff] [blame] | 33 | struct clk *clk; |
Thomas Petazzoni | 1c52a51 | 2016-04-26 10:31:46 +0200 | [diff] [blame] | 34 | }; |
| 35 | |
| 36 | #define PCIE_VENDOR_REGS_OFFSET 0x8000 |
| 37 | |
Bjorn Helgaas | 74e6907 | 2016-10-06 13:29:59 -0500 | [diff] [blame] | 38 | #define PCIE_GLOBAL_CONTROL_REG (PCIE_VENDOR_REGS_OFFSET + 0x0) |
Thomas Petazzoni | 1c52a51 | 2016-04-26 10:31:46 +0200 | [diff] [blame] | 39 | #define PCIE_APP_LTSSM_EN BIT(2) |
| 40 | #define PCIE_DEVICE_TYPE_SHIFT 4 |
| 41 | #define PCIE_DEVICE_TYPE_MASK 0xF |
| 42 | #define PCIE_DEVICE_TYPE_RC 0x4 /* Root complex */ |
| 43 | |
Bjorn Helgaas | 74e6907 | 2016-10-06 13:29:59 -0500 | [diff] [blame] | 44 | #define PCIE_GLOBAL_STATUS_REG (PCIE_VENDOR_REGS_OFFSET + 0x8) |
Thomas Petazzoni | 1c52a51 | 2016-04-26 10:31:46 +0200 | [diff] [blame] | 45 | #define PCIE_GLB_STS_RDLH_LINK_UP BIT(1) |
| 46 | #define PCIE_GLB_STS_PHY_LINK_UP BIT(9) |
| 47 | |
Bjorn Helgaas | 74e6907 | 2016-10-06 13:29:59 -0500 | [diff] [blame] | 48 | #define PCIE_GLOBAL_INT_CAUSE1_REG (PCIE_VENDOR_REGS_OFFSET + 0x1C) |
| 49 | #define PCIE_GLOBAL_INT_MASK1_REG (PCIE_VENDOR_REGS_OFFSET + 0x20) |
Thomas Petazzoni | 1c52a51 | 2016-04-26 10:31:46 +0200 | [diff] [blame] | 50 | #define PCIE_INT_A_ASSERT_MASK BIT(9) |
| 51 | #define PCIE_INT_B_ASSERT_MASK BIT(10) |
| 52 | #define PCIE_INT_C_ASSERT_MASK BIT(11) |
| 53 | #define PCIE_INT_D_ASSERT_MASK BIT(12) |
| 54 | |
Bjorn Helgaas | 74e6907 | 2016-10-06 13:29:59 -0500 | [diff] [blame] | 55 | #define PCIE_ARCACHE_TRC_REG (PCIE_VENDOR_REGS_OFFSET + 0x50) |
| 56 | #define PCIE_AWCACHE_TRC_REG (PCIE_VENDOR_REGS_OFFSET + 0x54) |
| 57 | #define PCIE_ARUSER_REG (PCIE_VENDOR_REGS_OFFSET + 0x5C) |
| 58 | #define PCIE_AWUSER_REG (PCIE_VENDOR_REGS_OFFSET + 0x60) |
Thomas Petazzoni | 1c52a51 | 2016-04-26 10:31:46 +0200 | [diff] [blame] | 59 | /* |
| 60 | * AR/AW Cache defauls: Normal memory, Write-Back, Read / Write |
| 61 | * allocate |
| 62 | */ |
| 63 | #define ARCACHE_DEFAULT_VALUE 0x3511 |
| 64 | #define AWCACHE_DEFAULT_VALUE 0x5311 |
| 65 | |
| 66 | #define DOMAIN_OUTER_SHAREABLE 0x2 |
| 67 | #define AX_USER_DOMAIN_MASK 0x3 |
| 68 | #define AX_USER_DOMAIN_SHIFT 4 |
| 69 | |
| 70 | #define to_armada8k_pcie(x) container_of(x, struct armada8k_pcie, pp) |
| 71 | |
| 72 | static int armada8k_pcie_link_up(struct pcie_port *pp) |
| 73 | { |
Thomas Petazzoni | 1c52a51 | 2016-04-26 10:31:46 +0200 | [diff] [blame] | 74 | u32 reg; |
| 75 | u32 mask = PCIE_GLB_STS_RDLH_LINK_UP | PCIE_GLB_STS_PHY_LINK_UP; |
| 76 | |
Bjorn Helgaas | 7687695 | 2016-10-11 20:15:11 -0500 | [diff] [blame] | 77 | reg = dw_pcie_readl_rc(pp, PCIE_GLOBAL_STATUS_REG); |
Thomas Petazzoni | 1c52a51 | 2016-04-26 10:31:46 +0200 | [diff] [blame] | 78 | |
| 79 | if ((reg & mask) == mask) |
| 80 | return 1; |
| 81 | |
| 82 | dev_dbg(pp->dev, "No link detected (Global-Status: 0x%08x).\n", reg); |
| 83 | return 0; |
| 84 | } |
| 85 | |
Bjorn Helgaas | b2d6fd7 | 2016-10-06 13:30:00 -0500 | [diff] [blame] | 86 | static void armada8k_pcie_establish_link(struct armada8k_pcie *pcie) |
Thomas Petazzoni | 1c52a51 | 2016-04-26 10:31:46 +0200 | [diff] [blame] | 87 | { |
Bjorn Helgaas | b2d6fd7 | 2016-10-06 13:30:00 -0500 | [diff] [blame] | 88 | struct pcie_port *pp = &pcie->pp; |
Thomas Petazzoni | 1c52a51 | 2016-04-26 10:31:46 +0200 | [diff] [blame] | 89 | u32 reg; |
| 90 | |
| 91 | if (!dw_pcie_link_up(pp)) { |
| 92 | /* Disable LTSSM state machine to enable configuration */ |
Bjorn Helgaas | 7687695 | 2016-10-11 20:15:11 -0500 | [diff] [blame] | 93 | reg = dw_pcie_readl_rc(pp, PCIE_GLOBAL_CONTROL_REG); |
Thomas Petazzoni | 1c52a51 | 2016-04-26 10:31:46 +0200 | [diff] [blame] | 94 | reg &= ~(PCIE_APP_LTSSM_EN); |
Bjorn Helgaas | 7687695 | 2016-10-11 20:15:11 -0500 | [diff] [blame] | 95 | dw_pcie_writel_rc(pp, PCIE_GLOBAL_CONTROL_REG, reg); |
Thomas Petazzoni | 1c52a51 | 2016-04-26 10:31:46 +0200 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | /* Set the device to root complex mode */ |
Bjorn Helgaas | 7687695 | 2016-10-11 20:15:11 -0500 | [diff] [blame] | 99 | reg = dw_pcie_readl_rc(pp, PCIE_GLOBAL_CONTROL_REG); |
Thomas Petazzoni | 1c52a51 | 2016-04-26 10:31:46 +0200 | [diff] [blame] | 100 | reg &= ~(PCIE_DEVICE_TYPE_MASK << PCIE_DEVICE_TYPE_SHIFT); |
| 101 | reg |= PCIE_DEVICE_TYPE_RC << PCIE_DEVICE_TYPE_SHIFT; |
Bjorn Helgaas | 7687695 | 2016-10-11 20:15:11 -0500 | [diff] [blame] | 102 | dw_pcie_writel_rc(pp, PCIE_GLOBAL_CONTROL_REG, reg); |
Thomas Petazzoni | 1c52a51 | 2016-04-26 10:31:46 +0200 | [diff] [blame] | 103 | |
| 104 | /* Set the PCIe master AxCache attributes */ |
Bjorn Helgaas | 7687695 | 2016-10-11 20:15:11 -0500 | [diff] [blame] | 105 | dw_pcie_writel_rc(pp, PCIE_ARCACHE_TRC_REG, ARCACHE_DEFAULT_VALUE); |
| 106 | dw_pcie_writel_rc(pp, PCIE_AWCACHE_TRC_REG, AWCACHE_DEFAULT_VALUE); |
Thomas Petazzoni | 1c52a51 | 2016-04-26 10:31:46 +0200 | [diff] [blame] | 107 | |
| 108 | /* Set the PCIe master AxDomain attributes */ |
Bjorn Helgaas | 7687695 | 2016-10-11 20:15:11 -0500 | [diff] [blame] | 109 | reg = dw_pcie_readl_rc(pp, PCIE_ARUSER_REG); |
Thomas Petazzoni | 1c52a51 | 2016-04-26 10:31:46 +0200 | [diff] [blame] | 110 | reg &= ~(AX_USER_DOMAIN_MASK << AX_USER_DOMAIN_SHIFT); |
| 111 | reg |= DOMAIN_OUTER_SHAREABLE << AX_USER_DOMAIN_SHIFT; |
Bjorn Helgaas | 7687695 | 2016-10-11 20:15:11 -0500 | [diff] [blame] | 112 | dw_pcie_writel_rc(pp, PCIE_ARUSER_REG, reg); |
Thomas Petazzoni | 1c52a51 | 2016-04-26 10:31:46 +0200 | [diff] [blame] | 113 | |
Bjorn Helgaas | 7687695 | 2016-10-11 20:15:11 -0500 | [diff] [blame] | 114 | reg = dw_pcie_readl_rc(pp, PCIE_AWUSER_REG); |
Thomas Petazzoni | 1c52a51 | 2016-04-26 10:31:46 +0200 | [diff] [blame] | 115 | reg &= ~(AX_USER_DOMAIN_MASK << AX_USER_DOMAIN_SHIFT); |
| 116 | reg |= DOMAIN_OUTER_SHAREABLE << AX_USER_DOMAIN_SHIFT; |
Bjorn Helgaas | 7687695 | 2016-10-11 20:15:11 -0500 | [diff] [blame] | 117 | dw_pcie_writel_rc(pp, PCIE_AWUSER_REG, reg); |
Thomas Petazzoni | 1c52a51 | 2016-04-26 10:31:46 +0200 | [diff] [blame] | 118 | |
| 119 | /* Enable INT A-D interrupts */ |
Bjorn Helgaas | 7687695 | 2016-10-11 20:15:11 -0500 | [diff] [blame] | 120 | reg = dw_pcie_readl_rc(pp, PCIE_GLOBAL_INT_MASK1_REG); |
Thomas Petazzoni | 1c52a51 | 2016-04-26 10:31:46 +0200 | [diff] [blame] | 121 | reg |= PCIE_INT_A_ASSERT_MASK | PCIE_INT_B_ASSERT_MASK | |
| 122 | PCIE_INT_C_ASSERT_MASK | PCIE_INT_D_ASSERT_MASK; |
Bjorn Helgaas | 7687695 | 2016-10-11 20:15:11 -0500 | [diff] [blame] | 123 | dw_pcie_writel_rc(pp, PCIE_GLOBAL_INT_MASK1_REG, reg); |
Thomas Petazzoni | 1c52a51 | 2016-04-26 10:31:46 +0200 | [diff] [blame] | 124 | |
| 125 | if (!dw_pcie_link_up(pp)) { |
| 126 | /* Configuration done. Start LTSSM */ |
Bjorn Helgaas | 7687695 | 2016-10-11 20:15:11 -0500 | [diff] [blame] | 127 | reg = dw_pcie_readl_rc(pp, PCIE_GLOBAL_CONTROL_REG); |
Thomas Petazzoni | 1c52a51 | 2016-04-26 10:31:46 +0200 | [diff] [blame] | 128 | reg |= PCIE_APP_LTSSM_EN; |
Bjorn Helgaas | 7687695 | 2016-10-11 20:15:11 -0500 | [diff] [blame] | 129 | dw_pcie_writel_rc(pp, PCIE_GLOBAL_CONTROL_REG, reg); |
Thomas Petazzoni | 1c52a51 | 2016-04-26 10:31:46 +0200 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | /* Wait until the link becomes active again */ |
| 133 | if (dw_pcie_wait_for_link(pp)) |
| 134 | dev_err(pp->dev, "Link not up after reconfiguration\n"); |
| 135 | } |
| 136 | |
| 137 | static void armada8k_pcie_host_init(struct pcie_port *pp) |
| 138 | { |
Bjorn Helgaas | b2d6fd7 | 2016-10-06 13:30:00 -0500 | [diff] [blame] | 139 | struct armada8k_pcie *pcie = to_armada8k_pcie(pp); |
| 140 | |
Thomas Petazzoni | 1c52a51 | 2016-04-26 10:31:46 +0200 | [diff] [blame] | 141 | dw_pcie_setup_rc(pp); |
Bjorn Helgaas | b2d6fd7 | 2016-10-06 13:30:00 -0500 | [diff] [blame] | 142 | armada8k_pcie_establish_link(pcie); |
Thomas Petazzoni | 1c52a51 | 2016-04-26 10:31:46 +0200 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | static irqreturn_t armada8k_pcie_irq_handler(int irq, void *arg) |
| 146 | { |
Bjorn Helgaas | b2d6fd7 | 2016-10-06 13:30:00 -0500 | [diff] [blame] | 147 | struct armada8k_pcie *pcie = arg; |
| 148 | struct pcie_port *pp = &pcie->pp; |
Thomas Petazzoni | 1c52a51 | 2016-04-26 10:31:46 +0200 | [diff] [blame] | 149 | u32 val; |
| 150 | |
| 151 | /* |
| 152 | * Interrupts are directly handled by the device driver of the |
| 153 | * PCI device. However, they are also latched into the PCIe |
| 154 | * controller, so we simply discard them. |
| 155 | */ |
Bjorn Helgaas | 7687695 | 2016-10-11 20:15:11 -0500 | [diff] [blame] | 156 | val = dw_pcie_readl_rc(pp, PCIE_GLOBAL_INT_CAUSE1_REG); |
| 157 | dw_pcie_writel_rc(pp, PCIE_GLOBAL_INT_CAUSE1_REG, val); |
Thomas Petazzoni | 1c52a51 | 2016-04-26 10:31:46 +0200 | [diff] [blame] | 158 | |
| 159 | return IRQ_HANDLED; |
| 160 | } |
| 161 | |
| 162 | static struct pcie_host_ops armada8k_pcie_host_ops = { |
| 163 | .link_up = armada8k_pcie_link_up, |
| 164 | .host_init = armada8k_pcie_host_init, |
| 165 | }; |
| 166 | |
Bjorn Helgaas | b2d6fd7 | 2016-10-06 13:30:00 -0500 | [diff] [blame] | 167 | static int armada8k_add_pcie_port(struct armada8k_pcie *pcie, |
Thomas Petazzoni | 1c52a51 | 2016-04-26 10:31:46 +0200 | [diff] [blame] | 168 | struct platform_device *pdev) |
| 169 | { |
Bjorn Helgaas | b2d6fd7 | 2016-10-06 13:30:00 -0500 | [diff] [blame] | 170 | struct pcie_port *pp = &pcie->pp; |
Thomas Petazzoni | 1c52a51 | 2016-04-26 10:31:46 +0200 | [diff] [blame] | 171 | struct device *dev = &pdev->dev; |
| 172 | int ret; |
| 173 | |
| 174 | pp->root_bus_nr = -1; |
| 175 | pp->ops = &armada8k_pcie_host_ops; |
| 176 | |
| 177 | pp->irq = platform_get_irq(pdev, 0); |
| 178 | if (!pp->irq) { |
| 179 | dev_err(dev, "failed to get irq for port\n"); |
| 180 | return -ENODEV; |
| 181 | } |
| 182 | |
| 183 | ret = devm_request_irq(dev, pp->irq, armada8k_pcie_irq_handler, |
Bjorn Helgaas | b2d6fd7 | 2016-10-06 13:30:00 -0500 | [diff] [blame] | 184 | IRQF_SHARED, "armada8k-pcie", pcie); |
Thomas Petazzoni | 1c52a51 | 2016-04-26 10:31:46 +0200 | [diff] [blame] | 185 | if (ret) { |
| 186 | dev_err(dev, "failed to request irq %d\n", pp->irq); |
| 187 | return ret; |
| 188 | } |
| 189 | |
| 190 | ret = dw_pcie_host_init(pp); |
| 191 | if (ret) { |
| 192 | dev_err(dev, "failed to initialize host: %d\n", ret); |
| 193 | return ret; |
| 194 | } |
| 195 | |
| 196 | return 0; |
| 197 | } |
| 198 | |
| 199 | static int armada8k_pcie_probe(struct platform_device *pdev) |
| 200 | { |
| 201 | struct armada8k_pcie *pcie; |
| 202 | struct pcie_port *pp; |
| 203 | struct device *dev = &pdev->dev; |
| 204 | struct resource *base; |
| 205 | int ret; |
| 206 | |
| 207 | pcie = devm_kzalloc(dev, sizeof(*pcie), GFP_KERNEL); |
| 208 | if (!pcie) |
| 209 | return -ENOMEM; |
| 210 | |
| 211 | pcie->clk = devm_clk_get(dev, NULL); |
| 212 | if (IS_ERR(pcie->clk)) |
| 213 | return PTR_ERR(pcie->clk); |
| 214 | |
| 215 | clk_prepare_enable(pcie->clk); |
| 216 | |
| 217 | pp = &pcie->pp; |
| 218 | pp->dev = dev; |
Thomas Petazzoni | 1c52a51 | 2016-04-26 10:31:46 +0200 | [diff] [blame] | 219 | |
| 220 | /* Get the dw-pcie unit configuration/control registers base. */ |
| 221 | base = platform_get_resource_byname(pdev, IORESOURCE_MEM, "ctrl"); |
| 222 | pp->dbi_base = devm_ioremap_resource(dev, base); |
| 223 | if (IS_ERR(pp->dbi_base)) { |
| 224 | dev_err(dev, "couldn't remap regs base %p\n", base); |
| 225 | ret = PTR_ERR(pp->dbi_base); |
| 226 | goto fail; |
| 227 | } |
| 228 | |
Bjorn Helgaas | b2d6fd7 | 2016-10-06 13:30:00 -0500 | [diff] [blame] | 229 | ret = armada8k_add_pcie_port(pcie, pdev); |
Thomas Petazzoni | 1c52a51 | 2016-04-26 10:31:46 +0200 | [diff] [blame] | 230 | if (ret) |
| 231 | goto fail; |
| 232 | |
| 233 | return 0; |
| 234 | |
| 235 | fail: |
| 236 | if (!IS_ERR(pcie->clk)) |
| 237 | clk_disable_unprepare(pcie->clk); |
| 238 | |
| 239 | return ret; |
| 240 | } |
| 241 | |
| 242 | static const struct of_device_id armada8k_pcie_of_match[] = { |
| 243 | { .compatible = "marvell,armada8k-pcie", }, |
| 244 | {}, |
| 245 | }; |
Thomas Petazzoni | 1c52a51 | 2016-04-26 10:31:46 +0200 | [diff] [blame] | 246 | |
| 247 | static struct platform_driver armada8k_pcie_driver = { |
| 248 | .probe = armada8k_pcie_probe, |
| 249 | .driver = { |
| 250 | .name = "armada8k-pcie", |
| 251 | .of_match_table = of_match_ptr(armada8k_pcie_of_match), |
| 252 | }, |
| 253 | }; |
Paul Gortmaker | 0e6f98c | 2016-07-02 19:13:21 -0400 | [diff] [blame] | 254 | builtin_platform_driver(armada8k_pcie_driver); |