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 MODULE_DESCRIPTION("Marvell 88E6060 Switch driver");
34 MODULE_AUTHOR("Felix Fietkau");
35 MODULE_LICENSE("GPL");
37 struct mvswitch_priv
{
38 /* the driver's tx function */
39 int (*hardstart
)(struct sk_buff
*skb
, struct net_device
*dev
);
40 struct vlan_group
*grp
;
44 #define to_mvsw(_phy) ((struct mvswitch_priv *) (_phy)->priv)
47 r16(struct phy_device
*phydev
, int addr
, int reg
)
49 return phydev
->bus
->read(phydev
->bus
, addr
, reg
);
53 w16(struct phy_device
*phydev
, int addr
, int reg
, u16 val
)
55 phydev
->bus
->write(phydev
->bus
, addr
, reg
, val
);
59 mvswitch_mangle_tx(struct sk_buff
*skb
, struct net_device
*dev
)
61 struct mvswitch_priv
*priv
;
62 struct vlan_ethhdr
*eh
;
70 if (unlikely(skb
->len
< 16))
73 eh
= (struct vlan_ethhdr
*) skb
->data
;
74 if (be16_to_cpu(eh
->h_vlan_proto
) != 0x8100)
77 vid
= be16_to_cpu(eh
->h_vlan_TCI
) & VLAN_VID_MASK
;
78 if (unlikely((vid
> 15 || !priv
->vlans
[vid
])))
82 if (pskb_expand_head(skb
, 0, 68 - skb
->len
, GFP_ATOMIC
)) {
84 printk("%s: failed to expand/update skb for the switch\n", dev
->name
);
91 if (skb_cloned(skb
) || unlikely(skb_tailroom(skb
) < 4)) {
92 if (pskb_expand_head(skb
, 0, 4, GFP_ATOMIC
)) {
94 printk("%s: failed to expand/update skb for the switch\n", dev
->name
);
98 buf
= skb_put(skb
, 4);
101 /* move the ethernet header 4 bytes forward, overwriting the vlan tag */
102 memmove(skb
->data
+ 4, skb
->data
, 12);
105 skb
->mac_header
+= 4;
113 ((priv
->vlans
[vid
] & 0x1f) << 16)
116 return priv
->hardstart(skb
, dev
);
119 /* any errors? drop the packet! */
120 dev_kfree_skb_any(skb
);
125 mvswitch_mangle_rx(struct sk_buff
*skb
, int napi
)
127 struct mvswitch_priv
*priv
;
128 struct net_device
*dev
;
144 buf
= skb
->data
+ skb
->len
- 4;
148 /* look for the vlan matching the incoming port */
149 for (i
= 0; i
< ARRAY_SIZE(priv
->vlans
); i
++) {
150 if ((1 << buf
[1]) & priv
->vlans
[i
])
158 return vlan_hwaccel_receive_skb(skb
, priv
->grp
, vlan
);
160 return vlan_hwaccel_rx(skb
, priv
->grp
, vlan
);
163 /* no vlan? eat the packet! */
164 dev_kfree_skb_any(skb
);
170 mvswitch_netif_rx(struct sk_buff
*skb
)
172 return mvswitch_mangle_rx(skb
, 0);
176 mvswitch_netif_receive_skb(struct sk_buff
*skb
)
178 return mvswitch_mangle_rx(skb
, 1);
183 mvswitch_vlan_rx_register(struct net_device
*dev
, struct vlan_group
*grp
)
185 struct mvswitch_priv
*priv
= dev
->phy_ptr
;
191 mvswitch_config_init(struct phy_device
*pdev
)
193 struct mvswitch_priv
*priv
= to_mvsw(pdev
);
194 struct net_device
*dev
= pdev
->attached_dev
;
201 printk("%s: Marvell 88E6060 PHY driver attached.\n", dev
->name
);
202 pdev
->supported
= ADVERTISED_100baseT_Full
;
203 pdev
->advertising
= ADVERTISED_100baseT_Full
;
206 /* initialize default vlans */
207 for (i
= 0; i
< MV_PORTS
; i
++)
208 priv
->vlans
[(i
== MV_WANPORT
? 1 : 0)] |= (1 << i
);
210 /* before entering reset, disable all ports */
211 for (i
= 0; i
< MV_PORTS
; i
++)
212 w16(pdev
, MV_PORTREG(CONTROL
, i
), 0x00);
214 msleep(2); /* wait for the status change to settle in */
216 /* put the device in reset and set ATU flags */
217 w16(pdev
, MV_SWITCHREG(ATU_CTRL
),
220 MV_ATUCTL_AGETIME(4080) /* maximum */
223 i
= 100; /* timeout */
225 if (!(r16(pdev
, MV_SWITCHREG(ATU_CTRL
)) & MV_ATUCTL_RESET
))
231 printk("%s: Timeout waiting for the switch to reset.\n", dev
->name
);
235 /* initialize the cpu port */
236 w16(pdev
, MV_PORTREG(CONTROL
, MV_CPUPORT
),
237 MV_PORTCTRL_ENABLED
|
241 /* wait for the phy change to settle in */
243 for (i
= 0; i
< MV_PORTS
; i
++) {
249 /* look for the matching vlan */
250 for (j
= 0; j
< ARRAY_SIZE(priv
->vlans
); j
++) {
251 if (priv
->vlans
[j
] & (1 << i
)) {
252 vlmap
= priv
->vlans
[j
];
256 /* leave port unconfigured if it's not part of a vlan */
260 /* add the cpu port to the allowed destinations list */
261 vlmap
|= (1 << MV_CPUPORT
);
263 /* take port out of its own vlan destination map */
266 /* apply vlan settings */
267 w16(pdev
, MV_PORTREG(VLANMAP
, i
),
268 MV_PORTVLAN_PORTS(vlmap
) |
273 w16(pdev
, MV_PORTREG(CONTROL
, i
), MV_PORTCTRL_ENABLED
);
276 /* build the target list for the cpu port */
277 for (i
= 0, vlmap
= 0; i
< ARRAY_SIZE(priv
->vlans
); i
++)
278 vlmap
|= priv
->vlans
[i
];
280 w16(pdev
, MV_PORTREG(VLANMAP
, MV_CPUPORT
),
281 MV_PORTVLAN_PORTS(vlmap
)
284 /* set the port association vector */
285 for (i
= 0; i
<= MV_PORTS
; i
++) {
286 w16(pdev
, MV_PORTREG(ASSOC
, i
),
287 MV_PORTASSOC_PORTS(1 << i
)
291 /* hook into the tx function */
292 priv
->hardstart
= dev
->hard_start_xmit
;
293 pdev
->netif_receive_skb
= mvswitch_netif_receive_skb
;
294 pdev
->netif_rx
= mvswitch_netif_rx
;
295 dev
->hard_start_xmit
= mvswitch_mangle_tx
;
296 dev
->vlan_rx_register
= mvswitch_vlan_rx_register
;
297 dev
->features
|= NETIF_F_HW_VLAN_RX
;
303 mvswitch_read_status(struct phy_device
*phydev
)
305 phydev
->speed
= SPEED_100
;
306 phydev
->duplex
= DUPLEX_FULL
;
307 phydev
->state
= PHY_UP
;
312 mvswitch_config_aneg(struct phy_device
*phydev
)
318 mvswitch_remove(struct phy_device
*pdev
)
320 struct mvswitch_priv
*priv
= to_mvsw(pdev
);
321 struct net_device
*dev
= pdev
->attached_dev
;
323 /* restore old xmit handler */
324 if (priv
->hardstart
&& dev
)
325 dev
->hard_start_xmit
= priv
->hardstart
;
326 dev
->vlan_rx_register
= NULL
;
327 dev
->vlan_rx_kill_vid
= NULL
;
329 dev
->features
&= ~NETIF_F_HW_VLAN_RX
;
334 mvswitch_detect(struct mii_bus
*bus
, int addr
)
339 /* we attach to phy id 31 to make sure that the late probe works */
343 /* look for the switch on the bus */
344 reg
= bus
->read(bus
, MV_PORTREG(IDENT
, 0)) & MV_IDENT_MASK
;
345 if (reg
!= MV_IDENT_VALUE
)
349 * Now that we've established that the switch actually exists, let's
350 * get rid of the competition :)
352 for (i
= 0; i
< 31; i
++) {
353 if (!bus
->phy_map
[i
])
356 device_unregister(&bus
->phy_map
[i
]->dev
);
357 kfree(bus
->phy_map
[i
]);
358 bus
->phy_map
[i
] = NULL
;
365 mvswitch_probe(struct phy_device
*pdev
)
367 struct mvswitch_priv
*priv
;
369 priv
= kzalloc(sizeof(struct mvswitch_priv
), GFP_KERNEL
);
379 static struct phy_driver mvswitch_driver
= {
380 .name
= "Marvell 88E6060",
381 .features
= PHY_BASIC_FEATURES
,
382 .detect
= &mvswitch_detect
,
383 .probe
= &mvswitch_probe
,
384 .remove
= &mvswitch_remove
,
385 .config_init
= &mvswitch_config_init
,
386 .config_aneg
= &mvswitch_config_aneg
,
387 .read_status
= &mvswitch_read_status
,
388 .driver
= { .owner
= THIS_MODULE
,},
394 return phy_driver_register(&mvswitch_driver
);
400 phy_driver_unregister(&mvswitch_driver
);
403 module_init(mvswitch_init
);
404 module_exit(mvswitch_exit
);