kmod-switch: vlan parser rewrite, some api changes, minor fixes
[openwrt.git] / target / linux / package / switch / src / switch-adm.c
1 /*
2 * ADMTEK Adm6996 switch configuration module
3 *
4 * Copyright (C) 2005 Felix Fietkau <nbd@nbd.name>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 * 02110-1301, USA.
20 */
21
22 #include <linux/config.h>
23 #include <linux/module.h>
24 #include <linux/init.h>
25 #include <linux/if.h>
26 #include <linux/if_arp.h>
27 #include <linux/sockios.h>
28 #include <linux/delay.h>
29 #include <asm/uaccess.h>
30
31 #include "switch-core.h"
32 #include "gpio.h"
33
34 #define DRIVER_NAME "adm6996"
35
36 static int eecs = 2;
37 static int eesk = 3;
38 static int eedi = 5;
39 static int eerc = 6;
40 static int force = 0;
41
42 MODULE_AUTHOR("Felix Fietkau <openwrt@nbd.name>");
43 MODULE_LICENSE("GPL");
44 MODULE_PARM(eecs, "i");
45 MODULE_PARM(eesk, "i");
46 MODULE_PARM(eedi, "i");
47 MODULE_PARM(eerc, "i");
48 MODULE_PARM(force, "i");
49
50 /* Minimum timing constants */
51 #define EECK_EDGE_TIME 3 /* 3us - max(adm 2.5us, 93c 1us) */
52 #define EEDI_SETUP_TIME 1 /* 1us - max(adm 10ns, 93c 400ns) */
53 #define EECS_SETUP_TIME 1 /* 1us - max(adm no, 93c 200ns) */
54
55 /* Handy macros for writing fixed length values */
56 #define adm_write8(cs, b) { __u8 val = (__u8) (b); adm_write(cs, &val, sizeof(val)*8); }
57 #define adm_write16(cs, w) { __u16 val = hton16(w); adm_write(cs, (__u8 *)&val, sizeof(val)*8); }
58 #define adm_write32(cs, i) { uint32 val = hton32(i); adm_write(cs, (__u8 *)&val, sizeof(val)*8); }
59
60
61 extern int getintvar(char **vars, char *name);
62
63
64 static void adm_write(int cs, char *buf, unsigned int bits)
65 {
66 int i, len = (bits + 7) / 8;
67 __u8 mask;
68
69 gpioout(eecs, (cs ? eecs : 0));
70 udelay(EECK_EDGE_TIME);
71
72 /* Byte assemble from MSB to LSB */
73 for (i = 0; i < len; i++) {
74 /* Bit bang from MSB to LSB */
75 for (mask = 0x80; mask && bits > 0; mask >>= 1, bits --) {
76 /* Clock low */
77 gpioout(eesk, 0);
78 udelay(EECK_EDGE_TIME);
79
80 /* Output on rising edge */
81 gpioout(eedi, ((mask & buf[i]) ? eedi : 0));
82 udelay(EEDI_SETUP_TIME);
83
84 /* Clock high */
85 gpioout(eesk, eesk);
86 udelay(EECK_EDGE_TIME);
87 }
88 }
89
90 /* Clock low */
91 gpioout(eesk, 0);
92 udelay(EECK_EDGE_TIME);
93
94 if (cs)
95 gpioout(eecs, 0);
96 }
97
98
99 static void adm_read(int cs, char *buf, unsigned int bits)
100 {
101 int i, len = (bits + 7) / 8;
102 __u8 mask;
103
104 gpioout(eecs, (cs ? eecs : 0));
105 udelay(EECK_EDGE_TIME);
106
107 /* Byte assemble from MSB to LSB */
108 for (i = 0; i < len; i++) {
109 __u8 byte;
110
111 /* Bit bang from MSB to LSB */
112 for (mask = 0x80, byte = 0; mask && bits > 0; mask >>= 1, bits --) {
113 __u8 gp;
114
115 /* Clock low */
116 gpioout(eesk, 0);
117 udelay(EECK_EDGE_TIME);
118
119 /* Input on rising edge */
120 gp = gpioin();
121 if (gp & eedi)
122 byte |= mask;
123
124 /* Clock high */
125 gpioout(eesk, eesk);
126 udelay(EECK_EDGE_TIME);
127 }
128
129 *buf++ = byte;
130 }
131
132 /* Clock low */
133 gpioout(eesk, 0);
134 udelay(EECK_EDGE_TIME);
135
136 if (cs)
137 gpioout(eecs, 0);
138 }
139
140
141 /* Enable outputs with specified value to the chip */
142 static void adm_enout(__u8 pins, __u8 val)
143 {
144 /* Prepare GPIO output value */
145 gpioout(pins, val);
146
147 /* Enable GPIO outputs */
148 gpioouten(pins, pins);
149 udelay(EECK_EDGE_TIME);
150 }
151
152
153 /* Disable outputs to the chip */
154 static void adm_disout(__u8 pins)
155 {
156 /* Disable GPIO outputs */
157 gpioouten(pins, 0);
158 udelay(EECK_EDGE_TIME);
159 }
160
161
162 /* Advance clock(s) */
163 static void adm_adclk(int clocks)
164 {
165 int i;
166 for (i = 0; i < clocks; i++) {
167 /* Clock high */
168 gpioout(eesk, eesk);
169 udelay(EECK_EDGE_TIME);
170
171 /* Clock low */
172 gpioout(eesk, 0);
173 udelay(EECK_EDGE_TIME);
174 }
175 }
176
177 static __u32 adm_rreg(__u8 table, __u8 addr)
178 {
179 /* cmd: 01 10 T DD R RRRRRR */
180 __u8 bits[6] = {
181 0xFF, 0xFF, 0xFF, 0xFF,
182 (0x06 << 4) | ((table & 0x01) << 3 | (addr&64)>>6),
183 ((addr&62)<<2)
184 };
185
186 __u8 rbits[4];
187
188 /* Enable GPIO outputs with all pins to 0 */
189 adm_enout((__u8)(eecs | eesk | eedi), 0);
190
191 adm_write(0, bits, 46);
192 adm_disout((__u8)(eedi));
193 adm_adclk(2);
194 adm_read (0, rbits, 32);
195
196 /* Extra clock(s) required per datasheet */
197 adm_adclk(2);
198
199 /* Disable GPIO outputs */
200 adm_disout((__u8)(eecs | eesk));
201
202 if (!table) /* EEPROM has 16-bit registers, but pumps out two registers in one request */
203 return (addr & 0x01 ? (rbits[0]<<8) | rbits[1] : (rbits[2]<<8) | (rbits[3]));
204 else
205 return (rbits[0]<<24) | (rbits[1]<<16) | (rbits[2]<<8) | rbits[3];
206 }
207
208
209
210 /* Write chip configuration register */
211 /* Follow 93c66 timing and chip's min EEPROM timing requirement */
212 void
213 adm_wreg(__u8 addr, __u16 val)
214 {
215 /* cmd(27bits): sb(1) + opc(01) + addr(bbbbbbbb) + data(bbbbbbbbbbbbbbbb) */
216 __u8 bits[4] = {
217 (0x05 << 5) | (addr >> 3),
218 (addr << 5) | (__u8)(val >> 11),
219 (__u8)(val >> 3),
220 (__u8)(val << 5)
221 };
222
223 /* Enable GPIO outputs with all pins to 0 */
224 adm_enout((__u8)(eecs | eesk | eedi), 0);
225
226 /* Write cmd. Total 27 bits */
227 adm_write(1, bits, 27);
228
229 /* Extra clock(s) required per datasheet */
230 adm_adclk(2);
231
232 /* Disable GPIO outputs */
233 adm_disout((__u8)(eecs | eesk | eedi));
234 }
235
236
237 /* Port configuration registers */
238 static int port_conf[] = { 0x01, 0x03, 0x05, 0x07, 0x08, 0x09 };
239
240 /* Bits in VLAN port mapping */
241 static int vlan_ports[] = { 1 << 0, 1 << 2, 1 << 4, 1 << 6, 1 << 7, 1 << 8 };
242
243 static int handle_vlan_port_read(void *driver, char *buf, int nr)
244 {
245 int ports, i, c, len = 0;
246
247 if ((nr < 0) || (nr > 15))
248 return 0;
249
250 /* Get VLAN port map */
251 ports = adm_rreg(0, 0x13 + nr);
252
253 for (i = 0; i <= 5; i++) {
254 if (ports & vlan_ports[i]) {
255 c = adm_rreg(0, port_conf[i]);
256 len += sprintf(buf + len, (c & (1 << 4) ? "%dt\t" : (i == 5 ? "%du\t" : "%d\t")), i);
257 }
258 }
259 len += sprintf(buf + len, "\n");
260
261 return len;
262 }
263
264 static int handle_vlan_port_write(void *driver, char *buf, int nr)
265 {
266 int i, cfg, ports;
267 switch_driver *d = (switch_driver *) driver;
268 switch_vlan_config *c = switch_parse_vlan(d, buf);
269
270 if (c == NULL)
271 return -1;
272
273 ports = adm_rreg(0, 0x13 + nr);
274 for (i = 0; i < d->ports; i++) {
275 if (c->port & (1 << i)) {
276 ports |= vlan_ports[i];
277
278 cfg = adm_rreg(0, port_conf[i]);
279
280 /* Tagging */
281 if (c->untag & (1 << i))
282 cfg &= ~(1 << 4);
283 else
284 cfg |= (1 << 4);
285
286 if ((c->untag | c->pvid) & (1 << i)) {
287 cfg = (cfg & ~(0xf << 10)) | (nr << 10);
288 }
289
290 adm_wreg(port_conf[i], (__u16) cfg);
291 } else {
292 ports &= ~(vlan_ports[i]);
293 }
294 }
295 adm_wreg(0x13 + nr, (__u16) ports);
296
297 return 0;
298 }
299
300 static int handle_port_enable_read(void *driver, char *buf, int nr)
301 {
302 return sprintf(buf, "%d\n", ((adm_rreg(0, port_conf[nr]) & (1 << 5)) ? 0 : 1));
303 }
304
305 static int handle_port_enable_write(void *driver, char *buf, int nr)
306 {
307 int reg = adm_rreg(0, port_conf[nr]);
308
309 if (buf[0] == '0')
310 reg |= (1 << 5);
311 else if (buf[0] == '1')
312 reg &= ~(1 << 5);
313 else return -1;
314
315 adm_wreg(port_conf[nr], (__u16) reg);
316 return 0;
317 }
318
319 static int handle_port_media_read(void *driver, char *buf, int nr)
320 {
321 int len;
322 int media = 0;
323 int reg = adm_rreg(0, port_conf[nr]);
324
325 if (reg & (1 << 1))
326 media |= SWITCH_MEDIA_AUTO;
327 if (reg & (1 << 2))
328 media |= SWITCH_MEDIA_100;
329 if (reg & (1 << 3))
330 media |= SWITCH_MEDIA_FD;
331
332 len = switch_print_media(buf, media);
333 return len + sprintf(buf + len, "\n");
334 }
335
336 static int handle_port_media_write(void *driver, char *buf, int nr)
337 {
338 int media = switch_parse_media(buf);
339 int reg = adm_rreg(0, port_conf[nr]);
340
341 if (media < 0)
342 return -1;
343
344 reg &= ~((1 << 1) | (1 << 2) | (1 << 3));
345 if (media & SWITCH_MEDIA_AUTO)
346 reg |= 1 << 1;
347 if (media & SWITCH_MEDIA_100)
348 reg |= 1 << 2;
349 if (media & SWITCH_MEDIA_FD)
350 reg |= 1 << 3;
351
352 adm_wreg(port_conf[nr], reg);
353
354 return 0;
355 }
356
357 static int handle_vlan_enable_read(void *driver, char *buf, int nr)
358 {
359 return sprintf(buf, "%d\n", ((adm_rreg(0, 0x11) & (1 << 5)) ? 1 : 0));
360 }
361
362 static int handle_vlan_enable_write(void *driver, char *buf, int nr)
363 {
364 int reg = adm_rreg(0, 0x11);
365
366 if (buf[0] == '1')
367 reg |= (1 << 5);
368 else if (buf[0] == '0')
369 reg &= ~(1 << 5);
370 else return -1;
371
372 adm_wreg(0x11, (__u16) reg);
373 return 0;
374 }
375
376 static int handle_reset(void *driver, char *buf, int nr)
377 {
378 int i;
379
380 /*
381 * Reset sequence: RC high->low(100ms)->high(30ms)
382 *
383 * WAR: Certain boards don't have the correct power on
384 * reset logic therefore we must explicitly perform the
385 * sequence in software.
386 */
387 /* Keep RC high for at least 20ms */
388 adm_enout(eerc, eerc);
389 for (i = 0; i < 20; i ++)
390 udelay(1000);
391 /* Keep RC low for at least 100ms */
392 adm_enout(eerc, 0);
393 for (i = 0; i < 100; i++)
394 udelay(1000);
395 /* Set default configuration */
396 adm_enout((__u8)(eesk | eedi), eesk);
397 /* Keep RC high for at least 30ms */
398 adm_enout(eerc, eerc);
399 for (i = 0; i < 30; i++)
400 udelay(1000);
401 /* Leave RC high and disable GPIO outputs */
402 adm_disout((__u8)(eecs | eesk | eedi));
403
404 /* set up initial configuration for ports */
405 for (i = 0; i <= 5; i++) {
406 int cfg = 0x8000 | /* Auto MDIX */
407 (((i == 5) ? 1 : 0) << 4) | /* Tagging */
408 0xf; /* full duplex, 100Mbps, auto neg, flow ctrl */
409 adm_wreg(port_conf[i], cfg);
410 }
411
412 /* vlan mode select register (0x11): vlan on, mac clone */
413 adm_wreg(0x11, 0xff30);
414
415 return 0;
416 }
417
418 static int handle_registers(void *driver, char *buf, int nr)
419 {
420 int i, len = 0;
421
422 for (i = 0; i <= 0x33; i++) {
423 len += sprintf(buf + len, "0x%02x: 0x%04x\n", i, adm_rreg(0, i));
424 }
425
426 return len;
427 }
428
429 static int handle_counters(void *driver, char *buf, int nr)
430 {
431 int i, len = 0;
432
433 for (i = 0; i <= 0x3c; i++) {
434 len += sprintf(buf + len, "0x%02x: 0x%08x\n", i, adm_rreg(1, i));
435 }
436
437 return len;
438 }
439
440 static int detect_adm()
441 {
442 int ret = 0;
443
444 #if defined(BCMGPIO2) || defined(BCMGPIO)
445 #ifdef LINUX_2_4
446 int boardflags = getintvar(NULL, "boardflags");
447 #else
448 extern int boardflags;
449 #endif
450 if ((boardflags & 0x80) || force)
451 ret = 1;
452 else
453 printk("BFL_ENETADM not set in boardflags. Use force=1 to ignore.\n");
454 #else
455 ret = 1;
456 #endif
457 if (ret == 1) {
458 int i = adm_rreg(0, 0);
459 if ((i == 0) || (i == 0xffff)) {
460 printk("No ADM6996 chip detected.\n");
461 ret = 0;
462 }
463 }
464
465 return ret;
466 }
467
468 static int __init adm_init()
469 {
470 switch_config cfg[] = {
471 {"registers", handle_registers, NULL},
472 {"counters", handle_counters, NULL},
473 {"reset", NULL, handle_reset},
474 {"enable_vlan", handle_vlan_enable_read, handle_vlan_enable_write},
475 {NULL, NULL, NULL}
476 };
477 switch_config port[] = {
478 {"enabled", handle_port_enable_read, handle_port_enable_write},
479 {"media", handle_port_media_read, handle_port_media_write},
480 {NULL, NULL, NULL}
481 };
482 switch_config vlan[] = {
483 {"ports", handle_vlan_port_read, handle_vlan_port_write},
484 {NULL, NULL, NULL}
485 };
486 switch_driver driver = {
487 name: DRIVER_NAME,
488 interface: "eth0",
489 ports: 6,
490 cpuport: 5,
491 vlans: 16,
492 driver_handlers: cfg,
493 port_handlers: port,
494 vlan_handlers: vlan,
495 };
496
497 eecs = (1 << eecs);
498 eesk = (1 << eesk);
499 eedi = (1 << eedi);
500
501 if (!detect_adm())
502 return -ENODEV;
503
504 return switch_register_driver(&driver);
505 }
506
507 static void __exit adm_exit()
508 {
509 switch_unregister_driver(DRIVER_NAME);
510 }
511
512
513 module_init(adm_init);
514 module_exit(adm_exit);
This page took 0.089313 seconds and 5 git commands to generate.