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>
32 typedef struct ip175c_reg
{
38 #define NOTSUPPORTED {-1,-1}
40 #define REG_SUPP(x) (((x).m != ((u16)-1)) && ((x).p != (u16)-1))
42 /*********** CONSTANTS ***********/
43 struct register_mappings
{
45 u16 MODEL_NO
; // compare to bits 4-9 of MII register 0,3.
49 /* The default VLAN for each port.
50 Default: 0x0001 for Ports 0,1,2,3
51 0x0002 for Ports 4,5 */
52 reg VLAN_DEFAULT_TAG_REG
[MAX_PORTS
];
54 /* These ports are tagged.
58 bitnum ADD_TAG_BIT
[MAX_PORTS
];
59 /* These ports are untagged.
60 Default: 0x00 (i.e. do not alter any VLAN tags...)
61 Maybe set to 0 if user disables VLANs. */
62 bitnum REMOVE_TAG_BIT
[MAX_PORTS
];
64 /* Port M and Port N are on the same VLAN.
65 Default: All ports on all VLANs. */
66 // Use register {29, 19+N/2}
68 // Port 5 uses register {30, 18} but same as odd bits.
69 reg VLAN_LOOKUP_REG_5
; // in a different register on IP175C.
70 bitnum VLAN_LOOKUP_EVEN_BIT
[MAX_PORTS
];
71 bitnum VLAN_LOOKUP_ODD_BIT
[MAX_PORTS
];
73 /* This VLAN corresponds to which ports.
74 Default: 0x2f,0x30,0x3f,0x3f... */
75 reg TAG_VLAN_MASK_REG
;
76 bitnum TAG_VLAN_MASK_EVEN_BIT
[MAX_PORTS
];
77 bitnum TAG_VLAN_MASK_ODD_BIT
[MAX_PORTS
];
86 reg ROUTER_CONTROL_REG
;
90 bitnum NUMLAN_GROUPS_MAX
;
91 bitnum NUMLAN_GROUPS_BIT
;
94 bitnum MII_REGISTER_EN_BIT
;
96 // set to 1 for 178C, 0 for 175C.
97 bitnum SIMPLE_VLAN_REGISTERS
; // 175C has two vlans per register but 178C has only one.
100 static const struct register_mappings IP178C
= {
103 .VLAN_DEFAULT_TAG_REG
= {
104 {30,3},{30,4},{30,5},{30,6},{30,7},{30,8},
105 {30,9},{30,10},{30,11},
108 .ADD_TAG_REG
= {30,12},
109 .ADD_TAG_BIT
= {0,1,2,3,4,5,6,7,8},
110 .REMOVE_TAG_REG
= {30,13},
111 .REMOVE_TAG_BIT
= {4,5,6,7,8,9,10,11,12},
113 .SIMPLE_VLAN_REGISTERS
= 1,
115 .VLAN_LOOKUP_REG
= {31,0},// +N
116 .VLAN_LOOKUP_REG_5
= NOTSUPPORTED
, // not used with SIMPLE_VLAN_REGISTERS
117 .VLAN_LOOKUP_EVEN_BIT
= {0,1,2,3,4,5,6,7,8},
118 .VLAN_LOOKUP_ODD_BIT
= {0,1,2,3,4,5,6,7,8},
120 .TAG_VLAN_MASK_REG
= {30,14}, // +N
121 .TAG_VLAN_MASK_EVEN_BIT
= {0,1,2,3,4,5,6,7,8},
122 .TAG_VLAN_MASK_ODD_BIT
= {0,1,2,3,4,5,6,7,8},
127 .MODE_REG
= NOTSUPPORTED
,
129 .ROUTER_CONTROL_REG
= {30,30},
131 .NUMLAN_GROUPS_MAX
= 8,
132 .NUMLAN_GROUPS_BIT
= 8, // {0-2}
134 .VLAN_CONTROL_REG
= {30,13},
140 .MII_REGISTER_EN
= NOTSUPPORTED
,
144 static const struct register_mappings IP175C
= {
147 .VLAN_DEFAULT_TAG_REG
= {
148 {29,24},{29,25},{29,26},{29,27},{29,28},{29,30},
149 NOTSUPPORTED
,NOTSUPPORTED
,NOTSUPPORTED
152 .ADD_TAG_REG
= {29,23},
153 .REMOVE_TAG_REG
= {29,23},
154 .ADD_TAG_BIT
= {11,12,13,14,15,1,-1,-1,-1},
155 .REMOVE_TAG_BIT
= {6,7,8,9,10,0,-1,-1,-1},
157 .SIMPLE_VLAN_REGISTERS
= 0,
159 .VLAN_LOOKUP_REG
= {29,19},// +N/2
160 .VLAN_LOOKUP_REG_5
= {30,18},
161 .VLAN_LOOKUP_EVEN_BIT
= {8,9,10,11,12,15,-1,-1,-1},
162 .VLAN_LOOKUP_ODD_BIT
= {0,1,2,3,4,7,-1,-1,-1},
164 .TAG_VLAN_MASK_REG
= {30,1}, // +N/2
165 .TAG_VLAN_MASK_EVEN_BIT
= {0,1,2,3,4,5,-1,-1,-1},
166 .TAG_VLAN_MASK_ODD_BIT
= {8,9,10,11,12,13,-1,-1,-1},
173 .ROUTER_CONTROL_REG
= {30,9},
175 .NUMLAN_GROUPS_MAX
= 8,
176 .NUMLAN_GROUPS_BIT
= 0, // {0-2}
178 .VLAN_CONTROL_REG
= {30,9},
184 .MII_REGISTER_EN
= NOTSUPPORTED
,
188 static const struct register_mappings IP175A
= {
191 .VLAN_DEFAULT_TAG_REG
= {
192 {0,24},{0,25},{0,26},{0,27},{0,28},NOTSUPPORTED
,
193 NOTSUPPORTED
,NOTSUPPORTED
,NOTSUPPORTED
196 .ADD_TAG_REG
= {0,23},
197 .REMOVE_TAG_REG
= {0,23},
198 .ADD_TAG_BIT
= {11,12,13,14,15,1,-1,-1,-1},
199 .REMOVE_TAG_BIT
= {6,7,8,9,10,0,-1,-1,-1},
201 .SIMPLE_VLAN_REGISTERS
= 1,
203 // Only programmable via. EEPROM
204 .VLAN_LOOKUP_REG
= NOTSUPPORTED
,// +N/2
205 .VLAN_LOOKUP_REG_5
= NOTSUPPORTED
,
206 .VLAN_LOOKUP_EVEN_BIT
= {8,9,10,11,12,15,-1,-1,-1},
207 .VLAN_LOOKUP_ODD_BIT
= {0,1,2,3,4,7,-1,-1,-1},
209 .TAG_VLAN_MASK_REG
= NOTSUPPORTED
, // +N/2
210 .TAG_VLAN_MASK_EVEN_BIT
= {0,1,2,3,4,5,-1,-1,-1},
211 .TAG_VLAN_MASK_ODD_BIT
= {8,9,10,11,12,13,-1,-1,-1},
214 .RESET_REG
= NOTSUPPORTED
,
216 .MODE_REG
= NOTSUPPORTED
,
218 .ROUTER_CONTROL_REG
= NOTSUPPORTED
,
219 .VLAN_CONTROL_REG
= NOTSUPPORTED
,
222 .NUMLAN_GROUPS_MAX
= -1,
223 .NUMLAN_GROUPS_BIT
= -1, // {0-2}
228 .MII_REGISTER_EN
= {0, 12},
229 .MII_REGISTER_EN_BIT
= 7,
232 struct ip175c_state
{
233 struct switch_dev dev
;
234 struct mii_bus
*mii_bus
;
237 int router_mode
; // ROUTER_EN
238 int vlan_enabled
; // TAG_VLAN_EN
241 unsigned int shareports
;
243 unsigned int add_tag
;
244 unsigned int remove_tag
;
246 unsigned int vlan_ports
[MAX_VLANS
];
247 const struct register_mappings
*regs
;
248 reg proc_mii
; /*!< phy/reg for the low level register access via /proc */
253 static int ip_phy_read(struct mii_bus
*bus
, int port
, int reg
)
257 mutex_lock(&bus
->mdio_lock
);
258 val
= bus
->read(bus
, port
, reg
);
259 mutex_unlock(&bus
->mdio_lock
);
265 static int ip_phy_write(struct mii_bus
*bus
, int port
, int reg
, u16 val
)
269 mutex_lock(&bus
->mdio_lock
);
270 err
= bus
->write(bus
, port
, reg
, val
);
271 mutex_unlock(&bus
->mdio_lock
);
277 static int getPhy (struct ip175c_state
*state
, reg mii
)
279 struct mii_bus
*bus
= state
->mii_bus
;
285 val
= ip_phy_read(bus
, mii
.p
, mii
.m
);
287 pr_warning("IP175C: Unable to get MII register %d,%d: error %d\n", mii
.p
,mii
.m
,-val
);
292 static int setPhy (struct ip175c_state
*state
, reg mii
, u16 value
)
294 struct mii_bus
*bus
= state
->mii_bus
;
300 err
= ip_phy_write(bus
, mii
.p
, mii
.m
, value
);
302 pr_warning("IP175C: Unable to set MII register %d,%d to %d: error %d\n", mii
.p
,mii
.m
,value
,-err
);
311 * These two macros are to simplify the mapping of logical bits to the bits in hardware.
312 * NOTE: these macros will return if there is an error!
315 #define GET_PORT_BITS(state, bits, addr, bit_lookup) \
317 int i, val = getPhy((state), (addr)); \
321 for (i = 0; i < MAX_PORTS; i++) { \
322 if ((bit_lookup)[i] == -1) continue; \
323 if (val & (1<<(bit_lookup)[i])) \
328 #define SET_PORT_BITS(state, bits, addr, bit_lookup) \
330 int i, val = getPhy((state), (addr)); \
333 for (i = 0; i < MAX_PORTS; i++) { \
334 unsigned int newmask = ((bits)&(1<<i)); \
335 if ((bit_lookup)[i] == -1) continue; \
336 val &= ~(1<<(bit_lookup)[i]); \
337 val |= ((newmask>>i)<<(bit_lookup)[i]); \
339 val = setPhy((state), (addr), val); \
344 static int get_model(struct ip175c_state
*state
)
346 reg oui_id_reg
= {0, 2};
348 reg model_no_reg
= {0, 3};
349 int model_no
, model_no_orig
;
351 // 175 and 178 have the same oui ID.
352 reg oui_id_reg_178c
= {5, 2}; // returns error on IP175C.
355 oui_id
= getPhy(state
, oui_id_reg
);
356 if (oui_id
!= 0x0243) {
358 return -ENODEV
; // Not a IC+ chip.
360 oui_id
= getPhy(state
, oui_id_reg_178c
);
361 if (oui_id
== 0x0243) {
365 model_no_orig
= getPhy(state
, model_no_reg
);
366 if (model_no_orig
< 0) {
369 model_no
= model_no_orig
>> 4; // shift out revision number.
370 model_no
&= 0x3f; // only take the model number (low 6 bits).
371 if (model_no
== IP175A
.MODEL_NO
) {
372 state
->regs
= &IP175A
;
373 } else if (model_no
== IP175C
.MODEL_NO
) {
375 state
->regs
= &IP178C
;
377 state
->regs
= &IP175C
;
380 printk(KERN_WARNING
"ip175c: Found an unknown IC+ switch with model number %02Xh.\n", model_no_orig
);
386 /** Get only the vlan and router flags on the router **/
387 static int get_flags(struct ip175c_state
*state
)
391 state
->router_mode
= 0;
392 state
->vlan_enabled
= -1; // hack
393 state
->num_vlans
= 0;
395 if (!REG_SUPP(state
->regs
->ROUTER_CONTROL_REG
)) {
396 return 0; // not an error if it doesn't support enable vlan.
399 val
= getPhy(state
, state
->regs
->ROUTER_CONTROL_REG
);
403 if (state
->regs
->ROUTER_EN_BIT
>= 0)
404 state
->router_mode
= ((val
>>state
->regs
->ROUTER_EN_BIT
) & 1);
406 if (state
->regs
->NUMLAN_GROUPS_BIT
>= 0) {
407 state
->num_vlans
= (val
>> state
->regs
->NUMLAN_GROUPS_BIT
);
408 state
->num_vlans
&= (state
->regs
->NUMLAN_GROUPS_MAX
-1);
409 state
->num_vlans
+=1; // does not include WAN.
413 val
= getPhy(state
, state
->regs
->VLAN_CONTROL_REG
);
417 if (state
->regs
->TAG_VLAN_BIT
>= 0)
418 state
->vlan_enabled
= ((val
>>state
->regs
->TAG_VLAN_BIT
) & 1);
422 /** Get all state variables for VLAN mappings and port-based tagging. **/
423 static int get_state(struct ip175c_state
*state
)
427 ret
= get_flags(state
);
431 GET_PORT_BITS(state
, state
->remove_tag
,
432 state
->regs
->REMOVE_TAG_REG
, state
->regs
->REMOVE_TAG_BIT
);
433 GET_PORT_BITS(state
, state
->add_tag
,
434 state
->regs
->ADD_TAG_REG
, state
->regs
->ADD_TAG_BIT
);
436 if (state
->vlan_enabled
== -1) {
437 // not sure how to get this...
438 state
->vlan_enabled
= (!state
->remove_tag
&& !state
->add_tag
);
441 if (REG_SUPP(state
->regs
->VLAN_LOOKUP_REG
)) {
442 for (j
=0; j
<MAX_PORTS
; j
++) {
443 state
->ports
[j
].shareports
= 0; // initialize them in case.
445 for (j
=0; j
<state
->regs
->NUM_PORTS
; j
++) {
447 const bitnum
*bit_lookup
= (j
%2==0)?
448 state
->regs
->VLAN_LOOKUP_EVEN_BIT
:
449 state
->regs
->VLAN_LOOKUP_ODD_BIT
;
450 addr
= state
->regs
->VLAN_LOOKUP_REG
;
451 if (state
->regs
->SIMPLE_VLAN_REGISTERS
) {
466 addr
= state
->regs
->VLAN_LOOKUP_REG_5
;
471 if (REG_SUPP(addr
)) {
472 GET_PORT_BITS(state
, state
->ports
[j
].shareports
, addr
, bit_lookup
);
476 for (j
=0; j
<MAX_PORTS
; j
++) {
477 state
->ports
[j
].shareports
= 0xff;
481 for (i
=0; i
<MAX_PORTS
; i
++) {
482 if (REG_SUPP(state
->regs
->VLAN_DEFAULT_TAG_REG
[i
])) {
483 int val
= getPhy(state
, state
->regs
->VLAN_DEFAULT_TAG_REG
[i
]);
487 state
->ports
[i
].pvid
= val
;
489 state
->ports
[i
].pvid
= 0;
493 if (REG_SUPP(state
->regs
->TAG_VLAN_MASK_REG
)) {
494 for (j
=0; j
<MAX_VLANS
; j
++) {
495 reg addr
= state
->regs
->TAG_VLAN_MASK_REG
;
496 const bitnum
*bit_lookup
= (j
%2==0)?
497 state
->regs
->TAG_VLAN_MASK_EVEN_BIT
:
498 state
->regs
->TAG_VLAN_MASK_ODD_BIT
;
499 if (state
->regs
->SIMPLE_VLAN_REGISTERS
) {
504 GET_PORT_BITS(state
, state
->vlan_ports
[j
], addr
, bit_lookup
);
507 for (j
=0; j
<MAX_VLANS
; j
++) {
508 state
->vlan_ports
[j
] = 0;
509 for (i
=0; i
<state
->regs
->NUM_PORTS
; i
++) {
510 if ((state
->ports
[i
].pvid
== j
) ||
511 (state
->ports
[i
].pvid
== 0)) {
512 state
->vlan_ports
[j
] |= (1<<i
);
522 /** Only update vlan and router flags in the switch **/
523 static int update_flags(struct ip175c_state
*state
)
527 if (!REG_SUPP(state
->regs
->ROUTER_CONTROL_REG
)) {
531 val
= getPhy(state
, state
->regs
->ROUTER_CONTROL_REG
);
535 if (state
->regs
->ROUTER_EN_BIT
>= 0) {
536 if (state
->router_mode
) {
537 val
|= (1<<state
->regs
->ROUTER_EN_BIT
);
539 val
&= (~(1<<state
->regs
->ROUTER_EN_BIT
));
542 if (state
->regs
->TAG_VLAN_BIT
>= 0) {
543 if (state
->vlan_enabled
) {
544 val
|= (1<<state
->regs
->TAG_VLAN_BIT
);
546 val
&= (~(1<<state
->regs
->TAG_VLAN_BIT
));
549 if (state
->regs
->NUMLAN_GROUPS_BIT
>= 0) {
550 val
&= (~((state
->regs
->NUMLAN_GROUPS_MAX
-1)<<state
->regs
->NUMLAN_GROUPS_BIT
));
551 if (state
->num_vlans
> state
->regs
->NUMLAN_GROUPS_MAX
) {
552 val
|= state
->regs
->NUMLAN_GROUPS_MAX
<< state
->regs
->NUMLAN_GROUPS_BIT
;
553 } else if (state
->num_vlans
>= 1) {
554 val
|= (state
->num_vlans
-1) << state
->regs
->NUMLAN_GROUPS_BIT
;
557 return setPhy(state
, state
->regs
->ROUTER_CONTROL_REG
, val
);
560 /** Update all VLAN and port state. Usually you should call "correct_vlan_state" first. **/
561 static int update_state(struct ip175c_state
*state
)
565 SET_PORT_BITS(state
, state
->add_tag
,
566 state
->regs
->ADD_TAG_REG
, state
->regs
->ADD_TAG_BIT
);
567 SET_PORT_BITS(state
, state
->remove_tag
,
568 state
->regs
->REMOVE_TAG_REG
, state
->regs
->REMOVE_TAG_BIT
);
570 if (REG_SUPP(state
->regs
->VLAN_LOOKUP_REG
)) {
571 for (j
=0; j
<state
->regs
->NUM_PORTS
; j
++) {
573 const bitnum
*bit_lookup
= (j
%2==0)?
574 state
->regs
->VLAN_LOOKUP_EVEN_BIT
:
575 state
->regs
->VLAN_LOOKUP_ODD_BIT
;
577 // duplicate code -- sorry
578 addr
= state
->regs
->VLAN_LOOKUP_REG
;
579 if (state
->regs
->SIMPLE_VLAN_REGISTERS
) {
594 addr
= state
->regs
->VLAN_LOOKUP_REG_5
;
597 addr
.m
= -1; // shouldn't get here, but...
601 //printf("shareports for %d is %02X\n",j,state->ports[j].shareports);
602 if (REG_SUPP(addr
)) {
603 SET_PORT_BITS(state
, state
->ports
[j
].shareports
, addr
, bit_lookup
);
607 if (REG_SUPP(state
->regs
->TAG_VLAN_MASK_REG
)) {
608 for (j
=0; j
<MAX_VLANS
; j
++) {
609 reg addr
= state
->regs
->TAG_VLAN_MASK_REG
;
610 const bitnum
*bit_lookup
= (j
%2==0)?
611 state
->regs
->TAG_VLAN_MASK_EVEN_BIT
:
612 state
->regs
->TAG_VLAN_MASK_ODD_BIT
;
613 unsigned int vlan_mask
;
614 if (state
->regs
->SIMPLE_VLAN_REGISTERS
) {
619 vlan_mask
= state
->vlan_ports
[j
];
620 SET_PORT_BITS(state
, vlan_mask
, addr
, bit_lookup
);
624 for (i
=0; i
<MAX_PORTS
; i
++) {
625 if (REG_SUPP(state
->regs
->VLAN_DEFAULT_TAG_REG
[i
])) {
626 int err
= setPhy(state
, state
->regs
->VLAN_DEFAULT_TAG_REG
[i
],
627 state
->ports
[i
].pvid
);
634 return update_flags(state
);
636 // software reset: 30.0 = 0x175C
638 // reset ports 0,1,2,3,4
642 Uses only the VLAN port mask and the add tag mask to generate the other fields:
643 which ports are part of the same VLAN, removing vlan tags, and VLAN tag ids.
645 static void correct_vlan_state(struct ip175c_state
*state
)
648 state
->num_vlans
= 0;
649 for (i
=0; i
<MAX_VLANS
; i
++) {
650 if (state
->vlan_ports
[i
] != 0) {
651 state
->num_vlans
= i
+1; //hack -- we need to store the "set" vlans somewhere...
657 for (i
=0; i
<state
->regs
->NUM_PORTS
; i
++) {
658 unsigned int portmask
= (1<<i
);
659 if (!state
->vlan_enabled
) {
660 // share with everybody!
661 state
->ports
[i
].shareports
= (1<<state
->regs
->NUM_PORTS
)-1;
664 state
->ports
[i
].shareports
= portmask
;
665 for (j
=0; j
<MAX_VLANS
; j
++) {
666 if (state
->vlan_ports
[j
] & portmask
)
667 state
->ports
[i
].shareports
|= state
->vlan_ports
[j
];
670 state
->remove_tag
= ((~state
->add_tag
) & ((1<<state
->regs
->NUM_PORTS
)-1));
673 static int ip175c_get_enable_vlan(struct switch_dev
*dev
, const struct switch_attr
*attr
, struct switch_val
*val
)
675 struct ip175c_state
*state
= dev
->priv
;
678 err
= get_state(state
); // may be set in get_state.
681 val
->value
.i
= state
->vlan_enabled
;
685 static int ip175c_set_enable_vlan(struct switch_dev
*dev
, const struct switch_attr
*attr
, struct switch_val
*val
)
687 struct ip175c_state
*state
= dev
->priv
;
692 err
= get_state(state
);
695 enable
= val
->value
.i
;
697 if (state
->vlan_enabled
== enable
) {
698 // do not change any state.
701 state
->vlan_enabled
= enable
;
703 // Otherwise, if we are switching state, set fields to a known default.
704 state
->remove_tag
= 0x0000;
705 state
->add_tag
= 0x0000;
706 for (i
= 0; i
< MAX_PORTS
; i
++)
707 state
->ports
[i
].shareports
= 0xffff;
709 for (i
= 0; i
< MAX_VLANS
; i
++)
710 state
->vlan_ports
[i
] = 0x0;
712 if (state
->vlan_enabled
) {
713 // updates other fields only based off vlan_ports and add_tag fields.
714 // Note that by default, no ports are in any vlans.
715 correct_vlan_state(state
);
717 // ensure sane defaults?
718 return update_state(state
);
721 static int ip175c_get_ports(struct switch_dev
*dev
, struct switch_val
*val
)
723 struct ip175c_state
*state
= dev
->priv
;
729 if (val
->port_vlan
>= dev
->vlans
|| val
->port_vlan
< 0)
732 err
= get_state(state
);
736 ports
= state
->vlan_ports
[val
->port_vlan
];
739 while (b
< MAX_PORTS
) {
741 int istagged
= ((state
->add_tag
>> b
) & 1);
742 val
->value
.ports
[ind
].id
= b
;
743 val
->value
.ports
[ind
].flags
= (istagged
<< SWITCH_PORT_FLAG_TAGGED
);
754 static int ip175c_set_ports(struct switch_dev
*dev
, struct switch_val
*val
)
756 struct ip175c_state
*state
= dev
->priv
;
760 if (val
->port_vlan
>= dev
->vlans
|| val
->port_vlan
< 0)
763 err
= get_state(state
);
767 state
->vlan_ports
[val
->port_vlan
] = 0;
768 for (i
= 0; i
< val
->len
; i
++) {
769 int bitmask
= (1<<val
->value
.ports
[i
].id
);
770 state
->vlan_ports
[val
->port_vlan
] |= bitmask
;
771 if (val
->value
.ports
[i
].flags
& (1<<SWITCH_PORT_FLAG_TAGGED
)) {
772 state
->add_tag
|= bitmask
;
774 state
->add_tag
&= (~bitmask
);
778 correct_vlan_state(state
);
779 err
= update_state(state
);
784 static int ip175c_apply(struct switch_dev
*dev
)
786 struct ip175c_state
*state
= dev
->priv
;
789 err
= get_flags(state
);
793 if (REG_SUPP(state
->regs
->MII_REGISTER_EN
)){
794 int val
= getPhy(state
, state
->regs
->MII_REGISTER_EN
);
798 val
|= (1<<state
->regs
->MII_REGISTER_EN_BIT
);
799 return setPhy(state
, state
->regs
->MII_REGISTER_EN
, val
);
804 static int ip175c_reset(struct switch_dev
*dev
)
806 struct ip175c_state
*state
= dev
->priv
;
809 err
= get_flags(state
);
813 if (REG_SUPP(state
->regs
->RESET_REG
)) {
814 err
= setPhy(state
, state
->regs
->RESET_REG
, state
->regs
->RESET_VAL
);
817 err
= getPhy(state
, state
->regs
->RESET_REG
);
819 /* data sheet specifies reset period is 2 msec
820 (don't see any mention of the 2ms delay in the IP178C spec, only
821 in IP175C, but it can't hurt.) */
825 if (REG_SUPP(state
->regs
->MODE_REG
)) {
826 err
= setPhy(state
, state
->regs
->MODE_REG
, state
->regs
->RESET_VAL
);
829 err
= getPhy(state
, state
->regs
->MODE_REG
);
832 /* reset switch ports */
833 for (i
= 0; i
< 5; i
++) {
834 err
= state
->mii_bus
->write(state
->mii_bus
, i
,
835 MII_BMCR
, BMCR_RESET
);
843 /*! get the current register number */
844 static int ip175c_get_tagged(struct switch_dev
*dev
, const struct switch_attr
*attr
, struct switch_val
*val
)
846 struct ip175c_state
*state
= dev
->priv
;
849 err
= get_state(state
);
853 if (state
->add_tag
& (1<<val
->port_vlan
)) {
854 if (state
->remove_tag
& (1<<val
->port_vlan
))
855 val
->value
.i
= 3; // shouldn't ever happen.
859 if (state
->remove_tag
& (1<<val
->port_vlan
))
867 /*! set a new register address for low level access to registers */
868 static int ip175c_set_tagged(struct switch_dev
*dev
, const struct switch_attr
*attr
, struct switch_val
*val
)
870 struct ip175c_state
*state
= dev
->priv
;
873 err
= get_state(state
);
877 state
->add_tag
&= ~(1<<val
->port_vlan
);
878 state
->remove_tag
&= ~(1<<val
->port_vlan
);
880 if (val
->value
.i
== 0)
881 state
->remove_tag
|= (1<<val
->port_vlan
);
882 if (val
->value
.i
== 1)
883 state
->add_tag
|= (1<<val
->port_vlan
);
885 SET_PORT_BITS(state
, state
->add_tag
,
886 state
->regs
->ADD_TAG_REG
, state
->regs
->ADD_TAG_BIT
);
887 SET_PORT_BITS(state
, state
->remove_tag
,
888 state
->regs
->REMOVE_TAG_REG
, state
->regs
->REMOVE_TAG_BIT
);
894 /* low level /proc procedures */
896 /*! get the current phy address */
897 static int ip175c_get_phy(struct switch_dev
*dev
, const struct switch_attr
*attr
, struct switch_val
*val
)
899 struct ip175c_state
*state
= dev
->priv
;
901 val
->value
.i
= state
->proc_mii
.p
;
905 /*! set a new phy address for low level access to registers */
906 static int ip175c_set_phy(struct switch_dev
*dev
, const struct switch_attr
*attr
, struct switch_val
*val
)
908 struct ip175c_state
*state
= dev
->priv
;
909 int new_reg
= val
->value
.i
;
911 if (new_reg
< 0 || new_reg
> 31)
912 state
->proc_mii
.p
= (u16
)-1;
914 state
->proc_mii
.p
= (u16
)new_reg
;
918 /*! get the current register number */
919 static int ip175c_get_reg(struct switch_dev
*dev
, const struct switch_attr
*attr
, struct switch_val
*val
)
921 struct ip175c_state
*state
= dev
->priv
;
923 val
->value
.i
= state
->proc_mii
.m
;
927 /*! set a new register address for low level access to registers */
928 static int ip175c_set_reg(struct switch_dev
*dev
, const struct switch_attr
*attr
, struct switch_val
*val
)
930 struct ip175c_state
*state
= dev
->priv
;
931 int new_reg
= val
->value
.i
;
933 if (new_reg
< 0 || new_reg
> 31)
934 state
->proc_mii
.m
= (u16
)-1;
936 state
->proc_mii
.m
= (u16
)new_reg
;
940 /*! get the register content of state->proc_mii */
941 static int ip175c_get_val(struct switch_dev
*dev
, const struct switch_attr
*attr
, struct switch_val
*val
)
943 struct ip175c_state
*state
= dev
->priv
;
944 int retval
= -EINVAL
;
945 if (REG_SUPP(state
->proc_mii
))
946 retval
= getPhy(state
, state
->proc_mii
);
951 val
->value
.i
= retval
;
956 /*! write a value to the register defined by phy/reg above */
957 static int ip175c_set_val(struct switch_dev
*dev
, const struct switch_attr
*attr
, struct switch_val
*val
)
959 struct ip175c_state
*state
= dev
->priv
;
962 myval
= val
->value
.i
;
963 if (myval
<= 0xffff && myval
>= 0 && REG_SUPP(state
->proc_mii
)) {
964 err
= setPhy(state
, state
->proc_mii
, (u16
)myval
);
969 static int ip175c_read_name(struct switch_dev
*dev
, const struct switch_attr
*attr
, struct switch_val
*val
)
971 struct ip175c_state
*state
= dev
->priv
;
972 val
->value
.s
= state
->regs
->NAME
; // just a const pointer, won't be freed by swconfig.
977 static int ip175c_set_port_speed(struct switch_dev
*dev
, const struct switch_attr
*attr
, struct switch_val
*val
)
979 struct ip175c_state
*state
= dev
->priv
;
980 struct mii_bus
*bus
= state
->mii_bus
;
981 int nr
= val
->port_vlan
;
985 if (val
->value
.i
== 100) {
988 } else if (val
->value
.i
== 10) {
996 /* can't set speed for cpu port */
997 if (nr
== state
->regs
->CPU_PORT
)
1000 if (nr
>= dev
->ports
|| nr
< 0)
1003 ctrl
= ip_phy_read(bus
, nr
, 0);
1009 ctrl
|= (autoneg
<<12);
1010 ctrl
|= (speed
<<13);
1012 return ip_phy_write(bus
, nr
, 0, ctrl
);
1015 static int ip175c_get_port_speed(struct switch_dev
*dev
, const struct switch_attr
*attr
, struct switch_val
*val
)
1017 struct ip175c_state
*state
= dev
->priv
;
1018 struct mii_bus
*bus
= state
->mii_bus
;
1019 int nr
= val
->port_vlan
;
1022 if (nr
== state
->regs
->CPU_PORT
) {
1027 if (nr
>= dev
->ports
|| nr
< 0)
1030 status
= ip_phy_read(bus
, nr
, 1);
1031 speed
= ip_phy_read(bus
, nr
, 18);
1032 if (status
< 0 || speed
< 0)
1036 val
->value
.i
= ((speed
& (1<<11)) ? 100 : 10);
1044 static int ip175c_get_port_status(struct switch_dev
*dev
, const struct switch_attr
*attr
, struct switch_val
*val
)
1046 struct ip175c_state
*state
= dev
->priv
;
1047 struct mii_bus
*bus
= state
->mii_bus
;
1048 int ctrl
, speed
, status
;
1049 int nr
= val
->port_vlan
;
1051 char *buf
= state
->buf
; // fixed-length at 80.
1053 if (nr
== state
->regs
->CPU_PORT
) {
1054 sprintf(buf
, "up, 100 Mbps, cpu port");
1059 if (nr
>= dev
->ports
|| nr
< 0)
1062 ctrl
= ip_phy_read(bus
, nr
, 0);
1063 status
= ip_phy_read(bus
, nr
, 1);
1064 speed
= ip_phy_read(bus
, nr
, 18);
1065 if (ctrl
< 0 || status
< 0 || speed
< 0)
1069 len
= sprintf(buf
, "up, %d Mbps, %s duplex",
1070 ((speed
& (1<<11)) ? 100 : 10),
1071 ((speed
& (1<<10)) ? "full" : "half"));
1073 len
= sprintf(buf
, "down");
1075 if (ctrl
& (1<<12)) {
1076 len
+= sprintf(buf
+len
, ", auto-negotiate");
1077 if (!(status
& (1<<5)))
1078 len
+= sprintf(buf
+len
, " (in progress)");
1080 len
+= sprintf(buf
+len
, ", fixed speed (%d)",
1081 ((ctrl
& (1<<13)) ? 100 : 10));
1089 static int ip175c_get_pvid(struct switch_dev
*dev
, int port
, int *val
)
1091 struct ip175c_state
*state
= dev
->priv
;
1093 *val
= state
->ports
[port
].pvid
;
1097 static int ip175c_set_pvid(struct switch_dev
*dev
, int port
, int val
)
1099 struct ip175c_state
*state
= dev
->priv
;
1101 state
->ports
[port
].pvid
= val
;
1103 if (!REG_SUPP(state
->regs
->VLAN_DEFAULT_TAG_REG
[port
]))
1106 return setPhy(state
, state
->regs
->VLAN_DEFAULT_TAG_REG
[port
], val
);
1120 IP175C_REGISTER_PHY
,
1121 IP175C_REGISTER_MII
,
1122 IP175C_REGISTER_VALUE
,
1123 IP175C_REGISTER_ERRNO
,
1126 static const struct switch_attr ip175c_global
[] = {
1127 [IP175C_ENABLE_VLAN
] = {
1128 .id
= IP175C_ENABLE_VLAN
,
1129 .type
= SWITCH_TYPE_INT
,
1130 .name
= "enable_vlan",
1131 .description
= "Flag to enable or disable VLANs and tagging",
1132 .get
= ip175c_get_enable_vlan
,
1133 .set
= ip175c_set_enable_vlan
,
1135 [IP175C_GET_NAME
] = {
1136 .id
= IP175C_GET_NAME
,
1137 .type
= SWITCH_TYPE_STRING
,
1138 .description
= "Returns the type of IC+ chip.",
1140 .get
= ip175c_read_name
,
1143 /* jal: added for low level debugging etc. */
1144 [IP175C_REGISTER_PHY
] = {
1145 .id
= IP175C_REGISTER_PHY
,
1146 .type
= SWITCH_TYPE_INT
,
1147 .description
= "Direct register access: set phy (0-4, or 29,30,31)",
1149 .get
= ip175c_get_phy
,
1150 .set
= ip175c_set_phy
,
1152 [IP175C_REGISTER_MII
] = {
1153 .id
= IP175C_REGISTER_MII
,
1154 .type
= SWITCH_TYPE_INT
,
1155 .description
= "Direct register access: set mii number (0-31)",
1157 .get
= ip175c_get_reg
,
1158 .set
= ip175c_set_reg
,
1160 [IP175C_REGISTER_VALUE
] = {
1161 .id
= IP175C_REGISTER_VALUE
,
1162 .type
= SWITCH_TYPE_INT
,
1163 .description
= "Direct register access: read/write to register (0-65535)",
1165 .get
= ip175c_get_val
,
1166 .set
= ip175c_set_val
,
1170 static const struct switch_attr ip175c_vlan
[] = {
1173 static const struct switch_attr ip175c_port
[] = {
1174 [IP175C_PORT_STATUS
] = {
1175 .id
= IP175C_PORT_STATUS
,
1176 .type
= SWITCH_TYPE_STRING
,
1177 .description
= "Returns Detailed port status",
1179 .get
= ip175c_get_port_status
,
1182 [IP175C_PORT_LINK
] = {
1183 .id
= IP175C_PORT_LINK
,
1184 .type
= SWITCH_TYPE_INT
,
1185 .description
= "Link speed. Can write 0 for auto-negotiate, or 10 or 100",
1187 .get
= ip175c_get_port_speed
,
1188 .set
= ip175c_set_port_speed
,
1190 [IP175C_PORT_TAGGED
] = {
1191 .id
= IP175C_PORT_LINK
,
1192 .type
= SWITCH_TYPE_INT
,
1193 .description
= "0 = untag, 1 = add tags, 2 = do not alter (This value is reset if vlans are altered)",
1195 .get
= ip175c_get_tagged
,
1196 .set
= ip175c_set_tagged
,
1200 static int ip175c_probe(struct phy_device
*pdev
)
1202 struct ip175c_state
*state
;
1203 struct switch_dev
*dev
;
1206 /* we only attach to PHY 0, but use all available PHYs */
1207 if (pdev
->addr
!= 0)
1210 state
= kzalloc(sizeof(*state
), GFP_KERNEL
);
1215 dev
->attr_global
.attr
= ip175c_global
;
1216 dev
->attr_global
.n_attr
= ARRAY_SIZE(ip175c_global
);
1217 dev
->attr_port
.attr
= ip175c_port
;
1218 dev
->attr_port
.n_attr
= ARRAY_SIZE(ip175c_port
);
1219 dev
->attr_vlan
.attr
= ip175c_vlan
;
1220 dev
->attr_vlan
.n_attr
= ARRAY_SIZE(ip175c_vlan
);
1222 dev
->get_port_pvid
= ip175c_get_pvid
;
1223 dev
->set_port_pvid
= ip175c_set_pvid
;
1224 dev
->get_vlan_ports
= ip175c_get_ports
;
1225 dev
->set_vlan_ports
= ip175c_set_ports
;
1226 dev
->apply_config
= ip175c_apply
;
1227 dev
->reset_switch
= ip175c_reset
;
1231 state
->mii_bus
= pdev
->bus
;
1233 err
= get_model(state
);
1237 dev
->vlans
= MAX_VLANS
;
1238 dev
->cpu_port
= state
->regs
->CPU_PORT
;
1239 dev
->ports
= state
->regs
->NUM_PORTS
;
1240 dev
->name
= state
->regs
->NAME
;
1249 static int ip175c_config_init(struct phy_device
*pdev
)
1251 struct ip175c_state
*state
= pdev
->priv
;
1252 struct net_device
*dev
= pdev
->attached_dev
;
1255 pdev
->irq
= PHY_IGNORE_INTERRUPT
;
1256 err
= register_switch(&state
->dev
, dev
);
1260 ip175c_reset(&state
->dev
);
1262 state
->registered
= true;
1263 netif_carrier_on(pdev
->attached_dev
);
1268 static void ip175c_remove(struct phy_device
*pdev
)
1270 struct ip175c_state
*state
= pdev
->priv
;
1272 if (state
->registered
)
1273 unregister_switch(&state
->dev
);
1277 static int ip175c_config_aneg(struct phy_device
*pdev
)
1282 static int ip175c_read_status(struct phy_device
*pdev
)
1284 pdev
->speed
= SPEED_100
;
1285 pdev
->duplex
= DUPLEX_FULL
;
1286 pdev
->pause
= pdev
->asym_pause
= 0;
1290 static struct phy_driver ip175c_driver
= {
1291 .name
= "IC+ IP175C",
1292 .phy_id
= 0x02430d80,
1293 .phy_id_mask
= 0x0ffffff0,
1294 .features
= PHY_BASIC_FEATURES
,
1295 .probe
= ip175c_probe
,
1296 .remove
= ip175c_remove
,
1297 .config_init
= ip175c_config_init
,
1298 .config_aneg
= ip175c_config_aneg
,
1299 .read_status
= ip175c_read_status
,
1300 .driver
= { .owner
= THIS_MODULE
},
1304 int __init
ip175c_init(void)
1306 return phy_driver_register(&ip175c_driver
);
1309 void __exit
ip175c_exit(void)
1311 phy_driver_unregister(&ip175c_driver
);
1314 MODULE_AUTHOR("Patrick Horn <patrick.horn@gmail.com>");
1315 MODULE_AUTHOR("Felix Fietkau <nbd@openwrt.org>");
1316 MODULE_LICENSE("GPL");
1318 module_init(ip175c_init
);
1319 module_exit(ip175c_exit
);