mtk_eth_soc.h 14 KB

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