switch: the BCM5365 is a special switch and the BCM5350 or BCM5325 is the normal...
[librecmc/librecmc.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[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 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         for (i = 0; i < 5; i++)
312                 robo.port[i] = i;
313         robo.port[5] = 8;
314
315         /* try access using MII ioctls - get phy address */
316         err = do_ioctl(SIOCGMIIPHY);
317         if (err < 0) {
318                 printk(KERN_ERR PFX "error (%i) while accessing MII phy registers with ioctls\n", err);
319                 goto err_put;
320         }
321
322         /* got phy address check for robo address */
323         mii = if_mii(&robo.ifr);
324         if ((mii->phy_id != ROBO_PHY_ADDR) &&
325             (mii->phy_id != ROBO_PHY_ADDR_BCM63XX) &&
326             (mii->phy_id != ROBO_PHY_ADDR_TG3)) {
327                 printk(KERN_ERR PFX "Invalid phy address (%d)\n", mii->phy_id);
328                 err = -ENODEV;
329                 goto err_put;
330         }
331
332 #ifdef CONFIG_BCM47XX
333         robo.gpio_lanports_enable = get_gpio_pin("lanports_enable");
334         if (robo.gpio_lanports_enable >= 0) {
335                 err = gpio_request(robo.gpio_lanports_enable, "lanports_enable");
336                 if (err) {
337                         printk(KERN_ERR PFX "error (%i) requesting lanports_enable gpio (%i)\n",
338                                err, robo.gpio_lanports_enable);
339                         goto err_put;
340                 }
341                 gpio_direction_output(robo.gpio_lanports_enable, 1);
342                 mdelay(5);
343         }
344
345         robo.gpio_robo_reset = get_gpio_pin("robo_reset");
346         if (robo.gpio_robo_reset >= 0) {
347                 err = gpio_request(robo.gpio_robo_reset, "robo_reset");
348                 if (err) {
349                         printk(KERN_ERR PFX "error (%i) requesting robo_reset gpio (%i)\n",
350                                err, robo.gpio_robo_reset);
351                         goto err_gpio_robo;
352                 }
353                 gpio_set_value(robo.gpio_robo_reset, 0);
354                 gpio_direction_output(robo.gpio_robo_reset, 1);
355                 gpio_set_value(robo.gpio_robo_reset, 0);
356                 mdelay(50);
357
358                 gpio_set_value(robo.gpio_robo_reset, 1);
359                 mdelay(20);
360         } else {
361                 // TODO: reset the internal robo switch
362         }
363 #endif
364
365         phyid = mdio_read(ROBO_PHY_ADDR, 0x2) |
366                 (mdio_read(ROBO_PHY_ADDR, 0x3) << 16);
367
368         if (phyid == 0xffffffff || phyid == 0x55210022) {
369                 printk(KERN_ERR PFX "No Robo switch in managed mode found, phy_id = 0x%08x\n", phyid);
370                 err = -ENODEV;
371                 goto err_gpio_lanports;
372         }
373
374         /* Get the device ID */
375         for (i = 0; i < 10; i++) {
376                 robo.devid = robo_read16(ROBO_MGMT_PAGE, ROBO_DEVICE_ID);
377                 if (robo.devid)
378                         break;
379                 udelay(10);
380         }
381         if (!robo.devid)
382                 robo.devid = ROBO_DEVICE_ID_5325; /* Fake it */
383         if (robo.devid == ROBO_DEVICE_ID_5325)
384                 robo.is_5365 = robo_bcm5365();
385         else
386                 robo.is_5365 = false;
387
388         robo.gmii = robo_gmii();
389
390         robo_switch_reset();
391         err = robo_switch_enable();
392         if (err)
393                 goto err_gpio_lanports;
394
395         printk(KERN_INFO PFX "found a 5%s%x!%s at %s\n", robo.devid & 0xff00 ? "" : "3", robo.devid,
396                 robo.is_5365 ? " It's a BCM5365." : "", devname);
397
398         return 0;
399
400 err_gpio_lanports:
401         if (robo.gpio_lanports_enable >= 0)
402                 gpio_free(robo.gpio_lanports_enable);
403 err_gpio_robo:
404         if (robo.gpio_robo_reset >= 0)
405                 gpio_free(robo.gpio_robo_reset);
406 err_put:
407         dev_put(robo.dev);
408         robo.dev = NULL;
409 err_done:
410         return err;
411 }
412
413
414 static int handle_vlan_port_read(void *driver, char *buf, int nr)
415 {
416         __u16 val16;
417         int len = 0;
418         int j;
419
420         val16 = (nr) /* vlan */ | (0 << 12) /* read */ | (1 << 13) /* enable */;
421
422         if (!robo.is_5365) {
423                 u32 val32;
424                 robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_TABLE_ACCESS, val16);
425                 /* actual read */
426                 val32 = robo_read32(ROBO_VLAN_PAGE, ROBO_VLAN_READ);
427                 if ((val32 & (1 << 20)) /* valid */) {
428                         for (j = 0; j < 6; j++) {
429                                 if (val32 & (1 << j)) {
430                                         len += sprintf(buf + len, "%d", j);
431                                         if (val32 & (1 << (j + 6))) {
432                                                 if (j == 5) buf[len++] = 'u';
433                                         } else {
434                                                 buf[len++] = 't';
435                                                 if (robo_read16(ROBO_VLAN_PAGE, ROBO_VLAN_PORT0_DEF_TAG + (j << 1)) == nr)
436                                                         buf[len++] = '*';
437                                         }
438                                         buf[len++] = '\t';
439                                 }
440                         }
441                         len += sprintf(buf + len, "\n");
442                 }
443         } else {
444                 robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_TABLE_ACCESS_5365, val16);
445                 /* actual read */
446                 val16 = robo_read16(ROBO_VLAN_PAGE, ROBO_VLAN_READ);
447                 if ((val16 & (1 << 14)) /* valid */) {
448                         for (j = 0; j < 6; j++) {
449                                 if (val16 & (1 << j)) {
450                                         len += sprintf(buf + len, "%d", j);
451                                         if (val16 & (1 << (j + 7))) {
452                                                 if (j == 5) buf[len++] = 'u';
453                                         } else {
454                                                 buf[len++] = 't';
455                                                 if (robo_read16(ROBO_VLAN_PAGE, ROBO_VLAN_PORT0_DEF_TAG + (j << 1)) == nr)
456                                                         buf[len++] = '*';
457                                         }
458                                         buf[len++] = '\t';
459                                 }
460                         }
461                         len += sprintf(buf + len, "\n");
462                 }
463         }
464
465         buf[len] = '\0';
466
467         return len;
468 }
469
470 static int handle_vlan_port_write(void *driver, char *buf, int nr)
471 {
472         switch_driver *d = (switch_driver *) driver;
473         switch_vlan_config *c = switch_parse_vlan(d, buf);
474         int j;
475         __u16 val16;
476
477         if (c == NULL)
478                 return -EINVAL;
479
480         for (j = 0; j < d->ports; j++) {
481                 if ((c->untag | c->pvid) & (1 << j))
482                         /* change default vlan tag */
483                         robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_PORT0_DEF_TAG + (j << 1), nr);
484         }
485
486         /* write config now */
487
488         if (robo.devid != ROBO_DEVICE_ID_5325) {
489                 __u8 regoff = ((robo.devid == ROBO_DEVICE_ID_5395) ||
490                         (robo.devid == ROBO_DEVICE_ID_53115)) ? 0x20 : 0;
491
492                 robo_write32(ROBO_ARLIO_PAGE, 0x63 + regoff, (c->untag << 9) | c->port);
493                 robo_write16(ROBO_ARLIO_PAGE, 0x61 + regoff, nr);
494                 robo_write16(ROBO_ARLIO_PAGE, 0x60 + regoff, 1 << 7);
495                 kfree(c);
496                 return 0;
497         }
498
499         val16 = (nr) /* vlan */ | (1 << 12) /* write */ | (1 << 13) /* enable */;
500         if (robo.is_5365) {
501                 robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_WRITE_5365,
502                         (1 << 14)  /* valid */ | (c->untag << 7) | c->port);
503                 robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_TABLE_ACCESS_5365, val16);
504         } else {
505                 robo_write32(ROBO_VLAN_PAGE, ROBO_VLAN_WRITE,
506                         (1 << 20) /* valid */ | (c->untag << 6) | c->port);
507                 robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_TABLE_ACCESS, val16);
508         }
509
510         kfree(c);
511         return 0;
512 }
513
514 #define set_switch(state) \
515         robo_write16(ROBO_CTRL_PAGE, ROBO_SWITCH_MODE, (robo_read16(ROBO_CTRL_PAGE, ROBO_SWITCH_MODE) & ~2) | (state ? 2 : 0));
516
517 static int handle_enable_read(void *driver, char *buf, int nr)
518 {
519         return sprintf(buf, "%d\n", (((robo_read16(ROBO_CTRL_PAGE, ROBO_SWITCH_MODE) & 2) == 2) ? 1 : 0));
520 }
521
522 static int handle_enable_write(void *driver, char *buf, int nr)
523 {
524         set_switch(buf[0] == '1');
525
526         return 0;
527 }
528
529 static int handle_port_enable_read(void *driver, char *buf, int nr)
530 {
531         return sprintf(buf, "%d\n", ((robo_read16(ROBO_CTRL_PAGE, robo.port[nr]) & 3) == 3 ? 0 : 1));
532 }
533
534 static int handle_port_enable_write(void *driver, char *buf, int nr)
535 {
536         u16 val16;
537
538         if (buf[0] == '0')
539                 val16 = 3; /* disabled */
540         else if (buf[0] == '1')
541                 val16 = 0; /* enabled */
542         else
543                 return -EINVAL;
544
545         robo_write16(ROBO_CTRL_PAGE, robo.port[nr],
546                 (robo_read16(ROBO_CTRL_PAGE, robo.port[nr]) & ~3) | val16);
547
548         return 0;
549 }
550
551 static int handle_port_media_read(void *driver, char *buf, int nr)
552 {
553         u16 bmcr = mdio_read(robo.port[nr], MII_BMCR);
554         int media, len;
555
556         if (bmcr & BMCR_ANENABLE)
557                 media = SWITCH_MEDIA_AUTO;
558         else {
559                 if (bmcr & BMCR_SPEED1000)
560                         media = SWITCH_MEDIA_1000;
561                 else if (bmcr & BMCR_SPEED100)
562                         media = SWITCH_MEDIA_100;
563                 else
564                         media = 0;
565
566                 if (bmcr & BMCR_FULLDPLX)
567                         media |= SWITCH_MEDIA_FD;
568         }
569
570         len = switch_print_media(buf, media);
571         return len + sprintf(buf + len, "\n");
572 }
573
574 static int handle_port_media_write(void *driver, char *buf, int nr)
575 {
576         int media = switch_parse_media(buf);
577         u16 bmcr, bmcr_mask;
578
579         if (media & SWITCH_MEDIA_AUTO)
580                 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
581         else {
582                 if (media & SWITCH_MEDIA_1000) {
583                         if (!robo.gmii)
584                                 return -EINVAL;
585                         bmcr = BMCR_SPEED1000;
586                 }
587                 else if (media & SWITCH_MEDIA_100)
588                         bmcr = BMCR_SPEED100;
589                 else
590                         bmcr = 0;
591
592                 if (media & SWITCH_MEDIA_FD)
593                         bmcr |= BMCR_FULLDPLX;
594         }
595
596         bmcr_mask = ~(BMCR_SPEED1000 | BMCR_SPEED100 | BMCR_FULLDPLX | BMCR_ANENABLE | BMCR_ANRESTART);
597         mdio_write(robo.port[nr], MII_BMCR,
598                 (mdio_read(robo.port[nr], MII_BMCR) & bmcr_mask) | bmcr);
599
600         return 0;
601 }
602
603 static int handle_enable_vlan_read(void *driver, char *buf, int nr)
604 {
605         return sprintf(buf, "%d\n", (((robo_read16(ROBO_VLAN_PAGE, ROBO_VLAN_CTRL0) & (1 << 7)) == (1 << 7)) ? 1 : 0));
606 }
607
608 static int handle_enable_vlan_write(void *driver, char *buf, int nr)
609 {
610         int disable = ((buf[0] != '1') ? 1 : 0);
611
612         robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_CTRL0, disable ? 0 :
613                 (1 << 7) /* 802.1Q VLAN */ | (3 << 5) /* mac check and hash */);
614         robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_CTRL1, disable ? 0 :
615                 (robo.devid == ROBO_DEVICE_ID_5325 ? (1 << 1) :
616                 0) | (1 << 2) | (1 << 3)); /* RSV multicast */
617
618         if (robo.devid != ROBO_DEVICE_ID_5325)
619                 return 0;
620
621         robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_CTRL4, disable ? 0 :
622                 (1 << 6) /* drop invalid VID frames */);
623         robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_CTRL5, disable ? 0 :
624                 (1 << 3) /* drop miss V table frames */);
625
626         return 0;
627 }
628
629 static int handle_reset(void *driver, char *buf, int nr)
630 {
631         switch_driver *d = (switch_driver *) driver;
632         int j;
633         __u16 val16;
634
635         /* disable switching */
636         set_switch(0);
637
638         /* reset vlans */
639         for (j = 0; j <= ((robo.is_5365) ? VLAN_ID_MAX_5365 : VLAN_ID_MAX); j++) {
640                 /* write config now */
641                 val16 = (j) /* vlan */ | (1 << 12) /* write */ | (1 << 13) /* enable */;
642                 if (robo.is_5365)
643                         robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_WRITE_5365, 0);
644                 else
645                         robo_write32(ROBO_VLAN_PAGE, ROBO_VLAN_WRITE, 0);
646                 robo_write16(ROBO_VLAN_PAGE, robo.is_5365 ? ROBO_VLAN_TABLE_ACCESS_5365 :
647                                                             ROBO_VLAN_TABLE_ACCESS,
648                              val16);
649         }
650
651         /* reset ports to a known good state */
652         for (j = 0; j < d->ports; j++) {
653                 robo_write16(ROBO_CTRL_PAGE, robo.port[j], 0x0000);
654                 robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_PORT0_DEF_TAG + (j << 1), 0);
655         }
656
657         /* enable switching */
658         set_switch(1);
659
660         /* enable vlans */
661         handle_enable_vlan_write(driver, "1", 0);
662
663         return 0;
664 }
665
666 static int __init robo_init(void)
667 {
668         int notfound = 1;
669         char *device;
670
671         device = strdup("ethX");
672         for (device[3] = '0'; (device[3] <= '3') && notfound; device[3]++) {
673                 if (! switch_device_registered (device))
674                         notfound = robo_probe(device);
675         }
676         device[3]--;
677
678         if (notfound) {
679                 kfree(device);
680                 return -ENODEV;
681         } else {
682                 static const switch_config cfg[] = {
683                         {
684                                 .name   = "enable",
685                                 .read   = handle_enable_read,
686                                 .write  = handle_enable_write
687                         }, {
688                                 .name   = "enable_vlan",
689                                 .read   = handle_enable_vlan_read,
690                                 .write  = handle_enable_vlan_write
691                         }, {
692                                 .name   = "reset",
693                                 .read   = NULL,
694                                 .write  = handle_reset
695                         }, { NULL, },
696                 };
697                 static const switch_config port[] = {
698                         {
699                                 .name   = "enable",
700                                 .read   = handle_port_enable_read,
701                                 .write  = handle_port_enable_write
702                         }, {
703                                 .name   = "media",
704                                 .read   = handle_port_media_read,
705                                 .write  = handle_port_media_write
706                         }, { NULL, },
707                 };
708                 static const switch_config vlan[] = {
709                         {
710                                 .name   = "ports",
711                                 .read   = handle_vlan_port_read,
712                                 .write  = handle_vlan_port_write
713                         }, { NULL, },
714                 };
715                 switch_driver driver = {
716                         .name                   = DRIVER_NAME,
717                         .version                = DRIVER_VERSION,
718                         .interface              = device,
719                         .cpuport                = 5,
720                         .ports                  = 6,
721                         .vlans                  = 16,
722                         .driver_handlers        = cfg,
723                         .port_handlers          = port,
724                         .vlan_handlers          = vlan,
725                 };
726                 if (robo.devid != ROBO_DEVICE_ID_5325) {
727                         driver.ports = 9;
728                         driver.cpuport = 8;
729                 }
730
731                 return switch_register_driver(&driver);
732         }
733 }
734
735 static void __exit robo_exit(void)
736 {
737         switch_unregister_driver(DRIVER_NAME);
738         if (robo.dev)
739                 dev_put(robo.dev);
740         if (robo.gpio_robo_reset >= 0)
741                 gpio_free(robo.gpio_robo_reset);
742         if (robo.gpio_lanports_enable >= 0)
743                 gpio_free(robo.gpio_lanports_enable);
744         kfree(robo.device);
745 }
746
747
748 MODULE_AUTHOR("Felix Fietkau <openwrt@nbd.name>");
749 MODULE_LICENSE("GPL");
750
751 module_init(robo_init);
752 module_exit(robo_exit);