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))
43 /*********** CONSTANTS ***********/
44 struct register_mappings
{
46 u16 MODEL_NO
; // Compare to bits 4-9 of MII register 0,3.
50 /* The default VLAN for each port.
51 Default: 0x0001 for Ports 0,1,2,3
52 0x0002 for Ports 4,5 */
53 reg VLAN_DEFAULT_TAG_REG
[MAX_PORTS
];
55 /* These ports are tagged.
59 bitnum ADD_TAG_BIT
[MAX_PORTS
];
60 /* These ports are untagged.
61 Default: 0x00 (i.e. do not alter any VLAN tags...)
62 Maybe set to 0 if user disables VLANs. */
63 bitnum REMOVE_TAG_BIT
[MAX_PORTS
];
65 /* Port M and Port N are on the same VLAN.
66 Default: All ports on all VLANs. */
67 // Use register {29, 19+N/2}
69 // Port 5 uses register {30, 18} but same as odd bits.
70 reg VLAN_LOOKUP_REG_5
; // in a different register on IP175C.
71 bitnum VLAN_LOOKUP_EVEN_BIT
[MAX_PORTS
];
72 bitnum VLAN_LOOKUP_ODD_BIT
[MAX_PORTS
];
74 /* This VLAN corresponds to which ports.
75 Default: 0x2f,0x30,0x3f,0x3f... */
76 reg TAG_VLAN_MASK_REG
;
77 bitnum TAG_VLAN_MASK_EVEN_BIT
[MAX_PORTS
];
78 bitnum TAG_VLAN_MASK_ODD_BIT
[MAX_PORTS
];
87 reg ROUTER_CONTROL_REG
;
91 bitnum NUMLAN_GROUPS_MAX
;
92 bitnum NUMLAN_GROUPS_BIT
;
95 bitnum MII_REGISTER_EN_BIT
;
97 // set to 1 for 178C, 0 for 175C.
98 bitnum SIMPLE_VLAN_REGISTERS
; // 175C has two vlans per register but 178C has only one.
101 static const struct register_mappings IP178C
= {
104 .VLAN_DEFAULT_TAG_REG
= {
105 {30,3},{30,4},{30,5},{30,6},{30,7},{30,8},
106 {30,9},{30,10},{30,11},
109 .ADD_TAG_REG
= {30,12},
110 .ADD_TAG_BIT
= {0,1,2,3,4,5,6,7,8},
111 .REMOVE_TAG_REG
= {30,13},
112 .REMOVE_TAG_BIT
= {4,5,6,7,8,9,10,11,12},
114 .SIMPLE_VLAN_REGISTERS
= 1,
116 .VLAN_LOOKUP_REG
= {31,0},// +N
117 .VLAN_LOOKUP_REG_5
= NOTSUPPORTED
, // not used with SIMPLE_VLAN_REGISTERS
118 .VLAN_LOOKUP_EVEN_BIT
= {0,1,2,3,4,5,6,7,8},
119 .VLAN_LOOKUP_ODD_BIT
= {0,1,2,3,4,5,6,7,8},
121 .TAG_VLAN_MASK_REG
= {30,14}, // +N
122 .TAG_VLAN_MASK_EVEN_BIT
= {0,1,2,3,4,5,6,7,8},
123 .TAG_VLAN_MASK_ODD_BIT
= {0,1,2,3,4,5,6,7,8},
128 .MODE_REG
= NOTSUPPORTED
,
130 .ROUTER_CONTROL_REG
= {30,30},
132 .NUMLAN_GROUPS_MAX
= 8,
133 .NUMLAN_GROUPS_BIT
= 8, // {0-2}
135 .VLAN_CONTROL_REG
= {30,13},
141 .MII_REGISTER_EN
= NOTSUPPORTED
,
145 static const struct register_mappings IP175C
= {
148 .VLAN_DEFAULT_TAG_REG
= {
149 {29,24},{29,25},{29,26},{29,27},{29,28},{29,30},
150 NOTSUPPORTED
,NOTSUPPORTED
,NOTSUPPORTED
153 .ADD_TAG_REG
= {29,23},
154 .REMOVE_TAG_REG
= {29,23},
155 .ADD_TAG_BIT
= {11,12,13,14,15,1,-1,-1,-1},
156 .REMOVE_TAG_BIT
= {6,7,8,9,10,0,-1,-1,-1},
158 .SIMPLE_VLAN_REGISTERS
= 0,
160 .VLAN_LOOKUP_REG
= {29,19},// +N/2
161 .VLAN_LOOKUP_REG_5
= {30,18},
162 .VLAN_LOOKUP_EVEN_BIT
= {8,9,10,11,12,15,-1,-1,-1},
163 .VLAN_LOOKUP_ODD_BIT
= {0,1,2,3,4,7,-1,-1,-1},
165 .TAG_VLAN_MASK_REG
= {30,1}, // +N/2
166 .TAG_VLAN_MASK_EVEN_BIT
= {0,1,2,3,4,5,-1,-1,-1},
167 .TAG_VLAN_MASK_ODD_BIT
= {8,9,10,11,12,13,-1,-1,-1},
174 .ROUTER_CONTROL_REG
= {30,9},
176 .NUMLAN_GROUPS_MAX
= 8,
177 .NUMLAN_GROUPS_BIT
= 0, // {0-2}
179 .VLAN_CONTROL_REG
= {30,9},
185 .MII_REGISTER_EN
= NOTSUPPORTED
,
189 static const struct register_mappings IP175A
= {
192 .VLAN_DEFAULT_TAG_REG
= {
193 {0,24},{0,25},{0,26},{0,27},{0,28},NOTSUPPORTED
,
194 NOTSUPPORTED
,NOTSUPPORTED
,NOTSUPPORTED
197 .ADD_TAG_REG
= {0,23},
198 .REMOVE_TAG_REG
= {0,23},
199 .ADD_TAG_BIT
= {11,12,13,14,15,-1,-1,-1,-1},
200 .REMOVE_TAG_BIT
= {6,7,8,9,10,-1,-1,-1,-1},
202 .SIMPLE_VLAN_REGISTERS
= 0,
204 // Only programmable via EEPROM
205 .VLAN_LOOKUP_REG
= NOTSUPPORTED
,// +N/2
206 .VLAN_LOOKUP_REG_5
= NOTSUPPORTED
,
207 .VLAN_LOOKUP_EVEN_BIT
= {8,9,10,11,12,-1,-1,-1,-1},
208 .VLAN_LOOKUP_ODD_BIT
= {0,1,2,3,4,-1,-1,-1,-1},
210 .TAG_VLAN_MASK_REG
= NOTSUPPORTED
, // +N/2,
211 .TAG_VLAN_MASK_EVEN_BIT
= {-1,-1,-1,-1,-1,-1,-1,-1,-1},
212 .TAG_VLAN_MASK_ODD_BIT
= {-1,-1,-1,-1,-1,-1,-1,-1,-1},
215 .RESET_REG
= NOTSUPPORTED
,
217 .MODE_REG
= NOTSUPPORTED
,
219 .ROUTER_CONTROL_REG
= NOTSUPPORTED
,
220 .VLAN_CONTROL_REG
= NOTSUPPORTED
,
223 .NUMLAN_GROUPS_MAX
= -1,
224 .NUMLAN_GROUPS_BIT
= -1, // {0-2}
229 .MII_REGISTER_EN
= {0, 18},
230 .MII_REGISTER_EN_BIT
= 7,
234 struct ip175c_state
{
235 struct switch_dev dev
;
236 struct mii_bus
*mii_bus
;
239 int router_mode
; // ROUTER_EN
240 int vlan_enabled
; // TAG_VLAN_EN
243 unsigned int shareports
;
245 unsigned int add_tag
;
246 unsigned int remove_tag
;
248 unsigned int vlan_ports
[MAX_VLANS
];
249 const struct register_mappings
*regs
;
250 reg proc_mii
; // phy/reg for the low level register access via swconfig
256 static int ip_phy_read(struct ip175c_state
*state
, int port
, int reg
)
258 int val
= mdiobus_read(state
->mii_bus
, port
, reg
);
260 pr_warning("IP175C: Unable to get MII register %d,%d: error %d\n", port
, reg
, -val
);
263 pr_debug("IP175C: Read MII(%d,%d) -> %04x\n", port
, reg
, val
);
268 static int ip_phy_write(struct ip175c_state
*state
, int port
, int reg
, u16 val
)
273 pr_debug("IP175C: Write MII(%d,%d) <- %04x\n", port
, reg
, val
);
275 err
= mdiobus_write(state
->mii_bus
, port
, reg
, val
);
277 pr_warning("IP175C: Unable to write MII register %d,%d: error %d\n", port
, reg
, -err
);
281 static int ip_phy_write_masked(struct ip175c_state
*state
, int port
, int reg
, unsigned int mask
, unsigned int data
)
283 int val
= ip_phy_read(state
, port
, reg
);
286 return ip_phy_write(state
, port
, reg
, (val
& ~mask
) | data
);
289 static int getPhy(struct ip175c_state
*state
, reg mii
)
293 return ip_phy_read(state
, mii
.p
, mii
.m
);
296 static int setPhy(struct ip175c_state
*state
, reg mii
, u16 value
)
302 err
= ip_phy_write(state
, mii
.p
, mii
.m
, value
);
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); \
345 static int get_model(struct ip175c_state
*state
)
347 reg oui_id_reg
= {0, 2};
349 reg model_no_reg
= {0, 3};
350 int model_no
, model_no_orig
;
352 // 175 and 178 have the same oui ID.
353 reg oui_id_reg_178c
= {5, 2}; // returns error on IP175C.
356 oui_id
= getPhy(state
, oui_id_reg
);
357 if (oui_id
!= 0x0243) {
359 return -ENODEV
; // Not a IC+ chip.
361 oui_id
= getPhy(state
, oui_id_reg_178c
);
362 if (oui_id
== 0x0243) {
366 model_no_orig
= getPhy(state
, model_no_reg
);
367 if (model_no_orig
< 0) {
370 model_no
= model_no_orig
>> 4; // shift out revision number.
371 model_no
&= 0x3f; // only take the model number (low 6 bits).
372 if (model_no
== IP175A
.MODEL_NO
) {
373 state
->regs
= &IP175A
;
374 } else if (model_no
== IP175C
.MODEL_NO
) {
376 state
->regs
= &IP178C
;
378 state
->regs
= &IP175C
;
381 printk(KERN_WARNING
"ip175c: Found an unknown IC+ switch with model number %02Xh.\n", model_no_orig
);
387 /** Get only the vlan and router flags on the router **/
388 static int get_flags(struct ip175c_state
*state
)
392 state
->router_mode
= 0;
393 state
->vlan_enabled
= -1; // hack
394 state
->num_vlans
= 0;
396 if (!REG_SUPP(state
->regs
->ROUTER_CONTROL_REG
)) {
397 return 0; // not an error if it doesn't support enable vlan.
400 val
= getPhy(state
, state
->regs
->ROUTER_CONTROL_REG
);
404 if (state
->regs
->ROUTER_EN_BIT
>= 0)
405 state
->router_mode
= ((val
>>state
->regs
->ROUTER_EN_BIT
) & 1);
407 if (state
->regs
->NUMLAN_GROUPS_BIT
>= 0) {
408 state
->num_vlans
= (val
>> state
->regs
->NUMLAN_GROUPS_BIT
);
409 state
->num_vlans
&= (state
->regs
->NUMLAN_GROUPS_MAX
-1);
410 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);
423 /** Get all state variables for VLAN mappings and port-based tagging **/
424 static int get_state(struct ip175c_state
*state
)
428 ret
= get_flags(state
);
432 GET_PORT_BITS(state
, state
->remove_tag
,
433 state
->regs
->REMOVE_TAG_REG
, state
->regs
->REMOVE_TAG_BIT
);
434 GET_PORT_BITS(state
, state
->add_tag
,
435 state
->regs
->ADD_TAG_REG
, state
->regs
->ADD_TAG_BIT
);
437 if (state
->vlan_enabled
== -1) {
438 // not sure how to get this...
439 state
->vlan_enabled
= (state
->remove_tag
|| state
->add_tag
);
442 if (REG_SUPP(state
->regs
->VLAN_LOOKUP_REG
)) {
443 for (j
=0; j
<MAX_PORTS
; j
++) {
444 state
->ports
[j
].shareports
= 0; // initialize them in case.
446 for (j
=0; j
<state
->regs
->NUM_PORTS
; j
++) {
448 const bitnum
*bit_lookup
= (j
%2==0)?
449 state
->regs
->VLAN_LOOKUP_EVEN_BIT
:
450 state
->regs
->VLAN_LOOKUP_ODD_BIT
;
451 addr
= state
->regs
->VLAN_LOOKUP_REG
;
452 if (state
->regs
->SIMPLE_VLAN_REGISTERS
) {
467 addr
= state
->regs
->VLAN_LOOKUP_REG_5
;
472 if (REG_SUPP(addr
)) {
473 GET_PORT_BITS(state
, state
->ports
[j
].shareports
, addr
, bit_lookup
);
477 for (j
=0; j
<MAX_PORTS
; j
++) {
478 state
->ports
[j
].shareports
= 0xff;
482 for (i
=0; i
<MAX_PORTS
; i
++) {
483 if (REG_SUPP(state
->regs
->VLAN_DEFAULT_TAG_REG
[i
])) {
484 int val
= getPhy(state
, state
->regs
->VLAN_DEFAULT_TAG_REG
[i
]);
488 state
->ports
[i
].pvid
= val
;
490 state
->ports
[i
].pvid
= 0;
494 if (REG_SUPP(state
->regs
->TAG_VLAN_MASK_REG
)) {
495 for (j
=0; j
<MAX_VLANS
; j
++) {
496 reg addr
= state
->regs
->TAG_VLAN_MASK_REG
;
497 const bitnum
*bit_lookup
= (j
%2==0)?
498 state
->regs
->TAG_VLAN_MASK_EVEN_BIT
:
499 state
->regs
->TAG_VLAN_MASK_ODD_BIT
;
500 if (state
->regs
->SIMPLE_VLAN_REGISTERS
) {
505 GET_PORT_BITS(state
, state
->vlan_ports
[j
], addr
, bit_lookup
);
508 for (j
=0; j
<MAX_VLANS
; j
++) {
509 state
->vlan_ports
[j
] = 0;
510 for (i
=0; i
<state
->regs
->NUM_PORTS
; i
++) {
511 if ((state
->ports
[i
].pvid
== j
) ||
512 (state
->ports
[i
].pvid
== 0)) {
513 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
);
638 * Uses only the VLAN port mask and the add tag mask to generate the other fields:
639 * which ports are part of the same VLAN, removing vlan tags, and VLAN tag ids.
641 static void correct_vlan_state(struct ip175c_state
*state
)
644 state
->num_vlans
= 0;
645 for (i
=0; i
<MAX_VLANS
; i
++) {
646 if (state
->vlan_ports
[i
] != 0) {
647 state
->num_vlans
= i
+1; // Hack -- we need to store the "set" vlans somewhere...
651 for (i
=0; i
<state
->regs
->NUM_PORTS
; i
++) {
652 unsigned int portmask
= (1<<i
);
653 if (!state
->vlan_enabled
) {
654 // Share with everybody!
655 state
->ports
[i
].shareports
= (1<<state
->regs
->NUM_PORTS
)-1;
658 state
->ports
[i
].shareports
= portmask
;
659 for (j
=0; j
<MAX_VLANS
; j
++) {
660 if (state
->vlan_ports
[j
] & portmask
)
661 state
->ports
[i
].shareports
|= state
->vlan_ports
[j
];
664 state
->remove_tag
= ((~state
->add_tag
) & ((1<<state
->regs
->NUM_PORTS
)-1));
667 static int ip175c_get_enable_vlan(struct switch_dev
*dev
, const struct switch_attr
*attr
, struct switch_val
*val
)
669 struct ip175c_state
*state
= dev
->priv
;
672 err
= get_state(state
); // May be set in get_state.
675 val
->value
.i
= state
->vlan_enabled
;
679 static int ip175c_set_enable_vlan(struct switch_dev
*dev
, const struct switch_attr
*attr
, struct switch_val
*val
)
681 struct ip175c_state
*state
= dev
->priv
;
686 err
= get_state(state
);
689 enable
= val
->value
.i
;
691 if (state
->vlan_enabled
== enable
) {
692 // Do not change any state.
695 state
->vlan_enabled
= enable
;
697 // Otherwise, if we are switching state, set fields to a known default.
698 state
->remove_tag
= 0x0000;
699 state
->add_tag
= 0x0000;
700 for (i
= 0; i
< MAX_PORTS
; i
++)
701 state
->ports
[i
].shareports
= 0xffff;
703 for (i
= 0; i
< MAX_VLANS
; i
++)
704 state
->vlan_ports
[i
] = 0x0;
706 if (state
->vlan_enabled
) {
707 // Updates other fields only based off vlan_ports and add_tag fields.
708 // Note that by default, no ports are in any vlans.
709 correct_vlan_state(state
);
711 // Ensure sane defaults?
712 return update_state(state
);
715 static int ip175c_get_ports(struct switch_dev
*dev
, struct switch_val
*val
)
717 struct ip175c_state
*state
= dev
->priv
;
723 if (val
->port_vlan
>= dev
->vlans
|| val
->port_vlan
< 0)
726 err
= get_state(state
);
730 ports
= state
->vlan_ports
[val
->port_vlan
];
733 while (b
< MAX_PORTS
) {
735 int istagged
= ((state
->add_tag
>> b
) & 1);
736 val
->value
.ports
[ind
].id
= b
;
737 val
->value
.ports
[ind
].flags
= (istagged
<< SWITCH_PORT_FLAG_TAGGED
);
748 static int ip175c_set_ports(struct switch_dev
*dev
, struct switch_val
*val
)
750 struct ip175c_state
*state
= dev
->priv
;
754 if (val
->port_vlan
>= dev
->vlans
|| val
->port_vlan
< 0)
757 err
= get_state(state
);
761 state
->vlan_ports
[val
->port_vlan
] = 0;
762 for (i
= 0; i
< val
->len
; i
++) {
763 int bitmask
= (1<<val
->value
.ports
[i
].id
);
764 state
->vlan_ports
[val
->port_vlan
] |= bitmask
;
765 if (val
->value
.ports
[i
].flags
& (1<<SWITCH_PORT_FLAG_TAGGED
)) {
766 state
->add_tag
|= bitmask
;
768 state
->add_tag
&= (~bitmask
);
772 correct_vlan_state(state
);
773 err
= update_state(state
);
778 static int ip175c_apply(struct switch_dev
*dev
)
780 struct ip175c_state
*state
= dev
->priv
;
783 err
= get_flags(state
);
787 if (REG_SUPP(state
->regs
->MII_REGISTER_EN
)) {
788 int val
= getPhy(state
, state
->regs
->MII_REGISTER_EN
);
792 val
|= (1<<state
->regs
->MII_REGISTER_EN_BIT
);
793 return setPhy(state
, state
->regs
->MII_REGISTER_EN
, val
);
798 static int ip175c_reset(struct switch_dev
*dev
)
800 struct ip175c_state
*state
= dev
->priv
;
803 err
= get_flags(state
);
807 if (REG_SUPP(state
->regs
->RESET_REG
)) {
808 err
= setPhy(state
, state
->regs
->RESET_REG
, state
->regs
->RESET_VAL
);
811 err
= getPhy(state
, state
->regs
->RESET_REG
);
814 * Data sheet specifies reset period to be 2 msec.
815 * (I don't see any mention of the 2ms delay in the IP178C spec, only
816 * in IP175C, but it can't hurt.)
821 if (REG_SUPP(state
->regs
->MODE_REG
)) {
822 err
= setPhy(state
, state
->regs
->MODE_REG
, state
->regs
->RESET_VAL
);
825 err
= getPhy(state
, state
->regs
->MODE_REG
);
828 /* reset switch ports */
829 for (i
= 0; i
< state
->regs
->NUM_PORTS
-1; i
++) {
830 err
= ip_phy_write(state
, i
, MII_BMCR
, BMCR_RESET
);
838 static int ip175c_get_tagged(struct switch_dev
*dev
, const struct switch_attr
*attr
, struct switch_val
*val
)
840 struct ip175c_state
*state
= dev
->priv
;
843 err
= get_state(state
);
847 if (state
->add_tag
& (1<<val
->port_vlan
)) {
848 if (state
->remove_tag
& (1<<val
->port_vlan
))
849 val
->value
.i
= 3; // shouldn't ever happen.
853 if (state
->remove_tag
& (1<<val
->port_vlan
))
861 static int ip175c_set_tagged(struct switch_dev
*dev
, const struct switch_attr
*attr
, struct switch_val
*val
)
863 struct ip175c_state
*state
= dev
->priv
;
866 err
= get_state(state
);
870 state
->add_tag
&= ~(1<<val
->port_vlan
);
871 state
->remove_tag
&= ~(1<<val
->port_vlan
);
873 if (val
->value
.i
== 0)
874 state
->remove_tag
|= (1<<val
->port_vlan
);
875 if (val
->value
.i
== 1)
876 state
->add_tag
|= (1<<val
->port_vlan
);
878 SET_PORT_BITS(state
, state
->add_tag
,
879 state
->regs
->ADD_TAG_REG
, state
->regs
->ADD_TAG_BIT
);
880 SET_PORT_BITS(state
, state
->remove_tag
,
881 state
->regs
->REMOVE_TAG_REG
, state
->regs
->REMOVE_TAG_BIT
);
886 /** Get the current phy address */
887 static int ip175c_get_phy(struct switch_dev
*dev
, const struct switch_attr
*attr
, struct switch_val
*val
)
889 struct ip175c_state
*state
= dev
->priv
;
891 val
->value
.i
= state
->proc_mii
.p
;
895 /** Set a new phy address for low level access to registers */
896 static int ip175c_set_phy(struct switch_dev
*dev
, const struct switch_attr
*attr
, struct switch_val
*val
)
898 struct ip175c_state
*state
= dev
->priv
;
899 int new_reg
= val
->value
.i
;
901 if (new_reg
< 0 || new_reg
> 31)
902 state
->proc_mii
.p
= (u16
)-1;
904 state
->proc_mii
.p
= (u16
)new_reg
;
908 /** Get the current register number */
909 static int ip175c_get_reg(struct switch_dev
*dev
, const struct switch_attr
*attr
, struct switch_val
*val
)
911 struct ip175c_state
*state
= dev
->priv
;
913 val
->value
.i
= state
->proc_mii
.m
;
917 /** Set a new register address for low level access to registers */
918 static int ip175c_set_reg(struct switch_dev
*dev
, const struct switch_attr
*attr
, struct switch_val
*val
)
920 struct ip175c_state
*state
= dev
->priv
;
921 int new_reg
= val
->value
.i
;
923 if (new_reg
< 0 || new_reg
> 31)
924 state
->proc_mii
.m
= (u16
)-1;
926 state
->proc_mii
.m
= (u16
)new_reg
;
930 /** Get the register content of state->proc_mii */
931 static int ip175c_get_val(struct switch_dev
*dev
, const struct switch_attr
*attr
, struct switch_val
*val
)
933 struct ip175c_state
*state
= dev
->priv
;
934 int retval
= -EINVAL
;
935 if (REG_SUPP(state
->proc_mii
))
936 retval
= getPhy(state
, state
->proc_mii
);
941 val
->value
.i
= retval
;
946 /** Write a value to the register defined by phy/reg above */
947 static int ip175c_set_val(struct switch_dev
*dev
, const struct switch_attr
*attr
, struct switch_val
*val
)
949 struct ip175c_state
*state
= dev
->priv
;
952 myval
= val
->value
.i
;
953 if (myval
<= 0xffff && myval
>= 0 && REG_SUPP(state
->proc_mii
)) {
954 err
= setPhy(state
, state
->proc_mii
, (u16
)myval
);
959 static int ip175c_read_name(struct switch_dev
*dev
, const struct switch_attr
*attr
, struct switch_val
*val
)
961 struct ip175c_state
*state
= dev
->priv
;
962 val
->value
.s
= state
->regs
->NAME
; // Just a const pointer, won't be freed by swconfig.
966 static int ip175c_set_port_speed(struct switch_dev
*dev
, const struct switch_attr
*attr
, struct switch_val
*val
)
968 struct ip175c_state
*state
= dev
->priv
;
969 int nr
= val
->port_vlan
;
973 if (val
->value
.i
== 100) {
976 } else if (val
->value
.i
== 10) {
984 /* Can't set speed for cpu port */
985 if (nr
== state
->regs
->CPU_PORT
)
988 if (nr
>= dev
->ports
|| nr
< 0)
991 ctrl
= ip_phy_read(state
, nr
, 0);
997 ctrl
|= (autoneg
<<12);
1000 return ip_phy_write(state
, nr
, 0, ctrl
);
1003 static int ip175c_get_port_speed(struct switch_dev
*dev
, const struct switch_attr
*attr
, struct switch_val
*val
)
1005 struct ip175c_state
*state
= dev
->priv
;
1006 int nr
= val
->port_vlan
;
1009 if (nr
== state
->regs
->CPU_PORT
) {
1014 if (nr
>= dev
->ports
|| nr
< 0)
1017 status
= ip_phy_read(state
, nr
, 1);
1018 speed
= ip_phy_read(state
, nr
, 18);
1019 if (status
< 0 || speed
< 0)
1023 val
->value
.i
= ((speed
& (1<<11)) ? 100 : 10);
1030 static int ip175c_get_port_status(struct switch_dev
*dev
, const struct switch_attr
*attr
, struct switch_val
*val
)
1032 struct ip175c_state
*state
= dev
->priv
;
1033 int ctrl
, speed
, status
;
1034 int nr
= val
->port_vlan
;
1036 char *buf
= state
->buf
; // fixed-length at 80.
1038 if (nr
== state
->regs
->CPU_PORT
) {
1039 sprintf(buf
, "up, 100 Mbps, cpu port");
1044 if (nr
>= dev
->ports
|| nr
< 0)
1047 ctrl
= ip_phy_read(state
, nr
, 0);
1048 status
= ip_phy_read(state
, nr
, 1);
1049 speed
= ip_phy_read(state
, nr
, 18);
1050 if (ctrl
< 0 || status
< 0 || speed
< 0)
1054 len
= sprintf(buf
, "up, %d Mbps, %s duplex",
1055 ((speed
& (1<<11)) ? 100 : 10),
1056 ((speed
& (1<<10)) ? "full" : "half"));
1058 len
= sprintf(buf
, "down");
1060 if (ctrl
& (1<<12)) {
1061 len
+= sprintf(buf
+len
, ", auto-negotiate");
1062 if (!(status
& (1<<5)))
1063 len
+= sprintf(buf
+len
, " (in progress)");
1065 len
+= sprintf(buf
+len
, ", fixed speed (%d)",
1066 ((ctrl
& (1<<13)) ? 100 : 10));
1074 static int ip175c_get_pvid(struct switch_dev
*dev
, int port
, int *val
)
1076 struct ip175c_state
*state
= dev
->priv
;
1078 *val
= state
->ports
[port
].pvid
;
1082 static int ip175c_set_pvid(struct switch_dev
*dev
, int port
, int val
)
1084 struct ip175c_state
*state
= dev
->priv
;
1086 state
->ports
[port
].pvid
= val
;
1088 if (!REG_SUPP(state
->regs
->VLAN_DEFAULT_TAG_REG
[port
]))
1091 return setPhy(state
, state
->regs
->VLAN_DEFAULT_TAG_REG
[port
], val
);
1105 IP175C_REGISTER_PHY
,
1106 IP175C_REGISTER_MII
,
1107 IP175C_REGISTER_VALUE
,
1108 IP175C_REGISTER_ERRNO
,
1111 static const struct switch_attr ip175c_global
[] = {
1112 [IP175C_ENABLE_VLAN
] = {
1113 .id
= IP175C_ENABLE_VLAN
,
1114 .type
= SWITCH_TYPE_INT
,
1115 .name
= "enable_vlan",
1116 .description
= "Flag to enable or disable VLANs and tagging",
1117 .get
= ip175c_get_enable_vlan
,
1118 .set
= ip175c_set_enable_vlan
,
1120 [IP175C_GET_NAME
] = {
1121 .id
= IP175C_GET_NAME
,
1122 .type
= SWITCH_TYPE_STRING
,
1123 .description
= "Returns the type of IC+ chip.",
1125 .get
= ip175c_read_name
,
1128 /* jal: added for low level debugging etc. */
1129 [IP175C_REGISTER_PHY
] = {
1130 .id
= IP175C_REGISTER_PHY
,
1131 .type
= SWITCH_TYPE_INT
,
1132 .description
= "Direct register access: set PHY (0-4, or 29,30,31)",
1134 .get
= ip175c_get_phy
,
1135 .set
= ip175c_set_phy
,
1137 [IP175C_REGISTER_MII
] = {
1138 .id
= IP175C_REGISTER_MII
,
1139 .type
= SWITCH_TYPE_INT
,
1140 .description
= "Direct register access: set MII register number (0-31)",
1142 .get
= ip175c_get_reg
,
1143 .set
= ip175c_set_reg
,
1145 [IP175C_REGISTER_VALUE
] = {
1146 .id
= IP175C_REGISTER_VALUE
,
1147 .type
= SWITCH_TYPE_INT
,
1148 .description
= "Direct register access: read/write to register (0-65535)",
1150 .get
= ip175c_get_val
,
1151 .set
= ip175c_set_val
,
1155 static const struct switch_attr ip175c_vlan
[] = {
1158 static const struct switch_attr ip175c_port
[] = {
1159 [IP175C_PORT_STATUS
] = {
1160 .id
= IP175C_PORT_STATUS
,
1161 .type
= SWITCH_TYPE_STRING
,
1162 .description
= "Returns Detailed port status",
1164 .get
= ip175c_get_port_status
,
1167 [IP175C_PORT_LINK
] = {
1168 .id
= IP175C_PORT_LINK
,
1169 .type
= SWITCH_TYPE_INT
,
1170 .description
= "Link speed. Can write 0 for auto-negotiate, or 10 or 100",
1172 .get
= ip175c_get_port_speed
,
1173 .set
= ip175c_set_port_speed
,
1175 [IP175C_PORT_TAGGED
] = {
1176 .id
= IP175C_PORT_LINK
,
1177 .type
= SWITCH_TYPE_INT
,
1178 .description
= "0 = untag, 1 = add tags, 2 = do not alter (This value is reset if vlans are altered)",
1180 .get
= ip175c_get_tagged
,
1181 .set
= ip175c_set_tagged
,
1185 static int ip175c_probe(struct phy_device
*pdev
)
1187 struct ip175c_state
*state
;
1188 struct switch_dev
*dev
;
1191 /* We only attach to PHY 0, but use all available PHYs */
1192 if (pdev
->addr
!= 0)
1195 state
= kzalloc(sizeof(*state
), GFP_KERNEL
);
1200 dev
->attr_global
.attr
= ip175c_global
;
1201 dev
->attr_global
.n_attr
= ARRAY_SIZE(ip175c_global
);
1202 dev
->attr_port
.attr
= ip175c_port
;
1203 dev
->attr_port
.n_attr
= ARRAY_SIZE(ip175c_port
);
1204 dev
->attr_vlan
.attr
= ip175c_vlan
;
1205 dev
->attr_vlan
.n_attr
= ARRAY_SIZE(ip175c_vlan
);
1207 dev
->get_port_pvid
= ip175c_get_pvid
;
1208 dev
->set_port_pvid
= ip175c_set_pvid
;
1209 dev
->get_vlan_ports
= ip175c_get_ports
;
1210 dev
->set_vlan_ports
= ip175c_set_ports
;
1211 dev
->apply_config
= ip175c_apply
;
1212 dev
->reset_switch
= ip175c_reset
;
1216 state
->mii_bus
= pdev
->bus
;
1218 err
= get_model(state
);
1222 dev
->vlans
= MAX_VLANS
;
1223 dev
->cpu_port
= state
->regs
->CPU_PORT
;
1224 dev
->ports
= state
->regs
->NUM_PORTS
;
1225 dev
->name
= state
->regs
->NAME
;
1234 static int ip175c_config_init(struct phy_device
*pdev
)
1236 struct ip175c_state
*state
= pdev
->priv
;
1237 struct net_device
*dev
= pdev
->attached_dev
;
1240 pdev
->irq
= PHY_IGNORE_INTERRUPT
;
1241 err
= register_switch(&state
->dev
, dev
);
1245 ip175c_reset(&state
->dev
);
1247 state
->registered
= true;
1248 netif_carrier_on(pdev
->attached_dev
);
1253 static void ip175c_remove(struct phy_device
*pdev
)
1255 struct ip175c_state
*state
= pdev
->priv
;
1257 if (state
->registered
)
1258 unregister_switch(&state
->dev
);
1262 static int ip175c_config_aneg(struct phy_device
*pdev
)
1267 static int ip175c_read_status(struct phy_device
*pdev
)
1269 pdev
->speed
= SPEED_100
;
1270 pdev
->duplex
= DUPLEX_FULL
;
1271 pdev
->pause
= pdev
->asym_pause
= 0;
1275 static struct phy_driver ip175c_driver
= {
1276 .name
= "IC+ IP175C",
1277 .phy_id
= 0x02430d80,
1278 .phy_id_mask
= 0x0ffffff0,
1279 .features
= PHY_BASIC_FEATURES
,
1280 .probe
= ip175c_probe
,
1281 .remove
= ip175c_remove
,
1282 .config_init
= ip175c_config_init
,
1283 .config_aneg
= ip175c_config_aneg
,
1284 .read_status
= ip175c_read_status
,
1285 .driver
= { .owner
= THIS_MODULE
},
1288 static struct phy_driver ip175a_driver
= {
1289 .name
= "IC+ IP175A",
1290 .phy_id
= 0x02430c50,
1291 .phy_id_mask
= 0x0ffffff0,
1292 .features
= PHY_BASIC_FEATURES
,
1293 .probe
= ip175c_probe
,
1294 .remove
= ip175c_remove
,
1295 .config_init
= ip175c_config_init
,
1296 .config_aneg
= ip175c_config_aneg
,
1297 .read_status
= ip175c_read_status
,
1298 .driver
= { .owner
= THIS_MODULE
},
1302 int __init
ip175c_init(void)
1306 ret
= phy_driver_register(&ip175a_driver
);
1310 return phy_driver_register(&ip175c_driver
);
1313 void __exit
ip175c_exit(void)
1315 phy_driver_unregister(&ip175c_driver
);
1316 phy_driver_unregister(&ip175a_driver
);
1319 MODULE_AUTHOR("Patrick Horn <patrick.horn@gmail.com>");
1320 MODULE_AUTHOR("Felix Fietkau <nbd@openwrt.org>");
1321 MODULE_LICENSE("GPL");
1323 module_init(ip175c_init
);
1324 module_exit(ip175c_exit
);