xref: /openbmc/linux/drivers/net/ethernet/wangxun/ngbe/ngbe_mdio.c (revision 26d0dfbb16fcb17d128a79dc70f3020ea6992af0)
1a1cf597bSMengyuan Lou // SPDX-License-Identifier: GPL-2.0
2a1cf597bSMengyuan Lou /* Copyright (c) 2019 - 2022 Beijing WangXun Technology Co., Ltd. */
3a1cf597bSMengyuan Lou 
4a1cf597bSMengyuan Lou #include <linux/ethtool.h>
5a1cf597bSMengyuan Lou #include <linux/iopoll.h>
6a1cf597bSMengyuan Lou #include <linux/pci.h>
7a1cf597bSMengyuan Lou #include <linux/phy.h>
8a1cf597bSMengyuan Lou 
9a1cf597bSMengyuan Lou #include "../libwx/wx_type.h"
10a1cf597bSMengyuan Lou #include "../libwx/wx_hw.h"
11a1cf597bSMengyuan Lou #include "ngbe_type.h"
12a1cf597bSMengyuan Lou #include "ngbe_mdio.h"
13a1cf597bSMengyuan Lou 
ngbe_phy_read_reg_internal(struct mii_bus * bus,int phy_addr,int regnum)14a1cf597bSMengyuan Lou static int ngbe_phy_read_reg_internal(struct mii_bus *bus, int phy_addr, int regnum)
15a1cf597bSMengyuan Lou {
16a1cf597bSMengyuan Lou 	struct wx *wx = bus->priv;
17a1cf597bSMengyuan Lou 
18a1cf597bSMengyuan Lou 	if (phy_addr != 0)
19a1cf597bSMengyuan Lou 		return 0xffff;
20a1cf597bSMengyuan Lou 	return (u16)rd32(wx, NGBE_PHY_CONFIG(regnum));
21a1cf597bSMengyuan Lou }
22a1cf597bSMengyuan Lou 
ngbe_phy_write_reg_internal(struct mii_bus * bus,int phy_addr,int regnum,u16 value)23a1cf597bSMengyuan Lou static int ngbe_phy_write_reg_internal(struct mii_bus *bus, int phy_addr, int regnum, u16 value)
24a1cf597bSMengyuan Lou {
25a1cf597bSMengyuan Lou 	struct wx *wx = bus->priv;
26a1cf597bSMengyuan Lou 
27a1cf597bSMengyuan Lou 	if (phy_addr == 0)
28a1cf597bSMengyuan Lou 		wr32(wx, NGBE_PHY_CONFIG(regnum), value);
29a1cf597bSMengyuan Lou 	return 0;
30a1cf597bSMengyuan Lou }
31a1cf597bSMengyuan Lou 
ngbe_phy_read_reg_mdi_c22(struct mii_bus * bus,int phy_addr,int regnum)32a1cf597bSMengyuan Lou static int ngbe_phy_read_reg_mdi_c22(struct mii_bus *bus, int phy_addr, int regnum)
33a1cf597bSMengyuan Lou {
34a1cf597bSMengyuan Lou 	u32 command, val, device_type = 0;
35a1cf597bSMengyuan Lou 	struct wx *wx = bus->priv;
36a1cf597bSMengyuan Lou 	int ret;
37a1cf597bSMengyuan Lou 
38a1cf597bSMengyuan Lou 	wr32(wx, NGBE_MDIO_CLAUSE_SELECT, 0xF);
39a1cf597bSMengyuan Lou 	/* setup and write the address cycle command */
40ad63f7aaSJiawen Wu 	command = WX_MSCA_RA(regnum) |
41ad63f7aaSJiawen Wu 		  WX_MSCA_PA(phy_addr) |
42ad63f7aaSJiawen Wu 		  WX_MSCA_DA(device_type);
43ad63f7aaSJiawen Wu 	wr32(wx, WX_MSCA, command);
44ad63f7aaSJiawen Wu 	command = WX_MSCC_CMD(WX_MSCA_CMD_READ) |
45ad63f7aaSJiawen Wu 		  WX_MSCC_BUSY |
46ad63f7aaSJiawen Wu 		  WX_MDIO_CLK(6);
47ad63f7aaSJiawen Wu 	wr32(wx, WX_MSCC, command);
48a1cf597bSMengyuan Lou 
49a1cf597bSMengyuan Lou 	/* wait to complete */
50ad63f7aaSJiawen Wu 	ret = read_poll_timeout(rd32, val, !(val & WX_MSCC_BUSY), 1000,
51ad63f7aaSJiawen Wu 				100000, false, wx, WX_MSCC);
52a1cf597bSMengyuan Lou 	if (ret) {
53a1cf597bSMengyuan Lou 		wx_err(wx, "Mdio read c22 command did not complete.\n");
54a1cf597bSMengyuan Lou 		return ret;
55a1cf597bSMengyuan Lou 	}
56a1cf597bSMengyuan Lou 
57ad63f7aaSJiawen Wu 	return (u16)rd32(wx, WX_MSCC);
58a1cf597bSMengyuan Lou }
59a1cf597bSMengyuan Lou 
ngbe_phy_write_reg_mdi_c22(struct mii_bus * bus,int phy_addr,int regnum,u16 value)60a1cf597bSMengyuan Lou static int ngbe_phy_write_reg_mdi_c22(struct mii_bus *bus, int phy_addr, int regnum, u16 value)
61a1cf597bSMengyuan Lou {
62a1cf597bSMengyuan Lou 	u32 command, val, device_type = 0;
63a1cf597bSMengyuan Lou 	struct wx *wx = bus->priv;
64a1cf597bSMengyuan Lou 	int ret;
65a1cf597bSMengyuan Lou 
66a1cf597bSMengyuan Lou 	wr32(wx, NGBE_MDIO_CLAUSE_SELECT, 0xF);
67a1cf597bSMengyuan Lou 	/* setup and write the address cycle command */
68ad63f7aaSJiawen Wu 	command = WX_MSCA_RA(regnum) |
69ad63f7aaSJiawen Wu 		  WX_MSCA_PA(phy_addr) |
70ad63f7aaSJiawen Wu 		  WX_MSCA_DA(device_type);
71ad63f7aaSJiawen Wu 	wr32(wx, WX_MSCA, command);
72a1cf597bSMengyuan Lou 	command = value |
73ad63f7aaSJiawen Wu 		  WX_MSCC_CMD(WX_MSCA_CMD_WRITE) |
74ad63f7aaSJiawen Wu 		  WX_MSCC_BUSY |
75ad63f7aaSJiawen Wu 		  WX_MDIO_CLK(6);
76ad63f7aaSJiawen Wu 	wr32(wx, WX_MSCC, command);
77a1cf597bSMengyuan Lou 
78a1cf597bSMengyuan Lou 	/* wait to complete */
79ad63f7aaSJiawen Wu 	ret = read_poll_timeout(rd32, val, !(val & WX_MSCC_BUSY), 1000,
80ad63f7aaSJiawen Wu 				100000, false, wx, WX_MSCC);
81a1cf597bSMengyuan Lou 	if (ret)
82a1cf597bSMengyuan Lou 		wx_err(wx, "Mdio write c22 command did not complete.\n");
83a1cf597bSMengyuan Lou 
84a1cf597bSMengyuan Lou 	return ret;
85a1cf597bSMengyuan Lou }
86a1cf597bSMengyuan Lou 
ngbe_phy_read_reg_mdi_c45(struct mii_bus * bus,int phy_addr,int devnum,int regnum)87a1cf597bSMengyuan Lou static int ngbe_phy_read_reg_mdi_c45(struct mii_bus *bus, int phy_addr, int devnum, int regnum)
88a1cf597bSMengyuan Lou {
89a1cf597bSMengyuan Lou 	struct wx *wx = bus->priv;
90a1cf597bSMengyuan Lou 	u32 val, command;
91a1cf597bSMengyuan Lou 	int ret;
92a1cf597bSMengyuan Lou 
93a1cf597bSMengyuan Lou 	wr32(wx, NGBE_MDIO_CLAUSE_SELECT, 0x0);
94a1cf597bSMengyuan Lou 	/* setup and write the address cycle command */
95ad63f7aaSJiawen Wu 	command = WX_MSCA_RA(regnum) |
96ad63f7aaSJiawen Wu 		  WX_MSCA_PA(phy_addr) |
97ad63f7aaSJiawen Wu 		  WX_MSCA_DA(devnum);
98ad63f7aaSJiawen Wu 	wr32(wx, WX_MSCA, command);
99ad63f7aaSJiawen Wu 	command = WX_MSCC_CMD(WX_MSCA_CMD_READ) |
100ad63f7aaSJiawen Wu 		  WX_MSCC_BUSY |
101ad63f7aaSJiawen Wu 		  WX_MDIO_CLK(6);
102ad63f7aaSJiawen Wu 	wr32(wx, WX_MSCC, command);
103a1cf597bSMengyuan Lou 
104a1cf597bSMengyuan Lou 	/* wait to complete */
105ad63f7aaSJiawen Wu 	ret = read_poll_timeout(rd32, val, !(val & WX_MSCC_BUSY), 1000,
106ad63f7aaSJiawen Wu 				100000, false, wx, WX_MSCC);
107a1cf597bSMengyuan Lou 	if (ret) {
108a1cf597bSMengyuan Lou 		wx_err(wx, "Mdio read c45 command did not complete.\n");
109a1cf597bSMengyuan Lou 		return ret;
110a1cf597bSMengyuan Lou 	}
111a1cf597bSMengyuan Lou 
112ad63f7aaSJiawen Wu 	return (u16)rd32(wx, WX_MSCC);
113a1cf597bSMengyuan Lou }
114a1cf597bSMengyuan Lou 
ngbe_phy_write_reg_mdi_c45(struct mii_bus * bus,int phy_addr,int devnum,int regnum,u16 value)115a1cf597bSMengyuan Lou static int ngbe_phy_write_reg_mdi_c45(struct mii_bus *bus, int phy_addr,
116a1cf597bSMengyuan Lou 				      int devnum, int regnum, u16 value)
117a1cf597bSMengyuan Lou {
118a1cf597bSMengyuan Lou 	struct wx *wx = bus->priv;
119a1cf597bSMengyuan Lou 	int ret, command;
120a1cf597bSMengyuan Lou 	u16 val;
121a1cf597bSMengyuan Lou 
122a1cf597bSMengyuan Lou 	wr32(wx, NGBE_MDIO_CLAUSE_SELECT, 0x0);
123a1cf597bSMengyuan Lou 	/* setup and write the address cycle command */
124ad63f7aaSJiawen Wu 	command = WX_MSCA_RA(regnum) |
125ad63f7aaSJiawen Wu 		  WX_MSCA_PA(phy_addr) |
126ad63f7aaSJiawen Wu 		  WX_MSCA_DA(devnum);
127ad63f7aaSJiawen Wu 	wr32(wx, WX_MSCA, command);
128a1cf597bSMengyuan Lou 	command = value |
129ad63f7aaSJiawen Wu 		  WX_MSCC_CMD(WX_MSCA_CMD_WRITE) |
130ad63f7aaSJiawen Wu 		  WX_MSCC_BUSY |
131ad63f7aaSJiawen Wu 		  WX_MDIO_CLK(6);
132ad63f7aaSJiawen Wu 	wr32(wx, WX_MSCC, command);
133a1cf597bSMengyuan Lou 
134a1cf597bSMengyuan Lou 	/* wait to complete */
135ad63f7aaSJiawen Wu 	ret = read_poll_timeout(rd32, val, !(val & WX_MSCC_BUSY), 1000,
136ad63f7aaSJiawen Wu 				100000, false, wx, WX_MSCC);
137a1cf597bSMengyuan Lou 	if (ret)
138a1cf597bSMengyuan Lou 		wx_err(wx, "Mdio write c45 command did not complete.\n");
139a1cf597bSMengyuan Lou 
140a1cf597bSMengyuan Lou 	return ret;
141a1cf597bSMengyuan Lou }
142a1cf597bSMengyuan Lou 
ngbe_phy_read_reg_c22(struct mii_bus * bus,int phy_addr,int regnum)143a1cf597bSMengyuan Lou static int ngbe_phy_read_reg_c22(struct mii_bus *bus, int phy_addr, int regnum)
144a1cf597bSMengyuan Lou {
145a1cf597bSMengyuan Lou 	struct wx *wx = bus->priv;
146a1cf597bSMengyuan Lou 	u16 phy_data;
147a1cf597bSMengyuan Lou 
148a1cf597bSMengyuan Lou 	if (wx->mac_type == em_mac_type_mdi)
149a1cf597bSMengyuan Lou 		phy_data = ngbe_phy_read_reg_internal(bus, phy_addr, regnum);
150a1cf597bSMengyuan Lou 	else
151a1cf597bSMengyuan Lou 		phy_data = ngbe_phy_read_reg_mdi_c22(bus, phy_addr, regnum);
152a1cf597bSMengyuan Lou 
153a1cf597bSMengyuan Lou 	return phy_data;
154a1cf597bSMengyuan Lou }
155a1cf597bSMengyuan Lou 
ngbe_phy_write_reg_c22(struct mii_bus * bus,int phy_addr,int regnum,u16 value)156a1cf597bSMengyuan Lou static int ngbe_phy_write_reg_c22(struct mii_bus *bus, int phy_addr,
157a1cf597bSMengyuan Lou 				  int regnum, u16 value)
158a1cf597bSMengyuan Lou {
159a1cf597bSMengyuan Lou 	struct wx *wx = bus->priv;
160a1cf597bSMengyuan Lou 	int ret;
161a1cf597bSMengyuan Lou 
162a1cf597bSMengyuan Lou 	if (wx->mac_type == em_mac_type_mdi)
163a1cf597bSMengyuan Lou 		ret = ngbe_phy_write_reg_internal(bus, phy_addr, regnum, value);
164a1cf597bSMengyuan Lou 	else
165a1cf597bSMengyuan Lou 		ret = ngbe_phy_write_reg_mdi_c22(bus, phy_addr, regnum, value);
166a1cf597bSMengyuan Lou 
167a1cf597bSMengyuan Lou 	return ret;
168a1cf597bSMengyuan Lou }
169a1cf597bSMengyuan Lou 
ngbe_handle_link_change(struct net_device * dev)170a1cf597bSMengyuan Lou static void ngbe_handle_link_change(struct net_device *dev)
171a1cf597bSMengyuan Lou {
172a1cf597bSMengyuan Lou 	struct wx *wx = netdev_priv(dev);
173a1cf597bSMengyuan Lou 	struct phy_device *phydev;
174a1cf597bSMengyuan Lou 	u32 lan_speed, reg;
175a1cf597bSMengyuan Lou 
176a1cf597bSMengyuan Lou 	phydev = wx->phydev;
177a1cf597bSMengyuan Lou 	if (!(wx->link != phydev->link ||
178a1cf597bSMengyuan Lou 	      wx->speed != phydev->speed ||
179a1cf597bSMengyuan Lou 	      wx->duplex != phydev->duplex))
180a1cf597bSMengyuan Lou 		return;
181a1cf597bSMengyuan Lou 
182a1cf597bSMengyuan Lou 	wx->link = phydev->link;
183a1cf597bSMengyuan Lou 	wx->speed = phydev->speed;
184a1cf597bSMengyuan Lou 	wx->duplex = phydev->duplex;
185a1cf597bSMengyuan Lou 	switch (phydev->speed) {
186a1cf597bSMengyuan Lou 	case SPEED_10:
187a1cf597bSMengyuan Lou 		lan_speed = 0;
188a1cf597bSMengyuan Lou 		break;
189a1cf597bSMengyuan Lou 	case SPEED_100:
190a1cf597bSMengyuan Lou 		lan_speed = 1;
191a1cf597bSMengyuan Lou 		break;
192a1cf597bSMengyuan Lou 	case SPEED_1000:
193a1cf597bSMengyuan Lou 	default:
194a1cf597bSMengyuan Lou 		lan_speed = 2;
195a1cf597bSMengyuan Lou 		break;
196a1cf597bSMengyuan Lou 	}
197a1cf597bSMengyuan Lou 	wr32m(wx, NGBE_CFG_LAN_SPEED, 0x3, lan_speed);
198a1cf597bSMengyuan Lou 
199a1cf597bSMengyuan Lou 	if (phydev->link) {
200a1cf597bSMengyuan Lou 		reg = rd32(wx, WX_MAC_TX_CFG);
201a1cf597bSMengyuan Lou 		reg &= ~WX_MAC_TX_CFG_SPEED_MASK;
202a1cf597bSMengyuan Lou 		reg |= WX_MAC_TX_CFG_SPEED_1G | WX_MAC_TX_CFG_TE;
203a1cf597bSMengyuan Lou 		wr32(wx, WX_MAC_TX_CFG, reg);
204a1cf597bSMengyuan Lou 		/* Re configure MAC RX */
205a1cf597bSMengyuan Lou 		reg = rd32(wx, WX_MAC_RX_CFG);
206a1cf597bSMengyuan Lou 		wr32(wx, WX_MAC_RX_CFG, reg);
207a1cf597bSMengyuan Lou 		wr32(wx, WX_MAC_PKT_FLT, WX_MAC_PKT_FLT_PR);
208a1cf597bSMengyuan Lou 		reg = rd32(wx, WX_MAC_WDG_TIMEOUT);
209a1cf597bSMengyuan Lou 		wr32(wx, WX_MAC_WDG_TIMEOUT, reg);
210a1cf597bSMengyuan Lou 	}
211a1cf597bSMengyuan Lou 	phy_print_status(phydev);
212a1cf597bSMengyuan Lou }
213a1cf597bSMengyuan Lou 
ngbe_phy_connect(struct wx * wx)214a1cf597bSMengyuan Lou int ngbe_phy_connect(struct wx *wx)
215a1cf597bSMengyuan Lou {
216a1cf597bSMengyuan Lou 	int ret;
217a1cf597bSMengyuan Lou 
218*b8d7b897SMengyuan Lou 	/* The MAC only has add the Tx delay and it can not be modified.
219*b8d7b897SMengyuan Lou 	 * So just disable TX delay in PHY, and it is does not matter to
220*b8d7b897SMengyuan Lou 	 * internal phy.
221*b8d7b897SMengyuan Lou 	 */
222a1cf597bSMengyuan Lou 	ret = phy_connect_direct(wx->netdev,
223a1cf597bSMengyuan Lou 				 wx->phydev,
224a1cf597bSMengyuan Lou 				 ngbe_handle_link_change,
225*b8d7b897SMengyuan Lou 				 PHY_INTERFACE_MODE_RGMII_RXID);
226a1cf597bSMengyuan Lou 	if (ret) {
227a1cf597bSMengyuan Lou 		wx_err(wx, "PHY connect failed.\n");
228a1cf597bSMengyuan Lou 		return ret;
229a1cf597bSMengyuan Lou 	}
230a1cf597bSMengyuan Lou 
231a1cf597bSMengyuan Lou 	return 0;
232a1cf597bSMengyuan Lou }
233a1cf597bSMengyuan Lou 
ngbe_phy_fixup(struct wx * wx)234a1cf597bSMengyuan Lou static void ngbe_phy_fixup(struct wx *wx)
235a1cf597bSMengyuan Lou {
236a1cf597bSMengyuan Lou 	struct phy_device *phydev = wx->phydev;
237a1cf597bSMengyuan Lou 	struct ethtool_eee eee;
238a1cf597bSMengyuan Lou 
239a1cf597bSMengyuan Lou 	phy_remove_link_mode(phydev, ETHTOOL_LINK_MODE_10baseT_Half_BIT);
240a1cf597bSMengyuan Lou 	phy_remove_link_mode(phydev, ETHTOOL_LINK_MODE_100baseT_Half_BIT);
241a1cf597bSMengyuan Lou 	phy_remove_link_mode(phydev, ETHTOOL_LINK_MODE_1000baseT_Half_BIT);
242a1cf597bSMengyuan Lou 
2436963e463SMengyuan Lou 	phydev->mac_managed_pm = true;
244a1cf597bSMengyuan Lou 	if (wx->mac_type != em_mac_type_mdi)
245a1cf597bSMengyuan Lou 		return;
246a1cf597bSMengyuan Lou 	/* disable EEE, internal phy does not support eee */
247a1cf597bSMengyuan Lou 	memset(&eee, 0, sizeof(eee));
248a1cf597bSMengyuan Lou 	phy_ethtool_set_eee(phydev, &eee);
249a1cf597bSMengyuan Lou }
250a1cf597bSMengyuan Lou 
ngbe_mdio_init(struct wx * wx)251a1cf597bSMengyuan Lou int ngbe_mdio_init(struct wx *wx)
252a1cf597bSMengyuan Lou {
253a1cf597bSMengyuan Lou 	struct pci_dev *pdev = wx->pdev;
254a1cf597bSMengyuan Lou 	struct mii_bus *mii_bus;
255a1cf597bSMengyuan Lou 	int ret;
256a1cf597bSMengyuan Lou 
257a1cf597bSMengyuan Lou 	mii_bus = devm_mdiobus_alloc(&pdev->dev);
258a1cf597bSMengyuan Lou 	if (!mii_bus)
259a1cf597bSMengyuan Lou 		return -ENOMEM;
260a1cf597bSMengyuan Lou 
261a1cf597bSMengyuan Lou 	mii_bus->name = "ngbe_mii_bus";
262a1cf597bSMengyuan Lou 	mii_bus->read = ngbe_phy_read_reg_c22;
263a1cf597bSMengyuan Lou 	mii_bus->write = ngbe_phy_write_reg_c22;
264a1cf597bSMengyuan Lou 	mii_bus->phy_mask = GENMASK(31, 4);
265a1cf597bSMengyuan Lou 	mii_bus->parent = &pdev->dev;
266a1cf597bSMengyuan Lou 	mii_bus->priv = wx;
267a1cf597bSMengyuan Lou 
268a1cf597bSMengyuan Lou 	if (wx->mac_type == em_mac_type_rgmii) {
269a1cf597bSMengyuan Lou 		mii_bus->read_c45 = ngbe_phy_read_reg_mdi_c45;
270a1cf597bSMengyuan Lou 		mii_bus->write_c45 = ngbe_phy_write_reg_mdi_c45;
271a1cf597bSMengyuan Lou 	}
272a1cf597bSMengyuan Lou 
273cf9b107fSZheng Zengkai 	snprintf(mii_bus->id, MII_BUS_ID_SIZE, "ngbe-%x", pci_dev_id(pdev));
274a1cf597bSMengyuan Lou 	ret = devm_mdiobus_register(&pdev->dev, mii_bus);
275a1cf597bSMengyuan Lou 	if (ret)
276a1cf597bSMengyuan Lou 		return ret;
277a1cf597bSMengyuan Lou 
278a1cf597bSMengyuan Lou 	wx->phydev = phy_find_first(mii_bus);
279a1cf597bSMengyuan Lou 	if (!wx->phydev)
280a1cf597bSMengyuan Lou 		return -ENODEV;
281a1cf597bSMengyuan Lou 
282a1cf597bSMengyuan Lou 	phy_attached_info(wx->phydev);
283a1cf597bSMengyuan Lou 	ngbe_phy_fixup(wx);
284a1cf597bSMengyuan Lou 
285a1cf597bSMengyuan Lou 	wx->link = 0;
286a1cf597bSMengyuan Lou 	wx->speed = 0;
287a1cf597bSMengyuan Lou 	wx->duplex = 0;
288a1cf597bSMengyuan Lou 
289a1cf597bSMengyuan Lou 	return 0;
290a1cf597bSMengyuan Lou }
291