xref: /linux/drivers/net/dsa/realtek/realtek.h (revision 621cde16e49b3ecf7d59a8106a20aaebfb4a59a9)
1 /* SPDX-License-Identifier: GPL-2.0+ */
2 /* Realtek SMI interface driver defines
3  *
4  * Copyright (C) 2017 Linus Walleij <linus.walleij@linaro.org>
5  * Copyright (C) 2009-2010 Gabor Juhos <juhosg@openwrt.org>
6  */
7 
8 #ifndef _REALTEK_H
9 #define _REALTEK_H
10 
11 #include <linux/phy.h>
12 #include <linux/platform_device.h>
13 #include <linux/gpio/consumer.h>
14 #include <net/dsa.h>
15 #include <linux/reset.h>
16 
17 #define REALTEK_HW_STOP_DELAY		25	/* msecs */
18 #define REALTEK_HW_START_DELAY		100	/* msecs */
19 
20 struct phylink_mac_ops;
21 struct realtek_ops;
22 struct dentry;
23 struct inode;
24 struct file;
25 
26 struct rtl8366_mib_counter {
27 	unsigned int	base;
28 	unsigned int	offset;
29 	unsigned int	length;
30 	const char	*name;
31 };
32 
33 /*
34  * struct rtl8366_vlan_mc - Virtual LAN member configuration
35  */
36 struct rtl8366_vlan_mc {
37 	u16	vid;
38 	u16	untag;
39 	u16	member;
40 	u8	fid;
41 	u8	priority;
42 };
43 
44 struct rtl8366_vlan_4k {
45 	u16	vid;
46 	u16	untag;
47 	u16	member;
48 	u8	fid;
49 };
50 
51 struct realtek_priv {
52 	struct device		*dev;
53 	struct reset_control    *reset_ctl;
54 	struct gpio_desc	*reset;
55 	struct gpio_desc	*mdc;
56 	struct gpio_desc	*mdio;
57 	struct regmap		*map;
58 	struct regmap		*map_nolock;
59 	struct mutex		map_lock;
60 	struct mii_bus		*user_mii_bus;
61 	struct mii_bus		*bus;
62 	int			mdio_addr;
63 
64 	const struct realtek_variant *variant;
65 
66 	spinlock_t		lock; /* Locks around command writes */
67 	struct dsa_switch	ds;
68 	struct irq_domain	*irqdomain;
69 	bool			leds_disabled;
70 
71 	unsigned int		cpu_port;
72 	unsigned int		num_ports;
73 	unsigned int		num_vlan_mc;
74 	unsigned int		num_mib_counters;
75 	struct rtl8366_mib_counter *mib_counters;
76 
77 	const struct realtek_ops *ops;
78 	int			(*write_reg_noack)(void *ctx, u32 addr, u32 data);
79 
80 	int			vlan_enabled;
81 	int			vlan4k_enabled;
82 
83 	char			buf[4096];
84 	void			*chip_data; /* Per-chip extra variant data */
85 };
86 
87 /*
88  * struct realtek_ops - vtable for the per-SMI-chiptype operations
89  * @detect: detects the chiptype
90  */
91 struct realtek_ops {
92 	int	(*detect)(struct realtek_priv *priv);
93 	int	(*reset_chip)(struct realtek_priv *priv);
94 	int	(*setup)(struct realtek_priv *priv);
95 	int	(*get_mib_counter)(struct realtek_priv *priv,
96 				   int port,
97 				   struct rtl8366_mib_counter *mib,
98 				   u64 *mibvalue);
99 	int	(*get_vlan_mc)(struct realtek_priv *priv, u32 index,
100 			       struct rtl8366_vlan_mc *vlanmc);
101 	int	(*set_vlan_mc)(struct realtek_priv *priv, u32 index,
102 			       const struct rtl8366_vlan_mc *vlanmc);
103 	int	(*get_vlan_4k)(struct realtek_priv *priv, u32 vid,
104 			       struct rtl8366_vlan_4k *vlan4k);
105 	int	(*set_vlan_4k)(struct realtek_priv *priv,
106 			       const struct rtl8366_vlan_4k *vlan4k);
107 	int	(*get_mc_index)(struct realtek_priv *priv, int port, int *val);
108 	int	(*set_mc_index)(struct realtek_priv *priv, int port, int index);
109 	bool	(*is_vlan_valid)(struct realtek_priv *priv, unsigned int vlan);
110 	int	(*enable_vlan)(struct realtek_priv *priv, bool enable);
111 	int	(*enable_vlan4k)(struct realtek_priv *priv, bool enable);
112 	int	(*enable_port)(struct realtek_priv *priv, int port, bool enable);
113 	int	(*phy_read)(struct realtek_priv *priv, int phy, int regnum);
114 	int	(*phy_write)(struct realtek_priv *priv, int phy, int regnum,
115 			     u16 val);
116 };
117 
118 struct realtek_variant {
119 	const struct dsa_switch_ops *ds_ops;
120 	const struct realtek_ops *ops;
121 	const struct phylink_mac_ops *phylink_mac_ops;
122 	unsigned int clk_delay;
123 	u8 cmd_read;
124 	u8 cmd_write;
125 	size_t chip_data_sz;
126 };
127 
128 /* RTL8366 library helpers */
129 int rtl8366_mc_is_used(struct realtek_priv *priv, int mc_index, int *used);
130 int rtl8366_set_vlan(struct realtek_priv *priv, int vid, u32 member,
131 		     u32 untag, u32 fid);
132 int rtl8366_set_pvid(struct realtek_priv *priv, unsigned int port,
133 		     unsigned int vid);
134 int rtl8366_enable_vlan4k(struct realtek_priv *priv, bool enable);
135 int rtl8366_enable_vlan(struct realtek_priv *priv, bool enable);
136 int rtl8366_reset_vlan(struct realtek_priv *priv);
137 int rtl8366_vlan_add(struct dsa_switch *ds, int port,
138 		     const struct switchdev_obj_port_vlan *vlan,
139 		     struct netlink_ext_ack *extack);
140 int rtl8366_vlan_del(struct dsa_switch *ds, int port,
141 		     const struct switchdev_obj_port_vlan *vlan);
142 void rtl8366_get_strings(struct dsa_switch *ds, int port, u32 stringset,
143 			 uint8_t *data);
144 int rtl8366_get_sset_count(struct dsa_switch *ds, int port, int sset);
145 void rtl8366_get_ethtool_stats(struct dsa_switch *ds, int port, uint64_t *data);
146 
147 extern const struct realtek_variant rtl8366rb_variant;
148 extern const struct realtek_variant rtl8365mb_variant;
149 
150 #endif /*  _REALTEK_H */
151