2009-10-14 22:13:45 +00:00
|
|
|
/*******************************************************************************
|
|
|
|
STMMAC Ethernet Driver -- MDIO bus implementation
|
|
|
|
Provides Bus interface for MII registers
|
|
|
|
|
|
|
|
Copyright (C) 2007-2009 STMicroelectronics Ltd
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify it
|
|
|
|
under the terms and conditions of the GNU General Public License,
|
|
|
|
version 2, as published by the Free Software Foundation.
|
|
|
|
|
|
|
|
This program is distributed in the hope it will be useful, but WITHOUT
|
|
|
|
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License along with
|
|
|
|
this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
|
|
|
|
The full GNU General Public License is included in this distribution in
|
|
|
|
the file called "COPYING".
|
|
|
|
|
|
|
|
Author: Carl Shaw <carl.shaw@st.com>
|
|
|
|
Maintainer: Giuseppe Cavallaro <peppe.cavallaro@st.com>
|
|
|
|
*******************************************************************************/
|
|
|
|
|
|
|
|
#include <linux/mii.h>
|
|
|
|
#include <linux/phy.h>
|
include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit slab.h inclusion from percpu.h
percpu.h is included by sched.h and module.h and thus ends up being
included when building most .c files. percpu.h includes slab.h which
in turn includes gfp.h making everything defined by the two files
universally available and complicating inclusion dependencies.
percpu.h -> slab.h dependency is about to be removed. Prepare for
this change by updating users of gfp and slab facilities include those
headers directly instead of assuming availability. As this conversion
needs to touch large number of source files, the following script is
used as the basis of conversion.
http://userweb.kernel.org/~tj/misc/slabh-sweep.py
The script does the followings.
* Scan files for gfp and slab usages and update includes such that
only the necessary includes are there. ie. if only gfp is used,
gfp.h, if slab is used, slab.h.
* When the script inserts a new include, it looks at the include
blocks and try to put the new include such that its order conforms
to its surrounding. It's put in the include block which contains
core kernel includes, in the same order that the rest are ordered -
alphabetical, Christmas tree, rev-Xmas-tree or at the end if there
doesn't seem to be any matching order.
* If the script can't find a place to put a new include (mostly
because the file doesn't have fitting include block), it prints out
an error message indicating which .h file needs to be added to the
file.
The conversion was done in the following steps.
1. The initial automatic conversion of all .c files updated slightly
over 4000 files, deleting around 700 includes and adding ~480 gfp.h
and ~3000 slab.h inclusions. The script emitted errors for ~400
files.
2. Each error was manually checked. Some didn't need the inclusion,
some needed manual addition while adding it to implementation .h or
embedding .c file was more appropriate for others. This step added
inclusions to around 150 files.
3. The script was run again and the output was compared to the edits
from #2 to make sure no file was left behind.
4. Several build tests were done and a couple of problems were fixed.
e.g. lib/decompress_*.c used malloc/free() wrappers around slab
APIs requiring slab.h to be added manually.
5. The script was run on all .h files but without automatically
editing them as sprinkling gfp.h and slab.h inclusions around .h
files could easily lead to inclusion dependency hell. Most gfp.h
inclusion directives were ignored as stuff from gfp.h was usually
wildly available and often used in preprocessor macros. Each
slab.h inclusion directive was examined and added manually as
necessary.
6. percpu.h was updated not to include slab.h.
7. Build test were done on the following configurations and failures
were fixed. CONFIG_GCOV_KERNEL was turned off for all tests (as my
distributed build env didn't work with gcov compiles) and a few
more options had to be turned off depending on archs to make things
build (like ipr on powerpc/64 which failed due to missing writeq).
* x86 and x86_64 UP and SMP allmodconfig and a custom test config.
* powerpc and powerpc64 SMP allmodconfig
* sparc and sparc64 SMP allmodconfig
* ia64 SMP allmodconfig
* s390 SMP allmodconfig
* alpha SMP allmodconfig
* um on x86_64 SMP allmodconfig
8. percpu.h modifications were reverted so that it could be applied as
a separate patch and serve as bisection point.
Given the fact that I had only a couple of failures from tests on step
6, I'm fairly confident about the coverage of this conversion patch.
If there is a breakage, it's likely to be something in one of the arch
headers which should be easily discoverable easily on most builds of
the specific arch.
Signed-off-by: Tejun Heo <tj@kernel.org>
Guess-its-ok-by: Christoph Lameter <cl@linux-foundation.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Lee Schermerhorn <Lee.Schermerhorn@hp.com>
2010-03-24 08:04:11 +00:00
|
|
|
#include <linux/slab.h>
|
2013-07-04 09:35:48 +00:00
|
|
|
#include <linux/of.h>
|
|
|
|
#include <linux/of_gpio.h>
|
2015-12-14 03:31:59 +00:00
|
|
|
#include <linux/of_mdio.h>
|
2011-06-16 11:01:34 +00:00
|
|
|
#include <asm/io.h>
|
2009-10-14 22:13:45 +00:00
|
|
|
|
|
|
|
#include "stmmac.h"
|
|
|
|
|
|
|
|
#define MII_BUSY 0x00000001
|
|
|
|
#define MII_WRITE 0x00000002
|
|
|
|
|
2012-04-04 04:33:24 +00:00
|
|
|
static int stmmac_mdio_busy_wait(void __iomem *ioaddr, unsigned int mii_addr)
|
|
|
|
{
|
|
|
|
unsigned long curr;
|
|
|
|
unsigned long finish = jiffies + 3 * HZ;
|
|
|
|
|
|
|
|
do {
|
|
|
|
curr = jiffies;
|
|
|
|
if (readl(ioaddr + mii_addr) & MII_BUSY)
|
|
|
|
cpu_relax();
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
} while (!time_after_eq(curr, finish));
|
|
|
|
|
|
|
|
return -EBUSY;
|
|
|
|
}
|
|
|
|
|
2009-10-14 22:13:45 +00:00
|
|
|
/**
|
|
|
|
* stmmac_mdio_read
|
|
|
|
* @bus: points to the mii_bus structure
|
|
|
|
* @phyaddr: MII addr reg bits 15-11
|
|
|
|
* @phyreg: MII addr reg bits 10-6
|
|
|
|
* Description: it reads data from the MII register from within the phy device.
|
|
|
|
* For the 7111 GMAC, we must set the bit 0 in the MII address register while
|
|
|
|
* accessing the PHY registers.
|
|
|
|
* Fortunately, it seems this has no drawback for the 7109 MAC.
|
|
|
|
*/
|
|
|
|
static int stmmac_mdio_read(struct mii_bus *bus, int phyaddr, int phyreg)
|
|
|
|
{
|
|
|
|
struct net_device *ndev = bus->priv;
|
|
|
|
struct stmmac_priv *priv = netdev_priv(ndev);
|
2010-01-06 23:07:17 +00:00
|
|
|
unsigned int mii_address = priv->hw->mii.addr;
|
|
|
|
unsigned int mii_data = priv->hw->mii.data;
|
2009-10-14 22:13:45 +00:00
|
|
|
|
|
|
|
int data;
|
|
|
|
u16 regValue = (((phyaddr << 11) & (0x0000F800)) |
|
|
|
|
((phyreg << 6) & (0x000007C0)));
|
2012-04-04 04:33:27 +00:00
|
|
|
regValue |= MII_BUSY | ((priv->clk_csr & 0xF) << 2);
|
2009-10-14 22:13:45 +00:00
|
|
|
|
2012-04-04 04:33:24 +00:00
|
|
|
if (stmmac_mdio_busy_wait(priv->ioaddr, mii_address))
|
|
|
|
return -EBUSY;
|
|
|
|
|
2010-08-23 20:40:42 +00:00
|
|
|
writel(regValue, priv->ioaddr + mii_address);
|
2012-04-04 04:33:24 +00:00
|
|
|
|
|
|
|
if (stmmac_mdio_busy_wait(priv->ioaddr, mii_address))
|
|
|
|
return -EBUSY;
|
2009-10-14 22:13:45 +00:00
|
|
|
|
|
|
|
/* Read the data from the MII data register */
|
2010-08-23 20:40:42 +00:00
|
|
|
data = (int)readl(priv->ioaddr + mii_data);
|
2009-10-14 22:13:45 +00:00
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* stmmac_mdio_write
|
|
|
|
* @bus: points to the mii_bus structure
|
|
|
|
* @phyaddr: MII addr reg bits 15-11
|
|
|
|
* @phyreg: MII addr reg bits 10-6
|
|
|
|
* @phydata: phy data
|
|
|
|
* Description: it writes the data into the MII register from within the device.
|
|
|
|
*/
|
|
|
|
static int stmmac_mdio_write(struct mii_bus *bus, int phyaddr, int phyreg,
|
|
|
|
u16 phydata)
|
|
|
|
{
|
|
|
|
struct net_device *ndev = bus->priv;
|
|
|
|
struct stmmac_priv *priv = netdev_priv(ndev);
|
2010-01-06 23:07:17 +00:00
|
|
|
unsigned int mii_address = priv->hw->mii.addr;
|
|
|
|
unsigned int mii_data = priv->hw->mii.data;
|
2009-10-14 22:13:45 +00:00
|
|
|
|
|
|
|
u16 value =
|
|
|
|
(((phyaddr << 11) & (0x0000F800)) | ((phyreg << 6) & (0x000007C0)))
|
|
|
|
| MII_WRITE;
|
|
|
|
|
2012-04-04 04:33:27 +00:00
|
|
|
value |= MII_BUSY | ((priv->clk_csr & 0xF) << 2);
|
2010-09-17 03:23:39 +00:00
|
|
|
|
2009-10-14 22:13:45 +00:00
|
|
|
/* Wait until any existing MII operation is complete */
|
2012-04-04 04:33:24 +00:00
|
|
|
if (stmmac_mdio_busy_wait(priv->ioaddr, mii_address))
|
|
|
|
return -EBUSY;
|
2009-10-14 22:13:45 +00:00
|
|
|
|
|
|
|
/* Set the MII address register to write */
|
2010-08-23 20:40:42 +00:00
|
|
|
writel(phydata, priv->ioaddr + mii_data);
|
|
|
|
writel(value, priv->ioaddr + mii_address);
|
2009-10-14 22:13:45 +00:00
|
|
|
|
|
|
|
/* Wait until any existing MII operation is complete */
|
2012-04-04 04:33:24 +00:00
|
|
|
return stmmac_mdio_busy_wait(priv->ioaddr, mii_address);
|
2009-10-14 22:13:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* stmmac_mdio_reset
|
|
|
|
* @bus: points to the mii_bus structure
|
|
|
|
* Description: reset the MII bus
|
|
|
|
*/
|
2014-01-16 10:52:27 +00:00
|
|
|
int stmmac_mdio_reset(struct mii_bus *bus)
|
2009-10-14 22:13:45 +00:00
|
|
|
{
|
2011-12-21 03:58:19 +00:00
|
|
|
#if defined(CONFIG_STMMAC_PLATFORM)
|
2009-10-14 22:13:45 +00:00
|
|
|
struct net_device *ndev = bus->priv;
|
|
|
|
struct stmmac_priv *priv = netdev_priv(ndev);
|
2010-01-06 23:07:17 +00:00
|
|
|
unsigned int mii_address = priv->hw->mii.addr;
|
2013-07-04 09:35:48 +00:00
|
|
|
struct stmmac_mdio_bus_data *data = priv->plat->mdio_bus_data;
|
|
|
|
|
|
|
|
#ifdef CONFIG_OF
|
|
|
|
if (priv->device->of_node) {
|
|
|
|
|
|
|
|
if (data->reset_gpio < 0) {
|
|
|
|
struct device_node *np = priv->device->of_node;
|
|
|
|
if (!np)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
data->reset_gpio = of_get_named_gpio(np,
|
|
|
|
"snps,reset-gpio", 0);
|
|
|
|
if (data->reset_gpio < 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
data->active_low = of_property_read_bool(np,
|
|
|
|
"snps,reset-active-low");
|
|
|
|
of_property_read_u32_array(np,
|
|
|
|
"snps,reset-delays-us", data->delays, 3);
|
|
|
|
|
2015-11-26 07:35:44 +00:00
|
|
|
if (gpio_request(data->reset_gpio, "mdio-reset"))
|
|
|
|
return 0;
|
|
|
|
}
|
2013-07-04 09:35:48 +00:00
|
|
|
|
2015-11-26 07:35:44 +00:00
|
|
|
gpio_direction_output(data->reset_gpio,
|
|
|
|
data->active_low ? 1 : 0);
|
|
|
|
if (data->delays[0])
|
|
|
|
msleep(DIV_ROUND_UP(data->delays[0], 1000));
|
2015-09-11 20:25:48 +00:00
|
|
|
|
2015-11-26 07:35:44 +00:00
|
|
|
gpio_set_value(data->reset_gpio, data->active_low ? 0 : 1);
|
|
|
|
if (data->delays[1])
|
|
|
|
msleep(DIV_ROUND_UP(data->delays[1], 1000));
|
2015-09-11 20:25:48 +00:00
|
|
|
|
2015-11-26 07:35:44 +00:00
|
|
|
gpio_set_value(data->reset_gpio, data->active_low ? 1 : 0);
|
|
|
|
if (data->delays[2])
|
|
|
|
msleep(DIV_ROUND_UP(data->delays[2], 1000));
|
2013-07-04 09:35:48 +00:00
|
|
|
}
|
|
|
|
#endif
|
2009-10-14 22:13:45 +00:00
|
|
|
|
2013-07-04 09:35:48 +00:00
|
|
|
if (data->phy_reset) {
|
2009-10-14 22:13:45 +00:00
|
|
|
pr_debug("stmmac_mdio_reset: calling phy_reset\n");
|
2013-07-04 09:35:48 +00:00
|
|
|
data->phy_reset(priv->plat->bsp_priv);
|
2009-10-14 22:13:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* This is a workaround for problems with the STE101P PHY.
|
|
|
|
* It doesn't complete its reset until at least one clock cycle
|
|
|
|
* on MDC, so perform a dummy mdio read.
|
|
|
|
*/
|
2010-08-23 20:40:42 +00:00
|
|
|
writel(0, priv->ioaddr + mii_address);
|
2011-12-21 03:58:19 +00:00
|
|
|
#endif
|
2009-10-14 22:13:45 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* stmmac_mdio_register
|
|
|
|
* @ndev: net device structure
|
|
|
|
* Description: it registers the MII bus
|
|
|
|
*/
|
|
|
|
int stmmac_mdio_register(struct net_device *ndev)
|
|
|
|
{
|
|
|
|
int err = 0;
|
|
|
|
struct mii_bus *new_bus;
|
|
|
|
struct stmmac_priv *priv = netdev_priv(ndev);
|
2011-07-20 00:05:23 +00:00
|
|
|
struct stmmac_mdio_bus_data *mdio_bus_data = priv->plat->mdio_bus_data;
|
2009-10-14 22:13:45 +00:00
|
|
|
int addr, found;
|
2015-12-14 03:31:59 +00:00
|
|
|
struct device_node *mdio_node = NULL;
|
|
|
|
struct device_node *child_node = NULL;
|
2009-10-14 22:13:45 +00:00
|
|
|
|
2011-07-20 00:05:23 +00:00
|
|
|
if (!mdio_bus_data)
|
|
|
|
return 0;
|
|
|
|
|
2015-12-14 03:31:59 +00:00
|
|
|
if (IS_ENABLED(CONFIG_OF)) {
|
|
|
|
for_each_child_of_node(priv->device->of_node, child_node) {
|
|
|
|
if (of_device_is_compatible(child_node,
|
|
|
|
"snps,dwmac-mdio")) {
|
|
|
|
mdio_node = child_node;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mdio_node) {
|
|
|
|
netdev_dbg(ndev, "FOUND MDIO subnode\n");
|
|
|
|
} else {
|
|
|
|
netdev_err(ndev, "NO MDIO subnode\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-14 22:13:45 +00:00
|
|
|
new_bus = mdiobus_alloc();
|
|
|
|
if (new_bus == NULL)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2016-01-06 19:11:15 +00:00
|
|
|
if (mdio_bus_data->irqs)
|
|
|
|
memcpy(new_bus->irq, mdio_bus_data, sizeof(new_bus->irq));
|
2009-10-14 22:13:45 +00:00
|
|
|
|
2013-07-04 09:35:48 +00:00
|
|
|
#ifdef CONFIG_OF
|
|
|
|
if (priv->device->of_node)
|
|
|
|
mdio_bus_data->reset_gpio = -1;
|
|
|
|
#endif
|
|
|
|
|
2012-01-23 23:26:48 +00:00
|
|
|
new_bus->name = "stmmac";
|
2009-10-14 22:13:45 +00:00
|
|
|
new_bus->read = &stmmac_mdio_read;
|
|
|
|
new_bus->write = &stmmac_mdio_write;
|
|
|
|
new_bus->reset = &stmmac_mdio_reset;
|
2012-01-09 23:59:20 +00:00
|
|
|
snprintf(new_bus->id, MII_BUS_ID_SIZE, "%s-%x",
|
2013-04-08 02:10:01 +00:00
|
|
|
new_bus->name, priv->plat->bus_id);
|
2009-10-14 22:13:45 +00:00
|
|
|
new_bus->priv = ndev;
|
2011-07-20 00:05:23 +00:00
|
|
|
new_bus->phy_mask = mdio_bus_data->phy_mask;
|
2009-10-14 22:13:45 +00:00
|
|
|
new_bus->parent = priv->device;
|
2015-12-14 03:31:59 +00:00
|
|
|
|
|
|
|
err = of_mdiobus_register(new_bus, mdio_node);
|
2009-10-14 22:13:45 +00:00
|
|
|
if (err != 0) {
|
|
|
|
pr_err("%s: Cannot register as MDIO bus\n", new_bus->name);
|
|
|
|
goto bus_register_fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
found = 0;
|
2011-07-20 00:05:23 +00:00
|
|
|
for (addr = 0; addr < PHY_MAX_ADDR; addr++) {
|
2016-01-06 19:11:18 +00:00
|
|
|
struct phy_device *phydev = mdiobus_get_phy(new_bus, addr);
|
2009-10-14 22:13:45 +00:00
|
|
|
if (phydev) {
|
2011-07-20 00:05:23 +00:00
|
|
|
int act = 0;
|
|
|
|
char irq_num[4];
|
|
|
|
char *irq_str;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If an IRQ was provided to be assigned after
|
|
|
|
* the bus probe, do it here.
|
|
|
|
*/
|
|
|
|
if ((mdio_bus_data->irqs == NULL) &&
|
|
|
|
(mdio_bus_data->probed_phy_irq > 0)) {
|
2016-01-06 19:11:15 +00:00
|
|
|
new_bus->irq[addr] =
|
|
|
|
mdio_bus_data->probed_phy_irq;
|
2011-07-20 00:05:23 +00:00
|
|
|
phydev->irq = mdio_bus_data->probed_phy_irq;
|
2009-10-14 22:13:45 +00:00
|
|
|
}
|
2011-07-20 00:05:23 +00:00
|
|
|
|
|
|
|
/*
|
2014-08-25 11:31:16 +00:00
|
|
|
* If we're going to bind the MAC to this PHY bus,
|
2011-07-20 00:05:23 +00:00
|
|
|
* and no PHY number was provided to the MAC,
|
|
|
|
* use the one probed here.
|
|
|
|
*/
|
2012-08-30 05:50:43 +00:00
|
|
|
if (priv->plat->phy_addr == -1)
|
2011-07-20 00:05:23 +00:00
|
|
|
priv->plat->phy_addr = addr;
|
|
|
|
|
2012-08-30 05:50:43 +00:00
|
|
|
act = (priv->plat->phy_addr == addr);
|
2011-07-20 00:05:23 +00:00
|
|
|
switch (phydev->irq) {
|
|
|
|
case PHY_POLL:
|
|
|
|
irq_str = "POLL";
|
|
|
|
break;
|
|
|
|
case PHY_IGNORE_INTERRUPT:
|
|
|
|
irq_str = "IGNORE";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
sprintf(irq_num, "%d", phydev->irq);
|
|
|
|
irq_str = irq_num;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
pr_info("%s: PHY ID %08x at %d IRQ %s (%s)%s\n",
|
|
|
|
ndev->name, phydev->phy_id, addr,
|
2016-01-06 19:11:10 +00:00
|
|
|
irq_str, phydev_name(phydev),
|
2011-07-20 00:05:23 +00:00
|
|
|
act ? " active" : "");
|
2009-10-14 22:13:45 +00:00
|
|
|
found = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-14 03:31:59 +00:00
|
|
|
if (!found && !mdio_node) {
|
2014-09-10 03:27:44 +00:00
|
|
|
pr_warn("%s: No PHY found\n", ndev->name);
|
2013-02-06 20:47:52 +00:00
|
|
|
mdiobus_unregister(new_bus);
|
|
|
|
mdiobus_free(new_bus);
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
|
|
|
priv->mii = new_bus;
|
2009-10-14 22:13:45 +00:00
|
|
|
|
|
|
|
return 0;
|
2011-07-20 00:05:23 +00:00
|
|
|
|
2009-10-14 22:13:45 +00:00
|
|
|
bus_register_fail:
|
2011-07-20 00:05:23 +00:00
|
|
|
mdiobus_free(new_bus);
|
2009-10-14 22:13:45 +00:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* stmmac_mdio_unregister
|
|
|
|
* @ndev: net device structure
|
|
|
|
* Description: it unregisters the MII bus
|
|
|
|
*/
|
|
|
|
int stmmac_mdio_unregister(struct net_device *ndev)
|
|
|
|
{
|
|
|
|
struct stmmac_priv *priv = netdev_priv(ndev);
|
|
|
|
|
2012-08-30 05:49:58 +00:00
|
|
|
if (!priv->mii)
|
|
|
|
return 0;
|
|
|
|
|
2009-10-14 22:13:45 +00:00
|
|
|
mdiobus_unregister(priv->mii);
|
|
|
|
priv->mii->priv = NULL;
|
2011-07-20 00:05:23 +00:00
|
|
|
mdiobus_free(priv->mii);
|
|
|
|
priv->mii = NULL;
|
2009-10-14 22:13:45 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|