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
)
153 static int rtl8366_smi_write_byte(struct rtl8366_smi
*smi
, u8 data
)
155 rtl8366_smi_write_bits(smi
, data
, 8);
156 return rtl8366_smi_wait_for_ack(smi
);
159 static int rtl8366_smi_read_byte0(struct rtl8366_smi
*smi
, u8
*data
)
164 rtl8366_smi_read_bits(smi
, 8, &t
);
168 rtl8366_smi_write_bits(smi
, 0x00, 1);
173 static int rtl8366_smi_read_byte1(struct rtl8366_smi
*smi
, u8
*data
)
178 rtl8366_smi_read_bits(smi
, 8, &t
);
182 rtl8366_smi_write_bits(smi
, 0x01, 1);
187 int rtl8366_smi_read_reg(struct rtl8366_smi
*smi
, u32 addr
, u32
*data
)
194 spin_lock_irqsave(&smi
->lock
, flags
);
196 rtl8366_smi_start(smi
);
198 /* send READ command */
199 ret
= rtl8366_smi_write_byte(smi
, 0x0a << 4 | 0x04 << 1 | 0x01);
204 ret
= rtl8366_smi_write_byte(smi
, addr
& 0xff);
209 ret
= rtl8366_smi_write_byte(smi
, addr
>> 8);
214 rtl8366_smi_read_byte0(smi
, &lo
);
215 /* read DATA[15:8] */
216 rtl8366_smi_read_byte1(smi
, &hi
);
218 *data
= ((u32
) lo
) | (((u32
) hi
) << 8);
223 rtl8366_smi_stop(smi
);
224 spin_unlock_irqrestore(&smi
->lock
, flags
);
228 EXPORT_SYMBOL_GPL(rtl8366_smi_read_reg
);
230 int rtl8366_smi_write_reg(struct rtl8366_smi
*smi
, u32 addr
, u32 data
)
235 spin_lock_irqsave(&smi
->lock
, flags
);
237 rtl8366_smi_start(smi
);
239 /* send WRITE command */
240 ret
= rtl8366_smi_write_byte(smi
, 0x0a << 4 | 0x04 << 1 | 0x00);
245 ret
= rtl8366_smi_write_byte(smi
, addr
& 0xff);
250 ret
= rtl8366_smi_write_byte(smi
, addr
>> 8);
254 /* write DATA[7:0] */
255 ret
= rtl8366_smi_write_byte(smi
, data
& 0xff);
259 /* write DATA[15:8] */
260 ret
= rtl8366_smi_write_byte(smi
, data
>> 8);
267 rtl8366_smi_stop(smi
);
268 spin_unlock_irqrestore(&smi
->lock
, flags
);
272 EXPORT_SYMBOL_GPL(rtl8366_smi_write_reg
);
274 int rtl8366_smi_rmwr(struct rtl8366_smi
*smi
, u32 addr
, u32 mask
, u32 data
)
279 err
= rtl8366_smi_read_reg(smi
, addr
, &t
);
283 err
= rtl8366_smi_write_reg(smi
, addr
, (t
& ~mask
) | data
);
287 EXPORT_SYMBOL_GPL(rtl8366_smi_rmwr
);
289 static int rtl8366_mc_is_used(struct rtl8366_smi
*smi
, int mc_index
, int *used
)
295 for (i
= 0; i
< smi
->num_ports
; i
++) {
298 err
= smi
->ops
->get_mc_index(smi
, i
, &index
);
302 if (mc_index
== index
) {
311 static int rtl8366_set_vlan(struct rtl8366_smi
*smi
, int vid
, u32 member
,
314 struct rtl8366_vlan_4k vlan4k
;
318 /* Update the 4K table */
319 err
= smi
->ops
->get_vlan_4k(smi
, vid
, &vlan4k
);
323 vlan4k
.member
= member
;
324 vlan4k
.untag
= untag
;
326 err
= smi
->ops
->set_vlan_4k(smi
, &vlan4k
);
330 /* Try to find an existing MC entry for this VID */
331 for (i
= 0; i
< smi
->num_vlan_mc
; i
++) {
332 struct rtl8366_vlan_mc vlanmc
;
334 err
= smi
->ops
->get_vlan_mc(smi
, i
, &vlanmc
);
338 if (vid
== vlanmc
.vid
) {
339 /* update the MC entry */
340 vlanmc
.member
= member
;
341 vlanmc
.untag
= untag
;
344 err
= smi
->ops
->set_vlan_mc(smi
, i
, &vlanmc
);
352 static int rtl8366_get_pvid(struct rtl8366_smi
*smi
, int port
, int *val
)
354 struct rtl8366_vlan_mc vlanmc
;
358 err
= smi
->ops
->get_mc_index(smi
, port
, &index
);
362 err
= smi
->ops
->get_vlan_mc(smi
, index
, &vlanmc
);
370 static int rtl8366_set_pvid(struct rtl8366_smi
*smi
, unsigned port
,
373 struct rtl8366_vlan_mc vlanmc
;
374 struct rtl8366_vlan_4k vlan4k
;
378 /* Try to find an existing MC entry for this VID */
379 for (i
= 0; i
< smi
->num_vlan_mc
; i
++) {
380 err
= smi
->ops
->get_vlan_mc(smi
, i
, &vlanmc
);
384 if (vid
== vlanmc
.vid
) {
385 err
= smi
->ops
->set_vlan_mc(smi
, i
, &vlanmc
);
389 err
= smi
->ops
->set_mc_index(smi
, port
, i
);
394 /* We have no MC entry for this VID, try to find an empty one */
395 for (i
= 0; i
< smi
->num_vlan_mc
; i
++) {
396 err
= smi
->ops
->get_vlan_mc(smi
, i
, &vlanmc
);
400 if (vlanmc
.vid
== 0 && vlanmc
.member
== 0) {
401 /* Update the entry from the 4K table */
402 err
= smi
->ops
->get_vlan_4k(smi
, vid
, &vlan4k
);
407 vlanmc
.member
= vlan4k
.member
;
408 vlanmc
.untag
= vlan4k
.untag
;
409 vlanmc
.fid
= vlan4k
.fid
;
410 err
= smi
->ops
->set_vlan_mc(smi
, i
, &vlanmc
);
414 err
= smi
->ops
->set_mc_index(smi
, port
, i
);
419 /* MC table is full, try to find an unused entry and replace it */
420 for (i
= 0; i
< smi
->num_vlan_mc
; i
++) {
423 err
= rtl8366_mc_is_used(smi
, i
, &used
);
428 /* Update the entry from the 4K table */
429 err
= smi
->ops
->get_vlan_4k(smi
, vid
, &vlan4k
);
434 vlanmc
.member
= vlan4k
.member
;
435 vlanmc
.untag
= vlan4k
.untag
;
436 vlanmc
.fid
= vlan4k
.fid
;
437 err
= smi
->ops
->set_vlan_mc(smi
, i
, &vlanmc
);
441 err
= smi
->ops
->set_mc_index(smi
, port
, i
);
447 "all VLAN member configurations are in use\n");
452 int rtl8366_enable_vlan(struct rtl8366_smi
*smi
, int enable
)
456 err
= smi
->ops
->enable_vlan(smi
, enable
);
460 smi
->vlan_enabled
= enable
;
463 smi
->vlan4k_enabled
= 0;
464 err
= smi
->ops
->enable_vlan4k(smi
, enable
);
469 EXPORT_SYMBOL_GPL(rtl8366_enable_vlan
);
471 static int rtl8366_enable_vlan4k(struct rtl8366_smi
*smi
, int enable
)
476 err
= smi
->ops
->enable_vlan(smi
, enable
);
480 smi
->vlan_enabled
= enable
;
483 err
= smi
->ops
->enable_vlan4k(smi
, enable
);
487 smi
->vlan4k_enabled
= enable
;
491 int rtl8366_enable_all_ports(struct rtl8366_smi
*smi
, int enable
)
496 for (port
= 0; port
< smi
->num_ports
; port
++) {
497 err
= smi
->ops
->enable_port(smi
, port
, enable
);
504 EXPORT_SYMBOL_GPL(rtl8366_enable_all_ports
);
506 int rtl8366_reset_vlan(struct rtl8366_smi
*smi
)
508 struct rtl8366_vlan_mc vlanmc
;
512 rtl8366_enable_vlan(smi
, 0);
513 rtl8366_enable_vlan4k(smi
, 0);
515 /* clear VLAN member configurations */
521 for (i
= 0; i
< smi
->num_vlan_mc
; i
++) {
522 err
= smi
->ops
->set_vlan_mc(smi
, i
, &vlanmc
);
529 EXPORT_SYMBOL_GPL(rtl8366_reset_vlan
);
531 static int rtl8366_init_vlan(struct rtl8366_smi
*smi
)
536 err
= rtl8366_reset_vlan(smi
);
540 for (port
= 0; port
< smi
->num_ports
; port
++) {
543 if (port
== smi
->cpu_port
)
544 mask
= (1 << smi
->num_ports
) - 1;
546 mask
= (1 << port
) | (1 << smi
->cpu_port
);
548 err
= rtl8366_set_vlan(smi
, (port
+ 1), mask
, mask
, 0);
552 err
= rtl8366_set_pvid(smi
, port
, (port
+ 1));
557 return rtl8366_enable_vlan(smi
, 1);
560 #ifdef CONFIG_RTL8366S_PHY_DEBUG_FS
561 int rtl8366_debugfs_open(struct inode
*inode
, struct file
*file
)
563 file
->private_data
= inode
->i_private
;
566 EXPORT_SYMBOL_GPL(rtl8366_debugfs_open
);
568 static ssize_t
rtl8366_read_debugfs_vlan_mc(struct file
*file
,
569 char __user
*user_buf
,
570 size_t count
, loff_t
*ppos
)
572 struct rtl8366_smi
*smi
= (struct rtl8366_smi
*)file
->private_data
;
574 char *buf
= smi
->buf
;
576 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
,
577 "%2s %6s %4s %6s %6s %3s\n",
578 "id", "vid","prio", "member", "untag", "fid");
580 for (i
= 0; i
< smi
->num_vlan_mc
; ++i
) {
581 struct rtl8366_vlan_mc vlanmc
;
583 smi
->ops
->get_vlan_mc(smi
, i
, &vlanmc
);
585 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
,
586 "%2d %6d %4d 0x%04x 0x%04x %3d\n",
587 i
, vlanmc
.vid
, vlanmc
.priority
,
588 vlanmc
.member
, vlanmc
.untag
, vlanmc
.fid
);
591 return simple_read_from_buffer(user_buf
, count
, ppos
, buf
, len
);
594 #define RTL8366_VLAN4K_PAGE_SIZE 64
595 #define RTL8366_VLAN4K_NUM_PAGES (4096 / RTL8366_VLAN4K_PAGE_SIZE)
597 static ssize_t
rtl8366_read_debugfs_vlan_4k(struct file
*file
,
598 char __user
*user_buf
,
599 size_t count
, loff_t
*ppos
)
601 struct rtl8366_smi
*smi
= (struct rtl8366_smi
*)file
->private_data
;
604 char *buf
= smi
->buf
;
606 if (smi
->dbg_vlan_4k_page
>= RTL8366_VLAN4K_NUM_PAGES
) {
607 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
,
608 "invalid page: %u\n", smi
->dbg_vlan_4k_page
);
609 return simple_read_from_buffer(user_buf
, count
, ppos
, buf
, len
);
612 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
,
614 "vid", "member", "untag", "fid");
616 offset
= RTL8366_VLAN4K_PAGE_SIZE
* smi
->dbg_vlan_4k_page
;
617 for (i
= 0; i
< RTL8366_VLAN4K_PAGE_SIZE
; i
++) {
618 struct rtl8366_vlan_4k vlan4k
;
620 smi
->ops
->get_vlan_4k(smi
, offset
+ i
, &vlan4k
);
622 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
,
623 "%4d 0x%04x 0x%04x %3d\n",
624 vlan4k
.vid
, vlan4k
.member
,
625 vlan4k
.untag
, vlan4k
.fid
);
628 return simple_read_from_buffer(user_buf
, count
, ppos
, buf
, len
);
631 static ssize_t
rtl8366_read_debugfs_pvid(struct file
*file
,
632 char __user
*user_buf
,
633 size_t count
, loff_t
*ppos
)
635 struct rtl8366_smi
*smi
= (struct rtl8366_smi
*)file
->private_data
;
636 char *buf
= smi
->buf
;
640 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
, "%4s %4s\n",
643 for (i
= 0; i
< smi
->num_ports
; i
++) {
647 err
= rtl8366_get_pvid(smi
, i
, &pvid
);
649 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
,
652 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
,
653 "%4d %4d\n", i
, pvid
);
656 return simple_read_from_buffer(user_buf
, count
, ppos
, buf
, len
);
659 static ssize_t
rtl8366_read_debugfs_reg(struct file
*file
,
660 char __user
*user_buf
,
661 size_t count
, loff_t
*ppos
)
663 struct rtl8366_smi
*smi
= (struct rtl8366_smi
*)file
->private_data
;
664 u32 t
, reg
= smi
->dbg_reg
;
666 char *buf
= smi
->buf
;
668 memset(buf
, '\0', sizeof(smi
->buf
));
670 err
= rtl8366_smi_read_reg(smi
, reg
, &t
);
672 len
+= snprintf(buf
, sizeof(smi
->buf
),
673 "Read failed (reg: 0x%04x)\n", reg
);
674 return simple_read_from_buffer(user_buf
, count
, ppos
, buf
, len
);
677 len
+= snprintf(buf
, sizeof(smi
->buf
), "reg = 0x%04x, val = 0x%04x\n",
680 return simple_read_from_buffer(user_buf
, count
, ppos
, buf
, len
);
683 static ssize_t
rtl8366_write_debugfs_reg(struct file
*file
,
684 const char __user
*user_buf
,
685 size_t count
, loff_t
*ppos
)
687 struct rtl8366_smi
*smi
= (struct rtl8366_smi
*)file
->private_data
;
689 u32 reg
= smi
->dbg_reg
;
692 char *buf
= smi
->buf
;
694 len
= min(count
, sizeof(smi
->buf
) - 1);
695 if (copy_from_user(buf
, user_buf
, len
)) {
696 dev_err(smi
->parent
, "copy from user failed\n");
701 if (len
> 0 && buf
[len
- 1] == '\n')
705 if (strict_strtoul(buf
, 16, &data
)) {
706 dev_err(smi
->parent
, "Invalid reg value %s\n", buf
);
708 err
= rtl8366_smi_write_reg(smi
, reg
, data
);
711 "writing reg 0x%04x val 0x%04lx failed\n",
719 static ssize_t
rtl8366_read_debugfs_mibs(struct file
*file
,
720 char __user
*user_buf
,
721 size_t count
, loff_t
*ppos
)
723 struct rtl8366_smi
*smi
= file
->private_data
;
725 char *buf
= smi
->buf
;
727 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
, "%-36s",
730 for (i
= 0; i
< smi
->num_ports
; i
++) {
733 snprintf(port_buf
, sizeof(port_buf
), "Port %d", i
);
734 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
, " %12s",
737 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
, "\n");
739 for (i
= 0; i
< smi
->num_mib_counters
; i
++) {
740 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
, "%-36s ",
741 smi
->mib_counters
[i
].name
);
742 for (j
= 0; j
< smi
->num_ports
; j
++) {
743 unsigned long long counter
= 0;
745 if (!smi
->ops
->get_mib_counter(smi
, i
, j
, &counter
))
746 len
+= snprintf(buf
+ len
,
747 sizeof(smi
->buf
) - len
,
750 len
+= snprintf(buf
+ len
,
751 sizeof(smi
->buf
) - len
,
754 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
, "\n");
757 return simple_read_from_buffer(user_buf
, count
, ppos
, buf
, len
);
760 static const struct file_operations fops_rtl8366_regs
= {
761 .read
= rtl8366_read_debugfs_reg
,
762 .write
= rtl8366_write_debugfs_reg
,
763 .open
= rtl8366_debugfs_open
,
767 static const struct file_operations fops_rtl8366_vlan_mc
= {
768 .read
= rtl8366_read_debugfs_vlan_mc
,
769 .open
= rtl8366_debugfs_open
,
773 static const struct file_operations fops_rtl8366_vlan_4k
= {
774 .read
= rtl8366_read_debugfs_vlan_4k
,
775 .open
= rtl8366_debugfs_open
,
779 static const struct file_operations fops_rtl8366_pvid
= {
780 .read
= rtl8366_read_debugfs_pvid
,
781 .open
= rtl8366_debugfs_open
,
785 static const struct file_operations fops_rtl8366_mibs
= {
786 .read
= rtl8366_read_debugfs_mibs
,
787 .open
= rtl8366_debugfs_open
,
791 static void rtl8366_debugfs_init(struct rtl8366_smi
*smi
)
796 if (!smi
->debugfs_root
)
797 smi
->debugfs_root
= debugfs_create_dir(dev_name(smi
->parent
),
800 if (!smi
->debugfs_root
) {
801 dev_err(smi
->parent
, "Unable to create debugfs dir\n");
804 root
= smi
->debugfs_root
;
806 node
= debugfs_create_x16("reg", S_IRUGO
| S_IWUSR
, root
,
809 dev_err(smi
->parent
, "Creating debugfs file '%s' failed\n",
814 node
= debugfs_create_file("val", S_IRUGO
| S_IWUSR
, root
, smi
,
817 dev_err(smi
->parent
, "Creating debugfs file '%s' failed\n",
822 node
= debugfs_create_file("vlan_mc", S_IRUSR
, root
, smi
,
823 &fops_rtl8366_vlan_mc
);
825 dev_err(smi
->parent
, "Creating debugfs file '%s' failed\n",
830 node
= debugfs_create_u8("vlan_4k_page", S_IRUGO
| S_IWUSR
, root
,
831 &smi
->dbg_vlan_4k_page
);
833 dev_err(smi
->parent
, "Creating debugfs file '%s' failed\n",
838 node
= debugfs_create_file("vlan_4k", S_IRUSR
, root
, smi
,
839 &fops_rtl8366_vlan_4k
);
841 dev_err(smi
->parent
, "Creating debugfs file '%s' failed\n",
846 node
= debugfs_create_file("pvid", S_IRUSR
, root
, smi
,
849 dev_err(smi
->parent
, "Creating debugfs file '%s' failed\n",
854 node
= debugfs_create_file("mibs", S_IRUSR
, smi
->debugfs_root
, smi
,
857 dev_err(smi
->parent
, "Creating debugfs file '%s' failed\n",
861 static void rtl8366_debugfs_remove(struct rtl8366_smi
*smi
)
863 if (smi
->debugfs_root
) {
864 debugfs_remove_recursive(smi
->debugfs_root
);
865 smi
->debugfs_root
= NULL
;
869 static inline void rtl8366_debugfs_init(struct rtl8366_smi
*smi
) {}
870 static inline void rtl8366_debugfs_remove(struct rtl8366_smi
*smi
) {}
871 #endif /* CONFIG_RTL8366S_PHY_DEBUG_FS */
873 static int rtl8366_smi_mii_init(struct rtl8366_smi
*smi
)
878 smi
->mii_bus
= mdiobus_alloc();
879 if (smi
->mii_bus
== NULL
) {
884 smi
->mii_bus
->priv
= (void *) smi
;
885 smi
->mii_bus
->name
= dev_name(smi
->parent
);
886 smi
->mii_bus
->read
= smi
->ops
->mii_read
;
887 smi
->mii_bus
->write
= smi
->ops
->mii_write
;
888 snprintf(smi
->mii_bus
->id
, MII_BUS_ID_SIZE
, "%s",
889 dev_name(smi
->parent
));
890 smi
->mii_bus
->parent
= smi
->parent
;
891 smi
->mii_bus
->phy_mask
= ~(0x1f);
892 smi
->mii_bus
->irq
= smi
->mii_irq
;
893 for (i
= 0; i
< PHY_MAX_ADDR
; i
++)
894 smi
->mii_irq
[i
] = PHY_POLL
;
896 ret
= mdiobus_register(smi
->mii_bus
);
903 mdiobus_free(smi
->mii_bus
);
908 static void rtl8366_smi_mii_cleanup(struct rtl8366_smi
*smi
)
910 mdiobus_unregister(smi
->mii_bus
);
911 mdiobus_free(smi
->mii_bus
);
914 int rtl8366_sw_get_port_pvid(struct switch_dev
*dev
, int port
, int *val
)
916 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
917 return rtl8366_get_pvid(smi
, port
, val
);
919 EXPORT_SYMBOL_GPL(rtl8366_sw_get_port_pvid
);
921 int rtl8366_sw_set_port_pvid(struct switch_dev
*dev
, int port
, int val
)
923 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
924 return rtl8366_set_pvid(smi
, port
, val
);
926 EXPORT_SYMBOL_GPL(rtl8366_sw_set_port_pvid
);
928 int rtl8366_sw_get_port_mib(struct switch_dev
*dev
,
929 const struct switch_attr
*attr
,
930 struct switch_val
*val
)
932 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
934 unsigned long long counter
= 0;
935 char *buf
= smi
->buf
;
937 if (val
->port_vlan
>= smi
->num_ports
)
940 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
,
941 "Port %d MIB counters\n",
944 for (i
= 0; i
< smi
->num_mib_counters
; ++i
) {
945 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
,
946 "%-36s: ", smi
->mib_counters
[i
].name
);
947 if (!smi
->ops
->get_mib_counter(smi
, i
, val
->port_vlan
,
949 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
,
952 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
,
960 EXPORT_SYMBOL_GPL(rtl8366_sw_get_port_mib
);
962 int rtl8366_sw_get_vlan_info(struct switch_dev
*dev
,
963 const struct switch_attr
*attr
,
964 struct switch_val
*val
)
968 struct rtl8366_vlan_4k vlan4k
;
969 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
970 char *buf
= smi
->buf
;
973 if (!smi
->ops
->is_vlan_valid(smi
, val
->port_vlan
))
976 memset(buf
, '\0', sizeof(smi
->buf
));
978 err
= smi
->ops
->get_vlan_4k(smi
, val
->port_vlan
, &vlan4k
);
982 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
,
983 "VLAN %d: Ports: '", vlan4k
.vid
);
985 for (i
= 0; i
< smi
->num_ports
; i
++) {
986 if (!(vlan4k
.member
& (1 << i
)))
989 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
, "%d%s", i
,
990 (vlan4k
.untag
& (1 << i
)) ? "" : "t");
993 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
,
994 "', members=%04x, untag=%04x, fid=%u",
995 vlan4k
.member
, vlan4k
.untag
, vlan4k
.fid
);
1002 EXPORT_SYMBOL_GPL(rtl8366_sw_get_vlan_info
);
1004 int rtl8366_sw_get_vlan_ports(struct switch_dev
*dev
, struct switch_val
*val
)
1006 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
1007 struct switch_port
*port
;
1008 struct rtl8366_vlan_4k vlan4k
;
1011 if (!smi
->ops
->is_vlan_valid(smi
, val
->port_vlan
))
1014 smi
->ops
->get_vlan_4k(smi
, val
->port_vlan
, &vlan4k
);
1016 port
= &val
->value
.ports
[0];
1018 for (i
= 0; i
< smi
->num_ports
; i
++) {
1019 if (!(vlan4k
.member
& BIT(i
)))
1023 port
->flags
= (vlan4k
.untag
& BIT(i
)) ?
1024 0 : BIT(SWITCH_PORT_FLAG_TAGGED
);
1030 EXPORT_SYMBOL_GPL(rtl8366_sw_get_vlan_ports
);
1032 int rtl8366_sw_set_vlan_ports(struct switch_dev
*dev
, struct switch_val
*val
)
1034 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
1035 struct switch_port
*port
;
1041 if (!smi
->ops
->is_vlan_valid(smi
, val
->port_vlan
))
1044 port
= &val
->value
.ports
[0];
1045 for (i
= 0; i
< val
->len
; i
++, port
++) {
1046 member
|= BIT(port
->id
);
1048 if (!(port
->flags
& BIT(SWITCH_PORT_FLAG_TAGGED
)))
1049 untag
|= BIT(port
->id
);
1052 * To ensure that we have a valid MC entry for this VLAN,
1053 * initialize the port VLAN ID here.
1055 err
= rtl8366_set_pvid(smi
, port
->id
, val
->port_vlan
);
1060 return rtl8366_set_vlan(smi
, val
->port_vlan
, member
, untag
, 0);
1062 EXPORT_SYMBOL_GPL(rtl8366_sw_set_vlan_ports
);
1064 int rtl8366_sw_get_vlan_fid(struct switch_dev
*dev
,
1065 const struct switch_attr
*attr
,
1066 struct switch_val
*val
)
1068 struct rtl8366_vlan_4k vlan4k
;
1069 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
1072 if (!smi
->ops
->is_vlan_valid(smi
, val
->port_vlan
))
1075 err
= smi
->ops
->get_vlan_4k(smi
, val
->port_vlan
, &vlan4k
);
1079 val
->value
.i
= vlan4k
.fid
;
1083 EXPORT_SYMBOL_GPL(rtl8366_sw_get_vlan_fid
);
1085 int rtl8366_sw_set_vlan_fid(struct switch_dev
*dev
,
1086 const struct switch_attr
*attr
,
1087 struct switch_val
*val
)
1089 struct rtl8366_vlan_4k vlan4k
;
1090 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
1093 if (!smi
->ops
->is_vlan_valid(smi
, val
->port_vlan
))
1096 if (val
->value
.i
< 0 || val
->value
.i
> attr
->max
)
1099 err
= smi
->ops
->get_vlan_4k(smi
, val
->port_vlan
, &vlan4k
);
1103 return rtl8366_set_vlan(smi
, val
->port_vlan
,
1108 EXPORT_SYMBOL_GPL(rtl8366_sw_set_vlan_fid
);
1110 int rtl8366_sw_get_vlan_enable(struct switch_dev
*dev
,
1111 const struct switch_attr
*attr
,
1112 struct switch_val
*val
)
1114 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
1120 val
->value
.i
= smi
->vlan_enabled
;
1122 val
->value
.i
= smi
->vlan4k_enabled
;
1126 EXPORT_SYMBOL_GPL(rtl8366_sw_get_vlan_enable
);
1128 int rtl8366_sw_set_vlan_enable(struct switch_dev
*dev
,
1129 const struct switch_attr
*attr
,
1130 struct switch_val
*val
)
1132 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
1139 err
= rtl8366_enable_vlan(smi
, val
->value
.i
);
1141 err
= rtl8366_enable_vlan4k(smi
, val
->value
.i
);
1145 EXPORT_SYMBOL_GPL(rtl8366_sw_set_vlan_enable
);
1147 struct rtl8366_smi
*rtl8366_smi_alloc(struct device
*parent
)
1149 struct rtl8366_smi
*smi
;
1153 smi
= kzalloc(sizeof(*smi
), GFP_KERNEL
);
1155 dev_err(parent
, "no memory for private data\n");
1159 smi
->parent
= parent
;
1162 EXPORT_SYMBOL_GPL(rtl8366_smi_alloc
);
1164 static int __rtl8366_smi_init(struct rtl8366_smi
*smi
, const char *name
)
1168 err
= gpio_request(smi
->gpio_sda
, name
);
1170 printk(KERN_ERR
"rtl8366_smi: gpio_request failed for %u, err=%d\n",
1171 smi
->gpio_sda
, err
);
1175 err
= gpio_request(smi
->gpio_sck
, name
);
1177 printk(KERN_ERR
"rtl8366_smi: gpio_request failed for %u, err=%d\n",
1178 smi
->gpio_sck
, err
);
1182 spin_lock_init(&smi
->lock
);
1186 gpio_free(smi
->gpio_sda
);
1191 static void __rtl8366_smi_cleanup(struct rtl8366_smi
*smi
)
1193 gpio_free(smi
->gpio_sck
);
1194 gpio_free(smi
->gpio_sda
);
1197 enum rtl8366_type
rtl8366_smi_detect(struct rtl8366_platform_data
*pdata
)
1199 static struct rtl8366_smi smi
;
1200 enum rtl8366_type type
= RTL8366_TYPE_UNKNOWN
;
1203 memset(&smi
, 0, sizeof(smi
));
1204 smi
.gpio_sda
= pdata
->gpio_sda
;
1205 smi
.gpio_sck
= pdata
->gpio_sck
;
1207 if (__rtl8366_smi_init(&smi
, "rtl8366"))
1210 if (rtl8366_smi_read_reg(&smi
, 0x5c, ®
))
1215 printk("Found an RTL8366S switch\n");
1216 type
= RTL8366_TYPE_S
;
1219 printk("Found an RTL8366RB switch\n");
1220 type
= RTL8366_TYPE_RB
;
1223 printk("Found an Unknown RTL8366 switch (id=0x%04x)\n", reg
);
1228 __rtl8366_smi_cleanup(&smi
);
1233 int rtl8366_smi_init(struct rtl8366_smi
*smi
)
1240 err
= __rtl8366_smi_init(smi
, dev_name(smi
->parent
));
1244 spin_lock_init(&smi
->lock
);
1246 dev_info(smi
->parent
, "using GPIO pins %u (SDA) and %u (SCK)\n",
1247 smi
->gpio_sda
, smi
->gpio_sck
);
1249 err
= smi
->ops
->detect(smi
);
1251 dev_err(smi
->parent
, "chip detection failed, err=%d\n", err
);
1255 err
= smi
->ops
->setup(smi
);
1257 dev_err(smi
->parent
, "chip setup failed, err=%d\n", err
);
1261 err
= rtl8366_init_vlan(smi
);
1263 dev_err(smi
->parent
, "VLAN initialization failed, err=%d\n",
1268 err
= rtl8366_enable_all_ports(smi
, 1);
1272 err
= rtl8366_smi_mii_init(smi
);
1276 rtl8366_debugfs_init(smi
);
1281 __rtl8366_smi_cleanup(smi
);
1285 EXPORT_SYMBOL_GPL(rtl8366_smi_init
);
1287 void rtl8366_smi_cleanup(struct rtl8366_smi
*smi
)
1289 rtl8366_debugfs_remove(smi
);
1290 rtl8366_smi_mii_cleanup(smi
);
1291 gpio_free(smi
->gpio_sck
);
1292 gpio_free(smi
->gpio_sda
);
1294 EXPORT_SYMBOL_GPL(rtl8366_smi_cleanup
);
1296 MODULE_DESCRIPTION("Realtek RTL8366 SMI interface driver");
1297 MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
1298 MODULE_LICENSE("GPL v2");