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 RTL8366S_DRIVER_DESC "Realtek RTL8366RB ethernet switch driver"
28 #define RTL8366S_DRIVER_VER "0.2.2"
30 #define RTL8366S_PHY_NO_MAX 4
31 #define RTL8366S_PHY_PAGE_MAX 7
32 #define RTL8366S_PHY_ADDR_MAX 31
34 #define RTL8366_CHIP_GLOBAL_CTRL_REG 0x0000
35 #define RTL8366_CHIP_CTRL_VLAN (1 << 13)
36 #define RTL8366_CHIP_CTRL_VLAN_4KTB (1 << 14)
38 /* Switch Global Configuration register */
39 #define RTL8366_SGCR 0x0000
40 #define RTL8366_SGCR_EN_BC_STORM_CTRL BIT(0)
41 #define RTL8366_SGCR_MAX_LENGTH(_x) (_x << 4)
42 #define RTL8366_SGCR_MAX_LENGTH_MASK RTL8366_SGCR_MAX_LENGTH(0x3)
43 #define RTL8366_SGCR_MAX_LENGTH_1522 RTL8366_SGCR_MAX_LENGTH(0x0)
44 #define RTL8366_SGCR_MAX_LENGTH_1536 RTL8366_SGCR_MAX_LENGTH(0x1)
45 #define RTL8366_SGCR_MAX_LENGTH_1552 RTL8366_SGCR_MAX_LENGTH(0x2)
46 #define RTL8366_SGCR_MAX_LENGTH_9216 RTL8366_SGCR_MAX_LENGTH(0x3)
48 /* Port Enable Control register */
49 #define RTL8366_PECR 0x0001
51 /* Switch Security Control registers */
52 #define RTL8366_SSCR0 0x0002
53 #define RTL8366_SSCR1 0x0003
54 #define RTL8366_SSCR2 0x0004
55 #define RTL8366_SSCR2_DROP_UNKNOWN_DA BIT(0)
57 #define RTL8366_RESET_CTRL_REG 0x0100
58 #define RTL8366_CHIP_CTRL_RESET_HW 1
59 #define RTL8366_CHIP_CTRL_RESET_SW (1 << 1)
61 #define RTL8366S_CHIP_VERSION_CTRL_REG 0x050A
62 #define RTL8366S_CHIP_VERSION_MASK 0xf
63 #define RTL8366S_CHIP_ID_REG 0x0509
64 #define RTL8366S_CHIP_ID_8366 0x5937
66 /* PHY registers control */
67 #define RTL8366S_PHY_ACCESS_CTRL_REG 0x8000
68 #define RTL8366S_PHY_ACCESS_DATA_REG 0x8002
70 #define RTL8366S_PHY_CTRL_READ 1
71 #define RTL8366S_PHY_CTRL_WRITE 0
73 #define RTL8366S_PHY_REG_MASK 0x1f
74 #define RTL8366S_PHY_PAGE_OFFSET 5
75 #define RTL8366S_PHY_PAGE_MASK (0xf << 5)
76 #define RTL8366S_PHY_NO_OFFSET 9
77 #define RTL8366S_PHY_NO_MASK (0x1f << 9)
79 /* LED control registers */
80 #define RTL8366_LED_BLINKRATE_REG 0x0430
81 #define RTL8366_LED_BLINKRATE_BIT 0
82 #define RTL8366_LED_BLINKRATE_MASK 0x0007
84 #define RTL8366_LED_CTRL_REG 0x0431
85 #define RTL8366_LED_0_1_CTRL_REG 0x0432
86 #define RTL8366_LED_2_3_CTRL_REG 0x0433
88 #define RTL8366S_MIB_COUNT 33
89 #define RTL8366S_GLOBAL_MIB_COUNT 1
90 #define RTL8366S_MIB_COUNTER_PORT_OFFSET 0x0050
91 #define RTL8366S_MIB_COUNTER_BASE 0x1000
92 #define RTL8366S_MIB_CTRL_REG 0x13F0
93 #define RTL8366S_MIB_CTRL_USER_MASK 0x0FFC
94 #define RTL8366S_MIB_CTRL_BUSY_MASK BIT(0)
95 #define RTL8366S_MIB_CTRL_RESET_MASK BIT(1)
96 #define RTL8366S_MIB_CTRL_PORT_RESET(_p) BIT(2 + (_p))
97 #define RTL8366S_MIB_CTRL_GLOBAL_RESET BIT(11)
99 #define RTL8366S_PORT_VLAN_CTRL_BASE 0x0063
100 #define RTL8366S_PORT_VLAN_CTRL_REG(_p) \
101 (RTL8366S_PORT_VLAN_CTRL_BASE + (_p) / 4)
102 #define RTL8366S_PORT_VLAN_CTRL_MASK 0xf
103 #define RTL8366S_PORT_VLAN_CTRL_SHIFT(_p) (4 * ((_p) % 4))
106 #define RTL8366S_VLAN_TABLE_READ_BASE 0x018C
107 #define RTL8366S_VLAN_TABLE_WRITE_BASE 0x0185
110 #define RTL8366S_TABLE_ACCESS_CTRL_REG 0x0180
111 #define RTL8366S_TABLE_VLAN_READ_CTRL 0x0E01
112 #define RTL8366S_TABLE_VLAN_WRITE_CTRL 0x0F01
114 #define RTL8366S_VLAN_MEMCONF_BASE 0x0020
117 #define RTL8366S_PORT_LINK_STATUS_BASE 0x0014
118 #define RTL8366S_PORT_STATUS_SPEED_MASK 0x0003
119 #define RTL8366S_PORT_STATUS_DUPLEX_MASK 0x0004
120 #define RTL8366S_PORT_STATUS_LINK_MASK 0x0010
121 #define RTL8366S_PORT_STATUS_TXPAUSE_MASK 0x0020
122 #define RTL8366S_PORT_STATUS_RXPAUSE_MASK 0x0040
123 #define RTL8366S_PORT_STATUS_AN_MASK 0x0080
126 #define RTL8366_PORT_NUM_CPU 5
127 #define RTL8366_NUM_PORTS 6
128 #define RTL8366_NUM_VLANS 16
129 #define RTL8366_NUM_LEDGROUPS 4
130 #define RTL8366_NUM_VIDS 4096
131 #define RTL8366S_PRIORITYMAX 7
132 #define RTL8366S_FIDMAX 7
135 #define RTL8366_PORT_1 (1 << 0) /* In userspace port 0 */
136 #define RTL8366_PORT_2 (1 << 1) /* In userspace port 1 */
137 #define RTL8366_PORT_3 (1 << 2) /* In userspace port 2 */
138 #define RTL8366_PORT_4 (1 << 3) /* In userspace port 3 */
139 #define RTL8366_PORT_5 (1 << 4) /* In userspace port 4 */
141 #define RTL8366_PORT_CPU (1 << 5) /* CPU port */
143 #define RTL8366_PORT_ALL (RTL8366_PORT_1 | \
150 #define RTL8366_PORT_ALL_BUT_CPU (RTL8366_PORT_1 | \
156 #define RTL8366_PORT_ALL_EXTERNAL (RTL8366_PORT_1 | \
161 #define RTL8366_PORT_ALL_INTERNAL RTL8366_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[RTL8366S_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, RTL8366_RESET_CTRL_REG,
276 RTL8366_CHIP_CTRL_RESET_HW);
279 if (rtl8366_smi_read_reg(smi, RTL8366_RESET_CTRL_REG, &data))
282 if (!(data & RTL8366_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, RTL8366_SGCR, RTL8366_SGCR_MAX_LENGTH_MASK,
300 RTL8366_SGCR_MAX_LENGTH_1536);
302 /* enable all ports */
303 REG_WR(smi, RTL8366_PECR, 0);
305 /* disable learning for all ports */
306 REG_WR(smi, RTL8366_SSCR0, RTL8366_PORT_ALL);
308 /* disable auto ageing for all ports */
309 REG_WR(smi, RTL8366_SSCR1, RTL8366_PORT_ALL);
311 /* don't drop packets whose DA has not been learned */
312 REG_RMW(smi, RTL8366_SSCR2, RTL8366_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 > RTL8366S_PHY_NO_MAX)
326 if (page > RTL8366S_PHY_PAGE_MAX)
329 if (addr > RTL8366S_PHY_ADDR_MAX)
332 ret = rtl8366_smi_write_reg(smi, RTL8366S_PHY_ACCESS_CTRL_REG,
333 RTL8366S_PHY_CTRL_READ);
337 reg = 0x8000 | (1 << (phy_no + RTL8366S_PHY_NO_OFFSET)) |
338 ((page << RTL8366S_PHY_PAGE_OFFSET) & RTL8366S_PHY_PAGE_MASK) |
339 (addr & RTL8366S_PHY_REG_MASK);
341 ret = rtl8366_smi_write_reg(smi, reg, 0);
345 ret = rtl8366_smi_read_reg(smi, RTL8366S_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 > RTL8366S_PHY_NO_MAX)
361 if (page > RTL8366S_PHY_PAGE_MAX)
364 if (addr > RTL8366S_PHY_ADDR_MAX)
367 ret = rtl8366_smi_write_reg(smi, RTL8366S_PHY_ACCESS_CTRL_REG,
368 RTL8366S_PHY_CTRL_WRITE);
372 reg = 0x8000 | (1 << (phy_no + RTL8366S_PHY_NO_OFFSET)) |
373 ((page << RTL8366S_PHY_PAGE_OFFSET) & RTL8366S_PHY_PAGE_MASK) |
374 (addr & RTL8366S_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 > RTL8366_NUM_PORTS || counter >= RTL8366S_MIB_COUNT)
394 addr = RTL8366S_MIB_COUNTER_BASE +
395 RTL8366S_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, RTL8366S_MIB_CTRL_REG, &data);
412 if (data & RTL8366S_MIB_CTRL_BUSY_MASK)
415 if (data & RTL8366S_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 >= RTL8366_NUM_VIDS)
445 tableaddr = (u16 *)&vlan4k_priv;
449 err = rtl8366_smi_write_reg(smi, RTL8366S_VLAN_TABLE_WRITE_BASE, data);
453 /* write table access control word */
454 err = rtl8366_smi_write_reg(smi, RTL8366S_TABLE_ACCESS_CTRL_REG,
455 RTL8366S_TABLE_VLAN_READ_CTRL);
459 err = rtl8366_smi_read_reg(smi, RTL8366S_VLAN_TABLE_READ_BASE, &data);
466 err = rtl8366_smi_read_reg(smi, RTL8366S_VLAN_TABLE_READ_BASE + 1,
474 err = rtl8366_smi_read_reg(smi, RTL8366S_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 >= RTL8366_NUM_VIDS ||
497 vlan4k->member > RTL8366_PORT_ALL ||
498 vlan4k->untag > RTL8366_PORT_ALL ||
499 vlan4k->fid > RTL8366S_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, RTL8366S_VLAN_TABLE_WRITE_BASE, data);
519 err = rtl8366_smi_write_reg(smi, RTL8366S_VLAN_TABLE_WRITE_BASE + 1,
528 err = rtl8366_smi_write_reg(smi, RTL8366S_VLAN_TABLE_WRITE_BASE + 2,
533 /* write table access control word */
534 err = rtl8366_smi_write_reg(smi, RTL8366S_TABLE_ACCESS_CTRL_REG,
535 RTL8366S_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 >= RTL8366_NUM_VLANS)
554 tableaddr = (u16 *)&vlanmc_priv;
556 addr = RTL8366S_VLAN_MEMCONF_BASE + (index * 3);
557 err = rtl8366_smi_read_reg(smi, addr, &data);
564 addr = RTL8366S_VLAN_MEMCONF_BASE + 1 + (index * 3);
565 err = rtl8366_smi_read_reg(smi, addr, &data);
572 addr = RTL8366S_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 >= RTL8366_NUM_VLANS ||
598 vlanmc->vid >= RTL8366_NUM_VIDS ||
599 vlanmc->priority > RTL8366S_PRIORITYMAX ||
600 vlanmc->member > RTL8366_PORT_ALL ||
601 vlanmc->untag > RTL8366_PORT_ALL ||
602 vlanmc->fid > RTL8366S_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 = RTL8366S_VLAN_MEMCONF_BASE + (index * 3);
615 tableaddr = (u16 *)&vlanmc_priv;
618 err = rtl8366_smi_write_reg(smi, addr, data);
622 addr = RTL8366S_VLAN_MEMCONF_BASE + 1 + (index * 3);
627 err = rtl8366_smi_write_reg(smi, addr, data);
631 addr = RTL8366S_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 >= RTL8366_NUM_PORTS)
650 err = rtl8366_smi_read_reg(smi, RTL8366S_PORT_VLAN_CTRL_REG(port),
655 *val = (data >> RTL8366S_PORT_VLAN_CTRL_SHIFT(port)) &
656 RTL8366S_PORT_VLAN_CTRL_MASK;
662 static int rtl8366rb_set_mc_index(struct rtl8366_smi *smi, int port, int index)
664 if (port >= RTL8366_NUM_PORTS || index >= RTL8366_NUM_VLANS)
667 return rtl8366_smi_rmwr(smi, RTL8366S_PORT_VLAN_CTRL_REG(port),
668 RTL8366S_PORT_VLAN_CTRL_MASK <<
669 RTL8366S_PORT_VLAN_CTRL_SHIFT(port),
670 (index & RTL8366S_PORT_VLAN_CTRL_MASK) <<
671 RTL8366S_PORT_VLAN_CTRL_SHIFT(port));
674 static int rtl8366rb_set_vlan(struct rtl8366_smi *smi, int vid, u32 member,
677 struct rtl8366_vlan_4k vlan4k;
681 /* Update the 4K table */
682 err = rtl8366rb_get_vlan_4k(smi, vid, &vlan4k);
686 vlan4k.member = member;
687 vlan4k.untag = untag;
689 err = rtl8366rb_set_vlan_4k(smi, &vlan4k);
693 /* Try to find an existing MC entry for this VID */
694 for (i = 0; i < RTL8366_NUM_VLANS; i++) {
695 struct rtl8366_vlan_mc vlanmc;
697 err = rtl8366rb_get_vlan_mc(smi, i, &vlanmc);
701 if (vid == vlanmc.vid) {
702 /* update the MC entry */
703 vlanmc.member = member;
704 vlanmc.untag = untag;
707 err = rtl8366rb_set_vlan_mc(smi, i, &vlanmc);
715 static int rtl8366rb_get_pvid(struct rtl8366_smi *smi, int port, int *val)
717 struct rtl8366_vlan_mc vlanmc;
721 err = rtl8366rb_get_mc_index(smi, port, &index);
725 err = rtl8366rb_get_vlan_mc(smi, index, &vlanmc);
733 static int rtl8366rb_mc_is_used(struct rtl8366_smi *smi, int mc_index,
740 for (i = 0; i < RTL8366_NUM_PORTS; i++) {
743 err = rtl8366rb_get_mc_index(smi, i, &index);
747 if (mc_index == index) {
756 static int rtl8366rb_set_pvid(struct rtl8366_smi *smi, unsigned port,
759 struct rtl8366_vlan_mc vlanmc;
760 struct rtl8366_vlan_4k vlan4k;
764 /* Try to find an existing MC entry for this VID */
765 for (i = 0; i < RTL8366_NUM_VLANS; i++) {
766 err = rtl8366rb_get_vlan_mc(smi, i, &vlanmc);
770 if (vid == vlanmc.vid) {
771 err = rtl8366rb_set_vlan_mc(smi, i, &vlanmc);
775 err = rtl8366rb_set_mc_index(smi, port, i);
780 /* We have no MC entry for this VID, try to find an empty one */
781 for (i = 0; i < RTL8366_NUM_VLANS; i++) {
782 err = rtl8366rb_get_vlan_mc(smi, i, &vlanmc);
786 if (vlanmc.vid == 0 && vlanmc.member == 0) {
787 /* Update the entry from the 4K table */
788 err = rtl8366rb_get_vlan_4k(smi, vid, &vlan4k);
793 vlanmc.member = vlan4k.member;
794 vlanmc.untag = vlan4k.untag;
795 vlanmc.fid = vlan4k.fid;
796 err = rtl8366rb_set_vlan_mc(smi, i, &vlanmc);
800 err = rtl8366rb_set_mc_index(smi, port, i);
805 /* MC table is full, try to find an unused entry and replace it */
806 for (i = 0; i < RTL8366_NUM_VLANS; i++) {
809 err = rtl8366rb_mc_is_used(smi, i, &used);
814 /* Update the entry from the 4K table */
815 err = rtl8366rb_get_vlan_4k(smi, vid, &vlan4k);
820 vlanmc.member = vlan4k.member;
821 vlanmc.untag = vlan4k.untag;
822 vlanmc.fid = vlan4k.fid;
823 err = rtl8366rb_set_vlan_mc(smi, i, &vlanmc);
827 err = rtl8366rb_set_mc_index(smi, port, i);
833 "all VLAN member configurations are in use\n");
838 static int rtl8366rb_vlan_set_vlan(struct rtl8366_smi *smi, int enable)
840 return rtl8366_smi_rmwr(smi, RTL8366_CHIP_GLOBAL_CTRL_REG,
841 RTL8366_CHIP_CTRL_VLAN,
842 (enable) ? RTL8366_CHIP_CTRL_VLAN : 0);
845 static int rtl8366rb_vlan_set_4ktable(struct rtl8366_smi *smi, int enable)
847 return rtl8366_smi_rmwr(smi, RTL8366_CHIP_GLOBAL_CTRL_REG,
848 RTL8366_CHIP_CTRL_VLAN_4KTB,
849 (enable) ? RTL8366_CHIP_CTRL_VLAN_4KTB : 0);
852 static int rtl8366rb_reset_vlan(struct rtl8366_smi *smi)
854 struct rtl8366_vlan_mc vlanmc;
858 /* clear VLAN member configurations */
864 for (i = 0; i < RTL8366_NUM_VLANS; i++) {
865 err = rtl8366rb_set_vlan_mc(smi, i, &vlanmc);
870 for (i = 0; i < RTL8366_NUM_PORTS; i++) {
871 if (i == RTL8366_PORT_CPU)
874 err = rtl8366rb_set_vlan(smi, (i + 1),
875 (1 << i) | RTL8366_PORT_CPU,
876 (1 << i) | RTL8366_PORT_CPU,
881 err = rtl8366rb_set_pvid(smi, i, (i + 1));
889 #ifdef CONFIG_RTL8366S_PHY_DEBUG_FS
890 static int rtl8366rb_debugfs_open(struct inode *inode, struct file *file)
892 file->private_data = inode->i_private;
896 static ssize_t rtl8366rb_read_debugfs_mibs(struct file *file,
897 char __user *user_buf,
898 size_t count, loff_t *ppos)
900 struct rtl8366rb *rtl = (struct rtl8366rb *)file->private_data;
901 struct rtl8366_smi *smi = &rtl->smi;
903 char *buf = rtl->buf;
905 len += snprintf(buf + len, sizeof(rtl->buf) - len,
906 "%-36s %12s %12s %12s %12s %12s %12s\n",
908 "Port 0", "Port 1", "Port 2",
909 "Port 3", "Port 4", "Port 5");
911 for (i = 0; i < ARRAY_SIZE(rtl8366rb_mib_counters); ++i) {
912 len += snprintf(buf + len, sizeof(rtl->buf) - len, "%-36s ",
913 rtl8366rb_mib_counters[i].name);
914 for (j = 0; j < RTL8366_NUM_PORTS; ++j) {
915 unsigned long long counter = 0;
917 if (!rtl8366_get_mib_counter(smi, i, j, &counter))
918 len += snprintf(buf + len,
919 sizeof(rtl->buf) - len,
922 len += snprintf(buf + len,
923 sizeof(rtl->buf) - len,
926 len += snprintf(buf + len, sizeof(rtl->buf) - len, "\n");
929 return simple_read_from_buffer(user_buf, count, ppos, buf, len);
932 static ssize_t rtl8366rb_read_debugfs_vlan_mc(struct file *file,
933 char __user *user_buf,
934 size_t count, loff_t *ppos)
936 struct rtl8366rb *rtl = (struct rtl8366rb *)file->private_data;
937 struct rtl8366_smi *smi = &rtl->smi;
939 char *buf = rtl->buf;
941 len += snprintf(buf + len, sizeof(rtl->buf) - len,
942 "%2s %6s %4s %6s %6s %3s\n",
943 "id", "vid","prio", "member", "untag", "fid");
945 for (i = 0; i < RTL8366_NUM_VLANS; ++i) {
946 struct rtl8366_vlan_mc vlanmc;
948 rtl8366rb_get_vlan_mc(smi, i, &vlanmc);
950 len += snprintf(buf + len, sizeof(rtl->buf) - len,
951 "%2d %6d %4d 0x%04x 0x%04x %3d\n",
952 i, vlanmc.vid, vlanmc.priority,
953 vlanmc.member, vlanmc.untag, vlanmc.fid);
956 return simple_read_from_buffer(user_buf, count, ppos, buf, len);
959 static ssize_t rtl8366rb_read_debugfs_reg(struct file *file,
960 char __user *user_buf,
961 size_t count, loff_t *ppos)
963 struct rtl8366rb *rtl = (struct rtl8366rb *)file->private_data;
964 struct rtl8366_smi *smi = &rtl->smi;
965 u32 t, reg = gl_dbg_reg;
967 char *buf = rtl->buf;
969 memset(buf, '\0', sizeof(rtl->buf));
971 err = rtl8366_smi_read_reg(smi, reg, &t);
973 len += snprintf(buf, sizeof(rtl->buf),
974 "Read failed (reg: 0x%04x)\n", reg);
975 return simple_read_from_buffer(user_buf, count, ppos, buf, len);
978 len += snprintf(buf, sizeof(rtl->buf), "reg = 0x%04x, val = 0x%04x\n",
981 return simple_read_from_buffer(user_buf, count, ppos, buf, len);
984 static ssize_t rtl8366rb_write_debugfs_reg(struct file *file,
985 const char __user *user_buf,
986 size_t count, loff_t *ppos)
988 struct rtl8366rb *rtl = (struct rtl8366rb *)file->private_data;
989 struct rtl8366_smi *smi = &rtl->smi;
991 u32 reg = gl_dbg_reg;
994 char *buf = rtl->buf;
996 len = min(count, sizeof(rtl->buf) - 1);
997 if (copy_from_user(buf, user_buf, len)) {
998 dev_err(rtl->parent, "copy from user failed\n");
1003 if (len > 0 && buf[len - 1] == '\n')
1004 buf[len - 1] = '\0';
1007 if (strict_strtoul(buf, 16, &data)) {
1008 dev_err(rtl->parent, "Invalid reg value %s\n", buf);
1010 err = rtl8366_smi_write_reg(smi, reg, data);
1012 dev_err(rtl->parent,
1013 "writing reg 0x%04x val 0x%04lx failed\n",
1021 static const struct file_operations fops_rtl8366rb_regs = {
1022 .read = rtl8366rb_read_debugfs_reg,
1023 .write = rtl8366rb_write_debugfs_reg,
1024 .open = rtl8366rb_debugfs_open,
1025 .owner = THIS_MODULE
1028 static const struct file_operations fops_rtl8366rb_vlan_mc = {
1029 .read = rtl8366rb_read_debugfs_vlan_mc,
1030 .open = rtl8366rb_debugfs_open,
1031 .owner = THIS_MODULE
1034 static const struct file_operations fops_rtl8366rb_mibs = {
1035 .read = rtl8366rb_read_debugfs_mibs,
1036 .open = rtl8366rb_debugfs_open,
1037 .owner = THIS_MODULE
1040 static void rtl8366rb_debugfs_init(struct rtl8366rb *rtl)
1042 struct dentry *node;
1043 struct dentry *root;
1045 if (!rtl->debugfs_root)
1046 rtl->debugfs_root = debugfs_create_dir("rtl8366rb", NULL);
1048 if (!rtl->debugfs_root) {
1049 dev_err(rtl->parent, "Unable to create debugfs dir\n");
1052 root = rtl->debugfs_root;
1054 node = debugfs_create_x16("reg", S_IRUGO | S_IWUSR, root, &gl_dbg_reg);
1056 dev_err(rtl->parent, "Creating debugfs file '%s' failed\n",
1061 node = debugfs_create_file("val", S_IRUGO | S_IWUSR, root, rtl,
1062 &fops_rtl8366rb_regs);
1064 dev_err(rtl->parent, "Creating debugfs file '%s' failed\n",
1069 node = debugfs_create_file("vlan_mc", S_IRUSR, root, rtl,
1070 &fops_rtl8366rb_vlan_mc);
1072 dev_err(rtl->parent, "Creating debugfs file '%s' failed\n",
1077 node = debugfs_create_file("mibs", S_IRUSR, root, rtl,
1078 &fops_rtl8366rb_mibs);
1080 dev_err(rtl->parent, "Creating debugfs file '%s' failed\n",
1086 static void rtl8366rb_debugfs_remove(struct rtl8366rb *rtl)
1088 if (rtl->debugfs_root) {
1089 debugfs_remove_recursive(rtl->debugfs_root);
1090 rtl->debugfs_root = NULL;
1095 static inline void rtl8366rb_debugfs_init(struct rtl8366rb *rtl) {}
1096 static inline void rtl8366rb_debugfs_remove(struct rtl8366rb *rtl) {}
1097 #endif /* CONFIG_RTL8366S_PHY_DEBUG_FS */
1099 static int rtl8366rb_sw_reset_mibs(struct switch_dev *dev,
1100 const struct switch_attr *attr,
1101 struct switch_val *val)
1103 struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev);
1106 if (val->value.i == 1)
1107 err = rtl8366_smi_rmwr(smi, RTL8366S_MIB_CTRL_REG, 0,
1108 RTL8366S_MIB_CTRL_GLOBAL_RESET);
1113 static int rtl8366rb_sw_get_vlan_enable(struct switch_dev *dev,
1114 const struct switch_attr *attr,
1115 struct switch_val *val)
1117 struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev);
1120 if (attr->ofs == 1) {
1121 rtl8366_smi_read_reg(smi, RTL8366_CHIP_GLOBAL_CTRL_REG, &data);
1123 if (data & RTL8366_CHIP_CTRL_VLAN)
1127 } else if (attr->ofs == 2) {
1128 rtl8366_smi_read_reg(smi, RTL8366_CHIP_GLOBAL_CTRL_REG, &data);
1130 if (data & RTL8366_CHIP_CTRL_VLAN_4KTB)
1139 static int rtl8366rb_sw_get_blinkrate(struct switch_dev *dev,
1140 const struct switch_attr *attr,
1141 struct switch_val *val)
1143 struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev);
1146 rtl8366_smi_read_reg(smi, RTL8366_LED_BLINKRATE_REG, &data);
1148 val->value.i = (data & (RTL8366_LED_BLINKRATE_MASK));
1153 static int rtl8366rb_sw_set_blinkrate(struct switch_dev *dev,
1154 const struct switch_attr *attr,
1155 struct switch_val *val)
1157 struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev);
1159 if (val->value.i >= 6)
1162 return rtl8366_smi_rmwr(smi, RTL8366_LED_BLINKRATE_REG,
1163 RTL8366_LED_BLINKRATE_MASK,
1167 static int rtl8366rb_sw_set_vlan_enable(struct switch_dev *dev,
1168 const struct switch_attr *attr,
1169 struct switch_val *val)
1171 struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev);
1174 return rtl8366rb_vlan_set_vlan(smi, val->value.i);
1176 return rtl8366rb_vlan_set_4ktable(smi, val->value.i);
1179 static const char *rtl8366rb_speed_str(unsigned speed)
1193 static int rtl8366rb_sw_get_port_link(struct switch_dev *dev,
1194 const struct switch_attr *attr,
1195 struct switch_val *val)
1197 struct rtl8366rb *rtl = sw_to_rtl8366rb(dev);
1198 struct rtl8366_smi *smi = &rtl->smi;
1199 u32 len = 0, data = 0;
1201 if (val->port_vlan >= RTL8366_NUM_PORTS)
1204 memset(rtl->buf, '\0', sizeof(rtl->buf));
1205 rtl8366_smi_read_reg(smi, RTL8366S_PORT_LINK_STATUS_BASE +
1206 (val->port_vlan / 2), &data);
1208 if (val->port_vlan % 2)
1211 if (data & RTL8366S_PORT_STATUS_LINK_MASK) {
1212 len = snprintf(rtl->buf, sizeof(rtl->buf),
1213 "port:%d link:up speed:%s %s-duplex %s%s%s",
1215 rtl8366rb_speed_str(data &
1216 RTL8366S_PORT_STATUS_SPEED_MASK),
1217 (data & RTL8366S_PORT_STATUS_DUPLEX_MASK) ?
1219 (data & RTL8366S_PORT_STATUS_TXPAUSE_MASK) ?
1221 (data & RTL8366S_PORT_STATUS_RXPAUSE_MASK) ?
1223 (data & RTL8366S_PORT_STATUS_AN_MASK) ?
1226 len = snprintf(rtl->buf, sizeof(rtl->buf), "port:%d link: down",
1230 val->value.s = rtl->buf;
1236 static int rtl8366rb_sw_get_vlan_info(struct switch_dev *dev,
1237 const struct switch_attr *attr,
1238 struct switch_val *val)
1242 struct rtl8366_vlan_4k vlan4k;
1243 struct rtl8366rb *rtl = sw_to_rtl8366rb(dev);
1244 struct rtl8366_smi *smi = &rtl->smi;
1245 char *buf = rtl->buf;
1248 if (val->port_vlan == 0 || val->port_vlan >= RTL8366_NUM_VLANS)
1251 memset(buf, '\0', sizeof(rtl->buf));
1253 err = rtl8366rb_get_vlan_4k(smi, val->port_vlan, &vlan4k);
1257 len += snprintf(buf + len, sizeof(rtl->buf) - len,
1258 "VLAN %d: Ports: '", vlan4k.vid);
1260 for (i = 0; i < RTL8366_NUM_PORTS; i++) {
1261 if (!(vlan4k.member & (1 << i)))
1264 len += snprintf(buf + len, sizeof(rtl->buf) - len, "%d%s", i,
1265 (vlan4k.untag & (1 << i)) ? "" : "t");
1268 len += snprintf(buf + len, sizeof(rtl->buf) - len,
1269 "', members=%04x, untag=%04x, fid=%u",
1270 vlan4k.member, vlan4k.untag, vlan4k.fid);
1278 static int rtl8366rb_sw_set_port_led(struct switch_dev *dev,
1279 const struct switch_attr *attr,
1280 struct switch_val *val)
1282 struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev);
1287 if (val->port_vlan >= RTL8366_NUM_PORTS)
1290 if (val->port_vlan == RTL8366_PORT_NUM_CPU) {
1291 reg = RTL8366_LED_BLINKRATE_REG;
1293 data = val->value.i << 4;
1295 reg = RTL8366_LED_CTRL_REG;
1296 mask = 0xF << (val->port_vlan * 4),
1297 data = val->value.i << (val->port_vlan * 4);
1300 return rtl8366_smi_rmwr(smi, RTL8366_LED_BLINKRATE_REG, mask, data);
1303 static int rtl8366rb_sw_get_port_led(struct switch_dev *dev,
1304 const struct switch_attr *attr,
1305 struct switch_val *val)
1307 struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev);
1310 if (val->port_vlan >= RTL8366_NUM_LEDGROUPS)
1313 rtl8366_smi_read_reg(smi, RTL8366_LED_CTRL_REG, &data);
1314 val->value.i = (data >> (val->port_vlan * 4)) & 0x000F;
1319 static int rtl8366rb_sw_reset_port_mibs(struct switch_dev *dev,
1320 const struct switch_attr *attr,
1321 struct switch_val *val)
1323 struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev);
1325 if (val->port_vlan >= RTL8366_NUM_PORTS)
1328 return rtl8366_smi_rmwr(smi, RTL8366S_MIB_CTRL_REG, 0,
1329 RTL8366S_MIB_CTRL_PORT_RESET(val->port_vlan));
1332 static int rtl8366rb_sw_get_port_mib(struct switch_dev *dev,
1333 const struct switch_attr *attr,
1334 struct switch_val *val)
1336 struct rtl8366rb *rtl = sw_to_rtl8366rb(dev);
1337 struct rtl8366_smi *smi = &rtl->smi;
1339 unsigned long long counter = 0;
1340 char *buf = rtl->buf;
1342 if (val->port_vlan >= RTL8366_NUM_PORTS)
1345 len += snprintf(buf + len, sizeof(rtl->buf) - len,
1346 "Port %d MIB counters\n",
1349 for (i = 0; i < ARRAY_SIZE(rtl8366rb_mib_counters); ++i) {
1350 len += snprintf(buf + len, sizeof(rtl->buf) - len,
1351 "%-36s: ", rtl8366rb_mib_counters[i].name);
1352 if (!rtl8366_get_mib_counter(smi, i, val->port_vlan, &counter))
1353 len += snprintf(buf + len, sizeof(rtl->buf) - len,
1356 len += snprintf(buf + len, sizeof(rtl->buf) - len,
1365 static int rtl8366rb_sw_get_vlan_ports(struct switch_dev *dev,
1366 struct switch_val *val)
1368 struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev);
1369 struct switch_port *port;
1370 struct rtl8366_vlan_4k vlan4k;
1373 if (val->port_vlan == 0 || val->port_vlan >= RTL8366_NUM_VLANS)
1376 rtl8366rb_get_vlan_4k(smi, val->port_vlan, &vlan4k);
1378 port = &val->value.ports[0];
1380 for (i = 0; i < RTL8366_NUM_PORTS; i++) {
1381 if (!(vlan4k.member & BIT(i)))
1385 port->flags = (vlan4k.untag & BIT(i)) ?
1386 0 : BIT(SWITCH_PORT_FLAG_TAGGED);
1393 static int rtl8366rb_sw_set_vlan_ports(struct switch_dev *dev,
1394 struct switch_val *val)
1396 struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev);
1397 struct switch_port *port;
1402 if (val->port_vlan == 0 || val->port_vlan >= RTL8366_NUM_VLANS)
1405 port = &val->value.ports[0];
1406 for (i = 0; i < val->len; i++, port++) {
1407 member |= BIT(port->id);
1409 if (!(port->flags & BIT(SWITCH_PORT_FLAG_TAGGED)))
1410 untag |= BIT(port->id);
1413 return rtl8366rb_set_vlan(smi, val->port_vlan, member, untag, 0);
1416 static int rtl8366rb_sw_get_port_pvid(struct switch_dev *dev, int port, int *val)
1418 struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev);
1419 return rtl8366rb_get_pvid(smi, port, val);
1422 static int rtl8366rb_sw_set_port_pvid(struct switch_dev *dev, int port, int val)
1424 struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev);
1425 return rtl8366rb_set_pvid(smi, port, val);
1428 static int rtl8366rb_sw_reset_switch(struct switch_dev *dev)
1430 struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev);
1433 err = rtl8366rb_reset_chip(smi);
1437 err = rtl8366rb_hw_init(smi);
1441 return rtl8366rb_reset_vlan(smi);
1444 static struct switch_attr rtl8366rb_globals[] = {
1446 .type = SWITCH_TYPE_INT,
1447 .name = "enable_vlan",
1448 .description = "Enable VLAN mode",
1449 .set = rtl8366rb_sw_set_vlan_enable,
1450 .get = rtl8366rb_sw_get_vlan_enable,
1454 .type = SWITCH_TYPE_INT,
1455 .name = "enable_vlan4k",
1456 .description = "Enable VLAN 4K mode",
1457 .set = rtl8366rb_sw_set_vlan_enable,
1458 .get = rtl8366rb_sw_get_vlan_enable,
1462 .type = SWITCH_TYPE_INT,
1463 .name = "reset_mibs",
1464 .description = "Reset all MIB counters",
1465 .set = rtl8366rb_sw_reset_mibs,
1469 .type = SWITCH_TYPE_INT,
1470 .name = "blinkrate",
1471 .description = "Get/Set LED blinking rate (0 = 43ms, 1 = 84ms,"
1472 " 2 = 120ms, 3 = 170ms, 4 = 340ms, 5 = 670ms)",
1473 .set = rtl8366rb_sw_set_blinkrate,
1474 .get = rtl8366rb_sw_get_blinkrate,
1479 static struct switch_attr rtl8366rb_port[] = {
1481 .type = SWITCH_TYPE_STRING,
1483 .description = "Get port link information",
1486 .get = rtl8366rb_sw_get_port_link,
1488 .type = SWITCH_TYPE_INT,
1489 .name = "reset_mib",
1490 .description = "Reset single port MIB counters",
1492 .set = rtl8366rb_sw_reset_port_mibs,
1495 .type = SWITCH_TYPE_STRING,
1497 .description = "Get MIB counters for port",
1500 .get = rtl8366rb_sw_get_port_mib,
1502 .type = SWITCH_TYPE_INT,
1504 .description = "Get/Set port group (0 - 3) led mode (0 - 15)",
1506 .set = rtl8366rb_sw_set_port_led,
1507 .get = rtl8366rb_sw_get_port_led,
1511 static struct switch_attr rtl8366rb_vlan[] = {
1513 .type = SWITCH_TYPE_STRING,
1515 .description = "Get vlan information",
1518 .get = rtl8366rb_sw_get_vlan_info,
1523 static struct switch_dev rtl8366_switch_dev = {
1525 .cpu_port = RTL8366_PORT_NUM_CPU,
1526 .ports = RTL8366_NUM_PORTS,
1527 .vlans = RTL8366_NUM_VLANS,
1529 .attr = rtl8366rb_globals,
1530 .n_attr = ARRAY_SIZE(rtl8366rb_globals),
1533 .attr = rtl8366rb_port,
1534 .n_attr = ARRAY_SIZE(rtl8366rb_port),
1537 .attr = rtl8366rb_vlan,
1538 .n_attr = ARRAY_SIZE(rtl8366rb_vlan),
1541 .get_vlan_ports = rtl8366rb_sw_get_vlan_ports,
1542 .set_vlan_ports = rtl8366rb_sw_set_vlan_ports,
1543 .get_port_pvid = rtl8366rb_sw_get_port_pvid,
1544 .set_port_pvid = rtl8366rb_sw_set_port_pvid,
1545 .reset_switch = rtl8366rb_sw_reset_switch,
1548 static int rtl8366rb_switch_init(struct rtl8366rb *rtl)
1550 struct switch_dev *dev = &rtl->dev;
1553 memcpy(dev, &rtl8366_switch_dev, sizeof(struct switch_dev));
1555 dev->devname = dev_name(rtl->parent);
1557 err = register_switch(dev, NULL);
1559 dev_err(rtl->parent, "switch registration failed\n");
1564 static void rtl8366rb_switch_cleanup(struct rtl8366rb *rtl)
1566 unregister_switch(&rtl->dev);
1569 static int rtl8366rb_mii_read(struct mii_bus *bus, int addr, int reg)
1571 struct rtl8366_smi *smi = bus->priv;
1575 err = rtl8366rb_read_phy_reg(smi, addr, 0, reg, &val);
1582 static int rtl8366rb_mii_write(struct mii_bus *bus, int addr, int reg, u16 val)
1584 struct rtl8366_smi *smi = bus->priv;
1588 err = rtl8366rb_write_phy_reg(smi, addr, 0, reg, val);
1590 (void) rtl8366rb_read_phy_reg(smi, addr, 0, reg, &t);
1595 static int rtl8366rb_mii_bus_match(struct mii_bus *bus)
1597 return (bus->read == rtl8366rb_mii_read &&
1598 bus->write == rtl8366rb_mii_write);
1601 static int rtl8366rb_setup(struct rtl8366rb *rtl)
1603 struct rtl8366_smi *smi = &rtl->smi;
1606 rtl8366rb_debugfs_init(rtl);
1608 ret = rtl8366rb_reset_chip(smi);
1612 ret = rtl8366rb_hw_init(smi);
1616 static int rtl8366rb_detect(struct rtl8366_smi *smi)
1622 ret = rtl8366_smi_read_reg(smi, RTL8366S_CHIP_ID_REG, &chip_id);
1624 dev_err(smi->parent, "unable to read chip id\n");
1629 case RTL8366S_CHIP_ID_8366:
1632 dev_err(smi->parent, "unknown chip id (%04x)\n", chip_id);
1636 ret = rtl8366_smi_read_reg(smi, RTL8366S_CHIP_VERSION_CTRL_REG,
1639 dev_err(smi->parent, "unable to read chip version\n");
1643 dev_info(smi->parent, "RTL%04x ver. %u chip found\n",
1644 chip_id, chip_ver & RTL8366S_CHIP_VERSION_MASK);
1649 static struct rtl8366_smi_ops rtl8366rb_smi_ops = {
1650 .detect = rtl8366rb_detect,
1651 .mii_read = rtl8366rb_mii_read,
1652 .mii_write = rtl8366rb_mii_write,
1655 static int __init rtl8366rb_probe(struct platform_device *pdev)
1657 static int rtl8366_smi_version_printed;
1658 struct rtl8366rb_platform_data *pdata;
1659 struct rtl8366rb *rtl;
1660 struct rtl8366_smi *smi;
1663 if (!rtl8366_smi_version_printed++)
1664 printk(KERN_NOTICE RTL8366S_DRIVER_DESC
1665 " version " RTL8366S_DRIVER_VER"\n");
1667 pdata = pdev->dev.platform_data;
1669 dev_err(&pdev->dev, "no platform data specified\n");
1674 rtl = kzalloc(sizeof(*rtl), GFP_KERNEL);
1676 dev_err(&pdev->dev, "no memory for private data\n");
1681 rtl->parent = &pdev->dev;
1684 smi->parent = &pdev->dev;
1685 smi->gpio_sda = pdata->gpio_sda;
1686 smi->gpio_sck = pdata->gpio_sck;
1687 smi->ops = &rtl8366rb_smi_ops;
1689 err = rtl8366_smi_init(smi);
1693 platform_set_drvdata(pdev, rtl);
1695 err = rtl8366rb_setup(rtl);
1697 goto err_clear_drvdata;
1699 err = rtl8366rb_switch_init(rtl);
1701 goto err_clear_drvdata;
1706 platform_set_drvdata(pdev, NULL);
1707 rtl8366_smi_cleanup(smi);
1714 static int rtl8366rb_phy_config_init(struct phy_device *phydev)
1716 if (!rtl8366rb_mii_bus_match(phydev->bus))
1722 static int rtl8366rb_phy_config_aneg(struct phy_device *phydev)
1727 static struct phy_driver rtl8366rb_phy_driver = {
1728 .phy_id = 0x001cc960,
1729 .name = "Realtek RTL8366RB",
1730 .phy_id_mask = 0x1ffffff0,
1731 .features = PHY_GBIT_FEATURES,
1732 .config_aneg = rtl8366rb_phy_config_aneg,
1733 .config_init = rtl8366rb_phy_config_init,
1734 .read_status = genphy_read_status,
1736 .owner = THIS_MODULE,
1740 static int __devexit rtl8366rb_remove(struct platform_device *pdev)
1742 struct rtl8366rb *rtl = platform_get_drvdata(pdev);
1745 rtl8366rb_switch_cleanup(rtl);
1746 rtl8366rb_debugfs_remove(rtl);
1747 platform_set_drvdata(pdev, NULL);
1748 rtl8366_smi_cleanup(&rtl->smi);
1755 static struct platform_driver rtl8366rb_driver = {
1757 .name = RTL8366RB_DRIVER_NAME,
1758 .owner = THIS_MODULE,
1760 .probe = rtl8366rb_probe,
1761 .remove = __devexit_p(rtl8366rb_remove),
1764 static int __init rtl8366rb_module_init(void)
1767 ret = platform_driver_register(&rtl8366rb_driver);
1771 ret = phy_driver_register(&rtl8366rb_phy_driver);
1773 goto err_platform_unregister;
1777 err_platform_unregister:
1778 platform_driver_unregister(&rtl8366rb_driver);
1781 module_init(rtl8366rb_module_init);
1783 static void __exit rtl8366rb_module_exit(void)
1785 phy_driver_unregister(&rtl8366rb_phy_driver);
1786 platform_driver_unregister(&rtl8366rb_driver);
1788 module_exit(rtl8366rb_module_exit);
1790 MODULE_DESCRIPTION(RTL8366S_DRIVER_DESC);
1791 MODULE_VERSION(RTL8366S_DRIVER_VER);
1792 MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
1793 MODULE_AUTHOR("Antti Seppälä <a.seppala@gmail.com>");
1794 MODULE_LICENSE("GPL v2");
1795 MODULE_ALIAS("platform:" RTL8366RB_DRIVER_NAME);