xref: /openbmc/linux/drivers/net/mdio/mdio-i2c.c (revision b48a1864)
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 
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 
__i2c_transfer_err(struct i2c_adapter * i2c,struct i2c_msg * msgs,int num)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 
__i2c_rollball_get_page(struct i2c_adapter * i2c,int bus_addr,u8 * page)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 
__i2c_rollball_set_page(struct i2c_adapter * i2c,int bus_addr,u8 page)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  */
i2c_transfer_rollball(struct i2c_adapter * i2c,struct i2c_msg * msgs,int num)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 
i2c_rollball_mii_poll(struct mii_bus * bus,int bus_addr,u8 * buf,size_t len)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 
i2c_rollball_mii_cmd(struct mii_bus * bus,int bus_addr,u8 cmd,u8 * data,size_t len)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 
i2c_mii_read_rollball(struct mii_bus * bus,int phy_id,int devad,int reg)294 static int i2c_mii_read_rollball(struct mii_bus *bus, int phy_id, int devad,
295 				 int reg)
296 {
297 	u8 buf[4], res[6];
298 	int bus_addr, ret;
299 	u16 val;
300 
301 	bus_addr = i2c_mii_phy_addr(phy_id);
302 	if (bus_addr != ROLLBALL_PHY_I2C_ADDR)
303 		return 0xffff;
304 
305 	buf[0] = ROLLBALL_DATA_ADDR;
306 	buf[1] = devad;
307 	buf[2] = (reg >> 8) & 0xff;
308 	buf[3] = reg & 0xff;
309 
310 	ret = i2c_rollball_mii_cmd(bus, bus_addr, ROLLBALL_CMD_READ, buf,
311 				   sizeof(buf));
312 	if (ret < 0)
313 		return ret;
314 
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;
320 
321 	val = res[4] << 8 | res[5];
322 
323 	return val;
324 }
325 
i2c_mii_write_rollball(struct mii_bus * bus,int phy_id,int devad,int reg,u16 val)326 static int i2c_mii_write_rollball(struct mii_bus *bus, int phy_id, int devad,
327 				  int reg, u16 val)
328 {
329 	int bus_addr, ret;
330 	u8 buf[6];
331 
332 	bus_addr = i2c_mii_phy_addr(phy_id);
333 	if (bus_addr != ROLLBALL_PHY_I2C_ADDR)
334 		return 0;
335 
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;
342 
343 	ret = i2c_rollball_mii_cmd(bus, bus_addr, ROLLBALL_CMD_WRITE, buf,
344 				   sizeof(buf));
345 	if (ret < 0)
346 		return ret;
347 
348 	ret = i2c_rollball_mii_poll(bus, bus_addr, NULL, 0);
349 	if (ret < 0)
350 		return ret;
351 
352 	return 0;
353 }
354 
i2c_mii_init_rollball(struct i2c_adapter * i2c)355 static int i2c_mii_init_rollball(struct i2c_adapter *i2c)
356 {
357 	struct i2c_msg msg;
358 	u8 pw[5];
359 	int ret;
360 
361 	pw[0] = ROLLBALL_PASSWORD;
362 	pw[1] = 0xff;
363 	pw[2] = 0xff;
364 	pw[3] = 0xff;
365 	pw[4] = 0xff;
366 
367 	msg.addr = ROLLBALL_PHY_I2C_ADDR;
368 	msg.flags = 0;
369 	msg.len = sizeof(pw);
370 	msg.buf = pw;
371 
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;
379 }
380 
mdio_i2c_alloc(struct device * parent,struct i2c_adapter * i2c,enum mdio_i2c_proto protocol)381 struct mii_bus *mdio_i2c_alloc(struct device *parent, struct i2c_adapter *i2c,
382 			       enum mdio_i2c_proto protocol)
383 {
384 	struct mii_bus *mii;
385 	int ret;
386 
387 	if (!i2c_check_functionality(i2c, I2C_FUNC_I2C))
388 		return ERR_PTR(-EINVAL);
389 
390 	mii = mdiobus_alloc();
391 	if (!mii)
392 		return ERR_PTR(-ENOMEM);
393 
394 	snprintf(mii->id, MII_BUS_ID_SIZE, "i2c:%s", dev_name(parent));
395 	mii->parent = parent;
396 	mii->priv = i2c;
397 
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);
407 		}
408 
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;
418 	}
419 
420 	return mii;
421 }
422 EXPORT_SYMBOL_GPL(mdio_i2c_alloc);
423 
424 MODULE_AUTHOR("Russell King");
425 MODULE_DESCRIPTION("MDIO I2C bridge library");
426 MODULE_LICENSE("GPL v2");
427