mtk_eth_soc.h 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. *
  4. * Copyright (C) 2009-2016 John Crispin <blogic@openwrt.org>
  5. * Copyright (C) 2009-2016 Felix Fietkau <nbd@openwrt.org>
  6. * Copyright (C) 2013-2016 Michael Lee <igvtee@gmail.com>
  7. */
  8. #ifndef MTK_ETH_H
  9. #define MTK_ETH_H
  10. #include <linux/dma-mapping.h>
  11. #include <linux/netdevice.h>
  12. #include <linux/of_net.h>
  13. #include <linux/u64_stats_sync.h>
  14. #include <linux/refcount.h>
  15. #include <linux/phylink.h>
  16. #include <linux/rhashtable.h>
  17. #include <linux/dim.h>
  18. #include <linux/bitfield.h>
  19. #include <net/page_pool/types.h>
  20. #include <linux/bpf_trace.h>
  21. #include "mtk_ppe.h"
  22. #define MTK_MAX_DSA_PORTS 7
  23. #define MTK_DSA_PORT_MASK GENMASK(2, 0)
  24. #define MTK_QDMA_NUM_QUEUES 16
  25. #define MTK_QDMA_PAGE_SIZE 2048
  26. #define MTK_MAX_RX_LENGTH 1536
  27. #define MTK_MAX_RX_LENGTH_2K 2048
  28. #define MTK_TX_DMA_BUF_LEN 0x3fff
  29. #define MTK_TX_DMA_BUF_LEN_V2 0xffff
  30. #define MTK_QDMA_RING_SIZE 2048
  31. #define MTK_DMA_SIZE(x) (SZ_##x)
  32. #define MTK_FQ_DMA_HEAD 32
  33. #define MTK_FQ_DMA_LENGTH 2048
  34. #define MTK_RX_ETH_HLEN (ETH_HLEN + ETH_FCS_LEN)
  35. #define MTK_RX_HLEN (NET_SKB_PAD + MTK_RX_ETH_HLEN + NET_IP_ALIGN)
  36. #define MTK_DMA_DUMMY_DESC 0xffffffff
  37. #define MTK_DEFAULT_MSG_ENABLE (NETIF_MSG_DRV | \
  38. NETIF_MSG_PROBE | \
  39. NETIF_MSG_LINK | \
  40. NETIF_MSG_TIMER | \
  41. NETIF_MSG_IFDOWN | \
  42. NETIF_MSG_IFUP | \
  43. NETIF_MSG_RX_ERR | \
  44. NETIF_MSG_TX_ERR)
  45. #define MTK_HW_FEATURES (NETIF_F_IP_CSUM | \
  46. NETIF_F_RXCSUM | \
  47. NETIF_F_HW_VLAN_CTAG_TX | \
  48. NETIF_F_SG | NETIF_F_TSO | \
  49. NETIF_F_TSO6 | \
  50. NETIF_F_IPV6_CSUM |\
  51. NETIF_F_HW_TC)
  52. #define MTK_HW_FEATURES_MT7628 (NETIF_F_SG | NETIF_F_RXCSUM)
  53. #define NEXT_DESP_IDX(X, Y) (((X) + 1) & ((Y) - 1))
  54. #define MTK_PP_HEADROOM XDP_PACKET_HEADROOM
  55. #define MTK_PP_PAD (MTK_PP_HEADROOM + \
  56. SKB_DATA_ALIGN(sizeof(struct skb_shared_info)))
  57. #define MTK_PP_MAX_BUF_SIZE (PAGE_SIZE - MTK_PP_PAD)
  58. #define MTK_QRX_OFFSET 0x10
  59. #define MTK_MAX_RX_RING_NUM 4
  60. #define MTK_HW_LRO_DMA_SIZE 8
  61. #define MTK_MAX_LRO_RX_LENGTH (4096 * 3)
  62. #define MTK_MAX_LRO_IP_CNT 2
  63. #define MTK_HW_LRO_TIMER_UNIT 1 /* 20 us */
  64. #define MTK_HW_LRO_REFRESH_TIME 50000 /* 1 sec. */
  65. #define MTK_HW_LRO_AGG_TIME 10 /* 200us */
  66. #define MTK_HW_LRO_AGE_TIME 50 /* 1ms */
  67. #define MTK_HW_LRO_MAX_AGG_CNT 64
  68. #define MTK_HW_LRO_BW_THRE 3000
  69. #define MTK_HW_LRO_REPLACE_DELTA 1000
  70. #define MTK_HW_LRO_SDL_REMAIN_ROOM 1522
  71. /* Frame Engine Global Configuration */
  72. #define MTK_FE_GLO_CFG(x) (((x) == MTK_GMAC3_ID) ? 0x24 : 0x00)
  73. #define MTK_FE_LINK_DOWN_P(x) BIT(((x) + 8) % 16)
  74. /* Frame Engine Global Reset Register */
  75. #define MTK_RST_GL 0x04
  76. #define RST_GL_PSE BIT(0)
  77. /* Frame Engine Interrupt Status Register */
  78. #define MTK_INT_STATUS2 0x08
  79. #define MTK_FE_INT_ENABLE 0x0c
  80. #define MTK_FE_INT_FQ_EMPTY BIT(8)
  81. #define MTK_FE_INT_TSO_FAIL BIT(12)
  82. #define MTK_FE_INT_TSO_ILLEGAL BIT(13)
  83. #define MTK_FE_INT_TSO_ALIGN BIT(14)
  84. #define MTK_FE_INT_RFIFO_OV BIT(18)
  85. #define MTK_FE_INT_RFIFO_UF BIT(19)
  86. #define MTK_GDM1_AF BIT(28)
  87. #define MTK_GDM2_AF BIT(29)
  88. /* PDMA HW LRO Alter Flow Timer Register */
  89. #define MTK_PDMA_LRO_ALT_REFRESH_TIMER 0x1c
  90. /* Frame Engine Interrupt Grouping Register */
  91. #define MTK_FE_INT_GRP 0x20
  92. /* CDMP Ingress Control Register */
  93. #define MTK_CDMQ_IG_CTRL 0x1400
  94. #define MTK_CDMQ_STAG_EN BIT(0)
  95. /* CDMQ Exgress Control Register */
  96. #define MTK_CDMQ_EG_CTRL 0x1404
  97. /* CDMP Ingress Control Register */
  98. #define MTK_CDMP_IG_CTRL 0x400
  99. #define MTK_CDMP_STAG_EN BIT(0)
  100. /* CDMP Exgress Control Register */
  101. #define MTK_CDMP_EG_CTRL 0x404
  102. /* GDM Exgress Control Register */
  103. #define MTK_GDMA_FWD_CFG(x) ({ typeof(x) _x = (x); (_x == MTK_GMAC3_ID) ? \
  104. 0x540 : 0x500 + (_x * 0x1000); })
  105. #define MTK_GDMA_SPECIAL_TAG BIT(24)
  106. #define MTK_GDMA_ICS_EN BIT(22)
  107. #define MTK_GDMA_TCS_EN BIT(21)
  108. #define MTK_GDMA_UCS_EN BIT(20)
  109. #define MTK_GDMA_STRP_CRC BIT(16)
  110. #define MTK_GDMA_TO_PDMA 0x0
  111. #define MTK_GDMA_DROP_ALL 0x7777
  112. /* GDM Egress Control Register */
  113. #define MTK_GDMA_EG_CTRL(x) ({ typeof(x) _x = (x); (_x == MTK_GMAC3_ID) ? \
  114. 0x544 : 0x504 + (_x * 0x1000); })
  115. #define MTK_GDMA_XGDM_SEL BIT(31)
  116. /* Unicast Filter MAC Address Register - Low */
  117. #define MTK_GDMA_MAC_ADRL(x) ({ typeof(x) _x = (x); (_x == MTK_GMAC3_ID) ? \
  118. 0x548 : 0x508 + (_x * 0x1000); })
  119. /* Unicast Filter MAC Address Register - High */
  120. #define MTK_GDMA_MAC_ADRH(x) ({ typeof(x) _x = (x); (_x == MTK_GMAC3_ID) ? \
  121. 0x54C : 0x50C + (_x * 0x1000); })
  122. /* legacy DT support for internal SRAM */
  123. #define MTK_ETH_SRAM_OFFSET 0x40000
  124. #define MTK_ETH_SRAM_GRANULARITY 32
  125. #define MTK_ETH_NETSYS_V2_SRAM_SIZE 0x40000
  126. /* FE global misc reg*/
  127. #define MTK_FE_GLO_MISC 0x124
  128. /* PSE Free Queue Flow Control */
  129. #define PSE_FQFC_CFG1 0x100
  130. #define PSE_FQFC_CFG2 0x104
  131. #define PSE_DROP_CFG 0x108
  132. #define PSE_PPE_DROP(x) (0x110 + ((x) * 0x4))
  133. /* PSE Last FreeQ Page Request Control */
  134. #define PSE_DUMY_REQ 0x10C
  135. /* PSE_DUMY_REQ is not a typo but actually called like that also in
  136. * MediaTek's datasheet
  137. */
  138. #define PSE_DUMMY_WORK_GDM(x) BIT(16 + (x))
  139. #define DUMMY_PAGE_THR 0x1
  140. /* PSE Input Queue Reservation Register*/
  141. #define PSE_IQ_REV(x) (0x140 + (((x) - 1) << 2))
  142. /* PSE Output Queue Threshold Register*/
  143. #define PSE_OQ_TH(x) (0x160 + (((x) - 1) << 2))
  144. /* GDM and CDM Threshold */
  145. #define MTK_GDM2_THRES 0x1530
  146. #define MTK_CDMW0_THRES 0x164c
  147. #define MTK_CDMW1_THRES 0x1650
  148. #define MTK_CDME0_THRES 0x1654
  149. #define MTK_CDME1_THRES 0x1658
  150. #define MTK_CDMM_THRES 0x165c
  151. /* PDMA HW LRO Control Registers */
  152. #define MTK_PDMA_LRO_CTRL_DW0 0x980
  153. #define MTK_LRO_EN BIT(0)
  154. #define MTK_L3_CKS_UPD_EN BIT(7)
  155. #define MTK_L3_CKS_UPD_EN_V2 BIT(19)
  156. #define MTK_LRO_ALT_PKT_CNT_MODE BIT(21)
  157. #define MTK_LRO_RING_RELINQUISH_REQ (0x7 << 26)
  158. #define MTK_LRO_RING_RELINQUISH_REQ_V2 (0xf << 24)
  159. #define MTK_LRO_RING_RELINQUISH_DONE (0x7 << 29)
  160. #define MTK_LRO_RING_RELINQUISH_DONE_V2 (0xf << 28)
  161. #define MTK_PDMA_LRO_CTRL_DW1 0x984
  162. #define MTK_PDMA_LRO_CTRL_DW2 0x988
  163. #define MTK_PDMA_LRO_CTRL_DW3 0x98c
  164. #define MTK_ADMA_MODE BIT(15)
  165. #define MTK_LRO_MIN_RXD_SDL (MTK_HW_LRO_SDL_REMAIN_ROOM << 16)
  166. #define MTK_RX_DMA_LRO_EN BIT(8)
  167. #define MTK_MULTI_EN BIT(10)
  168. #define MTK_PDMA_SIZE_8DWORDS (1 << 4)
  169. /* PDMA Global Configuration Register */
  170. #define MTK_PDMA_LRO_SDL 0x3000
  171. #define MTK_RX_CFG_SDL_OFFSET 16
  172. /* PDMA Reset Index Register */
  173. #define MTK_PST_DRX_IDX0 BIT(16)
  174. #define MTK_PST_DRX_IDX_CFG(x) (MTK_PST_DRX_IDX0 << (x))
  175. /* PDMA Delay Interrupt Register */
  176. #define MTK_PDMA_DELAY_RX_MASK GENMASK(15, 0)
  177. #define MTK_PDMA_DELAY_RX_EN BIT(15)
  178. #define MTK_PDMA_DELAY_RX_PINT_SHIFT 8
  179. #define MTK_PDMA_DELAY_RX_PTIME_SHIFT 0
  180. #define MTK_PDMA_DELAY_TX_MASK GENMASK(31, 16)
  181. #define MTK_PDMA_DELAY_TX_EN BIT(31)
  182. #define MTK_PDMA_DELAY_TX_PINT_SHIFT 24
  183. #define MTK_PDMA_DELAY_TX_PTIME_SHIFT 16
  184. #define MTK_PDMA_DELAY_PINT_MASK 0x7f
  185. #define MTK_PDMA_DELAY_PTIME_MASK 0xff
  186. /* PDMA HW LRO Alter Flow Delta Register */
  187. #define MTK_PDMA_LRO_ALT_SCORE_DELTA 0xa4c
  188. /* PDMA HW LRO IP Setting Registers */
  189. #define MTK_LRO_RX_RING0_DIP_DW0 0xb04
  190. #define MTK_LRO_DIP_DW0_CFG(x) (MTK_LRO_RX_RING0_DIP_DW0 + (x * 0x40))
  191. #define MTK_RING_MYIP_VLD BIT(9)
  192. /* PDMA HW LRO Ring Control Registers */
  193. #define MTK_LRO_RX_RING0_CTRL_DW1 0xb28
  194. #define MTK_LRO_RX_RING0_CTRL_DW2 0xb2c
  195. #define MTK_LRO_RX_RING0_CTRL_DW3 0xb30
  196. #define MTK_LRO_CTRL_DW1_CFG(x) (MTK_LRO_RX_RING0_CTRL_DW1 + (x * 0x40))
  197. #define MTK_LRO_CTRL_DW2_CFG(x) (MTK_LRO_RX_RING0_CTRL_DW2 + (x * 0x40))
  198. #define MTK_LRO_CTRL_DW3_CFG(x) (MTK_LRO_RX_RING0_CTRL_DW3 + (x * 0x40))
  199. #define MTK_RING_AGE_TIME_L ((MTK_HW_LRO_AGE_TIME & 0x3ff) << 22)
  200. #define MTK_RING_AGE_TIME_H ((MTK_HW_LRO_AGE_TIME >> 10) & 0x3f)
  201. #define MTK_RING_AUTO_LERAN_MODE (3 << 6)
  202. #define MTK_RING_VLD BIT(8)
  203. #define MTK_RING_MAX_AGG_TIME ((MTK_HW_LRO_AGG_TIME & 0xffff) << 10)
  204. #define MTK_RING_MAX_AGG_CNT_L ((MTK_HW_LRO_MAX_AGG_CNT & 0x3f) << 26)
  205. #define MTK_RING_MAX_AGG_CNT_H ((MTK_HW_LRO_MAX_AGG_CNT >> 6) & 0x3)
  206. /* QDMA TX Queue Configuration Registers */
  207. #define MTK_QTX_OFFSET 0x10
  208. #define QDMA_RES_THRES 4
  209. /* QDMA Tx Queue Scheduler Configuration Registers */
  210. #define MTK_QTX_SCH_TX_SEL BIT(31)
  211. #define MTK_QTX_SCH_TX_SEL_V2 GENMASK(31, 30)
  212. #define MTK_QTX_SCH_LEAKY_BUCKET_EN BIT(30)
  213. #define MTK_QTX_SCH_LEAKY_BUCKET_SIZE GENMASK(29, 28)
  214. #define MTK_QTX_SCH_MIN_RATE_EN BIT(27)
  215. #define MTK_QTX_SCH_MIN_RATE_MAN GENMASK(26, 20)
  216. #define MTK_QTX_SCH_MIN_RATE_EXP GENMASK(19, 16)
  217. #define MTK_QTX_SCH_MAX_RATE_WEIGHT GENMASK(15, 12)
  218. #define MTK_QTX_SCH_MAX_RATE_EN BIT(11)
  219. #define MTK_QTX_SCH_MAX_RATE_MAN GENMASK(10, 4)
  220. #define MTK_QTX_SCH_MAX_RATE_EXP GENMASK(3, 0)
  221. /* QDMA TX Scheduler Rate Control Register */
  222. #define MTK_QDMA_TX_SCH_MAX_WFQ BIT(15)
  223. /* QDMA Global Configuration Register */
  224. #define MTK_RX_2B_OFFSET BIT(31)
  225. #define MTK_RX_BT_32DWORDS (3 << 11)
  226. #define MTK_NDP_CO_PRO BIT(10)
  227. #define MTK_TX_WB_DDONE BIT(6)
  228. #define MTK_TX_BT_32DWORDS (3 << 4)
  229. #define MTK_RX_DMA_BUSY BIT(3)
  230. #define MTK_TX_DMA_BUSY BIT(1)
  231. #define MTK_RX_DMA_EN BIT(2)
  232. #define MTK_TX_DMA_EN BIT(0)
  233. #define MTK_DMA_BUSY_TIMEOUT_US 1000000
  234. /* QDMA V2 Global Configuration Register */
  235. #define MTK_CHK_DDONE_EN BIT(28)
  236. #define MTK_DMAD_WR_WDONE BIT(26)
  237. #define MTK_WCOMP_EN BIT(24)
  238. #define MTK_RESV_BUF (0x40 << 16)
  239. #define MTK_MUTLI_CNT (0x4 << 12)
  240. #define MTK_LEAKY_BUCKET_EN BIT(11)
  241. /* QDMA Flow Control Register */
  242. #define FC_THRES_DROP_MODE BIT(20)
  243. #define FC_THRES_DROP_EN (7 << 16)
  244. #define FC_THRES_MIN 0x4444
  245. /* QDMA Interrupt Status Register */
  246. #define MTK_RX_DONE_DLY BIT(30)
  247. #define MTK_TX_DONE_DLY BIT(28)
  248. #define MTK_RX_DONE_INT3 BIT(19)
  249. #define MTK_RX_DONE_INT2 BIT(18)
  250. #define MTK_RX_DONE_INT1 BIT(17)
  251. #define MTK_RX_DONE_INT0 BIT(16)
  252. #define MTK_TX_DONE_INT3 BIT(3)
  253. #define MTK_TX_DONE_INT2 BIT(2)
  254. #define MTK_TX_DONE_INT1 BIT(1)
  255. #define MTK_TX_DONE_INT0 BIT(0)
  256. #define MTK_RX_DONE_INT MTK_RX_DONE_DLY
  257. #define MTK_TX_DONE_INT MTK_TX_DONE_DLY
  258. #define MTK_RX_DONE_INT_V2 BIT(14)
  259. #define MTK_CDM_TXFIFO_RDY BIT(7)
  260. /* QDMA Interrupt grouping registers */
  261. #define MTK_RLS_DONE_INT BIT(0)
  262. /* QDMA TX NUM */
  263. #define QID_BITS_V2(x) (((x) & 0x3f) << 16)
  264. #define MTK_QDMA_GMAC2_QID 8
  265. #define MTK_TX_DMA_BUF_SHIFT 8
  266. /* QDMA V2 descriptor txd6 */
  267. #define TX_DMA_INS_VLAN_V2 BIT(16)
  268. /* QDMA V2 descriptor txd5 */
  269. #define TX_DMA_CHKSUM_V2 (0x7 << 28)
  270. #define TX_DMA_TSO_V2 BIT(31)
  271. #define TX_DMA_SPTAG_V3 BIT(27)
  272. /* QDMA V2 descriptor txd4 */
  273. #define TX_DMA_FPORT_SHIFT_V2 8
  274. #define TX_DMA_FPORT_MASK_V2 0xf
  275. #define TX_DMA_SWC_V2 BIT(30)
  276. /* QDMA descriptor txd4 */
  277. #define TX_DMA_CHKSUM (0x7 << 29)
  278. #define TX_DMA_TSO BIT(28)
  279. #define TX_DMA_FPORT_SHIFT 25
  280. #define TX_DMA_FPORT_MASK 0x7
  281. #define TX_DMA_INS_VLAN BIT(16)
  282. /* QDMA descriptor txd3 */
  283. #define TX_DMA_OWNER_CPU BIT(31)
  284. #define TX_DMA_LS0 BIT(30)
  285. #define TX_DMA_PLEN0(x) (((x) & eth->soc->tx.dma_max_len) << eth->soc->tx.dma_len_offset)
  286. #define TX_DMA_PLEN1(x) ((x) & eth->soc->tx.dma_max_len)
  287. #define TX_DMA_SWC BIT(14)
  288. #define TX_DMA_PQID GENMASK(3, 0)
  289. #define TX_DMA_ADDR64_MASK GENMASK(3, 0)
  290. #if IS_ENABLED(CONFIG_64BIT)
  291. # define TX_DMA_GET_ADDR64(x) (((u64)FIELD_GET(TX_DMA_ADDR64_MASK, (x))) << 32)
  292. # define TX_DMA_PREP_ADDR64(x) FIELD_PREP(TX_DMA_ADDR64_MASK, ((x) >> 32))
  293. #else
  294. # define TX_DMA_GET_ADDR64(x) (0)
  295. # define TX_DMA_PREP_ADDR64(x) (0)
  296. #endif
  297. /* PDMA on MT7628 */
  298. #define TX_DMA_DONE BIT(31)
  299. #define TX_DMA_LS1 BIT(14)
  300. #define TX_DMA_DESP2_DEF (TX_DMA_LS0 | TX_DMA_DONE)
  301. /* QDMA descriptor rxd2 */
  302. #define RX_DMA_DONE BIT(31)
  303. #define RX_DMA_LSO BIT(30)
  304. #define RX_DMA_PREP_PLEN0(x) (((x) & eth->soc->rx.dma_max_len) << eth->soc->rx.dma_len_offset)
  305. #define RX_DMA_GET_PLEN0(x) (((x) >> eth->soc->rx.dma_len_offset) & eth->soc->rx.dma_max_len)
  306. #define RX_DMA_VTAG BIT(15)
  307. #define RX_DMA_ADDR64_MASK GENMASK(3, 0)
  308. #if IS_ENABLED(CONFIG_64BIT)
  309. # define RX_DMA_GET_ADDR64(x) (((u64)FIELD_GET(RX_DMA_ADDR64_MASK, (x))) << 32)
  310. # define RX_DMA_PREP_ADDR64(x) FIELD_PREP(RX_DMA_ADDR64_MASK, ((x) >> 32))
  311. #else
  312. # define RX_DMA_GET_ADDR64(x) (0)
  313. # define RX_DMA_PREP_ADDR64(x) (0)
  314. #endif
  315. /* QDMA descriptor rxd3 */
  316. #define RX_DMA_VID(x) ((x) & VLAN_VID_MASK)
  317. #define RX_DMA_TCI(x) ((x) & (VLAN_PRIO_MASK | VLAN_VID_MASK))
  318. #define RX_DMA_VPID(x) (((x) >> 16) & 0xffff)
  319. /* QDMA descriptor rxd4 */
  320. #define MTK_RXD4_FOE_ENTRY GENMASK(13, 0)
  321. #define MTK_RXD4_PPE_CPU_REASON GENMASK(18, 14)
  322. #define MTK_RXD4_SRC_PORT GENMASK(21, 19)
  323. #define MTK_RXD4_ALG GENMASK(31, 22)
  324. /* QDMA descriptor rxd4 */
  325. #define RX_DMA_L4_VALID BIT(24)
  326. #define RX_DMA_L4_VALID_PDMA BIT(30) /* when PDMA is used */
  327. #define RX_DMA_SPECIAL_TAG BIT(22)
  328. /* PDMA descriptor rxd5 */
  329. #define MTK_RXD5_FOE_ENTRY GENMASK(14, 0)
  330. #define MTK_RXD5_PPE_CPU_REASON GENMASK(22, 18)
  331. #define MTK_RXD5_SRC_PORT GENMASK(29, 26)
  332. #define RX_DMA_GET_SPORT(x) (((x) >> 19) & 0x7)
  333. #define RX_DMA_GET_SPORT_V2(x) (((x) >> 26) & 0xf)
  334. /* PDMA V2 descriptor rxd3 */
  335. #define RX_DMA_VTAG_V2 BIT(0)
  336. #define RX_DMA_L4_VALID_V2 BIT(2)
  337. /* PHY Polling and SMI Master Control registers */
  338. #define MTK_PPSC 0x10000
  339. #define PPSC_MDC_CFG GENMASK(29, 24)
  340. #define PPSC_MDC_TURBO BIT(20)
  341. #define MDC_MAX_FREQ 25000000
  342. #define MDC_MAX_DIVIDER 63
  343. /* PHY Indirect Access Control registers */
  344. #define MTK_PHY_IAC 0x10004
  345. #define PHY_IAC_ACCESS BIT(31)
  346. #define PHY_IAC_REG_MASK GENMASK(29, 25)
  347. #define PHY_IAC_REG(x) FIELD_PREP(PHY_IAC_REG_MASK, (x))
  348. #define PHY_IAC_ADDR_MASK GENMASK(24, 20)
  349. #define PHY_IAC_ADDR(x) FIELD_PREP(PHY_IAC_ADDR_MASK, (x))
  350. #define PHY_IAC_CMD_MASK GENMASK(19, 18)
  351. #define PHY_IAC_CMD_C45_ADDR FIELD_PREP(PHY_IAC_CMD_MASK, 0)
  352. #define PHY_IAC_CMD_WRITE FIELD_PREP(PHY_IAC_CMD_MASK, 1)
  353. #define PHY_IAC_CMD_C22_READ FIELD_PREP(PHY_IAC_CMD_MASK, 2)
  354. #define PHY_IAC_CMD_C45_READ FIELD_PREP(PHY_IAC_CMD_MASK, 3)
  355. #define PHY_IAC_START_MASK GENMASK(17, 16)
  356. #define PHY_IAC_START_C45 FIELD_PREP(PHY_IAC_START_MASK, 0)
  357. #define PHY_IAC_START_C22 FIELD_PREP(PHY_IAC_START_MASK, 1)
  358. #define PHY_IAC_DATA_MASK GENMASK(15, 0)
  359. #define PHY_IAC_DATA(x) FIELD_PREP(PHY_IAC_DATA_MASK, (x))
  360. #define PHY_IAC_TIMEOUT HZ
  361. #define MTK_MAC_MISC 0x1000c
  362. #define MTK_MAC_MISC_V3 0x10010
  363. #define MTK_MUX_TO_ESW BIT(0)
  364. #define MISC_MDC_TURBO BIT(4)
  365. /* XMAC status registers */
  366. #define MTK_XGMAC_STS(x) (((x) == MTK_GMAC3_ID) ? 0x1001C : 0x1000C)
  367. #define MTK_XGMAC_FORCE_MODE(x) (((x) == MTK_GMAC2_ID) ? BIT(31) : BIT(15))
  368. #define MTK_XGMAC_FORCE_LINK(x) (((x) == MTK_GMAC2_ID) ? BIT(27) : BIT(11))
  369. #define MTK_USXGMII_PCS_LINK BIT(8)
  370. #define MTK_XGMAC_RX_FC BIT(5)
  371. #define MTK_XGMAC_TX_FC BIT(4)
  372. #define MTK_USXGMII_PCS_MODE GENMASK(3, 1)
  373. #define MTK_XGMAC_LINK_STS BIT(0)
  374. /* GSW bridge registers */
  375. #define MTK_GSW_CFG (0x10080)
  376. #define GSWTX_IPG_MASK GENMASK(19, 16)
  377. #define GSWTX_IPG_SHIFT 16
  378. #define GSWRX_IPG_MASK GENMASK(3, 0)
  379. #define GSWRX_IPG_SHIFT 0
  380. #define GSW_IPG_11 11
  381. /* Mac control registers */
  382. #define MTK_MAC_MCR(x) (0x10100 + (x * 0x100))
  383. #define MAC_MCR_MAX_RX_MASK GENMASK(25, 24)
  384. #define MAC_MCR_MAX_RX(_x) (MAC_MCR_MAX_RX_MASK & ((_x) << 24))
  385. #define MAC_MCR_MAX_RX_1518 0x0
  386. #define MAC_MCR_MAX_RX_1536 0x1
  387. #define MAC_MCR_MAX_RX_1552 0x2
  388. #define MAC_MCR_MAX_RX_2048 0x3
  389. #define MAC_MCR_IPG_CFG (BIT(18) | BIT(16))
  390. #define MAC_MCR_FORCE_MODE BIT(15)
  391. #define MAC_MCR_TX_EN BIT(14)
  392. #define MAC_MCR_RX_EN BIT(13)
  393. #define MAC_MCR_RX_FIFO_CLR_DIS BIT(12)
  394. #define MAC_MCR_BACKOFF_EN BIT(9)
  395. #define MAC_MCR_BACKPR_EN BIT(8)
  396. #define MAC_MCR_EEE1G BIT(7)
  397. #define MAC_MCR_EEE100M BIT(6)
  398. #define MAC_MCR_FORCE_RX_FC BIT(5)
  399. #define MAC_MCR_FORCE_TX_FC BIT(4)
  400. #define MAC_MCR_SPEED_1000 BIT(3)
  401. #define MAC_MCR_SPEED_100 BIT(2)
  402. #define MAC_MCR_FORCE_DPX BIT(1)
  403. #define MAC_MCR_FORCE_LINK BIT(0)
  404. #define MAC_MCR_FORCE_LINK_DOWN (MAC_MCR_FORCE_MODE)
  405. /* Mac EEE control registers */
  406. #define MTK_MAC_EEECR(x) (0x10104 + (x * 0x100))
  407. #define MAC_EEE_WAKEUP_TIME_1000 GENMASK(31, 24)
  408. #define MAC_EEE_WAKEUP_TIME_100 GENMASK(23, 16)
  409. #define MAC_EEE_LPI_TXIDLE_THD GENMASK(15, 8)
  410. #define MAC_EEE_CKG_TXIDLE BIT(3)
  411. #define MAC_EEE_CKG_RXLPI BIT(2)
  412. #define MAC_EEE_LPI_MODE BIT(0)
  413. /* Mac status registers */
  414. #define MTK_MAC_MSR(x) (0x10108 + (x * 0x100))
  415. #define MAC_MSR_EEE1G BIT(7)
  416. #define MAC_MSR_EEE100M BIT(6)
  417. #define MAC_MSR_RX_FC BIT(5)
  418. #define MAC_MSR_TX_FC BIT(4)
  419. #define MAC_MSR_SPEED_1000 BIT(3)
  420. #define MAC_MSR_SPEED_100 BIT(2)
  421. #define MAC_MSR_SPEED_MASK (MAC_MSR_SPEED_1000 | MAC_MSR_SPEED_100)
  422. #define MAC_MSR_DPX BIT(1)
  423. #define MAC_MSR_LINK BIT(0)
  424. /* TRGMII RXC control register */
  425. #define TRGMII_RCK_CTRL 0x10300
  426. #define DQSI0(x) ((x << 0) & GENMASK(6, 0))
  427. #define DQSI1(x) ((x << 8) & GENMASK(14, 8))
  428. #define RXCTL_DMWTLAT(x) ((x << 16) & GENMASK(18, 16))
  429. #define RXC_RST BIT(31)
  430. #define RXC_DQSISEL BIT(30)
  431. #define RCK_CTRL_RGMII_1000 (RXC_DQSISEL | RXCTL_DMWTLAT(2) | DQSI1(16))
  432. #define RCK_CTRL_RGMII_10_100 RXCTL_DMWTLAT(2)
  433. #define NUM_TRGMII_CTRL 5
  434. /* TRGMII RXC control register */
  435. #define TRGMII_TCK_CTRL 0x10340
  436. #define TXCTL_DMWTLAT(x) ((x << 16) & GENMASK(18, 16))
  437. #define TXC_INV BIT(30)
  438. #define TCK_CTRL_RGMII_1000 TXCTL_DMWTLAT(2)
  439. #define TCK_CTRL_RGMII_10_100 (TXC_INV | TXCTL_DMWTLAT(2))
  440. /* TRGMII TX Drive Strength */
  441. #define TRGMII_TD_ODT(i) (0x10354 + 8 * (i))
  442. #define TD_DM_DRVP(x) ((x) & 0xf)
  443. #define TD_DM_DRVN(x) (((x) & 0xf) << 4)
  444. /* TRGMII Interface mode register */
  445. #define INTF_MODE 0x10390
  446. #define TRGMII_INTF_DIS BIT(0)
  447. #define TRGMII_MODE BIT(1)
  448. #define TRGMII_CENTRAL_ALIGNED BIT(2)
  449. #define INTF_MODE_RGMII_1000 (TRGMII_MODE | TRGMII_CENTRAL_ALIGNED)
  450. #define INTF_MODE_RGMII_10_100 0
  451. /* XFI Mac control registers */
  452. #define MTK_XMAC_BASE(x) (0x12000 + (((x) - 1) * 0x1000))
  453. #define MTK_XMAC_MCR(x) (MTK_XMAC_BASE(x))
  454. #define XMAC_MCR_TRX_DISABLE 0xf
  455. #define XMAC_MCR_FORCE_TX_FC BIT(5)
  456. #define XMAC_MCR_FORCE_RX_FC BIT(4)
  457. /* XFI Mac logic reset registers */
  458. #define MTK_XMAC_LOGIC_RST(x) (MTK_XMAC_BASE(x) + 0x10)
  459. #define XMAC_LOGIC_RST BIT(0)
  460. /* XFI Mac count global control */
  461. #define MTK_XMAC_CNT_CTRL(x) (MTK_XMAC_BASE(x) + 0x100)
  462. #define XMAC_GLB_CNTCLR BIT(0)
  463. /* GPIO port control registers for GMAC 2*/
  464. #define GPIO_OD33_CTRL8 0x4c0
  465. #define GPIO_BIAS_CTRL 0xed0
  466. #define GPIO_DRV_SEL10 0xf00
  467. /* ethernet subsystem chip id register */
  468. #define ETHSYS_CHIPID0_3 0x0
  469. #define ETHSYS_CHIPID4_7 0x4
  470. #define MT7623_ETH 7623
  471. #define MT7622_ETH 7622
  472. #define MT7621_ETH 7621
  473. /* ethernet system control register */
  474. #define ETHSYS_SYSCFG 0x10
  475. #define SYSCFG_DRAM_TYPE_DDR2 BIT(4)
  476. /* ethernet subsystem config register */
  477. #define ETHSYS_SYSCFG0 0x14
  478. #define SYSCFG0_GE_MASK 0x3
  479. #define SYSCFG0_GE_MODE(x, y) (x << (12 + (y * 2)))
  480. #define SYSCFG0_SGMII_MASK GENMASK(9, 7)
  481. #define SYSCFG0_SGMII_GMAC1 ((2 << 8) & SYSCFG0_SGMII_MASK)
  482. #define SYSCFG0_SGMII_GMAC2 ((3 << 8) & SYSCFG0_SGMII_MASK)
  483. #define SYSCFG0_SGMII_GMAC1_V2 BIT(9)
  484. #define SYSCFG0_SGMII_GMAC2_V2 BIT(8)
  485. /* ethernet subsystem clock register */
  486. #define ETHSYS_CLKCFG0 0x2c
  487. #define ETHSYS_TRGMII_CLK_SEL362_5 BIT(11)
  488. #define ETHSYS_TRGMII_MT7621_MASK (BIT(5) | BIT(6))
  489. #define ETHSYS_TRGMII_MT7621_APLL BIT(6)
  490. #define ETHSYS_TRGMII_MT7621_DDR_PLL BIT(5)
  491. /* ethernet reset control register */
  492. #define ETHSYS_RSTCTRL 0x34
  493. #define RSTCTRL_FE BIT(6)
  494. #define RSTCTRL_WDMA0 BIT(24)
  495. #define RSTCTRL_WDMA1 BIT(25)
  496. #define RSTCTRL_WDMA2 BIT(26)
  497. #define RSTCTRL_PPE0 BIT(31)
  498. #define RSTCTRL_PPE0_V2 BIT(30)
  499. #define RSTCTRL_PPE1 BIT(31)
  500. #define RSTCTRL_PPE0_V3 BIT(29)
  501. #define RSTCTRL_PPE1_V3 BIT(30)
  502. #define RSTCTRL_PPE2 BIT(31)
  503. #define RSTCTRL_ETH BIT(23)
  504. /* ethernet reset check idle register */
  505. #define ETHSYS_FE_RST_CHK_IDLE_EN 0x28
  506. /* ethernet dma channel agent map */
  507. #define ETHSYS_DMA_AG_MAP 0x408
  508. #define ETHSYS_DMA_AG_MAP_PDMA BIT(0)
  509. #define ETHSYS_DMA_AG_MAP_QDMA BIT(1)
  510. #define ETHSYS_DMA_AG_MAP_PPE BIT(2)
  511. /* Infrasys subsystem config registers */
  512. #define INFRA_MISC2 0x70c
  513. #define CO_QPHY_SEL BIT(0)
  514. #define GEPHY_MAC_SEL BIT(1)
  515. /* Top misc registers */
  516. #define TOP_MISC_NETSYS_PCS_MUX 0x0
  517. #define NETSYS_PCS_MUX_MASK GENMASK(1, 0)
  518. #define MUX_G2_USXGMII_SEL BIT(1)
  519. #define USB_PHY_SWITCH_REG 0x218
  520. #define QPHY_SEL_MASK GENMASK(1, 0)
  521. #define SGMII_QPHY_SEL 0x2
  522. /* MT7628/88 specific stuff */
  523. #define MT7628_PDMA_OFFSET 0x0800
  524. #define MT7628_SDM_OFFSET 0x0c00
  525. #define MT7628_TX_BASE_PTR0 (MT7628_PDMA_OFFSET + 0x00)
  526. #define MT7628_TX_MAX_CNT0 (MT7628_PDMA_OFFSET + 0x04)
  527. #define MT7628_TX_CTX_IDX0 (MT7628_PDMA_OFFSET + 0x08)
  528. #define MT7628_TX_DTX_IDX0 (MT7628_PDMA_OFFSET + 0x0c)
  529. #define MT7628_PST_DTX_IDX0 BIT(0)
  530. #define MT7628_SDM_MAC_ADRL (MT7628_SDM_OFFSET + 0x0c)
  531. #define MT7628_SDM_MAC_ADRH (MT7628_SDM_OFFSET + 0x10)
  532. /* Counter / stat register */
  533. #define MT7628_SDM_TPCNT (MT7628_SDM_OFFSET + 0x100)
  534. #define MT7628_SDM_TBCNT (MT7628_SDM_OFFSET + 0x104)
  535. #define MT7628_SDM_RPCNT (MT7628_SDM_OFFSET + 0x108)
  536. #define MT7628_SDM_RBCNT (MT7628_SDM_OFFSET + 0x10c)
  537. #define MT7628_SDM_CS_ERR (MT7628_SDM_OFFSET + 0x110)
  538. #define MTK_FE_CDM1_FSM 0x220
  539. #define MTK_FE_CDM2_FSM 0x224
  540. #define MTK_FE_CDM3_FSM 0x238
  541. #define MTK_FE_CDM4_FSM 0x298
  542. #define MTK_FE_CDM5_FSM 0x318
  543. #define MTK_FE_CDM6_FSM 0x328
  544. #define MTK_FE_GDM1_FSM 0x228
  545. #define MTK_FE_GDM2_FSM 0x22C
  546. #define MTK_MAC_FSM(x) (0x1010C + ((x) * 0x100))
  547. #define MTK_FE_IRQ_SHARED 0
  548. #define MTK_FE_IRQ_TX 0
  549. #define MTK_FE_IRQ_RX 1
  550. #define MTK_FE_IRQ_NUM (MTK_FE_IRQ_RX + 1)
  551. struct mtk_rx_dma {
  552. unsigned int rxd1;
  553. unsigned int rxd2;
  554. unsigned int rxd3;
  555. unsigned int rxd4;
  556. } __packed __aligned(4);
  557. struct mtk_rx_dma_v2 {
  558. unsigned int rxd1;
  559. unsigned int rxd2;
  560. unsigned int rxd3;
  561. unsigned int rxd4;
  562. unsigned int rxd5;
  563. unsigned int rxd6;
  564. unsigned int rxd7;
  565. unsigned int rxd8;
  566. } __packed __aligned(4);
  567. struct mtk_tx_dma {
  568. unsigned int txd1;
  569. unsigned int txd2;
  570. unsigned int txd3;
  571. unsigned int txd4;
  572. } __packed __aligned(4);
  573. struct mtk_tx_dma_v2 {
  574. unsigned int txd1;
  575. unsigned int txd2;
  576. unsigned int txd3;
  577. unsigned int txd4;
  578. unsigned int txd5;
  579. unsigned int txd6;
  580. unsigned int txd7;
  581. unsigned int txd8;
  582. } __packed __aligned(4);
  583. struct mtk_eth;
  584. struct mtk_mac;
  585. struct mtk_xdp_stats {
  586. u64 rx_xdp_redirect;
  587. u64 rx_xdp_pass;
  588. u64 rx_xdp_drop;
  589. u64 rx_xdp_tx;
  590. u64 rx_xdp_tx_errors;
  591. u64 tx_xdp_xmit;
  592. u64 tx_xdp_xmit_errors;
  593. };
  594. /* struct mtk_hw_stats - the structure that holds the traffic statistics.
  595. * @stats_lock: make sure that stats operations are atomic
  596. * @reg_offset: the status register offset of the SoC
  597. * @syncp: the refcount
  598. *
  599. * All of the supported SoCs have hardware counters for traffic statistics.
  600. * Whenever the status IRQ triggers we can read the latest stats from these
  601. * counters and store them in this struct.
  602. */
  603. struct mtk_hw_stats {
  604. u64 tx_bytes;
  605. u64 tx_packets;
  606. u64 tx_skip;
  607. u64 tx_collisions;
  608. u64 rx_bytes;
  609. u64 rx_packets;
  610. u64 rx_overflow;
  611. u64 rx_fcs_errors;
  612. u64 rx_short_errors;
  613. u64 rx_long_errors;
  614. u64 rx_checksum_errors;
  615. u64 rx_flow_control_packets;
  616. struct mtk_xdp_stats xdp_stats;
  617. spinlock_t stats_lock;
  618. u32 reg_offset;
  619. struct u64_stats_sync syncp;
  620. };
  621. enum mtk_tx_flags {
  622. /* PDMA descriptor can point at 1-2 segments. This enum allows us to
  623. * track how memory was allocated so that it can be freed properly.
  624. */
  625. MTK_TX_FLAGS_SINGLE0 = 0x01,
  626. MTK_TX_FLAGS_PAGE0 = 0x02,
  627. };
  628. /* This enum allows us to identify how the clock is defined on the array of the
  629. * clock in the order
  630. */
  631. enum mtk_clks_map {
  632. MTK_CLK_ETHIF,
  633. MTK_CLK_SGMIITOP,
  634. MTK_CLK_ESW,
  635. MTK_CLK_GP0,
  636. MTK_CLK_GP1,
  637. MTK_CLK_GP2,
  638. MTK_CLK_GP3,
  639. MTK_CLK_XGP1,
  640. MTK_CLK_XGP2,
  641. MTK_CLK_XGP3,
  642. MTK_CLK_CRYPTO,
  643. MTK_CLK_FE,
  644. MTK_CLK_TRGPLL,
  645. MTK_CLK_SGMII_TX_250M,
  646. MTK_CLK_SGMII_RX_250M,
  647. MTK_CLK_SGMII_CDR_REF,
  648. MTK_CLK_SGMII_CDR_FB,
  649. MTK_CLK_SGMII2_TX_250M,
  650. MTK_CLK_SGMII2_RX_250M,
  651. MTK_CLK_SGMII2_CDR_REF,
  652. MTK_CLK_SGMII2_CDR_FB,
  653. MTK_CLK_SGMII_CK,
  654. MTK_CLK_ETH2PLL,
  655. MTK_CLK_WOCPU0,
  656. MTK_CLK_WOCPU1,
  657. MTK_CLK_NETSYS0,
  658. MTK_CLK_NETSYS1,
  659. MTK_CLK_ETHWARP_WOCPU2,
  660. MTK_CLK_ETHWARP_WOCPU1,
  661. MTK_CLK_ETHWARP_WOCPU0,
  662. MTK_CLK_TOP_SGM_0_SEL,
  663. MTK_CLK_TOP_SGM_1_SEL,
  664. MTK_CLK_TOP_ETH_GMII_SEL,
  665. MTK_CLK_TOP_ETH_REFCK_50M_SEL,
  666. MTK_CLK_TOP_ETH_SYS_200M_SEL,
  667. MTK_CLK_TOP_ETH_SYS_SEL,
  668. MTK_CLK_TOP_ETH_XGMII_SEL,
  669. MTK_CLK_TOP_ETH_MII_SEL,
  670. MTK_CLK_TOP_NETSYS_SEL,
  671. MTK_CLK_TOP_NETSYS_500M_SEL,
  672. MTK_CLK_TOP_NETSYS_PAO_2X_SEL,
  673. MTK_CLK_TOP_NETSYS_SYNC_250M_SEL,
  674. MTK_CLK_TOP_NETSYS_PPEFB_250M_SEL,
  675. MTK_CLK_TOP_NETSYS_WARP_SEL,
  676. MTK_CLK_MAX
  677. };
  678. #define MT7623_CLKS_BITMAP (BIT_ULL(MTK_CLK_ETHIF) | BIT_ULL(MTK_CLK_ESW) | \
  679. BIT_ULL(MTK_CLK_GP1) | BIT_ULL(MTK_CLK_GP2) | \
  680. BIT_ULL(MTK_CLK_TRGPLL))
  681. #define MT7622_CLKS_BITMAP (BIT_ULL(MTK_CLK_ETHIF) | BIT_ULL(MTK_CLK_ESW) | \
  682. BIT_ULL(MTK_CLK_GP0) | BIT_ULL(MTK_CLK_GP1) | \
  683. BIT_ULL(MTK_CLK_GP2) | \
  684. BIT_ULL(MTK_CLK_SGMII_TX_250M) | \
  685. BIT_ULL(MTK_CLK_SGMII_RX_250M) | \
  686. BIT_ULL(MTK_CLK_SGMII_CDR_REF) | \
  687. BIT_ULL(MTK_CLK_SGMII_CDR_FB) | \
  688. BIT_ULL(MTK_CLK_SGMII_CK) | \
  689. BIT_ULL(MTK_CLK_ETH2PLL))
  690. #define MT7621_CLKS_BITMAP (0)
  691. #define MT7628_CLKS_BITMAP (0)
  692. #define MT7629_CLKS_BITMAP (BIT_ULL(MTK_CLK_ETHIF) | BIT_ULL(MTK_CLK_ESW) | \
  693. BIT_ULL(MTK_CLK_GP0) | BIT_ULL(MTK_CLK_GP1) | \
  694. BIT_ULL(MTK_CLK_GP2) | BIT_ULL(MTK_CLK_FE) | \
  695. BIT_ULL(MTK_CLK_SGMII_TX_250M) | \
  696. BIT_ULL(MTK_CLK_SGMII_RX_250M) | \
  697. BIT_ULL(MTK_CLK_SGMII_CDR_REF) | \
  698. BIT_ULL(MTK_CLK_SGMII_CDR_FB) | \
  699. BIT_ULL(MTK_CLK_SGMII2_TX_250M) | \
  700. BIT_ULL(MTK_CLK_SGMII2_RX_250M) | \
  701. BIT_ULL(MTK_CLK_SGMII2_CDR_REF) | \
  702. BIT_ULL(MTK_CLK_SGMII2_CDR_FB) | \
  703. BIT_ULL(MTK_CLK_SGMII_CK) | \
  704. BIT_ULL(MTK_CLK_ETH2PLL) | BIT_ULL(MTK_CLK_SGMIITOP))
  705. #define MT7981_CLKS_BITMAP (BIT_ULL(MTK_CLK_FE) | BIT_ULL(MTK_CLK_GP2) | \
  706. BIT_ULL(MTK_CLK_GP1) | \
  707. BIT_ULL(MTK_CLK_WOCPU0) | \
  708. BIT_ULL(MTK_CLK_SGMII_TX_250M) | \
  709. BIT_ULL(MTK_CLK_SGMII_RX_250M) | \
  710. BIT_ULL(MTK_CLK_SGMII_CDR_REF) | \
  711. BIT_ULL(MTK_CLK_SGMII_CDR_FB) | \
  712. BIT_ULL(MTK_CLK_SGMII2_TX_250M) | \
  713. BIT_ULL(MTK_CLK_SGMII2_RX_250M) | \
  714. BIT_ULL(MTK_CLK_SGMII2_CDR_REF) | \
  715. BIT_ULL(MTK_CLK_SGMII2_CDR_FB) | \
  716. BIT_ULL(MTK_CLK_SGMII_CK))
  717. #define MT7986_CLKS_BITMAP (BIT_ULL(MTK_CLK_FE) | BIT_ULL(MTK_CLK_GP2) | \
  718. BIT_ULL(MTK_CLK_GP1) | \
  719. BIT_ULL(MTK_CLK_WOCPU1) | BIT_ULL(MTK_CLK_WOCPU0) | \
  720. BIT_ULL(MTK_CLK_SGMII_TX_250M) | \
  721. BIT_ULL(MTK_CLK_SGMII_RX_250M) | \
  722. BIT_ULL(MTK_CLK_SGMII_CDR_REF) | \
  723. BIT_ULL(MTK_CLK_SGMII_CDR_FB) | \
  724. BIT_ULL(MTK_CLK_SGMII2_TX_250M) | \
  725. BIT_ULL(MTK_CLK_SGMII2_RX_250M) | \
  726. BIT_ULL(MTK_CLK_SGMII2_CDR_REF) | \
  727. BIT_ULL(MTK_CLK_SGMII2_CDR_FB))
  728. #define MT7988_CLKS_BITMAP (BIT_ULL(MTK_CLK_FE) | BIT_ULL(MTK_CLK_ESW) | \
  729. BIT_ULL(MTK_CLK_GP1) | BIT_ULL(MTK_CLK_GP2) | \
  730. BIT_ULL(MTK_CLK_GP3) | BIT_ULL(MTK_CLK_XGP1) | \
  731. BIT_ULL(MTK_CLK_XGP2) | BIT_ULL(MTK_CLK_XGP3) | \
  732. BIT_ULL(MTK_CLK_CRYPTO) | \
  733. BIT_ULL(MTK_CLK_ETHWARP_WOCPU2) | \
  734. BIT_ULL(MTK_CLK_ETHWARP_WOCPU1) | \
  735. BIT_ULL(MTK_CLK_ETHWARP_WOCPU0) | \
  736. BIT_ULL(MTK_CLK_TOP_ETH_GMII_SEL) | \
  737. BIT_ULL(MTK_CLK_TOP_ETH_REFCK_50M_SEL) | \
  738. BIT_ULL(MTK_CLK_TOP_ETH_SYS_200M_SEL) | \
  739. BIT_ULL(MTK_CLK_TOP_ETH_SYS_SEL) | \
  740. BIT_ULL(MTK_CLK_TOP_ETH_XGMII_SEL) | \
  741. BIT_ULL(MTK_CLK_TOP_ETH_MII_SEL) | \
  742. BIT_ULL(MTK_CLK_TOP_NETSYS_SEL) | \
  743. BIT_ULL(MTK_CLK_TOP_NETSYS_500M_SEL) | \
  744. BIT_ULL(MTK_CLK_TOP_NETSYS_PAO_2X_SEL) | \
  745. BIT_ULL(MTK_CLK_TOP_NETSYS_SYNC_250M_SEL) | \
  746. BIT_ULL(MTK_CLK_TOP_NETSYS_PPEFB_250M_SEL) | \
  747. BIT_ULL(MTK_CLK_TOP_NETSYS_WARP_SEL))
  748. enum mtk_dev_state {
  749. MTK_HW_INIT,
  750. MTK_RESETTING
  751. };
  752. /* PSE Port Definition */
  753. enum mtk_pse_port {
  754. PSE_ADMA_PORT = 0,
  755. PSE_GDM1_PORT,
  756. PSE_GDM2_PORT,
  757. PSE_PPE0_PORT,
  758. PSE_PPE1_PORT,
  759. PSE_QDMA_TX_PORT,
  760. PSE_QDMA_RX_PORT,
  761. PSE_DROP_PORT,
  762. PSE_WDMA0_PORT,
  763. PSE_WDMA1_PORT,
  764. PSE_TDMA_PORT,
  765. PSE_NONE_PORT,
  766. PSE_PPE2_PORT,
  767. PSE_WDMA2_PORT,
  768. PSE_EIP197_PORT,
  769. PSE_GDM3_PORT,
  770. PSE_PORT_MAX
  771. };
  772. /* GMAC Identifier */
  773. enum mtk_gmac_id {
  774. MTK_GMAC1_ID = 0,
  775. MTK_GMAC2_ID,
  776. MTK_GMAC3_ID,
  777. MTK_GMAC_ID_MAX
  778. };
  779. enum mtk_tx_buf_type {
  780. MTK_TYPE_SKB,
  781. MTK_TYPE_XDP_TX,
  782. MTK_TYPE_XDP_NDO,
  783. };
  784. /* struct mtk_tx_buf - This struct holds the pointers to the memory pointed at
  785. * by the TX descriptor s
  786. * @skb: The SKB pointer of the packet being sent
  787. * @dma_addr0: The base addr of the first segment
  788. * @dma_len0: The length of the first segment
  789. * @dma_addr1: The base addr of the second segment
  790. * @dma_len1: The length of the second segment
  791. */
  792. struct mtk_tx_buf {
  793. enum mtk_tx_buf_type type;
  794. void *data;
  795. u16 mac_id;
  796. u16 flags;
  797. DEFINE_DMA_UNMAP_ADDR(dma_addr0);
  798. DEFINE_DMA_UNMAP_LEN(dma_len0);
  799. DEFINE_DMA_UNMAP_ADDR(dma_addr1);
  800. DEFINE_DMA_UNMAP_LEN(dma_len1);
  801. };
  802. /* struct mtk_tx_ring - This struct holds info describing a TX ring
  803. * @dma: The descriptor ring
  804. * @buf: The memory pointed at by the ring
  805. * @phys: The physical addr of tx_buf
  806. * @next_free: Pointer to the next free descriptor
  807. * @last_free: Pointer to the last free descriptor
  808. * @last_free_ptr: Hardware pointer value of the last free descriptor
  809. * @thresh: The threshold of minimum amount of free descriptors
  810. * @free_count: QDMA uses a linked list. Track how many free descriptors
  811. * are present
  812. */
  813. struct mtk_tx_ring {
  814. void *dma;
  815. struct mtk_tx_buf *buf;
  816. dma_addr_t phys;
  817. struct mtk_tx_dma *next_free;
  818. struct mtk_tx_dma *last_free;
  819. u32 last_free_ptr;
  820. u16 thresh;
  821. atomic_t free_count;
  822. int dma_size;
  823. struct mtk_tx_dma *dma_pdma; /* For MT7628/88 PDMA handling */
  824. dma_addr_t phys_pdma;
  825. int cpu_idx;
  826. };
  827. /* PDMA rx ring mode */
  828. enum mtk_rx_flags {
  829. MTK_RX_FLAGS_NORMAL = 0,
  830. MTK_RX_FLAGS_HWLRO,
  831. MTK_RX_FLAGS_QDMA,
  832. };
  833. /* struct mtk_rx_ring - This struct holds info describing a RX ring
  834. * @dma: The descriptor ring
  835. * @data: The memory pointed at by the ring
  836. * @phys: The physical addr of rx_buf
  837. * @frag_size: How big can each fragment be
  838. * @buf_size: The size of each packet buffer
  839. * @calc_idx: The current head of ring
  840. */
  841. struct mtk_rx_ring {
  842. void *dma;
  843. u8 **data;
  844. dma_addr_t phys;
  845. u16 frag_size;
  846. u16 buf_size;
  847. u16 dma_size;
  848. bool calc_idx_update;
  849. u16 calc_idx;
  850. u32 crx_idx_reg;
  851. /* page_pool */
  852. struct page_pool *page_pool;
  853. struct xdp_rxq_info xdp_q;
  854. };
  855. enum mkt_eth_capabilities {
  856. MTK_RGMII_BIT = 0,
  857. MTK_TRGMII_BIT,
  858. MTK_SGMII_BIT,
  859. MTK_2P5GPHY_BIT,
  860. MTK_ESW_BIT,
  861. MTK_GEPHY_BIT,
  862. MTK_MUX_BIT,
  863. MTK_INFRA_BIT,
  864. MTK_SHARED_SGMII_BIT,
  865. MTK_HWLRO_BIT,
  866. MTK_SHARED_INT_BIT,
  867. MTK_TRGMII_MT7621_CLK_BIT,
  868. MTK_QDMA_BIT,
  869. MTK_SOC_MT7628_BIT,
  870. MTK_RSTCTRL_PPE1_BIT,
  871. MTK_RSTCTRL_PPE2_BIT,
  872. MTK_U3_COPHY_V2_BIT,
  873. MTK_SRAM_BIT,
  874. MTK_36BIT_DMA_BIT,
  875. /* MUX BITS*/
  876. MTK_ETH_MUX_GDM1_TO_GMAC1_ESW_BIT,
  877. MTK_ETH_MUX_GMAC2_GMAC0_TO_GEPHY_BIT,
  878. MTK_ETH_MUX_U3_GMAC2_TO_QPHY_BIT,
  879. MTK_ETH_MUX_GMAC2_TO_2P5GPHY_BIT,
  880. MTK_ETH_MUX_GMAC1_GMAC2_TO_SGMII_RGMII_BIT,
  881. MTK_ETH_MUX_GMAC12_TO_GEPHY_SGMII_BIT,
  882. /* PATH BITS */
  883. MTK_ETH_PATH_GMAC1_RGMII_BIT,
  884. MTK_ETH_PATH_GMAC1_TRGMII_BIT,
  885. MTK_ETH_PATH_GMAC1_SGMII_BIT,
  886. MTK_ETH_PATH_GMAC2_RGMII_BIT,
  887. MTK_ETH_PATH_GMAC2_SGMII_BIT,
  888. MTK_ETH_PATH_GMAC2_2P5GPHY_BIT,
  889. MTK_ETH_PATH_GMAC2_GEPHY_BIT,
  890. MTK_ETH_PATH_GDM1_ESW_BIT,
  891. };
  892. /* Supported hardware group on SoCs */
  893. #define MTK_RGMII BIT_ULL(MTK_RGMII_BIT)
  894. #define MTK_TRGMII BIT_ULL(MTK_TRGMII_BIT)
  895. #define MTK_SGMII BIT_ULL(MTK_SGMII_BIT)
  896. #define MTK_2P5GPHY BIT_ULL(MTK_2P5GPHY_BIT)
  897. #define MTK_ESW BIT_ULL(MTK_ESW_BIT)
  898. #define MTK_GEPHY BIT_ULL(MTK_GEPHY_BIT)
  899. #define MTK_MUX BIT_ULL(MTK_MUX_BIT)
  900. #define MTK_INFRA BIT_ULL(MTK_INFRA_BIT)
  901. #define MTK_SHARED_SGMII BIT_ULL(MTK_SHARED_SGMII_BIT)
  902. #define MTK_HWLRO BIT_ULL(MTK_HWLRO_BIT)
  903. #define MTK_SHARED_INT BIT_ULL(MTK_SHARED_INT_BIT)
  904. #define MTK_TRGMII_MT7621_CLK BIT_ULL(MTK_TRGMII_MT7621_CLK_BIT)
  905. #define MTK_QDMA BIT_ULL(MTK_QDMA_BIT)
  906. #define MTK_SOC_MT7628 BIT_ULL(MTK_SOC_MT7628_BIT)
  907. #define MTK_RSTCTRL_PPE1 BIT_ULL(MTK_RSTCTRL_PPE1_BIT)
  908. #define MTK_RSTCTRL_PPE2 BIT_ULL(MTK_RSTCTRL_PPE2_BIT)
  909. #define MTK_U3_COPHY_V2 BIT_ULL(MTK_U3_COPHY_V2_BIT)
  910. #define MTK_SRAM BIT_ULL(MTK_SRAM_BIT)
  911. #define MTK_36BIT_DMA BIT_ULL(MTK_36BIT_DMA_BIT)
  912. #define MTK_ETH_MUX_GDM1_TO_GMAC1_ESW \
  913. BIT_ULL(MTK_ETH_MUX_GDM1_TO_GMAC1_ESW_BIT)
  914. #define MTK_ETH_MUX_GMAC2_GMAC0_TO_GEPHY \
  915. BIT_ULL(MTK_ETH_MUX_GMAC2_GMAC0_TO_GEPHY_BIT)
  916. #define MTK_ETH_MUX_U3_GMAC2_TO_QPHY \
  917. BIT_ULL(MTK_ETH_MUX_U3_GMAC2_TO_QPHY_BIT)
  918. #define MTK_ETH_MUX_GMAC2_TO_2P5GPHY \
  919. BIT_ULL(MTK_ETH_MUX_GMAC2_TO_2P5GPHY_BIT)
  920. #define MTK_ETH_MUX_GMAC1_GMAC2_TO_SGMII_RGMII \
  921. BIT_ULL(MTK_ETH_MUX_GMAC1_GMAC2_TO_SGMII_RGMII_BIT)
  922. #define MTK_ETH_MUX_GMAC12_TO_GEPHY_SGMII \
  923. BIT_ULL(MTK_ETH_MUX_GMAC12_TO_GEPHY_SGMII_BIT)
  924. /* Supported path present on SoCs */
  925. #define MTK_ETH_PATH_GMAC1_RGMII BIT_ULL(MTK_ETH_PATH_GMAC1_RGMII_BIT)
  926. #define MTK_ETH_PATH_GMAC1_TRGMII BIT_ULL(MTK_ETH_PATH_GMAC1_TRGMII_BIT)
  927. #define MTK_ETH_PATH_GMAC1_SGMII BIT_ULL(MTK_ETH_PATH_GMAC1_SGMII_BIT)
  928. #define MTK_ETH_PATH_GMAC2_RGMII BIT_ULL(MTK_ETH_PATH_GMAC2_RGMII_BIT)
  929. #define MTK_ETH_PATH_GMAC2_SGMII BIT_ULL(MTK_ETH_PATH_GMAC2_SGMII_BIT)
  930. #define MTK_ETH_PATH_GMAC2_2P5GPHY BIT_ULL(MTK_ETH_PATH_GMAC2_2P5GPHY_BIT)
  931. #define MTK_ETH_PATH_GMAC2_GEPHY BIT_ULL(MTK_ETH_PATH_GMAC2_GEPHY_BIT)
  932. #define MTK_ETH_PATH_GDM1_ESW BIT_ULL(MTK_ETH_PATH_GDM1_ESW_BIT)
  933. #define MTK_GMAC1_RGMII (MTK_ETH_PATH_GMAC1_RGMII | MTK_RGMII)
  934. #define MTK_GMAC1_TRGMII (MTK_ETH_PATH_GMAC1_TRGMII | MTK_TRGMII)
  935. #define MTK_GMAC1_SGMII (MTK_ETH_PATH_GMAC1_SGMII | MTK_SGMII)
  936. #define MTK_GMAC2_RGMII (MTK_ETH_PATH_GMAC2_RGMII | MTK_RGMII)
  937. #define MTK_GMAC2_SGMII (MTK_ETH_PATH_GMAC2_SGMII | MTK_SGMII)
  938. #define MTK_GMAC2_GEPHY (MTK_ETH_PATH_GMAC2_GEPHY | MTK_GEPHY)
  939. #define MTK_GMAC2_2P5GPHY (MTK_ETH_PATH_GMAC2_2P5GPHY | MTK_2P5GPHY)
  940. #define MTK_GDM1_ESW (MTK_ETH_PATH_GDM1_ESW | MTK_ESW)
  941. /* MUXes present on SoCs */
  942. /* 0: GDM1 -> GMAC1, 1: GDM1 -> ESW */
  943. #define MTK_MUX_GDM1_TO_GMAC1_ESW (MTK_ETH_MUX_GDM1_TO_GMAC1_ESW | MTK_MUX)
  944. /* 0: GMAC2 -> GEPHY, 1: GMAC0 -> GePHY */
  945. #define MTK_MUX_GMAC2_GMAC0_TO_GEPHY \
  946. (MTK_ETH_MUX_GMAC2_GMAC0_TO_GEPHY | MTK_MUX | MTK_INFRA)
  947. /* 0: U3 -> QPHY, 1: GMAC2 -> QPHY */
  948. #define MTK_MUX_U3_GMAC2_TO_QPHY \
  949. (MTK_ETH_MUX_U3_GMAC2_TO_QPHY | MTK_MUX | MTK_INFRA)
  950. /* 2: GMAC1 -> SGMII, 3: GMAC2 -> SGMII */
  951. #define MTK_MUX_GMAC1_GMAC2_TO_SGMII_RGMII \
  952. (MTK_ETH_MUX_GMAC1_GMAC2_TO_SGMII_RGMII | MTK_MUX | \
  953. MTK_SHARED_SGMII)
  954. /* 2: GMAC2 -> 2P5GPHY */
  955. #define MTK_MUX_GMAC2_TO_2P5GPHY \
  956. (MTK_ETH_MUX_GMAC2_TO_2P5GPHY | MTK_MUX | MTK_INFRA)
  957. /* 0: GMACx -> GEPHY, 1: GMACx -> SGMII where x is 1 or 2 */
  958. #define MTK_MUX_GMAC12_TO_GEPHY_SGMII \
  959. (MTK_ETH_MUX_GMAC12_TO_GEPHY_SGMII | MTK_MUX)
  960. #define MTK_HAS_CAPS(caps, _x) (((caps) & (_x)) == (_x))
  961. #define MT7621_CAPS (MTK_GMAC1_RGMII | MTK_GMAC1_TRGMII | \
  962. MTK_GMAC2_RGMII | MTK_SHARED_INT | \
  963. MTK_TRGMII_MT7621_CLK | MTK_QDMA)
  964. #define MT7622_CAPS (MTK_GMAC1_RGMII | MTK_GMAC1_SGMII | MTK_GMAC2_RGMII | \
  965. MTK_GMAC2_SGMII | MTK_GDM1_ESW | \
  966. MTK_MUX_GDM1_TO_GMAC1_ESW | \
  967. MTK_MUX_GMAC1_GMAC2_TO_SGMII_RGMII | MTK_QDMA)
  968. #define MT7623_CAPS (MTK_GMAC1_RGMII | MTK_GMAC1_TRGMII | MTK_GMAC2_RGMII | \
  969. MTK_QDMA)
  970. #define MT7628_CAPS (MTK_SHARED_INT | MTK_SOC_MT7628)
  971. #define MT7629_CAPS (MTK_GMAC1_SGMII | MTK_GMAC2_SGMII | MTK_GMAC2_GEPHY | \
  972. MTK_GDM1_ESW | MTK_MUX_GDM1_TO_GMAC1_ESW | \
  973. MTK_MUX_GMAC2_GMAC0_TO_GEPHY | \
  974. MTK_MUX_U3_GMAC2_TO_QPHY | \
  975. MTK_MUX_GMAC12_TO_GEPHY_SGMII | MTK_QDMA)
  976. #define MT7981_CAPS (MTK_GMAC1_SGMII | MTK_GMAC2_SGMII | MTK_GMAC2_GEPHY | \
  977. MTK_MUX_GMAC12_TO_GEPHY_SGMII | MTK_QDMA | \
  978. MTK_MUX_U3_GMAC2_TO_QPHY | MTK_U3_COPHY_V2 | \
  979. MTK_RSTCTRL_PPE1 | MTK_SRAM)
  980. #define MT7986_CAPS (MTK_GMAC1_SGMII | MTK_GMAC2_SGMII | \
  981. MTK_MUX_GMAC12_TO_GEPHY_SGMII | MTK_QDMA | \
  982. MTK_RSTCTRL_PPE1 | MTK_SRAM)
  983. #define MT7988_CAPS (MTK_36BIT_DMA | MTK_GDM1_ESW | MTK_GMAC2_2P5GPHY | \
  984. MTK_MUX_GMAC2_TO_2P5GPHY | MTK_QDMA | MTK_RSTCTRL_PPE1 | \
  985. MTK_RSTCTRL_PPE2 | MTK_SRAM)
  986. struct mtk_tx_dma_desc_info {
  987. dma_addr_t addr;
  988. u32 size;
  989. u16 vlan_tci;
  990. u16 qid;
  991. u8 gso:1;
  992. u8 csum:1;
  993. u8 vlan:1;
  994. u8 first:1;
  995. u8 last:1;
  996. };
  997. struct mtk_reg_map {
  998. u32 tx_irq_mask;
  999. u32 tx_irq_status;
  1000. struct {
  1001. u32 rx_ptr; /* rx base pointer */
  1002. u32 rx_cnt_cfg; /* rx max count configuration */
  1003. u32 pcrx_ptr; /* rx cpu pointer */
  1004. u32 glo_cfg; /* global configuration */
  1005. u32 rst_idx; /* reset index */
  1006. u32 delay_irq; /* delay interrupt */
  1007. u32 irq_status; /* interrupt status */
  1008. u32 irq_mask; /* interrupt mask */
  1009. u32 adma_rx_dbg0;
  1010. u32 int_grp;
  1011. } pdma;
  1012. struct {
  1013. u32 qtx_cfg; /* tx queue configuration */
  1014. u32 qtx_sch; /* tx queue scheduler configuration */
  1015. u32 rx_ptr; /* rx base pointer */
  1016. u32 rx_cnt_cfg; /* rx max count configuration */
  1017. u32 qcrx_ptr; /* rx cpu pointer */
  1018. u32 glo_cfg; /* global configuration */
  1019. u32 rst_idx; /* reset index */
  1020. u32 delay_irq; /* delay interrupt */
  1021. u32 fc_th; /* flow control */
  1022. u32 int_grp;
  1023. u32 hred; /* interrupt mask */
  1024. u32 ctx_ptr; /* tx acquire cpu pointer */
  1025. u32 dtx_ptr; /* tx acquire dma pointer */
  1026. u32 crx_ptr; /* tx release cpu pointer */
  1027. u32 drx_ptr; /* tx release dma pointer */
  1028. u32 fq_head; /* fq head pointer */
  1029. u32 fq_tail; /* fq tail pointer */
  1030. u32 fq_count; /* fq free page count */
  1031. u32 fq_blen; /* fq free page buffer length */
  1032. u32 tx_sch_rate; /* tx scheduler rate control registers */
  1033. } qdma;
  1034. u32 gdm1_cnt;
  1035. u32 gdma_to_ppe[3];
  1036. u32 ppe_base;
  1037. u32 wdma_base[3];
  1038. u32 pse_iq_sta;
  1039. u32 pse_oq_sta;
  1040. };
  1041. /* struct mtk_eth_data - This is the structure holding all differences
  1042. * among various platforms
  1043. * @reg_map Soc register map.
  1044. * @ana_rgc3: The offset for register ANA_RGC3 related to
  1045. * sgmiisys syscon
  1046. * @caps Flags shown the extra capability for the SoC
  1047. * @hw_features Flags shown HW features
  1048. * @required_clks Flags shown the bitmap for required clocks on
  1049. * the target SoC
  1050. * @required_pctl A bool value to show whether the SoC requires
  1051. * the extra setup for those pins used by GMAC.
  1052. * @hash_offset Flow table hash offset.
  1053. * @version SoC version.
  1054. * @foe_entry_size Foe table entry size.
  1055. * @has_accounting Bool indicating support for accounting of
  1056. * offloaded flows.
  1057. * @desc_size Tx/Rx DMA descriptor size.
  1058. * @irq_done_mask Rx irq done register mask.
  1059. * @dma_l4_valid Rx DMA valid register mask.
  1060. * @dma_max_len Max DMA tx/rx buffer length.
  1061. * @dma_len_offset Tx/Rx DMA length field offset.
  1062. */
  1063. struct mtk_soc_data {
  1064. const struct mtk_reg_map *reg_map;
  1065. u32 ana_rgc3;
  1066. u64 caps;
  1067. u64 required_clks;
  1068. bool required_pctl;
  1069. u8 offload_version;
  1070. u8 hash_offset;
  1071. u8 version;
  1072. u8 ppe_num;
  1073. u16 foe_entry_size;
  1074. netdev_features_t hw_features;
  1075. bool has_accounting;
  1076. bool disable_pll_modes;
  1077. struct {
  1078. u32 desc_size;
  1079. u32 dma_max_len;
  1080. u32 dma_len_offset;
  1081. u32 dma_size;
  1082. u32 fq_dma_size;
  1083. } tx;
  1084. struct {
  1085. u32 desc_size;
  1086. u32 irq_done_mask;
  1087. u32 dma_l4_valid;
  1088. u32 dma_max_len;
  1089. u32 dma_len_offset;
  1090. u32 dma_size;
  1091. } rx;
  1092. };
  1093. #define MTK_DMA_MONITOR_TIMEOUT msecs_to_jiffies(1000)
  1094. /* currently no SoC has more than 3 macs */
  1095. #define MTK_MAX_DEVS 3
  1096. /* struct mtk_eth - This is the main datasructure for holding the state
  1097. * of the driver
  1098. * @dev: The device pointer
  1099. * @dma_dev: The device pointer used for dma mapping/alloc
  1100. * @base: The mapped register i/o base
  1101. * @sram_pool: Pointer to SRAM pool used for DMA descriptor rings
  1102. * @page_lock: Make sure that register operations are atomic
  1103. * @tx_irq__lock: Make sure that IRQ register operations are atomic
  1104. * @rx_irq__lock: Make sure that IRQ register operations are atomic
  1105. * @dim_lock: Make sure that Net DIM operations are atomic
  1106. * @dummy_dev: we run 2 netdevs on 1 physical DMA ring and need a
  1107. * dummy for NAPI to work
  1108. * @netdev: The netdev instances
  1109. * @mac: Each netdev is linked to a physical MAC
  1110. * @irq: The IRQ that we are using
  1111. * @msg_enable: Ethtool msg level
  1112. * @ethsys: The register map pointing at the range used to setup
  1113. * MII modes
  1114. * @infra: The register map pointing at the range used to setup
  1115. * SGMII and GePHY path
  1116. * @sgmii_pcs: Pointers to mtk-pcs-lynxi phylink_pcs instances
  1117. * @pctl: The register map pointing at the range used to setup
  1118. * GMAC port drive/slew values
  1119. * @dma_refcnt: track how many netdevs are using the DMA engine
  1120. * @tx_ring: Pointer to the memory holding info about the TX ring
  1121. * @rx_ring: Pointer to the memory holding info about the RX ring
  1122. * @rx_ring_qdma: Pointer to the memory holding info about the QDMA RX ring
  1123. * @tx_napi: The TX NAPI struct
  1124. * @rx_napi: The RX NAPI struct
  1125. * @rx_events: Net DIM RX event counter
  1126. * @rx_packets: Net DIM RX packet counter
  1127. * @rx_bytes: Net DIM RX byte counter
  1128. * @rx_dim: Net DIM RX context
  1129. * @tx_events: Net DIM TX event counter
  1130. * @tx_packets: Net DIM TX packet counter
  1131. * @tx_bytes: Net DIM TX byte counter
  1132. * @tx_dim: Net DIM TX context
  1133. * @scratch_ring: Newer SoCs need memory for a second HW managed TX ring
  1134. * @phy_scratch_ring: physical address of scratch_ring
  1135. * @scratch_head: The scratch memory that scratch_ring points to.
  1136. * @clks: clock array for all clocks required
  1137. * @mii_bus: If there is a bus we need to create an instance for it
  1138. * @pending_work: The workqueue used to reset the dma ring
  1139. * @state: Initialization and runtime state of the device
  1140. * @soc: Holding specific data among various SoCs
  1141. */
  1142. struct mtk_eth {
  1143. struct device *dev;
  1144. struct device *dma_dev;
  1145. void __iomem *base;
  1146. struct gen_pool *sram_pool;
  1147. spinlock_t page_lock;
  1148. spinlock_t tx_irq_lock;
  1149. spinlock_t rx_irq_lock;
  1150. struct net_device *dummy_dev;
  1151. struct net_device *netdev[MTK_MAX_DEVS];
  1152. struct mtk_mac *mac[MTK_MAX_DEVS];
  1153. int irq[MTK_FE_IRQ_NUM];
  1154. u32 msg_enable;
  1155. unsigned long sysclk;
  1156. struct regmap *ethsys;
  1157. struct regmap *infra;
  1158. struct phylink_pcs *sgmii_pcs[MTK_MAX_DEVS];
  1159. struct regmap *pctl;
  1160. bool hwlro;
  1161. refcount_t dma_refcnt;
  1162. struct mtk_tx_ring tx_ring;
  1163. struct mtk_rx_ring rx_ring[MTK_MAX_RX_RING_NUM];
  1164. struct mtk_rx_ring rx_ring_qdma;
  1165. struct napi_struct tx_napi;
  1166. struct napi_struct rx_napi;
  1167. void *scratch_ring;
  1168. dma_addr_t phy_scratch_ring;
  1169. void *scratch_head[MTK_FQ_DMA_HEAD];
  1170. struct clk *clks[MTK_CLK_MAX];
  1171. struct mii_bus *mii_bus;
  1172. unsigned int mdc_divider;
  1173. struct work_struct pending_work;
  1174. unsigned long state;
  1175. const struct mtk_soc_data *soc;
  1176. spinlock_t dim_lock;
  1177. u32 rx_events;
  1178. u32 rx_packets;
  1179. u32 rx_bytes;
  1180. struct dim rx_dim;
  1181. u32 tx_events;
  1182. u32 tx_packets;
  1183. u32 tx_bytes;
  1184. struct dim tx_dim;
  1185. int ip_align;
  1186. struct metadata_dst *dsa_meta[MTK_MAX_DSA_PORTS];
  1187. struct mtk_ppe *ppe[3];
  1188. struct rhashtable flow_table;
  1189. struct bpf_prog __rcu *prog;
  1190. struct {
  1191. struct delayed_work monitor_work;
  1192. u32 wdidx;
  1193. u8 wdma_hang_count;
  1194. u8 qdma_hang_count;
  1195. u8 adma_hang_count;
  1196. } reset;
  1197. };
  1198. /* struct mtk_mac - the structure that holds the info about the MACs of the
  1199. * SoC
  1200. * @id: The number of the MAC
  1201. * @interface: Interface mode kept for detecting change in hw settings
  1202. * @of_node: Our devicetree node
  1203. * @hw: Backpointer to our main datastruture
  1204. * @hw_stats: Packet statistics counter
  1205. */
  1206. struct mtk_mac {
  1207. int id;
  1208. phy_interface_t interface;
  1209. u8 ppe_idx;
  1210. int speed;
  1211. struct device_node *of_node;
  1212. struct phylink *phylink;
  1213. struct phylink_config phylink_config;
  1214. struct mtk_eth *hw;
  1215. struct mtk_hw_stats *hw_stats;
  1216. __be32 hwlro_ip[MTK_MAX_LRO_IP_CNT];
  1217. int hwlro_ip_cnt;
  1218. unsigned int syscfg0;
  1219. struct notifier_block device_notifier;
  1220. };
  1221. /* the struct describing the SoC. these are declared in the soc_xyz.c files */
  1222. extern const struct of_device_id of_mtk_match[];
  1223. static inline bool mtk_is_netsys_v1(struct mtk_eth *eth)
  1224. {
  1225. return eth->soc->version == 1;
  1226. }
  1227. static inline bool mtk_is_netsys_v2_or_greater(struct mtk_eth *eth)
  1228. {
  1229. return eth->soc->version > 1;
  1230. }
  1231. static inline bool mtk_is_netsys_v3_or_greater(struct mtk_eth *eth)
  1232. {
  1233. return eth->soc->version > 2;
  1234. }
  1235. static inline struct mtk_foe_entry *
  1236. mtk_foe_get_entry(struct mtk_ppe *ppe, u16 hash)
  1237. {
  1238. const struct mtk_soc_data *soc = ppe->eth->soc;
  1239. return ppe->foe_table + hash * soc->foe_entry_size;
  1240. }
  1241. static inline u32 mtk_get_ib1_ts_mask(struct mtk_eth *eth)
  1242. {
  1243. if (mtk_is_netsys_v2_or_greater(eth))
  1244. return MTK_FOE_IB1_BIND_TIMESTAMP_V2;
  1245. return MTK_FOE_IB1_BIND_TIMESTAMP;
  1246. }
  1247. static inline u32 mtk_get_ib1_ppoe_mask(struct mtk_eth *eth)
  1248. {
  1249. if (mtk_is_netsys_v2_or_greater(eth))
  1250. return MTK_FOE_IB1_BIND_PPPOE_V2;
  1251. return MTK_FOE_IB1_BIND_PPPOE;
  1252. }
  1253. static inline u32 mtk_get_ib1_vlan_tag_mask(struct mtk_eth *eth)
  1254. {
  1255. if (mtk_is_netsys_v2_or_greater(eth))
  1256. return MTK_FOE_IB1_BIND_VLAN_TAG_V2;
  1257. return MTK_FOE_IB1_BIND_VLAN_TAG;
  1258. }
  1259. static inline u32 mtk_get_ib1_vlan_layer_mask(struct mtk_eth *eth)
  1260. {
  1261. if (mtk_is_netsys_v2_or_greater(eth))
  1262. return MTK_FOE_IB1_BIND_VLAN_LAYER_V2;
  1263. return MTK_FOE_IB1_BIND_VLAN_LAYER;
  1264. }
  1265. static inline u32 mtk_prep_ib1_vlan_layer(struct mtk_eth *eth, u32 val)
  1266. {
  1267. if (mtk_is_netsys_v2_or_greater(eth))
  1268. return FIELD_PREP(MTK_FOE_IB1_BIND_VLAN_LAYER_V2, val);
  1269. return FIELD_PREP(MTK_FOE_IB1_BIND_VLAN_LAYER, val);
  1270. }
  1271. static inline u32 mtk_get_ib1_vlan_layer(struct mtk_eth *eth, u32 val)
  1272. {
  1273. if (mtk_is_netsys_v2_or_greater(eth))
  1274. return FIELD_GET(MTK_FOE_IB1_BIND_VLAN_LAYER_V2, val);
  1275. return FIELD_GET(MTK_FOE_IB1_BIND_VLAN_LAYER, val);
  1276. }
  1277. static inline u32 mtk_get_ib1_pkt_type_mask(struct mtk_eth *eth)
  1278. {
  1279. if (mtk_is_netsys_v2_or_greater(eth))
  1280. return MTK_FOE_IB1_PACKET_TYPE_V2;
  1281. return MTK_FOE_IB1_PACKET_TYPE;
  1282. }
  1283. static inline u32 mtk_get_ib1_pkt_type(struct mtk_eth *eth, u32 val)
  1284. {
  1285. if (mtk_is_netsys_v2_or_greater(eth))
  1286. return FIELD_GET(MTK_FOE_IB1_PACKET_TYPE_V2, val);
  1287. return FIELD_GET(MTK_FOE_IB1_PACKET_TYPE, val);
  1288. }
  1289. static inline u32 mtk_get_ib2_multicast_mask(struct mtk_eth *eth)
  1290. {
  1291. if (mtk_is_netsys_v2_or_greater(eth))
  1292. return MTK_FOE_IB2_MULTICAST_V2;
  1293. return MTK_FOE_IB2_MULTICAST;
  1294. }
  1295. static inline bool mtk_interface_mode_is_xgmii(struct mtk_eth *eth,
  1296. phy_interface_t interface)
  1297. {
  1298. if (!mtk_is_netsys_v3_or_greater(eth))
  1299. return false;
  1300. switch (interface) {
  1301. case PHY_INTERFACE_MODE_INTERNAL:
  1302. case PHY_INTERFACE_MODE_USXGMII:
  1303. case PHY_INTERFACE_MODE_10GBASER:
  1304. case PHY_INTERFACE_MODE_5GBASER:
  1305. return true;
  1306. default:
  1307. return false;
  1308. }
  1309. }
  1310. /* read the hardware status register */
  1311. void mtk_stats_update_mac(struct mtk_mac *mac);
  1312. void mtk_w32(struct mtk_eth *eth, u32 val, unsigned reg);
  1313. u32 mtk_r32(struct mtk_eth *eth, unsigned reg);
  1314. u32 mtk_m32(struct mtk_eth *eth, u32 mask, u32 set, unsigned int reg);
  1315. int mtk_gmac_sgmii_path_setup(struct mtk_eth *eth, int mac_id);
  1316. int mtk_gmac_2p5gphy_path_setup(struct mtk_eth *eth, int mac_id);
  1317. int mtk_gmac_gephy_path_setup(struct mtk_eth *eth, int mac_id);
  1318. int mtk_gmac_rgmii_path_setup(struct mtk_eth *eth, int mac_id);
  1319. int mtk_eth_offload_init(struct mtk_eth *eth, u8 id);
  1320. int mtk_eth_setup_tc(struct net_device *dev, enum tc_setup_type type,
  1321. void *type_data);
  1322. int mtk_flow_offload_cmd(struct mtk_eth *eth, struct flow_cls_offload *cls,
  1323. int ppe_index);
  1324. void mtk_flow_offload_cleanup(struct mtk_eth *eth, struct list_head *list);
  1325. void mtk_eth_set_dma_device(struct mtk_eth *eth, struct device *dma_dev);
  1326. #endif /* MTK_ETH_H */