10da4c3d3SIoana Ciornei // SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause)
20da4c3d3SIoana Ciornei /* Copyright 2020 NXP
30da4c3d3SIoana Ciornei * Lynx PCS MDIO helpers
40da4c3d3SIoana Ciornei */
50da4c3d3SIoana Ciornei
60da4c3d3SIoana Ciornei #include <linux/mdio.h>
70da4c3d3SIoana Ciornei #include <linux/phylink.h>
80da4c3d3SIoana Ciornei #include <linux/pcs-lynx.h>
9d143898cSRussell King (Oracle) #include <linux/property.h>
100da4c3d3SIoana Ciornei
110da4c3d3SIoana Ciornei #define SGMII_CLOCK_PERIOD_NS 8 /* PCS is clocked at 125 MHz */
120da4c3d3SIoana Ciornei #define LINK_TIMER_VAL(ns) ((u32)((ns) / SGMII_CLOCK_PERIOD_NS))
130da4c3d3SIoana Ciornei
140da4c3d3SIoana Ciornei #define LINK_TIMER_LO 0x12
150da4c3d3SIoana Ciornei #define LINK_TIMER_HI 0x13
160da4c3d3SIoana Ciornei #define IF_MODE 0x14
170da4c3d3SIoana Ciornei #define IF_MODE_SGMII_EN BIT(0)
180da4c3d3SIoana Ciornei #define IF_MODE_USE_SGMII_AN BIT(1)
190da4c3d3SIoana Ciornei #define IF_MODE_SPEED(x) (((x) << 2) & GENMASK(3, 2))
200da4c3d3SIoana Ciornei #define IF_MODE_SPEED_MSK GENMASK(3, 2)
210da4c3d3SIoana Ciornei #define IF_MODE_HALF_DUPLEX BIT(4)
220da4c3d3SIoana Ciornei
23e7026f15SColin Foster struct lynx_pcs {
24e7026f15SColin Foster struct phylink_pcs pcs;
25e7026f15SColin Foster struct mdio_device *mdio;
26e7026f15SColin Foster };
27e7026f15SColin Foster
280da4c3d3SIoana Ciornei enum sgmii_speed {
290da4c3d3SIoana Ciornei SGMII_SPEED_10 = 0,
300da4c3d3SIoana Ciornei SGMII_SPEED_100 = 1,
310da4c3d3SIoana Ciornei SGMII_SPEED_1000 = 2,
320da4c3d3SIoana Ciornei SGMII_SPEED_2500 = 2,
330da4c3d3SIoana Ciornei };
340da4c3d3SIoana Ciornei
350da4c3d3SIoana Ciornei #define phylink_pcs_to_lynx(pl_pcs) container_of((pl_pcs), struct lynx_pcs, pcs)
36e7026f15SColin Foster #define lynx_to_phylink_pcs(lynx) (&(lynx)->pcs)
37e7026f15SColin Foster
lynx_pcs_get_state_usxgmii(struct mdio_device * pcs,struct phylink_link_state * state)380da4c3d3SIoana Ciornei static void lynx_pcs_get_state_usxgmii(struct mdio_device *pcs,
390da4c3d3SIoana Ciornei struct phylink_link_state *state)
400da4c3d3SIoana Ciornei {
410da4c3d3SIoana Ciornei struct mii_bus *bus = pcs->bus;
420da4c3d3SIoana Ciornei int addr = pcs->addr;
430da4c3d3SIoana Ciornei int status, lpa;
440da4c3d3SIoana Ciornei
450da4c3d3SIoana Ciornei status = mdiobus_c45_read(bus, addr, MDIO_MMD_VEND2, MII_BMSR);
460da4c3d3SIoana Ciornei if (status < 0)
470da4c3d3SIoana Ciornei return;
480da4c3d3SIoana Ciornei
490da4c3d3SIoana Ciornei state->link = !!(status & MDIO_STAT1_LSTATUS);
500da4c3d3SIoana Ciornei state->an_complete = !!(status & MDIO_AN_STAT1_COMPLETE);
510da4c3d3SIoana Ciornei if (!state->link || !state->an_complete)
520da4c3d3SIoana Ciornei return;
530da4c3d3SIoana Ciornei
540da4c3d3SIoana Ciornei lpa = mdiobus_c45_read(bus, addr, MDIO_MMD_VEND2, MII_LPA);
550da4c3d3SIoana Ciornei if (lpa < 0)
560da4c3d3SIoana Ciornei return;
570da4c3d3SIoana Ciornei
580da4c3d3SIoana Ciornei phylink_decode_usxgmii_word(state, lpa);
590da4c3d3SIoana Ciornei }
600da4c3d3SIoana Ciornei
lynx_pcs_get_state_2500basex(struct mdio_device * pcs,struct phylink_link_state * state)610da4c3d3SIoana Ciornei static void lynx_pcs_get_state_2500basex(struct mdio_device *pcs,
620da4c3d3SIoana Ciornei struct phylink_link_state *state)
630da4c3d3SIoana Ciornei {
640da4c3d3SIoana Ciornei int bmsr, lpa;
650da4c3d3SIoana Ciornei
66a8236dfdSRussell King (Oracle) bmsr = mdiodev_read(pcs, MII_BMSR);
67a8236dfdSRussell King (Oracle) lpa = mdiodev_read(pcs, MII_LPA);
680da4c3d3SIoana Ciornei if (bmsr < 0 || lpa < 0) {
690da4c3d3SIoana Ciornei state->link = false;
700da4c3d3SIoana Ciornei return;
710da4c3d3SIoana Ciornei }
720da4c3d3SIoana Ciornei
730da4c3d3SIoana Ciornei state->link = !!(bmsr & BMSR_LSTATUS);
740da4c3d3SIoana Ciornei state->an_complete = !!(bmsr & BMSR_ANEGCOMPLETE);
750da4c3d3SIoana Ciornei if (!state->link)
760da4c3d3SIoana Ciornei return;
770da4c3d3SIoana Ciornei
780da4c3d3SIoana Ciornei state->speed = SPEED_2500;
790da4c3d3SIoana Ciornei state->pause |= MLO_PAUSE_TX | MLO_PAUSE_RX;
800da4c3d3SIoana Ciornei state->duplex = DUPLEX_FULL;
810da4c3d3SIoana Ciornei }
820da4c3d3SIoana Ciornei
lynx_pcs_get_state(struct phylink_pcs * pcs,struct phylink_link_state * state)830da4c3d3SIoana Ciornei static void lynx_pcs_get_state(struct phylink_pcs *pcs,
840da4c3d3SIoana Ciornei struct phylink_link_state *state)
850da4c3d3SIoana Ciornei {
860da4c3d3SIoana Ciornei struct lynx_pcs *lynx = phylink_pcs_to_lynx(pcs);
870da4c3d3SIoana Ciornei
880da4c3d3SIoana Ciornei switch (state->interface) {
89694a0006SRussell King case PHY_INTERFACE_MODE_1000BASEX:
900da4c3d3SIoana Ciornei case PHY_INTERFACE_MODE_SGMII:
910da4c3d3SIoana Ciornei case PHY_INTERFACE_MODE_QSGMII:
920da4c3d3SIoana Ciornei phylink_mii_c22_pcs_get_state(lynx->mdio, state);
930da4c3d3SIoana Ciornei break;
940da4c3d3SIoana Ciornei case PHY_INTERFACE_MODE_2500BASEX:
950da4c3d3SIoana Ciornei lynx_pcs_get_state_2500basex(lynx->mdio, state);
960da4c3d3SIoana Ciornei break;
970da4c3d3SIoana Ciornei case PHY_INTERFACE_MODE_USXGMII:
980da4c3d3SIoana Ciornei lynx_pcs_get_state_usxgmii(lynx->mdio, state);
990da4c3d3SIoana Ciornei break;
100e7e95c90SIoana Ciornei case PHY_INTERFACE_MODE_10GBASER:
101e7e95c90SIoana Ciornei phylink_mii_c45_pcs_get_state(lynx->mdio, state);
102e7e95c90SIoana Ciornei break;
1030da4c3d3SIoana Ciornei default:
1040da4c3d3SIoana Ciornei break;
1050da4c3d3SIoana Ciornei }
1060da4c3d3SIoana Ciornei
1070da4c3d3SIoana Ciornei dev_dbg(&lynx->mdio->dev,
108ecec0ebbSRussell King (Oracle) "mode=%s/%s/%s link=%u an_complete=%u\n",
1090da4c3d3SIoana Ciornei phy_modes(state->interface),
1100da4c3d3SIoana Ciornei phy_speed_to_str(state->speed),
1110da4c3d3SIoana Ciornei phy_duplex_to_str(state->duplex),
112ecec0ebbSRussell King (Oracle) state->link, state->an_complete);
1130da4c3d3SIoana Ciornei }
1140da4c3d3SIoana Ciornei
lynx_pcs_config_giga(struct mdio_device * pcs,phy_interface_t interface,const unsigned long * advertising,unsigned int neg_mode)115febf2aafSRussell King (Oracle) static int lynx_pcs_config_giga(struct mdio_device *pcs,
11606f9a614SRussell King (Oracle) phy_interface_t interface,
117febf2aafSRussell King (Oracle) const unsigned long *advertising,
118febf2aafSRussell King (Oracle) unsigned int neg_mode)
119694a0006SRussell King {
120e2a95750SRussell King (Oracle) int link_timer_ns;
121694a0006SRussell King u32 link_timer;
12206f9a614SRussell King (Oracle) u16 if_mode;
123694a0006SRussell King int err;
124694a0006SRussell King
125e2a95750SRussell King (Oracle) link_timer_ns = phylink_get_link_timer_ns(interface);
126e2a95750SRussell King (Oracle) if (link_timer_ns > 0) {
127e2a95750SRussell King (Oracle) link_timer = LINK_TIMER_VAL(link_timer_ns);
128694a0006SRussell King
129a8236dfdSRussell King (Oracle) mdiodev_write(pcs, LINK_TIMER_LO, link_timer & 0xffff);
130a8236dfdSRussell King (Oracle) mdiodev_write(pcs, LINK_TIMER_HI, link_timer >> 16);
1310da4c3d3SIoana Ciornei }
132e2a95750SRussell King (Oracle)
133e2a95750SRussell King (Oracle) if (interface == PHY_INTERFACE_MODE_1000BASEX) {
134e2a95750SRussell King (Oracle) if_mode = 0;
135e2a95750SRussell King (Oracle) } else {
136febf2aafSRussell King (Oracle) /* SGMII and QSGMII */
137e2a95750SRussell King (Oracle) if_mode = IF_MODE_SGMII_EN;
138febf2aafSRussell King (Oracle) if (neg_mode == PHYLINK_PCS_NEG_INBAND_ENABLED)
139e2a95750SRussell King (Oracle) if_mode |= IF_MODE_USE_SGMII_AN;
14006f9a614SRussell King (Oracle) }
14106f9a614SRussell King (Oracle)
142a8236dfdSRussell King (Oracle) err = mdiodev_modify(pcs, IF_MODE,
1430da4c3d3SIoana Ciornei IF_MODE_SGMII_EN | IF_MODE_USE_SGMII_AN,
1440da4c3d3SIoana Ciornei if_mode);
1450da4c3d3SIoana Ciornei if (err)
1460da4c3d3SIoana Ciornei return err;
1470da4c3d3SIoana Ciornei
148febf2aafSRussell King (Oracle) return phylink_mii_c22_pcs_config(pcs, interface, advertising,
149febf2aafSRussell King (Oracle) neg_mode);
1500da4c3d3SIoana Ciornei }
1510da4c3d3SIoana Ciornei
lynx_pcs_config_usxgmii(struct mdio_device * pcs,const unsigned long * advertising,unsigned int neg_mode)152c689a652SRussell King (Oracle) static int lynx_pcs_config_usxgmii(struct mdio_device *pcs,
153c689a652SRussell King (Oracle) const unsigned long *advertising,
154c689a652SRussell King (Oracle) unsigned int neg_mode)
1550da4c3d3SIoana Ciornei {
1560da4c3d3SIoana Ciornei struct mii_bus *bus = pcs->bus;
1570da4c3d3SIoana Ciornei int addr = pcs->addr;
1580da4c3d3SIoana Ciornei
159c689a652SRussell King (Oracle) if (neg_mode != PHYLINK_PCS_NEG_INBAND_ENABLED) {
1600da4c3d3SIoana Ciornei dev_err(&pcs->dev, "USXGMII only supports in-band AN for now\n");
1610da4c3d3SIoana Ciornei return -EOPNOTSUPP;
1620da4c3d3SIoana Ciornei }
1630da4c3d3SIoana Ciornei
1640da4c3d3SIoana Ciornei /* Configure device ability for the USXGMII Replicator */
1650da4c3d3SIoana Ciornei return mdiobus_c45_write(bus, addr, MDIO_MMD_VEND2, MII_ADVERTISE,
1660da4c3d3SIoana Ciornei MDIO_USXGMII_10G | MDIO_USXGMII_LINK |
1670da4c3d3SIoana Ciornei MDIO_USXGMII_FULL_DUPLEX |
1680da4c3d3SIoana Ciornei ADVERTISE_SGMII | ADVERTISE_LPACK);
1690da4c3d3SIoana Ciornei }
1700da4c3d3SIoana Ciornei
lynx_pcs_config(struct phylink_pcs * pcs,unsigned int neg_mode,phy_interface_t ifmode,const unsigned long * advertising,bool permit)171c689a652SRussell King (Oracle) static int lynx_pcs_config(struct phylink_pcs *pcs, unsigned int neg_mode,
1720da4c3d3SIoana Ciornei phy_interface_t ifmode,
173c689a652SRussell King (Oracle) const unsigned long *advertising, bool permit)
1740da4c3d3SIoana Ciornei {
1750da4c3d3SIoana Ciornei struct lynx_pcs *lynx = phylink_pcs_to_lynx(pcs);
1760da4c3d3SIoana Ciornei
1770da4c3d3SIoana Ciornei switch (ifmode) {
178694a0006SRussell King case PHY_INTERFACE_MODE_1000BASEX:
1790da4c3d3SIoana Ciornei case PHY_INTERFACE_MODE_SGMII:
1800da4c3d3SIoana Ciornei case PHY_INTERFACE_MODE_QSGMII:
181febf2aafSRussell King (Oracle) return lynx_pcs_config_giga(lynx->mdio, ifmode, advertising,
182febf2aafSRussell King (Oracle) neg_mode);
1830da4c3d3SIoana Ciornei case PHY_INTERFACE_MODE_2500BASEX:
184c689a652SRussell King (Oracle) if (neg_mode == PHYLINK_PCS_NEG_INBAND_ENABLED) {
1850da4c3d3SIoana Ciornei dev_err(&lynx->mdio->dev,
1860da4c3d3SIoana Ciornei "AN not supported on 3.125GHz SerDes lane\n");
1870da4c3d3SIoana Ciornei return -EOPNOTSUPP;
1880da4c3d3SIoana Ciornei }
1890da4c3d3SIoana Ciornei break;
1900da4c3d3SIoana Ciornei case PHY_INTERFACE_MODE_USXGMII:
191c689a652SRussell King (Oracle) return lynx_pcs_config_usxgmii(lynx->mdio, advertising,
192c689a652SRussell King (Oracle) neg_mode);
193e7e95c90SIoana Ciornei case PHY_INTERFACE_MODE_10GBASER:
194e7e95c90SIoana Ciornei /* Nothing to do here for 10GBASER */
195e7e95c90SIoana Ciornei break;
1960da4c3d3SIoana Ciornei default:
1970da4c3d3SIoana Ciornei return -EOPNOTSUPP;
1980da4c3d3SIoana Ciornei }
1990da4c3d3SIoana Ciornei
2000da4c3d3SIoana Ciornei return 0;
2010da4c3d3SIoana Ciornei }
2020da4c3d3SIoana Ciornei
lynx_pcs_an_restart(struct phylink_pcs * pcs)203694a0006SRussell King static void lynx_pcs_an_restart(struct phylink_pcs *pcs)
204694a0006SRussell King {
205694a0006SRussell King struct lynx_pcs *lynx = phylink_pcs_to_lynx(pcs);
206694a0006SRussell King
207694a0006SRussell King phylink_mii_c22_pcs_an_restart(lynx->mdio);
208694a0006SRussell King }
209694a0006SRussell King
lynx_pcs_link_up_sgmii(struct mdio_device * pcs,unsigned int neg_mode,int speed,int duplex)210c689a652SRussell King (Oracle) static void lynx_pcs_link_up_sgmii(struct mdio_device *pcs,
211c689a652SRussell King (Oracle) unsigned int neg_mode,
2120da4c3d3SIoana Ciornei int speed, int duplex)
2130da4c3d3SIoana Ciornei {
2140da4c3d3SIoana Ciornei u16 if_mode = 0, sgmii_speed;
2150da4c3d3SIoana Ciornei
2160da4c3d3SIoana Ciornei /* The PCS needs to be configured manually only
2170da4c3d3SIoana Ciornei * when not operating on in-band mode
2180da4c3d3SIoana Ciornei */
219*2f4503f9SVladimir Oltean if (neg_mode == PHYLINK_PCS_NEG_INBAND_ENABLED)
2200da4c3d3SIoana Ciornei return;
2210da4c3d3SIoana Ciornei
2220da4c3d3SIoana Ciornei if (duplex == DUPLEX_HALF)
2230da4c3d3SIoana Ciornei if_mode |= IF_MODE_HALF_DUPLEX;
2240da4c3d3SIoana Ciornei
2250da4c3d3SIoana Ciornei switch (speed) {
2260da4c3d3SIoana Ciornei case SPEED_1000:
2270da4c3d3SIoana Ciornei sgmii_speed = SGMII_SPEED_1000;
2280da4c3d3SIoana Ciornei break;
2290da4c3d3SIoana Ciornei case SPEED_100:
2300da4c3d3SIoana Ciornei sgmii_speed = SGMII_SPEED_100;
2310da4c3d3SIoana Ciornei break;
2320da4c3d3SIoana Ciornei case SPEED_10:
2330da4c3d3SIoana Ciornei sgmii_speed = SGMII_SPEED_10;
2340da4c3d3SIoana Ciornei break;
2350da4c3d3SIoana Ciornei case SPEED_UNKNOWN:
2360da4c3d3SIoana Ciornei /* Silently don't do anything */
2370da4c3d3SIoana Ciornei return;
2380da4c3d3SIoana Ciornei default:
2390da4c3d3SIoana Ciornei dev_err(&pcs->dev, "Invalid PCS speed %d\n", speed);
2400da4c3d3SIoana Ciornei return;
2410da4c3d3SIoana Ciornei }
2420da4c3d3SIoana Ciornei if_mode |= IF_MODE_SPEED(sgmii_speed);
2430da4c3d3SIoana Ciornei
244a8236dfdSRussell King (Oracle) mdiodev_modify(pcs, IF_MODE,
2450da4c3d3SIoana Ciornei IF_MODE_HALF_DUPLEX | IF_MODE_SPEED_MSK,
2460da4c3d3SIoana Ciornei if_mode);
2470da4c3d3SIoana Ciornei }
2480da4c3d3SIoana Ciornei
2490da4c3d3SIoana Ciornei /* 2500Base-X is SerDes protocol 7 on Felix and 6 on ENETC. It is a SerDes lane
2500da4c3d3SIoana Ciornei * clocked at 3.125 GHz which encodes symbols with 8b/10b and does not have
2510da4c3d3SIoana Ciornei * auto-negotiation of any link parameters. Electrically it is compatible with
2520da4c3d3SIoana Ciornei * a single lane of XAUI.
2530da4c3d3SIoana Ciornei * The hardware reference manual wants to call this mode SGMII, but it isn't
2540da4c3d3SIoana Ciornei * really, since the fundamental features of SGMII:
2550da4c3d3SIoana Ciornei * - Downgrading the link speed by duplicating symbols
2560da4c3d3SIoana Ciornei * - Auto-negotiation
2570da4c3d3SIoana Ciornei * are not there.
2580da4c3d3SIoana Ciornei * The speed is configured at 1000 in the IF_MODE because the clock frequency
2590da4c3d3SIoana Ciornei * is actually given by a PLL configured in the Reset Configuration Word (RCW).
2600da4c3d3SIoana Ciornei * Since there is no difference between fixed speed SGMII w/o AN and 802.3z w/o
2610da4c3d3SIoana Ciornei * AN, we call this PHY interface type 2500Base-X. In case a PHY negotiates a
2620da4c3d3SIoana Ciornei * lower link speed on line side, the system-side interface remains fixed at
2630da4c3d3SIoana Ciornei * 2500 Mbps and we do rate adaptation through pause frames.
2640da4c3d3SIoana Ciornei */
lynx_pcs_link_up_2500basex(struct mdio_device * pcs,unsigned int neg_mode,int speed,int duplex)2650da4c3d3SIoana Ciornei static void lynx_pcs_link_up_2500basex(struct mdio_device *pcs,
266c689a652SRussell King (Oracle) unsigned int neg_mode,
2670da4c3d3SIoana Ciornei int speed, int duplex)
2680da4c3d3SIoana Ciornei {
2690da4c3d3SIoana Ciornei u16 if_mode = 0;
2700da4c3d3SIoana Ciornei
271c689a652SRussell King (Oracle) if (neg_mode == PHYLINK_PCS_NEG_INBAND_ENABLED) {
2720da4c3d3SIoana Ciornei dev_err(&pcs->dev, "AN not supported for 2500BaseX\n");
2730da4c3d3SIoana Ciornei return;
2740da4c3d3SIoana Ciornei }
2750da4c3d3SIoana Ciornei
2760da4c3d3SIoana Ciornei if (duplex == DUPLEX_HALF)
2770da4c3d3SIoana Ciornei if_mode |= IF_MODE_HALF_DUPLEX;
2780da4c3d3SIoana Ciornei if_mode |= IF_MODE_SPEED(SGMII_SPEED_2500);
2790da4c3d3SIoana Ciornei
280a8236dfdSRussell King (Oracle) mdiodev_modify(pcs, IF_MODE,
2810da4c3d3SIoana Ciornei IF_MODE_HALF_DUPLEX | IF_MODE_SPEED_MSK,
2820da4c3d3SIoana Ciornei if_mode);
2830da4c3d3SIoana Ciornei }
2840da4c3d3SIoana Ciornei
lynx_pcs_link_up(struct phylink_pcs * pcs,unsigned int neg_mode,phy_interface_t interface,int speed,int duplex)285c689a652SRussell King (Oracle) static void lynx_pcs_link_up(struct phylink_pcs *pcs, unsigned int neg_mode,
2860da4c3d3SIoana Ciornei phy_interface_t interface,
2870da4c3d3SIoana Ciornei int speed, int duplex)
2880da4c3d3SIoana Ciornei {
2890da4c3d3SIoana Ciornei struct lynx_pcs *lynx = phylink_pcs_to_lynx(pcs);
2900da4c3d3SIoana Ciornei
2910da4c3d3SIoana Ciornei switch (interface) {
2920da4c3d3SIoana Ciornei case PHY_INTERFACE_MODE_SGMII:
2930da4c3d3SIoana Ciornei case PHY_INTERFACE_MODE_QSGMII:
294c689a652SRussell King (Oracle) lynx_pcs_link_up_sgmii(lynx->mdio, neg_mode, speed, duplex);
2950da4c3d3SIoana Ciornei break;
2960da4c3d3SIoana Ciornei case PHY_INTERFACE_MODE_2500BASEX:
297c689a652SRussell King (Oracle) lynx_pcs_link_up_2500basex(lynx->mdio, neg_mode, speed, duplex);
2980da4c3d3SIoana Ciornei break;
2990da4c3d3SIoana Ciornei case PHY_INTERFACE_MODE_USXGMII:
3000da4c3d3SIoana Ciornei /* At the moment, only in-band AN is supported for USXGMII
3010da4c3d3SIoana Ciornei * so nothing to do in link_up
3020da4c3d3SIoana Ciornei */
3030da4c3d3SIoana Ciornei break;
3040da4c3d3SIoana Ciornei default:
3050da4c3d3SIoana Ciornei break;
3060da4c3d3SIoana Ciornei }
3070da4c3d3SIoana Ciornei }
3080da4c3d3SIoana Ciornei
3090da4c3d3SIoana Ciornei static const struct phylink_pcs_ops lynx_pcs_phylink_ops = {
3100da4c3d3SIoana Ciornei .pcs_get_state = lynx_pcs_get_state,
3110da4c3d3SIoana Ciornei .pcs_config = lynx_pcs_config,
312694a0006SRussell King .pcs_an_restart = lynx_pcs_an_restart,
3130da4c3d3SIoana Ciornei .pcs_link_up = lynx_pcs_link_up,
3140da4c3d3SIoana Ciornei };
3150da4c3d3SIoana Ciornei
lynx_pcs_create(struct mdio_device * mdio)31684e476b8SRussell King (Oracle) static struct phylink_pcs *lynx_pcs_create(struct mdio_device *mdio)
3170da4c3d3SIoana Ciornei {
3180699b3e0SColin Foster struct lynx_pcs *lynx;
3190da4c3d3SIoana Ciornei
3200699b3e0SColin Foster lynx = kzalloc(sizeof(*lynx), GFP_KERNEL);
3210699b3e0SColin Foster if (!lynx)
32205b606b8SRussell King (Oracle) return ERR_PTR(-ENOMEM);
3230da4c3d3SIoana Ciornei
32486b5f2d8SRussell King (Oracle) mdio_device_get(mdio);
3250699b3e0SColin Foster lynx->mdio = mdio;
3260699b3e0SColin Foster lynx->pcs.ops = &lynx_pcs_phylink_ops;
327c689a652SRussell King (Oracle) lynx->pcs.neg_mode = true;
3280699b3e0SColin Foster lynx->pcs.poll = true;
3290da4c3d3SIoana Ciornei
3300699b3e0SColin Foster return lynx_to_phylink_pcs(lynx);
3310da4c3d3SIoana Ciornei }
3320da4c3d3SIoana Ciornei
lynx_pcs_create_mdiodev(struct mii_bus * bus,int addr)33386b5f2d8SRussell King (Oracle) struct phylink_pcs *lynx_pcs_create_mdiodev(struct mii_bus *bus, int addr)
33486b5f2d8SRussell King (Oracle) {
33586b5f2d8SRussell King (Oracle) struct mdio_device *mdio;
33686b5f2d8SRussell King (Oracle) struct phylink_pcs *pcs;
33786b5f2d8SRussell King (Oracle)
33886b5f2d8SRussell King (Oracle) mdio = mdio_device_create(bus, addr);
33986b5f2d8SRussell King (Oracle) if (IS_ERR(mdio))
34086b5f2d8SRussell King (Oracle) return ERR_CAST(mdio);
34186b5f2d8SRussell King (Oracle)
34286b5f2d8SRussell King (Oracle) pcs = lynx_pcs_create(mdio);
34386b5f2d8SRussell King (Oracle)
34486b5f2d8SRussell King (Oracle) /* lynx_create() has taken a refcount on the mdiodev if it was
34586b5f2d8SRussell King (Oracle) * successful. If lynx_create() fails, this will free the mdio
34686b5f2d8SRussell King (Oracle) * device here. In any case, we don't need to hold our reference
34786b5f2d8SRussell King (Oracle) * anymore, and putting it here will allow mdio_device_put() in
34886b5f2d8SRussell King (Oracle) * lynx_destroy() to automatically free the mdio device.
34986b5f2d8SRussell King (Oracle) */
35086b5f2d8SRussell King (Oracle) mdio_device_put(mdio);
35186b5f2d8SRussell King (Oracle)
35286b5f2d8SRussell King (Oracle) return pcs;
35386b5f2d8SRussell King (Oracle) }
35486b5f2d8SRussell King (Oracle) EXPORT_SYMBOL(lynx_pcs_create_mdiodev);
35586b5f2d8SRussell King (Oracle)
356d143898cSRussell King (Oracle) /*
357d143898cSRussell King (Oracle) * lynx_pcs_create_fwnode() creates a lynx PCS instance from the fwnode
358d143898cSRussell King (Oracle) * device indicated by node.
359d143898cSRussell King (Oracle) *
360d143898cSRussell King (Oracle) * Returns:
361d143898cSRussell King (Oracle) * -ENODEV if the fwnode is marked unavailable
362d143898cSRussell King (Oracle) * -EPROBE_DEFER if we fail to find the device
363d143898cSRussell King (Oracle) * -ENOMEM if we fail to allocate memory
364d143898cSRussell King (Oracle) * pointer to a phylink_pcs on success
365d143898cSRussell King (Oracle) */
lynx_pcs_create_fwnode(struct fwnode_handle * node)3666e1a1282SRussell King (Oracle) struct phylink_pcs *lynx_pcs_create_fwnode(struct fwnode_handle *node)
3676e1a1282SRussell King (Oracle) {
3686e1a1282SRussell King (Oracle) struct mdio_device *mdio;
3696e1a1282SRussell King (Oracle) struct phylink_pcs *pcs;
3706e1a1282SRussell King (Oracle)
371d143898cSRussell King (Oracle) if (!fwnode_device_is_available(node))
372d143898cSRussell King (Oracle) return ERR_PTR(-ENODEV);
373d143898cSRussell King (Oracle)
3746e1a1282SRussell King (Oracle) mdio = fwnode_mdio_find_device(node);
3756e1a1282SRussell King (Oracle) if (!mdio)
3766e1a1282SRussell King (Oracle) return ERR_PTR(-EPROBE_DEFER);
3776e1a1282SRussell King (Oracle)
3786e1a1282SRussell King (Oracle) pcs = lynx_pcs_create(mdio);
3796e1a1282SRussell King (Oracle)
3806e1a1282SRussell King (Oracle) /* lynx_create() has taken a refcount on the mdiodev if it was
3816e1a1282SRussell King (Oracle) * successful. If lynx_create() fails, this will free the mdio
3826e1a1282SRussell King (Oracle) * device here. In any case, we don't need to hold our reference
3836e1a1282SRussell King (Oracle) * anymore, and putting it here will allow mdio_device_put() in
3846e1a1282SRussell King (Oracle) * lynx_destroy() to automatically free the mdio device.
3856e1a1282SRussell King (Oracle) */
3866e1a1282SRussell King (Oracle) mdio_device_put(mdio);
3876e1a1282SRussell King (Oracle)
3886e1a1282SRussell King (Oracle) return pcs;
3896e1a1282SRussell King (Oracle) }
3906e1a1282SRussell King (Oracle) EXPORT_SYMBOL_GPL(lynx_pcs_create_fwnode);
3916e1a1282SRussell King (Oracle)
lynx_pcs_destroy(struct phylink_pcs * pcs)392e7026f15SColin Foster void lynx_pcs_destroy(struct phylink_pcs *pcs)
3930da4c3d3SIoana Ciornei {
394e7026f15SColin Foster struct lynx_pcs *lynx = phylink_pcs_to_lynx(pcs);
395e7026f15SColin Foster
39686b5f2d8SRussell King (Oracle) mdio_device_put(lynx->mdio);
397e7026f15SColin Foster kfree(lynx);
3980da4c3d3SIoana Ciornei }
3990da4c3d3SIoana Ciornei EXPORT_SYMBOL(lynx_pcs_destroy);
4000da4c3d3SIoana Ciornei
4010da4c3d3SIoana Ciornei MODULE_LICENSE("Dual BSD/GPL");
402