switch: reset switch before using it.
[15.05/openwrt.git] / package / switch / src / switch-robo.c
1 /*
2  * Broadcom BCM5325E/536x switch configuration module
3  *
4  * Copyright (C) 2005 Felix Fietkau <nbd@nbd.name>
5  * Copyright (C) 2008 Michael Buesch <mb@bu3sch.de>
6  * Based on 'robocfg' by Oleg I. Vdovikin
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21  * 02110-1301, USA.
22  */
23
24 #include <linux/module.h>
25 #include <linux/init.h>
26 #include <linux/if.h>
27 #include <linux/if_arp.h>
28 #include <linux/sockios.h>
29 #include <linux/ethtool.h>
30 #include <linux/mii.h>
31 #include <linux/delay.h>
32 #include <linux/gpio.h>
33 #include <asm/uaccess.h>
34
35 #include "switch-core.h"
36 #include "etc53xx.h"
37
38 #ifdef CONFIG_BCM47XX
39 #include <bcm47xx_nvram.h>
40 #endif
41
42 #define DRIVER_NAME             "bcm53xx"
43 #define DRIVER_VERSION          "0.02"
44 #define PFX                     "roboswitch: "
45
46 #define ROBO_PHY_ADDR           0x1E    /* robo switch phy address */
47 #define ROBO_PHY_ADDR_TG3       0x01    /* Tigon3 PHY address */
48 #define ROBO_PHY_ADDR_BCM63XX   0x00    /* BCM63XX PHY address */
49
50 /* MII registers */
51 #define REG_MII_PAGE    0x10    /* MII Page register */
52 #define REG_MII_ADDR    0x11    /* MII Address register */
53 #define REG_MII_DATA0   0x18    /* MII Data register 0 */
54
55 #define REG_MII_PAGE_ENABLE     1
56 #define REG_MII_ADDR_WRITE      1
57 #define REG_MII_ADDR_READ       2
58
59 /* Robo device ID register (in ROBO_MGMT_PAGE) */
60 #define ROBO_DEVICE_ID          0x30
61 #define  ROBO_DEVICE_ID_5325    0x25 /* Faked */
62 #define  ROBO_DEVICE_ID_5395    0x95
63 #define  ROBO_DEVICE_ID_5397    0x97
64 #define  ROBO_DEVICE_ID_5398    0x98
65 #define  ROBO_DEVICE_ID_53115   0x3115
66
67 /* Private et.o ioctls */
68 #define SIOCGETCPHYRD           (SIOCDEVPRIVATE + 9)
69 #define SIOCSETCPHYWR           (SIOCDEVPRIVATE + 10)
70
71 /* Data structure for a Roboswitch device. */
72 struct robo_switch {
73         char *device;                   /* The device name string (ethX) */
74         u16 devid;                      /* ROBO_DEVICE_ID_53xx */
75         bool is_5350;
76         u8 gmii;                        /* gigabit mii */
77         int gpio_robo_reset;
78         int gpio_lanports_enable;
79         struct ifreq ifr;
80         struct net_device *dev;
81         unsigned char port[6];
82 };
83
84 /* Currently we can only have one device in the system. */
85 static struct robo_switch robo;
86
87
88 static int do_ioctl(int cmd)
89 {
90         mm_segment_t old_fs = get_fs();
91         int ret;
92
93         set_fs(KERNEL_DS);
94         ret = robo.dev->netdev_ops->ndo_do_ioctl(robo.dev, &robo.ifr, cmd);
95         set_fs(old_fs);
96
97         return ret;
98 }
99
100 static u16 mdio_read(__u16 phy_id, __u8 reg)
101 {
102         struct mii_ioctl_data *mii = if_mii(&robo.ifr);
103         int err;
104
105         mii->phy_id = phy_id;
106         mii->reg_num = reg;
107
108         err = do_ioctl(SIOCGMIIREG);
109         if (err < 0) {
110                 printk(KERN_ERR PFX "failed to read mdio reg %i with err %i.\n", reg, err);
111
112                 return 0xffff;
113         }
114
115         return mii->val_out;
116 }
117
118 static void mdio_write(__u16 phy_id, __u8 reg, __u16 val)
119 {
120         struct mii_ioctl_data *mii = if_mii(&robo.ifr);
121         int err;
122
123         mii->phy_id = phy_id;
124         mii->reg_num = reg;
125         mii->val_in = val;
126
127         err = do_ioctl(SIOCSMIIREG);
128         if (err < 0) {
129                 printk(KERN_ERR PFX "failed to write mdio reg: %i with err %i.\n", reg, err);
130                 return;
131         }
132 }
133
134 static int robo_reg(__u8 page, __u8 reg, __u8 op)
135 {
136         int i = 3;
137
138         /* set page number */
139         mdio_write(ROBO_PHY_ADDR, REG_MII_PAGE,
140                 (page << 8) | REG_MII_PAGE_ENABLE);
141
142         /* set register address */
143         mdio_write(ROBO_PHY_ADDR, REG_MII_ADDR,
144                 (reg << 8) | op);
145
146         /* check if operation completed */
147         while (i--) {
148                 if ((mdio_read(ROBO_PHY_ADDR, REG_MII_ADDR) & 3) == 0)
149                         return 0;
150         }
151
152         printk(KERN_ERR PFX "timeout in robo_reg on page %i and reg %i with op %i.\n", page, reg, op);
153
154         return 1;
155 }
156
157 /*
158 static void robo_read(__u8 page, __u8 reg, __u16 *val, int count)
159 {
160         int i;
161
162         robo_reg(page, reg, REG_MII_ADDR_READ);
163
164         for (i = 0; i < count; i++)
165                 val[i] = mdio_read(ROBO_PHY_ADDR, REG_MII_DATA0 + i);
166 }
167 */
168
169 static __u16 robo_read16(__u8 page, __u8 reg)
170 {
171         robo_reg(page, reg, REG_MII_ADDR_READ);
172
173         return mdio_read(ROBO_PHY_ADDR, REG_MII_DATA0);
174 }
175
176 static __u32 robo_read32(__u8 page, __u8 reg)
177 {
178         robo_reg(page, reg, REG_MII_ADDR_READ);
179
180         return mdio_read(ROBO_PHY_ADDR, REG_MII_DATA0) |
181                 (mdio_read(ROBO_PHY_ADDR, REG_MII_DATA0 + 1) << 16);
182 }
183
184 static void robo_write16(__u8 page, __u8 reg, __u16 val16)
185 {
186         /* write data */
187         mdio_write(ROBO_PHY_ADDR, REG_MII_DATA0, val16);
188
189         robo_reg(page, reg, REG_MII_ADDR_WRITE);
190 }
191
192 static void robo_write32(__u8 page, __u8 reg, __u32 val32)
193 {
194         /* write data */
195         mdio_write(ROBO_PHY_ADDR, REG_MII_DATA0, val32 & 0xFFFF);
196         mdio_write(ROBO_PHY_ADDR, REG_MII_DATA0 + 1, val32 >> 16);
197
198         robo_reg(page, reg, REG_MII_ADDR_WRITE);
199 }
200
201 /* checks that attached switch is 5325/5352/5354/5356/5357/53115 */
202 static int robo_vlan5350(__u32 phyid)
203 {
204         /* set vlan access id to 15 and read it back */
205         __u16 val16 = 15;
206         robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_TABLE_ACCESS_5350, val16);
207
208         /* 5365 will refuse this as it does not have this reg */
209         if (robo_read16(ROBO_VLAN_PAGE, ROBO_VLAN_TABLE_ACCESS_5350) != val16)
210                 return 0;
211         /* gigabit ? */
212         if (mdio_read(0, ROBO_MII_STAT) & 0x0100)
213                 robo.gmii = ((mdio_read(0, 0x0f) & 0xf000) != 0);
214         /* 53115 ? */
215         if (robo.gmii && robo_read32(ROBO_STAT_PAGE, ROBO_LSA_IM_PORT) != 0) {
216                 robo_write16(ROBO_ARLIO_PAGE, ROBO_VTBL_INDX_5395, val16);
217                 robo_write16(ROBO_ARLIO_PAGE, ROBO_VTBL_ACCESS_5395,
218                                          (1 << 7) /* start */ | 1 /* read */);
219                 if (robo_read16(ROBO_ARLIO_PAGE, ROBO_VTBL_ACCESS_5395) == 1 &&
220                     robo_read16(ROBO_ARLIO_PAGE, ROBO_VTBL_INDX_5395) == val16)
221                         return 4;
222         }
223         /* dirty trick for 5356/5357 */
224         if ((phyid & 0xfff0ffff ) == 0x5da00362 ||
225             (phyid & 0xfff0ffff ) == 0x5e000362)
226                 return 3;
227         /* 5325/5352/5354*/
228         return 1;
229 }
230
231 static int robo_switch_enable(void)
232 {
233         unsigned int i, last_port;
234         u16 val;
235 #ifdef CONFIG_BCM47XX
236         char buf[20];
237 #endif
238
239         val = robo_read16(ROBO_CTRL_PAGE, ROBO_SWITCH_MODE);
240         if (!(val & (1 << 1))) {
241                 /* Unmanaged mode */
242                 val &= ~(1 << 0);
243                 /* With forwarding */
244                 val |= (1 << 1);
245                 robo_write16(ROBO_CTRL_PAGE, ROBO_SWITCH_MODE, val);
246                 val = robo_read16(ROBO_CTRL_PAGE, ROBO_SWITCH_MODE);
247                 if (!(val & (1 << 1))) {
248                         printk(KERN_ERR PFX "Failed to enable switch\n");
249                         return -EBUSY;
250                 }
251
252                 last_port = (robo.devid == ROBO_DEVICE_ID_5398) ?
253                                 ROBO_PORT6_CTRL : ROBO_PORT3_CTRL;
254                 for (i = ROBO_PORT0_CTRL; i < last_port + 1; i++)
255                         robo_write16(ROBO_CTRL_PAGE, i, 0);
256         }
257
258 #ifdef CONFIG_BCM47XX
259         /* WAN port LED, except for Netgear WGT634U */
260         if (bcm47xx_nvram_getenv("nvram_type", buf, sizeof(buf)) >= 0) {
261                 if (strcmp(buf, "cfe") != 0)
262                         robo_write16(ROBO_CTRL_PAGE, 0x16, 0x1F);
263         }
264 #endif
265         return 0;
266 }
267
268 static void robo_switch_reset(void)
269 {
270         if ((robo.devid == ROBO_DEVICE_ID_5395) ||
271             (robo.devid == ROBO_DEVICE_ID_5397) ||
272             (robo.devid == ROBO_DEVICE_ID_5398)) {
273                 /* Trigger a software reset. */
274                 robo_write16(ROBO_CTRL_PAGE, 0x79, 0x83);
275                 mdelay(500);
276                 robo_write16(ROBO_CTRL_PAGE, 0x79, 0);
277         }
278 }
279
280 #ifdef CONFIG_BCM47XX
281 static int get_gpio_pin(const char *name)
282 {
283         int i, err;
284         char nvram_var[10];
285         char buf[30];
286
287         for (i = 0; i < 16; i++) {
288                 err = snprintf(nvram_var, sizeof(nvram_var), "gpio%i", i);
289                 if (err <= 0)
290                         continue;
291                 err = bcm47xx_nvram_getenv(nvram_var, buf, sizeof(buf));
292                 if (err <= 0)
293                         continue;
294                 if (!strcmp(name, buf))
295                         return i;
296         }
297         return -1;
298 }
299 #endif
300
301 static int robo_probe(char *devname)
302 {
303         __u32 phyid;
304         unsigned int i;
305         int err = -1;
306         struct mii_ioctl_data *mii;
307
308         printk(KERN_INFO PFX "Probing device '%s'\n", devname);
309         strcpy(robo.ifr.ifr_name, devname);
310
311         if ((robo.dev = dev_get_by_name(&init_net, devname)) == NULL) {
312                 printk(KERN_ERR PFX "No such device\n");
313                 err = -ENODEV;
314                 goto err_done;
315         }
316         if (!robo.dev->netdev_ops || !robo.dev->netdev_ops->ndo_do_ioctl) {
317                 printk(KERN_ERR PFX "ndo_do_ioctl not implemented in ethernet driver\n");
318                 err = -ENXIO;
319                 goto err_put;
320         }
321
322         robo.device = devname;
323         for (i = 0; i < 5; i++)
324                 robo.port[i] = i;
325         robo.port[5] = 8;
326
327         /* try access using MII ioctls - get phy address */
328         err = do_ioctl(SIOCGMIIPHY);
329         if (err < 0) {
330                 printk(KERN_ERR PFX "error (%i) while accessing MII phy registers with ioctls\n", err);
331                 goto err_put;
332         }
333
334         /* got phy address check for robo address */
335         mii = if_mii(&robo.ifr);
336         if ((mii->phy_id != ROBO_PHY_ADDR) &&
337             (mii->phy_id != ROBO_PHY_ADDR_BCM63XX) &&
338             (mii->phy_id != ROBO_PHY_ADDR_TG3)) {
339                 printk(KERN_ERR PFX "Invalid phy address (%d)\n", mii->phy_id);
340                 err = -ENODEV;
341                 goto err_put;
342         }
343
344 #ifdef CONFIG_BCM47XX
345         robo.gpio_lanports_enable = get_gpio_pin("lanports_enable");
346         if (robo.gpio_lanports_enable >= 0) {
347                 err = gpio_request(robo.gpio_lanports_enable, "lanports_enable");
348                 if (err) {
349                         printk(KERN_ERR PFX "error (%i) requesting lanports_enable gpio (%i)\n",
350                                err, robo.gpio_lanports_enable);
351                         goto err_put;
352                 }
353                 gpio_direction_output(robo.gpio_lanports_enable, 1);
354                 mdelay(5);
355         }
356
357         robo.gpio_robo_reset = get_gpio_pin("robo_reset");
358         if (robo.gpio_robo_reset >= 0) {
359                 err = gpio_request(robo.gpio_robo_reset, "robo_reset");
360                 if (err) {
361                         printk(KERN_ERR PFX "error (%i) requesting robo_reset gpio (%i)\n",
362                                err, robo.gpio_robo_reset);
363                         goto err_gpio_robo;
364                 }
365                 gpio_set_value(robo.gpio_robo_reset, 0);
366                 gpio_direction_output(robo.gpio_robo_reset, 1);
367                 gpio_set_value(robo.gpio_robo_reset, 0);
368                 mdelay(50);
369
370                 gpio_set_value(robo.gpio_robo_reset, 1);
371                 mdelay(20);
372         } else {
373                 // TODO: reset the internal robo switch
374         }
375 #endif
376
377         phyid = mdio_read(ROBO_PHY_ADDR, 0x2) |
378                 (mdio_read(ROBO_PHY_ADDR, 0x3) << 16);
379
380         if (phyid == 0xffffffff || phyid == 0x55210022) {
381                 printk(KERN_ERR PFX "No Robo switch in managed mode found, phy_id = 0x%08x\n", phyid);
382                 err = -ENODEV;
383                 goto err_gpio_lanports;
384         }
385
386         /* Get the device ID */
387         for (i = 0; i < 10; i++) {
388                 robo.devid = robo_read16(ROBO_MGMT_PAGE, ROBO_DEVICE_ID);
389                 if (robo.devid)
390                         break;
391                 udelay(10);
392         }
393         if (!robo.devid)
394                 robo.devid = ROBO_DEVICE_ID_5325; /* Fake it */
395         robo.is_5350 = robo_vlan5350(phyid);
396
397         robo_switch_reset();
398         err = robo_switch_enable();
399         if (err)
400                 goto err_gpio_lanports;
401
402         printk(KERN_INFO PFX "found a 5%s%x!%s at %s\n", robo.devid & 0xff00 ? "" : "3", robo.devid,
403                 robo.is_5350 ? " It's a 5350." : "", devname);
404
405         return 0;
406
407 err_gpio_lanports:
408         if (robo.gpio_lanports_enable >= 0)
409                 gpio_free(robo.gpio_lanports_enable);
410 err_gpio_robo:
411         if (robo.gpio_robo_reset >= 0)
412                 gpio_free(robo.gpio_robo_reset);
413 err_put:
414         dev_put(robo.dev);
415         robo.dev = NULL;
416 err_done:
417         return err;
418 }
419
420
421 static int handle_vlan_port_read(void *driver, char *buf, int nr)
422 {
423         __u16 val16;
424         int len = 0;
425         int j;
426
427         val16 = (nr) /* vlan */ | (0 << 12) /* read */ | (1 << 13) /* enable */;
428
429         if (robo.is_5350) {
430                 u32 val32;
431                 robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_TABLE_ACCESS_5350, val16);
432                 /* actual read */
433                 val32 = robo_read32(ROBO_VLAN_PAGE, ROBO_VLAN_READ);
434                 if ((val32 & (1 << 20)) /* valid */) {
435                         for (j = 0; j < 6; j++) {
436                                 if (val32 & (1 << j)) {
437                                         len += sprintf(buf + len, "%d", j);
438                                         if (val32 & (1 << (j + 6))) {
439                                                 if (j == 5) buf[len++] = 'u';
440                                         } else {
441                                                 buf[len++] = 't';
442                                                 if (robo_read16(ROBO_VLAN_PAGE, ROBO_VLAN_PORT0_DEF_TAG + (j << 1)) == nr)
443                                                         buf[len++] = '*';
444                                         }
445                                         buf[len++] = '\t';
446                                 }
447                         }
448                         len += sprintf(buf + len, "\n");
449                 }
450         } else {
451                 robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_TABLE_ACCESS, val16);
452                 /* actual read */
453                 val16 = robo_read16(ROBO_VLAN_PAGE, ROBO_VLAN_READ);
454                 if ((val16 & (1 << 14)) /* valid */) {
455                         for (j = 0; j < 6; j++) {
456                                 if (val16 & (1 << j)) {
457                                         len += sprintf(buf + len, "%d", j);
458                                         if (val16 & (1 << (j + 7))) {
459                                                 if (j == 5) buf[len++] = 'u';
460                                         } else {
461                                                 buf[len++] = 't';
462                                                 if (robo_read16(ROBO_VLAN_PAGE, ROBO_VLAN_PORT0_DEF_TAG + (j << 1)) == nr)
463                                                         buf[len++] = '*';
464                                         }
465                                         buf[len++] = '\t';
466                                 }
467                         }
468                         len += sprintf(buf + len, "\n");
469                 }
470         }
471
472         buf[len] = '\0';
473
474         return len;
475 }
476
477 static int handle_vlan_port_write(void *driver, char *buf, int nr)
478 {
479         switch_driver *d = (switch_driver *) driver;
480         switch_vlan_config *c = switch_parse_vlan(d, buf);
481         int j;
482         __u16 val16;
483
484         if (c == NULL)
485                 return -EINVAL;
486
487         for (j = 0; j < d->ports; j++) {
488                 if ((c->untag | c->pvid) & (1 << j))
489                         /* change default vlan tag */
490                         robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_PORT0_DEF_TAG + (j << 1), nr);
491         }
492
493         /* write config now */
494
495         if (robo.devid != ROBO_DEVICE_ID_5325) {
496                 __u8 regoff = ((robo.devid == ROBO_DEVICE_ID_5395) ||
497                         (robo.devid == ROBO_DEVICE_ID_53115)) ? 0x20 : 0;
498
499                 robo_write32(ROBO_ARLIO_PAGE, 0x63 + regoff, (c->untag << 9) | c->port);
500                 robo_write16(ROBO_ARLIO_PAGE, 0x61 + regoff, nr);
501                 robo_write16(ROBO_ARLIO_PAGE, 0x60 + regoff, 1 << 7);
502                 kfree(c);
503                 return 0;
504         }
505
506         val16 = (nr) /* vlan */ | (1 << 12) /* write */ | (1 << 13) /* enable */;
507         if (robo.is_5350) {
508                 robo_write32(ROBO_VLAN_PAGE, ROBO_VLAN_WRITE_5350,
509                         (1 << 20) /* valid */ | (c->untag << 6) | c->port);
510                 robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_TABLE_ACCESS_5350, val16);
511         } else {
512                 robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_WRITE,
513                         (1 << 14)  /* valid */ | (c->untag << 7) | c->port);
514                 robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_TABLE_ACCESS, val16);
515         }
516
517         kfree(c);
518         return 0;
519 }
520
521 #define set_switch(state) \
522         robo_write16(ROBO_CTRL_PAGE, ROBO_SWITCH_MODE, (robo_read16(ROBO_CTRL_PAGE, ROBO_SWITCH_MODE) & ~2) | (state ? 2 : 0));
523
524 static int handle_enable_read(void *driver, char *buf, int nr)
525 {
526         return sprintf(buf, "%d\n", (((robo_read16(ROBO_CTRL_PAGE, ROBO_SWITCH_MODE) & 2) == 2) ? 1 : 0));
527 }
528
529 static int handle_enable_write(void *driver, char *buf, int nr)
530 {
531         set_switch(buf[0] == '1');
532
533         return 0;
534 }
535
536 static int handle_port_enable_read(void *driver, char *buf, int nr)
537 {
538         return sprintf(buf, "%d\n", ((robo_read16(ROBO_CTRL_PAGE, robo.port[nr]) & 3) == 3 ? 0 : 1));
539 }
540
541 static int handle_port_enable_write(void *driver, char *buf, int nr)
542 {
543         u16 val16;
544
545         if (buf[0] == '0')
546                 val16 = 3; /* disabled */
547         else if (buf[0] == '1')
548                 val16 = 0; /* enabled */
549         else
550                 return -EINVAL;
551
552         robo_write16(ROBO_CTRL_PAGE, robo.port[nr],
553                 (robo_read16(ROBO_CTRL_PAGE, robo.port[nr]) & ~3) | val16);
554
555         return 0;
556 }
557
558 static int handle_port_media_read(void *driver, char *buf, int nr)
559 {
560         u16 bmcr = mdio_read(robo.port[nr], MII_BMCR);
561         int media, len;
562
563         if (bmcr & BMCR_ANENABLE)
564                 media = SWITCH_MEDIA_AUTO;
565         else {
566                 if (bmcr & BMCR_SPEED1000)
567                         media = SWITCH_MEDIA_1000;
568                 else if (bmcr & BMCR_SPEED100)
569                         media = SWITCH_MEDIA_100;
570                 else
571                         media = 0;
572
573                 if (bmcr & BMCR_FULLDPLX)
574                         media |= SWITCH_MEDIA_FD;
575         }
576
577         len = switch_print_media(buf, media);
578         return len + sprintf(buf + len, "\n");
579 }
580
581 static int handle_port_media_write(void *driver, char *buf, int nr)
582 {
583         int media = switch_parse_media(buf);
584         u16 bmcr, bmcr_mask;
585
586         if (media & SWITCH_MEDIA_AUTO)
587                 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
588         else {
589                 if (media & SWITCH_MEDIA_1000) {
590                         if (!robo.gmii)
591                                 return -EINVAL;
592                         bmcr = BMCR_SPEED1000;
593                 }
594                 else if (media & SWITCH_MEDIA_100)
595                         bmcr = BMCR_SPEED100;
596                 else
597                         bmcr = 0;
598
599                 if (media & SWITCH_MEDIA_FD)
600                         bmcr |= BMCR_FULLDPLX;
601         }
602
603         bmcr_mask = ~(BMCR_SPEED1000 | BMCR_SPEED100 | BMCR_FULLDPLX | BMCR_ANENABLE | BMCR_ANRESTART);
604         mdio_write(robo.port[nr], MII_BMCR,
605                 (mdio_read(robo.port[nr], MII_BMCR) & bmcr_mask) | bmcr);
606
607         return 0;
608 }
609
610 static int handle_enable_vlan_read(void *driver, char *buf, int nr)
611 {
612         return sprintf(buf, "%d\n", (((robo_read16(ROBO_VLAN_PAGE, ROBO_VLAN_CTRL0) & (1 << 7)) == (1 << 7)) ? 1 : 0));
613 }
614
615 static int handle_enable_vlan_write(void *driver, char *buf, int nr)
616 {
617         int disable = ((buf[0] != '1') ? 1 : 0);
618
619         robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_CTRL0, disable ? 0 :
620                 (1 << 7) /* 802.1Q VLAN */ | (3 << 5) /* mac check and hash */);
621         robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_CTRL1, disable ? 0 :
622                 (robo.devid == ROBO_DEVICE_ID_5325 ? (1 << 1) :
623                 0) | (1 << 2) | (1 << 3)); /* RSV multicast */
624
625         if (robo.devid != ROBO_DEVICE_ID_5325)
626                 return 0;
627
628         robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_CTRL4, disable ? 0 :
629                 (1 << 6) /* drop invalid VID frames */);
630         robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_CTRL5, disable ? 0 :
631                 (1 << 3) /* drop miss V table frames */);
632
633         return 0;
634 }
635
636 static int handle_reset(void *driver, char *buf, int nr)
637 {
638         switch_driver *d = (switch_driver *) driver;
639         int j;
640         __u16 val16;
641
642         /* disable switching */
643         set_switch(0);
644
645         /* reset vlans */
646         for (j = 0; j <= ((robo.is_5350) ? VLAN_ID_MAX5350 : VLAN_ID_MAX); j++) {
647                 /* write config now */
648                 val16 = (j) /* vlan */ | (1 << 12) /* write */ | (1 << 13) /* enable */;
649                 if (robo.is_5350)
650                         robo_write32(ROBO_VLAN_PAGE, ROBO_VLAN_WRITE_5350, 0);
651                 else
652                         robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_WRITE, 0);
653                 robo_write16(ROBO_VLAN_PAGE, robo.is_5350 ? ROBO_VLAN_TABLE_ACCESS_5350 :
654                                                             ROBO_VLAN_TABLE_ACCESS,
655                              val16);
656         }
657
658         /* reset ports to a known good state */
659         for (j = 0; j < d->ports; j++) {
660                 robo_write16(ROBO_CTRL_PAGE, robo.port[j], 0x0000);
661                 robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_PORT0_DEF_TAG + (j << 1), 0);
662         }
663
664         /* enable switching */
665         set_switch(1);
666
667         /* enable vlans */
668         handle_enable_vlan_write(driver, "1", 0);
669
670         return 0;
671 }
672
673 static int __init robo_init(void)
674 {
675         int notfound = 1;
676         char *device;
677
678         device = strdup("ethX");
679         for (device[3] = '0'; (device[3] <= '3') && notfound; device[3]++) {
680                 if (! switch_device_registered (device))
681                         notfound = robo_probe(device);
682         }
683         device[3]--;
684
685         if (notfound) {
686                 kfree(device);
687                 return -ENODEV;
688         } else {
689                 static const switch_config cfg[] = {
690                         {
691                                 .name   = "enable",
692                                 .read   = handle_enable_read,
693                                 .write  = handle_enable_write
694                         }, {
695                                 .name   = "enable_vlan",
696                                 .read   = handle_enable_vlan_read,
697                                 .write  = handle_enable_vlan_write
698                         }, {
699                                 .name   = "reset",
700                                 .read   = NULL,
701                                 .write  = handle_reset
702                         }, { NULL, },
703                 };
704                 static const switch_config port[] = {
705                         {
706                                 .name   = "enable",
707                                 .read   = handle_port_enable_read,
708                                 .write  = handle_port_enable_write
709                         }, {
710                                 .name   = "media",
711                                 .read   = handle_port_media_read,
712                                 .write  = handle_port_media_write
713                         }, { NULL, },
714                 };
715                 static const switch_config vlan[] = {
716                         {
717                                 .name   = "ports",
718                                 .read   = handle_vlan_port_read,
719                                 .write  = handle_vlan_port_write
720                         }, { NULL, },
721                 };
722                 switch_driver driver = {
723                         .name                   = DRIVER_NAME,
724                         .version                = DRIVER_VERSION,
725                         .interface              = device,
726                         .cpuport                = 5,
727                         .ports                  = 6,
728                         .vlans                  = 16,
729                         .driver_handlers        = cfg,
730                         .port_handlers          = port,
731                         .vlan_handlers          = vlan,
732                 };
733                 if (robo.devid != ROBO_DEVICE_ID_5325) {
734                         driver.ports = 9;
735                         driver.cpuport = 8;
736                 }
737
738                 return switch_register_driver(&driver);
739         }
740 }
741
742 static void __exit robo_exit(void)
743 {
744         switch_unregister_driver(DRIVER_NAME);
745         if (robo.dev)
746                 dev_put(robo.dev);
747         if (robo.gpio_robo_reset >= 0)
748                 gpio_free(robo.gpio_robo_reset);
749         if (robo.gpio_lanports_enable >= 0)
750                 gpio_free(robo.gpio_lanports_enable);
751         kfree(robo.device);
752 }
753
754
755 MODULE_AUTHOR("Felix Fietkau <openwrt@nbd.name>");
756 MODULE_LICENSE("GPL");
757
758 module_init(robo_init);
759 module_exit(robo_exit);