2828
2929#include "../pci.h"
3030
31+ #define PCIE_BASE_CFG_REG 0x14
32+ #define PCIE_BASE_CFG_SPEED GENMASK(15, 8)
33+
3134#define PCIE_SETTING_REG 0x80
35+ #define PCIE_SETTING_LINK_WIDTH GENMASK(11, 8)
36+ #define PCIE_SETTING_GEN_SUPPORT GENMASK(14, 12)
3237#define PCIE_PCI_IDS_1 0x9c
3338#define PCI_CLASS (class ) (class << 8)
3439#define PCIE_RC_MODE BIT(0)
125130
126131struct mtk_gen3_pcie ;
127132
133+ #define PCIE_CONF_LINK2_CTL_STS (PCIE_CFG_OFFSET_ADDR + 0xb0)
134+ #define PCIE_CONF_LINK2_LCR2_LINK_SPEED GENMASK(3, 0)
135+
128136/**
129137 * struct mtk_gen3_pcie_pdata - differentiate between host generations
130138 * @power_up: pcie power_up callback
@@ -160,6 +168,8 @@ struct mtk_msi_set {
160168 * @phy: PHY controller block
161169 * @clks: PCIe clocks
162170 * @num_clks: PCIe clocks count for this port
171+ * @max_link_speed: Maximum link speed (PCIe Gen) for this port
172+ * @num_lanes: Number of PCIe lanes for this port
163173 * @irq: PCIe controller interrupt number
164174 * @saved_irq_state: IRQ enable state saved at suspend time
165175 * @irq_lock: lock protecting IRQ register access
@@ -180,6 +190,8 @@ struct mtk_gen3_pcie {
180190 struct phy * phy ;
181191 struct clk_bulk_data * clks ;
182192 int num_clks ;
193+ u8 max_link_speed ;
194+ u8 num_lanes ;
183195
184196 int irq ;
185197 u32 saved_irq_state ;
@@ -381,11 +393,35 @@ static int mtk_pcie_startup_port(struct mtk_gen3_pcie *pcie)
381393 int err ;
382394 u32 val ;
383395
384- /* Set as RC mode */
396+ /* Set as RC mode and set controller PCIe Gen speed restriction, if any */
385397 val = readl_relaxed (pcie -> base + PCIE_SETTING_REG );
386398 val |= PCIE_RC_MODE ;
399+ if (pcie -> max_link_speed ) {
400+ val &= ~PCIE_SETTING_GEN_SUPPORT ;
401+
402+ /* Can enable link speed support only from Gen2 onwards */
403+ if (pcie -> max_link_speed >= 2 )
404+ val |= FIELD_PREP (PCIE_SETTING_GEN_SUPPORT ,
405+ GENMASK (pcie -> max_link_speed - 2 , 0 ));
406+ }
407+ if (pcie -> num_lanes ) {
408+ val &= ~PCIE_SETTING_LINK_WIDTH ;
409+
410+ /* Zero means one lane, each bit activates x2/x4/x8/x16 */
411+ if (pcie -> num_lanes > 1 )
412+ val |= FIELD_PREP (PCIE_SETTING_LINK_WIDTH ,
413+ GENMASK (fls (pcie -> num_lanes >> 2 ), 0 ));
414+ }
387415 writel_relaxed (val , pcie -> base + PCIE_SETTING_REG );
388416
417+ /* Set Link Control 2 (LNKCTL2) speed restriction, if any */
418+ if (pcie -> max_link_speed ) {
419+ val = readl_relaxed (pcie -> base + PCIE_CONF_LINK2_CTL_STS );
420+ val &= ~PCIE_CONF_LINK2_LCR2_LINK_SPEED ;
421+ val |= FIELD_PREP (PCIE_CONF_LINK2_LCR2_LINK_SPEED , pcie -> max_link_speed );
422+ writel_relaxed (val , pcie -> base + PCIE_CONF_LINK2_CTL_STS );
423+ }
424+
389425 /* Set class code */
390426 val = readl_relaxed (pcie -> base + PCIE_PCI_IDS_1 );
391427 val &= ~GENMASK (31 , 8 );
@@ -813,6 +849,7 @@ static int mtk_pcie_parse_port(struct mtk_gen3_pcie *pcie)
813849 struct device * dev = pcie -> dev ;
814850 struct platform_device * pdev = to_platform_device (dev );
815851 struct resource * regs ;
852+ u32 num_lanes ;
816853
817854 regs = platform_get_resource_byname (pdev , IORESOURCE_MEM , "pcie-mac" );
818855 if (!regs )
@@ -858,6 +895,14 @@ static int mtk_pcie_parse_port(struct mtk_gen3_pcie *pcie)
858895 return pcie -> num_clks ;
859896 }
860897
898+ ret = of_property_read_u32 (dev -> of_node , "num-lanes" , & num_lanes );
899+ if (ret == 0 ) {
900+ if (num_lanes == 0 || num_lanes > 16 || (num_lanes != 1 && num_lanes % 2 ))
901+ dev_warn (dev , "invalid num-lanes, using controller defaults\n" );
902+ else
903+ pcie -> num_lanes = num_lanes ;
904+ }
905+
861906 return 0 ;
862907}
863908
@@ -1004,9 +1049,21 @@ static void mtk_pcie_power_down(struct mtk_gen3_pcie *pcie)
10041049 reset_control_bulk_assert (pcie -> soc -> phy_resets .num_resets , pcie -> phy_resets );
10051050}
10061051
1052+ static int mtk_pcie_get_controller_max_link_speed (struct mtk_gen3_pcie * pcie )
1053+ {
1054+ u32 val ;
1055+ int ret ;
1056+
1057+ val = readl_relaxed (pcie -> base + PCIE_BASE_CFG_REG );
1058+ val = FIELD_GET (PCIE_BASE_CFG_SPEED , val );
1059+ ret = fls (val );
1060+
1061+ return ret > 0 ? ret : - EINVAL ;
1062+ }
1063+
10071064static int mtk_pcie_setup (struct mtk_gen3_pcie * pcie )
10081065{
1009- int err ;
1066+ int err , max_speed ;
10101067
10111068 err = mtk_pcie_parse_port (pcie );
10121069 if (err )
@@ -1031,6 +1088,20 @@ static int mtk_pcie_setup(struct mtk_gen3_pcie *pcie)
10311088 if (err )
10321089 return err ;
10331090
1091+ err = of_pci_get_max_link_speed (pcie -> dev -> of_node );
1092+ if (err ) {
1093+ /* Get the maximum speed supported by the controller */
1094+ max_speed = mtk_pcie_get_controller_max_link_speed (pcie );
1095+
1096+ /* Set max_link_speed only if the controller supports it */
1097+ if (max_speed >= 0 && max_speed <= err ) {
1098+ pcie -> max_link_speed = err ;
1099+ dev_info (pcie -> dev ,
1100+ "maximum controller link speed Gen%d, overriding to Gen%u" ,
1101+ max_speed , pcie -> max_link_speed );
1102+ }
1103+ }
1104+
10341105 /* Try link up */
10351106 err = mtk_pcie_startup_port (pcie );
10361107 if (err )
0 commit comments