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>
6 * Copyright (C) 2010 Roman Yeryomin <roman@advem.lv>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License version 2 as published
10 * by the Free Software Foundation.
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/init.h>
16 #include <linux/platform_device.h>
17 #include <linux/delay.h>
18 #include <linux/skbuff.h>
19 #include <linux/rtl8366rb.h>
21 #include "rtl8366_smi.h"
23 #define RTL8366RB_DRIVER_DESC "Realtek RTL8366RB ethernet switch driver"
24 #define RTL8366RB_DRIVER_VER "0.2.2"
26 #define RTL8366RB_PHY_NO_MAX 4
27 #define RTL8366RB_PHY_PAGE_MAX 7
28 #define RTL8366RB_PHY_ADDR_MAX 31
30 /* Switch Global Configuration register */
31 #define RTL8366RB_SGCR 0x0000
32 #define RTL8366RB_SGCR_EN_BC_STORM_CTRL BIT(0)
33 #define RTL8366RB_SGCR_MAX_LENGTH(_x) (_x << 4)
34 #define RTL8366RB_SGCR_MAX_LENGTH_MASK RTL8366RB_SGCR_MAX_LENGTH(0x3)
35 #define RTL8366RB_SGCR_MAX_LENGTH_1522 RTL8366RB_SGCR_MAX_LENGTH(0x0)
36 #define RTL8366RB_SGCR_MAX_LENGTH_1536 RTL8366RB_SGCR_MAX_LENGTH(0x1)
37 #define RTL8366RB_SGCR_MAX_LENGTH_1552 RTL8366RB_SGCR_MAX_LENGTH(0x2)
38 #define RTL8366RB_SGCR_MAX_LENGTH_9216 RTL8366RB_SGCR_MAX_LENGTH(0x3)
39 #define RTL8366RB_SGCR_EN_VLAN BIT(13)
40 #define RTL8366RB_SGCR_EN_VLAN_4KTB BIT(14)
42 /* Port Enable Control register */
43 #define RTL8366RB_PECR 0x0001
45 /* Switch Security Control registers */
46 #define RTL8366RB_SSCR0 0x0002
47 #define RTL8366RB_SSCR1 0x0003
48 #define RTL8366RB_SSCR2 0x0004
49 #define RTL8366RB_SSCR2_DROP_UNKNOWN_DA BIT(0)
51 #define RTL8366RB_RESET_CTRL_REG 0x0100
52 #define RTL8366RB_CHIP_CTRL_RESET_HW 1
53 #define RTL8366RB_CHIP_CTRL_RESET_SW (1 << 1)
55 #define RTL8366RB_CHIP_VERSION_CTRL_REG 0x050A
56 #define RTL8366RB_CHIP_VERSION_MASK 0xf
57 #define RTL8366RB_CHIP_ID_REG 0x0509
58 #define RTL8366RB_CHIP_ID_8366 0x5937
60 /* PHY registers control */
61 #define RTL8366RB_PHY_ACCESS_CTRL_REG 0x8000
62 #define RTL8366RB_PHY_ACCESS_DATA_REG 0x8002
64 #define RTL8366RB_PHY_CTRL_READ 1
65 #define RTL8366RB_PHY_CTRL_WRITE 0
67 #define RTL8366RB_PHY_REG_MASK 0x1f
68 #define RTL8366RB_PHY_PAGE_OFFSET 5
69 #define RTL8366RB_PHY_PAGE_MASK (0xf << 5)
70 #define RTL8366RB_PHY_NO_OFFSET 9
71 #define RTL8366RB_PHY_NO_MASK (0x1f << 9)
73 #define RTL8366RB_VLAN_INGRESS_CTRL2_REG 0x037f
75 /* LED control registers */
76 #define RTL8366RB_LED_BLINKRATE_REG 0x0430
77 #define RTL8366RB_LED_BLINKRATE_BIT 0
78 #define RTL8366RB_LED_BLINKRATE_MASK 0x0007
80 #define RTL8366RB_LED_CTRL_REG 0x0431
81 #define RTL8366RB_LED_0_1_CTRL_REG 0x0432
82 #define RTL8366RB_LED_2_3_CTRL_REG 0x0433
84 #define RTL8366RB_MIB_COUNT 33
85 #define RTL8366RB_GLOBAL_MIB_COUNT 1
86 #define RTL8366RB_MIB_COUNTER_PORT_OFFSET 0x0050
87 #define RTL8366RB_MIB_COUNTER_BASE 0x1000
88 #define RTL8366RB_MIB_CTRL_REG 0x13F0
89 #define RTL8366RB_MIB_CTRL_USER_MASK 0x0FFC
90 #define RTL8366RB_MIB_CTRL_BUSY_MASK BIT(0)
91 #define RTL8366RB_MIB_CTRL_RESET_MASK BIT(1)
92 #define RTL8366RB_MIB_CTRL_PORT_RESET(_p) BIT(2 + (_p))
93 #define RTL8366RB_MIB_CTRL_GLOBAL_RESET BIT(11)
95 #define RTL8366RB_PORT_VLAN_CTRL_BASE 0x0063
96 #define RTL8366RB_PORT_VLAN_CTRL_REG(_p) \
97 (RTL8366RB_PORT_VLAN_CTRL_BASE + (_p) / 4)
98 #define RTL8366RB_PORT_VLAN_CTRL_MASK 0xf
99 #define RTL8366RB_PORT_VLAN_CTRL_SHIFT(_p) (4 * ((_p) % 4))
102 #define RTL8366RB_VLAN_TABLE_READ_BASE 0x018C
103 #define RTL8366RB_VLAN_TABLE_WRITE_BASE 0x0185
106 #define RTL8366RB_TABLE_ACCESS_CTRL_REG 0x0180
107 #define RTL8366RB_TABLE_VLAN_READ_CTRL 0x0E01
108 #define RTL8366RB_TABLE_VLAN_WRITE_CTRL 0x0F01
110 #define RTL8366RB_VLAN_MC_BASE(_x) (0x0020 + (_x) * 3)
113 #define RTL8366RB_PORT_LINK_STATUS_BASE 0x0014
114 #define RTL8366RB_PORT_STATUS_SPEED_MASK 0x0003
115 #define RTL8366RB_PORT_STATUS_DUPLEX_MASK 0x0004
116 #define RTL8366RB_PORT_STATUS_LINK_MASK 0x0010
117 #define RTL8366RB_PORT_STATUS_TXPAUSE_MASK 0x0020
118 #define RTL8366RB_PORT_STATUS_RXPAUSE_MASK 0x0040
119 #define RTL8366RB_PORT_STATUS_AN_MASK 0x0080
122 #define RTL8366RB_PORT_NUM_CPU 5
123 #define RTL8366RB_NUM_PORTS 6
124 #define RTL8366RB_NUM_VLANS 16
125 #define RTL8366RB_NUM_LEDGROUPS 4
126 #define RTL8366RB_NUM_VIDS 4096
127 #define RTL8366RB_PRIORITYMAX 7
128 #define RTL8366RB_FIDMAX 7
131 #define RTL8366RB_PORT_1 (1 << 0) /* In userspace port 0 */
132 #define RTL8366RB_PORT_2 (1 << 1) /* In userspace port 1 */
133 #define RTL8366RB_PORT_3 (1 << 2) /* In userspace port 2 */
134 #define RTL8366RB_PORT_4 (1 << 3) /* In userspace port 3 */
135 #define RTL8366RB_PORT_5 (1 << 4) /* In userspace port 4 */
137 #define RTL8366RB_PORT_CPU (1 << 5) /* CPU port */
139 #define RTL8366RB_PORT_ALL (RTL8366RB_PORT_1 | \
146 #define RTL8366RB_PORT_ALL_BUT_CPU (RTL8366RB_PORT_1 | \
152 #define RTL8366RB_PORT_ALL_EXTERNAL (RTL8366RB_PORT_1 | \
157 #define RTL8366RB_PORT_ALL_INTERNAL RTL8366RB_PORT_CPU
159 #define RTL8366RB_VLAN_VID_MASK 0xfff
160 #define RTL8366RB_VLAN_PRIORITY_SHIFT 12
161 #define RTL8366RB_VLAN_PRIORITY_MASK 0x7
162 #define RTL8366RB_VLAN_UNTAG_SHIFT 8
163 #define RTL8366RB_VLAN_UNTAG_MASK 0xff
164 #define RTL8366RB_VLAN_MEMBER_MASK 0xff
165 #define RTL8366RB_VLAN_FID_MASK 0x7
168 /* Port ingress bandwidth control */
169 #define RTL8366RB_IB_BASE 0x0200
170 #define RTL8366RB_IB_REG(pnum) (RTL8366RB_IB_BASE + pnum)
171 #define RTL8366RB_IB_BDTH_MASK 0x3fff
172 #define RTL8366RB_IB_PREIFG_OFFSET 14
173 #define RTL8366RB_IB_PREIFG_MASK (1 << RTL8366RB_IB_PREIFG_OFFSET)
175 /* Port egress bandwidth control */
176 #define RTL8366RB_EB_BASE 0x02d1
177 #define RTL8366RB_EB_REG(pnum) (RTL8366RB_EB_BASE + pnum)
178 #define RTL8366RB_EB_BDTH_MASK 0x3fff
179 #define RTL8366RB_EB_PREIFG_REG 0x02f8
180 #define RTL8366RB_EB_PREIFG_OFFSET 9
181 #define RTL8366RB_EB_PREIFG_MASK (1 << RTL8366RB_EB_PREIFG_OFFSET)
183 #define RTL8366RB_BDTH_SW_MAX 1048512
184 #define RTL8366RB_BDTH_BASE 64
185 #define RTL8366RB_BDTH_REG_DEFAULT 16383
188 #define RTL8366RB_QOS_BIT 15
189 #define RTL8366RB_QOS_MASK (1 << RTL8366RB_QOS_BIT)
190 /* Include/Exclude Preamble and IFG (20 bytes). 0:Exclude, 1:Include. */
191 #define RTL8366RB_QOS_DEFAULT_PREIFG 1
194 static struct rtl8366_mib_counter rtl8366rb_mib_counters
[] = {
195 { 0, 0, 4, "IfInOctets" },
196 { 0, 4, 4, "EtherStatsOctets" },
197 { 0, 8, 2, "EtherStatsUnderSizePkts" },
198 { 0, 10, 2, "EtherFragments" },
199 { 0, 12, 2, "EtherStatsPkts64Octets" },
200 { 0, 14, 2, "EtherStatsPkts65to127Octets" },
201 { 0, 16, 2, "EtherStatsPkts128to255Octets" },
202 { 0, 18, 2, "EtherStatsPkts256to511Octets" },
203 { 0, 20, 2, "EtherStatsPkts512to1023Octets" },
204 { 0, 22, 2, "EtherStatsPkts1024to1518Octets" },
205 { 0, 24, 2, "EtherOversizeStats" },
206 { 0, 26, 2, "EtherStatsJabbers" },
207 { 0, 28, 2, "IfInUcastPkts" },
208 { 0, 30, 2, "EtherStatsMulticastPkts" },
209 { 0, 32, 2, "EtherStatsBroadcastPkts" },
210 { 0, 34, 2, "EtherStatsDropEvents" },
211 { 0, 36, 2, "Dot3StatsFCSErrors" },
212 { 0, 38, 2, "Dot3StatsSymbolErrors" },
213 { 0, 40, 2, "Dot3InPauseFrames" },
214 { 0, 42, 2, "Dot3ControlInUnknownOpcodes" },
215 { 0, 44, 4, "IfOutOctets" },
216 { 0, 48, 2, "Dot3StatsSingleCollisionFrames" },
217 { 0, 50, 2, "Dot3StatMultipleCollisionFrames" },
218 { 0, 52, 2, "Dot3sDeferredTransmissions" },
219 { 0, 54, 2, "Dot3StatsLateCollisions" },
220 { 0, 56, 2, "EtherStatsCollisions" },
221 { 0, 58, 2, "Dot3StatsExcessiveCollisions" },
222 { 0, 60, 2, "Dot3OutPauseFrames" },
223 { 0, 62, 2, "Dot1dBasePortDelayExceededDiscards" },
224 { 0, 64, 2, "Dot1dTpPortInDiscards" },
225 { 0, 66, 2, "IfOutUcastPkts" },
226 { 0, 68, 2, "IfOutMulticastPkts" },
227 { 0, 70, 2, "IfOutBroadcastPkts" },
230 #define REG_WR(_smi, _reg, _val) \
232 err = rtl8366_smi_write_reg(_smi, _reg, _val); \
237 #define REG_RMW(_smi, _reg, _mask, _val) \
239 err = rtl8366_smi_rmwr(_smi, _reg, _mask, _val); \
244 static int rtl8366rb_reset_chip(struct rtl8366_smi
*smi
)
249 rtl8366_smi_write_reg(smi
, RTL8366RB_RESET_CTRL_REG
,
250 RTL8366RB_CHIP_CTRL_RESET_HW
);
253 if (rtl8366_smi_read_reg(smi
, RTL8366RB_RESET_CTRL_REG
, &data
))
256 if (!(data
& RTL8366RB_CHIP_CTRL_RESET_HW
))
261 printk("Timeout waiting for the switch to reset\n");
268 static int rtl8366rb_hw_init(struct rtl8366_smi
*smi
)
272 /* set maximum packet length to 1536 bytes */
273 REG_RMW(smi
, RTL8366RB_SGCR
, RTL8366RB_SGCR_MAX_LENGTH_MASK
,
274 RTL8366RB_SGCR_MAX_LENGTH_1536
);
276 /* enable all ports */
277 REG_WR(smi
, RTL8366RB_PECR
, 0);
279 /* enable learning for all ports */
280 REG_WR(smi
, RTL8366RB_SSCR0
, 0);
282 /* enable auto ageing for all ports */
283 REG_WR(smi
, RTL8366RB_SSCR1
, 0);
286 * discard VLAN tagged packets if the port is not a member of
287 * the VLAN with which the packets is associated.
289 REG_WR(smi
, RTL8366RB_VLAN_INGRESS_CTRL2_REG
, RTL8366RB_PORT_ALL
);
291 /* don't drop packets whose DA has not been learned */
292 REG_RMW(smi
, RTL8366RB_SSCR2
, RTL8366RB_SSCR2_DROP_UNKNOWN_DA
, 0);
297 static int rtl8366rb_read_phy_reg(struct rtl8366_smi
*smi
,
298 u32 phy_no
, u32 page
, u32 addr
, u32
*data
)
303 if (phy_no
> RTL8366RB_PHY_NO_MAX
)
306 if (page
> RTL8366RB_PHY_PAGE_MAX
)
309 if (addr
> RTL8366RB_PHY_ADDR_MAX
)
312 ret
= rtl8366_smi_write_reg(smi
, RTL8366RB_PHY_ACCESS_CTRL_REG
,
313 RTL8366RB_PHY_CTRL_READ
);
317 reg
= 0x8000 | (1 << (phy_no
+ RTL8366RB_PHY_NO_OFFSET
)) |
318 ((page
<< RTL8366RB_PHY_PAGE_OFFSET
) & RTL8366RB_PHY_PAGE_MASK
) |
319 (addr
& RTL8366RB_PHY_REG_MASK
);
321 ret
= rtl8366_smi_write_reg(smi
, reg
, 0);
325 ret
= rtl8366_smi_read_reg(smi
, RTL8366RB_PHY_ACCESS_DATA_REG
, data
);
332 static int rtl8366rb_write_phy_reg(struct rtl8366_smi
*smi
,
333 u32 phy_no
, u32 page
, u32 addr
, u32 data
)
338 if (phy_no
> RTL8366RB_PHY_NO_MAX
)
341 if (page
> RTL8366RB_PHY_PAGE_MAX
)
344 if (addr
> RTL8366RB_PHY_ADDR_MAX
)
347 ret
= rtl8366_smi_write_reg(smi
, RTL8366RB_PHY_ACCESS_CTRL_REG
,
348 RTL8366RB_PHY_CTRL_WRITE
);
352 reg
= 0x8000 | (1 << (phy_no
+ RTL8366RB_PHY_NO_OFFSET
)) |
353 ((page
<< RTL8366RB_PHY_PAGE_OFFSET
) & RTL8366RB_PHY_PAGE_MASK
) |
354 (addr
& RTL8366RB_PHY_REG_MASK
);
356 ret
= rtl8366_smi_write_reg(smi
, reg
, data
);
363 static int rtl8366rb_get_mib_counter(struct rtl8366_smi
*smi
, int counter
,
364 int port
, unsigned long long *val
)
371 if (port
> RTL8366RB_NUM_PORTS
|| counter
>= RTL8366RB_MIB_COUNT
)
374 addr
= RTL8366RB_MIB_COUNTER_BASE
+
375 RTL8366RB_MIB_COUNTER_PORT_OFFSET
* (port
) +
376 rtl8366rb_mib_counters
[counter
].offset
;
379 * Writing access counter address first
380 * then ASIC will prepare 64bits counter wait for being retrived
382 data
= 0; /* writing data will be discard by ASIC */
383 err
= rtl8366_smi_write_reg(smi
, addr
, data
);
387 /* read MIB control register */
388 err
= rtl8366_smi_read_reg(smi
, RTL8366RB_MIB_CTRL_REG
, &data
);
392 if (data
& RTL8366RB_MIB_CTRL_BUSY_MASK
)
395 if (data
& RTL8366RB_MIB_CTRL_RESET_MASK
)
399 for (i
= rtl8366rb_mib_counters
[counter
].length
; i
> 0; i
--) {
400 err
= rtl8366_smi_read_reg(smi
, addr
+ (i
- 1), &data
);
404 mibvalue
= (mibvalue
<< 16) | (data
& 0xFFFF);
411 static int rtl8366rb_get_vlan_4k(struct rtl8366_smi
*smi
, u32 vid
,
412 struct rtl8366_vlan_4k
*vlan4k
)
418 memset(vlan4k
, '\0', sizeof(struct rtl8366_vlan_4k
));
420 if (vid
>= RTL8366RB_NUM_VIDS
)
424 err
= rtl8366_smi_write_reg(smi
, RTL8366RB_VLAN_TABLE_WRITE_BASE
,
425 vid
& RTL8366RB_VLAN_VID_MASK
);
429 /* write table access control word */
430 err
= rtl8366_smi_write_reg(smi
, RTL8366RB_TABLE_ACCESS_CTRL_REG
,
431 RTL8366RB_TABLE_VLAN_READ_CTRL
);
435 for (i
= 0; i
< 3; i
++) {
436 err
= rtl8366_smi_read_reg(smi
,
437 RTL8366RB_VLAN_TABLE_READ_BASE
+ i
,
444 vlan4k
->untag
= (data
[1] >> RTL8366RB_VLAN_UNTAG_SHIFT
) &
445 RTL8366RB_VLAN_UNTAG_MASK
;
446 vlan4k
->member
= data
[1] & RTL8366RB_VLAN_MEMBER_MASK
;
447 vlan4k
->fid
= data
[2] & RTL8366RB_VLAN_FID_MASK
;
452 static int rtl8366rb_set_vlan_4k(struct rtl8366_smi
*smi
,
453 const struct rtl8366_vlan_4k
*vlan4k
)
459 if (vlan4k
->vid
>= RTL8366RB_NUM_VIDS
||
460 vlan4k
->member
> RTL8366RB_PORT_ALL
||
461 vlan4k
->untag
> RTL8366RB_PORT_ALL
||
462 vlan4k
->fid
> RTL8366RB_FIDMAX
)
465 data
[0] = vlan4k
->vid
& RTL8366RB_VLAN_VID_MASK
;
466 data
[1] = (vlan4k
->member
& RTL8366RB_VLAN_MEMBER_MASK
) |
467 ((vlan4k
->untag
& RTL8366RB_VLAN_UNTAG_MASK
) <<
468 RTL8366RB_VLAN_UNTAG_SHIFT
);
469 data
[2] = vlan4k
->fid
& RTL8366RB_VLAN_FID_MASK
;
471 for (i
= 0; i
< 3; i
++) {
472 err
= rtl8366_smi_write_reg(smi
,
473 RTL8366RB_VLAN_TABLE_WRITE_BASE
+ i
,
479 /* write table access control word */
480 err
= rtl8366_smi_write_reg(smi
, RTL8366RB_TABLE_ACCESS_CTRL_REG
,
481 RTL8366RB_TABLE_VLAN_WRITE_CTRL
);
486 static int rtl8366rb_get_vlan_mc(struct rtl8366_smi
*smi
, u32 index
,
487 struct rtl8366_vlan_mc
*vlanmc
)
493 memset(vlanmc
, '\0', sizeof(struct rtl8366_vlan_mc
));
495 if (index
>= RTL8366RB_NUM_VLANS
)
498 for (i
= 0; i
< 3; i
++) {
499 err
= rtl8366_smi_read_reg(smi
,
500 RTL8366RB_VLAN_MC_BASE(index
) + i
,
506 vlanmc
->vid
= data
[0] & RTL8366RB_VLAN_VID_MASK
;
507 vlanmc
->priority
= (data
[0] >> RTL8366RB_VLAN_PRIORITY_SHIFT
) &
508 RTL8366RB_VLAN_PRIORITY_MASK
;
509 vlanmc
->untag
= (data
[1] >> RTL8366RB_VLAN_UNTAG_SHIFT
) &
510 RTL8366RB_VLAN_UNTAG_MASK
;
511 vlanmc
->member
= data
[1] & RTL8366RB_VLAN_MEMBER_MASK
;
512 vlanmc
->fid
= data
[2] & RTL8366RB_VLAN_FID_MASK
;
517 static int rtl8366rb_set_vlan_mc(struct rtl8366_smi
*smi
, u32 index
,
518 const struct rtl8366_vlan_mc
*vlanmc
)
524 if (index
>= RTL8366RB_NUM_VLANS
||
525 vlanmc
->vid
>= RTL8366RB_NUM_VIDS
||
526 vlanmc
->priority
> RTL8366RB_PRIORITYMAX
||
527 vlanmc
->member
> RTL8366RB_PORT_ALL
||
528 vlanmc
->untag
> RTL8366RB_PORT_ALL
||
529 vlanmc
->fid
> RTL8366RB_FIDMAX
)
532 data
[0] = (vlanmc
->vid
& RTL8366RB_VLAN_VID_MASK
) |
533 ((vlanmc
->priority
& RTL8366RB_VLAN_PRIORITY_MASK
) <<
534 RTL8366RB_VLAN_PRIORITY_SHIFT
);
535 data
[1] = (vlanmc
->member
& RTL8366RB_VLAN_MEMBER_MASK
) |
536 ((vlanmc
->untag
& RTL8366RB_VLAN_UNTAG_MASK
) <<
537 RTL8366RB_VLAN_UNTAG_SHIFT
);
538 data
[2] = vlanmc
->fid
& RTL8366RB_VLAN_FID_MASK
;
540 for (i
= 0; i
< 3; i
++) {
541 err
= rtl8366_smi_write_reg(smi
,
542 RTL8366RB_VLAN_MC_BASE(index
) + i
,
551 static int rtl8366rb_get_mc_index(struct rtl8366_smi
*smi
, int port
, int *val
)
556 if (port
>= RTL8366RB_NUM_PORTS
)
559 err
= rtl8366_smi_read_reg(smi
, RTL8366RB_PORT_VLAN_CTRL_REG(port
),
564 *val
= (data
>> RTL8366RB_PORT_VLAN_CTRL_SHIFT(port
)) &
565 RTL8366RB_PORT_VLAN_CTRL_MASK
;
571 static int rtl8366rb_set_mc_index(struct rtl8366_smi
*smi
, int port
, int index
)
573 if (port
>= RTL8366RB_NUM_PORTS
|| index
>= RTL8366RB_NUM_VLANS
)
576 return rtl8366_smi_rmwr(smi
, RTL8366RB_PORT_VLAN_CTRL_REG(port
),
577 RTL8366RB_PORT_VLAN_CTRL_MASK
<<
578 RTL8366RB_PORT_VLAN_CTRL_SHIFT(port
),
579 (index
& RTL8366RB_PORT_VLAN_CTRL_MASK
) <<
580 RTL8366RB_PORT_VLAN_CTRL_SHIFT(port
));
583 static int rtl8366rb_is_vlan_valid(struct rtl8366_smi
*smi
, unsigned vlan
)
585 unsigned max
= RTL8366RB_NUM_VLANS
;
587 if (smi
->vlan4k_enabled
)
588 max
= RTL8366RB_NUM_VIDS
- 1;
590 if (vlan
== 0 || vlan
>= max
)
596 static int rtl8366rb_enable_vlan(struct rtl8366_smi
*smi
, int enable
)
598 return rtl8366_smi_rmwr(smi
, RTL8366RB_SGCR
, RTL8366RB_SGCR_EN_VLAN
,
599 (enable
) ? RTL8366RB_SGCR_EN_VLAN
: 0);
602 static int rtl8366rb_enable_vlan4k(struct rtl8366_smi
*smi
, int enable
)
604 return rtl8366_smi_rmwr(smi
, RTL8366RB_SGCR
,
605 RTL8366RB_SGCR_EN_VLAN_4KTB
,
606 (enable
) ? RTL8366RB_SGCR_EN_VLAN_4KTB
: 0);
609 static int rtl8366rb_sw_reset_mibs(struct switch_dev
*dev
,
610 const struct switch_attr
*attr
,
611 struct switch_val
*val
)
613 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
615 return rtl8366_smi_rmwr(smi
, RTL8366RB_MIB_CTRL_REG
, 0,
616 RTL8366RB_MIB_CTRL_GLOBAL_RESET
);
619 static int rtl8366rb_sw_get_blinkrate(struct switch_dev
*dev
,
620 const struct switch_attr
*attr
,
621 struct switch_val
*val
)
623 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
626 rtl8366_smi_read_reg(smi
, RTL8366RB_LED_BLINKRATE_REG
, &data
);
628 val
->value
.i
= (data
& (RTL8366RB_LED_BLINKRATE_MASK
));
633 static int rtl8366rb_sw_set_blinkrate(struct switch_dev
*dev
,
634 const struct switch_attr
*attr
,
635 struct switch_val
*val
)
637 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
639 if (val
->value
.i
>= 6)
642 return rtl8366_smi_rmwr(smi
, RTL8366RB_LED_BLINKRATE_REG
,
643 RTL8366RB_LED_BLINKRATE_MASK
,
647 static int rtl8366rb_sw_get_learning_enable(struct switch_dev
*dev
,
648 const struct switch_attr
*attr
,
649 struct switch_val
*val
)
651 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
654 rtl8366_smi_read_reg(smi
, RTL8366RB_SSCR0
, &data
);
655 val
->value
.i
= !data
;
661 static int rtl8366rb_sw_set_learning_enable(struct switch_dev
*dev
,
662 const struct switch_attr
*attr
,
663 struct switch_val
*val
)
665 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
670 portmask
= RTL8366RB_PORT_ALL
;
672 /* set learning for all ports */
673 REG_WR(smi
, RTL8366RB_SSCR0
, portmask
);
675 /* set auto ageing for all ports */
676 REG_WR(smi
, RTL8366RB_SSCR1
, portmask
);
682 static const char *rtl8366rb_speed_str(unsigned speed
)
696 static int rtl8366rb_sw_get_port_link(struct switch_dev
*dev
,
697 const struct switch_attr
*attr
,
698 struct switch_val
*val
)
700 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
701 u32 len
= 0, data
= 0;
703 if (val
->port_vlan
>= RTL8366RB_NUM_PORTS
)
706 memset(smi
->buf
, '\0', sizeof(smi
->buf
));
707 rtl8366_smi_read_reg(smi
, RTL8366RB_PORT_LINK_STATUS_BASE
+
708 (val
->port_vlan
/ 2), &data
);
710 if (val
->port_vlan
% 2)
713 if (data
& RTL8366RB_PORT_STATUS_LINK_MASK
) {
714 len
= snprintf(smi
->buf
, sizeof(smi
->buf
),
715 "port:%d link:up speed:%s %s-duplex %s%s%s",
717 rtl8366rb_speed_str(data
&
718 RTL8366RB_PORT_STATUS_SPEED_MASK
),
719 (data
& RTL8366RB_PORT_STATUS_DUPLEX_MASK
) ?
721 (data
& RTL8366RB_PORT_STATUS_TXPAUSE_MASK
) ?
723 (data
& RTL8366RB_PORT_STATUS_RXPAUSE_MASK
) ?
725 (data
& RTL8366RB_PORT_STATUS_AN_MASK
) ?
728 len
= snprintf(smi
->buf
, sizeof(smi
->buf
), "port:%d link: down",
732 val
->value
.s
= smi
->buf
;
738 static int rtl8366rb_sw_set_port_led(struct switch_dev
*dev
,
739 const struct switch_attr
*attr
,
740 struct switch_val
*val
)
742 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
747 if (val
->port_vlan
>= RTL8366RB_NUM_PORTS
)
750 if (val
->port_vlan
== RTL8366RB_PORT_NUM_CPU
) {
751 reg
= RTL8366RB_LED_BLINKRATE_REG
;
753 data
= val
->value
.i
<< 4;
755 reg
= RTL8366RB_LED_CTRL_REG
;
756 mask
= 0xF << (val
->port_vlan
* 4),
757 data
= val
->value
.i
<< (val
->port_vlan
* 4);
760 return rtl8366_smi_rmwr(smi
, reg
, mask
, data
);
763 static int rtl8366rb_sw_get_port_led(struct switch_dev
*dev
,
764 const struct switch_attr
*attr
,
765 struct switch_val
*val
)
767 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
770 if (val
->port_vlan
>= RTL8366RB_NUM_LEDGROUPS
)
773 rtl8366_smi_read_reg(smi
, RTL8366RB_LED_CTRL_REG
, &data
);
774 val
->value
.i
= (data
>> (val
->port_vlan
* 4)) & 0x000F;
779 static int rtl8366rb_sw_set_port_disable(struct switch_dev
*dev
,
780 const struct switch_attr
*attr
,
781 struct switch_val
*val
)
783 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
786 if (val
->port_vlan
>= RTL8366RB_NUM_PORTS
)
789 mask
= 1 << val
->port_vlan
;
795 return rtl8366_smi_rmwr(smi
, RTL8366RB_PECR
, mask
, data
);
798 static int rtl8366rb_sw_get_port_disable(struct switch_dev
*dev
,
799 const struct switch_attr
*attr
,
800 struct switch_val
*val
)
802 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
805 if (val
->port_vlan
>= RTL8366RB_NUM_PORTS
)
808 rtl8366_smi_read_reg(smi
, RTL8366RB_PECR
, &data
);
809 if (data
& (1 << val
->port_vlan
))
817 static int rtl8366rb_sw_set_port_rate_in(struct switch_dev
*dev
,
818 const struct switch_attr
*attr
,
819 struct switch_val
*val
)
821 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
823 if (val
->port_vlan
>= RTL8366RB_NUM_PORTS
)
826 if (val
->value
.i
> 0 && val
->value
.i
< RTL8366RB_BDTH_SW_MAX
)
827 val
->value
.i
= (val
->value
.i
- 1) / RTL8366RB_BDTH_BASE
;
829 val
->value
.i
= RTL8366RB_BDTH_REG_DEFAULT
;
831 return rtl8366_smi_rmwr(smi
, RTL8366RB_IB_REG(val
->port_vlan
),
832 RTL8366RB_IB_BDTH_MASK
| RTL8366RB_IB_PREIFG_MASK
,
834 (RTL8366RB_QOS_DEFAULT_PREIFG
<< RTL8366RB_IB_PREIFG_OFFSET
));
838 static int rtl8366rb_sw_get_port_rate_in(struct switch_dev
*dev
,
839 const struct switch_attr
*attr
,
840 struct switch_val
*val
)
842 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
845 if (val
->port_vlan
>= RTL8366RB_NUM_PORTS
)
848 rtl8366_smi_read_reg(smi
, RTL8366RB_IB_REG(val
->port_vlan
), &data
);
849 data
&= RTL8366RB_IB_BDTH_MASK
;
850 if (data
< RTL8366RB_IB_BDTH_MASK
)
853 val
->value
.i
= (int)data
* RTL8366RB_BDTH_BASE
;
858 static int rtl8366rb_sw_set_port_rate_out(struct switch_dev
*dev
,
859 const struct switch_attr
*attr
,
860 struct switch_val
*val
)
862 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
864 if (val
->port_vlan
>= RTL8366RB_NUM_PORTS
)
867 rtl8366_smi_rmwr(smi
, RTL8366RB_EB_PREIFG_REG
,
868 RTL8366RB_EB_PREIFG_MASK
,
869 (RTL8366RB_QOS_DEFAULT_PREIFG
<< RTL8366RB_EB_PREIFG_OFFSET
));
871 if (val
->value
.i
> 0 && val
->value
.i
< RTL8366RB_BDTH_SW_MAX
)
872 val
->value
.i
= (val
->value
.i
- 1) / RTL8366RB_BDTH_BASE
;
874 val
->value
.i
= RTL8366RB_BDTH_REG_DEFAULT
;
876 return rtl8366_smi_rmwr(smi
, RTL8366RB_EB_REG(val
->port_vlan
),
877 RTL8366RB_EB_BDTH_MASK
, val
->value
.i
);
881 static int rtl8366rb_sw_get_port_rate_out(struct switch_dev
*dev
,
882 const struct switch_attr
*attr
,
883 struct switch_val
*val
)
885 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
888 if (val
->port_vlan
>= RTL8366RB_NUM_PORTS
)
891 rtl8366_smi_read_reg(smi
, RTL8366RB_EB_REG(val
->port_vlan
), &data
);
892 data
&= RTL8366RB_EB_BDTH_MASK
;
893 if (data
< RTL8366RB_EB_BDTH_MASK
)
896 val
->value
.i
= (int)data
* RTL8366RB_BDTH_BASE
;
901 static int rtl8366rb_sw_set_qos_enable(struct switch_dev
*dev
,
902 const struct switch_attr
*attr
,
903 struct switch_val
*val
)
905 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
909 data
= RTL8366RB_QOS_MASK
;
913 return rtl8366_smi_rmwr(smi
, RTL8366RB_SGCR
, RTL8366RB_QOS_MASK
, data
);
916 static int rtl8366rb_sw_get_qos_enable(struct switch_dev
*dev
,
917 const struct switch_attr
*attr
,
918 struct switch_val
*val
)
920 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
923 rtl8366_smi_read_reg(smi
, RTL8366RB_SGCR
, &data
);
924 if (data
& RTL8366RB_QOS_MASK
)
932 static int rtl8366rb_sw_reset_port_mibs(struct switch_dev
*dev
,
933 const struct switch_attr
*attr
,
934 struct switch_val
*val
)
936 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
938 if (val
->port_vlan
>= RTL8366RB_NUM_PORTS
)
941 return rtl8366_smi_rmwr(smi
, RTL8366RB_MIB_CTRL_REG
, 0,
942 RTL8366RB_MIB_CTRL_PORT_RESET(val
->port_vlan
));
945 static int rtl8366rb_sw_reset_switch(struct switch_dev
*dev
)
947 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
950 err
= rtl8366rb_reset_chip(smi
);
954 err
= rtl8366rb_hw_init(smi
);
958 return rtl8366_reset_vlan(smi
);
961 static struct switch_attr rtl8366rb_globals
[] = {
963 .type
= SWITCH_TYPE_INT
,
964 .name
= "enable_learning",
965 .description
= "Enable learning, enable aging",
966 .set
= rtl8366rb_sw_set_learning_enable
,
967 .get
= rtl8366rb_sw_get_learning_enable
,
970 .type
= SWITCH_TYPE_INT
,
971 .name
= "enable_vlan",
972 .description
= "Enable VLAN mode",
973 .set
= rtl8366_sw_set_vlan_enable
,
974 .get
= rtl8366_sw_get_vlan_enable
,
978 .type
= SWITCH_TYPE_INT
,
979 .name
= "enable_vlan4k",
980 .description
= "Enable VLAN 4K mode",
981 .set
= rtl8366_sw_set_vlan_enable
,
982 .get
= rtl8366_sw_get_vlan_enable
,
986 .type
= SWITCH_TYPE_NOVAL
,
987 .name
= "reset_mibs",
988 .description
= "Reset all MIB counters",
989 .set
= rtl8366rb_sw_reset_mibs
,
991 .type
= SWITCH_TYPE_INT
,
993 .description
= "Get/Set LED blinking rate (0 = 43ms, 1 = 84ms,"
994 " 2 = 120ms, 3 = 170ms, 4 = 340ms, 5 = 670ms)",
995 .set
= rtl8366rb_sw_set_blinkrate
,
996 .get
= rtl8366rb_sw_get_blinkrate
,
999 .type
= SWITCH_TYPE_INT
,
1000 .name
= "enable_qos",
1001 .description
= "Enable QOS",
1002 .set
= rtl8366rb_sw_set_qos_enable
,
1003 .get
= rtl8366rb_sw_get_qos_enable
,
1008 static struct switch_attr rtl8366rb_port
[] = {
1010 .type
= SWITCH_TYPE_STRING
,
1012 .description
= "Get port link information",
1015 .get
= rtl8366rb_sw_get_port_link
,
1017 .type
= SWITCH_TYPE_NOVAL
,
1018 .name
= "reset_mib",
1019 .description
= "Reset single port MIB counters",
1020 .set
= rtl8366rb_sw_reset_port_mibs
,
1022 .type
= SWITCH_TYPE_STRING
,
1024 .description
= "Get MIB counters for port",
1027 .get
= rtl8366_sw_get_port_mib
,
1029 .type
= SWITCH_TYPE_INT
,
1031 .description
= "Get/Set port group (0 - 3) led mode (0 - 15)",
1033 .set
= rtl8366rb_sw_set_port_led
,
1034 .get
= rtl8366rb_sw_get_port_led
,
1036 .type
= SWITCH_TYPE_INT
,
1038 .description
= "Get/Set port state (enabled or disabled)",
1040 .set
= rtl8366rb_sw_set_port_disable
,
1041 .get
= rtl8366rb_sw_get_port_disable
,
1043 .type
= SWITCH_TYPE_INT
,
1045 .description
= "Get/Set port ingress (incoming) bandwidth limit in kbps",
1046 .max
= RTL8366RB_BDTH_SW_MAX
,
1047 .set
= rtl8366rb_sw_set_port_rate_in
,
1048 .get
= rtl8366rb_sw_get_port_rate_in
,
1050 .type
= SWITCH_TYPE_INT
,
1052 .description
= "Get/Set port egress (outgoing) bandwidth limit in kbps",
1053 .max
= RTL8366RB_BDTH_SW_MAX
,
1054 .set
= rtl8366rb_sw_set_port_rate_out
,
1055 .get
= rtl8366rb_sw_get_port_rate_out
,
1059 static struct switch_attr rtl8366rb_vlan
[] = {
1061 .type
= SWITCH_TYPE_STRING
,
1063 .description
= "Get vlan information",
1066 .get
= rtl8366_sw_get_vlan_info
,
1070 static const struct switch_dev_ops rtl8366_ops
= {
1072 .attr
= rtl8366rb_globals
,
1073 .n_attr
= ARRAY_SIZE(rtl8366rb_globals
),
1076 .attr
= rtl8366rb_port
,
1077 .n_attr
= ARRAY_SIZE(rtl8366rb_port
),
1080 .attr
= rtl8366rb_vlan
,
1081 .n_attr
= ARRAY_SIZE(rtl8366rb_vlan
),
1084 .get_vlan_ports
= rtl8366_sw_get_vlan_ports
,
1085 .set_vlan_ports
= rtl8366_sw_set_vlan_ports
,
1086 .get_port_pvid
= rtl8366_sw_get_port_pvid
,
1087 .set_port_pvid
= rtl8366_sw_set_port_pvid
,
1088 .reset_switch
= rtl8366rb_sw_reset_switch
,
1091 static int rtl8366rb_switch_init(struct rtl8366_smi
*smi
)
1093 struct switch_dev
*dev
= &smi
->sw_dev
;
1096 dev
->name
= "RTL8366RB";
1097 dev
->cpu_port
= RTL8366RB_PORT_NUM_CPU
;
1098 dev
->ports
= RTL8366RB_NUM_PORTS
;
1099 dev
->vlans
= RTL8366RB_NUM_VIDS
;
1100 dev
->ops
= &rtl8366_ops
;
1101 dev
->devname
= dev_name(smi
->parent
);
1103 err
= register_switch(dev
, NULL
);
1105 dev_err(smi
->parent
, "switch registration failed\n");
1110 static void rtl8366rb_switch_cleanup(struct rtl8366_smi
*smi
)
1112 unregister_switch(&smi
->sw_dev
);
1115 static int rtl8366rb_mii_read(struct mii_bus
*bus
, int addr
, int reg
)
1117 struct rtl8366_smi
*smi
= bus
->priv
;
1121 err
= rtl8366rb_read_phy_reg(smi
, addr
, 0, reg
, &val
);
1128 static int rtl8366rb_mii_write(struct mii_bus
*bus
, int addr
, int reg
, u16 val
)
1130 struct rtl8366_smi
*smi
= bus
->priv
;
1134 err
= rtl8366rb_write_phy_reg(smi
, addr
, 0, reg
, val
);
1136 (void) rtl8366rb_read_phy_reg(smi
, addr
, 0, reg
, &t
);
1141 static int rtl8366rb_mii_bus_match(struct mii_bus
*bus
)
1143 return (bus
->read
== rtl8366rb_mii_read
&&
1144 bus
->write
== rtl8366rb_mii_write
);
1147 static int rtl8366rb_setup(struct rtl8366_smi
*smi
)
1151 ret
= rtl8366rb_reset_chip(smi
);
1155 ret
= rtl8366rb_hw_init(smi
);
1159 static int rtl8366rb_detect(struct rtl8366_smi
*smi
)
1165 ret
= rtl8366_smi_read_reg(smi
, RTL8366RB_CHIP_ID_REG
, &chip_id
);
1167 dev_err(smi
->parent
, "unable to read chip id\n");
1172 case RTL8366RB_CHIP_ID_8366
:
1175 dev_err(smi
->parent
, "unknown chip id (%04x)\n", chip_id
);
1179 ret
= rtl8366_smi_read_reg(smi
, RTL8366RB_CHIP_VERSION_CTRL_REG
,
1182 dev_err(smi
->parent
, "unable to read chip version\n");
1186 dev_info(smi
->parent
, "RTL%04x ver. %u chip found\n",
1187 chip_id
, chip_ver
& RTL8366RB_CHIP_VERSION_MASK
);
1192 static struct rtl8366_smi_ops rtl8366rb_smi_ops
= {
1193 .detect
= rtl8366rb_detect
,
1194 .setup
= rtl8366rb_setup
,
1196 .mii_read
= rtl8366rb_mii_read
,
1197 .mii_write
= rtl8366rb_mii_write
,
1199 .get_vlan_mc
= rtl8366rb_get_vlan_mc
,
1200 .set_vlan_mc
= rtl8366rb_set_vlan_mc
,
1201 .get_vlan_4k
= rtl8366rb_get_vlan_4k
,
1202 .set_vlan_4k
= rtl8366rb_set_vlan_4k
,
1203 .get_mc_index
= rtl8366rb_get_mc_index
,
1204 .set_mc_index
= rtl8366rb_set_mc_index
,
1205 .get_mib_counter
= rtl8366rb_get_mib_counter
,
1206 .is_vlan_valid
= rtl8366rb_is_vlan_valid
,
1207 .enable_vlan
= rtl8366rb_enable_vlan
,
1208 .enable_vlan4k
= rtl8366rb_enable_vlan4k
,
1211 static int __init
rtl8366rb_probe(struct platform_device
*pdev
)
1213 static int rtl8366_smi_version_printed
;
1214 struct rtl8366rb_platform_data
*pdata
;
1215 struct rtl8366_smi
*smi
;
1218 if (!rtl8366_smi_version_printed
++)
1219 printk(KERN_NOTICE RTL8366RB_DRIVER_DESC
1220 " version " RTL8366RB_DRIVER_VER
"\n");
1222 pdata
= pdev
->dev
.platform_data
;
1224 dev_err(&pdev
->dev
, "no platform data specified\n");
1229 smi
= rtl8366_smi_alloc(&pdev
->dev
);
1235 smi
->gpio_sda
= pdata
->gpio_sda
;
1236 smi
->gpio_sck
= pdata
->gpio_sck
;
1237 smi
->ops
= &rtl8366rb_smi_ops
;
1238 smi
->cpu_port
= RTL8366RB_PORT_NUM_CPU
;
1239 smi
->num_ports
= RTL8366RB_NUM_PORTS
;
1240 smi
->num_vlan_mc
= RTL8366RB_NUM_VLANS
;
1241 smi
->mib_counters
= rtl8366rb_mib_counters
;
1242 smi
->num_mib_counters
= ARRAY_SIZE(rtl8366rb_mib_counters
);
1244 err
= rtl8366_smi_init(smi
);
1248 platform_set_drvdata(pdev
, smi
);
1250 err
= rtl8366rb_switch_init(smi
);
1252 goto err_clear_drvdata
;
1257 platform_set_drvdata(pdev
, NULL
);
1258 rtl8366_smi_cleanup(smi
);
1265 static int rtl8366rb_phy_config_init(struct phy_device
*phydev
)
1267 if (!rtl8366rb_mii_bus_match(phydev
->bus
))
1273 static int rtl8366rb_phy_config_aneg(struct phy_device
*phydev
)
1278 static struct phy_driver rtl8366rb_phy_driver
= {
1279 .phy_id
= 0x001cc960,
1280 .name
= "Realtek RTL8366RB",
1281 .phy_id_mask
= 0x1ffffff0,
1282 .features
= PHY_GBIT_FEATURES
,
1283 .config_aneg
= rtl8366rb_phy_config_aneg
,
1284 .config_init
= rtl8366rb_phy_config_init
,
1285 .read_status
= genphy_read_status
,
1287 .owner
= THIS_MODULE
,
1291 static int __devexit
rtl8366rb_remove(struct platform_device
*pdev
)
1293 struct rtl8366_smi
*smi
= platform_get_drvdata(pdev
);
1296 rtl8366rb_switch_cleanup(smi
);
1297 platform_set_drvdata(pdev
, NULL
);
1298 rtl8366_smi_cleanup(smi
);
1305 static struct platform_driver rtl8366rb_driver
= {
1307 .name
= RTL8366RB_DRIVER_NAME
,
1308 .owner
= THIS_MODULE
,
1310 .probe
= rtl8366rb_probe
,
1311 .remove
= __devexit_p(rtl8366rb_remove
),
1314 static int __init
rtl8366rb_module_init(void)
1317 ret
= platform_driver_register(&rtl8366rb_driver
);
1321 ret
= phy_driver_register(&rtl8366rb_phy_driver
);
1323 goto err_platform_unregister
;
1327 err_platform_unregister
:
1328 platform_driver_unregister(&rtl8366rb_driver
);
1331 module_init(rtl8366rb_module_init
);
1333 static void __exit
rtl8366rb_module_exit(void)
1335 phy_driver_unregister(&rtl8366rb_phy_driver
);
1336 platform_driver_unregister(&rtl8366rb_driver
);
1338 module_exit(rtl8366rb_module_exit
);
1340 MODULE_DESCRIPTION(RTL8366RB_DRIVER_DESC
);
1341 MODULE_VERSION(RTL8366RB_DRIVER_VER
);
1342 MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
1343 MODULE_AUTHOR("Antti Seppälä <a.seppala@gmail.com>");
1344 MODULE_AUTHOR("Roman Yeryomin <roman@advem.lv>");
1345 MODULE_LICENSE("GPL v2");
1346 MODULE_ALIAS("platform:" RTL8366RB_DRIVER_NAME
);