Kishon Vijay Abraham I | f3e2591 | 2020-07-22 16:33:15 +0530 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Krzysztof WilczyĆski | 43395d9 | 2021-03-11 00:17:17 +0000 | [diff] [blame] | 2 | /* |
Kishon Vijay Abraham I | f3e2591 | 2020-07-22 16:33:15 +0530 | [diff] [blame] | 3 | * pci-j721e - PCIe controller driver for TI's J721E SoCs |
| 4 | * |
| 5 | * Copyright (C) 2020 Texas Instruments Incorporated - http://www.ti.com |
| 6 | * Author: Kishon Vijay Abraham I <kishon@ti.com> |
| 7 | */ |
| 8 | |
Kishon Vijay Abraham I | 49e0efd | 2021-03-08 12:05:50 +0530 | [diff] [blame] | 9 | #include <linux/clk.h> |
Kishon Vijay Abraham I | f3e2591 | 2020-07-22 16:33:15 +0530 | [diff] [blame] | 10 | #include <linux/delay.h> |
| 11 | #include <linux/gpio/consumer.h> |
| 12 | #include <linux/io.h> |
| 13 | #include <linux/irqchip/chained_irq.h> |
| 14 | #include <linux/irqdomain.h> |
| 15 | #include <linux/mfd/syscon.h> |
Kishon Vijay Abraham I | 7aa2562 | 2020-12-10 18:19:17 +0530 | [diff] [blame] | 16 | #include <linux/of.h> |
Kishon Vijay Abraham I | f3e2591 | 2020-07-22 16:33:15 +0530 | [diff] [blame] | 17 | #include <linux/of_device.h> |
| 18 | #include <linux/of_irq.h> |
| 19 | #include <linux/pci.h> |
| 20 | #include <linux/pm_runtime.h> |
| 21 | #include <linux/regmap.h> |
| 22 | |
| 23 | #include "../../pci.h" |
| 24 | #include "pcie-cadence.h" |
| 25 | |
| 26 | #define ENABLE_REG_SYS_2 0x108 |
| 27 | #define STATUS_REG_SYS_2 0x508 |
| 28 | #define STATUS_CLR_REG_SYS_2 0x708 |
| 29 | #define LINK_DOWN BIT(1) |
Kishon Vijay Abraham I | f1de588 | 2021-08-11 18:03:34 +0530 | [diff] [blame] | 30 | #define J7200_LINK_DOWN BIT(10) |
Kishon Vijay Abraham I | f3e2591 | 2020-07-22 16:33:15 +0530 | [diff] [blame] | 31 | |
| 32 | #define J721E_PCIE_USER_CMD_STATUS 0x4 |
| 33 | #define LINK_TRAINING_ENABLE BIT(0) |
| 34 | |
| 35 | #define J721E_PCIE_USER_LINKSTATUS 0x14 |
| 36 | #define LINK_STATUS GENMASK(1, 0) |
| 37 | |
| 38 | enum link_status { |
| 39 | NO_RECEIVERS_DETECTED, |
| 40 | LINK_TRAINING_IN_PROGRESS, |
| 41 | LINK_UP_DL_IN_PROGRESS, |
| 42 | LINK_UP_DL_COMPLETED, |
| 43 | }; |
| 44 | |
| 45 | #define J721E_MODE_RC BIT(7) |
| 46 | #define LANE_COUNT_MASK BIT(8) |
| 47 | #define LANE_COUNT(n) ((n) << 8) |
| 48 | |
| 49 | #define GENERATION_SEL_MASK GENMASK(1, 0) |
| 50 | |
| 51 | #define MAX_LANES 2 |
| 52 | |
| 53 | struct j721e_pcie { |
Bjorn Helgaas | 19e8638 | 2021-12-22 19:10:40 -0600 | [diff] [blame] | 54 | struct cdns_pcie *cdns_pcie; |
Kishon Vijay Abraham I | 49e0efd | 2021-03-08 12:05:50 +0530 | [diff] [blame] | 55 | struct clk *refclk; |
Kishon Vijay Abraham I | f3e2591 | 2020-07-22 16:33:15 +0530 | [diff] [blame] | 56 | u32 mode; |
| 57 | u32 num_lanes; |
Kishon Vijay Abraham I | f3e2591 | 2020-07-22 16:33:15 +0530 | [diff] [blame] | 58 | void __iomem *user_cfg_base; |
| 59 | void __iomem *intd_cfg_base; |
Kishon Vijay Abraham I | f1de588 | 2021-08-11 18:03:34 +0530 | [diff] [blame] | 60 | u32 linkdown_irq_regfield; |
Kishon Vijay Abraham I | f3e2591 | 2020-07-22 16:33:15 +0530 | [diff] [blame] | 61 | }; |
| 62 | |
| 63 | enum j721e_pcie_mode { |
| 64 | PCI_MODE_RC, |
| 65 | PCI_MODE_EP, |
| 66 | }; |
| 67 | |
| 68 | struct j721e_pcie_data { |
| 69 | enum j721e_pcie_mode mode; |
Kishon Vijay Abraham I | f445574 | 2021-08-11 18:03:32 +0530 | [diff] [blame] | 70 | unsigned int quirk_retrain_flag:1; |
Kishon Vijay Abraham I | f1de588 | 2021-08-11 18:03:34 +0530 | [diff] [blame] | 71 | unsigned int quirk_detect_quiet_flag:1; |
| 72 | u32 linkdown_irq_regfield; |
| 73 | unsigned int byte_access_allowed:1; |
Kishon Vijay Abraham I | f3e2591 | 2020-07-22 16:33:15 +0530 | [diff] [blame] | 74 | }; |
| 75 | |
| 76 | static inline u32 j721e_pcie_user_readl(struct j721e_pcie *pcie, u32 offset) |
| 77 | { |
| 78 | return readl(pcie->user_cfg_base + offset); |
| 79 | } |
| 80 | |
| 81 | static inline void j721e_pcie_user_writel(struct j721e_pcie *pcie, u32 offset, |
| 82 | u32 value) |
| 83 | { |
| 84 | writel(value, pcie->user_cfg_base + offset); |
| 85 | } |
| 86 | |
| 87 | static inline u32 j721e_pcie_intd_readl(struct j721e_pcie *pcie, u32 offset) |
| 88 | { |
| 89 | return readl(pcie->intd_cfg_base + offset); |
| 90 | } |
| 91 | |
| 92 | static inline void j721e_pcie_intd_writel(struct j721e_pcie *pcie, u32 offset, |
| 93 | u32 value) |
| 94 | { |
| 95 | writel(value, pcie->intd_cfg_base + offset); |
| 96 | } |
| 97 | |
| 98 | static irqreturn_t j721e_pcie_link_irq_handler(int irq, void *priv) |
| 99 | { |
| 100 | struct j721e_pcie *pcie = priv; |
Bjorn Helgaas | 19e8638 | 2021-12-22 19:10:40 -0600 | [diff] [blame] | 101 | struct device *dev = pcie->cdns_pcie->dev; |
Kishon Vijay Abraham I | f3e2591 | 2020-07-22 16:33:15 +0530 | [diff] [blame] | 102 | u32 reg; |
| 103 | |
| 104 | reg = j721e_pcie_intd_readl(pcie, STATUS_REG_SYS_2); |
Kishon Vijay Abraham I | f1de588 | 2021-08-11 18:03:34 +0530 | [diff] [blame] | 105 | if (!(reg & pcie->linkdown_irq_regfield)) |
Kishon Vijay Abraham I | f3e2591 | 2020-07-22 16:33:15 +0530 | [diff] [blame] | 106 | return IRQ_NONE; |
| 107 | |
| 108 | dev_err(dev, "LINK DOWN!\n"); |
| 109 | |
Kishon Vijay Abraham I | f1de588 | 2021-08-11 18:03:34 +0530 | [diff] [blame] | 110 | j721e_pcie_intd_writel(pcie, STATUS_CLR_REG_SYS_2, pcie->linkdown_irq_regfield); |
Kishon Vijay Abraham I | f3e2591 | 2020-07-22 16:33:15 +0530 | [diff] [blame] | 111 | return IRQ_HANDLED; |
| 112 | } |
| 113 | |
| 114 | static void j721e_pcie_config_link_irq(struct j721e_pcie *pcie) |
| 115 | { |
| 116 | u32 reg; |
| 117 | |
| 118 | reg = j721e_pcie_intd_readl(pcie, ENABLE_REG_SYS_2); |
Kishon Vijay Abraham I | f1de588 | 2021-08-11 18:03:34 +0530 | [diff] [blame] | 119 | reg |= pcie->linkdown_irq_regfield; |
Kishon Vijay Abraham I | f3e2591 | 2020-07-22 16:33:15 +0530 | [diff] [blame] | 120 | j721e_pcie_intd_writel(pcie, ENABLE_REG_SYS_2, reg); |
| 121 | } |
| 122 | |
| 123 | static int j721e_pcie_start_link(struct cdns_pcie *cdns_pcie) |
| 124 | { |
| 125 | struct j721e_pcie *pcie = dev_get_drvdata(cdns_pcie->dev); |
| 126 | u32 reg; |
| 127 | |
| 128 | reg = j721e_pcie_user_readl(pcie, J721E_PCIE_USER_CMD_STATUS); |
| 129 | reg |= LINK_TRAINING_ENABLE; |
| 130 | j721e_pcie_user_writel(pcie, J721E_PCIE_USER_CMD_STATUS, reg); |
| 131 | |
| 132 | return 0; |
| 133 | } |
| 134 | |
| 135 | static void j721e_pcie_stop_link(struct cdns_pcie *cdns_pcie) |
| 136 | { |
| 137 | struct j721e_pcie *pcie = dev_get_drvdata(cdns_pcie->dev); |
| 138 | u32 reg; |
| 139 | |
| 140 | reg = j721e_pcie_user_readl(pcie, J721E_PCIE_USER_CMD_STATUS); |
| 141 | reg &= ~LINK_TRAINING_ENABLE; |
| 142 | j721e_pcie_user_writel(pcie, J721E_PCIE_USER_CMD_STATUS, reg); |
| 143 | } |
| 144 | |
| 145 | static bool j721e_pcie_link_up(struct cdns_pcie *cdns_pcie) |
| 146 | { |
| 147 | struct j721e_pcie *pcie = dev_get_drvdata(cdns_pcie->dev); |
| 148 | u32 reg; |
| 149 | |
| 150 | reg = j721e_pcie_user_readl(pcie, J721E_PCIE_USER_LINKSTATUS); |
| 151 | reg &= LINK_STATUS; |
| 152 | if (reg == LINK_UP_DL_COMPLETED) |
| 153 | return true; |
| 154 | |
| 155 | return false; |
| 156 | } |
| 157 | |
| 158 | static const struct cdns_pcie_ops j721e_pcie_ops = { |
| 159 | .start_link = j721e_pcie_start_link, |
| 160 | .stop_link = j721e_pcie_stop_link, |
| 161 | .link_up = j721e_pcie_link_up, |
| 162 | }; |
| 163 | |
Kishon Vijay Abraham I | 7aa2562 | 2020-12-10 18:19:17 +0530 | [diff] [blame] | 164 | static int j721e_pcie_set_mode(struct j721e_pcie *pcie, struct regmap *syscon, |
| 165 | unsigned int offset) |
Kishon Vijay Abraham I | f3e2591 | 2020-07-22 16:33:15 +0530 | [diff] [blame] | 166 | { |
Bjorn Helgaas | 19e8638 | 2021-12-22 19:10:40 -0600 | [diff] [blame] | 167 | struct device *dev = pcie->cdns_pcie->dev; |
Kishon Vijay Abraham I | f3e2591 | 2020-07-22 16:33:15 +0530 | [diff] [blame] | 168 | u32 mask = J721E_MODE_RC; |
| 169 | u32 mode = pcie->mode; |
| 170 | u32 val = 0; |
| 171 | int ret = 0; |
| 172 | |
| 173 | if (mode == PCI_MODE_RC) |
| 174 | val = J721E_MODE_RC; |
| 175 | |
Kishon Vijay Abraham I | 7aa2562 | 2020-12-10 18:19:17 +0530 | [diff] [blame] | 176 | ret = regmap_update_bits(syscon, offset, mask, val); |
Kishon Vijay Abraham I | f3e2591 | 2020-07-22 16:33:15 +0530 | [diff] [blame] | 177 | if (ret) |
| 178 | dev_err(dev, "failed to set pcie mode\n"); |
| 179 | |
| 180 | return ret; |
| 181 | } |
| 182 | |
| 183 | static int j721e_pcie_set_link_speed(struct j721e_pcie *pcie, |
Kishon Vijay Abraham I | 7aa2562 | 2020-12-10 18:19:17 +0530 | [diff] [blame] | 184 | struct regmap *syscon, unsigned int offset) |
Kishon Vijay Abraham I | f3e2591 | 2020-07-22 16:33:15 +0530 | [diff] [blame] | 185 | { |
Bjorn Helgaas | 19e8638 | 2021-12-22 19:10:40 -0600 | [diff] [blame] | 186 | struct device *dev = pcie->cdns_pcie->dev; |
Kishon Vijay Abraham I | f3e2591 | 2020-07-22 16:33:15 +0530 | [diff] [blame] | 187 | struct device_node *np = dev->of_node; |
| 188 | int link_speed; |
| 189 | u32 val = 0; |
| 190 | int ret; |
| 191 | |
| 192 | link_speed = of_pci_get_max_link_speed(np); |
| 193 | if (link_speed < 2) |
| 194 | link_speed = 2; |
| 195 | |
| 196 | val = link_speed - 1; |
Kishon Vijay Abraham I | 7aa2562 | 2020-12-10 18:19:17 +0530 | [diff] [blame] | 197 | ret = regmap_update_bits(syscon, offset, GENERATION_SEL_MASK, val); |
Kishon Vijay Abraham I | f3e2591 | 2020-07-22 16:33:15 +0530 | [diff] [blame] | 198 | if (ret) |
| 199 | dev_err(dev, "failed to set link speed\n"); |
| 200 | |
| 201 | return ret; |
| 202 | } |
| 203 | |
| 204 | static int j721e_pcie_set_lane_count(struct j721e_pcie *pcie, |
Kishon Vijay Abraham I | 7aa2562 | 2020-12-10 18:19:17 +0530 | [diff] [blame] | 205 | struct regmap *syscon, unsigned int offset) |
Kishon Vijay Abraham I | f3e2591 | 2020-07-22 16:33:15 +0530 | [diff] [blame] | 206 | { |
Bjorn Helgaas | 19e8638 | 2021-12-22 19:10:40 -0600 | [diff] [blame] | 207 | struct device *dev = pcie->cdns_pcie->dev; |
Kishon Vijay Abraham I | f3e2591 | 2020-07-22 16:33:15 +0530 | [diff] [blame] | 208 | u32 lanes = pcie->num_lanes; |
| 209 | u32 val = 0; |
| 210 | int ret; |
| 211 | |
| 212 | val = LANE_COUNT(lanes - 1); |
Kishon Vijay Abraham I | 7aa2562 | 2020-12-10 18:19:17 +0530 | [diff] [blame] | 213 | ret = regmap_update_bits(syscon, offset, LANE_COUNT_MASK, val); |
Kishon Vijay Abraham I | f3e2591 | 2020-07-22 16:33:15 +0530 | [diff] [blame] | 214 | if (ret) |
| 215 | dev_err(dev, "failed to set link count\n"); |
| 216 | |
| 217 | return ret; |
| 218 | } |
| 219 | |
| 220 | static int j721e_pcie_ctrl_init(struct j721e_pcie *pcie) |
| 221 | { |
Bjorn Helgaas | 19e8638 | 2021-12-22 19:10:40 -0600 | [diff] [blame] | 222 | struct device *dev = pcie->cdns_pcie->dev; |
Kishon Vijay Abraham I | f3e2591 | 2020-07-22 16:33:15 +0530 | [diff] [blame] | 223 | struct device_node *node = dev->of_node; |
Kishon Vijay Abraham I | 7aa2562 | 2020-12-10 18:19:17 +0530 | [diff] [blame] | 224 | struct of_phandle_args args; |
| 225 | unsigned int offset = 0; |
Kishon Vijay Abraham I | f3e2591 | 2020-07-22 16:33:15 +0530 | [diff] [blame] | 226 | struct regmap *syscon; |
| 227 | int ret; |
| 228 | |
| 229 | syscon = syscon_regmap_lookup_by_phandle(node, "ti,syscon-pcie-ctrl"); |
| 230 | if (IS_ERR(syscon)) { |
| 231 | dev_err(dev, "Unable to get ti,syscon-pcie-ctrl regmap\n"); |
| 232 | return PTR_ERR(syscon); |
| 233 | } |
| 234 | |
Kishon Vijay Abraham I | 7aa2562 | 2020-12-10 18:19:17 +0530 | [diff] [blame] | 235 | /* Do not error out to maintain old DT compatibility */ |
| 236 | ret = of_parse_phandle_with_fixed_args(node, "ti,syscon-pcie-ctrl", 1, |
| 237 | 0, &args); |
| 238 | if (!ret) |
| 239 | offset = args.args[0]; |
| 240 | |
| 241 | ret = j721e_pcie_set_mode(pcie, syscon, offset); |
Kishon Vijay Abraham I | f3e2591 | 2020-07-22 16:33:15 +0530 | [diff] [blame] | 242 | if (ret < 0) { |
| 243 | dev_err(dev, "Failed to set pci mode\n"); |
| 244 | return ret; |
| 245 | } |
| 246 | |
Kishon Vijay Abraham I | 7aa2562 | 2020-12-10 18:19:17 +0530 | [diff] [blame] | 247 | ret = j721e_pcie_set_link_speed(pcie, syscon, offset); |
Kishon Vijay Abraham I | f3e2591 | 2020-07-22 16:33:15 +0530 | [diff] [blame] | 248 | if (ret < 0) { |
| 249 | dev_err(dev, "Failed to set link speed\n"); |
| 250 | return ret; |
| 251 | } |
| 252 | |
Kishon Vijay Abraham I | 7aa2562 | 2020-12-10 18:19:17 +0530 | [diff] [blame] | 253 | ret = j721e_pcie_set_lane_count(pcie, syscon, offset); |
Kishon Vijay Abraham I | f3e2591 | 2020-07-22 16:33:15 +0530 | [diff] [blame] | 254 | if (ret < 0) { |
| 255 | dev_err(dev, "Failed to set num-lanes\n"); |
| 256 | return ret; |
| 257 | } |
| 258 | |
| 259 | return 0; |
| 260 | } |
| 261 | |
| 262 | static int cdns_ti_pcie_config_read(struct pci_bus *bus, unsigned int devfn, |
| 263 | int where, int size, u32 *value) |
| 264 | { |
Bjorn Helgaas | 49e427e | 2020-08-05 18:24:21 -0500 | [diff] [blame] | 265 | if (pci_is_root_bus(bus)) |
Kishon Vijay Abraham I | f3e2591 | 2020-07-22 16:33:15 +0530 | [diff] [blame] | 266 | return pci_generic_config_read32(bus, devfn, where, size, |
| 267 | value); |
| 268 | |
| 269 | return pci_generic_config_read(bus, devfn, where, size, value); |
| 270 | } |
| 271 | |
| 272 | static int cdns_ti_pcie_config_write(struct pci_bus *bus, unsigned int devfn, |
| 273 | int where, int size, u32 value) |
| 274 | { |
Bjorn Helgaas | 49e427e | 2020-08-05 18:24:21 -0500 | [diff] [blame] | 275 | if (pci_is_root_bus(bus)) |
Kishon Vijay Abraham I | f3e2591 | 2020-07-22 16:33:15 +0530 | [diff] [blame] | 276 | return pci_generic_config_write32(bus, devfn, where, size, |
| 277 | value); |
| 278 | |
| 279 | return pci_generic_config_write(bus, devfn, where, size, value); |
| 280 | } |
| 281 | |
| 282 | static struct pci_ops cdns_ti_pcie_host_ops = { |
| 283 | .map_bus = cdns_pci_map_bus, |
| 284 | .read = cdns_ti_pcie_config_read, |
| 285 | .write = cdns_ti_pcie_config_write, |
| 286 | }; |
| 287 | |
| 288 | static const struct j721e_pcie_data j721e_pcie_rc_data = { |
| 289 | .mode = PCI_MODE_RC, |
Nadeem Athani | 4740b96 | 2021-02-09 15:46:21 +0100 | [diff] [blame] | 290 | .quirk_retrain_flag = true, |
Kishon Vijay Abraham I | f1de588 | 2021-08-11 18:03:34 +0530 | [diff] [blame] | 291 | .byte_access_allowed = false, |
| 292 | .linkdown_irq_regfield = LINK_DOWN, |
Kishon Vijay Abraham I | f3e2591 | 2020-07-22 16:33:15 +0530 | [diff] [blame] | 293 | }; |
| 294 | |
| 295 | static const struct j721e_pcie_data j721e_pcie_ep_data = { |
| 296 | .mode = PCI_MODE_EP, |
Kishon Vijay Abraham I | f1de588 | 2021-08-11 18:03:34 +0530 | [diff] [blame] | 297 | .linkdown_irq_regfield = LINK_DOWN, |
| 298 | }; |
| 299 | |
| 300 | static const struct j721e_pcie_data j7200_pcie_rc_data = { |
| 301 | .mode = PCI_MODE_RC, |
| 302 | .quirk_detect_quiet_flag = true, |
| 303 | .linkdown_irq_regfield = J7200_LINK_DOWN, |
| 304 | .byte_access_allowed = true, |
| 305 | }; |
| 306 | |
| 307 | static const struct j721e_pcie_data j7200_pcie_ep_data = { |
| 308 | .mode = PCI_MODE_EP, |
| 309 | .quirk_detect_quiet_flag = true, |
Kishon Vijay Abraham I | f3e2591 | 2020-07-22 16:33:15 +0530 | [diff] [blame] | 310 | }; |
| 311 | |
Kishon Vijay Abraham I | c8a375a | 2021-08-11 18:03:35 +0530 | [diff] [blame] | 312 | static const struct j721e_pcie_data am64_pcie_rc_data = { |
| 313 | .mode = PCI_MODE_RC, |
| 314 | .linkdown_irq_regfield = J7200_LINK_DOWN, |
| 315 | .byte_access_allowed = true, |
| 316 | }; |
| 317 | |
| 318 | static const struct j721e_pcie_data am64_pcie_ep_data = { |
| 319 | .mode = PCI_MODE_EP, |
| 320 | .linkdown_irq_regfield = J7200_LINK_DOWN, |
| 321 | }; |
| 322 | |
Kishon Vijay Abraham I | f3e2591 | 2020-07-22 16:33:15 +0530 | [diff] [blame] | 323 | static const struct of_device_id of_j721e_pcie_match[] = { |
| 324 | { |
| 325 | .compatible = "ti,j721e-pcie-host", |
| 326 | .data = &j721e_pcie_rc_data, |
| 327 | }, |
| 328 | { |
| 329 | .compatible = "ti,j721e-pcie-ep", |
| 330 | .data = &j721e_pcie_ep_data, |
| 331 | }, |
Kishon Vijay Abraham I | f1de588 | 2021-08-11 18:03:34 +0530 | [diff] [blame] | 332 | { |
| 333 | .compatible = "ti,j7200-pcie-host", |
| 334 | .data = &j7200_pcie_rc_data, |
| 335 | }, |
| 336 | { |
| 337 | .compatible = "ti,j7200-pcie-ep", |
| 338 | .data = &j7200_pcie_ep_data, |
| 339 | }, |
Kishon Vijay Abraham I | c8a375a | 2021-08-11 18:03:35 +0530 | [diff] [blame] | 340 | { |
| 341 | .compatible = "ti,am64-pcie-host", |
| 342 | .data = &am64_pcie_rc_data, |
| 343 | }, |
| 344 | { |
| 345 | .compatible = "ti,am64-pcie-ep", |
| 346 | .data = &am64_pcie_ep_data, |
| 347 | }, |
Kishon Vijay Abraham I | f3e2591 | 2020-07-22 16:33:15 +0530 | [diff] [blame] | 348 | {}, |
| 349 | }; |
| 350 | |
| 351 | static int j721e_pcie_probe(struct platform_device *pdev) |
| 352 | { |
| 353 | struct device *dev = &pdev->dev; |
| 354 | struct device_node *node = dev->of_node; |
| 355 | struct pci_host_bridge *bridge; |
Bjorn Helgaas | 72de208 | 2021-12-22 19:10:39 -0600 | [diff] [blame] | 356 | const struct j721e_pcie_data *data; |
Kishon Vijay Abraham I | f3e2591 | 2020-07-22 16:33:15 +0530 | [diff] [blame] | 357 | struct cdns_pcie *cdns_pcie; |
| 358 | struct j721e_pcie *pcie; |
Bjorn Helgaas | 053ca37 | 2022-01-27 15:49:49 -0600 | [diff] [blame] | 359 | struct cdns_pcie_rc *rc = NULL; |
| 360 | struct cdns_pcie_ep *ep = NULL; |
Kishon Vijay Abraham I | f3e2591 | 2020-07-22 16:33:15 +0530 | [diff] [blame] | 361 | struct gpio_desc *gpiod; |
| 362 | void __iomem *base; |
Kishon Vijay Abraham I | 49e0efd | 2021-03-08 12:05:50 +0530 | [diff] [blame] | 363 | struct clk *clk; |
Kishon Vijay Abraham I | f3e2591 | 2020-07-22 16:33:15 +0530 | [diff] [blame] | 364 | u32 num_lanes; |
| 365 | u32 mode; |
| 366 | int ret; |
| 367 | int irq; |
| 368 | |
Bjorn Helgaas | 72de208 | 2021-12-22 19:10:39 -0600 | [diff] [blame] | 369 | data = of_device_get_match_data(dev); |
Kishon Vijay Abraham I | f3e2591 | 2020-07-22 16:33:15 +0530 | [diff] [blame] | 370 | if (!data) |
| 371 | return -EINVAL; |
| 372 | |
| 373 | mode = (u32)data->mode; |
| 374 | |
| 375 | pcie = devm_kzalloc(dev, sizeof(*pcie), GFP_KERNEL); |
| 376 | if (!pcie) |
| 377 | return -ENOMEM; |
| 378 | |
Bjorn Helgaas | 053ca37 | 2022-01-27 15:49:49 -0600 | [diff] [blame] | 379 | switch (mode) { |
| 380 | case PCI_MODE_RC: |
| 381 | if (!IS_ENABLED(CONFIG_PCIE_CADENCE_HOST)) |
| 382 | return -ENODEV; |
| 383 | |
| 384 | bridge = devm_pci_alloc_host_bridge(dev, sizeof(*rc)); |
| 385 | if (!bridge) |
| 386 | return -ENOMEM; |
| 387 | |
| 388 | if (!data->byte_access_allowed) |
| 389 | bridge->ops = &cdns_ti_pcie_host_ops; |
| 390 | rc = pci_host_bridge_priv(bridge); |
| 391 | rc->quirk_retrain_flag = data->quirk_retrain_flag; |
| 392 | rc->quirk_detect_quiet_flag = data->quirk_detect_quiet_flag; |
| 393 | |
| 394 | cdns_pcie = &rc->pcie; |
| 395 | cdns_pcie->dev = dev; |
| 396 | cdns_pcie->ops = &j721e_pcie_ops; |
| 397 | pcie->cdns_pcie = cdns_pcie; |
| 398 | break; |
| 399 | case PCI_MODE_EP: |
| 400 | if (!IS_ENABLED(CONFIG_PCIE_CADENCE_EP)) |
| 401 | return -ENODEV; |
| 402 | |
| 403 | ep = devm_kzalloc(dev, sizeof(*ep), GFP_KERNEL); |
| 404 | if (!ep) |
| 405 | return -ENOMEM; |
| 406 | |
| 407 | ep->quirk_detect_quiet_flag = data->quirk_detect_quiet_flag; |
| 408 | |
| 409 | cdns_pcie = &ep->pcie; |
| 410 | cdns_pcie->dev = dev; |
| 411 | cdns_pcie->ops = &j721e_pcie_ops; |
| 412 | pcie->cdns_pcie = cdns_pcie; |
| 413 | break; |
| 414 | default: |
| 415 | dev_err(dev, "INVALID device type %d\n", mode); |
| 416 | return 0; |
| 417 | } |
| 418 | |
Kishon Vijay Abraham I | f3e2591 | 2020-07-22 16:33:15 +0530 | [diff] [blame] | 419 | pcie->mode = mode; |
Kishon Vijay Abraham I | f1de588 | 2021-08-11 18:03:34 +0530 | [diff] [blame] | 420 | pcie->linkdown_irq_regfield = data->linkdown_irq_regfield; |
Kishon Vijay Abraham I | f3e2591 | 2020-07-22 16:33:15 +0530 | [diff] [blame] | 421 | |
| 422 | base = devm_platform_ioremap_resource_byname(pdev, "intd_cfg"); |
| 423 | if (IS_ERR(base)) |
| 424 | return PTR_ERR(base); |
| 425 | pcie->intd_cfg_base = base; |
| 426 | |
| 427 | base = devm_platform_ioremap_resource_byname(pdev, "user_cfg"); |
| 428 | if (IS_ERR(base)) |
| 429 | return PTR_ERR(base); |
| 430 | pcie->user_cfg_base = base; |
| 431 | |
| 432 | ret = of_property_read_u32(node, "num-lanes", &num_lanes); |
| 433 | if (ret || num_lanes > MAX_LANES) |
| 434 | num_lanes = 1; |
| 435 | pcie->num_lanes = num_lanes; |
| 436 | |
| 437 | if (dma_set_mask_and_coherent(dev, DMA_BIT_MASK(48))) |
| 438 | return -EINVAL; |
| 439 | |
| 440 | irq = platform_get_irq_byname(pdev, "link_state"); |
| 441 | if (irq < 0) |
| 442 | return irq; |
| 443 | |
| 444 | dev_set_drvdata(dev, pcie); |
| 445 | pm_runtime_enable(dev); |
| 446 | ret = pm_runtime_get_sync(dev); |
| 447 | if (ret < 0) { |
| 448 | dev_err(dev, "pm_runtime_get_sync failed\n"); |
| 449 | goto err_get_sync; |
| 450 | } |
| 451 | |
| 452 | ret = j721e_pcie_ctrl_init(pcie); |
| 453 | if (ret < 0) { |
| 454 | dev_err(dev, "pm_runtime_get_sync failed\n"); |
| 455 | goto err_get_sync; |
| 456 | } |
| 457 | |
| 458 | ret = devm_request_irq(dev, irq, j721e_pcie_link_irq_handler, 0, |
| 459 | "j721e-pcie-link-down-irq", pcie); |
| 460 | if (ret < 0) { |
| 461 | dev_err(dev, "failed to request link state IRQ %d\n", irq); |
| 462 | goto err_get_sync; |
| 463 | } |
| 464 | |
| 465 | j721e_pcie_config_link_irq(pcie); |
| 466 | |
| 467 | switch (mode) { |
| 468 | case PCI_MODE_RC: |
Kishon Vijay Abraham I | f3e2591 | 2020-07-22 16:33:15 +0530 | [diff] [blame] | 469 | gpiod = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW); |
| 470 | if (IS_ERR(gpiod)) { |
| 471 | ret = PTR_ERR(gpiod); |
| 472 | if (ret != -EPROBE_DEFER) |
| 473 | dev_err(dev, "Failed to get reset GPIO\n"); |
| 474 | goto err_get_sync; |
| 475 | } |
| 476 | |
| 477 | ret = cdns_pcie_init_phy(dev, cdns_pcie); |
| 478 | if (ret) { |
| 479 | dev_err(dev, "Failed to init phy\n"); |
| 480 | goto err_get_sync; |
| 481 | } |
| 482 | |
Kishon Vijay Abraham I | 49e0efd | 2021-03-08 12:05:50 +0530 | [diff] [blame] | 483 | clk = devm_clk_get_optional(dev, "pcie_refclk"); |
| 484 | if (IS_ERR(clk)) { |
| 485 | ret = PTR_ERR(clk); |
| 486 | dev_err(dev, "failed to get pcie_refclk\n"); |
| 487 | goto err_pcie_setup; |
| 488 | } |
| 489 | |
| 490 | ret = clk_prepare_enable(clk); |
| 491 | if (ret) { |
| 492 | dev_err(dev, "failed to enable pcie_refclk\n"); |
Christophe JAILLET | 496bb18 | 2021-06-27 13:46:24 +0200 | [diff] [blame] | 493 | goto err_pcie_setup; |
Kishon Vijay Abraham I | 49e0efd | 2021-03-08 12:05:50 +0530 | [diff] [blame] | 494 | } |
| 495 | pcie->refclk = clk; |
| 496 | |
Kishon Vijay Abraham I | f3e2591 | 2020-07-22 16:33:15 +0530 | [diff] [blame] | 497 | /* |
| 498 | * "Power Sequencing and Reset Signal Timings" table in |
| 499 | * PCI EXPRESS CARD ELECTROMECHANICAL SPECIFICATION, REV. 3.0 |
| 500 | * indicates PERST# should be deasserted after minimum of 100us |
| 501 | * once REFCLK is stable. The REFCLK to the connector in RC |
| 502 | * mode is selected while enabling the PHY. So deassert PERST# |
| 503 | * after 100 us. |
| 504 | */ |
| 505 | if (gpiod) { |
| 506 | usleep_range(100, 200); |
| 507 | gpiod_set_value_cansleep(gpiod, 1); |
| 508 | } |
| 509 | |
| 510 | ret = cdns_pcie_host_setup(rc); |
Kishon Vijay Abraham I | 49e0efd | 2021-03-08 12:05:50 +0530 | [diff] [blame] | 511 | if (ret < 0) { |
| 512 | clk_disable_unprepare(pcie->refclk); |
Kishon Vijay Abraham I | f3e2591 | 2020-07-22 16:33:15 +0530 | [diff] [blame] | 513 | goto err_pcie_setup; |
Kishon Vijay Abraham I | 49e0efd | 2021-03-08 12:05:50 +0530 | [diff] [blame] | 514 | } |
Kishon Vijay Abraham I | f3e2591 | 2020-07-22 16:33:15 +0530 | [diff] [blame] | 515 | |
| 516 | break; |
| 517 | case PCI_MODE_EP: |
Kishon Vijay Abraham I | f3e2591 | 2020-07-22 16:33:15 +0530 | [diff] [blame] | 518 | ret = cdns_pcie_init_phy(dev, cdns_pcie); |
| 519 | if (ret) { |
| 520 | dev_err(dev, "Failed to init phy\n"); |
| 521 | goto err_get_sync; |
| 522 | } |
| 523 | |
| 524 | ret = cdns_pcie_ep_setup(ep); |
| 525 | if (ret < 0) |
| 526 | goto err_pcie_setup; |
| 527 | |
| 528 | break; |
Kishon Vijay Abraham I | f3e2591 | 2020-07-22 16:33:15 +0530 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | return 0; |
| 532 | |
| 533 | err_pcie_setup: |
| 534 | cdns_pcie_disable_phy(cdns_pcie); |
| 535 | |
| 536 | err_get_sync: |
| 537 | pm_runtime_put(dev); |
| 538 | pm_runtime_disable(dev); |
| 539 | |
| 540 | return ret; |
| 541 | } |
| 542 | |
| 543 | static int j721e_pcie_remove(struct platform_device *pdev) |
| 544 | { |
| 545 | struct j721e_pcie *pcie = platform_get_drvdata(pdev); |
| 546 | struct cdns_pcie *cdns_pcie = pcie->cdns_pcie; |
| 547 | struct device *dev = &pdev->dev; |
| 548 | |
Kishon Vijay Abraham I | 49e0efd | 2021-03-08 12:05:50 +0530 | [diff] [blame] | 549 | clk_disable_unprepare(pcie->refclk); |
Kishon Vijay Abraham I | f3e2591 | 2020-07-22 16:33:15 +0530 | [diff] [blame] | 550 | cdns_pcie_disable_phy(cdns_pcie); |
| 551 | pm_runtime_put(dev); |
| 552 | pm_runtime_disable(dev); |
| 553 | |
| 554 | return 0; |
| 555 | } |
| 556 | |
| 557 | static struct platform_driver j721e_pcie_driver = { |
| 558 | .probe = j721e_pcie_probe, |
| 559 | .remove = j721e_pcie_remove, |
| 560 | .driver = { |
| 561 | .name = "j721e-pcie", |
| 562 | .of_match_table = of_j721e_pcie_match, |
| 563 | .suppress_bind_attrs = true, |
| 564 | }, |
| 565 | }; |
| 566 | builtin_platform_driver(j721e_pcie_driver); |