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>
20 #define PSB6970_MAX_VLANS 16
21 #define PSB6970_NUM_PORTS 7
22 #define PSB6970_DEFAULT_PORT_CPU 6
23 #define PSB6970_IS_CPU_PORT(x) ((x) > 4)
25 #define PHYADDR(_reg) ((_reg >> 5) & 0xff), (_reg & 0x1f)
27 /* --- Identification --- */
28 #define PSB6970_CI0 0x0100
29 #define PSB6970_CI0_MASK 0x000f
30 #define PSB6970_CI1 0x0101
31 #define PSB6970_CI1_VAL 0x2599
32 #define PSB6970_CI1_MASK 0xffff
34 /* --- VLAN filter table --- */
35 #define PSB6970_VFxL(i) ((i)*2+0x10) /* VLAN Filter Low */
36 #define PSB6970_VFxL_VV (1 << 15) /* VLAN_Valid */
38 #define PSB6970_VFxH(i) ((i)*2+0x11) /* VLAN Filter High */
39 #define PSB6970_VFxH_TM_SHIFT 7 /* Tagged Member */
41 /* --- Port registers --- */
42 #define PSB6970_EC(p) ((p)*0x20+2) /* Extended Control */
43 #define PSB6970_EC_IFNTE (1 << 1) /* Input Force No Tag Enable */
45 #define PSB6970_PBVM(p) ((p)*0x20+3) /* Port Base VLAN Map */
46 #define PSB6970_PBVM_VMCE (1 << 8)
47 #define PSB6970_PBVM_AOVTP (1 << 9)
48 #define PSB6970_PBVM_VSD (1 << 10)
49 #define PSB6970_PBVM_VC (1 << 11) /* VID Check with VID table */
50 #define PSB6970_PBVM_TBVE (1 << 13) /* Tag-Based VLAN enable */
52 #define PSB6970_DVID(p) ((p)*0x20+4) /* Default VLAN ID & Priority */
55 struct switch_dev dev
;
56 struct phy_device
*phy
;
57 u16 (*read
) (struct phy_device
* phydev
, int reg
);
58 void (*write
) (struct phy_device
* phydev
, int reg
, u16 val
);
59 struct mutex reg_mutex
;
61 /* all fields below are cleared on reset */
63 u16 vlan_id
[PSB6970_MAX_VLANS
];
64 u8 vlan_table
[PSB6970_MAX_VLANS
];
66 u16 pvid
[PSB6970_NUM_PORTS
];
69 #define to_psb6970(_dev) container_of(_dev, struct psb6970_priv, dev)
71 static u16
psb6970_mii_read(struct phy_device
*phydev
, int reg
)
73 return phydev
->bus
->read(phydev
->bus
, PHYADDR(reg
));
76 static void psb6970_mii_write(struct phy_device
*phydev
, int reg
, u16 val
)
78 phydev
->bus
->write(phydev
->bus
, PHYADDR(reg
), val
);
82 psb6970_set_vlan(struct switch_dev
*dev
, const struct switch_attr
*attr
,
83 struct switch_val
*val
)
85 struct psb6970_priv
*priv
= to_psb6970(dev
);
86 priv
->vlan
= !!val
->value
.i
;
91 psb6970_get_vlan(struct switch_dev
*dev
, const struct switch_attr
*attr
,
92 struct switch_val
*val
)
94 struct psb6970_priv
*priv
= to_psb6970(dev
);
95 val
->value
.i
= priv
->vlan
;
99 static int psb6970_set_pvid(struct switch_dev
*dev
, int port
, int vlan
)
101 struct psb6970_priv
*priv
= to_psb6970(dev
);
103 /* make sure no invalid PVIDs get set */
104 if (vlan
>= dev
->vlans
)
107 priv
->pvid
[port
] = vlan
;
111 static int psb6970_get_pvid(struct switch_dev
*dev
, int port
, int *vlan
)
113 struct psb6970_priv
*priv
= to_psb6970(dev
);
114 *vlan
= priv
->pvid
[port
];
119 psb6970_set_vid(struct switch_dev
*dev
, const struct switch_attr
*attr
,
120 struct switch_val
*val
)
122 struct psb6970_priv
*priv
= to_psb6970(dev
);
123 priv
->vlan_id
[val
->port_vlan
] = val
->value
.i
;
128 psb6970_get_vid(struct switch_dev
*dev
, const struct switch_attr
*attr
,
129 struct switch_val
*val
)
131 struct psb6970_priv
*priv
= to_psb6970(dev
);
132 val
->value
.i
= priv
->vlan_id
[val
->port_vlan
];
136 static struct switch_attr psb6970_globals
[] = {
138 .type
= SWITCH_TYPE_INT
,
139 .name
= "enable_vlan",
140 .description
= "Enable VLAN mode",
141 .set
= psb6970_set_vlan
,
142 .get
= psb6970_get_vlan
,
146 static struct switch_attr psb6970_port
[] = {
149 static struct switch_attr psb6970_vlan
[] = {
151 .type
= SWITCH_TYPE_INT
,
153 .description
= "VLAN ID (0-4094)",
154 .set
= psb6970_set_vid
,
155 .get
= psb6970_get_vid
,
160 static int psb6970_get_ports(struct switch_dev
*dev
, struct switch_val
*val
)
162 struct psb6970_priv
*priv
= to_psb6970(dev
);
163 u8 ports
= priv
->vlan_table
[val
->port_vlan
];
167 for (i
= 0; i
< PSB6970_NUM_PORTS
; i
++) {
168 struct switch_port
*p
;
170 if (!(ports
& (1 << i
)))
173 p
= &val
->value
.ports
[val
->len
++];
175 if (priv
->vlan_tagged
& (1 << i
))
176 p
->flags
= (1 << SWITCH_PORT_FLAG_TAGGED
);
183 static int psb6970_set_ports(struct switch_dev
*dev
, struct switch_val
*val
)
185 struct psb6970_priv
*priv
= to_psb6970(dev
);
186 u8
*vt
= &priv
->vlan_table
[val
->port_vlan
];
190 for (i
= 0; i
< val
->len
; i
++) {
191 struct switch_port
*p
= &val
->value
.ports
[i
];
193 if (p
->flags
& (1 << SWITCH_PORT_FLAG_TAGGED
))
194 priv
->vlan_tagged
|= (1 << p
->id
);
196 priv
->vlan_tagged
&= ~(1 << p
->id
);
197 priv
->pvid
[p
->id
] = val
->port_vlan
;
199 /* make sure that an untagged port does not
200 * appear in other vlans */
201 for (j
= 0; j
< PSB6970_MAX_VLANS
; j
++) {
202 if (j
== val
->port_vlan
)
204 priv
->vlan_table
[j
] &= ~(1 << p
->id
);
213 static int psb6970_hw_apply(struct switch_dev
*dev
)
215 struct psb6970_priv
*priv
= to_psb6970(dev
);
218 mutex_lock(&priv
->reg_mutex
);
221 /* into the vlan translation unit */
222 for (j
= 0; j
< PSB6970_MAX_VLANS
; j
++) {
223 u8 vp
= priv
->vlan_table
[j
];
226 priv
->write(priv
->phy
, PSB6970_VFxL(j
),
227 PSB6970_VFxL_VV
| priv
->vlan_id
[j
]);
228 priv
->write(priv
->phy
, PSB6970_VFxH(j
),
231 PSB6970_VFxH_TM_SHIFT
) | vp
);
232 } else /* clear VLAN Valid flag for unused vlans */
233 priv
->write(priv
->phy
, PSB6970_VFxL(j
), 0);
238 /* update the port destination mask registers and tag settings */
239 for (i
= 0; i
< PSB6970_NUM_PORTS
; i
++) {
240 int dvid
= 1, pbvm
= 0x7f | PSB6970_PBVM_VSD
, ec
= 0;
243 ec
= PSB6970_EC_IFNTE
;
244 dvid
= priv
->vlan_id
[priv
->pvid
[i
]];
245 pbvm
|= PSB6970_PBVM_TBVE
| PSB6970_PBVM_VMCE
;
247 if ((i
<< 1) & priv
->vlan_tagged
)
248 pbvm
|= PSB6970_PBVM_AOVTP
| PSB6970_PBVM_VC
;
251 priv
->write(priv
->phy
, PSB6970_PBVM(i
), pbvm
);
253 if (!PSB6970_IS_CPU_PORT(i
)) {
254 priv
->write(priv
->phy
, PSB6970_EC(i
), ec
);
255 priv
->write(priv
->phy
, PSB6970_DVID(i
), dvid
);
259 mutex_unlock(&priv
->reg_mutex
);
263 static int psb6970_reset_switch(struct switch_dev
*dev
)
265 struct psb6970_priv
*priv
= to_psb6970(dev
);
268 mutex_lock(&priv
->reg_mutex
);
270 memset(&priv
->vlan
, 0, sizeof(struct psb6970_priv
) -
271 offsetof(struct psb6970_priv
, vlan
));
273 for (i
= 0; i
< PSB6970_MAX_VLANS
; i
++)
274 priv
->vlan_id
[i
] = i
;
276 mutex_unlock(&priv
->reg_mutex
);
278 return psb6970_hw_apply(dev
);
281 static const struct switch_dev_ops psb6970_ops
= {
283 .attr
= psb6970_globals
,
284 .n_attr
= ARRAY_SIZE(psb6970_globals
),
287 .attr
= psb6970_port
,
288 .n_attr
= ARRAY_SIZE(psb6970_port
),
291 .attr
= psb6970_vlan
,
292 .n_attr
= ARRAY_SIZE(psb6970_vlan
),
294 .get_port_pvid
= psb6970_get_pvid
,
295 .set_port_pvid
= psb6970_set_pvid
,
296 .get_vlan_ports
= psb6970_get_ports
,
297 .set_vlan_ports
= psb6970_set_ports
,
298 .apply_config
= psb6970_hw_apply
,
299 .reset_switch
= psb6970_reset_switch
,
302 static int psb6970_config_init(struct phy_device
*pdev
)
304 struct psb6970_priv
*priv
;
305 struct net_device
*dev
= pdev
->attached_dev
;
306 struct switch_dev
*swdev
;
309 priv
= kzalloc(sizeof(struct psb6970_priv
), GFP_KERNEL
);
316 printk(KERN_INFO
"%s: psb6970 switch driver attached.\n",
317 pdev
->attached_dev
->name
);
319 if (pdev
->addr
!= 0) {
324 pdev
->supported
= pdev
->advertising
= SUPPORTED_100baseT_Full
;
326 mutex_init(&priv
->reg_mutex
);
327 priv
->read
= psb6970_mii_read
;
328 priv
->write
= psb6970_mii_write
;
333 swdev
->cpu_port
= PSB6970_DEFAULT_PORT_CPU
;
334 swdev
->ops
= &psb6970_ops
;
336 swdev
->name
= "Lantiq PSB6970";
337 swdev
->vlans
= PSB6970_MAX_VLANS
;
338 swdev
->ports
= PSB6970_NUM_PORTS
;
340 if ((ret
= register_switch(&priv
->dev
, pdev
->attached_dev
)) < 0) {
345 ret
= psb6970_reset_switch(&priv
->dev
);
357 static int psb6970_read_status(struct phy_device
*phydev
)
359 phydev
->speed
= SPEED_100
;
360 phydev
->duplex
= DUPLEX_FULL
;
363 phydev
->state
= PHY_RUNNING
;
364 netif_carrier_on(phydev
->attached_dev
);
365 phydev
->adjust_link(phydev
->attached_dev
);
370 static int psb6970_config_aneg(struct phy_device
*phydev
)
375 static int psb6970_probe(struct phy_device
*pdev
)
380 static void psb6970_remove(struct phy_device
*pdev
)
382 struct psb6970_priv
*priv
= pdev
->priv
;
388 unregister_switch(&priv
->dev
);
392 static int psb6970_fixup(struct phy_device
*dev
)
394 struct mii_bus
*bus
= dev
->bus
;
397 /* look for the switch on the bus */
398 reg
= bus
->read(bus
, PHYADDR(PSB6970_CI1
)) & PSB6970_CI1_MASK
;
399 if (reg
!= PSB6970_CI1_VAL
)
402 dev
->phy_id
= (reg
<< 16);
403 dev
->phy_id
|= bus
->read(bus
, PHYADDR(PSB6970_CI0
)) & PSB6970_CI0_MASK
;
408 static struct phy_driver psb6970_driver
= {
409 .name
= "Lantiq PSB6970",
410 .phy_id
= PSB6970_CI1_VAL
<< 16,
411 .phy_id_mask
= 0xffff0000,
412 .features
= PHY_BASIC_FEATURES
,
413 .probe
= psb6970_probe
,
414 .remove
= psb6970_remove
,
415 .config_init
= &psb6970_config_init
,
416 .config_aneg
= &psb6970_config_aneg
,
417 .read_status
= &psb6970_read_status
,
418 .driver
= {.owner
= THIS_MODULE
},
421 int __init
psb6970_init(void)
423 phy_register_fixup_for_id(PHY_ANY_ID
, psb6970_fixup
);
424 return phy_driver_register(&psb6970_driver
);
427 module_init(psb6970_init
);
429 void __exit
psb6970_exit(void)
431 phy_driver_unregister(&psb6970_driver
);
434 module_exit(psb6970_exit
);
436 MODULE_DESCRIPTION("Lantiq PSB6970 Switch");
437 MODULE_AUTHOR("Ithamar R. Adema <ithamar.adema@team-embedded.nl>");
438 MODULE_LICENSE("GPL");