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#define DRIVER_NAME "bcm53xx"
38#define DRIVER_VERSION "0.02"
39#define PFX "roboswitch: "
40
41#define ROBO_PHY_ADDR 0x1E /* robo switch phy address */
42#define ROBO_PHY_ADDR_TG3 0x01 /* Tigon3 PHY address */
43#define ROBO_PHY_ADDR_BCM63XX 0x00 /* BCM63XX PHY address */
44
45/* MII registers */
46#define REG_MII_PAGE 0x10 /* MII Page register */
47#define REG_MII_ADDR 0x11 /* MII Address register */
48#define REG_MII_DATA0 0x18 /* MII Data register 0 */
49
50#define REG_MII_PAGE_ENABLE 1
51#define REG_MII_ADDR_WRITE 1
52#define REG_MII_ADDR_READ 2
53
54/* Robo device ID register (in ROBO_MGMT_PAGE) */
55#define ROBO_DEVICE_ID 0x30
56#define ROBO_DEVICE_ID_5325 0x25 /* Faked */
57#define ROBO_DEVICE_ID_5395 0x95
58#define ROBO_DEVICE_ID_5397 0x97
59#define ROBO_DEVICE_ID_5398 0x98
60#define ROBO_DEVICE_ID_53115 0x3115
61
62/* Private et.o ioctls */
63#define SIOCGETCPHYRD (SIOCDEVPRIVATE + 9)
64#define SIOCSETCPHYWR (SIOCDEVPRIVATE + 10)
65
66/* linux 2.4 does not have 'bool' */
67#if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
68#define bool int
69#endif
70
71/* Only available on brcm-2.4/brcm47xx */
72#ifdef BROADCOM
73extern char *nvram_get(const char *name);
74#define getvar(str) (nvram_get(str)?:"")
75#else
76#define getvar(str) ""
77#endif
78
79/* Data structure for a Roboswitch device. */
80struct robo_switch {
81    char *device; /* The device name string (ethX) */
82    u16 devid; /* ROBO_DEVICE_ID_53xx */
83    bool use_et;
84    bool is_5350;
85    u8 phy_addr; /* PHY address of the device */
86    struct ifreq ifr;
87    struct net_device *dev;
88    unsigned char port[6];
89};
90
91/* Currently we can only have one device in the system. */
92static struct robo_switch robo;
93
94
95static int do_ioctl(int cmd, void *buf)
96{
97    mm_segment_t old_fs = get_fs();
98    int ret;
99
100    if (buf != NULL)
101        robo.ifr.ifr_data = (caddr_t) buf;
102
103    set_fs(KERNEL_DS);
104#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,31)
105    ret = robo.dev->netdev_ops->ndo_do_ioctl(robo.dev, &robo.ifr, cmd);
106#else
107    ret = robo.dev->do_ioctl(robo.dev, &robo.ifr, cmd);
108#endif
109    set_fs(old_fs);
110
111    return ret;
112}
113
114static u16 mdio_read(__u16 phy_id, __u8 reg)
115{
116    if (robo.use_et) {
117        int args[2] = { reg };
118
119        if (phy_id != robo.phy_addr) {
120            printk(KERN_ERR PFX
121                "Access to real 'phy' registers unavaliable.\n"
122                "Upgrade kernel driver.\n");
123
124            return 0xffff;
125        }
126
127
128        if (do_ioctl(SIOCGETCPHYRD, &args) < 0) {
129            printk(KERN_ERR PFX
130                   "[%s:%d] SIOCGETCPHYRD failed!\n", __FILE__, __LINE__);
131            return 0xffff;
132        }
133    
134        return args[1];
135    } else {
136        struct mii_ioctl_data *mii = (struct mii_ioctl_data *) &robo.ifr.ifr_data;
137        mii->phy_id = phy_id;
138        mii->reg_num = reg;
139
140        if (do_ioctl(SIOCGMIIREG, NULL) < 0) {
141            printk(KERN_ERR PFX
142                   "[%s:%d] SIOCGMIIREG failed!\n", __FILE__, __LINE__);
143
144            return 0xffff;
145        }
146
147        return mii->val_out;
148    }
149}
150
151static void mdio_write(__u16 phy_id, __u8 reg, __u16 val)
152{
153    if (robo.use_et) {
154        int args[2] = { reg, val };
155
156        if (phy_id != robo.phy_addr) {
157            printk(KERN_ERR PFX
158                "Access to real 'phy' registers unavaliable.\n"
159                "Upgrade kernel driver.\n");
160
161            return;
162        }
163        
164        if (do_ioctl(SIOCSETCPHYWR, args) < 0) {
165            printk(KERN_ERR PFX
166                   "[%s:%d] SIOCGETCPHYWR failed!\n", __FILE__, __LINE__);
167            return;
168        }
169    } else {
170        struct mii_ioctl_data *mii = (struct mii_ioctl_data *)&robo.ifr.ifr_data;
171
172        mii->phy_id = phy_id;
173        mii->reg_num = reg;
174        mii->val_in = val;
175
176        if (do_ioctl(SIOCSMIIREG, NULL) < 0) {
177            printk(KERN_ERR PFX
178                   "[%s:%d] SIOCSMIIREG failed!\n", __FILE__, __LINE__);
179            return;
180        }
181    }
182}
183
184static int robo_reg(__u8 page, __u8 reg, __u8 op)
185{
186    int i = 3;
187    
188    /* set page number */
189    mdio_write(robo.phy_addr, REG_MII_PAGE,
190        (page << 8) | REG_MII_PAGE_ENABLE);
191    
192    /* set register address */
193    mdio_write(robo.phy_addr, REG_MII_ADDR,
194        (reg << 8) | op);
195
196    /* check if operation completed */
197    while (i--) {
198        if ((mdio_read(robo.phy_addr, REG_MII_ADDR) & 3) == 0)
199            return 0;
200    }
201
202    printk(KERN_ERR PFX "[%s:%d] timeout in robo_reg!\n", __FILE__, __LINE__);
203    
204    return 0;
205}
206
207/*
208static void robo_read(__u8 page, __u8 reg, __u16 *val, int count)
209{
210    int i;
211    
212    robo_reg(page, reg, REG_MII_ADDR_READ);
213    
214    for (i = 0; i < count; i++)
215        val[i] = mdio_read(robo.phy_addr, REG_MII_DATA0 + i);
216}
217*/
218
219static __u16 robo_read16(__u8 page, __u8 reg)
220{
221    robo_reg(page, reg, REG_MII_ADDR_READ);
222    
223    return mdio_read(robo.phy_addr, REG_MII_DATA0);
224}
225
226static __u32 robo_read32(__u8 page, __u8 reg)
227{
228    robo_reg(page, reg, REG_MII_ADDR_READ);
229    
230    return mdio_read(robo.phy_addr, REG_MII_DATA0) +
231        (mdio_read(robo.phy_addr, REG_MII_DATA0 + 1) << 16);
232}
233
234static void robo_write16(__u8 page, __u8 reg, __u16 val16)
235{
236    /* write data */
237    mdio_write(robo.phy_addr, REG_MII_DATA0, val16);
238
239    robo_reg(page, reg, REG_MII_ADDR_WRITE);
240}
241
242static void robo_write32(__u8 page, __u8 reg, __u32 val32)
243{
244    /* write data */
245    mdio_write(robo.phy_addr, REG_MII_DATA0, val32 & 65535);
246    mdio_write(robo.phy_addr, REG_MII_DATA0 + 1, val32 >> 16);
247    
248    robo_reg(page, reg, REG_MII_ADDR_WRITE);
249}
250
251/* checks that attached switch is 5325E/5350 */
252static int robo_vlan5350(void)
253{
254    /* set vlan access id to 15 and read it back */
255    __u16 val16 = 15;
256    robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_TABLE_ACCESS_5350, val16);
257    
258    /* 5365 will refuse this as it does not have this reg */
259    return (robo_read16(ROBO_VLAN_PAGE, ROBO_VLAN_TABLE_ACCESS_5350) == val16);
260}
261
262static int robo_switch_enable(void)
263{
264    unsigned int i, last_port;
265    u16 val;
266
267    val = robo_read16(ROBO_CTRL_PAGE, ROBO_SWITCH_MODE);
268    if (!(val & (1 << 1))) {
269        /* Unmanaged mode */
270        val &= ~(1 << 0);
271        /* With forwarding */
272        val |= (1 << 1);
273        robo_write16(ROBO_CTRL_PAGE, ROBO_SWITCH_MODE, val);
274        val = robo_read16(ROBO_CTRL_PAGE, ROBO_SWITCH_MODE);
275        if (!(val & (1 << 1))) {
276            printk("Failed to enable switch\n");
277            return -EBUSY;
278        }
279
280        last_port = (robo.devid == ROBO_DEVICE_ID_5398) ?
281                ROBO_PORT6_CTRL : ROBO_PORT3_CTRL;
282        for (i = ROBO_PORT0_CTRL; i < last_port + 1; i++)
283            robo_write16(ROBO_CTRL_PAGE, i, 0);
284    }
285
286    /* WAN port LED, except for Netgear WGT634U */
287    if (strcmp(getvar("nvram_type"), "cfe") != 0)
288        robo_write16(ROBO_CTRL_PAGE, 0x16, 0x1F);
289
290    return 0;
291}
292
293static void robo_switch_reset(void)
294{
295    if ((robo.devid == ROBO_DEVICE_ID_5395) ||
296        (robo.devid == ROBO_DEVICE_ID_5397) ||
297        (robo.devid == ROBO_DEVICE_ID_5398)) {
298        /* Trigger a software reset. */
299        robo_write16(ROBO_CTRL_PAGE, 0x79, 0x83);
300        mdelay(500);
301        robo_write16(ROBO_CTRL_PAGE, 0x79, 0);
302    }
303}
304
305static int robo_probe(char *devname)
306{
307    __u32 phyid;
308    unsigned int i;
309    int err = 1;
310
311    printk(KERN_INFO PFX "Probing device %s: ", devname);
312    strcpy(robo.ifr.ifr_name, devname);
313
314#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
315    if ((robo.dev = dev_get_by_name(devname)) == NULL) {
316#else
317    if ((robo.dev = dev_get_by_name(&init_net, devname)) == NULL) {
318#endif
319        printk("No such device\n");
320        return 1;
321    }
322
323    robo.device = devname;
324    for (i = 0; i < 5; i++)
325        robo.port[i] = i;
326    robo.port[5] = 8;
327
328    /* try access using MII ioctls - get phy address */
329    if (do_ioctl(SIOCGMIIPHY, NULL) < 0) {
330        robo.use_et = 1;
331        robo.phy_addr = ROBO_PHY_ADDR;
332    } else {
333        /* got phy address check for robo address */
334        struct mii_ioctl_data *mii = (struct mii_ioctl_data *) &robo.ifr.ifr_data;
335        if ((mii->phy_id != ROBO_PHY_ADDR) &&
336            (mii->phy_id != ROBO_PHY_ADDR_BCM63XX) &&
337            (mii->phy_id != ROBO_PHY_ADDR_TG3)) {
338            printk("Invalid phy address (%d)\n", mii->phy_id);
339            goto done;
340        }
341        robo.use_et = 0;
342        /* The robo has a fixed PHY address that is different from the
343         * Tigon3 and BCM63xx PHY address. */
344        robo.phy_addr = ROBO_PHY_ADDR;
345    }
346
347    phyid = mdio_read(robo.phy_addr, 0x2) |
348        (mdio_read(robo.phy_addr, 0x3) << 16);
349
350    if (phyid == 0xffffffff || phyid == 0x55210022) {
351        printk("No Robo switch in managed mode found, phy_id = 0x%08x\n", phyid);
352        goto done;
353    }
354
355    /* Get the device ID */
356    for (i = 0; i < 10; i++) {
357        robo.devid = robo_read16(ROBO_MGMT_PAGE, ROBO_DEVICE_ID);
358        if (robo.devid)
359            break;
360        udelay(10);
361    }
362    if (!robo.devid)
363        robo.devid = ROBO_DEVICE_ID_5325; /* Fake it */
364    robo.is_5350 = robo_vlan5350();
365
366    robo_switch_reset();
367    err = robo_switch_enable();
368    if (err)
369        goto done;
370    err = 0;
371
372    printk("found a 5%s%x!%s\n", robo.devid & 0xff00 ? "" : "3", robo.devid,
373        robo.is_5350 ? " It's a 5350." : "");
374
375done:
376    if (err) {
377        dev_put(robo.dev);
378        robo.dev = NULL;
379    }
380    return err;
381}
382
383
384static int handle_vlan_port_read(void *driver, char *buf, int nr)
385{
386    __u16 val16;
387    int len = 0;
388    int j;
389
390    val16 = (nr) /* vlan */ | (0 << 12) /* read */ | (1 << 13) /* enable */;
391    
392    if (robo.is_5350) {
393        u32 val32;
394        robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_TABLE_ACCESS_5350, val16);
395        /* actual read */
396        val32 = robo_read32(ROBO_VLAN_PAGE, ROBO_VLAN_READ);
397        if ((val32 & (1 << 20)) /* valid */) {
398            for (j = 0; j < 6; j++) {
399                if (val32 & (1 << j)) {
400                    len += sprintf(buf + len, "%d", j);
401                    if (val32 & (1 << (j + 6))) {
402                        if (j == 5) buf[len++] = 'u';
403                    } else {
404                        buf[len++] = 't';
405                        if (robo_read16(ROBO_VLAN_PAGE, ROBO_VLAN_PORT0_DEF_TAG + (j << 1)) == nr)
406                            buf[len++] = '*';
407                    }
408                    buf[len++] = '\t';
409                }
410            }
411            len += sprintf(buf + len, "\n");
412        }
413    } else {
414        robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_TABLE_ACCESS, val16);
415        /* actual read */
416        val16 = robo_read16(ROBO_VLAN_PAGE, ROBO_VLAN_READ);
417        if ((val16 & (1 << 14)) /* valid */) {
418            for (j = 0; j < 6; j++) {
419                if (val16 & (1 << j)) {
420                    len += sprintf(buf + len, "%d", j);
421                    if (val16 & (1 << (j + 7))) {
422                        if (j == 5) buf[len++] = 'u';
423                    } else {
424                        buf[len++] = 't';
425                        if (robo_read16(ROBO_VLAN_PAGE, ROBO_VLAN_PORT0_DEF_TAG + (j << 1)) == nr)
426                            buf[len++] = '*';
427                    }
428                    buf[len++] = '\t';
429                }
430            }
431            len += sprintf(buf + len, "\n");
432        }
433    }
434
435    buf[len] = '\0';
436
437    return len;
438}
439
440static int handle_vlan_port_write(void *driver, char *buf, int nr)
441{
442    switch_driver *d = (switch_driver *) driver;
443    switch_vlan_config *c = switch_parse_vlan(d, buf);
444    int j;
445    __u16 val16;
446    
447    if (c == NULL)
448        return -EINVAL;
449
450    for (j = 0; j < d->ports; j++) {
451        if ((c->untag | c->pvid) & (1 << j))
452            /* change default vlan tag */
453            robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_PORT0_DEF_TAG + (j << 1), nr);
454    }
455
456    /* write config now */
457
458    if (robo.devid != ROBO_DEVICE_ID_5325) {
459        __u8 regoff = ((robo.devid == ROBO_DEVICE_ID_5395) ||
460            (robo.devid == ROBO_DEVICE_ID_53115)) ? 0x20 : 0;
461
462        robo_write32(ROBO_ARLIO_PAGE, 0x63 + regoff, (c->untag << 9) | c->port);
463        robo_write16(ROBO_ARLIO_PAGE, 0x61 + regoff, nr);
464        robo_write16(ROBO_ARLIO_PAGE, 0x60 + regoff, 1 << 7);
465        return 0;
466    }
467
468    val16 = (nr) /* vlan */ | (1 << 12) /* write */ | (1 << 13) /* enable */;
469    if (robo.is_5350) {
470        robo_write32(ROBO_VLAN_PAGE, ROBO_VLAN_WRITE_5350,
471            (1 << 20) /* valid */ | (c->untag << 6) | c->port);
472        robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_TABLE_ACCESS_5350, val16);
473    } else {
474        robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_WRITE,
475            (1 << 14) /* valid */ | (c->untag << 7) | c->port);
476        robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_TABLE_ACCESS, val16);
477    }
478
479    return 0;
480}
481
482#define set_switch(state) \
483    robo_write16(ROBO_CTRL_PAGE, ROBO_SWITCH_MODE, (robo_read16(ROBO_CTRL_PAGE, ROBO_SWITCH_MODE) & ~2) | (state ? 2 : 0));
484
485static int handle_enable_read(void *driver, char *buf, int nr)
486{
487    return sprintf(buf, "%d\n", (((robo_read16(ROBO_CTRL_PAGE, ROBO_SWITCH_MODE) & 2) == 2) ? 1 : 0));
488}
489
490static int handle_enable_write(void *driver, char *buf, int nr)
491{
492    set_switch(buf[0] == '1');
493
494    return 0;
495}
496
497static int handle_enable_vlan_read(void *driver, char *buf, int nr)
498{
499    return sprintf(buf, "%d\n", (((robo_read16(ROBO_VLAN_PAGE, ROBO_VLAN_CTRL0) & (1 << 7)) == (1 << 7)) ? 1 : 0));
500}
501
502static int handle_enable_vlan_write(void *driver, char *buf, int nr)
503{
504    int disable = ((buf[0] != '1') ? 1 : 0);
505    
506    robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_CTRL0, disable ? 0 :
507        (1 << 7) /* 802.1Q VLAN */ | (3 << 5) /* mac check and hash */);
508    robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_CTRL1, disable ? 0 :
509        (robo.devid == ROBO_DEVICE_ID_5325 ? (1 << 1) :
510        0) | (1 << 2) | (1 << 3)); /* RSV multicast */
511
512    if (robo.devid != ROBO_DEVICE_ID_5325)
513        return 0;
514
515    robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_CTRL4, disable ? 0 :
516        (1 << 6) /* drop invalid VID frames */);
517    robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_CTRL5, disable ? 0 :
518        (1 << 3) /* drop miss V table frames */);
519
520    return 0;
521}
522
523static int handle_reset(void *driver, char *buf, int nr)
524{
525    switch_driver *d = (switch_driver *) driver;
526    switch_vlan_config *c = switch_parse_vlan(d, buf);
527    int j;
528    __u16 val16;
529    
530    if (c == NULL)
531        return -EINVAL;
532
533    /* disable switching */
534    set_switch(0);
535
536    /* reset vlans */
537    for (j = 0; j <= ((robo.is_5350) ? VLAN_ID_MAX5350 : VLAN_ID_MAX); j++) {
538        /* write config now */
539        val16 = (j) /* vlan */ | (1 << 12) /* write */ | (1 << 13) /* enable */;
540        if (robo.is_5350)
541            robo_write32(ROBO_VLAN_PAGE, ROBO_VLAN_WRITE_5350, 0);
542        else
543            robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_WRITE, 0);
544        robo_write16(ROBO_VLAN_PAGE, robo.is_5350 ? ROBO_VLAN_TABLE_ACCESS_5350 :
545                                ROBO_VLAN_TABLE_ACCESS,
546                 val16);
547    }
548
549    /* reset ports to a known good state */
550    for (j = 0; j < d->ports; j++) {
551        robo_write16(ROBO_CTRL_PAGE, robo.port[j], 0x0000);
552        robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_PORT0_DEF_TAG + (j << 1), 0);
553    }
554
555    /* enable switching */
556    set_switch(1);
557
558    /* enable vlans */
559    handle_enable_vlan_write(driver, "1", 0);
560
561    return 0;
562}
563
564static int __init robo_init(void)
565{
566    int notfound = 1;
567    char *device;
568
569    device = strdup("ethX");
570    for (device[3] = '0'; (device[3] <= '3') && notfound; device[3]++) {
571        if (! switch_device_registered (device))
572            notfound = robo_probe(device);
573    }
574    device[3]--;
575    
576    if (notfound) {
577        kfree(device);
578        return -ENODEV;
579    } else {
580        static const switch_config cfg[] = {
581            {
582                .name = "enable",
583                .read = handle_enable_read,
584                .write = handle_enable_write
585            }, {
586                .name = "enable_vlan",
587                .read = handle_enable_vlan_read,
588                .write = handle_enable_vlan_write
589            }, {
590                .name = "reset",
591                .read = NULL,
592                .write = handle_reset
593            }, { NULL, },
594        };
595        static const switch_config vlan[] = {
596            {
597                .name = "ports",
598                .read = handle_vlan_port_read,
599                .write = handle_vlan_port_write
600            }, { NULL, },
601        };
602        switch_driver driver = {
603            .name = DRIVER_NAME,
604            .version = DRIVER_VERSION,
605            .interface = device,
606            .cpuport = 5,
607            .ports = 6,
608            .vlans = 16,
609            .driver_handlers = cfg,
610            .port_handlers = NULL,
611            .vlan_handlers = vlan,
612        };
613        if (robo.devid != ROBO_DEVICE_ID_5325) {
614            driver.ports = 9;
615            driver.cpuport = 8;
616        }
617
618        return switch_register_driver(&driver);
619    }
620}
621
622static void __exit robo_exit(void)
623{
624    switch_unregister_driver(DRIVER_NAME);
625    if (robo.dev)
626        dev_put(robo.dev);
627    kfree(robo.device);
628}
629
630
631MODULE_AUTHOR("Felix Fietkau <openwrt@nbd.name>");
632MODULE_LICENSE("GPL");
633
634module_init(robo_init);
635module_exit(robo_exit);
636

Archive Download this file



interactive