mtk_eth_soc.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  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, Y) (((X) + 1) & ((Y) - 1))
  41. #define MTK_MAX_RX_RING_NUM 4
  42. #define MTK_HW_LRO_DMA_SIZE 8
  43. #define MTK_MAX_LRO_RX_LENGTH (4096 * 3)
  44. #define MTK_MAX_LRO_IP_CNT 2
  45. #define MTK_HW_LRO_TIMER_UNIT 1 /* 20 us */
  46. #define MTK_HW_LRO_REFRESH_TIME 50000 /* 1 sec. */
  47. #define MTK_HW_LRO_AGG_TIME 10 /* 200us */
  48. #define MTK_HW_LRO_AGE_TIME 50 /* 1ms */
  49. #define MTK_HW_LRO_MAX_AGG_CNT 64
  50. #define MTK_HW_LRO_BW_THRE 3000
  51. #define MTK_HW_LRO_REPLACE_DELTA 1000
  52. #define MTK_HW_LRO_SDL_REMAIN_ROOM 1522
  53. /* Frame Engine Global Reset Register */
  54. #define MTK_RST_GL 0x04
  55. #define RST_GL_PSE BIT(0)
  56. /* Frame Engine Interrupt Status Register */
  57. #define MTK_INT_STATUS2 0x08
  58. #define MTK_GDM1_AF BIT(28)
  59. #define MTK_GDM2_AF BIT(29)
  60. /* PDMA HW LRO Alter Flow Timer Register */
  61. #define MTK_PDMA_LRO_ALT_REFRESH_TIMER 0x1c
  62. /* Frame Engine Interrupt Grouping Register */
  63. #define MTK_FE_INT_GRP 0x20
  64. /* CDMP Ingress Control Register */
  65. #define MTK_CDMQ_IG_CTRL 0x1400
  66. #define MTK_CDMQ_STAG_EN BIT(0)
  67. /* CDMP Exgress Control Register */
  68. #define MTK_CDMP_EG_CTRL 0x404
  69. /* GDM Exgress Control Register */
  70. #define MTK_GDMA_FWD_CFG(x) (0x500 + (x * 0x1000))
  71. #define MTK_GDMA_ICS_EN BIT(22)
  72. #define MTK_GDMA_TCS_EN BIT(21)
  73. #define MTK_GDMA_UCS_EN BIT(20)
  74. /* Unicast Filter MAC Address Register - Low */
  75. #define MTK_GDMA_MAC_ADRL(x) (0x508 + (x * 0x1000))
  76. /* Unicast Filter MAC Address Register - High */
  77. #define MTK_GDMA_MAC_ADRH(x) (0x50C + (x * 0x1000))
  78. /* PDMA RX Base Pointer Register */
  79. #define MTK_PRX_BASE_PTR0 0x900
  80. #define MTK_PRX_BASE_PTR_CFG(x) (MTK_PRX_BASE_PTR0 + (x * 0x10))
  81. /* PDMA RX Maximum Count Register */
  82. #define MTK_PRX_MAX_CNT0 0x904
  83. #define MTK_PRX_MAX_CNT_CFG(x) (MTK_PRX_MAX_CNT0 + (x * 0x10))
  84. /* PDMA RX CPU Pointer Register */
  85. #define MTK_PRX_CRX_IDX0 0x908
  86. #define MTK_PRX_CRX_IDX_CFG(x) (MTK_PRX_CRX_IDX0 + (x * 0x10))
  87. /* PDMA HW LRO Control Registers */
  88. #define MTK_PDMA_LRO_CTRL_DW0 0x980
  89. #define MTK_LRO_EN BIT(0)
  90. #define MTK_L3_CKS_UPD_EN BIT(7)
  91. #define MTK_LRO_ALT_PKT_CNT_MODE BIT(21)
  92. #define MTK_LRO_RING_RELINQUISH_REQ (0x7 << 26)
  93. #define MTK_LRO_RING_RELINQUISH_DONE (0x7 << 29)
  94. #define MTK_PDMA_LRO_CTRL_DW1 0x984
  95. #define MTK_PDMA_LRO_CTRL_DW2 0x988
  96. #define MTK_PDMA_LRO_CTRL_DW3 0x98c
  97. #define MTK_ADMA_MODE BIT(15)
  98. #define MTK_LRO_MIN_RXD_SDL (MTK_HW_LRO_SDL_REMAIN_ROOM << 16)
  99. /* PDMA Global Configuration Register */
  100. #define MTK_PDMA_GLO_CFG 0xa04
  101. #define MTK_MULTI_EN BIT(10)
  102. /* PDMA Reset Index Register */
  103. #define MTK_PDMA_RST_IDX 0xa08
  104. #define MTK_PST_DRX_IDX0 BIT(16)
  105. #define MTK_PST_DRX_IDX_CFG(x) (MTK_PST_DRX_IDX0 << (x))
  106. /* PDMA Delay Interrupt Register */
  107. #define MTK_PDMA_DELAY_INT 0xa0c
  108. /* PDMA Interrupt Status Register */
  109. #define MTK_PDMA_INT_STATUS 0xa20
  110. /* PDMA Interrupt Mask Register */
  111. #define MTK_PDMA_INT_MASK 0xa28
  112. /* PDMA HW LRO Alter Flow Delta Register */
  113. #define MTK_PDMA_LRO_ALT_SCORE_DELTA 0xa4c
  114. /* PDMA Interrupt grouping registers */
  115. #define MTK_PDMA_INT_GRP1 0xa50
  116. #define MTK_PDMA_INT_GRP2 0xa54
  117. /* PDMA HW LRO IP Setting Registers */
  118. #define MTK_LRO_RX_RING0_DIP_DW0 0xb04
  119. #define MTK_LRO_DIP_DW0_CFG(x) (MTK_LRO_RX_RING0_DIP_DW0 + (x * 0x40))
  120. #define MTK_RING_MYIP_VLD BIT(9)
  121. /* PDMA HW LRO Ring Control Registers */
  122. #define MTK_LRO_RX_RING0_CTRL_DW1 0xb28
  123. #define MTK_LRO_RX_RING0_CTRL_DW2 0xb2c
  124. #define MTK_LRO_RX_RING0_CTRL_DW3 0xb30
  125. #define MTK_LRO_CTRL_DW1_CFG(x) (MTK_LRO_RX_RING0_CTRL_DW1 + (x * 0x40))
  126. #define MTK_LRO_CTRL_DW2_CFG(x) (MTK_LRO_RX_RING0_CTRL_DW2 + (x * 0x40))
  127. #define MTK_LRO_CTRL_DW3_CFG(x) (MTK_LRO_RX_RING0_CTRL_DW3 + (x * 0x40))
  128. #define MTK_RING_AGE_TIME_L ((MTK_HW_LRO_AGE_TIME & 0x3ff) << 22)
  129. #define MTK_RING_AGE_TIME_H ((MTK_HW_LRO_AGE_TIME >> 10) & 0x3f)
  130. #define MTK_RING_AUTO_LERAN_MODE (3 << 6)
  131. #define MTK_RING_VLD BIT(8)
  132. #define MTK_RING_MAX_AGG_TIME ((MTK_HW_LRO_AGG_TIME & 0xffff) << 10)
  133. #define MTK_RING_MAX_AGG_CNT_L ((MTK_HW_LRO_MAX_AGG_CNT & 0x3f) << 26)
  134. #define MTK_RING_MAX_AGG_CNT_H ((MTK_HW_LRO_MAX_AGG_CNT >> 6) & 0x3)
  135. /* QDMA TX Queue Configuration Registers */
  136. #define MTK_QTX_CFG(x) (0x1800 + (x * 0x10))
  137. #define QDMA_RES_THRES 4
  138. /* QDMA TX Queue Scheduler Registers */
  139. #define MTK_QTX_SCH(x) (0x1804 + (x * 0x10))
  140. /* QDMA RX Base Pointer Register */
  141. #define MTK_QRX_BASE_PTR0 0x1900
  142. /* QDMA RX Maximum Count Register */
  143. #define MTK_QRX_MAX_CNT0 0x1904
  144. /* QDMA RX CPU Pointer Register */
  145. #define MTK_QRX_CRX_IDX0 0x1908
  146. /* QDMA RX DMA Pointer Register */
  147. #define MTK_QRX_DRX_IDX0 0x190C
  148. /* QDMA Global Configuration Register */
  149. #define MTK_QDMA_GLO_CFG 0x1A04
  150. #define MTK_RX_2B_OFFSET BIT(31)
  151. #define MTK_RX_BT_32DWORDS (3 << 11)
  152. #define MTK_NDP_CO_PRO BIT(10)
  153. #define MTK_TX_WB_DDONE BIT(6)
  154. #define MTK_DMA_SIZE_16DWORDS (2 << 4)
  155. #define MTK_RX_DMA_BUSY BIT(3)
  156. #define MTK_TX_DMA_BUSY BIT(1)
  157. #define MTK_RX_DMA_EN BIT(2)
  158. #define MTK_TX_DMA_EN BIT(0)
  159. #define MTK_DMA_BUSY_TIMEOUT HZ
  160. /* QDMA Reset Index Register */
  161. #define MTK_QDMA_RST_IDX 0x1A08
  162. /* QDMA Delay Interrupt Register */
  163. #define MTK_QDMA_DELAY_INT 0x1A0C
  164. /* QDMA Flow Control Register */
  165. #define MTK_QDMA_FC_THRES 0x1A10
  166. #define FC_THRES_DROP_MODE BIT(20)
  167. #define FC_THRES_DROP_EN (7 << 16)
  168. #define FC_THRES_MIN 0x4444
  169. /* QDMA Interrupt Status Register */
  170. #define MTK_QMTK_INT_STATUS 0x1A18
  171. #define MTK_RX_DONE_INT3 BIT(19)
  172. #define MTK_RX_DONE_INT2 BIT(18)
  173. #define MTK_RX_DONE_INT1 BIT(17)
  174. #define MTK_RX_DONE_INT0 BIT(16)
  175. #define MTK_TX_DONE_INT3 BIT(3)
  176. #define MTK_TX_DONE_INT2 BIT(2)
  177. #define MTK_TX_DONE_INT1 BIT(1)
  178. #define MTK_TX_DONE_INT0 BIT(0)
  179. #define MTK_RX_DONE_INT (MTK_RX_DONE_INT0 | MTK_RX_DONE_INT1 | \
  180. MTK_RX_DONE_INT2 | MTK_RX_DONE_INT3)
  181. #define MTK_TX_DONE_INT (MTK_TX_DONE_INT0 | MTK_TX_DONE_INT1 | \
  182. MTK_TX_DONE_INT2 | MTK_TX_DONE_INT3)
  183. /* QDMA Interrupt grouping registers */
  184. #define MTK_QDMA_INT_GRP1 0x1a20
  185. #define MTK_QDMA_INT_GRP2 0x1a24
  186. #define MTK_RLS_DONE_INT BIT(0)
  187. /* QDMA Interrupt Status Register */
  188. #define MTK_QDMA_INT_MASK 0x1A1C
  189. /* QDMA Interrupt Mask Register */
  190. #define MTK_QDMA_HRED2 0x1A44
  191. /* QDMA TX Forward CPU Pointer Register */
  192. #define MTK_QTX_CTX_PTR 0x1B00
  193. /* QDMA TX Forward DMA Pointer Register */
  194. #define MTK_QTX_DTX_PTR 0x1B04
  195. /* QDMA TX Release CPU Pointer Register */
  196. #define MTK_QTX_CRX_PTR 0x1B10
  197. /* QDMA TX Release DMA Pointer Register */
  198. #define MTK_QTX_DRX_PTR 0x1B14
  199. /* QDMA FQ Head Pointer Register */
  200. #define MTK_QDMA_FQ_HEAD 0x1B20
  201. /* QDMA FQ Head Pointer Register */
  202. #define MTK_QDMA_FQ_TAIL 0x1B24
  203. /* QDMA FQ Free Page Counter Register */
  204. #define MTK_QDMA_FQ_CNT 0x1B28
  205. /* QDMA FQ Free Page Buffer Length Register */
  206. #define MTK_QDMA_FQ_BLEN 0x1B2C
  207. /* GMA1 Received Good Byte Count Register */
  208. #define MTK_GDM1_TX_GBCNT 0x2400
  209. #define MTK_STAT_OFFSET 0x40
  210. /* QDMA descriptor txd4 */
  211. #define TX_DMA_CHKSUM (0x7 << 29)
  212. #define TX_DMA_TSO BIT(28)
  213. #define TX_DMA_FPORT_SHIFT 25
  214. #define TX_DMA_FPORT_MASK 0x7
  215. #define TX_DMA_INS_VLAN BIT(16)
  216. /* QDMA descriptor txd3 */
  217. #define TX_DMA_OWNER_CPU BIT(31)
  218. #define TX_DMA_LS0 BIT(30)
  219. #define TX_DMA_PLEN0(_x) (((_x) & MTK_TX_DMA_BUF_LEN) << 16)
  220. #define TX_DMA_SWC BIT(14)
  221. #define TX_DMA_SDL(_x) (((_x) & 0x3fff) << 16)
  222. /* QDMA descriptor rxd2 */
  223. #define RX_DMA_DONE BIT(31)
  224. #define RX_DMA_PLEN0(_x) (((_x) & 0x3fff) << 16)
  225. #define RX_DMA_GET_PLEN0(_x) (((_x) >> 16) & 0x3fff)
  226. /* QDMA descriptor rxd3 */
  227. #define RX_DMA_VID(_x) ((_x) & 0xfff)
  228. /* QDMA descriptor rxd4 */
  229. #define RX_DMA_L4_VALID BIT(24)
  230. #define RX_DMA_FPORT_SHIFT 19
  231. #define RX_DMA_FPORT_MASK 0x7
  232. /* PHY Indirect Access Control registers */
  233. #define MTK_PHY_IAC 0x10004
  234. #define PHY_IAC_ACCESS BIT(31)
  235. #define PHY_IAC_READ BIT(19)
  236. #define PHY_IAC_WRITE BIT(18)
  237. #define PHY_IAC_START BIT(16)
  238. #define PHY_IAC_ADDR_SHIFT 20
  239. #define PHY_IAC_REG_SHIFT 25
  240. #define PHY_IAC_TIMEOUT HZ
  241. /* Mac control registers */
  242. #define MTK_MAC_MCR(x) (0x10100 + (x * 0x100))
  243. #define MAC_MCR_MAX_RX_1536 BIT(24)
  244. #define MAC_MCR_IPG_CFG (BIT(18) | BIT(16))
  245. #define MAC_MCR_FORCE_MODE BIT(15)
  246. #define MAC_MCR_TX_EN BIT(14)
  247. #define MAC_MCR_RX_EN BIT(13)
  248. #define MAC_MCR_BACKOFF_EN BIT(9)
  249. #define MAC_MCR_BACKPR_EN BIT(8)
  250. #define MAC_MCR_FORCE_RX_FC BIT(5)
  251. #define MAC_MCR_FORCE_TX_FC BIT(4)
  252. #define MAC_MCR_SPEED_1000 BIT(3)
  253. #define MAC_MCR_SPEED_100 BIT(2)
  254. #define MAC_MCR_FORCE_DPX BIT(1)
  255. #define MAC_MCR_FORCE_LINK BIT(0)
  256. #define MAC_MCR_FIXED_LINK (MAC_MCR_MAX_RX_1536 | MAC_MCR_IPG_CFG | \
  257. MAC_MCR_FORCE_MODE | MAC_MCR_TX_EN | \
  258. MAC_MCR_RX_EN | MAC_MCR_BACKOFF_EN | \
  259. MAC_MCR_BACKPR_EN | MAC_MCR_FORCE_RX_FC | \
  260. MAC_MCR_FORCE_TX_FC | MAC_MCR_SPEED_1000 | \
  261. MAC_MCR_FORCE_DPX | MAC_MCR_FORCE_LINK)
  262. /* TRGMII RXC control register */
  263. #define TRGMII_RCK_CTRL 0x10300
  264. #define DQSI0(x) ((x << 0) & GENMASK(6, 0))
  265. #define DQSI1(x) ((x << 8) & GENMASK(14, 8))
  266. #define RXCTL_DMWTLAT(x) ((x << 16) & GENMASK(18, 16))
  267. #define RXC_DQSISEL BIT(30)
  268. #define RCK_CTRL_RGMII_1000 (RXC_DQSISEL | RXCTL_DMWTLAT(2) | DQSI1(16))
  269. #define RCK_CTRL_RGMII_10_100 RXCTL_DMWTLAT(2)
  270. /* TRGMII RXC control register */
  271. #define TRGMII_TCK_CTRL 0x10340
  272. #define TXCTL_DMWTLAT(x) ((x << 16) & GENMASK(18, 16))
  273. #define TXC_INV BIT(30)
  274. #define TCK_CTRL_RGMII_1000 TXCTL_DMWTLAT(2)
  275. #define TCK_CTRL_RGMII_10_100 (TXC_INV | TXCTL_DMWTLAT(2))
  276. /* TRGMII Interface mode register */
  277. #define INTF_MODE 0x10390
  278. #define TRGMII_INTF_DIS BIT(0)
  279. #define TRGMII_MODE BIT(1)
  280. #define TRGMII_CENTRAL_ALIGNED BIT(2)
  281. #define INTF_MODE_RGMII_1000 (TRGMII_MODE | TRGMII_CENTRAL_ALIGNED)
  282. #define INTF_MODE_RGMII_10_100 0
  283. /* GPIO port control registers for GMAC 2*/
  284. #define GPIO_OD33_CTRL8 0x4c0
  285. #define GPIO_BIAS_CTRL 0xed0
  286. #define GPIO_DRV_SEL10 0xf00
  287. /* ethernet subsystem chip id register */
  288. #define ETHSYS_CHIPID0_3 0x0
  289. #define ETHSYS_CHIPID4_7 0x4
  290. #define MT7623_ETH 7623
  291. /* ethernet subsystem config register */
  292. #define ETHSYS_SYSCFG0 0x14
  293. #define SYSCFG0_GE_MASK 0x3
  294. #define SYSCFG0_GE_MODE(x, y) (x << (12 + (y * 2)))
  295. /* ethernet subsystem clock register */
  296. #define ETHSYS_CLKCFG0 0x2c
  297. #define ETHSYS_TRGMII_CLK_SEL362_5 BIT(11)
  298. /* ethernet reset control register */
  299. #define ETHSYS_RSTCTRL 0x34
  300. #define RSTCTRL_FE BIT(6)
  301. #define RSTCTRL_PPE BIT(31)
  302. struct mtk_rx_dma {
  303. unsigned int rxd1;
  304. unsigned int rxd2;
  305. unsigned int rxd3;
  306. unsigned int rxd4;
  307. } __packed __aligned(4);
  308. struct mtk_tx_dma {
  309. unsigned int txd1;
  310. unsigned int txd2;
  311. unsigned int txd3;
  312. unsigned int txd4;
  313. } __packed __aligned(4);
  314. struct mtk_eth;
  315. struct mtk_mac;
  316. /* struct mtk_hw_stats - the structure that holds the traffic statistics.
  317. * @stats_lock: make sure that stats operations are atomic
  318. * @reg_offset: the status register offset of the SoC
  319. * @syncp: the refcount
  320. *
  321. * All of the supported SoCs have hardware counters for traffic statistics.
  322. * Whenever the status IRQ triggers we can read the latest stats from these
  323. * counters and store them in this struct.
  324. */
  325. struct mtk_hw_stats {
  326. u64 tx_bytes;
  327. u64 tx_packets;
  328. u64 tx_skip;
  329. u64 tx_collisions;
  330. u64 rx_bytes;
  331. u64 rx_packets;
  332. u64 rx_overflow;
  333. u64 rx_fcs_errors;
  334. u64 rx_short_errors;
  335. u64 rx_long_errors;
  336. u64 rx_checksum_errors;
  337. u64 rx_flow_control_packets;
  338. spinlock_t stats_lock;
  339. u32 reg_offset;
  340. struct u64_stats_sync syncp;
  341. };
  342. enum mtk_tx_flags {
  343. /* PDMA descriptor can point at 1-2 segments. This enum allows us to
  344. * track how memory was allocated so that it can be freed properly.
  345. */
  346. MTK_TX_FLAGS_SINGLE0 = 0x01,
  347. MTK_TX_FLAGS_PAGE0 = 0x02,
  348. /* MTK_TX_FLAGS_FPORTx allows tracking which port the transmitted
  349. * SKB out instead of looking up through hardware TX descriptor.
  350. */
  351. MTK_TX_FLAGS_FPORT0 = 0x04,
  352. MTK_TX_FLAGS_FPORT1 = 0x08,
  353. };
  354. /* This enum allows us to identify how the clock is defined on the array of the
  355. * clock in the order
  356. */
  357. enum mtk_clks_map {
  358. MTK_CLK_ETHIF,
  359. MTK_CLK_ESW,
  360. MTK_CLK_GP1,
  361. MTK_CLK_GP2,
  362. MTK_CLK_TRGPLL,
  363. MTK_CLK_MAX
  364. };
  365. enum mtk_dev_state {
  366. MTK_HW_INIT,
  367. MTK_RESETTING
  368. };
  369. /* struct mtk_tx_buf - This struct holds the pointers to the memory pointed at
  370. * by the TX descriptor s
  371. * @skb: The SKB pointer of the packet being sent
  372. * @dma_addr0: The base addr of the first segment
  373. * @dma_len0: The length of the first segment
  374. * @dma_addr1: The base addr of the second segment
  375. * @dma_len1: The length of the second segment
  376. */
  377. struct mtk_tx_buf {
  378. struct sk_buff *skb;
  379. u32 flags;
  380. DEFINE_DMA_UNMAP_ADDR(dma_addr0);
  381. DEFINE_DMA_UNMAP_LEN(dma_len0);
  382. DEFINE_DMA_UNMAP_ADDR(dma_addr1);
  383. DEFINE_DMA_UNMAP_LEN(dma_len1);
  384. };
  385. /* struct mtk_tx_ring - This struct holds info describing a TX ring
  386. * @dma: The descriptor ring
  387. * @buf: The memory pointed at by the ring
  388. * @phys: The physical addr of tx_buf
  389. * @next_free: Pointer to the next free descriptor
  390. * @last_free: Pointer to the last free descriptor
  391. * @thresh: The threshold of minimum amount of free descriptors
  392. * @free_count: QDMA uses a linked list. Track how many free descriptors
  393. * are present
  394. */
  395. struct mtk_tx_ring {
  396. struct mtk_tx_dma *dma;
  397. struct mtk_tx_buf *buf;
  398. dma_addr_t phys;
  399. struct mtk_tx_dma *next_free;
  400. struct mtk_tx_dma *last_free;
  401. u16 thresh;
  402. atomic_t free_count;
  403. };
  404. /* PDMA rx ring mode */
  405. enum mtk_rx_flags {
  406. MTK_RX_FLAGS_NORMAL = 0,
  407. MTK_RX_FLAGS_HWLRO,
  408. };
  409. /* struct mtk_rx_ring - This struct holds info describing a RX ring
  410. * @dma: The descriptor ring
  411. * @data: The memory pointed at by the ring
  412. * @phys: The physical addr of rx_buf
  413. * @frag_size: How big can each fragment be
  414. * @buf_size: The size of each packet buffer
  415. * @calc_idx: The current head of ring
  416. */
  417. struct mtk_rx_ring {
  418. struct mtk_rx_dma *dma;
  419. u8 **data;
  420. dma_addr_t phys;
  421. u16 frag_size;
  422. u16 buf_size;
  423. u16 dma_size;
  424. bool calc_idx_update;
  425. u16 calc_idx;
  426. u32 crx_idx_reg;
  427. };
  428. /* currently no SoC has more than 2 macs */
  429. #define MTK_MAX_DEVS 2
  430. /* struct mtk_eth - This is the main datasructure for holding the state
  431. * of the driver
  432. * @dev: The device pointer
  433. * @base: The mapped register i/o base
  434. * @page_lock: Make sure that register operations are atomic
  435. * @dummy_dev: we run 2 netdevs on 1 physical DMA ring and need a
  436. * dummy for NAPI to work
  437. * @netdev: The netdev instances
  438. * @mac: Each netdev is linked to a physical MAC
  439. * @irq: The IRQ that we are using
  440. * @msg_enable: Ethtool msg level
  441. * @ethsys: The register map pointing at the range used to setup
  442. * MII modes
  443. * @pctl: The register map pointing at the range used to setup
  444. * GMAC port drive/slew values
  445. * @dma_refcnt: track how many netdevs are using the DMA engine
  446. * @tx_ring: Pointer to the memore holding info about the TX ring
  447. * @rx_ring: Pointer to the memore holding info about the RX ring
  448. * @tx_napi: The TX NAPI struct
  449. * @rx_napi: The RX NAPI struct
  450. * @scratch_ring: Newer SoCs need memory for a second HW managed TX ring
  451. * @phy_scratch_ring: physical address of scratch_ring
  452. * @scratch_head: The scratch memory that scratch_ring points to.
  453. * @clks: clock array for all clocks required
  454. * @mii_bus: If there is a bus we need to create an instance for it
  455. * @pending_work: The workqueue used to reset the dma ring
  456. * @state Initialization and runtime state of the device.
  457. */
  458. struct mtk_eth {
  459. struct device *dev;
  460. void __iomem *base;
  461. spinlock_t page_lock;
  462. spinlock_t irq_lock;
  463. struct net_device dummy_dev;
  464. struct net_device *netdev[MTK_MAX_DEVS];
  465. struct mtk_mac *mac[MTK_MAX_DEVS];
  466. int irq[3];
  467. u32 msg_enable;
  468. unsigned long sysclk;
  469. struct regmap *ethsys;
  470. struct regmap *pctl;
  471. u32 chip_id;
  472. bool hwlro;
  473. atomic_t dma_refcnt;
  474. struct mtk_tx_ring tx_ring;
  475. struct mtk_rx_ring rx_ring[MTK_MAX_RX_RING_NUM];
  476. struct napi_struct tx_napi;
  477. struct napi_struct rx_napi;
  478. struct mtk_tx_dma *scratch_ring;
  479. dma_addr_t phy_scratch_ring;
  480. void *scratch_head;
  481. struct clk *clks[MTK_CLK_MAX];
  482. struct mii_bus *mii_bus;
  483. struct work_struct pending_work;
  484. unsigned long state;
  485. };
  486. /* struct mtk_mac - the structure that holds the info about the MACs of the
  487. * SoC
  488. * @id: The number of the MAC
  489. * @ge_mode: Interface mode kept for setup restoring
  490. * @of_node: Our devicetree node
  491. * @hw: Backpointer to our main datastruture
  492. * @hw_stats: Packet statistics counter
  493. * @trgmii Indicate if the MAC uses TRGMII connected to internal
  494. switch
  495. */
  496. struct mtk_mac {
  497. int id;
  498. int ge_mode;
  499. struct device_node *of_node;
  500. struct mtk_eth *hw;
  501. struct mtk_hw_stats *hw_stats;
  502. __be32 hwlro_ip[MTK_MAX_LRO_IP_CNT];
  503. int hwlro_ip_cnt;
  504. bool trgmii;
  505. };
  506. /* the struct describing the SoC. these are declared in the soc_xyz.c files */
  507. extern const struct of_device_id of_mtk_match[];
  508. /* read the hardware status register */
  509. void mtk_stats_update_mac(struct mtk_mac *mac);
  510. void mtk_w32(struct mtk_eth *eth, u32 val, unsigned reg);
  511. u32 mtk_r32(struct mtk_eth *eth, unsigned reg);
  512. #endif /* MTK_ETH_H */