2 * Lantiq PSB6970 (Tantos) Switch driver
4 * Copyright (c) 2009,2010 Team Embedded.
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License v2 as published by the
8 * Free Software Foundation.
10 * The switch programming done in this driver follows the
11 * "Ethernet Traffic Separation using VLAN" Application Note as
12 * published by Lantiq.
15 #include <linux/module.h>
16 #include <linux/netdevice.h>
17 #include <linux/switch.h>
18 #include <linux/phy.h>
19 #include <linux/version.h>
21 #define PSB6970_MAX_VLANS 16
22 #define PSB6970_NUM_PORTS 7
23 #define PSB6970_DEFAULT_PORT_CPU 6
24 #define PSB6970_IS_CPU_PORT(x) ((x) > 4)
26 #define PHYADDR(_reg) ((_reg >> 5) & 0xff), (_reg & 0x1f)
28 /* --- Identification --- */
29 #define PSB6970_CI0 0x0100
30 #define PSB6970_CI0_MASK 0x000f
31 #define PSB6970_CI1 0x0101
32 #define PSB6970_CI1_VAL 0x2599
33 #define PSB6970_CI1_MASK 0xffff
35 /* --- VLAN filter table --- */
36 #define PSB6970_VFxL(i) ((i)*2+0x10) /* VLAN Filter Low */
37 #define PSB6970_VFxL_VV (1 << 15) /* VLAN_Valid */
39 #define PSB6970_VFxH(i) ((i)*2+0x11) /* VLAN Filter High */
40 #define PSB6970_VFxH_TM_SHIFT 7 /* Tagged Member */
42 /* --- Port registers --- */
43 #define PSB6970_EC(p) ((p)*0x20+2) /* Extended Control */
44 #define PSB6970_EC_IFNTE (1 << 1) /* Input Force No Tag Enable */
46 #define PSB6970_PBVM(p) ((p)*0x20+3) /* Port Base VLAN Map */
47 #define PSB6970_PBVM_VMCE (1 << 8)
48 #define PSB6970_PBVM_AOVTP (1 << 9)
49 #define PSB6970_PBVM_VSD (1 << 10)
50 #define PSB6970_PBVM_VC (1 << 11) /* VID Check with VID table */
51 #define PSB6970_PBVM_TBVE (1 << 13) /* Tag-Based VLAN enable */
53 #define PSB6970_DVID(p) ((p)*0x20+4) /* Default VLAN ID & Priority */
56 struct switch_dev dev
;
57 struct phy_device
*phy
;
58 u16 (*read
) (struct phy_device
* phydev
, int reg
);
59 void (*write
) (struct phy_device
* phydev
, int reg
, u16 val
);
60 struct mutex reg_mutex
;
62 /* all fields below are cleared on reset */
63 struct_group(psb6970_priv_volatile
,
65 u16 vlan_id
[PSB6970_MAX_VLANS
];
66 u8 vlan_table
[PSB6970_MAX_VLANS
];
68 u16 pvid
[PSB6970_NUM_PORTS
];
72 #define to_psb6970(_dev) container_of(_dev, struct psb6970_priv, dev)
74 static u16
psb6970_mii_read(struct phy_device
*phydev
, int reg
)
76 struct mii_bus
*bus
= phydev
->mdio
.bus
;
78 return bus
->read(bus
, PHYADDR(reg
));
81 static void psb6970_mii_write(struct phy_device
*phydev
, int reg
, u16 val
)
83 struct mii_bus
*bus
= phydev
->mdio
.bus
;
85 bus
->write(bus
, PHYADDR(reg
), val
);
89 psb6970_set_vlan(struct switch_dev
*dev
, const struct switch_attr
*attr
,
90 struct switch_val
*val
)
92 struct psb6970_priv
*priv
= to_psb6970(dev
);
93 priv
->vlan
= !!val
->value
.i
;
98 psb6970_get_vlan(struct switch_dev
*dev
, const struct switch_attr
*attr
,
99 struct switch_val
*val
)
101 struct psb6970_priv
*priv
= to_psb6970(dev
);
102 val
->value
.i
= priv
->vlan
;
106 static int psb6970_set_pvid(struct switch_dev
*dev
, int port
, int vlan
)
108 struct psb6970_priv
*priv
= to_psb6970(dev
);
110 /* make sure no invalid PVIDs get set */
111 if (vlan
>= dev
->vlans
)
114 priv
->pvid
[port
] = vlan
;
118 static int psb6970_get_pvid(struct switch_dev
*dev
, int port
, int *vlan
)
120 struct psb6970_priv
*priv
= to_psb6970(dev
);
121 *vlan
= priv
->pvid
[port
];
126 psb6970_set_vid(struct switch_dev
*dev
, const struct switch_attr
*attr
,
127 struct switch_val
*val
)
129 struct psb6970_priv
*priv
= to_psb6970(dev
);
130 priv
->vlan_id
[val
->port_vlan
] = val
->value
.i
;
135 psb6970_get_vid(struct switch_dev
*dev
, const struct switch_attr
*attr
,
136 struct switch_val
*val
)
138 struct psb6970_priv
*priv
= to_psb6970(dev
);
139 val
->value
.i
= priv
->vlan_id
[val
->port_vlan
];
143 static struct switch_attr psb6970_globals
[] = {
145 .type
= SWITCH_TYPE_INT
,
146 .name
= "enable_vlan",
147 .description
= "Enable VLAN mode",
148 .set
= psb6970_set_vlan
,
149 .get
= psb6970_get_vlan
,
153 static struct switch_attr psb6970_port
[] = {
156 static struct switch_attr psb6970_vlan
[] = {
158 .type
= SWITCH_TYPE_INT
,
160 .description
= "VLAN ID (0-4094)",
161 .set
= psb6970_set_vid
,
162 .get
= psb6970_get_vid
,
167 static int psb6970_get_ports(struct switch_dev
*dev
, struct switch_val
*val
)
169 struct psb6970_priv
*priv
= to_psb6970(dev
);
170 u8 ports
= priv
->vlan_table
[val
->port_vlan
];
174 for (i
= 0; i
< PSB6970_NUM_PORTS
; i
++) {
175 struct switch_port
*p
;
177 if (!(ports
& (1 << i
)))
180 p
= &val
->value
.ports
[val
->len
++];
182 if (priv
->vlan_tagged
& (1 << i
))
183 p
->flags
= (1 << SWITCH_PORT_FLAG_TAGGED
);
190 static int psb6970_set_ports(struct switch_dev
*dev
, struct switch_val
*val
)
192 struct psb6970_priv
*priv
= to_psb6970(dev
);
193 u8
*vt
= &priv
->vlan_table
[val
->port_vlan
];
197 for (i
= 0; i
< val
->len
; i
++) {
198 struct switch_port
*p
= &val
->value
.ports
[i
];
200 if (p
->flags
& (1 << SWITCH_PORT_FLAG_TAGGED
))
201 priv
->vlan_tagged
|= (1 << p
->id
);
203 priv
->vlan_tagged
&= ~(1 << p
->id
);
204 priv
->pvid
[p
->id
] = val
->port_vlan
;
206 /* make sure that an untagged port does not
207 * appear in other vlans */
208 for (j
= 0; j
< PSB6970_MAX_VLANS
; j
++) {
209 if (j
== val
->port_vlan
)
211 priv
->vlan_table
[j
] &= ~(1 << p
->id
);
220 static int psb6970_hw_apply(struct switch_dev
*dev
)
222 struct psb6970_priv
*priv
= to_psb6970(dev
);
225 mutex_lock(&priv
->reg_mutex
);
228 /* into the vlan translation unit */
229 for (j
= 0; j
< PSB6970_MAX_VLANS
; j
++) {
230 u8 vp
= priv
->vlan_table
[j
];
233 priv
->write(priv
->phy
, PSB6970_VFxL(j
),
234 PSB6970_VFxL_VV
| priv
->vlan_id
[j
]);
235 priv
->write(priv
->phy
, PSB6970_VFxH(j
),
238 PSB6970_VFxH_TM_SHIFT
) | vp
);
239 } else /* clear VLAN Valid flag for unused vlans */
240 priv
->write(priv
->phy
, PSB6970_VFxL(j
), 0);
245 /* update the port destination mask registers and tag settings */
246 for (i
= 0; i
< PSB6970_NUM_PORTS
; i
++) {
247 int dvid
= 1, pbvm
= 0x7f | PSB6970_PBVM_VSD
, ec
= 0;
250 ec
= PSB6970_EC_IFNTE
;
251 dvid
= priv
->vlan_id
[priv
->pvid
[i
]];
252 pbvm
|= PSB6970_PBVM_TBVE
| PSB6970_PBVM_VMCE
;
254 if ((i
<< 1) & priv
->vlan_tagged
)
255 pbvm
|= PSB6970_PBVM_AOVTP
| PSB6970_PBVM_VC
;
258 priv
->write(priv
->phy
, PSB6970_PBVM(i
), pbvm
);
260 if (!PSB6970_IS_CPU_PORT(i
)) {
261 priv
->write(priv
->phy
, PSB6970_EC(i
), ec
);
262 priv
->write(priv
->phy
, PSB6970_DVID(i
), dvid
);
266 mutex_unlock(&priv
->reg_mutex
);
270 static int psb6970_reset_switch(struct switch_dev
*dev
)
272 struct psb6970_priv
*priv
= to_psb6970(dev
);
275 mutex_lock(&priv
->reg_mutex
);
277 memset(&priv
->psb6970_priv_volatile
, 0,
278 sizeof(priv
->psb6970_priv_volatile
));
280 for (i
= 0; i
< PSB6970_MAX_VLANS
; i
++)
281 priv
->vlan_id
[i
] = i
;
283 mutex_unlock(&priv
->reg_mutex
);
285 return psb6970_hw_apply(dev
);
288 static const struct switch_dev_ops psb6970_ops
= {
290 .attr
= psb6970_globals
,
291 .n_attr
= ARRAY_SIZE(psb6970_globals
),
294 .attr
= psb6970_port
,
295 .n_attr
= ARRAY_SIZE(psb6970_port
),
298 .attr
= psb6970_vlan
,
299 .n_attr
= ARRAY_SIZE(psb6970_vlan
),
301 .get_port_pvid
= psb6970_get_pvid
,
302 .set_port_pvid
= psb6970_set_pvid
,
303 .get_vlan_ports
= psb6970_get_ports
,
304 .set_vlan_ports
= psb6970_set_ports
,
305 .apply_config
= psb6970_hw_apply
,
306 .reset_switch
= psb6970_reset_switch
,
309 static int psb6970_config_init(struct phy_device
*pdev
)
311 struct psb6970_priv
*priv
;
312 struct switch_dev
*swdev
;
315 priv
= kzalloc(sizeof(struct psb6970_priv
), GFP_KERNEL
);
321 if (pdev
->mdio
.addr
== 0)
322 printk(KERN_INFO
"%s: psb6970 switch driver attached.\n",
323 pdev
->attached_dev
->name
);
325 if (pdev
->mdio
.addr
!= 0) {
330 linkmode_zero(pdev
->supported
);
331 linkmode_set_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT
, pdev
->supported
);
332 linkmode_copy(pdev
->advertising
, pdev
->supported
);
334 mutex_init(&priv
->reg_mutex
);
335 priv
->read
= psb6970_mii_read
;
336 priv
->write
= psb6970_mii_write
;
341 swdev
->cpu_port
= PSB6970_DEFAULT_PORT_CPU
;
342 swdev
->ops
= &psb6970_ops
;
344 swdev
->name
= "Lantiq PSB6970";
345 swdev
->vlans
= PSB6970_MAX_VLANS
;
346 swdev
->ports
= PSB6970_NUM_PORTS
;
348 if ((ret
= register_switch(&priv
->dev
, pdev
->attached_dev
)) < 0) {
353 ret
= psb6970_reset_switch(&priv
->dev
);
363 static int psb6970_read_status(struct phy_device
*phydev
)
365 phydev
->speed
= SPEED_100
;
366 phydev
->duplex
= DUPLEX_FULL
;
369 phydev
->state
= PHY_RUNNING
;
370 netif_carrier_on(phydev
->attached_dev
);
371 phydev
->adjust_link(phydev
->attached_dev
);
376 static int psb6970_config_aneg(struct phy_device
*phydev
)
381 static int psb6970_probe(struct phy_device
*pdev
)
386 static void psb6970_remove(struct phy_device
*pdev
)
388 struct psb6970_priv
*priv
= pdev
->priv
;
393 if (pdev
->mdio
.addr
== 0)
394 unregister_switch(&priv
->dev
);
398 static int psb6970_fixup(struct phy_device
*dev
)
400 struct mii_bus
*bus
= dev
->mdio
.bus
;
403 /* look for the switch on the bus */
404 reg
= bus
->read(bus
, PHYADDR(PSB6970_CI1
)) & PSB6970_CI1_MASK
;
405 if (reg
!= PSB6970_CI1_VAL
)
408 dev
->phy_id
= (reg
<< 16);
409 dev
->phy_id
|= bus
->read(bus
, PHYADDR(PSB6970_CI0
)) & PSB6970_CI0_MASK
;
414 static struct phy_driver psb6970_driver
= {
415 .name
= "Lantiq PSB6970",
416 .phy_id
= PSB6970_CI1_VAL
<< 16,
417 .phy_id_mask
= 0xffff0000,
418 .features
= PHY_BASIC_FEATURES
,
419 .probe
= psb6970_probe
,
420 .remove
= psb6970_remove
,
421 .config_init
= &psb6970_config_init
,
422 .config_aneg
= &psb6970_config_aneg
,
423 .read_status
= &psb6970_read_status
,
426 int __init
psb6970_init(void)
428 phy_register_fixup_for_id(PHY_ANY_ID
, psb6970_fixup
);
429 return phy_driver_register(&psb6970_driver
, THIS_MODULE
);
432 module_init(psb6970_init
);
434 void __exit
psb6970_exit(void)
436 phy_driver_unregister(&psb6970_driver
);
439 module_exit(psb6970_exit
);
441 MODULE_DESCRIPTION("Lantiq PSB6970 Switch");
442 MODULE_AUTHOR("Ithamar R. Adema <ithamar.adema@team-embedded.nl>");
443 MODULE_LICENSE("GPL");