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/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 struct device
*parent
;
169 struct rtl8366_smi smi
;
170 struct switch_dev dev
;
173 static struct rtl8366_mib_counter rtl8366rb_mib_counters
[] = {
174 { 0, 0, 4, "IfInOctets" },
175 { 0, 4, 4, "EtherStatsOctets" },
176 { 0, 8, 2, "EtherStatsUnderSizePkts" },
177 { 0, 10, 2, "EtherFragments" },
178 { 0, 12, 2, "EtherStatsPkts64Octets" },
179 { 0, 14, 2, "EtherStatsPkts65to127Octets" },
180 { 0, 16, 2, "EtherStatsPkts128to255Octets" },
181 { 0, 18, 2, "EtherStatsPkts256to511Octets" },
182 { 0, 20, 2, "EtherStatsPkts512to1023Octets" },
183 { 0, 22, 2, "EtherStatsPkts1024to1518Octets" },
184 { 0, 24, 2, "EtherOversizeStats" },
185 { 0, 26, 2, "EtherStatsJabbers" },
186 { 0, 28, 2, "IfInUcastPkts" },
187 { 0, 30, 2, "EtherStatsMulticastPkts" },
188 { 0, 32, 2, "EtherStatsBroadcastPkts" },
189 { 0, 34, 2, "EtherStatsDropEvents" },
190 { 0, 36, 2, "Dot3StatsFCSErrors" },
191 { 0, 38, 2, "Dot3StatsSymbolErrors" },
192 { 0, 40, 2, "Dot3InPauseFrames" },
193 { 0, 42, 2, "Dot3ControlInUnknownOpcodes" },
194 { 0, 44, 4, "IfOutOctets" },
195 { 0, 48, 2, "Dot3StatsSingleCollisionFrames" },
196 { 0, 50, 2, "Dot3StatMultipleCollisionFrames" },
197 { 0, 52, 2, "Dot3sDeferredTransmissions" },
198 { 0, 54, 2, "Dot3StatsLateCollisions" },
199 { 0, 56, 2, "EtherStatsCollisions" },
200 { 0, 58, 2, "Dot3StatsExcessiveCollisions" },
201 { 0, 60, 2, "Dot3OutPauseFrames" },
202 { 0, 62, 2, "Dot1dBasePortDelayExceededDiscards" },
203 { 0, 64, 2, "Dot1dTpPortInDiscards" },
204 { 0, 66, 2, "IfOutUcastPkts" },
205 { 0, 68, 2, "IfOutMulticastPkts" },
206 { 0, 70, 2, "IfOutBroadcastPkts" },
209 #define REG_WR(_smi, _reg, _val) \
211 err = rtl8366_smi_write_reg(_smi, _reg, _val); \
216 #define REG_RMW(_smi, _reg, _mask, _val) \
218 err = rtl8366_smi_rmwr(_smi, _reg, _mask, _val); \
223 static inline struct rtl8366rb
*smi_to_rtl8366rb(struct rtl8366_smi
*smi
)
225 return container_of(smi
, struct rtl8366rb
, smi
);
228 static inline struct rtl8366rb
*sw_to_rtl8366rb(struct switch_dev
*sw
)
230 return container_of(sw
, struct rtl8366rb
, dev
);
233 static inline struct rtl8366_smi
*sw_to_rtl8366_smi(struct switch_dev
*sw
)
235 struct rtl8366rb
*rtl
= sw_to_rtl8366rb(sw
);
239 static int rtl8366rb_reset_chip(struct rtl8366_smi
*smi
)
244 rtl8366_smi_write_reg(smi
, RTL8366RB_RESET_CTRL_REG
,
245 RTL8366RB_CHIP_CTRL_RESET_HW
);
248 if (rtl8366_smi_read_reg(smi
, RTL8366RB_RESET_CTRL_REG
, &data
))
251 if (!(data
& RTL8366RB_CHIP_CTRL_RESET_HW
))
256 printk("Timeout waiting for the switch to reset\n");
263 static int rtl8366rb_hw_init(struct rtl8366_smi
*smi
)
267 /* set maximum packet length to 1536 bytes */
268 REG_RMW(smi
, RTL8366RB_SGCR
, RTL8366RB_SGCR_MAX_LENGTH_MASK
,
269 RTL8366RB_SGCR_MAX_LENGTH_1536
);
271 /* enable all ports */
272 REG_WR(smi
, RTL8366RB_PECR
, 0);
274 /* disable learning for all ports */
275 REG_WR(smi
, RTL8366RB_SSCR0
, RTL8366RB_PORT_ALL
);
277 /* disable auto ageing for all ports */
278 REG_WR(smi
, RTL8366RB_SSCR1
, RTL8366RB_PORT_ALL
);
281 * discard VLAN tagged packets if the port is not a member of
282 * the VLAN with which the packets is associated.
284 REG_WR(smi
, RTL8366RB_VLAN_INGRESS_CTRL2_REG
, RTL8366RB_PORT_ALL
);
286 /* don't drop packets whose DA has not been learned */
287 REG_RMW(smi
, RTL8366RB_SSCR2
, RTL8366RB_SSCR2_DROP_UNKNOWN_DA
, 0);
292 static int rtl8366rb_read_phy_reg(struct rtl8366_smi
*smi
,
293 u32 phy_no
, u32 page
, u32 addr
, u32
*data
)
298 if (phy_no
> RTL8366RB_PHY_NO_MAX
)
301 if (page
> RTL8366RB_PHY_PAGE_MAX
)
304 if (addr
> RTL8366RB_PHY_ADDR_MAX
)
307 ret
= rtl8366_smi_write_reg(smi
, RTL8366RB_PHY_ACCESS_CTRL_REG
,
308 RTL8366RB_PHY_CTRL_READ
);
312 reg
= 0x8000 | (1 << (phy_no
+ RTL8366RB_PHY_NO_OFFSET
)) |
313 ((page
<< RTL8366RB_PHY_PAGE_OFFSET
) & RTL8366RB_PHY_PAGE_MASK
) |
314 (addr
& RTL8366RB_PHY_REG_MASK
);
316 ret
= rtl8366_smi_write_reg(smi
, reg
, 0);
320 ret
= rtl8366_smi_read_reg(smi
, RTL8366RB_PHY_ACCESS_DATA_REG
, data
);
327 static int rtl8366rb_write_phy_reg(struct rtl8366_smi
*smi
,
328 u32 phy_no
, u32 page
, u32 addr
, u32 data
)
333 if (phy_no
> RTL8366RB_PHY_NO_MAX
)
336 if (page
> RTL8366RB_PHY_PAGE_MAX
)
339 if (addr
> RTL8366RB_PHY_ADDR_MAX
)
342 ret
= rtl8366_smi_write_reg(smi
, RTL8366RB_PHY_ACCESS_CTRL_REG
,
343 RTL8366RB_PHY_CTRL_WRITE
);
347 reg
= 0x8000 | (1 << (phy_no
+ RTL8366RB_PHY_NO_OFFSET
)) |
348 ((page
<< RTL8366RB_PHY_PAGE_OFFSET
) & RTL8366RB_PHY_PAGE_MASK
) |
349 (addr
& RTL8366RB_PHY_REG_MASK
);
351 ret
= rtl8366_smi_write_reg(smi
, reg
, data
);
358 static int rtl8366rb_get_mib_counter(struct rtl8366_smi
*smi
, int counter
,
359 int port
, unsigned long long *val
)
366 if (port
> RTL8366RB_NUM_PORTS
|| counter
>= RTL8366RB_MIB_COUNT
)
369 addr
= RTL8366RB_MIB_COUNTER_BASE
+
370 RTL8366RB_MIB_COUNTER_PORT_OFFSET
* (port
) +
371 rtl8366rb_mib_counters
[counter
].offset
;
374 * Writing access counter address first
375 * then ASIC will prepare 64bits counter wait for being retrived
377 data
= 0; /* writing data will be discard by ASIC */
378 err
= rtl8366_smi_write_reg(smi
, addr
, data
);
382 /* read MIB control register */
383 err
= rtl8366_smi_read_reg(smi
, RTL8366RB_MIB_CTRL_REG
, &data
);
387 if (data
& RTL8366RB_MIB_CTRL_BUSY_MASK
)
390 if (data
& RTL8366RB_MIB_CTRL_RESET_MASK
)
394 for (i
= rtl8366rb_mib_counters
[counter
].length
; i
> 0; i
--) {
395 err
= rtl8366_smi_read_reg(smi
, addr
+ (i
- 1), &data
);
399 mibvalue
= (mibvalue
<< 16) | (data
& 0xFFFF);
406 static int rtl8366rb_get_vlan_4k(struct rtl8366_smi
*smi
, u32 vid
,
407 struct rtl8366_vlan_4k
*vlan4k
)
413 memset(vlan4k
, '\0', sizeof(struct rtl8366_vlan_4k
));
415 if (vid
>= RTL8366RB_NUM_VIDS
)
419 err
= rtl8366_smi_write_reg(smi
, RTL8366RB_VLAN_TABLE_WRITE_BASE
,
420 vid
& RTL8366RB_VLAN_VID_MASK
);
424 /* write table access control word */
425 err
= rtl8366_smi_write_reg(smi
, RTL8366RB_TABLE_ACCESS_CTRL_REG
,
426 RTL8366RB_TABLE_VLAN_READ_CTRL
);
430 for (i
= 0; i
< 3; i
++) {
431 err
= rtl8366_smi_read_reg(smi
,
432 RTL8366RB_VLAN_TABLE_READ_BASE
+ i
,
439 vlan4k
->untag
= (data
[1] >> RTL8366RB_VLAN_UNTAG_SHIFT
) &
440 RTL8366RB_VLAN_UNTAG_MASK
;
441 vlan4k
->member
= data
[1] & RTL8366RB_VLAN_MEMBER_MASK
;
442 vlan4k
->fid
= data
[2] & RTL8366RB_VLAN_FID_MASK
;
447 static int rtl8366rb_set_vlan_4k(struct rtl8366_smi
*smi
,
448 const struct rtl8366_vlan_4k
*vlan4k
)
454 if (vlan4k
->vid
>= RTL8366RB_NUM_VIDS
||
455 vlan4k
->member
> RTL8366RB_PORT_ALL
||
456 vlan4k
->untag
> RTL8366RB_PORT_ALL
||
457 vlan4k
->fid
> RTL8366RB_FIDMAX
)
460 data
[0] = vlan4k
->vid
& RTL8366RB_VLAN_VID_MASK
;
461 data
[1] = (vlan4k
->member
& RTL8366RB_VLAN_MEMBER_MASK
) |
462 ((vlan4k
->untag
& RTL8366RB_VLAN_UNTAG_MASK
) <<
463 RTL8366RB_VLAN_UNTAG_SHIFT
);
464 data
[2] = vlan4k
->fid
& RTL8366RB_VLAN_FID_MASK
;
466 for (i
= 0; i
< 3; i
++) {
467 err
= rtl8366_smi_write_reg(smi
,
468 RTL8366RB_VLAN_TABLE_WRITE_BASE
+ i
,
474 /* write table access control word */
475 err
= rtl8366_smi_write_reg(smi
, RTL8366RB_TABLE_ACCESS_CTRL_REG
,
476 RTL8366RB_TABLE_VLAN_WRITE_CTRL
);
481 static int rtl8366rb_get_vlan_mc(struct rtl8366_smi
*smi
, u32 index
,
482 struct rtl8366_vlan_mc
*vlanmc
)
488 memset(vlanmc
, '\0', sizeof(struct rtl8366_vlan_mc
));
490 if (index
>= RTL8366RB_NUM_VLANS
)
493 for (i
= 0; i
< 3; i
++) {
494 err
= rtl8366_smi_read_reg(smi
,
495 RTL8366RB_VLAN_MC_BASE(index
) + i
,
501 vlanmc
->vid
= data
[0] & RTL8366RB_VLAN_VID_MASK
;
502 vlanmc
->priority
= (data
[0] >> RTL8366RB_VLAN_PRIORITY_SHIFT
) &
503 RTL8366RB_VLAN_PRIORITY_MASK
;
504 vlanmc
->untag
= (data
[1] >> RTL8366RB_VLAN_UNTAG_SHIFT
) &
505 RTL8366RB_VLAN_UNTAG_MASK
;
506 vlanmc
->member
= data
[1] & RTL8366RB_VLAN_MEMBER_MASK
;
507 vlanmc
->fid
= data
[2] & RTL8366RB_VLAN_FID_MASK
;
512 static int rtl8366rb_set_vlan_mc(struct rtl8366_smi
*smi
, u32 index
,
513 const struct rtl8366_vlan_mc
*vlanmc
)
519 if (index
>= RTL8366RB_NUM_VLANS
||
520 vlanmc
->vid
>= RTL8366RB_NUM_VIDS
||
521 vlanmc
->priority
> RTL8366RB_PRIORITYMAX
||
522 vlanmc
->member
> RTL8366RB_PORT_ALL
||
523 vlanmc
->untag
> RTL8366RB_PORT_ALL
||
524 vlanmc
->fid
> RTL8366RB_FIDMAX
)
527 data
[0] = (vlanmc
->vid
& RTL8366RB_VLAN_VID_MASK
) |
528 ((vlanmc
->priority
& RTL8366RB_VLAN_PRIORITY_MASK
) <<
529 RTL8366RB_VLAN_PRIORITY_SHIFT
);
530 data
[1] = (vlanmc
->member
& RTL8366RB_VLAN_MEMBER_MASK
) |
531 ((vlanmc
->untag
& RTL8366RB_VLAN_UNTAG_MASK
) <<
532 RTL8366RB_VLAN_UNTAG_SHIFT
);
533 data
[2] = vlanmc
->fid
& RTL8366RB_VLAN_FID_MASK
;
535 for (i
= 0; i
< 3; i
++) {
536 err
= rtl8366_smi_write_reg(smi
,
537 RTL8366RB_VLAN_MC_BASE(index
) + i
,
546 static int rtl8366rb_get_mc_index(struct rtl8366_smi
*smi
, int port
, int *val
)
551 if (port
>= RTL8366RB_NUM_PORTS
)
554 err
= rtl8366_smi_read_reg(smi
, RTL8366RB_PORT_VLAN_CTRL_REG(port
),
559 *val
= (data
>> RTL8366RB_PORT_VLAN_CTRL_SHIFT(port
)) &
560 RTL8366RB_PORT_VLAN_CTRL_MASK
;
566 static int rtl8366rb_set_mc_index(struct rtl8366_smi
*smi
, int port
, int index
)
568 if (port
>= RTL8366RB_NUM_PORTS
|| index
>= RTL8366RB_NUM_VLANS
)
571 return rtl8366_smi_rmwr(smi
, RTL8366RB_PORT_VLAN_CTRL_REG(port
),
572 RTL8366RB_PORT_VLAN_CTRL_MASK
<<
573 RTL8366RB_PORT_VLAN_CTRL_SHIFT(port
),
574 (index
& RTL8366RB_PORT_VLAN_CTRL_MASK
) <<
575 RTL8366RB_PORT_VLAN_CTRL_SHIFT(port
));
578 static int rtl8366rb_vlan_set_vlan(struct rtl8366_smi
*smi
, int enable
)
580 return rtl8366_smi_rmwr(smi
, RTL8366RB_SGCR
, RTL8366RB_SGCR_EN_VLAN
,
581 (enable
) ? RTL8366RB_SGCR_EN_VLAN
: 0);
584 static int rtl8366rb_vlan_set_4ktable(struct rtl8366_smi
*smi
, int enable
)
586 return rtl8366_smi_rmwr(smi
, RTL8366RB_SGCR
,
587 RTL8366RB_SGCR_EN_VLAN_4KTB
,
588 (enable
) ? RTL8366RB_SGCR_EN_VLAN_4KTB
: 0);
591 static int rtl8366rb_sw_reset_mibs(struct switch_dev
*dev
,
592 const struct switch_attr
*attr
,
593 struct switch_val
*val
)
595 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
597 return rtl8366_smi_rmwr(smi
, RTL8366RB_MIB_CTRL_REG
, 0,
598 RTL8366RB_MIB_CTRL_GLOBAL_RESET
);
601 static int rtl8366rb_sw_get_vlan_enable(struct switch_dev
*dev
,
602 const struct switch_attr
*attr
,
603 struct switch_val
*val
)
605 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
608 if (attr
->ofs
== 1) {
609 rtl8366_smi_read_reg(smi
, RTL8366RB_SGCR
, &data
);
611 if (data
& RTL8366RB_SGCR_EN_VLAN
)
615 } else if (attr
->ofs
== 2) {
616 rtl8366_smi_read_reg(smi
, RTL8366RB_SGCR
, &data
);
618 if (data
& RTL8366RB_SGCR_EN_VLAN_4KTB
)
627 static int rtl8366rb_sw_get_blinkrate(struct switch_dev
*dev
,
628 const struct switch_attr
*attr
,
629 struct switch_val
*val
)
631 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
634 rtl8366_smi_read_reg(smi
, RTL8366RB_LED_BLINKRATE_REG
, &data
);
636 val
->value
.i
= (data
& (RTL8366RB_LED_BLINKRATE_MASK
));
641 static int rtl8366rb_sw_set_blinkrate(struct switch_dev
*dev
,
642 const struct switch_attr
*attr
,
643 struct switch_val
*val
)
645 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
647 if (val
->value
.i
>= 6)
650 return rtl8366_smi_rmwr(smi
, RTL8366RB_LED_BLINKRATE_REG
,
651 RTL8366RB_LED_BLINKRATE_MASK
,
655 static int rtl8366rb_sw_set_vlan_enable(struct switch_dev
*dev
,
656 const struct switch_attr
*attr
,
657 struct switch_val
*val
)
659 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
662 return rtl8366rb_vlan_set_vlan(smi
, val
->value
.i
);
664 return rtl8366rb_vlan_set_4ktable(smi
, val
->value
.i
);
667 static const char *rtl8366rb_speed_str(unsigned speed
)
681 static int rtl8366rb_sw_get_port_link(struct switch_dev
*dev
,
682 const struct switch_attr
*attr
,
683 struct switch_val
*val
)
685 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
686 u32 len
= 0, data
= 0;
688 if (val
->port_vlan
>= RTL8366RB_NUM_PORTS
)
691 memset(smi
->buf
, '\0', sizeof(smi
->buf
));
692 rtl8366_smi_read_reg(smi
, RTL8366RB_PORT_LINK_STATUS_BASE
+
693 (val
->port_vlan
/ 2), &data
);
695 if (val
->port_vlan
% 2)
698 if (data
& RTL8366RB_PORT_STATUS_LINK_MASK
) {
699 len
= snprintf(smi
->buf
, sizeof(smi
->buf
),
700 "port:%d link:up speed:%s %s-duplex %s%s%s",
702 rtl8366rb_speed_str(data
&
703 RTL8366RB_PORT_STATUS_SPEED_MASK
),
704 (data
& RTL8366RB_PORT_STATUS_DUPLEX_MASK
) ?
706 (data
& RTL8366RB_PORT_STATUS_TXPAUSE_MASK
) ?
708 (data
& RTL8366RB_PORT_STATUS_RXPAUSE_MASK
) ?
710 (data
& RTL8366RB_PORT_STATUS_AN_MASK
) ?
713 len
= snprintf(smi
->buf
, sizeof(smi
->buf
), "port:%d link: down",
717 val
->value
.s
= smi
->buf
;
723 static int rtl8366rb_sw_get_vlan_info(struct switch_dev
*dev
,
724 const struct switch_attr
*attr
,
725 struct switch_val
*val
)
729 struct rtl8366_vlan_4k vlan4k
;
730 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
731 char *buf
= smi
->buf
;
734 if (val
->port_vlan
== 0 || val
->port_vlan
>= RTL8366RB_NUM_VLANS
)
737 memset(buf
, '\0', sizeof(smi
->buf
));
739 err
= rtl8366rb_get_vlan_4k(smi
, val
->port_vlan
, &vlan4k
);
743 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
,
744 "VLAN %d: Ports: '", vlan4k
.vid
);
746 for (i
= 0; i
< RTL8366RB_NUM_PORTS
; i
++) {
747 if (!(vlan4k
.member
& (1 << i
)))
750 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
, "%d%s", i
,
751 (vlan4k
.untag
& (1 << i
)) ? "" : "t");
754 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
,
755 "', members=%04x, untag=%04x, fid=%u",
756 vlan4k
.member
, vlan4k
.untag
, vlan4k
.fid
);
764 static int rtl8366rb_sw_set_port_led(struct switch_dev
*dev
,
765 const struct switch_attr
*attr
,
766 struct switch_val
*val
)
768 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
773 if (val
->port_vlan
>= RTL8366RB_NUM_PORTS
)
776 if (val
->port_vlan
== RTL8366RB_PORT_NUM_CPU
) {
777 reg
= RTL8366RB_LED_BLINKRATE_REG
;
779 data
= val
->value
.i
<< 4;
781 reg
= RTL8366RB_LED_CTRL_REG
;
782 mask
= 0xF << (val
->port_vlan
* 4),
783 data
= val
->value
.i
<< (val
->port_vlan
* 4);
786 return rtl8366_smi_rmwr(smi
, RTL8366RB_LED_BLINKRATE_REG
, mask
, data
);
789 static int rtl8366rb_sw_get_port_led(struct switch_dev
*dev
,
790 const struct switch_attr
*attr
,
791 struct switch_val
*val
)
793 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
796 if (val
->port_vlan
>= RTL8366RB_NUM_LEDGROUPS
)
799 rtl8366_smi_read_reg(smi
, RTL8366RB_LED_CTRL_REG
, &data
);
800 val
->value
.i
= (data
>> (val
->port_vlan
* 4)) & 0x000F;
805 static int rtl8366rb_sw_reset_port_mibs(struct switch_dev
*dev
,
806 const struct switch_attr
*attr
,
807 struct switch_val
*val
)
809 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
811 if (val
->port_vlan
>= RTL8366RB_NUM_PORTS
)
814 return rtl8366_smi_rmwr(smi
, RTL8366RB_MIB_CTRL_REG
, 0,
815 RTL8366RB_MIB_CTRL_PORT_RESET(val
->port_vlan
));
818 static int rtl8366rb_sw_get_port_mib(struct switch_dev
*dev
,
819 const struct switch_attr
*attr
,
820 struct switch_val
*val
)
822 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
824 unsigned long long counter
= 0;
825 char *buf
= smi
->buf
;
827 if (val
->port_vlan
>= RTL8366RB_NUM_PORTS
)
830 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
,
831 "Port %d MIB counters\n",
834 for (i
= 0; i
< ARRAY_SIZE(rtl8366rb_mib_counters
); ++i
) {
835 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
,
836 "%-36s: ", rtl8366rb_mib_counters
[i
].name
);
837 if (!rtl8366rb_get_mib_counter(smi
, i
, val
->port_vlan
, &counter
))
838 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
,
841 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
,
850 static int rtl8366rb_sw_get_vlan_ports(struct switch_dev
*dev
,
851 struct switch_val
*val
)
853 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
854 struct switch_port
*port
;
855 struct rtl8366_vlan_4k vlan4k
;
858 if (val
->port_vlan
== 0 || val
->port_vlan
>= RTL8366RB_NUM_VLANS
)
861 rtl8366rb_get_vlan_4k(smi
, val
->port_vlan
, &vlan4k
);
863 port
= &val
->value
.ports
[0];
865 for (i
= 0; i
< RTL8366RB_NUM_PORTS
; i
++) {
866 if (!(vlan4k
.member
& BIT(i
)))
870 port
->flags
= (vlan4k
.untag
& BIT(i
)) ?
871 0 : BIT(SWITCH_PORT_FLAG_TAGGED
);
878 static int rtl8366rb_sw_set_vlan_ports(struct switch_dev
*dev
,
879 struct switch_val
*val
)
881 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
882 struct switch_port
*port
;
887 if (val
->port_vlan
== 0 || val
->port_vlan
>= RTL8366RB_NUM_VLANS
)
890 port
= &val
->value
.ports
[0];
891 for (i
= 0; i
< val
->len
; i
++, port
++) {
892 member
|= BIT(port
->id
);
894 if (!(port
->flags
& BIT(SWITCH_PORT_FLAG_TAGGED
)))
895 untag
|= BIT(port
->id
);
898 return rtl8366_set_vlan(smi
, val
->port_vlan
, member
, untag
, 0);
901 static int rtl8366rb_sw_get_port_pvid(struct switch_dev
*dev
, int port
, int *val
)
903 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
904 return rtl8366_get_pvid(smi
, port
, val
);
907 static int rtl8366rb_sw_set_port_pvid(struct switch_dev
*dev
, int port
, int val
)
909 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
910 return rtl8366_set_pvid(smi
, port
, val
);
913 static int rtl8366rb_sw_reset_switch(struct switch_dev
*dev
)
915 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
918 err
= rtl8366rb_reset_chip(smi
);
922 err
= rtl8366rb_hw_init(smi
);
926 return rtl8366_reset_vlan(smi
);
929 static struct switch_attr rtl8366rb_globals
[] = {
931 .type
= SWITCH_TYPE_INT
,
932 .name
= "enable_vlan",
933 .description
= "Enable VLAN mode",
934 .set
= rtl8366rb_sw_set_vlan_enable
,
935 .get
= rtl8366rb_sw_get_vlan_enable
,
939 .type
= SWITCH_TYPE_INT
,
940 .name
= "enable_vlan4k",
941 .description
= "Enable VLAN 4K mode",
942 .set
= rtl8366rb_sw_set_vlan_enable
,
943 .get
= rtl8366rb_sw_get_vlan_enable
,
947 .type
= SWITCH_TYPE_NOVAL
,
948 .name
= "reset_mibs",
949 .description
= "Reset all MIB counters",
950 .set
= rtl8366rb_sw_reset_mibs
,
952 .type
= SWITCH_TYPE_INT
,
954 .description
= "Get/Set LED blinking rate (0 = 43ms, 1 = 84ms,"
955 " 2 = 120ms, 3 = 170ms, 4 = 340ms, 5 = 670ms)",
956 .set
= rtl8366rb_sw_set_blinkrate
,
957 .get
= rtl8366rb_sw_get_blinkrate
,
962 static struct switch_attr rtl8366rb_port
[] = {
964 .type
= SWITCH_TYPE_STRING
,
966 .description
= "Get port link information",
969 .get
= rtl8366rb_sw_get_port_link
,
971 .type
= SWITCH_TYPE_NOVAL
,
973 .description
= "Reset single port MIB counters",
974 .set
= rtl8366rb_sw_reset_port_mibs
,
976 .type
= SWITCH_TYPE_STRING
,
978 .description
= "Get MIB counters for port",
981 .get
= rtl8366rb_sw_get_port_mib
,
983 .type
= SWITCH_TYPE_INT
,
985 .description
= "Get/Set port group (0 - 3) led mode (0 - 15)",
987 .set
= rtl8366rb_sw_set_port_led
,
988 .get
= rtl8366rb_sw_get_port_led
,
992 static struct switch_attr rtl8366rb_vlan
[] = {
994 .type
= SWITCH_TYPE_STRING
,
996 .description
= "Get vlan information",
999 .get
= rtl8366rb_sw_get_vlan_info
,
1004 static struct switch_dev rtl8366_switch_dev
= {
1006 .cpu_port
= RTL8366RB_PORT_NUM_CPU
,
1007 .ports
= RTL8366RB_NUM_PORTS
,
1008 .vlans
= RTL8366RB_NUM_VLANS
,
1010 .attr
= rtl8366rb_globals
,
1011 .n_attr
= ARRAY_SIZE(rtl8366rb_globals
),
1014 .attr
= rtl8366rb_port
,
1015 .n_attr
= ARRAY_SIZE(rtl8366rb_port
),
1018 .attr
= rtl8366rb_vlan
,
1019 .n_attr
= ARRAY_SIZE(rtl8366rb_vlan
),
1022 .get_vlan_ports
= rtl8366rb_sw_get_vlan_ports
,
1023 .set_vlan_ports
= rtl8366rb_sw_set_vlan_ports
,
1024 .get_port_pvid
= rtl8366rb_sw_get_port_pvid
,
1025 .set_port_pvid
= rtl8366rb_sw_set_port_pvid
,
1026 .reset_switch
= rtl8366rb_sw_reset_switch
,
1029 static int rtl8366rb_switch_init(struct rtl8366rb
*rtl
)
1031 struct switch_dev
*dev
= &rtl
->dev
;
1034 memcpy(dev
, &rtl8366_switch_dev
, sizeof(struct switch_dev
));
1036 dev
->devname
= dev_name(rtl
->parent
);
1038 err
= register_switch(dev
, NULL
);
1040 dev_err(rtl
->parent
, "switch registration failed\n");
1045 static void rtl8366rb_switch_cleanup(struct rtl8366rb
*rtl
)
1047 unregister_switch(&rtl
->dev
);
1050 static int rtl8366rb_mii_read(struct mii_bus
*bus
, int addr
, int reg
)
1052 struct rtl8366_smi
*smi
= bus
->priv
;
1056 err
= rtl8366rb_read_phy_reg(smi
, addr
, 0, reg
, &val
);
1063 static int rtl8366rb_mii_write(struct mii_bus
*bus
, int addr
, int reg
, u16 val
)
1065 struct rtl8366_smi
*smi
= bus
->priv
;
1069 err
= rtl8366rb_write_phy_reg(smi
, addr
, 0, reg
, val
);
1071 (void) rtl8366rb_read_phy_reg(smi
, addr
, 0, reg
, &t
);
1076 static int rtl8366rb_mii_bus_match(struct mii_bus
*bus
)
1078 return (bus
->read
== rtl8366rb_mii_read
&&
1079 bus
->write
== rtl8366rb_mii_write
);
1082 static int rtl8366rb_setup(struct rtl8366rb
*rtl
)
1084 struct rtl8366_smi
*smi
= &rtl
->smi
;
1087 ret
= rtl8366rb_reset_chip(smi
);
1091 ret
= rtl8366rb_hw_init(smi
);
1095 static int rtl8366rb_detect(struct rtl8366_smi
*smi
)
1101 ret
= rtl8366_smi_read_reg(smi
, RTL8366RB_CHIP_ID_REG
, &chip_id
);
1103 dev_err(smi
->parent
, "unable to read chip id\n");
1108 case RTL8366RB_CHIP_ID_8366
:
1111 dev_err(smi
->parent
, "unknown chip id (%04x)\n", chip_id
);
1115 ret
= rtl8366_smi_read_reg(smi
, RTL8366RB_CHIP_VERSION_CTRL_REG
,
1118 dev_err(smi
->parent
, "unable to read chip version\n");
1122 dev_info(smi
->parent
, "RTL%04x ver. %u chip found\n",
1123 chip_id
, chip_ver
& RTL8366RB_CHIP_VERSION_MASK
);
1128 static struct rtl8366_smi_ops rtl8366rb_smi_ops
= {
1129 .detect
= rtl8366rb_detect
,
1130 .mii_read
= rtl8366rb_mii_read
,
1131 .mii_write
= rtl8366rb_mii_write
,
1133 .get_vlan_mc
= rtl8366rb_get_vlan_mc
,
1134 .set_vlan_mc
= rtl8366rb_set_vlan_mc
,
1135 .get_vlan_4k
= rtl8366rb_get_vlan_4k
,
1136 .set_vlan_4k
= rtl8366rb_set_vlan_4k
,
1137 .get_mc_index
= rtl8366rb_get_mc_index
,
1138 .set_mc_index
= rtl8366rb_set_mc_index
,
1139 .get_mib_counter
= rtl8366rb_get_mib_counter
,
1142 static int __init
rtl8366rb_probe(struct platform_device
*pdev
)
1144 static int rtl8366_smi_version_printed
;
1145 struct rtl8366rb_platform_data
*pdata
;
1146 struct rtl8366rb
*rtl
;
1147 struct rtl8366_smi
*smi
;
1150 if (!rtl8366_smi_version_printed
++)
1151 printk(KERN_NOTICE RTL8366RB_DRIVER_DESC
1152 " version " RTL8366RB_DRIVER_VER
"\n");
1154 pdata
= pdev
->dev
.platform_data
;
1156 dev_err(&pdev
->dev
, "no platform data specified\n");
1161 rtl
= kzalloc(sizeof(*rtl
), GFP_KERNEL
);
1163 dev_err(&pdev
->dev
, "no memory for private data\n");
1168 rtl
->parent
= &pdev
->dev
;
1171 smi
->parent
= &pdev
->dev
;
1172 smi
->gpio_sda
= pdata
->gpio_sda
;
1173 smi
->gpio_sck
= pdata
->gpio_sck
;
1174 smi
->ops
= &rtl8366rb_smi_ops
;
1175 smi
->cpu_port
= RTL8366RB_PORT_NUM_CPU
;
1176 smi
->num_ports
= RTL8366RB_NUM_PORTS
;
1177 smi
->num_vlan_mc
= RTL8366RB_NUM_VLANS
;
1178 smi
->mib_counters
= rtl8366rb_mib_counters
;
1179 smi
->num_mib_counters
= ARRAY_SIZE(rtl8366rb_mib_counters
);
1181 err
= rtl8366_smi_init(smi
);
1185 platform_set_drvdata(pdev
, rtl
);
1187 err
= rtl8366rb_setup(rtl
);
1189 goto err_clear_drvdata
;
1191 err
= rtl8366rb_switch_init(rtl
);
1193 goto err_clear_drvdata
;
1198 platform_set_drvdata(pdev
, NULL
);
1199 rtl8366_smi_cleanup(smi
);
1206 static int rtl8366rb_phy_config_init(struct phy_device
*phydev
)
1208 if (!rtl8366rb_mii_bus_match(phydev
->bus
))
1214 static int rtl8366rb_phy_config_aneg(struct phy_device
*phydev
)
1219 static struct phy_driver rtl8366rb_phy_driver
= {
1220 .phy_id
= 0x001cc960,
1221 .name
= "Realtek RTL8366RB",
1222 .phy_id_mask
= 0x1ffffff0,
1223 .features
= PHY_GBIT_FEATURES
,
1224 .config_aneg
= rtl8366rb_phy_config_aneg
,
1225 .config_init
= rtl8366rb_phy_config_init
,
1226 .read_status
= genphy_read_status
,
1228 .owner
= THIS_MODULE
,
1232 static int __devexit
rtl8366rb_remove(struct platform_device
*pdev
)
1234 struct rtl8366rb
*rtl
= platform_get_drvdata(pdev
);
1237 rtl8366rb_switch_cleanup(rtl
);
1238 platform_set_drvdata(pdev
, NULL
);
1239 rtl8366_smi_cleanup(&rtl
->smi
);
1246 static struct platform_driver rtl8366rb_driver
= {
1248 .name
= RTL8366RB_DRIVER_NAME
,
1249 .owner
= THIS_MODULE
,
1251 .probe
= rtl8366rb_probe
,
1252 .remove
= __devexit_p(rtl8366rb_remove
),
1255 static int __init
rtl8366rb_module_init(void)
1258 ret
= platform_driver_register(&rtl8366rb_driver
);
1262 ret
= phy_driver_register(&rtl8366rb_phy_driver
);
1264 goto err_platform_unregister
;
1268 err_platform_unregister
:
1269 platform_driver_unregister(&rtl8366rb_driver
);
1272 module_init(rtl8366rb_module_init
);
1274 static void __exit
rtl8366rb_module_exit(void)
1276 phy_driver_unregister(&rtl8366rb_phy_driver
);
1277 platform_driver_unregister(&rtl8366rb_driver
);
1279 module_exit(rtl8366rb_module_exit
);
1281 MODULE_DESCRIPTION(RTL8366RB_DRIVER_DESC
);
1282 MODULE_VERSION(RTL8366RB_DRIVER_VER
);
1283 MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
1284 MODULE_AUTHOR("Antti Seppälä <a.seppala@gmail.com>");
1285 MODULE_LICENSE("GPL v2");
1286 MODULE_ALIAS("platform:" RTL8366RB_DRIVER_NAME
);