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 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) 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; 32bb77f36aSVladimir Oltean u64 ptp_control; 33bb77f36aSVladimir Oltean u64 ptpclk; 34bb77f36aSVladimir Oltean u64 ptpclkrate; 35bb77f36aSVladimir Oltean u64 ptptsclk; 3647ed985eSVladimir Oltean u64 ptpegr_ts[SJA1105_NUM_PORTS]; 378aa9ebccSVladimir Oltean u64 pad_mii_tx[SJA1105_NUM_PORTS]; 38b5b0c7f4SVladimir Oltean u64 pad_mii_id[SJA1105_NUM_PORTS]; 398aa9ebccSVladimir Oltean u64 cgu_idiv[SJA1105_NUM_PORTS]; 408aa9ebccSVladimir Oltean u64 mii_tx_clk[SJA1105_NUM_PORTS]; 418aa9ebccSVladimir Oltean u64 mii_rx_clk[SJA1105_NUM_PORTS]; 428aa9ebccSVladimir Oltean u64 mii_ext_tx_clk[SJA1105_NUM_PORTS]; 438aa9ebccSVladimir Oltean u64 mii_ext_rx_clk[SJA1105_NUM_PORTS]; 448aa9ebccSVladimir Oltean u64 rgmii_tx_clk[SJA1105_NUM_PORTS]; 458aa9ebccSVladimir Oltean u64 rmii_ref_clk[SJA1105_NUM_PORTS]; 468aa9ebccSVladimir Oltean u64 rmii_ext_tx_clk[SJA1105_NUM_PORTS]; 478aa9ebccSVladimir Oltean u64 mac[SJA1105_NUM_PORTS]; 488aa9ebccSVladimir Oltean u64 mac_hl1[SJA1105_NUM_PORTS]; 498aa9ebccSVladimir Oltean u64 mac_hl2[SJA1105_NUM_PORTS]; 508aa9ebccSVladimir Oltean u64 qlevel[SJA1105_NUM_PORTS]; 518aa9ebccSVladimir Oltean }; 528aa9ebccSVladimir Oltean 538aa9ebccSVladimir Oltean struct sja1105_info { 548aa9ebccSVladimir Oltean u64 device_id; 558aa9ebccSVladimir Oltean /* Needed for distinction between P and R, and between Q and S 568aa9ebccSVladimir Oltean * (since the parts with/without SGMII share the same 578aa9ebccSVladimir Oltean * switch core and device_id) 588aa9ebccSVladimir Oltean */ 598aa9ebccSVladimir Oltean u64 part_no; 6047ed985eSVladimir Oltean /* E/T and P/Q/R/S have partial timestamps of different sizes. 6147ed985eSVladimir Oltean * They must be reconstructed on both families anyway to get the full 6247ed985eSVladimir Oltean * 64-bit values back. 6347ed985eSVladimir Oltean */ 6447ed985eSVladimir Oltean int ptp_ts_bits; 6547ed985eSVladimir Oltean /* Also SPI commands are of different sizes to retrieve 6647ed985eSVladimir Oltean * the egress timestamps. 6747ed985eSVladimir Oltean */ 6847ed985eSVladimir Oltean int ptpegr_ts_bytes; 698aa9ebccSVladimir Oltean const struct sja1105_dynamic_table_ops *dyn_ops; 708aa9ebccSVladimir Oltean const struct sja1105_table_ops *static_ops; 718aa9ebccSVladimir Oltean const struct sja1105_regs *regs; 72bb77f36aSVladimir Oltean int (*ptp_cmd)(const void *ctx, const void *data); 738aa9ebccSVladimir Oltean int (*reset_cmd)(const void *ctx, const void *data); 74f5b8631cSVladimir Oltean int (*setup_rgmii_delay)(const void *ctx, int port); 759dfa6911SVladimir Oltean /* Prototypes from include/net/dsa.h */ 769dfa6911SVladimir Oltean int (*fdb_add_cmd)(struct dsa_switch *ds, int port, 779dfa6911SVladimir Oltean const unsigned char *addr, u16 vid); 789dfa6911SVladimir Oltean int (*fdb_del_cmd)(struct dsa_switch *ds, int port, 799dfa6911SVladimir Oltean const unsigned char *addr, u16 vid); 808aa9ebccSVladimir Oltean const char *name; 818aa9ebccSVladimir Oltean }; 828aa9ebccSVladimir Oltean 838aa9ebccSVladimir Oltean struct sja1105_private { 848aa9ebccSVladimir Oltean struct sja1105_static_config static_config; 85f5b8631cSVladimir Oltean bool rgmii_rx_delay[SJA1105_NUM_PORTS]; 86f5b8631cSVladimir Oltean bool rgmii_tx_delay[SJA1105_NUM_PORTS]; 878aa9ebccSVladimir Oltean const struct sja1105_info *info; 888aa9ebccSVladimir Oltean struct gpio_desc *reset_gpio; 898aa9ebccSVladimir Oltean struct spi_device *spidev; 908aa9ebccSVladimir Oltean struct dsa_switch *ds; 91227d07a0SVladimir Oltean struct sja1105_port ports[SJA1105_NUM_PORTS]; 92bb77f36aSVladimir Oltean struct ptp_clock_info ptp_caps; 93bb77f36aSVladimir Oltean struct ptp_clock *clock; 94bb77f36aSVladimir Oltean /* The cycle counter translates the PTP timestamps (based on 95bb77f36aSVladimir Oltean * a free-running counter) into a software time domain. 96bb77f36aSVladimir Oltean */ 97bb77f36aSVladimir Oltean struct cyclecounter tstamp_cc; 98bb77f36aSVladimir Oltean struct timecounter tstamp_tc; 99bb77f36aSVladimir Oltean struct delayed_work refresh_work; 100bb77f36aSVladimir Oltean /* Serializes all operations on the cycle counter */ 101bb77f36aSVladimir Oltean struct mutex ptp_lock; 102227d07a0SVladimir Oltean /* Serializes transmission of management frames so that 103227d07a0SVladimir Oltean * the switch doesn't confuse them with one another. 104227d07a0SVladimir Oltean */ 105227d07a0SVladimir Oltean struct mutex mgmt_lock; 106844d7edcSVladimir Oltean struct sja1105_tagger_data tagger_data; 1078aa9ebccSVladimir Oltean }; 1088aa9ebccSVladimir Oltean 1098aa9ebccSVladimir Oltean #include "sja1105_dynamic_config.h" 110bb77f36aSVladimir Oltean #include "sja1105_ptp.h" 1118aa9ebccSVladimir Oltean 1128aa9ebccSVladimir Oltean struct sja1105_spi_message { 1138aa9ebccSVladimir Oltean u64 access; 1148aa9ebccSVladimir Oltean u64 read_count; 1158aa9ebccSVladimir Oltean u64 address; 1168aa9ebccSVladimir Oltean }; 1178aa9ebccSVladimir Oltean 1188aa9ebccSVladimir Oltean typedef enum { 1198aa9ebccSVladimir Oltean SPI_READ = 0, 1208aa9ebccSVladimir Oltean SPI_WRITE = 1, 1218aa9ebccSVladimir Oltean } sja1105_spi_rw_mode_t; 1228aa9ebccSVladimir Oltean 1238aa9ebccSVladimir Oltean /* From sja1105_spi.c */ 1248aa9ebccSVladimir Oltean int sja1105_spi_send_packed_buf(const struct sja1105_private *priv, 1258aa9ebccSVladimir Oltean sja1105_spi_rw_mode_t rw, u64 reg_addr, 1268aa9ebccSVladimir Oltean void *packed_buf, size_t size_bytes); 1278aa9ebccSVladimir Oltean int sja1105_spi_send_int(const struct sja1105_private *priv, 1288aa9ebccSVladimir Oltean sja1105_spi_rw_mode_t rw, u64 reg_addr, 1298aa9ebccSVladimir Oltean u64 *value, u64 size_bytes); 1308aa9ebccSVladimir Oltean int sja1105_spi_send_long_packed_buf(const struct sja1105_private *priv, 1318aa9ebccSVladimir Oltean sja1105_spi_rw_mode_t rw, u64 base_addr, 1328aa9ebccSVladimir Oltean void *packed_buf, u64 buf_len); 1338aa9ebccSVladimir Oltean int sja1105_static_config_upload(struct sja1105_private *priv); 134d114fb04SVladimir Oltean int sja1105_inhibit_tx(const struct sja1105_private *priv, 135d114fb04SVladimir Oltean unsigned long port_bitmap, bool tx_inhibited); 1368aa9ebccSVladimir Oltean 1378aa9ebccSVladimir Oltean extern struct sja1105_info sja1105e_info; 1388aa9ebccSVladimir Oltean extern struct sja1105_info sja1105t_info; 1398aa9ebccSVladimir Oltean extern struct sja1105_info sja1105p_info; 1408aa9ebccSVladimir Oltean extern struct sja1105_info sja1105q_info; 1418aa9ebccSVladimir Oltean extern struct sja1105_info sja1105r_info; 1428aa9ebccSVladimir Oltean extern struct sja1105_info sja1105s_info; 1438aa9ebccSVladimir Oltean 1448aa9ebccSVladimir Oltean /* From sja1105_clocking.c */ 1458aa9ebccSVladimir Oltean 1468aa9ebccSVladimir Oltean typedef enum { 1478aa9ebccSVladimir Oltean XMII_MAC = 0, 1488aa9ebccSVladimir Oltean XMII_PHY = 1, 1498aa9ebccSVladimir Oltean } sja1105_mii_role_t; 1508aa9ebccSVladimir Oltean 1518aa9ebccSVladimir Oltean typedef enum { 1528aa9ebccSVladimir Oltean XMII_MODE_MII = 0, 1538aa9ebccSVladimir Oltean XMII_MODE_RMII = 1, 1548aa9ebccSVladimir Oltean XMII_MODE_RGMII = 2, 1558aa9ebccSVladimir Oltean } sja1105_phy_interface_t; 1568aa9ebccSVladimir Oltean 1578aa9ebccSVladimir Oltean typedef enum { 1588aa9ebccSVladimir Oltean SJA1105_SPEED_10MBPS = 3, 1598aa9ebccSVladimir Oltean SJA1105_SPEED_100MBPS = 2, 1608aa9ebccSVladimir Oltean SJA1105_SPEED_1000MBPS = 1, 1618aa9ebccSVladimir Oltean SJA1105_SPEED_AUTO = 0, 1628aa9ebccSVladimir Oltean } sja1105_speed_t; 1638aa9ebccSVladimir Oltean 164*c05ec3d4SVladimir Oltean int sja1105pqrs_setup_rgmii_delay(const void *ctx, int port); 1658aa9ebccSVladimir Oltean int sja1105_clocking_setup_port(struct sja1105_private *priv, int port); 1668aa9ebccSVladimir Oltean int sja1105_clocking_setup(struct sja1105_private *priv); 1678aa9ebccSVladimir Oltean 16852c34e6eSVladimir Oltean /* From sja1105_ethtool.c */ 16952c34e6eSVladimir Oltean void sja1105_get_ethtool_stats(struct dsa_switch *ds, int port, u64 *data); 17052c34e6eSVladimir Oltean void sja1105_get_strings(struct dsa_switch *ds, int port, 17152c34e6eSVladimir Oltean u32 stringset, u8 *data); 17252c34e6eSVladimir Oltean int sja1105_get_sset_count(struct dsa_switch *ds, int port, int sset); 1738aa9ebccSVladimir Oltean 17452c34e6eSVladimir Oltean /* From sja1105_dynamic_config.c */ 1758aa9ebccSVladimir Oltean int sja1105_dynamic_config_read(struct sja1105_private *priv, 1768aa9ebccSVladimir Oltean enum sja1105_blk_idx blk_idx, 1778aa9ebccSVladimir Oltean int index, void *entry); 1788aa9ebccSVladimir Oltean int sja1105_dynamic_config_write(struct sja1105_private *priv, 1798aa9ebccSVladimir Oltean enum sja1105_blk_idx blk_idx, 1808aa9ebccSVladimir Oltean int index, void *entry, bool keep); 1818aa9ebccSVladimir Oltean 1821da73821SVladimir Oltean enum sja1105_iotag { 1831da73821SVladimir Oltean SJA1105_C_TAG = 0, /* Inner VLAN header */ 1841da73821SVladimir Oltean SJA1105_S_TAG = 1, /* Outer VLAN header */ 1851da73821SVladimir Oltean }; 1861da73821SVladimir Oltean 1879dfa6911SVladimir Oltean u8 sja1105et_fdb_hash(struct sja1105_private *priv, const u8 *addr, u16 vid); 1889dfa6911SVladimir Oltean int sja1105et_fdb_add(struct dsa_switch *ds, int port, 1899dfa6911SVladimir Oltean const unsigned char *addr, u16 vid); 1909dfa6911SVladimir Oltean int sja1105et_fdb_del(struct dsa_switch *ds, int port, 1919dfa6911SVladimir Oltean const unsigned char *addr, u16 vid); 1929dfa6911SVladimir Oltean int sja1105pqrs_fdb_add(struct dsa_switch *ds, int port, 1939dfa6911SVladimir Oltean const unsigned char *addr, u16 vid); 1949dfa6911SVladimir Oltean int sja1105pqrs_fdb_del(struct dsa_switch *ds, int port, 1959dfa6911SVladimir Oltean const unsigned char *addr, u16 vid); 196291d1e72SVladimir Oltean 1978aa9ebccSVladimir Oltean /* Common implementations for the static and dynamic configs */ 1988aa9ebccSVladimir Oltean size_t sja1105_l2_forwarding_entry_packing(void *buf, void *entry_ptr, 1998aa9ebccSVladimir Oltean enum packing_op op); 2008aa9ebccSVladimir Oltean size_t sja1105pqrs_l2_lookup_entry_packing(void *buf, void *entry_ptr, 2018aa9ebccSVladimir Oltean enum packing_op op); 2028aa9ebccSVladimir Oltean size_t sja1105et_l2_lookup_entry_packing(void *buf, void *entry_ptr, 2038aa9ebccSVladimir Oltean enum packing_op op); 2048aa9ebccSVladimir Oltean size_t sja1105_vlan_lookup_entry_packing(void *buf, void *entry_ptr, 2058aa9ebccSVladimir Oltean enum packing_op op); 2068aa9ebccSVladimir Oltean size_t sja1105pqrs_mac_config_entry_packing(void *buf, void *entry_ptr, 2078aa9ebccSVladimir Oltean enum packing_op op); 2088aa9ebccSVladimir Oltean 2098aa9ebccSVladimir Oltean #endif 210