Bjorn Helgaas | 8cfab3c | 2018-01-26 12:50:27 -0600 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Thomas Petazzoni | 1c52a51 | 2016-04-26 10:31:46 +0200 | [diff] [blame] | 2 | /* |
| 3 | * PCIe host controller driver for Marvell Armada-8K SoCs |
| 4 | * |
| 5 | * Armada-8K PCIe Glue Layer Source Code |
| 6 | * |
| 7 | * Copyright (C) 2016 Marvell Technology Group Ltd. |
| 8 | * |
Paul Gortmaker | 0e6f98c | 2016-07-02 19:13:21 -0400 | [diff] [blame] | 9 | * Author: Yehuda Yitshak <yehuday@marvell.com> |
| 10 | * Author: Shadi Ammouri <shadi@marvell.com> |
Thomas Petazzoni | 1c52a51 | 2016-04-26 10:31:46 +0200 | [diff] [blame] | 11 | */ |
| 12 | |
| 13 | #include <linux/clk.h> |
| 14 | #include <linux/delay.h> |
| 15 | #include <linux/interrupt.h> |
| 16 | #include <linux/kernel.h> |
Paul Gortmaker | 0e6f98c | 2016-07-02 19:13:21 -0400 | [diff] [blame] | 17 | #include <linux/init.h> |
Thomas Petazzoni | 1c52a51 | 2016-04-26 10:31:46 +0200 | [diff] [blame] | 18 | #include <linux/of.h> |
| 19 | #include <linux/pci.h> |
| 20 | #include <linux/phy/phy.h> |
| 21 | #include <linux/platform_device.h> |
| 22 | #include <linux/resource.h> |
| 23 | #include <linux/of_pci.h> |
| 24 | #include <linux/of_irq.h> |
| 25 | |
| 26 | #include "pcie-designware.h" |
| 27 | |
Miquel Raynal | c369b53 | 2019-04-01 15:12:39 +0200 | [diff] [blame] | 28 | #define ARMADA8K_PCIE_MAX_LANES PCIE_LNK_X4 |
| 29 | |
Thomas Petazzoni | 1c52a51 | 2016-04-26 10:31:46 +0200 | [diff] [blame] | 30 | struct armada8k_pcie { |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 31 | struct dw_pcie *pci; |
Thomas Petazzoni | 1c52a51 | 2016-04-26 10:31:46 +0200 | [diff] [blame] | 32 | struct clk *clk; |
Gregory CLEMENT | 2435cdd | 2018-02-28 17:35:30 +0100 | [diff] [blame] | 33 | struct clk *clk_reg; |
Miquel Raynal | c369b53 | 2019-04-01 15:12:39 +0200 | [diff] [blame] | 34 | struct phy *phy[ARMADA8K_PCIE_MAX_LANES]; |
| 35 | unsigned int phy_count; |
Thomas Petazzoni | 1c52a51 | 2016-04-26 10:31:46 +0200 | [diff] [blame] | 36 | }; |
| 37 | |
| 38 | #define PCIE_VENDOR_REGS_OFFSET 0x8000 |
| 39 | |
Bjorn Helgaas | 74e6907 | 2016-10-06 13:29:59 -0500 | [diff] [blame] | 40 | #define PCIE_GLOBAL_CONTROL_REG (PCIE_VENDOR_REGS_OFFSET + 0x0) |
Thomas Petazzoni | 1c52a51 | 2016-04-26 10:31:46 +0200 | [diff] [blame] | 41 | #define PCIE_APP_LTSSM_EN BIT(2) |
| 42 | #define PCIE_DEVICE_TYPE_SHIFT 4 |
| 43 | #define PCIE_DEVICE_TYPE_MASK 0xF |
| 44 | #define PCIE_DEVICE_TYPE_RC 0x4 /* Root complex */ |
| 45 | |
Bjorn Helgaas | 74e6907 | 2016-10-06 13:29:59 -0500 | [diff] [blame] | 46 | #define PCIE_GLOBAL_STATUS_REG (PCIE_VENDOR_REGS_OFFSET + 0x8) |
Thomas Petazzoni | 1c52a51 | 2016-04-26 10:31:46 +0200 | [diff] [blame] | 47 | #define PCIE_GLB_STS_RDLH_LINK_UP BIT(1) |
| 48 | #define PCIE_GLB_STS_PHY_LINK_UP BIT(9) |
| 49 | |
Bjorn Helgaas | 74e6907 | 2016-10-06 13:29:59 -0500 | [diff] [blame] | 50 | #define PCIE_GLOBAL_INT_CAUSE1_REG (PCIE_VENDOR_REGS_OFFSET + 0x1C) |
| 51 | #define PCIE_GLOBAL_INT_MASK1_REG (PCIE_VENDOR_REGS_OFFSET + 0x20) |
Thomas Petazzoni | 1c52a51 | 2016-04-26 10:31:46 +0200 | [diff] [blame] | 52 | #define PCIE_INT_A_ASSERT_MASK BIT(9) |
| 53 | #define PCIE_INT_B_ASSERT_MASK BIT(10) |
| 54 | #define PCIE_INT_C_ASSERT_MASK BIT(11) |
| 55 | #define PCIE_INT_D_ASSERT_MASK BIT(12) |
| 56 | |
Bjorn Helgaas | 74e6907 | 2016-10-06 13:29:59 -0500 | [diff] [blame] | 57 | #define PCIE_ARCACHE_TRC_REG (PCIE_VENDOR_REGS_OFFSET + 0x50) |
| 58 | #define PCIE_AWCACHE_TRC_REG (PCIE_VENDOR_REGS_OFFSET + 0x54) |
| 59 | #define PCIE_ARUSER_REG (PCIE_VENDOR_REGS_OFFSET + 0x5C) |
| 60 | #define PCIE_AWUSER_REG (PCIE_VENDOR_REGS_OFFSET + 0x60) |
Thomas Petazzoni | 1c52a51 | 2016-04-26 10:31:46 +0200 | [diff] [blame] | 61 | /* |
Bjorn Helgaas | f6b6aef | 2019-05-30 08:05:58 -0500 | [diff] [blame] | 62 | * AR/AW Cache defaults: Normal memory, Write-Back, Read / Write |
Thomas Petazzoni | 1c52a51 | 2016-04-26 10:31:46 +0200 | [diff] [blame] | 63 | * allocate |
| 64 | */ |
| 65 | #define ARCACHE_DEFAULT_VALUE 0x3511 |
| 66 | #define AWCACHE_DEFAULT_VALUE 0x5311 |
| 67 | |
| 68 | #define DOMAIN_OUTER_SHAREABLE 0x2 |
| 69 | #define AX_USER_DOMAIN_MASK 0x3 |
| 70 | #define AX_USER_DOMAIN_SHIFT 4 |
| 71 | |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 72 | #define to_armada8k_pcie(x) dev_get_drvdata((x)->dev) |
Thomas Petazzoni | 1c52a51 | 2016-04-26 10:31:46 +0200 | [diff] [blame] | 73 | |
Miquel Raynal | c369b53 | 2019-04-01 15:12:39 +0200 | [diff] [blame] | 74 | static void armada8k_pcie_disable_phys(struct armada8k_pcie *pcie) |
| 75 | { |
| 76 | int i; |
| 77 | |
| 78 | for (i = 0; i < ARMADA8K_PCIE_MAX_LANES; i++) { |
| 79 | phy_power_off(pcie->phy[i]); |
| 80 | phy_exit(pcie->phy[i]); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | static int armada8k_pcie_enable_phys(struct armada8k_pcie *pcie) |
| 85 | { |
| 86 | int ret; |
| 87 | int i; |
| 88 | |
| 89 | for (i = 0; i < ARMADA8K_PCIE_MAX_LANES; i++) { |
| 90 | ret = phy_init(pcie->phy[i]); |
| 91 | if (ret) |
| 92 | return ret; |
| 93 | |
| 94 | ret = phy_set_mode_ext(pcie->phy[i], PHY_MODE_PCIE, |
| 95 | pcie->phy_count); |
| 96 | if (ret) { |
| 97 | phy_exit(pcie->phy[i]); |
| 98 | return ret; |
| 99 | } |
| 100 | |
| 101 | ret = phy_power_on(pcie->phy[i]); |
| 102 | if (ret) { |
| 103 | phy_exit(pcie->phy[i]); |
| 104 | return ret; |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | return 0; |
| 109 | } |
| 110 | |
| 111 | static int armada8k_pcie_setup_phys(struct armada8k_pcie *pcie) |
| 112 | { |
| 113 | struct dw_pcie *pci = pcie->pci; |
| 114 | struct device *dev = pci->dev; |
| 115 | struct device_node *node = dev->of_node; |
| 116 | int ret = 0; |
| 117 | int i; |
| 118 | |
| 119 | for (i = 0; i < ARMADA8K_PCIE_MAX_LANES; i++) { |
| 120 | pcie->phy[i] = devm_of_phy_get_by_index(dev, node, i); |
| 121 | if (IS_ERR(pcie->phy[i]) && |
| 122 | (PTR_ERR(pcie->phy[i]) == -EPROBE_DEFER)) |
| 123 | return PTR_ERR(pcie->phy[i]); |
| 124 | |
| 125 | if (IS_ERR(pcie->phy[i])) { |
| 126 | pcie->phy[i] = NULL; |
| 127 | continue; |
| 128 | } |
| 129 | |
| 130 | pcie->phy_count++; |
| 131 | } |
| 132 | |
| 133 | /* Old bindings miss the PHY handle, so just warn if there is no PHY */ |
| 134 | if (!pcie->phy_count) |
| 135 | dev_warn(dev, "No available PHY\n"); |
| 136 | |
| 137 | ret = armada8k_pcie_enable_phys(pcie); |
| 138 | if (ret) |
| 139 | dev_err(dev, "Failed to initialize PHY(s) (%d)\n", ret); |
| 140 | |
| 141 | return ret; |
| 142 | } |
| 143 | |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 144 | static int armada8k_pcie_link_up(struct dw_pcie *pci) |
Thomas Petazzoni | 1c52a51 | 2016-04-26 10:31:46 +0200 | [diff] [blame] | 145 | { |
Thomas Petazzoni | 1c52a51 | 2016-04-26 10:31:46 +0200 | [diff] [blame] | 146 | u32 reg; |
| 147 | u32 mask = PCIE_GLB_STS_RDLH_LINK_UP | PCIE_GLB_STS_PHY_LINK_UP; |
| 148 | |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 149 | reg = dw_pcie_readl_dbi(pci, PCIE_GLOBAL_STATUS_REG); |
Thomas Petazzoni | 1c52a51 | 2016-04-26 10:31:46 +0200 | [diff] [blame] | 150 | |
| 151 | if ((reg & mask) == mask) |
| 152 | return 1; |
| 153 | |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 154 | dev_dbg(pci->dev, "No link detected (Global-Status: 0x%08x).\n", reg); |
Thomas Petazzoni | 1c52a51 | 2016-04-26 10:31:46 +0200 | [diff] [blame] | 155 | return 0; |
| 156 | } |
| 157 | |
Bjorn Helgaas | b2d6fd7 | 2016-10-06 13:30:00 -0500 | [diff] [blame] | 158 | static void armada8k_pcie_establish_link(struct armada8k_pcie *pcie) |
Thomas Petazzoni | 1c52a51 | 2016-04-26 10:31:46 +0200 | [diff] [blame] | 159 | { |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 160 | struct dw_pcie *pci = pcie->pci; |
Thomas Petazzoni | 1c52a51 | 2016-04-26 10:31:46 +0200 | [diff] [blame] | 161 | u32 reg; |
| 162 | |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 163 | if (!dw_pcie_link_up(pci)) { |
Thomas Petazzoni | 1c52a51 | 2016-04-26 10:31:46 +0200 | [diff] [blame] | 164 | /* Disable LTSSM state machine to enable configuration */ |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 165 | reg = dw_pcie_readl_dbi(pci, PCIE_GLOBAL_CONTROL_REG); |
Thomas Petazzoni | 1c52a51 | 2016-04-26 10:31:46 +0200 | [diff] [blame] | 166 | reg &= ~(PCIE_APP_LTSSM_EN); |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 167 | dw_pcie_writel_dbi(pci, PCIE_GLOBAL_CONTROL_REG, reg); |
Thomas Petazzoni | 1c52a51 | 2016-04-26 10:31:46 +0200 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | /* Set the device to root complex mode */ |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 171 | reg = dw_pcie_readl_dbi(pci, PCIE_GLOBAL_CONTROL_REG); |
Thomas Petazzoni | 1c52a51 | 2016-04-26 10:31:46 +0200 | [diff] [blame] | 172 | reg &= ~(PCIE_DEVICE_TYPE_MASK << PCIE_DEVICE_TYPE_SHIFT); |
| 173 | reg |= PCIE_DEVICE_TYPE_RC << PCIE_DEVICE_TYPE_SHIFT; |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 174 | dw_pcie_writel_dbi(pci, PCIE_GLOBAL_CONTROL_REG, reg); |
Thomas Petazzoni | 1c52a51 | 2016-04-26 10:31:46 +0200 | [diff] [blame] | 175 | |
| 176 | /* Set the PCIe master AxCache attributes */ |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 177 | dw_pcie_writel_dbi(pci, PCIE_ARCACHE_TRC_REG, ARCACHE_DEFAULT_VALUE); |
| 178 | dw_pcie_writel_dbi(pci, PCIE_AWCACHE_TRC_REG, AWCACHE_DEFAULT_VALUE); |
Thomas Petazzoni | 1c52a51 | 2016-04-26 10:31:46 +0200 | [diff] [blame] | 179 | |
| 180 | /* Set the PCIe master AxDomain attributes */ |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 181 | reg = dw_pcie_readl_dbi(pci, PCIE_ARUSER_REG); |
Thomas Petazzoni | 1c52a51 | 2016-04-26 10:31:46 +0200 | [diff] [blame] | 182 | reg &= ~(AX_USER_DOMAIN_MASK << AX_USER_DOMAIN_SHIFT); |
| 183 | reg |= DOMAIN_OUTER_SHAREABLE << AX_USER_DOMAIN_SHIFT; |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 184 | dw_pcie_writel_dbi(pci, PCIE_ARUSER_REG, reg); |
Thomas Petazzoni | 1c52a51 | 2016-04-26 10:31:46 +0200 | [diff] [blame] | 185 | |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 186 | reg = dw_pcie_readl_dbi(pci, PCIE_AWUSER_REG); |
Thomas Petazzoni | 1c52a51 | 2016-04-26 10:31:46 +0200 | [diff] [blame] | 187 | reg &= ~(AX_USER_DOMAIN_MASK << AX_USER_DOMAIN_SHIFT); |
| 188 | reg |= DOMAIN_OUTER_SHAREABLE << AX_USER_DOMAIN_SHIFT; |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 189 | dw_pcie_writel_dbi(pci, PCIE_AWUSER_REG, reg); |
Thomas Petazzoni | 1c52a51 | 2016-04-26 10:31:46 +0200 | [diff] [blame] | 190 | |
| 191 | /* Enable INT A-D interrupts */ |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 192 | reg = dw_pcie_readl_dbi(pci, PCIE_GLOBAL_INT_MASK1_REG); |
Thomas Petazzoni | 1c52a51 | 2016-04-26 10:31:46 +0200 | [diff] [blame] | 193 | reg |= PCIE_INT_A_ASSERT_MASK | PCIE_INT_B_ASSERT_MASK | |
| 194 | PCIE_INT_C_ASSERT_MASK | PCIE_INT_D_ASSERT_MASK; |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 195 | dw_pcie_writel_dbi(pci, PCIE_GLOBAL_INT_MASK1_REG, reg); |
Thomas Petazzoni | 1c52a51 | 2016-04-26 10:31:46 +0200 | [diff] [blame] | 196 | |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 197 | if (!dw_pcie_link_up(pci)) { |
Thomas Petazzoni | 1c52a51 | 2016-04-26 10:31:46 +0200 | [diff] [blame] | 198 | /* Configuration done. Start LTSSM */ |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 199 | reg = dw_pcie_readl_dbi(pci, PCIE_GLOBAL_CONTROL_REG); |
Thomas Petazzoni | 1c52a51 | 2016-04-26 10:31:46 +0200 | [diff] [blame] | 200 | reg |= PCIE_APP_LTSSM_EN; |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 201 | dw_pcie_writel_dbi(pci, PCIE_GLOBAL_CONTROL_REG, reg); |
Thomas Petazzoni | 1c52a51 | 2016-04-26 10:31:46 +0200 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | /* Wait until the link becomes active again */ |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 205 | if (dw_pcie_wait_for_link(pci)) |
| 206 | dev_err(pci->dev, "Link not up after reconfiguration\n"); |
Thomas Petazzoni | 1c52a51 | 2016-04-26 10:31:46 +0200 | [diff] [blame] | 207 | } |
| 208 | |
Bjorn Andersson | 4a30176 | 2017-07-15 23:39:45 -0700 | [diff] [blame] | 209 | static int armada8k_pcie_host_init(struct pcie_port *pp) |
Thomas Petazzoni | 1c52a51 | 2016-04-26 10:31:46 +0200 | [diff] [blame] | 210 | { |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 211 | struct dw_pcie *pci = to_dw_pcie_from_pp(pp); |
| 212 | struct armada8k_pcie *pcie = to_armada8k_pcie(pci); |
Bjorn Helgaas | b2d6fd7 | 2016-10-06 13:30:00 -0500 | [diff] [blame] | 213 | |
Thomas Petazzoni | 1c52a51 | 2016-04-26 10:31:46 +0200 | [diff] [blame] | 214 | dw_pcie_setup_rc(pp); |
Bjorn Helgaas | b2d6fd7 | 2016-10-06 13:30:00 -0500 | [diff] [blame] | 215 | armada8k_pcie_establish_link(pcie); |
Bjorn Andersson | 4a30176 | 2017-07-15 23:39:45 -0700 | [diff] [blame] | 216 | |
| 217 | return 0; |
Thomas Petazzoni | 1c52a51 | 2016-04-26 10:31:46 +0200 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | static irqreturn_t armada8k_pcie_irq_handler(int irq, void *arg) |
| 221 | { |
Bjorn Helgaas | b2d6fd7 | 2016-10-06 13:30:00 -0500 | [diff] [blame] | 222 | struct armada8k_pcie *pcie = arg; |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 223 | struct dw_pcie *pci = pcie->pci; |
Thomas Petazzoni | 1c52a51 | 2016-04-26 10:31:46 +0200 | [diff] [blame] | 224 | u32 val; |
| 225 | |
| 226 | /* |
| 227 | * Interrupts are directly handled by the device driver of the |
| 228 | * PCI device. However, they are also latched into the PCIe |
| 229 | * controller, so we simply discard them. |
| 230 | */ |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 231 | val = dw_pcie_readl_dbi(pci, PCIE_GLOBAL_INT_CAUSE1_REG); |
| 232 | dw_pcie_writel_dbi(pci, PCIE_GLOBAL_INT_CAUSE1_REG, val); |
Thomas Petazzoni | 1c52a51 | 2016-04-26 10:31:46 +0200 | [diff] [blame] | 233 | |
| 234 | return IRQ_HANDLED; |
| 235 | } |
| 236 | |
Jisheng Zhang | 4ab2e7c | 2017-06-05 16:53:46 +0800 | [diff] [blame] | 237 | static const struct dw_pcie_host_ops armada8k_pcie_host_ops = { |
Thomas Petazzoni | 1c52a51 | 2016-04-26 10:31:46 +0200 | [diff] [blame] | 238 | .host_init = armada8k_pcie_host_init, |
| 239 | }; |
| 240 | |
Bjorn Helgaas | b2d6fd7 | 2016-10-06 13:30:00 -0500 | [diff] [blame] | 241 | static int armada8k_add_pcie_port(struct armada8k_pcie *pcie, |
Thomas Petazzoni | 1c52a51 | 2016-04-26 10:31:46 +0200 | [diff] [blame] | 242 | struct platform_device *pdev) |
| 243 | { |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 244 | struct dw_pcie *pci = pcie->pci; |
| 245 | struct pcie_port *pp = &pci->pp; |
Thomas Petazzoni | 1c52a51 | 2016-04-26 10:31:46 +0200 | [diff] [blame] | 246 | struct device *dev = &pdev->dev; |
| 247 | int ret; |
| 248 | |
Thomas Petazzoni | 1c52a51 | 2016-04-26 10:31:46 +0200 | [diff] [blame] | 249 | pp->ops = &armada8k_pcie_host_ops; |
| 250 | |
| 251 | pp->irq = platform_get_irq(pdev, 0); |
Fabio Estevam | 0fe5f1c | 2017-08-31 14:52:03 -0300 | [diff] [blame] | 252 | if (pp->irq < 0) { |
Thomas Petazzoni | 1c52a51 | 2016-04-26 10:31:46 +0200 | [diff] [blame] | 253 | dev_err(dev, "failed to get irq for port\n"); |
Fabio Estevam | 0fe5f1c | 2017-08-31 14:52:03 -0300 | [diff] [blame] | 254 | return pp->irq; |
Thomas Petazzoni | 1c52a51 | 2016-04-26 10:31:46 +0200 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | ret = devm_request_irq(dev, pp->irq, armada8k_pcie_irq_handler, |
Bjorn Helgaas | b2d6fd7 | 2016-10-06 13:30:00 -0500 | [diff] [blame] | 258 | IRQF_SHARED, "armada8k-pcie", pcie); |
Thomas Petazzoni | 1c52a51 | 2016-04-26 10:31:46 +0200 | [diff] [blame] | 259 | if (ret) { |
| 260 | dev_err(dev, "failed to request irq %d\n", pp->irq); |
| 261 | return ret; |
| 262 | } |
| 263 | |
| 264 | ret = dw_pcie_host_init(pp); |
| 265 | if (ret) { |
| 266 | dev_err(dev, "failed to initialize host: %d\n", ret); |
| 267 | return ret; |
| 268 | } |
| 269 | |
| 270 | return 0; |
| 271 | } |
| 272 | |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 273 | static const struct dw_pcie_ops dw_pcie_ops = { |
| 274 | .link_up = armada8k_pcie_link_up, |
| 275 | }; |
| 276 | |
Thomas Petazzoni | 1c52a51 | 2016-04-26 10:31:46 +0200 | [diff] [blame] | 277 | static int armada8k_pcie_probe(struct platform_device *pdev) |
| 278 | { |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 279 | struct dw_pcie *pci; |
Thomas Petazzoni | 1c52a51 | 2016-04-26 10:31:46 +0200 | [diff] [blame] | 280 | struct armada8k_pcie *pcie; |
Thomas Petazzoni | 1c52a51 | 2016-04-26 10:31:46 +0200 | [diff] [blame] | 281 | struct device *dev = &pdev->dev; |
| 282 | struct resource *base; |
| 283 | int ret; |
| 284 | |
| 285 | pcie = devm_kzalloc(dev, sizeof(*pcie), GFP_KERNEL); |
| 286 | if (!pcie) |
| 287 | return -ENOMEM; |
| 288 | |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 289 | pci = devm_kzalloc(dev, sizeof(*pci), GFP_KERNEL); |
| 290 | if (!pci) |
| 291 | return -ENOMEM; |
| 292 | |
| 293 | pci->dev = dev; |
| 294 | pci->ops = &dw_pcie_ops; |
| 295 | |
Guenter Roeck | c046406 | 2017-02-25 02:08:12 -0800 | [diff] [blame] | 296 | pcie->pci = pci; |
| 297 | |
Thomas Petazzoni | 1c52a51 | 2016-04-26 10:31:46 +0200 | [diff] [blame] | 298 | pcie->clk = devm_clk_get(dev, NULL); |
| 299 | if (IS_ERR(pcie->clk)) |
| 300 | return PTR_ERR(pcie->clk); |
| 301 | |
Fabio Estevam | e2e5d7b | 2017-07-22 17:25:19 -0300 | [diff] [blame] | 302 | ret = clk_prepare_enable(pcie->clk); |
| 303 | if (ret) |
| 304 | return ret; |
Thomas Petazzoni | 1c52a51 | 2016-04-26 10:31:46 +0200 | [diff] [blame] | 305 | |
Gregory CLEMENT | 2435cdd | 2018-02-28 17:35:30 +0100 | [diff] [blame] | 306 | pcie->clk_reg = devm_clk_get(dev, "reg"); |
| 307 | if (pcie->clk_reg == ERR_PTR(-EPROBE_DEFER)) { |
| 308 | ret = -EPROBE_DEFER; |
| 309 | goto fail; |
| 310 | } |
| 311 | if (!IS_ERR(pcie->clk_reg)) { |
| 312 | ret = clk_prepare_enable(pcie->clk_reg); |
| 313 | if (ret) |
| 314 | goto fail_clkreg; |
| 315 | } |
| 316 | |
Thomas Petazzoni | 1c52a51 | 2016-04-26 10:31:46 +0200 | [diff] [blame] | 317 | /* Get the dw-pcie unit configuration/control registers base. */ |
| 318 | base = platform_get_resource_byname(pdev, IORESOURCE_MEM, "ctrl"); |
Lorenzo Pieralisi | 53dfa17 | 2017-04-19 17:49:04 +0100 | [diff] [blame] | 319 | pci->dbi_base = devm_pci_remap_cfg_resource(dev, base); |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 320 | if (IS_ERR(pci->dbi_base)) { |
Thomas Petazzoni | 1c52a51 | 2016-04-26 10:31:46 +0200 | [diff] [blame] | 321 | dev_err(dev, "couldn't remap regs base %p\n", base); |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 322 | ret = PTR_ERR(pci->dbi_base); |
Gregory CLEMENT | 2435cdd | 2018-02-28 17:35:30 +0100 | [diff] [blame] | 323 | goto fail_clkreg; |
Thomas Petazzoni | 1c52a51 | 2016-04-26 10:31:46 +0200 | [diff] [blame] | 324 | } |
| 325 | |
Miquel Raynal | c369b53 | 2019-04-01 15:12:39 +0200 | [diff] [blame] | 326 | ret = armada8k_pcie_setup_phys(pcie); |
| 327 | if (ret) |
| 328 | goto fail_clkreg; |
| 329 | |
Kishon Vijay Abraham I | 9bcf0a6 | 2017-02-15 18:48:11 +0530 | [diff] [blame] | 330 | platform_set_drvdata(pdev, pcie); |
| 331 | |
Bjorn Helgaas | b2d6fd7 | 2016-10-06 13:30:00 -0500 | [diff] [blame] | 332 | ret = armada8k_add_pcie_port(pcie, pdev); |
Thomas Petazzoni | 1c52a51 | 2016-04-26 10:31:46 +0200 | [diff] [blame] | 333 | if (ret) |
Miquel Raynal | c369b53 | 2019-04-01 15:12:39 +0200 | [diff] [blame] | 334 | goto disable_phy; |
Thomas Petazzoni | 1c52a51 | 2016-04-26 10:31:46 +0200 | [diff] [blame] | 335 | |
| 336 | return 0; |
| 337 | |
Miquel Raynal | c369b53 | 2019-04-01 15:12:39 +0200 | [diff] [blame] | 338 | disable_phy: |
| 339 | armada8k_pcie_disable_phys(pcie); |
Gregory CLEMENT | 2435cdd | 2018-02-28 17:35:30 +0100 | [diff] [blame] | 340 | fail_clkreg: |
| 341 | clk_disable_unprepare(pcie->clk_reg); |
Thomas Petazzoni | 1c52a51 | 2016-04-26 10:31:46 +0200 | [diff] [blame] | 342 | fail: |
Gregory CLEMENT | 5dcd7f1 | 2018-02-28 17:35:29 +0100 | [diff] [blame] | 343 | clk_disable_unprepare(pcie->clk); |
Thomas Petazzoni | 1c52a51 | 2016-04-26 10:31:46 +0200 | [diff] [blame] | 344 | |
| 345 | return ret; |
| 346 | } |
| 347 | |
| 348 | static const struct of_device_id armada8k_pcie_of_match[] = { |
| 349 | { .compatible = "marvell,armada8k-pcie", }, |
| 350 | {}, |
| 351 | }; |
Thomas Petazzoni | 1c52a51 | 2016-04-26 10:31:46 +0200 | [diff] [blame] | 352 | |
| 353 | static struct platform_driver armada8k_pcie_driver = { |
| 354 | .probe = armada8k_pcie_probe, |
| 355 | .driver = { |
| 356 | .name = "armada8k-pcie", |
| 357 | .of_match_table = of_match_ptr(armada8k_pcie_of_match), |
Brian Norris | a5f40e8 | 2017-04-20 15:36:25 -0500 | [diff] [blame] | 358 | .suppress_bind_attrs = true, |
Thomas Petazzoni | 1c52a51 | 2016-04-26 10:31:46 +0200 | [diff] [blame] | 359 | }, |
| 360 | }; |
Paul Gortmaker | 0e6f98c | 2016-07-02 19:13:21 -0400 | [diff] [blame] | 361 | builtin_platform_driver(armada8k_pcie_driver); |