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
28 static inline void rtl8366_smi_clk_delay(struct rtl8366_smi
*smi
)
30 ndelay(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
) {
146 dev_err(smi
->parent
, "ACK timeout\n");
154 static int rtl8366_smi_write_byte(struct rtl8366_smi
*smi
, u8 data
)
156 rtl8366_smi_write_bits(smi
, data
, 8);
157 return rtl8366_smi_wait_for_ack(smi
);
160 static int rtl8366_smi_read_byte0(struct rtl8366_smi
*smi
, u8
*data
)
165 rtl8366_smi_read_bits(smi
, 8, &t
);
169 rtl8366_smi_write_bits(smi
, 0x00, 1);
174 static int rtl8366_smi_read_byte1(struct rtl8366_smi
*smi
, u8
*data
)
179 rtl8366_smi_read_bits(smi
, 8, &t
);
183 rtl8366_smi_write_bits(smi
, 0x01, 1);
188 int rtl8366_smi_read_reg(struct rtl8366_smi
*smi
, u32 addr
, u32
*data
)
195 spin_lock_irqsave(&smi
->lock
, flags
);
197 rtl8366_smi_start(smi
);
199 /* send READ command */
200 ret
= rtl8366_smi_write_byte(smi
, smi
->cmd_read
);
205 ret
= rtl8366_smi_write_byte(smi
, addr
& 0xff);
210 ret
= rtl8366_smi_write_byte(smi
, addr
>> 8);
215 rtl8366_smi_read_byte0(smi
, &lo
);
216 /* read DATA[15:8] */
217 rtl8366_smi_read_byte1(smi
, &hi
);
219 *data
= ((u32
) lo
) | (((u32
) hi
) << 8);
224 rtl8366_smi_stop(smi
);
225 spin_unlock_irqrestore(&smi
->lock
, flags
);
229 EXPORT_SYMBOL_GPL(rtl8366_smi_read_reg
);
231 int rtl8366_smi_write_reg(struct rtl8366_smi
*smi
, u32 addr
, u32 data
)
236 spin_lock_irqsave(&smi
->lock
, flags
);
238 rtl8366_smi_start(smi
);
240 /* send WRITE command */
241 ret
= rtl8366_smi_write_byte(smi
, smi
->cmd_write
);
246 ret
= rtl8366_smi_write_byte(smi
, addr
& 0xff);
251 ret
= rtl8366_smi_write_byte(smi
, addr
>> 8);
255 /* write DATA[7:0] */
256 ret
= rtl8366_smi_write_byte(smi
, data
& 0xff);
260 /* write DATA[15:8] */
261 ret
= rtl8366_smi_write_byte(smi
, data
>> 8);
268 rtl8366_smi_stop(smi
);
269 spin_unlock_irqrestore(&smi
->lock
, flags
);
273 EXPORT_SYMBOL_GPL(rtl8366_smi_write_reg
);
275 int rtl8366_smi_rmwr(struct rtl8366_smi
*smi
, u32 addr
, u32 mask
, u32 data
)
280 err
= rtl8366_smi_read_reg(smi
, addr
, &t
);
284 err
= rtl8366_smi_write_reg(smi
, addr
, (t
& ~mask
) | data
);
288 EXPORT_SYMBOL_GPL(rtl8366_smi_rmwr
);
290 static int rtl8366_mc_is_used(struct rtl8366_smi
*smi
, int mc_index
, int *used
)
296 for (i
= 0; i
< smi
->num_ports
; i
++) {
299 err
= smi
->ops
->get_mc_index(smi
, i
, &index
);
303 if (mc_index
== index
) {
312 static int rtl8366_set_vlan(struct rtl8366_smi
*smi
, int vid
, u32 member
,
315 struct rtl8366_vlan_4k vlan4k
;
319 /* Update the 4K table */
320 err
= smi
->ops
->get_vlan_4k(smi
, vid
, &vlan4k
);
324 vlan4k
.member
= member
;
325 vlan4k
.untag
= untag
;
327 err
= smi
->ops
->set_vlan_4k(smi
, &vlan4k
);
331 /* Try to find an existing MC entry for this VID */
332 for (i
= 0; i
< smi
->num_vlan_mc
; i
++) {
333 struct rtl8366_vlan_mc vlanmc
;
335 err
= smi
->ops
->get_vlan_mc(smi
, i
, &vlanmc
);
339 if (vid
== vlanmc
.vid
) {
340 /* update the MC entry */
341 vlanmc
.member
= member
;
342 vlanmc
.untag
= untag
;
345 err
= smi
->ops
->set_vlan_mc(smi
, i
, &vlanmc
);
353 static int rtl8366_get_pvid(struct rtl8366_smi
*smi
, int port
, int *val
)
355 struct rtl8366_vlan_mc vlanmc
;
359 err
= smi
->ops
->get_mc_index(smi
, port
, &index
);
363 err
= smi
->ops
->get_vlan_mc(smi
, index
, &vlanmc
);
371 static int rtl8366_set_pvid(struct rtl8366_smi
*smi
, unsigned port
,
374 struct rtl8366_vlan_mc vlanmc
;
375 struct rtl8366_vlan_4k vlan4k
;
379 /* Try to find an existing MC entry for this VID */
380 for (i
= 0; i
< smi
->num_vlan_mc
; i
++) {
381 err
= smi
->ops
->get_vlan_mc(smi
, i
, &vlanmc
);
385 if (vid
== vlanmc
.vid
) {
386 err
= smi
->ops
->set_vlan_mc(smi
, i
, &vlanmc
);
390 err
= smi
->ops
->set_mc_index(smi
, port
, i
);
395 /* We have no MC entry for this VID, try to find an empty one */
396 for (i
= 0; i
< smi
->num_vlan_mc
; i
++) {
397 err
= smi
->ops
->get_vlan_mc(smi
, i
, &vlanmc
);
401 if (vlanmc
.vid
== 0 && vlanmc
.member
== 0) {
402 /* Update the entry from the 4K table */
403 err
= smi
->ops
->get_vlan_4k(smi
, vid
, &vlan4k
);
408 vlanmc
.member
= vlan4k
.member
;
409 vlanmc
.untag
= vlan4k
.untag
;
410 vlanmc
.fid
= vlan4k
.fid
;
411 err
= smi
->ops
->set_vlan_mc(smi
, i
, &vlanmc
);
415 err
= smi
->ops
->set_mc_index(smi
, port
, i
);
420 /* MC table is full, try to find an unused entry and replace it */
421 for (i
= 0; i
< smi
->num_vlan_mc
; i
++) {
424 err
= rtl8366_mc_is_used(smi
, i
, &used
);
429 /* Update the entry from the 4K table */
430 err
= smi
->ops
->get_vlan_4k(smi
, vid
, &vlan4k
);
435 vlanmc
.member
= vlan4k
.member
;
436 vlanmc
.untag
= vlan4k
.untag
;
437 vlanmc
.fid
= vlan4k
.fid
;
438 err
= smi
->ops
->set_vlan_mc(smi
, i
, &vlanmc
);
442 err
= smi
->ops
->set_mc_index(smi
, port
, i
);
448 "all VLAN member configurations are in use\n");
453 int rtl8366_enable_vlan(struct rtl8366_smi
*smi
, int enable
)
457 err
= smi
->ops
->enable_vlan(smi
, enable
);
461 smi
->vlan_enabled
= enable
;
464 smi
->vlan4k_enabled
= 0;
465 err
= smi
->ops
->enable_vlan4k(smi
, enable
);
470 EXPORT_SYMBOL_GPL(rtl8366_enable_vlan
);
472 static int rtl8366_enable_vlan4k(struct rtl8366_smi
*smi
, int enable
)
477 err
= smi
->ops
->enable_vlan(smi
, enable
);
481 smi
->vlan_enabled
= enable
;
484 err
= smi
->ops
->enable_vlan4k(smi
, enable
);
488 smi
->vlan4k_enabled
= enable
;
492 int rtl8366_enable_all_ports(struct rtl8366_smi
*smi
, int enable
)
497 for (port
= 0; port
< smi
->num_ports
; port
++) {
498 err
= smi
->ops
->enable_port(smi
, port
, enable
);
505 EXPORT_SYMBOL_GPL(rtl8366_enable_all_ports
);
507 int rtl8366_reset_vlan(struct rtl8366_smi
*smi
)
509 struct rtl8366_vlan_mc vlanmc
;
513 rtl8366_enable_vlan(smi
, 0);
514 rtl8366_enable_vlan4k(smi
, 0);
516 /* clear VLAN member configurations */
522 for (i
= 0; i
< smi
->num_vlan_mc
; i
++) {
523 err
= smi
->ops
->set_vlan_mc(smi
, i
, &vlanmc
);
530 EXPORT_SYMBOL_GPL(rtl8366_reset_vlan
);
532 static int rtl8366_init_vlan(struct rtl8366_smi
*smi
)
537 err
= rtl8366_reset_vlan(smi
);
541 for (port
= 0; port
< smi
->num_ports
; port
++) {
544 if (port
== smi
->cpu_port
)
545 mask
= (1 << smi
->num_ports
) - 1;
547 mask
= (1 << port
) | (1 << smi
->cpu_port
);
549 err
= rtl8366_set_vlan(smi
, (port
+ 1), mask
, mask
, 0);
553 err
= rtl8366_set_pvid(smi
, port
, (port
+ 1));
558 return rtl8366_enable_vlan(smi
, 1);
561 #ifdef CONFIG_RTL8366S_PHY_DEBUG_FS
562 int rtl8366_debugfs_open(struct inode
*inode
, struct file
*file
)
564 file
->private_data
= inode
->i_private
;
567 EXPORT_SYMBOL_GPL(rtl8366_debugfs_open
);
569 static ssize_t
rtl8366_read_debugfs_vlan_mc(struct file
*file
,
570 char __user
*user_buf
,
571 size_t count
, loff_t
*ppos
)
573 struct rtl8366_smi
*smi
= (struct rtl8366_smi
*)file
->private_data
;
575 char *buf
= smi
->buf
;
577 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
,
578 "%2s %6s %4s %6s %6s %3s\n",
579 "id", "vid","prio", "member", "untag", "fid");
581 for (i
= 0; i
< smi
->num_vlan_mc
; ++i
) {
582 struct rtl8366_vlan_mc vlanmc
;
584 smi
->ops
->get_vlan_mc(smi
, i
, &vlanmc
);
586 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
,
587 "%2d %6d %4d 0x%04x 0x%04x %3d\n",
588 i
, vlanmc
.vid
, vlanmc
.priority
,
589 vlanmc
.member
, vlanmc
.untag
, vlanmc
.fid
);
592 return simple_read_from_buffer(user_buf
, count
, ppos
, buf
, len
);
595 #define RTL8366_VLAN4K_PAGE_SIZE 64
596 #define RTL8366_VLAN4K_NUM_PAGES (4096 / RTL8366_VLAN4K_PAGE_SIZE)
598 static ssize_t
rtl8366_read_debugfs_vlan_4k(struct file
*file
,
599 char __user
*user_buf
,
600 size_t count
, loff_t
*ppos
)
602 struct rtl8366_smi
*smi
= (struct rtl8366_smi
*)file
->private_data
;
605 char *buf
= smi
->buf
;
607 if (smi
->dbg_vlan_4k_page
>= RTL8366_VLAN4K_NUM_PAGES
) {
608 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
,
609 "invalid page: %u\n", smi
->dbg_vlan_4k_page
);
610 return simple_read_from_buffer(user_buf
, count
, ppos
, buf
, len
);
613 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
,
615 "vid", "member", "untag", "fid");
617 offset
= RTL8366_VLAN4K_PAGE_SIZE
* smi
->dbg_vlan_4k_page
;
618 for (i
= 0; i
< RTL8366_VLAN4K_PAGE_SIZE
; i
++) {
619 struct rtl8366_vlan_4k vlan4k
;
621 smi
->ops
->get_vlan_4k(smi
, offset
+ i
, &vlan4k
);
623 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
,
624 "%4d 0x%04x 0x%04x %3d\n",
625 vlan4k
.vid
, vlan4k
.member
,
626 vlan4k
.untag
, vlan4k
.fid
);
629 return simple_read_from_buffer(user_buf
, count
, ppos
, buf
, len
);
632 static ssize_t
rtl8366_read_debugfs_pvid(struct file
*file
,
633 char __user
*user_buf
,
634 size_t count
, loff_t
*ppos
)
636 struct rtl8366_smi
*smi
= (struct rtl8366_smi
*)file
->private_data
;
637 char *buf
= smi
->buf
;
641 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
, "%4s %4s\n",
644 for (i
= 0; i
< smi
->num_ports
; i
++) {
648 err
= rtl8366_get_pvid(smi
, i
, &pvid
);
650 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
,
653 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
,
654 "%4d %4d\n", i
, pvid
);
657 return simple_read_from_buffer(user_buf
, count
, ppos
, buf
, len
);
660 static ssize_t
rtl8366_read_debugfs_reg(struct file
*file
,
661 char __user
*user_buf
,
662 size_t count
, loff_t
*ppos
)
664 struct rtl8366_smi
*smi
= (struct rtl8366_smi
*)file
->private_data
;
665 u32 t
, reg
= smi
->dbg_reg
;
667 char *buf
= smi
->buf
;
669 memset(buf
, '\0', sizeof(smi
->buf
));
671 err
= rtl8366_smi_read_reg(smi
, reg
, &t
);
673 len
+= snprintf(buf
, sizeof(smi
->buf
),
674 "Read failed (reg: 0x%04x)\n", reg
);
675 return simple_read_from_buffer(user_buf
, count
, ppos
, buf
, len
);
678 len
+= snprintf(buf
, sizeof(smi
->buf
), "reg = 0x%04x, val = 0x%04x\n",
681 return simple_read_from_buffer(user_buf
, count
, ppos
, buf
, len
);
684 static ssize_t
rtl8366_write_debugfs_reg(struct file
*file
,
685 const char __user
*user_buf
,
686 size_t count
, loff_t
*ppos
)
688 struct rtl8366_smi
*smi
= (struct rtl8366_smi
*)file
->private_data
;
690 u32 reg
= smi
->dbg_reg
;
693 char *buf
= smi
->buf
;
695 len
= min(count
, sizeof(smi
->buf
) - 1);
696 if (copy_from_user(buf
, user_buf
, len
)) {
697 dev_err(smi
->parent
, "copy from user failed\n");
702 if (len
> 0 && buf
[len
- 1] == '\n')
706 if (strict_strtoul(buf
, 16, &data
)) {
707 dev_err(smi
->parent
, "Invalid reg value %s\n", buf
);
709 err
= rtl8366_smi_write_reg(smi
, reg
, data
);
712 "writing reg 0x%04x val 0x%04lx failed\n",
720 static ssize_t
rtl8366_read_debugfs_mibs(struct file
*file
,
721 char __user
*user_buf
,
722 size_t count
, loff_t
*ppos
)
724 struct rtl8366_smi
*smi
= file
->private_data
;
726 char *buf
= smi
->buf
;
728 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
, "%-36s",
731 for (i
= 0; i
< smi
->num_ports
; i
++) {
734 snprintf(port_buf
, sizeof(port_buf
), "Port %d", i
);
735 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
, " %12s",
738 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
, "\n");
740 for (i
= 0; i
< smi
->num_mib_counters
; i
++) {
741 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
, "%-36s ",
742 smi
->mib_counters
[i
].name
);
743 for (j
= 0; j
< smi
->num_ports
; j
++) {
744 unsigned long long counter
= 0;
746 if (!smi
->ops
->get_mib_counter(smi
, i
, j
, &counter
))
747 len
+= snprintf(buf
+ len
,
748 sizeof(smi
->buf
) - len
,
751 len
+= snprintf(buf
+ len
,
752 sizeof(smi
->buf
) - len
,
755 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
, "\n");
758 return simple_read_from_buffer(user_buf
, count
, ppos
, buf
, len
);
761 static const struct file_operations fops_rtl8366_regs
= {
762 .read
= rtl8366_read_debugfs_reg
,
763 .write
= rtl8366_write_debugfs_reg
,
764 .open
= rtl8366_debugfs_open
,
768 static const struct file_operations fops_rtl8366_vlan_mc
= {
769 .read
= rtl8366_read_debugfs_vlan_mc
,
770 .open
= rtl8366_debugfs_open
,
774 static const struct file_operations fops_rtl8366_vlan_4k
= {
775 .read
= rtl8366_read_debugfs_vlan_4k
,
776 .open
= rtl8366_debugfs_open
,
780 static const struct file_operations fops_rtl8366_pvid
= {
781 .read
= rtl8366_read_debugfs_pvid
,
782 .open
= rtl8366_debugfs_open
,
786 static const struct file_operations fops_rtl8366_mibs
= {
787 .read
= rtl8366_read_debugfs_mibs
,
788 .open
= rtl8366_debugfs_open
,
792 static void rtl8366_debugfs_init(struct rtl8366_smi
*smi
)
797 if (!smi
->debugfs_root
)
798 smi
->debugfs_root
= debugfs_create_dir(dev_name(smi
->parent
),
801 if (!smi
->debugfs_root
) {
802 dev_err(smi
->parent
, "Unable to create debugfs dir\n");
805 root
= smi
->debugfs_root
;
807 node
= debugfs_create_x16("reg", S_IRUGO
| S_IWUSR
, root
,
810 dev_err(smi
->parent
, "Creating debugfs file '%s' failed\n",
815 node
= debugfs_create_file("val", S_IRUGO
| S_IWUSR
, root
, smi
,
818 dev_err(smi
->parent
, "Creating debugfs file '%s' failed\n",
823 node
= debugfs_create_file("vlan_mc", S_IRUSR
, root
, smi
,
824 &fops_rtl8366_vlan_mc
);
826 dev_err(smi
->parent
, "Creating debugfs file '%s' failed\n",
831 node
= debugfs_create_u8("vlan_4k_page", S_IRUGO
| S_IWUSR
, root
,
832 &smi
->dbg_vlan_4k_page
);
834 dev_err(smi
->parent
, "Creating debugfs file '%s' failed\n",
839 node
= debugfs_create_file("vlan_4k", S_IRUSR
, root
, smi
,
840 &fops_rtl8366_vlan_4k
);
842 dev_err(smi
->parent
, "Creating debugfs file '%s' failed\n",
847 node
= debugfs_create_file("pvid", S_IRUSR
, root
, smi
,
850 dev_err(smi
->parent
, "Creating debugfs file '%s' failed\n",
855 node
= debugfs_create_file("mibs", S_IRUSR
, smi
->debugfs_root
, smi
,
858 dev_err(smi
->parent
, "Creating debugfs file '%s' failed\n",
862 static void rtl8366_debugfs_remove(struct rtl8366_smi
*smi
)
864 if (smi
->debugfs_root
) {
865 debugfs_remove_recursive(smi
->debugfs_root
);
866 smi
->debugfs_root
= NULL
;
870 static inline void rtl8366_debugfs_init(struct rtl8366_smi
*smi
) {}
871 static inline void rtl8366_debugfs_remove(struct rtl8366_smi
*smi
) {}
872 #endif /* CONFIG_RTL8366S_PHY_DEBUG_FS */
874 static int rtl8366_smi_mii_init(struct rtl8366_smi
*smi
)
879 smi
->mii_bus
= mdiobus_alloc();
880 if (smi
->mii_bus
== NULL
) {
885 smi
->mii_bus
->priv
= (void *) smi
;
886 smi
->mii_bus
->name
= dev_name(smi
->parent
);
887 smi
->mii_bus
->read
= smi
->ops
->mii_read
;
888 smi
->mii_bus
->write
= smi
->ops
->mii_write
;
889 snprintf(smi
->mii_bus
->id
, MII_BUS_ID_SIZE
, "%s",
890 dev_name(smi
->parent
));
891 smi
->mii_bus
->parent
= smi
->parent
;
892 smi
->mii_bus
->phy_mask
= ~(0x1f);
893 smi
->mii_bus
->irq
= smi
->mii_irq
;
894 for (i
= 0; i
< PHY_MAX_ADDR
; i
++)
895 smi
->mii_irq
[i
] = PHY_POLL
;
897 ret
= mdiobus_register(smi
->mii_bus
);
904 mdiobus_free(smi
->mii_bus
);
909 static void rtl8366_smi_mii_cleanup(struct rtl8366_smi
*smi
)
911 mdiobus_unregister(smi
->mii_bus
);
912 mdiobus_free(smi
->mii_bus
);
915 int rtl8366_sw_get_port_pvid(struct switch_dev
*dev
, int port
, int *val
)
917 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
918 return rtl8366_get_pvid(smi
, port
, val
);
920 EXPORT_SYMBOL_GPL(rtl8366_sw_get_port_pvid
);
922 int rtl8366_sw_set_port_pvid(struct switch_dev
*dev
, int port
, int val
)
924 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
925 return rtl8366_set_pvid(smi
, port
, val
);
927 EXPORT_SYMBOL_GPL(rtl8366_sw_set_port_pvid
);
929 int rtl8366_sw_get_port_mib(struct switch_dev
*dev
,
930 const struct switch_attr
*attr
,
931 struct switch_val
*val
)
933 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
935 unsigned long long counter
= 0;
936 char *buf
= smi
->buf
;
938 if (val
->port_vlan
>= smi
->num_ports
)
941 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
,
942 "Port %d MIB counters\n",
945 for (i
= 0; i
< smi
->num_mib_counters
; ++i
) {
946 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
,
947 "%-36s: ", smi
->mib_counters
[i
].name
);
948 if (!smi
->ops
->get_mib_counter(smi
, i
, val
->port_vlan
,
950 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
,
953 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
,
961 EXPORT_SYMBOL_GPL(rtl8366_sw_get_port_mib
);
963 int rtl8366_sw_get_vlan_info(struct switch_dev
*dev
,
964 const struct switch_attr
*attr
,
965 struct switch_val
*val
)
969 struct rtl8366_vlan_4k vlan4k
;
970 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
971 char *buf
= smi
->buf
;
974 if (!smi
->ops
->is_vlan_valid(smi
, val
->port_vlan
))
977 memset(buf
, '\0', sizeof(smi
->buf
));
979 err
= smi
->ops
->get_vlan_4k(smi
, val
->port_vlan
, &vlan4k
);
983 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
,
984 "VLAN %d: Ports: '", vlan4k
.vid
);
986 for (i
= 0; i
< smi
->num_ports
; i
++) {
987 if (!(vlan4k
.member
& (1 << i
)))
990 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
, "%d%s", i
,
991 (vlan4k
.untag
& (1 << i
)) ? "" : "t");
994 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
,
995 "', members=%04x, untag=%04x, fid=%u",
996 vlan4k
.member
, vlan4k
.untag
, vlan4k
.fid
);
1003 EXPORT_SYMBOL_GPL(rtl8366_sw_get_vlan_info
);
1005 int rtl8366_sw_get_vlan_ports(struct switch_dev
*dev
, struct switch_val
*val
)
1007 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
1008 struct switch_port
*port
;
1009 struct rtl8366_vlan_4k vlan4k
;
1012 if (!smi
->ops
->is_vlan_valid(smi
, val
->port_vlan
))
1015 smi
->ops
->get_vlan_4k(smi
, val
->port_vlan
, &vlan4k
);
1017 port
= &val
->value
.ports
[0];
1019 for (i
= 0; i
< smi
->num_ports
; i
++) {
1020 if (!(vlan4k
.member
& BIT(i
)))
1024 port
->flags
= (vlan4k
.untag
& BIT(i
)) ?
1025 0 : BIT(SWITCH_PORT_FLAG_TAGGED
);
1031 EXPORT_SYMBOL_GPL(rtl8366_sw_get_vlan_ports
);
1033 int rtl8366_sw_set_vlan_ports(struct switch_dev
*dev
, struct switch_val
*val
)
1035 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
1036 struct switch_port
*port
;
1042 if (!smi
->ops
->is_vlan_valid(smi
, val
->port_vlan
))
1045 port
= &val
->value
.ports
[0];
1046 for (i
= 0; i
< val
->len
; i
++, port
++) {
1047 member
|= BIT(port
->id
);
1049 if (!(port
->flags
& BIT(SWITCH_PORT_FLAG_TAGGED
)))
1050 untag
|= BIT(port
->id
);
1053 * To ensure that we have a valid MC entry for this VLAN,
1054 * initialize the port VLAN ID here.
1056 err
= rtl8366_set_pvid(smi
, port
->id
, val
->port_vlan
);
1061 return rtl8366_set_vlan(smi
, val
->port_vlan
, member
, untag
, 0);
1063 EXPORT_SYMBOL_GPL(rtl8366_sw_set_vlan_ports
);
1065 int rtl8366_sw_get_vlan_fid(struct switch_dev
*dev
,
1066 const struct switch_attr
*attr
,
1067 struct switch_val
*val
)
1069 struct rtl8366_vlan_4k vlan4k
;
1070 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
1073 if (!smi
->ops
->is_vlan_valid(smi
, val
->port_vlan
))
1076 err
= smi
->ops
->get_vlan_4k(smi
, val
->port_vlan
, &vlan4k
);
1080 val
->value
.i
= vlan4k
.fid
;
1084 EXPORT_SYMBOL_GPL(rtl8366_sw_get_vlan_fid
);
1086 int rtl8366_sw_set_vlan_fid(struct switch_dev
*dev
,
1087 const struct switch_attr
*attr
,
1088 struct switch_val
*val
)
1090 struct rtl8366_vlan_4k vlan4k
;
1091 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
1094 if (!smi
->ops
->is_vlan_valid(smi
, val
->port_vlan
))
1097 if (val
->value
.i
< 0 || val
->value
.i
> attr
->max
)
1100 err
= smi
->ops
->get_vlan_4k(smi
, val
->port_vlan
, &vlan4k
);
1104 return rtl8366_set_vlan(smi
, val
->port_vlan
,
1109 EXPORT_SYMBOL_GPL(rtl8366_sw_set_vlan_fid
);
1111 int rtl8366_sw_get_vlan_enable(struct switch_dev
*dev
,
1112 const struct switch_attr
*attr
,
1113 struct switch_val
*val
)
1115 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
1121 val
->value
.i
= smi
->vlan_enabled
;
1123 val
->value
.i
= smi
->vlan4k_enabled
;
1127 EXPORT_SYMBOL_GPL(rtl8366_sw_get_vlan_enable
);
1129 int rtl8366_sw_set_vlan_enable(struct switch_dev
*dev
,
1130 const struct switch_attr
*attr
,
1131 struct switch_val
*val
)
1133 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
1140 err
= rtl8366_enable_vlan(smi
, val
->value
.i
);
1142 err
= rtl8366_enable_vlan4k(smi
, val
->value
.i
);
1146 EXPORT_SYMBOL_GPL(rtl8366_sw_set_vlan_enable
);
1148 struct rtl8366_smi
*rtl8366_smi_alloc(struct device
*parent
)
1150 struct rtl8366_smi
*smi
;
1154 smi
= kzalloc(sizeof(*smi
), GFP_KERNEL
);
1156 dev_err(parent
, "no memory for private data\n");
1160 smi
->parent
= parent
;
1163 EXPORT_SYMBOL_GPL(rtl8366_smi_alloc
);
1165 static int __rtl8366_smi_init(struct rtl8366_smi
*smi
, const char *name
)
1169 err
= gpio_request(smi
->gpio_sda
, name
);
1171 printk(KERN_ERR
"rtl8366_smi: gpio_request failed for %u, err=%d\n",
1172 smi
->gpio_sda
, err
);
1176 err
= gpio_request(smi
->gpio_sck
, name
);
1178 printk(KERN_ERR
"rtl8366_smi: gpio_request failed for %u, err=%d\n",
1179 smi
->gpio_sck
, err
);
1183 spin_lock_init(&smi
->lock
);
1187 gpio_free(smi
->gpio_sda
);
1192 static void __rtl8366_smi_cleanup(struct rtl8366_smi
*smi
)
1194 gpio_free(smi
->gpio_sck
);
1195 gpio_free(smi
->gpio_sda
);
1198 enum rtl8366_type
rtl8366_smi_detect(struct rtl8366_platform_data
*pdata
)
1200 static struct rtl8366_smi smi
;
1201 enum rtl8366_type type
= RTL8366_TYPE_UNKNOWN
;
1204 memset(&smi
, 0, sizeof(smi
));
1205 smi
.gpio_sda
= pdata
->gpio_sda
;
1206 smi
.gpio_sck
= pdata
->gpio_sck
;
1208 smi
.cmd_read
= 0xa9;
1209 smi
.cmd_write
= 0xa8;
1211 if (__rtl8366_smi_init(&smi
, "rtl8366"))
1214 if (rtl8366_smi_read_reg(&smi
, 0x5c, ®
))
1219 printk("Found an RTL8366S switch\n");
1220 type
= RTL8366_TYPE_S
;
1223 printk("Found an RTL8366RB switch\n");
1224 type
= RTL8366_TYPE_RB
;
1227 printk("Found an Unknown RTL8366 switch (id=0x%04x)\n", reg
);
1232 __rtl8366_smi_cleanup(&smi
);
1237 int rtl8366_smi_init(struct rtl8366_smi
*smi
)
1244 err
= __rtl8366_smi_init(smi
, dev_name(smi
->parent
));
1248 spin_lock_init(&smi
->lock
);
1250 dev_info(smi
->parent
, "using GPIO pins %u (SDA) and %u (SCK)\n",
1251 smi
->gpio_sda
, smi
->gpio_sck
);
1253 err
= smi
->ops
->detect(smi
);
1255 dev_err(smi
->parent
, "chip detection failed, err=%d\n", err
);
1259 err
= smi
->ops
->setup(smi
);
1261 dev_err(smi
->parent
, "chip setup failed, err=%d\n", err
);
1265 err
= rtl8366_init_vlan(smi
);
1267 dev_err(smi
->parent
, "VLAN initialization failed, err=%d\n",
1272 err
= rtl8366_enable_all_ports(smi
, 1);
1276 err
= rtl8366_smi_mii_init(smi
);
1280 rtl8366_debugfs_init(smi
);
1285 __rtl8366_smi_cleanup(smi
);
1289 EXPORT_SYMBOL_GPL(rtl8366_smi_init
);
1291 void rtl8366_smi_cleanup(struct rtl8366_smi
*smi
)
1293 rtl8366_debugfs_remove(smi
);
1294 rtl8366_smi_mii_cleanup(smi
);
1295 gpio_free(smi
->gpio_sck
);
1296 gpio_free(smi
->gpio_sda
);
1298 EXPORT_SYMBOL_GPL(rtl8366_smi_cleanup
);
1300 MODULE_DESCRIPTION("Realtek RTL8366 SMI interface driver");
1301 MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
1302 MODULE_LICENSE("GPL v2");