1 From 94f825a7eadfc8b4c8828efdb7705d9703f9c73e Mon Sep 17 00:00:00 2001
2 From: Lorenzo Bianconi <lorenzo@kernel.org>
3 Date: Tue, 25 Jul 2023 01:57:42 +0100
4 Subject: [PATCH 105/250] net: ethernet: mtk_eth_soc: add basic support for
7 Introduce support for ethernet chip available in MT7988 SoC to
8 mtk_eth_soc driver. As a first step support only the first GMAC which
9 is hard-wired to the internal DSA switch having 4 built-in gigabit
12 Signed-off-by: Lorenzo Bianconi <lorenzo@kernel.org>
13 Signed-off-by: Daniel Golle <daniel@makrotopia.org>
14 Link: https://lore.kernel.org/r/25c8377095b95d186872eeda7aa055da83e8f0ca.1690246605.git.daniel@makrotopia.org
15 Signed-off-by: Jakub Kicinski <kuba@kernel.org>
17 drivers/net/ethernet/mediatek/mtk_eth_path.c | 14 +-
18 drivers/net/ethernet/mediatek/mtk_eth_soc.c | 201 +++++++++++++++++--
19 drivers/net/ethernet/mediatek/mtk_eth_soc.h | 86 +++++++-
20 3 files changed, 273 insertions(+), 28 deletions(-)
22 --- a/drivers/net/ethernet/mediatek/mtk_eth_path.c
23 +++ b/drivers/net/ethernet/mediatek/mtk_eth_path.c
24 @@ -43,7 +43,7 @@ static const char *mtk_eth_path_name(u64
25 static int set_mux_gdm1_to_gmac1_esw(struct mtk_eth *eth, u64 path)
32 case MTK_ETH_PATH_GMAC1_SGMII:
33 @@ -59,11 +59,13 @@ static int set_mux_gdm1_to_gmac1_esw(str
38 - val = mtk_r32(eth, MTK_MAC_MISC);
39 - val = (val & mask) | set;
40 - mtk_w32(eth, val, MTK_MAC_MISC);
42 + if (mtk_is_netsys_v3_or_greater(eth))
43 + reg = MTK_MAC_MISC_V3;
48 + mtk_m32(eth, mask, set, reg);
50 dev_dbg(eth->dev, "path %s in %s updated = %d\n",
51 mtk_eth_path_name(path), __func__, updated);
52 --- a/drivers/net/ethernet/mediatek/mtk_eth_soc.c
53 +++ b/drivers/net/ethernet/mediatek/mtk_eth_soc.c
54 @@ -152,6 +152,54 @@ static const struct mtk_reg_map mt7986_r
58 +static const struct mtk_reg_map mt7988_reg_map = {
59 + .tx_irq_mask = 0x461c,
60 + .tx_irq_status = 0x4618,
63 + .rx_cnt_cfg = 0x6904,
67 + .delay_irq = 0x6a0c,
68 + .irq_status = 0x6a20,
70 + .adma_rx_dbg0 = 0x6a38,
77 + .rx_cnt_cfg = 0x4504,
81 + .delay_irq = 0x460c,
93 + .tx_sch_rate = 0x4798,
96 + .gdma_to_ppe = 0x3333,
102 + .pse_iq_sta = 0x0180,
103 + .pse_oq_sta = 0x01a0,
106 /* strings used by ethtool */
107 static const struct mtk_ethtool_stats {
108 char str[ETH_GSTRING_LEN];
109 @@ -179,10 +227,54 @@ static const struct mtk_ethtool_stats {
112 static const char * const mtk_clks_source_name[] = {
113 - "ethif", "sgmiitop", "esw", "gp0", "gp1", "gp2", "fe", "trgpll",
114 - "sgmii_tx250m", "sgmii_rx250m", "sgmii_cdr_ref", "sgmii_cdr_fb",
115 - "sgmii2_tx250m", "sgmii2_rx250m", "sgmii2_cdr_ref", "sgmii2_cdr_fb",
116 - "sgmii_ck", "eth2pll", "wocpu0", "wocpu1", "netsys0", "netsys1"
147 + "top_usxgmii0_sel",
148 + "top_usxgmii1_sel",
151 + "top_xfi_phy0_xtal_sel",
152 + "top_xfi_phy1_xtal_sel",
153 + "top_eth_gmii_sel",
154 + "top_eth_refck_50m_sel",
155 + "top_eth_sys_200m_sel",
157 + "top_eth_xgmii_sel",
160 + "top_netsys_500m_sel",
161 + "top_netsys_pao_2x_sel",
162 + "top_netsys_sync_250m_sel",
163 + "top_netsys_ppefb_250m_sel",
164 + "top_netsys_warp_sel",
167 void mtk_w32(struct mtk_eth *eth, u32 val, unsigned reg)
168 @@ -195,7 +287,7 @@ u32 mtk_r32(struct mtk_eth *eth, unsigne
169 return __raw_readl(eth->base + reg);
172 -static u32 mtk_m32(struct mtk_eth *eth, u32 mask, u32 set, unsigned reg)
173 +u32 mtk_m32(struct mtk_eth *eth, u32 mask, u32 set, unsigned int reg)
177 @@ -369,6 +461,19 @@ static void mtk_gmac0_rgmii_adjust(struc
178 dev_err(eth->dev, "Missing PLL configuration, ethernet may not work\n");
181 +static void mtk_setup_bridge_switch(struct mtk_eth *eth)
183 + /* Force Port1 XGMAC Link Up */
184 + mtk_m32(eth, 0, MTK_XGMAC_FORCE_LINK(MTK_GMAC1_ID),
185 + MTK_XGMAC_STS(MTK_GMAC1_ID));
187 + /* Adjust GSW bridge IPG to 11 */
188 + mtk_m32(eth, GSWTX_IPG_MASK | GSWRX_IPG_MASK,
189 + (GSW_IPG_11 << GSWTX_IPG_SHIFT) |
190 + (GSW_IPG_11 << GSWRX_IPG_SHIFT),
194 static struct phylink_pcs *mtk_mac_select_pcs(struct phylink_config *config,
195 phy_interface_t interface)
197 @@ -438,6 +543,8 @@ static void mtk_mac_config(struct phylin
201 + case PHY_INTERFACE_MODE_INTERNAL:
206 @@ -515,6 +622,15 @@ static void mtk_mac_config(struct phylin
211 + if (mtk_is_netsys_v3_or_greater(eth) &&
212 + mac->interface == PHY_INTERFACE_MODE_INTERNAL) {
213 + mtk_w32(mac->hw, MTK_GDMA_XGDM_SEL, MTK_GDMA_EG_CTRL(mac->id));
214 + mtk_w32(mac->hw, MAC_MCR_FORCE_LINK_DOWN, MTK_MAC_MCR(mac->id));
216 + mtk_setup_bridge_switch(eth);
222 @@ -726,11 +842,15 @@ static int mtk_mdio_init(struct mtk_eth
224 divider = min_t(unsigned int, DIV_ROUND_UP(MDC_MAX_FREQ, max_clk), 63);
226 + /* Configure MDC Turbo Mode */
227 + if (mtk_is_netsys_v3_or_greater(eth))
228 + mtk_m32(eth, 0, MISC_MDC_TURBO, MTK_MAC_MISC_V3);
230 /* Configure MDC Divider */
231 - val = mtk_r32(eth, MTK_PPSC);
232 - val &= ~PPSC_MDC_CFG;
233 - val |= FIELD_PREP(PPSC_MDC_CFG, divider) | PPSC_MDC_TURBO;
234 - mtk_w32(eth, val, MTK_PPSC);
235 + val = FIELD_PREP(PPSC_MDC_CFG, divider);
236 + if (!mtk_is_netsys_v3_or_greater(eth))
237 + val |= PPSC_MDC_TURBO;
238 + mtk_m32(eth, PPSC_MDC_CFG, val, MTK_PPSC);
240 dev_dbg(eth->dev, "MDC is running on %d Hz\n", MDC_MAX_FREQ / divider);
242 @@ -1191,10 +1311,19 @@ static void mtk_tx_set_dma_desc_v2(struc
244 WRITE_ONCE(desc->txd3, data);
246 - if (mac->id == MTK_GMAC3_ID)
247 - data = PSE_GDM3_PORT;
249 - data = (mac->id + 1) << TX_DMA_FPORT_SHIFT_V2; /* forward port */
250 + /* set forward port */
253 + data = PSE_GDM1_PORT << TX_DMA_FPORT_SHIFT_V2;
256 + data = PSE_GDM2_PORT << TX_DMA_FPORT_SHIFT_V2;
259 + data = PSE_GDM3_PORT << TX_DMA_FPORT_SHIFT_V2;
263 data |= TX_DMA_SWC_V2 | QID_BITS_V2(info->qid);
264 WRITE_ONCE(desc->txd4, data);
266 @@ -4361,6 +4490,17 @@ static int mtk_add_mac(struct mtk_eth *e
267 mac->phylink_config.supported_interfaces);
270 + if (mtk_is_netsys_v3_or_greater(mac->hw) &&
271 + MTK_HAS_CAPS(mac->hw->soc->caps, MTK_ESW_BIT) &&
272 + id == MTK_GMAC1_ID) {
273 + mac->phylink_config.mac_capabilities = MAC_ASYM_PAUSE |
276 + phy_interface_zero(mac->phylink_config.supported_interfaces);
277 + __set_bit(PHY_INTERFACE_MODE_INTERNAL,
278 + mac->phylink_config.supported_interfaces);
281 phylink = phylink_create(&mac->phylink_config,
282 of_fwnode_handle(mac->of_node),
283 phy_mode, &mtk_phylink_ops);
284 @@ -4881,6 +5021,24 @@ static const struct mtk_soc_data mt7986_
288 +static const struct mtk_soc_data mt7988_data = {
289 + .reg_map = &mt7988_reg_map,
291 + .caps = MT7988_CAPS,
292 + .hw_features = MTK_HW_FEATURES,
293 + .required_clks = MT7988_CLKS_BITMAP,
294 + .required_pctl = false,
297 + .txd_size = sizeof(struct mtk_tx_dma_v2),
298 + .rxd_size = sizeof(struct mtk_rx_dma_v2),
299 + .rx_irq_done_mask = MTK_RX_DONE_INT_V2,
300 + .rx_dma_l4_valid = RX_DMA_L4_VALID_V2,
301 + .dma_max_len = MTK_TX_DMA_BUF_LEN_V2,
302 + .dma_len_offset = 8,
306 static const struct mtk_soc_data rt5350_data = {
307 .reg_map = &mt7628_reg_map,
309 @@ -4899,14 +5057,15 @@ static const struct mtk_soc_data rt5350_
312 const struct of_device_id of_mtk_match[] = {
313 - { .compatible = "mediatek,mt2701-eth", .data = &mt2701_data},
314 - { .compatible = "mediatek,mt7621-eth", .data = &mt7621_data},
315 - { .compatible = "mediatek,mt7622-eth", .data = &mt7622_data},
316 - { .compatible = "mediatek,mt7623-eth", .data = &mt7623_data},
317 - { .compatible = "mediatek,mt7629-eth", .data = &mt7629_data},
318 - { .compatible = "mediatek,mt7981-eth", .data = &mt7981_data},
319 - { .compatible = "mediatek,mt7986-eth", .data = &mt7986_data},
320 - { .compatible = "ralink,rt5350-eth", .data = &rt5350_data},
321 + { .compatible = "mediatek,mt2701-eth", .data = &mt2701_data },
322 + { .compatible = "mediatek,mt7621-eth", .data = &mt7621_data },
323 + { .compatible = "mediatek,mt7622-eth", .data = &mt7622_data },
324 + { .compatible = "mediatek,mt7623-eth", .data = &mt7623_data },
325 + { .compatible = "mediatek,mt7629-eth", .data = &mt7629_data },
326 + { .compatible = "mediatek,mt7981-eth", .data = &mt7981_data },
327 + { .compatible = "mediatek,mt7986-eth", .data = &mt7986_data },
328 + { .compatible = "mediatek,mt7988-eth", .data = &mt7988_data },
329 + { .compatible = "ralink,rt5350-eth", .data = &rt5350_data },
332 MODULE_DEVICE_TABLE(of, of_mtk_match);
333 --- a/drivers/net/ethernet/mediatek/mtk_eth_soc.h
334 +++ b/drivers/net/ethernet/mediatek/mtk_eth_soc.h
336 #define MTK_CDMP_EG_CTRL 0x404
338 /* GDM Exgress Control Register */
339 -#define MTK_GDMA_FWD_CFG(x) (0x500 + (x * 0x1000))
340 +#define MTK_GDMA_FWD_CFG(x) ({ typeof(x) _x = (x); (_x == MTK_GMAC3_ID) ? \
341 + 0x540 : 0x500 + (_x * 0x1000); })
342 #define MTK_GDMA_SPECIAL_TAG BIT(24)
343 #define MTK_GDMA_ICS_EN BIT(22)
344 #define MTK_GDMA_TCS_EN BIT(21)
346 #define MTK_GDMA_TO_PDMA 0x0
347 #define MTK_GDMA_DROP_ALL 0x7777
349 +/* GDM Egress Control Register */
350 +#define MTK_GDMA_EG_CTRL(x) ({ typeof(x) _x = (x); (_x == MTK_GMAC3_ID) ? \
351 + 0x544 : 0x504 + (_x * 0x1000); })
352 +#define MTK_GDMA_XGDM_SEL BIT(31)
354 /* Unicast Filter MAC Address Register - Low */
355 #define MTK_GDMA_MAC_ADRL(x) (0x508 + (x * 0x1000))
358 #define PHY_IAC_TIMEOUT HZ
360 #define MTK_MAC_MISC 0x1000c
361 +#define MTK_MAC_MISC_V3 0x10010
362 #define MTK_MUX_TO_ESW BIT(0)
363 +#define MISC_MDC_TURBO BIT(4)
365 +/* XMAC status registers */
366 +#define MTK_XGMAC_STS(x) (((x) == MTK_GMAC3_ID) ? 0x1001C : 0x1000C)
367 +#define MTK_XGMAC_FORCE_LINK(x) (((x) == MTK_GMAC2_ID) ? BIT(31) : BIT(15))
368 +#define MTK_USXGMII_PCS_LINK BIT(8)
369 +#define MTK_XGMAC_RX_FC BIT(5)
370 +#define MTK_XGMAC_TX_FC BIT(4)
371 +#define MTK_USXGMII_PCS_MODE GENMASK(3, 1)
372 +#define MTK_XGMAC_LINK_STS BIT(0)
374 +/* GSW bridge registers */
375 +#define MTK_GSW_CFG (0x10080)
376 +#define GSWTX_IPG_MASK GENMASK(19, 16)
377 +#define GSWTX_IPG_SHIFT 16
378 +#define GSWRX_IPG_MASK GENMASK(3, 0)
379 +#define GSWRX_IPG_SHIFT 0
380 +#define GSW_IPG_11 11
382 /* Mac control registers */
383 #define MTK_MAC_MCR(x) (0x10100 + (x * 0x100))
384 @@ -647,6 +672,11 @@ enum mtk_clks_map {
395 MTK_CLK_SGMII_TX_250M,
396 @@ -663,6 +693,27 @@ enum mtk_clks_map {
400 + MTK_CLK_ETHWARP_WOCPU2,
401 + MTK_CLK_ETHWARP_WOCPU1,
402 + MTK_CLK_ETHWARP_WOCPU0,
403 + MTK_CLK_TOP_USXGMII_SBUS_0_SEL,
404 + MTK_CLK_TOP_USXGMII_SBUS_1_SEL,
405 + MTK_CLK_TOP_SGM_0_SEL,
406 + MTK_CLK_TOP_SGM_1_SEL,
407 + MTK_CLK_TOP_XFI_PHY_0_XTAL_SEL,
408 + MTK_CLK_TOP_XFI_PHY_1_XTAL_SEL,
409 + MTK_CLK_TOP_ETH_GMII_SEL,
410 + MTK_CLK_TOP_ETH_REFCK_50M_SEL,
411 + MTK_CLK_TOP_ETH_SYS_200M_SEL,
412 + MTK_CLK_TOP_ETH_SYS_SEL,
413 + MTK_CLK_TOP_ETH_XGMII_SEL,
414 + MTK_CLK_TOP_ETH_MII_SEL,
415 + MTK_CLK_TOP_NETSYS_SEL,
416 + MTK_CLK_TOP_NETSYS_500M_SEL,
417 + MTK_CLK_TOP_NETSYS_PAO_2X_SEL,
418 + MTK_CLK_TOP_NETSYS_SYNC_250M_SEL,
419 + MTK_CLK_TOP_NETSYS_PPEFB_250M_SEL,
420 + MTK_CLK_TOP_NETSYS_WARP_SEL,
424 @@ -716,6 +767,36 @@ enum mtk_clks_map {
425 BIT_ULL(MTK_CLK_SGMII2_RX_250M) | \
426 BIT_ULL(MTK_CLK_SGMII2_CDR_REF) | \
427 BIT_ULL(MTK_CLK_SGMII2_CDR_FB))
428 +#define MT7988_CLKS_BITMAP (BIT_ULL(MTK_CLK_FE) | BIT_ULL(MTK_CLK_ESW) | \
429 + BIT_ULL(MTK_CLK_GP1) | BIT_ULL(MTK_CLK_GP2) | \
430 + BIT_ULL(MTK_CLK_GP3) | BIT_ULL(MTK_CLK_XGP1) | \
431 + BIT_ULL(MTK_CLK_XGP2) | BIT_ULL(MTK_CLK_XGP3) | \
432 + BIT_ULL(MTK_CLK_CRYPTO) | \
433 + BIT_ULL(MTK_CLK_SGMII_TX_250M) | \
434 + BIT_ULL(MTK_CLK_SGMII_RX_250M) | \
435 + BIT_ULL(MTK_CLK_SGMII2_TX_250M) | \
436 + BIT_ULL(MTK_CLK_SGMII2_RX_250M) | \
437 + BIT_ULL(MTK_CLK_ETHWARP_WOCPU2) | \
438 + BIT_ULL(MTK_CLK_ETHWARP_WOCPU1) | \
439 + BIT_ULL(MTK_CLK_ETHWARP_WOCPU0) | \
440 + BIT_ULL(MTK_CLK_TOP_USXGMII_SBUS_0_SEL) | \
441 + BIT_ULL(MTK_CLK_TOP_USXGMII_SBUS_1_SEL) | \
442 + BIT_ULL(MTK_CLK_TOP_SGM_0_SEL) | \
443 + BIT_ULL(MTK_CLK_TOP_SGM_1_SEL) | \
444 + BIT_ULL(MTK_CLK_TOP_XFI_PHY_0_XTAL_SEL) | \
445 + BIT_ULL(MTK_CLK_TOP_XFI_PHY_1_XTAL_SEL) | \
446 + BIT_ULL(MTK_CLK_TOP_ETH_GMII_SEL) | \
447 + BIT_ULL(MTK_CLK_TOP_ETH_REFCK_50M_SEL) | \
448 + BIT_ULL(MTK_CLK_TOP_ETH_SYS_200M_SEL) | \
449 + BIT_ULL(MTK_CLK_TOP_ETH_SYS_SEL) | \
450 + BIT_ULL(MTK_CLK_TOP_ETH_XGMII_SEL) | \
451 + BIT_ULL(MTK_CLK_TOP_ETH_MII_SEL) | \
452 + BIT_ULL(MTK_CLK_TOP_NETSYS_SEL) | \
453 + BIT_ULL(MTK_CLK_TOP_NETSYS_500M_SEL) | \
454 + BIT_ULL(MTK_CLK_TOP_NETSYS_PAO_2X_SEL) | \
455 + BIT_ULL(MTK_CLK_TOP_NETSYS_SYNC_250M_SEL) | \
456 + BIT_ULL(MTK_CLK_TOP_NETSYS_PPEFB_250M_SEL) | \
457 + BIT_ULL(MTK_CLK_TOP_NETSYS_WARP_SEL))
461 @@ -964,6 +1045,8 @@ enum mkt_eth_capabilities {
462 MTK_MUX_GMAC12_TO_GEPHY_SGMII | MTK_QDMA | \
465 +#define MT7988_CAPS (MTK_GDM1_ESW | MTK_QDMA | MTK_RSTCTRL_PPE1)
467 struct mtk_tx_dma_desc_info {
470 @@ -1309,6 +1392,7 @@ void mtk_stats_update_mac(struct mtk_mac
472 void mtk_w32(struct mtk_eth *eth, u32 val, unsigned reg);
473 u32 mtk_r32(struct mtk_eth *eth, unsigned reg);
474 +u32 mtk_m32(struct mtk_eth *eth, u32 mask, u32 set, unsigned int reg);
476 int mtk_gmac_sgmii_path_setup(struct mtk_eth *eth, int mac_id);
477 int mtk_gmac_gephy_path_setup(struct mtk_eth *eth, int mac_id);