generic: ar8216: add vtu_load_vlan field to ar8xxx_chip
[openwrt.git] / target / linux / generic / files / drivers / net / phy / ar8216.c
1 /*
2  * ar8216.c: AR8216 switch driver
3  *
4  * Copyright (C) 2009 Felix Fietkau <nbd@openwrt.org>
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  */
16
17 #include <linux/if.h>
18 #include <linux/module.h>
19 #include <linux/init.h>
20 #include <linux/list.h>
21 #include <linux/if_ether.h>
22 #include <linux/skbuff.h>
23 #include <linux/netdevice.h>
24 #include <linux/netlink.h>
25 #include <linux/bitops.h>
26 #include <net/genetlink.h>
27 #include <linux/switch.h>
28 #include <linux/delay.h>
29 #include <linux/phy.h>
30 #include <linux/netdevice.h>
31 #include <linux/etherdevice.h>
32 #include <linux/lockdep.h>
33 #include "ar8216.h"
34
35 /* size of the vlan table */
36 #define AR8X16_MAX_VLANS        128
37 #define AR8X16_PROBE_RETRIES    10
38
39 struct ar8216_priv;
40
41 struct ar8xxx_chip {
42         int (*hw_init)(struct ar8216_priv *priv);
43         void (*init_port)(struct ar8216_priv *priv, int port);
44         void (*setup_port)(struct ar8216_priv *priv, int port, u32 egress,
45                            u32 ingress, u32 members, u32 pvid);
46         int (*atu_flush)(struct ar8216_priv *priv);
47         void (*vtu_flush)(struct ar8216_priv *priv);
48         void (*vtu_load_vlan)(struct ar8216_priv *priv, u32 vid, u32 port_mask);
49 };
50
51 struct ar8216_priv {
52         struct switch_dev dev;
53         struct phy_device *phy;
54         u32 (*read)(struct ar8216_priv *priv, int reg);
55         void (*write)(struct ar8216_priv *priv, int reg, u32 val);
56         const struct net_device_ops *ndo_old;
57         struct net_device_ops ndo;
58         struct mutex reg_mutex;
59         int chip_type;
60         const struct ar8xxx_chip *chip;
61         bool initialized;
62         bool port4_phy;
63         char buf[80];
64
65         bool init;
66
67         /* all fields below are cleared on reset */
68         bool vlan;
69         u16 vlan_id[AR8X16_MAX_VLANS];
70         u8 vlan_table[AR8X16_MAX_VLANS];
71         u8 vlan_tagged;
72         u16 pvid[AR8216_NUM_PORTS];
73 };
74
75 #define to_ar8216(_dev) container_of(_dev, struct ar8216_priv, dev)
76
77 static inline void
78 split_addr(u32 regaddr, u16 *r1, u16 *r2, u16 *page)
79 {
80         regaddr >>= 1;
81         *r1 = regaddr & 0x1e;
82
83         regaddr >>= 5;
84         *r2 = regaddr & 0x7;
85
86         regaddr >>= 3;
87         *page = regaddr & 0x1ff;
88 }
89
90 static u32
91 ar8216_mii_read(struct ar8216_priv *priv, int reg)
92 {
93         struct phy_device *phy = priv->phy;
94         struct mii_bus *bus = phy->bus;
95         u16 r1, r2, page;
96         u16 lo, hi;
97
98         split_addr((u32) reg, &r1, &r2, &page);
99
100         mutex_lock(&bus->mdio_lock);
101
102         bus->write(bus, 0x18, 0, page);
103         usleep_range(1000, 2000); /* wait for the page switch to propagate */
104         lo = bus->read(bus, 0x10 | r2, r1);
105         hi = bus->read(bus, 0x10 | r2, r1 + 1);
106
107         mutex_unlock(&bus->mdio_lock);
108
109         return (hi << 16) | lo;
110 }
111
112 static void
113 ar8216_mii_write(struct ar8216_priv *priv, int reg, u32 val)
114 {
115         struct phy_device *phy = priv->phy;
116         struct mii_bus *bus = phy->bus;
117         u16 r1, r2, r3;
118         u16 lo, hi;
119
120         split_addr((u32) reg, &r1, &r2, &r3);
121         lo = val & 0xffff;
122         hi = (u16) (val >> 16);
123
124         mutex_lock(&bus->mdio_lock);
125
126         bus->write(bus, 0x18, 0, r3);
127         usleep_range(1000, 2000); /* wait for the page switch to propagate */
128         bus->write(bus, 0x10 | r2, r1 + 1, hi);
129         bus->write(bus, 0x10 | r2, r1, lo);
130
131         mutex_unlock(&bus->mdio_lock);
132 }
133
134 static void
135 ar8216_phy_dbg_write(struct ar8216_priv *priv, int phy_addr,
136                      u16 dbg_addr, u16 dbg_data)
137 {
138         struct mii_bus *bus = priv->phy->bus;
139
140         mutex_lock(&bus->mdio_lock);
141         bus->write(bus, phy_addr, MII_ATH_DBG_ADDR, dbg_addr);
142         bus->write(bus, phy_addr, MII_ATH_DBG_DATA, dbg_data);
143         mutex_unlock(&bus->mdio_lock);
144 }
145
146 static u32
147 ar8216_rmw(struct ar8216_priv *priv, int reg, u32 mask, u32 val)
148 {
149         u32 v;
150
151         lockdep_assert_held(&priv->reg_mutex);
152
153         v = priv->read(priv, reg);
154         v &= ~mask;
155         v |= val;
156         priv->write(priv, reg, v);
157
158         return v;
159 }
160
161 static void
162 ar8216_read_port_link(struct ar8216_priv *priv, int port,
163                       struct switch_port_link *link)
164 {
165         u32 status;
166         u32 speed;
167
168         memset(link, '\0', sizeof(*link));
169
170         status = priv->read(priv, AR8216_REG_PORT_STATUS(port));
171
172         link->aneg = !!(status & AR8216_PORT_STATUS_LINK_AUTO);
173         if (link->aneg) {
174                 link->link = !!(status & AR8216_PORT_STATUS_LINK_UP);
175                 if (!link->link)
176                         return;
177         } else {
178                 link->link = true;
179         }
180
181         link->duplex = !!(status & AR8216_PORT_STATUS_DUPLEX);
182         link->tx_flow = !!(status & AR8216_PORT_STATUS_TXFLOW);
183         link->rx_flow = !!(status & AR8216_PORT_STATUS_RXFLOW);
184
185         speed = (status & AR8216_PORT_STATUS_SPEED) >>
186                  AR8216_PORT_STATUS_SPEED_S;
187
188         switch (speed) {
189         case AR8216_PORT_SPEED_10M:
190                 link->speed = SWITCH_PORT_SPEED_10;
191                 break;
192         case AR8216_PORT_SPEED_100M:
193                 link->speed = SWITCH_PORT_SPEED_100;
194                 break;
195         case AR8216_PORT_SPEED_1000M:
196                 link->speed = SWITCH_PORT_SPEED_1000;
197                 break;
198         default:
199                 link->speed = SWITCH_PORT_SPEED_UNKNOWN;
200                 break;
201         }
202 }
203
204 static int
205 ar8216_set_vlan(struct switch_dev *dev, const struct switch_attr *attr,
206                 struct switch_val *val)
207 {
208         struct ar8216_priv *priv = to_ar8216(dev);
209         priv->vlan = !!val->value.i;
210         return 0;
211 }
212
213 static int
214 ar8216_get_vlan(struct switch_dev *dev, const struct switch_attr *attr,
215                 struct switch_val *val)
216 {
217         struct ar8216_priv *priv = to_ar8216(dev);
218         val->value.i = priv->vlan;
219         return 0;
220 }
221
222
223 static int
224 ar8216_set_pvid(struct switch_dev *dev, int port, int vlan)
225 {
226         struct ar8216_priv *priv = to_ar8216(dev);
227
228         /* make sure no invalid PVIDs get set */
229
230         if (vlan >= dev->vlans)
231                 return -EINVAL;
232
233         priv->pvid[port] = vlan;
234         return 0;
235 }
236
237 static int
238 ar8216_get_pvid(struct switch_dev *dev, int port, int *vlan)
239 {
240         struct ar8216_priv *priv = to_ar8216(dev);
241         *vlan = priv->pvid[port];
242         return 0;
243 }
244
245 static int
246 ar8216_set_vid(struct switch_dev *dev, const struct switch_attr *attr,
247                struct switch_val *val)
248 {
249         struct ar8216_priv *priv = to_ar8216(dev);
250         priv->vlan_id[val->port_vlan] = val->value.i;
251         return 0;
252 }
253
254 static int
255 ar8216_get_vid(struct switch_dev *dev, const struct switch_attr *attr,
256                struct switch_val *val)
257 {
258         struct ar8216_priv *priv = to_ar8216(dev);
259         val->value.i = priv->vlan_id[val->port_vlan];
260         return 0;
261 }
262
263 static int
264 ar8216_get_port_link(struct switch_dev *dev, int port,
265                      struct switch_port_link *link)
266 {
267         struct ar8216_priv *priv = to_ar8216(dev);
268
269         ar8216_read_port_link(priv, port, link);
270         return 0;
271 }
272
273 static int
274 ar8216_mangle_tx(struct sk_buff *skb, struct net_device *dev)
275 {
276         struct ar8216_priv *priv = dev->phy_ptr;
277         unsigned char *buf;
278
279         if (unlikely(!priv))
280                 goto error;
281
282         if (!priv->vlan)
283                 goto send;
284
285         if (unlikely(skb_headroom(skb) < 2)) {
286                 if (pskb_expand_head(skb, 2, 0, GFP_ATOMIC) < 0)
287                         goto error;
288         }
289
290         buf = skb_push(skb, 2);
291         buf[0] = 0x10;
292         buf[1] = 0x80;
293
294 send:
295         return priv->ndo_old->ndo_start_xmit(skb, dev);
296
297 error:
298         dev_kfree_skb_any(skb);
299         return 0;
300 }
301
302 static int
303 ar8216_mangle_rx(struct sk_buff *skb, int napi)
304 {
305         struct ar8216_priv *priv;
306         struct net_device *dev;
307         unsigned char *buf;
308         int port, vlan;
309
310         dev = skb->dev;
311         if (!dev)
312                 goto error;
313
314         priv = dev->phy_ptr;
315         if (!priv)
316                 goto error;
317
318         /* don't strip the header if vlan mode is disabled */
319         if (!priv->vlan)
320                 goto recv;
321
322         /* strip header, get vlan id */
323         buf = skb->data;
324         skb_pull(skb, 2);
325
326         /* check for vlan header presence */
327         if ((buf[12 + 2] != 0x81) || (buf[13 + 2] != 0x00))
328                 goto recv;
329
330         port = buf[0] & 0xf;
331
332         /* no need to fix up packets coming from a tagged source */
333         if (priv->vlan_tagged & (1 << port))
334                 goto recv;
335
336         /* lookup port vid from local table, the switch passes an invalid vlan id */
337         vlan = priv->vlan_id[priv->pvid[port]];
338
339         buf[14 + 2] &= 0xf0;
340         buf[14 + 2] |= vlan >> 8;
341         buf[15 + 2] = vlan & 0xff;
342
343 recv:
344         skb->protocol = eth_type_trans(skb, skb->dev);
345
346         if (napi)
347                 return netif_receive_skb(skb);
348         else
349                 return netif_rx(skb);
350
351 error:
352         /* no vlan? eat the packet! */
353         dev_kfree_skb_any(skb);
354         return NET_RX_DROP;
355 }
356
357 static int
358 ar8216_netif_rx(struct sk_buff *skb)
359 {
360         return ar8216_mangle_rx(skb, 0);
361 }
362
363 static int
364 ar8216_netif_receive_skb(struct sk_buff *skb)
365 {
366         return ar8216_mangle_rx(skb, 1);
367 }
368
369
370 static struct switch_attr ar8216_globals[] = {
371         {
372                 .type = SWITCH_TYPE_INT,
373                 .name = "enable_vlan",
374                 .description = "Enable VLAN mode",
375                 .set = ar8216_set_vlan,
376                 .get = ar8216_get_vlan,
377                 .max = 1
378         },
379 };
380
381 static struct switch_attr ar8216_port[] = {
382 };
383
384 static struct switch_attr ar8216_vlan[] = {
385         {
386                 .type = SWITCH_TYPE_INT,
387                 .name = "vid",
388                 .description = "VLAN ID (0-4094)",
389                 .set = ar8216_set_vid,
390                 .get = ar8216_get_vid,
391                 .max = 4094,
392         },
393 };
394
395
396 static int
397 ar8216_get_ports(struct switch_dev *dev, struct switch_val *val)
398 {
399         struct ar8216_priv *priv = to_ar8216(dev);
400         u8 ports = priv->vlan_table[val->port_vlan];
401         int i;
402
403         val->len = 0;
404         for (i = 0; i < AR8216_NUM_PORTS; i++) {
405                 struct switch_port *p;
406
407                 if (!(ports & (1 << i)))
408                         continue;
409
410                 p = &val->value.ports[val->len++];
411                 p->id = i;
412                 if (priv->vlan_tagged & (1 << i))
413                         p->flags = (1 << SWITCH_PORT_FLAG_TAGGED);
414                 else
415                         p->flags = 0;
416         }
417         return 0;
418 }
419
420 static int
421 ar8216_set_ports(struct switch_dev *dev, struct switch_val *val)
422 {
423         struct ar8216_priv *priv = to_ar8216(dev);
424         u8 *vt = &priv->vlan_table[val->port_vlan];
425         int i, j;
426
427         *vt = 0;
428         for (i = 0; i < val->len; i++) {
429                 struct switch_port *p = &val->value.ports[i];
430
431                 if (p->flags & (1 << SWITCH_PORT_FLAG_TAGGED)) {
432                         priv->vlan_tagged |= (1 << p->id);
433                 } else {
434                         priv->vlan_tagged &= ~(1 << p->id);
435                         priv->pvid[p->id] = val->port_vlan;
436
437                         /* make sure that an untagged port does not
438                          * appear in other vlans */
439                         for (j = 0; j < AR8X16_MAX_VLANS; j++) {
440                                 if (j == val->port_vlan)
441                                         continue;
442                                 priv->vlan_table[j] &= ~(1 << p->id);
443                         }
444                 }
445
446                 *vt |= 1 << p->id;
447         }
448         return 0;
449 }
450
451 static int
452 ar8216_wait_bit(struct ar8216_priv *priv, int reg, u32 mask, u32 val)
453 {
454         int timeout = 20;
455         u32 t = 0;
456
457         while (1) {
458                 t = priv->read(priv, reg);
459                 if ((t & mask) == val)
460                         return 0;
461
462                 if (timeout-- <= 0)
463                         break;
464
465                 udelay(10);
466         }
467
468         pr_err("ar8216: timeout on reg %08x: %08x & %08x != %08x\n",
469                (unsigned int) reg, t, mask, val);
470         return -ETIMEDOUT;
471 }
472
473 static void
474 ar8216_vtu_op(struct ar8216_priv *priv, u32 op, u32 val)
475 {
476         if (ar8216_wait_bit(priv, AR8216_REG_VTU, AR8216_VTU_ACTIVE, 0))
477                 return;
478         if ((op & AR8216_VTU_OP) == AR8216_VTU_OP_LOAD) {
479                 val &= AR8216_VTUDATA_MEMBER;
480                 val |= AR8216_VTUDATA_VALID;
481                 priv->write(priv, AR8216_REG_VTU_DATA, val);
482         }
483         op |= AR8216_VTU_ACTIVE;
484         priv->write(priv, AR8216_REG_VTU, op);
485 }
486
487 static void
488 ar8216_vtu_flush(struct ar8216_priv *priv)
489 {
490         ar8216_vtu_op(priv, AR8216_VTU_OP_FLUSH, 0);
491 }
492
493 static void
494 ar8216_vtu_load_vlan(struct ar8216_priv *priv, u32 vid, u32 port_mask)
495 {
496         u32 op;
497
498         op = AR8216_VTU_OP_LOAD | (vid << AR8216_VTU_VID_S);
499         ar8216_vtu_op(priv, op, port_mask);
500 }
501
502 static int
503 ar8216_atu_flush(struct ar8216_priv *priv)
504 {
505         int ret;
506
507         ret = ar8216_wait_bit(priv, AR8216_REG_ATU, AR8216_ATU_ACTIVE, 0);
508         if (!ret)
509                 priv->write(priv, AR8216_REG_ATU, AR8216_ATU_OP_FLUSH);
510
511         return ret;
512 }
513
514 static void
515 ar8216_setup_port(struct ar8216_priv *priv, int port, u32 egress, u32 ingress,
516                   u32 members, u32 pvid)
517 {
518         u32 header;
519
520         if (priv->vlan && port == AR8216_PORT_CPU && priv->chip_type == AR8216)
521                 header = AR8216_PORT_CTRL_HEADER;
522         else
523                 header = 0;
524
525         ar8216_rmw(priv, AR8216_REG_PORT_CTRL(port),
526                    AR8216_PORT_CTRL_LEARN | AR8216_PORT_CTRL_VLAN_MODE |
527                    AR8216_PORT_CTRL_SINGLE_VLAN | AR8216_PORT_CTRL_STATE |
528                    AR8216_PORT_CTRL_HEADER | AR8216_PORT_CTRL_LEARN_LOCK,
529                    AR8216_PORT_CTRL_LEARN | header |
530                    (egress << AR8216_PORT_CTRL_VLAN_MODE_S) |
531                    (AR8216_PORT_STATE_FORWARD << AR8216_PORT_CTRL_STATE_S));
532
533         ar8216_rmw(priv, AR8216_REG_PORT_VLAN(port),
534                    AR8216_PORT_VLAN_DEST_PORTS | AR8216_PORT_VLAN_MODE |
535                    AR8216_PORT_VLAN_DEFAULT_ID,
536                    (members << AR8216_PORT_VLAN_DEST_PORTS_S) |
537                    (ingress << AR8216_PORT_VLAN_MODE_S) |
538                    (pvid << AR8216_PORT_VLAN_DEFAULT_ID_S));
539 }
540
541 static void
542 ar8236_setup_port(struct ar8216_priv *priv, int port, u32 egress, u32 ingress,
543                   u32 members, u32 pvid)
544 {
545         ar8216_rmw(priv, AR8216_REG_PORT_CTRL(port),
546                    AR8216_PORT_CTRL_LEARN | AR8216_PORT_CTRL_VLAN_MODE |
547                    AR8216_PORT_CTRL_SINGLE_VLAN | AR8216_PORT_CTRL_STATE |
548                    AR8216_PORT_CTRL_HEADER | AR8216_PORT_CTRL_LEARN_LOCK,
549                    AR8216_PORT_CTRL_LEARN |
550                    (egress << AR8216_PORT_CTRL_VLAN_MODE_S) |
551                    (AR8216_PORT_STATE_FORWARD << AR8216_PORT_CTRL_STATE_S));
552
553         ar8216_rmw(priv, AR8236_REG_PORT_VLAN(port),
554                    AR8236_PORT_VLAN_DEFAULT_ID,
555                    (pvid << AR8236_PORT_VLAN_DEFAULT_ID_S));
556
557         ar8216_rmw(priv, AR8236_REG_PORT_VLAN2(port),
558                    AR8236_PORT_VLAN2_VLAN_MODE |
559                    AR8236_PORT_VLAN2_MEMBER,
560                    (ingress << AR8236_PORT_VLAN2_VLAN_MODE_S) |
561                    (members << AR8236_PORT_VLAN2_MEMBER_S));
562 }
563
564 static int
565 ar8216_hw_apply(struct switch_dev *dev)
566 {
567         struct ar8216_priv *priv = to_ar8216(dev);
568         u8 portmask[AR8216_NUM_PORTS];
569         int i, j;
570
571         mutex_lock(&priv->reg_mutex);
572         /* flush all vlan translation unit entries */
573         priv->chip->vtu_flush(priv);
574
575         memset(portmask, 0, sizeof(portmask));
576         if (!priv->init) {
577                 /* calculate the port destination masks and load vlans
578                  * into the vlan translation unit */
579                 for (j = 0; j < AR8X16_MAX_VLANS; j++) {
580                         u8 vp = priv->vlan_table[j];
581
582                         if (!vp)
583                                 continue;
584
585                         for (i = 0; i < AR8216_NUM_PORTS; i++) {
586                                 u8 mask = (1 << i);
587                                 if (vp & mask)
588                                         portmask[i] |= vp & ~mask;
589                         }
590
591                         priv->chip->vtu_load_vlan(priv, priv->vlan_id[j],
592                                                  priv->vlan_table[j]);
593                 }
594         } else {
595                 /* vlan disabled:
596                  * isolate all ports, but connect them to the cpu port */
597                 for (i = 0; i < AR8216_NUM_PORTS; i++) {
598                         if (i == AR8216_PORT_CPU)
599                                 continue;
600
601                         portmask[i] = 1 << AR8216_PORT_CPU;
602                         portmask[AR8216_PORT_CPU] |= (1 << i);
603                 }
604         }
605
606         /* update the port destination mask registers and tag settings */
607         for (i = 0; i < AR8216_NUM_PORTS; i++) {
608                 int egress, ingress;
609                 int pvid;
610
611                 if (priv->vlan) {
612                         pvid = priv->vlan_id[priv->pvid[i]];
613                         if (priv->vlan_tagged & (1 << i))
614                                 egress = AR8216_OUT_ADD_VLAN;
615                         else
616                                 egress = AR8216_OUT_STRIP_VLAN;
617                         ingress = AR8216_IN_SECURE;
618                 } else {
619                         pvid = i;
620                         egress = AR8216_OUT_KEEP;
621                         ingress = AR8216_IN_PORT_ONLY;
622                 }
623
624                 priv->chip->setup_port(priv, i, egress, ingress, portmask[i],
625                                        pvid);
626         }
627         mutex_unlock(&priv->reg_mutex);
628         return 0;
629 }
630
631 static int
632 ar8216_hw_init(struct ar8216_priv *priv)
633 {
634         return 0;
635 }
636
637 static int
638 ar8236_hw_init(struct ar8216_priv *priv)
639 {
640         int i;
641         struct mii_bus *bus;
642
643         if (priv->initialized)
644                 return 0;
645
646         /* Initialize the PHYs */
647         bus = priv->phy->bus;
648         for (i = 0; i < 5; i++) {
649                 mdiobus_write(bus, i, MII_ADVERTISE,
650                               ADVERTISE_ALL | ADVERTISE_PAUSE_CAP |
651                               ADVERTISE_PAUSE_ASYM);
652                 mdiobus_write(bus, i, MII_BMCR, BMCR_RESET | BMCR_ANENABLE);
653         }
654         msleep(1000);
655
656         priv->initialized = true;
657         return 0;
658 }
659
660 static int
661 ar8316_hw_init(struct ar8216_priv *priv)
662 {
663         int i;
664         u32 val, newval;
665         struct mii_bus *bus;
666
667         val = priv->read(priv, 0x8);
668
669         if (priv->phy->interface == PHY_INTERFACE_MODE_RGMII) {
670                 if (priv->port4_phy) {
671                         /* value taken from Ubiquiti RouterStation Pro */
672                         newval = 0x81461bea;
673                         printk(KERN_INFO "ar8316: Using port 4 as PHY\n");
674                 } else {
675                         newval = 0x01261be2;
676                         printk(KERN_INFO "ar8316: Using port 4 as switch port\n");
677                 }
678         } else if (priv->phy->interface == PHY_INTERFACE_MODE_GMII) {
679                 /* value taken from AVM Fritz!Box 7390 sources */
680                 newval = 0x010e5b71;
681         } else {
682                 /* no known value for phy interface */
683                 printk(KERN_ERR "ar8316: unsupported mii mode: %d.\n",
684                         priv->phy->interface);
685                 return -EINVAL;
686         }
687
688         if (val == newval)
689                 goto out;
690
691         priv->write(priv, 0x8, newval);
692
693         /* Initialize the ports */
694         bus = priv->phy->bus;
695         for (i = 0; i < 5; i++) {
696                 if ((i == 4) && priv->port4_phy &&
697                     priv->phy->interface == PHY_INTERFACE_MODE_RGMII) {
698                         /* work around for phy4 rgmii mode */
699                         ar8216_phy_dbg_write(priv, i, 0x12, 0x480c);
700                         /* rx delay */
701                         ar8216_phy_dbg_write(priv, i, 0x0, 0x824e);
702                         /* tx delay */
703                         ar8216_phy_dbg_write(priv, i, 0x5, 0x3d47);
704                         msleep(1000);
705                 }
706
707                 /* initialize the port itself */
708                 mdiobus_write(bus, i, MII_ADVERTISE,
709                         ADVERTISE_ALL | ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM);
710                 mdiobus_write(bus, i, MII_CTRL1000, ADVERTISE_1000FULL);
711                 mdiobus_write(bus, i, MII_BMCR, BMCR_RESET | BMCR_ANENABLE);
712                 msleep(1000);
713         }
714
715 out:
716         priv->initialized = true;
717         return 0;
718 }
719
720 static void
721 ar8216_init_globals(struct ar8216_priv *priv)
722 {
723         switch (priv->chip_type) {
724         case AR8216:
725                 /* standard atheros magic */
726                 priv->write(priv, 0x38, 0xc000050e);
727
728                 ar8216_rmw(priv, AR8216_REG_GLOBAL_CTRL,
729                            AR8216_GCTRL_MTU, 1518 + 8 + 2);
730                 break;
731         case AR8316:
732                 /* standard atheros magic */
733                 priv->write(priv, 0x38, 0xc000050e);
734
735                 /* enable cpu port to receive multicast and broadcast frames */
736                 priv->write(priv, AR8216_REG_FLOOD_MASK, 0x003f003f);
737
738                 /* fall through */
739         case AR8236:
740                 /* enable jumbo frames */
741                 ar8216_rmw(priv, AR8216_REG_GLOBAL_CTRL,
742                            AR8316_GCTRL_MTU, 9018 + 8 + 2);
743                 break;
744         }
745 }
746
747 static void
748 ar8216_init_port(struct ar8216_priv *priv, int port)
749 {
750         /* Enable port learning and tx */
751         priv->write(priv, AR8216_REG_PORT_CTRL(port),
752                 AR8216_PORT_CTRL_LEARN |
753                 (4 << AR8216_PORT_CTRL_STATE_S));
754
755         priv->write(priv, AR8216_REG_PORT_VLAN(port), 0);
756
757         if (port == AR8216_PORT_CPU) {
758                 priv->write(priv, AR8216_REG_PORT_STATUS(port),
759                         AR8216_PORT_STATUS_LINK_UP |
760                         ((priv->chip_type == AR8316) ?
761                                 AR8216_PORT_SPEED_1000M : AR8216_PORT_SPEED_100M) |
762                         AR8216_PORT_STATUS_TXMAC |
763                         AR8216_PORT_STATUS_RXMAC |
764                         ((priv->chip_type == AR8316) ? AR8216_PORT_STATUS_RXFLOW : 0) |
765                         ((priv->chip_type == AR8316) ? AR8216_PORT_STATUS_TXFLOW : 0) |
766                         AR8216_PORT_STATUS_DUPLEX);
767         } else {
768                 priv->write(priv, AR8216_REG_PORT_STATUS(port),
769                         AR8216_PORT_STATUS_LINK_AUTO);
770         }
771 }
772
773 static const struct ar8xxx_chip ar8216_chip = {
774         .hw_init = ar8216_hw_init,
775         .init_port = ar8216_init_port,
776         .setup_port = ar8216_setup_port,
777         .atu_flush = ar8216_atu_flush,
778         .vtu_flush = ar8216_vtu_flush,
779         .vtu_load_vlan = ar8216_vtu_load_vlan,
780 };
781
782 static const struct ar8xxx_chip ar8236_chip = {
783         .hw_init = ar8236_hw_init,
784         .init_port = ar8216_init_port,
785         .setup_port = ar8236_setup_port,
786         .atu_flush = ar8216_atu_flush,
787         .vtu_flush = ar8216_vtu_flush,
788         .vtu_load_vlan = ar8216_vtu_load_vlan,
789 };
790
791 static const struct ar8xxx_chip ar8316_chip = {
792         .hw_init = ar8316_hw_init,
793         .init_port = ar8216_init_port,
794         .setup_port = ar8216_setup_port,
795         .atu_flush = ar8216_atu_flush,
796         .vtu_flush = ar8216_vtu_flush,
797         .vtu_load_vlan = ar8216_vtu_load_vlan,
798 };
799
800 static int
801 ar8216_reset_switch(struct switch_dev *dev)
802 {
803         struct ar8216_priv *priv = to_ar8216(dev);
804         int i;
805
806         mutex_lock(&priv->reg_mutex);
807         memset(&priv->vlan, 0, sizeof(struct ar8216_priv) -
808                 offsetof(struct ar8216_priv, vlan));
809
810         for (i = 0; i < AR8X16_MAX_VLANS; i++)
811                 priv->vlan_id[i] = i;
812
813         /* Configure all ports */
814         for (i = 0; i < AR8216_NUM_PORTS; i++)
815                 priv->chip->init_port(priv, i);
816
817         ar8216_init_globals(priv);
818         mutex_unlock(&priv->reg_mutex);
819
820         return ar8216_hw_apply(dev);
821 }
822
823 static const struct switch_dev_ops ar8216_sw_ops = {
824         .attr_global = {
825                 .attr = ar8216_globals,
826                 .n_attr = ARRAY_SIZE(ar8216_globals),
827         },
828         .attr_port = {
829                 .attr = ar8216_port,
830                 .n_attr = ARRAY_SIZE(ar8216_port),
831         },
832         .attr_vlan = {
833                 .attr = ar8216_vlan,
834                 .n_attr = ARRAY_SIZE(ar8216_vlan),
835         },
836         .get_port_pvid = ar8216_get_pvid,
837         .set_port_pvid = ar8216_set_pvid,
838         .get_vlan_ports = ar8216_get_ports,
839         .set_vlan_ports = ar8216_set_ports,
840         .apply_config = ar8216_hw_apply,
841         .reset_switch = ar8216_reset_switch,
842         .get_port_link = ar8216_get_port_link,
843 };
844
845 static int
846 ar8216_id_chip(struct ar8216_priv *priv)
847 {
848         u32 val;
849         u16 id;
850         int i;
851
852         priv->chip_type = UNKNOWN;
853
854         val = ar8216_mii_read(priv, AR8216_REG_CTRL);
855         if (val == ~0)
856                 return -ENODEV;
857
858         id = val & (AR8216_CTRL_REVISION | AR8216_CTRL_VERSION);
859         for (i = 0; i < AR8X16_PROBE_RETRIES; i++) {
860                 u16 t;
861
862                 val = ar8216_mii_read(priv, AR8216_REG_CTRL);
863                 if (val == ~0)
864                         return -ENODEV;
865
866                 t = val & (AR8216_CTRL_REVISION | AR8216_CTRL_VERSION);
867                 if (t != id)
868                         return -ENODEV;
869         }
870
871         switch (id) {
872         case 0x0101:
873                 priv->chip_type = AR8216;
874                 priv->chip = &ar8216_chip;
875                 break;
876         case 0x0301:
877                 priv->chip_type = AR8236;
878                 priv->chip = &ar8236_chip;
879                 break;
880         case 0x1000:
881         case 0x1001:
882                 priv->chip_type = AR8316;
883                 priv->chip = &ar8316_chip;
884                 break;
885         default:
886                 printk(KERN_DEBUG
887                         "ar8216: Unknown Atheros device [ver=%d, rev=%d, phy_id=%04x%04x]\n",
888                         (int)(id >> AR8216_CTRL_VERSION_S),
889                         (int)(id & AR8216_CTRL_REVISION),
890                         mdiobus_read(priv->phy->bus, priv->phy->addr, 2),
891                         mdiobus_read(priv->phy->bus, priv->phy->addr, 3));
892
893                 return -ENODEV;
894         }
895
896         return 0;
897 }
898
899 static int
900 ar8216_config_init(struct phy_device *pdev)
901 {
902         struct ar8216_priv *priv = pdev->priv;
903         struct net_device *dev = pdev->attached_dev;
904         struct switch_dev *swdev;
905         int ret;
906
907         if (!priv) {
908                 priv = kzalloc(sizeof(struct ar8216_priv), GFP_KERNEL);
909                 if (priv == NULL)
910                         return -ENOMEM;
911         }
912
913         priv->phy = pdev;
914
915         ret = ar8216_id_chip(priv);
916         if (ret)
917                 goto err_free_priv;
918
919         if (pdev->addr != 0) {
920                 if (priv->chip_type == AR8316) {
921                         pdev->supported |= SUPPORTED_1000baseT_Full;
922                         pdev->advertising |= ADVERTISED_1000baseT_Full;
923
924                         /* check if we're attaching to the switch twice */
925                         pdev = pdev->bus->phy_map[0];
926                         if (!pdev) {
927                                 kfree(priv);
928                                 return 0;
929                         }
930
931                         /* switch device has not been initialized, reuse priv */
932                         if (!pdev->priv) {
933                                 priv->port4_phy = true;
934                                 pdev->priv = priv;
935                                 return 0;
936                         }
937
938                         kfree(priv);
939
940                         /* switch device has been initialized, reinit */
941                         priv = pdev->priv;
942                         priv->dev.ports = (AR8216_NUM_PORTS - 1);
943                         priv->initialized = false;
944                         priv->port4_phy = true;
945                         ar8316_hw_init(priv);
946                         return 0;
947                 }
948
949                 kfree(priv);
950                 return 0;
951         }
952
953         printk(KERN_INFO "%s: AR%d switch driver attached.\n",
954                 pdev->attached_dev->name, priv->chip_type);
955
956         pdev->supported = priv->chip_type == AR8316 ?
957                 SUPPORTED_1000baseT_Full : SUPPORTED_100baseT_Full;
958         pdev->advertising = pdev->supported;
959
960         mutex_init(&priv->reg_mutex);
961         priv->read = ar8216_mii_read;
962         priv->write = ar8216_mii_write;
963
964         pdev->priv = priv;
965
966         swdev = &priv->dev;
967         swdev->cpu_port = AR8216_PORT_CPU;
968         swdev->ops = &ar8216_sw_ops;
969         swdev->ports = AR8216_NUM_PORTS;
970
971         if (priv->chip_type == AR8316) {
972                 swdev->name = "Atheros AR8316";
973                 swdev->vlans = AR8X16_MAX_VLANS;
974
975                 if (priv->port4_phy) {
976                         /* port 5 connected to the other mac, therefore unusable */
977                         swdev->ports = (AR8216_NUM_PORTS - 1);
978                 }
979         } else if (priv->chip_type == AR8236) {
980                 swdev->name = "Atheros AR8236";
981                 swdev->vlans = AR8216_NUM_VLANS;
982                 swdev->ports = AR8216_NUM_PORTS;
983         } else {
984                 swdev->name = "Atheros AR8216";
985                 swdev->vlans = AR8216_NUM_VLANS;
986         }
987
988         ret = register_switch(&priv->dev, pdev->attached_dev);
989         if (ret)
990                 goto err_free_priv;
991
992         priv->init = true;
993
994         ret = priv->chip->hw_init(priv);
995         if (ret)
996                 goto err_free_priv;
997
998         ret = ar8216_reset_switch(&priv->dev);
999         if (ret)
1000                 goto err_free_priv;
1001
1002         dev->phy_ptr = priv;
1003
1004         /* VID fixup only needed on ar8216 */
1005         if (pdev->addr == 0 && priv->chip_type == AR8216) {
1006                 pdev->pkt_align = 2;
1007                 pdev->netif_receive_skb = ar8216_netif_receive_skb;
1008                 pdev->netif_rx = ar8216_netif_rx;
1009                 priv->ndo_old = dev->netdev_ops;
1010                 memcpy(&priv->ndo, priv->ndo_old, sizeof(struct net_device_ops));
1011                 priv->ndo.ndo_start_xmit = ar8216_mangle_tx;
1012                 dev->netdev_ops = &priv->ndo;
1013         }
1014
1015         priv->init = false;
1016
1017         return 0;
1018
1019 err_free_priv:
1020         kfree(priv);
1021         return ret;
1022 }
1023
1024 static int
1025 ar8216_read_status(struct phy_device *phydev)
1026 {
1027         struct ar8216_priv *priv = phydev->priv;
1028         struct switch_port_link link;
1029         int ret;
1030
1031         if (phydev->addr != 0)
1032                 return genphy_read_status(phydev);
1033
1034         ar8216_read_port_link(priv, phydev->addr, &link);
1035         phydev->link = !!link.link;
1036         if (!phydev->link)
1037                 return 0;
1038
1039         switch (link.speed) {
1040         case SWITCH_PORT_SPEED_10:
1041                 phydev->speed = SPEED_10;
1042                 break;
1043         case SWITCH_PORT_SPEED_100:
1044                 phydev->speed = SPEED_100;
1045                 break;
1046         case SWITCH_PORT_SPEED_1000:
1047                 phydev->speed = SPEED_1000;
1048                 break;
1049         default:
1050                 phydev->speed = 0;
1051         }
1052         phydev->duplex = link.duplex ? DUPLEX_FULL : DUPLEX_HALF;
1053
1054         /* flush the address translation unit */
1055         mutex_lock(&priv->reg_mutex);
1056         ret = priv->chip->atu_flush(priv);
1057         mutex_unlock(&priv->reg_mutex);
1058
1059         phydev->state = PHY_RUNNING;
1060         netif_carrier_on(phydev->attached_dev);
1061         phydev->adjust_link(phydev->attached_dev);
1062
1063         return ret;
1064 }
1065
1066 static int
1067 ar8216_config_aneg(struct phy_device *phydev)
1068 {
1069         if (phydev->addr == 0)
1070                 return 0;
1071
1072         return genphy_config_aneg(phydev);
1073 }
1074
1075 static int
1076 ar8216_probe(struct phy_device *pdev)
1077 {
1078         struct ar8216_priv priv;
1079
1080         priv.phy = pdev;
1081         return ar8216_id_chip(&priv);
1082 }
1083
1084 static void
1085 ar8216_remove(struct phy_device *pdev)
1086 {
1087         struct ar8216_priv *priv = pdev->priv;
1088         struct net_device *dev = pdev->attached_dev;
1089
1090         if (!priv)
1091                 return;
1092
1093         if (priv->ndo_old && dev)
1094                 dev->netdev_ops = priv->ndo_old;
1095         if (pdev->addr == 0)
1096                 unregister_switch(&priv->dev);
1097         kfree(priv);
1098 }
1099
1100 static struct phy_driver ar8216_driver = {
1101         .phy_id         = 0x004d0000,
1102         .name           = "Atheros AR8216/AR8236/AR8316",
1103         .phy_id_mask    = 0xffff0000,
1104         .features       = PHY_BASIC_FEATURES,
1105         .probe          = ar8216_probe,
1106         .remove         = ar8216_remove,
1107         .config_init    = &ar8216_config_init,
1108         .config_aneg    = &ar8216_config_aneg,
1109         .read_status    = &ar8216_read_status,
1110         .driver         = { .owner = THIS_MODULE },
1111 };
1112
1113 int __init
1114 ar8216_init(void)
1115 {
1116         return phy_driver_register(&ar8216_driver);
1117 }
1118
1119 void __exit
1120 ar8216_exit(void)
1121 {
1122         phy_driver_unregister(&ar8216_driver);
1123 }
1124
1125 module_init(ar8216_init);
1126 module_exit(ar8216_exit);
1127 MODULE_LICENSE("GPL");
1128