Skip to content

Commit

Permalink
Merge branch 'sfp-infra'
Browse files Browse the repository at this point in the history
Russell King says:

====================
Initial SFP support patches

Please review and merge this initial patch set, which is part of a
larger set previously posted adding SFP support to phy and mvneta.

This initial set are focused on cleaning up and reorganising the
fixed-phy code to allow the core software-phy code to be re-used.

These are based on net-next.
====================

Signed-off-by: David S. Miller <davem@davemloft.net>
  • Loading branch information
David S. Miller committed Jun 27, 2016
2 parents 5db1587 + bf7afb2 commit de2fbe7
Show file tree
Hide file tree
Showing 5 changed files with 222 additions and 126 deletions.
4 changes: 4 additions & 0 deletions drivers/net/phy/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ menuconfig PHYLIB

if PHYLIB

config SWPHY
bool

comment "MII PHY device drivers"

config AQUANTIA_PHY
Expand Down Expand Up @@ -159,6 +162,7 @@ config MICROCHIP_PHY
config FIXED_PHY
tristate "Driver for MDIO Bus/PHY emulation with fixed speed/link PHYs"
depends on PHYLIB
select SWPHY
---help---
Adds the platform "fixed" MDIO Bus to cover the boards that use
PHYs that are not connected to the real MDIO bus.
Expand Down
3 changes: 2 additions & 1 deletion drivers/net/phy/Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Makefile for Linux PHY drivers

libphy-objs := phy.o phy_device.o mdio_bus.o mdio_device.o
libphy-y := phy.o phy_device.o mdio_bus.o mdio_device.o
libphy-$(CONFIG_SWPHY) += swphy.o

obj-$(CONFIG_PHYLIB) += libphy.o
obj-$(CONFIG_AQUANTIA_PHY) += aquantia.o
Expand Down
153 changes: 28 additions & 125 deletions drivers/net/phy/fixed_phy.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@
#include <linux/slab.h>
#include <linux/of.h>
#include <linux/gpio.h>
#include <linux/seqlock.h>

#define MII_REGS_NUM 29
#include "swphy.h"

struct fixed_mdio_bus {
struct mii_bus *mii_bus;
Expand All @@ -33,8 +34,8 @@ struct fixed_mdio_bus {

struct fixed_phy {
int addr;
u16 regs[MII_REGS_NUM];
struct phy_device *phydev;
seqcount_t seqcount;
struct fixed_phy_status status;
int (*link_update)(struct net_device *, struct fixed_phy_status *);
struct list_head node;
Expand All @@ -46,133 +47,34 @@ static struct fixed_mdio_bus platform_fmb = {
.phys = LIST_HEAD_INIT(platform_fmb.phys),
};

static int fixed_phy_update_regs(struct fixed_phy *fp)
static void fixed_phy_update(struct fixed_phy *fp)
{
u16 bmsr = BMSR_ANEGCAPABLE;
u16 bmcr = 0;
u16 lpagb = 0;
u16 lpa = 0;

if (gpio_is_valid(fp->link_gpio))
fp->status.link = !!gpio_get_value_cansleep(fp->link_gpio);

if (fp->status.duplex) {
switch (fp->status.speed) {
case 1000:
bmsr |= BMSR_ESTATEN;
break;
case 100:
bmsr |= BMSR_100FULL;
break;
case 10:
bmsr |= BMSR_10FULL;
break;
default:
break;
}
} else {
switch (fp->status.speed) {
case 1000:
bmsr |= BMSR_ESTATEN;
break;
case 100:
bmsr |= BMSR_100HALF;
break;
case 10:
bmsr |= BMSR_10HALF;
break;
default:
break;
}
}

if (fp->status.link) {
bmsr |= BMSR_LSTATUS | BMSR_ANEGCOMPLETE;

if (fp->status.duplex) {
bmcr |= BMCR_FULLDPLX;

switch (fp->status.speed) {
case 1000:
bmcr |= BMCR_SPEED1000;
lpagb |= LPA_1000FULL;
break;
case 100:
bmcr |= BMCR_SPEED100;
lpa |= LPA_100FULL;
break;
case 10:
lpa |= LPA_10FULL;
break;
default:
pr_warn("fixed phy: unknown speed\n");
return -EINVAL;
}
} else {
switch (fp->status.speed) {
case 1000:
bmcr |= BMCR_SPEED1000;
lpagb |= LPA_1000HALF;
break;
case 100:
bmcr |= BMCR_SPEED100;
lpa |= LPA_100HALF;
break;
case 10:
lpa |= LPA_10HALF;
break;
default:
pr_warn("fixed phy: unknown speed\n");
return -EINVAL;
}
}

if (fp->status.pause)
lpa |= LPA_PAUSE_CAP;

if (fp->status.asym_pause)
lpa |= LPA_PAUSE_ASYM;
}

fp->regs[MII_PHYSID1] = 0;
fp->regs[MII_PHYSID2] = 0;

fp->regs[MII_BMSR] = bmsr;
fp->regs[MII_BMCR] = bmcr;
fp->regs[MII_LPA] = lpa;
fp->regs[MII_STAT1000] = lpagb;

return 0;
}

static int fixed_mdio_read(struct mii_bus *bus, int phy_addr, int reg_num)
{
struct fixed_mdio_bus *fmb = bus->priv;
struct fixed_phy *fp;

if (reg_num >= MII_REGS_NUM)
return -1;

/* We do not support emulating Clause 45 over Clause 22 register reads
* return an error instead of bogus data.
*/
switch (reg_num) {
case MII_MMD_CTRL:
case MII_MMD_DATA:
return -1;
default:
break;
}

list_for_each_entry(fp, &fmb->phys, node) {
if (fp->addr == phy_addr) {
/* Issue callback if user registered it. */
if (fp->link_update) {
fp->link_update(fp->phydev->attached_dev,
&fp->status);
fixed_phy_update_regs(fp);
}
return fp->regs[reg_num];
struct fixed_phy_status state;
int s;

do {
s = read_seqcount_begin(&fp->seqcount);
/* Issue callback if user registered it. */
if (fp->link_update) {
fp->link_update(fp->phydev->attached_dev,
&fp->status);
fixed_phy_update(fp);
}
state = fp->status;
} while (read_seqcount_retry(&fp->seqcount, s));

return swphy_read_reg(reg_num, &state);
}
}

Expand Down Expand Up @@ -224,6 +126,7 @@ int fixed_phy_update_state(struct phy_device *phydev,

list_for_each_entry(fp, &fmb->phys, node) {
if (fp->addr == phydev->mdio.addr) {
write_seqcount_begin(&fp->seqcount);
#define _UPD(x) if (changed->x) \
fp->status.x = status->x
_UPD(link);
Expand All @@ -232,7 +135,8 @@ int fixed_phy_update_state(struct phy_device *phydev,
_UPD(pause);
_UPD(asym_pause);
#undef _UPD
fixed_phy_update_regs(fp);
fixed_phy_update(fp);
write_seqcount_end(&fp->seqcount);
return 0;
}
}
Expand All @@ -249,11 +153,15 @@ int fixed_phy_add(unsigned int irq, int phy_addr,
struct fixed_mdio_bus *fmb = &platform_fmb;
struct fixed_phy *fp;

ret = swphy_validate_state(status);
if (ret < 0)
return ret;

fp = kzalloc(sizeof(*fp), GFP_KERNEL);
if (!fp)
return -ENOMEM;

memset(fp->regs, 0xFF, sizeof(fp->regs[0]) * MII_REGS_NUM);
seqcount_init(&fp->seqcount);

if (irq != PHY_POLL)
fmb->mii_bus->irq[phy_addr] = irq;
Expand All @@ -269,17 +177,12 @@ int fixed_phy_add(unsigned int irq, int phy_addr,
goto err_regs;
}

ret = fixed_phy_update_regs(fp);
if (ret)
goto err_gpio;
fixed_phy_update(fp);

list_add_tail(&fp->node, &fmb->phys);

return 0;

err_gpio:
if (gpio_is_valid(fp->link_gpio))
gpio_free(fp->link_gpio);
err_regs:
kfree(fp);
return ret;
Expand Down
Loading

0 comments on commit de2fbe7

Please sign in to comment.