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
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>
15 #include <linux/of_device.h>
16 #include <linux/of_irq.h>
17 #include <linux/pci.h>
18 #include <linux/pm_runtime.h>
19 #include <linux/regmap.h>
21 #include "../../pci.h"
22 #include "pcie-cadence.h"
24 #define ENABLE_REG_SYS_2 0x108
25 #define STATUS_REG_SYS_2 0x508
26 #define STATUS_CLR_REG_SYS_2 0x708
27 #define LINK_DOWN BIT(1)
29 #define J721E_PCIE_USER_CMD_STATUS 0x4
30 #define LINK_TRAINING_ENABLE BIT(0)
32 #define J721E_PCIE_USER_LINKSTATUS 0x14
33 #define LINK_STATUS GENMASK(1, 0)
36 NO_RECEIVERS_DETECTED,
37 LINK_TRAINING_IN_PROGRESS,
38 LINK_UP_DL_IN_PROGRESS,
42 #define J721E_MODE_RC BIT(7)
43 #define LANE_COUNT_MASK BIT(8)
44 #define LANE_COUNT(n) ((n) << 8)
46 #define GENERATION_SEL_MASK GENMASK(1, 0)
54 struct cdns_pcie *cdns_pcie;
55 void __iomem *user_cfg_base;
56 void __iomem *intd_cfg_base;
59 enum j721e_pcie_mode {
64 struct j721e_pcie_data {
65 enum j721e_pcie_mode mode;
68 static inline u32 j721e_pcie_user_readl(struct j721e_pcie *pcie, u32 offset)
70 return readl(pcie->user_cfg_base + offset);
73 static inline void j721e_pcie_user_writel(struct j721e_pcie *pcie, u32 offset,
76 writel(value, pcie->user_cfg_base + offset);
79 static inline u32 j721e_pcie_intd_readl(struct j721e_pcie *pcie, u32 offset)
81 return readl(pcie->intd_cfg_base + offset);
84 static inline void j721e_pcie_intd_writel(struct j721e_pcie *pcie, u32 offset,
87 writel(value, pcie->intd_cfg_base + offset);
90 static irqreturn_t j721e_pcie_link_irq_handler(int irq, void *priv)
92 struct j721e_pcie *pcie = priv;
93 struct device *dev = pcie->dev;
96 reg = j721e_pcie_intd_readl(pcie, STATUS_REG_SYS_2);
97 if (!(reg & LINK_DOWN))
100 dev_err(dev, "LINK DOWN!\n");
102 j721e_pcie_intd_writel(pcie, STATUS_CLR_REG_SYS_2, LINK_DOWN);
106 static void j721e_pcie_config_link_irq(struct j721e_pcie *pcie)
110 reg = j721e_pcie_intd_readl(pcie, ENABLE_REG_SYS_2);
112 j721e_pcie_intd_writel(pcie, ENABLE_REG_SYS_2, reg);
115 static int j721e_pcie_start_link(struct cdns_pcie *cdns_pcie)
117 struct j721e_pcie *pcie = dev_get_drvdata(cdns_pcie->dev);
120 reg = j721e_pcie_user_readl(pcie, J721E_PCIE_USER_CMD_STATUS);
121 reg |= LINK_TRAINING_ENABLE;
122 j721e_pcie_user_writel(pcie, J721E_PCIE_USER_CMD_STATUS, reg);
127 static void j721e_pcie_stop_link(struct cdns_pcie *cdns_pcie)
129 struct j721e_pcie *pcie = dev_get_drvdata(cdns_pcie->dev);
132 reg = j721e_pcie_user_readl(pcie, J721E_PCIE_USER_CMD_STATUS);
133 reg &= ~LINK_TRAINING_ENABLE;
134 j721e_pcie_user_writel(pcie, J721E_PCIE_USER_CMD_STATUS, reg);
137 static bool j721e_pcie_link_up(struct cdns_pcie *cdns_pcie)
139 struct j721e_pcie *pcie = dev_get_drvdata(cdns_pcie->dev);
142 reg = j721e_pcie_user_readl(pcie, J721E_PCIE_USER_LINKSTATUS);
144 if (reg == LINK_UP_DL_COMPLETED)
150 static const struct cdns_pcie_ops j721e_pcie_ops = {
151 .start_link = j721e_pcie_start_link,
152 .stop_link = j721e_pcie_stop_link,
153 .link_up = j721e_pcie_link_up,
156 static int j721e_pcie_set_mode(struct j721e_pcie *pcie, struct regmap *syscon)
158 struct device *dev = pcie->dev;
159 u32 mask = J721E_MODE_RC;
160 u32 mode = pcie->mode;
164 if (mode == PCI_MODE_RC)
167 ret = regmap_update_bits(syscon, 0, mask, val);
169 dev_err(dev, "failed to set pcie mode\n");
174 static int j721e_pcie_set_link_speed(struct j721e_pcie *pcie,
175 struct regmap *syscon)
177 struct device *dev = pcie->dev;
178 struct device_node *np = dev->of_node;
183 link_speed = of_pci_get_max_link_speed(np);
187 val = link_speed - 1;
188 ret = regmap_update_bits(syscon, 0, GENERATION_SEL_MASK, val);
190 dev_err(dev, "failed to set link speed\n");
195 static int j721e_pcie_set_lane_count(struct j721e_pcie *pcie,
196 struct regmap *syscon)
198 struct device *dev = pcie->dev;
199 u32 lanes = pcie->num_lanes;
203 val = LANE_COUNT(lanes - 1);
204 ret = regmap_update_bits(syscon, 0, LANE_COUNT_MASK, val);
206 dev_err(dev, "failed to set link count\n");
211 static int j721e_pcie_ctrl_init(struct j721e_pcie *pcie)
213 struct device *dev = pcie->dev;
214 struct device_node *node = dev->of_node;
215 struct regmap *syscon;
218 syscon = syscon_regmap_lookup_by_phandle(node, "ti,syscon-pcie-ctrl");
219 if (IS_ERR(syscon)) {
220 dev_err(dev, "Unable to get ti,syscon-pcie-ctrl regmap\n");
221 return PTR_ERR(syscon);
224 ret = j721e_pcie_set_mode(pcie, syscon);
226 dev_err(dev, "Failed to set pci mode\n");
230 ret = j721e_pcie_set_link_speed(pcie, syscon);
232 dev_err(dev, "Failed to set link speed\n");
236 ret = j721e_pcie_set_lane_count(pcie, syscon);
238 dev_err(dev, "Failed to set num-lanes\n");
245 static int cdns_ti_pcie_config_read(struct pci_bus *bus, unsigned int devfn,
246 int where, int size, u32 *value)
248 if (pci_is_root_bus(bus))
249 return pci_generic_config_read32(bus, devfn, where, size,
252 return pci_generic_config_read(bus, devfn, where, size, value);
255 static int cdns_ti_pcie_config_write(struct pci_bus *bus, unsigned int devfn,
256 int where, int size, u32 value)
258 if (pci_is_root_bus(bus))
259 return pci_generic_config_write32(bus, devfn, where, size,
262 return pci_generic_config_write(bus, devfn, where, size, value);
265 static struct pci_ops cdns_ti_pcie_host_ops = {
266 .map_bus = cdns_pci_map_bus,
267 .read = cdns_ti_pcie_config_read,
268 .write = cdns_ti_pcie_config_write,
271 static const struct j721e_pcie_data j721e_pcie_rc_data = {
275 static const struct j721e_pcie_data j721e_pcie_ep_data = {
279 static const struct of_device_id of_j721e_pcie_match[] = {
281 .compatible = "ti,j721e-pcie-host",
282 .data = &j721e_pcie_rc_data,
285 .compatible = "ti,j721e-pcie-ep",
286 .data = &j721e_pcie_ep_data,
291 static int j721e_pcie_probe(struct platform_device *pdev)
293 struct device *dev = &pdev->dev;
294 struct device_node *node = dev->of_node;
295 struct pci_host_bridge *bridge;
296 struct j721e_pcie_data *data;
297 struct cdns_pcie *cdns_pcie;
298 struct j721e_pcie *pcie;
299 struct cdns_pcie_rc *rc;
300 struct cdns_pcie_ep *ep;
301 struct gpio_desc *gpiod;
308 data = (struct j721e_pcie_data *)of_device_get_match_data(dev);
312 mode = (u32)data->mode;
314 pcie = devm_kzalloc(dev, sizeof(*pcie), GFP_KERNEL);
321 base = devm_platform_ioremap_resource_byname(pdev, "intd_cfg");
323 return PTR_ERR(base);
324 pcie->intd_cfg_base = base;
326 base = devm_platform_ioremap_resource_byname(pdev, "user_cfg");
328 return PTR_ERR(base);
329 pcie->user_cfg_base = base;
331 ret = of_property_read_u32(node, "num-lanes", &num_lanes);
332 if (ret || num_lanes > MAX_LANES)
334 pcie->num_lanes = num_lanes;
336 if (dma_set_mask_and_coherent(dev, DMA_BIT_MASK(48)))
339 irq = platform_get_irq_byname(pdev, "link_state");
343 dev_set_drvdata(dev, pcie);
344 pm_runtime_enable(dev);
345 ret = pm_runtime_get_sync(dev);
347 dev_err(dev, "pm_runtime_get_sync failed\n");
351 ret = j721e_pcie_ctrl_init(pcie);
353 dev_err(dev, "pm_runtime_get_sync failed\n");
357 ret = devm_request_irq(dev, irq, j721e_pcie_link_irq_handler, 0,
358 "j721e-pcie-link-down-irq", pcie);
360 dev_err(dev, "failed to request link state IRQ %d\n", irq);
364 j721e_pcie_config_link_irq(pcie);
368 if (!IS_ENABLED(CONFIG_PCIE_CADENCE_HOST)) {
373 bridge = devm_pci_alloc_host_bridge(dev, sizeof(*rc));
379 bridge->ops = &cdns_ti_pcie_host_ops;
380 rc = pci_host_bridge_priv(bridge);
382 cdns_pcie = &rc->pcie;
383 cdns_pcie->dev = dev;
384 cdns_pcie->ops = &j721e_pcie_ops;
385 pcie->cdns_pcie = cdns_pcie;
387 gpiod = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW);
389 ret = PTR_ERR(gpiod);
390 if (ret != -EPROBE_DEFER)
391 dev_err(dev, "Failed to get reset GPIO\n");
395 ret = cdns_pcie_init_phy(dev, cdns_pcie);
397 dev_err(dev, "Failed to init phy\n");
402 * "Power Sequencing and Reset Signal Timings" table in
403 * PCI EXPRESS CARD ELECTROMECHANICAL SPECIFICATION, REV. 3.0
404 * indicates PERST# should be deasserted after minimum of 100us
405 * once REFCLK is stable. The REFCLK to the connector in RC
406 * mode is selected while enabling the PHY. So deassert PERST#
410 usleep_range(100, 200);
411 gpiod_set_value_cansleep(gpiod, 1);
414 ret = cdns_pcie_host_setup(rc);
420 if (!IS_ENABLED(CONFIG_PCIE_CADENCE_EP)) {
425 ep = devm_kzalloc(dev, sizeof(*ep), GFP_KERNEL);
431 cdns_pcie = &ep->pcie;
432 cdns_pcie->dev = dev;
433 cdns_pcie->ops = &j721e_pcie_ops;
434 pcie->cdns_pcie = cdns_pcie;
436 ret = cdns_pcie_init_phy(dev, cdns_pcie);
438 dev_err(dev, "Failed to init phy\n");
442 ret = cdns_pcie_ep_setup(ep);
448 dev_err(dev, "INVALID device type %d\n", mode);
454 cdns_pcie_disable_phy(cdns_pcie);
458 pm_runtime_disable(dev);
463 static int j721e_pcie_remove(struct platform_device *pdev)
465 struct j721e_pcie *pcie = platform_get_drvdata(pdev);
466 struct cdns_pcie *cdns_pcie = pcie->cdns_pcie;
467 struct device *dev = &pdev->dev;
469 cdns_pcie_disable_phy(cdns_pcie);
471 pm_runtime_disable(dev);
476 static struct platform_driver j721e_pcie_driver = {
477 .probe = j721e_pcie_probe,
478 .remove = j721e_pcie_remove,
480 .name = "j721e-pcie",
481 .of_match_table = of_j721e_pcie_match,
482 .suppress_bind_attrs = true,
485 builtin_platform_driver(j721e_pcie_driver);