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 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_reset_vlan(struct rtl8366_smi
*smi
)
454 struct rtl8366_vlan_mc vlanmc
;
458 /* clear VLAN member configurations */
464 for (i
= 0; i
< smi
->num_vlan_mc
; i
++) {
465 err
= smi
->ops
->set_vlan_mc(smi
, i
, &vlanmc
);
470 for (i
= 0; i
< smi
->num_ports
; i
++) {
471 if (i
== smi
->cpu_port
)
474 err
= rtl8366_set_vlan(smi
, (i
+ 1),
475 (1 << i
) | (1 << smi
->cpu_port
),
476 (1 << i
) | (1 << smi
->cpu_port
),
481 err
= rtl8366_set_pvid(smi
, i
, (i
+ 1));
488 EXPORT_SYMBOL_GPL(rtl8366_reset_vlan
);
490 #ifdef CONFIG_RTL8366S_PHY_DEBUG_FS
491 int rtl8366_debugfs_open(struct inode
*inode
, struct file
*file
)
493 file
->private_data
= inode
->i_private
;
496 EXPORT_SYMBOL_GPL(rtl8366_debugfs_open
);
498 static ssize_t
rtl8366_read_debugfs_vlan_mc(struct file
*file
,
499 char __user
*user_buf
,
500 size_t count
, loff_t
*ppos
)
502 struct rtl8366_smi
*smi
= (struct rtl8366_smi
*)file
->private_data
;
504 char *buf
= smi
->buf
;
506 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
,
507 "%2s %6s %4s %6s %6s %3s\n",
508 "id", "vid","prio", "member", "untag", "fid");
510 for (i
= 0; i
< smi
->num_vlan_mc
; ++i
) {
511 struct rtl8366_vlan_mc vlanmc
;
513 smi
->ops
->get_vlan_mc(smi
, i
, &vlanmc
);
515 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
,
516 "%2d %6d %4d 0x%04x 0x%04x %3d\n",
517 i
, vlanmc
.vid
, vlanmc
.priority
,
518 vlanmc
.member
, vlanmc
.untag
, vlanmc
.fid
);
521 return simple_read_from_buffer(user_buf
, count
, ppos
, buf
, len
);
524 static ssize_t
rtl8366_read_debugfs_reg(struct file
*file
,
525 char __user
*user_buf
,
526 size_t count
, loff_t
*ppos
)
528 struct rtl8366_smi
*smi
= (struct rtl8366_smi
*)file
->private_data
;
529 u32 t
, reg
= smi
->dbg_reg
;
531 char *buf
= smi
->buf
;
533 memset(buf
, '\0', sizeof(smi
->buf
));
535 err
= rtl8366_smi_read_reg(smi
, reg
, &t
);
537 len
+= snprintf(buf
, sizeof(smi
->buf
),
538 "Read failed (reg: 0x%04x)\n", reg
);
539 return simple_read_from_buffer(user_buf
, count
, ppos
, buf
, len
);
542 len
+= snprintf(buf
, sizeof(smi
->buf
), "reg = 0x%04x, val = 0x%04x\n",
545 return simple_read_from_buffer(user_buf
, count
, ppos
, buf
, len
);
548 static ssize_t
rtl8366_write_debugfs_reg(struct file
*file
,
549 const char __user
*user_buf
,
550 size_t count
, loff_t
*ppos
)
552 struct rtl8366_smi
*smi
= (struct rtl8366_smi
*)file
->private_data
;
554 u32 reg
= smi
->dbg_reg
;
557 char *buf
= smi
->buf
;
559 len
= min(count
, sizeof(smi
->buf
) - 1);
560 if (copy_from_user(buf
, user_buf
, len
)) {
561 dev_err(smi
->parent
, "copy from user failed\n");
566 if (len
> 0 && buf
[len
- 1] == '\n')
570 if (strict_strtoul(buf
, 16, &data
)) {
571 dev_err(smi
->parent
, "Invalid reg value %s\n", buf
);
573 err
= rtl8366_smi_write_reg(smi
, reg
, data
);
576 "writing reg 0x%04x val 0x%04lx failed\n",
584 static ssize_t
rtl8366_read_debugfs_mibs(struct file
*file
,
585 char __user
*user_buf
,
586 size_t count
, loff_t
*ppos
)
588 struct rtl8366_smi
*smi
= file
->private_data
;
590 char *buf
= smi
->buf
;
592 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
, "%-36s",
595 for (i
= 0; i
< smi
->num_ports
; i
++) {
598 snprintf(port_buf
, sizeof(port_buf
), "Port %d", i
);
599 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
, " %12s",
602 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
, "\n");
604 for (i
= 0; i
< smi
->num_mib_counters
; i
++) {
605 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
, "%-36s ",
606 smi
->mib_counters
[i
].name
);
607 for (j
= 0; j
< smi
->num_ports
; j
++) {
608 unsigned long long counter
= 0;
610 if (!smi
->ops
->get_mib_counter(smi
, i
, j
, &counter
))
611 len
+= snprintf(buf
+ len
,
612 sizeof(smi
->buf
) - len
,
615 len
+= snprintf(buf
+ len
,
616 sizeof(smi
->buf
) - len
,
619 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
, "\n");
622 return simple_read_from_buffer(user_buf
, count
, ppos
, buf
, len
);
625 static const struct file_operations fops_rtl8366_regs
= {
626 .read
= rtl8366_read_debugfs_reg
,
627 .write
= rtl8366_write_debugfs_reg
,
628 .open
= rtl8366_debugfs_open
,
632 static const struct file_operations fops_rtl8366_vlan_mc
= {
633 .read
= rtl8366_read_debugfs_vlan_mc
,
634 .open
= rtl8366_debugfs_open
,
638 static const struct file_operations fops_rtl8366_mibs
= {
639 .read
= rtl8366_read_debugfs_mibs
,
640 .open
= rtl8366_debugfs_open
,
644 static void rtl8366_debugfs_init(struct rtl8366_smi
*smi
)
649 if (!smi
->debugfs_root
)
650 smi
->debugfs_root
= debugfs_create_dir(dev_name(smi
->parent
),
653 if (!smi
->debugfs_root
) {
654 dev_err(smi
->parent
, "Unable to create debugfs dir\n");
657 root
= smi
->debugfs_root
;
659 node
= debugfs_create_x16("reg", S_IRUGO
| S_IWUSR
, root
,
662 dev_err(smi
->parent
, "Creating debugfs file '%s' failed\n",
667 node
= debugfs_create_file("val", S_IRUGO
| S_IWUSR
, root
, smi
,
670 dev_err(smi
->parent
, "Creating debugfs file '%s' failed\n",
675 node
= debugfs_create_file("vlan_mc", S_IRUSR
, root
, smi
,
676 &fops_rtl8366_vlan_mc
);
678 dev_err(smi
->parent
, "Creating debugfs file '%s' failed\n",
683 node
= debugfs_create_file("mibs", S_IRUSR
, smi
->debugfs_root
, smi
,
686 dev_err(smi
->parent
, "Creating debugfs file '%s' failed\n",
690 static void rtl8366_debugfs_remove(struct rtl8366_smi
*smi
)
692 if (smi
->debugfs_root
) {
693 debugfs_remove_recursive(smi
->debugfs_root
);
694 smi
->debugfs_root
= NULL
;
698 static inline void rtl8366_debugfs_init(struct rtl8366_smi
*smi
) {}
699 static inline void rtl8366_debugfs_remove(struct rtl8366_smi
*smi
) {}
700 #endif /* CONFIG_RTL8366S_PHY_DEBUG_FS */
702 static int rtl8366_smi_mii_init(struct rtl8366_smi
*smi
)
707 smi
->mii_bus
= mdiobus_alloc();
708 if (smi
->mii_bus
== NULL
) {
713 smi
->mii_bus
->priv
= (void *) smi
;
714 smi
->mii_bus
->name
= dev_name(smi
->parent
);
715 smi
->mii_bus
->read
= smi
->ops
->mii_read
;
716 smi
->mii_bus
->write
= smi
->ops
->mii_write
;
717 snprintf(smi
->mii_bus
->id
, MII_BUS_ID_SIZE
, "%s",
718 dev_name(smi
->parent
));
719 smi
->mii_bus
->parent
= smi
->parent
;
720 smi
->mii_bus
->phy_mask
= ~(0x1f);
721 smi
->mii_bus
->irq
= smi
->mii_irq
;
722 for (i
= 0; i
< PHY_MAX_ADDR
; i
++)
723 smi
->mii_irq
[i
] = PHY_POLL
;
725 ret
= mdiobus_register(smi
->mii_bus
);
732 mdiobus_free(smi
->mii_bus
);
737 static void rtl8366_smi_mii_cleanup(struct rtl8366_smi
*smi
)
739 mdiobus_unregister(smi
->mii_bus
);
740 mdiobus_free(smi
->mii_bus
);
743 int rtl8366_sw_get_port_pvid(struct switch_dev
*dev
, int port
, int *val
)
745 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
746 return rtl8366_get_pvid(smi
, port
, val
);
748 EXPORT_SYMBOL_GPL(rtl8366_sw_get_port_pvid
);
750 int rtl8366_sw_set_port_pvid(struct switch_dev
*dev
, int port
, int val
)
752 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
753 return rtl8366_set_pvid(smi
, port
, val
);
755 EXPORT_SYMBOL_GPL(rtl8366_sw_set_port_pvid
);
757 int rtl8366_sw_get_port_mib(struct switch_dev
*dev
,
758 const struct switch_attr
*attr
,
759 struct switch_val
*val
)
761 struct rtl8366_smi
*smi
= sw_to_rtl8366_smi(dev
);
763 unsigned long long counter
= 0;
764 char *buf
= smi
->buf
;
766 if (val
->port_vlan
>= smi
->num_ports
)
769 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
,
770 "Port %d MIB counters\n",
773 for (i
= 0; i
< smi
->num_mib_counters
; ++i
) {
774 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
,
775 "%-36s: ", smi
->mib_counters
[i
].name
);
776 if (!smi
->ops
->get_mib_counter(smi
, i
, val
->port_vlan
,
778 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
,
781 len
+= snprintf(buf
+ len
, sizeof(smi
->buf
) - len
,
789 EXPORT_SYMBOL_GPL(rtl8366_sw_get_port_mib
);
791 struct rtl8366_smi
*rtl8366_smi_alloc(struct device
*parent
)
793 struct rtl8366_smi
*smi
;
797 smi
= kzalloc(sizeof(*smi
), GFP_KERNEL
);
799 dev_err(parent
, "no memory for private data\n");
803 smi
->parent
= parent
;
806 EXPORT_SYMBOL_GPL(rtl8366_smi_alloc
);
808 int rtl8366_smi_init(struct rtl8366_smi
*smi
)
815 err
= gpio_request(smi
->gpio_sda
, dev_name(smi
->parent
));
817 dev_err(smi
->parent
, "gpio_request failed for %u, err=%d\n",
822 err
= gpio_request(smi
->gpio_sck
, dev_name(smi
->parent
));
824 dev_err(smi
->parent
, "gpio_request failed for %u, err=%d\n",
829 spin_lock_init(&smi
->lock
);
831 dev_info(smi
->parent
, "using GPIO pins %u (SDA) and %u (SCK)\n",
832 smi
->gpio_sda
, smi
->gpio_sck
);
834 err
= smi
->ops
->detect(smi
);
836 dev_err(smi
->parent
, "chip detection failed, err=%d\n", err
);
840 err
= smi
->ops
->setup(smi
);
842 dev_err(smi
->parent
, "chip setup failed, err=%d\n", err
);
846 err
= rtl8366_smi_mii_init(smi
);
850 rtl8366_debugfs_init(smi
);
855 gpio_free(smi
->gpio_sck
);
857 gpio_free(smi
->gpio_sda
);
861 EXPORT_SYMBOL_GPL(rtl8366_smi_init
);
863 void rtl8366_smi_cleanup(struct rtl8366_smi
*smi
)
865 rtl8366_debugfs_remove(smi
);
866 rtl8366_smi_mii_cleanup(smi
);
867 gpio_free(smi
->gpio_sck
);
868 gpio_free(smi
->gpio_sda
);
870 EXPORT_SYMBOL_GPL(rtl8366_smi_cleanup
);
872 MODULE_DESCRIPTION("Realtek RTL8366 SMI interface driver");
873 MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
874 MODULE_LICENSE("GPL v2");