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