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 netdev_features_t orig_features;
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);
63 static struct sk_buff *
64 mvswitch_mangle_tx(struct net_device *dev, struct sk_buff *skb)
66 struct mvswitch_priv *priv;
74 if (unlikely(skb->len < 16))
78 if (__vlan_hwaccel_get_tag(skb, &vid))
81 if (skb_cloned(skb) || (skb->len <= 62) || (skb_headroom(skb) < MV_HEADER_SIZE)) {
82 if (pskb_expand_head(skb, MV_HEADER_SIZE, (skb->len < 62 ? 62 - skb->len : 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)
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 net_device *dev, struct sk_buff *skb)
149 struct mvswitch_priv *priv;
155 if (WARN_ON_ONCE(!priv))
160 skb_pull(skb, MV_HEADER_SIZE);
162 buf = skb->data + skb->len - MV_TRAILER_SIZE;
167 /* look for the vlan matching the incoming port */
168 for (i = 0; i < ARRAY_SIZE(priv->vlans); i++) {
169 if ((1 << buf[1]) & priv->vlans[i])
176 __vlan_hwaccel_put_tag(skb, vlan);
181 mvswitch_wait_mask(struct phy_device *pdev, int addr, int reg, u16 mask, u16 val)
187 r = r16(pdev, addr, reg) & mask;
195 mvswitch_config_init(struct phy_device *pdev)
197 struct mvswitch_priv *priv = to_mvsw(pdev);
198 struct net_device *dev = pdev->attached_dev;
205 printk("%s: Marvell 88E6060 PHY driver attached.\n", dev->name);
206 pdev->supported = ADVERTISED_100baseT_Full;
207 pdev->advertising = ADVERTISED_100baseT_Full;
209 pdev->irq = PHY_POLL;
211 dev->flags |= IFF_PROMISC;
214 /* initialize default vlans */
215 for (i = 0; i < MV_PORTS; i++)
216 priv->vlans[(i == MV_WANPORT ? 2 : 1)] |= (1 << i);
218 /* before entering reset, disable all ports */
219 for (i = 0; i < MV_PORTS; i++)
220 w16(pdev, MV_PORTREG(CONTROL, i), 0x00);
222 msleep(2); /* wait for the status change to settle in */
224 /* put the ATU in reset */
225 w16(pdev, MV_SWITCHREG(ATU_CTRL), MV_ATUCTL_RESET);
227 i = mvswitch_wait_mask(pdev, MV_SWITCHREG(ATU_CTRL), MV_ATUCTL_RESET, 0);
229 printk("%s: Timeout waiting for the switch to reset.\n", dev->name);
233 /* set the ATU flags */
234 w16(pdev, MV_SWITCHREG(ATU_CTRL),
237 MV_ATUCTL_AGETIME(MV_ATUCTL_AGETIME_MIN) /* minimum without disabling ageing */
240 /* initialize the cpu port */
241 w16(pdev, MV_PORTREG(CONTROL, MV_CPUPORT),
250 /* wait for the phy change to settle in */
252 for (i = 0; i < MV_PORTS; i++) {
258 /* look for the matching vlan */
259 for (j = 0; j < ARRAY_SIZE(priv->vlans); j++) {
260 if (priv->vlans[j] & (1 << i)) {
261 vlmap = priv->vlans[j];
265 /* leave port unconfigured if it's not part of a vlan */
269 /* add the cpu port to the allowed destinations list */
270 vlmap |= (1 << MV_CPUPORT);
272 /* take port out of its own vlan destination map */
275 /* apply vlan settings */
276 w16(pdev, MV_PORTREG(VLANMAP, i),
277 MV_PORTVLAN_PORTS(vlmap) |
282 w16(pdev, MV_PORTREG(CONTROL, i),
287 w16(pdev, MV_PORTREG(VLANMAP, MV_CPUPORT),
288 MV_PORTVLAN_ID(MV_CPUPORT)
291 /* set the port association vector */
292 for (i = 0; i <= MV_PORTS; i++) {
293 w16(pdev, MV_PORTREG(ASSOC, i),
294 MV_PORTASSOC_PORTS(1 << i)
298 /* init switch control */
299 w16(pdev, MV_SWITCHREG(CTRL),
304 dev->eth_mangle_rx = mvswitch_mangle_rx;
305 dev->eth_mangle_tx = mvswitch_mangle_tx;
306 priv->orig_features = dev->features;
309 dev->priv_flags |= IFF_NO_IP_ALIGN;
310 dev->features |= NETIF_F_HW_VLAN_RX | NETIF_F_HW_VLAN_TX;
312 dev->features |= NETIF_F_HW_VLAN_RX;
319 mvswitch_read_status(struct phy_device *pdev)
321 pdev->speed = SPEED_100;
322 pdev->duplex = DUPLEX_FULL;
325 /* XXX ugly workaround: we can't force the switch
326 * to gracefully handle hosts moving from one port to another,
327 * so we have to regularly clear the ATU database */
329 /* wait for the ATU to become available */
330 mvswitch_wait_mask(pdev, MV_SWITCHREG(ATU_OP), MV_ATUOP_INPROGRESS, 0);
333 w16(pdev, MV_SWITCHREG(ATU_OP),
334 MV_ATUOP_INPROGRESS |
338 /* wait for operation to complete */
339 mvswitch_wait_mask(pdev, MV_SWITCHREG(ATU_OP), MV_ATUOP_INPROGRESS, 0);
345 mvswitch_config_aneg(struct phy_device *phydev)
351 mvswitch_remove(struct phy_device *pdev)
353 struct mvswitch_priv *priv = to_mvsw(pdev);
354 struct net_device *dev = pdev->attached_dev;
357 dev->eth_mangle_rx = NULL;
358 dev->eth_mangle_tx = NULL;
359 dev->features = priv->orig_features;
360 dev->priv_flags &= ~IFF_NO_IP_ALIGN;
365 mvswitch_probe(struct phy_device *pdev)
367 struct mvswitch_priv *priv;
369 priv = kzalloc(sizeof(struct mvswitch_priv), GFP_KERNEL);
379 mvswitch_fixup(struct phy_device *dev)
383 if (dev->addr != 0x10)
386 reg = dev->bus->read(dev->bus, MV_PORTREG(IDENT, 0)) & MV_IDENT_MASK;
387 if (reg != MV_IDENT_VALUE)
390 dev->phy_id = MVSWITCH_MAGIC;
395 static struct phy_driver mvswitch_driver = {
396 .name = "Marvell 88E6060",
397 .phy_id = MVSWITCH_MAGIC,
398 .phy_id_mask = 0xffffffff,
399 .features = PHY_BASIC_FEATURES,
400 .probe = &mvswitch_probe,
401 .remove = &mvswitch_remove,
402 .config_init = &mvswitch_config_init,
403 .config_aneg = &mvswitch_config_aneg,
404 .read_status = &mvswitch_read_status,
405 .driver = { .owner = THIS_MODULE,},
411 phy_register_fixup_for_id(PHY_ANY_ID, mvswitch_fixup);
412 return phy_driver_register(&mvswitch_driver);
418 phy_driver_unregister(&mvswitch_driver);
421 module_init(mvswitch_init);
422 module_exit(mvswitch_exit);