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,-1,-1,-1,-1},
201 .SIMPLE_VLAN_REGISTERS
= 0,
203 // Register 19-21 documentation is missing/contradictory.
204 // For registers 19-21 ports need to be: even numbers to MSB, odd to LSB.
205 // This contradicts text for ROM registers, but follows logic of CoS bits.
207 .VLAN_LOOKUP_REG
= {0,19},// +N/2
208 .VLAN_LOOKUP_REG_5
= NOTSUPPORTED
,
209 .VLAN_LOOKUP_EVEN_BIT
= {8,9,10,11,12,-1,-1,-1,-1},
210 .VLAN_LOOKUP_ODD_BIT
= {0,1,2,3,4,-1,-1,-1,-1},
212 .TAG_VLAN_MASK_REG
= NOTSUPPORTED
, // +N/2,
213 .TAG_VLAN_MASK_EVEN_BIT
= {-1,-1,-1,-1,-1,-1,-1,-1,-1},
214 .TAG_VLAN_MASK_ODD_BIT
= {-1,-1,-1,-1,-1,-1,-1,-1,-1},
217 .RESET_REG
= NOTSUPPORTED
,
219 .MODE_REG
= NOTSUPPORTED
,
221 .ROUTER_CONTROL_REG
= NOTSUPPORTED
,
222 .VLAN_CONTROL_REG
= NOTSUPPORTED
,
225 .NUMLAN_GROUPS_MAX
= -1,
226 .NUMLAN_GROUPS_BIT
= -1, // {0-2}
231 .MII_REGISTER_EN
= {0, 18},
232 .MII_REGISTER_EN_BIT
= 7,
236 struct ip175c_state
{
237 struct switch_dev dev
;
238 struct mii_bus
*mii_bus
;
241 int router_mode
; // ROUTER_EN
242 int vlan_enabled
; // TAG_VLAN_EN
245 unsigned int shareports
;
247 unsigned int add_tag
;
248 unsigned int remove_tag
;
250 unsigned int vlan_ports
[MAX_VLANS
];
251 const struct register_mappings
*regs
;
252 reg proc_mii
; /*!< phy/reg for the low level register access via /proc */
257 static int ip_phy_read(struct mii_bus
*bus
, int port
, int reg
)
261 mutex_lock(&bus
->mdio_lock
);
262 val
= bus
->read(bus
, port
, reg
);
263 mutex_unlock(&bus
->mdio_lock
);
269 static int ip_phy_write(struct mii_bus
*bus
, int port
, int reg
, u16 val
)
273 mutex_lock(&bus
->mdio_lock
);
274 err
= bus
->write(bus
, port
, reg
, val
);
275 mutex_unlock(&bus
->mdio_lock
);
281 static int getPhy (struct ip175c_state
*state
, reg mii
)
283 struct mii_bus
*bus
= state
->mii_bus
;
289 val
= ip_phy_read(bus
, mii
.p
, mii
.m
);
291 pr_warning("IP175C: Unable to get MII register %d,%d: error %d\n", mii
.p
,mii
.m
,-val
);
296 static int setPhy (struct ip175c_state
*state
, reg mii
, u16 value
)
298 struct mii_bus
*bus
= state
->mii_bus
;
304 err
= ip_phy_write(bus
, mii
.p
, mii
.m
, value
);
306 pr_warning("IP175C: Unable to set MII register %d,%d to %d: error %d\n", mii
.p
,mii
.m
,value
,-err
);
315 * These two macros are to simplify the mapping of logical bits to the bits in hardware.
316 * NOTE: these macros will return if there is an error!
319 #define GET_PORT_BITS(state, bits, addr, bit_lookup) \
321 int i, val = getPhy((state), (addr)); \
325 for (i = 0; i < MAX_PORTS; i++) { \
326 if ((bit_lookup)[i] == -1) continue; \
327 if (val & (1<<(bit_lookup)[i])) \
332 #define SET_PORT_BITS(state, bits, addr, bit_lookup) \
334 int i, val = getPhy((state), (addr)); \
337 for (i = 0; i < MAX_PORTS; i++) { \
338 unsigned int newmask = ((bits)&(1<<i)); \
339 if ((bit_lookup)[i] == -1) continue; \
340 val &= ~(1<<(bit_lookup)[i]); \
341 val |= ((newmask>>i)<<(bit_lookup)[i]); \
343 val = setPhy((state), (addr), val); \
348 static int get_model(struct ip175c_state
*state
)
350 reg oui_id_reg
= {0, 2};
352 reg model_no_reg
= {0, 3};
353 int model_no
, model_no_orig
;
355 // 175 and 178 have the same oui ID.
356 reg oui_id_reg_178c
= {5, 2}; // returns error on IP175C.
359 oui_id
= getPhy(state
, oui_id_reg
);
360 if (oui_id
!= 0x0243) {
362 return -ENODEV
; // Not a IC+ chip.
364 oui_id
= getPhy(state
, oui_id_reg_178c
);
365 if (oui_id
== 0x0243) {
369 model_no_orig
= getPhy(state
, model_no_reg
);
370 if (model_no_orig
< 0) {
373 model_no
= model_no_orig
>> 4; // shift out revision number.
374 model_no
&= 0x3f; // only take the model number (low 6 bits).
375 if (model_no
== IP175A
.MODEL_NO
) {
376 state
->regs
= &IP175A
;
377 } else if (model_no
== IP175C
.MODEL_NO
) {
379 state
->regs
= &IP178C
;
381 state
->regs
= &IP175C
;
384 printk(KERN_WARNING
"ip175c: Found an unknown IC+ switch with model number %02Xh.\n", model_no_orig
);
390 /** Get only the vlan and router flags on the router **/
391 static int get_flags(struct ip175c_state
*state
)
395 state
->router_mode
= 0;
396 state
->vlan_enabled
= -1; // hack
397 state
->num_vlans
= 0;
399 if (!REG_SUPP(state
->regs
->ROUTER_CONTROL_REG
)) {
400 return 0; // not an error if it doesn't support enable vlan.
403 val
= getPhy(state
, state
->regs
->ROUTER_CONTROL_REG
);
407 if (state
->regs
->ROUTER_EN_BIT
>= 0)
408 state
->router_mode
= ((val
>>state
->regs
->ROUTER_EN_BIT
) & 1);
410 if (state
->regs
->NUMLAN_GROUPS_BIT
>= 0) {
411 state
->num_vlans
= (val
>> state
->regs
->NUMLAN_GROUPS_BIT
);
412 state
->num_vlans
&= (state
->regs
->NUMLAN_GROUPS_MAX
-1);
413 state
->num_vlans
+=1; // does not include WAN.
417 val
= getPhy(state
, state
->regs
->VLAN_CONTROL_REG
);
421 if (state
->regs
->TAG_VLAN_BIT
>= 0)
422 state
->vlan_enabled
= ((val
>>state
->regs
->TAG_VLAN_BIT
) & 1);
426 /** Get all state variables for VLAN mappings and port-based tagging. **/
427 static int get_state(struct ip175c_state
*state
)
431 ret
= get_flags(state
);
435 GET_PORT_BITS(state
, state
->remove_tag
,
436 state
->regs
->REMOVE_TAG_REG
, state
->regs
->REMOVE_TAG_BIT
);
437 GET_PORT_BITS(state
, state
->add_tag
,
438 state
->regs
->ADD_TAG_REG
, state
->regs
->ADD_TAG_BIT
);
440 if (state
->vlan_enabled
== -1) {
441 // not sure how to get this...
442 state
->vlan_enabled
= (state
->remove_tag
|| state
->add_tag
);
445 if (REG_SUPP(state
->regs
->VLAN_LOOKUP_REG
)) {
446 for (j
=0; j
<MAX_PORTS
; j
++) {
447 state
->ports
[j
].shareports
= 0; // initialize them in case.
449 for (j
=0; j
<state
->regs
->NUM_PORTS
; j
++) {
451 const bitnum
*bit_lookup
= (j
%2==0)?
452 state
->regs
->VLAN_LOOKUP_EVEN_BIT
:
453 state
->regs
->VLAN_LOOKUP_ODD_BIT
;
454 addr
= state
->regs
->VLAN_LOOKUP_REG
;
455 if (state
->regs
->SIMPLE_VLAN_REGISTERS
) {
470 addr
= state
->regs
->VLAN_LOOKUP_REG_5
;
475 if (REG_SUPP(addr
)) {
476 GET_PORT_BITS(state
, state
->ports
[j
].shareports
, addr
, bit_lookup
);
480 for (j
=0; j
<MAX_PORTS
; j
++) {
481 state
->ports
[j
].shareports
= 0xff;
485 for (i
=0; i
<MAX_PORTS
; i
++) {
486 if (REG_SUPP(state
->regs
->VLAN_DEFAULT_TAG_REG
[i
])) {
487 int val
= getPhy(state
, state
->regs
->VLAN_DEFAULT_TAG_REG
[i
]);
491 state
->ports
[i
].pvid
= val
;
493 state
->ports
[i
].pvid
= 0;
497 if (REG_SUPP(state
->regs
->TAG_VLAN_MASK_REG
)) {
498 for (j
=0; j
<MAX_VLANS
; j
++) {
499 reg addr
= state
->regs
->TAG_VLAN_MASK_REG
;
500 const bitnum
*bit_lookup
= (j
%2==0)?
501 state
->regs
->TAG_VLAN_MASK_EVEN_BIT
:
502 state
->regs
->TAG_VLAN_MASK_ODD_BIT
;
503 if (state
->regs
->SIMPLE_VLAN_REGISTERS
) {
508 GET_PORT_BITS(state
, state
->vlan_ports
[j
], addr
, bit_lookup
);
511 for (j
=0; j
<MAX_VLANS
; j
++) {
512 state
->vlan_ports
[j
] = 0;
513 for (i
=0; i
<state
->regs
->NUM_PORTS
; i
++) {
514 if ((state
->ports
[i
].pvid
== j
) ||
515 (state
->ports
[i
].pvid
== 0)) {
516 state
->vlan_ports
[j
] |= (1<<i
);
526 /** Only update vlan and router flags in the switch **/
527 static int update_flags(struct ip175c_state
*state
)
531 if (!REG_SUPP(state
->regs
->ROUTER_CONTROL_REG
)) {
535 val
= getPhy(state
, state
->regs
->ROUTER_CONTROL_REG
);
539 if (state
->regs
->ROUTER_EN_BIT
>= 0) {
540 if (state
->router_mode
) {
541 val
|= (1<<state
->regs
->ROUTER_EN_BIT
);
543 val
&= (~(1<<state
->regs
->ROUTER_EN_BIT
));
546 if (state
->regs
->TAG_VLAN_BIT
>= 0) {
547 if (state
->vlan_enabled
) {
548 val
|= (1<<state
->regs
->TAG_VLAN_BIT
);
550 val
&= (~(1<<state
->regs
->TAG_VLAN_BIT
));
553 if (state
->regs
->NUMLAN_GROUPS_BIT
>= 0) {
554 val
&= (~((state
->regs
->NUMLAN_GROUPS_MAX
-1)<<state
->regs
->NUMLAN_GROUPS_BIT
));
555 if (state
->num_vlans
> state
->regs
->NUMLAN_GROUPS_MAX
) {
556 val
|= state
->regs
->NUMLAN_GROUPS_MAX
<< state
->regs
->NUMLAN_GROUPS_BIT
;
557 } else if (state
->num_vlans
>= 1) {
558 val
|= (state
->num_vlans
-1) << state
->regs
->NUMLAN_GROUPS_BIT
;
561 return setPhy(state
, state
->regs
->ROUTER_CONTROL_REG
, val
);
564 /** Update all VLAN and port state. Usually you should call "correct_vlan_state" first. **/
565 static int update_state(struct ip175c_state
*state
)
569 SET_PORT_BITS(state
, state
->add_tag
,
570 state
->regs
->ADD_TAG_REG
, state
->regs
->ADD_TAG_BIT
);
571 SET_PORT_BITS(state
, state
->remove_tag
,
572 state
->regs
->REMOVE_TAG_REG
, state
->regs
->REMOVE_TAG_BIT
);
574 if (REG_SUPP(state
->regs
->VLAN_LOOKUP_REG
)) {
575 for (j
=0; j
<state
->regs
->NUM_PORTS
; j
++) {
577 const bitnum
*bit_lookup
= (j
%2==0)?
578 state
->regs
->VLAN_LOOKUP_EVEN_BIT
:
579 state
->regs
->VLAN_LOOKUP_ODD_BIT
;
581 // duplicate code -- sorry
582 addr
= state
->regs
->VLAN_LOOKUP_REG
;
583 if (state
->regs
->SIMPLE_VLAN_REGISTERS
) {
598 addr
= state
->regs
->VLAN_LOOKUP_REG_5
;
601 addr
.m
= -1; // shouldn't get here, but...
605 //printf("shareports for %d is %02X\n",j,state->ports[j].shareports);
606 if (REG_SUPP(addr
)) {
607 SET_PORT_BITS(state
, state
->ports
[j
].shareports
, addr
, bit_lookup
);
611 if (REG_SUPP(state
->regs
->TAG_VLAN_MASK_REG
)) {
612 for (j
=0; j
<MAX_VLANS
; j
++) {
613 reg addr
= state
->regs
->TAG_VLAN_MASK_REG
;
614 const bitnum
*bit_lookup
= (j
%2==0)?
615 state
->regs
->TAG_VLAN_MASK_EVEN_BIT
:
616 state
->regs
->TAG_VLAN_MASK_ODD_BIT
;
617 unsigned int vlan_mask
;
618 if (state
->regs
->SIMPLE_VLAN_REGISTERS
) {
623 vlan_mask
= state
->vlan_ports
[j
];
624 SET_PORT_BITS(state
, vlan_mask
, addr
, bit_lookup
);
628 for (i
=0; i
<MAX_PORTS
; i
++) {
629 if (REG_SUPP(state
->regs
->VLAN_DEFAULT_TAG_REG
[i
])) {
630 int err
= setPhy(state
, state
->regs
->VLAN_DEFAULT_TAG_REG
[i
],
631 state
->ports
[i
].pvid
);
638 return update_flags(state
);
640 // software reset: 30.0 = 0x175C
642 // reset ports 0,1,2,3,4
646 Uses only the VLAN port mask and the add tag mask to generate the other fields:
647 which ports are part of the same VLAN, removing vlan tags, and VLAN tag ids.
649 static void correct_vlan_state(struct ip175c_state
*state
)
652 state
->num_vlans
= 0;
653 for (i
=0; i
<MAX_VLANS
; i
++) {
654 if (state
->vlan_ports
[i
] != 0) {
655 state
->num_vlans
= i
+1; //hack -- we need to store the "set" vlans somewhere...
661 for (i
=0; i
<state
->regs
->NUM_PORTS
; i
++) {
662 unsigned int portmask
= (1<<i
);
663 if (!state
->vlan_enabled
) {
664 // share with everybody!
665 state
->ports
[i
].shareports
= (1<<state
->regs
->NUM_PORTS
)-1;
668 state
->ports
[i
].shareports
= portmask
;
669 for (j
=0; j
<MAX_VLANS
; j
++) {
670 if (state
->vlan_ports
[j
] & portmask
)
671 state
->ports
[i
].shareports
|= state
->vlan_ports
[j
];
674 state
->remove_tag
= ((~state
->add_tag
) & ((1<<state
->regs
->NUM_PORTS
)-1));
677 static int ip175c_get_enable_vlan(struct switch_dev
*dev
, const struct switch_attr
*attr
, struct switch_val
*val
)
679 struct ip175c_state
*state
= dev
->priv
;
682 err
= get_state(state
); // may be set in get_state.
685 val
->value
.i
= state
->vlan_enabled
;
689 static int ip175c_set_enable_vlan(struct switch_dev
*dev
, const struct switch_attr
*attr
, struct switch_val
*val
)
691 struct ip175c_state
*state
= dev
->priv
;
696 err
= get_state(state
);
699 enable
= val
->value
.i
;
701 if (state
->vlan_enabled
== enable
) {
702 // do not change any state.
705 state
->vlan_enabled
= enable
;
707 // Otherwise, if we are switching state, set fields to a known default.
708 state
->remove_tag
= 0x0000;
709 state
->add_tag
= 0x0000;
710 for (i
= 0; i
< MAX_PORTS
; i
++)
711 state
->ports
[i
].shareports
= 0xffff;
713 for (i
= 0; i
< MAX_VLANS
; i
++)
714 state
->vlan_ports
[i
] = 0x0;
716 if (state
->vlan_enabled
) {
717 // updates other fields only based off vlan_ports and add_tag fields.
718 // Note that by default, no ports are in any vlans.
719 correct_vlan_state(state
);
721 // ensure sane defaults?
722 return update_state(state
);
725 static int ip175c_get_ports(struct switch_dev
*dev
, struct switch_val
*val
)
727 struct ip175c_state
*state
= dev
->priv
;
733 if (val
->port_vlan
>= dev
->vlans
|| val
->port_vlan
< 0)
736 err
= get_state(state
);
740 ports
= state
->vlan_ports
[val
->port_vlan
];
743 while (b
< MAX_PORTS
) {
745 int istagged
= ((state
->add_tag
>> b
) & 1);
746 val
->value
.ports
[ind
].id
= b
;
747 val
->value
.ports
[ind
].flags
= (istagged
<< SWITCH_PORT_FLAG_TAGGED
);
758 static int ip175c_set_ports(struct switch_dev
*dev
, struct switch_val
*val
)
760 struct ip175c_state
*state
= dev
->priv
;
764 if (val
->port_vlan
>= dev
->vlans
|| val
->port_vlan
< 0)
767 err
= get_state(state
);
771 state
->vlan_ports
[val
->port_vlan
] = 0;
772 for (i
= 0; i
< val
->len
; i
++) {
773 int bitmask
= (1<<val
->value
.ports
[i
].id
);
774 state
->vlan_ports
[val
->port_vlan
] |= bitmask
;
775 if (val
->value
.ports
[i
].flags
& (1<<SWITCH_PORT_FLAG_TAGGED
)) {
776 state
->add_tag
|= bitmask
;
778 state
->add_tag
&= (~bitmask
);
782 correct_vlan_state(state
);
783 err
= update_state(state
);
788 static int ip175c_apply(struct switch_dev
*dev
)
790 struct ip175c_state
*state
= dev
->priv
;
793 err
= get_flags(state
);
797 if (REG_SUPP(state
->regs
->MII_REGISTER_EN
)){
798 int val
= getPhy(state
, state
->regs
->MII_REGISTER_EN
);
802 val
|= (1<<state
->regs
->MII_REGISTER_EN_BIT
);
803 return setPhy(state
, state
->regs
->MII_REGISTER_EN
, val
);
808 static int ip175c_reset(struct switch_dev
*dev
)
810 struct ip175c_state
*state
= dev
->priv
;
813 err
= get_flags(state
);
817 if (REG_SUPP(state
->regs
->RESET_REG
)) {
818 err
= setPhy(state
, state
->regs
->RESET_REG
, state
->regs
->RESET_VAL
);
821 err
= getPhy(state
, state
->regs
->RESET_REG
);
823 /* data sheet specifies reset period is 2 msec
824 (don't see any mention of the 2ms delay in the IP178C spec, only
825 in IP175C, but it can't hurt.) */
829 if (REG_SUPP(state
->regs
->MODE_REG
)) {
830 err
= setPhy(state
, state
->regs
->MODE_REG
, state
->regs
->RESET_VAL
);
833 err
= getPhy(state
, state
->regs
->MODE_REG
);
836 if (REG_SUPP(state
->regs
->RESET_REG
)) {
837 /* reset external phy ports, except on IP175A */
838 for (i
= 0; i
< state
->regs
->NUM_PORTS
-1; i
++) {
839 err
= state
->mii_bus
->write(state
->mii_bus
, i
,
840 MII_BMCR
, BMCR_RESET
);
849 /*! get the current register number */
850 static int ip175c_get_tagged(struct switch_dev
*dev
, const struct switch_attr
*attr
, struct switch_val
*val
)
852 struct ip175c_state
*state
= dev
->priv
;
855 err
= get_state(state
);
859 if (state
->add_tag
& (1<<val
->port_vlan
)) {
860 if (state
->remove_tag
& (1<<val
->port_vlan
))
861 val
->value
.i
= 3; // shouldn't ever happen.
865 if (state
->remove_tag
& (1<<val
->port_vlan
))
873 /*! set a new register address for low level access to registers */
874 static int ip175c_set_tagged(struct switch_dev
*dev
, const struct switch_attr
*attr
, struct switch_val
*val
)
876 struct ip175c_state
*state
= dev
->priv
;
879 err
= get_state(state
);
883 state
->add_tag
&= ~(1<<val
->port_vlan
);
884 state
->remove_tag
&= ~(1<<val
->port_vlan
);
886 if (val
->value
.i
== 0)
887 state
->remove_tag
|= (1<<val
->port_vlan
);
888 if (val
->value
.i
== 1)
889 state
->add_tag
|= (1<<val
->port_vlan
);
891 SET_PORT_BITS(state
, state
->add_tag
,
892 state
->regs
->ADD_TAG_REG
, state
->regs
->ADD_TAG_BIT
);
893 SET_PORT_BITS(state
, state
->remove_tag
,
894 state
->regs
->REMOVE_TAG_REG
, state
->regs
->REMOVE_TAG_BIT
);
900 /* low level /proc procedures */
902 /*! get the current phy address */
903 static int ip175c_get_phy(struct switch_dev
*dev
, const struct switch_attr
*attr
, struct switch_val
*val
)
905 struct ip175c_state
*state
= dev
->priv
;
907 val
->value
.i
= state
->proc_mii
.p
;
911 /*! set a new phy address for low level access to registers */
912 static int ip175c_set_phy(struct switch_dev
*dev
, const struct switch_attr
*attr
, struct switch_val
*val
)
914 struct ip175c_state
*state
= dev
->priv
;
915 int new_reg
= val
->value
.i
;
917 if (new_reg
< 0 || new_reg
> 31)
918 state
->proc_mii
.p
= (u16
)-1;
920 state
->proc_mii
.p
= (u16
)new_reg
;
924 /*! get the current register number */
925 static int ip175c_get_reg(struct switch_dev
*dev
, const struct switch_attr
*attr
, struct switch_val
*val
)
927 struct ip175c_state
*state
= dev
->priv
;
929 val
->value
.i
= state
->proc_mii
.m
;
933 /*! set a new register address for low level access to registers */
934 static int ip175c_set_reg(struct switch_dev
*dev
, const struct switch_attr
*attr
, struct switch_val
*val
)
936 struct ip175c_state
*state
= dev
->priv
;
937 int new_reg
= val
->value
.i
;
939 if (new_reg
< 0 || new_reg
> 31)
940 state
->proc_mii
.m
= (u16
)-1;
942 state
->proc_mii
.m
= (u16
)new_reg
;
946 /*! get the register content of state->proc_mii */
947 static int ip175c_get_val(struct switch_dev
*dev
, const struct switch_attr
*attr
, struct switch_val
*val
)
949 struct ip175c_state
*state
= dev
->priv
;
950 int retval
= -EINVAL
;
951 if (REG_SUPP(state
->proc_mii
))
952 retval
= getPhy(state
, state
->proc_mii
);
957 val
->value
.i
= retval
;
962 /*! write a value to the register defined by phy/reg above */
963 static int ip175c_set_val(struct switch_dev
*dev
, const struct switch_attr
*attr
, struct switch_val
*val
)
965 struct ip175c_state
*state
= dev
->priv
;
968 myval
= val
->value
.i
;
969 if (myval
<= 0xffff && myval
>= 0 && REG_SUPP(state
->proc_mii
)) {
970 err
= setPhy(state
, state
->proc_mii
, (u16
)myval
);
975 static int ip175c_read_name(struct switch_dev
*dev
, const struct switch_attr
*attr
, struct switch_val
*val
)
977 struct ip175c_state
*state
= dev
->priv
;
978 val
->value
.s
= state
->regs
->NAME
; // just a const pointer, won't be freed by swconfig.
983 static int ip175c_set_port_speed(struct switch_dev
*dev
, const struct switch_attr
*attr
, struct switch_val
*val
)
985 struct ip175c_state
*state
= dev
->priv
;
986 struct mii_bus
*bus
= state
->mii_bus
;
987 int nr
= val
->port_vlan
;
991 if (val
->value
.i
== 100) {
994 } else if (val
->value
.i
== 10) {
1002 /* can't set speed for cpu port */
1003 if (nr
== state
->regs
->CPU_PORT
)
1006 if (nr
>= dev
->ports
|| nr
< 0)
1009 ctrl
= ip_phy_read(bus
, nr
, 0);
1015 ctrl
|= (autoneg
<<12);
1016 ctrl
|= (speed
<<13);
1018 return ip_phy_write(bus
, nr
, 0, ctrl
);
1021 static int ip175c_get_port_speed(struct switch_dev
*dev
, const struct switch_attr
*attr
, struct switch_val
*val
)
1023 struct ip175c_state
*state
= dev
->priv
;
1024 struct mii_bus
*bus
= state
->mii_bus
;
1025 int nr
= val
->port_vlan
;
1028 if (nr
== state
->regs
->CPU_PORT
) {
1033 if (nr
>= dev
->ports
|| nr
< 0)
1036 status
= ip_phy_read(bus
, nr
, 1);
1037 speed
= ip_phy_read(bus
, nr
, 18);
1038 if (status
< 0 || speed
< 0)
1042 val
->value
.i
= ((speed
& (1<<11)) ? 100 : 10);
1050 static int ip175c_get_port_status(struct switch_dev
*dev
, const struct switch_attr
*attr
, struct switch_val
*val
)
1052 struct ip175c_state
*state
= dev
->priv
;
1053 struct mii_bus
*bus
= state
->mii_bus
;
1054 int ctrl
, speed
, status
;
1055 int nr
= val
->port_vlan
;
1057 char *buf
= state
->buf
; // fixed-length at 80.
1059 if (nr
== state
->regs
->CPU_PORT
) {
1060 sprintf(buf
, "up, 100 Mbps, cpu port");
1065 if (nr
>= dev
->ports
|| nr
< 0)
1068 ctrl
= ip_phy_read(bus
, nr
, 0);
1069 status
= ip_phy_read(bus
, nr
, 1);
1070 speed
= ip_phy_read(bus
, nr
, 18);
1071 if (ctrl
< 0 || status
< 0 || speed
< 0)
1075 len
= sprintf(buf
, "up, %d Mbps, %s duplex",
1076 ((speed
& (1<<11)) ? 100 : 10),
1077 ((speed
& (1<<10)) ? "full" : "half"));
1079 len
= sprintf(buf
, "down");
1081 if (ctrl
& (1<<12)) {
1082 len
+= sprintf(buf
+len
, ", auto-negotiate");
1083 if (!(status
& (1<<5)))
1084 len
+= sprintf(buf
+len
, " (in progress)");
1086 len
+= sprintf(buf
+len
, ", fixed speed (%d)",
1087 ((ctrl
& (1<<13)) ? 100 : 10));
1095 static int ip175c_get_pvid(struct switch_dev
*dev
, int port
, int *val
)
1097 struct ip175c_state
*state
= dev
->priv
;
1099 *val
= state
->ports
[port
].pvid
;
1103 static int ip175c_set_pvid(struct switch_dev
*dev
, int port
, int val
)
1105 struct ip175c_state
*state
= dev
->priv
;
1107 state
->ports
[port
].pvid
= val
;
1109 if (!REG_SUPP(state
->regs
->VLAN_DEFAULT_TAG_REG
[port
]))
1112 return setPhy(state
, state
->regs
->VLAN_DEFAULT_TAG_REG
[port
], val
);
1126 IP175C_REGISTER_PHY
,
1127 IP175C_REGISTER_MII
,
1128 IP175C_REGISTER_VALUE
,
1129 IP175C_REGISTER_ERRNO
,
1132 static const struct switch_attr ip175c_global
[] = {
1133 [IP175C_ENABLE_VLAN
] = {
1134 .id
= IP175C_ENABLE_VLAN
,
1135 .type
= SWITCH_TYPE_INT
,
1136 .name
= "enable_vlan",
1137 .description
= "Flag to enable or disable VLANs and tagging",
1138 .get
= ip175c_get_enable_vlan
,
1139 .set
= ip175c_set_enable_vlan
,
1141 [IP175C_GET_NAME
] = {
1142 .id
= IP175C_GET_NAME
,
1143 .type
= SWITCH_TYPE_STRING
,
1144 .description
= "Returns the type of IC+ chip.",
1146 .get
= ip175c_read_name
,
1149 /* jal: added for low level debugging etc. */
1150 [IP175C_REGISTER_PHY
] = {
1151 .id
= IP175C_REGISTER_PHY
,
1152 .type
= SWITCH_TYPE_INT
,
1153 .description
= "Direct register access: set phy (0-4, or 29,30,31)",
1155 .get
= ip175c_get_phy
,
1156 .set
= ip175c_set_phy
,
1158 [IP175C_REGISTER_MII
] = {
1159 .id
= IP175C_REGISTER_MII
,
1160 .type
= SWITCH_TYPE_INT
,
1161 .description
= "Direct register access: set mii number (0-31)",
1163 .get
= ip175c_get_reg
,
1164 .set
= ip175c_set_reg
,
1166 [IP175C_REGISTER_VALUE
] = {
1167 .id
= IP175C_REGISTER_VALUE
,
1168 .type
= SWITCH_TYPE_INT
,
1169 .description
= "Direct register access: read/write to register (0-65535)",
1171 .get
= ip175c_get_val
,
1172 .set
= ip175c_set_val
,
1176 static const struct switch_attr ip175c_vlan
[] = {
1179 static const struct switch_attr ip175c_port
[] = {
1180 [IP175C_PORT_STATUS
] = {
1181 .id
= IP175C_PORT_STATUS
,
1182 .type
= SWITCH_TYPE_STRING
,
1183 .description
= "Returns Detailed port status",
1185 .get
= ip175c_get_port_status
,
1188 [IP175C_PORT_LINK
] = {
1189 .id
= IP175C_PORT_LINK
,
1190 .type
= SWITCH_TYPE_INT
,
1191 .description
= "Link speed. Can write 0 for auto-negotiate, or 10 or 100",
1193 .get
= ip175c_get_port_speed
,
1194 .set
= ip175c_set_port_speed
,
1196 [IP175C_PORT_TAGGED
] = {
1197 .id
= IP175C_PORT_LINK
,
1198 .type
= SWITCH_TYPE_INT
,
1199 .description
= "0 = untag, 1 = add tags, 2 = do not alter (This value is reset if vlans are altered)",
1201 .get
= ip175c_get_tagged
,
1202 .set
= ip175c_set_tagged
,
1206 static int ip175c_probe(struct phy_device
*pdev
)
1208 struct ip175c_state
*state
;
1209 struct switch_dev
*dev
;
1212 /* we only attach to PHY 0, but use all available PHYs */
1213 if (pdev
->addr
!= 0)
1216 state
= kzalloc(sizeof(*state
), GFP_KERNEL
);
1221 dev
->attr_global
.attr
= ip175c_global
;
1222 dev
->attr_global
.n_attr
= ARRAY_SIZE(ip175c_global
);
1223 dev
->attr_port
.attr
= ip175c_port
;
1224 dev
->attr_port
.n_attr
= ARRAY_SIZE(ip175c_port
);
1225 dev
->attr_vlan
.attr
= ip175c_vlan
;
1226 dev
->attr_vlan
.n_attr
= ARRAY_SIZE(ip175c_vlan
);
1228 dev
->get_port_pvid
= ip175c_get_pvid
;
1229 dev
->set_port_pvid
= ip175c_set_pvid
;
1230 dev
->get_vlan_ports
= ip175c_get_ports
;
1231 dev
->set_vlan_ports
= ip175c_set_ports
;
1232 dev
->apply_config
= ip175c_apply
;
1233 dev
->reset_switch
= ip175c_reset
;
1237 state
->mii_bus
= pdev
->bus
;
1239 err
= get_model(state
);
1243 dev
->vlans
= MAX_VLANS
;
1244 dev
->cpu_port
= state
->regs
->CPU_PORT
;
1245 dev
->ports
= state
->regs
->NUM_PORTS
;
1246 dev
->name
= state
->regs
->NAME
;
1255 static int ip175c_config_init(struct phy_device
*pdev
)
1257 struct ip175c_state
*state
= pdev
->priv
;
1258 struct net_device
*dev
= pdev
->attached_dev
;
1261 pdev
->irq
= PHY_IGNORE_INTERRUPT
;
1262 err
= register_switch(&state
->dev
, dev
);
1266 ip175c_reset(&state
->dev
);
1268 state
->registered
= true;
1269 netif_carrier_on(pdev
->attached_dev
);
1274 static void ip175c_remove(struct phy_device
*pdev
)
1276 struct ip175c_state
*state
= pdev
->priv
;
1278 if (state
->registered
)
1279 unregister_switch(&state
->dev
);
1283 static int ip175c_config_aneg(struct phy_device
*pdev
)
1288 static int ip175c_read_status(struct phy_device
*pdev
)
1290 pdev
->speed
= SPEED_100
;
1291 pdev
->duplex
= DUPLEX_FULL
;
1292 pdev
->pause
= pdev
->asym_pause
= 0;
1296 static struct phy_driver ip175c_driver
= {
1297 .name
= "IC+ IP175C",
1298 .phy_id
= 0x02430d80,
1299 .phy_id_mask
= 0x0ffffff0,
1300 .features
= PHY_BASIC_FEATURES
,
1301 .probe
= ip175c_probe
,
1302 .remove
= ip175c_remove
,
1303 .config_init
= ip175c_config_init
,
1304 .config_aneg
= ip175c_config_aneg
,
1305 .read_status
= ip175c_read_status
,
1306 .driver
= { .owner
= THIS_MODULE
},
1309 static struct phy_driver ip175a_driver
= {
1310 .name
= "IC+ IP175A",
1311 .phy_id
= 0x02430c50,
1312 .phy_id_mask
= 0x0ffffff0,
1313 .features
= PHY_BASIC_FEATURES
,
1314 .probe
= ip175c_probe
,
1315 .remove
= ip175c_remove
,
1316 .config_init
= ip175c_config_init
,
1317 .config_aneg
= ip175c_config_aneg
,
1318 .read_status
= ip175c_read_status
,
1319 .driver
= { .owner
= THIS_MODULE
},
1323 int __init
ip175c_init(void)
1327 ret
= phy_driver_register(&ip175a_driver
);
1331 return phy_driver_register(&ip175c_driver
);
1334 void __exit
ip175c_exit(void)
1336 phy_driver_unregister(&ip175c_driver
);
1337 phy_driver_unregister(&ip175a_driver
);
1340 MODULE_AUTHOR("Patrick Horn <patrick.horn@gmail.com>");
1341 MODULE_AUTHOR("Felix Fietkau <nbd@openwrt.org>");
1342 MODULE_LICENSE("GPL");
1344 module_init(ip175c_init
);
1345 module_exit(ip175c_exit
);