sja1105.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /* Copyright (c) 2018, Sensor-Technik Wiedemann GmbH
  3. * Copyright (c) 2018-2019, Vladimir Oltean <[email protected]>
  4. */
  5. #ifndef _SJA1105_H
  6. #define _SJA1105_H
  7. #include <linux/ptp_clock_kernel.h>
  8. #include <linux/timecounter.h>
  9. #include <linux/dsa/sja1105.h>
  10. #include <linux/dsa/8021q.h>
  11. #include <net/dsa.h>
  12. #include <linux/mutex.h>
  13. #include "sja1105_static_config.h"
  14. #define SJA1105ET_FDB_BIN_SIZE 4
  15. /* The hardware value is in multiples of 10 ms.
  16. * The passed parameter is in multiples of 1 ms.
  17. */
  18. #define SJA1105_AGEING_TIME_MS(ms) ((ms) / 10)
  19. #define SJA1105_NUM_L2_POLICERS SJA1110_MAX_L2_POLICING_COUNT
  20. /* Calculated assuming 1Gbps, where the clock has 125 MHz (8 ns period)
  21. * To avoid floating point operations, we'll multiply the degrees by 10
  22. * to get a "phase" and get 1 decimal point precision.
  23. */
  24. #define SJA1105_RGMII_DELAY_PS_TO_PHASE(ps) \
  25. (((ps) * 360) / 800)
  26. #define SJA1105_RGMII_DELAY_PHASE_TO_PS(phase) \
  27. ((800 * (phase)) / 360)
  28. #define SJA1105_RGMII_DELAY_PHASE_TO_HW(phase) \
  29. (((phase) - 738) / 9)
  30. #define SJA1105_RGMII_DELAY_PS_TO_HW(ps) \
  31. SJA1105_RGMII_DELAY_PHASE_TO_HW(SJA1105_RGMII_DELAY_PS_TO_PHASE(ps))
  32. /* Valid range in degrees is a value between 73.8 and 101.7
  33. * in 0.9 degree increments
  34. */
  35. #define SJA1105_RGMII_DELAY_MIN_PS \
  36. SJA1105_RGMII_DELAY_PHASE_TO_PS(738)
  37. #define SJA1105_RGMII_DELAY_MAX_PS \
  38. SJA1105_RGMII_DELAY_PHASE_TO_PS(1017)
  39. typedef enum {
  40. SPI_READ = 0,
  41. SPI_WRITE = 1,
  42. } sja1105_spi_rw_mode_t;
  43. #include "sja1105_tas.h"
  44. #include "sja1105_ptp.h"
  45. enum sja1105_stats_area {
  46. MAC,
  47. HL1,
  48. HL2,
  49. ETHER,
  50. __MAX_SJA1105_STATS_AREA,
  51. };
  52. /* Keeps the different addresses between E/T and P/Q/R/S */
  53. struct sja1105_regs {
  54. u64 device_id;
  55. u64 prod_id;
  56. u64 status;
  57. u64 port_control;
  58. u64 rgu;
  59. u64 vl_status;
  60. u64 config;
  61. u64 rmii_pll1;
  62. u64 ptppinst;
  63. u64 ptppindur;
  64. u64 ptp_control;
  65. u64 ptpclkval;
  66. u64 ptpclkrate;
  67. u64 ptpclkcorp;
  68. u64 ptpsyncts;
  69. u64 ptpschtm;
  70. u64 ptpegr_ts[SJA1105_MAX_NUM_PORTS];
  71. u64 pad_mii_tx[SJA1105_MAX_NUM_PORTS];
  72. u64 pad_mii_rx[SJA1105_MAX_NUM_PORTS];
  73. u64 pad_mii_id[SJA1105_MAX_NUM_PORTS];
  74. u64 cgu_idiv[SJA1105_MAX_NUM_PORTS];
  75. u64 mii_tx_clk[SJA1105_MAX_NUM_PORTS];
  76. u64 mii_rx_clk[SJA1105_MAX_NUM_PORTS];
  77. u64 mii_ext_tx_clk[SJA1105_MAX_NUM_PORTS];
  78. u64 mii_ext_rx_clk[SJA1105_MAX_NUM_PORTS];
  79. u64 rgmii_tx_clk[SJA1105_MAX_NUM_PORTS];
  80. u64 rmii_ref_clk[SJA1105_MAX_NUM_PORTS];
  81. u64 rmii_ext_tx_clk[SJA1105_MAX_NUM_PORTS];
  82. u64 stats[__MAX_SJA1105_STATS_AREA][SJA1105_MAX_NUM_PORTS];
  83. u64 mdio_100base_tx;
  84. u64 mdio_100base_t1;
  85. u64 pcs_base[SJA1105_MAX_NUM_PORTS];
  86. };
  87. struct sja1105_mdio_private {
  88. struct sja1105_private *priv;
  89. };
  90. enum {
  91. SJA1105_SPEED_AUTO,
  92. SJA1105_SPEED_10MBPS,
  93. SJA1105_SPEED_100MBPS,
  94. SJA1105_SPEED_1000MBPS,
  95. SJA1105_SPEED_2500MBPS,
  96. SJA1105_SPEED_MAX,
  97. };
  98. enum sja1105_internal_phy_t {
  99. SJA1105_NO_PHY = 0,
  100. SJA1105_PHY_BASE_TX,
  101. SJA1105_PHY_BASE_T1,
  102. };
  103. struct sja1105_info {
  104. u64 device_id;
  105. /* Needed for distinction between P and R, and between Q and S
  106. * (since the parts with/without SGMII share the same
  107. * switch core and device_id)
  108. */
  109. u64 part_no;
  110. /* E/T and P/Q/R/S have partial timestamps of different sizes.
  111. * They must be reconstructed on both families anyway to get the full
  112. * 64-bit values back.
  113. */
  114. int ptp_ts_bits;
  115. /* Also SPI commands are of different sizes to retrieve
  116. * the egress timestamps.
  117. */
  118. int ptpegr_ts_bytes;
  119. int num_cbs_shapers;
  120. int max_frame_mem;
  121. int num_ports;
  122. bool multiple_cascade_ports;
  123. /* Every {port, TXQ} has its own CBS shaper */
  124. bool fixed_cbs_mapping;
  125. enum dsa_tag_protocol tag_proto;
  126. const struct sja1105_dynamic_table_ops *dyn_ops;
  127. const struct sja1105_table_ops *static_ops;
  128. const struct sja1105_regs *regs;
  129. bool can_limit_mcast_flood;
  130. int (*reset_cmd)(struct dsa_switch *ds);
  131. int (*setup_rgmii_delay)(const void *ctx, int port);
  132. /* Prototypes from include/net/dsa.h */
  133. int (*fdb_add_cmd)(struct dsa_switch *ds, int port,
  134. const unsigned char *addr, u16 vid);
  135. int (*fdb_del_cmd)(struct dsa_switch *ds, int port,
  136. const unsigned char *addr, u16 vid);
  137. void (*ptp_cmd_packing)(u8 *buf, struct sja1105_ptp_cmd *cmd,
  138. enum packing_op op);
  139. bool (*rxtstamp)(struct dsa_switch *ds, int port, struct sk_buff *skb);
  140. void (*txtstamp)(struct dsa_switch *ds, int port, struct sk_buff *skb);
  141. int (*clocking_setup)(struct sja1105_private *priv);
  142. int (*pcs_mdio_read)(struct mii_bus *bus, int phy, int reg);
  143. int (*pcs_mdio_write)(struct mii_bus *bus, int phy, int reg, u16 val);
  144. int (*disable_microcontroller)(struct sja1105_private *priv);
  145. const char *name;
  146. bool supports_mii[SJA1105_MAX_NUM_PORTS];
  147. bool supports_rmii[SJA1105_MAX_NUM_PORTS];
  148. bool supports_rgmii[SJA1105_MAX_NUM_PORTS];
  149. bool supports_sgmii[SJA1105_MAX_NUM_PORTS];
  150. bool supports_2500basex[SJA1105_MAX_NUM_PORTS];
  151. enum sja1105_internal_phy_t internal_phy[SJA1105_MAX_NUM_PORTS];
  152. const u64 port_speed[SJA1105_SPEED_MAX];
  153. };
  154. enum sja1105_key_type {
  155. SJA1105_KEY_BCAST,
  156. SJA1105_KEY_TC,
  157. SJA1105_KEY_VLAN_UNAWARE_VL,
  158. SJA1105_KEY_VLAN_AWARE_VL,
  159. };
  160. struct sja1105_key {
  161. enum sja1105_key_type type;
  162. union {
  163. /* SJA1105_KEY_TC */
  164. struct {
  165. int pcp;
  166. } tc;
  167. /* SJA1105_KEY_VLAN_UNAWARE_VL */
  168. /* SJA1105_KEY_VLAN_AWARE_VL */
  169. struct {
  170. u64 dmac;
  171. u16 vid;
  172. u16 pcp;
  173. } vl;
  174. };
  175. };
  176. enum sja1105_rule_type {
  177. SJA1105_RULE_BCAST_POLICER,
  178. SJA1105_RULE_TC_POLICER,
  179. SJA1105_RULE_VL,
  180. };
  181. enum sja1105_vl_type {
  182. SJA1105_VL_NONCRITICAL,
  183. SJA1105_VL_RATE_CONSTRAINED,
  184. SJA1105_VL_TIME_TRIGGERED,
  185. };
  186. struct sja1105_rule {
  187. struct list_head list;
  188. unsigned long cookie;
  189. unsigned long port_mask;
  190. struct sja1105_key key;
  191. enum sja1105_rule_type type;
  192. /* Action */
  193. union {
  194. /* SJA1105_RULE_BCAST_POLICER */
  195. struct {
  196. int sharindx;
  197. } bcast_pol;
  198. /* SJA1105_RULE_TC_POLICER */
  199. struct {
  200. int sharindx;
  201. } tc_pol;
  202. /* SJA1105_RULE_VL */
  203. struct {
  204. enum sja1105_vl_type type;
  205. unsigned long destports;
  206. int sharindx;
  207. int maxlen;
  208. int ipv;
  209. u64 base_time;
  210. u64 cycle_time;
  211. int num_entries;
  212. struct action_gate_entry *entries;
  213. struct flow_stats stats;
  214. } vl;
  215. };
  216. };
  217. struct sja1105_flow_block {
  218. struct list_head rules;
  219. bool l2_policer_used[SJA1105_NUM_L2_POLICERS];
  220. int num_virtual_links;
  221. };
  222. struct sja1105_private {
  223. struct sja1105_static_config static_config;
  224. int rgmii_rx_delay_ps[SJA1105_MAX_NUM_PORTS];
  225. int rgmii_tx_delay_ps[SJA1105_MAX_NUM_PORTS];
  226. phy_interface_t phy_mode[SJA1105_MAX_NUM_PORTS];
  227. bool fixed_link[SJA1105_MAX_NUM_PORTS];
  228. unsigned long ucast_egress_floods;
  229. unsigned long bcast_egress_floods;
  230. unsigned long hwts_tx_en;
  231. unsigned long hwts_rx_en;
  232. const struct sja1105_info *info;
  233. size_t max_xfer_len;
  234. struct spi_device *spidev;
  235. struct dsa_switch *ds;
  236. u16 bridge_pvid[SJA1105_MAX_NUM_PORTS];
  237. u16 tag_8021q_pvid[SJA1105_MAX_NUM_PORTS];
  238. struct sja1105_flow_block flow_block;
  239. /* Serializes transmission of management frames so that
  240. * the switch doesn't confuse them with one another.
  241. */
  242. struct mutex mgmt_lock;
  243. /* Serializes accesses to the FDB */
  244. struct mutex fdb_lock;
  245. /* PTP two-step TX timestamp ID, and its serialization lock */
  246. spinlock_t ts_id_lock;
  247. u8 ts_id;
  248. /* Serializes access to the dynamic config interface */
  249. struct mutex dynamic_config_lock;
  250. struct devlink_region **regions;
  251. struct sja1105_cbs_entry *cbs;
  252. struct mii_bus *mdio_base_t1;
  253. struct mii_bus *mdio_base_tx;
  254. struct mii_bus *mdio_pcs;
  255. struct dw_xpcs *xpcs[SJA1105_MAX_NUM_PORTS];
  256. struct sja1105_ptp_data ptp_data;
  257. struct sja1105_tas_data tas_data;
  258. };
  259. #include "sja1105_dynamic_config.h"
  260. struct sja1105_spi_message {
  261. u64 access;
  262. u64 read_count;
  263. u64 address;
  264. };
  265. /* From sja1105_main.c */
  266. enum sja1105_reset_reason {
  267. SJA1105_VLAN_FILTERING = 0,
  268. SJA1105_AGEING_TIME,
  269. SJA1105_SCHEDULING,
  270. SJA1105_BEST_EFFORT_POLICING,
  271. SJA1105_VIRTUAL_LINKS,
  272. };
  273. int sja1105_static_config_reload(struct sja1105_private *priv,
  274. enum sja1105_reset_reason reason);
  275. int sja1105_vlan_filtering(struct dsa_switch *ds, int port, bool enabled,
  276. struct netlink_ext_ack *extack);
  277. void sja1105_frame_memory_partitioning(struct sja1105_private *priv);
  278. /* From sja1105_mdio.c */
  279. int sja1105_mdiobus_register(struct dsa_switch *ds);
  280. void sja1105_mdiobus_unregister(struct dsa_switch *ds);
  281. int sja1105_pcs_mdio_read(struct mii_bus *bus, int phy, int reg);
  282. int sja1105_pcs_mdio_write(struct mii_bus *bus, int phy, int reg, u16 val);
  283. int sja1110_pcs_mdio_read(struct mii_bus *bus, int phy, int reg);
  284. int sja1110_pcs_mdio_write(struct mii_bus *bus, int phy, int reg, u16 val);
  285. /* From sja1105_devlink.c */
  286. int sja1105_devlink_setup(struct dsa_switch *ds);
  287. void sja1105_devlink_teardown(struct dsa_switch *ds);
  288. int sja1105_devlink_info_get(struct dsa_switch *ds,
  289. struct devlink_info_req *req,
  290. struct netlink_ext_ack *extack);
  291. /* From sja1105_spi.c */
  292. int sja1105_xfer_buf(const struct sja1105_private *priv,
  293. sja1105_spi_rw_mode_t rw, u64 reg_addr,
  294. u8 *buf, size_t len);
  295. int sja1105_xfer_u32(const struct sja1105_private *priv,
  296. sja1105_spi_rw_mode_t rw, u64 reg_addr, u32 *value,
  297. struct ptp_system_timestamp *ptp_sts);
  298. int sja1105_xfer_u64(const struct sja1105_private *priv,
  299. sja1105_spi_rw_mode_t rw, u64 reg_addr, u64 *value,
  300. struct ptp_system_timestamp *ptp_sts);
  301. int static_config_buf_prepare_for_upload(struct sja1105_private *priv,
  302. void *config_buf, int buf_len);
  303. int sja1105_static_config_upload(struct sja1105_private *priv);
  304. int sja1105_inhibit_tx(const struct sja1105_private *priv,
  305. unsigned long port_bitmap, bool tx_inhibited);
  306. extern const struct sja1105_info sja1105e_info;
  307. extern const struct sja1105_info sja1105t_info;
  308. extern const struct sja1105_info sja1105p_info;
  309. extern const struct sja1105_info sja1105q_info;
  310. extern const struct sja1105_info sja1105r_info;
  311. extern const struct sja1105_info sja1105s_info;
  312. extern const struct sja1105_info sja1110a_info;
  313. extern const struct sja1105_info sja1110b_info;
  314. extern const struct sja1105_info sja1110c_info;
  315. extern const struct sja1105_info sja1110d_info;
  316. /* From sja1105_clocking.c */
  317. typedef enum {
  318. XMII_MAC = 0,
  319. XMII_PHY = 1,
  320. } sja1105_mii_role_t;
  321. typedef enum {
  322. XMII_MODE_MII = 0,
  323. XMII_MODE_RMII = 1,
  324. XMII_MODE_RGMII = 2,
  325. XMII_MODE_SGMII = 3,
  326. } sja1105_phy_interface_t;
  327. int sja1105pqrs_setup_rgmii_delay(const void *ctx, int port);
  328. int sja1110_setup_rgmii_delay(const void *ctx, int port);
  329. int sja1105_clocking_setup_port(struct sja1105_private *priv, int port);
  330. int sja1105_clocking_setup(struct sja1105_private *priv);
  331. int sja1110_disable_microcontroller(struct sja1105_private *priv);
  332. /* From sja1105_ethtool.c */
  333. void sja1105_get_ethtool_stats(struct dsa_switch *ds, int port, u64 *data);
  334. void sja1105_get_strings(struct dsa_switch *ds, int port,
  335. u32 stringset, u8 *data);
  336. int sja1105_get_sset_count(struct dsa_switch *ds, int port, int sset);
  337. /* From sja1105_dynamic_config.c */
  338. int sja1105_dynamic_config_read(struct sja1105_private *priv,
  339. enum sja1105_blk_idx blk_idx,
  340. int index, void *entry);
  341. int sja1105_dynamic_config_write(struct sja1105_private *priv,
  342. enum sja1105_blk_idx blk_idx,
  343. int index, void *entry, bool keep);
  344. enum sja1105_iotag {
  345. SJA1105_C_TAG = 0, /* Inner VLAN header */
  346. SJA1105_S_TAG = 1, /* Outer VLAN header */
  347. };
  348. enum sja1110_vlan_type {
  349. SJA1110_VLAN_INVALID = 0,
  350. SJA1110_VLAN_C_TAG = 1, /* Single inner VLAN tag */
  351. SJA1110_VLAN_S_TAG = 2, /* Single outer VLAN tag */
  352. SJA1110_VLAN_D_TAG = 3, /* Double tagged, use outer tag for lookup */
  353. };
  354. enum sja1110_shaper_type {
  355. SJA1110_LEAKY_BUCKET_SHAPER = 0,
  356. SJA1110_CBS_SHAPER = 1,
  357. };
  358. u8 sja1105et_fdb_hash(struct sja1105_private *priv, const u8 *addr, u16 vid);
  359. int sja1105et_fdb_add(struct dsa_switch *ds, int port,
  360. const unsigned char *addr, u16 vid);
  361. int sja1105et_fdb_del(struct dsa_switch *ds, int port,
  362. const unsigned char *addr, u16 vid);
  363. int sja1105pqrs_fdb_add(struct dsa_switch *ds, int port,
  364. const unsigned char *addr, u16 vid);
  365. int sja1105pqrs_fdb_del(struct dsa_switch *ds, int port,
  366. const unsigned char *addr, u16 vid);
  367. /* From sja1105_flower.c */
  368. int sja1105_cls_flower_del(struct dsa_switch *ds, int port,
  369. struct flow_cls_offload *cls, bool ingress);
  370. int sja1105_cls_flower_add(struct dsa_switch *ds, int port,
  371. struct flow_cls_offload *cls, bool ingress);
  372. int sja1105_cls_flower_stats(struct dsa_switch *ds, int port,
  373. struct flow_cls_offload *cls, bool ingress);
  374. void sja1105_flower_setup(struct dsa_switch *ds);
  375. void sja1105_flower_teardown(struct dsa_switch *ds);
  376. struct sja1105_rule *sja1105_rule_find(struct sja1105_private *priv,
  377. unsigned long cookie);
  378. #endif