switch: fix switches with more than 6 ports
[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                 last_port = (robo.devid == ROBO_DEVICE_ID_5398) ?
241                                 ROBO_PORT6_CTRL : ROBO_PORT3_CTRL;
242                 for (i = ROBO_PORT0_CTRL; i < last_port + 1; i++)
243                         robo_write16(ROBO_CTRL_PAGE, i, 0);
244         }
245
246 #ifdef CONFIG_BCM47XX
247         /* WAN port LED, except for Netgear WGT634U */
248         if (bcm47xx_nvram_getenv("nvram_type", buf, sizeof(buf)) >= 0) {
249                 if (strcmp(buf, "cfe") != 0)
250                         robo_write16(ROBO_CTRL_PAGE, 0x16, 0x1F);
251         }
252 #endif
253         return 0;
254 }
255
256 static void robo_switch_reset(void)
257 {
258         if ((robo.devid == ROBO_DEVICE_ID_5395) ||
259             (robo.devid == ROBO_DEVICE_ID_5397) ||
260             (robo.devid == ROBO_DEVICE_ID_5398)) {
261                 /* Trigger a software reset. */
262                 robo_write16(ROBO_CTRL_PAGE, 0x79, 0x83);
263                 mdelay(500);
264                 robo_write16(ROBO_CTRL_PAGE, 0x79, 0);
265         }
266 }
267
268 #ifdef CONFIG_BCM47XX
269 static int get_gpio_pin(const char *name)
270 {
271         int i, err;
272         char nvram_var[10];
273         char buf[30];
274
275         for (i = 0; i < 16; i++) {
276                 err = snprintf(nvram_var, sizeof(nvram_var), "gpio%i", i);
277                 if (err <= 0)
278                         continue;
279                 err = bcm47xx_nvram_getenv(nvram_var, buf, sizeof(buf));
280                 if (err <= 0)
281                         continue;
282                 if (!strcmp(name, buf))
283                         return i;
284         }
285         return -1;
286 }
287 #endif
288
289 static int robo_probe(char *devname)
290 {
291         __u32 phyid;
292         unsigned int i;
293         int err = -1;
294         struct mii_ioctl_data *mii;
295
296         printk(KERN_INFO PFX "Probing device '%s'\n", devname);
297         strcpy(robo.ifr.ifr_name, devname);
298
299         if ((robo.dev = dev_get_by_name(&init_net, devname)) == NULL) {
300                 printk(KERN_ERR PFX "No such device\n");
301                 err = -ENODEV;
302                 goto err_done;
303         }
304         if (!robo.dev->netdev_ops || !robo.dev->netdev_ops->ndo_do_ioctl) {
305                 printk(KERN_ERR PFX "ndo_do_ioctl not implemented in ethernet driver\n");
306                 err = -ENXIO;
307                 goto err_put;
308         }
309
310         robo.device = devname;
311
312         /* try access using MII ioctls - get phy address */
313         err = do_ioctl(SIOCGMIIPHY);
314         if (err < 0) {
315                 printk(KERN_ERR PFX "error (%i) while accessing MII phy registers with ioctls\n", err);
316                 goto err_put;
317         }
318
319         /* got phy address check for robo address */
320         mii = if_mii(&robo.ifr);
321         if ((mii->phy_id != ROBO_PHY_ADDR) &&
322             (mii->phy_id != ROBO_PHY_ADDR_BCM63XX) &&
323             (mii->phy_id != ROBO_PHY_ADDR_TG3)) {
324                 printk(KERN_ERR PFX "Invalid phy address (%d)\n", mii->phy_id);
325                 err = -ENODEV;
326                 goto err_put;
327         }
328
329 #ifdef CONFIG_BCM47XX
330         robo.gpio_lanports_enable = get_gpio_pin("lanports_enable");
331         if (robo.gpio_lanports_enable >= 0) {
332                 err = gpio_request(robo.gpio_lanports_enable, "lanports_enable");
333                 if (err) {
334                         printk(KERN_ERR PFX "error (%i) requesting lanports_enable gpio (%i)\n",
335                                err, robo.gpio_lanports_enable);
336                         goto err_put;
337                 }
338                 gpio_direction_output(robo.gpio_lanports_enable, 1);
339                 mdelay(5);
340         }
341
342         robo.gpio_robo_reset = get_gpio_pin("robo_reset");
343         if (robo.gpio_robo_reset >= 0) {
344                 err = gpio_request(robo.gpio_robo_reset, "robo_reset");
345                 if (err) {
346                         printk(KERN_ERR PFX "error (%i) requesting robo_reset gpio (%i)\n",
347                                err, robo.gpio_robo_reset);
348                         goto err_gpio_robo;
349                 }
350                 gpio_set_value(robo.gpio_robo_reset, 0);
351                 gpio_direction_output(robo.gpio_robo_reset, 1);
352                 gpio_set_value(robo.gpio_robo_reset, 0);
353                 mdelay(50);
354
355                 gpio_set_value(robo.gpio_robo_reset, 1);
356                 mdelay(20);
357         } else {
358                 // TODO: reset the internal robo switch
359         }
360 #endif
361
362         phyid = mdio_read(ROBO_PHY_ADDR, 0x2) |
363                 (mdio_read(ROBO_PHY_ADDR, 0x3) << 16);
364
365         if (phyid == 0xffffffff || phyid == 0x55210022) {
366                 printk(KERN_ERR PFX "No Robo switch in managed mode found, phy_id = 0x%08x\n", phyid);
367                 err = -ENODEV;
368                 goto err_gpio_lanports;
369         }
370
371         /* Get the device ID */
372         for (i = 0; i < 10; i++) {
373                 robo.devid = robo_read16(ROBO_MGMT_PAGE, ROBO_DEVICE_ID);
374                 if (robo.devid)
375                         break;
376                 udelay(10);
377         }
378         if (!robo.devid)
379                 robo.devid = ROBO_DEVICE_ID_5325; /* Fake it */
380         if (robo.devid == ROBO_DEVICE_ID_5325)
381                 robo.is_5365 = robo_bcm5365();
382         else
383                 robo.is_5365 = false;
384
385         robo.gmii = robo_gmii();
386         if (robo.devid == ROBO_DEVICE_ID_5325) {
387                 for (i = 0; i < 5; i++)
388                         robo.port[i] = i;
389         } else {
390                 for (i = 0; i < 8; i++)
391                         robo.port[i] = i;
392         }
393         robo.port[i] = ROBO_IM_PORT_CTRL;
394
395         robo_switch_reset();
396         err = robo_switch_enable();
397         if (err)
398                 goto err_gpio_lanports;
399
400         printk(KERN_INFO PFX "found a 5%s%x!%s at %s\n", robo.devid & 0xff00 ? "" : "3", robo.devid,
401                 robo.is_5365 ? " It's a BCM5365." : "", devname);
402
403         return 0;
404
405 err_gpio_lanports:
406         if (robo.gpio_lanports_enable >= 0)
407                 gpio_free(robo.gpio_lanports_enable);
408 err_gpio_robo:
409         if (robo.gpio_robo_reset >= 0)
410                 gpio_free(robo.gpio_robo_reset);
411 err_put:
412         dev_put(robo.dev);
413         robo.dev = NULL;
414 err_done:
415         return err;
416 }
417
418
419 static int handle_vlan_port_read(void *driver, char *buf, int nr)
420 {
421         __u16 val16;
422         int len = 0;
423         int j;
424
425         val16 = (nr) /* vlan */ | (0 << 12) /* read */ | (1 << 13) /* enable */;
426
427         if (robo.is_5365) {
428                 robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_TABLE_ACCESS_5365, val16);
429                 /* actual read */
430                 val16 = robo_read16(ROBO_VLAN_PAGE, ROBO_VLAN_READ);
431                 if ((val16 & (1 << 14)) /* valid */) {
432                         for (j = 0; j < 6; j++) {
433                                 if (val16 & (1 << j)) {
434                                         len += sprintf(buf + len, "%d", j);
435                                         if (val16 & (1 << (j + 7))) {
436                                                 if (j == 5)
437                                                         buf[len++] = 'u';
438                                         } else {
439                                                 buf[len++] = 't';
440                                                 if (robo_read16(ROBO_VLAN_PAGE, ROBO_VLAN_PORT0_DEF_TAG + (j << 1)) == nr)
441                                                         buf[len++] = '*';
442                                         }
443                                         buf[len++] = '\t';
444                                 }
445                         }
446                         len += sprintf(buf + len, "\n");
447                 }
448         } else {
449                 u32 val32;
450                 robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_TABLE_ACCESS, val16);
451                 /* actual read */
452                 val32 = robo_read32(ROBO_VLAN_PAGE, ROBO_VLAN_READ);
453                 if ((val32 & (1 << 20)) /* valid */) {
454                         for (j = 0; j < 6; j++) {
455                                 if (val32 & (1 << j)) {
456                                         len += sprintf(buf + len, "%d", j);
457                                         if (val32 & (1 << (j + 6))) {
458                                                 if (j == 5)
459                                                         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_5365) {
508                 robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_WRITE_5365,
509                         (1 << 14)  /* valid */ | (c->untag << 7) | c->port);
510                 robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_TABLE_ACCESS_5365, val16);
511         } else {
512                 robo_write32(ROBO_VLAN_PAGE, ROBO_VLAN_WRITE,
513                         (1 << 20) /* valid */ | (c->untag << 6) | 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_5365) ? VLAN_ID_MAX_5365 : VLAN_ID_MAX); j++) {
647                 /* write config now */
648                 val16 = (j) /* vlan */ | (1 << 12) /* write */ | (1 << 13) /* enable */;
649                 if (robo.is_5365)
650                         robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_WRITE_5365, 0);
651                 else
652                         robo_write32(ROBO_VLAN_PAGE, ROBO_VLAN_WRITE, 0);
653                 robo_write16(ROBO_VLAN_PAGE, robo.is_5365 ? ROBO_VLAN_TABLE_ACCESS_5365 :
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);