blob: 768d33f9ebc87569d4e247bba2588a958e529b40 [file] [log] [blame]
Kishon Vijay Abraham If3e25912020-07-22 16:33:15 +05301// SPDX-License-Identifier: GPL-2.0
Krzysztof WilczyƄski43395d92021-03-11 00:17:17 +00002/*
Kishon Vijay Abraham If3e25912020-07-22 16:33:15 +05303 * 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 I49e0efd2021-03-08 12:05:50 +05309#include <linux/clk.h>
Kishon Vijay Abraham If3e25912020-07-22 16:33:15 +053010#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 I7aa25622020-12-10 18:19:17 +053016#include <linux/of.h>
Kishon Vijay Abraham If3e25912020-07-22 16:33:15 +053017#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 If1de5882021-08-11 18:03:34 +053030#define J7200_LINK_DOWN BIT(10)
Kishon Vijay Abraham If3e25912020-07-22 16:33:15 +053031
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
38enum 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
53struct j721e_pcie {
Bjorn Helgaas19e86382021-12-22 19:10:40 -060054 struct cdns_pcie *cdns_pcie;
Kishon Vijay Abraham I49e0efd2021-03-08 12:05:50 +053055 struct clk *refclk;
Kishon Vijay Abraham If3e25912020-07-22 16:33:15 +053056 u32 mode;
57 u32 num_lanes;
Kishon Vijay Abraham If3e25912020-07-22 16:33:15 +053058 void __iomem *user_cfg_base;
59 void __iomem *intd_cfg_base;
Kishon Vijay Abraham If1de5882021-08-11 18:03:34 +053060 u32 linkdown_irq_regfield;
Kishon Vijay Abraham If3e25912020-07-22 16:33:15 +053061};
62
63enum j721e_pcie_mode {
64 PCI_MODE_RC,
65 PCI_MODE_EP,
66};
67
68struct j721e_pcie_data {
69 enum j721e_pcie_mode mode;
Kishon Vijay Abraham If4455742021-08-11 18:03:32 +053070 unsigned int quirk_retrain_flag:1;
Kishon Vijay Abraham If1de5882021-08-11 18:03:34 +053071 unsigned int quirk_detect_quiet_flag:1;
72 u32 linkdown_irq_regfield;
73 unsigned int byte_access_allowed:1;
Kishon Vijay Abraham If3e25912020-07-22 16:33:15 +053074};
75
76static inline u32 j721e_pcie_user_readl(struct j721e_pcie *pcie, u32 offset)
77{
78 return readl(pcie->user_cfg_base + offset);
79}
80
81static 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
87static inline u32 j721e_pcie_intd_readl(struct j721e_pcie *pcie, u32 offset)
88{
89 return readl(pcie->intd_cfg_base + offset);
90}
91
92static 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
98static irqreturn_t j721e_pcie_link_irq_handler(int irq, void *priv)
99{
100 struct j721e_pcie *pcie = priv;
Bjorn Helgaas19e86382021-12-22 19:10:40 -0600101 struct device *dev = pcie->cdns_pcie->dev;
Kishon Vijay Abraham If3e25912020-07-22 16:33:15 +0530102 u32 reg;
103
104 reg = j721e_pcie_intd_readl(pcie, STATUS_REG_SYS_2);
Kishon Vijay Abraham If1de5882021-08-11 18:03:34 +0530105 if (!(reg & pcie->linkdown_irq_regfield))
Kishon Vijay Abraham If3e25912020-07-22 16:33:15 +0530106 return IRQ_NONE;
107
108 dev_err(dev, "LINK DOWN!\n");
109
Kishon Vijay Abraham If1de5882021-08-11 18:03:34 +0530110 j721e_pcie_intd_writel(pcie, STATUS_CLR_REG_SYS_2, pcie->linkdown_irq_regfield);
Kishon Vijay Abraham If3e25912020-07-22 16:33:15 +0530111 return IRQ_HANDLED;
112}
113
114static 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 If1de5882021-08-11 18:03:34 +0530119 reg |= pcie->linkdown_irq_regfield;
Kishon Vijay Abraham If3e25912020-07-22 16:33:15 +0530120 j721e_pcie_intd_writel(pcie, ENABLE_REG_SYS_2, reg);
121}
122
123static 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
135static 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
145static 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
158static 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 I7aa25622020-12-10 18:19:17 +0530164static int j721e_pcie_set_mode(struct j721e_pcie *pcie, struct regmap *syscon,
165 unsigned int offset)
Kishon Vijay Abraham If3e25912020-07-22 16:33:15 +0530166{
Bjorn Helgaas19e86382021-12-22 19:10:40 -0600167 struct device *dev = pcie->cdns_pcie->dev;
Kishon Vijay Abraham If3e25912020-07-22 16:33:15 +0530168 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 I7aa25622020-12-10 18:19:17 +0530176 ret = regmap_update_bits(syscon, offset, mask, val);
Kishon Vijay Abraham If3e25912020-07-22 16:33:15 +0530177 if (ret)
178 dev_err(dev, "failed to set pcie mode\n");
179
180 return ret;
181}
182
183static int j721e_pcie_set_link_speed(struct j721e_pcie *pcie,
Kishon Vijay Abraham I7aa25622020-12-10 18:19:17 +0530184 struct regmap *syscon, unsigned int offset)
Kishon Vijay Abraham If3e25912020-07-22 16:33:15 +0530185{
Bjorn Helgaas19e86382021-12-22 19:10:40 -0600186 struct device *dev = pcie->cdns_pcie->dev;
Kishon Vijay Abraham If3e25912020-07-22 16:33:15 +0530187 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 I7aa25622020-12-10 18:19:17 +0530197 ret = regmap_update_bits(syscon, offset, GENERATION_SEL_MASK, val);
Kishon Vijay Abraham If3e25912020-07-22 16:33:15 +0530198 if (ret)
199 dev_err(dev, "failed to set link speed\n");
200
201 return ret;
202}
203
204static int j721e_pcie_set_lane_count(struct j721e_pcie *pcie,
Kishon Vijay Abraham I7aa25622020-12-10 18:19:17 +0530205 struct regmap *syscon, unsigned int offset)
Kishon Vijay Abraham If3e25912020-07-22 16:33:15 +0530206{
Bjorn Helgaas19e86382021-12-22 19:10:40 -0600207 struct device *dev = pcie->cdns_pcie->dev;
Kishon Vijay Abraham If3e25912020-07-22 16:33:15 +0530208 u32 lanes = pcie->num_lanes;
209 u32 val = 0;
210 int ret;
211
212 val = LANE_COUNT(lanes - 1);
Kishon Vijay Abraham I7aa25622020-12-10 18:19:17 +0530213 ret = regmap_update_bits(syscon, offset, LANE_COUNT_MASK, val);
Kishon Vijay Abraham If3e25912020-07-22 16:33:15 +0530214 if (ret)
215 dev_err(dev, "failed to set link count\n");
216
217 return ret;
218}
219
220static int j721e_pcie_ctrl_init(struct j721e_pcie *pcie)
221{
Bjorn Helgaas19e86382021-12-22 19:10:40 -0600222 struct device *dev = pcie->cdns_pcie->dev;
Kishon Vijay Abraham If3e25912020-07-22 16:33:15 +0530223 struct device_node *node = dev->of_node;
Kishon Vijay Abraham I7aa25622020-12-10 18:19:17 +0530224 struct of_phandle_args args;
225 unsigned int offset = 0;
Kishon Vijay Abraham If3e25912020-07-22 16:33:15 +0530226 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 I7aa25622020-12-10 18:19:17 +0530235 /* 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 If3e25912020-07-22 16:33:15 +0530242 if (ret < 0) {
243 dev_err(dev, "Failed to set pci mode\n");
244 return ret;
245 }
246
Kishon Vijay Abraham I7aa25622020-12-10 18:19:17 +0530247 ret = j721e_pcie_set_link_speed(pcie, syscon, offset);
Kishon Vijay Abraham If3e25912020-07-22 16:33:15 +0530248 if (ret < 0) {
249 dev_err(dev, "Failed to set link speed\n");
250 return ret;
251 }
252
Kishon Vijay Abraham I7aa25622020-12-10 18:19:17 +0530253 ret = j721e_pcie_set_lane_count(pcie, syscon, offset);
Kishon Vijay Abraham If3e25912020-07-22 16:33:15 +0530254 if (ret < 0) {
255 dev_err(dev, "Failed to set num-lanes\n");
256 return ret;
257 }
258
259 return 0;
260}
261
262static int cdns_ti_pcie_config_read(struct pci_bus *bus, unsigned int devfn,
263 int where, int size, u32 *value)
264{
Bjorn Helgaas49e427e2020-08-05 18:24:21 -0500265 if (pci_is_root_bus(bus))
Kishon Vijay Abraham If3e25912020-07-22 16:33:15 +0530266 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
272static int cdns_ti_pcie_config_write(struct pci_bus *bus, unsigned int devfn,
273 int where, int size, u32 value)
274{
Bjorn Helgaas49e427e2020-08-05 18:24:21 -0500275 if (pci_is_root_bus(bus))
Kishon Vijay Abraham If3e25912020-07-22 16:33:15 +0530276 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
282static 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
288static const struct j721e_pcie_data j721e_pcie_rc_data = {
289 .mode = PCI_MODE_RC,
Nadeem Athani4740b962021-02-09 15:46:21 +0100290 .quirk_retrain_flag = true,
Kishon Vijay Abraham If1de5882021-08-11 18:03:34 +0530291 .byte_access_allowed = false,
292 .linkdown_irq_regfield = LINK_DOWN,
Kishon Vijay Abraham If3e25912020-07-22 16:33:15 +0530293};
294
295static const struct j721e_pcie_data j721e_pcie_ep_data = {
296 .mode = PCI_MODE_EP,
Kishon Vijay Abraham If1de5882021-08-11 18:03:34 +0530297 .linkdown_irq_regfield = LINK_DOWN,
298};
299
300static 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
307static const struct j721e_pcie_data j7200_pcie_ep_data = {
308 .mode = PCI_MODE_EP,
309 .quirk_detect_quiet_flag = true,
Kishon Vijay Abraham If3e25912020-07-22 16:33:15 +0530310};
311
Kishon Vijay Abraham Ic8a375a2021-08-11 18:03:35 +0530312static 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
318static 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 If3e25912020-07-22 16:33:15 +0530323static 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 If1de5882021-08-11 18:03:34 +0530332 {
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 Ic8a375a2021-08-11 18:03:35 +0530340 {
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 If3e25912020-07-22 16:33:15 +0530348 {},
349};
350
351static 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 Helgaas72de2082021-12-22 19:10:39 -0600356 const struct j721e_pcie_data *data;
Kishon Vijay Abraham If3e25912020-07-22 16:33:15 +0530357 struct cdns_pcie *cdns_pcie;
358 struct j721e_pcie *pcie;
Bjorn Helgaas053ca372022-01-27 15:49:49 -0600359 struct cdns_pcie_rc *rc = NULL;
360 struct cdns_pcie_ep *ep = NULL;
Kishon Vijay Abraham If3e25912020-07-22 16:33:15 +0530361 struct gpio_desc *gpiod;
362 void __iomem *base;
Kishon Vijay Abraham I49e0efd2021-03-08 12:05:50 +0530363 struct clk *clk;
Kishon Vijay Abraham If3e25912020-07-22 16:33:15 +0530364 u32 num_lanes;
365 u32 mode;
366 int ret;
367 int irq;
368
Bjorn Helgaas72de2082021-12-22 19:10:39 -0600369 data = of_device_get_match_data(dev);
Kishon Vijay Abraham If3e25912020-07-22 16:33:15 +0530370 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 Helgaas053ca372022-01-27 15:49:49 -0600379 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 If3e25912020-07-22 16:33:15 +0530419 pcie->mode = mode;
Kishon Vijay Abraham If1de5882021-08-11 18:03:34 +0530420 pcie->linkdown_irq_regfield = data->linkdown_irq_regfield;
Kishon Vijay Abraham If3e25912020-07-22 16:33:15 +0530421
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 If3e25912020-07-22 16:33:15 +0530469 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 I49e0efd2021-03-08 12:05:50 +0530483 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 JAILLET496bb182021-06-27 13:46:24 +0200493 goto err_pcie_setup;
Kishon Vijay Abraham I49e0efd2021-03-08 12:05:50 +0530494 }
495 pcie->refclk = clk;
496
Kishon Vijay Abraham If3e25912020-07-22 16:33:15 +0530497 /*
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 I49e0efd2021-03-08 12:05:50 +0530511 if (ret < 0) {
512 clk_disable_unprepare(pcie->refclk);
Kishon Vijay Abraham If3e25912020-07-22 16:33:15 +0530513 goto err_pcie_setup;
Kishon Vijay Abraham I49e0efd2021-03-08 12:05:50 +0530514 }
Kishon Vijay Abraham If3e25912020-07-22 16:33:15 +0530515
516 break;
517 case PCI_MODE_EP:
Kishon Vijay Abraham If3e25912020-07-22 16:33:15 +0530518 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 If3e25912020-07-22 16:33:15 +0530529 }
530
531 return 0;
532
533err_pcie_setup:
534 cdns_pcie_disable_phy(cdns_pcie);
535
536err_get_sync:
537 pm_runtime_put(dev);
538 pm_runtime_disable(dev);
539
540 return ret;
541}
542
543static 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 I49e0efd2021-03-08 12:05:50 +0530549 clk_disable_unprepare(pcie->refclk);
Kishon Vijay Abraham If3e25912020-07-22 16:33:15 +0530550 cdns_pcie_disable_phy(cdns_pcie);
551 pm_runtime_put(dev);
552 pm_runtime_disable(dev);
553
554 return 0;
555}
556
557static 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};
566builtin_platform_driver(j721e_pcie_driver);