2 * Platform driver for the Realtek RTL8366 ethernet switch
4 * Copyright (C) 2009 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/gpio.h>
18 #include <linux/spinlock.h>
19 #include <linux/skbuff.h>
20 #include <linux/switch.h>
21 #include <linux/phy.h>
22 #include <linux/rtl8366_smi.h>
24 #ifdef CONFIG_RTL8366_SMI_DEBUG_FS
25 #include <linux/debugfs.h>
28 #define RTL8366S_DRIVER_NAME "rtl8366-smi"
29 #define RTL8366S_DRIVER_DESC "Realtek RTL8366 switch driver"
30 #define RTL8366S_DRIVER_VER "0.1.1"
32 #define RTL8366S_PHY_NO_MAX 4
33 #define RTL8366S_PHY_PAGE_MAX 7
34 #define RTL8366S_PHY_ADDR_MAX 31
36 #define RTL8366_CHIP_GLOBAL_CTRL_REG 0x0000
37 #define RTL8366_CHIP_CTRL_VLAN (1 << 13)
39 #define RTL8366_RESET_CTRL_REG 0x0100
40 #define RTL8366_CHIP_CTRL_RESET_HW 1
41 #define RTL8366_CHIP_CTRL_RESET_SW (1 << 1)
43 #define RTL8366S_CHIP_VERSION_CTRL_REG 0x0104
44 #define RTL8366S_CHIP_VERSION_MASK 0xf
45 #define RTL8366S_CHIP_ID_REG 0x0105
46 #define RTL8366S_CHIP_ID_8366 0x8366
48 /* PHY registers control */
49 #define RTL8366S_PHY_ACCESS_CTRL_REG 0x8028
50 #define RTL8366S_PHY_ACCESS_DATA_REG 0x8029
52 #define RTL8366S_PHY_CTRL_READ 1
53 #define RTL8366S_PHY_CTRL_WRITE 0
55 #define RTL8366S_PHY_REG_MASK 0x1f
56 #define RTL8366S_PHY_PAGE_OFFSET 5
57 #define RTL8366S_PHY_PAGE_MASK (0x7 << 5)
58 #define RTL8366S_PHY_NO_OFFSET 9
59 #define RTL8366S_PHY_NO_MASK (0x1f << 9)
61 #define RTL8366_SMI_ACK_RETRY_COUNT 5
62 #define RTL8366_SMI_CLK_DELAY 10 /* nsec */
64 /* LED control registers */
65 #define RTL8366_LED_BLINKRATE_REG 0x0420
66 #define RTL8366_LED_BLINKRATE_BIT 0
67 #define RTL8366_LED_BLINKRATE_MASK 0x0007
69 #define RTL8366_LED_CTRL_REG 0x0421
70 #define RTL8366_LED_0_1_CTRL_REG 0x0422
71 #define RTL8366_LED_2_3_CTRL_REG 0x0423
73 #define RTL8366S_MIB_COUNT 33
74 #define RTL8366S_GLOBAL_MIB_COUNT 1
75 #define RTL8366S_MIB_COUNTER_PORT_OFFSET 0x0040
76 #define RTL8366S_MIB_COUNTER_BASE 0x1000
77 #define RTL8366S_MIB_CTRL_REG 0x11F0
78 #define RTL8366S_MIB_CTRL_USER_MASK 0x01FF
79 #define RTL8366S_MIB_CTRL_BUSY_MASK 0x0001
80 #define RTL8366S_MIB_CTRL_RESET_MASK 0x0002
82 #define RTL8366S_MIB_CTRL_GLOBAL_RESET_MASK 0x0004
83 #define RTL8366S_MIB_CTRL_PORT_RESET_BIT 0x0003
84 #define RTL8366S_MIB_CTRL_PORT_RESET_MASK 0x01FC
87 #define RTL8366S_PORT_VLAN_CTRL_BASE 0x0058
88 #define RTL8366S_PORT_VLAN_CTRL_REG(_p) \
89 (RTL8366S_PORT_VLAN_CTRL_BASE + (_p) / 4)
90 #define RTL8366S_PORT_VLAN_CTRL_MASK 0xf
91 #define RTL8366S_PORT_VLAN_CTRL_SHIFT(_p) (4 * ((_p) % 4))
94 #define RTL8366S_VLAN_TABLE_READ_BASE 0x018B
95 #define RTL8366S_VLAN_TABLE_WRITE_BASE 0x0185
97 #define RTL8366S_VLAN_TB_CTRL_REG 0x010F
99 #define RTL8366S_TABLE_ACCESS_CTRL_REG 0x0180
100 #define RTL8366S_TABLE_VLAN_READ_CTRL 0x0E01
101 #define RTL8366S_TABLE_VLAN_WRITE_CTRL 0x0F01
103 #define RTL8366S_VLAN_MEMCONF_BASE 0x0016
106 #define RTL8366S_PORT_LINK_STATUS_BASE 0x0060
107 #define RTL8366S_PORT_STATUS_SPEED_MASK 0x0003
108 #define RTL8366S_PORT_STATUS_DUPLEX_MASK 0x0004
109 #define RTL8366S_PORT_STATUS_LINK_MASK 0x0010
110 #define RTL8366S_PORT_STATUS_TXPAUSE_MASK 0x0020
111 #define RTL8366S_PORT_STATUS_RXPAUSE_MASK 0x0040
112 #define RTL8366S_PORT_STATUS_AN_MASK 0x0080
115 #define RTL8366_PORT_NUM_CPU 5
116 #define RTL8366_NUM_PORTS 6
117 #define RTL8366_NUM_VLANS 16
118 #define RTL8366_NUM_LEDGROUPS 4
119 #define RTL8366_NUM_VIDS 4096
120 #define RTL8366S_PRIORITYMAX 7
121 #define RTL8366S_FIDMAX 7
124 #define RTL8366_PORT_1 (1 << 0) /* In userspace port 0 */
125 #define RTL8366_PORT_2 (1 << 1) /* In userspace port 1 */
126 #define RTL8366_PORT_3 (1 << 2) /* In userspace port 2 */
127 #define RTL8366_PORT_4 (1 << 3) /* In userspace port 3 */
129 #define RTL8366_PORT_UNKNOWN (1 << 4) /* No known connection */
130 #define RTL8366_PORT_CPU (1 << 5) /* CPU port */
132 #define RTL8366_PORT_ALL (RTL8366_PORT_1 | \
136 RTL8366_PORT_UNKNOWN | \
139 #define RTL8366_PORT_ALL_BUT_CPU (RTL8366_PORT_1 | \
143 RTL8366_PORT_UNKNOWN)
145 #define RTL8366_PORT_ALL_EXTERNAL (RTL8366_PORT_1 | \
150 #define RTL8366_PORT_ALL_INTERNAL (RTL8366_PORT_UNKNOWN | \
153 struct rtl8366s_vlanconfig
{
164 struct rtl8366s_vlan4kentry
{
174 static const char *MIBCOUNTERS
[] = {
177 "EtherStatsUnderSizePkts ",
179 "EtherStatsPkts64Octets ",
180 "EtherStatsPkts65to127Octets ",
181 "EtherStatsPkts128to255Octets ",
182 "EtherStatsPkts256to511Octets ",
183 "EtherStatsPkts512to1023Octets ",
184 "EtherStatsPkts1024to1518Octets ",
185 "EtherOversizeStats ",
186 "EtherStatsJabbers ",
188 "EtherStatsMulticastPkts ",
189 "EtherStatsBroadcastPkts ",
190 "EtherStatsDropEvents ",
191 "Dot3StatsFCSErrors ",
192 "Dot3StatsSymbolErrors ",
193 "Dot3InPauseFrames ",
194 "Dot3ControlInUnknownOpcodes ",
196 "Dot3StatsSingleCollisionFrames ",
197 "Dot3StatMultipleCollisionFrames ",
198 "Dot3sDeferredTransmissions ",
199 "Dot3StatsLateCollisions ",
200 "EtherStatsCollisions ",
201 "Dot3StatsExcessiveCollisions ",
202 "Dot3OutPauseFrames ",
203 "Dot1dBasePortDelayExceededDiscards",
204 "Dot1dTpPortInDiscards ",
206 "IfOutMulticastPkts ",
207 "IfOutBroadcastPkts ",
212 struct device
*parent
;
213 unsigned int gpio_sda
;
214 unsigned int gpio_sck
;
219 struct device
*parent
;
220 struct rtl8366_smi smi
;
221 struct mii_bus
*mii_bus
;
222 int mii_irq
[PHY_MAX_ADDR
];
223 struct switch_dev dev
;
225 #ifdef CONFIG_RTL8366_SMI_DEBUG_FS
226 struct dentry
*debugfs_root
;
230 #ifdef CONFIG_RTL8366_SMI_DEBUG_FS
234 static inline struct rtl8366s
*sw_to_rtl8366s(struct switch_dev
*sw
)
236 return container_of(sw
, struct rtl8366s
, dev
);
239 static inline void rtl8366_smi_clk_delay(struct rtl8366_smi
*smi
)
241 ndelay(RTL8366_SMI_CLK_DELAY
);
244 static void rtl8366_smi_start(struct rtl8366_smi
*smi
)
246 unsigned int sda
= smi
->gpio_sda
;
247 unsigned int sck
= smi
->gpio_sck
;
250 * Set GPIO pins to output mode, with initial state:
253 gpio_direction_output(sck
, 0);
254 gpio_direction_output(sda
, 1);
255 rtl8366_smi_clk_delay(smi
);
257 /* CLK 1: 0 -> 1, 1 -> 0 */
258 gpio_set_value(sck
, 1);
259 rtl8366_smi_clk_delay(smi
);
260 gpio_set_value(sck
, 0);
261 rtl8366_smi_clk_delay(smi
);
264 gpio_set_value(sck
, 1);
265 rtl8366_smi_clk_delay(smi
);
266 gpio_set_value(sda
, 0);
267 rtl8366_smi_clk_delay(smi
);
268 gpio_set_value(sck
, 0);
269 rtl8366_smi_clk_delay(smi
);
270 gpio_set_value(sda
, 1);
273 static void rtl8366_smi_stop(struct rtl8366_smi
*smi
)
275 unsigned int sda
= smi
->gpio_sda
;
276 unsigned int sck
= smi
->gpio_sck
;
278 rtl8366_smi_clk_delay(smi
);
279 gpio_set_value(sda
, 0);
280 gpio_set_value(sck
, 1);
281 rtl8366_smi_clk_delay(smi
);
282 gpio_set_value(sda
, 1);
283 rtl8366_smi_clk_delay(smi
);
284 gpio_set_value(sck
, 1);
285 rtl8366_smi_clk_delay(smi
);
286 gpio_set_value(sck
, 0);
287 rtl8366_smi_clk_delay(smi
);
288 gpio_set_value(sck
, 1);
291 rtl8366_smi_clk_delay(smi
);
292 gpio_set_value(sck
, 0);
293 rtl8366_smi_clk_delay(smi
);
294 gpio_set_value(sck
, 1);
296 /* set GPIO pins to input mode */
297 gpio_direction_input(sda
);
298 gpio_direction_input(sck
);
301 static void rtl8366_smi_write_bits(struct rtl8366_smi
*smi
, u32 data
, u32 len
)
303 unsigned int sda
= smi
->gpio_sda
;
304 unsigned int sck
= smi
->gpio_sck
;
306 for (; len
> 0; len
--) {
307 rtl8366_smi_clk_delay(smi
);
310 if ( data
& ( 1 << (len
- 1)) )
311 gpio_set_value(sda
, 1);
313 gpio_set_value(sda
, 0);
314 rtl8366_smi_clk_delay(smi
);
317 gpio_set_value(sck
, 1);
318 rtl8366_smi_clk_delay(smi
);
319 gpio_set_value(sck
, 0);
323 static void rtl8366_smi_read_bits(struct rtl8366_smi
*smi
, u32 len
, u32
*data
)
325 unsigned int sda
= smi
->gpio_sda
;
326 unsigned int sck
= smi
->gpio_sck
;
328 gpio_direction_input(sda
);
330 for (*data
= 0; len
> 0; len
--) {
333 rtl8366_smi_clk_delay(smi
);
336 gpio_set_value(sck
, 1);
337 rtl8366_smi_clk_delay(smi
);
338 u
= gpio_get_value(sda
);
339 gpio_set_value(sck
, 0);
341 *data
|= (u
<< (len
- 1));
344 gpio_direction_output(sda
, 0);
347 static int rtl8366_smi_wait_for_ack(struct rtl8366_smi
*smi
)
355 rtl8366_smi_read_bits(smi
, 1, &ack
);
359 if (++retry_cnt
> RTL8366_SMI_ACK_RETRY_COUNT
)
366 static int rtl8366_smi_write_byte(struct rtl8366_smi
*smi
, u8 data
)
368 rtl8366_smi_write_bits(smi
, data
, 8);
369 return rtl8366_smi_wait_for_ack(smi
);
372 static int rtl8366_smi_read_byte0(struct rtl8366_smi
*smi
, u8
*data
)
377 rtl8366_smi_read_bits(smi
, 8, &t
);
381 rtl8366_smi_write_bits(smi
, 0x00, 1);
386 static int rtl8366_smi_read_byte1(struct rtl8366_smi
*smi
, u8
*data
)
391 rtl8366_smi_read_bits(smi
, 8, &t
);
395 rtl8366_smi_write_bits(smi
, 0x01, 1);
400 static int rtl8366_smi_read_reg(struct rtl8366_smi
*smi
, u32 addr
, u32
*data
)
407 spin_lock_irqsave(&smi
->lock
, flags
);
409 rtl8366_smi_start(smi
);
411 /* send READ command */
412 ret
= rtl8366_smi_write_byte(smi
, 0x0a << 4 | 0x04 << 1 | 0x01);
417 ret
= rtl8366_smi_write_byte(smi
, addr
& 0xff);
422 ret
= rtl8366_smi_write_byte(smi
, addr
>> 8);
427 rtl8366_smi_read_byte0(smi
, &lo
);
428 /* read DATA[15:8] */
429 rtl8366_smi_read_byte1(smi
, &hi
);
431 *data
= ((u32
) lo
) | (((u32
) hi
) << 8);
436 rtl8366_smi_stop(smi
);
437 spin_unlock_irqrestore(&smi
->lock
, flags
);
442 static int rtl8366_smi_write_reg(struct rtl8366_smi
*smi
, u32 addr
, u32 data
)
447 spin_lock_irqsave(&smi
->lock
, flags
);
449 rtl8366_smi_start(smi
);
451 /* send WRITE command */
452 ret
= rtl8366_smi_write_byte(smi
, 0x0a << 4 | 0x04 << 1 | 0x00);
457 ret
= rtl8366_smi_write_byte(smi
, addr
& 0xff);
462 ret
= rtl8366_smi_write_byte(smi
, addr
>> 8);
466 /* write DATA[7:0] */
467 ret
= rtl8366_smi_write_byte(smi
, data
& 0xff);
471 /* write DATA[15:8] */
472 ret
= rtl8366_smi_write_byte(smi
, data
>> 8);
479 rtl8366_smi_stop(smi
);
480 spin_unlock_irqrestore(&smi
->lock
, flags
);
485 static int rtl8366s_read_phy_reg(struct rtl8366s
*rtl
,
486 u32 phy_no
, u32 page
, u32 addr
, u32
*data
)
488 struct rtl8366_smi
*smi
= &rtl
->smi
;
492 if (phy_no
> RTL8366S_PHY_NO_MAX
)
495 if (page
> RTL8366S_PHY_PAGE_MAX
)
498 if (addr
> RTL8366S_PHY_ADDR_MAX
)
501 ret
= rtl8366_smi_write_reg(smi
, RTL8366S_PHY_ACCESS_CTRL_REG
,
502 RTL8366S_PHY_CTRL_READ
);
506 reg
= 0x8000 | (1 << (phy_no
+ RTL8366S_PHY_NO_OFFSET
)) |
507 ((page
<< RTL8366S_PHY_PAGE_OFFSET
) & RTL8366S_PHY_PAGE_MASK
) |
508 (addr
& RTL8366S_PHY_REG_MASK
);
510 ret
= rtl8366_smi_write_reg(smi
, reg
, 0);
514 ret
= rtl8366_smi_read_reg(smi
, RTL8366S_PHY_ACCESS_DATA_REG
, data
);
521 static int rtl8366s_write_phy_reg(struct rtl8366s
*rtl
,
522 u32 phy_no
, u32 page
, u32 addr
, u32 data
)
524 struct rtl8366_smi
*smi
= &rtl
->smi
;
528 if (phy_no
> RTL8366S_PHY_NO_MAX
)
531 if (page
> RTL8366S_PHY_PAGE_MAX
)
534 if (addr
> RTL8366S_PHY_ADDR_MAX
)
537 ret
= rtl8366_smi_write_reg(smi
, RTL8366S_PHY_ACCESS_CTRL_REG
,
538 RTL8366S_PHY_CTRL_WRITE
);
542 reg
= 0x8000 | (1 << (phy_no
+ RTL8366S_PHY_NO_OFFSET
)) |
543 ((page
<< RTL8366S_PHY_PAGE_OFFSET
) & RTL8366S_PHY_PAGE_MASK
) |
544 (addr
& RTL8366S_PHY_REG_MASK
);
546 ret
= rtl8366_smi_write_reg(smi
, reg
, data
);
553 static int rtl8366_get_mib_counter(struct rtl8366s
*rtl
, int counter
,
554 int port
, unsigned long long *val
)
556 struct rtl8366_smi
*smi
= &rtl
->smi
;
559 u32 addr
, data
, regoffset
;
562 /* address offset to MIBs counter */
563 const u16 mibLength
[RTL8366S_MIB_COUNT
] = {4, 4, 2, 2, 2, 2, 2, 2, 2,
564 2, 2, 2, 2, 2, 2, 2, 2, 2,
565 2, 2, 4, 2, 2, 2, 2, 2, 2,
568 if (port
> RTL8366_NUM_PORTS
|| counter
>= RTL8366S_MIB_COUNT
)
571 regoffset
= RTL8366S_MIB_COUNTER_PORT_OFFSET
* (port
);
573 for (i
= 0; i
< counter
; i
++)
574 regoffset
+= mibLength
[i
];
576 addr
= RTL8366S_MIB_COUNTER_BASE
+ regoffset
;
579 * Writing access counter address first
580 * then ASIC will prepare 64bits counter wait for being retrived
582 data
= 0; /* writing data will be discard by ASIC */
583 err
= rtl8366_smi_write_reg(smi
, addr
, data
);
587 /* read MIB control register */
588 err
= rtl8366_smi_read_reg(smi
, RTL8366S_MIB_CTRL_REG
, &data
);
592 if (data
& RTL8366S_MIB_CTRL_BUSY_MASK
)
595 if (data
& RTL8366S_MIB_CTRL_RESET_MASK
)
599 addr
= addr
+ mibLength
[counter
] - 1;
600 i
= mibLength
[counter
];
603 err
= rtl8366_smi_read_reg(smi
, addr
, &data
);
607 mibvalue
= (mibvalue
<< 16) | (data
& 0xFFFF);
617 static int rtl8366s_get_vlan_4k_entry(struct rtl8366s
*rtl
, u32 vid
,
618 struct rtl8366s_vlan4kentry
*vlan4k
)
620 struct rtl8366_smi
*smi
= &rtl
->smi
;
625 memset(vlan4k
, '\0', sizeof(struct rtl8366s_vlan4kentry
));
628 if (vid
>= RTL8366_NUM_VIDS
)
631 tableaddr
= (u16
*)vlan4k
;
635 err
= rtl8366_smi_write_reg(smi
, RTL8366S_VLAN_TABLE_WRITE_BASE
, data
);
639 /* write table access control word */
640 err
= rtl8366_smi_write_reg(smi
, RTL8366S_TABLE_ACCESS_CTRL_REG
,
641 RTL8366S_TABLE_VLAN_READ_CTRL
);
645 err
= rtl8366_smi_read_reg(smi
, RTL8366S_VLAN_TABLE_READ_BASE
, &data
);
652 err
= rtl8366_smi_read_reg(smi
, RTL8366S_VLAN_TABLE_READ_BASE
+ 1,
663 static int rtl8366s_set_vlan_4k_entry(struct rtl8366s
*rtl
,
664 const struct rtl8366s_vlan4kentry
*vlan4k
)
666 struct rtl8366_smi
*smi
= &rtl
->smi
;
671 if (vlan4k
->vid
>= RTL8366_NUM_VIDS
||
672 vlan4k
->member
> RTL8366_PORT_ALL
||
673 vlan4k
->untag
> RTL8366_PORT_ALL
||
674 vlan4k
->fid
> RTL8366S_FIDMAX
)
677 tableaddr
= (u16
*)vlan4k
;
681 err
= rtl8366_smi_write_reg(smi
, RTL8366S_VLAN_TABLE_WRITE_BASE
, data
);
689 err
= rtl8366_smi_write_reg(smi
, RTL8366S_VLAN_TABLE_WRITE_BASE
+ 1,
694 /* write table access control word */
695 err
= rtl8366_smi_write_reg(smi
, RTL8366S_TABLE_ACCESS_CTRL_REG
,
696 RTL8366S_TABLE_VLAN_WRITE_CTRL
);
701 static int rtl8366s_get_vlan_member_config(struct rtl8366s
*rtl
, u32 index
,
702 struct rtl8366s_vlanconfig
*vlanmc
)
704 struct rtl8366_smi
*smi
= &rtl
->smi
;
710 memset(vlanmc
, '\0', sizeof(struct rtl8366s_vlanconfig
));
712 if (index
>= RTL8366_NUM_VLANS
)
715 tableaddr
= (u16
*)vlanmc
;
717 addr
= RTL8366S_VLAN_MEMCONF_BASE
+ (index
<< 1);
718 err
= rtl8366_smi_read_reg(smi
, addr
, &data
);
725 addr
= RTL8366S_VLAN_MEMCONF_BASE
+ 1 + (index
<< 1);
726 err
= rtl8366_smi_read_reg(smi
, addr
, &data
);
735 static int rtl8366s_set_vlan_member_config(struct rtl8366s
*rtl
, u32 index
,
736 const struct rtl8366s_vlanconfig
739 struct rtl8366_smi
*smi
= &rtl
->smi
;
745 if (index
>= RTL8366_NUM_VLANS
||
746 vlanmc
->vid
>= RTL8366_NUM_VIDS
||
747 vlanmc
->priority
> RTL8366S_PRIORITYMAX
||
748 vlanmc
->member
> RTL8366_PORT_ALL
||
749 vlanmc
->untag
> RTL8366_PORT_ALL
||
750 vlanmc
->fid
> RTL8366S_FIDMAX
)
753 addr
= RTL8366S_VLAN_MEMCONF_BASE
+ (index
<< 1);
755 tableaddr
= (u16
*)vlanmc
;
758 err
= rtl8366_smi_write_reg(smi
, addr
, data
);
762 addr
= RTL8366S_VLAN_MEMCONF_BASE
+ 1 + (index
<< 1);
767 err
= rtl8366_smi_write_reg(smi
, addr
, data
);
774 static int rtl8366s_get_port_vlan_index(struct rtl8366s
*rtl
, int port
,
777 struct rtl8366_smi
*smi
= &rtl
->smi
;
781 if (port
>= RTL8366_NUM_PORTS
)
784 err
= rtl8366_smi_read_reg(smi
, RTL8366S_PORT_VLAN_CTRL_REG(port
),
789 *val
= (data
>> RTL8366S_PORT_VLAN_CTRL_SHIFT(port
)) &
790 RTL8366S_PORT_VLAN_CTRL_MASK
;
796 static int rtl8366s_get_vlan_port_pvid(struct rtl8366s
*rtl
, int port
,
799 struct rtl8366s_vlanconfig vlanmc
;
803 err
= rtl8366s_get_port_vlan_index(rtl
, port
, &index
);
807 err
= rtl8366s_get_vlan_member_config(rtl
, index
, &vlanmc
);
815 static int rtl8366s_set_port_vlan_index(struct rtl8366s
*rtl
, int port
,
818 struct rtl8366_smi
*smi
= &rtl
->smi
;
822 if (port
>= RTL8366_NUM_PORTS
|| index
>= RTL8366_NUM_VLANS
)
825 err
= rtl8366_smi_read_reg(smi
, RTL8366S_PORT_VLAN_CTRL_REG(port
),
830 data
&= ~(RTL8366S_PORT_VLAN_CTRL_MASK
<<
831 RTL8366S_PORT_VLAN_CTRL_SHIFT(port
));
832 data
|= (index
& RTL8366S_PORT_VLAN_CTRL_MASK
) <<
833 RTL8366S_PORT_VLAN_CTRL_SHIFT(port
);
835 err
= rtl8366_smi_write_reg(smi
, RTL8366S_PORT_VLAN_CTRL_REG(port
),
840 static int rtl8366s_set_vlan_port_pvid(struct rtl8366s
*rtl
, int port
, int val
)
843 struct rtl8366s_vlanconfig vlanmc
;
844 struct rtl8366s_vlan4kentry vlan4k
;
846 if (port
>= RTL8366_NUM_PORTS
|| val
>= RTL8366_NUM_VIDS
)
849 /* Updating the 4K entry; lookup it and change the port member set */
850 rtl8366s_get_vlan_4k_entry(rtl
, val
, &vlan4k
);
851 vlan4k
.member
|= ((1 << port
) | RTL8366_PORT_CPU
);
852 vlan4k
.untag
= RTL8366_PORT_ALL_BUT_CPU
;
853 rtl8366s_set_vlan_4k_entry(rtl
, &vlan4k
);
856 * For the 16 entries more work needs to be done. First see if such
857 * VID is already there and change it
859 for (i
= 0; i
< RTL8366_NUM_VLANS
; ++i
) {
860 rtl8366s_get_vlan_member_config(rtl
, i
, &vlanmc
);
862 /* Try to find an existing vid and update port member set */
863 if (val
== vlanmc
.vid
) {
864 vlanmc
.member
|= ((1 << port
) | RTL8366_PORT_CPU
);
865 rtl8366s_set_vlan_member_config(rtl
, i
, &vlanmc
);
867 /* Now update PVID register settings */
868 rtl8366s_set_port_vlan_index(rtl
, port
, i
);
875 * PVID could not be found from vlan table. Replace unused (one that
876 * has no member ports) with new one
878 for (i
= 0; i
< RTL8366_NUM_VLANS
; ++i
) {
879 rtl8366s_get_vlan_member_config(rtl
, i
, &vlanmc
);
882 * See if this vlan member configuration is unused. It is
883 * unused if member set contains no ports or CPU port only
885 if (!vlanmc
.member
|| vlanmc
.member
== RTL8366_PORT_CPU
) {
888 vlanmc
.untag
= RTL8366_PORT_ALL_BUT_CPU
;
889 vlanmc
.member
= ((1 << port
) | RTL8366_PORT_CPU
);
892 rtl8366s_set_vlan_member_config(rtl
, i
, &vlanmc
);
894 /* Now update PVID register settings */
895 rtl8366s_set_port_vlan_index(rtl
, port
, i
);
902 "All 16 vlan member configurations are in use\n");
908 static int rtl8366s_vlan_set_vlan(struct rtl8366s
*rtl
, int enable
)
910 struct rtl8366_smi
*smi
= &rtl
->smi
;
913 rtl8366_smi_read_reg(smi
, RTL8366_CHIP_GLOBAL_CTRL_REG
, &data
);
916 data
|= RTL8366_CHIP_CTRL_VLAN
;
918 data
&= ~RTL8366_CHIP_CTRL_VLAN
;
920 return rtl8366_smi_write_reg(smi
, RTL8366_CHIP_GLOBAL_CTRL_REG
, data
);
923 static int rtl8366s_vlan_set_4ktable(struct rtl8366s
*rtl
, int enable
)
925 struct rtl8366_smi
*smi
= &rtl
->smi
;
928 rtl8366_smi_read_reg(smi
, RTL8366S_VLAN_TB_CTRL_REG
, &data
);
935 return rtl8366_smi_write_reg(smi
, RTL8366S_VLAN_TB_CTRL_REG
, data
);
938 static int rtl8366s_reset_vlan(struct rtl8366s
*rtl
)
940 struct rtl8366s_vlan4kentry vlan4k
;
941 struct rtl8366s_vlanconfig vlanmc
;
945 /* clear 16 VLAN member configuration */
951 for (i
= 0; i
< RTL8366_NUM_VLANS
; i
++) {
952 err
= rtl8366s_set_vlan_member_config(rtl
, i
, &vlanmc
);
957 /* Set a default VLAN with vid 1 to 4K table for all ports */
959 vlan4k
.member
= RTL8366_PORT_ALL
;
960 vlan4k
.untag
= RTL8366_PORT_ALL
;
962 err
= rtl8366s_set_vlan_4k_entry(rtl
, &vlan4k
);
966 /* Set all ports PVID to default VLAN */
967 for (i
= 0; i
< RTL8366_NUM_PORTS
; i
++) {
968 err
= rtl8366s_set_vlan_port_pvid(rtl
, i
, 0);
976 #ifdef CONFIG_RTL8366_SMI_DEBUG_FS
977 static int rtl8366s_debugfs_open(struct inode
*inode
, struct file
*file
)
979 file
->private_data
= inode
->i_private
;
983 static ssize_t
rtl8366s_read_debugfs_mibs(struct file
*file
,
984 char __user
*user_buf
,
985 size_t count
, loff_t
*ppos
)
987 struct rtl8366s
*rtl
= (struct rtl8366s
*)file
->private_data
;
989 char *buf
= rtl
->buf
;
991 len
+= snprintf(buf
+ len
, sizeof(rtl
->buf
) - len
, "MIB Counters:\n");
992 len
+= snprintf(buf
+ len
, sizeof(rtl
->buf
) - len
, "Counter"
994 "Port 0 \t\t Port 1 \t\t Port 2 \t\t Port 3 \t\t "
997 for (i
= 0; i
< 33; ++i
) {
998 len
+= snprintf(buf
+ len
, sizeof(rtl
->buf
) - len
, "%d:%s ",
1000 for (j
= 0; j
< RTL8366_NUM_PORTS
; ++j
) {
1001 unsigned long long counter
= 0;
1003 if (!rtl8366_get_mib_counter(rtl
, i
, j
, &counter
))
1004 len
+= snprintf(buf
+ len
,
1005 sizeof(rtl
->buf
) - len
,
1008 len
+= snprintf(buf
+ len
,
1009 sizeof(rtl
->buf
) - len
,
1012 if (j
!= RTL8366_NUM_PORTS
- 1) {
1013 if (counter
< 100000)
1014 len
+= snprintf(buf
+ len
,
1015 sizeof(rtl
->buf
) - len
,
1018 len
+= snprintf(buf
+ len
,
1019 sizeof(rtl
->buf
) - len
,
1023 len
+= snprintf(buf
+ len
, sizeof(rtl
->buf
) - len
, "\n");
1026 len
+= snprintf(buf
+ len
, sizeof(rtl
->buf
) - len
, "\n");
1028 return simple_read_from_buffer(user_buf
, count
, ppos
, buf
, len
);
1031 static ssize_t
rtl8366s_read_debugfs_vlan(struct file
*file
,
1032 char __user
*user_buf
,
1033 size_t count
, loff_t
*ppos
)
1035 struct rtl8366s
*rtl
= (struct rtl8366s
*)file
->private_data
;
1037 char *buf
= rtl
->buf
;
1039 len
+= snprintf(buf
+ len
, sizeof(rtl
->buf
) - len
,
1040 "VLAN Member Config:\n");
1041 len
+= snprintf(buf
+ len
, sizeof(rtl
->buf
) - len
,
1042 "\t id \t vid \t prio \t member \t untag \t fid "
1045 for (i
= 0; i
< RTL8366_NUM_VLANS
; ++i
) {
1046 struct rtl8366s_vlanconfig vlanmc
;
1048 rtl8366s_get_vlan_member_config(rtl
, i
, &vlanmc
);
1050 len
+= snprintf(buf
+ len
, sizeof(rtl
->buf
) - len
,
1051 "\t[%d] \t %d \t %d \t 0x%04x \t 0x%04x \t %d "
1052 "\t", i
, vlanmc
.vid
, vlanmc
.priority
,
1053 vlanmc
.member
, vlanmc
.untag
, vlanmc
.fid
);
1055 for (j
= 0; j
< RTL8366_NUM_PORTS
; ++j
) {
1057 if (!rtl8366s_get_port_vlan_index(rtl
, j
, &index
)) {
1059 len
+= snprintf(buf
+ len
,
1060 sizeof(rtl
->buf
) - len
,
1064 len
+= snprintf(buf
+ len
, sizeof(rtl
->buf
) - len
, "\n");
1067 return simple_read_from_buffer(user_buf
, count
, ppos
, buf
, len
);
1070 static ssize_t
rtl8366s_read_debugfs_reg(struct file
*file
,
1071 char __user
*user_buf
,
1072 size_t count
, loff_t
*ppos
)
1074 struct rtl8366s
*rtl
= (struct rtl8366s
*)file
->private_data
;
1075 struct rtl8366_smi
*smi
= &rtl
->smi
;
1076 u32 t
, reg
= g_dbg_reg
;
1078 char *buf
= rtl
->buf
;
1080 memset(buf
, '\0', sizeof(rtl
->buf
));
1082 err
= rtl8366_smi_read_reg(smi
, reg
, &t
);
1084 len
+= snprintf(buf
, sizeof(rtl
->buf
),
1085 "Read failed (reg: 0x%04x)\n", reg
);
1086 return simple_read_from_buffer(user_buf
, count
, ppos
, buf
, len
);
1089 len
+= snprintf(buf
, sizeof(rtl
->buf
), "reg = 0x%04x, val = 0x%04x\n",
1092 return simple_read_from_buffer(user_buf
, count
, ppos
, buf
, len
);
1095 static ssize_t
rtl8366s_write_debugfs_reg(struct file
*file
,
1096 const char __user
*user_buf
,
1097 size_t count
, loff_t
*ppos
)
1099 struct rtl8366s
*rtl
= (struct rtl8366s
*)file
->private_data
;
1100 struct rtl8366_smi
*smi
= &rtl
->smi
;
1102 u32 reg
= g_dbg_reg
;
1105 char *buf
= rtl
->buf
;
1107 len
= min(count
, sizeof(rtl
->buf
) - 1);
1108 if (copy_from_user(buf
, user_buf
, len
)) {
1109 dev_err(rtl
->parent
, "copy from user failed\n");
1114 if (len
> 0 && buf
[len
- 1] == '\n')
1115 buf
[len
- 1] = '\0';
1118 if (strict_strtoul(buf
, 16, &data
)) {
1119 dev_err(rtl
->parent
, "Invalid reg value %s\n", buf
);
1121 err
= rtl8366_smi_write_reg(smi
, reg
, data
);
1123 dev_err(rtl
->parent
,
1124 "writing reg 0x%04x val 0x%04lx failed\n",
1132 static const struct file_operations fops_rtl8366s_regs
= {
1133 .read
= rtl8366s_read_debugfs_reg
,
1134 .write
= rtl8366s_write_debugfs_reg
,
1135 .open
= rtl8366s_debugfs_open
,
1136 .owner
= THIS_MODULE
1139 static const struct file_operations fops_rtl8366s_vlan
= {
1140 .read
= rtl8366s_read_debugfs_vlan
,
1141 .open
= rtl8366s_debugfs_open
,
1142 .owner
= THIS_MODULE
1145 static const struct file_operations fops_rtl8366s_mibs
= {
1146 .read
= rtl8366s_read_debugfs_mibs
,
1147 .open
= rtl8366s_debugfs_open
,
1148 .owner
= THIS_MODULE
1151 static void rtl8366s_debugfs_init(struct rtl8366s
*rtl
)
1153 struct dentry
*node
;
1154 struct dentry
*root
;
1156 if (!rtl
->debugfs_root
)
1157 rtl
->debugfs_root
= debugfs_create_dir("rtl8366s", NULL
);
1159 if (!rtl
->debugfs_root
) {
1160 dev_err(rtl
->parent
, "Unable to create debugfs dir\n");
1163 root
= rtl
->debugfs_root
;
1165 node
= debugfs_create_x16("reg", S_IRUGO
| S_IWUSR
, root
, &g_dbg_reg
);
1167 dev_err(rtl
->parent
, "Creating debugfs file reg failed\n");
1171 node
= debugfs_create_file("val", S_IRUGO
| S_IWUSR
, root
, rtl
,
1172 &fops_rtl8366s_regs
);
1174 dev_err(rtl
->parent
, "Creating debugfs file val failed\n");
1178 node
= debugfs_create_file("vlan", S_IRUSR
, root
, rtl
,
1179 &fops_rtl8366s_vlan
);
1181 dev_err(rtl
->parent
,
1182 "Creating debugfs file vlan failed\n");
1186 node
= debugfs_create_file("mibs", S_IRUSR
, root
, rtl
,
1187 &fops_rtl8366s_mibs
);
1189 dev_err(rtl
->parent
,
1190 "Creating debugfs file mibs failed\n");
1195 static void rtl8366s_debugfs_remove(struct rtl8366s
*rtl
)
1197 if (rtl
->debugfs_root
) {
1198 debugfs_remove_recursive(rtl
->debugfs_root
);
1199 rtl
->debugfs_root
= NULL
;
1204 static inline void rtl8366s_debugfs_init(struct rtl8366s
*rtl
) {}
1205 static inline void rtl8366s_debugfs_remove(struct rtl8366s
*rtl
) {}
1206 #endif /* CONFIG_RTL8366_SMI_DEBUG_FS */
1208 static int rtl8366s_sw_reset_mibs(struct switch_dev
*dev
,
1209 const struct switch_attr
*attr
,
1210 struct switch_val
*val
)
1212 struct rtl8366s
*rtl
= sw_to_rtl8366s(dev
);
1213 struct rtl8366_smi
*smi
= &rtl
->smi
;
1216 if (val
->value
.i
== 1) {
1217 rtl8366_smi_read_reg(smi
, RTL8366S_MIB_CTRL_REG
, &data
);
1219 rtl8366_smi_write_reg(smi
, RTL8366S_MIB_CTRL_REG
, data
);
1225 static int rtl8366s_sw_get_vlan_enable(struct switch_dev
*dev
,
1226 const struct switch_attr
*attr
,
1227 struct switch_val
*val
)
1229 struct rtl8366s
*rtl
= sw_to_rtl8366s(dev
);
1230 struct rtl8366_smi
*smi
= &rtl
->smi
;
1233 if (attr
->ofs
== 1) {
1234 rtl8366_smi_read_reg(smi
, RTL8366_CHIP_GLOBAL_CTRL_REG
, &data
);
1236 if (data
& RTL8366_CHIP_CTRL_VLAN
)
1240 } else if (attr
->ofs
== 2) {
1241 rtl8366_smi_read_reg(smi
, RTL8366S_VLAN_TB_CTRL_REG
, &data
);
1252 static int rtl8366s_sw_get_blinkrate(struct switch_dev
*dev
,
1253 const struct switch_attr
*attr
,
1254 struct switch_val
*val
)
1256 struct rtl8366s
*rtl
= sw_to_rtl8366s(dev
);
1257 struct rtl8366_smi
*smi
= &rtl
->smi
;
1260 rtl8366_smi_read_reg(smi
, RTL8366_LED_BLINKRATE_REG
, &data
);
1262 val
->value
.i
= (data
& (RTL8366_LED_BLINKRATE_MASK
));
1267 static int rtl8366s_sw_set_blinkrate(struct switch_dev
*dev
,
1268 const struct switch_attr
*attr
,
1269 struct switch_val
*val
)
1271 struct rtl8366s
*rtl
= sw_to_rtl8366s(dev
);
1272 struct rtl8366_smi
*smi
= &rtl
->smi
;
1275 if (val
->value
.i
>= 6)
1278 rtl8366_smi_read_reg(smi
, RTL8366_LED_BLINKRATE_REG
, &data
);
1280 data
&= ~RTL8366_LED_BLINKRATE_MASK
;
1281 data
|= val
->value
.i
;
1283 rtl8366_smi_write_reg(smi
, RTL8366_LED_BLINKRATE_REG
, data
);
1288 static int rtl8366s_sw_set_vlan_enable(struct switch_dev
*dev
,
1289 const struct switch_attr
*attr
,
1290 struct switch_val
*val
)
1292 struct rtl8366s
*rtl
= sw_to_rtl8366s(dev
);
1295 return rtl8366s_vlan_set_vlan(rtl
, val
->value
.i
);
1297 return rtl8366s_vlan_set_4ktable(rtl
, val
->value
.i
);
1300 static const char *rtl8366s_speed_str(unsigned speed
)
1314 static int rtl8366s_sw_get_port_link(struct switch_dev
*dev
,
1315 const struct switch_attr
*attr
,
1316 struct switch_val
*val
)
1318 struct rtl8366s
*rtl
= sw_to_rtl8366s(dev
);
1319 struct rtl8366_smi
*smi
= &rtl
->smi
;
1320 u32 len
= 0, data
= 0;
1322 if (val
->port_vlan
>= RTL8366_NUM_PORTS
)
1325 memset(rtl
->buf
, '\0', sizeof(rtl
->buf
));
1326 rtl8366_smi_read_reg(smi
, RTL8366S_PORT_LINK_STATUS_BASE
+
1327 (val
->port_vlan
/ 2), &data
);
1329 if (val
->port_vlan
% 2)
1332 len
= snprintf(rtl
->buf
, sizeof(rtl
->buf
),
1333 "port:%d link:%s speed:%s %s-duplex %s%s%s",
1335 (data
& RTL8366S_PORT_STATUS_LINK_MASK
) ? "up" : "down",
1336 rtl8366s_speed_str(data
&
1337 RTL8366S_PORT_STATUS_SPEED_MASK
),
1338 (data
& RTL8366S_PORT_STATUS_DUPLEX_MASK
) ?
1340 (data
& RTL8366S_PORT_STATUS_TXPAUSE_MASK
) ?
1342 (data
& RTL8366S_PORT_STATUS_RXPAUSE_MASK
) ?
1344 (data
& RTL8366S_PORT_STATUS_AN_MASK
) ? "nway ": "");
1346 val
->value
.s
= rtl
->buf
;
1352 static int rtl8366s_sw_get_vlan_info(struct switch_dev
*dev
,
1353 const struct switch_attr
*attr
,
1354 struct switch_val
*val
)
1358 struct rtl8366s_vlanconfig vlanmc
;
1359 struct rtl8366s_vlan4kentry vlan4k
;
1360 struct rtl8366s
*rtl
= sw_to_rtl8366s(dev
);
1361 char *buf
= rtl
->buf
;
1363 if (val
->port_vlan
>= RTL8366_NUM_VLANS
)
1366 memset(buf
, '\0', sizeof(rtl
->buf
));
1368 rtl8366s_get_vlan_member_config(rtl
, val
->port_vlan
, &vlanmc
);
1369 rtl8366s_get_vlan_4k_entry(rtl
, vlanmc
.vid
, &vlan4k
);
1371 len
+= snprintf(buf
+ len
, sizeof(rtl
->buf
) - len
, "VLAN %d: Ports: ",
1374 for (i
= 0; i
< RTL8366_NUM_PORTS
; ++i
) {
1376 if (!rtl8366s_get_port_vlan_index(rtl
, i
, &index
) &&
1377 index
== val
->port_vlan
)
1378 len
+= snprintf(buf
+ len
, sizeof(rtl
->buf
) - len
,
1381 len
+= snprintf(buf
+ len
, sizeof(rtl
->buf
) - len
, "\n");
1383 len
+= snprintf(buf
+ len
, sizeof(rtl
->buf
) - len
,
1384 "\t\t vid \t prio \t member \t untag \t fid\n");
1385 len
+= snprintf(buf
+ len
, sizeof(rtl
->buf
) - len
, "\tMC:\t");
1386 len
+= snprintf(buf
+ len
, sizeof(rtl
->buf
) - len
,
1387 "%d \t %d \t 0x%04x \t 0x%04x \t %d\n",
1388 vlanmc
.vid
, vlanmc
.priority
, vlanmc
.member
,
1389 vlanmc
.untag
, vlanmc
.fid
);
1390 len
+= snprintf(buf
+ len
, sizeof(rtl
->buf
) - len
, "\t4K:\t");
1391 len
+= snprintf(buf
+ len
, sizeof(rtl
->buf
) - len
,
1392 "%d \t \t 0x%04x \t 0x%04x \t %d",
1393 vlan4k
.vid
, vlan4k
.member
, vlan4k
.untag
, vlan4k
.fid
);
1401 static int rtl8366s_sw_set_port_led(struct switch_dev
*dev
,
1402 const struct switch_attr
*attr
,
1403 struct switch_val
*val
)
1405 struct rtl8366s
*rtl
= sw_to_rtl8366s(dev
);
1406 struct rtl8366_smi
*smi
= &rtl
->smi
;
1409 if (val
->port_vlan
>= RTL8366_NUM_PORTS
||
1410 (1 << val
->port_vlan
) == RTL8366_PORT_UNKNOWN
)
1413 if (val
->port_vlan
== RTL8366_PORT_NUM_CPU
) {
1414 rtl8366_smi_read_reg(smi
, RTL8366_LED_BLINKRATE_REG
, &data
);
1415 data
= (data
& (~(0xF << 4))) | (val
->value
.i
<< 4);
1416 rtl8366_smi_write_reg(smi
, RTL8366_LED_BLINKRATE_REG
, data
);
1418 rtl8366_smi_read_reg(smi
, RTL8366_LED_CTRL_REG
, &data
);
1419 data
= (data
& (~(0xF << (val
->port_vlan
* 4)))) |
1420 (val
->value
.i
<< (val
->port_vlan
* 4));
1421 rtl8366_smi_write_reg(smi
, RTL8366_LED_CTRL_REG
, data
);
1427 static int rtl8366s_sw_get_port_led(struct switch_dev
*dev
,
1428 const struct switch_attr
*attr
,
1429 struct switch_val
*val
)
1431 struct rtl8366s
*rtl
= sw_to_rtl8366s(dev
);
1432 struct rtl8366_smi
*smi
= &rtl
->smi
;
1435 if (val
->port_vlan
>= RTL8366_NUM_LEDGROUPS
)
1438 rtl8366_smi_read_reg(smi
, RTL8366_LED_CTRL_REG
, &data
);
1439 val
->value
.i
= (data
>> (val
->port_vlan
* 4)) & 0x000F;
1444 static int rtl8366s_sw_reset_port_mibs(struct switch_dev
*dev
,
1445 const struct switch_attr
*attr
,
1446 struct switch_val
*val
)
1448 struct rtl8366s
*rtl
= sw_to_rtl8366s(dev
);
1449 struct rtl8366_smi
*smi
= &rtl
->smi
;
1452 if (val
->port_vlan
>= RTL8366_NUM_PORTS
)
1455 rtl8366_smi_read_reg(smi
, RTL8366S_MIB_CTRL_REG
, &data
);
1456 data
|= (1 << (val
->port_vlan
+ 3));
1457 rtl8366_smi_write_reg(smi
, RTL8366S_MIB_CTRL_REG
, data
);
1462 static int rtl8366s_sw_get_port_mib(struct switch_dev
*dev
,
1463 const struct switch_attr
*attr
,
1464 struct switch_val
*val
)
1466 struct rtl8366s
*rtl
= sw_to_rtl8366s(dev
);
1468 unsigned long long counter
= 0;
1469 char *buf
= rtl
->buf
;
1471 if (val
->port_vlan
>= RTL8366_NUM_PORTS
)
1474 len
+= snprintf(buf
+ len
, sizeof(rtl
->buf
) - len
,
1475 "Port %d MIB counters\n",
1478 for (i
= 0; i
< RTL8366S_MIB_COUNT
; ++i
) {
1479 len
+= snprintf(buf
+ len
, sizeof(rtl
->buf
) - len
,
1480 "%d:%s\t", i
, MIBCOUNTERS
[i
]);
1481 if (!rtl8366_get_mib_counter(rtl
, i
, val
->port_vlan
, &counter
))
1482 len
+= snprintf(buf
+ len
, sizeof(rtl
->buf
) - len
,
1483 "[%llu]\n", counter
);
1485 len
+= snprintf(buf
+ len
, sizeof(rtl
->buf
) - len
,
1494 static int rtl8366s_sw_get_vlan_ports(struct switch_dev
*dev
,
1495 struct switch_val
*val
)
1497 struct rtl8366s_vlanconfig vlanmc
;
1498 struct rtl8366s
*rtl
= sw_to_rtl8366s(dev
);
1499 struct switch_port
*port
;
1502 if (val
->port_vlan
>= RTL8366_NUM_VLANS
)
1505 rtl8366s_get_vlan_member_config(rtl
, val
->port_vlan
, &vlanmc
);
1507 port
= &val
->value
.ports
[0];
1509 for (i
= 0; i
< RTL8366_NUM_PORTS
; i
++) {
1510 if (!(vlanmc
.member
& BIT(i
)))
1514 port
->flags
= (vlanmc
.untag
& BIT(i
)) ?
1515 0 : BIT(SWITCH_PORT_FLAG_TAGGED
);
1522 static int rtl8366s_sw_set_vlan_ports(struct switch_dev
*dev
,
1523 struct switch_val
*val
)
1525 struct rtl8366s_vlanconfig vlanmc
;
1526 struct rtl8366s_vlan4kentry vlan4k
;
1527 struct rtl8366s
*rtl
= sw_to_rtl8366s(dev
);
1528 struct switch_port
*port
;
1531 if (val
->port_vlan
>= RTL8366_NUM_VLANS
)
1534 rtl8366s_get_vlan_member_config(rtl
, val
->port_vlan
, &vlanmc
);
1535 rtl8366s_get_vlan_4k_entry(rtl
, vlanmc
.vid
, &vlan4k
);
1540 port
= &val
->value
.ports
[0];
1541 for (i
= 0; i
< val
->len
; i
++, port
++) {
1542 vlanmc
.member
|= BIT(port
->id
);
1544 if (!(port
->flags
& BIT(SWITCH_PORT_FLAG_TAGGED
)))
1545 vlanmc
.untag
|= BIT(port
->id
);
1548 vlan4k
.member
= vlanmc
.member
;
1549 vlan4k
.untag
= vlanmc
.untag
;
1551 rtl8366s_set_vlan_member_config(rtl
, val
->port_vlan
, &vlanmc
);
1552 rtl8366s_set_vlan_4k_entry(rtl
, &vlan4k
);
1556 static int rtl8366s_sw_get_port_pvid(struct switch_dev
*dev
, int port
, int *val
)
1558 struct rtl8366s
*rtl
= sw_to_rtl8366s(dev
);
1559 return rtl8366s_get_vlan_port_pvid(rtl
, port
, val
);
1562 static int rtl8366s_sw_set_port_pvid(struct switch_dev
*dev
, int port
, int val
)
1564 struct rtl8366s
*rtl
= sw_to_rtl8366s(dev
);
1565 return rtl8366s_set_vlan_port_pvid(rtl
, port
, val
);
1568 static int rtl8366s_sw_reset_switch(struct switch_dev
*dev
)
1570 struct rtl8366s
*rtl
= sw_to_rtl8366s(dev
);
1571 struct rtl8366_smi
*smi
= &rtl
->smi
;
1575 rtl8366_smi_write_reg(smi
, RTL8366_RESET_CTRL_REG
,
1576 RTL8366_CHIP_CTRL_RESET_HW
);
1579 if (rtl8366_smi_read_reg(smi
, RTL8366_RESET_CTRL_REG
, &data
))
1582 if (!(data
& RTL8366_CHIP_CTRL_RESET_HW
))
1584 } while (--timeout
);
1587 printk("Timeout waiting for the switch to reset\n");
1591 return rtl8366s_reset_vlan(rtl
);
1594 static struct switch_attr rtl8366s_globals
[] = {
1596 .type
= SWITCH_TYPE_INT
,
1597 .name
= "enable_vlan",
1598 .description
= "Enable VLAN mode",
1599 .set
= rtl8366s_sw_set_vlan_enable
,
1600 .get
= rtl8366s_sw_get_vlan_enable
,
1604 .type
= SWITCH_TYPE_INT
,
1605 .name
= "enable_vlan4k",
1606 .description
= "Enable VLAN 4K mode",
1607 .set
= rtl8366s_sw_set_vlan_enable
,
1608 .get
= rtl8366s_sw_get_vlan_enable
,
1612 .type
= SWITCH_TYPE_INT
,
1613 .name
= "reset_mibs",
1614 .description
= "Reset all MIB counters",
1615 .set
= rtl8366s_sw_reset_mibs
,
1619 .type
= SWITCH_TYPE_INT
,
1620 .name
= "blinkrate",
1621 .description
= "Get/Set LED blinking rate (0 = 43ms, 1 = 84ms,"
1622 " 2 = 120ms, 3 = 170ms, 4 = 340ms, 5 = 670ms)",
1623 .set
= rtl8366s_sw_set_blinkrate
,
1624 .get
= rtl8366s_sw_get_blinkrate
,
1629 static struct switch_attr rtl8366s_port
[] = {
1631 .type
= SWITCH_TYPE_STRING
,
1633 .description
= "Get port link information",
1636 .get
= rtl8366s_sw_get_port_link
,
1638 .type
= SWITCH_TYPE_INT
,
1639 .name
= "reset_mib",
1640 .description
= "Reset single port MIB counters",
1642 .set
= rtl8366s_sw_reset_port_mibs
,
1645 .type
= SWITCH_TYPE_STRING
,
1647 .description
= "Get MIB counters for port",
1650 .get
= rtl8366s_sw_get_port_mib
,
1652 .type
= SWITCH_TYPE_INT
,
1654 .description
= "Get/Set port group (0 - 3) led mode (0 - 15)",
1656 .set
= rtl8366s_sw_set_port_led
,
1657 .get
= rtl8366s_sw_get_port_led
,
1661 static struct switch_attr rtl8366s_vlan
[] = {
1663 .type
= SWITCH_TYPE_STRING
,
1665 .description
= "Get vlan information",
1668 .get
= rtl8366s_sw_get_vlan_info
,
1674 static struct switch_dev rtl8366_switch_dev
= {
1676 .cpu_port
= RTL8366_PORT_NUM_CPU
,
1677 .ports
= RTL8366_NUM_PORTS
,
1678 .vlans
= RTL8366_NUM_VLANS
,
1680 .attr
= rtl8366s_globals
,
1681 .n_attr
= ARRAY_SIZE(rtl8366s_globals
),
1684 .attr
= rtl8366s_port
,
1685 .n_attr
= ARRAY_SIZE(rtl8366s_port
),
1688 .attr
= rtl8366s_vlan
,
1689 .n_attr
= ARRAY_SIZE(rtl8366s_vlan
),
1692 .get_vlan_ports
= rtl8366s_sw_get_vlan_ports
,
1693 .set_vlan_ports
= rtl8366s_sw_set_vlan_ports
,
1694 .get_port_pvid
= rtl8366s_sw_get_port_pvid
,
1695 .set_port_pvid
= rtl8366s_sw_set_port_pvid
,
1696 .reset_switch
= rtl8366s_sw_reset_switch
,
1699 static int rtl8366s_switch_init(struct rtl8366s
*rtl
)
1701 struct switch_dev
*dev
= &rtl
->dev
;
1704 memcpy(dev
, &rtl8366_switch_dev
, sizeof(struct switch_dev
));
1706 dev
->devname
= dev_name(rtl
->parent
);
1708 err
= register_switch(dev
, NULL
);
1710 dev_err(rtl
->parent
, "switch registration failed\n");
1715 static void rtl8366s_switch_cleanup(struct rtl8366s
*rtl
)
1717 unregister_switch(&rtl
->dev
);
1720 static int rtl8366s_mii_read(struct mii_bus
*bus
, int addr
, int reg
)
1722 struct rtl8366s
*rtl
= bus
->priv
;
1726 err
= rtl8366s_read_phy_reg(rtl
, addr
, 0, reg
, &val
);
1733 static int rtl8366s_mii_write(struct mii_bus
*bus
, int addr
, int reg
, u16 val
)
1735 struct rtl8366s
*rtl
= bus
->priv
;
1739 err
= rtl8366s_write_phy_reg(rtl
, addr
, 0, reg
, val
);
1741 (void) rtl8366s_read_phy_reg(rtl
, addr
, 0, reg
, &t
);
1746 static int rtl8366s_mii_init(struct rtl8366s
*rtl
)
1751 rtl
->mii_bus
= mdiobus_alloc();
1752 if (rtl
->mii_bus
== NULL
) {
1757 rtl
->mii_bus
->priv
= (void *) rtl
;
1758 rtl
->mii_bus
->name
= "rtl8366-rtl";
1759 rtl
->mii_bus
->read
= rtl8366s_mii_read
;
1760 rtl
->mii_bus
->write
= rtl8366s_mii_write
;
1761 snprintf(rtl
->mii_bus
->id
, MII_BUS_ID_SIZE
, "%s",
1762 dev_name(rtl
->parent
));
1763 rtl
->mii_bus
->parent
= rtl
->parent
;
1764 rtl
->mii_bus
->phy_mask
= ~(0x1f);
1765 rtl
->mii_bus
->irq
= rtl
->mii_irq
;
1766 for (i
= 0; i
< PHY_MAX_ADDR
; i
++)
1767 rtl
->mii_irq
[i
] = PHY_POLL
;
1769 ret
= mdiobus_register(rtl
->mii_bus
);
1776 mdiobus_free(rtl
->mii_bus
);
1781 static void rtl8366s_mii_cleanup(struct rtl8366s
*rtl
)
1783 mdiobus_unregister(rtl
->mii_bus
);
1784 mdiobus_free(rtl
->mii_bus
);
1787 static int rtl8366s_mii_bus_match(struct mii_bus
*bus
)
1789 return (bus
->read
== rtl8366s_mii_read
&&
1790 bus
->write
== rtl8366s_mii_write
);
1793 static int rtl8366s_setup(struct rtl8366s
*rtl
)
1795 struct rtl8366_smi
*smi
= &rtl
->smi
;
1800 ret
= rtl8366_smi_read_reg(smi
, RTL8366S_CHIP_ID_REG
, &chip_id
);
1802 dev_err(rtl
->parent
, "unable to read chip id\n");
1807 case RTL8366S_CHIP_ID_8366
:
1810 dev_err(rtl
->parent
, "unknown chip id (%04x)\n", chip_id
);
1814 ret
= rtl8366_smi_read_reg(smi
, RTL8366S_CHIP_VERSION_CTRL_REG
,
1817 dev_err(rtl
->parent
, "unable to read chip version\n");
1821 dev_info(rtl
->parent
, "RTL%04x ver. %u chip found\n",
1822 chip_id
, chip_ver
& RTL8366S_CHIP_VERSION_MASK
);
1824 rtl8366s_debugfs_init(rtl
);
1829 static int __init
rtl8366_smi_init(struct rtl8366_smi
*smi
)
1836 err
= gpio_request(smi
->gpio_sda
, dev_name(smi
->parent
));
1838 dev_err(smi
->parent
, "gpio_request failed for %u, err=%d\n",
1839 smi
->gpio_sda
, err
);
1843 err
= gpio_request(smi
->gpio_sck
, dev_name(smi
->parent
));
1845 dev_err(smi
->parent
, "gpio_request failed for %u, err=%d\n",
1846 smi
->gpio_sck
, err
);
1850 spin_lock_init(&smi
->lock
);
1852 dev_info(smi
->parent
, "using GPIO pins %u (SDA) and %u (SCK)\n",
1853 smi
->gpio_sda
, smi
->gpio_sck
);
1858 gpio_free(smi
->gpio_sda
);
1863 static void rtl8366_smi_cleanup(struct rtl8366_smi
*smi
)
1865 gpio_free(smi
->gpio_sck
);
1866 gpio_free(smi
->gpio_sda
);
1869 static int __init
rtl8366s_probe(struct platform_device
*pdev
)
1871 static int rtl8366_smi_version_printed
;
1872 struct rtl8366_smi_platform_data
*pdata
;
1873 struct rtl8366s
*rtl
;
1874 struct rtl8366_smi
*smi
;
1877 if (!rtl8366_smi_version_printed
++)
1878 printk(KERN_NOTICE RTL8366S_DRIVER_DESC
1879 " version " RTL8366S_DRIVER_VER
"\n");
1881 pdata
= pdev
->dev
.platform_data
;
1883 dev_err(&pdev
->dev
, "no platform data specified\n");
1888 rtl
= kzalloc(sizeof(*rtl
), GFP_KERNEL
);
1890 dev_err(&pdev
->dev
, "no memory for private data\n");
1895 rtl
->parent
= &pdev
->dev
;
1898 smi
->parent
= &pdev
->dev
;
1899 smi
->gpio_sda
= pdata
->gpio_sda
;
1900 smi
->gpio_sck
= pdata
->gpio_sck
;
1902 err
= rtl8366_smi_init(smi
);
1906 platform_set_drvdata(pdev
, rtl
);
1908 err
= rtl8366s_setup(rtl
);
1910 goto err_clear_drvdata
;
1912 err
= rtl8366s_mii_init(rtl
);
1914 goto err_clear_drvdata
;
1916 err
= rtl8366s_switch_init(rtl
);
1918 goto err_mii_cleanup
;
1923 rtl8366s_mii_cleanup(rtl
);
1925 platform_set_drvdata(pdev
, NULL
);
1926 rtl8366_smi_cleanup(smi
);
1933 static int rtl8366s_phy_config_init(struct phy_device
*phydev
)
1935 if (!rtl8366s_mii_bus_match(phydev
->bus
))
1941 static int rtl8366s_phy_config_aneg(struct phy_device
*phydev
)
1946 static struct phy_driver rtl8366s_phy_driver
= {
1947 .phy_id
= 0x001cc960,
1948 .name
= "Realtek RTL8366",
1949 .phy_id_mask
= 0x1ffffff0,
1950 .features
= PHY_GBIT_FEATURES
,
1951 .config_aneg
= rtl8366s_phy_config_aneg
,
1952 .config_init
= rtl8366s_phy_config_init
,
1953 .read_status
= genphy_read_status
,
1955 .owner
= THIS_MODULE
,
1959 static int __devexit
rtl8366s_remove(struct platform_device
*pdev
)
1961 struct rtl8366s
*rtl
= platform_get_drvdata(pdev
);
1964 rtl8366s_switch_cleanup(rtl
);
1965 rtl8366s_debugfs_remove(rtl
);
1966 rtl8366s_mii_cleanup(rtl
);
1967 platform_set_drvdata(pdev
, NULL
);
1968 rtl8366_smi_cleanup(&rtl
->smi
);
1975 static struct platform_driver rtl8366s_driver
= {
1977 .name
= RTL8366S_DRIVER_NAME
,
1978 .owner
= THIS_MODULE
,
1980 .probe
= rtl8366s_probe
,
1981 .remove
= __devexit_p(rtl8366s_remove
),
1984 static int __init
rtl8366s_module_init(void)
1987 ret
= platform_driver_register(&rtl8366s_driver
);
1991 ret
= phy_driver_register(&rtl8366s_phy_driver
);
1993 goto err_platform_unregister
;
1997 err_platform_unregister
:
1998 platform_driver_unregister(&rtl8366s_driver
);
2001 module_init(rtl8366s_module_init
);
2003 static void __exit
rtl8366s_module_exit(void)
2005 phy_driver_unregister(&rtl8366s_phy_driver
);
2006 platform_driver_unregister(&rtl8366s_driver
);
2008 module_exit(rtl8366s_module_exit
);
2010 MODULE_DESCRIPTION(RTL8366S_DRIVER_DESC
);
2011 MODULE_VERSION(RTL8366S_DRIVER_VER
);
2012 MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
2013 MODULE_AUTHOR("Antti Seppälä <a.seppala@gmail.com>");
2014 MODULE_LICENSE("GPL v2");
2015 MODULE_ALIAS("platform:" RTL8366S_DRIVER_NAME
);