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)
72 extern char *nvram_get(const char *name
);
74 /* Data structure for a Roboswitch device. */
76 char *device
; /* The device name string (ethX) */
77 u16 devid
; /* ROBO_DEVICE_ID_53xx */
80 u8 phy_addr
; /* PHY address of the device */
82 struct net_device
*dev
;
83 unsigned char port
[6];
86 /* Currently we can only have one device in the system. */
87 static struct robo_switch robo
;
90 static int do_ioctl(int cmd
, void *buf
)
92 mm_segment_t old_fs
= get_fs();
96 robo
.ifr
.ifr_data
= (caddr_t
) buf
;
99 ret
= robo
.dev
->do_ioctl(robo
.dev
, &robo
.ifr
, cmd
);
105 static u16
mdio_read(__u16 phy_id
, __u8 reg
)
108 int args
[2] = { reg
};
110 if (phy_id
!= robo
.phy_addr
) {
112 "Access to real 'phy' registers unavaliable.\n"
113 "Upgrade kernel driver.\n");
119 if (do_ioctl(SIOCGETCPHYRD
, &args
) < 0) {
121 "[%s:%d] SIOCGETCPHYRD failed!\n", __FILE__
, __LINE__
);
127 struct mii_ioctl_data
*mii
= (struct mii_ioctl_data
*) &robo
.ifr
.ifr_data
;
128 mii
->phy_id
= phy_id
;
131 if (do_ioctl(SIOCGMIIREG
, NULL
) < 0) {
133 "[%s:%d] SIOCGMIIREG failed!\n", __FILE__
, __LINE__
);
142 static void mdio_write(__u16 phy_id
, __u8 reg
, __u16 val
)
145 int args
[2] = { reg
, val
};
147 if (phy_id
!= robo
.phy_addr
) {
149 "Access to real 'phy' registers unavaliable.\n"
150 "Upgrade kernel driver.\n");
155 if (do_ioctl(SIOCSETCPHYWR
, args
) < 0) {
157 "[%s:%d] SIOCGETCPHYWR failed!\n", __FILE__
, __LINE__
);
161 struct mii_ioctl_data
*mii
= (struct mii_ioctl_data
*)&robo
.ifr
.ifr_data
;
163 mii
->phy_id
= phy_id
;
167 if (do_ioctl(SIOCSMIIREG
, NULL
) < 0) {
169 "[%s:%d] SIOCSMIIREG failed!\n", __FILE__
, __LINE__
);
175 static int robo_reg(__u8 page
, __u8 reg
, __u8 op
)
179 /* set page number */
180 mdio_write(robo
.phy_addr
, REG_MII_PAGE
,
181 (page
<< 8) | REG_MII_PAGE_ENABLE
);
183 /* set register address */
184 mdio_write(robo
.phy_addr
, REG_MII_ADDR
,
187 /* check if operation completed */
189 if ((mdio_read(robo
.phy_addr
, REG_MII_ADDR
) & 3) == 0)
193 printk(KERN_ERR PFX
"[%s:%d] timeout in robo_reg!\n", __FILE__
, __LINE__
);
199 static void robo_read(__u8 page, __u8 reg, __u16 *val, int count)
203 robo_reg(page, reg, REG_MII_ADDR_READ);
205 for (i = 0; i < count; i++)
206 val[i] = mdio_read(robo.phy_addr, REG_MII_DATA0 + i);
210 static __u16
robo_read16(__u8 page
, __u8 reg
)
212 robo_reg(page
, reg
, REG_MII_ADDR_READ
);
214 return mdio_read(robo
.phy_addr
, REG_MII_DATA0
);
217 static __u32
robo_read32(__u8 page
, __u8 reg
)
219 robo_reg(page
, reg
, REG_MII_ADDR_READ
);
221 return mdio_read(robo
.phy_addr
, REG_MII_DATA0
) +
222 (mdio_read(robo
.phy_addr
, REG_MII_DATA0
+ 1) << 16);
225 static void robo_write16(__u8 page
, __u8 reg
, __u16 val16
)
228 mdio_write(robo
.phy_addr
, REG_MII_DATA0
, val16
);
230 robo_reg(page
, reg
, REG_MII_ADDR_WRITE
);
233 static void robo_write32(__u8 page
, __u8 reg
, __u32 val32
)
236 mdio_write(robo
.phy_addr
, REG_MII_DATA0
, val32
& 65535);
237 mdio_write(robo
.phy_addr
, REG_MII_DATA0
+ 1, val32
>> 16);
239 robo_reg(page
, reg
, REG_MII_ADDR_WRITE
);
242 /* checks that attached switch is 5325E/5350 */
243 static int robo_vlan5350(void)
245 /* set vlan access id to 15 and read it back */
247 robo_write16(ROBO_VLAN_PAGE
, ROBO_VLAN_TABLE_ACCESS_5350
, val16
);
249 /* 5365 will refuse this as it does not have this reg */
250 return (robo_read16(ROBO_VLAN_PAGE
, ROBO_VLAN_TABLE_ACCESS_5350
) == val16
);
253 static int robo_switch_enable(void)
255 unsigned int i
, last_port
;
258 val
= robo_read16(ROBO_CTRL_PAGE
, ROBO_SWITCH_MODE
);
259 if (!(val
& (1 << 1))) {
262 /* With forwarding */
264 robo_write16(ROBO_CTRL_PAGE
, ROBO_SWITCH_MODE
, val
);
265 val
= robo_read16(ROBO_CTRL_PAGE
, ROBO_SWITCH_MODE
);
266 if (!(val
& (1 << 1))) {
267 printk("Failed to enable switch\n");
271 last_port
= (robo
.devid
== ROBO_DEVICE_ID_5398
) ?
272 ROBO_PORT6_CTRL
: ROBO_PORT3_CTRL
;
273 for (i
= ROBO_PORT0_CTRL
; i
< last_port
+ 1; i
++)
274 robo_write16(ROBO_CTRL_PAGE
, i
, 0);
277 /* WAN port LED, except for Netgear WGT634U */
278 if (strcmp(nvram_get("nvram_type"), "cfe"))
279 robo_write16(ROBO_CTRL_PAGE
, 0x16, 0x1F);
284 static void robo_switch_reset(void)
286 if ((robo
.devid
== ROBO_DEVICE_ID_5395
) ||
287 (robo
.devid
== ROBO_DEVICE_ID_5397
) ||
288 (robo
.devid
== ROBO_DEVICE_ID_5398
)) {
289 /* Trigger a software reset. */
290 robo_write16(ROBO_CTRL_PAGE
, 0x79, 0x83);
291 robo_write16(ROBO_CTRL_PAGE
, 0x79, 0);
295 static int robo_probe(char *devname
)
301 printk(KERN_INFO PFX
"Probing device %s: ", devname
);
302 strcpy(robo
.ifr
.ifr_name
, devname
);
304 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
305 if ((robo
.dev
= dev_get_by_name(devname
)) == NULL
) {
307 if ((robo
.dev
= dev_get_by_name(&init_net
, devname
)) == NULL
) {
309 printk("No such device\n");
313 robo
.device
= devname
;
314 for (i
= 0; i
< 5; i
++)
318 /* try access using MII ioctls - get phy address */
319 if (do_ioctl(SIOCGMIIPHY
, NULL
) < 0) {
321 robo
.phy_addr
= ROBO_PHY_ADDR
;
323 /* got phy address check for robo address */
324 struct mii_ioctl_data
*mii
= (struct mii_ioctl_data
*) &robo
.ifr
.ifr_data
;
325 if ((mii
->phy_id
!= ROBO_PHY_ADDR
) &&
326 (mii
->phy_id
!= ROBO_PHY_ADDR_BCM63XX
) &&
327 (mii
->phy_id
!= ROBO_PHY_ADDR_TG3
)) {
328 printk("Invalid phy address (%d)\n", mii
->phy_id
);
332 /* The robo has a fixed PHY address that is different from the
333 * Tigon3 and BCM63xx PHY address. */
334 robo
.phy_addr
= ROBO_PHY_ADDR
;
337 phyid
= mdio_read(robo
.phy_addr
, 0x2) |
338 (mdio_read(robo
.phy_addr
, 0x3) << 16);
340 if (phyid
== 0xffffffff || phyid
== 0x55210022) {
341 printk("No Robo switch in managed mode found, phy_id = 0x%08x\n", phyid
);
345 /* Get the device ID */
346 for (i
= 0; i
< 10; i
++) {
347 robo
.devid
= robo_read16(ROBO_MGMT_PAGE
, ROBO_DEVICE_ID
);
353 robo
.devid
= ROBO_DEVICE_ID_5325
; /* Fake it */
354 robo
.is_5350
= robo_vlan5350();
357 err
= robo_switch_enable();
366 static int handle_vlan_port_read(void *driver
, char *buf
, int nr
)
372 val16
= (nr
) /* vlan */ | (0 << 12) /* read */ | (1 << 13) /* enable */;
376 robo_write16(ROBO_VLAN_PAGE
, ROBO_VLAN_TABLE_ACCESS_5350
, val16
);
378 val32
= robo_read32(ROBO_VLAN_PAGE
, ROBO_VLAN_READ
);
379 if ((val32
& (1 << 20)) /* valid */) {
380 for (j
= 0; j
< 6; j
++) {
381 if (val32
& (1 << j
)) {
382 len
+= sprintf(buf
+ len
, "%d", j
);
383 if (val32
& (1 << (j
+ 6))) {
384 if (j
== 5) buf
[len
++] = 'u';
387 if (robo_read16(ROBO_VLAN_PAGE
, ROBO_VLAN_PORT0_DEF_TAG
+ (j
<< 1)) == nr
)
393 len
+= sprintf(buf
+ len
, "\n");
396 robo_write16(ROBO_VLAN_PAGE
, ROBO_VLAN_TABLE_ACCESS
, val16
);
398 val16
= robo_read16(ROBO_VLAN_PAGE
, ROBO_VLAN_READ
);
399 if ((val16
& (1 << 14)) /* valid */) {
400 for (j
= 0; j
< 6; j
++) {
401 if (val16
& (1 << j
)) {
402 len
+= sprintf(buf
+ len
, "%d", j
);
403 if (val16
& (1 << (j
+ 7))) {
404 if (j
== 5) buf
[len
++] = 'u';
407 if (robo_read16(ROBO_VLAN_PAGE
, ROBO_VLAN_PORT0_DEF_TAG
+ (j
<< 1)) == nr
)
413 len
+= sprintf(buf
+ len
, "\n");
422 static int handle_vlan_port_write(void *driver
, char *buf
, int nr
)
424 switch_driver
*d
= (switch_driver
*) driver
;
425 switch_vlan_config
*c
= switch_parse_vlan(d
, buf
);
432 for (j
= 0; j
< d
->ports
; j
++) {
433 if ((c
->untag
| c
->pvid
) & (1 << j
))
434 /* change default vlan tag */
435 robo_write16(ROBO_VLAN_PAGE
, ROBO_VLAN_PORT0_DEF_TAG
+ (j
<< 1), nr
);
438 /* write config now */
439 val16
= (nr
) /* vlan */ | (1 << 12) /* write */ | (1 << 13) /* enable */;
441 robo_write32(ROBO_VLAN_PAGE
, ROBO_VLAN_WRITE_5350
,
442 (1 << 20) /* valid */ | (c
->untag
<< 6) | c
->port
);
443 robo_write16(ROBO_VLAN_PAGE
, ROBO_VLAN_TABLE_ACCESS_5350
, val16
);
445 robo_write16(ROBO_VLAN_PAGE
, ROBO_VLAN_WRITE
,
446 (1 << 14) /* valid */ | (c
->untag
<< 7) | c
->port
);
447 robo_write16(ROBO_VLAN_PAGE
, ROBO_VLAN_TABLE_ACCESS
, val16
);
453 #define set_switch(state) \
454 robo_write16(ROBO_CTRL_PAGE, ROBO_SWITCH_MODE, (robo_read16(ROBO_CTRL_PAGE, ROBO_SWITCH_MODE) & ~2) | (state ? 2 : 0));
456 static int handle_enable_read(void *driver
, char *buf
, int nr
)
458 return sprintf(buf
, "%d\n", (((robo_read16(ROBO_CTRL_PAGE
, ROBO_SWITCH_MODE
) & 2) == 2) ? 1 : 0));
461 static int handle_enable_write(void *driver
, char *buf
, int nr
)
463 set_switch(buf
[0] == '1');
468 static int handle_enable_vlan_read(void *driver
, char *buf
, int nr
)
470 return sprintf(buf
, "%d\n", (((robo_read16(ROBO_VLAN_PAGE
, ROBO_VLAN_CTRL0
) & (1 << 7)) == (1 << 7)) ? 1 : 0));
473 static int handle_enable_vlan_write(void *driver
, char *buf
, int nr
)
475 int disable
= ((buf
[0] != '1') ? 1 : 0);
477 robo_write16(ROBO_VLAN_PAGE
, ROBO_VLAN_CTRL0
, disable
? 0 :
478 (1 << 7) /* 802.1Q VLAN */ | (3 << 5) /* mac check and hash */);
479 robo_write16(ROBO_VLAN_PAGE
, ROBO_VLAN_CTRL1
, disable
? 0 :
480 (1 << 1) | (1 << 2) | (1 << 3) /* RSV multicast */);
481 robo_write16(ROBO_VLAN_PAGE
, ROBO_VLAN_CTRL4
, disable
? 0 :
482 (1 << 6) /* drop invalid VID frames */);
483 robo_write16(ROBO_VLAN_PAGE
, ROBO_VLAN_CTRL5
, disable
? 0 :
484 (1 << 3) /* drop miss V table frames */);
489 static int handle_reset(void *driver
, char *buf
, int nr
)
491 switch_driver
*d
= (switch_driver
*) driver
;
492 switch_vlan_config
*c
= switch_parse_vlan(d
, buf
);
499 /* disable switching */
503 for (j
= 0; j
<= ((robo
.is_5350
) ? VLAN_ID_MAX5350
: VLAN_ID_MAX
); j
++) {
504 /* write config now */
505 val16
= (j
) /* vlan */ | (1 << 12) /* write */ | (1 << 13) /* enable */;
507 robo_write32(ROBO_VLAN_PAGE
, ROBO_VLAN_WRITE_5350
, 0);
509 robo_write16(ROBO_VLAN_PAGE
, ROBO_VLAN_WRITE
, 0);
510 robo_write16(ROBO_VLAN_PAGE
, robo
.is_5350
? ROBO_VLAN_TABLE_ACCESS_5350
:
511 ROBO_VLAN_TABLE_ACCESS
,
515 /* reset ports to a known good state */
516 for (j
= 0; j
< d
->ports
; j
++) {
517 robo_write16(ROBO_CTRL_PAGE
, robo
.port
[j
], 0x0000);
518 robo_write16(ROBO_VLAN_PAGE
, ROBO_VLAN_PORT0_DEF_TAG
+ (j
<< 1), 0);
521 /* enable switching */
525 handle_enable_vlan_write(driver
, "1", 0);
530 static int __init
robo_init(void)
535 device
= strdup("ethX");
536 for (device
[3] = '0'; (device
[3] <= '3') && notfound
; device
[3]++) {
537 if (! switch_device_registered (device
))
538 notfound
= robo_probe(device
);
546 static const switch_config cfg
[] = {
549 .read
= handle_enable_read
,
550 .write
= handle_enable_write
552 .name
= "enable_vlan",
553 .read
= handle_enable_vlan_read
,
554 .write
= handle_enable_vlan_write
558 .write
= handle_reset
561 static const switch_config vlan
[] = {
564 .read
= handle_vlan_port_read
,
565 .write
= handle_vlan_port_write
568 switch_driver driver
= {
570 .version
= DRIVER_VERSION
,
575 .driver_handlers
= cfg
,
576 .port_handlers
= NULL
,
577 .vlan_handlers
= vlan
,
580 return switch_register_driver(&driver
);
584 static void __exit
robo_exit(void)
586 switch_unregister_driver(DRIVER_NAME
);
591 MODULE_AUTHOR("Felix Fietkau <openwrt@nbd.name>");
592 MODULE_LICENSE("GPL");
594 module_init(robo_init
);
595 module_exit(robo_exit
);