af31d0b5ac009e818903d285214ad92b02d3adb0
[openwrt/staging/yousong.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 return phydev->bus->read(phydev->bus, addr, reg);
54 }
55
56 static inline void
57 w16(struct phy_device *phydev, int addr, int reg, u16 val)
58 {
59 phydev->bus->write(phydev->bus, addr, reg, val);
60 }
61
62
63 static struct sk_buff *
64 mvswitch_mangle_tx(struct net_device *dev, struct sk_buff *skb)
65 {
66 struct mvswitch_priv *priv;
67 char *buf = NULL;
68 u16 vid;
69
70 priv = dev->phy_ptr;
71 if (unlikely(!priv))
72 goto error;
73
74 if (unlikely(skb->len < 16))
75 goto error;
76
77 #ifdef HEADER_MODE
78 if (__vlan_hwaccel_get_tag(skb, &vid))
79 goto error;
80
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))
83 goto error_expand;
84 if (skb->len < 62)
85 skb->len = 62;
86 }
87 buf = skb_push(skb, MV_HEADER_SIZE);
88 #else
89 if (__vlan_get_tag(skb, &vid))
90 goto error;
91
92 if (unlikely((vid > 15 || !priv->vlans[vid])))
93 goto error;
94
95 if (skb->len <= 64) {
96 if (pskb_expand_head(skb, 0, 64 + MV_TRAILER_SIZE - skb->len, GFP_ATOMIC))
97 goto error_expand;
98
99 buf = skb->data + 64;
100 skb->len = 64 + MV_TRAILER_SIZE;
101 } else {
102 if (skb_cloned(skb) || unlikely(skb_tailroom(skb) < 4)) {
103 if (pskb_expand_head(skb, 0, 4, GFP_ATOMIC))
104 goto error_expand;
105 }
106 buf = skb_put(skb, 4);
107 }
108
109 /* move the ethernet header 4 bytes forward, overwriting the vlan tag */
110 memmove(skb->data + 4, skb->data, 12);
111 skb->data += 4;
112 skb->len -= 4;
113 skb->mac_header += 4;
114 #endif
115
116 if (!buf)
117 goto error;
118
119
120 #ifdef HEADER_MODE
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)
125 );
126 #else
127 /* append the tag */
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)
131 ));
132 #endif
133
134 return skb;
135
136 error_expand:
137 if (net_ratelimit())
138 printk("%s: failed to expand/update skb for the switch\n", dev->name);
139
140 error:
141 /* any errors? drop the packet! */
142 dev_kfree_skb_any(skb);
143 return NULL;
144 }
145
146 static void
147 mvswitch_mangle_rx(struct net_device *dev, struct sk_buff *skb)
148 {
149 struct mvswitch_priv *priv;
150 unsigned char *buf;
151 int vlan = -1;
152 int i;
153
154 priv = dev->phy_ptr;
155 if (WARN_ON_ONCE(!priv))
156 return;
157
158 #ifdef HEADER_MODE
159 buf = skb->data;
160 skb_pull(skb, MV_HEADER_SIZE);
161 #else
162 buf = skb->data + skb->len - MV_TRAILER_SIZE;
163 if (buf[0] != 0x80)
164 return;
165 #endif
166
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])
170 vlan = i;
171 }
172
173 if (vlan == -1)
174 return;
175
176 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vlan);
177 }
178
179
180 static int
181 mvswitch_wait_mask(struct phy_device *pdev, int addr, int reg, u16 mask, u16 val)
182 {
183 int i = 100;
184 u16 r;
185
186 do {
187 r = r16(pdev, addr, reg) & mask;
188 if (r == val)
189 return 0;
190 } while(--i > 0);
191 return -ETIMEDOUT;
192 }
193
194 static int
195 mvswitch_config_init(struct phy_device *pdev)
196 {
197 struct mvswitch_priv *priv = to_mvsw(pdev);
198 struct net_device *dev = pdev->attached_dev;
199 u8 vlmap = 0;
200 int i;
201
202 if (!dev)
203 return -EINVAL;
204
205 printk("%s: Marvell 88E6060 PHY driver attached.\n", dev->name);
206 pdev->supported = ADVERTISED_100baseT_Full;
207 pdev->advertising = ADVERTISED_100baseT_Full;
208 dev->phy_ptr = priv;
209 pdev->irq = PHY_POLL;
210 #ifdef HEADER_MODE
211 dev->flags |= IFF_PROMISC;
212 #endif
213
214 /* initialize default vlans */
215 for (i = 0; i < MV_PORTS; i++)
216 priv->vlans[(i == MV_WANPORT ? 2 : 1)] |= (1 << i);
217
218 /* before entering reset, disable all ports */
219 for (i = 0; i < MV_PORTS; i++)
220 w16(pdev, MV_PORTREG(CONTROL, i), 0x00);
221
222 msleep(2); /* wait for the status change to settle in */
223
224 /* put the ATU in reset */
225 w16(pdev, MV_SWITCHREG(ATU_CTRL), MV_ATUCTL_RESET);
226
227 i = mvswitch_wait_mask(pdev, MV_SWITCHREG(ATU_CTRL), MV_ATUCTL_RESET, 0);
228 if (i < 0) {
229 printk("%s: Timeout waiting for the switch to reset.\n", dev->name);
230 return i;
231 }
232
233 /* set the ATU flags */
234 w16(pdev, MV_SWITCHREG(ATU_CTRL),
235 MV_ATUCTL_NO_LEARN |
236 MV_ATUCTL_ATU_1K |
237 MV_ATUCTL_AGETIME(MV_ATUCTL_AGETIME_MIN) /* minimum without disabling ageing */
238 );
239
240 /* initialize the cpu port */
241 w16(pdev, MV_PORTREG(CONTROL, MV_CPUPORT),
242 #ifdef HEADER_MODE
243 MV_PORTCTRL_HEADER |
244 #else
245 MV_PORTCTRL_RXTR |
246 MV_PORTCTRL_TXTR |
247 #endif
248 MV_PORTCTRL_ENABLED
249 );
250 /* wait for the phy change to settle in */
251 msleep(2);
252 for (i = 0; i < MV_PORTS; i++) {
253 u8 pvid = 0;
254 int j;
255
256 vlmap = 0;
257
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];
262 pvid = j;
263 }
264 }
265 /* leave port unconfigured if it's not part of a vlan */
266 if (!vlmap)
267 continue;
268
269 /* add the cpu port to the allowed destinations list */
270 vlmap |= (1 << MV_CPUPORT);
271
272 /* take port out of its own vlan destination map */
273 vlmap &= ~(1 << i);
274
275 /* apply vlan settings */
276 w16(pdev, MV_PORTREG(VLANMAP, i),
277 MV_PORTVLAN_PORTS(vlmap) |
278 MV_PORTVLAN_ID(i)
279 );
280
281 /* re-enable port */
282 w16(pdev, MV_PORTREG(CONTROL, i),
283 MV_PORTCTRL_ENABLED
284 );
285 }
286
287 w16(pdev, MV_PORTREG(VLANMAP, MV_CPUPORT),
288 MV_PORTVLAN_ID(MV_CPUPORT)
289 );
290
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)
295 );
296 }
297
298 /* init switch control */
299 w16(pdev, MV_SWITCHREG(CTRL),
300 MV_SWITCHCTL_MSIZE |
301 MV_SWITCHCTL_DROP
302 );
303
304 dev->eth_mangle_rx = mvswitch_mangle_rx;
305 dev->eth_mangle_tx = mvswitch_mangle_tx;
306 priv->orig_features = dev->features;
307
308 #ifdef HEADER_MODE
309 dev->priv_flags |= IFF_NO_IP_ALIGN;
310 dev->features |= NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_TX;
311 #else
312 dev->features |= NETIF_F_HW_VLAN_CTAG_RX;
313 #endif
314
315 return 0;
316 }
317
318 static int
319 mvswitch_read_status(struct phy_device *pdev)
320 {
321 pdev->speed = SPEED_100;
322 pdev->duplex = DUPLEX_FULL;
323 pdev->link = 1;
324
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 */
328
329 /* wait for the ATU to become available */
330 mvswitch_wait_mask(pdev, MV_SWITCHREG(ATU_OP), MV_ATUOP_INPROGRESS, 0);
331
332 /* flush the ATU */
333 w16(pdev, MV_SWITCHREG(ATU_OP),
334 MV_ATUOP_INPROGRESS |
335 MV_ATUOP_FLUSH_ALL
336 );
337
338 /* wait for operation to complete */
339 mvswitch_wait_mask(pdev, MV_SWITCHREG(ATU_OP), MV_ATUOP_INPROGRESS, 0);
340
341 return 0;
342 }
343
344 static int
345 mvswitch_config_aneg(struct phy_device *phydev)
346 {
347 return 0;
348 }
349
350 static void
351 mvswitch_detach(struct phy_device *pdev)
352 {
353 struct mvswitch_priv *priv = to_mvsw(pdev);
354 struct net_device *dev = pdev->attached_dev;
355
356 if (!dev)
357 return;
358
359 dev->phy_ptr = NULL;
360 dev->eth_mangle_rx = NULL;
361 dev->eth_mangle_tx = NULL;
362 dev->features = priv->orig_features;
363 dev->priv_flags &= ~IFF_NO_IP_ALIGN;
364 }
365
366 static void
367 mvswitch_remove(struct phy_device *pdev)
368 {
369 struct mvswitch_priv *priv = to_mvsw(pdev);
370
371 kfree(priv);
372 }
373
374 static int
375 mvswitch_probe(struct phy_device *pdev)
376 {
377 struct mvswitch_priv *priv;
378
379 priv = kzalloc(sizeof(struct mvswitch_priv), GFP_KERNEL);
380 if (priv == NULL)
381 return -ENOMEM;
382
383 pdev->priv = priv;
384
385 return 0;
386 }
387
388 static int
389 mvswitch_fixup(struct phy_device *dev)
390 {
391 u16 reg;
392
393 if (dev->addr != 0x10)
394 return 0;
395
396 reg = dev->bus->read(dev->bus, MV_PORTREG(IDENT, 0)) & MV_IDENT_MASK;
397 if (reg != MV_IDENT_VALUE)
398 return 0;
399
400 dev->phy_id = MVSWITCH_MAGIC;
401 return 0;
402 }
403
404
405 static struct phy_driver mvswitch_driver = {
406 .name = "Marvell 88E6060",
407 .phy_id = MVSWITCH_MAGIC,
408 .phy_id_mask = 0xffffffff,
409 .features = PHY_BASIC_FEATURES,
410 .probe = &mvswitch_probe,
411 .remove = &mvswitch_remove,
412 .detach = &mvswitch_detach,
413 .config_init = &mvswitch_config_init,
414 .config_aneg = &mvswitch_config_aneg,
415 .read_status = &mvswitch_read_status,
416 .driver = { .owner = THIS_MODULE,},
417 };
418
419 static int __init
420 mvswitch_init(void)
421 {
422 phy_register_fixup_for_id(PHY_ANY_ID, mvswitch_fixup);
423 return phy_driver_register(&mvswitch_driver);
424 }
425
426 static void __exit
427 mvswitch_exit(void)
428 {
429 phy_driver_unregister(&mvswitch_driver);
430 }
431
432 module_init(mvswitch_init);
433 module_exit(mvswitch_exit);