2 * Realtek RTL8366 SMI interface driver
4 * Copyright (C) 2009-2010 Gabor Juhos <juhosg@openwrt.org>
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published
8 * by the Free Software Foundation.
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/device.h>
14 #include <linux/delay.h>
15 #include <linux/gpio.h>
16 #include <linux/spinlock.h>
17 #include <linux/skbuff.h>
19 #ifdef CONFIG_RTL8366S_PHY_DEBUG_FS
20 #include <linux/debugfs.h>
23 #include "rtl8366_smi.h"
25 #define RTL8366_SMI_ACK_RETRY_COUNT 5
26 #define RTL8366_SMI_CLK_DELAY 10 /* nsec */
28 static inline void rtl8366_smi_clk_delay(struct rtl8366_smi
*smi
)
30 ndelay(RTL8366_SMI_CLK_DELAY
);
33 static void rtl8366_smi_start(struct rtl8366_smi
*smi
)
35 unsigned int sda
= smi
->gpio_sda
;
36 unsigned int sck
= smi
->gpio_sck
;
39 * Set GPIO pins to output mode, with initial state:
42 gpio_direction_output(sck
, 0);
43 gpio_direction_output(sda
, 1);
44 rtl8366_smi_clk_delay(smi
);
46 /* CLK 1: 0 -> 1, 1 -> 0 */
47 gpio_set_value(sck
, 1);
48 rtl8366_smi_clk_delay(smi
);
49 gpio_set_value(sck
, 0);
50 rtl8366_smi_clk_delay(smi
);
53 gpio_set_value(sck
, 1);
54 rtl8366_smi_clk_delay(smi
);
55 gpio_set_value(sda
, 0);
56 rtl8366_smi_clk_delay(smi
);
57 gpio_set_value(sck
, 0);
58 rtl8366_smi_clk_delay(smi
);
59 gpio_set_value(sda
, 1);
62 static void rtl8366_smi_stop(struct rtl8366_smi
*smi
)
64 unsigned int sda
= smi
->gpio_sda
;
65 unsigned int sck
= smi
->gpio_sck
;
67 rtl8366_smi_clk_delay(smi
);
68 gpio_set_value(sda
, 0);
69 gpio_set_value(sck
, 1);
70 rtl8366_smi_clk_delay(smi
);
71 gpio_set_value(sda
, 1);
72 rtl8366_smi_clk_delay(smi
);
73 gpio_set_value(sck
, 1);
74 rtl8366_smi_clk_delay(smi
);
75 gpio_set_value(sck
, 0);
76 rtl8366_smi_clk_delay(smi
);
77 gpio_set_value(sck
, 1);
80 rtl8366_smi_clk_delay(smi
);
81 gpio_set_value(sck
, 0);
82 rtl8366_smi_clk_delay(smi
);
83 gpio_set_value(sck
, 1);
85 /* set GPIO pins to input mode */
86 gpio_direction_input(sda
);
87 gpio_direction_input(sck
);
90 static void rtl8366_smi_write_bits(struct rtl8366_smi
*smi
, u32 data
, u32 len
)
92 unsigned int sda
= smi
->gpio_sda
;
93 unsigned int sck
= smi
->gpio_sck
;
95 for (; len
> 0; len
--) {
96 rtl8366_smi_clk_delay(smi
);
99 gpio_set_value(sda
, !!(data
& ( 1 << (len
- 1))));
100 rtl8366_smi_clk_delay(smi
);
103 gpio_set_value(sck
, 1);
104 rtl8366_smi_clk_delay(smi
);
105 gpio_set_value(sck
, 0);
109 static void rtl8366_smi_read_bits(struct rtl8366_smi
*smi
, u32 len
, u32
*data
)
111 unsigned int sda
= smi
->gpio_sda
;
112 unsigned int sck
= smi
->gpio_sck
;
114 gpio_direction_input(sda
);
116 for (*data
= 0; len
> 0; len
--) {
119 rtl8366_smi_clk_delay(smi
);
122 gpio_set_value(sck
, 1);
123 rtl8366_smi_clk_delay(smi
);
124 u
= !!gpio_get_value(sda
);
125 gpio_set_value(sck
, 0);
127 *data
|= (u
<< (len
- 1));
130 gpio_direction_output(sda
, 0);
133 static int rtl8366_smi_wait_for_ack(struct rtl8366_smi
*smi
)
141 rtl8366_smi_read_bits(smi
, 1, &ack
);
145 if (++retry_cnt
> RTL8366_SMI_ACK_RETRY_COUNT
)
152 static int rtl8366_smi_write_byte(struct rtl8366_smi
*smi
, u8 data
)
154 rtl8366_smi_write_bits(smi
, data
, 8);
155 return rtl8366_smi_wait_for_ack(smi
);
158 static int rtl8366_smi_read_byte0(struct rtl8366_smi
*smi
, u8
*data
)
163 rtl8366_smi_read_bits(smi
, 8, &t
);
167 rtl8366_smi_write_bits(smi
, 0x00, 1);
172 static int rtl8366_smi_read_byte1(struct rtl8366_smi
*smi
, u8
*data
)
177 rtl8366_smi_read_bits(smi
, 8, &t
);
181 rtl8366_smi_write_bits(smi
, 0x01, 1);
186 int rtl8366_smi_read_reg(struct rtl8366_smi
*smi
, u32 addr
, u32
*data
)
193 spin_lock_irqsave(&smi
->lock
, flags
);
195 rtl8366_smi_start(smi
);
197 /* send READ command */
198 ret
= rtl8366_smi_write_byte(smi
, 0x0a << 4 | 0x04 << 1 | 0x01);
203 ret
= rtl8366_smi_write_byte(smi
, addr
& 0xff);
208 ret
= rtl8366_smi_write_byte(smi
, addr
>> 8);
213 rtl8366_smi_read_byte0(smi
, &lo
);
214 /* read DATA[15:8] */
215 rtl8366_smi_read_byte1(smi
, &hi
);
217 *data
= ((u32
) lo
) | (((u32
) hi
) << 8);
222 rtl8366_smi_stop(smi
);
223 spin_unlock_irqrestore(&smi
->lock
, flags
);
227 EXPORT_SYMBOL_GPL(rtl8366_smi_read_reg
);
229 int rtl8366_smi_write_reg(struct rtl8366_smi
*smi
, u32 addr
, u32 data
)
234 spin_lock_irqsave(&smi
->lock
, flags
);
236 rtl8366_smi_start(smi
);
238 /* send WRITE command */
239 ret
= rtl8366_smi_write_byte(smi
, 0x0a << 4 | 0x04 << 1 | 0x00);
244 ret
= rtl8366_smi_write_byte(smi
, addr
& 0xff);
249 ret
= rtl8366_smi_write_byte(smi
, addr
>> 8);
253 /* write DATA[7:0] */
254 ret
= rtl8366_smi_write_byte(smi
, data
& 0xff);
258 /* write DATA[15:8] */
259 ret
= rtl8366_smi_write_byte(smi
, data
>> 8);
266 rtl8366_smi_stop(smi
);
267 spin_unlock_irqrestore(&smi
->lock
, flags
);
271 EXPORT_SYMBOL_GPL(rtl8366_smi_write_reg
);
273 int rtl8366_smi_rmwr(struct rtl8366_smi
*smi
, u32 addr
, u32 mask
, u32 data
)
278 err
= rtl8366_smi_read_reg(smi
, addr
, &t
);
282 err
= rtl8366_smi_write_reg(smi
, addr
, (t
& ~mask
) | data
);
286 EXPORT_SYMBOL_GPL(rtl8366_smi_rmwr
);
288 static int rtl8366_mc_is_used(struct rtl8366_smi
*smi
, int mc_index
, int *used
)
294 for (i
= 0; i
< smi
->num_ports
; i
++) {
297 err
= smi
->ops
->get_mc_index(smi
, i
, &index
);
301 if (mc_index
== index
) {
310 static int rtl8366_set_vlan(struct rtl8366_smi
*smi
, int vid
, u32 member
,
313 struct rtl8366_vlan_4k vlan4k
;
317 /* Update the 4K table */
318 err
= smi
->ops
->get_vlan_4k(smi
, vid
, &vlan4k
);
322 vlan4k
.member
= member
;
323 vlan4k
.untag
= untag
;
325 err
= smi
->ops
->set_vlan_4k(smi
, &vlan4k
);
329 /* Try to find an existing MC entry for this VID */
330 for (i
= 0; i
< smi
->num_vlan_mc
; i
++) {
331 struct rtl8366_vlan_mc vlanmc
;
333 err
= smi
->ops
->get_vlan_mc(smi
, i
, &vlanmc
);
337 if (vid
== vlanmc
.vid
) {
338 /* update the MC entry */
339 vlanmc
.member
= member
;
340 vlanmc
.untag
= untag
;
343 err
= smi
->ops
->set_vlan_mc(smi
, i
, &vlanmc
);
351 static int rtl8366_get_pvid(struct rtl8366_smi
*smi
, int port
, int *val
)
353 struct rtl8366_vlan_mc vlanmc
;
357 err
= smi
->ops
->get_mc_index(smi
, port
, &index
);
361 err
= smi
->ops
->get_vlan_mc(smi
, index
, &vlanmc
);
369 static int rtl8366_set_pvid(struct rtl8366_smi
*smi
, unsigned port
,
372 struct rtl8366_vlan_mc vlanmc
;
373 struct rtl8366_vlan_4k vlan4k
;
377 /* Try to find an existing MC entry for this VID */
378 for (i
= 0; i
< smi
->num_vlan_mc
; i
++) {
379 err
= smi
->ops
->get_vlan_mc(smi
, i
, &vlanmc
);
383 if (vid
== vlanmc
.vid
) {
384 err
= smi
->ops
->set_vlan_mc(smi
, i
, &vlanmc
);
388 err
= smi
->ops
->set_mc_index(smi
, port
, i
);
393 /* We have no MC entry for this VID, try to find an empty one */
394 for (i
= 0; i
< smi
->num_vlan_mc
; i
++) {
395 err
= smi
->ops
->get_vlan_mc(smi
, i
, &vlanmc
);
399 if (vlanmc
.vid
== 0 && vlanmc
.member
== 0) {
400 /* Update the entry from the 4K table */
401 err
= smi
->ops
->get_vlan_4k(smi
, vid
, &vlan4k
);
406 vlanmc
.member
= vlan4k
.member
;
407 vlanmc
.untag
= vlan4k
.untag
;
408 vlanmc
.fid
= vlan4k
.fid
;
409 err
= smi
->ops
->set_vlan_mc(smi
, i
, &vlanmc
);
413 err
= smi
->ops
->set_mc_index(smi
, port
, i
);
418 /* MC table is full, try to find an unused entry and replace it */
419 for (i
= 0; i
< smi
->num_vlan_mc
; i
++) {
422 err
= rtl8366_mc_is_used(smi
, i
, &used
);
427 /* Update the entry from the 4K table */
428 err
= smi
->ops
->get_vlan_4k(smi
, vid
, &vlan4k
);
433 vlanmc
.member
= vlan4k
.member
;
434 vlanmc
.untag
= vlan4k
.untag
;
435 vlanmc
.fid
= vlan4k
.fid
;
436 err
= smi
->ops
->set_vlan_mc(smi
, i
, &vlanmc
);
440 err
= smi
->ops
->set_mc_index(smi
, port
, i
);
446 "all VLAN member configurations are in use\n");
451 static int rtl8366_enable_vlan(struct rtl8366_smi
*smi
, int enable
)
455 err
= smi
->ops
->enable_vlan(smi
, enable
);
459 smi
->vlan_enabled
= enable
;
462 smi
->vlan4k_enabled
= 0;
463 err
= smi
->ops
->enable_vlan4k(smi
, enable
);
469 static int rtl8366_enable_vlan4k(struct rtl8366_smi
*smi
, int enable
)
474 err
= smi
->ops
->enable_vlan(smi
, enable
);
478 smi
->vlan_enabled
= enable
;
481 err
= smi
->ops
->enable_vlan4k(smi
, enable
);
485 smi
->vlan4k_enabled
= enable
;
489 int rtl8366_reset_vlan(struct rtl8366_smi
*smi
)
491 struct rtl8366_vlan_mc vlanmc
;
495 rtl8366_enable_vlan(smi
, 0);
496 rtl8366_enable_vlan4k(smi
, 0);
498 /* clear VLAN member configurations */
504 for (i
= 0; i
< smi
->num_vlan_mc
; i
++) {
505 err
= smi
->ops
->set_vlan_mc(smi
, i
, &vlanmc
);
510 for (i
= 0; i
< smi
->num_ports
; i
++) {
511 if (i
== smi
->cpu_port
)
514 err
= rtl8366_set_vlan(smi
, (i
+ 1),
515 (1 << i
) | (1 << smi
->cpu_port
),
516 (1 << i
) | (1 << smi
->cpu_port
),
521 err
= rtl8366_set_pvid(smi
, i
, (i
+ 1));
528 EXPORT_SYMBOL_GPL(rtl8366_reset_vlan
);
530 #ifdef CONFIG_RTL8366S_PHY_DEBUG_FS
531 int rtl8366_debugfs_open(struct inode
*inode
, struct file
*file
)
533 file
->private_data
= inode
->i_private
;
536 EXPORT_SYMBOL_GPL(rtl8366_debugfs_open
);
538 static ssize_t
rtl8366_read_debugfs_vlan_mc(struct file
*file
,
539 char __user
*user_buf
,
540 size_t count
, loff_t
*ppos
)
542 struct rtl8366_smi
*smi
= (struct rtl8366_smi
*)file
->private_data
;
544 char *buf
= smi
->buf
;
546 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
,
547 "%2s %6s %4s %6s %6s %3s\n",
548 "id", "vid","prio", "member", "untag", "fid");
550 for (i
= 0; i
< smi
->num_vlan_mc
; ++i
) {
551 struct rtl8366_vlan_mc vlanmc
;
553 smi
->ops
->get_vlan_mc(smi
, i
, &vlanmc
);
555 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
,
556 "%2d %6d %4d 0x%04x 0x%04x %3d\n",
557 i
, vlanmc
.vid
, vlanmc
.priority
,
558 vlanmc
.member
, vlanmc
.untag
, vlanmc
.fid
);
561 return simple_read_from_buffer(user_buf
, count
, ppos
, buf
, len
);
564 #define RTL8366_VLAN4K_PAGE_SIZE 64
565 #define RTL8366_VLAN4K_NUM_PAGES (4096 / RTL8366_VLAN4K_PAGE_SIZE)
567 static ssize_t
rtl8366_read_debugfs_vlan_4k(struct file
*file
,
568 char __user
*user_buf
,
569 size_t count
, loff_t
*ppos
)
571 struct rtl8366_smi
*smi
= (struct rtl8366_smi
*)file
->private_data
;
574 char *buf
= smi
->buf
;
576 if (smi
->dbg_vlan_4k_page
>= RTL8366_VLAN4K_NUM_PAGES
) {
577 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
,
578 "invalid page: %u\n", smi
->dbg_vlan_4k_page
);
579 return simple_read_from_buffer(user_buf
, count
, ppos
, buf
, len
);
582 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
,
584 "vid", "member", "untag", "fid");
586 offset
= RTL8366_VLAN4K_PAGE_SIZE
* smi
->dbg_vlan_4k_page
;
587 for (i
= 0; i
< RTL8366_VLAN4K_PAGE_SIZE
; i
++) {
588 struct rtl8366_vlan_4k vlan4k
;
590 smi
->ops
->get_vlan_4k(smi
, offset
+ i
, &vlan4k
);
592 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
,
593 "%4d 0x%04x 0x%04x %3d\n",
594 vlan4k
.vid
, vlan4k
.member
,
595 vlan4k
.untag
, vlan4k
.fid
);
598 return simple_read_from_buffer(user_buf
, count
, ppos
, buf
, len
);
601 static ssize_t
rtl8366_read_debugfs_pvid(struct file
*file
,
602 char __user
*user_buf
,
603 size_t count
, loff_t
*ppos
)
605 struct rtl8366_smi
*smi
= (struct rtl8366_smi
*)file
->private_data
;
606 char *buf
= smi
->buf
;
610 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
, "%4s %4s\n",
613 for (i
= 0; i
< smi
->num_ports
; i
++) {
617 err
= rtl8366_get_pvid(smi
, i
, &pvid
);
619 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
,
622 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
,
623 "%4d %4d\n", i
, pvid
);
626 return simple_read_from_buffer(user_buf
, count
, ppos
, buf
, len
);
629 static ssize_t
rtl8366_read_debugfs_reg(struct file
*file
,
630 char __user
*user_buf
,
631 size_t count
, loff_t
*ppos
)
633 struct rtl8366_smi
*smi
= (struct rtl8366_smi
*)file
->private_data
;
634 u32 t
, reg
= smi
->dbg_reg
;
636 char *buf
= smi
->buf
;
638 memset(buf
, '\0', sizeof(smi
->buf
));
640 err
= rtl8366_smi_read_reg(smi
, reg
, &t
);
642 len
+= snprintf(buf
, sizeof(smi
->buf
),
643 "Read failed (reg: 0x%04x)\n", reg
);
644 return simple_read_from_buffer(user_buf
, count
, ppos
, buf
, len
);
647 len
+= snprintf(buf
, sizeof(smi
->buf
), "reg = 0x%04x, val = 0x%04x\n",
650 return simple_read_from_buffer(user_buf
, count
, ppos
, buf
, len
);
653 static ssize_t
rtl8366_write_debugfs_reg(struct file
*file
,
654 const char __user
*user_buf
,
655 size_t count
, loff_t
*ppos
)
657 struct rtl8366_smi
*smi
= (struct rtl8366_smi
*)file
->private_data
;
659 u32 reg
= smi
->dbg_reg
;
662 char *buf
= smi
->buf
;
664 len
= min(count
, sizeof(smi
->buf
) - 1);
665 if (copy_from_user(buf
, user_buf
, len
)) {
666 dev_err(smi
->parent
, "copy from user failed\n");
671 if (len
> 0 && buf
[len
- 1] == '\n')
675 if (strict_strtoul(buf
, 16, &data
)) {
676 dev_err(smi
->parent
, "Invalid reg value %s\n", buf
);
678 err
= rtl8366_smi_write_reg(smi
, reg
, data
);
681 "writing reg 0x%04x val 0x%04lx failed\n",
689 static ssize_t
rtl8366_read_debugfs_mibs(struct file
*file
,
690 char __user
*user_buf
,
691 size_t count
, loff_t
*ppos
)
693 struct rtl8366_smi
*smi
= file
->private_data
;
695 char *buf
= smi
->buf
;
697 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
, "%-36s",
700 for (i
= 0; i
< smi
->num_ports
; i
++) {
703 snprintf(port_buf
, sizeof(port_buf
), "Port %d", i
);
704 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
, " %12s",
707 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
, "\n");
709 for (i
= 0; i
< smi
->num_mib_counters
; i
++) {
710 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
, "%-36s ",
711 smi
->mib_counters
[i
].name
);
712 for (j
= 0; j
< smi
->num_ports
; j
++) {
713 unsigned long long counter
= 0;
715 if (!smi
->ops
->get_mib_counter(smi
, i
, j
, &counter
))
716 len
+= snprintf(buf
+ len
,
717 sizeof(smi
->buf
) - len
,
720 len
+= snprintf(buf
+ len
,
721 sizeof(smi
->buf
) - len
,
724 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
, "\n");
727 return simple_read_from_buffer(user_buf
, count
, ppos
, buf
, len
);
730 static const struct file_operations fops_rtl8366_regs
= {
731 .read
= rtl8366_read_debugfs_reg
,
732 .write
= rtl8366_write_debugfs_reg
,
733 .open
= rtl8366_debugfs_open
,
737 static const struct file_operations fops_rtl8366_vlan_mc
= {
738 .read
= rtl8366_read_debugfs_vlan_mc
,
739 .open
= rtl8366_debugfs_open
,
743 static const struct file_operations fops_rtl8366_vlan_4k
= {
744 .read
= rtl8366_read_debugfs_vlan_4k
,
745 .open
= rtl8366_debugfs_open
,
749 static const struct file_operations fops_rtl8366_pvid
= {
750 .read
= rtl8366_read_debugfs_pvid
,
751 .open
= rtl8366_debugfs_open
,
755 static const struct file_operations fops_rtl8366_mibs
= {
756 .read
= rtl8366_read_debugfs_mibs
,
757 .open
= rtl8366_debugfs_open
,
761 static void rtl8366_debugfs_init(struct rtl8366_smi
*smi
)
766 if (!smi
->debugfs_root
)
767 smi
->debugfs_root
= debugfs_create_dir(dev_name(smi
->parent
),
770 if (!smi
->debugfs_root
) {
771 dev_err(smi
->parent
, "Unable to create debugfs dir\n");
774 root
= smi
->debugfs_root
;
776 node
= debugfs_create_x16("reg", S_IRUGO
| S_IWUSR
, root
,
779 dev_err(smi
->parent
, "Creating debugfs file '%s' failed\n",
784 node
= debugfs_create_file("val", S_IRUGO
| S_IWUSR
, root
, smi
,
787 dev_err(smi
->parent
, "Creating debugfs file '%s' failed\n",
792 node
= debugfs_create_file("vlan_mc", S_IRUSR
, root
, smi
,
793 &fops_rtl8366_vlan_mc
);
795 dev_err(smi
->parent
, "Creating debugfs file '%s' failed\n",
800 node
= debugfs_create_u8("vlan_4k_page", S_IRUGO
| S_IWUSR
, root
,
801 &smi
->dbg_vlan_4k_page
);
803 dev_err(smi
->parent
, "Creating debugfs file '%s' failed\n",
808 node
= debugfs_create_file("vlan_4k", S_IRUSR
, root
, smi
,
809 &fops_rtl8366_vlan_4k
);
811 dev_err(smi
->parent
, "Creating debugfs file '%s' failed\n",
816 node
= debugfs_create_file("pvid", S_IRUSR
, root
, smi
,
819 dev_err(smi
->parent
, "Creating debugfs file '%s' failed\n",
824 node
= debugfs_create_file("mibs", S_IRUSR
, smi
->debugfs_root
, smi
,
827 dev_err(smi
->parent
, "Creating debugfs file '%s' failed\n",
831 static void rtl8366_debugfs_remove(struct rtl8366_smi
*smi
)
833 if (smi
->debugfs_root
) {
834 debugfs_remove_recursive(smi
->debugfs_root
);
835 smi
->debugfs_root
= NULL
;
839 static inline void rtl8366_debugfs_init(struct rtl8366_smi
*smi
) {}
840 static inline void rtl8366_debugfs_remove(struct rtl8366_smi
*smi
) {}
841 #endif /* CONFIG_RTL8366S_PHY_DEBUG_FS */
843 static int rtl8366_smi_mii_init(struct rtl8366_smi
*smi
)
848 smi
->mii_bus
= mdiobus_alloc();
849 if (smi
->mii_bus
== NULL
) {
854 smi
->mii_bus
->priv
= (void *) smi
;
855 smi
->mii_bus
->name
= dev_name(smi
->parent
);
856 smi
->mii_bus
->read
= smi
->ops
->mii_read
;
857 smi
->mii_bus
->write
= smi
->ops
->mii_write
;
858 snprintf(smi
->mii_bus
->id
, MII_BUS_ID_SIZE
, "%s",
859 dev_name(smi
->parent
));
860 smi
->mii_bus
->parent
= smi
->parent
;
861 smi
->mii_bus
->phy_mask
= ~(0x1f);
862 smi
->mii_bus
->irq
= smi
->mii_irq
;
863 for (i
= 0; i
< PHY_MAX_ADDR
; i
++)
864 smi
->mii_irq
[i
] = PHY_POLL
;
866 ret
= mdiobus_register(smi
->mii_bus
);
873 mdiobus_free(smi
->mii_bus
);
878 static void rtl8366_smi_mii_cleanup(struct rtl8366_smi
*smi
)
880 mdiobus_unregister(smi
->mii_bus
);
881 mdiobus_free(smi
->mii_bus
);
884 int rtl8366_sw_get_port_pvid(struct switch_dev
*dev
, int port
, int *val
)
886 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
887 return rtl8366_get_pvid(smi
, port
, val
);
889 EXPORT_SYMBOL_GPL(rtl8366_sw_get_port_pvid
);
891 int rtl8366_sw_set_port_pvid(struct switch_dev
*dev
, int port
, int val
)
893 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
894 return rtl8366_set_pvid(smi
, port
, val
);
896 EXPORT_SYMBOL_GPL(rtl8366_sw_set_port_pvid
);
898 int rtl8366_sw_get_port_mib(struct switch_dev
*dev
,
899 const struct switch_attr
*attr
,
900 struct switch_val
*val
)
902 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
904 unsigned long long counter
= 0;
905 char *buf
= smi
->buf
;
907 if (val
->port_vlan
>= smi
->num_ports
)
910 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
,
911 "Port %d MIB counters\n",
914 for (i
= 0; i
< smi
->num_mib_counters
; ++i
) {
915 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
,
916 "%-36s: ", smi
->mib_counters
[i
].name
);
917 if (!smi
->ops
->get_mib_counter(smi
, i
, val
->port_vlan
,
919 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
,
922 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
,
930 EXPORT_SYMBOL_GPL(rtl8366_sw_get_port_mib
);
932 int rtl8366_sw_get_vlan_info(struct switch_dev
*dev
,
933 const struct switch_attr
*attr
,
934 struct switch_val
*val
)
938 struct rtl8366_vlan_4k vlan4k
;
939 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
940 char *buf
= smi
->buf
;
943 if (!smi
->ops
->is_vlan_valid(smi
, val
->port_vlan
))
946 memset(buf
, '\0', sizeof(smi
->buf
));
948 err
= smi
->ops
->get_vlan_4k(smi
, val
->port_vlan
, &vlan4k
);
952 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
,
953 "VLAN %d: Ports: '", vlan4k
.vid
);
955 for (i
= 0; i
< smi
->num_ports
; i
++) {
956 if (!(vlan4k
.member
& (1 << i
)))
959 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
, "%d%s", i
,
960 (vlan4k
.untag
& (1 << i
)) ? "" : "t");
963 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
,
964 "', members=%04x, untag=%04x, fid=%u",
965 vlan4k
.member
, vlan4k
.untag
, vlan4k
.fid
);
972 EXPORT_SYMBOL_GPL(rtl8366_sw_get_vlan_info
);
974 int rtl8366_sw_get_vlan_ports(struct switch_dev
*dev
, struct switch_val
*val
)
976 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
977 struct switch_port
*port
;
978 struct rtl8366_vlan_4k vlan4k
;
981 if (!smi
->ops
->is_vlan_valid(smi
, val
->port_vlan
))
984 smi
->ops
->get_vlan_4k(smi
, val
->port_vlan
, &vlan4k
);
986 port
= &val
->value
.ports
[0];
988 for (i
= 0; i
< smi
->num_ports
; i
++) {
989 if (!(vlan4k
.member
& BIT(i
)))
993 port
->flags
= (vlan4k
.untag
& BIT(i
)) ?
994 0 : BIT(SWITCH_PORT_FLAG_TAGGED
);
1000 EXPORT_SYMBOL_GPL(rtl8366_sw_get_vlan_ports
);
1002 int rtl8366_sw_set_vlan_ports(struct switch_dev
*dev
, struct switch_val
*val
)
1004 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
1005 struct switch_port
*port
;
1011 if (!smi
->ops
->is_vlan_valid(smi
, val
->port_vlan
))
1014 port
= &val
->value
.ports
[0];
1015 for (i
= 0; i
< val
->len
; i
++, port
++) {
1016 member
|= BIT(port
->id
);
1018 if (!(port
->flags
& BIT(SWITCH_PORT_FLAG_TAGGED
)))
1019 untag
|= BIT(port
->id
);
1022 * To ensure that we have a valid MC entry for this VLAN,
1023 * initialize the port VLAN ID here.
1025 err
= rtl8366_set_pvid(smi
, port
->id
, val
->port_vlan
);
1030 return rtl8366_set_vlan(smi
, val
->port_vlan
, member
, untag
, 0);
1032 EXPORT_SYMBOL_GPL(rtl8366_sw_set_vlan_ports
);
1034 int rtl8366_sw_get_vlan_fid(struct switch_dev
*dev
,
1035 const struct switch_attr
*attr
,
1036 struct switch_val
*val
)
1038 struct rtl8366_vlan_4k vlan4k
;
1039 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
1042 if (!smi
->ops
->is_vlan_valid(smi
, val
->port_vlan
))
1045 err
= smi
->ops
->get_vlan_4k(smi
, val
->port_vlan
, &vlan4k
);
1049 val
->value
.i
= vlan4k
.fid
;
1053 EXPORT_SYMBOL_GPL(rtl8366_sw_get_vlan_fid
);
1055 int rtl8366_sw_set_vlan_fid(struct switch_dev
*dev
,
1056 const struct switch_attr
*attr
,
1057 struct switch_val
*val
)
1059 struct rtl8366_vlan_4k vlan4k
;
1060 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
1063 if (!smi
->ops
->is_vlan_valid(smi
, val
->port_vlan
))
1066 if (val
->value
.i
< 0 || val
->value
.i
> attr
->max
)
1069 err
= smi
->ops
->get_vlan_4k(smi
, val
->port_vlan
, &vlan4k
);
1073 return rtl8366_set_vlan(smi
, val
->port_vlan
,
1078 EXPORT_SYMBOL_GPL(rtl8366_sw_set_vlan_fid
);
1080 int rtl8366_sw_get_vlan_enable(struct switch_dev
*dev
,
1081 const struct switch_attr
*attr
,
1082 struct switch_val
*val
)
1084 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
1090 val
->value
.i
= smi
->vlan_enabled
;
1092 val
->value
.i
= smi
->vlan4k_enabled
;
1096 EXPORT_SYMBOL_GPL(rtl8366_sw_get_vlan_enable
);
1098 int rtl8366_sw_set_vlan_enable(struct switch_dev
*dev
,
1099 const struct switch_attr
*attr
,
1100 struct switch_val
*val
)
1102 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
1109 err
= rtl8366_enable_vlan(smi
, val
->value
.i
);
1111 err
= rtl8366_enable_vlan4k(smi
, val
->value
.i
);
1115 EXPORT_SYMBOL_GPL(rtl8366_sw_set_vlan_enable
);
1117 struct rtl8366_smi
*rtl8366_smi_alloc(struct device
*parent
)
1119 struct rtl8366_smi
*smi
;
1123 smi
= kzalloc(sizeof(*smi
), GFP_KERNEL
);
1125 dev_err(parent
, "no memory for private data\n");
1129 smi
->parent
= parent
;
1132 EXPORT_SYMBOL_GPL(rtl8366_smi_alloc
);
1134 int rtl8366_smi_init(struct rtl8366_smi
*smi
)
1141 err
= gpio_request(smi
->gpio_sda
, dev_name(smi
->parent
));
1143 dev_err(smi
->parent
, "gpio_request failed for %u, err=%d\n",
1144 smi
->gpio_sda
, err
);
1148 err
= gpio_request(smi
->gpio_sck
, dev_name(smi
->parent
));
1150 dev_err(smi
->parent
, "gpio_request failed for %u, err=%d\n",
1151 smi
->gpio_sck
, err
);
1155 spin_lock_init(&smi
->lock
);
1157 dev_info(smi
->parent
, "using GPIO pins %u (SDA) and %u (SCK)\n",
1158 smi
->gpio_sda
, smi
->gpio_sck
);
1160 err
= smi
->ops
->detect(smi
);
1162 dev_err(smi
->parent
, "chip detection failed, err=%d\n", err
);
1166 err
= smi
->ops
->setup(smi
);
1168 dev_err(smi
->parent
, "chip setup failed, err=%d\n", err
);
1172 err
= rtl8366_smi_mii_init(smi
);
1176 rtl8366_debugfs_init(smi
);
1181 gpio_free(smi
->gpio_sck
);
1183 gpio_free(smi
->gpio_sda
);
1187 EXPORT_SYMBOL_GPL(rtl8366_smi_init
);
1189 void rtl8366_smi_cleanup(struct rtl8366_smi
*smi
)
1191 rtl8366_debugfs_remove(smi
);
1192 rtl8366_smi_mii_cleanup(smi
);
1193 gpio_free(smi
->gpio_sck
);
1194 gpio_free(smi
->gpio_sda
);
1196 EXPORT_SYMBOL_GPL(rtl8366_smi_cleanup
);
1198 MODULE_DESCRIPTION("Realtek RTL8366 SMI interface driver");
1199 MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
1200 MODULE_LICENSE("GPL v2");