2 * Realtek RTL8366 SMI interface driver
4 * Copyright (C) 2009-2010 Gabor Juhos <juhosg@openwrt.org>
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published
8 * by the Free Software Foundation.
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/device.h>
14 #include <linux/delay.h>
15 #include <linux/gpio.h>
16 #include <linux/spinlock.h>
17 #include <linux/skbuff.h>
19 #ifdef CONFIG_RTL8366S_PHY_DEBUG_FS
20 #include <linux/debugfs.h>
23 #include "rtl8366_smi.h"
25 #define RTL8366_SMI_ACK_RETRY_COUNT 5
26 #define RTL8366_SMI_CLK_DELAY 10 /* nsec */
28 static inline void rtl8366_smi_clk_delay(struct rtl8366_smi
*smi
)
30 ndelay(RTL8366_SMI_CLK_DELAY
);
33 static void rtl8366_smi_start(struct rtl8366_smi
*smi
)
35 unsigned int sda
= smi
->gpio_sda
;
36 unsigned int sck
= smi
->gpio_sck
;
39 * Set GPIO pins to output mode, with initial state:
42 gpio_direction_output(sck
, 0);
43 gpio_direction_output(sda
, 1);
44 rtl8366_smi_clk_delay(smi
);
46 /* CLK 1: 0 -> 1, 1 -> 0 */
47 gpio_set_value(sck
, 1);
48 rtl8366_smi_clk_delay(smi
);
49 gpio_set_value(sck
, 0);
50 rtl8366_smi_clk_delay(smi
);
53 gpio_set_value(sck
, 1);
54 rtl8366_smi_clk_delay(smi
);
55 gpio_set_value(sda
, 0);
56 rtl8366_smi_clk_delay(smi
);
57 gpio_set_value(sck
, 0);
58 rtl8366_smi_clk_delay(smi
);
59 gpio_set_value(sda
, 1);
62 static void rtl8366_smi_stop(struct rtl8366_smi
*smi
)
64 unsigned int sda
= smi
->gpio_sda
;
65 unsigned int sck
= smi
->gpio_sck
;
67 rtl8366_smi_clk_delay(smi
);
68 gpio_set_value(sda
, 0);
69 gpio_set_value(sck
, 1);
70 rtl8366_smi_clk_delay(smi
);
71 gpio_set_value(sda
, 1);
72 rtl8366_smi_clk_delay(smi
);
73 gpio_set_value(sck
, 1);
74 rtl8366_smi_clk_delay(smi
);
75 gpio_set_value(sck
, 0);
76 rtl8366_smi_clk_delay(smi
);
77 gpio_set_value(sck
, 1);
80 rtl8366_smi_clk_delay(smi
);
81 gpio_set_value(sck
, 0);
82 rtl8366_smi_clk_delay(smi
);
83 gpio_set_value(sck
, 1);
85 /* set GPIO pins to input mode */
86 gpio_direction_input(sda
);
87 gpio_direction_input(sck
);
90 static void rtl8366_smi_write_bits(struct rtl8366_smi
*smi
, u32 data
, u32 len
)
92 unsigned int sda
= smi
->gpio_sda
;
93 unsigned int sck
= smi
->gpio_sck
;
95 for (; len
> 0; len
--) {
96 rtl8366_smi_clk_delay(smi
);
99 gpio_set_value(sda
, !!(data
& ( 1 << (len
- 1))));
100 rtl8366_smi_clk_delay(smi
);
103 gpio_set_value(sck
, 1);
104 rtl8366_smi_clk_delay(smi
);
105 gpio_set_value(sck
, 0);
109 static void rtl8366_smi_read_bits(struct rtl8366_smi
*smi
, u32 len
, u32
*data
)
111 unsigned int sda
= smi
->gpio_sda
;
112 unsigned int sck
= smi
->gpio_sck
;
114 gpio_direction_input(sda
);
116 for (*data
= 0; len
> 0; len
--) {
119 rtl8366_smi_clk_delay(smi
);
122 gpio_set_value(sck
, 1);
123 rtl8366_smi_clk_delay(smi
);
124 u
= !!gpio_get_value(sda
);
125 gpio_set_value(sck
, 0);
127 *data
|= (u
<< (len
- 1));
130 gpio_direction_output(sda
, 0);
133 static int rtl8366_smi_wait_for_ack(struct rtl8366_smi
*smi
)
141 rtl8366_smi_read_bits(smi
, 1, &ack
);
145 if (++retry_cnt
> RTL8366_SMI_ACK_RETRY_COUNT
)
152 static int rtl8366_smi_write_byte(struct rtl8366_smi
*smi
, u8 data
)
154 rtl8366_smi_write_bits(smi
, data
, 8);
155 return rtl8366_smi_wait_for_ack(smi
);
158 static int rtl8366_smi_read_byte0(struct rtl8366_smi
*smi
, u8
*data
)
163 rtl8366_smi_read_bits(smi
, 8, &t
);
167 rtl8366_smi_write_bits(smi
, 0x00, 1);
172 static int rtl8366_smi_read_byte1(struct rtl8366_smi
*smi
, u8
*data
)
177 rtl8366_smi_read_bits(smi
, 8, &t
);
181 rtl8366_smi_write_bits(smi
, 0x01, 1);
186 int rtl8366_smi_read_reg(struct rtl8366_smi
*smi
, u32 addr
, u32
*data
)
193 spin_lock_irqsave(&smi
->lock
, flags
);
195 rtl8366_smi_start(smi
);
197 /* send READ command */
198 ret
= rtl8366_smi_write_byte(smi
, 0x0a << 4 | 0x04 << 1 | 0x01);
203 ret
= rtl8366_smi_write_byte(smi
, addr
& 0xff);
208 ret
= rtl8366_smi_write_byte(smi
, addr
>> 8);
213 rtl8366_smi_read_byte0(smi
, &lo
);
214 /* read DATA[15:8] */
215 rtl8366_smi_read_byte1(smi
, &hi
);
217 *data
= ((u32
) lo
) | (((u32
) hi
) << 8);
222 rtl8366_smi_stop(smi
);
223 spin_unlock_irqrestore(&smi
->lock
, flags
);
227 EXPORT_SYMBOL_GPL(rtl8366_smi_read_reg
);
229 int rtl8366_smi_write_reg(struct rtl8366_smi
*smi
, u32 addr
, u32 data
)
234 spin_lock_irqsave(&smi
->lock
, flags
);
236 rtl8366_smi_start(smi
);
238 /* send WRITE command */
239 ret
= rtl8366_smi_write_byte(smi
, 0x0a << 4 | 0x04 << 1 | 0x00);
244 ret
= rtl8366_smi_write_byte(smi
, addr
& 0xff);
249 ret
= rtl8366_smi_write_byte(smi
, addr
>> 8);
253 /* write DATA[7:0] */
254 ret
= rtl8366_smi_write_byte(smi
, data
& 0xff);
258 /* write DATA[15:8] */
259 ret
= rtl8366_smi_write_byte(smi
, data
>> 8);
266 rtl8366_smi_stop(smi
);
267 spin_unlock_irqrestore(&smi
->lock
, flags
);
271 EXPORT_SYMBOL_GPL(rtl8366_smi_write_reg
);
273 int rtl8366_smi_rmwr(struct rtl8366_smi
*smi
, u32 addr
, u32 mask
, u32 data
)
278 err
= rtl8366_smi_read_reg(smi
, addr
, &t
);
282 err
= rtl8366_smi_write_reg(smi
, addr
, (t
& ~mask
) | data
);
286 EXPORT_SYMBOL_GPL(rtl8366_smi_rmwr
);
288 static int rtl8366_mc_is_used(struct rtl8366_smi
*smi
, int mc_index
, int *used
)
294 for (i
= 0; i
< smi
->num_ports
; i
++) {
297 err
= smi
->ops
->get_mc_index(smi
, i
, &index
);
301 if (mc_index
== index
) {
310 static int rtl8366_set_vlan(struct rtl8366_smi
*smi
, int vid
, u32 member
,
313 struct rtl8366_vlan_4k vlan4k
;
317 /* Update the 4K table */
318 err
= smi
->ops
->get_vlan_4k(smi
, vid
, &vlan4k
);
322 vlan4k
.member
= member
;
323 vlan4k
.untag
= untag
;
325 err
= smi
->ops
->set_vlan_4k(smi
, &vlan4k
);
329 /* Try to find an existing MC entry for this VID */
330 for (i
= 0; i
< smi
->num_vlan_mc
; i
++) {
331 struct rtl8366_vlan_mc vlanmc
;
333 err
= smi
->ops
->get_vlan_mc(smi
, i
, &vlanmc
);
337 if (vid
== vlanmc
.vid
) {
338 /* update the MC entry */
339 vlanmc
.member
= member
;
340 vlanmc
.untag
= untag
;
343 err
= smi
->ops
->set_vlan_mc(smi
, i
, &vlanmc
);
351 static int rtl8366_get_pvid(struct rtl8366_smi
*smi
, int port
, int *val
)
353 struct rtl8366_vlan_mc vlanmc
;
357 err
= smi
->ops
->get_mc_index(smi
, port
, &index
);
361 err
= smi
->ops
->get_vlan_mc(smi
, index
, &vlanmc
);
369 static int rtl8366_set_pvid(struct rtl8366_smi
*smi
, unsigned port
,
372 struct rtl8366_vlan_mc vlanmc
;
373 struct rtl8366_vlan_4k vlan4k
;
377 /* Try to find an existing MC entry for this VID */
378 for (i
= 0; i
< smi
->num_vlan_mc
; i
++) {
379 err
= smi
->ops
->get_vlan_mc(smi
, i
, &vlanmc
);
383 if (vid
== vlanmc
.vid
) {
384 err
= smi
->ops
->set_vlan_mc(smi
, i
, &vlanmc
);
388 err
= smi
->ops
->set_mc_index(smi
, port
, i
);
393 /* We have no MC entry for this VID, try to find an empty one */
394 for (i
= 0; i
< smi
->num_vlan_mc
; i
++) {
395 err
= smi
->ops
->get_vlan_mc(smi
, i
, &vlanmc
);
399 if (vlanmc
.vid
== 0 && vlanmc
.member
== 0) {
400 /* Update the entry from the 4K table */
401 err
= smi
->ops
->get_vlan_4k(smi
, vid
, &vlan4k
);
406 vlanmc
.member
= vlan4k
.member
;
407 vlanmc
.untag
= vlan4k
.untag
;
408 vlanmc
.fid
= vlan4k
.fid
;
409 err
= smi
->ops
->set_vlan_mc(smi
, i
, &vlanmc
);
413 err
= smi
->ops
->set_mc_index(smi
, port
, i
);
418 /* MC table is full, try to find an unused entry and replace it */
419 for (i
= 0; i
< smi
->num_vlan_mc
; i
++) {
422 err
= rtl8366_mc_is_used(smi
, i
, &used
);
427 /* Update the entry from the 4K table */
428 err
= smi
->ops
->get_vlan_4k(smi
, vid
, &vlan4k
);
433 vlanmc
.member
= vlan4k
.member
;
434 vlanmc
.untag
= vlan4k
.untag
;
435 vlanmc
.fid
= vlan4k
.fid
;
436 err
= smi
->ops
->set_vlan_mc(smi
, i
, &vlanmc
);
440 err
= smi
->ops
->set_mc_index(smi
, port
, i
);
446 "all VLAN member configurations are in use\n");
451 static int rtl8366_enable_vlan(struct rtl8366_smi
*smi
, int enable
)
455 err
= smi
->ops
->enable_vlan(smi
, enable
);
459 smi
->vlan_enabled
= enable
;
462 smi
->vlan4k_enabled
= 0;
463 err
= smi
->ops
->enable_vlan4k(smi
, enable
);
469 static int rtl8366_enable_vlan4k(struct rtl8366_smi
*smi
, int enable
)
474 err
= smi
->ops
->enable_vlan(smi
, enable
);
478 smi
->vlan_enabled
= enable
;
481 err
= smi
->ops
->enable_vlan4k(smi
, enable
);
485 smi
->vlan4k_enabled
= enable
;
489 int rtl8366_reset_vlan(struct rtl8366_smi
*smi
)
491 struct rtl8366_vlan_mc vlanmc
;
495 rtl8366_enable_vlan(smi
, 0);
496 rtl8366_enable_vlan4k(smi
, 0);
498 /* clear VLAN member configurations */
504 for (i
= 0; i
< smi
->num_vlan_mc
; i
++) {
505 err
= smi
->ops
->set_vlan_mc(smi
, i
, &vlanmc
);
510 for (i
= 0; i
< smi
->num_ports
; i
++) {
511 if (i
== smi
->cpu_port
)
514 err
= rtl8366_set_vlan(smi
, (i
+ 1),
515 (1 << i
) | (1 << smi
->cpu_port
),
516 (1 << i
) | (1 << smi
->cpu_port
),
521 err
= rtl8366_set_pvid(smi
, i
, (i
+ 1));
528 EXPORT_SYMBOL_GPL(rtl8366_reset_vlan
);
530 #ifdef CONFIG_RTL8366S_PHY_DEBUG_FS
531 int rtl8366_debugfs_open(struct inode
*inode
, struct file
*file
)
533 file
->private_data
= inode
->i_private
;
536 EXPORT_SYMBOL_GPL(rtl8366_debugfs_open
);
538 static ssize_t
rtl8366_read_debugfs_vlan_mc(struct file
*file
,
539 char __user
*user_buf
,
540 size_t count
, loff_t
*ppos
)
542 struct rtl8366_smi
*smi
= (struct rtl8366_smi
*)file
->private_data
;
544 char *buf
= smi
->buf
;
546 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
,
547 "%2s %6s %4s %6s %6s %3s\n",
548 "id", "vid","prio", "member", "untag", "fid");
550 for (i
= 0; i
< smi
->num_vlan_mc
; ++i
) {
551 struct rtl8366_vlan_mc vlanmc
;
553 smi
->ops
->get_vlan_mc(smi
, i
, &vlanmc
);
555 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
,
556 "%2d %6d %4d 0x%04x 0x%04x %3d\n",
557 i
, vlanmc
.vid
, vlanmc
.priority
,
558 vlanmc
.member
, vlanmc
.untag
, vlanmc
.fid
);
561 return simple_read_from_buffer(user_buf
, count
, ppos
, buf
, len
);
564 static ssize_t
rtl8366_read_debugfs_pvid(struct file
*file
,
565 char __user
*user_buf
,
566 size_t count
, loff_t
*ppos
)
568 struct rtl8366_smi
*smi
= (struct rtl8366_smi
*)file
->private_data
;
569 char *buf
= smi
->buf
;
573 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
, "%4s %4s\n",
576 for (i
= 0; i
< smi
->num_ports
; i
++) {
580 err
= rtl8366_get_pvid(smi
, i
, &pvid
);
582 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
,
585 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
,
586 "%4d %4d\n", i
, pvid
);
589 return simple_read_from_buffer(user_buf
, count
, ppos
, buf
, len
);
592 static ssize_t
rtl8366_read_debugfs_reg(struct file
*file
,
593 char __user
*user_buf
,
594 size_t count
, loff_t
*ppos
)
596 struct rtl8366_smi
*smi
= (struct rtl8366_smi
*)file
->private_data
;
597 u32 t
, reg
= smi
->dbg_reg
;
599 char *buf
= smi
->buf
;
601 memset(buf
, '\0', sizeof(smi
->buf
));
603 err
= rtl8366_smi_read_reg(smi
, reg
, &t
);
605 len
+= snprintf(buf
, sizeof(smi
->buf
),
606 "Read failed (reg: 0x%04x)\n", reg
);
607 return simple_read_from_buffer(user_buf
, count
, ppos
, buf
, len
);
610 len
+= snprintf(buf
, sizeof(smi
->buf
), "reg = 0x%04x, val = 0x%04x\n",
613 return simple_read_from_buffer(user_buf
, count
, ppos
, buf
, len
);
616 static ssize_t
rtl8366_write_debugfs_reg(struct file
*file
,
617 const char __user
*user_buf
,
618 size_t count
, loff_t
*ppos
)
620 struct rtl8366_smi
*smi
= (struct rtl8366_smi
*)file
->private_data
;
622 u32 reg
= smi
->dbg_reg
;
625 char *buf
= smi
->buf
;
627 len
= min(count
, sizeof(smi
->buf
) - 1);
628 if (copy_from_user(buf
, user_buf
, len
)) {
629 dev_err(smi
->parent
, "copy from user failed\n");
634 if (len
> 0 && buf
[len
- 1] == '\n')
638 if (strict_strtoul(buf
, 16, &data
)) {
639 dev_err(smi
->parent
, "Invalid reg value %s\n", buf
);
641 err
= rtl8366_smi_write_reg(smi
, reg
, data
);
644 "writing reg 0x%04x val 0x%04lx failed\n",
652 static ssize_t
rtl8366_read_debugfs_mibs(struct file
*file
,
653 char __user
*user_buf
,
654 size_t count
, loff_t
*ppos
)
656 struct rtl8366_smi
*smi
= file
->private_data
;
658 char *buf
= smi
->buf
;
660 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
, "%-36s",
663 for (i
= 0; i
< smi
->num_ports
; i
++) {
666 snprintf(port_buf
, sizeof(port_buf
), "Port %d", i
);
667 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
, " %12s",
670 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
, "\n");
672 for (i
= 0; i
< smi
->num_mib_counters
; i
++) {
673 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
, "%-36s ",
674 smi
->mib_counters
[i
].name
);
675 for (j
= 0; j
< smi
->num_ports
; j
++) {
676 unsigned long long counter
= 0;
678 if (!smi
->ops
->get_mib_counter(smi
, i
, j
, &counter
))
679 len
+= snprintf(buf
+ len
,
680 sizeof(smi
->buf
) - len
,
683 len
+= snprintf(buf
+ len
,
684 sizeof(smi
->buf
) - len
,
687 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
, "\n");
690 return simple_read_from_buffer(user_buf
, count
, ppos
, buf
, len
);
693 static const struct file_operations fops_rtl8366_regs
= {
694 .read
= rtl8366_read_debugfs_reg
,
695 .write
= rtl8366_write_debugfs_reg
,
696 .open
= rtl8366_debugfs_open
,
700 static const struct file_operations fops_rtl8366_vlan_mc
= {
701 .read
= rtl8366_read_debugfs_vlan_mc
,
702 .open
= rtl8366_debugfs_open
,
706 static const struct file_operations fops_rtl8366_pvid
= {
707 .read
= rtl8366_read_debugfs_pvid
,
708 .open
= rtl8366_debugfs_open
,
712 static const struct file_operations fops_rtl8366_mibs
= {
713 .read
= rtl8366_read_debugfs_mibs
,
714 .open
= rtl8366_debugfs_open
,
718 static void rtl8366_debugfs_init(struct rtl8366_smi
*smi
)
723 if (!smi
->debugfs_root
)
724 smi
->debugfs_root
= debugfs_create_dir(dev_name(smi
->parent
),
727 if (!smi
->debugfs_root
) {
728 dev_err(smi
->parent
, "Unable to create debugfs dir\n");
731 root
= smi
->debugfs_root
;
733 node
= debugfs_create_x16("reg", S_IRUGO
| S_IWUSR
, root
,
736 dev_err(smi
->parent
, "Creating debugfs file '%s' failed\n",
741 node
= debugfs_create_file("val", S_IRUGO
| S_IWUSR
, root
, smi
,
744 dev_err(smi
->parent
, "Creating debugfs file '%s' failed\n",
749 node
= debugfs_create_file("vlan_mc", S_IRUSR
, root
, smi
,
750 &fops_rtl8366_vlan_mc
);
752 dev_err(smi
->parent
, "Creating debugfs file '%s' failed\n",
757 node
= debugfs_create_file("pvid", S_IRUSR
, root
, smi
,
760 dev_err(smi
->parent
, "Creating debugfs file '%s' failed\n",
765 node
= debugfs_create_file("mibs", S_IRUSR
, smi
->debugfs_root
, smi
,
768 dev_err(smi
->parent
, "Creating debugfs file '%s' failed\n",
772 static void rtl8366_debugfs_remove(struct rtl8366_smi
*smi
)
774 if (smi
->debugfs_root
) {
775 debugfs_remove_recursive(smi
->debugfs_root
);
776 smi
->debugfs_root
= NULL
;
780 static inline void rtl8366_debugfs_init(struct rtl8366_smi
*smi
) {}
781 static inline void rtl8366_debugfs_remove(struct rtl8366_smi
*smi
) {}
782 #endif /* CONFIG_RTL8366S_PHY_DEBUG_FS */
784 static int rtl8366_smi_mii_init(struct rtl8366_smi
*smi
)
789 smi
->mii_bus
= mdiobus_alloc();
790 if (smi
->mii_bus
== NULL
) {
795 smi
->mii_bus
->priv
= (void *) smi
;
796 smi
->mii_bus
->name
= dev_name(smi
->parent
);
797 smi
->mii_bus
->read
= smi
->ops
->mii_read
;
798 smi
->mii_bus
->write
= smi
->ops
->mii_write
;
799 snprintf(smi
->mii_bus
->id
, MII_BUS_ID_SIZE
, "%s",
800 dev_name(smi
->parent
));
801 smi
->mii_bus
->parent
= smi
->parent
;
802 smi
->mii_bus
->phy_mask
= ~(0x1f);
803 smi
->mii_bus
->irq
= smi
->mii_irq
;
804 for (i
= 0; i
< PHY_MAX_ADDR
; i
++)
805 smi
->mii_irq
[i
] = PHY_POLL
;
807 ret
= mdiobus_register(smi
->mii_bus
);
814 mdiobus_free(smi
->mii_bus
);
819 static void rtl8366_smi_mii_cleanup(struct rtl8366_smi
*smi
)
821 mdiobus_unregister(smi
->mii_bus
);
822 mdiobus_free(smi
->mii_bus
);
825 int rtl8366_sw_get_port_pvid(struct switch_dev
*dev
, int port
, int *val
)
827 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
828 return rtl8366_get_pvid(smi
, port
, val
);
830 EXPORT_SYMBOL_GPL(rtl8366_sw_get_port_pvid
);
832 int rtl8366_sw_set_port_pvid(struct switch_dev
*dev
, int port
, int val
)
834 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
835 return rtl8366_set_pvid(smi
, port
, val
);
837 EXPORT_SYMBOL_GPL(rtl8366_sw_set_port_pvid
);
839 int rtl8366_sw_get_port_mib(struct switch_dev
*dev
,
840 const struct switch_attr
*attr
,
841 struct switch_val
*val
)
843 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
845 unsigned long long counter
= 0;
846 char *buf
= smi
->buf
;
848 if (val
->port_vlan
>= smi
->num_ports
)
851 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
,
852 "Port %d MIB counters\n",
855 for (i
= 0; i
< smi
->num_mib_counters
; ++i
) {
856 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
,
857 "%-36s: ", smi
->mib_counters
[i
].name
);
858 if (!smi
->ops
->get_mib_counter(smi
, i
, val
->port_vlan
,
860 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
,
863 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
,
871 EXPORT_SYMBOL_GPL(rtl8366_sw_get_port_mib
);
873 int rtl8366_sw_get_vlan_info(struct switch_dev
*dev
,
874 const struct switch_attr
*attr
,
875 struct switch_val
*val
)
879 struct rtl8366_vlan_4k vlan4k
;
880 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
881 char *buf
= smi
->buf
;
884 if (!smi
->ops
->is_vlan_valid(smi
, val
->port_vlan
))
887 memset(buf
, '\0', sizeof(smi
->buf
));
889 err
= smi
->ops
->get_vlan_4k(smi
, val
->port_vlan
, &vlan4k
);
893 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
,
894 "VLAN %d: Ports: '", vlan4k
.vid
);
896 for (i
= 0; i
< smi
->num_ports
; i
++) {
897 if (!(vlan4k
.member
& (1 << i
)))
900 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
, "%d%s", i
,
901 (vlan4k
.untag
& (1 << i
)) ? "" : "t");
904 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
,
905 "', members=%04x, untag=%04x, fid=%u",
906 vlan4k
.member
, vlan4k
.untag
, vlan4k
.fid
);
913 EXPORT_SYMBOL_GPL(rtl8366_sw_get_vlan_info
);
915 int rtl8366_sw_get_vlan_ports(struct switch_dev
*dev
, struct switch_val
*val
)
917 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
918 struct switch_port
*port
;
919 struct rtl8366_vlan_4k vlan4k
;
922 if (!smi
->ops
->is_vlan_valid(smi
, val
->port_vlan
))
925 smi
->ops
->get_vlan_4k(smi
, val
->port_vlan
, &vlan4k
);
927 port
= &val
->value
.ports
[0];
929 for (i
= 0; i
< smi
->num_ports
; i
++) {
930 if (!(vlan4k
.member
& BIT(i
)))
934 port
->flags
= (vlan4k
.untag
& BIT(i
)) ?
935 0 : BIT(SWITCH_PORT_FLAG_TAGGED
);
941 EXPORT_SYMBOL_GPL(rtl8366_sw_get_vlan_ports
);
943 int rtl8366_sw_set_vlan_ports(struct switch_dev
*dev
, struct switch_val
*val
)
945 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
946 struct switch_port
*port
;
952 if (!smi
->ops
->is_vlan_valid(smi
, val
->port_vlan
))
955 port
= &val
->value
.ports
[0];
956 for (i
= 0; i
< val
->len
; i
++, port
++) {
957 member
|= BIT(port
->id
);
959 if (!(port
->flags
& BIT(SWITCH_PORT_FLAG_TAGGED
)))
960 untag
|= BIT(port
->id
);
963 * To ensure that we have a valid MC entry for this VLAN,
964 * initialize the port VLAN ID here.
966 err
= rtl8366_set_pvid(smi
, port
->id
, val
->port_vlan
);
971 return rtl8366_set_vlan(smi
, val
->port_vlan
, member
, untag
, 0);
973 EXPORT_SYMBOL_GPL(rtl8366_sw_set_vlan_ports
);
975 int rtl8366_sw_get_vlan_fid(struct switch_dev
*dev
,
976 const struct switch_attr
*attr
,
977 struct switch_val
*val
)
979 struct rtl8366_vlan_4k vlan4k
;
980 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
983 if (!smi
->ops
->is_vlan_valid(smi
, val
->port_vlan
))
986 err
= smi
->ops
->get_vlan_4k(smi
, val
->port_vlan
, &vlan4k
);
990 val
->value
.i
= vlan4k
.fid
;
994 EXPORT_SYMBOL_GPL(rtl8366_sw_get_vlan_fid
);
996 int rtl8366_sw_set_vlan_fid(struct switch_dev
*dev
,
997 const struct switch_attr
*attr
,
998 struct switch_val
*val
)
1000 struct rtl8366_vlan_4k vlan4k
;
1001 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
1004 if (!smi
->ops
->is_vlan_valid(smi
, val
->port_vlan
))
1007 if (val
->value
.i
< 0 || val
->value
.i
> attr
->max
)
1010 err
= smi
->ops
->get_vlan_4k(smi
, val
->port_vlan
, &vlan4k
);
1014 return rtl8366_set_vlan(smi
, val
->port_vlan
,
1019 EXPORT_SYMBOL_GPL(rtl8366_sw_set_vlan_fid
);
1021 int rtl8366_sw_get_vlan_enable(struct switch_dev
*dev
,
1022 const struct switch_attr
*attr
,
1023 struct switch_val
*val
)
1025 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
1031 val
->value
.i
= smi
->vlan_enabled
;
1033 val
->value
.i
= smi
->vlan4k_enabled
;
1037 EXPORT_SYMBOL_GPL(rtl8366_sw_get_vlan_enable
);
1039 int rtl8366_sw_set_vlan_enable(struct switch_dev
*dev
,
1040 const struct switch_attr
*attr
,
1041 struct switch_val
*val
)
1043 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
1050 err
= rtl8366_enable_vlan(smi
, val
->value
.i
);
1052 err
= rtl8366_enable_vlan4k(smi
, val
->value
.i
);
1056 EXPORT_SYMBOL_GPL(rtl8366_sw_set_vlan_enable
);
1058 struct rtl8366_smi
*rtl8366_smi_alloc(struct device
*parent
)
1060 struct rtl8366_smi
*smi
;
1064 smi
= kzalloc(sizeof(*smi
), GFP_KERNEL
);
1066 dev_err(parent
, "no memory for private data\n");
1070 smi
->parent
= parent
;
1073 EXPORT_SYMBOL_GPL(rtl8366_smi_alloc
);
1075 int rtl8366_smi_init(struct rtl8366_smi
*smi
)
1082 err
= gpio_request(smi
->gpio_sda
, dev_name(smi
->parent
));
1084 dev_err(smi
->parent
, "gpio_request failed for %u, err=%d\n",
1085 smi
->gpio_sda
, err
);
1089 err
= gpio_request(smi
->gpio_sck
, dev_name(smi
->parent
));
1091 dev_err(smi
->parent
, "gpio_request failed for %u, err=%d\n",
1092 smi
->gpio_sck
, err
);
1096 spin_lock_init(&smi
->lock
);
1098 dev_info(smi
->parent
, "using GPIO pins %u (SDA) and %u (SCK)\n",
1099 smi
->gpio_sda
, smi
->gpio_sck
);
1101 err
= smi
->ops
->detect(smi
);
1103 dev_err(smi
->parent
, "chip detection failed, err=%d\n", err
);
1107 err
= smi
->ops
->setup(smi
);
1109 dev_err(smi
->parent
, "chip setup failed, err=%d\n", err
);
1113 err
= rtl8366_smi_mii_init(smi
);
1117 rtl8366_debugfs_init(smi
);
1122 gpio_free(smi
->gpio_sck
);
1124 gpio_free(smi
->gpio_sda
);
1128 EXPORT_SYMBOL_GPL(rtl8366_smi_init
);
1130 void rtl8366_smi_cleanup(struct rtl8366_smi
*smi
)
1132 rtl8366_debugfs_remove(smi
);
1133 rtl8366_smi_mii_cleanup(smi
);
1134 gpio_free(smi
->gpio_sck
);
1135 gpio_free(smi
->gpio_sda
);
1137 EXPORT_SYMBOL_GPL(rtl8366_smi_cleanup
);
1139 MODULE_DESCRIPTION("Realtek RTL8366 SMI interface driver");
1140 MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
1141 MODULE_LICENSE("GPL v2");