1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (C) 2015-2016 Russell King
6 * Copyright (C) 2021 Marek Behun
8 * Network PHYs can appear on I2C buses when they are part of SFP module.
9 * This driver exposes these PHYs to the networking PHY code, allowing
10 * our PHY drivers access to these PHYs, and so allowing configuration
13 #include <linux/i2c.h>
14 #include <linux/mdio/mdio-i2c.h>
15 #include <linux/phy.h>
16 #include <linux/sfp.h>
19 * I2C bus addresses 0x50 and 0x51 are normally an EEPROM, which is
20 * specified to be present in SFP modules. These correspond with PHY
21 * addresses 16 and 17. Disallow access to these "phy" addresses.
23 static bool i2c_mii_valid_phy_id(int phy_id
)
25 return phy_id
!= 0x10 && phy_id
!= 0x11;
28 static unsigned int i2c_mii_phy_addr(int phy_id
)
33 static int i2c_mii_read_default_c45(struct mii_bus
*bus
, int phy_id
, int devad
,
36 struct i2c_adapter
*i2c
= bus
->priv
;
37 struct i2c_msg msgs
[2];
38 u8 addr
[3], data
[2], *p
;
41 if (!i2c_mii_valid_phy_id(phy_id
))
51 bus_addr
= i2c_mii_phy_addr(phy_id
);
52 msgs
[0].addr
= bus_addr
;
54 msgs
[0].len
= p
- addr
;
56 msgs
[1].addr
= bus_addr
;
57 msgs
[1].flags
= I2C_M_RD
;
58 msgs
[1].len
= sizeof(data
);
61 ret
= i2c_transfer(i2c
, msgs
, ARRAY_SIZE(msgs
));
62 if (ret
!= ARRAY_SIZE(msgs
))
65 return data
[0] << 8 | data
[1];
68 static int i2c_mii_write_default_c45(struct mii_bus
*bus
, int phy_id
,
69 int devad
, int reg
, u16 val
)
71 struct i2c_adapter
*i2c
= bus
->priv
;
76 if (!i2c_mii_valid_phy_id(phy_id
))
88 msg
.addr
= i2c_mii_phy_addr(phy_id
);
93 ret
= i2c_transfer(i2c
, &msg
, 1);
95 return ret
< 0 ? ret
: 0;
98 static int i2c_mii_read_default_c22(struct mii_bus
*bus
, int phy_id
, int reg
)
100 return i2c_mii_read_default_c45(bus
, phy_id
, -1, reg
);
103 static int i2c_mii_write_default_c22(struct mii_bus
*bus
, int phy_id
, int reg
,
106 return i2c_mii_write_default_c45(bus
, phy_id
, -1, reg
, val
);
109 /* RollBall SFPs do not access internal PHY via I2C address 0x56, but
110 * instead via address 0x51, when SFP page is set to 0x03 and password to
113 * address size contents description
114 * ------- ---- -------- -----------
115 * 0x80 1 CMD 0x01/0x02/0x04 for write/read/done
116 * 0x81 1 DEV Clause 45 device
117 * 0x82 2 REG Clause 45 register
118 * 0x84 2 VAL Register value
120 #define ROLLBALL_PHY_I2C_ADDR 0x51
122 #define ROLLBALL_PASSWORD (SFP_VSL + 3)
124 #define ROLLBALL_CMD_ADDR 0x80
125 #define ROLLBALL_DATA_ADDR 0x81
127 #define ROLLBALL_CMD_WRITE 0x01
128 #define ROLLBALL_CMD_READ 0x02
129 #define ROLLBALL_CMD_DONE 0x04
131 #define SFP_PAGE_ROLLBALL_MDIO 3
133 static int __i2c_transfer_err(struct i2c_adapter
*i2c
, struct i2c_msg
*msgs
,
138 ret
= __i2c_transfer(i2c
, msgs
, num
);
147 static int __i2c_rollball_get_page(struct i2c_adapter
*i2c
, int bus_addr
,
150 struct i2c_msg msgs
[2];
153 msgs
[0].addr
= bus_addr
;
158 msgs
[1].addr
= bus_addr
;
159 msgs
[1].flags
= I2C_M_RD
;
163 return __i2c_transfer_err(i2c
, msgs
, 2);
166 static int __i2c_rollball_set_page(struct i2c_adapter
*i2c
, int bus_addr
,
180 return __i2c_transfer_err(i2c
, &msg
, 1);
183 /* In order to not interfere with other SFP code (which possibly may manipulate
184 * SFP_PAGE), for every transfer we do this:
186 * 2. save content of SFP_PAGE
187 * 3. set SFP_PAGE to 3
189 * 5. restore original SFP_PAGE
191 * Note that one might think that steps 2 to 5 could be theoretically done all
192 * in one call to i2c_transfer (by constructing msgs array in such a way), but
193 * unfortunately tests show that this does not work :-( Changed SFP_PAGE does
194 * not take into account until i2c_transfer() is done.
196 static int i2c_transfer_rollball(struct i2c_adapter
*i2c
,
197 struct i2c_msg
*msgs
, int num
)
199 int ret
, main_err
= 0;
202 i2c_lock_bus(i2c
, I2C_LOCK_SEGMENT
);
204 /* save original page */
205 ret
= __i2c_rollball_get_page(i2c
, msgs
->addr
, &saved_page
);
209 /* change to RollBall MDIO page */
210 ret
= __i2c_rollball_set_page(i2c
, msgs
->addr
, SFP_PAGE_ROLLBALL_MDIO
);
214 /* do the transfer; we try to restore original page if this fails */
215 ret
= __i2c_transfer_err(i2c
, msgs
, num
);
219 /* restore original page */
220 ret
= __i2c_rollball_set_page(i2c
, msgs
->addr
, saved_page
);
223 i2c_unlock_bus(i2c
, I2C_LOCK_SEGMENT
);
225 return main_err
? : ret
;
228 static int i2c_rollball_mii_poll(struct mii_bus
*bus
, int bus_addr
, u8
*buf
,
231 struct i2c_adapter
*i2c
= bus
->priv
;
232 struct i2c_msg msgs
[2];
233 u8 cmd_addr
, tmp
, *res
;
236 cmd_addr
= ROLLBALL_CMD_ADDR
;
238 res
= buf
? buf
: &tmp
;
241 msgs
[0].addr
= bus_addr
;
244 msgs
[0].buf
= &cmd_addr
;
246 msgs
[1].addr
= bus_addr
;
247 msgs
[1].flags
= I2C_M_RD
;
251 /* By experiment it takes up to 70 ms to access a register for these
252 * SFPs. Sleep 20ms between iterations and try 10 times.
258 ret
= i2c_transfer_rollball(i2c
, msgs
, ARRAY_SIZE(msgs
));
262 if (*res
== ROLLBALL_CMD_DONE
)
266 dev_dbg(&bus
->dev
, "poll timed out\n");
271 static int i2c_rollball_mii_cmd(struct mii_bus
*bus
, int bus_addr
, u8 cmd
,
272 u8
*data
, size_t len
)
274 struct i2c_adapter
*i2c
= bus
->priv
;
275 struct i2c_msg msgs
[2];
278 cmdbuf
[0] = ROLLBALL_CMD_ADDR
;
281 msgs
[0].addr
= bus_addr
;
286 msgs
[1].addr
= bus_addr
;
288 msgs
[1].len
= sizeof(cmdbuf
);
289 msgs
[1].buf
= cmdbuf
;
291 return i2c_transfer_rollball(i2c
, msgs
, ARRAY_SIZE(msgs
));
294 static int i2c_mii_read_rollball(struct mii_bus
*bus
, int phy_id
, int devad
,
301 bus_addr
= i2c_mii_phy_addr(phy_id
);
302 if (bus_addr
!= ROLLBALL_PHY_I2C_ADDR
)
305 buf
[0] = ROLLBALL_DATA_ADDR
;
307 buf
[2] = (reg
>> 8) & 0xff;
310 ret
= i2c_rollball_mii_cmd(bus
, bus_addr
, ROLLBALL_CMD_READ
, buf
,
315 ret
= i2c_rollball_mii_poll(bus
, bus_addr
, res
, sizeof(res
));
316 if (ret
== -ETIMEDOUT
)
321 val
= res
[4] << 8 | res
[5];
326 static int i2c_mii_write_rollball(struct mii_bus
*bus
, int phy_id
, int devad
,
332 bus_addr
= i2c_mii_phy_addr(phy_id
);
333 if (bus_addr
!= ROLLBALL_PHY_I2C_ADDR
)
336 buf
[0] = ROLLBALL_DATA_ADDR
;
338 buf
[2] = (reg
>> 8) & 0xff;
343 ret
= i2c_rollball_mii_cmd(bus
, bus_addr
, ROLLBALL_CMD_WRITE
, buf
,
348 ret
= i2c_rollball_mii_poll(bus
, bus_addr
, NULL
, 0);
355 static int i2c_mii_init_rollball(struct i2c_adapter
*i2c
)
361 pw
[0] = ROLLBALL_PASSWORD
;
367 msg
.addr
= ROLLBALL_PHY_I2C_ADDR
;
369 msg
.len
= sizeof(pw
);
372 ret
= i2c_transfer(i2c
, &msg
, 1);
381 struct mii_bus
*mdio_i2c_alloc(struct device
*parent
, struct i2c_adapter
*i2c
,
382 enum mdio_i2c_proto protocol
)
387 if (!i2c_check_functionality(i2c
, I2C_FUNC_I2C
))
388 return ERR_PTR(-EINVAL
);
390 mii
= mdiobus_alloc();
392 return ERR_PTR(-ENOMEM
);
394 snprintf(mii
->id
, MII_BUS_ID_SIZE
, "i2c:%s", dev_name(parent
));
395 mii
->parent
= parent
;
399 case MDIO_I2C_ROLLBALL
:
400 ret
= i2c_mii_init_rollball(i2c
);
403 "Cannot initialize RollBall MDIO I2C protocol: %d\n",
409 mii
->read_c45
= i2c_mii_read_rollball
;
410 mii
->write_c45
= i2c_mii_write_rollball
;
413 mii
->read
= i2c_mii_read_default_c22
;
414 mii
->write
= i2c_mii_write_default_c22
;
415 mii
->read_c45
= i2c_mii_read_default_c45
;
416 mii
->write_c45
= i2c_mii_write_default_c45
;
422 EXPORT_SYMBOL_GPL(mdio_i2c_alloc
);
424 MODULE_AUTHOR("Russell King");
425 MODULE_DESCRIPTION("MDIO I2C bridge library");
426 MODULE_LICENSE("GPL v2");