Commit | Line | Data |
---|---|---|
67620987 AK |
1 | /* |
2 | * This file is based on code from OCTEON SDK by Cavium Networks. | |
80ff0fd3 DD |
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. | |
67620987 AK |
9 | */ |
10 | ||
80ff0fd3 DD |
11 | #include <linux/kernel.h> |
12 | #include <linux/ethtool.h> | |
f6ed1b3b | 13 | #include <linux/phy.h> |
7a2eaf93 | 14 | #include <linux/ratelimit.h> |
df9244c5 | 15 | #include <linux/of_mdio.h> |
948c251b | 16 | #include <generated/utsrelease.h> |
80ff0fd3 DD |
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" | |
f6ed1b3b | 24 | #include "ethernet-util.h" |
80ff0fd3 | 25 | |
96217ebf | 26 | #include <asm/octeon/cvmx-gmxx-defs.h> |
af866496 | 27 | #include <asm/octeon/cvmx-smix-defs.h> |
80ff0fd3 | 28 | |
80ff0fd3 DD |
29 | static void cvm_oct_get_drvinfo(struct net_device *dev, |
30 | struct ethtool_drvinfo *info) | |
31 | { | |
948c251b AK |
32 | strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver)); |
33 | strlcpy(info->version, UTS_RELEASE, sizeof(info->version)); | |
7826d43f | 34 | strlcpy(info->bus_info, "Builtin", sizeof(info->bus_info)); |
80ff0fd3 DD |
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); | |
80ff0fd3 | 40 | |
f6ed1b3b DD |
41 | if (priv->phydev) |
42 | return phy_ethtool_gset(priv->phydev, cmd); | |
80ff0fd3 | 43 | |
f6ed1b3b | 44 | return -EINVAL; |
80ff0fd3 DD |
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); | |
80ff0fd3 | 50 | |
f6ed1b3b DD |
51 | if (!capable(CAP_NET_ADMIN)) |
52 | return -EPERM; | |
53 | ||
54 | if (priv->phydev) | |
55 | return phy_ethtool_sset(priv->phydev, cmd); | |
80ff0fd3 | 56 | |
f6ed1b3b | 57 | return -EINVAL; |
80ff0fd3 DD |
58 | } |
59 | ||
60 | static int cvm_oct_nway_reset(struct net_device *dev) | |
61 | { | |
62 | struct octeon_ethernet *priv = netdev_priv(dev); | |
80ff0fd3 | 63 | |
f6ed1b3b DD |
64 | if (!capable(CAP_NET_ADMIN)) |
65 | return -EPERM; | |
80ff0fd3 | 66 | |
f6ed1b3b DD |
67 | if (priv->phydev) |
68 | return phy_start_aneg(priv->phydev); | |
80ff0fd3 | 69 | |
f6ed1b3b | 70 | return -EINVAL; |
80ff0fd3 DD |
71 | } |
72 | ||
9326e361 | 73 | const struct ethtool_ops cvm_oct_ethtool_ops = { |
80ff0fd3 DD |
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, | |
f6ed1b3b | 78 | .get_link = ethtool_op_get_link, |
80ff0fd3 DD |
79 | }; |
80 | ||
81 | /** | |
ec977c5b | 82 | * cvm_oct_ioctl - IOCTL support for PHY control |
80ff0fd3 DD |
83 | * @dev: Device to change |
84 | * @rq: the request | |
85 | * @cmd: the command | |
ec977c5b | 86 | * |
80ff0fd3 DD |
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); | |
80ff0fd3 | 92 | |
f6ed1b3b DD |
93 | if (!netif_running(dev)) |
94 | return -EINVAL; | |
95 | ||
96 | if (!priv->phydev) | |
97 | return -EINVAL; | |
98 | ||
28b04113 | 99 | return phy_mii_ioctl(priv->phydev, rq, cmd); |
f6ed1b3b | 100 | } |
80ff0fd3 | 101 | |
2638f713 AK |
102 | void cvm_oct_note_carrier(struct octeon_ethernet *priv, |
103 | cvmx_helper_link_info_t li) | |
ec3a2207 DD |
104 | { |
105 | if (li.s.link_up) { | |
36a14572 | 106 | pr_notice_ratelimited("%s: %u Mbps %s duplex, port %d, queue %d\n", |
ec3a2207 DD |
107 | netdev_name(priv->netdev), li.s.speed, |
108 | (li.s.full_duplex) ? "Full" : "Half", | |
36a14572 | 109 | priv->port, priv->queue); |
ec3a2207 DD |
110 | } else { |
111 | pr_notice_ratelimited("%s: Link down\n", | |
112 | netdev_name(priv->netdev)); | |
113 | } | |
114 | } | |
115 | ||
ec3a2207 | 116 | void cvm_oct_adjust_link(struct net_device *dev) |
f6ed1b3b DD |
117 | { |
118 | struct octeon_ethernet *priv = netdev_priv(dev); | |
119 | cvmx_helper_link_info_t link_info; | |
120 | ||
710086db AK |
121 | link_info.u64 = 0; |
122 | link_info.s.link_up = priv->phydev->link ? 1 : 0; | |
123 | link_info.s.full_duplex = priv->phydev->duplex ? 1 : 0; | |
124 | link_info.s.speed = priv->phydev->speed; | |
125 | priv->link_info = link_info.u64; | |
126 | ||
127 | /* | |
128 | * The polling task need to know about link status changes. | |
129 | */ | |
130 | if (priv->poll) | |
131 | priv->poll(dev); | |
132 | ||
f6ed1b3b DD |
133 | if (priv->last_link != priv->phydev->link) { |
134 | priv->last_link = priv->phydev->link; | |
0da50c69 | 135 | cvmx_helper_link_set(priv->port, link_info); |
ec3a2207 | 136 | cvm_oct_note_carrier(priv, link_info); |
f6ed1b3b | 137 | } |
80ff0fd3 DD |
138 | } |
139 | ||
ec3a2207 DD |
140 | int cvm_oct_common_stop(struct net_device *dev) |
141 | { | |
142 | struct octeon_ethernet *priv = netdev_priv(dev); | |
96217ebf | 143 | int interface = INTERFACE(priv->port); |
ec3a2207 | 144 | cvmx_helper_link_info_t link_info; |
96217ebf AK |
145 | union cvmx_gmxx_prtx_cfg gmx_cfg; |
146 | int index = INDEX(priv->port); | |
147 | ||
148 | gmx_cfg.u64 = cvmx_read_csr(CVMX_GMXX_PRTX_CFG(index, interface)); | |
149 | gmx_cfg.s.en = 0; | |
150 | cvmx_write_csr(CVMX_GMXX_PRTX_CFG(index, interface), gmx_cfg.u64); | |
ec3a2207 DD |
151 | |
152 | priv->poll = NULL; | |
153 | ||
154 | if (priv->phydev) | |
155 | phy_disconnect(priv->phydev); | |
156 | priv->phydev = NULL; | |
157 | ||
158 | if (priv->last_link) { | |
159 | link_info.u64 = 0; | |
160 | priv->last_link = 0; | |
161 | ||
162 | cvmx_helper_link_set(priv->port, link_info); | |
163 | cvm_oct_note_carrier(priv, link_info); | |
164 | } | |
165 | return 0; | |
166 | } | |
f6ed1b3b | 167 | |
80ff0fd3 | 168 | /** |
ec977c5b | 169 | * cvm_oct_phy_setup_device - setup the PHY |
80ff0fd3 DD |
170 | * |
171 | * @dev: Device to setup | |
172 | * | |
173 | * Returns Zero on success, negative on failure | |
174 | */ | |
f6ed1b3b | 175 | int cvm_oct_phy_setup_device(struct net_device *dev) |
80ff0fd3 DD |
176 | { |
177 | struct octeon_ethernet *priv = netdev_priv(dev); | |
df9244c5 | 178 | struct device_node *phy_node; |
f6ed1b3b | 179 | |
df9244c5 | 180 | if (!priv->of_node) |
ec3a2207 | 181 | goto no_phy; |
f6ed1b3b | 182 | |
df9244c5 | 183 | phy_node = of_parse_phandle(priv->of_node, "phy-handle", 0); |
a25e2780 AK |
184 | if (!phy_node && of_phy_is_fixed_link(priv->of_node)) { |
185 | int rc; | |
186 | ||
187 | rc = of_phy_register_fixed_link(priv->of_node); | |
188 | if (rc) | |
189 | return rc; | |
190 | ||
191 | phy_node = of_node_get(priv->of_node); | |
192 | } | |
df9244c5 | 193 | if (!phy_node) |
ec3a2207 | 194 | goto no_phy; |
f6ed1b3b | 195 | |
df9244c5 DD |
196 | priv->phydev = of_phy_connect(dev, phy_node, cvm_oct_adjust_link, 0, |
197 | PHY_INTERFACE_MODE_GMII); | |
198 | ||
e8a4e572 | 199 | if (!priv->phydev) |
df9244c5 DD |
200 | return -ENODEV; |
201 | ||
202 | priv->last_link = 0; | |
203 | phy_start_aneg(priv->phydev); | |
f6ed1b3b | 204 | |
ec3a2207 DD |
205 | return 0; |
206 | no_phy: | |
207 | /* If there is no phy, assume a direct MAC connection and that | |
208 | * the link is up. | |
209 | */ | |
210 | netif_carrier_on(dev); | |
80ff0fd3 DD |
211 | return 0; |
212 | } |