Root/package/switch/src/switch-robo.c

1/*
2 * Broadcom BCM5325E/536x switch configuration module
3 *
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
7 *
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.
12 *
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.
17 *
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
21 * 02110-1301, USA.
22 */
23
24#include <linux/module.h>
25#include <linux/init.h>
26#include <linux/if.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>
33
34#include "switch-core.h"
35#include "etc53xx.h"
36
37#ifdef CONFIG_BCM47XX
38#include <nvram.h>
39#endif
40
41#define DRIVER_NAME "bcm53xx"
42#define DRIVER_VERSION "0.02"
43#define PFX "roboswitch: "
44
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 */
48
49/* MII registers */
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 */
53
54#define REG_MII_PAGE_ENABLE 1
55#define REG_MII_ADDR_WRITE 1
56#define REG_MII_ADDR_READ 2
57
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
65
66/* Private et.o ioctls */
67#define SIOCGETCPHYRD (SIOCDEVPRIVATE + 9)
68#define SIOCSETCPHYWR (SIOCDEVPRIVATE + 10)
69
70/* Data structure for a Roboswitch device. */
71struct robo_switch {
72    char *device; /* The device name string (ethX) */
73    u16 devid; /* ROBO_DEVICE_ID_53xx */
74    bool is_5350;
75    struct ifreq ifr;
76    struct net_device *dev;
77    unsigned char port[6];
78};
79
80/* Currently we can only have one device in the system. */
81static struct robo_switch robo;
82
83
84static int do_ioctl(int cmd)
85{
86    mm_segment_t old_fs = get_fs();
87    int ret;
88
89    set_fs(KERNEL_DS);
90    ret = robo.dev->netdev_ops->ndo_do_ioctl(robo.dev, &robo.ifr, cmd);
91    set_fs(old_fs);
92
93    return ret;
94}
95
96static u16 mdio_read(__u16 phy_id, __u8 reg)
97{
98    struct mii_ioctl_data *mii = if_mii(&robo.ifr);
99    int err;
100
101    mii->phy_id = phy_id;
102    mii->reg_num = reg;
103
104    err = do_ioctl(SIOCGMIIREG);
105    if (err < 0) {
106        printk(KERN_ERR PFX
107               "[%s:%d] SIOCGMIIREG failed! err: %i\n", __FILE__, __LINE__, err);
108
109        return 0xffff;
110    }
111
112    return mii->val_out;
113}
114
115static void mdio_write(__u16 phy_id, __u8 reg, __u16 val)
116{
117    struct mii_ioctl_data *mii = if_mii(&robo.ifr);
118    int err;
119
120    mii->phy_id = phy_id;
121    mii->reg_num = reg;
122    mii->val_in = val;
123
124    err = do_ioctl(SIOCSMIIREG);
125    if (err < 0) {
126        printk(KERN_ERR PFX
127               "[%s:%d] SIOCSMIIREG failed! err: %i\n", __FILE__, __LINE__, err);
128        return;
129    }
130}
131
132static int robo_reg(__u8 page, __u8 reg, __u8 op)
133{
134    int i = 3;
135
136    /* set page number */
137    mdio_write(ROBO_PHY_ADDR, REG_MII_PAGE,
138        (page << 8) | REG_MII_PAGE_ENABLE);
139
140    /* set register address */
141    mdio_write(ROBO_PHY_ADDR, REG_MII_ADDR,
142        (reg << 8) | op);
143
144    /* check if operation completed */
145    while (i--) {
146        if ((mdio_read(ROBO_PHY_ADDR, REG_MII_ADDR) & 3) == 0)
147            return 0;
148    }
149
150    printk(KERN_ERR PFX "[%s:%d] timeout in robo_reg!\n", __FILE__, __LINE__);
151
152    return 0;
153}
154
155/*
156static void robo_read(__u8 page, __u8 reg, __u16 *val, int count)
157{
158    int i;
159
160    robo_reg(page, reg, REG_MII_ADDR_READ);
161
162    for (i = 0; i < count; i++)
163        val[i] = mdio_read(ROBO_PHY_ADDR, REG_MII_DATA0 + i);
164}
165*/
166
167static __u16 robo_read16(__u8 page, __u8 reg)
168{
169    robo_reg(page, reg, REG_MII_ADDR_READ);
170
171    return mdio_read(ROBO_PHY_ADDR, REG_MII_DATA0);
172}
173
174static __u32 robo_read32(__u8 page, __u8 reg)
175{
176    robo_reg(page, reg, REG_MII_ADDR_READ);
177
178    return mdio_read(ROBO_PHY_ADDR, REG_MII_DATA0) +
179        (mdio_read(ROBO_PHY_ADDR, REG_MII_DATA0 + 1) << 16);
180}
181
182static void robo_write16(__u8 page, __u8 reg, __u16 val16)
183{
184    /* write data */
185    mdio_write(ROBO_PHY_ADDR, REG_MII_DATA0, val16);
186
187    robo_reg(page, reg, REG_MII_ADDR_WRITE);
188}
189
190static void robo_write32(__u8 page, __u8 reg, __u32 val32)
191{
192    /* write data */
193    mdio_write(ROBO_PHY_ADDR, REG_MII_DATA0, val32 & 65535);
194    mdio_write(ROBO_PHY_ADDR, REG_MII_DATA0 + 1, val32 >> 16);
195
196    robo_reg(page, reg, REG_MII_ADDR_WRITE);
197}
198
199/* checks that attached switch is 5325E/5350 */
200static int robo_vlan5350(void)
201{
202    /* set vlan access id to 15 and read it back */
203    __u16 val16 = 15;
204    robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_TABLE_ACCESS_5350, val16);
205
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);
208}
209
210static int robo_switch_enable(void)
211{
212    unsigned int i, last_port;
213    u16 val;
214#ifdef CONFIG_BCM47XX
215    char buf[20];
216#endif
217
218    val = robo_read16(ROBO_CTRL_PAGE, ROBO_SWITCH_MODE);
219    if (!(val & (1 << 1))) {
220        /* Unmanaged mode */
221        val &= ~(1 << 0);
222        /* With forwarding */
223        val |= (1 << 1);
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");
228            return -EBUSY;
229        }
230
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);
235    }
236
237#ifdef CONFIG_BCM47XX
238    /* WAN port LED, except for Netgear WGT634U */
239    if (nvram_getenv("nvram_type", buf, sizeof(buf)) >= 0) {
240        if (strcmp(buf, "cfe") != 0)
241            robo_write16(ROBO_CTRL_PAGE, 0x16, 0x1F);
242    }
243#endif
244    return 0;
245}
246
247static void robo_switch_reset(void)
248{
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);
254        mdelay(500);
255        robo_write16(ROBO_CTRL_PAGE, 0x79, 0);
256    }
257}
258
259static int robo_probe(char *devname)
260{
261    __u32 phyid;
262    unsigned int i;
263    int err = 1;
264
265    printk(KERN_INFO PFX "Probing device %s: ", devname);
266    strcpy(robo.ifr.ifr_name, devname);
267
268    if ((robo.dev = dev_get_by_name(&init_net, devname)) == NULL) {
269        printk("No such device\n");
270        return 1;
271    }
272    if (!robo.dev->netdev_ops || !robo.dev->netdev_ops->ndo_do_ioctl) {
273        printk("ndo_do_ioctl not implemented in ethernet driver\n");
274        return 1;
275    }
276
277    robo.device = devname;
278    for (i = 0; i < 5; i++)
279        robo.port[i] = i;
280    robo.port[5] = 8;
281
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");
285        goto done;
286    }
287
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);
294        goto done;
295    }
296
297    phyid = mdio_read(ROBO_PHY_ADDR, 0x2) |
298        (mdio_read(ROBO_PHY_ADDR, 0x3) << 16);
299
300    if (phyid == 0xffffffff || phyid == 0x55210022) {
301        printk("No Robo switch in managed mode found, phy_id = 0x%08x\n", phyid);
302        goto done;
303    }
304
305    /* Get the device ID */
306    for (i = 0; i < 10; i++) {
307        robo.devid = robo_read16(ROBO_MGMT_PAGE, ROBO_DEVICE_ID);
308        if (robo.devid)
309            break;
310        udelay(10);
311    }
312    if (!robo.devid)
313        robo.devid = ROBO_DEVICE_ID_5325; /* Fake it */
314    robo.is_5350 = robo_vlan5350();
315
316    robo_switch_reset();
317    err = robo_switch_enable();
318    if (err)
319        goto done;
320    err = 0;
321
322    printk("found a 5%s%x!%s\n", robo.devid & 0xff00 ? "" : "3", robo.devid,
323        robo.is_5350 ? " It's a 5350." : "");
324
325done:
326    if (err) {
327        dev_put(robo.dev);
328        robo.dev = NULL;
329    }
330    return err;
331}
332
333
334static int handle_vlan_port_read(void *driver, char *buf, int nr)
335{
336    __u16 val16;
337    int len = 0;
338    int j;
339
340    val16 = (nr) /* vlan */ | (0 << 12) /* read */ | (1 << 13) /* enable */;
341
342    if (robo.is_5350) {
343        u32 val32;
344        robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_TABLE_ACCESS_5350, val16);
345        /* actual read */
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';
353                    } else {
354                        buf[len++] = 't';
355                        if (robo_read16(ROBO_VLAN_PAGE, ROBO_VLAN_PORT0_DEF_TAG + (j << 1)) == nr)
356                            buf[len++] = '*';
357                    }
358                    buf[len++] = '\t';
359                }
360            }
361            len += sprintf(buf + len, "\n");
362        }
363    } else {
364        robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_TABLE_ACCESS, val16);
365        /* actual read */
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';
373                    } else {
374                        buf[len++] = 't';
375                        if (robo_read16(ROBO_VLAN_PAGE, ROBO_VLAN_PORT0_DEF_TAG + (j << 1)) == nr)
376                            buf[len++] = '*';
377                    }
378                    buf[len++] = '\t';
379                }
380            }
381            len += sprintf(buf + len, "\n");
382        }
383    }
384
385    buf[len] = '\0';
386
387    return len;
388}
389
390static int handle_vlan_port_write(void *driver, char *buf, int nr)
391{
392    switch_driver *d = (switch_driver *) driver;
393    switch_vlan_config *c = switch_parse_vlan(d, buf);
394    int j;
395    __u16 val16;
396
397    if (c == NULL)
398        return -EINVAL;
399
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);
404    }
405
406    /* write config now */
407
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;
411
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);
415        kfree(c);
416        return 0;
417    }
418
419    val16 = (nr) /* vlan */ | (1 << 12) /* write */ | (1 << 13) /* enable */;
420    if (robo.is_5350) {
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);
424    } else {
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);
428    }
429
430    kfree(c);
431    return 0;
432}
433
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));
436
437static int handle_enable_read(void *driver, char *buf, int nr)
438{
439    return sprintf(buf, "%d\n", (((robo_read16(ROBO_CTRL_PAGE, ROBO_SWITCH_MODE) & 2) == 2) ? 1 : 0));
440}
441
442static int handle_enable_write(void *driver, char *buf, int nr)
443{
444    set_switch(buf[0] == '1');
445
446    return 0;
447}
448
449static int handle_enable_vlan_read(void *driver, char *buf, int nr)
450{
451    return sprintf(buf, "%d\n", (((robo_read16(ROBO_VLAN_PAGE, ROBO_VLAN_CTRL0) & (1 << 7)) == (1 << 7)) ? 1 : 0));
452}
453
454static int handle_enable_vlan_write(void *driver, char *buf, int nr)
455{
456    int disable = ((buf[0] != '1') ? 1 : 0);
457
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 */
463
464    if (robo.devid != ROBO_DEVICE_ID_5325)
465        return 0;
466
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 */);
471
472    return 0;
473}
474
475static int handle_reset(void *driver, char *buf, int nr)
476{
477    switch_driver *d = (switch_driver *) driver;
478    int j;
479    __u16 val16;
480
481    /* disable switching */
482    set_switch(0);
483
484    /* reset vlans */
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 */;
488        if (robo.is_5350)
489            robo_write32(ROBO_VLAN_PAGE, ROBO_VLAN_WRITE_5350, 0);
490        else
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,
494                 val16);
495    }
496
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);
501    }
502
503    /* enable switching */
504    set_switch(1);
505
506    /* enable vlans */
507    handle_enable_vlan_write(driver, "1", 0);
508
509    return 0;
510}
511
512static int __init robo_init(void)
513{
514    int notfound = 1;
515    char *device;
516
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);
521    }
522    device[3]--;
523
524    if (notfound) {
525        kfree(device);
526        return -ENODEV;
527    } else {
528        static const switch_config cfg[] = {
529            {
530                .name = "enable",
531                .read = handle_enable_read,
532                .write = handle_enable_write
533            }, {
534                .name = "enable_vlan",
535                .read = handle_enable_vlan_read,
536                .write = handle_enable_vlan_write
537            }, {
538                .name = "reset",
539                .read = NULL,
540                .write = handle_reset
541            }, { NULL, },
542        };
543        static const switch_config vlan[] = {
544            {
545                .name = "ports",
546                .read = handle_vlan_port_read,
547                .write = handle_vlan_port_write
548            }, { NULL, },
549        };
550        switch_driver driver = {
551            .name = DRIVER_NAME,
552            .version = DRIVER_VERSION,
553            .interface = device,
554            .cpuport = 5,
555            .ports = 6,
556            .vlans = 16,
557            .driver_handlers = cfg,
558            .port_handlers = NULL,
559            .vlan_handlers = vlan,
560        };
561        if (robo.devid != ROBO_DEVICE_ID_5325) {
562            driver.ports = 9;
563            driver.cpuport = 8;
564        }
565
566        return switch_register_driver(&driver);
567    }
568}
569
570static void __exit robo_exit(void)
571{
572    switch_unregister_driver(DRIVER_NAME);
573    if (robo.dev)
574        dev_put(robo.dev);
575    kfree(robo.device);
576}
577
578
579MODULE_AUTHOR("Felix Fietkau <openwrt@nbd.name>");
580MODULE_LICENSE("GPL");
581
582module_init(robo_init);
583module_exit(robo_exit);
584

Archive Download this file



interactive