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/config.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"
38 #define ROBO_PHY_ADDR 0x1E /* robo switch phy address */
41 #define REG_MII_PAGE 0x10 /* MII Page register */
42 #define REG_MII_ADDR 0x11 /* MII Address register */
43 #define REG_MII_DATA0 0x18 /* MII Data register 0 */
45 #define REG_MII_PAGE_ENABLE 1
46 #define REG_MII_ADDR_WRITE 1
47 #define REG_MII_ADDR_READ 2
49 /* Private et.o ioctls */
50 #define SIOCGETCPHYRD (SIOCDEVPRIVATE + 9)
51 #define SIOCSETCPHYWR (SIOCDEVPRIVATE + 10)
53 static int use_et = 0;
54 static int is_5350 = 0;
55 static int max_vlans, max_ports;
56 static struct ifreq ifr;
57 static struct net_device *dev;
59 static int isspace(char c) {
71 static int do_ioctl(int cmd, void *buf)
73 mm_segment_t old_fs = get_fs();
77 ifr.ifr_data = (caddr_t) buf;
80 ret = dev->do_ioctl(dev, &ifr, cmd);
86 static u16 mdio_read(__u16 phy_id, __u8 reg)
89 int args[2] = { reg };
91 if (phy_id != ROBO_PHY_ADDR) {
93 "Access to real 'phy' registers unavaliable.\n"
94 "Upgrade kernel driver.\n");
100 if (do_ioctl(SIOCGETCPHYRD, &args) < 0) {
101 printk("[%s:%d] SIOCGETCPHYRD failed!\n", __FILE__, __LINE__);
107 struct mii_ioctl_data *mii = (struct mii_ioctl_data *) &ifr.ifr_data;
108 mii->phy_id = phy_id;
111 if (do_ioctl(SIOCGMIIREG, NULL) < 0) {
112 printk("[%s:%d] SIOCGMIIREG failed!\n", __FILE__, __LINE__);
121 static void mdio_write(__u16 phy_id, __u8 reg, __u16 val)
124 int args[2] = { reg, val };
126 if (phy_id != ROBO_PHY_ADDR) {
128 "Access to real 'phy' registers unavaliable.\n"
129 "Upgrade kernel driver.\n");
134 if (do_ioctl(SIOCSETCPHYWR, args) < 0) {
135 printk("[%s:%d] SIOCGETCPHYWR failed!\n", __FILE__, __LINE__);
139 struct mii_ioctl_data *mii = (struct mii_ioctl_data *)&ifr.ifr_data;
141 mii->phy_id = phy_id;
145 if (do_ioctl(SIOCSMIIREG, NULL) < 0) {
146 printk("[%s:%d] SIOCSMIIREG failed!\n", __FILE__, __LINE__);
152 static int robo_reg(__u8 page, __u8 reg, __u8 op)
156 /* set page number */
157 mdio_write(ROBO_PHY_ADDR, REG_MII_PAGE,
158 (page << 8) | REG_MII_PAGE_ENABLE);
160 /* set register address */
161 mdio_write(ROBO_PHY_ADDR, REG_MII_ADDR,
164 /* check if operation completed */
166 if ((mdio_read(ROBO_PHY_ADDR, REG_MII_ADDR) & 3) == 0)
170 printk("[%s:%d] timeout in robo_reg!\n", __FILE__, __LINE__);
175 static void robo_read(__u8 page, __u8 reg, __u16 *val, int count)
179 robo_reg(page, reg, REG_MII_ADDR_READ);
181 for (i = 0; i < count; i++)
182 val[i] = mdio_read(ROBO_PHY_ADDR, REG_MII_DATA0 + i);
185 static __u16 robo_read16(__u8 page, __u8 reg)
187 robo_reg(page, reg, REG_MII_ADDR_READ);
189 return mdio_read(ROBO_PHY_ADDR, REG_MII_DATA0);
192 static __u32 robo_read32(__u8 page, __u8 reg)
194 robo_reg(page, reg, REG_MII_ADDR_READ);
196 return mdio_read(ROBO_PHY_ADDR, REG_MII_DATA0) +
197 (mdio_read(ROBO_PHY_ADDR, REG_MII_DATA0 + 1) << 16);
200 static void robo_write16(__u8 page, __u8 reg, __u16 val16)
203 mdio_write(ROBO_PHY_ADDR, REG_MII_DATA0, val16);
205 robo_reg(page, reg, REG_MII_ADDR_WRITE);
208 static void robo_write32(__u8 page, __u8 reg, __u32 val32)
211 mdio_write(ROBO_PHY_ADDR, REG_MII_DATA0, val32 & 65535);
212 mdio_write(ROBO_PHY_ADDR, REG_MII_DATA0 + 1, val32 >> 16);
214 robo_reg(page, reg, REG_MII_ADDR_WRITE);
217 /* checks that attached switch is 5325E/5350 */
218 static int robo_vlan5350()
220 /* set vlan access id to 15 and read it back */
222 robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_TABLE_ACCESS_5350, val16);
224 /* 5365 will refuse this as it does not have this reg */
225 return (robo_read16(ROBO_VLAN_PAGE, ROBO_VLAN_TABLE_ACCESS_5350) == val16);
230 static int robo_probe(char *devname)
232 struct ethtool_drvinfo info;
236 printk("Probing device %s: ", devname);
237 strcpy(ifr.ifr_name, devname);
239 if ((dev = dev_get_by_name(devname)) == NULL) {
240 printk("No such device\n");
244 info.cmd = ETHTOOL_GDRVINFO;
245 if (do_ioctl(SIOCETHTOOL, (void *) &info) < 0) {
246 printk("SIOCETHTOOL: not supported\n");
250 /* try access using MII ioctls - get phy address */
251 if (do_ioctl(SIOCGMIIPHY, NULL) < 0) {
254 /* got phy address check for robo address */
255 struct mii_ioctl_data *mii = (struct mii_ioctl_data *) &ifr.ifr_data;
256 if (mii->phy_id != ROBO_PHY_ADDR) {
257 printk("Invalid phy address (%d)\n", mii->phy_id);
262 phyid = mdio_read(ROBO_PHY_ADDR, 0x2) |
263 (mdio_read(ROBO_PHY_ADDR, 0x3) << 16);
265 if (phyid == 0xffffffff || phyid == 0x55210022) {
266 printk("No Robo switch in managed mode found\n");
270 is_5350 = robo_vlan5350();
273 for (i = 0; i <= (is_5350 ? VLAN_ID_MAX5350 : VLAN_ID_MAX); i++) {
275 __u16 val16 = (i) /* vlan */ | (0 << 12) /* read */ | (1 << 13) /* enable */;
279 robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_TABLE_ACCESS_5350, val16);
281 val32 = robo_read32(ROBO_VLAN_PAGE, ROBO_VLAN_READ);
282 if ((val32 & (1 << 20)) /* valid */) {
286 robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_TABLE_ACCESS, val16);
288 val16 = robo_read16(ROBO_VLAN_PAGE, ROBO_VLAN_READ);
289 if ((val16 & (1 << 14)) /* valid */) {
300 static int handle_vlan_port_read(char *buf, int nr)
306 val16 = (nr) /* vlan */ | (0 << 12) /* read */ | (1 << 13) /* enable */;
310 robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_TABLE_ACCESS_5350, val16);
312 val32 = robo_read32(ROBO_VLAN_PAGE, ROBO_VLAN_READ);
313 if ((val32 & (1 << 20)) /* valid */) {
314 for (j = 0; j < 6; j++) {
315 if (val32 & (1 << j)) {
316 len += sprintf(buf + len, "%d%s\t", j,
317 (val32 & (1 << (j + 6))) ? (j == 5 ? "u" : "") : "t");
320 len += sprintf(buf + len, "\n");
323 robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_TABLE_ACCESS, val16);
325 val16 = robo_read16(ROBO_VLAN_PAGE, ROBO_VLAN_READ);
326 if ((val16 & (1 << 14)) /* valid */) {
327 for (j = 0; j < 6; j++) {
328 if (val16 & (1 << j)) {
329 len += sprintf(buf + len, "%d%s\t", j, (val16 & (1 << (j + 7))) ?
330 (j == 5 ? "u" : "") : "t");
333 len += sprintf(buf + len, "\n");
340 static int handle_vlan_port_write(char *buf, int nr)
347 while (*buf >= '0' && *buf <= '9') {
351 /* untag if needed, CPU port requires special handling */
352 if (*buf == 'u' || (j != 5 && (isspace(*buf) || *buf == 0))) {
355 /* change default vlan tag */
356 robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_PORT0_DEF_TAG + (j << 1), nr);
357 } else if (*buf == '*' || *buf == 't' || isspace(*buf)) {
361 while (isspace(*buf)) buf++;
367 /* write config now */
368 val16 = (nr) /* vlan */ | (1 << 12) /* write */ | (1 << 13) /* enable */;
370 robo_write32(ROBO_VLAN_PAGE, ROBO_VLAN_WRITE_5350,
371 (1 << 20) /* valid */ | (untag << 6) | member);
372 robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_TABLE_ACCESS_5350, val16);
374 robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_WRITE,
375 (1 << 14) /* valid */ | (untag << 7) | member);
376 robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_TABLE_ACCESS, val16);
382 static int __init robo_init()
384 char *device = "ethX";
387 for (device[3] = '0'; (device[3] <= '3') && notfound; device[3]++) {
388 notfound = robo_probe(device);
394 switch_config vlan[] = {
395 {"ports", handle_vlan_port_read, handle_vlan_port_write},
398 switch_driver driver = {
402 driver_handlers: NULL,
407 return switch_register_driver(&driver);
411 static void __exit robo_exit()
413 switch_unregister_driver(DRIVER_NAME);
417 MODULE_AUTHOR("Felix Fietkau <openwrt@nbd.name>");
418 MODULE_LICENSE("GPL");
420 module_init(robo_init);
421 module_exit(robo_exit);