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