xref: /linux/drivers/net/mdio/mdio-i2c.c (revision d90d90a1978af6530c7d8b201c4ab117d0506b1a)
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  */
23 static bool i2c_mii_valid_phy_id(int phy_id)
24 {
25 	return phy_id != 0x10 && phy_id != 0x11;
26 }
27 
28 static unsigned int i2c_mii_phy_addr(int phy_id)
29 {
30 	return phy_id + 0x40;
31 }
32 
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 
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 
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 
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 
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.
112  *
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
119  */
120 #define ROLLBALL_PHY_I2C_ADDR		0x51
121 
122 #define ROLLBALL_PASSWORD		(SFP_VSL + 3)
123 
124 #define ROLLBALL_CMD_ADDR		0x80
125 #define ROLLBALL_DATA_ADDR		0x81
126 
127 #define ROLLBALL_CMD_WRITE		0x01
128 #define ROLLBALL_CMD_READ		0x02
129 #define ROLLBALL_CMD_DONE		0x04
130 
131 #define SFP_PAGE_ROLLBALL_MDIO		3
132 
133 static int __i2c_transfer_err(struct i2c_adapter *i2c, struct i2c_msg *msgs,
134 			      int num)
135 {
136 	int ret;
137 
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;
145 }
146 
147 static int __i2c_rollball_get_page(struct i2c_adapter *i2c, int bus_addr,
148 				   u8 *page)
149 {
150 	struct i2c_msg msgs[2];
151 	u8 addr = SFP_PAGE;
152 
153 	msgs[0].addr = bus_addr;
154 	msgs[0].flags = 0;
155 	msgs[0].len = 1;
156 	msgs[0].buf = &addr;
157 
158 	msgs[1].addr = bus_addr;
159 	msgs[1].flags = I2C_M_RD;
160 	msgs[1].len = 1;
161 	msgs[1].buf = page;
162 
163 	return __i2c_transfer_err(i2c, msgs, 2);
164 }
165 
166 static int __i2c_rollball_set_page(struct i2c_adapter *i2c, int bus_addr,
167 				   u8 page)
168 {
169 	struct i2c_msg msg;
170 	u8 buf[2];
171 
172 	buf[0] = SFP_PAGE;
173 	buf[1] = page;
174 
175 	msg.addr = bus_addr;
176 	msg.flags = 0;
177 	msg.len = 2;
178 	msg.buf = buf;
179 
180 	return __i2c_transfer_err(i2c, &msg, 1);
181 }
182 
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.
195  */
196 static int i2c_transfer_rollball(struct i2c_adapter *i2c,
197 				 struct i2c_msg *msgs, int num)
198 {
199 	int ret, main_err = 0;
200 	u8 saved_page;
201 
202 	i2c_lock_bus(i2c, I2C_LOCK_SEGMENT);
203 
204 	/* save original page */
205 	ret = __i2c_rollball_get_page(i2c, msgs->addr, &saved_page);
206 	if (ret)
207 		goto unlock;
208 
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;
213 
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;
218 
219 	/* restore original page */
220 	ret = __i2c_rollball_set_page(i2c, msgs->addr, saved_page);
221 
222 unlock:
223 	i2c_unlock_bus(i2c, I2C_LOCK_SEGMENT);
224 
225 	return main_err ? : ret;
226 }
227 
228 static int i2c_rollball_mii_poll(struct mii_bus *bus, int bus_addr, u8 *buf,
229 				 size_t len)
230 {
231 	struct i2c_adapter *i2c = bus->priv;
232 	struct i2c_msg msgs[2];
233 	u8 cmd_addr, tmp, *res;
234 	int i, ret;
235 
236 	cmd_addr = ROLLBALL_CMD_ADDR;
237 
238 	res = buf ? buf : &tmp;
239 	len = buf ? len : 1;
240 
241 	msgs[0].addr = bus_addr;
242 	msgs[0].flags = 0;
243 	msgs[0].len = 1;
244 	msgs[0].buf = &cmd_addr;
245 
246 	msgs[1].addr = bus_addr;
247 	msgs[1].flags = I2C_M_RD;
248 	msgs[1].len = len;
249 	msgs[1].buf = res;
250 
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.
253 	 */
254 	i = 10;
255 	do {
256 		msleep(20);
257 
258 		ret = i2c_transfer_rollball(i2c, msgs, ARRAY_SIZE(msgs));
259 		if (ret)
260 			return ret;
261 
262 		if (*res == ROLLBALL_CMD_DONE)
263 			return 0;
264 	} while (i-- > 0);
265 
266 	dev_dbg(&bus->dev, "poll timed out\n");
267 
268 	return -ETIMEDOUT;
269 }
270 
271 static int i2c_rollball_mii_cmd(struct mii_bus *bus, int bus_addr, u8 cmd,
272 				u8 *data, size_t len)
273 {
274 	struct i2c_adapter *i2c = bus->priv;
275 	struct i2c_msg msgs[2];
276 	u8 cmdbuf[2];
277 
278 	cmdbuf[0] = ROLLBALL_CMD_ADDR;
279 	cmdbuf[1] = cmd;
280 
281 	msgs[0].addr = bus_addr;
282 	msgs[0].flags = 0;
283 	msgs[0].len = len;
284 	msgs[0].buf = data;
285 
286 	msgs[1].addr = bus_addr;
287 	msgs[1].flags = 0;
288 	msgs[1].len = sizeof(cmdbuf);
289 	msgs[1].buf = cmdbuf;
290 
291 	return i2c_transfer_rollball(i2c, msgs, ARRAY_SIZE(msgs));
292 }
293 
294 static int i2c_mii_read_rollball(struct mii_bus *bus, int phy_id, int reg)
295 {
296 	u8 buf[4], res[6];
297 	int bus_addr, ret;
298 	u16 val;
299 
300 	bus_addr = i2c_mii_phy_addr(phy_id);
301 	if (bus_addr != ROLLBALL_PHY_I2C_ADDR)
302 		return 0xffff;
303 
304 	buf[0] = ROLLBALL_DATA_ADDR;
305 	buf[1] = (reg >> 16) & 0x1f;
306 	buf[2] = (reg >> 8) & 0xff;
307 	buf[3] = reg & 0xff;
308 
309 	ret = i2c_rollball_mii_cmd(bus, bus_addr, ROLLBALL_CMD_READ, buf,
310 				   sizeof(buf));
311 	if (ret < 0)
312 		return ret;
313 
314 	ret = i2c_rollball_mii_poll(bus, bus_addr, res, sizeof(res));
315 	if (ret == -ETIMEDOUT)
316 		return 0xffff;
317 	else if (ret < 0)
318 		return ret;
319 
320 	val = res[4] << 8 | res[5];
321 
322 	return val;
323 }
324 
325 static int i2c_mii_write_rollball(struct mii_bus *bus, int phy_id, int reg,
326 				  u16 val)
327 {
328 	int bus_addr, ret;
329 	u8 buf[6];
330 
331 	bus_addr = i2c_mii_phy_addr(phy_id);
332 	if (bus_addr != ROLLBALL_PHY_I2C_ADDR)
333 		return 0;
334 
335 	buf[0] = ROLLBALL_DATA_ADDR;
336 	buf[1] = (reg >> 16) & 0x1f;
337 	buf[2] = (reg >> 8) & 0xff;
338 	buf[3] = reg & 0xff;
339 	buf[4] = val >> 8;
340 	buf[5] = val & 0xff;
341 
342 	ret = i2c_rollball_mii_cmd(bus, bus_addr, ROLLBALL_CMD_WRITE, buf,
343 				   sizeof(buf));
344 	if (ret < 0)
345 		return ret;
346 
347 	ret = i2c_rollball_mii_poll(bus, bus_addr, NULL, 0);
348 	if (ret < 0)
349 		return ret;
350 
351 	return 0;
352 }
353 
354 static int i2c_mii_init_rollball(struct i2c_adapter *i2c)
355 {
356 	struct i2c_msg msg;
357 	u8 pw[5];
358 	int ret;
359 
360 	pw[0] = ROLLBALL_PASSWORD;
361 	pw[1] = 0xff;
362 	pw[2] = 0xff;
363 	pw[3] = 0xff;
364 	pw[4] = 0xff;
365 
366 	msg.addr = ROLLBALL_PHY_I2C_ADDR;
367 	msg.flags = 0;
368 	msg.len = sizeof(pw);
369 	msg.buf = pw;
370 
371 	ret = i2c_transfer(i2c, &msg, 1);
372 	if (ret < 0)
373 		return ret;
374 	else if (ret != 1)
375 		return -EIO;
376 	else
377 		return 0;
378 }
379 
380 struct mii_bus *mdio_i2c_alloc(struct device *parent, struct i2c_adapter *i2c,
381 			       enum mdio_i2c_proto protocol)
382 {
383 	struct mii_bus *mii;
384 	int ret;
385 
386 	if (!i2c_check_functionality(i2c, I2C_FUNC_I2C))
387 		return ERR_PTR(-EINVAL);
388 
389 	mii = mdiobus_alloc();
390 	if (!mii)
391 		return ERR_PTR(-ENOMEM);
392 
393 	snprintf(mii->id, MII_BUS_ID_SIZE, "i2c:%s", dev_name(parent));
394 	mii->parent = parent;
395 	mii->priv = i2c;
396 
397 	switch (protocol) {
398 	case MDIO_I2C_ROLLBALL:
399 		ret = i2c_mii_init_rollball(i2c);
400 		if (ret < 0) {
401 			dev_err(parent,
402 				"Cannot initialize RollBall MDIO I2C protocol: %d\n",
403 				ret);
404 			mdiobus_free(mii);
405 			return ERR_PTR(ret);
406 		}
407 
408 		mii->read = i2c_mii_read_rollball;
409 		mii->write = i2c_mii_write_rollball;
410 		break;
411 	default:
412 		mii->read = i2c_mii_read_default_c22;
413 		mii->write = i2c_mii_write_default_c22;
414 		mii->read_c45 = i2c_mii_read_default_c45;
415 		mii->write_c45 = i2c_mii_write_default_c45;
416 		break;
417 	}
418 
419 	return mii;
420 }
421 EXPORT_SYMBOL_GPL(mdio_i2c_alloc);
422 
423 MODULE_AUTHOR("Russell King");
424 MODULE_DESCRIPTION("MDIO I2C bridge library");
425 MODULE_LICENSE("GPL v2");
426