18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 28c2ecf20Sopenharmony_ci// Copyright (c) 2018-2019 MediaTek Inc. 38c2ecf20Sopenharmony_ci 48c2ecf20Sopenharmony_ci/* A library for MediaTek SGMII circuit 58c2ecf20Sopenharmony_ci * 68c2ecf20Sopenharmony_ci * Author: Sean Wang <sean.wang@mediatek.com> 78c2ecf20Sopenharmony_ci * 88c2ecf20Sopenharmony_ci */ 98c2ecf20Sopenharmony_ci 108c2ecf20Sopenharmony_ci#include <linux/mfd/syscon.h> 118c2ecf20Sopenharmony_ci#include <linux/of.h> 128c2ecf20Sopenharmony_ci#include <linux/regmap.h> 138c2ecf20Sopenharmony_ci 148c2ecf20Sopenharmony_ci#include "mtk_eth_soc.h" 158c2ecf20Sopenharmony_ci 168c2ecf20Sopenharmony_ciint mtk_sgmii_init(struct mtk_sgmii *ss, struct device_node *r, u32 ana_rgc3) 178c2ecf20Sopenharmony_ci{ 188c2ecf20Sopenharmony_ci struct device_node *np; 198c2ecf20Sopenharmony_ci int i; 208c2ecf20Sopenharmony_ci 218c2ecf20Sopenharmony_ci ss->ana_rgc3 = ana_rgc3; 228c2ecf20Sopenharmony_ci 238c2ecf20Sopenharmony_ci for (i = 0; i < MTK_MAX_DEVS; i++) { 248c2ecf20Sopenharmony_ci np = of_parse_phandle(r, "mediatek,sgmiisys", i); 258c2ecf20Sopenharmony_ci if (!np) 268c2ecf20Sopenharmony_ci break; 278c2ecf20Sopenharmony_ci 288c2ecf20Sopenharmony_ci ss->regmap[i] = syscon_node_to_regmap(np); 298c2ecf20Sopenharmony_ci of_node_put(np); 308c2ecf20Sopenharmony_ci if (IS_ERR(ss->regmap[i])) 318c2ecf20Sopenharmony_ci return PTR_ERR(ss->regmap[i]); 328c2ecf20Sopenharmony_ci } 338c2ecf20Sopenharmony_ci 348c2ecf20Sopenharmony_ci return 0; 358c2ecf20Sopenharmony_ci} 368c2ecf20Sopenharmony_ci 378c2ecf20Sopenharmony_ciint mtk_sgmii_setup_mode_an(struct mtk_sgmii *ss, int id) 388c2ecf20Sopenharmony_ci{ 398c2ecf20Sopenharmony_ci unsigned int val; 408c2ecf20Sopenharmony_ci 418c2ecf20Sopenharmony_ci if (!ss->regmap[id]) 428c2ecf20Sopenharmony_ci return -EINVAL; 438c2ecf20Sopenharmony_ci 448c2ecf20Sopenharmony_ci /* Setup the link timer and QPHY power up inside SGMIISYS */ 458c2ecf20Sopenharmony_ci regmap_write(ss->regmap[id], SGMSYS_PCS_LINK_TIMER, 468c2ecf20Sopenharmony_ci SGMII_LINK_TIMER_DEFAULT); 478c2ecf20Sopenharmony_ci 488c2ecf20Sopenharmony_ci regmap_read(ss->regmap[id], SGMSYS_SGMII_MODE, &val); 498c2ecf20Sopenharmony_ci val |= SGMII_REMOTE_FAULT_DIS; 508c2ecf20Sopenharmony_ci regmap_write(ss->regmap[id], SGMSYS_SGMII_MODE, val); 518c2ecf20Sopenharmony_ci 528c2ecf20Sopenharmony_ci regmap_read(ss->regmap[id], SGMSYS_PCS_CONTROL_1, &val); 538c2ecf20Sopenharmony_ci val |= SGMII_AN_RESTART; 548c2ecf20Sopenharmony_ci regmap_write(ss->regmap[id], SGMSYS_PCS_CONTROL_1, val); 558c2ecf20Sopenharmony_ci 568c2ecf20Sopenharmony_ci regmap_read(ss->regmap[id], SGMSYS_QPHY_PWR_STATE_CTRL, &val); 578c2ecf20Sopenharmony_ci val &= ~SGMII_PHYA_PWD; 588c2ecf20Sopenharmony_ci regmap_write(ss->regmap[id], SGMSYS_QPHY_PWR_STATE_CTRL, val); 598c2ecf20Sopenharmony_ci 608c2ecf20Sopenharmony_ci return 0; 618c2ecf20Sopenharmony_ci} 628c2ecf20Sopenharmony_ci 638c2ecf20Sopenharmony_ciint mtk_sgmii_setup_mode_force(struct mtk_sgmii *ss, int id, 648c2ecf20Sopenharmony_ci const struct phylink_link_state *state) 658c2ecf20Sopenharmony_ci{ 668c2ecf20Sopenharmony_ci unsigned int val; 678c2ecf20Sopenharmony_ci 688c2ecf20Sopenharmony_ci if (!ss->regmap[id]) 698c2ecf20Sopenharmony_ci return -EINVAL; 708c2ecf20Sopenharmony_ci 718c2ecf20Sopenharmony_ci regmap_read(ss->regmap[id], ss->ana_rgc3, &val); 728c2ecf20Sopenharmony_ci val &= ~RG_PHY_SPEED_MASK; 738c2ecf20Sopenharmony_ci if (state->interface == PHY_INTERFACE_MODE_2500BASEX) 748c2ecf20Sopenharmony_ci val |= RG_PHY_SPEED_3_125G; 758c2ecf20Sopenharmony_ci regmap_write(ss->regmap[id], ss->ana_rgc3, val); 768c2ecf20Sopenharmony_ci 778c2ecf20Sopenharmony_ci /* Disable SGMII AN */ 788c2ecf20Sopenharmony_ci regmap_read(ss->regmap[id], SGMSYS_PCS_CONTROL_1, &val); 798c2ecf20Sopenharmony_ci val &= ~SGMII_AN_ENABLE; 808c2ecf20Sopenharmony_ci regmap_write(ss->regmap[id], SGMSYS_PCS_CONTROL_1, val); 818c2ecf20Sopenharmony_ci 828c2ecf20Sopenharmony_ci /* SGMII force mode setting */ 838c2ecf20Sopenharmony_ci regmap_read(ss->regmap[id], SGMSYS_SGMII_MODE, &val); 848c2ecf20Sopenharmony_ci val &= ~SGMII_IF_MODE_MASK; 858c2ecf20Sopenharmony_ci 868c2ecf20Sopenharmony_ci switch (state->speed) { 878c2ecf20Sopenharmony_ci case SPEED_10: 888c2ecf20Sopenharmony_ci val |= SGMII_SPEED_10; 898c2ecf20Sopenharmony_ci break; 908c2ecf20Sopenharmony_ci case SPEED_100: 918c2ecf20Sopenharmony_ci val |= SGMII_SPEED_100; 928c2ecf20Sopenharmony_ci break; 938c2ecf20Sopenharmony_ci case SPEED_2500: 948c2ecf20Sopenharmony_ci case SPEED_1000: 958c2ecf20Sopenharmony_ci val |= SGMII_SPEED_1000; 968c2ecf20Sopenharmony_ci break; 978c2ecf20Sopenharmony_ci } 988c2ecf20Sopenharmony_ci 998c2ecf20Sopenharmony_ci if (state->duplex == DUPLEX_FULL) 1008c2ecf20Sopenharmony_ci val |= SGMII_DUPLEX_FULL; 1018c2ecf20Sopenharmony_ci 1028c2ecf20Sopenharmony_ci regmap_write(ss->regmap[id], SGMSYS_SGMII_MODE, val); 1038c2ecf20Sopenharmony_ci 1048c2ecf20Sopenharmony_ci /* Release PHYA power down state */ 1058c2ecf20Sopenharmony_ci regmap_read(ss->regmap[id], SGMSYS_QPHY_PWR_STATE_CTRL, &val); 1068c2ecf20Sopenharmony_ci val &= ~SGMII_PHYA_PWD; 1078c2ecf20Sopenharmony_ci regmap_write(ss->regmap[id], SGMSYS_QPHY_PWR_STATE_CTRL, val); 1088c2ecf20Sopenharmony_ci 1098c2ecf20Sopenharmony_ci return 0; 1108c2ecf20Sopenharmony_ci} 1118c2ecf20Sopenharmony_ci 1128c2ecf20Sopenharmony_civoid mtk_sgmii_restart_an(struct mtk_eth *eth, int mac_id) 1138c2ecf20Sopenharmony_ci{ 1148c2ecf20Sopenharmony_ci struct mtk_sgmii *ss = eth->sgmii; 1158c2ecf20Sopenharmony_ci unsigned int val, sid; 1168c2ecf20Sopenharmony_ci 1178c2ecf20Sopenharmony_ci /* Decide how GMAC and SGMIISYS be mapped */ 1188c2ecf20Sopenharmony_ci sid = (MTK_HAS_CAPS(eth->soc->caps, MTK_SHARED_SGMII)) ? 1198c2ecf20Sopenharmony_ci 0 : mac_id; 1208c2ecf20Sopenharmony_ci 1218c2ecf20Sopenharmony_ci if (!ss->regmap[sid]) 1228c2ecf20Sopenharmony_ci return; 1238c2ecf20Sopenharmony_ci 1248c2ecf20Sopenharmony_ci regmap_read(ss->regmap[sid], SGMSYS_PCS_CONTROL_1, &val); 1258c2ecf20Sopenharmony_ci val |= SGMII_AN_RESTART; 1268c2ecf20Sopenharmony_ci regmap_write(ss->regmap[sid], SGMSYS_PCS_CONTROL_1, val); 1278c2ecf20Sopenharmony_ci} 128