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_write_byte_noack(struct rtl8366_smi
*smi
, u8 data
)
162 rtl8366_smi_write_bits(smi
, data
, 8);
166 static int rtl8366_smi_read_byte0(struct rtl8366_smi
*smi
, u8
*data
)
171 rtl8366_smi_read_bits(smi
, 8, &t
);
175 rtl8366_smi_write_bits(smi
, 0x00, 1);
180 static int rtl8366_smi_read_byte1(struct rtl8366_smi
*smi
, u8
*data
)
185 rtl8366_smi_read_bits(smi
, 8, &t
);
189 rtl8366_smi_write_bits(smi
, 0x01, 1);
194 int rtl8366_smi_read_reg(struct rtl8366_smi
*smi
, u32 addr
, u32
*data
)
201 spin_lock_irqsave(&smi
->lock
, flags
);
203 rtl8366_smi_start(smi
);
205 /* send READ command */
206 ret
= rtl8366_smi_write_byte(smi
, smi
->cmd_read
);
211 ret
= rtl8366_smi_write_byte(smi
, addr
& 0xff);
216 ret
= rtl8366_smi_write_byte(smi
, addr
>> 8);
221 rtl8366_smi_read_byte0(smi
, &lo
);
222 /* read DATA[15:8] */
223 rtl8366_smi_read_byte1(smi
, &hi
);
225 *data
= ((u32
) lo
) | (((u32
) hi
) << 8);
230 rtl8366_smi_stop(smi
);
231 spin_unlock_irqrestore(&smi
->lock
, flags
);
235 EXPORT_SYMBOL_GPL(rtl8366_smi_read_reg
);
237 static int __rtl8366_smi_write_reg(struct rtl8366_smi
*smi
,
238 u32 addr
, u32 data
, bool ack
)
243 spin_lock_irqsave(&smi
->lock
, flags
);
245 rtl8366_smi_start(smi
);
247 /* send WRITE command */
248 ret
= rtl8366_smi_write_byte(smi
, smi
->cmd_write
);
253 ret
= rtl8366_smi_write_byte(smi
, addr
& 0xff);
258 ret
= rtl8366_smi_write_byte(smi
, addr
>> 8);
262 /* write DATA[7:0] */
263 ret
= rtl8366_smi_write_byte(smi
, data
& 0xff);
267 /* write DATA[15:8] */
269 ret
= rtl8366_smi_write_byte(smi
, data
>> 8);
271 ret
= rtl8366_smi_write_byte_noack(smi
, data
>> 8);
278 rtl8366_smi_stop(smi
);
279 spin_unlock_irqrestore(&smi
->lock
, flags
);
284 int rtl8366_smi_write_reg(struct rtl8366_smi
*smi
, u32 addr
, u32 data
)
286 return __rtl8366_smi_write_reg(smi
, addr
, data
, true);
288 EXPORT_SYMBOL_GPL(rtl8366_smi_write_reg
);
290 int rtl8366_smi_write_reg_noack(struct rtl8366_smi
*smi
, u32 addr
, u32 data
)
292 return __rtl8366_smi_write_reg(smi
, addr
, data
, false);
294 EXPORT_SYMBOL_GPL(rtl8366_smi_write_reg_noack
);
296 int rtl8366_smi_rmwr(struct rtl8366_smi
*smi
, u32 addr
, u32 mask
, u32 data
)
301 err
= rtl8366_smi_read_reg(smi
, addr
, &t
);
305 err
= rtl8366_smi_write_reg(smi
, addr
, (t
& ~mask
) | data
);
309 EXPORT_SYMBOL_GPL(rtl8366_smi_rmwr
);
311 static int rtl8366_mc_is_used(struct rtl8366_smi
*smi
, int mc_index
, int *used
)
317 for (i
= 0; i
< smi
->num_ports
; i
++) {
320 err
= smi
->ops
->get_mc_index(smi
, i
, &index
);
324 if (mc_index
== index
) {
333 static int rtl8366_set_vlan(struct rtl8366_smi
*smi
, int vid
, u32 member
,
336 struct rtl8366_vlan_4k vlan4k
;
340 /* Update the 4K table */
341 err
= smi
->ops
->get_vlan_4k(smi
, vid
, &vlan4k
);
345 vlan4k
.member
= member
;
346 vlan4k
.untag
= untag
;
348 err
= smi
->ops
->set_vlan_4k(smi
, &vlan4k
);
352 /* Try to find an existing MC entry for this VID */
353 for (i
= 0; i
< smi
->num_vlan_mc
; i
++) {
354 struct rtl8366_vlan_mc vlanmc
;
356 err
= smi
->ops
->get_vlan_mc(smi
, i
, &vlanmc
);
360 if (vid
== vlanmc
.vid
) {
361 /* update the MC entry */
362 vlanmc
.member
= member
;
363 vlanmc
.untag
= untag
;
366 err
= smi
->ops
->set_vlan_mc(smi
, i
, &vlanmc
);
374 static int rtl8366_get_pvid(struct rtl8366_smi
*smi
, int port
, int *val
)
376 struct rtl8366_vlan_mc vlanmc
;
380 err
= smi
->ops
->get_mc_index(smi
, port
, &index
);
384 err
= smi
->ops
->get_vlan_mc(smi
, index
, &vlanmc
);
392 static int rtl8366_set_pvid(struct rtl8366_smi
*smi
, unsigned port
,
395 struct rtl8366_vlan_mc vlanmc
;
396 struct rtl8366_vlan_4k vlan4k
;
400 /* Try to find an existing MC entry for this VID */
401 for (i
= 0; i
< smi
->num_vlan_mc
; i
++) {
402 err
= smi
->ops
->get_vlan_mc(smi
, i
, &vlanmc
);
406 if (vid
== vlanmc
.vid
) {
407 err
= smi
->ops
->set_vlan_mc(smi
, i
, &vlanmc
);
411 err
= smi
->ops
->set_mc_index(smi
, port
, i
);
416 /* We have no MC entry for this VID, try to find an empty one */
417 for (i
= 0; i
< smi
->num_vlan_mc
; i
++) {
418 err
= smi
->ops
->get_vlan_mc(smi
, i
, &vlanmc
);
422 if (vlanmc
.vid
== 0 && vlanmc
.member
== 0) {
423 /* Update the entry from the 4K table */
424 err
= smi
->ops
->get_vlan_4k(smi
, vid
, &vlan4k
);
429 vlanmc
.member
= vlan4k
.member
;
430 vlanmc
.untag
= vlan4k
.untag
;
431 vlanmc
.fid
= vlan4k
.fid
;
432 err
= smi
->ops
->set_vlan_mc(smi
, i
, &vlanmc
);
436 err
= smi
->ops
->set_mc_index(smi
, port
, i
);
441 /* MC table is full, try to find an unused entry and replace it */
442 for (i
= 0; i
< smi
->num_vlan_mc
; i
++) {
445 err
= rtl8366_mc_is_used(smi
, i
, &used
);
450 /* Update the entry from the 4K table */
451 err
= smi
->ops
->get_vlan_4k(smi
, vid
, &vlan4k
);
456 vlanmc
.member
= vlan4k
.member
;
457 vlanmc
.untag
= vlan4k
.untag
;
458 vlanmc
.fid
= vlan4k
.fid
;
459 err
= smi
->ops
->set_vlan_mc(smi
, i
, &vlanmc
);
463 err
= smi
->ops
->set_mc_index(smi
, port
, i
);
469 "all VLAN member configurations are in use\n");
474 int rtl8366_enable_vlan(struct rtl8366_smi
*smi
, int enable
)
478 err
= smi
->ops
->enable_vlan(smi
, enable
);
482 smi
->vlan_enabled
= enable
;
485 smi
->vlan4k_enabled
= 0;
486 err
= smi
->ops
->enable_vlan4k(smi
, enable
);
491 EXPORT_SYMBOL_GPL(rtl8366_enable_vlan
);
493 static int rtl8366_enable_vlan4k(struct rtl8366_smi
*smi
, int enable
)
498 err
= smi
->ops
->enable_vlan(smi
, enable
);
502 smi
->vlan_enabled
= enable
;
505 err
= smi
->ops
->enable_vlan4k(smi
, enable
);
509 smi
->vlan4k_enabled
= enable
;
513 int rtl8366_enable_all_ports(struct rtl8366_smi
*smi
, int enable
)
518 for (port
= 0; port
< smi
->num_ports
; port
++) {
519 err
= smi
->ops
->enable_port(smi
, port
, enable
);
526 EXPORT_SYMBOL_GPL(rtl8366_enable_all_ports
);
528 int rtl8366_reset_vlan(struct rtl8366_smi
*smi
)
530 struct rtl8366_vlan_mc vlanmc
;
534 rtl8366_enable_vlan(smi
, 0);
535 rtl8366_enable_vlan4k(smi
, 0);
537 /* clear VLAN member configurations */
543 for (i
= 0; i
< smi
->num_vlan_mc
; i
++) {
544 err
= smi
->ops
->set_vlan_mc(smi
, i
, &vlanmc
);
551 EXPORT_SYMBOL_GPL(rtl8366_reset_vlan
);
553 static int rtl8366_init_vlan(struct rtl8366_smi
*smi
)
558 err
= rtl8366_reset_vlan(smi
);
562 for (port
= 0; port
< smi
->num_ports
; port
++) {
565 if (port
== smi
->cpu_port
)
566 mask
= (1 << smi
->num_ports
) - 1;
568 mask
= (1 << port
) | (1 << smi
->cpu_port
);
570 err
= rtl8366_set_vlan(smi
, (port
+ 1), mask
, mask
, 0);
574 err
= rtl8366_set_pvid(smi
, port
, (port
+ 1));
579 return rtl8366_enable_vlan(smi
, 1);
582 #ifdef CONFIG_RTL8366S_PHY_DEBUG_FS
583 int rtl8366_debugfs_open(struct inode
*inode
, struct file
*file
)
585 file
->private_data
= inode
->i_private
;
588 EXPORT_SYMBOL_GPL(rtl8366_debugfs_open
);
590 static ssize_t
rtl8366_read_debugfs_vlan_mc(struct file
*file
,
591 char __user
*user_buf
,
592 size_t count
, loff_t
*ppos
)
594 struct rtl8366_smi
*smi
= (struct rtl8366_smi
*)file
->private_data
;
596 char *buf
= smi
->buf
;
598 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
,
599 "%2s %6s %4s %6s %6s %3s\n",
600 "id", "vid","prio", "member", "untag", "fid");
602 for (i
= 0; i
< smi
->num_vlan_mc
; ++i
) {
603 struct rtl8366_vlan_mc vlanmc
;
605 smi
->ops
->get_vlan_mc(smi
, i
, &vlanmc
);
607 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
,
608 "%2d %6d %4d 0x%04x 0x%04x %3d\n",
609 i
, vlanmc
.vid
, vlanmc
.priority
,
610 vlanmc
.member
, vlanmc
.untag
, vlanmc
.fid
);
613 return simple_read_from_buffer(user_buf
, count
, ppos
, buf
, len
);
616 #define RTL8366_VLAN4K_PAGE_SIZE 64
617 #define RTL8366_VLAN4K_NUM_PAGES (4096 / RTL8366_VLAN4K_PAGE_SIZE)
619 static ssize_t
rtl8366_read_debugfs_vlan_4k(struct file
*file
,
620 char __user
*user_buf
,
621 size_t count
, loff_t
*ppos
)
623 struct rtl8366_smi
*smi
= (struct rtl8366_smi
*)file
->private_data
;
626 char *buf
= smi
->buf
;
628 if (smi
->dbg_vlan_4k_page
>= RTL8366_VLAN4K_NUM_PAGES
) {
629 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
,
630 "invalid page: %u\n", smi
->dbg_vlan_4k_page
);
631 return simple_read_from_buffer(user_buf
, count
, ppos
, buf
, len
);
634 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
,
636 "vid", "member", "untag", "fid");
638 offset
= RTL8366_VLAN4K_PAGE_SIZE
* smi
->dbg_vlan_4k_page
;
639 for (i
= 0; i
< RTL8366_VLAN4K_PAGE_SIZE
; i
++) {
640 struct rtl8366_vlan_4k vlan4k
;
642 smi
->ops
->get_vlan_4k(smi
, offset
+ i
, &vlan4k
);
644 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
,
645 "%4d 0x%04x 0x%04x %3d\n",
646 vlan4k
.vid
, vlan4k
.member
,
647 vlan4k
.untag
, vlan4k
.fid
);
650 return simple_read_from_buffer(user_buf
, count
, ppos
, buf
, len
);
653 static ssize_t
rtl8366_read_debugfs_pvid(struct file
*file
,
654 char __user
*user_buf
,
655 size_t count
, loff_t
*ppos
)
657 struct rtl8366_smi
*smi
= (struct rtl8366_smi
*)file
->private_data
;
658 char *buf
= smi
->buf
;
662 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
, "%4s %4s\n",
665 for (i
= 0; i
< smi
->num_ports
; i
++) {
669 err
= rtl8366_get_pvid(smi
, i
, &pvid
);
671 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
,
674 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
,
675 "%4d %4d\n", i
, pvid
);
678 return simple_read_from_buffer(user_buf
, count
, ppos
, buf
, len
);
681 static ssize_t
rtl8366_read_debugfs_reg(struct file
*file
,
682 char __user
*user_buf
,
683 size_t count
, loff_t
*ppos
)
685 struct rtl8366_smi
*smi
= (struct rtl8366_smi
*)file
->private_data
;
686 u32 t
, reg
= smi
->dbg_reg
;
688 char *buf
= smi
->buf
;
690 memset(buf
, '\0', sizeof(smi
->buf
));
692 err
= rtl8366_smi_read_reg(smi
, reg
, &t
);
694 len
+= snprintf(buf
, sizeof(smi
->buf
),
695 "Read failed (reg: 0x%04x)\n", reg
);
696 return simple_read_from_buffer(user_buf
, count
, ppos
, buf
, len
);
699 len
+= snprintf(buf
, sizeof(smi
->buf
), "reg = 0x%04x, val = 0x%04x\n",
702 return simple_read_from_buffer(user_buf
, count
, ppos
, buf
, len
);
705 static ssize_t
rtl8366_write_debugfs_reg(struct file
*file
,
706 const char __user
*user_buf
,
707 size_t count
, loff_t
*ppos
)
709 struct rtl8366_smi
*smi
= (struct rtl8366_smi
*)file
->private_data
;
711 u32 reg
= smi
->dbg_reg
;
714 char *buf
= smi
->buf
;
716 len
= min(count
, sizeof(smi
->buf
) - 1);
717 if (copy_from_user(buf
, user_buf
, len
)) {
718 dev_err(smi
->parent
, "copy from user failed\n");
723 if (len
> 0 && buf
[len
- 1] == '\n')
727 if (strict_strtoul(buf
, 16, &data
)) {
728 dev_err(smi
->parent
, "Invalid reg value %s\n", buf
);
730 err
= rtl8366_smi_write_reg(smi
, reg
, data
);
733 "writing reg 0x%04x val 0x%04lx failed\n",
741 static ssize_t
rtl8366_read_debugfs_mibs(struct file
*file
,
742 char __user
*user_buf
,
743 size_t count
, loff_t
*ppos
)
745 struct rtl8366_smi
*smi
= file
->private_data
;
747 char *buf
= smi
->buf
;
749 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
, "%-36s",
752 for (i
= 0; i
< smi
->num_ports
; i
++) {
755 snprintf(port_buf
, sizeof(port_buf
), "Port %d", i
);
756 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
, " %12s",
759 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
, "\n");
761 for (i
= 0; i
< smi
->num_mib_counters
; i
++) {
762 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
, "%-36s ",
763 smi
->mib_counters
[i
].name
);
764 for (j
= 0; j
< smi
->num_ports
; j
++) {
765 unsigned long long counter
= 0;
767 if (!smi
->ops
->get_mib_counter(smi
, i
, j
, &counter
))
768 len
+= snprintf(buf
+ len
,
769 sizeof(smi
->buf
) - len
,
772 len
+= snprintf(buf
+ len
,
773 sizeof(smi
->buf
) - len
,
776 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
, "\n");
779 return simple_read_from_buffer(user_buf
, count
, ppos
, buf
, len
);
782 static const struct file_operations fops_rtl8366_regs
= {
783 .read
= rtl8366_read_debugfs_reg
,
784 .write
= rtl8366_write_debugfs_reg
,
785 .open
= rtl8366_debugfs_open
,
789 static const struct file_operations fops_rtl8366_vlan_mc
= {
790 .read
= rtl8366_read_debugfs_vlan_mc
,
791 .open
= rtl8366_debugfs_open
,
795 static const struct file_operations fops_rtl8366_vlan_4k
= {
796 .read
= rtl8366_read_debugfs_vlan_4k
,
797 .open
= rtl8366_debugfs_open
,
801 static const struct file_operations fops_rtl8366_pvid
= {
802 .read
= rtl8366_read_debugfs_pvid
,
803 .open
= rtl8366_debugfs_open
,
807 static const struct file_operations fops_rtl8366_mibs
= {
808 .read
= rtl8366_read_debugfs_mibs
,
809 .open
= rtl8366_debugfs_open
,
813 static void rtl8366_debugfs_init(struct rtl8366_smi
*smi
)
818 if (!smi
->debugfs_root
)
819 smi
->debugfs_root
= debugfs_create_dir(dev_name(smi
->parent
),
822 if (!smi
->debugfs_root
) {
823 dev_err(smi
->parent
, "Unable to create debugfs dir\n");
826 root
= smi
->debugfs_root
;
828 node
= debugfs_create_x16("reg", S_IRUGO
| S_IWUSR
, root
,
831 dev_err(smi
->parent
, "Creating debugfs file '%s' failed\n",
836 node
= debugfs_create_file("val", S_IRUGO
| S_IWUSR
, root
, smi
,
839 dev_err(smi
->parent
, "Creating debugfs file '%s' failed\n",
844 node
= debugfs_create_file("vlan_mc", S_IRUSR
, root
, smi
,
845 &fops_rtl8366_vlan_mc
);
847 dev_err(smi
->parent
, "Creating debugfs file '%s' failed\n",
852 node
= debugfs_create_u8("vlan_4k_page", S_IRUGO
| S_IWUSR
, root
,
853 &smi
->dbg_vlan_4k_page
);
855 dev_err(smi
->parent
, "Creating debugfs file '%s' failed\n",
860 node
= debugfs_create_file("vlan_4k", S_IRUSR
, root
, smi
,
861 &fops_rtl8366_vlan_4k
);
863 dev_err(smi
->parent
, "Creating debugfs file '%s' failed\n",
868 node
= debugfs_create_file("pvid", S_IRUSR
, root
, smi
,
871 dev_err(smi
->parent
, "Creating debugfs file '%s' failed\n",
876 node
= debugfs_create_file("mibs", S_IRUSR
, smi
->debugfs_root
, smi
,
879 dev_err(smi
->parent
, "Creating debugfs file '%s' failed\n",
883 static void rtl8366_debugfs_remove(struct rtl8366_smi
*smi
)
885 if (smi
->debugfs_root
) {
886 debugfs_remove_recursive(smi
->debugfs_root
);
887 smi
->debugfs_root
= NULL
;
891 static inline void rtl8366_debugfs_init(struct rtl8366_smi
*smi
) {}
892 static inline void rtl8366_debugfs_remove(struct rtl8366_smi
*smi
) {}
893 #endif /* CONFIG_RTL8366S_PHY_DEBUG_FS */
895 static int rtl8366_smi_mii_init(struct rtl8366_smi
*smi
)
900 smi
->mii_bus
= mdiobus_alloc();
901 if (smi
->mii_bus
== NULL
) {
906 smi
->mii_bus
->priv
= (void *) smi
;
907 smi
->mii_bus
->name
= dev_name(smi
->parent
);
908 smi
->mii_bus
->read
= smi
->ops
->mii_read
;
909 smi
->mii_bus
->write
= smi
->ops
->mii_write
;
910 snprintf(smi
->mii_bus
->id
, MII_BUS_ID_SIZE
, "%s",
911 dev_name(smi
->parent
));
912 smi
->mii_bus
->parent
= smi
->parent
;
913 smi
->mii_bus
->phy_mask
= ~(0x1f);
914 smi
->mii_bus
->irq
= smi
->mii_irq
;
915 for (i
= 0; i
< PHY_MAX_ADDR
; i
++)
916 smi
->mii_irq
[i
] = PHY_POLL
;
918 ret
= mdiobus_register(smi
->mii_bus
);
925 mdiobus_free(smi
->mii_bus
);
930 static void rtl8366_smi_mii_cleanup(struct rtl8366_smi
*smi
)
932 mdiobus_unregister(smi
->mii_bus
);
933 mdiobus_free(smi
->mii_bus
);
936 int rtl8366_sw_get_port_pvid(struct switch_dev
*dev
, int port
, int *val
)
938 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
939 return rtl8366_get_pvid(smi
, port
, val
);
941 EXPORT_SYMBOL_GPL(rtl8366_sw_get_port_pvid
);
943 int rtl8366_sw_set_port_pvid(struct switch_dev
*dev
, int port
, int val
)
945 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
946 return rtl8366_set_pvid(smi
, port
, val
);
948 EXPORT_SYMBOL_GPL(rtl8366_sw_set_port_pvid
);
950 int rtl8366_sw_get_port_mib(struct switch_dev
*dev
,
951 const struct switch_attr
*attr
,
952 struct switch_val
*val
)
954 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
956 unsigned long long counter
= 0;
957 char *buf
= smi
->buf
;
959 if (val
->port_vlan
>= smi
->num_ports
)
962 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
,
963 "Port %d MIB counters\n",
966 for (i
= 0; i
< smi
->num_mib_counters
; ++i
) {
967 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
,
968 "%-36s: ", smi
->mib_counters
[i
].name
);
969 if (!smi
->ops
->get_mib_counter(smi
, i
, val
->port_vlan
,
971 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
,
974 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
,
982 EXPORT_SYMBOL_GPL(rtl8366_sw_get_port_mib
);
984 int rtl8366_sw_get_vlan_info(struct switch_dev
*dev
,
985 const struct switch_attr
*attr
,
986 struct switch_val
*val
)
990 struct rtl8366_vlan_4k vlan4k
;
991 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
992 char *buf
= smi
->buf
;
995 if (!smi
->ops
->is_vlan_valid(smi
, val
->port_vlan
))
998 memset(buf
, '\0', sizeof(smi
->buf
));
1000 err
= smi
->ops
->get_vlan_4k(smi
, val
->port_vlan
, &vlan4k
);
1004 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
,
1005 "VLAN %d: Ports: '", vlan4k
.vid
);
1007 for (i
= 0; i
< smi
->num_ports
; i
++) {
1008 if (!(vlan4k
.member
& (1 << i
)))
1011 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
, "%d%s", i
,
1012 (vlan4k
.untag
& (1 << i
)) ? "" : "t");
1015 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
,
1016 "', members=%04x, untag=%04x, fid=%u",
1017 vlan4k
.member
, vlan4k
.untag
, vlan4k
.fid
);
1024 EXPORT_SYMBOL_GPL(rtl8366_sw_get_vlan_info
);
1026 int rtl8366_sw_get_vlan_ports(struct switch_dev
*dev
, struct switch_val
*val
)
1028 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
1029 struct switch_port
*port
;
1030 struct rtl8366_vlan_4k vlan4k
;
1033 if (!smi
->ops
->is_vlan_valid(smi
, val
->port_vlan
))
1036 smi
->ops
->get_vlan_4k(smi
, val
->port_vlan
, &vlan4k
);
1038 port
= &val
->value
.ports
[0];
1040 for (i
= 0; i
< smi
->num_ports
; i
++) {
1041 if (!(vlan4k
.member
& BIT(i
)))
1045 port
->flags
= (vlan4k
.untag
& BIT(i
)) ?
1046 0 : BIT(SWITCH_PORT_FLAG_TAGGED
);
1052 EXPORT_SYMBOL_GPL(rtl8366_sw_get_vlan_ports
);
1054 int rtl8366_sw_set_vlan_ports(struct switch_dev
*dev
, struct switch_val
*val
)
1056 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
1057 struct switch_port
*port
;
1063 if (!smi
->ops
->is_vlan_valid(smi
, val
->port_vlan
))
1066 port
= &val
->value
.ports
[0];
1067 for (i
= 0; i
< val
->len
; i
++, port
++) {
1068 member
|= BIT(port
->id
);
1070 if (!(port
->flags
& BIT(SWITCH_PORT_FLAG_TAGGED
)))
1071 untag
|= BIT(port
->id
);
1074 * To ensure that we have a valid MC entry for this VLAN,
1075 * initialize the port VLAN ID here.
1077 err
= rtl8366_set_pvid(smi
, port
->id
, val
->port_vlan
);
1082 return rtl8366_set_vlan(smi
, val
->port_vlan
, member
, untag
, 0);
1084 EXPORT_SYMBOL_GPL(rtl8366_sw_set_vlan_ports
);
1086 int rtl8366_sw_get_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 err
= smi
->ops
->get_vlan_4k(smi
, val
->port_vlan
, &vlan4k
);
1101 val
->value
.i
= vlan4k
.fid
;
1105 EXPORT_SYMBOL_GPL(rtl8366_sw_get_vlan_fid
);
1107 int rtl8366_sw_set_vlan_fid(struct switch_dev
*dev
,
1108 const struct switch_attr
*attr
,
1109 struct switch_val
*val
)
1111 struct rtl8366_vlan_4k vlan4k
;
1112 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
1115 if (!smi
->ops
->is_vlan_valid(smi
, val
->port_vlan
))
1118 if (val
->value
.i
< 0 || val
->value
.i
> attr
->max
)
1121 err
= smi
->ops
->get_vlan_4k(smi
, val
->port_vlan
, &vlan4k
);
1125 return rtl8366_set_vlan(smi
, val
->port_vlan
,
1130 EXPORT_SYMBOL_GPL(rtl8366_sw_set_vlan_fid
);
1132 int rtl8366_sw_get_vlan_enable(struct switch_dev
*dev
,
1133 const struct switch_attr
*attr
,
1134 struct switch_val
*val
)
1136 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
1142 val
->value
.i
= smi
->vlan_enabled
;
1144 val
->value
.i
= smi
->vlan4k_enabled
;
1148 EXPORT_SYMBOL_GPL(rtl8366_sw_get_vlan_enable
);
1150 int rtl8366_sw_set_vlan_enable(struct switch_dev
*dev
,
1151 const struct switch_attr
*attr
,
1152 struct switch_val
*val
)
1154 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
1161 err
= rtl8366_enable_vlan(smi
, val
->value
.i
);
1163 err
= rtl8366_enable_vlan4k(smi
, val
->value
.i
);
1167 EXPORT_SYMBOL_GPL(rtl8366_sw_set_vlan_enable
);
1169 struct rtl8366_smi
*rtl8366_smi_alloc(struct device
*parent
)
1171 struct rtl8366_smi
*smi
;
1175 smi
= kzalloc(sizeof(*smi
), GFP_KERNEL
);
1177 dev_err(parent
, "no memory for private data\n");
1181 smi
->parent
= parent
;
1184 EXPORT_SYMBOL_GPL(rtl8366_smi_alloc
);
1186 static int __rtl8366_smi_init(struct rtl8366_smi
*smi
, const char *name
)
1190 err
= gpio_request(smi
->gpio_sda
, name
);
1192 printk(KERN_ERR
"rtl8366_smi: gpio_request failed for %u, err=%d\n",
1193 smi
->gpio_sda
, err
);
1197 err
= gpio_request(smi
->gpio_sck
, name
);
1199 printk(KERN_ERR
"rtl8366_smi: gpio_request failed for %u, err=%d\n",
1200 smi
->gpio_sck
, err
);
1204 spin_lock_init(&smi
->lock
);
1208 gpio_free(smi
->gpio_sda
);
1213 static void __rtl8366_smi_cleanup(struct rtl8366_smi
*smi
)
1215 gpio_free(smi
->gpio_sck
);
1216 gpio_free(smi
->gpio_sda
);
1219 enum rtl8366_type
rtl8366_smi_detect(struct rtl8366_platform_data
*pdata
)
1221 static struct rtl8366_smi smi
;
1222 enum rtl8366_type type
= RTL8366_TYPE_UNKNOWN
;
1225 memset(&smi
, 0, sizeof(smi
));
1226 smi
.gpio_sda
= pdata
->gpio_sda
;
1227 smi
.gpio_sck
= pdata
->gpio_sck
;
1229 smi
.cmd_read
= 0xa9;
1230 smi
.cmd_write
= 0xa8;
1232 if (__rtl8366_smi_init(&smi
, "rtl8366"))
1235 if (rtl8366_smi_read_reg(&smi
, 0x5c, ®
))
1240 printk("Found an RTL8366S switch\n");
1241 type
= RTL8366_TYPE_S
;
1244 printk("Found an RTL8366RB switch\n");
1245 type
= RTL8366_TYPE_RB
;
1248 printk("Found an Unknown RTL8366 switch (id=0x%04x)\n", reg
);
1253 __rtl8366_smi_cleanup(&smi
);
1258 int rtl8366_smi_init(struct rtl8366_smi
*smi
)
1265 err
= __rtl8366_smi_init(smi
, dev_name(smi
->parent
));
1269 spin_lock_init(&smi
->lock
);
1271 dev_info(smi
->parent
, "using GPIO pins %u (SDA) and %u (SCK)\n",
1272 smi
->gpio_sda
, smi
->gpio_sck
);
1274 err
= smi
->ops
->detect(smi
);
1276 dev_err(smi
->parent
, "chip detection failed, err=%d\n", err
);
1280 err
= smi
->ops
->setup(smi
);
1282 dev_err(smi
->parent
, "chip setup failed, err=%d\n", err
);
1286 err
= rtl8366_init_vlan(smi
);
1288 dev_err(smi
->parent
, "VLAN initialization failed, err=%d\n",
1293 err
= rtl8366_enable_all_ports(smi
, 1);
1297 err
= rtl8366_smi_mii_init(smi
);
1301 rtl8366_debugfs_init(smi
);
1306 __rtl8366_smi_cleanup(smi
);
1310 EXPORT_SYMBOL_GPL(rtl8366_smi_init
);
1312 void rtl8366_smi_cleanup(struct rtl8366_smi
*smi
)
1314 rtl8366_debugfs_remove(smi
);
1315 rtl8366_smi_mii_cleanup(smi
);
1316 gpio_free(smi
->gpio_sck
);
1317 gpio_free(smi
->gpio_sda
);
1319 EXPORT_SYMBOL_GPL(rtl8366_smi_cleanup
);
1321 MODULE_DESCRIPTION("Realtek RTL8366 SMI interface driver");
1322 MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
1323 MODULE_LICENSE("GPL v2");