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 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
);
468 EXPORT_SYMBOL_GPL(rtl8366_enable_vlan
);
470 static int rtl8366_enable_vlan4k(struct rtl8366_smi
*smi
, int enable
)
475 err
= smi
->ops
->enable_vlan(smi
, enable
);
479 smi
->vlan_enabled
= enable
;
482 err
= smi
->ops
->enable_vlan4k(smi
, enable
);
486 smi
->vlan4k_enabled
= enable
;
490 int rtl8366_enable_all_ports(struct rtl8366_smi
*smi
, int enable
)
495 for (port
= 0; port
< smi
->num_ports
; port
++) {
496 err
= smi
->ops
->enable_port(smi
, port
, enable
);
503 EXPORT_SYMBOL_GPL(rtl8366_enable_all_ports
);
505 int rtl8366_reset_vlan(struct rtl8366_smi
*smi
)
507 struct rtl8366_vlan_mc vlanmc
;
511 rtl8366_enable_vlan(smi
, 0);
512 rtl8366_enable_vlan4k(smi
, 0);
514 /* clear VLAN member configurations */
520 for (i
= 0; i
< smi
->num_vlan_mc
; i
++) {
521 err
= smi
->ops
->set_vlan_mc(smi
, i
, &vlanmc
);
528 EXPORT_SYMBOL_GPL(rtl8366_reset_vlan
);
530 static int rtl8366_init_vlan(struct rtl8366_smi
*smi
)
535 err
= rtl8366_reset_vlan(smi
);
539 for (port
= 0; port
< smi
->num_ports
; port
++) {
542 if (port
== smi
->cpu_port
)
543 mask
= (1 << smi
->num_ports
) - 1;
545 mask
= (1 << port
) | (1 << smi
->cpu_port
);
547 err
= rtl8366_set_vlan(smi
, (port
+ 1), mask
, mask
, 0);
551 err
= rtl8366_set_pvid(smi
, port
, (port
+ 1));
556 return rtl8366_enable_vlan(smi
, 1);
559 #ifdef CONFIG_RTL8366S_PHY_DEBUG_FS
560 int rtl8366_debugfs_open(struct inode
*inode
, struct file
*file
)
562 file
->private_data
= inode
->i_private
;
565 EXPORT_SYMBOL_GPL(rtl8366_debugfs_open
);
567 static ssize_t
rtl8366_read_debugfs_vlan_mc(struct file
*file
,
568 char __user
*user_buf
,
569 size_t count
, loff_t
*ppos
)
571 struct rtl8366_smi
*smi
= (struct rtl8366_smi
*)file
->private_data
;
573 char *buf
= smi
->buf
;
575 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
,
576 "%2s %6s %4s %6s %6s %3s\n",
577 "id", "vid","prio", "member", "untag", "fid");
579 for (i
= 0; i
< smi
->num_vlan_mc
; ++i
) {
580 struct rtl8366_vlan_mc vlanmc
;
582 smi
->ops
->get_vlan_mc(smi
, i
, &vlanmc
);
584 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
,
585 "%2d %6d %4d 0x%04x 0x%04x %3d\n",
586 i
, vlanmc
.vid
, vlanmc
.priority
,
587 vlanmc
.member
, vlanmc
.untag
, vlanmc
.fid
);
590 return simple_read_from_buffer(user_buf
, count
, ppos
, buf
, len
);
593 #define RTL8366_VLAN4K_PAGE_SIZE 64
594 #define RTL8366_VLAN4K_NUM_PAGES (4096 / RTL8366_VLAN4K_PAGE_SIZE)
596 static ssize_t
rtl8366_read_debugfs_vlan_4k(struct file
*file
,
597 char __user
*user_buf
,
598 size_t count
, loff_t
*ppos
)
600 struct rtl8366_smi
*smi
= (struct rtl8366_smi
*)file
->private_data
;
603 char *buf
= smi
->buf
;
605 if (smi
->dbg_vlan_4k_page
>= RTL8366_VLAN4K_NUM_PAGES
) {
606 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
,
607 "invalid page: %u\n", smi
->dbg_vlan_4k_page
);
608 return simple_read_from_buffer(user_buf
, count
, ppos
, buf
, len
);
611 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
,
613 "vid", "member", "untag", "fid");
615 offset
= RTL8366_VLAN4K_PAGE_SIZE
* smi
->dbg_vlan_4k_page
;
616 for (i
= 0; i
< RTL8366_VLAN4K_PAGE_SIZE
; i
++) {
617 struct rtl8366_vlan_4k vlan4k
;
619 smi
->ops
->get_vlan_4k(smi
, offset
+ i
, &vlan4k
);
621 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
,
622 "%4d 0x%04x 0x%04x %3d\n",
623 vlan4k
.vid
, vlan4k
.member
,
624 vlan4k
.untag
, vlan4k
.fid
);
627 return simple_read_from_buffer(user_buf
, count
, ppos
, buf
, len
);
630 static ssize_t
rtl8366_read_debugfs_pvid(struct file
*file
,
631 char __user
*user_buf
,
632 size_t count
, loff_t
*ppos
)
634 struct rtl8366_smi
*smi
= (struct rtl8366_smi
*)file
->private_data
;
635 char *buf
= smi
->buf
;
639 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
, "%4s %4s\n",
642 for (i
= 0; i
< smi
->num_ports
; i
++) {
646 err
= rtl8366_get_pvid(smi
, i
, &pvid
);
648 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
,
651 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
,
652 "%4d %4d\n", i
, pvid
);
655 return simple_read_from_buffer(user_buf
, count
, ppos
, buf
, len
);
658 static ssize_t
rtl8366_read_debugfs_reg(struct file
*file
,
659 char __user
*user_buf
,
660 size_t count
, loff_t
*ppos
)
662 struct rtl8366_smi
*smi
= (struct rtl8366_smi
*)file
->private_data
;
663 u32 t
, reg
= smi
->dbg_reg
;
665 char *buf
= smi
->buf
;
667 memset(buf
, '\0', sizeof(smi
->buf
));
669 err
= rtl8366_smi_read_reg(smi
, reg
, &t
);
671 len
+= snprintf(buf
, sizeof(smi
->buf
),
672 "Read failed (reg: 0x%04x)\n", reg
);
673 return simple_read_from_buffer(user_buf
, count
, ppos
, buf
, len
);
676 len
+= snprintf(buf
, sizeof(smi
->buf
), "reg = 0x%04x, val = 0x%04x\n",
679 return simple_read_from_buffer(user_buf
, count
, ppos
, buf
, len
);
682 static ssize_t
rtl8366_write_debugfs_reg(struct file
*file
,
683 const char __user
*user_buf
,
684 size_t count
, loff_t
*ppos
)
686 struct rtl8366_smi
*smi
= (struct rtl8366_smi
*)file
->private_data
;
688 u32 reg
= smi
->dbg_reg
;
691 char *buf
= smi
->buf
;
693 len
= min(count
, sizeof(smi
->buf
) - 1);
694 if (copy_from_user(buf
, user_buf
, len
)) {
695 dev_err(smi
->parent
, "copy from user failed\n");
700 if (len
> 0 && buf
[len
- 1] == '\n')
704 if (strict_strtoul(buf
, 16, &data
)) {
705 dev_err(smi
->parent
, "Invalid reg value %s\n", buf
);
707 err
= rtl8366_smi_write_reg(smi
, reg
, data
);
710 "writing reg 0x%04x val 0x%04lx failed\n",
718 static ssize_t
rtl8366_read_debugfs_mibs(struct file
*file
,
719 char __user
*user_buf
,
720 size_t count
, loff_t
*ppos
)
722 struct rtl8366_smi
*smi
= file
->private_data
;
724 char *buf
= smi
->buf
;
726 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
, "%-36s",
729 for (i
= 0; i
< smi
->num_ports
; i
++) {
732 snprintf(port_buf
, sizeof(port_buf
), "Port %d", i
);
733 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
, " %12s",
736 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
, "\n");
738 for (i
= 0; i
< smi
->num_mib_counters
; i
++) {
739 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
, "%-36s ",
740 smi
->mib_counters
[i
].name
);
741 for (j
= 0; j
< smi
->num_ports
; j
++) {
742 unsigned long long counter
= 0;
744 if (!smi
->ops
->get_mib_counter(smi
, i
, j
, &counter
))
745 len
+= snprintf(buf
+ len
,
746 sizeof(smi
->buf
) - len
,
749 len
+= snprintf(buf
+ len
,
750 sizeof(smi
->buf
) - len
,
753 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
, "\n");
756 return simple_read_from_buffer(user_buf
, count
, ppos
, buf
, len
);
759 static const struct file_operations fops_rtl8366_regs
= {
760 .read
= rtl8366_read_debugfs_reg
,
761 .write
= rtl8366_write_debugfs_reg
,
762 .open
= rtl8366_debugfs_open
,
766 static const struct file_operations fops_rtl8366_vlan_mc
= {
767 .read
= rtl8366_read_debugfs_vlan_mc
,
768 .open
= rtl8366_debugfs_open
,
772 static const struct file_operations fops_rtl8366_vlan_4k
= {
773 .read
= rtl8366_read_debugfs_vlan_4k
,
774 .open
= rtl8366_debugfs_open
,
778 static const struct file_operations fops_rtl8366_pvid
= {
779 .read
= rtl8366_read_debugfs_pvid
,
780 .open
= rtl8366_debugfs_open
,
784 static const struct file_operations fops_rtl8366_mibs
= {
785 .read
= rtl8366_read_debugfs_mibs
,
786 .open
= rtl8366_debugfs_open
,
790 static void rtl8366_debugfs_init(struct rtl8366_smi
*smi
)
795 if (!smi
->debugfs_root
)
796 smi
->debugfs_root
= debugfs_create_dir(dev_name(smi
->parent
),
799 if (!smi
->debugfs_root
) {
800 dev_err(smi
->parent
, "Unable to create debugfs dir\n");
803 root
= smi
->debugfs_root
;
805 node
= debugfs_create_x16("reg", S_IRUGO
| S_IWUSR
, root
,
808 dev_err(smi
->parent
, "Creating debugfs file '%s' failed\n",
813 node
= debugfs_create_file("val", S_IRUGO
| S_IWUSR
, root
, smi
,
816 dev_err(smi
->parent
, "Creating debugfs file '%s' failed\n",
821 node
= debugfs_create_file("vlan_mc", S_IRUSR
, root
, smi
,
822 &fops_rtl8366_vlan_mc
);
824 dev_err(smi
->parent
, "Creating debugfs file '%s' failed\n",
829 node
= debugfs_create_u8("vlan_4k_page", S_IRUGO
| S_IWUSR
, root
,
830 &smi
->dbg_vlan_4k_page
);
832 dev_err(smi
->parent
, "Creating debugfs file '%s' failed\n",
837 node
= debugfs_create_file("vlan_4k", S_IRUSR
, root
, smi
,
838 &fops_rtl8366_vlan_4k
);
840 dev_err(smi
->parent
, "Creating debugfs file '%s' failed\n",
845 node
= debugfs_create_file("pvid", S_IRUSR
, root
, smi
,
848 dev_err(smi
->parent
, "Creating debugfs file '%s' failed\n",
853 node
= debugfs_create_file("mibs", S_IRUSR
, smi
->debugfs_root
, smi
,
856 dev_err(smi
->parent
, "Creating debugfs file '%s' failed\n",
860 static void rtl8366_debugfs_remove(struct rtl8366_smi
*smi
)
862 if (smi
->debugfs_root
) {
863 debugfs_remove_recursive(smi
->debugfs_root
);
864 smi
->debugfs_root
= NULL
;
868 static inline void rtl8366_debugfs_init(struct rtl8366_smi
*smi
) {}
869 static inline void rtl8366_debugfs_remove(struct rtl8366_smi
*smi
) {}
870 #endif /* CONFIG_RTL8366S_PHY_DEBUG_FS */
872 static int rtl8366_smi_mii_init(struct rtl8366_smi
*smi
)
877 smi
->mii_bus
= mdiobus_alloc();
878 if (smi
->mii_bus
== NULL
) {
883 smi
->mii_bus
->priv
= (void *) smi
;
884 smi
->mii_bus
->name
= dev_name(smi
->parent
);
885 smi
->mii_bus
->read
= smi
->ops
->mii_read
;
886 smi
->mii_bus
->write
= smi
->ops
->mii_write
;
887 snprintf(smi
->mii_bus
->id
, MII_BUS_ID_SIZE
, "%s",
888 dev_name(smi
->parent
));
889 smi
->mii_bus
->parent
= smi
->parent
;
890 smi
->mii_bus
->phy_mask
= ~(0x1f);
891 smi
->mii_bus
->irq
= smi
->mii_irq
;
892 for (i
= 0; i
< PHY_MAX_ADDR
; i
++)
893 smi
->mii_irq
[i
] = PHY_POLL
;
895 ret
= mdiobus_register(smi
->mii_bus
);
902 mdiobus_free(smi
->mii_bus
);
907 static void rtl8366_smi_mii_cleanup(struct rtl8366_smi
*smi
)
909 mdiobus_unregister(smi
->mii_bus
);
910 mdiobus_free(smi
->mii_bus
);
913 int rtl8366_sw_get_port_pvid(struct switch_dev
*dev
, int port
, int *val
)
915 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
916 return rtl8366_get_pvid(smi
, port
, val
);
918 EXPORT_SYMBOL_GPL(rtl8366_sw_get_port_pvid
);
920 int rtl8366_sw_set_port_pvid(struct switch_dev
*dev
, int port
, int val
)
922 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
923 return rtl8366_set_pvid(smi
, port
, val
);
925 EXPORT_SYMBOL_GPL(rtl8366_sw_set_port_pvid
);
927 int rtl8366_sw_get_port_mib(struct switch_dev
*dev
,
928 const struct switch_attr
*attr
,
929 struct switch_val
*val
)
931 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
933 unsigned long long counter
= 0;
934 char *buf
= smi
->buf
;
936 if (val
->port_vlan
>= smi
->num_ports
)
939 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
,
940 "Port %d MIB counters\n",
943 for (i
= 0; i
< smi
->num_mib_counters
; ++i
) {
944 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
,
945 "%-36s: ", smi
->mib_counters
[i
].name
);
946 if (!smi
->ops
->get_mib_counter(smi
, i
, val
->port_vlan
,
948 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
,
951 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
,
959 EXPORT_SYMBOL_GPL(rtl8366_sw_get_port_mib
);
961 int rtl8366_sw_get_vlan_info(struct switch_dev
*dev
,
962 const struct switch_attr
*attr
,
963 struct switch_val
*val
)
967 struct rtl8366_vlan_4k vlan4k
;
968 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
969 char *buf
= smi
->buf
;
972 if (!smi
->ops
->is_vlan_valid(smi
, val
->port_vlan
))
975 memset(buf
, '\0', sizeof(smi
->buf
));
977 err
= smi
->ops
->get_vlan_4k(smi
, val
->port_vlan
, &vlan4k
);
981 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
,
982 "VLAN %d: Ports: '", vlan4k
.vid
);
984 for (i
= 0; i
< smi
->num_ports
; i
++) {
985 if (!(vlan4k
.member
& (1 << i
)))
988 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
, "%d%s", i
,
989 (vlan4k
.untag
& (1 << i
)) ? "" : "t");
992 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
,
993 "', members=%04x, untag=%04x, fid=%u",
994 vlan4k
.member
, vlan4k
.untag
, vlan4k
.fid
);
1001 EXPORT_SYMBOL_GPL(rtl8366_sw_get_vlan_info
);
1003 int rtl8366_sw_get_vlan_ports(struct switch_dev
*dev
, struct switch_val
*val
)
1005 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
1006 struct switch_port
*port
;
1007 struct rtl8366_vlan_4k vlan4k
;
1010 if (!smi
->ops
->is_vlan_valid(smi
, val
->port_vlan
))
1013 smi
->ops
->get_vlan_4k(smi
, val
->port_vlan
, &vlan4k
);
1015 port
= &val
->value
.ports
[0];
1017 for (i
= 0; i
< smi
->num_ports
; i
++) {
1018 if (!(vlan4k
.member
& BIT(i
)))
1022 port
->flags
= (vlan4k
.untag
& BIT(i
)) ?
1023 0 : BIT(SWITCH_PORT_FLAG_TAGGED
);
1029 EXPORT_SYMBOL_GPL(rtl8366_sw_get_vlan_ports
);
1031 int rtl8366_sw_set_vlan_ports(struct switch_dev
*dev
, struct switch_val
*val
)
1033 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
1034 struct switch_port
*port
;
1040 if (!smi
->ops
->is_vlan_valid(smi
, val
->port_vlan
))
1043 port
= &val
->value
.ports
[0];
1044 for (i
= 0; i
< val
->len
; i
++, port
++) {
1045 member
|= BIT(port
->id
);
1047 if (!(port
->flags
& BIT(SWITCH_PORT_FLAG_TAGGED
)))
1048 untag
|= BIT(port
->id
);
1051 * To ensure that we have a valid MC entry for this VLAN,
1052 * initialize the port VLAN ID here.
1054 err
= rtl8366_set_pvid(smi
, port
->id
, val
->port_vlan
);
1059 return rtl8366_set_vlan(smi
, val
->port_vlan
, member
, untag
, 0);
1061 EXPORT_SYMBOL_GPL(rtl8366_sw_set_vlan_ports
);
1063 int rtl8366_sw_get_vlan_fid(struct switch_dev
*dev
,
1064 const struct switch_attr
*attr
,
1065 struct switch_val
*val
)
1067 struct rtl8366_vlan_4k vlan4k
;
1068 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
1071 if (!smi
->ops
->is_vlan_valid(smi
, val
->port_vlan
))
1074 err
= smi
->ops
->get_vlan_4k(smi
, val
->port_vlan
, &vlan4k
);
1078 val
->value
.i
= vlan4k
.fid
;
1082 EXPORT_SYMBOL_GPL(rtl8366_sw_get_vlan_fid
);
1084 int rtl8366_sw_set_vlan_fid(struct switch_dev
*dev
,
1085 const struct switch_attr
*attr
,
1086 struct switch_val
*val
)
1088 struct rtl8366_vlan_4k vlan4k
;
1089 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
1092 if (!smi
->ops
->is_vlan_valid(smi
, val
->port_vlan
))
1095 if (val
->value
.i
< 0 || val
->value
.i
> attr
->max
)
1098 err
= smi
->ops
->get_vlan_4k(smi
, val
->port_vlan
, &vlan4k
);
1102 return rtl8366_set_vlan(smi
, val
->port_vlan
,
1107 EXPORT_SYMBOL_GPL(rtl8366_sw_set_vlan_fid
);
1109 int rtl8366_sw_get_vlan_enable(struct switch_dev
*dev
,
1110 const struct switch_attr
*attr
,
1111 struct switch_val
*val
)
1113 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
1119 val
->value
.i
= smi
->vlan_enabled
;
1121 val
->value
.i
= smi
->vlan4k_enabled
;
1125 EXPORT_SYMBOL_GPL(rtl8366_sw_get_vlan_enable
);
1127 int rtl8366_sw_set_vlan_enable(struct switch_dev
*dev
,
1128 const struct switch_attr
*attr
,
1129 struct switch_val
*val
)
1131 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
1138 err
= rtl8366_enable_vlan(smi
, val
->value
.i
);
1140 err
= rtl8366_enable_vlan4k(smi
, val
->value
.i
);
1144 EXPORT_SYMBOL_GPL(rtl8366_sw_set_vlan_enable
);
1146 struct rtl8366_smi
*rtl8366_smi_alloc(struct device
*parent
)
1148 struct rtl8366_smi
*smi
;
1152 smi
= kzalloc(sizeof(*smi
), GFP_KERNEL
);
1154 dev_err(parent
, "no memory for private data\n");
1158 smi
->parent
= parent
;
1161 EXPORT_SYMBOL_GPL(rtl8366_smi_alloc
);
1163 int rtl8366_smi_init(struct rtl8366_smi
*smi
)
1170 err
= gpio_request(smi
->gpio_sda
, dev_name(smi
->parent
));
1172 dev_err(smi
->parent
, "gpio_request failed for %u, err=%d\n",
1173 smi
->gpio_sda
, err
);
1177 err
= gpio_request(smi
->gpio_sck
, dev_name(smi
->parent
));
1179 dev_err(smi
->parent
, "gpio_request failed for %u, err=%d\n",
1180 smi
->gpio_sck
, err
);
1184 spin_lock_init(&smi
->lock
);
1186 dev_info(smi
->parent
, "using GPIO pins %u (SDA) and %u (SCK)\n",
1187 smi
->gpio_sda
, smi
->gpio_sck
);
1189 err
= smi
->ops
->detect(smi
);
1191 dev_err(smi
->parent
, "chip detection failed, err=%d\n", err
);
1195 err
= smi
->ops
->setup(smi
);
1197 dev_err(smi
->parent
, "chip setup failed, err=%d\n", err
);
1201 err
= rtl8366_init_vlan(smi
);
1203 dev_err(smi
->parent
, "VLAN initialization failed, err=%d\n",
1208 err
= rtl8366_enable_all_ports(smi
, 1);
1212 err
= rtl8366_smi_mii_init(smi
);
1216 rtl8366_debugfs_init(smi
);
1221 gpio_free(smi
->gpio_sck
);
1223 gpio_free(smi
->gpio_sda
);
1227 EXPORT_SYMBOL_GPL(rtl8366_smi_init
);
1229 void rtl8366_smi_cleanup(struct rtl8366_smi
*smi
)
1231 rtl8366_debugfs_remove(smi
);
1232 rtl8366_smi_mii_cleanup(smi
);
1233 gpio_free(smi
->gpio_sck
);
1234 gpio_free(smi
->gpio_sda
);
1236 EXPORT_SYMBOL_GPL(rtl8366_smi_cleanup
);
1238 MODULE_DESCRIPTION("Realtek RTL8366 SMI interface driver");
1239 MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
1240 MODULE_LICENSE("GPL v2");