a6dd7d8e26aeaef14c7a0a0fa02ac3400e997f75
[openwrt/openwrt.git] / target / linux / generic / files / drivers / net / phy / ar8327.c
1 /*
2 * ar8327.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/list.h>
19 #include <linux/bitops.h>
20 #include <linux/switch.h>
21 #include <linux/delay.h>
22 #include <linux/phy.h>
23 #include <linux/lockdep.h>
24 #include <linux/ar8216_platform.h>
25 #include <linux/workqueue.h>
26 #include <linux/of_device.h>
27 #include <linux/leds.h>
28 #include <linux/mdio.h>
29
30 #include "ar8216.h"
31 #include "ar8327.h"
32
33 extern const struct ar8xxx_mib_desc ar8236_mibs[39];
34 extern const struct switch_attr ar8xxx_sw_attr_vlan[1];
35
36 static u32
37 ar8327_get_pad_cfg(struct ar8327_pad_cfg *cfg)
38 {
39 u32 t;
40
41 if (!cfg)
42 return 0;
43
44 t = 0;
45 switch (cfg->mode) {
46 case AR8327_PAD_NC:
47 break;
48
49 case AR8327_PAD_MAC2MAC_MII:
50 t = AR8327_PAD_MAC_MII_EN;
51 if (cfg->rxclk_sel)
52 t |= AR8327_PAD_MAC_MII_RXCLK_SEL;
53 if (cfg->txclk_sel)
54 t |= AR8327_PAD_MAC_MII_TXCLK_SEL;
55 break;
56
57 case AR8327_PAD_MAC2MAC_GMII:
58 t = AR8327_PAD_MAC_GMII_EN;
59 if (cfg->rxclk_sel)
60 t |= AR8327_PAD_MAC_GMII_RXCLK_SEL;
61 if (cfg->txclk_sel)
62 t |= AR8327_PAD_MAC_GMII_TXCLK_SEL;
63 break;
64
65 case AR8327_PAD_MAC_SGMII:
66 t = AR8327_PAD_SGMII_EN;
67
68 /*
69 * WAR for the QUalcomm Atheros AP136 board.
70 * It seems that RGMII TX/RX delay settings needs to be
71 * applied for SGMII mode as well, The ethernet is not
72 * reliable without this.
73 */
74 t |= cfg->txclk_delay_sel << AR8327_PAD_RGMII_TXCLK_DELAY_SEL_S;
75 t |= cfg->rxclk_delay_sel << AR8327_PAD_RGMII_RXCLK_DELAY_SEL_S;
76 if (cfg->rxclk_delay_en)
77 t |= AR8327_PAD_RGMII_RXCLK_DELAY_EN;
78 if (cfg->txclk_delay_en)
79 t |= AR8327_PAD_RGMII_TXCLK_DELAY_EN;
80
81 if (cfg->sgmii_delay_en)
82 t |= AR8327_PAD_SGMII_DELAY_EN;
83
84 break;
85
86 case AR8327_PAD_MAC2PHY_MII:
87 t = AR8327_PAD_PHY_MII_EN;
88 if (cfg->rxclk_sel)
89 t |= AR8327_PAD_PHY_MII_RXCLK_SEL;
90 if (cfg->txclk_sel)
91 t |= AR8327_PAD_PHY_MII_TXCLK_SEL;
92 break;
93
94 case AR8327_PAD_MAC2PHY_GMII:
95 t = AR8327_PAD_PHY_GMII_EN;
96 if (cfg->pipe_rxclk_sel)
97 t |= AR8327_PAD_PHY_GMII_PIPE_RXCLK_SEL;
98 if (cfg->rxclk_sel)
99 t |= AR8327_PAD_PHY_GMII_RXCLK_SEL;
100 if (cfg->txclk_sel)
101 t |= AR8327_PAD_PHY_GMII_TXCLK_SEL;
102 break;
103
104 case AR8327_PAD_MAC_RGMII:
105 t = AR8327_PAD_RGMII_EN;
106 t |= cfg->txclk_delay_sel << AR8327_PAD_RGMII_TXCLK_DELAY_SEL_S;
107 t |= cfg->rxclk_delay_sel << AR8327_PAD_RGMII_RXCLK_DELAY_SEL_S;
108 if (cfg->rxclk_delay_en)
109 t |= AR8327_PAD_RGMII_RXCLK_DELAY_EN;
110 if (cfg->txclk_delay_en)
111 t |= AR8327_PAD_RGMII_TXCLK_DELAY_EN;
112 break;
113
114 case AR8327_PAD_PHY_GMII:
115 t = AR8327_PAD_PHYX_GMII_EN;
116 break;
117
118 case AR8327_PAD_PHY_RGMII:
119 t = AR8327_PAD_PHYX_RGMII_EN;
120 break;
121
122 case AR8327_PAD_PHY_MII:
123 t = AR8327_PAD_PHYX_MII_EN;
124 break;
125 }
126
127 if (cfg->mac06_exchange_en)
128 t |= AR8337_PAD_MAC06_EXCHANGE_EN;
129
130 return t;
131 }
132
133 static void
134 ar8327_phy_fixup(struct ar8xxx_priv *priv, int phy)
135 {
136 switch (priv->chip_rev) {
137 case 1:
138 /* For 100M waveform */
139 ar8xxx_phy_dbg_write(priv, phy, 0, 0x02ea);
140 /* Turn on Gigabit clock */
141 ar8xxx_phy_dbg_write(priv, phy, 0x3d, 0x68a0);
142 break;
143
144 case 2:
145 ar8xxx_phy_mmd_write(priv, phy, 0x7, 0x3c);
146 ar8xxx_phy_mmd_write(priv, phy, 0x4007, 0x0);
147 /* fallthrough */
148 case 4:
149 ar8xxx_phy_mmd_write(priv, phy, 0x3, 0x800d);
150 ar8xxx_phy_mmd_write(priv, phy, 0x4003, 0x803f);
151
152 ar8xxx_phy_dbg_write(priv, phy, 0x3d, 0x6860);
153 ar8xxx_phy_dbg_write(priv, phy, 0x5, 0x2c46);
154 ar8xxx_phy_dbg_write(priv, phy, 0x3c, 0x6000);
155 break;
156 }
157 }
158
159 static u32
160 ar8327_get_port_init_status(struct ar8327_port_cfg *cfg)
161 {
162 u32 t;
163
164 if (!cfg->force_link)
165 return AR8216_PORT_STATUS_LINK_AUTO;
166
167 t = AR8216_PORT_STATUS_TXMAC | AR8216_PORT_STATUS_RXMAC;
168 t |= cfg->duplex ? AR8216_PORT_STATUS_DUPLEX : 0;
169 t |= cfg->rxpause ? AR8216_PORT_STATUS_RXFLOW : 0;
170 t |= cfg->txpause ? AR8216_PORT_STATUS_TXFLOW : 0;
171
172 switch (cfg->speed) {
173 case AR8327_PORT_SPEED_10:
174 t |= AR8216_PORT_SPEED_10M;
175 break;
176 case AR8327_PORT_SPEED_100:
177 t |= AR8216_PORT_SPEED_100M;
178 break;
179 case AR8327_PORT_SPEED_1000:
180 t |= AR8216_PORT_SPEED_1000M;
181 break;
182 }
183
184 return t;
185 }
186
187 #define AR8327_LED_ENTRY(_num, _reg, _shift) \
188 [_num] = { .reg = (_reg), .shift = (_shift) }
189
190 static const struct ar8327_led_entry
191 ar8327_led_map[AR8327_NUM_LEDS] = {
192 AR8327_LED_ENTRY(AR8327_LED_PHY0_0, 0, 14),
193 AR8327_LED_ENTRY(AR8327_LED_PHY0_1, 1, 14),
194 AR8327_LED_ENTRY(AR8327_LED_PHY0_2, 2, 14),
195
196 AR8327_LED_ENTRY(AR8327_LED_PHY1_0, 3, 8),
197 AR8327_LED_ENTRY(AR8327_LED_PHY1_1, 3, 10),
198 AR8327_LED_ENTRY(AR8327_LED_PHY1_2, 3, 12),
199
200 AR8327_LED_ENTRY(AR8327_LED_PHY2_0, 3, 14),
201 AR8327_LED_ENTRY(AR8327_LED_PHY2_1, 3, 16),
202 AR8327_LED_ENTRY(AR8327_LED_PHY2_2, 3, 18),
203
204 AR8327_LED_ENTRY(AR8327_LED_PHY3_0, 3, 20),
205 AR8327_LED_ENTRY(AR8327_LED_PHY3_1, 3, 22),
206 AR8327_LED_ENTRY(AR8327_LED_PHY3_2, 3, 24),
207
208 AR8327_LED_ENTRY(AR8327_LED_PHY4_0, 0, 30),
209 AR8327_LED_ENTRY(AR8327_LED_PHY4_1, 1, 30),
210 AR8327_LED_ENTRY(AR8327_LED_PHY4_2, 2, 30),
211 };
212
213 static void
214 ar8327_set_led_pattern(struct ar8xxx_priv *priv, unsigned int led_num,
215 enum ar8327_led_pattern pattern)
216 {
217 const struct ar8327_led_entry *entry;
218
219 entry = &ar8327_led_map[led_num];
220 ar8xxx_rmw(priv, AR8327_REG_LED_CTRL(entry->reg),
221 (3 << entry->shift), pattern << entry->shift);
222 }
223
224 static void
225 ar8327_led_work_func(struct work_struct *work)
226 {
227 struct ar8327_led *aled;
228 u8 pattern;
229
230 aled = container_of(work, struct ar8327_led, led_work);
231
232 spin_lock(&aled->lock);
233 pattern = aled->pattern;
234 spin_unlock(&aled->lock);
235
236 ar8327_set_led_pattern(aled->sw_priv, aled->led_num,
237 pattern);
238 }
239
240 static void
241 ar8327_led_schedule_change(struct ar8327_led *aled, u8 pattern)
242 {
243 if (aled->pattern == pattern)
244 return;
245
246 aled->pattern = pattern;
247 schedule_work(&aled->led_work);
248 }
249
250 static inline struct ar8327_led *
251 led_cdev_to_ar8327_led(struct led_classdev *led_cdev)
252 {
253 return container_of(led_cdev, struct ar8327_led, cdev);
254 }
255
256 static int
257 ar8327_led_blink_set(struct led_classdev *led_cdev,
258 unsigned long *delay_on,
259 unsigned long *delay_off)
260 {
261 struct ar8327_led *aled = led_cdev_to_ar8327_led(led_cdev);
262
263 if (*delay_on == 0 && *delay_off == 0) {
264 *delay_on = 125;
265 *delay_off = 125;
266 }
267
268 if (*delay_on != 125 || *delay_off != 125) {
269 /*
270 * The hardware only supports blinking at 4Hz. Fall back
271 * to software implementation in other cases.
272 */
273 return -EINVAL;
274 }
275
276 spin_lock(&aled->lock);
277
278 aled->enable_hw_mode = false;
279 ar8327_led_schedule_change(aled, AR8327_LED_PATTERN_BLINK);
280
281 spin_unlock(&aled->lock);
282
283 return 0;
284 }
285
286 static void
287 ar8327_led_set_brightness(struct led_classdev *led_cdev,
288 enum led_brightness brightness)
289 {
290 struct ar8327_led *aled = led_cdev_to_ar8327_led(led_cdev);
291 u8 pattern;
292 bool active;
293
294 active = (brightness != LED_OFF);
295 active ^= aled->active_low;
296
297 pattern = (active) ? AR8327_LED_PATTERN_ON :
298 AR8327_LED_PATTERN_OFF;
299
300 spin_lock(&aled->lock);
301
302 aled->enable_hw_mode = false;
303 ar8327_led_schedule_change(aled, pattern);
304
305 spin_unlock(&aled->lock);
306 }
307
308 static ssize_t
309 ar8327_led_enable_hw_mode_show(struct device *dev,
310 struct device_attribute *attr,
311 char *buf)
312 {
313 struct led_classdev *led_cdev = dev_get_drvdata(dev);
314 struct ar8327_led *aled = led_cdev_to_ar8327_led(led_cdev);
315 ssize_t ret = 0;
316
317 spin_lock(&aled->lock);
318 ret += sprintf(buf, "%d\n", aled->enable_hw_mode);
319 spin_unlock(&aled->lock);
320
321 return ret;
322 }
323
324 static ssize_t
325 ar8327_led_enable_hw_mode_store(struct device *dev,
326 struct device_attribute *attr,
327 const char *buf,
328 size_t size)
329 {
330 struct led_classdev *led_cdev = dev_get_drvdata(dev);
331 struct ar8327_led *aled = led_cdev_to_ar8327_led(led_cdev);
332 u8 pattern;
333 u8 value;
334 int ret;
335
336 ret = kstrtou8(buf, 10, &value);
337 if (ret < 0)
338 return -EINVAL;
339
340 spin_lock(&aled->lock);
341
342 aled->enable_hw_mode = !!value;
343 if (aled->enable_hw_mode)
344 pattern = AR8327_LED_PATTERN_RULE;
345 else
346 pattern = AR8327_LED_PATTERN_OFF;
347
348 ar8327_led_schedule_change(aled, pattern);
349
350 spin_unlock(&aled->lock);
351
352 return size;
353 }
354
355 static DEVICE_ATTR(enable_hw_mode, S_IRUGO | S_IWUSR,
356 ar8327_led_enable_hw_mode_show,
357 ar8327_led_enable_hw_mode_store);
358
359 static int
360 ar8327_led_register(struct ar8327_led *aled)
361 {
362 int ret;
363
364 ret = led_classdev_register(NULL, &aled->cdev);
365 if (ret < 0)
366 return ret;
367
368 if (aled->mode == AR8327_LED_MODE_HW) {
369 ret = device_create_file(aled->cdev.dev,
370 &dev_attr_enable_hw_mode);
371 if (ret)
372 goto err_unregister;
373 }
374
375 return 0;
376
377 err_unregister:
378 led_classdev_unregister(&aled->cdev);
379 return ret;
380 }
381
382 static void
383 ar8327_led_unregister(struct ar8327_led *aled)
384 {
385 if (aled->mode == AR8327_LED_MODE_HW)
386 device_remove_file(aled->cdev.dev, &dev_attr_enable_hw_mode);
387
388 led_classdev_unregister(&aled->cdev);
389 cancel_work_sync(&aled->led_work);
390 }
391
392 static int
393 ar8327_led_create(struct ar8xxx_priv *priv,
394 const struct ar8327_led_info *led_info)
395 {
396 struct ar8327_data *data = priv->chip_data;
397 struct ar8327_led *aled;
398 int ret;
399
400 if (!IS_ENABLED(CONFIG_AR8216_PHY_LEDS))
401 return 0;
402
403 if (!led_info->name)
404 return -EINVAL;
405
406 if (led_info->led_num >= AR8327_NUM_LEDS)
407 return -EINVAL;
408
409 aled = kzalloc(sizeof(*aled) + strlen(led_info->name) + 1,
410 GFP_KERNEL);
411 if (!aled)
412 return -ENOMEM;
413
414 aled->sw_priv = priv;
415 aled->led_num = led_info->led_num;
416 aled->active_low = led_info->active_low;
417 aled->mode = led_info->mode;
418
419 if (aled->mode == AR8327_LED_MODE_HW)
420 aled->enable_hw_mode = true;
421
422 aled->name = (char *)(aled + 1);
423 strcpy(aled->name, led_info->name);
424
425 aled->cdev.name = aled->name;
426 aled->cdev.brightness_set = ar8327_led_set_brightness;
427 aled->cdev.blink_set = ar8327_led_blink_set;
428 aled->cdev.default_trigger = led_info->default_trigger;
429
430 spin_lock_init(&aled->lock);
431 mutex_init(&aled->mutex);
432 INIT_WORK(&aled->led_work, ar8327_led_work_func);
433
434 ret = ar8327_led_register(aled);
435 if (ret)
436 goto err_free;
437
438 data->leds[data->num_leds++] = aled;
439
440 return 0;
441
442 err_free:
443 kfree(aled);
444 return ret;
445 }
446
447 static void
448 ar8327_led_destroy(struct ar8327_led *aled)
449 {
450 ar8327_led_unregister(aled);
451 kfree(aled);
452 }
453
454 static void
455 ar8327_leds_init(struct ar8xxx_priv *priv)
456 {
457 struct ar8327_data *data = priv->chip_data;
458 unsigned i;
459
460 if (!IS_ENABLED(CONFIG_AR8216_PHY_LEDS))
461 return;
462
463 for (i = 0; i < data->num_leds; i++) {
464 struct ar8327_led *aled;
465
466 aled = data->leds[i];
467
468 if (aled->enable_hw_mode)
469 aled->pattern = AR8327_LED_PATTERN_RULE;
470 else
471 aled->pattern = AR8327_LED_PATTERN_OFF;
472
473 ar8327_set_led_pattern(priv, aled->led_num, aled->pattern);
474 }
475 }
476
477 static void
478 ar8327_leds_cleanup(struct ar8xxx_priv *priv)
479 {
480 struct ar8327_data *data = priv->chip_data;
481 unsigned i;
482
483 if (!IS_ENABLED(CONFIG_AR8216_PHY_LEDS))
484 return;
485
486 for (i = 0; i < data->num_leds; i++) {
487 struct ar8327_led *aled;
488
489 aled = data->leds[i];
490 ar8327_led_destroy(aled);
491 }
492
493 kfree(data->leds);
494 }
495
496 static int
497 ar8327_hw_config_pdata(struct ar8xxx_priv *priv,
498 struct ar8327_platform_data *pdata)
499 {
500 struct ar8327_led_cfg *led_cfg;
501 struct ar8327_data *data = priv->chip_data;
502 u32 pos, new_pos;
503 u32 t;
504
505 if (!pdata)
506 return -EINVAL;
507
508 priv->get_port_link = pdata->get_port_link;
509
510 data->port0_status = ar8327_get_port_init_status(&pdata->port0_cfg);
511 data->port6_status = ar8327_get_port_init_status(&pdata->port6_cfg);
512
513 t = ar8327_get_pad_cfg(pdata->pad0_cfg);
514 ar8xxx_write(priv, AR8327_REG_PAD0_MODE, t);
515 t = ar8327_get_pad_cfg(pdata->pad5_cfg);
516 ar8xxx_write(priv, AR8327_REG_PAD5_MODE, t);
517 t = ar8327_get_pad_cfg(pdata->pad6_cfg);
518 ar8xxx_write(priv, AR8327_REG_PAD6_MODE, t);
519
520 pos = ar8xxx_read(priv, AR8327_REG_POWER_ON_STRIP);
521 new_pos = pos;
522
523 led_cfg = pdata->led_cfg;
524 if (led_cfg) {
525 if (led_cfg->open_drain)
526 new_pos |= AR8327_POWER_ON_STRIP_LED_OPEN_EN;
527 else
528 new_pos &= ~AR8327_POWER_ON_STRIP_LED_OPEN_EN;
529
530 ar8xxx_write(priv, AR8327_REG_LED_CTRL0, led_cfg->led_ctrl0);
531 ar8xxx_write(priv, AR8327_REG_LED_CTRL1, led_cfg->led_ctrl1);
532 ar8xxx_write(priv, AR8327_REG_LED_CTRL2, led_cfg->led_ctrl2);
533 ar8xxx_write(priv, AR8327_REG_LED_CTRL3, led_cfg->led_ctrl3);
534
535 if (new_pos != pos)
536 new_pos |= AR8327_POWER_ON_STRIP_POWER_ON_SEL;
537 }
538
539 if (pdata->sgmii_cfg) {
540 t = pdata->sgmii_cfg->sgmii_ctrl;
541 if (priv->chip_rev == 1)
542 t |= AR8327_SGMII_CTRL_EN_PLL |
543 AR8327_SGMII_CTRL_EN_RX |
544 AR8327_SGMII_CTRL_EN_TX;
545 else
546 t &= ~(AR8327_SGMII_CTRL_EN_PLL |
547 AR8327_SGMII_CTRL_EN_RX |
548 AR8327_SGMII_CTRL_EN_TX);
549
550 ar8xxx_write(priv, AR8327_REG_SGMII_CTRL, t);
551
552 if (pdata->sgmii_cfg->serdes_aen)
553 new_pos &= ~AR8327_POWER_ON_STRIP_SERDES_AEN;
554 else
555 new_pos |= AR8327_POWER_ON_STRIP_SERDES_AEN;
556 }
557
558 ar8xxx_write(priv, AR8327_REG_POWER_ON_STRIP, new_pos);
559
560 if (pdata->leds && pdata->num_leds) {
561 int i;
562
563 data->leds = kzalloc(pdata->num_leds * sizeof(void *),
564 GFP_KERNEL);
565 if (!data->leds)
566 return -ENOMEM;
567
568 for (i = 0; i < pdata->num_leds; i++)
569 ar8327_led_create(priv, &pdata->leds[i]);
570 }
571
572 return 0;
573 }
574
575 #ifdef CONFIG_OF
576 static int
577 ar8327_hw_config_of(struct ar8xxx_priv *priv, struct device_node *np)
578 {
579 struct ar8327_data *data = priv->chip_data;
580 const __be32 *paddr;
581 int len;
582 int i;
583
584 paddr = of_get_property(np, "qca,ar8327-initvals", &len);
585 if (!paddr || len < (2 * sizeof(*paddr)))
586 return -EINVAL;
587
588 len /= sizeof(*paddr);
589
590 for (i = 0; i < len - 1; i += 2) {
591 u32 reg;
592 u32 val;
593
594 reg = be32_to_cpup(paddr + i);
595 val = be32_to_cpup(paddr + i + 1);
596
597 switch (reg) {
598 case AR8327_REG_PORT_STATUS(0):
599 data->port0_status = val;
600 break;
601 case AR8327_REG_PORT_STATUS(6):
602 data->port6_status = val;
603 break;
604 default:
605 ar8xxx_write(priv, reg, val);
606 break;
607 }
608 }
609
610 return 0;
611 }
612 #else
613 static inline int
614 ar8327_hw_config_of(struct ar8xxx_priv *priv, struct device_node *np)
615 {
616 return -EINVAL;
617 }
618 #endif
619
620 static int
621 ar8327_hw_init(struct ar8xxx_priv *priv)
622 {
623 int ret;
624
625 priv->chip_data = kzalloc(sizeof(struct ar8327_data), GFP_KERNEL);
626 if (!priv->chip_data)
627 return -ENOMEM;
628
629 if (priv->phy->dev.of_node)
630 ret = ar8327_hw_config_of(priv, priv->phy->dev.of_node);
631 else
632 ret = ar8327_hw_config_pdata(priv,
633 priv->phy->dev.platform_data);
634
635 if (ret)
636 return ret;
637
638 ar8327_leds_init(priv);
639
640 ar8xxx_phy_init(priv);
641
642 return 0;
643 }
644
645 static void
646 ar8327_cleanup(struct ar8xxx_priv *priv)
647 {
648 ar8327_leds_cleanup(priv);
649 }
650
651 static void
652 ar8327_init_globals(struct ar8xxx_priv *priv)
653 {
654 struct ar8327_data *data = priv->chip_data;
655 u32 t;
656 int i;
657
658 /* enable CPU port and disable mirror port */
659 t = AR8327_FWD_CTRL0_CPU_PORT_EN |
660 AR8327_FWD_CTRL0_MIRROR_PORT;
661 ar8xxx_write(priv, AR8327_REG_FWD_CTRL0, t);
662
663 /* forward multicast and broadcast frames to CPU */
664 t = (AR8327_PORTS_ALL << AR8327_FWD_CTRL1_UC_FLOOD_S) |
665 (AR8327_PORTS_ALL << AR8327_FWD_CTRL1_MC_FLOOD_S) |
666 (AR8327_PORTS_ALL << AR8327_FWD_CTRL1_BC_FLOOD_S);
667 ar8xxx_write(priv, AR8327_REG_FWD_CTRL1, t);
668
669 /* enable jumbo frames */
670 ar8xxx_rmw(priv, AR8327_REG_MAX_FRAME_SIZE,
671 AR8327_MAX_FRAME_SIZE_MTU, 9018 + 8 + 2);
672
673 /* Enable MIB counters */
674 ar8xxx_reg_set(priv, AR8327_REG_MODULE_EN,
675 AR8327_MODULE_EN_MIB);
676
677 /* Disable EEE on all phy's due to stability issues */
678 for (i = 0; i < AR8XXX_NUM_PHYS; i++)
679 data->eee[i] = false;
680 }
681
682 static void
683 ar8327_init_port(struct ar8xxx_priv *priv, int port)
684 {
685 struct ar8327_data *data = priv->chip_data;
686 u32 t;
687
688 if (port == AR8216_PORT_CPU)
689 t = data->port0_status;
690 else if (port == 6)
691 t = data->port6_status;
692 else
693 t = AR8216_PORT_STATUS_LINK_AUTO;
694
695 ar8xxx_write(priv, AR8327_REG_PORT_STATUS(port), t);
696 ar8xxx_write(priv, AR8327_REG_PORT_HEADER(port), 0);
697
698 t = 1 << AR8327_PORT_VLAN0_DEF_SVID_S;
699 t |= 1 << AR8327_PORT_VLAN0_DEF_CVID_S;
700 ar8xxx_write(priv, AR8327_REG_PORT_VLAN0(port), t);
701
702 t = AR8327_PORT_VLAN1_OUT_MODE_UNTOUCH << AR8327_PORT_VLAN1_OUT_MODE_S;
703 ar8xxx_write(priv, AR8327_REG_PORT_VLAN1(port), t);
704
705 t = AR8327_PORT_LOOKUP_LEARN;
706 t |= AR8216_PORT_STATE_FORWARD << AR8327_PORT_LOOKUP_STATE_S;
707 ar8xxx_write(priv, AR8327_REG_PORT_LOOKUP(port), t);
708 }
709
710 static u32
711 ar8327_read_port_status(struct ar8xxx_priv *priv, int port)
712 {
713 u32 t;
714
715 t = ar8xxx_read(priv, AR8327_REG_PORT_STATUS(port));
716 /* map the flow control autoneg result bits to the flow control bits
717 * used in forced mode to allow ar8216_read_port_link detect
718 * flow control properly if autoneg is used
719 */
720 if (t & AR8216_PORT_STATUS_LINK_UP &&
721 t & AR8216_PORT_STATUS_LINK_AUTO) {
722 t &= ~(AR8216_PORT_STATUS_TXFLOW | AR8216_PORT_STATUS_RXFLOW);
723 if (t & AR8327_PORT_STATUS_TXFLOW_AUTO)
724 t |= AR8216_PORT_STATUS_TXFLOW;
725 if (t & AR8327_PORT_STATUS_RXFLOW_AUTO)
726 t |= AR8216_PORT_STATUS_RXFLOW;
727 }
728
729 return t;
730 }
731
732 static u32
733 ar8327_read_port_eee_status(struct ar8xxx_priv *priv, int port)
734 {
735 int phy;
736 u16 t;
737
738 if (port >= priv->dev.ports)
739 return 0;
740
741 if (port == 0 || port == 6)
742 return 0;
743
744 phy = port - 1;
745
746 /* EEE Ability Auto-negotiation Result */
747 ar8xxx_phy_mmd_write(priv, phy, 0x7, 0x8000);
748 t = ar8xxx_phy_mmd_read(priv, phy, 0x4007);
749
750 return mmd_eee_adv_to_ethtool_adv_t(t);
751 }
752
753 static int
754 ar8327_atu_flush(struct ar8xxx_priv *priv)
755 {
756 int ret;
757
758 ret = ar8216_wait_bit(priv, AR8327_REG_ATU_FUNC,
759 AR8327_ATU_FUNC_BUSY, 0);
760 if (!ret)
761 ar8xxx_write(priv, AR8327_REG_ATU_FUNC,
762 AR8327_ATU_FUNC_OP_FLUSH |
763 AR8327_ATU_FUNC_BUSY);
764
765 return ret;
766 }
767
768 static void
769 ar8327_vtu_op(struct ar8xxx_priv *priv, u32 op, u32 val)
770 {
771 if (ar8216_wait_bit(priv, AR8327_REG_VTU_FUNC1,
772 AR8327_VTU_FUNC1_BUSY, 0))
773 return;
774
775 if ((op & AR8327_VTU_FUNC1_OP) == AR8327_VTU_FUNC1_OP_LOAD)
776 ar8xxx_write(priv, AR8327_REG_VTU_FUNC0, val);
777
778 op |= AR8327_VTU_FUNC1_BUSY;
779 ar8xxx_write(priv, AR8327_REG_VTU_FUNC1, op);
780 }
781
782 static void
783 ar8327_vtu_flush(struct ar8xxx_priv *priv)
784 {
785 ar8327_vtu_op(priv, AR8327_VTU_FUNC1_OP_FLUSH, 0);
786 }
787
788 static void
789 ar8327_vtu_load_vlan(struct ar8xxx_priv *priv, u32 vid, u32 port_mask)
790 {
791 u32 op;
792 u32 val;
793 int i;
794
795 op = AR8327_VTU_FUNC1_OP_LOAD | (vid << AR8327_VTU_FUNC1_VID_S);
796 val = AR8327_VTU_FUNC0_VALID | AR8327_VTU_FUNC0_IVL;
797 for (i = 0; i < AR8327_NUM_PORTS; i++) {
798 u32 mode;
799
800 if ((port_mask & BIT(i)) == 0)
801 mode = AR8327_VTU_FUNC0_EG_MODE_NOT;
802 else if (priv->vlan == 0)
803 mode = AR8327_VTU_FUNC0_EG_MODE_KEEP;
804 else if ((priv->vlan_tagged & BIT(i)) || (priv->vlan_id[priv->pvid[i]] != vid))
805 mode = AR8327_VTU_FUNC0_EG_MODE_TAG;
806 else
807 mode = AR8327_VTU_FUNC0_EG_MODE_UNTAG;
808
809 val |= mode << AR8327_VTU_FUNC0_EG_MODE_S(i);
810 }
811 ar8327_vtu_op(priv, op, val);
812 }
813
814 static void
815 ar8327_setup_port(struct ar8xxx_priv *priv, int port, u32 members)
816 {
817 u32 t;
818 u32 egress, ingress;
819 u32 pvid = priv->vlan_id[priv->pvid[port]];
820
821 if (priv->vlan) {
822 egress = AR8327_PORT_VLAN1_OUT_MODE_UNMOD;
823 ingress = AR8216_IN_SECURE;
824 } else {
825 egress = AR8327_PORT_VLAN1_OUT_MODE_UNTOUCH;
826 ingress = AR8216_IN_PORT_ONLY;
827 }
828
829 t = pvid << AR8327_PORT_VLAN0_DEF_SVID_S;
830 t |= pvid << AR8327_PORT_VLAN0_DEF_CVID_S;
831 ar8xxx_write(priv, AR8327_REG_PORT_VLAN0(port), t);
832
833 t = AR8327_PORT_VLAN1_PORT_VLAN_PROP;
834 t |= egress << AR8327_PORT_VLAN1_OUT_MODE_S;
835 ar8xxx_write(priv, AR8327_REG_PORT_VLAN1(port), t);
836
837 t = members;
838 t |= AR8327_PORT_LOOKUP_LEARN;
839 t |= ingress << AR8327_PORT_LOOKUP_IN_MODE_S;
840 t |= AR8216_PORT_STATE_FORWARD << AR8327_PORT_LOOKUP_STATE_S;
841 ar8xxx_write(priv, AR8327_REG_PORT_LOOKUP(port), t);
842 }
843
844 static int
845 ar8327_sw_get_ports(struct switch_dev *dev, struct switch_val *val)
846 {
847 struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
848 u8 ports = priv->vlan_table[val->port_vlan];
849 int i;
850
851 val->len = 0;
852 for (i = 0; i < dev->ports; i++) {
853 struct switch_port *p;
854
855 if (!(ports & (1 << i)))
856 continue;
857
858 p = &val->value.ports[val->len++];
859 p->id = i;
860 if ((priv->vlan_tagged & (1 << i)) || (priv->pvid[i] != val->port_vlan))
861 p->flags = (1 << SWITCH_PORT_FLAG_TAGGED);
862 else
863 p->flags = 0;
864 }
865 return 0;
866 }
867
868 static int
869 ar8327_sw_set_ports(struct switch_dev *dev, struct switch_val *val)
870 {
871 struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
872 u8 *vt = &priv->vlan_table[val->port_vlan];
873 int i;
874
875 *vt = 0;
876 for (i = 0; i < val->len; i++) {
877 struct switch_port *p = &val->value.ports[i];
878
879 if (p->flags & (1 << SWITCH_PORT_FLAG_TAGGED)) {
880 if (val->port_vlan == priv->pvid[p->id]) {
881 priv->vlan_tagged |= (1 << p->id);
882 }
883 } else {
884 priv->vlan_tagged &= ~(1 << p->id);
885 priv->pvid[p->id] = val->port_vlan;
886 }
887
888 *vt |= 1 << p->id;
889 }
890 return 0;
891 }
892
893 static void
894 ar8327_set_mirror_regs(struct ar8xxx_priv *priv)
895 {
896 int port;
897
898 /* reset all mirror registers */
899 ar8xxx_rmw(priv, AR8327_REG_FWD_CTRL0,
900 AR8327_FWD_CTRL0_MIRROR_PORT,
901 (0xF << AR8327_FWD_CTRL0_MIRROR_PORT_S));
902 for (port = 0; port < AR8327_NUM_PORTS; port++) {
903 ar8xxx_reg_clear(priv, AR8327_REG_PORT_LOOKUP(port),
904 AR8327_PORT_LOOKUP_ING_MIRROR_EN);
905
906 ar8xxx_reg_clear(priv, AR8327_REG_PORT_HOL_CTRL1(port),
907 AR8327_PORT_HOL_CTRL1_EG_MIRROR_EN);
908 }
909
910 /* now enable mirroring if necessary */
911 if (priv->source_port >= AR8327_NUM_PORTS ||
912 priv->monitor_port >= AR8327_NUM_PORTS ||
913 priv->source_port == priv->monitor_port) {
914 return;
915 }
916
917 ar8xxx_rmw(priv, AR8327_REG_FWD_CTRL0,
918 AR8327_FWD_CTRL0_MIRROR_PORT,
919 (priv->monitor_port << AR8327_FWD_CTRL0_MIRROR_PORT_S));
920
921 if (priv->mirror_rx)
922 ar8xxx_reg_set(priv, AR8327_REG_PORT_LOOKUP(priv->source_port),
923 AR8327_PORT_LOOKUP_ING_MIRROR_EN);
924
925 if (priv->mirror_tx)
926 ar8xxx_reg_set(priv, AR8327_REG_PORT_HOL_CTRL1(priv->source_port),
927 AR8327_PORT_HOL_CTRL1_EG_MIRROR_EN);
928 }
929
930 static int
931 ar8327_sw_set_eee(struct switch_dev *dev,
932 const struct switch_attr *attr,
933 struct switch_val *val)
934 {
935 struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
936 struct ar8327_data *data = priv->chip_data;
937 int port = val->port_vlan;
938 int phy;
939
940 if (port >= dev->ports)
941 return -EINVAL;
942 if (port == 0 || port == 6)
943 return -EOPNOTSUPP;
944
945 phy = port - 1;
946
947 data->eee[phy] = !!(val->value.i);
948
949 return 0;
950 }
951
952 static int
953 ar8327_sw_get_eee(struct switch_dev *dev,
954 const struct switch_attr *attr,
955 struct switch_val *val)
956 {
957 struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
958 const struct ar8327_data *data = priv->chip_data;
959 int port = val->port_vlan;
960 int phy;
961
962 if (port >= dev->ports)
963 return -EINVAL;
964 if (port == 0 || port == 6)
965 return -EOPNOTSUPP;
966
967 phy = port - 1;
968
969 val->value.i = data->eee[phy];
970
971 return 0;
972 }
973
974 static void
975 ar8327_wait_atu_ready(struct ar8xxx_priv *priv, u16 r2, u16 r1)
976 {
977 int timeout = 20;
978
979 while (ar8xxx_mii_read32(priv, r2, r1) & AR8327_ATU_FUNC_BUSY && --timeout)
980 udelay(10);
981
982 if (!timeout)
983 pr_err("ar8327: timeout waiting for atu to become ready\n");
984 }
985
986 static void ar8327_get_arl_entry(struct ar8xxx_priv *priv,
987 struct arl_entry *a, u32 *status, enum arl_op op)
988 {
989 struct mii_bus *bus = priv->mii_bus;
990 u16 r2, page;
991 u16 r1_data0, r1_data1, r1_data2, r1_func;
992 u32 t, val0, val1, val2;
993 int i;
994
995 split_addr(AR8327_REG_ATU_DATA0, &r1_data0, &r2, &page);
996 r2 |= 0x10;
997
998 r1_data1 = (AR8327_REG_ATU_DATA1 >> 1) & 0x1e;
999 r1_data2 = (AR8327_REG_ATU_DATA2 >> 1) & 0x1e;
1000 r1_func = (AR8327_REG_ATU_FUNC >> 1) & 0x1e;
1001
1002 switch (op) {
1003 case AR8XXX_ARL_INITIALIZE:
1004 /* all ATU registers are on the same page
1005 * therefore set page only once
1006 */
1007 bus->write(bus, 0x18, 0, page);
1008 wait_for_page_switch();
1009
1010 ar8327_wait_atu_ready(priv, r2, r1_func);
1011
1012 ar8xxx_mii_write32(priv, r2, r1_data0, 0);
1013 ar8xxx_mii_write32(priv, r2, r1_data1, 0);
1014 ar8xxx_mii_write32(priv, r2, r1_data2, 0);
1015 break;
1016 case AR8XXX_ARL_GET_NEXT:
1017 ar8xxx_mii_write32(priv, r2, r1_func,
1018 AR8327_ATU_FUNC_OP_GET_NEXT |
1019 AR8327_ATU_FUNC_BUSY);
1020 ar8327_wait_atu_ready(priv, r2, r1_func);
1021
1022 val0 = ar8xxx_mii_read32(priv, r2, r1_data0);
1023 val1 = ar8xxx_mii_read32(priv, r2, r1_data1);
1024 val2 = ar8xxx_mii_read32(priv, r2, r1_data2);
1025
1026 *status = val2 & AR8327_ATU_STATUS;
1027 if (!*status)
1028 break;
1029
1030 i = 0;
1031 t = AR8327_ATU_PORT0;
1032 while (!(val1 & t) && ++i < AR8327_NUM_PORTS)
1033 t <<= 1;
1034
1035 a->port = i;
1036 a->mac[0] = (val0 & AR8327_ATU_ADDR0) >> AR8327_ATU_ADDR0_S;
1037 a->mac[1] = (val0 & AR8327_ATU_ADDR1) >> AR8327_ATU_ADDR1_S;
1038 a->mac[2] = (val0 & AR8327_ATU_ADDR2) >> AR8327_ATU_ADDR2_S;
1039 a->mac[3] = (val0 & AR8327_ATU_ADDR3) >> AR8327_ATU_ADDR3_S;
1040 a->mac[4] = (val1 & AR8327_ATU_ADDR4) >> AR8327_ATU_ADDR4_S;
1041 a->mac[5] = (val1 & AR8327_ATU_ADDR5) >> AR8327_ATU_ADDR5_S;
1042 break;
1043 }
1044 }
1045
1046 static int
1047 ar8327_sw_hw_apply(struct switch_dev *dev)
1048 {
1049 struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
1050 const struct ar8327_data *data = priv->chip_data;
1051 int ret, i;
1052
1053 ret = ar8xxx_sw_hw_apply(dev);
1054 if (ret)
1055 return ret;
1056
1057 for (i=0; i < AR8XXX_NUM_PHYS; i++) {
1058 if (data->eee[i])
1059 ar8xxx_reg_clear(priv, AR8327_REG_EEE_CTRL,
1060 AR8327_EEE_CTRL_DISABLE_PHY(i));
1061 else
1062 ar8xxx_reg_set(priv, AR8327_REG_EEE_CTRL,
1063 AR8327_EEE_CTRL_DISABLE_PHY(i));
1064 }
1065
1066 return 0;
1067 }
1068
1069 static const struct switch_attr ar8327_sw_attr_globals[] = {
1070 {
1071 .type = SWITCH_TYPE_INT,
1072 .name = "enable_vlan",
1073 .description = "Enable VLAN mode",
1074 .set = ar8xxx_sw_set_vlan,
1075 .get = ar8xxx_sw_get_vlan,
1076 .max = 1
1077 },
1078 {
1079 .type = SWITCH_TYPE_NOVAL,
1080 .name = "reset_mibs",
1081 .description = "Reset all MIB counters",
1082 .set = ar8xxx_sw_set_reset_mibs,
1083 },
1084 {
1085 .type = SWITCH_TYPE_INT,
1086 .name = "enable_mirror_rx",
1087 .description = "Enable mirroring of RX packets",
1088 .set = ar8xxx_sw_set_mirror_rx_enable,
1089 .get = ar8xxx_sw_get_mirror_rx_enable,
1090 .max = 1
1091 },
1092 {
1093 .type = SWITCH_TYPE_INT,
1094 .name = "enable_mirror_tx",
1095 .description = "Enable mirroring of TX packets",
1096 .set = ar8xxx_sw_set_mirror_tx_enable,
1097 .get = ar8xxx_sw_get_mirror_tx_enable,
1098 .max = 1
1099 },
1100 {
1101 .type = SWITCH_TYPE_INT,
1102 .name = "mirror_monitor_port",
1103 .description = "Mirror monitor port",
1104 .set = ar8xxx_sw_set_mirror_monitor_port,
1105 .get = ar8xxx_sw_get_mirror_monitor_port,
1106 .max = AR8327_NUM_PORTS - 1
1107 },
1108 {
1109 .type = SWITCH_TYPE_INT,
1110 .name = "mirror_source_port",
1111 .description = "Mirror source port",
1112 .set = ar8xxx_sw_set_mirror_source_port,
1113 .get = ar8xxx_sw_get_mirror_source_port,
1114 .max = AR8327_NUM_PORTS - 1
1115 },
1116 {
1117 .type = SWITCH_TYPE_STRING,
1118 .name = "arl_table",
1119 .description = "Get ARL table",
1120 .set = NULL,
1121 .get = ar8xxx_sw_get_arl_table,
1122 },
1123 };
1124
1125 static const struct switch_attr ar8327_sw_attr_port[] = {
1126 {
1127 .type = SWITCH_TYPE_NOVAL,
1128 .name = "reset_mib",
1129 .description = "Reset single port MIB counters",
1130 .set = ar8xxx_sw_set_port_reset_mib,
1131 },
1132 {
1133 .type = SWITCH_TYPE_STRING,
1134 .name = "mib",
1135 .description = "Get port's MIB counters",
1136 .set = NULL,
1137 .get = ar8xxx_sw_get_port_mib,
1138 },
1139 {
1140 .type = SWITCH_TYPE_INT,
1141 .name = "enable_eee",
1142 .description = "Enable EEE PHY sleep mode",
1143 .set = ar8327_sw_set_eee,
1144 .get = ar8327_sw_get_eee,
1145 .max = 1,
1146 },
1147 };
1148
1149 static const struct switch_dev_ops ar8327_sw_ops = {
1150 .attr_global = {
1151 .attr = ar8327_sw_attr_globals,
1152 .n_attr = ARRAY_SIZE(ar8327_sw_attr_globals),
1153 },
1154 .attr_port = {
1155 .attr = ar8327_sw_attr_port,
1156 .n_attr = ARRAY_SIZE(ar8327_sw_attr_port),
1157 },
1158 .attr_vlan = {
1159 .attr = ar8xxx_sw_attr_vlan,
1160 .n_attr = ARRAY_SIZE(ar8xxx_sw_attr_vlan),
1161 },
1162 .get_port_pvid = ar8xxx_sw_get_pvid,
1163 .set_port_pvid = ar8xxx_sw_set_pvid,
1164 .get_vlan_ports = ar8327_sw_get_ports,
1165 .set_vlan_ports = ar8327_sw_set_ports,
1166 .apply_config = ar8327_sw_hw_apply,
1167 .reset_switch = ar8xxx_sw_reset_switch,
1168 .get_port_link = ar8xxx_sw_get_port_link,
1169 };
1170
1171 const struct ar8xxx_chip ar8327_chip = {
1172 .caps = AR8XXX_CAP_GIGE | AR8XXX_CAP_MIB_COUNTERS,
1173 .config_at_probe = true,
1174 .mii_lo_first = true,
1175
1176 .name = "Atheros AR8327",
1177 .ports = AR8327_NUM_PORTS,
1178 .vlans = AR8X16_MAX_VLANS,
1179 .swops = &ar8327_sw_ops,
1180
1181 .reg_port_stats_start = 0x1000,
1182 .reg_port_stats_length = 0x100,
1183
1184 .hw_init = ar8327_hw_init,
1185 .cleanup = ar8327_cleanup,
1186 .init_globals = ar8327_init_globals,
1187 .init_port = ar8327_init_port,
1188 .setup_port = ar8327_setup_port,
1189 .read_port_status = ar8327_read_port_status,
1190 .read_port_eee_status = ar8327_read_port_eee_status,
1191 .atu_flush = ar8327_atu_flush,
1192 .vtu_flush = ar8327_vtu_flush,
1193 .vtu_load_vlan = ar8327_vtu_load_vlan,
1194 .phy_fixup = ar8327_phy_fixup,
1195 .set_mirror_regs = ar8327_set_mirror_regs,
1196 .get_arl_entry = ar8327_get_arl_entry,
1197 .sw_hw_apply = ar8327_sw_hw_apply,
1198
1199 .num_mibs = ARRAY_SIZE(ar8236_mibs),
1200 .mib_decs = ar8236_mibs,
1201 .mib_func = AR8327_REG_MIB_FUNC
1202 };
1203
1204 const struct ar8xxx_chip ar8337_chip = {
1205 .caps = AR8XXX_CAP_GIGE | AR8XXX_CAP_MIB_COUNTERS,
1206 .config_at_probe = true,
1207 .mii_lo_first = true,
1208
1209 .name = "Atheros AR8337",
1210 .ports = AR8327_NUM_PORTS,
1211 .vlans = AR8X16_MAX_VLANS,
1212 .swops = &ar8327_sw_ops,
1213
1214 .reg_port_stats_start = 0x1000,
1215 .reg_port_stats_length = 0x100,
1216
1217 .hw_init = ar8327_hw_init,
1218 .cleanup = ar8327_cleanup,
1219 .init_globals = ar8327_init_globals,
1220 .init_port = ar8327_init_port,
1221 .setup_port = ar8327_setup_port,
1222 .read_port_status = ar8327_read_port_status,
1223 .read_port_eee_status = ar8327_read_port_eee_status,
1224 .atu_flush = ar8327_atu_flush,
1225 .vtu_flush = ar8327_vtu_flush,
1226 .vtu_load_vlan = ar8327_vtu_load_vlan,
1227 .phy_fixup = ar8327_phy_fixup,
1228 .set_mirror_regs = ar8327_set_mirror_regs,
1229 .get_arl_entry = ar8327_get_arl_entry,
1230 .sw_hw_apply = ar8327_sw_hw_apply,
1231
1232 .num_mibs = ARRAY_SIZE(ar8236_mibs),
1233 .mib_decs = ar8236_mibs,
1234 .mib_func = AR8327_REG_MIB_FUNC
1235 };
1236