imx6: move to 4.4 kernel
[openwrt/openwrt.git] / target / linux / imx6 / files-4.3 / drivers / net / phy / gw16083.c
1 /*
2 * drivers/net/phy/gw16083.c
3 *
4 * Driver for GW16083 Ventana Ethernet Expansion Mezzanine
5 *
6 * Author: Tim Harvey
7 *
8 * Copyright (c) 2014 Tim Harvey <tharvey@gateworks.com>
9 *
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the
12 * Free Software Foundation; either version 2 of the License, or (at your
13 * option) any later version.
14 *
15 */
16
17 /*
18 * The GW16083 interfaces with a Ventana baseboard via the PCIe bus, an i2c
19 * bus (i2c2), and a couple of GPIO's. On the PCIe bus is an i210 GigE with
20 * its MAC connected to Port4 of a Marvell MV88E6176 7-port GigE switch via
21 * MDIO and RGMII. Ports 0-3 are standard copper RJ45 but Ports 5 and 6
22 * connect to Marvell MV88E1111 dual-mode Copper/Fiber PHY's over SGMII and
23 * MDIO. The PHY's have both an RG45 for copper and an SFP module.
24 */
25
26 #include <linux/kernel.h>
27 #include <linux/string.h>
28 #include <linux/errno.h>
29 #include <linux/unistd.h>
30 #include <linux/i2c.h>
31 #include <linux/interrupt.h>
32 #include <linux/init.h>
33 #include <linux/delay.h>
34 #include <linux/device.h>
35 #include <linux/netdevice.h>
36 #include <linux/etherdevice.h>
37 #include <linux/skbuff.h>
38 #include <linux/spinlock.h>
39 #include <linux/mm.h>
40 #include <linux/module.h>
41 #include <linux/mii.h>
42 #include <linux/ethtool.h>
43 #include <linux/phy.h>
44 #include <linux/marvell_phy.h>
45 #include <linux/of_platform.h>
46 #include <linux/platform_device.h>
47
48 #include <linux/io.h>
49 #include <asm/irq.h>
50 #include <net/dsa.h>
51 #include <linux/uaccess.h>
52
53 #include "gw16083.h"
54
55 #undef FAIL_ON_CHECKSUM_ERR /* fail to configure SFP if checksum bad */
56 #define PORT_POWER_CONTROL /* ports can be enabled/disabled via sysfs */
57 #define PORT_MODE_CONTROL /* ports 5/6 can have SFP/RJ45 mode forced */
58 #define RGMII_DELAY_ON_PHY /* implement Port5/6 tx/rx delay on PHY vs sw */
59
60 MODULE_DESCRIPTION("GW16083 driver");
61 MODULE_AUTHOR("Tim Harvey");
62 MODULE_LICENSE("GPL");
63
64 struct mv88e1111_port_state {
65 int port;
66 bool present;
67 bool serdes;
68 bool sfp_signal;
69 bool sfp_present;
70 bool sfp_compat;
71 bool sfp_enabled;
72 char sfp_id[64];
73 };
74
75 struct mv88e1111_priv {
76 struct phy_device *phydev;
77 struct i2c_client *client;
78 struct mv88e1111_port_state port5;
79 struct mv88e1111_port_state port6;
80 struct kobject *sysfs_kobj;
81 struct delayed_work work;
82 struct workqueue_struct *workq;
83 };
84
85 enum {
86 mode_copper = 0,
87 mode_serdes = 1,
88 };
89
90 #if IS_ENABLED(CONFIG_NET_DSA_MV88E6352)
91 static struct dsa_chip_data switch_chip_data = {
92 .port_names = {
93 "lan4",
94 "lan3",
95 "lan2",
96 "lan1",
97 "cpu",
98 "lan5",
99 "lan6",
100 },
101 };
102
103 static struct dsa_platform_data switch_plat_data = {
104 .nr_chips = 1,
105 .chip = &switch_chip_data,
106 };
107
108 static struct platform_device switch_device = {
109 .name = "dsa",
110 .id = 0,
111 .num_resources = 0,
112 .dev.platform_data = &switch_plat_data,
113 };
114 #endif
115
116 static struct i2c_client *gw16083_client = NULL;
117
118 static int gw16083_read_port_sfp(struct i2c_client *client,
119 struct mv88e1111_port_state *state);
120
121 /* read switch port register from port0-6 */
122 u16 read_switch_port(struct phy_device *pdev, int port, u8 regaddr)
123 {
124 return pdev->bus->read(pdev->bus, MV_BASE + port, regaddr);
125 }
126
127 /* write switch port register to port0-6 */
128 int write_switch_port(struct phy_device *pdev, int port, u8 regaddr, u16 val)
129 {
130 return pdev->bus->write(pdev->bus, MV_BASE + port, regaddr, val);
131 }
132
133 /*
134 * read_switch_port_phy - write a register for a specific port on 88E6176
135 * The 88E6176 PHY registers must be accessed thorugh the Global2 address
136 * using the SMI_PHY_COMMAND_REG and SMI_PHY_DATA_REG.
137 */
138 int read_switch_port_phy(struct phy_device *pdev, int port, u8 regaddr)
139 {
140 struct mv88e1111_priv *priv = dev_get_drvdata(&pdev->dev);
141 u16 reg;
142 int i;
143
144 dev_dbg(&priv->client->dev, "read_phy: port%d reg=0x%02x\n", port,
145 regaddr);
146 reg = SMIBUSY | SMIMODE22 | SMIOP_READ;
147 reg |= port << DEVADDR;
148 reg |= regaddr << REGADDR;
149 pdev->bus->write(pdev->bus, MV_GLOBAL2, MV_SMI_PHY_COMMAND, reg);
150 for (i = 0; i < 10; i++) {
151 reg = pdev->bus->read(pdev->bus, MV_GLOBAL2,
152 MV_SMI_PHY_COMMAND);
153 if (!(reg & (1<<15)))
154 break;
155 mdelay(1);
156 }
157 /* timeout */
158 if (i == 10)
159 return 0xffff;
160 reg = pdev->bus->read(pdev->bus, MV_GLOBAL2, MV_SMI_PHY_DATA);
161 return reg;
162 }
163
164 /*
165 * write_switch_port_phy - write a register for a specific port on 88E6176
166 * The 88E6176 PHY registers must be accessed thorugh the Global2 address
167 * using the SMI_PHY_COMMAND_REG and SMI_PHY_DATA_REG.
168 */
169 int write_switch_port_phy(struct phy_device *pdev, int port, u8 addr, u16 reg)
170 {
171 struct mv88e1111_priv *priv = dev_get_drvdata(&pdev->dev);
172 int i;
173
174 dev_dbg(&priv->client->dev, "write_phy: port%d reg=0x%02x val=0x%04x\n",
175 port, addr, reg);
176 pdev->bus->write(pdev->bus, MV_GLOBAL2, MV_SMI_PHY_DATA, reg);
177 reg = SMIBUSY | SMIMODE22 | SMIOP_WRITE;
178 reg |= port << DEVADDR;
179 reg |= addr << REGADDR;
180 pdev->bus->write(pdev->bus, MV_GLOBAL2, MV_SMI_PHY_COMMAND, reg);
181 for (i = 0; i < 10; i++) {
182 reg = pdev->bus->read(pdev->bus, MV_GLOBAL2,
183 MV_SMI_PHY_COMMAND);
184 if (!(reg & (1<<15)))
185 break;
186 mdelay(1);
187 }
188 /* timeout */
189 if (i == 10)
190 return -ETIMEDOUT;
191
192 return 0;
193 }
194
195 /* read a scratch register from switch */
196 inline u8 read_switch_scratch(struct phy_device *pdev, u8 reg)
197 {
198 pdev->bus->write(pdev->bus, MV_GLOBAL2, MV_SCRATCH_MISC, (reg << 8));
199 return pdev->bus->read(pdev->bus, MV_GLOBAL2, MV_SCRATCH_MISC) & 0xff;
200 }
201
202 /* write a scratch register to switch */
203 inline void write_switch_scratch(struct phy_device *pdev, u8 reg, u8 val)
204 {
205 pdev->bus->write(pdev->bus, MV_GLOBAL2, MV_SCRATCH_MISC,
206 (1 << 15) | (reg << 8) | val);
207 }
208
209 /* enable or disable an SFP's TXEN signal */
210 static int enable_sfp_txen(struct phy_device *pdev, int port, bool enable)
211 {
212 struct mv88e1111_priv *priv = dev_get_drvdata(&pdev->dev);
213 u8 gpio;
214 int bit;
215
216 if (port != 5 && port != 6)
217 return -EINVAL;
218
219 /* GPIO[2:1] output low to enable TXEN */
220 bit = (port == 5) ? 1 : 2;
221 gpio = read_switch_scratch(pdev, MV_GPIO_DATA);
222 if (enable)
223 gpio |= (1 << bit);
224 else
225 gpio &= (1 << bit);
226 write_switch_scratch(pdev, MV_GPIO_DATA, gpio);
227 dev_info(&priv->client->dev, "Port%d: SFP TX %s\n", port, enable ?
228 "enabled" : "disabled");
229 return 0;
230 }
231
232 /* configure mv88e1111 port for copper or serdes
233 * For Copper we set auto link/duplex/speed detection
234 * For SerDes/Fiber we force 1000mbps link up and auto-neg duplex
235 */
236 static int config_mv88e1111_port_sfp(struct phy_device *pdev, int port,
237 bool sfp)
238 {
239 struct mv88e1111_priv *priv = dev_get_drvdata(&pdev->dev);
240 u16 reg;
241
242 if (port != 5 && port != 6)
243 return -EINVAL;
244
245 dev_dbg(&priv->client->dev, "%s: Port%d %s\n", __func__, port,
246 sfp ? "SFP" : "copper");
247 if (sfp) {
248 enable_sfp_txen(pdev, port, 1);
249
250 /* configure MV88E6176 Physical Control Port Register */
251 dev_info(&priv->client->dev,
252 "Port%d: SFP: force 1000mbps link up "
253 "(auto-negotiate duplex)\n",
254 port);
255 reg = read_switch_port(pdev, port, MV_PORT_PHYS_CONTROL);
256 reg &= ~0x3f; /* clear 5-0 */
257 reg |= (1 << 4) | (1 << 5); /* force link up */
258 reg |= 2; /* force 1000mbps */
259 write_switch_port(pdev, port, MV_PORT_PHYS_CONTROL, reg);
260 reg = read_switch_port(pdev, port, MV_PORT_PHYS_CONTROL);
261 }
262
263 /* copper */
264 else {
265 enable_sfp_txen(pdev, port, 0);
266
267 /* configure MV88E6176 Physical Control Port Register */
268 dev_info(&priv->client->dev,
269 "Port%d: Copper: set auto-neg link/duplex/speed\n",
270 port);
271 reg = read_switch_port(pdev, port, MV_PORT_PHYS_CONTROL);
272 reg &= ~0x3f; /* clear 5-0 */
273 reg |= 3; /* speed not forced */
274 write_switch_port(pdev, port, MV_PORT_PHYS_CONTROL, reg);
275 reg = read_switch_port(pdev, port, MV_PORT_PHYS_CONTROL);
276 }
277 dev_dbg(&priv->client->dev, "%s: Port%d %s PORT_PHYS_CONTROL=0x%04x\n",
278 __func__, port, sfp ? "SFP" : "copper",
279 read_switch_port(pdev, port, MV_PORT_PHYS_CONTROL));
280
281 return 0;
282 }
283
284 #if !IS_ENABLED(CONFIG_NET_DSA_MV88E6352) && defined(PORT_POWER_CONTROL)
285 static int enable_switch_port(struct phy_device *pdev, int port, bool enable)
286 {
287 struct mv88e1111_priv *priv = dev_get_drvdata(&pdev->dev);
288 u16 reg;
289
290 /* power up port */
291 dev_info(&priv->client->dev, "Port%d: %s\n", port,
292 enable ? "normal operation" : "power down");
293 reg = read_switch_port_phy(pdev, port, MV_PHY_CONTROL);
294 if (enable)
295 reg &= ~(1 << 11); /* Normal Operation */
296 else
297 reg |= (1 << 11); /* power down */
298 write_switch_port_phy(pdev, port, MV_PHY_CONTROL, reg);
299
300 reg = read_switch_port_phy(pdev, port, MV_PHY_CONTROL1);
301 if (enable)
302 reg &= ~(1 << 2); /* Normal Operation */
303 else
304 reg |= (1 << 2); /* power down */
305 write_switch_port_phy(pdev, port, MV_PHY_CONTROL1, reg);
306
307 return 0;
308 }
309 #endif
310
311 /*
312 * Sysfs API
313 */
314
315 struct mv88e1111_port_state *get_port_state(struct mv88e1111_priv *priv,
316 int port)
317 {
318 if (port == 5)
319 return &priv->port5;
320 if (port == 6)
321 return &priv->port6;
322 return NULL;
323 }
324
325 /*
326 * get MV88E6176 port number for a specific GW16083 port name
327 * The GW16083 ports as shown on the silkscreen are not mapped according to
328 * the MV88E6176 ports numbers.
329 */
330 static int gw16083_get_port(const char* name)
331 {
332 int i;
333 int map[] = { 3, 2, 1, 0, 5, 6 };
334
335 if (strncasecmp(name, "LAN", 3) != 0)
336 return -1;
337 i = name[3] - '0';
338 if (i < 1 || i > 6)
339 return -1;
340 return map[i-1];
341 }
342
343 #if !IS_ENABLED(CONFIG_NET_DSA_MV88E6352)
344 static ssize_t port_show(struct device *dev, struct device_attribute *attr,
345 char *buf)
346 {
347 struct mv88e1111_priv *priv = dev_get_drvdata(dev);
348 int port = -1;
349 u16 reg;
350
351 if (sscanf(attr->attr.name, "port%d", &port) != 1)
352 return 0;
353 if (port < 0 || port > 6)
354 return 0;
355 reg = read_switch_port_phy(priv->phydev, port, MV_PHY_CONTROL);
356 return sprintf(buf, "%s\n", (reg & (1 << 11)) ? "disabled" : "enabled");
357 }
358
359 #if defined(PORT_POWER_CONTROL)
360 static ssize_t port_store(struct device *dev, struct device_attribute *attr,
361 const char *buf, size_t count)
362 {
363 struct mv88e1111_priv *priv = dev_get_drvdata(dev);
364 int port = -1;
365 int val;
366
367 port = gw16083_get_port(attr->attr.name);
368 if (port < 0)
369 return 0;
370 if (sscanf(buf, "%d", &val) != 1)
371 return 0;
372 enable_switch_port(priv->phydev, port, val ? 1 : 0);
373 return count;
374 }
375
376 static DEVICE_ATTR(lan1, S_IWUSR | S_IRUGO, port_show, port_store);
377 static DEVICE_ATTR(lan2, S_IWUSR | S_IRUGO, port_show, port_store);
378 static DEVICE_ATTR(lan3, S_IWUSR | S_IRUGO, port_show, port_store);
379 static DEVICE_ATTR(lan4, S_IWUSR | S_IRUGO, port_show, port_store);
380 static DEVICE_ATTR(lan5, S_IWUSR | S_IRUGO, port_show, port_store);
381 static DEVICE_ATTR(lan6, S_IWUSR | S_IRUGO, port_show, port_store);
382 #else
383 static DEVICE_ATTR(lan1, S_IRUGO, port_show, NULL);
384 static DEVICE_ATTR(lan2, S_IRUGO, port_show, NULL);
385 static DEVICE_ATTR(lan3, S_IRUGO, port_show, NULL);
386 static DEVICE_ATTR(lan4, S_IRUGO, port_show, NULL);
387 static DEVICE_ATTR(lan5, S_IRUGO, port_show, NULL);
388 static DEVICE_ATTR(lan6, S_IRUGO, port_show, NULL);
389 #endif
390 #endif /* #if IS_ENABLED(CONFIG_NET_DSA_MV88E6352) */
391
392 static ssize_t portmode_show(struct device *dev, struct device_attribute *attr,
393 char *buf)
394 {
395 struct mv88e1111_priv *priv = dev_get_drvdata(dev);
396 struct mv88e1111_port_state *state;
397
398 state = get_port_state(priv, gw16083_get_port(attr->attr.name));
399 if (!state)
400 return 0;
401
402 return sprintf(buf, "%s\n", state->serdes ? "SFP" : "RJ45");
403 }
404 #ifdef PORT_MODE_CONTROL
405 static ssize_t portmode_store(struct device *dev, struct device_attribute *attr,
406 const char *buf, size_t count)
407 {
408 struct mv88e1111_priv *priv = dev_get_drvdata(dev);
409 struct mv88e1111_port_state *state;
410 u16 reg;
411 int port;
412
413 port = gw16083_get_port(attr->attr.name);
414 state = get_port_state(priv, port);
415 if (!state)
416 return 0;
417
418 reg = read_switch_port_phy(priv->phydev, port, MII_M1111_PHY_EXT_SR);
419 if (strcasecmp(buf, "auto") == 0) {
420 reg &= ~(1<<15); /* enable auto-selection */
421 dev_info(&priv->client->dev, "Port%d: enable auto-selection\n",
422 port);
423 } else if (strcasecmp(buf, "RJ45") == 0) {
424 reg |= (1<<15); /* disable auto-selection */
425 reg |= 0xb; /* RGMII to Copper */
426 config_mv88e1111_port_sfp(priv->phydev, port, 0);
427 dev_info(&priv->client->dev, "Port%d: select RJ45\n", port);
428 } else if (strcasecmp(buf, "SFP") == 0) {
429 reg |= (1<<15); /* disable auto-selection */
430 reg |= 0x3; /* RGMII to Fiber */
431 config_mv88e1111_port_sfp(priv->phydev, port, 1);
432 dev_info(&priv->client->dev, "Port%d: select SFP\n", port);
433 }
434 write_switch_port_phy(priv->phydev, port, MII_M1111_PHY_EXT_SR, reg);
435
436 return count;
437 }
438
439 static DEVICE_ATTR(lan5_mode, S_IWUSR | S_IRUGO, portmode_show, portmode_store);
440 static DEVICE_ATTR(lan6_mode, S_IWUSR | S_IRUGO, portmode_show, portmode_store);
441 #else
442 static DEVICE_ATTR(lan5_mode, S_IRUGO, portmode_show, NULL);
443 static DEVICE_ATTR(lan6_mode, S_IRUGO, portmode_show, NULL);
444 #endif
445
446 static ssize_t portsfp_show(struct device *dev, struct device_attribute *attr,
447 char *buf)
448 {
449 struct mv88e1111_priv *priv = dev_get_drvdata(dev);
450 struct mv88e1111_port_state *state;
451
452 state = get_port_state(priv, gw16083_get_port(attr->attr.name));
453 if (!state)
454 return 0;
455
456 if (!state->sfp_present)
457 return 0;
458
459 return sprintf(buf, "%s\n", state->sfp_id);
460 }
461
462 static DEVICE_ATTR(lan5_sfp, S_IRUGO, portsfp_show, NULL);
463 static DEVICE_ATTR(lan6_sfp, S_IRUGO, portsfp_show, NULL);
464
465 /*
466 * PHY driver
467 */
468
469 /* check MV88E1111 PHY status and MV88E6176 GPIO */
470 static void
471 mv88e6176_work(struct work_struct *work)
472 {
473 struct mv88e1111_priv *priv =
474 container_of(work, struct mv88e1111_priv, work.work);
475 struct phy_device *pdev = priv->phydev;
476 struct device *dev = &priv->client->dev;
477 struct mv88e1111_port_state *state;
478 bool serdes, sfp_present, sfp_signal;
479 int port;
480 u16 gpio;
481
482 mutex_lock(&pdev->lock);
483 gpio = read_switch_scratch(pdev, MV_GPIO_DATA);
484 for (port = 5; port < 7; port++) {
485 serdes = (read_switch_port_phy(pdev, port, MII_M1111_PHY_EXT_SR)
486 & (1<<13)) ? 1 : 0;
487 dev_dbg(dev, "%s: Port%d GPIO:0x%02x SerDes:%d\n",
488 __func__, port, gpio, serdes);
489 switch(port) {
490 case 5:
491 state = &priv->port5;
492 sfp_present = !((gpio >> 5) & 1);
493 sfp_signal = !((gpio >> 6) & 1);
494 break;
495 case 6:
496 state = &priv->port6;
497 sfp_present = !((gpio >> 3) & 1);
498 sfp_signal = !((gpio >> 4) & 1);
499 break;
500 }
501
502 /*
503 * on sfp_detect read/verify SFP MSA and set sfp_compat
504 * on sfp_signal issue link down?
505 * on serdes auto-select
506 */
507 if (state->sfp_present != sfp_present) {
508 state->sfp_present = sfp_present;
509 dev_info(dev, "Port%d: SFP %s\n",
510 port, sfp_present ? "inserted" : "removed");
511 if (state->sfp_present) {
512 if (gw16083_read_port_sfp(priv->client, state))
513 state->sfp_compat = false;
514 else
515 state->sfp_compat = true;
516 /* trigger a re-select/enable below */
517 state->serdes = !serdes;
518 pdev->state = PHY_RUNNING;
519 } else {
520 state->sfp_compat = false;
521 state->sfp_enabled = false;
522 pdev->state = PHY_NOLINK;
523 }
524 }
525 if (state->sfp_signal != sfp_signal) {
526 state->sfp_signal = sfp_signal;
527 dev_info(dev, "Port%d: SFP signal %s\n",
528 port, sfp_signal ? "detected" : "lost");
529 }
530 if (state->serdes != serdes) {
531 state->serdes = serdes;
532 dev_info(dev, "Port%d: %s auto-selected\n",
533 port, serdes ? "SERDES" : "copper");
534
535 /*
536 * if auto-selection has switched to copper
537 * disable serdes
538 */
539 if (!serdes) {
540 config_mv88e1111_port_sfp(pdev, port, 0);
541 state->sfp_enabled = false;
542 }
543 }
544
545 /* if compatible SFP module and not yet enabled then enable */
546 if (state->sfp_compat && state->sfp_signal &&
547 !state->sfp_enabled)
548 {
549 if (!config_mv88e1111_port_sfp(pdev, port, 1))
550 state->sfp_enabled = true;
551 }
552 }
553 mutex_unlock(&pdev->lock);
554
555 queue_delayed_work(priv->workq, &priv->work, HZ);
556 }
557
558 static int
559 mv88e6176_read_status(struct phy_device *pdev)
560 {
561 return 0;
562 }
563
564 static int
565 mv88e6176_config_aneg(struct phy_device *pdev)
566 {
567 return 0;
568 }
569
570 static int
571 mv88e6176_config_init(struct phy_device *pdev)
572 {
573 pdev->state = PHY_RUNNING;
574
575 return 0;
576 }
577
578 static void
579 mv88e6176_remove(struct phy_device *pdev)
580 {
581 struct mv88e1111_priv *priv = dev_get_drvdata(&pdev->dev);
582
583 dev_dbg(&priv->client->dev, "%s", __func__);
584
585 destroy_workqueue(priv->workq);
586 #if !IS_ENABLED(CONFIG_NET_DSA_MV88E6352)
587 device_remove_file(&pdev->dev, &dev_attr_lan1);
588 device_remove_file(&pdev->dev, &dev_attr_lan2);
589 device_remove_file(&pdev->dev, &dev_attr_lan3);
590 device_remove_file(&pdev->dev, &dev_attr_lan4);
591 device_remove_file(&pdev->dev, &dev_attr_lan5);
592 device_remove_file(&pdev->dev, &dev_attr_lan6);
593 #endif
594 device_remove_file(&pdev->dev, &dev_attr_lan5_sfp);
595 device_remove_file(&pdev->dev, &dev_attr_lan6_sfp);
596 device_remove_file(&pdev->dev, &dev_attr_lan5_mode);
597 device_remove_file(&pdev->dev, &dev_attr_lan6_mode);
598 sysfs_remove_link(kernel_kobj, "gw16083");
599 }
600
601 /*
602 * mv88e6176_probe - called any time an MV88E6176 is found on an mdio bus
603 * Determine if this MV88E6176 is indeed on a GW16083 and if so configure
604 * the port5/port6 phy and register a background procedure for monitoring
605 * their states to support SFP vs Copper switching.
606 *
607 * Verify this is a GW16083 by ensuring:
608 * - the phy address matches that of a GW16083
609 * - the mdio bus is from an i210 device (igb driver)
610 * - there are MV881111 PHY's hanging off of Port5 and Port6
611 */
612 static int
613 mv88e6176_probe(struct phy_device *pdev)
614 {
615 int port;
616 int ret = 0;
617 u32 id, reg;
618 struct mv88e1111_priv *priv;
619 struct device *dev;
620 #if IS_ENABLED(CONFIG_NET_DSA_MV88E6352)
621 struct net_device *netdev = NULL;
622 #endif
623
624 dev_dbg(&pdev->dev, "%s: addr=0x%02x bus=%s:%s gw16083_client=%p\n",
625 __func__, pdev->addr, pdev->bus->name, pdev->bus->id,
626 gw16083_client);
627
628 /* In single-chip addressing mode the MV88E6176 shows up on 0x10-0x16 */
629 if (pdev->addr != MV_BASE)
630 return 0;
631
632 /* i2c driver needs to be loaded first */
633 if (!gw16083_client)
634 return 0;
635 dev = &gw16083_client->dev;
636
637 /* gw16083 has MV88E1676 hanging off of i210 mdio bus */
638 if (strcmp(pdev->bus->name, "igb_enet_mii_bus") != 0)
639 return 0;
640
641 /* verify Port5/Port6 have an MV88E1111 PHY hanging off them */
642 for (port = 5; port < 7; port++) {
643 id = read_switch_port_phy(pdev, port,
644 MII_M1111_PHY_IDENT0) << 16;
645 id |= read_switch_port_phy(pdev, port, MII_M1111_PHY_IDENT1);
646 if ((id & MII_M1111_PHY_ID_MASK) != MII_M1111_PHY_ID) {
647 dev_err(dev, "Port%d: No MV88E1111 PHY detected", port);
648 return 0;
649 }
650 }
651
652 #if IS_ENABLED(CONFIG_NET_DSA_MV88E6352)
653 /*
654 * Find the netdev this bus is hanging off of and register with DSA:
655 * The netdev must be an Intel I210 (igb) with Gateworks MAC addr
656 */
657 read_lock(&dev_base_lock);
658 for_each_netdev(&init_net, netdev) {
659 if (netdev->dev.parent &&
660 !strcmp(netdev->dev.parent->driver->name, "igb") &&
661 (netdev->perm_addr[0] == 0x00) &&
662 (netdev->perm_addr[1] == 0xd0) &&
663 (netdev->perm_addr[2] == 0x12))
664 {
665 switch_plat_data.netdev = &netdev->dev;
666 switch_plat_data.chip[0].host_dev = &pdev->bus->dev;
667 break;
668 }
669 }
670 read_unlock(&dev_base_lock);
671
672 if (switch_plat_data.netdev) {
673 platform_device_register(&switch_device);
674 dev_info(dev, "registered GW16083 DSA switch\n");
675 } else {
676 dev_err(dev, "failed to find netdev for DSA switch\n");
677 }
678 #endif
679
680 /*
681 * port5/6 config: MV88E1111 PHY
682 * Register 20: PHY Control Register
683 * R20_7: add delay to RX_CLK for RXD
684 * R20_1: add delay to TX_CLK for TXD
685 * Register 24: LED Control Register
686 * 0x4111:
687 * Pulse stretch 170 to 340 ms
688 * Register 0: Control Register
689 * R0_15: phy reset
690 */
691 dev_info(dev, "Configuring MV88E6176 7-port switch");
692 for (port = 5; port < 7; port++) {
693 #ifdef RGMII_DELAY_ON_PHY
694 /* phy rx/tx delay */
695 reg = read_switch_port_phy(pdev, port, MII_M1111_PHY_EXT_CR);
696 reg |= (1<<1) | (1<<7);
697 write_switch_port_phy(pdev, port, MII_M1111_PHY_EXT_CR, reg);
698 #else
699 write_switch_port(pdev, port, MV_PORT_PHYS_CONTROL, 0xC003);
700 #endif
701
702 /* led config */
703 write_switch_port_phy(pdev, port, MII_M1111_PHY_LED_CONTROL,
704 MII_M1111_PHY_LED_PULSE_STR);
705 /* reset phy */
706 reg = read_switch_port_phy(pdev, port, MII_M1111_PHY_CONTROL);
707 reg |= MII_M1111_PHY_CONTROL_RESET;
708 write_switch_port_phy(pdev, port, MII_M1111_PHY_CONTROL, reg);
709 dev_info(dev, "Port%d: MV88E111 PHY configured\n", port);
710 }
711
712 /*
713 * GPIO Configuration:
714 * GPIO1: FIB5_TXEN# (output)
715 * GPIO2: FIB6_TXEN# (output)
716 * GPIO3: FIB6_PRES# (input)
717 * GPIO4: FIB6_LOS (input)
718 * GPIO5: FIB5_PRES# (input)
719 * GPIO6: FIB5_LOS (input)
720 */
721 write_switch_scratch(pdev, MV_GPIO_DATA, 0x06); /* GPIO[2:1] out hi */
722 write_switch_scratch(pdev, MV_GPIO_DIR, 0x78); /* GPIO[6:3] inp */
723
724 pdev->irq = PHY_POLL;
725
726 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
727 if (!priv)
728 return -ENOMEM;
729 memset(priv, 0, sizeof(*priv));
730 priv->phydev = pdev;
731 priv->client = gw16083_client;
732 priv->port5.port = 5;
733 priv->port6.port = 6;
734 dev_set_drvdata(&pdev->dev, priv);
735
736 /* register sysfs API */
737 #if !IS_ENABLED(CONFIG_NET_DSA_MV88E6352)
738 ret |= device_create_file(&pdev->dev, &dev_attr_lan1);
739 ret |= device_create_file(&pdev->dev, &dev_attr_lan2);
740 ret |= device_create_file(&pdev->dev, &dev_attr_lan3);
741 ret |= device_create_file(&pdev->dev, &dev_attr_lan4);
742 ret |= device_create_file(&pdev->dev, &dev_attr_lan5);
743 ret |= device_create_file(&pdev->dev, &dev_attr_lan6);
744 #endif
745 ret |= device_create_file(&pdev->dev, &dev_attr_lan5_sfp);
746 ret |= device_create_file(&pdev->dev, &dev_attr_lan6_sfp);
747 ret |= device_create_file(&pdev->dev, &dev_attr_lan5_mode);
748 ret |= device_create_file(&pdev->dev, &dev_attr_lan6_mode);
749
750 if (unlikely(ret))
751 dev_err(&pdev->dev, "Failed creating attrs\n");
752
753 /* Add a nice symlink to the real device */
754 ret = sysfs_create_link(kernel_kobj, &pdev->dev.kobj, "gw16083");
755
756 INIT_DELAYED_WORK(&priv->work, mv88e6176_work);
757 priv->workq = create_singlethread_workqueue("gw16083");
758 if (!priv->workq)
759 return -ENODEV;
760 queue_delayed_work(priv->workq, &priv->work, 0);
761
762 dev_dbg(dev, "initial state: GPIO=0x%02x "
763 "Port5_serdes=%d Port6_serdes=%d\n",
764 read_switch_scratch(pdev, MV_GPIO_DATA),
765 (read_switch_port_phy(pdev, 5, MII_M1111_PHY_EXT_SR)
766 & (1<<13) ? 1:0),
767 (read_switch_port_phy(pdev, 6, MII_M1111_PHY_EXT_SR)
768 & (1<<13) ? 1:0));
769
770 return ret;
771 }
772
773 static struct phy_driver mv88e6176_phy_driver = {
774 .name = "gw16083",
775 .phy_id = MV_IDENT_VALUE,
776 .phy_id_mask = MV_IDENT_MASK,
777 .features = PHY_BASIC_FEATURES,
778 .probe = &mv88e6176_probe,
779 .remove = &mv88e6176_remove,
780 .config_init = &mv88e6176_config_init,
781 .config_aneg = &mv88e6176_config_aneg,
782 .read_status = &mv88e6176_read_status,
783 .driver = { .owner = THIS_MODULE },
784 };
785
786 /*
787 * I2C driver
788 */
789
790 /* See SFF-8472 */
791 struct sfp_msa {
792 /* Basic ID fields */
793 u8 identifier;
794 u8 ext_identifier;
795 u8 connector;
796 u8 transceiver[8];
797 u8 encoding;
798 u8 br_nominal;
799 u8 rate_identifier;
800 u8 length_smf_km;
801 u8 length_smf;
802 u8 length_om2;
803 u8 length_om1;
804 u8 length_om4;
805 u8 length_om3;
806 u8 vendor_name[16];
807 u8 transceiver2;
808 u8 vendor_oui[3];
809 u8 vendor_pn[16];
810 u8 vendor_rev[4];
811 u8 wavelength[2];
812 u8 resv1;
813 u8 cc_base;
814
815 /* extended id fields */
816 u8 options[2];
817 u8 br_max;
818 u8 br_min;
819 u8 vendor_sn[16];
820 u8 date_code[8];
821 u8 diags_type;
822 u8 enhanced_options;
823 u8 sff8472_compliance;
824 u8 cc_ext;
825
826 /* Vendor specific ID fields */
827 u8 vendor_data[32];
828 u8 sff8079[128];
829 };
830
831 enum identifier {
832 UNKNOWN,
833 GBIC,
834 SFF,
835 SFP,
836 XBI,
837 XENPACK,
838 XFP,
839 XFF,
840 XFP_E,
841 XPAK,
842 X2,
843 DWDM_SFP,
844 QSFP,
845 MAX_ID,
846 };
847
848 const char* id_names[] = {
849 "UNKONWN",
850 "GBIC",
851 "SFF",
852 "SFP",
853 NULL,
854 };
855
856 /* Flags for SFP modules compatible with ETH up to 1Gb */
857 struct sfp_flags {
858 u8 e1000_base_sx:1;
859 u8 e1000_base_lx:1;
860 u8 e1000_base_cx:1;
861 u8 e1000_base_t:1;
862 u8 e100_base_lx:1;
863 u8 e100_base_fx:1;
864 u8 e10_base_bx10:1;
865 u8 e10_base_px:1;
866 };
867
868 #define STRING_APPEND(str, src) \
869 strncat(str, src, sizeof(src)); \
870 for (i = 1; i < sizeof(str); i++) \
871 if (str[i-1] == ' ' && str[i] == ' ') \
872 str[i] = 0;
873
874 static int gw16083_read_port_sfp(struct i2c_client *client,
875 struct mv88e1111_port_state *state)
876 {
877 int ret = 0;
878 u8 data[256];
879 struct sfp_flags *eth_flags;
880 u8 crc;
881 int i;
882 u8 *str;
883 struct sfp_msa *sfp_msa = (struct sfp_msa *)data;
884 int port = state->port;
885 union i2c_smbus_data d;
886
887 dev_dbg(&client->dev, "%s Port%d\n", __func__, port);
888 if (!i2c_check_functionality(client->adapter,
889 I2C_FUNC_SMBUS_READ_I2C_BLOCK))
890 return -ENODEV;
891 d.byte = (port == 5) ? 1 : 2;
892 if (i2c_smbus_xfer(client->adapter, GW16083_I2C_ADDR_PCA9543,
893 client->flags, I2C_SMBUS_WRITE, 0,
894 I2C_SMBUS_BYTE_DATA, &d) < 0)
895 {
896 dev_err(&client->dev,
897 "Port%d: failed writing PCA9543 register\n", port);
898 return ret;
899 }
900
901 /* read all 256 bytes of SFP EEPROM */
902 for (i = 0; i < sizeof(data); i += I2C_SMBUS_BLOCK_MAX) {
903 d.block[0] = I2C_SMBUS_BLOCK_MAX;
904 if (i2c_smbus_xfer(client->adapter, GW16083_I2C_ADDR_SFP1,
905 client->flags, I2C_SMBUS_READ, i,
906 I2C_SMBUS_I2C_BLOCK_DATA, &d) < 0)
907 {
908 dev_err(&client->dev,
909 "Port%d: failed reading SFP data\n", port);
910 return ret;
911 }
912 memcpy(data + i, d.block + 1, I2C_SMBUS_BLOCK_MAX);
913 }
914
915 /* Validate checksums */
916 for (crc = 0, i = 0; i < 63; i++)
917 crc += data[i];
918 if (crc != sfp_msa->cc_base) {
919 dev_err(&client->dev, "Port%d: "
920 "Checksum failure for Base ID fields: 0x%02x\n", port,
921 crc);
922 #ifdef FAIL_ON_CHECKSUM_ERR
923 return -EINVAL;
924 #endif
925 }
926 for (crc = 0, i = 64; i < 95; i++)
927 crc += data[i];
928 if (crc != sfp_msa->cc_ext) {
929 dev_err(&client->dev, "Port%d: "
930 "Checksum failure for Extended ID fields: 0x%02x\n",
931 port, crc);
932 #ifdef FAIL_ON_CHECKSUM_ERR
933 return -EINVAL;
934 #endif
935 }
936 state->sfp_id[0] = 0;
937 for (i = 0; id_names[i]; i++) {
938 if (sfp_msa->identifier == i) {
939 sprintf(state->sfp_id, "%s: ", id_names[i]);
940 break;
941 }
942 }
943 STRING_APPEND(state->sfp_id, sfp_msa->vendor_oui);
944 STRING_APPEND(state->sfp_id, sfp_msa->vendor_name);
945 STRING_APPEND(state->sfp_id, sfp_msa->vendor_pn);
946 STRING_APPEND(state->sfp_id, sfp_msa->vendor_rev);
947 STRING_APPEND(state->sfp_id, sfp_msa->vendor_sn);
948 dev_info(&client->dev, "Port%d: %s\n", port, state->sfp_id);
949
950 if ((sfp_msa->identifier != GBIC) &&
951 (sfp_msa->identifier != SFF) &&
952 (sfp_msa->identifier != SFP))
953 {
954 dev_err(&client->dev, "Port%d: Unknown module identifier: %d\n",
955 port, sfp_msa->identifier);
956 return -EINVAL;
957 }
958
959 str = "";
960 eth_flags = (struct sfp_flags *)(sfp_msa->transceiver + 3);
961 if (eth_flags->e1000_base_sx) {
962 str = "1000Base-SX (Fiber)";
963 } else if (eth_flags->e1000_base_lx) {
964 str = "1000Base-LX (Fiber)";
965 } else if (eth_flags->e1000_base_t) {
966 str = "1000Base-T (Copper)";
967 } else if (eth_flags->e100_base_fx) {
968 str = "100Base-FX (Fiber) - not supported";
969 ret = -EINVAL;
970 } else {
971 str = "Unknown/Unsupported media type";
972 ret = -EINVAL;
973 }
974 if (ret)
975 dev_err(&client->dev, "Port%d: %s (0x%02x)\n", port, str,
976 sfp_msa->transceiver[3]);
977 else
978 dev_info(&client->dev, "Port%d: %s (0x%02x)\n", port, str,
979 sfp_msa->transceiver[3]);
980
981 return ret;
982 }
983
984 static int gw16083_probe(struct i2c_client *client,
985 const struct i2c_device_id *id)
986 {
987 int ret;
988
989 dev_info(&client->dev, "GW16083 Ethernet Expansion Mezzanine\n");
990 if (gw16083_client) {
991 dev_err(&client->dev, "client already registered\n");
992 return -EINVAL;
993 }
994 gw16083_client = client;
995
996 ret = phy_driver_register(&mv88e6176_phy_driver);
997 if (ret)
998 dev_err(&client->dev,
999 "failed to register mv88e6176 phy driver: %d\n", ret);
1000 return ret;
1001 }
1002
1003 static int gw16083_remove(struct i2c_client *client)
1004 {
1005 dev_dbg(&client->dev, "%s\n", __func__);
1006
1007 phy_driver_unregister(&mv88e6176_phy_driver);
1008 gw16083_client = NULL;
1009 return 0;
1010 }
1011
1012 static const struct of_device_id gw16083_dt_ids[] = {
1013 { .compatible = "gateworks,gw16083", },
1014 { }
1015 };
1016
1017 MODULE_DEVICE_TABLE(of, gw16083_dt_ids);
1018
1019 static const struct i2c_device_id gw16083_id[] = {
1020 { "gw16083", 0 },
1021 { }
1022 };
1023 MODULE_DEVICE_TABLE(i2c, gw16083_id);
1024
1025 static struct i2c_driver gw16083_driver = {
1026 .driver = {
1027 .name = "gw16083",
1028 .of_match_table = gw16083_dt_ids,
1029 },
1030 .probe = gw16083_probe,
1031 .remove = gw16083_remove,
1032 .id_table = gw16083_id,
1033 };
1034
1035 static int __init mv88e6176_init(void)
1036 {
1037 return i2c_add_driver(&gw16083_driver);
1038 }
1039
1040 static void __exit mv88e6176_exit(void)
1041 {
1042 i2c_del_driver(&gw16083_driver);
1043 }
1044
1045 module_init(mv88e6176_init);
1046 module_exit(mv88e6176_exit);