1 // SPDX-License-Identifier: GPL-2.0+
2 
3 #include <linux/module.h>
4 #include <linux/phylink.h>
5 #include <linux/device.h>
6 #include <linux/netdevice.h>
7 #include <linux/phy/phy.h>
8 #include <linux/sfp.h>
9 
10 #include "lan966x_main.h"
11 
12 static struct phylink_pcs *lan966x_phylink_mac_select(struct phylink_config *config,
13 						      phy_interface_t interface)
14 {
15 	struct lan966x_port *port = netdev_priv(to_net_dev(config->dev));
16 
17 	return &port->phylink_pcs;
18 }
19 
20 static void lan966x_phylink_mac_config(struct phylink_config *config,
21 				       unsigned int mode,
22 				       const struct phylink_link_state *state)
23 {
24 }
25 
26 static int lan966x_phylink_mac_prepare(struct phylink_config *config,
27 				       unsigned int mode,
28 				       phy_interface_t iface)
29 {
30 	struct lan966x_port *port = netdev_priv(to_net_dev(config->dev));
31 	int err;
32 
33 	if (port->serdes) {
34 		err = phy_set_mode_ext(port->serdes, PHY_MODE_ETHERNET,
35 				       iface);
36 		if (err) {
37 			netdev_err(to_net_dev(config->dev),
38 				   "Could not set mode of SerDes\n");
39 			return err;
40 		}
41 	}
42 
43 	return 0;
44 }
45 
46 static void lan966x_phylink_mac_link_up(struct phylink_config *config,
47 					struct phy_device *phy,
48 					unsigned int mode,
49 					phy_interface_t interface,
50 					int speed, int duplex,
51 					bool tx_pause, bool rx_pause)
52 {
53 	struct lan966x_port *port = netdev_priv(to_net_dev(config->dev));
54 	struct lan966x_port_config *port_config = &port->config;
55 
56 	port_config->duplex = duplex;
57 	port_config->speed = speed;
58 	port_config->pause = 0;
59 	port_config->pause |= tx_pause ? MLO_PAUSE_TX : 0;
60 	port_config->pause |= rx_pause ? MLO_PAUSE_RX : 0;
61 
62 	lan966x_port_config_up(port);
63 }
64 
65 static void lan966x_phylink_mac_link_down(struct phylink_config *config,
66 					  unsigned int mode,
67 					  phy_interface_t interface)
68 {
69 	struct lan966x_port *port = netdev_priv(to_net_dev(config->dev));
70 	struct lan966x *lan966x = port->lan966x;
71 
72 	lan966x_port_config_down(port);
73 
74 	/* Take PCS out of reset */
75 	lan_rmw(DEV_CLOCK_CFG_PCS_RX_RST_SET(0) |
76 		DEV_CLOCK_CFG_PCS_TX_RST_SET(0),
77 		DEV_CLOCK_CFG_PCS_RX_RST |
78 		DEV_CLOCK_CFG_PCS_TX_RST,
79 		lan966x, DEV_CLOCK_CFG(port->chip_port));
80 }
81 
82 static struct lan966x_port *lan966x_pcs_to_port(struct phylink_pcs *pcs)
83 {
84 	return container_of(pcs, struct lan966x_port, phylink_pcs);
85 }
86 
87 static void lan966x_pcs_get_state(struct phylink_pcs *pcs,
88 				  struct phylink_link_state *state)
89 {
90 	struct lan966x_port *port = lan966x_pcs_to_port(pcs);
91 
92 	lan966x_port_status_get(port, state);
93 }
94 
95 static int lan966x_pcs_config(struct phylink_pcs *pcs,
96 			      unsigned int mode,
97 			      phy_interface_t interface,
98 			      const unsigned long *advertising,
99 			      bool permit_pause_to_mac)
100 {
101 	struct lan966x_port *port = lan966x_pcs_to_port(pcs);
102 	struct lan966x_port_config config;
103 	int ret;
104 
105 	config = port->config;
106 	config.portmode = interface;
107 	config.inband = phylink_autoneg_inband(mode);
108 	config.autoneg = phylink_test(advertising, Autoneg);
109 	config.advertising = advertising;
110 
111 	ret = lan966x_port_pcs_set(port, &config);
112 	if (ret)
113 		netdev_err(port->dev, "port PCS config failed: %d\n", ret);
114 
115 	return ret;
116 }
117 
118 static void lan966x_pcs_aneg_restart(struct phylink_pcs *pcs)
119 {
120 	/* Currently not used */
121 }
122 
123 const struct phylink_mac_ops lan966x_phylink_mac_ops = {
124 	.validate = phylink_generic_validate,
125 	.mac_select_pcs = lan966x_phylink_mac_select,
126 	.mac_config = lan966x_phylink_mac_config,
127 	.mac_prepare = lan966x_phylink_mac_prepare,
128 	.mac_link_down = lan966x_phylink_mac_link_down,
129 	.mac_link_up = lan966x_phylink_mac_link_up,
130 };
131 
132 const struct phylink_pcs_ops lan966x_phylink_pcs_ops = {
133 	.pcs_get_state = lan966x_pcs_get_state,
134 	.pcs_config = lan966x_pcs_config,
135 	.pcs_an_restart = lan966x_pcs_aneg_restart,
136 };
137