1 From: Russell King <rmk+kernel@arm.linux.org.uk>
2 Date: Fri, 25 Sep 2015 17:43:52 +0100
3 Subject: [PATCH] phy: add I2C mdio bus
5 Add an I2C MDIO bus bridge library, to allow phylib to access PHYs which
6 are connected to an I2C bus instead of the more conventional MDIO bus.
7 Such PHYs can be found in SFP adapters and SFF modules.
9 Since PHYs appear at I2C bus address 0x40..0x5f, and 0x50/0x51 are
10 reserved for SFP EEPROMs/diagnostics, we must not allow the MDIO bus
11 to access these I2C addresses.
13 Signed-off-by: Russell King <rmk+kernel@arm.linux.org.uk>
15 create mode 100644 drivers/net/phy/mdio-i2c.c
16 create mode 100644 drivers/net/phy/mdio-i2c.h
18 --- a/drivers/net/phy/Kconfig
19 +++ b/drivers/net/phy/Kconfig
20 @@ -105,6 +105,16 @@ config MDIO_HISI_FEMAC
21 This module provides a driver for the MDIO busses found in the
22 Hisilicon SoC that have an Fast Ethernet MAC.
28 + Support I2C based PHYs. This provides a MDIO bus bridged
29 + to I2C to allow PHYs connected in I2C mode to be accessed
30 + using the existing infrastructure.
32 + This is library mode.
35 tristate "MOXA ART MDIO interface support"
36 depends on ARCH_MOXART
37 --- a/drivers/net/phy/Makefile
38 +++ b/drivers/net/phy/Makefile
39 @@ -33,6 +33,7 @@ obj-$(CONFIG_MDIO_BUS_MUX_MMIOREG) += md
40 obj-$(CONFIG_MDIO_CAVIUM) += mdio-cavium.o
41 obj-$(CONFIG_MDIO_GPIO) += mdio-gpio.o
42 obj-$(CONFIG_MDIO_HISI_FEMAC) += mdio-hisi-femac.o
43 +obj-$(CONFIG_MDIO_I2C) += mdio-i2c.o
44 obj-$(CONFIG_MDIO_MOXART) += mdio-moxart.o
45 obj-$(CONFIG_MDIO_OCTEON) += mdio-octeon.o
46 obj-$(CONFIG_MDIO_SUN4I) += mdio-sun4i.o
48 +++ b/drivers/net/phy/mdio-i2c.c
53 + * Copyright (C) 2015-2016 Russell King
55 + * This program is free software; you can redistribute it and/or modify
56 + * it under the terms of the GNU General Public License version 2 as
57 + * published by the Free Software Foundation.
59 + * Network PHYs can appear on I2C buses when they are part of SFP module.
60 + * This driver exposes these PHYs to the networking PHY code, allowing
61 + * our PHY drivers access to these PHYs, and so allowing configuration
62 + * of their settings.
64 +#include <linux/i2c.h>
65 +#include <linux/phy.h>
67 +#include "mdio-i2c.h"
70 + * I2C bus addresses 0x50 and 0x51 are normally an EEPROM, which is
71 + * specified to be present in SFP modules. These correspond with PHY
72 + * addresses 16 and 17. Disallow access to these "phy" addresses.
74 +static bool i2c_mii_valid_phy_id(int phy_id)
76 + return phy_id != 0x10 && phy_id != 0x11;
79 +static unsigned int i2c_mii_phy_addr(int phy_id)
81 + return phy_id + 0x40;
84 +static int i2c_mii_read(struct mii_bus *bus, int phy_id, int reg)
86 + struct i2c_adapter *i2c = bus->priv;
87 + struct i2c_msg msgs[2];
88 + u8 data[2], dev_addr = reg;
91 + if (!i2c_mii_valid_phy_id(phy_id))
94 + bus_addr = i2c_mii_phy_addr(phy_id);
95 + msgs[0].addr = bus_addr;
98 + msgs[0].buf = &dev_addr;
99 + msgs[1].addr = bus_addr;
100 + msgs[1].flags = I2C_M_RD;
101 + msgs[1].len = sizeof(data);
102 + msgs[1].buf = data;
104 + ret = i2c_transfer(i2c, msgs, ARRAY_SIZE(msgs));
105 + if (ret != ARRAY_SIZE(msgs))
108 + return data[0] << 8 | data[1];
111 +static int i2c_mii_write(struct mii_bus *bus, int phy_id, int reg, u16 val)
113 + struct i2c_adapter *i2c = bus->priv;
114 + struct i2c_msg msg;
118 + if (!i2c_mii_valid_phy_id(phy_id))
122 + data[1] = val >> 8;
125 + msg.addr = i2c_mii_phy_addr(phy_id);
130 + ret = i2c_transfer(i2c, &msg, 1);
132 + return ret < 0 ? ret : 0;
135 +struct mii_bus *mdio_i2c_alloc(struct device *parent, struct i2c_adapter *i2c)
137 + struct mii_bus *mii;
139 + if (!i2c_check_functionality(i2c, I2C_FUNC_I2C))
140 + return ERR_PTR(-EINVAL);
142 + mii = mdiobus_alloc();
144 + return ERR_PTR(-ENOMEM);
146 + snprintf(mii->id, MII_BUS_ID_SIZE, "i2c:%s", dev_name(parent));
147 + mii->parent = parent;
148 + mii->read = i2c_mii_read;
149 + mii->write = i2c_mii_write;
154 +EXPORT_SYMBOL_GPL(mdio_i2c_alloc);
156 +MODULE_AUTHOR("Russell King");
157 +MODULE_DESCRIPTION("MDIO I2C bridge library");
158 +MODULE_LICENSE("GPL v2");
160 +++ b/drivers/net/phy/mdio-i2c.h
165 + * Copyright (C) 2015 Russell King
167 + * This program is free software; you can redistribute it and/or modify
168 + * it under the terms of the GNU General Public License version 2 as
169 + * published by the Free Software Foundation.
178 +struct mii_bus *mdio_i2c_alloc(struct device *parent, struct i2c_adapter *i2c);