1 // SPDX-License-Identifier: GPL-2.0
3 * pci-j721e - PCIe controller driver for TI's J721E SoCs
5 * Copyright (C) 2020 Texas Instruments Incorporated - http://www.ti.com
6 * Author: Kishon Vijay Abraham I <kishon@ti.com>
9 #include <linux/delay.h>
10 #include <linux/gpio/consumer.h>
12 #include <linux/irqchip/chained_irq.h>
13 #include <linux/irqdomain.h>
14 #include <linux/mfd/syscon.h>
16 #include <linux/of_device.h>
17 #include <linux/of_irq.h>
18 #include <linux/pci.h>
19 #include <linux/pm_runtime.h>
20 #include <linux/regmap.h>
22 #include "../../pci.h"
23 #include "pcie-cadence.h"
25 #define ENABLE_REG_SYS_2 0x108
26 #define STATUS_REG_SYS_2 0x508
27 #define STATUS_CLR_REG_SYS_2 0x708
28 #define LINK_DOWN BIT(1)
30 #define J721E_PCIE_USER_CMD_STATUS 0x4
31 #define LINK_TRAINING_ENABLE BIT(0)
33 #define J721E_PCIE_USER_LINKSTATUS 0x14
34 #define LINK_STATUS GENMASK(1, 0)
37 NO_RECEIVERS_DETECTED,
38 LINK_TRAINING_IN_PROGRESS,
39 LINK_UP_DL_IN_PROGRESS,
43 #define J721E_MODE_RC BIT(7)
44 #define LANE_COUNT_MASK BIT(8)
45 #define LANE_COUNT(n) ((n) << 8)
47 #define GENERATION_SEL_MASK GENMASK(1, 0)
55 struct cdns_pcie *cdns_pcie;
56 void __iomem *user_cfg_base;
57 void __iomem *intd_cfg_base;
60 enum j721e_pcie_mode {
65 struct j721e_pcie_data {
66 enum j721e_pcie_mode mode;
67 bool quirk_retrain_flag;
70 static inline u32 j721e_pcie_user_readl(struct j721e_pcie *pcie, u32 offset)
72 return readl(pcie->user_cfg_base + offset);
75 static inline void j721e_pcie_user_writel(struct j721e_pcie *pcie, u32 offset,
78 writel(value, pcie->user_cfg_base + offset);
81 static inline u32 j721e_pcie_intd_readl(struct j721e_pcie *pcie, u32 offset)
83 return readl(pcie->intd_cfg_base + offset);
86 static inline void j721e_pcie_intd_writel(struct j721e_pcie *pcie, u32 offset,
89 writel(value, pcie->intd_cfg_base + offset);
92 static irqreturn_t j721e_pcie_link_irq_handler(int irq, void *priv)
94 struct j721e_pcie *pcie = priv;
95 struct device *dev = pcie->dev;
98 reg = j721e_pcie_intd_readl(pcie, STATUS_REG_SYS_2);
99 if (!(reg & LINK_DOWN))
102 dev_err(dev, "LINK DOWN!\n");
104 j721e_pcie_intd_writel(pcie, STATUS_CLR_REG_SYS_2, LINK_DOWN);
108 static void j721e_pcie_config_link_irq(struct j721e_pcie *pcie)
112 reg = j721e_pcie_intd_readl(pcie, ENABLE_REG_SYS_2);
114 j721e_pcie_intd_writel(pcie, ENABLE_REG_SYS_2, reg);
117 static int j721e_pcie_start_link(struct cdns_pcie *cdns_pcie)
119 struct j721e_pcie *pcie = dev_get_drvdata(cdns_pcie->dev);
122 reg = j721e_pcie_user_readl(pcie, J721E_PCIE_USER_CMD_STATUS);
123 reg |= LINK_TRAINING_ENABLE;
124 j721e_pcie_user_writel(pcie, J721E_PCIE_USER_CMD_STATUS, reg);
129 static void j721e_pcie_stop_link(struct cdns_pcie *cdns_pcie)
131 struct j721e_pcie *pcie = dev_get_drvdata(cdns_pcie->dev);
134 reg = j721e_pcie_user_readl(pcie, J721E_PCIE_USER_CMD_STATUS);
135 reg &= ~LINK_TRAINING_ENABLE;
136 j721e_pcie_user_writel(pcie, J721E_PCIE_USER_CMD_STATUS, reg);
139 static bool j721e_pcie_link_up(struct cdns_pcie *cdns_pcie)
141 struct j721e_pcie *pcie = dev_get_drvdata(cdns_pcie->dev);
144 reg = j721e_pcie_user_readl(pcie, J721E_PCIE_USER_LINKSTATUS);
146 if (reg == LINK_UP_DL_COMPLETED)
152 static const struct cdns_pcie_ops j721e_pcie_ops = {
153 .start_link = j721e_pcie_start_link,
154 .stop_link = j721e_pcie_stop_link,
155 .link_up = j721e_pcie_link_up,
158 static int j721e_pcie_set_mode(struct j721e_pcie *pcie, struct regmap *syscon,
161 struct device *dev = pcie->dev;
162 u32 mask = J721E_MODE_RC;
163 u32 mode = pcie->mode;
167 if (mode == PCI_MODE_RC)
170 ret = regmap_update_bits(syscon, offset, mask, val);
172 dev_err(dev, "failed to set pcie mode\n");
177 static int j721e_pcie_set_link_speed(struct j721e_pcie *pcie,
178 struct regmap *syscon, unsigned int offset)
180 struct device *dev = pcie->dev;
181 struct device_node *np = dev->of_node;
186 link_speed = of_pci_get_max_link_speed(np);
190 val = link_speed - 1;
191 ret = regmap_update_bits(syscon, offset, GENERATION_SEL_MASK, val);
193 dev_err(dev, "failed to set link speed\n");
198 static int j721e_pcie_set_lane_count(struct j721e_pcie *pcie,
199 struct regmap *syscon, unsigned int offset)
201 struct device *dev = pcie->dev;
202 u32 lanes = pcie->num_lanes;
206 val = LANE_COUNT(lanes - 1);
207 ret = regmap_update_bits(syscon, offset, LANE_COUNT_MASK, val);
209 dev_err(dev, "failed to set link count\n");
214 static int j721e_pcie_ctrl_init(struct j721e_pcie *pcie)
216 struct device *dev = pcie->dev;
217 struct device_node *node = dev->of_node;
218 struct of_phandle_args args;
219 unsigned int offset = 0;
220 struct regmap *syscon;
223 syscon = syscon_regmap_lookup_by_phandle(node, "ti,syscon-pcie-ctrl");
224 if (IS_ERR(syscon)) {
225 dev_err(dev, "Unable to get ti,syscon-pcie-ctrl regmap\n");
226 return PTR_ERR(syscon);
229 /* Do not error out to maintain old DT compatibility */
230 ret = of_parse_phandle_with_fixed_args(node, "ti,syscon-pcie-ctrl", 1,
233 offset = args.args[0];
235 ret = j721e_pcie_set_mode(pcie, syscon, offset);
237 dev_err(dev, "Failed to set pci mode\n");
241 ret = j721e_pcie_set_link_speed(pcie, syscon, offset);
243 dev_err(dev, "Failed to set link speed\n");
247 ret = j721e_pcie_set_lane_count(pcie, syscon, offset);
249 dev_err(dev, "Failed to set num-lanes\n");
256 static int cdns_ti_pcie_config_read(struct pci_bus *bus, unsigned int devfn,
257 int where, int size, u32 *value)
259 if (pci_is_root_bus(bus))
260 return pci_generic_config_read32(bus, devfn, where, size,
263 return pci_generic_config_read(bus, devfn, where, size, value);
266 static int cdns_ti_pcie_config_write(struct pci_bus *bus, unsigned int devfn,
267 int where, int size, u32 value)
269 if (pci_is_root_bus(bus))
270 return pci_generic_config_write32(bus, devfn, where, size,
273 return pci_generic_config_write(bus, devfn, where, size, value);
276 static struct pci_ops cdns_ti_pcie_host_ops = {
277 .map_bus = cdns_pci_map_bus,
278 .read = cdns_ti_pcie_config_read,
279 .write = cdns_ti_pcie_config_write,
282 static const struct j721e_pcie_data j721e_pcie_rc_data = {
284 .quirk_retrain_flag = true,
287 static const struct j721e_pcie_data j721e_pcie_ep_data = {
291 static const struct of_device_id of_j721e_pcie_match[] = {
293 .compatible = "ti,j721e-pcie-host",
294 .data = &j721e_pcie_rc_data,
297 .compatible = "ti,j721e-pcie-ep",
298 .data = &j721e_pcie_ep_data,
303 static int j721e_pcie_probe(struct platform_device *pdev)
305 struct device *dev = &pdev->dev;
306 struct device_node *node = dev->of_node;
307 struct pci_host_bridge *bridge;
308 struct j721e_pcie_data *data;
309 struct cdns_pcie *cdns_pcie;
310 struct j721e_pcie *pcie;
311 struct cdns_pcie_rc *rc;
312 struct cdns_pcie_ep *ep;
313 struct gpio_desc *gpiod;
320 data = (struct j721e_pcie_data *)of_device_get_match_data(dev);
324 mode = (u32)data->mode;
326 pcie = devm_kzalloc(dev, sizeof(*pcie), GFP_KERNEL);
333 base = devm_platform_ioremap_resource_byname(pdev, "intd_cfg");
335 return PTR_ERR(base);
336 pcie->intd_cfg_base = base;
338 base = devm_platform_ioremap_resource_byname(pdev, "user_cfg");
340 return PTR_ERR(base);
341 pcie->user_cfg_base = base;
343 ret = of_property_read_u32(node, "num-lanes", &num_lanes);
344 if (ret || num_lanes > MAX_LANES)
346 pcie->num_lanes = num_lanes;
348 if (dma_set_mask_and_coherent(dev, DMA_BIT_MASK(48)))
351 irq = platform_get_irq_byname(pdev, "link_state");
355 dev_set_drvdata(dev, pcie);
356 pm_runtime_enable(dev);
357 ret = pm_runtime_get_sync(dev);
359 dev_err(dev, "pm_runtime_get_sync failed\n");
363 ret = j721e_pcie_ctrl_init(pcie);
365 dev_err(dev, "pm_runtime_get_sync failed\n");
369 ret = devm_request_irq(dev, irq, j721e_pcie_link_irq_handler, 0,
370 "j721e-pcie-link-down-irq", pcie);
372 dev_err(dev, "failed to request link state IRQ %d\n", irq);
376 j721e_pcie_config_link_irq(pcie);
380 if (!IS_ENABLED(CONFIG_PCIE_CADENCE_HOST)) {
385 bridge = devm_pci_alloc_host_bridge(dev, sizeof(*rc));
391 bridge->ops = &cdns_ti_pcie_host_ops;
392 rc = pci_host_bridge_priv(bridge);
393 rc->quirk_retrain_flag = data->quirk_retrain_flag;
395 cdns_pcie = &rc->pcie;
396 cdns_pcie->dev = dev;
397 cdns_pcie->ops = &j721e_pcie_ops;
398 pcie->cdns_pcie = cdns_pcie;
400 gpiod = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW);
402 ret = PTR_ERR(gpiod);
403 if (ret != -EPROBE_DEFER)
404 dev_err(dev, "Failed to get reset GPIO\n");
408 ret = cdns_pcie_init_phy(dev, cdns_pcie);
410 dev_err(dev, "Failed to init phy\n");
415 * "Power Sequencing and Reset Signal Timings" table in
416 * PCI EXPRESS CARD ELECTROMECHANICAL SPECIFICATION, REV. 3.0
417 * indicates PERST# should be deasserted after minimum of 100us
418 * once REFCLK is stable. The REFCLK to the connector in RC
419 * mode is selected while enabling the PHY. So deassert PERST#
423 usleep_range(100, 200);
424 gpiod_set_value_cansleep(gpiod, 1);
427 ret = cdns_pcie_host_setup(rc);
433 if (!IS_ENABLED(CONFIG_PCIE_CADENCE_EP)) {
438 ep = devm_kzalloc(dev, sizeof(*ep), GFP_KERNEL);
444 cdns_pcie = &ep->pcie;
445 cdns_pcie->dev = dev;
446 cdns_pcie->ops = &j721e_pcie_ops;
447 pcie->cdns_pcie = cdns_pcie;
449 ret = cdns_pcie_init_phy(dev, cdns_pcie);
451 dev_err(dev, "Failed to init phy\n");
455 ret = cdns_pcie_ep_setup(ep);
461 dev_err(dev, "INVALID device type %d\n", mode);
467 cdns_pcie_disable_phy(cdns_pcie);
471 pm_runtime_disable(dev);
476 static int j721e_pcie_remove(struct platform_device *pdev)
478 struct j721e_pcie *pcie = platform_get_drvdata(pdev);
479 struct cdns_pcie *cdns_pcie = pcie->cdns_pcie;
480 struct device *dev = &pdev->dev;
482 cdns_pcie_disable_phy(cdns_pcie);
484 pm_runtime_disable(dev);
489 static struct platform_driver j721e_pcie_driver = {
490 .probe = j721e_pcie_probe,
491 .remove = j721e_pcie_remove,
493 .name = "j721e-pcie",
494 .of_match_table = of_j721e_pcie_match,
495 .suppress_bind_attrs = true,
498 builtin_platform_driver(j721e_pcie_driver);