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 int rtl8366_set_vlan(struct rtl8366_smi
*smi
, int vid
, u32 member
, u32 untag
,
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
);
350 EXPORT_SYMBOL_GPL(rtl8366_set_vlan
);
352 int rtl8366_reset_vlan(struct rtl8366_smi
*smi
)
354 struct rtl8366_vlan_mc vlanmc
;
358 /* clear VLAN member configurations */
364 for (i
= 0; i
< smi
->num_vlan_mc
; i
++) {
365 err
= smi
->ops
->set_vlan_mc(smi
, i
, &vlanmc
);
370 for (i
= 0; i
< smi
->num_ports
; i
++) {
371 if (i
== smi
->cpu_port
)
374 err
= rtl8366_set_vlan(smi
, (i
+ 1),
375 (1 << i
) | (1 << smi
->cpu_port
),
376 (1 << i
) | (1 << smi
->cpu_port
),
381 err
= rtl8366_set_pvid(smi
, i
, (i
+ 1));
388 EXPORT_SYMBOL_GPL(rtl8366_reset_vlan
);
390 int rtl8366_get_pvid(struct rtl8366_smi
*smi
, int port
, int *val
)
392 struct rtl8366_vlan_mc vlanmc
;
396 err
= smi
->ops
->get_mc_index(smi
, port
, &index
);
400 err
= smi
->ops
->get_vlan_mc(smi
, index
, &vlanmc
);
407 EXPORT_SYMBOL_GPL(rtl8366_get_pvid
);
409 int rtl8366_set_pvid(struct rtl8366_smi
*smi
, unsigned port
, unsigned vid
)
411 struct rtl8366_vlan_mc vlanmc
;
412 struct rtl8366_vlan_4k vlan4k
;
416 /* Try to find an existing MC entry for this VID */
417 for (i
= 0; i
< smi
->num_vlan_mc
; i
++) {
418 err
= smi
->ops
->get_vlan_mc(smi
, i
, &vlanmc
);
422 if (vid
== vlanmc
.vid
) {
423 err
= smi
->ops
->set_vlan_mc(smi
, i
, &vlanmc
);
427 err
= smi
->ops
->set_mc_index(smi
, port
, i
);
432 /* We have no MC entry for this VID, try to find an empty one */
433 for (i
= 0; i
< smi
->num_vlan_mc
; i
++) {
434 err
= smi
->ops
->get_vlan_mc(smi
, i
, &vlanmc
);
438 if (vlanmc
.vid
== 0 && vlanmc
.member
== 0) {
439 /* Update the entry from the 4K table */
440 err
= smi
->ops
->get_vlan_4k(smi
, vid
, &vlan4k
);
445 vlanmc
.member
= vlan4k
.member
;
446 vlanmc
.untag
= vlan4k
.untag
;
447 vlanmc
.fid
= vlan4k
.fid
;
448 err
= smi
->ops
->set_vlan_mc(smi
, i
, &vlanmc
);
452 err
= smi
->ops
->set_mc_index(smi
, port
, i
);
457 /* MC table is full, try to find an unused entry and replace it */
458 for (i
= 0; i
< smi
->num_vlan_mc
; i
++) {
461 err
= rtl8366_mc_is_used(smi
, i
, &used
);
466 /* Update the entry from the 4K table */
467 err
= smi
->ops
->get_vlan_4k(smi
, vid
, &vlan4k
);
472 vlanmc
.member
= vlan4k
.member
;
473 vlanmc
.untag
= vlan4k
.untag
;
474 vlanmc
.fid
= vlan4k
.fid
;
475 err
= smi
->ops
->set_vlan_mc(smi
, i
, &vlanmc
);
479 err
= smi
->ops
->set_mc_index(smi
, port
, i
);
485 "all VLAN member configurations are in use\n");
489 EXPORT_SYMBOL_GPL(rtl8366_set_pvid
);
491 #ifdef CONFIG_RTL8366S_PHY_DEBUG_FS
492 int rtl8366_debugfs_open(struct inode
*inode
, struct file
*file
)
494 file
->private_data
= inode
->i_private
;
497 EXPORT_SYMBOL_GPL(rtl8366_debugfs_open
);
499 static ssize_t
rtl8366_read_debugfs_vlan_mc(struct file
*file
,
500 char __user
*user_buf
,
501 size_t count
, loff_t
*ppos
)
503 struct rtl8366_smi
*smi
= (struct rtl8366_smi
*)file
->private_data
;
505 char *buf
= smi
->buf
;
507 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
,
508 "%2s %6s %4s %6s %6s %3s\n",
509 "id", "vid","prio", "member", "untag", "fid");
511 for (i
= 0; i
< smi
->num_vlan_mc
; ++i
) {
512 struct rtl8366_vlan_mc vlanmc
;
514 smi
->ops
->get_vlan_mc(smi
, i
, &vlanmc
);
516 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
,
517 "%2d %6d %4d 0x%04x 0x%04x %3d\n",
518 i
, vlanmc
.vid
, vlanmc
.priority
,
519 vlanmc
.member
, vlanmc
.untag
, vlanmc
.fid
);
522 return simple_read_from_buffer(user_buf
, count
, ppos
, buf
, len
);
525 static ssize_t
rtl8366_read_debugfs_reg(struct file
*file
,
526 char __user
*user_buf
,
527 size_t count
, loff_t
*ppos
)
529 struct rtl8366_smi
*smi
= (struct rtl8366_smi
*)file
->private_data
;
530 u32 t
, reg
= smi
->dbg_reg
;
532 char *buf
= smi
->buf
;
534 memset(buf
, '\0', sizeof(smi
->buf
));
536 err
= rtl8366_smi_read_reg(smi
, reg
, &t
);
538 len
+= snprintf(buf
, sizeof(smi
->buf
),
539 "Read failed (reg: 0x%04x)\n", reg
);
540 return simple_read_from_buffer(user_buf
, count
, ppos
, buf
, len
);
543 len
+= snprintf(buf
, sizeof(smi
->buf
), "reg = 0x%04x, val = 0x%04x\n",
546 return simple_read_from_buffer(user_buf
, count
, ppos
, buf
, len
);
549 static ssize_t
rtl8366_write_debugfs_reg(struct file
*file
,
550 const char __user
*user_buf
,
551 size_t count
, loff_t
*ppos
)
553 struct rtl8366_smi
*smi
= (struct rtl8366_smi
*)file
->private_data
;
555 u32 reg
= smi
->dbg_reg
;
558 char *buf
= smi
->buf
;
560 len
= min(count
, sizeof(smi
->buf
) - 1);
561 if (copy_from_user(buf
, user_buf
, len
)) {
562 dev_err(smi
->parent
, "copy from user failed\n");
567 if (len
> 0 && buf
[len
- 1] == '\n')
571 if (strict_strtoul(buf
, 16, &data
)) {
572 dev_err(smi
->parent
, "Invalid reg value %s\n", buf
);
574 err
= rtl8366_smi_write_reg(smi
, reg
, data
);
577 "writing reg 0x%04x val 0x%04lx failed\n",
585 static ssize_t
rtl8366_read_debugfs_mibs(struct file
*file
,
586 char __user
*user_buf
,
587 size_t count
, loff_t
*ppos
)
589 struct rtl8366_smi
*smi
= file
->private_data
;
591 char *buf
= smi
->buf
;
593 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
, "%-36s",
596 for (i
= 0; i
< smi
->num_ports
; i
++) {
599 snprintf(port_buf
, sizeof(port_buf
), "Port %d", i
);
600 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
, " %12s",
603 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
, "\n");
605 for (i
= 0; i
< smi
->num_mib_counters
; i
++) {
606 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
, "%-36s ",
607 smi
->mib_counters
[i
].name
);
608 for (j
= 0; j
< smi
->num_ports
; j
++) {
609 unsigned long long counter
= 0;
611 if (!smi
->ops
->get_mib_counter(smi
, i
, j
, &counter
))
612 len
+= snprintf(buf
+ len
,
613 sizeof(smi
->buf
) - len
,
616 len
+= snprintf(buf
+ len
,
617 sizeof(smi
->buf
) - len
,
620 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
, "\n");
623 return simple_read_from_buffer(user_buf
, count
, ppos
, buf
, len
);
626 static const struct file_operations fops_rtl8366_regs
= {
627 .read
= rtl8366_read_debugfs_reg
,
628 .write
= rtl8366_write_debugfs_reg
,
629 .open
= rtl8366_debugfs_open
,
633 static const struct file_operations fops_rtl8366_vlan_mc
= {
634 .read
= rtl8366_read_debugfs_vlan_mc
,
635 .open
= rtl8366_debugfs_open
,
639 static const struct file_operations fops_rtl8366_mibs
= {
640 .read
= rtl8366_read_debugfs_mibs
,
641 .open
= rtl8366_debugfs_open
,
645 static void rtl8366_debugfs_init(struct rtl8366_smi
*smi
)
650 if (!smi
->debugfs_root
)
651 smi
->debugfs_root
= debugfs_create_dir(dev_name(smi
->parent
),
654 if (!smi
->debugfs_root
) {
655 dev_err(smi
->parent
, "Unable to create debugfs dir\n");
658 root
= smi
->debugfs_root
;
660 node
= debugfs_create_x16("reg", S_IRUGO
| S_IWUSR
, root
,
663 dev_err(smi
->parent
, "Creating debugfs file '%s' failed\n",
668 node
= debugfs_create_file("val", S_IRUGO
| S_IWUSR
, root
, smi
,
671 dev_err(smi
->parent
, "Creating debugfs file '%s' failed\n",
676 node
= debugfs_create_file("vlan_mc", S_IRUSR
, root
, smi
,
677 &fops_rtl8366_vlan_mc
);
679 dev_err(smi
->parent
, "Creating debugfs file '%s' failed\n",
684 node
= debugfs_create_file("mibs", S_IRUSR
, smi
->debugfs_root
, smi
,
687 dev_err(smi
->parent
, "Creating debugfs file '%s' failed\n",
691 static void rtl8366_debugfs_remove(struct rtl8366_smi
*smi
)
693 if (smi
->debugfs_root
) {
694 debugfs_remove_recursive(smi
->debugfs_root
);
695 smi
->debugfs_root
= NULL
;
699 static inline void rtl8366_debugfs_init(struct rtl8366_smi
*smi
) {}
700 static inline void rtl8366_debugfs_remove(struct rtl8366_smi
*smi
) {}
701 #endif /* CONFIG_RTL8366S_PHY_DEBUG_FS */
703 static int rtl8366_smi_mii_init(struct rtl8366_smi
*smi
)
708 smi
->mii_bus
= mdiobus_alloc();
709 if (smi
->mii_bus
== NULL
) {
714 smi
->mii_bus
->priv
= (void *) smi
;
715 smi
->mii_bus
->name
= dev_name(smi
->parent
);
716 smi
->mii_bus
->read
= smi
->ops
->mii_read
;
717 smi
->mii_bus
->write
= smi
->ops
->mii_write
;
718 snprintf(smi
->mii_bus
->id
, MII_BUS_ID_SIZE
, "%s",
719 dev_name(smi
->parent
));
720 smi
->mii_bus
->parent
= smi
->parent
;
721 smi
->mii_bus
->phy_mask
= ~(0x1f);
722 smi
->mii_bus
->irq
= smi
->mii_irq
;
723 for (i
= 0; i
< PHY_MAX_ADDR
; i
++)
724 smi
->mii_irq
[i
] = PHY_POLL
;
726 ret
= mdiobus_register(smi
->mii_bus
);
733 mdiobus_free(smi
->mii_bus
);
738 static void rtl8366_smi_mii_cleanup(struct rtl8366_smi
*smi
)
740 mdiobus_unregister(smi
->mii_bus
);
741 mdiobus_free(smi
->mii_bus
);
744 int rtl8366_sw_get_port_pvid(struct switch_dev
*dev
, int port
, int *val
)
746 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
747 return rtl8366_get_pvid(smi
, port
, val
);
749 EXPORT_SYMBOL_GPL(rtl8366_sw_get_port_pvid
);
751 int rtl8366_sw_set_port_pvid(struct switch_dev
*dev
, int port
, int val
)
753 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
754 return rtl8366_set_pvid(smi
, port
, val
);
756 EXPORT_SYMBOL_GPL(rtl8366_sw_set_port_pvid
);
758 struct rtl8366_smi
*rtl8366_smi_alloc(struct device
*parent
)
760 struct rtl8366_smi
*smi
;
764 smi
= kzalloc(sizeof(*smi
), GFP_KERNEL
);
766 dev_err(parent
, "no memory for private data\n");
770 smi
->parent
= parent
;
773 EXPORT_SYMBOL_GPL(rtl8366_smi_alloc
);
775 int rtl8366_smi_init(struct rtl8366_smi
*smi
)
782 err
= gpio_request(smi
->gpio_sda
, dev_name(smi
->parent
));
784 dev_err(smi
->parent
, "gpio_request failed for %u, err=%d\n",
789 err
= gpio_request(smi
->gpio_sck
, dev_name(smi
->parent
));
791 dev_err(smi
->parent
, "gpio_request failed for %u, err=%d\n",
796 spin_lock_init(&smi
->lock
);
798 dev_info(smi
->parent
, "using GPIO pins %u (SDA) and %u (SCK)\n",
799 smi
->gpio_sda
, smi
->gpio_sck
);
801 err
= smi
->ops
->detect(smi
);
803 dev_err(smi
->parent
, "chip detection failed, err=%d\n", err
);
807 err
= smi
->ops
->setup(smi
);
809 dev_err(smi
->parent
, "chip setup failed, err=%d\n", err
);
813 err
= rtl8366_smi_mii_init(smi
);
817 rtl8366_debugfs_init(smi
);
822 gpio_free(smi
->gpio_sck
);
824 gpio_free(smi
->gpio_sda
);
828 EXPORT_SYMBOL_GPL(rtl8366_smi_init
);
830 void rtl8366_smi_cleanup(struct rtl8366_smi
*smi
)
832 rtl8366_debugfs_remove(smi
);
833 rtl8366_smi_mii_cleanup(smi
);
834 gpio_free(smi
->gpio_sck
);
835 gpio_free(smi
->gpio_sda
);
837 EXPORT_SYMBOL_GPL(rtl8366_smi_cleanup
);
839 MODULE_DESCRIPTION("Realtek RTL8366 SMI interface driver");
840 MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
841 MODULE_LICENSE("GPL v2");