1f3cad261SSteen Hegelund // SPDX-License-Identifier: GPL-2.0+
2f3cad261SSteen Hegelund /* Microchip Sparx5 Switch driver
3f3cad261SSteen Hegelund *
4f3cad261SSteen Hegelund * Copyright (c) 2021 Microchip Technology Inc. and its subsidiaries.
5f3cad261SSteen Hegelund */
6f3cad261SSteen Hegelund
7f3cad261SSteen Hegelund #include <linux/module.h>
8f3cad261SSteen Hegelund #include <linux/phylink.h>
9f3cad261SSteen Hegelund #include <linux/device.h>
10f3cad261SSteen Hegelund #include <linux/netdevice.h>
11f3cad261SSteen Hegelund #include <linux/sfp.h>
12f3cad261SSteen Hegelund
13f3cad261SSteen Hegelund #include "sparx5_main_regs.h"
14f3cad261SSteen Hegelund #include "sparx5_main.h"
15946e7fd5SSteen Hegelund #include "sparx5_port.h"
16f3cad261SSteen Hegelund
port_conf_has_changed(struct sparx5_port_config * a,struct sparx5_port_config * b)17f3cad261SSteen Hegelund static bool port_conf_has_changed(struct sparx5_port_config *a, struct sparx5_port_config *b)
18f3cad261SSteen Hegelund {
19f3cad261SSteen Hegelund if (a->speed != b->speed ||
20f3cad261SSteen Hegelund a->portmode != b->portmode ||
21f3cad261SSteen Hegelund a->autoneg != b->autoneg ||
22f3cad261SSteen Hegelund a->pause_adv != b->pause_adv ||
23f3cad261SSteen Hegelund a->power_down != b->power_down ||
24f3cad261SSteen Hegelund a->media != b->media)
25f3cad261SSteen Hegelund return true;
26f3cad261SSteen Hegelund return false;
27f3cad261SSteen Hegelund }
28f3cad261SSteen Hegelund
299c8c4402SRussell King (Oracle) static struct phylink_pcs *
sparx5_phylink_mac_select_pcs(struct phylink_config * config,phy_interface_t interface)309c8c4402SRussell King (Oracle) sparx5_phylink_mac_select_pcs(struct phylink_config *config,
319c8c4402SRussell King (Oracle) phy_interface_t interface)
329c8c4402SRussell King (Oracle) {
339c8c4402SRussell King (Oracle) struct sparx5_port *port = netdev_priv(to_net_dev(config->dev));
349c8c4402SRussell King (Oracle)
359b8d70ecSDaniel Machon /* Return the PCS for all the modes that require it. */
369b8d70ecSDaniel Machon switch (interface) {
379b8d70ecSDaniel Machon case PHY_INTERFACE_MODE_SGMII:
389b8d70ecSDaniel Machon case PHY_INTERFACE_MODE_QSGMII:
399b8d70ecSDaniel Machon case PHY_INTERFACE_MODE_1000BASEX:
409b8d70ecSDaniel Machon case PHY_INTERFACE_MODE_2500BASEX:
419b8d70ecSDaniel Machon case PHY_INTERFACE_MODE_5GBASER:
429b8d70ecSDaniel Machon case PHY_INTERFACE_MODE_10GBASER:
439b8d70ecSDaniel Machon case PHY_INTERFACE_MODE_25GBASER:
449c8c4402SRussell King (Oracle) return &port->phylink_pcs;
459b8d70ecSDaniel Machon default:
469b8d70ecSDaniel Machon return NULL;
479b8d70ecSDaniel Machon }
489c8c4402SRussell King (Oracle) }
499c8c4402SRussell King (Oracle)
sparx5_phylink_mac_config(struct phylink_config * config,unsigned int mode,const struct phylink_link_state * state)50f3cad261SSteen Hegelund static void sparx5_phylink_mac_config(struct phylink_config *config,
51f3cad261SSteen Hegelund unsigned int mode,
52f3cad261SSteen Hegelund const struct phylink_link_state *state)
53f3cad261SSteen Hegelund {
54f3cad261SSteen Hegelund /* Currently not used */
55f3cad261SSteen Hegelund }
56f3cad261SSteen Hegelund
sparx5_phylink_mac_link_up(struct phylink_config * config,struct phy_device * phy,unsigned int mode,phy_interface_t interface,int speed,int duplex,bool tx_pause,bool rx_pause)57f3cad261SSteen Hegelund static void sparx5_phylink_mac_link_up(struct phylink_config *config,
58f3cad261SSteen Hegelund struct phy_device *phy,
59f3cad261SSteen Hegelund unsigned int mode,
60f3cad261SSteen Hegelund phy_interface_t interface,
61f3cad261SSteen Hegelund int speed, int duplex,
62f3cad261SSteen Hegelund bool tx_pause, bool rx_pause)
63f3cad261SSteen Hegelund {
64f3cad261SSteen Hegelund struct sparx5_port *port = netdev_priv(to_net_dev(config->dev));
65f3cad261SSteen Hegelund struct sparx5_port_config conf;
66946e7fd5SSteen Hegelund int err;
67f3cad261SSteen Hegelund
68f3cad261SSteen Hegelund conf = port->conf;
69f3cad261SSteen Hegelund conf.duplex = duplex;
70f3cad261SSteen Hegelund conf.pause = 0;
71f3cad261SSteen Hegelund conf.pause |= tx_pause ? MLO_PAUSE_TX : 0;
72f3cad261SSteen Hegelund conf.pause |= rx_pause ? MLO_PAUSE_RX : 0;
73f3cad261SSteen Hegelund conf.speed = speed;
74946e7fd5SSteen Hegelund /* Configure the port to speed/duplex/pause */
75946e7fd5SSteen Hegelund err = sparx5_port_config(port->sparx5, port, &conf);
76946e7fd5SSteen Hegelund if (err)
77946e7fd5SSteen Hegelund netdev_err(port->ndev, "port config failed: %d\n", err);
78f3cad261SSteen Hegelund }
79f3cad261SSteen Hegelund
sparx5_phylink_mac_link_down(struct phylink_config * config,unsigned int mode,phy_interface_t interface)80f3cad261SSteen Hegelund static void sparx5_phylink_mac_link_down(struct phylink_config *config,
81f3cad261SSteen Hegelund unsigned int mode,
82f3cad261SSteen Hegelund phy_interface_t interface)
83f3cad261SSteen Hegelund {
84f3cad261SSteen Hegelund /* Currently not used */
85f3cad261SSteen Hegelund }
86f3cad261SSteen Hegelund
sparx5_pcs_to_port(struct phylink_pcs * pcs)87f3cad261SSteen Hegelund static struct sparx5_port *sparx5_pcs_to_port(struct phylink_pcs *pcs)
88f3cad261SSteen Hegelund {
89f3cad261SSteen Hegelund return container_of(pcs, struct sparx5_port, phylink_pcs);
90f3cad261SSteen Hegelund }
91f3cad261SSteen Hegelund
sparx5_pcs_get_state(struct phylink_pcs * pcs,unsigned int neg_mode,struct phylink_link_state * state)92*c6739623SRussell King (Oracle) static void sparx5_pcs_get_state(struct phylink_pcs *pcs, unsigned int neg_mode,
93f3cad261SSteen Hegelund struct phylink_link_state *state)
94f3cad261SSteen Hegelund {
95946e7fd5SSteen Hegelund struct sparx5_port *port = sparx5_pcs_to_port(pcs);
96946e7fd5SSteen Hegelund struct sparx5_port_status status;
97946e7fd5SSteen Hegelund
98946e7fd5SSteen Hegelund sparx5_get_port_status(port->sparx5, port, &status);
99946e7fd5SSteen Hegelund state->link = status.link && !status.link_down;
100946e7fd5SSteen Hegelund state->an_complete = status.an_complete;
101946e7fd5SSteen Hegelund state->speed = status.speed;
102946e7fd5SSteen Hegelund state->duplex = status.duplex;
103946e7fd5SSteen Hegelund state->pause = status.pause;
104f3cad261SSteen Hegelund }
105f3cad261SSteen Hegelund
sparx5_pcs_config(struct phylink_pcs * pcs,unsigned int neg_mode,phy_interface_t interface,const unsigned long * advertising,bool permit_pause_to_mac)1066e5bb3daSRussell King (Oracle) static int sparx5_pcs_config(struct phylink_pcs *pcs, unsigned int neg_mode,
107f3cad261SSteen Hegelund phy_interface_t interface,
108f3cad261SSteen Hegelund const unsigned long *advertising,
109f3cad261SSteen Hegelund bool permit_pause_to_mac)
110f3cad261SSteen Hegelund {
111f3cad261SSteen Hegelund struct sparx5_port *port = sparx5_pcs_to_port(pcs);
112f3cad261SSteen Hegelund struct sparx5_port_config conf;
113f3cad261SSteen Hegelund int ret = 0;
114f3cad261SSteen Hegelund
115f3cad261SSteen Hegelund conf = port->conf;
116f3cad261SSteen Hegelund conf.power_down = false;
117f3cad261SSteen Hegelund conf.portmode = interface;
1186e5bb3daSRussell King (Oracle) conf.inband = neg_mode == PHYLINK_PCS_NEG_INBAND_DISABLED ||
1196e5bb3daSRussell King (Oracle) neg_mode == PHYLINK_PCS_NEG_INBAND_ENABLED;
1206e5bb3daSRussell King (Oracle) conf.autoneg = neg_mode == PHYLINK_PCS_NEG_INBAND_ENABLED;
121f3cad261SSteen Hegelund conf.pause_adv = 0;
122f3cad261SSteen Hegelund if (phylink_test(advertising, Pause))
123f3cad261SSteen Hegelund conf.pause_adv |= ADVERTISE_1000XPAUSE;
124f3cad261SSteen Hegelund if (phylink_test(advertising, Asym_Pause))
125f3cad261SSteen Hegelund conf.pause_adv |= ADVERTISE_1000XPSE_ASYM;
126f3cad261SSteen Hegelund if (sparx5_is_baser(interface)) {
127f3cad261SSteen Hegelund if (phylink_test(advertising, FIBRE))
128f3cad261SSteen Hegelund conf.media = PHY_MEDIA_SR;
129f3cad261SSteen Hegelund else
130f3cad261SSteen Hegelund conf.media = PHY_MEDIA_DAC;
131f3cad261SSteen Hegelund }
132f3cad261SSteen Hegelund if (!port_conf_has_changed(&port->conf, &conf))
133f3cad261SSteen Hegelund return ret;
134946e7fd5SSteen Hegelund /* Enable the PCS matching this interface type */
135946e7fd5SSteen Hegelund ret = sparx5_port_pcs_set(port->sparx5, port, &conf);
136946e7fd5SSteen Hegelund if (ret)
137946e7fd5SSteen Hegelund netdev_err(port->ndev, "port PCS config failed: %d\n", ret);
138f3cad261SSteen Hegelund return ret;
139f3cad261SSteen Hegelund }
140f3cad261SSteen Hegelund
sparx5_pcs_aneg_restart(struct phylink_pcs * pcs)141f3cad261SSteen Hegelund static void sparx5_pcs_aneg_restart(struct phylink_pcs *pcs)
142f3cad261SSteen Hegelund {
143f3cad261SSteen Hegelund /* Currently not used */
144f3cad261SSteen Hegelund }
145f3cad261SSteen Hegelund
146f3cad261SSteen Hegelund const struct phylink_pcs_ops sparx5_phylink_pcs_ops = {
147f3cad261SSteen Hegelund .pcs_get_state = sparx5_pcs_get_state,
148f3cad261SSteen Hegelund .pcs_config = sparx5_pcs_config,
149f3cad261SSteen Hegelund .pcs_an_restart = sparx5_pcs_aneg_restart,
150f3cad261SSteen Hegelund };
151f3cad261SSteen Hegelund
152f3cad261SSteen Hegelund const struct phylink_mac_ops sparx5_phylink_mac_ops = {
1539c8c4402SRussell King (Oracle) .mac_select_pcs = sparx5_phylink_mac_select_pcs,
154f3cad261SSteen Hegelund .mac_config = sparx5_phylink_mac_config,
155f3cad261SSteen Hegelund .mac_link_down = sparx5_phylink_mac_link_down,
156f3cad261SSteen Hegelund .mac_link_up = sparx5_phylink_mac_link_up,
157f3cad261SSteen Hegelund };
158