switch: read and write vlan config from and to gigabit switches
[openwrt/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 u8 corerev;
78 int gpio_robo_reset;
79 int gpio_lanports_enable;
80 struct ifreq ifr;
81 struct net_device *dev;
82 unsigned char port[9];
83 };
84
85 /* Currently we can only have one device in the system. */
86 static struct robo_switch robo;
87
88
89 static int do_ioctl(int cmd)
90 {
91 mm_segment_t old_fs = get_fs();
92 int ret;
93
94 set_fs(KERNEL_DS);
95 ret = robo.dev->netdev_ops->ndo_do_ioctl(robo.dev, &robo.ifr, cmd);
96 set_fs(old_fs);
97
98 return ret;
99 }
100
101 static u16 mdio_read(__u16 phy_id, __u8 reg)
102 {
103 struct mii_ioctl_data *mii = if_mii(&robo.ifr);
104 int err;
105
106 mii->phy_id = phy_id;
107 mii->reg_num = reg;
108
109 err = do_ioctl(SIOCGMIIREG);
110 if (err < 0) {
111 printk(KERN_ERR PFX "failed to read mdio reg %i with err %i.\n", reg, err);
112
113 return 0xffff;
114 }
115
116 return mii->val_out;
117 }
118
119 static void mdio_write(__u16 phy_id, __u8 reg, __u16 val)
120 {
121 struct mii_ioctl_data *mii = if_mii(&robo.ifr);
122 int err;
123
124 mii->phy_id = phy_id;
125 mii->reg_num = reg;
126 mii->val_in = val;
127
128 err = do_ioctl(SIOCSMIIREG);
129 if (err < 0) {
130 printk(KERN_ERR PFX "failed to write mdio reg: %i with err %i.\n", reg, err);
131 return;
132 }
133 }
134
135 static int robo_reg(__u8 page, __u8 reg, __u8 op)
136 {
137 int i = 3;
138
139 /* set page number */
140 mdio_write(ROBO_PHY_ADDR, REG_MII_PAGE,
141 (page << 8) | REG_MII_PAGE_ENABLE);
142
143 /* set register address */
144 mdio_write(ROBO_PHY_ADDR, REG_MII_ADDR,
145 (reg << 8) | op);
146
147 /* check if operation completed */
148 while (i--) {
149 if ((mdio_read(ROBO_PHY_ADDR, REG_MII_ADDR) & 3) == 0)
150 return 0;
151 }
152
153 printk(KERN_ERR PFX "timeout in robo_reg on page %i and reg %i with op %i.\n", page, reg, op);
154
155 return 1;
156 }
157
158 /*
159 static void robo_read(__u8 page, __u8 reg, __u16 *val, int count)
160 {
161 int i;
162
163 robo_reg(page, reg, REG_MII_ADDR_READ);
164
165 for (i = 0; i < count; i++)
166 val[i] = mdio_read(ROBO_PHY_ADDR, REG_MII_DATA0 + i);
167 }
168 */
169
170 static __u16 robo_read16(__u8 page, __u8 reg)
171 {
172 robo_reg(page, reg, REG_MII_ADDR_READ);
173
174 return mdio_read(ROBO_PHY_ADDR, REG_MII_DATA0);
175 }
176
177 static __u32 robo_read32(__u8 page, __u8 reg)
178 {
179 robo_reg(page, reg, REG_MII_ADDR_READ);
180
181 return mdio_read(ROBO_PHY_ADDR, REG_MII_DATA0) |
182 (mdio_read(ROBO_PHY_ADDR, REG_MII_DATA0 + 1) << 16);
183 }
184
185 static void robo_write16(__u8 page, __u8 reg, __u16 val16)
186 {
187 /* write data */
188 mdio_write(ROBO_PHY_ADDR, REG_MII_DATA0, val16);
189
190 robo_reg(page, reg, REG_MII_ADDR_WRITE);
191 }
192
193 static void robo_write32(__u8 page, __u8 reg, __u32 val32)
194 {
195 /* write data */
196 mdio_write(ROBO_PHY_ADDR, REG_MII_DATA0, val32 & 0xFFFF);
197 mdio_write(ROBO_PHY_ADDR, REG_MII_DATA0 + 1, val32 >> 16);
198
199 robo_reg(page, reg, REG_MII_ADDR_WRITE);
200 }
201
202 /* checks that attached switch is 5365 */
203 static bool robo_bcm5365(void)
204 {
205 /* set vlan access id to 15 and read it back */
206 __u16 val16 = 15;
207 robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_TABLE_ACCESS, val16);
208
209 /* 5365 will refuse this as it does not have this reg */
210 return robo_read16(ROBO_VLAN_PAGE, ROBO_VLAN_TABLE_ACCESS) != val16;
211 }
212
213 static bool robo_gmii(void)
214 {
215 if (mdio_read(0, ROBO_MII_STAT) & 0x0100)
216 return ((mdio_read(0, 0x0f) & 0xf000) != 0);
217 return false;
218 }
219
220 static int robo_switch_enable(void)
221 {
222 unsigned int i, last_port;
223 u16 val;
224 #ifdef CONFIG_BCM47XX
225 char buf[20];
226 #endif
227
228 val = robo_read16(ROBO_CTRL_PAGE, ROBO_SWITCH_MODE);
229 if (!(val & (1 << 1))) {
230 /* Unmanaged mode */
231 val &= ~(1 << 0);
232 /* With forwarding */
233 val |= (1 << 1);
234 robo_write16(ROBO_CTRL_PAGE, ROBO_SWITCH_MODE, val);
235 val = robo_read16(ROBO_CTRL_PAGE, ROBO_SWITCH_MODE);
236 if (!(val & (1 << 1))) {
237 printk(KERN_ERR PFX "Failed to enable switch\n");
238 return -EBUSY;
239 }
240
241 /* No spanning tree for unmanaged mode */
242 last_port = (robo.devid == ROBO_DEVICE_ID_5398) ?
243 ROBO_PORT7_CTRL : ROBO_PORT4_CTRL;
244 for (i = ROBO_PORT0_CTRL; i <= last_port; i++)
245 robo_write16(ROBO_CTRL_PAGE, i, 0);
246
247 /* No spanning tree on IMP port too */
248 robo_write16(ROBO_CTRL_PAGE, ROBO_IM_PORT_CTRL, 0);
249 }
250
251 #ifdef CONFIG_BCM47XX
252 /* WAN port LED, except for Netgear WGT634U */
253 if (bcm47xx_nvram_getenv("nvram_type", buf, sizeof(buf)) >= 0) {
254 if (strcmp(buf, "cfe") != 0)
255 robo_write16(ROBO_CTRL_PAGE, 0x16, 0x1F);
256 }
257 #endif
258 return 0;
259 }
260
261 static void robo_switch_reset(void)
262 {
263 if ((robo.devid == ROBO_DEVICE_ID_5395) ||
264 (robo.devid == ROBO_DEVICE_ID_5397) ||
265 (robo.devid == ROBO_DEVICE_ID_5398)) {
266 /* Trigger a software reset. */
267 robo_write16(ROBO_CTRL_PAGE, 0x79, 0x83);
268 mdelay(500);
269 robo_write16(ROBO_CTRL_PAGE, 0x79, 0);
270 }
271 }
272
273 #ifdef CONFIG_BCM47XX
274 static int get_gpio_pin(const char *name)
275 {
276 int i, err;
277 char nvram_var[10];
278 char buf[30];
279
280 for (i = 0; i < 16; i++) {
281 err = snprintf(nvram_var, sizeof(nvram_var), "gpio%i", i);
282 if (err <= 0)
283 continue;
284 err = bcm47xx_nvram_getenv(nvram_var, buf, sizeof(buf));
285 if (err <= 0)
286 continue;
287 if (!strcmp(name, buf))
288 return i;
289 }
290 return -1;
291 }
292 #endif
293
294 static int robo_probe(char *devname)
295 {
296 __u32 phyid;
297 unsigned int i;
298 int err = -1;
299 struct mii_ioctl_data *mii;
300
301 printk(KERN_INFO PFX "Probing device '%s'\n", devname);
302 strcpy(robo.ifr.ifr_name, devname);
303
304 if ((robo.dev = dev_get_by_name(&init_net, devname)) == NULL) {
305 printk(KERN_ERR PFX "No such device\n");
306 err = -ENODEV;
307 goto err_done;
308 }
309 if (!robo.dev->netdev_ops || !robo.dev->netdev_ops->ndo_do_ioctl) {
310 printk(KERN_ERR PFX "ndo_do_ioctl not implemented in ethernet driver\n");
311 err = -ENXIO;
312 goto err_put;
313 }
314
315 robo.device = devname;
316
317 /* try access using MII ioctls - get phy address */
318 err = do_ioctl(SIOCGMIIPHY);
319 if (err < 0) {
320 printk(KERN_ERR PFX "error (%i) while accessing MII phy registers with ioctls\n", err);
321 goto err_put;
322 }
323
324 /* got phy address check for robo address */
325 mii = if_mii(&robo.ifr);
326 if ((mii->phy_id != ROBO_PHY_ADDR) &&
327 (mii->phy_id != ROBO_PHY_ADDR_BCM63XX) &&
328 (mii->phy_id != ROBO_PHY_ADDR_TG3)) {
329 printk(KERN_ERR PFX "Invalid phy address (%d)\n", mii->phy_id);
330 err = -ENODEV;
331 goto err_put;
332 }
333
334 #ifdef CONFIG_BCM47XX
335 robo.gpio_lanports_enable = get_gpio_pin("lanports_enable");
336 if (robo.gpio_lanports_enable >= 0) {
337 err = gpio_request(robo.gpio_lanports_enable, "lanports_enable");
338 if (err) {
339 printk(KERN_ERR PFX "error (%i) requesting lanports_enable gpio (%i)\n",
340 err, robo.gpio_lanports_enable);
341 goto err_put;
342 }
343 gpio_direction_output(robo.gpio_lanports_enable, 1);
344 mdelay(5);
345 }
346
347 robo.gpio_robo_reset = get_gpio_pin("robo_reset");
348 if (robo.gpio_robo_reset >= 0) {
349 err = gpio_request(robo.gpio_robo_reset, "robo_reset");
350 if (err) {
351 printk(KERN_ERR PFX "error (%i) requesting robo_reset gpio (%i)\n",
352 err, robo.gpio_robo_reset);
353 goto err_gpio_robo;
354 }
355 gpio_set_value(robo.gpio_robo_reset, 0);
356 gpio_direction_output(robo.gpio_robo_reset, 1);
357 gpio_set_value(robo.gpio_robo_reset, 0);
358 mdelay(50);
359
360 gpio_set_value(robo.gpio_robo_reset, 1);
361 mdelay(20);
362 } else {
363 // TODO: reset the internal robo switch
364 }
365 #endif
366
367 phyid = mdio_read(ROBO_PHY_ADDR, 0x2) |
368 (mdio_read(ROBO_PHY_ADDR, 0x3) << 16);
369
370 if (phyid == 0xffffffff || phyid == 0x55210022) {
371 printk(KERN_ERR PFX "No Robo switch in managed mode found, phy_id = 0x%08x\n", phyid);
372 err = -ENODEV;
373 goto err_gpio_lanports;
374 }
375
376 /* Get the device ID */
377 for (i = 0; i < 10; i++) {
378 robo.devid = robo_read16(ROBO_MGMT_PAGE, ROBO_DEVICE_ID);
379 if (robo.devid)
380 break;
381 udelay(10);
382 }
383 if (!robo.devid)
384 robo.devid = ROBO_DEVICE_ID_5325; /* Fake it */
385 if (robo.devid == ROBO_DEVICE_ID_5325)
386 robo.is_5365 = robo_bcm5365();
387 else
388 robo.is_5365 = false;
389
390 robo.gmii = robo_gmii();
391 if (robo.devid == ROBO_DEVICE_ID_5325) {
392 for (i = 0; i < 5; i++)
393 robo.port[i] = i;
394 } else {
395 for (i = 0; i < 8; i++)
396 robo.port[i] = i;
397 }
398 robo.port[i] = ROBO_IM_PORT_CTRL;
399
400 robo_switch_reset();
401 err = robo_switch_enable();
402 if (err)
403 goto err_gpio_lanports;
404
405 printk(KERN_INFO PFX "found a 5%s%x!%s at %s\n", robo.devid & 0xff00 ? "" : "3", robo.devid,
406 robo.is_5365 ? " It's a BCM5365." : "", devname);
407
408 return 0;
409
410 err_gpio_lanports:
411 if (robo.gpio_lanports_enable >= 0)
412 gpio_free(robo.gpio_lanports_enable);
413 err_gpio_robo:
414 if (robo.gpio_robo_reset >= 0)
415 gpio_free(robo.gpio_robo_reset);
416 err_put:
417 dev_put(robo.dev);
418 robo.dev = NULL;
419 err_done:
420 return err;
421 }
422
423 static int handle_vlan_port_read_old(switch_driver *d, 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 < d->ports; j++) {
437 if (val16 & (1 << j)) {
438 len += sprintf(buf + len, "%d", j);
439 if (val16 & (1 << (j + 7))) {
440 if (j == d->cpuport)
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 < d->ports; j++) {
459 if (val32 & (1 << j)) {
460 len += sprintf(buf + len, "%d", j);
461 if (val32 & (1 << (j + d->ports))) {
462 if (j == d->cpuport)
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_read_new(switch_driver *d, char *buf, int nr)
482 {
483 __u8 vtbl_entry, vtbl_index, vtbl_access;
484 __u32 val32;
485 int len = 0;
486 int j;
487
488 if ((robo.devid == ROBO_DEVICE_ID_5395) ||
489 (robo.devid == ROBO_DEVICE_ID_53115)) {
490 vtbl_access = ROBO_VTBL_ACCESS_5395;
491 vtbl_index = ROBO_VTBL_INDX_5395;
492 vtbl_entry = ROBO_VTBL_ENTRY_5395;
493 } else {
494 vtbl_access = ROBO_VTBL_ACCESS;
495 vtbl_index = ROBO_VTBL_INDX;
496 vtbl_entry = ROBO_VTBL_ENTRY;
497 }
498
499 robo_write16(ROBO_ARLIO_PAGE, vtbl_index, nr);
500 robo_write16(ROBO_ARLIO_PAGE, vtbl_access, (1 << 7) | (1 << 0));
501 val32 = robo_read32(ROBO_ARLIO_PAGE, vtbl_entry);
502 for (j = 0; j < d->ports; j++) {
503 if (val32 & (1 << j)) {
504 len += sprintf(buf + len, "%d", j);
505 if (val32 & (1 << (j + d->ports))) {
506 if (j == d->cpuport)
507 buf[len++] = 'u';
508 } else {
509 buf[len++] = 't';
510 if (robo_read16(ROBO_VLAN_PAGE, ROBO_VLAN_PORT0_DEF_TAG + (j << 1)) == nr)
511 buf[len++] = '*';
512 }
513 buf[len++] = '\t';
514 }
515 }
516 len += sprintf(buf + len, "\n");
517 buf[len] = '\0';
518 return len;
519 }
520
521 static int handle_vlan_port_read(void *driver, char *buf, int nr)
522 {
523 switch_driver *d = (switch_driver *) driver;
524
525 if (robo.devid != ROBO_DEVICE_ID_5325)
526 return handle_vlan_port_read_new(d, buf, nr);
527 else
528 return handle_vlan_port_read_old(d, buf, nr);
529 }
530
531 static void handle_vlan_port_write_old(switch_driver *d, switch_vlan_config *c, int nr)
532 {
533 __u16 val16;
534 __u32 val32;
535 __u32 untag = ((c->untag & ~(1 << d->cpuport)) << d->ports);
536
537 /* write config now */
538 val16 = (nr) /* vlan */ | (1 << 12) /* write */ | (1 << 13) /* enable */;
539 if (robo.is_5365) {
540 robo_write32(ROBO_VLAN_PAGE, ROBO_VLAN_WRITE_5365,
541 (1 << 14) /* valid */ | (untag << 1 ) | c->port);
542 robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_TABLE_ACCESS_5365, val16);
543 } else {
544 if (robo.corerev < 3)
545 val32 = (1 << 20) | ((nr >> 4) << 12) | untag | c->port;
546 else
547 val32 = (1 << 24) | (nr << 12) | untag | c->port;
548 robo_write32(ROBO_VLAN_PAGE, ROBO_VLAN_WRITE, val32);
549 robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_TABLE_ACCESS, val16);
550 }
551 }
552
553 static void handle_vlan_port_write_new(switch_driver *d, switch_vlan_config *c, int nr)
554 {
555 __u8 vtbl_entry, vtbl_index, vtbl_access;
556 __u32 untag = ((c->untag & ~(1 << d->cpuport)) << d->ports);
557
558 /* write config now */
559 if ((robo.devid == ROBO_DEVICE_ID_5395) ||
560 (robo.devid == ROBO_DEVICE_ID_53115)) {
561 vtbl_access = ROBO_VTBL_ACCESS_5395;
562 vtbl_index = ROBO_VTBL_INDX_5395;
563 vtbl_entry = ROBO_VTBL_ENTRY_5395;
564 } else {
565 vtbl_access = ROBO_VTBL_ACCESS;
566 vtbl_index = ROBO_VTBL_INDX;
567 vtbl_entry = ROBO_VTBL_ENTRY;
568 }
569
570 robo_write32(ROBO_ARLIO_PAGE, vtbl_entry, untag | c->port);
571 robo_write16(ROBO_ARLIO_PAGE, vtbl_index, nr);
572 robo_write16(ROBO_ARLIO_PAGE, vtbl_access, 1 << 7);
573 }
574
575 static int handle_vlan_port_write(void *driver, char *buf, int nr)
576 {
577 switch_driver *d = (switch_driver *)driver;
578 switch_vlan_config *c = switch_parse_vlan(d, buf);
579 int j;
580
581 if (c == NULL)
582 return -EINVAL;
583
584 for (j = 0; j < d->ports; j++) {
585 if ((c->untag | c->pvid) & (1 << j)) {
586 /* change default vlan tag */
587 robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_PORT0_DEF_TAG + (j << 1), nr);
588 }
589 }
590
591 if (robo.devid != ROBO_DEVICE_ID_5325)
592 handle_vlan_port_write_new(d, c, nr);
593 else
594 handle_vlan_port_write_old(d, c, nr);
595
596 kfree(c);
597 return 0;
598 }
599
600 #define set_switch(state) \
601 robo_write16(ROBO_CTRL_PAGE, ROBO_SWITCH_MODE, (robo_read16(ROBO_CTRL_PAGE, ROBO_SWITCH_MODE) & ~2) | (state ? 2 : 0));
602
603 static int handle_enable_read(void *driver, char *buf, int nr)
604 {
605 return sprintf(buf, "%d\n", (((robo_read16(ROBO_CTRL_PAGE, ROBO_SWITCH_MODE) & 2) == 2) ? 1 : 0));
606 }
607
608 static int handle_enable_write(void *driver, char *buf, int nr)
609 {
610 set_switch(buf[0] == '1');
611
612 return 0;
613 }
614
615 static int handle_port_enable_read(void *driver, char *buf, int nr)
616 {
617 return sprintf(buf, "%d\n", ((robo_read16(ROBO_CTRL_PAGE, robo.port[nr]) & 3) == 3 ? 0 : 1));
618 }
619
620 static int handle_port_enable_write(void *driver, char *buf, int nr)
621 {
622 u16 val16;
623
624 if (buf[0] == '0')
625 val16 = 3; /* disabled */
626 else if (buf[0] == '1')
627 val16 = 0; /* enabled */
628 else
629 return -EINVAL;
630
631 robo_write16(ROBO_CTRL_PAGE, robo.port[nr],
632 (robo_read16(ROBO_CTRL_PAGE, robo.port[nr]) & ~3) | val16);
633
634 return 0;
635 }
636
637 static int handle_port_media_read(void *driver, char *buf, int nr)
638 {
639 u16 bmcr = mdio_read(robo.port[nr], MII_BMCR);
640 int media, len;
641
642 if (bmcr & BMCR_ANENABLE)
643 media = SWITCH_MEDIA_AUTO;
644 else {
645 if (bmcr & BMCR_SPEED1000)
646 media = SWITCH_MEDIA_1000;
647 else if (bmcr & BMCR_SPEED100)
648 media = SWITCH_MEDIA_100;
649 else
650 media = 0;
651
652 if (bmcr & BMCR_FULLDPLX)
653 media |= SWITCH_MEDIA_FD;
654 }
655
656 len = switch_print_media(buf, media);
657 return len + sprintf(buf + len, "\n");
658 }
659
660 static int handle_port_media_write(void *driver, char *buf, int nr)
661 {
662 int media = switch_parse_media(buf);
663 u16 bmcr, bmcr_mask;
664
665 if (media & SWITCH_MEDIA_AUTO)
666 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
667 else {
668 if (media & SWITCH_MEDIA_1000) {
669 if (!robo.gmii)
670 return -EINVAL;
671 bmcr = BMCR_SPEED1000;
672 }
673 else if (media & SWITCH_MEDIA_100)
674 bmcr = BMCR_SPEED100;
675 else
676 bmcr = 0;
677
678 if (media & SWITCH_MEDIA_FD)
679 bmcr |= BMCR_FULLDPLX;
680 }
681
682 bmcr_mask = ~(BMCR_SPEED1000 | BMCR_SPEED100 | BMCR_FULLDPLX | BMCR_ANENABLE | BMCR_ANRESTART);
683 mdio_write(robo.port[nr], MII_BMCR,
684 (mdio_read(robo.port[nr], MII_BMCR) & bmcr_mask) | bmcr);
685
686 return 0;
687 }
688
689 static int handle_enable_vlan_read(void *driver, char *buf, int nr)
690 {
691 return sprintf(buf, "%d\n", (((robo_read16(ROBO_VLAN_PAGE, ROBO_VLAN_CTRL0) & (1 << 7)) == (1 << 7)) ? 1 : 0));
692 }
693
694 static int handle_enable_vlan_write(void *driver, char *buf, int nr)
695 {
696 int disable = ((buf[0] != '1') ? 1 : 0);
697
698 robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_CTRL0, disable ? 0 :
699 (1 << 7) /* 802.1Q VLAN */ | (3 << 5) /* mac check and hash */);
700 robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_CTRL1, disable ? 0 :
701 (robo.devid == ROBO_DEVICE_ID_5325 ? (1 << 1) :
702 0) | (1 << 2) | (1 << 3)); /* RSV multicast */
703
704 if (robo.devid != ROBO_DEVICE_ID_5325)
705 return 0;
706
707 robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_CTRL4, disable ? 0 :
708 (1 << 6) /* drop invalid VID frames */);
709 robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_CTRL5, disable ? 0 :
710 (1 << 3) /* drop miss V table frames */);
711
712 return 0;
713 }
714
715 static int handle_reset(void *driver, char *buf, int nr)
716 {
717 switch_driver *d = (switch_driver *) driver;
718 int j;
719 __u16 val16;
720
721 /* disable switching */
722 set_switch(0);
723
724 /* reset vlans */
725 for (j = 0; j <= ((robo.is_5365) ? VLAN_ID_MAX_5365 : VLAN_ID_MAX); j++) {
726 /* write config now */
727 val16 = (j) /* vlan */ | (1 << 12) /* write */ | (1 << 13) /* enable */;
728 if (robo.is_5365)
729 robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_WRITE_5365, 0);
730 else
731 robo_write32(ROBO_VLAN_PAGE, ROBO_VLAN_WRITE, 0);
732 robo_write16(ROBO_VLAN_PAGE, robo.is_5365 ? ROBO_VLAN_TABLE_ACCESS_5365 :
733 ROBO_VLAN_TABLE_ACCESS,
734 val16);
735 }
736
737 /* reset ports to a known good state */
738 for (j = 0; j < d->ports; j++) {
739 robo_write16(ROBO_CTRL_PAGE, robo.port[j], 0x0000);
740 robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_PORT0_DEF_TAG + (j << 1), 0);
741 }
742
743 /* enable switching */
744 set_switch(1);
745
746 /* enable vlans */
747 handle_enable_vlan_write(driver, "1", 0);
748
749 return 0;
750 }
751
752 static int __init robo_init(void)
753 {
754 int notfound = 1;
755 char *device;
756
757 device = strdup("ethX");
758 for (device[3] = '0'; (device[3] <= '3') && notfound; device[3]++) {
759 if (! switch_device_registered (device))
760 notfound = robo_probe(device);
761 }
762 device[3]--;
763
764 if (notfound) {
765 kfree(device);
766 return -ENODEV;
767 } else {
768 static const switch_config cfg[] = {
769 {
770 .name = "enable",
771 .read = handle_enable_read,
772 .write = handle_enable_write
773 }, {
774 .name = "enable_vlan",
775 .read = handle_enable_vlan_read,
776 .write = handle_enable_vlan_write
777 }, {
778 .name = "reset",
779 .read = NULL,
780 .write = handle_reset
781 }, { NULL, },
782 };
783 static const switch_config port[] = {
784 {
785 .name = "enable",
786 .read = handle_port_enable_read,
787 .write = handle_port_enable_write
788 }, {
789 .name = "media",
790 .read = handle_port_media_read,
791 .write = handle_port_media_write
792 }, { NULL, },
793 };
794 static const switch_config vlan[] = {
795 {
796 .name = "ports",
797 .read = handle_vlan_port_read,
798 .write = handle_vlan_port_write
799 }, { NULL, },
800 };
801 switch_driver driver = {
802 .name = DRIVER_NAME,
803 .version = DRIVER_VERSION,
804 .interface = device,
805 .cpuport = 5,
806 .ports = 6,
807 .vlans = 16,
808 .driver_handlers = cfg,
809 .port_handlers = port,
810 .vlan_handlers = vlan,
811 };
812 if (robo.devid != ROBO_DEVICE_ID_5325) {
813 driver.ports = 9;
814 driver.cpuport = 8;
815 }
816
817 return switch_register_driver(&driver);
818 }
819 }
820
821 static void __exit robo_exit(void)
822 {
823 switch_unregister_driver(DRIVER_NAME);
824 if (robo.dev)
825 dev_put(robo.dev);
826 if (robo.gpio_robo_reset >= 0)
827 gpio_free(robo.gpio_robo_reset);
828 if (robo.gpio_lanports_enable >= 0)
829 gpio_free(robo.gpio_lanports_enable);
830 kfree(robo.device);
831 }
832
833
834 MODULE_AUTHOR("Felix Fietkau <openwrt@nbd.name>");
835 MODULE_LICENSE("GPL");
836
837 module_init(robo_init);
838 module_exit(robo_exit);