generic: ar8216: fix port number check in 'ar8xxx_mib_work_func'
[openwrt/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 * 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 <linux/workqueue.h>
36 #include "ar8216.h"
37
38 /* size of the vlan table */
39 #define AR8X16_MAX_VLANS 128
40 #define AR8X16_PROBE_RETRIES 10
41 #define AR8X16_MAX_PORTS 8
42
43 #define AR8XXX_MIB_WORK_DELAY 2000 /* msecs */
44
45 struct ar8216_priv;
46
47 #define AR8XXX_CAP_GIGE BIT(0)
48 #define AR8XXX_CAP_MIB_COUNTERS BIT(1)
49
50 enum {
51 AR8XXX_VER_AR8216 = 0x01,
52 AR8XXX_VER_AR8236 = 0x03,
53 AR8XXX_VER_AR8316 = 0x10,
54 AR8XXX_VER_AR8327 = 0x12,
55 };
56
57 struct ar8xxx_mib_desc {
58 unsigned int size;
59 unsigned int offset;
60 const char *name;
61 };
62
63 struct ar8xxx_chip {
64 unsigned long caps;
65
66 int (*hw_init)(struct ar8216_priv *priv);
67 void (*init_globals)(struct ar8216_priv *priv);
68 void (*init_port)(struct ar8216_priv *priv, int port);
69 void (*setup_port)(struct ar8216_priv *priv, int port, u32 egress,
70 u32 ingress, u32 members, u32 pvid);
71 u32 (*read_port_status)(struct ar8216_priv *priv, int port);
72 int (*atu_flush)(struct ar8216_priv *priv);
73 void (*vtu_flush)(struct ar8216_priv *priv);
74 void (*vtu_load_vlan)(struct ar8216_priv *priv, u32 vid, u32 port_mask);
75
76 const struct ar8xxx_mib_desc *mib_decs;
77 unsigned num_mibs;
78 };
79
80 struct ar8216_priv {
81 struct switch_dev dev;
82 struct phy_device *phy;
83 u32 (*read)(struct ar8216_priv *priv, int reg);
84 void (*write)(struct ar8216_priv *priv, int reg, u32 val);
85 const struct net_device_ops *ndo_old;
86 struct net_device_ops ndo;
87 struct mutex reg_mutex;
88 u8 chip_ver;
89 u8 chip_rev;
90 const struct ar8xxx_chip *chip;
91 bool initialized;
92 bool port4_phy;
93 char buf[2048];
94
95 bool init;
96 bool mii_lo_first;
97
98 struct mutex mib_lock;
99 struct delayed_work mib_work;
100 int mib_next_port;
101 u64 *mib_stats;
102
103 /* all fields below are cleared on reset */
104 bool vlan;
105 u16 vlan_id[AR8X16_MAX_VLANS];
106 u8 vlan_table[AR8X16_MAX_VLANS];
107 u8 vlan_tagged;
108 u16 pvid[AR8X16_MAX_PORTS];
109 };
110
111 #define MIB_DESC(_s , _o, _n) \
112 { \
113 .size = (_s), \
114 .offset = (_o), \
115 .name = (_n), \
116 }
117
118 static const struct ar8xxx_mib_desc ar8216_mibs[] = {
119 MIB_DESC(1, AR8216_STATS_RXBROAD, "RxBroad"),
120 MIB_DESC(1, AR8216_STATS_RXPAUSE, "RxPause"),
121 MIB_DESC(1, AR8216_STATS_RXMULTI, "RxMulti"),
122 MIB_DESC(1, AR8216_STATS_RXFCSERR, "RxFcsErr"),
123 MIB_DESC(1, AR8216_STATS_RXALIGNERR, "RxAlignErr"),
124 MIB_DESC(1, AR8216_STATS_RXRUNT, "RxRunt"),
125 MIB_DESC(1, AR8216_STATS_RXFRAGMENT, "RxFragment"),
126 MIB_DESC(1, AR8216_STATS_RX64BYTE, "Rx64Byte"),
127 MIB_DESC(1, AR8216_STATS_RX128BYTE, "Rx128Byte"),
128 MIB_DESC(1, AR8216_STATS_RX256BYTE, "Rx256Byte"),
129 MIB_DESC(1, AR8216_STATS_RX512BYTE, "Rx512Byte"),
130 MIB_DESC(1, AR8216_STATS_RX1024BYTE, "Rx1024Byte"),
131 MIB_DESC(1, AR8216_STATS_RXMAXBYTE, "RxMaxByte"),
132 MIB_DESC(1, AR8216_STATS_RXTOOLONG, "RxTooLong"),
133 MIB_DESC(2, AR8216_STATS_RXGOODBYTE, "RxGoodByte"),
134 MIB_DESC(2, AR8216_STATS_RXBADBYTE, "RxBadByte"),
135 MIB_DESC(1, AR8216_STATS_RXOVERFLOW, "RxOverFlow"),
136 MIB_DESC(1, AR8216_STATS_FILTERED, "Filtered"),
137 MIB_DESC(1, AR8216_STATS_TXBROAD, "TxBroad"),
138 MIB_DESC(1, AR8216_STATS_TXPAUSE, "TxPause"),
139 MIB_DESC(1, AR8216_STATS_TXMULTI, "TxMulti"),
140 MIB_DESC(1, AR8216_STATS_TXUNDERRUN, "TxUnderRun"),
141 MIB_DESC(1, AR8216_STATS_TX64BYTE, "Tx64Byte"),
142 MIB_DESC(1, AR8216_STATS_TX128BYTE, "Tx128Byte"),
143 MIB_DESC(1, AR8216_STATS_TX256BYTE, "Tx256Byte"),
144 MIB_DESC(1, AR8216_STATS_TX512BYTE, "Tx512Byte"),
145 MIB_DESC(1, AR8216_STATS_TX1024BYTE, "Tx1024Byte"),
146 MIB_DESC(1, AR8216_STATS_TXMAXBYTE, "TxMaxByte"),
147 MIB_DESC(1, AR8216_STATS_TXOVERSIZE, "TxOverSize"),
148 MIB_DESC(2, AR8216_STATS_TXBYTE, "TxByte"),
149 MIB_DESC(1, AR8216_STATS_TXCOLLISION, "TxCollision"),
150 MIB_DESC(1, AR8216_STATS_TXABORTCOL, "TxAbortCol"),
151 MIB_DESC(1, AR8216_STATS_TXMULTICOL, "TxMultiCol"),
152 MIB_DESC(1, AR8216_STATS_TXSINGLECOL, "TxSingleCol"),
153 MIB_DESC(1, AR8216_STATS_TXEXCDEFER, "TxExcDefer"),
154 MIB_DESC(1, AR8216_STATS_TXDEFER, "TxDefer"),
155 MIB_DESC(1, AR8216_STATS_TXLATECOL, "TxLateCol"),
156 };
157
158 static const struct ar8xxx_mib_desc ar8236_mibs[] = {
159 MIB_DESC(1, AR8236_STATS_RXBROAD, "RxBroad"),
160 MIB_DESC(1, AR8236_STATS_RXPAUSE, "RxPause"),
161 MIB_DESC(1, AR8236_STATS_RXMULTI, "RxMulti"),
162 MIB_DESC(1, AR8236_STATS_RXFCSERR, "RxFcsErr"),
163 MIB_DESC(1, AR8236_STATS_RXALIGNERR, "RxAlignErr"),
164 MIB_DESC(1, AR8236_STATS_RXRUNT, "RxRunt"),
165 MIB_DESC(1, AR8236_STATS_RXFRAGMENT, "RxFragment"),
166 MIB_DESC(1, AR8236_STATS_RX64BYTE, "Rx64Byte"),
167 MIB_DESC(1, AR8236_STATS_RX128BYTE, "Rx128Byte"),
168 MIB_DESC(1, AR8236_STATS_RX256BYTE, "Rx256Byte"),
169 MIB_DESC(1, AR8236_STATS_RX512BYTE, "Rx512Byte"),
170 MIB_DESC(1, AR8236_STATS_RX1024BYTE, "Rx1024Byte"),
171 MIB_DESC(1, AR8236_STATS_RX1518BYTE, "Rx1518Byte"),
172 MIB_DESC(1, AR8236_STATS_RXMAXBYTE, "RxMaxByte"),
173 MIB_DESC(1, AR8236_STATS_RXTOOLONG, "RxTooLong"),
174 MIB_DESC(2, AR8236_STATS_RXGOODBYTE, "RxGoodByte"),
175 MIB_DESC(2, AR8236_STATS_RXBADBYTE, "RxBadByte"),
176 MIB_DESC(1, AR8236_STATS_RXOVERFLOW, "RxOverFlow"),
177 MIB_DESC(1, AR8236_STATS_FILTERED, "Filtered"),
178 MIB_DESC(1, AR8236_STATS_TXBROAD, "TxBroad"),
179 MIB_DESC(1, AR8236_STATS_TXPAUSE, "TxPause"),
180 MIB_DESC(1, AR8236_STATS_TXMULTI, "TxMulti"),
181 MIB_DESC(1, AR8236_STATS_TXUNDERRUN, "TxUnderRun"),
182 MIB_DESC(1, AR8236_STATS_TX64BYTE, "Tx64Byte"),
183 MIB_DESC(1, AR8236_STATS_TX128BYTE, "Tx128Byte"),
184 MIB_DESC(1, AR8236_STATS_TX256BYTE, "Tx256Byte"),
185 MIB_DESC(1, AR8236_STATS_TX512BYTE, "Tx512Byte"),
186 MIB_DESC(1, AR8236_STATS_TX1024BYTE, "Tx1024Byte"),
187 MIB_DESC(1, AR8236_STATS_TX1518BYTE, "Tx1518Byte"),
188 MIB_DESC(1, AR8236_STATS_TXMAXBYTE, "TxMaxByte"),
189 MIB_DESC(1, AR8236_STATS_TXOVERSIZE, "TxOverSize"),
190 MIB_DESC(2, AR8236_STATS_TXBYTE, "TxByte"),
191 MIB_DESC(1, AR8236_STATS_TXCOLLISION, "TxCollision"),
192 MIB_DESC(1, AR8236_STATS_TXABORTCOL, "TxAbortCol"),
193 MIB_DESC(1, AR8236_STATS_TXMULTICOL, "TxMultiCol"),
194 MIB_DESC(1, AR8236_STATS_TXSINGLECOL, "TxSingleCol"),
195 MIB_DESC(1, AR8236_STATS_TXEXCDEFER, "TxExcDefer"),
196 MIB_DESC(1, AR8236_STATS_TXDEFER, "TxDefer"),
197 MIB_DESC(1, AR8236_STATS_TXLATECOL, "TxLateCol"),
198 };
199
200 #define to_ar8216(_dev) container_of(_dev, struct ar8216_priv, dev)
201
202 static inline bool ar8xxx_has_gige(struct ar8216_priv *priv)
203 {
204 return priv->chip->caps & AR8XXX_CAP_GIGE;
205 }
206
207 static inline bool ar8xxx_has_mib_counters(struct ar8216_priv *priv)
208 {
209 return priv->chip->caps & AR8XXX_CAP_MIB_COUNTERS;
210 }
211
212 static inline bool chip_is_ar8216(struct ar8216_priv *priv)
213 {
214 return priv->chip_ver == AR8XXX_VER_AR8216;
215 }
216
217 static inline bool chip_is_ar8236(struct ar8216_priv *priv)
218 {
219 return priv->chip_ver == AR8XXX_VER_AR8236;
220 }
221
222 static inline bool chip_is_ar8316(struct ar8216_priv *priv)
223 {
224 return priv->chip_ver == AR8XXX_VER_AR8316;
225 }
226
227 static inline bool chip_is_ar8327(struct ar8216_priv *priv)
228 {
229 return priv->chip_ver == AR8XXX_VER_AR8327;
230 }
231
232 static inline void
233 split_addr(u32 regaddr, u16 *r1, u16 *r2, u16 *page)
234 {
235 regaddr >>= 1;
236 *r1 = regaddr & 0x1e;
237
238 regaddr >>= 5;
239 *r2 = regaddr & 0x7;
240
241 regaddr >>= 3;
242 *page = regaddr & 0x1ff;
243 }
244
245 static u32
246 ar8216_mii_read(struct ar8216_priv *priv, int reg)
247 {
248 struct phy_device *phy = priv->phy;
249 struct mii_bus *bus = phy->bus;
250 u16 r1, r2, page;
251 u16 lo, hi;
252
253 split_addr((u32) reg, &r1, &r2, &page);
254
255 mutex_lock(&bus->mdio_lock);
256
257 bus->write(bus, 0x18, 0, page);
258 usleep_range(1000, 2000); /* wait for the page switch to propagate */
259 lo = bus->read(bus, 0x10 | r2, r1);
260 hi = bus->read(bus, 0x10 | r2, r1 + 1);
261
262 mutex_unlock(&bus->mdio_lock);
263
264 return (hi << 16) | lo;
265 }
266
267 static void
268 ar8216_mii_write(struct ar8216_priv *priv, int reg, u32 val)
269 {
270 struct phy_device *phy = priv->phy;
271 struct mii_bus *bus = phy->bus;
272 u16 r1, r2, r3;
273 u16 lo, hi;
274
275 split_addr((u32) reg, &r1, &r2, &r3);
276 lo = val & 0xffff;
277 hi = (u16) (val >> 16);
278
279 mutex_lock(&bus->mdio_lock);
280
281 bus->write(bus, 0x18, 0, r3);
282 usleep_range(1000, 2000); /* wait for the page switch to propagate */
283 if (priv->mii_lo_first) {
284 bus->write(bus, 0x10 | r2, r1, lo);
285 bus->write(bus, 0x10 | r2, r1 + 1, hi);
286 } else {
287 bus->write(bus, 0x10 | r2, r1 + 1, hi);
288 bus->write(bus, 0x10 | r2, r1, lo);
289 }
290
291 mutex_unlock(&bus->mdio_lock);
292 }
293
294 static void
295 ar8216_phy_dbg_write(struct ar8216_priv *priv, int phy_addr,
296 u16 dbg_addr, u16 dbg_data)
297 {
298 struct mii_bus *bus = priv->phy->bus;
299
300 mutex_lock(&bus->mdio_lock);
301 bus->write(bus, phy_addr, MII_ATH_DBG_ADDR, dbg_addr);
302 bus->write(bus, phy_addr, MII_ATH_DBG_DATA, dbg_data);
303 mutex_unlock(&bus->mdio_lock);
304 }
305
306 static void
307 ar8216_phy_mmd_write(struct ar8216_priv *priv, int phy_addr, u16 addr, u16 data)
308 {
309 struct mii_bus *bus = priv->phy->bus;
310
311 mutex_lock(&bus->mdio_lock);
312 bus->write(bus, phy_addr, MII_ATH_MMD_ADDR, addr);
313 bus->write(bus, phy_addr, MII_ATH_MMD_DATA, data);
314 mutex_unlock(&bus->mdio_lock);
315 }
316
317 static u32
318 ar8216_rmw(struct ar8216_priv *priv, int reg, u32 mask, u32 val)
319 {
320 u32 v;
321
322 lockdep_assert_held(&priv->reg_mutex);
323
324 v = priv->read(priv, reg);
325 v &= ~mask;
326 v |= val;
327 priv->write(priv, reg, v);
328
329 return v;
330 }
331
332 static inline void
333 ar8216_reg_set(struct ar8216_priv *priv, int reg, u32 val)
334 {
335 u32 v;
336
337 lockdep_assert_held(&priv->reg_mutex);
338
339 v = priv->read(priv, reg);
340 v |= val;
341 priv->write(priv, reg, v);
342 }
343
344 static int
345 ar8216_reg_wait(struct ar8216_priv *priv, u32 reg, u32 mask, u32 val,
346 unsigned timeout)
347 {
348 int i;
349
350 for (i = 0; i < timeout; i++) {
351 u32 t;
352
353 t = priv->read(priv, reg);
354 if ((t & mask) == val)
355 return 0;
356
357 usleep_range(1000, 2000);
358 }
359
360 return -ETIMEDOUT;
361 }
362
363 static int
364 ar8216_mib_capture(struct ar8216_priv *priv)
365 {
366 unsigned mib_func;
367 int ret;
368
369 lockdep_assert_held(&priv->mib_lock);
370
371 if (chip_is_ar8327(priv))
372 mib_func = AR8327_REG_MIB_FUNC;
373 else
374 mib_func = AR8216_REG_MIB_FUNC;
375
376 mutex_lock(&priv->reg_mutex);
377 /* Capture the hardware statistics for all ports */
378 ar8216_rmw(priv, mib_func, AR8216_MIB_FUNC,
379 (AR8216_MIB_FUNC_CAPTURE << AR8216_MIB_FUNC_S));
380 mutex_unlock(&priv->reg_mutex);
381
382 /* Wait for the capturing to complete. */
383 ret = ar8216_reg_wait(priv, mib_func, AR8216_MIB_BUSY, 0, 10);
384 if (ret)
385 goto out;
386
387 ret = 0;
388
389 out:
390 return ret;
391 }
392
393 static int
394 ar8216_mib_flush(struct ar8216_priv *priv)
395 {
396 unsigned mib_func;
397 int ret;
398
399 lockdep_assert_held(&priv->mib_lock);
400
401 if (chip_is_ar8327(priv))
402 mib_func = AR8327_REG_MIB_FUNC;
403 else
404 mib_func = AR8216_REG_MIB_FUNC;
405
406 /* Flush hardware statistics for all ports */
407 ar8216_rmw(priv, mib_func, AR8216_MIB_FUNC,
408 (AR8216_MIB_FUNC_FLUSH << AR8216_MIB_FUNC_S));
409
410 /* Wait for the capturing to complete. */
411 ret = ar8216_reg_wait(priv, mib_func, AR8216_MIB_BUSY, 0, 10);
412 if (ret)
413 goto out;
414
415 ret = 0;
416
417 out:
418 return ret;
419 }
420
421 static void
422 ar8216_mib_fetch_port_stat(struct ar8216_priv *priv, int port, bool flush)
423 {
424 unsigned int base;
425 u64 *mib_stats;
426 int i;
427
428 WARN_ON(port >= priv->dev.ports);
429
430 lockdep_assert_held(&priv->mib_lock);
431
432 if (chip_is_ar8327(priv))
433 base = AR8327_REG_PORT_STATS_BASE(port);
434 else if (chip_is_ar8236(priv) ||
435 chip_is_ar8316(priv))
436 base = AR8236_REG_PORT_STATS_BASE(port);
437 else
438 base = AR8216_REG_PORT_STATS_BASE(port);
439
440 mib_stats = &priv->mib_stats[port * priv->chip->num_mibs];
441 for (i = 0; i < priv->chip->num_mibs; i++) {
442 const struct ar8xxx_mib_desc *mib;
443 u64 t;
444
445 mib = &priv->chip->mib_decs[i];
446 t = priv->read(priv, base + mib->offset);
447 if (mib->size == 2) {
448 u64 hi;
449
450 hi = priv->read(priv, base + mib->offset + 4);
451 t |= hi << 32;
452 }
453
454 if (flush)
455 mib_stats[i] = 0;
456 else
457 mib_stats[i] += t;
458 }
459 }
460
461 static void
462 ar8216_read_port_link(struct ar8216_priv *priv, int port,
463 struct switch_port_link *link)
464 {
465 u32 status;
466 u32 speed;
467
468 memset(link, '\0', sizeof(*link));
469
470 status = priv->chip->read_port_status(priv, port);
471
472 link->aneg = !!(status & AR8216_PORT_STATUS_LINK_AUTO);
473 if (link->aneg) {
474 link->link = !!(status & AR8216_PORT_STATUS_LINK_UP);
475 if (!link->link)
476 return;
477 } else {
478 link->link = true;
479 }
480
481 link->duplex = !!(status & AR8216_PORT_STATUS_DUPLEX);
482 link->tx_flow = !!(status & AR8216_PORT_STATUS_TXFLOW);
483 link->rx_flow = !!(status & AR8216_PORT_STATUS_RXFLOW);
484
485 speed = (status & AR8216_PORT_STATUS_SPEED) >>
486 AR8216_PORT_STATUS_SPEED_S;
487
488 switch (speed) {
489 case AR8216_PORT_SPEED_10M:
490 link->speed = SWITCH_PORT_SPEED_10;
491 break;
492 case AR8216_PORT_SPEED_100M:
493 link->speed = SWITCH_PORT_SPEED_100;
494 break;
495 case AR8216_PORT_SPEED_1000M:
496 link->speed = SWITCH_PORT_SPEED_1000;
497 break;
498 default:
499 link->speed = SWITCH_PORT_SPEED_UNKNOWN;
500 break;
501 }
502 }
503
504 static struct sk_buff *
505 ar8216_mangle_tx(struct net_device *dev, struct sk_buff *skb)
506 {
507 struct ar8216_priv *priv = dev->phy_ptr;
508 unsigned char *buf;
509
510 if (unlikely(!priv))
511 goto error;
512
513 if (!priv->vlan)
514 goto send;
515
516 if (unlikely(skb_headroom(skb) < 2)) {
517 if (pskb_expand_head(skb, 2, 0, GFP_ATOMIC) < 0)
518 goto error;
519 }
520
521 buf = skb_push(skb, 2);
522 buf[0] = 0x10;
523 buf[1] = 0x80;
524
525 send:
526 return skb;
527
528 error:
529 dev_kfree_skb_any(skb);
530 return NULL;
531 }
532
533 static void
534 ar8216_mangle_rx(struct net_device *dev, struct sk_buff *skb)
535 {
536 struct ar8216_priv *priv;
537 unsigned char *buf;
538 int port, vlan;
539
540 priv = dev->phy_ptr;
541 if (!priv)
542 return;
543
544 /* don't strip the header if vlan mode is disabled */
545 if (!priv->vlan)
546 return;
547
548 /* strip header, get vlan id */
549 buf = skb->data;
550 skb_pull(skb, 2);
551
552 /* check for vlan header presence */
553 if ((buf[12 + 2] != 0x81) || (buf[13 + 2] != 0x00))
554 return;
555
556 port = buf[0] & 0xf;
557
558 /* no need to fix up packets coming from a tagged source */
559 if (priv->vlan_tagged & (1 << port))
560 return;
561
562 /* lookup port vid from local table, the switch passes an invalid vlan id */
563 vlan = priv->vlan_id[priv->pvid[port]];
564
565 buf[14 + 2] &= 0xf0;
566 buf[14 + 2] |= vlan >> 8;
567 buf[15 + 2] = vlan & 0xff;
568 }
569
570 static int
571 ar8216_wait_bit(struct ar8216_priv *priv, int reg, u32 mask, u32 val)
572 {
573 int timeout = 20;
574 u32 t = 0;
575
576 while (1) {
577 t = priv->read(priv, reg);
578 if ((t & mask) == val)
579 return 0;
580
581 if (timeout-- <= 0)
582 break;
583
584 udelay(10);
585 }
586
587 pr_err("ar8216: timeout on reg %08x: %08x & %08x != %08x\n",
588 (unsigned int) reg, t, mask, val);
589 return -ETIMEDOUT;
590 }
591
592 static void
593 ar8216_vtu_op(struct ar8216_priv *priv, u32 op, u32 val)
594 {
595 if (ar8216_wait_bit(priv, AR8216_REG_VTU, AR8216_VTU_ACTIVE, 0))
596 return;
597 if ((op & AR8216_VTU_OP) == AR8216_VTU_OP_LOAD) {
598 val &= AR8216_VTUDATA_MEMBER;
599 val |= AR8216_VTUDATA_VALID;
600 priv->write(priv, AR8216_REG_VTU_DATA, val);
601 }
602 op |= AR8216_VTU_ACTIVE;
603 priv->write(priv, AR8216_REG_VTU, op);
604 }
605
606 static void
607 ar8216_vtu_flush(struct ar8216_priv *priv)
608 {
609 ar8216_vtu_op(priv, AR8216_VTU_OP_FLUSH, 0);
610 }
611
612 static void
613 ar8216_vtu_load_vlan(struct ar8216_priv *priv, u32 vid, u32 port_mask)
614 {
615 u32 op;
616
617 op = AR8216_VTU_OP_LOAD | (vid << AR8216_VTU_VID_S);
618 ar8216_vtu_op(priv, op, port_mask);
619 }
620
621 static int
622 ar8216_atu_flush(struct ar8216_priv *priv)
623 {
624 int ret;
625
626 ret = ar8216_wait_bit(priv, AR8216_REG_ATU, AR8216_ATU_ACTIVE, 0);
627 if (!ret)
628 priv->write(priv, AR8216_REG_ATU, AR8216_ATU_OP_FLUSH);
629
630 return ret;
631 }
632
633 static u32
634 ar8216_read_port_status(struct ar8216_priv *priv, int port)
635 {
636 return priv->read(priv, AR8216_REG_PORT_STATUS(port));
637 }
638
639 static void
640 ar8216_setup_port(struct ar8216_priv *priv, int port, u32 egress, u32 ingress,
641 u32 members, u32 pvid)
642 {
643 u32 header;
644
645 if (chip_is_ar8216(priv) && priv->vlan && port == AR8216_PORT_CPU)
646 header = AR8216_PORT_CTRL_HEADER;
647 else
648 header = 0;
649
650 ar8216_rmw(priv, AR8216_REG_PORT_CTRL(port),
651 AR8216_PORT_CTRL_LEARN | AR8216_PORT_CTRL_VLAN_MODE |
652 AR8216_PORT_CTRL_SINGLE_VLAN | AR8216_PORT_CTRL_STATE |
653 AR8216_PORT_CTRL_HEADER | AR8216_PORT_CTRL_LEARN_LOCK,
654 AR8216_PORT_CTRL_LEARN | header |
655 (egress << AR8216_PORT_CTRL_VLAN_MODE_S) |
656 (AR8216_PORT_STATE_FORWARD << AR8216_PORT_CTRL_STATE_S));
657
658 ar8216_rmw(priv, AR8216_REG_PORT_VLAN(port),
659 AR8216_PORT_VLAN_DEST_PORTS | AR8216_PORT_VLAN_MODE |
660 AR8216_PORT_VLAN_DEFAULT_ID,
661 (members << AR8216_PORT_VLAN_DEST_PORTS_S) |
662 (ingress << AR8216_PORT_VLAN_MODE_S) |
663 (pvid << AR8216_PORT_VLAN_DEFAULT_ID_S));
664 }
665
666 static int
667 ar8216_hw_init(struct ar8216_priv *priv)
668 {
669 return 0;
670 }
671
672 static void
673 ar8216_init_globals(struct ar8216_priv *priv)
674 {
675 /* standard atheros magic */
676 priv->write(priv, 0x38, 0xc000050e);
677
678 ar8216_rmw(priv, AR8216_REG_GLOBAL_CTRL,
679 AR8216_GCTRL_MTU, 1518 + 8 + 2);
680 }
681
682 static void
683 ar8216_init_port(struct ar8216_priv *priv, int port)
684 {
685 /* Enable port learning and tx */
686 priv->write(priv, AR8216_REG_PORT_CTRL(port),
687 AR8216_PORT_CTRL_LEARN |
688 (4 << AR8216_PORT_CTRL_STATE_S));
689
690 priv->write(priv, AR8216_REG_PORT_VLAN(port), 0);
691
692 if (port == AR8216_PORT_CPU) {
693 priv->write(priv, AR8216_REG_PORT_STATUS(port),
694 AR8216_PORT_STATUS_LINK_UP |
695 (ar8xxx_has_gige(priv) ?
696 AR8216_PORT_SPEED_1000M : AR8216_PORT_SPEED_100M) |
697 AR8216_PORT_STATUS_TXMAC |
698 AR8216_PORT_STATUS_RXMAC |
699 (chip_is_ar8316(priv) ? AR8216_PORT_STATUS_RXFLOW : 0) |
700 (chip_is_ar8316(priv) ? AR8216_PORT_STATUS_TXFLOW : 0) |
701 AR8216_PORT_STATUS_DUPLEX);
702 } else {
703 priv->write(priv, AR8216_REG_PORT_STATUS(port),
704 AR8216_PORT_STATUS_LINK_AUTO);
705 }
706 }
707
708 static const struct ar8xxx_chip ar8216_chip = {
709 .caps = AR8XXX_CAP_MIB_COUNTERS,
710
711 .hw_init = ar8216_hw_init,
712 .init_globals = ar8216_init_globals,
713 .init_port = ar8216_init_port,
714 .setup_port = ar8216_setup_port,
715 .read_port_status = ar8216_read_port_status,
716 .atu_flush = ar8216_atu_flush,
717 .vtu_flush = ar8216_vtu_flush,
718 .vtu_load_vlan = ar8216_vtu_load_vlan,
719
720 .num_mibs = ARRAY_SIZE(ar8216_mibs),
721 .mib_decs = ar8216_mibs,
722 };
723
724 static void
725 ar8236_setup_port(struct ar8216_priv *priv, int port, u32 egress, u32 ingress,
726 u32 members, u32 pvid)
727 {
728 ar8216_rmw(priv, AR8216_REG_PORT_CTRL(port),
729 AR8216_PORT_CTRL_LEARN | AR8216_PORT_CTRL_VLAN_MODE |
730 AR8216_PORT_CTRL_SINGLE_VLAN | AR8216_PORT_CTRL_STATE |
731 AR8216_PORT_CTRL_HEADER | AR8216_PORT_CTRL_LEARN_LOCK,
732 AR8216_PORT_CTRL_LEARN |
733 (egress << AR8216_PORT_CTRL_VLAN_MODE_S) |
734 (AR8216_PORT_STATE_FORWARD << AR8216_PORT_CTRL_STATE_S));
735
736 ar8216_rmw(priv, AR8236_REG_PORT_VLAN(port),
737 AR8236_PORT_VLAN_DEFAULT_ID,
738 (pvid << AR8236_PORT_VLAN_DEFAULT_ID_S));
739
740 ar8216_rmw(priv, AR8236_REG_PORT_VLAN2(port),
741 AR8236_PORT_VLAN2_VLAN_MODE |
742 AR8236_PORT_VLAN2_MEMBER,
743 (ingress << AR8236_PORT_VLAN2_VLAN_MODE_S) |
744 (members << AR8236_PORT_VLAN2_MEMBER_S));
745 }
746
747 static int
748 ar8236_hw_init(struct ar8216_priv *priv)
749 {
750 int i;
751 struct mii_bus *bus;
752
753 if (priv->initialized)
754 return 0;
755
756 /* Initialize the PHYs */
757 bus = priv->phy->bus;
758 for (i = 0; i < 5; i++) {
759 mdiobus_write(bus, i, MII_ADVERTISE,
760 ADVERTISE_ALL | ADVERTISE_PAUSE_CAP |
761 ADVERTISE_PAUSE_ASYM);
762 mdiobus_write(bus, i, MII_BMCR, BMCR_RESET | BMCR_ANENABLE);
763 }
764 msleep(1000);
765
766 priv->initialized = true;
767 return 0;
768 }
769
770 static void
771 ar8236_init_globals(struct ar8216_priv *priv)
772 {
773 /* enable jumbo frames */
774 ar8216_rmw(priv, AR8216_REG_GLOBAL_CTRL,
775 AR8316_GCTRL_MTU, 9018 + 8 + 2);
776
777 /* Enable MIB counters */
778 ar8216_rmw(priv, AR8216_REG_MIB_FUNC, AR8216_MIB_FUNC | AR8236_MIB_EN,
779 (AR8216_MIB_FUNC_NO_OP << AR8216_MIB_FUNC_S) |
780 AR8236_MIB_EN);
781 }
782
783 static const struct ar8xxx_chip ar8236_chip = {
784 .caps = AR8XXX_CAP_MIB_COUNTERS,
785 .hw_init = ar8236_hw_init,
786 .init_globals = ar8236_init_globals,
787 .init_port = ar8216_init_port,
788 .setup_port = ar8236_setup_port,
789 .read_port_status = ar8216_read_port_status,
790 .atu_flush = ar8216_atu_flush,
791 .vtu_flush = ar8216_vtu_flush,
792 .vtu_load_vlan = ar8216_vtu_load_vlan,
793
794 .num_mibs = ARRAY_SIZE(ar8236_mibs),
795 .mib_decs = ar8236_mibs,
796 };
797
798 static int
799 ar8316_hw_init(struct ar8216_priv *priv)
800 {
801 int i;
802 u32 val, newval;
803 struct mii_bus *bus;
804
805 val = priv->read(priv, 0x8);
806
807 if (priv->phy->interface == PHY_INTERFACE_MODE_RGMII) {
808 if (priv->port4_phy) {
809 /* value taken from Ubiquiti RouterStation Pro */
810 newval = 0x81461bea;
811 printk(KERN_INFO "ar8316: Using port 4 as PHY\n");
812 } else {
813 newval = 0x01261be2;
814 printk(KERN_INFO "ar8316: Using port 4 as switch port\n");
815 }
816 } else if (priv->phy->interface == PHY_INTERFACE_MODE_GMII) {
817 /* value taken from AVM Fritz!Box 7390 sources */
818 newval = 0x010e5b71;
819 } else {
820 /* no known value for phy interface */
821 printk(KERN_ERR "ar8316: unsupported mii mode: %d.\n",
822 priv->phy->interface);
823 return -EINVAL;
824 }
825
826 if (val == newval)
827 goto out;
828
829 priv->write(priv, 0x8, newval);
830
831 /* Initialize the ports */
832 bus = priv->phy->bus;
833 for (i = 0; i < 5; i++) {
834 if ((i == 4) && priv->port4_phy &&
835 priv->phy->interface == PHY_INTERFACE_MODE_RGMII) {
836 /* work around for phy4 rgmii mode */
837 ar8216_phy_dbg_write(priv, i, 0x12, 0x480c);
838 /* rx delay */
839 ar8216_phy_dbg_write(priv, i, 0x0, 0x824e);
840 /* tx delay */
841 ar8216_phy_dbg_write(priv, i, 0x5, 0x3d47);
842 msleep(1000);
843 }
844
845 /* initialize the port itself */
846 mdiobus_write(bus, i, MII_ADVERTISE,
847 ADVERTISE_ALL | ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM);
848 mdiobus_write(bus, i, MII_CTRL1000, ADVERTISE_1000FULL);
849 mdiobus_write(bus, i, MII_BMCR, BMCR_RESET | BMCR_ANENABLE);
850 msleep(1000);
851 }
852
853 out:
854 priv->initialized = true;
855 return 0;
856 }
857
858 static void
859 ar8316_init_globals(struct ar8216_priv *priv)
860 {
861 /* standard atheros magic */
862 priv->write(priv, 0x38, 0xc000050e);
863
864 /* enable cpu port to receive multicast and broadcast frames */
865 priv->write(priv, AR8216_REG_FLOOD_MASK, 0x003f003f);
866
867 /* enable jumbo frames */
868 ar8216_rmw(priv, AR8216_REG_GLOBAL_CTRL,
869 AR8316_GCTRL_MTU, 9018 + 8 + 2);
870
871 /* Enable MIB counters */
872 ar8216_rmw(priv, AR8216_REG_MIB_FUNC, AR8216_MIB_FUNC | AR8236_MIB_EN,
873 (AR8216_MIB_FUNC_NO_OP << AR8216_MIB_FUNC_S) |
874 AR8236_MIB_EN);
875 }
876
877 static const struct ar8xxx_chip ar8316_chip = {
878 .caps = AR8XXX_CAP_GIGE | AR8XXX_CAP_MIB_COUNTERS,
879 .hw_init = ar8316_hw_init,
880 .init_globals = ar8316_init_globals,
881 .init_port = ar8216_init_port,
882 .setup_port = ar8216_setup_port,
883 .read_port_status = ar8216_read_port_status,
884 .atu_flush = ar8216_atu_flush,
885 .vtu_flush = ar8216_vtu_flush,
886 .vtu_load_vlan = ar8216_vtu_load_vlan,
887
888 .num_mibs = ARRAY_SIZE(ar8236_mibs),
889 .mib_decs = ar8236_mibs,
890 };
891
892 static u32
893 ar8327_get_pad_cfg(struct ar8327_pad_cfg *cfg)
894 {
895 u32 t;
896
897 if (!cfg)
898 return 0;
899
900 t = 0;
901 switch (cfg->mode) {
902 case AR8327_PAD_NC:
903 break;
904
905 case AR8327_PAD_MAC2MAC_MII:
906 t = AR8327_PAD_MAC_MII_EN;
907 if (cfg->rxclk_sel)
908 t |= AR8327_PAD_MAC_MII_RXCLK_SEL;
909 if (cfg->txclk_sel)
910 t |= AR8327_PAD_MAC_MII_TXCLK_SEL;
911 break;
912
913 case AR8327_PAD_MAC2MAC_GMII:
914 t = AR8327_PAD_MAC_GMII_EN;
915 if (cfg->rxclk_sel)
916 t |= AR8327_PAD_MAC_GMII_RXCLK_SEL;
917 if (cfg->txclk_sel)
918 t |= AR8327_PAD_MAC_GMII_TXCLK_SEL;
919 break;
920
921 case AR8327_PAD_MAC_SGMII:
922 t = AR8327_PAD_SGMII_EN;
923 break;
924
925 case AR8327_PAD_MAC2PHY_MII:
926 t = AR8327_PAD_PHY_MII_EN;
927 if (cfg->rxclk_sel)
928 t |= AR8327_PAD_PHY_MII_RXCLK_SEL;
929 if (cfg->txclk_sel)
930 t |= AR8327_PAD_PHY_MII_TXCLK_SEL;
931 break;
932
933 case AR8327_PAD_MAC2PHY_GMII:
934 t = AR8327_PAD_PHY_GMII_EN;
935 if (cfg->pipe_rxclk_sel)
936 t |= AR8327_PAD_PHY_GMII_PIPE_RXCLK_SEL;
937 if (cfg->rxclk_sel)
938 t |= AR8327_PAD_PHY_GMII_RXCLK_SEL;
939 if (cfg->txclk_sel)
940 t |= AR8327_PAD_PHY_GMII_TXCLK_SEL;
941 break;
942
943 case AR8327_PAD_MAC_RGMII:
944 t = AR8327_PAD_RGMII_EN;
945 t |= cfg->txclk_delay_sel << AR8327_PAD_RGMII_TXCLK_DELAY_SEL_S;
946 t |= cfg->rxclk_delay_sel << AR8327_PAD_RGMII_RXCLK_DELAY_SEL_S;
947 if (cfg->rxclk_delay_en)
948 t |= AR8327_PAD_RGMII_RXCLK_DELAY_EN;
949 if (cfg->txclk_delay_en)
950 t |= AR8327_PAD_RGMII_TXCLK_DELAY_EN;
951 break;
952
953 case AR8327_PAD_PHY_GMII:
954 t = AR8327_PAD_PHYX_GMII_EN;
955 break;
956
957 case AR8327_PAD_PHY_RGMII:
958 t = AR8327_PAD_PHYX_RGMII_EN;
959 break;
960
961 case AR8327_PAD_PHY_MII:
962 t = AR8327_PAD_PHYX_MII_EN;
963 break;
964 }
965
966 return t;
967 }
968
969 static void
970 ar8327_phy_fixup(struct ar8216_priv *priv, int phy)
971 {
972 switch (priv->chip_rev) {
973 case 1:
974 /* For 100M waveform */
975 ar8216_phy_dbg_write(priv, phy, 0, 0x02ea);
976 /* Turn on Gigabit clock */
977 ar8216_phy_dbg_write(priv, phy, 0x3d, 0x68a0);
978 break;
979
980 case 2:
981 ar8216_phy_mmd_write(priv, phy, 0x7, 0x3c);
982 ar8216_phy_mmd_write(priv, phy, 0x4007, 0x0);
983 /* fallthrough */
984 case 4:
985 ar8216_phy_mmd_write(priv, phy, 0x3, 0x800d);
986 ar8216_phy_mmd_write(priv, phy, 0x4003, 0x803f);
987
988 ar8216_phy_dbg_write(priv, phy, 0x3d, 0x6860);
989 ar8216_phy_dbg_write(priv, phy, 0x5, 0x2c46);
990 ar8216_phy_dbg_write(priv, phy, 0x3c, 0x6000);
991 break;
992 }
993 }
994
995 static int
996 ar8327_hw_init(struct ar8216_priv *priv)
997 {
998 struct ar8327_platform_data *pdata;
999 struct ar8327_led_cfg *led_cfg;
1000 struct mii_bus *bus;
1001 u32 pos, new_pos;
1002 u32 t;
1003 int i;
1004
1005 pdata = priv->phy->dev.platform_data;
1006 if (!pdata)
1007 return -EINVAL;
1008
1009 t = ar8327_get_pad_cfg(pdata->pad0_cfg);
1010 priv->write(priv, AR8327_REG_PAD0_MODE, t);
1011 t = ar8327_get_pad_cfg(pdata->pad5_cfg);
1012 priv->write(priv, AR8327_REG_PAD5_MODE, t);
1013 t = ar8327_get_pad_cfg(pdata->pad6_cfg);
1014 priv->write(priv, AR8327_REG_PAD6_MODE, t);
1015
1016 pos = priv->read(priv, AR8327_REG_POWER_ON_STRIP);
1017 new_pos = pos;
1018
1019 led_cfg = pdata->led_cfg;
1020 if (led_cfg) {
1021 if (led_cfg->open_drain)
1022 new_pos |= AR8327_POWER_ON_STRIP_LED_OPEN_EN;
1023 else
1024 new_pos &= ~AR8327_POWER_ON_STRIP_LED_OPEN_EN;
1025
1026 priv->write(priv, AR8327_REG_LED_CTRL0, led_cfg->led_ctrl0);
1027 priv->write(priv, AR8327_REG_LED_CTRL1, led_cfg->led_ctrl1);
1028 priv->write(priv, AR8327_REG_LED_CTRL2, led_cfg->led_ctrl2);
1029 priv->write(priv, AR8327_REG_LED_CTRL3, led_cfg->led_ctrl3);
1030 }
1031
1032 if (new_pos != pos) {
1033 new_pos |= AR8327_POWER_ON_STRIP_POWER_ON_SEL;
1034 priv->write(priv, AR8327_REG_POWER_ON_STRIP, new_pos);
1035 }
1036
1037 bus = priv->phy->bus;
1038 for (i = 0; i < AR8327_NUM_PHYS; i++) {
1039 ar8327_phy_fixup(priv, i);
1040
1041 /* start aneg on the PHY */
1042 mdiobus_write(bus, i, MII_ADVERTISE, ADVERTISE_ALL |
1043 ADVERTISE_PAUSE_CAP |
1044 ADVERTISE_PAUSE_ASYM);
1045 mdiobus_write(bus, i, MII_CTRL1000, ADVERTISE_1000FULL);
1046 mdiobus_write(bus, i, MII_BMCR, BMCR_RESET | BMCR_ANENABLE);
1047 }
1048
1049 msleep(1000);
1050
1051 return 0;
1052 }
1053
1054 static void
1055 ar8327_init_globals(struct ar8216_priv *priv)
1056 {
1057 u32 t;
1058
1059 /* enable CPU port and disable mirror port */
1060 t = AR8327_FWD_CTRL0_CPU_PORT_EN |
1061 AR8327_FWD_CTRL0_MIRROR_PORT;
1062 priv->write(priv, AR8327_REG_FWD_CTRL0, t);
1063
1064 /* forward multicast and broadcast frames to CPU */
1065 t = (AR8327_PORTS_ALL << AR8327_FWD_CTRL1_UC_FLOOD_S) |
1066 (AR8327_PORTS_ALL << AR8327_FWD_CTRL1_MC_FLOOD_S) |
1067 (AR8327_PORTS_ALL << AR8327_FWD_CTRL1_BC_FLOOD_S);
1068 priv->write(priv, AR8327_REG_FWD_CTRL1, t);
1069
1070 /* setup MTU */
1071 ar8216_rmw(priv, AR8327_REG_MAX_FRAME_SIZE,
1072 AR8327_MAX_FRAME_SIZE_MTU, 1518 + 8 + 2);
1073
1074 /* Enable MIB counters */
1075 ar8216_reg_set(priv, AR8327_REG_MODULE_EN,
1076 AR8327_MODULE_EN_MIB);
1077 }
1078
1079 static void
1080 ar8327_init_cpuport(struct ar8216_priv *priv)
1081 {
1082 struct ar8327_platform_data *pdata;
1083 struct ar8327_port_cfg *cfg;
1084 u32 t;
1085
1086 pdata = priv->phy->dev.platform_data;
1087 if (!pdata)
1088 return;
1089
1090 cfg = &pdata->cpuport_cfg;
1091 if (!cfg->force_link) {
1092 priv->write(priv, AR8327_REG_PORT_STATUS(AR8216_PORT_CPU),
1093 AR8216_PORT_STATUS_LINK_AUTO);
1094 return;
1095 }
1096
1097 t = AR8216_PORT_STATUS_TXMAC | AR8216_PORT_STATUS_RXMAC;
1098 t |= cfg->duplex ? AR8216_PORT_STATUS_DUPLEX : 0;
1099 t |= cfg->rxpause ? AR8216_PORT_STATUS_RXFLOW : 0;
1100 t |= cfg->txpause ? AR8216_PORT_STATUS_TXFLOW : 0;
1101 switch (cfg->speed) {
1102 case AR8327_PORT_SPEED_10:
1103 t |= AR8216_PORT_SPEED_10M;
1104 break;
1105 case AR8327_PORT_SPEED_100:
1106 t |= AR8216_PORT_SPEED_100M;
1107 break;
1108 case AR8327_PORT_SPEED_1000:
1109 t |= AR8216_PORT_SPEED_1000M;
1110 break;
1111 }
1112
1113 priv->write(priv, AR8327_REG_PORT_STATUS(AR8216_PORT_CPU), t);
1114 }
1115
1116 static void
1117 ar8327_init_port(struct ar8216_priv *priv, int port)
1118 {
1119 u32 t;
1120
1121 if (port == AR8216_PORT_CPU) {
1122 ar8327_init_cpuport(priv);
1123 } else {
1124 t = AR8216_PORT_STATUS_LINK_AUTO;
1125 priv->write(priv, AR8327_REG_PORT_STATUS(port), t);
1126 }
1127
1128 priv->write(priv, AR8327_REG_PORT_HEADER(port), 0);
1129
1130 priv->write(priv, AR8327_REG_PORT_VLAN0(port), 0);
1131
1132 t = AR8327_PORT_VLAN1_OUT_MODE_UNTOUCH << AR8327_PORT_VLAN1_OUT_MODE_S;
1133 priv->write(priv, AR8327_REG_PORT_VLAN1(port), t);
1134
1135 t = AR8327_PORT_LOOKUP_LEARN;
1136 t |= AR8216_PORT_STATE_FORWARD << AR8327_PORT_LOOKUP_STATE_S;
1137 priv->write(priv, AR8327_REG_PORT_LOOKUP(port), t);
1138 }
1139
1140 static u32
1141 ar8327_read_port_status(struct ar8216_priv *priv, int port)
1142 {
1143 return priv->read(priv, AR8327_REG_PORT_STATUS(port));
1144 }
1145
1146 static int
1147 ar8327_atu_flush(struct ar8216_priv *priv)
1148 {
1149 int ret;
1150
1151 ret = ar8216_wait_bit(priv, AR8327_REG_ATU_FUNC,
1152 AR8327_ATU_FUNC_BUSY, 0);
1153 if (!ret)
1154 priv->write(priv, AR8327_REG_ATU_FUNC,
1155 AR8327_ATU_FUNC_OP_FLUSH);
1156
1157 return ret;
1158 }
1159
1160 static void
1161 ar8327_vtu_op(struct ar8216_priv *priv, u32 op, u32 val)
1162 {
1163 if (ar8216_wait_bit(priv, AR8327_REG_VTU_FUNC1,
1164 AR8327_VTU_FUNC1_BUSY, 0))
1165 return;
1166
1167 if ((op & AR8327_VTU_FUNC1_OP) == AR8327_VTU_FUNC1_OP_LOAD)
1168 priv->write(priv, AR8327_REG_VTU_FUNC0, val);
1169
1170 op |= AR8327_VTU_FUNC1_BUSY;
1171 priv->write(priv, AR8327_REG_VTU_FUNC1, op);
1172 }
1173
1174 static void
1175 ar8327_vtu_flush(struct ar8216_priv *priv)
1176 {
1177 ar8327_vtu_op(priv, AR8327_VTU_FUNC1_OP_FLUSH, 0);
1178 }
1179
1180 static void
1181 ar8327_vtu_load_vlan(struct ar8216_priv *priv, u32 vid, u32 port_mask)
1182 {
1183 u32 op;
1184 u32 val;
1185 int i;
1186
1187 op = AR8327_VTU_FUNC1_OP_LOAD | (vid << AR8327_VTU_FUNC1_VID_S);
1188 val = AR8327_VTU_FUNC0_VALID | AR8327_VTU_FUNC0_IVL;
1189 for (i = 0; i < AR8327_NUM_PORTS; i++) {
1190 u32 mode;
1191
1192 if ((port_mask & BIT(i)) == 0)
1193 mode = AR8327_VTU_FUNC0_EG_MODE_NOT;
1194 else if (priv->vlan == 0)
1195 mode = AR8327_VTU_FUNC0_EG_MODE_KEEP;
1196 else if (priv->vlan_tagged & BIT(i))
1197 mode = AR8327_VTU_FUNC0_EG_MODE_TAG;
1198 else
1199 mode = AR8327_VTU_FUNC0_EG_MODE_UNTAG;
1200
1201 val |= mode << AR8327_VTU_FUNC0_EG_MODE_S(i);
1202 }
1203 ar8327_vtu_op(priv, op, val);
1204 }
1205
1206 static void
1207 ar8327_setup_port(struct ar8216_priv *priv, int port, u32 egress, u32 ingress,
1208 u32 members, u32 pvid)
1209 {
1210 u32 t;
1211 u32 mode;
1212
1213 t = pvid << AR8327_PORT_VLAN0_DEF_SVID_S;
1214 t |= pvid << AR8327_PORT_VLAN0_DEF_CVID_S;
1215 priv->write(priv, AR8327_REG_PORT_VLAN0(port), t);
1216
1217 mode = AR8327_PORT_VLAN1_OUT_MODE_UNMOD;
1218 switch (egress) {
1219 case AR8216_OUT_KEEP:
1220 mode = AR8327_PORT_VLAN1_OUT_MODE_UNTOUCH;
1221 break;
1222 case AR8216_OUT_STRIP_VLAN:
1223 mode = AR8327_PORT_VLAN1_OUT_MODE_UNTAG;
1224 break;
1225 case AR8216_OUT_ADD_VLAN:
1226 mode = AR8327_PORT_VLAN1_OUT_MODE_TAG;
1227 break;
1228 }
1229
1230 t = AR8327_PORT_VLAN1_PORT_VLAN_PROP;
1231 t |= mode << AR8327_PORT_VLAN1_OUT_MODE_S;
1232 priv->write(priv, AR8327_REG_PORT_VLAN1(port), t);
1233
1234 t = members;
1235 t |= AR8327_PORT_LOOKUP_LEARN;
1236 t |= ingress << AR8327_PORT_LOOKUP_IN_MODE_S;
1237 t |= AR8216_PORT_STATE_FORWARD << AR8327_PORT_LOOKUP_STATE_S;
1238 priv->write(priv, AR8327_REG_PORT_LOOKUP(port), t);
1239 }
1240
1241 static const struct ar8xxx_chip ar8327_chip = {
1242 .caps = AR8XXX_CAP_GIGE | AR8XXX_CAP_MIB_COUNTERS,
1243 .hw_init = ar8327_hw_init,
1244 .init_globals = ar8327_init_globals,
1245 .init_port = ar8327_init_port,
1246 .setup_port = ar8327_setup_port,
1247 .read_port_status = ar8327_read_port_status,
1248 .atu_flush = ar8327_atu_flush,
1249 .vtu_flush = ar8327_vtu_flush,
1250 .vtu_load_vlan = ar8327_vtu_load_vlan,
1251
1252 .num_mibs = ARRAY_SIZE(ar8236_mibs),
1253 .mib_decs = ar8236_mibs,
1254 };
1255
1256 static int
1257 ar8216_sw_set_vlan(struct switch_dev *dev, const struct switch_attr *attr,
1258 struct switch_val *val)
1259 {
1260 struct ar8216_priv *priv = to_ar8216(dev);
1261 priv->vlan = !!val->value.i;
1262 return 0;
1263 }
1264
1265 static int
1266 ar8216_sw_get_vlan(struct switch_dev *dev, const struct switch_attr *attr,
1267 struct switch_val *val)
1268 {
1269 struct ar8216_priv *priv = to_ar8216(dev);
1270 val->value.i = priv->vlan;
1271 return 0;
1272 }
1273
1274
1275 static int
1276 ar8216_sw_set_pvid(struct switch_dev *dev, int port, int vlan)
1277 {
1278 struct ar8216_priv *priv = to_ar8216(dev);
1279
1280 /* make sure no invalid PVIDs get set */
1281
1282 if (vlan >= dev->vlans)
1283 return -EINVAL;
1284
1285 priv->pvid[port] = vlan;
1286 return 0;
1287 }
1288
1289 static int
1290 ar8216_sw_get_pvid(struct switch_dev *dev, int port, int *vlan)
1291 {
1292 struct ar8216_priv *priv = to_ar8216(dev);
1293 *vlan = priv->pvid[port];
1294 return 0;
1295 }
1296
1297 static int
1298 ar8216_sw_set_vid(struct switch_dev *dev, const struct switch_attr *attr,
1299 struct switch_val *val)
1300 {
1301 struct ar8216_priv *priv = to_ar8216(dev);
1302 priv->vlan_id[val->port_vlan] = val->value.i;
1303 return 0;
1304 }
1305
1306 static int
1307 ar8216_sw_get_vid(struct switch_dev *dev, const struct switch_attr *attr,
1308 struct switch_val *val)
1309 {
1310 struct ar8216_priv *priv = to_ar8216(dev);
1311 val->value.i = priv->vlan_id[val->port_vlan];
1312 return 0;
1313 }
1314
1315 static int
1316 ar8216_sw_get_port_link(struct switch_dev *dev, int port,
1317 struct switch_port_link *link)
1318 {
1319 struct ar8216_priv *priv = to_ar8216(dev);
1320
1321 ar8216_read_port_link(priv, port, link);
1322 return 0;
1323 }
1324
1325 static int
1326 ar8216_sw_get_ports(struct switch_dev *dev, struct switch_val *val)
1327 {
1328 struct ar8216_priv *priv = to_ar8216(dev);
1329 u8 ports = priv->vlan_table[val->port_vlan];
1330 int i;
1331
1332 val->len = 0;
1333 for (i = 0; i < dev->ports; i++) {
1334 struct switch_port *p;
1335
1336 if (!(ports & (1 << i)))
1337 continue;
1338
1339 p = &val->value.ports[val->len++];
1340 p->id = i;
1341 if (priv->vlan_tagged & (1 << i))
1342 p->flags = (1 << SWITCH_PORT_FLAG_TAGGED);
1343 else
1344 p->flags = 0;
1345 }
1346 return 0;
1347 }
1348
1349 static int
1350 ar8216_sw_set_ports(struct switch_dev *dev, struct switch_val *val)
1351 {
1352 struct ar8216_priv *priv = to_ar8216(dev);
1353 u8 *vt = &priv->vlan_table[val->port_vlan];
1354 int i, j;
1355
1356 *vt = 0;
1357 for (i = 0; i < val->len; i++) {
1358 struct switch_port *p = &val->value.ports[i];
1359
1360 if (p->flags & (1 << SWITCH_PORT_FLAG_TAGGED)) {
1361 priv->vlan_tagged |= (1 << p->id);
1362 } else {
1363 priv->vlan_tagged &= ~(1 << p->id);
1364 priv->pvid[p->id] = val->port_vlan;
1365
1366 /* make sure that an untagged port does not
1367 * appear in other vlans */
1368 for (j = 0; j < AR8X16_MAX_VLANS; j++) {
1369 if (j == val->port_vlan)
1370 continue;
1371 priv->vlan_table[j] &= ~(1 << p->id);
1372 }
1373 }
1374
1375 *vt |= 1 << p->id;
1376 }
1377 return 0;
1378 }
1379
1380 static int
1381 ar8216_sw_hw_apply(struct switch_dev *dev)
1382 {
1383 struct ar8216_priv *priv = to_ar8216(dev);
1384 u8 portmask[AR8X16_MAX_PORTS];
1385 int i, j;
1386
1387 mutex_lock(&priv->reg_mutex);
1388 /* flush all vlan translation unit entries */
1389 priv->chip->vtu_flush(priv);
1390
1391 memset(portmask, 0, sizeof(portmask));
1392 if (!priv->init) {
1393 /* calculate the port destination masks and load vlans
1394 * into the vlan translation unit */
1395 for (j = 0; j < AR8X16_MAX_VLANS; j++) {
1396 u8 vp = priv->vlan_table[j];
1397
1398 if (!vp)
1399 continue;
1400
1401 for (i = 0; i < dev->ports; i++) {
1402 u8 mask = (1 << i);
1403 if (vp & mask)
1404 portmask[i] |= vp & ~mask;
1405 }
1406
1407 priv->chip->vtu_load_vlan(priv, priv->vlan_id[j],
1408 priv->vlan_table[j]);
1409 }
1410 } else {
1411 /* vlan disabled:
1412 * isolate all ports, but connect them to the cpu port */
1413 for (i = 0; i < dev->ports; i++) {
1414 if (i == AR8216_PORT_CPU)
1415 continue;
1416
1417 portmask[i] = 1 << AR8216_PORT_CPU;
1418 portmask[AR8216_PORT_CPU] |= (1 << i);
1419 }
1420 }
1421
1422 /* update the port destination mask registers and tag settings */
1423 for (i = 0; i < dev->ports; i++) {
1424 int egress, ingress;
1425 int pvid;
1426
1427 if (priv->vlan) {
1428 pvid = priv->vlan_id[priv->pvid[i]];
1429 if (priv->vlan_tagged & (1 << i))
1430 egress = AR8216_OUT_ADD_VLAN;
1431 else
1432 egress = AR8216_OUT_STRIP_VLAN;
1433 ingress = AR8216_IN_SECURE;
1434 } else {
1435 pvid = i;
1436 egress = AR8216_OUT_KEEP;
1437 ingress = AR8216_IN_PORT_ONLY;
1438 }
1439
1440 priv->chip->setup_port(priv, i, egress, ingress, portmask[i],
1441 pvid);
1442 }
1443 mutex_unlock(&priv->reg_mutex);
1444 return 0;
1445 }
1446
1447 static int
1448 ar8216_sw_reset_switch(struct switch_dev *dev)
1449 {
1450 struct ar8216_priv *priv = to_ar8216(dev);
1451 int i;
1452
1453 mutex_lock(&priv->reg_mutex);
1454 memset(&priv->vlan, 0, sizeof(struct ar8216_priv) -
1455 offsetof(struct ar8216_priv, vlan));
1456
1457 for (i = 0; i < AR8X16_MAX_VLANS; i++)
1458 priv->vlan_id[i] = i;
1459
1460 /* Configure all ports */
1461 for (i = 0; i < dev->ports; i++)
1462 priv->chip->init_port(priv, i);
1463
1464 priv->chip->init_globals(priv);
1465 mutex_unlock(&priv->reg_mutex);
1466
1467 return ar8216_sw_hw_apply(dev);
1468 }
1469
1470 static int
1471 ar8216_sw_set_reset_mibs(struct switch_dev *dev,
1472 const struct switch_attr *attr,
1473 struct switch_val *val)
1474 {
1475 struct ar8216_priv *priv = to_ar8216(dev);
1476 unsigned int len;
1477 int ret;
1478
1479 if (!ar8xxx_has_mib_counters(priv))
1480 return -EOPNOTSUPP;
1481
1482 mutex_lock(&priv->mib_lock);
1483
1484 len = priv->dev.ports * priv->chip->num_mibs *
1485 sizeof(*priv->mib_stats);
1486 memset(priv->mib_stats, '\0', len);
1487 ret = ar8216_mib_flush(priv);
1488 if (ret)
1489 goto unlock;
1490
1491 ret = 0;
1492
1493 unlock:
1494 mutex_unlock(&priv->mib_lock);
1495 return ret;
1496 }
1497
1498 static int
1499 ar8216_sw_set_port_reset_mib(struct switch_dev *dev,
1500 const struct switch_attr *attr,
1501 struct switch_val *val)
1502 {
1503 struct ar8216_priv *priv = to_ar8216(dev);
1504 int port;
1505 int ret;
1506
1507 if (!ar8xxx_has_mib_counters(priv))
1508 return -EOPNOTSUPP;
1509
1510 port = val->port_vlan;
1511 if (port >= dev->ports)
1512 return -EINVAL;
1513
1514 mutex_lock(&priv->mib_lock);
1515 ret = ar8216_mib_capture(priv);
1516 if (ret)
1517 goto unlock;
1518
1519 ar8216_mib_fetch_port_stat(priv, port, true);
1520
1521 ret = 0;
1522
1523 unlock:
1524 mutex_unlock(&priv->mib_lock);
1525 return ret;
1526 }
1527
1528 static int
1529 ar8216_sw_get_port_mib(struct switch_dev *dev,
1530 const struct switch_attr *attr,
1531 struct switch_val *val)
1532 {
1533 struct ar8216_priv *priv = to_ar8216(dev);
1534 const struct ar8xxx_chip *chip = priv->chip;
1535 u64 *mib_stats;
1536 int port;
1537 int ret;
1538 char *buf = priv->buf;
1539 int i, len = 0;
1540
1541 if (!ar8xxx_has_mib_counters(priv))
1542 return -EOPNOTSUPP;
1543
1544 port = val->port_vlan;
1545 if (port >= dev->ports)
1546 return -EINVAL;
1547
1548 mutex_lock(&priv->mib_lock);
1549 ret = ar8216_mib_capture(priv);
1550 if (ret)
1551 goto unlock;
1552
1553 ar8216_mib_fetch_port_stat(priv, port, false);
1554
1555 len += snprintf(buf + len, sizeof(priv->buf) - len,
1556 "Port %d MIB counters\n",
1557 port);
1558
1559 mib_stats = &priv->mib_stats[port * chip->num_mibs];
1560 for (i = 0; i < chip->num_mibs; i++)
1561 len += snprintf(buf + len, sizeof(priv->buf) - len,
1562 "%-12s: %llu\n",
1563 chip->mib_decs[i].name,
1564 mib_stats[i]);
1565
1566 val->value.s = buf;
1567 val->len = len;
1568
1569 ret = 0;
1570
1571 unlock:
1572 mutex_unlock(&priv->mib_lock);
1573 return ret;
1574 }
1575
1576 static struct switch_attr ar8216_globals[] = {
1577 {
1578 .type = SWITCH_TYPE_INT,
1579 .name = "enable_vlan",
1580 .description = "Enable VLAN mode",
1581 .set = ar8216_sw_set_vlan,
1582 .get = ar8216_sw_get_vlan,
1583 .max = 1
1584 },
1585 {
1586 .type = SWITCH_TYPE_NOVAL,
1587 .name = "reset_mibs",
1588 .description = "Reset all MIB counters",
1589 .set = ar8216_sw_set_reset_mibs,
1590 },
1591
1592 };
1593
1594 static struct switch_attr ar8216_port[] = {
1595 {
1596 .type = SWITCH_TYPE_NOVAL,
1597 .name = "reset_mib",
1598 .description = "Reset single port MIB counters",
1599 .set = ar8216_sw_set_port_reset_mib,
1600 },
1601 {
1602 .type = SWITCH_TYPE_STRING,
1603 .name = "mib",
1604 .description = "Get port's MIB counters",
1605 .set = NULL,
1606 .get = ar8216_sw_get_port_mib,
1607 },
1608 };
1609
1610 static struct switch_attr ar8216_vlan[] = {
1611 {
1612 .type = SWITCH_TYPE_INT,
1613 .name = "vid",
1614 .description = "VLAN ID (0-4094)",
1615 .set = ar8216_sw_set_vid,
1616 .get = ar8216_sw_get_vid,
1617 .max = 4094,
1618 },
1619 };
1620
1621 static const struct switch_dev_ops ar8216_sw_ops = {
1622 .attr_global = {
1623 .attr = ar8216_globals,
1624 .n_attr = ARRAY_SIZE(ar8216_globals),
1625 },
1626 .attr_port = {
1627 .attr = ar8216_port,
1628 .n_attr = ARRAY_SIZE(ar8216_port),
1629 },
1630 .attr_vlan = {
1631 .attr = ar8216_vlan,
1632 .n_attr = ARRAY_SIZE(ar8216_vlan),
1633 },
1634 .get_port_pvid = ar8216_sw_get_pvid,
1635 .set_port_pvid = ar8216_sw_set_pvid,
1636 .get_vlan_ports = ar8216_sw_get_ports,
1637 .set_vlan_ports = ar8216_sw_set_ports,
1638 .apply_config = ar8216_sw_hw_apply,
1639 .reset_switch = ar8216_sw_reset_switch,
1640 .get_port_link = ar8216_sw_get_port_link,
1641 };
1642
1643 static int
1644 ar8216_id_chip(struct ar8216_priv *priv)
1645 {
1646 u32 val;
1647 u16 id;
1648 int i;
1649
1650 val = ar8216_mii_read(priv, AR8216_REG_CTRL);
1651 if (val == ~0)
1652 return -ENODEV;
1653
1654 id = val & (AR8216_CTRL_REVISION | AR8216_CTRL_VERSION);
1655 for (i = 0; i < AR8X16_PROBE_RETRIES; i++) {
1656 u16 t;
1657
1658 val = ar8216_mii_read(priv, AR8216_REG_CTRL);
1659 if (val == ~0)
1660 return -ENODEV;
1661
1662 t = val & (AR8216_CTRL_REVISION | AR8216_CTRL_VERSION);
1663 if (t != id)
1664 return -ENODEV;
1665 }
1666
1667 priv->chip_ver = (id & AR8216_CTRL_VERSION) >> AR8216_CTRL_VERSION_S;
1668 priv->chip_rev = (id & AR8216_CTRL_REVISION);
1669
1670 switch (priv->chip_ver) {
1671 case AR8XXX_VER_AR8216:
1672 priv->chip = &ar8216_chip;
1673 break;
1674 case AR8XXX_VER_AR8236:
1675 priv->chip = &ar8236_chip;
1676 break;
1677 case AR8XXX_VER_AR8316:
1678 priv->chip = &ar8316_chip;
1679 break;
1680 case AR8XXX_VER_AR8327:
1681 priv->mii_lo_first = true;
1682 priv->chip = &ar8327_chip;
1683 break;
1684 default:
1685 printk(KERN_DEBUG
1686 "ar8216: Unknown Atheros device [ver=%d, rev=%d, phy_id=%04x%04x]\n",
1687 priv->chip_ver, priv->chip_rev,
1688 mdiobus_read(priv->phy->bus, priv->phy->addr, 2),
1689 mdiobus_read(priv->phy->bus, priv->phy->addr, 3));
1690
1691 return -ENODEV;
1692 }
1693
1694 return 0;
1695 }
1696
1697 static void
1698 ar8xxx_mib_work_func(struct work_struct *work)
1699 {
1700 struct ar8216_priv *priv;
1701 int err;
1702
1703 priv = container_of(work, struct ar8216_priv, mib_work.work);
1704
1705 mutex_lock(&priv->mib_lock);
1706
1707 err = ar8216_mib_capture(priv);
1708 if (err)
1709 goto next_port;
1710
1711 ar8216_mib_fetch_port_stat(priv, priv->mib_next_port, false);
1712
1713 next_port:
1714 priv->mib_next_port++;
1715 if (priv->mib_next_port >= priv->dev.ports)
1716 priv->mib_next_port = 0;
1717
1718 mutex_unlock(&priv->mib_lock);
1719 schedule_delayed_work(&priv->mib_work,
1720 msecs_to_jiffies(AR8XXX_MIB_WORK_DELAY));
1721 }
1722
1723 static int
1724 ar8xxx_mib_init(struct ar8216_priv *priv)
1725 {
1726 unsigned int len;
1727
1728 if (!ar8xxx_has_mib_counters(priv))
1729 return 0;
1730
1731 BUG_ON(!priv->chip->mib_decs || !priv->chip->num_mibs);
1732
1733 len = priv->dev.ports * priv->chip->num_mibs *
1734 sizeof(*priv->mib_stats);
1735 priv->mib_stats = kzalloc(len, GFP_KERNEL);
1736
1737 if (!priv->mib_stats)
1738 return -ENOMEM;
1739
1740 mutex_init(&priv->mib_lock);
1741 INIT_DELAYED_WORK(&priv->mib_work, ar8xxx_mib_work_func);
1742
1743 return 0;
1744 }
1745
1746 static void
1747 ar8xxx_mib_start(struct ar8216_priv *priv)
1748 {
1749 if (!ar8xxx_has_mib_counters(priv))
1750 return;
1751
1752 schedule_delayed_work(&priv->mib_work,
1753 msecs_to_jiffies(AR8XXX_MIB_WORK_DELAY));
1754 }
1755
1756 static void
1757 ar8xxx_mib_cleanup(struct ar8216_priv *priv)
1758 {
1759 if (!ar8xxx_has_mib_counters(priv))
1760 return;
1761
1762 cancel_delayed_work(&priv->mib_work);
1763 kfree(priv->mib_stats);
1764 }
1765
1766 static int
1767 ar8216_config_init(struct phy_device *pdev)
1768 {
1769 struct ar8216_priv *priv = pdev->priv;
1770 struct net_device *dev = pdev->attached_dev;
1771 struct switch_dev *swdev;
1772 int ret;
1773
1774 if (!priv) {
1775 priv = kzalloc(sizeof(struct ar8216_priv), GFP_KERNEL);
1776 if (priv == NULL)
1777 return -ENOMEM;
1778 }
1779
1780 priv->phy = pdev;
1781
1782 ret = ar8216_id_chip(priv);
1783 if (ret)
1784 goto err_free_priv;
1785
1786 if (pdev->addr != 0) {
1787 if (ar8xxx_has_gige(priv)) {
1788 pdev->supported |= SUPPORTED_1000baseT_Full;
1789 pdev->advertising |= ADVERTISED_1000baseT_Full;
1790 }
1791
1792 if (chip_is_ar8316(priv)) {
1793 /* check if we're attaching to the switch twice */
1794 pdev = pdev->bus->phy_map[0];
1795 if (!pdev) {
1796 kfree(priv);
1797 return 0;
1798 }
1799
1800 /* switch device has not been initialized, reuse priv */
1801 if (!pdev->priv) {
1802 priv->port4_phy = true;
1803 pdev->priv = priv;
1804 return 0;
1805 }
1806
1807 kfree(priv);
1808
1809 /* switch device has been initialized, reinit */
1810 priv = pdev->priv;
1811 priv->dev.ports = (AR8216_NUM_PORTS - 1);
1812 priv->initialized = false;
1813 priv->port4_phy = true;
1814 ar8316_hw_init(priv);
1815 return 0;
1816 }
1817
1818 kfree(priv);
1819 return 0;
1820 }
1821
1822 if (ar8xxx_has_gige(priv))
1823 pdev->supported = SUPPORTED_1000baseT_Full;
1824 else
1825 pdev->supported = SUPPORTED_100baseT_Full;
1826 pdev->advertising = pdev->supported;
1827
1828 mutex_init(&priv->reg_mutex);
1829 priv->read = ar8216_mii_read;
1830 priv->write = ar8216_mii_write;
1831
1832 pdev->priv = priv;
1833
1834 swdev = &priv->dev;
1835 swdev->cpu_port = AR8216_PORT_CPU;
1836 swdev->ops = &ar8216_sw_ops;
1837 swdev->ports = AR8216_NUM_PORTS;
1838
1839 if (chip_is_ar8316(priv)) {
1840 swdev->name = "Atheros AR8316";
1841 swdev->vlans = AR8X16_MAX_VLANS;
1842
1843 if (priv->port4_phy) {
1844 /* port 5 connected to the other mac, therefore unusable */
1845 swdev->ports = (AR8216_NUM_PORTS - 1);
1846 }
1847 } else if (chip_is_ar8236(priv)) {
1848 swdev->name = "Atheros AR8236";
1849 swdev->vlans = AR8216_NUM_VLANS;
1850 swdev->ports = AR8216_NUM_PORTS;
1851 } else if (chip_is_ar8327(priv)) {
1852 swdev->name = "Atheros AR8327";
1853 swdev->vlans = AR8X16_MAX_VLANS;
1854 swdev->ports = AR8327_NUM_PORTS;
1855 } else {
1856 swdev->name = "Atheros AR8216";
1857 swdev->vlans = AR8216_NUM_VLANS;
1858 }
1859
1860 ret = ar8xxx_mib_init(priv);
1861 if (ret)
1862 goto err_free_priv;
1863
1864 ret = register_switch(&priv->dev, pdev->attached_dev);
1865 if (ret)
1866 goto err_cleanup_mib;
1867
1868 printk(KERN_INFO "%s: %s switch driver attached.\n",
1869 pdev->attached_dev->name, swdev->name);
1870
1871 priv->init = true;
1872
1873 ret = priv->chip->hw_init(priv);
1874 if (ret)
1875 goto err_cleanup_mib;
1876
1877 ret = ar8216_sw_reset_switch(&priv->dev);
1878 if (ret)
1879 goto err_cleanup_mib;
1880
1881 dev->phy_ptr = priv;
1882
1883 /* VID fixup only needed on ar8216 */
1884 if (chip_is_ar8216(priv) && pdev->addr == 0) {
1885 dev->priv_flags |= IFF_NO_IP_ALIGN;
1886 dev->eth_mangle_rx = ar8216_mangle_rx;
1887 dev->eth_mangle_tx = ar8216_mangle_tx;
1888 }
1889
1890 priv->init = false;
1891
1892 ar8xxx_mib_start(priv);
1893
1894 return 0;
1895
1896 err_cleanup_mib:
1897 ar8xxx_mib_cleanup(priv);
1898 err_free_priv:
1899 kfree(priv);
1900 return ret;
1901 }
1902
1903 static int
1904 ar8216_read_status(struct phy_device *phydev)
1905 {
1906 struct ar8216_priv *priv = phydev->priv;
1907 struct switch_port_link link;
1908 int ret;
1909
1910 if (phydev->addr != 0)
1911 return genphy_read_status(phydev);
1912
1913 ar8216_read_port_link(priv, phydev->addr, &link);
1914 phydev->link = !!link.link;
1915 if (!phydev->link)
1916 return 0;
1917
1918 switch (link.speed) {
1919 case SWITCH_PORT_SPEED_10:
1920 phydev->speed = SPEED_10;
1921 break;
1922 case SWITCH_PORT_SPEED_100:
1923 phydev->speed = SPEED_100;
1924 break;
1925 case SWITCH_PORT_SPEED_1000:
1926 phydev->speed = SPEED_1000;
1927 break;
1928 default:
1929 phydev->speed = 0;
1930 }
1931 phydev->duplex = link.duplex ? DUPLEX_FULL : DUPLEX_HALF;
1932
1933 /* flush the address translation unit */
1934 mutex_lock(&priv->reg_mutex);
1935 ret = priv->chip->atu_flush(priv);
1936 mutex_unlock(&priv->reg_mutex);
1937
1938 phydev->state = PHY_RUNNING;
1939 netif_carrier_on(phydev->attached_dev);
1940 phydev->adjust_link(phydev->attached_dev);
1941
1942 return ret;
1943 }
1944
1945 static int
1946 ar8216_config_aneg(struct phy_device *phydev)
1947 {
1948 if (phydev->addr == 0)
1949 return 0;
1950
1951 return genphy_config_aneg(phydev);
1952 }
1953
1954 static int
1955 ar8216_probe(struct phy_device *pdev)
1956 {
1957 struct ar8216_priv *priv;
1958 int ret;
1959
1960 priv = kzalloc(sizeof(struct ar8216_priv), GFP_KERNEL);
1961 if (priv == NULL)
1962 return -ENOMEM;
1963
1964 priv->phy = pdev;
1965
1966 ret = ar8216_id_chip(priv);
1967 kfree(priv);
1968
1969 return ret;
1970 }
1971
1972 static void
1973 ar8216_remove(struct phy_device *pdev)
1974 {
1975 struct ar8216_priv *priv = pdev->priv;
1976 struct net_device *dev = pdev->attached_dev;
1977
1978 if (!priv)
1979 return;
1980
1981 dev->priv_flags &= ~IFF_NO_IP_ALIGN;
1982 dev->eth_mangle_rx = NULL;
1983 dev->eth_mangle_tx = NULL;
1984
1985 if (pdev->addr == 0)
1986 unregister_switch(&priv->dev);
1987
1988 ar8xxx_mib_cleanup(priv);
1989 kfree(priv);
1990 }
1991
1992 static struct phy_driver ar8216_driver = {
1993 .phy_id = 0x004d0000,
1994 .name = "Atheros AR8216/AR8236/AR8316",
1995 .phy_id_mask = 0xffff0000,
1996 .features = PHY_BASIC_FEATURES,
1997 .probe = ar8216_probe,
1998 .remove = ar8216_remove,
1999 .config_init = &ar8216_config_init,
2000 .config_aneg = &ar8216_config_aneg,
2001 .read_status = &ar8216_read_status,
2002 .driver = { .owner = THIS_MODULE },
2003 };
2004
2005 int __init
2006 ar8216_init(void)
2007 {
2008 return phy_driver_register(&ar8216_driver);
2009 }
2010
2011 void __exit
2012 ar8216_exit(void)
2013 {
2014 phy_driver_unregister(&ar8216_driver);
2015 }
2016
2017 module_init(ar8216_init);
2018 module_exit(ar8216_exit);
2019 MODULE_LICENSE("GPL");
2020