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