Skip to content

Commit bd6e61d

Browse files
damien-lemoalbjorn-helgaas
authored andcommitted
PCI: rockchip-ep: Improve link training
The Rockchip RK3399 TRM V1.3 Part2, Section 17.5.8.1.2, step 7, describes the endpoint mode link training process clearly and states that: Insure link training completion and success by observing link_st field in PCIe Client BASIC_STATUS1 register change to 2'b11. If both side support PCIe Gen2 speed, re-train can be Initiated by asserting the Retrain Link field in Link Control and Status Register. The software should insure the BASIC_STATUS0[negotiated_speed] changes to "1", that indicates re-train to Gen2 successfully. This procedure is very similar to what is done for the root-port mode in rockchip_pcie_host_init_port(). Implement this link training procedure for the endpoint mode as well. Given that the RK3399 SoC does not have an interrupt signaling link status changes, training is implemented as a delayed work which is rescheduled until the link training completes or the endpoint controller is stopped. The link training work is first scheduled in rockchip_pcie_ep_start() when the endpoint function is started. Link training completion is signaled to the function using pci_epc_linkup(). Accordingly, the linkup_notifier field of the Rockchip pci_epc_features structure is changed to true. Link: https://lore.kernel.org/r/20241017015849.190271-13-dlemoal@kernel.org Signed-off-by: Damien Le Moal <dlemoal@kernel.org> [kwilczynski: update log messages to make them consistent] Signed-off-by: Krzysztof Wilczyński <kwilczynski@kernel.org> Signed-off-by: Bjorn Helgaas <bhelgaas@google.com>
1 parent 00080d0 commit bd6e61d

File tree

2 files changed

+92
-1
lines changed

2 files changed

+92
-1
lines changed

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

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@
1010

1111
#include <linux/configfs.h>
1212
#include <linux/delay.h>
13+
#include <linux/iopoll.h>
1314
#include <linux/kernel.h>
1415
#include <linux/of.h>
1516
#include <linux/pci-epc.h>
1617
#include <linux/platform_device.h>
1718
#include <linux/pci-epf.h>
1819
#include <linux/sizes.h>
20+
#include <linux/workqueue.h>
1921

2022
#include "pcie-rockchip.h"
2123

@@ -48,6 +50,7 @@ struct rockchip_pcie_ep {
4850
u64 irq_pci_addr;
4951
u8 irq_pci_fn;
5052
u8 irq_pending;
53+
struct delayed_work link_training;
5154
};
5255

5356
static void rockchip_pcie_clear_ep_ob_atu(struct rockchip_pcie *rockchip,
@@ -473,6 +476,8 @@ static int rockchip_pcie_ep_start(struct pci_epc *epc)
473476
PCIE_CLIENT_CONF_ENABLE,
474477
PCIE_CLIENT_CONFIG);
475478

479+
schedule_delayed_work(&ep->link_training, 0);
480+
476481
return 0;
477482
}
478483

@@ -481,15 +486,89 @@ static void rockchip_pcie_ep_stop(struct pci_epc *epc)
481486
struct rockchip_pcie_ep *ep = epc_get_drvdata(epc);
482487
struct rockchip_pcie *rockchip = &ep->rockchip;
483488

489+
cancel_delayed_work_sync(&ep->link_training);
490+
484491
/* Stop link training and disable configuration */
485492
rockchip_pcie_write(rockchip,
486493
PCIE_CLIENT_CONF_DISABLE |
487494
PCIE_CLIENT_LINK_TRAIN_DISABLE,
488495
PCIE_CLIENT_CONFIG);
489496
}
490497

498+
static void rockchip_pcie_ep_retrain_link(struct rockchip_pcie *rockchip)
499+
{
500+
u32 status;
501+
502+
status = rockchip_pcie_read(rockchip, PCIE_EP_CONFIG_LCS);
503+
status |= PCI_EXP_LNKCTL_RL;
504+
rockchip_pcie_write(rockchip, status, PCIE_EP_CONFIG_LCS);
505+
}
506+
507+
static bool rockchip_pcie_ep_link_up(struct rockchip_pcie *rockchip)
508+
{
509+
u32 val = rockchip_pcie_read(rockchip, PCIE_CLIENT_BASIC_STATUS1);
510+
511+
return PCIE_LINK_UP(val);
512+
}
513+
514+
static void rockchip_pcie_ep_link_training(struct work_struct *work)
515+
{
516+
struct rockchip_pcie_ep *ep =
517+
container_of(work, struct rockchip_pcie_ep, link_training.work);
518+
struct rockchip_pcie *rockchip = &ep->rockchip;
519+
struct device *dev = rockchip->dev;
520+
u32 val;
521+
int ret;
522+
523+
/* Enable Gen1 training and wait for its completion */
524+
ret = readl_poll_timeout(rockchip->apb_base + PCIE_CORE_CTRL,
525+
val, PCIE_LINK_TRAINING_DONE(val), 50,
526+
LINK_TRAIN_TIMEOUT);
527+
if (ret)
528+
goto again;
529+
530+
/* Make sure that the link is up */
531+
ret = readl_poll_timeout(rockchip->apb_base + PCIE_CLIENT_BASIC_STATUS1,
532+
val, PCIE_LINK_UP(val), 50,
533+
LINK_TRAIN_TIMEOUT);
534+
if (ret)
535+
goto again;
536+
537+
/*
538+
* Check the current speed: if gen2 speed was requested and we are not
539+
* at gen2 speed yet, retrain again for gen2.
540+
*/
541+
val = rockchip_pcie_read(rockchip, PCIE_CORE_CTRL);
542+
if (!PCIE_LINK_IS_GEN2(val) && rockchip->link_gen == 2) {
543+
/* Enable retrain for gen2 */
544+
rockchip_pcie_ep_retrain_link(rockchip);
545+
readl_poll_timeout(rockchip->apb_base + PCIE_CORE_CTRL,
546+
val, PCIE_LINK_IS_GEN2(val), 50,
547+
LINK_TRAIN_TIMEOUT);
548+
}
549+
550+
/* Check again that the link is up */
551+
if (!rockchip_pcie_ep_link_up(rockchip))
552+
goto again;
553+
554+
val = rockchip_pcie_read(rockchip, PCIE_CLIENT_BASIC_STATUS0);
555+
dev_info(dev,
556+
"link up (negotiated speed: %sGT/s, width: x%lu)\n",
557+
(val & PCIE_CLIENT_NEG_LINK_SPEED) ? "5" : "2.5",
558+
((val & PCIE_CLIENT_NEG_LINK_WIDTH_MASK) >>
559+
PCIE_CLIENT_NEG_LINK_WIDTH_SHIFT) << 1);
560+
561+
/* Notify the function */
562+
pci_epc_linkup(ep->epc);
563+
564+
return;
565+
566+
again:
567+
schedule_delayed_work(&ep->link_training, msecs_to_jiffies(5));
568+
}
569+
491570
static const struct pci_epc_features rockchip_pcie_epc_features = {
492-
.linkup_notifier = false,
571+
.linkup_notifier = true,
493572
.msi_capable = true,
494573
.msix_capable = false,
495574
.align = ROCKCHIP_PCIE_AT_SIZE_ALIGN,
@@ -647,6 +726,7 @@ static int rockchip_pcie_ep_probe(struct platform_device *pdev)
647726
rockchip = &ep->rockchip;
648727
rockchip->is_rc = false;
649728
rockchip->dev = dev;
729+
INIT_DELAYED_WORK(&ep->link_training, rockchip_pcie_ep_link_training);
650730

651731
epc = devm_pci_epc_create(dev, &rockchip_pcie_epc_ops);
652732
if (IS_ERR(epc)) {

drivers/pci/controller/pcie-rockchip.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#define MAX_LANE_NUM 4
2727
#define MAX_REGION_LIMIT 32
2828
#define MIN_EP_APERTURE 28
29+
#define LINK_TRAIN_TIMEOUT (500 * USEC_PER_MSEC)
2930

3031
#define PCIE_CLIENT_BASE 0x0
3132
#define PCIE_CLIENT_CONFIG (PCIE_CLIENT_BASE + 0x00)
@@ -50,6 +51,10 @@
5051
#define PCIE_CLIENT_DEBUG_LTSSM_MASK GENMASK(5, 0)
5152
#define PCIE_CLIENT_DEBUG_LTSSM_L1 0x18
5253
#define PCIE_CLIENT_DEBUG_LTSSM_L2 0x19
54+
#define PCIE_CLIENT_BASIC_STATUS0 (PCIE_CLIENT_BASE + 0x44)
55+
#define PCIE_CLIENT_NEG_LINK_WIDTH_MASK GENMASK(7, 6)
56+
#define PCIE_CLIENT_NEG_LINK_WIDTH_SHIFT 6
57+
#define PCIE_CLIENT_NEG_LINK_SPEED BIT(5)
5358
#define PCIE_CLIENT_BASIC_STATUS1 (PCIE_CLIENT_BASE + 0x48)
5459
#define PCIE_CLIENT_LINK_STATUS_UP 0x00300000
5560
#define PCIE_CLIENT_LINK_STATUS_MASK 0x00300000
@@ -87,6 +92,8 @@
8792

8893
#define PCIE_CORE_CTRL_MGMT_BASE 0x900000
8994
#define PCIE_CORE_CTRL (PCIE_CORE_CTRL_MGMT_BASE + 0x000)
95+
#define PCIE_CORE_PL_CONF_LS_MASK 0x00000001
96+
#define PCIE_CORE_PL_CONF_LS_READY 0x00000001
9097
#define PCIE_CORE_PL_CONF_SPEED_5G 0x00000008
9198
#define PCIE_CORE_PL_CONF_SPEED_MASK 0x00000018
9299
#define PCIE_CORE_PL_CONF_LANE_MASK 0x00000006
@@ -144,6 +151,7 @@
144151
#define PCIE_RC_CONFIG_BASE 0xa00000
145152
#define PCIE_EP_CONFIG_BASE 0xa00000
146153
#define PCIE_EP_CONFIG_DID_VID (PCIE_EP_CONFIG_BASE + 0x00)
154+
#define PCIE_EP_CONFIG_LCS (PCIE_EP_CONFIG_BASE + 0xd0)
147155
#define PCIE_RC_CONFIG_RID_CCR (PCIE_RC_CONFIG_BASE + 0x08)
148156
#define PCIE_RC_CONFIG_DCR (PCIE_RC_CONFIG_BASE + 0xc4)
149157
#define PCIE_RC_CONFIG_DCR_CSPL_SHIFT 18
@@ -155,6 +163,7 @@
155163
#define PCIE_RC_CONFIG_LINK_CAP (PCIE_RC_CONFIG_BASE + 0xcc)
156164
#define PCIE_RC_CONFIG_LINK_CAP_L0S BIT(10)
157165
#define PCIE_RC_CONFIG_LCS (PCIE_RC_CONFIG_BASE + 0xd0)
166+
#define PCIE_EP_CONFIG_LCS (PCIE_EP_CONFIG_BASE + 0xd0)
158167
#define PCIE_RC_CONFIG_L1_SUBSTATE_CTRL2 (PCIE_RC_CONFIG_BASE + 0x90c)
159168
#define PCIE_RC_CONFIG_THP_CAP (PCIE_RC_CONFIG_BASE + 0x274)
160169
#define PCIE_RC_CONFIG_THP_CAP_NEXT_MASK GENMASK(31, 20)
@@ -192,6 +201,8 @@
192201
#define ROCKCHIP_VENDOR_ID 0x1d87
193202
#define PCIE_LINK_IS_L2(x) \
194203
(((x) & PCIE_CLIENT_DEBUG_LTSSM_MASK) == PCIE_CLIENT_DEBUG_LTSSM_L2)
204+
#define PCIE_LINK_TRAINING_DONE(x) \
205+
(((x) & PCIE_CORE_PL_CONF_LS_MASK) == PCIE_CORE_PL_CONF_LS_READY)
195206
#define PCIE_LINK_UP(x) \
196207
(((x) & PCIE_CLIENT_LINK_STATUS_MASK) == PCIE_CLIENT_LINK_STATUS_UP)
197208
#define PCIE_LINK_IS_GEN2(x) \

0 commit comments

Comments
 (0)