1 // SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause) 2 /* Copyright 2020 NXP 3 * Lynx PCS MDIO helpers 4 */ 5 6 #include <linux/mdio.h> 7 #include <linux/phylink.h> 8 #include <linux/pcs-lynx.h> 9 #include <linux/property.h> 10 11 #define SGMII_CLOCK_PERIOD_NS 8 /* PCS is clocked at 125 MHz */ 12 #define LINK_TIMER_VAL(ns) ((u32)((ns) / SGMII_CLOCK_PERIOD_NS)) 13 14 #define LINK_TIMER_LO 0x12 15 #define LINK_TIMER_HI 0x13 16 #define IF_MODE 0x14 17 #define IF_MODE_SGMII_EN BIT(0) 18 #define IF_MODE_USE_SGMII_AN BIT(1) 19 #define IF_MODE_SPEED(x) (((x) << 2) & GENMASK(3, 2)) 20 #define IF_MODE_SPEED_MSK GENMASK(3, 2) 21 #define IF_MODE_HALF_DUPLEX BIT(4) 22 23 struct lynx_pcs { 24 struct phylink_pcs pcs; 25 struct mdio_device *mdio; 26 }; 27 28 enum sgmii_speed { 29 SGMII_SPEED_10 = 0, 30 SGMII_SPEED_100 = 1, 31 SGMII_SPEED_1000 = 2, 32 SGMII_SPEED_2500 = 2, 33 }; 34 35 #define phylink_pcs_to_lynx(pl_pcs) container_of((pl_pcs), struct lynx_pcs, pcs) 36 #define lynx_to_phylink_pcs(lynx) (&(lynx)->pcs) 37 38 static void lynx_pcs_get_state_usxgmii(struct mdio_device *pcs, 39 struct phylink_link_state *state) 40 { 41 struct mii_bus *bus = pcs->bus; 42 int addr = pcs->addr; 43 int status, lpa; 44 45 status = mdiobus_c45_read(bus, addr, MDIO_MMD_VEND2, MII_BMSR); 46 if (status < 0) 47 return; 48 49 state->link = !!(status & MDIO_STAT1_LSTATUS); 50 state->an_complete = !!(status & MDIO_AN_STAT1_COMPLETE); 51 if (!state->link || !state->an_complete) 52 return; 53 54 lpa = mdiobus_c45_read(bus, addr, MDIO_MMD_VEND2, MII_LPA); 55 if (lpa < 0) 56 return; 57 58 phylink_decode_usxgmii_word(state, lpa); 59 } 60 61 static void lynx_pcs_get_state_2500basex(struct mdio_device *pcs, 62 struct phylink_link_state *state) 63 { 64 int bmsr, lpa; 65 66 bmsr = mdiodev_read(pcs, MII_BMSR); 67 lpa = mdiodev_read(pcs, MII_LPA); 68 if (bmsr < 0 || lpa < 0) { 69 state->link = false; 70 return; 71 } 72 73 state->link = !!(bmsr & BMSR_LSTATUS); 74 state->an_complete = !!(bmsr & BMSR_ANEGCOMPLETE); 75 if (!state->link) 76 return; 77 78 state->speed = SPEED_2500; 79 state->pause |= MLO_PAUSE_TX | MLO_PAUSE_RX; 80 state->duplex = DUPLEX_FULL; 81 } 82 83 static void lynx_pcs_get_state(struct phylink_pcs *pcs, 84 struct phylink_link_state *state) 85 { 86 struct lynx_pcs *lynx = phylink_pcs_to_lynx(pcs); 87 88 switch (state->interface) { 89 case PHY_INTERFACE_MODE_1000BASEX: 90 case PHY_INTERFACE_MODE_SGMII: 91 case PHY_INTERFACE_MODE_QSGMII: 92 phylink_mii_c22_pcs_get_state(lynx->mdio, state); 93 break; 94 case PHY_INTERFACE_MODE_2500BASEX: 95 lynx_pcs_get_state_2500basex(lynx->mdio, state); 96 break; 97 case PHY_INTERFACE_MODE_USXGMII: 98 lynx_pcs_get_state_usxgmii(lynx->mdio, state); 99 break; 100 case PHY_INTERFACE_MODE_10GBASER: 101 phylink_mii_c45_pcs_get_state(lynx->mdio, state); 102 break; 103 default: 104 break; 105 } 106 107 dev_dbg(&lynx->mdio->dev, 108 "mode=%s/%s/%s link=%u an_complete=%u\n", 109 phy_modes(state->interface), 110 phy_speed_to_str(state->speed), 111 phy_duplex_to_str(state->duplex), 112 state->link, state->an_complete); 113 } 114 115 static int lynx_pcs_config_giga(struct mdio_device *pcs, unsigned int mode, 116 phy_interface_t interface, 117 const unsigned long *advertising) 118 { 119 int link_timer_ns; 120 u32 link_timer; 121 u16 if_mode; 122 int err; 123 124 link_timer_ns = phylink_get_link_timer_ns(interface); 125 if (link_timer_ns > 0) { 126 link_timer = LINK_TIMER_VAL(link_timer_ns); 127 128 mdiodev_write(pcs, LINK_TIMER_LO, link_timer & 0xffff); 129 mdiodev_write(pcs, LINK_TIMER_HI, link_timer >> 16); 130 } 131 132 if (interface == PHY_INTERFACE_MODE_1000BASEX) { 133 if_mode = 0; 134 } else { 135 if_mode = IF_MODE_SGMII_EN; 136 if (mode == MLO_AN_INBAND) 137 if_mode |= IF_MODE_USE_SGMII_AN; 138 } 139 140 err = mdiodev_modify(pcs, IF_MODE, 141 IF_MODE_SGMII_EN | IF_MODE_USE_SGMII_AN, 142 if_mode); 143 if (err) 144 return err; 145 146 return phylink_mii_c22_pcs_config(pcs, mode, interface, advertising); 147 } 148 149 static int lynx_pcs_config_usxgmii(struct mdio_device *pcs, unsigned int mode, 150 const unsigned long *advertising) 151 { 152 struct mii_bus *bus = pcs->bus; 153 int addr = pcs->addr; 154 155 if (!phylink_autoneg_inband(mode)) { 156 dev_err(&pcs->dev, "USXGMII only supports in-band AN for now\n"); 157 return -EOPNOTSUPP; 158 } 159 160 /* Configure device ability for the USXGMII Replicator */ 161 return mdiobus_c45_write(bus, addr, MDIO_MMD_VEND2, MII_ADVERTISE, 162 MDIO_USXGMII_10G | MDIO_USXGMII_LINK | 163 MDIO_USXGMII_FULL_DUPLEX | 164 ADVERTISE_SGMII | ADVERTISE_LPACK); 165 } 166 167 static int lynx_pcs_config(struct phylink_pcs *pcs, unsigned int mode, 168 phy_interface_t ifmode, 169 const unsigned long *advertising, 170 bool permit) 171 { 172 struct lynx_pcs *lynx = phylink_pcs_to_lynx(pcs); 173 174 switch (ifmode) { 175 case PHY_INTERFACE_MODE_1000BASEX: 176 case PHY_INTERFACE_MODE_SGMII: 177 case PHY_INTERFACE_MODE_QSGMII: 178 return lynx_pcs_config_giga(lynx->mdio, mode, ifmode, 179 advertising); 180 case PHY_INTERFACE_MODE_2500BASEX: 181 if (phylink_autoneg_inband(mode)) { 182 dev_err(&lynx->mdio->dev, 183 "AN not supported on 3.125GHz SerDes lane\n"); 184 return -EOPNOTSUPP; 185 } 186 break; 187 case PHY_INTERFACE_MODE_USXGMII: 188 return lynx_pcs_config_usxgmii(lynx->mdio, mode, advertising); 189 case PHY_INTERFACE_MODE_10GBASER: 190 /* Nothing to do here for 10GBASER */ 191 break; 192 default: 193 return -EOPNOTSUPP; 194 } 195 196 return 0; 197 } 198 199 static void lynx_pcs_an_restart(struct phylink_pcs *pcs) 200 { 201 struct lynx_pcs *lynx = phylink_pcs_to_lynx(pcs); 202 203 phylink_mii_c22_pcs_an_restart(lynx->mdio); 204 } 205 206 static void lynx_pcs_link_up_sgmii(struct mdio_device *pcs, unsigned int mode, 207 int speed, int duplex) 208 { 209 u16 if_mode = 0, sgmii_speed; 210 211 /* The PCS needs to be configured manually only 212 * when not operating on in-band mode 213 */ 214 if (mode == MLO_AN_INBAND) 215 return; 216 217 if (duplex == DUPLEX_HALF) 218 if_mode |= IF_MODE_HALF_DUPLEX; 219 220 switch (speed) { 221 case SPEED_1000: 222 sgmii_speed = SGMII_SPEED_1000; 223 break; 224 case SPEED_100: 225 sgmii_speed = SGMII_SPEED_100; 226 break; 227 case SPEED_10: 228 sgmii_speed = SGMII_SPEED_10; 229 break; 230 case SPEED_UNKNOWN: 231 /* Silently don't do anything */ 232 return; 233 default: 234 dev_err(&pcs->dev, "Invalid PCS speed %d\n", speed); 235 return; 236 } 237 if_mode |= IF_MODE_SPEED(sgmii_speed); 238 239 mdiodev_modify(pcs, IF_MODE, 240 IF_MODE_HALF_DUPLEX | IF_MODE_SPEED_MSK, 241 if_mode); 242 } 243 244 /* 2500Base-X is SerDes protocol 7 on Felix and 6 on ENETC. It is a SerDes lane 245 * clocked at 3.125 GHz which encodes symbols with 8b/10b and does not have 246 * auto-negotiation of any link parameters. Electrically it is compatible with 247 * a single lane of XAUI. 248 * The hardware reference manual wants to call this mode SGMII, but it isn't 249 * really, since the fundamental features of SGMII: 250 * - Downgrading the link speed by duplicating symbols 251 * - Auto-negotiation 252 * are not there. 253 * The speed is configured at 1000 in the IF_MODE because the clock frequency 254 * is actually given by a PLL configured in the Reset Configuration Word (RCW). 255 * Since there is no difference between fixed speed SGMII w/o AN and 802.3z w/o 256 * AN, we call this PHY interface type 2500Base-X. In case a PHY negotiates a 257 * lower link speed on line side, the system-side interface remains fixed at 258 * 2500 Mbps and we do rate adaptation through pause frames. 259 */ 260 static void lynx_pcs_link_up_2500basex(struct mdio_device *pcs, 261 unsigned int mode, 262 int speed, int duplex) 263 { 264 u16 if_mode = 0; 265 266 if (mode == MLO_AN_INBAND) { 267 dev_err(&pcs->dev, "AN not supported for 2500BaseX\n"); 268 return; 269 } 270 271 if (duplex == DUPLEX_HALF) 272 if_mode |= IF_MODE_HALF_DUPLEX; 273 if_mode |= IF_MODE_SPEED(SGMII_SPEED_2500); 274 275 mdiodev_modify(pcs, IF_MODE, 276 IF_MODE_HALF_DUPLEX | IF_MODE_SPEED_MSK, 277 if_mode); 278 } 279 280 static void lynx_pcs_link_up(struct phylink_pcs *pcs, unsigned int mode, 281 phy_interface_t interface, 282 int speed, int duplex) 283 { 284 struct lynx_pcs *lynx = phylink_pcs_to_lynx(pcs); 285 286 switch (interface) { 287 case PHY_INTERFACE_MODE_SGMII: 288 case PHY_INTERFACE_MODE_QSGMII: 289 lynx_pcs_link_up_sgmii(lynx->mdio, mode, speed, duplex); 290 break; 291 case PHY_INTERFACE_MODE_2500BASEX: 292 lynx_pcs_link_up_2500basex(lynx->mdio, mode, speed, duplex); 293 break; 294 case PHY_INTERFACE_MODE_USXGMII: 295 /* At the moment, only in-band AN is supported for USXGMII 296 * so nothing to do in link_up 297 */ 298 break; 299 default: 300 break; 301 } 302 } 303 304 static const struct phylink_pcs_ops lynx_pcs_phylink_ops = { 305 .pcs_get_state = lynx_pcs_get_state, 306 .pcs_config = lynx_pcs_config, 307 .pcs_an_restart = lynx_pcs_an_restart, 308 .pcs_link_up = lynx_pcs_link_up, 309 }; 310 311 static struct phylink_pcs *lynx_pcs_create(struct mdio_device *mdio) 312 { 313 struct lynx_pcs *lynx; 314 315 lynx = kzalloc(sizeof(*lynx), GFP_KERNEL); 316 if (!lynx) 317 return ERR_PTR(-ENOMEM); 318 319 mdio_device_get(mdio); 320 lynx->mdio = mdio; 321 lynx->pcs.ops = &lynx_pcs_phylink_ops; 322 lynx->pcs.poll = true; 323 324 return lynx_to_phylink_pcs(lynx); 325 } 326 327 struct phylink_pcs *lynx_pcs_create_mdiodev(struct mii_bus *bus, int addr) 328 { 329 struct mdio_device *mdio; 330 struct phylink_pcs *pcs; 331 332 mdio = mdio_device_create(bus, addr); 333 if (IS_ERR(mdio)) 334 return ERR_CAST(mdio); 335 336 pcs = lynx_pcs_create(mdio); 337 338 /* lynx_create() has taken a refcount on the mdiodev if it was 339 * successful. If lynx_create() fails, this will free the mdio 340 * device here. In any case, we don't need to hold our reference 341 * anymore, and putting it here will allow mdio_device_put() in 342 * lynx_destroy() to automatically free the mdio device. 343 */ 344 mdio_device_put(mdio); 345 346 return pcs; 347 } 348 EXPORT_SYMBOL(lynx_pcs_create_mdiodev); 349 350 /* 351 * lynx_pcs_create_fwnode() creates a lynx PCS instance from the fwnode 352 * device indicated by node. 353 * 354 * Returns: 355 * -ENODEV if the fwnode is marked unavailable 356 * -EPROBE_DEFER if we fail to find the device 357 * -ENOMEM if we fail to allocate memory 358 * pointer to a phylink_pcs on success 359 */ 360 struct phylink_pcs *lynx_pcs_create_fwnode(struct fwnode_handle *node) 361 { 362 struct mdio_device *mdio; 363 struct phylink_pcs *pcs; 364 365 if (!fwnode_device_is_available(node)) 366 return ERR_PTR(-ENODEV); 367 368 mdio = fwnode_mdio_find_device(node); 369 if (!mdio) 370 return ERR_PTR(-EPROBE_DEFER); 371 372 pcs = lynx_pcs_create(mdio); 373 374 /* lynx_create() has taken a refcount on the mdiodev if it was 375 * successful. If lynx_create() fails, this will free the mdio 376 * device here. In any case, we don't need to hold our reference 377 * anymore, and putting it here will allow mdio_device_put() in 378 * lynx_destroy() to automatically free the mdio device. 379 */ 380 mdio_device_put(mdio); 381 382 return pcs; 383 } 384 EXPORT_SYMBOL_GPL(lynx_pcs_create_fwnode); 385 386 void lynx_pcs_destroy(struct phylink_pcs *pcs) 387 { 388 struct lynx_pcs *lynx = phylink_pcs_to_lynx(pcs); 389 390 mdio_device_put(lynx->mdio); 391 kfree(lynx); 392 } 393 EXPORT_SYMBOL(lynx_pcs_destroy); 394 395 MODULE_LICENSE("Dual BSD/GPL"); 396