Added mrd6 package : IPv6 multicast routing daemon
[openwrt.git] / openwrt / 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
38 #define ROBO_PHY_ADDR 0x1E /* robo switch phy address */
39
40 /* MII registers */
41 #define REG_MII_PAGE 0x10 /* MII Page register */
42 #define REG_MII_ADDR 0x11 /* MII Address register */
43 #define REG_MII_DATA0 0x18 /* MII Data register 0 */
44
45 #define REG_MII_PAGE_ENABLE 1
46 #define REG_MII_ADDR_WRITE 1
47 #define REG_MII_ADDR_READ 2
48
49 /* Private et.o ioctls */
50 #define SIOCGETCPHYRD (SIOCDEVPRIVATE + 9)
51 #define SIOCSETCPHYWR (SIOCDEVPRIVATE + 10)
52
53 static int use_et = 0;
54 static int is_5350 = 0;
55 static int max_vlans, max_ports;
56 static struct ifreq ifr;
57 static struct net_device *dev;
58
59 static int isspace(char c) {
60 switch(c) {
61 case ' ':
62 case 0x09:
63 case 0x0a:
64 case 0x0d:
65 return 1;
66 default:
67 return 0;
68 }
69 }
70
71 static int do_ioctl(int cmd, void *buf)
72 {
73 mm_segment_t old_fs = get_fs();
74 int ret;
75
76 if (buf != NULL)
77 ifr.ifr_data = (caddr_t) buf;
78
79 set_fs(KERNEL_DS);
80 ret = dev->do_ioctl(dev, &ifr, cmd);
81 set_fs(old_fs);
82
83 return ret;
84 }
85
86 static u16 mdio_read(__u16 phy_id, __u8 reg)
87 {
88 if (use_et) {
89 int args[2] = { reg };
90
91 if (phy_id != ROBO_PHY_ADDR) {
92 printk(
93 "Access to real 'phy' registers unavaliable.\n"
94 "Upgrade kernel driver.\n");
95
96 return 0xffff;
97 }
98
99
100 if (do_ioctl(SIOCGETCPHYRD, &args) < 0) {
101 printk("[%s:%d] SIOCGETCPHYRD failed!\n", __FILE__, __LINE__);
102 return 0xffff;
103 }
104
105 return args[1];
106 } else {
107 struct mii_ioctl_data *mii = (struct mii_ioctl_data *) &ifr.ifr_data;
108 mii->phy_id = phy_id;
109 mii->reg_num = reg;
110
111 if (do_ioctl(SIOCGMIIREG, NULL) < 0) {
112 printk("[%s:%d] SIOCGMIIREG failed!\n", __FILE__, __LINE__);
113
114 return 0xffff;
115 }
116
117 return mii->val_out;
118 }
119 }
120
121 static void mdio_write(__u16 phy_id, __u8 reg, __u16 val)
122 {
123 if (use_et) {
124 int args[2] = { reg, val };
125
126 if (phy_id != ROBO_PHY_ADDR) {
127 printk(
128 "Access to real 'phy' registers unavaliable.\n"
129 "Upgrade kernel driver.\n");
130
131 return;
132 }
133
134 if (do_ioctl(SIOCSETCPHYWR, args) < 0) {
135 printk("[%s:%d] SIOCGETCPHYWR failed!\n", __FILE__, __LINE__);
136 return;
137 }
138 } else {
139 struct mii_ioctl_data *mii = (struct mii_ioctl_data *)&ifr.ifr_data;
140
141 mii->phy_id = phy_id;
142 mii->reg_num = reg;
143 mii->val_in = val;
144
145 if (do_ioctl(SIOCSMIIREG, NULL) < 0) {
146 printk("[%s:%d] SIOCSMIIREG failed!\n", __FILE__, __LINE__);
147 return;
148 }
149 }
150 }
151
152 static int robo_reg(__u8 page, __u8 reg, __u8 op)
153 {
154 int i = 3;
155
156 /* set page number */
157 mdio_write(ROBO_PHY_ADDR, REG_MII_PAGE,
158 (page << 8) | REG_MII_PAGE_ENABLE);
159
160 /* set register address */
161 mdio_write(ROBO_PHY_ADDR, REG_MII_ADDR,
162 (reg << 8) | op);
163
164 /* check if operation completed */
165 while (i--) {
166 if ((mdio_read(ROBO_PHY_ADDR, REG_MII_ADDR) & 3) == 0)
167 return 0;
168 }
169
170 printk("[%s:%d] timeout in robo_reg!\n", __FILE__, __LINE__);
171
172 return 0;
173 }
174
175 static void robo_read(__u8 page, __u8 reg, __u16 *val, int count)
176 {
177 int i;
178
179 robo_reg(page, reg, REG_MII_ADDR_READ);
180
181 for (i = 0; i < count; i++)
182 val[i] = mdio_read(ROBO_PHY_ADDR, REG_MII_DATA0 + i);
183 }
184
185 static __u16 robo_read16(__u8 page, __u8 reg)
186 {
187 robo_reg(page, reg, REG_MII_ADDR_READ);
188
189 return mdio_read(ROBO_PHY_ADDR, REG_MII_DATA0);
190 }
191
192 static __u32 robo_read32(__u8 page, __u8 reg)
193 {
194 robo_reg(page, reg, REG_MII_ADDR_READ);
195
196 return mdio_read(ROBO_PHY_ADDR, REG_MII_DATA0) +
197 (mdio_read(ROBO_PHY_ADDR, REG_MII_DATA0 + 1) << 16);
198 }
199
200 static void robo_write16(__u8 page, __u8 reg, __u16 val16)
201 {
202 /* write data */
203 mdio_write(ROBO_PHY_ADDR, REG_MII_DATA0, val16);
204
205 robo_reg(page, reg, REG_MII_ADDR_WRITE);
206 }
207
208 static void robo_write32(__u8 page, __u8 reg, __u32 val32)
209 {
210 /* write data */
211 mdio_write(ROBO_PHY_ADDR, REG_MII_DATA0, val32 & 65535);
212 mdio_write(ROBO_PHY_ADDR, REG_MII_DATA0 + 1, val32 >> 16);
213
214 robo_reg(page, reg, REG_MII_ADDR_WRITE);
215 }
216
217 /* checks that attached switch is 5325E/5350 */
218 static int robo_vlan5350()
219 {
220 /* set vlan access id to 15 and read it back */
221 __u16 val16 = 15;
222 robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_TABLE_ACCESS_5350, val16);
223
224 /* 5365 will refuse this as it does not have this reg */
225 return (robo_read16(ROBO_VLAN_PAGE, ROBO_VLAN_TABLE_ACCESS_5350) == val16);
226 }
227
228
229
230 static int robo_probe(char *devname)
231 {
232 struct ethtool_drvinfo info;
233 int i;
234 __u32 phyid;
235
236 printk("Probing device %s: ", devname);
237 strcpy(ifr.ifr_name, devname);
238
239 if ((dev = dev_get_by_name(devname)) == NULL) {
240 printk("No such device\n");
241 return 1;
242 }
243
244 info.cmd = ETHTOOL_GDRVINFO;
245 if (do_ioctl(SIOCETHTOOL, (void *) &info) < 0) {
246 printk("SIOCETHTOOL: not supported\n");
247 return 1;
248 }
249
250 /* try access using MII ioctls - get phy address */
251 if (do_ioctl(SIOCGMIIPHY, NULL) < 0) {
252 use_et = 1;
253 } else {
254 /* got phy address check for robo address */
255 struct mii_ioctl_data *mii = (struct mii_ioctl_data *) &ifr.ifr_data;
256 if (mii->phy_id != ROBO_PHY_ADDR) {
257 printk("Invalid phy address (%d)\n", mii->phy_id);
258 return 1;
259 }
260 }
261
262 phyid = mdio_read(ROBO_PHY_ADDR, 0x2) |
263 (mdio_read(ROBO_PHY_ADDR, 0x3) << 16);
264
265 if (phyid == 0xffffffff || phyid == 0x55210022) {
266 printk("No Robo switch in managed mode found\n");
267 return 1;
268 }
269
270 is_5350 = robo_vlan5350();
271 max_ports = 6;
272
273 for (i = 0; i <= (is_5350 ? VLAN_ID_MAX5350 : VLAN_ID_MAX); i++) {
274 /* issue read */
275 __u16 val16 = (i) /* vlan */ | (0 << 12) /* read */ | (1 << 13) /* enable */;
276
277 if (is_5350) {
278 u32 val32;
279 robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_TABLE_ACCESS_5350, val16);
280 /* actual read */
281 val32 = robo_read32(ROBO_VLAN_PAGE, ROBO_VLAN_READ);
282 if ((val32 & (1 << 20)) /* valid */) {
283 max_vlans = i + 1;
284 }
285 } else {
286 robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_TABLE_ACCESS, val16);
287 /* actual read */
288 val16 = robo_read16(ROBO_VLAN_PAGE, ROBO_VLAN_READ);
289 if ((val16 & (1 << 14)) /* valid */) {
290 max_vlans = i + 1;
291 }
292 }
293 }
294
295 printk("found!\n");
296 return 0;
297 }
298
299
300 static int handle_vlan_port_read(char *buf, int nr)
301 {
302 __u16 val16;
303 int len = 0;
304 int j;
305
306 val16 = (nr) /* vlan */ | (0 << 12) /* read */ | (1 << 13) /* enable */;
307
308 if (is_5350) {
309 u32 val32;
310 robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_TABLE_ACCESS_5350, val16);
311 /* actual read */
312 val32 = robo_read32(ROBO_VLAN_PAGE, ROBO_VLAN_READ);
313 if ((val32 & (1 << 20)) /* valid */) {
314 for (j = 0; j < 6; j++) {
315 if (val32 & (1 << j)) {
316 len += sprintf(buf + len, "%d%s\t", j,
317 (val32 & (1 << (j + 6))) ? (j == 5 ? "u" : "") : "t");
318 }
319 }
320 len += sprintf(buf + len, "\n");
321 }
322 } else {
323 robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_TABLE_ACCESS, val16);
324 /* actual read */
325 val16 = robo_read16(ROBO_VLAN_PAGE, ROBO_VLAN_READ);
326 if ((val16 & (1 << 14)) /* valid */) {
327 for (j = 0; j < 6; j++) {
328 if (val16 & (1 << j)) {
329 len += sprintf(buf + len, "%d%s\t", j, (val16 & (1 << (j + 7))) ?
330 (j == 5 ? "u" : "") : "t");
331 }
332 }
333 len += sprintf(buf + len, "\n");
334 }
335 }
336
337 return len;
338 }
339
340 static int handle_vlan_port_write(char *buf, int nr)
341 {
342 int untag = 0;
343 int member = 0;
344 int j;
345 __u16 val16;
346
347 while (*buf >= '0' && *buf <= '9') {
348 j = *buf++ - '0';
349 member |= 1 << j;
350
351 /* untag if needed, CPU port requires special handling */
352 if (*buf == 'u' || (j != 5 && (isspace(*buf) || *buf == 0))) {
353 untag |= 1 << j;
354 if (*buf) buf++;
355 /* change default vlan tag */
356 robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_PORT0_DEF_TAG + (j << 1), nr);
357 } else if (*buf == '*' || *buf == 't' || isspace(*buf)) {
358 buf++;
359 } else break;
360
361 while (isspace(*buf)) buf++;
362 }
363
364 if (*buf) {
365 return -1;
366 } else {
367 /* write config now */
368 val16 = (nr) /* vlan */ | (1 << 12) /* write */ | (1 << 13) /* enable */;
369 if (is_5350) {
370 robo_write32(ROBO_VLAN_PAGE, ROBO_VLAN_WRITE_5350,
371 (1 << 20) /* valid */ | (untag << 6) | member);
372 robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_TABLE_ACCESS_5350, val16);
373 } else {
374 robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_WRITE,
375 (1 << 14) /* valid */ | (untag << 7) | member);
376 robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_TABLE_ACCESS, val16);
377 }
378 }
379 return 0;
380 }
381
382 static int __init robo_init()
383 {
384 char *device = "ethX";
385 int notfound = 1;
386
387 for (device[3] = '0'; (device[3] <= '3') && notfound; device[3]++) {
388 notfound = robo_probe(device);
389 }
390
391 if (notfound)
392 return -ENODEV;
393 else {
394 switch_config vlan[] = {
395 {"ports", handle_vlan_port_read, handle_vlan_port_write},
396 {NULL, NULL, NULL}
397 };
398 switch_driver driver = {
399 name: DRIVER_NAME,
400 ports: max_ports,
401 vlans: max_vlans,
402 driver_handlers: NULL,
403 port_handlers: NULL,
404 vlan_handlers: vlan,
405 };
406
407 return switch_register_driver(&driver);
408 }
409 }
410
411 static void __exit robo_exit()
412 {
413 switch_unregister_driver(DRIVER_NAME);
414 }
415
416
417 MODULE_AUTHOR("Felix Fietkau <openwrt@nbd.name>");
418 MODULE_LICENSE("GPL");
419
420 module_init(robo_init);
421 module_exit(robo_exit);
This page took 0.056047 seconds and 5 git commands to generate.