7e4c4de2967e14e97face2ca1cec1939e21ad6ad
[openwrt.git] / target / linux / 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  * Based on 'robocfg' by Oleg I. Vdovikin
6  *
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.
11  *
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.
16  *
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  
20  * 02110-1301, USA.
21  */
22
23 #include <linux/config.h>
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 <asm/uaccess.h>
32
33 #include "switch-core.h"
34 #include "etc53xx.h"
35
36 #define DRIVER_NAME             "bcm53xx"
37 #define DRIVER_VERSION  "0.01"
38
39 #define ROBO_PHY_ADDR   0x1E    /* robo switch phy address */
40
41 /* MII registers */
42 #define REG_MII_PAGE    0x10    /* MII Page register */
43 #define REG_MII_ADDR    0x11    /* MII Address register */
44 #define REG_MII_DATA0   0x18    /* MII Data register 0 */
45
46 #define REG_MII_PAGE_ENABLE     1
47 #define REG_MII_ADDR_WRITE      1
48 #define REG_MII_ADDR_READ       2
49
50 /* Private et.o ioctls */
51 #define SIOCGETCPHYRD           (SIOCDEVPRIVATE + 9)
52 #define SIOCSETCPHYWR           (SIOCDEVPRIVATE + 10)
53
54 static int use_et = 0;
55 static int is_5350 = 0;
56 static struct ifreq ifr;
57 static struct net_device *dev;
58 static unsigned char port[6] = { 0, 1, 2, 3, 4, 8 };
59
60 static int do_ioctl(int cmd, void *buf)
61 {
62         mm_segment_t old_fs = get_fs();
63         int ret;
64
65         if (buf != NULL)
66                 ifr.ifr_data = (caddr_t) buf;
67
68         set_fs(KERNEL_DS);
69         ret = dev->do_ioctl(dev, &ifr, cmd);
70         set_fs(old_fs);
71
72         return ret;
73 }
74
75 static u16 mdio_read(__u16 phy_id, __u8 reg)
76 {
77         if (use_et) {
78                 int args[2] = { reg };
79                 
80                 if (phy_id != ROBO_PHY_ADDR) {
81                         printk(
82                                 "Access to real 'phy' registers unavaliable.\n"
83                                 "Upgrade kernel driver.\n");
84
85                         return 0xffff;
86                 }
87
88
89                 if (do_ioctl(SIOCGETCPHYRD, &args) < 0) {
90                         printk("[%s:%d] SIOCGETCPHYRD failed!\n", __FILE__, __LINE__);
91                         return 0xffff;
92                 }
93         
94                 return args[1];
95         } else {
96                 struct mii_ioctl_data *mii = (struct mii_ioctl_data *) &ifr.ifr_data;
97                 mii->phy_id = phy_id;
98                 mii->reg_num = reg;
99
100                 if (do_ioctl(SIOCGMIIREG, NULL) < 0) {
101                         printk("[%s:%d] SIOCGMIIREG failed!\n", __FILE__, __LINE__);
102
103                         return 0xffff;
104                 }
105
106                 return mii->val_out;
107         }
108 }
109
110 static void mdio_write(__u16 phy_id, __u8 reg, __u16 val)
111 {
112         if (use_et) {
113                 int args[2] = { reg, val };
114
115                 if (phy_id != ROBO_PHY_ADDR) {
116                         printk(
117                                 "Access to real 'phy' registers unavaliable.\n"
118                                 "Upgrade kernel driver.\n");
119
120                         return;
121                 }
122                 
123                 if (do_ioctl(SIOCSETCPHYWR, args) < 0) {
124                         printk("[%s:%d] SIOCGETCPHYWR failed!\n", __FILE__, __LINE__);
125                         return;
126                 }
127         } else {
128                 struct mii_ioctl_data *mii = (struct mii_ioctl_data *)&ifr.ifr_data;
129
130                 mii->phy_id = phy_id;
131                 mii->reg_num = reg;
132                 mii->val_in = val;
133
134                 if (do_ioctl(SIOCSMIIREG, NULL) < 0) {
135                         printk("[%s:%d] SIOCSMIIREG failed!\n", __FILE__, __LINE__);
136                         return;
137                 }
138         }
139 }
140
141 static int robo_reg(__u8 page, __u8 reg, __u8 op)
142 {
143         int i = 3;
144         
145         /* set page number */
146         mdio_write(ROBO_PHY_ADDR, REG_MII_PAGE, 
147                 (page << 8) | REG_MII_PAGE_ENABLE);
148         
149         /* set register address */
150         mdio_write(ROBO_PHY_ADDR, REG_MII_ADDR, 
151                 (reg << 8) | op);
152
153         /* check if operation completed */
154         while (i--) {
155                 if ((mdio_read(ROBO_PHY_ADDR, REG_MII_ADDR) & 3) == 0)
156                         return 0;
157         }
158
159         printk("[%s:%d] timeout in robo_reg!\n", __FILE__, __LINE__);
160         
161         return 0;
162 }
163
164 static void robo_read(__u8 page, __u8 reg, __u16 *val, int count)
165 {
166         int i;
167         
168         robo_reg(page, reg, REG_MII_ADDR_READ);
169         
170         for (i = 0; i < count; i++)
171                 val[i] = mdio_read(ROBO_PHY_ADDR, REG_MII_DATA0 + i);
172 }
173
174 static __u16 robo_read16(__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 }
180
181 static __u32 robo_read32(__u8 page, __u8 reg)
182 {
183         robo_reg(page, reg, REG_MII_ADDR_READ);
184         
185         return mdio_read(ROBO_PHY_ADDR, REG_MII_DATA0) +
186                 (mdio_read(ROBO_PHY_ADDR, REG_MII_DATA0 + 1) << 16);
187 }
188
189 static void robo_write16(__u8 page, __u8 reg, __u16 val16)
190 {
191         /* write data */
192         mdio_write(ROBO_PHY_ADDR, REG_MII_DATA0, val16);
193
194         robo_reg(page, reg, REG_MII_ADDR_WRITE);
195 }
196
197 static void robo_write32(__u8 page, __u8 reg, __u32 val32)
198 {
199         /* write data */
200         mdio_write(ROBO_PHY_ADDR, REG_MII_DATA0, val32 & 65535);
201         mdio_write(ROBO_PHY_ADDR, REG_MII_DATA0 + 1, val32 >> 16);
202         
203         robo_reg(page, reg, REG_MII_ADDR_WRITE);
204 }
205
206 /* checks that attached switch is 5325E/5350 */
207 static int robo_vlan5350()
208 {
209         /* set vlan access id to 15 and read it back */
210         __u16 val16 = 15;
211         robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_TABLE_ACCESS_5350, val16);
212         
213         /* 5365 will refuse this as it does not have this reg */
214         return (robo_read16(ROBO_VLAN_PAGE, ROBO_VLAN_TABLE_ACCESS_5350) == val16);
215 }
216
217
218
219 static int robo_probe(char *devname)
220 {
221         struct ethtool_drvinfo info;
222         int i;
223         __u32 phyid;
224
225         printk("Probing device %s: ", devname);
226         strcpy(ifr.ifr_name, devname);
227
228         if ((dev = dev_get_by_name(devname)) == NULL) {
229                 printk("No such device\n");
230                 return 1;
231         }
232
233         info.cmd = ETHTOOL_GDRVINFO;
234         if (do_ioctl(SIOCETHTOOL, (void *) &info) < 0) {
235                 printk("SIOCETHTOOL: not supported\n");
236                 return 1;
237         }
238         
239         /* try access using MII ioctls - get phy address */
240         if (do_ioctl(SIOCGMIIPHY, NULL) < 0) {
241                 use_et = 1;
242         } else {
243                 /* got phy address check for robo address */
244                 struct mii_ioctl_data *mii = (struct mii_ioctl_data *) &ifr.ifr_data;
245                 if (mii->phy_id != ROBO_PHY_ADDR) {
246                         printk("Invalid phy address (%d)\n", mii->phy_id);
247                         return 1;
248                 }
249         }
250
251         phyid = mdio_read(ROBO_PHY_ADDR, 0x2) | 
252                 (mdio_read(ROBO_PHY_ADDR, 0x3) << 16);
253
254         if (phyid == 0xffffffff || phyid == 0x55210022) {
255                 printk("No Robo switch in managed mode found\n");
256                 return 1;
257         }
258         
259         is_5350 = robo_vlan5350();
260         
261         printk("found!\n");
262         return 0;
263 }
264
265
266 static int handle_vlan_port_read(void *driver, char *buf, int nr)
267 {
268         __u16 val16;
269         int len = 0;
270         int j;
271
272         val16 = (nr) /* vlan */ | (0 << 12) /* read */ | (1 << 13) /* enable */;
273         
274         if (is_5350) {
275                 u32 val32;
276                 robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_TABLE_ACCESS_5350, val16);
277                 /* actual read */
278                 val32 = robo_read32(ROBO_VLAN_PAGE, ROBO_VLAN_READ);
279                 if ((val32 & (1 << 20)) /* valid */) {
280                         for (j = 0; j < 6; j++) {
281                                 if (val32 & (1 << j)) {
282                                         len += sprintf(buf + len, "%d", j);
283                                         if (val32 & (1 << (j + 6))) {
284                                                 if (j == 5) buf[len++] = 'u';
285                                         } else {
286                                                 buf[len++] = 't';
287                                                 if (robo_read16(ROBO_VLAN_PAGE, ROBO_VLAN_PORT0_DEF_TAG + (j << 1)) == nr)
288                                                         buf[len++] = '*';
289                                         }
290                                         buf[len++] = '\t';
291                                 }
292                         }
293                         len += sprintf(buf + len, "\n");
294                 }
295         } else { 
296                 robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_TABLE_ACCESS, val16);
297                 /* actual read */
298                 val16 = robo_read16(ROBO_VLAN_PAGE, ROBO_VLAN_READ);
299                 if ((val16 & (1 << 14)) /* valid */) {
300                         for (j = 0; j < 6; j++) {
301                                 if (val16 & (1 << j)) {
302                                         len += sprintf(buf + len, "%d", j);
303                                         if (val16 & (1 << (j + 7))) {
304                                                 if (j == 5) buf[len++] = 'u';
305                                         } else {
306                                                 buf[len++] = 't';
307                                                 if (robo_read16(ROBO_VLAN_PAGE, ROBO_VLAN_PORT0_DEF_TAG + (j << 1)) == nr)
308                                                         buf[len++] = '*';
309                                         }
310                                         buf[len++] = '\t';
311                                 }
312                         }
313                         len += sprintf(buf + len, "\n");
314                 }
315         }
316
317         return len;
318 }
319
320 static int handle_vlan_port_write(void *driver, char *buf, int nr)
321 {
322         switch_driver *d = (switch_driver *) driver;
323         switch_vlan_config *c = switch_parse_vlan(d, buf);
324         int j;
325         __u16 val16;
326         
327         if (c == NULL)
328                 return -EINVAL;
329
330         for (j = 0; j < d->ports; j++) {
331                 if ((c->untag | c->pvid) & (1 << j))
332                         /* change default vlan tag */
333                         robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_PORT0_DEF_TAG + (j << 1), nr);
334         }
335
336         /* write config now */
337         val16 = (nr) /* vlan */ | (1 << 12) /* write */ | (1 << 13) /* enable */;
338         if (is_5350) {
339                 robo_write32(ROBO_VLAN_PAGE, ROBO_VLAN_WRITE_5350,
340                         (1 << 20) /* valid */ | (c->untag << 6) | c->port);
341                 robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_TABLE_ACCESS_5350, val16);
342         } else {
343                 robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_WRITE,
344                         (1 << 14)  /* valid */ | (c->untag << 7) | c->port);
345                 robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_TABLE_ACCESS, val16);
346         }
347
348         return 0;
349 }
350
351 #define set_switch(state) \
352         robo_write16(ROBO_CTRL_PAGE, ROBO_SWITCH_MODE, (robo_read16(ROBO_CTRL_PAGE, ROBO_SWITCH_MODE) & ~2) | (state ? 2 : 0));
353
354 static int handle_enable_read(void *driver, char *buf, int nr)
355 {
356         return sprintf(buf, "%d\n", (((robo_read16(ROBO_CTRL_PAGE, ROBO_SWITCH_MODE) & 2) == 2) ? 1 : 0));
357 }
358
359 static int handle_enable_write(void *driver, char *buf, int nr)
360 {
361         set_switch(buf[0] == '1');
362
363         return 0;
364 }
365
366 static int handle_enable_vlan_read(void *driver, char *buf, int nr)
367 {
368         return sprintf(buf, "%d\n", (((robo_read16(ROBO_VLAN_PAGE, ROBO_VLAN_CTRL0) & (1 << 7)) == (1 << 7)) ? 1 : 0));
369 }
370
371 static int handle_enable_vlan_write(void *driver, char *buf, int nr)
372 {
373         int disable = ((buf[0] != '1') ? 1 : 0);
374         
375         robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_CTRL0, disable ? 0 :
376                 (1 << 7) /* 802.1Q VLAN */ | (3 << 5) /* mac check and hash */);
377         robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_CTRL1, disable ? 0 :
378                 (1 << 1) | (1 << 2) | (1 << 3) /* RSV multicast */);
379         robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_CTRL4, disable ? 0 :
380                 (1 << 6) /* drop invalid VID frames */);
381         robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_CTRL5, disable ? 0 :
382                 (1 << 3) /* drop miss V table frames */);
383
384         return 0;
385 }
386
387 static int handle_reset(void *driver, char *buf, int nr)
388 {
389         switch_driver *d = (switch_driver *) driver;
390         switch_vlan_config *c = switch_parse_vlan(d, buf);
391         int j;
392         __u16 val16;
393         
394         if (c == NULL)
395                 return -EINVAL;
396
397         /* disable switching */
398         set_switch(0);
399
400         /* reset vlans */
401         for (j = 0; j <= (is_5350 ? VLAN_ID_MAX5350 : VLAN_ID_MAX); j++) {
402                 /* write config now */
403                 val16 = (j) /* vlan */ | (1 << 12) /* write */ | (1 << 13) /* enable */;
404                 if (is_5350)
405                         robo_write32(ROBO_VLAN_PAGE, ROBO_VLAN_WRITE_5350, 0);
406                 else
407                         robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_WRITE, 0);
408                 robo_write16(ROBO_VLAN_PAGE, (is_5350 ? ROBO_VLAN_TABLE_ACCESS_5350 : ROBO_VLAN_TABLE_ACCESS), val16);
409         }
410
411         /* reset ports to a known good state */
412         for (j = 0; j < d->ports; j++) {
413                 robo_write16(ROBO_CTRL_PAGE, port[j], 0x0000);
414                 robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_PORT0_DEF_TAG + (j << 1), 0);
415         }
416
417         /* enable switching */
418         set_switch(1);
419
420         /* enable vlans */
421         handle_enable_vlan_write(driver, "1", 0);
422
423         return 0;
424 }
425
426 static int __init robo_init()
427 {
428         char *device = "ethX";
429         int notfound = 1;
430
431         for (device[3] = '0'; (device[3] <= '3') && notfound; device[3]++) {
432                 notfound = robo_probe(device);
433         }
434         device[3]--;
435         
436         if (notfound)
437                 return -ENODEV;
438         else {
439                 switch_config cfg[] = {
440                         {"enable", handle_enable_read, handle_enable_write},
441                         {"enable_vlan", handle_enable_vlan_read, handle_enable_vlan_write},
442                         {"reset", NULL, handle_reset},
443                         {NULL, NULL, NULL}
444                 };
445                 switch_config vlan[] = {
446                         {"ports", handle_vlan_port_read, handle_vlan_port_write},
447                         {NULL, NULL, NULL}
448                 };
449                 switch_driver driver = {
450                         name: DRIVER_NAME,
451                         version: DRIVER_VERSION,
452                         interface: device,
453                         cpuport: 5,
454                         ports: 6,
455                         vlans: 16,
456                         driver_handlers: cfg,
457                         port_handlers: NULL,
458                         vlan_handlers: vlan,
459                 };
460
461                 return switch_register_driver(&driver);
462         }
463 }
464
465 static void __exit robo_exit()
466 {
467         switch_unregister_driver(DRIVER_NAME);
468 }
469
470
471 MODULE_AUTHOR("Felix Fietkau <openwrt@nbd.name>");
472 MODULE_LICENSE("GPL");
473
474 module_init(robo_init);
475 module_exit(robo_exit);