v1.5 branch refresh based upon upstream master @ c8677ca89e53e3be7988d54280fce166cc894a7e
[librecmc/librecmc.git] / target / linux / generic / files / drivers / net / phy / mvswitch.c
1 /*
2  * Marvell 88E6060 switch driver
3  * Copyright (c) 2008 Felix Fietkau <nbd@nbd.name>
4  *
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
8  */
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>
21 #include <linux/mm.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>
27
28 #include <asm/io.h>
29 #include <asm/irq.h>
30 #include <asm/uaccess.h>
31 #include "mvswitch.h"
32
33 /* Undefine this to use trailer mode instead.
34  * I don't know if header mode works with all chips */
35 #define HEADER_MODE     1
36
37 MODULE_DESCRIPTION("Marvell 88E6060 Switch driver");
38 MODULE_AUTHOR("Felix Fietkau");
39 MODULE_LICENSE("GPL");
40
41 #define MVSWITCH_MAGIC 0x88E6060
42
43 struct mvswitch_priv {
44         netdev_features_t orig_features;
45         u8 vlans[16];
46 };
47
48 #define to_mvsw(_phy) ((struct mvswitch_priv *) (_phy)->priv)
49
50 static inline u16
51 r16(struct phy_device *phydev, int addr, int reg)
52 {
53         struct mii_bus *bus = phydev->mdio.bus;
54
55         return bus->read(bus, addr, reg);
56 }
57
58 static inline void
59 w16(struct phy_device *phydev, int addr, int reg, u16 val)
60 {
61         struct mii_bus *bus = phydev->mdio.bus;
62
63         bus->write(bus, addr, reg, val);
64 }
65
66
67 static struct sk_buff *
68 mvswitch_mangle_tx(struct net_device *dev, struct sk_buff *skb)
69 {
70         struct mvswitch_priv *priv;
71         char *buf = NULL;
72         u16 vid;
73
74         priv = dev->phy_ptr;
75         if (unlikely(!priv))
76                 goto error;
77
78         if (unlikely(skb->len < 16))
79                 goto error;
80
81 #ifdef HEADER_MODE
82         if (__vlan_hwaccel_get_tag(skb, &vid))
83                 goto error;
84
85         if (skb_cloned(skb) || (skb->len <= 62) || (skb_headroom(skb) < MV_HEADER_SIZE)) {
86                 if (pskb_expand_head(skb, MV_HEADER_SIZE, (skb->len < 62 ? 62 - skb->len : 0), GFP_ATOMIC))
87                         goto error_expand;
88                 if (skb->len < 62)
89                         skb->len = 62;
90         }
91         buf = skb_push(skb, MV_HEADER_SIZE);
92 #else
93         if (__vlan_get_tag(skb, &vid))
94                 goto error;
95
96         if (unlikely((vid > 15 || !priv->vlans[vid])))
97                 goto error;
98
99         if (skb->len <= 64) {
100                 if (pskb_expand_head(skb, 0, 64 + MV_TRAILER_SIZE - skb->len, GFP_ATOMIC))
101                         goto error_expand;
102
103                 buf = skb->data + 64;
104                 skb->len = 64 + MV_TRAILER_SIZE;
105         } else {
106                 if (skb_cloned(skb) || unlikely(skb_tailroom(skb) < 4)) {
107                         if (pskb_expand_head(skb, 0, 4, GFP_ATOMIC))
108                                 goto error_expand;
109                 }
110                 buf = skb_put(skb, 4);
111         }
112
113         /* move the ethernet header 4 bytes forward, overwriting the vlan tag */
114         memmove(skb->data + 4, skb->data, 12);
115         skb->data += 4;
116         skb->len -= 4;
117         skb->mac_header += 4;
118 #endif
119
120         if (!buf)
121                 goto error;
122
123
124 #ifdef HEADER_MODE
125         /* prepend the tag */
126         *((__be16 *) buf) = cpu_to_be16(
127                 ((vid << MV_HEADER_VLAN_S) & MV_HEADER_VLAN_M) |
128                 ((priv->vlans[vid] << MV_HEADER_PORTS_S) & MV_HEADER_PORTS_M)
129         );
130 #else
131         /* append the tag */
132         *((__be32 *) buf) = cpu_to_be32((
133                 (MV_TRAILER_OVERRIDE << MV_TRAILER_FLAGS_S) |
134                 ((priv->vlans[vid] & MV_TRAILER_PORTS_M) << MV_TRAILER_PORTS_S)
135         ));
136 #endif
137
138         return skb;
139
140 error_expand:
141         if (net_ratelimit())
142                 printk("%s: failed to expand/update skb for the switch\n", dev->name);
143
144 error:
145         /* any errors? drop the packet! */
146         dev_kfree_skb_any(skb);
147         return NULL;
148 }
149
150 static void
151 mvswitch_mangle_rx(struct net_device *dev, struct sk_buff *skb)
152 {
153         struct mvswitch_priv *priv;
154         unsigned char *buf;
155         int vlan = -1;
156         int i;
157
158         priv = dev->phy_ptr;
159         if (WARN_ON_ONCE(!priv))
160                 return;
161
162 #ifdef HEADER_MODE
163         buf = skb->data;
164         skb_pull(skb, MV_HEADER_SIZE);
165 #else
166         buf = skb->data + skb->len - MV_TRAILER_SIZE;
167         if (buf[0] != 0x80)
168                 return;
169 #endif
170
171         /* look for the vlan matching the incoming port */
172         for (i = 0; i < ARRAY_SIZE(priv->vlans); i++) {
173                 if ((1 << buf[1]) & priv->vlans[i])
174                         vlan = i;
175         }
176
177         if (vlan == -1)
178                 return;
179
180         __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vlan);
181 }
182
183
184 static int
185 mvswitch_wait_mask(struct phy_device *pdev, int addr, int reg, u16 mask, u16 val)
186 {
187         int i = 100;
188         u16 r;
189
190         do {
191                 r = r16(pdev, addr, reg) & mask;
192                 if (r == val)
193                         return 0;
194         } while(--i > 0);
195         return -ETIMEDOUT;
196 }
197
198 static int
199 mvswitch_config_init(struct phy_device *pdev)
200 {
201         struct mvswitch_priv *priv = to_mvsw(pdev);
202         struct net_device *dev = pdev->attached_dev;
203         u8 vlmap = 0;
204         int i;
205
206         if (!dev)
207                 return -EINVAL;
208
209         printk("%s: Marvell 88E6060 PHY driver attached.\n", dev->name);
210         pdev->supported = ADVERTISED_100baseT_Full;
211         pdev->advertising = ADVERTISED_100baseT_Full;
212         dev->phy_ptr = priv;
213         pdev->irq = PHY_POLL;
214 #ifdef HEADER_MODE
215         dev->flags |= IFF_PROMISC;
216 #endif
217
218         /* initialize default vlans */
219         for (i = 0; i < MV_PORTS; i++)
220                 priv->vlans[(i == MV_WANPORT ? 2 : 1)] |= (1 << i);
221
222         /* before entering reset, disable all ports */
223         for (i = 0; i < MV_PORTS; i++)
224                 w16(pdev, MV_PORTREG(CONTROL, i), 0x00);
225
226         msleep(2); /* wait for the status change to settle in */
227
228         /* put the ATU in reset */
229         w16(pdev, MV_SWITCHREG(ATU_CTRL), MV_ATUCTL_RESET);
230
231         i = mvswitch_wait_mask(pdev, MV_SWITCHREG(ATU_CTRL), MV_ATUCTL_RESET, 0);
232         if (i < 0) {
233                 printk("%s: Timeout waiting for the switch to reset.\n", dev->name);
234                 return i;
235         }
236
237         /* set the ATU flags */
238         w16(pdev, MV_SWITCHREG(ATU_CTRL),
239                 MV_ATUCTL_NO_LEARN |
240                 MV_ATUCTL_ATU_1K |
241                 MV_ATUCTL_AGETIME(MV_ATUCTL_AGETIME_MIN) /* minimum without disabling ageing */
242         );
243
244         /* initialize the cpu port */
245         w16(pdev, MV_PORTREG(CONTROL, MV_CPUPORT),
246 #ifdef HEADER_MODE
247                 MV_PORTCTRL_HEADER |
248 #else
249                 MV_PORTCTRL_RXTR |
250                 MV_PORTCTRL_TXTR |
251 #endif
252                 MV_PORTCTRL_ENABLED
253         );
254         /* wait for the phy change to settle in */
255         msleep(2);
256         for (i = 0; i < MV_PORTS; i++) {
257                 u8 pvid = 0;
258                 int j;
259
260                 vlmap = 0;
261
262                 /* look for the matching vlan */
263                 for (j = 0; j < ARRAY_SIZE(priv->vlans); j++) {
264                         if (priv->vlans[j] & (1 << i)) {
265                                 vlmap = priv->vlans[j];
266                                 pvid = j;
267                         }
268                 }
269                 /* leave port unconfigured if it's not part of a vlan */
270                 if (!vlmap)
271                         continue;
272
273                 /* add the cpu port to the allowed destinations list */
274                 vlmap |= (1 << MV_CPUPORT);
275
276                 /* take port out of its own vlan destination map */
277                 vlmap &= ~(1 << i);
278
279                 /* apply vlan settings */
280                 w16(pdev, MV_PORTREG(VLANMAP, i),
281                         MV_PORTVLAN_PORTS(vlmap) |
282                         MV_PORTVLAN_ID(i)
283                 );
284
285                 /* re-enable port */
286                 w16(pdev, MV_PORTREG(CONTROL, i),
287                         MV_PORTCTRL_ENABLED
288                 );
289         }
290
291         w16(pdev, MV_PORTREG(VLANMAP, MV_CPUPORT),
292                 MV_PORTVLAN_ID(MV_CPUPORT)
293         );
294
295         /* set the port association vector */
296         for (i = 0; i <= MV_PORTS; i++) {
297                 w16(pdev, MV_PORTREG(ASSOC, i),
298                         MV_PORTASSOC_PORTS(1 << i)
299                 );
300         }
301
302         /* init switch control */
303         w16(pdev, MV_SWITCHREG(CTRL),
304                 MV_SWITCHCTL_MSIZE |
305                 MV_SWITCHCTL_DROP
306         );
307
308         dev->eth_mangle_rx = mvswitch_mangle_rx;
309         dev->eth_mangle_tx = mvswitch_mangle_tx;
310         priv->orig_features = dev->features;
311
312 #ifdef HEADER_MODE
313         dev->priv_flags |= IFF_NO_IP_ALIGN;
314         dev->features |= NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_TX;
315 #else
316         dev->features |= NETIF_F_HW_VLAN_CTAG_RX;
317 #endif
318
319         return 0;
320 }
321
322 static int
323 mvswitch_read_status(struct phy_device *pdev)
324 {
325         pdev->speed = SPEED_100;
326         pdev->duplex = DUPLEX_FULL;
327         pdev->link = 1;
328
329         /* XXX ugly workaround: we can't force the switch
330          * to gracefully handle hosts moving from one port to another,
331          * so we have to regularly clear the ATU database */
332
333         /* wait for the ATU to become available */
334         mvswitch_wait_mask(pdev, MV_SWITCHREG(ATU_OP), MV_ATUOP_INPROGRESS, 0);
335
336         /* flush the ATU */
337         w16(pdev, MV_SWITCHREG(ATU_OP),
338                 MV_ATUOP_INPROGRESS |
339                 MV_ATUOP_FLUSH_ALL
340         );
341
342         /* wait for operation to complete */
343         mvswitch_wait_mask(pdev, MV_SWITCHREG(ATU_OP), MV_ATUOP_INPROGRESS, 0);
344
345         return 0;
346 }
347
348 static int
349 mvswitch_aneg_done(struct phy_device *phydev)
350 {
351         return 1;       /* Return any positive value */
352 }
353
354 static int
355 mvswitch_config_aneg(struct phy_device *phydev)
356 {
357         return 0;
358 }
359
360 static void
361 mvswitch_detach(struct phy_device *pdev)
362 {
363         struct mvswitch_priv *priv = to_mvsw(pdev);
364         struct net_device *dev = pdev->attached_dev;
365
366         if (!dev)
367                 return;
368
369         dev->phy_ptr = NULL;
370         dev->eth_mangle_rx = NULL;
371         dev->eth_mangle_tx = NULL;
372         dev->features = priv->orig_features;
373         dev->priv_flags &= ~IFF_NO_IP_ALIGN;
374 }
375
376 static void
377 mvswitch_remove(struct phy_device *pdev)
378 {
379         struct mvswitch_priv *priv = to_mvsw(pdev);
380
381         kfree(priv);
382 }
383
384 static int
385 mvswitch_probe(struct phy_device *pdev)
386 {
387         struct mvswitch_priv *priv;
388
389         priv = kzalloc(sizeof(struct mvswitch_priv), GFP_KERNEL);
390         if (priv == NULL)
391                 return -ENOMEM;
392
393         pdev->priv = priv;
394
395         return 0;
396 }
397
398 static int
399 mvswitch_fixup(struct phy_device *dev)
400 {
401         struct mii_bus *bus = dev->mdio.bus;
402         u16 reg;
403
404         if (dev->mdio.addr != 0x10)
405                 return 0;
406
407         reg = bus->read(bus, MV_PORTREG(IDENT, 0)) & MV_IDENT_MASK;
408         if (reg != MV_IDENT_VALUE)
409                 return 0;
410
411         dev->phy_id = MVSWITCH_MAGIC;
412         return 0;
413 }
414
415
416 static struct phy_driver mvswitch_driver = {
417         .name           = "Marvell 88E6060",
418         .phy_id         = MVSWITCH_MAGIC,
419         .phy_id_mask    = 0xffffffff,
420         .features       = PHY_BASIC_FEATURES,
421         .probe          = &mvswitch_probe,
422         .remove         = &mvswitch_remove,
423         .detach         = &mvswitch_detach,
424         .config_init    = &mvswitch_config_init,
425         .config_aneg    = &mvswitch_config_aneg,
426         .aneg_done      = &mvswitch_aneg_done,
427         .read_status    = &mvswitch_read_status,
428 };
429
430 static int __init
431 mvswitch_init(void)
432 {
433         phy_register_fixup_for_id(PHY_ANY_ID, mvswitch_fixup);
434         return phy_driver_register(&mvswitch_driver, THIS_MODULE);
435 }
436
437 static void __exit
438 mvswitch_exit(void)
439 {
440         phy_driver_unregister(&mvswitch_driver);
441 }
442
443 module_init(mvswitch_init);
444 module_exit(mvswitch_exit);