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