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 struct mvswitch_priv
{
42 /* the driver's tx function */
43 int (*hardstart
)(struct sk_buff
*skb
, struct net_device
*dev
);
44 struct vlan_group
*grp
;
48 #define to_mvsw(_phy) ((struct mvswitch_priv *) (_phy)->priv)
51 r16(struct phy_device
*phydev
, int addr
, int reg
)
53 return phydev
->bus
->read(phydev
->bus
, addr
, reg
);
57 w16(struct phy_device
*phydev
, int addr
, int reg
, u16 val
)
59 phydev
->bus
->write(phydev
->bus
, addr
, reg
, val
);
64 mvswitch_mangle_tx(struct sk_buff
*skb
, struct net_device
*dev
)
66 struct mvswitch_priv
*priv
;
74 if (unlikely(skb
->len
< 16))
78 if (__vlan_hwaccel_get_tag(skb
, &vid
))
81 if ((skb
->len
<= 62) || (skb_headroom(skb
) < MV_HEADER_SIZE
)) {
82 if (pskb_expand_head(skb
, MV_HEADER_SIZE
, 0, GFP_ATOMIC
))
87 buf
= skb_push(skb
, MV_HEADER_SIZE
);
89 if (__vlan_get_tag(skb
, &vid
))
92 if (unlikely((vid
> 15 || !priv
->vlans
[vid
])))
96 if (pskb_expand_head(skb
, 0, 64 + MV_TRAILER_SIZE
- skb
->len
, GFP_ATOMIC
))
100 skb
->len
= 64 + MV_TRAILER_SIZE
;
102 if (skb_cloned(skb
) || unlikely(skb_tailroom(skb
) < 4)) {
103 if (pskb_expand_head(skb
, 0, 4, GFP_ATOMIC
))
106 buf
= skb_put(skb
, 4);
109 /* move the ethernet header 4 bytes forward, overwriting the vlan tag */
110 memmove(skb
->data
+ 4, skb
->data
, 12);
113 skb
->mac_header
+= 4;
121 /* prepend the tag */
122 *((__be16
*) buf
) = cpu_to_be16(
123 ((vid
<< MV_HEADER_VLAN_S
) & MV_HEADER_VLAN_M
) |
124 ((priv
->vlans
[vid
] << MV_HEADER_PORTS_S
) & MV_HEADER_PORTS_M
)
128 *((__be32
*) buf
) = cpu_to_be32((
129 (MV_TRAILER_OVERRIDE
<< MV_TRAILER_FLAGS_S
) |
130 ((priv
->vlans
[vid
] & MV_TRAILER_PORTS_M
) << MV_TRAILER_PORTS_S
)
134 return priv
->hardstart(skb
, dev
);
138 printk("%s: failed to expand/update skb for the switch\n", dev
->name
);
141 /* any errors? drop the packet! */
142 dev_kfree_skb_any(skb
);
147 mvswitch_mangle_rx(struct sk_buff
*skb
, int napi
)
149 struct mvswitch_priv
*priv
;
150 struct net_device
*dev
;
168 skb_pull(skb
, MV_HEADER_SIZE
);
170 buf
= skb
->data
+ skb
->len
- MV_TRAILER_SIZE
;
175 /* look for the vlan matching the incoming port */
176 for (i
= 0; i
< ARRAY_SIZE(priv
->vlans
); i
++) {
177 if ((1 << buf
[1]) & priv
->vlans
[i
])
184 skb
->protocol
= eth_type_trans(skb
, skb
->dev
);
187 return vlan_hwaccel_receive_skb(skb
, priv
->grp
, vlan
);
189 return vlan_hwaccel_rx(skb
, priv
->grp
, vlan
);
192 /* no vlan? eat the packet! */
193 dev_kfree_skb_any(skb
);
199 mvswitch_netif_rx(struct sk_buff
*skb
)
201 return mvswitch_mangle_rx(skb
, 0);
205 mvswitch_netif_receive_skb(struct sk_buff
*skb
)
207 return mvswitch_mangle_rx(skb
, 1);
212 mvswitch_vlan_rx_register(struct net_device
*dev
, struct vlan_group
*grp
)
214 struct mvswitch_priv
*priv
= dev
->phy_ptr
;
220 mvswitch_config_init(struct phy_device
*pdev
)
222 struct mvswitch_priv
*priv
= to_mvsw(pdev
);
223 struct net_device
*dev
= pdev
->attached_dev
;
230 printk("%s: Marvell 88E6060 PHY driver attached.\n", dev
->name
);
231 pdev
->supported
= ADVERTISED_100baseT_Full
;
232 pdev
->advertising
= ADVERTISED_100baseT_Full
;
235 /* initialize default vlans */
236 for (i
= 0; i
< MV_PORTS
; i
++)
237 priv
->vlans
[(i
== MV_WANPORT
? 1 : 0)] |= (1 << i
);
239 /* before entering reset, disable all ports */
240 for (i
= 0; i
< MV_PORTS
; i
++)
241 w16(pdev
, MV_PORTREG(CONTROL
, i
), 0x00);
243 msleep(2); /* wait for the status change to settle in */
245 /* put the device in reset and set ATU flags */
246 w16(pdev
, MV_SWITCHREG(ATU_CTRL
),
249 MV_ATUCTL_AGETIME(4080) /* maximum */
252 i
= 100; /* timeout */
254 if (!(r16(pdev
, MV_SWITCHREG(ATU_CTRL
)) & MV_ATUCTL_RESET
))
260 printk("%s: Timeout waiting for the switch to reset.\n", dev
->name
);
264 /* initialize the cpu port */
265 w16(pdev
, MV_PORTREG(CONTROL
, MV_CPUPORT
),
274 /* wait for the phy change to settle in */
276 for (i
= 0; i
< MV_PORTS
; i
++) {
282 /* look for the matching vlan */
283 for (j
= 0; j
< ARRAY_SIZE(priv
->vlans
); j
++) {
284 if (priv
->vlans
[j
] & (1 << i
)) {
285 vlmap
= priv
->vlans
[j
];
289 /* leave port unconfigured if it's not part of a vlan */
293 /* add the cpu port to the allowed destinations list */
294 vlmap
|= (1 << MV_CPUPORT
);
296 /* take port out of its own vlan destination map */
299 /* apply vlan settings */
300 w16(pdev
, MV_PORTREG(VLANMAP
, i
),
301 MV_PORTVLAN_PORTS(vlmap
) |
306 w16(pdev
, MV_PORTREG(CONTROL
, i
), MV_PORTCTRL_ENABLED
);
309 /* build the target list for the cpu port */
310 for (i
= 0; i
< MV_PORTS
; i
++)
313 w16(pdev
, MV_PORTREG(VLANMAP
, MV_CPUPORT
),
314 MV_PORTVLAN_PORTS(vlmap
)
317 /* set the port association vector */
318 for (i
= 0; i
<= MV_PORTS
; i
++) {
319 w16(pdev
, MV_PORTREG(ASSOC
, i
),
320 MV_PORTASSOC_PORTS(1 << i
)
324 /* init switch control */
325 w16(pdev
, MV_SWITCHREG(CTRL
),
330 /* hook into the tx function */
331 priv
->hardstart
= dev
->hard_start_xmit
;
332 pdev
->netif_receive_skb
= mvswitch_netif_receive_skb
;
333 pdev
->netif_rx
= mvswitch_netif_rx
;
334 dev
->hard_start_xmit
= mvswitch_mangle_tx
;
335 dev
->vlan_rx_register
= mvswitch_vlan_rx_register
;
337 dev
->features
|= NETIF_F_HW_VLAN_RX
| NETIF_F_HW_VLAN_TX
;
339 dev
->features
|= NETIF_F_HW_VLAN_RX
;
346 mvswitch_read_status(struct phy_device
*phydev
)
348 phydev
->speed
= SPEED_100
;
349 phydev
->duplex
= DUPLEX_FULL
;
350 phydev
->state
= PHY_UP
;
355 mvswitch_config_aneg(struct phy_device
*phydev
)
361 mvswitch_remove(struct phy_device
*pdev
)
363 struct mvswitch_priv
*priv
= to_mvsw(pdev
);
364 struct net_device
*dev
= pdev
->attached_dev
;
366 /* restore old xmit handler */
367 if (priv
->hardstart
&& dev
)
368 dev
->hard_start_xmit
= priv
->hardstart
;
369 dev
->vlan_rx_register
= NULL
;
370 dev
->vlan_rx_kill_vid
= NULL
;
372 dev
->features
&= ~NETIF_F_HW_VLAN_RX
;
377 mvswitch_detect(struct mii_bus
*bus
, int addr
)
382 /* we attach to phy id 31 to make sure that the late probe works */
386 /* look for the switch on the bus */
387 reg
= bus
->read(bus
, MV_PORTREG(IDENT
, 0)) & MV_IDENT_MASK
;
388 if (reg
!= MV_IDENT_VALUE
)
392 * Now that we've established that the switch actually exists, let's
393 * get rid of the competition :)
395 for (i
= 0; i
< 31; i
++) {
396 if (!bus
->phy_map
[i
])
399 device_unregister(&bus
->phy_map
[i
]->dev
);
400 kfree(bus
->phy_map
[i
]);
401 bus
->phy_map
[i
] = NULL
;
408 mvswitch_probe(struct phy_device
*pdev
)
410 struct mvswitch_priv
*priv
;
412 priv
= kzalloc(sizeof(struct mvswitch_priv
), GFP_KERNEL
);
422 static struct phy_driver mvswitch_driver
= {
423 .name
= "Marvell 88E6060",
424 .features
= PHY_BASIC_FEATURES
,
425 .detect
= &mvswitch_detect
,
426 .probe
= &mvswitch_probe
,
427 .remove
= &mvswitch_remove
,
428 .config_init
= &mvswitch_config_init
,
429 .config_aneg
= &mvswitch_config_aneg
,
430 .read_status
= &mvswitch_read_status
,
431 .driver
= { .owner
= THIS_MODULE
,},
437 return phy_driver_register(&mvswitch_driver
);
443 phy_driver_unregister(&mvswitch_driver
);
446 module_init(mvswitch_init
);
447 module_exit(mvswitch_exit
);