2 * Marvell 88E6060 switch driver
3 * Copyright (c) 2008 Felix Fietkau <nbd@openwrt.org>
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License v2 as published by the
7 * Free Software Foundation
9 #include <linux/kernel.h>
10 #include <linux/string.h>
11 #include <linux/errno.h>
12 #include <linux/unistd.h>
13 #include <linux/slab.h>
14 #include <linux/interrupt.h>
15 #include <linux/init.h>
16 #include <linux/delay.h>
17 #include <linux/netdevice.h>
18 #include <linux/etherdevice.h>
19 #include <linux/skbuff.h>
20 #include <linux/spinlock.h>
22 #include <linux/module.h>
23 #include <linux/mii.h>
24 #include <linux/ethtool.h>
25 #include <linux/phy.h>
26 #include <linux/if_vlan.h>
30 #include <asm/uaccess.h>
33 /* Undefine this to use trailer mode instead.
34 * I don't know if header mode works with all chips */
37 MODULE_DESCRIPTION("Marvell 88E6060 Switch driver");
38 MODULE_AUTHOR("Felix Fietkau");
39 MODULE_LICENSE("GPL");
41 #define MVSWITCH_MAGIC 0x88E6060
43 struct mvswitch_priv
{
44 /* the driver's tx function */
45 int (*hardstart
)(struct sk_buff
*skb
, struct net_device
*dev
);
46 struct vlan_group
*grp
;
50 #define to_mvsw(_phy) ((struct mvswitch_priv *) (_phy)->priv)
53 r16(struct phy_device
*phydev
, int addr
, int reg
)
55 return phydev
->bus
->read(phydev
->bus
, addr
, reg
);
59 w16(struct phy_device
*phydev
, int addr
, int reg
, u16 val
)
61 phydev
->bus
->write(phydev
->bus
, addr
, reg
, val
);
66 mvswitch_mangle_tx(struct sk_buff
*skb
, struct net_device
*dev
)
68 struct mvswitch_priv
*priv
;
76 if (unlikely(skb
->len
< 16))
80 if (__vlan_hwaccel_get_tag(skb
, &vid
))
83 if (skb_cloned(skb
) || (skb
->len
<= 62) || (skb_headroom(skb
) < MV_HEADER_SIZE
)) {
84 if (pskb_expand_head(skb
, MV_HEADER_SIZE
, (skb
->len
< 62 ? 62 - skb
->len
: 0), GFP_ATOMIC
))
89 buf
= skb_push(skb
, MV_HEADER_SIZE
);
91 if (__vlan_get_tag(skb
, &vid
))
94 if (unlikely((vid
> 15 || !priv
->vlans
[vid
])))
98 if (pskb_expand_head(skb
, 0, 64 + MV_TRAILER_SIZE
- skb
->len
, GFP_ATOMIC
))
101 buf
= skb
->data
+ 64;
102 skb
->len
= 64 + MV_TRAILER_SIZE
;
104 if (skb_cloned(skb
) || unlikely(skb_tailroom(skb
) < 4)) {
105 if (pskb_expand_head(skb
, 0, 4, GFP_ATOMIC
))
108 buf
= skb_put(skb
, 4);
111 /* move the ethernet header 4 bytes forward, overwriting the vlan tag */
112 memmove(skb
->data
+ 4, skb
->data
, 12);
115 skb
->mac_header
+= 4;
123 /* prepend the tag */
124 *((__be16
*) buf
) = cpu_to_be16(
125 ((vid
<< MV_HEADER_VLAN_S
) & MV_HEADER_VLAN_M
) |
126 ((priv
->vlans
[vid
] << MV_HEADER_PORTS_S
) & MV_HEADER_PORTS_M
)
130 *((__be32
*) buf
) = cpu_to_be32((
131 (MV_TRAILER_OVERRIDE
<< MV_TRAILER_FLAGS_S
) |
132 ((priv
->vlans
[vid
] & MV_TRAILER_PORTS_M
) << MV_TRAILER_PORTS_S
)
136 return priv
->hardstart(skb
, dev
);
140 printk("%s: failed to expand/update skb for the switch\n", dev
->name
);
143 /* any errors? drop the packet! */
144 dev_kfree_skb_any(skb
);
149 mvswitch_mangle_rx(struct sk_buff
*skb
, int napi
)
151 struct mvswitch_priv
*priv
;
152 struct net_device
*dev
;
170 skb_pull(skb
, MV_HEADER_SIZE
);
172 buf
= skb
->data
+ skb
->len
- MV_TRAILER_SIZE
;
177 /* look for the vlan matching the incoming port */
178 for (i
= 0; i
< ARRAY_SIZE(priv
->vlans
); i
++) {
179 if ((1 << buf
[1]) & priv
->vlans
[i
])
186 skb
->protocol
= eth_type_trans(skb
, skb
->dev
);
189 return vlan_hwaccel_receive_skb(skb
, priv
->grp
, vlan
);
191 return vlan_hwaccel_rx(skb
, priv
->grp
, vlan
);
194 /* no vlan? eat the packet! */
195 dev_kfree_skb_any(skb
);
201 mvswitch_netif_rx(struct sk_buff
*skb
)
203 return mvswitch_mangle_rx(skb
, 0);
207 mvswitch_netif_receive_skb(struct sk_buff
*skb
)
209 return mvswitch_mangle_rx(skb
, 1);
214 mvswitch_vlan_rx_register(struct net_device
*dev
, struct vlan_group
*grp
)
216 struct mvswitch_priv
*priv
= dev
->phy_ptr
;
222 mvswitch_wait_mask(struct phy_device
*pdev
, int addr
, int reg
, u16 mask
, u16 val
)
228 r
= r16(pdev
, addr
, reg
) & mask
;
236 mvswitch_config_init(struct phy_device
*pdev
)
238 struct mvswitch_priv
*priv
= to_mvsw(pdev
);
239 struct net_device
*dev
= pdev
->attached_dev
;
246 printk("%s: Marvell 88E6060 PHY driver attached.\n", dev
->name
);
247 pdev
->supported
= ADVERTISED_100baseT_Full
;
248 pdev
->advertising
= ADVERTISED_100baseT_Full
;
252 /* initialize default vlans */
253 for (i
= 0; i
< MV_PORTS
; i
++)
254 priv
->vlans
[(i
== MV_WANPORT
? 2 : 1)] |= (1 << i
);
256 /* before entering reset, disable all ports */
257 for (i
= 0; i
< MV_PORTS
; i
++)
258 w16(pdev
, MV_PORTREG(CONTROL
, i
), 0x00);
260 msleep(2); /* wait for the status change to settle in */
262 /* put the ATU in reset */
263 w16(pdev
, MV_SWITCHREG(ATU_CTRL
), MV_ATUCTL_RESET
);
265 i
= mvswitch_wait_mask(pdev
, MV_SWITCHREG(ATU_CTRL
), MV_ATUCTL_RESET
, 0);
267 printk("%s: Timeout waiting for the switch to reset.\n", dev
->name
);
271 /* set the ATU flags */
272 w16(pdev
, MV_SWITCHREG(ATU_CTRL
),
275 MV_ATUCTL_AGETIME(MV_ATUCTL_AGETIME_MIN
) /* minimum without disabling ageing */
278 /* initialize the cpu port */
279 w16(pdev
, MV_PORTREG(CONTROL
, MV_CPUPORT
),
288 /* wait for the phy change to settle in */
290 for (i
= 0; i
< MV_PORTS
; i
++) {
296 /* look for the matching vlan */
297 for (j
= 0; j
< ARRAY_SIZE(priv
->vlans
); j
++) {
298 if (priv
->vlans
[j
] & (1 << i
)) {
299 vlmap
= priv
->vlans
[j
];
303 /* leave port unconfigured if it's not part of a vlan */
307 /* add the cpu port to the allowed destinations list */
308 vlmap
|= (1 << MV_CPUPORT
);
310 /* take port out of its own vlan destination map */
313 /* apply vlan settings */
314 w16(pdev
, MV_PORTREG(VLANMAP
, i
),
315 MV_PORTVLAN_PORTS(vlmap
) |
320 w16(pdev
, MV_PORTREG(CONTROL
, i
),
325 w16(pdev
, MV_PORTREG(VLANMAP
, MV_CPUPORT
),
326 MV_PORTVLAN_ID(MV_CPUPORT
)
329 /* set the port association vector */
330 for (i
= 0; i
<= MV_PORTS
; i
++) {
331 w16(pdev
, MV_PORTREG(ASSOC
, i
),
332 MV_PORTASSOC_PORTS(1 << i
)
336 /* init switch control */
337 w16(pdev
, MV_SWITCHREG(CTRL
),
342 /* hook into the tx function */
344 priv
->hardstart
= dev
->hard_start_xmit
;
345 pdev
->netif_receive_skb
= mvswitch_netif_receive_skb
;
346 pdev
->netif_rx
= mvswitch_netif_rx
;
347 dev
->hard_start_xmit
= mvswitch_mangle_tx
;
348 dev
->vlan_rx_register
= mvswitch_vlan_rx_register
;
350 dev
->features
|= NETIF_F_HW_VLAN_RX
| NETIF_F_HW_VLAN_TX
;
352 dev
->features
|= NETIF_F_HW_VLAN_RX
;
359 mvswitch_read_status(struct phy_device
*pdev
)
361 pdev
->speed
= SPEED_100
;
362 pdev
->duplex
= DUPLEX_FULL
;
363 pdev
->state
= PHY_UP
;
365 /* XXX ugly workaround: we can't force the switch
366 * to gracefully handle hosts moving from one port to another,
367 * so we have to regularly clear the ATU database */
369 /* wait for the ATU to become available */
370 mvswitch_wait_mask(pdev
, MV_SWITCHREG(ATU_OP
), MV_ATUOP_INPROGRESS
, 0);
373 w16(pdev
, MV_SWITCHREG(ATU_OP
),
374 MV_ATUOP_INPROGRESS
|
378 /* wait for operation to complete */
379 mvswitch_wait_mask(pdev
, MV_SWITCHREG(ATU_OP
), MV_ATUOP_INPROGRESS
, 0);
385 mvswitch_config_aneg(struct phy_device
*phydev
)
391 mvswitch_remove(struct phy_device
*pdev
)
393 struct mvswitch_priv
*priv
= to_mvsw(pdev
);
394 struct net_device
*dev
= pdev
->attached_dev
;
396 /* restore old xmit handler */
397 if (priv
->hardstart
&& dev
)
398 dev
->hard_start_xmit
= priv
->hardstart
;
399 dev
->vlan_rx_register
= NULL
;
400 dev
->vlan_rx_kill_vid
= NULL
;
402 dev
->features
&= ~NETIF_F_HW_VLAN_RX
;
407 mvswitch_probe(struct phy_device
*pdev
)
409 struct mvswitch_priv
*priv
;
411 priv
= kzalloc(sizeof(struct mvswitch_priv
), GFP_KERNEL
);
421 mvswitch_fixup(struct phy_device
*dev
)
425 if (dev
->addr
!= 0x10)
428 reg
= dev
->bus
->read(dev
->bus
, MV_PORTREG(IDENT
, 0)) & MV_IDENT_MASK
;
429 if (reg
!= MV_IDENT_VALUE
)
432 dev
->phy_id
= MVSWITCH_MAGIC
;
437 static struct phy_driver mvswitch_driver
= {
438 .name
= "Marvell 88E6060",
439 .phy_id
= MVSWITCH_MAGIC
,
440 .phy_id_mask
= 0xffffffff,
441 .features
= PHY_BASIC_FEATURES
,
442 .probe
= &mvswitch_probe
,
443 .remove
= &mvswitch_remove
,
444 .config_init
= &mvswitch_config_init
,
445 .config_aneg
= &mvswitch_config_aneg
,
446 .read_status
= &mvswitch_read_status
,
447 .driver
= { .owner
= THIS_MODULE
,},
453 phy_register_fixup_for_id(PHY_ANY_ID
, mvswitch_fixup
);
454 return phy_driver_register(&mvswitch_driver
);
460 phy_driver_unregister(&mvswitch_driver
);
463 module_init(mvswitch_init
);
464 module_exit(mvswitch_exit
);