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/rtl8366s.h>
20 #include "rtl8366_smi.h"
22 #define RTL8366S_DRIVER_DESC "Realtek RTL8366S ethernet switch driver"
23 #define RTL8366S_DRIVER_VER "0.2.2"
25 #define RTL8366S_PHY_NO_MAX 4
26 #define RTL8366S_PHY_PAGE_MAX 7
27 #define RTL8366S_PHY_ADDR_MAX 31
29 /* Switch Global Configuration register */
30 #define RTL8366S_SGCR 0x0000
31 #define RTL8366S_SGCR_EN_BC_STORM_CTRL BIT(0)
32 #define RTL8366S_SGCR_MAX_LENGTH(_x) (_x << 4)
33 #define RTL8366S_SGCR_MAX_LENGTH_MASK RTL8366S_SGCR_MAX_LENGTH(0x3)
34 #define RTL8366S_SGCR_MAX_LENGTH_1522 RTL8366S_SGCR_MAX_LENGTH(0x0)
35 #define RTL8366S_SGCR_MAX_LENGTH_1536 RTL8366S_SGCR_MAX_LENGTH(0x1)
36 #define RTL8366S_SGCR_MAX_LENGTH_1552 RTL8366S_SGCR_MAX_LENGTH(0x2)
37 #define RTL8366S_SGCR_MAX_LENGTH_16000 RTL8366S_SGCR_MAX_LENGTH(0x3)
38 #define RTL8366S_SGCR_EN_VLAN BIT(13)
40 /* Port Enable Control register */
41 #define RTL8366S_PECR 0x0001
43 /* Switch Security Control registers */
44 #define RTL8366S_SSCR0 0x0002
45 #define RTL8366S_SSCR1 0x0003
46 #define RTL8366S_SSCR2 0x0004
47 #define RTL8366S_SSCR2_DROP_UNKNOWN_DA BIT(0)
49 #define RTL8366S_RESET_CTRL_REG 0x0100
50 #define RTL8366S_CHIP_CTRL_RESET_HW 1
51 #define RTL8366S_CHIP_CTRL_RESET_SW (1 << 1)
53 #define RTL8366S_CHIP_VERSION_CTRL_REG 0x0104
54 #define RTL8366S_CHIP_VERSION_MASK 0xf
55 #define RTL8366S_CHIP_ID_REG 0x0105
56 #define RTL8366S_CHIP_ID_8366 0x8366
58 /* PHY registers control */
59 #define RTL8366S_PHY_ACCESS_CTRL_REG 0x8028
60 #define RTL8366S_PHY_ACCESS_DATA_REG 0x8029
62 #define RTL8366S_PHY_CTRL_READ 1
63 #define RTL8366S_PHY_CTRL_WRITE 0
65 #define RTL8366S_PHY_REG_MASK 0x1f
66 #define RTL8366S_PHY_PAGE_OFFSET 5
67 #define RTL8366S_PHY_PAGE_MASK (0x7 << 5)
68 #define RTL8366S_PHY_NO_OFFSET 9
69 #define RTL8366S_PHY_NO_MASK (0x1f << 9)
71 /* LED control registers */
72 #define RTL8366S_LED_BLINKRATE_REG 0x0420
73 #define RTL8366S_LED_BLINKRATE_BIT 0
74 #define RTL8366S_LED_BLINKRATE_MASK 0x0007
76 #define RTL8366S_LED_CTRL_REG 0x0421
77 #define RTL8366S_LED_0_1_CTRL_REG 0x0422
78 #define RTL8366S_LED_2_3_CTRL_REG 0x0423
80 #define RTL8366S_MIB_COUNT 33
81 #define RTL8366S_GLOBAL_MIB_COUNT 1
82 #define RTL8366S_MIB_COUNTER_PORT_OFFSET 0x0040
83 #define RTL8366S_MIB_COUNTER_BASE 0x1000
84 #define RTL8366S_MIB_COUNTER_PORT_OFFSET2 0x0008
85 #define RTL8366S_MIB_COUNTER_BASE2 0x1180
86 #define RTL8366S_MIB_CTRL_REG 0x11F0
87 #define RTL8366S_MIB_CTRL_USER_MASK 0x01FF
88 #define RTL8366S_MIB_CTRL_BUSY_MASK 0x0001
89 #define RTL8366S_MIB_CTRL_RESET_MASK 0x0002
91 #define RTL8366S_MIB_CTRL_GLOBAL_RESET_MASK 0x0004
92 #define RTL8366S_MIB_CTRL_PORT_RESET_BIT 0x0003
93 #define RTL8366S_MIB_CTRL_PORT_RESET_MASK 0x01FC
96 #define RTL8366S_PORT_VLAN_CTRL_BASE 0x0058
97 #define RTL8366S_PORT_VLAN_CTRL_REG(_p) \
98 (RTL8366S_PORT_VLAN_CTRL_BASE + (_p) / 4)
99 #define RTL8366S_PORT_VLAN_CTRL_MASK 0xf
100 #define RTL8366S_PORT_VLAN_CTRL_SHIFT(_p) (4 * ((_p) % 4))
103 #define RTL8366S_VLAN_TABLE_READ_BASE 0x018B
104 #define RTL8366S_VLAN_TABLE_WRITE_BASE 0x0185
106 #define RTL8366S_VLAN_TB_CTRL_REG 0x010F
108 #define RTL8366S_TABLE_ACCESS_CTRL_REG 0x0180
109 #define RTL8366S_TABLE_VLAN_READ_CTRL 0x0E01
110 #define RTL8366S_TABLE_VLAN_WRITE_CTRL 0x0F01
112 #define RTL8366S_VLAN_MC_BASE(_x) (0x0016 + (_x) * 2)
114 #define RTL8366S_VLAN_MEMBERINGRESS_REG 0x0379
116 #define RTL8366S_PORT_LINK_STATUS_BASE 0x0060
117 #define RTL8366S_PORT_STATUS_SPEED_MASK 0x0003
118 #define RTL8366S_PORT_STATUS_DUPLEX_MASK 0x0004
119 #define RTL8366S_PORT_STATUS_LINK_MASK 0x0010
120 #define RTL8366S_PORT_STATUS_TXPAUSE_MASK 0x0020
121 #define RTL8366S_PORT_STATUS_RXPAUSE_MASK 0x0040
122 #define RTL8366S_PORT_STATUS_AN_MASK 0x0080
125 #define RTL8366S_PORT_NUM_CPU 5
126 #define RTL8366S_NUM_PORTS 6
127 #define RTL8366S_NUM_VLANS 16
128 #define RTL8366S_NUM_LEDGROUPS 4
129 #define RTL8366S_NUM_VIDS 4096
130 #define RTL8366S_PRIORITYMAX 7
131 #define RTL8366S_FIDMAX 7
134 #define RTL8366S_PORT_1 (1 << 0) /* In userspace port 0 */
135 #define RTL8366S_PORT_2 (1 << 1) /* In userspace port 1 */
136 #define RTL8366S_PORT_3 (1 << 2) /* In userspace port 2 */
137 #define RTL8366S_PORT_4 (1 << 3) /* In userspace port 3 */
139 #define RTL8366S_PORT_UNKNOWN (1 << 4) /* No known connection */
140 #define RTL8366S_PORT_CPU (1 << 5) /* CPU port */
142 #define RTL8366S_PORT_ALL (RTL8366S_PORT_1 | \
146 RTL8366S_PORT_UNKNOWN | \
149 #define RTL8366S_PORT_ALL_BUT_CPU (RTL8366S_PORT_1 | \
153 RTL8366S_PORT_UNKNOWN)
155 #define RTL8366S_PORT_ALL_EXTERNAL (RTL8366S_PORT_1 | \
160 #define RTL8366S_PORT_ALL_INTERNAL (RTL8366S_PORT_UNKNOWN | \
163 #define RTL8366S_VLAN_VID_MASK 0xfff
164 #define RTL8366S_VLAN_PRIORITY_SHIFT 12
165 #define RTL8366S_VLAN_PRIORITY_MASK 0x7
166 #define RTL8366S_VLAN_MEMBER_MASK 0x3f
167 #define RTL8366S_VLAN_UNTAG_SHIFT 6
168 #define RTL8366S_VLAN_UNTAG_MASK 0x3f
169 #define RTL8366S_VLAN_FID_SHIFT 12
170 #define RTL8366S_VLAN_FID_MASK 0x7
172 static struct rtl8366_mib_counter rtl8366s_mib_counters
[] = {
173 { 0, 0, 4, "IfInOctets" },
174 { 0, 4, 4, "EtherStatsOctets" },
175 { 0, 8, 2, "EtherStatsUnderSizePkts" },
176 { 0, 10, 2, "EtherFragments" },
177 { 0, 12, 2, "EtherStatsPkts64Octets" },
178 { 0, 14, 2, "EtherStatsPkts65to127Octets" },
179 { 0, 16, 2, "EtherStatsPkts128to255Octets" },
180 { 0, 18, 2, "EtherStatsPkts256to511Octets" },
181 { 0, 20, 2, "EtherStatsPkts512to1023Octets" },
182 { 0, 22, 2, "EtherStatsPkts1024to1518Octets" },
183 { 0, 24, 2, "EtherOversizeStats" },
184 { 0, 26, 2, "EtherStatsJabbers" },
185 { 0, 28, 2, "IfInUcastPkts" },
186 { 0, 30, 2, "EtherStatsMulticastPkts" },
187 { 0, 32, 2, "EtherStatsBroadcastPkts" },
188 { 0, 34, 2, "EtherStatsDropEvents" },
189 { 0, 36, 2, "Dot3StatsFCSErrors" },
190 { 0, 38, 2, "Dot3StatsSymbolErrors" },
191 { 0, 40, 2, "Dot3InPauseFrames" },
192 { 0, 42, 2, "Dot3ControlInUnknownOpcodes" },
193 { 0, 44, 4, "IfOutOctets" },
194 { 0, 48, 2, "Dot3StatsSingleCollisionFrames" },
195 { 0, 50, 2, "Dot3StatMultipleCollisionFrames" },
196 { 0, 52, 2, "Dot3sDeferredTransmissions" },
197 { 0, 54, 2, "Dot3StatsLateCollisions" },
198 { 0, 56, 2, "EtherStatsCollisions" },
199 { 0, 58, 2, "Dot3StatsExcessiveCollisions" },
200 { 0, 60, 2, "Dot3OutPauseFrames" },
201 { 0, 62, 2, "Dot1dBasePortDelayExceededDiscards" },
204 * The following counters are accessible at a different
207 { 1, 0, 2, "Dot1dTpPortInDiscards" },
208 { 1, 2, 2, "IfOutUcastPkts" },
209 { 1, 4, 2, "IfOutMulticastPkts" },
210 { 1, 6, 2, "IfOutBroadcastPkts" },
213 #define REG_WR(_smi, _reg, _val) \
215 err = rtl8366_smi_write_reg(_smi, _reg, _val); \
220 #define REG_RMW(_smi, _reg, _mask, _val) \
222 err = rtl8366_smi_rmwr(_smi, _reg, _mask, _val); \
227 static int rtl8366s_reset_chip(struct rtl8366_smi
*smi
)
232 rtl8366_smi_write_reg(smi
, RTL8366S_RESET_CTRL_REG
,
233 RTL8366S_CHIP_CTRL_RESET_HW
);
236 if (rtl8366_smi_read_reg(smi
, RTL8366S_RESET_CTRL_REG
, &data
))
239 if (!(data
& RTL8366S_CHIP_CTRL_RESET_HW
))
244 printk("Timeout waiting for the switch to reset\n");
251 static int rtl8366s_hw_init(struct rtl8366_smi
*smi
)
255 /* set maximum packet length to 1536 bytes */
256 REG_RMW(smi
, RTL8366S_SGCR
, RTL8366S_SGCR_MAX_LENGTH_MASK
,
257 RTL8366S_SGCR_MAX_LENGTH_1536
);
259 /* enable all ports */
260 REG_WR(smi
, RTL8366S_PECR
, 0);
262 /* disable learning for all ports */
263 REG_WR(smi
, RTL8366S_SSCR0
, RTL8366S_PORT_ALL
);
265 /* disable auto ageing for all ports */
266 REG_WR(smi
, RTL8366S_SSCR1
, RTL8366S_PORT_ALL
);
269 * discard VLAN tagged packets if the port is not a member of
270 * the VLAN with which the packets is associated.
272 REG_WR(smi
, RTL8366S_VLAN_MEMBERINGRESS_REG
, RTL8366S_PORT_ALL
);
274 /* don't drop packets whose DA has not been learned */
275 REG_RMW(smi
, RTL8366S_SSCR2
, RTL8366S_SSCR2_DROP_UNKNOWN_DA
, 0);
280 static int rtl8366s_read_phy_reg(struct rtl8366_smi
*smi
,
281 u32 phy_no
, u32 page
, u32 addr
, u32
*data
)
286 if (phy_no
> RTL8366S_PHY_NO_MAX
)
289 if (page
> RTL8366S_PHY_PAGE_MAX
)
292 if (addr
> RTL8366S_PHY_ADDR_MAX
)
295 ret
= rtl8366_smi_write_reg(smi
, RTL8366S_PHY_ACCESS_CTRL_REG
,
296 RTL8366S_PHY_CTRL_READ
);
300 reg
= 0x8000 | (1 << (phy_no
+ RTL8366S_PHY_NO_OFFSET
)) |
301 ((page
<< RTL8366S_PHY_PAGE_OFFSET
) & RTL8366S_PHY_PAGE_MASK
) |
302 (addr
& RTL8366S_PHY_REG_MASK
);
304 ret
= rtl8366_smi_write_reg(smi
, reg
, 0);
308 ret
= rtl8366_smi_read_reg(smi
, RTL8366S_PHY_ACCESS_DATA_REG
, data
);
315 static int rtl8366s_write_phy_reg(struct rtl8366_smi
*smi
,
316 u32 phy_no
, u32 page
, u32 addr
, u32 data
)
321 if (phy_no
> RTL8366S_PHY_NO_MAX
)
324 if (page
> RTL8366S_PHY_PAGE_MAX
)
327 if (addr
> RTL8366S_PHY_ADDR_MAX
)
330 ret
= rtl8366_smi_write_reg(smi
, RTL8366S_PHY_ACCESS_CTRL_REG
,
331 RTL8366S_PHY_CTRL_WRITE
);
335 reg
= 0x8000 | (1 << (phy_no
+ RTL8366S_PHY_NO_OFFSET
)) |
336 ((page
<< RTL8366S_PHY_PAGE_OFFSET
) & RTL8366S_PHY_PAGE_MASK
) |
337 (addr
& RTL8366S_PHY_REG_MASK
);
339 ret
= rtl8366_smi_write_reg(smi
, reg
, data
);
346 static int rtl8366_get_mib_counter(struct rtl8366_smi
*smi
, int counter
,
347 int port
, unsigned long long *val
)
354 if (port
> RTL8366S_NUM_PORTS
|| counter
>= RTL8366S_MIB_COUNT
)
357 switch (rtl8366s_mib_counters
[counter
].base
) {
359 addr
= RTL8366S_MIB_COUNTER_BASE
+
360 RTL8366S_MIB_COUNTER_PORT_OFFSET
* port
;
364 addr
= RTL8366S_MIB_COUNTER_BASE2
+
365 RTL8366S_MIB_COUNTER_PORT_OFFSET2
* port
;
372 addr
+= rtl8366s_mib_counters
[counter
].offset
;
375 * Writing access counter address first
376 * then ASIC will prepare 64bits counter wait for being retrived
378 data
= 0; /* writing data will be discard by ASIC */
379 err
= rtl8366_smi_write_reg(smi
, addr
, data
);
383 /* read MIB control register */
384 err
= rtl8366_smi_read_reg(smi
, RTL8366S_MIB_CTRL_REG
, &data
);
388 if (data
& RTL8366S_MIB_CTRL_BUSY_MASK
)
391 if (data
& RTL8366S_MIB_CTRL_RESET_MASK
)
395 for (i
= rtl8366s_mib_counters
[counter
].length
; i
> 0; i
--) {
396 err
= rtl8366_smi_read_reg(smi
, addr
+ (i
- 1), &data
);
400 mibvalue
= (mibvalue
<< 16) | (data
& 0xFFFF);
407 static int rtl8366s_get_vlan_4k(struct rtl8366_smi
*smi
, u32 vid
,
408 struct rtl8366_vlan_4k
*vlan4k
)
414 memset(vlan4k
, '\0', sizeof(struct rtl8366_vlan_4k
));
416 if (vid
>= RTL8366S_NUM_VIDS
)
420 err
= rtl8366_smi_write_reg(smi
, RTL8366S_VLAN_TABLE_WRITE_BASE
,
421 vid
& RTL8366S_VLAN_VID_MASK
);
425 /* write table access control word */
426 err
= rtl8366_smi_write_reg(smi
, RTL8366S_TABLE_ACCESS_CTRL_REG
,
427 RTL8366S_TABLE_VLAN_READ_CTRL
);
431 for (i
= 0; i
< 2; i
++) {
432 err
= rtl8366_smi_read_reg(smi
,
433 RTL8366S_VLAN_TABLE_READ_BASE
+ i
,
440 vlan4k
->untag
= (data
[1] >> RTL8366S_VLAN_UNTAG_SHIFT
) &
441 RTL8366S_VLAN_UNTAG_MASK
;
442 vlan4k
->member
= data
[1] & RTL8366S_VLAN_MEMBER_MASK
;
443 vlan4k
->fid
= (data
[1] >> RTL8366S_VLAN_FID_SHIFT
) &
444 RTL8366S_VLAN_FID_MASK
;
449 static int rtl8366s_set_vlan_4k(struct rtl8366_smi
*smi
,
450 const struct rtl8366_vlan_4k
*vlan4k
)
456 if (vlan4k
->vid
>= RTL8366S_NUM_VIDS
||
457 vlan4k
->member
> RTL8366S_PORT_ALL
||
458 vlan4k
->untag
> RTL8366S_PORT_ALL
||
459 vlan4k
->fid
> RTL8366S_FIDMAX
)
462 data
[0] = vlan4k
->vid
& RTL8366S_VLAN_VID_MASK
;
463 data
[1] = (vlan4k
->member
& RTL8366S_VLAN_MEMBER_MASK
) |
464 ((vlan4k
->untag
& RTL8366S_VLAN_UNTAG_MASK
) <<
465 RTL8366S_VLAN_UNTAG_SHIFT
) |
466 ((vlan4k
->fid
& RTL8366S_VLAN_FID_MASK
) <<
467 RTL8366S_VLAN_FID_SHIFT
);
469 for (i
= 0; i
< 2; i
++) {
470 err
= rtl8366_smi_write_reg(smi
,
471 RTL8366S_VLAN_TABLE_WRITE_BASE
+ i
,
477 /* write table access control word */
478 err
= rtl8366_smi_write_reg(smi
, RTL8366S_TABLE_ACCESS_CTRL_REG
,
479 RTL8366S_TABLE_VLAN_WRITE_CTRL
);
484 static int rtl8366s_get_vlan_mc(struct rtl8366_smi
*smi
, u32 index
,
485 struct rtl8366_vlan_mc
*vlanmc
)
491 memset(vlanmc
, '\0', sizeof(struct rtl8366_vlan_mc
));
493 if (index
>= RTL8366S_NUM_VLANS
)
496 for (i
= 0; i
< 2; i
++) {
497 err
= rtl8366_smi_read_reg(smi
,
498 RTL8366S_VLAN_MC_BASE(index
) + i
,
504 vlanmc
->vid
= data
[0] & RTL8366S_VLAN_VID_MASK
;
505 vlanmc
->priority
= (data
[0] >> RTL8366S_VLAN_PRIORITY_SHIFT
) &
506 RTL8366S_VLAN_PRIORITY_MASK
;
507 vlanmc
->untag
= (data
[1] >> RTL8366S_VLAN_UNTAG_SHIFT
) &
508 RTL8366S_VLAN_UNTAG_MASK
;
509 vlanmc
->member
= data
[1] & RTL8366S_VLAN_MEMBER_MASK
;
510 vlanmc
->fid
= (data
[1] >> RTL8366S_VLAN_FID_SHIFT
) &
511 RTL8366S_VLAN_FID_MASK
;
516 static int rtl8366s_set_vlan_mc(struct rtl8366_smi
*smi
, u32 index
,
517 const struct rtl8366_vlan_mc
*vlanmc
)
523 if (index
>= RTL8366S_NUM_VLANS
||
524 vlanmc
->vid
>= RTL8366S_NUM_VIDS
||
525 vlanmc
->priority
> RTL8366S_PRIORITYMAX
||
526 vlanmc
->member
> RTL8366S_PORT_ALL
||
527 vlanmc
->untag
> RTL8366S_PORT_ALL
||
528 vlanmc
->fid
> RTL8366S_FIDMAX
)
531 data
[0] = (vlanmc
->vid
& RTL8366S_VLAN_VID_MASK
) |
532 ((vlanmc
->priority
& RTL8366S_VLAN_PRIORITY_MASK
) <<
533 RTL8366S_VLAN_PRIORITY_SHIFT
);
534 data
[1] = (vlanmc
->member
& RTL8366S_VLAN_MEMBER_MASK
) |
535 ((vlanmc
->untag
& RTL8366S_VLAN_UNTAG_MASK
) <<
536 RTL8366S_VLAN_UNTAG_SHIFT
) |
537 ((vlanmc
->fid
& RTL8366S_VLAN_FID_MASK
) <<
538 RTL8366S_VLAN_FID_SHIFT
);
540 for (i
= 0; i
< 2; i
++) {
541 err
= rtl8366_smi_write_reg(smi
,
542 RTL8366S_VLAN_MC_BASE(index
) + i
,
551 static int rtl8366s_get_mc_index(struct rtl8366_smi
*smi
, int port
, int *val
)
556 if (port
>= RTL8366S_NUM_PORTS
)
559 err
= rtl8366_smi_read_reg(smi
, RTL8366S_PORT_VLAN_CTRL_REG(port
),
564 *val
= (data
>> RTL8366S_PORT_VLAN_CTRL_SHIFT(port
)) &
565 RTL8366S_PORT_VLAN_CTRL_MASK
;
570 static int rtl8366s_set_mc_index(struct rtl8366_smi
*smi
, int port
, int index
)
572 if (port
>= RTL8366S_NUM_PORTS
|| index
>= RTL8366S_NUM_VLANS
)
575 return rtl8366_smi_rmwr(smi
, RTL8366S_PORT_VLAN_CTRL_REG(port
),
576 RTL8366S_PORT_VLAN_CTRL_MASK
<<
577 RTL8366S_PORT_VLAN_CTRL_SHIFT(port
),
578 (index
& RTL8366S_PORT_VLAN_CTRL_MASK
) <<
579 RTL8366S_PORT_VLAN_CTRL_SHIFT(port
));
582 static int rtl8366s_vlan_set_vlan(struct rtl8366_smi
*smi
, int enable
)
584 return rtl8366_smi_rmwr(smi
, RTL8366S_SGCR
, RTL8366S_SGCR_EN_VLAN
,
585 (enable
) ? RTL8366S_SGCR_EN_VLAN
: 0);
588 static int rtl8366s_vlan_set_4ktable(struct rtl8366_smi
*smi
, int enable
)
590 return rtl8366_smi_rmwr(smi
, RTL8366S_VLAN_TB_CTRL_REG
,
591 1, (enable
) ? 1 : 0);
594 static int rtl8366s_is_vlan_valid(struct rtl8366_smi
*smi
, unsigned vlan
)
596 if (vlan
== 0 || vlan
>= RTL8366S_NUM_VLANS
)
602 static int rtl8366s_sw_reset_mibs(struct switch_dev
*dev
,
603 const struct switch_attr
*attr
,
604 struct switch_val
*val
)
606 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
608 return rtl8366_smi_rmwr(smi
, RTL8366S_MIB_CTRL_REG
, 0, (1 << 2));
611 static int rtl8366s_sw_get_vlan_enable(struct switch_dev
*dev
,
612 const struct switch_attr
*attr
,
613 struct switch_val
*val
)
615 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
618 if (attr
->ofs
== 1) {
619 rtl8366_smi_read_reg(smi
, RTL8366S_SGCR
, &data
);
621 if (data
& RTL8366S_SGCR_EN_VLAN
)
625 } else if (attr
->ofs
== 2) {
626 rtl8366_smi_read_reg(smi
, RTL8366S_VLAN_TB_CTRL_REG
, &data
);
637 static int rtl8366s_sw_get_blinkrate(struct switch_dev
*dev
,
638 const struct switch_attr
*attr
,
639 struct switch_val
*val
)
641 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
644 rtl8366_smi_read_reg(smi
, RTL8366S_LED_BLINKRATE_REG
, &data
);
646 val
->value
.i
= (data
& (RTL8366S_LED_BLINKRATE_MASK
));
651 static int rtl8366s_sw_set_blinkrate(struct switch_dev
*dev
,
652 const struct switch_attr
*attr
,
653 struct switch_val
*val
)
655 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
657 if (val
->value
.i
>= 6)
660 return rtl8366_smi_rmwr(smi
, RTL8366S_LED_BLINKRATE_REG
,
661 RTL8366S_LED_BLINKRATE_MASK
,
665 static int rtl8366s_sw_set_vlan_enable(struct switch_dev
*dev
,
666 const struct switch_attr
*attr
,
667 struct switch_val
*val
)
669 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
672 return rtl8366s_vlan_set_vlan(smi
, val
->value
.i
);
674 return rtl8366s_vlan_set_4ktable(smi
, val
->value
.i
);
677 static const char *rtl8366s_speed_str(unsigned speed
)
691 static int rtl8366s_sw_get_port_link(struct switch_dev
*dev
,
692 const struct switch_attr
*attr
,
693 struct switch_val
*val
)
695 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
696 u32 len
= 0, data
= 0;
698 if (val
->port_vlan
>= RTL8366S_NUM_PORTS
)
701 memset(smi
->buf
, '\0', sizeof(smi
->buf
));
702 rtl8366_smi_read_reg(smi
, RTL8366S_PORT_LINK_STATUS_BASE
+
703 (val
->port_vlan
/ 2), &data
);
705 if (val
->port_vlan
% 2)
708 if (data
& RTL8366S_PORT_STATUS_LINK_MASK
) {
709 len
= snprintf(smi
->buf
, sizeof(smi
->buf
),
710 "port:%d link:up speed:%s %s-duplex %s%s%s",
712 rtl8366s_speed_str(data
&
713 RTL8366S_PORT_STATUS_SPEED_MASK
),
714 (data
& RTL8366S_PORT_STATUS_DUPLEX_MASK
) ?
716 (data
& RTL8366S_PORT_STATUS_TXPAUSE_MASK
) ?
718 (data
& RTL8366S_PORT_STATUS_RXPAUSE_MASK
) ?
720 (data
& RTL8366S_PORT_STATUS_AN_MASK
) ?
723 len
= snprintf(smi
->buf
, sizeof(smi
->buf
), "port:%d link: down",
727 val
->value
.s
= smi
->buf
;
733 static int rtl8366s_sw_set_port_led(struct switch_dev
*dev
,
734 const struct switch_attr
*attr
,
735 struct switch_val
*val
)
737 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
742 if (val
->port_vlan
>= RTL8366S_NUM_PORTS
||
743 (1 << val
->port_vlan
) == RTL8366S_PORT_UNKNOWN
)
746 if (val
->port_vlan
== RTL8366S_PORT_NUM_CPU
) {
747 reg
= RTL8366S_LED_BLINKRATE_REG
;
749 data
= val
->value
.i
<< 4;
751 reg
= RTL8366S_LED_CTRL_REG
;
752 mask
= 0xF << (val
->port_vlan
* 4),
753 data
= val
->value
.i
<< (val
->port_vlan
* 4);
756 return rtl8366_smi_rmwr(smi
, RTL8366S_LED_BLINKRATE_REG
, mask
, data
);
759 static int rtl8366s_sw_get_port_led(struct switch_dev
*dev
,
760 const struct switch_attr
*attr
,
761 struct switch_val
*val
)
763 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
766 if (val
->port_vlan
>= RTL8366S_NUM_LEDGROUPS
)
769 rtl8366_smi_read_reg(smi
, RTL8366S_LED_CTRL_REG
, &data
);
770 val
->value
.i
= (data
>> (val
->port_vlan
* 4)) & 0x000F;
775 static int rtl8366s_sw_reset_port_mibs(struct switch_dev
*dev
,
776 const struct switch_attr
*attr
,
777 struct switch_val
*val
)
779 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
781 if (val
->port_vlan
>= RTL8366S_NUM_PORTS
)
785 return rtl8366_smi_rmwr(smi
, RTL8366S_MIB_CTRL_REG
,
786 0, (1 << (val
->port_vlan
+ 3)));
789 static int rtl8366s_sw_reset_switch(struct switch_dev
*dev
)
791 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
794 err
= rtl8366s_reset_chip(smi
);
798 err
= rtl8366s_hw_init(smi
);
802 return rtl8366_reset_vlan(smi
);
805 static struct switch_attr rtl8366s_globals
[] = {
807 .type
= SWITCH_TYPE_INT
,
808 .name
= "enable_vlan",
809 .description
= "Enable VLAN mode",
810 .set
= rtl8366s_sw_set_vlan_enable
,
811 .get
= rtl8366s_sw_get_vlan_enable
,
815 .type
= SWITCH_TYPE_INT
,
816 .name
= "enable_vlan4k",
817 .description
= "Enable VLAN 4K mode",
818 .set
= rtl8366s_sw_set_vlan_enable
,
819 .get
= rtl8366s_sw_get_vlan_enable
,
823 .type
= SWITCH_TYPE_NOVAL
,
824 .name
= "reset_mibs",
825 .description
= "Reset all MIB counters",
826 .set
= rtl8366s_sw_reset_mibs
,
828 .type
= SWITCH_TYPE_INT
,
830 .description
= "Get/Set LED blinking rate (0 = 43ms, 1 = 84ms,"
831 " 2 = 120ms, 3 = 170ms, 4 = 340ms, 5 = 670ms)",
832 .set
= rtl8366s_sw_set_blinkrate
,
833 .get
= rtl8366s_sw_get_blinkrate
,
838 static struct switch_attr rtl8366s_port
[] = {
840 .type
= SWITCH_TYPE_STRING
,
842 .description
= "Get port link information",
845 .get
= rtl8366s_sw_get_port_link
,
847 .type
= SWITCH_TYPE_NOVAL
,
849 .description
= "Reset single port MIB counters",
850 .set
= rtl8366s_sw_reset_port_mibs
,
852 .type
= SWITCH_TYPE_STRING
,
854 .description
= "Get MIB counters for port",
857 .get
= rtl8366_sw_get_port_mib
,
859 .type
= SWITCH_TYPE_INT
,
861 .description
= "Get/Set port group (0 - 3) led mode (0 - 15)",
863 .set
= rtl8366s_sw_set_port_led
,
864 .get
= rtl8366s_sw_get_port_led
,
868 static struct switch_attr rtl8366s_vlan
[] = {
870 .type
= SWITCH_TYPE_STRING
,
872 .description
= "Get vlan information",
875 .get
= rtl8366_sw_get_vlan_info
,
880 static struct switch_dev rtl8366_switch_dev
= {
882 .cpu_port
= RTL8366S_PORT_NUM_CPU
,
883 .ports
= RTL8366S_NUM_PORTS
,
884 .vlans
= RTL8366S_NUM_VLANS
,
886 .attr
= rtl8366s_globals
,
887 .n_attr
= ARRAY_SIZE(rtl8366s_globals
),
890 .attr
= rtl8366s_port
,
891 .n_attr
= ARRAY_SIZE(rtl8366s_port
),
894 .attr
= rtl8366s_vlan
,
895 .n_attr
= ARRAY_SIZE(rtl8366s_vlan
),
898 .get_vlan_ports
= rtl8366_sw_get_vlan_ports
,
899 .set_vlan_ports
= rtl8366_sw_set_vlan_ports
,
900 .get_port_pvid
= rtl8366_sw_get_port_pvid
,
901 .set_port_pvid
= rtl8366_sw_set_port_pvid
,
902 .reset_switch
= rtl8366s_sw_reset_switch
,
905 static int rtl8366s_switch_init(struct rtl8366_smi
*smi
)
907 struct switch_dev
*dev
= &smi
->sw_dev
;
910 memcpy(dev
, &rtl8366_switch_dev
, sizeof(struct switch_dev
));
912 dev
->devname
= dev_name(smi
->parent
);
914 err
= register_switch(dev
, NULL
);
916 dev_err(smi
->parent
, "switch registration failed\n");
921 static void rtl8366s_switch_cleanup(struct rtl8366_smi
*smi
)
923 unregister_switch(&smi
->sw_dev
);
926 static int rtl8366s_mii_read(struct mii_bus
*bus
, int addr
, int reg
)
928 struct rtl8366_smi
*smi
= bus
->priv
;
932 err
= rtl8366s_read_phy_reg(smi
, addr
, 0, reg
, &val
);
939 static int rtl8366s_mii_write(struct mii_bus
*bus
, int addr
, int reg
, u16 val
)
941 struct rtl8366_smi
*smi
= bus
->priv
;
945 err
= rtl8366s_write_phy_reg(smi
, addr
, 0, reg
, val
);
947 (void) rtl8366s_read_phy_reg(smi
, addr
, 0, reg
, &t
);
952 static int rtl8366s_mii_bus_match(struct mii_bus
*bus
)
954 return (bus
->read
== rtl8366s_mii_read
&&
955 bus
->write
== rtl8366s_mii_write
);
958 static int rtl8366s_setup(struct rtl8366_smi
*smi
)
962 ret
= rtl8366s_reset_chip(smi
);
966 ret
= rtl8366s_hw_init(smi
);
970 static int rtl8366s_detect(struct rtl8366_smi
*smi
)
976 ret
= rtl8366_smi_read_reg(smi
, RTL8366S_CHIP_ID_REG
, &chip_id
);
978 dev_err(smi
->parent
, "unable to read chip id\n");
983 case RTL8366S_CHIP_ID_8366
:
986 dev_err(smi
->parent
, "unknown chip id (%04x)\n", chip_id
);
990 ret
= rtl8366_smi_read_reg(smi
, RTL8366S_CHIP_VERSION_CTRL_REG
,
993 dev_err(smi
->parent
, "unable to read chip version\n");
997 dev_info(smi
->parent
, "RTL%04x ver. %u chip found\n",
998 chip_id
, chip_ver
& RTL8366S_CHIP_VERSION_MASK
);
1003 static struct rtl8366_smi_ops rtl8366s_smi_ops
= {
1004 .detect
= rtl8366s_detect
,
1005 .setup
= rtl8366s_setup
,
1007 .mii_read
= rtl8366s_mii_read
,
1008 .mii_write
= rtl8366s_mii_write
,
1010 .get_vlan_mc
= rtl8366s_get_vlan_mc
,
1011 .set_vlan_mc
= rtl8366s_set_vlan_mc
,
1012 .get_vlan_4k
= rtl8366s_get_vlan_4k
,
1013 .set_vlan_4k
= rtl8366s_set_vlan_4k
,
1014 .get_mc_index
= rtl8366s_get_mc_index
,
1015 .set_mc_index
= rtl8366s_set_mc_index
,
1016 .get_mib_counter
= rtl8366_get_mib_counter
,
1017 .is_vlan_valid
= rtl8366s_is_vlan_valid
,
1020 static int __init
rtl8366s_probe(struct platform_device
*pdev
)
1022 static int rtl8366_smi_version_printed
;
1023 struct rtl8366s_platform_data
*pdata
;
1024 struct rtl8366_smi
*smi
;
1027 if (!rtl8366_smi_version_printed
++)
1028 printk(KERN_NOTICE RTL8366S_DRIVER_DESC
1029 " version " RTL8366S_DRIVER_VER
"\n");
1031 pdata
= pdev
->dev
.platform_data
;
1033 dev_err(&pdev
->dev
, "no platform data specified\n");
1038 smi
= rtl8366_smi_alloc(&pdev
->dev
);
1044 smi
->gpio_sda
= pdata
->gpio_sda
;
1045 smi
->gpio_sck
= pdata
->gpio_sck
;
1046 smi
->ops
= &rtl8366s_smi_ops
;
1047 smi
->cpu_port
= RTL8366S_PORT_NUM_CPU
;
1048 smi
->num_ports
= RTL8366S_NUM_PORTS
;
1049 smi
->num_vlan_mc
= RTL8366S_NUM_VLANS
;
1050 smi
->mib_counters
= rtl8366s_mib_counters
;
1051 smi
->num_mib_counters
= ARRAY_SIZE(rtl8366s_mib_counters
);
1053 err
= rtl8366_smi_init(smi
);
1057 platform_set_drvdata(pdev
, smi
);
1059 err
= rtl8366s_switch_init(smi
);
1061 goto err_clear_drvdata
;
1066 platform_set_drvdata(pdev
, NULL
);
1067 rtl8366_smi_cleanup(smi
);
1074 static int rtl8366s_phy_config_init(struct phy_device
*phydev
)
1076 if (!rtl8366s_mii_bus_match(phydev
->bus
))
1082 static int rtl8366s_phy_config_aneg(struct phy_device
*phydev
)
1087 static struct phy_driver rtl8366s_phy_driver
= {
1088 .phy_id
= 0x001cc960,
1089 .name
= "Realtek RTL8366S",
1090 .phy_id_mask
= 0x1ffffff0,
1091 .features
= PHY_GBIT_FEATURES
,
1092 .config_aneg
= rtl8366s_phy_config_aneg
,
1093 .config_init
= rtl8366s_phy_config_init
,
1094 .read_status
= genphy_read_status
,
1096 .owner
= THIS_MODULE
,
1100 static int __devexit
rtl8366s_remove(struct platform_device
*pdev
)
1102 struct rtl8366_smi
*smi
= platform_get_drvdata(pdev
);
1105 rtl8366s_switch_cleanup(smi
);
1106 platform_set_drvdata(pdev
, NULL
);
1107 rtl8366_smi_cleanup(smi
);
1114 static struct platform_driver rtl8366s_driver
= {
1116 .name
= RTL8366S_DRIVER_NAME
,
1117 .owner
= THIS_MODULE
,
1119 .probe
= rtl8366s_probe
,
1120 .remove
= __devexit_p(rtl8366s_remove
),
1123 static int __init
rtl8366s_module_init(void)
1126 ret
= platform_driver_register(&rtl8366s_driver
);
1130 ret
= phy_driver_register(&rtl8366s_phy_driver
);
1132 goto err_platform_unregister
;
1136 err_platform_unregister
:
1137 platform_driver_unregister(&rtl8366s_driver
);
1140 module_init(rtl8366s_module_init
);
1142 static void __exit
rtl8366s_module_exit(void)
1144 phy_driver_unregister(&rtl8366s_phy_driver
);
1145 platform_driver_unregister(&rtl8366s_driver
);
1147 module_exit(rtl8366s_module_exit
);
1149 MODULE_DESCRIPTION(RTL8366S_DRIVER_DESC
);
1150 MODULE_VERSION(RTL8366S_DRIVER_VER
);
1151 MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
1152 MODULE_AUTHOR("Antti Seppälä <a.seppala@gmail.com>");
1153 MODULE_LICENSE("GPL v2");
1154 MODULE_ALIAS("platform:" RTL8366S_DRIVER_NAME
);