Merge remote-tracking branch 'mmc-uh/next'
[deliverable/linux.git] / drivers / staging / octeon / ethernet-mdio.c
CommitLineData
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
29static 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
80ff0fd3
DD
37static int cvm_oct_nway_reset(struct net_device *dev)
38{
f6ed1b3b
DD
39 if (!capable(CAP_NET_ADMIN))
40 return -EPERM;
80ff0fd3 41
86bc5ed6
PR
42 if (dev->phydev)
43 return phy_start_aneg(dev->phydev);
80ff0fd3 44
f6ed1b3b 45 return -EINVAL;
80ff0fd3
DD
46}
47
9326e361 48const struct ethtool_ops cvm_oct_ethtool_ops = {
80ff0fd3 49 .get_drvinfo = cvm_oct_get_drvinfo,
80ff0fd3 50 .nway_reset = cvm_oct_nway_reset,
f6ed1b3b 51 .get_link = ethtool_op_get_link,
0d5704bf
PR
52 .get_link_ksettings = phy_ethtool_get_link_ksettings,
53 .set_link_ksettings = phy_ethtool_set_link_ksettings,
80ff0fd3
DD
54};
55
56/**
ec977c5b 57 * cvm_oct_ioctl - IOCTL support for PHY control
80ff0fd3
DD
58 * @dev: Device to change
59 * @rq: the request
60 * @cmd: the command
ec977c5b 61 *
80ff0fd3
DD
62 * Returns Zero on success
63 */
64int cvm_oct_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
65{
f6ed1b3b
DD
66 if (!netif_running(dev))
67 return -EINVAL;
68
86bc5ed6 69 if (!dev->phydev)
f6ed1b3b
DD
70 return -EINVAL;
71
86bc5ed6 72 return phy_mii_ioctl(dev->phydev, rq, cmd);
f6ed1b3b 73}
80ff0fd3 74
2638f713
AK
75void cvm_oct_note_carrier(struct octeon_ethernet *priv,
76 cvmx_helper_link_info_t li)
ec3a2207
DD
77{
78 if (li.s.link_up) {
36a14572 79 pr_notice_ratelimited("%s: %u Mbps %s duplex, port %d, queue %d\n",
ec3a2207
DD
80 netdev_name(priv->netdev), li.s.speed,
81 (li.s.full_duplex) ? "Full" : "Half",
36a14572 82 priv->port, priv->queue);
ec3a2207
DD
83 } else {
84 pr_notice_ratelimited("%s: Link down\n",
85 netdev_name(priv->netdev));
86 }
87}
88
ec3a2207 89void cvm_oct_adjust_link(struct net_device *dev)
f6ed1b3b
DD
90{
91 struct octeon_ethernet *priv = netdev_priv(dev);
92 cvmx_helper_link_info_t link_info;
93
710086db 94 link_info.u64 = 0;
86bc5ed6
PR
95 link_info.s.link_up = dev->phydev->link ? 1 : 0;
96 link_info.s.full_duplex = dev->phydev->duplex ? 1 : 0;
97 link_info.s.speed = dev->phydev->speed;
710086db
AK
98 priv->link_info = link_info.u64;
99
100 /*
101 * The polling task need to know about link status changes.
102 */
103 if (priv->poll)
104 priv->poll(dev);
105
86bc5ed6
PR
106 if (priv->last_link != dev->phydev->link) {
107 priv->last_link = dev->phydev->link;
0da50c69 108 cvmx_helper_link_set(priv->port, link_info);
ec3a2207 109 cvm_oct_note_carrier(priv, link_info);
f6ed1b3b 110 }
80ff0fd3
DD
111}
112
ec3a2207
DD
113int cvm_oct_common_stop(struct net_device *dev)
114{
115 struct octeon_ethernet *priv = netdev_priv(dev);
96217ebf 116 int interface = INTERFACE(priv->port);
ec3a2207 117 cvmx_helper_link_info_t link_info;
96217ebf
AK
118 union cvmx_gmxx_prtx_cfg gmx_cfg;
119 int index = INDEX(priv->port);
120
121 gmx_cfg.u64 = cvmx_read_csr(CVMX_GMXX_PRTX_CFG(index, interface));
122 gmx_cfg.s.en = 0;
123 cvmx_write_csr(CVMX_GMXX_PRTX_CFG(index, interface), gmx_cfg.u64);
ec3a2207
DD
124
125 priv->poll = NULL;
126
86bc5ed6
PR
127 if (dev->phydev)
128 phy_disconnect(dev->phydev);
ec3a2207
DD
129
130 if (priv->last_link) {
131 link_info.u64 = 0;
132 priv->last_link = 0;
133
134 cvmx_helper_link_set(priv->port, link_info);
135 cvm_oct_note_carrier(priv, link_info);
136 }
137 return 0;
138}
f6ed1b3b 139
80ff0fd3 140/**
ec977c5b 141 * cvm_oct_phy_setup_device - setup the PHY
80ff0fd3
DD
142 *
143 * @dev: Device to setup
144 *
145 * Returns Zero on success, negative on failure
146 */
f6ed1b3b 147int cvm_oct_phy_setup_device(struct net_device *dev)
80ff0fd3
DD
148{
149 struct octeon_ethernet *priv = netdev_priv(dev);
df9244c5 150 struct device_node *phy_node;
86bc5ed6 151 struct phy_device *phydev = NULL;
f6ed1b3b 152
df9244c5 153 if (!priv->of_node)
ec3a2207 154 goto no_phy;
f6ed1b3b 155
df9244c5 156 phy_node = of_parse_phandle(priv->of_node, "phy-handle", 0);
a25e2780
AK
157 if (!phy_node && of_phy_is_fixed_link(priv->of_node)) {
158 int rc;
159
160 rc = of_phy_register_fixed_link(priv->of_node);
161 if (rc)
162 return rc;
163
164 phy_node = of_node_get(priv->of_node);
165 }
df9244c5 166 if (!phy_node)
ec3a2207 167 goto no_phy;
f6ed1b3b 168
86bc5ed6
PR
169 phydev = of_phy_connect(dev, phy_node, cvm_oct_adjust_link, 0,
170 PHY_INTERFACE_MODE_GMII);
df9244c5 171
86bc5ed6 172 if (!phydev)
df9244c5
DD
173 return -ENODEV;
174
175 priv->last_link = 0;
86bc5ed6 176 phy_start_aneg(phydev);
f6ed1b3b 177
ec3a2207
DD
178 return 0;
179no_phy:
180 /* If there is no phy, assume a direct MAC connection and that
181 * the link is up.
182 */
183 netif_carrier_on(dev);
80ff0fd3
DD
184 return 0;
185}
This page took 0.737828 seconds and 5 git commands to generate.