Skip to content

Commit a7137cb

Browse files
damien-lemoalbjorn-helgaas
authored andcommitted
PCI: rockchip-ep: Handle PERST# signal in EP mode
Currently, the Rockchip PCIe endpoint controller driver does not handle the PERST# signal, which prevents detecting when link training should actually be started or if the host resets the device. This however can be supported using the controller reset_gpios property set as an input GPIO for endpoint mode. Modify the Rockchip PCI endpoint controller driver to get the reset_gpio and its associated interrupt which is serviced using a threaded IRQ with the function rockchip_pcie_ep_perst_irq_thread() as handler. This handler function notifies a link down event corresponding to the RC side asserting the PERST# signal using pci_epc_linkdown() when the gpio is high. Once the gpio value goes down, corresponding to the RC de-asserting the PERST# signal, link training is started. The polarity of the gpio interrupt trigger is changed from high to low after the RC asserted PERST#, and conversely changed from low to high after the RC de-asserts PERST#. Also, given that the host mode controller and the endpoint mode controller use two different property names for the same PERST# signal (ep_gpios property and reset_gpios property respectively), for clarity, rename the ep_gpio field of struct rockchip_pcie to perst_gpio. Link: https://lore.kernel.org/r/20241017015849.190271-14-dlemoal@kernel.org Signed-off-by: Damien Le Moal <dlemoal@kernel.org> [kwilczynski: make log messages consistent, add missing include] Signed-off-by: Krzysztof Wilczyński <kwilczynski@kernel.org> Signed-off-by: Bjorn Helgaas <bhelgaas@google.com>
1 parent bd6e61d commit a7137cb

File tree

4 files changed

+141
-14
lines changed

4 files changed

+141
-14
lines changed

drivers/pci/controller/pcie-rockchip-ep.c

Lines changed: 129 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@
1010

1111
#include <linux/configfs.h>
1212
#include <linux/delay.h>
13+
#include <linux/gpio/consumer.h>
1314
#include <linux/iopoll.h>
1415
#include <linux/kernel.h>
16+
#include <linux/irq.h>
1517
#include <linux/of.h>
1618
#include <linux/pci-epc.h>
1719
#include <linux/platform_device.h>
@@ -50,6 +52,9 @@ struct rockchip_pcie_ep {
5052
u64 irq_pci_addr;
5153
u8 irq_pci_fn;
5254
u8 irq_pending;
55+
int perst_irq;
56+
bool perst_asserted;
57+
bool link_up;
5358
struct delayed_work link_training;
5459
};
5560

@@ -470,13 +475,17 @@ static int rockchip_pcie_ep_start(struct pci_epc *epc)
470475

471476
rockchip_pcie_write(rockchip, cfg, PCIE_CORE_PHY_FUNC_CFG);
472477

478+
if (rockchip->perst_gpio)
479+
enable_irq(ep->perst_irq);
480+
473481
/* Enable configuration and start link training */
474482
rockchip_pcie_write(rockchip,
475483
PCIE_CLIENT_LINK_TRAIN_ENABLE |
476484
PCIE_CLIENT_CONF_ENABLE,
477485
PCIE_CLIENT_CONFIG);
478486

479-
schedule_delayed_work(&ep->link_training, 0);
487+
if (!rockchip->perst_gpio)
488+
schedule_delayed_work(&ep->link_training, 0);
480489

481490
return 0;
482491
}
@@ -486,6 +495,11 @@ static void rockchip_pcie_ep_stop(struct pci_epc *epc)
486495
struct rockchip_pcie_ep *ep = epc_get_drvdata(epc);
487496
struct rockchip_pcie *rockchip = &ep->rockchip;
488497

498+
if (rockchip->perst_gpio) {
499+
ep->perst_asserted = true;
500+
disable_irq(ep->perst_irq);
501+
}
502+
489503
cancel_delayed_work_sync(&ep->link_training);
490504

491505
/* Stop link training and disable configuration */
@@ -551,6 +565,13 @@ static void rockchip_pcie_ep_link_training(struct work_struct *work)
551565
if (!rockchip_pcie_ep_link_up(rockchip))
552566
goto again;
553567

568+
/*
569+
* If PERST# was asserted while polling the link, do not notify
570+
* the function.
571+
*/
572+
if (ep->perst_asserted)
573+
return;
574+
554575
val = rockchip_pcie_read(rockchip, PCIE_CLIENT_BASIC_STATUS0);
555576
dev_info(dev,
556577
"link up (negotiated speed: %sGT/s, width: x%lu)\n",
@@ -560,13 +581,111 @@ static void rockchip_pcie_ep_link_training(struct work_struct *work)
560581

561582
/* Notify the function */
562583
pci_epc_linkup(ep->epc);
584+
ep->link_up = true;
563585

564586
return;
565587

566588
again:
567589
schedule_delayed_work(&ep->link_training, msecs_to_jiffies(5));
568590
}
569591

592+
static void rockchip_pcie_ep_perst_assert(struct rockchip_pcie_ep *ep)
593+
{
594+
struct rockchip_pcie *rockchip = &ep->rockchip;
595+
596+
dev_dbg(rockchip->dev, "PERST# asserted, link down\n");
597+
598+
if (ep->perst_asserted)
599+
return;
600+
601+
ep->perst_asserted = true;
602+
603+
cancel_delayed_work_sync(&ep->link_training);
604+
605+
if (ep->link_up) {
606+
pci_epc_linkdown(ep->epc);
607+
ep->link_up = false;
608+
}
609+
}
610+
611+
static void rockchip_pcie_ep_perst_deassert(struct rockchip_pcie_ep *ep)
612+
{
613+
struct rockchip_pcie *rockchip = &ep->rockchip;
614+
615+
dev_dbg(rockchip->dev, "PERST# de-asserted, starting link training\n");
616+
617+
if (!ep->perst_asserted)
618+
return;
619+
620+
ep->perst_asserted = false;
621+
622+
/* Enable link re-training */
623+
rockchip_pcie_ep_retrain_link(rockchip);
624+
625+
/* Start link training */
626+
schedule_delayed_work(&ep->link_training, 0);
627+
}
628+
629+
static irqreturn_t rockchip_pcie_ep_perst_irq_thread(int irq, void *data)
630+
{
631+
struct pci_epc *epc = data;
632+
struct rockchip_pcie_ep *ep = epc_get_drvdata(epc);
633+
struct rockchip_pcie *rockchip = &ep->rockchip;
634+
u32 perst = gpiod_get_value(rockchip->perst_gpio);
635+
636+
if (perst)
637+
rockchip_pcie_ep_perst_assert(ep);
638+
else
639+
rockchip_pcie_ep_perst_deassert(ep);
640+
641+
irq_set_irq_type(ep->perst_irq,
642+
(perst ? IRQF_TRIGGER_HIGH : IRQF_TRIGGER_LOW));
643+
644+
return IRQ_HANDLED;
645+
}
646+
647+
static int rockchip_pcie_ep_setup_irq(struct pci_epc *epc)
648+
{
649+
struct rockchip_pcie_ep *ep = epc_get_drvdata(epc);
650+
struct rockchip_pcie *rockchip = &ep->rockchip;
651+
struct device *dev = rockchip->dev;
652+
int ret;
653+
654+
if (!rockchip->perst_gpio)
655+
return 0;
656+
657+
/* PCIe reset interrupt */
658+
ep->perst_irq = gpiod_to_irq(rockchip->perst_gpio);
659+
if (ep->perst_irq < 0) {
660+
dev_err(dev,
661+
"failed to get IRQ for PERST# GPIO: %d\n",
662+
ep->perst_irq);
663+
664+
return ep->perst_irq;
665+
}
666+
667+
/*
668+
* The perst_gpio is active low, so when it is inactive on start, it
669+
* is high and will trigger the perst_irq handler. So treat this initial
670+
* IRQ as a dummy one by faking the host asserting PERST#.
671+
*/
672+
ep->perst_asserted = true;
673+
irq_set_status_flags(ep->perst_irq, IRQ_NOAUTOEN);
674+
ret = devm_request_threaded_irq(dev, ep->perst_irq, NULL,
675+
rockchip_pcie_ep_perst_irq_thread,
676+
IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
677+
"pcie-ep-perst", epc);
678+
if (ret) {
679+
dev_err(dev,
680+
"failed to request IRQ for PERST# GPIO: %d\n",
681+
ret);
682+
683+
return ret;
684+
}
685+
686+
return 0;
687+
}
688+
570689
static const struct pci_epc_features rockchip_pcie_epc_features = {
571690
.linkup_notifier = true,
572691
.msi_capable = true,
@@ -730,7 +849,7 @@ static int rockchip_pcie_ep_probe(struct platform_device *pdev)
730849

731850
epc = devm_pci_epc_create(dev, &rockchip_pcie_epc_ops);
732851
if (IS_ERR(epc)) {
733-
dev_err(dev, "failed to create epc device\n");
852+
dev_err(dev, "failed to create EPC device\n");
734853
return PTR_ERR(epc);
735854
}
736855

@@ -760,11 +879,17 @@ static int rockchip_pcie_ep_probe(struct platform_device *pdev)
760879

761880
pci_epc_init_notify(epc);
762881

882+
err = rockchip_pcie_ep_setup_irq(epc);
883+
if (err < 0)
884+
goto err_uninit_port;
885+
763886
return 0;
764-
err_exit_ob_mem:
765-
rockchip_pcie_ep_exit_ob_mem(ep);
887+
err_uninit_port:
888+
rockchip_pcie_deinit_phys(rockchip);
766889
err_disable_clocks:
767890
rockchip_pcie_disable_clocks(rockchip);
891+
err_exit_ob_mem:
892+
rockchip_pcie_ep_exit_ob_mem(ep);
768893
return err;
769894
}
770895

drivers/pci/controller/pcie-rockchip-host.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ static int rockchip_pcie_host_init_port(struct rockchip_pcie *rockchip)
294294
int err, i = MAX_LANE_NUM;
295295
u32 status;
296296

297-
gpiod_set_value_cansleep(rockchip->ep_gpio, 0);
297+
gpiod_set_value_cansleep(rockchip->perst_gpio, 0);
298298

299299
err = rockchip_pcie_init_port(rockchip);
300300
if (err)
@@ -323,7 +323,7 @@ static int rockchip_pcie_host_init_port(struct rockchip_pcie *rockchip)
323323
PCIE_CLIENT_CONFIG);
324324

325325
msleep(PCIE_T_PVPERL_MS);
326-
gpiod_set_value_cansleep(rockchip->ep_gpio, 1);
326+
gpiod_set_value_cansleep(rockchip->perst_gpio, 1);
327327

328328
msleep(PCIE_T_RRS_READY_MS);
329329

drivers/pci/controller/pcie-rockchip.c

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -119,13 +119,15 @@ int rockchip_pcie_parse_dt(struct rockchip_pcie *rockchip)
119119
return PTR_ERR(rockchip->aclk_rst);
120120
}
121121

122-
if (rockchip->is_rc) {
123-
rockchip->ep_gpio = devm_gpiod_get_optional(dev, "ep",
124-
GPIOD_OUT_LOW);
125-
if (IS_ERR(rockchip->ep_gpio))
126-
return dev_err_probe(dev, PTR_ERR(rockchip->ep_gpio),
127-
"failed to get ep GPIO\n");
128-
}
122+
if (rockchip->is_rc)
123+
rockchip->perst_gpio = devm_gpiod_get_optional(dev, "ep",
124+
GPIOD_OUT_LOW);
125+
else
126+
rockchip->perst_gpio = devm_gpiod_get_optional(dev, "reset",
127+
GPIOD_IN);
128+
if (IS_ERR(rockchip->perst_gpio))
129+
return dev_err_probe(dev, PTR_ERR(rockchip->perst_gpio),
130+
"failed to get PERST# GPIO\n");
129131

130132
rockchip->aclk_pcie = devm_clk_get(dev, "aclk");
131133
if (IS_ERR(rockchip->aclk_pcie)) {

drivers/pci/controller/pcie-rockchip.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ struct rockchip_pcie {
329329
struct regulator *vpcie3v3; /* 3.3V power supply */
330330
struct regulator *vpcie1v8; /* 1.8V power supply */
331331
struct regulator *vpcie0v9; /* 0.9V power supply */
332-
struct gpio_desc *ep_gpio;
332+
struct gpio_desc *perst_gpio;
333333
u32 lanes;
334334
u8 lanes_map;
335335
int link_gen;

0 commit comments

Comments
 (0)