1 // SPDX-License-Identifier: GPL-2.0 2 // Copyright (c) 2018-2019 MediaTek Inc. 3 /* A library for MediaTek SGMII circuit 4 * 5 * Author: Sean Wang <sean.wang@mediatek.com> 6 * Author: Alexander Couzens <lynxis@fe80.eu> 7 * Author: Daniel Golle <daniel@makrotopia.org> 8 * 9 */ 10 11 #include <linux/mdio.h> 12 #include <linux/of.h> 13 #include <linux/pcs/pcs-mtk-lynxi.h> 14 #include <linux/phylink.h> 15 #include <linux/regmap.h> 16 17 /* SGMII subsystem config registers */ 18 /* BMCR (low 16) BMSR (high 16) */ 19 #define SGMSYS_PCS_CONTROL_1 0x0 20 #define SGMII_BMCR GENMASK(15, 0) 21 #define SGMII_BMSR GENMASK(31, 16) 22 23 #define SGMSYS_PCS_DEVICE_ID 0x4 24 #define SGMII_LYNXI_DEV_ID 0x4d544950 25 26 #define SGMSYS_PCS_ADVERTISE 0x8 27 #define SGMII_ADVERTISE GENMASK(15, 0) 28 #define SGMII_LPA GENMASK(31, 16) 29 30 #define SGMSYS_PCS_SCRATCH 0x14 31 #define SGMII_DEV_VERSION GENMASK(31, 16) 32 33 /* Register to programmable link timer, the unit in 2 * 8ns */ 34 #define SGMSYS_PCS_LINK_TIMER 0x18 35 #define SGMII_LINK_TIMER_MASK GENMASK(19, 0) 36 #define SGMII_LINK_TIMER_VAL(ns) FIELD_PREP(SGMII_LINK_TIMER_MASK, \ 37 ((ns) / 2 / 8)) 38 39 /* Register to control remote fault */ 40 #define SGMSYS_SGMII_MODE 0x20 41 #define SGMII_IF_MODE_SGMII BIT(0) 42 #define SGMII_SPEED_DUPLEX_AN BIT(1) 43 #define SGMII_SPEED_MASK GENMASK(3, 2) 44 #define SGMII_SPEED_10 FIELD_PREP(SGMII_SPEED_MASK, 0) 45 #define SGMII_SPEED_100 FIELD_PREP(SGMII_SPEED_MASK, 1) 46 #define SGMII_SPEED_1000 FIELD_PREP(SGMII_SPEED_MASK, 2) 47 #define SGMII_DUPLEX_HALF BIT(4) 48 #define SGMII_REMOTE_FAULT_DIS BIT(8) 49 50 /* Register to reset SGMII design */ 51 #define SGMSYS_RESERVED_0 0x34 52 #define SGMII_SW_RESET BIT(0) 53 54 /* Register to set SGMII speed, ANA RG_ Control Signals III */ 55 #define SGMII_PHY_SPEED_MASK GENMASK(3, 2) 56 #define SGMII_PHY_SPEED_1_25G FIELD_PREP(SGMII_PHY_SPEED_MASK, 0) 57 #define SGMII_PHY_SPEED_3_125G FIELD_PREP(SGMII_PHY_SPEED_MASK, 1) 58 59 /* Register to power up QPHY */ 60 #define SGMSYS_QPHY_PWR_STATE_CTRL 0xe8 61 #define SGMII_PHYA_PWD BIT(4) 62 63 /* Register to QPHY wrapper control */ 64 #define SGMSYS_QPHY_WRAP_CTRL 0xec 65 #define SGMII_PN_SWAP_MASK GENMASK(1, 0) 66 #define SGMII_PN_SWAP_TX_RX (BIT(0) | BIT(1)) 67 68 /* struct mtk_pcs_lynxi - This structure holds each sgmii regmap andassociated 69 * data 70 * @regmap: The register map pointing at the range used to setup 71 * SGMII modes 72 * @dev: Pointer to device owning the PCS 73 * @ana_rgc3: The offset of register ANA_RGC3 relative to regmap 74 * @interface: Currently configured interface mode 75 * @pcs: Phylink PCS structure 76 * @flags: Flags indicating hardware properties 77 */ 78 struct mtk_pcs_lynxi { 79 struct regmap *regmap; 80 u32 ana_rgc3; 81 phy_interface_t interface; 82 struct phylink_pcs pcs; 83 u32 flags; 84 }; 85 86 static struct mtk_pcs_lynxi *pcs_to_mtk_pcs_lynxi(struct phylink_pcs *pcs) 87 { 88 return container_of(pcs, struct mtk_pcs_lynxi, pcs); 89 } 90 91 static unsigned int mtk_pcs_lynxi_inband_caps(struct phylink_pcs *pcs, 92 phy_interface_t interface) 93 { 94 switch (interface) { 95 case PHY_INTERFACE_MODE_1000BASEX: 96 case PHY_INTERFACE_MODE_2500BASEX: 97 case PHY_INTERFACE_MODE_SGMII: 98 case PHY_INTERFACE_MODE_QSGMII: 99 return LINK_INBAND_DISABLE | LINK_INBAND_ENABLE; 100 101 default: 102 return 0; 103 } 104 } 105 106 static void mtk_pcs_lynxi_get_state(struct phylink_pcs *pcs, 107 struct phylink_link_state *state) 108 { 109 struct mtk_pcs_lynxi *mpcs = pcs_to_mtk_pcs_lynxi(pcs); 110 unsigned int bm, adv; 111 112 /* Read the BMSR and LPA */ 113 regmap_read(mpcs->regmap, SGMSYS_PCS_CONTROL_1, &bm); 114 regmap_read(mpcs->regmap, SGMSYS_PCS_ADVERTISE, &adv); 115 116 phylink_mii_c22_pcs_decode_state(state, FIELD_GET(SGMII_BMSR, bm), 117 FIELD_GET(SGMII_LPA, adv)); 118 } 119 120 static int mtk_pcs_lynxi_config(struct phylink_pcs *pcs, unsigned int neg_mode, 121 phy_interface_t interface, 122 const unsigned long *advertising, 123 bool permit_pause_to_mac) 124 { 125 struct mtk_pcs_lynxi *mpcs = pcs_to_mtk_pcs_lynxi(pcs); 126 bool mode_changed = false, changed; 127 unsigned int rgc3, sgm_mode, bmcr; 128 int advertise, link_timer; 129 130 advertise = phylink_mii_c22_pcs_encode_advertisement(interface, 131 advertising); 132 if (advertise < 0) 133 return advertise; 134 135 /* Clearing IF_MODE_BIT0 switches the PCS to BASE-X mode, and 136 * we assume that fixes it's speed at bitrate = line rate (in 137 * other words, 1000Mbps or 2500Mbps). 138 */ 139 if (interface == PHY_INTERFACE_MODE_SGMII) 140 sgm_mode = SGMII_IF_MODE_SGMII; 141 else 142 sgm_mode = 0; 143 144 if (neg_mode & PHYLINK_PCS_NEG_INBAND) 145 sgm_mode |= SGMII_REMOTE_FAULT_DIS; 146 147 if (neg_mode == PHYLINK_PCS_NEG_INBAND_ENABLED) { 148 if (interface == PHY_INTERFACE_MODE_SGMII) 149 sgm_mode |= SGMII_SPEED_DUPLEX_AN; 150 bmcr = BMCR_ANENABLE; 151 } else { 152 bmcr = 0; 153 } 154 155 if (mpcs->interface != interface) { 156 link_timer = phylink_get_link_timer_ns(interface); 157 if (link_timer < 0) 158 return link_timer; 159 160 /* PHYA power down */ 161 regmap_set_bits(mpcs->regmap, SGMSYS_QPHY_PWR_STATE_CTRL, 162 SGMII_PHYA_PWD); 163 164 /* Reset SGMII PCS state */ 165 regmap_set_bits(mpcs->regmap, SGMSYS_RESERVED_0, 166 SGMII_SW_RESET); 167 168 if (mpcs->flags & MTK_SGMII_FLAG_PN_SWAP) 169 regmap_update_bits(mpcs->regmap, SGMSYS_QPHY_WRAP_CTRL, 170 SGMII_PN_SWAP_MASK, 171 SGMII_PN_SWAP_TX_RX); 172 173 if (interface == PHY_INTERFACE_MODE_2500BASEX) 174 rgc3 = SGMII_PHY_SPEED_3_125G; 175 else 176 rgc3 = SGMII_PHY_SPEED_1_25G; 177 178 /* Configure the underlying interface speed */ 179 regmap_update_bits(mpcs->regmap, mpcs->ana_rgc3, 180 SGMII_PHY_SPEED_MASK, rgc3); 181 182 /* Setup the link timer */ 183 regmap_write(mpcs->regmap, SGMSYS_PCS_LINK_TIMER, 184 SGMII_LINK_TIMER_VAL(link_timer)); 185 186 mpcs->interface = interface; 187 mode_changed = true; 188 } 189 190 /* Update the advertisement, noting whether it has changed */ 191 regmap_update_bits_check(mpcs->regmap, SGMSYS_PCS_ADVERTISE, 192 SGMII_ADVERTISE, advertise, &changed); 193 194 /* Update the sgmsys mode register */ 195 regmap_update_bits(mpcs->regmap, SGMSYS_SGMII_MODE, 196 SGMII_REMOTE_FAULT_DIS | SGMII_SPEED_DUPLEX_AN | 197 SGMII_IF_MODE_SGMII, sgm_mode); 198 199 /* Update the BMCR */ 200 regmap_update_bits(mpcs->regmap, SGMSYS_PCS_CONTROL_1, 201 BMCR_ANENABLE, bmcr); 202 203 /* Release PHYA power down state 204 * Only removing bit SGMII_PHYA_PWD isn't enough. 205 * There are cases when the SGMII_PHYA_PWD register contains 0x9 which 206 * prevents SGMII from working. The SGMII still shows link but no traffic 207 * can flow. Writing 0x0 to the PHYA_PWD register fix the issue. 0x0 was 208 * taken from a good working state of the SGMII interface. 209 * Unknown how much the QPHY needs but it is racy without a sleep. 210 * Tested on mt7622 & mt7986. 211 */ 212 usleep_range(50, 100); 213 regmap_write(mpcs->regmap, SGMSYS_QPHY_PWR_STATE_CTRL, 0); 214 215 return changed || mode_changed; 216 } 217 218 static void mtk_pcs_lynxi_restart_an(struct phylink_pcs *pcs) 219 { 220 struct mtk_pcs_lynxi *mpcs = pcs_to_mtk_pcs_lynxi(pcs); 221 222 regmap_set_bits(mpcs->regmap, SGMSYS_PCS_CONTROL_1, BMCR_ANRESTART); 223 } 224 225 static void mtk_pcs_lynxi_link_up(struct phylink_pcs *pcs, 226 unsigned int neg_mode, 227 phy_interface_t interface, int speed, 228 int duplex) 229 { 230 struct mtk_pcs_lynxi *mpcs = pcs_to_mtk_pcs_lynxi(pcs); 231 unsigned int sgm_mode; 232 233 if (neg_mode != PHYLINK_PCS_NEG_INBAND_ENABLED) { 234 /* Force the speed and duplex setting */ 235 if (speed == SPEED_10) 236 sgm_mode = SGMII_SPEED_10; 237 else if (speed == SPEED_100) 238 sgm_mode = SGMII_SPEED_100; 239 else 240 sgm_mode = SGMII_SPEED_1000; 241 242 if (duplex != DUPLEX_FULL) 243 sgm_mode |= SGMII_DUPLEX_HALF; 244 245 regmap_update_bits(mpcs->regmap, SGMSYS_SGMII_MODE, 246 SGMII_DUPLEX_HALF | SGMII_SPEED_MASK, 247 sgm_mode); 248 } 249 } 250 251 static void mtk_pcs_lynxi_disable(struct phylink_pcs *pcs) 252 { 253 struct mtk_pcs_lynxi *mpcs = pcs_to_mtk_pcs_lynxi(pcs); 254 255 mpcs->interface = PHY_INTERFACE_MODE_NA; 256 } 257 258 static const struct phylink_pcs_ops mtk_pcs_lynxi_ops = { 259 .pcs_inband_caps = mtk_pcs_lynxi_inband_caps, 260 .pcs_get_state = mtk_pcs_lynxi_get_state, 261 .pcs_config = mtk_pcs_lynxi_config, 262 .pcs_an_restart = mtk_pcs_lynxi_restart_an, 263 .pcs_link_up = mtk_pcs_lynxi_link_up, 264 .pcs_disable = mtk_pcs_lynxi_disable, 265 }; 266 267 struct phylink_pcs *mtk_pcs_lynxi_create(struct device *dev, 268 struct regmap *regmap, u32 ana_rgc3, 269 u32 flags) 270 { 271 struct mtk_pcs_lynxi *mpcs; 272 u32 id, ver; 273 int ret; 274 275 ret = regmap_read(regmap, SGMSYS_PCS_DEVICE_ID, &id); 276 if (ret < 0) 277 return NULL; 278 279 if (id != SGMII_LYNXI_DEV_ID) { 280 dev_err(dev, "unknown PCS device id %08x\n", id); 281 return NULL; 282 } 283 284 ret = regmap_read(regmap, SGMSYS_PCS_SCRATCH, &ver); 285 if (ret < 0) 286 return NULL; 287 288 ver = FIELD_GET(SGMII_DEV_VERSION, ver); 289 if (ver != 0x1) { 290 dev_err(dev, "unknown PCS device version %04x\n", ver); 291 return NULL; 292 } 293 294 dev_dbg(dev, "MediaTek LynxI SGMII PCS (id 0x%08x, ver 0x%04x)\n", id, 295 ver); 296 297 mpcs = kzalloc(sizeof(*mpcs), GFP_KERNEL); 298 if (!mpcs) 299 return NULL; 300 301 mpcs->ana_rgc3 = ana_rgc3; 302 mpcs->regmap = regmap; 303 mpcs->flags = flags; 304 mpcs->pcs.ops = &mtk_pcs_lynxi_ops; 305 mpcs->pcs.neg_mode = true; 306 mpcs->pcs.poll = true; 307 mpcs->interface = PHY_INTERFACE_MODE_NA; 308 309 return &mpcs->pcs; 310 } 311 EXPORT_SYMBOL(mtk_pcs_lynxi_create); 312 313 void mtk_pcs_lynxi_destroy(struct phylink_pcs *pcs) 314 { 315 if (!pcs) 316 return; 317 318 kfree(pcs_to_mtk_pcs_lynxi(pcs)); 319 } 320 EXPORT_SYMBOL(mtk_pcs_lynxi_destroy); 321 322 MODULE_DESCRIPTION("MediaTek SGMII library for LynxI"); 323 MODULE_LICENSE("GPL"); 324