blob: b2f09c8f9c3a8bd4c7a70af7f47918c8e191ddb4 [file] [log] [blame]
xjb04a4022021-11-25 15:01:52 +08001/*
2 * MDIO I2C bridge
3 *
4 * Copyright (C) 2015-2016 Russell King
5 *
6 * This program 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.
9 *
10 * Network PHYs can appear on I2C buses when they are part of SFP module.
11 * This driver exposes these PHYs to the networking PHY code, allowing
12 * our PHY drivers access to these PHYs, and so allowing configuration
13 * of their settings.
14 */
15#include <linux/i2c.h>
16#include <linux/phy.h>
17
18#include "mdio-i2c.h"
19
20/*
21 * I2C bus addresses 0x50 and 0x51 are normally an EEPROM, which is
22 * specified to be present in SFP modules. These correspond with PHY
23 * addresses 16 and 17. Disallow access to these "phy" addresses.
24 */
25static bool i2c_mii_valid_phy_id(int phy_id)
26{
27 return phy_id != 0x10 && phy_id != 0x11;
28}
29
30static unsigned int i2c_mii_phy_addr(int phy_id)
31{
32 return phy_id + 0x40;
33}
34
35static int i2c_mii_read(struct mii_bus *bus, int phy_id, int reg)
36{
37 struct i2c_adapter *i2c = bus->priv;
38 struct i2c_msg msgs[2];
39 u8 addr[3], data[2], *p;
40 int bus_addr, ret;
41
42 if (!i2c_mii_valid_phy_id(phy_id))
43 return 0xffff;
44
45 p = addr;
46 if (reg & MII_ADDR_C45) {
47 *p++ = 0x20 | ((reg >> 16) & 31);
48 *p++ = reg >> 8;
49 }
50 *p++ = reg;
51
52 bus_addr = i2c_mii_phy_addr(phy_id);
53 msgs[0].addr = bus_addr;
54 msgs[0].flags = 0;
55 msgs[0].len = p - addr;
56 msgs[0].buf = addr;
57 msgs[1].addr = bus_addr;
58 msgs[1].flags = I2C_M_RD;
59 msgs[1].len = sizeof(data);
60 msgs[1].buf = data;
61
62 ret = i2c_transfer(i2c, msgs, ARRAY_SIZE(msgs));
63 if (ret != ARRAY_SIZE(msgs))
64 return 0xffff;
65
66 return data[0] << 8 | data[1];
67}
68
69static int i2c_mii_write(struct mii_bus *bus, int phy_id, int reg, u16 val)
70{
71 struct i2c_adapter *i2c = bus->priv;
72 struct i2c_msg msg;
73 int ret;
74 u8 data[5], *p;
75
76 if (!i2c_mii_valid_phy_id(phy_id))
77 return 0;
78
79 p = data;
80 if (reg & MII_ADDR_C45) {
81 *p++ = (reg >> 16) & 31;
82 *p++ = reg >> 8;
83 }
84 *p++ = reg;
85 *p++ = val >> 8;
86 *p++ = val;
87
88 msg.addr = i2c_mii_phy_addr(phy_id);
89 msg.flags = 0;
90 msg.len = p - data;
91 msg.buf = data;
92
93 ret = i2c_transfer(i2c, &msg, 1);
94
95 return ret < 0 ? ret : 0;
96}
97
98struct mii_bus *mdio_i2c_alloc(struct device *parent, struct i2c_adapter *i2c)
99{
100 struct mii_bus *mii;
101
102 if (!i2c_check_functionality(i2c, I2C_FUNC_I2C))
103 return ERR_PTR(-EINVAL);
104
105 mii = mdiobus_alloc();
106 if (!mii)
107 return ERR_PTR(-ENOMEM);
108
109 snprintf(mii->id, MII_BUS_ID_SIZE, "i2c:%s", dev_name(parent));
110 mii->parent = parent;
111 mii->read = i2c_mii_read;
112 mii->write = i2c_mii_write;
113 mii->priv = i2c;
114
115 return mii;
116}
117EXPORT_SYMBOL_GPL(mdio_i2c_alloc);
118
119MODULE_AUTHOR("Russell King");
120MODULE_DESCRIPTION("MDIO I2C bridge library");
121MODULE_LICENSE("GPL v2");