2 * swconfig-ip175c.c: Swconfig configuration for IC+ IP175C switch
4 * Copyright (C) 2008 Patrick Horn <patrick.horn@gmail.com>
5 * Copyright (C) 2008 Martin Mares <mj@ucw.cz>
6 * Copyright (C) 2009 Felix Fietkau <nbd@openwrt.org>
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.
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.
19 #include <linux/kernel.h>
20 #include <linux/module.h>
21 #include <linux/init.h>
22 #include <linux/list.h>
23 #include <linux/skbuff.h>
24 #include <linux/mii.h>
25 #include <linux/phy.h>
26 #include <linux/delay.h>
27 #include <linux/switch.h>
33 typedef struct ip175c_reg
{
39 #define NOTSUPPORTED {-1,-1}
41 #define REG_SUPP(x) (((x).m != ((u16)-1)) && ((x).p != (u16)-1))
45 /*********** CONSTANTS ***********/
46 struct register_mappings
{
48 u16 MODEL_NO
; // Compare to bits 4-9 of MII register 0,3.
52 /* The default VLAN for each port.
53 Default: 0x0001 for Ports 0,1,2,3
54 0x0002 for Ports 4,5 */
55 reg VLAN_DEFAULT_TAG_REG
[MAX_PORTS
];
57 /* These ports are tagged.
61 bitnum ADD_TAG_BIT
[MAX_PORTS
];
62 /* These ports are untagged.
63 Default: 0x00 (i.e. do not alter any VLAN tags...)
64 Maybe set to 0 if user disables VLANs. */
65 bitnum REMOVE_TAG_BIT
[MAX_PORTS
];
67 /* Port M and Port N are on the same VLAN.
68 Default: All ports on all VLANs. */
69 // Use register {29, 19+N/2}
71 // Port 5 uses register {30, 18} but same as odd bits.
72 reg VLAN_LOOKUP_REG_5
; // in a different register on IP175C.
73 bitnum VLAN_LOOKUP_EVEN_BIT
[MAX_PORTS
];
74 bitnum VLAN_LOOKUP_ODD_BIT
[MAX_PORTS
];
76 /* This VLAN corresponds to which ports.
77 Default: 0x2f,0x30,0x3f,0x3f... */
78 reg TAG_VLAN_MASK_REG
;
79 bitnum TAG_VLAN_MASK_EVEN_BIT
[MAX_PORTS
];
80 bitnum TAG_VLAN_MASK_ODD_BIT
[MAX_PORTS
];
89 reg ROUTER_CONTROL_REG
;
93 bitnum NUMLAN_GROUPS_MAX
;
94 bitnum NUMLAN_GROUPS_BIT
;
97 bitnum MII_REGISTER_EN_BIT
;
99 // set to 1 for 178C, 0 for 175C.
100 bitnum SIMPLE_VLAN_REGISTERS
; // 175C has two vlans per register but 178C has only one.
102 // Pointers to functions which manipulate hardware state
103 int (*get_flags
)(struct ip175c_state
*state
);
104 int (*get_state
)(struct ip175c_state
*state
);
105 int (*update_state
)(struct ip175c_state
*state
);
106 int (*set_vlan_mode
)(struct ip175c_state
*state
);
107 int (*reset
)(struct ip175c_state
*state
);
110 static int ip175c_get_flags(struct ip175c_state
*state
);
111 static int ip175c_get_state(struct ip175c_state
*state
);
112 static int ip175c_update_state(struct ip175c_state
*state
);
113 static int ip175c_set_vlan_mode(struct ip175c_state
*state
);
114 static int ip175c_do_reset(struct ip175c_state
*state
);
116 static const struct register_mappings IP178C
= {
119 .VLAN_DEFAULT_TAG_REG
= {
120 {30,3},{30,4},{30,5},{30,6},{30,7},{30,8},
121 {30,9},{30,10},{30,11},
124 .ADD_TAG_REG
= {30,12},
125 .ADD_TAG_BIT
= {0,1,2,3,4,5,6,7,8},
126 .REMOVE_TAG_REG
= {30,13},
127 .REMOVE_TAG_BIT
= {4,5,6,7,8,9,10,11,12},
129 .SIMPLE_VLAN_REGISTERS
= 1,
131 .VLAN_LOOKUP_REG
= {31,0},// +N
132 .VLAN_LOOKUP_REG_5
= NOTSUPPORTED
, // not used with SIMPLE_VLAN_REGISTERS
133 .VLAN_LOOKUP_EVEN_BIT
= {0,1,2,3,4,5,6,7,8},
134 .VLAN_LOOKUP_ODD_BIT
= {0,1,2,3,4,5,6,7,8},
136 .TAG_VLAN_MASK_REG
= {30,14}, // +N
137 .TAG_VLAN_MASK_EVEN_BIT
= {0,1,2,3,4,5,6,7,8},
138 .TAG_VLAN_MASK_ODD_BIT
= {0,1,2,3,4,5,6,7,8},
143 .MODE_REG
= NOTSUPPORTED
,
145 .ROUTER_CONTROL_REG
= {30,30},
147 .NUMLAN_GROUPS_MAX
= 8,
148 .NUMLAN_GROUPS_BIT
= 8, // {0-2}
150 .VLAN_CONTROL_REG
= {30,13},
156 .MII_REGISTER_EN
= NOTSUPPORTED
,
158 .get_flags
= ip175c_get_flags
,
159 .get_state
= ip175c_get_state
,
160 .update_state
= ip175c_update_state
,
161 .set_vlan_mode
= ip175c_set_vlan_mode
,
162 .reset
= ip175c_do_reset
,
165 static const struct register_mappings IP175C
= {
168 .VLAN_DEFAULT_TAG_REG
= {
169 {29,24},{29,25},{29,26},{29,27},{29,28},{29,30},
170 NOTSUPPORTED
,NOTSUPPORTED
,NOTSUPPORTED
173 .ADD_TAG_REG
= {29,23},
174 .REMOVE_TAG_REG
= {29,23},
175 .ADD_TAG_BIT
= {11,12,13,14,15,1,-1,-1,-1},
176 .REMOVE_TAG_BIT
= {6,7,8,9,10,0,-1,-1,-1},
178 .SIMPLE_VLAN_REGISTERS
= 0,
180 .VLAN_LOOKUP_REG
= {29,19},// +N/2
181 .VLAN_LOOKUP_REG_5
= {30,18},
182 .VLAN_LOOKUP_EVEN_BIT
= {8,9,10,11,12,15,-1,-1,-1},
183 .VLAN_LOOKUP_ODD_BIT
= {0,1,2,3,4,7,-1,-1,-1},
185 .TAG_VLAN_MASK_REG
= {30,1}, // +N/2
186 .TAG_VLAN_MASK_EVEN_BIT
= {0,1,2,3,4,5,-1,-1,-1},
187 .TAG_VLAN_MASK_ODD_BIT
= {8,9,10,11,12,13,-1,-1,-1},
194 .ROUTER_CONTROL_REG
= {30,9},
196 .NUMLAN_GROUPS_MAX
= 8,
197 .NUMLAN_GROUPS_BIT
= 0, // {0-2}
199 .VLAN_CONTROL_REG
= {30,9},
205 .MII_REGISTER_EN
= NOTSUPPORTED
,
207 .get_flags
= ip175c_get_flags
,
208 .get_state
= ip175c_get_state
,
209 .update_state
= ip175c_update_state
,
210 .set_vlan_mode
= ip175c_set_vlan_mode
,
211 .reset
= ip175c_do_reset
,
214 static const struct register_mappings IP175A
= {
217 .VLAN_DEFAULT_TAG_REG
= {
218 {0,24},{0,25},{0,26},{0,27},{0,28},NOTSUPPORTED
,
219 NOTSUPPORTED
,NOTSUPPORTED
,NOTSUPPORTED
222 .ADD_TAG_REG
= {0,23},
223 .REMOVE_TAG_REG
= {0,23},
224 .ADD_TAG_BIT
= {11,12,13,14,15,-1,-1,-1,-1},
225 .REMOVE_TAG_BIT
= {6,7,8,9,10,-1,-1,-1,-1},
227 .SIMPLE_VLAN_REGISTERS
= 0,
229 // Only programmable via EEPROM
230 .VLAN_LOOKUP_REG
= NOTSUPPORTED
,// +N/2
231 .VLAN_LOOKUP_REG_5
= NOTSUPPORTED
,
232 .VLAN_LOOKUP_EVEN_BIT
= {8,9,10,11,12,-1,-1,-1,-1},
233 .VLAN_LOOKUP_ODD_BIT
= {0,1,2,3,4,-1,-1,-1,-1},
235 .TAG_VLAN_MASK_REG
= NOTSUPPORTED
, // +N/2,
236 .TAG_VLAN_MASK_EVEN_BIT
= {-1,-1,-1,-1,-1,-1,-1,-1,-1},
237 .TAG_VLAN_MASK_ODD_BIT
= {-1,-1,-1,-1,-1,-1,-1,-1,-1},
240 .RESET_REG
= NOTSUPPORTED
,
242 .MODE_REG
= NOTSUPPORTED
,
244 .ROUTER_CONTROL_REG
= NOTSUPPORTED
,
245 .VLAN_CONTROL_REG
= NOTSUPPORTED
,
248 .NUMLAN_GROUPS_MAX
= -1,
249 .NUMLAN_GROUPS_BIT
= -1, // {0-2}
254 .MII_REGISTER_EN
= {0, 18},
255 .MII_REGISTER_EN_BIT
= 7,
257 .get_flags
= ip175c_get_flags
,
258 .get_state
= ip175c_get_state
,
259 .update_state
= ip175c_update_state
,
260 .set_vlan_mode
= ip175c_set_vlan_mode
,
261 .reset
= ip175c_do_reset
,
265 struct ip175c_state
{
266 struct switch_dev dev
;
267 struct mii_bus
*mii_bus
;
270 int router_mode
; // ROUTER_EN
271 int vlan_enabled
; // TAG_VLAN_EN
274 unsigned int shareports
;
276 unsigned int add_tag
;
277 unsigned int remove_tag
;
281 unsigned int tag
; // VLAN tag (IP175D only)
283 const struct register_mappings
*regs
;
284 reg proc_mii
; // phy/reg for the low level register access via swconfig
290 static int ip_phy_read(struct ip175c_state
*state
, int port
, int reg
)
292 int val
= mdiobus_read(state
->mii_bus
, port
, reg
);
294 pr_warning("IP175C: Unable to get MII register %d,%d: error %d\n", port
, reg
, -val
);
297 pr_debug("IP175C: Read MII(%d,%d) -> %04x\n", port
, reg
, val
);
302 static int ip_phy_write(struct ip175c_state
*state
, int port
, int reg
, u16 val
)
307 pr_debug("IP175C: Write MII(%d,%d) <- %04x\n", port
, reg
, val
);
309 err
= mdiobus_write(state
->mii_bus
, port
, reg
, val
);
311 pr_warning("IP175C: Unable to write MII register %d,%d: error %d\n", port
, reg
, -err
);
315 static int ip_phy_write_masked(struct ip175c_state
*state
, int port
, int reg
, unsigned int mask
, unsigned int data
)
317 int val
= ip_phy_read(state
, port
, reg
);
320 return ip_phy_write(state
, port
, reg
, (val
& ~mask
) | data
);
323 static int getPhy(struct ip175c_state
*state
, reg mii
)
327 return ip_phy_read(state
, mii
.p
, mii
.m
);
330 static int setPhy(struct ip175c_state
*state
, reg mii
, u16 value
)
336 err
= ip_phy_write(state
, mii
.p
, mii
.m
, value
);
345 * These two macros are to simplify the mapping of logical bits to the bits in hardware.
346 * NOTE: these macros will return if there is an error!
349 #define GET_PORT_BITS(state, bits, addr, bit_lookup) \
351 int i, val = getPhy((state), (addr)); \
355 for (i = 0; i < MAX_PORTS; i++) { \
356 if ((bit_lookup)[i] == -1) continue; \
357 if (val & (1<<(bit_lookup)[i])) \
362 #define SET_PORT_BITS(state, bits, addr, bit_lookup) \
364 int i, val = getPhy((state), (addr)); \
367 for (i = 0; i < MAX_PORTS; i++) { \
368 unsigned int newmask = ((bits)&(1<<i)); \
369 if ((bit_lookup)[i] == -1) continue; \
370 val &= ~(1<<(bit_lookup)[i]); \
371 val |= ((newmask>>i)<<(bit_lookup)[i]); \
373 val = setPhy((state), (addr), val); \
379 static int get_model(struct ip175c_state
*state
)
381 reg oui_id_reg
= {0, 2};
383 reg model_no_reg
= {0, 3};
384 int model_no
, model_no_orig
;
386 // 175 and 178 have the same oui ID.
387 reg oui_id_reg_178c
= {5, 2}; // returns error on IP175C.
390 oui_id
= getPhy(state
, oui_id_reg
);
391 if (oui_id
!= 0x0243) {
393 return -ENODEV
; // Not a IC+ chip.
395 oui_id
= getPhy(state
, oui_id_reg_178c
);
396 if (oui_id
== 0x0243) {
400 model_no_orig
= getPhy(state
, model_no_reg
);
401 if (model_no_orig
< 0) {
404 model_no
= model_no_orig
>> 4; // shift out revision number.
405 model_no
&= 0x3f; // only take the model number (low 6 bits).
406 if (model_no
== IP175A
.MODEL_NO
) {
407 state
->regs
= &IP175A
;
408 } else if (model_no
== IP175C
.MODEL_NO
) {
410 state
->regs
= &IP178C
;
412 state
->regs
= &IP175C
;
415 printk(KERN_WARNING
"ip175c: Found an unknown IC+ switch with model number %02Xh.\n", model_no_orig
);
421 /** Get only the vlan and router flags on the router **/
422 static int ip175c_get_flags(struct ip175c_state
*state
)
426 state
->router_mode
= 0;
427 state
->vlan_enabled
= -1; // hack
428 state
->num_vlans
= 0;
430 if (!REG_SUPP(state
->regs
->ROUTER_CONTROL_REG
)) {
431 return 0; // not an error if it doesn't support enable vlan.
434 val
= getPhy(state
, state
->regs
->ROUTER_CONTROL_REG
);
438 if (state
->regs
->ROUTER_EN_BIT
>= 0)
439 state
->router_mode
= ((val
>>state
->regs
->ROUTER_EN_BIT
) & 1);
441 if (state
->regs
->NUMLAN_GROUPS_BIT
>= 0) {
442 state
->num_vlans
= (val
>> state
->regs
->NUMLAN_GROUPS_BIT
);
443 state
->num_vlans
&= (state
->regs
->NUMLAN_GROUPS_MAX
-1);
444 state
->num_vlans
+=1; // does not include WAN.
447 val
= getPhy(state
, state
->regs
->VLAN_CONTROL_REG
);
451 if (state
->regs
->TAG_VLAN_BIT
>= 0)
452 state
->vlan_enabled
= ((val
>>state
->regs
->TAG_VLAN_BIT
) & 1);
457 /** Get all state variables for VLAN mappings and port-based tagging **/
458 static int ip175c_get_state(struct ip175c_state
*state
)
463 ret
= ip175c_get_flags(state
);
468 GET_PORT_BITS(state
, state
->remove_tag
,
469 state
->regs
->REMOVE_TAG_REG
, state
->regs
->REMOVE_TAG_BIT
);
470 GET_PORT_BITS(state
, state
->add_tag
,
471 state
->regs
->ADD_TAG_REG
, state
->regs
->ADD_TAG_BIT
);
473 if (state
->vlan_enabled
== -1) {
474 // not sure how to get this...
475 state
->vlan_enabled
= (state
->remove_tag
|| state
->add_tag
);
478 if (REG_SUPP(state
->regs
->VLAN_LOOKUP_REG
)) {
479 for (j
=0; j
<MAX_PORTS
; j
++) {
480 state
->ports
[j
].shareports
= 0; // initialize them in case.
482 for (j
=0; j
<state
->regs
->NUM_PORTS
; j
++) {
484 const bitnum
*bit_lookup
= (j
%2==0)?
485 state
->regs
->VLAN_LOOKUP_EVEN_BIT
:
486 state
->regs
->VLAN_LOOKUP_ODD_BIT
;
487 addr
= state
->regs
->VLAN_LOOKUP_REG
;
488 if (state
->regs
->SIMPLE_VLAN_REGISTERS
) {
503 addr
= state
->regs
->VLAN_LOOKUP_REG_5
;
508 if (REG_SUPP(addr
)) {
509 GET_PORT_BITS(state
, state
->ports
[j
].shareports
, addr
, bit_lookup
);
513 for (j
=0; j
<MAX_PORTS
; j
++) {
514 state
->ports
[j
].shareports
= 0xff;
518 for (i
=0; i
<MAX_PORTS
; i
++) {
519 if (REG_SUPP(state
->regs
->VLAN_DEFAULT_TAG_REG
[i
])) {
520 int val
= getPhy(state
, state
->regs
->VLAN_DEFAULT_TAG_REG
[i
]);
524 state
->ports
[i
].pvid
= val
;
526 state
->ports
[i
].pvid
= 0;
530 if (REG_SUPP(state
->regs
->TAG_VLAN_MASK_REG
)) {
531 for (j
=0; j
<MAX_VLANS
; j
++) {
532 reg addr
= state
->regs
->TAG_VLAN_MASK_REG
;
533 const bitnum
*bit_lookup
= (j
%2==0)?
534 state
->regs
->TAG_VLAN_MASK_EVEN_BIT
:
535 state
->regs
->TAG_VLAN_MASK_ODD_BIT
;
536 if (state
->regs
->SIMPLE_VLAN_REGISTERS
) {
541 GET_PORT_BITS(state
, state
->vlans
[j
].ports
, addr
, bit_lookup
);
544 for (j
=0; j
<MAX_VLANS
; j
++) {
545 state
->vlans
[j
].ports
= 0;
546 for (i
=0; i
<state
->regs
->NUM_PORTS
; i
++) {
547 if ((state
->ports
[i
].pvid
== j
) ||
548 (state
->ports
[i
].pvid
== 0)) {
549 state
->vlans
[j
].ports
|= (1<<i
);
558 /** Only set vlan and router flags in the switch **/
559 static int ip175c_set_flags(struct ip175c_state
*state
)
563 if (!REG_SUPP(state
->regs
->ROUTER_CONTROL_REG
)) {
567 val
= getPhy(state
, state
->regs
->ROUTER_CONTROL_REG
);
571 if (state
->regs
->ROUTER_EN_BIT
>= 0) {
572 if (state
->router_mode
) {
573 val
|= (1<<state
->regs
->ROUTER_EN_BIT
);
575 val
&= (~(1<<state
->regs
->ROUTER_EN_BIT
));
578 if (state
->regs
->TAG_VLAN_BIT
>= 0) {
579 if (state
->vlan_enabled
) {
580 val
|= (1<<state
->regs
->TAG_VLAN_BIT
);
582 val
&= (~(1<<state
->regs
->TAG_VLAN_BIT
));
585 if (state
->regs
->NUMLAN_GROUPS_BIT
>= 0) {
586 val
&= (~((state
->regs
->NUMLAN_GROUPS_MAX
-1)<<state
->regs
->NUMLAN_GROUPS_BIT
));
587 if (state
->num_vlans
> state
->regs
->NUMLAN_GROUPS_MAX
) {
588 val
|= state
->regs
->NUMLAN_GROUPS_MAX
<< state
->regs
->NUMLAN_GROUPS_BIT
;
589 } else if (state
->num_vlans
>= 1) {
590 val
|= (state
->num_vlans
-1) << state
->regs
->NUMLAN_GROUPS_BIT
;
593 return setPhy(state
, state
->regs
->ROUTER_CONTROL_REG
, val
);
596 /** Set all VLAN and port state. Usually you should call "correct_vlan_state" first. **/
597 static int ip175c_set_state(struct ip175c_state
*state
)
601 SET_PORT_BITS(state
, state
->add_tag
,
602 state
->regs
->ADD_TAG_REG
, state
->regs
->ADD_TAG_BIT
);
603 SET_PORT_BITS(state
, state
->remove_tag
,
604 state
->regs
->REMOVE_TAG_REG
, state
->regs
->REMOVE_TAG_BIT
);
606 if (REG_SUPP(state
->regs
->VLAN_LOOKUP_REG
)) {
607 for (j
=0; j
<state
->regs
->NUM_PORTS
; j
++) {
609 const bitnum
*bit_lookup
= (j
%2==0)?
610 state
->regs
->VLAN_LOOKUP_EVEN_BIT
:
611 state
->regs
->VLAN_LOOKUP_ODD_BIT
;
613 // duplicate code -- sorry
614 addr
= state
->regs
->VLAN_LOOKUP_REG
;
615 if (state
->regs
->SIMPLE_VLAN_REGISTERS
) {
630 addr
= state
->regs
->VLAN_LOOKUP_REG_5
;
633 addr
.m
= -1; // shouldn't get here, but...
637 //printf("shareports for %d is %02X\n",j,state->ports[j].shareports);
638 if (REG_SUPP(addr
)) {
639 SET_PORT_BITS(state
, state
->ports
[j
].shareports
, addr
, bit_lookup
);
643 if (REG_SUPP(state
->regs
->TAG_VLAN_MASK_REG
)) {
644 for (j
=0; j
<MAX_VLANS
; j
++) {
645 reg addr
= state
->regs
->TAG_VLAN_MASK_REG
;
646 const bitnum
*bit_lookup
= (j
%2==0)?
647 state
->regs
->TAG_VLAN_MASK_EVEN_BIT
:
648 state
->regs
->TAG_VLAN_MASK_ODD_BIT
;
649 unsigned int vlan_mask
;
650 if (state
->regs
->SIMPLE_VLAN_REGISTERS
) {
655 vlan_mask
= state
->vlans
[j
].ports
;
656 SET_PORT_BITS(state
, vlan_mask
, addr
, bit_lookup
);
660 for (i
=0; i
<MAX_PORTS
; i
++) {
661 if (REG_SUPP(state
->regs
->VLAN_DEFAULT_TAG_REG
[i
])) {
662 int err
= setPhy(state
, state
->regs
->VLAN_DEFAULT_TAG_REG
[i
],
663 state
->ports
[i
].pvid
);
670 return ip175c_set_flags(state
);
674 * Uses only the VLAN port mask and the add tag mask to generate the other fields:
675 * which ports are part of the same VLAN, removing vlan tags, and VLAN tag ids.
677 static void ip175c_correct_vlan_state(struct ip175c_state
*state
)
680 state
->num_vlans
= 0;
681 for (i
=0; i
<MAX_VLANS
; i
++) {
682 if (state
->vlans
[i
].ports
!= 0) {
683 state
->num_vlans
= i
+1; // Hack -- we need to store the "set" vlans somewhere...
687 for (i
=0; i
<state
->regs
->NUM_PORTS
; i
++) {
688 unsigned int portmask
= (1<<i
);
689 if (!state
->vlan_enabled
) {
690 // Share with everybody!
691 state
->ports
[i
].shareports
= (1<<state
->regs
->NUM_PORTS
)-1;
694 state
->ports
[i
].shareports
= portmask
;
695 for (j
=0; j
<MAX_VLANS
; j
++) {
696 if (state
->vlans
[j
].ports
& portmask
)
697 state
->ports
[i
].shareports
|= state
->vlans
[j
].ports
;
702 static int ip175c_update_state(struct ip175c_state
*state
)
704 ip175c_correct_vlan_state(state
);
705 return ip175c_set_state(state
);
708 static int ip175c_set_vlan_mode(struct ip175c_state
*state
)
710 return ip175c_update_state(state
);
713 static int ip175c_do_reset(struct ip175c_state
*state
)
717 if (REG_SUPP(state
->regs
->MODE_REG
)) {
718 err
= setPhy(state
, state
->regs
->MODE_REG
, state
->regs
->MODE_VAL
);
721 err
= getPhy(state
, state
->regs
->MODE_REG
);
729 static int ip175c_get_enable_vlan(struct switch_dev
*dev
, const struct switch_attr
*attr
, struct switch_val
*val
)
731 struct ip175c_state
*state
= dev
->priv
;
734 err
= state
->regs
->get_state(state
); // May be set in get_state.
737 val
->value
.i
= state
->vlan_enabled
;
741 static void ip175c_reset_vlan_config(struct ip175c_state
*state
)
745 state
->remove_tag
= (state
->vlan_enabled
? ((1<<state
->regs
->NUM_PORTS
)-1) : 0x0000);
746 state
->add_tag
= 0x0000;
747 for (i
= 0; i
< MAX_VLANS
; i
++) {
748 state
->vlans
[i
].ports
= 0x0000;
749 state
->vlans
[i
].tag
= (i
? i
: 16);
751 for (i
= 0; i
< MAX_PORTS
; i
++)
752 state
->ports
[i
].pvid
= 0;
755 static int ip175c_set_enable_vlan(struct switch_dev
*dev
, const struct switch_attr
*attr
, struct switch_val
*val
)
757 struct ip175c_state
*state
= dev
->priv
;
761 err
= state
->regs
->get_state(state
);
764 enable
= val
->value
.i
;
766 if (state
->vlan_enabled
== enable
) {
767 // Do not change any state.
770 state
->vlan_enabled
= enable
;
772 // Otherwise, if we are switching state, set fields to a known default.
773 ip175c_reset_vlan_config(state
);
775 return state
->regs
->set_vlan_mode(state
);
778 static int ip175c_get_ports(struct switch_dev
*dev
, struct switch_val
*val
)
780 struct ip175c_state
*state
= dev
->priv
;
786 if (val
->port_vlan
>= dev
->vlans
|| val
->port_vlan
< 0)
789 err
= state
->regs
->get_state(state
);
793 ports
= state
->vlans
[val
->port_vlan
].ports
;
796 while (b
< MAX_PORTS
) {
798 int istagged
= ((state
->add_tag
>> b
) & 1);
799 val
->value
.ports
[ind
].id
= b
;
800 val
->value
.ports
[ind
].flags
= (istagged
<< SWITCH_PORT_FLAG_TAGGED
);
811 static int ip175c_set_ports(struct switch_dev
*dev
, struct switch_val
*val
)
813 struct ip175c_state
*state
= dev
->priv
;
817 if (val
->port_vlan
>= dev
->vlans
|| val
->port_vlan
< 0)
820 err
= state
->regs
->get_state(state
);
824 state
->vlans
[val
->port_vlan
].ports
= 0;
825 for (i
= 0; i
< val
->len
; i
++) {
826 unsigned int bitmask
= (1<<val
->value
.ports
[i
].id
);
827 state
->vlans
[val
->port_vlan
].ports
|= bitmask
;
828 if (val
->value
.ports
[i
].flags
& (1<<SWITCH_PORT_FLAG_TAGGED
)) {
829 state
->add_tag
|= bitmask
;
830 state
->remove_tag
&= (~bitmask
);
832 state
->add_tag
&= (~bitmask
);
833 state
->remove_tag
|= bitmask
;
837 return state
->regs
->update_state(state
);
840 static int ip175c_apply(struct switch_dev
*dev
)
842 struct ip175c_state
*state
= dev
->priv
;
845 err
= state
->regs
->get_flags(state
);
849 if (REG_SUPP(state
->regs
->MII_REGISTER_EN
)) {
850 int val
= getPhy(state
, state
->regs
->MII_REGISTER_EN
);
854 val
|= (1<<state
->regs
->MII_REGISTER_EN_BIT
);
855 return setPhy(state
, state
->regs
->MII_REGISTER_EN
, val
);
860 static int ip175c_reset(struct switch_dev
*dev
)
862 struct ip175c_state
*state
= dev
->priv
;
865 err
= state
->regs
->get_flags(state
);
869 if (REG_SUPP(state
->regs
->RESET_REG
)) {
870 err
= setPhy(state
, state
->regs
->RESET_REG
, state
->regs
->RESET_VAL
);
873 err
= getPhy(state
, state
->regs
->RESET_REG
);
876 * Data sheet specifies reset period to be 2 msec.
877 * (I don't see any mention of the 2ms delay in the IP178C spec, only
878 * in IP175C, but it can't hurt.)
883 /* reset switch ports */
884 for (i
= 0; i
< state
->regs
->NUM_PORTS
-1; i
++) {
885 err
= ip_phy_write(state
, i
, MII_BMCR
, BMCR_RESET
);
890 ip175c_reset_vlan_config(state
);
892 return state
->regs
->reset(state
);
895 static int ip175c_get_tagged(struct switch_dev
*dev
, const struct switch_attr
*attr
, struct switch_val
*val
)
897 struct ip175c_state
*state
= dev
->priv
;
900 err
= state
->regs
->get_state(state
);
904 if (state
->add_tag
& (1<<val
->port_vlan
)) {
905 if (state
->remove_tag
& (1<<val
->port_vlan
))
906 val
->value
.i
= 3; // shouldn't ever happen.
910 if (state
->remove_tag
& (1<<val
->port_vlan
))
918 static int ip175c_set_tagged(struct switch_dev
*dev
, const struct switch_attr
*attr
, struct switch_val
*val
)
920 struct ip175c_state
*state
= dev
->priv
;
923 err
= state
->regs
->get_state(state
);
927 state
->add_tag
&= ~(1<<val
->port_vlan
);
928 state
->remove_tag
&= ~(1<<val
->port_vlan
);
930 if (val
->value
.i
== 0)
931 state
->remove_tag
|= (1<<val
->port_vlan
);
932 if (val
->value
.i
== 1)
933 state
->add_tag
|= (1<<val
->port_vlan
);
935 return state
->regs
->update_state(state
);
938 /** Get the current phy address */
939 static int ip175c_get_phy(struct switch_dev
*dev
, const struct switch_attr
*attr
, struct switch_val
*val
)
941 struct ip175c_state
*state
= dev
->priv
;
943 val
->value
.i
= state
->proc_mii
.p
;
947 /** Set a new phy address for low level access to registers */
948 static int ip175c_set_phy(struct switch_dev
*dev
, const struct switch_attr
*attr
, struct switch_val
*val
)
950 struct ip175c_state
*state
= dev
->priv
;
951 int new_reg
= val
->value
.i
;
953 if (new_reg
< 0 || new_reg
> 31)
954 state
->proc_mii
.p
= (u16
)-1;
956 state
->proc_mii
.p
= (u16
)new_reg
;
960 /** Get the current register number */
961 static int ip175c_get_reg(struct switch_dev
*dev
, const struct switch_attr
*attr
, struct switch_val
*val
)
963 struct ip175c_state
*state
= dev
->priv
;
965 val
->value
.i
= state
->proc_mii
.m
;
969 /** Set a new register address for low level access to registers */
970 static int ip175c_set_reg(struct switch_dev
*dev
, const struct switch_attr
*attr
, struct switch_val
*val
)
972 struct ip175c_state
*state
= dev
->priv
;
973 int new_reg
= val
->value
.i
;
975 if (new_reg
< 0 || new_reg
> 31)
976 state
->proc_mii
.m
= (u16
)-1;
978 state
->proc_mii
.m
= (u16
)new_reg
;
982 /** Get the register content of state->proc_mii */
983 static int ip175c_get_val(struct switch_dev
*dev
, const struct switch_attr
*attr
, struct switch_val
*val
)
985 struct ip175c_state
*state
= dev
->priv
;
986 int retval
= -EINVAL
;
987 if (REG_SUPP(state
->proc_mii
))
988 retval
= getPhy(state
, state
->proc_mii
);
993 val
->value
.i
= retval
;
998 /** Write a value to the register defined by phy/reg above */
999 static int ip175c_set_val(struct switch_dev
*dev
, const struct switch_attr
*attr
, struct switch_val
*val
)
1001 struct ip175c_state
*state
= dev
->priv
;
1002 int myval
, err
= -EINVAL
;
1004 myval
= val
->value
.i
;
1005 if (myval
<= 0xffff && myval
>= 0 && REG_SUPP(state
->proc_mii
)) {
1006 err
= setPhy(state
, state
->proc_mii
, (u16
)myval
);
1011 static int ip175c_read_name(struct switch_dev
*dev
, const struct switch_attr
*attr
, struct switch_val
*val
)
1013 struct ip175c_state
*state
= dev
->priv
;
1014 val
->value
.s
= state
->regs
->NAME
; // Just a const pointer, won't be freed by swconfig.
1018 static int ip175c_get_tag(struct switch_dev
*dev
, const struct switch_attr
*attr
, struct switch_val
*val
)
1020 struct ip175c_state
*state
= dev
->priv
;
1021 int vlan
= val
->port_vlan
;
1023 if (vlan
< 0 || vlan
>= MAX_VLANS
)
1026 val
->value
.i
= state
->vlans
[vlan
].tag
;
1030 static int ip175c_set_tag(struct switch_dev
*dev
, const struct switch_attr
*attr
, struct switch_val
*val
)
1032 struct ip175c_state
*state
= dev
->priv
;
1033 int vlan
= val
->port_vlan
;
1034 int tag
= val
->value
.i
;
1036 if (vlan
< 0 || vlan
>= MAX_VLANS
)
1039 if (tag
< 0 || tag
> 4095)
1042 state
->vlans
[vlan
].tag
= tag
;
1043 return state
->regs
->update_state(state
);
1046 static int ip175c_set_port_speed(struct switch_dev
*dev
, const struct switch_attr
*attr
, struct switch_val
*val
)
1048 struct ip175c_state
*state
= dev
->priv
;
1049 int nr
= val
->port_vlan
;
1053 if (val
->value
.i
== 100) {
1056 } else if (val
->value
.i
== 10) {
1064 /* Can't set speed for cpu port */
1065 if (nr
== state
->regs
->CPU_PORT
)
1068 if (nr
>= dev
->ports
|| nr
< 0)
1071 ctrl
= ip_phy_read(state
, nr
, 0);
1077 ctrl
|= (autoneg
<<12);
1078 ctrl
|= (speed
<<13);
1080 return ip_phy_write(state
, nr
, 0, ctrl
);
1083 static int ip175c_get_port_speed(struct switch_dev
*dev
, const struct switch_attr
*attr
, struct switch_val
*val
)
1085 struct ip175c_state
*state
= dev
->priv
;
1086 int nr
= val
->port_vlan
;
1089 if (nr
== state
->regs
->CPU_PORT
) {
1094 if (nr
>= dev
->ports
|| nr
< 0)
1097 status
= ip_phy_read(state
, nr
, 1);
1098 speed
= ip_phy_read(state
, nr
, 18);
1099 if (status
< 0 || speed
< 0)
1103 val
->value
.i
= ((speed
& (1<<11)) ? 100 : 10);
1110 static int ip175c_get_port_status(struct switch_dev
*dev
, const struct switch_attr
*attr
, struct switch_val
*val
)
1112 struct ip175c_state
*state
= dev
->priv
;
1113 int ctrl
, speed
, status
;
1114 int nr
= val
->port_vlan
;
1116 char *buf
= state
->buf
; // fixed-length at 80.
1118 if (nr
== state
->regs
->CPU_PORT
) {
1119 sprintf(buf
, "up, 100 Mbps, cpu port");
1124 if (nr
>= dev
->ports
|| nr
< 0)
1127 ctrl
= ip_phy_read(state
, nr
, 0);
1128 status
= ip_phy_read(state
, nr
, 1);
1129 speed
= ip_phy_read(state
, nr
, 18);
1130 if (ctrl
< 0 || status
< 0 || speed
< 0)
1134 len
= sprintf(buf
, "up, %d Mbps, %s duplex",
1135 ((speed
& (1<<11)) ? 100 : 10),
1136 ((speed
& (1<<10)) ? "full" : "half"));
1138 len
= sprintf(buf
, "down");
1140 if (ctrl
& (1<<12)) {
1141 len
+= sprintf(buf
+len
, ", auto-negotiate");
1142 if (!(status
& (1<<5)))
1143 len
+= sprintf(buf
+len
, " (in progress)");
1145 len
+= sprintf(buf
+len
, ", fixed speed (%d)",
1146 ((ctrl
& (1<<13)) ? 100 : 10));
1154 static int ip175c_get_pvid(struct switch_dev
*dev
, int port
, int *val
)
1156 struct ip175c_state
*state
= dev
->priv
;
1158 *val
= state
->ports
[port
].pvid
;
1162 static int ip175c_set_pvid(struct switch_dev
*dev
, int port
, int val
)
1164 struct ip175c_state
*state
= dev
->priv
;
1167 if (val
< 0 || val
>= MAX_VLANS
)
1170 err
= state
->regs
->get_state(state
);
1174 state
->ports
[port
].pvid
= val
;
1176 return state
->regs
->update_state(state
);
1190 IP175C_REGISTER_PHY
,
1191 IP175C_REGISTER_MII
,
1192 IP175C_REGISTER_VALUE
,
1193 IP175C_REGISTER_ERRNO
,
1200 static const struct switch_attr ip175c_global
[] = {
1201 [IP175C_ENABLE_VLAN
] = {
1202 .id
= IP175C_ENABLE_VLAN
,
1203 .type
= SWITCH_TYPE_INT
,
1204 .name
= "enable_vlan",
1205 .description
= "Flag to enable or disable VLANs and tagging",
1206 .get
= ip175c_get_enable_vlan
,
1207 .set
= ip175c_set_enable_vlan
,
1209 [IP175C_GET_NAME
] = {
1210 .id
= IP175C_GET_NAME
,
1211 .type
= SWITCH_TYPE_STRING
,
1212 .description
= "Returns the type of IC+ chip.",
1214 .get
= ip175c_read_name
,
1217 /* jal: added for low level debugging etc. */
1218 [IP175C_REGISTER_PHY
] = {
1219 .id
= IP175C_REGISTER_PHY
,
1220 .type
= SWITCH_TYPE_INT
,
1221 .description
= "Direct register access: set PHY (0-4, or 29,30,31)",
1223 .get
= ip175c_get_phy
,
1224 .set
= ip175c_set_phy
,
1226 [IP175C_REGISTER_MII
] = {
1227 .id
= IP175C_REGISTER_MII
,
1228 .type
= SWITCH_TYPE_INT
,
1229 .description
= "Direct register access: set MII register number (0-31)",
1231 .get
= ip175c_get_reg
,
1232 .set
= ip175c_set_reg
,
1234 [IP175C_REGISTER_VALUE
] = {
1235 .id
= IP175C_REGISTER_VALUE
,
1236 .type
= SWITCH_TYPE_INT
,
1237 .description
= "Direct register access: read/write to register (0-65535)",
1239 .get
= ip175c_get_val
,
1240 .set
= ip175c_set_val
,
1244 static const struct switch_attr ip175c_vlan
[] = {
1245 [IP175C_VLAN_TAG
] = {
1246 .id
= IP175C_VLAN_TAG
,
1247 .type
= SWITCH_TYPE_INT
,
1248 .description
= "VLAN tag (0-4095) [IP175D only]",
1250 .get
= ip175c_get_tag
,
1251 .set
= ip175c_set_tag
,
1255 static const struct switch_attr ip175c_port
[] = {
1256 [IP175C_PORT_STATUS
] = {
1257 .id
= IP175C_PORT_STATUS
,
1258 .type
= SWITCH_TYPE_STRING
,
1259 .description
= "Returns Detailed port status",
1261 .get
= ip175c_get_port_status
,
1264 [IP175C_PORT_LINK
] = {
1265 .id
= IP175C_PORT_LINK
,
1266 .type
= SWITCH_TYPE_INT
,
1267 .description
= "Link speed. Can write 0 for auto-negotiate, or 10 or 100",
1269 .get
= ip175c_get_port_speed
,
1270 .set
= ip175c_set_port_speed
,
1272 [IP175C_PORT_TAGGED
] = {
1273 .id
= IP175C_PORT_LINK
,
1274 .type
= SWITCH_TYPE_INT
,
1275 .description
= "0 = untag, 1 = add tags, 2 = do not alter (This value is reset if vlans are altered)",
1277 .get
= ip175c_get_tagged
,
1278 .set
= ip175c_set_tagged
,
1282 static int ip175c_probe(struct phy_device
*pdev
)
1284 struct ip175c_state
*state
;
1285 struct switch_dev
*dev
;
1288 /* We only attach to PHY 0, but use all available PHYs */
1289 if (pdev
->addr
!= 0)
1292 state
= kzalloc(sizeof(*state
), GFP_KERNEL
);
1297 dev
->attr_global
.attr
= ip175c_global
;
1298 dev
->attr_global
.n_attr
= ARRAY_SIZE(ip175c_global
);
1299 dev
->attr_port
.attr
= ip175c_port
;
1300 dev
->attr_port
.n_attr
= ARRAY_SIZE(ip175c_port
);
1301 dev
->attr_vlan
.attr
= ip175c_vlan
;
1302 dev
->attr_vlan
.n_attr
= ARRAY_SIZE(ip175c_vlan
);
1304 dev
->get_port_pvid
= ip175c_get_pvid
;
1305 dev
->set_port_pvid
= ip175c_set_pvid
;
1306 dev
->get_vlan_ports
= ip175c_get_ports
;
1307 dev
->set_vlan_ports
= ip175c_set_ports
;
1308 dev
->apply_config
= ip175c_apply
;
1309 dev
->reset_switch
= ip175c_reset
;
1313 state
->mii_bus
= pdev
->bus
;
1315 err
= get_model(state
);
1319 dev
->vlans
= MAX_VLANS
;
1320 dev
->cpu_port
= state
->regs
->CPU_PORT
;
1321 dev
->ports
= state
->regs
->NUM_PORTS
;
1322 dev
->name
= state
->regs
->NAME
;
1331 static int ip175c_config_init(struct phy_device
*pdev
)
1333 struct ip175c_state
*state
= pdev
->priv
;
1334 struct net_device
*dev
= pdev
->attached_dev
;
1337 pdev
->irq
= PHY_IGNORE_INTERRUPT
;
1338 err
= register_switch(&state
->dev
, dev
);
1342 ip175c_reset(&state
->dev
);
1344 state
->registered
= true;
1345 netif_carrier_on(pdev
->attached_dev
);
1350 static void ip175c_remove(struct phy_device
*pdev
)
1352 struct ip175c_state
*state
= pdev
->priv
;
1354 if (state
->registered
)
1355 unregister_switch(&state
->dev
);
1359 static int ip175c_config_aneg(struct phy_device
*pdev
)
1364 static int ip175c_read_status(struct phy_device
*pdev
)
1366 pdev
->speed
= SPEED_100
;
1367 pdev
->duplex
= DUPLEX_FULL
;
1368 pdev
->pause
= pdev
->asym_pause
= 0;
1372 static struct phy_driver ip175c_driver
= {
1373 .name
= "IC+ IP175C",
1374 .phy_id
= 0x02430d80,
1375 .phy_id_mask
= 0x0ffffff0,
1376 .features
= PHY_BASIC_FEATURES
,
1377 .probe
= ip175c_probe
,
1378 .remove
= ip175c_remove
,
1379 .config_init
= ip175c_config_init
,
1380 .config_aneg
= ip175c_config_aneg
,
1381 .read_status
= ip175c_read_status
,
1382 .driver
= { .owner
= THIS_MODULE
},
1385 static struct phy_driver ip175a_driver
= {
1386 .name
= "IC+ IP175A",
1387 .phy_id
= 0x02430c50,
1388 .phy_id_mask
= 0x0ffffff0,
1389 .features
= PHY_BASIC_FEATURES
,
1390 .probe
= ip175c_probe
,
1391 .remove
= ip175c_remove
,
1392 .config_init
= ip175c_config_init
,
1393 .config_aneg
= ip175c_config_aneg
,
1394 .read_status
= ip175c_read_status
,
1395 .driver
= { .owner
= THIS_MODULE
},
1399 int __init
ip175c_init(void)
1403 ret
= phy_driver_register(&ip175a_driver
);
1407 return phy_driver_register(&ip175c_driver
);
1410 void __exit
ip175c_exit(void)
1412 phy_driver_unregister(&ip175c_driver
);
1413 phy_driver_unregister(&ip175a_driver
);
1416 MODULE_AUTHOR("Patrick Horn <patrick.horn@gmail.com>");
1417 MODULE_AUTHOR("Felix Fietkau <nbd@openwrt.org>");
1418 MODULE_LICENSE("GPL");
1420 module_init(ip175c_init
);
1421 module_exit(ip175c_exit
);