2 * Broadcom BCM5325E/536x switch configuration module
4 * Copyright (C) 2005 Felix Fietkau <nbd@nbd.name>
5 * Based on 'robocfg' by Oleg I. Vdovikin
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23 #include <linux/autoconf.h>
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 <asm/uaccess.h>
33 #include "switch-core.h"
36 #define DRIVER_NAME "bcm53xx"
37 #define DRIVER_VERSION "0.01"
39 #define ROBO_PHY_ADDR 0x1E /* robo switch phy address */
42 #define REG_MII_PAGE 0x10 /* MII Page register */
43 #define REG_MII_ADDR 0x11 /* MII Address register */
44 #define REG_MII_DATA0 0x18 /* MII Data register 0 */
46 #define REG_MII_PAGE_ENABLE 1
47 #define REG_MII_ADDR_WRITE 1
48 #define REG_MII_ADDR_READ 2
50 /* Private et.o ioctls */
51 #define SIOCGETCPHYRD (SIOCDEVPRIVATE + 9)
52 #define SIOCSETCPHYWR (SIOCDEVPRIVATE + 10)
55 static int use_et
= 0;
56 static int is_5350
= 0;
57 static struct ifreq ifr
;
58 static struct net_device
*dev
;
59 static unsigned char port
[6] = { 0, 1, 2, 3, 4, 8 };
61 static int do_ioctl(int cmd
, void *buf
)
63 mm_segment_t old_fs
= get_fs();
67 ifr
.ifr_data
= (caddr_t
) buf
;
70 ret
= dev
->do_ioctl(dev
, &ifr
, cmd
);
76 static u16
mdio_read(__u16 phy_id
, __u8 reg
)
79 int args
[2] = { reg
};
81 if (phy_id
!= ROBO_PHY_ADDR
) {
83 "Access to real 'phy' registers unavaliable.\n"
84 "Upgrade kernel driver.\n");
90 if (do_ioctl(SIOCGETCPHYRD
, &args
) < 0) {
91 printk("[%s:%d] SIOCGETCPHYRD failed!\n", __FILE__
, __LINE__
);
97 struct mii_ioctl_data
*mii
= (struct mii_ioctl_data
*) &ifr
.ifr_data
;
101 if (do_ioctl(SIOCGMIIREG
, NULL
) < 0) {
102 printk("[%s:%d] SIOCGMIIREG failed!\n", __FILE__
, __LINE__
);
111 static void mdio_write(__u16 phy_id
, __u8 reg
, __u16 val
)
114 int args
[2] = { reg
, val
};
116 if (phy_id
!= ROBO_PHY_ADDR
) {
118 "Access to real 'phy' registers unavaliable.\n"
119 "Upgrade kernel driver.\n");
124 if (do_ioctl(SIOCSETCPHYWR
, args
) < 0) {
125 printk("[%s:%d] SIOCGETCPHYWR failed!\n", __FILE__
, __LINE__
);
129 struct mii_ioctl_data
*mii
= (struct mii_ioctl_data
*)&ifr
.ifr_data
;
131 mii
->phy_id
= phy_id
;
135 if (do_ioctl(SIOCSMIIREG
, NULL
) < 0) {
136 printk("[%s:%d] SIOCSMIIREG failed!\n", __FILE__
, __LINE__
);
142 static int robo_reg(__u8 page
, __u8 reg
, __u8 op
)
146 /* set page number */
147 mdio_write(ROBO_PHY_ADDR
, REG_MII_PAGE
,
148 (page
<< 8) | REG_MII_PAGE_ENABLE
);
150 /* set register address */
151 mdio_write(ROBO_PHY_ADDR
, REG_MII_ADDR
,
154 /* check if operation completed */
156 if ((mdio_read(ROBO_PHY_ADDR
, REG_MII_ADDR
) & 3) == 0)
160 printk("[%s:%d] timeout in robo_reg!\n", __FILE__
, __LINE__
);
166 static void robo_read(__u8 page, __u8 reg, __u16 *val, int count)
170 robo_reg(page, reg, REG_MII_ADDR_READ);
172 for (i = 0; i < count; i++)
173 val[i] = mdio_read(ROBO_PHY_ADDR, REG_MII_DATA0 + i);
177 static __u16
robo_read16(__u8 page
, __u8 reg
)
179 robo_reg(page
, reg
, REG_MII_ADDR_READ
);
181 return mdio_read(ROBO_PHY_ADDR
, REG_MII_DATA0
);
184 static __u32
robo_read32(__u8 page
, __u8 reg
)
186 robo_reg(page
, reg
, REG_MII_ADDR_READ
);
188 return mdio_read(ROBO_PHY_ADDR
, REG_MII_DATA0
) +
189 (mdio_read(ROBO_PHY_ADDR
, REG_MII_DATA0
+ 1) << 16);
192 static void robo_write16(__u8 page
, __u8 reg
, __u16 val16
)
195 mdio_write(ROBO_PHY_ADDR
, REG_MII_DATA0
, val16
);
197 robo_reg(page
, reg
, REG_MII_ADDR_WRITE
);
200 static void robo_write32(__u8 page
, __u8 reg
, __u32 val32
)
203 mdio_write(ROBO_PHY_ADDR
, REG_MII_DATA0
, val32
& 65535);
204 mdio_write(ROBO_PHY_ADDR
, REG_MII_DATA0
+ 1, val32
>> 16);
206 robo_reg(page
, reg
, REG_MII_ADDR_WRITE
);
209 /* checks that attached switch is 5325E/5350 */
210 static int robo_vlan5350(void)
212 /* set vlan access id to 15 and read it back */
214 robo_write16(ROBO_VLAN_PAGE
, ROBO_VLAN_TABLE_ACCESS_5350
, val16
);
216 /* 5365 will refuse this as it does not have this reg */
217 return (robo_read16(ROBO_VLAN_PAGE
, ROBO_VLAN_TABLE_ACCESS_5350
) == val16
);
222 static int robo_probe(char *devname
)
226 printk("Probing device %s: ", devname
);
227 strcpy(ifr
.ifr_name
, devname
);
229 if ((dev
= dev_get_by_name(devname
)) == NULL
) {
230 printk("No such device\n");
234 /* try access using MII ioctls - get phy address */
235 if (do_ioctl(SIOCGMIIPHY
, NULL
) < 0) {
238 /* got phy address check for robo address */
239 struct mii_ioctl_data
*mii
= (struct mii_ioctl_data
*) &ifr
.ifr_data
;
240 if (mii
->phy_id
!= ROBO_PHY_ADDR
) {
241 printk("Invalid phy address (%d)\n", mii
->phy_id
);
246 phyid
= mdio_read(ROBO_PHY_ADDR
, 0x2) |
247 (mdio_read(ROBO_PHY_ADDR
, 0x3) << 16);
249 if (phyid
== 0xffffffff || phyid
== 0x55210022) {
250 printk("No Robo switch in managed mode found\n");
254 is_5350
= robo_vlan5350();
261 static int handle_vlan_port_read(void *driver
, char *buf
, int nr
)
267 val16
= (nr
) /* vlan */ | (0 << 12) /* read */ | (1 << 13) /* enable */;
271 robo_write16(ROBO_VLAN_PAGE
, ROBO_VLAN_TABLE_ACCESS_5350
, val16
);
273 val32
= robo_read32(ROBO_VLAN_PAGE
, ROBO_VLAN_READ
);
274 if ((val32
& (1 << 20)) /* valid */) {
275 for (j
= 0; j
< 6; j
++) {
276 if (val32
& (1 << j
)) {
277 len
+= sprintf(buf
+ len
, "%d", j
);
278 if (val32
& (1 << (j
+ 6))) {
279 if (j
== 5) buf
[len
++] = 'u';
282 if (robo_read16(ROBO_VLAN_PAGE
, ROBO_VLAN_PORT0_DEF_TAG
+ (j
<< 1)) == nr
)
288 len
+= sprintf(buf
+ len
, "\n");
291 robo_write16(ROBO_VLAN_PAGE
, ROBO_VLAN_TABLE_ACCESS
, val16
);
293 val16
= robo_read16(ROBO_VLAN_PAGE
, ROBO_VLAN_READ
);
294 if ((val16
& (1 << 14)) /* valid */) {
295 for (j
= 0; j
< 6; j
++) {
296 if (val16
& (1 << j
)) {
297 len
+= sprintf(buf
+ len
, "%d", j
);
298 if (val16
& (1 << (j
+ 7))) {
299 if (j
== 5) buf
[len
++] = 'u';
302 if (robo_read16(ROBO_VLAN_PAGE
, ROBO_VLAN_PORT0_DEF_TAG
+ (j
<< 1)) == nr
)
308 len
+= sprintf(buf
+ len
, "\n");
317 static int handle_vlan_port_write(void *driver
, char *buf
, int nr
)
319 switch_driver
*d
= (switch_driver
*) driver
;
320 switch_vlan_config
*c
= switch_parse_vlan(d
, buf
);
327 for (j
= 0; j
< d
->ports
; j
++) {
328 if ((c
->untag
| c
->pvid
) & (1 << j
))
329 /* change default vlan tag */
330 robo_write16(ROBO_VLAN_PAGE
, ROBO_VLAN_PORT0_DEF_TAG
+ (j
<< 1), nr
);
333 /* write config now */
334 val16
= (nr
) /* vlan */ | (1 << 12) /* write */ | (1 << 13) /* enable */;
336 robo_write32(ROBO_VLAN_PAGE
, ROBO_VLAN_WRITE_5350
,
337 (1 << 20) /* valid */ | (c
->untag
<< 6) | c
->port
);
338 robo_write16(ROBO_VLAN_PAGE
, ROBO_VLAN_TABLE_ACCESS_5350
, val16
);
340 robo_write16(ROBO_VLAN_PAGE
, ROBO_VLAN_WRITE
,
341 (1 << 14) /* valid */ | (c
->untag
<< 7) | c
->port
);
342 robo_write16(ROBO_VLAN_PAGE
, ROBO_VLAN_TABLE_ACCESS
, val16
);
348 #define set_switch(state) \
349 robo_write16(ROBO_CTRL_PAGE, ROBO_SWITCH_MODE, (robo_read16(ROBO_CTRL_PAGE, ROBO_SWITCH_MODE) & ~2) | (state ? 2 : 0));
351 static int handle_enable_read(void *driver
, char *buf
, int nr
)
353 return sprintf(buf
, "%d\n", (((robo_read16(ROBO_CTRL_PAGE
, ROBO_SWITCH_MODE
) & 2) == 2) ? 1 : 0));
356 static int handle_enable_write(void *driver
, char *buf
, int nr
)
358 set_switch(buf
[0] == '1');
363 static int handle_enable_vlan_read(void *driver
, char *buf
, int nr
)
365 return sprintf(buf
, "%d\n", (((robo_read16(ROBO_VLAN_PAGE
, ROBO_VLAN_CTRL0
) & (1 << 7)) == (1 << 7)) ? 1 : 0));
368 static int handle_enable_vlan_write(void *driver
, char *buf
, int nr
)
370 int disable
= ((buf
[0] != '1') ? 1 : 0);
372 robo_write16(ROBO_VLAN_PAGE
, ROBO_VLAN_CTRL0
, disable
? 0 :
373 (1 << 7) /* 802.1Q VLAN */ | (3 << 5) /* mac check and hash */);
374 robo_write16(ROBO_VLAN_PAGE
, ROBO_VLAN_CTRL1
, disable
? 0 :
375 (1 << 1) | (1 << 2) | (1 << 3) /* RSV multicast */);
376 robo_write16(ROBO_VLAN_PAGE
, ROBO_VLAN_CTRL4
, disable
? 0 :
377 (1 << 6) /* drop invalid VID frames */);
378 robo_write16(ROBO_VLAN_PAGE
, ROBO_VLAN_CTRL5
, disable
? 0 :
379 (1 << 3) /* drop miss V table frames */);
384 static int handle_reset(void *driver
, char *buf
, int nr
)
386 switch_driver
*d
= (switch_driver
*) driver
;
387 switch_vlan_config
*c
= switch_parse_vlan(d
, buf
);
394 /* disable switching */
398 for (j
= 0; j
<= (is_5350
? VLAN_ID_MAX5350
: VLAN_ID_MAX
); j
++) {
399 /* write config now */
400 val16
= (j
) /* vlan */ | (1 << 12) /* write */ | (1 << 13) /* enable */;
402 robo_write32(ROBO_VLAN_PAGE
, ROBO_VLAN_WRITE_5350
, 0);
404 robo_write16(ROBO_VLAN_PAGE
, ROBO_VLAN_WRITE
, 0);
405 robo_write16(ROBO_VLAN_PAGE
, (is_5350
? ROBO_VLAN_TABLE_ACCESS_5350
: ROBO_VLAN_TABLE_ACCESS
), val16
);
408 /* reset ports to a known good state */
409 for (j
= 0; j
< d
->ports
; j
++) {
410 robo_write16(ROBO_CTRL_PAGE
, port
[j
], 0x0000);
411 robo_write16(ROBO_VLAN_PAGE
, ROBO_VLAN_PORT0_DEF_TAG
+ (j
<< 1), 0);
414 /* enable switching */
418 handle_enable_vlan_write(driver
, "1", 0);
423 static int __init
robo_init(void)
427 device
= strdup("ethX");
428 for (device
[3] = '0'; (device
[3] <= '3') && notfound
; device
[3]++) {
429 notfound
= robo_probe(device
);
437 switch_config cfg
[] = {
438 {"enable", handle_enable_read
, handle_enable_write
},
439 {"enable_vlan", handle_enable_vlan_read
, handle_enable_vlan_write
},
440 {"reset", NULL
, handle_reset
},
443 switch_config vlan
[] = {
444 {"ports", handle_vlan_port_read
, handle_vlan_port_write
},
447 switch_driver driver
= {
449 version
: DRIVER_VERSION
,
454 driver_handlers
: cfg
,
459 return switch_register_driver(&driver
);
463 static void __exit
robo_exit(void)
465 switch_unregister_driver(DRIVER_NAME
);
470 MODULE_AUTHOR("Felix Fietkau <openwrt@nbd.name>");
471 MODULE_LICENSE("GPL");
473 module_init(robo_init
);
474 module_exit(robo_exit
);