altera_tse.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /* Altera Triple-Speed Ethernet MAC driver
  3. * Copyright (C) 2008-2014 Altera Corporation. All rights reserved
  4. *
  5. * Contributors:
  6. * Dalon Westergreen
  7. * Thomas Chou
  8. * Ian Abbott
  9. * Yuriy Kozlov
  10. * Tobias Klauser
  11. * Andriy Smolskyy
  12. * Roman Bulgakov
  13. * Dmytro Mytarchuk
  14. * Matthew Gerlach
  15. *
  16. * Original driver contributed by SLS.
  17. * Major updates contributed by GlobalLogic
  18. */
  19. #ifndef __ALTERA_TSE_H__
  20. #define __ALTERA_TSE_H__
  21. #define ALTERA_TSE_RESOURCE_NAME "altera_tse"
  22. #include <linux/bitops.h>
  23. #include <linux/if_vlan.h>
  24. #include <linux/list.h>
  25. #include <linux/netdevice.h>
  26. #include <linux/phy.h>
  27. #include <linux/phylink.h>
  28. #define ALTERA_TSE_SW_RESET_WATCHDOG_CNTR 10000
  29. #define ALTERA_TSE_MAC_FIFO_WIDTH 4 /* TX/RX FIFO width in
  30. * bytes
  31. */
  32. /* Rx FIFO default settings */
  33. #define ALTERA_TSE_RX_SECTION_EMPTY 16
  34. #define ALTERA_TSE_RX_SECTION_FULL 0
  35. #define ALTERA_TSE_RX_ALMOST_EMPTY 8
  36. #define ALTERA_TSE_RX_ALMOST_FULL 8
  37. /* Tx FIFO default settings */
  38. #define ALTERA_TSE_TX_SECTION_EMPTY 16
  39. #define ALTERA_TSE_TX_SECTION_FULL 0
  40. #define ALTERA_TSE_TX_ALMOST_EMPTY 8
  41. #define ALTERA_TSE_TX_ALMOST_FULL 3
  42. /* MAC function configuration default settings */
  43. #define ALTERA_TSE_TX_IPG_LENGTH 12
  44. #define ALTERA_TSE_PAUSE_QUANTA 0xffff
  45. #define GET_BIT_VALUE(v, bit) (((v) >> (bit)) & 0x1)
  46. /* MAC Command_Config Register Bit Definitions
  47. */
  48. #define MAC_CMDCFG_TX_ENA BIT(0)
  49. #define MAC_CMDCFG_RX_ENA BIT(1)
  50. #define MAC_CMDCFG_XON_GEN BIT(2)
  51. #define MAC_CMDCFG_ETH_SPEED BIT(3)
  52. #define MAC_CMDCFG_PROMIS_EN BIT(4)
  53. #define MAC_CMDCFG_PAD_EN BIT(5)
  54. #define MAC_CMDCFG_CRC_FWD BIT(6)
  55. #define MAC_CMDCFG_PAUSE_FWD BIT(7)
  56. #define MAC_CMDCFG_PAUSE_IGNORE BIT(8)
  57. #define MAC_CMDCFG_TX_ADDR_INS BIT(9)
  58. #define MAC_CMDCFG_HD_ENA BIT(10)
  59. #define MAC_CMDCFG_EXCESS_COL BIT(11)
  60. #define MAC_CMDCFG_LATE_COL BIT(12)
  61. #define MAC_CMDCFG_SW_RESET BIT(13)
  62. #define MAC_CMDCFG_MHASH_SEL BIT(14)
  63. #define MAC_CMDCFG_LOOP_ENA BIT(15)
  64. #define MAC_CMDCFG_TX_ADDR_SEL(v) (((v) & 0x7) << 16)
  65. #define MAC_CMDCFG_MAGIC_ENA BIT(19)
  66. #define MAC_CMDCFG_SLEEP BIT(20)
  67. #define MAC_CMDCFG_WAKEUP BIT(21)
  68. #define MAC_CMDCFG_XOFF_GEN BIT(22)
  69. #define MAC_CMDCFG_CNTL_FRM_ENA BIT(23)
  70. #define MAC_CMDCFG_NO_LGTH_CHECK BIT(24)
  71. #define MAC_CMDCFG_ENA_10 BIT(25)
  72. #define MAC_CMDCFG_RX_ERR_DISC BIT(26)
  73. #define MAC_CMDCFG_DISABLE_READ_TIMEOUT BIT(27)
  74. #define MAC_CMDCFG_CNT_RESET BIT(31)
  75. #define MAC_CMDCFG_TX_ENA_GET(v) GET_BIT_VALUE(v, 0)
  76. #define MAC_CMDCFG_RX_ENA_GET(v) GET_BIT_VALUE(v, 1)
  77. #define MAC_CMDCFG_XON_GEN_GET(v) GET_BIT_VALUE(v, 2)
  78. #define MAC_CMDCFG_ETH_SPEED_GET(v) GET_BIT_VALUE(v, 3)
  79. #define MAC_CMDCFG_PROMIS_EN_GET(v) GET_BIT_VALUE(v, 4)
  80. #define MAC_CMDCFG_PAD_EN_GET(v) GET_BIT_VALUE(v, 5)
  81. #define MAC_CMDCFG_CRC_FWD_GET(v) GET_BIT_VALUE(v, 6)
  82. #define MAC_CMDCFG_PAUSE_FWD_GET(v) GET_BIT_VALUE(v, 7)
  83. #define MAC_CMDCFG_PAUSE_IGNORE_GET(v) GET_BIT_VALUE(v, 8)
  84. #define MAC_CMDCFG_TX_ADDR_INS_GET(v) GET_BIT_VALUE(v, 9)
  85. #define MAC_CMDCFG_HD_ENA_GET(v) GET_BIT_VALUE(v, 10)
  86. #define MAC_CMDCFG_EXCESS_COL_GET(v) GET_BIT_VALUE(v, 11)
  87. #define MAC_CMDCFG_LATE_COL_GET(v) GET_BIT_VALUE(v, 12)
  88. #define MAC_CMDCFG_SW_RESET_GET(v) GET_BIT_VALUE(v, 13)
  89. #define MAC_CMDCFG_MHASH_SEL_GET(v) GET_BIT_VALUE(v, 14)
  90. #define MAC_CMDCFG_LOOP_ENA_GET(v) GET_BIT_VALUE(v, 15)
  91. #define MAC_CMDCFG_TX_ADDR_SEL_GET(v) (((v) >> 16) & 0x7)
  92. #define MAC_CMDCFG_MAGIC_ENA_GET(v) GET_BIT_VALUE(v, 19)
  93. #define MAC_CMDCFG_SLEEP_GET(v) GET_BIT_VALUE(v, 20)
  94. #define MAC_CMDCFG_WAKEUP_GET(v) GET_BIT_VALUE(v, 21)
  95. #define MAC_CMDCFG_XOFF_GEN_GET(v) GET_BIT_VALUE(v, 22)
  96. #define MAC_CMDCFG_CNTL_FRM_ENA_GET(v) GET_BIT_VALUE(v, 23)
  97. #define MAC_CMDCFG_NO_LGTH_CHECK_GET(v) GET_BIT_VALUE(v, 24)
  98. #define MAC_CMDCFG_ENA_10_GET(v) GET_BIT_VALUE(v, 25)
  99. #define MAC_CMDCFG_RX_ERR_DISC_GET(v) GET_BIT_VALUE(v, 26)
  100. #define MAC_CMDCFG_DISABLE_READ_TIMEOUT_GET(v) GET_BIT_VALUE(v, 27)
  101. #define MAC_CMDCFG_CNT_RESET_GET(v) GET_BIT_VALUE(v, 31)
  102. /* MDIO registers within MAC register Space
  103. */
  104. struct altera_tse_mdio {
  105. u32 control; /* PHY device operation control register */
  106. u32 status; /* PHY device operation status register */
  107. u32 phy_id1; /* Bits 31:16 of PHY identifier */
  108. u32 phy_id2; /* Bits 15:0 of PHY identifier */
  109. u32 auto_negotiation_advertisement; /* Auto-negotiation
  110. * advertisement
  111. * register
  112. */
  113. u32 remote_partner_base_page_ability;
  114. u32 reg6;
  115. u32 reg7;
  116. u32 reg8;
  117. u32 reg9;
  118. u32 rega;
  119. u32 regb;
  120. u32 regc;
  121. u32 regd;
  122. u32 rege;
  123. u32 regf;
  124. u32 reg10;
  125. u32 reg11;
  126. u32 reg12;
  127. u32 reg13;
  128. u32 reg14;
  129. u32 reg15;
  130. u32 reg16;
  131. u32 reg17;
  132. u32 reg18;
  133. u32 reg19;
  134. u32 reg1a;
  135. u32 reg1b;
  136. u32 reg1c;
  137. u32 reg1d;
  138. u32 reg1e;
  139. u32 reg1f;
  140. };
  141. /* MAC register Space. Note that some of these registers may or may not be
  142. * present depending upon options chosen by the user when the core was
  143. * configured and built. Please consult the Altera Triple Speed Ethernet User
  144. * Guide for details.
  145. */
  146. struct altera_tse_mac {
  147. /* Bits 15:0: MegaCore function revision (0x0800). Bit 31:16: Customer
  148. * specific revision
  149. */
  150. u32 megacore_revision;
  151. /* Provides a memory location for user applications to test the device
  152. * memory operation.
  153. */
  154. u32 scratch_pad;
  155. /* The host processor uses this register to control and configure the
  156. * MAC block
  157. */
  158. u32 command_config;
  159. /* 32-bit primary MAC address word 0 bits 0 to 31 of the primary
  160. * MAC address
  161. */
  162. u32 mac_addr_0;
  163. /* 32-bit primary MAC address word 1 bits 32 to 47 of the primary
  164. * MAC address
  165. */
  166. u32 mac_addr_1;
  167. /* 14-bit maximum frame length. The MAC receive logic */
  168. u32 frm_length;
  169. /* The pause quanta is used in each pause frame sent to a remote
  170. * Ethernet device, in increments of 512 Ethernet bit times
  171. */
  172. u32 pause_quanta;
  173. /* 12-bit receive FIFO section-empty threshold */
  174. u32 rx_section_empty;
  175. /* 12-bit receive FIFO section-full threshold */
  176. u32 rx_section_full;
  177. /* 12-bit transmit FIFO section-empty threshold */
  178. u32 tx_section_empty;
  179. /* 12-bit transmit FIFO section-full threshold */
  180. u32 tx_section_full;
  181. /* 12-bit receive FIFO almost-empty threshold */
  182. u32 rx_almost_empty;
  183. /* 12-bit receive FIFO almost-full threshold */
  184. u32 rx_almost_full;
  185. /* 12-bit transmit FIFO almost-empty threshold */
  186. u32 tx_almost_empty;
  187. /* 12-bit transmit FIFO almost-full threshold */
  188. u32 tx_almost_full;
  189. /* MDIO address of PHY Device 0. Bits 0 to 4 hold a 5-bit PHY address */
  190. u32 mdio_phy0_addr;
  191. /* MDIO address of PHY Device 1. Bits 0 to 4 hold a 5-bit PHY address */
  192. u32 mdio_phy1_addr;
  193. /* Bit[15:0]—16-bit holdoff quanta */
  194. u32 holdoff_quant;
  195. /* only if 100/1000 BaseX PCS, reserved otherwise */
  196. u32 reserved1[5];
  197. /* Minimum IPG between consecutive transmit frame in terms of bytes */
  198. u32 tx_ipg_length;
  199. /* IEEE 802.3 oEntity Managed Object Support */
  200. /* The MAC addresses */
  201. u32 mac_id_1;
  202. u32 mac_id_2;
  203. /* Number of frames transmitted without error including pause frames */
  204. u32 frames_transmitted_ok;
  205. /* Number of frames received without error including pause frames */
  206. u32 frames_received_ok;
  207. /* Number of frames received with a CRC error */
  208. u32 frames_check_sequence_errors;
  209. /* Frame received with an alignment error */
  210. u32 alignment_errors;
  211. /* Sum of payload and padding octets of frames transmitted without
  212. * error
  213. */
  214. u32 octets_transmitted_ok;
  215. /* Sum of payload and padding octets of frames received without error */
  216. u32 octets_received_ok;
  217. /* IEEE 802.3 oPausedEntity Managed Object Support */
  218. /* Number of transmitted pause frames */
  219. u32 tx_pause_mac_ctrl_frames;
  220. /* Number of Received pause frames */
  221. u32 rx_pause_mac_ctrl_frames;
  222. /* IETF MIB (MIB-II) Object Support */
  223. /* Number of frames received with error */
  224. u32 if_in_errors;
  225. /* Number of frames transmitted with error */
  226. u32 if_out_errors;
  227. /* Number of valid received unicast frames */
  228. u32 if_in_ucast_pkts;
  229. /* Number of valid received multicasts frames (without pause) */
  230. u32 if_in_multicast_pkts;
  231. /* Number of valid received broadcast frames */
  232. u32 if_in_broadcast_pkts;
  233. u32 if_out_discards;
  234. /* The number of valid unicast frames transmitted */
  235. u32 if_out_ucast_pkts;
  236. /* The number of valid multicast frames transmitted,
  237. * excluding pause frames
  238. */
  239. u32 if_out_multicast_pkts;
  240. u32 if_out_broadcast_pkts;
  241. /* IETF RMON MIB Object Support */
  242. /* Counts the number of dropped packets due to internal errors
  243. * of the MAC client.
  244. */
  245. u32 ether_stats_drop_events;
  246. /* Total number of bytes received. Good and bad frames. */
  247. u32 ether_stats_octets;
  248. /* Total number of packets received. Counts good and bad packets. */
  249. u32 ether_stats_pkts;
  250. /* Number of packets received with less than 64 bytes. */
  251. u32 ether_stats_undersize_pkts;
  252. /* The number of frames received that are longer than the
  253. * value configured in the frm_length register
  254. */
  255. u32 ether_stats_oversize_pkts;
  256. /* Number of received packet with 64 bytes */
  257. u32 ether_stats_pkts_64_octets;
  258. /* Frames (good and bad) with 65 to 127 bytes */
  259. u32 ether_stats_pkts_65to127_octets;
  260. /* Frames (good and bad) with 128 to 255 bytes */
  261. u32 ether_stats_pkts_128to255_octets;
  262. /* Frames (good and bad) with 256 to 511 bytes */
  263. u32 ether_stats_pkts_256to511_octets;
  264. /* Frames (good and bad) with 512 to 1023 bytes */
  265. u32 ether_stats_pkts_512to1023_octets;
  266. /* Frames (good and bad) with 1024 to 1518 bytes */
  267. u32 ether_stats_pkts_1024to1518_octets;
  268. /* Any frame length from 1519 to the maximum length configured in the
  269. * frm_length register, if it is greater than 1518
  270. */
  271. u32 ether_stats_pkts_1519tox_octets;
  272. /* Too long frames with CRC error */
  273. u32 ether_stats_jabbers;
  274. /* Too short frames with CRC error */
  275. u32 ether_stats_fragments;
  276. u32 reserved2;
  277. /* FIFO control register */
  278. u32 tx_cmd_stat;
  279. u32 rx_cmd_stat;
  280. /* Extended Statistics Counters */
  281. u32 msb_octets_transmitted_ok;
  282. u32 msb_octets_received_ok;
  283. u32 msb_ether_stats_octets;
  284. u32 reserved3;
  285. /* Multicast address resolution table, mapped in the controller address
  286. * space
  287. */
  288. u32 hash_table[64];
  289. /* Registers 0 to 31 within PHY device 0/1 connected to the MDIO PHY
  290. * management interface
  291. */
  292. struct altera_tse_mdio mdio_phy0;
  293. struct altera_tse_mdio mdio_phy1;
  294. /* 4 Supplemental MAC Addresses */
  295. u32 supp_mac_addr_0_0;
  296. u32 supp_mac_addr_0_1;
  297. u32 supp_mac_addr_1_0;
  298. u32 supp_mac_addr_1_1;
  299. u32 supp_mac_addr_2_0;
  300. u32 supp_mac_addr_2_1;
  301. u32 supp_mac_addr_3_0;
  302. u32 supp_mac_addr_3_1;
  303. u32 reserved4[8];
  304. /* IEEE 1588v2 Feature */
  305. u32 tx_period;
  306. u32 tx_adjust_fns;
  307. u32 tx_adjust_ns;
  308. u32 rx_period;
  309. u32 rx_adjust_fns;
  310. u32 rx_adjust_ns;
  311. u32 reserved5[42];
  312. };
  313. #define tse_csroffs(a) (offsetof(struct altera_tse_mac, a))
  314. /* Transmit and Receive Command Registers Bit Definitions
  315. */
  316. #define ALTERA_TSE_TX_CMD_STAT_OMIT_CRC BIT(17)
  317. #define ALTERA_TSE_TX_CMD_STAT_TX_SHIFT16 BIT(18)
  318. #define ALTERA_TSE_RX_CMD_STAT_RX_SHIFT16 BIT(25)
  319. /* Wrapper around a pointer to a socket buffer,
  320. * so a DMA handle can be stored along with the buffer
  321. */
  322. struct tse_buffer {
  323. struct list_head lh;
  324. struct sk_buff *skb;
  325. dma_addr_t dma_addr;
  326. u32 len;
  327. int mapped_as_page;
  328. };
  329. struct altera_tse_private;
  330. #define ALTERA_DTYPE_SGDMA 1
  331. #define ALTERA_DTYPE_MSGDMA 2
  332. /* standard DMA interface for SGDMA and MSGDMA */
  333. struct altera_dmaops {
  334. int altera_dtype;
  335. int dmamask;
  336. void (*reset_dma)(struct altera_tse_private *);
  337. void (*enable_txirq)(struct altera_tse_private *);
  338. void (*enable_rxirq)(struct altera_tse_private *);
  339. void (*disable_txirq)(struct altera_tse_private *);
  340. void (*disable_rxirq)(struct altera_tse_private *);
  341. void (*clear_txirq)(struct altera_tse_private *);
  342. void (*clear_rxirq)(struct altera_tse_private *);
  343. int (*tx_buffer)(struct altera_tse_private *, struct tse_buffer *);
  344. u32 (*tx_completions)(struct altera_tse_private *);
  345. void (*add_rx_desc)(struct altera_tse_private *, struct tse_buffer *);
  346. u32 (*get_rx_status)(struct altera_tse_private *);
  347. int (*init_dma)(struct altera_tse_private *);
  348. void (*uninit_dma)(struct altera_tse_private *);
  349. void (*start_rxdma)(struct altera_tse_private *);
  350. };
  351. /* This structure is private to each device.
  352. */
  353. struct altera_tse_private {
  354. struct net_device *dev;
  355. struct device *device;
  356. struct napi_struct napi;
  357. /* MAC address space */
  358. struct altera_tse_mac __iomem *mac_dev;
  359. /* TSE Revision */
  360. u32 revision;
  361. /* mSGDMA Rx Dispatcher address space */
  362. void __iomem *rx_dma_csr;
  363. void __iomem *rx_dma_desc;
  364. void __iomem *rx_dma_resp;
  365. /* mSGDMA Tx Dispatcher address space */
  366. void __iomem *tx_dma_csr;
  367. void __iomem *tx_dma_desc;
  368. /* SGMII PCS address space */
  369. void __iomem *pcs_base;
  370. /* Rx buffers queue */
  371. struct tse_buffer *rx_ring;
  372. u32 rx_cons;
  373. u32 rx_prod;
  374. u32 rx_ring_size;
  375. u32 rx_dma_buf_sz;
  376. /* Tx ring buffer */
  377. struct tse_buffer *tx_ring;
  378. u32 tx_prod;
  379. u32 tx_cons;
  380. u32 tx_ring_size;
  381. /* Interrupts */
  382. u32 tx_irq;
  383. u32 rx_irq;
  384. /* RX/TX MAC FIFO configs */
  385. u32 tx_fifo_depth;
  386. u32 rx_fifo_depth;
  387. /* Hash filter settings */
  388. u32 hash_filter;
  389. u32 added_unicast;
  390. /* Descriptor memory info for managing SGDMA */
  391. u32 txdescmem;
  392. u32 rxdescmem;
  393. dma_addr_t rxdescmem_busaddr;
  394. dma_addr_t txdescmem_busaddr;
  395. u32 txctrlreg;
  396. u32 rxctrlreg;
  397. dma_addr_t rxdescphys;
  398. dma_addr_t txdescphys;
  399. struct list_head txlisthd;
  400. struct list_head rxlisthd;
  401. /* MAC command_config register protection */
  402. spinlock_t mac_cfg_lock;
  403. /* Tx path protection */
  404. spinlock_t tx_lock;
  405. /* Rx DMA & interrupt control protection */
  406. spinlock_t rxdma_irq_lock;
  407. /* PHY */
  408. int phy_addr; /* PHY's MDIO address, -1 for autodetection */
  409. phy_interface_t phy_iface;
  410. struct mii_bus *mdio;
  411. int oldspeed;
  412. int oldduplex;
  413. int oldlink;
  414. /* ethtool msglvl option */
  415. u32 msg_enable;
  416. struct altera_dmaops *dmaops;
  417. struct phylink *phylink;
  418. struct phylink_config phylink_config;
  419. struct phylink_pcs *pcs;
  420. };
  421. /* Function prototypes
  422. */
  423. void altera_tse_set_ethtool_ops(struct net_device *);
  424. static inline
  425. u32 csrrd32(void __iomem *mac, size_t offs)
  426. {
  427. void __iomem *paddr = (void __iomem *)((uintptr_t)mac + offs);
  428. return readl(paddr);
  429. }
  430. static inline
  431. u16 csrrd16(void __iomem *mac, size_t offs)
  432. {
  433. void __iomem *paddr = (void __iomem *)((uintptr_t)mac + offs);
  434. return readw(paddr);
  435. }
  436. static inline
  437. u8 csrrd8(void __iomem *mac, size_t offs)
  438. {
  439. void __iomem *paddr = (void __iomem *)((uintptr_t)mac + offs);
  440. return readb(paddr);
  441. }
  442. static inline
  443. void csrwr32(u32 val, void __iomem *mac, size_t offs)
  444. {
  445. void __iomem *paddr = (void __iomem *)((uintptr_t)mac + offs);
  446. writel(val, paddr);
  447. }
  448. static inline
  449. void csrwr16(u16 val, void __iomem *mac, size_t offs)
  450. {
  451. void __iomem *paddr = (void __iomem *)((uintptr_t)mac + offs);
  452. writew(val, paddr);
  453. }
  454. static inline
  455. void csrwr8(u8 val, void __iomem *mac, size_t offs)
  456. {
  457. void __iomem *paddr = (void __iomem *)((uintptr_t)mac + offs);
  458. writeb(val, paddr);
  459. }
  460. #endif /* __ALTERA_TSE_H__ */