mtk_eth_soc.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. /* This program is free software; you can redistribute it and/or modify
  2. * it under the terms of the GNU General Public License as published by
  3. * the Free Software Foundation; version 2 of the License
  4. *
  5. * This program is distributed in the hope that it will be useful,
  6. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  7. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  8. * GNU General Public License for more details.
  9. *
  10. * Copyright (C) 2009-2016 John Crispin <blogic@openwrt.org>
  11. * Copyright (C) 2009-2016 Felix Fietkau <nbd@openwrt.org>
  12. * Copyright (C) 2013-2016 Michael Lee <igvtee@gmail.com>
  13. */
  14. #ifndef MTK_ETH_H
  15. #define MTK_ETH_H
  16. #define MTK_QDMA_PAGE_SIZE 2048
  17. #define MTK_MAX_RX_LENGTH 1536
  18. #define MTK_TX_DMA_BUF_LEN 0x3fff
  19. #define MTK_DMA_SIZE 256
  20. #define MTK_NAPI_WEIGHT 64
  21. #define MTK_MAC_COUNT 2
  22. #define MTK_RX_ETH_HLEN (VLAN_ETH_HLEN + VLAN_HLEN + ETH_FCS_LEN)
  23. #define MTK_RX_HLEN (NET_SKB_PAD + MTK_RX_ETH_HLEN + NET_IP_ALIGN)
  24. #define MTK_DMA_DUMMY_DESC 0xffffffff
  25. #define MTK_DEFAULT_MSG_ENABLE (NETIF_MSG_DRV | \
  26. NETIF_MSG_PROBE | \
  27. NETIF_MSG_LINK | \
  28. NETIF_MSG_TIMER | \
  29. NETIF_MSG_IFDOWN | \
  30. NETIF_MSG_IFUP | \
  31. NETIF_MSG_RX_ERR | \
  32. NETIF_MSG_TX_ERR)
  33. #define MTK_HW_FEATURES (NETIF_F_IP_CSUM | \
  34. NETIF_F_RXCSUM | \
  35. NETIF_F_HW_VLAN_CTAG_TX | \
  36. NETIF_F_HW_VLAN_CTAG_RX | \
  37. NETIF_F_SG | NETIF_F_TSO | \
  38. NETIF_F_TSO6 | \
  39. NETIF_F_IPV6_CSUM)
  40. #define NEXT_RX_DESP_IDX(X) (((X) + 1) & (MTK_DMA_SIZE - 1))
  41. /* Frame Engine Global Reset Register */
  42. #define MTK_RST_GL 0x04
  43. #define RST_GL_PSE BIT(0)
  44. /* Frame Engine Interrupt Status Register */
  45. #define MTK_INT_STATUS2 0x08
  46. #define MTK_GDM1_AF BIT(28)
  47. #define MTK_GDM2_AF BIT(29)
  48. /* Frame Engine Interrupt Grouping Register */
  49. #define MTK_FE_INT_GRP 0x20
  50. /* CDMP Exgress Control Register */
  51. #define MTK_CDMP_EG_CTRL 0x404
  52. /* GDM Exgress Control Register */
  53. #define MTK_GDMA_FWD_CFG(x) (0x500 + (x * 0x1000))
  54. #define MTK_GDMA_ICS_EN BIT(22)
  55. #define MTK_GDMA_TCS_EN BIT(21)
  56. #define MTK_GDMA_UCS_EN BIT(20)
  57. /* Unicast Filter MAC Address Register - Low */
  58. #define MTK_GDMA_MAC_ADRL(x) (0x508 + (x * 0x1000))
  59. /* Unicast Filter MAC Address Register - High */
  60. #define MTK_GDMA_MAC_ADRH(x) (0x50C + (x * 0x1000))
  61. /* QDMA TX Queue Configuration Registers */
  62. #define MTK_QTX_CFG(x) (0x1800 + (x * 0x10))
  63. #define QDMA_RES_THRES 4
  64. /* QDMA TX Queue Scheduler Registers */
  65. #define MTK_QTX_SCH(x) (0x1804 + (x * 0x10))
  66. /* QDMA RX Base Pointer Register */
  67. #define MTK_QRX_BASE_PTR0 0x1900
  68. /* QDMA RX Maximum Count Register */
  69. #define MTK_QRX_MAX_CNT0 0x1904
  70. /* QDMA RX CPU Pointer Register */
  71. #define MTK_QRX_CRX_IDX0 0x1908
  72. /* QDMA RX DMA Pointer Register */
  73. #define MTK_QRX_DRX_IDX0 0x190C
  74. /* QDMA Global Configuration Register */
  75. #define MTK_QDMA_GLO_CFG 0x1A04
  76. #define MTK_RX_2B_OFFSET BIT(31)
  77. #define MTK_RX_BT_32DWORDS (3 << 11)
  78. #define MTK_TX_WB_DDONE BIT(6)
  79. #define MTK_DMA_SIZE_16DWORDS (2 << 4)
  80. #define MTK_RX_DMA_BUSY BIT(3)
  81. #define MTK_TX_DMA_BUSY BIT(1)
  82. #define MTK_RX_DMA_EN BIT(2)
  83. #define MTK_TX_DMA_EN BIT(0)
  84. #define MTK_DMA_BUSY_TIMEOUT HZ
  85. /* QDMA Reset Index Register */
  86. #define MTK_QDMA_RST_IDX 0x1A08
  87. #define MTK_PST_DRX_IDX0 BIT(16)
  88. /* QDMA Delay Interrupt Register */
  89. #define MTK_QDMA_DELAY_INT 0x1A0C
  90. /* QDMA Flow Control Register */
  91. #define MTK_QDMA_FC_THRES 0x1A10
  92. #define FC_THRES_DROP_MODE BIT(20)
  93. #define FC_THRES_DROP_EN (7 << 16)
  94. #define FC_THRES_MIN 0x4444
  95. /* QDMA Interrupt Status Register */
  96. #define MTK_QMTK_INT_STATUS 0x1A18
  97. #define MTK_RX_DONE_INT1 BIT(17)
  98. #define MTK_RX_DONE_INT0 BIT(16)
  99. #define MTK_TX_DONE_INT3 BIT(3)
  100. #define MTK_TX_DONE_INT2 BIT(2)
  101. #define MTK_TX_DONE_INT1 BIT(1)
  102. #define MTK_TX_DONE_INT0 BIT(0)
  103. #define MTK_RX_DONE_INT (MTK_RX_DONE_INT0 | MTK_RX_DONE_INT1)
  104. #define MTK_TX_DONE_INT (MTK_TX_DONE_INT0 | MTK_TX_DONE_INT1 | \
  105. MTK_TX_DONE_INT2 | MTK_TX_DONE_INT3)
  106. /* QDMA Interrupt Status Register */
  107. #define MTK_QDMA_INT_MASK 0x1A1C
  108. /* QDMA Interrupt Mask Register */
  109. #define MTK_QDMA_HRED2 0x1A44
  110. /* QDMA TX Forward CPU Pointer Register */
  111. #define MTK_QTX_CTX_PTR 0x1B00
  112. /* QDMA TX Forward DMA Pointer Register */
  113. #define MTK_QTX_DTX_PTR 0x1B04
  114. /* QDMA TX Release CPU Pointer Register */
  115. #define MTK_QTX_CRX_PTR 0x1B10
  116. /* QDMA TX Release DMA Pointer Register */
  117. #define MTK_QTX_DRX_PTR 0x1B14
  118. /* QDMA FQ Head Pointer Register */
  119. #define MTK_QDMA_FQ_HEAD 0x1B20
  120. /* QDMA FQ Head Pointer Register */
  121. #define MTK_QDMA_FQ_TAIL 0x1B24
  122. /* QDMA FQ Free Page Counter Register */
  123. #define MTK_QDMA_FQ_CNT 0x1B28
  124. /* QDMA FQ Free Page Buffer Length Register */
  125. #define MTK_QDMA_FQ_BLEN 0x1B2C
  126. /* GMA1 Received Good Byte Count Register */
  127. #define MTK_GDM1_TX_GBCNT 0x2400
  128. #define MTK_STAT_OFFSET 0x40
  129. /* QDMA descriptor txd4 */
  130. #define TX_DMA_CHKSUM (0x7 << 29)
  131. #define TX_DMA_TSO BIT(28)
  132. #define TX_DMA_FPORT_SHIFT 25
  133. #define TX_DMA_FPORT_MASK 0x7
  134. #define TX_DMA_INS_VLAN BIT(16)
  135. /* QDMA descriptor txd3 */
  136. #define TX_DMA_OWNER_CPU BIT(31)
  137. #define TX_DMA_LS0 BIT(30)
  138. #define TX_DMA_PLEN0(_x) (((_x) & MTK_TX_DMA_BUF_LEN) << 16)
  139. #define TX_DMA_SWC BIT(14)
  140. #define TX_DMA_SDL(_x) (((_x) & 0x3fff) << 16)
  141. /* QDMA descriptor rxd2 */
  142. #define RX_DMA_DONE BIT(31)
  143. #define RX_DMA_PLEN0(_x) (((_x) & 0x3fff) << 16)
  144. #define RX_DMA_GET_PLEN0(_x) (((_x) >> 16) & 0x3fff)
  145. /* QDMA descriptor rxd3 */
  146. #define RX_DMA_VID(_x) ((_x) & 0xfff)
  147. /* QDMA descriptor rxd4 */
  148. #define RX_DMA_L4_VALID BIT(24)
  149. #define RX_DMA_FPORT_SHIFT 19
  150. #define RX_DMA_FPORT_MASK 0x7
  151. /* PHY Indirect Access Control registers */
  152. #define MTK_PHY_IAC 0x10004
  153. #define PHY_IAC_ACCESS BIT(31)
  154. #define PHY_IAC_READ BIT(19)
  155. #define PHY_IAC_WRITE BIT(18)
  156. #define PHY_IAC_START BIT(16)
  157. #define PHY_IAC_ADDR_SHIFT 20
  158. #define PHY_IAC_REG_SHIFT 25
  159. #define PHY_IAC_TIMEOUT HZ
  160. /* Mac control registers */
  161. #define MTK_MAC_MCR(x) (0x10100 + (x * 0x100))
  162. #define MAC_MCR_MAX_RX_1536 BIT(24)
  163. #define MAC_MCR_IPG_CFG (BIT(18) | BIT(16))
  164. #define MAC_MCR_FORCE_MODE BIT(15)
  165. #define MAC_MCR_TX_EN BIT(14)
  166. #define MAC_MCR_RX_EN BIT(13)
  167. #define MAC_MCR_BACKOFF_EN BIT(9)
  168. #define MAC_MCR_BACKPR_EN BIT(8)
  169. #define MAC_MCR_FORCE_RX_FC BIT(5)
  170. #define MAC_MCR_FORCE_TX_FC BIT(4)
  171. #define MAC_MCR_SPEED_1000 BIT(3)
  172. #define MAC_MCR_SPEED_100 BIT(2)
  173. #define MAC_MCR_FORCE_DPX BIT(1)
  174. #define MAC_MCR_FORCE_LINK BIT(0)
  175. #define MAC_MCR_FIXED_LINK (MAC_MCR_MAX_RX_1536 | MAC_MCR_IPG_CFG | \
  176. MAC_MCR_FORCE_MODE | MAC_MCR_TX_EN | \
  177. MAC_MCR_RX_EN | MAC_MCR_BACKOFF_EN | \
  178. MAC_MCR_BACKPR_EN | MAC_MCR_FORCE_RX_FC | \
  179. MAC_MCR_FORCE_TX_FC | MAC_MCR_SPEED_1000 | \
  180. MAC_MCR_FORCE_DPX | MAC_MCR_FORCE_LINK)
  181. /* GPIO port control registers for GMAC 2*/
  182. #define GPIO_OD33_CTRL8 0x4c0
  183. #define GPIO_BIAS_CTRL 0xed0
  184. #define GPIO_DRV_SEL10 0xf00
  185. /* ethernet subsystem config register */
  186. #define ETHSYS_SYSCFG0 0x14
  187. #define SYSCFG0_GE_MASK 0x3
  188. #define SYSCFG0_GE_MODE(x, y) (x << (12 + (y * 2)))
  189. struct mtk_rx_dma {
  190. unsigned int rxd1;
  191. unsigned int rxd2;
  192. unsigned int rxd3;
  193. unsigned int rxd4;
  194. } __packed __aligned(4);
  195. struct mtk_tx_dma {
  196. unsigned int txd1;
  197. unsigned int txd2;
  198. unsigned int txd3;
  199. unsigned int txd4;
  200. } __packed __aligned(4);
  201. struct mtk_eth;
  202. struct mtk_mac;
  203. /* struct mtk_hw_stats - the structure that holds the traffic statistics.
  204. * @stats_lock: make sure that stats operations are atomic
  205. * @reg_offset: the status register offset of the SoC
  206. * @syncp: the refcount
  207. *
  208. * All of the supported SoCs have hardware counters for traffic statistics.
  209. * Whenever the status IRQ triggers we can read the latest stats from these
  210. * counters and store them in this struct.
  211. */
  212. struct mtk_hw_stats {
  213. u64 tx_bytes;
  214. u64 tx_packets;
  215. u64 tx_skip;
  216. u64 tx_collisions;
  217. u64 rx_bytes;
  218. u64 rx_packets;
  219. u64 rx_overflow;
  220. u64 rx_fcs_errors;
  221. u64 rx_short_errors;
  222. u64 rx_long_errors;
  223. u64 rx_checksum_errors;
  224. u64 rx_flow_control_packets;
  225. spinlock_t stats_lock;
  226. u32 reg_offset;
  227. struct u64_stats_sync syncp;
  228. };
  229. /* PDMA descriptor can point at 1-2 segments. This enum allows us to track how
  230. * memory was allocated so that it can be freed properly
  231. */
  232. enum mtk_tx_flags {
  233. MTK_TX_FLAGS_SINGLE0 = 0x01,
  234. MTK_TX_FLAGS_PAGE0 = 0x02,
  235. };
  236. /* struct mtk_tx_buf - This struct holds the pointers to the memory pointed at
  237. * by the TX descriptor s
  238. * @skb: The SKB pointer of the packet being sent
  239. * @dma_addr0: The base addr of the first segment
  240. * @dma_len0: The length of the first segment
  241. * @dma_addr1: The base addr of the second segment
  242. * @dma_len1: The length of the second segment
  243. */
  244. struct mtk_tx_buf {
  245. struct sk_buff *skb;
  246. u32 flags;
  247. DEFINE_DMA_UNMAP_ADDR(dma_addr0);
  248. DEFINE_DMA_UNMAP_LEN(dma_len0);
  249. DEFINE_DMA_UNMAP_ADDR(dma_addr1);
  250. DEFINE_DMA_UNMAP_LEN(dma_len1);
  251. };
  252. /* struct mtk_tx_ring - This struct holds info describing a TX ring
  253. * @dma: The descriptor ring
  254. * @buf: The memory pointed at by the ring
  255. * @phys: The physical addr of tx_buf
  256. * @next_free: Pointer to the next free descriptor
  257. * @last_free: Pointer to the last free descriptor
  258. * @thresh: The threshold of minimum amount of free descriptors
  259. * @free_count: QDMA uses a linked list. Track how many free descriptors
  260. * are present
  261. */
  262. struct mtk_tx_ring {
  263. struct mtk_tx_dma *dma;
  264. struct mtk_tx_buf *buf;
  265. dma_addr_t phys;
  266. struct mtk_tx_dma *next_free;
  267. struct mtk_tx_dma *last_free;
  268. u16 thresh;
  269. atomic_t free_count;
  270. };
  271. /* struct mtk_rx_ring - This struct holds info describing a RX ring
  272. * @dma: The descriptor ring
  273. * @data: The memory pointed at by the ring
  274. * @phys: The physical addr of rx_buf
  275. * @frag_size: How big can each fragment be
  276. * @buf_size: The size of each packet buffer
  277. * @calc_idx: The current head of ring
  278. */
  279. struct mtk_rx_ring {
  280. struct mtk_rx_dma *dma;
  281. u8 **data;
  282. dma_addr_t phys;
  283. u16 frag_size;
  284. u16 buf_size;
  285. u16 calc_idx;
  286. };
  287. /* currently no SoC has more than 2 macs */
  288. #define MTK_MAX_DEVS 2
  289. /* struct mtk_eth - This is the main datasructure for holding the state
  290. * of the driver
  291. * @dev: The device pointer
  292. * @base: The mapped register i/o base
  293. * @page_lock: Make sure that register operations are atomic
  294. * @dummy_dev: we run 2 netdevs on 1 physical DMA ring and need a
  295. * dummy for NAPI to work
  296. * @netdev: The netdev instances
  297. * @mac: Each netdev is linked to a physical MAC
  298. * @irq: The IRQ that we are using
  299. * @msg_enable: Ethtool msg level
  300. * @ethsys: The register map pointing at the range used to setup
  301. * MII modes
  302. * @pctl: The register map pointing at the range used to setup
  303. * GMAC port drive/slew values
  304. * @dma_refcnt: track how many netdevs are using the DMA engine
  305. * @tx_ring: Pointer to the memore holding info about the TX ring
  306. * @rx_ring: Pointer to the memore holding info about the RX ring
  307. * @rx_napi: The NAPI struct
  308. * @scratch_ring: Newer SoCs need memory for a second HW managed TX ring
  309. * @scratch_head: The scratch memory that scratch_ring points to.
  310. * @clk_ethif: The ethif clock
  311. * @clk_esw: The switch clock
  312. * @clk_gp1: The gmac1 clock
  313. * @clk_gp2: The gmac2 clock
  314. * @mii_bus: If there is a bus we need to create an instance for it
  315. */
  316. struct mtk_eth {
  317. struct device *dev;
  318. void __iomem *base;
  319. struct reset_control *rstc;
  320. spinlock_t page_lock;
  321. struct net_device dummy_dev;
  322. struct net_device *netdev[MTK_MAX_DEVS];
  323. struct mtk_mac *mac[MTK_MAX_DEVS];
  324. int irq;
  325. u32 msg_enable;
  326. unsigned long sysclk;
  327. struct regmap *ethsys;
  328. struct regmap *pctl;
  329. atomic_t dma_refcnt;
  330. struct mtk_tx_ring tx_ring;
  331. struct mtk_rx_ring rx_ring;
  332. struct napi_struct rx_napi;
  333. struct mtk_tx_dma *scratch_ring;
  334. void *scratch_head;
  335. struct clk *clk_ethif;
  336. struct clk *clk_esw;
  337. struct clk *clk_gp1;
  338. struct clk *clk_gp2;
  339. struct mii_bus *mii_bus;
  340. };
  341. /* struct mtk_mac - the structure that holds the info about the MACs of the
  342. * SoC
  343. * @id: The number of the MAC
  344. * @of_node: Our devicetree node
  345. * @hw: Backpointer to our main datastruture
  346. * @hw_stats: Packet statistics counter
  347. * @phy_dev: The attached PHY if available
  348. * @pending_work: The workqueue used to reset the dma ring
  349. */
  350. struct mtk_mac {
  351. int id;
  352. struct device_node *of_node;
  353. struct mtk_eth *hw;
  354. struct mtk_hw_stats *hw_stats;
  355. struct phy_device *phy_dev;
  356. struct work_struct pending_work;
  357. };
  358. /* the struct describing the SoC. these are declared in the soc_xyz.c files */
  359. extern const struct of_device_id of_mtk_match[];
  360. /* read the hardware status register */
  361. void mtk_stats_update_mac(struct mtk_mac *mac);
  362. void mtk_w32(struct mtk_eth *eth, u32 val, unsigned reg);
  363. u32 mtk_r32(struct mtk_eth *eth, unsigned reg);
  364. #endif /* MTK_ETH_H */