Aaro Koskinen | 6762098 | 2015-04-04 22:51:21 +0300 | [diff] [blame] | 1 | /* |
| 2 | * This file is based on code from OCTEON SDK by Cavium Networks. |
David Daney | 80ff0fd | 2009-05-05 17:35:21 -0700 | [diff] [blame] | 3 | * |
| 4 | * Copyright (c) 2003-2007 Cavium Networks |
| 5 | * |
| 6 | * This file is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License, Version 2, as |
| 8 | * published by the Free Software Foundation. |
Aaro Koskinen | 6762098 | 2015-04-04 22:51:21 +0300 | [diff] [blame] | 9 | */ |
| 10 | |
David Daney | 80ff0fd | 2009-05-05 17:35:21 -0700 | [diff] [blame] | 11 | #include <linux/kernel.h> |
| 12 | #include <linux/ethtool.h> |
David Daney | f6ed1b3 | 2009-10-14 12:04:42 -0700 | [diff] [blame] | 13 | #include <linux/phy.h> |
Christian Dietrich | 7a2eaf9 | 2011-06-04 17:35:58 +0200 | [diff] [blame] | 14 | #include <linux/ratelimit.h> |
David Daney | df9244c | 2012-07-05 18:12:40 +0200 | [diff] [blame] | 15 | #include <linux/of_mdio.h> |
Aaro Koskinen | 948c251 | 2015-04-04 22:51:20 +0300 | [diff] [blame] | 16 | #include <generated/utsrelease.h> |
David Daney | 80ff0fd | 2009-05-05 17:35:21 -0700 | [diff] [blame] | 17 | #include <net/dst.h> |
| 18 | |
| 19 | #include <asm/octeon/octeon.h> |
| 20 | |
| 21 | #include "ethernet-defines.h" |
| 22 | #include "octeon-ethernet.h" |
| 23 | #include "ethernet-mdio.h" |
David Daney | f6ed1b3 | 2009-10-14 12:04:42 -0700 | [diff] [blame] | 24 | #include "ethernet-util.h" |
David Daney | 80ff0fd | 2009-05-05 17:35:21 -0700 | [diff] [blame] | 25 | |
Aaro Koskinen | 96217eb | 2015-04-04 22:51:03 +0300 | [diff] [blame] | 26 | #include <asm/octeon/cvmx-gmxx-defs.h> |
David Daney | af86649 | 2011-11-22 14:47:00 +0000 | [diff] [blame] | 27 | #include <asm/octeon/cvmx-smix-defs.h> |
David Daney | 80ff0fd | 2009-05-05 17:35:21 -0700 | [diff] [blame] | 28 | |
David Daney | 80ff0fd | 2009-05-05 17:35:21 -0700 | [diff] [blame] | 29 | static void cvm_oct_get_drvinfo(struct net_device *dev, |
| 30 | struct ethtool_drvinfo *info) |
| 31 | { |
Aaro Koskinen | 948c251 | 2015-04-04 22:51:20 +0300 | [diff] [blame] | 32 | strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver)); |
| 33 | strlcpy(info->version, UTS_RELEASE, sizeof(info->version)); |
Jiri Pirko | 7826d43 | 2013-01-06 00:44:26 +0000 | [diff] [blame] | 34 | strlcpy(info->bus_info, "Builtin", sizeof(info->bus_info)); |
David Daney | 80ff0fd | 2009-05-05 17:35:21 -0700 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | static int cvm_oct_get_settings(struct net_device *dev, struct ethtool_cmd *cmd) |
| 38 | { |
| 39 | struct octeon_ethernet *priv = netdev_priv(dev); |
David Daney | 80ff0fd | 2009-05-05 17:35:21 -0700 | [diff] [blame] | 40 | |
David Daney | f6ed1b3 | 2009-10-14 12:04:42 -0700 | [diff] [blame] | 41 | if (priv->phydev) |
| 42 | return phy_ethtool_gset(priv->phydev, cmd); |
David Daney | 80ff0fd | 2009-05-05 17:35:21 -0700 | [diff] [blame] | 43 | |
David Daney | f6ed1b3 | 2009-10-14 12:04:42 -0700 | [diff] [blame] | 44 | return -EINVAL; |
David Daney | 80ff0fd | 2009-05-05 17:35:21 -0700 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | static int cvm_oct_set_settings(struct net_device *dev, struct ethtool_cmd *cmd) |
| 48 | { |
| 49 | struct octeon_ethernet *priv = netdev_priv(dev); |
David Daney | 80ff0fd | 2009-05-05 17:35:21 -0700 | [diff] [blame] | 50 | |
David Daney | f6ed1b3 | 2009-10-14 12:04:42 -0700 | [diff] [blame] | 51 | if (!capable(CAP_NET_ADMIN)) |
| 52 | return -EPERM; |
David Daney | 80ff0fd | 2009-05-05 17:35:21 -0700 | [diff] [blame] | 53 | |
David Daney | f6ed1b3 | 2009-10-14 12:04:42 -0700 | [diff] [blame] | 54 | if (priv->phydev) |
| 55 | return phy_ethtool_sset(priv->phydev, cmd); |
| 56 | |
| 57 | return -EINVAL; |
David Daney | 80ff0fd | 2009-05-05 17:35:21 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | static int cvm_oct_nway_reset(struct net_device *dev) |
| 61 | { |
| 62 | struct octeon_ethernet *priv = netdev_priv(dev); |
David Daney | 80ff0fd | 2009-05-05 17:35:21 -0700 | [diff] [blame] | 63 | |
David Daney | f6ed1b3 | 2009-10-14 12:04:42 -0700 | [diff] [blame] | 64 | if (!capable(CAP_NET_ADMIN)) |
| 65 | return -EPERM; |
David Daney | 80ff0fd | 2009-05-05 17:35:21 -0700 | [diff] [blame] | 66 | |
David Daney | f6ed1b3 | 2009-10-14 12:04:42 -0700 | [diff] [blame] | 67 | if (priv->phydev) |
| 68 | return phy_start_aneg(priv->phydev); |
David Daney | 80ff0fd | 2009-05-05 17:35:21 -0700 | [diff] [blame] | 69 | |
David Daney | f6ed1b3 | 2009-10-14 12:04:42 -0700 | [diff] [blame] | 70 | return -EINVAL; |
David Daney | 80ff0fd | 2009-05-05 17:35:21 -0700 | [diff] [blame] | 71 | } |
| 72 | |
David Daney | 9326e36 | 2009-10-01 16:47:39 -0700 | [diff] [blame] | 73 | const struct ethtool_ops cvm_oct_ethtool_ops = { |
David Daney | 80ff0fd | 2009-05-05 17:35:21 -0700 | [diff] [blame] | 74 | .get_drvinfo = cvm_oct_get_drvinfo, |
| 75 | .get_settings = cvm_oct_get_settings, |
| 76 | .set_settings = cvm_oct_set_settings, |
| 77 | .nway_reset = cvm_oct_nway_reset, |
David Daney | f6ed1b3 | 2009-10-14 12:04:42 -0700 | [diff] [blame] | 78 | .get_link = ethtool_op_get_link, |
David Daney | 80ff0fd | 2009-05-05 17:35:21 -0700 | [diff] [blame] | 79 | }; |
| 80 | |
| 81 | /** |
David Daney | ec977c5 | 2010-02-16 17:25:32 -0800 | [diff] [blame] | 82 | * cvm_oct_ioctl - IOCTL support for PHY control |
David Daney | 80ff0fd | 2009-05-05 17:35:21 -0700 | [diff] [blame] | 83 | * @dev: Device to change |
| 84 | * @rq: the request |
| 85 | * @cmd: the command |
David Daney | ec977c5 | 2010-02-16 17:25:32 -0800 | [diff] [blame] | 86 | * |
David Daney | 80ff0fd | 2009-05-05 17:35:21 -0700 | [diff] [blame] | 87 | * Returns Zero on success |
| 88 | */ |
| 89 | int cvm_oct_ioctl(struct net_device *dev, struct ifreq *rq, int cmd) |
| 90 | { |
| 91 | struct octeon_ethernet *priv = netdev_priv(dev); |
David Daney | 80ff0fd | 2009-05-05 17:35:21 -0700 | [diff] [blame] | 92 | |
David Daney | f6ed1b3 | 2009-10-14 12:04:42 -0700 | [diff] [blame] | 93 | if (!netif_running(dev)) |
| 94 | return -EINVAL; |
David Daney | 80ff0fd | 2009-05-05 17:35:21 -0700 | [diff] [blame] | 95 | |
David Daney | f6ed1b3 | 2009-10-14 12:04:42 -0700 | [diff] [blame] | 96 | if (!priv->phydev) |
| 97 | return -EINVAL; |
| 98 | |
Richard Cochran | 28b0411 | 2010-07-17 08:48:55 +0000 | [diff] [blame] | 99 | return phy_mii_ioctl(priv->phydev, rq, cmd); |
David Daney | 80ff0fd | 2009-05-05 17:35:21 -0700 | [diff] [blame] | 100 | } |
| 101 | |
Aaro Koskinen | 2638f71 | 2015-04-04 22:51:07 +0300 | [diff] [blame] | 102 | void cvm_oct_note_carrier(struct octeon_ethernet *priv, |
| 103 | cvmx_helper_link_info_t li) |
David Daney | ec3a220 | 2014-05-29 11:10:02 +0100 | [diff] [blame] | 104 | { |
| 105 | if (li.s.link_up) { |
Aaro Koskinen | 36a1457 | 2015-04-04 22:51:06 +0300 | [diff] [blame] | 106 | pr_notice_ratelimited("%s: %u Mbps %s duplex, port %d, queue %d\n", |
David Daney | ec3a220 | 2014-05-29 11:10:02 +0100 | [diff] [blame] | 107 | netdev_name(priv->netdev), li.s.speed, |
| 108 | (li.s.full_duplex) ? "Full" : "Half", |
Aaro Koskinen | 36a1457 | 2015-04-04 22:51:06 +0300 | [diff] [blame] | 109 | priv->port, priv->queue); |
David Daney | ec3a220 | 2014-05-29 11:10:02 +0100 | [diff] [blame] | 110 | } else { |
| 111 | pr_notice_ratelimited("%s: Link down\n", |
| 112 | netdev_name(priv->netdev)); |
| 113 | } |
| 114 | } |
| 115 | |
David Daney | ec3a220 | 2014-05-29 11:10:02 +0100 | [diff] [blame] | 116 | void cvm_oct_adjust_link(struct net_device *dev) |
David Daney | f6ed1b3 | 2009-10-14 12:04:42 -0700 | [diff] [blame] | 117 | { |
| 118 | struct octeon_ethernet *priv = netdev_priv(dev); |
| 119 | cvmx_helper_link_info_t link_info; |
| 120 | |
| 121 | if (priv->last_link != priv->phydev->link) { |
| 122 | priv->last_link = priv->phydev->link; |
| 123 | link_info.u64 = 0; |
| 124 | link_info.s.link_up = priv->last_link ? 1 : 0; |
| 125 | link_info.s.full_duplex = priv->phydev->duplex ? 1 : 0; |
| 126 | link_info.s.speed = priv->phydev->speed; |
David Daney | ec3a220 | 2014-05-29 11:10:02 +0100 | [diff] [blame] | 127 | |
Himangi Saraogi | 0da50c6 | 2014-03-03 06:30:46 +0530 | [diff] [blame] | 128 | cvmx_helper_link_set(priv->port, link_info); |
David Daney | ec3a220 | 2014-05-29 11:10:02 +0100 | [diff] [blame] | 129 | cvm_oct_note_carrier(priv, link_info); |
David Daney | f6ed1b3 | 2009-10-14 12:04:42 -0700 | [diff] [blame] | 130 | } |
| 131 | } |
| 132 | |
David Daney | ec3a220 | 2014-05-29 11:10:02 +0100 | [diff] [blame] | 133 | int cvm_oct_common_stop(struct net_device *dev) |
| 134 | { |
| 135 | struct octeon_ethernet *priv = netdev_priv(dev); |
Aaro Koskinen | 96217eb | 2015-04-04 22:51:03 +0300 | [diff] [blame] | 136 | int interface = INTERFACE(priv->port); |
David Daney | ec3a220 | 2014-05-29 11:10:02 +0100 | [diff] [blame] | 137 | cvmx_helper_link_info_t link_info; |
Aaro Koskinen | 96217eb | 2015-04-04 22:51:03 +0300 | [diff] [blame] | 138 | union cvmx_gmxx_prtx_cfg gmx_cfg; |
| 139 | int index = INDEX(priv->port); |
| 140 | |
| 141 | gmx_cfg.u64 = cvmx_read_csr(CVMX_GMXX_PRTX_CFG(index, interface)); |
| 142 | gmx_cfg.s.en = 0; |
| 143 | cvmx_write_csr(CVMX_GMXX_PRTX_CFG(index, interface), gmx_cfg.u64); |
David Daney | ec3a220 | 2014-05-29 11:10:02 +0100 | [diff] [blame] | 144 | |
| 145 | priv->poll = NULL; |
| 146 | |
| 147 | if (priv->phydev) |
| 148 | phy_disconnect(priv->phydev); |
| 149 | priv->phydev = NULL; |
| 150 | |
| 151 | if (priv->last_link) { |
| 152 | link_info.u64 = 0; |
| 153 | priv->last_link = 0; |
| 154 | |
| 155 | cvmx_helper_link_set(priv->port, link_info); |
| 156 | cvm_oct_note_carrier(priv, link_info); |
| 157 | } |
| 158 | return 0; |
| 159 | } |
David Daney | f6ed1b3 | 2009-10-14 12:04:42 -0700 | [diff] [blame] | 160 | |
David Daney | 80ff0fd | 2009-05-05 17:35:21 -0700 | [diff] [blame] | 161 | /** |
David Daney | ec977c5 | 2010-02-16 17:25:32 -0800 | [diff] [blame] | 162 | * cvm_oct_phy_setup_device - setup the PHY |
David Daney | 80ff0fd | 2009-05-05 17:35:21 -0700 | [diff] [blame] | 163 | * |
| 164 | * @dev: Device to setup |
| 165 | * |
| 166 | * Returns Zero on success, negative on failure |
| 167 | */ |
David Daney | f6ed1b3 | 2009-10-14 12:04:42 -0700 | [diff] [blame] | 168 | int cvm_oct_phy_setup_device(struct net_device *dev) |
David Daney | 80ff0fd | 2009-05-05 17:35:21 -0700 | [diff] [blame] | 169 | { |
| 170 | struct octeon_ethernet *priv = netdev_priv(dev); |
David Daney | df9244c | 2012-07-05 18:12:40 +0200 | [diff] [blame] | 171 | struct device_node *phy_node; |
David Daney | f6ed1b3 | 2009-10-14 12:04:42 -0700 | [diff] [blame] | 172 | |
David Daney | df9244c | 2012-07-05 18:12:40 +0200 | [diff] [blame] | 173 | if (!priv->of_node) |
David Daney | ec3a220 | 2014-05-29 11:10:02 +0100 | [diff] [blame] | 174 | goto no_phy; |
David Daney | f6ed1b3 | 2009-10-14 12:04:42 -0700 | [diff] [blame] | 175 | |
David Daney | df9244c | 2012-07-05 18:12:40 +0200 | [diff] [blame] | 176 | phy_node = of_parse_phandle(priv->of_node, "phy-handle", 0); |
| 177 | if (!phy_node) |
David Daney | ec3a220 | 2014-05-29 11:10:02 +0100 | [diff] [blame] | 178 | goto no_phy; |
David Daney | f6ed1b3 | 2009-10-14 12:04:42 -0700 | [diff] [blame] | 179 | |
David Daney | df9244c | 2012-07-05 18:12:40 +0200 | [diff] [blame] | 180 | priv->phydev = of_phy_connect(dev, phy_node, cvm_oct_adjust_link, 0, |
| 181 | PHY_INTERFACE_MODE_GMII); |
David Daney | f6ed1b3 | 2009-10-14 12:04:42 -0700 | [diff] [blame] | 182 | |
David Daney | df9244c | 2012-07-05 18:12:40 +0200 | [diff] [blame] | 183 | if (priv->phydev == NULL) |
| 184 | return -ENODEV; |
| 185 | |
| 186 | priv->last_link = 0; |
| 187 | phy_start_aneg(priv->phydev); |
| 188 | |
David Daney | 80ff0fd | 2009-05-05 17:35:21 -0700 | [diff] [blame] | 189 | return 0; |
David Daney | ec3a220 | 2014-05-29 11:10:02 +0100 | [diff] [blame] | 190 | no_phy: |
| 191 | /* If there is no phy, assume a direct MAC connection and that |
| 192 | * the link is up. |
| 193 | */ |
| 194 | netif_carrier_on(dev); |
| 195 | return 0; |
David Daney | 80ff0fd | 2009-05-05 17:35:21 -0700 | [diff] [blame] | 196 | } |