xref: /openbmc/linux/drivers/net/usb/ax88172a.c (revision 2c4cdf5950b1a7a9d731c03c56e2cea3bd8ca7b7)
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 
31 struct 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 	struct asix_rx_fixup_info rx_fixup_info;
39 };
40 
41 /* MDIO read and write wrappers for phylib */
42 static int asix_mdio_bus_read(struct mii_bus *bus, int phy_id, int regnum)
43 {
44 	return asix_mdio_read(((struct usbnet *)bus->priv)->net, phy_id,
45 			      regnum);
46 }
47 
48 static int asix_mdio_bus_write(struct mii_bus *bus, int phy_id, int regnum,
49 			       u16 val)
50 {
51 	asix_mdio_write(((struct usbnet *)bus->priv)->net, phy_id, regnum, val);
52 	return 0;
53 }
54 
55 static int ax88172a_ioctl(struct net_device *net, struct ifreq *rq, int cmd)
56 {
57 	if (!netif_running(net))
58 		return -EINVAL;
59 
60 	if (!net->phydev)
61 		return -ENODEV;
62 
63 	return phy_mii_ioctl(net->phydev, rq, cmd);
64 }
65 
66 /* set MAC link settings according to information from phylib */
67 static void ax88172a_adjust_link(struct net_device *netdev)
68 {
69 	struct phy_device *phydev = netdev->phydev;
70 	struct usbnet *dev = netdev_priv(netdev);
71 	struct ax88172a_private *priv = dev->driver_priv;
72 	u16 mode = 0;
73 
74 	if (phydev->link) {
75 		mode = AX88772_MEDIUM_DEFAULT;
76 
77 		if (phydev->duplex == DUPLEX_HALF)
78 			mode &= ~AX_MEDIUM_FD;
79 
80 		if (phydev->speed != SPEED_100)
81 			mode &= ~AX_MEDIUM_PS;
82 	}
83 
84 	if (mode != priv->oldmode) {
85 		asix_write_medium_mode(dev, mode);
86 		priv->oldmode = mode;
87 		netdev_dbg(netdev, "speed %u duplex %d, setting mode to 0x%04x\n",
88 			   phydev->speed, phydev->duplex, mode);
89 		phy_print_status(phydev);
90 	}
91 }
92 
93 static void ax88172a_status(struct usbnet *dev, struct urb *urb)
94 {
95 	/* link changes are detected by polling the phy */
96 }
97 
98 /* use phylib infrastructure */
99 static int ax88172a_init_mdio(struct usbnet *dev)
100 {
101 	struct ax88172a_private *priv = dev->driver_priv;
102 	int ret, i;
103 
104 	priv->mdio = mdiobus_alloc();
105 	if (!priv->mdio) {
106 		netdev_err(dev->net, "Could not allocate MDIO bus\n");
107 		return -ENOMEM;
108 	}
109 
110 	priv->mdio->priv = (void *)dev;
111 	priv->mdio->read = &asix_mdio_bus_read;
112 	priv->mdio->write = &asix_mdio_bus_write;
113 	priv->mdio->name = "Asix MDIO Bus";
114 	/* mii bus name is usb-<usb bus number>-<usb device number> */
115 	snprintf(priv->mdio->id, MII_BUS_ID_SIZE, "usb-%03d:%03d",
116 		 dev->udev->bus->busnum, dev->udev->devnum);
117 
118 	priv->mdio->irq = kzalloc(sizeof(int) * PHY_MAX_ADDR, GFP_KERNEL);
119 	if (!priv->mdio->irq) {
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 
135 ifree:
136 	kfree(priv->mdio->irq);
137 mfree:
138 	mdiobus_free(priv->mdio);
139 	return ret;
140 }
141 
142 static 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 
152 static 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 
164 int 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 
172 int 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 
180 int 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 
188 static 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 	.set_eeprom		= asix_set_eeprom,
198 	.get_settings		= ax88172a_get_settings,
199 	.set_settings		= ax88172a_set_settings,
200 	.nway_reset		= ax88172a_nway_reset,
201 };
202 
203 static int ax88172a_reset_phy(struct usbnet *dev, int embd_phy)
204 {
205 	int ret;
206 
207 	ret = asix_sw_reset(dev, AX_SWRESET_IPPD);
208 	if (ret < 0)
209 		goto err;
210 
211 	msleep(150);
212 	ret = asix_sw_reset(dev, AX_SWRESET_CLEAR);
213 	if (ret < 0)
214 		goto err;
215 
216 	msleep(150);
217 
218 	ret = asix_sw_reset(dev, embd_phy ? AX_SWRESET_IPRL : AX_SWRESET_IPPD);
219 	if (ret < 0)
220 		goto err;
221 
222 	return 0;
223 
224 err:
225 	return ret;
226 }
227 
228 
229 static int ax88172a_bind(struct usbnet *dev, struct usb_interface *intf)
230 {
231 	int ret;
232 	u8 buf[ETH_ALEN];
233 	struct ax88172a_private *priv;
234 
235 	usbnet_get_endpoints(dev, intf);
236 
237 	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
238 	if (!priv)
239 		return -ENOMEM;
240 
241 	dev->driver_priv = priv;
242 
243 	/* Get the MAC address */
244 	ret = asix_read_cmd(dev, AX_CMD_READ_NODE_ID, 0, 0, ETH_ALEN, buf);
245 	if (ret < 0) {
246 		netdev_err(dev->net, "Failed to read MAC address: %d\n", ret);
247 		goto free;
248 	}
249 	memcpy(dev->net->dev_addr, buf, ETH_ALEN);
250 
251 	dev->net->netdev_ops = &ax88172a_netdev_ops;
252 	dev->net->ethtool_ops = &ax88172a_ethtool_ops;
253 
254 	/* are we using the internal or the external phy? */
255 	ret = asix_read_cmd(dev, AX_CMD_SW_PHY_STATUS, 0, 0, 1, buf);
256 	if (ret < 0) {
257 		netdev_err(dev->net, "Failed to read software interface selection register: %d\n",
258 			   ret);
259 		goto free;
260 	}
261 
262 	netdev_dbg(dev->net, "AX_CMD_SW_PHY_STATUS = 0x%02x\n", buf[0]);
263 	switch (buf[0] & AX_PHY_SELECT_MASK) {
264 	case AX_PHY_SELECT_INTERNAL:
265 		netdev_dbg(dev->net, "use internal phy\n");
266 		priv->use_embdphy = 1;
267 		break;
268 	case AX_PHY_SELECT_EXTERNAL:
269 		netdev_dbg(dev->net, "use external phy\n");
270 		priv->use_embdphy = 0;
271 		break;
272 	default:
273 		netdev_err(dev->net, "Interface mode not supported by driver\n");
274 		ret = -ENOTSUPP;
275 		goto free;
276 	}
277 
278 	priv->phy_addr = asix_read_phy_addr(dev, priv->use_embdphy);
279 	ax88172a_reset_phy(dev, priv->use_embdphy);
280 
281 	/* Asix framing packs multiple eth frames into a 2K usb bulk transfer */
282 	if (dev->driver_info->flags & FLAG_FRAMING_AX) {
283 		/* hard_mtu  is still the default - the device does not support
284 		   jumbo eth frames */
285 		dev->rx_urb_size = 2048;
286 	}
287 
288 	/* init MDIO bus */
289 	ret = ax88172a_init_mdio(dev);
290 	if (ret)
291 		goto free;
292 
293 	return 0;
294 
295 free:
296 	kfree(priv);
297 	return ret;
298 }
299 
300 static int ax88172a_stop(struct usbnet *dev)
301 {
302 	struct ax88172a_private *priv = dev->driver_priv;
303 
304 	netdev_dbg(dev->net, "Stopping interface\n");
305 
306 	if (priv->phydev) {
307 		netdev_info(dev->net, "Disconnecting from phy %s\n",
308 			    priv->phy_name);
309 		phy_stop(priv->phydev);
310 		phy_disconnect(priv->phydev);
311 	}
312 
313 	return 0;
314 }
315 
316 static void ax88172a_unbind(struct usbnet *dev, struct usb_interface *intf)
317 {
318 	struct ax88172a_private *priv = dev->driver_priv;
319 
320 	ax88172a_remove_mdio(dev);
321 	kfree(priv);
322 }
323 
324 static int ax88172a_reset(struct usbnet *dev)
325 {
326 	struct asix_data *data = (struct asix_data *)&dev->data;
327 	struct ax88172a_private *priv = dev->driver_priv;
328 	int ret;
329 	u16 rx_ctl;
330 
331 	ax88172a_reset_phy(dev, priv->use_embdphy);
332 
333 	msleep(150);
334 	rx_ctl = asix_read_rx_ctl(dev);
335 	netdev_dbg(dev->net, "RX_CTL is 0x%04x after software reset\n", rx_ctl);
336 	ret = asix_write_rx_ctl(dev, 0x0000);
337 	if (ret < 0)
338 		goto out;
339 
340 	rx_ctl = asix_read_rx_ctl(dev);
341 	netdev_dbg(dev->net, "RX_CTL is 0x%04x setting to 0x0000\n", rx_ctl);
342 
343 	msleep(150);
344 
345 	ret = asix_write_cmd(dev, AX_CMD_WRITE_IPG0,
346 			     AX88772_IPG0_DEFAULT | AX88772_IPG1_DEFAULT,
347 			     AX88772_IPG2_DEFAULT, 0, NULL);
348 	if (ret < 0) {
349 		netdev_err(dev->net, "Write IPG,IPG1,IPG2 failed: %d\n", ret);
350 		goto out;
351 	}
352 
353 	/* Rewrite MAC address */
354 	memcpy(data->mac_addr, dev->net->dev_addr, ETH_ALEN);
355 	ret = asix_write_cmd(dev, AX_CMD_WRITE_NODE_ID, 0, 0, ETH_ALEN,
356 			     data->mac_addr);
357 	if (ret < 0)
358 		goto out;
359 
360 	/* Set RX_CTL to default values with 2k buffer, and enable cactus */
361 	ret = asix_write_rx_ctl(dev, AX_DEFAULT_RX_CTL);
362 	if (ret < 0)
363 		goto out;
364 
365 	rx_ctl = asix_read_rx_ctl(dev);
366 	netdev_dbg(dev->net, "RX_CTL is 0x%04x after all initializations\n",
367 		   rx_ctl);
368 
369 	rx_ctl = asix_read_medium_status(dev);
370 	netdev_dbg(dev->net, "Medium Status is 0x%04x after all initializations\n",
371 		   rx_ctl);
372 
373 	/* Connect to PHY */
374 	snprintf(priv->phy_name, 20, PHY_ID_FMT,
375 		 priv->mdio->id, priv->phy_addr);
376 
377 	priv->phydev = phy_connect(dev->net, priv->phy_name,
378 				   &ax88172a_adjust_link,
379 				   PHY_INTERFACE_MODE_MII);
380 	if (IS_ERR(priv->phydev)) {
381 		netdev_err(dev->net, "Could not connect to PHY device %s\n",
382 			   priv->phy_name);
383 		ret = PTR_ERR(priv->phydev);
384 		goto out;
385 	}
386 
387 	netdev_info(dev->net, "Connected to phy %s\n", priv->phy_name);
388 
389 	/* During power-up, the AX88172A set the power down (BMCR_PDOWN)
390 	 * bit of the PHY. Bring the PHY up again.
391 	 */
392 	genphy_resume(priv->phydev);
393 	phy_start(priv->phydev);
394 
395 	return 0;
396 
397 out:
398 	return ret;
399 
400 }
401 
402 static int ax88172a_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
403 {
404 	struct ax88172a_private *dp = dev->driver_priv;
405 	struct asix_rx_fixup_info *rx = &dp->rx_fixup_info;
406 
407 	return asix_rx_fixup_internal(dev, skb, rx);
408 }
409 
410 const struct driver_info ax88172a_info = {
411 	.description = "ASIX AX88172A USB 2.0 Ethernet",
412 	.bind = ax88172a_bind,
413 	.reset = ax88172a_reset,
414 	.stop = ax88172a_stop,
415 	.unbind = ax88172a_unbind,
416 	.status = ax88172a_status,
417 	.flags = FLAG_ETHER | FLAG_FRAMING_AX | FLAG_LINK_INTR |
418 		 FLAG_MULTI_PACKET,
419 	.rx_fixup = ax88172a_rx_fixup,
420 	.tx_fixup = asix_tx_fixup,
421 };
422