blob: 6101d82102e79e7239622007c07a87791f6ac51f [file] [log] [blame]
David Brazdil0f672f62019-12-10 10:32:29 +00001// SPDX-License-Identifier: GPL-2.0-or-later
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002/*
3 * ASIX AX88172A based USB 2.0 Ethernet Devices
4 * Copyright (C) 2012 OMICRON electronics GmbH
5 *
6 * Supports external PHYs via phylib. Based on the driver for the
7 * AX88772. Original copyrights follow:
8 *
9 * Copyright (C) 2003-2006 David Hollis <dhollis@davehollis.com>
10 * Copyright (C) 2005 Phil Chang <pchang23@sbcglobal.net>
11 * Copyright (C) 2006 James Painter <jamie.painter@iname.com>
12 * Copyright (c) 2002-2003 TiVo Inc.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000013 */
14
15#include "asix.h"
16#include <linux/phy.h>
17
18struct ax88172a_private {
19 struct mii_bus *mdio;
20 struct phy_device *phydev;
21 char phy_name[20];
22 u16 phy_addr;
23 u16 oldmode;
24 int use_embdphy;
25 struct asix_rx_fixup_info rx_fixup_info;
26};
27
28/* MDIO read and write wrappers for phylib */
29static int asix_mdio_bus_read(struct mii_bus *bus, int phy_id, int regnum)
30{
31 return asix_mdio_read(((struct usbnet *)bus->priv)->net, phy_id,
32 regnum);
33}
34
35static int asix_mdio_bus_write(struct mii_bus *bus, int phy_id, int regnum,
36 u16 val)
37{
38 asix_mdio_write(((struct usbnet *)bus->priv)->net, phy_id, regnum, val);
39 return 0;
40}
41
42static int ax88172a_ioctl(struct net_device *net, struct ifreq *rq, int cmd)
43{
44 if (!netif_running(net))
45 return -EINVAL;
46
47 if (!net->phydev)
48 return -ENODEV;
49
50 return phy_mii_ioctl(net->phydev, rq, cmd);
51}
52
53/* set MAC link settings according to information from phylib */
54static void ax88172a_adjust_link(struct net_device *netdev)
55{
56 struct phy_device *phydev = netdev->phydev;
57 struct usbnet *dev = netdev_priv(netdev);
58 struct ax88172a_private *priv = dev->driver_priv;
59 u16 mode = 0;
60
61 if (phydev->link) {
62 mode = AX88772_MEDIUM_DEFAULT;
63
64 if (phydev->duplex == DUPLEX_HALF)
65 mode &= ~AX_MEDIUM_FD;
66
67 if (phydev->speed != SPEED_100)
68 mode &= ~AX_MEDIUM_PS;
69 }
70
71 if (mode != priv->oldmode) {
72 asix_write_medium_mode(dev, mode, 0);
73 priv->oldmode = mode;
74 netdev_dbg(netdev, "speed %u duplex %d, setting mode to 0x%04x\n",
75 phydev->speed, phydev->duplex, mode);
76 phy_print_status(phydev);
77 }
78}
79
80static void ax88172a_status(struct usbnet *dev, struct urb *urb)
81{
82 /* link changes are detected by polling the phy */
83}
84
85/* use phylib infrastructure */
86static int ax88172a_init_mdio(struct usbnet *dev)
87{
88 struct ax88172a_private *priv = dev->driver_priv;
89 int ret;
90
91 priv->mdio = mdiobus_alloc();
92 if (!priv->mdio) {
93 netdev_err(dev->net, "Could not allocate MDIO bus\n");
94 return -ENOMEM;
95 }
96
97 priv->mdio->priv = (void *)dev;
98 priv->mdio->read = &asix_mdio_bus_read;
99 priv->mdio->write = &asix_mdio_bus_write;
100 priv->mdio->name = "Asix MDIO Bus";
101 /* mii bus name is usb-<usb bus number>-<usb device number> */
102 snprintf(priv->mdio->id, MII_BUS_ID_SIZE, "usb-%03d:%03d",
103 dev->udev->bus->busnum, dev->udev->devnum);
104
105 ret = mdiobus_register(priv->mdio);
106 if (ret) {
107 netdev_err(dev->net, "Could not register MDIO bus\n");
108 goto mfree;
109 }
110
111 netdev_info(dev->net, "registered mdio bus %s\n", priv->mdio->id);
112 return 0;
113
114mfree:
115 mdiobus_free(priv->mdio);
116 return ret;
117}
118
119static void ax88172a_remove_mdio(struct usbnet *dev)
120{
121 struct ax88172a_private *priv = dev->driver_priv;
122
123 netdev_info(dev->net, "deregistering mdio bus %s\n", priv->mdio->id);
124 mdiobus_unregister(priv->mdio);
125 mdiobus_free(priv->mdio);
126}
127
128static const struct net_device_ops ax88172a_netdev_ops = {
129 .ndo_open = usbnet_open,
130 .ndo_stop = usbnet_stop,
131 .ndo_start_xmit = usbnet_start_xmit,
132 .ndo_tx_timeout = usbnet_tx_timeout,
133 .ndo_change_mtu = usbnet_change_mtu,
134 .ndo_get_stats64 = usbnet_get_stats64,
135 .ndo_set_mac_address = asix_set_mac_address,
136 .ndo_validate_addr = eth_validate_addr,
137 .ndo_do_ioctl = ax88172a_ioctl,
138 .ndo_set_rx_mode = asix_set_multicast,
139};
140
141static const struct ethtool_ops ax88172a_ethtool_ops = {
142 .get_drvinfo = asix_get_drvinfo,
143 .get_link = usbnet_get_link,
144 .get_msglevel = usbnet_get_msglevel,
145 .set_msglevel = usbnet_set_msglevel,
146 .get_wol = asix_get_wol,
147 .set_wol = asix_set_wol,
148 .get_eeprom_len = asix_get_eeprom_len,
149 .get_eeprom = asix_get_eeprom,
150 .set_eeprom = asix_set_eeprom,
151 .nway_reset = phy_ethtool_nway_reset,
152 .get_link_ksettings = phy_ethtool_get_link_ksettings,
153 .set_link_ksettings = phy_ethtool_set_link_ksettings,
154};
155
156static int ax88172a_reset_phy(struct usbnet *dev, int embd_phy)
157{
158 int ret;
159
160 ret = asix_sw_reset(dev, AX_SWRESET_IPPD, 0);
161 if (ret < 0)
162 goto err;
163
164 msleep(150);
165 ret = asix_sw_reset(dev, AX_SWRESET_CLEAR, 0);
166 if (ret < 0)
167 goto err;
168
169 msleep(150);
170
171 ret = asix_sw_reset(dev, embd_phy ? AX_SWRESET_IPRL : AX_SWRESET_IPPD,
172 0);
173 if (ret < 0)
174 goto err;
175
176 return 0;
177
178err:
179 return ret;
180}
181
182
183static int ax88172a_bind(struct usbnet *dev, struct usb_interface *intf)
184{
185 int ret;
186 u8 buf[ETH_ALEN];
187 struct ax88172a_private *priv;
188
189 usbnet_get_endpoints(dev, intf);
190
191 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
192 if (!priv)
193 return -ENOMEM;
194
195 dev->driver_priv = priv;
196
197 /* Get the MAC address */
198 ret = asix_read_cmd(dev, AX_CMD_READ_NODE_ID, 0, 0, ETH_ALEN, buf, 0);
David Brazdil0f672f62019-12-10 10:32:29 +0000199 if (ret < ETH_ALEN) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000200 netdev_err(dev->net, "Failed to read MAC address: %d\n", ret);
Olivier Deprez0e641232021-09-23 10:07:05 +0200201 ret = -EIO;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000202 goto free;
203 }
204 memcpy(dev->net->dev_addr, buf, ETH_ALEN);
205
206 dev->net->netdev_ops = &ax88172a_netdev_ops;
207 dev->net->ethtool_ops = &ax88172a_ethtool_ops;
208
209 /* are we using the internal or the external phy? */
210 ret = asix_read_cmd(dev, AX_CMD_SW_PHY_STATUS, 0, 0, 1, buf, 0);
211 if (ret < 0) {
212 netdev_err(dev->net, "Failed to read software interface selection register: %d\n",
213 ret);
214 goto free;
215 }
216
217 netdev_dbg(dev->net, "AX_CMD_SW_PHY_STATUS = 0x%02x\n", buf[0]);
218 switch (buf[0] & AX_PHY_SELECT_MASK) {
219 case AX_PHY_SELECT_INTERNAL:
220 netdev_dbg(dev->net, "use internal phy\n");
221 priv->use_embdphy = 1;
222 break;
223 case AX_PHY_SELECT_EXTERNAL:
224 netdev_dbg(dev->net, "use external phy\n");
225 priv->use_embdphy = 0;
226 break;
227 default:
228 netdev_err(dev->net, "Interface mode not supported by driver\n");
229 ret = -ENOTSUPP;
230 goto free;
231 }
232
233 priv->phy_addr = asix_read_phy_addr(dev, priv->use_embdphy);
234 ax88172a_reset_phy(dev, priv->use_embdphy);
235
236 /* Asix framing packs multiple eth frames into a 2K usb bulk transfer */
237 if (dev->driver_info->flags & FLAG_FRAMING_AX) {
238 /* hard_mtu is still the default - the device does not support
239 jumbo eth frames */
240 dev->rx_urb_size = 2048;
241 }
242
243 /* init MDIO bus */
244 ret = ax88172a_init_mdio(dev);
245 if (ret)
246 goto free;
247
248 return 0;
249
250free:
251 kfree(priv);
252 return ret;
253}
254
255static int ax88172a_stop(struct usbnet *dev)
256{
257 struct ax88172a_private *priv = dev->driver_priv;
258
259 netdev_dbg(dev->net, "Stopping interface\n");
260
261 if (priv->phydev) {
262 netdev_info(dev->net, "Disconnecting from phy %s\n",
263 priv->phy_name);
264 phy_stop(priv->phydev);
265 phy_disconnect(priv->phydev);
266 }
267
268 return 0;
269}
270
271static void ax88172a_unbind(struct usbnet *dev, struct usb_interface *intf)
272{
273 struct ax88172a_private *priv = dev->driver_priv;
274
275 ax88172a_remove_mdio(dev);
276 kfree(priv);
277}
278
279static int ax88172a_reset(struct usbnet *dev)
280{
281 struct asix_data *data = (struct asix_data *)&dev->data;
282 struct ax88172a_private *priv = dev->driver_priv;
283 int ret;
284 u16 rx_ctl;
285
286 ax88172a_reset_phy(dev, priv->use_embdphy);
287
288 msleep(150);
289 rx_ctl = asix_read_rx_ctl(dev, 0);
290 netdev_dbg(dev->net, "RX_CTL is 0x%04x after software reset\n", rx_ctl);
291 ret = asix_write_rx_ctl(dev, 0x0000, 0);
292 if (ret < 0)
293 goto out;
294
295 rx_ctl = asix_read_rx_ctl(dev, 0);
296 netdev_dbg(dev->net, "RX_CTL is 0x%04x setting to 0x0000\n", rx_ctl);
297
298 msleep(150);
299
300 ret = asix_write_cmd(dev, AX_CMD_WRITE_IPG0,
301 AX88772_IPG0_DEFAULT | AX88772_IPG1_DEFAULT,
302 AX88772_IPG2_DEFAULT, 0, NULL, 0);
303 if (ret < 0) {
304 netdev_err(dev->net, "Write IPG,IPG1,IPG2 failed: %d\n", ret);
305 goto out;
306 }
307
308 /* Rewrite MAC address */
309 memcpy(data->mac_addr, dev->net->dev_addr, ETH_ALEN);
310 ret = asix_write_cmd(dev, AX_CMD_WRITE_NODE_ID, 0, 0, ETH_ALEN,
311 data->mac_addr, 0);
312 if (ret < 0)
313 goto out;
314
315 /* Set RX_CTL to default values with 2k buffer, and enable cactus */
316 ret = asix_write_rx_ctl(dev, AX_DEFAULT_RX_CTL, 0);
317 if (ret < 0)
318 goto out;
319
320 rx_ctl = asix_read_rx_ctl(dev, 0);
321 netdev_dbg(dev->net, "RX_CTL is 0x%04x after all initializations\n",
322 rx_ctl);
323
324 rx_ctl = asix_read_medium_status(dev, 0);
325 netdev_dbg(dev->net, "Medium Status is 0x%04x after all initializations\n",
326 rx_ctl);
327
328 /* Connect to PHY */
329 snprintf(priv->phy_name, 20, PHY_ID_FMT,
330 priv->mdio->id, priv->phy_addr);
331
332 priv->phydev = phy_connect(dev->net, priv->phy_name,
333 &ax88172a_adjust_link,
334 PHY_INTERFACE_MODE_MII);
335 if (IS_ERR(priv->phydev)) {
336 netdev_err(dev->net, "Could not connect to PHY device %s\n",
337 priv->phy_name);
338 ret = PTR_ERR(priv->phydev);
339 goto out;
340 }
341
342 netdev_info(dev->net, "Connected to phy %s\n", priv->phy_name);
343
344 /* During power-up, the AX88172A set the power down (BMCR_PDOWN)
345 * bit of the PHY. Bring the PHY up again.
346 */
347 genphy_resume(priv->phydev);
348 phy_start(priv->phydev);
349
350 return 0;
351
352out:
353 return ret;
354
355}
356
357static int ax88172a_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
358{
359 struct ax88172a_private *dp = dev->driver_priv;
360 struct asix_rx_fixup_info *rx = &dp->rx_fixup_info;
361
362 return asix_rx_fixup_internal(dev, skb, rx);
363}
364
365const struct driver_info ax88172a_info = {
366 .description = "ASIX AX88172A USB 2.0 Ethernet",
367 .bind = ax88172a_bind,
368 .reset = ax88172a_reset,
369 .stop = ax88172a_stop,
370 .unbind = ax88172a_unbind,
371 .status = ax88172a_status,
372 .flags = FLAG_ETHER | FLAG_FRAMING_AX | FLAG_LINK_INTR |
373 FLAG_MULTI_PACKET,
374 .rx_fixup = ax88172a_rx_fixup,
375 .tx_fixup = asix_tx_fixup,
376};