2 * Broadcom BCM5325E/536x switch configuration module
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
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.
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.
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
24 #include <linux/module.h>
25 #include <linux/init.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 <asm/uaccess.h>
34 #include "switch-core.h"
38 #include <bcm47xx_nvram.h>
41 #define DRIVER_NAME "bcm53xx"
42 #define DRIVER_VERSION "0.02"
43 #define PFX "roboswitch: "
45 #define ROBO_PHY_ADDR 0x1E /* robo switch phy address */
46 #define ROBO_PHY_ADDR_TG3 0x01 /* Tigon3 PHY address */
47 #define ROBO_PHY_ADDR_BCM63XX 0x00 /* BCM63XX PHY address */
50 #define REG_MII_PAGE 0x10 /* MII Page register */
51 #define REG_MII_ADDR 0x11 /* MII Address register */
52 #define REG_MII_DATA0 0x18 /* MII Data register 0 */
54 #define REG_MII_PAGE_ENABLE 1
55 #define REG_MII_ADDR_WRITE 1
56 #define REG_MII_ADDR_READ 2
58 /* Robo device ID register (in ROBO_MGMT_PAGE) */
59 #define ROBO_DEVICE_ID 0x30
60 #define ROBO_DEVICE_ID_5325 0x25 /* Faked */
61 #define ROBO_DEVICE_ID_5395 0x95
62 #define ROBO_DEVICE_ID_5397 0x97
63 #define ROBO_DEVICE_ID_5398 0x98
64 #define ROBO_DEVICE_ID_53115 0x3115
66 /* Private et.o ioctls */
67 #define SIOCGETCPHYRD (SIOCDEVPRIVATE + 9)
68 #define SIOCSETCPHYWR (SIOCDEVPRIVATE + 10)
70 /* Data structure for a Roboswitch device. */
72 char *device
; /* The device name string (ethX) */
73 u16 devid
; /* ROBO_DEVICE_ID_53xx */
76 struct net_device
*dev
;
77 unsigned char port
[6];
80 /* Currently we can only have one device in the system. */
81 static struct robo_switch robo
;
84 static int do_ioctl(int cmd
)
86 mm_segment_t old_fs
= get_fs();
90 ret
= robo
.dev
->netdev_ops
->ndo_do_ioctl(robo
.dev
, &robo
.ifr
, cmd
);
96 static u16
mdio_read(__u16 phy_id
, __u8 reg
)
98 struct mii_ioctl_data
*mii
= if_mii(&robo
.ifr
);
101 mii
->phy_id
= phy_id
;
104 err
= do_ioctl(SIOCGMIIREG
);
106 printk(KERN_ERR PFX
"failed to read mdio reg %i with err %i.\n", reg
, err
);
114 static void mdio_write(__u16 phy_id
, __u8 reg
, __u16 val
)
116 struct mii_ioctl_data
*mii
= if_mii(&robo
.ifr
);
119 mii
->phy_id
= phy_id
;
123 err
= do_ioctl(SIOCSMIIREG
);
125 printk(KERN_ERR PFX
"failed to write mdio reg: %i with err %i.\n", reg
, err
);
130 static int robo_reg(__u8 page
, __u8 reg
, __u8 op
)
134 /* set page number */
135 mdio_write(ROBO_PHY_ADDR
, REG_MII_PAGE
,
136 (page
<< 8) | REG_MII_PAGE_ENABLE
);
138 /* set register address */
139 mdio_write(ROBO_PHY_ADDR
, REG_MII_ADDR
,
142 /* check if operation completed */
144 if ((mdio_read(ROBO_PHY_ADDR
, REG_MII_ADDR
) & 3) == 0)
148 printk(KERN_ERR PFX
"timeout in robo_reg on page %i and reg %i with op %i.\n", page
, reg
, op
);
154 static void robo_read(__u8 page, __u8 reg, __u16 *val, int count)
158 robo_reg(page, reg, REG_MII_ADDR_READ);
160 for (i = 0; i < count; i++)
161 val[i] = mdio_read(ROBO_PHY_ADDR, REG_MII_DATA0 + i);
165 static __u16
robo_read16(__u8 page
, __u8 reg
)
167 robo_reg(page
, reg
, REG_MII_ADDR_READ
);
169 return mdio_read(ROBO_PHY_ADDR
, REG_MII_DATA0
);
172 static __u32
robo_read32(__u8 page
, __u8 reg
)
174 robo_reg(page
, reg
, REG_MII_ADDR_READ
);
176 return mdio_read(ROBO_PHY_ADDR
, REG_MII_DATA0
) +
177 (mdio_read(ROBO_PHY_ADDR
, REG_MII_DATA0
+ 1) << 16);
180 static void robo_write16(__u8 page
, __u8 reg
, __u16 val16
)
183 mdio_write(ROBO_PHY_ADDR
, REG_MII_DATA0
, val16
);
185 robo_reg(page
, reg
, REG_MII_ADDR_WRITE
);
188 static void robo_write32(__u8 page
, __u8 reg
, __u32 val32
)
191 mdio_write(ROBO_PHY_ADDR
, REG_MII_DATA0
, val32
& 65535);
192 mdio_write(ROBO_PHY_ADDR
, REG_MII_DATA0
+ 1, val32
>> 16);
194 robo_reg(page
, reg
, REG_MII_ADDR_WRITE
);
197 /* checks that attached switch is 5325E/5350 */
198 static int robo_vlan5350(void)
200 /* set vlan access id to 15 and read it back */
202 robo_write16(ROBO_VLAN_PAGE
, ROBO_VLAN_TABLE_ACCESS_5350
, val16
);
204 /* 5365 will refuse this as it does not have this reg */
205 return (robo_read16(ROBO_VLAN_PAGE
, ROBO_VLAN_TABLE_ACCESS_5350
) == val16
);
208 static int robo_switch_enable(void)
210 unsigned int i
, last_port
;
212 #ifdef CONFIG_BCM47XX
216 val
= robo_read16(ROBO_CTRL_PAGE
, ROBO_SWITCH_MODE
);
217 if (!(val
& (1 << 1))) {
220 /* With forwarding */
222 robo_write16(ROBO_CTRL_PAGE
, ROBO_SWITCH_MODE
, val
);
223 val
= robo_read16(ROBO_CTRL_PAGE
, ROBO_SWITCH_MODE
);
224 if (!(val
& (1 << 1))) {
225 printk(KERN_ERR PFX
"Failed to enable switch\n");
229 last_port
= (robo
.devid
== ROBO_DEVICE_ID_5398
) ?
230 ROBO_PORT6_CTRL
: ROBO_PORT3_CTRL
;
231 for (i
= ROBO_PORT0_CTRL
; i
< last_port
+ 1; i
++)
232 robo_write16(ROBO_CTRL_PAGE
, i
, 0);
235 #ifdef CONFIG_BCM47XX
236 /* WAN port LED, except for Netgear WGT634U */
237 if (bcm47xx_nvram_getenv("nvram_type", buf
, sizeof(buf
)) >= 0) {
238 if (strcmp(buf
, "cfe") != 0)
239 robo_write16(ROBO_CTRL_PAGE
, 0x16, 0x1F);
245 static void robo_switch_reset(void)
247 if ((robo
.devid
== ROBO_DEVICE_ID_5395
) ||
248 (robo
.devid
== ROBO_DEVICE_ID_5397
) ||
249 (robo
.devid
== ROBO_DEVICE_ID_5398
)) {
250 /* Trigger a software reset. */
251 robo_write16(ROBO_CTRL_PAGE
, 0x79, 0x83);
253 robo_write16(ROBO_CTRL_PAGE
, 0x79, 0);
257 static int robo_probe(char *devname
)
263 printk(KERN_INFO PFX
"Probing device '%s'\n", devname
);
264 strcpy(robo
.ifr
.ifr_name
, devname
);
266 if ((robo
.dev
= dev_get_by_name(&init_net
, devname
)) == NULL
) {
267 printk(KERN_ERR PFX
"No such device\n");
270 if (!robo
.dev
->netdev_ops
|| !robo
.dev
->netdev_ops
->ndo_do_ioctl
) {
271 printk(KERN_ERR PFX
"ndo_do_ioctl not implemented in ethernet driver\n");
275 robo
.device
= devname
;
276 for (i
= 0; i
< 5; i
++)
280 /* try access using MII ioctls - get phy address */
281 err
= do_ioctl(SIOCGMIIPHY
);
283 printk(KERN_ERR PFX
"error (%i) while accessing MII phy registers with ioctls\n", err
);
287 /* got phy address check for robo address */
288 struct mii_ioctl_data
*mii
= if_mii(&robo
.ifr
);
289 if ((mii
->phy_id
!= ROBO_PHY_ADDR
) &&
290 (mii
->phy_id
!= ROBO_PHY_ADDR_BCM63XX
) &&
291 (mii
->phy_id
!= ROBO_PHY_ADDR_TG3
)) {
292 printk(KERN_ERR PFX
"Invalid phy address (%d)\n", mii
->phy_id
);
296 phyid
= mdio_read(ROBO_PHY_ADDR
, 0x2) |
297 (mdio_read(ROBO_PHY_ADDR
, 0x3) << 16);
299 if (phyid
== 0xffffffff || phyid
== 0x55210022) {
300 printk(KERN_ERR PFX
"No Robo switch in managed mode found, phy_id = 0x%08x\n", phyid
);
304 /* Get the device ID */
305 for (i
= 0; i
< 10; i
++) {
306 robo
.devid
= robo_read16(ROBO_MGMT_PAGE
, ROBO_DEVICE_ID
);
312 robo
.devid
= ROBO_DEVICE_ID_5325
; /* Fake it */
313 robo
.is_5350
= robo_vlan5350();
316 err
= robo_switch_enable();
321 printk(KERN_INFO PFX
"found a 5%s%x!%s at %s\n", robo
.devid
& 0xff00 ? "" : "3", robo
.devid
,
322 robo
.is_5350
? " It's a 5350." : "", devname
);
333 static int handle_vlan_port_read(void *driver
, char *buf
, int nr
)
339 val16
= (nr
) /* vlan */ | (0 << 12) /* read */ | (1 << 13) /* enable */;
343 robo_write16(ROBO_VLAN_PAGE
, ROBO_VLAN_TABLE_ACCESS_5350
, val16
);
345 val32
= robo_read32(ROBO_VLAN_PAGE
, ROBO_VLAN_READ
);
346 if ((val32
& (1 << 20)) /* valid */) {
347 for (j
= 0; j
< 6; j
++) {
348 if (val32
& (1 << j
)) {
349 len
+= sprintf(buf
+ len
, "%d", j
);
350 if (val32
& (1 << (j
+ 6))) {
351 if (j
== 5) buf
[len
++] = 'u';
354 if (robo_read16(ROBO_VLAN_PAGE
, ROBO_VLAN_PORT0_DEF_TAG
+ (j
<< 1)) == nr
)
360 len
+= sprintf(buf
+ len
, "\n");
363 robo_write16(ROBO_VLAN_PAGE
, ROBO_VLAN_TABLE_ACCESS
, val16
);
365 val16
= robo_read16(ROBO_VLAN_PAGE
, ROBO_VLAN_READ
);
366 if ((val16
& (1 << 14)) /* valid */) {
367 for (j
= 0; j
< 6; j
++) {
368 if (val16
& (1 << j
)) {
369 len
+= sprintf(buf
+ len
, "%d", j
);
370 if (val16
& (1 << (j
+ 7))) {
371 if (j
== 5) buf
[len
++] = 'u';
374 if (robo_read16(ROBO_VLAN_PAGE
, ROBO_VLAN_PORT0_DEF_TAG
+ (j
<< 1)) == nr
)
380 len
+= sprintf(buf
+ len
, "\n");
389 static int handle_vlan_port_write(void *driver
, char *buf
, int nr
)
391 switch_driver
*d
= (switch_driver
*) driver
;
392 switch_vlan_config
*c
= switch_parse_vlan(d
, buf
);
399 for (j
= 0; j
< d
->ports
; j
++) {
400 if ((c
->untag
| c
->pvid
) & (1 << j
))
401 /* change default vlan tag */
402 robo_write16(ROBO_VLAN_PAGE
, ROBO_VLAN_PORT0_DEF_TAG
+ (j
<< 1), nr
);
405 /* write config now */
407 if (robo
.devid
!= ROBO_DEVICE_ID_5325
) {
408 __u8 regoff
= ((robo
.devid
== ROBO_DEVICE_ID_5395
) ||
409 (robo
.devid
== ROBO_DEVICE_ID_53115
)) ? 0x20 : 0;
411 robo_write32(ROBO_ARLIO_PAGE
, 0x63 + regoff
, (c
->untag
<< 9) | c
->port
);
412 robo_write16(ROBO_ARLIO_PAGE
, 0x61 + regoff
, nr
);
413 robo_write16(ROBO_ARLIO_PAGE
, 0x60 + regoff
, 1 << 7);
418 val16
= (nr
) /* vlan */ | (1 << 12) /* write */ | (1 << 13) /* enable */;
420 robo_write32(ROBO_VLAN_PAGE
, ROBO_VLAN_WRITE_5350
,
421 (1 << 20) /* valid */ | (c
->untag
<< 6) | c
->port
);
422 robo_write16(ROBO_VLAN_PAGE
, ROBO_VLAN_TABLE_ACCESS_5350
, val16
);
424 robo_write16(ROBO_VLAN_PAGE
, ROBO_VLAN_WRITE
,
425 (1 << 14) /* valid */ | (c
->untag
<< 7) | c
->port
);
426 robo_write16(ROBO_VLAN_PAGE
, ROBO_VLAN_TABLE_ACCESS
, val16
);
433 #define set_switch(state) \
434 robo_write16(ROBO_CTRL_PAGE, ROBO_SWITCH_MODE, (robo_read16(ROBO_CTRL_PAGE, ROBO_SWITCH_MODE) & ~2) | (state ? 2 : 0));
436 static int handle_enable_read(void *driver
, char *buf
, int nr
)
438 return sprintf(buf
, "%d\n", (((robo_read16(ROBO_CTRL_PAGE
, ROBO_SWITCH_MODE
) & 2) == 2) ? 1 : 0));
441 static int handle_enable_write(void *driver
, char *buf
, int nr
)
443 set_switch(buf
[0] == '1');
448 static int handle_enable_vlan_read(void *driver
, char *buf
, int nr
)
450 return sprintf(buf
, "%d\n", (((robo_read16(ROBO_VLAN_PAGE
, ROBO_VLAN_CTRL0
) & (1 << 7)) == (1 << 7)) ? 1 : 0));
453 static int handle_enable_vlan_write(void *driver
, char *buf
, int nr
)
455 int disable
= ((buf
[0] != '1') ? 1 : 0);
457 robo_write16(ROBO_VLAN_PAGE
, ROBO_VLAN_CTRL0
, disable
? 0 :
458 (1 << 7) /* 802.1Q VLAN */ | (3 << 5) /* mac check and hash */);
459 robo_write16(ROBO_VLAN_PAGE
, ROBO_VLAN_CTRL1
, disable
? 0 :
460 (robo
.devid
== ROBO_DEVICE_ID_5325
? (1 << 1) :
461 0) | (1 << 2) | (1 << 3)); /* RSV multicast */
463 if (robo
.devid
!= ROBO_DEVICE_ID_5325
)
466 robo_write16(ROBO_VLAN_PAGE
, ROBO_VLAN_CTRL4
, disable
? 0 :
467 (1 << 6) /* drop invalid VID frames */);
468 robo_write16(ROBO_VLAN_PAGE
, ROBO_VLAN_CTRL5
, disable
? 0 :
469 (1 << 3) /* drop miss V table frames */);
474 static int handle_reset(void *driver
, char *buf
, int nr
)
476 switch_driver
*d
= (switch_driver
*) driver
;
480 /* disable switching */
484 for (j
= 0; j
<= ((robo
.is_5350
) ? VLAN_ID_MAX5350
: VLAN_ID_MAX
); j
++) {
485 /* write config now */
486 val16
= (j
) /* vlan */ | (1 << 12) /* write */ | (1 << 13) /* enable */;
488 robo_write32(ROBO_VLAN_PAGE
, ROBO_VLAN_WRITE_5350
, 0);
490 robo_write16(ROBO_VLAN_PAGE
, ROBO_VLAN_WRITE
, 0);
491 robo_write16(ROBO_VLAN_PAGE
, robo
.is_5350
? ROBO_VLAN_TABLE_ACCESS_5350
:
492 ROBO_VLAN_TABLE_ACCESS
,
496 /* reset ports to a known good state */
497 for (j
= 0; j
< d
->ports
; j
++) {
498 robo_write16(ROBO_CTRL_PAGE
, robo
.port
[j
], 0x0000);
499 robo_write16(ROBO_VLAN_PAGE
, ROBO_VLAN_PORT0_DEF_TAG
+ (j
<< 1), 0);
502 /* enable switching */
506 handle_enable_vlan_write(driver
, "1", 0);
511 static int __init
robo_init(void)
516 device
= strdup("ethX");
517 for (device
[3] = '0'; (device
[3] <= '3') && notfound
; device
[3]++) {
518 if (! switch_device_registered (device
))
519 notfound
= robo_probe(device
);
527 static const switch_config cfg
[] = {
530 .read
= handle_enable_read
,
531 .write
= handle_enable_write
533 .name
= "enable_vlan",
534 .read
= handle_enable_vlan_read
,
535 .write
= handle_enable_vlan_write
539 .write
= handle_reset
542 static const switch_config vlan
[] = {
545 .read
= handle_vlan_port_read
,
546 .write
= handle_vlan_port_write
549 switch_driver driver
= {
551 .version
= DRIVER_VERSION
,
556 .driver_handlers
= cfg
,
557 .port_handlers
= NULL
,
558 .vlan_handlers
= vlan
,
560 if (robo
.devid
!= ROBO_DEVICE_ID_5325
) {
565 return switch_register_driver(&driver
);
569 static void __exit
robo_exit(void)
571 switch_unregister_driver(DRIVER_NAME
);
578 MODULE_AUTHOR("Felix Fietkau <openwrt@nbd.name>");
579 MODULE_LICENSE("GPL");
581 module_init(robo_init
);
582 module_exit(robo_exit
);