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 #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 0x050A
43 #define RTL8366S_CHIP_VERSION_MASK 0xf
44 #define RTL8366S_CHIP_ID_REG 0x0509
45 #define RTL8366S_CHIP_ID_8366 0x5937
47 /* PHY registers control */
48 #define RTL8366S_PHY_ACCESS_CTRL_REG 0x8000
49 #define RTL8366S_PHY_ACCESS_DATA_REG 0x8002
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 (0xf << 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 0x0430
62 #define RTL8366_LED_BLINKRATE_BIT 0
63 #define RTL8366_LED_BLINKRATE_MASK 0x0007
65 #define RTL8366_LED_CTRL_REG 0x0431
66 #define RTL8366_LED_0_1_CTRL_REG 0x0432
67 #define RTL8366_LED_2_3_CTRL_REG 0x0433
69 #define RTL8366S_MIB_COUNT 33
70 #define RTL8366S_GLOBAL_MIB_COUNT 1
71 #define RTL8366S_MIB_COUNTER_PORT_OFFSET 0x0050
72 #define RTL8366S_MIB_COUNTER_BASE 0x1000
73 #define RTL8366S_MIB_CTRL_REG 0x13F0
74 #define RTL8366S_MIB_CTRL_USER_MASK 0x0FFC
75 #define RTL8366S_MIB_CTRL_BUSY_MASK 0x0001
76 #define RTL8366S_MIB_CTRL_RESET_MASK 0x0001
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 0x0063
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 0x018C
91 #define RTL8366S_VLAN_TABLE_WRITE_BASE 0x0185
94 #define RTL8366S_TABLE_ACCESS_CTRL_REG 0x0180
95 #define RTL8366S_TABLE_VLAN_READ_CTRL 0x0E01
96 #define RTL8366S_TABLE_VLAN_WRITE_CTRL 0x0F01
98 #define RTL8366S_VLAN_MEMCONF_BASE 0x0020
101 #define RTL8366S_PORT_LINK_STATUS_BASE 0x0014
102 #define RTL8366S_PORT_STATUS_SPEED_MASK 0x0003
103 #define RTL8366S_PORT_STATUS_DUPLEX_MASK 0x0004
104 #define RTL8366S_PORT_STATUS_LINK_MASK 0x0010
105 #define RTL8366S_PORT_STATUS_TXPAUSE_MASK 0x0020
106 #define RTL8366S_PORT_STATUS_RXPAUSE_MASK 0x0040
107 #define RTL8366S_PORT_STATUS_AN_MASK 0x0080
110 #define RTL8366_PORT_NUM_CPU 5
111 #define RTL8366_NUM_PORTS 6
112 #define RTL8366_NUM_VLANS 16
113 #define RTL8366_NUM_LEDGROUPS 4
114 #define RTL8366_NUM_VIDS 4096
115 #define RTL8366S_PRIORITYMAX 7
116 #define RTL8366S_FIDMAX 7
119 #define RTL8366_PORT_1 (1 << 0) /* In userspace port 0 */
120 #define RTL8366_PORT_2 (1 << 1) /* In userspace port 1 */
121 #define RTL8366_PORT_3 (1 << 2) /* In userspace port 2 */
122 #define RTL8366_PORT_4 (1 << 3) /* In userspace port 3 */
123 #define RTL8366_PORT_5 (1 << 4) /* In userspace port 4 */
125 #define RTL8366_PORT_CPU (1 << 5) /* CPU port */
127 #define RTL8366_PORT_ALL (RTL8366_PORT_1 | \
134 #define RTL8366_PORT_ALL_BUT_CPU (RTL8366_PORT_1 | \
140 #define RTL8366_PORT_ALL_EXTERNAL (RTL8366_PORT_1 | \
145 #define RTL8366_PORT_ALL_INTERNAL RTL8366_PORT_CPU
148 struct device *parent;
149 struct rtl8366_smi smi;
150 struct switch_dev dev;
152 #ifdef CONFIG_RTL8366S_PHY_DEBUG_FS
153 struct dentry *debugfs_root;
157 struct rtl8366rb_vlan_mc {
169 struct rtl8366rb_vlan_4k {
178 #ifdef CONFIG_RTL8366S_PHY_DEBUG_FS
188 static struct mib_counter rtl8366rb_mib_counters[RTL8366S_MIB_COUNT] = {
189 { 0, 4, "IfInOctets " },
190 { 4, 4, "EtherStatsOctets " },
191 { 8, 2, "EtherStatsUnderSizePkts " },
192 { 10, 2, "EtherFregament " },
193 { 12, 2, "EtherStatsPkts64Octets " },
194 { 14, 2, "EtherStatsPkts65to127Octets " },
195 { 16, 2, "EtherStatsPkts128to255Octets " },
196 { 18, 2, "EtherStatsPkts256to511Octets " },
197 { 20, 2, "EtherStatsPkts512to1023Octets " },
198 { 22, 2, "EtherStatsPkts1024to1518Octets " },
199 { 24, 2, "EtherOversizeStats " },
200 { 26, 2, "EtherStatsJabbers " },
201 { 28, 2, "IfInUcastPkts " },
202 { 30, 2, "EtherStatsMulticastPkts " },
203 { 32, 2, "EtherStatsBroadcastPkts " },
204 { 34, 2, "EtherStatsDropEvents " },
205 { 36, 2, "Dot3StatsFCSErrors " },
206 { 38, 2, "Dot3StatsSymbolErrors " },
207 { 40, 2, "Dot3InPauseFrames " },
208 { 42, 2, "Dot3ControlInUnknownOpcodes " },
209 { 44, 4, "IfOutOctets " },
210 { 48, 2, "Dot3StatsSingleCollisionFrames " },
211 { 50, 2, "Dot3StatMultipleCollisionFrames " },
212 { 52, 2, "Dot3sDeferredTransmissions " },
213 { 54, 2, "Dot3StatsLateCollisions " },
214 { 56, 2, "EtherStatsCollisions " },
215 { 58, 2, "Dot3StatsExcessiveCollisions " },
216 { 60, 2, "Dot3OutPauseFrames " },
217 { 62, 2, "Dot1dBasePortDelayExceededDiscards" },
218 { 64, 2, "Dot1dTpPortInDiscards " },
219 { 66, 2, "IfOutUcastPkts " },
220 { 68, 2, "IfOutMulticastPkts " },
221 { 70, 2, "IfOutBroadcastPkts " },
224 static inline struct rtl8366rb *smi_to_rtl8366rb(struct rtl8366_smi *smi)
226 return container_of(smi, struct rtl8366rb, smi);
229 static inline struct rtl8366rb *sw_to_rtl8366rb(struct switch_dev *sw)
231 return container_of(sw, struct rtl8366rb, dev);
234 static int rtl8366rb_reset_chip(struct rtl8366rb *rtl)
236 struct rtl8366_smi *smi = &rtl->smi;
240 rtl8366_smi_write_reg(smi, RTL8366_RESET_CTRL_REG,
241 RTL8366_CHIP_CTRL_RESET_HW);
244 if (rtl8366_smi_read_reg(smi, RTL8366_RESET_CTRL_REG, &data))
247 if (!(data & RTL8366_CHIP_CTRL_RESET_HW))
252 printk("Timeout waiting for the switch to reset\n");
259 static int rtl8366rb_read_phy_reg(struct rtl8366_smi *smi,
260 u32 phy_no, u32 page, u32 addr, u32 *data)
265 if (phy_no > RTL8366S_PHY_NO_MAX)
268 if (page > RTL8366S_PHY_PAGE_MAX)
271 if (addr > RTL8366S_PHY_ADDR_MAX)
274 ret = rtl8366_smi_write_reg(smi, RTL8366S_PHY_ACCESS_CTRL_REG,
275 RTL8366S_PHY_CTRL_READ);
279 reg = 0x8000 | (1 << (phy_no + RTL8366S_PHY_NO_OFFSET)) |
280 ((page << RTL8366S_PHY_PAGE_OFFSET) & RTL8366S_PHY_PAGE_MASK) |
281 (addr & RTL8366S_PHY_REG_MASK);
283 ret = rtl8366_smi_write_reg(smi, reg, 0);
287 ret = rtl8366_smi_read_reg(smi, RTL8366S_PHY_ACCESS_DATA_REG, data);
294 static int rtl8366rb_write_phy_reg(struct rtl8366_smi *smi,
295 u32 phy_no, u32 page, u32 addr, u32 data)
300 if (phy_no > RTL8366S_PHY_NO_MAX)
303 if (page > RTL8366S_PHY_PAGE_MAX)
306 if (addr > RTL8366S_PHY_ADDR_MAX)
309 ret = rtl8366_smi_write_reg(smi, RTL8366S_PHY_ACCESS_CTRL_REG,
310 RTL8366S_PHY_CTRL_WRITE);
314 reg = 0x8000 | (1 << (phy_no + RTL8366S_PHY_NO_OFFSET)) |
315 ((page << RTL8366S_PHY_PAGE_OFFSET) & RTL8366S_PHY_PAGE_MASK) |
316 (addr & RTL8366S_PHY_REG_MASK);
318 ret = rtl8366_smi_write_reg(smi, reg, data);
325 static int rtl8366_get_mib_counter(struct rtl8366rb *rtl, int counter,
326 int port, unsigned long long *val)
328 struct rtl8366_smi *smi = &rtl->smi;
334 if (port > RTL8366_NUM_PORTS || counter >= RTL8366S_MIB_COUNT)
337 addr = RTL8366S_MIB_COUNTER_BASE +
338 RTL8366S_MIB_COUNTER_PORT_OFFSET * (port) +
339 rtl8366rb_mib_counters[counter].offset;
342 * Writing access counter address first
343 * then ASIC will prepare 64bits counter wait for being retrived
345 data = 0; /* writing data will be discard by ASIC */
346 err = rtl8366_smi_write_reg(smi, addr, data);
350 /* read MIB control register */
351 err = rtl8366_smi_read_reg(smi, RTL8366S_MIB_CTRL_REG, &data);
355 if (data & RTL8366S_MIB_CTRL_BUSY_MASK)
358 if (data & RTL8366S_MIB_CTRL_RESET_MASK)
362 for (i = rtl8366rb_mib_counters[counter].length; i > 0; i--) {
363 err = rtl8366_smi_read_reg(smi, addr + (i - 1), &data);
367 mibvalue = (mibvalue << 16) | (data & 0xFFFF);
374 static int rtl8366rb_get_vlan_4k(struct rtl8366rb *rtl, u32 vid,
375 struct rtl8366rb_vlan_4k *vlan4k)
377 struct rtl8366_smi *smi = &rtl->smi;
382 memset(vlan4k, '\0', sizeof(struct rtl8366rb_vlan_4k));
385 if (vid >= RTL8366_NUM_VIDS)
388 tableaddr = (u16 *)vlan4k;
392 err = rtl8366_smi_write_reg(smi, RTL8366S_VLAN_TABLE_WRITE_BASE, data);
396 /* write table access control word */
397 err = rtl8366_smi_write_reg(smi, RTL8366S_TABLE_ACCESS_CTRL_REG,
398 RTL8366S_TABLE_VLAN_READ_CTRL);
402 err = rtl8366_smi_read_reg(smi, RTL8366S_VLAN_TABLE_READ_BASE, &data);
409 err = rtl8366_smi_read_reg(smi, RTL8366S_VLAN_TABLE_READ_BASE + 1,
417 err = rtl8366_smi_read_reg(smi, RTL8366S_VLAN_TABLE_READ_BASE + 2,
427 static int rtl8366rb_set_vlan_4k(struct rtl8366rb *rtl,
428 const struct rtl8366rb_vlan_4k *vlan4k)
430 struct rtl8366_smi *smi = &rtl->smi;
435 if (vlan4k->vid >= RTL8366_NUM_VIDS ||
436 vlan4k->member > RTL8366_PORT_ALL ||
437 vlan4k->untag > RTL8366_PORT_ALL ||
438 vlan4k->fid > RTL8366S_FIDMAX)
441 tableaddr = (u16 *)vlan4k;
445 err = rtl8366_smi_write_reg(smi, RTL8366S_VLAN_TABLE_WRITE_BASE, data);
453 err = rtl8366_smi_write_reg(smi, RTL8366S_VLAN_TABLE_WRITE_BASE + 1,
462 err = rtl8366_smi_write_reg(smi, RTL8366S_VLAN_TABLE_WRITE_BASE + 2,
467 /* write table access control word */
468 err = rtl8366_smi_write_reg(smi, RTL8366S_TABLE_ACCESS_CTRL_REG,
469 RTL8366S_TABLE_VLAN_WRITE_CTRL);
474 static int rtl8366rb_get_vlan_mc(struct rtl8366rb *rtl, u32 index,
475 struct rtl8366rb_vlan_mc *vlanmc)
477 struct rtl8366_smi *smi = &rtl->smi;
483 memset(vlanmc, '\0', sizeof(struct rtl8366rb_vlan_mc));
485 if (index >= RTL8366_NUM_VLANS)
488 tableaddr = (u16 *)vlanmc;
490 addr = RTL8366S_VLAN_MEMCONF_BASE + (index * 3);
491 err = rtl8366_smi_read_reg(smi, addr, &data);
498 addr = RTL8366S_VLAN_MEMCONF_BASE + 1 + (index * 3);
499 err = rtl8366_smi_read_reg(smi, addr, &data);
506 addr = RTL8366S_VLAN_MEMCONF_BASE + 2 + (index * 3);
507 err = rtl8366_smi_read_reg(smi, addr, &data);
516 static int rtl8366rb_set_vlan_mc(struct rtl8366rb *rtl, u32 index,
517 const struct rtl8366rb_vlan_mc *vlanmc)
519 struct rtl8366_smi *smi = &rtl->smi;
525 if (index >= RTL8366_NUM_VLANS ||
526 vlanmc->vid >= RTL8366_NUM_VIDS ||
527 vlanmc->priority > RTL8366S_PRIORITYMAX ||
528 vlanmc->member > RTL8366_PORT_ALL ||
529 vlanmc->untag > RTL8366_PORT_ALL ||
530 vlanmc->fid > RTL8366S_FIDMAX)
533 addr = RTL8366S_VLAN_MEMCONF_BASE + (index * 3);
535 tableaddr = (u16 *)vlanmc;
538 err = rtl8366_smi_write_reg(smi, addr, data);
542 addr = RTL8366S_VLAN_MEMCONF_BASE + 1 + (index * 3);
547 err = rtl8366_smi_write_reg(smi, addr, data);
551 addr = RTL8366S_VLAN_MEMCONF_BASE + 2 + (index * 3);
556 err = rtl8366_smi_write_reg(smi, addr, data);
562 static int rtl8366rb_get_port_vlan_index(struct rtl8366rb *rtl, int port,
565 struct rtl8366_smi *smi = &rtl->smi;
569 if (port >= RTL8366_NUM_PORTS)
572 err = rtl8366_smi_read_reg(smi, RTL8366S_PORT_VLAN_CTRL_REG(port),
577 *val = (data >> RTL8366S_PORT_VLAN_CTRL_SHIFT(port)) &
578 RTL8366S_PORT_VLAN_CTRL_MASK;
584 static int rtl8366rb_get_vlan_port_pvid(struct rtl8366rb *rtl, int port,
587 struct rtl8366rb_vlan_mc vlanmc;
591 err = rtl8366rb_get_port_vlan_index(rtl, port, &index);
595 err = rtl8366rb_get_vlan_mc(rtl, index, &vlanmc);
603 static int rtl8366rb_set_port_vlan_index(struct rtl8366rb *rtl, int port,
606 struct rtl8366_smi *smi = &rtl->smi;
610 if (port >= RTL8366_NUM_PORTS || index >= RTL8366_NUM_VLANS)
613 err = rtl8366_smi_read_reg(smi, RTL8366S_PORT_VLAN_CTRL_REG(port),
618 data &= ~(RTL8366S_PORT_VLAN_CTRL_MASK <<
619 RTL8366S_PORT_VLAN_CTRL_SHIFT(port));
620 data |= (index & RTL8366S_PORT_VLAN_CTRL_MASK) <<
621 RTL8366S_PORT_VLAN_CTRL_SHIFT(port);
623 err = rtl8366_smi_write_reg(smi, RTL8366S_PORT_VLAN_CTRL_REG(port),
628 static int rtl8366rb_set_vlan_port_pvid(struct rtl8366rb *rtl, int port, int val)
631 struct rtl8366rb_vlan_mc vlanmc;
632 struct rtl8366rb_vlan_4k vlan4k;
634 if (port >= RTL8366_NUM_PORTS || val >= RTL8366_NUM_VIDS)
637 /* Updating the 4K entry; lookup it and change the port member set */
638 rtl8366rb_get_vlan_4k(rtl, val, &vlan4k);
639 vlan4k.member |= ((1 << port) | RTL8366_PORT_CPU);
640 vlan4k.untag = RTL8366_PORT_ALL_BUT_CPU;
641 rtl8366rb_set_vlan_4k(rtl, &vlan4k);
644 * For the 16 entries more work needs to be done. First see if such
645 * VID is already there and change it
647 for (i = 0; i < RTL8366_NUM_VLANS; ++i) {
648 rtl8366rb_get_vlan_mc(rtl, i, &vlanmc);
650 /* Try to find an existing vid and update port member set */
651 if (val == vlanmc.vid) {
652 vlanmc.member |= ((1 << port) | RTL8366_PORT_CPU);
653 rtl8366rb_set_vlan_mc(rtl, i, &vlanmc);
655 /* Now update PVID register settings */
656 rtl8366rb_set_port_vlan_index(rtl, port, i);
663 * PVID could not be found from vlan table. Replace unused (one that
664 * has no member ports) with new one
666 for (i = 0; i < RTL8366_NUM_VLANS; ++i) {
667 rtl8366rb_get_vlan_mc(rtl, i, &vlanmc);
670 * See if this vlan member configuration is unused. It is
671 * unused if member set contains no ports or CPU port only
673 if (!vlanmc.member || vlanmc.member == RTL8366_PORT_CPU) {
676 vlanmc.untag = RTL8366_PORT_ALL_BUT_CPU;
677 vlanmc.member = ((1 << port) | RTL8366_PORT_CPU);
680 rtl8366rb_set_vlan_mc(rtl, i, &vlanmc);
682 /* Now update PVID register settings */
683 rtl8366rb_set_port_vlan_index(rtl, port, i);
690 "All 16 vlan member configurations are in use\n");
696 static int rtl8366rb_vlan_set_vlan(struct rtl8366rb *rtl, int enable)
698 struct rtl8366_smi *smi = &rtl->smi;
701 rtl8366_smi_read_reg(smi, RTL8366_CHIP_GLOBAL_CTRL_REG, &data);
704 data |= RTL8366_CHIP_CTRL_VLAN;
706 data &= ~RTL8366_CHIP_CTRL_VLAN;
708 return rtl8366_smi_write_reg(smi, RTL8366_CHIP_GLOBAL_CTRL_REG, data);
711 static int rtl8366rb_vlan_set_4ktable(struct rtl8366rb *rtl, int enable)
713 struct rtl8366_smi *smi = &rtl->smi;
716 rtl8366_smi_read_reg(smi, RTL8366_CHIP_GLOBAL_CTRL_REG, &data);
719 data |= RTL8366_CHIP_CTRL_VLAN_4KTB;
721 data &= ~RTL8366_CHIP_CTRL_VLAN_4KTB;
723 return rtl8366_smi_write_reg(smi, RTL8366_CHIP_GLOBAL_CTRL_REG, data);
726 static int rtl8366rb_reset_vlan(struct rtl8366rb *rtl)
728 struct rtl8366rb_vlan_4k vlan4k;
729 struct rtl8366rb_vlan_mc vlanmc;
733 /* clear 16 VLAN member configuration */
739 for (i = 0; i < RTL8366_NUM_VLANS; i++) {
740 err = rtl8366rb_set_vlan_mc(rtl, i, &vlanmc);
745 /* Set a default VLAN with vid 1 to 4K table for all ports */
747 vlan4k.member = RTL8366_PORT_ALL;
748 vlan4k.untag = RTL8366_PORT_ALL;
750 err = rtl8366rb_set_vlan_4k(rtl, &vlan4k);
754 /* Set all ports PVID to default VLAN */
755 for (i = 0; i < RTL8366_NUM_PORTS; i++) {
756 err = rtl8366rb_set_vlan_port_pvid(rtl, i, 0);
764 #ifdef CONFIG_RTL8366S_PHY_DEBUG_FS
765 static int rtl8366rb_debugfs_open(struct inode *inode, struct file *file)
767 file->private_data = inode->i_private;
771 static ssize_t rtl8366rb_read_debugfs_mibs(struct file *file,
772 char __user *user_buf,
773 size_t count, loff_t *ppos)
775 struct rtl8366rb *rtl = (struct rtl8366rb *)file->private_data;
777 char *buf = rtl->buf;
779 len += snprintf(buf + len, sizeof(rtl->buf) - len, "MIB Counters:\n");
780 len += snprintf(buf + len, sizeof(rtl->buf) - len, "Counter"
782 "Port 0 \t\t Port 1 \t\t Port 2 \t\t Port 3 \t\t "
785 for (i = 0; i < 33; ++i) {
786 len += snprintf(buf + len, sizeof(rtl->buf) - len, "%d:%s ",
787 i, rtl8366rb_mib_counters[i].name);
788 for (j = 0; j < RTL8366_NUM_PORTS; ++j) {
789 unsigned long long counter = 0;
791 if (!rtl8366_get_mib_counter(rtl, i, j, &counter))
792 len += snprintf(buf + len,
793 sizeof(rtl->buf) - len,
796 len += snprintf(buf + len,
797 sizeof(rtl->buf) - len,
800 if (j != RTL8366_NUM_PORTS - 1) {
801 if (counter < 100000)
802 len += snprintf(buf + len,
803 sizeof(rtl->buf) - len,
806 len += snprintf(buf + len,
807 sizeof(rtl->buf) - len,
811 len += snprintf(buf + len, sizeof(rtl->buf) - len, "\n");
814 len += snprintf(buf + len, sizeof(rtl->buf) - len, "\n");
816 return simple_read_from_buffer(user_buf, count, ppos, buf, len);
819 static ssize_t rtl8366rb_read_debugfs_vlan(struct file *file,
820 char __user *user_buf,
821 size_t count, loff_t *ppos)
823 struct rtl8366rb *rtl = (struct rtl8366rb *)file->private_data;
825 char *buf = rtl->buf;
827 len += snprintf(buf + len, sizeof(rtl->buf) - len,
828 "VLAN Member Config:\n");
829 len += snprintf(buf + len, sizeof(rtl->buf) - len,
830 "\t id \t vid \t prio \t member \t untag \t fid "
833 for (i = 0; i < RTL8366_NUM_VLANS; ++i) {
834 struct rtl8366rb_vlan_mc vlanmc;
836 rtl8366rb_get_vlan_mc(rtl, i, &vlanmc);
838 len += snprintf(buf + len, sizeof(rtl->buf) - len,
839 "\t[%d] \t %d \t %d \t 0x%04x \t 0x%04x \t %d "
840 "\t", i, vlanmc.vid, vlanmc.priority,
841 vlanmc.member, vlanmc.untag, vlanmc.fid);
843 for (j = 0; j < RTL8366_NUM_PORTS; ++j) {
845 if (!rtl8366rb_get_port_vlan_index(rtl, j, &index)) {
847 len += snprintf(buf + len,
848 sizeof(rtl->buf) - len,
852 len += snprintf(buf + len, sizeof(rtl->buf) - len, "\n");
855 return simple_read_from_buffer(user_buf, count, ppos, buf, len);
858 static ssize_t rtl8366rb_read_debugfs_reg(struct file *file,
859 char __user *user_buf,
860 size_t count, loff_t *ppos)
862 struct rtl8366rb *rtl = (struct rtl8366rb *)file->private_data;
863 struct rtl8366_smi *smi = &rtl->smi;
864 u32 t, reg = gl_dbg_reg;
866 char *buf = rtl->buf;
868 memset(buf, '\0', sizeof(rtl->buf));
870 err = rtl8366_smi_read_reg(smi, reg, &t);
872 len += snprintf(buf, sizeof(rtl->buf),
873 "Read failed (reg: 0x%04x)\n", reg);
874 return simple_read_from_buffer(user_buf, count, ppos, buf, len);
877 len += snprintf(buf, sizeof(rtl->buf), "reg = 0x%04x, val = 0x%04x\n",
880 return simple_read_from_buffer(user_buf, count, ppos, buf, len);
883 static ssize_t rtl8366rb_write_debugfs_reg(struct file *file,
884 const char __user *user_buf,
885 size_t count, loff_t *ppos)
887 struct rtl8366rb *rtl = (struct rtl8366rb *)file->private_data;
888 struct rtl8366_smi *smi = &rtl->smi;
890 u32 reg = gl_dbg_reg;
893 char *buf = rtl->buf;
895 len = min(count, sizeof(rtl->buf) - 1);
896 if (copy_from_user(buf, user_buf, len)) {
897 dev_err(rtl->parent, "copy from user failed\n");
902 if (len > 0 && buf[len - 1] == '\n')
906 if (strict_strtoul(buf, 16, &data)) {
907 dev_err(rtl->parent, "Invalid reg value %s\n", buf);
909 err = rtl8366_smi_write_reg(smi, reg, data);
912 "writing reg 0x%04x val 0x%04lx failed\n",
920 static const struct file_operations fops_rtl8366rb_regs = {
921 .read = rtl8366rb_read_debugfs_reg,
922 .write = rtl8366rb_write_debugfs_reg,
923 .open = rtl8366rb_debugfs_open,
927 static const struct file_operations fops_rtl8366rb_vlan = {
928 .read = rtl8366rb_read_debugfs_vlan,
929 .open = rtl8366rb_debugfs_open,
933 static const struct file_operations fops_rtl8366rb_mibs = {
934 .read = rtl8366rb_read_debugfs_mibs,
935 .open = rtl8366rb_debugfs_open,
939 static void rtl8366rb_debugfs_init(struct rtl8366rb *rtl)
944 if (!rtl->debugfs_root)
945 rtl->debugfs_root = debugfs_create_dir("rtl8366rb", NULL);
947 if (!rtl->debugfs_root) {
948 dev_err(rtl->parent, "Unable to create debugfs dir\n");
951 root = rtl->debugfs_root;
953 node = debugfs_create_x16("reg", S_IRUGO | S_IWUSR, root, &gl_dbg_reg);
955 dev_err(rtl->parent, "Creating debugfs file '%s' failed\n",
960 node = debugfs_create_file("val", S_IRUGO | S_IWUSR, root, rtl,
961 &fops_rtl8366rb_regs);
963 dev_err(rtl->parent, "Creating debugfs file '%s' failed\n",
968 node = debugfs_create_file("vlan", S_IRUSR, root, rtl,
969 &fops_rtl8366rb_vlan);
971 dev_err(rtl->parent, "Creating debugfs file '%s' failed\n",
976 node = debugfs_create_file("mibs", S_IRUSR, root, rtl,
977 &fops_rtl8366rb_mibs);
979 dev_err(rtl->parent, "Creating debugfs file '%s' failed\n",
985 static void rtl8366rb_debugfs_remove(struct rtl8366rb *rtl)
987 if (rtl->debugfs_root) {
988 debugfs_remove_recursive(rtl->debugfs_root);
989 rtl->debugfs_root = NULL;
994 static inline void rtl8366rb_debugfs_init(struct rtl8366rb *rtl) {}
995 static inline void rtl8366rb_debugfs_remove(struct rtl8366rb *rtl) {}
996 #endif /* CONFIG_RTL8366S_PHY_DEBUG_FS */
998 static int rtl8366rb_sw_reset_mibs(struct switch_dev *dev,
999 const struct switch_attr *attr,
1000 struct switch_val *val)
1002 struct rtl8366rb *rtl = sw_to_rtl8366rb(dev);
1003 struct rtl8366_smi *smi = &rtl->smi;
1006 if (val->value.i == 1) {
1007 rtl8366_smi_read_reg(smi, RTL8366S_MIB_CTRL_REG, &data);
1009 rtl8366_smi_write_reg(smi, RTL8366S_MIB_CTRL_REG, data);
1015 static int rtl8366rb_sw_get_vlan_enable(struct switch_dev *dev,
1016 const struct switch_attr *attr,
1017 struct switch_val *val)
1019 struct rtl8366rb *rtl = sw_to_rtl8366rb(dev);
1020 struct rtl8366_smi *smi = &rtl->smi;
1023 if (attr->ofs == 1) {
1024 rtl8366_smi_read_reg(smi, RTL8366_CHIP_GLOBAL_CTRL_REG, &data);
1026 if (data & RTL8366_CHIP_CTRL_VLAN)
1030 } else if (attr->ofs == 2) {
1031 rtl8366_smi_read_reg(smi, RTL8366_CHIP_GLOBAL_CTRL_REG, &data);
1033 if (data & RTL8366_CHIP_CTRL_VLAN_4KTB)
1042 static int rtl8366rb_sw_get_blinkrate(struct switch_dev *dev,
1043 const struct switch_attr *attr,
1044 struct switch_val *val)
1046 struct rtl8366rb *rtl = sw_to_rtl8366rb(dev);
1047 struct rtl8366_smi *smi = &rtl->smi;
1050 rtl8366_smi_read_reg(smi, RTL8366_LED_BLINKRATE_REG, &data);
1052 val->value.i = (data & (RTL8366_LED_BLINKRATE_MASK));
1057 static int rtl8366rb_sw_set_blinkrate(struct switch_dev *dev,
1058 const struct switch_attr *attr,
1059 struct switch_val *val)
1061 struct rtl8366rb *rtl = sw_to_rtl8366rb(dev);
1062 struct rtl8366_smi *smi = &rtl->smi;
1065 if (val->value.i >= 6)
1068 rtl8366_smi_read_reg(smi, RTL8366_LED_BLINKRATE_REG, &data);
1070 data &= ~RTL8366_LED_BLINKRATE_MASK;
1071 data |= val->value.i;
1073 rtl8366_smi_write_reg(smi, RTL8366_LED_BLINKRATE_REG, data);
1078 static int rtl8366rb_sw_set_vlan_enable(struct switch_dev *dev,
1079 const struct switch_attr *attr,
1080 struct switch_val *val)
1082 struct rtl8366rb *rtl = sw_to_rtl8366rb(dev);
1085 return rtl8366rb_vlan_set_vlan(rtl, val->value.i);
1087 return rtl8366rb_vlan_set_4ktable(rtl, val->value.i);
1090 static const char *rtl8366rb_speed_str(unsigned speed)
1104 static int rtl8366rb_sw_get_port_link(struct switch_dev *dev,
1105 const struct switch_attr *attr,
1106 struct switch_val *val)
1108 struct rtl8366rb *rtl = sw_to_rtl8366rb(dev);
1109 struct rtl8366_smi *smi = &rtl->smi;
1110 u32 len = 0, data = 0;
1112 if (val->port_vlan >= RTL8366_NUM_PORTS)
1115 memset(rtl->buf, '\0', sizeof(rtl->buf));
1116 rtl8366_smi_read_reg(smi, RTL8366S_PORT_LINK_STATUS_BASE +
1117 (val->port_vlan / 2), &data);
1119 if (val->port_vlan % 2)
1122 if (data & RTL8366S_PORT_STATUS_LINK_MASK) {
1123 len = snprintf(rtl->buf, sizeof(rtl->buf),
1124 "port:%d link:up speed:%s %s-duplex %s%s%s",
1126 rtl8366rb_speed_str(data &
1127 RTL8366S_PORT_STATUS_SPEED_MASK),
1128 (data & RTL8366S_PORT_STATUS_DUPLEX_MASK) ?
1130 (data & RTL8366S_PORT_STATUS_TXPAUSE_MASK) ?
1132 (data & RTL8366S_PORT_STATUS_RXPAUSE_MASK) ?
1134 (data & RTL8366S_PORT_STATUS_AN_MASK) ?
1137 len = snprintf(rtl->buf, sizeof(rtl->buf), "port:%d link: down",
1141 val->value.s = rtl->buf;
1147 static int rtl8366rb_sw_get_vlan_info(struct switch_dev *dev,
1148 const struct switch_attr *attr,
1149 struct switch_val *val)
1153 struct rtl8366rb_vlan_mc vlanmc;
1154 struct rtl8366rb_vlan_4k vlan4k;
1155 struct rtl8366rb *rtl = sw_to_rtl8366rb(dev);
1156 char *buf = rtl->buf;
1158 if (val->port_vlan == 0 || val->port_vlan >= RTL8366_NUM_VLANS)
1161 memset(buf, '\0', sizeof(rtl->buf));
1163 rtl8366rb_get_vlan_mc(rtl, val->port_vlan, &vlanmc);
1164 rtl8366rb_get_vlan_4k(rtl, vlanmc.vid, &vlan4k);
1166 len += snprintf(buf + len, sizeof(rtl->buf) - len, "VLAN %d: Ports: ",
1169 for (i = 0; i < RTL8366_NUM_PORTS; ++i) {
1171 if (!rtl8366rb_get_port_vlan_index(rtl, i, &index) &&
1172 index == val->port_vlan)
1173 len += snprintf(buf + len, sizeof(rtl->buf) - len,
1176 len += snprintf(buf + len, sizeof(rtl->buf) - len, "\n");
1178 len += snprintf(buf + len, sizeof(rtl->buf) - len,
1179 "\t\t vid \t prio \t member \t untag \t fid\n");
1180 len += snprintf(buf + len, sizeof(rtl->buf) - len, "\tMC:\t");
1181 len += snprintf(buf + len, sizeof(rtl->buf) - len,
1182 "%d \t %d \t 0x%04x \t 0x%04x \t %d\n",
1183 vlanmc.vid, vlanmc.priority, vlanmc.member,
1184 vlanmc.untag, vlanmc.fid);
1185 len += snprintf(buf + len, sizeof(rtl->buf) - len, "\t4K:\t");
1186 len += snprintf(buf + len, sizeof(rtl->buf) - len,
1187 "%d \t \t 0x%04x \t 0x%04x \t %d",
1188 vlan4k.vid, vlan4k.member, vlan4k.untag, vlan4k.fid);
1196 static int rtl8366rb_sw_set_port_led(struct switch_dev *dev,
1197 const struct switch_attr *attr,
1198 struct switch_val *val)
1200 struct rtl8366rb *rtl = sw_to_rtl8366rb(dev);
1201 struct rtl8366_smi *smi = &rtl->smi;
1204 if (val->port_vlan >= RTL8366_NUM_PORTS)
1207 if (val->port_vlan == RTL8366_PORT_NUM_CPU) {
1208 rtl8366_smi_read_reg(smi, RTL8366_LED_BLINKRATE_REG, &data);
1209 data = (data & (~(0xF << 4))) | (val->value.i << 4);
1210 rtl8366_smi_write_reg(smi, RTL8366_LED_BLINKRATE_REG, data);
1212 rtl8366_smi_read_reg(smi, RTL8366_LED_CTRL_REG, &data);
1213 data = (data & (~(0xF << (val->port_vlan * 4)))) |
1214 (val->value.i << (val->port_vlan * 4));
1215 rtl8366_smi_write_reg(smi, RTL8366_LED_CTRL_REG, data);
1221 static int rtl8366rb_sw_get_port_led(struct switch_dev *dev,
1222 const struct switch_attr *attr,
1223 struct switch_val *val)
1225 struct rtl8366rb *rtl = sw_to_rtl8366rb(dev);
1226 struct rtl8366_smi *smi = &rtl->smi;
1229 if (val->port_vlan >= RTL8366_NUM_LEDGROUPS)
1232 rtl8366_smi_read_reg(smi, RTL8366_LED_CTRL_REG, &data);
1233 val->value.i = (data >> (val->port_vlan * 4)) & 0x000F;
1238 static int rtl8366rb_sw_reset_port_mibs(struct switch_dev *dev,
1239 const struct switch_attr *attr,
1240 struct switch_val *val)
1242 struct rtl8366rb *rtl = sw_to_rtl8366rb(dev);
1243 struct rtl8366_smi *smi = &rtl->smi;
1246 if (val->port_vlan >= RTL8366_NUM_PORTS)
1249 rtl8366_smi_read_reg(smi, RTL8366S_MIB_CTRL_REG, &data);
1250 data |= (1 << (val->port_vlan + 3));
1251 rtl8366_smi_write_reg(smi, RTL8366S_MIB_CTRL_REG, data);
1256 static int rtl8366rb_sw_get_port_mib(struct switch_dev *dev,
1257 const struct switch_attr *attr,
1258 struct switch_val *val)
1260 struct rtl8366rb *rtl = sw_to_rtl8366rb(dev);
1262 unsigned long long counter = 0;
1263 char *buf = rtl->buf;
1265 if (val->port_vlan >= RTL8366_NUM_PORTS)
1268 len += snprintf(buf + len, sizeof(rtl->buf) - len,
1269 "Port %d MIB counters\n",
1272 for (i = 0; i < RTL8366S_MIB_COUNT; ++i) {
1273 len += snprintf(buf + len, sizeof(rtl->buf) - len,
1274 "%d:%s\t", i, rtl8366rb_mib_counters[i].name);
1275 if (!rtl8366_get_mib_counter(rtl, i, val->port_vlan, &counter))
1276 len += snprintf(buf + len, sizeof(rtl->buf) - len,
1277 "[%llu]\n", counter);
1279 len += snprintf(buf + len, sizeof(rtl->buf) - len,
1288 static int rtl8366rb_sw_get_vlan_ports(struct switch_dev *dev,
1289 struct switch_val *val)
1291 struct rtl8366rb_vlan_mc vlanmc;
1292 struct rtl8366rb *rtl = sw_to_rtl8366rb(dev);
1293 struct switch_port *port;
1296 if (val->port_vlan == 0 || val->port_vlan >= RTL8366_NUM_VLANS)
1299 rtl8366rb_get_vlan_mc(rtl, val->port_vlan, &vlanmc);
1301 port = &val->value.ports[0];
1303 for (i = 0; i < RTL8366_NUM_PORTS; i++) {
1304 if (!(vlanmc.member & BIT(i)))
1308 port->flags = (vlanmc.untag & BIT(i)) ?
1309 0 : BIT(SWITCH_PORT_FLAG_TAGGED);
1316 static int rtl8366rb_sw_set_vlan_ports(struct switch_dev *dev,
1317 struct switch_val *val)
1319 struct rtl8366rb_vlan_mc vlanmc;
1320 struct rtl8366rb_vlan_4k vlan4k;
1321 struct rtl8366rb *rtl = sw_to_rtl8366rb(dev);
1322 struct switch_port *port;
1325 if (val->port_vlan == 0 || val->port_vlan >= RTL8366_NUM_VLANS)
1328 rtl8366rb_get_vlan_mc(rtl, val->port_vlan, &vlanmc);
1329 rtl8366rb_get_vlan_4k(rtl, vlanmc.vid, &vlan4k);
1334 port = &val->value.ports[0];
1335 for (i = 0; i < val->len; i++, port++) {
1336 vlanmc.member |= BIT(port->id);
1338 if (!(port->flags & BIT(SWITCH_PORT_FLAG_TAGGED)))
1339 vlanmc.untag |= BIT(port->id);
1342 vlan4k.member = vlanmc.member;
1343 vlan4k.untag = vlanmc.untag;
1345 rtl8366rb_set_vlan_mc(rtl, val->port_vlan, &vlanmc);
1346 rtl8366rb_set_vlan_4k(rtl, &vlan4k);
1350 static int rtl8366rb_sw_get_port_pvid(struct switch_dev *dev, int port, int *val)
1352 struct rtl8366rb *rtl = sw_to_rtl8366rb(dev);
1353 return rtl8366rb_get_vlan_port_pvid(rtl, port, val);
1356 static int rtl8366rb_sw_set_port_pvid(struct switch_dev *dev, int port, int val)
1358 struct rtl8366rb *rtl = sw_to_rtl8366rb(dev);
1359 return rtl8366rb_set_vlan_port_pvid(rtl, port, val);
1362 static int rtl8366rb_sw_reset_switch(struct switch_dev *dev)
1364 struct rtl8366rb *rtl = sw_to_rtl8366rb(dev);
1367 err = rtl8366rb_reset_chip(rtl);
1371 return rtl8366rb_reset_vlan(rtl);
1374 static struct switch_attr rtl8366rb_globals[] = {
1376 .type = SWITCH_TYPE_INT,
1377 .name = "enable_vlan",
1378 .description = "Enable VLAN mode",
1379 .set = rtl8366rb_sw_set_vlan_enable,
1380 .get = rtl8366rb_sw_get_vlan_enable,
1384 .type = SWITCH_TYPE_INT,
1385 .name = "enable_vlan4k",
1386 .description = "Enable VLAN 4K mode",
1387 .set = rtl8366rb_sw_set_vlan_enable,
1388 .get = rtl8366rb_sw_get_vlan_enable,
1392 .type = SWITCH_TYPE_INT,
1393 .name = "reset_mibs",
1394 .description = "Reset all MIB counters",
1395 .set = rtl8366rb_sw_reset_mibs,
1399 .type = SWITCH_TYPE_INT,
1400 .name = "blinkrate",
1401 .description = "Get/Set LED blinking rate (0 = 43ms, 1 = 84ms,"
1402 " 2 = 120ms, 3 = 170ms, 4 = 340ms, 5 = 670ms)",
1403 .set = rtl8366rb_sw_set_blinkrate,
1404 .get = rtl8366rb_sw_get_blinkrate,
1409 static struct switch_attr rtl8366rb_port[] = {
1411 .type = SWITCH_TYPE_STRING,
1413 .description = "Get port link information",
1416 .get = rtl8366rb_sw_get_port_link,
1418 .type = SWITCH_TYPE_INT,
1419 .name = "reset_mib",
1420 .description = "Reset single port MIB counters",
1422 .set = rtl8366rb_sw_reset_port_mibs,
1425 .type = SWITCH_TYPE_STRING,
1427 .description = "Get MIB counters for port",
1430 .get = rtl8366rb_sw_get_port_mib,
1432 .type = SWITCH_TYPE_INT,
1434 .description = "Get/Set port group (0 - 3) led mode (0 - 15)",
1436 .set = rtl8366rb_sw_set_port_led,
1437 .get = rtl8366rb_sw_get_port_led,
1441 static struct switch_attr rtl8366rb_vlan[] = {
1443 .type = SWITCH_TYPE_STRING,
1445 .description = "Get vlan information",
1448 .get = rtl8366rb_sw_get_vlan_info,
1453 static struct switch_dev rtl8366_switch_dev = {
1455 .cpu_port = RTL8366_PORT_NUM_CPU,
1456 .ports = RTL8366_NUM_PORTS,
1457 .vlans = RTL8366_NUM_VLANS,
1459 .attr = rtl8366rb_globals,
1460 .n_attr = ARRAY_SIZE(rtl8366rb_globals),
1463 .attr = rtl8366rb_port,
1464 .n_attr = ARRAY_SIZE(rtl8366rb_port),
1467 .attr = rtl8366rb_vlan,
1468 .n_attr = ARRAY_SIZE(rtl8366rb_vlan),
1471 .get_vlan_ports = rtl8366rb_sw_get_vlan_ports,
1472 .set_vlan_ports = rtl8366rb_sw_set_vlan_ports,
1473 .get_port_pvid = rtl8366rb_sw_get_port_pvid,
1474 .set_port_pvid = rtl8366rb_sw_set_port_pvid,
1475 .reset_switch = rtl8366rb_sw_reset_switch,
1478 static int rtl8366rb_switch_init(struct rtl8366rb *rtl)
1480 struct switch_dev *dev = &rtl->dev;
1483 memcpy(dev, &rtl8366_switch_dev, sizeof(struct switch_dev));
1485 dev->devname = dev_name(rtl->parent);
1487 err = register_switch(dev, NULL);
1489 dev_err(rtl->parent, "switch registration failed\n");
1494 static void rtl8366rb_switch_cleanup(struct rtl8366rb *rtl)
1496 unregister_switch(&rtl->dev);
1499 static int rtl8366rb_mii_read(struct mii_bus *bus, int addr, int reg)
1501 struct rtl8366_smi *smi = bus->priv;
1505 err = rtl8366rb_read_phy_reg(smi, addr, 0, reg, &val);
1512 static int rtl8366rb_mii_write(struct mii_bus *bus, int addr, int reg, u16 val)
1514 struct rtl8366_smi *smi = bus->priv;
1518 err = rtl8366rb_write_phy_reg(smi, addr, 0, reg, val);
1520 (void) rtl8366rb_read_phy_reg(smi, addr, 0, reg, &t);
1525 static int rtl8366rb_mii_bus_match(struct mii_bus *bus)
1527 return (bus->read == rtl8366rb_mii_read &&
1528 bus->write == rtl8366rb_mii_write);
1531 static int rtl8366rb_setup(struct rtl8366rb *rtl)
1535 ret = rtl8366rb_reset_chip(rtl);
1539 rtl8366rb_debugfs_init(rtl);
1543 static int rtl8366rb_detect(struct rtl8366_smi *smi)
1549 ret = rtl8366_smi_read_reg(smi, RTL8366S_CHIP_ID_REG, &chip_id);
1551 dev_err(smi->parent, "unable to read chip id\n");
1556 case RTL8366S_CHIP_ID_8366:
1559 dev_err(smi->parent, "unknown chip id (%04x)\n", chip_id);
1563 ret = rtl8366_smi_read_reg(smi, RTL8366S_CHIP_VERSION_CTRL_REG,
1566 dev_err(smi->parent, "unable to read chip version\n");
1570 dev_info(smi->parent, "RTL%04x ver. %u chip found\n",
1571 chip_id, chip_ver & RTL8366S_CHIP_VERSION_MASK);
1576 static struct rtl8366_smi_ops rtl8366rb_smi_ops = {
1577 .detect = rtl8366rb_detect,
1578 .mii_read = rtl8366rb_mii_read,
1579 .mii_write = rtl8366rb_mii_write,
1582 static int __init rtl8366rb_probe(struct platform_device *pdev)
1584 static int rtl8366_smi_version_printed;
1585 struct rtl8366rb_platform_data *pdata;
1586 struct rtl8366rb *rtl;
1587 struct rtl8366_smi *smi;
1590 if (!rtl8366_smi_version_printed++)
1591 printk(KERN_NOTICE RTL8366S_DRIVER_DESC
1592 " version " RTL8366S_DRIVER_VER"\n");
1594 pdata = pdev->dev.platform_data;
1596 dev_err(&pdev->dev, "no platform data specified\n");
1601 rtl = kzalloc(sizeof(*rtl), GFP_KERNEL);
1603 dev_err(&pdev->dev, "no memory for private data\n");
1608 rtl->parent = &pdev->dev;
1611 smi->parent = &pdev->dev;
1612 smi->gpio_sda = pdata->gpio_sda;
1613 smi->gpio_sck = pdata->gpio_sck;
1614 smi->ops = &rtl8366rb_smi_ops;
1616 err = rtl8366_smi_init(smi);
1620 platform_set_drvdata(pdev, rtl);
1622 err = rtl8366rb_setup(rtl);
1624 goto err_clear_drvdata;
1626 err = rtl8366rb_switch_init(rtl);
1628 goto err_clear_drvdata;
1633 platform_set_drvdata(pdev, NULL);
1634 rtl8366_smi_cleanup(smi);
1641 static int rtl8366rb_phy_config_init(struct phy_device *phydev)
1643 if (!rtl8366rb_mii_bus_match(phydev->bus))
1649 static int rtl8366rb_phy_config_aneg(struct phy_device *phydev)
1654 static struct phy_driver rtl8366rb_phy_driver = {
1655 .phy_id = 0x001cc960,
1656 .name = "Realtek RTL8366RB",
1657 .phy_id_mask = 0x1ffffff0,
1658 .features = PHY_GBIT_FEATURES,
1659 .config_aneg = rtl8366rb_phy_config_aneg,
1660 .config_init = rtl8366rb_phy_config_init,
1661 .read_status = genphy_read_status,
1663 .owner = THIS_MODULE,
1667 static int __devexit rtl8366rb_remove(struct platform_device *pdev)
1669 struct rtl8366rb *rtl = platform_get_drvdata(pdev);
1672 rtl8366rb_switch_cleanup(rtl);
1673 rtl8366rb_debugfs_remove(rtl);
1674 platform_set_drvdata(pdev, NULL);
1675 rtl8366_smi_cleanup(&rtl->smi);
1682 static struct platform_driver rtl8366rb_driver = {
1684 .name = RTL8366RB_DRIVER_NAME,
1685 .owner = THIS_MODULE,
1687 .probe = rtl8366rb_probe,
1688 .remove = __devexit_p(rtl8366rb_remove),
1691 static int __init rtl8366rb_module_init(void)
1694 ret = platform_driver_register(&rtl8366rb_driver);
1698 ret = phy_driver_register(&rtl8366rb_phy_driver);
1700 goto err_platform_unregister;
1704 err_platform_unregister:
1705 platform_driver_unregister(&rtl8366rb_driver);
1708 module_init(rtl8366rb_module_init);
1710 static void __exit rtl8366rb_module_exit(void)
1712 phy_driver_unregister(&rtl8366rb_phy_driver);
1713 platform_driver_unregister(&rtl8366rb_driver);
1715 module_exit(rtl8366rb_module_exit);
1717 MODULE_DESCRIPTION(RTL8366S_DRIVER_DESC);
1718 MODULE_VERSION(RTL8366S_DRIVER_VER);
1719 MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
1720 MODULE_AUTHOR("Antti Seppälä <a.seppala@gmail.com>");
1721 MODULE_LICENSE("GPL v2");
1722 MODULE_ALIAS("platform:" RTL8366RB_DRIVER_NAME);