da83a50a942bb5d8f2ceae0a918a9e35328ac346
[openwrt/staging/jow.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 * Copyright (C) 2011-2012 Gabor Juhos <juhosg@openwrt.org>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 */
17
18 #include <linux/if.h>
19 #include <linux/module.h>
20 #include <linux/init.h>
21 #include <linux/list.h>
22 #include <linux/if_ether.h>
23 #include <linux/skbuff.h>
24 #include <linux/netdevice.h>
25 #include <linux/netlink.h>
26 #include <linux/bitops.h>
27 #include <net/genetlink.h>
28 #include <linux/switch.h>
29 #include <linux/delay.h>
30 #include <linux/phy.h>
31 #include <linux/netdevice.h>
32 #include <linux/etherdevice.h>
33 #include <linux/lockdep.h>
34 #include <linux/ar8216_platform.h>
35 #include "ar8216.h"
36
37 /* size of the vlan table */
38 #define AR8X16_MAX_VLANS 128
39 #define AR8X16_PROBE_RETRIES 10
40 #define AR8X16_MAX_PORTS 8
41
42 struct ar8216_priv;
43
44 #define AR8XXX_CAP_GIGE BIT(0)
45
46 struct ar8xxx_chip {
47 unsigned long caps;
48
49 int (*hw_init)(struct ar8216_priv *priv);
50 void (*init_globals)(struct ar8216_priv *priv);
51 void (*init_port)(struct ar8216_priv *priv, int port);
52 void (*setup_port)(struct ar8216_priv *priv, int port, u32 egress,
53 u32 ingress, u32 members, u32 pvid);
54 u32 (*read_port_status)(struct ar8216_priv *priv, int port);
55 int (*atu_flush)(struct ar8216_priv *priv);
56 void (*vtu_flush)(struct ar8216_priv *priv);
57 void (*vtu_load_vlan)(struct ar8216_priv *priv, u32 vid, u32 port_mask);
58 };
59
60 struct ar8216_priv {
61 struct switch_dev dev;
62 struct phy_device *phy;
63 u32 (*read)(struct ar8216_priv *priv, int reg);
64 void (*write)(struct ar8216_priv *priv, int reg, u32 val);
65 const struct net_device_ops *ndo_old;
66 struct net_device_ops ndo;
67 struct mutex reg_mutex;
68 int chip_type;
69 const struct ar8xxx_chip *chip;
70 bool initialized;
71 bool port4_phy;
72 char buf[80];
73
74 bool init;
75 bool mii_lo_first;
76
77 /* all fields below are cleared on reset */
78 bool vlan;
79 u16 vlan_id[AR8X16_MAX_VLANS];
80 u8 vlan_table[AR8X16_MAX_VLANS];
81 u8 vlan_tagged;
82 u16 pvid[AR8X16_MAX_PORTS];
83 };
84
85 #define to_ar8216(_dev) container_of(_dev, struct ar8216_priv, dev)
86
87 static inline bool ar8xxx_has_gige(struct ar8216_priv *priv)
88 {
89 return priv->chip->caps & AR8XXX_CAP_GIGE;
90 }
91
92 static inline void
93 split_addr(u32 regaddr, u16 *r1, u16 *r2, u16 *page)
94 {
95 regaddr >>= 1;
96 *r1 = regaddr & 0x1e;
97
98 regaddr >>= 5;
99 *r2 = regaddr & 0x7;
100
101 regaddr >>= 3;
102 *page = regaddr & 0x1ff;
103 }
104
105 static u32
106 ar8216_mii_read(struct ar8216_priv *priv, int reg)
107 {
108 struct phy_device *phy = priv->phy;
109 struct mii_bus *bus = phy->bus;
110 u16 r1, r2, page;
111 u16 lo, hi;
112
113 split_addr((u32) reg, &r1, &r2, &page);
114
115 mutex_lock(&bus->mdio_lock);
116
117 bus->write(bus, 0x18, 0, page);
118 usleep_range(1000, 2000); /* wait for the page switch to propagate */
119 lo = bus->read(bus, 0x10 | r2, r1);
120 hi = bus->read(bus, 0x10 | r2, r1 + 1);
121
122 mutex_unlock(&bus->mdio_lock);
123
124 return (hi << 16) | lo;
125 }
126
127 static void
128 ar8216_mii_write(struct ar8216_priv *priv, int reg, u32 val)
129 {
130 struct phy_device *phy = priv->phy;
131 struct mii_bus *bus = phy->bus;
132 u16 r1, r2, r3;
133 u16 lo, hi;
134
135 split_addr((u32) reg, &r1, &r2, &r3);
136 lo = val & 0xffff;
137 hi = (u16) (val >> 16);
138
139 mutex_lock(&bus->mdio_lock);
140
141 bus->write(bus, 0x18, 0, r3);
142 usleep_range(1000, 2000); /* wait for the page switch to propagate */
143 if (priv->mii_lo_first) {
144 bus->write(bus, 0x10 | r2, r1, lo);
145 bus->write(bus, 0x10 | r2, r1 + 1, hi);
146 } else {
147 bus->write(bus, 0x10 | r2, r1 + 1, hi);
148 bus->write(bus, 0x10 | r2, r1, lo);
149 }
150
151 mutex_unlock(&bus->mdio_lock);
152 }
153
154 static void
155 ar8216_phy_dbg_write(struct ar8216_priv *priv, int phy_addr,
156 u16 dbg_addr, u16 dbg_data)
157 {
158 struct mii_bus *bus = priv->phy->bus;
159
160 mutex_lock(&bus->mdio_lock);
161 bus->write(bus, phy_addr, MII_ATH_DBG_ADDR, dbg_addr);
162 bus->write(bus, phy_addr, MII_ATH_DBG_DATA, dbg_data);
163 mutex_unlock(&bus->mdio_lock);
164 }
165
166 static u32
167 ar8216_rmw(struct ar8216_priv *priv, int reg, u32 mask, u32 val)
168 {
169 u32 v;
170
171 lockdep_assert_held(&priv->reg_mutex);
172
173 v = priv->read(priv, reg);
174 v &= ~mask;
175 v |= val;
176 priv->write(priv, reg, v);
177
178 return v;
179 }
180
181 static void
182 ar8216_read_port_link(struct ar8216_priv *priv, int port,
183 struct switch_port_link *link)
184 {
185 u32 status;
186 u32 speed;
187
188 memset(link, '\0', sizeof(*link));
189
190 status = priv->chip->read_port_status(priv, port);
191
192 link->aneg = !!(status & AR8216_PORT_STATUS_LINK_AUTO);
193 if (link->aneg) {
194 link->link = !!(status & AR8216_PORT_STATUS_LINK_UP);
195 if (!link->link)
196 return;
197 } else {
198 link->link = true;
199 }
200
201 link->duplex = !!(status & AR8216_PORT_STATUS_DUPLEX);
202 link->tx_flow = !!(status & AR8216_PORT_STATUS_TXFLOW);
203 link->rx_flow = !!(status & AR8216_PORT_STATUS_RXFLOW);
204
205 speed = (status & AR8216_PORT_STATUS_SPEED) >>
206 AR8216_PORT_STATUS_SPEED_S;
207
208 switch (speed) {
209 case AR8216_PORT_SPEED_10M:
210 link->speed = SWITCH_PORT_SPEED_10;
211 break;
212 case AR8216_PORT_SPEED_100M:
213 link->speed = SWITCH_PORT_SPEED_100;
214 break;
215 case AR8216_PORT_SPEED_1000M:
216 link->speed = SWITCH_PORT_SPEED_1000;
217 break;
218 default:
219 link->speed = SWITCH_PORT_SPEED_UNKNOWN;
220 break;
221 }
222 }
223
224 static int
225 ar8216_mangle_tx(struct sk_buff *skb, struct net_device *dev)
226 {
227 struct ar8216_priv *priv = dev->phy_ptr;
228 unsigned char *buf;
229
230 if (unlikely(!priv))
231 goto error;
232
233 if (!priv->vlan)
234 goto send;
235
236 if (unlikely(skb_headroom(skb) < 2)) {
237 if (pskb_expand_head(skb, 2, 0, GFP_ATOMIC) < 0)
238 goto error;
239 }
240
241 buf = skb_push(skb, 2);
242 buf[0] = 0x10;
243 buf[1] = 0x80;
244
245 send:
246 return priv->ndo_old->ndo_start_xmit(skb, dev);
247
248 error:
249 dev_kfree_skb_any(skb);
250 return 0;
251 }
252
253 static int
254 ar8216_mangle_rx(struct sk_buff *skb, int napi)
255 {
256 struct ar8216_priv *priv;
257 struct net_device *dev;
258 unsigned char *buf;
259 int port, vlan;
260
261 dev = skb->dev;
262 if (!dev)
263 goto error;
264
265 priv = dev->phy_ptr;
266 if (!priv)
267 goto error;
268
269 /* don't strip the header if vlan mode is disabled */
270 if (!priv->vlan)
271 goto recv;
272
273 /* strip header, get vlan id */
274 buf = skb->data;
275 skb_pull(skb, 2);
276
277 /* check for vlan header presence */
278 if ((buf[12 + 2] != 0x81) || (buf[13 + 2] != 0x00))
279 goto recv;
280
281 port = buf[0] & 0xf;
282
283 /* no need to fix up packets coming from a tagged source */
284 if (priv->vlan_tagged & (1 << port))
285 goto recv;
286
287 /* lookup port vid from local table, the switch passes an invalid vlan id */
288 vlan = priv->vlan_id[priv->pvid[port]];
289
290 buf[14 + 2] &= 0xf0;
291 buf[14 + 2] |= vlan >> 8;
292 buf[15 + 2] = vlan & 0xff;
293
294 recv:
295 skb->protocol = eth_type_trans(skb, skb->dev);
296
297 if (napi)
298 return netif_receive_skb(skb);
299 else
300 return netif_rx(skb);
301
302 error:
303 /* no vlan? eat the packet! */
304 dev_kfree_skb_any(skb);
305 return NET_RX_DROP;
306 }
307
308 static int
309 ar8216_netif_rx(struct sk_buff *skb)
310 {
311 return ar8216_mangle_rx(skb, 0);
312 }
313
314 static int
315 ar8216_netif_receive_skb(struct sk_buff *skb)
316 {
317 return ar8216_mangle_rx(skb, 1);
318 }
319
320 static int
321 ar8216_wait_bit(struct ar8216_priv *priv, int reg, u32 mask, u32 val)
322 {
323 int timeout = 20;
324 u32 t = 0;
325
326 while (1) {
327 t = priv->read(priv, reg);
328 if ((t & mask) == val)
329 return 0;
330
331 if (timeout-- <= 0)
332 break;
333
334 udelay(10);
335 }
336
337 pr_err("ar8216: timeout on reg %08x: %08x & %08x != %08x\n",
338 (unsigned int) reg, t, mask, val);
339 return -ETIMEDOUT;
340 }
341
342 static void
343 ar8216_vtu_op(struct ar8216_priv *priv, u32 op, u32 val)
344 {
345 if (ar8216_wait_bit(priv, AR8216_REG_VTU, AR8216_VTU_ACTIVE, 0))
346 return;
347 if ((op & AR8216_VTU_OP) == AR8216_VTU_OP_LOAD) {
348 val &= AR8216_VTUDATA_MEMBER;
349 val |= AR8216_VTUDATA_VALID;
350 priv->write(priv, AR8216_REG_VTU_DATA, val);
351 }
352 op |= AR8216_VTU_ACTIVE;
353 priv->write(priv, AR8216_REG_VTU, op);
354 }
355
356 static void
357 ar8216_vtu_flush(struct ar8216_priv *priv)
358 {
359 ar8216_vtu_op(priv, AR8216_VTU_OP_FLUSH, 0);
360 }
361
362 static void
363 ar8216_vtu_load_vlan(struct ar8216_priv *priv, u32 vid, u32 port_mask)
364 {
365 u32 op;
366
367 op = AR8216_VTU_OP_LOAD | (vid << AR8216_VTU_VID_S);
368 ar8216_vtu_op(priv, op, port_mask);
369 }
370
371 static int
372 ar8216_atu_flush(struct ar8216_priv *priv)
373 {
374 int ret;
375
376 ret = ar8216_wait_bit(priv, AR8216_REG_ATU, AR8216_ATU_ACTIVE, 0);
377 if (!ret)
378 priv->write(priv, AR8216_REG_ATU, AR8216_ATU_OP_FLUSH);
379
380 return ret;
381 }
382
383 static u32
384 ar8216_read_port_status(struct ar8216_priv *priv, int port)
385 {
386 return priv->read(priv, AR8216_REG_PORT_STATUS(port));
387 }
388
389 static void
390 ar8216_setup_port(struct ar8216_priv *priv, int port, u32 egress, u32 ingress,
391 u32 members, u32 pvid)
392 {
393 u32 header;
394
395 if (priv->vlan && port == AR8216_PORT_CPU && priv->chip_type == AR8216)
396 header = AR8216_PORT_CTRL_HEADER;
397 else
398 header = 0;
399
400 ar8216_rmw(priv, AR8216_REG_PORT_CTRL(port),
401 AR8216_PORT_CTRL_LEARN | AR8216_PORT_CTRL_VLAN_MODE |
402 AR8216_PORT_CTRL_SINGLE_VLAN | AR8216_PORT_CTRL_STATE |
403 AR8216_PORT_CTRL_HEADER | AR8216_PORT_CTRL_LEARN_LOCK,
404 AR8216_PORT_CTRL_LEARN | header |
405 (egress << AR8216_PORT_CTRL_VLAN_MODE_S) |
406 (AR8216_PORT_STATE_FORWARD << AR8216_PORT_CTRL_STATE_S));
407
408 ar8216_rmw(priv, AR8216_REG_PORT_VLAN(port),
409 AR8216_PORT_VLAN_DEST_PORTS | AR8216_PORT_VLAN_MODE |
410 AR8216_PORT_VLAN_DEFAULT_ID,
411 (members << AR8216_PORT_VLAN_DEST_PORTS_S) |
412 (ingress << AR8216_PORT_VLAN_MODE_S) |
413 (pvid << AR8216_PORT_VLAN_DEFAULT_ID_S));
414 }
415
416 static int
417 ar8216_hw_init(struct ar8216_priv *priv)
418 {
419 return 0;
420 }
421
422 static void
423 ar8216_init_globals(struct ar8216_priv *priv)
424 {
425 /* standard atheros magic */
426 priv->write(priv, 0x38, 0xc000050e);
427
428 ar8216_rmw(priv, AR8216_REG_GLOBAL_CTRL,
429 AR8216_GCTRL_MTU, 1518 + 8 + 2);
430 }
431
432 static void
433 ar8216_init_port(struct ar8216_priv *priv, int port)
434 {
435 /* Enable port learning and tx */
436 priv->write(priv, AR8216_REG_PORT_CTRL(port),
437 AR8216_PORT_CTRL_LEARN |
438 (4 << AR8216_PORT_CTRL_STATE_S));
439
440 priv->write(priv, AR8216_REG_PORT_VLAN(port), 0);
441
442 if (port == AR8216_PORT_CPU) {
443 priv->write(priv, AR8216_REG_PORT_STATUS(port),
444 AR8216_PORT_STATUS_LINK_UP |
445 (ar8xxx_has_gige(priv) ?
446 AR8216_PORT_SPEED_1000M : AR8216_PORT_SPEED_100M) |
447 AR8216_PORT_STATUS_TXMAC |
448 AR8216_PORT_STATUS_RXMAC |
449 ((priv->chip_type == AR8316) ? AR8216_PORT_STATUS_RXFLOW : 0) |
450 ((priv->chip_type == AR8316) ? AR8216_PORT_STATUS_TXFLOW : 0) |
451 AR8216_PORT_STATUS_DUPLEX);
452 } else {
453 priv->write(priv, AR8216_REG_PORT_STATUS(port),
454 AR8216_PORT_STATUS_LINK_AUTO);
455 }
456 }
457
458 static const struct ar8xxx_chip ar8216_chip = {
459 .hw_init = ar8216_hw_init,
460 .init_globals = ar8216_init_globals,
461 .init_port = ar8216_init_port,
462 .setup_port = ar8216_setup_port,
463 .read_port_status = ar8216_read_port_status,
464 .atu_flush = ar8216_atu_flush,
465 .vtu_flush = ar8216_vtu_flush,
466 .vtu_load_vlan = ar8216_vtu_load_vlan,
467 };
468
469 static void
470 ar8236_setup_port(struct ar8216_priv *priv, int port, u32 egress, u32 ingress,
471 u32 members, u32 pvid)
472 {
473 ar8216_rmw(priv, AR8216_REG_PORT_CTRL(port),
474 AR8216_PORT_CTRL_LEARN | AR8216_PORT_CTRL_VLAN_MODE |
475 AR8216_PORT_CTRL_SINGLE_VLAN | AR8216_PORT_CTRL_STATE |
476 AR8216_PORT_CTRL_HEADER | AR8216_PORT_CTRL_LEARN_LOCK,
477 AR8216_PORT_CTRL_LEARN |
478 (egress << AR8216_PORT_CTRL_VLAN_MODE_S) |
479 (AR8216_PORT_STATE_FORWARD << AR8216_PORT_CTRL_STATE_S));
480
481 ar8216_rmw(priv, AR8236_REG_PORT_VLAN(port),
482 AR8236_PORT_VLAN_DEFAULT_ID,
483 (pvid << AR8236_PORT_VLAN_DEFAULT_ID_S));
484
485 ar8216_rmw(priv, AR8236_REG_PORT_VLAN2(port),
486 AR8236_PORT_VLAN2_VLAN_MODE |
487 AR8236_PORT_VLAN2_MEMBER,
488 (ingress << AR8236_PORT_VLAN2_VLAN_MODE_S) |
489 (members << AR8236_PORT_VLAN2_MEMBER_S));
490 }
491
492 static int
493 ar8236_hw_init(struct ar8216_priv *priv)
494 {
495 int i;
496 struct mii_bus *bus;
497
498 if (priv->initialized)
499 return 0;
500
501 /* Initialize the PHYs */
502 bus = priv->phy->bus;
503 for (i = 0; i < 5; i++) {
504 mdiobus_write(bus, i, MII_ADVERTISE,
505 ADVERTISE_ALL | ADVERTISE_PAUSE_CAP |
506 ADVERTISE_PAUSE_ASYM);
507 mdiobus_write(bus, i, MII_BMCR, BMCR_RESET | BMCR_ANENABLE);
508 }
509 msleep(1000);
510
511 priv->initialized = true;
512 return 0;
513 }
514
515 static void
516 ar8236_init_globals(struct ar8216_priv *priv)
517 {
518 /* enable jumbo frames */
519 ar8216_rmw(priv, AR8216_REG_GLOBAL_CTRL,
520 AR8316_GCTRL_MTU, 9018 + 8 + 2);
521 }
522
523 static const struct ar8xxx_chip ar8236_chip = {
524 .hw_init = ar8236_hw_init,
525 .init_globals = ar8236_init_globals,
526 .init_port = ar8216_init_port,
527 .setup_port = ar8236_setup_port,
528 .read_port_status = ar8216_read_port_status,
529 .atu_flush = ar8216_atu_flush,
530 .vtu_flush = ar8216_vtu_flush,
531 .vtu_load_vlan = ar8216_vtu_load_vlan,
532 };
533
534 static int
535 ar8316_hw_init(struct ar8216_priv *priv)
536 {
537 int i;
538 u32 val, newval;
539 struct mii_bus *bus;
540
541 val = priv->read(priv, 0x8);
542
543 if (priv->phy->interface == PHY_INTERFACE_MODE_RGMII) {
544 if (priv->port4_phy) {
545 /* value taken from Ubiquiti RouterStation Pro */
546 newval = 0x81461bea;
547 printk(KERN_INFO "ar8316: Using port 4 as PHY\n");
548 } else {
549 newval = 0x01261be2;
550 printk(KERN_INFO "ar8316: Using port 4 as switch port\n");
551 }
552 } else if (priv->phy->interface == PHY_INTERFACE_MODE_GMII) {
553 /* value taken from AVM Fritz!Box 7390 sources */
554 newval = 0x010e5b71;
555 } else {
556 /* no known value for phy interface */
557 printk(KERN_ERR "ar8316: unsupported mii mode: %d.\n",
558 priv->phy->interface);
559 return -EINVAL;
560 }
561
562 if (val == newval)
563 goto out;
564
565 priv->write(priv, 0x8, newval);
566
567 /* Initialize the ports */
568 bus = priv->phy->bus;
569 for (i = 0; i < 5; i++) {
570 if ((i == 4) && priv->port4_phy &&
571 priv->phy->interface == PHY_INTERFACE_MODE_RGMII) {
572 /* work around for phy4 rgmii mode */
573 ar8216_phy_dbg_write(priv, i, 0x12, 0x480c);
574 /* rx delay */
575 ar8216_phy_dbg_write(priv, i, 0x0, 0x824e);
576 /* tx delay */
577 ar8216_phy_dbg_write(priv, i, 0x5, 0x3d47);
578 msleep(1000);
579 }
580
581 /* initialize the port itself */
582 mdiobus_write(bus, i, MII_ADVERTISE,
583 ADVERTISE_ALL | ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM);
584 mdiobus_write(bus, i, MII_CTRL1000, ADVERTISE_1000FULL);
585 mdiobus_write(bus, i, MII_BMCR, BMCR_RESET | BMCR_ANENABLE);
586 msleep(1000);
587 }
588
589 out:
590 priv->initialized = true;
591 return 0;
592 }
593
594 static void
595 ar8316_init_globals(struct ar8216_priv *priv)
596 {
597 /* standard atheros magic */
598 priv->write(priv, 0x38, 0xc000050e);
599
600 /* enable cpu port to receive multicast and broadcast frames */
601 priv->write(priv, AR8216_REG_FLOOD_MASK, 0x003f003f);
602
603 /* enable jumbo frames */
604 ar8216_rmw(priv, AR8216_REG_GLOBAL_CTRL,
605 AR8316_GCTRL_MTU, 9018 + 8 + 2);
606 }
607
608 static const struct ar8xxx_chip ar8316_chip = {
609 .caps = AR8XXX_CAP_GIGE,
610 .hw_init = ar8316_hw_init,
611 .init_globals = ar8316_init_globals,
612 .init_port = ar8216_init_port,
613 .setup_port = ar8216_setup_port,
614 .read_port_status = ar8216_read_port_status,
615 .atu_flush = ar8216_atu_flush,
616 .vtu_flush = ar8216_vtu_flush,
617 .vtu_load_vlan = ar8216_vtu_load_vlan,
618 };
619
620 static u32
621 ar8327_get_pad_cfg(struct ar8327_pad_cfg *cfg)
622 {
623 u32 t;
624
625 if (!cfg)
626 return 0;
627
628 t = 0;
629 switch (cfg->mode) {
630 case AR8327_PAD_NC:
631 break;
632
633 case AR8327_PAD_MAC2MAC_MII:
634 t = AR8327_PAD_MAC_MII_EN;
635 if (cfg->rxclk_sel)
636 t |= AR8327_PAD_MAC_MII_RXCLK_SEL;
637 if (cfg->txclk_sel)
638 t |= AR8327_PAD_MAC_MII_TXCLK_SEL;
639 break;
640
641 case AR8327_PAD_MAC2MAC_GMII:
642 t = AR8327_PAD_MAC_GMII_EN;
643 if (cfg->rxclk_sel)
644 t |= AR8327_PAD_MAC_GMII_RXCLK_SEL;
645 if (cfg->txclk_sel)
646 t |= AR8327_PAD_MAC_GMII_TXCLK_SEL;
647 break;
648
649 case AR8327_PAD_MAC_SGMII:
650 t = AR8327_PAD_SGMII_EN;
651 break;
652
653 case AR8327_PAD_MAC2PHY_MII:
654 t = AR8327_PAD_PHY_MII_EN;
655 if (cfg->rxclk_sel)
656 t |= AR8327_PAD_PHY_MII_RXCLK_SEL;
657 if (cfg->txclk_sel)
658 t |= AR8327_PAD_PHY_MII_TXCLK_SEL;
659 break;
660
661 case AR8327_PAD_MAC2PHY_GMII:
662 t = AR8327_PAD_PHY_GMII_EN;
663 if (cfg->pipe_rxclk_sel)
664 t |= AR8327_PAD_PHY_GMII_PIPE_RXCLK_SEL;
665 if (cfg->rxclk_sel)
666 t |= AR8327_PAD_PHY_GMII_RXCLK_SEL;
667 if (cfg->txclk_sel)
668 t |= AR8327_PAD_PHY_GMII_TXCLK_SEL;
669 break;
670
671 case AR8327_PAD_MAC_RGMII:
672 t = AR8327_PAD_RGMII_EN;
673 t |= cfg->txclk_delay_sel << AR8327_PAD_RGMII_TXCLK_DELAY_SEL_S;
674 t |= cfg->rxclk_delay_sel << AR8327_PAD_RGMII_RXCLK_DELAY_SEL_S;
675 if (cfg->rxclk_delay_en)
676 t |= AR8327_PAD_RGMII_RXCLK_DELAY_EN;
677 if (cfg->txclk_delay_en)
678 t |= AR8327_PAD_RGMII_TXCLK_DELAY_EN;
679 break;
680
681 case AR8327_PAD_PHY_GMII:
682 t = AR8327_PAD_PHYX_GMII_EN;
683 break;
684
685 case AR8327_PAD_PHY_RGMII:
686 t = AR8327_PAD_PHYX_RGMII_EN;
687 break;
688
689 case AR8327_PAD_PHY_MII:
690 t = AR8327_PAD_PHYX_MII_EN;
691 break;
692 }
693
694 return t;
695 }
696
697 static int
698 ar8327_hw_init(struct ar8216_priv *priv)
699 {
700 struct ar8327_platform_data *pdata;
701 u32 t;
702 int i;
703
704 pdata = priv->phy->dev.platform_data;
705 if (!pdata)
706 return -EINVAL;
707
708 t = ar8327_get_pad_cfg(pdata->pad0_cfg);
709 priv->write(priv, AR8327_REG_PAD0_MODE, t);
710 t = ar8327_get_pad_cfg(pdata->pad5_cfg);
711 priv->write(priv, AR8327_REG_PAD5_MODE, t);
712 t = ar8327_get_pad_cfg(pdata->pad6_cfg);
713 priv->write(priv, AR8327_REG_PAD6_MODE, t);
714
715 priv->write(priv, AR8327_REG_POWER_ON_STRIP, 0x40000000);
716
717 /* fixup PHYs */
718 for (i = 0; i < AR8327_NUM_PHYS; i++) {
719 /* For 100M waveform */
720 ar8216_phy_dbg_write(priv, i, 0, 0x02ea);
721
722 /* Turn on Gigabit clock */
723 ar8216_phy_dbg_write(priv, i, 0x3d, 0x68a0);
724 }
725
726 return 0;
727 }
728
729 static void
730 ar8327_init_globals(struct ar8216_priv *priv)
731 {
732 u32 t;
733
734 /* enable CPU port and disable mirror port */
735 t = AR8327_FWD_CTRL0_CPU_PORT_EN |
736 AR8327_FWD_CTRL0_MIRROR_PORT;
737 priv->write(priv, AR8327_REG_FWD_CTRL0, t);
738
739 /* forward multicast and broadcast frames to CPU */
740 t = (AR8327_PORTS_ALL << AR8327_FWD_CTRL1_UC_FLOOD_S) |
741 (AR8327_PORTS_ALL << AR8327_FWD_CTRL1_MC_FLOOD_S) |
742 (AR8327_PORTS_ALL << AR8327_FWD_CTRL1_BC_FLOOD_S);
743 priv->write(priv, AR8327_REG_FWD_CTRL1, t);
744
745 /* setup MTU */
746 ar8216_rmw(priv, AR8327_REG_MAX_FRAME_SIZE,
747 AR8327_MAX_FRAME_SIZE_MTU, 1518 + 8 + 2);
748 }
749
750 static void
751 ar8327_init_cpuport(struct ar8216_priv *priv)
752 {
753 struct ar8327_platform_data *pdata;
754 struct ar8327_port_cfg *cfg;
755 u32 t;
756
757 pdata = priv->phy->dev.platform_data;
758 if (!pdata)
759 return;
760
761 cfg = &pdata->cpuport_cfg;
762 if (!cfg->force_link) {
763 priv->write(priv, AR8327_REG_PORT_STATUS(AR8216_PORT_CPU),
764 AR8216_PORT_STATUS_LINK_AUTO);
765 return;
766 }
767
768 t = AR8216_PORT_STATUS_TXMAC | AR8216_PORT_STATUS_RXMAC;
769 t |= cfg->duplex ? AR8216_PORT_STATUS_DUPLEX : 0;
770 t |= cfg->rxpause ? AR8216_PORT_STATUS_RXFLOW : 0;
771 t |= cfg->txpause ? AR8216_PORT_STATUS_TXFLOW : 0;
772 switch (cfg->speed) {
773 case AR8327_PORT_SPEED_10:
774 t |= AR8216_PORT_SPEED_10M;
775 break;
776 case AR8327_PORT_SPEED_100:
777 t |= AR8216_PORT_SPEED_100M;
778 break;
779 case AR8327_PORT_SPEED_1000:
780 t |= AR8216_PORT_SPEED_1000M;
781 break;
782 }
783
784 priv->write(priv, AR8327_REG_PORT_STATUS(AR8216_PORT_CPU), t);
785 }
786
787 static void
788 ar8327_init_port(struct ar8216_priv *priv, int port)
789 {
790 u32 t;
791
792 if (port == AR8216_PORT_CPU) {
793 ar8327_init_cpuport(priv);
794 } else {
795 t = AR8216_PORT_STATUS_LINK_AUTO;
796 priv->write(priv, AR8327_REG_PORT_STATUS(port), t);
797 }
798
799 priv->write(priv, AR8327_REG_PORT_HEADER(port), 0);
800
801 priv->write(priv, AR8327_REG_PORT_VLAN0(port), 0);
802
803 t = AR8327_PORT_VLAN1_OUT_MODE_UNTOUCH << AR8327_PORT_VLAN1_OUT_MODE_S;
804 priv->write(priv, AR8327_REG_PORT_VLAN1(port), t);
805
806 t = AR8327_PORT_LOOKUP_LEARN;
807 t |= AR8216_PORT_STATE_FORWARD << AR8327_PORT_LOOKUP_STATE_S;
808 priv->write(priv, AR8327_REG_PORT_LOOKUP(port), t);
809 }
810
811 static u32
812 ar8327_read_port_status(struct ar8216_priv *priv, int port)
813 {
814 return priv->read(priv, AR8327_REG_PORT_STATUS(port));
815 }
816
817 static int
818 ar8327_atu_flush(struct ar8216_priv *priv)
819 {
820 int ret;
821
822 ret = ar8216_wait_bit(priv, AR8327_REG_ATU_FUNC,
823 AR8327_ATU_FUNC_BUSY, 0);
824 if (!ret)
825 priv->write(priv, AR8327_REG_ATU_FUNC,
826 AR8327_ATU_FUNC_OP_FLUSH);
827
828 return ret;
829 }
830
831 static void
832 ar8327_vtu_op(struct ar8216_priv *priv, u32 op, u32 val)
833 {
834 if (ar8216_wait_bit(priv, AR8327_REG_VTU_FUNC1,
835 AR8327_VTU_FUNC1_BUSY, 0))
836 return;
837
838 if ((op & AR8327_VTU_FUNC1_OP) == AR8327_VTU_FUNC1_OP_LOAD)
839 priv->write(priv, AR8327_REG_VTU_FUNC0, val);
840
841 op |= AR8327_VTU_FUNC1_BUSY;
842 priv->write(priv, AR8327_REG_VTU_FUNC1, op);
843 }
844
845 static void
846 ar8327_vtu_flush(struct ar8216_priv *priv)
847 {
848 ar8327_vtu_op(priv, AR8327_VTU_FUNC1_OP_FLUSH, 0);
849 }
850
851 static void
852 ar8327_vtu_load_vlan(struct ar8216_priv *priv, u32 vid, u32 port_mask)
853 {
854 u32 op;
855 u32 val;
856 int i;
857
858 op = AR8327_VTU_FUNC1_OP_LOAD | (vid << AR8327_VTU_FUNC1_VID_S);
859 val = AR8327_VTU_FUNC0_VALID | AR8327_VTU_FUNC0_IVL;
860 for (i = 0; i < AR8327_NUM_PORTS; i++) {
861 u32 mode;
862
863 if ((port_mask & BIT(i)) == 0)
864 mode = AR8327_VTU_FUNC0_EG_MODE_NOT;
865 else if (priv->vlan == 0)
866 mode = AR8327_VTU_FUNC0_EG_MODE_KEEP;
867 else if (priv->vlan_tagged & BIT(i))
868 mode = AR8327_VTU_FUNC0_EG_MODE_TAG;
869 else
870 mode = AR8327_VTU_FUNC0_EG_MODE_UNTAG;
871
872 val |= mode << AR8327_VTU_FUNC0_EG_MODE_S(i);
873 }
874 ar8327_vtu_op(priv, op, val);
875 }
876
877 static void
878 ar8327_setup_port(struct ar8216_priv *priv, int port, u32 egress, u32 ingress,
879 u32 members, u32 pvid)
880 {
881 u32 t;
882 u32 mode;
883
884 t = pvid << AR8327_PORT_VLAN0_DEF_SVID_S;
885 t |= pvid << AR8327_PORT_VLAN0_DEF_CVID_S;
886 priv->write(priv, AR8327_REG_PORT_VLAN0(port), t);
887
888 mode = AR8327_PORT_VLAN1_OUT_MODE_UNMOD;
889 switch (egress) {
890 case AR8216_OUT_KEEP:
891 mode = AR8327_PORT_VLAN1_OUT_MODE_UNTOUCH;
892 break;
893 case AR8216_OUT_STRIP_VLAN:
894 mode = AR8327_PORT_VLAN1_OUT_MODE_UNTAG;
895 break;
896 case AR8216_OUT_ADD_VLAN:
897 mode = AR8327_PORT_VLAN1_OUT_MODE_TAG;
898 break;
899 }
900
901 t = AR8327_PORT_VLAN1_PORT_VLAN_PROP;
902 t |= mode << AR8327_PORT_VLAN1_OUT_MODE_S;
903 priv->write(priv, AR8327_REG_PORT_VLAN1(port), t);
904
905 t = members;
906 t |= AR8327_PORT_LOOKUP_LEARN;
907 t |= ingress << AR8327_PORT_LOOKUP_IN_MODE_S;
908 t |= AR8216_PORT_STATE_FORWARD << AR8327_PORT_LOOKUP_STATE_S;
909 priv->write(priv, AR8327_REG_PORT_LOOKUP(port), t);
910 }
911
912 static const struct ar8xxx_chip ar8327_chip = {
913 .caps = AR8XXX_CAP_GIGE,
914 .hw_init = ar8327_hw_init,
915 .init_globals = ar8327_init_globals,
916 .init_port = ar8327_init_port,
917 .setup_port = ar8327_setup_port,
918 .read_port_status = ar8327_read_port_status,
919 .atu_flush = ar8327_atu_flush,
920 .vtu_flush = ar8327_vtu_flush,
921 .vtu_load_vlan = ar8327_vtu_load_vlan,
922 };
923
924 static int
925 ar8216_sw_set_vlan(struct switch_dev *dev, const struct switch_attr *attr,
926 struct switch_val *val)
927 {
928 struct ar8216_priv *priv = to_ar8216(dev);
929 priv->vlan = !!val->value.i;
930 return 0;
931 }
932
933 static int
934 ar8216_sw_get_vlan(struct switch_dev *dev, const struct switch_attr *attr,
935 struct switch_val *val)
936 {
937 struct ar8216_priv *priv = to_ar8216(dev);
938 val->value.i = priv->vlan;
939 return 0;
940 }
941
942
943 static int
944 ar8216_sw_set_pvid(struct switch_dev *dev, int port, int vlan)
945 {
946 struct ar8216_priv *priv = to_ar8216(dev);
947
948 /* make sure no invalid PVIDs get set */
949
950 if (vlan >= dev->vlans)
951 return -EINVAL;
952
953 priv->pvid[port] = vlan;
954 return 0;
955 }
956
957 static int
958 ar8216_sw_get_pvid(struct switch_dev *dev, int port, int *vlan)
959 {
960 struct ar8216_priv *priv = to_ar8216(dev);
961 *vlan = priv->pvid[port];
962 return 0;
963 }
964
965 static int
966 ar8216_sw_set_vid(struct switch_dev *dev, const struct switch_attr *attr,
967 struct switch_val *val)
968 {
969 struct ar8216_priv *priv = to_ar8216(dev);
970 priv->vlan_id[val->port_vlan] = val->value.i;
971 return 0;
972 }
973
974 static int
975 ar8216_sw_get_vid(struct switch_dev *dev, const struct switch_attr *attr,
976 struct switch_val *val)
977 {
978 struct ar8216_priv *priv = to_ar8216(dev);
979 val->value.i = priv->vlan_id[val->port_vlan];
980 return 0;
981 }
982
983 static int
984 ar8216_sw_get_port_link(struct switch_dev *dev, int port,
985 struct switch_port_link *link)
986 {
987 struct ar8216_priv *priv = to_ar8216(dev);
988
989 ar8216_read_port_link(priv, port, link);
990 return 0;
991 }
992
993 static int
994 ar8216_sw_get_ports(struct switch_dev *dev, struct switch_val *val)
995 {
996 struct ar8216_priv *priv = to_ar8216(dev);
997 u8 ports = priv->vlan_table[val->port_vlan];
998 int i;
999
1000 val->len = 0;
1001 for (i = 0; i < dev->ports; i++) {
1002 struct switch_port *p;
1003
1004 if (!(ports & (1 << i)))
1005 continue;
1006
1007 p = &val->value.ports[val->len++];
1008 p->id = i;
1009 if (priv->vlan_tagged & (1 << i))
1010 p->flags = (1 << SWITCH_PORT_FLAG_TAGGED);
1011 else
1012 p->flags = 0;
1013 }
1014 return 0;
1015 }
1016
1017 static int
1018 ar8216_sw_set_ports(struct switch_dev *dev, struct switch_val *val)
1019 {
1020 struct ar8216_priv *priv = to_ar8216(dev);
1021 u8 *vt = &priv->vlan_table[val->port_vlan];
1022 int i, j;
1023
1024 *vt = 0;
1025 for (i = 0; i < val->len; i++) {
1026 struct switch_port *p = &val->value.ports[i];
1027
1028 if (p->flags & (1 << SWITCH_PORT_FLAG_TAGGED)) {
1029 priv->vlan_tagged |= (1 << p->id);
1030 } else {
1031 priv->vlan_tagged &= ~(1 << p->id);
1032 priv->pvid[p->id] = val->port_vlan;
1033
1034 /* make sure that an untagged port does not
1035 * appear in other vlans */
1036 for (j = 0; j < AR8X16_MAX_VLANS; j++) {
1037 if (j == val->port_vlan)
1038 continue;
1039 priv->vlan_table[j] &= ~(1 << p->id);
1040 }
1041 }
1042
1043 *vt |= 1 << p->id;
1044 }
1045 return 0;
1046 }
1047
1048 static int
1049 ar8216_sw_hw_apply(struct switch_dev *dev)
1050 {
1051 struct ar8216_priv *priv = to_ar8216(dev);
1052 u8 portmask[AR8X16_MAX_PORTS];
1053 int i, j;
1054
1055 mutex_lock(&priv->reg_mutex);
1056 /* flush all vlan translation unit entries */
1057 priv->chip->vtu_flush(priv);
1058
1059 memset(portmask, 0, sizeof(portmask));
1060 if (!priv->init) {
1061 /* calculate the port destination masks and load vlans
1062 * into the vlan translation unit */
1063 for (j = 0; j < AR8X16_MAX_VLANS; j++) {
1064 u8 vp = priv->vlan_table[j];
1065
1066 if (!vp)
1067 continue;
1068
1069 for (i = 0; i < dev->ports; i++) {
1070 u8 mask = (1 << i);
1071 if (vp & mask)
1072 portmask[i] |= vp & ~mask;
1073 }
1074
1075 priv->chip->vtu_load_vlan(priv, priv->vlan_id[j],
1076 priv->vlan_table[j]);
1077 }
1078 } else {
1079 /* vlan disabled:
1080 * isolate all ports, but connect them to the cpu port */
1081 for (i = 0; i < dev->ports; i++) {
1082 if (i == AR8216_PORT_CPU)
1083 continue;
1084
1085 portmask[i] = 1 << AR8216_PORT_CPU;
1086 portmask[AR8216_PORT_CPU] |= (1 << i);
1087 }
1088 }
1089
1090 /* update the port destination mask registers and tag settings */
1091 for (i = 0; i < dev->ports; i++) {
1092 int egress, ingress;
1093 int pvid;
1094
1095 if (priv->vlan) {
1096 pvid = priv->vlan_id[priv->pvid[i]];
1097 if (priv->vlan_tagged & (1 << i))
1098 egress = AR8216_OUT_ADD_VLAN;
1099 else
1100 egress = AR8216_OUT_STRIP_VLAN;
1101 ingress = AR8216_IN_SECURE;
1102 } else {
1103 pvid = i;
1104 egress = AR8216_OUT_KEEP;
1105 ingress = AR8216_IN_PORT_ONLY;
1106 }
1107
1108 priv->chip->setup_port(priv, i, egress, ingress, portmask[i],
1109 pvid);
1110 }
1111 mutex_unlock(&priv->reg_mutex);
1112 return 0;
1113 }
1114
1115 static int
1116 ar8216_sw_reset_switch(struct switch_dev *dev)
1117 {
1118 struct ar8216_priv *priv = to_ar8216(dev);
1119 int i;
1120
1121 mutex_lock(&priv->reg_mutex);
1122 memset(&priv->vlan, 0, sizeof(struct ar8216_priv) -
1123 offsetof(struct ar8216_priv, vlan));
1124
1125 for (i = 0; i < AR8X16_MAX_VLANS; i++)
1126 priv->vlan_id[i] = i;
1127
1128 /* Configure all ports */
1129 for (i = 0; i < dev->ports; i++)
1130 priv->chip->init_port(priv, i);
1131
1132 priv->chip->init_globals(priv);
1133 mutex_unlock(&priv->reg_mutex);
1134
1135 return ar8216_sw_hw_apply(dev);
1136 }
1137
1138 static struct switch_attr ar8216_globals[] = {
1139 {
1140 .type = SWITCH_TYPE_INT,
1141 .name = "enable_vlan",
1142 .description = "Enable VLAN mode",
1143 .set = ar8216_sw_set_vlan,
1144 .get = ar8216_sw_get_vlan,
1145 .max = 1
1146 },
1147 };
1148
1149 static struct switch_attr ar8216_port[] = {
1150 };
1151
1152 static struct switch_attr ar8216_vlan[] = {
1153 {
1154 .type = SWITCH_TYPE_INT,
1155 .name = "vid",
1156 .description = "VLAN ID (0-4094)",
1157 .set = ar8216_sw_set_vid,
1158 .get = ar8216_sw_get_vid,
1159 .max = 4094,
1160 },
1161 };
1162
1163 static const struct switch_dev_ops ar8216_sw_ops = {
1164 .attr_global = {
1165 .attr = ar8216_globals,
1166 .n_attr = ARRAY_SIZE(ar8216_globals),
1167 },
1168 .attr_port = {
1169 .attr = ar8216_port,
1170 .n_attr = ARRAY_SIZE(ar8216_port),
1171 },
1172 .attr_vlan = {
1173 .attr = ar8216_vlan,
1174 .n_attr = ARRAY_SIZE(ar8216_vlan),
1175 },
1176 .get_port_pvid = ar8216_sw_get_pvid,
1177 .set_port_pvid = ar8216_sw_set_pvid,
1178 .get_vlan_ports = ar8216_sw_get_ports,
1179 .set_vlan_ports = ar8216_sw_set_ports,
1180 .apply_config = ar8216_sw_hw_apply,
1181 .reset_switch = ar8216_sw_reset_switch,
1182 .get_port_link = ar8216_sw_get_port_link,
1183 };
1184
1185 static int
1186 ar8216_id_chip(struct ar8216_priv *priv)
1187 {
1188 u32 val;
1189 u16 id;
1190 int i;
1191
1192 priv->chip_type = UNKNOWN;
1193
1194 val = ar8216_mii_read(priv, AR8216_REG_CTRL);
1195 if (val == ~0)
1196 return -ENODEV;
1197
1198 id = val & (AR8216_CTRL_REVISION | AR8216_CTRL_VERSION);
1199 for (i = 0; i < AR8X16_PROBE_RETRIES; i++) {
1200 u16 t;
1201
1202 val = ar8216_mii_read(priv, AR8216_REG_CTRL);
1203 if (val == ~0)
1204 return -ENODEV;
1205
1206 t = val & (AR8216_CTRL_REVISION | AR8216_CTRL_VERSION);
1207 if (t != id)
1208 return -ENODEV;
1209 }
1210
1211 switch (id) {
1212 case 0x0101:
1213 priv->chip_type = AR8216;
1214 priv->chip = &ar8216_chip;
1215 break;
1216 case 0x0301:
1217 priv->chip_type = AR8236;
1218 priv->chip = &ar8236_chip;
1219 break;
1220 case 0x1000:
1221 case 0x1001:
1222 priv->chip_type = AR8316;
1223 priv->chip = &ar8316_chip;
1224 break;
1225 case 0x1202:
1226 priv->chip_type = AR8327;
1227 priv->mii_lo_first = true;
1228 priv->chip = &ar8327_chip;
1229 break;
1230 default:
1231 printk(KERN_DEBUG
1232 "ar8216: Unknown Atheros device [ver=%d, rev=%d, phy_id=%04x%04x]\n",
1233 (int)(id >> AR8216_CTRL_VERSION_S),
1234 (int)(id & AR8216_CTRL_REVISION),
1235 mdiobus_read(priv->phy->bus, priv->phy->addr, 2),
1236 mdiobus_read(priv->phy->bus, priv->phy->addr, 3));
1237
1238 return -ENODEV;
1239 }
1240
1241 return 0;
1242 }
1243
1244 static int
1245 ar8216_config_init(struct phy_device *pdev)
1246 {
1247 struct ar8216_priv *priv = pdev->priv;
1248 struct net_device *dev = pdev->attached_dev;
1249 struct switch_dev *swdev;
1250 int ret;
1251
1252 if (!priv) {
1253 priv = kzalloc(sizeof(struct ar8216_priv), GFP_KERNEL);
1254 if (priv == NULL)
1255 return -ENOMEM;
1256 }
1257
1258 priv->phy = pdev;
1259
1260 ret = ar8216_id_chip(priv);
1261 if (ret)
1262 goto err_free_priv;
1263
1264 if (pdev->addr != 0) {
1265 if (ar8xxx_has_gige(priv)) {
1266 pdev->supported |= SUPPORTED_1000baseT_Full;
1267 pdev->advertising |= ADVERTISED_1000baseT_Full;
1268 }
1269
1270 if (priv->chip_type == AR8316) {
1271 /* check if we're attaching to the switch twice */
1272 pdev = pdev->bus->phy_map[0];
1273 if (!pdev) {
1274 kfree(priv);
1275 return 0;
1276 }
1277
1278 /* switch device has not been initialized, reuse priv */
1279 if (!pdev->priv) {
1280 priv->port4_phy = true;
1281 pdev->priv = priv;
1282 return 0;
1283 }
1284
1285 kfree(priv);
1286
1287 /* switch device has been initialized, reinit */
1288 priv = pdev->priv;
1289 priv->dev.ports = (AR8216_NUM_PORTS - 1);
1290 priv->initialized = false;
1291 priv->port4_phy = true;
1292 ar8316_hw_init(priv);
1293 return 0;
1294 }
1295
1296 kfree(priv);
1297 return 0;
1298 }
1299
1300 printk(KERN_INFO "%s: AR%d switch driver attached.\n",
1301 pdev->attached_dev->name, priv->chip_type);
1302
1303 if (ar8xxx_has_gige(priv))
1304 pdev->supported = SUPPORTED_1000baseT_Full;
1305 else
1306 pdev->supported = SUPPORTED_100baseT_Full;
1307 pdev->advertising = pdev->supported;
1308
1309 mutex_init(&priv->reg_mutex);
1310 priv->read = ar8216_mii_read;
1311 priv->write = ar8216_mii_write;
1312
1313 pdev->priv = priv;
1314
1315 swdev = &priv->dev;
1316 swdev->cpu_port = AR8216_PORT_CPU;
1317 swdev->ops = &ar8216_sw_ops;
1318 swdev->ports = AR8216_NUM_PORTS;
1319
1320 if (priv->chip_type == AR8316) {
1321 swdev->name = "Atheros AR8316";
1322 swdev->vlans = AR8X16_MAX_VLANS;
1323
1324 if (priv->port4_phy) {
1325 /* port 5 connected to the other mac, therefore unusable */
1326 swdev->ports = (AR8216_NUM_PORTS - 1);
1327 }
1328 } else if (priv->chip_type == AR8236) {
1329 swdev->name = "Atheros AR8236";
1330 swdev->vlans = AR8216_NUM_VLANS;
1331 swdev->ports = AR8216_NUM_PORTS;
1332 } else if (priv->chip_type == AR8327) {
1333 swdev->name = "Atheros AR8327";
1334 swdev->vlans = AR8X16_MAX_VLANS;
1335 swdev->ports = AR8327_NUM_PORTS;
1336 } else {
1337 swdev->name = "Atheros AR8216";
1338 swdev->vlans = AR8216_NUM_VLANS;
1339 }
1340
1341 ret = register_switch(&priv->dev, pdev->attached_dev);
1342 if (ret)
1343 goto err_free_priv;
1344
1345 priv->init = true;
1346
1347 ret = priv->chip->hw_init(priv);
1348 if (ret)
1349 goto err_free_priv;
1350
1351 ret = ar8216_sw_reset_switch(&priv->dev);
1352 if (ret)
1353 goto err_free_priv;
1354
1355 dev->phy_ptr = priv;
1356
1357 /* VID fixup only needed on ar8216 */
1358 if (pdev->addr == 0 && priv->chip_type == AR8216) {
1359 pdev->pkt_align = 2;
1360 pdev->netif_receive_skb = ar8216_netif_receive_skb;
1361 pdev->netif_rx = ar8216_netif_rx;
1362 priv->ndo_old = dev->netdev_ops;
1363 memcpy(&priv->ndo, priv->ndo_old, sizeof(struct net_device_ops));
1364 priv->ndo.ndo_start_xmit = ar8216_mangle_tx;
1365 dev->netdev_ops = &priv->ndo;
1366 }
1367
1368 priv->init = false;
1369
1370 return 0;
1371
1372 err_free_priv:
1373 kfree(priv);
1374 return ret;
1375 }
1376
1377 static int
1378 ar8216_read_status(struct phy_device *phydev)
1379 {
1380 struct ar8216_priv *priv = phydev->priv;
1381 struct switch_port_link link;
1382 int ret;
1383
1384 if (phydev->addr != 0)
1385 return genphy_read_status(phydev);
1386
1387 ar8216_read_port_link(priv, phydev->addr, &link);
1388 phydev->link = !!link.link;
1389 if (!phydev->link)
1390 return 0;
1391
1392 switch (link.speed) {
1393 case SWITCH_PORT_SPEED_10:
1394 phydev->speed = SPEED_10;
1395 break;
1396 case SWITCH_PORT_SPEED_100:
1397 phydev->speed = SPEED_100;
1398 break;
1399 case SWITCH_PORT_SPEED_1000:
1400 phydev->speed = SPEED_1000;
1401 break;
1402 default:
1403 phydev->speed = 0;
1404 }
1405 phydev->duplex = link.duplex ? DUPLEX_FULL : DUPLEX_HALF;
1406
1407 /* flush the address translation unit */
1408 mutex_lock(&priv->reg_mutex);
1409 ret = priv->chip->atu_flush(priv);
1410 mutex_unlock(&priv->reg_mutex);
1411
1412 phydev->state = PHY_RUNNING;
1413 netif_carrier_on(phydev->attached_dev);
1414 phydev->adjust_link(phydev->attached_dev);
1415
1416 return ret;
1417 }
1418
1419 static int
1420 ar8216_config_aneg(struct phy_device *phydev)
1421 {
1422 if (phydev->addr == 0)
1423 return 0;
1424
1425 return genphy_config_aneg(phydev);
1426 }
1427
1428 static int
1429 ar8216_probe(struct phy_device *pdev)
1430 {
1431 struct ar8216_priv priv;
1432
1433 priv.phy = pdev;
1434 return ar8216_id_chip(&priv);
1435 }
1436
1437 static void
1438 ar8216_remove(struct phy_device *pdev)
1439 {
1440 struct ar8216_priv *priv = pdev->priv;
1441 struct net_device *dev = pdev->attached_dev;
1442
1443 if (!priv)
1444 return;
1445
1446 if (priv->ndo_old && dev)
1447 dev->netdev_ops = priv->ndo_old;
1448 if (pdev->addr == 0)
1449 unregister_switch(&priv->dev);
1450 kfree(priv);
1451 }
1452
1453 static struct phy_driver ar8216_driver = {
1454 .phy_id = 0x004d0000,
1455 .name = "Atheros AR8216/AR8236/AR8316",
1456 .phy_id_mask = 0xffff0000,
1457 .features = PHY_BASIC_FEATURES,
1458 .probe = ar8216_probe,
1459 .remove = ar8216_remove,
1460 .config_init = &ar8216_config_init,
1461 .config_aneg = &ar8216_config_aneg,
1462 .read_status = &ar8216_read_status,
1463 .driver = { .owner = THIS_MODULE },
1464 };
1465
1466 int __init
1467 ar8216_init(void)
1468 {
1469 return phy_driver_register(&ar8216_driver);
1470 }
1471
1472 void __exit
1473 ar8216_exit(void)
1474 {
1475 phy_driver_unregister(&ar8216_driver);
1476 }
1477
1478 module_init(ar8216_init);
1479 module_exit(ar8216_exit);
1480 MODULE_LICENSE("GPL");
1481