Linux 6.13
[linux.git] / drivers / net / mdio / mdio-i2c.c
blobda2001ea1f9937dd5c4e633dd3d781ef7d5f1dbb
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * MDIO I2C bridge
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
11 * of their settings.
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)
30 return phy_id + 0x40;
33 static int i2c_mii_read_default_c45(struct mii_bus *bus, int phy_id, int devad,
34 int reg)
36 struct i2c_adapter *i2c = bus->priv;
37 struct i2c_msg msgs[2];
38 u8 addr[3], data[2], *p;
39 int bus_addr, ret;
41 if (!i2c_mii_valid_phy_id(phy_id))
42 return 0xffff;
44 p = addr;
45 if (devad >= 0) {
46 *p++ = 0x20 | devad;
47 *p++ = reg >> 8;
49 *p++ = reg;
51 bus_addr = i2c_mii_phy_addr(phy_id);
52 msgs[0].addr = bus_addr;
53 msgs[0].flags = 0;
54 msgs[0].len = p - addr;
55 msgs[0].buf = addr;
56 msgs[1].addr = bus_addr;
57 msgs[1].flags = I2C_M_RD;
58 msgs[1].len = sizeof(data);
59 msgs[1].buf = data;
61 ret = i2c_transfer(i2c, msgs, ARRAY_SIZE(msgs));
62 if (ret != ARRAY_SIZE(msgs))
63 return 0xffff;
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;
72 struct i2c_msg msg;
73 int ret;
74 u8 data[5], *p;
76 if (!i2c_mii_valid_phy_id(phy_id))
77 return 0;
79 p = data;
80 if (devad >= 0) {
81 *p++ = devad;
82 *p++ = reg >> 8;
84 *p++ = reg;
85 *p++ = val >> 8;
86 *p++ = val;
88 msg.addr = i2c_mii_phy_addr(phy_id);
89 msg.flags = 0;
90 msg.len = p - data;
91 msg.buf = data;
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,
104 u16 val)
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
111 * 0xffffffff.
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,
134 int num)
136 int ret;
138 ret = __i2c_transfer(i2c, msgs, num);
139 if (ret < 0)
140 return ret;
141 else if (ret != num)
142 return -EIO;
143 else
144 return 0;
147 static int __i2c_rollball_get_page(struct i2c_adapter *i2c, int bus_addr,
148 u8 *page)
150 struct i2c_msg msgs[2];
151 u8 addr = SFP_PAGE;
153 msgs[0].addr = bus_addr;
154 msgs[0].flags = 0;
155 msgs[0].len = 1;
156 msgs[0].buf = &addr;
158 msgs[1].addr = bus_addr;
159 msgs[1].flags = I2C_M_RD;
160 msgs[1].len = 1;
161 msgs[1].buf = page;
163 return __i2c_transfer_err(i2c, msgs, 2);
166 static int __i2c_rollball_set_page(struct i2c_adapter *i2c, int bus_addr,
167 u8 page)
169 struct i2c_msg msg;
170 u8 buf[2];
172 buf[0] = SFP_PAGE;
173 buf[1] = page;
175 msg.addr = bus_addr;
176 msg.flags = 0;
177 msg.len = 2;
178 msg.buf = buf;
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:
185 * 1. lock the bus
186 * 2. save content of SFP_PAGE
187 * 3. set SFP_PAGE to 3
188 * 4. do the transfer
189 * 5. restore original SFP_PAGE
190 * 6. unlock the bus
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;
200 u8 saved_page;
202 i2c_lock_bus(i2c, I2C_LOCK_SEGMENT);
204 /* save original page */
205 ret = __i2c_rollball_get_page(i2c, msgs->addr, &saved_page);
206 if (ret)
207 goto unlock;
209 /* change to RollBall MDIO page */
210 ret = __i2c_rollball_set_page(i2c, msgs->addr, SFP_PAGE_ROLLBALL_MDIO);
211 if (ret)
212 goto unlock;
214 /* do the transfer; we try to restore original page if this fails */
215 ret = __i2c_transfer_err(i2c, msgs, num);
216 if (ret)
217 main_err = ret;
219 /* restore original page */
220 ret = __i2c_rollball_set_page(i2c, msgs->addr, saved_page);
222 unlock:
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,
229 size_t len)
231 struct i2c_adapter *i2c = bus->priv;
232 struct i2c_msg msgs[2];
233 u8 cmd_addr, tmp, *res;
234 int i, ret;
236 cmd_addr = ROLLBALL_CMD_ADDR;
238 res = buf ? buf : &tmp;
239 len = buf ? len : 1;
241 msgs[0].addr = bus_addr;
242 msgs[0].flags = 0;
243 msgs[0].len = 1;
244 msgs[0].buf = &cmd_addr;
246 msgs[1].addr = bus_addr;
247 msgs[1].flags = I2C_M_RD;
248 msgs[1].len = len;
249 msgs[1].buf = res;
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.
254 i = 10;
255 do {
256 msleep(20);
258 ret = i2c_transfer_rollball(i2c, msgs, ARRAY_SIZE(msgs));
259 if (ret)
260 return ret;
262 if (*res == ROLLBALL_CMD_DONE)
263 return 0;
264 } while (i-- > 0);
266 dev_dbg(&bus->dev, "poll timed out\n");
268 return -ETIMEDOUT;
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];
276 u8 cmdbuf[2];
278 cmdbuf[0] = ROLLBALL_CMD_ADDR;
279 cmdbuf[1] = cmd;
281 msgs[0].addr = bus_addr;
282 msgs[0].flags = 0;
283 msgs[0].len = len;
284 msgs[0].buf = data;
286 msgs[1].addr = bus_addr;
287 msgs[1].flags = 0;
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,
295 int reg)
297 u8 buf[4], res[6];
298 int bus_addr, ret;
299 u16 val;
301 bus_addr = i2c_mii_phy_addr(phy_id);
302 if (bus_addr != ROLLBALL_PHY_I2C_ADDR)
303 return 0xffff;
305 buf[0] = ROLLBALL_DATA_ADDR;
306 buf[1] = devad;
307 buf[2] = (reg >> 8) & 0xff;
308 buf[3] = reg & 0xff;
310 ret = i2c_rollball_mii_cmd(bus, bus_addr, ROLLBALL_CMD_READ, buf,
311 sizeof(buf));
312 if (ret < 0)
313 return ret;
315 ret = i2c_rollball_mii_poll(bus, bus_addr, res, sizeof(res));
316 if (ret == -ETIMEDOUT)
317 return 0xffff;
318 else if (ret < 0)
319 return ret;
321 val = res[4] << 8 | res[5];
323 return val;
326 static int i2c_mii_write_rollball(struct mii_bus *bus, int phy_id, int devad,
327 int reg, u16 val)
329 int bus_addr, ret;
330 u8 buf[6];
332 bus_addr = i2c_mii_phy_addr(phy_id);
333 if (bus_addr != ROLLBALL_PHY_I2C_ADDR)
334 return 0;
336 buf[0] = ROLLBALL_DATA_ADDR;
337 buf[1] = devad;
338 buf[2] = (reg >> 8) & 0xff;
339 buf[3] = reg & 0xff;
340 buf[4] = val >> 8;
341 buf[5] = val & 0xff;
343 ret = i2c_rollball_mii_cmd(bus, bus_addr, ROLLBALL_CMD_WRITE, buf,
344 sizeof(buf));
345 if (ret < 0)
346 return ret;
348 ret = i2c_rollball_mii_poll(bus, bus_addr, NULL, 0);
349 if (ret < 0)
350 return ret;
352 return 0;
355 static int i2c_mii_init_rollball(struct i2c_adapter *i2c)
357 struct i2c_msg msg;
358 u8 pw[5];
359 int ret;
361 pw[0] = ROLLBALL_PASSWORD;
362 pw[1] = 0xff;
363 pw[2] = 0xff;
364 pw[3] = 0xff;
365 pw[4] = 0xff;
367 msg.addr = ROLLBALL_PHY_I2C_ADDR;
368 msg.flags = 0;
369 msg.len = sizeof(pw);
370 msg.buf = pw;
372 ret = i2c_transfer(i2c, &msg, 1);
373 if (ret < 0)
374 return ret;
375 else if (ret != 1)
376 return -EIO;
377 else
378 return 0;
381 struct mii_bus *mdio_i2c_alloc(struct device *parent, struct i2c_adapter *i2c,
382 enum mdio_i2c_proto protocol)
384 struct mii_bus *mii;
385 int ret;
387 if (!i2c_check_functionality(i2c, I2C_FUNC_I2C))
388 return ERR_PTR(-EINVAL);
390 mii = mdiobus_alloc();
391 if (!mii)
392 return ERR_PTR(-ENOMEM);
394 snprintf(mii->id, MII_BUS_ID_SIZE, "i2c:%s", dev_name(parent));
395 mii->parent = parent;
396 mii->priv = i2c;
398 switch (protocol) {
399 case MDIO_I2C_ROLLBALL:
400 ret = i2c_mii_init_rollball(i2c);
401 if (ret < 0) {
402 dev_err(parent,
403 "Cannot initialize RollBall MDIO I2C protocol: %d\n",
404 ret);
405 mdiobus_free(mii);
406 return ERR_PTR(ret);
409 mii->read_c45 = i2c_mii_read_rollball;
410 mii->write_c45 = i2c_mii_write_rollball;
411 break;
412 default:
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;
417 break;
420 return mii;
422 EXPORT_SYMBOL_GPL(mdio_i2c_alloc);
424 MODULE_AUTHOR("Russell King");
425 MODULE_DESCRIPTION("MDIO I2C bridge library");
426 MODULE_LICENSE("GPL v2");