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>
18 #include <linux/rtl8366.h>
20 #ifdef CONFIG_RTL8366S_PHY_DEBUG_FS
21 #include <linux/debugfs.h>
24 #include "rtl8366_smi.h"
26 #define RTL8366_SMI_ACK_RETRY_COUNT 5
27 #define RTL8366_SMI_CLK_DELAY 10 /* nsec */
29 static inline void rtl8366_smi_clk_delay(struct rtl8366_smi
*smi
)
31 ndelay(RTL8366_SMI_CLK_DELAY
);
34 static void rtl8366_smi_start(struct rtl8366_smi
*smi
)
36 unsigned int sda
= smi
->gpio_sda
;
37 unsigned int sck
= smi
->gpio_sck
;
40 * Set GPIO pins to output mode, with initial state:
43 gpio_direction_output(sck
, 0);
44 gpio_direction_output(sda
, 1);
45 rtl8366_smi_clk_delay(smi
);
47 /* CLK 1: 0 -> 1, 1 -> 0 */
48 gpio_set_value(sck
, 1);
49 rtl8366_smi_clk_delay(smi
);
50 gpio_set_value(sck
, 0);
51 rtl8366_smi_clk_delay(smi
);
54 gpio_set_value(sck
, 1);
55 rtl8366_smi_clk_delay(smi
);
56 gpio_set_value(sda
, 0);
57 rtl8366_smi_clk_delay(smi
);
58 gpio_set_value(sck
, 0);
59 rtl8366_smi_clk_delay(smi
);
60 gpio_set_value(sda
, 1);
63 static void rtl8366_smi_stop(struct rtl8366_smi
*smi
)
65 unsigned int sda
= smi
->gpio_sda
;
66 unsigned int sck
= smi
->gpio_sck
;
68 rtl8366_smi_clk_delay(smi
);
69 gpio_set_value(sda
, 0);
70 gpio_set_value(sck
, 1);
71 rtl8366_smi_clk_delay(smi
);
72 gpio_set_value(sda
, 1);
73 rtl8366_smi_clk_delay(smi
);
74 gpio_set_value(sck
, 1);
75 rtl8366_smi_clk_delay(smi
);
76 gpio_set_value(sck
, 0);
77 rtl8366_smi_clk_delay(smi
);
78 gpio_set_value(sck
, 1);
81 rtl8366_smi_clk_delay(smi
);
82 gpio_set_value(sck
, 0);
83 rtl8366_smi_clk_delay(smi
);
84 gpio_set_value(sck
, 1);
86 /* set GPIO pins to input mode */
87 gpio_direction_input(sda
);
88 gpio_direction_input(sck
);
91 static void rtl8366_smi_write_bits(struct rtl8366_smi
*smi
, u32 data
, u32 len
)
93 unsigned int sda
= smi
->gpio_sda
;
94 unsigned int sck
= smi
->gpio_sck
;
96 for (; len
> 0; len
--) {
97 rtl8366_smi_clk_delay(smi
);
100 gpio_set_value(sda
, !!(data
& ( 1 << (len
- 1))));
101 rtl8366_smi_clk_delay(smi
);
104 gpio_set_value(sck
, 1);
105 rtl8366_smi_clk_delay(smi
);
106 gpio_set_value(sck
, 0);
110 static void rtl8366_smi_read_bits(struct rtl8366_smi
*smi
, u32 len
, u32
*data
)
112 unsigned int sda
= smi
->gpio_sda
;
113 unsigned int sck
= smi
->gpio_sck
;
115 gpio_direction_input(sda
);
117 for (*data
= 0; len
> 0; len
--) {
120 rtl8366_smi_clk_delay(smi
);
123 gpio_set_value(sck
, 1);
124 rtl8366_smi_clk_delay(smi
);
125 u
= !!gpio_get_value(sda
);
126 gpio_set_value(sck
, 0);
128 *data
|= (u
<< (len
- 1));
131 gpio_direction_output(sda
, 0);
134 static int rtl8366_smi_wait_for_ack(struct rtl8366_smi
*smi
)
142 rtl8366_smi_read_bits(smi
, 1, &ack
);
146 if (++retry_cnt
> RTL8366_SMI_ACK_RETRY_COUNT
) {
147 dev_err(smi
->parent
, "ACK timeout\n");
155 static int rtl8366_smi_write_byte(struct rtl8366_smi
*smi
, u8 data
)
157 rtl8366_smi_write_bits(smi
, data
, 8);
158 return rtl8366_smi_wait_for_ack(smi
);
161 static int rtl8366_smi_read_byte0(struct rtl8366_smi
*smi
, u8
*data
)
166 rtl8366_smi_read_bits(smi
, 8, &t
);
170 rtl8366_smi_write_bits(smi
, 0x00, 1);
175 static int rtl8366_smi_read_byte1(struct rtl8366_smi
*smi
, u8
*data
)
180 rtl8366_smi_read_bits(smi
, 8, &t
);
184 rtl8366_smi_write_bits(smi
, 0x01, 1);
189 int rtl8366_smi_read_reg(struct rtl8366_smi
*smi
, u32 addr
, u32
*data
)
196 spin_lock_irqsave(&smi
->lock
, flags
);
198 rtl8366_smi_start(smi
);
200 /* send READ command */
201 ret
= rtl8366_smi_write_byte(smi
, 0x0a << 4 | 0x04 << 1 | 0x01);
206 ret
= rtl8366_smi_write_byte(smi
, addr
& 0xff);
211 ret
= rtl8366_smi_write_byte(smi
, addr
>> 8);
216 rtl8366_smi_read_byte0(smi
, &lo
);
217 /* read DATA[15:8] */
218 rtl8366_smi_read_byte1(smi
, &hi
);
220 *data
= ((u32
) lo
) | (((u32
) hi
) << 8);
225 rtl8366_smi_stop(smi
);
226 spin_unlock_irqrestore(&smi
->lock
, flags
);
230 EXPORT_SYMBOL_GPL(rtl8366_smi_read_reg
);
232 int rtl8366_smi_write_reg(struct rtl8366_smi
*smi
, u32 addr
, u32 data
)
237 spin_lock_irqsave(&smi
->lock
, flags
);
239 rtl8366_smi_start(smi
);
241 /* send WRITE command */
242 ret
= rtl8366_smi_write_byte(smi
, 0x0a << 4 | 0x04 << 1 | 0x00);
247 ret
= rtl8366_smi_write_byte(smi
, addr
& 0xff);
252 ret
= rtl8366_smi_write_byte(smi
, addr
>> 8);
256 /* write DATA[7:0] */
257 ret
= rtl8366_smi_write_byte(smi
, data
& 0xff);
261 /* write DATA[15:8] */
262 ret
= rtl8366_smi_write_byte(smi
, data
>> 8);
269 rtl8366_smi_stop(smi
);
270 spin_unlock_irqrestore(&smi
->lock
, flags
);
274 EXPORT_SYMBOL_GPL(rtl8366_smi_write_reg
);
276 int rtl8366_smi_rmwr(struct rtl8366_smi
*smi
, u32 addr
, u32 mask
, u32 data
)
281 err
= rtl8366_smi_read_reg(smi
, addr
, &t
);
285 err
= rtl8366_smi_write_reg(smi
, addr
, (t
& ~mask
) | data
);
289 EXPORT_SYMBOL_GPL(rtl8366_smi_rmwr
);
291 static int rtl8366_mc_is_used(struct rtl8366_smi
*smi
, int mc_index
, int *used
)
297 for (i
= 0; i
< smi
->num_ports
; i
++) {
300 err
= smi
->ops
->get_mc_index(smi
, i
, &index
);
304 if (mc_index
== index
) {
313 static int rtl8366_set_vlan(struct rtl8366_smi
*smi
, int vid
, u32 member
,
316 struct rtl8366_vlan_4k vlan4k
;
320 /* Update the 4K table */
321 err
= smi
->ops
->get_vlan_4k(smi
, vid
, &vlan4k
);
325 vlan4k
.member
= member
;
326 vlan4k
.untag
= untag
;
328 err
= smi
->ops
->set_vlan_4k(smi
, &vlan4k
);
332 /* Try to find an existing MC entry for this VID */
333 for (i
= 0; i
< smi
->num_vlan_mc
; i
++) {
334 struct rtl8366_vlan_mc vlanmc
;
336 err
= smi
->ops
->get_vlan_mc(smi
, i
, &vlanmc
);
340 if (vid
== vlanmc
.vid
) {
341 /* update the MC entry */
342 vlanmc
.member
= member
;
343 vlanmc
.untag
= untag
;
346 err
= smi
->ops
->set_vlan_mc(smi
, i
, &vlanmc
);
354 static int rtl8366_get_pvid(struct rtl8366_smi
*smi
, int port
, int *val
)
356 struct rtl8366_vlan_mc vlanmc
;
360 err
= smi
->ops
->get_mc_index(smi
, port
, &index
);
364 err
= smi
->ops
->get_vlan_mc(smi
, index
, &vlanmc
);
372 static int rtl8366_set_pvid(struct rtl8366_smi
*smi
, unsigned port
,
375 struct rtl8366_vlan_mc vlanmc
;
376 struct rtl8366_vlan_4k vlan4k
;
380 /* Try to find an existing MC entry for this VID */
381 for (i
= 0; i
< smi
->num_vlan_mc
; i
++) {
382 err
= smi
->ops
->get_vlan_mc(smi
, i
, &vlanmc
);
386 if (vid
== vlanmc
.vid
) {
387 err
= smi
->ops
->set_vlan_mc(smi
, i
, &vlanmc
);
391 err
= smi
->ops
->set_mc_index(smi
, port
, i
);
396 /* We have no MC entry for this VID, try to find an empty one */
397 for (i
= 0; i
< smi
->num_vlan_mc
; i
++) {
398 err
= smi
->ops
->get_vlan_mc(smi
, i
, &vlanmc
);
402 if (vlanmc
.vid
== 0 && vlanmc
.member
== 0) {
403 /* Update the entry from the 4K table */
404 err
= smi
->ops
->get_vlan_4k(smi
, vid
, &vlan4k
);
409 vlanmc
.member
= vlan4k
.member
;
410 vlanmc
.untag
= vlan4k
.untag
;
411 vlanmc
.fid
= vlan4k
.fid
;
412 err
= smi
->ops
->set_vlan_mc(smi
, i
, &vlanmc
);
416 err
= smi
->ops
->set_mc_index(smi
, port
, i
);
421 /* MC table is full, try to find an unused entry and replace it */
422 for (i
= 0; i
< smi
->num_vlan_mc
; i
++) {
425 err
= rtl8366_mc_is_used(smi
, i
, &used
);
430 /* Update the entry from the 4K table */
431 err
= smi
->ops
->get_vlan_4k(smi
, vid
, &vlan4k
);
436 vlanmc
.member
= vlan4k
.member
;
437 vlanmc
.untag
= vlan4k
.untag
;
438 vlanmc
.fid
= vlan4k
.fid
;
439 err
= smi
->ops
->set_vlan_mc(smi
, i
, &vlanmc
);
443 err
= smi
->ops
->set_mc_index(smi
, port
, i
);
449 "all VLAN member configurations are in use\n");
454 int rtl8366_enable_vlan(struct rtl8366_smi
*smi
, int enable
)
458 err
= smi
->ops
->enable_vlan(smi
, enable
);
462 smi
->vlan_enabled
= enable
;
465 smi
->vlan4k_enabled
= 0;
466 err
= smi
->ops
->enable_vlan4k(smi
, enable
);
471 EXPORT_SYMBOL_GPL(rtl8366_enable_vlan
);
473 static int rtl8366_enable_vlan4k(struct rtl8366_smi
*smi
, int enable
)
478 err
= smi
->ops
->enable_vlan(smi
, enable
);
482 smi
->vlan_enabled
= enable
;
485 err
= smi
->ops
->enable_vlan4k(smi
, enable
);
489 smi
->vlan4k_enabled
= enable
;
493 int rtl8366_enable_all_ports(struct rtl8366_smi
*smi
, int enable
)
498 for (port
= 0; port
< smi
->num_ports
; port
++) {
499 err
= smi
->ops
->enable_port(smi
, port
, enable
);
506 EXPORT_SYMBOL_GPL(rtl8366_enable_all_ports
);
508 int rtl8366_reset_vlan(struct rtl8366_smi
*smi
)
510 struct rtl8366_vlan_mc vlanmc
;
514 rtl8366_enable_vlan(smi
, 0);
515 rtl8366_enable_vlan4k(smi
, 0);
517 /* clear VLAN member configurations */
523 for (i
= 0; i
< smi
->num_vlan_mc
; i
++) {
524 err
= smi
->ops
->set_vlan_mc(smi
, i
, &vlanmc
);
531 EXPORT_SYMBOL_GPL(rtl8366_reset_vlan
);
533 static int rtl8366_init_vlan(struct rtl8366_smi
*smi
)
538 err
= rtl8366_reset_vlan(smi
);
542 for (port
= 0; port
< smi
->num_ports
; port
++) {
545 if (port
== smi
->cpu_port
)
546 mask
= (1 << smi
->num_ports
) - 1;
548 mask
= (1 << port
) | (1 << smi
->cpu_port
);
550 err
= rtl8366_set_vlan(smi
, (port
+ 1), mask
, mask
, 0);
554 err
= rtl8366_set_pvid(smi
, port
, (port
+ 1));
559 return rtl8366_enable_vlan(smi
, 1);
562 #ifdef CONFIG_RTL8366S_PHY_DEBUG_FS
563 int rtl8366_debugfs_open(struct inode
*inode
, struct file
*file
)
565 file
->private_data
= inode
->i_private
;
568 EXPORT_SYMBOL_GPL(rtl8366_debugfs_open
);
570 static ssize_t
rtl8366_read_debugfs_vlan_mc(struct file
*file
,
571 char __user
*user_buf
,
572 size_t count
, loff_t
*ppos
)
574 struct rtl8366_smi
*smi
= (struct rtl8366_smi
*)file
->private_data
;
576 char *buf
= smi
->buf
;
578 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
,
579 "%2s %6s %4s %6s %6s %3s\n",
580 "id", "vid","prio", "member", "untag", "fid");
582 for (i
= 0; i
< smi
->num_vlan_mc
; ++i
) {
583 struct rtl8366_vlan_mc vlanmc
;
585 smi
->ops
->get_vlan_mc(smi
, i
, &vlanmc
);
587 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
,
588 "%2d %6d %4d 0x%04x 0x%04x %3d\n",
589 i
, vlanmc
.vid
, vlanmc
.priority
,
590 vlanmc
.member
, vlanmc
.untag
, vlanmc
.fid
);
593 return simple_read_from_buffer(user_buf
, count
, ppos
, buf
, len
);
596 #define RTL8366_VLAN4K_PAGE_SIZE 64
597 #define RTL8366_VLAN4K_NUM_PAGES (4096 / RTL8366_VLAN4K_PAGE_SIZE)
599 static ssize_t
rtl8366_read_debugfs_vlan_4k(struct file
*file
,
600 char __user
*user_buf
,
601 size_t count
, loff_t
*ppos
)
603 struct rtl8366_smi
*smi
= (struct rtl8366_smi
*)file
->private_data
;
606 char *buf
= smi
->buf
;
608 if (smi
->dbg_vlan_4k_page
>= RTL8366_VLAN4K_NUM_PAGES
) {
609 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
,
610 "invalid page: %u\n", smi
->dbg_vlan_4k_page
);
611 return simple_read_from_buffer(user_buf
, count
, ppos
, buf
, len
);
614 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
,
616 "vid", "member", "untag", "fid");
618 offset
= RTL8366_VLAN4K_PAGE_SIZE
* smi
->dbg_vlan_4k_page
;
619 for (i
= 0; i
< RTL8366_VLAN4K_PAGE_SIZE
; i
++) {
620 struct rtl8366_vlan_4k vlan4k
;
622 smi
->ops
->get_vlan_4k(smi
, offset
+ i
, &vlan4k
);
624 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
,
625 "%4d 0x%04x 0x%04x %3d\n",
626 vlan4k
.vid
, vlan4k
.member
,
627 vlan4k
.untag
, vlan4k
.fid
);
630 return simple_read_from_buffer(user_buf
, count
, ppos
, buf
, len
);
633 static ssize_t
rtl8366_read_debugfs_pvid(struct file
*file
,
634 char __user
*user_buf
,
635 size_t count
, loff_t
*ppos
)
637 struct rtl8366_smi
*smi
= (struct rtl8366_smi
*)file
->private_data
;
638 char *buf
= smi
->buf
;
642 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
, "%4s %4s\n",
645 for (i
= 0; i
< smi
->num_ports
; i
++) {
649 err
= rtl8366_get_pvid(smi
, i
, &pvid
);
651 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
,
654 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
,
655 "%4d %4d\n", i
, pvid
);
658 return simple_read_from_buffer(user_buf
, count
, ppos
, buf
, len
);
661 static ssize_t
rtl8366_read_debugfs_reg(struct file
*file
,
662 char __user
*user_buf
,
663 size_t count
, loff_t
*ppos
)
665 struct rtl8366_smi
*smi
= (struct rtl8366_smi
*)file
->private_data
;
666 u32 t
, reg
= smi
->dbg_reg
;
668 char *buf
= smi
->buf
;
670 memset(buf
, '\0', sizeof(smi
->buf
));
672 err
= rtl8366_smi_read_reg(smi
, reg
, &t
);
674 len
+= snprintf(buf
, sizeof(smi
->buf
),
675 "Read failed (reg: 0x%04x)\n", reg
);
676 return simple_read_from_buffer(user_buf
, count
, ppos
, buf
, len
);
679 len
+= snprintf(buf
, sizeof(smi
->buf
), "reg = 0x%04x, val = 0x%04x\n",
682 return simple_read_from_buffer(user_buf
, count
, ppos
, buf
, len
);
685 static ssize_t
rtl8366_write_debugfs_reg(struct file
*file
,
686 const char __user
*user_buf
,
687 size_t count
, loff_t
*ppos
)
689 struct rtl8366_smi
*smi
= (struct rtl8366_smi
*)file
->private_data
;
691 u32 reg
= smi
->dbg_reg
;
694 char *buf
= smi
->buf
;
696 len
= min(count
, sizeof(smi
->buf
) - 1);
697 if (copy_from_user(buf
, user_buf
, len
)) {
698 dev_err(smi
->parent
, "copy from user failed\n");
703 if (len
> 0 && buf
[len
- 1] == '\n')
707 if (strict_strtoul(buf
, 16, &data
)) {
708 dev_err(smi
->parent
, "Invalid reg value %s\n", buf
);
710 err
= rtl8366_smi_write_reg(smi
, reg
, data
);
713 "writing reg 0x%04x val 0x%04lx failed\n",
721 static ssize_t
rtl8366_read_debugfs_mibs(struct file
*file
,
722 char __user
*user_buf
,
723 size_t count
, loff_t
*ppos
)
725 struct rtl8366_smi
*smi
= file
->private_data
;
727 char *buf
= smi
->buf
;
729 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
, "%-36s",
732 for (i
= 0; i
< smi
->num_ports
; i
++) {
735 snprintf(port_buf
, sizeof(port_buf
), "Port %d", i
);
736 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
, " %12s",
739 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
, "\n");
741 for (i
= 0; i
< smi
->num_mib_counters
; i
++) {
742 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
, "%-36s ",
743 smi
->mib_counters
[i
].name
);
744 for (j
= 0; j
< smi
->num_ports
; j
++) {
745 unsigned long long counter
= 0;
747 if (!smi
->ops
->get_mib_counter(smi
, i
, j
, &counter
))
748 len
+= snprintf(buf
+ len
,
749 sizeof(smi
->buf
) - len
,
752 len
+= snprintf(buf
+ len
,
753 sizeof(smi
->buf
) - len
,
756 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
, "\n");
759 return simple_read_from_buffer(user_buf
, count
, ppos
, buf
, len
);
762 static const struct file_operations fops_rtl8366_regs
= {
763 .read
= rtl8366_read_debugfs_reg
,
764 .write
= rtl8366_write_debugfs_reg
,
765 .open
= rtl8366_debugfs_open
,
769 static const struct file_operations fops_rtl8366_vlan_mc
= {
770 .read
= rtl8366_read_debugfs_vlan_mc
,
771 .open
= rtl8366_debugfs_open
,
775 static const struct file_operations fops_rtl8366_vlan_4k
= {
776 .read
= rtl8366_read_debugfs_vlan_4k
,
777 .open
= rtl8366_debugfs_open
,
781 static const struct file_operations fops_rtl8366_pvid
= {
782 .read
= rtl8366_read_debugfs_pvid
,
783 .open
= rtl8366_debugfs_open
,
787 static const struct file_operations fops_rtl8366_mibs
= {
788 .read
= rtl8366_read_debugfs_mibs
,
789 .open
= rtl8366_debugfs_open
,
793 static void rtl8366_debugfs_init(struct rtl8366_smi
*smi
)
798 if (!smi
->debugfs_root
)
799 smi
->debugfs_root
= debugfs_create_dir(dev_name(smi
->parent
),
802 if (!smi
->debugfs_root
) {
803 dev_err(smi
->parent
, "Unable to create debugfs dir\n");
806 root
= smi
->debugfs_root
;
808 node
= debugfs_create_x16("reg", S_IRUGO
| S_IWUSR
, root
,
811 dev_err(smi
->parent
, "Creating debugfs file '%s' failed\n",
816 node
= debugfs_create_file("val", S_IRUGO
| S_IWUSR
, root
, smi
,
819 dev_err(smi
->parent
, "Creating debugfs file '%s' failed\n",
824 node
= debugfs_create_file("vlan_mc", S_IRUSR
, root
, smi
,
825 &fops_rtl8366_vlan_mc
);
827 dev_err(smi
->parent
, "Creating debugfs file '%s' failed\n",
832 node
= debugfs_create_u8("vlan_4k_page", S_IRUGO
| S_IWUSR
, root
,
833 &smi
->dbg_vlan_4k_page
);
835 dev_err(smi
->parent
, "Creating debugfs file '%s' failed\n",
840 node
= debugfs_create_file("vlan_4k", S_IRUSR
, root
, smi
,
841 &fops_rtl8366_vlan_4k
);
843 dev_err(smi
->parent
, "Creating debugfs file '%s' failed\n",
848 node
= debugfs_create_file("pvid", S_IRUSR
, root
, smi
,
851 dev_err(smi
->parent
, "Creating debugfs file '%s' failed\n",
856 node
= debugfs_create_file("mibs", S_IRUSR
, smi
->debugfs_root
, smi
,
859 dev_err(smi
->parent
, "Creating debugfs file '%s' failed\n",
863 static void rtl8366_debugfs_remove(struct rtl8366_smi
*smi
)
865 if (smi
->debugfs_root
) {
866 debugfs_remove_recursive(smi
->debugfs_root
);
867 smi
->debugfs_root
= NULL
;
871 static inline void rtl8366_debugfs_init(struct rtl8366_smi
*smi
) {}
872 static inline void rtl8366_debugfs_remove(struct rtl8366_smi
*smi
) {}
873 #endif /* CONFIG_RTL8366S_PHY_DEBUG_FS */
875 static int rtl8366_smi_mii_init(struct rtl8366_smi
*smi
)
880 smi
->mii_bus
= mdiobus_alloc();
881 if (smi
->mii_bus
== NULL
) {
886 smi
->mii_bus
->priv
= (void *) smi
;
887 smi
->mii_bus
->name
= dev_name(smi
->parent
);
888 smi
->mii_bus
->read
= smi
->ops
->mii_read
;
889 smi
->mii_bus
->write
= smi
->ops
->mii_write
;
890 snprintf(smi
->mii_bus
->id
, MII_BUS_ID_SIZE
, "%s",
891 dev_name(smi
->parent
));
892 smi
->mii_bus
->parent
= smi
->parent
;
893 smi
->mii_bus
->phy_mask
= ~(0x1f);
894 smi
->mii_bus
->irq
= smi
->mii_irq
;
895 for (i
= 0; i
< PHY_MAX_ADDR
; i
++)
896 smi
->mii_irq
[i
] = PHY_POLL
;
898 ret
= mdiobus_register(smi
->mii_bus
);
905 mdiobus_free(smi
->mii_bus
);
910 static void rtl8366_smi_mii_cleanup(struct rtl8366_smi
*smi
)
912 mdiobus_unregister(smi
->mii_bus
);
913 mdiobus_free(smi
->mii_bus
);
916 int rtl8366_sw_get_port_pvid(struct switch_dev
*dev
, int port
, int *val
)
918 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
919 return rtl8366_get_pvid(smi
, port
, val
);
921 EXPORT_SYMBOL_GPL(rtl8366_sw_get_port_pvid
);
923 int rtl8366_sw_set_port_pvid(struct switch_dev
*dev
, int port
, int val
)
925 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
926 return rtl8366_set_pvid(smi
, port
, val
);
928 EXPORT_SYMBOL_GPL(rtl8366_sw_set_port_pvid
);
930 int rtl8366_sw_get_port_mib(struct switch_dev
*dev
,
931 const struct switch_attr
*attr
,
932 struct switch_val
*val
)
934 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
936 unsigned long long counter
= 0;
937 char *buf
= smi
->buf
;
939 if (val
->port_vlan
>= smi
->num_ports
)
942 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
,
943 "Port %d MIB counters\n",
946 for (i
= 0; i
< smi
->num_mib_counters
; ++i
) {
947 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
,
948 "%-36s: ", smi
->mib_counters
[i
].name
);
949 if (!smi
->ops
->get_mib_counter(smi
, i
, val
->port_vlan
,
951 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
,
954 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
,
962 EXPORT_SYMBOL_GPL(rtl8366_sw_get_port_mib
);
964 int rtl8366_sw_get_vlan_info(struct switch_dev
*dev
,
965 const struct switch_attr
*attr
,
966 struct switch_val
*val
)
970 struct rtl8366_vlan_4k vlan4k
;
971 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
972 char *buf
= smi
->buf
;
975 if (!smi
->ops
->is_vlan_valid(smi
, val
->port_vlan
))
978 memset(buf
, '\0', sizeof(smi
->buf
));
980 err
= smi
->ops
->get_vlan_4k(smi
, val
->port_vlan
, &vlan4k
);
984 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
,
985 "VLAN %d: Ports: '", vlan4k
.vid
);
987 for (i
= 0; i
< smi
->num_ports
; i
++) {
988 if (!(vlan4k
.member
& (1 << i
)))
991 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
, "%d%s", i
,
992 (vlan4k
.untag
& (1 << i
)) ? "" : "t");
995 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
,
996 "', members=%04x, untag=%04x, fid=%u",
997 vlan4k
.member
, vlan4k
.untag
, vlan4k
.fid
);
1004 EXPORT_SYMBOL_GPL(rtl8366_sw_get_vlan_info
);
1006 int rtl8366_sw_get_vlan_ports(struct switch_dev
*dev
, struct switch_val
*val
)
1008 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
1009 struct switch_port
*port
;
1010 struct rtl8366_vlan_4k vlan4k
;
1013 if (!smi
->ops
->is_vlan_valid(smi
, val
->port_vlan
))
1016 smi
->ops
->get_vlan_4k(smi
, val
->port_vlan
, &vlan4k
);
1018 port
= &val
->value
.ports
[0];
1020 for (i
= 0; i
< smi
->num_ports
; i
++) {
1021 if (!(vlan4k
.member
& BIT(i
)))
1025 port
->flags
= (vlan4k
.untag
& BIT(i
)) ?
1026 0 : BIT(SWITCH_PORT_FLAG_TAGGED
);
1032 EXPORT_SYMBOL_GPL(rtl8366_sw_get_vlan_ports
);
1034 int rtl8366_sw_set_vlan_ports(struct switch_dev
*dev
, struct switch_val
*val
)
1036 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
1037 struct switch_port
*port
;
1043 if (!smi
->ops
->is_vlan_valid(smi
, val
->port_vlan
))
1046 port
= &val
->value
.ports
[0];
1047 for (i
= 0; i
< val
->len
; i
++, port
++) {
1048 member
|= BIT(port
->id
);
1050 if (!(port
->flags
& BIT(SWITCH_PORT_FLAG_TAGGED
)))
1051 untag
|= BIT(port
->id
);
1054 * To ensure that we have a valid MC entry for this VLAN,
1055 * initialize the port VLAN ID here.
1057 err
= rtl8366_set_pvid(smi
, port
->id
, val
->port_vlan
);
1062 return rtl8366_set_vlan(smi
, val
->port_vlan
, member
, untag
, 0);
1064 EXPORT_SYMBOL_GPL(rtl8366_sw_set_vlan_ports
);
1066 int rtl8366_sw_get_vlan_fid(struct switch_dev
*dev
,
1067 const struct switch_attr
*attr
,
1068 struct switch_val
*val
)
1070 struct rtl8366_vlan_4k vlan4k
;
1071 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
1074 if (!smi
->ops
->is_vlan_valid(smi
, val
->port_vlan
))
1077 err
= smi
->ops
->get_vlan_4k(smi
, val
->port_vlan
, &vlan4k
);
1081 val
->value
.i
= vlan4k
.fid
;
1085 EXPORT_SYMBOL_GPL(rtl8366_sw_get_vlan_fid
);
1087 int rtl8366_sw_set_vlan_fid(struct switch_dev
*dev
,
1088 const struct switch_attr
*attr
,
1089 struct switch_val
*val
)
1091 struct rtl8366_vlan_4k vlan4k
;
1092 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
1095 if (!smi
->ops
->is_vlan_valid(smi
, val
->port_vlan
))
1098 if (val
->value
.i
< 0 || val
->value
.i
> attr
->max
)
1101 err
= smi
->ops
->get_vlan_4k(smi
, val
->port_vlan
, &vlan4k
);
1105 return rtl8366_set_vlan(smi
, val
->port_vlan
,
1110 EXPORT_SYMBOL_GPL(rtl8366_sw_set_vlan_fid
);
1112 int rtl8366_sw_get_vlan_enable(struct switch_dev
*dev
,
1113 const struct switch_attr
*attr
,
1114 struct switch_val
*val
)
1116 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
1122 val
->value
.i
= smi
->vlan_enabled
;
1124 val
->value
.i
= smi
->vlan4k_enabled
;
1128 EXPORT_SYMBOL_GPL(rtl8366_sw_get_vlan_enable
);
1130 int rtl8366_sw_set_vlan_enable(struct switch_dev
*dev
,
1131 const struct switch_attr
*attr
,
1132 struct switch_val
*val
)
1134 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
1141 err
= rtl8366_enable_vlan(smi
, val
->value
.i
);
1143 err
= rtl8366_enable_vlan4k(smi
, val
->value
.i
);
1147 EXPORT_SYMBOL_GPL(rtl8366_sw_set_vlan_enable
);
1149 struct rtl8366_smi
*rtl8366_smi_alloc(struct device
*parent
)
1151 struct rtl8366_smi
*smi
;
1155 smi
= kzalloc(sizeof(*smi
), GFP_KERNEL
);
1157 dev_err(parent
, "no memory for private data\n");
1161 smi
->parent
= parent
;
1164 EXPORT_SYMBOL_GPL(rtl8366_smi_alloc
);
1166 static int __rtl8366_smi_init(struct rtl8366_smi
*smi
, const char *name
)
1170 err
= gpio_request(smi
->gpio_sda
, name
);
1172 printk(KERN_ERR
"rtl8366_smi: gpio_request failed for %u, err=%d\n",
1173 smi
->gpio_sda
, err
);
1177 err
= gpio_request(smi
->gpio_sck
, name
);
1179 printk(KERN_ERR
"rtl8366_smi: gpio_request failed for %u, err=%d\n",
1180 smi
->gpio_sck
, err
);
1184 spin_lock_init(&smi
->lock
);
1188 gpio_free(smi
->gpio_sda
);
1193 static void __rtl8366_smi_cleanup(struct rtl8366_smi
*smi
)
1195 gpio_free(smi
->gpio_sck
);
1196 gpio_free(smi
->gpio_sda
);
1199 enum rtl8366_type
rtl8366_smi_detect(struct rtl8366_platform_data
*pdata
)
1201 static struct rtl8366_smi smi
;
1202 enum rtl8366_type type
= RTL8366_TYPE_UNKNOWN
;
1205 memset(&smi
, 0, sizeof(smi
));
1206 smi
.gpio_sda
= pdata
->gpio_sda
;
1207 smi
.gpio_sck
= pdata
->gpio_sck
;
1209 if (__rtl8366_smi_init(&smi
, "rtl8366"))
1212 if (rtl8366_smi_read_reg(&smi
, 0x5c, ®
))
1217 printk("Found an RTL8366S switch\n");
1218 type
= RTL8366_TYPE_S
;
1221 printk("Found an RTL8366RB switch\n");
1222 type
= RTL8366_TYPE_RB
;
1225 printk("Found an Unknown RTL8366 switch (id=0x%04x)\n", reg
);
1230 __rtl8366_smi_cleanup(&smi
);
1235 int rtl8366_smi_init(struct rtl8366_smi
*smi
)
1242 err
= __rtl8366_smi_init(smi
, dev_name(smi
->parent
));
1246 spin_lock_init(&smi
->lock
);
1248 dev_info(smi
->parent
, "using GPIO pins %u (SDA) and %u (SCK)\n",
1249 smi
->gpio_sda
, smi
->gpio_sck
);
1251 err
= smi
->ops
->detect(smi
);
1253 dev_err(smi
->parent
, "chip detection failed, err=%d\n", err
);
1257 err
= smi
->ops
->setup(smi
);
1259 dev_err(smi
->parent
, "chip setup failed, err=%d\n", err
);
1263 err
= rtl8366_init_vlan(smi
);
1265 dev_err(smi
->parent
, "VLAN initialization failed, err=%d\n",
1270 err
= rtl8366_enable_all_ports(smi
, 1);
1274 err
= rtl8366_smi_mii_init(smi
);
1278 rtl8366_debugfs_init(smi
);
1283 __rtl8366_smi_cleanup(smi
);
1287 EXPORT_SYMBOL_GPL(rtl8366_smi_init
);
1289 void rtl8366_smi_cleanup(struct rtl8366_smi
*smi
)
1291 rtl8366_debugfs_remove(smi
);
1292 rtl8366_smi_mii_cleanup(smi
);
1293 gpio_free(smi
->gpio_sck
);
1294 gpio_free(smi
->gpio_sda
);
1296 EXPORT_SYMBOL_GPL(rtl8366_smi_cleanup
);
1298 MODULE_DESCRIPTION("Realtek RTL8366 SMI interface driver");
1299 MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
1300 MODULE_LICENSE("GPL v2");