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)
359c8c4402SRussell King (Oracle) return &port->phylink_pcs;
369c8c4402SRussell King (Oracle) }
379c8c4402SRussell King (Oracle)
sparx5_phylink_mac_config(struct phylink_config * config,unsigned int mode,const struct phylink_link_state * state)38f3cad261SSteen Hegelund static void sparx5_phylink_mac_config(struct phylink_config *config,
39f3cad261SSteen Hegelund unsigned int mode,
40f3cad261SSteen Hegelund const struct phylink_link_state *state)
41f3cad261SSteen Hegelund {
42f3cad261SSteen Hegelund /* Currently not used */
43f3cad261SSteen Hegelund }
44f3cad261SSteen 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)45f3cad261SSteen Hegelund static void sparx5_phylink_mac_link_up(struct phylink_config *config,
46f3cad261SSteen Hegelund struct phy_device *phy,
47f3cad261SSteen Hegelund unsigned int mode,
48f3cad261SSteen Hegelund phy_interface_t interface,
49f3cad261SSteen Hegelund int speed, int duplex,
50f3cad261SSteen Hegelund bool tx_pause, bool rx_pause)
51f3cad261SSteen Hegelund {
52f3cad261SSteen Hegelund struct sparx5_port *port = netdev_priv(to_net_dev(config->dev));
53f3cad261SSteen Hegelund struct sparx5_port_config conf;
54946e7fd5SSteen Hegelund int err;
55f3cad261SSteen Hegelund
56f3cad261SSteen Hegelund conf = port->conf;
57f3cad261SSteen Hegelund conf.duplex = duplex;
58f3cad261SSteen Hegelund conf.pause = 0;
59f3cad261SSteen Hegelund conf.pause |= tx_pause ? MLO_PAUSE_TX : 0;
60f3cad261SSteen Hegelund conf.pause |= rx_pause ? MLO_PAUSE_RX : 0;
61f3cad261SSteen Hegelund conf.speed = speed;
62946e7fd5SSteen Hegelund /* Configure the port to speed/duplex/pause */
63946e7fd5SSteen Hegelund err = sparx5_port_config(port->sparx5, port, &conf);
64946e7fd5SSteen Hegelund if (err)
65946e7fd5SSteen Hegelund netdev_err(port->ndev, "port config failed: %d\n", err);
66f3cad261SSteen Hegelund }
67f3cad261SSteen Hegelund
sparx5_phylink_mac_link_down(struct phylink_config * config,unsigned int mode,phy_interface_t interface)68f3cad261SSteen Hegelund static void sparx5_phylink_mac_link_down(struct phylink_config *config,
69f3cad261SSteen Hegelund unsigned int mode,
70f3cad261SSteen Hegelund phy_interface_t interface)
71f3cad261SSteen Hegelund {
72f3cad261SSteen Hegelund /* Currently not used */
73f3cad261SSteen Hegelund }
74f3cad261SSteen Hegelund
sparx5_pcs_to_port(struct phylink_pcs * pcs)75f3cad261SSteen Hegelund static struct sparx5_port *sparx5_pcs_to_port(struct phylink_pcs *pcs)
76f3cad261SSteen Hegelund {
77f3cad261SSteen Hegelund return container_of(pcs, struct sparx5_port, phylink_pcs);
78f3cad261SSteen Hegelund }
79f3cad261SSteen Hegelund
sparx5_pcs_get_state(struct phylink_pcs * pcs,struct phylink_link_state * state)80f3cad261SSteen Hegelund static void sparx5_pcs_get_state(struct phylink_pcs *pcs,
81f3cad261SSteen Hegelund struct phylink_link_state *state)
82f3cad261SSteen Hegelund {
83946e7fd5SSteen Hegelund struct sparx5_port *port = sparx5_pcs_to_port(pcs);
84946e7fd5SSteen Hegelund struct sparx5_port_status status;
85946e7fd5SSteen Hegelund
86946e7fd5SSteen Hegelund sparx5_get_port_status(port->sparx5, port, &status);
87946e7fd5SSteen Hegelund state->link = status.link && !status.link_down;
88946e7fd5SSteen Hegelund state->an_complete = status.an_complete;
89946e7fd5SSteen Hegelund state->speed = status.speed;
90946e7fd5SSteen Hegelund state->duplex = status.duplex;
91946e7fd5SSteen Hegelund state->pause = status.pause;
92f3cad261SSteen Hegelund }
93f3cad261SSteen 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)94*6e5bb3daSRussell King (Oracle) static int sparx5_pcs_config(struct phylink_pcs *pcs, unsigned int neg_mode,
95f3cad261SSteen Hegelund phy_interface_t interface,
96f3cad261SSteen Hegelund const unsigned long *advertising,
97f3cad261SSteen Hegelund bool permit_pause_to_mac)
98f3cad261SSteen Hegelund {
99f3cad261SSteen Hegelund struct sparx5_port *port = sparx5_pcs_to_port(pcs);
100f3cad261SSteen Hegelund struct sparx5_port_config conf;
101f3cad261SSteen Hegelund int ret = 0;
102f3cad261SSteen Hegelund
103f3cad261SSteen Hegelund conf = port->conf;
104f3cad261SSteen Hegelund conf.power_down = false;
105f3cad261SSteen Hegelund conf.portmode = interface;
106*6e5bb3daSRussell King (Oracle) conf.inband = neg_mode == PHYLINK_PCS_NEG_INBAND_DISABLED ||
107*6e5bb3daSRussell King (Oracle) neg_mode == PHYLINK_PCS_NEG_INBAND_ENABLED;
108*6e5bb3daSRussell King (Oracle) conf.autoneg = neg_mode == PHYLINK_PCS_NEG_INBAND_ENABLED;
109f3cad261SSteen Hegelund conf.pause_adv = 0;
110f3cad261SSteen Hegelund if (phylink_test(advertising, Pause))
111f3cad261SSteen Hegelund conf.pause_adv |= ADVERTISE_1000XPAUSE;
112f3cad261SSteen Hegelund if (phylink_test(advertising, Asym_Pause))
113f3cad261SSteen Hegelund conf.pause_adv |= ADVERTISE_1000XPSE_ASYM;
114f3cad261SSteen Hegelund if (sparx5_is_baser(interface)) {
115f3cad261SSteen Hegelund if (phylink_test(advertising, FIBRE))
116f3cad261SSteen Hegelund conf.media = PHY_MEDIA_SR;
117f3cad261SSteen Hegelund else
118f3cad261SSteen Hegelund conf.media = PHY_MEDIA_DAC;
119f3cad261SSteen Hegelund }
120f3cad261SSteen Hegelund if (!port_conf_has_changed(&port->conf, &conf))
121f3cad261SSteen Hegelund return ret;
122946e7fd5SSteen Hegelund /* Enable the PCS matching this interface type */
123946e7fd5SSteen Hegelund ret = sparx5_port_pcs_set(port->sparx5, port, &conf);
124946e7fd5SSteen Hegelund if (ret)
125946e7fd5SSteen Hegelund netdev_err(port->ndev, "port PCS config failed: %d\n", ret);
126f3cad261SSteen Hegelund return ret;
127f3cad261SSteen Hegelund }
128f3cad261SSteen Hegelund
sparx5_pcs_aneg_restart(struct phylink_pcs * pcs)129f3cad261SSteen Hegelund static void sparx5_pcs_aneg_restart(struct phylink_pcs *pcs)
130f3cad261SSteen Hegelund {
131f3cad261SSteen Hegelund /* Currently not used */
132f3cad261SSteen Hegelund }
133f3cad261SSteen Hegelund
134f3cad261SSteen Hegelund const struct phylink_pcs_ops sparx5_phylink_pcs_ops = {
135f3cad261SSteen Hegelund .pcs_get_state = sparx5_pcs_get_state,
136f3cad261SSteen Hegelund .pcs_config = sparx5_pcs_config,
137f3cad261SSteen Hegelund .pcs_an_restart = sparx5_pcs_aneg_restart,
138f3cad261SSteen Hegelund };
139f3cad261SSteen Hegelund
140f3cad261SSteen Hegelund const struct phylink_mac_ops sparx5_phylink_mac_ops = {
1419c8c4402SRussell King (Oracle) .mac_select_pcs = sparx5_phylink_mac_select_pcs,
142f3cad261SSteen Hegelund .mac_config = sparx5_phylink_mac_config,
143f3cad261SSteen Hegelund .mac_link_down = sparx5_phylink_mac_link_down,
144f3cad261SSteen Hegelund .mac_link_up = sparx5_phylink_mac_link_up,
145f3cad261SSteen Hegelund };
146