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