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
);
107 "[%s:%d] SIOCGMIIREG failed! err: %i\n", __FILE__
, __LINE__
, err
);
115 static void mdio_write(__u16 phy_id
, __u8 reg
, __u16 val
)
117 struct mii_ioctl_data
*mii
= if_mii(&robo
.ifr
);
120 mii
->phy_id
= phy_id
;
124 err
= do_ioctl(SIOCSMIIREG
);
127 "[%s:%d] SIOCSMIIREG failed! err: %i\n", __FILE__
, __LINE__
, err
);
132 static int robo_reg(__u8 page
, __u8 reg
, __u8 op
)
136 /* set page number */
137 mdio_write(ROBO_PHY_ADDR
, REG_MII_PAGE
,
138 (page
<< 8) | REG_MII_PAGE_ENABLE
);
140 /* set register address */
141 mdio_write(ROBO_PHY_ADDR
, REG_MII_ADDR
,
144 /* check if operation completed */
146 if ((mdio_read(ROBO_PHY_ADDR
, REG_MII_ADDR
) & 3) == 0)
150 printk(KERN_ERR PFX
"[%s:%d] timeout in robo_reg!\n", __FILE__
, __LINE__
);
156 static void robo_read(__u8 page, __u8 reg, __u16 *val, int count)
160 robo_reg(page, reg, REG_MII_ADDR_READ);
162 for (i = 0; i < count; i++)
163 val[i] = mdio_read(ROBO_PHY_ADDR, REG_MII_DATA0 + i);
167 static __u16
robo_read16(__u8 page
, __u8 reg
)
169 robo_reg(page
, reg
, REG_MII_ADDR_READ
);
171 return mdio_read(ROBO_PHY_ADDR
, REG_MII_DATA0
);
174 static __u32
robo_read32(__u8 page
, __u8 reg
)
176 robo_reg(page
, reg
, REG_MII_ADDR_READ
);
178 return mdio_read(ROBO_PHY_ADDR
, REG_MII_DATA0
) +
179 (mdio_read(ROBO_PHY_ADDR
, REG_MII_DATA0
+ 1) << 16);
182 static void robo_write16(__u8 page
, __u8 reg
, __u16 val16
)
185 mdio_write(ROBO_PHY_ADDR
, REG_MII_DATA0
, val16
);
187 robo_reg(page
, reg
, REG_MII_ADDR_WRITE
);
190 static void robo_write32(__u8 page
, __u8 reg
, __u32 val32
)
193 mdio_write(ROBO_PHY_ADDR
, REG_MII_DATA0
, val32
& 65535);
194 mdio_write(ROBO_PHY_ADDR
, REG_MII_DATA0
+ 1, val32
>> 16);
196 robo_reg(page
, reg
, REG_MII_ADDR_WRITE
);
199 /* checks that attached switch is 5325E/5350 */
200 static int robo_vlan5350(void)
202 /* set vlan access id to 15 and read it back */
204 robo_write16(ROBO_VLAN_PAGE
, ROBO_VLAN_TABLE_ACCESS_5350
, val16
);
206 /* 5365 will refuse this as it does not have this reg */
207 return (robo_read16(ROBO_VLAN_PAGE
, ROBO_VLAN_TABLE_ACCESS_5350
) == val16
);
210 static int robo_switch_enable(void)
212 unsigned int i
, last_port
;
214 #ifdef CONFIG_BCM47XX
218 val
= robo_read16(ROBO_CTRL_PAGE
, ROBO_SWITCH_MODE
);
219 if (!(val
& (1 << 1))) {
222 /* With forwarding */
224 robo_write16(ROBO_CTRL_PAGE
, ROBO_SWITCH_MODE
, val
);
225 val
= robo_read16(ROBO_CTRL_PAGE
, ROBO_SWITCH_MODE
);
226 if (!(val
& (1 << 1))) {
227 printk("Failed to enable switch\n");
231 last_port
= (robo
.devid
== ROBO_DEVICE_ID_5398
) ?
232 ROBO_PORT6_CTRL
: ROBO_PORT3_CTRL
;
233 for (i
= ROBO_PORT0_CTRL
; i
< last_port
+ 1; i
++)
234 robo_write16(ROBO_CTRL_PAGE
, i
, 0);
237 #ifdef CONFIG_BCM47XX
238 /* WAN port LED, except for Netgear WGT634U */
239 if (bcm47xx_nvram_getenv("nvram_type", buf
, sizeof(buf
)) >= 0) {
240 if (strcmp(buf
, "cfe") != 0)
241 robo_write16(ROBO_CTRL_PAGE
, 0x16, 0x1F);
247 static void robo_switch_reset(void)
249 if ((robo
.devid
== ROBO_DEVICE_ID_5395
) ||
250 (robo
.devid
== ROBO_DEVICE_ID_5397
) ||
251 (robo
.devid
== ROBO_DEVICE_ID_5398
)) {
252 /* Trigger a software reset. */
253 robo_write16(ROBO_CTRL_PAGE
, 0x79, 0x83);
255 robo_write16(ROBO_CTRL_PAGE
, 0x79, 0);
259 static int robo_probe(char *devname
)
265 printk(KERN_INFO PFX
"Probing device %s: ", devname
);
266 strcpy(robo
.ifr
.ifr_name
, devname
);
268 if ((robo
.dev
= dev_get_by_name(&init_net
, devname
)) == NULL
) {
269 printk("No such device\n");
272 if (!robo
.dev
->netdev_ops
|| !robo
.dev
->netdev_ops
->ndo_do_ioctl
) {
273 printk("ndo_do_ioctl not implemented in ethernet driver\n");
277 robo
.device
= devname
;
278 for (i
= 0; i
< 5; i
++)
282 /* try access using MII ioctls - get phy address */
283 if (do_ioctl(SIOCGMIIPHY
) < 0) {
284 printk("error while accessing MII phy registers with ioctls\n");
288 /* got phy address check for robo address */
289 struct mii_ioctl_data
*mii
= if_mii(&robo
.ifr
);
290 if ((mii
->phy_id
!= ROBO_PHY_ADDR
) &&
291 (mii
->phy_id
!= ROBO_PHY_ADDR_BCM63XX
) &&
292 (mii
->phy_id
!= ROBO_PHY_ADDR_TG3
)) {
293 printk("Invalid phy address (%d)\n", mii
->phy_id
);
297 phyid
= mdio_read(ROBO_PHY_ADDR
, 0x2) |
298 (mdio_read(ROBO_PHY_ADDR
, 0x3) << 16);
300 if (phyid
== 0xffffffff || phyid
== 0x55210022) {
301 printk("No Robo switch in managed mode found, phy_id = 0x%08x\n", phyid
);
305 /* Get the device ID */
306 for (i
= 0; i
< 10; i
++) {
307 robo
.devid
= robo_read16(ROBO_MGMT_PAGE
, ROBO_DEVICE_ID
);
313 robo
.devid
= ROBO_DEVICE_ID_5325
; /* Fake it */
314 robo
.is_5350
= robo_vlan5350();
317 err
= robo_switch_enable();
322 printk("found a 5%s%x!%s\n", robo
.devid
& 0xff00 ? "" : "3", robo
.devid
,
323 robo
.is_5350
? " It's a 5350." : "");
334 static int handle_vlan_port_read(void *driver
, char *buf
, int nr
)
340 val16
= (nr
) /* vlan */ | (0 << 12) /* read */ | (1 << 13) /* enable */;
344 robo_write16(ROBO_VLAN_PAGE
, ROBO_VLAN_TABLE_ACCESS_5350
, val16
);
346 val32
= robo_read32(ROBO_VLAN_PAGE
, ROBO_VLAN_READ
);
347 if ((val32
& (1 << 20)) /* valid */) {
348 for (j
= 0; j
< 6; j
++) {
349 if (val32
& (1 << j
)) {
350 len
+= sprintf(buf
+ len
, "%d", j
);
351 if (val32
& (1 << (j
+ 6))) {
352 if (j
== 5) buf
[len
++] = 'u';
355 if (robo_read16(ROBO_VLAN_PAGE
, ROBO_VLAN_PORT0_DEF_TAG
+ (j
<< 1)) == nr
)
361 len
+= sprintf(buf
+ len
, "\n");
364 robo_write16(ROBO_VLAN_PAGE
, ROBO_VLAN_TABLE_ACCESS
, val16
);
366 val16
= robo_read16(ROBO_VLAN_PAGE
, ROBO_VLAN_READ
);
367 if ((val16
& (1 << 14)) /* valid */) {
368 for (j
= 0; j
< 6; j
++) {
369 if (val16
& (1 << j
)) {
370 len
+= sprintf(buf
+ len
, "%d", j
);
371 if (val16
& (1 << (j
+ 7))) {
372 if (j
== 5) buf
[len
++] = 'u';
375 if (robo_read16(ROBO_VLAN_PAGE
, ROBO_VLAN_PORT0_DEF_TAG
+ (j
<< 1)) == nr
)
381 len
+= sprintf(buf
+ len
, "\n");
390 static int handle_vlan_port_write(void *driver
, char *buf
, int nr
)
392 switch_driver
*d
= (switch_driver
*) driver
;
393 switch_vlan_config
*c
= switch_parse_vlan(d
, buf
);
400 for (j
= 0; j
< d
->ports
; j
++) {
401 if ((c
->untag
| c
->pvid
) & (1 << j
))
402 /* change default vlan tag */
403 robo_write16(ROBO_VLAN_PAGE
, ROBO_VLAN_PORT0_DEF_TAG
+ (j
<< 1), nr
);
406 /* write config now */
408 if (robo
.devid
!= ROBO_DEVICE_ID_5325
) {
409 __u8 regoff
= ((robo
.devid
== ROBO_DEVICE_ID_5395
) ||
410 (robo
.devid
== ROBO_DEVICE_ID_53115
)) ? 0x20 : 0;
412 robo_write32(ROBO_ARLIO_PAGE
, 0x63 + regoff
, (c
->untag
<< 9) | c
->port
);
413 robo_write16(ROBO_ARLIO_PAGE
, 0x61 + regoff
, nr
);
414 robo_write16(ROBO_ARLIO_PAGE
, 0x60 + regoff
, 1 << 7);
419 val16
= (nr
) /* vlan */ | (1 << 12) /* write */ | (1 << 13) /* enable */;
421 robo_write32(ROBO_VLAN_PAGE
, ROBO_VLAN_WRITE_5350
,
422 (1 << 20) /* valid */ | (c
->untag
<< 6) | c
->port
);
423 robo_write16(ROBO_VLAN_PAGE
, ROBO_VLAN_TABLE_ACCESS_5350
, val16
);
425 robo_write16(ROBO_VLAN_PAGE
, ROBO_VLAN_WRITE
,
426 (1 << 14) /* valid */ | (c
->untag
<< 7) | c
->port
);
427 robo_write16(ROBO_VLAN_PAGE
, ROBO_VLAN_TABLE_ACCESS
, val16
);
434 #define set_switch(state) \
435 robo_write16(ROBO_CTRL_PAGE, ROBO_SWITCH_MODE, (robo_read16(ROBO_CTRL_PAGE, ROBO_SWITCH_MODE) & ~2) | (state ? 2 : 0));
437 static int handle_enable_read(void *driver
, char *buf
, int nr
)
439 return sprintf(buf
, "%d\n", (((robo_read16(ROBO_CTRL_PAGE
, ROBO_SWITCH_MODE
) & 2) == 2) ? 1 : 0));
442 static int handle_enable_write(void *driver
, char *buf
, int nr
)
444 set_switch(buf
[0] == '1');
449 static int handle_enable_vlan_read(void *driver
, char *buf
, int nr
)
451 return sprintf(buf
, "%d\n", (((robo_read16(ROBO_VLAN_PAGE
, ROBO_VLAN_CTRL0
) & (1 << 7)) == (1 << 7)) ? 1 : 0));
454 static int handle_enable_vlan_write(void *driver
, char *buf
, int nr
)
456 int disable
= ((buf
[0] != '1') ? 1 : 0);
458 robo_write16(ROBO_VLAN_PAGE
, ROBO_VLAN_CTRL0
, disable
? 0 :
459 (1 << 7) /* 802.1Q VLAN */ | (3 << 5) /* mac check and hash */);
460 robo_write16(ROBO_VLAN_PAGE
, ROBO_VLAN_CTRL1
, disable
? 0 :
461 (robo
.devid
== ROBO_DEVICE_ID_5325
? (1 << 1) :
462 0) | (1 << 2) | (1 << 3)); /* RSV multicast */
464 if (robo
.devid
!= ROBO_DEVICE_ID_5325
)
467 robo_write16(ROBO_VLAN_PAGE
, ROBO_VLAN_CTRL4
, disable
? 0 :
468 (1 << 6) /* drop invalid VID frames */);
469 robo_write16(ROBO_VLAN_PAGE
, ROBO_VLAN_CTRL5
, disable
? 0 :
470 (1 << 3) /* drop miss V table frames */);
475 static int handle_reset(void *driver
, char *buf
, int nr
)
477 switch_driver
*d
= (switch_driver
*) driver
;
481 /* disable switching */
485 for (j
= 0; j
<= ((robo
.is_5350
) ? VLAN_ID_MAX5350
: VLAN_ID_MAX
); j
++) {
486 /* write config now */
487 val16
= (j
) /* vlan */ | (1 << 12) /* write */ | (1 << 13) /* enable */;
489 robo_write32(ROBO_VLAN_PAGE
, ROBO_VLAN_WRITE_5350
, 0);
491 robo_write16(ROBO_VLAN_PAGE
, ROBO_VLAN_WRITE
, 0);
492 robo_write16(ROBO_VLAN_PAGE
, robo
.is_5350
? ROBO_VLAN_TABLE_ACCESS_5350
:
493 ROBO_VLAN_TABLE_ACCESS
,
497 /* reset ports to a known good state */
498 for (j
= 0; j
< d
->ports
; j
++) {
499 robo_write16(ROBO_CTRL_PAGE
, robo
.port
[j
], 0x0000);
500 robo_write16(ROBO_VLAN_PAGE
, ROBO_VLAN_PORT0_DEF_TAG
+ (j
<< 1), 0);
503 /* enable switching */
507 handle_enable_vlan_write(driver
, "1", 0);
512 static int __init
robo_init(void)
517 device
= strdup("ethX");
518 for (device
[3] = '0'; (device
[3] <= '3') && notfound
; device
[3]++) {
519 if (! switch_device_registered (device
))
520 notfound
= robo_probe(device
);
528 static const switch_config cfg
[] = {
531 .read
= handle_enable_read
,
532 .write
= handle_enable_write
534 .name
= "enable_vlan",
535 .read
= handle_enable_vlan_read
,
536 .write
= handle_enable_vlan_write
540 .write
= handle_reset
543 static const switch_config vlan
[] = {
546 .read
= handle_vlan_port_read
,
547 .write
= handle_vlan_port_write
550 switch_driver driver
= {
552 .version
= DRIVER_VERSION
,
557 .driver_handlers
= cfg
,
558 .port_handlers
= NULL
,
559 .vlan_handlers
= vlan
,
561 if (robo
.devid
!= ROBO_DEVICE_ID_5325
) {
566 return switch_register_driver(&driver
);
570 static void __exit
robo_exit(void)
572 switch_unregister_driver(DRIVER_NAME
);
579 MODULE_AUTHOR("Felix Fietkau <openwrt@nbd.name>");
580 MODULE_LICENSE("GPL");
582 module_init(robo_init
);
583 module_exit(robo_exit
);