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
;
699 state
->remove_tag
= ((~state
->add_tag
) & ((1<<state
->regs
->NUM_PORTS
)-1));
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
= 0x0000;
746 state
->add_tag
= 0x0000;
747 for (i
= 0; i
< MAX_VLANS
; i
++)
748 state
->vlans
[i
].ports
= 0x0000;
749 for (i
= 0; i
< MAX_PORTS
; i
++)
750 state
->ports
[i
].pvid
= 0;
753 static int ip175c_set_enable_vlan(struct switch_dev
*dev
, const struct switch_attr
*attr
, struct switch_val
*val
)
755 struct ip175c_state
*state
= dev
->priv
;
759 err
= state
->regs
->get_state(state
);
762 enable
= val
->value
.i
;
764 if (state
->vlan_enabled
== enable
) {
765 // Do not change any state.
768 state
->vlan_enabled
= enable
;
770 // Otherwise, if we are switching state, set fields to a known default.
771 ip175c_reset_vlan_config(state
);
773 return state
->regs
->set_vlan_mode(state
);
776 static int ip175c_get_ports(struct switch_dev
*dev
, struct switch_val
*val
)
778 struct ip175c_state
*state
= dev
->priv
;
784 if (val
->port_vlan
>= dev
->vlans
|| val
->port_vlan
< 0)
787 err
= state
->regs
->get_state(state
);
791 ports
= state
->vlans
[val
->port_vlan
].ports
;
794 while (b
< MAX_PORTS
) {
796 int istagged
= ((state
->add_tag
>> b
) & 1);
797 val
->value
.ports
[ind
].id
= b
;
798 val
->value
.ports
[ind
].flags
= (istagged
<< SWITCH_PORT_FLAG_TAGGED
);
809 static int ip175c_set_ports(struct switch_dev
*dev
, struct switch_val
*val
)
811 struct ip175c_state
*state
= dev
->priv
;
815 if (val
->port_vlan
>= dev
->vlans
|| val
->port_vlan
< 0)
818 err
= state
->regs
->get_state(state
);
822 state
->vlans
[val
->port_vlan
].ports
= 0;
823 for (i
= 0; i
< val
->len
; i
++) {
824 int bitmask
= (1<<val
->value
.ports
[i
].id
);
825 state
->vlans
[val
->port_vlan
].ports
|= bitmask
;
826 if (val
->value
.ports
[i
].flags
& (1<<SWITCH_PORT_FLAG_TAGGED
)) {
827 state
->add_tag
|= bitmask
;
829 state
->add_tag
&= (~bitmask
);
833 return state
->regs
->update_state(state
);
836 static int ip175c_apply(struct switch_dev
*dev
)
838 struct ip175c_state
*state
= dev
->priv
;
841 err
= state
->regs
->get_flags(state
);
845 if (REG_SUPP(state
->regs
->MII_REGISTER_EN
)) {
846 int val
= getPhy(state
, state
->regs
->MII_REGISTER_EN
);
850 val
|= (1<<state
->regs
->MII_REGISTER_EN_BIT
);
851 return setPhy(state
, state
->regs
->MII_REGISTER_EN
, val
);
856 static int ip175c_reset(struct switch_dev
*dev
)
858 struct ip175c_state
*state
= dev
->priv
;
861 err
= state
->regs
->get_flags(state
);
865 if (REG_SUPP(state
->regs
->RESET_REG
)) {
866 err
= setPhy(state
, state
->regs
->RESET_REG
, state
->regs
->RESET_VAL
);
869 err
= getPhy(state
, state
->regs
->RESET_REG
);
872 * Data sheet specifies reset period to be 2 msec.
873 * (I don't see any mention of the 2ms delay in the IP178C spec, only
874 * in IP175C, but it can't hurt.)
879 /* reset switch ports */
880 for (i
= 0; i
< state
->regs
->NUM_PORTS
-1; i
++) {
881 err
= ip_phy_write(state
, i
, MII_BMCR
, BMCR_RESET
);
886 ip175c_reset_vlan_config(state
);
888 return state
->regs
->reset(state
);
891 static int ip175c_get_tagged(struct switch_dev
*dev
, const struct switch_attr
*attr
, struct switch_val
*val
)
893 struct ip175c_state
*state
= dev
->priv
;
896 err
= state
->regs
->get_state(state
);
900 if (state
->add_tag
& (1<<val
->port_vlan
)) {
901 if (state
->remove_tag
& (1<<val
->port_vlan
))
902 val
->value
.i
= 3; // shouldn't ever happen.
906 if (state
->remove_tag
& (1<<val
->port_vlan
))
914 static int ip175c_set_tagged(struct switch_dev
*dev
, const struct switch_attr
*attr
, struct switch_val
*val
)
916 struct ip175c_state
*state
= dev
->priv
;
919 err
= state
->regs
->get_state(state
);
923 state
->add_tag
&= ~(1<<val
->port_vlan
);
924 state
->remove_tag
&= ~(1<<val
->port_vlan
);
926 if (val
->value
.i
== 0)
927 state
->remove_tag
|= (1<<val
->port_vlan
);
928 if (val
->value
.i
== 1)
929 state
->add_tag
|= (1<<val
->port_vlan
);
931 return state
->regs
->update_state(state
);
934 /** Get the current phy address */
935 static int ip175c_get_phy(struct switch_dev
*dev
, const struct switch_attr
*attr
, struct switch_val
*val
)
937 struct ip175c_state
*state
= dev
->priv
;
939 val
->value
.i
= state
->proc_mii
.p
;
943 /** Set a new phy address for low level access to registers */
944 static int ip175c_set_phy(struct switch_dev
*dev
, const struct switch_attr
*attr
, struct switch_val
*val
)
946 struct ip175c_state
*state
= dev
->priv
;
947 int new_reg
= val
->value
.i
;
949 if (new_reg
< 0 || new_reg
> 31)
950 state
->proc_mii
.p
= (u16
)-1;
952 state
->proc_mii
.p
= (u16
)new_reg
;
956 /** Get the current register number */
957 static int ip175c_get_reg(struct switch_dev
*dev
, const struct switch_attr
*attr
, struct switch_val
*val
)
959 struct ip175c_state
*state
= dev
->priv
;
961 val
->value
.i
= state
->proc_mii
.m
;
965 /** Set a new register address for low level access to registers */
966 static int ip175c_set_reg(struct switch_dev
*dev
, const struct switch_attr
*attr
, struct switch_val
*val
)
968 struct ip175c_state
*state
= dev
->priv
;
969 int new_reg
= val
->value
.i
;
971 if (new_reg
< 0 || new_reg
> 31)
972 state
->proc_mii
.m
= (u16
)-1;
974 state
->proc_mii
.m
= (u16
)new_reg
;
978 /** Get the register content of state->proc_mii */
979 static int ip175c_get_val(struct switch_dev
*dev
, const struct switch_attr
*attr
, struct switch_val
*val
)
981 struct ip175c_state
*state
= dev
->priv
;
982 int retval
= -EINVAL
;
983 if (REG_SUPP(state
->proc_mii
))
984 retval
= getPhy(state
, state
->proc_mii
);
989 val
->value
.i
= retval
;
994 /** Write a value to the register defined by phy/reg above */
995 static int ip175c_set_val(struct switch_dev
*dev
, const struct switch_attr
*attr
, struct switch_val
*val
)
997 struct ip175c_state
*state
= dev
->priv
;
998 int myval
, err
= -EINVAL
;
1000 myval
= val
->value
.i
;
1001 if (myval
<= 0xffff && myval
>= 0 && REG_SUPP(state
->proc_mii
)) {
1002 err
= setPhy(state
, state
->proc_mii
, (u16
)myval
);
1007 static int ip175c_read_name(struct switch_dev
*dev
, const struct switch_attr
*attr
, struct switch_val
*val
)
1009 struct ip175c_state
*state
= dev
->priv
;
1010 val
->value
.s
= state
->regs
->NAME
; // Just a const pointer, won't be freed by swconfig.
1014 static int ip175c_set_port_speed(struct switch_dev
*dev
, const struct switch_attr
*attr
, struct switch_val
*val
)
1016 struct ip175c_state
*state
= dev
->priv
;
1017 int nr
= val
->port_vlan
;
1021 if (val
->value
.i
== 100) {
1024 } else if (val
->value
.i
== 10) {
1032 /* Can't set speed for cpu port */
1033 if (nr
== state
->regs
->CPU_PORT
)
1036 if (nr
>= dev
->ports
|| nr
< 0)
1039 ctrl
= ip_phy_read(state
, nr
, 0);
1045 ctrl
|= (autoneg
<<12);
1046 ctrl
|= (speed
<<13);
1048 return ip_phy_write(state
, nr
, 0, ctrl
);
1051 static int ip175c_get_port_speed(struct switch_dev
*dev
, const struct switch_attr
*attr
, struct switch_val
*val
)
1053 struct ip175c_state
*state
= dev
->priv
;
1054 int nr
= val
->port_vlan
;
1057 if (nr
== state
->regs
->CPU_PORT
) {
1062 if (nr
>= dev
->ports
|| nr
< 0)
1065 status
= ip_phy_read(state
, nr
, 1);
1066 speed
= ip_phy_read(state
, nr
, 18);
1067 if (status
< 0 || speed
< 0)
1071 val
->value
.i
= ((speed
& (1<<11)) ? 100 : 10);
1078 static int ip175c_get_port_status(struct switch_dev
*dev
, const struct switch_attr
*attr
, struct switch_val
*val
)
1080 struct ip175c_state
*state
= dev
->priv
;
1081 int ctrl
, speed
, status
;
1082 int nr
= val
->port_vlan
;
1084 char *buf
= state
->buf
; // fixed-length at 80.
1086 if (nr
== state
->regs
->CPU_PORT
) {
1087 sprintf(buf
, "up, 100 Mbps, cpu port");
1092 if (nr
>= dev
->ports
|| nr
< 0)
1095 ctrl
= ip_phy_read(state
, nr
, 0);
1096 status
= ip_phy_read(state
, nr
, 1);
1097 speed
= ip_phy_read(state
, nr
, 18);
1098 if (ctrl
< 0 || status
< 0 || speed
< 0)
1102 len
= sprintf(buf
, "up, %d Mbps, %s duplex",
1103 ((speed
& (1<<11)) ? 100 : 10),
1104 ((speed
& (1<<10)) ? "full" : "half"));
1106 len
= sprintf(buf
, "down");
1108 if (ctrl
& (1<<12)) {
1109 len
+= sprintf(buf
+len
, ", auto-negotiate");
1110 if (!(status
& (1<<5)))
1111 len
+= sprintf(buf
+len
, " (in progress)");
1113 len
+= sprintf(buf
+len
, ", fixed speed (%d)",
1114 ((ctrl
& (1<<13)) ? 100 : 10));
1122 static int ip175c_get_pvid(struct switch_dev
*dev
, int port
, int *val
)
1124 struct ip175c_state
*state
= dev
->priv
;
1126 *val
= state
->ports
[port
].pvid
;
1130 static int ip175c_set_pvid(struct switch_dev
*dev
, int port
, int val
)
1132 struct ip175c_state
*state
= dev
->priv
;
1135 if (val
< 0 || val
>= MAX_VLANS
)
1138 err
= state
->regs
->get_state(state
);
1142 state
->ports
[port
].pvid
= val
;
1144 return state
->regs
->update_state(state
);
1158 IP175C_REGISTER_PHY
,
1159 IP175C_REGISTER_MII
,
1160 IP175C_REGISTER_VALUE
,
1161 IP175C_REGISTER_ERRNO
,
1164 static const struct switch_attr ip175c_global
[] = {
1165 [IP175C_ENABLE_VLAN
] = {
1166 .id
= IP175C_ENABLE_VLAN
,
1167 .type
= SWITCH_TYPE_INT
,
1168 .name
= "enable_vlan",
1169 .description
= "Flag to enable or disable VLANs and tagging",
1170 .get
= ip175c_get_enable_vlan
,
1171 .set
= ip175c_set_enable_vlan
,
1173 [IP175C_GET_NAME
] = {
1174 .id
= IP175C_GET_NAME
,
1175 .type
= SWITCH_TYPE_STRING
,
1176 .description
= "Returns the type of IC+ chip.",
1178 .get
= ip175c_read_name
,
1181 /* jal: added for low level debugging etc. */
1182 [IP175C_REGISTER_PHY
] = {
1183 .id
= IP175C_REGISTER_PHY
,
1184 .type
= SWITCH_TYPE_INT
,
1185 .description
= "Direct register access: set PHY (0-4, or 29,30,31)",
1187 .get
= ip175c_get_phy
,
1188 .set
= ip175c_set_phy
,
1190 [IP175C_REGISTER_MII
] = {
1191 .id
= IP175C_REGISTER_MII
,
1192 .type
= SWITCH_TYPE_INT
,
1193 .description
= "Direct register access: set MII register number (0-31)",
1195 .get
= ip175c_get_reg
,
1196 .set
= ip175c_set_reg
,
1198 [IP175C_REGISTER_VALUE
] = {
1199 .id
= IP175C_REGISTER_VALUE
,
1200 .type
= SWITCH_TYPE_INT
,
1201 .description
= "Direct register access: read/write to register (0-65535)",
1203 .get
= ip175c_get_val
,
1204 .set
= ip175c_set_val
,
1208 static const struct switch_attr ip175c_vlan
[] = {
1211 static const struct switch_attr ip175c_port
[] = {
1212 [IP175C_PORT_STATUS
] = {
1213 .id
= IP175C_PORT_STATUS
,
1214 .type
= SWITCH_TYPE_STRING
,
1215 .description
= "Returns Detailed port status",
1217 .get
= ip175c_get_port_status
,
1220 [IP175C_PORT_LINK
] = {
1221 .id
= IP175C_PORT_LINK
,
1222 .type
= SWITCH_TYPE_INT
,
1223 .description
= "Link speed. Can write 0 for auto-negotiate, or 10 or 100",
1225 .get
= ip175c_get_port_speed
,
1226 .set
= ip175c_set_port_speed
,
1228 [IP175C_PORT_TAGGED
] = {
1229 .id
= IP175C_PORT_LINK
,
1230 .type
= SWITCH_TYPE_INT
,
1231 .description
= "0 = untag, 1 = add tags, 2 = do not alter (This value is reset if vlans are altered)",
1233 .get
= ip175c_get_tagged
,
1234 .set
= ip175c_set_tagged
,
1238 static int ip175c_probe(struct phy_device
*pdev
)
1240 struct ip175c_state
*state
;
1241 struct switch_dev
*dev
;
1244 /* We only attach to PHY 0, but use all available PHYs */
1245 if (pdev
->addr
!= 0)
1248 state
= kzalloc(sizeof(*state
), GFP_KERNEL
);
1253 dev
->attr_global
.attr
= ip175c_global
;
1254 dev
->attr_global
.n_attr
= ARRAY_SIZE(ip175c_global
);
1255 dev
->attr_port
.attr
= ip175c_port
;
1256 dev
->attr_port
.n_attr
= ARRAY_SIZE(ip175c_port
);
1257 dev
->attr_vlan
.attr
= ip175c_vlan
;
1258 dev
->attr_vlan
.n_attr
= ARRAY_SIZE(ip175c_vlan
);
1260 dev
->get_port_pvid
= ip175c_get_pvid
;
1261 dev
->set_port_pvid
= ip175c_set_pvid
;
1262 dev
->get_vlan_ports
= ip175c_get_ports
;
1263 dev
->set_vlan_ports
= ip175c_set_ports
;
1264 dev
->apply_config
= ip175c_apply
;
1265 dev
->reset_switch
= ip175c_reset
;
1269 state
->mii_bus
= pdev
->bus
;
1271 err
= get_model(state
);
1275 dev
->vlans
= MAX_VLANS
;
1276 dev
->cpu_port
= state
->regs
->CPU_PORT
;
1277 dev
->ports
= state
->regs
->NUM_PORTS
;
1278 dev
->name
= state
->regs
->NAME
;
1287 static int ip175c_config_init(struct phy_device
*pdev
)
1289 struct ip175c_state
*state
= pdev
->priv
;
1290 struct net_device
*dev
= pdev
->attached_dev
;
1293 pdev
->irq
= PHY_IGNORE_INTERRUPT
;
1294 err
= register_switch(&state
->dev
, dev
);
1298 ip175c_reset(&state
->dev
);
1300 state
->registered
= true;
1301 netif_carrier_on(pdev
->attached_dev
);
1306 static void ip175c_remove(struct phy_device
*pdev
)
1308 struct ip175c_state
*state
= pdev
->priv
;
1310 if (state
->registered
)
1311 unregister_switch(&state
->dev
);
1315 static int ip175c_config_aneg(struct phy_device
*pdev
)
1320 static int ip175c_read_status(struct phy_device
*pdev
)
1322 pdev
->speed
= SPEED_100
;
1323 pdev
->duplex
= DUPLEX_FULL
;
1324 pdev
->pause
= pdev
->asym_pause
= 0;
1328 static struct phy_driver ip175c_driver
= {
1329 .name
= "IC+ IP175C",
1330 .phy_id
= 0x02430d80,
1331 .phy_id_mask
= 0x0ffffff0,
1332 .features
= PHY_BASIC_FEATURES
,
1333 .probe
= ip175c_probe
,
1334 .remove
= ip175c_remove
,
1335 .config_init
= ip175c_config_init
,
1336 .config_aneg
= ip175c_config_aneg
,
1337 .read_status
= ip175c_read_status
,
1338 .driver
= { .owner
= THIS_MODULE
},
1341 static struct phy_driver ip175a_driver
= {
1342 .name
= "IC+ IP175A",
1343 .phy_id
= 0x02430c50,
1344 .phy_id_mask
= 0x0ffffff0,
1345 .features
= PHY_BASIC_FEATURES
,
1346 .probe
= ip175c_probe
,
1347 .remove
= ip175c_remove
,
1348 .config_init
= ip175c_config_init
,
1349 .config_aneg
= ip175c_config_aneg
,
1350 .read_status
= ip175c_read_status
,
1351 .driver
= { .owner
= THIS_MODULE
},
1355 int __init
ip175c_init(void)
1359 ret
= phy_driver_register(&ip175a_driver
);
1363 return phy_driver_register(&ip175c_driver
);
1366 void __exit
ip175c_exit(void)
1368 phy_driver_unregister(&ip175c_driver
);
1369 phy_driver_unregister(&ip175a_driver
);
1372 MODULE_AUTHOR("Patrick Horn <patrick.horn@gmail.com>");
1373 MODULE_AUTHOR("Felix Fietkau <nbd@openwrt.org>");
1374 MODULE_LICENSE("GPL");
1376 module_init(ip175c_init
);
1377 module_exit(ip175c_exit
);