switch: reverse if else in handle_vlan_port_read()
[openwrt/staging/mkresin.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 robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_TABLE_ACCESS_5365, val16);
424 /* actual read */
425 val16 = robo_read16(ROBO_VLAN_PAGE, ROBO_VLAN_READ);
426 if ((val16 & (1 << 14)) /* valid */) {
427 for (j = 0; j < 6; j++) {
428 if (val16 & (1 << j)) {
429 len += sprintf(buf + len, "%d", j);
430 if (val16 & (1 << (j + 7))) {
431 if (j == 5)
432 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 u32 val32;
445 robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_TABLE_ACCESS, val16);
446 /* actual read */
447 val32 = robo_read32(ROBO_VLAN_PAGE, ROBO_VLAN_READ);
448 if ((val32 & (1 << 20)) /* valid */) {
449 for (j = 0; j < 6; j++) {
450 if (val32 & (1 << j)) {
451 len += sprintf(buf + len, "%d", j);
452 if (val32 & (1 << (j + 6))) {
453 if (j == 5)
454 buf[len++] = 'u';
455 } else {
456 buf[len++] = 't';
457 if (robo_read16(ROBO_VLAN_PAGE, ROBO_VLAN_PORT0_DEF_TAG + (j << 1)) == nr)
458 buf[len++] = '*';
459 }
460 buf[len++] = '\t';
461 }
462 }
463 len += sprintf(buf + len, "\n");
464 }
465 }
466
467 buf[len] = '\0';
468
469 return len;
470 }
471
472 static int handle_vlan_port_write(void *driver, char *buf, int nr)
473 {
474 switch_driver *d = (switch_driver *) driver;
475 switch_vlan_config *c = switch_parse_vlan(d, buf);
476 int j;
477 __u16 val16;
478
479 if (c == NULL)
480 return -EINVAL;
481
482 for (j = 0; j < d->ports; j++) {
483 if ((c->untag | c->pvid) & (1 << j))
484 /* change default vlan tag */
485 robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_PORT0_DEF_TAG + (j << 1), nr);
486 }
487
488 /* write config now */
489
490 if (robo.devid != ROBO_DEVICE_ID_5325) {
491 __u8 regoff = ((robo.devid == ROBO_DEVICE_ID_5395) ||
492 (robo.devid == ROBO_DEVICE_ID_53115)) ? 0x20 : 0;
493
494 robo_write32(ROBO_ARLIO_PAGE, 0x63 + regoff, (c->untag << 9) | c->port);
495 robo_write16(ROBO_ARLIO_PAGE, 0x61 + regoff, nr);
496 robo_write16(ROBO_ARLIO_PAGE, 0x60 + regoff, 1 << 7);
497 kfree(c);
498 return 0;
499 }
500
501 val16 = (nr) /* vlan */ | (1 << 12) /* write */ | (1 << 13) /* enable */;
502 if (robo.is_5365) {
503 robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_WRITE_5365,
504 (1 << 14) /* valid */ | (c->untag << 7) | c->port);
505 robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_TABLE_ACCESS_5365, val16);
506 } else {
507 robo_write32(ROBO_VLAN_PAGE, ROBO_VLAN_WRITE,
508 (1 << 20) /* valid */ | (c->untag << 6) | c->port);
509 robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_TABLE_ACCESS, val16);
510 }
511
512 kfree(c);
513 return 0;
514 }
515
516 #define set_switch(state) \
517 robo_write16(ROBO_CTRL_PAGE, ROBO_SWITCH_MODE, (robo_read16(ROBO_CTRL_PAGE, ROBO_SWITCH_MODE) & ~2) | (state ? 2 : 0));
518
519 static int handle_enable_read(void *driver, char *buf, int nr)
520 {
521 return sprintf(buf, "%d\n", (((robo_read16(ROBO_CTRL_PAGE, ROBO_SWITCH_MODE) & 2) == 2) ? 1 : 0));
522 }
523
524 static int handle_enable_write(void *driver, char *buf, int nr)
525 {
526 set_switch(buf[0] == '1');
527
528 return 0;
529 }
530
531 static int handle_port_enable_read(void *driver, char *buf, int nr)
532 {
533 return sprintf(buf, "%d\n", ((robo_read16(ROBO_CTRL_PAGE, robo.port[nr]) & 3) == 3 ? 0 : 1));
534 }
535
536 static int handle_port_enable_write(void *driver, char *buf, int nr)
537 {
538 u16 val16;
539
540 if (buf[0] == '0')
541 val16 = 3; /* disabled */
542 else if (buf[0] == '1')
543 val16 = 0; /* enabled */
544 else
545 return -EINVAL;
546
547 robo_write16(ROBO_CTRL_PAGE, robo.port[nr],
548 (robo_read16(ROBO_CTRL_PAGE, robo.port[nr]) & ~3) | val16);
549
550 return 0;
551 }
552
553 static int handle_port_media_read(void *driver, char *buf, int nr)
554 {
555 u16 bmcr = mdio_read(robo.port[nr], MII_BMCR);
556 int media, len;
557
558 if (bmcr & BMCR_ANENABLE)
559 media = SWITCH_MEDIA_AUTO;
560 else {
561 if (bmcr & BMCR_SPEED1000)
562 media = SWITCH_MEDIA_1000;
563 else if (bmcr & BMCR_SPEED100)
564 media = SWITCH_MEDIA_100;
565 else
566 media = 0;
567
568 if (bmcr & BMCR_FULLDPLX)
569 media |= SWITCH_MEDIA_FD;
570 }
571
572 len = switch_print_media(buf, media);
573 return len + sprintf(buf + len, "\n");
574 }
575
576 static int handle_port_media_write(void *driver, char *buf, int nr)
577 {
578 int media = switch_parse_media(buf);
579 u16 bmcr, bmcr_mask;
580
581 if (media & SWITCH_MEDIA_AUTO)
582 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
583 else {
584 if (media & SWITCH_MEDIA_1000) {
585 if (!robo.gmii)
586 return -EINVAL;
587 bmcr = BMCR_SPEED1000;
588 }
589 else if (media & SWITCH_MEDIA_100)
590 bmcr = BMCR_SPEED100;
591 else
592 bmcr = 0;
593
594 if (media & SWITCH_MEDIA_FD)
595 bmcr |= BMCR_FULLDPLX;
596 }
597
598 bmcr_mask = ~(BMCR_SPEED1000 | BMCR_SPEED100 | BMCR_FULLDPLX | BMCR_ANENABLE | BMCR_ANRESTART);
599 mdio_write(robo.port[nr], MII_BMCR,
600 (mdio_read(robo.port[nr], MII_BMCR) & bmcr_mask) | bmcr);
601
602 return 0;
603 }
604
605 static int handle_enable_vlan_read(void *driver, char *buf, int nr)
606 {
607 return sprintf(buf, "%d\n", (((robo_read16(ROBO_VLAN_PAGE, ROBO_VLAN_CTRL0) & (1 << 7)) == (1 << 7)) ? 1 : 0));
608 }
609
610 static int handle_enable_vlan_write(void *driver, char *buf, int nr)
611 {
612 int disable = ((buf[0] != '1') ? 1 : 0);
613
614 robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_CTRL0, disable ? 0 :
615 (1 << 7) /* 802.1Q VLAN */ | (3 << 5) /* mac check and hash */);
616 robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_CTRL1, disable ? 0 :
617 (robo.devid == ROBO_DEVICE_ID_5325 ? (1 << 1) :
618 0) | (1 << 2) | (1 << 3)); /* RSV multicast */
619
620 if (robo.devid != ROBO_DEVICE_ID_5325)
621 return 0;
622
623 robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_CTRL4, disable ? 0 :
624 (1 << 6) /* drop invalid VID frames */);
625 robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_CTRL5, disable ? 0 :
626 (1 << 3) /* drop miss V table frames */);
627
628 return 0;
629 }
630
631 static int handle_reset(void *driver, char *buf, int nr)
632 {
633 switch_driver *d = (switch_driver *) driver;
634 int j;
635 __u16 val16;
636
637 /* disable switching */
638 set_switch(0);
639
640 /* reset vlans */
641 for (j = 0; j <= ((robo.is_5365) ? VLAN_ID_MAX_5365 : VLAN_ID_MAX); j++) {
642 /* write config now */
643 val16 = (j) /* vlan */ | (1 << 12) /* write */ | (1 << 13) /* enable */;
644 if (robo.is_5365)
645 robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_WRITE_5365, 0);
646 else
647 robo_write32(ROBO_VLAN_PAGE, ROBO_VLAN_WRITE, 0);
648 robo_write16(ROBO_VLAN_PAGE, robo.is_5365 ? ROBO_VLAN_TABLE_ACCESS_5365 :
649 ROBO_VLAN_TABLE_ACCESS,
650 val16);
651 }
652
653 /* reset ports to a known good state */
654 for (j = 0; j < d->ports; j++) {
655 robo_write16(ROBO_CTRL_PAGE, robo.port[j], 0x0000);
656 robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_PORT0_DEF_TAG + (j << 1), 0);
657 }
658
659 /* enable switching */
660 set_switch(1);
661
662 /* enable vlans */
663 handle_enable_vlan_write(driver, "1", 0);
664
665 return 0;
666 }
667
668 static int __init robo_init(void)
669 {
670 int notfound = 1;
671 char *device;
672
673 device = strdup("ethX");
674 for (device[3] = '0'; (device[3] <= '3') && notfound; device[3]++) {
675 if (! switch_device_registered (device))
676 notfound = robo_probe(device);
677 }
678 device[3]--;
679
680 if (notfound) {
681 kfree(device);
682 return -ENODEV;
683 } else {
684 static const switch_config cfg[] = {
685 {
686 .name = "enable",
687 .read = handle_enable_read,
688 .write = handle_enable_write
689 }, {
690 .name = "enable_vlan",
691 .read = handle_enable_vlan_read,
692 .write = handle_enable_vlan_write
693 }, {
694 .name = "reset",
695 .read = NULL,
696 .write = handle_reset
697 }, { NULL, },
698 };
699 static const switch_config port[] = {
700 {
701 .name = "enable",
702 .read = handle_port_enable_read,
703 .write = handle_port_enable_write
704 }, {
705 .name = "media",
706 .read = handle_port_media_read,
707 .write = handle_port_media_write
708 }, { NULL, },
709 };
710 static const switch_config vlan[] = {
711 {
712 .name = "ports",
713 .read = handle_vlan_port_read,
714 .write = handle_vlan_port_write
715 }, { NULL, },
716 };
717 switch_driver driver = {
718 .name = DRIVER_NAME,
719 .version = DRIVER_VERSION,
720 .interface = device,
721 .cpuport = 5,
722 .ports = 6,
723 .vlans = 16,
724 .driver_handlers = cfg,
725 .port_handlers = port,
726 .vlan_handlers = vlan,
727 };
728 if (robo.devid != ROBO_DEVICE_ID_5325) {
729 driver.ports = 9;
730 driver.cpuport = 8;
731 }
732
733 return switch_register_driver(&driver);
734 }
735 }
736
737 static void __exit robo_exit(void)
738 {
739 switch_unregister_driver(DRIVER_NAME);
740 if (robo.dev)
741 dev_put(robo.dev);
742 if (robo.gpio_robo_reset >= 0)
743 gpio_free(robo.gpio_robo_reset);
744 if (robo.gpio_lanports_enable >= 0)
745 gpio_free(robo.gpio_lanports_enable);
746 kfree(robo.device);
747 }
748
749
750 MODULE_AUTHOR("Felix Fietkau <openwrt@nbd.name>");
751 MODULE_LICENSE("GPL");
752
753 module_init(robo_init);
754 module_exit(robo_exit);