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