18aa9ebccSVladimir Oltean /* SPDX-License-Identifier: GPL-2.0 28aa9ebccSVladimir Oltean * 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 8*bb77f36aSVladimir Oltean #include <linux/ptp_clock_kernel.h> 9*bb77f36aSVladimir 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) 228aa9ebccSVladimir Oltean 238aa9ebccSVladimir Oltean /* Keeps the different addresses between E/T and P/Q/R/S */ 248aa9ebccSVladimir Oltean struct sja1105_regs { 258aa9ebccSVladimir Oltean u64 device_id; 268aa9ebccSVladimir Oltean u64 prod_id; 278aa9ebccSVladimir Oltean u64 status; 281a4c6940SVladimir Oltean u64 port_control; 298aa9ebccSVladimir Oltean u64 rgu; 308aa9ebccSVladimir Oltean u64 config; 318aa9ebccSVladimir Oltean u64 rmii_pll1; 32*bb77f36aSVladimir Oltean u64 ptp_control; 33*bb77f36aSVladimir Oltean u64 ptpclk; 34*bb77f36aSVladimir Oltean u64 ptpclkrate; 35*bb77f36aSVladimir Oltean u64 ptptsclk; 368aa9ebccSVladimir Oltean u64 pad_mii_tx[SJA1105_NUM_PORTS]; 378aa9ebccSVladimir Oltean u64 cgu_idiv[SJA1105_NUM_PORTS]; 388aa9ebccSVladimir Oltean u64 rgmii_pad_mii_tx[SJA1105_NUM_PORTS]; 398aa9ebccSVladimir Oltean u64 mii_tx_clk[SJA1105_NUM_PORTS]; 408aa9ebccSVladimir Oltean u64 mii_rx_clk[SJA1105_NUM_PORTS]; 418aa9ebccSVladimir Oltean u64 mii_ext_tx_clk[SJA1105_NUM_PORTS]; 428aa9ebccSVladimir Oltean u64 mii_ext_rx_clk[SJA1105_NUM_PORTS]; 438aa9ebccSVladimir Oltean u64 rgmii_tx_clk[SJA1105_NUM_PORTS]; 448aa9ebccSVladimir Oltean u64 rmii_ref_clk[SJA1105_NUM_PORTS]; 458aa9ebccSVladimir Oltean u64 rmii_ext_tx_clk[SJA1105_NUM_PORTS]; 468aa9ebccSVladimir Oltean u64 mac[SJA1105_NUM_PORTS]; 478aa9ebccSVladimir Oltean u64 mac_hl1[SJA1105_NUM_PORTS]; 488aa9ebccSVladimir Oltean u64 mac_hl2[SJA1105_NUM_PORTS]; 498aa9ebccSVladimir Oltean u64 qlevel[SJA1105_NUM_PORTS]; 508aa9ebccSVladimir Oltean }; 518aa9ebccSVladimir Oltean 528aa9ebccSVladimir Oltean struct sja1105_info { 538aa9ebccSVladimir Oltean u64 device_id; 548aa9ebccSVladimir Oltean /* Needed for distinction between P and R, and between Q and S 558aa9ebccSVladimir Oltean * (since the parts with/without SGMII share the same 568aa9ebccSVladimir Oltean * switch core and device_id) 578aa9ebccSVladimir Oltean */ 588aa9ebccSVladimir Oltean u64 part_no; 598aa9ebccSVladimir Oltean const struct sja1105_dynamic_table_ops *dyn_ops; 608aa9ebccSVladimir Oltean const struct sja1105_table_ops *static_ops; 618aa9ebccSVladimir Oltean const struct sja1105_regs *regs; 62*bb77f36aSVladimir Oltean int (*ptp_cmd)(const void *ctx, const void *data); 638aa9ebccSVladimir Oltean int (*reset_cmd)(const void *ctx, const void *data); 64f5b8631cSVladimir Oltean int (*setup_rgmii_delay)(const void *ctx, int port); 659dfa6911SVladimir Oltean /* Prototypes from include/net/dsa.h */ 669dfa6911SVladimir Oltean int (*fdb_add_cmd)(struct dsa_switch *ds, int port, 679dfa6911SVladimir Oltean const unsigned char *addr, u16 vid); 689dfa6911SVladimir Oltean int (*fdb_del_cmd)(struct dsa_switch *ds, int port, 699dfa6911SVladimir Oltean const unsigned char *addr, u16 vid); 708aa9ebccSVladimir Oltean const char *name; 718aa9ebccSVladimir Oltean }; 728aa9ebccSVladimir Oltean 738aa9ebccSVladimir Oltean struct sja1105_private { 748aa9ebccSVladimir Oltean struct sja1105_static_config static_config; 75f5b8631cSVladimir Oltean bool rgmii_rx_delay[SJA1105_NUM_PORTS]; 76f5b8631cSVladimir Oltean bool rgmii_tx_delay[SJA1105_NUM_PORTS]; 778aa9ebccSVladimir Oltean const struct sja1105_info *info; 788aa9ebccSVladimir Oltean struct gpio_desc *reset_gpio; 798aa9ebccSVladimir Oltean struct spi_device *spidev; 808aa9ebccSVladimir Oltean struct dsa_switch *ds; 81227d07a0SVladimir Oltean struct sja1105_port ports[SJA1105_NUM_PORTS]; 82*bb77f36aSVladimir Oltean struct ptp_clock_info ptp_caps; 83*bb77f36aSVladimir Oltean struct ptp_clock *clock; 84*bb77f36aSVladimir Oltean /* The cycle counter translates the PTP timestamps (based on 85*bb77f36aSVladimir Oltean * a free-running counter) into a software time domain. 86*bb77f36aSVladimir Oltean */ 87*bb77f36aSVladimir Oltean struct cyclecounter tstamp_cc; 88*bb77f36aSVladimir Oltean struct timecounter tstamp_tc; 89*bb77f36aSVladimir Oltean struct delayed_work refresh_work; 90*bb77f36aSVladimir Oltean /* Serializes all operations on the cycle counter */ 91*bb77f36aSVladimir Oltean struct mutex ptp_lock; 92227d07a0SVladimir Oltean /* Serializes transmission of management frames so that 93227d07a0SVladimir Oltean * the switch doesn't confuse them with one another. 94227d07a0SVladimir Oltean */ 95227d07a0SVladimir Oltean struct mutex mgmt_lock; 968aa9ebccSVladimir Oltean }; 978aa9ebccSVladimir Oltean 988aa9ebccSVladimir Oltean #include "sja1105_dynamic_config.h" 99*bb77f36aSVladimir Oltean #include "sja1105_ptp.h" 1008aa9ebccSVladimir Oltean 1018aa9ebccSVladimir Oltean struct sja1105_spi_message { 1028aa9ebccSVladimir Oltean u64 access; 1038aa9ebccSVladimir Oltean u64 read_count; 1048aa9ebccSVladimir Oltean u64 address; 1058aa9ebccSVladimir Oltean }; 1068aa9ebccSVladimir Oltean 1078aa9ebccSVladimir Oltean typedef enum { 1088aa9ebccSVladimir Oltean SPI_READ = 0, 1098aa9ebccSVladimir Oltean SPI_WRITE = 1, 1108aa9ebccSVladimir Oltean } sja1105_spi_rw_mode_t; 1118aa9ebccSVladimir Oltean 1128aa9ebccSVladimir Oltean /* From sja1105_spi.c */ 1138aa9ebccSVladimir Oltean int sja1105_spi_send_packed_buf(const struct sja1105_private *priv, 1148aa9ebccSVladimir Oltean sja1105_spi_rw_mode_t rw, u64 reg_addr, 1158aa9ebccSVladimir Oltean void *packed_buf, size_t size_bytes); 1168aa9ebccSVladimir Oltean int sja1105_spi_send_int(const struct sja1105_private *priv, 1178aa9ebccSVladimir Oltean sja1105_spi_rw_mode_t rw, u64 reg_addr, 1188aa9ebccSVladimir Oltean u64 *value, u64 size_bytes); 1198aa9ebccSVladimir Oltean int sja1105_spi_send_long_packed_buf(const struct sja1105_private *priv, 1208aa9ebccSVladimir Oltean sja1105_spi_rw_mode_t rw, u64 base_addr, 1218aa9ebccSVladimir Oltean void *packed_buf, u64 buf_len); 1228aa9ebccSVladimir Oltean int sja1105_static_config_upload(struct sja1105_private *priv); 1238aa9ebccSVladimir Oltean 1248aa9ebccSVladimir Oltean extern struct sja1105_info sja1105e_info; 1258aa9ebccSVladimir Oltean extern struct sja1105_info sja1105t_info; 1268aa9ebccSVladimir Oltean extern struct sja1105_info sja1105p_info; 1278aa9ebccSVladimir Oltean extern struct sja1105_info sja1105q_info; 1288aa9ebccSVladimir Oltean extern struct sja1105_info sja1105r_info; 1298aa9ebccSVladimir Oltean extern struct sja1105_info sja1105s_info; 1308aa9ebccSVladimir Oltean 1318aa9ebccSVladimir Oltean /* From sja1105_clocking.c */ 1328aa9ebccSVladimir Oltean 1338aa9ebccSVladimir Oltean typedef enum { 1348aa9ebccSVladimir Oltean XMII_MAC = 0, 1358aa9ebccSVladimir Oltean XMII_PHY = 1, 1368aa9ebccSVladimir Oltean } sja1105_mii_role_t; 1378aa9ebccSVladimir Oltean 1388aa9ebccSVladimir Oltean typedef enum { 1398aa9ebccSVladimir Oltean XMII_MODE_MII = 0, 1408aa9ebccSVladimir Oltean XMII_MODE_RMII = 1, 1418aa9ebccSVladimir Oltean XMII_MODE_RGMII = 2, 1428aa9ebccSVladimir Oltean } sja1105_phy_interface_t; 1438aa9ebccSVladimir Oltean 1448aa9ebccSVladimir Oltean typedef enum { 1458aa9ebccSVladimir Oltean SJA1105_SPEED_10MBPS = 3, 1468aa9ebccSVladimir Oltean SJA1105_SPEED_100MBPS = 2, 1478aa9ebccSVladimir Oltean SJA1105_SPEED_1000MBPS = 1, 1488aa9ebccSVladimir Oltean SJA1105_SPEED_AUTO = 0, 1498aa9ebccSVladimir Oltean } sja1105_speed_t; 1508aa9ebccSVladimir Oltean 1518aa9ebccSVladimir Oltean int sja1105_clocking_setup_port(struct sja1105_private *priv, int port); 1528aa9ebccSVladimir Oltean int sja1105_clocking_setup(struct sja1105_private *priv); 1538aa9ebccSVladimir Oltean 15452c34e6eSVladimir Oltean /* From sja1105_ethtool.c */ 15552c34e6eSVladimir Oltean void sja1105_get_ethtool_stats(struct dsa_switch *ds, int port, u64 *data); 15652c34e6eSVladimir Oltean void sja1105_get_strings(struct dsa_switch *ds, int port, 15752c34e6eSVladimir Oltean u32 stringset, u8 *data); 15852c34e6eSVladimir Oltean int sja1105_get_sset_count(struct dsa_switch *ds, int port, int sset); 1598aa9ebccSVladimir Oltean 16052c34e6eSVladimir Oltean /* From sja1105_dynamic_config.c */ 1618aa9ebccSVladimir Oltean int sja1105_dynamic_config_read(struct sja1105_private *priv, 1628aa9ebccSVladimir Oltean enum sja1105_blk_idx blk_idx, 1638aa9ebccSVladimir Oltean int index, void *entry); 1648aa9ebccSVladimir Oltean int sja1105_dynamic_config_write(struct sja1105_private *priv, 1658aa9ebccSVladimir Oltean enum sja1105_blk_idx blk_idx, 1668aa9ebccSVladimir Oltean int index, void *entry, bool keep); 1678aa9ebccSVladimir Oltean 1681da73821SVladimir Oltean enum sja1105_iotag { 1691da73821SVladimir Oltean SJA1105_C_TAG = 0, /* Inner VLAN header */ 1701da73821SVladimir Oltean SJA1105_S_TAG = 1, /* Outer VLAN header */ 1711da73821SVladimir Oltean }; 1721da73821SVladimir Oltean 1739dfa6911SVladimir Oltean u8 sja1105et_fdb_hash(struct sja1105_private *priv, const u8 *addr, u16 vid); 1749dfa6911SVladimir Oltean int sja1105et_fdb_add(struct dsa_switch *ds, int port, 1759dfa6911SVladimir Oltean const unsigned char *addr, u16 vid); 1769dfa6911SVladimir Oltean int sja1105et_fdb_del(struct dsa_switch *ds, int port, 1779dfa6911SVladimir Oltean const unsigned char *addr, u16 vid); 1789dfa6911SVladimir Oltean int sja1105pqrs_fdb_add(struct dsa_switch *ds, int port, 1799dfa6911SVladimir Oltean const unsigned char *addr, u16 vid); 1809dfa6911SVladimir Oltean int sja1105pqrs_fdb_del(struct dsa_switch *ds, int port, 1819dfa6911SVladimir Oltean const unsigned char *addr, u16 vid); 182291d1e72SVladimir Oltean 1838aa9ebccSVladimir Oltean /* Common implementations for the static and dynamic configs */ 1848aa9ebccSVladimir Oltean size_t sja1105_l2_forwarding_entry_packing(void *buf, void *entry_ptr, 1858aa9ebccSVladimir Oltean enum packing_op op); 1868aa9ebccSVladimir Oltean size_t sja1105pqrs_l2_lookup_entry_packing(void *buf, void *entry_ptr, 1878aa9ebccSVladimir Oltean enum packing_op op); 1888aa9ebccSVladimir Oltean size_t sja1105et_l2_lookup_entry_packing(void *buf, void *entry_ptr, 1898aa9ebccSVladimir Oltean enum packing_op op); 1908aa9ebccSVladimir Oltean size_t sja1105_vlan_lookup_entry_packing(void *buf, void *entry_ptr, 1918aa9ebccSVladimir Oltean enum packing_op op); 1928aa9ebccSVladimir Oltean size_t sja1105pqrs_mac_config_entry_packing(void *buf, void *entry_ptr, 1938aa9ebccSVladimir Oltean enum packing_op op); 1948aa9ebccSVladimir Oltean 1958aa9ebccSVladimir Oltean #endif 196