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