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/autoconf.h>
25 #include <linux/module.h>
26 #include <linux/init.h>
28 #include <linux/if_arp.h>
29 #include <linux/sockios.h>
30 #include <linux/ethtool.h>
31 #include <linux/mii.h>
32 #include <linux/delay.h>
33 #include <asm/uaccess.h>
35 #include "switch-core.h"
38 #define DRIVER_NAME "bcm53xx"
39 #define DRIVER_VERSION "0.02"
40 #define PFX "roboswitch: "
42 #define ROBO_PHY_ADDR 0x1E /* robo switch phy address */
43 #define ROBO_PHY_ADDR_TG3 0x01 /* Tigon3 PHY address */
44 #define ROBO_PHY_ADDR_BCM63XX 0x00 /* BCM63XX PHY address */
47 #define REG_MII_PAGE 0x10 /* MII Page register */
48 #define REG_MII_ADDR 0x11 /* MII Address register */
49 #define REG_MII_DATA0 0x18 /* MII Data register 0 */
51 #define REG_MII_PAGE_ENABLE 1
52 #define REG_MII_ADDR_WRITE 1
53 #define REG_MII_ADDR_READ 2
55 /* Robo device ID register (in ROBO_MGMT_PAGE) */
56 #define ROBO_DEVICE_ID 0x30
57 #define ROBO_DEVICE_ID_5325 0x25 /* Faked */
58 #define ROBO_DEVICE_ID_5395 0x95
59 #define ROBO_DEVICE_ID_5397 0x97
60 #define ROBO_DEVICE_ID_5398 0x98
62 /* Private et.o ioctls */
63 #define SIOCGETCPHYRD (SIOCDEVPRIVATE + 9)
64 #define SIOCSETCPHYWR (SIOCDEVPRIVATE + 10)
66 /* linux 2.4 does not have 'bool' */
67 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
71 /* Only available on brcm-2.4/brcm47xx */
73 extern char *nvram_get(const char *name
);
74 #define getvar(str) (nvram_get(str)?:"")
76 #define getvar(str) ""
79 /* Data structure for a Roboswitch device. */
81 char *device
; /* The device name string (ethX) */
82 u16 devid
; /* ROBO_DEVICE_ID_53xx */
85 u8 phy_addr
; /* PHY address of the device */
87 struct net_device
*dev
;
88 unsigned char port
[6];
91 /* Currently we can only have one device in the system. */
92 static struct robo_switch robo
;
95 static int do_ioctl(int cmd
, void *buf
)
97 mm_segment_t old_fs
= get_fs();
101 robo
.ifr
.ifr_data
= (caddr_t
) buf
;
104 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,31)
105 ret
= robo
.dev
->netdev_ops
->ndo_do_ioctl(robo
.dev
, &robo
.ifr
, cmd
);
107 ret
= robo
.dev
->do_ioctl(robo
.dev
, &robo
.ifr
, cmd
);
114 static u16
mdio_read(__u16 phy_id
, __u8 reg
)
117 int args
[2] = { reg
};
119 if (phy_id
!= robo
.phy_addr
) {
121 "Access to real 'phy' registers unavaliable.\n"
122 "Upgrade kernel driver.\n");
128 if (do_ioctl(SIOCGETCPHYRD
, &args
) < 0) {
130 "[%s:%d] SIOCGETCPHYRD failed!\n", __FILE__
, __LINE__
);
136 struct mii_ioctl_data
*mii
= (struct mii_ioctl_data
*) &robo
.ifr
.ifr_data
;
137 mii
->phy_id
= phy_id
;
140 if (do_ioctl(SIOCGMIIREG
, NULL
) < 0) {
142 "[%s:%d] SIOCGMIIREG failed!\n", __FILE__
, __LINE__
);
151 static void mdio_write(__u16 phy_id
, __u8 reg
, __u16 val
)
154 int args
[2] = { reg
, val
};
156 if (phy_id
!= robo
.phy_addr
) {
158 "Access to real 'phy' registers unavaliable.\n"
159 "Upgrade kernel driver.\n");
164 if (do_ioctl(SIOCSETCPHYWR
, args
) < 0) {
166 "[%s:%d] SIOCGETCPHYWR failed!\n", __FILE__
, __LINE__
);
170 struct mii_ioctl_data
*mii
= (struct mii_ioctl_data
*)&robo
.ifr
.ifr_data
;
172 mii
->phy_id
= phy_id
;
176 if (do_ioctl(SIOCSMIIREG
, NULL
) < 0) {
178 "[%s:%d] SIOCSMIIREG failed!\n", __FILE__
, __LINE__
);
184 static int robo_reg(__u8 page
, __u8 reg
, __u8 op
)
188 /* set page number */
189 mdio_write(robo
.phy_addr
, REG_MII_PAGE
,
190 (page
<< 8) | REG_MII_PAGE_ENABLE
);
192 /* set register address */
193 mdio_write(robo
.phy_addr
, REG_MII_ADDR
,
196 /* check if operation completed */
198 if ((mdio_read(robo
.phy_addr
, REG_MII_ADDR
) & 3) == 0)
202 printk(KERN_ERR PFX
"[%s:%d] timeout in robo_reg!\n", __FILE__
, __LINE__
);
208 static void robo_read(__u8 page, __u8 reg, __u16 *val, int count)
212 robo_reg(page, reg, REG_MII_ADDR_READ);
214 for (i = 0; i < count; i++)
215 val[i] = mdio_read(robo.phy_addr, REG_MII_DATA0 + i);
219 static __u16
robo_read16(__u8 page
, __u8 reg
)
221 robo_reg(page
, reg
, REG_MII_ADDR_READ
);
223 return mdio_read(robo
.phy_addr
, REG_MII_DATA0
);
226 static __u32
robo_read32(__u8 page
, __u8 reg
)
228 robo_reg(page
, reg
, REG_MII_ADDR_READ
);
230 return mdio_read(robo
.phy_addr
, REG_MII_DATA0
) +
231 (mdio_read(robo
.phy_addr
, REG_MII_DATA0
+ 1) << 16);
234 static void robo_write16(__u8 page
, __u8 reg
, __u16 val16
)
237 mdio_write(robo
.phy_addr
, REG_MII_DATA0
, val16
);
239 robo_reg(page
, reg
, REG_MII_ADDR_WRITE
);
242 static void robo_write32(__u8 page
, __u8 reg
, __u32 val32
)
245 mdio_write(robo
.phy_addr
, REG_MII_DATA0
, val32
& 65535);
246 mdio_write(robo
.phy_addr
, REG_MII_DATA0
+ 1, val32
>> 16);
248 robo_reg(page
, reg
, REG_MII_ADDR_WRITE
);
251 /* checks that attached switch is 5325E/5350 */
252 static int robo_vlan5350(void)
254 /* set vlan access id to 15 and read it back */
256 robo_write16(ROBO_VLAN_PAGE
, ROBO_VLAN_TABLE_ACCESS_5350
, val16
);
258 /* 5365 will refuse this as it does not have this reg */
259 return (robo_read16(ROBO_VLAN_PAGE
, ROBO_VLAN_TABLE_ACCESS_5350
) == val16
);
262 static int robo_switch_enable(void)
264 unsigned int i
, last_port
;
267 val
= robo_read16(ROBO_CTRL_PAGE
, ROBO_SWITCH_MODE
);
268 if (!(val
& (1 << 1))) {
271 /* With forwarding */
273 robo_write16(ROBO_CTRL_PAGE
, ROBO_SWITCH_MODE
, val
);
274 val
= robo_read16(ROBO_CTRL_PAGE
, ROBO_SWITCH_MODE
);
275 if (!(val
& (1 << 1))) {
276 printk("Failed to enable switch\n");
280 last_port
= (robo
.devid
== ROBO_DEVICE_ID_5398
) ?
281 ROBO_PORT6_CTRL
: ROBO_PORT3_CTRL
;
282 for (i
= ROBO_PORT0_CTRL
; i
< last_port
+ 1; i
++)
283 robo_write16(ROBO_CTRL_PAGE
, i
, 0);
286 /* WAN port LED, except for Netgear WGT634U */
287 if (strcmp(getvar("nvram_type"), "cfe") != 0)
288 robo_write16(ROBO_CTRL_PAGE
, 0x16, 0x1F);
293 static void robo_switch_reset(void)
295 if ((robo
.devid
== ROBO_DEVICE_ID_5395
) ||
296 (robo
.devid
== ROBO_DEVICE_ID_5397
) ||
297 (robo
.devid
== ROBO_DEVICE_ID_5398
)) {
298 /* Trigger a software reset. */
299 robo_write16(ROBO_CTRL_PAGE
, 0x79, 0x83);
300 robo_write16(ROBO_CTRL_PAGE
, 0x79, 0);
304 static int robo_probe(char *devname
)
310 printk(KERN_INFO PFX
"Probing device %s: ", devname
);
311 strcpy(robo
.ifr
.ifr_name
, devname
);
313 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
314 if ((robo
.dev
= dev_get_by_name(devname
)) == NULL
) {
316 if ((robo
.dev
= dev_get_by_name(&init_net
, devname
)) == NULL
) {
318 printk("No such device\n");
322 robo
.device
= devname
;
323 for (i
= 0; i
< 5; i
++)
327 /* try access using MII ioctls - get phy address */
328 if (do_ioctl(SIOCGMIIPHY
, NULL
) < 0) {
330 robo
.phy_addr
= ROBO_PHY_ADDR
;
332 /* got phy address check for robo address */
333 struct mii_ioctl_data
*mii
= (struct mii_ioctl_data
*) &robo
.ifr
.ifr_data
;
334 if ((mii
->phy_id
!= ROBO_PHY_ADDR
) &&
335 (mii
->phy_id
!= ROBO_PHY_ADDR_BCM63XX
) &&
336 (mii
->phy_id
!= ROBO_PHY_ADDR_TG3
)) {
337 printk("Invalid phy address (%d)\n", mii
->phy_id
);
341 /* The robo has a fixed PHY address that is different from the
342 * Tigon3 and BCM63xx PHY address. */
343 robo
.phy_addr
= ROBO_PHY_ADDR
;
346 phyid
= mdio_read(robo
.phy_addr
, 0x2) |
347 (mdio_read(robo
.phy_addr
, 0x3) << 16);
349 if (phyid
== 0xffffffff || phyid
== 0x55210022) {
350 printk("No Robo switch in managed mode found, phy_id = 0x%08x\n", phyid
);
354 /* Get the device ID */
355 for (i
= 0; i
< 10; i
++) {
356 robo
.devid
= robo_read16(ROBO_MGMT_PAGE
, ROBO_DEVICE_ID
);
362 robo
.devid
= ROBO_DEVICE_ID_5325
; /* Fake it */
363 robo
.is_5350
= robo_vlan5350();
366 err
= robo_switch_enable();
375 static int handle_vlan_port_read(void *driver
, char *buf
, int nr
)
381 val16
= (nr
) /* vlan */ | (0 << 12) /* read */ | (1 << 13) /* enable */;
385 robo_write16(ROBO_VLAN_PAGE
, ROBO_VLAN_TABLE_ACCESS_5350
, val16
);
387 val32
= robo_read32(ROBO_VLAN_PAGE
, ROBO_VLAN_READ
);
388 if ((val32
& (1 << 20)) /* valid */) {
389 for (j
= 0; j
< 6; j
++) {
390 if (val32
& (1 << j
)) {
391 len
+= sprintf(buf
+ len
, "%d", j
);
392 if (val32
& (1 << (j
+ 6))) {
393 if (j
== 5) buf
[len
++] = 'u';
396 if (robo_read16(ROBO_VLAN_PAGE
, ROBO_VLAN_PORT0_DEF_TAG
+ (j
<< 1)) == nr
)
402 len
+= sprintf(buf
+ len
, "\n");
405 robo_write16(ROBO_VLAN_PAGE
, ROBO_VLAN_TABLE_ACCESS
, val16
);
407 val16
= robo_read16(ROBO_VLAN_PAGE
, ROBO_VLAN_READ
);
408 if ((val16
& (1 << 14)) /* valid */) {
409 for (j
= 0; j
< 6; j
++) {
410 if (val16
& (1 << j
)) {
411 len
+= sprintf(buf
+ len
, "%d", j
);
412 if (val16
& (1 << (j
+ 7))) {
413 if (j
== 5) buf
[len
++] = 'u';
416 if (robo_read16(ROBO_VLAN_PAGE
, ROBO_VLAN_PORT0_DEF_TAG
+ (j
<< 1)) == nr
)
422 len
+= sprintf(buf
+ len
, "\n");
431 static int handle_vlan_port_write(void *driver
, char *buf
, int nr
)
433 switch_driver
*d
= (switch_driver
*) driver
;
434 switch_vlan_config
*c
= switch_parse_vlan(d
, buf
);
441 for (j
= 0; j
< d
->ports
; j
++) {
442 if ((c
->untag
| c
->pvid
) & (1 << j
))
443 /* change default vlan tag */
444 robo_write16(ROBO_VLAN_PAGE
, ROBO_VLAN_PORT0_DEF_TAG
+ (j
<< 1), nr
);
447 /* write config now */
448 val16
= (nr
) /* vlan */ | (1 << 12) /* write */ | (1 << 13) /* enable */;
450 robo_write32(ROBO_VLAN_PAGE
, ROBO_VLAN_WRITE_5350
,
451 (1 << 20) /* valid */ | (c
->untag
<< 6) | c
->port
);
452 robo_write16(ROBO_VLAN_PAGE
, ROBO_VLAN_TABLE_ACCESS_5350
, val16
);
454 robo_write16(ROBO_VLAN_PAGE
, ROBO_VLAN_WRITE
,
455 (1 << 14) /* valid */ | (c
->untag
<< 7) | c
->port
);
456 robo_write16(ROBO_VLAN_PAGE
, ROBO_VLAN_TABLE_ACCESS
, val16
);
462 #define set_switch(state) \
463 robo_write16(ROBO_CTRL_PAGE, ROBO_SWITCH_MODE, (robo_read16(ROBO_CTRL_PAGE, ROBO_SWITCH_MODE) & ~2) | (state ? 2 : 0));
465 static int handle_enable_read(void *driver
, char *buf
, int nr
)
467 return sprintf(buf
, "%d\n", (((robo_read16(ROBO_CTRL_PAGE
, ROBO_SWITCH_MODE
) & 2) == 2) ? 1 : 0));
470 static int handle_enable_write(void *driver
, char *buf
, int nr
)
472 set_switch(buf
[0] == '1');
477 static int handle_enable_vlan_read(void *driver
, char *buf
, int nr
)
479 return sprintf(buf
, "%d\n", (((robo_read16(ROBO_VLAN_PAGE
, ROBO_VLAN_CTRL0
) & (1 << 7)) == (1 << 7)) ? 1 : 0));
482 static int handle_enable_vlan_write(void *driver
, char *buf
, int nr
)
484 int disable
= ((buf
[0] != '1') ? 1 : 0);
486 robo_write16(ROBO_VLAN_PAGE
, ROBO_VLAN_CTRL0
, disable
? 0 :
487 (1 << 7) /* 802.1Q VLAN */ | (3 << 5) /* mac check and hash */);
488 robo_write16(ROBO_VLAN_PAGE
, ROBO_VLAN_CTRL1
, disable
? 0 :
489 (1 << 1) | (1 << 2) | (1 << 3) /* RSV multicast */);
490 robo_write16(ROBO_VLAN_PAGE
, ROBO_VLAN_CTRL4
, disable
? 0 :
491 (1 << 6) /* drop invalid VID frames */);
492 robo_write16(ROBO_VLAN_PAGE
, ROBO_VLAN_CTRL5
, disable
? 0 :
493 (1 << 3) /* drop miss V table frames */);
498 static int handle_reset(void *driver
, char *buf
, int nr
)
500 switch_driver
*d
= (switch_driver
*) driver
;
501 switch_vlan_config
*c
= switch_parse_vlan(d
, buf
);
508 /* disable switching */
512 for (j
= 0; j
<= ((robo
.is_5350
) ? VLAN_ID_MAX5350
: VLAN_ID_MAX
); j
++) {
513 /* write config now */
514 val16
= (j
) /* vlan */ | (1 << 12) /* write */ | (1 << 13) /* enable */;
516 robo_write32(ROBO_VLAN_PAGE
, ROBO_VLAN_WRITE_5350
, 0);
518 robo_write16(ROBO_VLAN_PAGE
, ROBO_VLAN_WRITE
, 0);
519 robo_write16(ROBO_VLAN_PAGE
, robo
.is_5350
? ROBO_VLAN_TABLE_ACCESS_5350
:
520 ROBO_VLAN_TABLE_ACCESS
,
524 /* reset ports to a known good state */
525 for (j
= 0; j
< d
->ports
; j
++) {
526 robo_write16(ROBO_CTRL_PAGE
, robo
.port
[j
], 0x0000);
527 robo_write16(ROBO_VLAN_PAGE
, ROBO_VLAN_PORT0_DEF_TAG
+ (j
<< 1), 0);
530 /* enable switching */
534 handle_enable_vlan_write(driver
, "1", 0);
539 static int __init
robo_init(void)
544 device
= strdup("ethX");
545 for (device
[3] = '0'; (device
[3] <= '3') && notfound
; device
[3]++) {
546 if (! switch_device_registered (device
))
547 notfound
= robo_probe(device
);
555 static const switch_config cfg
[] = {
558 .read
= handle_enable_read
,
559 .write
= handle_enable_write
561 .name
= "enable_vlan",
562 .read
= handle_enable_vlan_read
,
563 .write
= handle_enable_vlan_write
567 .write
= handle_reset
570 static const switch_config vlan
[] = {
573 .read
= handle_vlan_port_read
,
574 .write
= handle_vlan_port_write
577 switch_driver driver
= {
579 .version
= DRIVER_VERSION
,
584 .driver_handlers
= cfg
,
585 .port_handlers
= NULL
,
586 .vlan_handlers
= vlan
,
589 return switch_register_driver(&driver
);
593 static void __exit
robo_exit(void)
595 switch_unregister_driver(DRIVER_NAME
);
600 MODULE_AUTHOR("Felix Fietkau <openwrt@nbd.name>");
601 MODULE_LICENSE("GPL");
603 module_init(robo_init
);
604 module_exit(robo_exit
);