2 * Platform driver for the Realtek RTL8366S ethernet switch
4 * Copyright (C) 2009-2010 Gabor Juhos <juhosg@openwrt.org>
5 * Copyright (C) 2010 Antti Seppälä <a.seppala@gmail.com>
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License version 2 as published
9 * by the Free Software Foundation.
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/init.h>
15 #include <linux/platform_device.h>
16 #include <linux/delay.h>
17 #include <linux/skbuff.h>
18 #include <linux/switch.h>
19 #include <linux/rtl8366s.h>
21 #include "rtl8366_smi.h"
23 #ifdef CONFIG_RTL8366S_PHY_DEBUG_FS
24 #include <linux/debugfs.h>
27 #define RTL8366S_DRIVER_DESC "Realtek RTL8366S ethernet switch driver"
28 #define RTL8366S_DRIVER_VER "0.2.2"
30 #define RTL8366S_PHY_NO_MAX 4
31 #define RTL8366S_PHY_PAGE_MAX 7
32 #define RTL8366S_PHY_ADDR_MAX 31
34 #define RTL8366S_CHIP_GLOBAL_CTRL_REG 0x0000
35 #define RTL8366S_CHIP_CTRL_VLAN (1 << 13)
37 /* Switch Global Configuration register */
38 #define RTL8366S_SGCR 0x0000
39 #define RTL8366S_SGCR_EN_BC_STORM_CTRL BIT(0)
40 #define RTL8366S_SGCR_MAX_LENGTH(_x) (_x << 4)
41 #define RTL8366S_SGCR_MAX_LENGTH_MASK RTL8366S_SGCR_MAX_LENGTH(0x3)
42 #define RTL8366S_SGCR_MAX_LENGTH_1522 RTL8366S_SGCR_MAX_LENGTH(0x0)
43 #define RTL8366S_SGCR_MAX_LENGTH_1536 RTL8366S_SGCR_MAX_LENGTH(0x1)
44 #define RTL8366S_SGCR_MAX_LENGTH_1552 RTL8366S_SGCR_MAX_LENGTH(0x2)
45 #define RTL8366S_SGCR_MAX_LENGTH_16000 RTL8366S_SGCR_MAX_LENGTH(0x3)
47 /* Port Enable Control register */
48 #define RTL8366S_PECR 0x0001
50 /* Switch Security Control registers */
51 #define RTL8366S_SSCR0 0x0002
52 #define RTL8366S_SSCR1 0x0003
53 #define RTL8366S_SSCR2 0x0004
54 #define RTL8366S_SSCR2_DROP_UNKNOWN_DA BIT(0)
56 #define RTL8366S_RESET_CTRL_REG 0x0100
57 #define RTL8366S_CHIP_CTRL_RESET_HW 1
58 #define RTL8366S_CHIP_CTRL_RESET_SW (1 << 1)
60 #define RTL8366S_CHIP_VERSION_CTRL_REG 0x0104
61 #define RTL8366S_CHIP_VERSION_MASK 0xf
62 #define RTL8366S_CHIP_ID_REG 0x0105
63 #define RTL8366S_CHIP_ID_8366 0x8366
65 /* PHY registers control */
66 #define RTL8366S_PHY_ACCESS_CTRL_REG 0x8028
67 #define RTL8366S_PHY_ACCESS_DATA_REG 0x8029
69 #define RTL8366S_PHY_CTRL_READ 1
70 #define RTL8366S_PHY_CTRL_WRITE 0
72 #define RTL8366S_PHY_REG_MASK 0x1f
73 #define RTL8366S_PHY_PAGE_OFFSET 5
74 #define RTL8366S_PHY_PAGE_MASK (0x7 << 5)
75 #define RTL8366S_PHY_NO_OFFSET 9
76 #define RTL8366S_PHY_NO_MASK (0x1f << 9)
78 /* LED control registers */
79 #define RTL8366S_LED_BLINKRATE_REG 0x0420
80 #define RTL8366S_LED_BLINKRATE_BIT 0
81 #define RTL8366S_LED_BLINKRATE_MASK 0x0007
83 #define RTL8366S_LED_CTRL_REG 0x0421
84 #define RTL8366S_LED_0_1_CTRL_REG 0x0422
85 #define RTL8366S_LED_2_3_CTRL_REG 0x0423
87 #define RTL8366S_MIB_COUNT 33
88 #define RTL8366S_GLOBAL_MIB_COUNT 1
89 #define RTL8366S_MIB_COUNTER_PORT_OFFSET 0x0040
90 #define RTL8366S_MIB_COUNTER_BASE 0x1000
91 #define RTL8366S_MIB_COUNTER_PORT_OFFSET2 0x0008
92 #define RTL8366S_MIB_COUNTER_BASE2 0x1180
93 #define RTL8366S_MIB_CTRL_REG 0x11F0
94 #define RTL8366S_MIB_CTRL_USER_MASK 0x01FF
95 #define RTL8366S_MIB_CTRL_BUSY_MASK 0x0001
96 #define RTL8366S_MIB_CTRL_RESET_MASK 0x0002
98 #define RTL8366S_MIB_CTRL_GLOBAL_RESET_MASK 0x0004
99 #define RTL8366S_MIB_CTRL_PORT_RESET_BIT 0x0003
100 #define RTL8366S_MIB_CTRL_PORT_RESET_MASK 0x01FC
103 #define RTL8366S_PORT_VLAN_CTRL_BASE 0x0058
104 #define RTL8366S_PORT_VLAN_CTRL_REG(_p) \
105 (RTL8366S_PORT_VLAN_CTRL_BASE + (_p) / 4)
106 #define RTL8366S_PORT_VLAN_CTRL_MASK 0xf
107 #define RTL8366S_PORT_VLAN_CTRL_SHIFT(_p) (4 * ((_p) % 4))
110 #define RTL8366S_VLAN_TABLE_READ_BASE 0x018B
111 #define RTL8366S_VLAN_TABLE_WRITE_BASE 0x0185
113 #define RTL8366S_VLAN_TB_CTRL_REG 0x010F
115 #define RTL8366S_TABLE_ACCESS_CTRL_REG 0x0180
116 #define RTL8366S_TABLE_VLAN_READ_CTRL 0x0E01
117 #define RTL8366S_TABLE_VLAN_WRITE_CTRL 0x0F01
119 #define RTL8366S_VLAN_MEMCONF_BASE 0x0016
122 #define RTL8366S_PORT_LINK_STATUS_BASE 0x0060
123 #define RTL8366S_PORT_STATUS_SPEED_MASK 0x0003
124 #define RTL8366S_PORT_STATUS_DUPLEX_MASK 0x0004
125 #define RTL8366S_PORT_STATUS_LINK_MASK 0x0010
126 #define RTL8366S_PORT_STATUS_TXPAUSE_MASK 0x0020
127 #define RTL8366S_PORT_STATUS_RXPAUSE_MASK 0x0040
128 #define RTL8366S_PORT_STATUS_AN_MASK 0x0080
131 #define RTL8366S_PORT_NUM_CPU 5
132 #define RTL8366S_NUM_PORTS 6
133 #define RTL8366S_NUM_VLANS 16
134 #define RTL8366S_NUM_LEDGROUPS 4
135 #define RTL8366S_NUM_VIDS 4096
136 #define RTL8366S_PRIORITYMAX 7
137 #define RTL8366S_FIDMAX 7
140 #define RTL8366S_PORT_1 (1 << 0) /* In userspace port 0 */
141 #define RTL8366S_PORT_2 (1 << 1) /* In userspace port 1 */
142 #define RTL8366S_PORT_3 (1 << 2) /* In userspace port 2 */
143 #define RTL8366S_PORT_4 (1 << 3) /* In userspace port 3 */
145 #define RTL8366S_PORT_UNKNOWN (1 << 4) /* No known connection */
146 #define RTL8366S_PORT_CPU (1 << 5) /* CPU port */
148 #define RTL8366S_PORT_ALL (RTL8366S_PORT_1 | \
152 RTL8366S_PORT_UNKNOWN | \
155 #define RTL8366S_PORT_ALL_BUT_CPU (RTL8366S_PORT_1 | \
159 RTL8366S_PORT_UNKNOWN)
161 #define RTL8366S_PORT_ALL_EXTERNAL (RTL8366S_PORT_1 | \
166 #define RTL8366S_PORT_ALL_INTERNAL (RTL8366S_PORT_UNKNOWN | \
170 struct device
*parent
;
171 struct rtl8366_smi smi
;
172 struct switch_dev dev
;
174 #ifdef CONFIG_RTL8366S_PHY_DEBUG_FS
175 struct dentry
*debugfs_root
;
179 struct rtl8366s_vlan_mc
{
190 struct rtl8366s_vlan_4k
{
200 #ifdef CONFIG_RTL8366S_PHY_DEBUG_FS
211 static struct mib_counter rtl8366s_mib_counters
[RTL8366S_MIB_COUNT
] = {
212 { 0, 0, 4, "IfInOctets" },
213 { 0, 4, 4, "EtherStatsOctets" },
214 { 0, 8, 2, "EtherStatsUnderSizePkts" },
215 { 0, 10, 2, "EtherFragments" },
216 { 0, 12, 2, "EtherStatsPkts64Octets" },
217 { 0, 14, 2, "EtherStatsPkts65to127Octets" },
218 { 0, 16, 2, "EtherStatsPkts128to255Octets" },
219 { 0, 18, 2, "EtherStatsPkts256to511Octets" },
220 { 0, 20, 2, "EtherStatsPkts512to1023Octets" },
221 { 0, 22, 2, "EtherStatsPkts1024to1518Octets" },
222 { 0, 24, 2, "EtherOversizeStats" },
223 { 0, 26, 2, "EtherStatsJabbers" },
224 { 0, 28, 2, "IfInUcastPkts" },
225 { 0, 30, 2, "EtherStatsMulticastPkts" },
226 { 0, 32, 2, "EtherStatsBroadcastPkts" },
227 { 0, 34, 2, "EtherStatsDropEvents" },
228 { 0, 36, 2, "Dot3StatsFCSErrors" },
229 { 0, 38, 2, "Dot3StatsSymbolErrors" },
230 { 0, 40, 2, "Dot3InPauseFrames" },
231 { 0, 42, 2, "Dot3ControlInUnknownOpcodes" },
232 { 0, 44, 4, "IfOutOctets" },
233 { 0, 48, 2, "Dot3StatsSingleCollisionFrames" },
234 { 0, 50, 2, "Dot3StatMultipleCollisionFrames" },
235 { 0, 52, 2, "Dot3sDeferredTransmissions" },
236 { 0, 54, 2, "Dot3StatsLateCollisions" },
237 { 0, 56, 2, "EtherStatsCollisions" },
238 { 0, 58, 2, "Dot3StatsExcessiveCollisions" },
239 { 0, 60, 2, "Dot3OutPauseFrames" },
240 { 0, 62, 2, "Dot1dBasePortDelayExceededDiscards" },
243 * The following counters are accessible at a different
246 { 1, 0, 2, "Dot1dTpPortInDiscards" },
247 { 1, 2, 2, "IfOutUcastPkts" },
248 { 1, 4, 2, "IfOutMulticastPkts" },
249 { 1, 6, 2, "IfOutBroadcastPkts" },
252 #define REG_WR(_smi, _reg, _val) \
254 err = rtl8366_smi_write_reg(_smi, _reg, _val); \
259 #define REG_RMW(_smi, _reg, _mask, _val) \
261 err = rtl8366_smi_rmwr(_smi, _reg, _mask, _val); \
266 static inline struct rtl8366s
*smi_to_rtl8366s(struct rtl8366_smi
*smi
)
268 return container_of(smi
, struct rtl8366s
, smi
);
271 static inline struct rtl8366s
*sw_to_rtl8366s(struct switch_dev
*sw
)
273 return container_of(sw
, struct rtl8366s
, dev
);
276 static inline struct rtl8366_smi
*sw_to_rtl8366_smi(struct switch_dev
*sw
)
278 struct rtl8366s
*rtl
= sw_to_rtl8366s(sw
);
282 static int rtl8366s_reset_chip(struct rtl8366_smi
*smi
)
287 rtl8366_smi_write_reg(smi
, RTL8366S_RESET_CTRL_REG
,
288 RTL8366S_CHIP_CTRL_RESET_HW
);
291 if (rtl8366_smi_read_reg(smi
, RTL8366S_RESET_CTRL_REG
, &data
))
294 if (!(data
& RTL8366S_CHIP_CTRL_RESET_HW
))
299 printk("Timeout waiting for the switch to reset\n");
306 static int rtl8366s_hw_init(struct rtl8366_smi
*smi
)
310 /* set maximum packet length to 1536 bytes */
311 REG_RMW(smi
, RTL8366S_SGCR
, RTL8366S_SGCR_MAX_LENGTH_MASK
,
312 RTL8366S_SGCR_MAX_LENGTH_1536
);
314 /* enable all ports */
315 REG_WR(smi
, RTL8366S_PECR
, 0);
317 /* disable learning for all ports */
318 REG_WR(smi
, RTL8366S_SSCR0
, RTL8366S_PORT_ALL
);
320 /* disable auto ageing for all ports */
321 REG_WR(smi
, RTL8366S_SSCR1
, RTL8366S_PORT_ALL
);
323 /* don't drop packets whose DA has not been learned */
324 REG_RMW(smi
, RTL8366S_SSCR2
, RTL8366S_SSCR2_DROP_UNKNOWN_DA
, 0);
329 static int rtl8366s_read_phy_reg(struct rtl8366_smi
*smi
,
330 u32 phy_no
, u32 page
, u32 addr
, u32
*data
)
335 if (phy_no
> RTL8366S_PHY_NO_MAX
)
338 if (page
> RTL8366S_PHY_PAGE_MAX
)
341 if (addr
> RTL8366S_PHY_ADDR_MAX
)
344 ret
= rtl8366_smi_write_reg(smi
, RTL8366S_PHY_ACCESS_CTRL_REG
,
345 RTL8366S_PHY_CTRL_READ
);
349 reg
= 0x8000 | (1 << (phy_no
+ RTL8366S_PHY_NO_OFFSET
)) |
350 ((page
<< RTL8366S_PHY_PAGE_OFFSET
) & RTL8366S_PHY_PAGE_MASK
) |
351 (addr
& RTL8366S_PHY_REG_MASK
);
353 ret
= rtl8366_smi_write_reg(smi
, reg
, 0);
357 ret
= rtl8366_smi_read_reg(smi
, RTL8366S_PHY_ACCESS_DATA_REG
, data
);
364 static int rtl8366s_write_phy_reg(struct rtl8366_smi
*smi
,
365 u32 phy_no
, u32 page
, u32 addr
, u32 data
)
370 if (phy_no
> RTL8366S_PHY_NO_MAX
)
373 if (page
> RTL8366S_PHY_PAGE_MAX
)
376 if (addr
> RTL8366S_PHY_ADDR_MAX
)
379 ret
= rtl8366_smi_write_reg(smi
, RTL8366S_PHY_ACCESS_CTRL_REG
,
380 RTL8366S_PHY_CTRL_WRITE
);
384 reg
= 0x8000 | (1 << (phy_no
+ RTL8366S_PHY_NO_OFFSET
)) |
385 ((page
<< RTL8366S_PHY_PAGE_OFFSET
) & RTL8366S_PHY_PAGE_MASK
) |
386 (addr
& RTL8366S_PHY_REG_MASK
);
388 ret
= rtl8366_smi_write_reg(smi
, reg
, data
);
395 static int rtl8366_get_mib_counter(struct rtl8366_smi
*smi
, int counter
,
396 int port
, unsigned long long *val
)
403 if (port
> RTL8366S_NUM_PORTS
|| counter
>= RTL8366S_MIB_COUNT
)
406 switch (rtl8366s_mib_counters
[counter
].base
) {
408 addr
= RTL8366S_MIB_COUNTER_BASE
+
409 RTL8366S_MIB_COUNTER_PORT_OFFSET
* port
;
413 addr
= RTL8366S_MIB_COUNTER_BASE2
+
414 RTL8366S_MIB_COUNTER_PORT_OFFSET2
* port
;
421 addr
+= rtl8366s_mib_counters
[counter
].offset
;
424 * Writing access counter address first
425 * then ASIC will prepare 64bits counter wait for being retrived
427 data
= 0; /* writing data will be discard by ASIC */
428 err
= rtl8366_smi_write_reg(smi
, addr
, data
);
432 /* read MIB control register */
433 err
= rtl8366_smi_read_reg(smi
, RTL8366S_MIB_CTRL_REG
, &data
);
437 if (data
& RTL8366S_MIB_CTRL_BUSY_MASK
)
440 if (data
& RTL8366S_MIB_CTRL_RESET_MASK
)
444 for (i
= rtl8366s_mib_counters
[counter
].length
; i
> 0; i
--) {
445 err
= rtl8366_smi_read_reg(smi
, addr
+ (i
- 1), &data
);
449 mibvalue
= (mibvalue
<< 16) | (data
& 0xFFFF);
456 static int rtl8366s_get_vlan_4k(struct rtl8366_smi
*smi
, u32 vid
,
457 struct rtl8366_vlan_4k
*vlan4k
)
459 struct rtl8366s_vlan_4k vlan4k_priv
;
464 memset(vlan4k
, '\0', sizeof(struct rtl8366_vlan_4k
));
465 vlan4k_priv
.vid
= vid
;
467 if (vid
>= RTL8366S_NUM_VIDS
)
470 tableaddr
= (u16
*)&vlan4k_priv
;
474 err
= rtl8366_smi_write_reg(smi
, RTL8366S_VLAN_TABLE_WRITE_BASE
, data
);
478 /* write table access control word */
479 err
= rtl8366_smi_write_reg(smi
, RTL8366S_TABLE_ACCESS_CTRL_REG
,
480 RTL8366S_TABLE_VLAN_READ_CTRL
);
484 err
= rtl8366_smi_read_reg(smi
, RTL8366S_VLAN_TABLE_READ_BASE
, &data
);
491 err
= rtl8366_smi_read_reg(smi
, RTL8366S_VLAN_TABLE_READ_BASE
+ 1,
499 vlan4k
->untag
= vlan4k_priv
.untag
;
500 vlan4k
->member
= vlan4k_priv
.member
;
501 vlan4k
->fid
= vlan4k_priv
.fid
;
506 static int rtl8366s_set_vlan_4k(struct rtl8366_smi
*smi
,
507 const struct rtl8366_vlan_4k
*vlan4k
)
509 struct rtl8366s_vlan_4k vlan4k_priv
;
514 if (vlan4k
->vid
>= RTL8366S_NUM_VIDS
||
515 vlan4k
->member
> RTL8366S_PORT_ALL
||
516 vlan4k
->untag
> RTL8366S_PORT_ALL
||
517 vlan4k
->fid
> RTL8366S_FIDMAX
)
520 vlan4k_priv
.vid
= vlan4k
->vid
;
521 vlan4k_priv
.untag
= vlan4k
->untag
;
522 vlan4k_priv
.member
= vlan4k
->member
;
523 vlan4k_priv
.fid
= vlan4k
->fid
;
525 tableaddr
= (u16
*)&vlan4k_priv
;
529 err
= rtl8366_smi_write_reg(smi
, RTL8366S_VLAN_TABLE_WRITE_BASE
, data
);
537 err
= rtl8366_smi_write_reg(smi
, RTL8366S_VLAN_TABLE_WRITE_BASE
+ 1,
542 /* write table access control word */
543 err
= rtl8366_smi_write_reg(smi
, RTL8366S_TABLE_ACCESS_CTRL_REG
,
544 RTL8366S_TABLE_VLAN_WRITE_CTRL
);
549 static int rtl8366s_get_vlan_mc(struct rtl8366_smi
*smi
, u32 index
,
550 struct rtl8366_vlan_mc
*vlanmc
)
552 struct rtl8366s_vlan_mc vlanmc_priv
;
558 memset(vlanmc
, '\0', sizeof(struct rtl8366_vlan_mc
));
560 if (index
>= RTL8366S_NUM_VLANS
)
563 tableaddr
= (u16
*)&vlanmc_priv
;
565 addr
= RTL8366S_VLAN_MEMCONF_BASE
+ (index
<< 1);
566 err
= rtl8366_smi_read_reg(smi
, addr
, &data
);
573 addr
= RTL8366S_VLAN_MEMCONF_BASE
+ 1 + (index
<< 1);
574 err
= rtl8366_smi_read_reg(smi
, addr
, &data
);
580 vlanmc
->vid
= vlanmc_priv
.vid
;
581 vlanmc
->priority
= vlanmc_priv
.priority
;
582 vlanmc
->untag
= vlanmc_priv
.untag
;
583 vlanmc
->member
= vlanmc_priv
.member
;
584 vlanmc
->fid
= vlanmc_priv
.fid
;
589 static int rtl8366s_set_vlan_mc(struct rtl8366_smi
*smi
, u32 index
,
590 const struct rtl8366_vlan_mc
*vlanmc
)
592 struct rtl8366s_vlan_mc vlanmc_priv
;
598 if (index
>= RTL8366S_NUM_VLANS
||
599 vlanmc
->vid
>= RTL8366S_NUM_VIDS
||
600 vlanmc
->priority
> RTL8366S_PRIORITYMAX
||
601 vlanmc
->member
> RTL8366S_PORT_ALL
||
602 vlanmc
->untag
> RTL8366S_PORT_ALL
||
603 vlanmc
->fid
> RTL8366S_FIDMAX
)
606 vlanmc_priv
.vid
= vlanmc
->vid
;
607 vlanmc_priv
.priority
= vlanmc
->priority
;
608 vlanmc_priv
.untag
= vlanmc
->untag
;
609 vlanmc_priv
.member
= vlanmc
->member
;
610 vlanmc_priv
.fid
= vlanmc
->fid
;
612 addr
= RTL8366S_VLAN_MEMCONF_BASE
+ (index
<< 1);
614 tableaddr
= (u16
*)&vlanmc_priv
;
617 err
= rtl8366_smi_write_reg(smi
, addr
, data
);
621 addr
= RTL8366S_VLAN_MEMCONF_BASE
+ 1 + (index
<< 1);
626 err
= rtl8366_smi_write_reg(smi
, addr
, data
);
633 static int rtl8366s_get_mc_index(struct rtl8366_smi
*smi
, int port
, int *val
)
638 if (port
>= RTL8366S_NUM_PORTS
)
641 err
= rtl8366_smi_read_reg(smi
, RTL8366S_PORT_VLAN_CTRL_REG(port
),
646 *val
= (data
>> RTL8366S_PORT_VLAN_CTRL_SHIFT(port
)) &
647 RTL8366S_PORT_VLAN_CTRL_MASK
;
652 static int rtl8366s_set_mc_index(struct rtl8366_smi
*smi
, int port
, int index
)
654 if (port
>= RTL8366S_NUM_PORTS
|| index
>= RTL8366S_NUM_VLANS
)
657 return rtl8366_smi_rmwr(smi
, RTL8366S_PORT_VLAN_CTRL_REG(port
),
658 RTL8366S_PORT_VLAN_CTRL_MASK
<<
659 RTL8366S_PORT_VLAN_CTRL_SHIFT(port
),
660 (index
& RTL8366S_PORT_VLAN_CTRL_MASK
) <<
661 RTL8366S_PORT_VLAN_CTRL_SHIFT(port
));
664 static int rtl8366s_set_vlan(struct rtl8366_smi
*smi
, int vid
, u32 member
,
667 struct rtl8366_vlan_4k vlan4k
;
671 /* Update the 4K table */
672 err
= smi
->ops
->get_vlan_4k(smi
, vid
, &vlan4k
);
676 vlan4k
.member
= member
;
677 vlan4k
.untag
= untag
;
679 err
= smi
->ops
->set_vlan_4k(smi
, &vlan4k
);
683 /* Try to find an existing MC entry for this VID */
684 for (i
= 0; i
< RTL8366S_NUM_VLANS
; i
++) {
685 struct rtl8366_vlan_mc vlanmc
;
687 err
= smi
->ops
->get_vlan_mc(smi
, i
, &vlanmc
);
691 if (vid
== vlanmc
.vid
) {
692 /* update the MC entry */
693 vlanmc
.member
= member
;
694 vlanmc
.untag
= untag
;
697 err
= smi
->ops
->set_vlan_mc(smi
, i
, &vlanmc
);
705 static int rtl8366s_get_pvid(struct rtl8366_smi
*smi
, int port
, int *val
)
707 struct rtl8366_vlan_mc vlanmc
;
711 err
= smi
->ops
->get_mc_index(smi
, port
, &index
);
715 err
= smi
->ops
->get_vlan_mc(smi
, index
, &vlanmc
);
723 static int rtl8366s_mc_is_used(struct rtl8366_smi
*smi
, int mc_index
,
730 for (i
= 0; i
< RTL8366S_NUM_PORTS
; i
++) {
733 err
= smi
->ops
->get_mc_index(smi
, i
, &index
);
737 if (mc_index
== index
) {
746 static int rtl8366s_set_pvid(struct rtl8366_smi
*smi
, unsigned port
,
749 struct rtl8366_vlan_mc vlanmc
;
750 struct rtl8366_vlan_4k vlan4k
;
754 /* Try to find an existing MC entry for this VID */
755 for (i
= 0; i
< RTL8366S_NUM_VLANS
; i
++) {
756 err
= smi
->ops
->get_vlan_mc(smi
, i
, &vlanmc
);
760 if (vid
== vlanmc
.vid
) {
761 err
= smi
->ops
->set_vlan_mc(smi
, i
, &vlanmc
);
765 err
= smi
->ops
->set_mc_index(smi
, port
, i
);
770 /* We have no MC entry for this VID, try to find an empty one */
771 for (i
= 0; i
< RTL8366S_NUM_VLANS
; i
++) {
772 err
= smi
->ops
->get_vlan_mc(smi
, i
, &vlanmc
);
776 if (vlanmc
.vid
== 0 && vlanmc
.member
== 0) {
777 /* Update the entry from the 4K table */
778 err
= smi
->ops
->get_vlan_4k(smi
, vid
, &vlan4k
);
783 vlanmc
.member
= vlan4k
.member
;
784 vlanmc
.untag
= vlan4k
.untag
;
785 vlanmc
.fid
= vlan4k
.fid
;
786 err
= smi
->ops
->set_vlan_mc(smi
, i
, &vlanmc
);
790 err
= smi
->ops
->set_mc_index(smi
, port
, i
);
795 /* MC table is full, try to find an unused entry and replace it */
796 for (i
= 0; i
< RTL8366S_NUM_VLANS
; i
++) {
799 err
= rtl8366s_mc_is_used(smi
, i
, &used
);
804 /* Update the entry from the 4K table */
805 err
= smi
->ops
->get_vlan_4k(smi
, vid
, &vlan4k
);
810 vlanmc
.member
= vlan4k
.member
;
811 vlanmc
.untag
= vlan4k
.untag
;
812 vlanmc
.fid
= vlan4k
.fid
;
813 err
= smi
->ops
->set_vlan_mc(smi
, i
, &vlanmc
);
817 err
= smi
->ops
->set_mc_index(smi
, port
, i
);
823 "all VLAN member configurations are in use\n");
828 static int rtl8366s_vlan_set_vlan(struct rtl8366_smi
*smi
, int enable
)
830 return rtl8366_smi_rmwr(smi
, RTL8366S_CHIP_GLOBAL_CTRL_REG
,
831 RTL8366S_CHIP_CTRL_VLAN
,
832 (enable
) ? RTL8366S_CHIP_CTRL_VLAN
: 0);
835 static int rtl8366s_vlan_set_4ktable(struct rtl8366_smi
*smi
, int enable
)
837 return rtl8366_smi_rmwr(smi
, RTL8366S_VLAN_TB_CTRL_REG
,
838 1, (enable
) ? 1 : 0);
841 static int rtl8366s_reset_vlan(struct rtl8366_smi
*smi
)
843 struct rtl8366_vlan_mc vlanmc
;
847 /* clear VLAN member configurations */
853 for (i
= 0; i
< RTL8366S_NUM_VLANS
; i
++) {
854 err
= smi
->ops
->set_vlan_mc(smi
, i
, &vlanmc
);
859 for (i
= 0; i
< RTL8366S_NUM_PORTS
; i
++) {
860 if (i
== RTL8366S_PORT_CPU
)
863 err
= rtl8366s_set_vlan(smi
, (i
+ 1),
864 (1 << i
) | RTL8366S_PORT_CPU
,
865 (1 << i
) | RTL8366S_PORT_CPU
,
870 err
= rtl8366s_set_pvid(smi
, i
, (i
+ 1));
878 #ifdef CONFIG_RTL8366S_PHY_DEBUG_FS
879 static int rtl8366s_debugfs_open(struct inode
*inode
, struct file
*file
)
881 file
->private_data
= inode
->i_private
;
885 static ssize_t
rtl8366s_read_debugfs_mibs(struct file
*file
,
886 char __user
*user_buf
,
887 size_t count
, loff_t
*ppos
)
889 struct rtl8366s
*rtl
= (struct rtl8366s
*)file
->private_data
;
890 struct rtl8366_smi
*smi
= &rtl
->smi
;
892 char *buf
= rtl
->buf
;
894 len
+= snprintf(buf
+ len
, sizeof(rtl
->buf
) - len
,
895 "%-36s %12s %12s %12s %12s %12s %12s\n",
897 "Port 0", "Port 1", "Port 2",
898 "Port 3", "Port 4", "Port 5");
900 for (i
= 0; i
< ARRAY_SIZE(rtl8366s_mib_counters
); ++i
) {
901 len
+= snprintf(buf
+ len
, sizeof(rtl
->buf
) - len
, "%-36s ",
902 rtl8366s_mib_counters
[i
].name
);
903 for (j
= 0; j
< RTL8366S_NUM_PORTS
; ++j
) {
904 unsigned long long counter
= 0;
906 if (!rtl8366_get_mib_counter(smi
, i
, j
, &counter
))
907 len
+= snprintf(buf
+ len
,
908 sizeof(rtl
->buf
) - len
,
911 len
+= snprintf(buf
+ len
,
912 sizeof(rtl
->buf
) - len
,
915 len
+= snprintf(buf
+ len
, sizeof(rtl
->buf
) - len
, "\n");
918 return simple_read_from_buffer(user_buf
, count
, ppos
, buf
, len
);
921 static ssize_t
rtl8366s_read_debugfs_vlan_mc(struct file
*file
,
922 char __user
*user_buf
,
923 size_t count
, loff_t
*ppos
)
925 struct rtl8366s
*rtl
= (struct rtl8366s
*)file
->private_data
;
926 struct rtl8366_smi
*smi
= &rtl
->smi
;
928 char *buf
= rtl
->buf
;
930 len
+= snprintf(buf
+ len
, sizeof(rtl
->buf
) - len
,
931 "%2s %6s %4s %6s %6s %3s\n",
932 "id", "vid","prio", "member", "untag", "fid");
934 for (i
= 0; i
< RTL8366S_NUM_VLANS
; ++i
) {
935 struct rtl8366_vlan_mc vlanmc
;
937 rtl8366s_get_vlan_mc(smi
, i
, &vlanmc
);
939 len
+= snprintf(buf
+ len
, sizeof(rtl
->buf
) - len
,
940 "%2d %6d %4d 0x%04x 0x%04x %3d\n",
941 i
, vlanmc
.vid
, vlanmc
.priority
,
942 vlanmc
.member
, vlanmc
.untag
, vlanmc
.fid
);
945 return simple_read_from_buffer(user_buf
, count
, ppos
, buf
, len
);
948 static ssize_t
rtl8366s_read_debugfs_reg(struct file
*file
,
949 char __user
*user_buf
,
950 size_t count
, loff_t
*ppos
)
952 struct rtl8366s
*rtl
= (struct rtl8366s
*)file
->private_data
;
953 struct rtl8366_smi
*smi
= &rtl
->smi
;
954 u32 t
, reg
= g_dbg_reg
;
956 char *buf
= rtl
->buf
;
958 memset(buf
, '\0', sizeof(rtl
->buf
));
960 err
= rtl8366_smi_read_reg(smi
, reg
, &t
);
962 len
+= snprintf(buf
, sizeof(rtl
->buf
),
963 "Read failed (reg: 0x%04x)\n", reg
);
964 return simple_read_from_buffer(user_buf
, count
, ppos
, buf
, len
);
967 len
+= snprintf(buf
, sizeof(rtl
->buf
), "reg = 0x%04x, val = 0x%04x\n",
970 return simple_read_from_buffer(user_buf
, count
, ppos
, buf
, len
);
973 static ssize_t
rtl8366s_write_debugfs_reg(struct file
*file
,
974 const char __user
*user_buf
,
975 size_t count
, loff_t
*ppos
)
977 struct rtl8366s
*rtl
= (struct rtl8366s
*)file
->private_data
;
978 struct rtl8366_smi
*smi
= &rtl
->smi
;
983 char *buf
= rtl
->buf
;
985 len
= min(count
, sizeof(rtl
->buf
) - 1);
986 if (copy_from_user(buf
, user_buf
, len
)) {
987 dev_err(rtl
->parent
, "copy from user failed\n");
992 if (len
> 0 && buf
[len
- 1] == '\n')
996 if (strict_strtoul(buf
, 16, &data
)) {
997 dev_err(rtl
->parent
, "Invalid reg value %s\n", buf
);
999 err
= rtl8366_smi_write_reg(smi
, reg
, data
);
1001 dev_err(rtl
->parent
,
1002 "writing reg 0x%04x val 0x%04lx failed\n",
1010 static const struct file_operations fops_rtl8366s_regs
= {
1011 .read
= rtl8366s_read_debugfs_reg
,
1012 .write
= rtl8366s_write_debugfs_reg
,
1013 .open
= rtl8366s_debugfs_open
,
1014 .owner
= THIS_MODULE
1017 static const struct file_operations fops_rtl8366s_vlan_mc
= {
1018 .read
= rtl8366s_read_debugfs_vlan_mc
,
1019 .open
= rtl8366s_debugfs_open
,
1020 .owner
= THIS_MODULE
1023 static const struct file_operations fops_rtl8366s_mibs
= {
1024 .read
= rtl8366s_read_debugfs_mibs
,
1025 .open
= rtl8366s_debugfs_open
,
1026 .owner
= THIS_MODULE
1029 static void rtl8366s_debugfs_init(struct rtl8366s
*rtl
)
1031 struct dentry
*node
;
1032 struct dentry
*root
;
1034 if (!rtl
->debugfs_root
)
1035 rtl
->debugfs_root
= debugfs_create_dir("rtl8366s", NULL
);
1037 if (!rtl
->debugfs_root
) {
1038 dev_err(rtl
->parent
, "Unable to create debugfs dir\n");
1041 root
= rtl
->debugfs_root
;
1043 node
= debugfs_create_x16("reg", S_IRUGO
| S_IWUSR
, root
, &g_dbg_reg
);
1045 dev_err(rtl
->parent
, "Creating debugfs file '%s' failed\n",
1050 node
= debugfs_create_file("val", S_IRUGO
| S_IWUSR
, root
, rtl
,
1051 &fops_rtl8366s_regs
);
1053 dev_err(rtl
->parent
, "Creating debugfs file '%s' failed\n",
1058 node
= debugfs_create_file("vlan_mc", S_IRUSR
, root
, rtl
,
1059 &fops_rtl8366s_vlan_mc
);
1061 dev_err(rtl
->parent
, "Creating debugfs file '%s' failed\n",
1066 node
= debugfs_create_file("mibs", S_IRUSR
, root
, rtl
,
1067 &fops_rtl8366s_mibs
);
1069 dev_err(rtl
->parent
, "Creating debugfs file '%s' failed\n",
1075 static void rtl8366s_debugfs_remove(struct rtl8366s
*rtl
)
1077 if (rtl
->debugfs_root
) {
1078 debugfs_remove_recursive(rtl
->debugfs_root
);
1079 rtl
->debugfs_root
= NULL
;
1084 static inline void rtl8366s_debugfs_init(struct rtl8366s
*rtl
) {}
1085 static inline void rtl8366s_debugfs_remove(struct rtl8366s
*rtl
) {}
1086 #endif /* CONFIG_RTL8366S_PHY_DEBUG_FS */
1088 static int rtl8366s_sw_reset_mibs(struct switch_dev
*dev
,
1089 const struct switch_attr
*attr
,
1090 struct switch_val
*val
)
1092 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
1095 if (val
->value
.i
== 1)
1096 err
= rtl8366_smi_rmwr(smi
, RTL8366S_MIB_CTRL_REG
, 0, (1 << 2));
1101 static int rtl8366s_sw_get_vlan_enable(struct switch_dev
*dev
,
1102 const struct switch_attr
*attr
,
1103 struct switch_val
*val
)
1105 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
1108 if (attr
->ofs
== 1) {
1109 rtl8366_smi_read_reg(smi
, RTL8366S_CHIP_GLOBAL_CTRL_REG
, &data
);
1111 if (data
& RTL8366S_CHIP_CTRL_VLAN
)
1115 } else if (attr
->ofs
== 2) {
1116 rtl8366_smi_read_reg(smi
, RTL8366S_VLAN_TB_CTRL_REG
, &data
);
1127 static int rtl8366s_sw_get_blinkrate(struct switch_dev
*dev
,
1128 const struct switch_attr
*attr
,
1129 struct switch_val
*val
)
1131 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
1134 rtl8366_smi_read_reg(smi
, RTL8366S_LED_BLINKRATE_REG
, &data
);
1136 val
->value
.i
= (data
& (RTL8366S_LED_BLINKRATE_MASK
));
1141 static int rtl8366s_sw_set_blinkrate(struct switch_dev
*dev
,
1142 const struct switch_attr
*attr
,
1143 struct switch_val
*val
)
1145 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
1147 if (val
->value
.i
>= 6)
1150 return rtl8366_smi_rmwr(smi
, RTL8366S_LED_BLINKRATE_REG
,
1151 RTL8366S_LED_BLINKRATE_MASK
,
1155 static int rtl8366s_sw_set_vlan_enable(struct switch_dev
*dev
,
1156 const struct switch_attr
*attr
,
1157 struct switch_val
*val
)
1159 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
1162 return rtl8366s_vlan_set_vlan(smi
, val
->value
.i
);
1164 return rtl8366s_vlan_set_4ktable(smi
, val
->value
.i
);
1167 static const char *rtl8366s_speed_str(unsigned speed
)
1181 static int rtl8366s_sw_get_port_link(struct switch_dev
*dev
,
1182 const struct switch_attr
*attr
,
1183 struct switch_val
*val
)
1185 struct rtl8366s
*rtl
= sw_to_rtl8366s(dev
);
1186 struct rtl8366_smi
*smi
= &rtl
->smi
;
1187 u32 len
= 0, data
= 0;
1189 if (val
->port_vlan
>= RTL8366S_NUM_PORTS
)
1192 memset(rtl
->buf
, '\0', sizeof(rtl
->buf
));
1193 rtl8366_smi_read_reg(smi
, RTL8366S_PORT_LINK_STATUS_BASE
+
1194 (val
->port_vlan
/ 2), &data
);
1196 if (val
->port_vlan
% 2)
1199 if (data
& RTL8366S_PORT_STATUS_LINK_MASK
) {
1200 len
= snprintf(rtl
->buf
, sizeof(rtl
->buf
),
1201 "port:%d link:up speed:%s %s-duplex %s%s%s",
1203 rtl8366s_speed_str(data
&
1204 RTL8366S_PORT_STATUS_SPEED_MASK
),
1205 (data
& RTL8366S_PORT_STATUS_DUPLEX_MASK
) ?
1207 (data
& RTL8366S_PORT_STATUS_TXPAUSE_MASK
) ?
1209 (data
& RTL8366S_PORT_STATUS_RXPAUSE_MASK
) ?
1211 (data
& RTL8366S_PORT_STATUS_AN_MASK
) ?
1214 len
= snprintf(rtl
->buf
, sizeof(rtl
->buf
), "port:%d link: down",
1218 val
->value
.s
= rtl
->buf
;
1224 static int rtl8366s_sw_get_vlan_info(struct switch_dev
*dev
,
1225 const struct switch_attr
*attr
,
1226 struct switch_val
*val
)
1230 struct rtl8366_vlan_4k vlan4k
;
1231 struct rtl8366s
*rtl
= sw_to_rtl8366s(dev
);
1232 struct rtl8366_smi
*smi
= &rtl
->smi
;
1233 char *buf
= rtl
->buf
;
1236 if (val
->port_vlan
== 0 || val
->port_vlan
>= RTL8366S_NUM_VLANS
)
1239 memset(buf
, '\0', sizeof(rtl
->buf
));
1241 err
= rtl8366s_get_vlan_4k(smi
, val
->port_vlan
, &vlan4k
);
1245 len
+= snprintf(buf
+ len
, sizeof(rtl
->buf
) - len
,
1246 "VLAN %d: Ports: '", vlan4k
.vid
);
1248 for (i
= 0; i
< RTL8366S_NUM_PORTS
; i
++) {
1249 if (!(vlan4k
.member
& (1 << i
)))
1252 len
+= snprintf(buf
+ len
, sizeof(rtl
->buf
) - len
, "%d%s", i
,
1253 (vlan4k
.untag
& (1 << i
)) ? "" : "t");
1256 len
+= snprintf(buf
+ len
, sizeof(rtl
->buf
) - len
,
1257 "', members=%04x, untag=%04x, fid=%u",
1258 vlan4k
.member
, vlan4k
.untag
, vlan4k
.fid
);
1266 static int rtl8366s_sw_set_port_led(struct switch_dev
*dev
,
1267 const struct switch_attr
*attr
,
1268 struct switch_val
*val
)
1270 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
1275 if (val
->port_vlan
>= RTL8366S_NUM_PORTS
||
1276 (1 << val
->port_vlan
) == RTL8366S_PORT_UNKNOWN
)
1279 if (val
->port_vlan
== RTL8366S_PORT_NUM_CPU
) {
1280 reg
= RTL8366S_LED_BLINKRATE_REG
;
1282 data
= val
->value
.i
<< 4;
1284 reg
= RTL8366S_LED_CTRL_REG
;
1285 mask
= 0xF << (val
->port_vlan
* 4),
1286 data
= val
->value
.i
<< (val
->port_vlan
* 4);
1289 return rtl8366_smi_rmwr(smi
, RTL8366S_LED_BLINKRATE_REG
, mask
, data
);
1292 static int rtl8366s_sw_get_port_led(struct switch_dev
*dev
,
1293 const struct switch_attr
*attr
,
1294 struct switch_val
*val
)
1296 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
1299 if (val
->port_vlan
>= RTL8366S_NUM_LEDGROUPS
)
1302 rtl8366_smi_read_reg(smi
, RTL8366S_LED_CTRL_REG
, &data
);
1303 val
->value
.i
= (data
>> (val
->port_vlan
* 4)) & 0x000F;
1308 static int rtl8366s_sw_reset_port_mibs(struct switch_dev
*dev
,
1309 const struct switch_attr
*attr
,
1310 struct switch_val
*val
)
1312 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
1314 if (val
->port_vlan
>= RTL8366S_NUM_PORTS
)
1318 return rtl8366_smi_rmwr(smi
, RTL8366S_MIB_CTRL_REG
,
1319 0, (1 << (val
->port_vlan
+ 3)));
1322 static int rtl8366s_sw_get_port_mib(struct switch_dev
*dev
,
1323 const struct switch_attr
*attr
,
1324 struct switch_val
*val
)
1326 struct rtl8366s
*rtl
= sw_to_rtl8366s(dev
);
1327 struct rtl8366_smi
*smi
= &rtl
->smi
;
1329 unsigned long long counter
= 0;
1330 char *buf
= rtl
->buf
;
1332 if (val
->port_vlan
>= RTL8366S_NUM_PORTS
)
1335 len
+= snprintf(buf
+ len
, sizeof(rtl
->buf
) - len
,
1336 "Port %d MIB counters\n",
1339 for (i
= 0; i
< ARRAY_SIZE(rtl8366s_mib_counters
); ++i
) {
1340 len
+= snprintf(buf
+ len
, sizeof(rtl
->buf
) - len
,
1341 "%-36s: ", rtl8366s_mib_counters
[i
].name
);
1342 if (!rtl8366_get_mib_counter(smi
, i
, val
->port_vlan
, &counter
))
1343 len
+= snprintf(buf
+ len
, sizeof(rtl
->buf
) - len
,
1346 len
+= snprintf(buf
+ len
, sizeof(rtl
->buf
) - len
,
1355 static int rtl8366s_sw_get_vlan_ports(struct switch_dev
*dev
,
1356 struct switch_val
*val
)
1358 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
1359 struct switch_port
*port
;
1360 struct rtl8366_vlan_4k vlan4k
;
1363 if (val
->port_vlan
== 0 || val
->port_vlan
>= RTL8366S_NUM_VLANS
)
1366 rtl8366s_get_vlan_4k(smi
, val
->port_vlan
, &vlan4k
);
1368 port
= &val
->value
.ports
[0];
1370 for (i
= 0; i
< RTL8366S_NUM_PORTS
; i
++) {
1371 if (!(vlan4k
.member
& BIT(i
)))
1375 port
->flags
= (vlan4k
.untag
& BIT(i
)) ?
1376 0 : BIT(SWITCH_PORT_FLAG_TAGGED
);
1383 static int rtl8366s_sw_set_vlan_ports(struct switch_dev
*dev
,
1384 struct switch_val
*val
)
1386 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
1387 struct switch_port
*port
;
1392 if (val
->port_vlan
== 0 || val
->port_vlan
>= RTL8366S_NUM_VLANS
)
1395 port
= &val
->value
.ports
[0];
1396 for (i
= 0; i
< val
->len
; i
++, port
++) {
1397 member
|= BIT(port
->id
);
1399 if (!(port
->flags
& BIT(SWITCH_PORT_FLAG_TAGGED
)))
1400 untag
|= BIT(port
->id
);
1403 return rtl8366s_set_vlan(smi
, val
->port_vlan
, member
, untag
, 0);
1406 static int rtl8366s_sw_get_port_pvid(struct switch_dev
*dev
, int port
, int *val
)
1408 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
1409 return rtl8366s_get_pvid(smi
, port
, val
);
1412 static int rtl8366s_sw_set_port_pvid(struct switch_dev
*dev
, int port
, int val
)
1414 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
1415 return rtl8366s_set_pvid(smi
, port
, val
);
1418 static int rtl8366s_sw_reset_switch(struct switch_dev
*dev
)
1420 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
1423 err
= rtl8366s_reset_chip(smi
);
1427 err
= rtl8366s_hw_init(smi
);
1431 return rtl8366s_reset_vlan(smi
);
1434 static struct switch_attr rtl8366s_globals
[] = {
1436 .type
= SWITCH_TYPE_INT
,
1437 .name
= "enable_vlan",
1438 .description
= "Enable VLAN mode",
1439 .set
= rtl8366s_sw_set_vlan_enable
,
1440 .get
= rtl8366s_sw_get_vlan_enable
,
1444 .type
= SWITCH_TYPE_INT
,
1445 .name
= "enable_vlan4k",
1446 .description
= "Enable VLAN 4K mode",
1447 .set
= rtl8366s_sw_set_vlan_enable
,
1448 .get
= rtl8366s_sw_get_vlan_enable
,
1452 .type
= SWITCH_TYPE_INT
,
1453 .name
= "reset_mibs",
1454 .description
= "Reset all MIB counters",
1455 .set
= rtl8366s_sw_reset_mibs
,
1459 .type
= SWITCH_TYPE_INT
,
1460 .name
= "blinkrate",
1461 .description
= "Get/Set LED blinking rate (0 = 43ms, 1 = 84ms,"
1462 " 2 = 120ms, 3 = 170ms, 4 = 340ms, 5 = 670ms)",
1463 .set
= rtl8366s_sw_set_blinkrate
,
1464 .get
= rtl8366s_sw_get_blinkrate
,
1469 static struct switch_attr rtl8366s_port
[] = {
1471 .type
= SWITCH_TYPE_STRING
,
1473 .description
= "Get port link information",
1476 .get
= rtl8366s_sw_get_port_link
,
1478 .type
= SWITCH_TYPE_INT
,
1479 .name
= "reset_mib",
1480 .description
= "Reset single port MIB counters",
1482 .set
= rtl8366s_sw_reset_port_mibs
,
1485 .type
= SWITCH_TYPE_STRING
,
1487 .description
= "Get MIB counters for port",
1490 .get
= rtl8366s_sw_get_port_mib
,
1492 .type
= SWITCH_TYPE_INT
,
1494 .description
= "Get/Set port group (0 - 3) led mode (0 - 15)",
1496 .set
= rtl8366s_sw_set_port_led
,
1497 .get
= rtl8366s_sw_get_port_led
,
1501 static struct switch_attr rtl8366s_vlan
[] = {
1503 .type
= SWITCH_TYPE_STRING
,
1505 .description
= "Get vlan information",
1508 .get
= rtl8366s_sw_get_vlan_info
,
1513 static struct switch_dev rtl8366_switch_dev
= {
1515 .cpu_port
= RTL8366S_PORT_NUM_CPU
,
1516 .ports
= RTL8366S_NUM_PORTS
,
1517 .vlans
= RTL8366S_NUM_VLANS
,
1519 .attr
= rtl8366s_globals
,
1520 .n_attr
= ARRAY_SIZE(rtl8366s_globals
),
1523 .attr
= rtl8366s_port
,
1524 .n_attr
= ARRAY_SIZE(rtl8366s_port
),
1527 .attr
= rtl8366s_vlan
,
1528 .n_attr
= ARRAY_SIZE(rtl8366s_vlan
),
1531 .get_vlan_ports
= rtl8366s_sw_get_vlan_ports
,
1532 .set_vlan_ports
= rtl8366s_sw_set_vlan_ports
,
1533 .get_port_pvid
= rtl8366s_sw_get_port_pvid
,
1534 .set_port_pvid
= rtl8366s_sw_set_port_pvid
,
1535 .reset_switch
= rtl8366s_sw_reset_switch
,
1538 static int rtl8366s_switch_init(struct rtl8366s
*rtl
)
1540 struct switch_dev
*dev
= &rtl
->dev
;
1543 memcpy(dev
, &rtl8366_switch_dev
, sizeof(struct switch_dev
));
1545 dev
->devname
= dev_name(rtl
->parent
);
1547 err
= register_switch(dev
, NULL
);
1549 dev_err(rtl
->parent
, "switch registration failed\n");
1554 static void rtl8366s_switch_cleanup(struct rtl8366s
*rtl
)
1556 unregister_switch(&rtl
->dev
);
1559 static int rtl8366s_mii_read(struct mii_bus
*bus
, int addr
, int reg
)
1561 struct rtl8366_smi
*smi
= bus
->priv
;
1565 err
= rtl8366s_read_phy_reg(smi
, addr
, 0, reg
, &val
);
1572 static int rtl8366s_mii_write(struct mii_bus
*bus
, int addr
, int reg
, u16 val
)
1574 struct rtl8366_smi
*smi
= bus
->priv
;
1578 err
= rtl8366s_write_phy_reg(smi
, addr
, 0, reg
, val
);
1580 (void) rtl8366s_read_phy_reg(smi
, addr
, 0, reg
, &t
);
1585 static int rtl8366s_mii_bus_match(struct mii_bus
*bus
)
1587 return (bus
->read
== rtl8366s_mii_read
&&
1588 bus
->write
== rtl8366s_mii_write
);
1591 static int rtl8366s_setup(struct rtl8366s
*rtl
)
1593 struct rtl8366_smi
*smi
= &rtl
->smi
;
1596 rtl8366s_debugfs_init(rtl
);
1598 ret
= rtl8366s_reset_chip(smi
);
1602 ret
= rtl8366s_hw_init(smi
);
1606 static int rtl8366s_detect(struct rtl8366_smi
*smi
)
1612 ret
= rtl8366_smi_read_reg(smi
, RTL8366S_CHIP_ID_REG
, &chip_id
);
1614 dev_err(smi
->parent
, "unable to read chip id\n");
1619 case RTL8366S_CHIP_ID_8366
:
1622 dev_err(smi
->parent
, "unknown chip id (%04x)\n", chip_id
);
1626 ret
= rtl8366_smi_read_reg(smi
, RTL8366S_CHIP_VERSION_CTRL_REG
,
1629 dev_err(smi
->parent
, "unable to read chip version\n");
1633 dev_info(smi
->parent
, "RTL%04x ver. %u chip found\n",
1634 chip_id
, chip_ver
& RTL8366S_CHIP_VERSION_MASK
);
1639 static struct rtl8366_smi_ops rtl8366s_smi_ops
= {
1640 .detect
= rtl8366s_detect
,
1641 .mii_read
= rtl8366s_mii_read
,
1642 .mii_write
= rtl8366s_mii_write
,
1644 .get_vlan_mc
= rtl8366s_get_vlan_mc
,
1645 .set_vlan_mc
= rtl8366s_set_vlan_mc
,
1646 .get_vlan_4k
= rtl8366s_get_vlan_4k
,
1647 .set_vlan_4k
= rtl8366s_set_vlan_4k
,
1648 .get_mc_index
= rtl8366s_get_mc_index
,
1649 .set_mc_index
= rtl8366s_set_mc_index
,
1652 static int __init
rtl8366s_probe(struct platform_device
*pdev
)
1654 static int rtl8366_smi_version_printed
;
1655 struct rtl8366s_platform_data
*pdata
;
1656 struct rtl8366s
*rtl
;
1657 struct rtl8366_smi
*smi
;
1660 if (!rtl8366_smi_version_printed
++)
1661 printk(KERN_NOTICE RTL8366S_DRIVER_DESC
1662 " version " RTL8366S_DRIVER_VER
"\n");
1664 pdata
= pdev
->dev
.platform_data
;
1666 dev_err(&pdev
->dev
, "no platform data specified\n");
1671 rtl
= kzalloc(sizeof(*rtl
), GFP_KERNEL
);
1673 dev_err(&pdev
->dev
, "no memory for private data\n");
1678 rtl
->parent
= &pdev
->dev
;
1681 smi
->parent
= &pdev
->dev
;
1682 smi
->gpio_sda
= pdata
->gpio_sda
;
1683 smi
->gpio_sck
= pdata
->gpio_sck
;
1684 smi
->ops
= &rtl8366s_smi_ops
;
1686 err
= rtl8366_smi_init(smi
);
1690 platform_set_drvdata(pdev
, rtl
);
1692 err
= rtl8366s_setup(rtl
);
1694 goto err_clear_drvdata
;
1696 err
= rtl8366s_switch_init(rtl
);
1698 goto err_clear_drvdata
;
1703 platform_set_drvdata(pdev
, NULL
);
1704 rtl8366_smi_cleanup(smi
);
1711 static int rtl8366s_phy_config_init(struct phy_device
*phydev
)
1713 if (!rtl8366s_mii_bus_match(phydev
->bus
))
1719 static int rtl8366s_phy_config_aneg(struct phy_device
*phydev
)
1724 static struct phy_driver rtl8366s_phy_driver
= {
1725 .phy_id
= 0x001cc960,
1726 .name
= "Realtek RTL8366S",
1727 .phy_id_mask
= 0x1ffffff0,
1728 .features
= PHY_GBIT_FEATURES
,
1729 .config_aneg
= rtl8366s_phy_config_aneg
,
1730 .config_init
= rtl8366s_phy_config_init
,
1731 .read_status
= genphy_read_status
,
1733 .owner
= THIS_MODULE
,
1737 static int __devexit
rtl8366s_remove(struct platform_device
*pdev
)
1739 struct rtl8366s
*rtl
= platform_get_drvdata(pdev
);
1742 rtl8366s_switch_cleanup(rtl
);
1743 rtl8366s_debugfs_remove(rtl
);
1744 platform_set_drvdata(pdev
, NULL
);
1745 rtl8366_smi_cleanup(&rtl
->smi
);
1752 static struct platform_driver rtl8366s_driver
= {
1754 .name
= RTL8366S_DRIVER_NAME
,
1755 .owner
= THIS_MODULE
,
1757 .probe
= rtl8366s_probe
,
1758 .remove
= __devexit_p(rtl8366s_remove
),
1761 static int __init
rtl8366s_module_init(void)
1764 ret
= platform_driver_register(&rtl8366s_driver
);
1768 ret
= phy_driver_register(&rtl8366s_phy_driver
);
1770 goto err_platform_unregister
;
1774 err_platform_unregister
:
1775 platform_driver_unregister(&rtl8366s_driver
);
1778 module_init(rtl8366s_module_init
);
1780 static void __exit
rtl8366s_module_exit(void)
1782 phy_driver_unregister(&rtl8366s_phy_driver
);
1783 platform_driver_unregister(&rtl8366s_driver
);
1785 module_exit(rtl8366s_module_exit
);
1787 MODULE_DESCRIPTION(RTL8366S_DRIVER_DESC
);
1788 MODULE_VERSION(RTL8366S_DRIVER_VER
);
1789 MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
1790 MODULE_AUTHOR("Antti Seppälä <a.seppala@gmail.com>");
1791 MODULE_LICENSE("GPL v2");
1792 MODULE_ALIAS("platform:" RTL8366S_DRIVER_NAME
);