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
;
282 const struct register_mappings
*regs
;
283 reg proc_mii
; // phy/reg for the low level register access via swconfig
289 static int ip_phy_read(struct ip175c_state
*state
, int port
, int reg
)
291 int val
= mdiobus_read(state
->mii_bus
, port
, reg
);
293 pr_warning("IP175C: Unable to get MII register %d,%d: error %d\n", port
, reg
, -val
);
296 pr_debug("IP175C: Read MII(%d,%d) -> %04x\n", port
, reg
, val
);
301 static int ip_phy_write(struct ip175c_state
*state
, int port
, int reg
, u16 val
)
306 pr_debug("IP175C: Write MII(%d,%d) <- %04x\n", port
, reg
, val
);
308 err
= mdiobus_write(state
->mii_bus
, port
, reg
, val
);
310 pr_warning("IP175C: Unable to write MII register %d,%d: error %d\n", port
, reg
, -err
);
314 static int ip_phy_write_masked(struct ip175c_state
*state
, int port
, int reg
, unsigned int mask
, unsigned int data
)
316 int val
= ip_phy_read(state
, port
, reg
);
319 return ip_phy_write(state
, port
, reg
, (val
& ~mask
) | data
);
322 static int getPhy(struct ip175c_state
*state
, reg mii
)
326 return ip_phy_read(state
, mii
.p
, mii
.m
);
329 static int setPhy(struct ip175c_state
*state
, reg mii
, u16 value
)
335 err
= ip_phy_write(state
, mii
.p
, mii
.m
, value
);
344 * These two macros are to simplify the mapping of logical bits to the bits in hardware.
345 * NOTE: these macros will return if there is an error!
348 #define GET_PORT_BITS(state, bits, addr, bit_lookup) \
350 int i, val = getPhy((state), (addr)); \
354 for (i = 0; i < MAX_PORTS; i++) { \
355 if ((bit_lookup)[i] == -1) continue; \
356 if (val & (1<<(bit_lookup)[i])) \
361 #define SET_PORT_BITS(state, bits, addr, bit_lookup) \
363 int i, val = getPhy((state), (addr)); \
366 for (i = 0; i < MAX_PORTS; i++) { \
367 unsigned int newmask = ((bits)&(1<<i)); \
368 if ((bit_lookup)[i] == -1) continue; \
369 val &= ~(1<<(bit_lookup)[i]); \
370 val |= ((newmask>>i)<<(bit_lookup)[i]); \
372 val = setPhy((state), (addr), val); \
378 static int get_model(struct ip175c_state
*state
)
380 reg oui_id_reg
= {0, 2};
382 reg model_no_reg
= {0, 3};
383 int model_no
, model_no_orig
;
385 // 175 and 178 have the same oui ID.
386 reg oui_id_reg_178c
= {5, 2}; // returns error on IP175C.
389 oui_id
= getPhy(state
, oui_id_reg
);
390 if (oui_id
!= 0x0243) {
392 return -ENODEV
; // Not a IC+ chip.
394 oui_id
= getPhy(state
, oui_id_reg_178c
);
395 if (oui_id
== 0x0243) {
399 model_no_orig
= getPhy(state
, model_no_reg
);
400 if (model_no_orig
< 0) {
403 model_no
= model_no_orig
>> 4; // shift out revision number.
404 model_no
&= 0x3f; // only take the model number (low 6 bits).
405 if (model_no
== IP175A
.MODEL_NO
) {
406 state
->regs
= &IP175A
;
407 } else if (model_no
== IP175C
.MODEL_NO
) {
409 state
->regs
= &IP178C
;
411 state
->regs
= &IP175C
;
414 printk(KERN_WARNING
"ip175c: Found an unknown IC+ switch with model number %02Xh.\n", model_no_orig
);
420 /** Get only the vlan and router flags on the router **/
421 static int ip175c_get_flags(struct ip175c_state
*state
)
425 state
->router_mode
= 0;
426 state
->vlan_enabled
= -1; // hack
427 state
->num_vlans
= 0;
429 if (!REG_SUPP(state
->regs
->ROUTER_CONTROL_REG
)) {
430 return 0; // not an error if it doesn't support enable vlan.
433 val
= getPhy(state
, state
->regs
->ROUTER_CONTROL_REG
);
437 if (state
->regs
->ROUTER_EN_BIT
>= 0)
438 state
->router_mode
= ((val
>>state
->regs
->ROUTER_EN_BIT
) & 1);
440 if (state
->regs
->NUMLAN_GROUPS_BIT
>= 0) {
441 state
->num_vlans
= (val
>> state
->regs
->NUMLAN_GROUPS_BIT
);
442 state
->num_vlans
&= (state
->regs
->NUMLAN_GROUPS_MAX
-1);
443 state
->num_vlans
+=1; // does not include WAN.
446 val
= getPhy(state
, state
->regs
->VLAN_CONTROL_REG
);
450 if (state
->regs
->TAG_VLAN_BIT
>= 0)
451 state
->vlan_enabled
= ((val
>>state
->regs
->TAG_VLAN_BIT
) & 1);
456 /** Get all state variables for VLAN mappings and port-based tagging **/
457 static int ip175c_get_state(struct ip175c_state
*state
)
462 ret
= ip175c_get_flags(state
);
467 GET_PORT_BITS(state
, state
->remove_tag
,
468 state
->regs
->REMOVE_TAG_REG
, state
->regs
->REMOVE_TAG_BIT
);
469 GET_PORT_BITS(state
, state
->add_tag
,
470 state
->regs
->ADD_TAG_REG
, state
->regs
->ADD_TAG_BIT
);
472 if (state
->vlan_enabled
== -1) {
473 // not sure how to get this...
474 state
->vlan_enabled
= (state
->remove_tag
|| state
->add_tag
);
477 if (REG_SUPP(state
->regs
->VLAN_LOOKUP_REG
)) {
478 for (j
=0; j
<MAX_PORTS
; j
++) {
479 state
->ports
[j
].shareports
= 0; // initialize them in case.
481 for (j
=0; j
<state
->regs
->NUM_PORTS
; j
++) {
483 const bitnum
*bit_lookup
= (j
%2==0)?
484 state
->regs
->VLAN_LOOKUP_EVEN_BIT
:
485 state
->regs
->VLAN_LOOKUP_ODD_BIT
;
486 addr
= state
->regs
->VLAN_LOOKUP_REG
;
487 if (state
->regs
->SIMPLE_VLAN_REGISTERS
) {
502 addr
= state
->regs
->VLAN_LOOKUP_REG_5
;
507 if (REG_SUPP(addr
)) {
508 GET_PORT_BITS(state
, state
->ports
[j
].shareports
, addr
, bit_lookup
);
512 for (j
=0; j
<MAX_PORTS
; j
++) {
513 state
->ports
[j
].shareports
= 0xff;
517 for (i
=0; i
<MAX_PORTS
; i
++) {
518 if (REG_SUPP(state
->regs
->VLAN_DEFAULT_TAG_REG
[i
])) {
519 int val
= getPhy(state
, state
->regs
->VLAN_DEFAULT_TAG_REG
[i
]);
523 state
->ports
[i
].pvid
= val
;
525 state
->ports
[i
].pvid
= 0;
529 if (REG_SUPP(state
->regs
->TAG_VLAN_MASK_REG
)) {
530 for (j
=0; j
<MAX_VLANS
; j
++) {
531 reg addr
= state
->regs
->TAG_VLAN_MASK_REG
;
532 const bitnum
*bit_lookup
= (j
%2==0)?
533 state
->regs
->TAG_VLAN_MASK_EVEN_BIT
:
534 state
->regs
->TAG_VLAN_MASK_ODD_BIT
;
535 if (state
->regs
->SIMPLE_VLAN_REGISTERS
) {
540 GET_PORT_BITS(state
, state
->vlans
[j
].ports
, addr
, bit_lookup
);
543 for (j
=0; j
<MAX_VLANS
; j
++) {
544 state
->vlans
[j
].ports
= 0;
545 for (i
=0; i
<state
->regs
->NUM_PORTS
; i
++) {
546 if ((state
->ports
[i
].pvid
== j
) ||
547 (state
->ports
[i
].pvid
== 0)) {
548 state
->vlans
[j
].ports
|= (1<<i
);
557 /** Only set vlan and router flags in the switch **/
558 static int ip175c_set_flags(struct ip175c_state
*state
)
562 if (!REG_SUPP(state
->regs
->ROUTER_CONTROL_REG
)) {
566 val
= getPhy(state
, state
->regs
->ROUTER_CONTROL_REG
);
570 if (state
->regs
->ROUTER_EN_BIT
>= 0) {
571 if (state
->router_mode
) {
572 val
|= (1<<state
->regs
->ROUTER_EN_BIT
);
574 val
&= (~(1<<state
->regs
->ROUTER_EN_BIT
));
577 if (state
->regs
->TAG_VLAN_BIT
>= 0) {
578 if (state
->vlan_enabled
) {
579 val
|= (1<<state
->regs
->TAG_VLAN_BIT
);
581 val
&= (~(1<<state
->regs
->TAG_VLAN_BIT
));
584 if (state
->regs
->NUMLAN_GROUPS_BIT
>= 0) {
585 val
&= (~((state
->regs
->NUMLAN_GROUPS_MAX
-1)<<state
->regs
->NUMLAN_GROUPS_BIT
));
586 if (state
->num_vlans
> state
->regs
->NUMLAN_GROUPS_MAX
) {
587 val
|= state
->regs
->NUMLAN_GROUPS_MAX
<< state
->regs
->NUMLAN_GROUPS_BIT
;
588 } else if (state
->num_vlans
>= 1) {
589 val
|= (state
->num_vlans
-1) << state
->regs
->NUMLAN_GROUPS_BIT
;
592 return setPhy(state
, state
->regs
->ROUTER_CONTROL_REG
, val
);
595 /** Set all VLAN and port state. Usually you should call "correct_vlan_state" first. **/
596 static int ip175c_set_state(struct ip175c_state
*state
)
600 SET_PORT_BITS(state
, state
->add_tag
,
601 state
->regs
->ADD_TAG_REG
, state
->regs
->ADD_TAG_BIT
);
602 SET_PORT_BITS(state
, state
->remove_tag
,
603 state
->regs
->REMOVE_TAG_REG
, state
->regs
->REMOVE_TAG_BIT
);
605 if (REG_SUPP(state
->regs
->VLAN_LOOKUP_REG
)) {
606 for (j
=0; j
<state
->regs
->NUM_PORTS
; j
++) {
608 const bitnum
*bit_lookup
= (j
%2==0)?
609 state
->regs
->VLAN_LOOKUP_EVEN_BIT
:
610 state
->regs
->VLAN_LOOKUP_ODD_BIT
;
612 // duplicate code -- sorry
613 addr
= state
->regs
->VLAN_LOOKUP_REG
;
614 if (state
->regs
->SIMPLE_VLAN_REGISTERS
) {
629 addr
= state
->regs
->VLAN_LOOKUP_REG_5
;
632 addr
.m
= -1; // shouldn't get here, but...
636 //printf("shareports for %d is %02X\n",j,state->ports[j].shareports);
637 if (REG_SUPP(addr
)) {
638 SET_PORT_BITS(state
, state
->ports
[j
].shareports
, addr
, bit_lookup
);
642 if (REG_SUPP(state
->regs
->TAG_VLAN_MASK_REG
)) {
643 for (j
=0; j
<MAX_VLANS
; j
++) {
644 reg addr
= state
->regs
->TAG_VLAN_MASK_REG
;
645 const bitnum
*bit_lookup
= (j
%2==0)?
646 state
->regs
->TAG_VLAN_MASK_EVEN_BIT
:
647 state
->regs
->TAG_VLAN_MASK_ODD_BIT
;
648 unsigned int vlan_mask
;
649 if (state
->regs
->SIMPLE_VLAN_REGISTERS
) {
654 vlan_mask
= state
->vlans
[j
].ports
;
655 SET_PORT_BITS(state
, vlan_mask
, addr
, bit_lookup
);
659 for (i
=0; i
<MAX_PORTS
; i
++) {
660 if (REG_SUPP(state
->regs
->VLAN_DEFAULT_TAG_REG
[i
])) {
661 int err
= setPhy(state
, state
->regs
->VLAN_DEFAULT_TAG_REG
[i
],
662 state
->ports
[i
].pvid
);
669 return ip175c_set_flags(state
);
673 * Uses only the VLAN port mask and the add tag mask to generate the other fields:
674 * which ports are part of the same VLAN, removing vlan tags, and VLAN tag ids.
676 static void ip175c_correct_vlan_state(struct ip175c_state
*state
)
679 state
->num_vlans
= 0;
680 for (i
=0; i
<MAX_VLANS
; i
++) {
681 if (state
->vlans
[i
].ports
!= 0) {
682 state
->num_vlans
= i
+1; // Hack -- we need to store the "set" vlans somewhere...
686 for (i
=0; i
<state
->regs
->NUM_PORTS
; i
++) {
687 unsigned int portmask
= (1<<i
);
688 if (!state
->vlan_enabled
) {
689 // Share with everybody!
690 state
->ports
[i
].shareports
= (1<<state
->regs
->NUM_PORTS
)-1;
693 state
->ports
[i
].shareports
= portmask
;
694 for (j
=0; j
<MAX_VLANS
; j
++) {
695 if (state
->vlans
[j
].ports
& portmask
)
696 state
->ports
[i
].shareports
|= state
->vlans
[j
].ports
;
701 static int ip175c_update_state(struct ip175c_state
*state
)
703 ip175c_correct_vlan_state(state
);
704 return ip175c_set_state(state
);
707 static int ip175c_set_vlan_mode(struct ip175c_state
*state
)
709 return ip175c_update_state(state
);
712 static int ip175c_do_reset(struct ip175c_state
*state
)
716 if (REG_SUPP(state
->regs
->MODE_REG
)) {
717 err
= setPhy(state
, state
->regs
->MODE_REG
, state
->regs
->MODE_VAL
);
720 err
= getPhy(state
, state
->regs
->MODE_REG
);
728 static int ip175c_get_enable_vlan(struct switch_dev
*dev
, const struct switch_attr
*attr
, struct switch_val
*val
)
730 struct ip175c_state
*state
= dev
->priv
;
733 err
= state
->regs
->get_state(state
); // May be set in get_state.
736 val
->value
.i
= state
->vlan_enabled
;
740 static void ip175c_reset_vlan_config(struct ip175c_state
*state
)
744 state
->remove_tag
= (state
->vlan_enabled
? ((1<<state
->regs
->NUM_PORTS
)-1) : 0x0000);
745 state
->add_tag
= 0x0000;
746 for (i
= 0; i
< MAX_VLANS
; i
++)
747 state
->vlans
[i
].ports
= 0x0000;
748 for (i
= 0; i
< MAX_PORTS
; i
++)
749 state
->ports
[i
].pvid
= 0;
752 static int ip175c_set_enable_vlan(struct switch_dev
*dev
, const struct switch_attr
*attr
, struct switch_val
*val
)
754 struct ip175c_state
*state
= dev
->priv
;
758 err
= state
->regs
->get_state(state
);
761 enable
= val
->value
.i
;
763 if (state
->vlan_enabled
== enable
) {
764 // Do not change any state.
767 state
->vlan_enabled
= enable
;
769 // Otherwise, if we are switching state, set fields to a known default.
770 ip175c_reset_vlan_config(state
);
772 return state
->regs
->set_vlan_mode(state
);
775 static int ip175c_get_ports(struct switch_dev
*dev
, struct switch_val
*val
)
777 struct ip175c_state
*state
= dev
->priv
;
783 if (val
->port_vlan
>= dev
->vlans
|| val
->port_vlan
< 0)
786 err
= state
->regs
->get_state(state
);
790 ports
= state
->vlans
[val
->port_vlan
].ports
;
793 while (b
< MAX_PORTS
) {
795 int istagged
= ((state
->add_tag
>> b
) & 1);
796 val
->value
.ports
[ind
].id
= b
;
797 val
->value
.ports
[ind
].flags
= (istagged
<< SWITCH_PORT_FLAG_TAGGED
);
808 static int ip175c_set_ports(struct switch_dev
*dev
, struct switch_val
*val
)
810 struct ip175c_state
*state
= dev
->priv
;
814 if (val
->port_vlan
>= dev
->vlans
|| val
->port_vlan
< 0)
817 err
= state
->regs
->get_state(state
);
821 state
->vlans
[val
->port_vlan
].ports
= 0;
822 for (i
= 0; i
< val
->len
; i
++) {
823 unsigned int bitmask
= (1<<val
->value
.ports
[i
].id
);
824 state
->vlans
[val
->port_vlan
].ports
|= bitmask
;
825 if (val
->value
.ports
[i
].flags
& (1<<SWITCH_PORT_FLAG_TAGGED
)) {
826 state
->add_tag
|= bitmask
;
827 state
->remove_tag
&= (~bitmask
);
829 state
->add_tag
&= (~bitmask
);
830 state
->remove_tag
|= bitmask
;
834 return state
->regs
->update_state(state
);
837 static int ip175c_apply(struct switch_dev
*dev
)
839 struct ip175c_state
*state
= dev
->priv
;
842 err
= state
->regs
->get_flags(state
);
846 if (REG_SUPP(state
->regs
->MII_REGISTER_EN
)) {
847 int val
= getPhy(state
, state
->regs
->MII_REGISTER_EN
);
851 val
|= (1<<state
->regs
->MII_REGISTER_EN_BIT
);
852 return setPhy(state
, state
->regs
->MII_REGISTER_EN
, val
);
857 static int ip175c_reset(struct switch_dev
*dev
)
859 struct ip175c_state
*state
= dev
->priv
;
862 err
= state
->regs
->get_flags(state
);
866 if (REG_SUPP(state
->regs
->RESET_REG
)) {
867 err
= setPhy(state
, state
->regs
->RESET_REG
, state
->regs
->RESET_VAL
);
870 err
= getPhy(state
, state
->regs
->RESET_REG
);
873 * Data sheet specifies reset period to be 2 msec.
874 * (I don't see any mention of the 2ms delay in the IP178C spec, only
875 * in IP175C, but it can't hurt.)
880 /* reset switch ports */
881 for (i
= 0; i
< state
->regs
->NUM_PORTS
-1; i
++) {
882 err
= ip_phy_write(state
, i
, MII_BMCR
, BMCR_RESET
);
887 ip175c_reset_vlan_config(state
);
889 return state
->regs
->reset(state
);
892 static int ip175c_get_tagged(struct switch_dev
*dev
, const struct switch_attr
*attr
, struct switch_val
*val
)
894 struct ip175c_state
*state
= dev
->priv
;
897 err
= state
->regs
->get_state(state
);
901 if (state
->add_tag
& (1<<val
->port_vlan
)) {
902 if (state
->remove_tag
& (1<<val
->port_vlan
))
903 val
->value
.i
= 3; // shouldn't ever happen.
907 if (state
->remove_tag
& (1<<val
->port_vlan
))
915 static int ip175c_set_tagged(struct switch_dev
*dev
, const struct switch_attr
*attr
, struct switch_val
*val
)
917 struct ip175c_state
*state
= dev
->priv
;
920 err
= state
->regs
->get_state(state
);
924 state
->add_tag
&= ~(1<<val
->port_vlan
);
925 state
->remove_tag
&= ~(1<<val
->port_vlan
);
927 if (val
->value
.i
== 0)
928 state
->remove_tag
|= (1<<val
->port_vlan
);
929 if (val
->value
.i
== 1)
930 state
->add_tag
|= (1<<val
->port_vlan
);
932 return state
->regs
->update_state(state
);
935 /** Get the current phy address */
936 static int ip175c_get_phy(struct switch_dev
*dev
, const struct switch_attr
*attr
, struct switch_val
*val
)
938 struct ip175c_state
*state
= dev
->priv
;
940 val
->value
.i
= state
->proc_mii
.p
;
944 /** Set a new phy address for low level access to registers */
945 static int ip175c_set_phy(struct switch_dev
*dev
, const struct switch_attr
*attr
, struct switch_val
*val
)
947 struct ip175c_state
*state
= dev
->priv
;
948 int new_reg
= val
->value
.i
;
950 if (new_reg
< 0 || new_reg
> 31)
951 state
->proc_mii
.p
= (u16
)-1;
953 state
->proc_mii
.p
= (u16
)new_reg
;
957 /** Get the current register number */
958 static int ip175c_get_reg(struct switch_dev
*dev
, const struct switch_attr
*attr
, struct switch_val
*val
)
960 struct ip175c_state
*state
= dev
->priv
;
962 val
->value
.i
= state
->proc_mii
.m
;
966 /** Set a new register address for low level access to registers */
967 static int ip175c_set_reg(struct switch_dev
*dev
, const struct switch_attr
*attr
, struct switch_val
*val
)
969 struct ip175c_state
*state
= dev
->priv
;
970 int new_reg
= val
->value
.i
;
972 if (new_reg
< 0 || new_reg
> 31)
973 state
->proc_mii
.m
= (u16
)-1;
975 state
->proc_mii
.m
= (u16
)new_reg
;
979 /** Get the register content of state->proc_mii */
980 static int ip175c_get_val(struct switch_dev
*dev
, const struct switch_attr
*attr
, struct switch_val
*val
)
982 struct ip175c_state
*state
= dev
->priv
;
983 int retval
= -EINVAL
;
984 if (REG_SUPP(state
->proc_mii
))
985 retval
= getPhy(state
, state
->proc_mii
);
990 val
->value
.i
= retval
;
995 /** Write a value to the register defined by phy/reg above */
996 static int ip175c_set_val(struct switch_dev
*dev
, const struct switch_attr
*attr
, struct switch_val
*val
)
998 struct ip175c_state
*state
= dev
->priv
;
999 int myval
, err
= -EINVAL
;
1001 myval
= val
->value
.i
;
1002 if (myval
<= 0xffff && myval
>= 0 && REG_SUPP(state
->proc_mii
)) {
1003 err
= setPhy(state
, state
->proc_mii
, (u16
)myval
);
1008 static int ip175c_read_name(struct switch_dev
*dev
, const struct switch_attr
*attr
, struct switch_val
*val
)
1010 struct ip175c_state
*state
= dev
->priv
;
1011 val
->value
.s
= state
->regs
->NAME
; // Just a const pointer, won't be freed by swconfig.
1015 static int ip175c_set_port_speed(struct switch_dev
*dev
, const struct switch_attr
*attr
, struct switch_val
*val
)
1017 struct ip175c_state
*state
= dev
->priv
;
1018 int nr
= val
->port_vlan
;
1022 if (val
->value
.i
== 100) {
1025 } else if (val
->value
.i
== 10) {
1033 /* Can't set speed for cpu port */
1034 if (nr
== state
->regs
->CPU_PORT
)
1037 if (nr
>= dev
->ports
|| nr
< 0)
1040 ctrl
= ip_phy_read(state
, nr
, 0);
1046 ctrl
|= (autoneg
<<12);
1047 ctrl
|= (speed
<<13);
1049 return ip_phy_write(state
, nr
, 0, ctrl
);
1052 static int ip175c_get_port_speed(struct switch_dev
*dev
, const struct switch_attr
*attr
, struct switch_val
*val
)
1054 struct ip175c_state
*state
= dev
->priv
;
1055 int nr
= val
->port_vlan
;
1058 if (nr
== state
->regs
->CPU_PORT
) {
1063 if (nr
>= dev
->ports
|| nr
< 0)
1066 status
= ip_phy_read(state
, nr
, 1);
1067 speed
= ip_phy_read(state
, nr
, 18);
1068 if (status
< 0 || speed
< 0)
1072 val
->value
.i
= ((speed
& (1<<11)) ? 100 : 10);
1079 static int ip175c_get_port_status(struct switch_dev
*dev
, const struct switch_attr
*attr
, struct switch_val
*val
)
1081 struct ip175c_state
*state
= dev
->priv
;
1082 int ctrl
, speed
, status
;
1083 int nr
= val
->port_vlan
;
1085 char *buf
= state
->buf
; // fixed-length at 80.
1087 if (nr
== state
->regs
->CPU_PORT
) {
1088 sprintf(buf
, "up, 100 Mbps, cpu port");
1093 if (nr
>= dev
->ports
|| nr
< 0)
1096 ctrl
= ip_phy_read(state
, nr
, 0);
1097 status
= ip_phy_read(state
, nr
, 1);
1098 speed
= ip_phy_read(state
, nr
, 18);
1099 if (ctrl
< 0 || status
< 0 || speed
< 0)
1103 len
= sprintf(buf
, "up, %d Mbps, %s duplex",
1104 ((speed
& (1<<11)) ? 100 : 10),
1105 ((speed
& (1<<10)) ? "full" : "half"));
1107 len
= sprintf(buf
, "down");
1109 if (ctrl
& (1<<12)) {
1110 len
+= sprintf(buf
+len
, ", auto-negotiate");
1111 if (!(status
& (1<<5)))
1112 len
+= sprintf(buf
+len
, " (in progress)");
1114 len
+= sprintf(buf
+len
, ", fixed speed (%d)",
1115 ((ctrl
& (1<<13)) ? 100 : 10));
1123 static int ip175c_get_pvid(struct switch_dev
*dev
, int port
, int *val
)
1125 struct ip175c_state
*state
= dev
->priv
;
1127 *val
= state
->ports
[port
].pvid
;
1131 static int ip175c_set_pvid(struct switch_dev
*dev
, int port
, int val
)
1133 struct ip175c_state
*state
= dev
->priv
;
1136 if (val
< 0 || val
>= MAX_VLANS
)
1139 err
= state
->regs
->get_state(state
);
1143 state
->ports
[port
].pvid
= val
;
1145 return state
->regs
->update_state(state
);
1159 IP175C_REGISTER_PHY
,
1160 IP175C_REGISTER_MII
,
1161 IP175C_REGISTER_VALUE
,
1162 IP175C_REGISTER_ERRNO
,
1165 static const struct switch_attr ip175c_global
[] = {
1166 [IP175C_ENABLE_VLAN
] = {
1167 .id
= IP175C_ENABLE_VLAN
,
1168 .type
= SWITCH_TYPE_INT
,
1169 .name
= "enable_vlan",
1170 .description
= "Flag to enable or disable VLANs and tagging",
1171 .get
= ip175c_get_enable_vlan
,
1172 .set
= ip175c_set_enable_vlan
,
1174 [IP175C_GET_NAME
] = {
1175 .id
= IP175C_GET_NAME
,
1176 .type
= SWITCH_TYPE_STRING
,
1177 .description
= "Returns the type of IC+ chip.",
1179 .get
= ip175c_read_name
,
1182 /* jal: added for low level debugging etc. */
1183 [IP175C_REGISTER_PHY
] = {
1184 .id
= IP175C_REGISTER_PHY
,
1185 .type
= SWITCH_TYPE_INT
,
1186 .description
= "Direct register access: set PHY (0-4, or 29,30,31)",
1188 .get
= ip175c_get_phy
,
1189 .set
= ip175c_set_phy
,
1191 [IP175C_REGISTER_MII
] = {
1192 .id
= IP175C_REGISTER_MII
,
1193 .type
= SWITCH_TYPE_INT
,
1194 .description
= "Direct register access: set MII register number (0-31)",
1196 .get
= ip175c_get_reg
,
1197 .set
= ip175c_set_reg
,
1199 [IP175C_REGISTER_VALUE
] = {
1200 .id
= IP175C_REGISTER_VALUE
,
1201 .type
= SWITCH_TYPE_INT
,
1202 .description
= "Direct register access: read/write to register (0-65535)",
1204 .get
= ip175c_get_val
,
1205 .set
= ip175c_set_val
,
1209 static const struct switch_attr ip175c_vlan
[] = {
1212 static const struct switch_attr ip175c_port
[] = {
1213 [IP175C_PORT_STATUS
] = {
1214 .id
= IP175C_PORT_STATUS
,
1215 .type
= SWITCH_TYPE_STRING
,
1216 .description
= "Returns Detailed port status",
1218 .get
= ip175c_get_port_status
,
1221 [IP175C_PORT_LINK
] = {
1222 .id
= IP175C_PORT_LINK
,
1223 .type
= SWITCH_TYPE_INT
,
1224 .description
= "Link speed. Can write 0 for auto-negotiate, or 10 or 100",
1226 .get
= ip175c_get_port_speed
,
1227 .set
= ip175c_set_port_speed
,
1229 [IP175C_PORT_TAGGED
] = {
1230 .id
= IP175C_PORT_LINK
,
1231 .type
= SWITCH_TYPE_INT
,
1232 .description
= "0 = untag, 1 = add tags, 2 = do not alter (This value is reset if vlans are altered)",
1234 .get
= ip175c_get_tagged
,
1235 .set
= ip175c_set_tagged
,
1239 static int ip175c_probe(struct phy_device
*pdev
)
1241 struct ip175c_state
*state
;
1242 struct switch_dev
*dev
;
1245 /* We only attach to PHY 0, but use all available PHYs */
1246 if (pdev
->addr
!= 0)
1249 state
= kzalloc(sizeof(*state
), GFP_KERNEL
);
1254 dev
->attr_global
.attr
= ip175c_global
;
1255 dev
->attr_global
.n_attr
= ARRAY_SIZE(ip175c_global
);
1256 dev
->attr_port
.attr
= ip175c_port
;
1257 dev
->attr_port
.n_attr
= ARRAY_SIZE(ip175c_port
);
1258 dev
->attr_vlan
.attr
= ip175c_vlan
;
1259 dev
->attr_vlan
.n_attr
= ARRAY_SIZE(ip175c_vlan
);
1261 dev
->get_port_pvid
= ip175c_get_pvid
;
1262 dev
->set_port_pvid
= ip175c_set_pvid
;
1263 dev
->get_vlan_ports
= ip175c_get_ports
;
1264 dev
->set_vlan_ports
= ip175c_set_ports
;
1265 dev
->apply_config
= ip175c_apply
;
1266 dev
->reset_switch
= ip175c_reset
;
1270 state
->mii_bus
= pdev
->bus
;
1272 err
= get_model(state
);
1276 dev
->vlans
= MAX_VLANS
;
1277 dev
->cpu_port
= state
->regs
->CPU_PORT
;
1278 dev
->ports
= state
->regs
->NUM_PORTS
;
1279 dev
->name
= state
->regs
->NAME
;
1288 static int ip175c_config_init(struct phy_device
*pdev
)
1290 struct ip175c_state
*state
= pdev
->priv
;
1291 struct net_device
*dev
= pdev
->attached_dev
;
1294 pdev
->irq
= PHY_IGNORE_INTERRUPT
;
1295 err
= register_switch(&state
->dev
, dev
);
1299 ip175c_reset(&state
->dev
);
1301 state
->registered
= true;
1302 netif_carrier_on(pdev
->attached_dev
);
1307 static void ip175c_remove(struct phy_device
*pdev
)
1309 struct ip175c_state
*state
= pdev
->priv
;
1311 if (state
->registered
)
1312 unregister_switch(&state
->dev
);
1316 static int ip175c_config_aneg(struct phy_device
*pdev
)
1321 static int ip175c_read_status(struct phy_device
*pdev
)
1323 pdev
->speed
= SPEED_100
;
1324 pdev
->duplex
= DUPLEX_FULL
;
1325 pdev
->pause
= pdev
->asym_pause
= 0;
1329 static struct phy_driver ip175c_driver
= {
1330 .name
= "IC+ IP175C",
1331 .phy_id
= 0x02430d80,
1332 .phy_id_mask
= 0x0ffffff0,
1333 .features
= PHY_BASIC_FEATURES
,
1334 .probe
= ip175c_probe
,
1335 .remove
= ip175c_remove
,
1336 .config_init
= ip175c_config_init
,
1337 .config_aneg
= ip175c_config_aneg
,
1338 .read_status
= ip175c_read_status
,
1339 .driver
= { .owner
= THIS_MODULE
},
1342 static struct phy_driver ip175a_driver
= {
1343 .name
= "IC+ IP175A",
1344 .phy_id
= 0x02430c50,
1345 .phy_id_mask
= 0x0ffffff0,
1346 .features
= PHY_BASIC_FEATURES
,
1347 .probe
= ip175c_probe
,
1348 .remove
= ip175c_remove
,
1349 .config_init
= ip175c_config_init
,
1350 .config_aneg
= ip175c_config_aneg
,
1351 .read_status
= ip175c_read_status
,
1352 .driver
= { .owner
= THIS_MODULE
},
1356 int __init
ip175c_init(void)
1360 ret
= phy_driver_register(&ip175a_driver
);
1364 return phy_driver_register(&ip175c_driver
);
1367 void __exit
ip175c_exit(void)
1369 phy_driver_unregister(&ip175c_driver
);
1370 phy_driver_unregister(&ip175a_driver
);
1373 MODULE_AUTHOR("Patrick Horn <patrick.horn@gmail.com>");
1374 MODULE_AUTHOR("Felix Fietkau <nbd@openwrt.org>");
1375 MODULE_LICENSE("GPL");
1377 module_init(ip175c_init
);
1378 module_exit(ip175c_exit
);