switch: improve robo_switch_enable()
[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_5365;
76         bool 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[9];
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 5365 */
202 static bool robo_bcm5365(void)
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, val16);
207
208         /* 5365 will refuse this as it does not have this reg */
209         return robo_read16(ROBO_VLAN_PAGE, ROBO_VLAN_TABLE_ACCESS) != val16;
210 }
211
212 static bool robo_gmii(void)
213 {
214         if (mdio_read(0, ROBO_MII_STAT) & 0x0100)
215                 return ((mdio_read(0, 0x0f) & 0xf000) != 0);
216         return false;
217 }
218
219 static int robo_switch_enable(void)
220 {
221         unsigned int i, last_port;
222         u16 val;
223 #ifdef CONFIG_BCM47XX
224         char buf[20];
225 #endif
226
227         val = robo_read16(ROBO_CTRL_PAGE, ROBO_SWITCH_MODE);
228         if (!(val & (1 << 1))) {
229                 /* Unmanaged mode */
230                 val &= ~(1 << 0);
231                 /* With forwarding */
232                 val |= (1 << 1);
233                 robo_write16(ROBO_CTRL_PAGE, ROBO_SWITCH_MODE, val);
234                 val = robo_read16(ROBO_CTRL_PAGE, ROBO_SWITCH_MODE);
235                 if (!(val & (1 << 1))) {
236                         printk(KERN_ERR PFX "Failed to enable switch\n");
237                         return -EBUSY;
238                 }
239
240                 /* No spanning tree for unmanaged mode */
241                 last_port = (robo.devid == ROBO_DEVICE_ID_5398) ?
242                                 ROBO_PORT7_CTRL : ROBO_PORT4_CTRL;
243                 for (i = ROBO_PORT0_CTRL; i <= last_port; i++)
244                         robo_write16(ROBO_CTRL_PAGE, i, 0);
245
246                 /* No spanning tree on IMP port too */
247                 robo_write16(ROBO_CTRL_PAGE, ROBO_IM_PORT_CTRL, 0);
248         }
249
250 #ifdef CONFIG_BCM47XX
251         /* WAN port LED, except for Netgear WGT634U */
252         if (bcm47xx_nvram_getenv("nvram_type", buf, sizeof(buf)) >= 0) {
253                 if (strcmp(buf, "cfe") != 0)
254                         robo_write16(ROBO_CTRL_PAGE, 0x16, 0x1F);
255         }
256 #endif
257         return 0;
258 }
259
260 static void robo_switch_reset(void)
261 {
262         if ((robo.devid == ROBO_DEVICE_ID_5395) ||
263             (robo.devid == ROBO_DEVICE_ID_5397) ||
264             (robo.devid == ROBO_DEVICE_ID_5398)) {
265                 /* Trigger a software reset. */
266                 robo_write16(ROBO_CTRL_PAGE, 0x79, 0x83);
267                 mdelay(500);
268                 robo_write16(ROBO_CTRL_PAGE, 0x79, 0);
269         }
270 }
271
272 #ifdef CONFIG_BCM47XX
273 static int get_gpio_pin(const char *name)
274 {
275         int i, err;
276         char nvram_var[10];
277         char buf[30];
278
279         for (i = 0; i < 16; i++) {
280                 err = snprintf(nvram_var, sizeof(nvram_var), "gpio%i", i);
281                 if (err <= 0)
282                         continue;
283                 err = bcm47xx_nvram_getenv(nvram_var, buf, sizeof(buf));
284                 if (err <= 0)
285                         continue;
286                 if (!strcmp(name, buf))
287                         return i;
288         }
289         return -1;
290 }
291 #endif
292
293 static int robo_probe(char *devname)
294 {
295         __u32 phyid;
296         unsigned int i;
297         int err = -1;
298         struct mii_ioctl_data *mii;
299
300         printk(KERN_INFO PFX "Probing device '%s'\n", devname);
301         strcpy(robo.ifr.ifr_name, devname);
302
303         if ((robo.dev = dev_get_by_name(&init_net, devname)) == NULL) {
304                 printk(KERN_ERR PFX "No such device\n");
305                 err = -ENODEV;
306                 goto err_done;
307         }
308         if (!robo.dev->netdev_ops || !robo.dev->netdev_ops->ndo_do_ioctl) {
309                 printk(KERN_ERR PFX "ndo_do_ioctl not implemented in ethernet driver\n");
310                 err = -ENXIO;
311                 goto err_put;
312         }
313
314         robo.device = devname;
315
316         /* try access using MII ioctls - get phy address */
317         err = do_ioctl(SIOCGMIIPHY);
318         if (err < 0) {
319                 printk(KERN_ERR PFX "error (%i) while accessing MII phy registers with ioctls\n", err);
320                 goto err_put;
321         }
322
323         /* got phy address check for robo address */
324         mii = if_mii(&robo.ifr);
325         if ((mii->phy_id != ROBO_PHY_ADDR) &&
326             (mii->phy_id != ROBO_PHY_ADDR_BCM63XX) &&
327             (mii->phy_id != ROBO_PHY_ADDR_TG3)) {
328                 printk(KERN_ERR PFX "Invalid phy address (%d)\n", mii->phy_id);
329                 err = -ENODEV;
330                 goto err_put;
331         }
332
333 #ifdef CONFIG_BCM47XX
334         robo.gpio_lanports_enable = get_gpio_pin("lanports_enable");
335         if (robo.gpio_lanports_enable >= 0) {
336                 err = gpio_request(robo.gpio_lanports_enable, "lanports_enable");
337                 if (err) {
338                         printk(KERN_ERR PFX "error (%i) requesting lanports_enable gpio (%i)\n",
339                                err, robo.gpio_lanports_enable);
340                         goto err_put;
341                 }
342                 gpio_direction_output(robo.gpio_lanports_enable, 1);
343                 mdelay(5);
344         }
345
346         robo.gpio_robo_reset = get_gpio_pin("robo_reset");
347         if (robo.gpio_robo_reset >= 0) {
348                 err = gpio_request(robo.gpio_robo_reset, "robo_reset");
349                 if (err) {
350                         printk(KERN_ERR PFX "error (%i) requesting robo_reset gpio (%i)\n",
351                                err, robo.gpio_robo_reset);
352                         goto err_gpio_robo;
353                 }
354                 gpio_set_value(robo.gpio_robo_reset, 0);
355                 gpio_direction_output(robo.gpio_robo_reset, 1);
356                 gpio_set_value(robo.gpio_robo_reset, 0);
357                 mdelay(50);
358
359                 gpio_set_value(robo.gpio_robo_reset, 1);
360                 mdelay(20);
361         } else {
362                 // TODO: reset the internal robo switch
363         }
364 #endif
365
366         phyid = mdio_read(ROBO_PHY_ADDR, 0x2) |
367                 (mdio_read(ROBO_PHY_ADDR, 0x3) << 16);
368
369         if (phyid == 0xffffffff || phyid == 0x55210022) {
370                 printk(KERN_ERR PFX "No Robo switch in managed mode found, phy_id = 0x%08x\n", phyid);
371                 err = -ENODEV;
372                 goto err_gpio_lanports;
373         }
374
375         /* Get the device ID */
376         for (i = 0; i < 10; i++) {
377                 robo.devid = robo_read16(ROBO_MGMT_PAGE, ROBO_DEVICE_ID);
378                 if (robo.devid)
379                         break;
380                 udelay(10);
381         }
382         if (!robo.devid)
383                 robo.devid = ROBO_DEVICE_ID_5325; /* Fake it */
384         if (robo.devid == ROBO_DEVICE_ID_5325)
385                 robo.is_5365 = robo_bcm5365();
386         else
387                 robo.is_5365 = false;
388
389         robo.gmii = robo_gmii();
390         if (robo.devid == ROBO_DEVICE_ID_5325) {
391                 for (i = 0; i < 5; i++)
392                         robo.port[i] = i;
393         } else {
394                 for (i = 0; i < 8; i++)
395                         robo.port[i] = i;
396         }
397         robo.port[i] = ROBO_IM_PORT_CTRL;
398
399         robo_switch_reset();
400         err = robo_switch_enable();
401         if (err)
402                 goto err_gpio_lanports;
403
404         printk(KERN_INFO PFX "found a 5%s%x!%s at %s\n", robo.devid & 0xff00 ? "" : "3", robo.devid,
405                 robo.is_5365 ? " It's a BCM5365." : "", devname);
406
407         return 0;
408
409 err_gpio_lanports:
410         if (robo.gpio_lanports_enable >= 0)
411                 gpio_free(robo.gpio_lanports_enable);
412 err_gpio_robo:
413         if (robo.gpio_robo_reset >= 0)
414                 gpio_free(robo.gpio_robo_reset);
415 err_put:
416         dev_put(robo.dev);
417         robo.dev = NULL;
418 err_done:
419         return err;
420 }
421
422
423 static int handle_vlan_port_read(void *driver, char *buf, int nr)
424 {
425         __u16 val16;
426         int len = 0;
427         int j;
428
429         val16 = (nr) /* vlan */ | (0 << 12) /* read */ | (1 << 13) /* enable */;
430
431         if (robo.is_5365) {
432                 robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_TABLE_ACCESS_5365, val16);
433                 /* actual read */
434                 val16 = robo_read16(ROBO_VLAN_PAGE, ROBO_VLAN_READ);
435                 if ((val16 & (1 << 14)) /* valid */) {
436                         for (j = 0; j < 6; j++) {
437                                 if (val16 & (1 << j)) {
438                                         len += sprintf(buf + len, "%d", j);
439                                         if (val16 & (1 << (j + 7))) {
440                                                 if (j == 5)
441                                                         buf[len++] = 'u';
442                                         } else {
443                                                 buf[len++] = 't';
444                                                 if (robo_read16(ROBO_VLAN_PAGE, ROBO_VLAN_PORT0_DEF_TAG + (j << 1)) == nr)
445                                                         buf[len++] = '*';
446                                         }
447                                         buf[len++] = '\t';
448                                 }
449                         }
450                         len += sprintf(buf + len, "\n");
451                 }
452         } else {
453                 u32 val32;
454                 robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_TABLE_ACCESS, val16);
455                 /* actual read */
456                 val32 = robo_read32(ROBO_VLAN_PAGE, ROBO_VLAN_READ);
457                 if ((val32 & (1 << 20)) /* valid */) {
458                         for (j = 0; j < 6; j++) {
459                                 if (val32 & (1 << j)) {
460                                         len += sprintf(buf + len, "%d", j);
461                                         if (val32 & (1 << (j + 6))) {
462                                                 if (j == 5)
463                                                         buf[len++] = 'u';
464                                         } else {
465                                                 buf[len++] = 't';
466                                                 if (robo_read16(ROBO_VLAN_PAGE, ROBO_VLAN_PORT0_DEF_TAG + (j << 1)) == nr)
467                                                         buf[len++] = '*';
468                                         }
469                                         buf[len++] = '\t';
470                                 }
471                         }
472                         len += sprintf(buf + len, "\n");
473                 }
474         }
475
476         buf[len] = '\0';
477
478         return len;
479 }
480
481 static int handle_vlan_port_write(void *driver, char *buf, int nr)
482 {
483         switch_driver *d = (switch_driver *) driver;
484         switch_vlan_config *c = switch_parse_vlan(d, buf);
485         int j;
486         __u16 val16;
487
488         if (c == NULL)
489                 return -EINVAL;
490
491         for (j = 0; j < d->ports; j++) {
492                 if ((c->untag | c->pvid) & (1 << j))
493                         /* change default vlan tag */
494                         robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_PORT0_DEF_TAG + (j << 1), nr);
495         }
496
497         /* write config now */
498
499         if (robo.devid != ROBO_DEVICE_ID_5325) {
500                 __u8 regoff = ((robo.devid == ROBO_DEVICE_ID_5395) ||
501                         (robo.devid == ROBO_DEVICE_ID_53115)) ? 0x20 : 0;
502
503                 robo_write32(ROBO_ARLIO_PAGE, 0x63 + regoff, (c->untag << 9) | c->port);
504                 robo_write16(ROBO_ARLIO_PAGE, 0x61 + regoff, nr);
505                 robo_write16(ROBO_ARLIO_PAGE, 0x60 + regoff, 1 << 7);
506                 kfree(c);
507                 return 0;
508         }
509
510         val16 = (nr) /* vlan */ | (1 << 12) /* write */ | (1 << 13) /* enable */;
511         if (robo.is_5365) {
512                 robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_WRITE_5365,
513                         (1 << 14)  /* valid */ | (c->untag << 7) | c->port);
514                 robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_TABLE_ACCESS_5365, val16);
515         } else {
516                 robo_write32(ROBO_VLAN_PAGE, ROBO_VLAN_WRITE,
517                         (1 << 20) /* valid */ | (c->untag << 6) | c->port);
518                 robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_TABLE_ACCESS, val16);
519         }
520
521         kfree(c);
522         return 0;
523 }
524
525 #define set_switch(state) \
526         robo_write16(ROBO_CTRL_PAGE, ROBO_SWITCH_MODE, (robo_read16(ROBO_CTRL_PAGE, ROBO_SWITCH_MODE) & ~2) | (state ? 2 : 0));
527
528 static int handle_enable_read(void *driver, char *buf, int nr)
529 {
530         return sprintf(buf, "%d\n", (((robo_read16(ROBO_CTRL_PAGE, ROBO_SWITCH_MODE) & 2) == 2) ? 1 : 0));
531 }
532
533 static int handle_enable_write(void *driver, char *buf, int nr)
534 {
535         set_switch(buf[0] == '1');
536
537         return 0;
538 }
539
540 static int handle_port_enable_read(void *driver, char *buf, int nr)
541 {
542         return sprintf(buf, "%d\n", ((robo_read16(ROBO_CTRL_PAGE, robo.port[nr]) & 3) == 3 ? 0 : 1));
543 }
544
545 static int handle_port_enable_write(void *driver, char *buf, int nr)
546 {
547         u16 val16;
548
549         if (buf[0] == '0')
550                 val16 = 3; /* disabled */
551         else if (buf[0] == '1')
552                 val16 = 0; /* enabled */
553         else
554                 return -EINVAL;
555
556         robo_write16(ROBO_CTRL_PAGE, robo.port[nr],
557                 (robo_read16(ROBO_CTRL_PAGE, robo.port[nr]) & ~3) | val16);
558
559         return 0;
560 }
561
562 static int handle_port_media_read(void *driver, char *buf, int nr)
563 {
564         u16 bmcr = mdio_read(robo.port[nr], MII_BMCR);
565         int media, len;
566
567         if (bmcr & BMCR_ANENABLE)
568                 media = SWITCH_MEDIA_AUTO;
569         else {
570                 if (bmcr & BMCR_SPEED1000)
571                         media = SWITCH_MEDIA_1000;
572                 else if (bmcr & BMCR_SPEED100)
573                         media = SWITCH_MEDIA_100;
574                 else
575                         media = 0;
576
577                 if (bmcr & BMCR_FULLDPLX)
578                         media |= SWITCH_MEDIA_FD;
579         }
580
581         len = switch_print_media(buf, media);
582         return len + sprintf(buf + len, "\n");
583 }
584
585 static int handle_port_media_write(void *driver, char *buf, int nr)
586 {
587         int media = switch_parse_media(buf);
588         u16 bmcr, bmcr_mask;
589
590         if (media & SWITCH_MEDIA_AUTO)
591                 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
592         else {
593                 if (media & SWITCH_MEDIA_1000) {
594                         if (!robo.gmii)
595                                 return -EINVAL;
596                         bmcr = BMCR_SPEED1000;
597                 }
598                 else if (media & SWITCH_MEDIA_100)
599                         bmcr = BMCR_SPEED100;
600                 else
601                         bmcr = 0;
602
603                 if (media & SWITCH_MEDIA_FD)
604                         bmcr |= BMCR_FULLDPLX;
605         }
606
607         bmcr_mask = ~(BMCR_SPEED1000 | BMCR_SPEED100 | BMCR_FULLDPLX | BMCR_ANENABLE | BMCR_ANRESTART);
608         mdio_write(robo.port[nr], MII_BMCR,
609                 (mdio_read(robo.port[nr], MII_BMCR) & bmcr_mask) | bmcr);
610
611         return 0;
612 }
613
614 static int handle_enable_vlan_read(void *driver, char *buf, int nr)
615 {
616         return sprintf(buf, "%d\n", (((robo_read16(ROBO_VLAN_PAGE, ROBO_VLAN_CTRL0) & (1 << 7)) == (1 << 7)) ? 1 : 0));
617 }
618
619 static int handle_enable_vlan_write(void *driver, char *buf, int nr)
620 {
621         int disable = ((buf[0] != '1') ? 1 : 0);
622
623         robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_CTRL0, disable ? 0 :
624                 (1 << 7) /* 802.1Q VLAN */ | (3 << 5) /* mac check and hash */);
625         robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_CTRL1, disable ? 0 :
626                 (robo.devid == ROBO_DEVICE_ID_5325 ? (1 << 1) :
627                 0) | (1 << 2) | (1 << 3)); /* RSV multicast */
628
629         if (robo.devid != ROBO_DEVICE_ID_5325)
630                 return 0;
631
632         robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_CTRL4, disable ? 0 :
633                 (1 << 6) /* drop invalid VID frames */);
634         robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_CTRL5, disable ? 0 :
635                 (1 << 3) /* drop miss V table frames */);
636
637         return 0;
638 }
639
640 static int handle_reset(void *driver, char *buf, int nr)
641 {
642         switch_driver *d = (switch_driver *) driver;
643         int j;
644         __u16 val16;
645
646         /* disable switching */
647         set_switch(0);
648
649         /* reset vlans */
650         for (j = 0; j <= ((robo.is_5365) ? VLAN_ID_MAX_5365 : VLAN_ID_MAX); j++) {
651                 /* write config now */
652                 val16 = (j) /* vlan */ | (1 << 12) /* write */ | (1 << 13) /* enable */;
653                 if (robo.is_5365)
654                         robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_WRITE_5365, 0);
655                 else
656                         robo_write32(ROBO_VLAN_PAGE, ROBO_VLAN_WRITE, 0);
657                 robo_write16(ROBO_VLAN_PAGE, robo.is_5365 ? ROBO_VLAN_TABLE_ACCESS_5365 :
658                                                             ROBO_VLAN_TABLE_ACCESS,
659                              val16);
660         }
661
662         /* reset ports to a known good state */
663         for (j = 0; j < d->ports; j++) {
664                 robo_write16(ROBO_CTRL_PAGE, robo.port[j], 0x0000);
665                 robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_PORT0_DEF_TAG + (j << 1), 0);
666         }
667
668         /* enable switching */
669         set_switch(1);
670
671         /* enable vlans */
672         handle_enable_vlan_write(driver, "1", 0);
673
674         return 0;
675 }
676
677 static int __init robo_init(void)
678 {
679         int notfound = 1;
680         char *device;
681
682         device = strdup("ethX");
683         for (device[3] = '0'; (device[3] <= '3') && notfound; device[3]++) {
684                 if (! switch_device_registered (device))
685                         notfound = robo_probe(device);
686         }
687         device[3]--;
688
689         if (notfound) {
690                 kfree(device);
691                 return -ENODEV;
692         } else {
693                 static const switch_config cfg[] = {
694                         {
695                                 .name   = "enable",
696                                 .read   = handle_enable_read,
697                                 .write  = handle_enable_write
698                         }, {
699                                 .name   = "enable_vlan",
700                                 .read   = handle_enable_vlan_read,
701                                 .write  = handle_enable_vlan_write
702                         }, {
703                                 .name   = "reset",
704                                 .read   = NULL,
705                                 .write  = handle_reset
706                         }, { NULL, },
707                 };
708                 static const switch_config port[] = {
709                         {
710                                 .name   = "enable",
711                                 .read   = handle_port_enable_read,
712                                 .write  = handle_port_enable_write
713                         }, {
714                                 .name   = "media",
715                                 .read   = handle_port_media_read,
716                                 .write  = handle_port_media_write
717                         }, { NULL, },
718                 };
719                 static const switch_config vlan[] = {
720                         {
721                                 .name   = "ports",
722                                 .read   = handle_vlan_port_read,
723                                 .write  = handle_vlan_port_write
724                         }, { NULL, },
725                 };
726                 switch_driver driver = {
727                         .name                   = DRIVER_NAME,
728                         .version                = DRIVER_VERSION,
729                         .interface              = device,
730                         .cpuport                = 5,
731                         .ports                  = 6,
732                         .vlans                  = 16,
733                         .driver_handlers        = cfg,
734                         .port_handlers          = port,
735                         .vlan_handlers          = vlan,
736                 };
737                 if (robo.devid != ROBO_DEVICE_ID_5325) {
738                         driver.ports = 9;
739                         driver.cpuport = 8;
740                 }
741
742                 return switch_register_driver(&driver);
743         }
744 }
745
746 static void __exit robo_exit(void)
747 {
748         switch_unregister_driver(DRIVER_NAME);
749         if (robo.dev)
750                 dev_put(robo.dev);
751         if (robo.gpio_robo_reset >= 0)
752                 gpio_free(robo.gpio_robo_reset);
753         if (robo.gpio_lanports_enable >= 0)
754                 gpio_free(robo.gpio_lanports_enable);
755         kfree(robo.device);
756 }
757
758
759 MODULE_AUTHOR("Felix Fietkau <openwrt@nbd.name>");
760 MODULE_LICENSE("GPL");
761
762 module_init(robo_init);
763 module_exit(robo_exit);