xref: /openbmc/linux/drivers/net/dsa/sja1105/sja1105.h (revision dfacc5a23e227cabdff41b6202f510398e90d36b)
1b790b554SNishad Kamdar /* SPDX-License-Identifier: GPL-2.0 */
2b790b554SNishad Kamdar /* Copyright (c) 2018, Sensor-Technik Wiedemann GmbH
38aa9ebccSVladimir Oltean  * Copyright (c) 2018-2019, Vladimir Oltean <olteanv@gmail.com>
48aa9ebccSVladimir Oltean  */
58aa9ebccSVladimir Oltean #ifndef _SJA1105_H
68aa9ebccSVladimir Oltean #define _SJA1105_H
78aa9ebccSVladimir Oltean 
8bb77f36aSVladimir Oltean #include <linux/ptp_clock_kernel.h>
9bb77f36aSVladimir Oltean #include <linux/timecounter.h>
108aa9ebccSVladimir Oltean #include <linux/dsa/sja1105.h>
118aa9ebccSVladimir Oltean #include <net/dsa.h>
12227d07a0SVladimir Oltean #include <linux/mutex.h>
138aa9ebccSVladimir Oltean #include "sja1105_static_config.h"
148aa9ebccSVladimir Oltean 
158aa9ebccSVladimir Oltean #define SJA1105_NUM_PORTS		5
168aa9ebccSVladimir Oltean #define SJA1105_NUM_TC			8
178aa9ebccSVladimir Oltean #define SJA1105ET_FDB_BIN_SIZE		4
188456721dSVladimir Oltean /* The hardware value is in multiples of 10 ms.
198456721dSVladimir Oltean  * The passed parameter is in multiples of 1 ms.
208456721dSVladimir Oltean  */
218456721dSVladimir Oltean #define SJA1105_AGEING_TIME_MS(ms)	((ms) / 10)
22a6af7763SVladimir Oltean #define SJA1105_NUM_L2_POLICERS		45
238aa9ebccSVladimir Oltean 
2441603d78SVladimir Oltean typedef enum {
2541603d78SVladimir Oltean 	SPI_READ = 0,
2641603d78SVladimir Oltean 	SPI_WRITE = 1,
2741603d78SVladimir Oltean } sja1105_spi_rw_mode_t;
2841603d78SVladimir Oltean 
29317ab5b8SVladimir Oltean #include "sja1105_tas.h"
30a9d6ed7aSVladimir Oltean #include "sja1105_ptp.h"
31317ab5b8SVladimir Oltean 
328aa9ebccSVladimir Oltean /* Keeps the different addresses between E/T and P/Q/R/S */
338aa9ebccSVladimir Oltean struct sja1105_regs {
348aa9ebccSVladimir Oltean 	u64 device_id;
358aa9ebccSVladimir Oltean 	u64 prod_id;
368aa9ebccSVladimir Oltean 	u64 status;
371a4c6940SVladimir Oltean 	u64 port_control;
388aa9ebccSVladimir Oltean 	u64 rgu;
398aa9ebccSVladimir Oltean 	u64 config;
40ffe10e67SVladimir Oltean 	u64 sgmii;
418aa9ebccSVladimir Oltean 	u64 rmii_pll1;
42747e5eb3SVladimir Oltean 	u64 ptppinst;
43747e5eb3SVladimir Oltean 	u64 ptppindur;
44bb77f36aSVladimir Oltean 	u64 ptp_control;
452fb079a2SVladimir Oltean 	u64 ptpclkval;
46bb77f36aSVladimir Oltean 	u64 ptpclkrate;
4786db36a3SVladimir Oltean 	u64 ptpclkcorp;
48747e5eb3SVladimir Oltean 	u64 ptpsyncts;
4986db36a3SVladimir Oltean 	u64 ptpschtm;
5047ed985eSVladimir Oltean 	u64 ptpegr_ts[SJA1105_NUM_PORTS];
518aa9ebccSVladimir Oltean 	u64 pad_mii_tx[SJA1105_NUM_PORTS];
52135e3018SVladimir Oltean 	u64 pad_mii_rx[SJA1105_NUM_PORTS];
53b5b0c7f4SVladimir Oltean 	u64 pad_mii_id[SJA1105_NUM_PORTS];
548aa9ebccSVladimir Oltean 	u64 cgu_idiv[SJA1105_NUM_PORTS];
558aa9ebccSVladimir Oltean 	u64 mii_tx_clk[SJA1105_NUM_PORTS];
568aa9ebccSVladimir Oltean 	u64 mii_rx_clk[SJA1105_NUM_PORTS];
578aa9ebccSVladimir Oltean 	u64 mii_ext_tx_clk[SJA1105_NUM_PORTS];
588aa9ebccSVladimir Oltean 	u64 mii_ext_rx_clk[SJA1105_NUM_PORTS];
598aa9ebccSVladimir Oltean 	u64 rgmii_tx_clk[SJA1105_NUM_PORTS];
608aa9ebccSVladimir Oltean 	u64 rmii_ref_clk[SJA1105_NUM_PORTS];
618aa9ebccSVladimir Oltean 	u64 rmii_ext_tx_clk[SJA1105_NUM_PORTS];
628aa9ebccSVladimir Oltean 	u64 mac[SJA1105_NUM_PORTS];
638aa9ebccSVladimir Oltean 	u64 mac_hl1[SJA1105_NUM_PORTS];
648aa9ebccSVladimir Oltean 	u64 mac_hl2[SJA1105_NUM_PORTS];
65336aa67bSVladimir Oltean 	u64 ether_stats[SJA1105_NUM_PORTS];
668aa9ebccSVladimir Oltean 	u64 qlevel[SJA1105_NUM_PORTS];
678aa9ebccSVladimir Oltean };
688aa9ebccSVladimir Oltean 
698aa9ebccSVladimir Oltean struct sja1105_info {
708aa9ebccSVladimir Oltean 	u64 device_id;
718aa9ebccSVladimir Oltean 	/* Needed for distinction between P and R, and between Q and S
728aa9ebccSVladimir Oltean 	 * (since the parts with/without SGMII share the same
738aa9ebccSVladimir Oltean 	 * switch core and device_id)
748aa9ebccSVladimir Oltean 	 */
758aa9ebccSVladimir Oltean 	u64 part_no;
7647ed985eSVladimir Oltean 	/* E/T and P/Q/R/S have partial timestamps of different sizes.
7747ed985eSVladimir Oltean 	 * They must be reconstructed on both families anyway to get the full
7847ed985eSVladimir Oltean 	 * 64-bit values back.
7947ed985eSVladimir Oltean 	 */
8047ed985eSVladimir Oltean 	int ptp_ts_bits;
8147ed985eSVladimir Oltean 	/* Also SPI commands are of different sizes to retrieve
8247ed985eSVladimir Oltean 	 * the egress timestamps.
8347ed985eSVladimir Oltean 	 */
8447ed985eSVladimir Oltean 	int ptpegr_ts_bytes;
858aa9ebccSVladimir Oltean 	const struct sja1105_dynamic_table_ops *dyn_ops;
868aa9ebccSVladimir Oltean 	const struct sja1105_table_ops *static_ops;
878aa9ebccSVladimir Oltean 	const struct sja1105_regs *regs;
88abfb228aSVladimir Oltean 	int (*reset_cmd)(struct dsa_switch *ds);
89f5b8631cSVladimir Oltean 	int (*setup_rgmii_delay)(const void *ctx, int port);
909dfa6911SVladimir Oltean 	/* Prototypes from include/net/dsa.h */
919dfa6911SVladimir Oltean 	int (*fdb_add_cmd)(struct dsa_switch *ds, int port,
929dfa6911SVladimir Oltean 			   const unsigned char *addr, u16 vid);
939dfa6911SVladimir Oltean 	int (*fdb_del_cmd)(struct dsa_switch *ds, int port,
949dfa6911SVladimir Oltean 			   const unsigned char *addr, u16 vid);
9541603d78SVladimir Oltean 	void (*ptp_cmd_packing)(u8 *buf, struct sja1105_ptp_cmd *cmd,
9641603d78SVladimir Oltean 				enum packing_op op);
978aa9ebccSVladimir Oltean 	const char *name;
988aa9ebccSVladimir Oltean };
998aa9ebccSVladimir Oltean 
100b70bb8d4SVladimir Oltean enum sja1105_key_type {
101b70bb8d4SVladimir Oltean 	SJA1105_KEY_BCAST,
102b70bb8d4SVladimir Oltean 	SJA1105_KEY_TC,
103b70bb8d4SVladimir Oltean 	SJA1105_KEY_VLAN_UNAWARE_VL,
104b70bb8d4SVladimir Oltean 	SJA1105_KEY_VLAN_AWARE_VL,
105b70bb8d4SVladimir Oltean };
106b70bb8d4SVladimir Oltean 
107b70bb8d4SVladimir Oltean struct sja1105_key {
108b70bb8d4SVladimir Oltean 	enum sja1105_key_type type;
109b70bb8d4SVladimir Oltean 
110b70bb8d4SVladimir Oltean 	union {
111b70bb8d4SVladimir Oltean 		/* SJA1105_KEY_TC */
112b70bb8d4SVladimir Oltean 		struct {
113b70bb8d4SVladimir Oltean 			int pcp;
114b70bb8d4SVladimir Oltean 		} tc;
115b70bb8d4SVladimir Oltean 
116b70bb8d4SVladimir Oltean 		/* SJA1105_KEY_VLAN_UNAWARE_VL */
117b70bb8d4SVladimir Oltean 		/* SJA1105_KEY_VLAN_AWARE_VL */
118b70bb8d4SVladimir Oltean 		struct {
119b70bb8d4SVladimir Oltean 			u64 dmac;
120b70bb8d4SVladimir Oltean 			u16 vid;
121b70bb8d4SVladimir Oltean 			u16 pcp;
122b70bb8d4SVladimir Oltean 		} vl;
123b70bb8d4SVladimir Oltean 	};
124b70bb8d4SVladimir Oltean };
125b70bb8d4SVladimir Oltean 
126a6af7763SVladimir Oltean enum sja1105_rule_type {
127a6af7763SVladimir Oltean 	SJA1105_RULE_BCAST_POLICER,
128a6af7763SVladimir Oltean 	SJA1105_RULE_TC_POLICER,
129*dfacc5a2SVladimir Oltean 	SJA1105_RULE_VL,
130*dfacc5a2SVladimir Oltean };
131*dfacc5a2SVladimir Oltean 
132*dfacc5a2SVladimir Oltean enum sja1105_vl_type {
133*dfacc5a2SVladimir Oltean 	SJA1105_VL_NONCRITICAL,
134*dfacc5a2SVladimir Oltean 	SJA1105_VL_RATE_CONSTRAINED,
135*dfacc5a2SVladimir Oltean 	SJA1105_VL_TIME_TRIGGERED,
136a6af7763SVladimir Oltean };
137a6af7763SVladimir Oltean 
138a6af7763SVladimir Oltean struct sja1105_rule {
139a6af7763SVladimir Oltean 	struct list_head list;
140a6af7763SVladimir Oltean 	unsigned long cookie;
141a6af7763SVladimir Oltean 	unsigned long port_mask;
142b70bb8d4SVladimir Oltean 	struct sja1105_key key;
143a6af7763SVladimir Oltean 	enum sja1105_rule_type type;
144a6af7763SVladimir Oltean 
145*dfacc5a2SVladimir Oltean 	/* Action */
146a6af7763SVladimir Oltean 	union {
147a6af7763SVladimir Oltean 		/* SJA1105_RULE_BCAST_POLICER */
148a6af7763SVladimir Oltean 		struct {
149a6af7763SVladimir Oltean 			int sharindx;
150a6af7763SVladimir Oltean 		} bcast_pol;
151a6af7763SVladimir Oltean 
152a6af7763SVladimir Oltean 		/* SJA1105_RULE_TC_POLICER */
153a6af7763SVladimir Oltean 		struct {
154a6af7763SVladimir Oltean 			int sharindx;
155a6af7763SVladimir Oltean 		} tc_pol;
156*dfacc5a2SVladimir Oltean 
157*dfacc5a2SVladimir Oltean 		/* SJA1105_RULE_VL */
158*dfacc5a2SVladimir Oltean 		struct {
159*dfacc5a2SVladimir Oltean 			unsigned long destports;
160*dfacc5a2SVladimir Oltean 			enum sja1105_vl_type type;
161*dfacc5a2SVladimir Oltean 		} vl;
162a6af7763SVladimir Oltean 	};
163a6af7763SVladimir Oltean };
164a6af7763SVladimir Oltean 
165a6af7763SVladimir Oltean struct sja1105_flow_block {
166a6af7763SVladimir Oltean 	struct list_head rules;
167a6af7763SVladimir Oltean 	bool l2_policer_used[SJA1105_NUM_L2_POLICERS];
168*dfacc5a2SVladimir Oltean 	int num_virtual_links;
169a6af7763SVladimir Oltean };
170a6af7763SVladimir Oltean 
1718aa9ebccSVladimir Oltean struct sja1105_private {
1728aa9ebccSVladimir Oltean 	struct sja1105_static_config static_config;
173f5b8631cSVladimir Oltean 	bool rgmii_rx_delay[SJA1105_NUM_PORTS];
174f5b8631cSVladimir Oltean 	bool rgmii_tx_delay[SJA1105_NUM_PORTS];
1758aa9ebccSVladimir Oltean 	const struct sja1105_info *info;
1768aa9ebccSVladimir Oltean 	struct gpio_desc *reset_gpio;
1778aa9ebccSVladimir Oltean 	struct spi_device *spidev;
1788aa9ebccSVladimir Oltean 	struct dsa_switch *ds;
179a6af7763SVladimir Oltean 	struct sja1105_flow_block flow_block;
180227d07a0SVladimir Oltean 	struct sja1105_port ports[SJA1105_NUM_PORTS];
181227d07a0SVladimir Oltean 	/* Serializes transmission of management frames so that
182227d07a0SVladimir Oltean 	 * the switch doesn't confuse them with one another.
183227d07a0SVladimir Oltean 	 */
184227d07a0SVladimir Oltean 	struct mutex mgmt_lock;
185844d7edcSVladimir Oltean 	struct sja1105_tagger_data tagger_data;
186a9d6ed7aSVladimir Oltean 	struct sja1105_ptp_data ptp_data;
187317ab5b8SVladimir Oltean 	struct sja1105_tas_data tas_data;
1888aa9ebccSVladimir Oltean };
1898aa9ebccSVladimir Oltean 
1908aa9ebccSVladimir Oltean #include "sja1105_dynamic_config.h"
1918aa9ebccSVladimir Oltean 
1928aa9ebccSVladimir Oltean struct sja1105_spi_message {
1938aa9ebccSVladimir Oltean 	u64 access;
1948aa9ebccSVladimir Oltean 	u64 read_count;
1958aa9ebccSVladimir Oltean 	u64 address;
1968aa9ebccSVladimir Oltean };
1978aa9ebccSVladimir Oltean 
198317ab5b8SVladimir Oltean /* From sja1105_main.c */
1992eea1fa8SVladimir Oltean enum sja1105_reset_reason {
2002eea1fa8SVladimir Oltean 	SJA1105_VLAN_FILTERING = 0,
2012eea1fa8SVladimir Oltean 	SJA1105_RX_HWTSTAMPING,
2022eea1fa8SVladimir Oltean 	SJA1105_AGEING_TIME,
2032eea1fa8SVladimir Oltean 	SJA1105_SCHEDULING,
204c279c726SVladimir Oltean 	SJA1105_BEST_EFFORT_POLICING,
205*dfacc5a2SVladimir Oltean 	SJA1105_VIRTUAL_LINKS,
2062eea1fa8SVladimir Oltean };
2072eea1fa8SVladimir Oltean 
2082eea1fa8SVladimir Oltean int sja1105_static_config_reload(struct sja1105_private *priv,
2092eea1fa8SVladimir Oltean 				 enum sja1105_reset_reason reason);
210317ab5b8SVladimir Oltean 
2118aa9ebccSVladimir Oltean /* From sja1105_spi.c */
2121bd44870SVladimir Oltean int sja1105_xfer_buf(const struct sja1105_private *priv,
2138aa9ebccSVladimir Oltean 		     sja1105_spi_rw_mode_t rw, u64 reg_addr,
21408839c06SVladimir Oltean 		     u8 *buf, size_t len);
215dff79620SVladimir Oltean int sja1105_xfer_u32(const struct sja1105_private *priv,
21634d76e9fSVladimir Oltean 		     sja1105_spi_rw_mode_t rw, u64 reg_addr, u32 *value,
21734d76e9fSVladimir Oltean 		     struct ptp_system_timestamp *ptp_sts);
218dff79620SVladimir Oltean int sja1105_xfer_u64(const struct sja1105_private *priv,
21934d76e9fSVladimir Oltean 		     sja1105_spi_rw_mode_t rw, u64 reg_addr, u64 *value,
22034d76e9fSVladimir Oltean 		     struct ptp_system_timestamp *ptp_sts);
2218aa9ebccSVladimir Oltean int sja1105_static_config_upload(struct sja1105_private *priv);
222d114fb04SVladimir Oltean int sja1105_inhibit_tx(const struct sja1105_private *priv,
223d114fb04SVladimir Oltean 		       unsigned long port_bitmap, bool tx_inhibited);
2248aa9ebccSVladimir Oltean 
2258aa9ebccSVladimir Oltean extern struct sja1105_info sja1105e_info;
2268aa9ebccSVladimir Oltean extern struct sja1105_info sja1105t_info;
2278aa9ebccSVladimir Oltean extern struct sja1105_info sja1105p_info;
2288aa9ebccSVladimir Oltean extern struct sja1105_info sja1105q_info;
2298aa9ebccSVladimir Oltean extern struct sja1105_info sja1105r_info;
2308aa9ebccSVladimir Oltean extern struct sja1105_info sja1105s_info;
2318aa9ebccSVladimir Oltean 
2328aa9ebccSVladimir Oltean /* From sja1105_clocking.c */
2338aa9ebccSVladimir Oltean 
2348aa9ebccSVladimir Oltean typedef enum {
2358aa9ebccSVladimir Oltean 	XMII_MAC = 0,
2368aa9ebccSVladimir Oltean 	XMII_PHY = 1,
2378aa9ebccSVladimir Oltean } sja1105_mii_role_t;
2388aa9ebccSVladimir Oltean 
2398aa9ebccSVladimir Oltean typedef enum {
2408aa9ebccSVladimir Oltean 	XMII_MODE_MII		= 0,
2418aa9ebccSVladimir Oltean 	XMII_MODE_RMII		= 1,
2428aa9ebccSVladimir Oltean 	XMII_MODE_RGMII		= 2,
243ffe10e67SVladimir Oltean 	XMII_MODE_SGMII		= 3,
2448aa9ebccSVladimir Oltean } sja1105_phy_interface_t;
2458aa9ebccSVladimir Oltean 
2468aa9ebccSVladimir Oltean typedef enum {
2478aa9ebccSVladimir Oltean 	SJA1105_SPEED_10MBPS	= 3,
2488aa9ebccSVladimir Oltean 	SJA1105_SPEED_100MBPS	= 2,
2498aa9ebccSVladimir Oltean 	SJA1105_SPEED_1000MBPS	= 1,
2508aa9ebccSVladimir Oltean 	SJA1105_SPEED_AUTO	= 0,
2518aa9ebccSVladimir Oltean } sja1105_speed_t;
2528aa9ebccSVladimir Oltean 
253c05ec3d4SVladimir Oltean int sja1105pqrs_setup_rgmii_delay(const void *ctx, int port);
2548aa9ebccSVladimir Oltean int sja1105_clocking_setup_port(struct sja1105_private *priv, int port);
2558aa9ebccSVladimir Oltean int sja1105_clocking_setup(struct sja1105_private *priv);
2568aa9ebccSVladimir Oltean 
25752c34e6eSVladimir Oltean /* From sja1105_ethtool.c */
25852c34e6eSVladimir Oltean void sja1105_get_ethtool_stats(struct dsa_switch *ds, int port, u64 *data);
25952c34e6eSVladimir Oltean void sja1105_get_strings(struct dsa_switch *ds, int port,
26052c34e6eSVladimir Oltean 			 u32 stringset, u8 *data);
26152c34e6eSVladimir Oltean int sja1105_get_sset_count(struct dsa_switch *ds, int port, int sset);
2628aa9ebccSVladimir Oltean 
26352c34e6eSVladimir Oltean /* From sja1105_dynamic_config.c */
2648aa9ebccSVladimir Oltean int sja1105_dynamic_config_read(struct sja1105_private *priv,
2658aa9ebccSVladimir Oltean 				enum sja1105_blk_idx blk_idx,
2668aa9ebccSVladimir Oltean 				int index, void *entry);
2678aa9ebccSVladimir Oltean int sja1105_dynamic_config_write(struct sja1105_private *priv,
2688aa9ebccSVladimir Oltean 				 enum sja1105_blk_idx blk_idx,
2698aa9ebccSVladimir Oltean 				 int index, void *entry, bool keep);
2708aa9ebccSVladimir Oltean 
2711da73821SVladimir Oltean enum sja1105_iotag {
2721da73821SVladimir Oltean 	SJA1105_C_TAG = 0, /* Inner VLAN header */
2731da73821SVladimir Oltean 	SJA1105_S_TAG = 1, /* Outer VLAN header */
2741da73821SVladimir Oltean };
2751da73821SVladimir Oltean 
2769dfa6911SVladimir Oltean u8 sja1105et_fdb_hash(struct sja1105_private *priv, const u8 *addr, u16 vid);
2779dfa6911SVladimir Oltean int sja1105et_fdb_add(struct dsa_switch *ds, int port,
2789dfa6911SVladimir Oltean 		      const unsigned char *addr, u16 vid);
2799dfa6911SVladimir Oltean int sja1105et_fdb_del(struct dsa_switch *ds, int port,
2809dfa6911SVladimir Oltean 		      const unsigned char *addr, u16 vid);
2819dfa6911SVladimir Oltean int sja1105pqrs_fdb_add(struct dsa_switch *ds, int port,
2829dfa6911SVladimir Oltean 			const unsigned char *addr, u16 vid);
2839dfa6911SVladimir Oltean int sja1105pqrs_fdb_del(struct dsa_switch *ds, int port,
2849dfa6911SVladimir Oltean 			const unsigned char *addr, u16 vid);
285291d1e72SVladimir Oltean 
2868aa9ebccSVladimir Oltean /* Common implementations for the static and dynamic configs */
2878aa9ebccSVladimir Oltean size_t sja1105_l2_forwarding_entry_packing(void *buf, void *entry_ptr,
2888aa9ebccSVladimir Oltean 					   enum packing_op op);
2898aa9ebccSVladimir Oltean size_t sja1105pqrs_l2_lookup_entry_packing(void *buf, void *entry_ptr,
2908aa9ebccSVladimir Oltean 					   enum packing_op op);
2918aa9ebccSVladimir Oltean size_t sja1105et_l2_lookup_entry_packing(void *buf, void *entry_ptr,
2928aa9ebccSVladimir Oltean 					 enum packing_op op);
2938aa9ebccSVladimir Oltean size_t sja1105_vlan_lookup_entry_packing(void *buf, void *entry_ptr,
2948aa9ebccSVladimir Oltean 					 enum packing_op op);
2958aa9ebccSVladimir Oltean size_t sja1105pqrs_mac_config_entry_packing(void *buf, void *entry_ptr,
2968aa9ebccSVladimir Oltean 					    enum packing_op op);
2970a7e984cSVladimir Oltean size_t sja1105pqrs_avb_params_entry_packing(void *buf, void *entry_ptr,
2980a7e984cSVladimir Oltean 					    enum packing_op op);
29994f94d4aSVladimir Oltean size_t sja1105_vl_lookup_entry_packing(void *buf, void *entry_ptr,
30094f94d4aSVladimir Oltean 				       enum packing_op op);
3018aa9ebccSVladimir Oltean 
302a6af7763SVladimir Oltean /* From sja1105_flower.c */
303a6af7763SVladimir Oltean int sja1105_cls_flower_del(struct dsa_switch *ds, int port,
304a6af7763SVladimir Oltean 			   struct flow_cls_offload *cls, bool ingress);
305a6af7763SVladimir Oltean int sja1105_cls_flower_add(struct dsa_switch *ds, int port,
306a6af7763SVladimir Oltean 			   struct flow_cls_offload *cls, bool ingress);
307a6af7763SVladimir Oltean void sja1105_flower_setup(struct dsa_switch *ds);
308a6af7763SVladimir Oltean void sja1105_flower_teardown(struct dsa_switch *ds);
309*dfacc5a2SVladimir Oltean struct sja1105_rule *sja1105_rule_find(struct sja1105_private *priv,
310*dfacc5a2SVladimir Oltean 				       unsigned long cookie);
311a6af7763SVladimir Oltean 
3128aa9ebccSVladimir Oltean #endif
313