1 // SPDX-License-Identifier: GPL-2.0
2 /*
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
9 #include <linux/clk.h>
10 #include <linux/clk-provider.h>
11 #include <linux/container_of.h>
12 #include <linux/delay.h>
13 #include <linux/gpio/consumer.h>
14 #include <linux/io.h>
15 #include <linux/irqchip/chained_irq.h>
16 #include <linux/irqdomain.h>
17 #include <linux/mfd/syscon.h>
18 #include <linux/of.h>
19 #include <linux/pci.h>
20 #include <linux/platform_device.h>
21 #include <linux/pm_runtime.h>
22 #include <linux/regmap.h>
23
24 #include "../../pci.h"
25 #include "pcie-cadence.h"
26
27 #define cdns_pcie_to_rc(p) container_of(p, struct cdns_pcie_rc, pcie)
28
29 #define ENABLE_REG_SYS_2 0x108
30 #define STATUS_REG_SYS_2 0x508
31 #define STATUS_CLR_REG_SYS_2 0x708
32 #define LINK_DOWN BIT(1)
33 #define J7200_LINK_DOWN BIT(10)
34
35 #define J721E_PCIE_USER_CMD_STATUS 0x4
36 #define LINK_TRAINING_ENABLE BIT(0)
37
38 #define J721E_PCIE_USER_LINKSTATUS 0x14
39 #define LINK_STATUS GENMASK(1, 0)
40
41 enum link_status {
42 NO_RECEIVERS_DETECTED,
43 LINK_TRAINING_IN_PROGRESS,
44 LINK_UP_DL_IN_PROGRESS,
45 LINK_UP_DL_COMPLETED,
46 };
47
48 #define J721E_MODE_RC BIT(7)
49 #define LANE_COUNT(n) ((n) << 8)
50
51 #define ACSPCIE_PAD_DISABLE_MASK GENMASK(1, 0)
52 #define GENERATION_SEL_MASK GENMASK(1, 0)
53
54 struct j721e_pcie {
55 struct cdns_pcie *cdns_pcie;
56 struct clk *refclk;
57 u32 mode;
58 u32 num_lanes;
59 u32 max_lanes;
60 struct gpio_desc *reset_gpio;
61 void __iomem *user_cfg_base;
62 void __iomem *intd_cfg_base;
63 u32 linkdown_irq_regfield;
64 };
65
66 enum j721e_pcie_mode {
67 PCI_MODE_RC,
68 PCI_MODE_EP,
69 };
70
71 struct j721e_pcie_data {
72 enum j721e_pcie_mode mode;
73 unsigned int quirk_retrain_flag:1;
74 unsigned int quirk_detect_quiet_flag:1;
75 unsigned int quirk_disable_flr:1;
76 u32 linkdown_irq_regfield;
77 unsigned int byte_access_allowed:1;
78 unsigned int max_lanes;
79 };
80
j721e_pcie_user_readl(struct j721e_pcie * pcie,u32 offset)81 static inline u32 j721e_pcie_user_readl(struct j721e_pcie *pcie, u32 offset)
82 {
83 return readl(pcie->user_cfg_base + offset);
84 }
85
j721e_pcie_user_writel(struct j721e_pcie * pcie,u32 offset,u32 value)86 static inline void j721e_pcie_user_writel(struct j721e_pcie *pcie, u32 offset,
87 u32 value)
88 {
89 writel(value, pcie->user_cfg_base + offset);
90 }
91
j721e_pcie_intd_readl(struct j721e_pcie * pcie,u32 offset)92 static inline u32 j721e_pcie_intd_readl(struct j721e_pcie *pcie, u32 offset)
93 {
94 return readl(pcie->intd_cfg_base + offset);
95 }
96
j721e_pcie_intd_writel(struct j721e_pcie * pcie,u32 offset,u32 value)97 static inline void j721e_pcie_intd_writel(struct j721e_pcie *pcie, u32 offset,
98 u32 value)
99 {
100 writel(value, pcie->intd_cfg_base + offset);
101 }
102
j721e_pcie_link_irq_handler(int irq,void * priv)103 static irqreturn_t j721e_pcie_link_irq_handler(int irq, void *priv)
104 {
105 struct j721e_pcie *pcie = priv;
106 struct device *dev = pcie->cdns_pcie->dev;
107 u32 reg;
108
109 reg = j721e_pcie_intd_readl(pcie, STATUS_REG_SYS_2);
110 if (!(reg & pcie->linkdown_irq_regfield))
111 return IRQ_NONE;
112
113 dev_err(dev, "LINK DOWN!\n");
114
115 j721e_pcie_intd_writel(pcie, STATUS_CLR_REG_SYS_2, pcie->linkdown_irq_regfield);
116 return IRQ_HANDLED;
117 }
118
j721e_pcie_config_link_irq(struct j721e_pcie * pcie)119 static void j721e_pcie_config_link_irq(struct j721e_pcie *pcie)
120 {
121 u32 reg;
122
123 reg = j721e_pcie_intd_readl(pcie, ENABLE_REG_SYS_2);
124 reg |= pcie->linkdown_irq_regfield;
125 j721e_pcie_intd_writel(pcie, ENABLE_REG_SYS_2, reg);
126 }
127
j721e_pcie_start_link(struct cdns_pcie * cdns_pcie)128 static int j721e_pcie_start_link(struct cdns_pcie *cdns_pcie)
129 {
130 struct j721e_pcie *pcie = dev_get_drvdata(cdns_pcie->dev);
131 u32 reg;
132
133 reg = j721e_pcie_user_readl(pcie, J721E_PCIE_USER_CMD_STATUS);
134 reg |= LINK_TRAINING_ENABLE;
135 j721e_pcie_user_writel(pcie, J721E_PCIE_USER_CMD_STATUS, reg);
136
137 return 0;
138 }
139
j721e_pcie_stop_link(struct cdns_pcie * cdns_pcie)140 static void j721e_pcie_stop_link(struct cdns_pcie *cdns_pcie)
141 {
142 struct j721e_pcie *pcie = dev_get_drvdata(cdns_pcie->dev);
143 u32 reg;
144
145 reg = j721e_pcie_user_readl(pcie, J721E_PCIE_USER_CMD_STATUS);
146 reg &= ~LINK_TRAINING_ENABLE;
147 j721e_pcie_user_writel(pcie, J721E_PCIE_USER_CMD_STATUS, reg);
148 }
149
j721e_pcie_link_up(struct cdns_pcie * cdns_pcie)150 static bool j721e_pcie_link_up(struct cdns_pcie *cdns_pcie)
151 {
152 struct j721e_pcie *pcie = dev_get_drvdata(cdns_pcie->dev);
153 u32 reg;
154
155 reg = j721e_pcie_user_readl(pcie, J721E_PCIE_USER_LINKSTATUS);
156 reg &= LINK_STATUS;
157 if (reg == LINK_UP_DL_COMPLETED)
158 return true;
159
160 return false;
161 }
162
163 static const struct cdns_pcie_ops j721e_pcie_ops = {
164 .start_link = j721e_pcie_start_link,
165 .stop_link = j721e_pcie_stop_link,
166 .link_up = j721e_pcie_link_up,
167 };
168
j721e_pcie_set_mode(struct j721e_pcie * pcie,struct regmap * syscon,unsigned int offset)169 static int j721e_pcie_set_mode(struct j721e_pcie *pcie, struct regmap *syscon,
170 unsigned int offset)
171 {
172 struct device *dev = pcie->cdns_pcie->dev;
173 u32 mask = J721E_MODE_RC;
174 u32 mode = pcie->mode;
175 u32 val = 0;
176 int ret = 0;
177
178 if (mode == PCI_MODE_RC)
179 val = J721E_MODE_RC;
180
181 ret = regmap_update_bits(syscon, offset, mask, val);
182 if (ret)
183 dev_err(dev, "failed to set pcie mode\n");
184
185 return ret;
186 }
187
j721e_pcie_set_link_speed(struct j721e_pcie * pcie,struct regmap * syscon,unsigned int offset)188 static int j721e_pcie_set_link_speed(struct j721e_pcie *pcie,
189 struct regmap *syscon, unsigned int offset)
190 {
191 struct device *dev = pcie->cdns_pcie->dev;
192 struct device_node *np = dev->of_node;
193 int link_speed;
194 u32 val = 0;
195 int ret;
196
197 link_speed = of_pci_get_max_link_speed(np);
198 if (link_speed < 2)
199 link_speed = 2;
200
201 val = link_speed - 1;
202 ret = regmap_update_bits(syscon, offset, GENERATION_SEL_MASK, val);
203 if (ret)
204 dev_err(dev, "failed to set link speed\n");
205
206 return ret;
207 }
208
j721e_pcie_set_lane_count(struct j721e_pcie * pcie,struct regmap * syscon,unsigned int offset)209 static int j721e_pcie_set_lane_count(struct j721e_pcie *pcie,
210 struct regmap *syscon, unsigned int offset)
211 {
212 struct device *dev = pcie->cdns_pcie->dev;
213 u32 lanes = pcie->num_lanes;
214 u32 mask = BIT(8);
215 u32 val = 0;
216 int ret;
217
218 if (pcie->max_lanes == 4)
219 mask = GENMASK(9, 8);
220
221 val = LANE_COUNT(lanes - 1);
222 ret = regmap_update_bits(syscon, offset, mask, val);
223 if (ret)
224 dev_err(dev, "failed to set link count\n");
225
226 return ret;
227 }
228
j721e_enable_acspcie_refclk(struct j721e_pcie * pcie,struct regmap * syscon)229 static int j721e_enable_acspcie_refclk(struct j721e_pcie *pcie,
230 struct regmap *syscon)
231 {
232 struct device *dev = pcie->cdns_pcie->dev;
233 struct device_node *node = dev->of_node;
234 u32 mask = ACSPCIE_PAD_DISABLE_MASK;
235 struct of_phandle_args args;
236 u32 val;
237 int ret;
238
239 ret = of_parse_phandle_with_fixed_args(node,
240 "ti,syscon-acspcie-proxy-ctrl",
241 1, 0, &args);
242 if (ret) {
243 dev_err(dev,
244 "ti,syscon-acspcie-proxy-ctrl has invalid arguments\n");
245 return ret;
246 }
247
248 /* Clear PAD IO disable bits to enable refclk output */
249 val = ~(args.args[0]);
250 ret = regmap_update_bits(syscon, 0, mask, val);
251 if (ret) {
252 dev_err(dev, "failed to enable ACSPCIE refclk: %d\n", ret);
253 return ret;
254 }
255
256 return 0;
257 }
258
j721e_pcie_ctrl_init(struct j721e_pcie * pcie)259 static int j721e_pcie_ctrl_init(struct j721e_pcie *pcie)
260 {
261 struct device *dev = pcie->cdns_pcie->dev;
262 struct device_node *node = dev->of_node;
263 struct of_phandle_args args;
264 unsigned int offset = 0;
265 struct regmap *syscon;
266 int ret;
267
268 syscon = syscon_regmap_lookup_by_phandle(node, "ti,syscon-pcie-ctrl");
269 if (IS_ERR(syscon)) {
270 dev_err(dev, "Unable to get ti,syscon-pcie-ctrl regmap\n");
271 return PTR_ERR(syscon);
272 }
273
274 /* Do not error out to maintain old DT compatibility */
275 ret = of_parse_phandle_with_fixed_args(node, "ti,syscon-pcie-ctrl", 1,
276 0, &args);
277 if (!ret)
278 offset = args.args[0];
279
280 ret = j721e_pcie_set_mode(pcie, syscon, offset);
281 if (ret < 0) {
282 dev_err(dev, "Failed to set pci mode\n");
283 return ret;
284 }
285
286 ret = j721e_pcie_set_link_speed(pcie, syscon, offset);
287 if (ret < 0) {
288 dev_err(dev, "Failed to set link speed\n");
289 return ret;
290 }
291
292 ret = j721e_pcie_set_lane_count(pcie, syscon, offset);
293 if (ret < 0) {
294 dev_err(dev, "Failed to set num-lanes\n");
295 return ret;
296 }
297
298 /* Enable ACSPCIE refclk output if the optional property exists */
299 syscon = syscon_regmap_lookup_by_phandle_optional(node,
300 "ti,syscon-acspcie-proxy-ctrl");
301 if (!syscon)
302 return 0;
303
304 return j721e_enable_acspcie_refclk(pcie, syscon);
305 }
306
cdns_ti_pcie_config_read(struct pci_bus * bus,unsigned int devfn,int where,int size,u32 * value)307 static int cdns_ti_pcie_config_read(struct pci_bus *bus, unsigned int devfn,
308 int where, int size, u32 *value)
309 {
310 if (pci_is_root_bus(bus))
311 return pci_generic_config_read32(bus, devfn, where, size,
312 value);
313
314 return pci_generic_config_read(bus, devfn, where, size, value);
315 }
316
cdns_ti_pcie_config_write(struct pci_bus * bus,unsigned int devfn,int where,int size,u32 value)317 static int cdns_ti_pcie_config_write(struct pci_bus *bus, unsigned int devfn,
318 int where, int size, u32 value)
319 {
320 if (pci_is_root_bus(bus))
321 return pci_generic_config_write32(bus, devfn, where, size,
322 value);
323
324 return pci_generic_config_write(bus, devfn, where, size, value);
325 }
326
327 static struct pci_ops cdns_ti_pcie_host_ops = {
328 .map_bus = cdns_pci_map_bus,
329 .read = cdns_ti_pcie_config_read,
330 .write = cdns_ti_pcie_config_write,
331 };
332
333 static const struct j721e_pcie_data j721e_pcie_rc_data = {
334 .mode = PCI_MODE_RC,
335 .quirk_retrain_flag = true,
336 .byte_access_allowed = false,
337 .linkdown_irq_regfield = LINK_DOWN,
338 .max_lanes = 2,
339 };
340
341 static const struct j721e_pcie_data j721e_pcie_ep_data = {
342 .mode = PCI_MODE_EP,
343 .linkdown_irq_regfield = LINK_DOWN,
344 .max_lanes = 2,
345 };
346
347 static const struct j721e_pcie_data j7200_pcie_rc_data = {
348 .mode = PCI_MODE_RC,
349 .quirk_detect_quiet_flag = true,
350 .linkdown_irq_regfield = J7200_LINK_DOWN,
351 .byte_access_allowed = true,
352 .max_lanes = 2,
353 };
354
355 static const struct j721e_pcie_data j7200_pcie_ep_data = {
356 .mode = PCI_MODE_EP,
357 .quirk_detect_quiet_flag = true,
358 .linkdown_irq_regfield = J7200_LINK_DOWN,
359 .quirk_disable_flr = true,
360 .max_lanes = 2,
361 };
362
363 static const struct j721e_pcie_data am64_pcie_rc_data = {
364 .mode = PCI_MODE_RC,
365 .linkdown_irq_regfield = J7200_LINK_DOWN,
366 .byte_access_allowed = true,
367 .max_lanes = 1,
368 };
369
370 static const struct j721e_pcie_data am64_pcie_ep_data = {
371 .mode = PCI_MODE_EP,
372 .linkdown_irq_regfield = J7200_LINK_DOWN,
373 .max_lanes = 1,
374 };
375
376 static const struct j721e_pcie_data j784s4_pcie_rc_data = {
377 .mode = PCI_MODE_RC,
378 .quirk_retrain_flag = true,
379 .byte_access_allowed = false,
380 .linkdown_irq_regfield = J7200_LINK_DOWN,
381 .max_lanes = 4,
382 };
383
384 static const struct j721e_pcie_data j784s4_pcie_ep_data = {
385 .mode = PCI_MODE_EP,
386 .linkdown_irq_regfield = J7200_LINK_DOWN,
387 .max_lanes = 4,
388 };
389
390 static const struct of_device_id of_j721e_pcie_match[] = {
391 {
392 .compatible = "ti,j721e-pcie-host",
393 .data = &j721e_pcie_rc_data,
394 },
395 {
396 .compatible = "ti,j721e-pcie-ep",
397 .data = &j721e_pcie_ep_data,
398 },
399 {
400 .compatible = "ti,j7200-pcie-host",
401 .data = &j7200_pcie_rc_data,
402 },
403 {
404 .compatible = "ti,j7200-pcie-ep",
405 .data = &j7200_pcie_ep_data,
406 },
407 {
408 .compatible = "ti,am64-pcie-host",
409 .data = &am64_pcie_rc_data,
410 },
411 {
412 .compatible = "ti,am64-pcie-ep",
413 .data = &am64_pcie_ep_data,
414 },
415 {
416 .compatible = "ti,j784s4-pcie-host",
417 .data = &j784s4_pcie_rc_data,
418 },
419 {
420 .compatible = "ti,j784s4-pcie-ep",
421 .data = &j784s4_pcie_ep_data,
422 },
423 {},
424 };
425
j721e_pcie_probe(struct platform_device * pdev)426 static int j721e_pcie_probe(struct platform_device *pdev)
427 {
428 struct device *dev = &pdev->dev;
429 struct device_node *node = dev->of_node;
430 struct pci_host_bridge *bridge;
431 const struct j721e_pcie_data *data;
432 struct cdns_pcie *cdns_pcie;
433 struct j721e_pcie *pcie;
434 struct cdns_pcie_rc *rc = NULL;
435 struct cdns_pcie_ep *ep = NULL;
436 struct gpio_desc *gpiod;
437 void __iomem *base;
438 struct clk *clk;
439 u32 num_lanes;
440 u32 mode;
441 int ret;
442 int irq;
443
444 data = of_device_get_match_data(dev);
445 if (!data)
446 return -EINVAL;
447
448 mode = (u32)data->mode;
449
450 pcie = devm_kzalloc(dev, sizeof(*pcie), GFP_KERNEL);
451 if (!pcie)
452 return -ENOMEM;
453
454 switch (mode) {
455 case PCI_MODE_RC:
456 if (!IS_ENABLED(CONFIG_PCIE_CADENCE_HOST))
457 return -ENODEV;
458
459 bridge = devm_pci_alloc_host_bridge(dev, sizeof(*rc));
460 if (!bridge)
461 return -ENOMEM;
462
463 if (!data->byte_access_allowed)
464 bridge->ops = &cdns_ti_pcie_host_ops;
465 rc = pci_host_bridge_priv(bridge);
466 rc->quirk_retrain_flag = data->quirk_retrain_flag;
467 rc->quirk_detect_quiet_flag = data->quirk_detect_quiet_flag;
468
469 cdns_pcie = &rc->pcie;
470 cdns_pcie->dev = dev;
471 cdns_pcie->ops = &j721e_pcie_ops;
472 pcie->cdns_pcie = cdns_pcie;
473 break;
474 case PCI_MODE_EP:
475 if (!IS_ENABLED(CONFIG_PCIE_CADENCE_EP))
476 return -ENODEV;
477
478 ep = devm_kzalloc(dev, sizeof(*ep), GFP_KERNEL);
479 if (!ep)
480 return -ENOMEM;
481
482 ep->quirk_detect_quiet_flag = data->quirk_detect_quiet_flag;
483 ep->quirk_disable_flr = data->quirk_disable_flr;
484
485 cdns_pcie = &ep->pcie;
486 cdns_pcie->dev = dev;
487 cdns_pcie->ops = &j721e_pcie_ops;
488 pcie->cdns_pcie = cdns_pcie;
489 break;
490 default:
491 dev_err(dev, "INVALID device type %d\n", mode);
492 return 0;
493 }
494
495 pcie->mode = mode;
496 pcie->linkdown_irq_regfield = data->linkdown_irq_regfield;
497
498 base = devm_platform_ioremap_resource_byname(pdev, "intd_cfg");
499 if (IS_ERR(base))
500 return PTR_ERR(base);
501 pcie->intd_cfg_base = base;
502
503 base = devm_platform_ioremap_resource_byname(pdev, "user_cfg");
504 if (IS_ERR(base))
505 return PTR_ERR(base);
506 pcie->user_cfg_base = base;
507
508 ret = of_property_read_u32(node, "num-lanes", &num_lanes);
509 if (ret || num_lanes > data->max_lanes) {
510 dev_warn(dev, "num-lanes property not provided or invalid, setting num-lanes to 1\n");
511 num_lanes = 1;
512 }
513
514 pcie->num_lanes = num_lanes;
515 pcie->max_lanes = data->max_lanes;
516
517 if (dma_set_mask_and_coherent(dev, DMA_BIT_MASK(48)))
518 return -EINVAL;
519
520 irq = platform_get_irq_byname(pdev, "link_state");
521 if (irq < 0)
522 return irq;
523
524 dev_set_drvdata(dev, pcie);
525 pm_runtime_enable(dev);
526 ret = pm_runtime_get_sync(dev);
527 if (ret < 0) {
528 dev_err_probe(dev, ret, "pm_runtime_get_sync failed\n");
529 goto err_get_sync;
530 }
531
532 ret = j721e_pcie_ctrl_init(pcie);
533 if (ret < 0) {
534 dev_err_probe(dev, ret, "pm_runtime_get_sync failed\n");
535 goto err_get_sync;
536 }
537
538 ret = devm_request_irq(dev, irq, j721e_pcie_link_irq_handler, 0,
539 "j721e-pcie-link-down-irq", pcie);
540 if (ret < 0) {
541 dev_err_probe(dev, ret, "failed to request link state IRQ %d\n", irq);
542 goto err_get_sync;
543 }
544
545 j721e_pcie_config_link_irq(pcie);
546
547 switch (mode) {
548 case PCI_MODE_RC:
549 gpiod = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW);
550 if (IS_ERR(gpiod)) {
551 ret = dev_err_probe(dev, PTR_ERR(gpiod), "Failed to get reset GPIO\n");
552 goto err_get_sync;
553 }
554 pcie->reset_gpio = gpiod;
555
556 ret = cdns_pcie_init_phy(dev, cdns_pcie);
557 if (ret) {
558 dev_err_probe(dev, ret, "Failed to init phy\n");
559 goto err_get_sync;
560 }
561
562 clk = devm_clk_get_optional(dev, "pcie_refclk");
563 if (IS_ERR(clk)) {
564 ret = dev_err_probe(dev, PTR_ERR(clk), "failed to get pcie_refclk\n");
565 goto err_pcie_setup;
566 }
567
568 ret = clk_prepare_enable(clk);
569 if (ret) {
570 dev_err_probe(dev, ret, "failed to enable pcie_refclk\n");
571 goto err_pcie_setup;
572 }
573 pcie->refclk = clk;
574
575 /*
576 * Section 2.2 of the PCI Express Card Electromechanical
577 * Specification (Revision 5.1) mandates that the deassertion
578 * of the PERST# signal should be delayed by 100 ms (TPVPERL).
579 * This shall ensure that the power and the reference clock
580 * are stable.
581 */
582 if (gpiod) {
583 msleep(PCIE_T_PVPERL_MS);
584 gpiod_set_value_cansleep(gpiod, 1);
585 }
586
587 ret = cdns_pcie_host_setup(rc);
588 if (ret < 0) {
589 clk_disable_unprepare(pcie->refclk);
590 goto err_pcie_setup;
591 }
592
593 break;
594 case PCI_MODE_EP:
595 ret = cdns_pcie_init_phy(dev, cdns_pcie);
596 if (ret) {
597 dev_err_probe(dev, ret, "Failed to init phy\n");
598 goto err_get_sync;
599 }
600
601 ret = cdns_pcie_ep_setup(ep);
602 if (ret < 0)
603 goto err_pcie_setup;
604
605 break;
606 }
607
608 return 0;
609
610 err_pcie_setup:
611 cdns_pcie_disable_phy(cdns_pcie);
612
613 err_get_sync:
614 pm_runtime_put(dev);
615 pm_runtime_disable(dev);
616
617 return ret;
618 }
619
j721e_pcie_remove(struct platform_device * pdev)620 static void j721e_pcie_remove(struct platform_device *pdev)
621 {
622 struct j721e_pcie *pcie = platform_get_drvdata(pdev);
623 struct cdns_pcie *cdns_pcie = pcie->cdns_pcie;
624 struct device *dev = &pdev->dev;
625
626 clk_disable_unprepare(pcie->refclk);
627 cdns_pcie_disable_phy(cdns_pcie);
628 pm_runtime_put(dev);
629 pm_runtime_disable(dev);
630 }
631
j721e_pcie_suspend_noirq(struct device * dev)632 static int j721e_pcie_suspend_noirq(struct device *dev)
633 {
634 struct j721e_pcie *pcie = dev_get_drvdata(dev);
635
636 if (pcie->mode == PCI_MODE_RC) {
637 gpiod_set_value_cansleep(pcie->reset_gpio, 0);
638 clk_disable_unprepare(pcie->refclk);
639 }
640
641 cdns_pcie_disable_phy(pcie->cdns_pcie);
642
643 return 0;
644 }
645
j721e_pcie_resume_noirq(struct device * dev)646 static int j721e_pcie_resume_noirq(struct device *dev)
647 {
648 struct j721e_pcie *pcie = dev_get_drvdata(dev);
649 struct cdns_pcie *cdns_pcie = pcie->cdns_pcie;
650 int ret;
651
652 ret = j721e_pcie_ctrl_init(pcie);
653 if (ret < 0)
654 return ret;
655
656 j721e_pcie_config_link_irq(pcie);
657
658 /*
659 * This is not called explicitly in the probe, it is called by
660 * cdns_pcie_init_phy().
661 */
662 ret = cdns_pcie_enable_phy(pcie->cdns_pcie);
663 if (ret < 0)
664 return ret;
665
666 if (pcie->mode == PCI_MODE_RC) {
667 struct cdns_pcie_rc *rc = cdns_pcie_to_rc(cdns_pcie);
668
669 ret = clk_prepare_enable(pcie->refclk);
670 if (ret < 0)
671 return ret;
672
673 /*
674 * Section 2.2 of the PCI Express Card Electromechanical
675 * Specification (Revision 5.1) mandates that the deassertion
676 * of the PERST# signal should be delayed by 100 ms (TPVPERL).
677 * This shall ensure that the power and the reference clock
678 * are stable.
679 */
680 if (pcie->reset_gpio) {
681 msleep(PCIE_T_PVPERL_MS);
682 gpiod_set_value_cansleep(pcie->reset_gpio, 1);
683 }
684
685 ret = cdns_pcie_host_link_setup(rc);
686 if (ret < 0) {
687 clk_disable_unprepare(pcie->refclk);
688 return ret;
689 }
690
691 /*
692 * Reset internal status of BARs to force reinitialization in
693 * cdns_pcie_host_init().
694 */
695 for (enum cdns_pcie_rp_bar bar = RP_BAR0; bar <= RP_NO_BAR; bar++)
696 rc->avail_ib_bar[bar] = true;
697
698 ret = cdns_pcie_host_init(rc);
699 if (ret) {
700 clk_disable_unprepare(pcie->refclk);
701 return ret;
702 }
703 }
704
705 return 0;
706 }
707
708 static DEFINE_NOIRQ_DEV_PM_OPS(j721e_pcie_pm_ops,
709 j721e_pcie_suspend_noirq,
710 j721e_pcie_resume_noirq);
711
712 static struct platform_driver j721e_pcie_driver = {
713 .probe = j721e_pcie_probe,
714 .remove = j721e_pcie_remove,
715 .driver = {
716 .name = "j721e-pcie",
717 .of_match_table = of_j721e_pcie_match,
718 .suppress_bind_attrs = true,
719 .pm = pm_sleep_ptr(&j721e_pcie_pm_ops),
720 },
721 };
722 builtin_platform_driver(j721e_pcie_driver);
723