rzn1_a5psw.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Copyright (C) 2022 Schneider Electric
  4. *
  5. * Clément Léger <clement.leger@bootlin.com>
  6. */
  7. #include <linux/clk.h>
  8. #include <linux/debugfs.h>
  9. #include <linux/kernel.h>
  10. #include <linux/module.h>
  11. #include <linux/of.h>
  12. #include <linux/of_mdio.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/pcs-rzn1-miic.h>
  15. #include <net/dsa.h>
  16. #define A5PSW_REVISION 0x0
  17. #define A5PSW_PORT_OFFSET(port) (0x400 * (port))
  18. #define A5PSW_PORT_ENA 0x8
  19. #define A5PSW_PORT_ENA_TX(port) BIT(port)
  20. #define A5PSW_PORT_ENA_RX_SHIFT 16
  21. #define A5PSW_PORT_ENA_TX_RX(port) (BIT((port) + A5PSW_PORT_ENA_RX_SHIFT) | \
  22. BIT(port))
  23. #define A5PSW_UCAST_DEF_MASK 0xC
  24. #define A5PSW_VLAN_VERIFY 0x10
  25. #define A5PSW_VLAN_VERI_SHIFT 0
  26. #define A5PSW_VLAN_DISC_SHIFT 16
  27. #define A5PSW_BCAST_DEF_MASK 0x14
  28. #define A5PSW_MCAST_DEF_MASK 0x18
  29. #define A5PSW_INPUT_LEARN 0x1C
  30. #define A5PSW_INPUT_LEARN_DIS(p) BIT((p) + 16)
  31. #define A5PSW_INPUT_LEARN_BLOCK(p) BIT(p)
  32. #define A5PSW_MGMT_CFG 0x20
  33. #define A5PSW_MGMT_CFG_ENABLE BIT(6)
  34. #define A5PSW_MODE_CFG 0x24
  35. #define A5PSW_MODE_STATS_RESET BIT(31)
  36. #define A5PSW_VLAN_IN_MODE 0x28
  37. #define A5PSW_VLAN_IN_MODE_PORT_SHIFT(port) ((port) * 2)
  38. #define A5PSW_VLAN_IN_MODE_PORT(port) (GENMASK(1, 0) << \
  39. A5PSW_VLAN_IN_MODE_PORT_SHIFT(port))
  40. #define A5PSW_VLAN_IN_MODE_SINGLE_PASSTHROUGH 0x0
  41. #define A5PSW_VLAN_IN_MODE_SINGLE_REPLACE 0x1
  42. #define A5PSW_VLAN_IN_MODE_TAG_ALWAYS 0x2
  43. #define A5PSW_VLAN_OUT_MODE 0x2C
  44. #define A5PSW_VLAN_OUT_MODE_PORT_SHIFT(port) ((port) * 2)
  45. #define A5PSW_VLAN_OUT_MODE_PORT(port) (GENMASK(1, 0) << \
  46. A5PSW_VLAN_OUT_MODE_PORT_SHIFT(port))
  47. #define A5PSW_VLAN_OUT_MODE_DIS 0x0
  48. #define A5PSW_VLAN_OUT_MODE_STRIP 0x1
  49. #define A5PSW_VLAN_OUT_MODE_TAG_THROUGH 0x2
  50. #define A5PSW_VLAN_OUT_MODE_TRANSPARENT 0x3
  51. #define A5PSW_VLAN_IN_MODE_ENA 0x30
  52. #define A5PSW_VLAN_TAG_ID 0x34
  53. #define A5PSW_SYSTEM_TAGINFO(port) (0x200 + 4 * (port))
  54. #define A5PSW_AUTH_PORT(port) (0x240 + 4 * (port))
  55. #define A5PSW_AUTH_PORT_AUTHORIZED BIT(0)
  56. #define A5PSW_VLAN_RES(entry) (0x280 + 4 * (entry))
  57. #define A5PSW_VLAN_RES_WR_PORTMASK BIT(30)
  58. #define A5PSW_VLAN_RES_WR_TAGMASK BIT(29)
  59. #define A5PSW_VLAN_RES_RD_TAGMASK BIT(28)
  60. #define A5PSW_VLAN_RES_VLANID GENMASK(16, 5)
  61. #define A5PSW_VLAN_RES_PORTMASK GENMASK(4, 0)
  62. #define A5PSW_RXMATCH_CONFIG(port) (0x3e80 + 4 * (port))
  63. #define A5PSW_RXMATCH_CONFIG_PATTERN(p) BIT(p)
  64. #define A5PSW_PATTERN_CTRL(p) (0x3eb0 + 4 * (p))
  65. #define A5PSW_PATTERN_CTRL_MGMTFWD BIT(1)
  66. #define A5PSW_LK_CTRL 0x400
  67. #define A5PSW_LK_ADDR_CTRL_BLOCKING BIT(0)
  68. #define A5PSW_LK_ADDR_CTRL_LEARNING BIT(1)
  69. #define A5PSW_LK_ADDR_CTRL_AGEING BIT(2)
  70. #define A5PSW_LK_ADDR_CTRL_ALLOW_MIGR BIT(3)
  71. #define A5PSW_LK_ADDR_CTRL_CLEAR_TABLE BIT(6)
  72. #define A5PSW_LK_ADDR_CTRL 0x408
  73. #define A5PSW_LK_ADDR_CTRL_BUSY BIT(31)
  74. #define A5PSW_LK_ADDR_CTRL_DELETE_PORT BIT(30)
  75. #define A5PSW_LK_ADDR_CTRL_CLEAR BIT(29)
  76. #define A5PSW_LK_ADDR_CTRL_LOOKUP BIT(28)
  77. #define A5PSW_LK_ADDR_CTRL_WAIT BIT(27)
  78. #define A5PSW_LK_ADDR_CTRL_READ BIT(26)
  79. #define A5PSW_LK_ADDR_CTRL_WRITE BIT(25)
  80. #define A5PSW_LK_ADDR_CTRL_ADDRESS GENMASK(12, 0)
  81. #define A5PSW_LK_DATA_LO 0x40C
  82. #define A5PSW_LK_DATA_HI 0x410
  83. #define A5PSW_LK_DATA_HI_VALID BIT(16)
  84. #define A5PSW_LK_DATA_HI_PORT BIT(16)
  85. #define A5PSW_LK_LEARNCOUNT 0x418
  86. #define A5PSW_LK_LEARNCOUNT_COUNT GENMASK(13, 0)
  87. #define A5PSW_LK_LEARNCOUNT_MODE GENMASK(31, 30)
  88. #define A5PSW_LK_LEARNCOUNT_MODE_SET 0x0
  89. #define A5PSW_LK_LEARNCOUNT_MODE_INC 0x1
  90. #define A5PSW_LK_LEARNCOUNT_MODE_DEC 0x2
  91. #define A5PSW_MGMT_TAG_CFG 0x480
  92. #define A5PSW_MGMT_TAG_CFG_TAGFIELD GENMASK(31, 16)
  93. #define A5PSW_MGMT_TAG_CFG_ALL_FRAMES BIT(1)
  94. #define A5PSW_MGMT_TAG_CFG_ENABLE BIT(0)
  95. #define A5PSW_LK_AGETIME 0x41C
  96. #define A5PSW_LK_AGETIME_MASK GENMASK(23, 0)
  97. #define A5PSW_MDIO_CFG_STATUS 0x700
  98. #define A5PSW_MDIO_CFG_STATUS_CLKDIV GENMASK(15, 7)
  99. #define A5PSW_MDIO_CFG_STATUS_READERR BIT(1)
  100. #define A5PSW_MDIO_CFG_STATUS_BUSY BIT(0)
  101. #define A5PSW_MDIO_COMMAND 0x704
  102. /* Register is named TRAININIT in datasheet and should be set when reading */
  103. #define A5PSW_MDIO_COMMAND_READ BIT(15)
  104. #define A5PSW_MDIO_COMMAND_PHY_ADDR GENMASK(9, 5)
  105. #define A5PSW_MDIO_COMMAND_REG_ADDR GENMASK(4, 0)
  106. #define A5PSW_MDIO_DATA 0x708
  107. #define A5PSW_MDIO_DATA_MASK GENMASK(15, 0)
  108. #define A5PSW_CMD_CFG(port) (0x808 + A5PSW_PORT_OFFSET(port))
  109. #define A5PSW_CMD_CFG_CNTL_FRM_ENA BIT(23)
  110. #define A5PSW_CMD_CFG_SW_RESET BIT(13)
  111. #define A5PSW_CMD_CFG_TX_CRC_APPEND BIT(11)
  112. #define A5PSW_CMD_CFG_HD_ENA BIT(10)
  113. #define A5PSW_CMD_CFG_PAUSE_IGNORE BIT(8)
  114. #define A5PSW_CMD_CFG_CRC_FWD BIT(6)
  115. #define A5PSW_CMD_CFG_ETH_SPEED BIT(3)
  116. #define A5PSW_CMD_CFG_RX_ENA BIT(1)
  117. #define A5PSW_CMD_CFG_TX_ENA BIT(0)
  118. #define A5PSW_FRM_LENGTH(port) (0x814 + A5PSW_PORT_OFFSET(port))
  119. #define A5PSW_FRM_LENGTH_MASK GENMASK(13, 0)
  120. #define A5PSW_STATUS(port) (0x840 + A5PSW_PORT_OFFSET(port))
  121. #define A5PSW_STATS_HIWORD 0x900
  122. /* Stats */
  123. #define A5PSW_aFramesTransmittedOK 0x868
  124. #define A5PSW_aFramesReceivedOK 0x86C
  125. #define A5PSW_aFrameCheckSequenceErrors 0x870
  126. #define A5PSW_aAlignmentErrors 0x874
  127. #define A5PSW_aOctetsTransmittedOK 0x878
  128. #define A5PSW_aOctetsReceivedOK 0x87C
  129. #define A5PSW_aTxPAUSEMACCtrlFrames 0x880
  130. #define A5PSW_aRxPAUSEMACCtrlFrames 0x884
  131. /* If */
  132. #define A5PSW_ifInErrors 0x888
  133. #define A5PSW_ifOutErrors 0x88C
  134. #define A5PSW_ifInUcastPkts 0x890
  135. #define A5PSW_ifInMulticastPkts 0x894
  136. #define A5PSW_ifInBroadcastPkts 0x898
  137. #define A5PSW_ifOutDiscards 0x89C
  138. #define A5PSW_ifOutUcastPkts 0x8A0
  139. #define A5PSW_ifOutMulticastPkts 0x8A4
  140. #define A5PSW_ifOutBroadcastPkts 0x8A8
  141. /* Ether */
  142. #define A5PSW_etherStatsDropEvents 0x8AC
  143. #define A5PSW_etherStatsOctets 0x8B0
  144. #define A5PSW_etherStatsPkts 0x8B4
  145. #define A5PSW_etherStatsUndersizePkts 0x8B8
  146. #define A5PSW_etherStatsOversizePkts 0x8BC
  147. #define A5PSW_etherStatsPkts64Octets 0x8C0
  148. #define A5PSW_etherStatsPkts65to127Octets 0x8C4
  149. #define A5PSW_etherStatsPkts128to255Octets 0x8C8
  150. #define A5PSW_etherStatsPkts256to511Octets 0x8CC
  151. #define A5PSW_etherStatsPkts512to1023Octets 0x8D0
  152. #define A5PSW_etherStatsPkts1024to1518Octets 0x8D4
  153. #define A5PSW_etherStatsPkts1519toXOctets 0x8D8
  154. #define A5PSW_etherStatsJabbers 0x8DC
  155. #define A5PSW_etherStatsFragments 0x8E0
  156. #define A5PSW_VLANReceived 0x8E8
  157. #define A5PSW_VLANTransmitted 0x8EC
  158. #define A5PSW_aDeferred 0x910
  159. #define A5PSW_aMultipleCollisions 0x914
  160. #define A5PSW_aSingleCollisions 0x918
  161. #define A5PSW_aLateCollisions 0x91C
  162. #define A5PSW_aExcessiveCollisions 0x920
  163. #define A5PSW_aCarrierSenseErrors 0x924
  164. #define A5PSW_VLAN_TAG(prio, id) (((prio) << 12) | (id))
  165. #define A5PSW_PORTS_NUM 5
  166. #define A5PSW_CPU_PORT (A5PSW_PORTS_NUM - 1)
  167. #define A5PSW_MDIO_DEF_FREQ 2500000
  168. #define A5PSW_MDIO_TIMEOUT 100
  169. #define A5PSW_JUMBO_LEN (10 * SZ_1K)
  170. #define A5PSW_MDIO_CLK_DIV_MIN 5
  171. #define A5PSW_TAG_LEN 8
  172. #define A5PSW_VLAN_COUNT 32
  173. /* Ensure enough space for 2 VLAN tags */
  174. #define A5PSW_EXTRA_MTU_LEN (A5PSW_TAG_LEN + 8)
  175. #define A5PSW_MAX_MTU (A5PSW_JUMBO_LEN - A5PSW_EXTRA_MTU_LEN)
  176. #define A5PSW_PATTERN_MGMTFWD 0
  177. #define A5PSW_LK_BUSY_USEC_POLL 10
  178. #define A5PSW_CTRL_TIMEOUT 1000
  179. #define A5PSW_TABLE_ENTRIES 8192
  180. struct fdb_entry {
  181. u8 mac[ETH_ALEN];
  182. u16 valid:1;
  183. u16 is_static:1;
  184. u16 prio:3;
  185. u16 port_mask:5;
  186. u16 reserved:6;
  187. } __packed;
  188. union lk_data {
  189. struct {
  190. u32 lo;
  191. u32 hi;
  192. };
  193. struct fdb_entry entry;
  194. };
  195. /**
  196. * struct a5psw - switch struct
  197. * @base: Base address of the switch
  198. * @hclk: hclk_switch clock
  199. * @clk: clk_switch clock
  200. * @dev: Device associated to the switch
  201. * @mii_bus: MDIO bus struct
  202. * @mdio_freq: MDIO bus frequency requested
  203. * @pcs: Array of PCS connected to the switch ports (not for the CPU)
  204. * @ds: DSA switch struct
  205. * @stats_lock: lock to access statistics (shared HI counter)
  206. * @lk_lock: Lock for the lookup table
  207. * @reg_lock: Lock for register read-modify-write operation
  208. * @bridged_ports: Mask of ports that are bridged and should be flooded
  209. * @br_dev: Bridge net device
  210. */
  211. struct a5psw {
  212. void __iomem *base;
  213. struct clk *hclk;
  214. struct clk *clk;
  215. struct device *dev;
  216. struct mii_bus *mii_bus;
  217. struct phylink_pcs *pcs[A5PSW_PORTS_NUM - 1];
  218. struct dsa_switch ds;
  219. struct mutex lk_lock;
  220. spinlock_t reg_lock;
  221. u32 bridged_ports;
  222. struct net_device *br_dev;
  223. };