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; i < MV_PORTS; 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 /* init switch control */
292 w16(pdev, MV_SWITCHREG(CTRL),
297 /* hook into the tx function */
298 priv->hardstart = dev->hard_start_xmit;
299 pdev->netif_receive_skb = mvswitch_netif_receive_skb;
300 pdev->netif_rx = mvswitch_netif_rx;
301 dev->hard_start_xmit = mvswitch_mangle_tx;
302 dev->vlan_rx_register = mvswitch_vlan_rx_register;
303 dev->features |= NETIF_F_HW_VLAN_RX;
309 mvswitch_read_status(struct phy_device *phydev)
311 phydev->speed = SPEED_100;
312 phydev->duplex = DUPLEX_FULL;
313 phydev->state = PHY_UP;
318 mvswitch_config_aneg(struct phy_device *phydev)
324 mvswitch_remove(struct phy_device *pdev)
326 struct mvswitch_priv *priv = to_mvsw(pdev);
327 struct net_device *dev = pdev->attached_dev;
329 /* restore old xmit handler */
330 if (priv->hardstart && dev)
331 dev->hard_start_xmit = priv->hardstart;
332 dev->vlan_rx_register = NULL;
333 dev->vlan_rx_kill_vid = NULL;
335 dev->features &= ~NETIF_F_HW_VLAN_RX;
340 mvswitch_detect(struct mii_bus *bus, int addr)
345 /* we attach to phy id 31 to make sure that the late probe works */
349 /* look for the switch on the bus */
350 reg = bus->read(bus, MV_PORTREG(IDENT, 0)) & MV_IDENT_MASK;
351 if (reg != MV_IDENT_VALUE)
355 * Now that we've established that the switch actually exists, let's
356 * get rid of the competition :)
358 for (i = 0; i < 31; i++) {
359 if (!bus->phy_map[i])
362 device_unregister(&bus->phy_map[i]->dev);
363 kfree(bus->phy_map[i]);
364 bus->phy_map[i] = NULL;
371 mvswitch_probe(struct phy_device *pdev)
373 struct mvswitch_priv *priv;
375 priv = kzalloc(sizeof(struct mvswitch_priv), GFP_KERNEL);
385 static struct phy_driver mvswitch_driver = {
386 .name = "Marvell 88E6060",
387 .features = PHY_BASIC_FEATURES,
388 .detect = &mvswitch_detect,
389 .probe = &mvswitch_probe,
390 .remove = &mvswitch_remove,
391 .config_init = &mvswitch_config_init,
392 .config_aneg = &mvswitch_config_aneg,
393 .read_status = &mvswitch_read_status,
394 .driver = { .owner = THIS_MODULE,},
400 return phy_driver_register(&mvswitch_driver);
406 phy_driver_unregister(&mvswitch_driver);
409 module_init(mvswitch_init);
410 module_exit(mvswitch_exit);