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 #ifdef CONFIG_RTL8366S_PHY_DEBUG_FS
24 #include <linux/debugfs.h>
27 #define RTL8366RB_DRIVER_DESC "Realtek RTL8366RB ethernet switch driver"
28 #define RTL8366RB_DRIVER_VER "0.2.2"
30 #define RTL8366RB_PHY_NO_MAX 4
31 #define RTL8366RB_PHY_PAGE_MAX 7
32 #define RTL8366RB_PHY_ADDR_MAX 31
34 #define RTL8366RB_CHIP_GLOBAL_CTRL_REG 0x0000
35 #define RTL8366RB_CHIP_CTRL_VLAN (1 << 13)
36 #define RTL8366RB_CHIP_CTRL_VLAN_4KTB (1 << 14)
38 /* Switch Global Configuration register */
39 #define RTL8366RB_SGCR 0x0000
40 #define RTL8366RB_SGCR_EN_BC_STORM_CTRL BIT(0)
41 #define RTL8366RB_SGCR_MAX_LENGTH(_x) (_x << 4)
42 #define RTL8366RB_SGCR_MAX_LENGTH_MASK RTL8366RB_SGCR_MAX_LENGTH(0x3)
43 #define RTL8366RB_SGCR_MAX_LENGTH_1522 RTL8366RB_SGCR_MAX_LENGTH(0x0)
44 #define RTL8366RB_SGCR_MAX_LENGTH_1536 RTL8366RB_SGCR_MAX_LENGTH(0x1)
45 #define RTL8366RB_SGCR_MAX_LENGTH_1552 RTL8366RB_SGCR_MAX_LENGTH(0x2)
46 #define RTL8366RB_SGCR_MAX_LENGTH_9216 RTL8366RB_SGCR_MAX_LENGTH(0x3)
48 /* Port Enable Control register */
49 #define RTL8366RB_PECR 0x0001
51 /* Switch Security Control registers */
52 #define RTL8366RB_SSCR0 0x0002
53 #define RTL8366RB_SSCR1 0x0003
54 #define RTL8366RB_SSCR2 0x0004
55 #define RTL8366RB_SSCR2_DROP_UNKNOWN_DA BIT(0)
57 #define RTL8366RB_RESET_CTRL_REG 0x0100
58 #define RTL8366RB_CHIP_CTRL_RESET_HW 1
59 #define RTL8366RB_CHIP_CTRL_RESET_SW (1 << 1)
61 #define RTL8366RB_CHIP_VERSION_CTRL_REG 0x050A
62 #define RTL8366RB_CHIP_VERSION_MASK 0xf
63 #define RTL8366RB_CHIP_ID_REG 0x0509
64 #define RTL8366RB_CHIP_ID_8366 0x5937
66 /* PHY registers control */
67 #define RTL8366RB_PHY_ACCESS_CTRL_REG 0x8000
68 #define RTL8366RB_PHY_ACCESS_DATA_REG 0x8002
70 #define RTL8366RB_PHY_CTRL_READ 1
71 #define RTL8366RB_PHY_CTRL_WRITE 0
73 #define RTL8366RB_PHY_REG_MASK 0x1f
74 #define RTL8366RB_PHY_PAGE_OFFSET 5
75 #define RTL8366RB_PHY_PAGE_MASK (0xf << 5)
76 #define RTL8366RB_PHY_NO_OFFSET 9
77 #define RTL8366RB_PHY_NO_MASK (0x1f << 9)
79 /* LED control registers */
80 #define RTL8366RB_LED_BLINKRATE_REG 0x0430
81 #define RTL8366RB_LED_BLINKRATE_BIT 0
82 #define RTL8366RB_LED_BLINKRATE_MASK 0x0007
84 #define RTL8366RB_LED_CTRL_REG 0x0431
85 #define RTL8366RB_LED_0_1_CTRL_REG 0x0432
86 #define RTL8366RB_LED_2_3_CTRL_REG 0x0433
88 #define RTL8366RB_MIB_COUNT 33
89 #define RTL8366RB_GLOBAL_MIB_COUNT 1
90 #define RTL8366RB_MIB_COUNTER_PORT_OFFSET 0x0050
91 #define RTL8366RB_MIB_COUNTER_BASE 0x1000
92 #define RTL8366RB_MIB_CTRL_REG 0x13F0
93 #define RTL8366RB_MIB_CTRL_USER_MASK 0x0FFC
94 #define RTL8366RB_MIB_CTRL_BUSY_MASK BIT(0)
95 #define RTL8366RB_MIB_CTRL_RESET_MASK BIT(1)
96 #define RTL8366RB_MIB_CTRL_PORT_RESET(_p) BIT(2 + (_p))
97 #define RTL8366RB_MIB_CTRL_GLOBAL_RESET BIT(11)
99 #define RTL8366RB_PORT_VLAN_CTRL_BASE 0x0063
100 #define RTL8366RB_PORT_VLAN_CTRL_REG(_p) \
101 (RTL8366RB_PORT_VLAN_CTRL_BASE + (_p) / 4)
102 #define RTL8366RB_PORT_VLAN_CTRL_MASK 0xf
103 #define RTL8366RB_PORT_VLAN_CTRL_SHIFT(_p) (4 * ((_p) % 4))
106 #define RTL8366RB_VLAN_TABLE_READ_BASE 0x018C
107 #define RTL8366RB_VLAN_TABLE_WRITE_BASE 0x0185
110 #define RTL8366RB_TABLE_ACCESS_CTRL_REG 0x0180
111 #define RTL8366RB_TABLE_VLAN_READ_CTRL 0x0E01
112 #define RTL8366RB_TABLE_VLAN_WRITE_CTRL 0x0F01
114 #define RTL8366RB_VLAN_MEMCONF_BASE 0x0020
117 #define RTL8366RB_PORT_LINK_STATUS_BASE 0x0014
118 #define RTL8366RB_PORT_STATUS_SPEED_MASK 0x0003
119 #define RTL8366RB_PORT_STATUS_DUPLEX_MASK 0x0004
120 #define RTL8366RB_PORT_STATUS_LINK_MASK 0x0010
121 #define RTL8366RB_PORT_STATUS_TXPAUSE_MASK 0x0020
122 #define RTL8366RB_PORT_STATUS_RXPAUSE_MASK 0x0040
123 #define RTL8366RB_PORT_STATUS_AN_MASK 0x0080
126 #define RTL8366RB_PORT_NUM_CPU 5
127 #define RTL8366RB_NUM_PORTS 6
128 #define RTL8366RB_NUM_VLANS 16
129 #define RTL8366RB_NUM_LEDGROUPS 4
130 #define RTL8366RB_NUM_VIDS 4096
131 #define RTL8366RB_PRIORITYMAX 7
132 #define RTL8366RB_FIDMAX 7
135 #define RTL8366RB_PORT_1 (1 << 0) /* In userspace port 0 */
136 #define RTL8366RB_PORT_2 (1 << 1) /* In userspace port 1 */
137 #define RTL8366RB_PORT_3 (1 << 2) /* In userspace port 2 */
138 #define RTL8366RB_PORT_4 (1 << 3) /* In userspace port 3 */
139 #define RTL8366RB_PORT_5 (1 << 4) /* In userspace port 4 */
141 #define RTL8366RB_PORT_CPU (1 << 5) /* CPU port */
143 #define RTL8366RB_PORT_ALL (RTL8366RB_PORT_1 | \
150 #define RTL8366RB_PORT_ALL_BUT_CPU (RTL8366RB_PORT_1 | \
156 #define RTL8366RB_PORT_ALL_EXTERNAL (RTL8366RB_PORT_1 | \
161 #define RTL8366RB_PORT_ALL_INTERNAL RTL8366RB_PORT_CPU
164 struct device
*parent
;
165 struct rtl8366_smi smi
;
166 struct switch_dev dev
;
168 #ifdef CONFIG_RTL8366S_PHY_DEBUG_FS
169 struct dentry
*debugfs_root
;
173 struct rtl8366rb_vlan_mc
{
185 struct rtl8366rb_vlan_4k
{
194 #ifdef CONFIG_RTL8366S_PHY_DEBUG_FS
204 static struct mib_counter rtl8366rb_mib_counters
[RTL8366RB_MIB_COUNT
] = {
205 { 0, 4, "IfInOctets" },
206 { 4, 4, "EtherStatsOctets" },
207 { 8, 2, "EtherStatsUnderSizePkts" },
208 { 10, 2, "EtherFragments" },
209 { 12, 2, "EtherStatsPkts64Octets" },
210 { 14, 2, "EtherStatsPkts65to127Octets" },
211 { 16, 2, "EtherStatsPkts128to255Octets" },
212 { 18, 2, "EtherStatsPkts256to511Octets" },
213 { 20, 2, "EtherStatsPkts512to1023Octets" },
214 { 22, 2, "EtherStatsPkts1024to1518Octets" },
215 { 24, 2, "EtherOversizeStats" },
216 { 26, 2, "EtherStatsJabbers" },
217 { 28, 2, "IfInUcastPkts" },
218 { 30, 2, "EtherStatsMulticastPkts" },
219 { 32, 2, "EtherStatsBroadcastPkts" },
220 { 34, 2, "EtherStatsDropEvents" },
221 { 36, 2, "Dot3StatsFCSErrors" },
222 { 38, 2, "Dot3StatsSymbolErrors" },
223 { 40, 2, "Dot3InPauseFrames" },
224 { 42, 2, "Dot3ControlInUnknownOpcodes" },
225 { 44, 4, "IfOutOctets" },
226 { 48, 2, "Dot3StatsSingleCollisionFrames" },
227 { 50, 2, "Dot3StatMultipleCollisionFrames" },
228 { 52, 2, "Dot3sDeferredTransmissions" },
229 { 54, 2, "Dot3StatsLateCollisions" },
230 { 56, 2, "EtherStatsCollisions" },
231 { 58, 2, "Dot3StatsExcessiveCollisions" },
232 { 60, 2, "Dot3OutPauseFrames" },
233 { 62, 2, "Dot1dBasePortDelayExceededDiscards" },
234 { 64, 2, "Dot1dTpPortInDiscards" },
235 { 66, 2, "IfOutUcastPkts" },
236 { 68, 2, "IfOutMulticastPkts" },
237 { 70, 2, "IfOutBroadcastPkts" },
240 #define REG_WR(_smi, _reg, _val) \
242 err = rtl8366_smi_write_reg(_smi, _reg, _val); \
247 #define REG_RMW(_smi, _reg, _mask, _val) \
249 err = rtl8366_smi_rmwr(_smi, _reg, _mask, _val); \
254 static inline struct rtl8366rb
*smi_to_rtl8366rb(struct rtl8366_smi
*smi
)
256 return container_of(smi
, struct rtl8366rb
, smi
);
259 static inline struct rtl8366rb
*sw_to_rtl8366rb(struct switch_dev
*sw
)
261 return container_of(sw
, struct rtl8366rb
, dev
);
264 static inline struct rtl8366_smi
*sw_to_rtl8366_smi(struct switch_dev
*sw
)
266 struct rtl8366rb
*rtl
= sw_to_rtl8366rb(sw
);
270 static int rtl8366rb_reset_chip(struct rtl8366_smi
*smi
)
275 rtl8366_smi_write_reg(smi
, RTL8366RB_RESET_CTRL_REG
,
276 RTL8366RB_CHIP_CTRL_RESET_HW
);
279 if (rtl8366_smi_read_reg(smi
, RTL8366RB_RESET_CTRL_REG
, &data
))
282 if (!(data
& RTL8366RB_CHIP_CTRL_RESET_HW
))
287 printk("Timeout waiting for the switch to reset\n");
294 static int rtl8366rb_hw_init(struct rtl8366_smi
*smi
)
298 /* set maximum packet length to 1536 bytes */
299 REG_RMW(smi
, RTL8366RB_SGCR
, RTL8366RB_SGCR_MAX_LENGTH_MASK
,
300 RTL8366RB_SGCR_MAX_LENGTH_1536
);
302 /* enable all ports */
303 REG_WR(smi
, RTL8366RB_PECR
, 0);
305 /* disable learning for all ports */
306 REG_WR(smi
, RTL8366RB_SSCR0
, RTL8366RB_PORT_ALL
);
308 /* disable auto ageing for all ports */
309 REG_WR(smi
, RTL8366RB_SSCR1
, RTL8366RB_PORT_ALL
);
311 /* don't drop packets whose DA has not been learned */
312 REG_RMW(smi
, RTL8366RB_SSCR2
, RTL8366RB_SSCR2_DROP_UNKNOWN_DA
, 0);
317 static int rtl8366rb_read_phy_reg(struct rtl8366_smi
*smi
,
318 u32 phy_no
, u32 page
, u32 addr
, u32
*data
)
323 if (phy_no
> RTL8366RB_PHY_NO_MAX
)
326 if (page
> RTL8366RB_PHY_PAGE_MAX
)
329 if (addr
> RTL8366RB_PHY_ADDR_MAX
)
332 ret
= rtl8366_smi_write_reg(smi
, RTL8366RB_PHY_ACCESS_CTRL_REG
,
333 RTL8366RB_PHY_CTRL_READ
);
337 reg
= 0x8000 | (1 << (phy_no
+ RTL8366RB_PHY_NO_OFFSET
)) |
338 ((page
<< RTL8366RB_PHY_PAGE_OFFSET
) & RTL8366RB_PHY_PAGE_MASK
) |
339 (addr
& RTL8366RB_PHY_REG_MASK
);
341 ret
= rtl8366_smi_write_reg(smi
, reg
, 0);
345 ret
= rtl8366_smi_read_reg(smi
, RTL8366RB_PHY_ACCESS_DATA_REG
, data
);
352 static int rtl8366rb_write_phy_reg(struct rtl8366_smi
*smi
,
353 u32 phy_no
, u32 page
, u32 addr
, u32 data
)
358 if (phy_no
> RTL8366RB_PHY_NO_MAX
)
361 if (page
> RTL8366RB_PHY_PAGE_MAX
)
364 if (addr
> RTL8366RB_PHY_ADDR_MAX
)
367 ret
= rtl8366_smi_write_reg(smi
, RTL8366RB_PHY_ACCESS_CTRL_REG
,
368 RTL8366RB_PHY_CTRL_WRITE
);
372 reg
= 0x8000 | (1 << (phy_no
+ RTL8366RB_PHY_NO_OFFSET
)) |
373 ((page
<< RTL8366RB_PHY_PAGE_OFFSET
) & RTL8366RB_PHY_PAGE_MASK
) |
374 (addr
& RTL8366RB_PHY_REG_MASK
);
376 ret
= rtl8366_smi_write_reg(smi
, reg
, data
);
383 static int rtl8366_get_mib_counter(struct rtl8366_smi
*smi
, int counter
,
384 int port
, unsigned long long *val
)
391 if (port
> RTL8366RB_NUM_PORTS
|| counter
>= RTL8366RB_MIB_COUNT
)
394 addr
= RTL8366RB_MIB_COUNTER_BASE
+
395 RTL8366RB_MIB_COUNTER_PORT_OFFSET
* (port
) +
396 rtl8366rb_mib_counters
[counter
].offset
;
399 * Writing access counter address first
400 * then ASIC will prepare 64bits counter wait for being retrived
402 data
= 0; /* writing data will be discard by ASIC */
403 err
= rtl8366_smi_write_reg(smi
, addr
, data
);
407 /* read MIB control register */
408 err
= rtl8366_smi_read_reg(smi
, RTL8366RB_MIB_CTRL_REG
, &data
);
412 if (data
& RTL8366RB_MIB_CTRL_BUSY_MASK
)
415 if (data
& RTL8366RB_MIB_CTRL_RESET_MASK
)
419 for (i
= rtl8366rb_mib_counters
[counter
].length
; i
> 0; i
--) {
420 err
= rtl8366_smi_read_reg(smi
, addr
+ (i
- 1), &data
);
424 mibvalue
= (mibvalue
<< 16) | (data
& 0xFFFF);
431 static int rtl8366rb_get_vlan_4k(struct rtl8366_smi
*smi
, u32 vid
,
432 struct rtl8366_vlan_4k
*vlan4k
)
434 struct rtl8366rb_vlan_4k vlan4k_priv
;
439 memset(vlan4k
, '\0', sizeof(struct rtl8366_vlan_4k
));
440 vlan4k_priv
.vid
= vid
;
442 if (vid
>= RTL8366RB_NUM_VIDS
)
445 tableaddr
= (u16
*)&vlan4k_priv
;
449 err
= rtl8366_smi_write_reg(smi
, RTL8366RB_VLAN_TABLE_WRITE_BASE
, data
);
453 /* write table access control word */
454 err
= rtl8366_smi_write_reg(smi
, RTL8366RB_TABLE_ACCESS_CTRL_REG
,
455 RTL8366RB_TABLE_VLAN_READ_CTRL
);
459 err
= rtl8366_smi_read_reg(smi
, RTL8366RB_VLAN_TABLE_READ_BASE
, &data
);
466 err
= rtl8366_smi_read_reg(smi
, RTL8366RB_VLAN_TABLE_READ_BASE
+ 1,
474 err
= rtl8366_smi_read_reg(smi
, RTL8366RB_VLAN_TABLE_READ_BASE
+ 2,
481 vlan4k
->untag
= vlan4k_priv
.untag
;
482 vlan4k
->member
= vlan4k_priv
.member
;
483 vlan4k
->fid
= vlan4k_priv
.fid
;
488 static int rtl8366rb_set_vlan_4k(struct rtl8366_smi
*smi
,
489 const struct rtl8366_vlan_4k
*vlan4k
)
491 struct rtl8366rb_vlan_4k vlan4k_priv
;
496 if (vlan4k
->vid
>= RTL8366RB_NUM_VIDS
||
497 vlan4k
->member
> RTL8366RB_PORT_ALL
||
498 vlan4k
->untag
> RTL8366RB_PORT_ALL
||
499 vlan4k
->fid
> RTL8366RB_FIDMAX
)
502 vlan4k_priv
.vid
= vlan4k
->vid
;
503 vlan4k_priv
.untag
= vlan4k
->untag
;
504 vlan4k_priv
.member
= vlan4k
->member
;
505 vlan4k_priv
.fid
= vlan4k
->fid
;
507 tableaddr
= (u16
*)&vlan4k_priv
;
511 err
= rtl8366_smi_write_reg(smi
, RTL8366RB_VLAN_TABLE_WRITE_BASE
, data
);
519 err
= rtl8366_smi_write_reg(smi
, RTL8366RB_VLAN_TABLE_WRITE_BASE
+ 1,
528 err
= rtl8366_smi_write_reg(smi
, RTL8366RB_VLAN_TABLE_WRITE_BASE
+ 2,
533 /* write table access control word */
534 err
= rtl8366_smi_write_reg(smi
, RTL8366RB_TABLE_ACCESS_CTRL_REG
,
535 RTL8366RB_TABLE_VLAN_WRITE_CTRL
);
540 static int rtl8366rb_get_vlan_mc(struct rtl8366_smi
*smi
, u32 index
,
541 struct rtl8366_vlan_mc
*vlanmc
)
543 struct rtl8366rb_vlan_mc vlanmc_priv
;
549 memset(vlanmc
, '\0', sizeof(struct rtl8366_vlan_mc
));
551 if (index
>= RTL8366RB_NUM_VLANS
)
554 tableaddr
= (u16
*)&vlanmc_priv
;
556 addr
= RTL8366RB_VLAN_MEMCONF_BASE
+ (index
* 3);
557 err
= rtl8366_smi_read_reg(smi
, addr
, &data
);
564 addr
= RTL8366RB_VLAN_MEMCONF_BASE
+ 1 + (index
* 3);
565 err
= rtl8366_smi_read_reg(smi
, addr
, &data
);
572 addr
= RTL8366RB_VLAN_MEMCONF_BASE
+ 2 + (index
* 3);
573 err
= rtl8366_smi_read_reg(smi
, addr
, &data
);
579 vlanmc
->vid
= vlanmc_priv
.vid
;
580 vlanmc
->priority
= vlanmc_priv
.priority
;
581 vlanmc
->untag
= vlanmc_priv
.untag
;
582 vlanmc
->member
= vlanmc_priv
.member
;
583 vlanmc
->fid
= vlanmc_priv
.fid
;
588 static int rtl8366rb_set_vlan_mc(struct rtl8366_smi
*smi
, u32 index
,
589 const struct rtl8366_vlan_mc
*vlanmc
)
591 struct rtl8366rb_vlan_mc vlanmc_priv
;
597 if (index
>= RTL8366RB_NUM_VLANS
||
598 vlanmc
->vid
>= RTL8366RB_NUM_VIDS
||
599 vlanmc
->priority
> RTL8366RB_PRIORITYMAX
||
600 vlanmc
->member
> RTL8366RB_PORT_ALL
||
601 vlanmc
->untag
> RTL8366RB_PORT_ALL
||
602 vlanmc
->fid
> RTL8366RB_FIDMAX
)
605 vlanmc_priv
.vid
= vlanmc
->vid
;
606 vlanmc_priv
.priority
= vlanmc
->priority
;
607 vlanmc_priv
.untag
= vlanmc
->untag
;
608 vlanmc_priv
.member
= vlanmc
->member
;
609 vlanmc_priv
.stag_mbr
= 0;
610 vlanmc_priv
.stag_idx
= 0;
611 vlanmc_priv
.fid
= vlanmc
->fid
;
613 addr
= RTL8366RB_VLAN_MEMCONF_BASE
+ (index
* 3);
615 tableaddr
= (u16
*)&vlanmc_priv
;
618 err
= rtl8366_smi_write_reg(smi
, addr
, data
);
622 addr
= RTL8366RB_VLAN_MEMCONF_BASE
+ 1 + (index
* 3);
627 err
= rtl8366_smi_write_reg(smi
, addr
, data
);
631 addr
= RTL8366RB_VLAN_MEMCONF_BASE
+ 2 + (index
* 3);
636 err
= rtl8366_smi_write_reg(smi
, addr
, data
);
642 static int rtl8366rb_get_mc_index(struct rtl8366_smi
*smi
, int port
, int *val
)
647 if (port
>= RTL8366RB_NUM_PORTS
)
650 err
= rtl8366_smi_read_reg(smi
, RTL8366RB_PORT_VLAN_CTRL_REG(port
),
655 *val
= (data
>> RTL8366RB_PORT_VLAN_CTRL_SHIFT(port
)) &
656 RTL8366RB_PORT_VLAN_CTRL_MASK
;
662 static int rtl8366rb_set_mc_index(struct rtl8366_smi
*smi
, int port
, int index
)
664 if (port
>= RTL8366RB_NUM_PORTS
|| index
>= RTL8366RB_NUM_VLANS
)
667 return rtl8366_smi_rmwr(smi
, RTL8366RB_PORT_VLAN_CTRL_REG(port
),
668 RTL8366RB_PORT_VLAN_CTRL_MASK
<<
669 RTL8366RB_PORT_VLAN_CTRL_SHIFT(port
),
670 (index
& RTL8366RB_PORT_VLAN_CTRL_MASK
) <<
671 RTL8366RB_PORT_VLAN_CTRL_SHIFT(port
));
674 static int rtl8366rb_vlan_set_vlan(struct rtl8366_smi
*smi
, int enable
)
676 return rtl8366_smi_rmwr(smi
, RTL8366RB_CHIP_GLOBAL_CTRL_REG
,
677 RTL8366RB_CHIP_CTRL_VLAN
,
678 (enable
) ? RTL8366RB_CHIP_CTRL_VLAN
: 0);
681 static int rtl8366rb_vlan_set_4ktable(struct rtl8366_smi
*smi
, int enable
)
683 return rtl8366_smi_rmwr(smi
, RTL8366RB_CHIP_GLOBAL_CTRL_REG
,
684 RTL8366RB_CHIP_CTRL_VLAN_4KTB
,
685 (enable
) ? RTL8366RB_CHIP_CTRL_VLAN_4KTB
: 0);
688 #ifdef CONFIG_RTL8366S_PHY_DEBUG_FS
689 static int rtl8366rb_debugfs_open(struct inode
*inode
, struct file
*file
)
691 file
->private_data
= inode
->i_private
;
695 static ssize_t
rtl8366rb_read_debugfs_mibs(struct file
*file
,
696 char __user
*user_buf
,
697 size_t count
, loff_t
*ppos
)
699 struct rtl8366rb
*rtl
= (struct rtl8366rb
*)file
->private_data
;
700 struct rtl8366_smi
*smi
= &rtl
->smi
;
702 char *buf
= rtl
->buf
;
704 len
+= snprintf(buf
+ len
, sizeof(rtl
->buf
) - len
,
705 "%-36s %12s %12s %12s %12s %12s %12s\n",
707 "Port 0", "Port 1", "Port 2",
708 "Port 3", "Port 4", "Port 5");
710 for (i
= 0; i
< ARRAY_SIZE(rtl8366rb_mib_counters
); ++i
) {
711 len
+= snprintf(buf
+ len
, sizeof(rtl
->buf
) - len
, "%-36s ",
712 rtl8366rb_mib_counters
[i
].name
);
713 for (j
= 0; j
< RTL8366RB_NUM_PORTS
; ++j
) {
714 unsigned long long counter
= 0;
716 if (!rtl8366_get_mib_counter(smi
, i
, j
, &counter
))
717 len
+= snprintf(buf
+ len
,
718 sizeof(rtl
->buf
) - len
,
721 len
+= snprintf(buf
+ len
,
722 sizeof(rtl
->buf
) - len
,
725 len
+= snprintf(buf
+ len
, sizeof(rtl
->buf
) - len
, "\n");
728 return simple_read_from_buffer(user_buf
, count
, ppos
, buf
, len
);
731 static ssize_t
rtl8366rb_read_debugfs_vlan_mc(struct file
*file
,
732 char __user
*user_buf
,
733 size_t count
, loff_t
*ppos
)
735 struct rtl8366rb
*rtl
= (struct rtl8366rb
*)file
->private_data
;
736 struct rtl8366_smi
*smi
= &rtl
->smi
;
738 char *buf
= rtl
->buf
;
740 len
+= snprintf(buf
+ len
, sizeof(rtl
->buf
) - len
,
741 "%2s %6s %4s %6s %6s %3s\n",
742 "id", "vid","prio", "member", "untag", "fid");
744 for (i
= 0; i
< RTL8366RB_NUM_VLANS
; ++i
) {
745 struct rtl8366_vlan_mc vlanmc
;
747 rtl8366rb_get_vlan_mc(smi
, i
, &vlanmc
);
749 len
+= snprintf(buf
+ len
, sizeof(rtl
->buf
) - len
,
750 "%2d %6d %4d 0x%04x 0x%04x %3d\n",
751 i
, vlanmc
.vid
, vlanmc
.priority
,
752 vlanmc
.member
, vlanmc
.untag
, vlanmc
.fid
);
755 return simple_read_from_buffer(user_buf
, count
, ppos
, buf
, len
);
758 static ssize_t
rtl8366rb_read_debugfs_reg(struct file
*file
,
759 char __user
*user_buf
,
760 size_t count
, loff_t
*ppos
)
762 struct rtl8366rb
*rtl
= (struct rtl8366rb
*)file
->private_data
;
763 struct rtl8366_smi
*smi
= &rtl
->smi
;
764 u32 t
, reg
= gl_dbg_reg
;
766 char *buf
= rtl
->buf
;
768 memset(buf
, '\0', sizeof(rtl
->buf
));
770 err
= rtl8366_smi_read_reg(smi
, reg
, &t
);
772 len
+= snprintf(buf
, sizeof(rtl
->buf
),
773 "Read failed (reg: 0x%04x)\n", reg
);
774 return simple_read_from_buffer(user_buf
, count
, ppos
, buf
, len
);
777 len
+= snprintf(buf
, sizeof(rtl
->buf
), "reg = 0x%04x, val = 0x%04x\n",
780 return simple_read_from_buffer(user_buf
, count
, ppos
, buf
, len
);
783 static ssize_t
rtl8366rb_write_debugfs_reg(struct file
*file
,
784 const char __user
*user_buf
,
785 size_t count
, loff_t
*ppos
)
787 struct rtl8366rb
*rtl
= (struct rtl8366rb
*)file
->private_data
;
788 struct rtl8366_smi
*smi
= &rtl
->smi
;
790 u32 reg
= gl_dbg_reg
;
793 char *buf
= rtl
->buf
;
795 len
= min(count
, sizeof(rtl
->buf
) - 1);
796 if (copy_from_user(buf
, user_buf
, len
)) {
797 dev_err(rtl
->parent
, "copy from user failed\n");
802 if (len
> 0 && buf
[len
- 1] == '\n')
806 if (strict_strtoul(buf
, 16, &data
)) {
807 dev_err(rtl
->parent
, "Invalid reg value %s\n", buf
);
809 err
= rtl8366_smi_write_reg(smi
, reg
, data
);
812 "writing reg 0x%04x val 0x%04lx failed\n",
820 static const struct file_operations fops_rtl8366rb_regs
= {
821 .read
= rtl8366rb_read_debugfs_reg
,
822 .write
= rtl8366rb_write_debugfs_reg
,
823 .open
= rtl8366rb_debugfs_open
,
827 static const struct file_operations fops_rtl8366rb_vlan_mc
= {
828 .read
= rtl8366rb_read_debugfs_vlan_mc
,
829 .open
= rtl8366rb_debugfs_open
,
833 static const struct file_operations fops_rtl8366rb_mibs
= {
834 .read
= rtl8366rb_read_debugfs_mibs
,
835 .open
= rtl8366rb_debugfs_open
,
839 static void rtl8366rb_debugfs_init(struct rtl8366rb
*rtl
)
844 if (!rtl
->debugfs_root
)
845 rtl
->debugfs_root
= debugfs_create_dir("rtl8366rb", NULL
);
847 if (!rtl
->debugfs_root
) {
848 dev_err(rtl
->parent
, "Unable to create debugfs dir\n");
851 root
= rtl
->debugfs_root
;
853 node
= debugfs_create_x16("reg", S_IRUGO
| S_IWUSR
, root
, &gl_dbg_reg
);
855 dev_err(rtl
->parent
, "Creating debugfs file '%s' failed\n",
860 node
= debugfs_create_file("val", S_IRUGO
| S_IWUSR
, root
, rtl
,
861 &fops_rtl8366rb_regs
);
863 dev_err(rtl
->parent
, "Creating debugfs file '%s' failed\n",
868 node
= debugfs_create_file("vlan_mc", S_IRUSR
, root
, rtl
,
869 &fops_rtl8366rb_vlan_mc
);
871 dev_err(rtl
->parent
, "Creating debugfs file '%s' failed\n",
876 node
= debugfs_create_file("mibs", S_IRUSR
, root
, rtl
,
877 &fops_rtl8366rb_mibs
);
879 dev_err(rtl
->parent
, "Creating debugfs file '%s' failed\n",
885 static void rtl8366rb_debugfs_remove(struct rtl8366rb
*rtl
)
887 if (rtl
->debugfs_root
) {
888 debugfs_remove_recursive(rtl
->debugfs_root
);
889 rtl
->debugfs_root
= NULL
;
894 static inline void rtl8366rb_debugfs_init(struct rtl8366rb
*rtl
) {}
895 static inline void rtl8366rb_debugfs_remove(struct rtl8366rb
*rtl
) {}
896 #endif /* CONFIG_RTL8366S_PHY_DEBUG_FS */
898 static int rtl8366rb_sw_reset_mibs(struct switch_dev
*dev
,
899 const struct switch_attr
*attr
,
900 struct switch_val
*val
)
902 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
905 if (val
->value
.i
== 1)
906 err
= rtl8366_smi_rmwr(smi
, RTL8366RB_MIB_CTRL_REG
, 0,
907 RTL8366RB_MIB_CTRL_GLOBAL_RESET
);
912 static int rtl8366rb_sw_get_vlan_enable(struct switch_dev
*dev
,
913 const struct switch_attr
*attr
,
914 struct switch_val
*val
)
916 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
919 if (attr
->ofs
== 1) {
920 rtl8366_smi_read_reg(smi
, RTL8366RB_CHIP_GLOBAL_CTRL_REG
, &data
);
922 if (data
& RTL8366RB_CHIP_CTRL_VLAN
)
926 } else if (attr
->ofs
== 2) {
927 rtl8366_smi_read_reg(smi
, RTL8366RB_CHIP_GLOBAL_CTRL_REG
, &data
);
929 if (data
& RTL8366RB_CHIP_CTRL_VLAN_4KTB
)
938 static int rtl8366rb_sw_get_blinkrate(struct switch_dev
*dev
,
939 const struct switch_attr
*attr
,
940 struct switch_val
*val
)
942 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
945 rtl8366_smi_read_reg(smi
, RTL8366RB_LED_BLINKRATE_REG
, &data
);
947 val
->value
.i
= (data
& (RTL8366RB_LED_BLINKRATE_MASK
));
952 static int rtl8366rb_sw_set_blinkrate(struct switch_dev
*dev
,
953 const struct switch_attr
*attr
,
954 struct switch_val
*val
)
956 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
958 if (val
->value
.i
>= 6)
961 return rtl8366_smi_rmwr(smi
, RTL8366RB_LED_BLINKRATE_REG
,
962 RTL8366RB_LED_BLINKRATE_MASK
,
966 static int rtl8366rb_sw_set_vlan_enable(struct switch_dev
*dev
,
967 const struct switch_attr
*attr
,
968 struct switch_val
*val
)
970 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
973 return rtl8366rb_vlan_set_vlan(smi
, val
->value
.i
);
975 return rtl8366rb_vlan_set_4ktable(smi
, val
->value
.i
);
978 static const char *rtl8366rb_speed_str(unsigned speed
)
992 static int rtl8366rb_sw_get_port_link(struct switch_dev
*dev
,
993 const struct switch_attr
*attr
,
994 struct switch_val
*val
)
996 struct rtl8366rb
*rtl
= sw_to_rtl8366rb(dev
);
997 struct rtl8366_smi
*smi
= &rtl
->smi
;
998 u32 len
= 0, data
= 0;
1000 if (val
->port_vlan
>= RTL8366RB_NUM_PORTS
)
1003 memset(rtl
->buf
, '\0', sizeof(rtl
->buf
));
1004 rtl8366_smi_read_reg(smi
, RTL8366RB_PORT_LINK_STATUS_BASE
+
1005 (val
->port_vlan
/ 2), &data
);
1007 if (val
->port_vlan
% 2)
1010 if (data
& RTL8366RB_PORT_STATUS_LINK_MASK
) {
1011 len
= snprintf(rtl
->buf
, sizeof(rtl
->buf
),
1012 "port:%d link:up speed:%s %s-duplex %s%s%s",
1014 rtl8366rb_speed_str(data
&
1015 RTL8366RB_PORT_STATUS_SPEED_MASK
),
1016 (data
& RTL8366RB_PORT_STATUS_DUPLEX_MASK
) ?
1018 (data
& RTL8366RB_PORT_STATUS_TXPAUSE_MASK
) ?
1020 (data
& RTL8366RB_PORT_STATUS_RXPAUSE_MASK
) ?
1022 (data
& RTL8366RB_PORT_STATUS_AN_MASK
) ?
1025 len
= snprintf(rtl
->buf
, sizeof(rtl
->buf
), "port:%d link: down",
1029 val
->value
.s
= rtl
->buf
;
1035 static int rtl8366rb_sw_get_vlan_info(struct switch_dev
*dev
,
1036 const struct switch_attr
*attr
,
1037 struct switch_val
*val
)
1041 struct rtl8366_vlan_4k vlan4k
;
1042 struct rtl8366rb
*rtl
= sw_to_rtl8366rb(dev
);
1043 struct rtl8366_smi
*smi
= &rtl
->smi
;
1044 char *buf
= rtl
->buf
;
1047 if (val
->port_vlan
== 0 || val
->port_vlan
>= RTL8366RB_NUM_VLANS
)
1050 memset(buf
, '\0', sizeof(rtl
->buf
));
1052 err
= rtl8366rb_get_vlan_4k(smi
, val
->port_vlan
, &vlan4k
);
1056 len
+= snprintf(buf
+ len
, sizeof(rtl
->buf
) - len
,
1057 "VLAN %d: Ports: '", vlan4k
.vid
);
1059 for (i
= 0; i
< RTL8366RB_NUM_PORTS
; i
++) {
1060 if (!(vlan4k
.member
& (1 << i
)))
1063 len
+= snprintf(buf
+ len
, sizeof(rtl
->buf
) - len
, "%d%s", i
,
1064 (vlan4k
.untag
& (1 << i
)) ? "" : "t");
1067 len
+= snprintf(buf
+ len
, sizeof(rtl
->buf
) - len
,
1068 "', members=%04x, untag=%04x, fid=%u",
1069 vlan4k
.member
, vlan4k
.untag
, vlan4k
.fid
);
1077 static int rtl8366rb_sw_set_port_led(struct switch_dev
*dev
,
1078 const struct switch_attr
*attr
,
1079 struct switch_val
*val
)
1081 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
1086 if (val
->port_vlan
>= RTL8366RB_NUM_PORTS
)
1089 if (val
->port_vlan
== RTL8366RB_PORT_NUM_CPU
) {
1090 reg
= RTL8366RB_LED_BLINKRATE_REG
;
1092 data
= val
->value
.i
<< 4;
1094 reg
= RTL8366RB_LED_CTRL_REG
;
1095 mask
= 0xF << (val
->port_vlan
* 4),
1096 data
= val
->value
.i
<< (val
->port_vlan
* 4);
1099 return rtl8366_smi_rmwr(smi
, RTL8366RB_LED_BLINKRATE_REG
, mask
, data
);
1102 static int rtl8366rb_sw_get_port_led(struct switch_dev
*dev
,
1103 const struct switch_attr
*attr
,
1104 struct switch_val
*val
)
1106 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
1109 if (val
->port_vlan
>= RTL8366RB_NUM_LEDGROUPS
)
1112 rtl8366_smi_read_reg(smi
, RTL8366RB_LED_CTRL_REG
, &data
);
1113 val
->value
.i
= (data
>> (val
->port_vlan
* 4)) & 0x000F;
1118 static int rtl8366rb_sw_reset_port_mibs(struct switch_dev
*dev
,
1119 const struct switch_attr
*attr
,
1120 struct switch_val
*val
)
1122 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
1124 if (val
->port_vlan
>= RTL8366RB_NUM_PORTS
)
1127 return rtl8366_smi_rmwr(smi
, RTL8366RB_MIB_CTRL_REG
, 0,
1128 RTL8366RB_MIB_CTRL_PORT_RESET(val
->port_vlan
));
1131 static int rtl8366rb_sw_get_port_mib(struct switch_dev
*dev
,
1132 const struct switch_attr
*attr
,
1133 struct switch_val
*val
)
1135 struct rtl8366rb
*rtl
= sw_to_rtl8366rb(dev
);
1136 struct rtl8366_smi
*smi
= &rtl
->smi
;
1138 unsigned long long counter
= 0;
1139 char *buf
= rtl
->buf
;
1141 if (val
->port_vlan
>= RTL8366RB_NUM_PORTS
)
1144 len
+= snprintf(buf
+ len
, sizeof(rtl
->buf
) - len
,
1145 "Port %d MIB counters\n",
1148 for (i
= 0; i
< ARRAY_SIZE(rtl8366rb_mib_counters
); ++i
) {
1149 len
+= snprintf(buf
+ len
, sizeof(rtl
->buf
) - len
,
1150 "%-36s: ", rtl8366rb_mib_counters
[i
].name
);
1151 if (!rtl8366_get_mib_counter(smi
, i
, val
->port_vlan
, &counter
))
1152 len
+= snprintf(buf
+ len
, sizeof(rtl
->buf
) - len
,
1155 len
+= snprintf(buf
+ len
, sizeof(rtl
->buf
) - len
,
1164 static int rtl8366rb_sw_get_vlan_ports(struct switch_dev
*dev
,
1165 struct switch_val
*val
)
1167 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
1168 struct switch_port
*port
;
1169 struct rtl8366_vlan_4k vlan4k
;
1172 if (val
->port_vlan
== 0 || val
->port_vlan
>= RTL8366RB_NUM_VLANS
)
1175 rtl8366rb_get_vlan_4k(smi
, val
->port_vlan
, &vlan4k
);
1177 port
= &val
->value
.ports
[0];
1179 for (i
= 0; i
< RTL8366RB_NUM_PORTS
; i
++) {
1180 if (!(vlan4k
.member
& BIT(i
)))
1184 port
->flags
= (vlan4k
.untag
& BIT(i
)) ?
1185 0 : BIT(SWITCH_PORT_FLAG_TAGGED
);
1192 static int rtl8366rb_sw_set_vlan_ports(struct switch_dev
*dev
,
1193 struct switch_val
*val
)
1195 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
1196 struct switch_port
*port
;
1201 if (val
->port_vlan
== 0 || val
->port_vlan
>= RTL8366RB_NUM_VLANS
)
1204 port
= &val
->value
.ports
[0];
1205 for (i
= 0; i
< val
->len
; i
++, port
++) {
1206 member
|= BIT(port
->id
);
1208 if (!(port
->flags
& BIT(SWITCH_PORT_FLAG_TAGGED
)))
1209 untag
|= BIT(port
->id
);
1212 return rtl8366_set_vlan(smi
, val
->port_vlan
, member
, untag
, 0);
1215 static int rtl8366rb_sw_get_port_pvid(struct switch_dev
*dev
, int port
, int *val
)
1217 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
1218 return rtl8366_get_pvid(smi
, port
, val
);
1221 static int rtl8366rb_sw_set_port_pvid(struct switch_dev
*dev
, int port
, int val
)
1223 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
1224 return rtl8366_set_pvid(smi
, port
, val
);
1227 static int rtl8366rb_sw_reset_switch(struct switch_dev
*dev
)
1229 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
1232 err
= rtl8366rb_reset_chip(smi
);
1236 err
= rtl8366rb_hw_init(smi
);
1240 return rtl8366_reset_vlan(smi
);
1243 static struct switch_attr rtl8366rb_globals
[] = {
1245 .type
= SWITCH_TYPE_INT
,
1246 .name
= "enable_vlan",
1247 .description
= "Enable VLAN mode",
1248 .set
= rtl8366rb_sw_set_vlan_enable
,
1249 .get
= rtl8366rb_sw_get_vlan_enable
,
1253 .type
= SWITCH_TYPE_INT
,
1254 .name
= "enable_vlan4k",
1255 .description
= "Enable VLAN 4K mode",
1256 .set
= rtl8366rb_sw_set_vlan_enable
,
1257 .get
= rtl8366rb_sw_get_vlan_enable
,
1261 .type
= SWITCH_TYPE_INT
,
1262 .name
= "reset_mibs",
1263 .description
= "Reset all MIB counters",
1264 .set
= rtl8366rb_sw_reset_mibs
,
1268 .type
= SWITCH_TYPE_INT
,
1269 .name
= "blinkrate",
1270 .description
= "Get/Set LED blinking rate (0 = 43ms, 1 = 84ms,"
1271 " 2 = 120ms, 3 = 170ms, 4 = 340ms, 5 = 670ms)",
1272 .set
= rtl8366rb_sw_set_blinkrate
,
1273 .get
= rtl8366rb_sw_get_blinkrate
,
1278 static struct switch_attr rtl8366rb_port
[] = {
1280 .type
= SWITCH_TYPE_STRING
,
1282 .description
= "Get port link information",
1285 .get
= rtl8366rb_sw_get_port_link
,
1287 .type
= SWITCH_TYPE_INT
,
1288 .name
= "reset_mib",
1289 .description
= "Reset single port MIB counters",
1291 .set
= rtl8366rb_sw_reset_port_mibs
,
1294 .type
= SWITCH_TYPE_STRING
,
1296 .description
= "Get MIB counters for port",
1299 .get
= rtl8366rb_sw_get_port_mib
,
1301 .type
= SWITCH_TYPE_INT
,
1303 .description
= "Get/Set port group (0 - 3) led mode (0 - 15)",
1305 .set
= rtl8366rb_sw_set_port_led
,
1306 .get
= rtl8366rb_sw_get_port_led
,
1310 static struct switch_attr rtl8366rb_vlan
[] = {
1312 .type
= SWITCH_TYPE_STRING
,
1314 .description
= "Get vlan information",
1317 .get
= rtl8366rb_sw_get_vlan_info
,
1322 static struct switch_dev rtl8366_switch_dev
= {
1324 .cpu_port
= RTL8366RB_PORT_NUM_CPU
,
1325 .ports
= RTL8366RB_NUM_PORTS
,
1326 .vlans
= RTL8366RB_NUM_VLANS
,
1328 .attr
= rtl8366rb_globals
,
1329 .n_attr
= ARRAY_SIZE(rtl8366rb_globals
),
1332 .attr
= rtl8366rb_port
,
1333 .n_attr
= ARRAY_SIZE(rtl8366rb_port
),
1336 .attr
= rtl8366rb_vlan
,
1337 .n_attr
= ARRAY_SIZE(rtl8366rb_vlan
),
1340 .get_vlan_ports
= rtl8366rb_sw_get_vlan_ports
,
1341 .set_vlan_ports
= rtl8366rb_sw_set_vlan_ports
,
1342 .get_port_pvid
= rtl8366rb_sw_get_port_pvid
,
1343 .set_port_pvid
= rtl8366rb_sw_set_port_pvid
,
1344 .reset_switch
= rtl8366rb_sw_reset_switch
,
1347 static int rtl8366rb_switch_init(struct rtl8366rb
*rtl
)
1349 struct switch_dev
*dev
= &rtl
->dev
;
1352 memcpy(dev
, &rtl8366_switch_dev
, sizeof(struct switch_dev
));
1354 dev
->devname
= dev_name(rtl
->parent
);
1356 err
= register_switch(dev
, NULL
);
1358 dev_err(rtl
->parent
, "switch registration failed\n");
1363 static void rtl8366rb_switch_cleanup(struct rtl8366rb
*rtl
)
1365 unregister_switch(&rtl
->dev
);
1368 static int rtl8366rb_mii_read(struct mii_bus
*bus
, int addr
, int reg
)
1370 struct rtl8366_smi
*smi
= bus
->priv
;
1374 err
= rtl8366rb_read_phy_reg(smi
, addr
, 0, reg
, &val
);
1381 static int rtl8366rb_mii_write(struct mii_bus
*bus
, int addr
, int reg
, u16 val
)
1383 struct rtl8366_smi
*smi
= bus
->priv
;
1387 err
= rtl8366rb_write_phy_reg(smi
, addr
, 0, reg
, val
);
1389 (void) rtl8366rb_read_phy_reg(smi
, addr
, 0, reg
, &t
);
1394 static int rtl8366rb_mii_bus_match(struct mii_bus
*bus
)
1396 return (bus
->read
== rtl8366rb_mii_read
&&
1397 bus
->write
== rtl8366rb_mii_write
);
1400 static int rtl8366rb_setup(struct rtl8366rb
*rtl
)
1402 struct rtl8366_smi
*smi
= &rtl
->smi
;
1405 rtl8366rb_debugfs_init(rtl
);
1407 ret
= rtl8366rb_reset_chip(smi
);
1411 ret
= rtl8366rb_hw_init(smi
);
1415 static int rtl8366rb_detect(struct rtl8366_smi
*smi
)
1421 ret
= rtl8366_smi_read_reg(smi
, RTL8366RB_CHIP_ID_REG
, &chip_id
);
1423 dev_err(smi
->parent
, "unable to read chip id\n");
1428 case RTL8366RB_CHIP_ID_8366
:
1431 dev_err(smi
->parent
, "unknown chip id (%04x)\n", chip_id
);
1435 ret
= rtl8366_smi_read_reg(smi
, RTL8366RB_CHIP_VERSION_CTRL_REG
,
1438 dev_err(smi
->parent
, "unable to read chip version\n");
1442 dev_info(smi
->parent
, "RTL%04x ver. %u chip found\n",
1443 chip_id
, chip_ver
& RTL8366RB_CHIP_VERSION_MASK
);
1448 static struct rtl8366_smi_ops rtl8366rb_smi_ops
= {
1449 .detect
= rtl8366rb_detect
,
1450 .mii_read
= rtl8366rb_mii_read
,
1451 .mii_write
= rtl8366rb_mii_write
,
1453 .get_vlan_mc
= rtl8366rb_get_vlan_mc
,
1454 .set_vlan_mc
= rtl8366rb_set_vlan_mc
,
1455 .get_vlan_4k
= rtl8366rb_get_vlan_4k
,
1456 .set_vlan_4k
= rtl8366rb_set_vlan_4k
,
1457 .get_mc_index
= rtl8366rb_get_mc_index
,
1458 .set_mc_index
= rtl8366rb_set_mc_index
,
1461 static int __init
rtl8366rb_probe(struct platform_device
*pdev
)
1463 static int rtl8366_smi_version_printed
;
1464 struct rtl8366rb_platform_data
*pdata
;
1465 struct rtl8366rb
*rtl
;
1466 struct rtl8366_smi
*smi
;
1469 if (!rtl8366_smi_version_printed
++)
1470 printk(KERN_NOTICE RTL8366RB_DRIVER_DESC
1471 " version " RTL8366RB_DRIVER_VER
"\n");
1473 pdata
= pdev
->dev
.platform_data
;
1475 dev_err(&pdev
->dev
, "no platform data specified\n");
1480 rtl
= kzalloc(sizeof(*rtl
), GFP_KERNEL
);
1482 dev_err(&pdev
->dev
, "no memory for private data\n");
1487 rtl
->parent
= &pdev
->dev
;
1490 smi
->parent
= &pdev
->dev
;
1491 smi
->gpio_sda
= pdata
->gpio_sda
;
1492 smi
->gpio_sck
= pdata
->gpio_sck
;
1493 smi
->ops
= &rtl8366rb_smi_ops
;
1494 smi
->cpu_port
= RTL8366RB_PORT_NUM_CPU
;
1495 smi
->num_ports
= RTL8366RB_NUM_PORTS
;
1496 smi
->num_vlan_mc
= RTL8366RB_NUM_VLANS
;
1498 err
= rtl8366_smi_init(smi
);
1502 platform_set_drvdata(pdev
, rtl
);
1504 err
= rtl8366rb_setup(rtl
);
1506 goto err_clear_drvdata
;
1508 err
= rtl8366rb_switch_init(rtl
);
1510 goto err_clear_drvdata
;
1515 platform_set_drvdata(pdev
, NULL
);
1516 rtl8366_smi_cleanup(smi
);
1523 static int rtl8366rb_phy_config_init(struct phy_device
*phydev
)
1525 if (!rtl8366rb_mii_bus_match(phydev
->bus
))
1531 static int rtl8366rb_phy_config_aneg(struct phy_device
*phydev
)
1536 static struct phy_driver rtl8366rb_phy_driver
= {
1537 .phy_id
= 0x001cc960,
1538 .name
= "Realtek RTL8366RB",
1539 .phy_id_mask
= 0x1ffffff0,
1540 .features
= PHY_GBIT_FEATURES
,
1541 .config_aneg
= rtl8366rb_phy_config_aneg
,
1542 .config_init
= rtl8366rb_phy_config_init
,
1543 .read_status
= genphy_read_status
,
1545 .owner
= THIS_MODULE
,
1549 static int __devexit
rtl8366rb_remove(struct platform_device
*pdev
)
1551 struct rtl8366rb
*rtl
= platform_get_drvdata(pdev
);
1554 rtl8366rb_switch_cleanup(rtl
);
1555 rtl8366rb_debugfs_remove(rtl
);
1556 platform_set_drvdata(pdev
, NULL
);
1557 rtl8366_smi_cleanup(&rtl
->smi
);
1564 static struct platform_driver rtl8366rb_driver
= {
1566 .name
= RTL8366RB_DRIVER_NAME
,
1567 .owner
= THIS_MODULE
,
1569 .probe
= rtl8366rb_probe
,
1570 .remove
= __devexit_p(rtl8366rb_remove
),
1573 static int __init
rtl8366rb_module_init(void)
1576 ret
= platform_driver_register(&rtl8366rb_driver
);
1580 ret
= phy_driver_register(&rtl8366rb_phy_driver
);
1582 goto err_platform_unregister
;
1586 err_platform_unregister
:
1587 platform_driver_unregister(&rtl8366rb_driver
);
1590 module_init(rtl8366rb_module_init
);
1592 static void __exit
rtl8366rb_module_exit(void)
1594 phy_driver_unregister(&rtl8366rb_phy_driver
);
1595 platform_driver_unregister(&rtl8366rb_driver
);
1597 module_exit(rtl8366rb_module_exit
);
1599 MODULE_DESCRIPTION(RTL8366RB_DRIVER_DESC
);
1600 MODULE_VERSION(RTL8366RB_DRIVER_VER
);
1601 MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
1602 MODULE_AUTHOR("Antti Seppälä <a.seppala@gmail.com>");
1603 MODULE_LICENSE("GPL v2");
1604 MODULE_ALIAS("platform:" RTL8366RB_DRIVER_NAME
);