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 	if (phy_interface_mode_is_rgmii(interface))
64 		phy_set_speed(port->serdes, speed);
65 
66 	lan966x_port_config_up(port);
67 }
68 
69 static void lan966x_phylink_mac_link_down(struct phylink_config *config,
70 					  unsigned int mode,
71 					  phy_interface_t interface)
72 {
73 	struct lan966x_port *port = netdev_priv(to_net_dev(config->dev));
74 	struct lan966x *lan966x = port->lan966x;
75 
76 	lan966x_port_config_down(port);
77 
78 	/* Take PCS out of reset */
79 	lan_rmw(DEV_CLOCK_CFG_PCS_RX_RST_SET(0) |
80 		DEV_CLOCK_CFG_PCS_TX_RST_SET(0),
81 		DEV_CLOCK_CFG_PCS_RX_RST |
82 		DEV_CLOCK_CFG_PCS_TX_RST,
83 		lan966x, DEV_CLOCK_CFG(port->chip_port));
84 }
85 
86 static struct lan966x_port *lan966x_pcs_to_port(struct phylink_pcs *pcs)
87 {
88 	return container_of(pcs, struct lan966x_port, phylink_pcs);
89 }
90 
91 static void lan966x_pcs_get_state(struct phylink_pcs *pcs,
92 				  struct phylink_link_state *state)
93 {
94 	struct lan966x_port *port = lan966x_pcs_to_port(pcs);
95 
96 	lan966x_port_status_get(port, state);
97 }
98 
99 static int lan966x_pcs_config(struct phylink_pcs *pcs,
100 			      unsigned int mode,
101 			      phy_interface_t interface,
102 			      const unsigned long *advertising,
103 			      bool permit_pause_to_mac)
104 {
105 	struct lan966x_port *port = lan966x_pcs_to_port(pcs);
106 	struct lan966x_port_config config;
107 	int ret;
108 
109 	config = port->config;
110 	config.portmode = interface;
111 	config.inband = phylink_autoneg_inband(mode);
112 	config.autoneg = phylink_test(advertising, Autoneg);
113 	config.advertising = advertising;
114 
115 	ret = lan966x_port_pcs_set(port, &config);
116 	if (ret)
117 		netdev_err(port->dev, "port PCS config failed: %d\n", ret);
118 
119 	return ret;
120 }
121 
122 static void lan966x_pcs_aneg_restart(struct phylink_pcs *pcs)
123 {
124 	/* Currently not used */
125 }
126 
127 const struct phylink_mac_ops lan966x_phylink_mac_ops = {
128 	.validate = phylink_generic_validate,
129 	.mac_select_pcs = lan966x_phylink_mac_select,
130 	.mac_config = lan966x_phylink_mac_config,
131 	.mac_prepare = lan966x_phylink_mac_prepare,
132 	.mac_link_down = lan966x_phylink_mac_link_down,
133 	.mac_link_up = lan966x_phylink_mac_link_up,
134 };
135 
136 const struct phylink_pcs_ops lan966x_phylink_pcs_ops = {
137 	.pcs_get_state = lan966x_pcs_get_state,
138 	.pcs_config = lan966x_pcs_config,
139 	.pcs_an_restart = lan966x_pcs_aneg_restart,
140 };
141