Add support for the ultra-crappy Marvell 88E6060, which is used in Fonera+ and the...
[openwrt.git] / target / linux / generic-2.6 / files / drivers / net / phy / mvswitch.c
1 /*
2 * Marvell 88E6060 switch driver
3 * Copyright (c) 2008 Felix Fietkau <nbd@openwrt.org>
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 MODULE_DESCRIPTION("Marvell 88E6060 Switch driver");
34 MODULE_AUTHOR("Felix Fietkau");
35 MODULE_LICENSE("GPL");
36
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;
41 u8 vlans[16];
42 };
43
44 #define to_mvsw(_phy) ((struct mvswitch_priv *) (_phy)->priv)
45
46 static inline u16
47 r16(struct phy_device *phydev, int addr, int reg)
48 {
49 return phydev->bus->read(phydev->bus, addr, reg);
50 }
51
52 static inline void
53 w16(struct phy_device *phydev, int addr, int reg, u16 val)
54 {
55 phydev->bus->write(phydev->bus, addr, reg, val);
56 }
57
58 static int
59 mvswitch_mangle_tx(struct sk_buff *skb, struct net_device *dev)
60 {
61 struct mvswitch_priv *priv;
62 struct vlan_ethhdr *eh;
63 char *buf = NULL;
64 u16 vid;
65
66 priv = dev->phy_ptr;
67 if (unlikely(!priv))
68 goto error;
69
70 if (unlikely(skb->len < 16))
71 goto error;
72
73 eh = (struct vlan_ethhdr *) skb->data;
74 if (be16_to_cpu(eh->h_vlan_proto) != 0x8100)
75 goto error;
76
77 vid = be16_to_cpu(eh->h_vlan_TCI) & VLAN_VID_MASK;
78 if (unlikely((vid > 15 || !priv->vlans[vid])))
79 goto error;
80
81 if (skb->len <= 64) {
82 if (pskb_expand_head(skb, 0, 68 - skb->len, GFP_ATOMIC)) {
83 if (net_ratelimit())
84 printk("%s: failed to expand/update skb for the switch\n", dev->name);
85 goto error;
86 }
87
88 buf = skb->data + 64;
89 skb->len = 68;
90 } else {
91 if (skb_cloned(skb) || unlikely(skb_tailroom(skb) < 4)) {
92 if (pskb_expand_head(skb, 0, 4, GFP_ATOMIC)) {
93 if (net_ratelimit())
94 printk("%s: failed to expand/update skb for the switch\n", dev->name);
95 goto error;
96 }
97 }
98 buf = skb_put(skb, 4);
99 }
100
101 /* move the ethernet header 4 bytes forward, overwriting the vlan tag */
102 memmove(skb->data + 4, skb->data, 12);
103 skb->data += 4;
104 skb->len -= 4;
105 skb->mac_header += 4;
106
107 if (!buf)
108 goto error;
109
110 /* append the tag */
111 *((u32 *) buf) = (
112 (0x80 << 24) |
113 ((priv->vlans[vid] & 0x1f) << 16)
114 );
115
116 return priv->hardstart(skb, dev);
117
118 error:
119 /* any errors? drop the packet! */
120 dev_kfree_skb_any(skb);
121 return 0;
122 }
123
124 static int
125 mvswitch_mangle_rx(struct sk_buff *skb, int napi)
126 {
127 struct mvswitch_priv *priv;
128 struct net_device *dev;
129 int vlan = -1;
130 unsigned char *buf;
131 int i;
132
133 dev = skb->dev;
134 if (!dev)
135 goto error;
136
137 priv = dev->phy_ptr;
138 if (!priv)
139 goto error;
140
141 if (!priv->grp)
142 goto error;
143
144 buf = skb->data + skb->len - 4;
145 if (buf[0] != 0x80)
146 goto error;
147
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])
151 vlan = i;
152 }
153
154 if (vlan == -1)
155 goto error;
156
157 if (napi)
158 return vlan_hwaccel_receive_skb(skb, priv->grp, vlan);
159 else
160 return vlan_hwaccel_rx(skb, priv->grp, vlan);
161
162 error:
163 /* no vlan? eat the packet! */
164 dev_kfree_skb_any(skb);
165 return 0;
166 }
167
168
169 static int
170 mvswitch_netif_rx(struct sk_buff *skb)
171 {
172 return mvswitch_mangle_rx(skb, 0);
173 }
174
175 static int
176 mvswitch_netif_receive_skb(struct sk_buff *skb)
177 {
178 return mvswitch_mangle_rx(skb, 1);
179 }
180
181
182 static void
183 mvswitch_vlan_rx_register(struct net_device *dev, struct vlan_group *grp)
184 {
185 struct mvswitch_priv *priv = dev->phy_ptr;
186 priv->grp = grp;
187 }
188
189
190 static int
191 mvswitch_config_init(struct phy_device *pdev)
192 {
193 struct mvswitch_priv *priv = to_mvsw(pdev);
194 struct net_device *dev = pdev->attached_dev;
195 u8 vlmap = 0;
196 int i;
197
198 if (!dev)
199 return -EINVAL;
200
201 printk("%s: Marvell 88E6060 PHY driver attached.\n", dev->name);
202 pdev->supported = ADVERTISED_100baseT_Full;
203 pdev->advertising = ADVERTISED_100baseT_Full;
204 dev->phy_ptr = priv;
205
206 /* initialize default vlans */
207 for (i = 0; i < MV_PORTS; i++)
208 priv->vlans[(i == MV_WANPORT ? 1 : 0)] |= (1 << i);
209
210 /* before entering reset, disable all ports */
211 for (i = 0; i < MV_PORTS; i++)
212 w16(pdev, MV_PORTREG(CONTROL, i), 0x00);
213
214 msleep(2); /* wait for the status change to settle in */
215
216 /* put the device in reset and set ATU flags */
217 w16(pdev, MV_SWITCHREG(ATU_CTRL),
218 MV_ATUCTL_RESET |
219 MV_ATUCTL_ATU_1K |
220 MV_ATUCTL_AGETIME(4080) /* maximum */
221 );
222
223 i = 100; /* timeout */
224 do {
225 if (!(r16(pdev, MV_SWITCHREG(ATU_CTRL)) & MV_ATUCTL_RESET))
226 break;
227 msleep(1);
228 } while (--i > 0);
229
230 if (!i) {
231 printk("%s: Timeout waiting for the switch to reset.\n", dev->name);
232 return -ETIMEDOUT;
233 }
234
235 /* initialize the cpu port */
236 w16(pdev, MV_PORTREG(CONTROL, MV_CPUPORT),
237 MV_PORTCTRL_ENABLED |
238 MV_PORTCTRL_VLANTUN |
239 MV_PORTCTRL_RXTR |
240 MV_PORTCTRL_TXTR
241 );
242 /* wait for the phy change to settle in */
243 msleep(2);
244 for (i = 0; i < MV_PORTS; i++) {
245 u8 pvid = 0;
246 int j;
247
248 vlmap = 0;
249
250 /* look for the matching vlan */
251 for (j = 0; j < ARRAY_SIZE(priv->vlans); j++) {
252 if (priv->vlans[j] & (1 << i)) {
253 vlmap = priv->vlans[j];
254 pvid = j;
255 }
256 }
257 /* leave port unconfigured if it's not part of a vlan */
258 if (!vlmap)
259 break;
260
261 /* add the cpu port to the allowed destinations list */
262 vlmap |= (1 << MV_CPUPORT);
263
264 /* take port out of its own vlan destination map */
265 vlmap &= ~(1 << i);
266
267 /* apply vlan settings */
268 w16(pdev, MV_PORTREG(VLANMAP, i),
269 MV_PORTVLAN_PORTS(vlmap) |
270 MV_PORTVLAN_ID(pvid)
271 );
272
273 /* re-enable port */
274 w16(pdev, MV_PORTREG(CONTROL, i), MV_PORTCTRL_ENABLED);
275 }
276
277 /* build the target list for the cpu port */
278 for (i = 0, vlmap = 0; i < ARRAY_SIZE(priv->vlans); i++)
279 vlmap |= priv->vlans[i];
280
281 w16(pdev, MV_PORTREG(VLANMAP, MV_CPUPORT),
282 MV_PORTVLAN_PORTS(vlmap)
283 );
284
285 /* set the port association vector */
286 for (i = 0; i <= MV_PORTS; i++) {
287 w16(pdev, MV_PORTREG(ASSOC, i),
288 MV_PORTASSOC_PORTS(1 << i)
289 );
290 }
291
292 /* hook into the tx function */
293 priv->hardstart = dev->hard_start_xmit;
294 pdev->netif_receive_skb = mvswitch_netif_receive_skb;
295 pdev->netif_rx = mvswitch_netif_rx;
296 dev->hard_start_xmit = mvswitch_mangle_tx;
297 dev->vlan_rx_register = mvswitch_vlan_rx_register;
298 dev->features |= NETIF_F_HW_VLAN_RX;
299
300 return 0;
301 }
302
303 static int
304 mvswitch_read_status(struct phy_device *phydev)
305 {
306 phydev->speed = SPEED_100;
307 phydev->duplex = DUPLEX_FULL;
308 phydev->state = PHY_UP;
309 return 0;
310 }
311
312 static int
313 mvswitch_config_aneg(struct phy_device *phydev)
314 {
315 return 0;
316 }
317
318 static void
319 mvswitch_remove(struct phy_device *pdev)
320 {
321 struct mvswitch_priv *priv = to_mvsw(pdev);
322 struct net_device *dev = pdev->attached_dev;
323
324 /* restore old xmit handler */
325 if (priv->hardstart && dev)
326 dev->hard_start_xmit = priv->hardstart;
327 dev->vlan_rx_register = NULL;
328 dev->vlan_rx_kill_vid = NULL;
329 dev->phy_ptr = NULL;
330 dev->features &= ~NETIF_F_HW_VLAN_RX;
331 kfree(priv);
332 }
333
334 static bool
335 mvswitch_detect(struct mii_bus *bus, int addr)
336 {
337 u16 reg;
338 int i;
339
340 /* we attach to phy id 31 to make sure that the late probe works */
341 if (addr != 31)
342 return false;
343
344 /* look for the switch on the bus */
345 reg = bus->read(bus, MV_PORTREG(IDENT, 0)) & MV_IDENT_MASK;
346 if (reg != MV_IDENT_VALUE)
347 return false;
348
349 /*
350 * Now that we've established that the switch actually exists, let's
351 * get rid of the competition :)
352 */
353 for (i = 0; i < 31; i++) {
354 if (!bus->phy_map[i])
355 continue;
356
357 device_unregister(&bus->phy_map[i]->dev);
358 kfree(bus->phy_map[i]);
359 bus->phy_map[i] = NULL;
360 }
361
362 return true;
363 }
364
365 static int
366 mvswitch_probe(struct phy_device *pdev)
367 {
368 struct mvswitch_priv *priv;
369
370 priv = kzalloc(sizeof(struct mvswitch_priv), GFP_KERNEL);
371 if (priv == NULL)
372 return -ENOMEM;
373
374 pdev->priv = priv;
375
376 return 0;
377 }
378
379
380 static struct phy_driver mvswitch_driver = {
381 .name = "Marvell 88E6060",
382 .features = PHY_BASIC_FEATURES,
383 .detect = &mvswitch_detect,
384 .probe = &mvswitch_probe,
385 .remove = &mvswitch_remove,
386 .config_init = &mvswitch_config_init,
387 .config_aneg = &mvswitch_config_aneg,
388 .read_status = &mvswitch_read_status,
389 .driver = { .owner = THIS_MODULE,},
390 };
391
392 static int __init
393 mvswitch_init(void)
394 {
395 return phy_driver_register(&mvswitch_driver);
396 }
397
398 static void __exit
399 mvswitch_exit(void)
400 {
401 phy_driver_unregister(&mvswitch_driver);
402 }
403
404 module_init(mvswitch_init);
405 module_exit(mvswitch_exit);
This page took 0.066973 seconds and 5 git commands to generate.