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