lantiq: clarify VG3503J name
[oweals/openwrt.git] / target / linux / mvebu / patches-4.9 / 414-phy-add-I2C-mdio-bus.patch
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
4
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.
8
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.
12
13 Signed-off-by: Russell King <rmk+kernel@arm.linux.org.uk>
14 ---
15  create mode 100644 drivers/net/phy/mdio-i2c.c
16  create mode 100644 drivers/net/phy/mdio-i2c.h
17
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.
23  
24 +config MDIO_I2C
25 +       tristate
26 +       depends on I2C
27 +       help
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.
31 +
32 +         This is library mode.
33 +
34  config MDIO_MOXART
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
47 --- /dev/null
48 +++ b/drivers/net/phy/mdio-i2c.c
49 @@ -0,0 +1,109 @@
50 +/*
51 + * MDIO I2C bridge
52 + *
53 + * Copyright (C) 2015-2016 Russell King
54 + *
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.
58 + *
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.
63 + */
64 +#include <linux/i2c.h>
65 +#include <linux/phy.h>
66 +
67 +#include "mdio-i2c.h"
68 +
69 +/*
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.
73 + */
74 +static bool i2c_mii_valid_phy_id(int phy_id)
75 +{
76 +       return phy_id != 0x10 && phy_id != 0x11;
77 +}
78 +
79 +static unsigned int i2c_mii_phy_addr(int phy_id)
80 +{
81 +       return phy_id + 0x40;
82 +}
83 +
84 +static int i2c_mii_read(struct mii_bus *bus, int phy_id, int reg)
85 +{
86 +       struct i2c_adapter *i2c = bus->priv;
87 +       struct i2c_msg msgs[2];
88 +       u8 data[2], dev_addr = reg;
89 +       int bus_addr, ret;
90 +
91 +       if (!i2c_mii_valid_phy_id(phy_id))
92 +               return 0xffff;
93 +
94 +       bus_addr = i2c_mii_phy_addr(phy_id);
95 +       msgs[0].addr = bus_addr;
96 +       msgs[0].flags = 0;
97 +       msgs[0].len = 1;
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;
103 +
104 +       ret = i2c_transfer(i2c, msgs, ARRAY_SIZE(msgs));
105 +       if (ret != ARRAY_SIZE(msgs))
106 +               return 0xffff;
107 +
108 +       return data[0] << 8 | data[1];
109 +}
110 +
111 +static int i2c_mii_write(struct mii_bus *bus, int phy_id, int reg, u16 val)
112 +{
113 +       struct i2c_adapter *i2c = bus->priv;
114 +       struct i2c_msg msg;
115 +       int ret;
116 +       u8 data[3];
117 +
118 +       if (!i2c_mii_valid_phy_id(phy_id))
119 +               return 0;
120 +
121 +       data[0] = reg;
122 +       data[1] = val >> 8;
123 +       data[2] = val;
124 +
125 +       msg.addr = i2c_mii_phy_addr(phy_id);
126 +       msg.flags = 0;
127 +       msg.len = 3;
128 +       msg.buf = data;
129 +
130 +       ret = i2c_transfer(i2c, &msg, 1);
131 +
132 +       return ret < 0 ? ret : 0;
133 +}
134 +
135 +struct mii_bus *mdio_i2c_alloc(struct device *parent, struct i2c_adapter *i2c)
136 +{
137 +       struct mii_bus *mii;
138 +
139 +       if (!i2c_check_functionality(i2c, I2C_FUNC_I2C))
140 +               return ERR_PTR(-EINVAL);
141 +
142 +       mii = mdiobus_alloc();
143 +       if (!mii)
144 +               return ERR_PTR(-ENOMEM);
145 +
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;
150 +       mii->priv = i2c;
151 +
152 +       return mii;
153 +}
154 +EXPORT_SYMBOL_GPL(mdio_i2c_alloc);
155 +
156 +MODULE_AUTHOR("Russell King");
157 +MODULE_DESCRIPTION("MDIO I2C bridge library");
158 +MODULE_LICENSE("GPL v2");
159 --- /dev/null
160 +++ b/drivers/net/phy/mdio-i2c.h
161 @@ -0,0 +1,19 @@
162 +/*
163 + * MDIO I2C bridge
164 + *
165 + * Copyright (C) 2015 Russell King
166 + *
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.
170 + */
171 +#ifndef MDIO_I2C_H
172 +#define MDIO_I2C_H
173 +
174 +struct device;
175 +struct i2c_adapter;
176 +struct mii_bus;
177 +
178 +struct mii_bus *mdio_i2c_alloc(struct device *parent, struct i2c_adapter *i2c);
179 +
180 +#endif