28
28
29
29
#include "../pci.h"
30
30
31
+ #define PCIE_BASE_CFG_REG 0x14
32
+ #define PCIE_BASE_CFG_SPEED GENMASK(15, 8)
33
+
31
34
#define PCIE_SETTING_REG 0x80
35
+ #define PCIE_SETTING_LINK_WIDTH GENMASK(11, 8)
36
+ #define PCIE_SETTING_GEN_SUPPORT GENMASK(14, 12)
32
37
#define PCIE_PCI_IDS_1 0x9c
33
38
#define PCI_CLASS (class ) (class << 8)
34
39
#define PCIE_RC_MODE BIT(0)
125
130
126
131
struct mtk_gen3_pcie ;
127
132
133
+ #define PCIE_CONF_LINK2_CTL_STS (PCIE_CFG_OFFSET_ADDR + 0xb0)
134
+ #define PCIE_CONF_LINK2_LCR2_LINK_SPEED GENMASK(3, 0)
135
+
128
136
/**
129
137
* struct mtk_gen3_pcie_pdata - differentiate between host generations
130
138
* @power_up: pcie power_up callback
@@ -160,6 +168,8 @@ struct mtk_msi_set {
160
168
* @phy: PHY controller block
161
169
* @clks: PCIe clocks
162
170
* @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
163
173
* @irq: PCIe controller interrupt number
164
174
* @saved_irq_state: IRQ enable state saved at suspend time
165
175
* @irq_lock: lock protecting IRQ register access
@@ -180,6 +190,8 @@ struct mtk_gen3_pcie {
180
190
struct phy * phy ;
181
191
struct clk_bulk_data * clks ;
182
192
int num_clks ;
193
+ u8 max_link_speed ;
194
+ u8 num_lanes ;
183
195
184
196
int irq ;
185
197
u32 saved_irq_state ;
@@ -381,11 +393,35 @@ static int mtk_pcie_startup_port(struct mtk_gen3_pcie *pcie)
381
393
int err ;
382
394
u32 val ;
383
395
384
- /* Set as RC mode */
396
+ /* Set as RC mode and set controller PCIe Gen speed restriction, if any */
385
397
val = readl_relaxed (pcie -> base + PCIE_SETTING_REG );
386
398
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
+ }
387
415
writel_relaxed (val , pcie -> base + PCIE_SETTING_REG );
388
416
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
+
389
425
/* Set class code */
390
426
val = readl_relaxed (pcie -> base + PCIE_PCI_IDS_1 );
391
427
val &= ~GENMASK (31 , 8 );
@@ -813,6 +849,7 @@ static int mtk_pcie_parse_port(struct mtk_gen3_pcie *pcie)
813
849
struct device * dev = pcie -> dev ;
814
850
struct platform_device * pdev = to_platform_device (dev );
815
851
struct resource * regs ;
852
+ u32 num_lanes ;
816
853
817
854
regs = platform_get_resource_byname (pdev , IORESOURCE_MEM , "pcie-mac" );
818
855
if (!regs )
@@ -858,6 +895,14 @@ static int mtk_pcie_parse_port(struct mtk_gen3_pcie *pcie)
858
895
return pcie -> num_clks ;
859
896
}
860
897
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
+
861
906
return 0 ;
862
907
}
863
908
@@ -1004,9 +1049,21 @@ static void mtk_pcie_power_down(struct mtk_gen3_pcie *pcie)
1004
1049
reset_control_bulk_assert (pcie -> soc -> phy_resets .num_resets , pcie -> phy_resets );
1005
1050
}
1006
1051
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
+
1007
1064
static int mtk_pcie_setup (struct mtk_gen3_pcie * pcie )
1008
1065
{
1009
- int err ;
1066
+ int err , max_speed ;
1010
1067
1011
1068
err = mtk_pcie_parse_port (pcie );
1012
1069
if (err )
@@ -1031,6 +1088,20 @@ static int mtk_pcie_setup(struct mtk_gen3_pcie *pcie)
1031
1088
if (err )
1032
1089
return err ;
1033
1090
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
+
1034
1105
/* Try link up */
1035
1106
err = mtk_pcie_startup_port (pcie );
1036
1107
if (err )
0 commit comments