goldfish: Fix build error of missing ioremap on UM
[linux-2.6-block.git] / drivers / net / usb / ax88172a.c
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, see <http://www.gnu.org/licenses/>.
25  */
26
27 #include "asix.h"
28 #include <linux/phy.h>
29
30 struct 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;
37         struct asix_rx_fixup_info rx_fixup_info;
38 };
39
40 /* MDIO read and write wrappers for phylib */
41 static 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
47 static 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
54 static 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 */
66 static 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
92 static 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 */
98 static int ax88172a_init_mdio(struct usbnet *dev)
99 {
100         struct ax88172a_private *priv = dev->driver_priv;
101         int ret;
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         ret = mdiobus_register(priv->mdio);
118         if (ret) {
119                 netdev_err(dev->net, "Could not register MDIO bus\n");
120                 goto mfree;
121         }
122
123         netdev_info(dev->net, "registered mdio bus %s\n", priv->mdio->id);
124         return 0;
125
126 mfree:
127         mdiobus_free(priv->mdio);
128         return ret;
129 }
130
131 static 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);
137         kfree(priv->mdio->irq);
138         mdiobus_free(priv->mdio);
139 }
140
141 static const struct net_device_ops ax88172a_netdev_ops = {
142         .ndo_open               = usbnet_open,
143         .ndo_stop               = usbnet_stop,
144         .ndo_start_xmit         = usbnet_start_xmit,
145         .ndo_tx_timeout         = usbnet_tx_timeout,
146         .ndo_change_mtu         = usbnet_change_mtu,
147         .ndo_set_mac_address    = asix_set_mac_address,
148         .ndo_validate_addr      = eth_validate_addr,
149         .ndo_do_ioctl           = ax88172a_ioctl,
150         .ndo_set_rx_mode        = asix_set_multicast,
151 };
152
153 static int ax88172a_get_settings(struct net_device *net,
154                                  struct ethtool_cmd *cmd)
155 {
156         if (!net->phydev)
157                 return -ENODEV;
158
159         return phy_ethtool_gset(net->phydev, cmd);
160 }
161
162 static int ax88172a_set_settings(struct net_device *net,
163                                  struct ethtool_cmd *cmd)
164 {
165         if (!net->phydev)
166                 return -ENODEV;
167
168         return phy_ethtool_sset(net->phydev, cmd);
169 }
170
171 static int ax88172a_nway_reset(struct net_device *net)
172 {
173         if (!net->phydev)
174                 return -ENODEV;
175
176         return phy_start_aneg(net->phydev);
177 }
178
179 static const struct ethtool_ops ax88172a_ethtool_ops = {
180         .get_drvinfo            = asix_get_drvinfo,
181         .get_link               = usbnet_get_link,
182         .get_msglevel           = usbnet_get_msglevel,
183         .set_msglevel           = usbnet_set_msglevel,
184         .get_wol                = asix_get_wol,
185         .set_wol                = asix_set_wol,
186         .get_eeprom_len         = asix_get_eeprom_len,
187         .get_eeprom             = asix_get_eeprom,
188         .set_eeprom             = asix_set_eeprom,
189         .get_settings           = ax88172a_get_settings,
190         .set_settings           = ax88172a_set_settings,
191         .nway_reset             = ax88172a_nway_reset,
192 };
193
194 static int ax88172a_reset_phy(struct usbnet *dev, int embd_phy)
195 {
196         int ret;
197
198         ret = asix_sw_reset(dev, AX_SWRESET_IPPD);
199         if (ret < 0)
200                 goto err;
201
202         msleep(150);
203         ret = asix_sw_reset(dev, AX_SWRESET_CLEAR);
204         if (ret < 0)
205                 goto err;
206
207         msleep(150);
208
209         ret = asix_sw_reset(dev, embd_phy ? AX_SWRESET_IPRL : AX_SWRESET_IPPD);
210         if (ret < 0)
211                 goto err;
212
213         return 0;
214
215 err:
216         return ret;
217 }
218
219
220 static int ax88172a_bind(struct usbnet *dev, struct usb_interface *intf)
221 {
222         int ret;
223         u8 buf[ETH_ALEN];
224         struct ax88172a_private *priv;
225
226         usbnet_get_endpoints(dev, intf);
227
228         priv = kzalloc(sizeof(*priv), GFP_KERNEL);
229         if (!priv)
230                 return -ENOMEM;
231
232         dev->driver_priv = priv;
233
234         /* Get the MAC address */
235         ret = asix_read_cmd(dev, AX_CMD_READ_NODE_ID, 0, 0, ETH_ALEN, buf);
236         if (ret < 0) {
237                 netdev_err(dev->net, "Failed to read MAC address: %d\n", ret);
238                 goto free;
239         }
240         memcpy(dev->net->dev_addr, buf, ETH_ALEN);
241
242         dev->net->netdev_ops = &ax88172a_netdev_ops;
243         dev->net->ethtool_ops = &ax88172a_ethtool_ops;
244
245         /* are we using the internal or the external phy? */
246         ret = asix_read_cmd(dev, AX_CMD_SW_PHY_STATUS, 0, 0, 1, buf);
247         if (ret < 0) {
248                 netdev_err(dev->net, "Failed to read software interface selection register: %d\n",
249                            ret);
250                 goto free;
251         }
252
253         netdev_dbg(dev->net, "AX_CMD_SW_PHY_STATUS = 0x%02x\n", buf[0]);
254         switch (buf[0] & AX_PHY_SELECT_MASK) {
255         case AX_PHY_SELECT_INTERNAL:
256                 netdev_dbg(dev->net, "use internal phy\n");
257                 priv->use_embdphy = 1;
258                 break;
259         case AX_PHY_SELECT_EXTERNAL:
260                 netdev_dbg(dev->net, "use external phy\n");
261                 priv->use_embdphy = 0;
262                 break;
263         default:
264                 netdev_err(dev->net, "Interface mode not supported by driver\n");
265                 ret = -ENOTSUPP;
266                 goto free;
267         }
268
269         priv->phy_addr = asix_read_phy_addr(dev, priv->use_embdphy);
270         ax88172a_reset_phy(dev, priv->use_embdphy);
271
272         /* Asix framing packs multiple eth frames into a 2K usb bulk transfer */
273         if (dev->driver_info->flags & FLAG_FRAMING_AX) {
274                 /* hard_mtu  is still the default - the device does not support
275                    jumbo eth frames */
276                 dev->rx_urb_size = 2048;
277         }
278
279         /* init MDIO bus */
280         ret = ax88172a_init_mdio(dev);
281         if (ret)
282                 goto free;
283
284         return 0;
285
286 free:
287         kfree(priv);
288         return ret;
289 }
290
291 static int ax88172a_stop(struct usbnet *dev)
292 {
293         struct ax88172a_private *priv = dev->driver_priv;
294
295         netdev_dbg(dev->net, "Stopping interface\n");
296
297         if (priv->phydev) {
298                 netdev_info(dev->net, "Disconnecting from phy %s\n",
299                             priv->phy_name);
300                 phy_stop(priv->phydev);
301                 phy_disconnect(priv->phydev);
302         }
303
304         return 0;
305 }
306
307 static void ax88172a_unbind(struct usbnet *dev, struct usb_interface *intf)
308 {
309         struct ax88172a_private *priv = dev->driver_priv;
310
311         ax88172a_remove_mdio(dev);
312         kfree(priv);
313 }
314
315 static int ax88172a_reset(struct usbnet *dev)
316 {
317         struct asix_data *data = (struct asix_data *)&dev->data;
318         struct ax88172a_private *priv = dev->driver_priv;
319         int ret;
320         u16 rx_ctl;
321
322         ax88172a_reset_phy(dev, priv->use_embdphy);
323
324         msleep(150);
325         rx_ctl = asix_read_rx_ctl(dev);
326         netdev_dbg(dev->net, "RX_CTL is 0x%04x after software reset\n", rx_ctl);
327         ret = asix_write_rx_ctl(dev, 0x0000);
328         if (ret < 0)
329                 goto out;
330
331         rx_ctl = asix_read_rx_ctl(dev);
332         netdev_dbg(dev->net, "RX_CTL is 0x%04x setting to 0x0000\n", rx_ctl);
333
334         msleep(150);
335
336         ret = asix_write_cmd(dev, AX_CMD_WRITE_IPG0,
337                              AX88772_IPG0_DEFAULT | AX88772_IPG1_DEFAULT,
338                              AX88772_IPG2_DEFAULT, 0, NULL);
339         if (ret < 0) {
340                 netdev_err(dev->net, "Write IPG,IPG1,IPG2 failed: %d\n", ret);
341                 goto out;
342         }
343
344         /* Rewrite MAC address */
345         memcpy(data->mac_addr, dev->net->dev_addr, ETH_ALEN);
346         ret = asix_write_cmd(dev, AX_CMD_WRITE_NODE_ID, 0, 0, ETH_ALEN,
347                              data->mac_addr);
348         if (ret < 0)
349                 goto out;
350
351         /* Set RX_CTL to default values with 2k buffer, and enable cactus */
352         ret = asix_write_rx_ctl(dev, AX_DEFAULT_RX_CTL);
353         if (ret < 0)
354                 goto out;
355
356         rx_ctl = asix_read_rx_ctl(dev);
357         netdev_dbg(dev->net, "RX_CTL is 0x%04x after all initializations\n",
358                    rx_ctl);
359
360         rx_ctl = asix_read_medium_status(dev);
361         netdev_dbg(dev->net, "Medium Status is 0x%04x after all initializations\n",
362                    rx_ctl);
363
364         /* Connect to PHY */
365         snprintf(priv->phy_name, 20, PHY_ID_FMT,
366                  priv->mdio->id, priv->phy_addr);
367
368         priv->phydev = phy_connect(dev->net, priv->phy_name,
369                                    &ax88172a_adjust_link,
370                                    PHY_INTERFACE_MODE_MII);
371         if (IS_ERR(priv->phydev)) {
372                 netdev_err(dev->net, "Could not connect to PHY device %s\n",
373                            priv->phy_name);
374                 ret = PTR_ERR(priv->phydev);
375                 goto out;
376         }
377
378         netdev_info(dev->net, "Connected to phy %s\n", priv->phy_name);
379
380         /* During power-up, the AX88172A set the power down (BMCR_PDOWN)
381          * bit of the PHY. Bring the PHY up again.
382          */
383         genphy_resume(priv->phydev);
384         phy_start(priv->phydev);
385
386         return 0;
387
388 out:
389         return ret;
390
391 }
392
393 static int ax88172a_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
394 {
395         struct ax88172a_private *dp = dev->driver_priv;
396         struct asix_rx_fixup_info *rx = &dp->rx_fixup_info;
397
398         return asix_rx_fixup_internal(dev, skb, rx);
399 }
400
401 const struct driver_info ax88172a_info = {
402         .description = "ASIX AX88172A USB 2.0 Ethernet",
403         .bind = ax88172a_bind,
404         .reset = ax88172a_reset,
405         .stop = ax88172a_stop,
406         .unbind = ax88172a_unbind,
407         .status = ax88172a_status,
408         .flags = FLAG_ETHER | FLAG_FRAMING_AX | FLAG_LINK_INTR |
409                  FLAG_MULTI_PACKET,
410         .rx_fixup = ax88172a_rx_fixup,
411         .tx_fixup = asix_tx_fixup,
412 };