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>
28 #include <linux/device.h>
34 typedef struct ip175c_reg
{
40 #define NOTSUPPORTED {-1,-1}
42 #define REG_SUPP(x) (((x).m != ((u16)-1)) && ((x).p != (u16)-1))
46 /*********** CONSTANTS ***********/
47 struct register_mappings
{
49 u16 MODEL_NO
; // Compare to bits 4-9 of MII register 0,3.
53 /* The default VLAN for each port.
54 Default: 0x0001 for Ports 0,1,2,3
55 0x0002 for Ports 4,5 */
56 reg VLAN_DEFAULT_TAG_REG
[MAX_PORTS
];
58 /* These ports are tagged.
62 bitnum ADD_TAG_BIT
[MAX_PORTS
];
63 /* These ports are untagged.
64 Default: 0x00 (i.e. do not alter any VLAN tags...)
65 Maybe set to 0 if user disables VLANs. */
66 bitnum REMOVE_TAG_BIT
[MAX_PORTS
];
68 /* Port M and Port N are on the same VLAN.
69 Default: All ports on all VLANs. */
70 // Use register {29, 19+N/2}
72 // Port 5 uses register {30, 18} but same as odd bits.
73 reg VLAN_LOOKUP_REG_5
; // in a different register on IP175C.
74 bitnum VLAN_LOOKUP_EVEN_BIT
[MAX_PORTS
];
75 bitnum VLAN_LOOKUP_ODD_BIT
[MAX_PORTS
];
77 /* This VLAN corresponds to which ports.
78 Default: 0x2f,0x30,0x3f,0x3f... */
79 reg TAG_VLAN_MASK_REG
;
80 bitnum TAG_VLAN_MASK_EVEN_BIT
[MAX_PORTS
];
81 bitnum TAG_VLAN_MASK_ODD_BIT
[MAX_PORTS
];
90 reg ROUTER_CONTROL_REG
;
94 bitnum NUMLAN_GROUPS_MAX
;
95 bitnum NUMLAN_GROUPS_BIT
;
98 bitnum MII_REGISTER_EN_BIT
;
100 // set to 1 for 178C, 0 for 175C.
101 bitnum SIMPLE_VLAN_REGISTERS
; // 175C has two vlans per register but 178C has only one.
103 // Pointers to functions which manipulate hardware state
104 int (*get_flags
)(struct ip175c_state
*state
);
105 int (*get_state
)(struct ip175c_state
*state
);
106 int (*update_state
)(struct ip175c_state
*state
);
107 int (*set_vlan_mode
)(struct ip175c_state
*state
);
108 int (*reset
)(struct ip175c_state
*state
);
111 static int ip175c_get_flags(struct ip175c_state
*state
);
112 static int ip175c_get_state(struct ip175c_state
*state
);
113 static int ip175c_update_state(struct ip175c_state
*state
);
114 static int ip175c_set_vlan_mode(struct ip175c_state
*state
);
115 static int ip175c_do_reset(struct ip175c_state
*state
);
117 static const struct register_mappings IP178C
= {
120 .VLAN_DEFAULT_TAG_REG
= {
121 {30,3},{30,4},{30,5},{30,6},{30,7},{30,8},
122 {30,9},{30,10},{30,11},
125 .ADD_TAG_REG
= {30,12},
126 .ADD_TAG_BIT
= {0,1,2,3,4,5,6,7,8},
127 .REMOVE_TAG_REG
= {30,13},
128 .REMOVE_TAG_BIT
= {4,5,6,7,8,9,10,11,12},
130 .SIMPLE_VLAN_REGISTERS
= 1,
132 .VLAN_LOOKUP_REG
= {31,0},// +N
133 .VLAN_LOOKUP_REG_5
= NOTSUPPORTED
, // not used with SIMPLE_VLAN_REGISTERS
134 .VLAN_LOOKUP_EVEN_BIT
= {0,1,2,3,4,5,6,7,8},
135 .VLAN_LOOKUP_ODD_BIT
= {0,1,2,3,4,5,6,7,8},
137 .TAG_VLAN_MASK_REG
= {30,14}, // +N
138 .TAG_VLAN_MASK_EVEN_BIT
= {0,1,2,3,4,5,6,7,8},
139 .TAG_VLAN_MASK_ODD_BIT
= {0,1,2,3,4,5,6,7,8},
144 .MODE_REG
= NOTSUPPORTED
,
146 .ROUTER_CONTROL_REG
= {30,30},
148 .NUMLAN_GROUPS_MAX
= 8,
149 .NUMLAN_GROUPS_BIT
= 8, // {0-2}
151 .VLAN_CONTROL_REG
= {30,13},
157 .MII_REGISTER_EN
= NOTSUPPORTED
,
159 .get_flags
= ip175c_get_flags
,
160 .get_state
= ip175c_get_state
,
161 .update_state
= ip175c_update_state
,
162 .set_vlan_mode
= ip175c_set_vlan_mode
,
163 .reset
= ip175c_do_reset
,
166 static const struct register_mappings IP175C
= {
169 .VLAN_DEFAULT_TAG_REG
= {
170 {29,24},{29,25},{29,26},{29,27},{29,28},{29,30},
171 NOTSUPPORTED
,NOTSUPPORTED
,NOTSUPPORTED
174 .ADD_TAG_REG
= {29,23},
175 .REMOVE_TAG_REG
= {29,23},
176 .ADD_TAG_BIT
= {11,12,13,14,15,1,-1,-1,-1},
177 .REMOVE_TAG_BIT
= {6,7,8,9,10,0,-1,-1,-1},
179 .SIMPLE_VLAN_REGISTERS
= 0,
181 .VLAN_LOOKUP_REG
= {29,19},// +N/2
182 .VLAN_LOOKUP_REG_5
= {30,18},
183 .VLAN_LOOKUP_EVEN_BIT
= {8,9,10,11,12,15,-1,-1,-1},
184 .VLAN_LOOKUP_ODD_BIT
= {0,1,2,3,4,7,-1,-1,-1},
186 .TAG_VLAN_MASK_REG
= {30,1}, // +N/2
187 .TAG_VLAN_MASK_EVEN_BIT
= {0,1,2,3,4,5,-1,-1,-1},
188 .TAG_VLAN_MASK_ODD_BIT
= {8,9,10,11,12,13,-1,-1,-1},
195 .ROUTER_CONTROL_REG
= {30,9},
197 .NUMLAN_GROUPS_MAX
= 8,
198 .NUMLAN_GROUPS_BIT
= 0, // {0-2}
200 .VLAN_CONTROL_REG
= {30,9},
206 .MII_REGISTER_EN
= NOTSUPPORTED
,
208 .get_flags
= ip175c_get_flags
,
209 .get_state
= ip175c_get_state
,
210 .update_state
= ip175c_update_state
,
211 .set_vlan_mode
= ip175c_set_vlan_mode
,
212 .reset
= ip175c_do_reset
,
215 static const struct register_mappings IP175A
= {
218 .VLAN_DEFAULT_TAG_REG
= {
219 {0,24},{0,25},{0,26},{0,27},{0,28},NOTSUPPORTED
,
220 NOTSUPPORTED
,NOTSUPPORTED
,NOTSUPPORTED
223 .ADD_TAG_REG
= {0,23},
224 .REMOVE_TAG_REG
= {0,23},
225 .ADD_TAG_BIT
= {11,12,13,14,15,-1,-1,-1,-1},
226 .REMOVE_TAG_BIT
= {6,7,8,9,10,-1,-1,-1,-1},
228 .SIMPLE_VLAN_REGISTERS
= 0,
230 // Only programmable via EEPROM
231 .VLAN_LOOKUP_REG
= NOTSUPPORTED
,// +N/2
232 .VLAN_LOOKUP_REG_5
= NOTSUPPORTED
,
233 .VLAN_LOOKUP_EVEN_BIT
= {8,9,10,11,12,-1,-1,-1,-1},
234 .VLAN_LOOKUP_ODD_BIT
= {0,1,2,3,4,-1,-1,-1,-1},
236 .TAG_VLAN_MASK_REG
= NOTSUPPORTED
, // +N/2,
237 .TAG_VLAN_MASK_EVEN_BIT
= {-1,-1,-1,-1,-1,-1,-1,-1,-1},
238 .TAG_VLAN_MASK_ODD_BIT
= {-1,-1,-1,-1,-1,-1,-1,-1,-1},
241 .RESET_REG
= NOTSUPPORTED
,
243 .MODE_REG
= NOTSUPPORTED
,
245 .ROUTER_CONTROL_REG
= NOTSUPPORTED
,
246 .VLAN_CONTROL_REG
= NOTSUPPORTED
,
249 .NUMLAN_GROUPS_MAX
= -1,
250 .NUMLAN_GROUPS_BIT
= -1, // {0-2}
255 .MII_REGISTER_EN
= {0, 18},
256 .MII_REGISTER_EN_BIT
= 7,
258 .get_flags
= ip175c_get_flags
,
259 .get_state
= ip175c_get_state
,
260 .update_state
= ip175c_update_state
,
261 .set_vlan_mode
= ip175c_set_vlan_mode
,
262 .reset
= ip175c_do_reset
,
266 struct ip175c_state
{
267 struct switch_dev dev
;
268 struct mii_bus
*mii_bus
;
271 int router_mode
; // ROUTER_EN
272 int vlan_enabled
; // TAG_VLAN_EN
275 unsigned int shareports
;
277 unsigned int add_tag
;
278 unsigned int remove_tag
;
282 unsigned int tag
; // VLAN tag (IP175D only)
284 const struct register_mappings
*regs
;
285 reg proc_mii
; // phy/reg for the low level register access via swconfig
291 static int ip_phy_read(struct ip175c_state
*state
, int port
, int reg
)
293 int val
= mdiobus_read(state
->mii_bus
, port
, reg
);
295 pr_warning("IP175C: Unable to get MII register %d,%d: error %d\n", port
, reg
, -val
);
298 pr_debug("IP175C: Read MII(%d,%d) -> %04x\n", port
, reg
, val
);
303 static int ip_phy_write(struct ip175c_state
*state
, int port
, int reg
, u16 val
)
308 pr_debug("IP175C: Write MII(%d,%d) <- %04x\n", port
, reg
, val
);
310 err
= mdiobus_write(state
->mii_bus
, port
, reg
, val
);
312 pr_warning("IP175C: Unable to write MII register %d,%d: error %d\n", port
, reg
, -err
);
316 static int ip_phy_write_masked(struct ip175c_state
*state
, int port
, int reg
, unsigned int mask
, unsigned int data
)
318 int val
= ip_phy_read(state
, port
, reg
);
321 return ip_phy_write(state
, port
, reg
, (val
& ~mask
) | data
);
324 static int getPhy(struct ip175c_state
*state
, reg mii
)
328 return ip_phy_read(state
, mii
.p
, mii
.m
);
331 static int setPhy(struct ip175c_state
*state
, reg mii
, u16 value
)
337 err
= ip_phy_write(state
, mii
.p
, mii
.m
, value
);
346 * These two macros are to simplify the mapping of logical bits to the bits in hardware.
347 * NOTE: these macros will return if there is an error!
350 #define GET_PORT_BITS(state, bits, addr, bit_lookup) \
352 int i, val = getPhy((state), (addr)); \
356 for (i = 0; i < MAX_PORTS; i++) { \
357 if ((bit_lookup)[i] == -1) continue; \
358 if (val & (1<<(bit_lookup)[i])) \
363 #define SET_PORT_BITS(state, bits, addr, bit_lookup) \
365 int i, val = getPhy((state), (addr)); \
368 for (i = 0; i < MAX_PORTS; i++) { \
369 unsigned int newmask = ((bits)&(1<<i)); \
370 if ((bit_lookup)[i] == -1) continue; \
371 val &= ~(1<<(bit_lookup)[i]); \
372 val |= ((newmask>>i)<<(bit_lookup)[i]); \
374 val = setPhy((state), (addr), val); \
380 static int get_model(struct ip175c_state
*state
)
383 int oui_id
, model_no
, rev_no
, chip_no
;
385 id1
= ip_phy_read(state
, 0, 2);
386 id2
= ip_phy_read(state
, 0, 3);
387 oui_id
= (id1
<< 6) | ((id2
>> 10) & 0x3f);
388 model_no
= (id2
>> 4) & 0x3f;
390 pr_debug("IP175C: Identified oui=%06x model=%02x rev=%X\n", oui_id
, model_no
, rev_no
);
392 if (oui_id
!= 0x0090c3) // No other oui_id should have reached us anyway
395 if (model_no
== IP175A
.MODEL_NO
) {
396 state
->regs
= &IP175A
;
397 } else if (model_no
== IP175C
.MODEL_NO
) {
399 * Several models share the same model_no:
400 * 178C has more PHYs, so we try whether the device responds to a read from PHY5
401 * 175D has a new chip ID register
404 if (ip_phy_read(state
, 5, 2) == 0x0243) {
405 state
->regs
= &IP178C
;
407 chip_no
= ip_phy_read(state
, 20, 0);
408 pr_debug("IP175C: Chip ID register reads %04x\n", chip_no
);
409 if (chip_no
== 0x175d) {
410 state
->regs
= &IP175C
;
412 state
->regs
= &IP175C
;
416 pr_warning("IP175C: Found an unknown IC+ switch with model number %02x, revision %X.\n", model_no
, rev_no
);
422 /** Get only the vlan and router flags on the router **/
423 static int ip175c_get_flags(struct ip175c_state
*state
)
427 state
->router_mode
= 0;
428 state
->vlan_enabled
= -1; // hack
429 state
->num_vlans
= 0;
431 if (!REG_SUPP(state
->regs
->ROUTER_CONTROL_REG
)) {
432 return 0; // not an error if it doesn't support enable vlan.
435 val
= getPhy(state
, state
->regs
->ROUTER_CONTROL_REG
);
439 if (state
->regs
->ROUTER_EN_BIT
>= 0)
440 state
->router_mode
= ((val
>>state
->regs
->ROUTER_EN_BIT
) & 1);
442 if (state
->regs
->NUMLAN_GROUPS_BIT
>= 0) {
443 state
->num_vlans
= (val
>> state
->regs
->NUMLAN_GROUPS_BIT
);
444 state
->num_vlans
&= (state
->regs
->NUMLAN_GROUPS_MAX
-1);
445 state
->num_vlans
+=1; // does not include WAN.
448 val
= getPhy(state
, state
->regs
->VLAN_CONTROL_REG
);
452 if (state
->regs
->TAG_VLAN_BIT
>= 0)
453 state
->vlan_enabled
= ((val
>>state
->regs
->TAG_VLAN_BIT
) & 1);
458 /** Get all state variables for VLAN mappings and port-based tagging **/
459 static int ip175c_get_state(struct ip175c_state
*state
)
464 ret
= ip175c_get_flags(state
);
469 GET_PORT_BITS(state
, state
->remove_tag
,
470 state
->regs
->REMOVE_TAG_REG
, state
->regs
->REMOVE_TAG_BIT
);
471 GET_PORT_BITS(state
, state
->add_tag
,
472 state
->regs
->ADD_TAG_REG
, state
->regs
->ADD_TAG_BIT
);
474 if (state
->vlan_enabled
== -1) {
475 // not sure how to get this...
476 state
->vlan_enabled
= (state
->remove_tag
|| state
->add_tag
);
479 if (REG_SUPP(state
->regs
->VLAN_LOOKUP_REG
)) {
480 for (j
=0; j
<MAX_PORTS
; j
++) {
481 state
->ports
[j
].shareports
= 0; // initialize them in case.
483 for (j
=0; j
<state
->regs
->NUM_PORTS
; j
++) {
485 const bitnum
*bit_lookup
= (j
%2==0)?
486 state
->regs
->VLAN_LOOKUP_EVEN_BIT
:
487 state
->regs
->VLAN_LOOKUP_ODD_BIT
;
488 addr
= state
->regs
->VLAN_LOOKUP_REG
;
489 if (state
->regs
->SIMPLE_VLAN_REGISTERS
) {
504 addr
= state
->regs
->VLAN_LOOKUP_REG_5
;
509 if (REG_SUPP(addr
)) {
510 GET_PORT_BITS(state
, state
->ports
[j
].shareports
, addr
, bit_lookup
);
514 for (j
=0; j
<MAX_PORTS
; j
++) {
515 state
->ports
[j
].shareports
= 0xff;
519 for (i
=0; i
<MAX_PORTS
; i
++) {
520 if (REG_SUPP(state
->regs
->VLAN_DEFAULT_TAG_REG
[i
])) {
521 int val
= getPhy(state
, state
->regs
->VLAN_DEFAULT_TAG_REG
[i
]);
525 state
->ports
[i
].pvid
= val
;
527 state
->ports
[i
].pvid
= 0;
531 if (REG_SUPP(state
->regs
->TAG_VLAN_MASK_REG
)) {
532 for (j
=0; j
<MAX_VLANS
; j
++) {
533 reg addr
= state
->regs
->TAG_VLAN_MASK_REG
;
534 const bitnum
*bit_lookup
= (j
%2==0)?
535 state
->regs
->TAG_VLAN_MASK_EVEN_BIT
:
536 state
->regs
->TAG_VLAN_MASK_ODD_BIT
;
537 if (state
->regs
->SIMPLE_VLAN_REGISTERS
) {
542 GET_PORT_BITS(state
, state
->vlans
[j
].ports
, addr
, bit_lookup
);
545 for (j
=0; j
<MAX_VLANS
; j
++) {
546 state
->vlans
[j
].ports
= 0;
547 for (i
=0; i
<state
->regs
->NUM_PORTS
; i
++) {
548 if ((state
->ports
[i
].pvid
== j
) ||
549 (state
->ports
[i
].pvid
== 0)) {
550 state
->vlans
[j
].ports
|= (1<<i
);
559 /** Only set vlan and router flags in the switch **/
560 static int ip175c_set_flags(struct ip175c_state
*state
)
564 if (!REG_SUPP(state
->regs
->ROUTER_CONTROL_REG
)) {
568 val
= getPhy(state
, state
->regs
->ROUTER_CONTROL_REG
);
572 if (state
->regs
->ROUTER_EN_BIT
>= 0) {
573 if (state
->router_mode
) {
574 val
|= (1<<state
->regs
->ROUTER_EN_BIT
);
576 val
&= (~(1<<state
->regs
->ROUTER_EN_BIT
));
579 if (state
->regs
->TAG_VLAN_BIT
>= 0) {
580 if (state
->vlan_enabled
) {
581 val
|= (1<<state
->regs
->TAG_VLAN_BIT
);
583 val
&= (~(1<<state
->regs
->TAG_VLAN_BIT
));
586 if (state
->regs
->NUMLAN_GROUPS_BIT
>= 0) {
587 val
&= (~((state
->regs
->NUMLAN_GROUPS_MAX
-1)<<state
->regs
->NUMLAN_GROUPS_BIT
));
588 if (state
->num_vlans
> state
->regs
->NUMLAN_GROUPS_MAX
) {
589 val
|= state
->regs
->NUMLAN_GROUPS_MAX
<< state
->regs
->NUMLAN_GROUPS_BIT
;
590 } else if (state
->num_vlans
>= 1) {
591 val
|= (state
->num_vlans
-1) << state
->regs
->NUMLAN_GROUPS_BIT
;
594 return setPhy(state
, state
->regs
->ROUTER_CONTROL_REG
, val
);
597 /** Set all VLAN and port state. Usually you should call "correct_vlan_state" first. **/
598 static int ip175c_set_state(struct ip175c_state
*state
)
602 SET_PORT_BITS(state
, state
->add_tag
,
603 state
->regs
->ADD_TAG_REG
, state
->regs
->ADD_TAG_BIT
);
604 SET_PORT_BITS(state
, state
->remove_tag
,
605 state
->regs
->REMOVE_TAG_REG
, state
->regs
->REMOVE_TAG_BIT
);
607 if (REG_SUPP(state
->regs
->VLAN_LOOKUP_REG
)) {
608 for (j
=0; j
<state
->regs
->NUM_PORTS
; j
++) {
610 const bitnum
*bit_lookup
= (j
%2==0)?
611 state
->regs
->VLAN_LOOKUP_EVEN_BIT
:
612 state
->regs
->VLAN_LOOKUP_ODD_BIT
;
614 // duplicate code -- sorry
615 addr
= state
->regs
->VLAN_LOOKUP_REG
;
616 if (state
->regs
->SIMPLE_VLAN_REGISTERS
) {
631 addr
= state
->regs
->VLAN_LOOKUP_REG_5
;
634 addr
.m
= -1; // shouldn't get here, but...
638 //printf("shareports for %d is %02X\n",j,state->ports[j].shareports);
639 if (REG_SUPP(addr
)) {
640 SET_PORT_BITS(state
, state
->ports
[j
].shareports
, addr
, bit_lookup
);
644 if (REG_SUPP(state
->regs
->TAG_VLAN_MASK_REG
)) {
645 for (j
=0; j
<MAX_VLANS
; j
++) {
646 reg addr
= state
->regs
->TAG_VLAN_MASK_REG
;
647 const bitnum
*bit_lookup
= (j
%2==0)?
648 state
->regs
->TAG_VLAN_MASK_EVEN_BIT
:
649 state
->regs
->TAG_VLAN_MASK_ODD_BIT
;
650 unsigned int vlan_mask
;
651 if (state
->regs
->SIMPLE_VLAN_REGISTERS
) {
656 vlan_mask
= state
->vlans
[j
].ports
;
657 SET_PORT_BITS(state
, vlan_mask
, addr
, bit_lookup
);
661 for (i
=0; i
<MAX_PORTS
; i
++) {
662 if (REG_SUPP(state
->regs
->VLAN_DEFAULT_TAG_REG
[i
])) {
663 int err
= setPhy(state
, state
->regs
->VLAN_DEFAULT_TAG_REG
[i
],
664 state
->ports
[i
].pvid
);
671 return ip175c_set_flags(state
);
675 * Uses only the VLAN port mask and the add tag mask to generate the other fields:
676 * which ports are part of the same VLAN, removing vlan tags, and VLAN tag ids.
678 static void ip175c_correct_vlan_state(struct ip175c_state
*state
)
681 state
->num_vlans
= 0;
682 for (i
=0; i
<MAX_VLANS
; i
++) {
683 if (state
->vlans
[i
].ports
!= 0) {
684 state
->num_vlans
= i
+1; // Hack -- we need to store the "set" vlans somewhere...
688 for (i
=0; i
<state
->regs
->NUM_PORTS
; i
++) {
689 unsigned int portmask
= (1<<i
);
690 if (!state
->vlan_enabled
) {
691 // Share with everybody!
692 state
->ports
[i
].shareports
= (1<<state
->regs
->NUM_PORTS
)-1;
695 state
->ports
[i
].shareports
= portmask
;
696 for (j
=0; j
<MAX_VLANS
; j
++) {
697 if (state
->vlans
[j
].ports
& portmask
)
698 state
->ports
[i
].shareports
|= state
->vlans
[j
].ports
;
703 static int ip175c_update_state(struct ip175c_state
*state
)
705 ip175c_correct_vlan_state(state
);
706 return ip175c_set_state(state
);
709 static int ip175c_set_vlan_mode(struct ip175c_state
*state
)
711 return ip175c_update_state(state
);
714 static int ip175c_do_reset(struct ip175c_state
*state
)
718 if (REG_SUPP(state
->regs
->MODE_REG
)) {
719 err
= setPhy(state
, state
->regs
->MODE_REG
, state
->regs
->MODE_VAL
);
722 err
= getPhy(state
, state
->regs
->MODE_REG
);
730 static int ip175c_get_enable_vlan(struct switch_dev
*dev
, const struct switch_attr
*attr
, struct switch_val
*val
)
732 struct ip175c_state
*state
= dev
->priv
;
735 err
= state
->regs
->get_state(state
); // May be set in get_state.
738 val
->value
.i
= state
->vlan_enabled
;
742 static void ip175c_reset_vlan_config(struct ip175c_state
*state
)
746 state
->remove_tag
= (state
->vlan_enabled
? ((1<<state
->regs
->NUM_PORTS
)-1) : 0x0000);
747 state
->add_tag
= 0x0000;
748 for (i
= 0; i
< MAX_VLANS
; i
++) {
749 state
->vlans
[i
].ports
= 0x0000;
750 state
->vlans
[i
].tag
= (i
? i
: 16);
752 for (i
= 0; i
< MAX_PORTS
; i
++)
753 state
->ports
[i
].pvid
= 0;
756 static int ip175c_set_enable_vlan(struct switch_dev
*dev
, const struct switch_attr
*attr
, struct switch_val
*val
)
758 struct ip175c_state
*state
= dev
->priv
;
762 err
= state
->regs
->get_state(state
);
765 enable
= val
->value
.i
;
767 if (state
->vlan_enabled
== enable
) {
768 // Do not change any state.
771 state
->vlan_enabled
= enable
;
773 // Otherwise, if we are switching state, set fields to a known default.
774 ip175c_reset_vlan_config(state
);
776 return state
->regs
->set_vlan_mode(state
);
779 static int ip175c_get_ports(struct switch_dev
*dev
, struct switch_val
*val
)
781 struct ip175c_state
*state
= dev
->priv
;
787 if (val
->port_vlan
>= dev
->vlans
|| val
->port_vlan
< 0)
790 err
= state
->regs
->get_state(state
);
794 ports
= state
->vlans
[val
->port_vlan
].ports
;
797 while (b
< MAX_PORTS
) {
799 int istagged
= ((state
->add_tag
>> b
) & 1);
800 val
->value
.ports
[ind
].id
= b
;
801 val
->value
.ports
[ind
].flags
= (istagged
<< SWITCH_PORT_FLAG_TAGGED
);
812 static int ip175c_set_ports(struct switch_dev
*dev
, struct switch_val
*val
)
814 struct ip175c_state
*state
= dev
->priv
;
818 if (val
->port_vlan
>= dev
->vlans
|| val
->port_vlan
< 0)
821 err
= state
->regs
->get_state(state
);
825 state
->vlans
[val
->port_vlan
].ports
= 0;
826 for (i
= 0; i
< val
->len
; i
++) {
827 unsigned int bitmask
= (1<<val
->value
.ports
[i
].id
);
828 state
->vlans
[val
->port_vlan
].ports
|= bitmask
;
829 if (val
->value
.ports
[i
].flags
& (1<<SWITCH_PORT_FLAG_TAGGED
)) {
830 state
->add_tag
|= bitmask
;
831 state
->remove_tag
&= (~bitmask
);
833 state
->add_tag
&= (~bitmask
);
834 state
->remove_tag
|= bitmask
;
838 return state
->regs
->update_state(state
);
841 static int ip175c_apply(struct switch_dev
*dev
)
843 struct ip175c_state
*state
= dev
->priv
;
846 err
= state
->regs
->get_flags(state
);
850 if (REG_SUPP(state
->regs
->MII_REGISTER_EN
)) {
851 int val
= getPhy(state
, state
->regs
->MII_REGISTER_EN
);
855 val
|= (1<<state
->regs
->MII_REGISTER_EN_BIT
);
856 return setPhy(state
, state
->regs
->MII_REGISTER_EN
, val
);
861 static int ip175c_reset(struct switch_dev
*dev
)
863 struct ip175c_state
*state
= dev
->priv
;
866 err
= state
->regs
->get_flags(state
);
870 if (REG_SUPP(state
->regs
->RESET_REG
)) {
871 err
= setPhy(state
, state
->regs
->RESET_REG
, state
->regs
->RESET_VAL
);
874 err
= getPhy(state
, state
->regs
->RESET_REG
);
877 * Data sheet specifies reset period to be 2 msec.
878 * (I don't see any mention of the 2ms delay in the IP178C spec, only
879 * in IP175C, but it can't hurt.)
884 /* reset switch ports */
885 for (i
= 0; i
< state
->regs
->NUM_PORTS
-1; i
++) {
886 err
= ip_phy_write(state
, i
, MII_BMCR
, BMCR_RESET
);
891 ip175c_reset_vlan_config(state
);
893 return state
->regs
->reset(state
);
896 static int ip175c_get_tagged(struct switch_dev
*dev
, const struct switch_attr
*attr
, struct switch_val
*val
)
898 struct ip175c_state
*state
= dev
->priv
;
901 err
= state
->regs
->get_state(state
);
905 if (state
->add_tag
& (1<<val
->port_vlan
)) {
906 if (state
->remove_tag
& (1<<val
->port_vlan
))
907 val
->value
.i
= 3; // shouldn't ever happen.
911 if (state
->remove_tag
& (1<<val
->port_vlan
))
919 static int ip175c_set_tagged(struct switch_dev
*dev
, const struct switch_attr
*attr
, struct switch_val
*val
)
921 struct ip175c_state
*state
= dev
->priv
;
924 err
= state
->regs
->get_state(state
);
928 state
->add_tag
&= ~(1<<val
->port_vlan
);
929 state
->remove_tag
&= ~(1<<val
->port_vlan
);
931 if (val
->value
.i
== 0)
932 state
->remove_tag
|= (1<<val
->port_vlan
);
933 if (val
->value
.i
== 1)
934 state
->add_tag
|= (1<<val
->port_vlan
);
936 return state
->regs
->update_state(state
);
939 /** Get the current phy address */
940 static int ip175c_get_phy(struct switch_dev
*dev
, const struct switch_attr
*attr
, struct switch_val
*val
)
942 struct ip175c_state
*state
= dev
->priv
;
944 val
->value
.i
= state
->proc_mii
.p
;
948 /** Set a new phy address for low level access to registers */
949 static int ip175c_set_phy(struct switch_dev
*dev
, const struct switch_attr
*attr
, struct switch_val
*val
)
951 struct ip175c_state
*state
= dev
->priv
;
952 int new_reg
= val
->value
.i
;
954 if (new_reg
< 0 || new_reg
> 31)
955 state
->proc_mii
.p
= (u16
)-1;
957 state
->proc_mii
.p
= (u16
)new_reg
;
961 /** Get the current register number */
962 static int ip175c_get_reg(struct switch_dev
*dev
, const struct switch_attr
*attr
, struct switch_val
*val
)
964 struct ip175c_state
*state
= dev
->priv
;
966 val
->value
.i
= state
->proc_mii
.m
;
970 /** Set a new register address for low level access to registers */
971 static int ip175c_set_reg(struct switch_dev
*dev
, const struct switch_attr
*attr
, struct switch_val
*val
)
973 struct ip175c_state
*state
= dev
->priv
;
974 int new_reg
= val
->value
.i
;
976 if (new_reg
< 0 || new_reg
> 31)
977 state
->proc_mii
.m
= (u16
)-1;
979 state
->proc_mii
.m
= (u16
)new_reg
;
983 /** Get the register content of state->proc_mii */
984 static int ip175c_get_val(struct switch_dev
*dev
, const struct switch_attr
*attr
, struct switch_val
*val
)
986 struct ip175c_state
*state
= dev
->priv
;
987 int retval
= -EINVAL
;
988 if (REG_SUPP(state
->proc_mii
))
989 retval
= getPhy(state
, state
->proc_mii
);
994 val
->value
.i
= retval
;
999 /** Write a value to the register defined by phy/reg above */
1000 static int ip175c_set_val(struct switch_dev
*dev
, const struct switch_attr
*attr
, struct switch_val
*val
)
1002 struct ip175c_state
*state
= dev
->priv
;
1003 int myval
, err
= -EINVAL
;
1005 myval
= val
->value
.i
;
1006 if (myval
<= 0xffff && myval
>= 0 && REG_SUPP(state
->proc_mii
)) {
1007 err
= setPhy(state
, state
->proc_mii
, (u16
)myval
);
1012 static int ip175c_read_name(struct switch_dev
*dev
, const struct switch_attr
*attr
, struct switch_val
*val
)
1014 struct ip175c_state
*state
= dev
->priv
;
1015 val
->value
.s
= state
->regs
->NAME
; // Just a const pointer, won't be freed by swconfig.
1019 static int ip175c_get_tag(struct switch_dev
*dev
, const struct switch_attr
*attr
, struct switch_val
*val
)
1021 struct ip175c_state
*state
= dev
->priv
;
1022 int vlan
= val
->port_vlan
;
1024 if (vlan
< 0 || vlan
>= MAX_VLANS
)
1027 val
->value
.i
= state
->vlans
[vlan
].tag
;
1031 static int ip175c_set_tag(struct switch_dev
*dev
, const struct switch_attr
*attr
, struct switch_val
*val
)
1033 struct ip175c_state
*state
= dev
->priv
;
1034 int vlan
= val
->port_vlan
;
1035 int tag
= val
->value
.i
;
1037 if (vlan
< 0 || vlan
>= MAX_VLANS
)
1040 if (tag
< 0 || tag
> 4095)
1043 state
->vlans
[vlan
].tag
= tag
;
1044 return state
->regs
->update_state(state
);
1047 static int ip175c_set_port_speed(struct switch_dev
*dev
, const struct switch_attr
*attr
, struct switch_val
*val
)
1049 struct ip175c_state
*state
= dev
->priv
;
1050 int nr
= val
->port_vlan
;
1054 if (val
->value
.i
== 100) {
1057 } else if (val
->value
.i
== 10) {
1065 /* Can't set speed for cpu port */
1066 if (nr
== state
->regs
->CPU_PORT
)
1069 if (nr
>= dev
->ports
|| nr
< 0)
1072 ctrl
= ip_phy_read(state
, nr
, 0);
1078 ctrl
|= (autoneg
<<12);
1079 ctrl
|= (speed
<<13);
1081 return ip_phy_write(state
, nr
, 0, ctrl
);
1084 static int ip175c_get_port_speed(struct switch_dev
*dev
, const struct switch_attr
*attr
, struct switch_val
*val
)
1086 struct ip175c_state
*state
= dev
->priv
;
1087 int nr
= val
->port_vlan
;
1090 if (nr
== state
->regs
->CPU_PORT
) {
1095 if (nr
>= dev
->ports
|| nr
< 0)
1098 status
= ip_phy_read(state
, nr
, 1);
1099 speed
= ip_phy_read(state
, nr
, 18);
1100 if (status
< 0 || speed
< 0)
1104 val
->value
.i
= ((speed
& (1<<11)) ? 100 : 10);
1111 static int ip175c_get_port_status(struct switch_dev
*dev
, const struct switch_attr
*attr
, struct switch_val
*val
)
1113 struct ip175c_state
*state
= dev
->priv
;
1114 int ctrl
, speed
, status
;
1115 int nr
= val
->port_vlan
;
1117 char *buf
= state
->buf
; // fixed-length at 80.
1119 if (nr
== state
->regs
->CPU_PORT
) {
1120 sprintf(buf
, "up, 100 Mbps, cpu port");
1125 if (nr
>= dev
->ports
|| nr
< 0)
1128 ctrl
= ip_phy_read(state
, nr
, 0);
1129 status
= ip_phy_read(state
, nr
, 1);
1130 speed
= ip_phy_read(state
, nr
, 18);
1131 if (ctrl
< 0 || status
< 0 || speed
< 0)
1135 len
= sprintf(buf
, "up, %d Mbps, %s duplex",
1136 ((speed
& (1<<11)) ? 100 : 10),
1137 ((speed
& (1<<10)) ? "full" : "half"));
1139 len
= sprintf(buf
, "down");
1141 if (ctrl
& (1<<12)) {
1142 len
+= sprintf(buf
+len
, ", auto-negotiate");
1143 if (!(status
& (1<<5)))
1144 len
+= sprintf(buf
+len
, " (in progress)");
1146 len
+= sprintf(buf
+len
, ", fixed speed (%d)",
1147 ((ctrl
& (1<<13)) ? 100 : 10));
1155 static int ip175c_get_pvid(struct switch_dev
*dev
, int port
, int *val
)
1157 struct ip175c_state
*state
= dev
->priv
;
1159 *val
= state
->ports
[port
].pvid
;
1163 static int ip175c_set_pvid(struct switch_dev
*dev
, int port
, int val
)
1165 struct ip175c_state
*state
= dev
->priv
;
1168 if (val
< 0 || val
>= MAX_VLANS
)
1171 err
= state
->regs
->get_state(state
);
1175 state
->ports
[port
].pvid
= val
;
1177 return state
->regs
->update_state(state
);
1191 IP175C_REGISTER_PHY
,
1192 IP175C_REGISTER_MII
,
1193 IP175C_REGISTER_VALUE
,
1194 IP175C_REGISTER_ERRNO
,
1201 static const struct switch_attr ip175c_global
[] = {
1202 [IP175C_ENABLE_VLAN
] = {
1203 .id
= IP175C_ENABLE_VLAN
,
1204 .type
= SWITCH_TYPE_INT
,
1205 .name
= "enable_vlan",
1206 .description
= "Flag to enable or disable VLANs and tagging",
1207 .get
= ip175c_get_enable_vlan
,
1208 .set
= ip175c_set_enable_vlan
,
1210 [IP175C_GET_NAME
] = {
1211 .id
= IP175C_GET_NAME
,
1212 .type
= SWITCH_TYPE_STRING
,
1213 .description
= "Returns the type of IC+ chip.",
1215 .get
= ip175c_read_name
,
1218 /* jal: added for low level debugging etc. */
1219 [IP175C_REGISTER_PHY
] = {
1220 .id
= IP175C_REGISTER_PHY
,
1221 .type
= SWITCH_TYPE_INT
,
1222 .description
= "Direct register access: set PHY (0-4, or 29,30,31)",
1224 .get
= ip175c_get_phy
,
1225 .set
= ip175c_set_phy
,
1227 [IP175C_REGISTER_MII
] = {
1228 .id
= IP175C_REGISTER_MII
,
1229 .type
= SWITCH_TYPE_INT
,
1230 .description
= "Direct register access: set MII register number (0-31)",
1232 .get
= ip175c_get_reg
,
1233 .set
= ip175c_set_reg
,
1235 [IP175C_REGISTER_VALUE
] = {
1236 .id
= IP175C_REGISTER_VALUE
,
1237 .type
= SWITCH_TYPE_INT
,
1238 .description
= "Direct register access: read/write to register (0-65535)",
1240 .get
= ip175c_get_val
,
1241 .set
= ip175c_set_val
,
1245 static const struct switch_attr ip175c_vlan
[] = {
1246 [IP175C_VLAN_TAG
] = {
1247 .id
= IP175C_VLAN_TAG
,
1248 .type
= SWITCH_TYPE_INT
,
1249 .description
= "VLAN tag (0-4095) [IP175D only]",
1251 .get
= ip175c_get_tag
,
1252 .set
= ip175c_set_tag
,
1256 static const struct switch_attr ip175c_port
[] = {
1257 [IP175C_PORT_STATUS
] = {
1258 .id
= IP175C_PORT_STATUS
,
1259 .type
= SWITCH_TYPE_STRING
,
1260 .description
= "Returns Detailed port status",
1262 .get
= ip175c_get_port_status
,
1265 [IP175C_PORT_LINK
] = {
1266 .id
= IP175C_PORT_LINK
,
1267 .type
= SWITCH_TYPE_INT
,
1268 .description
= "Link speed. Can write 0 for auto-negotiate, or 10 or 100",
1270 .get
= ip175c_get_port_speed
,
1271 .set
= ip175c_set_port_speed
,
1273 [IP175C_PORT_TAGGED
] = {
1274 .id
= IP175C_PORT_LINK
,
1275 .type
= SWITCH_TYPE_INT
,
1276 .description
= "0 = untag, 1 = add tags, 2 = do not alter (This value is reset if vlans are altered)",
1278 .get
= ip175c_get_tagged
,
1279 .set
= ip175c_set_tagged
,
1283 static int ip175c_probe(struct phy_device
*pdev
)
1285 struct ip175c_state
*state
;
1286 struct switch_dev
*dev
;
1289 /* We only attach to PHY 0, but use all available PHYs */
1290 if (pdev
->addr
!= 0)
1293 state
= kzalloc(sizeof(*state
), GFP_KERNEL
);
1298 dev
->attr_global
.attr
= ip175c_global
;
1299 dev
->attr_global
.n_attr
= ARRAY_SIZE(ip175c_global
);
1300 dev
->attr_port
.attr
= ip175c_port
;
1301 dev
->attr_port
.n_attr
= ARRAY_SIZE(ip175c_port
);
1302 dev
->attr_vlan
.attr
= ip175c_vlan
;
1303 dev
->attr_vlan
.n_attr
= ARRAY_SIZE(ip175c_vlan
);
1305 dev
->get_port_pvid
= ip175c_get_pvid
;
1306 dev
->set_port_pvid
= ip175c_set_pvid
;
1307 dev
->get_vlan_ports
= ip175c_get_ports
;
1308 dev
->set_vlan_ports
= ip175c_set_ports
;
1309 dev
->apply_config
= ip175c_apply
;
1310 dev
->reset_switch
= ip175c_reset
;
1314 state
->mii_bus
= pdev
->bus
;
1316 err
= get_model(state
);
1320 dev
->vlans
= MAX_VLANS
;
1321 dev
->cpu_port
= state
->regs
->CPU_PORT
;
1322 dev
->ports
= state
->regs
->NUM_PORTS
;
1323 dev
->name
= state
->regs
->NAME
;
1325 pr_info("IP175C: Found %s at %s\n", dev
->name
, dev_name(&pdev
->dev
));
1333 static int ip175c_config_init(struct phy_device
*pdev
)
1335 struct ip175c_state
*state
= pdev
->priv
;
1336 struct net_device
*dev
= pdev
->attached_dev
;
1339 pdev
->irq
= PHY_IGNORE_INTERRUPT
;
1340 err
= register_switch(&state
->dev
, dev
);
1344 ip175c_reset(&state
->dev
);
1346 state
->registered
= true;
1348 pdev
->state
= PHY_RUNNING
;
1349 pdev
->speed
= SPEED_100
;
1350 pdev
->duplex
= DUPLEX_FULL
;
1351 pdev
->pause
= pdev
->asym_pause
= 0;
1352 netif_carrier_on(pdev
->attached_dev
);
1357 static void ip175c_remove(struct phy_device
*pdev
)
1359 struct ip175c_state
*state
= pdev
->priv
;
1361 if (state
->registered
)
1362 unregister_switch(&state
->dev
);
1366 static int ip175c_config_aneg(struct phy_device
*pdev
)
1371 static int ip175c_read_status(struct phy_device
*pdev
)
1376 static struct phy_driver ip175c_driver
= {
1377 .name
= "IC+ IP175C",
1378 .phy_id
= 0x02430c00,
1379 .phy_id_mask
= 0x0ffffc00,
1380 .features
= PHY_BASIC_FEATURES
,
1381 .probe
= ip175c_probe
,
1382 .remove
= ip175c_remove
,
1383 .config_init
= ip175c_config_init
,
1384 .config_aneg
= ip175c_config_aneg
,
1385 .read_status
= ip175c_read_status
,
1386 .driver
= { .owner
= THIS_MODULE
},
1389 static struct phy_driver ip175a_driver
= {
1390 .name
= "IC+ IP175A",
1391 .phy_id
= 0x02430c50,
1392 .phy_id_mask
= 0x0ffffff0,
1393 .features
= PHY_BASIC_FEATURES
,
1394 .probe
= ip175c_probe
,
1395 .remove
= ip175c_remove
,
1396 .config_init
= ip175c_config_init
,
1397 .config_aneg
= ip175c_config_aneg
,
1398 .read_status
= ip175c_read_status
,
1399 .driver
= { .owner
= THIS_MODULE
},
1403 int __init
ip175c_init(void)
1407 ret
= phy_driver_register(&ip175a_driver
);
1411 return phy_driver_register(&ip175c_driver
);
1414 void __exit
ip175c_exit(void)
1416 phy_driver_unregister(&ip175c_driver
);
1417 phy_driver_unregister(&ip175a_driver
);
1420 MODULE_AUTHOR("Patrick Horn <patrick.horn@gmail.com>");
1421 MODULE_AUTHOR("Felix Fietkau <nbd@openwrt.org>");
1422 MODULE_LICENSE("GPL");
1424 module_init(ip175c_init
);
1425 module_exit(ip175c_exit
);