1 // SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause)
3 * Lynx PCS MDIO helpers
6 #include <linux/mdio.h>
7 #include <linux/phylink.h>
8 #include <linux/pcs-lynx.h>
9 #include <linux/property.h>
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))
14 #define LINK_TIMER_LO 0x12
15 #define LINK_TIMER_HI 0x13
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)
24 struct phylink_pcs pcs;
25 struct mdio_device *mdio;
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)
38 static void lynx_pcs_get_state_usxgmii(struct mdio_device *pcs,
39 struct phylink_link_state *state)
41 struct mii_bus *bus = pcs->bus;
45 status = mdiobus_c45_read(bus, addr, MDIO_MMD_VEND2, MII_BMSR);
49 state->link = !!(status & MDIO_STAT1_LSTATUS);
50 state->an_complete = !!(status & MDIO_AN_STAT1_COMPLETE);
51 if (!state->link || !state->an_complete)
54 lpa = mdiobus_c45_read(bus, addr, MDIO_MMD_VEND2, MII_LPA);
58 phylink_decode_usxgmii_word(state, lpa);
61 static void lynx_pcs_get_state_2500basex(struct mdio_device *pcs,
62 struct phylink_link_state *state)
66 bmsr = mdiodev_read(pcs, MII_BMSR);
67 lpa = mdiodev_read(pcs, MII_LPA);
68 if (bmsr < 0 || lpa < 0) {
73 state->link = !!(bmsr & BMSR_LSTATUS);
74 state->an_complete = !!(bmsr & BMSR_ANEGCOMPLETE);
78 state->speed = SPEED_2500;
79 state->pause |= MLO_PAUSE_TX | MLO_PAUSE_RX;
80 state->duplex = DUPLEX_FULL;
83 static void lynx_pcs_get_state(struct phylink_pcs *pcs,
84 struct phylink_link_state *state)
86 struct lynx_pcs *lynx = phylink_pcs_to_lynx(pcs);
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);
94 case PHY_INTERFACE_MODE_2500BASEX:
95 lynx_pcs_get_state_2500basex(lynx->mdio, state);
97 case PHY_INTERFACE_MODE_USXGMII:
98 lynx_pcs_get_state_usxgmii(lynx->mdio, state);
100 case PHY_INTERFACE_MODE_10GBASER:
101 phylink_mii_c45_pcs_get_state(lynx->mdio, state);
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);
115 static int lynx_pcs_config_giga(struct mdio_device *pcs,
116 phy_interface_t interface,
117 const unsigned long *advertising,
118 unsigned int neg_mode)
125 link_timer_ns = phylink_get_link_timer_ns(interface);
126 if (link_timer_ns > 0) {
127 link_timer = LINK_TIMER_VAL(link_timer_ns);
129 mdiodev_write(pcs, LINK_TIMER_LO, link_timer & 0xffff);
130 mdiodev_write(pcs, LINK_TIMER_HI, link_timer >> 16);
133 if (interface == PHY_INTERFACE_MODE_1000BASEX) {
136 /* SGMII and QSGMII */
137 if_mode = IF_MODE_SGMII_EN;
138 if (neg_mode == PHYLINK_PCS_NEG_INBAND_ENABLED)
139 if_mode |= IF_MODE_USE_SGMII_AN;
142 err = mdiodev_modify(pcs, IF_MODE,
143 IF_MODE_SGMII_EN | IF_MODE_USE_SGMII_AN,
148 return phylink_mii_c22_pcs_config(pcs, interface, advertising,
152 static int lynx_pcs_config_usxgmii(struct mdio_device *pcs,
153 const unsigned long *advertising,
154 unsigned int neg_mode)
156 struct mii_bus *bus = pcs->bus;
157 int addr = pcs->addr;
159 if (neg_mode != PHYLINK_PCS_NEG_INBAND_ENABLED) {
160 dev_err(&pcs->dev, "USXGMII only supports in-band AN for now\n");
164 /* Configure device ability for the USXGMII Replicator */
165 return mdiobus_c45_write(bus, addr, MDIO_MMD_VEND2, MII_ADVERTISE,
166 MDIO_USXGMII_10G | MDIO_USXGMII_LINK |
167 MDIO_USXGMII_FULL_DUPLEX |
168 ADVERTISE_SGMII | ADVERTISE_LPACK);
171 static int lynx_pcs_config(struct phylink_pcs *pcs, unsigned int neg_mode,
172 phy_interface_t ifmode,
173 const unsigned long *advertising, bool permit)
175 struct lynx_pcs *lynx = phylink_pcs_to_lynx(pcs);
178 case PHY_INTERFACE_MODE_1000BASEX:
179 case PHY_INTERFACE_MODE_SGMII:
180 case PHY_INTERFACE_MODE_QSGMII:
181 return lynx_pcs_config_giga(lynx->mdio, ifmode, advertising,
183 case PHY_INTERFACE_MODE_2500BASEX:
184 if (neg_mode == PHYLINK_PCS_NEG_INBAND_ENABLED) {
185 dev_err(&lynx->mdio->dev,
186 "AN not supported on 3.125GHz SerDes lane\n");
190 case PHY_INTERFACE_MODE_USXGMII:
191 return lynx_pcs_config_usxgmii(lynx->mdio, advertising,
193 case PHY_INTERFACE_MODE_10GBASER:
194 /* Nothing to do here for 10GBASER */
203 static void lynx_pcs_an_restart(struct phylink_pcs *pcs)
205 struct lynx_pcs *lynx = phylink_pcs_to_lynx(pcs);
207 phylink_mii_c22_pcs_an_restart(lynx->mdio);
210 static void lynx_pcs_link_up_sgmii(struct mdio_device *pcs,
211 unsigned int neg_mode,
212 int speed, int duplex)
214 u16 if_mode = 0, sgmii_speed;
216 /* The PCS needs to be configured manually only
217 * when not operating on in-band mode
219 if (neg_mode == PHYLINK_PCS_NEG_INBAND_ENABLED)
222 if (duplex == DUPLEX_HALF)
223 if_mode |= IF_MODE_HALF_DUPLEX;
227 sgmii_speed = SGMII_SPEED_1000;
230 sgmii_speed = SGMII_SPEED_100;
233 sgmii_speed = SGMII_SPEED_10;
236 /* Silently don't do anything */
239 dev_err(&pcs->dev, "Invalid PCS speed %d\n", speed);
242 if_mode |= IF_MODE_SPEED(sgmii_speed);
244 mdiodev_modify(pcs, IF_MODE,
245 IF_MODE_HALF_DUPLEX | IF_MODE_SPEED_MSK,
249 /* 2500Base-X is SerDes protocol 7 on Felix and 6 on ENETC. It is a SerDes lane
250 * clocked at 3.125 GHz which encodes symbols with 8b/10b and does not have
251 * auto-negotiation of any link parameters. Electrically it is compatible with
252 * a single lane of XAUI.
253 * The hardware reference manual wants to call this mode SGMII, but it isn't
254 * really, since the fundamental features of SGMII:
255 * - Downgrading the link speed by duplicating symbols
258 * The speed is configured at 1000 in the IF_MODE because the clock frequency
259 * is actually given by a PLL configured in the Reset Configuration Word (RCW).
260 * Since there is no difference between fixed speed SGMII w/o AN and 802.3z w/o
261 * AN, we call this PHY interface type 2500Base-X. In case a PHY negotiates a
262 * lower link speed on line side, the system-side interface remains fixed at
263 * 2500 Mbps and we do rate adaptation through pause frames.
265 static void lynx_pcs_link_up_2500basex(struct mdio_device *pcs,
266 unsigned int neg_mode,
267 int speed, int duplex)
271 if (neg_mode == PHYLINK_PCS_NEG_INBAND_ENABLED) {
272 dev_err(&pcs->dev, "AN not supported for 2500BaseX\n");
276 if (duplex == DUPLEX_HALF)
277 if_mode |= IF_MODE_HALF_DUPLEX;
278 if_mode |= IF_MODE_SPEED(SGMII_SPEED_2500);
280 mdiodev_modify(pcs, IF_MODE,
281 IF_MODE_HALF_DUPLEX | IF_MODE_SPEED_MSK,
285 static void lynx_pcs_link_up(struct phylink_pcs *pcs, unsigned int neg_mode,
286 phy_interface_t interface,
287 int speed, int duplex)
289 struct lynx_pcs *lynx = phylink_pcs_to_lynx(pcs);
292 case PHY_INTERFACE_MODE_SGMII:
293 case PHY_INTERFACE_MODE_QSGMII:
294 lynx_pcs_link_up_sgmii(lynx->mdio, neg_mode, speed, duplex);
296 case PHY_INTERFACE_MODE_2500BASEX:
297 lynx_pcs_link_up_2500basex(lynx->mdio, neg_mode, speed, duplex);
299 case PHY_INTERFACE_MODE_USXGMII:
300 /* At the moment, only in-band AN is supported for USXGMII
301 * so nothing to do in link_up
309 static const struct phylink_pcs_ops lynx_pcs_phylink_ops = {
310 .pcs_get_state = lynx_pcs_get_state,
311 .pcs_config = lynx_pcs_config,
312 .pcs_an_restart = lynx_pcs_an_restart,
313 .pcs_link_up = lynx_pcs_link_up,
316 static struct phylink_pcs *lynx_pcs_create(struct mdio_device *mdio)
318 struct lynx_pcs *lynx;
320 lynx = kzalloc(sizeof(*lynx), GFP_KERNEL);
322 return ERR_PTR(-ENOMEM);
324 mdio_device_get(mdio);
326 lynx->pcs.ops = &lynx_pcs_phylink_ops;
327 lynx->pcs.neg_mode = true;
328 lynx->pcs.poll = true;
330 return lynx_to_phylink_pcs(lynx);
333 struct phylink_pcs *lynx_pcs_create_mdiodev(struct mii_bus *bus, int addr)
335 struct mdio_device *mdio;
336 struct phylink_pcs *pcs;
338 mdio = mdio_device_create(bus, addr);
340 return ERR_CAST(mdio);
342 pcs = lynx_pcs_create(mdio);
344 /* lynx_create() has taken a refcount on the mdiodev if it was
345 * successful. If lynx_create() fails, this will free the mdio
346 * device here. In any case, we don't need to hold our reference
347 * anymore, and putting it here will allow mdio_device_put() in
348 * lynx_destroy() to automatically free the mdio device.
350 mdio_device_put(mdio);
354 EXPORT_SYMBOL(lynx_pcs_create_mdiodev);
357 * lynx_pcs_create_fwnode() creates a lynx PCS instance from the fwnode
358 * device indicated by node.
361 * -ENODEV if the fwnode is marked unavailable
362 * -EPROBE_DEFER if we fail to find the device
363 * -ENOMEM if we fail to allocate memory
364 * pointer to a phylink_pcs on success
366 struct phylink_pcs *lynx_pcs_create_fwnode(struct fwnode_handle *node)
368 struct mdio_device *mdio;
369 struct phylink_pcs *pcs;
371 if (!fwnode_device_is_available(node))
372 return ERR_PTR(-ENODEV);
374 mdio = fwnode_mdio_find_device(node);
376 return ERR_PTR(-EPROBE_DEFER);
378 pcs = lynx_pcs_create(mdio);
380 /* lynx_create() has taken a refcount on the mdiodev if it was
381 * successful. If lynx_create() fails, this will free the mdio
382 * device here. In any case, we don't need to hold our reference
383 * anymore, and putting it here will allow mdio_device_put() in
384 * lynx_destroy() to automatically free the mdio device.
386 mdio_device_put(mdio);
390 EXPORT_SYMBOL_GPL(lynx_pcs_create_fwnode);
392 void lynx_pcs_destroy(struct phylink_pcs *pcs)
394 struct lynx_pcs *lynx = phylink_pcs_to_lynx(pcs);
396 mdio_device_put(lynx->mdio);
399 EXPORT_SYMBOL(lynx_pcs_destroy);
401 MODULE_LICENSE("Dual BSD/GPL");