66b5536da273cafe71596762a8fd6990d4af9156
[openwrt.git] / target / linux / imx6 / files-3.14 / 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
47 #include <linux/io.h>
48 #include <asm/irq.h>
49 #include <linux/uaccess.h>
50
51 #include "gw16083.h"
52
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 */
56
57 MODULE_DESCRIPTION("GW16083 driver");
58 MODULE_AUTHOR("Tim Harvey");
59 MODULE_LICENSE("GPL");
60
61 struct mv88e1111_port_state {
62         int port;
63         bool present;
64         bool serdes;
65         bool sfp_signal;
66         bool sfp_present;
67         bool sfp_compat;
68         bool sfp_enabled;
69         char sfp_id[64];
70 };
71
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;
78 };
79
80 enum {
81         mode_copper = 0,
82         mode_serdes = 1,
83 };
84
85 static struct i2c_client *gw16083_client = NULL;
86
87 static int gw16083_read_port_sfp(struct i2c_client *client,
88                                  struct mv88e1111_port_state *state);
89
90 /* read switch port register from port0-6 */
91 u16 read_switch_port(struct phy_device *pdev, int port, u8 regaddr)
92 {
93         return pdev->bus->read(pdev->bus, MV_BASE + port, regaddr);
94 }
95
96 /* write switch port register to port0-6 */
97 int write_switch_port(struct phy_device *pdev, int port, u8 regaddr, u16 val)
98 {
99         return pdev->bus->write(pdev->bus, MV_BASE + port, regaddr, val);
100 }
101
102 /*
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.
106  */
107 int read_switch_port_phy(struct phy_device *pdev, int port, u8 regaddr)
108 {
109         u16 reg;
110         int i;
111
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,
119                                       MV_SMI_PHY_COMMAND);
120                 if (!(reg & (1<<15)))
121                         break;
122                 mdelay(1);
123         }
124         /* timeout */
125         if (i == 10)
126                 return 0xffff;
127         reg = pdev->bus->read(pdev->bus, MV_GLOBAL2, MV_SMI_PHY_DATA);
128         return reg;
129 }
130
131 /*
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.
135  */
136 int write_switch_port_phy(struct phy_device *pdev, int port, u8 addr, u16 reg)
137 {
138         int i;
139
140         dev_dbg(&pdev->dev, "write_phy: port%d reg=0x%02x val=0x%04x\n", port,
141                 addr, reg);
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,
149                                       MV_SMI_PHY_COMMAND);
150                 if (!(reg & (1<<15)))
151                         break;
152                 mdelay(1);
153         }
154         /* timeout */
155         if (i == 10)
156                 return -ETIMEDOUT;
157
158         return 0;
159 }
160
161 /* read a scratch register from switch */
162 inline u8 read_switch_scratch(struct phy_device *pdev, u8 reg)
163 {
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;
166 }
167
168 /* write a scratch register to switch */
169 inline void write_switch_scratch(struct phy_device *pdev, u8 reg, u8 val)
170 {
171         pdev->bus->write(pdev->bus, MV_GLOBAL2, MV_SCRATCH_MISC,
172                          (1 << 15) | (reg << 8) | val);
173 }
174
175 /* enable or disable an SFP's TXEN signal */
176 static int enable_sfp_txen(struct phy_device *pdev, int port, bool enable)
177 {
178         u8 gpio;
179         int bit;
180
181         if (port != 5 && port != 6)
182                 return -EINVAL;
183
184         /* GPIO[2:1] output low to enable TXEN */
185         bit = (port == 5) ? 1 : 2;
186         gpio = read_switch_scratch(pdev, MV_GPIO_DATA);
187         if (enable)
188                 gpio |= (1 << bit);
189         else
190                 gpio &= (1 << bit);
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");
194         return 0;
195 }
196
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
200  */
201 static int config_mv88e1111_port_sfp(struct phy_device *pdev, int port,
202                                      bool sfp)
203 {
204         u16 reg;
205
206         if (port != 5 && port != 6)
207                 return -EINVAL;
208
209         dev_dbg(&pdev->dev, "%s: Port%d %s\n", __func__, port,
210                 sfp ? "SFP" : "copper");
211         if (sfp) {
212                 enable_sfp_txen(pdev, port, 1);
213
214                 /* configure MV88E6176 Physical Control Port Register */
215                 dev_info(&pdev->dev,
216                          "Port%d: SFP: force 1000mbps link up "
217                          "(auto-negotiate duplex)\n",
218                          port);
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);
225         }
226
227         /* copper */
228         else {
229                 enable_sfp_txen(pdev, port, 0);
230
231                 /* configure MV88E6176 Physical Control Port Register */
232                 dev_info(&pdev->dev,
233                          "Port%d: Copper: set auto-neg link/duplex/speed\n",
234                          port);
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);
240         }
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));
244
245         return 0;
246 }
247
248 #if defined(PORT_POWER_CONTROL)
249 static int enable_switch_port(struct phy_device *pdev, int port, bool enable)
250 {
251         struct mv88e1111_priv *priv = dev_get_drvdata(&pdev->dev);
252         u16 reg;
253
254         /* power up port */
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);
258         if (enable)
259                 reg &= ~(1 << 11);      /* Normal Operation */
260         else
261                 reg |= (1 << 11);       /* power down */
262         write_switch_port_phy(pdev, port, MV_PHY_CONTROL, reg);
263
264         reg = read_switch_port_phy(pdev, port, MV_PHY_CONTROL1);
265         if (enable)
266                 reg &= ~(1 << 2);       /* Normal Operation */
267         else
268                 reg |= (1 << 2);        /* power down */
269         write_switch_port_phy(pdev, port, MV_PHY_CONTROL1, reg);
270
271         return 0;
272 }
273 #endif
274
275 /*
276  * Sysfs API
277  */
278
279 struct mv88e1111_port_state *get_port_state(struct mv88e1111_priv *priv,
280                                             int port)
281 {
282         if (port == 5)
283                 return &priv->port5;
284         if (port == 6)
285                 return &priv->port6;
286         return NULL;
287 }
288
289 /*
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.
293  */
294 static int gw16083_get_port(const char* name)
295 {
296         int i;
297         int map[] = { 3, 2, 1, 0, 5, 6 };
298
299         if (strncasecmp(name, "ETHERNET", 8) != 0 || strlen(name) != 9)
300                 return -1;
301         i = name[8] - '0';
302         if (i < 1 || i > 6)
303                 return -1;
304         return map[i-1];
305 }
306
307 static ssize_t port_show(struct device *dev, struct device_attribute *attr,
308                          char *buf)
309 {
310         struct mv88e1111_priv *priv = dev_get_drvdata(dev);
311         int port = -1;
312         u16 reg;
313
314         if (sscanf(attr->attr.name, "port%d", &port) != 1)
315                 return 0;
316         if (port < 0 || port > 6)
317                 return 0;
318         reg = read_switch_port_phy(priv->phydev, port, MV_PHY_CONTROL);
319         return sprintf(buf, "%s\n", (reg & (1 << 11)) ? "disabled" : "enabled");
320 }
321
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)
325 {
326         struct mv88e1111_priv *priv = dev_get_drvdata(dev);
327         int port = -1;
328         int val;
329
330         port = gw16083_get_port(attr->attr.name);
331         if (port < 0)
332                 return 0;
333         if (sscanf(buf, "%d", &val) != 1)
334                 return 0;
335         enable_switch_port(priv->phydev, port, val ? 1 : 0);
336         return count;
337 }
338
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);
345 #else
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);
352 #endif
353
354 static ssize_t portsfp_show(struct device *dev, struct device_attribute *attr,
355                              char *buf)
356 {
357         struct mv88e1111_priv *priv = dev_get_drvdata(dev);
358         struct mv88e1111_port_state *state;
359
360         state = get_port_state(priv, gw16083_get_port(attr->attr.name));
361         if (!state)
362                 return 0;
363
364         if (!state->sfp_present)
365                 return 0;
366
367         return sprintf(buf, "%s\n", state->sfp_id);
368 }
369
370 static ssize_t portmode_show(struct device *dev, struct device_attribute *attr,
371                              char *buf)
372 {
373         struct mv88e1111_priv *priv = dev_get_drvdata(dev);
374         struct mv88e1111_port_state *state;
375
376         state = get_port_state(priv, gw16083_get_port(attr->attr.name));
377         if (!state)
378                 return 0;
379
380         return sprintf(buf, "%s\n", state->serdes ? "SFP" : "RJ45");
381 }
382 static DEVICE_ATTR(ethernet5_sfp, S_IRUGO, portsfp_show, NULL);
383 static DEVICE_ATTR(ethernet6_sfp, S_IRUGO, portsfp_show, NULL);
384
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)
388 {
389         struct mv88e1111_priv *priv = dev_get_drvdata(dev);
390         struct mv88e1111_port_state *state;
391         u16 reg;
392         int port;
393
394         port = gw16083_get_port(attr->attr.name);
395         state = get_port_state(priv, port);
396         if (!state)
397                 return 0;
398
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",
403                          port);
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);
414         }
415         write_switch_port_phy(priv->phydev, port, MII_M1111_PHY_EXT_SR, reg);
416
417         return count;
418 }
419
420 static DEVICE_ATTR(ethernet5_mode, S_IWUSR | S_IRUGO, portmode_show,
421                    portmode_store);
422 static DEVICE_ATTR(ethernet6_mode, S_IWUSR | S_IRUGO, portmode_show,
423                    portmode_store);
424 #else
425 static DEVICE_ATTR(ethernet5_mode, S_IRUGO, portmode_show, NULL);
426 static DEVICE_ATTR(ethernet6_mode, S_IRUGO, portmode_show, NULL);
427 #endif
428
429
430 /*
431  * PHY driver
432  */
433
434 static int
435 mv88e6176_config_init(struct phy_device *pdev)
436 {
437         dev_dbg(&pdev->dev, "%s\n", __func__);
438         pdev->state = PHY_RUNNING;
439
440         return 0;
441 }
442
443 /* check MV88E1111 PHY status and MV88E6176 GPIO */
444 static int
445 mv88e6176_read_status(struct phy_device *pdev)
446 {
447         struct mv88e1111_priv *priv = dev_get_drvdata(&pdev->dev);
448         struct mv88e1111_port_state *state;
449         bool serdes, sfp_present, sfp_signal;
450         int port;
451         int ret = 0;
452         u16 gpio;
453
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)
458                          & (1<<13)) ? 1 : 0;
459                 dev_dbg(&pdev->dev, "%s: Port%d GPIO:0x%02x SerDes:%d\n",
460                         __func__, port, gpio, serdes);
461                 switch(port) {
462                 case 5:
463                         state = &priv->port5;
464                         sfp_present = !((gpio >> 5) & 1);
465                         sfp_signal = !((gpio >> 6) & 1);
466                         break;
467                 case 6:
468                         state = &priv->port6;
469                         sfp_present = !((gpio >> 3) & 1);
470                         sfp_signal = !((gpio >> 4) & 1);
471                         break;
472                 }
473
474                 /*
475                  * on sfp_detect read/verify SFP MSA and set sfp_compat
476                  * on sfp_signal issue link down?
477                  * on serdes auto-select
478                  */
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;
486                                 else
487                                         state->sfp_compat = true;
488                         } else {
489                                 state->sfp_compat = false;
490                                 state->sfp_enabled = false;
491                         }
492                 }
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");
497                 }
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");
502
503                         /*
504                          * if auto-selection has switched to copper
505                          * disable serdes
506                          */
507                         if (!serdes) {
508                                 config_mv88e1111_port_sfp(pdev, port, 0);
509                                 state->sfp_enabled = false;
510                         }
511                 }
512
513                 /*
514                  * if serdes and compatible SFP module and not yet enabled
515                  * then enable for serdes
516                  */
517                 if (serdes && state->sfp_compat && state->sfp_signal &&
518                     !state->sfp_enabled)
519                 {
520                         if (!config_mv88e1111_port_sfp(pdev, port, 1))
521                                 state->sfp_enabled = true;
522                 }
523         }
524
525         return ret;
526 }
527
528 static int
529 mv88e6176_config_aneg(struct phy_device *pdev)
530 {
531         dev_dbg(&pdev->dev, "%s", __func__);
532         return 0;
533 }
534
535 static void
536 mv88e6176_remove(struct phy_device *pdev)
537 {
538         dev_dbg(&pdev->dev, "%s", __func__);
539
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");
551 }
552
553 static int
554 mv88e6176_probe(struct phy_device *pdev)
555 {
556         int port;
557         int ret = 0;
558         u32 id, reg;
559         struct mv88e1111_priv *priv;
560
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,
563                 gw16083_client);
564
565         /* In single-chip addressing mode the MV88E6176 shows up on 0x10-0x16 */
566         if (pdev->addr != MV_BASE)
567                 return 0;
568
569         /* i2c driver needs to be loaded first */
570         if (!gw16083_client)
571                 return 0;
572
573         /* gw16083 has MV88E1676 hanging off of i210 mdio bus */
574         if (strcmp(pdev->bus->name, "igb_enet_mii_bus") != 0)
575                 return 0;
576
577         //dev_info(&pdev->dev, "Detected");
578         dev_info(&gw16083_client->dev, "%s: MV88E6176 7-port switch detected",
579                  pdev->bus->id);
580
581         /*
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
587          *   0x4111:
588          *     Pulse stretch 170 to 340 ms
589          * Register 0: Control Register
590          *   R0_15: phy reset
591          */
592         for (port = 5; port < 7; port++) {
593 #ifndef RGMII_DELAY_ON_PHY
594                 write_switch_port(pdev, port, MV_PORT_PHYS_CONTROL, 0xC003);
595 #endif
596
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);
603                         return 0;
604                         //continue;
605                 }
606
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);
612 #endif
613                 /* led config */
614                 write_switch_port_phy(pdev, port, MII_M1111_PHY_LED_CONTROL,
615                                       MII_M1111_PHY_LED_PULSE_STR);
616                 /* reset phy */
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);
622         }
623
624         /*
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)
632          */
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 */
635
636         pdev->irq = PHY_POLL;
637
638         priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
639         if (!priv)
640                 return -ENOMEM;
641         memset(priv, 0, sizeof(*priv));
642         priv->phydev = pdev;
643         priv->client = gw16083_client;
644         priv->port5.port = 5;
645         priv->port6.port = 6;
646         dev_set_drvdata(&pdev->dev, priv);
647
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);
658
659         if (unlikely(ret))
660                 dev_err(&pdev->dev, "Failed creating attrs\n");
661
662         /* Add a nice symlink to the real device */
663         ret = sysfs_create_link(kernel_kobj, &pdev->dev.kobj, "gw16083");
664
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)
669                  & (1<<13) ? 1:0),
670                 (read_switch_port_phy(pdev, 6, MII_M1111_PHY_EXT_SR)
671                  & (1<<13) ? 1:0));
672
673         return ret;
674 }
675
676 static struct phy_driver mv88e6176_phy_driver = {
677         .name           = "MV88E6176",
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 },
687 };
688
689 /*
690  * I2C driver
691  */
692
693 /* See SFF-8472 */
694 struct sfp_msa {
695         /* Basic ID fields */
696         u8      identifier;
697         u8      ext_identifier;
698         u8      connector;
699         u8      transceiver[8];
700         u8      encoding;
701         u8      br_nominal;
702         u8      rate_identifier;
703         u8      length_smf_km;
704         u8      length_smf;
705         u8      length_om2;
706         u8      length_om1;
707         u8      length_om4;
708         u8      length_om3;
709         u8      vendor_name[16];
710         u8      transceiver2;
711         u8      vendor_oui[3];
712         u8      vendor_pn[16];
713         u8      vendor_rev[4];
714         u8      wavelength[2];
715         u8      resv1;
716         u8      cc_base;
717
718         /* extended id fields */
719         u8      options[2];
720         u8      br_max;
721         u8      br_min;
722         u8      vendor_sn[16];
723         u8      date_code[8];
724         u8      diags_type;
725         u8      enhanced_options;
726         u8      sff8472_compliance;
727         u8      cc_ext;
728
729         /* Vendor specific ID fields */
730         u8      vendor_data[32];
731         u8      sff8079[128];
732 };
733
734 enum identifier {
735         UNKNOWN,
736         GBIC,
737         SFF,
738         SFP,
739         XBI,
740         XENPACK,
741         XFP,
742         XFF,
743         XFP_E,
744         XPAK,
745         X2,
746         DWDM_SFP,
747         QSFP,
748         MAX_ID,
749 };
750
751 const char* id_names[] = {
752         "UNKONWN",
753         "GBIC",
754         "SFF",
755         "SFP",
756         NULL,
757 };
758
759 /* Flags for SFP modules compatible with ETH up to 1Gb */
760 struct sfp_flags {
761         u8 e1000_base_sx:1;
762         u8 e1000_base_lx:1;
763         u8 e1000_base_cx:1;
764         u8 e1000_base_t:1;
765         u8 e100_base_lx:1;
766         u8 e100_base_fx:1;
767         u8 e10_base_bx10:1;
768         u8 e10_base_px:1;
769 };
770
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] == ' ')   \
775                         str[i] = 0;
776
777 static int gw16083_read_port_sfp(struct i2c_client *client,
778                                  struct mv88e1111_port_state *state)
779 {
780         int ret = 0;
781         u8 data[256];
782         struct sfp_flags *eth_flags;
783         u8 crc;
784         int i;
785         u8 *str;
786         struct sfp_msa *sfp_msa = (struct sfp_msa *)data;
787         int port = state->port;
788         union i2c_smbus_data d;
789
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))
793                 return -ENODEV;
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)
798         {
799                 dev_err(&client->dev,
800                         "Port%d: failed writing PCA9543 register\n", port);
801                 return ret;
802         }
803
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)
810                 {
811                         dev_err(&client->dev,
812                                 "Port%d: failed reading SFP data\n", port);
813                         return ret;
814                 }
815                 memcpy(data + i, d.block + 1, I2C_SMBUS_BLOCK_MAX);
816         }
817
818         /* Validate checksums */
819         for (crc = 0, i = 0; i < 63; i++)
820                 crc += data[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,
824                         crc);
825 #ifdef FAIL_ON_CHECKSUM_ERR
826                 return -EINVAL;
827 #endif
828         }
829         for (crc = 0, i = 64; i < 95; i++)
830                 crc += data[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",
834                         port, crc);
835 #ifdef FAIL_ON_CHECKSUM_ERR
836                 return -EINVAL;
837 #endif
838         }
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]);
843                         break;
844                 }
845         }
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);
852
853         if ((sfp_msa->identifier != GBIC) &&
854             (sfp_msa->identifier != SFF) &&
855             (sfp_msa->identifier != SFP))
856         {
857                 dev_err(&client->dev, "Port%d: Unknown module identifier: %d\n",
858                         port, sfp_msa->identifier);
859                 return -EINVAL;
860         }
861
862         str = "";
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";
872                 ret = -EINVAL;
873         } else {
874                 str = "Unknown/Unsupported media type";
875                 ret = -EINVAL;
876         }
877         if (ret)
878                 dev_err(&client->dev, "Port%d: %s (0x%02x)\n", port, str,
879                         sfp_msa->transceiver[3]);
880         else
881                 dev_info(&client->dev, "Port%d: %s (0x%02x)\n", port, str,
882                         sfp_msa->transceiver[3]);
883
884         return ret;
885 }
886
887 static int gw16083_probe(struct i2c_client *client,
888                          const struct i2c_device_id *id)
889 {
890         int ret;
891
892         dev_info(&client->dev, "GW16083 Ethernet Expansion Mezzanine\n");
893         if (gw16083_client) {
894                 dev_err(&client->dev, "client already registered\n");
895                 return -EINVAL;
896         }
897         gw16083_client = client;
898
899         ret = phy_driver_register(&mv88e6176_phy_driver);
900         if (ret)
901                 dev_err(&client->dev,
902                         "failed to register mv88e6176 phy driver: %d\n", ret);
903         return ret;
904 }
905
906 static int gw16083_remove(struct i2c_client *client)
907 {
908         dev_dbg(&client->dev, "%s\n", __func__);
909
910         phy_driver_unregister(&mv88e6176_phy_driver);
911         gw16083_client = NULL;
912         return 0;
913 }
914
915 static const struct of_device_id gw16083_dt_ids[] = {
916         { .compatible = "gateworks,gw16083", },
917         { }
918 };
919
920 MODULE_DEVICE_TABLE(of, gw16083_dt_ids);
921
922 static const struct i2c_device_id gw16083_id[] = {
923         { "gw16083", 0 },
924         { }
925 };
926 MODULE_DEVICE_TABLE(i2c, gw16083_id);
927
928 static struct i2c_driver gw16083_driver = {
929         .driver = {
930                 .name   = "gw16083",
931                 .of_match_table = gw16083_dt_ids,
932         },
933         .probe          = gw16083_probe,
934         .remove         = gw16083_remove,
935         .id_table       = gw16083_id,
936 };
937
938 static int __init mv88e6176_init(void)
939 {
940         return i2c_add_driver(&gw16083_driver);
941 }
942
943 static void __exit mv88e6176_exit(void)
944 {
945         i2c_del_driver(&gw16083_driver);
946 }
947
948 module_init(mv88e6176_init);
949 module_exit(mv88e6176_exit);