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/phy.h>
20 #include <linux/rtl8366s.h>
22 #include "rtl8366_smi.h"
24 #ifdef CONFIG_RTL8366S_PHY_DEBUG_FS
25 #include <linux/debugfs.h>
28 #define RTL8366S_DRIVER_DESC "Realtek RTL8366S ethernet switch driver"
29 #define RTL8366S_DRIVER_VER "0.2.0"
31 #define RTL8366S_PHY_NO_MAX 4
32 #define RTL8366S_PHY_PAGE_MAX 7
33 #define RTL8366S_PHY_ADDR_MAX 31
35 #define RTL8366_CHIP_GLOBAL_CTRL_REG 0x0000
36 #define RTL8366_CHIP_CTRL_VLAN (1 << 13)
38 #define RTL8366_RESET_CTRL_REG 0x0100
39 #define RTL8366_CHIP_CTRL_RESET_HW 1
40 #define RTL8366_CHIP_CTRL_RESET_SW (1 << 1)
42 #define RTL8366S_CHIP_VERSION_CTRL_REG 0x0104
43 #define RTL8366S_CHIP_VERSION_MASK 0xf
44 #define RTL8366S_CHIP_ID_REG 0x0105
45 #define RTL8366S_CHIP_ID_8366 0x8366
47 /* PHY registers control */
48 #define RTL8366S_PHY_ACCESS_CTRL_REG 0x8028
49 #define RTL8366S_PHY_ACCESS_DATA_REG 0x8029
51 #define RTL8366S_PHY_CTRL_READ 1
52 #define RTL8366S_PHY_CTRL_WRITE 0
54 #define RTL8366S_PHY_REG_MASK 0x1f
55 #define RTL8366S_PHY_PAGE_OFFSET 5
56 #define RTL8366S_PHY_PAGE_MASK (0x7 << 5)
57 #define RTL8366S_PHY_NO_OFFSET 9
58 #define RTL8366S_PHY_NO_MASK (0x1f << 9)
60 /* LED control registers */
61 #define RTL8366_LED_BLINKRATE_REG 0x0420
62 #define RTL8366_LED_BLINKRATE_BIT 0
63 #define RTL8366_LED_BLINKRATE_MASK 0x0007
65 #define RTL8366_LED_CTRL_REG 0x0421
66 #define RTL8366_LED_0_1_CTRL_REG 0x0422
67 #define RTL8366_LED_2_3_CTRL_REG 0x0423
69 #define RTL8366S_MIB_COUNT 33
70 #define RTL8366S_GLOBAL_MIB_COUNT 1
71 #define RTL8366S_MIB_COUNTER_PORT_OFFSET 0x0040
72 #define RTL8366S_MIB_COUNTER_BASE 0x1000
73 #define RTL8366S_MIB_CTRL_REG 0x11F0
74 #define RTL8366S_MIB_CTRL_USER_MASK 0x01FF
75 #define RTL8366S_MIB_CTRL_BUSY_MASK 0x0001
76 #define RTL8366S_MIB_CTRL_RESET_MASK 0x0002
78 #define RTL8366S_MIB_CTRL_GLOBAL_RESET_MASK 0x0004
79 #define RTL8366S_MIB_CTRL_PORT_RESET_BIT 0x0003
80 #define RTL8366S_MIB_CTRL_PORT_RESET_MASK 0x01FC
83 #define RTL8366S_PORT_VLAN_CTRL_BASE 0x0058
84 #define RTL8366S_PORT_VLAN_CTRL_REG(_p) \
85 (RTL8366S_PORT_VLAN_CTRL_BASE + (_p) / 4)
86 #define RTL8366S_PORT_VLAN_CTRL_MASK 0xf
87 #define RTL8366S_PORT_VLAN_CTRL_SHIFT(_p) (4 * ((_p) % 4))
90 #define RTL8366S_VLAN_TABLE_READ_BASE 0x018B
91 #define RTL8366S_VLAN_TABLE_WRITE_BASE 0x0185
93 #define RTL8366S_VLAN_TB_CTRL_REG 0x010F
95 #define RTL8366S_TABLE_ACCESS_CTRL_REG 0x0180
96 #define RTL8366S_TABLE_VLAN_READ_CTRL 0x0E01
97 #define RTL8366S_TABLE_VLAN_WRITE_CTRL 0x0F01
99 #define RTL8366S_VLAN_MEMCONF_BASE 0x0016
102 #define RTL8366S_PORT_LINK_STATUS_BASE 0x0060
103 #define RTL8366S_PORT_STATUS_SPEED_MASK 0x0003
104 #define RTL8366S_PORT_STATUS_DUPLEX_MASK 0x0004
105 #define RTL8366S_PORT_STATUS_LINK_MASK 0x0010
106 #define RTL8366S_PORT_STATUS_TXPAUSE_MASK 0x0020
107 #define RTL8366S_PORT_STATUS_RXPAUSE_MASK 0x0040
108 #define RTL8366S_PORT_STATUS_AN_MASK 0x0080
111 #define RTL8366_PORT_NUM_CPU 5
112 #define RTL8366_NUM_PORTS 6
113 #define RTL8366_NUM_VLANS 16
114 #define RTL8366_NUM_LEDGROUPS 4
115 #define RTL8366_NUM_VIDS 4096
116 #define RTL8366S_PRIORITYMAX 7
117 #define RTL8366S_FIDMAX 7
120 #define RTL8366_PORT_1 (1 << 0) /* In userspace port 0 */
121 #define RTL8366_PORT_2 (1 << 1) /* In userspace port 1 */
122 #define RTL8366_PORT_3 (1 << 2) /* In userspace port 2 */
123 #define RTL8366_PORT_4 (1 << 3) /* In userspace port 3 */
125 #define RTL8366_PORT_UNKNOWN (1 << 4) /* No known connection */
126 #define RTL8366_PORT_CPU (1 << 5) /* CPU port */
128 #define RTL8366_PORT_ALL (RTL8366_PORT_1 | \
132 RTL8366_PORT_UNKNOWN | \
135 #define RTL8366_PORT_ALL_BUT_CPU (RTL8366_PORT_1 | \
139 RTL8366_PORT_UNKNOWN)
141 #define RTL8366_PORT_ALL_EXTERNAL (RTL8366_PORT_1 | \
146 #define RTL8366_PORT_ALL_INTERNAL (RTL8366_PORT_UNKNOWN | \
150 struct device
*parent
;
151 struct rtl8366_smi smi
;
152 struct mii_bus
*mii_bus
;
153 int mii_irq
[PHY_MAX_ADDR
];
154 struct switch_dev dev
;
156 #ifdef CONFIG_RTL8366S_PHY_DEBUG_FS
157 struct dentry
*debugfs_root
;
161 struct rtl8366s_vlanconfig
{
172 struct rtl8366s_vlan4kentry
{
182 #ifdef CONFIG_RTL8366S_PHY_DEBUG_FS
192 static struct mib_counter rtl8366s_mib_counters
[RTL8366S_MIB_COUNT
] = {
193 { 0, 4, "IfInOctets " },
194 { 4, 4, "EtherStatsOctets " },
195 { 8, 2, "EtherStatsUnderSizePkts " },
196 { 10, 2, "EtherFregament " },
197 { 12, 2, "EtherStatsPkts64Octets " },
198 { 14, 2, "EtherStatsPkts65to127Octets " },
199 { 16, 2, "EtherStatsPkts128to255Octets " },
200 { 18, 2, "EtherStatsPkts256to511Octets " },
201 { 20, 2, "EtherStatsPkts512to1023Octets " },
202 { 22, 2, "EtherStatsPkts1024to1518Octets " },
203 { 24, 2, "EtherOversizeStats " },
204 { 26, 2, "EtherStatsJabbers " },
205 { 28, 2, "IfInUcastPkts " },
206 { 30, 2, "EtherStatsMulticastPkts " },
207 { 32, 2, "EtherStatsBroadcastPkts " },
208 { 34, 2, "EtherStatsDropEvents " },
209 { 36, 2, "Dot3StatsFCSErrors " },
210 { 38, 2, "Dot3StatsSymbolErrors " },
211 { 40, 2, "Dot3InPauseFrames " },
212 { 42, 2, "Dot3ControlInUnknownOpcodes " },
213 { 44, 2, "IfOutOctets " },
214 { 46, 2, "Dot3StatsSingleCollisionFrames " },
215 { 48, 2, "Dot3StatMultipleCollisionFrames " },
216 { 50, 2, "Dot3sDeferredTransmissions " },
217 { 52, 2, "Dot3StatsLateCollisions " },
218 { 54, 2, "EtherStatsCollisions " },
219 { 56, 2, "Dot3StatsExcessiveCollisions " },
220 { 58, 2, "Dot3OutPauseFrames " },
221 { 60, 2, "Dot1dBasePortDelayExceededDiscards" },
222 { 62, 2, "Dot1dTpPortInDiscards " },
223 { 64, 2, "IfOutUcastPkts " },
224 { 66, 2, "IfOutMulticastPkts " },
225 { 68, 2, "IfOutBroadcastPkts " },
228 static inline struct rtl8366s
*sw_to_rtl8366s(struct switch_dev
*sw
)
230 return container_of(sw
, struct rtl8366s
, dev
);
233 static int rtl8366s_read_phy_reg(struct rtl8366s
*rtl
,
234 u32 phy_no
, u32 page
, u32 addr
, u32
*data
)
236 struct rtl8366_smi
*smi
= &rtl
->smi
;
240 if (phy_no
> RTL8366S_PHY_NO_MAX
)
243 if (page
> RTL8366S_PHY_PAGE_MAX
)
246 if (addr
> RTL8366S_PHY_ADDR_MAX
)
249 ret
= rtl8366_smi_write_reg(smi
, RTL8366S_PHY_ACCESS_CTRL_REG
,
250 RTL8366S_PHY_CTRL_READ
);
254 reg
= 0x8000 | (1 << (phy_no
+ RTL8366S_PHY_NO_OFFSET
)) |
255 ((page
<< RTL8366S_PHY_PAGE_OFFSET
) & RTL8366S_PHY_PAGE_MASK
) |
256 (addr
& RTL8366S_PHY_REG_MASK
);
258 ret
= rtl8366_smi_write_reg(smi
, reg
, 0);
262 ret
= rtl8366_smi_read_reg(smi
, RTL8366S_PHY_ACCESS_DATA_REG
, data
);
269 static int rtl8366s_write_phy_reg(struct rtl8366s
*rtl
,
270 u32 phy_no
, u32 page
, u32 addr
, u32 data
)
272 struct rtl8366_smi
*smi
= &rtl
->smi
;
276 if (phy_no
> RTL8366S_PHY_NO_MAX
)
279 if (page
> RTL8366S_PHY_PAGE_MAX
)
282 if (addr
> RTL8366S_PHY_ADDR_MAX
)
285 ret
= rtl8366_smi_write_reg(smi
, RTL8366S_PHY_ACCESS_CTRL_REG
,
286 RTL8366S_PHY_CTRL_WRITE
);
290 reg
= 0x8000 | (1 << (phy_no
+ RTL8366S_PHY_NO_OFFSET
)) |
291 ((page
<< RTL8366S_PHY_PAGE_OFFSET
) & RTL8366S_PHY_PAGE_MASK
) |
292 (addr
& RTL8366S_PHY_REG_MASK
);
294 ret
= rtl8366_smi_write_reg(smi
, reg
, data
);
301 static int rtl8366_get_mib_counter(struct rtl8366s
*rtl
, int counter
,
302 int port
, unsigned long long *val
)
304 struct rtl8366_smi
*smi
= &rtl
->smi
;
310 if (port
> RTL8366_NUM_PORTS
|| counter
>= RTL8366S_MIB_COUNT
)
313 addr
= RTL8366S_MIB_COUNTER_BASE
+
314 RTL8366S_MIB_COUNTER_PORT_OFFSET
* (port
) +
315 rtl8366s_mib_counters
[counter
].offset
;
318 * Writing access counter address first
319 * then ASIC will prepare 64bits counter wait for being retrived
321 data
= 0; /* writing data will be discard by ASIC */
322 err
= rtl8366_smi_write_reg(smi
, addr
, data
);
326 /* read MIB control register */
327 err
= rtl8366_smi_read_reg(smi
, RTL8366S_MIB_CTRL_REG
, &data
);
331 if (data
& RTL8366S_MIB_CTRL_BUSY_MASK
)
334 if (data
& RTL8366S_MIB_CTRL_RESET_MASK
)
338 for (i
= rtl8366s_mib_counters
[counter
].length
; i
> 0; i
--) {
339 err
= rtl8366_smi_read_reg(smi
, addr
+ (i
- 1), &data
);
343 mibvalue
= (mibvalue
<< 16) | (data
& 0xFFFF);
350 static int rtl8366s_get_vlan_4k_entry(struct rtl8366s
*rtl
, u32 vid
,
351 struct rtl8366s_vlan4kentry
*vlan4k
)
353 struct rtl8366_smi
*smi
= &rtl
->smi
;
358 memset(vlan4k
, '\0', sizeof(struct rtl8366s_vlan4kentry
));
361 if (vid
>= RTL8366_NUM_VIDS
)
364 tableaddr
= (u16
*)vlan4k
;
368 err
= rtl8366_smi_write_reg(smi
, RTL8366S_VLAN_TABLE_WRITE_BASE
, data
);
372 /* write table access control word */
373 err
= rtl8366_smi_write_reg(smi
, RTL8366S_TABLE_ACCESS_CTRL_REG
,
374 RTL8366S_TABLE_VLAN_READ_CTRL
);
378 err
= rtl8366_smi_read_reg(smi
, RTL8366S_VLAN_TABLE_READ_BASE
, &data
);
385 err
= rtl8366_smi_read_reg(smi
, RTL8366S_VLAN_TABLE_READ_BASE
+ 1,
396 static int rtl8366s_set_vlan_4k_entry(struct rtl8366s
*rtl
,
397 const struct rtl8366s_vlan4kentry
*vlan4k
)
399 struct rtl8366_smi
*smi
= &rtl
->smi
;
404 if (vlan4k
->vid
>= RTL8366_NUM_VIDS
||
405 vlan4k
->member
> RTL8366_PORT_ALL
||
406 vlan4k
->untag
> RTL8366_PORT_ALL
||
407 vlan4k
->fid
> RTL8366S_FIDMAX
)
410 tableaddr
= (u16
*)vlan4k
;
414 err
= rtl8366_smi_write_reg(smi
, RTL8366S_VLAN_TABLE_WRITE_BASE
, data
);
422 err
= rtl8366_smi_write_reg(smi
, RTL8366S_VLAN_TABLE_WRITE_BASE
+ 1,
427 /* write table access control word */
428 err
= rtl8366_smi_write_reg(smi
, RTL8366S_TABLE_ACCESS_CTRL_REG
,
429 RTL8366S_TABLE_VLAN_WRITE_CTRL
);
434 static int rtl8366s_get_vlan_member_config(struct rtl8366s
*rtl
, u32 index
,
435 struct rtl8366s_vlanconfig
*vlanmc
)
437 struct rtl8366_smi
*smi
= &rtl
->smi
;
443 memset(vlanmc
, '\0', sizeof(struct rtl8366s_vlanconfig
));
445 if (index
>= RTL8366_NUM_VLANS
)
448 tableaddr
= (u16
*)vlanmc
;
450 addr
= RTL8366S_VLAN_MEMCONF_BASE
+ (index
<< 1);
451 err
= rtl8366_smi_read_reg(smi
, addr
, &data
);
458 addr
= RTL8366S_VLAN_MEMCONF_BASE
+ 1 + (index
<< 1);
459 err
= rtl8366_smi_read_reg(smi
, addr
, &data
);
468 static int rtl8366s_set_vlan_member_config(struct rtl8366s
*rtl
, u32 index
,
469 const struct rtl8366s_vlanconfig
472 struct rtl8366_smi
*smi
= &rtl
->smi
;
478 if (index
>= RTL8366_NUM_VLANS
||
479 vlanmc
->vid
>= RTL8366_NUM_VIDS
||
480 vlanmc
->priority
> RTL8366S_PRIORITYMAX
||
481 vlanmc
->member
> RTL8366_PORT_ALL
||
482 vlanmc
->untag
> RTL8366_PORT_ALL
||
483 vlanmc
->fid
> RTL8366S_FIDMAX
)
486 addr
= RTL8366S_VLAN_MEMCONF_BASE
+ (index
<< 1);
488 tableaddr
= (u16
*)vlanmc
;
491 err
= rtl8366_smi_write_reg(smi
, addr
, data
);
495 addr
= RTL8366S_VLAN_MEMCONF_BASE
+ 1 + (index
<< 1);
500 err
= rtl8366_smi_write_reg(smi
, addr
, data
);
507 static int rtl8366s_get_port_vlan_index(struct rtl8366s
*rtl
, int port
,
510 struct rtl8366_smi
*smi
= &rtl
->smi
;
514 if (port
>= RTL8366_NUM_PORTS
)
517 err
= rtl8366_smi_read_reg(smi
, RTL8366S_PORT_VLAN_CTRL_REG(port
),
522 *val
= (data
>> RTL8366S_PORT_VLAN_CTRL_SHIFT(port
)) &
523 RTL8366S_PORT_VLAN_CTRL_MASK
;
529 static int rtl8366s_get_vlan_port_pvid(struct rtl8366s
*rtl
, int port
,
532 struct rtl8366s_vlanconfig vlanmc
;
536 err
= rtl8366s_get_port_vlan_index(rtl
, port
, &index
);
540 err
= rtl8366s_get_vlan_member_config(rtl
, index
, &vlanmc
);
548 static int rtl8366s_set_port_vlan_index(struct rtl8366s
*rtl
, int port
,
551 struct rtl8366_smi
*smi
= &rtl
->smi
;
555 if (port
>= RTL8366_NUM_PORTS
|| index
>= RTL8366_NUM_VLANS
)
558 err
= rtl8366_smi_read_reg(smi
, RTL8366S_PORT_VLAN_CTRL_REG(port
),
563 data
&= ~(RTL8366S_PORT_VLAN_CTRL_MASK
<<
564 RTL8366S_PORT_VLAN_CTRL_SHIFT(port
));
565 data
|= (index
& RTL8366S_PORT_VLAN_CTRL_MASK
) <<
566 RTL8366S_PORT_VLAN_CTRL_SHIFT(port
);
568 err
= rtl8366_smi_write_reg(smi
, RTL8366S_PORT_VLAN_CTRL_REG(port
),
573 static int rtl8366s_set_vlan_port_pvid(struct rtl8366s
*rtl
, int port
, int val
)
576 struct rtl8366s_vlanconfig vlanmc
;
577 struct rtl8366s_vlan4kentry vlan4k
;
579 if (port
>= RTL8366_NUM_PORTS
|| val
>= RTL8366_NUM_VIDS
)
582 /* Updating the 4K entry; lookup it and change the port member set */
583 rtl8366s_get_vlan_4k_entry(rtl
, val
, &vlan4k
);
584 vlan4k
.member
|= ((1 << port
) | RTL8366_PORT_CPU
);
585 vlan4k
.untag
= RTL8366_PORT_ALL_BUT_CPU
;
586 rtl8366s_set_vlan_4k_entry(rtl
, &vlan4k
);
589 * For the 16 entries more work needs to be done. First see if such
590 * VID is already there and change it
592 for (i
= 0; i
< RTL8366_NUM_VLANS
; ++i
) {
593 rtl8366s_get_vlan_member_config(rtl
, i
, &vlanmc
);
595 /* Try to find an existing vid and update port member set */
596 if (val
== vlanmc
.vid
) {
597 vlanmc
.member
|= ((1 << port
) | RTL8366_PORT_CPU
);
598 rtl8366s_set_vlan_member_config(rtl
, i
, &vlanmc
);
600 /* Now update PVID register settings */
601 rtl8366s_set_port_vlan_index(rtl
, port
, i
);
608 * PVID could not be found from vlan table. Replace unused (one that
609 * has no member ports) with new one
611 for (i
= 0; i
< RTL8366_NUM_VLANS
; ++i
) {
612 rtl8366s_get_vlan_member_config(rtl
, i
, &vlanmc
);
615 * See if this vlan member configuration is unused. It is
616 * unused if member set contains no ports or CPU port only
618 if (!vlanmc
.member
|| vlanmc
.member
== RTL8366_PORT_CPU
) {
621 vlanmc
.untag
= RTL8366_PORT_ALL_BUT_CPU
;
622 vlanmc
.member
= ((1 << port
) | RTL8366_PORT_CPU
);
625 rtl8366s_set_vlan_member_config(rtl
, i
, &vlanmc
);
627 /* Now update PVID register settings */
628 rtl8366s_set_port_vlan_index(rtl
, port
, i
);
635 "All 16 vlan member configurations are in use\n");
641 static int rtl8366s_vlan_set_vlan(struct rtl8366s
*rtl
, int enable
)
643 struct rtl8366_smi
*smi
= &rtl
->smi
;
646 rtl8366_smi_read_reg(smi
, RTL8366_CHIP_GLOBAL_CTRL_REG
, &data
);
649 data
|= RTL8366_CHIP_CTRL_VLAN
;
651 data
&= ~RTL8366_CHIP_CTRL_VLAN
;
653 return rtl8366_smi_write_reg(smi
, RTL8366_CHIP_GLOBAL_CTRL_REG
, data
);
656 static int rtl8366s_vlan_set_4ktable(struct rtl8366s
*rtl
, int enable
)
658 struct rtl8366_smi
*smi
= &rtl
->smi
;
661 rtl8366_smi_read_reg(smi
, RTL8366S_VLAN_TB_CTRL_REG
, &data
);
668 return rtl8366_smi_write_reg(smi
, RTL8366S_VLAN_TB_CTRL_REG
, data
);
671 static int rtl8366s_reset_vlan(struct rtl8366s
*rtl
)
673 struct rtl8366s_vlan4kentry vlan4k
;
674 struct rtl8366s_vlanconfig vlanmc
;
678 /* clear 16 VLAN member configuration */
684 for (i
= 0; i
< RTL8366_NUM_VLANS
; i
++) {
685 err
= rtl8366s_set_vlan_member_config(rtl
, i
, &vlanmc
);
690 /* Set a default VLAN with vid 1 to 4K table for all ports */
692 vlan4k
.member
= RTL8366_PORT_ALL
;
693 vlan4k
.untag
= RTL8366_PORT_ALL
;
695 err
= rtl8366s_set_vlan_4k_entry(rtl
, &vlan4k
);
699 /* Set all ports PVID to default VLAN */
700 for (i
= 0; i
< RTL8366_NUM_PORTS
; i
++) {
701 err
= rtl8366s_set_vlan_port_pvid(rtl
, i
, 0);
709 #ifdef CONFIG_RTL8366S_PHY_DEBUG_FS
710 static int rtl8366s_debugfs_open(struct inode
*inode
, struct file
*file
)
712 file
->private_data
= inode
->i_private
;
716 static ssize_t
rtl8366s_read_debugfs_mibs(struct file
*file
,
717 char __user
*user_buf
,
718 size_t count
, loff_t
*ppos
)
720 struct rtl8366s
*rtl
= (struct rtl8366s
*)file
->private_data
;
722 char *buf
= rtl
->buf
;
724 len
+= snprintf(buf
+ len
, sizeof(rtl
->buf
) - len
, "MIB Counters:\n");
725 len
+= snprintf(buf
+ len
, sizeof(rtl
->buf
) - len
, "Counter"
727 "Port 0 \t\t Port 1 \t\t Port 2 \t\t Port 3 \t\t "
730 for (i
= 0; i
< 33; ++i
) {
731 len
+= snprintf(buf
+ len
, sizeof(rtl
->buf
) - len
, "%d:%s ",
732 i
, rtl8366s_mib_counters
[i
].name
);
733 for (j
= 0; j
< RTL8366_NUM_PORTS
; ++j
) {
734 unsigned long long counter
= 0;
736 if (!rtl8366_get_mib_counter(rtl
, i
, j
, &counter
))
737 len
+= snprintf(buf
+ len
,
738 sizeof(rtl
->buf
) - len
,
741 len
+= snprintf(buf
+ len
,
742 sizeof(rtl
->buf
) - len
,
745 if (j
!= RTL8366_NUM_PORTS
- 1) {
746 if (counter
< 100000)
747 len
+= snprintf(buf
+ len
,
748 sizeof(rtl
->buf
) - len
,
751 len
+= snprintf(buf
+ len
,
752 sizeof(rtl
->buf
) - len
,
756 len
+= snprintf(buf
+ len
, sizeof(rtl
->buf
) - len
, "\n");
759 len
+= snprintf(buf
+ len
, sizeof(rtl
->buf
) - len
, "\n");
761 return simple_read_from_buffer(user_buf
, count
, ppos
, buf
, len
);
764 static ssize_t
rtl8366s_read_debugfs_vlan(struct file
*file
,
765 char __user
*user_buf
,
766 size_t count
, loff_t
*ppos
)
768 struct rtl8366s
*rtl
= (struct rtl8366s
*)file
->private_data
;
770 char *buf
= rtl
->buf
;
772 len
+= snprintf(buf
+ len
, sizeof(rtl
->buf
) - len
,
773 "VLAN Member Config:\n");
774 len
+= snprintf(buf
+ len
, sizeof(rtl
->buf
) - len
,
775 "\t id \t vid \t prio \t member \t untag \t fid "
778 for (i
= 0; i
< RTL8366_NUM_VLANS
; ++i
) {
779 struct rtl8366s_vlanconfig vlanmc
;
781 rtl8366s_get_vlan_member_config(rtl
, i
, &vlanmc
);
783 len
+= snprintf(buf
+ len
, sizeof(rtl
->buf
) - len
,
784 "\t[%d] \t %d \t %d \t 0x%04x \t 0x%04x \t %d "
785 "\t", i
, vlanmc
.vid
, vlanmc
.priority
,
786 vlanmc
.member
, vlanmc
.untag
, vlanmc
.fid
);
788 for (j
= 0; j
< RTL8366_NUM_PORTS
; ++j
) {
790 if (!rtl8366s_get_port_vlan_index(rtl
, j
, &index
)) {
792 len
+= snprintf(buf
+ len
,
793 sizeof(rtl
->buf
) - len
,
797 len
+= snprintf(buf
+ len
, sizeof(rtl
->buf
) - len
, "\n");
800 return simple_read_from_buffer(user_buf
, count
, ppos
, buf
, len
);
803 static ssize_t
rtl8366s_read_debugfs_reg(struct file
*file
,
804 char __user
*user_buf
,
805 size_t count
, loff_t
*ppos
)
807 struct rtl8366s
*rtl
= (struct rtl8366s
*)file
->private_data
;
808 struct rtl8366_smi
*smi
= &rtl
->smi
;
809 u32 t
, reg
= g_dbg_reg
;
811 char *buf
= rtl
->buf
;
813 memset(buf
, '\0', sizeof(rtl
->buf
));
815 err
= rtl8366_smi_read_reg(smi
, reg
, &t
);
817 len
+= snprintf(buf
, sizeof(rtl
->buf
),
818 "Read failed (reg: 0x%04x)\n", reg
);
819 return simple_read_from_buffer(user_buf
, count
, ppos
, buf
, len
);
822 len
+= snprintf(buf
, sizeof(rtl
->buf
), "reg = 0x%04x, val = 0x%04x\n",
825 return simple_read_from_buffer(user_buf
, count
, ppos
, buf
, len
);
828 static ssize_t
rtl8366s_write_debugfs_reg(struct file
*file
,
829 const char __user
*user_buf
,
830 size_t count
, loff_t
*ppos
)
832 struct rtl8366s
*rtl
= (struct rtl8366s
*)file
->private_data
;
833 struct rtl8366_smi
*smi
= &rtl
->smi
;
838 char *buf
= rtl
->buf
;
840 len
= min(count
, sizeof(rtl
->buf
) - 1);
841 if (copy_from_user(buf
, user_buf
, len
)) {
842 dev_err(rtl
->parent
, "copy from user failed\n");
847 if (len
> 0 && buf
[len
- 1] == '\n')
851 if (strict_strtoul(buf
, 16, &data
)) {
852 dev_err(rtl
->parent
, "Invalid reg value %s\n", buf
);
854 err
= rtl8366_smi_write_reg(smi
, reg
, data
);
857 "writing reg 0x%04x val 0x%04lx failed\n",
865 static const struct file_operations fops_rtl8366s_regs
= {
866 .read
= rtl8366s_read_debugfs_reg
,
867 .write
= rtl8366s_write_debugfs_reg
,
868 .open
= rtl8366s_debugfs_open
,
872 static const struct file_operations fops_rtl8366s_vlan
= {
873 .read
= rtl8366s_read_debugfs_vlan
,
874 .open
= rtl8366s_debugfs_open
,
878 static const struct file_operations fops_rtl8366s_mibs
= {
879 .read
= rtl8366s_read_debugfs_mibs
,
880 .open
= rtl8366s_debugfs_open
,
884 static void rtl8366s_debugfs_init(struct rtl8366s
*rtl
)
889 if (!rtl
->debugfs_root
)
890 rtl
->debugfs_root
= debugfs_create_dir("rtl8366s", NULL
);
892 if (!rtl
->debugfs_root
) {
893 dev_err(rtl
->parent
, "Unable to create debugfs dir\n");
896 root
= rtl
->debugfs_root
;
898 node
= debugfs_create_x16("reg", S_IRUGO
| S_IWUSR
, root
, &g_dbg_reg
);
900 dev_err(rtl
->parent
, "Creating debugfs file reg failed\n");
904 node
= debugfs_create_file("val", S_IRUGO
| S_IWUSR
, root
, rtl
,
905 &fops_rtl8366s_regs
);
907 dev_err(rtl
->parent
, "Creating debugfs file val failed\n");
911 node
= debugfs_create_file("vlan", S_IRUSR
, root
, rtl
,
912 &fops_rtl8366s_vlan
);
915 "Creating debugfs file vlan failed\n");
919 node
= debugfs_create_file("mibs", S_IRUSR
, root
, rtl
,
920 &fops_rtl8366s_mibs
);
923 "Creating debugfs file mibs failed\n");
928 static void rtl8366s_debugfs_remove(struct rtl8366s
*rtl
)
930 if (rtl
->debugfs_root
) {
931 debugfs_remove_recursive(rtl
->debugfs_root
);
932 rtl
->debugfs_root
= NULL
;
937 static inline void rtl8366s_debugfs_init(struct rtl8366s
*rtl
) {}
938 static inline void rtl8366s_debugfs_remove(struct rtl8366s
*rtl
) {}
939 #endif /* CONFIG_RTL8366S_PHY_DEBUG_FS */
941 static int rtl8366s_sw_reset_mibs(struct switch_dev
*dev
,
942 const struct switch_attr
*attr
,
943 struct switch_val
*val
)
945 struct rtl8366s
*rtl
= sw_to_rtl8366s(dev
);
946 struct rtl8366_smi
*smi
= &rtl
->smi
;
949 if (val
->value
.i
== 1) {
950 rtl8366_smi_read_reg(smi
, RTL8366S_MIB_CTRL_REG
, &data
);
952 rtl8366_smi_write_reg(smi
, RTL8366S_MIB_CTRL_REG
, data
);
958 static int rtl8366s_sw_get_vlan_enable(struct switch_dev
*dev
,
959 const struct switch_attr
*attr
,
960 struct switch_val
*val
)
962 struct rtl8366s
*rtl
= sw_to_rtl8366s(dev
);
963 struct rtl8366_smi
*smi
= &rtl
->smi
;
966 if (attr
->ofs
== 1) {
967 rtl8366_smi_read_reg(smi
, RTL8366_CHIP_GLOBAL_CTRL_REG
, &data
);
969 if (data
& RTL8366_CHIP_CTRL_VLAN
)
973 } else if (attr
->ofs
== 2) {
974 rtl8366_smi_read_reg(smi
, RTL8366S_VLAN_TB_CTRL_REG
, &data
);
985 static int rtl8366s_sw_get_blinkrate(struct switch_dev
*dev
,
986 const struct switch_attr
*attr
,
987 struct switch_val
*val
)
989 struct rtl8366s
*rtl
= sw_to_rtl8366s(dev
);
990 struct rtl8366_smi
*smi
= &rtl
->smi
;
993 rtl8366_smi_read_reg(smi
, RTL8366_LED_BLINKRATE_REG
, &data
);
995 val
->value
.i
= (data
& (RTL8366_LED_BLINKRATE_MASK
));
1000 static int rtl8366s_sw_set_blinkrate(struct switch_dev
*dev
,
1001 const struct switch_attr
*attr
,
1002 struct switch_val
*val
)
1004 struct rtl8366s
*rtl
= sw_to_rtl8366s(dev
);
1005 struct rtl8366_smi
*smi
= &rtl
->smi
;
1008 if (val
->value
.i
>= 6)
1011 rtl8366_smi_read_reg(smi
, RTL8366_LED_BLINKRATE_REG
, &data
);
1013 data
&= ~RTL8366_LED_BLINKRATE_MASK
;
1014 data
|= val
->value
.i
;
1016 rtl8366_smi_write_reg(smi
, RTL8366_LED_BLINKRATE_REG
, data
);
1021 static int rtl8366s_sw_set_vlan_enable(struct switch_dev
*dev
,
1022 const struct switch_attr
*attr
,
1023 struct switch_val
*val
)
1025 struct rtl8366s
*rtl
= sw_to_rtl8366s(dev
);
1028 return rtl8366s_vlan_set_vlan(rtl
, val
->value
.i
);
1030 return rtl8366s_vlan_set_4ktable(rtl
, val
->value
.i
);
1033 static const char *rtl8366s_speed_str(unsigned speed
)
1047 static int rtl8366s_sw_get_port_link(struct switch_dev
*dev
,
1048 const struct switch_attr
*attr
,
1049 struct switch_val
*val
)
1051 struct rtl8366s
*rtl
= sw_to_rtl8366s(dev
);
1052 struct rtl8366_smi
*smi
= &rtl
->smi
;
1053 u32 len
= 0, data
= 0;
1055 if (val
->port_vlan
>= RTL8366_NUM_PORTS
)
1058 memset(rtl
->buf
, '\0', sizeof(rtl
->buf
));
1059 rtl8366_smi_read_reg(smi
, RTL8366S_PORT_LINK_STATUS_BASE
+
1060 (val
->port_vlan
/ 2), &data
);
1062 if (val
->port_vlan
% 2)
1065 len
= snprintf(rtl
->buf
, sizeof(rtl
->buf
),
1066 "port:%d link:%s speed:%s %s-duplex %s%s%s",
1068 (data
& RTL8366S_PORT_STATUS_LINK_MASK
) ? "up" : "down",
1069 rtl8366s_speed_str(data
&
1070 RTL8366S_PORT_STATUS_SPEED_MASK
),
1071 (data
& RTL8366S_PORT_STATUS_DUPLEX_MASK
) ?
1073 (data
& RTL8366S_PORT_STATUS_TXPAUSE_MASK
) ?
1075 (data
& RTL8366S_PORT_STATUS_RXPAUSE_MASK
) ?
1077 (data
& RTL8366S_PORT_STATUS_AN_MASK
) ? "nway ": "");
1079 val
->value
.s
= rtl
->buf
;
1085 static int rtl8366s_sw_get_vlan_info(struct switch_dev
*dev
,
1086 const struct switch_attr
*attr
,
1087 struct switch_val
*val
)
1091 struct rtl8366s_vlanconfig vlanmc
;
1092 struct rtl8366s_vlan4kentry vlan4k
;
1093 struct rtl8366s
*rtl
= sw_to_rtl8366s(dev
);
1094 char *buf
= rtl
->buf
;
1096 if (val
->port_vlan
>= RTL8366_NUM_VLANS
)
1099 memset(buf
, '\0', sizeof(rtl
->buf
));
1101 rtl8366s_get_vlan_member_config(rtl
, val
->port_vlan
, &vlanmc
);
1102 rtl8366s_get_vlan_4k_entry(rtl
, vlanmc
.vid
, &vlan4k
);
1104 len
+= snprintf(buf
+ len
, sizeof(rtl
->buf
) - len
, "VLAN %d: Ports: ",
1107 for (i
= 0; i
< RTL8366_NUM_PORTS
; ++i
) {
1109 if (!rtl8366s_get_port_vlan_index(rtl
, i
, &index
) &&
1110 index
== val
->port_vlan
)
1111 len
+= snprintf(buf
+ len
, sizeof(rtl
->buf
) - len
,
1114 len
+= snprintf(buf
+ len
, sizeof(rtl
->buf
) - len
, "\n");
1116 len
+= snprintf(buf
+ len
, sizeof(rtl
->buf
) - len
,
1117 "\t\t vid \t prio \t member \t untag \t fid\n");
1118 len
+= snprintf(buf
+ len
, sizeof(rtl
->buf
) - len
, "\tMC:\t");
1119 len
+= snprintf(buf
+ len
, sizeof(rtl
->buf
) - len
,
1120 "%d \t %d \t 0x%04x \t 0x%04x \t %d\n",
1121 vlanmc
.vid
, vlanmc
.priority
, vlanmc
.member
,
1122 vlanmc
.untag
, vlanmc
.fid
);
1123 len
+= snprintf(buf
+ len
, sizeof(rtl
->buf
) - len
, "\t4K:\t");
1124 len
+= snprintf(buf
+ len
, sizeof(rtl
->buf
) - len
,
1125 "%d \t \t 0x%04x \t 0x%04x \t %d",
1126 vlan4k
.vid
, vlan4k
.member
, vlan4k
.untag
, vlan4k
.fid
);
1134 static int rtl8366s_sw_set_port_led(struct switch_dev
*dev
,
1135 const struct switch_attr
*attr
,
1136 struct switch_val
*val
)
1138 struct rtl8366s
*rtl
= sw_to_rtl8366s(dev
);
1139 struct rtl8366_smi
*smi
= &rtl
->smi
;
1142 if (val
->port_vlan
>= RTL8366_NUM_PORTS
||
1143 (1 << val
->port_vlan
) == RTL8366_PORT_UNKNOWN
)
1146 if (val
->port_vlan
== RTL8366_PORT_NUM_CPU
) {
1147 rtl8366_smi_read_reg(smi
, RTL8366_LED_BLINKRATE_REG
, &data
);
1148 data
= (data
& (~(0xF << 4))) | (val
->value
.i
<< 4);
1149 rtl8366_smi_write_reg(smi
, RTL8366_LED_BLINKRATE_REG
, data
);
1151 rtl8366_smi_read_reg(smi
, RTL8366_LED_CTRL_REG
, &data
);
1152 data
= (data
& (~(0xF << (val
->port_vlan
* 4)))) |
1153 (val
->value
.i
<< (val
->port_vlan
* 4));
1154 rtl8366_smi_write_reg(smi
, RTL8366_LED_CTRL_REG
, data
);
1160 static int rtl8366s_sw_get_port_led(struct switch_dev
*dev
,
1161 const struct switch_attr
*attr
,
1162 struct switch_val
*val
)
1164 struct rtl8366s
*rtl
= sw_to_rtl8366s(dev
);
1165 struct rtl8366_smi
*smi
= &rtl
->smi
;
1168 if (val
->port_vlan
>= RTL8366_NUM_LEDGROUPS
)
1171 rtl8366_smi_read_reg(smi
, RTL8366_LED_CTRL_REG
, &data
);
1172 val
->value
.i
= (data
>> (val
->port_vlan
* 4)) & 0x000F;
1177 static int rtl8366s_sw_reset_port_mibs(struct switch_dev
*dev
,
1178 const struct switch_attr
*attr
,
1179 struct switch_val
*val
)
1181 struct rtl8366s
*rtl
= sw_to_rtl8366s(dev
);
1182 struct rtl8366_smi
*smi
= &rtl
->smi
;
1185 if (val
->port_vlan
>= RTL8366_NUM_PORTS
)
1188 rtl8366_smi_read_reg(smi
, RTL8366S_MIB_CTRL_REG
, &data
);
1189 data
|= (1 << (val
->port_vlan
+ 3));
1190 rtl8366_smi_write_reg(smi
, RTL8366S_MIB_CTRL_REG
, data
);
1195 static int rtl8366s_sw_get_port_mib(struct switch_dev
*dev
,
1196 const struct switch_attr
*attr
,
1197 struct switch_val
*val
)
1199 struct rtl8366s
*rtl
= sw_to_rtl8366s(dev
);
1201 unsigned long long counter
= 0;
1202 char *buf
= rtl
->buf
;
1204 if (val
->port_vlan
>= RTL8366_NUM_PORTS
)
1207 len
+= snprintf(buf
+ len
, sizeof(rtl
->buf
) - len
,
1208 "Port %d MIB counters\n",
1211 for (i
= 0; i
< RTL8366S_MIB_COUNT
; ++i
) {
1212 len
+= snprintf(buf
+ len
, sizeof(rtl
->buf
) - len
,
1213 "%d:%s\t", i
, rtl8366s_mib_counters
[i
].name
);
1214 if (!rtl8366_get_mib_counter(rtl
, i
, val
->port_vlan
, &counter
))
1215 len
+= snprintf(buf
+ len
, sizeof(rtl
->buf
) - len
,
1216 "[%llu]\n", counter
);
1218 len
+= snprintf(buf
+ len
, sizeof(rtl
->buf
) - len
,
1227 static int rtl8366s_sw_get_vlan_ports(struct switch_dev
*dev
,
1228 struct switch_val
*val
)
1230 struct rtl8366s_vlanconfig vlanmc
;
1231 struct rtl8366s
*rtl
= sw_to_rtl8366s(dev
);
1232 struct switch_port
*port
;
1235 if (val
->port_vlan
>= RTL8366_NUM_VLANS
)
1238 rtl8366s_get_vlan_member_config(rtl
, val
->port_vlan
, &vlanmc
);
1240 port
= &val
->value
.ports
[0];
1242 for (i
= 0; i
< RTL8366_NUM_PORTS
; i
++) {
1243 if (!(vlanmc
.member
& BIT(i
)))
1247 port
->flags
= (vlanmc
.untag
& BIT(i
)) ?
1248 0 : BIT(SWITCH_PORT_FLAG_TAGGED
);
1255 static int rtl8366s_sw_set_vlan_ports(struct switch_dev
*dev
,
1256 struct switch_val
*val
)
1258 struct rtl8366s_vlanconfig vlanmc
;
1259 struct rtl8366s_vlan4kentry vlan4k
;
1260 struct rtl8366s
*rtl
= sw_to_rtl8366s(dev
);
1261 struct switch_port
*port
;
1264 if (val
->port_vlan
>= RTL8366_NUM_VLANS
)
1267 rtl8366s_get_vlan_member_config(rtl
, val
->port_vlan
, &vlanmc
);
1268 rtl8366s_get_vlan_4k_entry(rtl
, vlanmc
.vid
, &vlan4k
);
1273 port
= &val
->value
.ports
[0];
1274 for (i
= 0; i
< val
->len
; i
++, port
++) {
1275 vlanmc
.member
|= BIT(port
->id
);
1277 if (!(port
->flags
& BIT(SWITCH_PORT_FLAG_TAGGED
)))
1278 vlanmc
.untag
|= BIT(port
->id
);
1281 vlan4k
.member
= vlanmc
.member
;
1282 vlan4k
.untag
= vlanmc
.untag
;
1284 rtl8366s_set_vlan_member_config(rtl
, val
->port_vlan
, &vlanmc
);
1285 rtl8366s_set_vlan_4k_entry(rtl
, &vlan4k
);
1289 static int rtl8366s_sw_get_port_pvid(struct switch_dev
*dev
, int port
, int *val
)
1291 struct rtl8366s
*rtl
= sw_to_rtl8366s(dev
);
1292 return rtl8366s_get_vlan_port_pvid(rtl
, port
, val
);
1295 static int rtl8366s_sw_set_port_pvid(struct switch_dev
*dev
, int port
, int val
)
1297 struct rtl8366s
*rtl
= sw_to_rtl8366s(dev
);
1298 return rtl8366s_set_vlan_port_pvid(rtl
, port
, val
);
1301 static int rtl8366s_sw_reset_switch(struct switch_dev
*dev
)
1303 struct rtl8366s
*rtl
= sw_to_rtl8366s(dev
);
1304 struct rtl8366_smi
*smi
= &rtl
->smi
;
1308 rtl8366_smi_write_reg(smi
, RTL8366_RESET_CTRL_REG
,
1309 RTL8366_CHIP_CTRL_RESET_HW
);
1312 if (rtl8366_smi_read_reg(smi
, RTL8366_RESET_CTRL_REG
, &data
))
1315 if (!(data
& RTL8366_CHIP_CTRL_RESET_HW
))
1317 } while (--timeout
);
1320 printk("Timeout waiting for the switch to reset\n");
1324 return rtl8366s_reset_vlan(rtl
);
1327 static struct switch_attr rtl8366s_globals
[] = {
1329 .type
= SWITCH_TYPE_INT
,
1330 .name
= "enable_vlan",
1331 .description
= "Enable VLAN mode",
1332 .set
= rtl8366s_sw_set_vlan_enable
,
1333 .get
= rtl8366s_sw_get_vlan_enable
,
1337 .type
= SWITCH_TYPE_INT
,
1338 .name
= "enable_vlan4k",
1339 .description
= "Enable VLAN 4K mode",
1340 .set
= rtl8366s_sw_set_vlan_enable
,
1341 .get
= rtl8366s_sw_get_vlan_enable
,
1345 .type
= SWITCH_TYPE_INT
,
1346 .name
= "reset_mibs",
1347 .description
= "Reset all MIB counters",
1348 .set
= rtl8366s_sw_reset_mibs
,
1352 .type
= SWITCH_TYPE_INT
,
1353 .name
= "blinkrate",
1354 .description
= "Get/Set LED blinking rate (0 = 43ms, 1 = 84ms,"
1355 " 2 = 120ms, 3 = 170ms, 4 = 340ms, 5 = 670ms)",
1356 .set
= rtl8366s_sw_set_blinkrate
,
1357 .get
= rtl8366s_sw_get_blinkrate
,
1362 static struct switch_attr rtl8366s_port
[] = {
1364 .type
= SWITCH_TYPE_STRING
,
1366 .description
= "Get port link information",
1369 .get
= rtl8366s_sw_get_port_link
,
1371 .type
= SWITCH_TYPE_INT
,
1372 .name
= "reset_mib",
1373 .description
= "Reset single port MIB counters",
1375 .set
= rtl8366s_sw_reset_port_mibs
,
1378 .type
= SWITCH_TYPE_STRING
,
1380 .description
= "Get MIB counters for port",
1383 .get
= rtl8366s_sw_get_port_mib
,
1385 .type
= SWITCH_TYPE_INT
,
1387 .description
= "Get/Set port group (0 - 3) led mode (0 - 15)",
1389 .set
= rtl8366s_sw_set_port_led
,
1390 .get
= rtl8366s_sw_get_port_led
,
1394 static struct switch_attr rtl8366s_vlan
[] = {
1396 .type
= SWITCH_TYPE_STRING
,
1398 .description
= "Get vlan information",
1401 .get
= rtl8366s_sw_get_vlan_info
,
1406 static struct switch_dev rtl8366_switch_dev
= {
1408 .cpu_port
= RTL8366_PORT_NUM_CPU
,
1409 .ports
= RTL8366_NUM_PORTS
,
1410 .vlans
= RTL8366_NUM_VLANS
,
1412 .attr
= rtl8366s_globals
,
1413 .n_attr
= ARRAY_SIZE(rtl8366s_globals
),
1416 .attr
= rtl8366s_port
,
1417 .n_attr
= ARRAY_SIZE(rtl8366s_port
),
1420 .attr
= rtl8366s_vlan
,
1421 .n_attr
= ARRAY_SIZE(rtl8366s_vlan
),
1424 .get_vlan_ports
= rtl8366s_sw_get_vlan_ports
,
1425 .set_vlan_ports
= rtl8366s_sw_set_vlan_ports
,
1426 .get_port_pvid
= rtl8366s_sw_get_port_pvid
,
1427 .set_port_pvid
= rtl8366s_sw_set_port_pvid
,
1428 .reset_switch
= rtl8366s_sw_reset_switch
,
1431 static int rtl8366s_switch_init(struct rtl8366s
*rtl
)
1433 struct switch_dev
*dev
= &rtl
->dev
;
1436 memcpy(dev
, &rtl8366_switch_dev
, sizeof(struct switch_dev
));
1438 dev
->devname
= dev_name(rtl
->parent
);
1440 err
= register_switch(dev
, NULL
);
1442 dev_err(rtl
->parent
, "switch registration failed\n");
1447 static void rtl8366s_switch_cleanup(struct rtl8366s
*rtl
)
1449 unregister_switch(&rtl
->dev
);
1452 static int rtl8366s_mii_read(struct mii_bus
*bus
, int addr
, int reg
)
1454 struct rtl8366s
*rtl
= bus
->priv
;
1458 err
= rtl8366s_read_phy_reg(rtl
, addr
, 0, reg
, &val
);
1465 static int rtl8366s_mii_write(struct mii_bus
*bus
, int addr
, int reg
, u16 val
)
1467 struct rtl8366s
*rtl
= bus
->priv
;
1471 err
= rtl8366s_write_phy_reg(rtl
, addr
, 0, reg
, val
);
1473 (void) rtl8366s_read_phy_reg(rtl
, addr
, 0, reg
, &t
);
1478 static int rtl8366s_mii_init(struct rtl8366s
*rtl
)
1483 rtl
->mii_bus
= mdiobus_alloc();
1484 if (rtl
->mii_bus
== NULL
) {
1489 rtl
->mii_bus
->priv
= (void *) rtl
;
1490 rtl
->mii_bus
->name
= "rtl8366-rtl";
1491 rtl
->mii_bus
->read
= rtl8366s_mii_read
;
1492 rtl
->mii_bus
->write
= rtl8366s_mii_write
;
1493 snprintf(rtl
->mii_bus
->id
, MII_BUS_ID_SIZE
, "%s",
1494 dev_name(rtl
->parent
));
1495 rtl
->mii_bus
->parent
= rtl
->parent
;
1496 rtl
->mii_bus
->phy_mask
= ~(0x1f);
1497 rtl
->mii_bus
->irq
= rtl
->mii_irq
;
1498 for (i
= 0; i
< PHY_MAX_ADDR
; i
++)
1499 rtl
->mii_irq
[i
] = PHY_POLL
;
1501 ret
= mdiobus_register(rtl
->mii_bus
);
1508 mdiobus_free(rtl
->mii_bus
);
1513 static void rtl8366s_mii_cleanup(struct rtl8366s
*rtl
)
1515 mdiobus_unregister(rtl
->mii_bus
);
1516 mdiobus_free(rtl
->mii_bus
);
1519 static int rtl8366s_mii_bus_match(struct mii_bus
*bus
)
1521 return (bus
->read
== rtl8366s_mii_read
&&
1522 bus
->write
== rtl8366s_mii_write
);
1525 static int rtl8366s_setup(struct rtl8366s
*rtl
)
1527 struct rtl8366_smi
*smi
= &rtl
->smi
;
1532 ret
= rtl8366_smi_read_reg(smi
, RTL8366S_CHIP_ID_REG
, &chip_id
);
1534 dev_err(rtl
->parent
, "unable to read chip id\n");
1539 case RTL8366S_CHIP_ID_8366
:
1542 dev_err(rtl
->parent
, "unknown chip id (%04x)\n", chip_id
);
1546 ret
= rtl8366_smi_read_reg(smi
, RTL8366S_CHIP_VERSION_CTRL_REG
,
1549 dev_err(rtl
->parent
, "unable to read chip version\n");
1553 dev_info(rtl
->parent
, "RTL%04x ver. %u chip found\n",
1554 chip_id
, chip_ver
& RTL8366S_CHIP_VERSION_MASK
);
1556 rtl8366s_debugfs_init(rtl
);
1561 static int __init
rtl8366s_probe(struct platform_device
*pdev
)
1563 static int rtl8366_smi_version_printed
;
1564 struct rtl8366s_platform_data
*pdata
;
1565 struct rtl8366s
*rtl
;
1566 struct rtl8366_smi
*smi
;
1569 if (!rtl8366_smi_version_printed
++)
1570 printk(KERN_NOTICE RTL8366S_DRIVER_DESC
1571 " version " RTL8366S_DRIVER_VER
"\n");
1573 pdata
= pdev
->dev
.platform_data
;
1575 dev_err(&pdev
->dev
, "no platform data specified\n");
1580 rtl
= kzalloc(sizeof(*rtl
), GFP_KERNEL
);
1582 dev_err(&pdev
->dev
, "no memory for private data\n");
1587 rtl
->parent
= &pdev
->dev
;
1590 smi
->parent
= &pdev
->dev
;
1591 smi
->gpio_sda
= pdata
->gpio_sda
;
1592 smi
->gpio_sck
= pdata
->gpio_sck
;
1594 err
= rtl8366_smi_init(smi
);
1598 platform_set_drvdata(pdev
, rtl
);
1600 err
= rtl8366s_setup(rtl
);
1602 goto err_clear_drvdata
;
1604 err
= rtl8366s_mii_init(rtl
);
1606 goto err_clear_drvdata
;
1608 err
= rtl8366s_switch_init(rtl
);
1610 goto err_mii_cleanup
;
1615 rtl8366s_mii_cleanup(rtl
);
1617 platform_set_drvdata(pdev
, NULL
);
1618 rtl8366_smi_cleanup(smi
);
1625 static int rtl8366s_phy_config_init(struct phy_device
*phydev
)
1627 if (!rtl8366s_mii_bus_match(phydev
->bus
))
1633 static int rtl8366s_phy_config_aneg(struct phy_device
*phydev
)
1638 static struct phy_driver rtl8366s_phy_driver
= {
1639 .phy_id
= 0x001cc960,
1640 .name
= "Realtek RTL8366S",
1641 .phy_id_mask
= 0x1ffffff0,
1642 .features
= PHY_GBIT_FEATURES
,
1643 .config_aneg
= rtl8366s_phy_config_aneg
,
1644 .config_init
= rtl8366s_phy_config_init
,
1645 .read_status
= genphy_read_status
,
1647 .owner
= THIS_MODULE
,
1651 static int __devexit
rtl8366s_remove(struct platform_device
*pdev
)
1653 struct rtl8366s
*rtl
= platform_get_drvdata(pdev
);
1656 rtl8366s_switch_cleanup(rtl
);
1657 rtl8366s_debugfs_remove(rtl
);
1658 rtl8366s_mii_cleanup(rtl
);
1659 platform_set_drvdata(pdev
, NULL
);
1660 rtl8366_smi_cleanup(&rtl
->smi
);
1667 static struct platform_driver rtl8366s_driver
= {
1669 .name
= RTL8366S_DRIVER_NAME
,
1670 .owner
= THIS_MODULE
,
1672 .probe
= rtl8366s_probe
,
1673 .remove
= __devexit_p(rtl8366s_remove
),
1676 static int __init
rtl8366s_module_init(void)
1679 ret
= platform_driver_register(&rtl8366s_driver
);
1683 ret
= phy_driver_register(&rtl8366s_phy_driver
);
1685 goto err_platform_unregister
;
1689 err_platform_unregister
:
1690 platform_driver_unregister(&rtl8366s_driver
);
1693 module_init(rtl8366s_module_init
);
1695 static void __exit
rtl8366s_module_exit(void)
1697 phy_driver_unregister(&rtl8366s_phy_driver
);
1698 platform_driver_unregister(&rtl8366s_driver
);
1700 module_exit(rtl8366s_module_exit
);
1702 MODULE_DESCRIPTION(RTL8366S_DRIVER_DESC
);
1703 MODULE_VERSION(RTL8366S_DRIVER_VER
);
1704 MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
1705 MODULE_AUTHOR("Antti Seppälä <a.seppala@gmail.com>");
1706 MODULE_LICENSE("GPL v2");
1707 MODULE_ALIAS("platform:" RTL8366S_DRIVER_NAME
);