mtk_eth_soc.h 21 KB

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