aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/net/usb/ax88172a.c
diff options
context:
space:
mode:
authorChristian Riesch <christian.riesch@omicron.at>2012-07-13 01:26:31 -0400
committerDavid S. Miller <davem@davemloft.net>2012-07-17 02:26:23 -0400
commit16626b0cc3d5afe250850f96759b241f8a403b52 (patch)
tree97cb3cc00d2cebc7d59d6a56d253538d08c8fb02 /drivers/net/usb/ax88172a.c
parent607740bc1605395c96b09416cdcc5e77c8976eed (diff)
asix: Add a new driver for the AX88172A
The Asix AX88172A is a USB 2.0 Ethernet interface that supports both an internal PHY as well as an external PHY (connected via MII). This patch adds a driver for the AX88172A and provides support for both modes and the phylib. Signed-off-by: Christian Riesch <christian.riesch@omicron.at> Signed-off-by: David S. Miller <davem@davemloft.net>
Diffstat (limited to 'drivers/net/usb/ax88172a.c')
-rw-r--r--drivers/net/usb/ax88172a.c415
1 files changed, 415 insertions, 0 deletions
diff --git a/drivers/net/usb/ax88172a.c b/drivers/net/usb/ax88172a.c
new file mode 100644
index 000000000000..534a144bd9e7
--- /dev/null
+++ b/drivers/net/usb/ax88172a.c
@@ -0,0 +1,415 @@
1/*
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
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 */
27
28#include "asix.h"
29#include <linux/phy.h>
30
31struct ax88172a_private {
32 struct mii_bus *mdio;
33 struct phy_device *phydev;
34 char phy_name[20];
35 u16 phy_addr;
36 u16 oldmode;
37 int use_embdphy;
38};
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;
101 int ret, i;
102
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
117 priv->mdio->irq = kzalloc(sizeof(int) * PHY_MAX_ADDR, GFP_KERNEL);
118 if (!priv->mdio->irq) {
119 netdev_err(dev->net, "Could not allocate mdio->irq\n");
120 ret = -ENOMEM;
121 goto mfree;
122 }
123 for (i = 0; i < PHY_MAX_ADDR; i++)
124 priv->mdio->irq[i] = PHY_POLL;
125
126 ret = mdiobus_register(priv->mdio);
127 if (ret) {
128 netdev_err(dev->net, "Could not register MDIO bus\n");
129 goto ifree;
130 }
131
132 netdev_info(dev->net, "registered mdio bus %s\n", priv->mdio->id);
133 return 0;
134
135ifree:
136 kfree(priv->mdio->irq);
137mfree:
138 mdiobus_free(priv->mdio);
139 return ret;
140}
141
142static void ax88172a_remove_mdio(struct usbnet *dev)
143{
144 struct ax88172a_private *priv = dev->driver_priv;
145
146 netdev_info(dev->net, "deregistering mdio bus %s\n", priv->mdio->id);
147 mdiobus_unregister(priv->mdio);
148 kfree(priv->mdio->irq);
149 mdiobus_free(priv->mdio);
150}
151
152static const struct net_device_ops ax88172a_netdev_ops = {
153 .ndo_open = usbnet_open,
154 .ndo_stop = usbnet_stop,
155 .ndo_start_xmit = usbnet_start_xmit,
156 .ndo_tx_timeout = usbnet_tx_timeout,
157 .ndo_change_mtu = usbnet_change_mtu,
158 .ndo_set_mac_address = asix_set_mac_address,
159 .ndo_validate_addr = eth_validate_addr,
160 .ndo_do_ioctl = ax88172a_ioctl,
161 .ndo_set_rx_mode = asix_set_multicast,
162};
163
164int ax88172a_get_settings(struct net_device *net, struct ethtool_cmd *cmd)
165{
166 if (!net->phydev)
167 return -ENODEV;
168
169 return phy_ethtool_gset(net->phydev, cmd);
170}
171
172int ax88172a_set_settings(struct net_device *net, struct ethtool_cmd *cmd)
173{
174 if (!net->phydev)
175 return -ENODEV;
176
177 return phy_ethtool_sset(net->phydev, cmd);
178}
179
180int ax88172a_nway_reset(struct net_device *net)
181{
182 if (!net->phydev)
183 return -ENODEV;
184
185 return phy_start_aneg(net->phydev);
186}
187
188static const struct ethtool_ops ax88172a_ethtool_ops = {
189 .get_drvinfo = asix_get_drvinfo,
190 .get_link = usbnet_get_link,
191 .get_msglevel = usbnet_get_msglevel,
192 .set_msglevel = usbnet_set_msglevel,
193 .get_wol = asix_get_wol,
194 .set_wol = asix_set_wol,
195 .get_eeprom_len = asix_get_eeprom_len,
196 .get_eeprom = asix_get_eeprom,
197 .get_settings = ax88172a_get_settings,
198 .set_settings = ax88172a_set_settings,
199 .nway_reset = ax88172a_nway_reset,
200};
201
202static int ax88172a_reset_phy(struct usbnet *dev, int embd_phy)
203{
204 int ret;
205
206 ret = asix_sw_reset(dev, AX_SWRESET_IPPD);
207 if (ret < 0)
208 goto err;
209
210 msleep(150);
211 ret = asix_sw_reset(dev, AX_SWRESET_CLEAR);
212 if (ret < 0)
213 goto err;
214
215 msleep(150);
216
217 ret = asix_sw_reset(dev, embd_phy ? AX_SWRESET_IPRL : AX_SWRESET_IPPD);
218 if (ret < 0)
219 goto err;
220
221 return 0;
222
223err:
224 return ret;
225}
226
227
228static int ax88172a_bind(struct usbnet *dev, struct usb_interface *intf)
229{
230 int ret;
231 struct asix_data *data = (struct asix_data *)&dev->data;
232 u8 buf[ETH_ALEN];
233 struct ax88172a_private *priv;
234
235 data->eeprom_len = AX88772_EEPROM_LEN;
236
237 usbnet_get_endpoints(dev, intf);
238
239 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
240 if (!priv) {
241 netdev_err(dev->net, "Could not allocate memory for private data\n");
242 return -ENOMEM;
243 }
244 dev->driver_priv = priv;
245
246 /* Get the MAC address */
247 ret = asix_read_cmd(dev, AX_CMD_READ_NODE_ID, 0, 0, ETH_ALEN, buf);
248 if (ret < 0) {
249 netdev_err(dev->net, "Failed to read MAC address: %d\n", ret);
250 goto free;
251 }
252 memcpy(dev->net->dev_addr, buf, ETH_ALEN);
253
254 dev->net->netdev_ops = &ax88172a_netdev_ops;
255 dev->net->ethtool_ops = &ax88172a_ethtool_ops;
256
257 /* are we using the internal or the external phy? */
258 ret = asix_read_cmd(dev, AX_CMD_SW_PHY_STATUS, 0, 0, 1, buf);
259 if (ret < 0) {
260 netdev_err(dev->net, "Failed to read software interface selection register: %d\n",
261 ret);
262 goto free;
263 }
264
265 netdev_dbg(dev->net, "AX_CMD_SW_PHY_STATUS = 0x%02x\n", buf[0]);
266 switch (buf[0] & AX_PHY_SELECT_MASK) {
267 case AX_PHY_SELECT_INTERNAL:
268 netdev_dbg(dev->net, "use internal phy\n");
269 priv->use_embdphy = 1;
270 break;
271 case AX_PHY_SELECT_EXTERNAL:
272 netdev_dbg(dev->net, "use external phy\n");
273 priv->use_embdphy = 0;
274 break;
275 default:
276 netdev_err(dev->net, "Interface mode not supported by driver\n");
277 goto free;
278 }
279
280 priv->phy_addr = asix_read_phy_addr(dev, priv->use_embdphy);
281 ax88172a_reset_phy(dev, priv->use_embdphy);
282
283 /* Asix framing packs multiple eth frames into a 2K usb bulk transfer */
284 if (dev->driver_info->flags & FLAG_FRAMING_AX) {
285 /* hard_mtu is still the default - the device does not support
286 jumbo eth frames */
287 dev->rx_urb_size = 2048;
288 }
289
290 /* init MDIO bus */
291 ret = ax88172a_init_mdio(dev);
292 if (ret)
293 goto free;
294
295 return 0;
296
297free:
298 kfree(priv);
299 return ret;
300}
301
302static int ax88172a_stop(struct usbnet *dev)
303{
304 struct ax88172a_private *priv = dev->driver_priv;
305
306 netdev_dbg(dev->net, "Stopping interface\n");
307
308 if (priv->phydev) {
309 netdev_info(dev->net, "Disconnecting from phy %s\n",
310 priv->phy_name);
311 phy_stop(priv->phydev);
312 phy_disconnect(priv->phydev);
313 }
314
315 return 0;
316}
317
318static void ax88172a_unbind(struct usbnet *dev, struct usb_interface *intf)
319{
320 struct ax88172a_private *priv = dev->driver_priv;
321
322 ax88172a_remove_mdio(dev);
323 kfree(priv);
324}
325
326static int ax88172a_reset(struct usbnet *dev)
327{
328 struct asix_data *data = (struct asix_data *)&dev->data;
329 struct ax88172a_private *priv = dev->driver_priv;
330 int ret;
331 u16 rx_ctl;
332
333 ax88172a_reset_phy(dev, priv->use_embdphy);
334
335 msleep(150);
336 rx_ctl = asix_read_rx_ctl(dev);
337 netdev_dbg(dev->net, "RX_CTL is 0x%04x after software reset\n", rx_ctl);
338 ret = asix_write_rx_ctl(dev, 0x0000);
339 if (ret < 0)
340 goto out;
341
342 rx_ctl = asix_read_rx_ctl(dev);
343 netdev_dbg(dev->net, "RX_CTL is 0x%04x setting to 0x0000\n", rx_ctl);
344
345 msleep(150);
346
347 ret = asix_write_cmd(dev, AX_CMD_WRITE_IPG0,
348 AX88772_IPG0_DEFAULT | AX88772_IPG1_DEFAULT,
349 AX88772_IPG2_DEFAULT, 0, NULL);
350 if (ret < 0) {
351 netdev_err(dev->net, "Write IPG,IPG1,IPG2 failed: %d\n", ret);
352 goto out;
353 }
354
355 /* Rewrite MAC address */
356 memcpy(data->mac_addr, dev->net->dev_addr, ETH_ALEN);
357 ret = asix_write_cmd(dev, AX_CMD_WRITE_NODE_ID, 0, 0, ETH_ALEN,
358 data->mac_addr);
359 if (ret < 0)
360 goto out;
361
362 /* Set RX_CTL to default values with 2k buffer, and enable cactus */
363 ret = asix_write_rx_ctl(dev, AX_DEFAULT_RX_CTL);
364 if (ret < 0)
365 goto out;
366
367 rx_ctl = asix_read_rx_ctl(dev);
368 netdev_dbg(dev->net, "RX_CTL is 0x%04x after all initializations\n",
369 rx_ctl);
370
371 rx_ctl = asix_read_medium_status(dev);
372 netdev_dbg(dev->net, "Medium Status is 0x%04x after all initializations\n",
373 rx_ctl);
374
375 /* Connect to PHY */
376 snprintf(priv->phy_name, 20, PHY_ID_FMT,
377 priv->mdio->id, priv->phy_addr);
378
379 priv->phydev = phy_connect(dev->net, priv->phy_name,
380 &ax88172a_adjust_link,
381 0, PHY_INTERFACE_MODE_MII);
382 if (IS_ERR(priv->phydev)) {
383 netdev_err(dev->net, "Could not connect to PHY device %s\n",
384 priv->phy_name);
385 ret = PTR_ERR(priv->phydev);
386 goto out;
387 }
388
389 netdev_info(dev->net, "Connected to phy %s\n", priv->phy_name);
390
391 /* During power-up, the AX88172A set the power down (BMCR_PDOWN)
392 * bit of the PHY. Bring the PHY up again.
393 */
394 genphy_resume(priv->phydev);
395 phy_start(priv->phydev);
396
397 return 0;
398
399out:
400 return ret;
401
402}
403
404const struct driver_info ax88172a_info = {
405 .description = "ASIX AX88172A USB 2.0 Ethernet",
406 .bind = ax88172a_bind,
407 .reset = ax88172a_reset,
408 .stop = ax88172a_stop,
409 .unbind = ax88172a_unbind,
410 .status = ax88172a_status,
411 .flags = FLAG_ETHER | FLAG_FRAMING_AX | FLAG_LINK_INTR |
412 FLAG_MULTI_PACKET,
413 .rx_fixup = asix_rx_fixup,
414 .tx_fixup = asix_tx_fixup,
415};