xref: /linux/drivers/net/mdio/mdio-i2c.c (revision 1a9239bb4253f9076b5b4b2a1a4e8d7defd77a95)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * MDIO I2C bridge
4  *
5  * Copyright (C) 2015-2016 Russell King
6  * Copyright (C) 2021 Marek Behun
7  *
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.
12  */
13 #include <linux/i2c.h>
14 #include <linux/mdio/mdio-i2c.h>
15 #include <linux/phy.h>
16 #include <linux/sfp.h>
17 
18 /*
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.
22  */
i2c_mii_valid_phy_id(int phy_id)23 static bool i2c_mii_valid_phy_id(int phy_id)
24 {
25 	return phy_id != 0x10 && phy_id != 0x11;
26 }
27 
i2c_mii_phy_addr(int phy_id)28 static unsigned int i2c_mii_phy_addr(int phy_id)
29 {
30 	return phy_id + 0x40;
31 }
32 
i2c_mii_read_default_c45(struct mii_bus * bus,int phy_id,int devad,int reg)33 static int i2c_mii_read_default_c45(struct mii_bus *bus, int phy_id, int devad,
34 				    int reg)
35 {
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;
40 
41 	if (!i2c_mii_valid_phy_id(phy_id))
42 		return 0xffff;
43 
44 	p = addr;
45 	if (devad >= 0) {
46 		*p++ = 0x20 | devad;
47 		*p++ = reg >> 8;
48 	}
49 	*p++ = reg;
50 
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;
60 
61 	ret = i2c_transfer(i2c, msgs, ARRAY_SIZE(msgs));
62 	if (ret != ARRAY_SIZE(msgs))
63 		return 0xffff;
64 
65 	return data[0] << 8 | data[1];
66 }
67 
i2c_mii_write_default_c45(struct mii_bus * bus,int phy_id,int devad,int reg,u16 val)68 static int i2c_mii_write_default_c45(struct mii_bus *bus, int phy_id,
69 				     int devad, 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 (devad >= 0) {
81 		*p++ = devad;
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 
i2c_mii_read_default_c22(struct mii_bus * bus,int phy_id,int reg)98 static int i2c_mii_read_default_c22(struct mii_bus *bus, int phy_id, int reg)
99 {
100 	return i2c_mii_read_default_c45(bus, phy_id, -1, reg);
101 }
102 
i2c_mii_write_default_c22(struct mii_bus * bus,int phy_id,int reg,u16 val)103 static int i2c_mii_write_default_c22(struct mii_bus *bus, int phy_id, int reg,
104 				     u16 val)
105 {
106 	return i2c_mii_write_default_c45(bus, phy_id, -1, reg, val);
107 }
108 
smbus_byte_mii_read_default_c22(struct mii_bus * bus,int phy_id,int reg)109 static int smbus_byte_mii_read_default_c22(struct mii_bus *bus, int phy_id,
110 					   int reg)
111 {
112 	struct i2c_adapter *i2c = bus->priv;
113 	union i2c_smbus_data smbus_data;
114 	int val = 0, ret;
115 
116 	if (!i2c_mii_valid_phy_id(phy_id))
117 		return 0;
118 
119 	ret = i2c_smbus_xfer(i2c, i2c_mii_phy_addr(phy_id), 0,
120 			     I2C_SMBUS_READ, reg,
121 			     I2C_SMBUS_BYTE_DATA, &smbus_data);
122 	if (ret < 0)
123 		return ret;
124 
125 	val = (smbus_data.byte & 0xff) << 8;
126 
127 	ret = i2c_smbus_xfer(i2c, i2c_mii_phy_addr(phy_id), 0,
128 			     I2C_SMBUS_READ, reg,
129 			     I2C_SMBUS_BYTE_DATA, &smbus_data);
130 	if (ret < 0)
131 		return ret;
132 
133 	val |= smbus_data.byte & 0xff;
134 
135 	return val;
136 }
137 
smbus_byte_mii_write_default_c22(struct mii_bus * bus,int phy_id,int reg,u16 val)138 static int smbus_byte_mii_write_default_c22(struct mii_bus *bus, int phy_id,
139 					    int reg, u16 val)
140 {
141 	struct i2c_adapter *i2c = bus->priv;
142 	union i2c_smbus_data smbus_data;
143 	int ret;
144 
145 	if (!i2c_mii_valid_phy_id(phy_id))
146 		return 0;
147 
148 	smbus_data.byte = (val & 0xff00) >> 8;
149 
150 	ret = i2c_smbus_xfer(i2c, i2c_mii_phy_addr(phy_id), 0,
151 			     I2C_SMBUS_WRITE, reg,
152 			     I2C_SMBUS_BYTE_DATA, &smbus_data);
153 	if (ret < 0)
154 		return ret;
155 
156 	smbus_data.byte = val & 0xff;
157 
158 	ret = i2c_smbus_xfer(i2c, i2c_mii_phy_addr(phy_id), 0,
159 			     I2C_SMBUS_WRITE, reg,
160 			     I2C_SMBUS_BYTE_DATA, &smbus_data);
161 
162 	return ret < 0 ? ret : 0;
163 }
164 
165 /* RollBall SFPs do not access internal PHY via I2C address 0x56, but
166  * instead via address 0x51, when SFP page is set to 0x03 and password to
167  * 0xffffffff.
168  *
169  * address  size  contents  description
170  * -------  ----  --------  -----------
171  * 0x80     1     CMD       0x01/0x02/0x04 for write/read/done
172  * 0x81     1     DEV       Clause 45 device
173  * 0x82     2     REG       Clause 45 register
174  * 0x84     2     VAL       Register value
175  */
176 #define ROLLBALL_PHY_I2C_ADDR		0x51
177 
178 #define ROLLBALL_PASSWORD		(SFP_VSL + 3)
179 
180 #define ROLLBALL_CMD_ADDR		0x80
181 #define ROLLBALL_DATA_ADDR		0x81
182 
183 #define ROLLBALL_CMD_WRITE		0x01
184 #define ROLLBALL_CMD_READ		0x02
185 #define ROLLBALL_CMD_DONE		0x04
186 
187 #define SFP_PAGE_ROLLBALL_MDIO		3
188 
__i2c_transfer_err(struct i2c_adapter * i2c,struct i2c_msg * msgs,int num)189 static int __i2c_transfer_err(struct i2c_adapter *i2c, struct i2c_msg *msgs,
190 			      int num)
191 {
192 	int ret;
193 
194 	ret = __i2c_transfer(i2c, msgs, num);
195 	if (ret < 0)
196 		return ret;
197 	else if (ret != num)
198 		return -EIO;
199 	else
200 		return 0;
201 }
202 
__i2c_rollball_get_page(struct i2c_adapter * i2c,int bus_addr,u8 * page)203 static int __i2c_rollball_get_page(struct i2c_adapter *i2c, int bus_addr,
204 				   u8 *page)
205 {
206 	struct i2c_msg msgs[2];
207 	u8 addr = SFP_PAGE;
208 
209 	msgs[0].addr = bus_addr;
210 	msgs[0].flags = 0;
211 	msgs[0].len = 1;
212 	msgs[0].buf = &addr;
213 
214 	msgs[1].addr = bus_addr;
215 	msgs[1].flags = I2C_M_RD;
216 	msgs[1].len = 1;
217 	msgs[1].buf = page;
218 
219 	return __i2c_transfer_err(i2c, msgs, 2);
220 }
221 
__i2c_rollball_set_page(struct i2c_adapter * i2c,int bus_addr,u8 page)222 static int __i2c_rollball_set_page(struct i2c_adapter *i2c, int bus_addr,
223 				   u8 page)
224 {
225 	struct i2c_msg msg;
226 	u8 buf[2];
227 
228 	buf[0] = SFP_PAGE;
229 	buf[1] = page;
230 
231 	msg.addr = bus_addr;
232 	msg.flags = 0;
233 	msg.len = 2;
234 	msg.buf = buf;
235 
236 	return __i2c_transfer_err(i2c, &msg, 1);
237 }
238 
239 /* In order to not interfere with other SFP code (which possibly may manipulate
240  * SFP_PAGE), for every transfer we do this:
241  *   1. lock the bus
242  *   2. save content of SFP_PAGE
243  *   3. set SFP_PAGE to 3
244  *   4. do the transfer
245  *   5. restore original SFP_PAGE
246  *   6. unlock the bus
247  * Note that one might think that steps 2 to 5 could be theoretically done all
248  * in one call to i2c_transfer (by constructing msgs array in such a way), but
249  * unfortunately tests show that this does not work :-( Changed SFP_PAGE does
250  * not take into account until i2c_transfer() is done.
251  */
i2c_transfer_rollball(struct i2c_adapter * i2c,struct i2c_msg * msgs,int num)252 static int i2c_transfer_rollball(struct i2c_adapter *i2c,
253 				 struct i2c_msg *msgs, int num)
254 {
255 	int ret, main_err = 0;
256 	u8 saved_page;
257 
258 	i2c_lock_bus(i2c, I2C_LOCK_SEGMENT);
259 
260 	/* save original page */
261 	ret = __i2c_rollball_get_page(i2c, msgs->addr, &saved_page);
262 	if (ret)
263 		goto unlock;
264 
265 	/* change to RollBall MDIO page */
266 	ret = __i2c_rollball_set_page(i2c, msgs->addr, SFP_PAGE_ROLLBALL_MDIO);
267 	if (ret)
268 		goto unlock;
269 
270 	/* do the transfer; we try to restore original page if this fails */
271 	ret = __i2c_transfer_err(i2c, msgs, num);
272 	if (ret)
273 		main_err = ret;
274 
275 	/* restore original page */
276 	ret = __i2c_rollball_set_page(i2c, msgs->addr, saved_page);
277 
278 unlock:
279 	i2c_unlock_bus(i2c, I2C_LOCK_SEGMENT);
280 
281 	return main_err ? : ret;
282 }
283 
i2c_rollball_mii_poll(struct mii_bus * bus,int bus_addr,u8 * buf,size_t len)284 static int i2c_rollball_mii_poll(struct mii_bus *bus, int bus_addr, u8 *buf,
285 				 size_t len)
286 {
287 	struct i2c_adapter *i2c = bus->priv;
288 	struct i2c_msg msgs[2];
289 	u8 cmd_addr, tmp, *res;
290 	int i, ret;
291 
292 	cmd_addr = ROLLBALL_CMD_ADDR;
293 
294 	res = buf ? buf : &tmp;
295 	len = buf ? len : 1;
296 
297 	msgs[0].addr = bus_addr;
298 	msgs[0].flags = 0;
299 	msgs[0].len = 1;
300 	msgs[0].buf = &cmd_addr;
301 
302 	msgs[1].addr = bus_addr;
303 	msgs[1].flags = I2C_M_RD;
304 	msgs[1].len = len;
305 	msgs[1].buf = res;
306 
307 	/* By experiment it takes up to 70 ms to access a register for these
308 	 * SFPs. Sleep 20ms between iterations and try 10 times.
309 	 */
310 	i = 10;
311 	do {
312 		msleep(20);
313 
314 		ret = i2c_transfer_rollball(i2c, msgs, ARRAY_SIZE(msgs));
315 		if (ret)
316 			return ret;
317 
318 		if (*res == ROLLBALL_CMD_DONE)
319 			return 0;
320 	} while (i-- > 0);
321 
322 	dev_dbg(&bus->dev, "poll timed out\n");
323 
324 	return -ETIMEDOUT;
325 }
326 
i2c_rollball_mii_cmd(struct mii_bus * bus,int bus_addr,u8 cmd,u8 * data,size_t len)327 static int i2c_rollball_mii_cmd(struct mii_bus *bus, int bus_addr, u8 cmd,
328 				u8 *data, size_t len)
329 {
330 	struct i2c_adapter *i2c = bus->priv;
331 	struct i2c_msg msgs[2];
332 	u8 cmdbuf[2];
333 
334 	cmdbuf[0] = ROLLBALL_CMD_ADDR;
335 	cmdbuf[1] = cmd;
336 
337 	msgs[0].addr = bus_addr;
338 	msgs[0].flags = 0;
339 	msgs[0].len = len;
340 	msgs[0].buf = data;
341 
342 	msgs[1].addr = bus_addr;
343 	msgs[1].flags = 0;
344 	msgs[1].len = sizeof(cmdbuf);
345 	msgs[1].buf = cmdbuf;
346 
347 	return i2c_transfer_rollball(i2c, msgs, ARRAY_SIZE(msgs));
348 }
349 
i2c_mii_read_rollball(struct mii_bus * bus,int phy_id,int devad,int reg)350 static int i2c_mii_read_rollball(struct mii_bus *bus, int phy_id, int devad,
351 				 int reg)
352 {
353 	u8 buf[4], res[6];
354 	int bus_addr, ret;
355 	u16 val;
356 
357 	bus_addr = i2c_mii_phy_addr(phy_id);
358 	if (bus_addr != ROLLBALL_PHY_I2C_ADDR)
359 		return 0xffff;
360 
361 	buf[0] = ROLLBALL_DATA_ADDR;
362 	buf[1] = devad;
363 	buf[2] = (reg >> 8) & 0xff;
364 	buf[3] = reg & 0xff;
365 
366 	ret = i2c_rollball_mii_cmd(bus, bus_addr, ROLLBALL_CMD_READ, buf,
367 				   sizeof(buf));
368 	if (ret < 0)
369 		return ret;
370 
371 	ret = i2c_rollball_mii_poll(bus, bus_addr, res, sizeof(res));
372 	if (ret == -ETIMEDOUT)
373 		return 0xffff;
374 	else if (ret < 0)
375 		return ret;
376 
377 	val = res[4] << 8 | res[5];
378 
379 	return val;
380 }
381 
i2c_mii_write_rollball(struct mii_bus * bus,int phy_id,int devad,int reg,u16 val)382 static int i2c_mii_write_rollball(struct mii_bus *bus, int phy_id, int devad,
383 				  int reg, u16 val)
384 {
385 	int bus_addr, ret;
386 	u8 buf[6];
387 
388 	bus_addr = i2c_mii_phy_addr(phy_id);
389 	if (bus_addr != ROLLBALL_PHY_I2C_ADDR)
390 		return 0;
391 
392 	buf[0] = ROLLBALL_DATA_ADDR;
393 	buf[1] = devad;
394 	buf[2] = (reg >> 8) & 0xff;
395 	buf[3] = reg & 0xff;
396 	buf[4] = val >> 8;
397 	buf[5] = val & 0xff;
398 
399 	ret = i2c_rollball_mii_cmd(bus, bus_addr, ROLLBALL_CMD_WRITE, buf,
400 				   sizeof(buf));
401 	if (ret < 0)
402 		return ret;
403 
404 	ret = i2c_rollball_mii_poll(bus, bus_addr, NULL, 0);
405 	if (ret < 0)
406 		return ret;
407 
408 	return 0;
409 }
410 
i2c_mii_init_rollball(struct i2c_adapter * i2c)411 static int i2c_mii_init_rollball(struct i2c_adapter *i2c)
412 {
413 	struct i2c_msg msg;
414 	u8 pw[5];
415 	int ret;
416 
417 	pw[0] = ROLLBALL_PASSWORD;
418 	pw[1] = 0xff;
419 	pw[2] = 0xff;
420 	pw[3] = 0xff;
421 	pw[4] = 0xff;
422 
423 	msg.addr = ROLLBALL_PHY_I2C_ADDR;
424 	msg.flags = 0;
425 	msg.len = sizeof(pw);
426 	msg.buf = pw;
427 
428 	ret = i2c_transfer(i2c, &msg, 1);
429 	if (ret < 0)
430 		return ret;
431 	else if (ret != 1)
432 		return -EIO;
433 	else
434 		return 0;
435 }
436 
mdio_i2c_check_functionality(struct i2c_adapter * i2c,enum mdio_i2c_proto protocol)437 static bool mdio_i2c_check_functionality(struct i2c_adapter *i2c,
438 					 enum mdio_i2c_proto protocol)
439 {
440 	if (i2c_check_functionality(i2c, I2C_FUNC_I2C))
441 		return true;
442 
443 	if (i2c_check_functionality(i2c, I2C_FUNC_SMBUS_BYTE_DATA) &&
444 	    protocol == MDIO_I2C_MARVELL_C22)
445 		return true;
446 
447 	return false;
448 }
449 
mdio_i2c_alloc(struct device * parent,struct i2c_adapter * i2c,enum mdio_i2c_proto protocol)450 struct mii_bus *mdio_i2c_alloc(struct device *parent, struct i2c_adapter *i2c,
451 			       enum mdio_i2c_proto protocol)
452 {
453 	struct mii_bus *mii;
454 	int ret;
455 
456 	if (!mdio_i2c_check_functionality(i2c, protocol))
457 		return ERR_PTR(-EINVAL);
458 
459 	mii = mdiobus_alloc();
460 	if (!mii)
461 		return ERR_PTR(-ENOMEM);
462 
463 	snprintf(mii->id, MII_BUS_ID_SIZE, "i2c:%s", dev_name(parent));
464 	mii->parent = parent;
465 	mii->priv = i2c;
466 
467 	/* Only use SMBus if we have no other choice */
468 	if (i2c_check_functionality(i2c, I2C_FUNC_SMBUS_BYTE_DATA) &&
469 	    !i2c_check_functionality(i2c, I2C_FUNC_I2C)) {
470 		mii->read = smbus_byte_mii_read_default_c22;
471 		mii->write = smbus_byte_mii_write_default_c22;
472 		return mii;
473 	}
474 
475 	switch (protocol) {
476 	case MDIO_I2C_ROLLBALL:
477 		ret = i2c_mii_init_rollball(i2c);
478 		if (ret < 0) {
479 			dev_err(parent,
480 				"Cannot initialize RollBall MDIO I2C protocol: %d\n",
481 				ret);
482 			mdiobus_free(mii);
483 			return ERR_PTR(ret);
484 		}
485 
486 		mii->read_c45 = i2c_mii_read_rollball;
487 		mii->write_c45 = i2c_mii_write_rollball;
488 		break;
489 	default:
490 		mii->read = i2c_mii_read_default_c22;
491 		mii->write = i2c_mii_write_default_c22;
492 		mii->read_c45 = i2c_mii_read_default_c45;
493 		mii->write_c45 = i2c_mii_write_default_c45;
494 		break;
495 	}
496 
497 	return mii;
498 }
499 EXPORT_SYMBOL_GPL(mdio_i2c_alloc);
500 
501 MODULE_AUTHOR("Russell King");
502 MODULE_DESCRIPTION("MDIO I2C bridge library");
503 MODULE_LICENSE("GPL v2");
504