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>
49 #include <linux/uaccess.h>
53 #undef FAIL_ON_CHECKSUM_ERR /* fail to configure SFP if checksum bad */
54 #define PORT_POWER_CONTROL /* ports can be enabled/disabled via sysfs */
55 #define PORT_MODE_CONTROL /* ports 5/6 can have SFP/RJ45 mode forced */
57 MODULE_DESCRIPTION("GW16083 driver");
58 MODULE_AUTHOR("Tim Harvey");
59 MODULE_LICENSE("GPL");
61 struct mv88e1111_port_state {
72 struct mv88e1111_priv {
73 struct phy_device *phydev;
74 struct i2c_client *client;
75 struct mv88e1111_port_state port5;
76 struct mv88e1111_port_state port6;
77 struct kobject *sysfs_kobj;
85 static struct i2c_client *gw16083_client = NULL;
87 static int gw16083_read_port_sfp(struct i2c_client *client,
88 struct mv88e1111_port_state *state);
90 /* read switch port register from port0-6 */
91 u16 read_switch_port(struct phy_device *pdev, int port, u8 regaddr)
93 return pdev->bus->read(pdev->bus, MV_BASE + port, regaddr);
96 /* write switch port register to port0-6 */
97 int write_switch_port(struct phy_device *pdev, int port, u8 regaddr, u16 val)
99 return pdev->bus->write(pdev->bus, MV_BASE + port, regaddr, val);
103 * read_switch_port_phy - write a register for a specific port on 88E6176
104 * The 88E6176 PHY registers must be accessed thorugh the Global2 address
105 * using the SMI_PHY_COMMAND_REG and SMI_PHY_DATA_REG.
107 int read_switch_port_phy(struct phy_device *pdev, int port, u8 regaddr)
112 dev_dbg(&pdev->dev, "read_phy: port%d reg=0x%02x\n", port, regaddr);
113 reg = SMIBUSY | SMIMODE22 | SMIOP_READ;
114 reg |= port << DEVADDR;
115 reg |= regaddr << REGADDR;
116 pdev->bus->write(pdev->bus, MV_GLOBAL2, MV_SMI_PHY_COMMAND, reg);
117 for (i = 0; i < 10; i++) {
118 reg = pdev->bus->read(pdev->bus, MV_GLOBAL2,
120 if (!(reg & (1<<15)))
127 reg = pdev->bus->read(pdev->bus, MV_GLOBAL2, MV_SMI_PHY_DATA);
132 * write_switch_port_phy - write a register for a specific port on 88E6176
133 * The 88E6176 PHY registers must be accessed thorugh the Global2 address
134 * using the SMI_PHY_COMMAND_REG and SMI_PHY_DATA_REG.
136 int write_switch_port_phy(struct phy_device *pdev, int port, u8 addr, u16 reg)
140 dev_dbg(&pdev->dev, "write_phy: port%d reg=0x%02x val=0x%04x\n", port,
142 pdev->bus->write(pdev->bus, MV_GLOBAL2, MV_SMI_PHY_DATA, reg);
143 reg = SMIBUSY | SMIMODE22 | SMIOP_WRITE;
144 reg |= port << DEVADDR;
145 reg |= addr << REGADDR;
146 pdev->bus->write(pdev->bus, MV_GLOBAL2, MV_SMI_PHY_COMMAND, reg);
147 for (i = 0; i < 10; i++) {
148 reg = pdev->bus->read(pdev->bus, MV_GLOBAL2,
150 if (!(reg & (1<<15)))
161 /* read a scratch register from switch */
162 inline u8 read_switch_scratch(struct phy_device *pdev, u8 reg)
164 pdev->bus->write(pdev->bus, MV_GLOBAL2, MV_SCRATCH_MISC, (reg << 8));
165 return pdev->bus->read(pdev->bus, MV_GLOBAL2, MV_SCRATCH_MISC) & 0xff;
168 /* write a scratch register to switch */
169 inline void write_switch_scratch(struct phy_device *pdev, u8 reg, u8 val)
171 pdev->bus->write(pdev->bus, MV_GLOBAL2, MV_SCRATCH_MISC,
172 (1 << 15) | (reg << 8) | val);
175 /* enable or disable an SFP's TXEN signal */
176 static int enable_sfp_txen(struct phy_device *pdev, int port, bool enable)
181 if (port != 5 && port != 6)
184 /* GPIO[2:1] output low to enable TXEN */
185 bit = (port == 5) ? 1 : 2;
186 gpio = read_switch_scratch(pdev, MV_GPIO_DATA);
191 write_switch_scratch(pdev, MV_GPIO_DATA, gpio);
192 dev_info(&pdev->dev, "Port%d: SFP TX %s\n", port, enable ?
193 "enabled" : "disabled");
197 /* configure mv88e1111 port for copper or serdes
198 * For Copper we set auto link/duplex/speed detection
199 * For SerDes/Fiber we force 1000mbps link up and auto-neg duplex
201 static int config_mv88e1111_port_sfp(struct phy_device *pdev, int port,
206 if (port != 5 && port != 6)
209 dev_dbg(&pdev->dev, "%s: Port%d %s\n", __func__, port,
210 sfp ? "SFP" : "copper");
212 enable_sfp_txen(pdev, port, 1);
214 /* configure MV88E6176 Physical Control Port Register */
216 "Port%d: SFP: force 1000mbps link up "
217 "(auto-negotiate duplex)\n",
219 reg = read_switch_port(pdev, port, MV_PORT_PHYS_CONTROL);
220 reg &= ~0x3f; /* clear 5-0 */
221 reg |= (1 << 4) | (1 << 5); /* force link up */
222 reg |= 2; /* force 1000mbps */
223 write_switch_port(pdev, port, MV_PORT_PHYS_CONTROL, reg);
224 reg = read_switch_port(pdev, port, MV_PORT_PHYS_CONTROL);
229 enable_sfp_txen(pdev, port, 0);
231 /* configure MV88E6176 Physical Control Port Register */
233 "Port%d: Copper: set auto-neg link/duplex/speed\n",
235 reg = read_switch_port(pdev, port, MV_PORT_PHYS_CONTROL);
236 reg &= ~0x3f; /* clear 5-0 */
237 reg |= 3; /* speed not forced */
238 write_switch_port(pdev, port, MV_PORT_PHYS_CONTROL, reg);
239 reg = read_switch_port(pdev, port, MV_PORT_PHYS_CONTROL);
241 dev_dbg(&pdev->dev, "%s: Port%d %s PORT_PHYS_CONTROL=0x%04x\n",
242 __func__, port, sfp ? "SFP" : "copper",
243 read_switch_port(pdev, port, MV_PORT_PHYS_CONTROL));
248 #if defined(PORT_POWER_CONTROL)
249 static int enable_switch_port(struct phy_device *pdev, int port, bool enable)
251 struct mv88e1111_priv *priv = dev_get_drvdata(&pdev->dev);
255 dev_info(&priv->client->dev, "Port%d: %s\n", port,
256 enable ? "normal operation" : "power down");
257 reg = read_switch_port_phy(pdev, port, MV_PHY_CONTROL);
259 reg &= ~(1 << 11); /* Normal Operation */
261 reg |= (1 << 11); /* power down */
262 write_switch_port_phy(pdev, port, MV_PHY_CONTROL, reg);
264 reg = read_switch_port_phy(pdev, port, MV_PHY_CONTROL1);
266 reg &= ~(1 << 2); /* Normal Operation */
268 reg |= (1 << 2); /* power down */
269 write_switch_port_phy(pdev, port, MV_PHY_CONTROL1, reg);
279 struct mv88e1111_port_state *get_port_state(struct mv88e1111_priv *priv,
290 * get MV88E6176 port number for a specific GW16083 port name
291 * The GW16083 ports as shown on the silkscreen are not mapped according to
292 * the MV88E6176 ports numbers.
294 static int gw16083_get_port(const char* name)
297 int map[] = { 3, 2, 1, 0, 5, 6 };
299 if (strncasecmp(name, "ETHERNET", 8) != 0 || strlen(name) != 9)
307 static ssize_t port_show(struct device *dev, struct device_attribute *attr,
310 struct mv88e1111_priv *priv = dev_get_drvdata(dev);
314 if (sscanf(attr->attr.name, "port%d", &port) != 1)
316 if (port < 0 || port > 6)
318 reg = read_switch_port_phy(priv->phydev, port, MV_PHY_CONTROL);
319 return sprintf(buf, "%s\n", (reg & (1 << 11)) ? "disabled" : "enabled");
322 #if defined(PORT_POWER_CONTROL)
323 static ssize_t port_store(struct device *dev, struct device_attribute *attr,
324 const char *buf, size_t count)
326 struct mv88e1111_priv *priv = dev_get_drvdata(dev);
330 port = gw16083_get_port(attr->attr.name);
333 if (sscanf(buf, "%d", &val) != 1)
335 enable_switch_port(priv->phydev, port, val ? 1 : 0);
339 static DEVICE_ATTR(ethernet1, S_IWUSR | S_IRUGO, port_show, port_store);
340 static DEVICE_ATTR(ethernet2, S_IWUSR | S_IRUGO, port_show, port_store);
341 static DEVICE_ATTR(ethernet3, S_IWUSR | S_IRUGO, port_show, port_store);
342 static DEVICE_ATTR(ethernet4, S_IWUSR | S_IRUGO, port_show, port_store);
343 static DEVICE_ATTR(ethernet5, S_IWUSR | S_IRUGO, port_show, port_store);
344 static DEVICE_ATTR(ethernet6, S_IWUSR | S_IRUGO, port_show, port_store);
346 static DEVICE_ATTR(ethernet1, S_IRUGO, port_show, NULL);
347 static DEVICE_ATTR(ethernet2, S_IRUGO, port_show, NULL);
348 static DEVICE_ATTR(ethernet3, S_IRUGO, port_show, NULL);
349 static DEVICE_ATTR(ethernet4, S_IRUGO, port_show, NULL);
350 static DEVICE_ATTR(ethernet5, S_IRUGO, port_show, NULL);
351 static DEVICE_ATTR(ethernet6, S_IRUGO, port_show, NULL);
354 static ssize_t portsfp_show(struct device *dev, struct device_attribute *attr,
357 struct mv88e1111_priv *priv = dev_get_drvdata(dev);
358 struct mv88e1111_port_state *state;
360 state = get_port_state(priv, gw16083_get_port(attr->attr.name));
364 if (!state->sfp_present)
367 return sprintf(buf, "%s\n", state->sfp_id);
370 static ssize_t portmode_show(struct device *dev, struct device_attribute *attr,
373 struct mv88e1111_priv *priv = dev_get_drvdata(dev);
374 struct mv88e1111_port_state *state;
376 state = get_port_state(priv, gw16083_get_port(attr->attr.name));
380 return sprintf(buf, "%s\n", state->serdes ? "SFP" : "RJ45");
382 static DEVICE_ATTR(ethernet5_sfp, S_IRUGO, portsfp_show, NULL);
383 static DEVICE_ATTR(ethernet6_sfp, S_IRUGO, portsfp_show, NULL);
385 #ifdef PORT_MODE_CONTROL
386 static ssize_t portmode_store(struct device *dev, struct device_attribute *attr,
387 const char *buf, size_t count)
389 struct mv88e1111_priv *priv = dev_get_drvdata(dev);
390 struct mv88e1111_port_state *state;
394 port = gw16083_get_port(attr->attr.name);
395 state = get_port_state(priv, port);
399 reg = read_switch_port_phy(priv->phydev, port, MII_M1111_PHY_EXT_SR);
400 if (strcasecmp(buf, "auto") == 0) {
401 reg &= ~(1<<15); /* enable auto-selection */
402 dev_info(&priv->client->dev, "Port%d: enable auto-selection\n",
404 } else if (strcasecmp(buf, "RJ45") == 0) {
405 reg |= (1<<15); /* disable auto-selection */
406 reg |= 0xb; /* RGMII to Copper */
407 config_mv88e1111_port_sfp(priv->phydev, port, 0);
408 dev_info(&priv->client->dev, "Port%d: select RJ45\n", port);
409 } else if (strcasecmp(buf, "SFP") == 0) {
410 reg |= (1<<15); /* disable auto-selection */
411 reg |= 0x3; /* RGMII to Fiber */
412 config_mv88e1111_port_sfp(priv->phydev, port, 1);
413 dev_info(&priv->client->dev, "Port%d: select SFP\n", port);
415 write_switch_port_phy(priv->phydev, port, MII_M1111_PHY_EXT_SR, reg);
420 static DEVICE_ATTR(ethernet5_mode, S_IWUSR | S_IRUGO, portmode_show,
422 static DEVICE_ATTR(ethernet6_mode, S_IWUSR | S_IRUGO, portmode_show,
425 static DEVICE_ATTR(ethernet5_mode, S_IRUGO, portmode_show, NULL);
426 static DEVICE_ATTR(ethernet6_mode, S_IRUGO, portmode_show, NULL);
435 mv88e6176_config_init(struct phy_device *pdev)
437 dev_dbg(&pdev->dev, "%s\n", __func__);
438 pdev->state = PHY_RUNNING;
443 /* check MV88E1111 PHY status and MV88E6176 GPIO */
445 mv88e6176_read_status(struct phy_device *pdev)
447 struct mv88e1111_priv *priv = dev_get_drvdata(&pdev->dev);
448 struct mv88e1111_port_state *state;
449 bool serdes, sfp_present, sfp_signal;
454 dev_dbg(&pdev->dev, "%s", __func__);
455 gpio = read_switch_scratch(pdev, MV_GPIO_DATA);
456 for (port = 5; port < 7; port++) {
457 serdes = (read_switch_port_phy(pdev, port, MII_M1111_PHY_EXT_SR)
459 dev_dbg(&pdev->dev, "%s: Port%d GPIO:0x%02x SerDes:%d\n",
460 __func__, port, gpio, serdes);
463 state = &priv->port5;
464 sfp_present = !((gpio >> 5) & 1);
465 sfp_signal = !((gpio >> 6) & 1);
468 state = &priv->port6;
469 sfp_present = !((gpio >> 3) & 1);
470 sfp_signal = !((gpio >> 4) & 1);
475 * on sfp_detect read/verify SFP MSA and set sfp_compat
476 * on sfp_signal issue link down?
477 * on serdes auto-select
479 if (state->sfp_present != sfp_present) {
480 state->sfp_present = sfp_present;
481 dev_info(&pdev->dev, "Port%d: SFP %s\n",
482 port, sfp_present ? "inserted" : "removed");
483 if (state->sfp_present) {
484 if (gw16083_read_port_sfp(priv->client, state))
485 state->sfp_compat = false;
487 state->sfp_compat = true;
489 state->sfp_compat = false;
490 state->sfp_enabled = false;
493 if (state->sfp_signal != sfp_signal) {
494 state->sfp_signal = sfp_signal;
495 dev_info(&pdev->dev, "Port%d: SFP signal %s\n",
496 port, sfp_signal ? "detected" : "lost");
498 if (state->serdes != serdes) {
499 state->serdes = serdes;
500 dev_info(&pdev->dev, "Port%d: %s auto-selected\n",
501 port, serdes ? "SERDES" : "copper");
504 * if auto-selection has switched to copper
508 config_mv88e1111_port_sfp(pdev, port, 0);
509 state->sfp_enabled = false;
514 * if serdes and compatible SFP module and not yet enabled
515 * then enable for serdes
517 if (serdes && state->sfp_compat && state->sfp_signal &&
520 if (!config_mv88e1111_port_sfp(pdev, port, 1))
521 state->sfp_enabled = true;
529 mv88e6176_config_aneg(struct phy_device *pdev)
531 dev_dbg(&pdev->dev, "%s", __func__);
536 mv88e6176_remove(struct phy_device *pdev)
538 dev_dbg(&pdev->dev, "%s", __func__);
540 device_remove_file(&pdev->dev, &dev_attr_ethernet1);
541 device_remove_file(&pdev->dev, &dev_attr_ethernet2);
542 device_remove_file(&pdev->dev, &dev_attr_ethernet3);
543 device_remove_file(&pdev->dev, &dev_attr_ethernet4);
544 device_remove_file(&pdev->dev, &dev_attr_ethernet5);
545 device_remove_file(&pdev->dev, &dev_attr_ethernet6);
546 device_remove_file(&pdev->dev, &dev_attr_ethernet5_sfp);
547 device_remove_file(&pdev->dev, &dev_attr_ethernet6_sfp);
548 device_remove_file(&pdev->dev, &dev_attr_ethernet5_mode);
549 device_remove_file(&pdev->dev, &dev_attr_ethernet6_mode);
550 sysfs_remove_link(kernel_kobj, "gw16083");
554 mv88e6176_probe(struct phy_device *pdev)
559 struct mv88e1111_priv *priv;
561 dev_dbg(&pdev->dev, "%s: addr=0x%02x bus=%s:%s gw16083_client=%p\n",
562 __func__, pdev->addr, pdev->bus->name, pdev->bus->id,
565 /* In single-chip addressing mode the MV88E6176 shows up on 0x10-0x16 */
566 if (pdev->addr != MV_BASE)
569 /* i2c driver needs to be loaded first */
573 /* gw16083 has MV88E1676 hanging off of i210 mdio bus */
574 if (strcmp(pdev->bus->name, "igb_enet_mii_bus") != 0)
577 //dev_info(&pdev->dev, "Detected");
578 dev_info(&gw16083_client->dev, "%s: MV88E6176 7-port switch detected",
582 * port5/6 config: MV88E1111 PHY
583 * Register 20: PHY Control Register
584 * R20_7: add delay to RX_CLK for RXD
585 * R20_1: add delay to TX_CLK for TXD
586 * Register 24: LED Control Register
588 * Pulse stretch 170 to 340 ms
589 * Register 0: Control Register
592 for (port = 5; port < 7; port++) {
593 #ifndef RGMII_DELAY_ON_PHY
594 write_switch_port(pdev, port, MV_PORT_PHYS_CONTROL, 0xC003);
597 id = read_switch_port_phy(pdev, port,
598 MII_M1111_PHY_IDENT0) << 16;
599 id |= read_switch_port_phy(pdev, port, MII_M1111_PHY_IDENT1);
600 if ((id & MII_M1111_PHY_ID_MASK) != MII_M1111_PHY_ID) {
601 dev_err(&gw16083_client->dev,
602 "Port%d: No MV88E1111 PHY detected", port);
607 #ifdef RGMII_DELAY_ON_PHY
608 /* phy rx/tx delay */
609 reg = read_switch_port_phy(pdev, port, MII_M1111_PHY_EXT_CR);
610 reg |= (1<<1) | (1<<7);
611 write_switch_port_phy(pdev, port, MII_M1111_PHY_EXT_CR, reg);
614 write_switch_port_phy(pdev, port, MII_M1111_PHY_LED_CONTROL,
615 MII_M1111_PHY_LED_PULSE_STR);
617 reg = read_switch_port_phy(pdev, port, MII_M1111_PHY_CONTROL);
618 reg |= MII_M1111_PHY_CONTROL_RESET;
619 write_switch_port_phy(pdev, port, MII_M1111_PHY_CONTROL, reg);
620 dev_info(&gw16083_client->dev,
621 "Port%d MV88E111 PHY configured\n", port);
625 * GPIO Configuration:
626 * GPIO1: FIB5_TXEN# (output)
627 * GPIO2: FIB6_TXEN# (output)
628 * GPIO3: FIB6_PRES# (input)
629 * GPIO4: FIB6_LOS (input)
630 * GPIO5: FIB5_PRES# (input)
631 * GPIO6: FIB5_LOS (input)
633 write_switch_scratch(pdev, MV_GPIO_DATA, 0x06); /* GPIO[2:1] out hi */
634 write_switch_scratch(pdev, MV_GPIO_DIR, 0x78); /* GPIO[6:3] inp */
636 pdev->irq = PHY_POLL;
638 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
641 memset(priv, 0, sizeof(*priv));
643 priv->client = gw16083_client;
644 priv->port5.port = 5;
645 priv->port6.port = 6;
646 dev_set_drvdata(&pdev->dev, priv);
648 ret |= device_create_file(&pdev->dev, &dev_attr_ethernet1);
649 ret |= device_create_file(&pdev->dev, &dev_attr_ethernet2);
650 ret |= device_create_file(&pdev->dev, &dev_attr_ethernet3);
651 ret |= device_create_file(&pdev->dev, &dev_attr_ethernet4);
652 ret |= device_create_file(&pdev->dev, &dev_attr_ethernet5);
653 ret |= device_create_file(&pdev->dev, &dev_attr_ethernet6);
654 ret |= device_create_file(&pdev->dev, &dev_attr_ethernet5_sfp);
655 ret |= device_create_file(&pdev->dev, &dev_attr_ethernet6_sfp);
656 ret |= device_create_file(&pdev->dev, &dev_attr_ethernet5_mode);
657 ret |= device_create_file(&pdev->dev, &dev_attr_ethernet6_mode);
660 dev_err(&pdev->dev, "Failed creating attrs\n");
662 /* Add a nice symlink to the real device */
663 ret = sysfs_create_link(kernel_kobj, &pdev->dev.kobj, "gw16083");
665 dev_dbg(&pdev->dev, "initial state: GPIO=0x%02x "
666 "Port5_serdes=%d Port6_serdes=%d\n",
667 read_switch_scratch(pdev, MV_GPIO_DATA),
668 (read_switch_port_phy(pdev, 5, MII_M1111_PHY_EXT_SR)
670 (read_switch_port_phy(pdev, 6, MII_M1111_PHY_EXT_SR)
676 static struct phy_driver mv88e6176_phy_driver = {
678 .phy_id = MV_IDENT_VALUE,
679 .phy_id_mask = MV_IDENT_MASK,
680 .features = PHY_BASIC_FEATURES,
681 .probe = &mv88e6176_probe,
682 .remove = &mv88e6176_remove,
683 .config_init = &mv88e6176_config_init,
684 .config_aneg = &mv88e6176_config_aneg,
685 .read_status = &mv88e6176_read_status,
686 .driver = { .owner = THIS_MODULE },
695 /* Basic ID fields */
718 /* extended id fields */
726 u8 sff8472_compliance;
729 /* Vendor specific ID fields */
751 const char* id_names[] = {
759 /* Flags for SFP modules compatible with ETH up to 1Gb */
771 #define STRING_APPEND(str, src) \
772 strncat(str, src, sizeof(src)); \
773 for (i = 1; i < sizeof(str); i++) \
774 if (str[i-1] == ' ' && str[i] == ' ') \
777 static int gw16083_read_port_sfp(struct i2c_client *client,
778 struct mv88e1111_port_state *state)
782 struct sfp_flags *eth_flags;
786 struct sfp_msa *sfp_msa = (struct sfp_msa *)data;
787 int port = state->port;
788 union i2c_smbus_data d;
790 dev_dbg(&client->dev, "%s Port%d\n", __func__, port);
791 if (!i2c_check_functionality(client->adapter,
792 I2C_FUNC_SMBUS_READ_I2C_BLOCK))
794 d.byte = (port == 5) ? 1 : 2;
795 if (i2c_smbus_xfer(client->adapter, GW16083_I2C_ADDR_PCA9543,
796 client->flags, I2C_SMBUS_WRITE, 0,
797 I2C_SMBUS_BYTE_DATA, &d) < 0)
799 dev_err(&client->dev,
800 "Port%d: failed writing PCA9543 register\n", port);
804 /* read all 256 bytes of SFP EEPROM */
805 for (i = 0; i < sizeof(data); i += I2C_SMBUS_BLOCK_MAX) {
806 d.block[0] = I2C_SMBUS_BLOCK_MAX;
807 if (i2c_smbus_xfer(client->adapter, GW16083_I2C_ADDR_SFP1,
808 client->flags, I2C_SMBUS_READ, i,
809 I2C_SMBUS_I2C_BLOCK_DATA, &d) < 0)
811 dev_err(&client->dev,
812 "Port%d: failed reading SFP data\n", port);
815 memcpy(data + i, d.block + 1, I2C_SMBUS_BLOCK_MAX);
818 /* Validate checksums */
819 for (crc = 0, i = 0; i < 63; i++)
821 if (crc != sfp_msa->cc_base) {
822 dev_err(&client->dev, "Port%d: "
823 "Checksum failure for Base ID fields: 0x%02x\n", port,
825 #ifdef FAIL_ON_CHECKSUM_ERR
829 for (crc = 0, i = 64; i < 95; i++)
831 if (crc != sfp_msa->cc_ext) {
832 dev_err(&client->dev, "Port%d: "
833 "Checksum failure for Extended ID fields: 0x%02x\n",
835 #ifdef FAIL_ON_CHECKSUM_ERR
839 state->sfp_id[0] = 0;
840 for (i = 0; id_names[i]; i++) {
841 if (sfp_msa->identifier == i) {
842 sprintf(state->sfp_id, "%s: ", id_names[i]);
846 STRING_APPEND(state->sfp_id, sfp_msa->vendor_oui);
847 STRING_APPEND(state->sfp_id, sfp_msa->vendor_name);
848 STRING_APPEND(state->sfp_id, sfp_msa->vendor_pn);
849 STRING_APPEND(state->sfp_id, sfp_msa->vendor_rev);
850 STRING_APPEND(state->sfp_id, sfp_msa->vendor_sn);
851 dev_info(&client->dev, "Port%d: %s\n", port, state->sfp_id);
853 if ((sfp_msa->identifier != GBIC) &&
854 (sfp_msa->identifier != SFF) &&
855 (sfp_msa->identifier != SFP))
857 dev_err(&client->dev, "Port%d: Unknown module identifier: %d\n",
858 port, sfp_msa->identifier);
863 eth_flags = (struct sfp_flags *)(sfp_msa->transceiver + 3);
864 if (eth_flags->e1000_base_sx) {
865 str = "1000Base-SX (Fiber)";
866 } else if (eth_flags->e1000_base_lx) {
867 str = "1000Base-LX (Fiber)";
868 } else if (eth_flags->e1000_base_t) {
869 str = "1000Base-T (Copper)";
870 } else if (eth_flags->e100_base_fx) {
871 str = "100Base-FX (Fiber) - not supported";
874 str = "Unknown/Unsupported media type";
878 dev_err(&client->dev, "Port%d: %s (0x%02x)\n", port, str,
879 sfp_msa->transceiver[3]);
881 dev_info(&client->dev, "Port%d: %s (0x%02x)\n", port, str,
882 sfp_msa->transceiver[3]);
887 static int gw16083_probe(struct i2c_client *client,
888 const struct i2c_device_id *id)
892 dev_info(&client->dev, "GW16083 Ethernet Expansion Mezzanine\n");
893 if (gw16083_client) {
894 dev_err(&client->dev, "client already registered\n");
897 gw16083_client = client;
899 ret = phy_driver_register(&mv88e6176_phy_driver);
901 dev_err(&client->dev,
902 "failed to register mv88e6176 phy driver: %d\n", ret);
906 static int gw16083_remove(struct i2c_client *client)
908 dev_dbg(&client->dev, "%s\n", __func__);
910 phy_driver_unregister(&mv88e6176_phy_driver);
911 gw16083_client = NULL;
915 static const struct of_device_id gw16083_dt_ids[] = {
916 { .compatible = "gateworks,gw16083", },
920 MODULE_DEVICE_TABLE(of, gw16083_dt_ids);
922 static const struct i2c_device_id gw16083_id[] = {
926 MODULE_DEVICE_TABLE(i2c, gw16083_id);
928 static struct i2c_driver gw16083_driver = {
931 .of_match_table = gw16083_dt_ids,
933 .probe = gw16083_probe,
934 .remove = gw16083_remove,
935 .id_table = gw16083_id,
938 static int __init mv88e6176_init(void)
940 return i2c_add_driver(&gw16083_driver);
943 static void __exit mv88e6176_exit(void)
945 i2c_del_driver(&gw16083_driver);
948 module_init(mv88e6176_init);
949 module_exit(mv88e6176_exit);