1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2010 ASIX Electronics Corporation
4 * Copyright (c) 2020 Samsung Electronics Co., Ltd.
6 * ASIX AX88796C SPI Fast Ethernet Linux driver
9 #define pr_fmt(fmt) "ax88796c: " fmt
11 #include <linux/bitmap.h>
12 #include <linux/iopoll.h>
13 #include <linux/phy.h>
14 #include <linux/netdevice.h>
16 #include "ax88796c_main.h"
17 #include "ax88796c_ioctl.h"
19 static const char ax88796c_priv_flag_names[][ETH_GSTRING_LEN] = {
24 ax88796c_get_drvinfo(struct net_device *ndev, struct ethtool_drvinfo *info)
26 /* Inherit standard device info */
27 strscpy(info->driver, DRV_NAME, sizeof(info->driver));
30 static u32 ax88796c_get_msglevel(struct net_device *ndev)
32 struct ax88796c_device *ax_local = to_ax88796c_device(ndev);
34 return ax_local->msg_enable;
37 static void ax88796c_set_msglevel(struct net_device *ndev, u32 level)
39 struct ax88796c_device *ax_local = to_ax88796c_device(ndev);
41 ax_local->msg_enable = level;
45 ax88796c_get_pauseparam(struct net_device *ndev, struct ethtool_pauseparam *pause)
47 struct ax88796c_device *ax_local = to_ax88796c_device(ndev);
49 pause->tx_pause = !!(ax_local->flowctrl & AX_FC_TX);
50 pause->rx_pause = !!(ax_local->flowctrl & AX_FC_RX);
51 pause->autoneg = (ax_local->flowctrl & AX_FC_ANEG) ?
57 ax88796c_set_pauseparam(struct net_device *ndev, struct ethtool_pauseparam *pause)
59 struct ax88796c_device *ax_local = to_ax88796c_device(ndev);
62 /* The following logic comes from phylink_ethtool_set_pauseparam() */
63 fc = pause->tx_pause ? AX_FC_TX : 0;
64 fc |= pause->rx_pause ? AX_FC_RX : 0;
65 fc |= pause->autoneg ? AX_FC_ANEG : 0;
67 ax_local->flowctrl = fc;
70 phy_set_asym_pause(ax_local->phydev, pause->tx_pause,
75 phy_set_asym_pause(ax_local->phydev, 0, 0);
76 maccr |= (ax_local->flowctrl & AX_FC_RX) ? MACCR_RXFC_ENABLE : 0;
77 maccr |= (ax_local->flowctrl & AX_FC_TX) ? MACCR_TXFC_ENABLE : 0;
79 mutex_lock(&ax_local->spi_lock);
81 maccr |= AX_READ(&ax_local->ax_spi, P0_MACCR) &
82 ~(MACCR_TXFC_ENABLE | MACCR_RXFC_ENABLE);
83 AX_WRITE(&ax_local->ax_spi, maccr, P0_MACCR);
85 mutex_unlock(&ax_local->spi_lock);
91 static int ax88796c_get_regs_len(struct net_device *ndev)
93 return AX88796C_REGDUMP_LEN + AX88796C_PHY_REGDUMP_LEN;
97 ax88796c_get_regs(struct net_device *ndev, struct ethtool_regs *regs, void *_p)
99 struct ax88796c_device *ax_local = to_ax88796c_device(ndev);
103 memset(p, 0, ax88796c_get_regs_len(ndev));
105 mutex_lock(&ax_local->spi_lock);
107 for (offset = 0; offset < AX88796C_REGDUMP_LEN; offset += 2) {
108 if (!test_bit(offset / 2, ax88796c_no_regs_mask))
109 *p = AX_READ(&ax_local->ax_spi, offset);
113 mutex_unlock(&ax_local->spi_lock);
115 for (i = 0; i < AX88796C_PHY_REGDUMP_LEN / 2; i++) {
116 *p = phy_read(ax_local->phydev, i);
122 ax88796c_get_strings(struct net_device *ndev, u32 stringset, u8 *data)
125 case ETH_SS_PRIV_FLAGS:
126 memcpy(data, ax88796c_priv_flag_names,
127 sizeof(ax88796c_priv_flag_names));
133 ax88796c_get_sset_count(struct net_device *ndev, int stringset)
138 case ETH_SS_PRIV_FLAGS:
139 ret = ARRAY_SIZE(ax88796c_priv_flag_names);
148 static int ax88796c_set_priv_flags(struct net_device *ndev, u32 flags)
150 struct ax88796c_device *ax_local = to_ax88796c_device(ndev);
152 if (flags & ~AX_PRIV_FLAGS_MASK)
155 if ((ax_local->priv_flags ^ flags) & AX_CAP_COMP)
156 if (netif_running(ndev))
159 ax_local->priv_flags = flags;
164 static u32 ax88796c_get_priv_flags(struct net_device *ndev)
166 struct ax88796c_device *ax_local = to_ax88796c_device(ndev);
168 return ax_local->priv_flags;
171 int ax88796c_mdio_read(struct mii_bus *mdiobus, int phy_id, int loc)
173 struct ax88796c_device *ax_local = mdiobus->priv;
176 mutex_lock(&ax_local->spi_lock);
177 AX_WRITE(&ax_local->ax_spi, MDIOCR_RADDR(loc)
178 | MDIOCR_FADDR(phy_id) | MDIOCR_READ, P2_MDIOCR);
180 ret = read_poll_timeout(AX_READ, ret,
182 0, jiffies_to_usecs(HZ / 100), false,
183 &ax_local->ax_spi, P2_MDIOCR);
185 ret = AX_READ(&ax_local->ax_spi, P2_MDIODR);
187 mutex_unlock(&ax_local->spi_lock);
193 ax88796c_mdio_write(struct mii_bus *mdiobus, int phy_id, int loc, u16 val)
195 struct ax88796c_device *ax_local = mdiobus->priv;
198 mutex_lock(&ax_local->spi_lock);
199 AX_WRITE(&ax_local->ax_spi, val, P2_MDIODR);
201 AX_WRITE(&ax_local->ax_spi,
202 MDIOCR_RADDR(loc) | MDIOCR_FADDR(phy_id)
203 | MDIOCR_WRITE, P2_MDIOCR);
205 ret = read_poll_timeout(AX_READ, ret,
206 ((ret & MDIOCR_VALID) != 0), 0,
207 jiffies_to_usecs(HZ / 100), false,
208 &ax_local->ax_spi, P2_MDIOCR);
209 mutex_unlock(&ax_local->spi_lock);
214 const struct ethtool_ops ax88796c_ethtool_ops = {
215 .get_drvinfo = ax88796c_get_drvinfo,
216 .get_link = ethtool_op_get_link,
217 .get_msglevel = ax88796c_get_msglevel,
218 .set_msglevel = ax88796c_set_msglevel,
219 .get_link_ksettings = phy_ethtool_get_link_ksettings,
220 .set_link_ksettings = phy_ethtool_set_link_ksettings,
221 .nway_reset = phy_ethtool_nway_reset,
222 .get_pauseparam = ax88796c_get_pauseparam,
223 .set_pauseparam = ax88796c_set_pauseparam,
224 .get_regs_len = ax88796c_get_regs_len,
225 .get_regs = ax88796c_get_regs,
226 .get_strings = ax88796c_get_strings,
227 .get_sset_count = ax88796c_get_sset_count,
228 .get_priv_flags = ax88796c_get_priv_flags,
229 .set_priv_flags = ax88796c_set_priv_flags,
232 int ax88796c_ioctl(struct net_device *ndev, struct ifreq *ifr, int cmd)
236 ret = phy_mii_ioctl(ndev->phydev, ifr, cmd);