2 * drivers/net/phy/gw16083.c
4 * Driver for GW16083 Ventana Ethernet Expansion Mezzanine
8 * Copyright (c) 2014 Tim Harvey <tharvey@gateworks.com>
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.
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.
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>
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>
51 #include <linux/uaccess.h>
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 */
60 MODULE_DESCRIPTION("GW16083 driver");
61 MODULE_AUTHOR("Tim Harvey");
62 MODULE_LICENSE("GPL");
64 struct mv88e1111_port_state
{
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
;
90 #if IS_ENABLED(CONFIG_NET_DSA_MV88E6352)
91 static struct dsa_chip_data switch_chip_data
= {
103 static struct dsa_platform_data switch_plat_data
= {
105 .chip
= &switch_chip_data
,
108 static struct platform_device switch_device
= {
112 .dev
.platform_data
= &switch_plat_data
,
116 static struct i2c_client
*gw16083_client
= NULL
;
118 static int gw16083_read_port_sfp(struct i2c_client
*client
,
119 struct mv88e1111_port_state
*state
);
121 /* read switch port register from port0-6 */
122 u16
read_switch_port(struct phy_device
*pdev
, int port
, u8 regaddr
)
124 return pdev
->bus
->read(pdev
->bus
, MV_BASE
+ port
, regaddr
);
127 /* write switch port register to port0-6 */
128 int write_switch_port(struct phy_device
*pdev
, int port
, u8 regaddr
, u16 val
)
130 return pdev
->bus
->write(pdev
->bus
, MV_BASE
+ port
, regaddr
, val
);
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.
138 int read_switch_port_phy(struct phy_device
*pdev
, int port
, u8 regaddr
)
140 struct mv88e1111_priv
*priv
= dev_get_drvdata(&pdev
->dev
);
144 dev_dbg(&priv
->client
->dev
, "read_phy: port%d reg=0x%02x\n", port
,
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
,
153 if (!(reg
& (1<<15)))
160 reg
= pdev
->bus
->read(pdev
->bus
, MV_GLOBAL2
, MV_SMI_PHY_DATA
);
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.
169 int write_switch_port_phy(struct phy_device
*pdev
, int port
, u8 addr
, u16 reg
)
171 struct mv88e1111_priv
*priv
= dev_get_drvdata(&pdev
->dev
);
174 dev_dbg(&priv
->client
->dev
, "write_phy: port%d reg=0x%02x val=0x%04x\n",
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
,
184 if (!(reg
& (1<<15)))
195 /* read a scratch register from switch */
196 inline u8
read_switch_scratch(struct phy_device
*pdev
, u8 reg
)
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;
202 /* write a scratch register to switch */
203 inline void write_switch_scratch(struct phy_device
*pdev
, u8 reg
, u8 val
)
205 pdev
->bus
->write(pdev
->bus
, MV_GLOBAL2
, MV_SCRATCH_MISC
,
206 (1 << 15) | (reg
<< 8) | val
);
209 /* enable or disable an SFP's TXEN signal */
210 static int enable_sfp_txen(struct phy_device
*pdev
, int port
, bool enable
)
212 struct mv88e1111_priv
*priv
= dev_get_drvdata(&pdev
->dev
);
216 if (port
!= 5 && port
!= 6)
219 /* GPIO[2:1] output low to enable TXEN */
220 bit
= (port
== 5) ? 1 : 2;
221 gpio
= read_switch_scratch(pdev
, MV_GPIO_DATA
);
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");
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
236 static int config_mv88e1111_port_sfp(struct phy_device
*pdev
, int port
,
239 struct mv88e1111_priv
*priv
= dev_get_drvdata(&pdev
->dev
);
242 if (port
!= 5 && port
!= 6)
245 dev_dbg(&priv
->client
->dev
, "%s: Port%d %s\n", __func__
, port
,
246 sfp
? "SFP" : "copper");
248 enable_sfp_txen(pdev
, port
, 1);
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",
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
);
265 enable_sfp_txen(pdev
, port
, 0);
267 /* configure MV88E6176 Physical Control Port Register */
268 dev_info(&priv
->client
->dev
,
269 "Port%d: Copper: set auto-neg link/duplex/speed\n",
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
);
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
));
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
)
287 struct mv88e1111_priv
*priv
= dev_get_drvdata(&pdev
->dev
);
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
);
295 reg
&= ~(1 << 11); /* Normal Operation */
297 reg
|= (1 << 11); /* power down */
298 write_switch_port_phy(pdev
, port
, MV_PHY_CONTROL
, reg
);
300 reg
= read_switch_port_phy(pdev
, port
, MV_PHY_CONTROL1
);
302 reg
&= ~(1 << 2); /* Normal Operation */
304 reg
|= (1 << 2); /* power down */
305 write_switch_port_phy(pdev
, port
, MV_PHY_CONTROL1
, reg
);
315 struct mv88e1111_port_state
*get_port_state(struct mv88e1111_priv
*priv
,
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.
330 static int gw16083_get_port(const char* name
)
333 int map
[] = { 3, 2, 1, 0, 5, 6 };
335 if (strncasecmp(name
, "LAN", 3) != 0)
343 #if !IS_ENABLED(CONFIG_NET_DSA_MV88E6352)
344 static ssize_t
port_show(struct device
*dev
, struct device_attribute
*attr
,
347 struct mv88e1111_priv
*priv
= dev_get_drvdata(dev
);
351 if (sscanf(attr
->attr
.name
, "port%d", &port
) != 1)
353 if (port
< 0 || port
> 6)
355 reg
= read_switch_port_phy(priv
->phydev
, port
, MV_PHY_CONTROL
);
356 return sprintf(buf
, "%s\n", (reg
& (1 << 11)) ? "disabled" : "enabled");
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
)
363 struct mv88e1111_priv
*priv
= dev_get_drvdata(dev
);
367 port
= gw16083_get_port(attr
->attr
.name
);
370 if (sscanf(buf
, "%d", &val
) != 1)
372 enable_switch_port(priv
->phydev
, port
, val
? 1 : 0);
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
);
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
);
390 #endif /* #if IS_ENABLED(CONFIG_NET_DSA_MV88E6352) */
392 static ssize_t
portmode_show(struct device
*dev
, struct device_attribute
*attr
,
395 struct mv88e1111_priv
*priv
= dev_get_drvdata(dev
);
396 struct mv88e1111_port_state
*state
;
398 state
= get_port_state(priv
, gw16083_get_port(attr
->attr
.name
));
402 return sprintf(buf
, "%s\n", state
->serdes
? "SFP" : "RJ45");
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
)
408 struct mv88e1111_priv
*priv
= dev_get_drvdata(dev
);
409 struct mv88e1111_port_state
*state
;
413 port
= gw16083_get_port(attr
->attr
.name
);
414 state
= get_port_state(priv
, port
);
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",
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
);
434 write_switch_port_phy(priv
->phydev
, port
, MII_M1111_PHY_EXT_SR
, reg
);
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
);
442 static DEVICE_ATTR(lan5_mode
, S_IRUGO
, portmode_show
, NULL
);
443 static DEVICE_ATTR(lan6_mode
, S_IRUGO
, portmode_show
, NULL
);
446 static ssize_t
portsfp_show(struct device
*dev
, struct device_attribute
*attr
,
449 struct mv88e1111_priv
*priv
= dev_get_drvdata(dev
);
450 struct mv88e1111_port_state
*state
;
452 state
= get_port_state(priv
, gw16083_get_port(attr
->attr
.name
));
456 if (!state
->sfp_present
)
459 return sprintf(buf
, "%s\n", state
->sfp_id
);
462 static DEVICE_ATTR(lan5_sfp
, S_IRUGO
, portsfp_show
, NULL
);
463 static DEVICE_ATTR(lan6_sfp
, S_IRUGO
, portsfp_show
, NULL
);
469 /* check MV88E1111 PHY status and MV88E6176 GPIO */
471 mv88e6176_work(struct work_struct
*work
)
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
;
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
)
487 dev_dbg(dev
, "%s: Port%d GPIO:0x%02x SerDes:%d\n",
488 __func__
, port
, gpio
, serdes
);
491 state
= &priv
->port5
;
492 sfp_present
= !((gpio
>> 5) & 1);
493 sfp_signal
= !((gpio
>> 6) & 1);
496 state
= &priv
->port6
;
497 sfp_present
= !((gpio
>> 3) & 1);
498 sfp_signal
= !((gpio
>> 4) & 1);
503 * on sfp_detect read/verify SFP MSA and set sfp_compat
504 * on sfp_signal issue link down?
505 * on serdes auto-select
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;
515 state
->sfp_compat
= true;
516 /* trigger a re-select/enable below */
517 state
->serdes
= !serdes
;
518 pdev
->state
= PHY_RUNNING
;
520 state
->sfp_compat
= false;
521 state
->sfp_enabled
= false;
522 pdev
->state
= PHY_NOLINK
;
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");
530 if (state
->serdes
!= serdes
) {
531 state
->serdes
= serdes
;
532 dev_info(dev
, "Port%d: %s auto-selected\n",
533 port
, serdes
? "SERDES" : "copper");
536 * if auto-selection has switched to copper
540 config_mv88e1111_port_sfp(pdev
, port
, 0);
541 state
->sfp_enabled
= false;
545 /* if compatible SFP module and not yet enabled then enable */
546 if (state
->sfp_compat
&& state
->sfp_signal
&&
549 if (!config_mv88e1111_port_sfp(pdev
, port
, 1))
550 state
->sfp_enabled
= true;
553 mutex_unlock(&pdev
->lock
);
555 queue_delayed_work(priv
->workq
, &priv
->work
, HZ
);
559 mv88e6176_read_status(struct phy_device
*pdev
)
565 mv88e6176_config_aneg(struct phy_device
*pdev
)
571 mv88e6176_config_init(struct phy_device
*pdev
)
573 pdev
->state
= PHY_RUNNING
;
579 mv88e6176_remove(struct phy_device
*pdev
)
581 struct mv88e1111_priv
*priv
= dev_get_drvdata(&pdev
->dev
);
583 dev_dbg(&priv
->client
->dev
, "%s", __func__
);
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
);
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");
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.
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
613 mv88e6176_probe(struct phy_device
*pdev
)
618 struct mv88e1111_priv
*priv
;
620 #if IS_ENABLED(CONFIG_NET_DSA_MV88E6352)
621 struct net_device
*netdev
= NULL
;
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
,
628 /* In single-chip addressing mode the MV88E6176 shows up on 0x10-0x16 */
629 if (pdev
->addr
!= MV_BASE
)
632 /* i2c driver needs to be loaded first */
635 dev
= &gw16083_client
->dev
;
637 /* gw16083 has MV88E1676 hanging off of i210 mdio bus */
638 if (strcmp(pdev
->bus
->name
, "igb_enet_mii_bus") != 0)
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
);
652 #if IS_ENABLED(CONFIG_NET_DSA_MV88E6352)
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
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))
665 switch_plat_data
.netdev
= &netdev
->dev
;
666 switch_plat_data
.chip
[0].host_dev
= &pdev
->bus
->dev
;
670 read_unlock(&dev_base_lock
);
672 if (switch_plat_data
.netdev
) {
673 platform_device_register(&switch_device
);
674 dev_info(dev
, "registered GW16083 DSA switch\n");
676 dev_err(dev
, "failed to find netdev for DSA switch\n");
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
687 * Pulse stretch 170 to 340 ms
688 * Register 0: Control Register
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
);
699 write_switch_port(pdev
, port
, MV_PORT_PHYS_CONTROL
, 0xC003);
703 write_switch_port_phy(pdev
, port
, MII_M1111_PHY_LED_CONTROL
,
704 MII_M1111_PHY_LED_PULSE_STR
);
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
);
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)
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 */
724 pdev
->irq
= PHY_POLL
;
726 priv
= devm_kzalloc(&pdev
->dev
, sizeof(*priv
), GFP_KERNEL
);
729 memset(priv
, 0, sizeof(*priv
));
731 priv
->client
= gw16083_client
;
732 priv
->port5
.port
= 5;
733 priv
->port6
.port
= 6;
734 dev_set_drvdata(&pdev
->dev
, priv
);
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
);
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
);
751 dev_err(&pdev
->dev
, "Failed creating attrs\n");
753 /* Add a nice symlink to the real device */
754 ret
= sysfs_create_link(kernel_kobj
, &pdev
->dev
.kobj
, "gw16083");
756 INIT_DELAYED_WORK(&priv
->work
, mv88e6176_work
);
757 priv
->workq
= create_singlethread_workqueue("gw16083");
760 queue_delayed_work(priv
->workq
, &priv
->work
, 0);
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
)
767 (read_switch_port_phy(pdev
, 6, MII_M1111_PHY_EXT_SR
)
773 static struct phy_driver mv88e6176_phy_driver
= {
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
},
792 /* Basic ID fields */
815 /* extended id fields */
823 u8 sff8472_compliance
;
826 /* Vendor specific ID fields */
848 const char* id_names
[] = {
856 /* Flags for SFP modules compatible with ETH up to 1Gb */
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] == ' ') \
874 static int gw16083_read_port_sfp(struct i2c_client
*client
,
875 struct mv88e1111_port_state
*state
)
879 struct sfp_flags
*eth_flags
;
883 struct sfp_msa
*sfp_msa
= (struct sfp_msa
*)data
;
884 int port
= state
->port
;
885 union i2c_smbus_data d
;
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
))
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)
896 dev_err(&client
->dev
,
897 "Port%d: failed writing PCA9543 register\n", port
);
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)
908 dev_err(&client
->dev
,
909 "Port%d: failed reading SFP data\n", port
);
912 memcpy(data
+ i
, d
.block
+ 1, I2C_SMBUS_BLOCK_MAX
);
915 /* Validate checksums */
916 for (crc
= 0, i
= 0; i
< 63; 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
,
922 #ifdef FAIL_ON_CHECKSUM_ERR
926 for (crc
= 0, i
= 64; i
< 95; 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",
932 #ifdef FAIL_ON_CHECKSUM_ERR
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
]);
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
);
950 if ((sfp_msa
->identifier
!= GBIC
) &&
951 (sfp_msa
->identifier
!= SFF
) &&
952 (sfp_msa
->identifier
!= SFP
))
954 dev_err(&client
->dev
, "Port%d: Unknown module identifier: %d\n",
955 port
, sfp_msa
->identifier
);
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";
971 str
= "Unknown/Unsupported media type";
975 dev_err(&client
->dev
, "Port%d: %s (0x%02x)\n", port
, str
,
976 sfp_msa
->transceiver
[3]);
978 dev_info(&client
->dev
, "Port%d: %s (0x%02x)\n", port
, str
,
979 sfp_msa
->transceiver
[3]);
984 static int gw16083_probe(struct i2c_client
*client
,
985 const struct i2c_device_id
*id
)
989 dev_info(&client
->dev
, "GW16083 Ethernet Expansion Mezzanine\n");
990 if (gw16083_client
) {
991 dev_err(&client
->dev
, "client already registered\n");
994 gw16083_client
= client
;
996 ret
= phy_driver_register(&mv88e6176_phy_driver
);
998 dev_err(&client
->dev
,
999 "failed to register mv88e6176 phy driver: %d\n", ret
);
1003 static int gw16083_remove(struct i2c_client
*client
)
1005 dev_dbg(&client
->dev
, "%s\n", __func__
);
1007 phy_driver_unregister(&mv88e6176_phy_driver
);
1008 gw16083_client
= NULL
;
1012 static const struct of_device_id gw16083_dt_ids
[] = {
1013 { .compatible
= "gateworks,gw16083", },
1017 MODULE_DEVICE_TABLE(of
, gw16083_dt_ids
);
1019 static const struct i2c_device_id gw16083_id
[] = {
1023 MODULE_DEVICE_TABLE(i2c
, gw16083_id
);
1025 static struct i2c_driver gw16083_driver
= {
1028 .of_match_table
= gw16083_dt_ids
,
1030 .probe
= gw16083_probe
,
1031 .remove
= gw16083_remove
,
1032 .id_table
= gw16083_id
,
1035 static int __init
mv88e6176_init(void)
1037 return i2c_add_driver(&gw16083_driver
);
1040 static void __exit
mv88e6176_exit(void)
1042 i2c_del_driver(&gw16083_driver
);
1045 module_init(mv88e6176_init
);
1046 module_exit(mv88e6176_exit
);