2 * ADMTEK Adm6996 switch configuration module
4 * Copyright (C) 2005 Felix Fietkau <nbd@nbd.name>
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.
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.
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
22 #include <linux/config.h>
23 #include <linux/module.h>
24 #include <linux/init.h>
26 #include <linux/if_arp.h>
27 #include <linux/sockios.h>
28 #include <linux/delay.h>
29 #include <asm/uaccess.h>
31 #include "switch-core.h"
34 #define DRIVER_NAME "adm6996"
35 #define DRIVER_VERSION "0.01"
43 MODULE_AUTHOR("Felix Fietkau <openwrt@nbd.name>");
44 MODULE_LICENSE("GPL");
45 MODULE_PARM(eecs, "i");
46 MODULE_PARM(eesk, "i");
47 MODULE_PARM(eedi, "i");
48 MODULE_PARM(eerc, "i");
49 MODULE_PARM(force, "i");
51 /* Minimum timing constants */
52 #define EECK_EDGE_TIME 3 /* 3us - max(adm 2.5us, 93c 1us) */
53 #define EEDI_SETUP_TIME 1 /* 1us - max(adm 10ns, 93c 400ns) */
54 #define EECS_SETUP_TIME 1 /* 1us - max(adm no, 93c 200ns) */
56 /* Handy macros for writing fixed length values */
57 #define adm_write8(cs, b) { __u8 val = (__u8) (b); adm_write(cs, &val, sizeof(val)*8); }
58 #define adm_write16(cs, w) { __u16 val = hton16(w); adm_write(cs, (__u8 *)&val, sizeof(val)*8); }
59 #define adm_write32(cs, i) { uint32 val = hton32(i); adm_write(cs, (__u8 *)&val, sizeof(val)*8); }
61 #define atoi(str) simple_strtoul(((str != NULL) ? str : ""), NULL, 0)
63 #if defined(BCMGPIO2) || defined(BCMGPIO)
64 extern char *nvram_get(char *name);
66 /* Return gpio pin number assigned to the named pin */
68 * Variable should be in format:
72 * 'def_pin' is returned if there is no such variable found.
74 static unsigned int getgpiopin(char *pin_name, unsigned int def_pin)
76 char name[] = "gpioXXXX";
80 /* Go thru all possibilities till a match in pin name */
81 for (pin = 0; pin < 16; pin ++) {
82 sprintf(name, "gpio%d", pin);
83 val = nvram_get(name);
84 if (val && !strcmp(val, pin_name))
92 static void adm_write(int cs, char *buf, unsigned int bits)
94 int i, len = (bits + 7) / 8;
97 gpioout(eecs, (cs ? eecs : 0));
98 udelay(EECK_EDGE_TIME);
100 /* Byte assemble from MSB to LSB */
101 for (i = 0; i < len; i++) {
102 /* Bit bang from MSB to LSB */
103 for (mask = 0x80; mask && bits > 0; mask >>= 1, bits --) {
106 udelay(EECK_EDGE_TIME);
108 /* Output on rising edge */
109 gpioout(eedi, ((mask & buf[i]) ? eedi : 0));
110 udelay(EEDI_SETUP_TIME);
114 udelay(EECK_EDGE_TIME);
120 udelay(EECK_EDGE_TIME);
127 static void adm_read(int cs, char *buf, unsigned int bits)
129 int i, len = (bits + 7) / 8;
132 gpioout(eecs, (cs ? eecs : 0));
133 udelay(EECK_EDGE_TIME);
135 /* Byte assemble from MSB to LSB */
136 for (i = 0; i < len; i++) {
139 /* Bit bang from MSB to LSB */
140 for (mask = 0x80, byte = 0; mask && bits > 0; mask >>= 1, bits --) {
145 udelay(EECK_EDGE_TIME);
147 /* Input on rising edge */
154 udelay(EECK_EDGE_TIME);
162 udelay(EECK_EDGE_TIME);
169 /* Enable outputs with specified value to the chip */
170 static void adm_enout(__u8 pins, __u8 val)
172 /* Prepare GPIO output value */
175 /* Enable GPIO outputs */
176 gpioouten(pins, pins);
177 udelay(EECK_EDGE_TIME);
181 /* Disable outputs to the chip */
182 static void adm_disout(__u8 pins)
184 /* Disable GPIO outputs */
186 udelay(EECK_EDGE_TIME);
190 /* Advance clock(s) */
191 static void adm_adclk(int clocks)
194 for (i = 0; i < clocks; i++) {
197 udelay(EECK_EDGE_TIME);
201 udelay(EECK_EDGE_TIME);
205 static __u32 adm_rreg(__u8 table, __u8 addr)
207 /* cmd: 01 10 T DD R RRRRRR */
209 0xFF, 0xFF, 0xFF, 0xFF,
210 (0x06 << 4) | ((table & 0x01) << 3 | (addr&64)>>6),
216 /* Enable GPIO outputs with all pins to 0 */
217 adm_enout((__u8)(eecs | eesk | eedi), 0);
219 adm_write(0, bits, 46);
220 adm_disout((__u8)(eedi));
222 adm_read (0, rbits, 32);
224 /* Extra clock(s) required per datasheet */
227 /* Disable GPIO outputs */
228 adm_disout((__u8)(eecs | eesk));
230 if (!table) /* EEPROM has 16-bit registers, but pumps out two registers in one request */
231 return (addr & 0x01 ? (rbits[0]<<8) | rbits[1] : (rbits[2]<<8) | (rbits[3]));
233 return (rbits[0]<<24) | (rbits[1]<<16) | (rbits[2]<<8) | rbits[3];
238 /* Write chip configuration register */
239 /* Follow 93c66 timing and chip's min EEPROM timing requirement */
241 adm_wreg(__u8 addr, __u16 val)
243 /* cmd(27bits): sb(1) + opc(01) + addr(bbbbbbbb) + data(bbbbbbbbbbbbbbbb) */
245 (0x05 << 5) | (addr >> 3),
246 (addr << 5) | (__u8)(val >> 11),
251 /* Enable GPIO outputs with all pins to 0 */
252 adm_enout((__u8)(eecs | eesk | eedi), 0);
254 /* Write cmd. Total 27 bits */
255 adm_write(1, bits, 27);
257 /* Extra clock(s) required per datasheet */
260 /* Disable GPIO outputs */
261 adm_disout((__u8)(eecs | eesk | eedi));
265 /* Port configuration registers */
266 static int port_conf[] = { 0x01, 0x03, 0x05, 0x07, 0x08, 0x09 };
268 /* Bits in VLAN port mapping */
269 static int vlan_ports[] = { 1 << 0, 1 << 2, 1 << 4, 1 << 6, 1 << 7, 1 << 8 };
271 static int handle_vlan_port_read(void *driver, char *buf, int nr)
273 int ports, i, c, len = 0;
275 if ((nr < 0) || (nr > 15))
278 /* Get VLAN port map */
279 ports = adm_rreg(0, 0x13 + nr);
281 for (i = 0; i <= 5; i++) {
282 if (ports & vlan_ports[i]) {
283 c = adm_rreg(0, port_conf[i]);
285 len += sprintf(buf + len, "%d", i);
288 if (((c & (0xf << 10)) >> 10) == nr)
296 len += sprintf(buf + len, "\n");
301 static int handle_vlan_port_write(void *driver, char *buf, int nr)
304 switch_driver *d = (switch_driver *) driver;
305 switch_vlan_config *c = switch_parse_vlan(d, buf);
310 ports = adm_rreg(0, 0x13 + nr);
311 for (i = 0; i < d->ports; i++) {
312 if (c->port & (1 << i)) {
313 ports |= vlan_ports[i];
315 cfg = adm_rreg(0, port_conf[i]);
318 if (c->untag & (1 << i))
323 if ((c->untag | c->pvid) & (1 << i)) {
324 cfg = (cfg & ~(0xf << 10)) | (nr << 10);
327 adm_wreg(port_conf[i], (__u16) cfg);
329 ports &= ~(vlan_ports[i]);
332 adm_wreg(0x13 + nr, (__u16) ports);
337 static int handle_port_enable_read(void *driver, char *buf, int nr)
339 return sprintf(buf, "%d\n", ((adm_rreg(0, port_conf[nr]) & (1 << 5)) ? 0 : 1));
342 static int handle_port_enable_write(void *driver, char *buf, int nr)
344 int reg = adm_rreg(0, port_conf[nr]);
348 else if (buf[0] == '1')
352 adm_wreg(port_conf[nr], (__u16) reg);
356 static int handle_port_media_read(void *driver, char *buf, int nr)
360 int reg = adm_rreg(0, port_conf[nr]);
363 media |= SWITCH_MEDIA_AUTO;
365 media |= SWITCH_MEDIA_100;
367 media |= SWITCH_MEDIA_FD;
369 len = switch_print_media(buf, media);
370 return len + sprintf(buf + len, "\n");
373 static int handle_port_media_write(void *driver, char *buf, int nr)
375 int media = switch_parse_media(buf);
376 int reg = adm_rreg(0, port_conf[nr]);
381 reg &= ~((1 << 1) | (1 << 2) | (1 << 3));
382 if (media & SWITCH_MEDIA_AUTO)
384 if (media & SWITCH_MEDIA_100)
386 if (media & SWITCH_MEDIA_FD)
389 adm_wreg(port_conf[nr], reg);
394 static int handle_vlan_enable_read(void *driver, char *buf, int nr)
396 return sprintf(buf, "%d\n", ((adm_rreg(0, 0x11) & (1 << 5)) ? 1 : 0));
399 static int handle_vlan_enable_write(void *driver, char *buf, int nr)
401 int reg = adm_rreg(0, 0x11);
405 else if (buf[0] == '0')
409 adm_wreg(0x11, (__u16) reg);
413 static int handle_reset(void *driver, char *buf, int nr)
419 * Reset sequence: RC high->low(100ms)->high(30ms)
421 * WAR: Certain boards don't have the correct power on
422 * reset logic therefore we must explicitly perform the
423 * sequence in software.
426 /* Keep RC high for at least 20ms */
427 adm_enout(eerc, eerc);
428 for (i = 0; i < 20; i ++)
430 /* Keep RC low for at least 100ms */
432 for (i = 0; i < 100; i++)
434 /* Set default configuration */
435 adm_enout((__u8)(eesk | eedi), eesk);
436 /* Keep RC high for at least 30ms */
437 adm_enout(eerc, eerc);
438 for (i = 0; i < 30; i++)
440 /* Leave RC high and disable GPIO outputs */
441 adm_disout((__u8)(eecs | eesk | eedi));
445 /* set up initial configuration for cpu port */
446 cfg = (0x8000 | /* Auto MDIX */
447 (0xf << 10) | /* PVID */
448 (1 << 4) | /* Tagging */
449 0xf); /* full duplex, 100Mbps, auto neg, flow ctrl */
450 adm_wreg(port_conf[5], cfg);
452 /* vlan mode select register (0x11): vlan on, mac clone */
453 adm_wreg(0x11, 0xff30);
458 static int handle_registers(void *driver, char *buf, int nr)
462 for (i = 0; i <= 0x33; i++) {
463 len += sprintf(buf + len, "0x%02x: 0x%04x\n", i, adm_rreg(0, i));
469 static int handle_counters(void *driver, char *buf, int nr)
473 for (i = 0; i <= 0x3c; i++) {
474 len += sprintf(buf + len, "0x%02x: 0x%08x\n", i, adm_rreg(1, i));
480 static int detect_adm()
484 #if defined(BCMGPIO2) || defined(BCMGPIO)
485 int boardflags = atoi(nvram_get("boardflags"));
487 if ((boardflags & 0x80) || force) {
490 eecs = getgpiopin("adm_eecs", 2);
491 eesk = getgpiopin("adm_eesk", 3);
492 eedi = getgpiopin("adm_eedi", 4);
493 eerc = getgpiopin("adm_rc", 0);
495 } else if ((strcmp(nvram_get("boardtype") ?: "", "bcm94710dev") == 0) &&
496 (strncmp(nvram_get("boardnum") ?: "", "42", 2) == 0)) {
497 /* WRT54G v1.1 hack */
504 printk("BFL_ENETADM not set in boardflags. Use force=1 to ignore.\n");
521 static int __init adm_init()
523 switch_config cfg[] = {
524 {"registers", handle_registers, NULL},
525 {"counters", handle_counters, NULL},
526 {"reset", NULL, handle_reset},
527 {"enable_vlan", handle_vlan_enable_read, handle_vlan_enable_write},
530 switch_config port[] = {
531 {"enable", handle_port_enable_read, handle_port_enable_write},
532 {"media", handle_port_media_read, handle_port_media_write},
535 switch_config vlan[] = {
536 {"ports", handle_vlan_port_read, handle_vlan_port_write},
539 switch_driver driver = {
541 version: DRIVER_VERSION,
546 driver_handlers: cfg,
554 return switch_register_driver(&driver);
557 static void __exit adm_exit()
559 switch_unregister_driver(DRIVER_NAME);
563 module_init(adm_init);
564 module_exit(adm_exit);